1/******************************************************************************
2** This file is an amalgamation of many separate C source files from SQLite
3** version 3.37.2. By combining all the individual C code files into this
4** single large file, the entire code can be compiled as a single translation
5** unit. This allows many compilers to do optimizations that would not be
6** possible if the files were compiled separately. Performance improvements
7** of 5% or more are commonly seen when SQLite is compiled as a single
8** translation unit.
9**
10** This file is all you need to compile SQLite. To use SQLite in other
11** programs, you need this file and the "sqlite3.h" header file that defines
12** the programming interface to the SQLite library. (If you do not have
13** the "sqlite3.h" header file at hand, you will find a copy embedded within
14** the text of this file. Search for "Begin file sqlite3.h" to find the start
15** of the embedded sqlite3.h header file.) Additional code files may be needed
16** if you want a wrapper to interface SQLite with your choice of programming
17** language. The code for the "sqlite3" command-line shell is also in a
18** separate file. This file contains only code for the core SQLite library.
19*/
20#define SQLITE_CORE 1
21#define SQLITE_AMALGAMATION 1
22#ifndef SQLITE_PRIVATE
23# define SQLITE_PRIVATE static
24#endif
25/************** Begin file sqliteInt.h ***************************************/
26/*
27** 2001 September 15
28**
29** The author disclaims copyright to this source code. In place of
30** a legal notice, here is a blessing:
31**
32** May you do good and not evil.
33** May you find forgiveness for yourself and forgive others.
34** May you share freely, never taking more than you give.
35**
36*************************************************************************
37** Internal interface definitions for SQLite.
38**
39*/
40#ifndef SQLITEINT_H
41#define SQLITEINT_H
42
43/* Special Comments:
44**
45** Some comments have special meaning to the tools that measure test
46** coverage:
47**
48** NO_TEST - The branches on this line are not
49** measured by branch coverage. This is
50** used on lines of code that actually
51** implement parts of coverage testing.
52**
53** OPTIMIZATION-IF-TRUE - This branch is allowed to alway be false
54** and the correct answer is still obtained,
55** though perhaps more slowly.
56**
57** OPTIMIZATION-IF-FALSE - This branch is allowed to alway be true
58** and the correct answer is still obtained,
59** though perhaps more slowly.
60**
61** PREVENTS-HARMLESS-OVERREAD - This branch prevents a buffer overread
62** that would be harmless and undetectable
63** if it did occur.
64**
65** In all cases, the special comment must be enclosed in the usual
66** slash-asterisk...asterisk-slash comment marks, with no spaces between the
67** asterisks and the comment text.
68*/
69
70/*
71** Make sure the Tcl calling convention macro is defined. This macro is
72** only used by test code and Tcl integration code.
73*/
74#ifndef SQLITE_TCLAPI
75# define SQLITE_TCLAPI
76#endif
77
78/*
79** Include the header file used to customize the compiler options for MSVC.
80** This should be done first so that it can successfully prevent spurious
81** compiler warnings due to subsequent content in this file and other files
82** that are included by this file.
83*/
84/************** Include msvc.h in the middle of sqliteInt.h ******************/
85/************** Begin file msvc.h ********************************************/
86/*
87** 2015 January 12
88**
89** The author disclaims copyright to this source code. In place of
90** a legal notice, here is a blessing:
91**
92** May you do good and not evil.
93** May you find forgiveness for yourself and forgive others.
94** May you share freely, never taking more than you give.
95**
96******************************************************************************
97**
98** This file contains code that is specific to MSVC.
99*/
100#ifndef SQLITE_MSVC_H
101#define SQLITE_MSVC_H
102
103#if defined(_MSC_VER)
104#pragma warning(disable : 4054)
105#pragma warning(disable : 4055)
106#pragma warning(disable : 4100)
107#pragma warning(disable : 4127)
108#pragma warning(disable : 4130)
109#pragma warning(disable : 4152)
110#pragma warning(disable : 4189)
111#pragma warning(disable : 4206)
112#pragma warning(disable : 4210)
113#pragma warning(disable : 4232)
114#pragma warning(disable : 4244)
115#pragma warning(disable : 4305)
116#pragma warning(disable : 4306)
117#pragma warning(disable : 4702)
118#pragma warning(disable : 4706)
119#endif /* defined(_MSC_VER) */
120
121#if defined(_MSC_VER) && !defined(_WIN64)
122#undef SQLITE_4_BYTE_ALIGNED_MALLOC
123#define SQLITE_4_BYTE_ALIGNED_MALLOC
124#endif /* defined(_MSC_VER) && !defined(_WIN64) */
125
126#endif /* SQLITE_MSVC_H */
127
128/************** End of msvc.h ************************************************/
129/************** Continuing where we left off in sqliteInt.h ******************/
130
131/*
132** Special setup for VxWorks
133*/
134/************** Include vxworks.h in the middle of sqliteInt.h ***************/
135/************** Begin file vxworks.h *****************************************/
136/*
137** 2015-03-02
138**
139** The author disclaims copyright to this source code. In place of
140** a legal notice, here is a blessing:
141**
142** May you do good and not evil.
143** May you find forgiveness for yourself and forgive others.
144** May you share freely, never taking more than you give.
145**
146******************************************************************************
147**
148** This file contains code that is specific to Wind River's VxWorks
149*/
150#if defined(__RTP__) || defined(_WRS_KERNEL)
151/* This is VxWorks. Set up things specially for that OS
152*/
153#include <vxWorks.h>
154#include <pthread.h> /* amalgamator: dontcache */
155#define OS_VXWORKS 1
156#define SQLITE_OS_OTHER 0
157#define SQLITE_HOMEGROWN_RECURSIVE_MUTEX 1
158#define SQLITE_OMIT_LOAD_EXTENSION 1
159#define SQLITE_ENABLE_LOCKING_STYLE 0
160#define HAVE_UTIME 1
161#else
162/* This is not VxWorks. */
163#define OS_VXWORKS 0
164#define HAVE_FCHOWN 1
165#define HAVE_READLINK 1
166#define HAVE_LSTAT 1
167#endif /* defined(_WRS_KERNEL) */
168
169/************** End of vxworks.h *********************************************/
170/************** Continuing where we left off in sqliteInt.h ******************/
171
172/*
173** These #defines should enable >2GB file support on POSIX if the
174** underlying operating system supports it. If the OS lacks
175** large file support, or if the OS is windows, these should be no-ops.
176**
177** Ticket #2739: The _LARGEFILE_SOURCE macro must appear before any
178** system #includes. Hence, this block of code must be the very first
179** code in all source files.
180**
181** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
182** on the compiler command line. This is necessary if you are compiling
183** on a recent machine (ex: Red Hat 7.2) but you want your code to work
184** on an older machine (ex: Red Hat 6.0). If you compile on Red Hat 7.2
185** without this option, LFS is enable. But LFS does not exist in the kernel
186** in Red Hat 6.0, so the code won't work. Hence, for maximum binary
187** portability you should omit LFS.
188**
189** The previous paragraph was written in 2005. (This paragraph is written
190** on 2008-11-28.) These days, all Linux kernels support large files, so
191** you should probably leave LFS enabled. But some embedded platforms might
192** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
193**
194** Similar is true for Mac OS X. LFS is only supported on Mac OS X 9 and later.
195*/
196#ifndef SQLITE_DISABLE_LFS
197# define _LARGE_FILE 1
198# ifndef _FILE_OFFSET_BITS
199# define _FILE_OFFSET_BITS 64
200# endif
201# define _LARGEFILE_SOURCE 1
202#endif
203
204/* The GCC_VERSION and MSVC_VERSION macros are used to
205** conditionally include optimizations for each of these compilers. A
206** value of 0 means that compiler is not being used. The
207** SQLITE_DISABLE_INTRINSIC macro means do not use any compiler-specific
208** optimizations, and hence set all compiler macros to 0
209**
210** There was once also a CLANG_VERSION macro. However, we learn that the
211** version numbers in clang are for "marketing" only and are inconsistent
212** and unreliable. Fortunately, all versions of clang also recognize the
213** gcc version numbers and have reasonable settings for gcc version numbers,
214** so the GCC_VERSION macro will be set to a correct non-zero value even
215** when compiling with clang.
216*/
217#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
218# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
219#else
220# define GCC_VERSION 0
221#endif
222#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
223# define MSVC_VERSION _MSC_VER
224#else
225# define MSVC_VERSION 0
226#endif
227
228/*
229** Some C99 functions in "math.h" are only present for MSVC when its version
230** is associated with Visual Studio 2013 or higher.
231*/
232#ifndef SQLITE_HAVE_C99_MATH_FUNCS
233# if MSVC_VERSION==0 || MSVC_VERSION>=1800
234# define SQLITE_HAVE_C99_MATH_FUNCS (1)
235# else
236# define SQLITE_HAVE_C99_MATH_FUNCS (0)
237# endif
238#endif
239
240/* Needed for various definitions... */
241#if defined(__GNUC__) && !defined(_GNU_SOURCE)
242# define _GNU_SOURCE
243#endif
244
245#if defined(__OpenBSD__) && !defined(_BSD_SOURCE)
246# define _BSD_SOURCE
247#endif
248
249/*
250** Macro to disable warnings about missing "break" at the end of a "case".
251*/
252#if GCC_VERSION>=7000000
253# define deliberate_fall_through __attribute__((fallthrough));
254#else
255# define deliberate_fall_through
256#endif
257
258/*
259** For MinGW, check to see if we can include the header file containing its
260** version information, among other things. Normally, this internal MinGW
261** header file would [only] be included automatically by other MinGW header
262** files; however, the contained version information is now required by this
263** header file to work around binary compatibility issues (see below) and
264** this is the only known way to reliably obtain it. This entire #if block
265** would be completely unnecessary if there was any other way of detecting
266** MinGW via their preprocessor (e.g. if they customized their GCC to define
267** some MinGW-specific macros). When compiling for MinGW, either the
268** _HAVE_MINGW_H or _HAVE__MINGW_H (note the extra underscore) macro must be
269** defined; otherwise, detection of conditions specific to MinGW will be
270** disabled.
271*/
272#if defined(_HAVE_MINGW_H)
273# include "mingw.h"
274#elif defined(_HAVE__MINGW_H)
275# include "_mingw.h"
276#endif
277
278/*
279** For MinGW version 4.x (and higher), check to see if the _USE_32BIT_TIME_T
280** define is required to maintain binary compatibility with the MSVC runtime
281** library in use (e.g. for Windows XP).
282*/
283#if !defined(_USE_32BIT_TIME_T) && !defined(_USE_64BIT_TIME_T) && \
284 defined(_WIN32) && !defined(_WIN64) && \
285 defined(__MINGW_MAJOR_VERSION) && __MINGW_MAJOR_VERSION >= 4 && \
286 defined(__MSVCRT__)
287# define _USE_32BIT_TIME_T
288#endif
289
290/* Optionally #include a user-defined header, whereby compilation options
291** may be set prior to where they take effect, but after platform setup.
292** If SQLITE_CUSTOM_INCLUDE=? is defined, its value names the #include
293** file.
294*/
295#ifdef SQLITE_CUSTOM_INCLUDE
296# define INC_STRINGIFY_(f) #f
297# define INC_STRINGIFY(f) INC_STRINGIFY_(f)
298# include INC_STRINGIFY(SQLITE_CUSTOM_INCLUDE)
299#endif
300
301/* The public SQLite interface. The _FILE_OFFSET_BITS macro must appear
302** first in QNX. Also, the _USE_32BIT_TIME_T macro must appear first for
303** MinGW.
304*/
305/************** Include sqlite3.h in the middle of sqliteInt.h ***************/
306/************** Begin file sqlite3.h *****************************************/
307/*
308** 2001-09-15
309**
310** The author disclaims copyright to this source code. In place of
311** a legal notice, here is a blessing:
312**
313** May you do good and not evil.
314** May you find forgiveness for yourself and forgive others.
315** May you share freely, never taking more than you give.
316**
317*************************************************************************
318** This header file defines the interface that the SQLite library
319** presents to client programs. If a C-function, structure, datatype,
320** or constant definition does not appear in this file, then it is
321** not a published API of SQLite, is subject to change without
322** notice, and should not be referenced by programs that use SQLite.
323**
324** Some of the definitions that are in this file are marked as
325** "experimental". Experimental interfaces are normally new
326** features recently added to SQLite. We do not anticipate changes
327** to experimental interfaces but reserve the right to make minor changes
328** if experience from use "in the wild" suggest such changes are prudent.
329**
330** The official C-language API documentation for SQLite is derived
331** from comments in this file. This file is the authoritative source
332** on how SQLite interfaces are supposed to operate.
333**
334** The name of this file under configuration management is "sqlite.h.in".
335** The makefile makes some minor changes to this file (such as inserting
336** the version number) and changes its name to "sqlite3.h" as
337** part of the build process.
338*/
339#ifndef SQLITE3_H
340#define SQLITE3_H
341#include <stdarg.h> /* Needed for the definition of va_list */
342
343/*
344** Make sure we can call this stuff from C++.
345*/
346#if 0
347extern "C" {
348#endif
349
350
351/*
352** Facilitate override of interface linkage and calling conventions.
353** Be aware that these macros may not be used within this particular
354** translation of the amalgamation and its associated header file.
355**
356** The SQLITE_EXTERN and SQLITE_API macros are used to instruct the
357** compiler that the target identifier should have external linkage.
358**
359** The SQLITE_CDECL macro is used to set the calling convention for
360** public functions that accept a variable number of arguments.
361**
362** The SQLITE_APICALL macro is used to set the calling convention for
363** public functions that accept a fixed number of arguments.
364**
365** The SQLITE_STDCALL macro is no longer used and is now deprecated.
366**
367** The SQLITE_CALLBACK macro is used to set the calling convention for
368** function pointers.
369**
370** The SQLITE_SYSAPI macro is used to set the calling convention for
371** functions provided by the operating system.
372**
373** Currently, the SQLITE_CDECL, SQLITE_APICALL, SQLITE_CALLBACK, and
374** SQLITE_SYSAPI macros are used only when building for environments
375** that require non-default calling conventions.
376*/
377#ifndef SQLITE_EXTERN
378# define SQLITE_EXTERN extern
379#endif
380#ifndef SQLITE_API
381# define SQLITE_API
382#endif
383#ifndef SQLITE_CDECL
384# define SQLITE_CDECL
385#endif
386#ifndef SQLITE_APICALL
387# define SQLITE_APICALL
388#endif
389#ifndef SQLITE_STDCALL
390# define SQLITE_STDCALL SQLITE_APICALL
391#endif
392#ifndef SQLITE_CALLBACK
393# define SQLITE_CALLBACK
394#endif
395#ifndef SQLITE_SYSAPI
396# define SQLITE_SYSAPI
397#endif
398
399/*
400** These no-op macros are used in front of interfaces to mark those
401** interfaces as either deprecated or experimental. New applications
402** should not use deprecated interfaces - they are supported for backwards
403** compatibility only. Application writers should be aware that
404** experimental interfaces are subject to change in point releases.
405**
406** These macros used to resolve to various kinds of compiler magic that
407** would generate warning messages when they were used. But that
408** compiler magic ended up generating such a flurry of bug reports
409** that we have taken it all out and gone back to using simple
410** noop macros.
411*/
412#define SQLITE_DEPRECATED
413#define SQLITE_EXPERIMENTAL
414
415/*
416** Ensure these symbols were not defined by some previous header file.
417*/
418#ifdef SQLITE_VERSION
419# undef SQLITE_VERSION
420#endif
421#ifdef SQLITE_VERSION_NUMBER
422# undef SQLITE_VERSION_NUMBER
423#endif
424
425/*
426** CAPI3REF: Compile-Time Library Version Numbers
427**
428** ^(The [SQLITE_VERSION] C preprocessor macro in the sqlite3.h header
429** evaluates to a string literal that is the SQLite version in the
430** format "X.Y.Z" where X is the major version number (always 3 for
431** SQLite3) and Y is the minor version number and Z is the release number.)^
432** ^(The [SQLITE_VERSION_NUMBER] C preprocessor macro resolves to an integer
433** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same
434** numbers used in [SQLITE_VERSION].)^
435** The SQLITE_VERSION_NUMBER for any given release of SQLite will also
436** be larger than the release from which it is derived. Either Y will
437** be held constant and Z will be incremented or else Y will be incremented
438** and Z will be reset to zero.
439**
440** Since [version 3.6.18] ([dateof:3.6.18]),
441** SQLite source code has been stored in the
442** <a href="http://www.fossil-scm.org/">Fossil configuration management
443** system</a>. ^The SQLITE_SOURCE_ID macro evaluates to
444** a string which identifies a particular check-in of SQLite
445** within its configuration management system. ^The SQLITE_SOURCE_ID
446** string contains the date and time of the check-in (UTC) and a SHA1
447** or SHA3-256 hash of the entire source tree. If the source code has
448** been edited in any way since it was last checked in, then the last
449** four hexadecimal digits of the hash may be modified.
450**
451** See also: [sqlite3_libversion()],
452** [sqlite3_libversion_number()], [sqlite3_sourceid()],
453** [sqlite_version()] and [sqlite_source_id()].
454*/
455#define SQLITE_VERSION "3.37.2"
456#define SQLITE_VERSION_NUMBER 3037002
457#define SQLITE_SOURCE_ID "2022-01-06 13:25:41 872ba256cbf61d9290b571c0e6d82a20c224ca3ad82971edc46b29818d5d17a0"
458
459/*
460** CAPI3REF: Run-Time Library Version Numbers
461** KEYWORDS: sqlite3_version sqlite3_sourceid
462**
463** These interfaces provide the same information as the [SQLITE_VERSION],
464** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
465** but are associated with the library instead of the header file. ^(Cautious
466** programmers might include assert() statements in their application to
467** verify that values returned by these interfaces match the macros in
468** the header, and thus ensure that the application is
469** compiled with matching library and header files.
470**
471** <blockquote><pre>
472** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
473** assert( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,80)==0 );
474** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );
475** </pre></blockquote>)^
476**
477** ^The sqlite3_version[] string constant contains the text of [SQLITE_VERSION]
478** macro. ^The sqlite3_libversion() function returns a pointer to the
479** to the sqlite3_version[] string constant. The sqlite3_libversion()
480** function is provided for use in DLLs since DLL users usually do not have
481** direct access to string constants within the DLL. ^The
482** sqlite3_libversion_number() function returns an integer equal to
483** [SQLITE_VERSION_NUMBER]. ^(The sqlite3_sourceid() function returns
484** a pointer to a string constant whose value is the same as the
485** [SQLITE_SOURCE_ID] C preprocessor macro. Except if SQLite is built
486** using an edited copy of [the amalgamation], then the last four characters
487** of the hash might be different from [SQLITE_SOURCE_ID].)^
488**
489** See also: [sqlite_version()] and [sqlite_source_id()].
490*/
491SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
492SQLITE_API const char *sqlite3_libversion(void);
493SQLITE_API const char *sqlite3_sourceid(void);
494SQLITE_API int sqlite3_libversion_number(void);
495
496/*
497** CAPI3REF: Run-Time Library Compilation Options Diagnostics
498**
499** ^The sqlite3_compileoption_used() function returns 0 or 1
500** indicating whether the specified option was defined at
501** compile time. ^The SQLITE_ prefix may be omitted from the
502** option name passed to sqlite3_compileoption_used().
503**
504** ^The sqlite3_compileoption_get() function allows iterating
505** over the list of options that were defined at compile time by
506** returning the N-th compile time option string. ^If N is out of range,
507** sqlite3_compileoption_get() returns a NULL pointer. ^The SQLITE_
508** prefix is omitted from any strings returned by
509** sqlite3_compileoption_get().
510**
511** ^Support for the diagnostic functions sqlite3_compileoption_used()
512** and sqlite3_compileoption_get() may be omitted by specifying the
513** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time.
514**
515** See also: SQL functions [sqlite_compileoption_used()] and
516** [sqlite_compileoption_get()] and the [compile_options pragma].
517*/
518#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
519SQLITE_API int sqlite3_compileoption_used(const char *zOptName);
520SQLITE_API const char *sqlite3_compileoption_get(int N);
521#else
522# define sqlite3_compileoption_used(X) 0
523# define sqlite3_compileoption_get(X) ((void*)0)
524#endif
525
526/*
527** CAPI3REF: Test To See If The Library Is Threadsafe
528**
529** ^The sqlite3_threadsafe() function returns zero if and only if
530** SQLite was compiled with mutexing code omitted due to the
531** [SQLITE_THREADSAFE] compile-time option being set to 0.
532**
533** SQLite can be compiled with or without mutexes. When
534** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
535** are enabled and SQLite is threadsafe. When the
536** [SQLITE_THREADSAFE] macro is 0,
537** the mutexes are omitted. Without the mutexes, it is not safe
538** to use SQLite concurrently from more than one thread.
539**
540** Enabling mutexes incurs a measurable performance penalty.
541** So if speed is of utmost importance, it makes sense to disable
542** the mutexes. But for maximum safety, mutexes should be enabled.
543** ^The default behavior is for mutexes to be enabled.
544**
545** This interface can be used by an application to make sure that the
546** version of SQLite that it is linking against was compiled with
547** the desired setting of the [SQLITE_THREADSAFE] macro.
548**
549** This interface only reports on the compile-time mutex setting
550** of the [SQLITE_THREADSAFE] flag. If SQLite is compiled with
551** SQLITE_THREADSAFE=1 or =2 then mutexes are enabled by default but
552** can be fully or partially disabled using a call to [sqlite3_config()]
553** with the verbs [SQLITE_CONFIG_SINGLETHREAD], [SQLITE_CONFIG_MULTITHREAD],
554** or [SQLITE_CONFIG_SERIALIZED]. ^(The return value of the
555** sqlite3_threadsafe() function shows only the compile-time setting of
556** thread safety, not any run-time changes to that setting made by
557** sqlite3_config(). In other words, the return value from sqlite3_threadsafe()
558** is unchanged by calls to sqlite3_config().)^
559**
560** See the [threading mode] documentation for additional information.
561*/
562SQLITE_API int sqlite3_threadsafe(void);
563
564/*
565** CAPI3REF: Database Connection Handle
566** KEYWORDS: {database connection} {database connections}
567**
568** Each open SQLite database is represented by a pointer to an instance of
569** the opaque structure named "sqlite3". It is useful to think of an sqlite3
570** pointer as an object. The [sqlite3_open()], [sqlite3_open16()], and
571** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
572** and [sqlite3_close_v2()] are its destructors. There are many other
573** interfaces (such as
574** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
575** [sqlite3_busy_timeout()] to name but three) that are methods on an
576** sqlite3 object.
577*/
578typedef struct sqlite3 sqlite3;
579
580/*
581** CAPI3REF: 64-Bit Integer Types
582** KEYWORDS: sqlite_int64 sqlite_uint64
583**
584** Because there is no cross-platform way to specify 64-bit integer types
585** SQLite includes typedefs for 64-bit signed and unsigned integers.
586**
587** The sqlite3_int64 and sqlite3_uint64 are the preferred type definitions.
588** The sqlite_int64 and sqlite_uint64 types are supported for backwards
589** compatibility only.
590**
591** ^The sqlite3_int64 and sqlite_int64 types can store integer values
592** between -9223372036854775808 and +9223372036854775807 inclusive. ^The
593** sqlite3_uint64 and sqlite_uint64 types can store integer values
594** between 0 and +18446744073709551615 inclusive.
595*/
596#ifdef SQLITE_INT64_TYPE
597 typedef SQLITE_INT64_TYPE sqlite_int64;
598# ifdef SQLITE_UINT64_TYPE
599 typedef SQLITE_UINT64_TYPE sqlite_uint64;
600# else
601 typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
602# endif
603#elif defined(_MSC_VER) || defined(__BORLANDC__)
604 typedef __int64 sqlite_int64;
605 typedef unsigned __int64 sqlite_uint64;
606#else
607 typedef long long int sqlite_int64;
608 typedef unsigned long long int sqlite_uint64;
609#endif
610typedef sqlite_int64 sqlite3_int64;
611typedef sqlite_uint64 sqlite3_uint64;
612
613/*
614** If compiling for a processor that lacks floating point support,
615** substitute integer for floating-point.
616*/
617#ifdef SQLITE_OMIT_FLOATING_POINT
618# define double sqlite3_int64
619#endif
620
621/*
622** CAPI3REF: Closing A Database Connection
623** DESTRUCTOR: sqlite3
624**
625** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
626** for the [sqlite3] object.
627** ^Calls to sqlite3_close() and sqlite3_close_v2() return [SQLITE_OK] if
628** the [sqlite3] object is successfully destroyed and all associated
629** resources are deallocated.
630**
631** Ideally, applications should [sqlite3_finalize | finalize] all
632** [prepared statements], [sqlite3_blob_close | close] all [BLOB handles], and
633** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
634** with the [sqlite3] object prior to attempting to close the object.
635** ^If the database connection is associated with unfinalized prepared
636** statements, BLOB handlers, and/or unfinished sqlite3_backup objects then
637** sqlite3_close() will leave the database connection open and return
638** [SQLITE_BUSY]. ^If sqlite3_close_v2() is called with unfinalized prepared
639** statements, unclosed BLOB handlers, and/or unfinished sqlite3_backups,
640** it returns [SQLITE_OK] regardless, but instead of deallocating the database
641** connection immediately, it marks the database connection as an unusable
642** "zombie" and makes arrangements to automatically deallocate the database
643** connection after all prepared statements are finalized, all BLOB handles
644** are closed, and all backups have finished. The sqlite3_close_v2() interface
645** is intended for use with host languages that are garbage collected, and
646** where the order in which destructors are called is arbitrary.
647**
648** ^If an [sqlite3] object is destroyed while a transaction is open,
649** the transaction is automatically rolled back.
650**
651** The C parameter to [sqlite3_close(C)] and [sqlite3_close_v2(C)]
652** must be either a NULL
653** pointer or an [sqlite3] object pointer obtained
654** from [sqlite3_open()], [sqlite3_open16()], or
655** [sqlite3_open_v2()], and not previously closed.
656** ^Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer
657** argument is a harmless no-op.
658*/
659SQLITE_API int sqlite3_close(sqlite3*);
660SQLITE_API int sqlite3_close_v2(sqlite3*);
661
662/*
663** The type for a callback function.
664** This is legacy and deprecated. It is included for historical
665** compatibility and is not documented.
666*/
667typedef int (*sqlite3_callback)(void*,int,char**, char**);
668
669/*
670** CAPI3REF: One-Step Query Execution Interface
671** METHOD: sqlite3
672**
673** The sqlite3_exec() interface is a convenience wrapper around
674** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()],
675** that allows an application to run multiple statements of SQL
676** without having to use a lot of C code.
677**
678** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
679** semicolon-separate SQL statements passed into its 2nd argument,
680** in the context of the [database connection] passed in as its 1st
681** argument. ^If the callback function of the 3rd argument to
682** sqlite3_exec() is not NULL, then it is invoked for each result row
683** coming out of the evaluated SQL statements. ^The 4th argument to
684** sqlite3_exec() is relayed through to the 1st argument of each
685** callback invocation. ^If the callback pointer to sqlite3_exec()
686** is NULL, then no callback is ever invoked and result rows are
687** ignored.
688**
689** ^If an error occurs while evaluating the SQL statements passed into
690** sqlite3_exec(), then execution of the current statement stops and
691** subsequent statements are skipped. ^If the 5th parameter to sqlite3_exec()
692** is not NULL then any error message is written into memory obtained
693** from [sqlite3_malloc()] and passed back through the 5th parameter.
694** To avoid memory leaks, the application should invoke [sqlite3_free()]
695** on error message strings returned through the 5th parameter of
696** sqlite3_exec() after the error message string is no longer needed.
697** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
698** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
699** NULL before returning.
700**
701** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
702** routine returns SQLITE_ABORT without invoking the callback again and
703** without running any subsequent SQL statements.
704**
705** ^The 2nd argument to the sqlite3_exec() callback function is the
706** number of columns in the result. ^The 3rd argument to the sqlite3_exec()
707** callback is an array of pointers to strings obtained as if from
708** [sqlite3_column_text()], one for each column. ^If an element of a
709** result row is NULL then the corresponding string pointer for the
710** sqlite3_exec() callback is a NULL pointer. ^The 4th argument to the
711** sqlite3_exec() callback is an array of pointers to strings where each
712** entry represents the name of corresponding result column as obtained
713** from [sqlite3_column_name()].
714**
715** ^If the 2nd parameter to sqlite3_exec() is a NULL pointer, a pointer
716** to an empty string, or a pointer that contains only whitespace and/or
717** SQL comments, then no SQL statements are evaluated and the database
718** is not changed.
719**
720** Restrictions:
721**
722** <ul>
723** <li> The application must ensure that the 1st parameter to sqlite3_exec()
724** is a valid and open [database connection].
725** <li> The application must not close the [database connection] specified by
726** the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
727** <li> The application must not modify the SQL statement text passed into
728** the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
729** </ul>
730*/
731SQLITE_API int sqlite3_exec(
732 sqlite3*, /* An open database */
733 const char *sql, /* SQL to be evaluated */
734 int (*callback)(void*,int,char**,char**), /* Callback function */
735 void *, /* 1st argument to callback */
736 char **errmsg /* Error msg written here */
737);
738
739/*
740** CAPI3REF: Result Codes
741** KEYWORDS: {result code definitions}
742**
743** Many SQLite functions return an integer result code from the set shown
744** here in order to indicate success or failure.
745**
746** New error codes may be added in future versions of SQLite.
747**
748** See also: [extended result code definitions]
749*/
750#define SQLITE_OK 0 /* Successful result */
751/* beginning-of-error-codes */
752#define SQLITE_ERROR 1 /* Generic error */
753#define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */
754#define SQLITE_PERM 3 /* Access permission denied */
755#define SQLITE_ABORT 4 /* Callback routine requested an abort */
756#define SQLITE_BUSY 5 /* The database file is locked */
757#define SQLITE_LOCKED 6 /* A table in the database is locked */
758#define SQLITE_NOMEM 7 /* A malloc() failed */
759#define SQLITE_READONLY 8 /* Attempt to write a readonly database */
760#define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/
761#define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
762#define SQLITE_CORRUPT 11 /* The database disk image is malformed */
763#define SQLITE_NOTFOUND 12 /* Unknown opcode in sqlite3_file_control() */
764#define SQLITE_FULL 13 /* Insertion failed because database is full */
765#define SQLITE_CANTOPEN 14 /* Unable to open the database file */
766#define SQLITE_PROTOCOL 15 /* Database lock protocol error */
767#define SQLITE_EMPTY 16 /* Internal use only */
768#define SQLITE_SCHEMA 17 /* The database schema changed */
769#define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */
770#define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */
771#define SQLITE_MISMATCH 20 /* Data type mismatch */
772#define SQLITE_MISUSE 21 /* Library used incorrectly */
773#define SQLITE_NOLFS 22 /* Uses OS features not supported on host */
774#define SQLITE_AUTH 23 /* Authorization denied */
775#define SQLITE_FORMAT 24 /* Not used */
776#define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */
777#define SQLITE_NOTADB 26 /* File opened that is not a database file */
778#define SQLITE_NOTICE 27 /* Notifications from sqlite3_log() */
779#define SQLITE_WARNING 28 /* Warnings from sqlite3_log() */
780#define SQLITE_ROW 100 /* sqlite3_step() has another row ready */
781#define SQLITE_DONE 101 /* sqlite3_step() has finished executing */
782/* end-of-error-codes */
783
784/*
785** CAPI3REF: Extended Result Codes
786** KEYWORDS: {extended result code definitions}
787**
788** In its default configuration, SQLite API routines return one of 30 integer
789** [result codes]. However, experience has shown that many of
790** these result codes are too coarse-grained. They do not provide as
791** much information about problems as programmers might like. In an effort to
792** address this, newer versions of SQLite (version 3.3.8 [dateof:3.3.8]
793** and later) include
794** support for additional result codes that provide more detailed information
795** about errors. These [extended result codes] are enabled or disabled
796** on a per database connection basis using the
797** [sqlite3_extended_result_codes()] API. Or, the extended code for
798** the most recent error can be obtained using
799** [sqlite3_extended_errcode()].
800*/
801#define SQLITE_ERROR_MISSING_COLLSEQ (SQLITE_ERROR | (1<<8))
802#define SQLITE_ERROR_RETRY (SQLITE_ERROR | (2<<8))
803#define SQLITE_ERROR_SNAPSHOT (SQLITE_ERROR | (3<<8))
804#define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8))
805#define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8))
806#define SQLITE_IOERR_WRITE (SQLITE_IOERR | (3<<8))
807#define SQLITE_IOERR_FSYNC (SQLITE_IOERR | (4<<8))
808#define SQLITE_IOERR_DIR_FSYNC (SQLITE_IOERR | (5<<8))
809#define SQLITE_IOERR_TRUNCATE (SQLITE_IOERR | (6<<8))
810#define SQLITE_IOERR_FSTAT (SQLITE_IOERR | (7<<8))
811#define SQLITE_IOERR_UNLOCK (SQLITE_IOERR | (8<<8))
812#define SQLITE_IOERR_RDLOCK (SQLITE_IOERR | (9<<8))
813#define SQLITE_IOERR_DELETE (SQLITE_IOERR | (10<<8))
814#define SQLITE_IOERR_BLOCKED (SQLITE_IOERR | (11<<8))
815#define SQLITE_IOERR_NOMEM (SQLITE_IOERR | (12<<8))
816#define SQLITE_IOERR_ACCESS (SQLITE_IOERR | (13<<8))
817#define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8))
818#define SQLITE_IOERR_LOCK (SQLITE_IOERR | (15<<8))
819#define SQLITE_IOERR_CLOSE (SQLITE_IOERR | (16<<8))
820#define SQLITE_IOERR_DIR_CLOSE (SQLITE_IOERR | (17<<8))
821#define SQLITE_IOERR_SHMOPEN (SQLITE_IOERR | (18<<8))
822#define SQLITE_IOERR_SHMSIZE (SQLITE_IOERR | (19<<8))
823#define SQLITE_IOERR_SHMLOCK (SQLITE_IOERR | (20<<8))
824#define SQLITE_IOERR_SHMMAP (SQLITE_IOERR | (21<<8))
825#define SQLITE_IOERR_SEEK (SQLITE_IOERR | (22<<8))
826#define SQLITE_IOERR_DELETE_NOENT (SQLITE_IOERR | (23<<8))
827#define SQLITE_IOERR_MMAP (SQLITE_IOERR | (24<<8))
828#define SQLITE_IOERR_GETTEMPPATH (SQLITE_IOERR | (25<<8))
829#define SQLITE_IOERR_CONVPATH (SQLITE_IOERR | (26<<8))
830#define SQLITE_IOERR_VNODE (SQLITE_IOERR | (27<<8))
831#define SQLITE_IOERR_AUTH (SQLITE_IOERR | (28<<8))
832#define SQLITE_IOERR_BEGIN_ATOMIC (SQLITE_IOERR | (29<<8))
833#define SQLITE_IOERR_COMMIT_ATOMIC (SQLITE_IOERR | (30<<8))
834#define SQLITE_IOERR_ROLLBACK_ATOMIC (SQLITE_IOERR | (31<<8))
835#define SQLITE_IOERR_DATA (SQLITE_IOERR | (32<<8))
836#define SQLITE_IOERR_CORRUPTFS (SQLITE_IOERR | (33<<8))
837#define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8))
838#define SQLITE_LOCKED_VTAB (SQLITE_LOCKED | (2<<8))
839#define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8))
840#define SQLITE_BUSY_SNAPSHOT (SQLITE_BUSY | (2<<8))
841#define SQLITE_BUSY_TIMEOUT (SQLITE_BUSY | (3<<8))
842#define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8))
843#define SQLITE_CANTOPEN_ISDIR (SQLITE_CANTOPEN | (2<<8))
844#define SQLITE_CANTOPEN_FULLPATH (SQLITE_CANTOPEN | (3<<8))
845#define SQLITE_CANTOPEN_CONVPATH (SQLITE_CANTOPEN | (4<<8))
846#define SQLITE_CANTOPEN_DIRTYWAL (SQLITE_CANTOPEN | (5<<8)) /* Not Used */
847#define SQLITE_CANTOPEN_SYMLINK (SQLITE_CANTOPEN | (6<<8))
848#define SQLITE_CORRUPT_VTAB (SQLITE_CORRUPT | (1<<8))
849#define SQLITE_CORRUPT_SEQUENCE (SQLITE_CORRUPT | (2<<8))
850#define SQLITE_CORRUPT_INDEX (SQLITE_CORRUPT | (3<<8))
851#define SQLITE_READONLY_RECOVERY (SQLITE_READONLY | (1<<8))
852#define SQLITE_READONLY_CANTLOCK (SQLITE_READONLY | (2<<8))
853#define SQLITE_READONLY_ROLLBACK (SQLITE_READONLY | (3<<8))
854#define SQLITE_READONLY_DBMOVED (SQLITE_READONLY | (4<<8))
855#define SQLITE_READONLY_CANTINIT (SQLITE_READONLY | (5<<8))
856#define SQLITE_READONLY_DIRECTORY (SQLITE_READONLY | (6<<8))
857#define SQLITE_ABORT_ROLLBACK (SQLITE_ABORT | (2<<8))
858#define SQLITE_CONSTRAINT_CHECK (SQLITE_CONSTRAINT | (1<<8))
859#define SQLITE_CONSTRAINT_COMMITHOOK (SQLITE_CONSTRAINT | (2<<8))
860#define SQLITE_CONSTRAINT_FOREIGNKEY (SQLITE_CONSTRAINT | (3<<8))
861#define SQLITE_CONSTRAINT_FUNCTION (SQLITE_CONSTRAINT | (4<<8))
862#define SQLITE_CONSTRAINT_NOTNULL (SQLITE_CONSTRAINT | (5<<8))
863#define SQLITE_CONSTRAINT_PRIMARYKEY (SQLITE_CONSTRAINT | (6<<8))
864#define SQLITE_CONSTRAINT_TRIGGER (SQLITE_CONSTRAINT | (7<<8))
865#define SQLITE_CONSTRAINT_UNIQUE (SQLITE_CONSTRAINT | (8<<8))
866#define SQLITE_CONSTRAINT_VTAB (SQLITE_CONSTRAINT | (9<<8))
867#define SQLITE_CONSTRAINT_ROWID (SQLITE_CONSTRAINT |(10<<8))
868#define SQLITE_CONSTRAINT_PINNED (SQLITE_CONSTRAINT |(11<<8))
869#define SQLITE_CONSTRAINT_DATATYPE (SQLITE_CONSTRAINT |(12<<8))
870#define SQLITE_NOTICE_RECOVER_WAL (SQLITE_NOTICE | (1<<8))
871#define SQLITE_NOTICE_RECOVER_ROLLBACK (SQLITE_NOTICE | (2<<8))
872#define SQLITE_WARNING_AUTOINDEX (SQLITE_WARNING | (1<<8))
873#define SQLITE_AUTH_USER (SQLITE_AUTH | (1<<8))
874#define SQLITE_OK_LOAD_PERMANENTLY (SQLITE_OK | (1<<8))
875#define SQLITE_OK_SYMLINK (SQLITE_OK | (2<<8))
876
877/*
878** CAPI3REF: Flags For File Open Operations
879**
880** These bit values are intended for use in the
881** 3rd parameter to the [sqlite3_open_v2()] interface and
882** in the 4th parameter to the [sqlite3_vfs.xOpen] method.
883**
884** Only those flags marked as "Ok for sqlite3_open_v2()" may be
885** used as the third argument to the [sqlite3_open_v2()] interface.
886** The other flags have historically been ignored by sqlite3_open_v2(),
887** though future versions of SQLite might change so that an error is
888** raised if any of the disallowed bits are passed into sqlite3_open_v2().
889** Applications should not depend on the historical behavior.
890**
891** Note in particular that passing the SQLITE_OPEN_EXCLUSIVE flag into
892** [sqlite3_open_v2()] does *not* cause the underlying database file
893** to be opened using O_EXCL. Passing SQLITE_OPEN_EXCLUSIVE into
894** [sqlite3_open_v2()] has historically be a no-op and might become an
895** error in future versions of SQLite.
896*/
897#define SQLITE_OPEN_READONLY 0x00000001 /* Ok for sqlite3_open_v2() */
898#define SQLITE_OPEN_READWRITE 0x00000002 /* Ok for sqlite3_open_v2() */
899#define SQLITE_OPEN_CREATE 0x00000004 /* Ok for sqlite3_open_v2() */
900#define SQLITE_OPEN_DELETEONCLOSE 0x00000008 /* VFS only */
901#define SQLITE_OPEN_EXCLUSIVE 0x00000010 /* VFS only */
902#define SQLITE_OPEN_AUTOPROXY 0x00000020 /* VFS only */
903#define SQLITE_OPEN_URI 0x00000040 /* Ok for sqlite3_open_v2() */
904#define SQLITE_OPEN_MEMORY 0x00000080 /* Ok for sqlite3_open_v2() */
905#define SQLITE_OPEN_MAIN_DB 0x00000100 /* VFS only */
906#define SQLITE_OPEN_TEMP_DB 0x00000200 /* VFS only */
907#define SQLITE_OPEN_TRANSIENT_DB 0x00000400 /* VFS only */
908#define SQLITE_OPEN_MAIN_JOURNAL 0x00000800 /* VFS only */
909#define SQLITE_OPEN_TEMP_JOURNAL 0x00001000 /* VFS only */
910#define SQLITE_OPEN_SUBJOURNAL 0x00002000 /* VFS only */
911#define SQLITE_OPEN_SUPER_JOURNAL 0x00004000 /* VFS only */
912#define SQLITE_OPEN_NOMUTEX 0x00008000 /* Ok for sqlite3_open_v2() */
913#define SQLITE_OPEN_FULLMUTEX 0x00010000 /* Ok for sqlite3_open_v2() */
914#define SQLITE_OPEN_SHAREDCACHE 0x00020000 /* Ok for sqlite3_open_v2() */
915#define SQLITE_OPEN_PRIVATECACHE 0x00040000 /* Ok for sqlite3_open_v2() */
916#define SQLITE_OPEN_WAL 0x00080000 /* VFS only */
917#define SQLITE_OPEN_NOFOLLOW 0x01000000 /* Ok for sqlite3_open_v2() */
918#define SQLITE_OPEN_EXRESCODE 0x02000000 /* Extended result codes */
919
920/* Reserved: 0x00F00000 */
921/* Legacy compatibility: */
922#define SQLITE_OPEN_MASTER_JOURNAL 0x00004000 /* VFS only */
923
924
925/*
926** CAPI3REF: Device Characteristics
927**
928** The xDeviceCharacteristics method of the [sqlite3_io_methods]
929** object returns an integer which is a vector of these
930** bit values expressing I/O characteristics of the mass storage
931** device that holds the file that the [sqlite3_io_methods]
932** refers to.
933**
934** The SQLITE_IOCAP_ATOMIC property means that all writes of
935** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
936** mean that writes of blocks that are nnn bytes in size and
937** are aligned to an address which is an integer multiple of
938** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means
939** that when data is appended to a file, the data is appended
940** first then the size of the file is extended, never the other
941** way around. The SQLITE_IOCAP_SEQUENTIAL property means that
942** information is written to disk in the same order as calls
943** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
944** after reboot following a crash or power loss, the only bytes in a
945** file that were written at the application level might have changed
946** and that adjacent bytes, even bytes within the same sector are
947** guaranteed to be unchanged. The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
948** flag indicates that a file cannot be deleted when open. The
949** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
950** read-only media and cannot be changed even by processes with
951** elevated privileges.
952**
953** The SQLITE_IOCAP_BATCH_ATOMIC property means that the underlying
954** filesystem supports doing multiple write operations atomically when those
955** write operations are bracketed by [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] and
956** [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE].
957*/
958#define SQLITE_IOCAP_ATOMIC 0x00000001
959#define SQLITE_IOCAP_ATOMIC512 0x00000002
960#define SQLITE_IOCAP_ATOMIC1K 0x00000004
961#define SQLITE_IOCAP_ATOMIC2K 0x00000008
962#define SQLITE_IOCAP_ATOMIC4K 0x00000010
963#define SQLITE_IOCAP_ATOMIC8K 0x00000020
964#define SQLITE_IOCAP_ATOMIC16K 0x00000040
965#define SQLITE_IOCAP_ATOMIC32K 0x00000080
966#define SQLITE_IOCAP_ATOMIC64K 0x00000100
967#define SQLITE_IOCAP_SAFE_APPEND 0x00000200
968#define SQLITE_IOCAP_SEQUENTIAL 0x00000400
969#define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN 0x00000800
970#define SQLITE_IOCAP_POWERSAFE_OVERWRITE 0x00001000
971#define SQLITE_IOCAP_IMMUTABLE 0x00002000
972#define SQLITE_IOCAP_BATCH_ATOMIC 0x00004000
973
974/*
975** CAPI3REF: File Locking Levels
976**
977** SQLite uses one of these integer values as the second
978** argument to calls it makes to the xLock() and xUnlock() methods
979** of an [sqlite3_io_methods] object.
980*/
981#define SQLITE_LOCK_NONE 0
982#define SQLITE_LOCK_SHARED 1
983#define SQLITE_LOCK_RESERVED 2
984#define SQLITE_LOCK_PENDING 3
985#define SQLITE_LOCK_EXCLUSIVE 4
986
987/*
988** CAPI3REF: Synchronization Type Flags
989**
990** When SQLite invokes the xSync() method of an
991** [sqlite3_io_methods] object it uses a combination of
992** these integer values as the second argument.
993**
994** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
995** sync operation only needs to flush data to mass storage. Inode
996** information need not be flushed. If the lower four bits of the flag
997** equal SQLITE_SYNC_NORMAL, that means to use normal fsync() semantics.
998** If the lower four bits equal SQLITE_SYNC_FULL, that means
999** to use Mac OS X style fullsync instead of fsync().
1000**
1001** Do not confuse the SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags
1002** with the [PRAGMA synchronous]=NORMAL and [PRAGMA synchronous]=FULL
1003** settings. The [synchronous pragma] determines when calls to the
1004** xSync VFS method occur and applies uniformly across all platforms.
1005** The SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags determine how
1006** energetic or rigorous or forceful the sync operations are and
1007** only make a difference on Mac OSX for the default SQLite code.
1008** (Third-party VFS implementations might also make the distinction
1009** between SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL, but among the
1010** operating systems natively supported by SQLite, only Mac OSX
1011** cares about the difference.)
1012*/
1013#define SQLITE_SYNC_NORMAL 0x00002
1014#define SQLITE_SYNC_FULL 0x00003
1015#define SQLITE_SYNC_DATAONLY 0x00010
1016
1017/*
1018** CAPI3REF: OS Interface Open File Handle
1019**
1020** An [sqlite3_file] object represents an open file in the
1021** [sqlite3_vfs | OS interface layer]. Individual OS interface
1022** implementations will
1023** want to subclass this object by appending additional fields
1024** for their own use. The pMethods entry is a pointer to an
1025** [sqlite3_io_methods] object that defines methods for performing
1026** I/O operations on the open file.
1027*/
1028typedef struct sqlite3_file sqlite3_file;
1029struct sqlite3_file {
1030 const struct sqlite3_io_methods *pMethods; /* Methods for an open file */
1031};
1032
1033/*
1034** CAPI3REF: OS Interface File Virtual Methods Object
1035**
1036** Every file opened by the [sqlite3_vfs.xOpen] method populates an
1037** [sqlite3_file] object (or, more commonly, a subclass of the
1038** [sqlite3_file] object) with a pointer to an instance of this object.
1039** This object defines the methods used to perform various operations
1040** against the open file represented by the [sqlite3_file] object.
1041**
1042** If the [sqlite3_vfs.xOpen] method sets the sqlite3_file.pMethods element
1043** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
1044** may be invoked even if the [sqlite3_vfs.xOpen] reported that it failed. The
1045** only way to prevent a call to xClose following a failed [sqlite3_vfs.xOpen]
1046** is for the [sqlite3_vfs.xOpen] to set the sqlite3_file.pMethods element
1047** to NULL.
1048**
1049** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
1050** [SQLITE_SYNC_FULL]. The first choice is the normal fsync().
1051** The second choice is a Mac OS X style fullsync. The [SQLITE_SYNC_DATAONLY]
1052** flag may be ORed in to indicate that only the data of the file
1053** and not its inode needs to be synced.
1054**
1055** The integer values to xLock() and xUnlock() are one of
1056** <ul>
1057** <li> [SQLITE_LOCK_NONE],
1058** <li> [SQLITE_LOCK_SHARED],
1059** <li> [SQLITE_LOCK_RESERVED],
1060** <li> [SQLITE_LOCK_PENDING], or
1061** <li> [SQLITE_LOCK_EXCLUSIVE].
1062** </ul>
1063** xLock() increases the lock. xUnlock() decreases the lock.
1064** The xCheckReservedLock() method checks whether any database connection,
1065** either in this process or in some other process, is holding a RESERVED,
1066** PENDING, or EXCLUSIVE lock on the file. It returns true
1067** if such a lock exists and false otherwise.
1068**
1069** The xFileControl() method is a generic interface that allows custom
1070** VFS implementations to directly control an open file using the
1071** [sqlite3_file_control()] interface. The second "op" argument is an
1072** integer opcode. The third argument is a generic pointer intended to
1073** point to a structure that may contain arguments or space in which to
1074** write return values. Potential uses for xFileControl() might be
1075** functions to enable blocking locks with timeouts, to change the
1076** locking strategy (for example to use dot-file locks), to inquire
1077** about the status of a lock, or to break stale locks. The SQLite
1078** core reserves all opcodes less than 100 for its own use.
1079** A [file control opcodes | list of opcodes] less than 100 is available.
1080** Applications that define a custom xFileControl method should use opcodes
1081** greater than 100 to avoid conflicts. VFS implementations should
1082** return [SQLITE_NOTFOUND] for file control opcodes that they do not
1083** recognize.
1084**
1085** The xSectorSize() method returns the sector size of the
1086** device that underlies the file. The sector size is the
1087** minimum write that can be performed without disturbing
1088** other bytes in the file. The xDeviceCharacteristics()
1089** method returns a bit vector describing behaviors of the
1090** underlying device:
1091**
1092** <ul>
1093** <li> [SQLITE_IOCAP_ATOMIC]
1094** <li> [SQLITE_IOCAP_ATOMIC512]
1095** <li> [SQLITE_IOCAP_ATOMIC1K]
1096** <li> [SQLITE_IOCAP_ATOMIC2K]
1097** <li> [SQLITE_IOCAP_ATOMIC4K]
1098** <li> [SQLITE_IOCAP_ATOMIC8K]
1099** <li> [SQLITE_IOCAP_ATOMIC16K]
1100** <li> [SQLITE_IOCAP_ATOMIC32K]
1101** <li> [SQLITE_IOCAP_ATOMIC64K]
1102** <li> [SQLITE_IOCAP_SAFE_APPEND]
1103** <li> [SQLITE_IOCAP_SEQUENTIAL]
1104** <li> [SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN]
1105** <li> [SQLITE_IOCAP_POWERSAFE_OVERWRITE]
1106** <li> [SQLITE_IOCAP_IMMUTABLE]
1107** <li> [SQLITE_IOCAP_BATCH_ATOMIC]
1108** </ul>
1109**
1110** The SQLITE_IOCAP_ATOMIC property means that all writes of
1111** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
1112** mean that writes of blocks that are nnn bytes in size and
1113** are aligned to an address which is an integer multiple of
1114** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means
1115** that when data is appended to a file, the data is appended
1116** first then the size of the file is extended, never the other
1117** way around. The SQLITE_IOCAP_SEQUENTIAL property means that
1118** information is written to disk in the same order as calls
1119** to xWrite().
1120**
1121** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill
1122** in the unread portions of the buffer with zeros. A VFS that
1123** fails to zero-fill short reads might seem to work. However,
1124** failure to zero-fill short reads will eventually lead to
1125** database corruption.
1126*/
1127typedef struct sqlite3_io_methods sqlite3_io_methods;
1128struct sqlite3_io_methods {
1129 int iVersion;
1130 int (*xClose)(sqlite3_file*);
1131 int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
1132 int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
1133 int (*xTruncate)(sqlite3_file*, sqlite3_int64 size);
1134 int (*xSync)(sqlite3_file*, int flags);
1135 int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize);
1136 int (*xLock)(sqlite3_file*, int);
1137 int (*xUnlock)(sqlite3_file*, int);
1138 int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
1139 int (*xFileControl)(sqlite3_file*, int op, void *pArg);
1140 int (*xSectorSize)(sqlite3_file*);
1141 int (*xDeviceCharacteristics)(sqlite3_file*);
1142 /* Methods above are valid for version 1 */
1143 int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
1144 int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
1145 void (*xShmBarrier)(sqlite3_file*);
1146 int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
1147 /* Methods above are valid for version 2 */
1148 int (*xFetch)(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
1149 int (*xUnfetch)(sqlite3_file*, sqlite3_int64 iOfst, void *p);
1150 /* Methods above are valid for version 3 */
1151 /* Additional methods may be added in future releases */
1152};
1153
1154/*
1155** CAPI3REF: Standard File Control Opcodes
1156** KEYWORDS: {file control opcodes} {file control opcode}
1157**
1158** These integer constants are opcodes for the xFileControl method
1159** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
1160** interface.
1161**
1162** <ul>
1163** <li>[[SQLITE_FCNTL_LOCKSTATE]]
1164** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This
1165** opcode causes the xFileControl method to write the current state of
1166** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
1167** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
1168** into an integer that the pArg argument points to. This capability
1169** is used during testing and is only available when the SQLITE_TEST
1170** compile-time option is used.
1171**
1172** <li>[[SQLITE_FCNTL_SIZE_HINT]]
1173** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
1174** layer a hint of how large the database file will grow to be during the
1175** current transaction. This hint is not guaranteed to be accurate but it
1176** is often close. The underlying VFS might choose to preallocate database
1177** file space based on this hint in order to help writes to the database
1178** file run faster.
1179**
1180** <li>[[SQLITE_FCNTL_SIZE_LIMIT]]
1181** The [SQLITE_FCNTL_SIZE_LIMIT] opcode is used by in-memory VFS that
1182** implements [sqlite3_deserialize()] to set an upper bound on the size
1183** of the in-memory database. The argument is a pointer to a [sqlite3_int64].
1184** If the integer pointed to is negative, then it is filled in with the
1185** current limit. Otherwise the limit is set to the larger of the value
1186** of the integer pointed to and the current database size. The integer
1187** pointed to is set to the new limit.
1188**
1189** <li>[[SQLITE_FCNTL_CHUNK_SIZE]]
1190** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS
1191** extends and truncates the database file in chunks of a size specified
1192** by the user. The fourth argument to [sqlite3_file_control()] should
1193** point to an integer (type int) containing the new chunk-size to use
1194** for the nominated database. Allocating database file space in large
1195** chunks (say 1MB at a time), may reduce file-system fragmentation and
1196** improve performance on some systems.
1197**
1198** <li>[[SQLITE_FCNTL_FILE_POINTER]]
1199** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
1200** to the [sqlite3_file] object associated with a particular database
1201** connection. See also [SQLITE_FCNTL_JOURNAL_POINTER].
1202**
1203** <li>[[SQLITE_FCNTL_JOURNAL_POINTER]]
1204** The [SQLITE_FCNTL_JOURNAL_POINTER] opcode is used to obtain a pointer
1205** to the [sqlite3_file] object associated with the journal file (either
1206** the [rollback journal] or the [write-ahead log]) for a particular database
1207** connection. See also [SQLITE_FCNTL_FILE_POINTER].
1208**
1209** <li>[[SQLITE_FCNTL_SYNC_OMITTED]]
1210** No longer in use.
1211**
1212** <li>[[SQLITE_FCNTL_SYNC]]
1213** The [SQLITE_FCNTL_SYNC] opcode is generated internally by SQLite and
1214** sent to the VFS immediately before the xSync method is invoked on a
1215** database file descriptor. Or, if the xSync method is not invoked
1216** because the user has configured SQLite with
1217** [PRAGMA synchronous | PRAGMA synchronous=OFF] it is invoked in place
1218** of the xSync method. In most cases, the pointer argument passed with
1219** this file-control is NULL. However, if the database file is being synced
1220** as part of a multi-database commit, the argument points to a nul-terminated
1221** string containing the transactions super-journal file name. VFSes that
1222** do not need this signal should silently ignore this opcode. Applications
1223** should not call [sqlite3_file_control()] with this opcode as doing so may
1224** disrupt the operation of the specialized VFSes that do require it.
1225**
1226** <li>[[SQLITE_FCNTL_COMMIT_PHASETWO]]
1227** The [SQLITE_FCNTL_COMMIT_PHASETWO] opcode is generated internally by SQLite
1228** and sent to the VFS after a transaction has been committed immediately
1229** but before the database is unlocked. VFSes that do not need this signal
1230** should silently ignore this opcode. Applications should not call
1231** [sqlite3_file_control()] with this opcode as doing so may disrupt the
1232** operation of the specialized VFSes that do require it.
1233**
1234** <li>[[SQLITE_FCNTL_WIN32_AV_RETRY]]
1235** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic
1236** retry counts and intervals for certain disk I/O operations for the
1237** windows [VFS] in order to provide robustness in the presence of
1238** anti-virus programs. By default, the windows VFS will retry file read,
1239** file write, and file delete operations up to 10 times, with a delay
1240** of 25 milliseconds before the first retry and with the delay increasing
1241** by an additional 25 milliseconds with each subsequent retry. This
1242** opcode allows these two values (10 retries and 25 milliseconds of delay)
1243** to be adjusted. The values are changed for all database connections
1244** within the same process. The argument is a pointer to an array of two
1245** integers where the first integer is the new retry count and the second
1246** integer is the delay. If either integer is negative, then the setting
1247** is not changed but instead the prior value of that setting is written
1248** into the array entry, allowing the current retry settings to be
1249** interrogated. The zDbName parameter is ignored.
1250**
1251** <li>[[SQLITE_FCNTL_PERSIST_WAL]]
1252** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the
1253** persistent [WAL | Write Ahead Log] setting. By default, the auxiliary
1254** write ahead log ([WAL file]) and shared memory
1255** files used for transaction control
1256** are automatically deleted when the latest connection to the database
1257** closes. Setting persistent WAL mode causes those files to persist after
1258** close. Persisting the files is useful when other processes that do not
1259** have write permission on the directory containing the database file want
1260** to read the database file, as the WAL and shared memory files must exist
1261** in order for the database to be readable. The fourth parameter to
1262** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
1263** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
1264** WAL mode. If the integer is -1, then it is overwritten with the current
1265** WAL persistence setting.
1266**
1267** <li>[[SQLITE_FCNTL_POWERSAFE_OVERWRITE]]
1268** ^The [SQLITE_FCNTL_POWERSAFE_OVERWRITE] opcode is used to set or query the
1269** persistent "powersafe-overwrite" or "PSOW" setting. The PSOW setting
1270** determines the [SQLITE_IOCAP_POWERSAFE_OVERWRITE] bit of the
1271** xDeviceCharacteristics methods. The fourth parameter to
1272** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
1273** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
1274** mode. If the integer is -1, then it is overwritten with the current
1275** zero-damage mode setting.
1276**
1277** <li>[[SQLITE_FCNTL_OVERWRITE]]
1278** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
1279** a write transaction to indicate that, unless it is rolled back for some
1280** reason, the entire database file will be overwritten by the current
1281** transaction. This is used by VACUUM operations.
1282**
1283** <li>[[SQLITE_FCNTL_VFSNAME]]
1284** ^The [SQLITE_FCNTL_VFSNAME] opcode can be used to obtain the names of
1285** all [VFSes] in the VFS stack. The names are of all VFS shims and the
1286** final bottom-level VFS are written into memory obtained from
1287** [sqlite3_malloc()] and the result is stored in the char* variable
1288** that the fourth parameter of [sqlite3_file_control()] points to.
1289** The caller is responsible for freeing the memory when done. As with
1290** all file-control actions, there is no guarantee that this will actually
1291** do anything. Callers should initialize the char* variable to a NULL
1292** pointer in case this file-control is not implemented. This file-control
1293** is intended for diagnostic use only.
1294**
1295** <li>[[SQLITE_FCNTL_VFS_POINTER]]
1296** ^The [SQLITE_FCNTL_VFS_POINTER] opcode finds a pointer to the top-level
1297** [VFSes] currently in use. ^(The argument X in
1298** sqlite3_file_control(db,SQLITE_FCNTL_VFS_POINTER,X) must be
1299** of type "[sqlite3_vfs] **". This opcodes will set *X
1300** to a pointer to the top-level VFS.)^
1301** ^When there are multiple VFS shims in the stack, this opcode finds the
1302** upper-most shim only.
1303**
1304** <li>[[SQLITE_FCNTL_PRAGMA]]
1305** ^Whenever a [PRAGMA] statement is parsed, an [SQLITE_FCNTL_PRAGMA]
1306** file control is sent to the open [sqlite3_file] object corresponding
1307** to the database file to which the pragma statement refers. ^The argument
1308** to the [SQLITE_FCNTL_PRAGMA] file control is an array of
1309** pointers to strings (char**) in which the second element of the array
1310** is the name of the pragma and the third element is the argument to the
1311** pragma or NULL if the pragma has no argument. ^The handler for an
1312** [SQLITE_FCNTL_PRAGMA] file control can optionally make the first element
1313** of the char** argument point to a string obtained from [sqlite3_mprintf()]
1314** or the equivalent and that string will become the result of the pragma or
1315** the error message if the pragma fails. ^If the
1316** [SQLITE_FCNTL_PRAGMA] file control returns [SQLITE_NOTFOUND], then normal
1317** [PRAGMA] processing continues. ^If the [SQLITE_FCNTL_PRAGMA]
1318** file control returns [SQLITE_OK], then the parser assumes that the
1319** VFS has handled the PRAGMA itself and the parser generates a no-op
1320** prepared statement if result string is NULL, or that returns a copy
1321** of the result string if the string is non-NULL.
1322** ^If the [SQLITE_FCNTL_PRAGMA] file control returns
1323** any result code other than [SQLITE_OK] or [SQLITE_NOTFOUND], that means
1324** that the VFS encountered an error while handling the [PRAGMA] and the
1325** compilation of the PRAGMA fails with an error. ^The [SQLITE_FCNTL_PRAGMA]
1326** file control occurs at the beginning of pragma statement analysis and so
1327** it is able to override built-in [PRAGMA] statements.
1328**
1329** <li>[[SQLITE_FCNTL_BUSYHANDLER]]
1330** ^The [SQLITE_FCNTL_BUSYHANDLER]
1331** file-control may be invoked by SQLite on the database file handle
1332** shortly after it is opened in order to provide a custom VFS with access
1333** to the connection's busy-handler callback. The argument is of type (void**)
1334** - an array of two (void *) values. The first (void *) actually points
1335** to a function of type (int (*)(void *)). In order to invoke the connection's
1336** busy-handler, this function should be invoked with the second (void *) in
1337** the array as the only argument. If it returns non-zero, then the operation
1338** should be retried. If it returns zero, the custom VFS should abandon the
1339** current operation.
1340**
1341** <li>[[SQLITE_FCNTL_TEMPFILENAME]]
1342** ^Applications can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control
1343** to have SQLite generate a
1344** temporary filename using the same algorithm that is followed to generate
1345** temporary filenames for TEMP tables and other internal uses. The
1346** argument should be a char** which will be filled with the filename
1347** written into memory obtained from [sqlite3_malloc()]. The caller should
1348** invoke [sqlite3_free()] on the result to avoid a memory leak.
1349**
1350** <li>[[SQLITE_FCNTL_MMAP_SIZE]]
1351** The [SQLITE_FCNTL_MMAP_SIZE] file control is used to query or set the
1352** maximum number of bytes that will be used for memory-mapped I/O.
1353** The argument is a pointer to a value of type sqlite3_int64 that
1354** is an advisory maximum number of bytes in the file to memory map. The
1355** pointer is overwritten with the old value. The limit is not changed if
1356** the value originally pointed to is negative, and so the current limit
1357** can be queried by passing in a pointer to a negative number. This
1358** file-control is used internally to implement [PRAGMA mmap_size].
1359**
1360** <li>[[SQLITE_FCNTL_TRACE]]
1361** The [SQLITE_FCNTL_TRACE] file control provides advisory information
1362** to the VFS about what the higher layers of the SQLite stack are doing.
1363** This file control is used by some VFS activity tracing [shims].
1364** The argument is a zero-terminated string. Higher layers in the
1365** SQLite stack may generate instances of this file control if
1366** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled.
1367**
1368** <li>[[SQLITE_FCNTL_HAS_MOVED]]
1369** The [SQLITE_FCNTL_HAS_MOVED] file control interprets its argument as a
1370** pointer to an integer and it writes a boolean into that integer depending
1371** on whether or not the file has been renamed, moved, or deleted since it
1372** was first opened.
1373**
1374** <li>[[SQLITE_FCNTL_WIN32_GET_HANDLE]]
1375** The [SQLITE_FCNTL_WIN32_GET_HANDLE] opcode can be used to obtain the
1376** underlying native file handle associated with a file handle. This file
1377** control interprets its argument as a pointer to a native file handle and
1378** writes the resulting value there.
1379**
1380** <li>[[SQLITE_FCNTL_WIN32_SET_HANDLE]]
1381** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This
1382** opcode causes the xFileControl method to swap the file handle with the one
1383** pointed to by the pArg argument. This capability is used during testing
1384** and only needs to be supported when SQLITE_TEST is defined.
1385**
1386** <li>[[SQLITE_FCNTL_WAL_BLOCK]]
1387** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might
1388** be advantageous to block on the next WAL lock if the lock is not immediately
1389** available. The WAL subsystem issues this signal during rare
1390** circumstances in order to fix a problem with priority inversion.
1391** Applications should <em>not</em> use this file-control.
1392**
1393** <li>[[SQLITE_FCNTL_ZIPVFS]]
1394** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other
1395** VFS should return SQLITE_NOTFOUND for this opcode.
1396**
1397** <li>[[SQLITE_FCNTL_RBU]]
1398** The [SQLITE_FCNTL_RBU] opcode is implemented by the special VFS used by
1399** the RBU extension only. All other VFS should return SQLITE_NOTFOUND for
1400** this opcode.
1401**
1402** <li>[[SQLITE_FCNTL_BEGIN_ATOMIC_WRITE]]
1403** If the [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] opcode returns SQLITE_OK, then
1404** the file descriptor is placed in "batch write mode", which
1405** means all subsequent write operations will be deferred and done
1406** atomically at the next [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE]. Systems
1407** that do not support batch atomic writes will return SQLITE_NOTFOUND.
1408** ^Following a successful SQLITE_FCNTL_BEGIN_ATOMIC_WRITE and prior to
1409** the closing [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] or
1410** [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE], SQLite will make
1411** no VFS interface calls on the same [sqlite3_file] file descriptor
1412** except for calls to the xWrite method and the xFileControl method
1413** with [SQLITE_FCNTL_SIZE_HINT].
1414**
1415** <li>[[SQLITE_FCNTL_COMMIT_ATOMIC_WRITE]]
1416** The [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] opcode causes all write
1417** operations since the previous successful call to
1418** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be performed atomically.
1419** This file control returns [SQLITE_OK] if and only if the writes were
1420** all performed successfully and have been committed to persistent storage.
1421** ^Regardless of whether or not it is successful, this file control takes
1422** the file descriptor out of batch write mode so that all subsequent
1423** write operations are independent.
1424** ^SQLite will never invoke SQLITE_FCNTL_COMMIT_ATOMIC_WRITE without
1425** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
1426**
1427** <li>[[SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE]]
1428** The [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE] opcode causes all write
1429** operations since the previous successful call to
1430** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be rolled back.
1431** ^This file control takes the file descriptor out of batch write mode
1432** so that all subsequent write operations are independent.
1433** ^SQLite will never invoke SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE without
1434** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
1435**
1436** <li>[[SQLITE_FCNTL_LOCK_TIMEOUT]]
1437** The [SQLITE_FCNTL_LOCK_TIMEOUT] opcode is used to configure a VFS
1438** to block for up to M milliseconds before failing when attempting to
1439** obtain a file lock using the xLock or xShmLock methods of the VFS.
1440** The parameter is a pointer to a 32-bit signed integer that contains
1441** the value that M is to be set to. Before returning, the 32-bit signed
1442** integer is overwritten with the previous value of M.
1443**
1444** <li>[[SQLITE_FCNTL_DATA_VERSION]]
1445** The [SQLITE_FCNTL_DATA_VERSION] opcode is used to detect changes to
1446** a database file. The argument is a pointer to a 32-bit unsigned integer.
1447** The "data version" for the pager is written into the pointer. The
1448** "data version" changes whenever any change occurs to the corresponding
1449** database file, either through SQL statements on the same database
1450** connection or through transactions committed by separate database
1451** connections possibly in other processes. The [sqlite3_total_changes()]
1452** interface can be used to find if any database on the connection has changed,
1453** but that interface responds to changes on TEMP as well as MAIN and does
1454** not provide a mechanism to detect changes to MAIN only. Also, the
1455** [sqlite3_total_changes()] interface responds to internal changes only and
1456** omits changes made by other database connections. The
1457** [PRAGMA data_version] command provides a mechanism to detect changes to
1458** a single attached database that occur due to other database connections,
1459** but omits changes implemented by the database connection on which it is
1460** called. This file control is the only mechanism to detect changes that
1461** happen either internally or externally and that are associated with
1462** a particular attached database.
1463**
1464** <li>[[SQLITE_FCNTL_CKPT_START]]
1465** The [SQLITE_FCNTL_CKPT_START] opcode is invoked from within a checkpoint
1466** in wal mode before the client starts to copy pages from the wal
1467** file to the database file.
1468**
1469** <li>[[SQLITE_FCNTL_CKPT_DONE]]
1470** The [SQLITE_FCNTL_CKPT_DONE] opcode is invoked from within a checkpoint
1471** in wal mode after the client has finished copying pages from the wal
1472** file to the database file, but before the *-shm file is updated to
1473** record the fact that the pages have been checkpointed.
1474** </ul>
1475**
1476** <li>[[SQLITE_FCNTL_EXTERNAL_READER]]
1477** The EXPERIMENTAL [SQLITE_FCNTL_EXTERNAL_READER] opcode is used to detect
1478** whether or not there is a database client in another process with a wal-mode
1479** transaction open on the database or not. It is only available on unix.The
1480** (void*) argument passed with this file-control should be a pointer to a
1481** value of type (int). The integer value is set to 1 if the database is a wal
1482** mode database and there exists at least one client in another process that
1483** currently has an SQL transaction open on the database. It is set to 0 if
1484** the database is not a wal-mode db, or if there is no such connection in any
1485** other process. This opcode cannot be used to detect transactions opened
1486** by clients within the current process, only within other processes.
1487** </ul>
1488**
1489** <li>[[SQLITE_FCNTL_CKSM_FILE]]
1490** Used by the cksmvfs VFS module only.
1491** </ul>
1492*/
1493#define SQLITE_FCNTL_LOCKSTATE 1
1494#define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
1495#define SQLITE_FCNTL_SET_LOCKPROXYFILE 3
1496#define SQLITE_FCNTL_LAST_ERRNO 4
1497#define SQLITE_FCNTL_SIZE_HINT 5
1498#define SQLITE_FCNTL_CHUNK_SIZE 6
1499#define SQLITE_FCNTL_FILE_POINTER 7
1500#define SQLITE_FCNTL_SYNC_OMITTED 8
1501#define SQLITE_FCNTL_WIN32_AV_RETRY 9
1502#define SQLITE_FCNTL_PERSIST_WAL 10
1503#define SQLITE_FCNTL_OVERWRITE 11
1504#define SQLITE_FCNTL_VFSNAME 12
1505#define SQLITE_FCNTL_POWERSAFE_OVERWRITE 13
1506#define SQLITE_FCNTL_PRAGMA 14
1507#define SQLITE_FCNTL_BUSYHANDLER 15
1508#define SQLITE_FCNTL_TEMPFILENAME 16
1509#define SQLITE_FCNTL_MMAP_SIZE 18
1510#define SQLITE_FCNTL_TRACE 19
1511#define SQLITE_FCNTL_HAS_MOVED 20
1512#define SQLITE_FCNTL_SYNC 21
1513#define SQLITE_FCNTL_COMMIT_PHASETWO 22
1514#define SQLITE_FCNTL_WIN32_SET_HANDLE 23
1515#define SQLITE_FCNTL_WAL_BLOCK 24
1516#define SQLITE_FCNTL_ZIPVFS 25
1517#define SQLITE_FCNTL_RBU 26
1518#define SQLITE_FCNTL_VFS_POINTER 27
1519#define SQLITE_FCNTL_JOURNAL_POINTER 28
1520#define SQLITE_FCNTL_WIN32_GET_HANDLE 29
1521#define SQLITE_FCNTL_PDB 30
1522#define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE 31
1523#define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE 32
1524#define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE 33
1525#define SQLITE_FCNTL_LOCK_TIMEOUT 34
1526#define SQLITE_FCNTL_DATA_VERSION 35
1527#define SQLITE_FCNTL_SIZE_LIMIT 36
1528#define SQLITE_FCNTL_CKPT_DONE 37
1529#define SQLITE_FCNTL_RESERVE_BYTES 38
1530#define SQLITE_FCNTL_CKPT_START 39
1531#define SQLITE_FCNTL_EXTERNAL_READER 40
1532#define SQLITE_FCNTL_CKSM_FILE 41
1533
1534/* deprecated names */
1535#define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
1536#define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
1537#define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
1538
1539
1540/*
1541** CAPI3REF: Mutex Handle
1542**
1543** The mutex module within SQLite defines [sqlite3_mutex] to be an
1544** abstract type for a mutex object. The SQLite core never looks
1545** at the internal representation of an [sqlite3_mutex]. It only
1546** deals with pointers to the [sqlite3_mutex] object.
1547**
1548** Mutexes are created using [sqlite3_mutex_alloc()].
1549*/
1550typedef struct sqlite3_mutex sqlite3_mutex;
1551
1552/*
1553** CAPI3REF: Loadable Extension Thunk
1554**
1555** A pointer to the opaque sqlite3_api_routines structure is passed as
1556** the third parameter to entry points of [loadable extensions]. This
1557** structure must be typedefed in order to work around compiler warnings
1558** on some platforms.
1559*/
1560typedef struct sqlite3_api_routines sqlite3_api_routines;
1561
1562/*
1563** CAPI3REF: OS Interface Object
1564**
1565** An instance of the sqlite3_vfs object defines the interface between
1566** the SQLite core and the underlying operating system. The "vfs"
1567** in the name of the object stands for "virtual file system". See
1568** the [VFS | VFS documentation] for further information.
1569**
1570** The VFS interface is sometimes extended by adding new methods onto
1571** the end. Each time such an extension occurs, the iVersion field
1572** is incremented. The iVersion value started out as 1 in
1573** SQLite [version 3.5.0] on [dateof:3.5.0], then increased to 2
1574** with SQLite [version 3.7.0] on [dateof:3.7.0], and then increased
1575** to 3 with SQLite [version 3.7.6] on [dateof:3.7.6]. Additional fields
1576** may be appended to the sqlite3_vfs object and the iVersion value
1577** may increase again in future versions of SQLite.
1578** Note that due to an oversight, the structure
1579** of the sqlite3_vfs object changed in the transition from
1580** SQLite [version 3.5.9] to [version 3.6.0] on [dateof:3.6.0]
1581** and yet the iVersion field was not increased.
1582**
1583** The szOsFile field is the size of the subclassed [sqlite3_file]
1584** structure used by this VFS. mxPathname is the maximum length of
1585** a pathname in this VFS.
1586**
1587** Registered sqlite3_vfs objects are kept on a linked list formed by
1588** the pNext pointer. The [sqlite3_vfs_register()]
1589** and [sqlite3_vfs_unregister()] interfaces manage this list
1590** in a thread-safe way. The [sqlite3_vfs_find()] interface
1591** searches the list. Neither the application code nor the VFS
1592** implementation should use the pNext pointer.
1593**
1594** The pNext field is the only field in the sqlite3_vfs
1595** structure that SQLite will ever modify. SQLite will only access
1596** or modify this field while holding a particular static mutex.
1597** The application should never modify anything within the sqlite3_vfs
1598** object once the object has been registered.
1599**
1600** The zName field holds the name of the VFS module. The name must
1601** be unique across all VFS modules.
1602**
1603** [[sqlite3_vfs.xOpen]]
1604** ^SQLite guarantees that the zFilename parameter to xOpen
1605** is either a NULL pointer or string obtained
1606** from xFullPathname() with an optional suffix added.
1607** ^If a suffix is added to the zFilename parameter, it will
1608** consist of a single "-" character followed by no more than
1609** 11 alphanumeric and/or "-" characters.
1610** ^SQLite further guarantees that
1611** the string will be valid and unchanged until xClose() is
1612** called. Because of the previous sentence,
1613** the [sqlite3_file] can safely store a pointer to the
1614** filename if it needs to remember the filename for some reason.
1615** If the zFilename parameter to xOpen is a NULL pointer then xOpen
1616** must invent its own temporary name for the file. ^Whenever the
1617** xFilename parameter is NULL it will also be the case that the
1618** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE].
1619**
1620** The flags argument to xOpen() includes all bits set in
1621** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()]
1622** or [sqlite3_open16()] is used, then flags includes at least
1623** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE].
1624** If xOpen() opens a file read-only then it sets *pOutFlags to
1625** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be set.
1626**
1627** ^(SQLite will also add one of the following flags to the xOpen()
1628** call, depending on the object being opened:
1629**
1630** <ul>
1631** <li> [SQLITE_OPEN_MAIN_DB]
1632** <li> [SQLITE_OPEN_MAIN_JOURNAL]
1633** <li> [SQLITE_OPEN_TEMP_DB]
1634** <li> [SQLITE_OPEN_TEMP_JOURNAL]
1635** <li> [SQLITE_OPEN_TRANSIENT_DB]
1636** <li> [SQLITE_OPEN_SUBJOURNAL]
1637** <li> [SQLITE_OPEN_SUPER_JOURNAL]
1638** <li> [SQLITE_OPEN_WAL]
1639** </ul>)^
1640**
1641** The file I/O implementation can use the object type flags to
1642** change the way it deals with files. For example, an application
1643** that does not care about crash recovery or rollback might make
1644** the open of a journal file a no-op. Writes to this journal would
1645** also be no-ops, and any attempt to read the journal would return
1646** SQLITE_IOERR. Or the implementation might recognize that a database
1647** file will be doing page-aligned sector reads and writes in a random
1648** order and set up its I/O subsystem accordingly.
1649**
1650** SQLite might also add one of the following flags to the xOpen method:
1651**
1652** <ul>
1653** <li> [SQLITE_OPEN_DELETEONCLOSE]
1654** <li> [SQLITE_OPEN_EXCLUSIVE]
1655** </ul>
1656**
1657** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be
1658** deleted when it is closed. ^The [SQLITE_OPEN_DELETEONCLOSE]
1659** will be set for TEMP databases and their journals, transient
1660** databases, and subjournals.
1661**
1662** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction
1663** with the [SQLITE_OPEN_CREATE] flag, which are both directly
1664** analogous to the O_EXCL and O_CREAT flags of the POSIX open()
1665** API. The SQLITE_OPEN_EXCLUSIVE flag, when paired with the
1666** SQLITE_OPEN_CREATE, is used to indicate that file should always
1667** be created, and that it is an error if it already exists.
1668** It is <i>not</i> used to indicate the file should be opened
1669** for exclusive access.
1670**
1671** ^At least szOsFile bytes of memory are allocated by SQLite
1672** to hold the [sqlite3_file] structure passed as the third
1673** argument to xOpen. The xOpen method does not have to
1674** allocate the structure; it should just fill it in. Note that
1675** the xOpen method must set the sqlite3_file.pMethods to either
1676** a valid [sqlite3_io_methods] object or to NULL. xOpen must do
1677** this even if the open fails. SQLite expects that the sqlite3_file.pMethods
1678** element will be valid after xOpen returns regardless of the success
1679** or failure of the xOpen call.
1680**
1681** [[sqlite3_vfs.xAccess]]
1682** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
1683** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
1684** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
1685** to test whether a file is at least readable. The SQLITE_ACCESS_READ
1686** flag is never actually used and is not implemented in the built-in
1687** VFSes of SQLite. The file is named by the second argument and can be a
1688** directory. The xAccess method returns [SQLITE_OK] on success or some
1689** non-zero error code if there is an I/O error or if the name of
1690** the file given in the second argument is illegal. If SQLITE_OK
1691** is returned, then non-zero or zero is written into *pResOut to indicate
1692** whether or not the file is accessible.
1693**
1694** ^SQLite will always allocate at least mxPathname+1 bytes for the
1695** output buffer xFullPathname. The exact size of the output buffer
1696** is also passed as a parameter to both methods. If the output buffer
1697** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is
1698** handled as a fatal error by SQLite, vfs implementations should endeavor
1699** to prevent this by setting mxPathname to a sufficiently large value.
1700**
1701** The xRandomness(), xSleep(), xCurrentTime(), and xCurrentTimeInt64()
1702** interfaces are not strictly a part of the filesystem, but they are
1703** included in the VFS structure for completeness.
1704** The xRandomness() function attempts to return nBytes bytes
1705** of good-quality randomness into zOut. The return value is
1706** the actual number of bytes of randomness obtained.
1707** The xSleep() method causes the calling thread to sleep for at
1708** least the number of microseconds given. ^The xCurrentTime()
1709** method returns a Julian Day Number for the current date and time as
1710** a floating point value.
1711** ^The xCurrentTimeInt64() method returns, as an integer, the Julian
1712** Day Number multiplied by 86400000 (the number of milliseconds in
1713** a 24-hour day).
1714** ^SQLite will use the xCurrentTimeInt64() method to get the current
1715** date and time if that method is available (if iVersion is 2 or
1716** greater and the function pointer is not NULL) and will fall back
1717** to xCurrentTime() if xCurrentTimeInt64() is unavailable.
1718**
1719** ^The xSetSystemCall(), xGetSystemCall(), and xNestSystemCall() interfaces
1720** are not used by the SQLite core. These optional interfaces are provided
1721** by some VFSes to facilitate testing of the VFS code. By overriding
1722** system calls with functions under its control, a test program can
1723** simulate faults and error conditions that would otherwise be difficult
1724** or impossible to induce. The set of system calls that can be overridden
1725** varies from one VFS to another, and from one version of the same VFS to the
1726** next. Applications that use these interfaces must be prepared for any
1727** or all of these interfaces to be NULL or for their behavior to change
1728** from one release to the next. Applications must not attempt to access
1729** any of these methods if the iVersion of the VFS is less than 3.
1730*/
1731typedef struct sqlite3_vfs sqlite3_vfs;
1732typedef void (*sqlite3_syscall_ptr)(void);
1733struct sqlite3_vfs {
1734 int iVersion; /* Structure version number (currently 3) */
1735 int szOsFile; /* Size of subclassed sqlite3_file */
1736 int mxPathname; /* Maximum file pathname length */
1737 sqlite3_vfs *pNext; /* Next registered VFS */
1738 const char *zName; /* Name of this virtual file system */
1739 void *pAppData; /* Pointer to application-specific data */
1740 int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*,
1741 int flags, int *pOutFlags);
1742 int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
1743 int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
1744 int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
1745 void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
1746 void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
1747 void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
1748 void (*xDlClose)(sqlite3_vfs*, void*);
1749 int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
1750 int (*xSleep)(sqlite3_vfs*, int microseconds);
1751 int (*xCurrentTime)(sqlite3_vfs*, double*);
1752 int (*xGetLastError)(sqlite3_vfs*, int, char *);
1753 /*
1754 ** The methods above are in version 1 of the sqlite_vfs object
1755 ** definition. Those that follow are added in version 2 or later
1756 */
1757 int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
1758 /*
1759 ** The methods above are in versions 1 and 2 of the sqlite_vfs object.
1760 ** Those below are for version 3 and greater.
1761 */
1762 int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr);
1763 sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName);
1764 const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
1765 /*
1766 ** The methods above are in versions 1 through 3 of the sqlite_vfs object.
1767 ** New fields may be appended in future versions. The iVersion
1768 ** value will increment whenever this happens.
1769 */
1770};
1771
1772/*
1773** CAPI3REF: Flags for the xAccess VFS method
1774**
1775** These integer constants can be used as the third parameter to
1776** the xAccess method of an [sqlite3_vfs] object. They determine
1777** what kind of permissions the xAccess method is looking for.
1778** With SQLITE_ACCESS_EXISTS, the xAccess method
1779** simply checks whether the file exists.
1780** With SQLITE_ACCESS_READWRITE, the xAccess method
1781** checks whether the named directory is both readable and writable
1782** (in other words, if files can be added, removed, and renamed within
1783** the directory).
1784** The SQLITE_ACCESS_READWRITE constant is currently used only by the
1785** [temp_store_directory pragma], though this could change in a future
1786** release of SQLite.
1787** With SQLITE_ACCESS_READ, the xAccess method
1788** checks whether the file is readable. The SQLITE_ACCESS_READ constant is
1789** currently unused, though it might be used in a future release of
1790** SQLite.
1791*/
1792#define SQLITE_ACCESS_EXISTS 0
1793#define SQLITE_ACCESS_READWRITE 1 /* Used by PRAGMA temp_store_directory */
1794#define SQLITE_ACCESS_READ 2 /* Unused */
1795
1796/*
1797** CAPI3REF: Flags for the xShmLock VFS method
1798**
1799** These integer constants define the various locking operations
1800** allowed by the xShmLock method of [sqlite3_io_methods]. The
1801** following are the only legal combinations of flags to the
1802** xShmLock method:
1803**
1804** <ul>
1805** <li> SQLITE_SHM_LOCK | SQLITE_SHM_SHARED
1806** <li> SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE
1807** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED
1808** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE
1809** </ul>
1810**
1811** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as
1812** was given on the corresponding lock.
1813**
1814** The xShmLock method can transition between unlocked and SHARED or
1815** between unlocked and EXCLUSIVE. It cannot transition between SHARED
1816** and EXCLUSIVE.
1817*/
1818#define SQLITE_SHM_UNLOCK 1
1819#define SQLITE_SHM_LOCK 2
1820#define SQLITE_SHM_SHARED 4
1821#define SQLITE_SHM_EXCLUSIVE 8
1822
1823/*
1824** CAPI3REF: Maximum xShmLock index
1825**
1826** The xShmLock method on [sqlite3_io_methods] may use values
1827** between 0 and this upper bound as its "offset" argument.
1828** The SQLite core will never attempt to acquire or release a
1829** lock outside of this range
1830*/
1831#define SQLITE_SHM_NLOCK 8
1832
1833
1834/*
1835** CAPI3REF: Initialize The SQLite Library
1836**
1837** ^The sqlite3_initialize() routine initializes the
1838** SQLite library. ^The sqlite3_shutdown() routine
1839** deallocates any resources that were allocated by sqlite3_initialize().
1840** These routines are designed to aid in process initialization and
1841** shutdown on embedded systems. Workstation applications using
1842** SQLite normally do not need to invoke either of these routines.
1843**
1844** A call to sqlite3_initialize() is an "effective" call if it is
1845** the first time sqlite3_initialize() is invoked during the lifetime of
1846** the process, or if it is the first time sqlite3_initialize() is invoked
1847** following a call to sqlite3_shutdown(). ^(Only an effective call
1848** of sqlite3_initialize() does any initialization. All other calls
1849** are harmless no-ops.)^
1850**
1851** A call to sqlite3_shutdown() is an "effective" call if it is the first
1852** call to sqlite3_shutdown() since the last sqlite3_initialize(). ^(Only
1853** an effective call to sqlite3_shutdown() does any deinitialization.
1854** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
1855**
1856** The sqlite3_initialize() interface is threadsafe, but sqlite3_shutdown()
1857** is not. The sqlite3_shutdown() interface must only be called from a
1858** single thread. All open [database connections] must be closed and all
1859** other SQLite resources must be deallocated prior to invoking
1860** sqlite3_shutdown().
1861**
1862** Among other things, ^sqlite3_initialize() will invoke
1863** sqlite3_os_init(). Similarly, ^sqlite3_shutdown()
1864** will invoke sqlite3_os_end().
1865**
1866** ^The sqlite3_initialize() routine returns [SQLITE_OK] on success.
1867** ^If for some reason, sqlite3_initialize() is unable to initialize
1868** the library (perhaps it is unable to allocate a needed resource such
1869** as a mutex) it returns an [error code] other than [SQLITE_OK].
1870**
1871** ^The sqlite3_initialize() routine is called internally by many other
1872** SQLite interfaces so that an application usually does not need to
1873** invoke sqlite3_initialize() directly. For example, [sqlite3_open()]
1874** calls sqlite3_initialize() so the SQLite library will be automatically
1875** initialized when [sqlite3_open()] is called if it has not be initialized
1876** already. ^However, if SQLite is compiled with the [SQLITE_OMIT_AUTOINIT]
1877** compile-time option, then the automatic calls to sqlite3_initialize()
1878** are omitted and the application must call sqlite3_initialize() directly
1879** prior to using any other SQLite interface. For maximum portability,
1880** it is recommended that applications always invoke sqlite3_initialize()
1881** directly prior to using any other SQLite interface. Future releases
1882** of SQLite may require this. In other words, the behavior exhibited
1883** when SQLite is compiled with [SQLITE_OMIT_AUTOINIT] might become the
1884** default behavior in some future release of SQLite.
1885**
1886** The sqlite3_os_init() routine does operating-system specific
1887** initialization of the SQLite library. The sqlite3_os_end()
1888** routine undoes the effect of sqlite3_os_init(). Typical tasks
1889** performed by these routines include allocation or deallocation
1890** of static resources, initialization of global variables,
1891** setting up a default [sqlite3_vfs] module, or setting up
1892** a default configuration using [sqlite3_config()].
1893**
1894** The application should never invoke either sqlite3_os_init()
1895** or sqlite3_os_end() directly. The application should only invoke
1896** sqlite3_initialize() and sqlite3_shutdown(). The sqlite3_os_init()
1897** interface is called automatically by sqlite3_initialize() and
1898** sqlite3_os_end() is called by sqlite3_shutdown(). Appropriate
1899** implementations for sqlite3_os_init() and sqlite3_os_end()
1900** are built into SQLite when it is compiled for Unix, Windows, or OS/2.
1901** When [custom builds | built for other platforms]
1902** (using the [SQLITE_OS_OTHER=1] compile-time
1903** option) the application must supply a suitable implementation for
1904** sqlite3_os_init() and sqlite3_os_end(). An application-supplied
1905** implementation of sqlite3_os_init() or sqlite3_os_end()
1906** must return [SQLITE_OK] on success and some other [error code] upon
1907** failure.
1908*/
1909SQLITE_API int sqlite3_initialize(void);
1910SQLITE_API int sqlite3_shutdown(void);
1911SQLITE_API int sqlite3_os_init(void);
1912SQLITE_API int sqlite3_os_end(void);
1913
1914/*
1915** CAPI3REF: Configuring The SQLite Library
1916**
1917** The sqlite3_config() interface is used to make global configuration
1918** changes to SQLite in order to tune SQLite to the specific needs of
1919** the application. The default configuration is recommended for most
1920** applications and so this routine is usually not necessary. It is
1921** provided to support rare applications with unusual needs.
1922**
1923** <b>The sqlite3_config() interface is not threadsafe. The application
1924** must ensure that no other SQLite interfaces are invoked by other
1925** threads while sqlite3_config() is running.</b>
1926**
1927** The sqlite3_config() interface
1928** may only be invoked prior to library initialization using
1929** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()].
1930** ^If sqlite3_config() is called after [sqlite3_initialize()] and before
1931** [sqlite3_shutdown()] then it will return SQLITE_MISUSE.
1932** Note, however, that ^sqlite3_config() can be called as part of the
1933** implementation of an application-defined [sqlite3_os_init()].
1934**
1935** The first argument to sqlite3_config() is an integer
1936** [configuration option] that determines
1937** what property of SQLite is to be configured. Subsequent arguments
1938** vary depending on the [configuration option]
1939** in the first argument.
1940**
1941** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK].
1942** ^If the option is unknown or SQLite is unable to set the option
1943** then this routine returns a non-zero [error code].
1944*/
1945SQLITE_API int sqlite3_config(int, ...);
1946
1947/*
1948** CAPI3REF: Configure database connections
1949** METHOD: sqlite3
1950**
1951** The sqlite3_db_config() interface is used to make configuration
1952** changes to a [database connection]. The interface is similar to
1953** [sqlite3_config()] except that the changes apply to a single
1954** [database connection] (specified in the first argument).
1955**
1956** The second argument to sqlite3_db_config(D,V,...) is the
1957** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code
1958** that indicates what aspect of the [database connection] is being configured.
1959** Subsequent arguments vary depending on the configuration verb.
1960**
1961** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if
1962** the call is considered successful.
1963*/
1964SQLITE_API int sqlite3_db_config(sqlite3*, int op, ...);
1965
1966/*
1967** CAPI3REF: Memory Allocation Routines
1968**
1969** An instance of this object defines the interface between SQLite
1970** and low-level memory allocation routines.
1971**
1972** This object is used in only one place in the SQLite interface.
1973** A pointer to an instance of this object is the argument to
1974** [sqlite3_config()] when the configuration option is
1975** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC].
1976** By creating an instance of this object
1977** and passing it to [sqlite3_config]([SQLITE_CONFIG_MALLOC])
1978** during configuration, an application can specify an alternative
1979** memory allocation subsystem for SQLite to use for all of its
1980** dynamic memory needs.
1981**
1982** Note that SQLite comes with several [built-in memory allocators]
1983** that are perfectly adequate for the overwhelming majority of applications
1984** and that this object is only useful to a tiny minority of applications
1985** with specialized memory allocation requirements. This object is
1986** also used during testing of SQLite in order to specify an alternative
1987** memory allocator that simulates memory out-of-memory conditions in
1988** order to verify that SQLite recovers gracefully from such
1989** conditions.
1990**
1991** The xMalloc, xRealloc, and xFree methods must work like the
1992** malloc(), realloc() and free() functions from the standard C library.
1993** ^SQLite guarantees that the second argument to
1994** xRealloc is always a value returned by a prior call to xRoundup.
1995**
1996** xSize should return the allocated size of a memory allocation
1997** previously obtained from xMalloc or xRealloc. The allocated size
1998** is always at least as big as the requested size but may be larger.
1999**
2000** The xRoundup method returns what would be the allocated size of
2001** a memory allocation given a particular requested size. Most memory
2002** allocators round up memory allocations at least to the next multiple
2003** of 8. Some allocators round up to a larger multiple or to a power of 2.
2004** Every memory allocation request coming in through [sqlite3_malloc()]
2005** or [sqlite3_realloc()] first calls xRoundup. If xRoundup returns 0,
2006** that causes the corresponding memory allocation to fail.
2007**
2008** The xInit method initializes the memory allocator. For example,
2009** it might allocate any required mutexes or initialize internal data
2010** structures. The xShutdown method is invoked (indirectly) by
2011** [sqlite3_shutdown()] and should deallocate any resources acquired
2012** by xInit. The pAppData pointer is used as the only parameter to
2013** xInit and xShutdown.
2014**
2015** SQLite holds the [SQLITE_MUTEX_STATIC_MAIN] mutex when it invokes
2016** the xInit method, so the xInit method need not be threadsafe. The
2017** xShutdown method is only called from [sqlite3_shutdown()] so it does
2018** not need to be threadsafe either. For all other methods, SQLite
2019** holds the [SQLITE_MUTEX_STATIC_MEM] mutex as long as the
2020** [SQLITE_CONFIG_MEMSTATUS] configuration option is turned on (which
2021** it is by default) and so the methods are automatically serialized.
2022** However, if [SQLITE_CONFIG_MEMSTATUS] is disabled, then the other
2023** methods must be threadsafe or else make their own arrangements for
2024** serialization.
2025**
2026** SQLite will never invoke xInit() more than once without an intervening
2027** call to xShutdown().
2028*/
2029typedef struct sqlite3_mem_methods sqlite3_mem_methods;
2030struct sqlite3_mem_methods {
2031 void *(*xMalloc)(int); /* Memory allocation function */
2032 void (*xFree)(void*); /* Free a prior allocation */
2033 void *(*xRealloc)(void*,int); /* Resize an allocation */
2034 int (*xSize)(void*); /* Return the size of an allocation */
2035 int (*xRoundup)(int); /* Round up request size to allocation size */
2036 int (*xInit)(void*); /* Initialize the memory allocator */
2037 void (*xShutdown)(void*); /* Deinitialize the memory allocator */
2038 void *pAppData; /* Argument to xInit() and xShutdown() */
2039};
2040
2041/*
2042** CAPI3REF: Configuration Options
2043** KEYWORDS: {configuration option}
2044**
2045** These constants are the available integer configuration options that
2046** can be passed as the first argument to the [sqlite3_config()] interface.
2047**
2048** New configuration options may be added in future releases of SQLite.
2049** Existing configuration options might be discontinued. Applications
2050** should check the return code from [sqlite3_config()] to make sure that
2051** the call worked. The [sqlite3_config()] interface will return a
2052** non-zero [error code] if a discontinued or unsupported configuration option
2053** is invoked.
2054**
2055** <dl>
2056** [[SQLITE_CONFIG_SINGLETHREAD]] <dt>SQLITE_CONFIG_SINGLETHREAD</dt>
2057** <dd>There are no arguments to this option. ^This option sets the
2058** [threading mode] to Single-thread. In other words, it disables
2059** all mutexing and puts SQLite into a mode where it can only be used
2060** by a single thread. ^If SQLite is compiled with
2061** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2062** it is not possible to change the [threading mode] from its default
2063** value of Single-thread and so [sqlite3_config()] will return
2064** [SQLITE_ERROR] if called with the SQLITE_CONFIG_SINGLETHREAD
2065** configuration option.</dd>
2066**
2067** [[SQLITE_CONFIG_MULTITHREAD]] <dt>SQLITE_CONFIG_MULTITHREAD</dt>
2068** <dd>There are no arguments to this option. ^This option sets the
2069** [threading mode] to Multi-thread. In other words, it disables
2070** mutexing on [database connection] and [prepared statement] objects.
2071** The application is responsible for serializing access to
2072** [database connections] and [prepared statements]. But other mutexes
2073** are enabled so that SQLite will be safe to use in a multi-threaded
2074** environment as long as no two threads attempt to use the same
2075** [database connection] at the same time. ^If SQLite is compiled with
2076** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2077** it is not possible to set the Multi-thread [threading mode] and
2078** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
2079** SQLITE_CONFIG_MULTITHREAD configuration option.</dd>
2080**
2081** [[SQLITE_CONFIG_SERIALIZED]] <dt>SQLITE_CONFIG_SERIALIZED</dt>
2082** <dd>There are no arguments to this option. ^This option sets the
2083** [threading mode] to Serialized. In other words, this option enables
2084** all mutexes including the recursive
2085** mutexes on [database connection] and [prepared statement] objects.
2086** In this mode (which is the default when SQLite is compiled with
2087** [SQLITE_THREADSAFE=1]) the SQLite library will itself serialize access
2088** to [database connections] and [prepared statements] so that the
2089** application is free to use the same [database connection] or the
2090** same [prepared statement] in different threads at the same time.
2091** ^If SQLite is compiled with
2092** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2093** it is not possible to set the Serialized [threading mode] and
2094** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
2095** SQLITE_CONFIG_SERIALIZED configuration option.</dd>
2096**
2097** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt>
2098** <dd> ^(The SQLITE_CONFIG_MALLOC option takes a single argument which is
2099** a pointer to an instance of the [sqlite3_mem_methods] structure.
2100** The argument specifies
2101** alternative low-level memory allocation routines to be used in place of
2102** the memory allocation routines built into SQLite.)^ ^SQLite makes
2103** its own private copy of the content of the [sqlite3_mem_methods] structure
2104** before the [sqlite3_config()] call returns.</dd>
2105**
2106** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt>
2107** <dd> ^(The SQLITE_CONFIG_GETMALLOC option takes a single argument which
2108** is a pointer to an instance of the [sqlite3_mem_methods] structure.
2109** The [sqlite3_mem_methods]
2110** structure is filled with the currently defined memory allocation routines.)^
2111** This option can be used to overload the default memory allocation
2112** routines with a wrapper that simulations memory allocation failure or
2113** tracks memory usage, for example. </dd>
2114**
2115** [[SQLITE_CONFIG_SMALL_MALLOC]] <dt>SQLITE_CONFIG_SMALL_MALLOC</dt>
2116** <dd> ^The SQLITE_CONFIG_SMALL_MALLOC option takes single argument of
2117** type int, interpreted as a boolean, which if true provides a hint to
2118** SQLite that it should avoid large memory allocations if possible.
2119** SQLite will run faster if it is free to make large memory allocations,
2120** but some application might prefer to run slower in exchange for
2121** guarantees about memory fragmentation that are possible if large
2122** allocations are avoided. This hint is normally off.
2123** </dd>
2124**
2125** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
2126** <dd> ^The SQLITE_CONFIG_MEMSTATUS option takes single argument of type int,
2127** interpreted as a boolean, which enables or disables the collection of
2128** memory allocation statistics. ^(When memory allocation statistics are
2129** disabled, the following SQLite interfaces become non-operational:
2130** <ul>
2131** <li> [sqlite3_hard_heap_limit64()]
2132** <li> [sqlite3_memory_used()]
2133** <li> [sqlite3_memory_highwater()]
2134** <li> [sqlite3_soft_heap_limit64()]
2135** <li> [sqlite3_status64()]
2136** </ul>)^
2137** ^Memory allocation statistics are enabled by default unless SQLite is
2138** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
2139** allocation statistics are disabled by default.
2140** </dd>
2141**
2142** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
2143** <dd> The SQLITE_CONFIG_SCRATCH option is no longer used.
2144** </dd>
2145**
2146** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
2147** <dd> ^The SQLITE_CONFIG_PAGECACHE option specifies a memory pool
2148** that SQLite can use for the database page cache with the default page
2149** cache implementation.
2150** This configuration option is a no-op if an application-defined page
2151** cache implementation is loaded using the [SQLITE_CONFIG_PCACHE2].
2152** ^There are three arguments to SQLITE_CONFIG_PAGECACHE: A pointer to
2153** 8-byte aligned memory (pMem), the size of each page cache line (sz),
2154** and the number of cache lines (N).
2155** The sz argument should be the size of the largest database page
2156** (a power of two between 512 and 65536) plus some extra bytes for each
2157** page header. ^The number of extra bytes needed by the page header
2158** can be determined using [SQLITE_CONFIG_PCACHE_HDRSZ].
2159** ^It is harmless, apart from the wasted memory,
2160** for the sz parameter to be larger than necessary. The pMem
2161** argument must be either a NULL pointer or a pointer to an 8-byte
2162** aligned block of memory of at least sz*N bytes, otherwise
2163** subsequent behavior is undefined.
2164** ^When pMem is not NULL, SQLite will strive to use the memory provided
2165** to satisfy page cache needs, falling back to [sqlite3_malloc()] if
2166** a page cache line is larger than sz bytes or if all of the pMem buffer
2167** is exhausted.
2168** ^If pMem is NULL and N is non-zero, then each database connection
2169** does an initial bulk allocation for page cache memory
2170** from [sqlite3_malloc()] sufficient for N cache lines if N is positive or
2171** of -1024*N bytes if N is negative, . ^If additional
2172** page cache memory is needed beyond what is provided by the initial
2173** allocation, then SQLite goes to [sqlite3_malloc()] separately for each
2174** additional cache line. </dd>
2175**
2176** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
2177** <dd> ^The SQLITE_CONFIG_HEAP option specifies a static memory buffer
2178** that SQLite will use for all of its dynamic memory allocation needs
2179** beyond those provided for by [SQLITE_CONFIG_PAGECACHE].
2180** ^The SQLITE_CONFIG_HEAP option is only available if SQLite is compiled
2181** with either [SQLITE_ENABLE_MEMSYS3] or [SQLITE_ENABLE_MEMSYS5] and returns
2182** [SQLITE_ERROR] if invoked otherwise.
2183** ^There are three arguments to SQLITE_CONFIG_HEAP:
2184** An 8-byte aligned pointer to the memory,
2185** the number of bytes in the memory buffer, and the minimum allocation size.
2186** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
2187** to using its default memory allocator (the system malloc() implementation),
2188** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. ^If the
2189** memory pointer is not NULL then the alternative memory
2190** allocator is engaged to handle all of SQLites memory allocation needs.
2191** The first pointer (the memory pointer) must be aligned to an 8-byte
2192** boundary or subsequent behavior of SQLite will be undefined.
2193** The minimum allocation size is capped at 2**12. Reasonable values
2194** for the minimum allocation size are 2**5 through 2**8.</dd>
2195**
2196** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt>
2197** <dd> ^(The SQLITE_CONFIG_MUTEX option takes a single argument which is a
2198** pointer to an instance of the [sqlite3_mutex_methods] structure.
2199** The argument specifies alternative low-level mutex routines to be used
2200** in place the mutex routines built into SQLite.)^ ^SQLite makes a copy of
2201** the content of the [sqlite3_mutex_methods] structure before the call to
2202** [sqlite3_config()] returns. ^If SQLite is compiled with
2203** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2204** the entire mutexing subsystem is omitted from the build and hence calls to
2205** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will
2206** return [SQLITE_ERROR].</dd>
2207**
2208** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt>
2209** <dd> ^(The SQLITE_CONFIG_GETMUTEX option takes a single argument which
2210** is a pointer to an instance of the [sqlite3_mutex_methods] structure. The
2211** [sqlite3_mutex_methods]
2212** structure is filled with the currently defined mutex routines.)^
2213** This option can be used to overload the default mutex allocation
2214** routines with a wrapper used to track mutex usage for performance
2215** profiling or testing, for example. ^If SQLite is compiled with
2216** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2217** the entire mutexing subsystem is omitted from the build and hence calls to
2218** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will
2219** return [SQLITE_ERROR].</dd>
2220**
2221** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt>
2222** <dd> ^(The SQLITE_CONFIG_LOOKASIDE option takes two arguments that determine
2223** the default size of lookaside memory on each [database connection].
2224** The first argument is the
2225** size of each lookaside buffer slot and the second is the number of
2226** slots allocated to each database connection.)^ ^(SQLITE_CONFIG_LOOKASIDE
2227** sets the <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
2228** option to [sqlite3_db_config()] can be used to change the lookaside
2229** configuration on individual connections.)^ </dd>
2230**
2231** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt>
2232** <dd> ^(The SQLITE_CONFIG_PCACHE2 option takes a single argument which is
2233** a pointer to an [sqlite3_pcache_methods2] object. This object specifies
2234** the interface to a custom page cache implementation.)^
2235** ^SQLite makes a copy of the [sqlite3_pcache_methods2] object.</dd>
2236**
2237** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt>
2238** <dd> ^(The SQLITE_CONFIG_GETPCACHE2 option takes a single argument which
2239** is a pointer to an [sqlite3_pcache_methods2] object. SQLite copies of
2240** the current page cache implementation into that object.)^ </dd>
2241**
2242** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
2243** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite
2244** global [error log].
2245** (^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
2246** function with a call signature of void(*)(void*,int,const char*),
2247** and a pointer to void. ^If the function pointer is not NULL, it is
2248** invoked by [sqlite3_log()] to process each logging event. ^If the
2249** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
2250** ^The void pointer that is the second argument to SQLITE_CONFIG_LOG is
2251** passed through as the first parameter to the application-defined logger
2252** function whenever that function is invoked. ^The second parameter to
2253** the logger function is a copy of the first parameter to the corresponding
2254** [sqlite3_log()] call and is intended to be a [result code] or an
2255** [extended result code]. ^The third parameter passed to the logger is
2256** log message after formatting via [sqlite3_snprintf()].
2257** The SQLite logging interface is not reentrant; the logger function
2258** supplied by the application must not invoke any SQLite interface.
2259** In a multi-threaded application, the application-defined logger
2260** function must be threadsafe. </dd>
2261**
2262** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI
2263** <dd>^(The SQLITE_CONFIG_URI option takes a single argument of type int.
2264** If non-zero, then URI handling is globally enabled. If the parameter is zero,
2265** then URI handling is globally disabled.)^ ^If URI handling is globally
2266** enabled, all filenames passed to [sqlite3_open()], [sqlite3_open_v2()],
2267** [sqlite3_open16()] or
2268** specified as part of [ATTACH] commands are interpreted as URIs, regardless
2269** of whether or not the [SQLITE_OPEN_URI] flag is set when the database
2270** connection is opened. ^If it is globally disabled, filenames are
2271** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
2272** database connection is opened. ^(By default, URI handling is globally
2273** disabled. The default value may be changed by compiling with the
2274** [SQLITE_USE_URI] symbol defined.)^
2275**
2276** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN
2277** <dd>^The SQLITE_CONFIG_COVERING_INDEX_SCAN option takes a single integer
2278** argument which is interpreted as a boolean in order to enable or disable
2279** the use of covering indices for full table scans in the query optimizer.
2280** ^The default setting is determined
2281** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
2282** if that compile-time option is omitted.
2283** The ability to disable the use of covering indices for full table scans
2284** is because some incorrectly coded legacy applications might malfunction
2285** when the optimization is enabled. Providing the ability to
2286** disable the optimization allows the older, buggy application code to work
2287** without change even with newer versions of SQLite.
2288**
2289** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
2290** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
2291** <dd> These options are obsolete and should not be used by new code.
2292** They are retained for backwards compatibility but are now no-ops.
2293** </dd>
2294**
2295** [[SQLITE_CONFIG_SQLLOG]]
2296** <dt>SQLITE_CONFIG_SQLLOG
2297** <dd>This option is only available if sqlite is compiled with the
2298** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should
2299** be a pointer to a function of type void(*)(void*,sqlite3*,const char*, int).
2300** The second should be of type (void*). The callback is invoked by the library
2301** in three separate circumstances, identified by the value passed as the
2302** fourth parameter. If the fourth parameter is 0, then the database connection
2303** passed as the second argument has just been opened. The third argument
2304** points to a buffer containing the name of the main database file. If the
2305** fourth parameter is 1, then the SQL statement that the third parameter
2306** points to has just been executed. Or, if the fourth parameter is 2, then
2307** the connection being passed as the second parameter is being closed. The
2308** third parameter is passed NULL In this case. An example of using this
2309** configuration option can be seen in the "test_sqllog.c" source file in
2310** the canonical SQLite source tree.</dd>
2311**
2312** [[SQLITE_CONFIG_MMAP_SIZE]]
2313** <dt>SQLITE_CONFIG_MMAP_SIZE
2314** <dd>^SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values
2315** that are the default mmap size limit (the default setting for
2316** [PRAGMA mmap_size]) and the maximum allowed mmap size limit.
2317** ^The default setting can be overridden by each database connection using
2318** either the [PRAGMA mmap_size] command, or by using the
2319** [SQLITE_FCNTL_MMAP_SIZE] file control. ^(The maximum allowed mmap size
2320** will be silently truncated if necessary so that it does not exceed the
2321** compile-time maximum mmap size set by the
2322** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
2323** ^If either argument to this option is negative, then that argument is
2324** changed to its compile-time default.
2325**
2326** [[SQLITE_CONFIG_WIN32_HEAPSIZE]]
2327** <dt>SQLITE_CONFIG_WIN32_HEAPSIZE
2328** <dd>^The SQLITE_CONFIG_WIN32_HEAPSIZE option is only available if SQLite is
2329** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro
2330** defined. ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
2331** that specifies the maximum size of the created heap.
2332**
2333** [[SQLITE_CONFIG_PCACHE_HDRSZ]]
2334** <dt>SQLITE_CONFIG_PCACHE_HDRSZ
2335** <dd>^The SQLITE_CONFIG_PCACHE_HDRSZ option takes a single parameter which
2336** is a pointer to an integer and writes into that integer the number of extra
2337** bytes per page required for each page in [SQLITE_CONFIG_PAGECACHE].
2338** The amount of extra space required can change depending on the compiler,
2339** target platform, and SQLite version.
2340**
2341** [[SQLITE_CONFIG_PMASZ]]
2342** <dt>SQLITE_CONFIG_PMASZ
2343** <dd>^The SQLITE_CONFIG_PMASZ option takes a single parameter which
2344** is an unsigned integer and sets the "Minimum PMA Size" for the multithreaded
2345** sorter to that integer. The default minimum PMA Size is set by the
2346** [SQLITE_SORTER_PMASZ] compile-time option. New threads are launched
2347** to help with sort operations when multithreaded sorting
2348** is enabled (using the [PRAGMA threads] command) and the amount of content
2349** to be sorted exceeds the page size times the minimum of the
2350** [PRAGMA cache_size] setting and this value.
2351**
2352** [[SQLITE_CONFIG_STMTJRNL_SPILL]]
2353** <dt>SQLITE_CONFIG_STMTJRNL_SPILL
2354** <dd>^The SQLITE_CONFIG_STMTJRNL_SPILL option takes a single parameter which
2355** becomes the [statement journal] spill-to-disk threshold.
2356** [Statement journals] are held in memory until their size (in bytes)
2357** exceeds this threshold, at which point they are written to disk.
2358** Or if the threshold is -1, statement journals are always held
2359** exclusively in memory.
2360** Since many statement journals never become large, setting the spill
2361** threshold to a value such as 64KiB can greatly reduce the amount of
2362** I/O required to support statement rollback.
2363** The default value for this setting is controlled by the
2364** [SQLITE_STMTJRNL_SPILL] compile-time option.
2365**
2366** [[SQLITE_CONFIG_SORTERREF_SIZE]]
2367** <dt>SQLITE_CONFIG_SORTERREF_SIZE
2368** <dd>The SQLITE_CONFIG_SORTERREF_SIZE option accepts a single parameter
2369** of type (int) - the new value of the sorter-reference size threshold.
2370** Usually, when SQLite uses an external sort to order records according
2371** to an ORDER BY clause, all fields required by the caller are present in the
2372** sorted records. However, if SQLite determines based on the declared type
2373** of a table column that its values are likely to be very large - larger
2374** than the configured sorter-reference size threshold - then a reference
2375** is stored in each sorted record and the required column values loaded
2376** from the database as records are returned in sorted order. The default
2377** value for this option is to never use this optimization. Specifying a
2378** negative value for this option restores the default behaviour.
2379** This option is only available if SQLite is compiled with the
2380** [SQLITE_ENABLE_SORTER_REFERENCES] compile-time option.
2381**
2382** [[SQLITE_CONFIG_MEMDB_MAXSIZE]]
2383** <dt>SQLITE_CONFIG_MEMDB_MAXSIZE
2384** <dd>The SQLITE_CONFIG_MEMDB_MAXSIZE option accepts a single parameter
2385** [sqlite3_int64] parameter which is the default maximum size for an in-memory
2386** database created using [sqlite3_deserialize()]. This default maximum
2387** size can be adjusted up or down for individual databases using the
2388** [SQLITE_FCNTL_SIZE_LIMIT] [sqlite3_file_control|file-control]. If this
2389** configuration setting is never used, then the default maximum is determined
2390** by the [SQLITE_MEMDB_DEFAULT_MAXSIZE] compile-time option. If that
2391** compile-time option is not set, then the default maximum is 1073741824.
2392** </dl>
2393*/
2394#define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
2395#define SQLITE_CONFIG_MULTITHREAD 2 /* nil */
2396#define SQLITE_CONFIG_SERIALIZED 3 /* nil */
2397#define SQLITE_CONFIG_MALLOC 4 /* sqlite3_mem_methods* */
2398#define SQLITE_CONFIG_GETMALLOC 5 /* sqlite3_mem_methods* */
2399#define SQLITE_CONFIG_SCRATCH 6 /* No longer used */
2400#define SQLITE_CONFIG_PAGECACHE 7 /* void*, int sz, int N */
2401#define SQLITE_CONFIG_HEAP 8 /* void*, int nByte, int min */
2402#define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */
2403#define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */
2404#define SQLITE_CONFIG_GETMUTEX 11 /* sqlite3_mutex_methods* */
2405/* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
2406#define SQLITE_CONFIG_LOOKASIDE 13 /* int int */
2407#define SQLITE_CONFIG_PCACHE 14 /* no-op */
2408#define SQLITE_CONFIG_GETPCACHE 15 /* no-op */
2409#define SQLITE_CONFIG_LOG 16 /* xFunc, void* */
2410#define SQLITE_CONFIG_URI 17 /* int */
2411#define SQLITE_CONFIG_PCACHE2 18 /* sqlite3_pcache_methods2* */
2412#define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */
2413#define SQLITE_CONFIG_COVERING_INDEX_SCAN 20 /* int */
2414#define SQLITE_CONFIG_SQLLOG 21 /* xSqllog, void* */
2415#define SQLITE_CONFIG_MMAP_SIZE 22 /* sqlite3_int64, sqlite3_int64 */
2416#define SQLITE_CONFIG_WIN32_HEAPSIZE 23 /* int nByte */
2417#define SQLITE_CONFIG_PCACHE_HDRSZ 24 /* int *psz */
2418#define SQLITE_CONFIG_PMASZ 25 /* unsigned int szPma */
2419#define SQLITE_CONFIG_STMTJRNL_SPILL 26 /* int nByte */
2420#define SQLITE_CONFIG_SMALL_MALLOC 27 /* boolean */
2421#define SQLITE_CONFIG_SORTERREF_SIZE 28 /* int nByte */
2422#define SQLITE_CONFIG_MEMDB_MAXSIZE 29 /* sqlite3_int64 */
2423
2424/*
2425** CAPI3REF: Database Connection Configuration Options
2426**
2427** These constants are the available integer configuration options that
2428** can be passed as the second argument to the [sqlite3_db_config()] interface.
2429**
2430** New configuration options may be added in future releases of SQLite.
2431** Existing configuration options might be discontinued. Applications
2432** should check the return code from [sqlite3_db_config()] to make sure that
2433** the call worked. ^The [sqlite3_db_config()] interface will return a
2434** non-zero [error code] if a discontinued or unsupported configuration option
2435** is invoked.
2436**
2437** <dl>
2438** [[SQLITE_DBCONFIG_LOOKASIDE]]
2439** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
2440** <dd> ^This option takes three additional arguments that determine the
2441** [lookaside memory allocator] configuration for the [database connection].
2442** ^The first argument (the third parameter to [sqlite3_db_config()] is a
2443** pointer to a memory buffer to use for lookaside memory.
2444** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb
2445** may be NULL in which case SQLite will allocate the
2446** lookaside buffer itself using [sqlite3_malloc()]. ^The second argument is the
2447** size of each lookaside buffer slot. ^The third argument is the number of
2448** slots. The size of the buffer in the first argument must be greater than
2449** or equal to the product of the second and third arguments. The buffer
2450** must be aligned to an 8-byte boundary. ^If the second argument to
2451** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally
2452** rounded down to the next smaller multiple of 8. ^(The lookaside memory
2453** configuration for a database connection can only be changed when that
2454** connection is not currently using lookaside memory, or in other words
2455** when the "current value" returned by
2456** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero.
2457** Any attempt to change the lookaside memory configuration when lookaside
2458** memory is in use leaves the configuration unchanged and returns
2459** [SQLITE_BUSY].)^</dd>
2460**
2461** [[SQLITE_DBCONFIG_ENABLE_FKEY]]
2462** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
2463** <dd> ^This option is used to enable or disable the enforcement of
2464** [foreign key constraints]. There should be two additional arguments.
2465** The first argument is an integer which is 0 to disable FK enforcement,
2466** positive to enable FK enforcement or negative to leave FK enforcement
2467** unchanged. The second parameter is a pointer to an integer into which
2468** is written 0 or 1 to indicate whether FK enforcement is off or on
2469** following this call. The second parameter may be a NULL pointer, in
2470** which case the FK enforcement setting is not reported back. </dd>
2471**
2472** [[SQLITE_DBCONFIG_ENABLE_TRIGGER]]
2473** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
2474** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
2475** There should be two additional arguments.
2476** The first argument is an integer which is 0 to disable triggers,
2477** positive to enable triggers or negative to leave the setting unchanged.
2478** The second parameter is a pointer to an integer into which
2479** is written 0 or 1 to indicate whether triggers are disabled or enabled
2480** following this call. The second parameter may be a NULL pointer, in
2481** which case the trigger setting is not reported back.
2482**
2483** <p>Originally this option disabled all triggers. ^(However, since
2484** SQLite version 3.35.0, TEMP triggers are still allowed even if
2485** this option is off. So, in other words, this option now only disables
2486** triggers in the main database schema or in the schemas of ATTACH-ed
2487** databases.)^ </dd>
2488**
2489** [[SQLITE_DBCONFIG_ENABLE_VIEW]]
2490** <dt>SQLITE_DBCONFIG_ENABLE_VIEW</dt>
2491** <dd> ^This option is used to enable or disable [CREATE VIEW | views].
2492** There should be two additional arguments.
2493** The first argument is an integer which is 0 to disable views,
2494** positive to enable views or negative to leave the setting unchanged.
2495** The second parameter is a pointer to an integer into which
2496** is written 0 or 1 to indicate whether views are disabled or enabled
2497** following this call. The second parameter may be a NULL pointer, in
2498** which case the view setting is not reported back.
2499**
2500** <p>Originally this option disabled all views. ^(However, since
2501** SQLite version 3.35.0, TEMP views are still allowed even if
2502** this option is off. So, in other words, this option now only disables
2503** views in the main database schema or in the schemas of ATTACH-ed
2504** databases.)^ </dd>
2505**
2506** [[SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER]]
2507** <dt>SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER</dt>
2508** <dd> ^This option is used to enable or disable the
2509** [fts3_tokenizer()] function which is part of the
2510** [FTS3] full-text search engine extension.
2511** There should be two additional arguments.
2512** The first argument is an integer which is 0 to disable fts3_tokenizer() or
2513** positive to enable fts3_tokenizer() or negative to leave the setting
2514** unchanged.
2515** The second parameter is a pointer to an integer into which
2516** is written 0 or 1 to indicate whether fts3_tokenizer is disabled or enabled
2517** following this call. The second parameter may be a NULL pointer, in
2518** which case the new setting is not reported back. </dd>
2519**
2520** [[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION]]
2521** <dt>SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION</dt>
2522** <dd> ^This option is used to enable or disable the [sqlite3_load_extension()]
2523** interface independently of the [load_extension()] SQL function.
2524** The [sqlite3_enable_load_extension()] API enables or disables both the
2525** C-API [sqlite3_load_extension()] and the SQL function [load_extension()].
2526** There should be two additional arguments.
2527** When the first argument to this interface is 1, then only the C-API is
2528** enabled and the SQL function remains disabled. If the first argument to
2529** this interface is 0, then both the C-API and the SQL function are disabled.
2530** If the first argument is -1, then no changes are made to state of either the
2531** C-API or the SQL function.
2532** The second parameter is a pointer to an integer into which
2533** is written 0 or 1 to indicate whether [sqlite3_load_extension()] interface
2534** is disabled or enabled following this call. The second parameter may
2535** be a NULL pointer, in which case the new setting is not reported back.
2536** </dd>
2537**
2538** [[SQLITE_DBCONFIG_MAINDBNAME]] <dt>SQLITE_DBCONFIG_MAINDBNAME</dt>
2539** <dd> ^This option is used to change the name of the "main" database
2540** schema. ^The sole argument is a pointer to a constant UTF8 string
2541** which will become the new schema name in place of "main". ^SQLite
2542** does not make a copy of the new main schema name string, so the application
2543** must ensure that the argument passed into this DBCONFIG option is unchanged
2544** until after the database connection closes.
2545** </dd>
2546**
2547** [[SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE]]
2548** <dt>SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE</dt>
2549** <dd> Usually, when a database in wal mode is closed or detached from a
2550** database handle, SQLite checks if this will mean that there are now no
2551** connections at all to the database. If so, it performs a checkpoint
2552** operation before closing the connection. This option may be used to
2553** override this behaviour. The first parameter passed to this operation
2554** is an integer - positive to disable checkpoints-on-close, or zero (the
2555** default) to enable them, and negative to leave the setting unchanged.
2556** The second parameter is a pointer to an integer
2557** into which is written 0 or 1 to indicate whether checkpoints-on-close
2558** have been disabled - 0 if they are not disabled, 1 if they are.
2559** </dd>
2560**
2561** [[SQLITE_DBCONFIG_ENABLE_QPSG]] <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt>
2562** <dd>^(The SQLITE_DBCONFIG_ENABLE_QPSG option activates or deactivates
2563** the [query planner stability guarantee] (QPSG). When the QPSG is active,
2564** a single SQL query statement will always use the same algorithm regardless
2565** of values of [bound parameters].)^ The QPSG disables some query optimizations
2566** that look at the values of bound parameters, which can make some queries
2567** slower. But the QPSG has the advantage of more predictable behavior. With
2568** the QPSG active, SQLite will always use the same query plan in the field as
2569** was used during testing in the lab.
2570** The first argument to this setting is an integer which is 0 to disable
2571** the QPSG, positive to enable QPSG, or negative to leave the setting
2572** unchanged. The second parameter is a pointer to an integer into which
2573** is written 0 or 1 to indicate whether the QPSG is disabled or enabled
2574** following this call.
2575** </dd>
2576**
2577** [[SQLITE_DBCONFIG_TRIGGER_EQP]] <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
2578** <dd> By default, the output of EXPLAIN QUERY PLAN commands does not
2579** include output for any operations performed by trigger programs. This
2580** option is used to set or clear (the default) a flag that governs this
2581** behavior. The first parameter passed to this operation is an integer -
2582** positive to enable output for trigger programs, or zero to disable it,
2583** or negative to leave the setting unchanged.
2584** The second parameter is a pointer to an integer into which is written
2585** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if
2586** it is not disabled, 1 if it is.
2587** </dd>
2588**
2589** [[SQLITE_DBCONFIG_RESET_DATABASE]] <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt>
2590** <dd> Set the SQLITE_DBCONFIG_RESET_DATABASE flag and then run
2591** [VACUUM] in order to reset a database back to an empty database
2592** with no schema and no content. The following process works even for
2593** a badly corrupted database file:
2594** <ol>
2595** <li> If the database connection is newly opened, make sure it has read the
2596** database schema by preparing then discarding some query against the
2597** database, or calling sqlite3_table_column_metadata(), ignoring any
2598** errors. This step is only necessary if the application desires to keep
2599** the database in WAL mode after the reset if it was in WAL mode before
2600** the reset.
2601** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
2602** <li> [sqlite3_exec](db, "[VACUUM]", 0, 0, 0);
2603** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
2604** </ol>
2605** Because resetting a database is destructive and irreversible, the
2606** process requires the use of this obscure API and multiple steps to help
2607** ensure that it does not happen by accident.
2608**
2609** [[SQLITE_DBCONFIG_DEFENSIVE]] <dt>SQLITE_DBCONFIG_DEFENSIVE</dt>
2610** <dd>The SQLITE_DBCONFIG_DEFENSIVE option activates or deactivates the
2611** "defensive" flag for a database connection. When the defensive
2612** flag is enabled, language features that allow ordinary SQL to
2613** deliberately corrupt the database file are disabled. The disabled
2614** features include but are not limited to the following:
2615** <ul>
2616** <li> The [PRAGMA writable_schema=ON] statement.
2617** <li> The [PRAGMA journal_mode=OFF] statement.
2618** <li> Writes to the [sqlite_dbpage] virtual table.
2619** <li> Direct writes to [shadow tables].
2620** </ul>
2621** </dd>
2622**
2623** [[SQLITE_DBCONFIG_WRITABLE_SCHEMA]] <dt>SQLITE_DBCONFIG_WRITABLE_SCHEMA</dt>
2624** <dd>The SQLITE_DBCONFIG_WRITABLE_SCHEMA option activates or deactivates the
2625** "writable_schema" flag. This has the same effect and is logically equivalent
2626** to setting [PRAGMA writable_schema=ON] or [PRAGMA writable_schema=OFF].
2627** The first argument to this setting is an integer which is 0 to disable
2628** the writable_schema, positive to enable writable_schema, or negative to
2629** leave the setting unchanged. The second parameter is a pointer to an
2630** integer into which is written 0 or 1 to indicate whether the writable_schema
2631** is enabled or disabled following this call.
2632** </dd>
2633**
2634** [[SQLITE_DBCONFIG_LEGACY_ALTER_TABLE]]
2635** <dt>SQLITE_DBCONFIG_LEGACY_ALTER_TABLE</dt>
2636** <dd>The SQLITE_DBCONFIG_LEGACY_ALTER_TABLE option activates or deactivates
2637** the legacy behavior of the [ALTER TABLE RENAME] command such it
2638** behaves as it did prior to [version 3.24.0] (2018-06-04). See the
2639** "Compatibility Notice" on the [ALTER TABLE RENAME documentation] for
2640** additional information. This feature can also be turned on and off
2641** using the [PRAGMA legacy_alter_table] statement.
2642** </dd>
2643**
2644** [[SQLITE_DBCONFIG_DQS_DML]]
2645** <dt>SQLITE_DBCONFIG_DQS_DML</td>
2646** <dd>The SQLITE_DBCONFIG_DQS_DML option activates or deactivates
2647** the legacy [double-quoted string literal] misfeature for DML statements
2648** only, that is DELETE, INSERT, SELECT, and UPDATE statements. The
2649** default value of this setting is determined by the [-DSQLITE_DQS]
2650** compile-time option.
2651** </dd>
2652**
2653** [[SQLITE_DBCONFIG_DQS_DDL]]
2654** <dt>SQLITE_DBCONFIG_DQS_DDL</td>
2655** <dd>The SQLITE_DBCONFIG_DQS option activates or deactivates
2656** the legacy [double-quoted string literal] misfeature for DDL statements,
2657** such as CREATE TABLE and CREATE INDEX. The
2658** default value of this setting is determined by the [-DSQLITE_DQS]
2659** compile-time option.
2660** </dd>
2661**
2662** [[SQLITE_DBCONFIG_TRUSTED_SCHEMA]]
2663** <dt>SQLITE_DBCONFIG_TRUSTED_SCHEMA</td>
2664** <dd>The SQLITE_DBCONFIG_TRUSTED_SCHEMA option tells SQLite to
2665** assume that database schemas are untainted by malicious content.
2666** When the SQLITE_DBCONFIG_TRUSTED_SCHEMA option is disabled, SQLite
2667** takes additional defensive steps to protect the application from harm
2668** including:
2669** <ul>
2670** <li> Prohibit the use of SQL functions inside triggers, views,
2671** CHECK constraints, DEFAULT clauses, expression indexes,
2672** partial indexes, or generated columns
2673** unless those functions are tagged with [SQLITE_INNOCUOUS].
2674** <li> Prohibit the use of virtual tables inside of triggers or views
2675** unless those virtual tables are tagged with [SQLITE_VTAB_INNOCUOUS].
2676** </ul>
2677** This setting defaults to "on" for legacy compatibility, however
2678** all applications are advised to turn it off if possible. This setting
2679** can also be controlled using the [PRAGMA trusted_schema] statement.
2680** </dd>
2681**
2682** [[SQLITE_DBCONFIG_LEGACY_FILE_FORMAT]]
2683** <dt>SQLITE_DBCONFIG_LEGACY_FILE_FORMAT</td>
2684** <dd>The SQLITE_DBCONFIG_LEGACY_FILE_FORMAT option activates or deactivates
2685** the legacy file format flag. When activated, this flag causes all newly
2686** created database file to have a schema format version number (the 4-byte
2687** integer found at offset 44 into the database header) of 1. This in turn
2688** means that the resulting database file will be readable and writable by
2689** any SQLite version back to 3.0.0 ([dateof:3.0.0]). Without this setting,
2690** newly created databases are generally not understandable by SQLite versions
2691** prior to 3.3.0 ([dateof:3.3.0]). As these words are written, there
2692** is now scarcely any need to generated database files that are compatible
2693** all the way back to version 3.0.0, and so this setting is of little
2694** practical use, but is provided so that SQLite can continue to claim the
2695** ability to generate new database files that are compatible with version
2696** 3.0.0.
2697** <p>Note that when the SQLITE_DBCONFIG_LEGACY_FILE_FORMAT setting is on,
2698** the [VACUUM] command will fail with an obscure error when attempting to
2699** process a table with generated columns and a descending index. This is
2700** not considered a bug since SQLite versions 3.3.0 and earlier do not support
2701** either generated columns or decending indexes.
2702** </dd>
2703** </dl>
2704*/
2705#define SQLITE_DBCONFIG_MAINDBNAME 1000 /* const char* */
2706#define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */
2707#define SQLITE_DBCONFIG_ENABLE_FKEY 1002 /* int int* */
2708#define SQLITE_DBCONFIG_ENABLE_TRIGGER 1003 /* int int* */
2709#define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */
2710#define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
2711#define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE 1006 /* int int* */
2712#define SQLITE_DBCONFIG_ENABLE_QPSG 1007 /* int int* */
2713#define SQLITE_DBCONFIG_TRIGGER_EQP 1008 /* int int* */
2714#define SQLITE_DBCONFIG_RESET_DATABASE 1009 /* int int* */
2715#define SQLITE_DBCONFIG_DEFENSIVE 1010 /* int int* */
2716#define SQLITE_DBCONFIG_WRITABLE_SCHEMA 1011 /* int int* */
2717#define SQLITE_DBCONFIG_LEGACY_ALTER_TABLE 1012 /* int int* */
2718#define SQLITE_DBCONFIG_DQS_DML 1013 /* int int* */
2719#define SQLITE_DBCONFIG_DQS_DDL 1014 /* int int* */
2720#define SQLITE_DBCONFIG_ENABLE_VIEW 1015 /* int int* */
2721#define SQLITE_DBCONFIG_LEGACY_FILE_FORMAT 1016 /* int int* */
2722#define SQLITE_DBCONFIG_TRUSTED_SCHEMA 1017 /* int int* */
2723#define SQLITE_DBCONFIG_MAX 1017 /* Largest DBCONFIG */
2724
2725/*
2726** CAPI3REF: Enable Or Disable Extended Result Codes
2727** METHOD: sqlite3
2728**
2729** ^The sqlite3_extended_result_codes() routine enables or disables the
2730** [extended result codes] feature of SQLite. ^The extended result
2731** codes are disabled by default for historical compatibility.
2732*/
2733SQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff);
2734
2735/*
2736** CAPI3REF: Last Insert Rowid
2737** METHOD: sqlite3
2738**
2739** ^Each entry in most SQLite tables (except for [WITHOUT ROWID] tables)
2740** has a unique 64-bit signed
2741** integer key called the [ROWID | "rowid"]. ^The rowid is always available
2742** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
2743** names are not also used by explicitly declared columns. ^If
2744** the table has a column of type [INTEGER PRIMARY KEY] then that column
2745** is another alias for the rowid.
2746**
2747** ^The sqlite3_last_insert_rowid(D) interface usually returns the [rowid] of
2748** the most recent successful [INSERT] into a rowid table or [virtual table]
2749** on database connection D. ^Inserts into [WITHOUT ROWID] tables are not
2750** recorded. ^If no successful [INSERT]s into rowid tables have ever occurred
2751** on the database connection D, then sqlite3_last_insert_rowid(D) returns
2752** zero.
2753**
2754** As well as being set automatically as rows are inserted into database
2755** tables, the value returned by this function may be set explicitly by
2756** [sqlite3_set_last_insert_rowid()]
2757**
2758** Some virtual table implementations may INSERT rows into rowid tables as
2759** part of committing a transaction (e.g. to flush data accumulated in memory
2760** to disk). In this case subsequent calls to this function return the rowid
2761** associated with these internal INSERT operations, which leads to
2762** unintuitive results. Virtual table implementations that do write to rowid
2763** tables in this way can avoid this problem by restoring the original
2764** rowid value using [sqlite3_set_last_insert_rowid()] before returning
2765** control to the user.
2766**
2767** ^(If an [INSERT] occurs within a trigger then this routine will
2768** return the [rowid] of the inserted row as long as the trigger is
2769** running. Once the trigger program ends, the value returned
2770** by this routine reverts to what it was before the trigger was fired.)^
2771**
2772** ^An [INSERT] that fails due to a constraint violation is not a
2773** successful [INSERT] and does not change the value returned by this
2774** routine. ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
2775** and INSERT OR ABORT make no changes to the return value of this
2776** routine when their insertion fails. ^(When INSERT OR REPLACE
2777** encounters a constraint violation, it does not fail. The
2778** INSERT continues to completion after deleting rows that caused
2779** the constraint problem so INSERT OR REPLACE will always change
2780** the return value of this interface.)^
2781**
2782** ^For the purposes of this routine, an [INSERT] is considered to
2783** be successful even if it is subsequently rolled back.
2784**
2785** This function is accessible to SQL statements via the
2786** [last_insert_rowid() SQL function].
2787**
2788** If a separate thread performs a new [INSERT] on the same
2789** database connection while the [sqlite3_last_insert_rowid()]
2790** function is running and thus changes the last insert [rowid],
2791** then the value returned by [sqlite3_last_insert_rowid()] is
2792** unpredictable and might not equal either the old or the new
2793** last insert [rowid].
2794*/
2795SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
2796
2797/*
2798** CAPI3REF: Set the Last Insert Rowid value.
2799** METHOD: sqlite3
2800**
2801** The sqlite3_set_last_insert_rowid(D, R) method allows the application to
2802** set the value returned by calling sqlite3_last_insert_rowid(D) to R
2803** without inserting a row into the database.
2804*/
2805SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3*,sqlite3_int64);
2806
2807/*
2808** CAPI3REF: Count The Number Of Rows Modified
2809** METHOD: sqlite3
2810**
2811** ^These functions return the number of rows modified, inserted or
2812** deleted by the most recently completed INSERT, UPDATE or DELETE
2813** statement on the database connection specified by the only parameter.
2814** The two functions are identical except for the type of the return value
2815** and that if the number of rows modified by the most recent INSERT, UPDATE
2816** or DELETE is greater than the maximum value supported by type "int", then
2817** the return value of sqlite3_changes() is undefined. ^Executing any other
2818** type of SQL statement does not modify the value returned by these functions.
2819**
2820** ^Only changes made directly by the INSERT, UPDATE or DELETE statement are
2821** considered - auxiliary changes caused by [CREATE TRIGGER | triggers],
2822** [foreign key actions] or [REPLACE] constraint resolution are not counted.
2823**
2824** Changes to a view that are intercepted by
2825** [INSTEAD OF trigger | INSTEAD OF triggers] are not counted. ^The value
2826** returned by sqlite3_changes() immediately after an INSERT, UPDATE or
2827** DELETE statement run on a view is always zero. Only changes made to real
2828** tables are counted.
2829**
2830** Things are more complicated if the sqlite3_changes() function is
2831** executed while a trigger program is running. This may happen if the
2832** program uses the [changes() SQL function], or if some other callback
2833** function invokes sqlite3_changes() directly. Essentially:
2834**
2835** <ul>
2836** <li> ^(Before entering a trigger program the value returned by
2837** sqlite3_changes() function is saved. After the trigger program
2838** has finished, the original value is restored.)^
2839**
2840** <li> ^(Within a trigger program each INSERT, UPDATE and DELETE
2841** statement sets the value returned by sqlite3_changes()
2842** upon completion as normal. Of course, this value will not include
2843** any changes performed by sub-triggers, as the sqlite3_changes()
2844** value will be saved and restored after each sub-trigger has run.)^
2845** </ul>
2846**
2847** ^This means that if the changes() SQL function (or similar) is used
2848** by the first INSERT, UPDATE or DELETE statement within a trigger, it
2849** returns the value as set when the calling statement began executing.
2850** ^If it is used by the second or subsequent such statement within a trigger
2851** program, the value returned reflects the number of rows modified by the
2852** previous INSERT, UPDATE or DELETE statement within the same trigger.
2853**
2854** If a separate thread makes changes on the same database connection
2855** while [sqlite3_changes()] is running then the value returned
2856** is unpredictable and not meaningful.
2857**
2858** See also:
2859** <ul>
2860** <li> the [sqlite3_total_changes()] interface
2861** <li> the [count_changes pragma]
2862** <li> the [changes() SQL function]
2863** <li> the [data_version pragma]
2864** </ul>
2865*/
2866SQLITE_API int sqlite3_changes(sqlite3*);
2867SQLITE_API sqlite3_int64 sqlite3_changes64(sqlite3*);
2868
2869/*
2870** CAPI3REF: Total Number Of Rows Modified
2871** METHOD: sqlite3
2872**
2873** ^These functions return the total number of rows inserted, modified or
2874** deleted by all [INSERT], [UPDATE] or [DELETE] statements completed
2875** since the database connection was opened, including those executed as
2876** part of trigger programs. The two functions are identical except for the
2877** type of the return value and that if the number of rows modified by the
2878** connection exceeds the maximum value supported by type "int", then
2879** the return value of sqlite3_total_changes() is undefined. ^Executing
2880** any other type of SQL statement does not affect the value returned by
2881** sqlite3_total_changes().
2882**
2883** ^Changes made as part of [foreign key actions] are included in the
2884** count, but those made as part of REPLACE constraint resolution are
2885** not. ^Changes to a view that are intercepted by INSTEAD OF triggers
2886** are not counted.
2887**
2888** The [sqlite3_total_changes(D)] interface only reports the number
2889** of rows that changed due to SQL statement run against database
2890** connection D. Any changes by other database connections are ignored.
2891** To detect changes against a database file from other database
2892** connections use the [PRAGMA data_version] command or the
2893** [SQLITE_FCNTL_DATA_VERSION] [file control].
2894**
2895** If a separate thread makes changes on the same database connection
2896** while [sqlite3_total_changes()] is running then the value
2897** returned is unpredictable and not meaningful.
2898**
2899** See also:
2900** <ul>
2901** <li> the [sqlite3_changes()] interface
2902** <li> the [count_changes pragma]
2903** <li> the [changes() SQL function]
2904** <li> the [data_version pragma]
2905** <li> the [SQLITE_FCNTL_DATA_VERSION] [file control]
2906** </ul>
2907*/
2908SQLITE_API int sqlite3_total_changes(sqlite3*);
2909SQLITE_API sqlite3_int64 sqlite3_total_changes64(sqlite3*);
2910
2911/*
2912** CAPI3REF: Interrupt A Long-Running Query
2913** METHOD: sqlite3
2914**
2915** ^This function causes any pending database operation to abort and
2916** return at its earliest opportunity. This routine is typically
2917** called in response to a user action such as pressing "Cancel"
2918** or Ctrl-C where the user wants a long query operation to halt
2919** immediately.
2920**
2921** ^It is safe to call this routine from a thread different from the
2922** thread that is currently running the database operation. But it
2923** is not safe to call this routine with a [database connection] that
2924** is closed or might close before sqlite3_interrupt() returns.
2925**
2926** ^If an SQL operation is very nearly finished at the time when
2927** sqlite3_interrupt() is called, then it might not have an opportunity
2928** to be interrupted and might continue to completion.
2929**
2930** ^An SQL operation that is interrupted will return [SQLITE_INTERRUPT].
2931** ^If the interrupted SQL operation is an INSERT, UPDATE, or DELETE
2932** that is inside an explicit transaction, then the entire transaction
2933** will be rolled back automatically.
2934**
2935** ^The sqlite3_interrupt(D) call is in effect until all currently running
2936** SQL statements on [database connection] D complete. ^Any new SQL statements
2937** that are started after the sqlite3_interrupt() call and before the
2938** running statement count reaches zero are interrupted as if they had been
2939** running prior to the sqlite3_interrupt() call. ^New SQL statements
2940** that are started after the running statement count reaches zero are
2941** not effected by the sqlite3_interrupt().
2942** ^A call to sqlite3_interrupt(D) that occurs when there are no running
2943** SQL statements is a no-op and has no effect on SQL statements
2944** that are started after the sqlite3_interrupt() call returns.
2945*/
2946SQLITE_API void sqlite3_interrupt(sqlite3*);
2947
2948/*
2949** CAPI3REF: Determine If An SQL Statement Is Complete
2950**
2951** These routines are useful during command-line input to determine if the
2952** currently entered text seems to form a complete SQL statement or
2953** if additional input is needed before sending the text into
2954** SQLite for parsing. ^These routines return 1 if the input string
2955** appears to be a complete SQL statement. ^A statement is judged to be
2956** complete if it ends with a semicolon token and is not a prefix of a
2957** well-formed CREATE TRIGGER statement. ^Semicolons that are embedded within
2958** string literals or quoted identifier names or comments are not
2959** independent tokens (they are part of the token in which they are
2960** embedded) and thus do not count as a statement terminator. ^Whitespace
2961** and comments that follow the final semicolon are ignored.
2962**
2963** ^These routines return 0 if the statement is incomplete. ^If a
2964** memory allocation fails, then SQLITE_NOMEM is returned.
2965**
2966** ^These routines do not parse the SQL statements thus
2967** will not detect syntactically incorrect SQL.
2968**
2969** ^(If SQLite has not been initialized using [sqlite3_initialize()] prior
2970** to invoking sqlite3_complete16() then sqlite3_initialize() is invoked
2971** automatically by sqlite3_complete16(). If that initialization fails,
2972** then the return value from sqlite3_complete16() will be non-zero
2973** regardless of whether or not the input SQL is complete.)^
2974**
2975** The input to [sqlite3_complete()] must be a zero-terminated
2976** UTF-8 string.
2977**
2978** The input to [sqlite3_complete16()] must be a zero-terminated
2979** UTF-16 string in native byte order.
2980*/
2981SQLITE_API int sqlite3_complete(const char *sql);
2982SQLITE_API int sqlite3_complete16(const void *sql);
2983
2984/*
2985** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
2986** KEYWORDS: {busy-handler callback} {busy handler}
2987** METHOD: sqlite3
2988**
2989** ^The sqlite3_busy_handler(D,X,P) routine sets a callback function X
2990** that might be invoked with argument P whenever
2991** an attempt is made to access a database table associated with
2992** [database connection] D when another thread
2993** or process has the table locked.
2994** The sqlite3_busy_handler() interface is used to implement
2995** [sqlite3_busy_timeout()] and [PRAGMA busy_timeout].
2996**
2997** ^If the busy callback is NULL, then [SQLITE_BUSY]
2998** is returned immediately upon encountering the lock. ^If the busy callback
2999** is not NULL, then the callback might be invoked with two arguments.
3000**
3001** ^The first argument to the busy handler is a copy of the void* pointer which
3002** is the third argument to sqlite3_busy_handler(). ^The second argument to
3003** the busy handler callback is the number of times that the busy handler has
3004** been invoked previously for the same locking event. ^If the
3005** busy callback returns 0, then no additional attempts are made to
3006** access the database and [SQLITE_BUSY] is returned
3007** to the application.
3008** ^If the callback returns non-zero, then another attempt
3009** is made to access the database and the cycle repeats.
3010**
3011** The presence of a busy handler does not guarantee that it will be invoked
3012** when there is lock contention. ^If SQLite determines that invoking the busy
3013** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
3014** to the application instead of invoking the
3015** busy handler.
3016** Consider a scenario where one process is holding a read lock that
3017** it is trying to promote to a reserved lock and
3018** a second process is holding a reserved lock that it is trying
3019** to promote to an exclusive lock. The first process cannot proceed
3020** because it is blocked by the second and the second process cannot
3021** proceed because it is blocked by the first. If both processes
3022** invoke the busy handlers, neither will make any progress. Therefore,
3023** SQLite returns [SQLITE_BUSY] for the first process, hoping that this
3024** will induce the first process to release its read lock and allow
3025** the second process to proceed.
3026**
3027** ^The default busy callback is NULL.
3028**
3029** ^(There can only be a single busy handler defined for each
3030** [database connection]. Setting a new busy handler clears any
3031** previously set handler.)^ ^Note that calling [sqlite3_busy_timeout()]
3032** or evaluating [PRAGMA busy_timeout=N] will change the
3033** busy handler and thus clear any previously set busy handler.
3034**
3035** The busy callback should not take any actions which modify the
3036** database connection that invoked the busy handler. In other words,
3037** the busy handler is not reentrant. Any such actions
3038** result in undefined behavior.
3039**
3040** A busy handler must not close the database connection
3041** or [prepared statement] that invoked the busy handler.
3042*/
3043SQLITE_API int sqlite3_busy_handler(sqlite3*,int(*)(void*,int),void*);
3044
3045/*
3046** CAPI3REF: Set A Busy Timeout
3047** METHOD: sqlite3
3048**
3049** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
3050** for a specified amount of time when a table is locked. ^The handler
3051** will sleep multiple times until at least "ms" milliseconds of sleeping
3052** have accumulated. ^After at least "ms" milliseconds of sleeping,
3053** the handler returns 0 which causes [sqlite3_step()] to return
3054** [SQLITE_BUSY].
3055**
3056** ^Calling this routine with an argument less than or equal to zero
3057** turns off all busy handlers.
3058**
3059** ^(There can only be a single busy handler for a particular
3060** [database connection] at any given moment. If another busy handler
3061** was defined (using [sqlite3_busy_handler()]) prior to calling
3062** this routine, that other busy handler is cleared.)^
3063**
3064** See also: [PRAGMA busy_timeout]
3065*/
3066SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
3067
3068/*
3069** CAPI3REF: Convenience Routines For Running Queries
3070** METHOD: sqlite3
3071**
3072** This is a legacy interface that is preserved for backwards compatibility.
3073** Use of this interface is not recommended.
3074**
3075** Definition: A <b>result table</b> is memory data structure created by the
3076** [sqlite3_get_table()] interface. A result table records the
3077** complete query results from one or more queries.
3078**
3079** The table conceptually has a number of rows and columns. But
3080** these numbers are not part of the result table itself. These
3081** numbers are obtained separately. Let N be the number of rows
3082** and M be the number of columns.
3083**
3084** A result table is an array of pointers to zero-terminated UTF-8 strings.
3085** There are (N+1)*M elements in the array. The first M pointers point
3086** to zero-terminated strings that contain the names of the columns.
3087** The remaining entries all point to query results. NULL values result
3088** in NULL pointers. All other values are in their UTF-8 zero-terminated
3089** string representation as returned by [sqlite3_column_text()].
3090**
3091** A result table might consist of one or more memory allocations.
3092** It is not safe to pass a result table directly to [sqlite3_free()].
3093** A result table should be deallocated using [sqlite3_free_table()].
3094**
3095** ^(As an example of the result table format, suppose a query result
3096** is as follows:
3097**
3098** <blockquote><pre>
3099** Name | Age
3100** -----------------------
3101** Alice | 43
3102** Bob | 28
3103** Cindy | 21
3104** </pre></blockquote>
3105**
3106** There are two columns (M==2) and three rows (N==3). Thus the
3107** result table has 8 entries. Suppose the result table is stored
3108** in an array named azResult. Then azResult holds this content:
3109**
3110** <blockquote><pre>
3111** azResult&#91;0] = "Name";
3112** azResult&#91;1] = "Age";
3113** azResult&#91;2] = "Alice";
3114** azResult&#91;3] = "43";
3115** azResult&#91;4] = "Bob";
3116** azResult&#91;5] = "28";
3117** azResult&#91;6] = "Cindy";
3118** azResult&#91;7] = "21";
3119** </pre></blockquote>)^
3120**
3121** ^The sqlite3_get_table() function evaluates one or more
3122** semicolon-separated SQL statements in the zero-terminated UTF-8
3123** string of its 2nd parameter and returns a result table to the
3124** pointer given in its 3rd parameter.
3125**
3126** After the application has finished with the result from sqlite3_get_table(),
3127** it must pass the result table pointer to sqlite3_free_table() in order to
3128** release the memory that was malloced. Because of the way the
3129** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling
3130** function must not try to call [sqlite3_free()] directly. Only
3131** [sqlite3_free_table()] is able to release the memory properly and safely.
3132**
3133** The sqlite3_get_table() interface is implemented as a wrapper around
3134** [sqlite3_exec()]. The sqlite3_get_table() routine does not have access
3135** to any internal data structures of SQLite. It uses only the public
3136** interface defined here. As a consequence, errors that occur in the
3137** wrapper layer outside of the internal [sqlite3_exec()] call are not
3138** reflected in subsequent calls to [sqlite3_errcode()] or
3139** [sqlite3_errmsg()].
3140*/
3141SQLITE_API int sqlite3_get_table(
3142 sqlite3 *db, /* An open database */
3143 const char *zSql, /* SQL to be evaluated */
3144 char ***pazResult, /* Results of the query */
3145 int *pnRow, /* Number of result rows written here */
3146 int *pnColumn, /* Number of result columns written here */
3147 char **pzErrmsg /* Error msg written here */
3148);
3149SQLITE_API void sqlite3_free_table(char **result);
3150
3151/*
3152** CAPI3REF: Formatted String Printing Functions
3153**
3154** These routines are work-alikes of the "printf()" family of functions
3155** from the standard C library.
3156** These routines understand most of the common formatting options from
3157** the standard library printf()
3158** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
3159** See the [built-in printf()] documentation for details.
3160**
3161** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
3162** results into memory obtained from [sqlite3_malloc64()].
3163** The strings returned by these two routines should be
3164** released by [sqlite3_free()]. ^Both routines return a
3165** NULL pointer if [sqlite3_malloc64()] is unable to allocate enough
3166** memory to hold the resulting string.
3167**
3168** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
3169** the standard C library. The result is written into the
3170** buffer supplied as the second parameter whose size is given by
3171** the first parameter. Note that the order of the
3172** first two parameters is reversed from snprintf().)^ This is an
3173** historical accident that cannot be fixed without breaking
3174** backwards compatibility. ^(Note also that sqlite3_snprintf()
3175** returns a pointer to its buffer instead of the number of
3176** characters actually written into the buffer.)^ We admit that
3177** the number of characters written would be a more useful return
3178** value but we cannot change the implementation of sqlite3_snprintf()
3179** now without breaking compatibility.
3180**
3181** ^As long as the buffer size is greater than zero, sqlite3_snprintf()
3182** guarantees that the buffer is always zero-terminated. ^The first
3183** parameter "n" is the total size of the buffer, including space for
3184** the zero terminator. So the longest string that can be completely
3185** written will be n-1 characters.
3186**
3187** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
3188**
3189** See also: [built-in printf()], [printf() SQL function]
3190*/
3191SQLITE_API char *sqlite3_mprintf(const char*,...);
3192SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
3193SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
3194SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
3195
3196/*
3197** CAPI3REF: Memory Allocation Subsystem
3198**
3199** The SQLite core uses these three routines for all of its own
3200** internal memory allocation needs. "Core" in the previous sentence
3201** does not include operating-system specific [VFS] implementation. The
3202** Windows VFS uses native malloc() and free() for some operations.
3203**
3204** ^The sqlite3_malloc() routine returns a pointer to a block
3205** of memory at least N bytes in length, where N is the parameter.
3206** ^If sqlite3_malloc() is unable to obtain sufficient free
3207** memory, it returns a NULL pointer. ^If the parameter N to
3208** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns
3209** a NULL pointer.
3210**
3211** ^The sqlite3_malloc64(N) routine works just like
3212** sqlite3_malloc(N) except that N is an unsigned 64-bit integer instead
3213** of a signed 32-bit integer.
3214**
3215** ^Calling sqlite3_free() with a pointer previously returned
3216** by sqlite3_malloc() or sqlite3_realloc() releases that memory so
3217** that it might be reused. ^The sqlite3_free() routine is
3218** a no-op if is called with a NULL pointer. Passing a NULL pointer
3219** to sqlite3_free() is harmless. After being freed, memory
3220** should neither be read nor written. Even reading previously freed
3221** memory might result in a segmentation fault or other severe error.
3222** Memory corruption, a segmentation fault, or other severe error
3223** might result if sqlite3_free() is called with a non-NULL pointer that
3224** was not obtained from sqlite3_malloc() or sqlite3_realloc().
3225**
3226** ^The sqlite3_realloc(X,N) interface attempts to resize a
3227** prior memory allocation X to be at least N bytes.
3228** ^If the X parameter to sqlite3_realloc(X,N)
3229** is a NULL pointer then its behavior is identical to calling
3230** sqlite3_malloc(N).
3231** ^If the N parameter to sqlite3_realloc(X,N) is zero or
3232** negative then the behavior is exactly the same as calling
3233** sqlite3_free(X).
3234** ^sqlite3_realloc(X,N) returns a pointer to a memory allocation
3235** of at least N bytes in size or NULL if insufficient memory is available.
3236** ^If M is the size of the prior allocation, then min(N,M) bytes
3237** of the prior allocation are copied into the beginning of buffer returned
3238** by sqlite3_realloc(X,N) and the prior allocation is freed.
3239** ^If sqlite3_realloc(X,N) returns NULL and N is positive, then the
3240** prior allocation is not freed.
3241**
3242** ^The sqlite3_realloc64(X,N) interfaces works the same as
3243** sqlite3_realloc(X,N) except that N is a 64-bit unsigned integer instead
3244** of a 32-bit signed integer.
3245**
3246** ^If X is a memory allocation previously obtained from sqlite3_malloc(),
3247** sqlite3_malloc64(), sqlite3_realloc(), or sqlite3_realloc64(), then
3248** sqlite3_msize(X) returns the size of that memory allocation in bytes.
3249** ^The value returned by sqlite3_msize(X) might be larger than the number
3250** of bytes requested when X was allocated. ^If X is a NULL pointer then
3251** sqlite3_msize(X) returns zero. If X points to something that is not
3252** the beginning of memory allocation, or if it points to a formerly
3253** valid memory allocation that has now been freed, then the behavior
3254** of sqlite3_msize(X) is undefined and possibly harmful.
3255**
3256** ^The memory returned by sqlite3_malloc(), sqlite3_realloc(),
3257** sqlite3_malloc64(), and sqlite3_realloc64()
3258** is always aligned to at least an 8 byte boundary, or to a
3259** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
3260** option is used.
3261**
3262** The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()]
3263** must be either NULL or else pointers obtained from a prior
3264** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that have
3265** not yet been released.
3266**
3267** The application must not read or write any part of
3268** a block of memory after it has been released using
3269** [sqlite3_free()] or [sqlite3_realloc()].
3270*/
3271SQLITE_API void *sqlite3_malloc(int);
3272SQLITE_API void *sqlite3_malloc64(sqlite3_uint64);
3273SQLITE_API void *sqlite3_realloc(void*, int);
3274SQLITE_API void *sqlite3_realloc64(void*, sqlite3_uint64);
3275SQLITE_API void sqlite3_free(void*);
3276SQLITE_API sqlite3_uint64 sqlite3_msize(void*);
3277
3278/*
3279** CAPI3REF: Memory Allocator Statistics
3280**
3281** SQLite provides these two interfaces for reporting on the status
3282** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()]
3283** routines, which form the built-in memory allocation subsystem.
3284**
3285** ^The [sqlite3_memory_used()] routine returns the number of bytes
3286** of memory currently outstanding (malloced but not freed).
3287** ^The [sqlite3_memory_highwater()] routine returns the maximum
3288** value of [sqlite3_memory_used()] since the high-water mark
3289** was last reset. ^The values returned by [sqlite3_memory_used()] and
3290** [sqlite3_memory_highwater()] include any overhead
3291** added by SQLite in its implementation of [sqlite3_malloc()],
3292** but not overhead added by the any underlying system library
3293** routines that [sqlite3_malloc()] may call.
3294**
3295** ^The memory high-water mark is reset to the current value of
3296** [sqlite3_memory_used()] if and only if the parameter to
3297** [sqlite3_memory_highwater()] is true. ^The value returned
3298** by [sqlite3_memory_highwater(1)] is the high-water mark
3299** prior to the reset.
3300*/
3301SQLITE_API sqlite3_int64 sqlite3_memory_used(void);
3302SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
3303
3304/*
3305** CAPI3REF: Pseudo-Random Number Generator
3306**
3307** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
3308** select random [ROWID | ROWIDs] when inserting new records into a table that
3309** already uses the largest possible [ROWID]. The PRNG is also used for
3310** the built-in random() and randomblob() SQL functions. This interface allows
3311** applications to access the same PRNG for other purposes.
3312**
3313** ^A call to this routine stores N bytes of randomness into buffer P.
3314** ^The P parameter can be a NULL pointer.
3315**
3316** ^If this routine has not been previously called or if the previous
3317** call had N less than one or a NULL pointer for P, then the PRNG is
3318** seeded using randomness obtained from the xRandomness method of
3319** the default [sqlite3_vfs] object.
3320** ^If the previous call to this routine had an N of 1 or more and a
3321** non-NULL P then the pseudo-randomness is generated
3322** internally and without recourse to the [sqlite3_vfs] xRandomness
3323** method.
3324*/
3325SQLITE_API void sqlite3_randomness(int N, void *P);
3326
3327/*
3328** CAPI3REF: Compile-Time Authorization Callbacks
3329** METHOD: sqlite3
3330** KEYWORDS: {authorizer callback}
3331**
3332** ^This routine registers an authorizer callback with a particular
3333** [database connection], supplied in the first argument.
3334** ^The authorizer callback is invoked as SQL statements are being compiled
3335** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
3336** [sqlite3_prepare_v3()], [sqlite3_prepare16()], [sqlite3_prepare16_v2()],
3337** and [sqlite3_prepare16_v3()]. ^At various
3338** points during the compilation process, as logic is being created
3339** to perform various actions, the authorizer callback is invoked to
3340** see if those actions are allowed. ^The authorizer callback should
3341** return [SQLITE_OK] to allow the action, [SQLITE_IGNORE] to disallow the
3342** specific action but allow the SQL statement to continue to be
3343** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be
3344** rejected with an error. ^If the authorizer callback returns
3345** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY]
3346** then the [sqlite3_prepare_v2()] or equivalent call that triggered
3347** the authorizer will fail with an error message.
3348**
3349** When the callback returns [SQLITE_OK], that means the operation
3350** requested is ok. ^When the callback returns [SQLITE_DENY], the
3351** [sqlite3_prepare_v2()] or equivalent call that triggered the
3352** authorizer will fail with an error message explaining that
3353** access is denied.
3354**
3355** ^The first parameter to the authorizer callback is a copy of the third
3356** parameter to the sqlite3_set_authorizer() interface. ^The second parameter
3357** to the callback is an integer [SQLITE_COPY | action code] that specifies
3358** the particular action to be authorized. ^The third through sixth parameters
3359** to the callback are either NULL pointers or zero-terminated strings
3360** that contain additional details about the action to be authorized.
3361** Applications must always be prepared to encounter a NULL pointer in any
3362** of the third through the sixth parameters of the authorization callback.
3363**
3364** ^If the action code is [SQLITE_READ]
3365** and the callback returns [SQLITE_IGNORE] then the
3366** [prepared statement] statement is constructed to substitute
3367** a NULL value in place of the table column that would have
3368** been read if [SQLITE_OK] had been returned. The [SQLITE_IGNORE]
3369** return can be used to deny an untrusted user access to individual
3370** columns of a table.
3371** ^When a table is referenced by a [SELECT] but no column values are
3372** extracted from that table (for example in a query like
3373** "SELECT count(*) FROM tab") then the [SQLITE_READ] authorizer callback
3374** is invoked once for that table with a column name that is an empty string.
3375** ^If the action code is [SQLITE_DELETE] and the callback returns
3376** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the
3377** [truncate optimization] is disabled and all rows are deleted individually.
3378**
3379** An authorizer is used when [sqlite3_prepare | preparing]
3380** SQL statements from an untrusted source, to ensure that the SQL statements
3381** do not try to access data they are not allowed to see, or that they do not
3382** try to execute malicious statements that damage the database. For
3383** example, an application may allow a user to enter arbitrary
3384** SQL queries for evaluation by a database. But the application does
3385** not want the user to be able to make arbitrary changes to the
3386** database. An authorizer could then be put in place while the
3387** user-entered SQL is being [sqlite3_prepare | prepared] that
3388** disallows everything except [SELECT] statements.
3389**
3390** Applications that need to process SQL from untrusted sources
3391** might also consider lowering resource limits using [sqlite3_limit()]
3392** and limiting database size using the [max_page_count] [PRAGMA]
3393** in addition to using an authorizer.
3394**
3395** ^(Only a single authorizer can be in place on a database connection
3396** at a time. Each call to sqlite3_set_authorizer overrides the
3397** previous call.)^ ^Disable the authorizer by installing a NULL callback.
3398** The authorizer is disabled by default.
3399**
3400** The authorizer callback must not do anything that will modify
3401** the database connection that invoked the authorizer callback.
3402** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
3403** database connections for the meaning of "modify" in this paragraph.
3404**
3405** ^When [sqlite3_prepare_v2()] is used to prepare a statement, the
3406** statement might be re-prepared during [sqlite3_step()] due to a
3407** schema change. Hence, the application should ensure that the
3408** correct authorizer callback remains in place during the [sqlite3_step()].
3409**
3410** ^Note that the authorizer callback is invoked only during
3411** [sqlite3_prepare()] or its variants. Authorization is not
3412** performed during statement evaluation in [sqlite3_step()], unless
3413** as stated in the previous paragraph, sqlite3_step() invokes
3414** sqlite3_prepare_v2() to reprepare a statement after a schema change.
3415*/
3416SQLITE_API int sqlite3_set_authorizer(
3417 sqlite3*,
3418 int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
3419 void *pUserData
3420);
3421
3422/*
3423** CAPI3REF: Authorizer Return Codes
3424**
3425** The [sqlite3_set_authorizer | authorizer callback function] must
3426** return either [SQLITE_OK] or one of these two constants in order
3427** to signal SQLite whether or not the action is permitted. See the
3428** [sqlite3_set_authorizer | authorizer documentation] for additional
3429** information.
3430**
3431** Note that SQLITE_IGNORE is also used as a [conflict resolution mode]
3432** returned from the [sqlite3_vtab_on_conflict()] interface.
3433*/
3434#define SQLITE_DENY 1 /* Abort the SQL statement with an error */
3435#define SQLITE_IGNORE 2 /* Don't allow access, but don't generate an error */
3436
3437/*
3438** CAPI3REF: Authorizer Action Codes
3439**
3440** The [sqlite3_set_authorizer()] interface registers a callback function
3441** that is invoked to authorize certain SQL statement actions. The
3442** second parameter to the callback is an integer code that specifies
3443** what action is being authorized. These are the integer action codes that
3444** the authorizer callback may be passed.
3445**
3446** These action code values signify what kind of operation is to be
3447** authorized. The 3rd and 4th parameters to the authorization
3448** callback function will be parameters or NULL depending on which of these
3449** codes is used as the second parameter. ^(The 5th parameter to the
3450** authorizer callback is the name of the database ("main", "temp",
3451** etc.) if applicable.)^ ^The 6th parameter to the authorizer callback
3452** is the name of the inner-most trigger or view that is responsible for
3453** the access attempt or NULL if this access attempt is directly from
3454** top-level SQL code.
3455*/
3456/******************************************* 3rd ************ 4th ***********/
3457#define SQLITE_CREATE_INDEX 1 /* Index Name Table Name */
3458#define SQLITE_CREATE_TABLE 2 /* Table Name NULL */
3459#define SQLITE_CREATE_TEMP_INDEX 3 /* Index Name Table Name */
3460#define SQLITE_CREATE_TEMP_TABLE 4 /* Table Name NULL */
3461#define SQLITE_CREATE_TEMP_TRIGGER 5 /* Trigger Name Table Name */
3462#define SQLITE_CREATE_TEMP_VIEW 6 /* View Name NULL */
3463#define SQLITE_CREATE_TRIGGER 7 /* Trigger Name Table Name */
3464#define SQLITE_CREATE_VIEW 8 /* View Name NULL */
3465#define SQLITE_DELETE 9 /* Table Name NULL */
3466#define SQLITE_DROP_INDEX 10 /* Index Name Table Name */
3467#define SQLITE_DROP_TABLE 11 /* Table Name NULL */
3468#define SQLITE_DROP_TEMP_INDEX 12 /* Index Name Table Name */
3469#define SQLITE_DROP_TEMP_TABLE 13 /* Table Name NULL */
3470#define SQLITE_DROP_TEMP_TRIGGER 14 /* Trigger Name Table Name */
3471#define SQLITE_DROP_TEMP_VIEW 15 /* View Name NULL */
3472#define SQLITE_DROP_TRIGGER 16 /* Trigger Name Table Name */
3473#define SQLITE_DROP_VIEW 17 /* View Name NULL */
3474#define SQLITE_INSERT 18 /* Table Name NULL */
3475#define SQLITE_PRAGMA 19 /* Pragma Name 1st arg or NULL */
3476#define SQLITE_READ 20 /* Table Name Column Name */
3477#define SQLITE_SELECT 21 /* NULL NULL */
3478#define SQLITE_TRANSACTION 22 /* Operation NULL */
3479#define SQLITE_UPDATE 23 /* Table Name Column Name */
3480#define SQLITE_ATTACH 24 /* Filename NULL */
3481#define SQLITE_DETACH 25 /* Database Name NULL */
3482#define SQLITE_ALTER_TABLE 26 /* Database Name Table Name */
3483#define SQLITE_REINDEX 27 /* Index Name NULL */
3484#define SQLITE_ANALYZE 28 /* Table Name NULL */
3485#define SQLITE_CREATE_VTABLE 29 /* Table Name Module Name */
3486#define SQLITE_DROP_VTABLE 30 /* Table Name Module Name */
3487#define SQLITE_FUNCTION 31 /* NULL Function Name */
3488#define SQLITE_SAVEPOINT 32 /* Operation Savepoint Name */
3489#define SQLITE_COPY 0 /* No longer used */
3490#define SQLITE_RECURSIVE 33 /* NULL NULL */
3491
3492/*
3493** CAPI3REF: Tracing And Profiling Functions
3494** METHOD: sqlite3
3495**
3496** These routines are deprecated. Use the [sqlite3_trace_v2()] interface
3497** instead of the routines described here.
3498**
3499** These routines register callback functions that can be used for
3500** tracing and profiling the execution of SQL statements.
3501**
3502** ^The callback function registered by sqlite3_trace() is invoked at
3503** various times when an SQL statement is being run by [sqlite3_step()].
3504** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
3505** SQL statement text as the statement first begins executing.
3506** ^(Additional sqlite3_trace() callbacks might occur
3507** as each triggered subprogram is entered. The callbacks for triggers
3508** contain a UTF-8 SQL comment that identifies the trigger.)^
3509**
3510** The [SQLITE_TRACE_SIZE_LIMIT] compile-time option can be used to limit
3511** the length of [bound parameter] expansion in the output of sqlite3_trace().
3512**
3513** ^The callback function registered by sqlite3_profile() is invoked
3514** as each SQL statement finishes. ^The profile callback contains
3515** the original statement text and an estimate of wall-clock time
3516** of how long that statement took to run. ^The profile callback
3517** time is in units of nanoseconds, however the current implementation
3518** is only capable of millisecond resolution so the six least significant
3519** digits in the time are meaningless. Future versions of SQLite
3520** might provide greater resolution on the profiler callback. Invoking
3521** either [sqlite3_trace()] or [sqlite3_trace_v2()] will cancel the
3522** profile callback.
3523*/
3524SQLITE_API SQLITE_DEPRECATED void *sqlite3_trace(sqlite3*,
3525 void(*xTrace)(void*,const char*), void*);
3526SQLITE_API SQLITE_DEPRECATED void *sqlite3_profile(sqlite3*,
3527 void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
3528
3529/*
3530** CAPI3REF: SQL Trace Event Codes
3531** KEYWORDS: SQLITE_TRACE
3532**
3533** These constants identify classes of events that can be monitored
3534** using the [sqlite3_trace_v2()] tracing logic. The M argument
3535** to [sqlite3_trace_v2(D,M,X,P)] is an OR-ed combination of one or more of
3536** the following constants. ^The first argument to the trace callback
3537** is one of the following constants.
3538**
3539** New tracing constants may be added in future releases.
3540**
3541** ^A trace callback has four arguments: xCallback(T,C,P,X).
3542** ^The T argument is one of the integer type codes above.
3543** ^The C argument is a copy of the context pointer passed in as the
3544** fourth argument to [sqlite3_trace_v2()].
3545** The P and X arguments are pointers whose meanings depend on T.
3546**
3547** <dl>
3548** [[SQLITE_TRACE_STMT]] <dt>SQLITE_TRACE_STMT</dt>
3549** <dd>^An SQLITE_TRACE_STMT callback is invoked when a prepared statement
3550** first begins running and possibly at other times during the
3551** execution of the prepared statement, such as at the start of each
3552** trigger subprogram. ^The P argument is a pointer to the
3553** [prepared statement]. ^The X argument is a pointer to a string which
3554** is the unexpanded SQL text of the prepared statement or an SQL comment
3555** that indicates the invocation of a trigger. ^The callback can compute
3556** the same text that would have been returned by the legacy [sqlite3_trace()]
3557** interface by using the X argument when X begins with "--" and invoking
3558** [sqlite3_expanded_sql(P)] otherwise.
3559**
3560** [[SQLITE_TRACE_PROFILE]] <dt>SQLITE_TRACE_PROFILE</dt>
3561** <dd>^An SQLITE_TRACE_PROFILE callback provides approximately the same
3562** information as is provided by the [sqlite3_profile()] callback.
3563** ^The P argument is a pointer to the [prepared statement] and the
3564** X argument points to a 64-bit integer which is the estimated of
3565** the number of nanosecond that the prepared statement took to run.
3566** ^The SQLITE_TRACE_PROFILE callback is invoked when the statement finishes.
3567**
3568** [[SQLITE_TRACE_ROW]] <dt>SQLITE_TRACE_ROW</dt>
3569** <dd>^An SQLITE_TRACE_ROW callback is invoked whenever a prepared
3570** statement generates a single row of result.
3571** ^The P argument is a pointer to the [prepared statement] and the
3572** X argument is unused.
3573**
3574** [[SQLITE_TRACE_CLOSE]] <dt>SQLITE_TRACE_CLOSE</dt>
3575** <dd>^An SQLITE_TRACE_CLOSE callback is invoked when a database
3576** connection closes.
3577** ^The P argument is a pointer to the [database connection] object
3578** and the X argument is unused.
3579** </dl>
3580*/
3581#define SQLITE_TRACE_STMT 0x01
3582#define SQLITE_TRACE_PROFILE 0x02
3583#define SQLITE_TRACE_ROW 0x04
3584#define SQLITE_TRACE_CLOSE 0x08
3585
3586/*
3587** CAPI3REF: SQL Trace Hook
3588** METHOD: sqlite3
3589**
3590** ^The sqlite3_trace_v2(D,M,X,P) interface registers a trace callback
3591** function X against [database connection] D, using property mask M
3592** and context pointer P. ^If the X callback is
3593** NULL or if the M mask is zero, then tracing is disabled. The
3594** M argument should be the bitwise OR-ed combination of
3595** zero or more [SQLITE_TRACE] constants.
3596**
3597** ^Each call to either sqlite3_trace() or sqlite3_trace_v2() overrides
3598** (cancels) any prior calls to sqlite3_trace() or sqlite3_trace_v2().
3599**
3600** ^The X callback is invoked whenever any of the events identified by
3601** mask M occur. ^The integer return value from the callback is currently
3602** ignored, though this may change in future releases. Callback
3603** implementations should return zero to ensure future compatibility.
3604**
3605** ^A trace callback is invoked with four arguments: callback(T,C,P,X).
3606** ^The T argument is one of the [SQLITE_TRACE]
3607** constants to indicate why the callback was invoked.
3608** ^The C argument is a copy of the context pointer.
3609** The P and X arguments are pointers whose meanings depend on T.
3610**
3611** The sqlite3_trace_v2() interface is intended to replace the legacy
3612** interfaces [sqlite3_trace()] and [sqlite3_profile()], both of which
3613** are deprecated.
3614*/
3615SQLITE_API int sqlite3_trace_v2(
3616 sqlite3*,
3617 unsigned uMask,
3618 int(*xCallback)(unsigned,void*,void*,void*),
3619 void *pCtx
3620);
3621
3622/*
3623** CAPI3REF: Query Progress Callbacks
3624** METHOD: sqlite3
3625**
3626** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback
3627** function X to be invoked periodically during long running calls to
3628** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for
3629** database connection D. An example use for this
3630** interface is to keep a GUI updated during a large query.
3631**
3632** ^The parameter P is passed through as the only parameter to the
3633** callback function X. ^The parameter N is the approximate number of
3634** [virtual machine instructions] that are evaluated between successive
3635** invocations of the callback X. ^If N is less than one then the progress
3636** handler is disabled.
3637**
3638** ^Only a single progress handler may be defined at one time per
3639** [database connection]; setting a new progress handler cancels the
3640** old one. ^Setting parameter X to NULL disables the progress handler.
3641** ^The progress handler is also disabled by setting N to a value less
3642** than 1.
3643**
3644** ^If the progress callback returns non-zero, the operation is
3645** interrupted. This feature can be used to implement a
3646** "Cancel" button on a GUI progress dialog box.
3647**
3648** The progress handler callback must not do anything that will modify
3649** the database connection that invoked the progress handler.
3650** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
3651** database connections for the meaning of "modify" in this paragraph.
3652**
3653*/
3654SQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
3655
3656/*
3657** CAPI3REF: Opening A New Database Connection
3658** CONSTRUCTOR: sqlite3
3659**
3660** ^These routines open an SQLite database file as specified by the
3661** filename argument. ^The filename argument is interpreted as UTF-8 for
3662** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
3663** order for sqlite3_open16(). ^(A [database connection] handle is usually
3664** returned in *ppDb, even if an error occurs. The only exception is that
3665** if SQLite is unable to allocate memory to hold the [sqlite3] object,
3666** a NULL will be written into *ppDb instead of a pointer to the [sqlite3]
3667** object.)^ ^(If the database is opened (and/or created) successfully, then
3668** [SQLITE_OK] is returned. Otherwise an [error code] is returned.)^ ^The
3669** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
3670** an English language description of the error following a failure of any
3671** of the sqlite3_open() routines.
3672**
3673** ^The default encoding will be UTF-8 for databases created using
3674** sqlite3_open() or sqlite3_open_v2(). ^The default encoding for databases
3675** created using sqlite3_open16() will be UTF-16 in the native byte order.
3676**
3677** Whether or not an error occurs when it is opened, resources
3678** associated with the [database connection] handle should be released by
3679** passing it to [sqlite3_close()] when it is no longer required.
3680**
3681** The sqlite3_open_v2() interface works like sqlite3_open()
3682** except that it accepts two additional parameters for additional control
3683** over the new database connection. ^(The flags parameter to
3684** sqlite3_open_v2() must include, at a minimum, one of the following
3685** three flag combinations:)^
3686**
3687** <dl>
3688** ^(<dt>[SQLITE_OPEN_READONLY]</dt>
3689** <dd>The database is opened in read-only mode. If the database does not
3690** already exist, an error is returned.</dd>)^
3691**
3692** ^(<dt>[SQLITE_OPEN_READWRITE]</dt>
3693** <dd>The database is opened for reading and writing if possible, or reading
3694** only if the file is write protected by the operating system. In either
3695** case the database must already exist, otherwise an error is returned.</dd>)^
3696**
3697** ^(<dt>[SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]</dt>
3698** <dd>The database is opened for reading and writing, and is created if
3699** it does not already exist. This is the behavior that is always used for
3700** sqlite3_open() and sqlite3_open16().</dd>)^
3701** </dl>
3702**
3703** In addition to the required flags, the following optional flags are
3704** also supported:
3705**
3706** <dl>
3707** ^(<dt>[SQLITE_OPEN_URI]</dt>
3708** <dd>The filename can be interpreted as a URI if this flag is set.</dd>)^
3709**
3710** ^(<dt>[SQLITE_OPEN_MEMORY]</dt>
3711** <dd>The database will be opened as an in-memory database. The database
3712** is named by the "filename" argument for the purposes of cache-sharing,
3713** if shared cache mode is enabled, but the "filename" is otherwise ignored.
3714** </dd>)^
3715**
3716** ^(<dt>[SQLITE_OPEN_NOMUTEX]</dt>
3717** <dd>The new database connection will use the "multi-thread"
3718** [threading mode].)^ This means that separate threads are allowed
3719** to use SQLite at the same time, as long as each thread is using
3720** a different [database connection].
3721**
3722** ^(<dt>[SQLITE_OPEN_FULLMUTEX]</dt>
3723** <dd>The new database connection will use the "serialized"
3724** [threading mode].)^ This means the multiple threads can safely
3725** attempt to use the same database connection at the same time.
3726** (Mutexes will block any actual concurrency, but in this mode
3727** there is no harm in trying.)
3728**
3729** ^(<dt>[SQLITE_OPEN_SHAREDCACHE]</dt>
3730** <dd>The database is opened [shared cache] enabled, overriding
3731** the default shared cache setting provided by
3732** [sqlite3_enable_shared_cache()].)^
3733**
3734** ^(<dt>[SQLITE_OPEN_PRIVATECACHE]</dt>
3735** <dd>The database is opened [shared cache] disabled, overriding
3736** the default shared cache setting provided by
3737** [sqlite3_enable_shared_cache()].)^
3738**
3739** [[OPEN_EXRESCODE]] ^(<dt>[SQLITE_OPEN_EXRESCODE]</dt>
3740** <dd>The database connection comes up in "extended result code mode".
3741** In other words, the database behaves has if
3742** [sqlite3_extended_result_codes(db,1)] where called on the database
3743** connection as soon as the connection is created. In addition to setting
3744** the extended result code mode, this flag also causes [sqlite3_open_v2()]
3745** to return an extended result code.</dd>
3746**
3747** [[OPEN_NOFOLLOW]] ^(<dt>[SQLITE_OPEN_NOFOLLOW]</dt>
3748** <dd>The database filename is not allowed to be a symbolic link</dd>
3749** </dl>)^
3750**
3751** If the 3rd parameter to sqlite3_open_v2() is not one of the
3752** required combinations shown above optionally combined with other
3753** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits]
3754** then the behavior is undefined. Historic versions of SQLite
3755** have silently ignored surplus bits in the flags parameter to
3756** sqlite3_open_v2(), however that behavior might not be carried through
3757** into future versions of SQLite and so applications should not rely
3758** upon it. Note in particular that the SQLITE_OPEN_EXCLUSIVE flag is a no-op
3759** for sqlite3_open_v2(). The SQLITE_OPEN_EXCLUSIVE does *not* cause
3760** the open to fail if the database already exists. The SQLITE_OPEN_EXCLUSIVE
3761** flag is intended for use by the [sqlite3_vfs|VFS interface] only, and not
3762** by sqlite3_open_v2().
3763**
3764** ^The fourth parameter to sqlite3_open_v2() is the name of the
3765** [sqlite3_vfs] object that defines the operating system interface that
3766** the new database connection should use. ^If the fourth parameter is
3767** a NULL pointer then the default [sqlite3_vfs] object is used.
3768**
3769** ^If the filename is ":memory:", then a private, temporary in-memory database
3770** is created for the connection. ^This in-memory database will vanish when
3771** the database connection is closed. Future versions of SQLite might
3772** make use of additional special filenames that begin with the ":" character.
3773** It is recommended that when a database filename actually does begin with
3774** a ":" character you should prefix the filename with a pathname such as
3775** "./" to avoid ambiguity.
3776**
3777** ^If the filename is an empty string, then a private, temporary
3778** on-disk database will be created. ^This private database will be
3779** automatically deleted as soon as the database connection is closed.
3780**
3781** [[URI filenames in sqlite3_open()]] <h3>URI Filenames</h3>
3782**
3783** ^If [URI filename] interpretation is enabled, and the filename argument
3784** begins with "file:", then the filename is interpreted as a URI. ^URI
3785** filename interpretation is enabled if the [SQLITE_OPEN_URI] flag is
3786** set in the third argument to sqlite3_open_v2(), or if it has
3787** been enabled globally using the [SQLITE_CONFIG_URI] option with the
3788** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
3789** URI filename interpretation is turned off
3790** by default, but future releases of SQLite might enable URI filename
3791** interpretation by default. See "[URI filenames]" for additional
3792** information.
3793**
3794** URI filenames are parsed according to RFC 3986. ^If the URI contains an
3795** authority, then it must be either an empty string or the string
3796** "localhost". ^If the authority is not an empty string or "localhost", an
3797** error is returned to the caller. ^The fragment component of a URI, if
3798** present, is ignored.
3799**
3800** ^SQLite uses the path component of the URI as the name of the disk file
3801** which contains the database. ^If the path begins with a '/' character,
3802** then it is interpreted as an absolute path. ^If the path does not begin
3803** with a '/' (meaning that the authority section is omitted from the URI)
3804** then the path is interpreted as a relative path.
3805** ^(On windows, the first component of an absolute path
3806** is a drive specification (e.g. "C:").)^
3807**
3808** [[core URI query parameters]]
3809** The query component of a URI may contain parameters that are interpreted
3810** either by SQLite itself, or by a [VFS | custom VFS implementation].
3811** SQLite and its built-in [VFSes] interpret the
3812** following query parameters:
3813**
3814** <ul>
3815** <li> <b>vfs</b>: ^The "vfs" parameter may be used to specify the name of
3816** a VFS object that provides the operating system interface that should
3817** be used to access the database file on disk. ^If this option is set to
3818** an empty string the default VFS object is used. ^Specifying an unknown
3819** VFS is an error. ^If sqlite3_open_v2() is used and the vfs option is
3820** present, then the VFS specified by the option takes precedence over
3821** the value passed as the fourth parameter to sqlite3_open_v2().
3822**
3823** <li> <b>mode</b>: ^(The mode parameter may be set to either "ro", "rw",
3824** "rwc", or "memory". Attempting to set it to any other value is
3825** an error)^.
3826** ^If "ro" is specified, then the database is opened for read-only
3827** access, just as if the [SQLITE_OPEN_READONLY] flag had been set in the
3828** third argument to sqlite3_open_v2(). ^If the mode option is set to
3829** "rw", then the database is opened for read-write (but not create)
3830** access, as if SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had
3831** been set. ^Value "rwc" is equivalent to setting both
3832** SQLITE_OPEN_READWRITE and SQLITE_OPEN_CREATE. ^If the mode option is
3833** set to "memory" then a pure [in-memory database] that never reads
3834** or writes from disk is used. ^It is an error to specify a value for
3835** the mode parameter that is less restrictive than that specified by
3836** the flags passed in the third parameter to sqlite3_open_v2().
3837**
3838** <li> <b>cache</b>: ^The cache parameter may be set to either "shared" or
3839** "private". ^Setting it to "shared" is equivalent to setting the
3840** SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed to
3841** sqlite3_open_v2(). ^Setting the cache parameter to "private" is
3842** equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
3843** ^If sqlite3_open_v2() is used and the "cache" parameter is present in
3844** a URI filename, its value overrides any behavior requested by setting
3845** SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
3846**
3847** <li> <b>psow</b>: ^The psow parameter indicates whether or not the
3848** [powersafe overwrite] property does or does not apply to the
3849** storage media on which the database file resides.
3850**
3851** <li> <b>nolock</b>: ^The nolock parameter is a boolean query parameter
3852** which if set disables file locking in rollback journal modes. This
3853** is useful for accessing a database on a filesystem that does not
3854** support locking. Caution: Database corruption might result if two
3855** or more processes write to the same database and any one of those
3856** processes uses nolock=1.
3857**
3858** <li> <b>immutable</b>: ^The immutable parameter is a boolean query
3859** parameter that indicates that the database file is stored on
3860** read-only media. ^When immutable is set, SQLite assumes that the
3861** database file cannot be changed, even by a process with higher
3862** privilege, and so the database is opened read-only and all locking
3863** and change detection is disabled. Caution: Setting the immutable
3864** property on a database file that does in fact change can result
3865** in incorrect query results and/or [SQLITE_CORRUPT] errors.
3866** See also: [SQLITE_IOCAP_IMMUTABLE].
3867**
3868** </ul>
3869**
3870** ^Specifying an unknown parameter in the query component of a URI is not an
3871** error. Future versions of SQLite might understand additional query
3872** parameters. See "[query parameters with special meaning to SQLite]" for
3873** additional information.
3874**
3875** [[URI filename examples]] <h3>URI filename examples</h3>
3876**
3877** <table border="1" align=center cellpadding=5>
3878** <tr><th> URI filenames <th> Results
3879** <tr><td> file:data.db <td>
3880** Open the file "data.db" in the current directory.
3881** <tr><td> file:/home/fred/data.db<br>
3882** file:///home/fred/data.db <br>
3883** file://localhost/home/fred/data.db <br> <td>
3884** Open the database file "/home/fred/data.db".
3885** <tr><td> file://darkstar/home/fred/data.db <td>
3886** An error. "darkstar" is not a recognized authority.
3887** <tr><td style="white-space:nowrap">
3888** file:///C:/Documents%20and%20Settings/fred/Desktop/data.db
3889** <td> Windows only: Open the file "data.db" on fred's desktop on drive
3890** C:. Note that the %20 escaping in this example is not strictly
3891** necessary - space characters can be used literally
3892** in URI filenames.
3893** <tr><td> file:data.db?mode=ro&cache=private <td>
3894** Open file "data.db" in the current directory for read-only access.
3895** Regardless of whether or not shared-cache mode is enabled by
3896** default, use a private cache.
3897** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
3898** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
3899** that uses dot-files in place of posix advisory locking.
3900** <tr><td> file:data.db?mode=readonly <td>
3901** An error. "readonly" is not a valid option for the "mode" parameter.
3902** Use "ro" instead: "file:data.db?mode=ro".
3903** </table>
3904**
3905** ^URI hexadecimal escape sequences (%HH) are supported within the path and
3906** query components of a URI. A hexadecimal escape sequence consists of a
3907** percent sign - "%" - followed by exactly two hexadecimal digits
3908** specifying an octet value. ^Before the path or query components of a
3909** URI filename are interpreted, they are encoded using UTF-8 and all
3910** hexadecimal escape sequences replaced by a single byte containing the
3911** corresponding octet. If this process generates an invalid UTF-8 encoding,
3912** the results are undefined.
3913**
3914** <b>Note to Windows users:</b> The encoding used for the filename argument
3915** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
3916** codepage is currently defined. Filenames containing international
3917** characters must be converted to UTF-8 prior to passing them into
3918** sqlite3_open() or sqlite3_open_v2().
3919**
3920** <b>Note to Windows Runtime users:</b> The temporary directory must be set
3921** prior to calling sqlite3_open() or sqlite3_open_v2(). Otherwise, various
3922** features that require the use of temporary files may fail.
3923**
3924** See also: [sqlite3_temp_directory]
3925*/
3926SQLITE_API int sqlite3_open(
3927 const char *filename, /* Database filename (UTF-8) */
3928 sqlite3 **ppDb /* OUT: SQLite db handle */
3929);
3930SQLITE_API int sqlite3_open16(
3931 const void *filename, /* Database filename (UTF-16) */
3932 sqlite3 **ppDb /* OUT: SQLite db handle */
3933);
3934SQLITE_API int sqlite3_open_v2(
3935 const char *filename, /* Database filename (UTF-8) */
3936 sqlite3 **ppDb, /* OUT: SQLite db handle */
3937 int flags, /* Flags */
3938 const char *zVfs /* Name of VFS module to use */
3939);
3940
3941/*
3942** CAPI3REF: Obtain Values For URI Parameters
3943**
3944** These are utility routines, useful to [VFS|custom VFS implementations],
3945** that check if a database file was a URI that contained a specific query
3946** parameter, and if so obtains the value of that query parameter.
3947**
3948** The first parameter to these interfaces (hereafter referred to
3949** as F) must be one of:
3950** <ul>
3951** <li> A database filename pointer created by the SQLite core and
3952** passed into the xOpen() method of a VFS implemention, or
3953** <li> A filename obtained from [sqlite3_db_filename()], or
3954** <li> A new filename constructed using [sqlite3_create_filename()].
3955** </ul>
3956** If the F parameter is not one of the above, then the behavior is
3957** undefined and probably undesirable. Older versions of SQLite were
3958** more tolerant of invalid F parameters than newer versions.
3959**
3960** If F is a suitable filename (as described in the previous paragraph)
3961** and if P is the name of the query parameter, then
3962** sqlite3_uri_parameter(F,P) returns the value of the P
3963** parameter if it exists or a NULL pointer if P does not appear as a
3964** query parameter on F. If P is a query parameter of F and it
3965** has no explicit value, then sqlite3_uri_parameter(F,P) returns
3966** a pointer to an empty string.
3967**
3968** The sqlite3_uri_boolean(F,P,B) routine assumes that P is a boolean
3969** parameter and returns true (1) or false (0) according to the value
3970** of P. The sqlite3_uri_boolean(F,P,B) routine returns true (1) if the
3971** value of query parameter P is one of "yes", "true", or "on" in any
3972** case or if the value begins with a non-zero number. The
3973** sqlite3_uri_boolean(F,P,B) routines returns false (0) if the value of
3974** query parameter P is one of "no", "false", or "off" in any case or
3975** if the value begins with a numeric zero. If P is not a query
3976** parameter on F or if the value of P does not match any of the
3977** above, then sqlite3_uri_boolean(F,P,B) returns (B!=0).
3978**
3979** The sqlite3_uri_int64(F,P,D) routine converts the value of P into a
3980** 64-bit signed integer and returns that integer, or D if P does not
3981** exist. If the value of P is something other than an integer, then
3982** zero is returned.
3983**
3984** The sqlite3_uri_key(F,N) returns a pointer to the name (not
3985** the value) of the N-th query parameter for filename F, or a NULL
3986** pointer if N is less than zero or greater than the number of query
3987** parameters minus 1. The N value is zero-based so N should be 0 to obtain
3988** the name of the first query parameter, 1 for the second parameter, and
3989** so forth.
3990**
3991** If F is a NULL pointer, then sqlite3_uri_parameter(F,P) returns NULL and
3992** sqlite3_uri_boolean(F,P,B) returns B. If F is not a NULL pointer and
3993** is not a database file pathname pointer that the SQLite core passed
3994** into the xOpen VFS method, then the behavior of this routine is undefined
3995** and probably undesirable.
3996**
3997** Beginning with SQLite [version 3.31.0] ([dateof:3.31.0]) the input F
3998** parameter can also be the name of a rollback journal file or WAL file
3999** in addition to the main database file. Prior to version 3.31.0, these
4000** routines would only work if F was the name of the main database file.
4001** When the F parameter is the name of the rollback journal or WAL file,
4002** it has access to all the same query parameters as were found on the
4003** main database file.
4004**
4005** See the [URI filename] documentation for additional information.
4006*/
4007SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
4008SQLITE_API int sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
4009SQLITE_API sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
4010SQLITE_API const char *sqlite3_uri_key(const char *zFilename, int N);
4011
4012/*
4013** CAPI3REF: Translate filenames
4014**
4015** These routines are available to [VFS|custom VFS implementations] for
4016** translating filenames between the main database file, the journal file,
4017** and the WAL file.
4018**
4019** If F is the name of an sqlite database file, journal file, or WAL file
4020** passed by the SQLite core into the VFS, then sqlite3_filename_database(F)
4021** returns the name of the corresponding database file.
4022**
4023** If F is the name of an sqlite database file, journal file, or WAL file
4024** passed by the SQLite core into the VFS, or if F is a database filename
4025** obtained from [sqlite3_db_filename()], then sqlite3_filename_journal(F)
4026** returns the name of the corresponding rollback journal file.
4027**
4028** If F is the name of an sqlite database file, journal file, or WAL file
4029** that was passed by the SQLite core into the VFS, or if F is a database
4030** filename obtained from [sqlite3_db_filename()], then
4031** sqlite3_filename_wal(F) returns the name of the corresponding
4032** WAL file.
4033**
4034** In all of the above, if F is not the name of a database, journal or WAL
4035** filename passed into the VFS from the SQLite core and F is not the
4036** return value from [sqlite3_db_filename()], then the result is
4037** undefined and is likely a memory access violation.
4038*/
4039SQLITE_API const char *sqlite3_filename_database(const char*);
4040SQLITE_API const char *sqlite3_filename_journal(const char*);
4041SQLITE_API const char *sqlite3_filename_wal(const char*);
4042
4043/*
4044** CAPI3REF: Database File Corresponding To A Journal
4045**
4046** ^If X is the name of a rollback or WAL-mode journal file that is
4047** passed into the xOpen method of [sqlite3_vfs], then
4048** sqlite3_database_file_object(X) returns a pointer to the [sqlite3_file]
4049** object that represents the main database file.
4050**
4051** This routine is intended for use in custom [VFS] implementations
4052** only. It is not a general-purpose interface.
4053** The argument sqlite3_file_object(X) must be a filename pointer that
4054** has been passed into [sqlite3_vfs].xOpen method where the
4055** flags parameter to xOpen contains one of the bits
4056** [SQLITE_OPEN_MAIN_JOURNAL] or [SQLITE_OPEN_WAL]. Any other use
4057** of this routine results in undefined and probably undesirable
4058** behavior.
4059*/
4060SQLITE_API sqlite3_file *sqlite3_database_file_object(const char*);
4061
4062/*
4063** CAPI3REF: Create and Destroy VFS Filenames
4064**
4065** These interfces are provided for use by [VFS shim] implementations and
4066** are not useful outside of that context.
4067**
4068** The sqlite3_create_filename(D,J,W,N,P) allocates memory to hold a version of
4069** database filename D with corresponding journal file J and WAL file W and
4070** with N URI parameters key/values pairs in the array P. The result from
4071** sqlite3_create_filename(D,J,W,N,P) is a pointer to a database filename that
4072** is safe to pass to routines like:
4073** <ul>
4074** <li> [sqlite3_uri_parameter()],
4075** <li> [sqlite3_uri_boolean()],
4076** <li> [sqlite3_uri_int64()],
4077** <li> [sqlite3_uri_key()],
4078** <li> [sqlite3_filename_database()],
4079** <li> [sqlite3_filename_journal()], or
4080** <li> [sqlite3_filename_wal()].
4081** </ul>
4082** If a memory allocation error occurs, sqlite3_create_filename() might
4083** return a NULL pointer. The memory obtained from sqlite3_create_filename(X)
4084** must be released by a corresponding call to sqlite3_free_filename(Y).
4085**
4086** The P parameter in sqlite3_create_filename(D,J,W,N,P) should be an array
4087** of 2*N pointers to strings. Each pair of pointers in this array corresponds
4088** to a key and value for a query parameter. The P parameter may be a NULL
4089** pointer if N is zero. None of the 2*N pointers in the P array may be
4090** NULL pointers and key pointers should not be empty strings.
4091** None of the D, J, or W parameters to sqlite3_create_filename(D,J,W,N,P) may
4092** be NULL pointers, though they can be empty strings.
4093**
4094** The sqlite3_free_filename(Y) routine releases a memory allocation
4095** previously obtained from sqlite3_create_filename(). Invoking
4096** sqlite3_free_filename(Y) where Y is a NULL pointer is a harmless no-op.
4097**
4098** If the Y parameter to sqlite3_free_filename(Y) is anything other
4099** than a NULL pointer or a pointer previously acquired from
4100** sqlite3_create_filename(), then bad things such as heap
4101** corruption or segfaults may occur. The value Y should not be
4102** used again after sqlite3_free_filename(Y) has been called. This means
4103** that if the [sqlite3_vfs.xOpen()] method of a VFS has been called using Y,
4104** then the corresponding [sqlite3_module.xClose() method should also be
4105** invoked prior to calling sqlite3_free_filename(Y).
4106*/
4107SQLITE_API char *sqlite3_create_filename(
4108 const char *zDatabase,
4109 const char *zJournal,
4110 const char *zWal,
4111 int nParam,
4112 const char **azParam
4113);
4114SQLITE_API void sqlite3_free_filename(char*);
4115
4116/*
4117** CAPI3REF: Error Codes And Messages
4118** METHOD: sqlite3
4119**
4120** ^If the most recent sqlite3_* API call associated with
4121** [database connection] D failed, then the sqlite3_errcode(D) interface
4122** returns the numeric [result code] or [extended result code] for that
4123** API call.
4124** ^The sqlite3_extended_errcode()
4125** interface is the same except that it always returns the
4126** [extended result code] even when extended result codes are
4127** disabled.
4128**
4129** The values returned by sqlite3_errcode() and/or
4130** sqlite3_extended_errcode() might change with each API call.
4131** Except, there are some interfaces that are guaranteed to never
4132** change the value of the error code. The error-code preserving
4133** interfaces are:
4134**
4135** <ul>
4136** <li> sqlite3_errcode()
4137** <li> sqlite3_extended_errcode()
4138** <li> sqlite3_errmsg()
4139** <li> sqlite3_errmsg16()
4140** </ul>
4141**
4142** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
4143** text that describes the error, as either UTF-8 or UTF-16 respectively.
4144** ^(Memory to hold the error message string is managed internally.
4145** The application does not need to worry about freeing the result.
4146** However, the error string might be overwritten or deallocated by
4147** subsequent calls to other SQLite interface functions.)^
4148**
4149** ^The sqlite3_errstr() interface returns the English-language text
4150** that describes the [result code], as UTF-8.
4151** ^(Memory to hold the error message string is managed internally
4152** and must not be freed by the application)^.
4153**
4154** When the serialized [threading mode] is in use, it might be the
4155** case that a second error occurs on a separate thread in between
4156** the time of the first error and the call to these interfaces.
4157** When that happens, the second error will be reported since these
4158** interfaces always report the most recent result. To avoid
4159** this, each thread can obtain exclusive use of the [database connection] D
4160** by invoking [sqlite3_mutex_enter]([sqlite3_db_mutex](D)) before beginning
4161** to use D and invoking [sqlite3_mutex_leave]([sqlite3_db_mutex](D)) after
4162** all calls to the interfaces listed here are completed.
4163**
4164** If an interface fails with SQLITE_MISUSE, that means the interface
4165** was invoked incorrectly by the application. In that case, the
4166** error code and message may or may not be set.
4167*/
4168SQLITE_API int sqlite3_errcode(sqlite3 *db);
4169SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
4170SQLITE_API const char *sqlite3_errmsg(sqlite3*);
4171SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
4172SQLITE_API const char *sqlite3_errstr(int);
4173
4174/*
4175** CAPI3REF: Prepared Statement Object
4176** KEYWORDS: {prepared statement} {prepared statements}
4177**
4178** An instance of this object represents a single SQL statement that
4179** has been compiled into binary form and is ready to be evaluated.
4180**
4181** Think of each SQL statement as a separate computer program. The
4182** original SQL text is source code. A prepared statement object
4183** is the compiled object code. All SQL must be converted into a
4184** prepared statement before it can be run.
4185**
4186** The life-cycle of a prepared statement object usually goes like this:
4187**
4188** <ol>
4189** <li> Create the prepared statement object using [sqlite3_prepare_v2()].
4190** <li> Bind values to [parameters] using the sqlite3_bind_*()
4191** interfaces.
4192** <li> Run the SQL by calling [sqlite3_step()] one or more times.
4193** <li> Reset the prepared statement using [sqlite3_reset()] then go back
4194** to step 2. Do this zero or more times.
4195** <li> Destroy the object using [sqlite3_finalize()].
4196** </ol>
4197*/
4198typedef struct sqlite3_stmt sqlite3_stmt;
4199
4200/*
4201** CAPI3REF: Run-time Limits
4202** METHOD: sqlite3
4203**
4204** ^(This interface allows the size of various constructs to be limited
4205** on a connection by connection basis. The first parameter is the
4206** [database connection] whose limit is to be set or queried. The
4207** second parameter is one of the [limit categories] that define a
4208** class of constructs to be size limited. The third parameter is the
4209** new limit for that construct.)^
4210**
4211** ^If the new limit is a negative number, the limit is unchanged.
4212** ^(For each limit category SQLITE_LIMIT_<i>NAME</i> there is a
4213** [limits | hard upper bound]
4214** set at compile-time by a C preprocessor macro called
4215** [limits | SQLITE_MAX_<i>NAME</i>].
4216** (The "_LIMIT_" in the name is changed to "_MAX_".))^
4217** ^Attempts to increase a limit above its hard upper bound are
4218** silently truncated to the hard upper bound.
4219**
4220** ^Regardless of whether or not the limit was changed, the
4221** [sqlite3_limit()] interface returns the prior value of the limit.
4222** ^Hence, to find the current value of a limit without changing it,
4223** simply invoke this interface with the third parameter set to -1.
4224**
4225** Run-time limits are intended for use in applications that manage
4226** both their own internal database and also databases that are controlled
4227** by untrusted external sources. An example application might be a
4228** web browser that has its own databases for storing history and
4229** separate databases controlled by JavaScript applications downloaded
4230** off the Internet. The internal databases can be given the
4231** large, default limits. Databases managed by external sources can
4232** be given much smaller limits designed to prevent a denial of service
4233** attack. Developers might also want to use the [sqlite3_set_authorizer()]
4234** interface to further control untrusted SQL. The size of the database
4235** created by an untrusted script can be contained using the
4236** [max_page_count] [PRAGMA].
4237**
4238** New run-time limit categories may be added in future releases.
4239*/
4240SQLITE_API int sqlite3_limit(sqlite3*, int id, int newVal);
4241
4242/*
4243** CAPI3REF: Run-Time Limit Categories
4244** KEYWORDS: {limit category} {*limit categories}
4245**
4246** These constants define various performance limits
4247** that can be lowered at run-time using [sqlite3_limit()].
4248** The synopsis of the meanings of the various limits is shown below.
4249** Additional information is available at [limits | Limits in SQLite].
4250**
4251** <dl>
4252** [[SQLITE_LIMIT_LENGTH]] ^(<dt>SQLITE_LIMIT_LENGTH</dt>
4253** <dd>The maximum size of any string or BLOB or table row, in bytes.<dd>)^
4254**
4255** [[SQLITE_LIMIT_SQL_LENGTH]] ^(<dt>SQLITE_LIMIT_SQL_LENGTH</dt>
4256** <dd>The maximum length of an SQL statement, in bytes.</dd>)^
4257**
4258** [[SQLITE_LIMIT_COLUMN]] ^(<dt>SQLITE_LIMIT_COLUMN</dt>
4259** <dd>The maximum number of columns in a table definition or in the
4260** result set of a [SELECT] or the maximum number of columns in an index
4261** or in an ORDER BY or GROUP BY clause.</dd>)^
4262**
4263** [[SQLITE_LIMIT_EXPR_DEPTH]] ^(<dt>SQLITE_LIMIT_EXPR_DEPTH</dt>
4264** <dd>The maximum depth of the parse tree on any expression.</dd>)^
4265**
4266** [[SQLITE_LIMIT_COMPOUND_SELECT]] ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt>
4267** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^
4268**
4269** [[SQLITE_LIMIT_VDBE_OP]] ^(<dt>SQLITE_LIMIT_VDBE_OP</dt>
4270** <dd>The maximum number of instructions in a virtual machine program
4271** used to implement an SQL statement. If [sqlite3_prepare_v2()] or
4272** the equivalent tries to allocate space for more than this many opcodes
4273** in a single prepared statement, an SQLITE_NOMEM error is returned.</dd>)^
4274**
4275** [[SQLITE_LIMIT_FUNCTION_ARG]] ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt>
4276** <dd>The maximum number of arguments on a function.</dd>)^
4277**
4278** [[SQLITE_LIMIT_ATTACHED]] ^(<dt>SQLITE_LIMIT_ATTACHED</dt>
4279** <dd>The maximum number of [ATTACH | attached databases].)^</dd>
4280**
4281** [[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]]
4282** ^(<dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt>
4283** <dd>The maximum length of the pattern argument to the [LIKE] or
4284** [GLOB] operators.</dd>)^
4285**
4286** [[SQLITE_LIMIT_VARIABLE_NUMBER]]
4287** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt>
4288** <dd>The maximum index number of any [parameter] in an SQL statement.)^
4289**
4290** [[SQLITE_LIMIT_TRIGGER_DEPTH]] ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt>
4291** <dd>The maximum depth of recursion for triggers.</dd>)^
4292**
4293** [[SQLITE_LIMIT_WORKER_THREADS]] ^(<dt>SQLITE_LIMIT_WORKER_THREADS</dt>
4294** <dd>The maximum number of auxiliary worker threads that a single
4295** [prepared statement] may start.</dd>)^
4296** </dl>
4297*/
4298#define SQLITE_LIMIT_LENGTH 0
4299#define SQLITE_LIMIT_SQL_LENGTH 1
4300#define SQLITE_LIMIT_COLUMN 2
4301#define SQLITE_LIMIT_EXPR_DEPTH 3
4302#define SQLITE_LIMIT_COMPOUND_SELECT 4
4303#define SQLITE_LIMIT_VDBE_OP 5
4304#define SQLITE_LIMIT_FUNCTION_ARG 6
4305#define SQLITE_LIMIT_ATTACHED 7
4306#define SQLITE_LIMIT_LIKE_PATTERN_LENGTH 8
4307#define SQLITE_LIMIT_VARIABLE_NUMBER 9
4308#define SQLITE_LIMIT_TRIGGER_DEPTH 10
4309#define SQLITE_LIMIT_WORKER_THREADS 11
4310
4311/*
4312** CAPI3REF: Prepare Flags
4313**
4314** These constants define various flags that can be passed into
4315** "prepFlags" parameter of the [sqlite3_prepare_v3()] and
4316** [sqlite3_prepare16_v3()] interfaces.
4317**
4318** New flags may be added in future releases of SQLite.
4319**
4320** <dl>
4321** [[SQLITE_PREPARE_PERSISTENT]] ^(<dt>SQLITE_PREPARE_PERSISTENT</dt>
4322** <dd>The SQLITE_PREPARE_PERSISTENT flag is a hint to the query planner
4323** that the prepared statement will be retained for a long time and
4324** probably reused many times.)^ ^Without this flag, [sqlite3_prepare_v3()]
4325** and [sqlite3_prepare16_v3()] assume that the prepared statement will
4326** be used just once or at most a few times and then destroyed using
4327** [sqlite3_finalize()] relatively soon. The current implementation acts
4328** on this hint by avoiding the use of [lookaside memory] so as not to
4329** deplete the limited store of lookaside memory. Future versions of
4330** SQLite may act on this hint differently.
4331**
4332** [[SQLITE_PREPARE_NORMALIZE]] <dt>SQLITE_PREPARE_NORMALIZE</dt>
4333** <dd>The SQLITE_PREPARE_NORMALIZE flag is a no-op. This flag used
4334** to be required for any prepared statement that wanted to use the
4335** [sqlite3_normalized_sql()] interface. However, the
4336** [sqlite3_normalized_sql()] interface is now available to all
4337** prepared statements, regardless of whether or not they use this
4338** flag.
4339**
4340** [[SQLITE_PREPARE_NO_VTAB]] <dt>SQLITE_PREPARE_NO_VTAB</dt>
4341** <dd>The SQLITE_PREPARE_NO_VTAB flag causes the SQL compiler
4342** to return an error (error code SQLITE_ERROR) if the statement uses
4343** any virtual tables.
4344** </dl>
4345*/
4346#define SQLITE_PREPARE_PERSISTENT 0x01
4347#define SQLITE_PREPARE_NORMALIZE 0x02
4348#define SQLITE_PREPARE_NO_VTAB 0x04
4349
4350/*
4351** CAPI3REF: Compiling An SQL Statement
4352** KEYWORDS: {SQL statement compiler}
4353** METHOD: sqlite3
4354** CONSTRUCTOR: sqlite3_stmt
4355**
4356** To execute an SQL statement, it must first be compiled into a byte-code
4357** program using one of these routines. Or, in other words, these routines
4358** are constructors for the [prepared statement] object.
4359**
4360** The preferred routine to use is [sqlite3_prepare_v2()]. The
4361** [sqlite3_prepare()] interface is legacy and should be avoided.
4362** [sqlite3_prepare_v3()] has an extra "prepFlags" option that is used
4363** for special purposes.
4364**
4365** The use of the UTF-8 interfaces is preferred, as SQLite currently
4366** does all parsing using UTF-8. The UTF-16 interfaces are provided
4367** as a convenience. The UTF-16 interfaces work by converting the
4368** input text into UTF-8, then invoking the corresponding UTF-8 interface.
4369**
4370** The first argument, "db", is a [database connection] obtained from a
4371** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or
4372** [sqlite3_open16()]. The database connection must not have been closed.
4373**
4374** The second argument, "zSql", is the statement to be compiled, encoded
4375** as either UTF-8 or UTF-16. The sqlite3_prepare(), sqlite3_prepare_v2(),
4376** and sqlite3_prepare_v3()
4377** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(),
4378** and sqlite3_prepare16_v3() use UTF-16.
4379**
4380** ^If the nByte argument is negative, then zSql is read up to the
4381** first zero terminator. ^If nByte is positive, then it is the
4382** number of bytes read from zSql. ^If nByte is zero, then no prepared
4383** statement is generated.
4384** If the caller knows that the supplied string is nul-terminated, then
4385** there is a small performance advantage to passing an nByte parameter that
4386** is the number of bytes in the input string <i>including</i>
4387** the nul-terminator.
4388**
4389** ^If pzTail is not NULL then *pzTail is made to point to the first byte
4390** past the end of the first SQL statement in zSql. These routines only
4391** compile the first statement in zSql, so *pzTail is left pointing to
4392** what remains uncompiled.
4393**
4394** ^*ppStmt is left pointing to a compiled [prepared statement] that can be
4395** executed using [sqlite3_step()]. ^If there is an error, *ppStmt is set
4396** to NULL. ^If the input text contains no SQL (if the input is an empty
4397** string or a comment) then *ppStmt is set to NULL.
4398** The calling procedure is responsible for deleting the compiled
4399** SQL statement using [sqlite3_finalize()] after it has finished with it.
4400** ppStmt may not be NULL.
4401**
4402** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK];
4403** otherwise an [error code] is returned.
4404**
4405** The sqlite3_prepare_v2(), sqlite3_prepare_v3(), sqlite3_prepare16_v2(),
4406** and sqlite3_prepare16_v3() interfaces are recommended for all new programs.
4407** The older interfaces (sqlite3_prepare() and sqlite3_prepare16())
4408** are retained for backwards compatibility, but their use is discouraged.
4409** ^In the "vX" interfaces, the prepared statement
4410** that is returned (the [sqlite3_stmt] object) contains a copy of the
4411** original SQL text. This causes the [sqlite3_step()] interface to
4412** behave differently in three ways:
4413**
4414** <ol>
4415** <li>
4416** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it
4417** always used to do, [sqlite3_step()] will automatically recompile the SQL
4418** statement and try to run it again. As many as [SQLITE_MAX_SCHEMA_RETRY]
4419** retries will occur before sqlite3_step() gives up and returns an error.
4420** </li>
4421**
4422** <li>
4423** ^When an error occurs, [sqlite3_step()] will return one of the detailed
4424** [error codes] or [extended error codes]. ^The legacy behavior was that
4425** [sqlite3_step()] would only return a generic [SQLITE_ERROR] result code
4426** and the application would have to make a second call to [sqlite3_reset()]
4427** in order to find the underlying cause of the problem. With the "v2" prepare
4428** interfaces, the underlying reason for the error is returned immediately.
4429** </li>
4430**
4431** <li>
4432** ^If the specific value bound to a [parameter | host parameter] in the
4433** WHERE clause might influence the choice of query plan for a statement,
4434** then the statement will be automatically recompiled, as if there had been
4435** a schema change, on the first [sqlite3_step()] call following any change
4436** to the [sqlite3_bind_text | bindings] of that [parameter].
4437** ^The specific value of a WHERE-clause [parameter] might influence the
4438** choice of query plan if the parameter is the left-hand side of a [LIKE]
4439** or [GLOB] operator or if the parameter is compared to an indexed column
4440** and the [SQLITE_ENABLE_STAT4] compile-time option is enabled.
4441** </li>
4442** </ol>
4443**
4444** <p>^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having
4445** the extra prepFlags parameter, which is a bit array consisting of zero or
4446** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags. ^The
4447** sqlite3_prepare_v2() interface works exactly the same as
4448** sqlite3_prepare_v3() with a zero prepFlags parameter.
4449*/
4450SQLITE_API int sqlite3_prepare(
4451 sqlite3 *db, /* Database handle */
4452 const char *zSql, /* SQL statement, UTF-8 encoded */
4453 int nByte, /* Maximum length of zSql in bytes. */
4454 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4455 const char **pzTail /* OUT: Pointer to unused portion of zSql */
4456);
4457SQLITE_API int sqlite3_prepare_v2(
4458 sqlite3 *db, /* Database handle */
4459 const char *zSql, /* SQL statement, UTF-8 encoded */
4460 int nByte, /* Maximum length of zSql in bytes. */
4461 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4462 const char **pzTail /* OUT: Pointer to unused portion of zSql */
4463);
4464SQLITE_API int sqlite3_prepare_v3(
4465 sqlite3 *db, /* Database handle */
4466 const char *zSql, /* SQL statement, UTF-8 encoded */
4467 int nByte, /* Maximum length of zSql in bytes. */
4468 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */
4469 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4470 const char **pzTail /* OUT: Pointer to unused portion of zSql */
4471);
4472SQLITE_API int sqlite3_prepare16(
4473 sqlite3 *db, /* Database handle */
4474 const void *zSql, /* SQL statement, UTF-16 encoded */
4475 int nByte, /* Maximum length of zSql in bytes. */
4476 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4477 const void **pzTail /* OUT: Pointer to unused portion of zSql */
4478);
4479SQLITE_API int sqlite3_prepare16_v2(
4480 sqlite3 *db, /* Database handle */
4481 const void *zSql, /* SQL statement, UTF-16 encoded */
4482 int nByte, /* Maximum length of zSql in bytes. */
4483 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4484 const void **pzTail /* OUT: Pointer to unused portion of zSql */
4485);
4486SQLITE_API int sqlite3_prepare16_v3(
4487 sqlite3 *db, /* Database handle */
4488 const void *zSql, /* SQL statement, UTF-16 encoded */
4489 int nByte, /* Maximum length of zSql in bytes. */
4490 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */
4491 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4492 const void **pzTail /* OUT: Pointer to unused portion of zSql */
4493);
4494
4495/*
4496** CAPI3REF: Retrieving Statement SQL
4497** METHOD: sqlite3_stmt
4498**
4499** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8
4500** SQL text used to create [prepared statement] P if P was
4501** created by [sqlite3_prepare_v2()], [sqlite3_prepare_v3()],
4502** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
4503** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8
4504** string containing the SQL text of prepared statement P with
4505** [bound parameters] expanded.
4506** ^The sqlite3_normalized_sql(P) interface returns a pointer to a UTF-8
4507** string containing the normalized SQL text of prepared statement P. The
4508** semantics used to normalize a SQL statement are unspecified and subject
4509** to change. At a minimum, literal values will be replaced with suitable
4510** placeholders.
4511**
4512** ^(For example, if a prepared statement is created using the SQL
4513** text "SELECT $abc,:xyz" and if parameter $abc is bound to integer 2345
4514** and parameter :xyz is unbound, then sqlite3_sql() will return
4515** the original string, "SELECT $abc,:xyz" but sqlite3_expanded_sql()
4516** will return "SELECT 2345,NULL".)^
4517**
4518** ^The sqlite3_expanded_sql() interface returns NULL if insufficient memory
4519** is available to hold the result, or if the result would exceed the
4520** the maximum string length determined by the [SQLITE_LIMIT_LENGTH].
4521**
4522** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of
4523** bound parameter expansions. ^The [SQLITE_OMIT_TRACE] compile-time
4524** option causes sqlite3_expanded_sql() to always return NULL.
4525**
4526** ^The strings returned by sqlite3_sql(P) and sqlite3_normalized_sql(P)
4527** are managed by SQLite and are automatically freed when the prepared
4528** statement is finalized.
4529** ^The string returned by sqlite3_expanded_sql(P), on the other hand,
4530** is obtained from [sqlite3_malloc()] and must be freed by the application
4531** by passing it to [sqlite3_free()].
4532**
4533** ^The sqlite3_normalized_sql() interface is only available if
4534** the [SQLITE_ENABLE_NORMALIZE] compile-time option is defined.
4535*/
4536SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
4537SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt);
4538#ifdef SQLITE_ENABLE_NORMALIZE
4539SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt);
4540#endif
4541
4542/*
4543** CAPI3REF: Determine If An SQL Statement Writes The Database
4544** METHOD: sqlite3_stmt
4545**
4546** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
4547** and only if the [prepared statement] X makes no direct changes to
4548** the content of the database file.
4549**
4550** Note that [application-defined SQL functions] or
4551** [virtual tables] might change the database indirectly as a side effect.
4552** ^(For example, if an application defines a function "eval()" that
4553** calls [sqlite3_exec()], then the following SQL statement would
4554** change the database file through side-effects:
4555**
4556** <blockquote><pre>
4557** SELECT eval('DELETE FROM t1') FROM t2;
4558** </pre></blockquote>
4559**
4560** But because the [SELECT] statement does not change the database file
4561** directly, sqlite3_stmt_readonly() would still return true.)^
4562**
4563** ^Transaction control statements such as [BEGIN], [COMMIT], [ROLLBACK],
4564** [SAVEPOINT], and [RELEASE] cause sqlite3_stmt_readonly() to return true,
4565** since the statements themselves do not actually modify the database but
4566** rather they control the timing of when other statements modify the
4567** database. ^The [ATTACH] and [DETACH] statements also cause
4568** sqlite3_stmt_readonly() to return true since, while those statements
4569** change the configuration of a database connection, they do not make
4570** changes to the content of the database files on disk.
4571** ^The sqlite3_stmt_readonly() interface returns true for [BEGIN] since
4572** [BEGIN] merely sets internal flags, but the [BEGIN|BEGIN IMMEDIATE] and
4573** [BEGIN|BEGIN EXCLUSIVE] commands do touch the database and so
4574** sqlite3_stmt_readonly() returns false for those commands.
4575**
4576** ^This routine returns false if there is any possibility that the
4577** statement might change the database file. ^A false return does
4578** not guarantee that the statement will change the database file.
4579** ^For example, an UPDATE statement might have a WHERE clause that
4580** makes it a no-op, but the sqlite3_stmt_readonly() result would still
4581** be false. ^Similarly, a CREATE TABLE IF NOT EXISTS statement is a
4582** read-only no-op if the table already exists, but
4583** sqlite3_stmt_readonly() still returns false for such a statement.
4584*/
4585SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
4586
4587/*
4588** CAPI3REF: Query The EXPLAIN Setting For A Prepared Statement
4589** METHOD: sqlite3_stmt
4590**
4591** ^The sqlite3_stmt_isexplain(S) interface returns 1 if the
4592** prepared statement S is an EXPLAIN statement, or 2 if the
4593** statement S is an EXPLAIN QUERY PLAN.
4594** ^The sqlite3_stmt_isexplain(S) interface returns 0 if S is
4595** an ordinary statement or a NULL pointer.
4596*/
4597SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt);
4598
4599/*
4600** CAPI3REF: Determine If A Prepared Statement Has Been Reset
4601** METHOD: sqlite3_stmt
4602**
4603** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
4604** [prepared statement] S has been stepped at least once using
4605** [sqlite3_step(S)] but has neither run to completion (returned
4606** [SQLITE_DONE] from [sqlite3_step(S)]) nor
4607** been reset using [sqlite3_reset(S)]. ^The sqlite3_stmt_busy(S)
4608** interface returns false if S is a NULL pointer. If S is not a
4609** NULL pointer and is not a pointer to a valid [prepared statement]
4610** object, then the behavior is undefined and probably undesirable.
4611**
4612** This interface can be used in combination [sqlite3_next_stmt()]
4613** to locate all prepared statements associated with a database
4614** connection that are in need of being reset. This can be used,
4615** for example, in diagnostic routines to search for prepared
4616** statements that are holding a transaction open.
4617*/
4618SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt*);
4619
4620/*
4621** CAPI3REF: Dynamically Typed Value Object
4622** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
4623**
4624** SQLite uses the sqlite3_value object to represent all values
4625** that can be stored in a database table. SQLite uses dynamic typing
4626** for the values it stores. ^Values stored in sqlite3_value objects
4627** can be integers, floating point values, strings, BLOBs, or NULL.
4628**
4629** An sqlite3_value object may be either "protected" or "unprotected".
4630** Some interfaces require a protected sqlite3_value. Other interfaces
4631** will accept either a protected or an unprotected sqlite3_value.
4632** Every interface that accepts sqlite3_value arguments specifies
4633** whether or not it requires a protected sqlite3_value. The
4634** [sqlite3_value_dup()] interface can be used to construct a new
4635** protected sqlite3_value from an unprotected sqlite3_value.
4636**
4637** The terms "protected" and "unprotected" refer to whether or not
4638** a mutex is held. An internal mutex is held for a protected
4639** sqlite3_value object but no mutex is held for an unprotected
4640** sqlite3_value object. If SQLite is compiled to be single-threaded
4641** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0)
4642** or if SQLite is run in one of reduced mutex modes
4643** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD]
4644** then there is no distinction between protected and unprotected
4645** sqlite3_value objects and they can be used interchangeably. However,
4646** for maximum code portability it is recommended that applications
4647** still make the distinction between protected and unprotected
4648** sqlite3_value objects even when not strictly required.
4649**
4650** ^The sqlite3_value objects that are passed as parameters into the
4651** implementation of [application-defined SQL functions] are protected.
4652** ^The sqlite3_value object returned by
4653** [sqlite3_column_value()] is unprotected.
4654** Unprotected sqlite3_value objects may only be used as arguments
4655** to [sqlite3_result_value()], [sqlite3_bind_value()], and
4656** [sqlite3_value_dup()].
4657** The [sqlite3_value_blob | sqlite3_value_type()] family of
4658** interfaces require protected sqlite3_value objects.
4659*/
4660typedef struct sqlite3_value sqlite3_value;
4661
4662/*
4663** CAPI3REF: SQL Function Context Object
4664**
4665** The context in which an SQL function executes is stored in an
4666** sqlite3_context object. ^A pointer to an sqlite3_context object
4667** is always first parameter to [application-defined SQL functions].
4668** The application-defined SQL function implementation will pass this
4669** pointer through into calls to [sqlite3_result_int | sqlite3_result()],
4670** [sqlite3_aggregate_context()], [sqlite3_user_data()],
4671** [sqlite3_context_db_handle()], [sqlite3_get_auxdata()],
4672** and/or [sqlite3_set_auxdata()].
4673*/
4674typedef struct sqlite3_context sqlite3_context;
4675
4676/*
4677** CAPI3REF: Binding Values To Prepared Statements
4678** KEYWORDS: {host parameter} {host parameters} {host parameter name}
4679** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding}
4680** METHOD: sqlite3_stmt
4681**
4682** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants,
4683** literals may be replaced by a [parameter] that matches one of following
4684** templates:
4685**
4686** <ul>
4687** <li> ?
4688** <li> ?NNN
4689** <li> :VVV
4690** <li> @VVV
4691** <li> $VVV
4692** </ul>
4693**
4694** In the templates above, NNN represents an integer literal,
4695** and VVV represents an alphanumeric identifier.)^ ^The values of these
4696** parameters (also called "host parameter names" or "SQL parameters")
4697** can be set using the sqlite3_bind_*() routines defined here.
4698**
4699** ^The first argument to the sqlite3_bind_*() routines is always
4700** a pointer to the [sqlite3_stmt] object returned from
4701** [sqlite3_prepare_v2()] or its variants.
4702**
4703** ^The second argument is the index of the SQL parameter to be set.
4704** ^The leftmost SQL parameter has an index of 1. ^When the same named
4705** SQL parameter is used more than once, second and subsequent
4706** occurrences have the same index as the first occurrence.
4707** ^The index for named parameters can be looked up using the
4708** [sqlite3_bind_parameter_index()] API if desired. ^The index
4709** for "?NNN" parameters is the value of NNN.
4710** ^The NNN value must be between 1 and the [sqlite3_limit()]
4711** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 32766).
4712**
4713** ^The third argument is the value to bind to the parameter.
4714** ^If the third parameter to sqlite3_bind_text() or sqlite3_bind_text16()
4715** or sqlite3_bind_blob() is a NULL pointer then the fourth parameter
4716** is ignored and the end result is the same as sqlite3_bind_null().
4717** ^If the third parameter to sqlite3_bind_text() is not NULL, then
4718** it should be a pointer to well-formed UTF8 text.
4719** ^If the third parameter to sqlite3_bind_text16() is not NULL, then
4720** it should be a pointer to well-formed UTF16 text.
4721** ^If the third parameter to sqlite3_bind_text64() is not NULL, then
4722** it should be a pointer to a well-formed unicode string that is
4723** either UTF8 if the sixth parameter is SQLITE_UTF8, or UTF16
4724** otherwise.
4725**
4726** [[byte-order determination rules]] ^The byte-order of
4727** UTF16 input text is determined by the byte-order mark (BOM, U+FEFF)
4728** found in first character, which is removed, or in the absence of a BOM
4729** the byte order is the native byte order of the host
4730** machine for sqlite3_bind_text16() or the byte order specified in
4731** the 6th parameter for sqlite3_bind_text64().)^
4732** ^If UTF16 input text contains invalid unicode
4733** characters, then SQLite might change those invalid characters
4734** into the unicode replacement character: U+FFFD.
4735**
4736** ^(In those routines that have a fourth argument, its value is the
4737** number of bytes in the parameter. To be clear: the value is the
4738** number of <u>bytes</u> in the value, not the number of characters.)^
4739** ^If the fourth parameter to sqlite3_bind_text() or sqlite3_bind_text16()
4740** is negative, then the length of the string is
4741** the number of bytes up to the first zero terminator.
4742** If the fourth parameter to sqlite3_bind_blob() is negative, then
4743** the behavior is undefined.
4744** If a non-negative fourth parameter is provided to sqlite3_bind_text()
4745** or sqlite3_bind_text16() or sqlite3_bind_text64() then
4746** that parameter must be the byte offset
4747** where the NUL terminator would occur assuming the string were NUL
4748** terminated. If any NUL characters occurs at byte offsets less than
4749** the value of the fourth parameter then the resulting string value will
4750** contain embedded NULs. The result of expressions involving strings
4751** with embedded NULs is undefined.
4752**
4753** ^The fifth argument to the BLOB and string binding interfaces controls
4754** or indicates the lifetime of the object referenced by the third parameter.
4755** These three options exist:
4756** ^ (1) A destructor to dispose of the BLOB or string after SQLite has finished
4757** with it may be passed. ^It is called to dispose of the BLOB or string even
4758** if the call to the bind API fails, except the destructor is not called if
4759** the third parameter is a NULL pointer or the fourth parameter is negative.
4760** ^ (2) The special constant, [SQLITE_STATIC], may be passsed to indicate that
4761** the application remains responsible for disposing of the object. ^In this
4762** case, the object and the provided pointer to it must remain valid until
4763** either the prepared statement is finalized or the same SQL parameter is
4764** bound to something else, whichever occurs sooner.
4765** ^ (3) The constant, [SQLITE_TRANSIENT], may be passed to indicate that the
4766** object is to be copied prior to the return from sqlite3_bind_*(). ^The
4767** object and pointer to it must remain valid until then. ^SQLite will then
4768** manage the lifetime of its private copy.
4769**
4770** ^The sixth argument to sqlite3_bind_text64() must be one of
4771** [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE]
4772** to specify the encoding of the text in the third parameter. If
4773** the sixth argument to sqlite3_bind_text64() is not one of the
4774** allowed values shown above, or if the text encoding is different
4775** from the encoding specified by the sixth parameter, then the behavior
4776** is undefined.
4777**
4778** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
4779** is filled with zeroes. ^A zeroblob uses a fixed amount of memory
4780** (just an integer to hold its size) while it is being processed.
4781** Zeroblobs are intended to serve as placeholders for BLOBs whose
4782** content is later written using
4783** [sqlite3_blob_open | incremental BLOB I/O] routines.
4784** ^A negative value for the zeroblob results in a zero-length BLOB.
4785**
4786** ^The sqlite3_bind_pointer(S,I,P,T,D) routine causes the I-th parameter in
4787** [prepared statement] S to have an SQL value of NULL, but to also be
4788** associated with the pointer P of type T. ^D is either a NULL pointer or
4789** a pointer to a destructor function for P. ^SQLite will invoke the
4790** destructor D with a single argument of P when it is finished using
4791** P. The T parameter should be a static string, preferably a string
4792** literal. The sqlite3_bind_pointer() routine is part of the
4793** [pointer passing interface] added for SQLite 3.20.0.
4794**
4795** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer
4796** for the [prepared statement] or with a prepared statement for which
4797** [sqlite3_step()] has been called more recently than [sqlite3_reset()],
4798** then the call will return [SQLITE_MISUSE]. If any sqlite3_bind_()
4799** routine is passed a [prepared statement] that has been finalized, the
4800** result is undefined and probably harmful.
4801**
4802** ^Bindings are not cleared by the [sqlite3_reset()] routine.
4803** ^Unbound parameters are interpreted as NULL.
4804**
4805** ^The sqlite3_bind_* routines return [SQLITE_OK] on success or an
4806** [error code] if anything goes wrong.
4807** ^[SQLITE_TOOBIG] might be returned if the size of a string or BLOB
4808** exceeds limits imposed by [sqlite3_limit]([SQLITE_LIMIT_LENGTH]) or
4809** [SQLITE_MAX_LENGTH].
4810** ^[SQLITE_RANGE] is returned if the parameter
4811** index is out of range. ^[SQLITE_NOMEM] is returned if malloc() fails.
4812**
4813** See also: [sqlite3_bind_parameter_count()],
4814** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()].
4815*/
4816SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
4817SQLITE_API int sqlite3_bind_blob64(sqlite3_stmt*, int, const void*, sqlite3_uint64,
4818 void(*)(void*));
4819SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
4820SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
4821SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
4822SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
4823SQLITE_API int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*));
4824SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
4825SQLITE_API int sqlite3_bind_text64(sqlite3_stmt*, int, const char*, sqlite3_uint64,
4826 void(*)(void*), unsigned char encoding);
4827SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
4828SQLITE_API int sqlite3_bind_pointer(sqlite3_stmt*, int, void*, const char*,void(*)(void*));
4829SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
4830SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt*, int, sqlite3_uint64);
4831
4832/*
4833** CAPI3REF: Number Of SQL Parameters
4834** METHOD: sqlite3_stmt
4835**
4836** ^This routine can be used to find the number of [SQL parameters]
4837** in a [prepared statement]. SQL parameters are tokens of the
4838** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as
4839** placeholders for values that are [sqlite3_bind_blob | bound]
4840** to the parameters at a later time.
4841**
4842** ^(This routine actually returns the index of the largest (rightmost)
4843** parameter. For all forms except ?NNN, this will correspond to the
4844** number of unique parameters. If parameters of the ?NNN form are used,
4845** there may be gaps in the list.)^
4846**
4847** See also: [sqlite3_bind_blob|sqlite3_bind()],
4848** [sqlite3_bind_parameter_name()], and
4849** [sqlite3_bind_parameter_index()].
4850*/
4851SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*);
4852
4853/*
4854** CAPI3REF: Name Of A Host Parameter
4855** METHOD: sqlite3_stmt
4856**
4857** ^The sqlite3_bind_parameter_name(P,N) interface returns
4858** the name of the N-th [SQL parameter] in the [prepared statement] P.
4859** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA"
4860** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA"
4861** respectively.
4862** In other words, the initial ":" or "$" or "@" or "?"
4863** is included as part of the name.)^
4864** ^Parameters of the form "?" without a following integer have no name
4865** and are referred to as "nameless" or "anonymous parameters".
4866**
4867** ^The first host parameter has an index of 1, not 0.
4868**
4869** ^If the value N is out of range or if the N-th parameter is
4870** nameless, then NULL is returned. ^The returned string is
4871** always in UTF-8 encoding even if the named parameter was
4872** originally specified as UTF-16 in [sqlite3_prepare16()],
4873** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
4874**
4875** See also: [sqlite3_bind_blob|sqlite3_bind()],
4876** [sqlite3_bind_parameter_count()], and
4877** [sqlite3_bind_parameter_index()].
4878*/
4879SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
4880
4881/*
4882** CAPI3REF: Index Of A Parameter With A Given Name
4883** METHOD: sqlite3_stmt
4884**
4885** ^Return the index of an SQL parameter given its name. ^The
4886** index value returned is suitable for use as the second
4887** parameter to [sqlite3_bind_blob|sqlite3_bind()]. ^A zero
4888** is returned if no matching parameter is found. ^The parameter
4889** name must be given in UTF-8 even if the original statement
4890** was prepared from UTF-16 text using [sqlite3_prepare16_v2()] or
4891** [sqlite3_prepare16_v3()].
4892**
4893** See also: [sqlite3_bind_blob|sqlite3_bind()],
4894** [sqlite3_bind_parameter_count()], and
4895** [sqlite3_bind_parameter_name()].
4896*/
4897SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
4898
4899/*
4900** CAPI3REF: Reset All Bindings On A Prepared Statement
4901** METHOD: sqlite3_stmt
4902**
4903** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset
4904** the [sqlite3_bind_blob | bindings] on a [prepared statement].
4905** ^Use this routine to reset all host parameters to NULL.
4906*/
4907SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*);
4908
4909/*
4910** CAPI3REF: Number Of Columns In A Result Set
4911** METHOD: sqlite3_stmt
4912**
4913** ^Return the number of columns in the result set returned by the
4914** [prepared statement]. ^If this routine returns 0, that means the
4915** [prepared statement] returns no data (for example an [UPDATE]).
4916** ^However, just because this routine returns a positive number does not
4917** mean that one or more rows of data will be returned. ^A SELECT statement
4918** will always have a positive sqlite3_column_count() but depending on the
4919** WHERE clause constraints and the table content, it might return no rows.
4920**
4921** See also: [sqlite3_data_count()]
4922*/
4923SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt);
4924
4925/*
4926** CAPI3REF: Column Names In A Result Set
4927** METHOD: sqlite3_stmt
4928**
4929** ^These routines return the name assigned to a particular column
4930** in the result set of a [SELECT] statement. ^The sqlite3_column_name()
4931** interface returns a pointer to a zero-terminated UTF-8 string
4932** and sqlite3_column_name16() returns a pointer to a zero-terminated
4933** UTF-16 string. ^The first parameter is the [prepared statement]
4934** that implements the [SELECT] statement. ^The second parameter is the
4935** column number. ^The leftmost column is number 0.
4936**
4937** ^The returned string pointer is valid until either the [prepared statement]
4938** is destroyed by [sqlite3_finalize()] or until the statement is automatically
4939** reprepared by the first call to [sqlite3_step()] for a particular run
4940** or until the next call to
4941** sqlite3_column_name() or sqlite3_column_name16() on the same column.
4942**
4943** ^If sqlite3_malloc() fails during the processing of either routine
4944** (for example during a conversion from UTF-8 to UTF-16) then a
4945** NULL pointer is returned.
4946**
4947** ^The name of a result column is the value of the "AS" clause for
4948** that column, if there is an AS clause. If there is no AS clause
4949** then the name of the column is unspecified and may change from
4950** one release of SQLite to the next.
4951*/
4952SQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N);
4953SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N);
4954
4955/*
4956** CAPI3REF: Source Of Data In A Query Result
4957** METHOD: sqlite3_stmt
4958**
4959** ^These routines provide a means to determine the database, table, and
4960** table column that is the origin of a particular result column in
4961** [SELECT] statement.
4962** ^The name of the database or table or column can be returned as
4963** either a UTF-8 or UTF-16 string. ^The _database_ routines return
4964** the database name, the _table_ routines return the table name, and
4965** the origin_ routines return the column name.
4966** ^The returned string is valid until the [prepared statement] is destroyed
4967** using [sqlite3_finalize()] or until the statement is automatically
4968** reprepared by the first call to [sqlite3_step()] for a particular run
4969** or until the same information is requested
4970** again in a different encoding.
4971**
4972** ^The names returned are the original un-aliased names of the
4973** database, table, and column.
4974**
4975** ^The first argument to these interfaces is a [prepared statement].
4976** ^These functions return information about the Nth result column returned by
4977** the statement, where N is the second function argument.
4978** ^The left-most column is column 0 for these routines.
4979**
4980** ^If the Nth column returned by the statement is an expression or
4981** subquery and is not a column value, then all of these functions return
4982** NULL. ^These routines might also return NULL if a memory allocation error
4983** occurs. ^Otherwise, they return the name of the attached database, table,
4984** or column that query result column was extracted from.
4985**
4986** ^As with all other SQLite APIs, those whose names end with "16" return
4987** UTF-16 encoded strings and the other functions return UTF-8.
4988**
4989** ^These APIs are only available if the library was compiled with the
4990** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
4991**
4992** If two or more threads call one or more
4993** [sqlite3_column_database_name | column metadata interfaces]
4994** for the same [prepared statement] and result column
4995** at the same time then the results are undefined.
4996*/
4997SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int);
4998SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int);
4999SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt*,int);
5000SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt*,int);
5001SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
5002SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
5003
5004/*
5005** CAPI3REF: Declared Datatype Of A Query Result
5006** METHOD: sqlite3_stmt
5007**
5008** ^(The first parameter is a [prepared statement].
5009** If this statement is a [SELECT] statement and the Nth column of the
5010** returned result set of that [SELECT] is a table column (not an
5011** expression or subquery) then the declared type of the table
5012** column is returned.)^ ^If the Nth column of the result set is an
5013** expression or subquery, then a NULL pointer is returned.
5014** ^The returned string is always UTF-8 encoded.
5015**
5016** ^(For example, given the database schema:
5017**
5018** CREATE TABLE t1(c1 VARIANT);
5019**
5020** and the following statement to be compiled:
5021**
5022** SELECT c1 + 1, c1 FROM t1;
5023**
5024** this routine would return the string "VARIANT" for the second result
5025** column (i==1), and a NULL pointer for the first result column (i==0).)^
5026**
5027** ^SQLite uses dynamic run-time typing. ^So just because a column
5028** is declared to contain a particular type does not mean that the
5029** data stored in that column is of the declared type. SQLite is
5030** strongly typed, but the typing is dynamic not static. ^Type
5031** is associated with individual values, not with the containers
5032** used to hold those values.
5033*/
5034SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int);
5035SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
5036
5037/*
5038** CAPI3REF: Evaluate An SQL Statement
5039** METHOD: sqlite3_stmt
5040**
5041** After a [prepared statement] has been prepared using any of
5042** [sqlite3_prepare_v2()], [sqlite3_prepare_v3()], [sqlite3_prepare16_v2()],
5043** or [sqlite3_prepare16_v3()] or one of the legacy
5044** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function
5045** must be called one or more times to evaluate the statement.
5046**
5047** The details of the behavior of the sqlite3_step() interface depend
5048** on whether the statement was prepared using the newer "vX" interfaces
5049** [sqlite3_prepare_v3()], [sqlite3_prepare_v2()], [sqlite3_prepare16_v3()],
5050** [sqlite3_prepare16_v2()] or the older legacy
5051** interfaces [sqlite3_prepare()] and [sqlite3_prepare16()]. The use of the
5052** new "vX" interface is recommended for new applications but the legacy
5053** interface will continue to be supported.
5054**
5055** ^In the legacy interface, the return value will be either [SQLITE_BUSY],
5056** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE].
5057** ^With the "v2" interface, any of the other [result codes] or
5058** [extended result codes] might be returned as well.
5059**
5060** ^[SQLITE_BUSY] means that the database engine was unable to acquire the
5061** database locks it needs to do its job. ^If the statement is a [COMMIT]
5062** or occurs outside of an explicit transaction, then you can retry the
5063** statement. If the statement is not a [COMMIT] and occurs within an
5064** explicit transaction then you should rollback the transaction before
5065** continuing.
5066**
5067** ^[SQLITE_DONE] means that the statement has finished executing
5068** successfully. sqlite3_step() should not be called again on this virtual
5069** machine without first calling [sqlite3_reset()] to reset the virtual
5070** machine back to its initial state.
5071**
5072** ^If the SQL statement being executed returns any data, then [SQLITE_ROW]
5073** is returned each time a new row of data is ready for processing by the
5074** caller. The values may be accessed using the [column access functions].
5075** sqlite3_step() is called again to retrieve the next row of data.
5076**
5077** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
5078** violation) has occurred. sqlite3_step() should not be called again on
5079** the VM. More information may be found by calling [sqlite3_errmsg()].
5080** ^With the legacy interface, a more specific error code (for example,
5081** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth)
5082** can be obtained by calling [sqlite3_reset()] on the
5083** [prepared statement]. ^In the "v2" interface,
5084** the more specific error code is returned directly by sqlite3_step().
5085**
5086** [SQLITE_MISUSE] means that the this routine was called inappropriately.
5087** Perhaps it was called on a [prepared statement] that has
5088** already been [sqlite3_finalize | finalized] or on one that had
5089** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could
5090** be the case that the same database connection is being used by two or
5091** more threads at the same moment in time.
5092**
5093** For all versions of SQLite up to and including 3.6.23.1, a call to
5094** [sqlite3_reset()] was required after sqlite3_step() returned anything
5095** other than [SQLITE_ROW] before any subsequent invocation of
5096** sqlite3_step(). Failure to reset the prepared statement using
5097** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
5098** sqlite3_step(). But after [version 3.6.23.1] ([dateof:3.6.23.1],
5099** sqlite3_step() began
5100** calling [sqlite3_reset()] automatically in this circumstance rather
5101** than returning [SQLITE_MISUSE]. This is not considered a compatibility
5102** break because any application that ever receives an SQLITE_MISUSE error
5103** is broken by definition. The [SQLITE_OMIT_AUTORESET] compile-time option
5104** can be used to restore the legacy behavior.
5105**
5106** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
5107** API always returns a generic error code, [SQLITE_ERROR], following any
5108** error other than [SQLITE_BUSY] and [SQLITE_MISUSE]. You must call
5109** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
5110** specific [error codes] that better describes the error.
5111** We admit that this is a goofy design. The problem has been fixed
5112** with the "v2" interface. If you prepare all of your SQL statements
5113** using [sqlite3_prepare_v3()] or [sqlite3_prepare_v2()]
5114** or [sqlite3_prepare16_v2()] or [sqlite3_prepare16_v3()] instead
5115** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces,
5116** then the more specific [error codes] are returned directly
5117** by sqlite3_step(). The use of the "vX" interfaces is recommended.
5118*/
5119SQLITE_API int sqlite3_step(sqlite3_stmt*);
5120
5121/*
5122** CAPI3REF: Number of columns in a result set
5123** METHOD: sqlite3_stmt
5124**
5125** ^The sqlite3_data_count(P) interface returns the number of columns in the
5126** current row of the result set of [prepared statement] P.
5127** ^If prepared statement P does not have results ready to return
5128** (via calls to the [sqlite3_column_int | sqlite3_column()] family of
5129** interfaces) then sqlite3_data_count(P) returns 0.
5130** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
5131** ^The sqlite3_data_count(P) routine returns 0 if the previous call to
5132** [sqlite3_step](P) returned [SQLITE_DONE]. ^The sqlite3_data_count(P)
5133** will return non-zero if previous call to [sqlite3_step](P) returned
5134** [SQLITE_ROW], except in the case of the [PRAGMA incremental_vacuum]
5135** where it always returns zero since each step of that multi-step
5136** pragma returns 0 columns of data.
5137**
5138** See also: [sqlite3_column_count()]
5139*/
5140SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
5141
5142/*
5143** CAPI3REF: Fundamental Datatypes
5144** KEYWORDS: SQLITE_TEXT
5145**
5146** ^(Every value in SQLite has one of five fundamental datatypes:
5147**
5148** <ul>
5149** <li> 64-bit signed integer
5150** <li> 64-bit IEEE floating point number
5151** <li> string
5152** <li> BLOB
5153** <li> NULL
5154** </ul>)^
5155**
5156** These constants are codes for each of those types.
5157**
5158** Note that the SQLITE_TEXT constant was also used in SQLite version 2
5159** for a completely different meaning. Software that links against both
5160** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT, not
5161** SQLITE_TEXT.
5162*/
5163#define SQLITE_INTEGER 1
5164#define SQLITE_FLOAT 2
5165#define SQLITE_BLOB 4
5166#define SQLITE_NULL 5
5167#ifdef SQLITE_TEXT
5168# undef SQLITE_TEXT
5169#else
5170# define SQLITE_TEXT 3
5171#endif
5172#define SQLITE3_TEXT 3
5173
5174/*
5175** CAPI3REF: Result Values From A Query
5176** KEYWORDS: {column access functions}
5177** METHOD: sqlite3_stmt
5178**
5179** <b>Summary:</b>
5180** <blockquote><table border=0 cellpadding=0 cellspacing=0>
5181** <tr><td><b>sqlite3_column_blob</b><td>&rarr;<td>BLOB result
5182** <tr><td><b>sqlite3_column_double</b><td>&rarr;<td>REAL result
5183** <tr><td><b>sqlite3_column_int</b><td>&rarr;<td>32-bit INTEGER result
5184** <tr><td><b>sqlite3_column_int64</b><td>&rarr;<td>64-bit INTEGER result
5185** <tr><td><b>sqlite3_column_text</b><td>&rarr;<td>UTF-8 TEXT result
5186** <tr><td><b>sqlite3_column_text16</b><td>&rarr;<td>UTF-16 TEXT result
5187** <tr><td><b>sqlite3_column_value</b><td>&rarr;<td>The result as an
5188** [sqlite3_value|unprotected sqlite3_value] object.
5189** <tr><td>&nbsp;<td>&nbsp;<td>&nbsp;
5190** <tr><td><b>sqlite3_column_bytes</b><td>&rarr;<td>Size of a BLOB
5191** or a UTF-8 TEXT result in bytes
5192** <tr><td><b>sqlite3_column_bytes16&nbsp;&nbsp;</b>
5193** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
5194** TEXT in bytes
5195** <tr><td><b>sqlite3_column_type</b><td>&rarr;<td>Default
5196** datatype of the result
5197** </table></blockquote>
5198**
5199** <b>Details:</b>
5200**
5201** ^These routines return information about a single column of the current
5202** result row of a query. ^In every case the first argument is a pointer
5203** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
5204** that was returned from [sqlite3_prepare_v2()] or one of its variants)
5205** and the second argument is the index of the column for which information
5206** should be returned. ^The leftmost column of the result set has the index 0.
5207** ^The number of columns in the result can be determined using
5208** [sqlite3_column_count()].
5209**
5210** If the SQL statement does not currently point to a valid row, or if the
5211** column index is out of range, the result is undefined.
5212** These routines may only be called when the most recent call to
5213** [sqlite3_step()] has returned [SQLITE_ROW] and neither
5214** [sqlite3_reset()] nor [sqlite3_finalize()] have been called subsequently.
5215** If any of these routines are called after [sqlite3_reset()] or
5216** [sqlite3_finalize()] or after [sqlite3_step()] has returned
5217** something other than [SQLITE_ROW], the results are undefined.
5218** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()]
5219** are called from a different thread while any of these routines
5220** are pending, then the results are undefined.
5221**
5222** The first six interfaces (_blob, _double, _int, _int64, _text, and _text16)
5223** each return the value of a result column in a specific data format. If
5224** the result column is not initially in the requested format (for example,
5225** if the query returns an integer but the sqlite3_column_text() interface
5226** is used to extract the value) then an automatic type conversion is performed.
5227**
5228** ^The sqlite3_column_type() routine returns the
5229** [SQLITE_INTEGER | datatype code] for the initial data type
5230** of the result column. ^The returned value is one of [SQLITE_INTEGER],
5231** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].
5232** The return value of sqlite3_column_type() can be used to decide which
5233** of the first six interface should be used to extract the column value.
5234** The value returned by sqlite3_column_type() is only meaningful if no
5235** automatic type conversions have occurred for the value in question.
5236** After a type conversion, the result of calling sqlite3_column_type()
5237** is undefined, though harmless. Future
5238** versions of SQLite may change the behavior of sqlite3_column_type()
5239** following a type conversion.
5240**
5241** If the result is a BLOB or a TEXT string, then the sqlite3_column_bytes()
5242** or sqlite3_column_bytes16() interfaces can be used to determine the size
5243** of that BLOB or string.
5244**
5245** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
5246** routine returns the number of bytes in that BLOB or string.
5247** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
5248** the string to UTF-8 and then returns the number of bytes.
5249** ^If the result is a numeric value then sqlite3_column_bytes() uses
5250** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
5251** the number of bytes in that string.
5252** ^If the result is NULL, then sqlite3_column_bytes() returns zero.
5253**
5254** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
5255** routine returns the number of bytes in that BLOB or string.
5256** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
5257** the string to UTF-16 and then returns the number of bytes.
5258** ^If the result is a numeric value then sqlite3_column_bytes16() uses
5259** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
5260** the number of bytes in that string.
5261** ^If the result is NULL, then sqlite3_column_bytes16() returns zero.
5262**
5263** ^The values returned by [sqlite3_column_bytes()] and
5264** [sqlite3_column_bytes16()] do not include the zero terminators at the end
5265** of the string. ^For clarity: the values returned by
5266** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of
5267** bytes in the string, not the number of characters.
5268**
5269** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
5270** even empty strings, are always zero-terminated. ^The return
5271** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
5272**
5273** <b>Warning:</b> ^The object returned by [sqlite3_column_value()] is an
5274** [unprotected sqlite3_value] object. In a multithreaded environment,
5275** an unprotected sqlite3_value object may only be used safely with
5276** [sqlite3_bind_value()] and [sqlite3_result_value()].
5277** If the [unprotected sqlite3_value] object returned by
5278** [sqlite3_column_value()] is used in any other way, including calls
5279** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
5280** or [sqlite3_value_bytes()], the behavior is not threadsafe.
5281** Hence, the sqlite3_column_value() interface
5282** is normally only useful within the implementation of
5283** [application-defined SQL functions] or [virtual tables], not within
5284** top-level application code.
5285**
5286** The these routines may attempt to convert the datatype of the result.
5287** ^For example, if the internal representation is FLOAT and a text result
5288** is requested, [sqlite3_snprintf()] is used internally to perform the
5289** conversion automatically. ^(The following table details the conversions
5290** that are applied:
5291**
5292** <blockquote>
5293** <table border="1">
5294** <tr><th> Internal<br>Type <th> Requested<br>Type <th> Conversion
5295**
5296** <tr><td> NULL <td> INTEGER <td> Result is 0
5297** <tr><td> NULL <td> FLOAT <td> Result is 0.0
5298** <tr><td> NULL <td> TEXT <td> Result is a NULL pointer
5299** <tr><td> NULL <td> BLOB <td> Result is a NULL pointer
5300** <tr><td> INTEGER <td> FLOAT <td> Convert from integer to float
5301** <tr><td> INTEGER <td> TEXT <td> ASCII rendering of the integer
5302** <tr><td> INTEGER <td> BLOB <td> Same as INTEGER->TEXT
5303** <tr><td> FLOAT <td> INTEGER <td> [CAST] to INTEGER
5304** <tr><td> FLOAT <td> TEXT <td> ASCII rendering of the float
5305** <tr><td> FLOAT <td> BLOB <td> [CAST] to BLOB
5306** <tr><td> TEXT <td> INTEGER <td> [CAST] to INTEGER
5307** <tr><td> TEXT <td> FLOAT <td> [CAST] to REAL
5308** <tr><td> TEXT <td> BLOB <td> No change
5309** <tr><td> BLOB <td> INTEGER <td> [CAST] to INTEGER
5310** <tr><td> BLOB <td> FLOAT <td> [CAST] to REAL
5311** <tr><td> BLOB <td> TEXT <td> Add a zero terminator if needed
5312** </table>
5313** </blockquote>)^
5314**
5315** Note that when type conversions occur, pointers returned by prior
5316** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
5317** sqlite3_column_text16() may be invalidated.
5318** Type conversions and pointer invalidations might occur
5319** in the following cases:
5320**
5321** <ul>
5322** <li> The initial content is a BLOB and sqlite3_column_text() or
5323** sqlite3_column_text16() is called. A zero-terminator might
5324** need to be added to the string.</li>
5325** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
5326** sqlite3_column_text16() is called. The content must be converted
5327** to UTF-16.</li>
5328** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
5329** sqlite3_column_text() is called. The content must be converted
5330** to UTF-8.</li>
5331** </ul>
5332**
5333** ^Conversions between UTF-16be and UTF-16le are always done in place and do
5334** not invalidate a prior pointer, though of course the content of the buffer
5335** that the prior pointer references will have been modified. Other kinds
5336** of conversion are done in place when it is possible, but sometimes they
5337** are not possible and in those cases prior pointers are invalidated.
5338**
5339** The safest policy is to invoke these routines
5340** in one of the following ways:
5341**
5342** <ul>
5343** <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
5344** <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
5345** <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li>
5346** </ul>
5347**
5348** In other words, you should call sqlite3_column_text(),
5349** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result
5350** into the desired format, then invoke sqlite3_column_bytes() or
5351** sqlite3_column_bytes16() to find the size of the result. Do not mix calls
5352** to sqlite3_column_text() or sqlite3_column_blob() with calls to
5353** sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16()
5354** with calls to sqlite3_column_bytes().
5355**
5356** ^The pointers returned are valid until a type conversion occurs as
5357** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
5358** [sqlite3_finalize()] is called. ^The memory space used to hold strings
5359** and BLOBs is freed automatically. Do not pass the pointers returned
5360** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
5361** [sqlite3_free()].
5362**
5363** As long as the input parameters are correct, these routines will only
5364** fail if an out-of-memory error occurs during a format conversion.
5365** Only the following subset of interfaces are subject to out-of-memory
5366** errors:
5367**
5368** <ul>
5369** <li> sqlite3_column_blob()
5370** <li> sqlite3_column_text()
5371** <li> sqlite3_column_text16()
5372** <li> sqlite3_column_bytes()
5373** <li> sqlite3_column_bytes16()
5374** </ul>
5375**
5376** If an out-of-memory error occurs, then the return value from these
5377** routines is the same as if the column had contained an SQL NULL value.
5378** Valid SQL NULL returns can be distinguished from out-of-memory errors
5379** by invoking the [sqlite3_errcode()] immediately after the suspect
5380** return value is obtained and before any
5381** other SQLite interface is called on the same [database connection].
5382*/
5383SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
5384SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);
5385SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
5386SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
5387SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
5388SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
5389SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
5390SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
5391SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
5392SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);
5393
5394/*
5395** CAPI3REF: Destroy A Prepared Statement Object
5396** DESTRUCTOR: sqlite3_stmt
5397**
5398** ^The sqlite3_finalize() function is called to delete a [prepared statement].
5399** ^If the most recent evaluation of the statement encountered no errors
5400** or if the statement is never been evaluated, then sqlite3_finalize() returns
5401** SQLITE_OK. ^If the most recent evaluation of statement S failed, then
5402** sqlite3_finalize(S) returns the appropriate [error code] or
5403** [extended error code].
5404**
5405** ^The sqlite3_finalize(S) routine can be called at any point during
5406** the life cycle of [prepared statement] S:
5407** before statement S is ever evaluated, after
5408** one or more calls to [sqlite3_reset()], or after any call
5409** to [sqlite3_step()] regardless of whether or not the statement has
5410** completed execution.
5411**
5412** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
5413**
5414** The application must finalize every [prepared statement] in order to avoid
5415** resource leaks. It is a grievous error for the application to try to use
5416** a prepared statement after it has been finalized. Any use of a prepared
5417** statement after it has been finalized can result in undefined and
5418** undesirable behavior such as segfaults and heap corruption.
5419*/
5420SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
5421
5422/*
5423** CAPI3REF: Reset A Prepared Statement Object
5424** METHOD: sqlite3_stmt
5425**
5426** The sqlite3_reset() function is called to reset a [prepared statement]
5427** object back to its initial state, ready to be re-executed.
5428** ^Any SQL statement variables that had values bound to them using
5429** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values.
5430** Use [sqlite3_clear_bindings()] to reset the bindings.
5431**
5432** ^The [sqlite3_reset(S)] interface resets the [prepared statement] S
5433** back to the beginning of its program.
5434**
5435** ^If the most recent call to [sqlite3_step(S)] for the
5436** [prepared statement] S returned [SQLITE_ROW] or [SQLITE_DONE],
5437** or if [sqlite3_step(S)] has never before been called on S,
5438** then [sqlite3_reset(S)] returns [SQLITE_OK].
5439**
5440** ^If the most recent call to [sqlite3_step(S)] for the
5441** [prepared statement] S indicated an error, then
5442** [sqlite3_reset(S)] returns an appropriate [error code].
5443**
5444** ^The [sqlite3_reset(S)] interface does not change the values
5445** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S.
5446*/
5447SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
5448
5449/*
5450** CAPI3REF: Create Or Redefine SQL Functions
5451** KEYWORDS: {function creation routines}
5452** METHOD: sqlite3
5453**
5454** ^These functions (collectively known as "function creation routines")
5455** are used to add SQL functions or aggregates or to redefine the behavior
5456** of existing SQL functions or aggregates. The only differences between
5457** the three "sqlite3_create_function*" routines are the text encoding
5458** expected for the second parameter (the name of the function being
5459** created) and the presence or absence of a destructor callback for
5460** the application data pointer. Function sqlite3_create_window_function()
5461** is similar, but allows the user to supply the extra callback functions
5462** needed by [aggregate window functions].
5463**
5464** ^The first parameter is the [database connection] to which the SQL
5465** function is to be added. ^If an application uses more than one database
5466** connection then application-defined SQL functions must be added
5467** to each database connection separately.
5468**
5469** ^The second parameter is the name of the SQL function to be created or
5470** redefined. ^The length of the name is limited to 255 bytes in a UTF-8
5471** representation, exclusive of the zero-terminator. ^Note that the name
5472** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.
5473** ^Any attempt to create a function with a longer name
5474** will result in [SQLITE_MISUSE] being returned.
5475**
5476** ^The third parameter (nArg)
5477** is the number of arguments that the SQL function or
5478** aggregate takes. ^If this parameter is -1, then the SQL function or
5479** aggregate may take any number of arguments between 0 and the limit
5480** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]). If the third
5481** parameter is less than -1 or greater than 127 then the behavior is
5482** undefined.
5483**
5484** ^The fourth parameter, eTextRep, specifies what
5485** [SQLITE_UTF8 | text encoding] this SQL function prefers for
5486** its parameters. The application should set this parameter to
5487** [SQLITE_UTF16LE] if the function implementation invokes
5488** [sqlite3_value_text16le()] on an input, or [SQLITE_UTF16BE] if the
5489** implementation invokes [sqlite3_value_text16be()] on an input, or
5490** [SQLITE_UTF16] if [sqlite3_value_text16()] is used, or [SQLITE_UTF8]
5491** otherwise. ^The same SQL function may be registered multiple times using
5492** different preferred text encodings, with different implementations for
5493** each encoding.
5494** ^When multiple implementations of the same function are available, SQLite
5495** will pick the one that involves the least amount of data conversion.
5496**
5497** ^The fourth parameter may optionally be ORed with [SQLITE_DETERMINISTIC]
5498** to signal that the function will always return the same result given
5499** the same inputs within a single SQL statement. Most SQL functions are
5500** deterministic. The built-in [random()] SQL function is an example of a
5501** function that is not deterministic. The SQLite query planner is able to
5502** perform additional optimizations on deterministic functions, so use
5503** of the [SQLITE_DETERMINISTIC] flag is recommended where possible.
5504**
5505** ^The fourth parameter may also optionally include the [SQLITE_DIRECTONLY]
5506** flag, which if present prevents the function from being invoked from
5507** within VIEWs, TRIGGERs, CHECK constraints, generated column expressions,
5508** index expressions, or the WHERE clause of partial indexes.
5509**
5510** For best security, the [SQLITE_DIRECTONLY] flag is recommended for
5511** all application-defined SQL functions that do not need to be
5512** used inside of triggers, view, CHECK constraints, or other elements of
5513** the database schema. This flags is especially recommended for SQL
5514** functions that have side effects or reveal internal application state.
5515** Without this flag, an attacker might be able to modify the schema of
5516** a database file to include invocations of the function with parameters
5517** chosen by the attacker, which the application will then execute when
5518** the database file is opened and read.
5519**
5520** ^(The fifth parameter is an arbitrary pointer. The implementation of the
5521** function can gain access to this pointer using [sqlite3_user_data()].)^
5522**
5523** ^The sixth, seventh and eighth parameters passed to the three
5524** "sqlite3_create_function*" functions, xFunc, xStep and xFinal, are
5525** pointers to C-language functions that implement the SQL function or
5526** aggregate. ^A scalar SQL function requires an implementation of the xFunc
5527** callback only; NULL pointers must be passed as the xStep and xFinal
5528** parameters. ^An aggregate SQL function requires an implementation of xStep
5529** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing
5530** SQL function or aggregate, pass NULL pointers for all three function
5531** callbacks.
5532**
5533** ^The sixth, seventh, eighth and ninth parameters (xStep, xFinal, xValue
5534** and xInverse) passed to sqlite3_create_window_function are pointers to
5535** C-language callbacks that implement the new function. xStep and xFinal
5536** must both be non-NULL. xValue and xInverse may either both be NULL, in
5537** which case a regular aggregate function is created, or must both be
5538** non-NULL, in which case the new function may be used as either an aggregate
5539** or aggregate window function. More details regarding the implementation
5540** of aggregate window functions are
5541** [user-defined window functions|available here].
5542**
5543** ^(If the final parameter to sqlite3_create_function_v2() or
5544** sqlite3_create_window_function() is not NULL, then it is destructor for
5545** the application data pointer. The destructor is invoked when the function
5546** is deleted, either by being overloaded or when the database connection
5547** closes.)^ ^The destructor is also invoked if the call to
5548** sqlite3_create_function_v2() fails. ^When the destructor callback is
5549** invoked, it is passed a single argument which is a copy of the application
5550** data pointer which was the fifth parameter to sqlite3_create_function_v2().
5551**
5552** ^It is permitted to register multiple implementations of the same
5553** functions with the same name but with either differing numbers of
5554** arguments or differing preferred text encodings. ^SQLite will use
5555** the implementation that most closely matches the way in which the
5556** SQL function is used. ^A function implementation with a non-negative
5557** nArg parameter is a better match than a function implementation with
5558** a negative nArg. ^A function where the preferred text encoding
5559** matches the database encoding is a better
5560** match than a function where the encoding is different.
5561** ^A function where the encoding difference is between UTF16le and UTF16be
5562** is a closer match than a function where the encoding difference is
5563** between UTF8 and UTF16.
5564**
5565** ^Built-in functions may be overloaded by new application-defined functions.
5566**
5567** ^An application-defined function is permitted to call other
5568** SQLite interfaces. However, such calls must not
5569** close the database connection nor finalize or reset the prepared
5570** statement in which the function is running.
5571*/
5572SQLITE_API int sqlite3_create_function(
5573 sqlite3 *db,
5574 const char *zFunctionName,
5575 int nArg,
5576 int eTextRep,
5577 void *pApp,
5578 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
5579 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5580 void (*xFinal)(sqlite3_context*)
5581);
5582SQLITE_API int sqlite3_create_function16(
5583 sqlite3 *db,
5584 const void *zFunctionName,
5585 int nArg,
5586 int eTextRep,
5587 void *pApp,
5588 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
5589 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5590 void (*xFinal)(sqlite3_context*)
5591);
5592SQLITE_API int sqlite3_create_function_v2(
5593 sqlite3 *db,
5594 const char *zFunctionName,
5595 int nArg,
5596 int eTextRep,
5597 void *pApp,
5598 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
5599 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5600 void (*xFinal)(sqlite3_context*),
5601 void(*xDestroy)(void*)
5602);
5603SQLITE_API int sqlite3_create_window_function(
5604 sqlite3 *db,
5605 const char *zFunctionName,
5606 int nArg,
5607 int eTextRep,
5608 void *pApp,
5609 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5610 void (*xFinal)(sqlite3_context*),
5611 void (*xValue)(sqlite3_context*),
5612 void (*xInverse)(sqlite3_context*,int,sqlite3_value**),
5613 void(*xDestroy)(void*)
5614);
5615
5616/*
5617** CAPI3REF: Text Encodings
5618**
5619** These constant define integer codes that represent the various
5620** text encodings supported by SQLite.
5621*/
5622#define SQLITE_UTF8 1 /* IMP: R-37514-35566 */
5623#define SQLITE_UTF16LE 2 /* IMP: R-03371-37637 */
5624#define SQLITE_UTF16BE 3 /* IMP: R-51971-34154 */
5625#define SQLITE_UTF16 4 /* Use native byte order */
5626#define SQLITE_ANY 5 /* Deprecated */
5627#define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */
5628
5629/*
5630** CAPI3REF: Function Flags
5631**
5632** These constants may be ORed together with the
5633** [SQLITE_UTF8 | preferred text encoding] as the fourth argument
5634** to [sqlite3_create_function()], [sqlite3_create_function16()], or
5635** [sqlite3_create_function_v2()].
5636**
5637** <dl>
5638** [[SQLITE_DETERMINISTIC]] <dt>SQLITE_DETERMINISTIC</dt><dd>
5639** The SQLITE_DETERMINISTIC flag means that the new function always gives
5640** the same output when the input parameters are the same.
5641** The [abs|abs() function] is deterministic, for example, but
5642** [randomblob|randomblob()] is not. Functions must
5643** be deterministic in order to be used in certain contexts such as
5644** with the WHERE clause of [partial indexes] or in [generated columns].
5645** SQLite might also optimize deterministic functions by factoring them
5646** out of inner loops.
5647** </dd>
5648**
5649** [[SQLITE_DIRECTONLY]] <dt>SQLITE_DIRECTONLY</dt><dd>
5650** The SQLITE_DIRECTONLY flag means that the function may only be invoked
5651** from top-level SQL, and cannot be used in VIEWs or TRIGGERs nor in
5652** schema structures such as [CHECK constraints], [DEFAULT clauses],
5653** [expression indexes], [partial indexes], or [generated columns].
5654** The SQLITE_DIRECTONLY flags is a security feature which is recommended
5655** for all [application-defined SQL functions], and especially for functions
5656** that have side-effects or that could potentially leak sensitive
5657** information.
5658** </dd>
5659**
5660** [[SQLITE_INNOCUOUS]] <dt>SQLITE_INNOCUOUS</dt><dd>
5661** The SQLITE_INNOCUOUS flag means that the function is unlikely
5662** to cause problems even if misused. An innocuous function should have
5663** no side effects and should not depend on any values other than its
5664** input parameters. The [abs|abs() function] is an example of an
5665** innocuous function.
5666** The [load_extension() SQL function] is not innocuous because of its
5667** side effects.
5668** <p> SQLITE_INNOCUOUS is similar to SQLITE_DETERMINISTIC, but is not
5669** exactly the same. The [random|random() function] is an example of a
5670** function that is innocuous but not deterministic.
5671** <p>Some heightened security settings
5672** ([SQLITE_DBCONFIG_TRUSTED_SCHEMA] and [PRAGMA trusted_schema=OFF])
5673** disable the use of SQL functions inside views and triggers and in
5674** schema structures such as [CHECK constraints], [DEFAULT clauses],
5675** [expression indexes], [partial indexes], and [generated columns] unless
5676** the function is tagged with SQLITE_INNOCUOUS. Most built-in functions
5677** are innocuous. Developers are advised to avoid using the
5678** SQLITE_INNOCUOUS flag for application-defined functions unless the
5679** function has been carefully audited and found to be free of potentially
5680** security-adverse side-effects and information-leaks.
5681** </dd>
5682**
5683** [[SQLITE_SUBTYPE]] <dt>SQLITE_SUBTYPE</dt><dd>
5684** The SQLITE_SUBTYPE flag indicates to SQLite that a function may call
5685** [sqlite3_value_subtype()] to inspect the sub-types of its arguments.
5686** Specifying this flag makes no difference for scalar or aggregate user
5687** functions. However, if it is not specified for a user-defined window
5688** function, then any sub-types belonging to arguments passed to the window
5689** function may be discarded before the window function is called (i.e.
5690** sqlite3_value_subtype() will always return 0).
5691** </dd>
5692** </dl>
5693*/
5694#define SQLITE_DETERMINISTIC 0x000000800
5695#define SQLITE_DIRECTONLY 0x000080000
5696#define SQLITE_SUBTYPE 0x000100000
5697#define SQLITE_INNOCUOUS 0x000200000
5698
5699/*
5700** CAPI3REF: Deprecated Functions
5701** DEPRECATED
5702**
5703** These functions are [deprecated]. In order to maintain
5704** backwards compatibility with older code, these functions continue
5705** to be supported. However, new applications should avoid
5706** the use of these functions. To encourage programmers to avoid
5707** these functions, we will not explain what they do.
5708*/
5709#ifndef SQLITE_OMIT_DEPRECATED
5710SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
5711SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
5712SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
5713SQLITE_API SQLITE_DEPRECATED int sqlite3_global_recover(void);
5714SQLITE_API SQLITE_DEPRECATED void sqlite3_thread_cleanup(void);
5715SQLITE_API SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
5716 void*,sqlite3_int64);
5717#endif
5718
5719/*
5720** CAPI3REF: Obtaining SQL Values
5721** METHOD: sqlite3_value
5722**
5723** <b>Summary:</b>
5724** <blockquote><table border=0 cellpadding=0 cellspacing=0>
5725** <tr><td><b>sqlite3_value_blob</b><td>&rarr;<td>BLOB value
5726** <tr><td><b>sqlite3_value_double</b><td>&rarr;<td>REAL value
5727** <tr><td><b>sqlite3_value_int</b><td>&rarr;<td>32-bit INTEGER value
5728** <tr><td><b>sqlite3_value_int64</b><td>&rarr;<td>64-bit INTEGER value
5729** <tr><td><b>sqlite3_value_pointer</b><td>&rarr;<td>Pointer value
5730** <tr><td><b>sqlite3_value_text</b><td>&rarr;<td>UTF-8 TEXT value
5731** <tr><td><b>sqlite3_value_text16</b><td>&rarr;<td>UTF-16 TEXT value in
5732** the native byteorder
5733** <tr><td><b>sqlite3_value_text16be</b><td>&rarr;<td>UTF-16be TEXT value
5734** <tr><td><b>sqlite3_value_text16le</b><td>&rarr;<td>UTF-16le TEXT value
5735** <tr><td>&nbsp;<td>&nbsp;<td>&nbsp;
5736** <tr><td><b>sqlite3_value_bytes</b><td>&rarr;<td>Size of a BLOB
5737** or a UTF-8 TEXT in bytes
5738** <tr><td><b>sqlite3_value_bytes16&nbsp;&nbsp;</b>
5739** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
5740** TEXT in bytes
5741** <tr><td><b>sqlite3_value_type</b><td>&rarr;<td>Default
5742** datatype of the value
5743** <tr><td><b>sqlite3_value_numeric_type&nbsp;&nbsp;</b>
5744** <td>&rarr;&nbsp;&nbsp;<td>Best numeric datatype of the value
5745** <tr><td><b>sqlite3_value_nochange&nbsp;&nbsp;</b>
5746** <td>&rarr;&nbsp;&nbsp;<td>True if the column is unchanged in an UPDATE
5747** against a virtual table.
5748** <tr><td><b>sqlite3_value_frombind&nbsp;&nbsp;</b>
5749** <td>&rarr;&nbsp;&nbsp;<td>True if value originated from a [bound parameter]
5750** </table></blockquote>
5751**
5752** <b>Details:</b>
5753**
5754** These routines extract type, size, and content information from
5755** [protected sqlite3_value] objects. Protected sqlite3_value objects
5756** are used to pass parameter information into the functions that
5757** implement [application-defined SQL functions] and [virtual tables].
5758**
5759** These routines work only with [protected sqlite3_value] objects.
5760** Any attempt to use these routines on an [unprotected sqlite3_value]
5761** is not threadsafe.
5762**
5763** ^These routines work just like the corresponding [column access functions]
5764** except that these routines take a single [protected sqlite3_value] object
5765** pointer instead of a [sqlite3_stmt*] pointer and an integer column number.
5766**
5767** ^The sqlite3_value_text16() interface extracts a UTF-16 string
5768** in the native byte-order of the host machine. ^The
5769** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces
5770** extract UTF-16 strings as big-endian and little-endian respectively.
5771**
5772** ^If [sqlite3_value] object V was initialized
5773** using [sqlite3_bind_pointer(S,I,P,X,D)] or [sqlite3_result_pointer(C,P,X,D)]
5774** and if X and Y are strings that compare equal according to strcmp(X,Y),
5775** then sqlite3_value_pointer(V,Y) will return the pointer P. ^Otherwise,
5776** sqlite3_value_pointer(V,Y) returns a NULL. The sqlite3_bind_pointer()
5777** routine is part of the [pointer passing interface] added for SQLite 3.20.0.
5778**
5779** ^(The sqlite3_value_type(V) interface returns the
5780** [SQLITE_INTEGER | datatype code] for the initial datatype of the
5781** [sqlite3_value] object V. The returned value is one of [SQLITE_INTEGER],
5782** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].)^
5783** Other interfaces might change the datatype for an sqlite3_value object.
5784** For example, if the datatype is initially SQLITE_INTEGER and
5785** sqlite3_value_text(V) is called to extract a text value for that
5786** integer, then subsequent calls to sqlite3_value_type(V) might return
5787** SQLITE_TEXT. Whether or not a persistent internal datatype conversion
5788** occurs is undefined and may change from one release of SQLite to the next.
5789**
5790** ^(The sqlite3_value_numeric_type() interface attempts to apply
5791** numeric affinity to the value. This means that an attempt is
5792** made to convert the value to an integer or floating point. If
5793** such a conversion is possible without loss of information (in other
5794** words, if the value is a string that looks like a number)
5795** then the conversion is performed. Otherwise no conversion occurs.
5796** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
5797**
5798** ^Within the [xUpdate] method of a [virtual table], the
5799** sqlite3_value_nochange(X) interface returns true if and only if
5800** the column corresponding to X is unchanged by the UPDATE operation
5801** that the xUpdate method call was invoked to implement and if
5802** and the prior [xColumn] method call that was invoked to extracted
5803** the value for that column returned without setting a result (probably
5804** because it queried [sqlite3_vtab_nochange()] and found that the column
5805** was unchanging). ^Within an [xUpdate] method, any value for which
5806** sqlite3_value_nochange(X) is true will in all other respects appear
5807** to be a NULL value. If sqlite3_value_nochange(X) is invoked anywhere other
5808** than within an [xUpdate] method call for an UPDATE statement, then
5809** the return value is arbitrary and meaningless.
5810**
5811** ^The sqlite3_value_frombind(X) interface returns non-zero if the
5812** value X originated from one of the [sqlite3_bind_int|sqlite3_bind()]
5813** interfaces. ^If X comes from an SQL literal value, or a table column,
5814** or an expression, then sqlite3_value_frombind(X) returns zero.
5815**
5816** Please pay particular attention to the fact that the pointer returned
5817** from [sqlite3_value_blob()], [sqlite3_value_text()], or
5818** [sqlite3_value_text16()] can be invalidated by a subsequent call to
5819** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
5820** or [sqlite3_value_text16()].
5821**
5822** These routines must be called from the same thread as
5823** the SQL function that supplied the [sqlite3_value*] parameters.
5824**
5825** As long as the input parameter is correct, these routines can only
5826** fail if an out-of-memory error occurs during a format conversion.
5827** Only the following subset of interfaces are subject to out-of-memory
5828** errors:
5829**
5830** <ul>
5831** <li> sqlite3_value_blob()
5832** <li> sqlite3_value_text()
5833** <li> sqlite3_value_text16()
5834** <li> sqlite3_value_text16le()
5835** <li> sqlite3_value_text16be()
5836** <li> sqlite3_value_bytes()
5837** <li> sqlite3_value_bytes16()
5838** </ul>
5839**
5840** If an out-of-memory error occurs, then the return value from these
5841** routines is the same as if the column had contained an SQL NULL value.
5842** Valid SQL NULL returns can be distinguished from out-of-memory errors
5843** by invoking the [sqlite3_errcode()] immediately after the suspect
5844** return value is obtained and before any
5845** other SQLite interface is called on the same [database connection].
5846*/
5847SQLITE_API const void *sqlite3_value_blob(sqlite3_value*);
5848SQLITE_API double sqlite3_value_double(sqlite3_value*);
5849SQLITE_API int sqlite3_value_int(sqlite3_value*);
5850SQLITE_API sqlite3_int64 sqlite3_value_int64(sqlite3_value*);
5851SQLITE_API void *sqlite3_value_pointer(sqlite3_value*, const char*);
5852SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value*);
5853SQLITE_API const void *sqlite3_value_text16(sqlite3_value*);
5854SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
5855SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
5856SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
5857SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
5858SQLITE_API int sqlite3_value_type(sqlite3_value*);
5859SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
5860SQLITE_API int sqlite3_value_nochange(sqlite3_value*);
5861SQLITE_API int sqlite3_value_frombind(sqlite3_value*);
5862
5863/*
5864** CAPI3REF: Finding The Subtype Of SQL Values
5865** METHOD: sqlite3_value
5866**
5867** The sqlite3_value_subtype(V) function returns the subtype for
5868** an [application-defined SQL function] argument V. The subtype
5869** information can be used to pass a limited amount of context from
5870** one SQL function to another. Use the [sqlite3_result_subtype()]
5871** routine to set the subtype for the return value of an SQL function.
5872*/
5873SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value*);
5874
5875/*
5876** CAPI3REF: Copy And Free SQL Values
5877** METHOD: sqlite3_value
5878**
5879** ^The sqlite3_value_dup(V) interface makes a copy of the [sqlite3_value]
5880** object D and returns a pointer to that copy. ^The [sqlite3_value] returned
5881** is a [protected sqlite3_value] object even if the input is not.
5882** ^The sqlite3_value_dup(V) interface returns NULL if V is NULL or if a
5883** memory allocation fails.
5884**
5885** ^The sqlite3_value_free(V) interface frees an [sqlite3_value] object
5886** previously obtained from [sqlite3_value_dup()]. ^If V is a NULL pointer
5887** then sqlite3_value_free(V) is a harmless no-op.
5888*/
5889SQLITE_API sqlite3_value *sqlite3_value_dup(const sqlite3_value*);
5890SQLITE_API void sqlite3_value_free(sqlite3_value*);
5891
5892/*
5893** CAPI3REF: Obtain Aggregate Function Context
5894** METHOD: sqlite3_context
5895**
5896** Implementations of aggregate SQL functions use this
5897** routine to allocate memory for storing their state.
5898**
5899** ^The first time the sqlite3_aggregate_context(C,N) routine is called
5900** for a particular aggregate function, SQLite allocates
5901** N bytes of memory, zeroes out that memory, and returns a pointer
5902** to the new memory. ^On second and subsequent calls to
5903** sqlite3_aggregate_context() for the same aggregate function instance,
5904** the same buffer is returned. Sqlite3_aggregate_context() is normally
5905** called once for each invocation of the xStep callback and then one
5906** last time when the xFinal callback is invoked. ^(When no rows match
5907** an aggregate query, the xStep() callback of the aggregate function
5908** implementation is never called and xFinal() is called exactly once.
5909** In those cases, sqlite3_aggregate_context() might be called for the
5910** first time from within xFinal().)^
5911**
5912** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer
5913** when first called if N is less than or equal to zero or if a memory
5914** allocate error occurs.
5915**
5916** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is
5917** determined by the N parameter on first successful call. Changing the
5918** value of N in any subsequent call to sqlite3_aggregate_context() within
5919** the same aggregate function instance will not resize the memory
5920** allocation.)^ Within the xFinal callback, it is customary to set
5921** N=0 in calls to sqlite3_aggregate_context(C,N) so that no
5922** pointless memory allocations occur.
5923**
5924** ^SQLite automatically frees the memory allocated by
5925** sqlite3_aggregate_context() when the aggregate query concludes.
5926**
5927** The first parameter must be a copy of the
5928** [sqlite3_context | SQL function context] that is the first parameter
5929** to the xStep or xFinal callback routine that implements the aggregate
5930** function.
5931**
5932** This routine must be called from the same thread in which
5933** the aggregate SQL function is running.
5934*/
5935SQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
5936
5937/*
5938** CAPI3REF: User Data For Functions
5939** METHOD: sqlite3_context
5940**
5941** ^The sqlite3_user_data() interface returns a copy of
5942** the pointer that was the pUserData parameter (the 5th parameter)
5943** of the [sqlite3_create_function()]
5944** and [sqlite3_create_function16()] routines that originally
5945** registered the application defined function.
5946**
5947** This routine must be called from the same thread in which
5948** the application-defined function is running.
5949*/
5950SQLITE_API void *sqlite3_user_data(sqlite3_context*);
5951
5952/*
5953** CAPI3REF: Database Connection For Functions
5954** METHOD: sqlite3_context
5955**
5956** ^The sqlite3_context_db_handle() interface returns a copy of
5957** the pointer to the [database connection] (the 1st parameter)
5958** of the [sqlite3_create_function()]
5959** and [sqlite3_create_function16()] routines that originally
5960** registered the application defined function.
5961*/
5962SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
5963
5964/*
5965** CAPI3REF: Function Auxiliary Data
5966** METHOD: sqlite3_context
5967**
5968** These functions may be used by (non-aggregate) SQL functions to
5969** associate metadata with argument values. If the same value is passed to
5970** multiple invocations of the same SQL function during query execution, under
5971** some circumstances the associated metadata may be preserved. An example
5972** of where this might be useful is in a regular-expression matching
5973** function. The compiled version of the regular expression can be stored as
5974** metadata associated with the pattern string.
5975** Then as long as the pattern string remains the same,
5976** the compiled regular expression can be reused on multiple
5977** invocations of the same function.
5978**
5979** ^The sqlite3_get_auxdata(C,N) interface returns a pointer to the metadata
5980** associated by the sqlite3_set_auxdata(C,N,P,X) function with the Nth argument
5981** value to the application-defined function. ^N is zero for the left-most
5982** function argument. ^If there is no metadata
5983** associated with the function argument, the sqlite3_get_auxdata(C,N) interface
5984** returns a NULL pointer.
5985**
5986** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as metadata for the N-th
5987** argument of the application-defined function. ^Subsequent
5988** calls to sqlite3_get_auxdata(C,N) return P from the most recent
5989** sqlite3_set_auxdata(C,N,P,X) call if the metadata is still valid or
5990** NULL if the metadata has been discarded.
5991** ^After each call to sqlite3_set_auxdata(C,N,P,X) where X is not NULL,
5992** SQLite will invoke the destructor function X with parameter P exactly
5993** once, when the metadata is discarded.
5994** SQLite is free to discard the metadata at any time, including: <ul>
5995** <li> ^(when the corresponding function parameter changes)^, or
5996** <li> ^(when [sqlite3_reset()] or [sqlite3_finalize()] is called for the
5997** SQL statement)^, or
5998** <li> ^(when sqlite3_set_auxdata() is invoked again on the same
5999** parameter)^, or
6000** <li> ^(during the original sqlite3_set_auxdata() call when a memory
6001** allocation error occurs.)^ </ul>
6002**
6003** Note the last bullet in particular. The destructor X in
6004** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the
6005** sqlite3_set_auxdata() interface even returns. Hence sqlite3_set_auxdata()
6006** should be called near the end of the function implementation and the
6007** function implementation should not make any use of P after
6008** sqlite3_set_auxdata() has been called.
6009**
6010** ^(In practice, metadata is preserved between function calls for
6011** function parameters that are compile-time constants, including literal
6012** values and [parameters] and expressions composed from the same.)^
6013**
6014** The value of the N parameter to these interfaces should be non-negative.
6015** Future enhancements may make use of negative N values to define new
6016** kinds of function caching behavior.
6017**
6018** These routines must be called from the same thread in which
6019** the SQL function is running.
6020*/
6021SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N);
6022SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
6023
6024
6025/*
6026** CAPI3REF: Constants Defining Special Destructor Behavior
6027**
6028** These are special values for the destructor that is passed in as the
6029** final argument to routines like [sqlite3_result_blob()]. ^If the destructor
6030** argument is SQLITE_STATIC, it means that the content pointer is constant
6031** and will never change. It does not need to be destroyed. ^The
6032** SQLITE_TRANSIENT value means that the content will likely change in
6033** the near future and that SQLite should make its own private copy of
6034** the content before returning.
6035**
6036** The typedef is necessary to work around problems in certain
6037** C++ compilers.
6038*/
6039typedef void (*sqlite3_destructor_type)(void*);
6040#define SQLITE_STATIC ((sqlite3_destructor_type)0)
6041#define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1)
6042
6043/*
6044** CAPI3REF: Setting The Result Of An SQL Function
6045** METHOD: sqlite3_context
6046**
6047** These routines are used by the xFunc or xFinal callbacks that
6048** implement SQL functions and aggregates. See
6049** [sqlite3_create_function()] and [sqlite3_create_function16()]
6050** for additional information.
6051**
6052** These functions work very much like the [parameter binding] family of
6053** functions used to bind values to host parameters in prepared statements.
6054** Refer to the [SQL parameter] documentation for additional information.
6055**
6056** ^The sqlite3_result_blob() interface sets the result from
6057** an application-defined function to be the BLOB whose content is pointed
6058** to by the second parameter and which is N bytes long where N is the
6059** third parameter.
6060**
6061** ^The sqlite3_result_zeroblob(C,N) and sqlite3_result_zeroblob64(C,N)
6062** interfaces set the result of the application-defined function to be
6063** a BLOB containing all zero bytes and N bytes in size.
6064**
6065** ^The sqlite3_result_double() interface sets the result from
6066** an application-defined function to be a floating point value specified
6067** by its 2nd argument.
6068**
6069** ^The sqlite3_result_error() and sqlite3_result_error16() functions
6070** cause the implemented SQL function to throw an exception.
6071** ^SQLite uses the string pointed to by the
6072** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16()
6073** as the text of an error message. ^SQLite interprets the error
6074** message string from sqlite3_result_error() as UTF-8. ^SQLite
6075** interprets the string from sqlite3_result_error16() as UTF-16 using
6076** the same [byte-order determination rules] as [sqlite3_bind_text16()].
6077** ^If the third parameter to sqlite3_result_error()
6078** or sqlite3_result_error16() is negative then SQLite takes as the error
6079** message all text up through the first zero character.
6080** ^If the third parameter to sqlite3_result_error() or
6081** sqlite3_result_error16() is non-negative then SQLite takes that many
6082** bytes (not characters) from the 2nd parameter as the error message.
6083** ^The sqlite3_result_error() and sqlite3_result_error16()
6084** routines make a private copy of the error message text before
6085** they return. Hence, the calling function can deallocate or
6086** modify the text after they return without harm.
6087** ^The sqlite3_result_error_code() function changes the error code
6088** returned by SQLite as a result of an error in a function. ^By default,
6089** the error code is SQLITE_ERROR. ^A subsequent call to sqlite3_result_error()
6090** or sqlite3_result_error16() resets the error code to SQLITE_ERROR.
6091**
6092** ^The sqlite3_result_error_toobig() interface causes SQLite to throw an
6093** error indicating that a string or BLOB is too long to represent.
6094**
6095** ^The sqlite3_result_error_nomem() interface causes SQLite to throw an
6096** error indicating that a memory allocation failed.
6097**
6098** ^The sqlite3_result_int() interface sets the return value
6099** of the application-defined function to be the 32-bit signed integer
6100** value given in the 2nd argument.
6101** ^The sqlite3_result_int64() interface sets the return value
6102** of the application-defined function to be the 64-bit signed integer
6103** value given in the 2nd argument.
6104**
6105** ^The sqlite3_result_null() interface sets the return value
6106** of the application-defined function to be NULL.
6107**
6108** ^The sqlite3_result_text(), sqlite3_result_text16(),
6109** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
6110** set the return value of the application-defined function to be
6111** a text string which is represented as UTF-8, UTF-16 native byte order,
6112** UTF-16 little endian, or UTF-16 big endian, respectively.
6113** ^The sqlite3_result_text64() interface sets the return value of an
6114** application-defined function to be a text string in an encoding
6115** specified by the fifth (and last) parameter, which must be one
6116** of [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE].
6117** ^SQLite takes the text result from the application from
6118** the 2nd parameter of the sqlite3_result_text* interfaces.
6119** ^If the 3rd parameter to the sqlite3_result_text* interfaces
6120** is negative, then SQLite takes result text from the 2nd parameter
6121** through the first zero character.
6122** ^If the 3rd parameter to the sqlite3_result_text* interfaces
6123** is non-negative, then as many bytes (not characters) of the text
6124** pointed to by the 2nd parameter are taken as the application-defined
6125** function result. If the 3rd parameter is non-negative, then it
6126** must be the byte offset into the string where the NUL terminator would
6127** appear if the string where NUL terminated. If any NUL characters occur
6128** in the string at a byte offset that is less than the value of the 3rd
6129** parameter, then the resulting string will contain embedded NULs and the
6130** result of expressions operating on strings with embedded NULs is undefined.
6131** ^If the 4th parameter to the sqlite3_result_text* interfaces
6132** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
6133** function as the destructor on the text or BLOB result when it has
6134** finished using that result.
6135** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
6136** sqlite3_result_blob is the special constant SQLITE_STATIC, then SQLite
6137** assumes that the text or BLOB result is in constant space and does not
6138** copy the content of the parameter nor call a destructor on the content
6139** when it has finished using that result.
6140** ^If the 4th parameter to the sqlite3_result_text* interfaces
6141** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
6142** then SQLite makes a copy of the result into space obtained
6143** from [sqlite3_malloc()] before it returns.
6144**
6145** ^For the sqlite3_result_text16(), sqlite3_result_text16le(), and
6146** sqlite3_result_text16be() routines, and for sqlite3_result_text64()
6147** when the encoding is not UTF8, if the input UTF16 begins with a
6148** byte-order mark (BOM, U+FEFF) then the BOM is removed from the
6149** string and the rest of the string is interpreted according to the
6150** byte-order specified by the BOM. ^The byte-order specified by
6151** the BOM at the beginning of the text overrides the byte-order
6152** specified by the interface procedure. ^So, for example, if
6153** sqlite3_result_text16le() is invoked with text that begins
6154** with bytes 0xfe, 0xff (a big-endian byte-order mark) then the
6155** first two bytes of input are skipped and the remaining input
6156** is interpreted as UTF16BE text.
6157**
6158** ^For UTF16 input text to the sqlite3_result_text16(),
6159** sqlite3_result_text16be(), sqlite3_result_text16le(), and
6160** sqlite3_result_text64() routines, if the text contains invalid
6161** UTF16 characters, the invalid characters might be converted
6162** into the unicode replacement character, U+FFFD.
6163**
6164** ^The sqlite3_result_value() interface sets the result of
6165** the application-defined function to be a copy of the
6166** [unprotected sqlite3_value] object specified by the 2nd parameter. ^The
6167** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
6168** so that the [sqlite3_value] specified in the parameter may change or
6169** be deallocated after sqlite3_result_value() returns without harm.
6170** ^A [protected sqlite3_value] object may always be used where an
6171** [unprotected sqlite3_value] object is required, so either
6172** kind of [sqlite3_value] object can be used with this interface.
6173**
6174** ^The sqlite3_result_pointer(C,P,T,D) interface sets the result to an
6175** SQL NULL value, just like [sqlite3_result_null(C)], except that it
6176** also associates the host-language pointer P or type T with that
6177** NULL value such that the pointer can be retrieved within an
6178** [application-defined SQL function] using [sqlite3_value_pointer()].
6179** ^If the D parameter is not NULL, then it is a pointer to a destructor
6180** for the P parameter. ^SQLite invokes D with P as its only argument
6181** when SQLite is finished with P. The T parameter should be a static
6182** string and preferably a string literal. The sqlite3_result_pointer()
6183** routine is part of the [pointer passing interface] added for SQLite 3.20.0.
6184**
6185** If these routines are called from within the different thread
6186** than the one containing the application-defined function that received
6187** the [sqlite3_context] pointer, the results are undefined.
6188*/
6189SQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
6190SQLITE_API void sqlite3_result_blob64(sqlite3_context*,const void*,
6191 sqlite3_uint64,void(*)(void*));
6192SQLITE_API void sqlite3_result_double(sqlite3_context*, double);
6193SQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int);
6194SQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int);
6195SQLITE_API void sqlite3_result_error_toobig(sqlite3_context*);
6196SQLITE_API void sqlite3_result_error_nomem(sqlite3_context*);
6197SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
6198SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
6199SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
6200SQLITE_API void sqlite3_result_null(sqlite3_context*);
6201SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
6202SQLITE_API void sqlite3_result_text64(sqlite3_context*, const char*,sqlite3_uint64,
6203 void(*)(void*), unsigned char encoding);
6204SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
6205SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
6206SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
6207SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
6208SQLITE_API void sqlite3_result_pointer(sqlite3_context*, void*,const char*,void(*)(void*));
6209SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n);
6210SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context*, sqlite3_uint64 n);
6211
6212
6213/*
6214** CAPI3REF: Setting The Subtype Of An SQL Function
6215** METHOD: sqlite3_context
6216**
6217** The sqlite3_result_subtype(C,T) function causes the subtype of
6218** the result from the [application-defined SQL function] with
6219** [sqlite3_context] C to be the value T. Only the lower 8 bits
6220** of the subtype T are preserved in current versions of SQLite;
6221** higher order bits are discarded.
6222** The number of subtype bytes preserved by SQLite might increase
6223** in future releases of SQLite.
6224*/
6225SQLITE_API void sqlite3_result_subtype(sqlite3_context*,unsigned int);
6226
6227/*
6228** CAPI3REF: Define New Collating Sequences
6229** METHOD: sqlite3
6230**
6231** ^These functions add, remove, or modify a [collation] associated
6232** with the [database connection] specified as the first argument.
6233**
6234** ^The name of the collation is a UTF-8 string
6235** for sqlite3_create_collation() and sqlite3_create_collation_v2()
6236** and a UTF-16 string in native byte order for sqlite3_create_collation16().
6237** ^Collation names that compare equal according to [sqlite3_strnicmp()] are
6238** considered to be the same name.
6239**
6240** ^(The third argument (eTextRep) must be one of the constants:
6241** <ul>
6242** <li> [SQLITE_UTF8],
6243** <li> [SQLITE_UTF16LE],
6244** <li> [SQLITE_UTF16BE],
6245** <li> [SQLITE_UTF16], or
6246** <li> [SQLITE_UTF16_ALIGNED].
6247** </ul>)^
6248** ^The eTextRep argument determines the encoding of strings passed
6249** to the collating function callback, xCompare.
6250** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep
6251** force strings to be UTF16 with native byte order.
6252** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin
6253** on an even byte address.
6254**
6255** ^The fourth argument, pArg, is an application data pointer that is passed
6256** through as the first argument to the collating function callback.
6257**
6258** ^The fifth argument, xCompare, is a pointer to the collating function.
6259** ^Multiple collating functions can be registered using the same name but
6260** with different eTextRep parameters and SQLite will use whichever
6261** function requires the least amount of data transformation.
6262** ^If the xCompare argument is NULL then the collating function is
6263** deleted. ^When all collating functions having the same name are deleted,
6264** that collation is no longer usable.
6265**
6266** ^The collating function callback is invoked with a copy of the pArg
6267** application data pointer and with two strings in the encoding specified
6268** by the eTextRep argument. The two integer parameters to the collating
6269** function callback are the length of the two strings, in bytes. The collating
6270** function must return an integer that is negative, zero, or positive
6271** if the first string is less than, equal to, or greater than the second,
6272** respectively. A collating function must always return the same answer
6273** given the same inputs. If two or more collating functions are registered
6274** to the same collation name (using different eTextRep values) then all
6275** must give an equivalent answer when invoked with equivalent strings.
6276** The collating function must obey the following properties for all
6277** strings A, B, and C:
6278**
6279** <ol>
6280** <li> If A==B then B==A.
6281** <li> If A==B and B==C then A==C.
6282** <li> If A&lt;B THEN B&gt;A.
6283** <li> If A&lt;B and B&lt;C then A&lt;C.
6284** </ol>
6285**
6286** If a collating function fails any of the above constraints and that
6287** collating function is registered and used, then the behavior of SQLite
6288** is undefined.
6289**
6290** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation()
6291** with the addition that the xDestroy callback is invoked on pArg when
6292** the collating function is deleted.
6293** ^Collating functions are deleted when they are overridden by later
6294** calls to the collation creation functions or when the
6295** [database connection] is closed using [sqlite3_close()].
6296**
6297** ^The xDestroy callback is <u>not</u> called if the
6298** sqlite3_create_collation_v2() function fails. Applications that invoke
6299** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should
6300** check the return code and dispose of the application data pointer
6301** themselves rather than expecting SQLite to deal with it for them.
6302** This is different from every other SQLite interface. The inconsistency
6303** is unfortunate but cannot be changed without breaking backwards
6304** compatibility.
6305**
6306** See also: [sqlite3_collation_needed()] and [sqlite3_collation_needed16()].
6307*/
6308SQLITE_API int sqlite3_create_collation(
6309 sqlite3*,
6310 const char *zName,
6311 int eTextRep,
6312 void *pArg,
6313 int(*xCompare)(void*,int,const void*,int,const void*)
6314);
6315SQLITE_API int sqlite3_create_collation_v2(
6316 sqlite3*,
6317 const char *zName,
6318 int eTextRep,
6319 void *pArg,
6320 int(*xCompare)(void*,int,const void*,int,const void*),
6321 void(*xDestroy)(void*)
6322);
6323SQLITE_API int sqlite3_create_collation16(
6324 sqlite3*,
6325 const void *zName,
6326 int eTextRep,
6327 void *pArg,
6328 int(*xCompare)(void*,int,const void*,int,const void*)
6329);
6330
6331/*
6332** CAPI3REF: Collation Needed Callbacks
6333** METHOD: sqlite3
6334**
6335** ^To avoid having to register all collation sequences before a database
6336** can be used, a single callback function may be registered with the
6337** [database connection] to be invoked whenever an undefined collation
6338** sequence is required.
6339**
6340** ^If the function is registered using the sqlite3_collation_needed() API,
6341** then it is passed the names of undefined collation sequences as strings
6342** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
6343** the names are passed as UTF-16 in machine native byte order.
6344** ^A call to either function replaces the existing collation-needed callback.
6345**
6346** ^(When the callback is invoked, the first argument passed is a copy
6347** of the second argument to sqlite3_collation_needed() or
6348** sqlite3_collation_needed16(). The second argument is the database
6349** connection. The third argument is one of [SQLITE_UTF8], [SQLITE_UTF16BE],
6350** or [SQLITE_UTF16LE], indicating the most desirable form of the collation
6351** sequence function required. The fourth parameter is the name of the
6352** required collation sequence.)^
6353**
6354** The callback function should register the desired collation using
6355** [sqlite3_create_collation()], [sqlite3_create_collation16()], or
6356** [sqlite3_create_collation_v2()].
6357*/
6358SQLITE_API int sqlite3_collation_needed(
6359 sqlite3*,
6360 void*,
6361 void(*)(void*,sqlite3*,int eTextRep,const char*)
6362);
6363SQLITE_API int sqlite3_collation_needed16(
6364 sqlite3*,
6365 void*,
6366 void(*)(void*,sqlite3*,int eTextRep,const void*)
6367);
6368
6369#ifdef SQLITE_ENABLE_CEROD
6370/*
6371** Specify the activation key for a CEROD database. Unless
6372** activated, none of the CEROD routines will work.
6373*/
6374SQLITE_API void sqlite3_activate_cerod(
6375 const char *zPassPhrase /* Activation phrase */
6376);
6377#endif
6378
6379/*
6380** CAPI3REF: Suspend Execution For A Short Time
6381**
6382** The sqlite3_sleep() function causes the current thread to suspend execution
6383** for at least a number of milliseconds specified in its parameter.
6384**
6385** If the operating system does not support sleep requests with
6386** millisecond time resolution, then the time will be rounded up to
6387** the nearest second. The number of milliseconds of sleep actually
6388** requested from the operating system is returned.
6389**
6390** ^SQLite implements this interface by calling the xSleep()
6391** method of the default [sqlite3_vfs] object. If the xSleep() method
6392** of the default VFS is not implemented correctly, or not implemented at
6393** all, then the behavior of sqlite3_sleep() may deviate from the description
6394** in the previous paragraphs.
6395*/
6396SQLITE_API int sqlite3_sleep(int);
6397
6398/*
6399** CAPI3REF: Name Of The Folder Holding Temporary Files
6400**
6401** ^(If this global variable is made to point to a string which is
6402** the name of a folder (a.k.a. directory), then all temporary files
6403** created by SQLite when using a built-in [sqlite3_vfs | VFS]
6404** will be placed in that directory.)^ ^If this variable
6405** is a NULL pointer, then SQLite performs a search for an appropriate
6406** temporary file directory.
6407**
6408** Applications are strongly discouraged from using this global variable.
6409** It is required to set a temporary folder on Windows Runtime (WinRT).
6410** But for all other platforms, it is highly recommended that applications
6411** neither read nor write this variable. This global variable is a relic
6412** that exists for backwards compatibility of legacy applications and should
6413** be avoided in new projects.
6414**
6415** It is not safe to read or modify this variable in more than one
6416** thread at a time. It is not safe to read or modify this variable
6417** if a [database connection] is being used at the same time in a separate
6418** thread.
6419** It is intended that this variable be set once
6420** as part of process initialization and before any SQLite interface
6421** routines have been called and that this variable remain unchanged
6422** thereafter.
6423**
6424** ^The [temp_store_directory pragma] may modify this variable and cause
6425** it to point to memory obtained from [sqlite3_malloc]. ^Furthermore,
6426** the [temp_store_directory pragma] always assumes that any string
6427** that this variable points to is held in memory obtained from
6428** [sqlite3_malloc] and the pragma may attempt to free that memory
6429** using [sqlite3_free].
6430** Hence, if this variable is modified directly, either it should be
6431** made NULL or made to point to memory obtained from [sqlite3_malloc]
6432** or else the use of the [temp_store_directory pragma] should be avoided.
6433** Except when requested by the [temp_store_directory pragma], SQLite
6434** does not free the memory that sqlite3_temp_directory points to. If
6435** the application wants that memory to be freed, it must do
6436** so itself, taking care to only do so after all [database connection]
6437** objects have been destroyed.
6438**
6439** <b>Note to Windows Runtime users:</b> The temporary directory must be set
6440** prior to calling [sqlite3_open] or [sqlite3_open_v2]. Otherwise, various
6441** features that require the use of temporary files may fail. Here is an
6442** example of how to do this using C++ with the Windows Runtime:
6443**
6444** <blockquote><pre>
6445** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
6446** &nbsp; TemporaryFolder->Path->Data();
6447** char zPathBuf&#91;MAX_PATH + 1&#93;;
6448** memset(zPathBuf, 0, sizeof(zPathBuf));
6449** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
6450** &nbsp; NULL, NULL);
6451** sqlite3_temp_directory = sqlite3_mprintf("%s", zPathBuf);
6452** </pre></blockquote>
6453*/
6454SQLITE_API char *sqlite3_temp_directory;
6455
6456/*
6457** CAPI3REF: Name Of The Folder Holding Database Files
6458**
6459** ^(If this global variable is made to point to a string which is
6460** the name of a folder (a.k.a. directory), then all database files
6461** specified with a relative pathname and created or accessed by
6462** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
6463** to be relative to that directory.)^ ^If this variable is a NULL
6464** pointer, then SQLite assumes that all database files specified
6465** with a relative pathname are relative to the current directory
6466** for the process. Only the windows VFS makes use of this global
6467** variable; it is ignored by the unix VFS.
6468**
6469** Changing the value of this variable while a database connection is
6470** open can result in a corrupt database.
6471**
6472** It is not safe to read or modify this variable in more than one
6473** thread at a time. It is not safe to read or modify this variable
6474** if a [database connection] is being used at the same time in a separate
6475** thread.
6476** It is intended that this variable be set once
6477** as part of process initialization and before any SQLite interface
6478** routines have been called and that this variable remain unchanged
6479** thereafter.
6480**
6481** ^The [data_store_directory pragma] may modify this variable and cause
6482** it to point to memory obtained from [sqlite3_malloc]. ^Furthermore,
6483** the [data_store_directory pragma] always assumes that any string
6484** that this variable points to is held in memory obtained from
6485** [sqlite3_malloc] and the pragma may attempt to free that memory
6486** using [sqlite3_free].
6487** Hence, if this variable is modified directly, either it should be
6488** made NULL or made to point to memory obtained from [sqlite3_malloc]
6489** or else the use of the [data_store_directory pragma] should be avoided.
6490*/
6491SQLITE_API char *sqlite3_data_directory;
6492
6493/*
6494** CAPI3REF: Win32 Specific Interface
6495**
6496** These interfaces are available only on Windows. The
6497** [sqlite3_win32_set_directory] interface is used to set the value associated
6498** with the [sqlite3_temp_directory] or [sqlite3_data_directory] variable, to
6499** zValue, depending on the value of the type parameter. The zValue parameter
6500** should be NULL to cause the previous value to be freed via [sqlite3_free];
6501** a non-NULL value will be copied into memory obtained from [sqlite3_malloc]
6502** prior to being used. The [sqlite3_win32_set_directory] interface returns
6503** [SQLITE_OK] to indicate success, [SQLITE_ERROR] if the type is unsupported,
6504** or [SQLITE_NOMEM] if memory could not be allocated. The value of the
6505** [sqlite3_data_directory] variable is intended to act as a replacement for
6506** the current directory on the sub-platforms of Win32 where that concept is
6507** not present, e.g. WinRT and UWP. The [sqlite3_win32_set_directory8] and
6508** [sqlite3_win32_set_directory16] interfaces behave exactly the same as the
6509** sqlite3_win32_set_directory interface except the string parameter must be
6510** UTF-8 or UTF-16, respectively.
6511*/
6512SQLITE_API int sqlite3_win32_set_directory(
6513 unsigned long type, /* Identifier for directory being set or reset */
6514 void *zValue /* New value for directory being set or reset */
6515);
6516SQLITE_API int sqlite3_win32_set_directory8(unsigned long type, const char *zValue);
6517SQLITE_API int sqlite3_win32_set_directory16(unsigned long type, const void *zValue);
6518
6519/*
6520** CAPI3REF: Win32 Directory Types
6521**
6522** These macros are only available on Windows. They define the allowed values
6523** for the type argument to the [sqlite3_win32_set_directory] interface.
6524*/
6525#define SQLITE_WIN32_DATA_DIRECTORY_TYPE 1
6526#define SQLITE_WIN32_TEMP_DIRECTORY_TYPE 2
6527
6528/*
6529** CAPI3REF: Test For Auto-Commit Mode
6530** KEYWORDS: {autocommit mode}
6531** METHOD: sqlite3
6532**
6533** ^The sqlite3_get_autocommit() interface returns non-zero or
6534** zero if the given database connection is or is not in autocommit mode,
6535** respectively. ^Autocommit mode is on by default.
6536** ^Autocommit mode is disabled by a [BEGIN] statement.
6537** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
6538**
6539** If certain kinds of errors occur on a statement within a multi-statement
6540** transaction (errors including [SQLITE_FULL], [SQLITE_IOERR],
6541** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the
6542** transaction might be rolled back automatically. The only way to
6543** find out whether SQLite automatically rolled back the transaction after
6544** an error is to use this function.
6545**
6546** If another thread changes the autocommit status of the database
6547** connection while this routine is running, then the return value
6548** is undefined.
6549*/
6550SQLITE_API int sqlite3_get_autocommit(sqlite3*);
6551
6552/*
6553** CAPI3REF: Find The Database Handle Of A Prepared Statement
6554** METHOD: sqlite3_stmt
6555**
6556** ^The sqlite3_db_handle interface returns the [database connection] handle
6557** to which a [prepared statement] belongs. ^The [database connection]
6558** returned by sqlite3_db_handle is the same [database connection]
6559** that was the first argument
6560** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
6561** create the statement in the first place.
6562*/
6563SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
6564
6565/*
6566** CAPI3REF: Return The Filename For A Database Connection
6567** METHOD: sqlite3
6568**
6569** ^The sqlite3_db_filename(D,N) interface returns a pointer to the filename
6570** associated with database N of connection D.
6571** ^If there is no attached database N on the database
6572** connection D, or if database N is a temporary or in-memory database, then
6573** this function will return either a NULL pointer or an empty string.
6574**
6575** ^The string value returned by this routine is owned and managed by
6576** the database connection. ^The value will be valid until the database N
6577** is [DETACH]-ed or until the database connection closes.
6578**
6579** ^The filename returned by this function is the output of the
6580** xFullPathname method of the [VFS]. ^In other words, the filename
6581** will be an absolute pathname, even if the filename used
6582** to open the database originally was a URI or relative pathname.
6583**
6584** If the filename pointer returned by this routine is not NULL, then it
6585** can be used as the filename input parameter to these routines:
6586** <ul>
6587** <li> [sqlite3_uri_parameter()]
6588** <li> [sqlite3_uri_boolean()]
6589** <li> [sqlite3_uri_int64()]
6590** <li> [sqlite3_filename_database()]
6591** <li> [sqlite3_filename_journal()]
6592** <li> [sqlite3_filename_wal()]
6593** </ul>
6594*/
6595SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
6596
6597/*
6598** CAPI3REF: Determine if a database is read-only
6599** METHOD: sqlite3
6600**
6601** ^The sqlite3_db_readonly(D,N) interface returns 1 if the database N
6602** of connection D is read-only, 0 if it is read/write, or -1 if N is not
6603** the name of a database on connection D.
6604*/
6605SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName);
6606
6607/*
6608** CAPI3REF: Determine the transaction state of a database
6609** METHOD: sqlite3
6610**
6611** ^The sqlite3_txn_state(D,S) interface returns the current
6612** [transaction state] of schema S in database connection D. ^If S is NULL,
6613** then the highest transaction state of any schema on database connection D
6614** is returned. Transaction states are (in order of lowest to highest):
6615** <ol>
6616** <li value="0"> SQLITE_TXN_NONE
6617** <li value="1"> SQLITE_TXN_READ
6618** <li value="2"> SQLITE_TXN_WRITE
6619** </ol>
6620** ^If the S argument to sqlite3_txn_state(D,S) is not the name of
6621** a valid schema, then -1 is returned.
6622*/
6623SQLITE_API int sqlite3_txn_state(sqlite3*,const char *zSchema);
6624
6625/*
6626** CAPI3REF: Allowed return values from [sqlite3_txn_state()]
6627** KEYWORDS: {transaction state}
6628**
6629** These constants define the current transaction state of a database file.
6630** ^The [sqlite3_txn_state(D,S)] interface returns one of these
6631** constants in order to describe the transaction state of schema S
6632** in [database connection] D.
6633**
6634** <dl>
6635** [[SQLITE_TXN_NONE]] <dt>SQLITE_TXN_NONE</dt>
6636** <dd>The SQLITE_TXN_NONE state means that no transaction is currently
6637** pending.</dd>
6638**
6639** [[SQLITE_TXN_READ]] <dt>SQLITE_TXN_READ</dt>
6640** <dd>The SQLITE_TXN_READ state means that the database is currently
6641** in a read transaction. Content has been read from the database file
6642** but nothing in the database file has changed. The transaction state
6643** will advanced to SQLITE_TXN_WRITE if any changes occur and there are
6644** no other conflicting concurrent write transactions. The transaction
6645** state will revert to SQLITE_TXN_NONE following a [ROLLBACK] or
6646** [COMMIT].</dd>
6647**
6648** [[SQLITE_TXN_WRITE]] <dt>SQLITE_TXN_WRITE</dt>
6649** <dd>The SQLITE_TXN_WRITE state means that the database is currently
6650** in a write transaction. Content has been written to the database file
6651** but has not yet committed. The transaction state will change to
6652** to SQLITE_TXN_NONE at the next [ROLLBACK] or [COMMIT].</dd>
6653*/
6654#define SQLITE_TXN_NONE 0
6655#define SQLITE_TXN_READ 1
6656#define SQLITE_TXN_WRITE 2
6657
6658/*
6659** CAPI3REF: Find the next prepared statement
6660** METHOD: sqlite3
6661**
6662** ^This interface returns a pointer to the next [prepared statement] after
6663** pStmt associated with the [database connection] pDb. ^If pStmt is NULL
6664** then this interface returns a pointer to the first prepared statement
6665** associated with the database connection pDb. ^If no prepared statement
6666** satisfies the conditions of this routine, it returns NULL.
6667**
6668** The [database connection] pointer D in a call to
6669** [sqlite3_next_stmt(D,S)] must refer to an open database
6670** connection and in particular must not be a NULL pointer.
6671*/
6672SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
6673
6674/*
6675** CAPI3REF: Commit And Rollback Notification Callbacks
6676** METHOD: sqlite3
6677**
6678** ^The sqlite3_commit_hook() interface registers a callback
6679** function to be invoked whenever a transaction is [COMMIT | committed].
6680** ^Any callback set by a previous call to sqlite3_commit_hook()
6681** for the same database connection is overridden.
6682** ^The sqlite3_rollback_hook() interface registers a callback
6683** function to be invoked whenever a transaction is [ROLLBACK | rolled back].
6684** ^Any callback set by a previous call to sqlite3_rollback_hook()
6685** for the same database connection is overridden.
6686** ^The pArg argument is passed through to the callback.
6687** ^If the callback on a commit hook function returns non-zero,
6688** then the commit is converted into a rollback.
6689**
6690** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions
6691** return the P argument from the previous call of the same function
6692** on the same [database connection] D, or NULL for
6693** the first call for each function on D.
6694**
6695** The commit and rollback hook callbacks are not reentrant.
6696** The callback implementation must not do anything that will modify
6697** the database connection that invoked the callback. Any actions
6698** to modify the database connection must be deferred until after the
6699** completion of the [sqlite3_step()] call that triggered the commit
6700** or rollback hook in the first place.
6701** Note that running any other SQL statements, including SELECT statements,
6702** or merely calling [sqlite3_prepare_v2()] and [sqlite3_step()] will modify
6703** the database connections for the meaning of "modify" in this paragraph.
6704**
6705** ^Registering a NULL function disables the callback.
6706**
6707** ^When the commit hook callback routine returns zero, the [COMMIT]
6708** operation is allowed to continue normally. ^If the commit hook
6709** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
6710** ^The rollback hook is invoked on a rollback that results from a commit
6711** hook returning non-zero, just as it would be with any other rollback.
6712**
6713** ^For the purposes of this API, a transaction is said to have been
6714** rolled back if an explicit "ROLLBACK" statement is executed, or
6715** an error or constraint causes an implicit rollback to occur.
6716** ^The rollback callback is not invoked if a transaction is
6717** automatically rolled back because the database connection is closed.
6718**
6719** See also the [sqlite3_update_hook()] interface.
6720*/
6721SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
6722SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
6723
6724/*
6725** CAPI3REF: Autovacuum Compaction Amount Callback
6726** METHOD: sqlite3
6727**
6728** ^The sqlite3_autovacuum_pages(D,C,P,X) interface registers a callback
6729** function C that is invoked prior to each autovacuum of the database
6730** file. ^The callback is passed a copy of the generic data pointer (P),
6731** the schema-name of the attached database that is being autovacuumed,
6732** the the size of the database file in pages, the number of free pages,
6733** and the number of bytes per page, respectively. The callback should
6734** return the number of free pages that should be removed by the
6735** autovacuum. ^If the callback returns zero, then no autovacuum happens.
6736** ^If the value returned is greater than or equal to the number of
6737** free pages, then a complete autovacuum happens.
6738**
6739** <p>^If there are multiple ATTACH-ed database files that are being
6740** modified as part of a transaction commit, then the autovacuum pages
6741** callback is invoked separately for each file.
6742**
6743** <p><b>The callback is not reentrant.</b> The callback function should
6744** not attempt to invoke any other SQLite interface. If it does, bad
6745** things may happen, including segmentation faults and corrupt database
6746** files. The callback function should be a simple function that
6747** does some arithmetic on its input parameters and returns a result.
6748**
6749** ^The X parameter to sqlite3_autovacuum_pages(D,C,P,X) is an optional
6750** destructor for the P parameter. ^If X is not NULL, then X(P) is
6751** invoked whenever the database connection closes or when the callback
6752** is overwritten by another invocation of sqlite3_autovacuum_pages().
6753**
6754** <p>^There is only one autovacuum pages callback per database connection.
6755** ^Each call to the sqlite3_autovacuum_pages() interface overrides all
6756** previous invocations for that database connection. ^If the callback
6757** argument (C) to sqlite3_autovacuum_pages(D,C,P,X) is a NULL pointer,
6758** then the autovacuum steps callback is cancelled. The return value
6759** from sqlite3_autovacuum_pages() is normally SQLITE_OK, but might
6760** be some other error code if something goes wrong. The current
6761** implementation will only return SQLITE_OK or SQLITE_MISUSE, but other
6762** return codes might be added in future releases.
6763**
6764** <p>If no autovacuum pages callback is specified (the usual case) or
6765** a NULL pointer is provided for the callback,
6766** then the default behavior is to vacuum all free pages. So, in other
6767** words, the default behavior is the same as if the callback function
6768** were something like this:
6769**
6770** <blockquote><pre>
6771** &nbsp; unsigned int demonstration_autovac_pages_callback(
6772** &nbsp; void *pClientData,
6773** &nbsp; const char *zSchema,
6774** &nbsp; unsigned int nDbPage,
6775** &nbsp; unsigned int nFreePage,
6776** &nbsp; unsigned int nBytePerPage
6777** &nbsp; ){
6778** &nbsp; return nFreePage;
6779** &nbsp; }
6780** </pre></blockquote>
6781*/
6782SQLITE_API int sqlite3_autovacuum_pages(
6783 sqlite3 *db,
6784 unsigned int(*)(void*,const char*,unsigned int,unsigned int,unsigned int),
6785 void*,
6786 void(*)(void*)
6787);
6788
6789
6790/*
6791** CAPI3REF: Data Change Notification Callbacks
6792** METHOD: sqlite3
6793**
6794** ^The sqlite3_update_hook() interface registers a callback function
6795** with the [database connection] identified by the first argument
6796** to be invoked whenever a row is updated, inserted or deleted in
6797** a [rowid table].
6798** ^Any callback set by a previous call to this function
6799** for the same database connection is overridden.
6800**
6801** ^The second argument is a pointer to the function to invoke when a
6802** row is updated, inserted or deleted in a rowid table.
6803** ^The first argument to the callback is a copy of the third argument
6804** to sqlite3_update_hook().
6805** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE],
6806** or [SQLITE_UPDATE], depending on the operation that caused the callback
6807** to be invoked.
6808** ^The third and fourth arguments to the callback contain pointers to the
6809** database and table name containing the affected row.
6810** ^The final callback parameter is the [rowid] of the row.
6811** ^In the case of an update, this is the [rowid] after the update takes place.
6812**
6813** ^(The update hook is not invoked when internal system tables are
6814** modified (i.e. sqlite_sequence).)^
6815** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified.
6816**
6817** ^In the current implementation, the update hook
6818** is not invoked when conflicting rows are deleted because of an
6819** [ON CONFLICT | ON CONFLICT REPLACE] clause. ^Nor is the update hook
6820** invoked when rows are deleted using the [truncate optimization].
6821** The exceptions defined in this paragraph might change in a future
6822** release of SQLite.
6823**
6824** The update hook implementation must not do anything that will modify
6825** the database connection that invoked the update hook. Any actions
6826** to modify the database connection must be deferred until after the
6827** completion of the [sqlite3_step()] call that triggered the update hook.
6828** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
6829** database connections for the meaning of "modify" in this paragraph.
6830**
6831** ^The sqlite3_update_hook(D,C,P) function
6832** returns the P argument from the previous call
6833** on the same [database connection] D, or NULL for
6834** the first call on D.
6835**
6836** See also the [sqlite3_commit_hook()], [sqlite3_rollback_hook()],
6837** and [sqlite3_preupdate_hook()] interfaces.
6838*/
6839SQLITE_API void *sqlite3_update_hook(
6840 sqlite3*,
6841 void(*)(void *,int ,char const *,char const *,sqlite3_int64),
6842 void*
6843);
6844
6845/*
6846** CAPI3REF: Enable Or Disable Shared Pager Cache
6847**
6848** ^(This routine enables or disables the sharing of the database cache
6849** and schema data structures between [database connection | connections]
6850** to the same database. Sharing is enabled if the argument is true
6851** and disabled if the argument is false.)^
6852**
6853** ^Cache sharing is enabled and disabled for an entire process.
6854** This is a change as of SQLite [version 3.5.0] ([dateof:3.5.0]).
6855** In prior versions of SQLite,
6856** sharing was enabled or disabled for each thread separately.
6857**
6858** ^(The cache sharing mode set by this interface effects all subsequent
6859** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()].
6860** Existing database connections continue to use the sharing mode
6861** that was in effect at the time they were opened.)^
6862**
6863** ^(This routine returns [SQLITE_OK] if shared cache was enabled or disabled
6864** successfully. An [error code] is returned otherwise.)^
6865**
6866** ^Shared cache is disabled by default. It is recommended that it stay
6867** that way. In other words, do not use this routine. This interface
6868** continues to be provided for historical compatibility, but its use is
6869** discouraged. Any use of shared cache is discouraged. If shared cache
6870** must be used, it is recommended that shared cache only be enabled for
6871** individual database connections using the [sqlite3_open_v2()] interface
6872** with the [SQLITE_OPEN_SHAREDCACHE] flag.
6873**
6874** Note: This method is disabled on MacOS X 10.7 and iOS version 5.0
6875** and will always return SQLITE_MISUSE. On those systems,
6876** shared cache mode should be enabled per-database connection via
6877** [sqlite3_open_v2()] with [SQLITE_OPEN_SHAREDCACHE].
6878**
6879** This interface is threadsafe on processors where writing a
6880** 32-bit integer is atomic.
6881**
6882** See Also: [SQLite Shared-Cache Mode]
6883*/
6884SQLITE_API int sqlite3_enable_shared_cache(int);
6885
6886/*
6887** CAPI3REF: Attempt To Free Heap Memory
6888**
6889** ^The sqlite3_release_memory() interface attempts to free N bytes
6890** of heap memory by deallocating non-essential memory allocations
6891** held by the database library. Memory used to cache database
6892** pages to improve performance is an example of non-essential memory.
6893** ^sqlite3_release_memory() returns the number of bytes actually freed,
6894** which might be more or less than the amount requested.
6895** ^The sqlite3_release_memory() routine is a no-op returning zero
6896** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT].
6897**
6898** See also: [sqlite3_db_release_memory()]
6899*/
6900SQLITE_API int sqlite3_release_memory(int);
6901
6902/*
6903** CAPI3REF: Free Memory Used By A Database Connection
6904** METHOD: sqlite3
6905**
6906** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
6907** memory as possible from database connection D. Unlike the
6908** [sqlite3_release_memory()] interface, this interface is in effect even
6909** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
6910** omitted.
6911**
6912** See also: [sqlite3_release_memory()]
6913*/
6914SQLITE_API int sqlite3_db_release_memory(sqlite3*);
6915
6916/*
6917** CAPI3REF: Impose A Limit On Heap Size
6918**
6919** These interfaces impose limits on the amount of heap memory that will be
6920** by all database connections within a single process.
6921**
6922** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the
6923** soft limit on the amount of heap memory that may be allocated by SQLite.
6924** ^SQLite strives to keep heap memory utilization below the soft heap
6925** limit by reducing the number of pages held in the page cache
6926** as heap memory usages approaches the limit.
6927** ^The soft heap limit is "soft" because even though SQLite strives to stay
6928** below the limit, it will exceed the limit rather than generate
6929** an [SQLITE_NOMEM] error. In other words, the soft heap limit
6930** is advisory only.
6931**
6932** ^The sqlite3_hard_heap_limit64(N) interface sets a hard upper bound of
6933** N bytes on the amount of memory that will be allocated. ^The
6934** sqlite3_hard_heap_limit64(N) interface is similar to
6935** sqlite3_soft_heap_limit64(N) except that memory allocations will fail
6936** when the hard heap limit is reached.
6937**
6938** ^The return value from both sqlite3_soft_heap_limit64() and
6939** sqlite3_hard_heap_limit64() is the size of
6940** the heap limit prior to the call, or negative in the case of an
6941** error. ^If the argument N is negative
6942** then no change is made to the heap limit. Hence, the current
6943** size of heap limits can be determined by invoking
6944** sqlite3_soft_heap_limit64(-1) or sqlite3_hard_heap_limit(-1).
6945**
6946** ^Setting the heap limits to zero disables the heap limiter mechanism.
6947**
6948** ^The soft heap limit may not be greater than the hard heap limit.
6949** ^If the hard heap limit is enabled and if sqlite3_soft_heap_limit(N)
6950** is invoked with a value of N that is greater than the hard heap limit,
6951** the the soft heap limit is set to the value of the hard heap limit.
6952** ^The soft heap limit is automatically enabled whenever the hard heap
6953** limit is enabled. ^When sqlite3_hard_heap_limit64(N) is invoked and
6954** the soft heap limit is outside the range of 1..N, then the soft heap
6955** limit is set to N. ^Invoking sqlite3_soft_heap_limit64(0) when the
6956** hard heap limit is enabled makes the soft heap limit equal to the
6957** hard heap limit.
6958**
6959** The memory allocation limits can also be adjusted using
6960** [PRAGMA soft_heap_limit] and [PRAGMA hard_heap_limit].
6961**
6962** ^(The heap limits are not enforced in the current implementation
6963** if one or more of following conditions are true:
6964**
6965** <ul>
6966** <li> The limit value is set to zero.
6967** <li> Memory accounting is disabled using a combination of the
6968** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
6969** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
6970** <li> An alternative page cache implementation is specified using
6971** [sqlite3_config]([SQLITE_CONFIG_PCACHE2],...).
6972** <li> The page cache allocates from its own memory pool supplied
6973** by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
6974** from the heap.
6975** </ul>)^
6976**
6977** The circumstances under which SQLite will enforce the heap limits may
6978** changes in future releases of SQLite.
6979*/
6980SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N);
6981SQLITE_API sqlite3_int64 sqlite3_hard_heap_limit64(sqlite3_int64 N);
6982
6983/*
6984** CAPI3REF: Deprecated Soft Heap Limit Interface
6985** DEPRECATED
6986**
6987** This is a deprecated version of the [sqlite3_soft_heap_limit64()]
6988** interface. This routine is provided for historical compatibility
6989** only. All new applications should use the
6990** [sqlite3_soft_heap_limit64()] interface rather than this one.
6991*/
6992SQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N);
6993
6994
6995/*
6996** CAPI3REF: Extract Metadata About A Column Of A Table
6997** METHOD: sqlite3
6998**
6999** ^(The sqlite3_table_column_metadata(X,D,T,C,....) routine returns
7000** information about column C of table T in database D
7001** on [database connection] X.)^ ^The sqlite3_table_column_metadata()
7002** interface returns SQLITE_OK and fills in the non-NULL pointers in
7003** the final five arguments with appropriate values if the specified
7004** column exists. ^The sqlite3_table_column_metadata() interface returns
7005** SQLITE_ERROR if the specified column does not exist.
7006** ^If the column-name parameter to sqlite3_table_column_metadata() is a
7007** NULL pointer, then this routine simply checks for the existence of the
7008** table and returns SQLITE_OK if the table exists and SQLITE_ERROR if it
7009** does not. If the table name parameter T in a call to
7010** sqlite3_table_column_metadata(X,D,T,C,...) is NULL then the result is
7011** undefined behavior.
7012**
7013** ^The column is identified by the second, third and fourth parameters to
7014** this function. ^(The second parameter is either the name of the database
7015** (i.e. "main", "temp", or an attached database) containing the specified
7016** table or NULL.)^ ^If it is NULL, then all attached databases are searched
7017** for the table using the same algorithm used by the database engine to
7018** resolve unqualified table references.
7019**
7020** ^The third and fourth parameters to this function are the table and column
7021** name of the desired column, respectively.
7022**
7023** ^Metadata is returned by writing to the memory locations passed as the 5th
7024** and subsequent parameters to this function. ^Any of these arguments may be
7025** NULL, in which case the corresponding element of metadata is omitted.
7026**
7027** ^(<blockquote>
7028** <table border="1">
7029** <tr><th> Parameter <th> Output<br>Type <th> Description
7030**
7031** <tr><td> 5th <td> const char* <td> Data type
7032** <tr><td> 6th <td> const char* <td> Name of default collation sequence
7033** <tr><td> 7th <td> int <td> True if column has a NOT NULL constraint
7034** <tr><td> 8th <td> int <td> True if column is part of the PRIMARY KEY
7035** <tr><td> 9th <td> int <td> True if column is [AUTOINCREMENT]
7036** </table>
7037** </blockquote>)^
7038**
7039** ^The memory pointed to by the character pointers returned for the
7040** declaration type and collation sequence is valid until the next
7041** call to any SQLite API function.
7042**
7043** ^If the specified table is actually a view, an [error code] is returned.
7044**
7045** ^If the specified column is "rowid", "oid" or "_rowid_" and the table
7046** is not a [WITHOUT ROWID] table and an
7047** [INTEGER PRIMARY KEY] column has been explicitly declared, then the output
7048** parameters are set for the explicitly declared column. ^(If there is no
7049** [INTEGER PRIMARY KEY] column, then the outputs
7050** for the [rowid] are set as follows:
7051**
7052** <pre>
7053** data type: "INTEGER"
7054** collation sequence: "BINARY"
7055** not null: 0
7056** primary key: 1
7057** auto increment: 0
7058** </pre>)^
7059**
7060** ^This function causes all database schemas to be read from disk and
7061** parsed, if that has not already been done, and returns an error if
7062** any errors are encountered while loading the schema.
7063*/
7064SQLITE_API int sqlite3_table_column_metadata(
7065 sqlite3 *db, /* Connection handle */
7066 const char *zDbName, /* Database name or NULL */
7067 const char *zTableName, /* Table name */
7068 const char *zColumnName, /* Column name */
7069 char const **pzDataType, /* OUTPUT: Declared data type */
7070 char const **pzCollSeq, /* OUTPUT: Collation sequence name */
7071 int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
7072 int *pPrimaryKey, /* OUTPUT: True if column part of PK */
7073 int *pAutoinc /* OUTPUT: True if column is auto-increment */
7074);
7075
7076/*
7077** CAPI3REF: Load An Extension
7078** METHOD: sqlite3
7079**
7080** ^This interface loads an SQLite extension library from the named file.
7081**
7082** ^The sqlite3_load_extension() interface attempts to load an
7083** [SQLite extension] library contained in the file zFile. If
7084** the file cannot be loaded directly, attempts are made to load
7085** with various operating-system specific extensions added.
7086** So for example, if "samplelib" cannot be loaded, then names like
7087** "samplelib.so" or "samplelib.dylib" or "samplelib.dll" might
7088** be tried also.
7089**
7090** ^The entry point is zProc.
7091** ^(zProc may be 0, in which case SQLite will try to come up with an
7092** entry point name on its own. It first tries "sqlite3_extension_init".
7093** If that does not work, it constructs a name "sqlite3_X_init" where the
7094** X is consists of the lower-case equivalent of all ASCII alphabetic
7095** characters in the filename from the last "/" to the first following
7096** "." and omitting any initial "lib".)^
7097** ^The sqlite3_load_extension() interface returns
7098** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
7099** ^If an error occurs and pzErrMsg is not 0, then the
7100** [sqlite3_load_extension()] interface shall attempt to
7101** fill *pzErrMsg with error message text stored in memory
7102** obtained from [sqlite3_malloc()]. The calling function
7103** should free this memory by calling [sqlite3_free()].
7104**
7105** ^Extension loading must be enabled using
7106** [sqlite3_enable_load_extension()] or
7107** [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],1,NULL)
7108** prior to calling this API,
7109** otherwise an error will be returned.
7110**
7111** <b>Security warning:</b> It is recommended that the
7112** [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method be used to enable only this
7113** interface. The use of the [sqlite3_enable_load_extension()] interface
7114** should be avoided. This will keep the SQL function [load_extension()]
7115** disabled and prevent SQL injections from giving attackers
7116** access to extension loading capabilities.
7117**
7118** See also the [load_extension() SQL function].
7119*/
7120SQLITE_API int sqlite3_load_extension(
7121 sqlite3 *db, /* Load the extension into this database connection */
7122 const char *zFile, /* Name of the shared library containing extension */
7123 const char *zProc, /* Entry point. Derived from zFile if 0 */
7124 char **pzErrMsg /* Put error message here if not 0 */
7125);
7126
7127/*
7128** CAPI3REF: Enable Or Disable Extension Loading
7129** METHOD: sqlite3
7130**
7131** ^So as not to open security holes in older applications that are
7132** unprepared to deal with [extension loading], and as a means of disabling
7133** [extension loading] while evaluating user-entered SQL, the following API
7134** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
7135**
7136** ^Extension loading is off by default.
7137** ^Call the sqlite3_enable_load_extension() routine with onoff==1
7138** to turn extension loading on and call it with onoff==0 to turn
7139** it back off again.
7140**
7141** ^This interface enables or disables both the C-API
7142** [sqlite3_load_extension()] and the SQL function [load_extension()].
7143** ^(Use [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],..)
7144** to enable or disable only the C-API.)^
7145**
7146** <b>Security warning:</b> It is recommended that extension loading
7147** be enabled using the [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method
7148** rather than this interface, so the [load_extension()] SQL function
7149** remains disabled. This will prevent SQL injections from giving attackers
7150** access to extension loading capabilities.
7151*/
7152SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
7153
7154/*
7155** CAPI3REF: Automatically Load Statically Linked Extensions
7156**
7157** ^This interface causes the xEntryPoint() function to be invoked for
7158** each new [database connection] that is created. The idea here is that
7159** xEntryPoint() is the entry point for a statically linked [SQLite extension]
7160** that is to be automatically loaded into all new database connections.
7161**
7162** ^(Even though the function prototype shows that xEntryPoint() takes
7163** no arguments and returns void, SQLite invokes xEntryPoint() with three
7164** arguments and expects an integer result as if the signature of the
7165** entry point where as follows:
7166**
7167** <blockquote><pre>
7168** &nbsp; int xEntryPoint(
7169** &nbsp; sqlite3 *db,
7170** &nbsp; const char **pzErrMsg,
7171** &nbsp; const struct sqlite3_api_routines *pThunk
7172** &nbsp; );
7173** </pre></blockquote>)^
7174**
7175** If the xEntryPoint routine encounters an error, it should make *pzErrMsg
7176** point to an appropriate error message (obtained from [sqlite3_mprintf()])
7177** and return an appropriate [error code]. ^SQLite ensures that *pzErrMsg
7178** is NULL before calling the xEntryPoint(). ^SQLite will invoke
7179** [sqlite3_free()] on *pzErrMsg after xEntryPoint() returns. ^If any
7180** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()],
7181** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail.
7182**
7183** ^Calling sqlite3_auto_extension(X) with an entry point X that is already
7184** on the list of automatic extensions is a harmless no-op. ^No entry point
7185** will be called more than once for each database connection that is opened.
7186**
7187** See also: [sqlite3_reset_auto_extension()]
7188** and [sqlite3_cancel_auto_extension()]
7189*/
7190SQLITE_API int sqlite3_auto_extension(void(*xEntryPoint)(void));
7191
7192/*
7193** CAPI3REF: Cancel Automatic Extension Loading
7194**
7195** ^The [sqlite3_cancel_auto_extension(X)] interface unregisters the
7196** initialization routine X that was registered using a prior call to
7197** [sqlite3_auto_extension(X)]. ^The [sqlite3_cancel_auto_extension(X)]
7198** routine returns 1 if initialization routine X was successfully
7199** unregistered and it returns 0 if X was not on the list of initialization
7200** routines.
7201*/
7202SQLITE_API int sqlite3_cancel_auto_extension(void(*xEntryPoint)(void));
7203
7204/*
7205** CAPI3REF: Reset Automatic Extension Loading
7206**
7207** ^This interface disables all automatic extensions previously
7208** registered using [sqlite3_auto_extension()].
7209*/
7210SQLITE_API void sqlite3_reset_auto_extension(void);
7211
7212/*
7213** The interface to the virtual-table mechanism is currently considered
7214** to be experimental. The interface might change in incompatible ways.
7215** If this is a problem for you, do not use the interface at this time.
7216**
7217** When the virtual-table mechanism stabilizes, we will declare the
7218** interface fixed, support it indefinitely, and remove this comment.
7219*/
7220
7221/*
7222** Structures used by the virtual table interface
7223*/
7224typedef struct sqlite3_vtab sqlite3_vtab;
7225typedef struct sqlite3_index_info sqlite3_index_info;
7226typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor;
7227typedef struct sqlite3_module sqlite3_module;
7228
7229/*
7230** CAPI3REF: Virtual Table Object
7231** KEYWORDS: sqlite3_module {virtual table module}
7232**
7233** This structure, sometimes called a "virtual table module",
7234** defines the implementation of a [virtual table].
7235** This structure consists mostly of methods for the module.
7236**
7237** ^A virtual table module is created by filling in a persistent
7238** instance of this structure and passing a pointer to that instance
7239** to [sqlite3_create_module()] or [sqlite3_create_module_v2()].
7240** ^The registration remains valid until it is replaced by a different
7241** module or until the [database connection] closes. The content
7242** of this structure must not change while it is registered with
7243** any database connection.
7244*/
7245struct sqlite3_module {
7246 int iVersion;
7247 int (*xCreate)(sqlite3*, void *pAux,
7248 int argc, const char *const*argv,
7249 sqlite3_vtab **ppVTab, char**);
7250 int (*xConnect)(sqlite3*, void *pAux,
7251 int argc, const char *const*argv,
7252 sqlite3_vtab **ppVTab, char**);
7253 int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*);
7254 int (*xDisconnect)(sqlite3_vtab *pVTab);
7255 int (*xDestroy)(sqlite3_vtab *pVTab);
7256 int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);
7257 int (*xClose)(sqlite3_vtab_cursor*);
7258 int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
7259 int argc, sqlite3_value **argv);
7260 int (*xNext)(sqlite3_vtab_cursor*);
7261 int (*xEof)(sqlite3_vtab_cursor*);
7262 int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int);
7263 int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid);
7264 int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *);
7265 int (*xBegin)(sqlite3_vtab *pVTab);
7266 int (*xSync)(sqlite3_vtab *pVTab);
7267 int (*xCommit)(sqlite3_vtab *pVTab);
7268 int (*xRollback)(sqlite3_vtab *pVTab);
7269 int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName,
7270 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
7271 void **ppArg);
7272 int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
7273 /* The methods above are in version 1 of the sqlite_module object. Those
7274 ** below are for version 2 and greater. */
7275 int (*xSavepoint)(sqlite3_vtab *pVTab, int);
7276 int (*xRelease)(sqlite3_vtab *pVTab, int);
7277 int (*xRollbackTo)(sqlite3_vtab *pVTab, int);
7278 /* The methods above are in versions 1 and 2 of the sqlite_module object.
7279 ** Those below are for version 3 and greater. */
7280 int (*xShadowName)(const char*);
7281};
7282
7283/*
7284** CAPI3REF: Virtual Table Indexing Information
7285** KEYWORDS: sqlite3_index_info
7286**
7287** The sqlite3_index_info structure and its substructures is used as part
7288** of the [virtual table] interface to
7289** pass information into and receive the reply from the [xBestIndex]
7290** method of a [virtual table module]. The fields under **Inputs** are the
7291** inputs to xBestIndex and are read-only. xBestIndex inserts its
7292** results into the **Outputs** fields.
7293**
7294** ^(The aConstraint[] array records WHERE clause constraints of the form:
7295**
7296** <blockquote>column OP expr</blockquote>
7297**
7298** where OP is =, &lt;, &lt;=, &gt;, or &gt;=.)^ ^(The particular operator is
7299** stored in aConstraint[].op using one of the
7300** [SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_ values].)^
7301** ^(The index of the column is stored in
7302** aConstraint[].iColumn.)^ ^(aConstraint[].usable is TRUE if the
7303** expr on the right-hand side can be evaluated (and thus the constraint
7304** is usable) and false if it cannot.)^
7305**
7306** ^The optimizer automatically inverts terms of the form "expr OP column"
7307** and makes other simplifications to the WHERE clause in an attempt to
7308** get as many WHERE clause terms into the form shown above as possible.
7309** ^The aConstraint[] array only reports WHERE clause terms that are
7310** relevant to the particular virtual table being queried.
7311**
7312** ^Information about the ORDER BY clause is stored in aOrderBy[].
7313** ^Each term of aOrderBy records a column of the ORDER BY clause.
7314**
7315** The colUsed field indicates which columns of the virtual table may be
7316** required by the current scan. Virtual table columns are numbered from
7317** zero in the order in which they appear within the CREATE TABLE statement
7318** passed to sqlite3_declare_vtab(). For the first 63 columns (columns 0-62),
7319** the corresponding bit is set within the colUsed mask if the column may be
7320** required by SQLite. If the table has at least 64 columns and any column
7321** to the right of the first 63 is required, then bit 63 of colUsed is also
7322** set. In other words, column iCol may be required if the expression
7323** (colUsed & ((sqlite3_uint64)1 << (iCol>=63 ? 63 : iCol))) evaluates to
7324** non-zero.
7325**
7326** The [xBestIndex] method must fill aConstraintUsage[] with information
7327** about what parameters to pass to xFilter. ^If argvIndex>0 then
7328** the right-hand side of the corresponding aConstraint[] is evaluated
7329** and becomes the argvIndex-th entry in argv. ^(If aConstraintUsage[].omit
7330** is true, then the constraint is assumed to be fully handled by the
7331** virtual table and might not be checked again by the byte code.)^ ^(The
7332** aConstraintUsage[].omit flag is an optimization hint. When the omit flag
7333** is left in its default setting of false, the constraint will always be
7334** checked separately in byte code. If the omit flag is change to true, then
7335** the constraint may or may not be checked in byte code. In other words,
7336** when the omit flag is true there is no guarantee that the constraint will
7337** not be checked again using byte code.)^
7338**
7339** ^The idxNum and idxPtr values are recorded and passed into the
7340** [xFilter] method.
7341** ^[sqlite3_free()] is used to free idxPtr if and only if
7342** needToFreeIdxPtr is true.
7343**
7344** ^The orderByConsumed means that output from [xFilter]/[xNext] will occur in
7345** the correct order to satisfy the ORDER BY clause so that no separate
7346** sorting step is required.
7347**
7348** ^The estimatedCost value is an estimate of the cost of a particular
7349** strategy. A cost of N indicates that the cost of the strategy is similar
7350** to a linear scan of an SQLite table with N rows. A cost of log(N)
7351** indicates that the expense of the operation is similar to that of a
7352** binary search on a unique indexed field of an SQLite table with N rows.
7353**
7354** ^The estimatedRows value is an estimate of the number of rows that
7355** will be returned by the strategy.
7356**
7357** The xBestIndex method may optionally populate the idxFlags field with a
7358** mask of SQLITE_INDEX_SCAN_* flags. Currently there is only one such flag -
7359** SQLITE_INDEX_SCAN_UNIQUE. If the xBestIndex method sets this flag, SQLite
7360** assumes that the strategy may visit at most one row.
7361**
7362** Additionally, if xBestIndex sets the SQLITE_INDEX_SCAN_UNIQUE flag, then
7363** SQLite also assumes that if a call to the xUpdate() method is made as
7364** part of the same statement to delete or update a virtual table row and the
7365** implementation returns SQLITE_CONSTRAINT, then there is no need to rollback
7366** any database changes. In other words, if the xUpdate() returns
7367** SQLITE_CONSTRAINT, the database contents must be exactly as they were
7368** before xUpdate was called. By contrast, if SQLITE_INDEX_SCAN_UNIQUE is not
7369** set and xUpdate returns SQLITE_CONSTRAINT, any database changes made by
7370** the xUpdate method are automatically rolled back by SQLite.
7371**
7372** IMPORTANT: The estimatedRows field was added to the sqlite3_index_info
7373** structure for SQLite [version 3.8.2] ([dateof:3.8.2]).
7374** If a virtual table extension is
7375** used with an SQLite version earlier than 3.8.2, the results of attempting
7376** to read or write the estimatedRows field are undefined (but are likely
7377** to include crashing the application). The estimatedRows field should
7378** therefore only be used if [sqlite3_libversion_number()] returns a
7379** value greater than or equal to 3008002. Similarly, the idxFlags field
7380** was added for [version 3.9.0] ([dateof:3.9.0]).
7381** It may therefore only be used if
7382** sqlite3_libversion_number() returns a value greater than or equal to
7383** 3009000.
7384*/
7385struct sqlite3_index_info {
7386 /* Inputs */
7387 int nConstraint; /* Number of entries in aConstraint */
7388 struct sqlite3_index_constraint {
7389 int iColumn; /* Column constrained. -1 for ROWID */
7390 unsigned char op; /* Constraint operator */
7391 unsigned char usable; /* True if this constraint is usable */
7392 int iTermOffset; /* Used internally - xBestIndex should ignore */
7393 } *aConstraint; /* Table of WHERE clause constraints */
7394 int nOrderBy; /* Number of terms in the ORDER BY clause */
7395 struct sqlite3_index_orderby {
7396 int iColumn; /* Column number */
7397 unsigned char desc; /* True for DESC. False for ASC. */
7398 } *aOrderBy; /* The ORDER BY clause */
7399 /* Outputs */
7400 struct sqlite3_index_constraint_usage {
7401 int argvIndex; /* if >0, constraint is part of argv to xFilter */
7402 unsigned char omit; /* Do not code a test for this constraint */
7403 } *aConstraintUsage;
7404 int idxNum; /* Number used to identify the index */
7405 char *idxStr; /* String, possibly obtained from sqlite3_malloc */
7406 int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */
7407 int orderByConsumed; /* True if output is already ordered */
7408 double estimatedCost; /* Estimated cost of using this index */
7409 /* Fields below are only available in SQLite 3.8.2 and later */
7410 sqlite3_int64 estimatedRows; /* Estimated number of rows returned */
7411 /* Fields below are only available in SQLite 3.9.0 and later */
7412 int idxFlags; /* Mask of SQLITE_INDEX_SCAN_* flags */
7413 /* Fields below are only available in SQLite 3.10.0 and later */
7414 sqlite3_uint64 colUsed; /* Input: Mask of columns used by statement */
7415};
7416
7417/*
7418** CAPI3REF: Virtual Table Scan Flags
7419**
7420** Virtual table implementations are allowed to set the
7421** [sqlite3_index_info].idxFlags field to some combination of
7422** these bits.
7423*/
7424#define SQLITE_INDEX_SCAN_UNIQUE 1 /* Scan visits at most 1 row */
7425
7426/*
7427** CAPI3REF: Virtual Table Constraint Operator Codes
7428**
7429** These macros define the allowed values for the
7430** [sqlite3_index_info].aConstraint[].op field. Each value represents
7431** an operator that is part of a constraint term in the wHERE clause of
7432** a query that uses a [virtual table].
7433*/
7434#define SQLITE_INDEX_CONSTRAINT_EQ 2
7435#define SQLITE_INDEX_CONSTRAINT_GT 4
7436#define SQLITE_INDEX_CONSTRAINT_LE 8
7437#define SQLITE_INDEX_CONSTRAINT_LT 16
7438#define SQLITE_INDEX_CONSTRAINT_GE 32
7439#define SQLITE_INDEX_CONSTRAINT_MATCH 64
7440#define SQLITE_INDEX_CONSTRAINT_LIKE 65
7441#define SQLITE_INDEX_CONSTRAINT_GLOB 66
7442#define SQLITE_INDEX_CONSTRAINT_REGEXP 67
7443#define SQLITE_INDEX_CONSTRAINT_NE 68
7444#define SQLITE_INDEX_CONSTRAINT_ISNOT 69
7445#define SQLITE_INDEX_CONSTRAINT_ISNOTNULL 70
7446#define SQLITE_INDEX_CONSTRAINT_ISNULL 71
7447#define SQLITE_INDEX_CONSTRAINT_IS 72
7448#define SQLITE_INDEX_CONSTRAINT_FUNCTION 150
7449
7450/*
7451** CAPI3REF: Register A Virtual Table Implementation
7452** METHOD: sqlite3
7453**
7454** ^These routines are used to register a new [virtual table module] name.
7455** ^Module names must be registered before
7456** creating a new [virtual table] using the module and before using a
7457** preexisting [virtual table] for the module.
7458**
7459** ^The module name is registered on the [database connection] specified
7460** by the first parameter. ^The name of the module is given by the
7461** second parameter. ^The third parameter is a pointer to
7462** the implementation of the [virtual table module]. ^The fourth
7463** parameter is an arbitrary client data pointer that is passed through
7464** into the [xCreate] and [xConnect] methods of the virtual table module
7465** when a new virtual table is be being created or reinitialized.
7466**
7467** ^The sqlite3_create_module_v2() interface has a fifth parameter which
7468** is a pointer to a destructor for the pClientData. ^SQLite will
7469** invoke the destructor function (if it is not NULL) when SQLite
7470** no longer needs the pClientData pointer. ^The destructor will also
7471** be invoked if the call to sqlite3_create_module_v2() fails.
7472** ^The sqlite3_create_module()
7473** interface is equivalent to sqlite3_create_module_v2() with a NULL
7474** destructor.
7475**
7476** ^If the third parameter (the pointer to the sqlite3_module object) is
7477** NULL then no new module is create and any existing modules with the
7478** same name are dropped.
7479**
7480** See also: [sqlite3_drop_modules()]
7481*/
7482SQLITE_API int sqlite3_create_module(
7483 sqlite3 *db, /* SQLite connection to register module with */
7484 const char *zName, /* Name of the module */
7485 const sqlite3_module *p, /* Methods for the module */
7486 void *pClientData /* Client data for xCreate/xConnect */
7487);
7488SQLITE_API int sqlite3_create_module_v2(
7489 sqlite3 *db, /* SQLite connection to register module with */
7490 const char *zName, /* Name of the module */
7491 const sqlite3_module *p, /* Methods for the module */
7492 void *pClientData, /* Client data for xCreate/xConnect */
7493 void(*xDestroy)(void*) /* Module destructor function */
7494);
7495
7496/*
7497** CAPI3REF: Remove Unnecessary Virtual Table Implementations
7498** METHOD: sqlite3
7499**
7500** ^The sqlite3_drop_modules(D,L) interface removes all virtual
7501** table modules from database connection D except those named on list L.
7502** The L parameter must be either NULL or a pointer to an array of pointers
7503** to strings where the array is terminated by a single NULL pointer.
7504** ^If the L parameter is NULL, then all virtual table modules are removed.
7505**
7506** See also: [sqlite3_create_module()]
7507*/
7508SQLITE_API int sqlite3_drop_modules(
7509 sqlite3 *db, /* Remove modules from this connection */
7510 const char **azKeep /* Except, do not remove the ones named here */
7511);
7512
7513/*
7514** CAPI3REF: Virtual Table Instance Object
7515** KEYWORDS: sqlite3_vtab
7516**
7517** Every [virtual table module] implementation uses a subclass
7518** of this object to describe a particular instance
7519** of the [virtual table]. Each subclass will
7520** be tailored to the specific needs of the module implementation.
7521** The purpose of this superclass is to define certain fields that are
7522** common to all module implementations.
7523**
7524** ^Virtual tables methods can set an error message by assigning a
7525** string obtained from [sqlite3_mprintf()] to zErrMsg. The method should
7526** take care that any prior string is freed by a call to [sqlite3_free()]
7527** prior to assigning a new string to zErrMsg. ^After the error message
7528** is delivered up to the client application, the string will be automatically
7529** freed by sqlite3_free() and the zErrMsg field will be zeroed.
7530*/
7531struct sqlite3_vtab {
7532 const sqlite3_module *pModule; /* The module for this virtual table */
7533 int nRef; /* Number of open cursors */
7534 char *zErrMsg; /* Error message from sqlite3_mprintf() */
7535 /* Virtual table implementations will typically add additional fields */
7536};
7537
7538/*
7539** CAPI3REF: Virtual Table Cursor Object
7540** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor}
7541**
7542** Every [virtual table module] implementation uses a subclass of the
7543** following structure to describe cursors that point into the
7544** [virtual table] and are used
7545** to loop through the virtual table. Cursors are created using the
7546** [sqlite3_module.xOpen | xOpen] method of the module and are destroyed
7547** by the [sqlite3_module.xClose | xClose] method. Cursors are used
7548** by the [xFilter], [xNext], [xEof], [xColumn], and [xRowid] methods
7549** of the module. Each module implementation will define
7550** the content of a cursor structure to suit its own needs.
7551**
7552** This superclass exists in order to define fields of the cursor that
7553** are common to all implementations.
7554*/
7555struct sqlite3_vtab_cursor {
7556 sqlite3_vtab *pVtab; /* Virtual table of this cursor */
7557 /* Virtual table implementations will typically add additional fields */
7558};
7559
7560/*
7561** CAPI3REF: Declare The Schema Of A Virtual Table
7562**
7563** ^The [xCreate] and [xConnect] methods of a
7564** [virtual table module] call this interface
7565** to declare the format (the names and datatypes of the columns) of
7566** the virtual tables they implement.
7567*/
7568SQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zSQL);
7569
7570/*
7571** CAPI3REF: Overload A Function For A Virtual Table
7572** METHOD: sqlite3
7573**
7574** ^(Virtual tables can provide alternative implementations of functions
7575** using the [xFindFunction] method of the [virtual table module].
7576** But global versions of those functions
7577** must exist in order to be overloaded.)^
7578**
7579** ^(This API makes sure a global version of a function with a particular
7580** name and number of parameters exists. If no such function exists
7581** before this API is called, a new function is created.)^ ^The implementation
7582** of the new function always causes an exception to be thrown. So
7583** the new function is not good for anything by itself. Its only
7584** purpose is to be a placeholder function that can be overloaded
7585** by a [virtual table].
7586*/
7587SQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
7588
7589/*
7590** The interface to the virtual-table mechanism defined above (back up
7591** to a comment remarkably similar to this one) is currently considered
7592** to be experimental. The interface might change in incompatible ways.
7593** If this is a problem for you, do not use the interface at this time.
7594**
7595** When the virtual-table mechanism stabilizes, we will declare the
7596** interface fixed, support it indefinitely, and remove this comment.
7597*/
7598
7599/*
7600** CAPI3REF: A Handle To An Open BLOB
7601** KEYWORDS: {BLOB handle} {BLOB handles}
7602**
7603** An instance of this object represents an open BLOB on which
7604** [sqlite3_blob_open | incremental BLOB I/O] can be performed.
7605** ^Objects of this type are created by [sqlite3_blob_open()]
7606** and destroyed by [sqlite3_blob_close()].
7607** ^The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces
7608** can be used to read or write small subsections of the BLOB.
7609** ^The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes.
7610*/
7611typedef struct sqlite3_blob sqlite3_blob;
7612
7613/*
7614** CAPI3REF: Open A BLOB For Incremental I/O
7615** METHOD: sqlite3
7616** CONSTRUCTOR: sqlite3_blob
7617**
7618** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located
7619** in row iRow, column zColumn, table zTable in database zDb;
7620** in other words, the same BLOB that would be selected by:
7621**
7622** <pre>
7623** SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
7624** </pre>)^
7625**
7626** ^(Parameter zDb is not the filename that contains the database, but
7627** rather the symbolic name of the database. For attached databases, this is
7628** the name that appears after the AS keyword in the [ATTACH] statement.
7629** For the main database file, the database name is "main". For TEMP
7630** tables, the database name is "temp".)^
7631**
7632** ^If the flags parameter is non-zero, then the BLOB is opened for read
7633** and write access. ^If the flags parameter is zero, the BLOB is opened for
7634** read-only access.
7635**
7636** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is stored
7637** in *ppBlob. Otherwise an [error code] is returned and, unless the error
7638** code is SQLITE_MISUSE, *ppBlob is set to NULL.)^ ^This means that, provided
7639** the API is not misused, it is always safe to call [sqlite3_blob_close()]
7640** on *ppBlob after this function it returns.
7641**
7642** This function fails with SQLITE_ERROR if any of the following are true:
7643** <ul>
7644** <li> ^(Database zDb does not exist)^,
7645** <li> ^(Table zTable does not exist within database zDb)^,
7646** <li> ^(Table zTable is a WITHOUT ROWID table)^,
7647** <li> ^(Column zColumn does not exist)^,
7648** <li> ^(Row iRow is not present in the table)^,
7649** <li> ^(The specified column of row iRow contains a value that is not
7650** a TEXT or BLOB value)^,
7651** <li> ^(Column zColumn is part of an index, PRIMARY KEY or UNIQUE
7652** constraint and the blob is being opened for read/write access)^,
7653** <li> ^([foreign key constraints | Foreign key constraints] are enabled,
7654** column zColumn is part of a [child key] definition and the blob is
7655** being opened for read/write access)^.
7656** </ul>
7657**
7658** ^Unless it returns SQLITE_MISUSE, this function sets the
7659** [database connection] error code and message accessible via
7660** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
7661**
7662** A BLOB referenced by sqlite3_blob_open() may be read using the
7663** [sqlite3_blob_read()] interface and modified by using
7664** [sqlite3_blob_write()]. The [BLOB handle] can be moved to a
7665** different row of the same table using the [sqlite3_blob_reopen()]
7666** interface. However, the column, table, or database of a [BLOB handle]
7667** cannot be changed after the [BLOB handle] is opened.
7668**
7669** ^(If the row that a BLOB handle points to is modified by an
7670** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
7671** then the BLOB handle is marked as "expired".
7672** This is true if any column of the row is changed, even a column
7673** other than the one the BLOB handle is open on.)^
7674** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for
7675** an expired BLOB handle fail with a return code of [SQLITE_ABORT].
7676** ^(Changes written into a BLOB prior to the BLOB expiring are not
7677** rolled back by the expiration of the BLOB. Such changes will eventually
7678** commit if the transaction continues to completion.)^
7679**
7680** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
7681** the opened blob. ^The size of a blob may not be changed by this
7682** interface. Use the [UPDATE] SQL command to change the size of a
7683** blob.
7684**
7685** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
7686** and the built-in [zeroblob] SQL function may be used to create a
7687** zero-filled blob to read or write using the incremental-blob interface.
7688**
7689** To avoid a resource leak, every open [BLOB handle] should eventually
7690** be released by a call to [sqlite3_blob_close()].
7691**
7692** See also: [sqlite3_blob_close()],
7693** [sqlite3_blob_reopen()], [sqlite3_blob_read()],
7694** [sqlite3_blob_bytes()], [sqlite3_blob_write()].
7695*/
7696SQLITE_API int sqlite3_blob_open(
7697 sqlite3*,
7698 const char *zDb,
7699 const char *zTable,
7700 const char *zColumn,
7701 sqlite3_int64 iRow,
7702 int flags,
7703 sqlite3_blob **ppBlob
7704);
7705
7706/*
7707** CAPI3REF: Move a BLOB Handle to a New Row
7708** METHOD: sqlite3_blob
7709**
7710** ^This function is used to move an existing [BLOB handle] so that it points
7711** to a different row of the same database table. ^The new row is identified
7712** by the rowid value passed as the second argument. Only the row can be
7713** changed. ^The database, table and column on which the blob handle is open
7714** remain the same. Moving an existing [BLOB handle] to a new row is
7715** faster than closing the existing handle and opening a new one.
7716**
7717** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
7718** it must exist and there must be either a blob or text value stored in
7719** the nominated column.)^ ^If the new row is not present in the table, or if
7720** it does not contain a blob or text value, or if another error occurs, an
7721** SQLite error code is returned and the blob handle is considered aborted.
7722** ^All subsequent calls to [sqlite3_blob_read()], [sqlite3_blob_write()] or
7723** [sqlite3_blob_reopen()] on an aborted blob handle immediately return
7724** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle
7725** always returns zero.
7726**
7727** ^This function sets the database handle error code and message.
7728*/
7729SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
7730
7731/*
7732** CAPI3REF: Close A BLOB Handle
7733** DESTRUCTOR: sqlite3_blob
7734**
7735** ^This function closes an open [BLOB handle]. ^(The BLOB handle is closed
7736** unconditionally. Even if this routine returns an error code, the
7737** handle is still closed.)^
7738**
7739** ^If the blob handle being closed was opened for read-write access, and if
7740** the database is in auto-commit mode and there are no other open read-write
7741** blob handles or active write statements, the current transaction is
7742** committed. ^If an error occurs while committing the transaction, an error
7743** code is returned and the transaction rolled back.
7744**
7745** Calling this function with an argument that is not a NULL pointer or an
7746** open blob handle results in undefined behaviour. ^Calling this routine
7747** with a null pointer (such as would be returned by a failed call to
7748** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function
7749** is passed a valid open blob handle, the values returned by the
7750** sqlite3_errcode() and sqlite3_errmsg() functions are set before returning.
7751*/
7752SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
7753
7754/*
7755** CAPI3REF: Return The Size Of An Open BLOB
7756** METHOD: sqlite3_blob
7757**
7758** ^Returns the size in bytes of the BLOB accessible via the
7759** successfully opened [BLOB handle] in its only argument. ^The
7760** incremental blob I/O routines can only read or overwriting existing
7761** blob content; they cannot change the size of a blob.
7762**
7763** This routine only works on a [BLOB handle] which has been created
7764** by a prior successful call to [sqlite3_blob_open()] and which has not
7765** been closed by [sqlite3_blob_close()]. Passing any other pointer in
7766** to this routine results in undefined and probably undesirable behavior.
7767*/
7768SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *);
7769
7770/*
7771** CAPI3REF: Read Data From A BLOB Incrementally
7772** METHOD: sqlite3_blob
7773**
7774** ^(This function is used to read data from an open [BLOB handle] into a
7775** caller-supplied buffer. N bytes of data are copied into buffer Z
7776** from the open BLOB, starting at offset iOffset.)^
7777**
7778** ^If offset iOffset is less than N bytes from the end of the BLOB,
7779** [SQLITE_ERROR] is returned and no data is read. ^If N or iOffset is
7780** less than zero, [SQLITE_ERROR] is returned and no data is read.
7781** ^The size of the blob (and hence the maximum value of N+iOffset)
7782** can be determined using the [sqlite3_blob_bytes()] interface.
7783**
7784** ^An attempt to read from an expired [BLOB handle] fails with an
7785** error code of [SQLITE_ABORT].
7786**
7787** ^(On success, sqlite3_blob_read() returns SQLITE_OK.
7788** Otherwise, an [error code] or an [extended error code] is returned.)^
7789**
7790** This routine only works on a [BLOB handle] which has been created
7791** by a prior successful call to [sqlite3_blob_open()] and which has not
7792** been closed by [sqlite3_blob_close()]. Passing any other pointer in
7793** to this routine results in undefined and probably undesirable behavior.
7794**
7795** See also: [sqlite3_blob_write()].
7796*/
7797SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
7798
7799/*
7800** CAPI3REF: Write Data Into A BLOB Incrementally
7801** METHOD: sqlite3_blob
7802**
7803** ^(This function is used to write data into an open [BLOB handle] from a
7804** caller-supplied buffer. N bytes of data are copied from the buffer Z
7805** into the open BLOB, starting at offset iOffset.)^
7806**
7807** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
7808** Otherwise, an [error code] or an [extended error code] is returned.)^
7809** ^Unless SQLITE_MISUSE is returned, this function sets the
7810** [database connection] error code and message accessible via
7811** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
7812**
7813** ^If the [BLOB handle] passed as the first argument was not opened for
7814** writing (the flags parameter to [sqlite3_blob_open()] was zero),
7815** this function returns [SQLITE_READONLY].
7816**
7817** This function may only modify the contents of the BLOB; it is
7818** not possible to increase the size of a BLOB using this API.
7819** ^If offset iOffset is less than N bytes from the end of the BLOB,
7820** [SQLITE_ERROR] is returned and no data is written. The size of the
7821** BLOB (and hence the maximum value of N+iOffset) can be determined
7822** using the [sqlite3_blob_bytes()] interface. ^If N or iOffset are less
7823** than zero [SQLITE_ERROR] is returned and no data is written.
7824**
7825** ^An attempt to write to an expired [BLOB handle] fails with an
7826** error code of [SQLITE_ABORT]. ^Writes to the BLOB that occurred
7827** before the [BLOB handle] expired are not rolled back by the
7828** expiration of the handle, though of course those changes might
7829** have been overwritten by the statement that expired the BLOB handle
7830** or by other independent statements.
7831**
7832** This routine only works on a [BLOB handle] which has been created
7833** by a prior successful call to [sqlite3_blob_open()] and which has not
7834** been closed by [sqlite3_blob_close()]. Passing any other pointer in
7835** to this routine results in undefined and probably undesirable behavior.
7836**
7837** See also: [sqlite3_blob_read()].
7838*/
7839SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
7840
7841/*
7842** CAPI3REF: Virtual File System Objects
7843**
7844** A virtual filesystem (VFS) is an [sqlite3_vfs] object
7845** that SQLite uses to interact
7846** with the underlying operating system. Most SQLite builds come with a
7847** single default VFS that is appropriate for the host computer.
7848** New VFSes can be registered and existing VFSes can be unregistered.
7849** The following interfaces are provided.
7850**
7851** ^The sqlite3_vfs_find() interface returns a pointer to a VFS given its name.
7852** ^Names are case sensitive.
7853** ^Names are zero-terminated UTF-8 strings.
7854** ^If there is no match, a NULL pointer is returned.
7855** ^If zVfsName is NULL then the default VFS is returned.
7856**
7857** ^New VFSes are registered with sqlite3_vfs_register().
7858** ^Each new VFS becomes the default VFS if the makeDflt flag is set.
7859** ^The same VFS can be registered multiple times without injury.
7860** ^To make an existing VFS into the default VFS, register it again
7861** with the makeDflt flag set. If two different VFSes with the
7862** same name are registered, the behavior is undefined. If a
7863** VFS is registered with a name that is NULL or an empty string,
7864** then the behavior is undefined.
7865**
7866** ^Unregister a VFS with the sqlite3_vfs_unregister() interface.
7867** ^(If the default VFS is unregistered, another VFS is chosen as
7868** the default. The choice for the new VFS is arbitrary.)^
7869*/
7870SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName);
7871SQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
7872SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*);
7873
7874/*
7875** CAPI3REF: Mutexes
7876**
7877** The SQLite core uses these routines for thread
7878** synchronization. Though they are intended for internal
7879** use by SQLite, code that links against SQLite is
7880** permitted to use any of these routines.
7881**
7882** The SQLite source code contains multiple implementations
7883** of these mutex routines. An appropriate implementation
7884** is selected automatically at compile-time. The following
7885** implementations are available in the SQLite core:
7886**
7887** <ul>
7888** <li> SQLITE_MUTEX_PTHREADS
7889** <li> SQLITE_MUTEX_W32
7890** <li> SQLITE_MUTEX_NOOP
7891** </ul>
7892**
7893** The SQLITE_MUTEX_NOOP implementation is a set of routines
7894** that does no real locking and is appropriate for use in
7895** a single-threaded application. The SQLITE_MUTEX_PTHREADS and
7896** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix
7897** and Windows.
7898**
7899** If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
7900** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
7901** implementation is included with the library. In this case the
7902** application must supply a custom mutex implementation using the
7903** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function
7904** before calling sqlite3_initialize() or any other public sqlite3_
7905** function that calls sqlite3_initialize().
7906**
7907** ^The sqlite3_mutex_alloc() routine allocates a new
7908** mutex and returns a pointer to it. ^The sqlite3_mutex_alloc()
7909** routine returns NULL if it is unable to allocate the requested
7910** mutex. The argument to sqlite3_mutex_alloc() must one of these
7911** integer constants:
7912**
7913** <ul>
7914** <li> SQLITE_MUTEX_FAST
7915** <li> SQLITE_MUTEX_RECURSIVE
7916** <li> SQLITE_MUTEX_STATIC_MAIN
7917** <li> SQLITE_MUTEX_STATIC_MEM
7918** <li> SQLITE_MUTEX_STATIC_OPEN
7919** <li> SQLITE_MUTEX_STATIC_PRNG
7920** <li> SQLITE_MUTEX_STATIC_LRU
7921** <li> SQLITE_MUTEX_STATIC_PMEM
7922** <li> SQLITE_MUTEX_STATIC_APP1
7923** <li> SQLITE_MUTEX_STATIC_APP2
7924** <li> SQLITE_MUTEX_STATIC_APP3
7925** <li> SQLITE_MUTEX_STATIC_VFS1
7926** <li> SQLITE_MUTEX_STATIC_VFS2
7927** <li> SQLITE_MUTEX_STATIC_VFS3
7928** </ul>
7929**
7930** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
7931** cause sqlite3_mutex_alloc() to create
7932** a new mutex. ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
7933** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
7934** The mutex implementation does not need to make a distinction
7935** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
7936** not want to. SQLite will only request a recursive mutex in
7937** cases where it really needs one. If a faster non-recursive mutex
7938** implementation is available on the host platform, the mutex subsystem
7939** might return such a mutex in response to SQLITE_MUTEX_FAST.
7940**
7941** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other
7942** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return
7943** a pointer to a static preexisting mutex. ^Nine static mutexes are
7944** used by the current version of SQLite. Future versions of SQLite
7945** may add additional static mutexes. Static mutexes are for internal
7946** use by SQLite only. Applications that use SQLite mutexes should
7947** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
7948** SQLITE_MUTEX_RECURSIVE.
7949**
7950** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
7951** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
7952** returns a different mutex on every call. ^For the static
7953** mutex types, the same mutex is returned on every call that has
7954** the same type number.
7955**
7956** ^The sqlite3_mutex_free() routine deallocates a previously
7957** allocated dynamic mutex. Attempting to deallocate a static
7958** mutex results in undefined behavior.
7959**
7960** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
7961** to enter a mutex. ^If another thread is already within the mutex,
7962** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
7963** SQLITE_BUSY. ^The sqlite3_mutex_try() interface returns [SQLITE_OK]
7964** upon successful entry. ^(Mutexes created using
7965** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread.
7966** In such cases, the
7967** mutex must be exited an equal number of times before another thread
7968** can enter.)^ If the same thread tries to enter any mutex other
7969** than an SQLITE_MUTEX_RECURSIVE more than once, the behavior is undefined.
7970**
7971** ^(Some systems (for example, Windows 95) do not support the operation
7972** implemented by sqlite3_mutex_try(). On those systems, sqlite3_mutex_try()
7973** will always return SQLITE_BUSY. The SQLite core only ever uses
7974** sqlite3_mutex_try() as an optimization so this is acceptable
7975** behavior.)^
7976**
7977** ^The sqlite3_mutex_leave() routine exits a mutex that was
7978** previously entered by the same thread. The behavior
7979** is undefined if the mutex is not currently entered by the
7980** calling thread or is not currently allocated.
7981**
7982** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or
7983** sqlite3_mutex_leave() is a NULL pointer, then all three routines
7984** behave as no-ops.
7985**
7986** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()].
7987*/
7988SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int);
7989SQLITE_API void sqlite3_mutex_free(sqlite3_mutex*);
7990SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex*);
7991SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*);
7992SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*);
7993
7994/*
7995** CAPI3REF: Mutex Methods Object
7996**
7997** An instance of this structure defines the low-level routines
7998** used to allocate and use mutexes.
7999**
8000** Usually, the default mutex implementations provided by SQLite are
8001** sufficient, however the application has the option of substituting a custom
8002** implementation for specialized deployments or systems for which SQLite
8003** does not provide a suitable implementation. In this case, the application
8004** creates and populates an instance of this structure to pass
8005** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option.
8006** Additionally, an instance of this structure can be used as an
8007** output variable when querying the system for the current mutex
8008** implementation, using the [SQLITE_CONFIG_GETMUTEX] option.
8009**
8010** ^The xMutexInit method defined by this structure is invoked as
8011** part of system initialization by the sqlite3_initialize() function.
8012** ^The xMutexInit routine is called by SQLite exactly once for each
8013** effective call to [sqlite3_initialize()].
8014**
8015** ^The xMutexEnd method defined by this structure is invoked as
8016** part of system shutdown by the sqlite3_shutdown() function. The
8017** implementation of this method is expected to release all outstanding
8018** resources obtained by the mutex methods implementation, especially
8019** those obtained by the xMutexInit method. ^The xMutexEnd()
8020** interface is invoked exactly once for each call to [sqlite3_shutdown()].
8021**
8022** ^(The remaining seven methods defined by this structure (xMutexAlloc,
8023** xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld and
8024** xMutexNotheld) implement the following interfaces (respectively):
8025**
8026** <ul>
8027** <li> [sqlite3_mutex_alloc()] </li>
8028** <li> [sqlite3_mutex_free()] </li>
8029** <li> [sqlite3_mutex_enter()] </li>
8030** <li> [sqlite3_mutex_try()] </li>
8031** <li> [sqlite3_mutex_leave()] </li>
8032** <li> [sqlite3_mutex_held()] </li>
8033** <li> [sqlite3_mutex_notheld()] </li>
8034** </ul>)^
8035**
8036** The only difference is that the public sqlite3_XXX functions enumerated
8037** above silently ignore any invocations that pass a NULL pointer instead
8038** of a valid mutex handle. The implementations of the methods defined
8039** by this structure are not required to handle this case. The results
8040** of passing a NULL pointer instead of a valid mutex handle are undefined
8041** (i.e. it is acceptable to provide an implementation that segfaults if
8042** it is passed a NULL pointer).
8043**
8044** The xMutexInit() method must be threadsafe. It must be harmless to
8045** invoke xMutexInit() multiple times within the same process and without
8046** intervening calls to xMutexEnd(). Second and subsequent calls to
8047** xMutexInit() must be no-ops.
8048**
8049** xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()]
8050** and its associates). Similarly, xMutexAlloc() must not use SQLite memory
8051** allocation for a static mutex. ^However xMutexAlloc() may use SQLite
8052** memory allocation for a fast or recursive mutex.
8053**
8054** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is
8055** called, but only if the prior call to xMutexInit returned SQLITE_OK.
8056** If xMutexInit fails in any way, it is expected to clean up after itself
8057** prior to returning.
8058*/
8059typedef struct sqlite3_mutex_methods sqlite3_mutex_methods;
8060struct sqlite3_mutex_methods {
8061 int (*xMutexInit)(void);
8062 int (*xMutexEnd)(void);
8063 sqlite3_mutex *(*xMutexAlloc)(int);
8064 void (*xMutexFree)(sqlite3_mutex *);
8065 void (*xMutexEnter)(sqlite3_mutex *);
8066 int (*xMutexTry)(sqlite3_mutex *);
8067 void (*xMutexLeave)(sqlite3_mutex *);
8068 int (*xMutexHeld)(sqlite3_mutex *);
8069 int (*xMutexNotheld)(sqlite3_mutex *);
8070};
8071
8072/*
8073** CAPI3REF: Mutex Verification Routines
8074**
8075** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines
8076** are intended for use inside assert() statements. The SQLite core
8077** never uses these routines except inside an assert() and applications
8078** are advised to follow the lead of the core. The SQLite core only
8079** provides implementations for these routines when it is compiled
8080** with the SQLITE_DEBUG flag. External mutex implementations
8081** are only required to provide these routines if SQLITE_DEBUG is
8082** defined and if NDEBUG is not defined.
8083**
8084** These routines should return true if the mutex in their argument
8085** is held or not held, respectively, by the calling thread.
8086**
8087** The implementation is not required to provide versions of these
8088** routines that actually work. If the implementation does not provide working
8089** versions of these routines, it should at least provide stubs that always
8090** return true so that one does not get spurious assertion failures.
8091**
8092** If the argument to sqlite3_mutex_held() is a NULL pointer then
8093** the routine should return 1. This seems counter-intuitive since
8094** clearly the mutex cannot be held if it does not exist. But
8095** the reason the mutex does not exist is because the build is not
8096** using mutexes. And we do not want the assert() containing the
8097** call to sqlite3_mutex_held() to fail, so a non-zero return is
8098** the appropriate thing to do. The sqlite3_mutex_notheld()
8099** interface should also return 1 when given a NULL pointer.
8100*/
8101#ifndef NDEBUG
8102SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
8103SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*);
8104#endif
8105
8106/*
8107** CAPI3REF: Mutex Types
8108**
8109** The [sqlite3_mutex_alloc()] interface takes a single argument
8110** which is one of these integer constants.
8111**
8112** The set of static mutexes may change from one SQLite release to the
8113** next. Applications that override the built-in mutex logic must be
8114** prepared to accommodate additional static mutexes.
8115*/
8116#define SQLITE_MUTEX_FAST 0
8117#define SQLITE_MUTEX_RECURSIVE 1
8118#define SQLITE_MUTEX_STATIC_MAIN 2
8119#define SQLITE_MUTEX_STATIC_MEM 3 /* sqlite3_malloc() */
8120#define SQLITE_MUTEX_STATIC_MEM2 4 /* NOT USED */
8121#define SQLITE_MUTEX_STATIC_OPEN 4 /* sqlite3BtreeOpen() */
8122#define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_randomness() */
8123#define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */
8124#define SQLITE_MUTEX_STATIC_LRU2 7 /* NOT USED */
8125#define SQLITE_MUTEX_STATIC_PMEM 7 /* sqlite3PageMalloc() */
8126#define SQLITE_MUTEX_STATIC_APP1 8 /* For use by application */
8127#define SQLITE_MUTEX_STATIC_APP2 9 /* For use by application */
8128#define SQLITE_MUTEX_STATIC_APP3 10 /* For use by application */
8129#define SQLITE_MUTEX_STATIC_VFS1 11 /* For use by built-in VFS */
8130#define SQLITE_MUTEX_STATIC_VFS2 12 /* For use by extension VFS */
8131#define SQLITE_MUTEX_STATIC_VFS3 13 /* For use by application VFS */
8132
8133/* Legacy compatibility: */
8134#define SQLITE_MUTEX_STATIC_MASTER 2
8135
8136
8137/*
8138** CAPI3REF: Retrieve the mutex for a database connection
8139** METHOD: sqlite3
8140**
8141** ^This interface returns a pointer the [sqlite3_mutex] object that
8142** serializes access to the [database connection] given in the argument
8143** when the [threading mode] is Serialized.
8144** ^If the [threading mode] is Single-thread or Multi-thread then this
8145** routine returns a NULL pointer.
8146*/
8147SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3*);
8148
8149/*
8150** CAPI3REF: Low-Level Control Of Database Files
8151** METHOD: sqlite3
8152** KEYWORDS: {file control}
8153**
8154** ^The [sqlite3_file_control()] interface makes a direct call to the
8155** xFileControl method for the [sqlite3_io_methods] object associated
8156** with a particular database identified by the second argument. ^The
8157** name of the database is "main" for the main database or "temp" for the
8158** TEMP database, or the name that appears after the AS keyword for
8159** databases that are added using the [ATTACH] SQL command.
8160** ^A NULL pointer can be used in place of "main" to refer to the
8161** main database file.
8162** ^The third and fourth parameters to this routine
8163** are passed directly through to the second and third parameters of
8164** the xFileControl method. ^The return value of the xFileControl
8165** method becomes the return value of this routine.
8166**
8167** A few opcodes for [sqlite3_file_control()] are handled directly
8168** by the SQLite core and never invoke the
8169** sqlite3_io_methods.xFileControl method.
8170** ^The [SQLITE_FCNTL_FILE_POINTER] value for the op parameter causes
8171** a pointer to the underlying [sqlite3_file] object to be written into
8172** the space pointed to by the 4th parameter. The
8173** [SQLITE_FCNTL_JOURNAL_POINTER] works similarly except that it returns
8174** the [sqlite3_file] object associated with the journal file instead of
8175** the main database. The [SQLITE_FCNTL_VFS_POINTER] opcode returns
8176** a pointer to the underlying [sqlite3_vfs] object for the file.
8177** The [SQLITE_FCNTL_DATA_VERSION] returns the data version counter
8178** from the pager.
8179**
8180** ^If the second parameter (zDbName) does not match the name of any
8181** open database file, then SQLITE_ERROR is returned. ^This error
8182** code is not remembered and will not be recalled by [sqlite3_errcode()]
8183** or [sqlite3_errmsg()]. The underlying xFileControl method might
8184** also return SQLITE_ERROR. There is no way to distinguish between
8185** an incorrect zDbName and an SQLITE_ERROR return from the underlying
8186** xFileControl method.
8187**
8188** See also: [file control opcodes]
8189*/
8190SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
8191
8192/*
8193** CAPI3REF: Testing Interface
8194**
8195** ^The sqlite3_test_control() interface is used to read out internal
8196** state of SQLite and to inject faults into SQLite for testing
8197** purposes. ^The first parameter is an operation code that determines
8198** the number, meaning, and operation of all subsequent parameters.
8199**
8200** This interface is not for use by applications. It exists solely
8201** for verifying the correct operation of the SQLite library. Depending
8202** on how the SQLite library is compiled, this interface might not exist.
8203**
8204** The details of the operation codes, their meanings, the parameters
8205** they take, and what they do are all subject to change without notice.
8206** Unlike most of the SQLite API, this function is not guaranteed to
8207** operate consistently from one release to the next.
8208*/
8209SQLITE_API int sqlite3_test_control(int op, ...);
8210
8211/*
8212** CAPI3REF: Testing Interface Operation Codes
8213**
8214** These constants are the valid operation code parameters used
8215** as the first argument to [sqlite3_test_control()].
8216**
8217** These parameters and their meanings are subject to change
8218** without notice. These values are for testing purposes only.
8219** Applications should not use any of these parameters or the
8220** [sqlite3_test_control()] interface.
8221*/
8222#define SQLITE_TESTCTRL_FIRST 5
8223#define SQLITE_TESTCTRL_PRNG_SAVE 5
8224#define SQLITE_TESTCTRL_PRNG_RESTORE 6
8225#define SQLITE_TESTCTRL_PRNG_RESET 7 /* NOT USED */
8226#define SQLITE_TESTCTRL_BITVEC_TEST 8
8227#define SQLITE_TESTCTRL_FAULT_INSTALL 9
8228#define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS 10
8229#define SQLITE_TESTCTRL_PENDING_BYTE 11
8230#define SQLITE_TESTCTRL_ASSERT 12
8231#define SQLITE_TESTCTRL_ALWAYS 13
8232#define SQLITE_TESTCTRL_RESERVE 14 /* NOT USED */
8233#define SQLITE_TESTCTRL_OPTIMIZATIONS 15
8234#define SQLITE_TESTCTRL_ISKEYWORD 16 /* NOT USED */
8235#define SQLITE_TESTCTRL_SCRATCHMALLOC 17 /* NOT USED */
8236#define SQLITE_TESTCTRL_INTERNAL_FUNCTIONS 17
8237#define SQLITE_TESTCTRL_LOCALTIME_FAULT 18
8238#define SQLITE_TESTCTRL_EXPLAIN_STMT 19 /* NOT USED */
8239#define SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD 19
8240#define SQLITE_TESTCTRL_NEVER_CORRUPT 20
8241#define SQLITE_TESTCTRL_VDBE_COVERAGE 21
8242#define SQLITE_TESTCTRL_BYTEORDER 22
8243#define SQLITE_TESTCTRL_ISINIT 23
8244#define SQLITE_TESTCTRL_SORTER_MMAP 24
8245#define SQLITE_TESTCTRL_IMPOSTER 25
8246#define SQLITE_TESTCTRL_PARSER_COVERAGE 26
8247#define SQLITE_TESTCTRL_RESULT_INTREAL 27
8248#define SQLITE_TESTCTRL_PRNG_SEED 28
8249#define SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS 29
8250#define SQLITE_TESTCTRL_SEEK_COUNT 30
8251#define SQLITE_TESTCTRL_TRACEFLAGS 31
8252#define SQLITE_TESTCTRL_TUNE 32
8253#define SQLITE_TESTCTRL_LAST 32 /* Largest TESTCTRL */
8254
8255/*
8256** CAPI3REF: SQL Keyword Checking
8257**
8258** These routines provide access to the set of SQL language keywords
8259** recognized by SQLite. Applications can uses these routines to determine
8260** whether or not a specific identifier needs to be escaped (for example,
8261** by enclosing in double-quotes) so as not to confuse the parser.
8262**
8263** The sqlite3_keyword_count() interface returns the number of distinct
8264** keywords understood by SQLite.
8265**
8266** The sqlite3_keyword_name(N,Z,L) interface finds the N-th keyword and
8267** makes *Z point to that keyword expressed as UTF8 and writes the number
8268** of bytes in the keyword into *L. The string that *Z points to is not
8269** zero-terminated. The sqlite3_keyword_name(N,Z,L) routine returns
8270** SQLITE_OK if N is within bounds and SQLITE_ERROR if not. If either Z
8271** or L are NULL or invalid pointers then calls to
8272** sqlite3_keyword_name(N,Z,L) result in undefined behavior.
8273**
8274** The sqlite3_keyword_check(Z,L) interface checks to see whether or not
8275** the L-byte UTF8 identifier that Z points to is a keyword, returning non-zero
8276** if it is and zero if not.
8277**
8278** The parser used by SQLite is forgiving. It is often possible to use
8279** a keyword as an identifier as long as such use does not result in a
8280** parsing ambiguity. For example, the statement
8281** "CREATE TABLE BEGIN(REPLACE,PRAGMA,END);" is accepted by SQLite, and
8282** creates a new table named "BEGIN" with three columns named
8283** "REPLACE", "PRAGMA", and "END". Nevertheless, best practice is to avoid
8284** using keywords as identifiers. Common techniques used to avoid keyword
8285** name collisions include:
8286** <ul>
8287** <li> Put all identifier names inside double-quotes. This is the official
8288** SQL way to escape identifier names.
8289** <li> Put identifier names inside &#91;...&#93;. This is not standard SQL,
8290** but it is what SQL Server does and so lots of programmers use this
8291** technique.
8292** <li> Begin every identifier with the letter "Z" as no SQL keywords start
8293** with "Z".
8294** <li> Include a digit somewhere in every identifier name.
8295** </ul>
8296**
8297** Note that the number of keywords understood by SQLite can depend on
8298** compile-time options. For example, "VACUUM" is not a keyword if
8299** SQLite is compiled with the [-DSQLITE_OMIT_VACUUM] option. Also,
8300** new keywords may be added to future releases of SQLite.
8301*/
8302SQLITE_API int sqlite3_keyword_count(void);
8303SQLITE_API int sqlite3_keyword_name(int,const char**,int*);
8304SQLITE_API int sqlite3_keyword_check(const char*,int);
8305
8306/*
8307** CAPI3REF: Dynamic String Object
8308** KEYWORDS: {dynamic string}
8309**
8310** An instance of the sqlite3_str object contains a dynamically-sized
8311** string under construction.
8312**
8313** The lifecycle of an sqlite3_str object is as follows:
8314** <ol>
8315** <li> ^The sqlite3_str object is created using [sqlite3_str_new()].
8316** <li> ^Text is appended to the sqlite3_str object using various
8317** methods, such as [sqlite3_str_appendf()].
8318** <li> ^The sqlite3_str object is destroyed and the string it created
8319** is returned using the [sqlite3_str_finish()] interface.
8320** </ol>
8321*/
8322typedef struct sqlite3_str sqlite3_str;
8323
8324/*
8325** CAPI3REF: Create A New Dynamic String Object
8326** CONSTRUCTOR: sqlite3_str
8327**
8328** ^The [sqlite3_str_new(D)] interface allocates and initializes
8329** a new [sqlite3_str] object. To avoid memory leaks, the object returned by
8330** [sqlite3_str_new()] must be freed by a subsequent call to
8331** [sqlite3_str_finish(X)].
8332**
8333** ^The [sqlite3_str_new(D)] interface always returns a pointer to a
8334** valid [sqlite3_str] object, though in the event of an out-of-memory
8335** error the returned object might be a special singleton that will
8336** silently reject new text, always return SQLITE_NOMEM from
8337** [sqlite3_str_errcode()], always return 0 for
8338** [sqlite3_str_length()], and always return NULL from
8339** [sqlite3_str_finish(X)]. It is always safe to use the value
8340** returned by [sqlite3_str_new(D)] as the sqlite3_str parameter
8341** to any of the other [sqlite3_str] methods.
8342**
8343** The D parameter to [sqlite3_str_new(D)] may be NULL. If the
8344** D parameter in [sqlite3_str_new(D)] is not NULL, then the maximum
8345** length of the string contained in the [sqlite3_str] object will be
8346** the value set for [sqlite3_limit](D,[SQLITE_LIMIT_LENGTH]) instead
8347** of [SQLITE_MAX_LENGTH].
8348*/
8349SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3*);
8350
8351/*
8352** CAPI3REF: Finalize A Dynamic String
8353** DESTRUCTOR: sqlite3_str
8354**
8355** ^The [sqlite3_str_finish(X)] interface destroys the sqlite3_str object X
8356** and returns a pointer to a memory buffer obtained from [sqlite3_malloc64()]
8357** that contains the constructed string. The calling application should
8358** pass the returned value to [sqlite3_free()] to avoid a memory leak.
8359** ^The [sqlite3_str_finish(X)] interface may return a NULL pointer if any
8360** errors were encountered during construction of the string. ^The
8361** [sqlite3_str_finish(X)] interface will also return a NULL pointer if the
8362** string in [sqlite3_str] object X is zero bytes long.
8363*/
8364SQLITE_API char *sqlite3_str_finish(sqlite3_str*);
8365
8366/*
8367** CAPI3REF: Add Content To A Dynamic String
8368** METHOD: sqlite3_str
8369**
8370** These interfaces add content to an sqlite3_str object previously obtained
8371** from [sqlite3_str_new()].
8372**
8373** ^The [sqlite3_str_appendf(X,F,...)] and
8374** [sqlite3_str_vappendf(X,F,V)] interfaces uses the [built-in printf]
8375** functionality of SQLite to append formatted text onto the end of
8376** [sqlite3_str] object X.
8377**
8378** ^The [sqlite3_str_append(X,S,N)] method appends exactly N bytes from string S
8379** onto the end of the [sqlite3_str] object X. N must be non-negative.
8380** S must contain at least N non-zero bytes of content. To append a
8381** zero-terminated string in its entirety, use the [sqlite3_str_appendall()]
8382** method instead.
8383**
8384** ^The [sqlite3_str_appendall(X,S)] method appends the complete content of
8385** zero-terminated string S onto the end of [sqlite3_str] object X.
8386**
8387** ^The [sqlite3_str_appendchar(X,N,C)] method appends N copies of the
8388** single-byte character C onto the end of [sqlite3_str] object X.
8389** ^This method can be used, for example, to add whitespace indentation.
8390**
8391** ^The [sqlite3_str_reset(X)] method resets the string under construction
8392** inside [sqlite3_str] object X back to zero bytes in length.
8393**
8394** These methods do not return a result code. ^If an error occurs, that fact
8395** is recorded in the [sqlite3_str] object and can be recovered by a
8396** subsequent call to [sqlite3_str_errcode(X)].
8397*/
8398SQLITE_API void sqlite3_str_appendf(sqlite3_str*, const char *zFormat, ...);
8399SQLITE_API void sqlite3_str_vappendf(sqlite3_str*, const char *zFormat, va_list);
8400SQLITE_API void sqlite3_str_append(sqlite3_str*, const char *zIn, int N);
8401SQLITE_API void sqlite3_str_appendall(sqlite3_str*, const char *zIn);
8402SQLITE_API void sqlite3_str_appendchar(sqlite3_str*, int N, char C);
8403SQLITE_API void sqlite3_str_reset(sqlite3_str*);
8404
8405/*
8406** CAPI3REF: Status Of A Dynamic String
8407** METHOD: sqlite3_str
8408**
8409** These interfaces return the current status of an [sqlite3_str] object.
8410**
8411** ^If any prior errors have occurred while constructing the dynamic string
8412** in sqlite3_str X, then the [sqlite3_str_errcode(X)] method will return
8413** an appropriate error code. ^The [sqlite3_str_errcode(X)] method returns
8414** [SQLITE_NOMEM] following any out-of-memory error, or
8415** [SQLITE_TOOBIG] if the size of the dynamic string exceeds
8416** [SQLITE_MAX_LENGTH], or [SQLITE_OK] if there have been no errors.
8417**
8418** ^The [sqlite3_str_length(X)] method returns the current length, in bytes,
8419** of the dynamic string under construction in [sqlite3_str] object X.
8420** ^The length returned by [sqlite3_str_length(X)] does not include the
8421** zero-termination byte.
8422**
8423** ^The [sqlite3_str_value(X)] method returns a pointer to the current
8424** content of the dynamic string under construction in X. The value
8425** returned by [sqlite3_str_value(X)] is managed by the sqlite3_str object X
8426** and might be freed or altered by any subsequent method on the same
8427** [sqlite3_str] object. Applications must not used the pointer returned
8428** [sqlite3_str_value(X)] after any subsequent method call on the same
8429** object. ^Applications may change the content of the string returned
8430** by [sqlite3_str_value(X)] as long as they do not write into any bytes
8431** outside the range of 0 to [sqlite3_str_length(X)] and do not read or
8432** write any byte after any subsequent sqlite3_str method call.
8433*/
8434SQLITE_API int sqlite3_str_errcode(sqlite3_str*);
8435SQLITE_API int sqlite3_str_length(sqlite3_str*);
8436SQLITE_API char *sqlite3_str_value(sqlite3_str*);
8437
8438/*
8439** CAPI3REF: SQLite Runtime Status
8440**
8441** ^These interfaces are used to retrieve runtime status information
8442** about the performance of SQLite, and optionally to reset various
8443** highwater marks. ^The first argument is an integer code for
8444** the specific parameter to measure. ^(Recognized integer codes
8445** are of the form [status parameters | SQLITE_STATUS_...].)^
8446** ^The current value of the parameter is returned into *pCurrent.
8447** ^The highest recorded value is returned in *pHighwater. ^If the
8448** resetFlag is true, then the highest record value is reset after
8449** *pHighwater is written. ^(Some parameters do not record the highest
8450** value. For those parameters
8451** nothing is written into *pHighwater and the resetFlag is ignored.)^
8452** ^(Other parameters record only the highwater mark and not the current
8453** value. For these latter parameters nothing is written into *pCurrent.)^
8454**
8455** ^The sqlite3_status() and sqlite3_status64() routines return
8456** SQLITE_OK on success and a non-zero [error code] on failure.
8457**
8458** If either the current value or the highwater mark is too large to
8459** be represented by a 32-bit integer, then the values returned by
8460** sqlite3_status() are undefined.
8461**
8462** See also: [sqlite3_db_status()]
8463*/
8464SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
8465SQLITE_API int sqlite3_status64(
8466 int op,
8467 sqlite3_int64 *pCurrent,
8468 sqlite3_int64 *pHighwater,
8469 int resetFlag
8470);
8471
8472
8473/*
8474** CAPI3REF: Status Parameters
8475** KEYWORDS: {status parameters}
8476**
8477** These integer constants designate various run-time status parameters
8478** that can be returned by [sqlite3_status()].
8479**
8480** <dl>
8481** [[SQLITE_STATUS_MEMORY_USED]] ^(<dt>SQLITE_STATUS_MEMORY_USED</dt>
8482** <dd>This parameter is the current amount of memory checked out
8483** using [sqlite3_malloc()], either directly or indirectly. The
8484** figure includes calls made to [sqlite3_malloc()] by the application
8485** and internal memory usage by the SQLite library. Auxiliary page-cache
8486** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in
8487** this parameter. The amount returned is the sum of the allocation
8488** sizes as reported by the xSize method in [sqlite3_mem_methods].</dd>)^
8489**
8490** [[SQLITE_STATUS_MALLOC_SIZE]] ^(<dt>SQLITE_STATUS_MALLOC_SIZE</dt>
8491** <dd>This parameter records the largest memory allocation request
8492** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their
8493** internal equivalents). Only the value returned in the
8494** *pHighwater parameter to [sqlite3_status()] is of interest.
8495** The value written into the *pCurrent parameter is undefined.</dd>)^
8496**
8497** [[SQLITE_STATUS_MALLOC_COUNT]] ^(<dt>SQLITE_STATUS_MALLOC_COUNT</dt>
8498** <dd>This parameter records the number of separate memory allocations
8499** currently checked out.</dd>)^
8500**
8501** [[SQLITE_STATUS_PAGECACHE_USED]] ^(<dt>SQLITE_STATUS_PAGECACHE_USED</dt>
8502** <dd>This parameter returns the number of pages used out of the
8503** [pagecache memory allocator] that was configured using
8504** [SQLITE_CONFIG_PAGECACHE]. The
8505** value returned is in pages, not in bytes.</dd>)^
8506**
8507** [[SQLITE_STATUS_PAGECACHE_OVERFLOW]]
8508** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt>
8509** <dd>This parameter returns the number of bytes of page cache
8510** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE]
8511** buffer and where forced to overflow to [sqlite3_malloc()]. The
8512** returned value includes allocations that overflowed because they
8513** where too large (they were larger than the "sz" parameter to
8514** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because
8515** no space was left in the page cache.</dd>)^
8516**
8517** [[SQLITE_STATUS_PAGECACHE_SIZE]] ^(<dt>SQLITE_STATUS_PAGECACHE_SIZE</dt>
8518** <dd>This parameter records the largest memory allocation request
8519** handed to the [pagecache memory allocator]. Only the value returned in the
8520** *pHighwater parameter to [sqlite3_status()] is of interest.
8521** The value written into the *pCurrent parameter is undefined.</dd>)^
8522**
8523** [[SQLITE_STATUS_SCRATCH_USED]] <dt>SQLITE_STATUS_SCRATCH_USED</dt>
8524** <dd>No longer used.</dd>
8525**
8526** [[SQLITE_STATUS_SCRATCH_OVERFLOW]] ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt>
8527** <dd>No longer used.</dd>
8528**
8529** [[SQLITE_STATUS_SCRATCH_SIZE]] <dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
8530** <dd>No longer used.</dd>
8531**
8532** [[SQLITE_STATUS_PARSER_STACK]] ^(<dt>SQLITE_STATUS_PARSER_STACK</dt>
8533** <dd>The *pHighwater parameter records the deepest parser stack.
8534** The *pCurrent value is undefined. The *pHighwater value is only
8535** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].</dd>)^
8536** </dl>
8537**
8538** New status parameters may be added from time to time.
8539*/
8540#define SQLITE_STATUS_MEMORY_USED 0
8541#define SQLITE_STATUS_PAGECACHE_USED 1
8542#define SQLITE_STATUS_PAGECACHE_OVERFLOW 2
8543#define SQLITE_STATUS_SCRATCH_USED 3 /* NOT USED */
8544#define SQLITE_STATUS_SCRATCH_OVERFLOW 4 /* NOT USED */
8545#define SQLITE_STATUS_MALLOC_SIZE 5
8546#define SQLITE_STATUS_PARSER_STACK 6
8547#define SQLITE_STATUS_PAGECACHE_SIZE 7
8548#define SQLITE_STATUS_SCRATCH_SIZE 8 /* NOT USED */
8549#define SQLITE_STATUS_MALLOC_COUNT 9
8550
8551/*
8552** CAPI3REF: Database Connection Status
8553** METHOD: sqlite3
8554**
8555** ^This interface is used to retrieve runtime status information
8556** about a single [database connection]. ^The first argument is the
8557** database connection object to be interrogated. ^The second argument
8558** is an integer constant, taken from the set of
8559** [SQLITE_DBSTATUS options], that
8560** determines the parameter to interrogate. The set of
8561** [SQLITE_DBSTATUS options] is likely
8562** to grow in future releases of SQLite.
8563**
8564** ^The current value of the requested parameter is written into *pCur
8565** and the highest instantaneous value is written into *pHiwtr. ^If
8566** the resetFlg is true, then the highest instantaneous value is
8567** reset back down to the current value.
8568**
8569** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a
8570** non-zero [error code] on failure.
8571**
8572** See also: [sqlite3_status()] and [sqlite3_stmt_status()].
8573*/
8574SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
8575
8576/*
8577** CAPI3REF: Status Parameters for database connections
8578** KEYWORDS: {SQLITE_DBSTATUS options}
8579**
8580** These constants are the available integer "verbs" that can be passed as
8581** the second argument to the [sqlite3_db_status()] interface.
8582**
8583** New verbs may be added in future releases of SQLite. Existing verbs
8584** might be discontinued. Applications should check the return code from
8585** [sqlite3_db_status()] to make sure that the call worked.
8586** The [sqlite3_db_status()] interface will return a non-zero error code
8587** if a discontinued or unsupported verb is invoked.
8588**
8589** <dl>
8590** [[SQLITE_DBSTATUS_LOOKASIDE_USED]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_USED</dt>
8591** <dd>This parameter returns the number of lookaside memory slots currently
8592** checked out.</dd>)^
8593**
8594** [[SQLITE_DBSTATUS_LOOKASIDE_HIT]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt>
8595** <dd>This parameter returns the number of malloc attempts that were
8596** satisfied using lookaside memory. Only the high-water value is meaningful;
8597** the current value is always zero.)^
8598**
8599** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE]]
8600** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt>
8601** <dd>This parameter returns the number malloc attempts that might have
8602** been satisfied using lookaside memory but failed due to the amount of
8603** memory requested being larger than the lookaside slot size.
8604** Only the high-water value is meaningful;
8605** the current value is always zero.)^
8606**
8607** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL]]
8608** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt>
8609** <dd>This parameter returns the number malloc attempts that might have
8610** been satisfied using lookaside memory but failed due to all lookaside
8611** memory already being in use.
8612** Only the high-water value is meaningful;
8613** the current value is always zero.)^
8614**
8615** [[SQLITE_DBSTATUS_CACHE_USED]] ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
8616** <dd>This parameter returns the approximate number of bytes of heap
8617** memory used by all pager caches associated with the database connection.)^
8618** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
8619**
8620** [[SQLITE_DBSTATUS_CACHE_USED_SHARED]]
8621** ^(<dt>SQLITE_DBSTATUS_CACHE_USED_SHARED</dt>
8622** <dd>This parameter is similar to DBSTATUS_CACHE_USED, except that if a
8623** pager cache is shared between two or more connections the bytes of heap
8624** memory used by that pager cache is divided evenly between the attached
8625** connections.)^ In other words, if none of the pager caches associated
8626** with the database connection are shared, this request returns the same
8627** value as DBSTATUS_CACHE_USED. Or, if one or more or the pager caches are
8628** shared, the value returned by this call will be smaller than that returned
8629** by DBSTATUS_CACHE_USED. ^The highwater mark associated with
8630** SQLITE_DBSTATUS_CACHE_USED_SHARED is always 0.
8631**
8632** [[SQLITE_DBSTATUS_SCHEMA_USED]] ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt>
8633** <dd>This parameter returns the approximate number of bytes of heap
8634** memory used to store the schema for all databases associated
8635** with the connection - main, temp, and any [ATTACH]-ed databases.)^
8636** ^The full amount of memory used by the schemas is reported, even if the
8637** schema memory is shared with other database connections due to
8638** [shared cache mode] being enabled.
8639** ^The highwater mark associated with SQLITE_DBSTATUS_SCHEMA_USED is always 0.
8640**
8641** [[SQLITE_DBSTATUS_STMT_USED]] ^(<dt>SQLITE_DBSTATUS_STMT_USED</dt>
8642** <dd>This parameter returns the approximate number of bytes of heap
8643** and lookaside memory used by all prepared statements associated with
8644** the database connection.)^
8645** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
8646** </dd>
8647**
8648** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(<dt>SQLITE_DBSTATUS_CACHE_HIT</dt>
8649** <dd>This parameter returns the number of pager cache hits that have
8650** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT
8651** is always 0.
8652** </dd>
8653**
8654** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(<dt>SQLITE_DBSTATUS_CACHE_MISS</dt>
8655** <dd>This parameter returns the number of pager cache misses that have
8656** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS
8657** is always 0.
8658** </dd>
8659**
8660** [[SQLITE_DBSTATUS_CACHE_WRITE]] ^(<dt>SQLITE_DBSTATUS_CACHE_WRITE</dt>
8661** <dd>This parameter returns the number of dirty cache entries that have
8662** been written to disk. Specifically, the number of pages written to the
8663** wal file in wal mode databases, or the number of pages written to the
8664** database file in rollback mode databases. Any pages written as part of
8665** transaction rollback or database recovery operations are not included.
8666** If an IO or other error occurs while writing a page to disk, the effect
8667** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
8668** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
8669** </dd>
8670**
8671** [[SQLITE_DBSTATUS_CACHE_SPILL]] ^(<dt>SQLITE_DBSTATUS_CACHE_SPILL</dt>
8672** <dd>This parameter returns the number of dirty cache entries that have
8673** been written to disk in the middle of a transaction due to the page
8674** cache overflowing. Transactions are more efficient if they are written
8675** to disk all at once. When pages spill mid-transaction, that introduces
8676** additional overhead. This parameter can be used help identify
8677** inefficiencies that can be resolved by increasing the cache size.
8678** </dd>
8679**
8680** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
8681** <dd>This parameter returns zero for the current value if and only if
8682** all foreign key constraints (deferred or immediate) have been
8683** resolved.)^ ^The highwater mark is always 0.
8684** </dd>
8685** </dl>
8686*/
8687#define SQLITE_DBSTATUS_LOOKASIDE_USED 0
8688#define SQLITE_DBSTATUS_CACHE_USED 1
8689#define SQLITE_DBSTATUS_SCHEMA_USED 2
8690#define SQLITE_DBSTATUS_STMT_USED 3
8691#define SQLITE_DBSTATUS_LOOKASIDE_HIT 4
8692#define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE 5
8693#define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL 6
8694#define SQLITE_DBSTATUS_CACHE_HIT 7
8695#define SQLITE_DBSTATUS_CACHE_MISS 8
8696#define SQLITE_DBSTATUS_CACHE_WRITE 9
8697#define SQLITE_DBSTATUS_DEFERRED_FKS 10
8698#define SQLITE_DBSTATUS_CACHE_USED_SHARED 11
8699#define SQLITE_DBSTATUS_CACHE_SPILL 12
8700#define SQLITE_DBSTATUS_MAX 12 /* Largest defined DBSTATUS */
8701
8702
8703/*
8704** CAPI3REF: Prepared Statement Status
8705** METHOD: sqlite3_stmt
8706**
8707** ^(Each prepared statement maintains various
8708** [SQLITE_STMTSTATUS counters] that measure the number
8709** of times it has performed specific operations.)^ These counters can
8710** be used to monitor the performance characteristics of the prepared
8711** statements. For example, if the number of table steps greatly exceeds
8712** the number of table searches or result rows, that would tend to indicate
8713** that the prepared statement is using a full table scan rather than
8714** an index.
8715**
8716** ^(This interface is used to retrieve and reset counter values from
8717** a [prepared statement]. The first argument is the prepared statement
8718** object to be interrogated. The second argument
8719** is an integer code for a specific [SQLITE_STMTSTATUS counter]
8720** to be interrogated.)^
8721** ^The current value of the requested counter is returned.
8722** ^If the resetFlg is true, then the counter is reset to zero after this
8723** interface call returns.
8724**
8725** See also: [sqlite3_status()] and [sqlite3_db_status()].
8726*/
8727SQLITE_API int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg);
8728
8729/*
8730** CAPI3REF: Status Parameters for prepared statements
8731** KEYWORDS: {SQLITE_STMTSTATUS counter} {SQLITE_STMTSTATUS counters}
8732**
8733** These preprocessor macros define integer codes that name counter
8734** values associated with the [sqlite3_stmt_status()] interface.
8735** The meanings of the various counters are as follows:
8736**
8737** <dl>
8738** [[SQLITE_STMTSTATUS_FULLSCAN_STEP]] <dt>SQLITE_STMTSTATUS_FULLSCAN_STEP</dt>
8739** <dd>^This is the number of times that SQLite has stepped forward in
8740** a table as part of a full table scan. Large numbers for this counter
8741** may indicate opportunities for performance improvement through
8742** careful use of indices.</dd>
8743**
8744** [[SQLITE_STMTSTATUS_SORT]] <dt>SQLITE_STMTSTATUS_SORT</dt>
8745** <dd>^This is the number of sort operations that have occurred.
8746** A non-zero value in this counter may indicate an opportunity to
8747** improvement performance through careful use of indices.</dd>
8748**
8749** [[SQLITE_STMTSTATUS_AUTOINDEX]] <dt>SQLITE_STMTSTATUS_AUTOINDEX</dt>
8750** <dd>^This is the number of rows inserted into transient indices that
8751** were created automatically in order to help joins run faster.
8752** A non-zero value in this counter may indicate an opportunity to
8753** improvement performance by adding permanent indices that do not
8754** need to be reinitialized each time the statement is run.</dd>
8755**
8756** [[SQLITE_STMTSTATUS_VM_STEP]] <dt>SQLITE_STMTSTATUS_VM_STEP</dt>
8757** <dd>^This is the number of virtual machine operations executed
8758** by the prepared statement if that number is less than or equal
8759** to 2147483647. The number of virtual machine operations can be
8760** used as a proxy for the total work done by the prepared statement.
8761** If the number of virtual machine operations exceeds 2147483647
8762** then the value returned by this statement status code is undefined.
8763**
8764** [[SQLITE_STMTSTATUS_REPREPARE]] <dt>SQLITE_STMTSTATUS_REPREPARE</dt>
8765** <dd>^This is the number of times that the prepare statement has been
8766** automatically regenerated due to schema changes or changes to
8767** [bound parameters] that might affect the query plan.
8768**
8769** [[SQLITE_STMTSTATUS_RUN]] <dt>SQLITE_STMTSTATUS_RUN</dt>
8770** <dd>^This is the number of times that the prepared statement has
8771** been run. A single "run" for the purposes of this counter is one
8772** or more calls to [sqlite3_step()] followed by a call to [sqlite3_reset()].
8773** The counter is incremented on the first [sqlite3_step()] call of each
8774** cycle.
8775**
8776** [[SQLITE_STMTSTATUS_MEMUSED]] <dt>SQLITE_STMTSTATUS_MEMUSED</dt>
8777** <dd>^This is the approximate number of bytes of heap memory
8778** used to store the prepared statement. ^This value is not actually
8779** a counter, and so the resetFlg parameter to sqlite3_stmt_status()
8780** is ignored when the opcode is SQLITE_STMTSTATUS_MEMUSED.
8781** </dd>
8782** </dl>
8783*/
8784#define SQLITE_STMTSTATUS_FULLSCAN_STEP 1
8785#define SQLITE_STMTSTATUS_SORT 2
8786#define SQLITE_STMTSTATUS_AUTOINDEX 3
8787#define SQLITE_STMTSTATUS_VM_STEP 4
8788#define SQLITE_STMTSTATUS_REPREPARE 5
8789#define SQLITE_STMTSTATUS_RUN 6
8790#define SQLITE_STMTSTATUS_MEMUSED 99
8791
8792/*
8793** CAPI3REF: Custom Page Cache Object
8794**
8795** The sqlite3_pcache type is opaque. It is implemented by
8796** the pluggable module. The SQLite core has no knowledge of
8797** its size or internal structure and never deals with the
8798** sqlite3_pcache object except by holding and passing pointers
8799** to the object.
8800**
8801** See [sqlite3_pcache_methods2] for additional information.
8802*/
8803typedef struct sqlite3_pcache sqlite3_pcache;
8804
8805/*
8806** CAPI3REF: Custom Page Cache Object
8807**
8808** The sqlite3_pcache_page object represents a single page in the
8809** page cache. The page cache will allocate instances of this
8810** object. Various methods of the page cache use pointers to instances
8811** of this object as parameters or as their return value.
8812**
8813** See [sqlite3_pcache_methods2] for additional information.
8814*/
8815typedef struct sqlite3_pcache_page sqlite3_pcache_page;
8816struct sqlite3_pcache_page {
8817 void *pBuf; /* The content of the page */
8818 void *pExtra; /* Extra information associated with the page */
8819};
8820
8821/*
8822** CAPI3REF: Application Defined Page Cache.
8823** KEYWORDS: {page cache}
8824**
8825** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE2], ...) interface can
8826** register an alternative page cache implementation by passing in an
8827** instance of the sqlite3_pcache_methods2 structure.)^
8828** In many applications, most of the heap memory allocated by
8829** SQLite is used for the page cache.
8830** By implementing a
8831** custom page cache using this API, an application can better control
8832** the amount of memory consumed by SQLite, the way in which
8833** that memory is allocated and released, and the policies used to
8834** determine exactly which parts of a database file are cached and for
8835** how long.
8836**
8837** The alternative page cache mechanism is an
8838** extreme measure that is only needed by the most demanding applications.
8839** The built-in page cache is recommended for most uses.
8840**
8841** ^(The contents of the sqlite3_pcache_methods2 structure are copied to an
8842** internal buffer by SQLite within the call to [sqlite3_config]. Hence
8843** the application may discard the parameter after the call to
8844** [sqlite3_config()] returns.)^
8845**
8846** [[the xInit() page cache method]]
8847** ^(The xInit() method is called once for each effective
8848** call to [sqlite3_initialize()])^
8849** (usually only once during the lifetime of the process). ^(The xInit()
8850** method is passed a copy of the sqlite3_pcache_methods2.pArg value.)^
8851** The intent of the xInit() method is to set up global data structures
8852** required by the custom page cache implementation.
8853** ^(If the xInit() method is NULL, then the
8854** built-in default page cache is used instead of the application defined
8855** page cache.)^
8856**
8857** [[the xShutdown() page cache method]]
8858** ^The xShutdown() method is called by [sqlite3_shutdown()].
8859** It can be used to clean up
8860** any outstanding resources before process shutdown, if required.
8861** ^The xShutdown() method may be NULL.
8862**
8863** ^SQLite automatically serializes calls to the xInit method,
8864** so the xInit method need not be threadsafe. ^The
8865** xShutdown method is only called from [sqlite3_shutdown()] so it does
8866** not need to be threadsafe either. All other methods must be threadsafe
8867** in multithreaded applications.
8868**
8869** ^SQLite will never invoke xInit() more than once without an intervening
8870** call to xShutdown().
8871**
8872** [[the xCreate() page cache methods]]
8873** ^SQLite invokes the xCreate() method to construct a new cache instance.
8874** SQLite will typically create one cache instance for each open database file,
8875** though this is not guaranteed. ^The
8876** first parameter, szPage, is the size in bytes of the pages that must
8877** be allocated by the cache. ^szPage will always a power of two. ^The
8878** second parameter szExtra is a number of bytes of extra storage
8879** associated with each page cache entry. ^The szExtra parameter will
8880** a number less than 250. SQLite will use the
8881** extra szExtra bytes on each page to store metadata about the underlying
8882** database page on disk. The value passed into szExtra depends
8883** on the SQLite version, the target platform, and how SQLite was compiled.
8884** ^The third argument to xCreate(), bPurgeable, is true if the cache being
8885** created will be used to cache database pages of a file stored on disk, or
8886** false if it is used for an in-memory database. The cache implementation
8887** does not have to do anything special based with the value of bPurgeable;
8888** it is purely advisory. ^On a cache where bPurgeable is false, SQLite will
8889** never invoke xUnpin() except to deliberately delete a page.
8890** ^In other words, calls to xUnpin() on a cache with bPurgeable set to
8891** false will always have the "discard" flag set to true.
8892** ^Hence, a cache created with bPurgeable false will
8893** never contain any unpinned pages.
8894**
8895** [[the xCachesize() page cache method]]
8896** ^(The xCachesize() method may be called at any time by SQLite to set the
8897** suggested maximum cache-size (number of pages stored by) the cache
8898** instance passed as the first argument. This is the value configured using
8899** the SQLite "[PRAGMA cache_size]" command.)^ As with the bPurgeable
8900** parameter, the implementation is not required to do anything with this
8901** value; it is advisory only.
8902**
8903** [[the xPagecount() page cache methods]]
8904** The xPagecount() method must return the number of pages currently
8905** stored in the cache, both pinned and unpinned.
8906**
8907** [[the xFetch() page cache methods]]
8908** The xFetch() method locates a page in the cache and returns a pointer to
8909** an sqlite3_pcache_page object associated with that page, or a NULL pointer.
8910** The pBuf element of the returned sqlite3_pcache_page object will be a
8911** pointer to a buffer of szPage bytes used to store the content of a
8912** single database page. The pExtra element of sqlite3_pcache_page will be
8913** a pointer to the szExtra bytes of extra storage that SQLite has requested
8914** for each entry in the page cache.
8915**
8916** The page to be fetched is determined by the key. ^The minimum key value
8917** is 1. After it has been retrieved using xFetch, the page is considered
8918** to be "pinned".
8919**
8920** If the requested page is already in the page cache, then the page cache
8921** implementation must return a pointer to the page buffer with its content
8922** intact. If the requested page is not already in the cache, then the
8923** cache implementation should use the value of the createFlag
8924** parameter to help it determined what action to take:
8925**
8926** <table border=1 width=85% align=center>
8927** <tr><th> createFlag <th> Behavior when page is not already in cache
8928** <tr><td> 0 <td> Do not allocate a new page. Return NULL.
8929** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so.
8930** Otherwise return NULL.
8931** <tr><td> 2 <td> Make every effort to allocate a new page. Only return
8932** NULL if allocating a new page is effectively impossible.
8933** </table>
8934**
8935** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1. SQLite
8936** will only use a createFlag of 2 after a prior call with a createFlag of 1
8937** failed.)^ In between the xFetch() calls, SQLite may
8938** attempt to unpin one or more cache pages by spilling the content of
8939** pinned pages to disk and synching the operating system disk cache.
8940**
8941** [[the xUnpin() page cache method]]
8942** ^xUnpin() is called by SQLite with a pointer to a currently pinned page
8943** as its second argument. If the third parameter, discard, is non-zero,
8944** then the page must be evicted from the cache.
8945** ^If the discard parameter is
8946** zero, then the page may be discarded or retained at the discretion of
8947** page cache implementation. ^The page cache implementation
8948** may choose to evict unpinned pages at any time.
8949**
8950** The cache must not perform any reference counting. A single
8951** call to xUnpin() unpins the page regardless of the number of prior calls
8952** to xFetch().
8953**
8954** [[the xRekey() page cache methods]]
8955** The xRekey() method is used to change the key value associated with the
8956** page passed as the second argument. If the cache
8957** previously contains an entry associated with newKey, it must be
8958** discarded. ^Any prior cache entry associated with newKey is guaranteed not
8959** to be pinned.
8960**
8961** When SQLite calls the xTruncate() method, the cache must discard all
8962** existing cache entries with page numbers (keys) greater than or equal
8963** to the value of the iLimit parameter passed to xTruncate(). If any
8964** of these pages are pinned, they are implicitly unpinned, meaning that
8965** they can be safely discarded.
8966**
8967** [[the xDestroy() page cache method]]
8968** ^The xDestroy() method is used to delete a cache allocated by xCreate().
8969** All resources associated with the specified cache should be freed. ^After
8970** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
8971** handle invalid, and will not use it with any other sqlite3_pcache_methods2
8972** functions.
8973**
8974** [[the xShrink() page cache method]]
8975** ^SQLite invokes the xShrink() method when it wants the page cache to
8976** free up as much of heap memory as possible. The page cache implementation
8977** is not obligated to free any memory, but well-behaved implementations should
8978** do their best.
8979*/
8980typedef struct sqlite3_pcache_methods2 sqlite3_pcache_methods2;
8981struct sqlite3_pcache_methods2 {
8982 int iVersion;
8983 void *pArg;
8984 int (*xInit)(void*);
8985 void (*xShutdown)(void*);
8986 sqlite3_pcache *(*xCreate)(int szPage, int szExtra, int bPurgeable);
8987 void (*xCachesize)(sqlite3_pcache*, int nCachesize);
8988 int (*xPagecount)(sqlite3_pcache*);
8989 sqlite3_pcache_page *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
8990 void (*xUnpin)(sqlite3_pcache*, sqlite3_pcache_page*, int discard);
8991 void (*xRekey)(sqlite3_pcache*, sqlite3_pcache_page*,
8992 unsigned oldKey, unsigned newKey);
8993 void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
8994 void (*xDestroy)(sqlite3_pcache*);
8995 void (*xShrink)(sqlite3_pcache*);
8996};
8997
8998/*
8999** This is the obsolete pcache_methods object that has now been replaced
9000** by sqlite3_pcache_methods2. This object is not used by SQLite. It is
9001** retained in the header file for backwards compatibility only.
9002*/
9003typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
9004struct sqlite3_pcache_methods {
9005 void *pArg;
9006 int (*xInit)(void*);
9007 void (*xShutdown)(void*);
9008 sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable);
9009 void (*xCachesize)(sqlite3_pcache*, int nCachesize);
9010 int (*xPagecount)(sqlite3_pcache*);
9011 void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
9012 void (*xUnpin)(sqlite3_pcache*, void*, int discard);
9013 void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
9014 void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
9015 void (*xDestroy)(sqlite3_pcache*);
9016};
9017
9018
9019/*
9020** CAPI3REF: Online Backup Object
9021**
9022** The sqlite3_backup object records state information about an ongoing
9023** online backup operation. ^The sqlite3_backup object is created by
9024** a call to [sqlite3_backup_init()] and is destroyed by a call to
9025** [sqlite3_backup_finish()].
9026**
9027** See Also: [Using the SQLite Online Backup API]
9028*/
9029typedef struct sqlite3_backup sqlite3_backup;
9030
9031/*
9032** CAPI3REF: Online Backup API.
9033**
9034** The backup API copies the content of one database into another.
9035** It is useful either for creating backups of databases or
9036** for copying in-memory databases to or from persistent files.
9037**
9038** See Also: [Using the SQLite Online Backup API]
9039**
9040** ^SQLite holds a write transaction open on the destination database file
9041** for the duration of the backup operation.
9042** ^The source database is read-locked only while it is being read;
9043** it is not locked continuously for the entire backup operation.
9044** ^Thus, the backup may be performed on a live source database without
9045** preventing other database connections from
9046** reading or writing to the source database while the backup is underway.
9047**
9048** ^(To perform a backup operation:
9049** <ol>
9050** <li><b>sqlite3_backup_init()</b> is called once to initialize the
9051** backup,
9052** <li><b>sqlite3_backup_step()</b> is called one or more times to transfer
9053** the data between the two databases, and finally
9054** <li><b>sqlite3_backup_finish()</b> is called to release all resources
9055** associated with the backup operation.
9056** </ol>)^
9057** There should be exactly one call to sqlite3_backup_finish() for each
9058** successful call to sqlite3_backup_init().
9059**
9060** [[sqlite3_backup_init()]] <b>sqlite3_backup_init()</b>
9061**
9062** ^The D and N arguments to sqlite3_backup_init(D,N,S,M) are the
9063** [database connection] associated with the destination database
9064** and the database name, respectively.
9065** ^The database name is "main" for the main database, "temp" for the
9066** temporary database, or the name specified after the AS keyword in
9067** an [ATTACH] statement for an attached database.
9068** ^The S and M arguments passed to
9069** sqlite3_backup_init(D,N,S,M) identify the [database connection]
9070** and database name of the source database, respectively.
9071** ^The source and destination [database connections] (parameters S and D)
9072** must be different or else sqlite3_backup_init(D,N,S,M) will fail with
9073** an error.
9074**
9075** ^A call to sqlite3_backup_init() will fail, returning NULL, if
9076** there is already a read or read-write transaction open on the
9077** destination database.
9078**
9079** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is
9080** returned and an error code and error message are stored in the
9081** destination [database connection] D.
9082** ^The error code and message for the failed call to sqlite3_backup_init()
9083** can be retrieved using the [sqlite3_errcode()], [sqlite3_errmsg()], and/or
9084** [sqlite3_errmsg16()] functions.
9085** ^A successful call to sqlite3_backup_init() returns a pointer to an
9086** [sqlite3_backup] object.
9087** ^The [sqlite3_backup] object may be used with the sqlite3_backup_step() and
9088** sqlite3_backup_finish() functions to perform the specified backup
9089** operation.
9090**
9091** [[sqlite3_backup_step()]] <b>sqlite3_backup_step()</b>
9092**
9093** ^Function sqlite3_backup_step(B,N) will copy up to N pages between
9094** the source and destination databases specified by [sqlite3_backup] object B.
9095** ^If N is negative, all remaining source pages are copied.
9096** ^If sqlite3_backup_step(B,N) successfully copies N pages and there
9097** are still more pages to be copied, then the function returns [SQLITE_OK].
9098** ^If sqlite3_backup_step(B,N) successfully finishes copying all pages
9099** from source to destination, then it returns [SQLITE_DONE].
9100** ^If an error occurs while running sqlite3_backup_step(B,N),
9101** then an [error code] is returned. ^As well as [SQLITE_OK] and
9102** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
9103** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
9104** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
9105**
9106** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if
9107** <ol>
9108** <li> the destination database was opened read-only, or
9109** <li> the destination database is using write-ahead-log journaling
9110** and the destination and source page sizes differ, or
9111** <li> the destination database is an in-memory database and the
9112** destination and source page sizes differ.
9113** </ol>)^
9114**
9115** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
9116** the [sqlite3_busy_handler | busy-handler function]
9117** is invoked (if one is specified). ^If the
9118** busy-handler returns non-zero before the lock is available, then
9119** [SQLITE_BUSY] is returned to the caller. ^In this case the call to
9120** sqlite3_backup_step() can be retried later. ^If the source
9121** [database connection]
9122** is being used to write to the source database when sqlite3_backup_step()
9123** is called, then [SQLITE_LOCKED] is returned immediately. ^Again, in this
9124** case the call to sqlite3_backup_step() can be retried later on. ^(If
9125** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX], [SQLITE_NOMEM], or
9126** [SQLITE_READONLY] is returned, then
9127** there is no point in retrying the call to sqlite3_backup_step(). These
9128** errors are considered fatal.)^ The application must accept
9129** that the backup operation has failed and pass the backup operation handle
9130** to the sqlite3_backup_finish() to release associated resources.
9131**
9132** ^The first call to sqlite3_backup_step() obtains an exclusive lock
9133** on the destination file. ^The exclusive lock is not released until either
9134** sqlite3_backup_finish() is called or the backup operation is complete
9135** and sqlite3_backup_step() returns [SQLITE_DONE]. ^Every call to
9136** sqlite3_backup_step() obtains a [shared lock] on the source database that
9137** lasts for the duration of the sqlite3_backup_step() call.
9138** ^Because the source database is not locked between calls to
9139** sqlite3_backup_step(), the source database may be modified mid-way
9140** through the backup process. ^If the source database is modified by an
9141** external process or via a database connection other than the one being
9142** used by the backup operation, then the backup will be automatically
9143** restarted by the next call to sqlite3_backup_step(). ^If the source
9144** database is modified by the using the same database connection as is used
9145** by the backup operation, then the backup database is automatically
9146** updated at the same time.
9147**
9148** [[sqlite3_backup_finish()]] <b>sqlite3_backup_finish()</b>
9149**
9150** When sqlite3_backup_step() has returned [SQLITE_DONE], or when the
9151** application wishes to abandon the backup operation, the application
9152** should destroy the [sqlite3_backup] by passing it to sqlite3_backup_finish().
9153** ^The sqlite3_backup_finish() interfaces releases all
9154** resources associated with the [sqlite3_backup] object.
9155** ^If sqlite3_backup_step() has not yet returned [SQLITE_DONE], then any
9156** active write-transaction on the destination database is rolled back.
9157** The [sqlite3_backup] object is invalid
9158** and may not be used following a call to sqlite3_backup_finish().
9159**
9160** ^The value returned by sqlite3_backup_finish is [SQLITE_OK] if no
9161** sqlite3_backup_step() errors occurred, regardless or whether or not
9162** sqlite3_backup_step() completed.
9163** ^If an out-of-memory condition or IO error occurred during any prior
9164** sqlite3_backup_step() call on the same [sqlite3_backup] object, then
9165** sqlite3_backup_finish() returns the corresponding [error code].
9166**
9167** ^A return of [SQLITE_BUSY] or [SQLITE_LOCKED] from sqlite3_backup_step()
9168** is not a permanent error and does not affect the return value of
9169** sqlite3_backup_finish().
9170**
9171** [[sqlite3_backup_remaining()]] [[sqlite3_backup_pagecount()]]
9172** <b>sqlite3_backup_remaining() and sqlite3_backup_pagecount()</b>
9173**
9174** ^The sqlite3_backup_remaining() routine returns the number of pages still
9175** to be backed up at the conclusion of the most recent sqlite3_backup_step().
9176** ^The sqlite3_backup_pagecount() routine returns the total number of pages
9177** in the source database at the conclusion of the most recent
9178** sqlite3_backup_step().
9179** ^(The values returned by these functions are only updated by
9180** sqlite3_backup_step(). If the source database is modified in a way that
9181** changes the size of the source database or the number of pages remaining,
9182** those changes are not reflected in the output of sqlite3_backup_pagecount()
9183** and sqlite3_backup_remaining() until after the next
9184** sqlite3_backup_step().)^
9185**
9186** <b>Concurrent Usage of Database Handles</b>
9187**
9188** ^The source [database connection] may be used by the application for other
9189** purposes while a backup operation is underway or being initialized.
9190** ^If SQLite is compiled and configured to support threadsafe database
9191** connections, then the source database connection may be used concurrently
9192** from within other threads.
9193**
9194** However, the application must guarantee that the destination
9195** [database connection] is not passed to any other API (by any thread) after
9196** sqlite3_backup_init() is called and before the corresponding call to
9197** sqlite3_backup_finish(). SQLite does not currently check to see
9198** if the application incorrectly accesses the destination [database connection]
9199** and so no error code is reported, but the operations may malfunction
9200** nevertheless. Use of the destination database connection while a
9201** backup is in progress might also also cause a mutex deadlock.
9202**
9203** If running in [shared cache mode], the application must
9204** guarantee that the shared cache used by the destination database
9205** is not accessed while the backup is running. In practice this means
9206** that the application must guarantee that the disk file being
9207** backed up to is not accessed by any connection within the process,
9208** not just the specific connection that was passed to sqlite3_backup_init().
9209**
9210** The [sqlite3_backup] object itself is partially threadsafe. Multiple
9211** threads may safely make multiple concurrent calls to sqlite3_backup_step().
9212** However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount()
9213** APIs are not strictly speaking threadsafe. If they are invoked at the
9214** same time as another thread is invoking sqlite3_backup_step() it is
9215** possible that they return invalid values.
9216*/
9217SQLITE_API sqlite3_backup *sqlite3_backup_init(
9218 sqlite3 *pDest, /* Destination database handle */
9219 const char *zDestName, /* Destination database name */
9220 sqlite3 *pSource, /* Source database handle */
9221 const char *zSourceName /* Source database name */
9222);
9223SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage);
9224SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p);
9225SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p);
9226SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p);
9227
9228/*
9229** CAPI3REF: Unlock Notification
9230** METHOD: sqlite3
9231**
9232** ^When running in shared-cache mode, a database operation may fail with
9233** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
9234** individual tables within the shared-cache cannot be obtained. See
9235** [SQLite Shared-Cache Mode] for a description of shared-cache locking.
9236** ^This API may be used to register a callback that SQLite will invoke
9237** when the connection currently holding the required lock relinquishes it.
9238** ^This API is only available if the library was compiled with the
9239** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
9240**
9241** See Also: [Using the SQLite Unlock Notification Feature].
9242**
9243** ^Shared-cache locks are released when a database connection concludes
9244** its current transaction, either by committing it or rolling it back.
9245**
9246** ^When a connection (known as the blocked connection) fails to obtain a
9247** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
9248** identity of the database connection (the blocking connection) that
9249** has locked the required resource is stored internally. ^After an
9250** application receives an SQLITE_LOCKED error, it may call the
9251** sqlite3_unlock_notify() method with the blocked connection handle as
9252** the first argument to register for a callback that will be invoked
9253** when the blocking connections current transaction is concluded. ^The
9254** callback is invoked from within the [sqlite3_step] or [sqlite3_close]
9255** call that concludes the blocking connection's transaction.
9256**
9257** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
9258** there is a chance that the blocking connection will have already
9259** concluded its transaction by the time sqlite3_unlock_notify() is invoked.
9260** If this happens, then the specified callback is invoked immediately,
9261** from within the call to sqlite3_unlock_notify().)^
9262**
9263** ^If the blocked connection is attempting to obtain a write-lock on a
9264** shared-cache table, and more than one other connection currently holds
9265** a read-lock on the same table, then SQLite arbitrarily selects one of
9266** the other connections to use as the blocking connection.
9267**
9268** ^(There may be at most one unlock-notify callback registered by a
9269** blocked connection. If sqlite3_unlock_notify() is called when the
9270** blocked connection already has a registered unlock-notify callback,
9271** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is
9272** called with a NULL pointer as its second argument, then any existing
9273** unlock-notify callback is canceled. ^The blocked connections
9274** unlock-notify callback may also be canceled by closing the blocked
9275** connection using [sqlite3_close()].
9276**
9277** The unlock-notify callback is not reentrant. If an application invokes
9278** any sqlite3_xxx API functions from within an unlock-notify callback, a
9279** crash or deadlock may be the result.
9280**
9281** ^Unless deadlock is detected (see below), sqlite3_unlock_notify() always
9282** returns SQLITE_OK.
9283**
9284** <b>Callback Invocation Details</b>
9285**
9286** When an unlock-notify callback is registered, the application provides a
9287** single void* pointer that is passed to the callback when it is invoked.
9288** However, the signature of the callback function allows SQLite to pass
9289** it an array of void* context pointers. The first argument passed to
9290** an unlock-notify callback is a pointer to an array of void* pointers,
9291** and the second is the number of entries in the array.
9292**
9293** When a blocking connection's transaction is concluded, there may be
9294** more than one blocked connection that has registered for an unlock-notify
9295** callback. ^If two or more such blocked connections have specified the
9296** same callback function, then instead of invoking the callback function
9297** multiple times, it is invoked once with the set of void* context pointers
9298** specified by the blocked connections bundled together into an array.
9299** This gives the application an opportunity to prioritize any actions
9300** related to the set of unblocked database connections.
9301**
9302** <b>Deadlock Detection</b>
9303**
9304** Assuming that after registering for an unlock-notify callback a
9305** database waits for the callback to be issued before taking any further
9306** action (a reasonable assumption), then using this API may cause the
9307** application to deadlock. For example, if connection X is waiting for
9308** connection Y's transaction to be concluded, and similarly connection
9309** Y is waiting on connection X's transaction, then neither connection
9310** will proceed and the system may remain deadlocked indefinitely.
9311**
9312** To avoid this scenario, the sqlite3_unlock_notify() performs deadlock
9313** detection. ^If a given call to sqlite3_unlock_notify() would put the
9314** system in a deadlocked state, then SQLITE_LOCKED is returned and no
9315** unlock-notify callback is registered. The system is said to be in
9316** a deadlocked state if connection A has registered for an unlock-notify
9317** callback on the conclusion of connection B's transaction, and connection
9318** B has itself registered for an unlock-notify callback when connection
9319** A's transaction is concluded. ^Indirect deadlock is also detected, so
9320** the system is also considered to be deadlocked if connection B has
9321** registered for an unlock-notify callback on the conclusion of connection
9322** C's transaction, where connection C is waiting on connection A. ^Any
9323** number of levels of indirection are allowed.
9324**
9325** <b>The "DROP TABLE" Exception</b>
9326**
9327** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost
9328** always appropriate to call sqlite3_unlock_notify(). There is however,
9329** one exception. When executing a "DROP TABLE" or "DROP INDEX" statement,
9330** SQLite checks if there are any currently executing SELECT statements
9331** that belong to the same connection. If there are, SQLITE_LOCKED is
9332** returned. In this case there is no "blocking connection", so invoking
9333** sqlite3_unlock_notify() results in the unlock-notify callback being
9334** invoked immediately. If the application then re-attempts the "DROP TABLE"
9335** or "DROP INDEX" query, an infinite loop might be the result.
9336**
9337** One way around this problem is to check the extended error code returned
9338** by an sqlite3_step() call. ^(If there is a blocking connection, then the
9339** extended error code is set to SQLITE_LOCKED_SHAREDCACHE. Otherwise, in
9340** the special "DROP TABLE/INDEX" case, the extended error code is just
9341** SQLITE_LOCKED.)^
9342*/
9343SQLITE_API int sqlite3_unlock_notify(
9344 sqlite3 *pBlocked, /* Waiting connection */
9345 void (*xNotify)(void **apArg, int nArg), /* Callback function to invoke */
9346 void *pNotifyArg /* Argument to pass to xNotify */
9347);
9348
9349
9350/*
9351** CAPI3REF: String Comparison
9352**
9353** ^The [sqlite3_stricmp()] and [sqlite3_strnicmp()] APIs allow applications
9354** and extensions to compare the contents of two buffers containing UTF-8
9355** strings in a case-independent fashion, using the same definition of "case
9356** independence" that SQLite uses internally when comparing identifiers.
9357*/
9358SQLITE_API int sqlite3_stricmp(const char *, const char *);
9359SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
9360
9361/*
9362** CAPI3REF: String Globbing
9363*
9364** ^The [sqlite3_strglob(P,X)] interface returns zero if and only if
9365** string X matches the [GLOB] pattern P.
9366** ^The definition of [GLOB] pattern matching used in
9367** [sqlite3_strglob(P,X)] is the same as for the "X GLOB P" operator in the
9368** SQL dialect understood by SQLite. ^The [sqlite3_strglob(P,X)] function
9369** is case sensitive.
9370**
9371** Note that this routine returns zero on a match and non-zero if the strings
9372** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
9373**
9374** See also: [sqlite3_strlike()].
9375*/
9376SQLITE_API int sqlite3_strglob(const char *zGlob, const char *zStr);
9377
9378/*
9379** CAPI3REF: String LIKE Matching
9380*
9381** ^The [sqlite3_strlike(P,X,E)] interface returns zero if and only if
9382** string X matches the [LIKE] pattern P with escape character E.
9383** ^The definition of [LIKE] pattern matching used in
9384** [sqlite3_strlike(P,X,E)] is the same as for the "X LIKE P ESCAPE E"
9385** operator in the SQL dialect understood by SQLite. ^For "X LIKE P" without
9386** the ESCAPE clause, set the E parameter of [sqlite3_strlike(P,X,E)] to 0.
9387** ^As with the LIKE operator, the [sqlite3_strlike(P,X,E)] function is case
9388** insensitive - equivalent upper and lower case ASCII characters match
9389** one another.
9390**
9391** ^The [sqlite3_strlike(P,X,E)] function matches Unicode characters, though
9392** only ASCII characters are case folded.
9393**
9394** Note that this routine returns zero on a match and non-zero if the strings
9395** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
9396**
9397** See also: [sqlite3_strglob()].
9398*/
9399SQLITE_API int sqlite3_strlike(const char *zGlob, const char *zStr, unsigned int cEsc);
9400
9401/*
9402** CAPI3REF: Error Logging Interface
9403**
9404** ^The [sqlite3_log()] interface writes a message into the [error log]
9405** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()].
9406** ^If logging is enabled, the zFormat string and subsequent arguments are
9407** used with [sqlite3_snprintf()] to generate the final output string.
9408**
9409** The sqlite3_log() interface is intended for use by extensions such as
9410** virtual tables, collating functions, and SQL functions. While there is
9411** nothing to prevent an application from calling sqlite3_log(), doing so
9412** is considered bad form.
9413**
9414** The zFormat string must not be NULL.
9415**
9416** To avoid deadlocks and other threading problems, the sqlite3_log() routine
9417** will not use dynamically allocated memory. The log message is stored in
9418** a fixed-length buffer on the stack. If the log message is longer than
9419** a few hundred characters, it will be truncated to the length of the
9420** buffer.
9421*/
9422SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...);
9423
9424/*
9425** CAPI3REF: Write-Ahead Log Commit Hook
9426** METHOD: sqlite3
9427**
9428** ^The [sqlite3_wal_hook()] function is used to register a callback that
9429** is invoked each time data is committed to a database in wal mode.
9430**
9431** ^(The callback is invoked by SQLite after the commit has taken place and
9432** the associated write-lock on the database released)^, so the implementation
9433** may read, write or [checkpoint] the database as required.
9434**
9435** ^The first parameter passed to the callback function when it is invoked
9436** is a copy of the third parameter passed to sqlite3_wal_hook() when
9437** registering the callback. ^The second is a copy of the database handle.
9438** ^The third parameter is the name of the database that was written to -
9439** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
9440** is the number of pages currently in the write-ahead log file,
9441** including those that were just committed.
9442**
9443** The callback function should normally return [SQLITE_OK]. ^If an error
9444** code is returned, that error will propagate back up through the
9445** SQLite code base to cause the statement that provoked the callback
9446** to report an error, though the commit will have still occurred. If the
9447** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value
9448** that does not correspond to any valid SQLite error code, the results
9449** are undefined.
9450**
9451** A single database handle may have at most a single write-ahead log callback
9452** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any
9453** previously registered write-ahead log callback. ^The return value is
9454** a copy of the third parameter from the previous call, if any, or 0.
9455** ^Note that the [sqlite3_wal_autocheckpoint()] interface and the
9456** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
9457** overwrite any prior [sqlite3_wal_hook()] settings.
9458*/
9459SQLITE_API void *sqlite3_wal_hook(
9460 sqlite3*,
9461 int(*)(void *,sqlite3*,const char*,int),
9462 void*
9463);
9464
9465/*
9466** CAPI3REF: Configure an auto-checkpoint
9467** METHOD: sqlite3
9468**
9469** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
9470** [sqlite3_wal_hook()] that causes any database on [database connection] D
9471** to automatically [checkpoint]
9472** after committing a transaction if there are N or
9473** more frames in the [write-ahead log] file. ^Passing zero or
9474** a negative value as the nFrame parameter disables automatic
9475** checkpoints entirely.
9476**
9477** ^The callback registered by this function replaces any existing callback
9478** registered using [sqlite3_wal_hook()]. ^Likewise, registering a callback
9479** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
9480** configured by this function.
9481**
9482** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
9483** from SQL.
9484**
9485** ^Checkpoints initiated by this mechanism are
9486** [sqlite3_wal_checkpoint_v2|PASSIVE].
9487**
9488** ^Every new [database connection] defaults to having the auto-checkpoint
9489** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
9490** pages. The use of this interface
9491** is only necessary if the default setting is found to be suboptimal
9492** for a particular application.
9493*/
9494SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
9495
9496/*
9497** CAPI3REF: Checkpoint a database
9498** METHOD: sqlite3
9499**
9500** ^(The sqlite3_wal_checkpoint(D,X) is equivalent to
9501** [sqlite3_wal_checkpoint_v2](D,X,[SQLITE_CHECKPOINT_PASSIVE],0,0).)^
9502**
9503** In brief, sqlite3_wal_checkpoint(D,X) causes the content in the
9504** [write-ahead log] for database X on [database connection] D to be
9505** transferred into the database file and for the write-ahead log to
9506** be reset. See the [checkpointing] documentation for addition
9507** information.
9508**
9509** This interface used to be the only way to cause a checkpoint to
9510** occur. But then the newer and more powerful [sqlite3_wal_checkpoint_v2()]
9511** interface was added. This interface is retained for backwards
9512** compatibility and as a convenience for applications that need to manually
9513** start a callback but which do not need the full power (and corresponding
9514** complication) of [sqlite3_wal_checkpoint_v2()].
9515*/
9516SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
9517
9518/*
9519** CAPI3REF: Checkpoint a database
9520** METHOD: sqlite3
9521**
9522** ^(The sqlite3_wal_checkpoint_v2(D,X,M,L,C) interface runs a checkpoint
9523** operation on database X of [database connection] D in mode M. Status
9524** information is written back into integers pointed to by L and C.)^
9525** ^(The M parameter must be a valid [checkpoint mode]:)^
9526**
9527** <dl>
9528** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
9529** ^Checkpoint as many frames as possible without waiting for any database
9530** readers or writers to finish, then sync the database file if all frames
9531** in the log were checkpointed. ^The [busy-handler callback]
9532** is never invoked in the SQLITE_CHECKPOINT_PASSIVE mode.
9533** ^On the other hand, passive mode might leave the checkpoint unfinished
9534** if there are concurrent readers or writers.
9535**
9536** <dt>SQLITE_CHECKPOINT_FULL<dd>
9537** ^This mode blocks (it invokes the
9538** [sqlite3_busy_handler|busy-handler callback]) until there is no
9539** database writer and all readers are reading from the most recent database
9540** snapshot. ^It then checkpoints all frames in the log file and syncs the
9541** database file. ^This mode blocks new database writers while it is pending,
9542** but new database readers are allowed to continue unimpeded.
9543**
9544** <dt>SQLITE_CHECKPOINT_RESTART<dd>
9545** ^This mode works the same way as SQLITE_CHECKPOINT_FULL with the addition
9546** that after checkpointing the log file it blocks (calls the
9547** [busy-handler callback])
9548** until all readers are reading from the database file only. ^This ensures
9549** that the next writer will restart the log file from the beginning.
9550** ^Like SQLITE_CHECKPOINT_FULL, this mode blocks new
9551** database writer attempts while it is pending, but does not impede readers.
9552**
9553** <dt>SQLITE_CHECKPOINT_TRUNCATE<dd>
9554** ^This mode works the same way as SQLITE_CHECKPOINT_RESTART with the
9555** addition that it also truncates the log file to zero bytes just prior
9556** to a successful return.
9557** </dl>
9558**
9559** ^If pnLog is not NULL, then *pnLog is set to the total number of frames in
9560** the log file or to -1 if the checkpoint could not run because
9561** of an error or because the database is not in [WAL mode]. ^If pnCkpt is not
9562** NULL,then *pnCkpt is set to the total number of checkpointed frames in the
9563** log file (including any that were already checkpointed before the function
9564** was called) or to -1 if the checkpoint could not run due to an error or
9565** because the database is not in WAL mode. ^Note that upon successful
9566** completion of an SQLITE_CHECKPOINT_TRUNCATE, the log file will have been
9567** truncated to zero bytes and so both *pnLog and *pnCkpt will be set to zero.
9568**
9569** ^All calls obtain an exclusive "checkpoint" lock on the database file. ^If
9570** any other process is running a checkpoint operation at the same time, the
9571** lock cannot be obtained and SQLITE_BUSY is returned. ^Even if there is a
9572** busy-handler configured, it will not be invoked in this case.
9573**
9574** ^The SQLITE_CHECKPOINT_FULL, RESTART and TRUNCATE modes also obtain the
9575** exclusive "writer" lock on the database file. ^If the writer lock cannot be
9576** obtained immediately, and a busy-handler is configured, it is invoked and
9577** the writer lock retried until either the busy-handler returns 0 or the lock
9578** is successfully obtained. ^The busy-handler is also invoked while waiting for
9579** database readers as described above. ^If the busy-handler returns 0 before
9580** the writer lock is obtained or while waiting for database readers, the
9581** checkpoint operation proceeds from that point in the same way as
9582** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
9583** without blocking any further. ^SQLITE_BUSY is returned in this case.
9584**
9585** ^If parameter zDb is NULL or points to a zero length string, then the
9586** specified operation is attempted on all WAL databases [attached] to
9587** [database connection] db. In this case the
9588** values written to output parameters *pnLog and *pnCkpt are undefined. ^If
9589** an SQLITE_BUSY error is encountered when processing one or more of the
9590** attached WAL databases, the operation is still attempted on any remaining
9591** attached databases and SQLITE_BUSY is returned at the end. ^If any other
9592** error occurs while processing an attached database, processing is abandoned
9593** and the error code is returned to the caller immediately. ^If no error
9594** (SQLITE_BUSY or otherwise) is encountered while processing the attached
9595** databases, SQLITE_OK is returned.
9596**
9597** ^If database zDb is the name of an attached database that is not in WAL
9598** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. ^If
9599** zDb is not NULL (or a zero length string) and is not the name of any
9600** attached database, SQLITE_ERROR is returned to the caller.
9601**
9602** ^Unless it returns SQLITE_MISUSE,
9603** the sqlite3_wal_checkpoint_v2() interface
9604** sets the error information that is queried by
9605** [sqlite3_errcode()] and [sqlite3_errmsg()].
9606**
9607** ^The [PRAGMA wal_checkpoint] command can be used to invoke this interface
9608** from SQL.
9609*/
9610SQLITE_API int sqlite3_wal_checkpoint_v2(
9611 sqlite3 *db, /* Database handle */
9612 const char *zDb, /* Name of attached database (or NULL) */
9613 int eMode, /* SQLITE_CHECKPOINT_* value */
9614 int *pnLog, /* OUT: Size of WAL log in frames */
9615 int *pnCkpt /* OUT: Total number of frames checkpointed */
9616);
9617
9618/*
9619** CAPI3REF: Checkpoint Mode Values
9620** KEYWORDS: {checkpoint mode}
9621**
9622** These constants define all valid values for the "checkpoint mode" passed
9623** as the third parameter to the [sqlite3_wal_checkpoint_v2()] interface.
9624** See the [sqlite3_wal_checkpoint_v2()] documentation for details on the
9625** meaning of each of these checkpoint modes.
9626*/
9627#define SQLITE_CHECKPOINT_PASSIVE 0 /* Do as much as possible w/o blocking */
9628#define SQLITE_CHECKPOINT_FULL 1 /* Wait for writers, then checkpoint */
9629#define SQLITE_CHECKPOINT_RESTART 2 /* Like FULL but wait for for readers */
9630#define SQLITE_CHECKPOINT_TRUNCATE 3 /* Like RESTART but also truncate WAL */
9631
9632/*
9633** CAPI3REF: Virtual Table Interface Configuration
9634**
9635** This function may be called by either the [xConnect] or [xCreate] method
9636** of a [virtual table] implementation to configure
9637** various facets of the virtual table interface.
9638**
9639** If this interface is invoked outside the context of an xConnect or
9640** xCreate virtual table method then the behavior is undefined.
9641**
9642** In the call sqlite3_vtab_config(D,C,...) the D parameter is the
9643** [database connection] in which the virtual table is being created and
9644** which is passed in as the first argument to the [xConnect] or [xCreate]
9645** method that is invoking sqlite3_vtab_config(). The C parameter is one
9646** of the [virtual table configuration options]. The presence and meaning
9647** of parameters after C depend on which [virtual table configuration option]
9648** is used.
9649*/
9650SQLITE_API int sqlite3_vtab_config(sqlite3*, int op, ...);
9651
9652/*
9653** CAPI3REF: Virtual Table Configuration Options
9654** KEYWORDS: {virtual table configuration options}
9655** KEYWORDS: {virtual table configuration option}
9656**
9657** These macros define the various options to the
9658** [sqlite3_vtab_config()] interface that [virtual table] implementations
9659** can use to customize and optimize their behavior.
9660**
9661** <dl>
9662** [[SQLITE_VTAB_CONSTRAINT_SUPPORT]]
9663** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT</dt>
9664** <dd>Calls of the form
9665** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported,
9666** where X is an integer. If X is zero, then the [virtual table] whose
9667** [xCreate] or [xConnect] method invoked [sqlite3_vtab_config()] does not
9668** support constraints. In this configuration (which is the default) if
9669** a call to the [xUpdate] method returns [SQLITE_CONSTRAINT], then the entire
9670** statement is rolled back as if [ON CONFLICT | OR ABORT] had been
9671** specified as part of the users SQL statement, regardless of the actual
9672** ON CONFLICT mode specified.
9673**
9674** If X is non-zero, then the virtual table implementation guarantees
9675** that if [xUpdate] returns [SQLITE_CONSTRAINT], it will do so before
9676** any modifications to internal or persistent data structures have been made.
9677** If the [ON CONFLICT] mode is ABORT, FAIL, IGNORE or ROLLBACK, SQLite
9678** is able to roll back a statement or database transaction, and abandon
9679** or continue processing the current SQL statement as appropriate.
9680** If the ON CONFLICT mode is REPLACE and the [xUpdate] method returns
9681** [SQLITE_CONSTRAINT], SQLite handles this as if the ON CONFLICT mode
9682** had been ABORT.
9683**
9684** Virtual table implementations that are required to handle OR REPLACE
9685** must do so within the [xUpdate] method. If a call to the
9686** [sqlite3_vtab_on_conflict()] function indicates that the current ON
9687** CONFLICT policy is REPLACE, the virtual table implementation should
9688** silently replace the appropriate rows within the xUpdate callback and
9689** return SQLITE_OK. Or, if this is not possible, it may return
9690** SQLITE_CONSTRAINT, in which case SQLite falls back to OR ABORT
9691** constraint handling.
9692** </dd>
9693**
9694** [[SQLITE_VTAB_DIRECTONLY]]<dt>SQLITE_VTAB_DIRECTONLY</dt>
9695** <dd>Calls of the form
9696** [sqlite3_vtab_config](db,SQLITE_VTAB_DIRECTONLY) from within the
9697** the [xConnect] or [xCreate] methods of a [virtual table] implmentation
9698** prohibits that virtual table from being used from within triggers and
9699** views.
9700** </dd>
9701**
9702** [[SQLITE_VTAB_INNOCUOUS]]<dt>SQLITE_VTAB_INNOCUOUS</dt>
9703** <dd>Calls of the form
9704** [sqlite3_vtab_config](db,SQLITE_VTAB_INNOCUOUS) from within the
9705** the [xConnect] or [xCreate] methods of a [virtual table] implmentation
9706** identify that virtual table as being safe to use from within triggers
9707** and views. Conceptually, the SQLITE_VTAB_INNOCUOUS tag means that the
9708** virtual table can do no serious harm even if it is controlled by a
9709** malicious hacker. Developers should avoid setting the SQLITE_VTAB_INNOCUOUS
9710** flag unless absolutely necessary.
9711** </dd>
9712** </dl>
9713*/
9714#define SQLITE_VTAB_CONSTRAINT_SUPPORT 1
9715#define SQLITE_VTAB_INNOCUOUS 2
9716#define SQLITE_VTAB_DIRECTONLY 3
9717
9718/*
9719** CAPI3REF: Determine The Virtual Table Conflict Policy
9720**
9721** This function may only be called from within a call to the [xUpdate] method
9722** of a [virtual table] implementation for an INSERT or UPDATE operation. ^The
9723** value returned is one of [SQLITE_ROLLBACK], [SQLITE_IGNORE], [SQLITE_FAIL],
9724** [SQLITE_ABORT], or [SQLITE_REPLACE], according to the [ON CONFLICT] mode
9725** of the SQL statement that triggered the call to the [xUpdate] method of the
9726** [virtual table].
9727*/
9728SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *);
9729
9730/*
9731** CAPI3REF: Determine If Virtual Table Column Access Is For UPDATE
9732**
9733** If the sqlite3_vtab_nochange(X) routine is called within the [xColumn]
9734** method of a [virtual table], then it might return true if the
9735** column is being fetched as part of an UPDATE operation during which the
9736** column value will not change. The virtual table implementation can use
9737** this hint as permission to substitute a return value that is less
9738** expensive to compute and that the corresponding
9739** [xUpdate] method understands as a "no-change" value.
9740**
9741** If the [xColumn] method calls sqlite3_vtab_nochange() and finds that
9742** the column is not changed by the UPDATE statement, then the xColumn
9743** method can optionally return without setting a result, without calling
9744** any of the [sqlite3_result_int|sqlite3_result_xxxxx() interfaces].
9745** In that case, [sqlite3_value_nochange(X)] will return true for the
9746** same column in the [xUpdate] method.
9747**
9748** The sqlite3_vtab_nochange() routine is an optimization. Virtual table
9749** implementations should continue to give a correct answer even if the
9750** sqlite3_vtab_nochange() interface were to always return false. In the
9751** current implementation, the sqlite3_vtab_nochange() interface does always
9752** returns false for the enhanced [UPDATE FROM] statement.
9753*/
9754SQLITE_API int sqlite3_vtab_nochange(sqlite3_context*);
9755
9756/*
9757** CAPI3REF: Determine The Collation For a Virtual Table Constraint
9758**
9759** This function may only be called from within a call to the [xBestIndex]
9760** method of a [virtual table].
9761**
9762** The first argument must be the sqlite3_index_info object that is the
9763** first parameter to the xBestIndex() method. The second argument must be
9764** an index into the aConstraint[] array belonging to the sqlite3_index_info
9765** structure passed to xBestIndex. This function returns a pointer to a buffer
9766** containing the name of the collation sequence for the corresponding
9767** constraint.
9768*/
9769SQLITE_API SQLITE_EXPERIMENTAL const char *sqlite3_vtab_collation(sqlite3_index_info*,int);
9770
9771/*
9772** CAPI3REF: Conflict resolution modes
9773** KEYWORDS: {conflict resolution mode}
9774**
9775** These constants are returned by [sqlite3_vtab_on_conflict()] to
9776** inform a [virtual table] implementation what the [ON CONFLICT] mode
9777** is for the SQL statement being evaluated.
9778**
9779** Note that the [SQLITE_IGNORE] constant is also used as a potential
9780** return value from the [sqlite3_set_authorizer()] callback and that
9781** [SQLITE_ABORT] is also a [result code].
9782*/
9783#define SQLITE_ROLLBACK 1
9784/* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */
9785#define SQLITE_FAIL 3
9786/* #define SQLITE_ABORT 4 // Also an error code */
9787#define SQLITE_REPLACE 5
9788
9789/*
9790** CAPI3REF: Prepared Statement Scan Status Opcodes
9791** KEYWORDS: {scanstatus options}
9792**
9793** The following constants can be used for the T parameter to the
9794** [sqlite3_stmt_scanstatus(S,X,T,V)] interface. Each constant designates a
9795** different metric for sqlite3_stmt_scanstatus() to return.
9796**
9797** When the value returned to V is a string, space to hold that string is
9798** managed by the prepared statement S and will be automatically freed when
9799** S is finalized.
9800**
9801** <dl>
9802** [[SQLITE_SCANSTAT_NLOOP]] <dt>SQLITE_SCANSTAT_NLOOP</dt>
9803** <dd>^The [sqlite3_int64] variable pointed to by the V parameter will be
9804** set to the total number of times that the X-th loop has run.</dd>
9805**
9806** [[SQLITE_SCANSTAT_NVISIT]] <dt>SQLITE_SCANSTAT_NVISIT</dt>
9807** <dd>^The [sqlite3_int64] variable pointed to by the V parameter will be set
9808** to the total number of rows examined by all iterations of the X-th loop.</dd>
9809**
9810** [[SQLITE_SCANSTAT_EST]] <dt>SQLITE_SCANSTAT_EST</dt>
9811** <dd>^The "double" variable pointed to by the V parameter will be set to the
9812** query planner's estimate for the average number of rows output from each
9813** iteration of the X-th loop. If the query planner's estimates was accurate,
9814** then this value will approximate the quotient NVISIT/NLOOP and the
9815** product of this value for all prior loops with the same SELECTID will
9816** be the NLOOP value for the current loop.
9817**
9818** [[SQLITE_SCANSTAT_NAME]] <dt>SQLITE_SCANSTAT_NAME</dt>
9819** <dd>^The "const char *" variable pointed to by the V parameter will be set
9820** to a zero-terminated UTF-8 string containing the name of the index or table
9821** used for the X-th loop.
9822**
9823** [[SQLITE_SCANSTAT_EXPLAIN]] <dt>SQLITE_SCANSTAT_EXPLAIN</dt>
9824** <dd>^The "const char *" variable pointed to by the V parameter will be set
9825** to a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN]
9826** description for the X-th loop.
9827**
9828** [[SQLITE_SCANSTAT_SELECTID]] <dt>SQLITE_SCANSTAT_SELECT</dt>
9829** <dd>^The "int" variable pointed to by the V parameter will be set to the
9830** "select-id" for the X-th loop. The select-id identifies which query or
9831** subquery the loop is part of. The main query has a select-id of zero.
9832** The select-id is the same value as is output in the first column
9833** of an [EXPLAIN QUERY PLAN] query.
9834** </dl>
9835*/
9836#define SQLITE_SCANSTAT_NLOOP 0
9837#define SQLITE_SCANSTAT_NVISIT 1
9838#define SQLITE_SCANSTAT_EST 2
9839#define SQLITE_SCANSTAT_NAME 3
9840#define SQLITE_SCANSTAT_EXPLAIN 4
9841#define SQLITE_SCANSTAT_SELECTID 5
9842
9843/*
9844** CAPI3REF: Prepared Statement Scan Status
9845** METHOD: sqlite3_stmt
9846**
9847** This interface returns information about the predicted and measured
9848** performance for pStmt. Advanced applications can use this
9849** interface to compare the predicted and the measured performance and
9850** issue warnings and/or rerun [ANALYZE] if discrepancies are found.
9851**
9852** Since this interface is expected to be rarely used, it is only
9853** available if SQLite is compiled using the [SQLITE_ENABLE_STMT_SCANSTATUS]
9854** compile-time option.
9855**
9856** The "iScanStatusOp" parameter determines which status information to return.
9857** The "iScanStatusOp" must be one of the [scanstatus options] or the behavior
9858** of this interface is undefined.
9859** ^The requested measurement is written into a variable pointed to by
9860** the "pOut" parameter.
9861** Parameter "idx" identifies the specific loop to retrieve statistics for.
9862** Loops are numbered starting from zero. ^If idx is out of range - less than
9863** zero or greater than or equal to the total number of loops used to implement
9864** the statement - a non-zero value is returned and the variable that pOut
9865** points to is unchanged.
9866**
9867** ^Statistics might not be available for all loops in all statements. ^In cases
9868** where there exist loops with no available statistics, this function behaves
9869** as if the loop did not exist - it returns non-zero and leave the variable
9870** that pOut points to unchanged.
9871**
9872** See also: [sqlite3_stmt_scanstatus_reset()]
9873*/
9874SQLITE_API int sqlite3_stmt_scanstatus(
9875 sqlite3_stmt *pStmt, /* Prepared statement for which info desired */
9876 int idx, /* Index of loop to report on */
9877 int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */
9878 void *pOut /* Result written here */
9879);
9880
9881/*
9882** CAPI3REF: Zero Scan-Status Counters
9883** METHOD: sqlite3_stmt
9884**
9885** ^Zero all [sqlite3_stmt_scanstatus()] related event counters.
9886**
9887** This API is only available if the library is built with pre-processor
9888** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
9889*/
9890SQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
9891
9892/*
9893** CAPI3REF: Flush caches to disk mid-transaction
9894** METHOD: sqlite3
9895**
9896** ^If a write-transaction is open on [database connection] D when the
9897** [sqlite3_db_cacheflush(D)] interface invoked, any dirty
9898** pages in the pager-cache that are not currently in use are written out
9899** to disk. A dirty page may be in use if a database cursor created by an
9900** active SQL statement is reading from it, or if it is page 1 of a database
9901** file (page 1 is always "in use"). ^The [sqlite3_db_cacheflush(D)]
9902** interface flushes caches for all schemas - "main", "temp", and
9903** any [attached] databases.
9904**
9905** ^If this function needs to obtain extra database locks before dirty pages
9906** can be flushed to disk, it does so. ^If those locks cannot be obtained
9907** immediately and there is a busy-handler callback configured, it is invoked
9908** in the usual manner. ^If the required lock still cannot be obtained, then
9909** the database is skipped and an attempt made to flush any dirty pages
9910** belonging to the next (if any) database. ^If any databases are skipped
9911** because locks cannot be obtained, but no other error occurs, this
9912** function returns SQLITE_BUSY.
9913**
9914** ^If any other error occurs while flushing dirty pages to disk (for
9915** example an IO error or out-of-memory condition), then processing is
9916** abandoned and an SQLite [error code] is returned to the caller immediately.
9917**
9918** ^Otherwise, if no error occurs, [sqlite3_db_cacheflush()] returns SQLITE_OK.
9919**
9920** ^This function does not set the database handle error code or message
9921** returned by the [sqlite3_errcode()] and [sqlite3_errmsg()] functions.
9922*/
9923SQLITE_API int sqlite3_db_cacheflush(sqlite3*);
9924
9925/*
9926** CAPI3REF: The pre-update hook.
9927** METHOD: sqlite3
9928**
9929** ^These interfaces are only available if SQLite is compiled using the
9930** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
9931**
9932** ^The [sqlite3_preupdate_hook()] interface registers a callback function
9933** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation
9934** on a database table.
9935** ^At most one preupdate hook may be registered at a time on a single
9936** [database connection]; each call to [sqlite3_preupdate_hook()] overrides
9937** the previous setting.
9938** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()]
9939** with a NULL pointer as the second parameter.
9940** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as
9941** the first parameter to callbacks.
9942**
9943** ^The preupdate hook only fires for changes to real database tables; the
9944** preupdate hook is not invoked for changes to [virtual tables] or to
9945** system tables like sqlite_sequence or sqlite_stat1.
9946**
9947** ^The second parameter to the preupdate callback is a pointer to
9948** the [database connection] that registered the preupdate hook.
9949** ^The third parameter to the preupdate callback is one of the constants
9950** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to identify the
9951** kind of update operation that is about to occur.
9952** ^(The fourth parameter to the preupdate callback is the name of the
9953** database within the database connection that is being modified. This
9954** will be "main" for the main database or "temp" for TEMP tables or
9955** the name given after the AS keyword in the [ATTACH] statement for attached
9956** databases.)^
9957** ^The fifth parameter to the preupdate callback is the name of the
9958** table that is being modified.
9959**
9960** For an UPDATE or DELETE operation on a [rowid table], the sixth
9961** parameter passed to the preupdate callback is the initial [rowid] of the
9962** row being modified or deleted. For an INSERT operation on a rowid table,
9963** or any operation on a WITHOUT ROWID table, the value of the sixth
9964** parameter is undefined. For an INSERT or UPDATE on a rowid table the
9965** seventh parameter is the final rowid value of the row being inserted
9966** or updated. The value of the seventh parameter passed to the callback
9967** function is not defined for operations on WITHOUT ROWID tables, or for
9968** DELETE operations on rowid tables.
9969**
9970** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()],
9971** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces
9972** provide additional information about a preupdate event. These routines
9973** may only be called from within a preupdate callback. Invoking any of
9974** these routines from outside of a preupdate callback or with a
9975** [database connection] pointer that is different from the one supplied
9976** to the preupdate callback results in undefined and probably undesirable
9977** behavior.
9978**
9979** ^The [sqlite3_preupdate_count(D)] interface returns the number of columns
9980** in the row that is being inserted, updated, or deleted.
9981**
9982** ^The [sqlite3_preupdate_old(D,N,P)] interface writes into P a pointer to
9983** a [protected sqlite3_value] that contains the value of the Nth column of
9984** the table row before it is updated. The N parameter must be between 0
9985** and one less than the number of columns or the behavior will be
9986** undefined. This must only be used within SQLITE_UPDATE and SQLITE_DELETE
9987** preupdate callbacks; if it is used by an SQLITE_INSERT callback then the
9988** behavior is undefined. The [sqlite3_value] that P points to
9989** will be destroyed when the preupdate callback returns.
9990**
9991** ^The [sqlite3_preupdate_new(D,N,P)] interface writes into P a pointer to
9992** a [protected sqlite3_value] that contains the value of the Nth column of
9993** the table row after it is updated. The N parameter must be between 0
9994** and one less than the number of columns or the behavior will be
9995** undefined. This must only be used within SQLITE_INSERT and SQLITE_UPDATE
9996** preupdate callbacks; if it is used by an SQLITE_DELETE callback then the
9997** behavior is undefined. The [sqlite3_value] that P points to
9998** will be destroyed when the preupdate callback returns.
9999**
10000** ^The [sqlite3_preupdate_depth(D)] interface returns 0 if the preupdate
10001** callback was invoked as a result of a direct insert, update, or delete
10002** operation; or 1 for inserts, updates, or deletes invoked by top-level
10003** triggers; or 2 for changes resulting from triggers called by top-level
10004** triggers; and so forth.
10005**
10006** When the [sqlite3_blob_write()] API is used to update a blob column,
10007** the pre-update hook is invoked with SQLITE_DELETE. This is because the
10008** in this case the new values are not available. In this case, when a
10009** callback made with op==SQLITE_DELETE is actuall a write using the
10010** sqlite3_blob_write() API, the [sqlite3_preupdate_blobwrite()] returns
10011** the index of the column being written. In other cases, where the
10012** pre-update hook is being invoked for some other reason, including a
10013** regular DELETE, sqlite3_preupdate_blobwrite() returns -1.
10014**
10015** See also: [sqlite3_update_hook()]
10016*/
10017#if defined(SQLITE_ENABLE_PREUPDATE_HOOK)
10018SQLITE_API void *sqlite3_preupdate_hook(
10019 sqlite3 *db,
10020 void(*xPreUpdate)(
10021 void *pCtx, /* Copy of third arg to preupdate_hook() */
10022 sqlite3 *db, /* Database handle */
10023 int op, /* SQLITE_UPDATE, DELETE or INSERT */
10024 char const *zDb, /* Database name */
10025 char const *zName, /* Table name */
10026 sqlite3_int64 iKey1, /* Rowid of row about to be deleted/updated */
10027 sqlite3_int64 iKey2 /* New rowid value (for a rowid UPDATE) */
10028 ),
10029 void*
10030);
10031SQLITE_API int sqlite3_preupdate_old(sqlite3 *, int, sqlite3_value **);
10032SQLITE_API int sqlite3_preupdate_count(sqlite3 *);
10033SQLITE_API int sqlite3_preupdate_depth(sqlite3 *);
10034SQLITE_API int sqlite3_preupdate_new(sqlite3 *, int, sqlite3_value **);
10035SQLITE_API int sqlite3_preupdate_blobwrite(sqlite3 *);
10036#endif
10037
10038/*
10039** CAPI3REF: Low-level system error code
10040** METHOD: sqlite3
10041**
10042** ^Attempt to return the underlying operating system error code or error
10043** number that caused the most recent I/O error or failure to open a file.
10044** The return value is OS-dependent. For example, on unix systems, after
10045** [sqlite3_open_v2()] returns [SQLITE_CANTOPEN], this interface could be
10046** called to get back the underlying "errno" that caused the problem, such
10047** as ENOSPC, EAUTH, EISDIR, and so forth.
10048*/
10049SQLITE_API int sqlite3_system_errno(sqlite3*);
10050
10051/*
10052** CAPI3REF: Database Snapshot
10053** KEYWORDS: {snapshot} {sqlite3_snapshot}
10054**
10055** An instance of the snapshot object records the state of a [WAL mode]
10056** database for some specific point in history.
10057**
10058** In [WAL mode], multiple [database connections] that are open on the
10059** same database file can each be reading a different historical version
10060** of the database file. When a [database connection] begins a read
10061** transaction, that connection sees an unchanging copy of the database
10062** as it existed for the point in time when the transaction first started.
10063** Subsequent changes to the database from other connections are not seen
10064** by the reader until a new read transaction is started.
10065**
10066** The sqlite3_snapshot object records state information about an historical
10067** version of the database file so that it is possible to later open a new read
10068** transaction that sees that historical version of the database rather than
10069** the most recent version.
10070*/
10071typedef struct sqlite3_snapshot {
10072 unsigned char hidden[48];
10073} sqlite3_snapshot;
10074
10075/*
10076** CAPI3REF: Record A Database Snapshot
10077** CONSTRUCTOR: sqlite3_snapshot
10078**
10079** ^The [sqlite3_snapshot_get(D,S,P)] interface attempts to make a
10080** new [sqlite3_snapshot] object that records the current state of
10081** schema S in database connection D. ^On success, the
10082** [sqlite3_snapshot_get(D,S,P)] interface writes a pointer to the newly
10083** created [sqlite3_snapshot] object into *P and returns SQLITE_OK.
10084** If there is not already a read-transaction open on schema S when
10085** this function is called, one is opened automatically.
10086**
10087** The following must be true for this function to succeed. If any of
10088** the following statements are false when sqlite3_snapshot_get() is
10089** called, SQLITE_ERROR is returned. The final value of *P is undefined
10090** in this case.
10091**
10092** <ul>
10093** <li> The database handle must not be in [autocommit mode].
10094**
10095** <li> Schema S of [database connection] D must be a [WAL mode] database.
10096**
10097** <li> There must not be a write transaction open on schema S of database
10098** connection D.
10099**
10100** <li> One or more transactions must have been written to the current wal
10101** file since it was created on disk (by any connection). This means
10102** that a snapshot cannot be taken on a wal mode database with no wal
10103** file immediately after it is first opened. At least one transaction
10104** must be written to it first.
10105** </ul>
10106**
10107** This function may also return SQLITE_NOMEM. If it is called with the
10108** database handle in autocommit mode but fails for some other reason,
10109** whether or not a read transaction is opened on schema S is undefined.
10110**
10111** The [sqlite3_snapshot] object returned from a successful call to
10112** [sqlite3_snapshot_get()] must be freed using [sqlite3_snapshot_free()]
10113** to avoid a memory leak.
10114**
10115** The [sqlite3_snapshot_get()] interface is only available when the
10116** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10117*/
10118SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_get(
10119 sqlite3 *db,
10120 const char *zSchema,
10121 sqlite3_snapshot **ppSnapshot
10122);
10123
10124/*
10125** CAPI3REF: Start a read transaction on an historical snapshot
10126** METHOD: sqlite3_snapshot
10127**
10128** ^The [sqlite3_snapshot_open(D,S,P)] interface either starts a new read
10129** transaction or upgrades an existing one for schema S of
10130** [database connection] D such that the read transaction refers to
10131** historical [snapshot] P, rather than the most recent change to the
10132** database. ^The [sqlite3_snapshot_open()] interface returns SQLITE_OK
10133** on success or an appropriate [error code] if it fails.
10134**
10135** ^In order to succeed, the database connection must not be in
10136** [autocommit mode] when [sqlite3_snapshot_open(D,S,P)] is called. If there
10137** is already a read transaction open on schema S, then the database handle
10138** must have no active statements (SELECT statements that have been passed
10139** to sqlite3_step() but not sqlite3_reset() or sqlite3_finalize()).
10140** SQLITE_ERROR is returned if either of these conditions is violated, or
10141** if schema S does not exist, or if the snapshot object is invalid.
10142**
10143** ^A call to sqlite3_snapshot_open() will fail to open if the specified
10144** snapshot has been overwritten by a [checkpoint]. In this case
10145** SQLITE_ERROR_SNAPSHOT is returned.
10146**
10147** If there is already a read transaction open when this function is
10148** invoked, then the same read transaction remains open (on the same
10149** database snapshot) if SQLITE_ERROR, SQLITE_BUSY or SQLITE_ERROR_SNAPSHOT
10150** is returned. If another error code - for example SQLITE_PROTOCOL or an
10151** SQLITE_IOERR error code - is returned, then the final state of the
10152** read transaction is undefined. If SQLITE_OK is returned, then the
10153** read transaction is now open on database snapshot P.
10154**
10155** ^(A call to [sqlite3_snapshot_open(D,S,P)] will fail if the
10156** database connection D does not know that the database file for
10157** schema S is in [WAL mode]. A database connection might not know
10158** that the database file is in [WAL mode] if there has been no prior
10159** I/O on that database connection, or if the database entered [WAL mode]
10160** after the most recent I/O on the database connection.)^
10161** (Hint: Run "[PRAGMA application_id]" against a newly opened
10162** database connection in order to make it ready to use snapshots.)
10163**
10164** The [sqlite3_snapshot_open()] interface is only available when the
10165** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10166*/
10167SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_open(
10168 sqlite3 *db,
10169 const char *zSchema,
10170 sqlite3_snapshot *pSnapshot
10171);
10172
10173/*
10174** CAPI3REF: Destroy a snapshot
10175** DESTRUCTOR: sqlite3_snapshot
10176**
10177** ^The [sqlite3_snapshot_free(P)] interface destroys [sqlite3_snapshot] P.
10178** The application must eventually free every [sqlite3_snapshot] object
10179** using this routine to avoid a memory leak.
10180**
10181** The [sqlite3_snapshot_free()] interface is only available when the
10182** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10183*/
10184SQLITE_API SQLITE_EXPERIMENTAL void sqlite3_snapshot_free(sqlite3_snapshot*);
10185
10186/*
10187** CAPI3REF: Compare the ages of two snapshot handles.
10188** METHOD: sqlite3_snapshot
10189**
10190** The sqlite3_snapshot_cmp(P1, P2) interface is used to compare the ages
10191** of two valid snapshot handles.
10192**
10193** If the two snapshot handles are not associated with the same database
10194** file, the result of the comparison is undefined.
10195**
10196** Additionally, the result of the comparison is only valid if both of the
10197** snapshot handles were obtained by calling sqlite3_snapshot_get() since the
10198** last time the wal file was deleted. The wal file is deleted when the
10199** database is changed back to rollback mode or when the number of database
10200** clients drops to zero. If either snapshot handle was obtained before the
10201** wal file was last deleted, the value returned by this function
10202** is undefined.
10203**
10204** Otherwise, this API returns a negative value if P1 refers to an older
10205** snapshot than P2, zero if the two handles refer to the same database
10206** snapshot, and a positive value if P1 is a newer snapshot than P2.
10207**
10208** This interface is only available if SQLite is compiled with the
10209** [SQLITE_ENABLE_SNAPSHOT] option.
10210*/
10211SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_cmp(
10212 sqlite3_snapshot *p1,
10213 sqlite3_snapshot *p2
10214);
10215
10216/*
10217** CAPI3REF: Recover snapshots from a wal file
10218** METHOD: sqlite3_snapshot
10219**
10220** If a [WAL file] remains on disk after all database connections close
10221** (either through the use of the [SQLITE_FCNTL_PERSIST_WAL] [file control]
10222** or because the last process to have the database opened exited without
10223** calling [sqlite3_close()]) and a new connection is subsequently opened
10224** on that database and [WAL file], the [sqlite3_snapshot_open()] interface
10225** will only be able to open the last transaction added to the WAL file
10226** even though the WAL file contains other valid transactions.
10227**
10228** This function attempts to scan the WAL file associated with database zDb
10229** of database handle db and make all valid snapshots available to
10230** sqlite3_snapshot_open(). It is an error if there is already a read
10231** transaction open on the database, or if the database is not a WAL mode
10232** database.
10233**
10234** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
10235**
10236** This interface is only available if SQLite is compiled with the
10237** [SQLITE_ENABLE_SNAPSHOT] option.
10238*/
10239SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
10240
10241/*
10242** CAPI3REF: Serialize a database
10243**
10244** The sqlite3_serialize(D,S,P,F) interface returns a pointer to memory
10245** that is a serialization of the S database on [database connection] D.
10246** If P is not a NULL pointer, then the size of the database in bytes
10247** is written into *P.
10248**
10249** For an ordinary on-disk database file, the serialization is just a
10250** copy of the disk file. For an in-memory database or a "TEMP" database,
10251** the serialization is the same sequence of bytes which would be written
10252** to disk if that database where backed up to disk.
10253**
10254** The usual case is that sqlite3_serialize() copies the serialization of
10255** the database into memory obtained from [sqlite3_malloc64()] and returns
10256** a pointer to that memory. The caller is responsible for freeing the
10257** returned value to avoid a memory leak. However, if the F argument
10258** contains the SQLITE_SERIALIZE_NOCOPY bit, then no memory allocations
10259** are made, and the sqlite3_serialize() function will return a pointer
10260** to the contiguous memory representation of the database that SQLite
10261** is currently using for that database, or NULL if the no such contiguous
10262** memory representation of the database exists. A contiguous memory
10263** representation of the database will usually only exist if there has
10264** been a prior call to [sqlite3_deserialize(D,S,...)] with the same
10265** values of D and S.
10266** The size of the database is written into *P even if the
10267** SQLITE_SERIALIZE_NOCOPY bit is set but no contiguous copy
10268** of the database exists.
10269**
10270** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
10271** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
10272** allocation error occurs.
10273**
10274** This interface is omitted if SQLite is compiled with the
10275** [SQLITE_OMIT_DESERIALIZE] option.
10276*/
10277SQLITE_API unsigned char *sqlite3_serialize(
10278 sqlite3 *db, /* The database connection */
10279 const char *zSchema, /* Which DB to serialize. ex: "main", "temp", ... */
10280 sqlite3_int64 *piSize, /* Write size of the DB here, if not NULL */
10281 unsigned int mFlags /* Zero or more SQLITE_SERIALIZE_* flags */
10282);
10283
10284/*
10285** CAPI3REF: Flags for sqlite3_serialize
10286**
10287** Zero or more of the following constants can be OR-ed together for
10288** the F argument to [sqlite3_serialize(D,S,P,F)].
10289**
10290** SQLITE_SERIALIZE_NOCOPY means that [sqlite3_serialize()] will return
10291** a pointer to contiguous in-memory database that it is currently using,
10292** without making a copy of the database. If SQLite is not currently using
10293** a contiguous in-memory database, then this option causes
10294** [sqlite3_serialize()] to return a NULL pointer. SQLite will only be
10295** using a contiguous in-memory database if it has been initialized by a
10296** prior call to [sqlite3_deserialize()].
10297*/
10298#define SQLITE_SERIALIZE_NOCOPY 0x001 /* Do no memory allocations */
10299
10300/*
10301** CAPI3REF: Deserialize a database
10302**
10303** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the
10304** [database connection] D to disconnect from database S and then
10305** reopen S as an in-memory database based on the serialization contained
10306** in P. The serialized database P is N bytes in size. M is the size of
10307** the buffer P, which might be larger than N. If M is larger than N, and
10308** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
10309** permitted to add content to the in-memory database as long as the total
10310** size does not exceed M bytes.
10311**
10312** If the SQLITE_DESERIALIZE_FREEONCLOSE bit is set in F, then SQLite will
10313** invoke sqlite3_free() on the serialization buffer when the database
10314** connection closes. If the SQLITE_DESERIALIZE_RESIZEABLE bit is set, then
10315** SQLite will try to increase the buffer size using sqlite3_realloc64()
10316** if writes on the database cause it to grow larger than M bytes.
10317**
10318** The sqlite3_deserialize() interface will fail with SQLITE_BUSY if the
10319** database is currently in a read transaction or is involved in a backup
10320** operation.
10321**
10322** It is not possible to deserialized into the TEMP database. If the
10323** S argument to sqlite3_deserialize(D,S,P,N,M,F) is "temp" then the
10324** function returns SQLITE_ERROR.
10325**
10326** If sqlite3_deserialize(D,S,P,N,M,F) fails for any reason and if the
10327** SQLITE_DESERIALIZE_FREEONCLOSE bit is set in argument F, then
10328** [sqlite3_free()] is invoked on argument P prior to returning.
10329**
10330** This interface is omitted if SQLite is compiled with the
10331** [SQLITE_OMIT_DESERIALIZE] option.
10332*/
10333SQLITE_API int sqlite3_deserialize(
10334 sqlite3 *db, /* The database connection */
10335 const char *zSchema, /* Which DB to reopen with the deserialization */
10336 unsigned char *pData, /* The serialized database content */
10337 sqlite3_int64 szDb, /* Number bytes in the deserialization */
10338 sqlite3_int64 szBuf, /* Total size of buffer pData[] */
10339 unsigned mFlags /* Zero or more SQLITE_DESERIALIZE_* flags */
10340);
10341
10342/*
10343** CAPI3REF: Flags for sqlite3_deserialize()
10344**
10345** The following are allowed values for 6th argument (the F argument) to
10346** the [sqlite3_deserialize(D,S,P,N,M,F)] interface.
10347**
10348** The SQLITE_DESERIALIZE_FREEONCLOSE means that the database serialization
10349** in the P argument is held in memory obtained from [sqlite3_malloc64()]
10350** and that SQLite should take ownership of this memory and automatically
10351** free it when it has finished using it. Without this flag, the caller
10352** is responsible for freeing any dynamically allocated memory.
10353**
10354** The SQLITE_DESERIALIZE_RESIZEABLE flag means that SQLite is allowed to
10355** grow the size of the database using calls to [sqlite3_realloc64()]. This
10356** flag should only be used if SQLITE_DESERIALIZE_FREEONCLOSE is also used.
10357** Without this flag, the deserialized database cannot increase in size beyond
10358** the number of bytes specified by the M parameter.
10359**
10360** The SQLITE_DESERIALIZE_READONLY flag means that the deserialized database
10361** should be treated as read-only.
10362*/
10363#define SQLITE_DESERIALIZE_FREEONCLOSE 1 /* Call sqlite3_free() on close */
10364#define SQLITE_DESERIALIZE_RESIZEABLE 2 /* Resize using sqlite3_realloc64() */
10365#define SQLITE_DESERIALIZE_READONLY 4 /* Database is read-only */
10366
10367/*
10368** Undo the hack that converts floating point types to integer for
10369** builds on processors without floating point support.
10370*/
10371#ifdef SQLITE_OMIT_FLOATING_POINT
10372# undef double
10373#endif
10374
10375#if 0
10376} /* End of the 'extern "C"' block */
10377#endif
10378#endif /* SQLITE3_H */
10379
10380/******** Begin file sqlite3rtree.h *********/
10381/*
10382** 2010 August 30
10383**
10384** The author disclaims copyright to this source code. In place of
10385** a legal notice, here is a blessing:
10386**
10387** May you do good and not evil.
10388** May you find forgiveness for yourself and forgive others.
10389** May you share freely, never taking more than you give.
10390**
10391*************************************************************************
10392*/
10393
10394#ifndef _SQLITE3RTREE_H_
10395#define _SQLITE3RTREE_H_
10396
10397
10398#if 0
10399extern "C" {
10400#endif
10401
10402typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
10403typedef struct sqlite3_rtree_query_info sqlite3_rtree_query_info;
10404
10405/* The double-precision datatype used by RTree depends on the
10406** SQLITE_RTREE_INT_ONLY compile-time option.
10407*/
10408#ifdef SQLITE_RTREE_INT_ONLY
10409 typedef sqlite3_int64 sqlite3_rtree_dbl;
10410#else
10411 typedef double sqlite3_rtree_dbl;
10412#endif
10413
10414/*
10415** Register a geometry callback named zGeom that can be used as part of an
10416** R-Tree geometry query as follows:
10417**
10418** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
10419*/
10420SQLITE_API int sqlite3_rtree_geometry_callback(
10421 sqlite3 *db,
10422 const char *zGeom,
10423 int (*xGeom)(sqlite3_rtree_geometry*, int, sqlite3_rtree_dbl*,int*),
10424 void *pContext
10425);
10426
10427
10428/*
10429** A pointer to a structure of the following type is passed as the first
10430** argument to callbacks registered using rtree_geometry_callback().
10431*/
10432struct sqlite3_rtree_geometry {
10433 void *pContext; /* Copy of pContext passed to s_r_g_c() */
10434 int nParam; /* Size of array aParam[] */
10435 sqlite3_rtree_dbl *aParam; /* Parameters passed to SQL geom function */
10436 void *pUser; /* Callback implementation user data */
10437 void (*xDelUser)(void *); /* Called by SQLite to clean up pUser */
10438};
10439
10440/*
10441** Register a 2nd-generation geometry callback named zScore that can be
10442** used as part of an R-Tree geometry query as follows:
10443**
10444** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zQueryFunc(... params ...)
10445*/
10446SQLITE_API int sqlite3_rtree_query_callback(
10447 sqlite3 *db,
10448 const char *zQueryFunc,
10449 int (*xQueryFunc)(sqlite3_rtree_query_info*),
10450 void *pContext,
10451 void (*xDestructor)(void*)
10452);
10453
10454
10455/*
10456** A pointer to a structure of the following type is passed as the
10457** argument to scored geometry callback registered using
10458** sqlite3_rtree_query_callback().
10459**
10460** Note that the first 5 fields of this structure are identical to
10461** sqlite3_rtree_geometry. This structure is a subclass of
10462** sqlite3_rtree_geometry.
10463*/
10464struct sqlite3_rtree_query_info {
10465 void *pContext; /* pContext from when function registered */
10466 int nParam; /* Number of function parameters */
10467 sqlite3_rtree_dbl *aParam; /* value of function parameters */
10468 void *pUser; /* callback can use this, if desired */
10469 void (*xDelUser)(void*); /* function to free pUser */
10470 sqlite3_rtree_dbl *aCoord; /* Coordinates of node or entry to check */
10471 unsigned int *anQueue; /* Number of pending entries in the queue */
10472 int nCoord; /* Number of coordinates */
10473 int iLevel; /* Level of current node or entry */
10474 int mxLevel; /* The largest iLevel value in the tree */
10475 sqlite3_int64 iRowid; /* Rowid for current entry */
10476 sqlite3_rtree_dbl rParentScore; /* Score of parent node */
10477 int eParentWithin; /* Visibility of parent node */
10478 int eWithin; /* OUT: Visibility */
10479 sqlite3_rtree_dbl rScore; /* OUT: Write the score here */
10480 /* The following fields are only available in 3.8.11 and later */
10481 sqlite3_value **apSqlParam; /* Original SQL values of parameters */
10482};
10483
10484/*
10485** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin.
10486*/
10487#define NOT_WITHIN 0 /* Object completely outside of query region */
10488#define PARTLY_WITHIN 1 /* Object partially overlaps query region */
10489#define FULLY_WITHIN 2 /* Object fully contained within query region */
10490
10491
10492#if 0
10493} /* end of the 'extern "C"' block */
10494#endif
10495
10496#endif /* ifndef _SQLITE3RTREE_H_ */
10497
10498/******** End of sqlite3rtree.h *********/
10499/******** Begin file sqlite3session.h *********/
10500
10501#if !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION)
10502#define __SQLITESESSION_H_ 1
10503
10504/*
10505** Make sure we can call this stuff from C++.
10506*/
10507#if 0
10508extern "C" {
10509#endif
10510
10511
10512/*
10513** CAPI3REF: Session Object Handle
10514**
10515** An instance of this object is a [session] that can be used to
10516** record changes to a database.
10517*/
10518typedef struct sqlite3_session sqlite3_session;
10519
10520/*
10521** CAPI3REF: Changeset Iterator Handle
10522**
10523** An instance of this object acts as a cursor for iterating
10524** over the elements of a [changeset] or [patchset].
10525*/
10526typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
10527
10528/*
10529** CAPI3REF: Create A New Session Object
10530** CONSTRUCTOR: sqlite3_session
10531**
10532** Create a new session object attached to database handle db. If successful,
10533** a pointer to the new object is written to *ppSession and SQLITE_OK is
10534** returned. If an error occurs, *ppSession is set to NULL and an SQLite
10535** error code (e.g. SQLITE_NOMEM) is returned.
10536**
10537** It is possible to create multiple session objects attached to a single
10538** database handle.
10539**
10540** Session objects created using this function should be deleted using the
10541** [sqlite3session_delete()] function before the database handle that they
10542** are attached to is itself closed. If the database handle is closed before
10543** the session object is deleted, then the results of calling any session
10544** module function, including [sqlite3session_delete()] on the session object
10545** are undefined.
10546**
10547** Because the session module uses the [sqlite3_preupdate_hook()] API, it
10548** is not possible for an application to register a pre-update hook on a
10549** database handle that has one or more session objects attached. Nor is
10550** it possible to create a session object attached to a database handle for
10551** which a pre-update hook is already defined. The results of attempting
10552** either of these things are undefined.
10553**
10554** The session object will be used to create changesets for tables in
10555** database zDb, where zDb is either "main", or "temp", or the name of an
10556** attached database. It is not an error if database zDb is not attached
10557** to the database when the session object is created.
10558*/
10559SQLITE_API int sqlite3session_create(
10560 sqlite3 *db, /* Database handle */
10561 const char *zDb, /* Name of db (e.g. "main") */
10562 sqlite3_session **ppSession /* OUT: New session object */
10563);
10564
10565/*
10566** CAPI3REF: Delete A Session Object
10567** DESTRUCTOR: sqlite3_session
10568**
10569** Delete a session object previously allocated using
10570** [sqlite3session_create()]. Once a session object has been deleted, the
10571** results of attempting to use pSession with any other session module
10572** function are undefined.
10573**
10574** Session objects must be deleted before the database handle to which they
10575** are attached is closed. Refer to the documentation for
10576** [sqlite3session_create()] for details.
10577*/
10578SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
10579
10580/*
10581** CAPIREF: Conigure a Session Object
10582** METHOD: sqlite3_session
10583**
10584** This method is used to configure a session object after it has been
10585** created. At present the only valid value for the second parameter is
10586** [SQLITE_SESSION_OBJCONFIG_SIZE].
10587**
10588** Arguments for sqlite3session_object_config()
10589**
10590** The following values may passed as the the 4th parameter to
10591** sqlite3session_object_config().
10592**
10593** <dt>SQLITE_SESSION_OBJCONFIG_SIZE <dd>
10594** This option is used to set, clear or query the flag that enables
10595** the [sqlite3session_changeset_size()] API. Because it imposes some
10596** computational overhead, this API is disabled by default. Argument
10597** pArg must point to a value of type (int). If the value is initially
10598** 0, then the sqlite3session_changeset_size() API is disabled. If it
10599** is greater than 0, then the same API is enabled. Or, if the initial
10600** value is less than zero, no change is made. In all cases the (int)
10601** variable is set to 1 if the sqlite3session_changeset_size() API is
10602** enabled following the current call, or 0 otherwise.
10603**
10604** It is an error (SQLITE_MISUSE) to attempt to modify this setting after
10605** the first table has been attached to the session object.
10606*/
10607SQLITE_API int sqlite3session_object_config(sqlite3_session*, int op, void *pArg);
10608
10609/*
10610*/
10611#define SQLITE_SESSION_OBJCONFIG_SIZE 1
10612
10613/*
10614** CAPI3REF: Enable Or Disable A Session Object
10615** METHOD: sqlite3_session
10616**
10617** Enable or disable the recording of changes by a session object. When
10618** enabled, a session object records changes made to the database. When
10619** disabled - it does not. A newly created session object is enabled.
10620** Refer to the documentation for [sqlite3session_changeset()] for further
10621** details regarding how enabling and disabling a session object affects
10622** the eventual changesets.
10623**
10624** Passing zero to this function disables the session. Passing a value
10625** greater than zero enables it. Passing a value less than zero is a
10626** no-op, and may be used to query the current state of the session.
10627**
10628** The return value indicates the final state of the session object: 0 if
10629** the session is disabled, or 1 if it is enabled.
10630*/
10631SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
10632
10633/*
10634** CAPI3REF: Set Or Clear the Indirect Change Flag
10635** METHOD: sqlite3_session
10636**
10637** Each change recorded by a session object is marked as either direct or
10638** indirect. A change is marked as indirect if either:
10639**
10640** <ul>
10641** <li> The session object "indirect" flag is set when the change is
10642** made, or
10643** <li> The change is made by an SQL trigger or foreign key action
10644** instead of directly as a result of a users SQL statement.
10645** </ul>
10646**
10647** If a single row is affected by more than one operation within a session,
10648** then the change is considered indirect if all operations meet the criteria
10649** for an indirect change above, or direct otherwise.
10650**
10651** This function is used to set, clear or query the session object indirect
10652** flag. If the second argument passed to this function is zero, then the
10653** indirect flag is cleared. If it is greater than zero, the indirect flag
10654** is set. Passing a value less than zero does not modify the current value
10655** of the indirect flag, and may be used to query the current state of the
10656** indirect flag for the specified session object.
10657**
10658** The return value indicates the final state of the indirect flag: 0 if
10659** it is clear, or 1 if it is set.
10660*/
10661SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
10662
10663/*
10664** CAPI3REF: Attach A Table To A Session Object
10665** METHOD: sqlite3_session
10666**
10667** If argument zTab is not NULL, then it is the name of a table to attach
10668** to the session object passed as the first argument. All subsequent changes
10669** made to the table while the session object is enabled will be recorded. See
10670** documentation for [sqlite3session_changeset()] for further details.
10671**
10672** Or, if argument zTab is NULL, then changes are recorded for all tables
10673** in the database. If additional tables are added to the database (by
10674** executing "CREATE TABLE" statements) after this call is made, changes for
10675** the new tables are also recorded.
10676**
10677** Changes can only be recorded for tables that have a PRIMARY KEY explicitly
10678** defined as part of their CREATE TABLE statement. It does not matter if the
10679** PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias) or not. The PRIMARY
10680** KEY may consist of a single column, or may be a composite key.
10681**
10682** It is not an error if the named table does not exist in the database. Nor
10683** is it an error if the named table does not have a PRIMARY KEY. However,
10684** no changes will be recorded in either of these scenarios.
10685**
10686** Changes are not recorded for individual rows that have NULL values stored
10687** in one or more of their PRIMARY KEY columns.
10688**
10689** SQLITE_OK is returned if the call completes without error. Or, if an error
10690** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
10691**
10692** <h3>Special sqlite_stat1 Handling</h3>
10693**
10694** As of SQLite version 3.22.0, the "sqlite_stat1" table is an exception to
10695** some of the rules above. In SQLite, the schema of sqlite_stat1 is:
10696** <pre>
10697** &nbsp; CREATE TABLE sqlite_stat1(tbl,idx,stat)
10698** </pre>
10699**
10700** Even though sqlite_stat1 does not have a PRIMARY KEY, changes are
10701** recorded for it as if the PRIMARY KEY is (tbl,idx). Additionally, changes
10702** are recorded for rows for which (idx IS NULL) is true. However, for such
10703** rows a zero-length blob (SQL value X'') is stored in the changeset or
10704** patchset instead of a NULL value. This allows such changesets to be
10705** manipulated by legacy implementations of sqlite3changeset_invert(),
10706** concat() and similar.
10707**
10708** The sqlite3changeset_apply() function automatically converts the
10709** zero-length blob back to a NULL value when updating the sqlite_stat1
10710** table. However, if the application calls sqlite3changeset_new(),
10711** sqlite3changeset_old() or sqlite3changeset_conflict on a changeset
10712** iterator directly (including on a changeset iterator passed to a
10713** conflict-handler callback) then the X'' value is returned. The application
10714** must translate X'' to NULL itself if required.
10715**
10716** Legacy (older than 3.22.0) versions of the sessions module cannot capture
10717** changes made to the sqlite_stat1 table. Legacy versions of the
10718** sqlite3changeset_apply() function silently ignore any modifications to the
10719** sqlite_stat1 table that are part of a changeset or patchset.
10720*/
10721SQLITE_API int sqlite3session_attach(
10722 sqlite3_session *pSession, /* Session object */
10723 const char *zTab /* Table name */
10724);
10725
10726/*
10727** CAPI3REF: Set a table filter on a Session Object.
10728** METHOD: sqlite3_session
10729**
10730** The second argument (xFilter) is the "filter callback". For changes to rows
10731** in tables that are not attached to the Session object, the filter is called
10732** to determine whether changes to the table's rows should be tracked or not.
10733** If xFilter returns 0, changes are not tracked. Note that once a table is
10734** attached, xFilter will not be called again.
10735*/
10736SQLITE_API void sqlite3session_table_filter(
10737 sqlite3_session *pSession, /* Session object */
10738 int(*xFilter)(
10739 void *pCtx, /* Copy of third arg to _filter_table() */
10740 const char *zTab /* Table name */
10741 ),
10742 void *pCtx /* First argument passed to xFilter */
10743);
10744
10745/*
10746** CAPI3REF: Generate A Changeset From A Session Object
10747** METHOD: sqlite3_session
10748**
10749** Obtain a changeset containing changes to the tables attached to the
10750** session object passed as the first argument. If successful,
10751** set *ppChangeset to point to a buffer containing the changeset
10752** and *pnChangeset to the size of the changeset in bytes before returning
10753** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to
10754** zero and return an SQLite error code.
10755**
10756** A changeset consists of zero or more INSERT, UPDATE and/or DELETE changes,
10757** each representing a change to a single row of an attached table. An INSERT
10758** change contains the values of each field of a new database row. A DELETE
10759** contains the original values of each field of a deleted database row. An
10760** UPDATE change contains the original values of each field of an updated
10761** database row along with the updated values for each updated non-primary-key
10762** column. It is not possible for an UPDATE change to represent a change that
10763** modifies the values of primary key columns. If such a change is made, it
10764** is represented in a changeset as a DELETE followed by an INSERT.
10765**
10766** Changes are not recorded for rows that have NULL values stored in one or
10767** more of their PRIMARY KEY columns. If such a row is inserted or deleted,
10768** no corresponding change is present in the changesets returned by this
10769** function. If an existing row with one or more NULL values stored in
10770** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL,
10771** only an INSERT is appears in the changeset. Similarly, if an existing row
10772** with non-NULL PRIMARY KEY values is updated so that one or more of its
10773** PRIMARY KEY columns are set to NULL, the resulting changeset contains a
10774** DELETE change only.
10775**
10776** The contents of a changeset may be traversed using an iterator created
10777** using the [sqlite3changeset_start()] API. A changeset may be applied to
10778** a database with a compatible schema using the [sqlite3changeset_apply()]
10779** API.
10780**
10781** Within a changeset generated by this function, all changes related to a
10782** single table are grouped together. In other words, when iterating through
10783** a changeset or when applying a changeset to a database, all changes related
10784** to a single table are processed before moving on to the next table. Tables
10785** are sorted in the same order in which they were attached (or auto-attached)
10786** to the sqlite3_session object. The order in which the changes related to
10787** a single table are stored is undefined.
10788**
10789** Following a successful call to this function, it is the responsibility of
10790** the caller to eventually free the buffer that *ppChangeset points to using
10791** [sqlite3_free()].
10792**
10793** <h3>Changeset Generation</h3>
10794**
10795** Once a table has been attached to a session object, the session object
10796** records the primary key values of all new rows inserted into the table.
10797** It also records the original primary key and other column values of any
10798** deleted or updated rows. For each unique primary key value, data is only
10799** recorded once - the first time a row with said primary key is inserted,
10800** updated or deleted in the lifetime of the session.
10801**
10802** There is one exception to the previous paragraph: when a row is inserted,
10803** updated or deleted, if one or more of its primary key columns contain a
10804** NULL value, no record of the change is made.
10805**
10806** The session object therefore accumulates two types of records - those
10807** that consist of primary key values only (created when the user inserts
10808** a new record) and those that consist of the primary key values and the
10809** original values of other table columns (created when the users deletes
10810** or updates a record).
10811**
10812** When this function is called, the requested changeset is created using
10813** both the accumulated records and the current contents of the database
10814** file. Specifically:
10815**
10816** <ul>
10817** <li> For each record generated by an insert, the database is queried
10818** for a row with a matching primary key. If one is found, an INSERT
10819** change is added to the changeset. If no such row is found, no change
10820** is added to the changeset.
10821**
10822** <li> For each record generated by an update or delete, the database is
10823** queried for a row with a matching primary key. If such a row is
10824** found and one or more of the non-primary key fields have been
10825** modified from their original values, an UPDATE change is added to
10826** the changeset. Or, if no such row is found in the table, a DELETE
10827** change is added to the changeset. If there is a row with a matching
10828** primary key in the database, but all fields contain their original
10829** values, no change is added to the changeset.
10830** </ul>
10831**
10832** This means, amongst other things, that if a row is inserted and then later
10833** deleted while a session object is active, neither the insert nor the delete
10834** will be present in the changeset. Or if a row is deleted and then later a
10835** row with the same primary key values inserted while a session object is
10836** active, the resulting changeset will contain an UPDATE change instead of
10837** a DELETE and an INSERT.
10838**
10839** When a session object is disabled (see the [sqlite3session_enable()] API),
10840** it does not accumulate records when rows are inserted, updated or deleted.
10841** This may appear to have some counter-intuitive effects if a single row
10842** is written to more than once during a session. For example, if a row
10843** is inserted while a session object is enabled, then later deleted while
10844** the same session object is disabled, no INSERT record will appear in the
10845** changeset, even though the delete took place while the session was disabled.
10846** Or, if one field of a row is updated while a session is disabled, and
10847** another field of the same row is updated while the session is enabled, the
10848** resulting changeset will contain an UPDATE change that updates both fields.
10849*/
10850SQLITE_API int sqlite3session_changeset(
10851 sqlite3_session *pSession, /* Session object */
10852 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
10853 void **ppChangeset /* OUT: Buffer containing changeset */
10854);
10855
10856/*
10857** CAPI3REF: Return An Upper-limit For The Size Of The Changeset
10858** METHOD: sqlite3_session
10859**
10860** By default, this function always returns 0. For it to return
10861** a useful result, the sqlite3_session object must have been configured
10862** to enable this API using sqlite3session_object_config() with the
10863** SQLITE_SESSION_OBJCONFIG_SIZE verb.
10864**
10865** When enabled, this function returns an upper limit, in bytes, for the size
10866** of the changeset that might be produced if sqlite3session_changeset() were
10867** called. The final changeset size might be equal to or smaller than the
10868** size in bytes returned by this function.
10869*/
10870SQLITE_API sqlite3_int64 sqlite3session_changeset_size(sqlite3_session *pSession);
10871
10872/*
10873** CAPI3REF: Load The Difference Between Tables Into A Session
10874** METHOD: sqlite3_session
10875**
10876** If it is not already attached to the session object passed as the first
10877** argument, this function attaches table zTbl in the same manner as the
10878** [sqlite3session_attach()] function. If zTbl does not exist, or if it
10879** does not have a primary key, this function is a no-op (but does not return
10880** an error).
10881**
10882** Argument zFromDb must be the name of a database ("main", "temp" etc.)
10883** attached to the same database handle as the session object that contains
10884** a table compatible with the table attached to the session by this function.
10885** A table is considered compatible if it:
10886**
10887** <ul>
10888** <li> Has the same name,
10889** <li> Has the same set of columns declared in the same order, and
10890** <li> Has the same PRIMARY KEY definition.
10891** </ul>
10892**
10893** If the tables are not compatible, SQLITE_SCHEMA is returned. If the tables
10894** are compatible but do not have any PRIMARY KEY columns, it is not an error
10895** but no changes are added to the session object. As with other session
10896** APIs, tables without PRIMARY KEYs are simply ignored.
10897**
10898** This function adds a set of changes to the session object that could be
10899** used to update the table in database zFrom (call this the "from-table")
10900** so that its content is the same as the table attached to the session
10901** object (call this the "to-table"). Specifically:
10902**
10903** <ul>
10904** <li> For each row (primary key) that exists in the to-table but not in
10905** the from-table, an INSERT record is added to the session object.
10906**
10907** <li> For each row (primary key) that exists in the to-table but not in
10908** the from-table, a DELETE record is added to the session object.
10909**
10910** <li> For each row (primary key) that exists in both tables, but features
10911** different non-PK values in each, an UPDATE record is added to the
10912** session.
10913** </ul>
10914**
10915** To clarify, if this function is called and then a changeset constructed
10916** using [sqlite3session_changeset()], then after applying that changeset to
10917** database zFrom the contents of the two compatible tables would be
10918** identical.
10919**
10920** It an error if database zFrom does not exist or does not contain the
10921** required compatible table.
10922**
10923** If the operation is successful, SQLITE_OK is returned. Otherwise, an SQLite
10924** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg
10925** may be set to point to a buffer containing an English language error
10926** message. It is the responsibility of the caller to free this buffer using
10927** sqlite3_free().
10928*/
10929SQLITE_API int sqlite3session_diff(
10930 sqlite3_session *pSession,
10931 const char *zFromDb,
10932 const char *zTbl,
10933 char **pzErrMsg
10934);
10935
10936
10937/*
10938** CAPI3REF: Generate A Patchset From A Session Object
10939** METHOD: sqlite3_session
10940**
10941** The differences between a patchset and a changeset are that:
10942**
10943** <ul>
10944** <li> DELETE records consist of the primary key fields only. The
10945** original values of other fields are omitted.
10946** <li> The original values of any modified fields are omitted from
10947** UPDATE records.
10948** </ul>
10949**
10950** A patchset blob may be used with up to date versions of all
10951** sqlite3changeset_xxx API functions except for sqlite3changeset_invert(),
10952** which returns SQLITE_CORRUPT if it is passed a patchset. Similarly,
10953** attempting to use a patchset blob with old versions of the
10954** sqlite3changeset_xxx APIs also provokes an SQLITE_CORRUPT error.
10955**
10956** Because the non-primary key "old.*" fields are omitted, no
10957** SQLITE_CHANGESET_DATA conflicts can be detected or reported if a patchset
10958** is passed to the sqlite3changeset_apply() API. Other conflict types work
10959** in the same way as for changesets.
10960**
10961** Changes within a patchset are ordered in the same way as for changesets
10962** generated by the sqlite3session_changeset() function (i.e. all changes for
10963** a single table are grouped together, tables appear in the order in which
10964** they were attached to the session object).
10965*/
10966SQLITE_API int sqlite3session_patchset(
10967 sqlite3_session *pSession, /* Session object */
10968 int *pnPatchset, /* OUT: Size of buffer at *ppPatchset */
10969 void **ppPatchset /* OUT: Buffer containing patchset */
10970);
10971
10972/*
10973** CAPI3REF: Test if a changeset has recorded any changes.
10974**
10975** Return non-zero if no changes to attached tables have been recorded by
10976** the session object passed as the first argument. Otherwise, if one or
10977** more changes have been recorded, return zero.
10978**
10979** Even if this function returns zero, it is possible that calling
10980** [sqlite3session_changeset()] on the session handle may still return a
10981** changeset that contains no changes. This can happen when a row in
10982** an attached table is modified and then later on the original values
10983** are restored. However, if this function returns non-zero, then it is
10984** guaranteed that a call to sqlite3session_changeset() will return a
10985** changeset containing zero changes.
10986*/
10987SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
10988
10989/*
10990** CAPI3REF: Query for the amount of heap memory used by a session object.
10991**
10992** This API returns the total amount of heap memory in bytes currently
10993** used by the session object passed as the only argument.
10994*/
10995SQLITE_API sqlite3_int64 sqlite3session_memory_used(sqlite3_session *pSession);
10996
10997/*
10998** CAPI3REF: Create An Iterator To Traverse A Changeset
10999** CONSTRUCTOR: sqlite3_changeset_iter
11000**
11001** Create an iterator used to iterate through the contents of a changeset.
11002** If successful, *pp is set to point to the iterator handle and SQLITE_OK
11003** is returned. Otherwise, if an error occurs, *pp is set to zero and an
11004** SQLite error code is returned.
11005**
11006** The following functions can be used to advance and query a changeset
11007** iterator created by this function:
11008**
11009** <ul>
11010** <li> [sqlite3changeset_next()]
11011** <li> [sqlite3changeset_op()]
11012** <li> [sqlite3changeset_new()]
11013** <li> [sqlite3changeset_old()]
11014** </ul>
11015**
11016** It is the responsibility of the caller to eventually destroy the iterator
11017** by passing it to [sqlite3changeset_finalize()]. The buffer containing the
11018** changeset (pChangeset) must remain valid until after the iterator is
11019** destroyed.
11020**
11021** Assuming the changeset blob was created by one of the
11022** [sqlite3session_changeset()], [sqlite3changeset_concat()] or
11023** [sqlite3changeset_invert()] functions, all changes within the changeset
11024** that apply to a single table are grouped together. This means that when
11025** an application iterates through a changeset using an iterator created by
11026** this function, all changes that relate to a single table are visited
11027** consecutively. There is no chance that the iterator will visit a change
11028** the applies to table X, then one for table Y, and then later on visit
11029** another change for table X.
11030**
11031** The behavior of sqlite3changeset_start_v2() and its streaming equivalent
11032** may be modified by passing a combination of
11033** [SQLITE_CHANGESETSTART_INVERT | supported flags] as the 4th parameter.
11034**
11035** Note that the sqlite3changeset_start_v2() API is still <b>experimental</b>
11036** and therefore subject to change.
11037*/
11038SQLITE_API int sqlite3changeset_start(
11039 sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */
11040 int nChangeset, /* Size of changeset blob in bytes */
11041 void *pChangeset /* Pointer to blob containing changeset */
11042);
11043SQLITE_API int sqlite3changeset_start_v2(
11044 sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */
11045 int nChangeset, /* Size of changeset blob in bytes */
11046 void *pChangeset, /* Pointer to blob containing changeset */
11047 int flags /* SESSION_CHANGESETSTART_* flags */
11048);
11049
11050/*
11051** CAPI3REF: Flags for sqlite3changeset_start_v2
11052**
11053** The following flags may passed via the 4th parameter to
11054** [sqlite3changeset_start_v2] and [sqlite3changeset_start_v2_strm]:
11055**
11056** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
11057** Invert the changeset while iterating through it. This is equivalent to
11058** inverting a changeset using sqlite3changeset_invert() before applying it.
11059** It is an error to specify this flag with a patchset.
11060*/
11061#define SQLITE_CHANGESETSTART_INVERT 0x0002
11062
11063
11064/*
11065** CAPI3REF: Advance A Changeset Iterator
11066** METHOD: sqlite3_changeset_iter
11067**
11068** This function may only be used with iterators created by the function
11069** [sqlite3changeset_start()]. If it is called on an iterator passed to
11070** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
11071** is returned and the call has no effect.
11072**
11073** Immediately after an iterator is created by sqlite3changeset_start(), it
11074** does not point to any change in the changeset. Assuming the changeset
11075** is not empty, the first call to this function advances the iterator to
11076** point to the first change in the changeset. Each subsequent call advances
11077** the iterator to point to the next change in the changeset (if any). If
11078** no error occurs and the iterator points to a valid change after a call
11079** to sqlite3changeset_next() has advanced it, SQLITE_ROW is returned.
11080** Otherwise, if all changes in the changeset have already been visited,
11081** SQLITE_DONE is returned.
11082**
11083** If an error occurs, an SQLite error code is returned. Possible error
11084** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or
11085** SQLITE_NOMEM.
11086*/
11087SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
11088
11089/*
11090** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
11091** METHOD: sqlite3_changeset_iter
11092**
11093** The pIter argument passed to this function may either be an iterator
11094** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11095** created by [sqlite3changeset_start()]. In the latter case, the most recent
11096** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
11097** is not the case, this function returns [SQLITE_MISUSE].
11098**
11099** Arguments pOp, pnCol and pzTab may not be NULL. Upon return, three
11100** outputs are set through these pointers:
11101**
11102** *pOp is set to one of [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE],
11103** depending on the type of change that the iterator currently points to;
11104**
11105** *pnCol is set to the number of columns in the table affected by the change; and
11106**
11107** *pzTab is set to point to a nul-terminated utf-8 encoded string containing
11108** the name of the table affected by the current change. The buffer remains
11109** valid until either sqlite3changeset_next() is called on the iterator
11110** or until the conflict-handler function returns.
11111**
11112** If pbIndirect is not NULL, then *pbIndirect is set to true (1) if the change
11113** is an indirect change, or false (0) otherwise. See the documentation for
11114** [sqlite3session_indirect()] for a description of direct and indirect
11115** changes.
11116**
11117** If no error occurs, SQLITE_OK is returned. If an error does occur, an
11118** SQLite error code is returned. The values of the output variables may not
11119** be trusted in this case.
11120*/
11121SQLITE_API int sqlite3changeset_op(
11122 sqlite3_changeset_iter *pIter, /* Iterator object */
11123 const char **pzTab, /* OUT: Pointer to table name */
11124 int *pnCol, /* OUT: Number of columns in table */
11125 int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */
11126 int *pbIndirect /* OUT: True for an 'indirect' change */
11127);
11128
11129/*
11130** CAPI3REF: Obtain The Primary Key Definition Of A Table
11131** METHOD: sqlite3_changeset_iter
11132**
11133** For each modified table, a changeset includes the following:
11134**
11135** <ul>
11136** <li> The number of columns in the table, and
11137** <li> Which of those columns make up the tables PRIMARY KEY.
11138** </ul>
11139**
11140** This function is used to find which columns comprise the PRIMARY KEY of
11141** the table modified by the change that iterator pIter currently points to.
11142** If successful, *pabPK is set to point to an array of nCol entries, where
11143** nCol is the number of columns in the table. Elements of *pabPK are set to
11144** 0x01 if the corresponding column is part of the tables primary key, or
11145** 0x00 if it is not.
11146**
11147** If argument pnCol is not NULL, then *pnCol is set to the number of columns
11148** in the table.
11149**
11150** If this function is called when the iterator does not point to a valid
11151** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise,
11152** SQLITE_OK is returned and the output variables populated as described
11153** above.
11154*/
11155SQLITE_API int sqlite3changeset_pk(
11156 sqlite3_changeset_iter *pIter, /* Iterator object */
11157 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
11158 int *pnCol /* OUT: Number of entries in output array */
11159);
11160
11161/*
11162** CAPI3REF: Obtain old.* Values From A Changeset Iterator
11163** METHOD: sqlite3_changeset_iter
11164**
11165** The pIter argument passed to this function may either be an iterator
11166** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11167** created by [sqlite3changeset_start()]. In the latter case, the most recent
11168** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
11169** Furthermore, it may only be called if the type of change that the iterator
11170** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise,
11171** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
11172**
11173** Argument iVal must be greater than or equal to 0, and less than the number
11174** of columns in the table affected by the current change. Otherwise,
11175** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
11176**
11177** If successful, this function sets *ppValue to point to a protected
11178** sqlite3_value object containing the iVal'th value from the vector of
11179** original row values stored as part of the UPDATE or DELETE change and
11180** returns SQLITE_OK. The name of the function comes from the fact that this
11181** is similar to the "old.*" columns available to update or delete triggers.
11182**
11183** If some other error occurs (e.g. an OOM condition), an SQLite error code
11184** is returned and *ppValue is set to NULL.
11185*/
11186SQLITE_API int sqlite3changeset_old(
11187 sqlite3_changeset_iter *pIter, /* Changeset iterator */
11188 int iVal, /* Column number */
11189 sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
11190);
11191
11192/*
11193** CAPI3REF: Obtain new.* Values From A Changeset Iterator
11194** METHOD: sqlite3_changeset_iter
11195**
11196** The pIter argument passed to this function may either be an iterator
11197** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11198** created by [sqlite3changeset_start()]. In the latter case, the most recent
11199** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
11200** Furthermore, it may only be called if the type of change that the iterator
11201** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise,
11202** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
11203**
11204** Argument iVal must be greater than or equal to 0, and less than the number
11205** of columns in the table affected by the current change. Otherwise,
11206** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
11207**
11208** If successful, this function sets *ppValue to point to a protected
11209** sqlite3_value object containing the iVal'th value from the vector of
11210** new row values stored as part of the UPDATE or INSERT change and
11211** returns SQLITE_OK. If the change is an UPDATE and does not include
11212** a new value for the requested column, *ppValue is set to NULL and
11213** SQLITE_OK returned. The name of the function comes from the fact that
11214** this is similar to the "new.*" columns available to update or delete
11215** triggers.
11216**
11217** If some other error occurs (e.g. an OOM condition), an SQLite error code
11218** is returned and *ppValue is set to NULL.
11219*/
11220SQLITE_API int sqlite3changeset_new(
11221 sqlite3_changeset_iter *pIter, /* Changeset iterator */
11222 int iVal, /* Column number */
11223 sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
11224);
11225
11226/*
11227** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
11228** METHOD: sqlite3_changeset_iter
11229**
11230** This function should only be used with iterator objects passed to a
11231** conflict-handler callback by [sqlite3changeset_apply()] with either
11232** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
11233** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
11234** is set to NULL.
11235**
11236** Argument iVal must be greater than or equal to 0, and less than the number
11237** of columns in the table affected by the current change. Otherwise,
11238** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
11239**
11240** If successful, this function sets *ppValue to point to a protected
11241** sqlite3_value object containing the iVal'th value from the
11242** "conflicting row" associated with the current conflict-handler callback
11243** and returns SQLITE_OK.
11244**
11245** If some other error occurs (e.g. an OOM condition), an SQLite error code
11246** is returned and *ppValue is set to NULL.
11247*/
11248SQLITE_API int sqlite3changeset_conflict(
11249 sqlite3_changeset_iter *pIter, /* Changeset iterator */
11250 int iVal, /* Column number */
11251 sqlite3_value **ppValue /* OUT: Value from conflicting row */
11252);
11253
11254/*
11255** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
11256** METHOD: sqlite3_changeset_iter
11257**
11258** This function may only be called with an iterator passed to an
11259** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
11260** it sets the output variable to the total number of known foreign key
11261** violations in the destination database and returns SQLITE_OK.
11262**
11263** In all other cases this function returns SQLITE_MISUSE.
11264*/
11265SQLITE_API int sqlite3changeset_fk_conflicts(
11266 sqlite3_changeset_iter *pIter, /* Changeset iterator */
11267 int *pnOut /* OUT: Number of FK violations */
11268);
11269
11270
11271/*
11272** CAPI3REF: Finalize A Changeset Iterator
11273** METHOD: sqlite3_changeset_iter
11274**
11275** This function is used to finalize an iterator allocated with
11276** [sqlite3changeset_start()].
11277**
11278** This function should only be called on iterators created using the
11279** [sqlite3changeset_start()] function. If an application calls this
11280** function with an iterator passed to a conflict-handler by
11281** [sqlite3changeset_apply()], [SQLITE_MISUSE] is immediately returned and the
11282** call has no effect.
11283**
11284** If an error was encountered within a call to an sqlite3changeset_xxx()
11285** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an
11286** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
11287** to that error is returned by this function. Otherwise, SQLITE_OK is
11288** returned. This is to allow the following pattern (pseudo-code):
11289**
11290** <pre>
11291** sqlite3changeset_start();
11292** while( SQLITE_ROW==sqlite3changeset_next() ){
11293** // Do something with change.
11294** }
11295** rc = sqlite3changeset_finalize();
11296** if( rc!=SQLITE_OK ){
11297** // An error has occurred
11298** }
11299** </pre>
11300*/
11301SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
11302
11303/*
11304** CAPI3REF: Invert A Changeset
11305**
11306** This function is used to "invert" a changeset object. Applying an inverted
11307** changeset to a database reverses the effects of applying the uninverted
11308** changeset. Specifically:
11309**
11310** <ul>
11311** <li> Each DELETE change is changed to an INSERT, and
11312** <li> Each INSERT change is changed to a DELETE, and
11313** <li> For each UPDATE change, the old.* and new.* values are exchanged.
11314** </ul>
11315**
11316** This function does not change the order in which changes appear within
11317** the changeset. It merely reverses the sense of each individual change.
11318**
11319** If successful, a pointer to a buffer containing the inverted changeset
11320** is stored in *ppOut, the size of the same buffer is stored in *pnOut, and
11321** SQLITE_OK is returned. If an error occurs, both *pnOut and *ppOut are
11322** zeroed and an SQLite error code returned.
11323**
11324** It is the responsibility of the caller to eventually call sqlite3_free()
11325** on the *ppOut pointer to free the buffer allocation following a successful
11326** call to this function.
11327**
11328** WARNING/TODO: This function currently assumes that the input is a valid
11329** changeset. If it is not, the results are undefined.
11330*/
11331SQLITE_API int sqlite3changeset_invert(
11332 int nIn, const void *pIn, /* Input changeset */
11333 int *pnOut, void **ppOut /* OUT: Inverse of input */
11334);
11335
11336/*
11337** CAPI3REF: Concatenate Two Changeset Objects
11338**
11339** This function is used to concatenate two changesets, A and B, into a
11340** single changeset. The result is a changeset equivalent to applying
11341** changeset A followed by changeset B.
11342**
11343** This function combines the two input changesets using an
11344** sqlite3_changegroup object. Calling it produces similar results as the
11345** following code fragment:
11346**
11347** <pre>
11348** sqlite3_changegroup *pGrp;
11349** rc = sqlite3_changegroup_new(&pGrp);
11350** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
11351** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
11352** if( rc==SQLITE_OK ){
11353** rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
11354** }else{
11355** *ppOut = 0;
11356** *pnOut = 0;
11357** }
11358** </pre>
11359**
11360** Refer to the sqlite3_changegroup documentation below for details.
11361*/
11362SQLITE_API int sqlite3changeset_concat(
11363 int nA, /* Number of bytes in buffer pA */
11364 void *pA, /* Pointer to buffer containing changeset A */
11365 int nB, /* Number of bytes in buffer pB */
11366 void *pB, /* Pointer to buffer containing changeset B */
11367 int *pnOut, /* OUT: Number of bytes in output changeset */
11368 void **ppOut /* OUT: Buffer containing output changeset */
11369);
11370
11371
11372/*
11373** CAPI3REF: Changegroup Handle
11374**
11375** A changegroup is an object used to combine two or more
11376** [changesets] or [patchsets]
11377*/
11378typedef struct sqlite3_changegroup sqlite3_changegroup;
11379
11380/*
11381** CAPI3REF: Create A New Changegroup Object
11382** CONSTRUCTOR: sqlite3_changegroup
11383**
11384** An sqlite3_changegroup object is used to combine two or more changesets
11385** (or patchsets) into a single changeset (or patchset). A single changegroup
11386** object may combine changesets or patchsets, but not both. The output is
11387** always in the same format as the input.
11388**
11389** If successful, this function returns SQLITE_OK and populates (*pp) with
11390** a pointer to a new sqlite3_changegroup object before returning. The caller
11391** should eventually free the returned object using a call to
11392** sqlite3changegroup_delete(). If an error occurs, an SQLite error code
11393** (i.e. SQLITE_NOMEM) is returned and *pp is set to NULL.
11394**
11395** The usual usage pattern for an sqlite3_changegroup object is as follows:
11396**
11397** <ul>
11398** <li> It is created using a call to sqlite3changegroup_new().
11399**
11400** <li> Zero or more changesets (or patchsets) are added to the object
11401** by calling sqlite3changegroup_add().
11402**
11403** <li> The result of combining all input changesets together is obtained
11404** by the application via a call to sqlite3changegroup_output().
11405**
11406** <li> The object is deleted using a call to sqlite3changegroup_delete().
11407** </ul>
11408**
11409** Any number of calls to add() and output() may be made between the calls to
11410** new() and delete(), and in any order.
11411**
11412** As well as the regular sqlite3changegroup_add() and
11413** sqlite3changegroup_output() functions, also available are the streaming
11414** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm().
11415*/
11416SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp);
11417
11418/*
11419** CAPI3REF: Add A Changeset To A Changegroup
11420** METHOD: sqlite3_changegroup
11421**
11422** Add all changes within the changeset (or patchset) in buffer pData (size
11423** nData bytes) to the changegroup.
11424**
11425** If the buffer contains a patchset, then all prior calls to this function
11426** on the same changegroup object must also have specified patchsets. Or, if
11427** the buffer contains a changeset, so must have the earlier calls to this
11428** function. Otherwise, SQLITE_ERROR is returned and no changes are added
11429** to the changegroup.
11430**
11431** Rows within the changeset and changegroup are identified by the values in
11432** their PRIMARY KEY columns. A change in the changeset is considered to
11433** apply to the same row as a change already present in the changegroup if
11434** the two rows have the same primary key.
11435**
11436** Changes to rows that do not already appear in the changegroup are
11437** simply copied into it. Or, if both the new changeset and the changegroup
11438** contain changes that apply to a single row, the final contents of the
11439** changegroup depends on the type of each change, as follows:
11440**
11441** <table border=1 style="margin-left:8ex;margin-right:8ex">
11442** <tr><th style="white-space:pre">Existing Change </th>
11443** <th style="white-space:pre">New Change </th>
11444** <th>Output Change
11445** <tr><td>INSERT <td>INSERT <td>
11446** The new change is ignored. This case does not occur if the new
11447** changeset was recorded immediately after the changesets already
11448** added to the changegroup.
11449** <tr><td>INSERT <td>UPDATE <td>
11450** The INSERT change remains in the changegroup. The values in the
11451** INSERT change are modified as if the row was inserted by the
11452** existing change and then updated according to the new change.
11453** <tr><td>INSERT <td>DELETE <td>
11454** The existing INSERT is removed from the changegroup. The DELETE is
11455** not added.
11456** <tr><td>UPDATE <td>INSERT <td>
11457** The new change is ignored. This case does not occur if the new
11458** changeset was recorded immediately after the changesets already
11459** added to the changegroup.
11460** <tr><td>UPDATE <td>UPDATE <td>
11461** The existing UPDATE remains within the changegroup. It is amended
11462** so that the accompanying values are as if the row was updated once
11463** by the existing change and then again by the new change.
11464** <tr><td>UPDATE <td>DELETE <td>
11465** The existing UPDATE is replaced by the new DELETE within the
11466** changegroup.
11467** <tr><td>DELETE <td>INSERT <td>
11468** If one or more of the column values in the row inserted by the
11469** new change differ from those in the row deleted by the existing
11470** change, the existing DELETE is replaced by an UPDATE within the
11471** changegroup. Otherwise, if the inserted row is exactly the same
11472** as the deleted row, the existing DELETE is simply discarded.
11473** <tr><td>DELETE <td>UPDATE <td>
11474** The new change is ignored. This case does not occur if the new
11475** changeset was recorded immediately after the changesets already
11476** added to the changegroup.
11477** <tr><td>DELETE <td>DELETE <td>
11478** The new change is ignored. This case does not occur if the new
11479** changeset was recorded immediately after the changesets already
11480** added to the changegroup.
11481** </table>
11482**
11483** If the new changeset contains changes to a table that is already present
11484** in the changegroup, then the number of columns and the position of the
11485** primary key columns for the table must be consistent. If this is not the
11486** case, this function fails with SQLITE_SCHEMA. If the input changeset
11487** appears to be corrupt and the corruption is detected, SQLITE_CORRUPT is
11488** returned. Or, if an out-of-memory condition occurs during processing, this
11489** function returns SQLITE_NOMEM. In all cases, if an error occurs the state
11490** of the final contents of the changegroup is undefined.
11491**
11492** If no error occurs, SQLITE_OK is returned.
11493*/
11494SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
11495
11496/*
11497** CAPI3REF: Obtain A Composite Changeset From A Changegroup
11498** METHOD: sqlite3_changegroup
11499**
11500** Obtain a buffer containing a changeset (or patchset) representing the
11501** current contents of the changegroup. If the inputs to the changegroup
11502** were themselves changesets, the output is a changeset. Or, if the
11503** inputs were patchsets, the output is also a patchset.
11504**
11505** As with the output of the sqlite3session_changeset() and
11506** sqlite3session_patchset() functions, all changes related to a single
11507** table are grouped together in the output of this function. Tables appear
11508** in the same order as for the very first changeset added to the changegroup.
11509** If the second or subsequent changesets added to the changegroup contain
11510** changes for tables that do not appear in the first changeset, they are
11511** appended onto the end of the output changeset, again in the order in
11512** which they are first encountered.
11513**
11514** If an error occurs, an SQLite error code is returned and the output
11515** variables (*pnData) and (*ppData) are set to 0. Otherwise, SQLITE_OK
11516** is returned and the output variables are set to the size of and a
11517** pointer to the output buffer, respectively. In this case it is the
11518** responsibility of the caller to eventually free the buffer using a
11519** call to sqlite3_free().
11520*/
11521SQLITE_API int sqlite3changegroup_output(
11522 sqlite3_changegroup*,
11523 int *pnData, /* OUT: Size of output buffer in bytes */
11524 void **ppData /* OUT: Pointer to output buffer */
11525);
11526
11527/*
11528** CAPI3REF: Delete A Changegroup Object
11529** DESTRUCTOR: sqlite3_changegroup
11530*/
11531SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
11532
11533/*
11534** CAPI3REF: Apply A Changeset To A Database
11535**
11536** Apply a changeset or patchset to a database. These functions attempt to
11537** update the "main" database attached to handle db with the changes found in
11538** the changeset passed via the second and third arguments.
11539**
11540** The fourth argument (xFilter) passed to these functions is the "filter
11541** callback". If it is not NULL, then for each table affected by at least one
11542** change in the changeset, the filter callback is invoked with
11543** the table name as the second argument, and a copy of the context pointer
11544** passed as the sixth argument as the first. If the "filter callback"
11545** returns zero, then no attempt is made to apply any changes to the table.
11546** Otherwise, if the return value is non-zero or the xFilter argument to
11547** is NULL, all changes related to the table are attempted.
11548**
11549** For each table that is not excluded by the filter callback, this function
11550** tests that the target database contains a compatible table. A table is
11551** considered compatible if all of the following are true:
11552**
11553** <ul>
11554** <li> The table has the same name as the name recorded in the
11555** changeset, and
11556** <li> The table has at least as many columns as recorded in the
11557** changeset, and
11558** <li> The table has primary key columns in the same position as
11559** recorded in the changeset.
11560** </ul>
11561**
11562** If there is no compatible table, it is not an error, but none of the
11563** changes associated with the table are applied. A warning message is issued
11564** via the sqlite3_log() mechanism with the error code SQLITE_SCHEMA. At most
11565** one such warning is issued for each table in the changeset.
11566**
11567** For each change for which there is a compatible table, an attempt is made
11568** to modify the table contents according to the UPDATE, INSERT or DELETE
11569** change. If a change cannot be applied cleanly, the conflict handler
11570** function passed as the fifth argument to sqlite3changeset_apply() may be
11571** invoked. A description of exactly when the conflict handler is invoked for
11572** each type of change is below.
11573**
11574** Unlike the xFilter argument, xConflict may not be passed NULL. The results
11575** of passing anything other than a valid function pointer as the xConflict
11576** argument are undefined.
11577**
11578** Each time the conflict handler function is invoked, it must return one
11579** of [SQLITE_CHANGESET_OMIT], [SQLITE_CHANGESET_ABORT] or
11580** [SQLITE_CHANGESET_REPLACE]. SQLITE_CHANGESET_REPLACE may only be returned
11581** if the second argument passed to the conflict handler is either
11582** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler
11583** returns an illegal value, any changes already made are rolled back and
11584** the call to sqlite3changeset_apply() returns SQLITE_MISUSE. Different
11585** actions are taken by sqlite3changeset_apply() depending on the value
11586** returned by each invocation of the conflict-handler function. Refer to
11587** the documentation for the three
11588** [SQLITE_CHANGESET_OMIT|available return values] for details.
11589**
11590** <dl>
11591** <dt>DELETE Changes<dd>
11592** For each DELETE change, the function checks if the target database
11593** contains a row with the same primary key value (or values) as the
11594** original row values stored in the changeset. If it does, and the values
11595** stored in all non-primary key columns also match the values stored in
11596** the changeset the row is deleted from the target database.
11597**
11598** If a row with matching primary key values is found, but one or more of
11599** the non-primary key fields contains a value different from the original
11600** row value stored in the changeset, the conflict-handler function is
11601** invoked with [SQLITE_CHANGESET_DATA] as the second argument. If the
11602** database table has more columns than are recorded in the changeset,
11603** only the values of those non-primary key fields are compared against
11604** the current database contents - any trailing database table columns
11605** are ignored.
11606**
11607** If no row with matching primary key values is found in the database,
11608** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
11609** passed as the second argument.
11610**
11611** If the DELETE operation is attempted, but SQLite returns SQLITE_CONSTRAINT
11612** (which can only happen if a foreign key constraint is violated), the
11613** conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
11614** passed as the second argument. This includes the case where the DELETE
11615** operation is attempted because an earlier call to the conflict handler
11616** function returned [SQLITE_CHANGESET_REPLACE].
11617**
11618** <dt>INSERT Changes<dd>
11619** For each INSERT change, an attempt is made to insert the new row into
11620** the database. If the changeset row contains fewer fields than the
11621** database table, the trailing fields are populated with their default
11622** values.
11623**
11624** If the attempt to insert the row fails because the database already
11625** contains a row with the same primary key values, the conflict handler
11626** function is invoked with the second argument set to
11627** [SQLITE_CHANGESET_CONFLICT].
11628**
11629** If the attempt to insert the row fails because of some other constraint
11630** violation (e.g. NOT NULL or UNIQUE), the conflict handler function is
11631** invoked with the second argument set to [SQLITE_CHANGESET_CONSTRAINT].
11632** This includes the case where the INSERT operation is re-attempted because
11633** an earlier call to the conflict handler function returned
11634** [SQLITE_CHANGESET_REPLACE].
11635**
11636** <dt>UPDATE Changes<dd>
11637** For each UPDATE change, the function checks if the target database
11638** contains a row with the same primary key value (or values) as the
11639** original row values stored in the changeset. If it does, and the values
11640** stored in all modified non-primary key columns also match the values
11641** stored in the changeset the row is updated within the target database.
11642**
11643** If a row with matching primary key values is found, but one or more of
11644** the modified non-primary key fields contains a value different from an
11645** original row value stored in the changeset, the conflict-handler function
11646** is invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
11647** UPDATE changes only contain values for non-primary key fields that are
11648** to be modified, only those fields need to match the original values to
11649** avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
11650**
11651** If no row with matching primary key values is found in the database,
11652** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
11653** passed as the second argument.
11654**
11655** If the UPDATE operation is attempted, but SQLite returns
11656** SQLITE_CONSTRAINT, the conflict-handler function is invoked with
11657** [SQLITE_CHANGESET_CONSTRAINT] passed as the second argument.
11658** This includes the case where the UPDATE operation is attempted after
11659** an earlier call to the conflict handler function returned
11660** [SQLITE_CHANGESET_REPLACE].
11661** </dl>
11662**
11663** It is safe to execute SQL statements, including those that write to the
11664** table that the callback related to, from within the xConflict callback.
11665** This can be used to further customize the application's conflict
11666** resolution strategy.
11667**
11668** All changes made by these functions are enclosed in a savepoint transaction.
11669** If any other error (aside from a constraint failure when attempting to
11670** write to the target database) occurs, then the savepoint transaction is
11671** rolled back, restoring the target database to its original state, and an
11672** SQLite error code returned.
11673**
11674** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
11675** the input is a changeset (not a patchset), then sqlite3changeset_apply_v2()
11676** may set (*ppRebase) to point to a "rebase" that may be used with the
11677** sqlite3_rebaser APIs buffer before returning. In this case (*pnRebase)
11678** is set to the size of the buffer in bytes. It is the responsibility of the
11679** caller to eventually free any such buffer using sqlite3_free(). The buffer
11680** is only allocated and populated if one or more conflicts were encountered
11681** while applying the patchset. See comments surrounding the sqlite3_rebaser
11682** APIs for further details.
11683**
11684** The behavior of sqlite3changeset_apply_v2() and its streaming equivalent
11685** may be modified by passing a combination of
11686** [SQLITE_CHANGESETAPPLY_NOSAVEPOINT | supported flags] as the 9th parameter.
11687**
11688** Note that the sqlite3changeset_apply_v2() API is still <b>experimental</b>
11689** and therefore subject to change.
11690*/
11691SQLITE_API int sqlite3changeset_apply(
11692 sqlite3 *db, /* Apply change to "main" db of this handle */
11693 int nChangeset, /* Size of changeset in bytes */
11694 void *pChangeset, /* Changeset blob */
11695 int(*xFilter)(
11696 void *pCtx, /* Copy of sixth arg to _apply() */
11697 const char *zTab /* Table name */
11698 ),
11699 int(*xConflict)(
11700 void *pCtx, /* Copy of sixth arg to _apply() */
11701 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
11702 sqlite3_changeset_iter *p /* Handle describing change and conflict */
11703 ),
11704 void *pCtx /* First argument passed to xConflict */
11705);
11706SQLITE_API int sqlite3changeset_apply_v2(
11707 sqlite3 *db, /* Apply change to "main" db of this handle */
11708 int nChangeset, /* Size of changeset in bytes */
11709 void *pChangeset, /* Changeset blob */
11710 int(*xFilter)(
11711 void *pCtx, /* Copy of sixth arg to _apply() */
11712 const char *zTab /* Table name */
11713 ),
11714 int(*xConflict)(
11715 void *pCtx, /* Copy of sixth arg to _apply() */
11716 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
11717 sqlite3_changeset_iter *p /* Handle describing change and conflict */
11718 ),
11719 void *pCtx, /* First argument passed to xConflict */
11720 void **ppRebase, int *pnRebase, /* OUT: Rebase data */
11721 int flags /* SESSION_CHANGESETAPPLY_* flags */
11722);
11723
11724/*
11725** CAPI3REF: Flags for sqlite3changeset_apply_v2
11726**
11727** The following flags may passed via the 9th parameter to
11728** [sqlite3changeset_apply_v2] and [sqlite3changeset_apply_v2_strm]:
11729**
11730** <dl>
11731** <dt>SQLITE_CHANGESETAPPLY_NOSAVEPOINT <dd>
11732** Usually, the sessions module encloses all operations performed by
11733** a single call to apply_v2() or apply_v2_strm() in a [SAVEPOINT]. The
11734** SAVEPOINT is committed if the changeset or patchset is successfully
11735** applied, or rolled back if an error occurs. Specifying this flag
11736** causes the sessions module to omit this savepoint. In this case, if the
11737** caller has an open transaction or savepoint when apply_v2() is called,
11738** it may revert the partially applied changeset by rolling it back.
11739**
11740** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
11741** Invert the changeset before applying it. This is equivalent to inverting
11742** a changeset using sqlite3changeset_invert() before applying it. It is
11743** an error to specify this flag with a patchset.
11744*/
11745#define SQLITE_CHANGESETAPPLY_NOSAVEPOINT 0x0001
11746#define SQLITE_CHANGESETAPPLY_INVERT 0x0002
11747
11748/*
11749** CAPI3REF: Constants Passed To The Conflict Handler
11750**
11751** Values that may be passed as the second argument to a conflict-handler.
11752**
11753** <dl>
11754** <dt>SQLITE_CHANGESET_DATA<dd>
11755** The conflict handler is invoked with CHANGESET_DATA as the second argument
11756** when processing a DELETE or UPDATE change if a row with the required
11757** PRIMARY KEY fields is present in the database, but one or more other
11758** (non primary-key) fields modified by the update do not contain the
11759** expected "before" values.
11760**
11761** The conflicting row, in this case, is the database row with the matching
11762** primary key.
11763**
11764** <dt>SQLITE_CHANGESET_NOTFOUND<dd>
11765** The conflict handler is invoked with CHANGESET_NOTFOUND as the second
11766** argument when processing a DELETE or UPDATE change if a row with the
11767** required PRIMARY KEY fields is not present in the database.
11768**
11769** There is no conflicting row in this case. The results of invoking the
11770** sqlite3changeset_conflict() API are undefined.
11771**
11772** <dt>SQLITE_CHANGESET_CONFLICT<dd>
11773** CHANGESET_CONFLICT is passed as the second argument to the conflict
11774** handler while processing an INSERT change if the operation would result
11775** in duplicate primary key values.
11776**
11777** The conflicting row in this case is the database row with the matching
11778** primary key.
11779**
11780** <dt>SQLITE_CHANGESET_FOREIGN_KEY<dd>
11781** If foreign key handling is enabled, and applying a changeset leaves the
11782** database in a state containing foreign key violations, the conflict
11783** handler is invoked with CHANGESET_FOREIGN_KEY as the second argument
11784** exactly once before the changeset is committed. If the conflict handler
11785** returns CHANGESET_OMIT, the changes, including those that caused the
11786** foreign key constraint violation, are committed. Or, if it returns
11787** CHANGESET_ABORT, the changeset is rolled back.
11788**
11789** No current or conflicting row information is provided. The only function
11790** it is possible to call on the supplied sqlite3_changeset_iter handle
11791** is sqlite3changeset_fk_conflicts().
11792**
11793** <dt>SQLITE_CHANGESET_CONSTRAINT<dd>
11794** If any other constraint violation occurs while applying a change (i.e.
11795** a UNIQUE, CHECK or NOT NULL constraint), the conflict handler is
11796** invoked with CHANGESET_CONSTRAINT as the second argument.
11797**
11798** There is no conflicting row in this case. The results of invoking the
11799** sqlite3changeset_conflict() API are undefined.
11800**
11801** </dl>
11802*/
11803#define SQLITE_CHANGESET_DATA 1
11804#define SQLITE_CHANGESET_NOTFOUND 2
11805#define SQLITE_CHANGESET_CONFLICT 3
11806#define SQLITE_CHANGESET_CONSTRAINT 4
11807#define SQLITE_CHANGESET_FOREIGN_KEY 5
11808
11809/*
11810** CAPI3REF: Constants Returned By The Conflict Handler
11811**
11812** A conflict handler callback must return one of the following three values.
11813**
11814** <dl>
11815** <dt>SQLITE_CHANGESET_OMIT<dd>
11816** If a conflict handler returns this value no special action is taken. The
11817** change that caused the conflict is not applied. The session module
11818** continues to the next change in the changeset.
11819**
11820** <dt>SQLITE_CHANGESET_REPLACE<dd>
11821** This value may only be returned if the second argument to the conflict
11822** handler was SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If this
11823** is not the case, any changes applied so far are rolled back and the
11824** call to sqlite3changeset_apply() returns SQLITE_MISUSE.
11825**
11826** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_DATA conflict
11827** handler, then the conflicting row is either updated or deleted, depending
11828** on the type of change.
11829**
11830** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_CONFLICT conflict
11831** handler, then the conflicting row is removed from the database and a
11832** second attempt to apply the change is made. If this second attempt fails,
11833** the original row is restored to the database before continuing.
11834**
11835** <dt>SQLITE_CHANGESET_ABORT<dd>
11836** If this value is returned, any changes applied so far are rolled back
11837** and the call to sqlite3changeset_apply() returns SQLITE_ABORT.
11838** </dl>
11839*/
11840#define SQLITE_CHANGESET_OMIT 0
11841#define SQLITE_CHANGESET_REPLACE 1
11842#define SQLITE_CHANGESET_ABORT 2
11843
11844/*
11845** CAPI3REF: Rebasing changesets
11846** EXPERIMENTAL
11847**
11848** Suppose there is a site hosting a database in state S0. And that
11849** modifications are made that move that database to state S1 and a
11850** changeset recorded (the "local" changeset). Then, a changeset based
11851** on S0 is received from another site (the "remote" changeset) and
11852** applied to the database. The database is then in state
11853** (S1+"remote"), where the exact state depends on any conflict
11854** resolution decisions (OMIT or REPLACE) made while applying "remote".
11855** Rebasing a changeset is to update it to take those conflict
11856** resolution decisions into account, so that the same conflicts
11857** do not have to be resolved elsewhere in the network.
11858**
11859** For example, if both the local and remote changesets contain an
11860** INSERT of the same key on "CREATE TABLE t1(a PRIMARY KEY, b)":
11861**
11862** local: INSERT INTO t1 VALUES(1, 'v1');
11863** remote: INSERT INTO t1 VALUES(1, 'v2');
11864**
11865** and the conflict resolution is REPLACE, then the INSERT change is
11866** removed from the local changeset (it was overridden). Or, if the
11867** conflict resolution was "OMIT", then the local changeset is modified
11868** to instead contain:
11869**
11870** UPDATE t1 SET b = 'v2' WHERE a=1;
11871**
11872** Changes within the local changeset are rebased as follows:
11873**
11874** <dl>
11875** <dt>Local INSERT<dd>
11876** This may only conflict with a remote INSERT. If the conflict
11877** resolution was OMIT, then add an UPDATE change to the rebased
11878** changeset. Or, if the conflict resolution was REPLACE, add
11879** nothing to the rebased changeset.
11880**
11881** <dt>Local DELETE<dd>
11882** This may conflict with a remote UPDATE or DELETE. In both cases the
11883** only possible resolution is OMIT. If the remote operation was a
11884** DELETE, then add no change to the rebased changeset. If the remote
11885** operation was an UPDATE, then the old.* fields of change are updated
11886** to reflect the new.* values in the UPDATE.
11887**
11888** <dt>Local UPDATE<dd>
11889** This may conflict with a remote UPDATE or DELETE. If it conflicts
11890** with a DELETE, and the conflict resolution was OMIT, then the update
11891** is changed into an INSERT. Any undefined values in the new.* record
11892** from the update change are filled in using the old.* values from
11893** the conflicting DELETE. Or, if the conflict resolution was REPLACE,
11894** the UPDATE change is simply omitted from the rebased changeset.
11895**
11896** If conflict is with a remote UPDATE and the resolution is OMIT, then
11897** the old.* values are rebased using the new.* values in the remote
11898** change. Or, if the resolution is REPLACE, then the change is copied
11899** into the rebased changeset with updates to columns also updated by
11900** the conflicting remote UPDATE removed. If this means no columns would
11901** be updated, the change is omitted.
11902** </dl>
11903**
11904** A local change may be rebased against multiple remote changes
11905** simultaneously. If a single key is modified by multiple remote
11906** changesets, they are combined as follows before the local changeset
11907** is rebased:
11908**
11909** <ul>
11910** <li> If there has been one or more REPLACE resolutions on a
11911** key, it is rebased according to a REPLACE.
11912**
11913** <li> If there have been no REPLACE resolutions on a key, then
11914** the local changeset is rebased according to the most recent
11915** of the OMIT resolutions.
11916** </ul>
11917**
11918** Note that conflict resolutions from multiple remote changesets are
11919** combined on a per-field basis, not per-row. This means that in the
11920** case of multiple remote UPDATE operations, some fields of a single
11921** local change may be rebased for REPLACE while others are rebased for
11922** OMIT.
11923**
11924** In order to rebase a local changeset, the remote changeset must first
11925** be applied to the local database using sqlite3changeset_apply_v2() and
11926** the buffer of rebase information captured. Then:
11927**
11928** <ol>
11929** <li> An sqlite3_rebaser object is created by calling
11930** sqlite3rebaser_create().
11931** <li> The new object is configured with the rebase buffer obtained from
11932** sqlite3changeset_apply_v2() by calling sqlite3rebaser_configure().
11933** If the local changeset is to be rebased against multiple remote
11934** changesets, then sqlite3rebaser_configure() should be called
11935** multiple times, in the same order that the multiple
11936** sqlite3changeset_apply_v2() calls were made.
11937** <li> Each local changeset is rebased by calling sqlite3rebaser_rebase().
11938** <li> The sqlite3_rebaser object is deleted by calling
11939** sqlite3rebaser_delete().
11940** </ol>
11941*/
11942typedef struct sqlite3_rebaser sqlite3_rebaser;
11943
11944/*
11945** CAPI3REF: Create a changeset rebaser object.
11946** EXPERIMENTAL
11947**
11948** Allocate a new changeset rebaser object. If successful, set (*ppNew) to
11949** point to the new object and return SQLITE_OK. Otherwise, if an error
11950** occurs, return an SQLite error code (e.g. SQLITE_NOMEM) and set (*ppNew)
11951** to NULL.
11952*/
11953SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew);
11954
11955/*
11956** CAPI3REF: Configure a changeset rebaser object.
11957** EXPERIMENTAL
11958**
11959** Configure the changeset rebaser object to rebase changesets according
11960** to the conflict resolutions described by buffer pRebase (size nRebase
11961** bytes), which must have been obtained from a previous call to
11962** sqlite3changeset_apply_v2().
11963*/
11964SQLITE_API int sqlite3rebaser_configure(
11965 sqlite3_rebaser*,
11966 int nRebase, const void *pRebase
11967);
11968
11969/*
11970** CAPI3REF: Rebase a changeset
11971** EXPERIMENTAL
11972**
11973** Argument pIn must point to a buffer containing a changeset nIn bytes
11974** in size. This function allocates and populates a buffer with a copy
11975** of the changeset rebased according to the configuration of the
11976** rebaser object passed as the first argument. If successful, (*ppOut)
11977** is set to point to the new buffer containing the rebased changeset and
11978** (*pnOut) to its size in bytes and SQLITE_OK returned. It is the
11979** responsibility of the caller to eventually free the new buffer using
11980** sqlite3_free(). Otherwise, if an error occurs, (*ppOut) and (*pnOut)
11981** are set to zero and an SQLite error code returned.
11982*/
11983SQLITE_API int sqlite3rebaser_rebase(
11984 sqlite3_rebaser*,
11985 int nIn, const void *pIn,
11986 int *pnOut, void **ppOut
11987);
11988
11989/*
11990** CAPI3REF: Delete a changeset rebaser object.
11991** EXPERIMENTAL
11992**
11993** Delete the changeset rebaser object and all associated resources. There
11994** should be one call to this function for each successful invocation
11995** of sqlite3rebaser_create().
11996*/
11997SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p);
11998
11999/*
12000** CAPI3REF: Streaming Versions of API functions.
12001**
12002** The six streaming API xxx_strm() functions serve similar purposes to the
12003** corresponding non-streaming API functions:
12004**
12005** <table border=1 style="margin-left:8ex;margin-right:8ex">
12006** <tr><th>Streaming function<th>Non-streaming equivalent</th>
12007** <tr><td>sqlite3changeset_apply_strm<td>[sqlite3changeset_apply]
12008** <tr><td>sqlite3changeset_apply_strm_v2<td>[sqlite3changeset_apply_v2]
12009** <tr><td>sqlite3changeset_concat_strm<td>[sqlite3changeset_concat]
12010** <tr><td>sqlite3changeset_invert_strm<td>[sqlite3changeset_invert]
12011** <tr><td>sqlite3changeset_start_strm<td>[sqlite3changeset_start]
12012** <tr><td>sqlite3session_changeset_strm<td>[sqlite3session_changeset]
12013** <tr><td>sqlite3session_patchset_strm<td>[sqlite3session_patchset]
12014** </table>
12015**
12016** Non-streaming functions that accept changesets (or patchsets) as input
12017** require that the entire changeset be stored in a single buffer in memory.
12018** Similarly, those that return a changeset or patchset do so by returning
12019** a pointer to a single large buffer allocated using sqlite3_malloc().
12020** Normally this is convenient. However, if an application running in a
12021** low-memory environment is required to handle very large changesets, the
12022** large contiguous memory allocations required can become onerous.
12023**
12024** In order to avoid this problem, instead of a single large buffer, input
12025** is passed to a streaming API functions by way of a callback function that
12026** the sessions module invokes to incrementally request input data as it is
12027** required. In all cases, a pair of API function parameters such as
12028**
12029** <pre>
12030** &nbsp; int nChangeset,
12031** &nbsp; void *pChangeset,
12032** </pre>
12033**
12034** Is replaced by:
12035**
12036** <pre>
12037** &nbsp; int (*xInput)(void *pIn, void *pData, int *pnData),
12038** &nbsp; void *pIn,
12039** </pre>
12040**
12041** Each time the xInput callback is invoked by the sessions module, the first
12042** argument passed is a copy of the supplied pIn context pointer. The second
12043** argument, pData, points to a buffer (*pnData) bytes in size. Assuming no
12044** error occurs the xInput method should copy up to (*pnData) bytes of data
12045** into the buffer and set (*pnData) to the actual number of bytes copied
12046** before returning SQLITE_OK. If the input is completely exhausted, (*pnData)
12047** should be set to zero to indicate this. Or, if an error occurs, an SQLite
12048** error code should be returned. In all cases, if an xInput callback returns
12049** an error, all processing is abandoned and the streaming API function
12050** returns a copy of the error code to the caller.
12051**
12052** In the case of sqlite3changeset_start_strm(), the xInput callback may be
12053** invoked by the sessions module at any point during the lifetime of the
12054** iterator. If such an xInput callback returns an error, the iterator enters
12055** an error state, whereby all subsequent calls to iterator functions
12056** immediately fail with the same error code as returned by xInput.
12057**
12058** Similarly, streaming API functions that return changesets (or patchsets)
12059** return them in chunks by way of a callback function instead of via a
12060** pointer to a single large buffer. In this case, a pair of parameters such
12061** as:
12062**
12063** <pre>
12064** &nbsp; int *pnChangeset,
12065** &nbsp; void **ppChangeset,
12066** </pre>
12067**
12068** Is replaced by:
12069**
12070** <pre>
12071** &nbsp; int (*xOutput)(void *pOut, const void *pData, int nData),
12072** &nbsp; void *pOut
12073** </pre>
12074**
12075** The xOutput callback is invoked zero or more times to return data to
12076** the application. The first parameter passed to each call is a copy of the
12077** pOut pointer supplied by the application. The second parameter, pData,
12078** points to a buffer nData bytes in size containing the chunk of output
12079** data being returned. If the xOutput callback successfully processes the
12080** supplied data, it should return SQLITE_OK to indicate success. Otherwise,
12081** it should return some other SQLite error code. In this case processing
12082** is immediately abandoned and the streaming API function returns a copy
12083** of the xOutput error code to the application.
12084**
12085** The sessions module never invokes an xOutput callback with the third
12086** parameter set to a value less than or equal to zero. Other than this,
12087** no guarantees are made as to the size of the chunks of data returned.
12088*/
12089SQLITE_API int sqlite3changeset_apply_strm(
12090 sqlite3 *db, /* Apply change to "main" db of this handle */
12091 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
12092 void *pIn, /* First arg for xInput */
12093 int(*xFilter)(
12094 void *pCtx, /* Copy of sixth arg to _apply() */
12095 const char *zTab /* Table name */
12096 ),
12097 int(*xConflict)(
12098 void *pCtx, /* Copy of sixth arg to _apply() */
12099 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
12100 sqlite3_changeset_iter *p /* Handle describing change and conflict */
12101 ),
12102 void *pCtx /* First argument passed to xConflict */
12103);
12104SQLITE_API int sqlite3changeset_apply_v2_strm(
12105 sqlite3 *db, /* Apply change to "main" db of this handle */
12106 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
12107 void *pIn, /* First arg for xInput */
12108 int(*xFilter)(
12109 void *pCtx, /* Copy of sixth arg to _apply() */
12110 const char *zTab /* Table name */
12111 ),
12112 int(*xConflict)(
12113 void *pCtx, /* Copy of sixth arg to _apply() */
12114 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
12115 sqlite3_changeset_iter *p /* Handle describing change and conflict */
12116 ),
12117 void *pCtx, /* First argument passed to xConflict */
12118 void **ppRebase, int *pnRebase,
12119 int flags
12120);
12121SQLITE_API int sqlite3changeset_concat_strm(
12122 int (*xInputA)(void *pIn, void *pData, int *pnData),
12123 void *pInA,
12124 int (*xInputB)(void *pIn, void *pData, int *pnData),
12125 void *pInB,
12126 int (*xOutput)(void *pOut, const void *pData, int nData),
12127 void *pOut
12128);
12129SQLITE_API int sqlite3changeset_invert_strm(
12130 int (*xInput)(void *pIn, void *pData, int *pnData),
12131 void *pIn,
12132 int (*xOutput)(void *pOut, const void *pData, int nData),
12133 void *pOut
12134);
12135SQLITE_API int sqlite3changeset_start_strm(
12136 sqlite3_changeset_iter **pp,
12137 int (*xInput)(void *pIn, void *pData, int *pnData),
12138 void *pIn
12139);
12140SQLITE_API int sqlite3changeset_start_v2_strm(
12141 sqlite3_changeset_iter **pp,
12142 int (*xInput)(void *pIn, void *pData, int *pnData),
12143 void *pIn,
12144 int flags
12145);
12146SQLITE_API int sqlite3session_changeset_strm(
12147 sqlite3_session *pSession,
12148 int (*xOutput)(void *pOut, const void *pData, int nData),
12149 void *pOut
12150);
12151SQLITE_API int sqlite3session_patchset_strm(
12152 sqlite3_session *pSession,
12153 int (*xOutput)(void *pOut, const void *pData, int nData),
12154 void *pOut
12155);
12156SQLITE_API int sqlite3changegroup_add_strm(sqlite3_changegroup*,
12157 int (*xInput)(void *pIn, void *pData, int *pnData),
12158 void *pIn
12159);
12160SQLITE_API int sqlite3changegroup_output_strm(sqlite3_changegroup*,
12161 int (*xOutput)(void *pOut, const void *pData, int nData),
12162 void *pOut
12163);
12164SQLITE_API int sqlite3rebaser_rebase_strm(
12165 sqlite3_rebaser *pRebaser,
12166 int (*xInput)(void *pIn, void *pData, int *pnData),
12167 void *pIn,
12168 int (*xOutput)(void *pOut, const void *pData, int nData),
12169 void *pOut
12170);
12171
12172/*
12173** CAPI3REF: Configure global parameters
12174**
12175** The sqlite3session_config() interface is used to make global configuration
12176** changes to the sessions module in order to tune it to the specific needs
12177** of the application.
12178**
12179** The sqlite3session_config() interface is not threadsafe. If it is invoked
12180** while any other thread is inside any other sessions method then the
12181** results are undefined. Furthermore, if it is invoked after any sessions
12182** related objects have been created, the results are also undefined.
12183**
12184** The first argument to the sqlite3session_config() function must be one
12185** of the SQLITE_SESSION_CONFIG_XXX constants defined below. The
12186** interpretation of the (void*) value passed as the second parameter and
12187** the effect of calling this function depends on the value of the first
12188** parameter.
12189**
12190** <dl>
12191** <dt>SQLITE_SESSION_CONFIG_STRMSIZE<dd>
12192** By default, the sessions module streaming interfaces attempt to input
12193** and output data in approximately 1 KiB chunks. This operand may be used
12194** to set and query the value of this configuration setting. The pointer
12195** passed as the second argument must point to a value of type (int).
12196** If this value is greater than 0, it is used as the new streaming data
12197** chunk size for both input and output. Before returning, the (int) value
12198** pointed to by pArg is set to the final value of the streaming interface
12199** chunk size.
12200** </dl>
12201**
12202** This function returns SQLITE_OK if successful, or an SQLite error code
12203** otherwise.
12204*/
12205SQLITE_API int sqlite3session_config(int op, void *pArg);
12206
12207/*
12208** CAPI3REF: Values for sqlite3session_config().
12209*/
12210#define SQLITE_SESSION_CONFIG_STRMSIZE 1
12211
12212/*
12213** Make sure we can call this stuff from C++.
12214*/
12215#if 0
12216}
12217#endif
12218
12219#endif /* !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION) */
12220
12221/******** End of sqlite3session.h *********/
12222/******** Begin file fts5.h *********/
12223/*
12224** 2014 May 31
12225**
12226** The author disclaims copyright to this source code. In place of
12227** a legal notice, here is a blessing:
12228**
12229** May you do good and not evil.
12230** May you find forgiveness for yourself and forgive others.
12231** May you share freely, never taking more than you give.
12232**
12233******************************************************************************
12234**
12235** Interfaces to extend FTS5. Using the interfaces defined in this file,
12236** FTS5 may be extended with:
12237**
12238** * custom tokenizers, and
12239** * custom auxiliary functions.
12240*/
12241
12242
12243#ifndef _FTS5_H
12244#define _FTS5_H
12245
12246
12247#if 0
12248extern "C" {
12249#endif
12250
12251/*************************************************************************
12252** CUSTOM AUXILIARY FUNCTIONS
12253**
12254** Virtual table implementations may overload SQL functions by implementing
12255** the sqlite3_module.xFindFunction() method.
12256*/
12257
12258typedef struct Fts5ExtensionApi Fts5ExtensionApi;
12259typedef struct Fts5Context Fts5Context;
12260typedef struct Fts5PhraseIter Fts5PhraseIter;
12261
12262typedef void (*fts5_extension_function)(
12263 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
12264 Fts5Context *pFts, /* First arg to pass to pApi functions */
12265 sqlite3_context *pCtx, /* Context for returning result/error */
12266 int nVal, /* Number of values in apVal[] array */
12267 sqlite3_value **apVal /* Array of trailing arguments */
12268);
12269
12270struct Fts5PhraseIter {
12271 const unsigned char *a;
12272 const unsigned char *b;
12273};
12274
12275/*
12276** EXTENSION API FUNCTIONS
12277**
12278** xUserData(pFts):
12279** Return a copy of the context pointer the extension function was
12280** registered with.
12281**
12282** xColumnTotalSize(pFts, iCol, pnToken):
12283** If parameter iCol is less than zero, set output variable *pnToken
12284** to the total number of tokens in the FTS5 table. Or, if iCol is
12285** non-negative but less than the number of columns in the table, return
12286** the total number of tokens in column iCol, considering all rows in
12287** the FTS5 table.
12288**
12289** If parameter iCol is greater than or equal to the number of columns
12290** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
12291** an OOM condition or IO error), an appropriate SQLite error code is
12292** returned.
12293**
12294** xColumnCount(pFts):
12295** Return the number of columns in the table.
12296**
12297** xColumnSize(pFts, iCol, pnToken):
12298** If parameter iCol is less than zero, set output variable *pnToken
12299** to the total number of tokens in the current row. Or, if iCol is
12300** non-negative but less than the number of columns in the table, set
12301** *pnToken to the number of tokens in column iCol of the current row.
12302**
12303** If parameter iCol is greater than or equal to the number of columns
12304** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
12305** an OOM condition or IO error), an appropriate SQLite error code is
12306** returned.
12307**
12308** This function may be quite inefficient if used with an FTS5 table
12309** created with the "columnsize=0" option.
12310**
12311** xColumnText:
12312** This function attempts to retrieve the text of column iCol of the
12313** current document. If successful, (*pz) is set to point to a buffer
12314** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
12315** (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
12316** if an error occurs, an SQLite error code is returned and the final values
12317** of (*pz) and (*pn) are undefined.
12318**
12319** xPhraseCount:
12320** Returns the number of phrases in the current query expression.
12321**
12322** xPhraseSize:
12323** Returns the number of tokens in phrase iPhrase of the query. Phrases
12324** are numbered starting from zero.
12325**
12326** xInstCount:
12327** Set *pnInst to the total number of occurrences of all phrases within
12328** the query within the current row. Return SQLITE_OK if successful, or
12329** an error code (i.e. SQLITE_NOMEM) if an error occurs.
12330**
12331** This API can be quite slow if used with an FTS5 table created with the
12332** "detail=none" or "detail=column" option. If the FTS5 table is created
12333** with either "detail=none" or "detail=column" and "content=" option
12334** (i.e. if it is a contentless table), then this API always returns 0.
12335**
12336** xInst:
12337** Query for the details of phrase match iIdx within the current row.
12338** Phrase matches are numbered starting from zero, so the iIdx argument
12339** should be greater than or equal to zero and smaller than the value
12340** output by xInstCount().
12341**
12342** Usually, output parameter *piPhrase is set to the phrase number, *piCol
12343** to the column in which it occurs and *piOff the token offset of the
12344** first token of the phrase. Returns SQLITE_OK if successful, or an error
12345** code (i.e. SQLITE_NOMEM) if an error occurs.
12346**
12347** This API can be quite slow if used with an FTS5 table created with the
12348** "detail=none" or "detail=column" option.
12349**
12350** xRowid:
12351** Returns the rowid of the current row.
12352**
12353** xTokenize:
12354** Tokenize text using the tokenizer belonging to the FTS5 table.
12355**
12356** xQueryPhrase(pFts5, iPhrase, pUserData, xCallback):
12357** This API function is used to query the FTS table for phrase iPhrase
12358** of the current query. Specifically, a query equivalent to:
12359**
12360** ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid
12361**
12362** with $p set to a phrase equivalent to the phrase iPhrase of the
12363** current query is executed. Any column filter that applies to
12364** phrase iPhrase of the current query is included in $p. For each
12365** row visited, the callback function passed as the fourth argument
12366** is invoked. The context and API objects passed to the callback
12367** function may be used to access the properties of each matched row.
12368** Invoking Api.xUserData() returns a copy of the pointer passed as
12369** the third argument to pUserData.
12370**
12371** If the callback function returns any value other than SQLITE_OK, the
12372** query is abandoned and the xQueryPhrase function returns immediately.
12373** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
12374** Otherwise, the error code is propagated upwards.
12375**
12376** If the query runs to completion without incident, SQLITE_OK is returned.
12377** Or, if some error occurs before the query completes or is aborted by
12378** the callback, an SQLite error code is returned.
12379**
12380**
12381** xSetAuxdata(pFts5, pAux, xDelete)
12382**
12383** Save the pointer passed as the second argument as the extension function's
12384** "auxiliary data". The pointer may then be retrieved by the current or any
12385** future invocation of the same fts5 extension function made as part of
12386** the same MATCH query using the xGetAuxdata() API.
12387**
12388** Each extension function is allocated a single auxiliary data slot for
12389** each FTS query (MATCH expression). If the extension function is invoked
12390** more than once for a single FTS query, then all invocations share a
12391** single auxiliary data context.
12392**
12393** If there is already an auxiliary data pointer when this function is
12394** invoked, then it is replaced by the new pointer. If an xDelete callback
12395** was specified along with the original pointer, it is invoked at this
12396** point.
12397**
12398** The xDelete callback, if one is specified, is also invoked on the
12399** auxiliary data pointer after the FTS5 query has finished.
12400**
12401** If an error (e.g. an OOM condition) occurs within this function,
12402** the auxiliary data is set to NULL and an error code returned. If the
12403** xDelete parameter was not NULL, it is invoked on the auxiliary data
12404** pointer before returning.
12405**
12406**
12407** xGetAuxdata(pFts5, bClear)
12408**
12409** Returns the current auxiliary data pointer for the fts5 extension
12410** function. See the xSetAuxdata() method for details.
12411**
12412** If the bClear argument is non-zero, then the auxiliary data is cleared
12413** (set to NULL) before this function returns. In this case the xDelete,
12414** if any, is not invoked.
12415**
12416**
12417** xRowCount(pFts5, pnRow)
12418**
12419** This function is used to retrieve the total number of rows in the table.
12420** In other words, the same value that would be returned by:
12421**
12422** SELECT count(*) FROM ftstable;
12423**
12424** xPhraseFirst()
12425** This function is used, along with type Fts5PhraseIter and the xPhraseNext
12426** method, to iterate through all instances of a single query phrase within
12427** the current row. This is the same information as is accessible via the
12428** xInstCount/xInst APIs. While the xInstCount/xInst APIs are more convenient
12429** to use, this API may be faster under some circumstances. To iterate
12430** through instances of phrase iPhrase, use the following code:
12431**
12432** Fts5PhraseIter iter;
12433** int iCol, iOff;
12434** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
12435** iCol>=0;
12436** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
12437** ){
12438** // An instance of phrase iPhrase at offset iOff of column iCol
12439** }
12440**
12441** The Fts5PhraseIter structure is defined above. Applications should not
12442** modify this structure directly - it should only be used as shown above
12443** with the xPhraseFirst() and xPhraseNext() API methods (and by
12444** xPhraseFirstColumn() and xPhraseNextColumn() as illustrated below).
12445**
12446** This API can be quite slow if used with an FTS5 table created with the
12447** "detail=none" or "detail=column" option. If the FTS5 table is created
12448** with either "detail=none" or "detail=column" and "content=" option
12449** (i.e. if it is a contentless table), then this API always iterates
12450** through an empty set (all calls to xPhraseFirst() set iCol to -1).
12451**
12452** xPhraseNext()
12453** See xPhraseFirst above.
12454**
12455** xPhraseFirstColumn()
12456** This function and xPhraseNextColumn() are similar to the xPhraseFirst()
12457** and xPhraseNext() APIs described above. The difference is that instead
12458** of iterating through all instances of a phrase in the current row, these
12459** APIs are used to iterate through the set of columns in the current row
12460** that contain one or more instances of a specified phrase. For example:
12461**
12462** Fts5PhraseIter iter;
12463** int iCol;
12464** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
12465** iCol>=0;
12466** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
12467** ){
12468** // Column iCol contains at least one instance of phrase iPhrase
12469** }
12470**
12471** This API can be quite slow if used with an FTS5 table created with the
12472** "detail=none" option. If the FTS5 table is created with either
12473** "detail=none" "content=" option (i.e. if it is a contentless table),
12474** then this API always iterates through an empty set (all calls to
12475** xPhraseFirstColumn() set iCol to -1).
12476**
12477** The information accessed using this API and its companion
12478** xPhraseFirstColumn() may also be obtained using xPhraseFirst/xPhraseNext
12479** (or xInst/xInstCount). The chief advantage of this API is that it is
12480** significantly more efficient than those alternatives when used with
12481** "detail=column" tables.
12482**
12483** xPhraseNextColumn()
12484** See xPhraseFirstColumn above.
12485*/
12486struct Fts5ExtensionApi {
12487 int iVersion; /* Currently always set to 3 */
12488
12489 void *(*xUserData)(Fts5Context*);
12490
12491 int (*xColumnCount)(Fts5Context*);
12492 int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow);
12493 int (*xColumnTotalSize)(Fts5Context*, int iCol, sqlite3_int64 *pnToken);
12494
12495 int (*xTokenize)(Fts5Context*,
12496 const char *pText, int nText, /* Text to tokenize */
12497 void *pCtx, /* Context passed to xToken() */
12498 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
12499 );
12500
12501 int (*xPhraseCount)(Fts5Context*);
12502 int (*xPhraseSize)(Fts5Context*, int iPhrase);
12503
12504 int (*xInstCount)(Fts5Context*, int *pnInst);
12505 int (*xInst)(Fts5Context*, int iIdx, int *piPhrase, int *piCol, int *piOff);
12506
12507 sqlite3_int64 (*xRowid)(Fts5Context*);
12508 int (*xColumnText)(Fts5Context*, int iCol, const char **pz, int *pn);
12509 int (*xColumnSize)(Fts5Context*, int iCol, int *pnToken);
12510
12511 int (*xQueryPhrase)(Fts5Context*, int iPhrase, void *pUserData,
12512 int(*)(const Fts5ExtensionApi*,Fts5Context*,void*)
12513 );
12514 int (*xSetAuxdata)(Fts5Context*, void *pAux, void(*xDelete)(void*));
12515 void *(*xGetAuxdata)(Fts5Context*, int bClear);
12516
12517 int (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*);
12518 void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff);
12519
12520 int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*);
12521 void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol);
12522};
12523
12524/*
12525** CUSTOM AUXILIARY FUNCTIONS
12526*************************************************************************/
12527
12528/*************************************************************************
12529** CUSTOM TOKENIZERS
12530**
12531** Applications may also register custom tokenizer types. A tokenizer
12532** is registered by providing fts5 with a populated instance of the
12533** following structure. All structure methods must be defined, setting
12534** any member of the fts5_tokenizer struct to NULL leads to undefined
12535** behaviour. The structure methods are expected to function as follows:
12536**
12537** xCreate:
12538** This function is used to allocate and initialize a tokenizer instance.
12539** A tokenizer instance is required to actually tokenize text.
12540**
12541** The first argument passed to this function is a copy of the (void*)
12542** pointer provided by the application when the fts5_tokenizer object
12543** was registered with FTS5 (the third argument to xCreateTokenizer()).
12544** The second and third arguments are an array of nul-terminated strings
12545** containing the tokenizer arguments, if any, specified following the
12546** tokenizer name as part of the CREATE VIRTUAL TABLE statement used
12547** to create the FTS5 table.
12548**
12549** The final argument is an output variable. If successful, (*ppOut)
12550** should be set to point to the new tokenizer handle and SQLITE_OK
12551** returned. If an error occurs, some value other than SQLITE_OK should
12552** be returned. In this case, fts5 assumes that the final value of *ppOut
12553** is undefined.
12554**
12555** xDelete:
12556** This function is invoked to delete a tokenizer handle previously
12557** allocated using xCreate(). Fts5 guarantees that this function will
12558** be invoked exactly once for each successful call to xCreate().
12559**
12560** xTokenize:
12561** This function is expected to tokenize the nText byte string indicated
12562** by argument pText. pText may or may not be nul-terminated. The first
12563** argument passed to this function is a pointer to an Fts5Tokenizer object
12564** returned by an earlier call to xCreate().
12565**
12566** The second argument indicates the reason that FTS5 is requesting
12567** tokenization of the supplied text. This is always one of the following
12568** four values:
12569**
12570** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
12571** or removed from the FTS table. The tokenizer is being invoked to
12572** determine the set of tokens to add to (or delete from) the
12573** FTS index.
12574**
12575** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
12576** against the FTS index. The tokenizer is being called to tokenize
12577** a bareword or quoted string specified as part of the query.
12578**
12579** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
12580** FTS5_TOKENIZE_QUERY, except that the bareword or quoted string is
12581** followed by a "*" character, indicating that the last token
12582** returned by the tokenizer will be treated as a token prefix.
12583**
12584** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
12585** satisfy an fts5_api.xTokenize() request made by an auxiliary
12586** function. Or an fts5_api.xColumnSize() request made by the same
12587** on a columnsize=0 database.
12588** </ul>
12589**
12590** For each token in the input string, the supplied callback xToken() must
12591** be invoked. The first argument to it should be a copy of the pointer
12592** passed as the second argument to xTokenize(). The third and fourth
12593** arguments are a pointer to a buffer containing the token text, and the
12594** size of the token in bytes. The 4th and 5th arguments are the byte offsets
12595** of the first byte of and first byte immediately following the text from
12596** which the token is derived within the input.
12597**
12598** The second argument passed to the xToken() callback ("tflags") should
12599** normally be set to 0. The exception is if the tokenizer supports
12600** synonyms. In this case see the discussion below for details.
12601**
12602** FTS5 assumes the xToken() callback is invoked for each token in the
12603** order that they occur within the input text.
12604**
12605** If an xToken() callback returns any value other than SQLITE_OK, then
12606** the tokenization should be abandoned and the xTokenize() method should
12607** immediately return a copy of the xToken() return value. Or, if the
12608** input buffer is exhausted, xTokenize() should return SQLITE_OK. Finally,
12609** if an error occurs with the xTokenize() implementation itself, it
12610** may abandon the tokenization and return any error code other than
12611** SQLITE_OK or SQLITE_DONE.
12612**
12613** SYNONYM SUPPORT
12614**
12615** Custom tokenizers may also support synonyms. Consider a case in which a
12616** user wishes to query for a phrase such as "first place". Using the
12617** built-in tokenizers, the FTS5 query 'first + place' will match instances
12618** of "first place" within the document set, but not alternative forms
12619** such as "1st place". In some applications, it would be better to match
12620** all instances of "first place" or "1st place" regardless of which form
12621** the user specified in the MATCH query text.
12622**
12623** There are several ways to approach this in FTS5:
12624**
12625** <ol><li> By mapping all synonyms to a single token. In this case, using
12626** the above example, this means that the tokenizer returns the
12627** same token for inputs "first" and "1st". Say that token is in
12628** fact "first", so that when the user inserts the document "I won
12629** 1st place" entries are added to the index for tokens "i", "won",
12630** "first" and "place". If the user then queries for '1st + place',
12631** the tokenizer substitutes "first" for "1st" and the query works
12632** as expected.
12633**
12634** <li> By querying the index for all synonyms of each query term
12635** separately. In this case, when tokenizing query text, the
12636** tokenizer may provide multiple synonyms for a single term
12637** within the document. FTS5 then queries the index for each
12638** synonym individually. For example, faced with the query:
12639**
12640** <codeblock>
12641** ... MATCH 'first place'</codeblock>
12642**
12643** the tokenizer offers both "1st" and "first" as synonyms for the
12644** first token in the MATCH query and FTS5 effectively runs a query
12645** similar to:
12646**
12647** <codeblock>
12648** ... MATCH '(first OR 1st) place'</codeblock>
12649**
12650** except that, for the purposes of auxiliary functions, the query
12651** still appears to contain just two phrases - "(first OR 1st)"
12652** being treated as a single phrase.
12653**
12654** <li> By adding multiple synonyms for a single term to the FTS index.
12655** Using this method, when tokenizing document text, the tokenizer
12656** provides multiple synonyms for each token. So that when a
12657** document such as "I won first place" is tokenized, entries are
12658** added to the FTS index for "i", "won", "first", "1st" and
12659** "place".
12660**
12661** This way, even if the tokenizer does not provide synonyms
12662** when tokenizing query text (it should not - to do so would be
12663** inefficient), it doesn't matter if the user queries for
12664** 'first + place' or '1st + place', as there are entries in the
12665** FTS index corresponding to both forms of the first token.
12666** </ol>
12667**
12668** Whether it is parsing document or query text, any call to xToken that
12669** specifies a <i>tflags</i> argument with the FTS5_TOKEN_COLOCATED bit
12670** is considered to supply a synonym for the previous token. For example,
12671** when parsing the document "I won first place", a tokenizer that supports
12672** synonyms would call xToken() 5 times, as follows:
12673**
12674** <codeblock>
12675** xToken(pCtx, 0, "i", 1, 0, 1);
12676** xToken(pCtx, 0, "won", 3, 2, 5);
12677** xToken(pCtx, 0, "first", 5, 6, 11);
12678** xToken(pCtx, FTS5_TOKEN_COLOCATED, "1st", 3, 6, 11);
12679** xToken(pCtx, 0, "place", 5, 12, 17);
12680**</codeblock>
12681**
12682** It is an error to specify the FTS5_TOKEN_COLOCATED flag the first time
12683** xToken() is called. Multiple synonyms may be specified for a single token
12684** by making multiple calls to xToken(FTS5_TOKEN_COLOCATED) in sequence.
12685** There is no limit to the number of synonyms that may be provided for a
12686** single token.
12687**
12688** In many cases, method (1) above is the best approach. It does not add
12689** extra data to the FTS index or require FTS5 to query for multiple terms,
12690** so it is efficient in terms of disk space and query speed. However, it
12691** does not support prefix queries very well. If, as suggested above, the
12692** token "first" is substituted for "1st" by the tokenizer, then the query:
12693**
12694** <codeblock>
12695** ... MATCH '1s*'</codeblock>
12696**
12697** will not match documents that contain the token "1st" (as the tokenizer
12698** will probably not map "1s" to any prefix of "first").
12699**
12700** For full prefix support, method (3) may be preferred. In this case,
12701** because the index contains entries for both "first" and "1st", prefix
12702** queries such as 'fi*' or '1s*' will match correctly. However, because
12703** extra entries are added to the FTS index, this method uses more space
12704** within the database.
12705**
12706** Method (2) offers a midpoint between (1) and (3). Using this method,
12707** a query such as '1s*' will match documents that contain the literal
12708** token "1st", but not "first" (assuming the tokenizer is not able to
12709** provide synonyms for prefixes). However, a non-prefix query like '1st'
12710** will match against "1st" and "first". This method does not require
12711** extra disk space, as no extra entries are added to the FTS index.
12712** On the other hand, it may require more CPU cycles to run MATCH queries,
12713** as separate queries of the FTS index are required for each synonym.
12714**
12715** When using methods (2) or (3), it is important that the tokenizer only
12716** provide synonyms when tokenizing document text (method (2)) or query
12717** text (method (3)), not both. Doing so will not cause any errors, but is
12718** inefficient.
12719*/
12720typedef struct Fts5Tokenizer Fts5Tokenizer;
12721typedef struct fts5_tokenizer fts5_tokenizer;
12722struct fts5_tokenizer {
12723 int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
12724 void (*xDelete)(Fts5Tokenizer*);
12725 int (*xTokenize)(Fts5Tokenizer*,
12726 void *pCtx,
12727 int flags, /* Mask of FTS5_TOKENIZE_* flags */
12728 const char *pText, int nText,
12729 int (*xToken)(
12730 void *pCtx, /* Copy of 2nd argument to xTokenize() */
12731 int tflags, /* Mask of FTS5_TOKEN_* flags */
12732 const char *pToken, /* Pointer to buffer containing token */
12733 int nToken, /* Size of token in bytes */
12734 int iStart, /* Byte offset of token within input text */
12735 int iEnd /* Byte offset of end of token within input text */
12736 )
12737 );
12738};
12739
12740/* Flags that may be passed as the third argument to xTokenize() */
12741#define FTS5_TOKENIZE_QUERY 0x0001
12742#define FTS5_TOKENIZE_PREFIX 0x0002
12743#define FTS5_TOKENIZE_DOCUMENT 0x0004
12744#define FTS5_TOKENIZE_AUX 0x0008
12745
12746/* Flags that may be passed by the tokenizer implementation back to FTS5
12747** as the third argument to the supplied xToken callback. */
12748#define FTS5_TOKEN_COLOCATED 0x0001 /* Same position as prev. token */
12749
12750/*
12751** END OF CUSTOM TOKENIZERS
12752*************************************************************************/
12753
12754/*************************************************************************
12755** FTS5 EXTENSION REGISTRATION API
12756*/
12757typedef struct fts5_api fts5_api;
12758struct fts5_api {
12759 int iVersion; /* Currently always set to 2 */
12760
12761 /* Create a new tokenizer */
12762 int (*xCreateTokenizer)(
12763 fts5_api *pApi,
12764 const char *zName,
12765 void *pContext,
12766 fts5_tokenizer *pTokenizer,
12767 void (*xDestroy)(void*)
12768 );
12769
12770 /* Find an existing tokenizer */
12771 int (*xFindTokenizer)(
12772 fts5_api *pApi,
12773 const char *zName,
12774 void **ppContext,
12775 fts5_tokenizer *pTokenizer
12776 );
12777
12778 /* Create a new auxiliary function */
12779 int (*xCreateFunction)(
12780 fts5_api *pApi,
12781 const char *zName,
12782 void *pContext,
12783 fts5_extension_function xFunction,
12784 void (*xDestroy)(void*)
12785 );
12786};
12787
12788/*
12789** END OF REGISTRATION API
12790*************************************************************************/
12791
12792#if 0
12793} /* end of the 'extern "C"' block */
12794#endif
12795
12796#endif /* _FTS5_H */
12797
12798/******** End of fts5.h *********/
12799
12800/************** End of sqlite3.h *********************************************/
12801/************** Continuing where we left off in sqliteInt.h ******************/
12802
12803/*
12804** Include the configuration header output by 'configure' if we're using the
12805** autoconf-based build
12806*/
12807#if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
12808#include "config.h"
12809#define SQLITECONFIG_H 1
12810#endif
12811
12812/************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/
12813/************** Begin file sqliteLimit.h *************************************/
12814/*
12815** 2007 May 7
12816**
12817** The author disclaims copyright to this source code. In place of
12818** a legal notice, here is a blessing:
12819**
12820** May you do good and not evil.
12821** May you find forgiveness for yourself and forgive others.
12822** May you share freely, never taking more than you give.
12823**
12824*************************************************************************
12825**
12826** This file defines various limits of what SQLite can process.
12827*/
12828
12829/*
12830** The maximum length of a TEXT or BLOB in bytes. This also
12831** limits the size of a row in a table or index.
12832**
12833** The hard limit is the ability of a 32-bit signed integer
12834** to count the size: 2^31-1 or 2147483647.
12835*/
12836#ifndef SQLITE_MAX_LENGTH
12837# define SQLITE_MAX_LENGTH 1000000000
12838#endif
12839
12840/*
12841** This is the maximum number of
12842**
12843** * Columns in a table
12844** * Columns in an index
12845** * Columns in a view
12846** * Terms in the SET clause of an UPDATE statement
12847** * Terms in the result set of a SELECT statement
12848** * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.
12849** * Terms in the VALUES clause of an INSERT statement
12850**
12851** The hard upper limit here is 32676. Most database people will
12852** tell you that in a well-normalized database, you usually should
12853** not have more than a dozen or so columns in any table. And if
12854** that is the case, there is no point in having more than a few
12855** dozen values in any of the other situations described above.
12856*/
12857#ifndef SQLITE_MAX_COLUMN
12858# define SQLITE_MAX_COLUMN 2000
12859#endif
12860
12861/*
12862** The maximum length of a single SQL statement in bytes.
12863**
12864** It used to be the case that setting this value to zero would
12865** turn the limit off. That is no longer true. It is not possible
12866** to turn this limit off.
12867*/
12868#ifndef SQLITE_MAX_SQL_LENGTH
12869# define SQLITE_MAX_SQL_LENGTH 1000000000
12870#endif
12871
12872/*
12873** The maximum depth of an expression tree. This is limited to
12874** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might
12875** want to place more severe limits on the complexity of an
12876** expression. A value of 0 means that there is no limit.
12877*/
12878#ifndef SQLITE_MAX_EXPR_DEPTH
12879# define SQLITE_MAX_EXPR_DEPTH 1000
12880#endif
12881
12882/*
12883** The maximum number of terms in a compound SELECT statement.
12884** The code generator for compound SELECT statements does one
12885** level of recursion for each term. A stack overflow can result
12886** if the number of terms is too large. In practice, most SQL
12887** never has more than 3 or 4 terms. Use a value of 0 to disable
12888** any limit on the number of terms in a compount SELECT.
12889*/
12890#ifndef SQLITE_MAX_COMPOUND_SELECT
12891# define SQLITE_MAX_COMPOUND_SELECT 500
12892#endif
12893
12894/*
12895** The maximum number of opcodes in a VDBE program.
12896** Not currently enforced.
12897*/
12898#ifndef SQLITE_MAX_VDBE_OP
12899# define SQLITE_MAX_VDBE_OP 250000000
12900#endif
12901
12902/*
12903** The maximum number of arguments to an SQL function.
12904*/
12905#ifndef SQLITE_MAX_FUNCTION_ARG
12906# define SQLITE_MAX_FUNCTION_ARG 127
12907#endif
12908
12909/*
12910** The suggested maximum number of in-memory pages to use for
12911** the main database table and for temporary tables.
12912**
12913** IMPLEMENTATION-OF: R-30185-15359 The default suggested cache size is -2000,
12914** which means the cache size is limited to 2048000 bytes of memory.
12915** IMPLEMENTATION-OF: R-48205-43578 The default suggested cache size can be
12916** altered using the SQLITE_DEFAULT_CACHE_SIZE compile-time options.
12917*/
12918#ifndef SQLITE_DEFAULT_CACHE_SIZE
12919# define SQLITE_DEFAULT_CACHE_SIZE -2000
12920#endif
12921
12922/*
12923** The default number of frames to accumulate in the log file before
12924** checkpointing the database in WAL mode.
12925*/
12926#ifndef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT
12927# define SQLITE_DEFAULT_WAL_AUTOCHECKPOINT 1000
12928#endif
12929
12930/*
12931** The maximum number of attached databases. This must be between 0
12932** and 125. The upper bound of 125 is because the attached databases are
12933** counted using a signed 8-bit integer which has a maximum value of 127
12934** and we have to allow 2 extra counts for the "main" and "temp" databases.
12935*/
12936#ifndef SQLITE_MAX_ATTACHED
12937# define SQLITE_MAX_ATTACHED 10
12938#endif
12939
12940
12941/*
12942** The maximum value of a ?nnn wildcard that the parser will accept.
12943** If the value exceeds 32767 then extra space is required for the Expr
12944** structure. But otherwise, we believe that the number can be as large
12945** as a signed 32-bit integer can hold.
12946*/
12947#ifndef SQLITE_MAX_VARIABLE_NUMBER
12948# define SQLITE_MAX_VARIABLE_NUMBER 32766
12949#endif
12950
12951/* Maximum page size. The upper bound on this value is 65536. This a limit
12952** imposed by the use of 16-bit offsets within each page.
12953**
12954** Earlier versions of SQLite allowed the user to change this value at
12955** compile time. This is no longer permitted, on the grounds that it creates
12956** a library that is technically incompatible with an SQLite library
12957** compiled with a different limit. If a process operating on a database
12958** with a page-size of 65536 bytes crashes, then an instance of SQLite
12959** compiled with the default page-size limit will not be able to rollback
12960** the aborted transaction. This could lead to database corruption.
12961*/
12962#ifdef SQLITE_MAX_PAGE_SIZE
12963# undef SQLITE_MAX_PAGE_SIZE
12964#endif
12965#define SQLITE_MAX_PAGE_SIZE 65536
12966
12967
12968/*
12969** The default size of a database page.
12970*/
12971#ifndef SQLITE_DEFAULT_PAGE_SIZE
12972# define SQLITE_DEFAULT_PAGE_SIZE 4096
12973#endif
12974#if SQLITE_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
12975# undef SQLITE_DEFAULT_PAGE_SIZE
12976# define SQLITE_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
12977#endif
12978
12979/*
12980** Ordinarily, if no value is explicitly provided, SQLite creates databases
12981** with page size SQLITE_DEFAULT_PAGE_SIZE. However, based on certain
12982** device characteristics (sector-size and atomic write() support),
12983** SQLite may choose a larger value. This constant is the maximum value
12984** SQLite will choose on its own.
12985*/
12986#ifndef SQLITE_MAX_DEFAULT_PAGE_SIZE
12987# define SQLITE_MAX_DEFAULT_PAGE_SIZE 8192
12988#endif
12989#if SQLITE_MAX_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
12990# undef SQLITE_MAX_DEFAULT_PAGE_SIZE
12991# define SQLITE_MAX_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
12992#endif
12993
12994
12995/*
12996** Maximum number of pages in one database file.
12997**
12998** This is really just the default value for the max_page_count pragma.
12999** This value can be lowered (or raised) at run-time using that the
13000** max_page_count macro.
13001*/
13002#ifndef SQLITE_MAX_PAGE_COUNT
13003# define SQLITE_MAX_PAGE_COUNT 1073741823
13004#endif
13005
13006/*
13007** Maximum length (in bytes) of the pattern in a LIKE or GLOB
13008** operator.
13009*/
13010#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
13011# define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
13012#endif
13013
13014/*
13015** Maximum depth of recursion for triggers.
13016**
13017** A value of 1 means that a trigger program will not be able to itself
13018** fire any triggers. A value of 0 means that no trigger programs at all
13019** may be executed.
13020*/
13021#ifndef SQLITE_MAX_TRIGGER_DEPTH
13022# define SQLITE_MAX_TRIGGER_DEPTH 1000
13023#endif
13024
13025/************** End of sqliteLimit.h *****************************************/
13026/************** Continuing where we left off in sqliteInt.h ******************/
13027
13028/* Disable nuisance warnings on Borland compilers */
13029#if defined(__BORLANDC__)
13030#pragma warn -rch /* unreachable code */
13031#pragma warn -ccc /* Condition is always true or false */
13032#pragma warn -aus /* Assigned value is never used */
13033#pragma warn -csu /* Comparing signed and unsigned */
13034#pragma warn -spa /* Suspicious pointer arithmetic */
13035#endif
13036
13037/*
13038** WAL mode depends on atomic aligned 32-bit loads and stores in a few
13039** places. The following macros try to make this explicit.
13040*/
13041#ifndef __has_extension
13042# define __has_extension(x) 0 /* compatibility with non-clang compilers */
13043#endif
13044#if GCC_VERSION>=4007000 || __has_extension(c_atomic)
13045# define SQLITE_ATOMIC_INTRINSICS 1
13046# define AtomicLoad(PTR) __atomic_load_n((PTR),__ATOMIC_RELAXED)
13047# define AtomicStore(PTR,VAL) __atomic_store_n((PTR),(VAL),__ATOMIC_RELAXED)
13048#else
13049# define SQLITE_ATOMIC_INTRINSICS 0
13050# define AtomicLoad(PTR) (*(PTR))
13051# define AtomicStore(PTR,VAL) (*(PTR) = (VAL))
13052#endif
13053
13054/*
13055** Include standard header files as necessary
13056*/
13057#ifdef HAVE_STDINT_H
13058#include <stdint.h>
13059#endif
13060#ifdef HAVE_INTTYPES_H
13061#include <inttypes.h>
13062#endif
13063
13064/*
13065** The following macros are used to cast pointers to integers and
13066** integers to pointers. The way you do this varies from one compiler
13067** to the next, so we have developed the following set of #if statements
13068** to generate appropriate macros for a wide range of compilers.
13069**
13070** The correct "ANSI" way to do this is to use the intptr_t type.
13071** Unfortunately, that typedef is not available on all compilers, or
13072** if it is available, it requires an #include of specific headers
13073** that vary from one machine to the next.
13074**
13075** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on
13076** the ((void*)&((char*)0)[X]) construct. But MSVC chokes on ((void*)(X)).
13077** So we have to define the macros in different ways depending on the
13078** compiler.
13079*/
13080#if defined(HAVE_STDINT_H) /* Use this case if we have ANSI headers */
13081# define SQLITE_INT_TO_PTR(X) ((void*)(intptr_t)(X))
13082# define SQLITE_PTR_TO_INT(X) ((int)(intptr_t)(X))
13083#elif defined(__PTRDIFF_TYPE__) /* This case should work for GCC */
13084# define SQLITE_INT_TO_PTR(X) ((void*)(__PTRDIFF_TYPE__)(X))
13085# define SQLITE_PTR_TO_INT(X) ((int)(__PTRDIFF_TYPE__)(X))
13086#elif !defined(__GNUC__) /* Works for compilers other than LLVM */
13087# define SQLITE_INT_TO_PTR(X) ((void*)&((char*)0)[X])
13088# define SQLITE_PTR_TO_INT(X) ((int)(((char*)X)-(char*)0))
13089#else /* Generates a warning - but it always works */
13090# define SQLITE_INT_TO_PTR(X) ((void*)(X))
13091# define SQLITE_PTR_TO_INT(X) ((int)(X))
13092#endif
13093
13094/*
13095** A macro to hint to the compiler that a function should not be
13096** inlined.
13097*/
13098#if defined(__GNUC__)
13099# define SQLITE_NOINLINE __attribute__((noinline))
13100#elif defined(_MSC_VER) && _MSC_VER>=1310
13101# define SQLITE_NOINLINE __declspec(noinline)
13102#else
13103# define SQLITE_NOINLINE
13104#endif
13105
13106/*
13107** Make sure that the compiler intrinsics we desire are enabled when
13108** compiling with an appropriate version of MSVC unless prevented by
13109** the SQLITE_DISABLE_INTRINSIC define.
13110*/
13111#if !defined(SQLITE_DISABLE_INTRINSIC)
13112# if defined(_MSC_VER) && _MSC_VER>=1400
13113# if !defined(_WIN32_WCE)
13114# include <intrin.h>
13115# pragma intrinsic(_byteswap_ushort)
13116# pragma intrinsic(_byteswap_ulong)
13117# pragma intrinsic(_byteswap_uint64)
13118# pragma intrinsic(_ReadWriteBarrier)
13119# else
13120# include <cmnintrin.h>
13121# endif
13122# endif
13123#endif
13124
13125/*
13126** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
13127** 0 means mutexes are permanently disable and the library is never
13128** threadsafe. 1 means the library is serialized which is the highest
13129** level of threadsafety. 2 means the library is multithreaded - multiple
13130** threads can use SQLite as long as no two threads try to use the same
13131** database connection at the same time.
13132**
13133** Older versions of SQLite used an optional THREADSAFE macro.
13134** We support that for legacy.
13135**
13136** To ensure that the correct value of "THREADSAFE" is reported when querying
13137** for compile-time options at runtime (e.g. "PRAGMA compile_options"), this
13138** logic is partially replicated in ctime.c. If it is updated here, it should
13139** also be updated there.
13140*/
13141#if !defined(SQLITE_THREADSAFE)
13142# if defined(THREADSAFE)
13143# define SQLITE_THREADSAFE THREADSAFE
13144# else
13145# define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
13146# endif
13147#endif
13148
13149/*
13150** Powersafe overwrite is on by default. But can be turned off using
13151** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
13152*/
13153#ifndef SQLITE_POWERSAFE_OVERWRITE
13154# define SQLITE_POWERSAFE_OVERWRITE 1
13155#endif
13156
13157/*
13158** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by
13159** default unless SQLite is compiled with SQLITE_DEFAULT_MEMSTATUS=0 in
13160** which case memory allocation statistics are disabled by default.
13161*/
13162#if !defined(SQLITE_DEFAULT_MEMSTATUS)
13163# define SQLITE_DEFAULT_MEMSTATUS 1
13164#endif
13165
13166/*
13167** Exactly one of the following macros must be defined in order to
13168** specify which memory allocation subsystem to use.
13169**
13170** SQLITE_SYSTEM_MALLOC // Use normal system malloc()
13171** SQLITE_WIN32_MALLOC // Use Win32 native heap API
13172** SQLITE_ZERO_MALLOC // Use a stub allocator that always fails
13173** SQLITE_MEMDEBUG // Debugging version of system malloc()
13174**
13175** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
13176** assert() macro is enabled, each call into the Win32 native heap subsystem
13177** will cause HeapValidate to be called. If heap validation should fail, an
13178** assertion will be triggered.
13179**
13180** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
13181** the default.
13182*/
13183#if defined(SQLITE_SYSTEM_MALLOC) \
13184 + defined(SQLITE_WIN32_MALLOC) \
13185 + defined(SQLITE_ZERO_MALLOC) \
13186 + defined(SQLITE_MEMDEBUG)>1
13187# error "Two or more of the following compile-time configuration options\
13188 are defined but at most one is allowed:\
13189 SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG,\
13190 SQLITE_ZERO_MALLOC"
13191#endif
13192#if defined(SQLITE_SYSTEM_MALLOC) \
13193 + defined(SQLITE_WIN32_MALLOC) \
13194 + defined(SQLITE_ZERO_MALLOC) \
13195 + defined(SQLITE_MEMDEBUG)==0
13196# define SQLITE_SYSTEM_MALLOC 1
13197#endif
13198
13199/*
13200** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
13201** sizes of memory allocations below this value where possible.
13202*/
13203#if !defined(SQLITE_MALLOC_SOFT_LIMIT)
13204# define SQLITE_MALLOC_SOFT_LIMIT 1024
13205#endif
13206
13207/*
13208** We need to define _XOPEN_SOURCE as follows in order to enable
13209** recursive mutexes on most Unix systems and fchmod() on OpenBSD.
13210** But _XOPEN_SOURCE define causes problems for Mac OS X, so omit
13211** it.
13212*/
13213#if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__)
13214# define _XOPEN_SOURCE 600
13215#endif
13216
13217/*
13218** NDEBUG and SQLITE_DEBUG are opposites. It should always be true that
13219** defined(NDEBUG)==!defined(SQLITE_DEBUG). If this is not currently true,
13220** make it true by defining or undefining NDEBUG.
13221**
13222** Setting NDEBUG makes the code smaller and faster by disabling the
13223** assert() statements in the code. So we want the default action
13224** to be for NDEBUG to be set and NDEBUG to be undefined only if SQLITE_DEBUG
13225** is set. Thus NDEBUG becomes an opt-in rather than an opt-out
13226** feature.
13227*/
13228#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
13229# define NDEBUG 1
13230#endif
13231#if defined(NDEBUG) && defined(SQLITE_DEBUG)
13232# undef NDEBUG
13233#endif
13234
13235/*
13236** Enable SQLITE_ENABLE_EXPLAIN_COMMENTS if SQLITE_DEBUG is turned on.
13237*/
13238#if !defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) && defined(SQLITE_DEBUG)
13239# define SQLITE_ENABLE_EXPLAIN_COMMENTS 1
13240#endif
13241
13242/*
13243** The testcase() macro is used to aid in coverage testing. When
13244** doing coverage testing, the condition inside the argument to
13245** testcase() must be evaluated both true and false in order to
13246** get full branch coverage. The testcase() macro is inserted
13247** to help ensure adequate test coverage in places where simple
13248** condition/decision coverage is inadequate. For example, testcase()
13249** can be used to make sure boundary values are tested. For
13250** bitmask tests, testcase() can be used to make sure each bit
13251** is significant and used at least once. On switch statements
13252** where multiple cases go to the same block of code, testcase()
13253** can insure that all cases are evaluated.
13254*/
13255#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_DEBUG)
13256# ifndef SQLITE_AMALGAMATION
13257 extern unsigned int sqlite3CoverageCounter;
13258# endif
13259# define testcase(X) if( X ){ sqlite3CoverageCounter += (unsigned)__LINE__; }
13260#else
13261# define testcase(X)
13262#endif
13263
13264/*
13265** The TESTONLY macro is used to enclose variable declarations or
13266** other bits of code that are needed to support the arguments
13267** within testcase() and assert() macros.
13268*/
13269#if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST)
13270# define TESTONLY(X) X
13271#else
13272# define TESTONLY(X)
13273#endif
13274
13275/*
13276** Sometimes we need a small amount of code such as a variable initialization
13277** to setup for a later assert() statement. We do not want this code to
13278** appear when assert() is disabled. The following macro is therefore
13279** used to contain that setup code. The "VVA" acronym stands for
13280** "Verification, Validation, and Accreditation". In other words, the
13281** code within VVA_ONLY() will only run during verification processes.
13282*/
13283#ifndef NDEBUG
13284# define VVA_ONLY(X) X
13285#else
13286# define VVA_ONLY(X)
13287#endif
13288
13289/*
13290** Disable ALWAYS() and NEVER() (make them pass-throughs) for coverage
13291** and mutation testing
13292*/
13293#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
13294# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
13295#endif
13296
13297/*
13298** The ALWAYS and NEVER macros surround boolean expressions which
13299** are intended to always be true or false, respectively. Such
13300** expressions could be omitted from the code completely. But they
13301** are included in a few cases in order to enhance the resilience
13302** of SQLite to unexpected behavior - to make the code "self-healing"
13303** or "ductile" rather than being "brittle" and crashing at the first
13304** hint of unplanned behavior.
13305**
13306** In other words, ALWAYS and NEVER are added for defensive code.
13307**
13308** When doing coverage testing ALWAYS and NEVER are hard-coded to
13309** be true and false so that the unreachable code they specify will
13310** not be counted as untested code.
13311*/
13312#if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
13313# define ALWAYS(X) (1)
13314# define NEVER(X) (0)
13315#elif !defined(NDEBUG)
13316# define ALWAYS(X) ((X)?1:(assert(0),0))
13317# define NEVER(X) ((X)?(assert(0),1):0)
13318#else
13319# define ALWAYS(X) (X)
13320# define NEVER(X) (X)
13321#endif
13322
13323/*
13324** Some conditionals are optimizations only. In other words, if the
13325** conditionals are replaced with a constant 1 (true) or 0 (false) then
13326** the correct answer is still obtained, though perhaps not as quickly.
13327**
13328** The following macros mark these optimizations conditionals.
13329*/
13330#if defined(SQLITE_MUTATION_TEST)
13331# define OK_IF_ALWAYS_TRUE(X) (1)
13332# define OK_IF_ALWAYS_FALSE(X) (0)
13333#else
13334# define OK_IF_ALWAYS_TRUE(X) (X)
13335# define OK_IF_ALWAYS_FALSE(X) (X)
13336#endif
13337
13338/*
13339** Some malloc failures are only possible if SQLITE_TEST_REALLOC_STRESS is
13340** defined. We need to defend against those failures when testing with
13341** SQLITE_TEST_REALLOC_STRESS, but we don't want the unreachable branches
13342** during a normal build. The following macro can be used to disable tests
13343** that are always false except when SQLITE_TEST_REALLOC_STRESS is set.
13344*/
13345#if defined(SQLITE_TEST_REALLOC_STRESS)
13346# define ONLY_IF_REALLOC_STRESS(X) (X)
13347#elif !defined(NDEBUG)
13348# define ONLY_IF_REALLOC_STRESS(X) ((X)?(assert(0),1):0)
13349#else
13350# define ONLY_IF_REALLOC_STRESS(X) (0)
13351#endif
13352
13353/*
13354** Declarations used for tracing the operating system interfaces.
13355*/
13356#if defined(SQLITE_FORCE_OS_TRACE) || defined(SQLITE_TEST) || \
13357 (defined(SQLITE_DEBUG) && SQLITE_OS_WIN)
13358 extern int sqlite3OSTrace;
13359# define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
13360# define SQLITE_HAVE_OS_TRACE
13361#else
13362# define OSTRACE(X)
13363# undef SQLITE_HAVE_OS_TRACE
13364#endif
13365
13366/*
13367** Is the sqlite3ErrName() function needed in the build? Currently,
13368** it is needed by "mutex_w32.c" (when debugging), "os_win.c" (when
13369** OSTRACE is enabled), and by several "test*.c" files (which are
13370** compiled using SQLITE_TEST).
13371*/
13372#if defined(SQLITE_HAVE_OS_TRACE) || defined(SQLITE_TEST) || \
13373 (defined(SQLITE_DEBUG) && SQLITE_OS_WIN)
13374# define SQLITE_NEED_ERR_NAME
13375#else
13376# undef SQLITE_NEED_ERR_NAME
13377#endif
13378
13379/*
13380** SQLITE_ENABLE_EXPLAIN_COMMENTS is incompatible with SQLITE_OMIT_EXPLAIN
13381*/
13382#ifdef SQLITE_OMIT_EXPLAIN
13383# undef SQLITE_ENABLE_EXPLAIN_COMMENTS
13384#endif
13385
13386/*
13387** SQLITE_OMIT_VIRTUALTABLE implies SQLITE_OMIT_ALTERTABLE
13388*/
13389#if defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_ALTERTABLE)
13390# define SQLITE_OMIT_ALTERTABLE
13391#endif
13392
13393/*
13394** Return true (non-zero) if the input is an integer that is too large
13395** to fit in 32-bits. This macro is used inside of various testcase()
13396** macros to verify that we have tested SQLite for large-file support.
13397*/
13398#define IS_BIG_INT(X) (((X)&~(i64)0xffffffff)!=0)
13399
13400/*
13401** The macro unlikely() is a hint that surrounds a boolean
13402** expression that is usually false. Macro likely() surrounds
13403** a boolean expression that is usually true. These hints could,
13404** in theory, be used by the compiler to generate better code, but
13405** currently they are just comments for human readers.
13406*/
13407#define likely(X) (X)
13408#define unlikely(X) (X)
13409
13410/************** Include hash.h in the middle of sqliteInt.h ******************/
13411/************** Begin file hash.h ********************************************/
13412/*
13413** 2001 September 22
13414**
13415** The author disclaims copyright to this source code. In place of
13416** a legal notice, here is a blessing:
13417**
13418** May you do good and not evil.
13419** May you find forgiveness for yourself and forgive others.
13420** May you share freely, never taking more than you give.
13421**
13422*************************************************************************
13423** This is the header file for the generic hash-table implementation
13424** used in SQLite.
13425*/
13426#ifndef SQLITE_HASH_H
13427#define SQLITE_HASH_H
13428
13429/* Forward declarations of structures. */
13430typedef struct Hash Hash;
13431typedef struct HashElem HashElem;
13432
13433/* A complete hash table is an instance of the following structure.
13434** The internals of this structure are intended to be opaque -- client
13435** code should not attempt to access or modify the fields of this structure
13436** directly. Change this structure only by using the routines below.
13437** However, some of the "procedures" and "functions" for modifying and
13438** accessing this structure are really macros, so we can't really make
13439** this structure opaque.
13440**
13441** All elements of the hash table are on a single doubly-linked list.
13442** Hash.first points to the head of this list.
13443**
13444** There are Hash.htsize buckets. Each bucket points to a spot in
13445** the global doubly-linked list. The contents of the bucket are the
13446** element pointed to plus the next _ht.count-1 elements in the list.
13447**
13448** Hash.htsize and Hash.ht may be zero. In that case lookup is done
13449** by a linear search of the global list. For small tables, the
13450** Hash.ht table is never allocated because if there are few elements
13451** in the table, it is faster to do a linear search than to manage
13452** the hash table.
13453*/
13454struct Hash {
13455 unsigned int htsize; /* Number of buckets in the hash table */
13456 unsigned int count; /* Number of entries in this table */
13457 HashElem *first; /* The first element of the array */
13458 struct _ht { /* the hash table */
13459 unsigned int count; /* Number of entries with this hash */
13460 HashElem *chain; /* Pointer to first entry with this hash */
13461 } *ht;
13462};
13463
13464/* Each element in the hash table is an instance of the following
13465** structure. All elements are stored on a single doubly-linked list.
13466**
13467** Again, this structure is intended to be opaque, but it can't really
13468** be opaque because it is used by macros.
13469*/
13470struct HashElem {
13471 HashElem *next, *prev; /* Next and previous elements in the table */
13472 void *data; /* Data associated with this element */
13473 const char *pKey; /* Key associated with this element */
13474};
13475
13476/*
13477** Access routines. To delete, insert a NULL pointer.
13478*/
13479SQLITE_PRIVATE void sqlite3HashInit(Hash*);
13480SQLITE_PRIVATE void *sqlite3HashInsert(Hash*, const char *pKey, void *pData);
13481SQLITE_PRIVATE void *sqlite3HashFind(const Hash*, const char *pKey);
13482SQLITE_PRIVATE void sqlite3HashClear(Hash*);
13483
13484/*
13485** Macros for looping over all elements of a hash table. The idiom is
13486** like this:
13487**
13488** Hash h;
13489** HashElem *p;
13490** ...
13491** for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
13492** SomeStructure *pData = sqliteHashData(p);
13493** // do something with pData
13494** }
13495*/
13496#define sqliteHashFirst(H) ((H)->first)
13497#define sqliteHashNext(E) ((E)->next)
13498#define sqliteHashData(E) ((E)->data)
13499/* #define sqliteHashKey(E) ((E)->pKey) // NOT USED */
13500/* #define sqliteHashKeysize(E) ((E)->nKey) // NOT USED */
13501
13502/*
13503** Number of entries in a hash table
13504*/
13505#define sqliteHashCount(H) ((H)->count)
13506
13507#endif /* SQLITE_HASH_H */
13508
13509/************** End of hash.h ************************************************/
13510/************** Continuing where we left off in sqliteInt.h ******************/
13511/************** Include parse.h in the middle of sqliteInt.h *****************/
13512/************** Begin file parse.h *******************************************/
13513#define TK_SEMI 1
13514#define TK_EXPLAIN 2
13515#define TK_QUERY 3
13516#define TK_PLAN 4
13517#define TK_BEGIN 5
13518#define TK_TRANSACTION 6
13519#define TK_DEFERRED 7
13520#define TK_IMMEDIATE 8
13521#define TK_EXCLUSIVE 9
13522#define TK_COMMIT 10
13523#define TK_END 11
13524#define TK_ROLLBACK 12
13525#define TK_SAVEPOINT 13
13526#define TK_RELEASE 14
13527#define TK_TO 15
13528#define TK_TABLE 16
13529#define TK_CREATE 17
13530#define TK_IF 18
13531#define TK_NOT 19
13532#define TK_EXISTS 20
13533#define TK_TEMP 21
13534#define TK_LP 22
13535#define TK_RP 23
13536#define TK_AS 24
13537#define TK_COMMA 25
13538#define TK_WITHOUT 26
13539#define TK_ABORT 27
13540#define TK_ACTION 28
13541#define TK_AFTER 29
13542#define TK_ANALYZE 30
13543#define TK_ASC 31
13544#define TK_ATTACH 32
13545#define TK_BEFORE 33
13546#define TK_BY 34
13547#define TK_CASCADE 35
13548#define TK_CAST 36
13549#define TK_CONFLICT 37
13550#define TK_DATABASE 38
13551#define TK_DESC 39
13552#define TK_DETACH 40
13553#define TK_EACH 41
13554#define TK_FAIL 42
13555#define TK_OR 43
13556#define TK_AND 44
13557#define TK_IS 45
13558#define TK_MATCH 46
13559#define TK_LIKE_KW 47
13560#define TK_BETWEEN 48
13561#define TK_IN 49
13562#define TK_ISNULL 50
13563#define TK_NOTNULL 51
13564#define TK_NE 52
13565#define TK_EQ 53
13566#define TK_GT 54
13567#define TK_LE 55
13568#define TK_LT 56
13569#define TK_GE 57
13570#define TK_ESCAPE 58
13571#define TK_ID 59
13572#define TK_COLUMNKW 60
13573#define TK_DO 61
13574#define TK_FOR 62
13575#define TK_IGNORE 63
13576#define TK_INITIALLY 64
13577#define TK_INSTEAD 65
13578#define TK_NO 66
13579#define TK_KEY 67
13580#define TK_OF 68
13581#define TK_OFFSET 69
13582#define TK_PRAGMA 70
13583#define TK_RAISE 71
13584#define TK_RECURSIVE 72
13585#define TK_REPLACE 73
13586#define TK_RESTRICT 74
13587#define TK_ROW 75
13588#define TK_ROWS 76
13589#define TK_TRIGGER 77
13590#define TK_VACUUM 78
13591#define TK_VIEW 79
13592#define TK_VIRTUAL 80
13593#define TK_WITH 81
13594#define TK_NULLS 82
13595#define TK_FIRST 83
13596#define TK_LAST 84
13597#define TK_CURRENT 85
13598#define TK_FOLLOWING 86
13599#define TK_PARTITION 87
13600#define TK_PRECEDING 88
13601#define TK_RANGE 89
13602#define TK_UNBOUNDED 90
13603#define TK_EXCLUDE 91
13604#define TK_GROUPS 92
13605#define TK_OTHERS 93
13606#define TK_TIES 94
13607#define TK_GENERATED 95
13608#define TK_ALWAYS 96
13609#define TK_MATERIALIZED 97
13610#define TK_REINDEX 98
13611#define TK_RENAME 99
13612#define TK_CTIME_KW 100
13613#define TK_ANY 101
13614#define TK_BITAND 102
13615#define TK_BITOR 103
13616#define TK_LSHIFT 104
13617#define TK_RSHIFT 105
13618#define TK_PLUS 106
13619#define TK_MINUS 107
13620#define TK_STAR 108
13621#define TK_SLASH 109
13622#define TK_REM 110
13623#define TK_CONCAT 111
13624#define TK_COLLATE 112
13625#define TK_BITNOT 113
13626#define TK_ON 114
13627#define TK_INDEXED 115
13628#define TK_STRING 116
13629#define TK_JOIN_KW 117
13630#define TK_CONSTRAINT 118
13631#define TK_DEFAULT 119
13632#define TK_NULL 120
13633#define TK_PRIMARY 121
13634#define TK_UNIQUE 122
13635#define TK_CHECK 123
13636#define TK_REFERENCES 124
13637#define TK_AUTOINCR 125
13638#define TK_INSERT 126
13639#define TK_DELETE 127
13640#define TK_UPDATE 128
13641#define TK_SET 129
13642#define TK_DEFERRABLE 130
13643#define TK_FOREIGN 131
13644#define TK_DROP 132
13645#define TK_UNION 133
13646#define TK_ALL 134
13647#define TK_EXCEPT 135
13648#define TK_INTERSECT 136
13649#define TK_SELECT 137
13650#define TK_VALUES 138
13651#define TK_DISTINCT 139
13652#define TK_DOT 140
13653#define TK_FROM 141
13654#define TK_JOIN 142
13655#define TK_USING 143
13656#define TK_ORDER 144
13657#define TK_GROUP 145
13658#define TK_HAVING 146
13659#define TK_LIMIT 147
13660#define TK_WHERE 148
13661#define TK_RETURNING 149
13662#define TK_INTO 150
13663#define TK_NOTHING 151
13664#define TK_FLOAT 152
13665#define TK_BLOB 153
13666#define TK_INTEGER 154
13667#define TK_VARIABLE 155
13668#define TK_CASE 156
13669#define TK_WHEN 157
13670#define TK_THEN 158
13671#define TK_ELSE 159
13672#define TK_INDEX 160
13673#define TK_ALTER 161
13674#define TK_ADD 162
13675#define TK_WINDOW 163
13676#define TK_OVER 164
13677#define TK_FILTER 165
13678#define TK_COLUMN 166
13679#define TK_AGG_FUNCTION 167
13680#define TK_AGG_COLUMN 168
13681#define TK_TRUEFALSE 169
13682#define TK_ISNOT 170
13683#define TK_FUNCTION 171
13684#define TK_UMINUS 172
13685#define TK_UPLUS 173
13686#define TK_TRUTH 174
13687#define TK_REGISTER 175
13688#define TK_VECTOR 176
13689#define TK_SELECT_COLUMN 177
13690#define TK_IF_NULL_ROW 178
13691#define TK_ASTERISK 179
13692#define TK_SPAN 180
13693#define TK_ERROR 181
13694#define TK_SPACE 182
13695#define TK_ILLEGAL 183
13696
13697/************** End of parse.h ***********************************************/
13698/************** Continuing where we left off in sqliteInt.h ******************/
13699#include <stdio.h>
13700#include <stdlib.h>
13701#include <string.h>
13702#include <assert.h>
13703#include <stddef.h>
13704
13705/*
13706** Use a macro to replace memcpy() if compiled with SQLITE_INLINE_MEMCPY.
13707** This allows better measurements of where memcpy() is used when running
13708** cachegrind. But this macro version of memcpy() is very slow so it
13709** should not be used in production. This is a performance measurement
13710** hack only.
13711*/
13712#ifdef SQLITE_INLINE_MEMCPY
13713# define memcpy(D,S,N) {char*xxd=(char*)(D);const char*xxs=(const char*)(S);\
13714 int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);}
13715#endif
13716
13717/*
13718** If compiling for a processor that lacks floating point support,
13719** substitute integer for floating-point
13720*/
13721#ifdef SQLITE_OMIT_FLOATING_POINT
13722# define double sqlite_int64
13723# define float sqlite_int64
13724# define LONGDOUBLE_TYPE sqlite_int64
13725# ifndef SQLITE_BIG_DBL
13726# define SQLITE_BIG_DBL (((sqlite3_int64)1)<<50)
13727# endif
13728# define SQLITE_OMIT_DATETIME_FUNCS 1
13729# define SQLITE_OMIT_TRACE 1
13730# undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
13731# undef SQLITE_HAVE_ISNAN
13732#endif
13733#ifndef SQLITE_BIG_DBL
13734# define SQLITE_BIG_DBL (1e99)
13735#endif
13736
13737/*
13738** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0
13739** afterward. Having this macro allows us to cause the C compiler
13740** to omit code used by TEMP tables without messy #ifndef statements.
13741*/
13742#ifdef SQLITE_OMIT_TEMPDB
13743#define OMIT_TEMPDB 1
13744#else
13745#define OMIT_TEMPDB 0
13746#endif
13747
13748/*
13749** The "file format" number is an integer that is incremented whenever
13750** the VDBE-level file format changes. The following macros define the
13751** the default file format for new databases and the maximum file format
13752** that the library can read.
13753*/
13754#define SQLITE_MAX_FILE_FORMAT 4
13755#ifndef SQLITE_DEFAULT_FILE_FORMAT
13756# define SQLITE_DEFAULT_FILE_FORMAT 4
13757#endif
13758
13759/*
13760** Determine whether triggers are recursive by default. This can be
13761** changed at run-time using a pragma.
13762*/
13763#ifndef SQLITE_DEFAULT_RECURSIVE_TRIGGERS
13764# define SQLITE_DEFAULT_RECURSIVE_TRIGGERS 0
13765#endif
13766
13767/*
13768** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
13769** on the command-line
13770*/
13771#ifndef SQLITE_TEMP_STORE
13772# define SQLITE_TEMP_STORE 1
13773#endif
13774
13775/*
13776** If no value has been provided for SQLITE_MAX_WORKER_THREADS, or if
13777** SQLITE_TEMP_STORE is set to 3 (never use temporary files), set it
13778** to zero.
13779*/
13780#if SQLITE_TEMP_STORE==3 || SQLITE_THREADSAFE==0
13781# undef SQLITE_MAX_WORKER_THREADS
13782# define SQLITE_MAX_WORKER_THREADS 0
13783#endif
13784#ifndef SQLITE_MAX_WORKER_THREADS
13785# define SQLITE_MAX_WORKER_THREADS 8
13786#endif
13787#ifndef SQLITE_DEFAULT_WORKER_THREADS
13788# define SQLITE_DEFAULT_WORKER_THREADS 0
13789#endif
13790#if SQLITE_DEFAULT_WORKER_THREADS>SQLITE_MAX_WORKER_THREADS
13791# undef SQLITE_MAX_WORKER_THREADS
13792# define SQLITE_MAX_WORKER_THREADS SQLITE_DEFAULT_WORKER_THREADS
13793#endif
13794
13795/*
13796** The default initial allocation for the pagecache when using separate
13797** pagecaches for each database connection. A positive number is the
13798** number of pages. A negative number N translations means that a buffer
13799** of -1024*N bytes is allocated and used for as many pages as it will hold.
13800**
13801** The default value of "20" was chosen to minimize the run-time of the
13802** speedtest1 test program with options: --shrink-memory --reprepare
13803*/
13804#ifndef SQLITE_DEFAULT_PCACHE_INITSZ
13805# define SQLITE_DEFAULT_PCACHE_INITSZ 20
13806#endif
13807
13808/*
13809** Default value for the SQLITE_CONFIG_SORTERREF_SIZE option.
13810*/
13811#ifndef SQLITE_DEFAULT_SORTERREF_SIZE
13812# define SQLITE_DEFAULT_SORTERREF_SIZE 0x7fffffff
13813#endif
13814
13815/*
13816** The compile-time options SQLITE_MMAP_READWRITE and
13817** SQLITE_ENABLE_BATCH_ATOMIC_WRITE are not compatible with one another.
13818** You must choose one or the other (or neither) but not both.
13819*/
13820#if defined(SQLITE_MMAP_READWRITE) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
13821#error Cannot use both SQLITE_MMAP_READWRITE and SQLITE_ENABLE_BATCH_ATOMIC_WRITE
13822#endif
13823
13824/*
13825** GCC does not define the offsetof() macro so we'll have to do it
13826** ourselves.
13827*/
13828#ifndef offsetof
13829#define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
13830#endif
13831
13832/*
13833** Macros to compute minimum and maximum of two numbers.
13834*/
13835#ifndef MIN
13836# define MIN(A,B) ((A)<(B)?(A):(B))
13837#endif
13838#ifndef MAX
13839# define MAX(A,B) ((A)>(B)?(A):(B))
13840#endif
13841
13842/*
13843** Swap two objects of type TYPE.
13844*/
13845#define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
13846
13847/*
13848** Check to see if this machine uses EBCDIC. (Yes, believe it or
13849** not, there are still machines out there that use EBCDIC.)
13850*/
13851#if 'A' == '\301'
13852# define SQLITE_EBCDIC 1
13853#else
13854# define SQLITE_ASCII 1
13855#endif
13856
13857/*
13858** Integers of known sizes. These typedefs might change for architectures
13859** where the sizes very. Preprocessor macros are available so that the
13860** types can be conveniently redefined at compile-type. Like this:
13861**
13862** cc '-DUINTPTR_TYPE=long long int' ...
13863*/
13864#ifndef UINT32_TYPE
13865# ifdef HAVE_UINT32_T
13866# define UINT32_TYPE uint32_t
13867# else
13868# define UINT32_TYPE unsigned int
13869# endif
13870#endif
13871#ifndef UINT16_TYPE
13872# ifdef HAVE_UINT16_T
13873# define UINT16_TYPE uint16_t
13874# else
13875# define UINT16_TYPE unsigned short int
13876# endif
13877#endif
13878#ifndef INT16_TYPE
13879# ifdef HAVE_INT16_T
13880# define INT16_TYPE int16_t
13881# else
13882# define INT16_TYPE short int
13883# endif
13884#endif
13885#ifndef UINT8_TYPE
13886# ifdef HAVE_UINT8_T
13887# define UINT8_TYPE uint8_t
13888# else
13889# define UINT8_TYPE unsigned char
13890# endif
13891#endif
13892#ifndef INT8_TYPE
13893# ifdef HAVE_INT8_T
13894# define INT8_TYPE int8_t
13895# else
13896# define INT8_TYPE signed char
13897# endif
13898#endif
13899#ifndef LONGDOUBLE_TYPE
13900# define LONGDOUBLE_TYPE long double
13901#endif
13902typedef sqlite_int64 i64; /* 8-byte signed integer */
13903typedef sqlite_uint64 u64; /* 8-byte unsigned integer */
13904typedef UINT32_TYPE u32; /* 4-byte unsigned integer */
13905typedef UINT16_TYPE u16; /* 2-byte unsigned integer */
13906typedef INT16_TYPE i16; /* 2-byte signed integer */
13907typedef UINT8_TYPE u8; /* 1-byte unsigned integer */
13908typedef INT8_TYPE i8; /* 1-byte signed integer */
13909
13910/*
13911** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
13912** that can be stored in a u32 without loss of data. The value
13913** is 0x00000000ffffffff. But because of quirks of some compilers, we
13914** have to specify the value in the less intuitive manner shown:
13915*/
13916#define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
13917
13918/*
13919** The datatype used to store estimates of the number of rows in a
13920** table or index. This is an unsigned integer type. For 99.9% of
13921** the world, a 32-bit integer is sufficient. But a 64-bit integer
13922** can be used at compile-time if desired.
13923*/
13924#ifdef SQLITE_64BIT_STATS
13925 typedef u64 tRowcnt; /* 64-bit only if requested at compile-time */
13926#else
13927 typedef u32 tRowcnt; /* 32-bit is the default */
13928#endif
13929
13930/*
13931** Estimated quantities used for query planning are stored as 16-bit
13932** logarithms. For quantity X, the value stored is 10*log2(X). This
13933** gives a possible range of values of approximately 1.0e986 to 1e-986.
13934** But the allowed values are "grainy". Not every value is representable.
13935** For example, quantities 16 and 17 are both represented by a LogEst
13936** of 40. However, since LogEst quantities are suppose to be estimates,
13937** not exact values, this imprecision is not a problem.
13938**
13939** "LogEst" is short for "Logarithmic Estimate".
13940**
13941** Examples:
13942** 1 -> 0 20 -> 43 10000 -> 132
13943** 2 -> 10 25 -> 46 25000 -> 146
13944** 3 -> 16 100 -> 66 1000000 -> 199
13945** 4 -> 20 1000 -> 99 1048576 -> 200
13946** 10 -> 33 1024 -> 100 4294967296 -> 320
13947**
13948** The LogEst can be negative to indicate fractional values.
13949** Examples:
13950**
13951** 0.5 -> -10 0.1 -> -33 0.0625 -> -40
13952*/
13953typedef INT16_TYPE LogEst;
13954
13955/*
13956** Set the SQLITE_PTRSIZE macro to the number of bytes in a pointer
13957*/
13958#ifndef SQLITE_PTRSIZE
13959# if defined(__SIZEOF_POINTER__)
13960# define SQLITE_PTRSIZE __SIZEOF_POINTER__
13961# elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \
13962 defined(_M_ARM) || defined(__arm__) || defined(__x86) || \
13963 (defined(__APPLE__) && defined(__POWERPC__)) || \
13964 (defined(__TOS_AIX__) && !defined(__64BIT__))
13965# define SQLITE_PTRSIZE 4
13966# else
13967# define SQLITE_PTRSIZE 8
13968# endif
13969#endif
13970
13971/* The uptr type is an unsigned integer large enough to hold a pointer
13972*/
13973#if defined(HAVE_STDINT_H)
13974 typedef uintptr_t uptr;
13975#elif SQLITE_PTRSIZE==4
13976 typedef u32 uptr;
13977#else
13978 typedef u64 uptr;
13979#endif
13980
13981/*
13982** The SQLITE_WITHIN(P,S,E) macro checks to see if pointer P points to
13983** something between S (inclusive) and E (exclusive).
13984**
13985** In other words, S is a buffer and E is a pointer to the first byte after
13986** the end of buffer S. This macro returns true if P points to something
13987** contained within the buffer S.
13988*/
13989#define SQLITE_WITHIN(P,S,E) (((uptr)(P)>=(uptr)(S))&&((uptr)(P)<(uptr)(E)))
13990
13991
13992/*
13993** Macros to determine whether the machine is big or little endian,
13994** and whether or not that determination is run-time or compile-time.
13995**
13996** For best performance, an attempt is made to guess at the byte-order
13997** using C-preprocessor macros. If that is unsuccessful, or if
13998** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined
13999** at run-time.
14000*/
14001#ifndef SQLITE_BYTEORDER
14002# if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
14003 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
14004 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
14005 defined(__ARMEL__) || defined(__AARCH64EL__) || defined(_M_ARM64)
14006# define SQLITE_BYTEORDER 1234
14007# elif defined(sparc) || defined(__ppc__) || \
14008 defined(__ARMEB__) || defined(__AARCH64EB__)
14009# define SQLITE_BYTEORDER 4321
14010# else
14011# define SQLITE_BYTEORDER 0
14012# endif
14013#endif
14014#if SQLITE_BYTEORDER==4321
14015# define SQLITE_BIGENDIAN 1
14016# define SQLITE_LITTLEENDIAN 0
14017# define SQLITE_UTF16NATIVE SQLITE_UTF16BE
14018#elif SQLITE_BYTEORDER==1234
14019# define SQLITE_BIGENDIAN 0
14020# define SQLITE_LITTLEENDIAN 1
14021# define SQLITE_UTF16NATIVE SQLITE_UTF16LE
14022#else
14023# ifdef SQLITE_AMALGAMATION
14024 const int sqlite3one = 1;
14025# else
14026 extern const int sqlite3one;
14027# endif
14028# define SQLITE_BIGENDIAN (*(char *)(&sqlite3one)==0)
14029# define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
14030# define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
14031#endif
14032
14033/*
14034** Constants for the largest and smallest possible 64-bit signed integers.
14035** These macros are designed to work correctly on both 32-bit and 64-bit
14036** compilers.
14037*/
14038#define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
14039#define LARGEST_UINT64 (0xffffffff|(((u64)0xffffffff)<<32))
14040#define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
14041
14042/*
14043** Round up a number to the next larger multiple of 8. This is used
14044** to force 8-byte alignment on 64-bit architectures.
14045*/
14046#define ROUND8(x) (((x)+7)&~7)
14047
14048/*
14049** Round down to the nearest multiple of 8
14050*/
14051#define ROUNDDOWN8(x) ((x)&~7)
14052
14053/*
14054** Assert that the pointer X is aligned to an 8-byte boundary. This
14055** macro is used only within assert() to verify that the code gets
14056** all alignment restrictions correct.
14057**
14058** Except, if SQLITE_4_BYTE_ALIGNED_MALLOC is defined, then the
14059** underlying malloc() implementation might return us 4-byte aligned
14060** pointers. In that case, only verify 4-byte alignment.
14061*/
14062#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
14063# define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&3)==0)
14064#else
14065# define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&7)==0)
14066#endif
14067
14068/*
14069** Disable MMAP on platforms where it is known to not work
14070*/
14071#if defined(__OpenBSD__) || defined(__QNXNTO__)
14072# undef SQLITE_MAX_MMAP_SIZE
14073# define SQLITE_MAX_MMAP_SIZE 0
14074#endif
14075
14076/*
14077** Default maximum size of memory used by memory-mapped I/O in the VFS
14078*/
14079#ifdef __APPLE__
14080# include <TargetConditionals.h>
14081#endif
14082#ifndef SQLITE_MAX_MMAP_SIZE
14083# if defined(__linux__) \
14084 || defined(_WIN32) \
14085 || (defined(__APPLE__) && defined(__MACH__)) \
14086 || defined(__sun) \
14087 || defined(__FreeBSD__) \
14088 || defined(__DragonFly__)
14089# define SQLITE_MAX_MMAP_SIZE 0x7fff0000 /* 2147418112 */
14090# else
14091# define SQLITE_MAX_MMAP_SIZE 0
14092# endif
14093#endif
14094
14095/*
14096** The default MMAP_SIZE is zero on all platforms. Or, even if a larger
14097** default MMAP_SIZE is specified at compile-time, make sure that it does
14098** not exceed the maximum mmap size.
14099*/
14100#ifndef SQLITE_DEFAULT_MMAP_SIZE
14101# define SQLITE_DEFAULT_MMAP_SIZE 0
14102#endif
14103#if SQLITE_DEFAULT_MMAP_SIZE>SQLITE_MAX_MMAP_SIZE
14104# undef SQLITE_DEFAULT_MMAP_SIZE
14105# define SQLITE_DEFAULT_MMAP_SIZE SQLITE_MAX_MMAP_SIZE
14106#endif
14107
14108/*
14109** SELECTTRACE_ENABLED will be either 1 or 0 depending on whether or not
14110** the Select query generator tracing logic is turned on.
14111*/
14112#if !defined(SQLITE_AMALGAMATION)
14113SQLITE_PRIVATE u32 sqlite3SelectTrace;
14114#endif
14115#if defined(SQLITE_DEBUG) \
14116 && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_SELECTTRACE))
14117# define SELECTTRACE_ENABLED 1
14118# define SELECTTRACE(K,P,S,X) \
14119 if(sqlite3SelectTrace&(K)) \
14120 sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
14121 sqlite3DebugPrintf X
14122#else
14123# define SELECTTRACE(K,P,S,X)
14124# define SELECTTRACE_ENABLED 0
14125#endif
14126
14127/*
14128** Macros for "wheretrace"
14129*/
14130SQLITE_PRIVATE u32 sqlite3WhereTrace;
14131#if defined(SQLITE_DEBUG) \
14132 && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
14133# define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X
14134# define WHERETRACE_ENABLED 1
14135#else
14136# define WHERETRACE(K,X)
14137#endif
14138
14139
14140/*
14141** An instance of the following structure is used to store the busy-handler
14142** callback for a given sqlite handle.
14143**
14144** The sqlite.busyHandler member of the sqlite struct contains the busy
14145** callback for the database handle. Each pager opened via the sqlite
14146** handle is passed a pointer to sqlite.busyHandler. The busy-handler
14147** callback is currently invoked only from within pager.c.
14148*/
14149typedef struct BusyHandler BusyHandler;
14150struct BusyHandler {
14151 int (*xBusyHandler)(void *,int); /* The busy callback */
14152 void *pBusyArg; /* First arg to busy callback */
14153 int nBusy; /* Incremented with each busy call */
14154};
14155
14156/*
14157** Name of table that holds the database schema.
14158**
14159** The PREFERRED names are used whereever possible. But LEGACY is also
14160** used for backwards compatibility.
14161**
14162** 1. Queries can use either the PREFERRED or the LEGACY names
14163** 2. The sqlite3_set_authorizer() callback uses the LEGACY name
14164** 3. The PRAGMA table_list statement uses the PREFERRED name
14165**
14166** The LEGACY names are stored in the internal symbol hash table
14167** in support of (2). Names are translated using sqlite3PreferredTableName()
14168** for (3). The sqlite3FindTable() function takes care of translating
14169** names for (1).
14170**
14171** Note that "sqlite_temp_schema" can also be called "temp.sqlite_schema".
14172*/
14173#define LEGACY_SCHEMA_TABLE "sqlite_master"
14174#define LEGACY_TEMP_SCHEMA_TABLE "sqlite_temp_master"
14175#define PREFERRED_SCHEMA_TABLE "sqlite_schema"
14176#define PREFERRED_TEMP_SCHEMA_TABLE "sqlite_temp_schema"
14177
14178
14179/*
14180** The root-page of the schema table.
14181*/
14182#define SCHEMA_ROOT 1
14183
14184/*
14185** The name of the schema table. The name is different for TEMP.
14186*/
14187#define SCHEMA_TABLE(x) \
14188 ((!OMIT_TEMPDB)&&(x==1)?LEGACY_TEMP_SCHEMA_TABLE:LEGACY_SCHEMA_TABLE)
14189
14190/*
14191** A convenience macro that returns the number of elements in
14192** an array.
14193*/
14194#define ArraySize(X) ((int)(sizeof(X)/sizeof(X[0])))
14195
14196/*
14197** Determine if the argument is a power of two
14198*/
14199#define IsPowerOfTwo(X) (((X)&((X)-1))==0)
14200
14201/*
14202** The following value as a destructor means to use sqlite3DbFree().
14203** The sqlite3DbFree() routine requires two parameters instead of the
14204** one parameter that destructors normally want. So we have to introduce
14205** this magic value that the code knows to handle differently. Any
14206** pointer will work here as long as it is distinct from SQLITE_STATIC
14207** and SQLITE_TRANSIENT.
14208*/
14209#define SQLITE_DYNAMIC ((sqlite3_destructor_type)sqlite3OomFault)
14210
14211/*
14212** When SQLITE_OMIT_WSD is defined, it means that the target platform does
14213** not support Writable Static Data (WSD) such as global and static variables.
14214** All variables must either be on the stack or dynamically allocated from
14215** the heap. When WSD is unsupported, the variable declarations scattered
14216** throughout the SQLite code must become constants instead. The SQLITE_WSD
14217** macro is used for this purpose. And instead of referencing the variable
14218** directly, we use its constant as a key to lookup the run-time allocated
14219** buffer that holds real variable. The constant is also the initializer
14220** for the run-time allocated buffer.
14221**
14222** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL
14223** macros become no-ops and have zero performance impact.
14224*/
14225#ifdef SQLITE_OMIT_WSD
14226 #define SQLITE_WSD const
14227 #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
14228 #define sqlite3GlobalConfig GLOBAL(struct Sqlite3Config, sqlite3Config)
14229SQLITE_API int sqlite3_wsd_init(int N, int J);
14230SQLITE_API void *sqlite3_wsd_find(void *K, int L);
14231#else
14232 #define SQLITE_WSD
14233 #define GLOBAL(t,v) v
14234 #define sqlite3GlobalConfig sqlite3Config
14235#endif
14236
14237/*
14238** The following macros are used to suppress compiler warnings and to
14239** make it clear to human readers when a function parameter is deliberately
14240** left unused within the body of a function. This usually happens when
14241** a function is called via a function pointer. For example the
14242** implementation of an SQL aggregate step callback may not use the
14243** parameter indicating the number of arguments passed to the aggregate,
14244** if it knows that this is enforced elsewhere.
14245**
14246** When a function parameter is not used at all within the body of a function,
14247** it is generally named "NotUsed" or "NotUsed2" to make things even clearer.
14248** However, these macros may also be used to suppress warnings related to
14249** parameters that may or may not be used depending on compilation options.
14250** For example those parameters only used in assert() statements. In these
14251** cases the parameters are named as per the usual conventions.
14252*/
14253#define UNUSED_PARAMETER(x) (void)(x)
14254#define UNUSED_PARAMETER2(x,y) UNUSED_PARAMETER(x),UNUSED_PARAMETER(y)
14255
14256/*
14257** Forward references to structures
14258*/
14259typedef struct AggInfo AggInfo;
14260typedef struct AuthContext AuthContext;
14261typedef struct AutoincInfo AutoincInfo;
14262typedef struct Bitvec Bitvec;
14263typedef struct CollSeq CollSeq;
14264typedef struct Column Column;
14265typedef struct Cte Cte;
14266typedef struct CteUse CteUse;
14267typedef struct Db Db;
14268typedef struct DbFixer DbFixer;
14269typedef struct Schema Schema;
14270typedef struct Expr Expr;
14271typedef struct ExprList ExprList;
14272typedef struct FKey FKey;
14273typedef struct FuncDestructor FuncDestructor;
14274typedef struct FuncDef FuncDef;
14275typedef struct FuncDefHash FuncDefHash;
14276typedef struct IdList IdList;
14277typedef struct Index Index;
14278typedef struct IndexSample IndexSample;
14279typedef struct KeyClass KeyClass;
14280typedef struct KeyInfo KeyInfo;
14281typedef struct Lookaside Lookaside;
14282typedef struct LookasideSlot LookasideSlot;
14283typedef struct Module Module;
14284typedef struct NameContext NameContext;
14285typedef struct Parse Parse;
14286typedef struct ParseCleanup ParseCleanup;
14287typedef struct PreUpdate PreUpdate;
14288typedef struct PrintfArguments PrintfArguments;
14289typedef struct RenameToken RenameToken;
14290typedef struct Returning Returning;
14291typedef struct RowSet RowSet;
14292typedef struct Savepoint Savepoint;
14293typedef struct Select Select;
14294typedef struct SQLiteThread SQLiteThread;
14295typedef struct SelectDest SelectDest;
14296typedef struct SrcItem SrcItem;
14297typedef struct SrcList SrcList;
14298typedef struct sqlite3_str StrAccum; /* Internal alias for sqlite3_str */
14299typedef struct Table Table;
14300typedef struct TableLock TableLock;
14301typedef struct Token Token;
14302typedef struct TreeView TreeView;
14303typedef struct Trigger Trigger;
14304typedef struct TriggerPrg TriggerPrg;
14305typedef struct TriggerStep TriggerStep;
14306typedef struct UnpackedRecord UnpackedRecord;
14307typedef struct Upsert Upsert;
14308typedef struct VTable VTable;
14309typedef struct VtabCtx VtabCtx;
14310typedef struct Walker Walker;
14311typedef struct WhereInfo WhereInfo;
14312typedef struct Window Window;
14313typedef struct With With;
14314
14315
14316/*
14317** The bitmask datatype defined below is used for various optimizations.
14318**
14319** Changing this from a 64-bit to a 32-bit type limits the number of
14320** tables in a join to 32 instead of 64. But it also reduces the size
14321** of the library by 738 bytes on ix86.
14322*/
14323#ifdef SQLITE_BITMASK_TYPE
14324 typedef SQLITE_BITMASK_TYPE Bitmask;
14325#else
14326 typedef u64 Bitmask;
14327#endif
14328
14329/*
14330** The number of bits in a Bitmask. "BMS" means "BitMask Size".
14331*/
14332#define BMS ((int)(sizeof(Bitmask)*8))
14333
14334/*
14335** A bit in a Bitmask
14336*/
14337#define MASKBIT(n) (((Bitmask)1)<<(n))
14338#define MASKBIT64(n) (((u64)1)<<(n))
14339#define MASKBIT32(n) (((unsigned int)1)<<(n))
14340#define ALLBITS ((Bitmask)-1)
14341
14342/* A VList object records a mapping between parameters/variables/wildcards
14343** in the SQL statement (such as $abc, @pqr, or :xyz) and the integer
14344** variable number associated with that parameter. See the format description
14345** on the sqlite3VListAdd() routine for more information. A VList is really
14346** just an array of integers.
14347*/
14348typedef int VList;
14349
14350/*
14351** Defer sourcing vdbe.h and btree.h until after the "u8" and
14352** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
14353** pointer types (i.e. FuncDef) defined above.
14354*/
14355/************** Include pager.h in the middle of sqliteInt.h *****************/
14356/************** Begin file pager.h *******************************************/
14357/*
14358** 2001 September 15
14359**
14360** The author disclaims copyright to this source code. In place of
14361** a legal notice, here is a blessing:
14362**
14363** May you do good and not evil.
14364** May you find forgiveness for yourself and forgive others.
14365** May you share freely, never taking more than you give.
14366**
14367*************************************************************************
14368** This header file defines the interface that the sqlite page cache
14369** subsystem. The page cache subsystem reads and writes a file a page
14370** at a time and provides a journal for rollback.
14371*/
14372
14373#ifndef SQLITE_PAGER_H
14374#define SQLITE_PAGER_H
14375
14376/*
14377** Default maximum size for persistent journal files. A negative
14378** value means no limit. This value may be overridden using the
14379** sqlite3PagerJournalSizeLimit() API. See also "PRAGMA journal_size_limit".
14380*/
14381#ifndef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT
14382 #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
14383#endif
14384
14385/*
14386** The type used to represent a page number. The first page in a file
14387** is called page 1. 0 is used to represent "not a page".
14388*/
14389typedef u32 Pgno;
14390
14391/*
14392** Each open file is managed by a separate instance of the "Pager" structure.
14393*/
14394typedef struct Pager Pager;
14395
14396/*
14397** Handle type for pages.
14398*/
14399typedef struct PgHdr DbPage;
14400
14401/*
14402** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
14403** reserved for working around a windows/posix incompatibility). It is
14404** used in the journal to signify that the remainder of the journal file
14405** is devoted to storing a super-journal name - there are no more pages to
14406** roll back. See comments for function writeSuperJournal() in pager.c
14407** for details.
14408*/
14409#define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
14410
14411/*
14412** Allowed values for the flags parameter to sqlite3PagerOpen().
14413**
14414** NOTE: These values must match the corresponding BTREE_ values in btree.h.
14415*/
14416#define PAGER_OMIT_JOURNAL 0x0001 /* Do not use a rollback journal */
14417#define PAGER_MEMORY 0x0002 /* In-memory database */
14418
14419/*
14420** Valid values for the second argument to sqlite3PagerLockingMode().
14421*/
14422#define PAGER_LOCKINGMODE_QUERY -1
14423#define PAGER_LOCKINGMODE_NORMAL 0
14424#define PAGER_LOCKINGMODE_EXCLUSIVE 1
14425
14426/*
14427** Numeric constants that encode the journalmode.
14428**
14429** The numeric values encoded here (other than PAGER_JOURNALMODE_QUERY)
14430** are exposed in the API via the "PRAGMA journal_mode" command and
14431** therefore cannot be changed without a compatibility break.
14432*/
14433#define PAGER_JOURNALMODE_QUERY (-1) /* Query the value of journalmode */
14434#define PAGER_JOURNALMODE_DELETE 0 /* Commit by deleting journal file */
14435#define PAGER_JOURNALMODE_PERSIST 1 /* Commit by zeroing journal header */
14436#define PAGER_JOURNALMODE_OFF 2 /* Journal omitted. */
14437#define PAGER_JOURNALMODE_TRUNCATE 3 /* Commit by truncating journal */
14438#define PAGER_JOURNALMODE_MEMORY 4 /* In-memory journal file */
14439#define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */
14440
14441/*
14442** Flags that make up the mask passed to sqlite3PagerGet().
14443*/
14444#define PAGER_GET_NOCONTENT 0x01 /* Do not load data from disk */
14445#define PAGER_GET_READONLY 0x02 /* Read-only page is acceptable */
14446
14447/*
14448** Flags for sqlite3PagerSetFlags()
14449**
14450** Value constraints (enforced via assert()):
14451** PAGER_FULLFSYNC == SQLITE_FullFSync
14452** PAGER_CKPT_FULLFSYNC == SQLITE_CkptFullFSync
14453** PAGER_CACHE_SPILL == SQLITE_CacheSpill
14454*/
14455#define PAGER_SYNCHRONOUS_OFF 0x01 /* PRAGMA synchronous=OFF */
14456#define PAGER_SYNCHRONOUS_NORMAL 0x02 /* PRAGMA synchronous=NORMAL */
14457#define PAGER_SYNCHRONOUS_FULL 0x03 /* PRAGMA synchronous=FULL */
14458#define PAGER_SYNCHRONOUS_EXTRA 0x04 /* PRAGMA synchronous=EXTRA */
14459#define PAGER_SYNCHRONOUS_MASK 0x07 /* Mask for four values above */
14460#define PAGER_FULLFSYNC 0x08 /* PRAGMA fullfsync=ON */
14461#define PAGER_CKPT_FULLFSYNC 0x10 /* PRAGMA checkpoint_fullfsync=ON */
14462#define PAGER_CACHESPILL 0x20 /* PRAGMA cache_spill=ON */
14463#define PAGER_FLAGS_MASK 0x38 /* All above except SYNCHRONOUS */
14464
14465/*
14466** The remainder of this file contains the declarations of the functions
14467** that make up the Pager sub-system API. See source code comments for
14468** a detailed description of each routine.
14469*/
14470
14471/* Open and close a Pager connection. */
14472SQLITE_PRIVATE int sqlite3PagerOpen(
14473 sqlite3_vfs*,
14474 Pager **ppPager,
14475 const char*,
14476 int,
14477 int,
14478 int,
14479 void(*)(DbPage*)
14480);
14481SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3*);
14482SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
14483
14484/* Functions used to configure a Pager object. */
14485SQLITE_PRIVATE void sqlite3PagerSetBusyHandler(Pager*, int(*)(void *), void *);
14486SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
14487SQLITE_PRIVATE Pgno sqlite3PagerMaxPageCount(Pager*, Pgno);
14488SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
14489SQLITE_PRIVATE int sqlite3PagerSetSpillsize(Pager*, int);
14490SQLITE_PRIVATE void sqlite3PagerSetMmapLimit(Pager *, sqlite3_int64);
14491SQLITE_PRIVATE void sqlite3PagerShrink(Pager*);
14492SQLITE_PRIVATE void sqlite3PagerSetFlags(Pager*,unsigned);
14493SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
14494SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
14495SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*);
14496SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager*);
14497SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
14498SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager*);
14499SQLITE_PRIVATE int sqlite3PagerFlush(Pager*);
14500
14501/* Functions used to obtain and release page references. */
14502SQLITE_PRIVATE int sqlite3PagerGet(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
14503SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
14504SQLITE_PRIVATE void sqlite3PagerRef(DbPage*);
14505SQLITE_PRIVATE void sqlite3PagerUnref(DbPage*);
14506SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage*);
14507SQLITE_PRIVATE void sqlite3PagerUnrefPageOne(DbPage*);
14508
14509/* Operations on page references. */
14510SQLITE_PRIVATE int sqlite3PagerWrite(DbPage*);
14511SQLITE_PRIVATE void sqlite3PagerDontWrite(DbPage*);
14512SQLITE_PRIVATE int sqlite3PagerMovepage(Pager*,DbPage*,Pgno,int);
14513SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage*);
14514SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *);
14515SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *);
14516
14517/* Functions used to manage pager transactions and savepoints. */
14518SQLITE_PRIVATE void sqlite3PagerPagecount(Pager*, int*);
14519SQLITE_PRIVATE int sqlite3PagerBegin(Pager*, int exFlag, int);
14520SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager*,const char *zSuper, int);
14521SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager*);
14522SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zSuper);
14523SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*);
14524SQLITE_PRIVATE int sqlite3PagerRollback(Pager*);
14525SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
14526SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
14527SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager);
14528
14529#ifndef SQLITE_OMIT_WAL
14530SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, sqlite3*, int, int*, int*);
14531SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager);
14532SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager);
14533SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
14534SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager, sqlite3*);
14535# ifdef SQLITE_ENABLE_SNAPSHOT
14536SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager*, sqlite3_snapshot **ppSnapshot);
14537SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(Pager*, sqlite3_snapshot *pSnapshot);
14538SQLITE_PRIVATE int sqlite3PagerSnapshotRecover(Pager *pPager);
14539SQLITE_PRIVATE int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot);
14540SQLITE_PRIVATE void sqlite3PagerSnapshotUnlock(Pager *pPager);
14541# endif
14542#endif
14543
14544#if !defined(SQLITE_OMIT_WAL) && defined(SQLITE_ENABLE_SETLK_TIMEOUT)
14545SQLITE_PRIVATE int sqlite3PagerWalWriteLock(Pager*, int);
14546SQLITE_PRIVATE void sqlite3PagerWalDb(Pager*, sqlite3*);
14547#else
14548# define sqlite3PagerWalWriteLock(y,z) SQLITE_OK
14549# define sqlite3PagerWalDb(x,y)
14550#endif
14551
14552#ifdef SQLITE_DIRECT_OVERFLOW_READ
14553SQLITE_PRIVATE int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno);
14554#endif
14555
14556#ifdef SQLITE_ENABLE_ZIPVFS
14557SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager);
14558#endif
14559
14560/* Functions used to query pager state and configuration. */
14561SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*);
14562SQLITE_PRIVATE u32 sqlite3PagerDataVersion(Pager*);
14563#ifdef SQLITE_DEBUG
14564SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*);
14565#endif
14566SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*);
14567SQLITE_PRIVATE const char *sqlite3PagerFilename(const Pager*, int);
14568SQLITE_PRIVATE sqlite3_vfs *sqlite3PagerVfs(Pager*);
14569SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
14570SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager*);
14571SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
14572SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
14573SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
14574SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
14575SQLITE_PRIVATE void sqlite3PagerClearCache(Pager*);
14576SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *);
14577
14578/* Functions used to truncate the database file. */
14579SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
14580
14581SQLITE_PRIVATE void sqlite3PagerRekey(DbPage*, Pgno, u16);
14582
14583/* Functions to support testing and debugging. */
14584#if !defined(NDEBUG) || defined(SQLITE_TEST)
14585SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage*);
14586SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage*);
14587#endif
14588#ifdef SQLITE_TEST
14589SQLITE_PRIVATE int *sqlite3PagerStats(Pager*);
14590SQLITE_PRIVATE void sqlite3PagerRefdump(Pager*);
14591 void disable_simulated_io_errors(void);
14592 void enable_simulated_io_errors(void);
14593#else
14594# define disable_simulated_io_errors()
14595# define enable_simulated_io_errors()
14596#endif
14597
14598#endif /* SQLITE_PAGER_H */
14599
14600/************** End of pager.h ***********************************************/
14601/************** Continuing where we left off in sqliteInt.h ******************/
14602/************** Include btree.h in the middle of sqliteInt.h *****************/
14603/************** Begin file btree.h *******************************************/
14604/*
14605** 2001 September 15
14606**
14607** The author disclaims copyright to this source code. In place of
14608** a legal notice, here is a blessing:
14609**
14610** May you do good and not evil.
14611** May you find forgiveness for yourself and forgive others.
14612** May you share freely, never taking more than you give.
14613**
14614*************************************************************************
14615** This header file defines the interface that the sqlite B-Tree file
14616** subsystem. See comments in the source code for a detailed description
14617** of what each interface routine does.
14618*/
14619#ifndef SQLITE_BTREE_H
14620#define SQLITE_BTREE_H
14621
14622/* TODO: This definition is just included so other modules compile. It
14623** needs to be revisited.
14624*/
14625#define SQLITE_N_BTREE_META 16
14626
14627/*
14628** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
14629** it must be turned on for each database using "PRAGMA auto_vacuum = 1".
14630*/
14631#ifndef SQLITE_DEFAULT_AUTOVACUUM
14632 #define SQLITE_DEFAULT_AUTOVACUUM 0
14633#endif
14634
14635#define BTREE_AUTOVACUUM_NONE 0 /* Do not do auto-vacuum */
14636#define BTREE_AUTOVACUUM_FULL 1 /* Do full auto-vacuum */
14637#define BTREE_AUTOVACUUM_INCR 2 /* Incremental vacuum */
14638
14639/*
14640** Forward declarations of structure
14641*/
14642typedef struct Btree Btree;
14643typedef struct BtCursor BtCursor;
14644typedef struct BtShared BtShared;
14645typedef struct BtreePayload BtreePayload;
14646
14647
14648SQLITE_PRIVATE int sqlite3BtreeOpen(
14649 sqlite3_vfs *pVfs, /* VFS to use with this b-tree */
14650 const char *zFilename, /* Name of database file to open */
14651 sqlite3 *db, /* Associated database connection */
14652 Btree **ppBtree, /* Return open Btree* here */
14653 int flags, /* Flags */
14654 int vfsFlags /* Flags passed through to VFS open */
14655);
14656
14657/* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
14658** following values.
14659**
14660** NOTE: These values must match the corresponding PAGER_ values in
14661** pager.h.
14662*/
14663#define BTREE_OMIT_JOURNAL 1 /* Do not create or use a rollback journal */
14664#define BTREE_MEMORY 2 /* This is an in-memory DB */
14665#define BTREE_SINGLE 4 /* The file contains at most 1 b-tree */
14666#define BTREE_UNORDERED 8 /* Use of a hash implementation is OK */
14667
14668SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
14669SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
14670SQLITE_PRIVATE int sqlite3BtreeSetSpillSize(Btree*,int);
14671#if SQLITE_MAX_MMAP_SIZE>0
14672SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
14673#endif
14674SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(Btree*,unsigned);
14675SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
14676SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
14677SQLITE_PRIVATE Pgno sqlite3BtreeMaxPageCount(Btree*,Pgno);
14678SQLITE_PRIVATE Pgno sqlite3BtreeLastPage(Btree*);
14679SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree*,int);
14680SQLITE_PRIVATE int sqlite3BtreeGetRequestedReserve(Btree*);
14681SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p);
14682SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
14683SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
14684SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int,int*);
14685SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char*);
14686SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int);
14687SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
14688SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*,int,int);
14689SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int);
14690SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, Pgno*, int flags);
14691SQLITE_PRIVATE int sqlite3BtreeTxnState(Btree*);
14692SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*);
14693
14694SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
14695SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *pBtree);
14696#ifndef SQLITE_OMIT_SHARED_CACHE
14697SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
14698#endif
14699
14700/* Savepoints are named, nestable SQL transactions mostly implemented */
14701/* in vdbe.c and pager.c See https://sqlite.org/lang_savepoint.html */
14702SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *, int, int);
14703
14704/* "Checkpoint" only refers to WAL. See https://sqlite.org/wal.html#ckpt */
14705#ifndef SQLITE_OMIT_WAL
14706SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
14707#endif
14708
14709SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *);
14710SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *);
14711SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *, Btree *);
14712
14713SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *);
14714
14715/* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
14716** of the flags shown below.
14717**
14718** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set.
14719** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
14720** is stored in the leaves. (BTREE_INTKEY is used for SQL tables.) With
14721** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored
14722** anywhere - the key is the content. (BTREE_BLOBKEY is used for SQL
14723** indices.)
14724*/
14725#define BTREE_INTKEY 1 /* Table has only 64-bit signed integer keys */
14726#define BTREE_BLOBKEY 2 /* Table has keys only - no data */
14727
14728SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*);
14729SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, i64*);
14730SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor*);
14731SQLITE_PRIVATE int sqlite3BtreeTripAllCursors(Btree*, int, int);
14732
14733SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
14734SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
14735
14736SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p);
14737
14738/*
14739** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta
14740** should be one of the following values. The integer values are assigned
14741** to constants so that the offset of the corresponding field in an
14742** SQLite database header may be found using the following formula:
14743**
14744** offset = 36 + (idx * 4)
14745**
14746** For example, the free-page-count field is located at byte offset 36 of
14747** the database file header. The incr-vacuum-flag field is located at
14748** byte offset 64 (== 36+4*7).
14749**
14750** The BTREE_DATA_VERSION value is not really a value stored in the header.
14751** It is a read-only number computed by the pager. But we merge it with
14752** the header value access routines since its access pattern is the same.
14753** Call it a "virtual meta value".
14754*/
14755#define BTREE_FREE_PAGE_COUNT 0
14756#define BTREE_SCHEMA_VERSION 1
14757#define BTREE_FILE_FORMAT 2
14758#define BTREE_DEFAULT_CACHE_SIZE 3
14759#define BTREE_LARGEST_ROOT_PAGE 4
14760#define BTREE_TEXT_ENCODING 5
14761#define BTREE_USER_VERSION 6
14762#define BTREE_INCR_VACUUM 7
14763#define BTREE_APPLICATION_ID 8
14764#define BTREE_DATA_VERSION 15 /* A virtual meta-value */
14765
14766/*
14767** Kinds of hints that can be passed into the sqlite3BtreeCursorHint()
14768** interface.
14769**
14770** BTREE_HINT_RANGE (arguments: Expr*, Mem*)
14771**
14772** The first argument is an Expr* (which is guaranteed to be constant for
14773** the lifetime of the cursor) that defines constraints on which rows
14774** might be fetched with this cursor. The Expr* tree may contain
14775** TK_REGISTER nodes that refer to values stored in the array of registers
14776** passed as the second parameter. In other words, if Expr.op==TK_REGISTER
14777** then the value of the node is the value in Mem[pExpr.iTable]. Any
14778** TK_COLUMN node in the expression tree refers to the Expr.iColumn-th
14779** column of the b-tree of the cursor. The Expr tree will not contain
14780** any function calls nor subqueries nor references to b-trees other than
14781** the cursor being hinted.
14782**
14783** The design of the _RANGE hint is aid b-tree implementations that try
14784** to prefetch content from remote machines - to provide those
14785** implementations with limits on what needs to be prefetched and thereby
14786** reduce network bandwidth.
14787**
14788** Note that BTREE_HINT_FLAGS with BTREE_BULKLOAD is the only hint used by
14789** standard SQLite. The other hints are provided for extentions that use
14790** the SQLite parser and code generator but substitute their own storage
14791** engine.
14792*/
14793#define BTREE_HINT_RANGE 0 /* Range constraints on queries */
14794
14795/*
14796** Values that may be OR'd together to form the argument to the
14797** BTREE_HINT_FLAGS hint for sqlite3BtreeCursorHint():
14798**
14799** The BTREE_BULKLOAD flag is set on index cursors when the index is going
14800** to be filled with content that is already in sorted order.
14801**
14802** The BTREE_SEEK_EQ flag is set on cursors that will get OP_SeekGE or
14803** OP_SeekLE opcodes for a range search, but where the range of entries
14804** selected will all have the same key. In other words, the cursor will
14805** be used only for equality key searches.
14806**
14807*/
14808#define BTREE_BULKLOAD 0x00000001 /* Used to full index in sorted order */
14809#define BTREE_SEEK_EQ 0x00000002 /* EQ seeks only - no range seeks */
14810
14811/*
14812** Flags passed as the third argument to sqlite3BtreeCursor().
14813**
14814** For read-only cursors the wrFlag argument is always zero. For read-write
14815** cursors it may be set to either (BTREE_WRCSR|BTREE_FORDELETE) or just
14816** (BTREE_WRCSR). If the BTREE_FORDELETE bit is set, then the cursor will
14817** only be used by SQLite for the following:
14818**
14819** * to seek to and then delete specific entries, and/or
14820**
14821** * to read values that will be used to create keys that other
14822** BTREE_FORDELETE cursors will seek to and delete.
14823**
14824** The BTREE_FORDELETE flag is an optimization hint. It is not used by
14825** by this, the native b-tree engine of SQLite, but it is available to
14826** alternative storage engines that might be substituted in place of this
14827** b-tree system. For alternative storage engines in which a delete of
14828** the main table row automatically deletes corresponding index rows,
14829** the FORDELETE flag hint allows those alternative storage engines to
14830** skip a lot of work. Namely: FORDELETE cursors may treat all SEEK
14831** and DELETE operations as no-ops, and any READ operation against a
14832** FORDELETE cursor may return a null row: 0x01 0x00.
14833*/
14834#define BTREE_WRCSR 0x00000004 /* read-write cursor */
14835#define BTREE_FORDELETE 0x00000008 /* Cursor is for seek/delete only */
14836
14837SQLITE_PRIVATE int sqlite3BtreeCursor(
14838 Btree*, /* BTree containing table to open */
14839 Pgno iTable, /* Index of root page */
14840 int wrFlag, /* 1 for writing. 0 for read-only */
14841 struct KeyInfo*, /* First argument to compare function */
14842 BtCursor *pCursor /* Space to write cursor structure */
14843);
14844SQLITE_PRIVATE BtCursor *sqlite3BtreeFakeValidCursor(void);
14845SQLITE_PRIVATE int sqlite3BtreeCursorSize(void);
14846SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor*);
14847SQLITE_PRIVATE void sqlite3BtreeCursorHintFlags(BtCursor*, unsigned);
14848#ifdef SQLITE_ENABLE_CURSOR_HINTS
14849SQLITE_PRIVATE void sqlite3BtreeCursorHint(BtCursor*, int, ...);
14850#endif
14851
14852SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor*);
14853SQLITE_PRIVATE int sqlite3BtreeTableMoveto(
14854 BtCursor*,
14855 i64 intKey,
14856 int bias,
14857 int *pRes
14858);
14859SQLITE_PRIVATE int sqlite3BtreeIndexMoveto(
14860 BtCursor*,
14861 UnpackedRecord *pUnKey,
14862 int *pRes
14863);
14864SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*);
14865SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor*, int*);
14866SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*, u8 flags);
14867
14868/* Allowed flags for sqlite3BtreeDelete() and sqlite3BtreeInsert() */
14869#define BTREE_SAVEPOSITION 0x02 /* Leave cursor pointing at NEXT or PREV */
14870#define BTREE_AUXDELETE 0x04 /* not the primary delete operation */
14871#define BTREE_APPEND 0x08 /* Insert is likely an append */
14872#define BTREE_PREFORMAT 0x80 /* Inserted data is a preformated cell */
14873
14874/* An instance of the BtreePayload object describes the content of a single
14875** entry in either an index or table btree.
14876**
14877** Index btrees (used for indexes and also WITHOUT ROWID tables) contain
14878** an arbitrary key and no data. These btrees have pKey,nKey set to the
14879** key and the pData,nData,nZero fields are uninitialized. The aMem,nMem
14880** fields give an array of Mem objects that are a decomposition of the key.
14881** The nMem field might be zero, indicating that no decomposition is available.
14882**
14883** Table btrees (used for rowid tables) contain an integer rowid used as
14884** the key and passed in the nKey field. The pKey field is zero.
14885** pData,nData hold the content of the new entry. nZero extra zero bytes
14886** are appended to the end of the content when constructing the entry.
14887** The aMem,nMem fields are uninitialized for table btrees.
14888**
14889** Field usage summary:
14890**
14891** Table BTrees Index Btrees
14892**
14893** pKey always NULL encoded key
14894** nKey the ROWID length of pKey
14895** pData data not used
14896** aMem not used decomposed key value
14897** nMem not used entries in aMem
14898** nData length of pData not used
14899** nZero extra zeros after pData not used
14900**
14901** This object is used to pass information into sqlite3BtreeInsert(). The
14902** same information used to be passed as five separate parameters. But placing
14903** the information into this object helps to keep the interface more
14904** organized and understandable, and it also helps the resulting code to
14905** run a little faster by using fewer registers for parameter passing.
14906*/
14907struct BtreePayload {
14908 const void *pKey; /* Key content for indexes. NULL for tables */
14909 sqlite3_int64 nKey; /* Size of pKey for indexes. PRIMARY KEY for tabs */
14910 const void *pData; /* Data for tables. */
14911 sqlite3_value *aMem; /* First of nMem value in the unpacked pKey */
14912 u16 nMem; /* Number of aMem[] value. Might be zero */
14913 int nData; /* Size of pData. 0 if none. */
14914 int nZero; /* Extra zero data appended after pData,nData */
14915};
14916
14917SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
14918 int flags, int seekResult);
14919SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
14920SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
14921SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int flags);
14922SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
14923SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int flags);
14924SQLITE_PRIVATE i64 sqlite3BtreeIntegerKey(BtCursor*);
14925SQLITE_PRIVATE void sqlite3BtreeCursorPin(BtCursor*);
14926SQLITE_PRIVATE void sqlite3BtreeCursorUnpin(BtCursor*);
14927#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
14928SQLITE_PRIVATE i64 sqlite3BtreeOffset(BtCursor*);
14929#endif
14930SQLITE_PRIVATE int sqlite3BtreePayload(BtCursor*, u32 offset, u32 amt, void*);
14931SQLITE_PRIVATE const void *sqlite3BtreePayloadFetch(BtCursor*, u32 *pAmt);
14932SQLITE_PRIVATE u32 sqlite3BtreePayloadSize(BtCursor*);
14933SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeMaxRecordSize(BtCursor*);
14934
14935SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(sqlite3*,Btree*,Pgno*aRoot,int nRoot,int,int*);
14936SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
14937SQLITE_PRIVATE i64 sqlite3BtreeRowCountEst(BtCursor*);
14938
14939#ifndef SQLITE_OMIT_INCRBLOB
14940SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor*, u32 offset, u32 amt, void*);
14941SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
14942SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *);
14943#endif
14944SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
14945SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
14946SQLITE_PRIVATE int sqlite3BtreeCursorHasHint(BtCursor*, unsigned int mask);
14947SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *pBt);
14948SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void);
14949
14950#ifdef SQLITE_DEBUG
14951SQLITE_PRIVATE sqlite3_uint64 sqlite3BtreeSeekCount(Btree*);
14952#else
14953# define sqlite3BtreeSeekCount(X) 0
14954#endif
14955
14956#ifndef NDEBUG
14957SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
14958#endif
14959SQLITE_PRIVATE int sqlite3BtreeCursorIsValidNN(BtCursor*);
14960
14961SQLITE_PRIVATE int sqlite3BtreeCount(sqlite3*, BtCursor*, i64*);
14962
14963#ifdef SQLITE_TEST
14964SQLITE_PRIVATE int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
14965SQLITE_PRIVATE void sqlite3BtreeCursorList(Btree*);
14966#endif
14967
14968#ifndef SQLITE_OMIT_WAL
14969SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
14970#endif
14971
14972SQLITE_PRIVATE int sqlite3BtreeTransferRow(BtCursor*, BtCursor*, i64);
14973
14974/*
14975** If we are not using shared cache, then there is no need to
14976** use mutexes to access the BtShared structures. So make the
14977** Enter and Leave procedures no-ops.
14978*/
14979#ifndef SQLITE_OMIT_SHARED_CACHE
14980SQLITE_PRIVATE void sqlite3BtreeEnter(Btree*);
14981SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3*);
14982SQLITE_PRIVATE int sqlite3BtreeSharable(Btree*);
14983SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor*);
14984SQLITE_PRIVATE int sqlite3BtreeConnectionCount(Btree*);
14985#else
14986# define sqlite3BtreeEnter(X)
14987# define sqlite3BtreeEnterAll(X)
14988# define sqlite3BtreeSharable(X) 0
14989# define sqlite3BtreeEnterCursor(X)
14990# define sqlite3BtreeConnectionCount(X) 1
14991#endif
14992
14993#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
14994SQLITE_PRIVATE void sqlite3BtreeLeave(Btree*);
14995SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor*);
14996SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3*);
14997#ifndef NDEBUG
14998 /* These routines are used inside assert() statements only. */
14999SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree*);
15000SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3*);
15001SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*);
15002#endif
15003#else
15004
15005# define sqlite3BtreeLeave(X)
15006# define sqlite3BtreeLeaveCursor(X)
15007# define sqlite3BtreeLeaveAll(X)
15008
15009# define sqlite3BtreeHoldsMutex(X) 1
15010# define sqlite3BtreeHoldsAllMutexes(X) 1
15011# define sqlite3SchemaMutexHeld(X,Y,Z) 1
15012#endif
15013
15014
15015#endif /* SQLITE_BTREE_H */
15016
15017/************** End of btree.h ***********************************************/
15018/************** Continuing where we left off in sqliteInt.h ******************/
15019/************** Include vdbe.h in the middle of sqliteInt.h ******************/
15020/************** Begin file vdbe.h ********************************************/
15021/*
15022** 2001 September 15
15023**
15024** The author disclaims copyright to this source code. In place of
15025** a legal notice, here is a blessing:
15026**
15027** May you do good and not evil.
15028** May you find forgiveness for yourself and forgive others.
15029** May you share freely, never taking more than you give.
15030**
15031*************************************************************************
15032** Header file for the Virtual DataBase Engine (VDBE)
15033**
15034** This header defines the interface to the virtual database engine
15035** or VDBE. The VDBE implements an abstract machine that runs a
15036** simple program to access and modify the underlying database.
15037*/
15038#ifndef SQLITE_VDBE_H
15039#define SQLITE_VDBE_H
15040/* #include <stdio.h> */
15041
15042/*
15043** A single VDBE is an opaque structure named "Vdbe". Only routines
15044** in the source file sqliteVdbe.c are allowed to see the insides
15045** of this structure.
15046*/
15047typedef struct Vdbe Vdbe;
15048
15049/*
15050** The names of the following types declared in vdbeInt.h are required
15051** for the VdbeOp definition.
15052*/
15053typedef struct sqlite3_value Mem;
15054typedef struct SubProgram SubProgram;
15055
15056/*
15057** A single instruction of the virtual machine has an opcode
15058** and as many as three operands. The instruction is recorded
15059** as an instance of the following structure:
15060*/
15061struct VdbeOp {
15062 u8 opcode; /* What operation to perform */
15063 signed char p4type; /* One of the P4_xxx constants for p4 */
15064 u16 p5; /* Fifth parameter is an unsigned 16-bit integer */
15065 int p1; /* First operand */
15066 int p2; /* Second parameter (often the jump destination) */
15067 int p3; /* The third parameter */
15068 union p4union { /* fourth parameter */
15069 int i; /* Integer value if p4type==P4_INT32 */
15070 void *p; /* Generic pointer */
15071 char *z; /* Pointer to data for string (char array) types */
15072 i64 *pI64; /* Used when p4type is P4_INT64 */
15073 double *pReal; /* Used when p4type is P4_REAL */
15074 FuncDef *pFunc; /* Used when p4type is P4_FUNCDEF */
15075 sqlite3_context *pCtx; /* Used when p4type is P4_FUNCCTX */
15076 CollSeq *pColl; /* Used when p4type is P4_COLLSEQ */
15077 Mem *pMem; /* Used when p4type is P4_MEM */
15078 VTable *pVtab; /* Used when p4type is P4_VTAB */
15079 KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */
15080 u32 *ai; /* Used when p4type is P4_INTARRAY */
15081 SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */
15082 Table *pTab; /* Used when p4type is P4_TABLE */
15083#ifdef SQLITE_ENABLE_CURSOR_HINTS
15084 Expr *pExpr; /* Used when p4type is P4_EXPR */
15085#endif
15086 int (*xAdvance)(BtCursor *, int);
15087 } p4;
15088#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
15089 char *zComment; /* Comment to improve readability */
15090#endif
15091#ifdef VDBE_PROFILE
15092 u32 cnt; /* Number of times this instruction was executed */
15093 u64 cycles; /* Total time spent executing this instruction */
15094#endif
15095#ifdef SQLITE_VDBE_COVERAGE
15096 u32 iSrcLine; /* Source-code line that generated this opcode
15097 ** with flags in the upper 8 bits */
15098#endif
15099};
15100typedef struct VdbeOp VdbeOp;
15101
15102
15103/*
15104** A sub-routine used to implement a trigger program.
15105*/
15106struct SubProgram {
15107 VdbeOp *aOp; /* Array of opcodes for sub-program */
15108 int nOp; /* Elements in aOp[] */
15109 int nMem; /* Number of memory cells required */
15110 int nCsr; /* Number of cursors required */
15111 u8 *aOnce; /* Array of OP_Once flags */
15112 void *token; /* id that may be used to recursive triggers */
15113 SubProgram *pNext; /* Next sub-program already visited */
15114};
15115
15116/*
15117** A smaller version of VdbeOp used for the VdbeAddOpList() function because
15118** it takes up less space.
15119*/
15120struct VdbeOpList {
15121 u8 opcode; /* What operation to perform */
15122 signed char p1; /* First operand */
15123 signed char p2; /* Second parameter (often the jump destination) */
15124 signed char p3; /* Third parameter */
15125};
15126typedef struct VdbeOpList VdbeOpList;
15127
15128/*
15129** Allowed values of VdbeOp.p4type
15130*/
15131#define P4_NOTUSED 0 /* The P4 parameter is not used */
15132#define P4_TRANSIENT 0 /* P4 is a pointer to a transient string */
15133#define P4_STATIC (-1) /* Pointer to a static string */
15134#define P4_COLLSEQ (-2) /* P4 is a pointer to a CollSeq structure */
15135#define P4_INT32 (-3) /* P4 is a 32-bit signed integer */
15136#define P4_SUBPROGRAM (-4) /* P4 is a pointer to a SubProgram structure */
15137#define P4_ADVANCE (-5) /* P4 is a pointer to BtreeNext() or BtreePrev() */
15138#define P4_TABLE (-6) /* P4 is a pointer to a Table structure */
15139/* Above do not own any resources. Must free those below */
15140#define P4_FREE_IF_LE (-7)
15141#define P4_DYNAMIC (-7) /* Pointer to memory from sqliteMalloc() */
15142#define P4_FUNCDEF (-8) /* P4 is a pointer to a FuncDef structure */
15143#define P4_KEYINFO (-9) /* P4 is a pointer to a KeyInfo structure */
15144#define P4_EXPR (-10) /* P4 is a pointer to an Expr tree */
15145#define P4_MEM (-11) /* P4 is a pointer to a Mem* structure */
15146#define P4_VTAB (-12) /* P4 is a pointer to an sqlite3_vtab structure */
15147#define P4_REAL (-13) /* P4 is a 64-bit floating point value */
15148#define P4_INT64 (-14) /* P4 is a 64-bit signed integer */
15149#define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
15150#define P4_FUNCCTX (-16) /* P4 is a pointer to an sqlite3_context object */
15151#define P4_DYNBLOB (-17) /* Pointer to memory from sqliteMalloc() */
15152
15153/* Error message codes for OP_Halt */
15154#define P5_ConstraintNotNull 1
15155#define P5_ConstraintUnique 2
15156#define P5_ConstraintCheck 3
15157#define P5_ConstraintFK 4
15158
15159/*
15160** The Vdbe.aColName array contains 5n Mem structures, where n is the
15161** number of columns of data returned by the statement.
15162*/
15163#define COLNAME_NAME 0
15164#define COLNAME_DECLTYPE 1
15165#define COLNAME_DATABASE 2
15166#define COLNAME_TABLE 3
15167#define COLNAME_COLUMN 4
15168#ifdef SQLITE_ENABLE_COLUMN_METADATA
15169# define COLNAME_N 5 /* Number of COLNAME_xxx symbols */
15170#else
15171# ifdef SQLITE_OMIT_DECLTYPE
15172# define COLNAME_N 1 /* Store only the name */
15173# else
15174# define COLNAME_N 2 /* Store the name and decltype */
15175# endif
15176#endif
15177
15178/*
15179** The following macro converts a label returned by sqlite3VdbeMakeLabel()
15180** into an index into the Parse.aLabel[] array that contains the resolved
15181** address of that label.
15182*/
15183#define ADDR(X) (~(X))
15184
15185/*
15186** The makefile scans the vdbe.c source file and creates the "opcodes.h"
15187** header file that defines a number for each opcode used by the VDBE.
15188*/
15189/************** Include opcodes.h in the middle of vdbe.h ********************/
15190/************** Begin file opcodes.h *****************************************/
15191/* Automatically generated. Do not edit */
15192/* See the tool/mkopcodeh.tcl script for details */
15193#define OP_Savepoint 0
15194#define OP_AutoCommit 1
15195#define OP_Transaction 2
15196#define OP_SorterNext 3 /* jump */
15197#define OP_Prev 4 /* jump */
15198#define OP_Next 5 /* jump */
15199#define OP_Checkpoint 6
15200#define OP_JournalMode 7
15201#define OP_Vacuum 8
15202#define OP_VFilter 9 /* jump, synopsis: iplan=r[P3] zplan='P4' */
15203#define OP_VUpdate 10 /* synopsis: data=r[P3@P2] */
15204#define OP_Goto 11 /* jump */
15205#define OP_Gosub 12 /* jump */
15206#define OP_InitCoroutine 13 /* jump */
15207#define OP_Yield 14 /* jump */
15208#define OP_MustBeInt 15 /* jump */
15209#define OP_Jump 16 /* jump */
15210#define OP_Once 17 /* jump */
15211#define OP_If 18 /* jump */
15212#define OP_Not 19 /* same as TK_NOT, synopsis: r[P2]= !r[P1] */
15213#define OP_IfNot 20 /* jump */
15214#define OP_IsNullOrType 21 /* jump, synopsis: if typeof(r[P1]) IN (P3,5) goto P2 */
15215#define OP_IfNullRow 22 /* jump, synopsis: if P1.nullRow then r[P3]=NULL, goto P2 */
15216#define OP_SeekLT 23 /* jump, synopsis: key=r[P3@P4] */
15217#define OP_SeekLE 24 /* jump, synopsis: key=r[P3@P4] */
15218#define OP_SeekGE 25 /* jump, synopsis: key=r[P3@P4] */
15219#define OP_SeekGT 26 /* jump, synopsis: key=r[P3@P4] */
15220#define OP_IfNotOpen 27 /* jump, synopsis: if( !csr[P1] ) goto P2 */
15221#define OP_IfNoHope 28 /* jump, synopsis: key=r[P3@P4] */
15222#define OP_NoConflict 29 /* jump, synopsis: key=r[P3@P4] */
15223#define OP_NotFound 30 /* jump, synopsis: key=r[P3@P4] */
15224#define OP_Found 31 /* jump, synopsis: key=r[P3@P4] */
15225#define OP_SeekRowid 32 /* jump, synopsis: intkey=r[P3] */
15226#define OP_NotExists 33 /* jump, synopsis: intkey=r[P3] */
15227#define OP_Last 34 /* jump */
15228#define OP_IfSmaller 35 /* jump */
15229#define OP_SorterSort 36 /* jump */
15230#define OP_Sort 37 /* jump */
15231#define OP_Rewind 38 /* jump */
15232#define OP_IdxLE 39 /* jump, synopsis: key=r[P3@P4] */
15233#define OP_IdxGT 40 /* jump, synopsis: key=r[P3@P4] */
15234#define OP_IdxLT 41 /* jump, synopsis: key=r[P3@P4] */
15235#define OP_IdxGE 42 /* jump, synopsis: key=r[P3@P4] */
15236#define OP_Or 43 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
15237#define OP_And 44 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
15238#define OP_RowSetRead 45 /* jump, synopsis: r[P3]=rowset(P1) */
15239#define OP_RowSetTest 46 /* jump, synopsis: if r[P3] in rowset(P1) goto P2 */
15240#define OP_Program 47 /* jump */
15241#define OP_FkIfZero 48 /* jump, synopsis: if fkctr[P1]==0 goto P2 */
15242#define OP_IfPos 49 /* jump, synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
15243#define OP_IsNull 50 /* jump, same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
15244#define OP_NotNull 51 /* jump, same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
15245#define OP_Ne 52 /* jump, same as TK_NE, synopsis: IF r[P3]!=r[P1] */
15246#define OP_Eq 53 /* jump, same as TK_EQ, synopsis: IF r[P3]==r[P1] */
15247#define OP_Gt 54 /* jump, same as TK_GT, synopsis: IF r[P3]>r[P1] */
15248#define OP_Le 55 /* jump, same as TK_LE, synopsis: IF r[P3]<=r[P1] */
15249#define OP_Lt 56 /* jump, same as TK_LT, synopsis: IF r[P3]<r[P1] */
15250#define OP_Ge 57 /* jump, same as TK_GE, synopsis: IF r[P3]>=r[P1] */
15251#define OP_ElseEq 58 /* jump, same as TK_ESCAPE */
15252#define OP_IfNotZero 59 /* jump, synopsis: if r[P1]!=0 then r[P1]--, goto P2 */
15253#define OP_DecrJumpZero 60 /* jump, synopsis: if (--r[P1])==0 goto P2 */
15254#define OP_IncrVacuum 61 /* jump */
15255#define OP_VNext 62 /* jump */
15256#define OP_Init 63 /* jump, synopsis: Start at P2 */
15257#define OP_PureFunc 64 /* synopsis: r[P3]=func(r[P2@NP]) */
15258#define OP_Function 65 /* synopsis: r[P3]=func(r[P2@NP]) */
15259#define OP_Return 66
15260#define OP_EndCoroutine 67
15261#define OP_HaltIfNull 68 /* synopsis: if r[P3]=null halt */
15262#define OP_Halt 69
15263#define OP_Integer 70 /* synopsis: r[P2]=P1 */
15264#define OP_Int64 71 /* synopsis: r[P2]=P4 */
15265#define OP_String 72 /* synopsis: r[P2]='P4' (len=P1) */
15266#define OP_Null 73 /* synopsis: r[P2..P3]=NULL */
15267#define OP_SoftNull 74 /* synopsis: r[P1]=NULL */
15268#define OP_Blob 75 /* synopsis: r[P2]=P4 (len=P1) */
15269#define OP_Variable 76 /* synopsis: r[P2]=parameter(P1,P4) */
15270#define OP_Move 77 /* synopsis: r[P2@P3]=r[P1@P3] */
15271#define OP_Copy 78 /* synopsis: r[P2@P3+1]=r[P1@P3+1] */
15272#define OP_SCopy 79 /* synopsis: r[P2]=r[P1] */
15273#define OP_IntCopy 80 /* synopsis: r[P2]=r[P1] */
15274#define OP_ChngCntRow 81 /* synopsis: output=r[P1] */
15275#define OP_ResultRow 82 /* synopsis: output=r[P1@P2] */
15276#define OP_CollSeq 83
15277#define OP_AddImm 84 /* synopsis: r[P1]=r[P1]+P2 */
15278#define OP_RealAffinity 85
15279#define OP_Cast 86 /* synopsis: affinity(r[P1]) */
15280#define OP_Permutation 87
15281#define OP_Compare 88 /* synopsis: r[P1@P3] <-> r[P2@P3] */
15282#define OP_IsTrue 89 /* synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 */
15283#define OP_ZeroOrNull 90 /* synopsis: r[P2] = 0 OR NULL */
15284#define OP_Offset 91 /* synopsis: r[P3] = sqlite_offset(P1) */
15285#define OP_Column 92 /* synopsis: r[P3]=PX */
15286#define OP_TypeCheck 93 /* synopsis: typecheck(r[P1@P2]) */
15287#define OP_Affinity 94 /* synopsis: affinity(r[P1@P2]) */
15288#define OP_MakeRecord 95 /* synopsis: r[P3]=mkrec(r[P1@P2]) */
15289#define OP_Count 96 /* synopsis: r[P2]=count() */
15290#define OP_ReadCookie 97
15291#define OP_SetCookie 98
15292#define OP_ReopenIdx 99 /* synopsis: root=P2 iDb=P3 */
15293#define OP_OpenRead 100 /* synopsis: root=P2 iDb=P3 */
15294#define OP_OpenWrite 101 /* synopsis: root=P2 iDb=P3 */
15295#define OP_BitAnd 102 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
15296#define OP_BitOr 103 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
15297#define OP_ShiftLeft 104 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
15298#define OP_ShiftRight 105 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
15299#define OP_Add 106 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
15300#define OP_Subtract 107 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
15301#define OP_Multiply 108 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
15302#define OP_Divide 109 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
15303#define OP_Remainder 110 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
15304#define OP_Concat 111 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
15305#define OP_OpenDup 112
15306#define OP_BitNot 113 /* same as TK_BITNOT, synopsis: r[P2]= ~r[P1] */
15307#define OP_OpenAutoindex 114 /* synopsis: nColumn=P2 */
15308#define OP_OpenEphemeral 115 /* synopsis: nColumn=P2 */
15309#define OP_String8 116 /* same as TK_STRING, synopsis: r[P2]='P4' */
15310#define OP_SorterOpen 117
15311#define OP_SequenceTest 118 /* synopsis: if( cursor[P1].ctr++ ) pc = P2 */
15312#define OP_OpenPseudo 119 /* synopsis: P3 columns in r[P2] */
15313#define OP_Close 120
15314#define OP_ColumnsUsed 121
15315#define OP_SeekScan 122 /* synopsis: Scan-ahead up to P1 rows */
15316#define OP_SeekHit 123 /* synopsis: set P2<=seekHit<=P3 */
15317#define OP_Sequence 124 /* synopsis: r[P2]=cursor[P1].ctr++ */
15318#define OP_NewRowid 125 /* synopsis: r[P2]=rowid */
15319#define OP_Insert 126 /* synopsis: intkey=r[P3] data=r[P2] */
15320#define OP_RowCell 127
15321#define OP_Delete 128
15322#define OP_ResetCount 129
15323#define OP_SorterCompare 130 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
15324#define OP_SorterData 131 /* synopsis: r[P2]=data */
15325#define OP_RowData 132 /* synopsis: r[P2]=data */
15326#define OP_Rowid 133 /* synopsis: r[P2]=rowid */
15327#define OP_NullRow 134
15328#define OP_SeekEnd 135
15329#define OP_IdxInsert 136 /* synopsis: key=r[P2] */
15330#define OP_SorterInsert 137 /* synopsis: key=r[P2] */
15331#define OP_IdxDelete 138 /* synopsis: key=r[P2@P3] */
15332#define OP_DeferredSeek 139 /* synopsis: Move P3 to P1.rowid if needed */
15333#define OP_IdxRowid 140 /* synopsis: r[P2]=rowid */
15334#define OP_FinishSeek 141
15335#define OP_Destroy 142
15336#define OP_Clear 143
15337#define OP_ResetSorter 144
15338#define OP_CreateBtree 145 /* synopsis: r[P2]=root iDb=P1 flags=P3 */
15339#define OP_SqlExec 146
15340#define OP_ParseSchema 147
15341#define OP_LoadAnalysis 148
15342#define OP_DropTable 149
15343#define OP_DropIndex 150
15344#define OP_DropTrigger 151
15345#define OP_Real 152 /* same as TK_FLOAT, synopsis: r[P2]=P4 */
15346#define OP_IntegrityCk 153
15347#define OP_RowSetAdd 154 /* synopsis: rowset(P1)=r[P2] */
15348#define OP_Param 155
15349#define OP_FkCounter 156 /* synopsis: fkctr[P1]+=P2 */
15350#define OP_MemMax 157 /* synopsis: r[P1]=max(r[P1],r[P2]) */
15351#define OP_OffsetLimit 158 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
15352#define OP_AggInverse 159 /* synopsis: accum=r[P3] inverse(r[P2@P5]) */
15353#define OP_AggStep 160 /* synopsis: accum=r[P3] step(r[P2@P5]) */
15354#define OP_AggStep1 161 /* synopsis: accum=r[P3] step(r[P2@P5]) */
15355#define OP_AggValue 162 /* synopsis: r[P3]=value N=P2 */
15356#define OP_AggFinal 163 /* synopsis: accum=r[P1] N=P2 */
15357#define OP_Expire 164
15358#define OP_CursorLock 165
15359#define OP_CursorUnlock 166
15360#define OP_TableLock 167 /* synopsis: iDb=P1 root=P2 write=P3 */
15361#define OP_VBegin 168
15362#define OP_VCreate 169
15363#define OP_VDestroy 170
15364#define OP_VOpen 171
15365#define OP_VColumn 172 /* synopsis: r[P3]=vcolumn(P2) */
15366#define OP_VRename 173
15367#define OP_Pagecount 174
15368#define OP_MaxPgcnt 175
15369#define OP_Trace 176
15370#define OP_CursorHint 177
15371#define OP_ReleaseReg 178 /* synopsis: release r[P1@P2] mask P3 */
15372#define OP_Noop 179
15373#define OP_Explain 180
15374#define OP_Abortable 181
15375
15376/* Properties such as "out2" or "jump" that are specified in
15377** comments following the "case" for each opcode in the vdbe.c
15378** are encoded into bitvectors as follows:
15379*/
15380#define OPFLG_JUMP 0x01 /* jump: P2 holds jmp target */
15381#define OPFLG_IN1 0x02 /* in1: P1 is an input */
15382#define OPFLG_IN2 0x04 /* in2: P2 is an input */
15383#define OPFLG_IN3 0x08 /* in3: P3 is an input */
15384#define OPFLG_OUT2 0x10 /* out2: P2 is an output */
15385#define OPFLG_OUT3 0x20 /* out3: P3 is an output */
15386#define OPFLG_INITIALIZER {\
15387/* 0 */ 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x10,\
15388/* 8 */ 0x00, 0x01, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03,\
15389/* 16 */ 0x01, 0x01, 0x03, 0x12, 0x03, 0x03, 0x01, 0x09,\
15390/* 24 */ 0x09, 0x09, 0x09, 0x01, 0x09, 0x09, 0x09, 0x09,\
15391/* 32 */ 0x09, 0x09, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,\
15392/* 40 */ 0x01, 0x01, 0x01, 0x26, 0x26, 0x23, 0x0b, 0x01,\
15393/* 48 */ 0x01, 0x03, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
15394/* 56 */ 0x0b, 0x0b, 0x01, 0x03, 0x03, 0x01, 0x01, 0x01,\
15395/* 64 */ 0x00, 0x00, 0x02, 0x02, 0x08, 0x00, 0x10, 0x10,\
15396/* 72 */ 0x10, 0x10, 0x00, 0x10, 0x10, 0x00, 0x00, 0x10,\
15397/* 80 */ 0x10, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x00,\
15398/* 88 */ 0x00, 0x12, 0x1e, 0x20, 0x00, 0x00, 0x00, 0x00,\
15399/* 96 */ 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x26, 0x26,\
15400/* 104 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26,\
15401/* 112 */ 0x00, 0x12, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,\
15402/* 120 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
15403/* 128 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00,\
15404/* 136 */ 0x04, 0x04, 0x00, 0x00, 0x10, 0x00, 0x10, 0x00,\
15405/* 144 */ 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
15406/* 152 */ 0x10, 0x00, 0x06, 0x10, 0x00, 0x04, 0x1a, 0x00,\
15407/* 160 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
15408/* 168 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10,\
15409/* 176 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,}
15410
15411/* The resolve3P2Values() routine is able to run faster if it knows
15412** the value of the largest JUMP opcode. The smaller the maximum
15413** JUMP opcode the better, so the mkopcodeh.tcl script that
15414** generated this include file strives to group all JUMP opcodes
15415** together near the beginning of the list.
15416*/
15417#define SQLITE_MX_JUMP_OPCODE 63 /* Maximum JUMP opcode */
15418
15419/************** End of opcodes.h *********************************************/
15420/************** Continuing where we left off in vdbe.h ***********************/
15421
15422/*
15423** Additional non-public SQLITE_PREPARE_* flags
15424*/
15425#define SQLITE_PREPARE_SAVESQL 0x80 /* Preserve SQL text */
15426#define SQLITE_PREPARE_MASK 0x0f /* Mask of public flags */
15427
15428/*
15429** Prototypes for the VDBE interface. See comments on the implementation
15430** for a description of what each of these routines does.
15431*/
15432SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse*);
15433SQLITE_PRIVATE Parse *sqlite3VdbeParser(Vdbe*);
15434SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int);
15435SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int);
15436SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
15437SQLITE_PRIVATE int sqlite3VdbeGoto(Vdbe*,int);
15438SQLITE_PRIVATE int sqlite3VdbeLoadString(Vdbe*,int,const char*);
15439SQLITE_PRIVATE void sqlite3VdbeMultiLoad(Vdbe*,int,const char*,...);
15440SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
15441SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
15442SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(Vdbe*,int,int,int,int,const u8*,int);
15443SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
15444SQLITE_PRIVATE int sqlite3VdbeAddFunctionCall(Parse*,int,int,int,int,const FuncDef*,int);
15445SQLITE_PRIVATE void sqlite3VdbeEndCoroutine(Vdbe*,int);
15446#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
15447SQLITE_PRIVATE void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N);
15448SQLITE_PRIVATE void sqlite3VdbeVerifyNoResultRow(Vdbe *p);
15449#else
15450# define sqlite3VdbeVerifyNoMallocRequired(A,B)
15451# define sqlite3VdbeVerifyNoResultRow(A)
15452#endif
15453#if defined(SQLITE_DEBUG)
15454SQLITE_PRIVATE void sqlite3VdbeVerifyAbortable(Vdbe *p, int);
15455#else
15456# define sqlite3VdbeVerifyAbortable(A,B)
15457#endif
15458SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp,int iLineno);
15459#ifndef SQLITE_OMIT_EXPLAIN
15460SQLITE_PRIVATE void sqlite3VdbeExplain(Parse*,u8,const char*,...);
15461SQLITE_PRIVATE void sqlite3VdbeExplainPop(Parse*);
15462SQLITE_PRIVATE int sqlite3VdbeExplainParent(Parse*);
15463# define ExplainQueryPlan(P) sqlite3VdbeExplain P
15464# define ExplainQueryPlanPop(P) sqlite3VdbeExplainPop(P)
15465# define ExplainQueryPlanParent(P) sqlite3VdbeExplainParent(P)
15466#else
15467# define ExplainQueryPlan(P)
15468# define ExplainQueryPlanPop(P)
15469# define ExplainQueryPlanParent(P) 0
15470# define sqlite3ExplainBreakpoint(A,B) /*no-op*/
15471#endif
15472#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_EXPLAIN)
15473SQLITE_PRIVATE void sqlite3ExplainBreakpoint(const char*,const char*);
15474#else
15475# define sqlite3ExplainBreakpoint(A,B) /*no-op*/
15476#endif
15477SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*, int, char*, u16);
15478SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe*, int addr, u8);
15479SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, int addr, int P1);
15480SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2);
15481SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, int addr, int P3);
15482SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u16 P5);
15483SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
15484SQLITE_PRIVATE void sqlite3VdbeJumpHereOrPopInst(Vdbe*, int addr);
15485SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe*, int addr);
15486SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
15487#ifdef SQLITE_DEBUG
15488SQLITE_PRIVATE void sqlite3VdbeReleaseRegisters(Parse*,int addr, int n, u32 mask, int);
15489#else
15490# define sqlite3VdbeReleaseRegisters(P,A,N,M,F)
15491#endif
15492SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
15493SQLITE_PRIVATE void sqlite3VdbeAppendP4(Vdbe*, void *pP4, int p4type);
15494SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse*, Index*);
15495SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
15496SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
15497SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Parse*);
15498SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*);
15499SQLITE_PRIVATE void sqlite3VdbeReusable(Vdbe*);
15500SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*);
15501SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3*,Vdbe*);
15502SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,Parse*);
15503SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe*);
15504SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe*, int);
15505SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe*);
15506#ifdef SQLITE_DEBUG
15507SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *, int);
15508#endif
15509SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe*);
15510SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe*);
15511SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe*);
15512SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe*,int);
15513SQLITE_PRIVATE int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
15514SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe*);
15515SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe*);
15516SQLITE_PRIVATE u8 sqlite3VdbePrepareFlags(Vdbe*);
15517SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, u8);
15518#ifdef SQLITE_ENABLE_NORMALIZE
15519SQLITE_PRIVATE void sqlite3VdbeAddDblquoteStr(sqlite3*,Vdbe*,const char*);
15520SQLITE_PRIVATE int sqlite3VdbeUsesDoubleQuotedString(Vdbe*,const char*);
15521#endif
15522SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe*,Vdbe*);
15523SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
15524SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe*, int, u8);
15525SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int);
15526#ifndef SQLITE_OMIT_TRACE
15527SQLITE_PRIVATE char *sqlite3VdbeExpandSql(Vdbe*, const char*);
15528#endif
15529SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
15530SQLITE_PRIVATE int sqlite3BlobCompare(const Mem*, const Mem*);
15531
15532SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
15533SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
15534SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(int, const void *, UnpackedRecord *, int);
15535SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo*);
15536
15537typedef int (*RecordCompare)(int,const void*,UnpackedRecord*);
15538SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*);
15539
15540SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
15541SQLITE_PRIVATE int sqlite3VdbeHasSubProgram(Vdbe*);
15542
15543SQLITE_PRIVATE int sqlite3NotPureFunc(sqlite3_context*);
15544#ifdef SQLITE_ENABLE_BYTECODE_VTAB
15545SQLITE_PRIVATE int sqlite3VdbeBytecodeVtabInit(sqlite3*);
15546#endif
15547
15548/* Use SQLITE_ENABLE_COMMENTS to enable generation of extra comments on
15549** each VDBE opcode.
15550**
15551** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op
15552** comments in VDBE programs that show key decision points in the code
15553** generator.
15554*/
15555#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
15556SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe*, const char*, ...);
15557# define VdbeComment(X) sqlite3VdbeComment X
15558SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
15559# define VdbeNoopComment(X) sqlite3VdbeNoopComment X
15560# ifdef SQLITE_ENABLE_MODULE_COMMENTS
15561# define VdbeModuleComment(X) sqlite3VdbeNoopComment X
15562# else
15563# define VdbeModuleComment(X)
15564# endif
15565#else
15566# define VdbeComment(X)
15567# define VdbeNoopComment(X)
15568# define VdbeModuleComment(X)
15569#endif
15570
15571/*
15572** The VdbeCoverage macros are used to set a coverage testing point
15573** for VDBE branch instructions. The coverage testing points are line
15574** numbers in the sqlite3.c source file. VDBE branch coverage testing
15575** only works with an amalagmation build. That's ok since a VDBE branch
15576** coverage build designed for testing the test suite only. No application
15577** should ever ship with VDBE branch coverage measuring turned on.
15578**
15579** VdbeCoverage(v) // Mark the previously coded instruction
15580** // as a branch
15581**
15582** VdbeCoverageIf(v, conditional) // Mark previous if conditional true
15583**
15584** VdbeCoverageAlwaysTaken(v) // Previous branch is always taken
15585**
15586** VdbeCoverageNeverTaken(v) // Previous branch is never taken
15587**
15588** VdbeCoverageNeverNull(v) // Previous three-way branch is only
15589** // taken on the first two ways. The
15590** // NULL option is not possible
15591**
15592** VdbeCoverageEqNe(v) // Previous OP_Jump is only interested
15593** // in distingishing equal and not-equal.
15594**
15595** Every VDBE branch operation must be tagged with one of the macros above.
15596** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
15597** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch()
15598** routine in vdbe.c, alerting the developer to the missed tag.
15599**
15600** During testing, the test application will invoke
15601** sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE,...) to set a callback
15602** routine that is invoked as each bytecode branch is taken. The callback
15603** contains the sqlite3.c source line number ov the VdbeCoverage macro and
15604** flags to indicate whether or not the branch was taken. The test application
15605** is responsible for keeping track of this and reporting byte-code branches
15606** that are never taken.
15607**
15608** See the VdbeBranchTaken() macro and vdbeTakeBranch() function in the
15609** vdbe.c source file for additional information.
15610*/
15611#ifdef SQLITE_VDBE_COVERAGE
15612SQLITE_PRIVATE void sqlite3VdbeSetLineNumber(Vdbe*,int);
15613# define VdbeCoverage(v) sqlite3VdbeSetLineNumber(v,__LINE__)
15614# define VdbeCoverageIf(v,x) if(x)sqlite3VdbeSetLineNumber(v,__LINE__)
15615# define VdbeCoverageAlwaysTaken(v) \
15616 sqlite3VdbeSetLineNumber(v,__LINE__|0x5000000);
15617# define VdbeCoverageNeverTaken(v) \
15618 sqlite3VdbeSetLineNumber(v,__LINE__|0x6000000);
15619# define VdbeCoverageNeverNull(v) \
15620 sqlite3VdbeSetLineNumber(v,__LINE__|0x4000000);
15621# define VdbeCoverageNeverNullIf(v,x) \
15622 if(x)sqlite3VdbeSetLineNumber(v,__LINE__|0x4000000);
15623# define VdbeCoverageEqNe(v) \
15624 sqlite3VdbeSetLineNumber(v,__LINE__|0x8000000);
15625# define VDBE_OFFSET_LINENO(x) (__LINE__+x)
15626#else
15627# define VdbeCoverage(v)
15628# define VdbeCoverageIf(v,x)
15629# define VdbeCoverageAlwaysTaken(v)
15630# define VdbeCoverageNeverTaken(v)
15631# define VdbeCoverageNeverNull(v)
15632# define VdbeCoverageNeverNullIf(v,x)
15633# define VdbeCoverageEqNe(v)
15634# define VDBE_OFFSET_LINENO(x) 0
15635#endif
15636
15637#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
15638SQLITE_PRIVATE void sqlite3VdbeScanStatus(Vdbe*, int, int, int, LogEst, const char*);
15639#else
15640# define sqlite3VdbeScanStatus(a,b,c,d,e)
15641#endif
15642
15643#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
15644SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, VdbeOp*);
15645#endif
15646
15647#endif /* SQLITE_VDBE_H */
15648
15649/************** End of vdbe.h ************************************************/
15650/************** Continuing where we left off in sqliteInt.h ******************/
15651/************** Include pcache.h in the middle of sqliteInt.h ****************/
15652/************** Begin file pcache.h ******************************************/
15653/*
15654** 2008 August 05
15655**
15656** The author disclaims copyright to this source code. In place of
15657** a legal notice, here is a blessing:
15658**
15659** May you do good and not evil.
15660** May you find forgiveness for yourself and forgive others.
15661** May you share freely, never taking more than you give.
15662**
15663*************************************************************************
15664** This header file defines the interface that the sqlite page cache
15665** subsystem.
15666*/
15667
15668#ifndef _PCACHE_H_
15669
15670typedef struct PgHdr PgHdr;
15671typedef struct PCache PCache;
15672
15673/*
15674** Every page in the cache is controlled by an instance of the following
15675** structure.
15676*/
15677struct PgHdr {
15678 sqlite3_pcache_page *pPage; /* Pcache object page handle */
15679 void *pData; /* Page data */
15680 void *pExtra; /* Extra content */
15681 PCache *pCache; /* PRIVATE: Cache that owns this page */
15682 PgHdr *pDirty; /* Transient list of dirty sorted by pgno */
15683 Pager *pPager; /* The pager this page is part of */
15684 Pgno pgno; /* Page number for this page */
15685#ifdef SQLITE_CHECK_PAGES
15686 u32 pageHash; /* Hash of page content */
15687#endif
15688 u16 flags; /* PGHDR flags defined below */
15689
15690 /**********************************************************************
15691 ** Elements above, except pCache, are public. All that follow are
15692 ** private to pcache.c and should not be accessed by other modules.
15693 ** pCache is grouped with the public elements for efficiency.
15694 */
15695 i16 nRef; /* Number of users of this page */
15696 PgHdr *pDirtyNext; /* Next element in list of dirty pages */
15697 PgHdr *pDirtyPrev; /* Previous element in list of dirty pages */
15698 /* NB: pDirtyNext and pDirtyPrev are undefined if the
15699 ** PgHdr object is not dirty */
15700};
15701
15702/* Bit values for PgHdr.flags */
15703#define PGHDR_CLEAN 0x001 /* Page not on the PCache.pDirty list */
15704#define PGHDR_DIRTY 0x002 /* Page is on the PCache.pDirty list */
15705#define PGHDR_WRITEABLE 0x004 /* Journaled and ready to modify */
15706#define PGHDR_NEED_SYNC 0x008 /* Fsync the rollback journal before
15707 ** writing this page to the database */
15708#define PGHDR_DONT_WRITE 0x010 /* Do not write content to disk */
15709#define PGHDR_MMAP 0x020 /* This is an mmap page object */
15710
15711#define PGHDR_WAL_APPEND 0x040 /* Appended to wal file */
15712
15713/* Initialize and shutdown the page cache subsystem */
15714SQLITE_PRIVATE int sqlite3PcacheInitialize(void);
15715SQLITE_PRIVATE void sqlite3PcacheShutdown(void);
15716
15717/* Page cache buffer management:
15718** These routines implement SQLITE_CONFIG_PAGECACHE.
15719*/
15720SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *, int sz, int n);
15721
15722/* Create a new pager cache.
15723** Under memory stress, invoke xStress to try to make pages clean.
15724** Only clean and unpinned pages can be reclaimed.
15725*/
15726SQLITE_PRIVATE int sqlite3PcacheOpen(
15727 int szPage, /* Size of every page */
15728 int szExtra, /* Extra space associated with each page */
15729 int bPurgeable, /* True if pages are on backing store */
15730 int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
15731 void *pStress, /* Argument to xStress */
15732 PCache *pToInit /* Preallocated space for the PCache */
15733);
15734
15735/* Modify the page-size after the cache has been created. */
15736SQLITE_PRIVATE int sqlite3PcacheSetPageSize(PCache *, int);
15737
15738/* Return the size in bytes of a PCache object. Used to preallocate
15739** storage space.
15740*/
15741SQLITE_PRIVATE int sqlite3PcacheSize(void);
15742
15743/* One release per successful fetch. Page is pinned until released.
15744** Reference counted.
15745*/
15746SQLITE_PRIVATE sqlite3_pcache_page *sqlite3PcacheFetch(PCache*, Pgno, int createFlag);
15747SQLITE_PRIVATE int sqlite3PcacheFetchStress(PCache*, Pgno, sqlite3_pcache_page**);
15748SQLITE_PRIVATE PgHdr *sqlite3PcacheFetchFinish(PCache*, Pgno, sqlite3_pcache_page *pPage);
15749SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr*);
15750
15751SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr*); /* Remove page from cache */
15752SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr*); /* Make sure page is marked dirty */
15753SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr*); /* Mark a single page as clean */
15754SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache*); /* Mark all dirty list pages as clean */
15755SQLITE_PRIVATE void sqlite3PcacheClearWritable(PCache*);
15756
15757/* Change a page number. Used by incr-vacuum. */
15758SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr*, Pgno);
15759
15760/* Remove all pages with pgno>x. Reset the cache if x==0 */
15761SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache*, Pgno x);
15762
15763/* Get a list of all dirty pages in the cache, sorted by page number */
15764SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache*);
15765
15766/* Reset and close the cache object */
15767SQLITE_PRIVATE void sqlite3PcacheClose(PCache*);
15768
15769/* Clear flags from pages of the page cache */
15770SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *);
15771
15772/* Discard the contents of the cache */
15773SQLITE_PRIVATE void sqlite3PcacheClear(PCache*);
15774
15775/* Return the total number of outstanding page references */
15776SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache*);
15777
15778/* Increment the reference count of an existing page */
15779SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr*);
15780
15781SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr*);
15782
15783/* Return the total number of pages stored in the cache */
15784SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*);
15785
15786#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
15787/* Iterate through all dirty pages currently stored in the cache. This
15788** interface is only available if SQLITE_CHECK_PAGES is defined when the
15789** library is built.
15790*/
15791SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
15792#endif
15793
15794#if defined(SQLITE_DEBUG)
15795/* Check invariants on a PgHdr object */
15796SQLITE_PRIVATE int sqlite3PcachePageSanity(PgHdr*);
15797#endif
15798
15799/* Set and get the suggested cache-size for the specified pager-cache.
15800**
15801** If no global maximum is configured, then the system attempts to limit
15802** the total number of pages cached by purgeable pager-caches to the sum
15803** of the suggested cache-sizes.
15804*/
15805SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int);
15806#ifdef SQLITE_TEST
15807SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *);
15808#endif
15809
15810/* Set or get the suggested spill-size for the specified pager-cache.
15811**
15812** The spill-size is the minimum number of pages in cache before the cache
15813** will attempt to spill dirty pages by calling xStress.
15814*/
15815SQLITE_PRIVATE int sqlite3PcacheSetSpillsize(PCache *, int);
15816
15817/* Free up as much memory as possible from the page cache */
15818SQLITE_PRIVATE void sqlite3PcacheShrink(PCache*);
15819
15820#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
15821/* Try to return memory used by the pcache module to the main memory heap */
15822SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int);
15823#endif
15824
15825#ifdef SQLITE_TEST
15826SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*);
15827#endif
15828
15829SQLITE_PRIVATE void sqlite3PCacheSetDefault(void);
15830
15831/* Return the header size */
15832SQLITE_PRIVATE int sqlite3HeaderSizePcache(void);
15833SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void);
15834
15835/* Number of dirty pages as a percentage of the configured cache size */
15836SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache*);
15837
15838#ifdef SQLITE_DIRECT_OVERFLOW_READ
15839SQLITE_PRIVATE int sqlite3PCacheIsDirty(PCache *pCache);
15840#endif
15841
15842#endif /* _PCACHE_H_ */
15843
15844/************** End of pcache.h **********************************************/
15845/************** Continuing where we left off in sqliteInt.h ******************/
15846/************** Include os.h in the middle of sqliteInt.h ********************/
15847/************** Begin file os.h **********************************************/
15848/*
15849** 2001 September 16
15850**
15851** The author disclaims copyright to this source code. In place of
15852** a legal notice, here is a blessing:
15853**
15854** May you do good and not evil.
15855** May you find forgiveness for yourself and forgive others.
15856** May you share freely, never taking more than you give.
15857**
15858******************************************************************************
15859**
15860** This header file (together with is companion C source-code file
15861** "os.c") attempt to abstract the underlying operating system so that
15862** the SQLite library will work on both POSIX and windows systems.
15863**
15864** This header file is #include-ed by sqliteInt.h and thus ends up
15865** being included by every source file.
15866*/
15867#ifndef _SQLITE_OS_H_
15868#define _SQLITE_OS_H_
15869
15870/*
15871** Attempt to automatically detect the operating system and setup the
15872** necessary pre-processor macros for it.
15873*/
15874/************** Include os_setup.h in the middle of os.h *********************/
15875/************** Begin file os_setup.h ****************************************/
15876/*
15877** 2013 November 25
15878**
15879** The author disclaims copyright to this source code. In place of
15880** a legal notice, here is a blessing:
15881**
15882** May you do good and not evil.
15883** May you find forgiveness for yourself and forgive others.
15884** May you share freely, never taking more than you give.
15885**
15886******************************************************************************
15887**
15888** This file contains pre-processor directives related to operating system
15889** detection and/or setup.
15890*/
15891#ifndef SQLITE_OS_SETUP_H
15892#define SQLITE_OS_SETUP_H
15893
15894/*
15895** Figure out if we are dealing with Unix, Windows, or some other operating
15896** system.
15897**
15898** After the following block of preprocess macros, all of SQLITE_OS_UNIX,
15899** SQLITE_OS_WIN, and SQLITE_OS_OTHER will defined to either 1 or 0. One of
15900** the three will be 1. The other two will be 0.
15901*/
15902#if defined(SQLITE_OS_OTHER)
15903# if SQLITE_OS_OTHER==1
15904# undef SQLITE_OS_UNIX
15905# define SQLITE_OS_UNIX 0
15906# undef SQLITE_OS_WIN
15907# define SQLITE_OS_WIN 0
15908# else
15909# undef SQLITE_OS_OTHER
15910# endif
15911#endif
15912#if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
15913# define SQLITE_OS_OTHER 0
15914# ifndef SQLITE_OS_WIN
15915# if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \
15916 defined(__MINGW32__) || defined(__BORLANDC__)
15917# define SQLITE_OS_WIN 1
15918# define SQLITE_OS_UNIX 0
15919# else
15920# define SQLITE_OS_WIN 0
15921# define SQLITE_OS_UNIX 1
15922# endif
15923# else
15924# define SQLITE_OS_UNIX 0
15925# endif
15926#else
15927# ifndef SQLITE_OS_WIN
15928# define SQLITE_OS_WIN 0
15929# endif
15930#endif
15931
15932#endif /* SQLITE_OS_SETUP_H */
15933
15934/************** End of os_setup.h ********************************************/
15935/************** Continuing where we left off in os.h *************************/
15936
15937/* If the SET_FULLSYNC macro is not defined above, then make it
15938** a no-op
15939*/
15940#ifndef SET_FULLSYNC
15941# define SET_FULLSYNC(x,y)
15942#endif
15943
15944/* Maximum pathname length. Note: FILENAME_MAX defined by stdio.h
15945*/
15946#ifndef SQLITE_MAX_PATHLEN
15947# define SQLITE_MAX_PATHLEN FILENAME_MAX
15948#endif
15949
15950/*
15951** The default size of a disk sector
15952*/
15953#ifndef SQLITE_DEFAULT_SECTOR_SIZE
15954# define SQLITE_DEFAULT_SECTOR_SIZE 4096
15955#endif
15956
15957/*
15958** Temporary files are named starting with this prefix followed by 16 random
15959** alphanumeric characters, and no file extension. They are stored in the
15960** OS's standard temporary file directory, and are deleted prior to exit.
15961** If sqlite is being embedded in another program, you may wish to change the
15962** prefix to reflect your program's name, so that if your program exits
15963** prematurely, old temporary files can be easily identified. This can be done
15964** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
15965**
15966** 2006-10-31: The default prefix used to be "sqlite_". But then
15967** Mcafee started using SQLite in their anti-virus product and it
15968** started putting files with the "sqlite" name in the c:/temp folder.
15969** This annoyed many windows users. Those users would then do a
15970** Google search for "sqlite", find the telephone numbers of the
15971** developers and call to wake them up at night and complain.
15972** For this reason, the default name prefix is changed to be "sqlite"
15973** spelled backwards. So the temp files are still identified, but
15974** anybody smart enough to figure out the code is also likely smart
15975** enough to know that calling the developer will not help get rid
15976** of the file.
15977*/
15978#ifndef SQLITE_TEMP_FILE_PREFIX
15979# define SQLITE_TEMP_FILE_PREFIX "etilqs_"
15980#endif
15981
15982/*
15983** The following values may be passed as the second argument to
15984** sqlite3OsLock(). The various locks exhibit the following semantics:
15985**
15986** SHARED: Any number of processes may hold a SHARED lock simultaneously.
15987** RESERVED: A single process may hold a RESERVED lock on a file at
15988** any time. Other processes may hold and obtain new SHARED locks.
15989** PENDING: A single process may hold a PENDING lock on a file at
15990** any one time. Existing SHARED locks may persist, but no new
15991** SHARED locks may be obtained by other processes.
15992** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
15993**
15994** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
15995** process that requests an EXCLUSIVE lock may actually obtain a PENDING
15996** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
15997** sqlite3OsLock().
15998*/
15999#define NO_LOCK 0
16000#define SHARED_LOCK 1
16001#define RESERVED_LOCK 2
16002#define PENDING_LOCK 3
16003#define EXCLUSIVE_LOCK 4
16004
16005/*
16006** File Locking Notes: (Mostly about windows but also some info for Unix)
16007**
16008** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
16009** those functions are not available. So we use only LockFile() and
16010** UnlockFile().
16011**
16012** LockFile() prevents not just writing but also reading by other processes.
16013** A SHARED_LOCK is obtained by locking a single randomly-chosen
16014** byte out of a specific range of bytes. The lock byte is obtained at
16015** random so two separate readers can probably access the file at the
16016** same time, unless they are unlucky and choose the same lock byte.
16017** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
16018** There can only be one writer. A RESERVED_LOCK is obtained by locking
16019** a single byte of the file that is designated as the reserved lock byte.
16020** A PENDING_LOCK is obtained by locking a designated byte different from
16021** the RESERVED_LOCK byte.
16022**
16023** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
16024** which means we can use reader/writer locks. When reader/writer locks
16025** are used, the lock is placed on the same range of bytes that is used
16026** for probabilistic locking in Win95/98/ME. Hence, the locking scheme
16027** will support two or more Win95 readers or two or more WinNT readers.
16028** But a single Win95 reader will lock out all WinNT readers and a single
16029** WinNT reader will lock out all other Win95 readers.
16030**
16031** The following #defines specify the range of bytes used for locking.
16032** SHARED_SIZE is the number of bytes available in the pool from which
16033** a random byte is selected for a shared lock. The pool of bytes for
16034** shared locks begins at SHARED_FIRST.
16035**
16036** The same locking strategy and
16037** byte ranges are used for Unix. This leaves open the possibility of having
16038** clients on win95, winNT, and unix all talking to the same shared file
16039** and all locking correctly. To do so would require that samba (or whatever
16040** tool is being used for file sharing) implements locks correctly between
16041** windows and unix. I'm guessing that isn't likely to happen, but by
16042** using the same locking range we are at least open to the possibility.
16043**
16044** Locking in windows is manditory. For this reason, we cannot store
16045** actual data in the bytes used for locking. The pager never allocates
16046** the pages involved in locking therefore. SHARED_SIZE is selected so
16047** that all locks will fit on a single page even at the minimum page size.
16048** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE
16049** is set high so that we don't have to allocate an unused page except
16050** for very large databases. But one should test the page skipping logic
16051** by setting PENDING_BYTE low and running the entire regression suite.
16052**
16053** Changing the value of PENDING_BYTE results in a subtly incompatible
16054** file format. Depending on how it is changed, you might not notice
16055** the incompatibility right away, even running a full regression test.
16056** The default location of PENDING_BYTE is the first byte past the
16057** 1GB boundary.
16058**
16059*/
16060#ifdef SQLITE_OMIT_WSD
16061# define PENDING_BYTE (0x40000000)
16062#else
16063# define PENDING_BYTE sqlite3PendingByte
16064#endif
16065#define RESERVED_BYTE (PENDING_BYTE+1)
16066#define SHARED_FIRST (PENDING_BYTE+2)
16067#define SHARED_SIZE 510
16068
16069/*
16070** Wrapper around OS specific sqlite3_os_init() function.
16071*/
16072SQLITE_PRIVATE int sqlite3OsInit(void);
16073
16074/*
16075** Functions for accessing sqlite3_file methods
16076*/
16077SQLITE_PRIVATE void sqlite3OsClose(sqlite3_file*);
16078SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
16079SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
16080SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size);
16081SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file*, int);
16082SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
16083SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int);
16084SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int);
16085SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
16086SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
16087SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
16088#define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
16089SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
16090SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
16091#ifndef SQLITE_OMIT_WAL
16092SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
16093SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
16094SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
16095SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
16096#endif /* SQLITE_OMIT_WAL */
16097SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64, int, void **);
16098SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *, i64, void *);
16099
16100
16101/*
16102** Functions for accessing sqlite3_vfs methods
16103*/
16104SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
16105SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
16106SQLITE_PRIVATE int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
16107SQLITE_PRIVATE int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
16108#ifndef SQLITE_OMIT_LOAD_EXTENSION
16109SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
16110SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *, int, char *);
16111SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
16112SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *);
16113#endif /* SQLITE_OMIT_LOAD_EXTENSION */
16114SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
16115SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
16116SQLITE_PRIVATE int sqlite3OsGetLastError(sqlite3_vfs*);
16117SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
16118
16119/*
16120** Convenience functions for opening and closing files using
16121** sqlite3_malloc() to obtain space for the file-handle structure.
16122*/
16123SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
16124SQLITE_PRIVATE void sqlite3OsCloseFree(sqlite3_file *);
16125
16126#endif /* _SQLITE_OS_H_ */
16127
16128/************** End of os.h **************************************************/
16129/************** Continuing where we left off in sqliteInt.h ******************/
16130/************** Include mutex.h in the middle of sqliteInt.h *****************/
16131/************** Begin file mutex.h *******************************************/
16132/*
16133** 2007 August 28
16134**
16135** The author disclaims copyright to this source code. In place of
16136** a legal notice, here is a blessing:
16137**
16138** May you do good and not evil.
16139** May you find forgiveness for yourself and forgive others.
16140** May you share freely, never taking more than you give.
16141**
16142*************************************************************************
16143**
16144** This file contains the common header for all mutex implementations.
16145** The sqliteInt.h header #includes this file so that it is available
16146** to all source files. We break it out in an effort to keep the code
16147** better organized.
16148**
16149** NOTE: source files should *not* #include this header file directly.
16150** Source files should #include the sqliteInt.h file and let that file
16151** include this one indirectly.
16152*/
16153
16154
16155/*
16156** Figure out what version of the code to use. The choices are
16157**
16158** SQLITE_MUTEX_OMIT No mutex logic. Not even stubs. The
16159** mutexes implementation cannot be overridden
16160** at start-time.
16161**
16162** SQLITE_MUTEX_NOOP For single-threaded applications. No
16163** mutual exclusion is provided. But this
16164** implementation can be overridden at
16165** start-time.
16166**
16167** SQLITE_MUTEX_PTHREADS For multi-threaded applications on Unix.
16168**
16169** SQLITE_MUTEX_W32 For multi-threaded applications on Win32.
16170*/
16171#if !SQLITE_THREADSAFE
16172# define SQLITE_MUTEX_OMIT
16173#endif
16174#if SQLITE_THREADSAFE && !defined(SQLITE_MUTEX_NOOP)
16175# if SQLITE_OS_UNIX
16176# define SQLITE_MUTEX_PTHREADS
16177# elif SQLITE_OS_WIN
16178# define SQLITE_MUTEX_W32
16179# else
16180# define SQLITE_MUTEX_NOOP
16181# endif
16182#endif
16183
16184#ifdef SQLITE_MUTEX_OMIT
16185/*
16186** If this is a no-op implementation, implement everything as macros.
16187*/
16188#define sqlite3_mutex_alloc(X) ((sqlite3_mutex*)8)
16189#define sqlite3_mutex_free(X)
16190#define sqlite3_mutex_enter(X)
16191#define sqlite3_mutex_try(X) SQLITE_OK
16192#define sqlite3_mutex_leave(X)
16193#define sqlite3_mutex_held(X) ((void)(X),1)
16194#define sqlite3_mutex_notheld(X) ((void)(X),1)
16195#define sqlite3MutexAlloc(X) ((sqlite3_mutex*)8)
16196#define sqlite3MutexInit() SQLITE_OK
16197#define sqlite3MutexEnd()
16198#define MUTEX_LOGIC(X)
16199#else
16200#define MUTEX_LOGIC(X) X
16201SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
16202#endif /* defined(SQLITE_MUTEX_OMIT) */
16203
16204/************** End of mutex.h ***********************************************/
16205/************** Continuing where we left off in sqliteInt.h ******************/
16206
16207/* The SQLITE_EXTRA_DURABLE compile-time option used to set the default
16208** synchronous setting to EXTRA. It is no longer supported.
16209*/
16210#ifdef SQLITE_EXTRA_DURABLE
16211# warning Use SQLITE_DEFAULT_SYNCHRONOUS=3 instead of SQLITE_EXTRA_DURABLE
16212# define SQLITE_DEFAULT_SYNCHRONOUS 3
16213#endif
16214
16215/*
16216** Default synchronous levels.
16217**
16218** Note that (for historcal reasons) the PAGER_SYNCHRONOUS_* macros differ
16219** from the SQLITE_DEFAULT_SYNCHRONOUS value by 1.
16220**
16221** PAGER_SYNCHRONOUS DEFAULT_SYNCHRONOUS
16222** OFF 1 0
16223** NORMAL 2 1
16224** FULL 3 2
16225** EXTRA 4 3
16226**
16227** The "PRAGMA synchronous" statement also uses the zero-based numbers.
16228** In other words, the zero-based numbers are used for all external interfaces
16229** and the one-based values are used internally.
16230*/
16231#ifndef SQLITE_DEFAULT_SYNCHRONOUS
16232# define SQLITE_DEFAULT_SYNCHRONOUS 2
16233#endif
16234#ifndef SQLITE_DEFAULT_WAL_SYNCHRONOUS
16235# define SQLITE_DEFAULT_WAL_SYNCHRONOUS SQLITE_DEFAULT_SYNCHRONOUS
16236#endif
16237
16238/*
16239** Each database file to be accessed by the system is an instance
16240** of the following structure. There are normally two of these structures
16241** in the sqlite.aDb[] array. aDb[0] is the main database file and
16242** aDb[1] is the database file used to hold temporary tables. Additional
16243** databases may be attached.
16244*/
16245struct Db {
16246 char *zDbSName; /* Name of this database. (schema name, not filename) */
16247 Btree *pBt; /* The B*Tree structure for this database file */
16248 u8 safety_level; /* How aggressive at syncing data to disk */
16249 u8 bSyncSet; /* True if "PRAGMA synchronous=N" has been run */
16250 Schema *pSchema; /* Pointer to database schema (possibly shared) */
16251};
16252
16253/*
16254** An instance of the following structure stores a database schema.
16255**
16256** Most Schema objects are associated with a Btree. The exception is
16257** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
16258** In shared cache mode, a single Schema object can be shared by multiple
16259** Btrees that refer to the same underlying BtShared object.
16260**
16261** Schema objects are automatically deallocated when the last Btree that
16262** references them is destroyed. The TEMP Schema is manually freed by
16263** sqlite3_close().
16264*
16265** A thread must be holding a mutex on the corresponding Btree in order
16266** to access Schema content. This implies that the thread must also be
16267** holding a mutex on the sqlite3 connection pointer that owns the Btree.
16268** For a TEMP Schema, only the connection mutex is required.
16269*/
16270struct Schema {
16271 int schema_cookie; /* Database schema version number for this file */
16272 int iGeneration; /* Generation counter. Incremented with each change */
16273 Hash tblHash; /* All tables indexed by name */
16274 Hash idxHash; /* All (named) indices indexed by name */
16275 Hash trigHash; /* All triggers indexed by name */
16276 Hash fkeyHash; /* All foreign keys by referenced table name */
16277 Table *pSeqTab; /* The sqlite_sequence table used by AUTOINCREMENT */
16278 u8 file_format; /* Schema format version for this file */
16279 u8 enc; /* Text encoding used by this database */
16280 u16 schemaFlags; /* Flags associated with this schema */
16281 int cache_size; /* Number of pages to use in the cache */
16282};
16283
16284/*
16285** These macros can be used to test, set, or clear bits in the
16286** Db.pSchema->flags field.
16287*/
16288#define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
16289#define DbHasAnyProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))!=0)
16290#define DbSetProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags|=(P)
16291#define DbClearProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags&=~(P)
16292
16293/*
16294** Allowed values for the DB.pSchema->flags field.
16295**
16296** The DB_SchemaLoaded flag is set after the database schema has been
16297** read into internal hash tables.
16298**
16299** DB_UnresetViews means that one or more views have column names that
16300** have been filled out. If the schema changes, these column names might
16301** changes and so the view will need to be reset.
16302*/
16303#define DB_SchemaLoaded 0x0001 /* The schema has been loaded */
16304#define DB_UnresetViews 0x0002 /* Some views have defined column names */
16305#define DB_ResetWanted 0x0008 /* Reset the schema when nSchemaLock==0 */
16306
16307/*
16308** The number of different kinds of things that can be limited
16309** using the sqlite3_limit() interface.
16310*/
16311#define SQLITE_N_LIMIT (SQLITE_LIMIT_WORKER_THREADS+1)
16312
16313/*
16314** Lookaside malloc is a set of fixed-size buffers that can be used
16315** to satisfy small transient memory allocation requests for objects
16316** associated with a particular database connection. The use of
16317** lookaside malloc provides a significant performance enhancement
16318** (approx 10%) by avoiding numerous malloc/free requests while parsing
16319** SQL statements.
16320**
16321** The Lookaside structure holds configuration information about the
16322** lookaside malloc subsystem. Each available memory allocation in
16323** the lookaside subsystem is stored on a linked list of LookasideSlot
16324** objects.
16325**
16326** Lookaside allocations are only allowed for objects that are associated
16327** with a particular database connection. Hence, schema information cannot
16328** be stored in lookaside because in shared cache mode the schema information
16329** is shared by multiple database connections. Therefore, while parsing
16330** schema information, the Lookaside.bEnabled flag is cleared so that
16331** lookaside allocations are not used to construct the schema objects.
16332**
16333** New lookaside allocations are only allowed if bDisable==0. When
16334** bDisable is greater than zero, sz is set to zero which effectively
16335** disables lookaside without adding a new test for the bDisable flag
16336** in a performance-critical path. sz should be set by to szTrue whenever
16337** bDisable changes back to zero.
16338**
16339** Lookaside buffers are initially held on the pInit list. As they are
16340** used and freed, they are added back to the pFree list. New allocations
16341** come off of pFree first, then pInit as a fallback. This dual-list
16342** allows use to compute a high-water mark - the maximum number of allocations
16343** outstanding at any point in the past - by subtracting the number of
16344** allocations on the pInit list from the total number of allocations.
16345**
16346** Enhancement on 2019-12-12: Two-size-lookaside
16347** The default lookaside configuration is 100 slots of 1200 bytes each.
16348** The larger slot sizes are important for performance, but they waste
16349** a lot of space, as most lookaside allocations are less than 128 bytes.
16350** The two-size-lookaside enhancement breaks up the lookaside allocation
16351** into two pools: One of 128-byte slots and the other of the default size
16352** (1200-byte) slots. Allocations are filled from the small-pool first,
16353** failing over to the full-size pool if that does not work. Thus more
16354** lookaside slots are available while also using less memory.
16355** This enhancement can be omitted by compiling with
16356** SQLITE_OMIT_TWOSIZE_LOOKASIDE.
16357*/
16358struct Lookaside {
16359 u32 bDisable; /* Only operate the lookaside when zero */
16360 u16 sz; /* Size of each buffer in bytes */
16361 u16 szTrue; /* True value of sz, even if disabled */
16362 u8 bMalloced; /* True if pStart obtained from sqlite3_malloc() */
16363 u32 nSlot; /* Number of lookaside slots allocated */
16364 u32 anStat[3]; /* 0: hits. 1: size misses. 2: full misses */
16365 LookasideSlot *pInit; /* List of buffers not previously used */
16366 LookasideSlot *pFree; /* List of available buffers */
16367#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
16368 LookasideSlot *pSmallInit; /* List of small buffers not prediously used */
16369 LookasideSlot *pSmallFree; /* List of available small buffers */
16370 void *pMiddle; /* First byte past end of full-size buffers and
16371 ** the first byte of LOOKASIDE_SMALL buffers */
16372#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
16373 void *pStart; /* First byte of available memory space */
16374 void *pEnd; /* First byte past end of available space */
16375};
16376struct LookasideSlot {
16377 LookasideSlot *pNext; /* Next buffer in the list of free buffers */
16378};
16379
16380#define DisableLookaside db->lookaside.bDisable++;db->lookaside.sz=0
16381#define EnableLookaside db->lookaside.bDisable--;\
16382 db->lookaside.sz=db->lookaside.bDisable?0:db->lookaside.szTrue
16383
16384/* Size of the smaller allocations in two-size lookside */
16385#ifdef SQLITE_OMIT_TWOSIZE_LOOKASIDE
16386# define LOOKASIDE_SMALL 0
16387#else
16388# define LOOKASIDE_SMALL 128
16389#endif
16390
16391/*
16392** A hash table for built-in function definitions. (Application-defined
16393** functions use a regular table table from hash.h.)
16394**
16395** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
16396** Collisions are on the FuncDef.u.pHash chain. Use the SQLITE_FUNC_HASH()
16397** macro to compute a hash on the function name.
16398*/
16399#define SQLITE_FUNC_HASH_SZ 23
16400struct FuncDefHash {
16401 FuncDef *a[SQLITE_FUNC_HASH_SZ]; /* Hash table for functions */
16402};
16403#define SQLITE_FUNC_HASH(C,L) (((C)+(L))%SQLITE_FUNC_HASH_SZ)
16404
16405#ifdef SQLITE_USER_AUTHENTICATION
16406/*
16407** Information held in the "sqlite3" database connection object and used
16408** to manage user authentication.
16409*/
16410typedef struct sqlite3_userauth sqlite3_userauth;
16411struct sqlite3_userauth {
16412 u8 authLevel; /* Current authentication level */
16413 int nAuthPW; /* Size of the zAuthPW in bytes */
16414 char *zAuthPW; /* Password used to authenticate */
16415 char *zAuthUser; /* User name used to authenticate */
16416};
16417
16418/* Allowed values for sqlite3_userauth.authLevel */
16419#define UAUTH_Unknown 0 /* Authentication not yet checked */
16420#define UAUTH_Fail 1 /* User authentication failed */
16421#define UAUTH_User 2 /* Authenticated as a normal user */
16422#define UAUTH_Admin 3 /* Authenticated as an administrator */
16423
16424/* Functions used only by user authorization logic */
16425SQLITE_PRIVATE int sqlite3UserAuthTable(const char*);
16426SQLITE_PRIVATE int sqlite3UserAuthCheckLogin(sqlite3*,const char*,u8*);
16427SQLITE_PRIVATE void sqlite3UserAuthInit(sqlite3*);
16428SQLITE_PRIVATE void sqlite3CryptFunc(sqlite3_context*,int,sqlite3_value**);
16429
16430#endif /* SQLITE_USER_AUTHENTICATION */
16431
16432/*
16433** typedef for the authorization callback function.
16434*/
16435#ifdef SQLITE_USER_AUTHENTICATION
16436 typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
16437 const char*, const char*);
16438#else
16439 typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
16440 const char*);
16441#endif
16442
16443#ifndef SQLITE_OMIT_DEPRECATED
16444/* This is an extra SQLITE_TRACE macro that indicates "legacy" tracing
16445** in the style of sqlite3_trace()
16446*/
16447#define SQLITE_TRACE_LEGACY 0x40 /* Use the legacy xTrace */
16448#define SQLITE_TRACE_XPROFILE 0x80 /* Use the legacy xProfile */
16449#else
16450#define SQLITE_TRACE_LEGACY 0
16451#define SQLITE_TRACE_XPROFILE 0
16452#endif /* SQLITE_OMIT_DEPRECATED */
16453#define SQLITE_TRACE_NONLEGACY_MASK 0x0f /* Normal flags */
16454
16455/*
16456** Maximum number of sqlite3.aDb[] entries. This is the number of attached
16457** databases plus 2 for "main" and "temp".
16458*/
16459#define SQLITE_MAX_DB (SQLITE_MAX_ATTACHED+2)
16460
16461/*
16462** Each database connection is an instance of the following structure.
16463*/
16464struct sqlite3 {
16465 sqlite3_vfs *pVfs; /* OS Interface */
16466 struct Vdbe *pVdbe; /* List of active virtual machines */
16467 CollSeq *pDfltColl; /* BINARY collseq for the database encoding */
16468 sqlite3_mutex *mutex; /* Connection mutex */
16469 Db *aDb; /* All backends */
16470 int nDb; /* Number of backends currently in use */
16471 u32 mDbFlags; /* flags recording internal state */
16472 u64 flags; /* flags settable by pragmas. See below */
16473 i64 lastRowid; /* ROWID of most recent insert (see above) */
16474 i64 szMmap; /* Default mmap_size setting */
16475 u32 nSchemaLock; /* Do not reset the schema when non-zero */
16476 unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */
16477 int errCode; /* Most recent error code (SQLITE_*) */
16478 int errMask; /* & result codes with this before returning */
16479 int iSysErrno; /* Errno value from last system error */
16480 u32 dbOptFlags; /* Flags to enable/disable optimizations */
16481 u8 enc; /* Text encoding */
16482 u8 autoCommit; /* The auto-commit flag. */
16483 u8 temp_store; /* 1: file 2: memory 0: default */
16484 u8 mallocFailed; /* True if we have seen a malloc failure */
16485 u8 bBenignMalloc; /* Do not require OOMs if true */
16486 u8 dfltLockMode; /* Default locking-mode for attached dbs */
16487 signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */
16488 u8 suppressErr; /* Do not issue error messages if true */
16489 u8 vtabOnConflict; /* Value to return for s3_vtab_on_conflict() */
16490 u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */
16491 u8 mTrace; /* zero or more SQLITE_TRACE flags */
16492 u8 noSharedCache; /* True if no shared-cache backends */
16493 u8 nSqlExec; /* Number of pending OP_SqlExec opcodes */
16494 u8 eOpenState; /* Current condition of the connection */
16495 int nextPagesize; /* Pagesize after VACUUM if >0 */
16496 i64 nChange; /* Value returned by sqlite3_changes() */
16497 i64 nTotalChange; /* Value returned by sqlite3_total_changes() */
16498 int aLimit[SQLITE_N_LIMIT]; /* Limits */
16499 int nMaxSorterMmap; /* Maximum size of regions mapped by sorter */
16500 struct sqlite3InitInfo { /* Information used during initialization */
16501 Pgno newTnum; /* Rootpage of table being initialized */
16502 u8 iDb; /* Which db file is being initialized */
16503 u8 busy; /* TRUE if currently initializing */
16504 unsigned orphanTrigger : 1; /* Last statement is orphaned TEMP trigger */
16505 unsigned imposterTable : 1; /* Building an imposter table */
16506 unsigned reopenMemdb : 1; /* ATTACH is really a reopen using MemDB */
16507 const char **azInit; /* "type", "name", and "tbl_name" columns */
16508 } init;
16509 int nVdbeActive; /* Number of VDBEs currently running */
16510 int nVdbeRead; /* Number of active VDBEs that read or write */
16511 int nVdbeWrite; /* Number of active VDBEs that read and write */
16512 int nVdbeExec; /* Number of nested calls to VdbeExec() */
16513 int nVDestroy; /* Number of active OP_VDestroy operations */
16514 int nExtension; /* Number of loaded extensions */
16515 void **aExtension; /* Array of shared library handles */
16516 union {
16517 void (*xLegacy)(void*,const char*); /* mTrace==SQLITE_TRACE_LEGACY */
16518 int (*xV2)(u32,void*,void*,void*); /* All other mTrace values */
16519 } trace;
16520 void *pTraceArg; /* Argument to the trace function */
16521#ifndef SQLITE_OMIT_DEPRECATED
16522 void (*xProfile)(void*,const char*,u64); /* Profiling function */
16523 void *pProfileArg; /* Argument to profile function */
16524#endif
16525 void *pCommitArg; /* Argument to xCommitCallback() */
16526 int (*xCommitCallback)(void*); /* Invoked at every commit. */
16527 void *pRollbackArg; /* Argument to xRollbackCallback() */
16528 void (*xRollbackCallback)(void*); /* Invoked at every commit. */
16529 void *pUpdateArg;
16530 void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
16531 void *pAutovacPagesArg; /* Client argument to autovac_pages */
16532 void (*xAutovacDestr)(void*); /* Destructor for pAutovacPAgesArg */
16533 unsigned int (*xAutovacPages)(void*,const char*,u32,u32,u32);
16534 Parse *pParse; /* Current parse */
16535#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
16536 void *pPreUpdateArg; /* First argument to xPreUpdateCallback */
16537 void (*xPreUpdateCallback)( /* Registered using sqlite3_preupdate_hook() */
16538 void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64
16539 );
16540 PreUpdate *pPreUpdate; /* Context for active pre-update callback */
16541#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
16542#ifndef SQLITE_OMIT_WAL
16543 int (*xWalCallback)(void *, sqlite3 *, const char *, int);
16544 void *pWalArg;
16545#endif
16546 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
16547 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
16548 void *pCollNeededArg;
16549 sqlite3_value *pErr; /* Most recent error message */
16550 union {
16551 volatile int isInterrupted; /* True if sqlite3_interrupt has been called */
16552 double notUsed1; /* Spacer */
16553 } u1;
16554 Lookaside lookaside; /* Lookaside malloc configuration */
16555#ifndef SQLITE_OMIT_AUTHORIZATION
16556 sqlite3_xauth xAuth; /* Access authorization function */
16557 void *pAuthArg; /* 1st argument to the access auth function */
16558#endif
16559#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
16560 int (*xProgress)(void *); /* The progress callback */
16561 void *pProgressArg; /* Argument to the progress callback */
16562 unsigned nProgressOps; /* Number of opcodes for progress callback */
16563#endif
16564#ifndef SQLITE_OMIT_VIRTUALTABLE
16565 int nVTrans; /* Allocated size of aVTrans */
16566 Hash aModule; /* populated by sqlite3_create_module() */
16567 VtabCtx *pVtabCtx; /* Context for active vtab connect/create */
16568 VTable **aVTrans; /* Virtual tables with open transactions */
16569 VTable *pDisconnect; /* Disconnect these in next sqlite3_prepare() */
16570#endif
16571 Hash aFunc; /* Hash table of connection functions */
16572 Hash aCollSeq; /* All collating sequences */
16573 BusyHandler busyHandler; /* Busy callback */
16574 Db aDbStatic[2]; /* Static space for the 2 default backends */
16575 Savepoint *pSavepoint; /* List of active savepoints */
16576 int nAnalysisLimit; /* Number of index rows to ANALYZE */
16577 int busyTimeout; /* Busy handler timeout, in msec */
16578 int nSavepoint; /* Number of non-transaction savepoints */
16579 int nStatement; /* Number of nested statement-transactions */
16580 i64 nDeferredCons; /* Net deferred constraints this transaction. */
16581 i64 nDeferredImmCons; /* Net deferred immediate constraints */
16582 int *pnBytesFreed; /* If not NULL, increment this in DbFree() */
16583#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
16584 /* The following variables are all protected by the STATIC_MAIN
16585 ** mutex, not by sqlite3.mutex. They are used by code in notify.c.
16586 **
16587 ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
16588 ** unlock so that it can proceed.
16589 **
16590 ** When X.pBlockingConnection==Y, that means that something that X tried
16591 ** tried to do recently failed with an SQLITE_LOCKED error due to locks
16592 ** held by Y.
16593 */
16594 sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */
16595 sqlite3 *pUnlockConnection; /* Connection to watch for unlock */
16596 void *pUnlockArg; /* Argument to xUnlockNotify */
16597 void (*xUnlockNotify)(void **, int); /* Unlock notify callback */
16598 sqlite3 *pNextBlocked; /* Next in list of all blocked connections */
16599#endif
16600#ifdef SQLITE_USER_AUTHENTICATION
16601 sqlite3_userauth auth; /* User authentication information */
16602#endif
16603};
16604
16605/*
16606** A macro to discover the encoding of a database.
16607*/
16608#define SCHEMA_ENC(db) ((db)->aDb[0].pSchema->enc)
16609#define ENC(db) ((db)->enc)
16610
16611/*
16612** A u64 constant where the lower 32 bits are all zeros. Only the
16613** upper 32 bits are included in the argument. Necessary because some
16614** C-compilers still do not accept LL integer literals.
16615*/
16616#define HI(X) ((u64)(X)<<32)
16617
16618/*
16619** Possible values for the sqlite3.flags.
16620**
16621** Value constraints (enforced via assert()):
16622** SQLITE_FullFSync == PAGER_FULLFSYNC
16623** SQLITE_CkptFullFSync == PAGER_CKPT_FULLFSYNC
16624** SQLITE_CacheSpill == PAGER_CACHE_SPILL
16625*/
16626#define SQLITE_WriteSchema 0x00000001 /* OK to update SQLITE_SCHEMA */
16627#define SQLITE_LegacyFileFmt 0x00000002 /* Create new databases in format 1 */
16628#define SQLITE_FullColNames 0x00000004 /* Show full column names on SELECT */
16629#define SQLITE_FullFSync 0x00000008 /* Use full fsync on the backend */
16630#define SQLITE_CkptFullFSync 0x00000010 /* Use full fsync for checkpoint */
16631#define SQLITE_CacheSpill 0x00000020 /* OK to spill pager cache */
16632#define SQLITE_ShortColNames 0x00000040 /* Show short columns names */
16633#define SQLITE_TrustedSchema 0x00000080 /* Allow unsafe functions and
16634 ** vtabs in the schema definition */
16635#define SQLITE_NullCallback 0x00000100 /* Invoke the callback once if the */
16636 /* result set is empty */
16637#define SQLITE_IgnoreChecks 0x00000200 /* Do not enforce check constraints */
16638#define SQLITE_ReadUncommit 0x00000400 /* READ UNCOMMITTED in shared-cache */
16639#define SQLITE_NoCkptOnClose 0x00000800 /* No checkpoint on close()/DETACH */
16640#define SQLITE_ReverseOrder 0x00001000 /* Reverse unordered SELECTs */
16641#define SQLITE_RecTriggers 0x00002000 /* Enable recursive triggers */
16642#define SQLITE_ForeignKeys 0x00004000 /* Enforce foreign key constraints */
16643#define SQLITE_AutoIndex 0x00008000 /* Enable automatic indexes */
16644#define SQLITE_LoadExtension 0x00010000 /* Enable load_extension */
16645#define SQLITE_LoadExtFunc 0x00020000 /* Enable load_extension() SQL func */
16646#define SQLITE_EnableTrigger 0x00040000 /* True to enable triggers */
16647#define SQLITE_DeferFKs 0x00080000 /* Defer all FK constraints */
16648#define SQLITE_QueryOnly 0x00100000 /* Disable database changes */
16649#define SQLITE_CellSizeCk 0x00200000 /* Check btree cell sizes on load */
16650#define SQLITE_Fts3Tokenizer 0x00400000 /* Enable fts3_tokenizer(2) */
16651#define SQLITE_EnableQPSG 0x00800000 /* Query Planner Stability Guarantee*/
16652#define SQLITE_TriggerEQP 0x01000000 /* Show trigger EXPLAIN QUERY PLAN */
16653#define SQLITE_ResetDatabase 0x02000000 /* Reset the database */
16654#define SQLITE_LegacyAlter 0x04000000 /* Legacy ALTER TABLE behaviour */
16655#define SQLITE_NoSchemaError 0x08000000 /* Do not report schema parse errors*/
16656#define SQLITE_Defensive 0x10000000 /* Input SQL is likely hostile */
16657#define SQLITE_DqsDDL 0x20000000 /* dbl-quoted strings allowed in DDL*/
16658#define SQLITE_DqsDML 0x40000000 /* dbl-quoted strings allowed in DML*/
16659#define SQLITE_EnableView 0x80000000 /* Enable the use of views */
16660#define SQLITE_CountRows HI(0x00001) /* Count rows changed by INSERT, */
16661 /* DELETE, or UPDATE and return */
16662 /* the count using a callback. */
16663#define SQLITE_CorruptRdOnly HI(0x00002) /* Prohibit writes due to error */
16664
16665/* Flags used only if debugging */
16666#ifdef SQLITE_DEBUG
16667#define SQLITE_SqlTrace HI(0x0100000) /* Debug print SQL as it executes */
16668#define SQLITE_VdbeListing HI(0x0200000) /* Debug listings of VDBE progs */
16669#define SQLITE_VdbeTrace HI(0x0400000) /* True to trace VDBE execution */
16670#define SQLITE_VdbeAddopTrace HI(0x0800000) /* Trace sqlite3VdbeAddOp() calls */
16671#define SQLITE_VdbeEQP HI(0x1000000) /* Debug EXPLAIN QUERY PLAN */
16672#define SQLITE_ParserTrace HI(0x2000000) /* PRAGMA parser_trace=ON */
16673#endif
16674
16675/*
16676** Allowed values for sqlite3.mDbFlags
16677*/
16678#define DBFLAG_SchemaChange 0x0001 /* Uncommitted Hash table changes */
16679#define DBFLAG_PreferBuiltin 0x0002 /* Preference to built-in funcs */
16680#define DBFLAG_Vacuum 0x0004 /* Currently in a VACUUM */
16681#define DBFLAG_VacuumInto 0x0008 /* Currently running VACUUM INTO */
16682#define DBFLAG_SchemaKnownOk 0x0010 /* Schema is known to be valid */
16683#define DBFLAG_InternalFunc 0x0020 /* Allow use of internal functions */
16684#define DBFLAG_EncodingFixed 0x0040 /* No longer possible to change enc. */
16685
16686/*
16687** Bits of the sqlite3.dbOptFlags field that are used by the
16688** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
16689** selectively disable various optimizations.
16690*/
16691#define SQLITE_QueryFlattener 0x00000001 /* Query flattening */
16692#define SQLITE_WindowFunc 0x00000002 /* Use xInverse for window functions */
16693#define SQLITE_GroupByOrder 0x00000004 /* GROUPBY cover of ORDERBY */
16694#define SQLITE_FactorOutConst 0x00000008 /* Constant factoring */
16695#define SQLITE_DistinctOpt 0x00000010 /* DISTINCT using indexes */
16696#define SQLITE_CoverIdxScan 0x00000020 /* Covering index scans */
16697#define SQLITE_OrderByIdxJoin 0x00000040 /* ORDER BY of joins via index */
16698#define SQLITE_Transitive 0x00000080 /* Transitive constraints */
16699#define SQLITE_OmitNoopJoin 0x00000100 /* Omit unused tables in joins */
16700#define SQLITE_CountOfView 0x00000200 /* The count-of-view optimization */
16701#define SQLITE_CursorHints 0x00000400 /* Add OP_CursorHint opcodes */
16702#define SQLITE_Stat4 0x00000800 /* Use STAT4 data */
16703 /* TH3 expects this value ^^^^^^^^^^ to be 0x0000800. Don't change it */
16704#define SQLITE_PushDown 0x00001000 /* The push-down optimization */
16705#define SQLITE_SimplifyJoin 0x00002000 /* Convert LEFT JOIN to JOIN */
16706#define SQLITE_SkipScan 0x00004000 /* Skip-scans */
16707#define SQLITE_PropagateConst 0x00008000 /* The constant propagation opt */
16708#define SQLITE_MinMaxOpt 0x00010000 /* The min/max optimization */
16709#define SQLITE_SeekScan 0x00020000 /* The OP_SeekScan optimization */
16710#define SQLITE_OmitOrderBy 0x00040000 /* Omit pointless ORDER BY */
16711 /* TH3 expects this value ^^^^^^^^^^ to be 0x40000. Coordinate any change */
16712#define SQLITE_AllOpts 0xffffffff /* All optimizations */
16713
16714/*
16715** Macros for testing whether or not optimizations are enabled or disabled.
16716*/
16717#define OptimizationDisabled(db, mask) (((db)->dbOptFlags&(mask))!=0)
16718#define OptimizationEnabled(db, mask) (((db)->dbOptFlags&(mask))==0)
16719
16720/*
16721** Return true if it OK to factor constant expressions into the initialization
16722** code. The argument is a Parse object for the code generator.
16723*/
16724#define ConstFactorOk(P) ((P)->okConstFactor)
16725
16726/* Possible values for the sqlite3.eOpenState field.
16727** The numbers are randomly selected such that a minimum of three bits must
16728** change to convert any number to another or to zero
16729*/
16730#define SQLITE_STATE_OPEN 0x76 /* Database is open */
16731#define SQLITE_STATE_CLOSED 0xce /* Database is closed */
16732#define SQLITE_STATE_SICK 0xba /* Error and awaiting close */
16733#define SQLITE_STATE_BUSY 0x6d /* Database currently in use */
16734#define SQLITE_STATE_ERROR 0xd5 /* An SQLITE_MISUSE error occurred */
16735#define SQLITE_STATE_ZOMBIE 0xa7 /* Close with last statement close */
16736
16737/*
16738** Each SQL function is defined by an instance of the following
16739** structure. For global built-in functions (ex: substr(), max(), count())
16740** a pointer to this structure is held in the sqlite3BuiltinFunctions object.
16741** For per-connection application-defined functions, a pointer to this
16742** structure is held in the db->aHash hash table.
16743**
16744** The u.pHash field is used by the global built-ins. The u.pDestructor
16745** field is used by per-connection app-def functions.
16746*/
16747struct FuncDef {
16748 i8 nArg; /* Number of arguments. -1 means unlimited */
16749 u32 funcFlags; /* Some combination of SQLITE_FUNC_* */
16750 void *pUserData; /* User data parameter */
16751 FuncDef *pNext; /* Next function with same name */
16752 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */
16753 void (*xFinalize)(sqlite3_context*); /* Agg finalizer */
16754 void (*xValue)(sqlite3_context*); /* Current agg value */
16755 void (*xInverse)(sqlite3_context*,int,sqlite3_value**); /* inverse agg-step */
16756 const char *zName; /* SQL name of the function. */
16757 union {
16758 FuncDef *pHash; /* Next with a different name but the same hash */
16759 FuncDestructor *pDestructor; /* Reference counted destructor function */
16760 } u; /* pHash if SQLITE_FUNC_BUILTIN, pDestructor otherwise */
16761};
16762
16763/*
16764** This structure encapsulates a user-function destructor callback (as
16765** configured using create_function_v2()) and a reference counter. When
16766** create_function_v2() is called to create a function with a destructor,
16767** a single object of this type is allocated. FuncDestructor.nRef is set to
16768** the number of FuncDef objects created (either 1 or 3, depending on whether
16769** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor
16770** member of each of the new FuncDef objects is set to point to the allocated
16771** FuncDestructor.
16772**
16773** Thereafter, when one of the FuncDef objects is deleted, the reference
16774** count on this object is decremented. When it reaches 0, the destructor
16775** is invoked and the FuncDestructor structure freed.
16776*/
16777struct FuncDestructor {
16778 int nRef;
16779 void (*xDestroy)(void *);
16780 void *pUserData;
16781};
16782
16783/*
16784** Possible values for FuncDef.flags. Note that the _LENGTH and _TYPEOF
16785** values must correspond to OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG. And
16786** SQLITE_FUNC_CONSTANT must be the same as SQLITE_DETERMINISTIC. There
16787** are assert() statements in the code to verify this.
16788**
16789** Value constraints (enforced via assert()):
16790** SQLITE_FUNC_MINMAX == NC_MinMaxAgg == SF_MinMaxAgg
16791** SQLITE_FUNC_ANYORDER == NC_OrderAgg == SF_OrderByReqd
16792** SQLITE_FUNC_LENGTH == OPFLAG_LENGTHARG
16793** SQLITE_FUNC_TYPEOF == OPFLAG_TYPEOFARG
16794** SQLITE_FUNC_CONSTANT == SQLITE_DETERMINISTIC from the API
16795** SQLITE_FUNC_DIRECT == SQLITE_DIRECTONLY from the API
16796** SQLITE_FUNC_UNSAFE == SQLITE_INNOCUOUS
16797** SQLITE_FUNC_ENCMASK depends on SQLITE_UTF* macros in the API
16798*/
16799#define SQLITE_FUNC_ENCMASK 0x0003 /* SQLITE_UTF8, SQLITE_UTF16BE or UTF16LE */
16800#define SQLITE_FUNC_LIKE 0x0004 /* Candidate for the LIKE optimization */
16801#define SQLITE_FUNC_CASE 0x0008 /* Case-sensitive LIKE-type function */
16802#define SQLITE_FUNC_EPHEM 0x0010 /* Ephemeral. Delete with VDBE */
16803#define SQLITE_FUNC_NEEDCOLL 0x0020 /* sqlite3GetFuncCollSeq() might be called*/
16804#define SQLITE_FUNC_LENGTH 0x0040 /* Built-in length() function */
16805#define SQLITE_FUNC_TYPEOF 0x0080 /* Built-in typeof() function */
16806#define SQLITE_FUNC_COUNT 0x0100 /* Built-in count(*) aggregate */
16807/* 0x0200 -- available for reuse */
16808#define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */
16809#define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */
16810#define SQLITE_FUNC_MINMAX 0x1000 /* True for min() and max() aggregates */
16811#define SQLITE_FUNC_SLOCHNG 0x2000 /* "Slow Change". Value constant during a
16812 ** single query - might change over time */
16813#define SQLITE_FUNC_TEST 0x4000 /* Built-in testing functions */
16814#define SQLITE_FUNC_OFFSET 0x8000 /* Built-in sqlite_offset() function */
16815#define SQLITE_FUNC_WINDOW 0x00010000 /* Built-in window-only function */
16816#define SQLITE_FUNC_INTERNAL 0x00040000 /* For use by NestedParse() only */
16817#define SQLITE_FUNC_DIRECT 0x00080000 /* Not for use in TRIGGERs or VIEWs */
16818#define SQLITE_FUNC_SUBTYPE 0x00100000 /* Result likely to have sub-type */
16819#define SQLITE_FUNC_UNSAFE 0x00200000 /* Function has side effects */
16820#define SQLITE_FUNC_INLINE 0x00400000 /* Functions implemented in-line */
16821#define SQLITE_FUNC_BUILTIN 0x00800000 /* This is a built-in function */
16822#define SQLITE_FUNC_ANYORDER 0x08000000 /* count/min/max aggregate */
16823
16824/* Identifier numbers for each in-line function */
16825#define INLINEFUNC_coalesce 0
16826#define INLINEFUNC_implies_nonnull_row 1
16827#define INLINEFUNC_expr_implies_expr 2
16828#define INLINEFUNC_expr_compare 3
16829#define INLINEFUNC_affinity 4
16830#define INLINEFUNC_iif 5
16831#define INLINEFUNC_unlikely 99 /* Default case */
16832
16833/*
16834** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
16835** used to create the initializers for the FuncDef structures.
16836**
16837** FUNCTION(zName, nArg, iArg, bNC, xFunc)
16838** Used to create a scalar function definition of a function zName
16839** implemented by C function xFunc that accepts nArg arguments. The
16840** value passed as iArg is cast to a (void*) and made available
16841** as the user-data (sqlite3_user_data()) for the function. If
16842** argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set.
16843**
16844** VFUNCTION(zName, nArg, iArg, bNC, xFunc)
16845** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag.
16846**
16847** SFUNCTION(zName, nArg, iArg, bNC, xFunc)
16848** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag and
16849** adds the SQLITE_DIRECTONLY flag.
16850**
16851** INLINE_FUNC(zName, nArg, iFuncId, mFlags)
16852** zName is the name of a function that is implemented by in-line
16853** byte code rather than by the usual callbacks. The iFuncId
16854** parameter determines the function id. The mFlags parameter is
16855** optional SQLITE_FUNC_ flags for this function.
16856**
16857** TEST_FUNC(zName, nArg, iFuncId, mFlags)
16858** zName is the name of a test-only function implemented by in-line
16859** byte code rather than by the usual callbacks. The iFuncId
16860** parameter determines the function id. The mFlags parameter is
16861** optional SQLITE_FUNC_ flags for this function.
16862**
16863** DFUNCTION(zName, nArg, iArg, bNC, xFunc)
16864** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag and
16865** adds the SQLITE_FUNC_SLOCHNG flag. Used for date & time functions
16866** and functions like sqlite_version() that can change, but not during
16867** a single query. The iArg is ignored. The user-data is always set
16868** to a NULL pointer. The bNC parameter is not used.
16869**
16870** MFUNCTION(zName, nArg, xPtr, xFunc)
16871** For math-library functions. xPtr is an arbitrary pointer.
16872**
16873** PURE_DATE(zName, nArg, iArg, bNC, xFunc)
16874** Used for "pure" date/time functions, this macro is like DFUNCTION
16875** except that it does set the SQLITE_FUNC_CONSTANT flags. iArg is
16876** ignored and the user-data for these functions is set to an
16877** arbitrary non-NULL pointer. The bNC parameter is not used.
16878**
16879** AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal)
16880** Used to create an aggregate function definition implemented by
16881** the C functions xStep and xFinal. The first four parameters
16882** are interpreted in the same way as the first 4 parameters to
16883** FUNCTION().
16884**
16885** WFUNCTION(zName, nArg, iArg, xStep, xFinal, xValue, xInverse)
16886** Used to create an aggregate function definition implemented by
16887** the C functions xStep and xFinal. The first four parameters
16888** are interpreted in the same way as the first 4 parameters to
16889** FUNCTION().
16890**
16891** LIKEFUNC(zName, nArg, pArg, flags)
16892** Used to create a scalar function definition of a function zName
16893** that accepts nArg arguments and is implemented by a call to C
16894** function likeFunc. Argument pArg is cast to a (void *) and made
16895** available as the function user-data (sqlite3_user_data()). The
16896** FuncDef.flags variable is set to the value passed as the flags
16897** parameter.
16898*/
16899#define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
16900 {nArg, SQLITE_FUNC_BUILTIN|\
16901 SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
16902 SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
16903#define VFUNCTION(zName, nArg, iArg, bNC, xFunc) \
16904 {nArg, SQLITE_FUNC_BUILTIN|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
16905 SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
16906#define SFUNCTION(zName, nArg, iArg, bNC, xFunc) \
16907 {nArg, SQLITE_FUNC_BUILTIN|SQLITE_UTF8|SQLITE_DIRECTONLY|SQLITE_FUNC_UNSAFE, \
16908 SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
16909#define MFUNCTION(zName, nArg, xPtr, xFunc) \
16910 {nArg, SQLITE_FUNC_BUILTIN|SQLITE_FUNC_CONSTANT|SQLITE_UTF8, \
16911 xPtr, 0, xFunc, 0, 0, 0, #zName, {0} }
16912#define INLINE_FUNC(zName, nArg, iArg, mFlags) \
16913 {nArg, SQLITE_FUNC_BUILTIN|\
16914 SQLITE_UTF8|SQLITE_FUNC_INLINE|SQLITE_FUNC_CONSTANT|(mFlags), \
16915 SQLITE_INT_TO_PTR(iArg), 0, noopFunc, 0, 0, 0, #zName, {0} }
16916#define TEST_FUNC(zName, nArg, iArg, mFlags) \
16917 {nArg, SQLITE_FUNC_BUILTIN|\
16918 SQLITE_UTF8|SQLITE_FUNC_INTERNAL|SQLITE_FUNC_TEST| \
16919 SQLITE_FUNC_INLINE|SQLITE_FUNC_CONSTANT|(mFlags), \
16920 SQLITE_INT_TO_PTR(iArg), 0, noopFunc, 0, 0, 0, #zName, {0} }
16921#define DFUNCTION(zName, nArg, iArg, bNC, xFunc) \
16922 {nArg, SQLITE_FUNC_BUILTIN|SQLITE_FUNC_SLOCHNG|SQLITE_UTF8, \
16923 0, 0, xFunc, 0, 0, 0, #zName, {0} }
16924#define PURE_DATE(zName, nArg, iArg, bNC, xFunc) \
16925 {nArg, SQLITE_FUNC_BUILTIN|\
16926 SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|SQLITE_FUNC_CONSTANT, \
16927 (void*)&sqlite3Config, 0, xFunc, 0, 0, 0, #zName, {0} }
16928#define FUNCTION2(zName, nArg, iArg, bNC, xFunc, extraFlags) \
16929 {nArg, SQLITE_FUNC_BUILTIN|\
16930 SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL)|extraFlags,\
16931 SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
16932#define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
16933 {nArg, SQLITE_FUNC_BUILTIN|\
16934 SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
16935 pArg, 0, xFunc, 0, 0, 0, #zName, }
16936#define LIKEFUNC(zName, nArg, arg, flags) \
16937 {nArg, SQLITE_FUNC_BUILTIN|SQLITE_FUNC_CONSTANT|SQLITE_UTF8|flags, \
16938 (void *)arg, 0, likeFunc, 0, 0, 0, #zName, {0} }
16939#define WAGGREGATE(zName, nArg, arg, nc, xStep, xFinal, xValue, xInverse, f) \
16940 {nArg, SQLITE_FUNC_BUILTIN|SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL)|f, \
16941 SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,xValue,xInverse,#zName, {0}}
16942#define INTERNAL_FUNCTION(zName, nArg, xFunc) \
16943 {nArg, SQLITE_FUNC_BUILTIN|\
16944 SQLITE_FUNC_INTERNAL|SQLITE_UTF8|SQLITE_FUNC_CONSTANT, \
16945 0, 0, xFunc, 0, 0, 0, #zName, {0} }
16946
16947
16948/*
16949** All current savepoints are stored in a linked list starting at
16950** sqlite3.pSavepoint. The first element in the list is the most recently
16951** opened savepoint. Savepoints are added to the list by the vdbe
16952** OP_Savepoint instruction.
16953*/
16954struct Savepoint {
16955 char *zName; /* Savepoint name (nul-terminated) */
16956 i64 nDeferredCons; /* Number of deferred fk violations */
16957 i64 nDeferredImmCons; /* Number of deferred imm fk. */
16958 Savepoint *pNext; /* Parent savepoint (if any) */
16959};
16960
16961/*
16962** The following are used as the second parameter to sqlite3Savepoint(),
16963** and as the P1 argument to the OP_Savepoint instruction.
16964*/
16965#define SAVEPOINT_BEGIN 0
16966#define SAVEPOINT_RELEASE 1
16967#define SAVEPOINT_ROLLBACK 2
16968
16969
16970/*
16971** Each SQLite module (virtual table definition) is defined by an
16972** instance of the following structure, stored in the sqlite3.aModule
16973** hash table.
16974*/
16975struct Module {
16976 const sqlite3_module *pModule; /* Callback pointers */
16977 const char *zName; /* Name passed to create_module() */
16978 int nRefModule; /* Number of pointers to this object */
16979 void *pAux; /* pAux passed to create_module() */
16980 void (*xDestroy)(void *); /* Module destructor function */
16981 Table *pEpoTab; /* Eponymous table for this module */
16982};
16983
16984/*
16985** Information about each column of an SQL table is held in an instance
16986** of the Column structure, in the Table.aCol[] array.
16987**
16988** Definitions:
16989**
16990** "table column index" This is the index of the column in the
16991** Table.aCol[] array, and also the index of
16992** the column in the original CREATE TABLE stmt.
16993**
16994** "storage column index" This is the index of the column in the
16995** record BLOB generated by the OP_MakeRecord
16996** opcode. The storage column index is less than
16997** or equal to the table column index. It is
16998** equal if and only if there are no VIRTUAL
16999** columns to the left.
17000**
17001** Notes on zCnName:
17002** The zCnName field stores the name of the column, the datatype of the
17003** column, and the collating sequence for the column, in that order, all in
17004** a single allocation. Each string is 0x00 terminated. The datatype
17005** is only included if the COLFLAG_HASTYPE bit of colFlags is set and the
17006** collating sequence name is only included if the COLFLAG_HASCOLL bit is
17007** set.
17008*/
17009struct Column {
17010 char *zCnName; /* Name of this column */
17011 unsigned notNull :4; /* An OE_ code for handling a NOT NULL constraint */
17012 unsigned eCType :4; /* One of the standard types */
17013 char affinity; /* One of the SQLITE_AFF_... values */
17014 u8 szEst; /* Est size of value in this column. sizeof(INT)==1 */
17015 u8 hName; /* Column name hash for faster lookup */
17016 u16 iDflt; /* 1-based index of DEFAULT. 0 means "none" */
17017 u16 colFlags; /* Boolean properties. See COLFLAG_ defines below */
17018};
17019
17020/* Allowed values for Column.eCType.
17021**
17022** Values must match entries in the global constant arrays
17023** sqlite3StdTypeLen[] and sqlite3StdType[]. Each value is one more
17024** than the offset into these arrays for the corresponding name.
17025** Adjust the SQLITE_N_STDTYPE value if adding or removing entries.
17026*/
17027#define COLTYPE_CUSTOM 0 /* Type appended to zName */
17028#define COLTYPE_ANY 1
17029#define COLTYPE_BLOB 2
17030#define COLTYPE_INT 3
17031#define COLTYPE_INTEGER 4
17032#define COLTYPE_REAL 5
17033#define COLTYPE_TEXT 6
17034#define SQLITE_N_STDTYPE 6 /* Number of standard types */
17035
17036/* Allowed values for Column.colFlags.
17037**
17038** Constraints:
17039** TF_HasVirtual == COLFLAG_VIRTUAL
17040** TF_HasStored == COLFLAG_STORED
17041** TF_HasHidden == COLFLAG_HIDDEN
17042*/
17043#define COLFLAG_PRIMKEY 0x0001 /* Column is part of the primary key */
17044#define COLFLAG_HIDDEN 0x0002 /* A hidden column in a virtual table */
17045#define COLFLAG_HASTYPE 0x0004 /* Type name follows column name */
17046#define COLFLAG_UNIQUE 0x0008 /* Column def contains "UNIQUE" or "PK" */
17047#define COLFLAG_SORTERREF 0x0010 /* Use sorter-refs with this column */
17048#define COLFLAG_VIRTUAL 0x0020 /* GENERATED ALWAYS AS ... VIRTUAL */
17049#define COLFLAG_STORED 0x0040 /* GENERATED ALWAYS AS ... STORED */
17050#define COLFLAG_NOTAVAIL 0x0080 /* STORED column not yet calculated */
17051#define COLFLAG_BUSY 0x0100 /* Blocks recursion on GENERATED columns */
17052#define COLFLAG_HASCOLL 0x0200 /* Has collating sequence name in zCnName */
17053#define COLFLAG_GENERATED 0x0060 /* Combo: _STORED, _VIRTUAL */
17054#define COLFLAG_NOINSERT 0x0062 /* Combo: _HIDDEN, _STORED, _VIRTUAL */
17055
17056/*
17057** A "Collating Sequence" is defined by an instance of the following
17058** structure. Conceptually, a collating sequence consists of a name and
17059** a comparison routine that defines the order of that sequence.
17060**
17061** If CollSeq.xCmp is NULL, it means that the
17062** collating sequence is undefined. Indices built on an undefined
17063** collating sequence may not be read or written.
17064*/
17065struct CollSeq {
17066 char *zName; /* Name of the collating sequence, UTF-8 encoded */
17067 u8 enc; /* Text encoding handled by xCmp() */
17068 void *pUser; /* First argument to xCmp() */
17069 int (*xCmp)(void*,int, const void*, int, const void*);
17070 void (*xDel)(void*); /* Destructor for pUser */
17071};
17072
17073/*
17074** A sort order can be either ASC or DESC.
17075*/
17076#define SQLITE_SO_ASC 0 /* Sort in ascending order */
17077#define SQLITE_SO_DESC 1 /* Sort in ascending order */
17078#define SQLITE_SO_UNDEFINED -1 /* No sort order specified */
17079
17080/*
17081** Column affinity types.
17082**
17083** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
17084** 't' for SQLITE_AFF_TEXT. But we can save a little space and improve
17085** the speed a little by numbering the values consecutively.
17086**
17087** But rather than start with 0 or 1, we begin with 'A'. That way,
17088** when multiple affinity types are concatenated into a string and
17089** used as the P4 operand, they will be more readable.
17090**
17091** Note also that the numeric types are grouped together so that testing
17092** for a numeric type is a single comparison. And the BLOB type is first.
17093*/
17094#define SQLITE_AFF_NONE 0x40 /* '@' */
17095#define SQLITE_AFF_BLOB 0x41 /* 'A' */
17096#define SQLITE_AFF_TEXT 0x42 /* 'B' */
17097#define SQLITE_AFF_NUMERIC 0x43 /* 'C' */
17098#define SQLITE_AFF_INTEGER 0x44 /* 'D' */
17099#define SQLITE_AFF_REAL 0x45 /* 'E' */
17100
17101#define sqlite3IsNumericAffinity(X) ((X)>=SQLITE_AFF_NUMERIC)
17102
17103/*
17104** The SQLITE_AFF_MASK values masks off the significant bits of an
17105** affinity value.
17106*/
17107#define SQLITE_AFF_MASK 0x47
17108
17109/*
17110** Additional bit values that can be ORed with an affinity without
17111** changing the affinity.
17112**
17113** The SQLITE_NOTNULL flag is a combination of NULLEQ and JUMPIFNULL.
17114** It causes an assert() to fire if either operand to a comparison
17115** operator is NULL. It is added to certain comparison operators to
17116** prove that the operands are always NOT NULL.
17117*/
17118#define SQLITE_JUMPIFNULL 0x10 /* jumps if either operand is NULL */
17119#define SQLITE_NULLEQ 0x80 /* NULL=NULL */
17120#define SQLITE_NOTNULL 0x90 /* Assert that operands are never NULL */
17121
17122/*
17123** An object of this type is created for each virtual table present in
17124** the database schema.
17125**
17126** If the database schema is shared, then there is one instance of this
17127** structure for each database connection (sqlite3*) that uses the shared
17128** schema. This is because each database connection requires its own unique
17129** instance of the sqlite3_vtab* handle used to access the virtual table
17130** implementation. sqlite3_vtab* handles can not be shared between
17131** database connections, even when the rest of the in-memory database
17132** schema is shared, as the implementation often stores the database
17133** connection handle passed to it via the xConnect() or xCreate() method
17134** during initialization internally. This database connection handle may
17135** then be used by the virtual table implementation to access real tables
17136** within the database. So that they appear as part of the callers
17137** transaction, these accesses need to be made via the same database
17138** connection as that used to execute SQL operations on the virtual table.
17139**
17140** All VTable objects that correspond to a single table in a shared
17141** database schema are initially stored in a linked-list pointed to by
17142** the Table.pVTable member variable of the corresponding Table object.
17143** When an sqlite3_prepare() operation is required to access the virtual
17144** table, it searches the list for the VTable that corresponds to the
17145** database connection doing the preparing so as to use the correct
17146** sqlite3_vtab* handle in the compiled query.
17147**
17148** When an in-memory Table object is deleted (for example when the
17149** schema is being reloaded for some reason), the VTable objects are not
17150** deleted and the sqlite3_vtab* handles are not xDisconnect()ed
17151** immediately. Instead, they are moved from the Table.pVTable list to
17152** another linked list headed by the sqlite3.pDisconnect member of the
17153** corresponding sqlite3 structure. They are then deleted/xDisconnected
17154** next time a statement is prepared using said sqlite3*. This is done
17155** to avoid deadlock issues involving multiple sqlite3.mutex mutexes.
17156** Refer to comments above function sqlite3VtabUnlockList() for an
17157** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect
17158** list without holding the corresponding sqlite3.mutex mutex.
17159**
17160** The memory for objects of this type is always allocated by
17161** sqlite3DbMalloc(), using the connection handle stored in VTable.db as
17162** the first argument.
17163*/
17164struct VTable {
17165 sqlite3 *db; /* Database connection associated with this table */
17166 Module *pMod; /* Pointer to module implementation */
17167 sqlite3_vtab *pVtab; /* Pointer to vtab instance */
17168 int nRef; /* Number of pointers to this structure */
17169 u8 bConstraint; /* True if constraints are supported */
17170 u8 eVtabRisk; /* Riskiness of allowing hacker access */
17171 int iSavepoint; /* Depth of the SAVEPOINT stack */
17172 VTable *pNext; /* Next in linked list (see above) */
17173};
17174
17175/* Allowed values for VTable.eVtabRisk
17176*/
17177#define SQLITE_VTABRISK_Low 0
17178#define SQLITE_VTABRISK_Normal 1
17179#define SQLITE_VTABRISK_High 2
17180
17181/*
17182** The schema for each SQL table, virtual table, and view is represented
17183** in memory by an instance of the following structure.
17184*/
17185struct Table {
17186 char *zName; /* Name of the table or view */
17187 Column *aCol; /* Information about each column */
17188 Index *pIndex; /* List of SQL indexes on this table. */
17189 char *zColAff; /* String defining the affinity of each column */
17190 ExprList *pCheck; /* All CHECK constraints */
17191 /* ... also used as column name list in a VIEW */
17192 Pgno tnum; /* Root BTree page for this table */
17193 u32 nTabRef; /* Number of pointers to this Table */
17194 u32 tabFlags; /* Mask of TF_* values */
17195 i16 iPKey; /* If not negative, use aCol[iPKey] as the rowid */
17196 i16 nCol; /* Number of columns in this table */
17197 i16 nNVCol; /* Number of columns that are not VIRTUAL */
17198 LogEst nRowLogEst; /* Estimated rows in table - from sqlite_stat1 table */
17199 LogEst szTabRow; /* Estimated size of each table row in bytes */
17200#ifdef SQLITE_ENABLE_COSTMULT
17201 LogEst costMult; /* Cost multiplier for using this table */
17202#endif
17203 u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
17204 u8 eTabType; /* 0: normal, 1: virtual, 2: view */
17205 union {
17206 struct { /* Used by ordinary tables: */
17207 int addColOffset; /* Offset in CREATE TABLE stmt to add a new column */
17208 FKey *pFKey; /* Linked list of all foreign keys in this table */
17209 ExprList *pDfltList; /* DEFAULT clauses on various columns.
17210 ** Or the AS clause for generated columns. */
17211 } tab;
17212 struct { /* Used by views: */
17213 Select *pSelect; /* View definition */
17214 } view;
17215 struct { /* Used by virtual tables only: */
17216 int nArg; /* Number of arguments to the module */
17217 char **azArg; /* 0: module 1: schema 2: vtab name 3...: args */
17218 VTable *p; /* List of VTable objects. */
17219 } vtab;
17220 } u;
17221 Trigger *pTrigger; /* List of triggers on this object */
17222 Schema *pSchema; /* Schema that contains this table */
17223};
17224
17225/*
17226** Allowed values for Table.tabFlags.
17227**
17228** TF_OOOHidden applies to tables or view that have hidden columns that are
17229** followed by non-hidden columns. Example: "CREATE VIRTUAL TABLE x USING
17230** vtab1(a HIDDEN, b);". Since "b" is a non-hidden column but "a" is hidden,
17231** the TF_OOOHidden attribute would apply in this case. Such tables require
17232** special handling during INSERT processing. The "OOO" means "Out Of Order".
17233**
17234** Constraints:
17235**
17236** TF_HasVirtual == COLFLAG_VIRTUAL
17237** TF_HasStored == COLFLAG_STORED
17238** TF_HasHidden == COLFLAG_HIDDEN
17239*/
17240#define TF_Readonly 0x00000001 /* Read-only system table */
17241#define TF_HasHidden 0x00000002 /* Has one or more hidden columns */
17242#define TF_HasPrimaryKey 0x00000004 /* Table has a primary key */
17243#define TF_Autoincrement 0x00000008 /* Integer primary key is autoincrement */
17244#define TF_HasStat1 0x00000010 /* nRowLogEst set from sqlite_stat1 */
17245#define TF_HasVirtual 0x00000020 /* Has one or more VIRTUAL columns */
17246#define TF_HasStored 0x00000040 /* Has one or more STORED columns */
17247#define TF_HasGenerated 0x00000060 /* Combo: HasVirtual + HasStored */
17248#define TF_WithoutRowid 0x00000080 /* No rowid. PRIMARY KEY is the key */
17249#define TF_StatsUsed 0x00000100 /* Query planner decisions affected by
17250 ** Index.aiRowLogEst[] values */
17251#define TF_NoVisibleRowid 0x00000200 /* No user-visible "rowid" column */
17252#define TF_OOOHidden 0x00000400 /* Out-of-Order hidden columns */
17253#define TF_HasNotNull 0x00000800 /* Contains NOT NULL constraints */
17254#define TF_Shadow 0x00001000 /* True for a shadow table */
17255#define TF_HasStat4 0x00002000 /* STAT4 info available for this table */
17256#define TF_Ephemeral 0x00004000 /* An ephemeral table */
17257#define TF_Eponymous 0x00008000 /* An eponymous virtual table */
17258#define TF_Strict 0x00010000 /* STRICT mode */
17259
17260/*
17261** Allowed values for Table.eTabType
17262*/
17263#define TABTYP_NORM 0 /* Ordinary table */
17264#define TABTYP_VTAB 1 /* Virtual table */
17265#define TABTYP_VIEW 2 /* A view */
17266
17267#define IsView(X) ((X)->eTabType==TABTYP_VIEW)
17268#define IsOrdinaryTable(X) ((X)->eTabType==TABTYP_NORM)
17269
17270/*
17271** Test to see whether or not a table is a virtual table. This is
17272** done as a macro so that it will be optimized out when virtual
17273** table support is omitted from the build.
17274*/
17275#ifndef SQLITE_OMIT_VIRTUALTABLE
17276# define IsVirtual(X) ((X)->eTabType==TABTYP_VTAB)
17277# define ExprIsVtab(X) \
17278 ((X)->op==TK_COLUMN && (X)->y.pTab!=0 && (X)->y.pTab->eTabType==TABTYP_VTAB)
17279#else
17280# define IsVirtual(X) 0
17281# define ExprIsVtab(X) 0
17282#endif
17283
17284/*
17285** Macros to determine if a column is hidden. IsOrdinaryHiddenColumn()
17286** only works for non-virtual tables (ordinary tables and views) and is
17287** always false unless SQLITE_ENABLE_HIDDEN_COLUMNS is defined. The
17288** IsHiddenColumn() macro is general purpose.
17289*/
17290#if defined(SQLITE_ENABLE_HIDDEN_COLUMNS)
17291# define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
17292# define IsOrdinaryHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
17293#elif !defined(SQLITE_OMIT_VIRTUALTABLE)
17294# define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
17295# define IsOrdinaryHiddenColumn(X) 0
17296#else
17297# define IsHiddenColumn(X) 0
17298# define IsOrdinaryHiddenColumn(X) 0
17299#endif
17300
17301
17302/* Does the table have a rowid */
17303#define HasRowid(X) (((X)->tabFlags & TF_WithoutRowid)==0)
17304#define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0)
17305
17306/*
17307** Each foreign key constraint is an instance of the following structure.
17308**
17309** A foreign key is associated with two tables. The "from" table is
17310** the table that contains the REFERENCES clause that creates the foreign
17311** key. The "to" table is the table that is named in the REFERENCES clause.
17312** Consider this example:
17313**
17314** CREATE TABLE ex1(
17315** a INTEGER PRIMARY KEY,
17316** b INTEGER CONSTRAINT fk1 REFERENCES ex2(x)
17317** );
17318**
17319** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
17320** Equivalent names:
17321**
17322** from-table == child-table
17323** to-table == parent-table
17324**
17325** Each REFERENCES clause generates an instance of the following structure
17326** which is attached to the from-table. The to-table need not exist when
17327** the from-table is created. The existence of the to-table is not checked.
17328**
17329** The list of all parents for child Table X is held at X.pFKey.
17330**
17331** A list of all children for a table named Z (which might not even exist)
17332** is held in Schema.fkeyHash with a hash key of Z.
17333*/
17334struct FKey {
17335 Table *pFrom; /* Table containing the REFERENCES clause (aka: Child) */
17336 FKey *pNextFrom; /* Next FKey with the same in pFrom. Next parent of pFrom */
17337 char *zTo; /* Name of table that the key points to (aka: Parent) */
17338 FKey *pNextTo; /* Next with the same zTo. Next child of zTo. */
17339 FKey *pPrevTo; /* Previous with the same zTo */
17340 int nCol; /* Number of columns in this key */
17341 /* EV: R-30323-21917 */
17342 u8 isDeferred; /* True if constraint checking is deferred till COMMIT */
17343 u8 aAction[2]; /* ON DELETE and ON UPDATE actions, respectively */
17344 Trigger *apTrigger[2];/* Triggers for aAction[] actions */
17345 struct sColMap { /* Mapping of columns in pFrom to columns in zTo */
17346 int iFrom; /* Index of column in pFrom */
17347 char *zCol; /* Name of column in zTo. If NULL use PRIMARY KEY */
17348 } aCol[1]; /* One entry for each of nCol columns */
17349};
17350
17351/*
17352** SQLite supports many different ways to resolve a constraint
17353** error. ROLLBACK processing means that a constraint violation
17354** causes the operation in process to fail and for the current transaction
17355** to be rolled back. ABORT processing means the operation in process
17356** fails and any prior changes from that one operation are backed out,
17357** but the transaction is not rolled back. FAIL processing means that
17358** the operation in progress stops and returns an error code. But prior
17359** changes due to the same operation are not backed out and no rollback
17360** occurs. IGNORE means that the particular row that caused the constraint
17361** error is not inserted or updated. Processing continues and no error
17362** is returned. REPLACE means that preexisting database rows that caused
17363** a UNIQUE constraint violation are removed so that the new insert or
17364** update can proceed. Processing continues and no error is reported.
17365** UPDATE applies to insert operations only and means that the insert
17366** is omitted and the DO UPDATE clause of an upsert is run instead.
17367**
17368** RESTRICT, SETNULL, SETDFLT, and CASCADE actions apply only to foreign keys.
17369** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the
17370** same as ROLLBACK for DEFERRED keys. SETNULL means that the foreign
17371** key is set to NULL. SETDFLT means that the foreign key is set
17372** to its default value. CASCADE means that a DELETE or UPDATE of the
17373** referenced table row is propagated into the row that holds the
17374** foreign key.
17375**
17376** The OE_Default value is a place holder that means to use whatever
17377** conflict resolution algorthm is required from context.
17378**
17379** The following symbolic values are used to record which type
17380** of conflict resolution action to take.
17381*/
17382#define OE_None 0 /* There is no constraint to check */
17383#define OE_Rollback 1 /* Fail the operation and rollback the transaction */
17384#define OE_Abort 2 /* Back out changes but do no rollback transaction */
17385#define OE_Fail 3 /* Stop the operation but leave all prior changes */
17386#define OE_Ignore 4 /* Ignore the error. Do not do the INSERT or UPDATE */
17387#define OE_Replace 5 /* Delete existing record, then do INSERT or UPDATE */
17388#define OE_Update 6 /* Process as a DO UPDATE in an upsert */
17389#define OE_Restrict 7 /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
17390#define OE_SetNull 8 /* Set the foreign key value to NULL */
17391#define OE_SetDflt 9 /* Set the foreign key value to its default */
17392#define OE_Cascade 10 /* Cascade the changes */
17393#define OE_Default 11 /* Do whatever the default action is */
17394
17395
17396/*
17397** An instance of the following structure is passed as the first
17398** argument to sqlite3VdbeKeyCompare and is used to control the
17399** comparison of the two index keys.
17400**
17401** Note that aSortOrder[] and aColl[] have nField+1 slots. There
17402** are nField slots for the columns of an index then one extra slot
17403** for the rowid at the end.
17404*/
17405struct KeyInfo {
17406 u32 nRef; /* Number of references to this KeyInfo object */
17407 u8 enc; /* Text encoding - one of the SQLITE_UTF* values */
17408 u16 nKeyField; /* Number of key columns in the index */
17409 u16 nAllField; /* Total columns, including key plus others */
17410 sqlite3 *db; /* The database connection */
17411 u8 *aSortFlags; /* Sort order for each column. */
17412 CollSeq *aColl[1]; /* Collating sequence for each term of the key */
17413};
17414
17415/*
17416** Allowed bit values for entries in the KeyInfo.aSortFlags[] array.
17417*/
17418#define KEYINFO_ORDER_DESC 0x01 /* DESC sort order */
17419#define KEYINFO_ORDER_BIGNULL 0x02 /* NULL is larger than any other value */
17420
17421/*
17422** This object holds a record which has been parsed out into individual
17423** fields, for the purposes of doing a comparison.
17424**
17425** A record is an object that contains one or more fields of data.
17426** Records are used to store the content of a table row and to store
17427** the key of an index. A blob encoding of a record is created by
17428** the OP_MakeRecord opcode of the VDBE and is disassembled by the
17429** OP_Column opcode.
17430**
17431** An instance of this object serves as a "key" for doing a search on
17432** an index b+tree. The goal of the search is to find the entry that
17433** is closed to the key described by this object. This object might hold
17434** just a prefix of the key. The number of fields is given by
17435** pKeyInfo->nField.
17436**
17437** The r1 and r2 fields are the values to return if this key is less than
17438** or greater than a key in the btree, respectively. These are normally
17439** -1 and +1 respectively, but might be inverted to +1 and -1 if the b-tree
17440** is in DESC order.
17441**
17442** The key comparison functions actually return default_rc when they find
17443** an equals comparison. default_rc can be -1, 0, or +1. If there are
17444** multiple entries in the b-tree with the same key (when only looking
17445** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to
17446** cause the search to find the last match, or +1 to cause the search to
17447** find the first match.
17448**
17449** The key comparison functions will set eqSeen to true if they ever
17450** get and equal results when comparing this structure to a b-tree record.
17451** When default_rc!=0, the search might end up on the record immediately
17452** before the first match or immediately after the last match. The
17453** eqSeen field will indicate whether or not an exact match exists in the
17454** b-tree.
17455*/
17456struct UnpackedRecord {
17457 KeyInfo *pKeyInfo; /* Collation and sort-order information */
17458 Mem *aMem; /* Values */
17459 u16 nField; /* Number of entries in apMem[] */
17460 i8 default_rc; /* Comparison result if keys are equal */
17461 u8 errCode; /* Error detected by xRecordCompare (CORRUPT or NOMEM) */
17462 i8 r1; /* Value to return if (lhs < rhs) */
17463 i8 r2; /* Value to return if (lhs > rhs) */
17464 u8 eqSeen; /* True if an equality comparison has been seen */
17465};
17466
17467
17468/*
17469** Each SQL index is represented in memory by an
17470** instance of the following structure.
17471**
17472** The columns of the table that are to be indexed are described
17473** by the aiColumn[] field of this structure. For example, suppose
17474** we have the following table and index:
17475**
17476** CREATE TABLE Ex1(c1 int, c2 int, c3 text);
17477** CREATE INDEX Ex2 ON Ex1(c3,c1);
17478**
17479** In the Table structure describing Ex1, nCol==3 because there are
17480** three columns in the table. In the Index structure describing
17481** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
17482** The value of aiColumn is {2, 0}. aiColumn[0]==2 because the
17483** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
17484** The second column to be indexed (c1) has an index of 0 in
17485** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
17486**
17487** The Index.onError field determines whether or not the indexed columns
17488** must be unique and what to do if they are not. When Index.onError=OE_None,
17489** it means this is not a unique index. Otherwise it is a unique index
17490** and the value of Index.onError indicate the which conflict resolution
17491** algorithm to employ whenever an attempt is made to insert a non-unique
17492** element.
17493**
17494** While parsing a CREATE TABLE or CREATE INDEX statement in order to
17495** generate VDBE code (as opposed to parsing one read from an sqlite_schema
17496** table as part of parsing an existing database schema), transient instances
17497** of this structure may be created. In this case the Index.tnum variable is
17498** used to store the address of a VDBE instruction, not a database page
17499** number (it cannot - the database page is not allocated until the VDBE
17500** program is executed). See convertToWithoutRowidTable() for details.
17501*/
17502struct Index {
17503 char *zName; /* Name of this index */
17504 i16 *aiColumn; /* Which columns are used by this index. 1st is 0 */
17505 LogEst *aiRowLogEst; /* From ANALYZE: Est. rows selected by each column */
17506 Table *pTable; /* The SQL table being indexed */
17507 char *zColAff; /* String defining the affinity of each column */
17508 Index *pNext; /* The next index associated with the same table */
17509 Schema *pSchema; /* Schema containing this index */
17510 u8 *aSortOrder; /* for each column: True==DESC, False==ASC */
17511 const char **azColl; /* Array of collation sequence names for index */
17512 Expr *pPartIdxWhere; /* WHERE clause for partial indices */
17513 ExprList *aColExpr; /* Column expressions */
17514 Pgno tnum; /* DB Page containing root of this index */
17515 LogEst szIdxRow; /* Estimated average row size in bytes */
17516 u16 nKeyCol; /* Number of columns forming the key */
17517 u16 nColumn; /* Number of columns stored in the index */
17518 u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
17519 unsigned idxType:2; /* 0:Normal 1:UNIQUE, 2:PRIMARY KEY, 3:IPK */
17520 unsigned bUnordered:1; /* Use this index for == or IN queries only */
17521 unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */
17522 unsigned isResized:1; /* True if resizeIndexObject() has been called */
17523 unsigned isCovering:1; /* True if this is a covering index */
17524 unsigned noSkipScan:1; /* Do not try to use skip-scan if true */
17525 unsigned hasStat1:1; /* aiRowLogEst values come from sqlite_stat1 */
17526 unsigned bNoQuery:1; /* Do not use this index to optimize queries */
17527 unsigned bAscKeyBug:1; /* True if the bba7b69f9849b5bf bug applies */
17528 unsigned bHasVCol:1; /* Index references one or more VIRTUAL columns */
17529#ifdef SQLITE_ENABLE_STAT4
17530 int nSample; /* Number of elements in aSample[] */
17531 int nSampleCol; /* Size of IndexSample.anEq[] and so on */
17532 tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */
17533 IndexSample *aSample; /* Samples of the left-most key */
17534 tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */
17535 tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */
17536#endif
17537 Bitmask colNotIdxed; /* 0 for unindexed columns in pTab */
17538};
17539
17540/*
17541** Allowed values for Index.idxType
17542*/
17543#define SQLITE_IDXTYPE_APPDEF 0 /* Created using CREATE INDEX */
17544#define SQLITE_IDXTYPE_UNIQUE 1 /* Implements a UNIQUE constraint */
17545#define SQLITE_IDXTYPE_PRIMARYKEY 2 /* Is the PRIMARY KEY for the table */
17546#define SQLITE_IDXTYPE_IPK 3 /* INTEGER PRIMARY KEY index */
17547
17548/* Return true if index X is a PRIMARY KEY index */
17549#define IsPrimaryKeyIndex(X) ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
17550
17551/* Return true if index X is a UNIQUE index */
17552#define IsUniqueIndex(X) ((X)->onError!=OE_None)
17553
17554/* The Index.aiColumn[] values are normally positive integer. But
17555** there are some negative values that have special meaning:
17556*/
17557#define XN_ROWID (-1) /* Indexed column is the rowid */
17558#define XN_EXPR (-2) /* Indexed column is an expression */
17559
17560/*
17561** Each sample stored in the sqlite_stat4 table is represented in memory
17562** using a structure of this type. See documentation at the top of the
17563** analyze.c source file for additional information.
17564*/
17565struct IndexSample {
17566 void *p; /* Pointer to sampled record */
17567 int n; /* Size of record in bytes */
17568 tRowcnt *anEq; /* Est. number of rows where the key equals this sample */
17569 tRowcnt *anLt; /* Est. number of rows where key is less than this sample */
17570 tRowcnt *anDLt; /* Est. number of distinct keys less than this sample */
17571};
17572
17573/*
17574** Possible values to use within the flags argument to sqlite3GetToken().
17575*/
17576#define SQLITE_TOKEN_QUOTED 0x1 /* Token is a quoted identifier. */
17577#define SQLITE_TOKEN_KEYWORD 0x2 /* Token is a keyword. */
17578
17579/*
17580** Each token coming out of the lexer is an instance of
17581** this structure. Tokens are also used as part of an expression.
17582**
17583** The memory that "z" points to is owned by other objects. Take care
17584** that the owner of the "z" string does not deallocate the string before
17585** the Token goes out of scope! Very often, the "z" points to some place
17586** in the middle of the Parse.zSql text. But it might also point to a
17587** static string.
17588*/
17589struct Token {
17590 const char *z; /* Text of the token. Not NULL-terminated! */
17591 unsigned int n; /* Number of characters in this token */
17592};
17593
17594/*
17595** An instance of this structure contains information needed to generate
17596** code for a SELECT that contains aggregate functions.
17597**
17598** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a
17599** pointer to this structure. The Expr.iAgg field is the index in
17600** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate
17601** code for that node.
17602**
17603** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the
17604** original Select structure that describes the SELECT statement. These
17605** fields do not need to be freed when deallocating the AggInfo structure.
17606*/
17607struct AggInfo {
17608 u8 directMode; /* Direct rendering mode means take data directly
17609 ** from source tables rather than from accumulators */
17610 u8 useSortingIdx; /* In direct mode, reference the sorting index rather
17611 ** than the source table */
17612 int sortingIdx; /* Cursor number of the sorting index */
17613 int sortingIdxPTab; /* Cursor number of pseudo-table */
17614 int nSortingColumn; /* Number of columns in the sorting index */
17615 int mnReg, mxReg; /* Range of registers allocated for aCol and aFunc */
17616 ExprList *pGroupBy; /* The group by clause */
17617 struct AggInfo_col { /* For each column used in source tables */
17618 Table *pTab; /* Source table */
17619 Expr *pCExpr; /* The original expression */
17620 int iTable; /* Cursor number of the source table */
17621 int iMem; /* Memory location that acts as accumulator */
17622 i16 iColumn; /* Column number within the source table */
17623 i16 iSorterColumn; /* Column number in the sorting index */
17624 } *aCol;
17625 int nColumn; /* Number of used entries in aCol[] */
17626 int nAccumulator; /* Number of columns that show through to the output.
17627 ** Additional columns are used only as parameters to
17628 ** aggregate functions */
17629 struct AggInfo_func { /* For each aggregate function */
17630 Expr *pFExpr; /* Expression encoding the function */
17631 FuncDef *pFunc; /* The aggregate function implementation */
17632 int iMem; /* Memory location that acts as accumulator */
17633 int iDistinct; /* Ephemeral table used to enforce DISTINCT */
17634 int iDistAddr; /* Address of OP_OpenEphemeral */
17635 } *aFunc;
17636 int nFunc; /* Number of entries in aFunc[] */
17637 u32 selId; /* Select to which this AggInfo belongs */
17638};
17639
17640/*
17641** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
17642** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
17643** than 32767 we have to make it 32-bit. 16-bit is preferred because
17644** it uses less memory in the Expr object, which is a big memory user
17645** in systems with lots of prepared statements. And few applications
17646** need more than about 10 or 20 variables. But some extreme users want
17647** to have prepared statements with over 32766 variables, and for them
17648** the option is available (at compile-time).
17649*/
17650#if SQLITE_MAX_VARIABLE_NUMBER<32767
17651typedef i16 ynVar;
17652#else
17653typedef int ynVar;
17654#endif
17655
17656/*
17657** Each node of an expression in the parse tree is an instance
17658** of this structure.
17659**
17660** Expr.op is the opcode. The integer parser token codes are reused
17661** as opcodes here. For example, the parser defines TK_GE to be an integer
17662** code representing the ">=" operator. This same integer code is reused
17663** to represent the greater-than-or-equal-to operator in the expression
17664** tree.
17665**
17666** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB,
17667** or TK_STRING), then Expr.u.zToken contains the text of the SQL literal. If
17668** the expression is a variable (TK_VARIABLE), then Expr.u.zToken contains the
17669** variable name. Finally, if the expression is an SQL function (TK_FUNCTION),
17670** then Expr.u.zToken contains the name of the function.
17671**
17672** Expr.pRight and Expr.pLeft are the left and right subexpressions of a
17673** binary operator. Either or both may be NULL.
17674**
17675** Expr.x.pList is a list of arguments if the expression is an SQL function,
17676** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)".
17677** Expr.x.pSelect is used if the expression is a sub-select or an expression of
17678** the form "<lhs> IN (SELECT ...)". If the EP_xIsSelect bit is set in the
17679** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is
17680** valid.
17681**
17682** An expression of the form ID or ID.ID refers to a column in a table.
17683** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is
17684** the integer cursor number of a VDBE cursor pointing to that table and
17685** Expr.iColumn is the column number for the specific column. If the
17686** expression is used as a result in an aggregate SELECT, then the
17687** value is also stored in the Expr.iAgg column in the aggregate so that
17688** it can be accessed after all aggregates are computed.
17689**
17690** If the expression is an unbound variable marker (a question mark
17691** character '?' in the original SQL) then the Expr.iTable holds the index
17692** number for that variable.
17693**
17694** If the expression is a subquery then Expr.iColumn holds an integer
17695** register number containing the result of the subquery. If the
17696** subquery gives a constant result, then iTable is -1. If the subquery
17697** gives a different answer at different times during statement processing
17698** then iTable is the address of a subroutine that computes the subquery.
17699**
17700** If the Expr is of type OP_Column, and the table it is selecting from
17701** is a disk table or the "old.*" pseudo-table, then pTab points to the
17702** corresponding table definition.
17703**
17704** ALLOCATION NOTES:
17705**
17706** Expr objects can use a lot of memory space in database schema. To
17707** help reduce memory requirements, sometimes an Expr object will be
17708** truncated. And to reduce the number of memory allocations, sometimes
17709** two or more Expr objects will be stored in a single memory allocation,
17710** together with Expr.u.zToken strings.
17711**
17712** If the EP_Reduced and EP_TokenOnly flags are set when
17713** an Expr object is truncated. When EP_Reduced is set, then all
17714** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees
17715** are contained within the same memory allocation. Note, however, that
17716** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
17717** allocated, regardless of whether or not EP_Reduced is set.
17718*/
17719struct Expr {
17720 u8 op; /* Operation performed by this node */
17721 char affExpr; /* affinity, or RAISE type */
17722 u8 op2; /* TK_REGISTER/TK_TRUTH: original value of Expr.op
17723 ** TK_COLUMN: the value of p5 for OP_Column
17724 ** TK_AGG_FUNCTION: nesting depth
17725 ** TK_FUNCTION: NC_SelfRef flag if needs OP_PureFunc */
17726#ifdef SQLITE_DEBUG
17727 u8 vvaFlags; /* Verification flags. */
17728#endif
17729 u32 flags; /* Various flags. EP_* See below */
17730 union {
17731 char *zToken; /* Token value. Zero terminated and dequoted */
17732 int iValue; /* Non-negative integer value if EP_IntValue */
17733 } u;
17734
17735 /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
17736 ** space is allocated for the fields below this point. An attempt to
17737 ** access them will result in a segfault or malfunction.
17738 *********************************************************************/
17739
17740 Expr *pLeft; /* Left subnode */
17741 Expr *pRight; /* Right subnode */
17742 union {
17743 ExprList *pList; /* op = IN, EXISTS, SELECT, CASE, FUNCTION, BETWEEN */
17744 Select *pSelect; /* EP_xIsSelect and op = IN, EXISTS, SELECT */
17745 } x;
17746
17747 /* If the EP_Reduced flag is set in the Expr.flags mask, then no
17748 ** space is allocated for the fields below this point. An attempt to
17749 ** access them will result in a segfault or malfunction.
17750 *********************************************************************/
17751
17752#if SQLITE_MAX_EXPR_DEPTH>0
17753 int nHeight; /* Height of the tree headed by this node */
17754#endif
17755 int iTable; /* TK_COLUMN: cursor number of table holding column
17756 ** TK_REGISTER: register number
17757 ** TK_TRIGGER: 1 -> new, 0 -> old
17758 ** EP_Unlikely: 134217728 times likelihood
17759 ** TK_IN: ephemerial table holding RHS
17760 ** TK_SELECT_COLUMN: Number of columns on the LHS
17761 ** TK_SELECT: 1st register of result vector */
17762 ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
17763 ** TK_VARIABLE: variable number (always >= 1).
17764 ** TK_SELECT_COLUMN: column of the result vector */
17765 i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
17766 int iRightJoinTable; /* If EP_FromJoin, the right table of the join */
17767 AggInfo *pAggInfo; /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
17768 union {
17769 Table *pTab; /* TK_COLUMN: Table containing column. Can be NULL
17770 ** for a column of an index on an expression */
17771 Window *pWin; /* EP_WinFunc: Window/Filter defn for a function */
17772 struct { /* TK_IN, TK_SELECT, and TK_EXISTS */
17773 int iAddr; /* Subroutine entry address */
17774 int regReturn; /* Register used to hold return address */
17775 } sub;
17776 } y;
17777};
17778
17779/* The following are the meanings of bits in the Expr.flags field.
17780** Value restrictions:
17781**
17782** EP_Agg == NC_HasAgg == SF_HasAgg
17783** EP_Win == NC_HasWin
17784*/
17785#define EP_FromJoin 0x000001 /* Originates in ON/USING clause of outer join */
17786#define EP_Distinct 0x000002 /* Aggregate function with DISTINCT keyword */
17787#define EP_HasFunc 0x000004 /* Contains one or more functions of any kind */
17788#define EP_FixedCol 0x000008 /* TK_Column with a known fixed value */
17789#define EP_Agg 0x000010 /* Contains one or more aggregate functions */
17790#define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */
17791#define EP_DblQuoted 0x000040 /* token.z was originally in "..." */
17792#define EP_InfixFunc 0x000080 /* True for an infix function: LIKE, GLOB, etc */
17793#define EP_Collate 0x000100 /* Tree contains a TK_COLLATE operator */
17794#define EP_Commuted 0x000200 /* Comparison operator has been commuted */
17795#define EP_IntValue 0x000400 /* Integer value contained in u.iValue */
17796#define EP_xIsSelect 0x000800 /* x.pSelect is valid (otherwise x.pList is) */
17797#define EP_Skip 0x001000 /* Operator does not contribute to affinity */
17798#define EP_Reduced 0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
17799#define EP_TokenOnly 0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
17800#define EP_Win 0x008000 /* Contains window functions */
17801#define EP_MemToken 0x010000 /* Need to sqlite3DbFree() Expr.zToken */
17802#define EP_IfNullRow 0x020000 /* The TK_IF_NULL_ROW opcode */
17803#define EP_Unlikely 0x040000 /* unlikely() or likelihood() function */
17804#define EP_ConstFunc 0x080000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
17805#define EP_CanBeNull 0x100000 /* Can be null despite NOT NULL constraint */
17806#define EP_Subquery 0x200000 /* Tree contains a TK_SELECT operator */
17807 /* 0x400000 // Available */
17808#define EP_Leaf 0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */
17809#define EP_WinFunc 0x1000000 /* TK_FUNCTION with Expr.y.pWin set */
17810#define EP_Subrtn 0x2000000 /* Uses Expr.y.sub. TK_IN, _SELECT, or _EXISTS */
17811#define EP_Quoted 0x4000000 /* TK_ID was originally quoted */
17812#define EP_Static 0x8000000 /* Held in memory not obtained from malloc() */
17813#define EP_IsTrue 0x10000000 /* Always has boolean value of TRUE */
17814#define EP_IsFalse 0x20000000 /* Always has boolean value of FALSE */
17815#define EP_FromDDL 0x40000000 /* Originates from sqlite_schema */
17816 /* 0x80000000 // Available */
17817
17818/* The EP_Propagate mask is a set of properties that automatically propagate
17819** upwards into parent nodes.
17820*/
17821#define EP_Propagate (EP_Collate|EP_Subquery|EP_HasFunc)
17822
17823/* Macros can be used to test, set, or clear bits in the
17824** Expr.flags field.
17825*/
17826#define ExprHasProperty(E,P) (((E)->flags&(P))!=0)
17827#define ExprHasAllProperty(E,P) (((E)->flags&(P))==(P))
17828#define ExprSetProperty(E,P) (E)->flags|=(P)
17829#define ExprClearProperty(E,P) (E)->flags&=~(P)
17830#define ExprAlwaysTrue(E) (((E)->flags&(EP_FromJoin|EP_IsTrue))==EP_IsTrue)
17831#define ExprAlwaysFalse(E) (((E)->flags&(EP_FromJoin|EP_IsFalse))==EP_IsFalse)
17832
17833/* Macros used to ensure that the correct members of unions are accessed
17834** in Expr.
17835*/
17836#define ExprUseUToken(E) (((E)->flags&EP_IntValue)==0)
17837#define ExprUseUValue(E) (((E)->flags&EP_IntValue)!=0)
17838#define ExprUseXList(E) (((E)->flags&EP_xIsSelect)==0)
17839#define ExprUseXSelect(E) (((E)->flags&EP_xIsSelect)!=0)
17840#define ExprUseYTab(E) (((E)->flags&(EP_WinFunc|EP_Subrtn))==0)
17841#define ExprUseYWin(E) (((E)->flags&EP_WinFunc)!=0)
17842#define ExprUseYSub(E) (((E)->flags&EP_Subrtn)!=0)
17843
17844/* Flags for use with Expr.vvaFlags
17845*/
17846#define EP_NoReduce 0x01 /* Cannot EXPRDUP_REDUCE this Expr */
17847#define EP_Immutable 0x02 /* Do not change this Expr node */
17848
17849/* The ExprSetVVAProperty() macro is used for Verification, Validation,
17850** and Accreditation only. It works like ExprSetProperty() during VVA
17851** processes but is a no-op for delivery.
17852*/
17853#ifdef SQLITE_DEBUG
17854# define ExprSetVVAProperty(E,P) (E)->vvaFlags|=(P)
17855# define ExprHasVVAProperty(E,P) (((E)->vvaFlags&(P))!=0)
17856# define ExprClearVVAProperties(E) (E)->vvaFlags = 0
17857#else
17858# define ExprSetVVAProperty(E,P)
17859# define ExprHasVVAProperty(E,P) 0
17860# define ExprClearVVAProperties(E)
17861#endif
17862
17863/*
17864** Macros to determine the number of bytes required by a normal Expr
17865** struct, an Expr struct with the EP_Reduced flag set in Expr.flags
17866** and an Expr struct with the EP_TokenOnly flag set.
17867*/
17868#define EXPR_FULLSIZE sizeof(Expr) /* Full size */
17869#define EXPR_REDUCEDSIZE offsetof(Expr,iTable) /* Common features */
17870#define EXPR_TOKENONLYSIZE offsetof(Expr,pLeft) /* Fewer features */
17871
17872/*
17873** Flags passed to the sqlite3ExprDup() function. See the header comment
17874** above sqlite3ExprDup() for details.
17875*/
17876#define EXPRDUP_REDUCE 0x0001 /* Used reduced-size Expr nodes */
17877
17878/*
17879** True if the expression passed as an argument was a function with
17880** an OVER() clause (a window function).
17881*/
17882#ifdef SQLITE_OMIT_WINDOWFUNC
17883# define IsWindowFunc(p) 0
17884#else
17885# define IsWindowFunc(p) ( \
17886 ExprHasProperty((p), EP_WinFunc) && p->y.pWin->eFrmType!=TK_FILTER \
17887 )
17888#endif
17889
17890/*
17891** A list of expressions. Each expression may optionally have a
17892** name. An expr/name combination can be used in several ways, such
17893** as the list of "expr AS ID" fields following a "SELECT" or in the
17894** list of "ID = expr" items in an UPDATE. A list of expressions can
17895** also be used as the argument to a function, in which case the a.zName
17896** field is not used.
17897**
17898** In order to try to keep memory usage down, the Expr.a.zEName field
17899** is used for multiple purposes:
17900**
17901** eEName Usage
17902** ---------- -------------------------
17903** ENAME_NAME (1) the AS of result set column
17904** (2) COLUMN= of an UPDATE
17905**
17906** ENAME_TAB DB.TABLE.NAME used to resolve names
17907** of subqueries
17908**
17909** ENAME_SPAN Text of the original result set
17910** expression.
17911*/
17912struct ExprList {
17913 int nExpr; /* Number of expressions on the list */
17914 int nAlloc; /* Number of a[] slots allocated */
17915 struct ExprList_item { /* For each expression in the list */
17916 Expr *pExpr; /* The parse tree for this expression */
17917 char *zEName; /* Token associated with this expression */
17918 u8 sortFlags; /* Mask of KEYINFO_ORDER_* flags */
17919 unsigned eEName :2; /* Meaning of zEName */
17920 unsigned done :1; /* A flag to indicate when processing is finished */
17921 unsigned reusable :1; /* Constant expression is reusable */
17922 unsigned bSorterRef :1; /* Defer evaluation until after sorting */
17923 unsigned bNulls: 1; /* True if explicit "NULLS FIRST/LAST" */
17924 union {
17925 struct { /* Used by any ExprList other than Parse.pConsExpr */
17926 u16 iOrderByCol; /* For ORDER BY, column number in result set */
17927 u16 iAlias; /* Index into Parse.aAlias[] for zName */
17928 } x;
17929 int iConstExprReg; /* Register in which Expr value is cached. Used only
17930 ** by Parse.pConstExpr */
17931 } u;
17932 } a[1]; /* One slot for each expression in the list */
17933};
17934
17935/*
17936** Allowed values for Expr.a.eEName
17937*/
17938#define ENAME_NAME 0 /* The AS clause of a result set */
17939#define ENAME_SPAN 1 /* Complete text of the result set expression */
17940#define ENAME_TAB 2 /* "DB.TABLE.NAME" for the result set */
17941
17942/*
17943** An instance of this structure can hold a simple list of identifiers,
17944** such as the list "a,b,c" in the following statements:
17945**
17946** INSERT INTO t(a,b,c) VALUES ...;
17947** CREATE INDEX idx ON t(a,b,c);
17948** CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...;
17949**
17950** The IdList.a.idx field is used when the IdList represents the list of
17951** column names after a table name in an INSERT statement. In the statement
17952**
17953** INSERT INTO t(a,b,c) ...
17954**
17955** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
17956*/
17957struct IdList {
17958 struct IdList_item {
17959 char *zName; /* Name of the identifier */
17960 int idx; /* Index in some Table.aCol[] of a column named zName */
17961 } *a;
17962 int nId; /* Number of identifiers on the list */
17963};
17964
17965/*
17966** The SrcItem object represents a single term in the FROM clause of a query.
17967** The SrcList object is mostly an array of SrcItems.
17968**
17969** Union member validity:
17970**
17971** u1.zIndexedBy fg.isIndexedBy && !fg.isTabFunc
17972** u1.pFuncArg fg.isTabFunc && !fg.isIndexedBy
17973** u2.pIBIndex fg.isIndexedBy && !fg.isCte
17974** u2.pCteUse fg.isCte && !fg.isIndexedBy
17975*/
17976struct SrcItem {
17977 Schema *pSchema; /* Schema to which this item is fixed */
17978 char *zDatabase; /* Name of database holding this table */
17979 char *zName; /* Name of the table */
17980 char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */
17981 Table *pTab; /* An SQL table corresponding to zName */
17982 Select *pSelect; /* A SELECT statement used in place of a table name */
17983 int addrFillSub; /* Address of subroutine to manifest a subquery */
17984 int regReturn; /* Register holding return address of addrFillSub */
17985 int regResult; /* Registers holding results of a co-routine */
17986 struct {
17987 u8 jointype; /* Type of join between this table and the previous */
17988 unsigned notIndexed :1; /* True if there is a NOT INDEXED clause */
17989 unsigned isIndexedBy :1; /* True if there is an INDEXED BY clause */
17990 unsigned isTabFunc :1; /* True if table-valued-function syntax */
17991 unsigned isCorrelated :1; /* True if sub-query is correlated */
17992 unsigned viaCoroutine :1; /* Implemented as a co-routine */
17993 unsigned isRecursive :1; /* True for recursive reference in WITH */
17994 unsigned fromDDL :1; /* Comes from sqlite_schema */
17995 unsigned isCte :1; /* This is a CTE */
17996 unsigned notCte :1; /* This item may not match a CTE */
17997 } fg;
17998 int iCursor; /* The VDBE cursor number used to access this table */
17999 Expr *pOn; /* The ON clause of a join */
18000 IdList *pUsing; /* The USING clause of a join */
18001 Bitmask colUsed; /* Bit N (1<<N) set if column N of pTab is used */
18002 union {
18003 char *zIndexedBy; /* Identifier from "INDEXED BY <zIndex>" clause */
18004 ExprList *pFuncArg; /* Arguments to table-valued-function */
18005 } u1;
18006 union {
18007 Index *pIBIndex; /* Index structure corresponding to u1.zIndexedBy */
18008 CteUse *pCteUse; /* CTE Usage info info fg.isCte is true */
18009 } u2;
18010};
18011
18012/*
18013** The following structure describes the FROM clause of a SELECT statement.
18014** Each table or subquery in the FROM clause is a separate element of
18015** the SrcList.a[] array.
18016**
18017** With the addition of multiple database support, the following structure
18018** can also be used to describe a particular table such as the table that
18019** is modified by an INSERT, DELETE, or UPDATE statement. In standard SQL,
18020** such a table must be a simple name: ID. But in SQLite, the table can
18021** now be identified by a database name, a dot, then the table name: ID.ID.
18022**
18023** The jointype starts out showing the join type between the current table
18024** and the next table on the list. The parser builds the list this way.
18025** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
18026** jointype expresses the join between the table and the previous table.
18027**
18028** In the colUsed field, the high-order bit (bit 63) is set if the table
18029** contains more than 63 columns and the 64-th or later column is used.
18030*/
18031struct SrcList {
18032 int nSrc; /* Number of tables or subqueries in the FROM clause */
18033 u32 nAlloc; /* Number of entries allocated in a[] below */
18034 SrcItem a[1]; /* One entry for each identifier on the list */
18035};
18036
18037/*
18038** Permitted values of the SrcList.a.jointype field
18039*/
18040#define JT_INNER 0x0001 /* Any kind of inner or cross join */
18041#define JT_CROSS 0x0002 /* Explicit use of the CROSS keyword */
18042#define JT_NATURAL 0x0004 /* True for a "natural" join */
18043#define JT_LEFT 0x0008 /* Left outer join */
18044#define JT_RIGHT 0x0010 /* Right outer join */
18045#define JT_OUTER 0x0020 /* The "OUTER" keyword is present */
18046#define JT_ERROR 0x0040 /* unknown or unsupported join type */
18047
18048
18049/*
18050** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
18051** and the WhereInfo.wctrlFlags member.
18052**
18053** Value constraints (enforced via assert()):
18054** WHERE_USE_LIMIT == SF_FixedLimit
18055*/
18056#define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */
18057#define WHERE_ORDERBY_MIN 0x0001 /* ORDER BY processing for min() func */
18058#define WHERE_ORDERBY_MAX 0x0002 /* ORDER BY processing for max() func */
18059#define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */
18060#define WHERE_ONEPASS_MULTIROW 0x0008 /* ONEPASS is ok with multiple rows */
18061#define WHERE_DUPLICATES_OK 0x0010 /* Ok to return a row more than once */
18062#define WHERE_OR_SUBCLAUSE 0x0020 /* Processing a sub-WHERE as part of
18063 ** the OR optimization */
18064#define WHERE_GROUPBY 0x0040 /* pOrderBy is really a GROUP BY */
18065#define WHERE_DISTINCTBY 0x0080 /* pOrderby is really a DISTINCT clause */
18066#define WHERE_WANT_DISTINCT 0x0100 /* All output needs to be distinct */
18067#define WHERE_SORTBYGROUP 0x0200 /* Support sqlite3WhereIsSorted() */
18068#define WHERE_AGG_DISTINCT 0x0400 /* Query is "SELECT agg(DISTINCT ...)" */
18069#define WHERE_ORDERBY_LIMIT 0x0800 /* ORDERBY+LIMIT on the inner loop */
18070 /* 0x1000 not currently used */
18071 /* 0x2000 not currently used */
18072#define WHERE_USE_LIMIT 0x4000 /* Use the LIMIT in cost estimates */
18073 /* 0x8000 not currently used */
18074
18075/* Allowed return values from sqlite3WhereIsDistinct()
18076*/
18077#define WHERE_DISTINCT_NOOP 0 /* DISTINCT keyword not used */
18078#define WHERE_DISTINCT_UNIQUE 1 /* No duplicates */
18079#define WHERE_DISTINCT_ORDERED 2 /* All duplicates are adjacent */
18080#define WHERE_DISTINCT_UNORDERED 3 /* Duplicates are scattered */
18081
18082/*
18083** A NameContext defines a context in which to resolve table and column
18084** names. The context consists of a list of tables (the pSrcList) field and
18085** a list of named expression (pEList). The named expression list may
18086** be NULL. The pSrc corresponds to the FROM clause of a SELECT or
18087** to the table being operated on by INSERT, UPDATE, or DELETE. The
18088** pEList corresponds to the result set of a SELECT and is NULL for
18089** other statements.
18090**
18091** NameContexts can be nested. When resolving names, the inner-most
18092** context is searched first. If no match is found, the next outer
18093** context is checked. If there is still no match, the next context
18094** is checked. This process continues until either a match is found
18095** or all contexts are check. When a match is found, the nRef member of
18096** the context containing the match is incremented.
18097**
18098** Each subquery gets a new NameContext. The pNext field points to the
18099** NameContext in the parent query. Thus the process of scanning the
18100** NameContext list corresponds to searching through successively outer
18101** subqueries looking for a match.
18102*/
18103struct NameContext {
18104 Parse *pParse; /* The parser */
18105 SrcList *pSrcList; /* One or more tables used to resolve names */
18106 union {
18107 ExprList *pEList; /* Optional list of result-set columns */
18108 AggInfo *pAggInfo; /* Information about aggregates at this level */
18109 Upsert *pUpsert; /* ON CONFLICT clause information from an upsert */
18110 int iBaseReg; /* For TK_REGISTER when parsing RETURNING */
18111 } uNC;
18112 NameContext *pNext; /* Next outer name context. NULL for outermost */
18113 int nRef; /* Number of names resolved by this context */
18114 int nNcErr; /* Number of errors encountered while resolving names */
18115 int ncFlags; /* Zero or more NC_* flags defined below */
18116 Select *pWinSelect; /* SELECT statement for any window functions */
18117};
18118
18119/*
18120** Allowed values for the NameContext, ncFlags field.
18121**
18122** Value constraints (all checked via assert()):
18123** NC_HasAgg == SF_HasAgg == EP_Agg
18124** NC_MinMaxAgg == SF_MinMaxAgg == SQLITE_FUNC_MINMAX
18125** NC_OrderAgg == SF_OrderByReqd == SQLITE_FUNC_ANYORDER
18126** NC_HasWin == EP_Win
18127**
18128*/
18129#define NC_AllowAgg 0x000001 /* Aggregate functions are allowed here */
18130#define NC_PartIdx 0x000002 /* True if resolving a partial index WHERE */
18131#define NC_IsCheck 0x000004 /* True if resolving a CHECK constraint */
18132#define NC_GenCol 0x000008 /* True for a GENERATED ALWAYS AS clause */
18133#define NC_HasAgg 0x000010 /* One or more aggregate functions seen */
18134#define NC_IdxExpr 0x000020 /* True if resolving columns of CREATE INDEX */
18135#define NC_SelfRef 0x00002e /* Combo: PartIdx, isCheck, GenCol, and IdxExpr */
18136#define NC_VarSelect 0x000040 /* A correlated subquery has been seen */
18137#define NC_UEList 0x000080 /* True if uNC.pEList is used */
18138#define NC_UAggInfo 0x000100 /* True if uNC.pAggInfo is used */
18139#define NC_UUpsert 0x000200 /* True if uNC.pUpsert is used */
18140#define NC_UBaseReg 0x000400 /* True if uNC.iBaseReg is used */
18141#define NC_MinMaxAgg 0x001000 /* min/max aggregates seen. See note above */
18142#define NC_Complex 0x002000 /* True if a function or subquery seen */
18143#define NC_AllowWin 0x004000 /* Window functions are allowed here */
18144#define NC_HasWin 0x008000 /* One or more window functions seen */
18145#define NC_IsDDL 0x010000 /* Resolving names in a CREATE statement */
18146#define NC_InAggFunc 0x020000 /* True if analyzing arguments to an agg func */
18147#define NC_FromDDL 0x040000 /* SQL text comes from sqlite_schema */
18148#define NC_NoSelect 0x080000 /* Do not descend into sub-selects */
18149#define NC_OrderAgg 0x8000000 /* Has an aggregate other than count/min/max */
18150
18151/*
18152** An instance of the following object describes a single ON CONFLICT
18153** clause in an upsert.
18154**
18155** The pUpsertTarget field is only set if the ON CONFLICT clause includes
18156** conflict-target clause. (In "ON CONFLICT(a,b)" the "(a,b)" is the
18157** conflict-target clause.) The pUpsertTargetWhere is the optional
18158** WHERE clause used to identify partial unique indexes.
18159**
18160** pUpsertSet is the list of column=expr terms of the UPDATE statement.
18161** The pUpsertSet field is NULL for a ON CONFLICT DO NOTHING. The
18162** pUpsertWhere is the WHERE clause for the UPDATE and is NULL if the
18163** WHERE clause is omitted.
18164*/
18165struct Upsert {
18166 ExprList *pUpsertTarget; /* Optional description of conflict target */
18167 Expr *pUpsertTargetWhere; /* WHERE clause for partial index targets */
18168 ExprList *pUpsertSet; /* The SET clause from an ON CONFLICT UPDATE */
18169 Expr *pUpsertWhere; /* WHERE clause for the ON CONFLICT UPDATE */
18170 Upsert *pNextUpsert; /* Next ON CONFLICT clause in the list */
18171 u8 isDoUpdate; /* True for DO UPDATE. False for DO NOTHING */
18172 /* Above this point is the parse tree for the ON CONFLICT clauses.
18173 ** The next group of fields stores intermediate data. */
18174 void *pToFree; /* Free memory when deleting the Upsert object */
18175 /* All fields above are owned by the Upsert object and must be freed
18176 ** when the Upsert is destroyed. The fields below are used to transfer
18177 ** information from the INSERT processing down into the UPDATE processing
18178 ** while generating code. The fields below are owned by the INSERT
18179 ** statement and will be freed by INSERT processing. */
18180 Index *pUpsertIdx; /* UNIQUE constraint specified by pUpsertTarget */
18181 SrcList *pUpsertSrc; /* Table to be updated */
18182 int regData; /* First register holding array of VALUES */
18183 int iDataCur; /* Index of the data cursor */
18184 int iIdxCur; /* Index of the first index cursor */
18185};
18186
18187/*
18188** An instance of the following structure contains all information
18189** needed to generate code for a single SELECT statement.
18190**
18191** See the header comment on the computeLimitRegisters() routine for a
18192** detailed description of the meaning of the iLimit and iOffset fields.
18193**
18194** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
18195** These addresses must be stored so that we can go back and fill in
18196** the P4_KEYINFO and P2 parameters later. Neither the KeyInfo nor
18197** the number of columns in P2 can be computed at the same time
18198** as the OP_OpenEphm instruction is coded because not
18199** enough information about the compound query is known at that point.
18200** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
18201** for the result set. The KeyInfo for addrOpenEphm[2] contains collating
18202** sequences for the ORDER BY clause.
18203*/
18204struct Select {
18205 u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
18206 LogEst nSelectRow; /* Estimated number of result rows */
18207 u32 selFlags; /* Various SF_* values */
18208 int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */
18209 u32 selId; /* Unique identifier number for this SELECT */
18210 int addrOpenEphm[2]; /* OP_OpenEphem opcodes related to this select */
18211 ExprList *pEList; /* The fields of the result */
18212 SrcList *pSrc; /* The FROM clause */
18213 Expr *pWhere; /* The WHERE clause */
18214 ExprList *pGroupBy; /* The GROUP BY clause */
18215 Expr *pHaving; /* The HAVING clause */
18216 ExprList *pOrderBy; /* The ORDER BY clause */
18217 Select *pPrior; /* Prior select in a compound select statement */
18218 Select *pNext; /* Next select to the left in a compound */
18219 Expr *pLimit; /* LIMIT expression. NULL means not used. */
18220 With *pWith; /* WITH clause attached to this select. Or NULL. */
18221#ifndef SQLITE_OMIT_WINDOWFUNC
18222 Window *pWin; /* List of window functions */
18223 Window *pWinDefn; /* List of named window definitions */
18224#endif
18225};
18226
18227/*
18228** Allowed values for Select.selFlags. The "SF" prefix stands for
18229** "Select Flag".
18230**
18231** Value constraints (all checked via assert())
18232** SF_HasAgg == NC_HasAgg
18233** SF_MinMaxAgg == NC_MinMaxAgg == SQLITE_FUNC_MINMAX
18234** SF_OrderByReqd == NC_OrderAgg == SQLITE_FUNC_ANYORDER
18235** SF_FixedLimit == WHERE_USE_LIMIT
18236*/
18237#define SF_Distinct 0x0000001 /* Output should be DISTINCT */
18238#define SF_All 0x0000002 /* Includes the ALL keyword */
18239#define SF_Resolved 0x0000004 /* Identifiers have been resolved */
18240#define SF_Aggregate 0x0000008 /* Contains agg functions or a GROUP BY */
18241#define SF_HasAgg 0x0000010 /* Contains aggregate functions */
18242#define SF_UsesEphemeral 0x0000020 /* Uses the OpenEphemeral opcode */
18243#define SF_Expanded 0x0000040 /* sqlite3SelectExpand() called on this */
18244#define SF_HasTypeInfo 0x0000080 /* FROM subqueries have Table metadata */
18245#define SF_Compound 0x0000100 /* Part of a compound query */
18246#define SF_Values 0x0000200 /* Synthesized from VALUES clause */
18247#define SF_MultiValue 0x0000400 /* Single VALUES term with multiple rows */
18248#define SF_NestedFrom 0x0000800 /* Part of a parenthesized FROM clause */
18249#define SF_MinMaxAgg 0x0001000 /* Aggregate containing min() or max() */
18250#define SF_Recursive 0x0002000 /* The recursive part of a recursive CTE */
18251#define SF_FixedLimit 0x0004000 /* nSelectRow set by a constant LIMIT */
18252#define SF_MaybeConvert 0x0008000 /* Need convertCompoundSelectToSubquery() */
18253#define SF_Converted 0x0010000 /* By convertCompoundSelectToSubquery() */
18254#define SF_IncludeHidden 0x0020000 /* Include hidden columns in output */
18255#define SF_ComplexResult 0x0040000 /* Result contains subquery or function */
18256#define SF_WhereBegin 0x0080000 /* Really a WhereBegin() call. Debug Only */
18257#define SF_WinRewrite 0x0100000 /* Window function rewrite accomplished */
18258#define SF_View 0x0200000 /* SELECT statement is a view */
18259#define SF_NoopOrderBy 0x0400000 /* ORDER BY is ignored for this query */
18260#define SF_UFSrcCheck 0x0800000 /* Check pSrc as required by UPDATE...FROM */
18261#define SF_PushDown 0x1000000 /* SELECT has be modified by push-down opt */
18262#define SF_MultiPart 0x2000000 /* Has multiple incompatible PARTITIONs */
18263#define SF_CopyCte 0x4000000 /* SELECT statement is a copy of a CTE */
18264#define SF_OrderByReqd 0x8000000 /* The ORDER BY clause may not be omitted */
18265
18266/*
18267** The results of a SELECT can be distributed in several ways, as defined
18268** by one of the following macros. The "SRT" prefix means "SELECT Result
18269** Type".
18270**
18271** SRT_Union Store results as a key in a temporary index
18272** identified by pDest->iSDParm.
18273**
18274** SRT_Except Remove results from the temporary index pDest->iSDParm.
18275**
18276** SRT_Exists Store a 1 in memory cell pDest->iSDParm if the result
18277** set is not empty.
18278**
18279** SRT_Discard Throw the results away. This is used by SELECT
18280** statements within triggers whose only purpose is
18281** the side-effects of functions.
18282**
18283** SRT_Output Generate a row of output (using the OP_ResultRow
18284** opcode) for each row in the result set.
18285**
18286** SRT_Mem Only valid if the result is a single column.
18287** Store the first column of the first result row
18288** in register pDest->iSDParm then abandon the rest
18289** of the query. This destination implies "LIMIT 1".
18290**
18291** SRT_Set The result must be a single column. Store each
18292** row of result as the key in table pDest->iSDParm.
18293** Apply the affinity pDest->affSdst before storing
18294** results. Used to implement "IN (SELECT ...)".
18295**
18296** SRT_EphemTab Create an temporary table pDest->iSDParm and store
18297** the result there. The cursor is left open after
18298** returning. This is like SRT_Table except that
18299** this destination uses OP_OpenEphemeral to create
18300** the table first.
18301**
18302** SRT_Coroutine Generate a co-routine that returns a new row of
18303** results each time it is invoked. The entry point
18304** of the co-routine is stored in register pDest->iSDParm
18305** and the result row is stored in pDest->nDest registers
18306** starting with pDest->iSdst.
18307**
18308** SRT_Table Store results in temporary table pDest->iSDParm.
18309** SRT_Fifo This is like SRT_EphemTab except that the table
18310** is assumed to already be open. SRT_Fifo has
18311** the additional property of being able to ignore
18312** the ORDER BY clause.
18313**
18314** SRT_DistFifo Store results in a temporary table pDest->iSDParm.
18315** But also use temporary table pDest->iSDParm+1 as
18316** a record of all prior results and ignore any duplicate
18317** rows. Name means: "Distinct Fifo".
18318**
18319** SRT_Queue Store results in priority queue pDest->iSDParm (really
18320** an index). Append a sequence number so that all entries
18321** are distinct.
18322**
18323** SRT_DistQueue Store results in priority queue pDest->iSDParm only if
18324** the same record has never been stored before. The
18325** index at pDest->iSDParm+1 hold all prior stores.
18326**
18327** SRT_Upfrom Store results in the temporary table already opened by
18328** pDest->iSDParm. If (pDest->iSDParm<0), then the temp
18329** table is an intkey table - in this case the first
18330** column returned by the SELECT is used as the integer
18331** key. If (pDest->iSDParm>0), then the table is an index
18332** table. (pDest->iSDParm) is the number of key columns in
18333** each index record in this case.
18334*/
18335#define SRT_Union 1 /* Store result as keys in an index */
18336#define SRT_Except 2 /* Remove result from a UNION index */
18337#define SRT_Exists 3 /* Store 1 if the result is not empty */
18338#define SRT_Discard 4 /* Do not save the results anywhere */
18339#define SRT_DistFifo 5 /* Like SRT_Fifo, but unique results only */
18340#define SRT_DistQueue 6 /* Like SRT_Queue, but unique results only */
18341
18342/* The DISTINCT clause is ignored for all of the above. Not that
18343** IgnorableDistinct() implies IgnorableOrderby() */
18344#define IgnorableDistinct(X) ((X->eDest)<=SRT_DistQueue)
18345
18346#define SRT_Queue 7 /* Store result in an queue */
18347#define SRT_Fifo 8 /* Store result as data with an automatic rowid */
18348
18349/* The ORDER BY clause is ignored for all of the above */
18350#define IgnorableOrderby(X) ((X->eDest)<=SRT_Fifo)
18351
18352#define SRT_Output 9 /* Output each row of result */
18353#define SRT_Mem 10 /* Store result in a memory cell */
18354#define SRT_Set 11 /* Store results as keys in an index */
18355#define SRT_EphemTab 12 /* Create transient tab and store like SRT_Table */
18356#define SRT_Coroutine 13 /* Generate a single row of result */
18357#define SRT_Table 14 /* Store result as data with an automatic rowid */
18358#define SRT_Upfrom 15 /* Store result as data with rowid */
18359
18360/*
18361** An instance of this object describes where to put of the results of
18362** a SELECT statement.
18363*/
18364struct SelectDest {
18365 u8 eDest; /* How to dispose of the results. One of SRT_* above. */
18366 int iSDParm; /* A parameter used by the eDest disposal method */
18367 int iSDParm2; /* A second parameter for the eDest disposal method */
18368 int iSdst; /* Base register where results are written */
18369 int nSdst; /* Number of registers allocated */
18370 char *zAffSdst; /* Affinity used when eDest==SRT_Set */
18371 ExprList *pOrderBy; /* Key columns for SRT_Queue and SRT_DistQueue */
18372};
18373
18374/*
18375** During code generation of statements that do inserts into AUTOINCREMENT
18376** tables, the following information is attached to the Table.u.autoInc.p
18377** pointer of each autoincrement table to record some side information that
18378** the code generator needs. We have to keep per-table autoincrement
18379** information in case inserts are done within triggers. Triggers do not
18380** normally coordinate their activities, but we do need to coordinate the
18381** loading and saving of autoincrement information.
18382*/
18383struct AutoincInfo {
18384 AutoincInfo *pNext; /* Next info block in a list of them all */
18385 Table *pTab; /* Table this info block refers to */
18386 int iDb; /* Index in sqlite3.aDb[] of database holding pTab */
18387 int regCtr; /* Memory register holding the rowid counter */
18388};
18389
18390/*
18391** At least one instance of the following structure is created for each
18392** trigger that may be fired while parsing an INSERT, UPDATE or DELETE
18393** statement. All such objects are stored in the linked list headed at
18394** Parse.pTriggerPrg and deleted once statement compilation has been
18395** completed.
18396**
18397** A Vdbe sub-program that implements the body and WHEN clause of trigger
18398** TriggerPrg.pTrigger, assuming a default ON CONFLICT clause of
18399** TriggerPrg.orconf, is stored in the TriggerPrg.pProgram variable.
18400** The Parse.pTriggerPrg list never contains two entries with the same
18401** values for both pTrigger and orconf.
18402**
18403** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns
18404** accessed (or set to 0 for triggers fired as a result of INSERT
18405** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to
18406** a mask of new.* columns used by the program.
18407*/
18408struct TriggerPrg {
18409 Trigger *pTrigger; /* Trigger this program was coded from */
18410 TriggerPrg *pNext; /* Next entry in Parse.pTriggerPrg list */
18411 SubProgram *pProgram; /* Program implementing pTrigger/orconf */
18412 int orconf; /* Default ON CONFLICT policy */
18413 u32 aColmask[2]; /* Masks of old.*, new.* columns accessed */
18414};
18415
18416/*
18417** The yDbMask datatype for the bitmask of all attached databases.
18418*/
18419#if SQLITE_MAX_ATTACHED>30
18420 typedef unsigned char yDbMask[(SQLITE_MAX_ATTACHED+9)/8];
18421# define DbMaskTest(M,I) (((M)[(I)/8]&(1<<((I)&7)))!=0)
18422# define DbMaskZero(M) memset((M),0,sizeof(M))
18423# define DbMaskSet(M,I) (M)[(I)/8]|=(1<<((I)&7))
18424# define DbMaskAllZero(M) sqlite3DbMaskAllZero(M)
18425# define DbMaskNonZero(M) (sqlite3DbMaskAllZero(M)==0)
18426#else
18427 typedef unsigned int yDbMask;
18428# define DbMaskTest(M,I) (((M)&(((yDbMask)1)<<(I)))!=0)
18429# define DbMaskZero(M) (M)=0
18430# define DbMaskSet(M,I) (M)|=(((yDbMask)1)<<(I))
18431# define DbMaskAllZero(M) (M)==0
18432# define DbMaskNonZero(M) (M)!=0
18433#endif
18434
18435/*
18436** An instance of the ParseCleanup object specifies an operation that
18437** should be performed after parsing to deallocation resources obtained
18438** during the parse and which are no longer needed.
18439*/
18440struct ParseCleanup {
18441 ParseCleanup *pNext; /* Next cleanup task */
18442 void *pPtr; /* Pointer to object to deallocate */
18443 void (*xCleanup)(sqlite3*,void*); /* Deallocation routine */
18444};
18445
18446/*
18447** An SQL parser context. A copy of this structure is passed through
18448** the parser and down into all the parser action routine in order to
18449** carry around information that is global to the entire parse.
18450**
18451** The structure is divided into two parts. When the parser and code
18452** generate call themselves recursively, the first part of the structure
18453** is constant but the second part is reset at the beginning and end of
18454** each recursion.
18455**
18456** The nTableLock and aTableLock variables are only used if the shared-cache
18457** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
18458** used to store the set of table-locks required by the statement being
18459** compiled. Function sqlite3TableLock() is used to add entries to the
18460** list.
18461*/
18462struct Parse {
18463 sqlite3 *db; /* The main database structure */
18464 char *zErrMsg; /* An error message */
18465 Vdbe *pVdbe; /* An engine for executing database bytecode */
18466 int rc; /* Return code from execution */
18467 u8 colNamesSet; /* TRUE after OP_ColumnName has been issued to pVdbe */
18468 u8 checkSchema; /* Causes schema cookie check after an error */
18469 u8 nested; /* Number of nested calls to the parser/code generator */
18470 u8 nTempReg; /* Number of temporary registers in aTempReg[] */
18471 u8 isMultiWrite; /* True if statement may modify/insert multiple rows */
18472 u8 mayAbort; /* True if statement may throw an ABORT exception */
18473 u8 hasCompound; /* Need to invoke convertCompoundSelectToSubquery() */
18474 u8 okConstFactor; /* OK to factor out constants */
18475 u8 disableLookaside; /* Number of times lookaside has been disabled */
18476 u8 disableVtab; /* Disable all virtual tables for this parse */
18477#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
18478 u8 earlyCleanup; /* OOM inside sqlite3ParserAddCleanup() */
18479#endif
18480 int nRangeReg; /* Size of the temporary register block */
18481 int iRangeReg; /* First register in temporary register block */
18482 int nErr; /* Number of errors seen */
18483 int nTab; /* Number of previously allocated VDBE cursors */
18484 int nMem; /* Number of memory cells used so far */
18485 int szOpAlloc; /* Bytes of memory space allocated for Vdbe.aOp[] */
18486 int iSelfTab; /* Table associated with an index on expr, or negative
18487 ** of the base register during check-constraint eval */
18488 int nLabel; /* The *negative* of the number of labels used */
18489 int nLabelAlloc; /* Number of slots in aLabel */
18490 int *aLabel; /* Space to hold the labels */
18491 ExprList *pConstExpr;/* Constant expressions */
18492 Token constraintName;/* Name of the constraint currently being parsed */
18493 yDbMask writeMask; /* Start a write transaction on these databases */
18494 yDbMask cookieMask; /* Bitmask of schema verified databases */
18495 int regRowid; /* Register holding rowid of CREATE TABLE entry */
18496 int regRoot; /* Register holding root page number for new objects */
18497 int nMaxArg; /* Max args passed to user function by sub-program */
18498 int nSelect; /* Number of SELECT stmts. Counter for Select.selId */
18499#ifndef SQLITE_OMIT_SHARED_CACHE
18500 int nTableLock; /* Number of locks in aTableLock */
18501 TableLock *aTableLock; /* Required table locks for shared-cache mode */
18502#endif
18503 AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */
18504 Parse *pToplevel; /* Parse structure for main program (or NULL) */
18505 Table *pTriggerTab; /* Table triggers are being coded for */
18506 TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */
18507 ParseCleanup *pCleanup; /* List of cleanup operations to run after parse */
18508 union {
18509 int addrCrTab; /* Address of OP_CreateBtree on CREATE TABLE */
18510 Returning *pReturning; /* The RETURNING clause */
18511 } u1;
18512 u32 nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */
18513 u32 oldmask; /* Mask of old.* columns referenced */
18514 u32 newmask; /* Mask of new.* columns referenced */
18515 u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */
18516 u8 bReturning; /* Coding a RETURNING trigger */
18517 u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */
18518 u8 disableTriggers; /* True to disable triggers */
18519
18520 /**************************************************************************
18521 ** Fields above must be initialized to zero. The fields that follow,
18522 ** down to the beginning of the recursive section, do not need to be
18523 ** initialized as they will be set before being used. The boundary is
18524 ** determined by offsetof(Parse,aTempReg).
18525 **************************************************************************/
18526
18527 int aTempReg[8]; /* Holding area for temporary registers */
18528 Token sNameToken; /* Token with unqualified schema object name */
18529
18530 /************************************************************************
18531 ** Above is constant between recursions. Below is reset before and after
18532 ** each recursion. The boundary between these two regions is determined
18533 ** using offsetof(Parse,sLastToken) so the sLastToken field must be the
18534 ** first field in the recursive region.
18535 ************************************************************************/
18536
18537 Token sLastToken; /* The last token parsed */
18538 ynVar nVar; /* Number of '?' variables seen in the SQL so far */
18539 u8 iPkSortOrder; /* ASC or DESC for INTEGER PRIMARY KEY */
18540 u8 explain; /* True if the EXPLAIN flag is found on the query */
18541 u8 eParseMode; /* PARSE_MODE_XXX constant */
18542#ifndef SQLITE_OMIT_VIRTUALTABLE
18543 int nVtabLock; /* Number of virtual tables to lock */
18544#endif
18545 int nHeight; /* Expression tree height of current sub-select */
18546#ifndef SQLITE_OMIT_EXPLAIN
18547 int addrExplain; /* Address of current OP_Explain opcode */
18548#endif
18549 VList *pVList; /* Mapping between variable names and numbers */
18550 Vdbe *pReprepare; /* VM being reprepared (sqlite3Reprepare()) */
18551 const char *zTail; /* All SQL text past the last semicolon parsed */
18552 Table *pNewTable; /* A table being constructed by CREATE TABLE */
18553 Index *pNewIndex; /* An index being constructed by CREATE INDEX.
18554 ** Also used to hold redundant UNIQUE constraints
18555 ** during a RENAME COLUMN */
18556 Trigger *pNewTrigger; /* Trigger under construct by a CREATE TRIGGER */
18557 const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
18558#ifndef SQLITE_OMIT_VIRTUALTABLE
18559 Token sArg; /* Complete text of a module argument */
18560 Table **apVtabLock; /* Pointer to virtual tables needing locking */
18561#endif
18562 With *pWith; /* Current WITH clause, or NULL */
18563#ifndef SQLITE_OMIT_ALTERTABLE
18564 RenameToken *pRename; /* Tokens subject to renaming by ALTER TABLE */
18565#endif
18566};
18567
18568/* Allowed values for Parse.eParseMode
18569*/
18570#define PARSE_MODE_NORMAL 0
18571#define PARSE_MODE_DECLARE_VTAB 1
18572#define PARSE_MODE_RENAME 2
18573#define PARSE_MODE_UNMAP 3
18574
18575/*
18576** Sizes and pointers of various parts of the Parse object.
18577*/
18578#define PARSE_HDR_SZ offsetof(Parse,aTempReg) /* Recursive part w/o aColCache*/
18579#define PARSE_RECURSE_SZ offsetof(Parse,sLastToken) /* Recursive part */
18580#define PARSE_TAIL_SZ (sizeof(Parse)-PARSE_RECURSE_SZ) /* Non-recursive part */
18581#define PARSE_TAIL(X) (((char*)(X))+PARSE_RECURSE_SZ) /* Pointer to tail */
18582
18583/*
18584** Return true if currently inside an sqlite3_declare_vtab() call.
18585*/
18586#ifdef SQLITE_OMIT_VIRTUALTABLE
18587 #define IN_DECLARE_VTAB 0
18588#else
18589 #define IN_DECLARE_VTAB (pParse->eParseMode==PARSE_MODE_DECLARE_VTAB)
18590#endif
18591
18592#if defined(SQLITE_OMIT_ALTERTABLE)
18593 #define IN_RENAME_OBJECT 0
18594#else
18595 #define IN_RENAME_OBJECT (pParse->eParseMode>=PARSE_MODE_RENAME)
18596#endif
18597
18598#if defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_OMIT_ALTERTABLE)
18599 #define IN_SPECIAL_PARSE 0
18600#else
18601 #define IN_SPECIAL_PARSE (pParse->eParseMode!=PARSE_MODE_NORMAL)
18602#endif
18603
18604/*
18605** An instance of the following structure can be declared on a stack and used
18606** to save the Parse.zAuthContext value so that it can be restored later.
18607*/
18608struct AuthContext {
18609 const char *zAuthContext; /* Put saved Parse.zAuthContext here */
18610 Parse *pParse; /* The Parse structure */
18611};
18612
18613/*
18614** Bitfield flags for P5 value in various opcodes.
18615**
18616** Value constraints (enforced via assert()):
18617** OPFLAG_LENGTHARG == SQLITE_FUNC_LENGTH
18618** OPFLAG_TYPEOFARG == SQLITE_FUNC_TYPEOF
18619** OPFLAG_BULKCSR == BTREE_BULKLOAD
18620** OPFLAG_SEEKEQ == BTREE_SEEK_EQ
18621** OPFLAG_FORDELETE == BTREE_FORDELETE
18622** OPFLAG_SAVEPOSITION == BTREE_SAVEPOSITION
18623** OPFLAG_AUXDELETE == BTREE_AUXDELETE
18624*/
18625#define OPFLAG_NCHANGE 0x01 /* OP_Insert: Set to update db->nChange */
18626 /* Also used in P2 (not P5) of OP_Delete */
18627#define OPFLAG_NOCHNG 0x01 /* OP_VColumn nochange for UPDATE */
18628#define OPFLAG_EPHEM 0x01 /* OP_Column: Ephemeral output is ok */
18629#define OPFLAG_LASTROWID 0x20 /* Set to update db->lastRowid */
18630#define OPFLAG_ISUPDATE 0x04 /* This OP_Insert is an sql UPDATE */
18631#define OPFLAG_APPEND 0x08 /* This is likely to be an append */
18632#define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */
18633#define OPFLAG_ISNOOP 0x40 /* OP_Delete does pre-update-hook only */
18634#define OPFLAG_LENGTHARG 0x40 /* OP_Column only used for length() */
18635#define OPFLAG_TYPEOFARG 0x80 /* OP_Column only used for typeof() */
18636#define OPFLAG_BULKCSR 0x01 /* OP_Open** used to open bulk cursor */
18637#define OPFLAG_SEEKEQ 0x02 /* OP_Open** cursor uses EQ seek only */
18638#define OPFLAG_FORDELETE 0x08 /* OP_Open should use BTREE_FORDELETE */
18639#define OPFLAG_P2ISREG 0x10 /* P2 to OP_Open** is a register number */
18640#define OPFLAG_PERMUTE 0x01 /* OP_Compare: use the permutation */
18641#define OPFLAG_SAVEPOSITION 0x02 /* OP_Delete/Insert: save cursor pos */
18642#define OPFLAG_AUXDELETE 0x04 /* OP_Delete: index in a DELETE op */
18643#define OPFLAG_NOCHNG_MAGIC 0x6d /* OP_MakeRecord: serialtype 10 is ok */
18644#define OPFLAG_PREFORMAT 0x80 /* OP_Insert uses preformatted cell */
18645
18646/*
18647 * Each trigger present in the database schema is stored as an instance of
18648 * struct Trigger.
18649 *
18650 * Pointers to instances of struct Trigger are stored in two ways.
18651 * 1. In the "trigHash" hash table (part of the sqlite3* that represents the
18652 * database). This allows Trigger structures to be retrieved by name.
18653 * 2. All triggers associated with a single table form a linked list, using the
18654 * pNext member of struct Trigger. A pointer to the first element of the
18655 * linked list is stored as the "pTrigger" member of the associated
18656 * struct Table.
18657 *
18658 * The "step_list" member points to the first element of a linked list
18659 * containing the SQL statements specified as the trigger program.
18660 */
18661struct Trigger {
18662 char *zName; /* The name of the trigger */
18663 char *table; /* The table or view to which the trigger applies */
18664 u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT */
18665 u8 tr_tm; /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
18666 u8 bReturning; /* This trigger implements a RETURNING clause */
18667 Expr *pWhen; /* The WHEN clause of the expression (may be NULL) */
18668 IdList *pColumns; /* If this is an UPDATE OF <column-list> trigger,
18669 the <column-list> is stored here */
18670 Schema *pSchema; /* Schema containing the trigger */
18671 Schema *pTabSchema; /* Schema containing the table */
18672 TriggerStep *step_list; /* Link list of trigger program steps */
18673 Trigger *pNext; /* Next trigger associated with the table */
18674};
18675
18676/*
18677** A trigger is either a BEFORE or an AFTER trigger. The following constants
18678** determine which.
18679**
18680** If there are multiple triggers, you might of some BEFORE and some AFTER.
18681** In that cases, the constants below can be ORed together.
18682*/
18683#define TRIGGER_BEFORE 1
18684#define TRIGGER_AFTER 2
18685
18686/*
18687 * An instance of struct TriggerStep is used to store a single SQL statement
18688 * that is a part of a trigger-program.
18689 *
18690 * Instances of struct TriggerStep are stored in a singly linked list (linked
18691 * using the "pNext" member) referenced by the "step_list" member of the
18692 * associated struct Trigger instance. The first element of the linked list is
18693 * the first step of the trigger-program.
18694 *
18695 * The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or
18696 * "SELECT" statement. The meanings of the other members is determined by the
18697 * value of "op" as follows:
18698 *
18699 * (op == TK_INSERT)
18700 * orconf -> stores the ON CONFLICT algorithm
18701 * pSelect -> If this is an INSERT INTO ... SELECT ... statement, then
18702 * this stores a pointer to the SELECT statement. Otherwise NULL.
18703 * zTarget -> Dequoted name of the table to insert into.
18704 * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then
18705 * this stores values to be inserted. Otherwise NULL.
18706 * pIdList -> If this is an INSERT INTO ... (<column-names>) VALUES ...
18707 * statement, then this stores the column-names to be
18708 * inserted into.
18709 *
18710 * (op == TK_DELETE)
18711 * zTarget -> Dequoted name of the table to delete from.
18712 * pWhere -> The WHERE clause of the DELETE statement if one is specified.
18713 * Otherwise NULL.
18714 *
18715 * (op == TK_UPDATE)
18716 * zTarget -> Dequoted name of the table to update.
18717 * pWhere -> The WHERE clause of the UPDATE statement if one is specified.
18718 * Otherwise NULL.
18719 * pExprList -> A list of the columns to update and the expressions to update
18720 * them to. See sqlite3Update() documentation of "pChanges"
18721 * argument.
18722 *
18723 */
18724struct TriggerStep {
18725 u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT,
18726 ** or TK_RETURNING */
18727 u8 orconf; /* OE_Rollback etc. */
18728 Trigger *pTrig; /* The trigger that this step is a part of */
18729 Select *pSelect; /* SELECT statement or RHS of INSERT INTO SELECT ... */
18730 char *zTarget; /* Target table for DELETE, UPDATE, INSERT */
18731 SrcList *pFrom; /* FROM clause for UPDATE statement (if any) */
18732 Expr *pWhere; /* The WHERE clause for DELETE or UPDATE steps */
18733 ExprList *pExprList; /* SET clause for UPDATE, or RETURNING clause */
18734 IdList *pIdList; /* Column names for INSERT */
18735 Upsert *pUpsert; /* Upsert clauses on an INSERT */
18736 char *zSpan; /* Original SQL text of this command */
18737 TriggerStep *pNext; /* Next in the link-list */
18738 TriggerStep *pLast; /* Last element in link-list. Valid for 1st elem only */
18739};
18740
18741/*
18742** Information about a RETURNING clause
18743*/
18744struct Returning {
18745 Parse *pParse; /* The parse that includes the RETURNING clause */
18746 ExprList *pReturnEL; /* List of expressions to return */
18747 Trigger retTrig; /* The transient trigger that implements RETURNING */
18748 TriggerStep retTStep; /* The trigger step */
18749 int iRetCur; /* Transient table holding RETURNING results */
18750 int nRetCol; /* Number of in pReturnEL after expansion */
18751 int iRetReg; /* Register array for holding a row of RETURNING */
18752};
18753
18754/*
18755** An objected used to accumulate the text of a string where we
18756** do not necessarily know how big the string will be in the end.
18757*/
18758struct sqlite3_str {
18759 sqlite3 *db; /* Optional database for lookaside. Can be NULL */
18760 char *zText; /* The string collected so far */
18761 u32 nAlloc; /* Amount of space allocated in zText */
18762 u32 mxAlloc; /* Maximum allowed allocation. 0 for no malloc usage */
18763 u32 nChar; /* Length of the string so far */
18764 u8 accError; /* SQLITE_NOMEM or SQLITE_TOOBIG */
18765 u8 printfFlags; /* SQLITE_PRINTF flags below */
18766};
18767#define SQLITE_PRINTF_INTERNAL 0x01 /* Internal-use-only converters allowed */
18768#define SQLITE_PRINTF_SQLFUNC 0x02 /* SQL function arguments to VXPrintf */
18769#define SQLITE_PRINTF_MALLOCED 0x04 /* True if xText is allocated space */
18770
18771#define isMalloced(X) (((X)->printfFlags & SQLITE_PRINTF_MALLOCED)!=0)
18772
18773
18774/*
18775** A pointer to this structure is used to communicate information
18776** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
18777*/
18778typedef struct {
18779 sqlite3 *db; /* The database being initialized */
18780 char **pzErrMsg; /* Error message stored here */
18781 int iDb; /* 0 for main database. 1 for TEMP, 2.. for ATTACHed */
18782 int rc; /* Result code stored here */
18783 u32 mInitFlags; /* Flags controlling error messages */
18784 u32 nInitRow; /* Number of rows processed */
18785 Pgno mxPage; /* Maximum page number. 0 for no limit. */
18786} InitData;
18787
18788/*
18789** Allowed values for mInitFlags
18790*/
18791#define INITFLAG_AlterMask 0x0003 /* Types of ALTER */
18792#define INITFLAG_AlterRename 0x0001 /* Reparse after a RENAME */
18793#define INITFLAG_AlterDrop 0x0002 /* Reparse after a DROP COLUMN */
18794#define INITFLAG_AlterAdd 0x0003 /* Reparse after an ADD COLUMN */
18795
18796/* Tuning parameters are set using SQLITE_TESTCTRL_TUNE and are controlled
18797** on debug-builds of the CLI using ".testctrl tune ID VALUE". Tuning
18798** parameters are for temporary use during development, to help find
18799** optimial values for parameters in the query planner. The should not
18800** be used on trunk check-ins. They are a temporary mechanism available
18801** for transient development builds only.
18802**
18803** Tuning parameters are numbered starting with 1.
18804*/
18805#define SQLITE_NTUNE 6 /* Should be zero for all trunk check-ins */
18806#ifdef SQLITE_DEBUG
18807# define Tuning(X) (sqlite3Config.aTune[(X)-1])
18808#else
18809# define Tuning(X) 0
18810#endif
18811
18812/*
18813** Structure containing global configuration data for the SQLite library.
18814**
18815** This structure also contains some state information.
18816*/
18817struct Sqlite3Config {
18818 int bMemstat; /* True to enable memory status */
18819 u8 bCoreMutex; /* True to enable core mutexing */
18820 u8 bFullMutex; /* True to enable full mutexing */
18821 u8 bOpenUri; /* True to interpret filenames as URIs */
18822 u8 bUseCis; /* Use covering indices for full-scans */
18823 u8 bSmallMalloc; /* Avoid large memory allocations if true */
18824 u8 bExtraSchemaChecks; /* Verify type,name,tbl_name in schema */
18825 int mxStrlen; /* Maximum string length */
18826 int neverCorrupt; /* Database is always well-formed */
18827 int szLookaside; /* Default lookaside buffer size */
18828 int nLookaside; /* Default lookaside buffer count */
18829 int nStmtSpill; /* Stmt-journal spill-to-disk threshold */
18830 sqlite3_mem_methods m; /* Low-level memory allocation interface */
18831 sqlite3_mutex_methods mutex; /* Low-level mutex interface */
18832 sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */
18833 void *pHeap; /* Heap storage space */
18834 int nHeap; /* Size of pHeap[] */
18835 int mnReq, mxReq; /* Min and max heap requests sizes */
18836 sqlite3_int64 szMmap; /* mmap() space per open file */
18837 sqlite3_int64 mxMmap; /* Maximum value for szMmap */
18838 void *pPage; /* Page cache memory */
18839 int szPage; /* Size of each page in pPage[] */
18840 int nPage; /* Number of pages in pPage[] */
18841 int mxParserStack; /* maximum depth of the parser stack */
18842 int sharedCacheEnabled; /* true if shared-cache mode enabled */
18843 u32 szPma; /* Maximum Sorter PMA size */
18844 /* The above might be initialized to non-zero. The following need to always
18845 ** initially be zero, however. */
18846 int isInit; /* True after initialization has finished */
18847 int inProgress; /* True while initialization in progress */
18848 int isMutexInit; /* True after mutexes are initialized */
18849 int isMallocInit; /* True after malloc is initialized */
18850 int isPCacheInit; /* True after malloc is initialized */
18851 int nRefInitMutex; /* Number of users of pInitMutex */
18852 sqlite3_mutex *pInitMutex; /* Mutex used by sqlite3_initialize() */
18853 void (*xLog)(void*,int,const char*); /* Function for logging */
18854 void *pLogArg; /* First argument to xLog() */
18855#ifdef SQLITE_ENABLE_SQLLOG
18856 void(*xSqllog)(void*,sqlite3*,const char*, int);
18857 void *pSqllogArg;
18858#endif
18859#ifdef SQLITE_VDBE_COVERAGE
18860 /* The following callback (if not NULL) is invoked on every VDBE branch
18861 ** operation. Set the callback using SQLITE_TESTCTRL_VDBE_COVERAGE.
18862 */
18863 void (*xVdbeBranch)(void*,unsigned iSrcLine,u8 eThis,u8 eMx); /* Callback */
18864 void *pVdbeBranchArg; /* 1st argument */
18865#endif
18866#ifndef SQLITE_OMIT_DESERIALIZE
18867 sqlite3_int64 mxMemdbSize; /* Default max memdb size */
18868#endif
18869#ifndef SQLITE_UNTESTABLE
18870 int (*xTestCallback)(int); /* Invoked by sqlite3FaultSim() */
18871#endif
18872 int bLocaltimeFault; /* True to fail localtime() calls */
18873 int iOnceResetThreshold; /* When to reset OP_Once counters */
18874 u32 szSorterRef; /* Min size in bytes to use sorter-refs */
18875 unsigned int iPrngSeed; /* Alternative fixed seed for the PRNG */
18876 /* vvvv--- must be last ---vvv */
18877#ifdef SQLITE_DEBUG
18878 sqlite3_int64 aTune[SQLITE_NTUNE]; /* Tuning parameters */
18879#endif
18880};
18881
18882/*
18883** This macro is used inside of assert() statements to indicate that
18884** the assert is only valid on a well-formed database. Instead of:
18885**
18886** assert( X );
18887**
18888** One writes:
18889**
18890** assert( X || CORRUPT_DB );
18891**
18892** CORRUPT_DB is true during normal operation. CORRUPT_DB does not indicate
18893** that the database is definitely corrupt, only that it might be corrupt.
18894** For most test cases, CORRUPT_DB is set to false using a special
18895** sqlite3_test_control(). This enables assert() statements to prove
18896** things that are always true for well-formed databases.
18897*/
18898#define CORRUPT_DB (sqlite3Config.neverCorrupt==0)
18899
18900/*
18901** Context pointer passed down through the tree-walk.
18902*/
18903struct Walker {
18904 Parse *pParse; /* Parser context. */
18905 int (*xExprCallback)(Walker*, Expr*); /* Callback for expressions */
18906 int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */
18907 void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */
18908 int walkerDepth; /* Number of subqueries */
18909 u16 eCode; /* A small processing code */
18910 union { /* Extra data for callback */
18911 NameContext *pNC; /* Naming context */
18912 int n; /* A counter */
18913 int iCur; /* A cursor number */
18914 SrcList *pSrcList; /* FROM clause */
18915 struct CCurHint *pCCurHint; /* Used by codeCursorHint() */
18916 struct RefSrcList *pRefSrcList; /* sqlite3ReferencesSrcList() */
18917 int *aiCol; /* array of column indexes */
18918 struct IdxCover *pIdxCover; /* Check for index coverage */
18919 struct IdxExprTrans *pIdxTrans; /* Convert idxed expr to column */
18920 ExprList *pGroupBy; /* GROUP BY clause */
18921 Select *pSelect; /* HAVING to WHERE clause ctx */
18922 struct WindowRewrite *pRewrite; /* Window rewrite context */
18923 struct WhereConst *pConst; /* WHERE clause constants */
18924 struct RenameCtx *pRename; /* RENAME COLUMN context */
18925 struct Table *pTab; /* Table of generated column */
18926 SrcItem *pSrcItem; /* A single FROM clause item */
18927 DbFixer *pFix;
18928 } u;
18929};
18930
18931/*
18932** The following structure contains information used by the sqliteFix...
18933** routines as they walk the parse tree to make database references
18934** explicit.
18935*/
18936struct DbFixer {
18937 Parse *pParse; /* The parsing context. Error messages written here */
18938 Walker w; /* Walker object */
18939 Schema *pSchema; /* Fix items to this schema */
18940 u8 bTemp; /* True for TEMP schema entries */
18941 const char *zDb; /* Make sure all objects are contained in this database */
18942 const char *zType; /* Type of the container - used for error messages */
18943 const Token *pName; /* Name of the container - used for error messages */
18944};
18945
18946/* Forward declarations */
18947SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
18948SQLITE_PRIVATE int sqlite3WalkExprList(Walker*, ExprList*);
18949SQLITE_PRIVATE int sqlite3WalkSelect(Walker*, Select*);
18950SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker*, Select*);
18951SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker*, Select*);
18952SQLITE_PRIVATE int sqlite3ExprWalkNoop(Walker*, Expr*);
18953SQLITE_PRIVATE int sqlite3SelectWalkNoop(Walker*, Select*);
18954SQLITE_PRIVATE int sqlite3SelectWalkFail(Walker*, Select*);
18955SQLITE_PRIVATE int sqlite3WalkerDepthIncrease(Walker*,Select*);
18956SQLITE_PRIVATE void sqlite3WalkerDepthDecrease(Walker*,Select*);
18957SQLITE_PRIVATE void sqlite3WalkWinDefnDummyCallback(Walker*,Select*);
18958
18959#ifdef SQLITE_DEBUG
18960SQLITE_PRIVATE void sqlite3SelectWalkAssert2(Walker*, Select*);
18961#endif
18962
18963#ifndef SQLITE_OMIT_CTE
18964SQLITE_PRIVATE void sqlite3SelectPopWith(Walker*, Select*);
18965#else
18966# define sqlite3SelectPopWith 0
18967#endif
18968
18969/*
18970** Return code from the parse-tree walking primitives and their
18971** callbacks.
18972*/
18973#define WRC_Continue 0 /* Continue down into children */
18974#define WRC_Prune 1 /* Omit children but continue walking siblings */
18975#define WRC_Abort 2 /* Abandon the tree walk */
18976
18977/*
18978** A single common table expression
18979*/
18980struct Cte {
18981 char *zName; /* Name of this CTE */
18982 ExprList *pCols; /* List of explicit column names, or NULL */
18983 Select *pSelect; /* The definition of this CTE */
18984 const char *zCteErr; /* Error message for circular references */
18985 CteUse *pUse; /* Usage information for this CTE */
18986 u8 eM10d; /* The MATERIALIZED flag */
18987};
18988
18989/*
18990** Allowed values for the materialized flag (eM10d):
18991*/
18992#define M10d_Yes 0 /* AS MATERIALIZED */
18993#define M10d_Any 1 /* Not specified. Query planner's choice */
18994#define M10d_No 2 /* AS NOT MATERIALIZED */
18995
18996/*
18997** An instance of the With object represents a WITH clause containing
18998** one or more CTEs (common table expressions).
18999*/
19000struct With {
19001 int nCte; /* Number of CTEs in the WITH clause */
19002 int bView; /* Belongs to the outermost Select of a view */
19003 With *pOuter; /* Containing WITH clause, or NULL */
19004 Cte a[1]; /* For each CTE in the WITH clause.... */
19005};
19006
19007/*
19008** The Cte object is not guaranteed to persist for the entire duration
19009** of code generation. (The query flattener or other parser tree
19010** edits might delete it.) The following object records information
19011** about each Common Table Expression that must be preserved for the
19012** duration of the parse.
19013**
19014** The CteUse objects are freed using sqlite3ParserAddCleanup() rather
19015** than sqlite3SelectDelete(), which is what enables them to persist
19016** until the end of code generation.
19017*/
19018struct CteUse {
19019 int nUse; /* Number of users of this CTE */
19020 int addrM9e; /* Start of subroutine to compute materialization */
19021 int regRtn; /* Return address register for addrM9e subroutine */
19022 int iCur; /* Ephemeral table holding the materialization */
19023 LogEst nRowEst; /* Estimated number of rows in the table */
19024 u8 eM10d; /* The MATERIALIZED flag */
19025};
19026
19027
19028#ifdef SQLITE_DEBUG
19029/*
19030** An instance of the TreeView object is used for printing the content of
19031** data structures on sqlite3DebugPrintf() using a tree-like view.
19032*/
19033struct TreeView {
19034 int iLevel; /* Which level of the tree we are on */
19035 u8 bLine[100]; /* Draw vertical in column i if bLine[i] is true */
19036};
19037#endif /* SQLITE_DEBUG */
19038
19039/*
19040** This object is used in various ways, most (but not all) related to window
19041** functions.
19042**
19043** (1) A single instance of this structure is attached to the
19044** the Expr.y.pWin field for each window function in an expression tree.
19045** This object holds the information contained in the OVER clause,
19046** plus additional fields used during code generation.
19047**
19048** (2) All window functions in a single SELECT form a linked-list
19049** attached to Select.pWin. The Window.pFunc and Window.pExpr
19050** fields point back to the expression that is the window function.
19051**
19052** (3) The terms of the WINDOW clause of a SELECT are instances of this
19053** object on a linked list attached to Select.pWinDefn.
19054**
19055** (4) For an aggregate function with a FILTER clause, an instance
19056** of this object is stored in Expr.y.pWin with eFrmType set to
19057** TK_FILTER. In this case the only field used is Window.pFilter.
19058**
19059** The uses (1) and (2) are really the same Window object that just happens
19060** to be accessible in two different ways. Use case (3) are separate objects.
19061*/
19062struct Window {
19063 char *zName; /* Name of window (may be NULL) */
19064 char *zBase; /* Name of base window for chaining (may be NULL) */
19065 ExprList *pPartition; /* PARTITION BY clause */
19066 ExprList *pOrderBy; /* ORDER BY clause */
19067 u8 eFrmType; /* TK_RANGE, TK_GROUPS, TK_ROWS, or 0 */
19068 u8 eStart; /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
19069 u8 eEnd; /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
19070 u8 bImplicitFrame; /* True if frame was implicitly specified */
19071 u8 eExclude; /* TK_NO, TK_CURRENT, TK_TIES, TK_GROUP, or 0 */
19072 Expr *pStart; /* Expression for "<expr> PRECEDING" */
19073 Expr *pEnd; /* Expression for "<expr> FOLLOWING" */
19074 Window **ppThis; /* Pointer to this object in Select.pWin list */
19075 Window *pNextWin; /* Next window function belonging to this SELECT */
19076 Expr *pFilter; /* The FILTER expression */
19077 FuncDef *pFunc; /* The function */
19078 int iEphCsr; /* Partition buffer or Peer buffer */
19079 int regAccum; /* Accumulator */
19080 int regResult; /* Interim result */
19081 int csrApp; /* Function cursor (used by min/max) */
19082 int regApp; /* Function register (also used by min/max) */
19083 int regPart; /* Array of registers for PARTITION BY values */
19084 Expr *pOwner; /* Expression object this window is attached to */
19085 int nBufferCol; /* Number of columns in buffer table */
19086 int iArgCol; /* Offset of first argument for this function */
19087 int regOne; /* Register containing constant value 1 */
19088 int regStartRowid;
19089 int regEndRowid;
19090 u8 bExprArgs; /* Defer evaluation of window function arguments
19091 ** due to the SQLITE_SUBTYPE flag */
19092};
19093
19094#ifndef SQLITE_OMIT_WINDOWFUNC
19095SQLITE_PRIVATE void sqlite3WindowDelete(sqlite3*, Window*);
19096SQLITE_PRIVATE void sqlite3WindowUnlinkFromSelect(Window*);
19097SQLITE_PRIVATE void sqlite3WindowListDelete(sqlite3 *db, Window *p);
19098SQLITE_PRIVATE Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*, u8);
19099SQLITE_PRIVATE void sqlite3WindowAttach(Parse*, Expr*, Window*);
19100SQLITE_PRIVATE void sqlite3WindowLink(Select *pSel, Window *pWin);
19101SQLITE_PRIVATE int sqlite3WindowCompare(const Parse*, const Window*, const Window*, int);
19102SQLITE_PRIVATE void sqlite3WindowCodeInit(Parse*, Select*);
19103SQLITE_PRIVATE void sqlite3WindowCodeStep(Parse*, Select*, WhereInfo*, int, int);
19104SQLITE_PRIVATE int sqlite3WindowRewrite(Parse*, Select*);
19105SQLITE_PRIVATE void sqlite3WindowUpdate(Parse*, Window*, Window*, FuncDef*);
19106SQLITE_PRIVATE Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p);
19107SQLITE_PRIVATE Window *sqlite3WindowListDup(sqlite3 *db, Window *p);
19108SQLITE_PRIVATE void sqlite3WindowFunctions(void);
19109SQLITE_PRIVATE void sqlite3WindowChain(Parse*, Window*, Window*);
19110SQLITE_PRIVATE Window *sqlite3WindowAssemble(Parse*, Window*, ExprList*, ExprList*, Token*);
19111#else
19112# define sqlite3WindowDelete(a,b)
19113# define sqlite3WindowFunctions()
19114# define sqlite3WindowAttach(a,b,c)
19115#endif
19116
19117/*
19118** Assuming zIn points to the first byte of a UTF-8 character,
19119** advance zIn to point to the first byte of the next UTF-8 character.
19120*/
19121#define SQLITE_SKIP_UTF8(zIn) { \
19122 if( (*(zIn++))>=0xc0 ){ \
19123 while( (*zIn & 0xc0)==0x80 ){ zIn++; } \
19124 } \
19125}
19126
19127/*
19128** The SQLITE_*_BKPT macros are substitutes for the error codes with
19129** the same name but without the _BKPT suffix. These macros invoke
19130** routines that report the line-number on which the error originated
19131** using sqlite3_log(). The routines also provide a convenient place
19132** to set a debugger breakpoint.
19133*/
19134SQLITE_PRIVATE int sqlite3ReportError(int iErr, int lineno, const char *zType);
19135SQLITE_PRIVATE int sqlite3CorruptError(int);
19136SQLITE_PRIVATE int sqlite3MisuseError(int);
19137SQLITE_PRIVATE int sqlite3CantopenError(int);
19138#define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__)
19139#define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__)
19140#define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__)
19141#ifdef SQLITE_DEBUG
19142SQLITE_PRIVATE int sqlite3NomemError(int);
19143SQLITE_PRIVATE int sqlite3IoerrnomemError(int);
19144# define SQLITE_NOMEM_BKPT sqlite3NomemError(__LINE__)
19145# define SQLITE_IOERR_NOMEM_BKPT sqlite3IoerrnomemError(__LINE__)
19146#else
19147# define SQLITE_NOMEM_BKPT SQLITE_NOMEM
19148# define SQLITE_IOERR_NOMEM_BKPT SQLITE_IOERR_NOMEM
19149#endif
19150#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_CORRUPT_PGNO)
19151SQLITE_PRIVATE int sqlite3CorruptPgnoError(int,Pgno);
19152# define SQLITE_CORRUPT_PGNO(P) sqlite3CorruptPgnoError(__LINE__,(P))
19153#else
19154# define SQLITE_CORRUPT_PGNO(P) sqlite3CorruptError(__LINE__)
19155#endif
19156
19157/*
19158** FTS3 and FTS4 both require virtual table support
19159*/
19160#if defined(SQLITE_OMIT_VIRTUALTABLE)
19161# undef SQLITE_ENABLE_FTS3
19162# undef SQLITE_ENABLE_FTS4
19163#endif
19164
19165/*
19166** FTS4 is really an extension for FTS3. It is enabled using the
19167** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also call
19168** the SQLITE_ENABLE_FTS4 macro to serve as an alias for SQLITE_ENABLE_FTS3.
19169*/
19170#if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
19171# define SQLITE_ENABLE_FTS3 1
19172#endif
19173
19174/*
19175** The ctype.h header is needed for non-ASCII systems. It is also
19176** needed by FTS3 when FTS3 is included in the amalgamation.
19177*/
19178#if !defined(SQLITE_ASCII) || \
19179 (defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_AMALGAMATION))
19180# include <ctype.h>
19181#endif
19182
19183/*
19184** The following macros mimic the standard library functions toupper(),
19185** isspace(), isalnum(), isdigit() and isxdigit(), respectively. The
19186** sqlite versions only work for ASCII characters, regardless of locale.
19187*/
19188#ifdef SQLITE_ASCII
19189# define sqlite3Toupper(x) ((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20))
19190# define sqlite3Isspace(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x01)
19191# define sqlite3Isalnum(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x06)
19192# define sqlite3Isalpha(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x02)
19193# define sqlite3Isdigit(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x04)
19194# define sqlite3Isxdigit(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x08)
19195# define sqlite3Tolower(x) (sqlite3UpperToLower[(unsigned char)(x)])
19196# define sqlite3Isquote(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x80)
19197#else
19198# define sqlite3Toupper(x) toupper((unsigned char)(x))
19199# define sqlite3Isspace(x) isspace((unsigned char)(x))
19200# define sqlite3Isalnum(x) isalnum((unsigned char)(x))
19201# define sqlite3Isalpha(x) isalpha((unsigned char)(x))
19202# define sqlite3Isdigit(x) isdigit((unsigned char)(x))
19203# define sqlite3Isxdigit(x) isxdigit((unsigned char)(x))
19204# define sqlite3Tolower(x) tolower((unsigned char)(x))
19205# define sqlite3Isquote(x) ((x)=='"'||(x)=='\''||(x)=='['||(x)=='`')
19206#endif
19207SQLITE_PRIVATE int sqlite3IsIdChar(u8);
19208
19209/*
19210** Internal function prototypes
19211*/
19212SQLITE_PRIVATE int sqlite3StrICmp(const char*,const char*);
19213SQLITE_PRIVATE int sqlite3Strlen30(const char*);
19214#define sqlite3Strlen30NN(C) (strlen(C)&0x3fffffff)
19215SQLITE_PRIVATE char *sqlite3ColumnType(Column*,char*);
19216#define sqlite3StrNICmp sqlite3_strnicmp
19217
19218SQLITE_PRIVATE int sqlite3MallocInit(void);
19219SQLITE_PRIVATE void sqlite3MallocEnd(void);
19220SQLITE_PRIVATE void *sqlite3Malloc(u64);
19221SQLITE_PRIVATE void *sqlite3MallocZero(u64);
19222SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3*, u64);
19223SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3*, u64);
19224SQLITE_PRIVATE void *sqlite3DbMallocRawNN(sqlite3*, u64);
19225SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3*,const char*);
19226SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3*,const char*, u64);
19227SQLITE_PRIVATE char *sqlite3DbSpanDup(sqlite3*,const char*,const char*);
19228SQLITE_PRIVATE void *sqlite3Realloc(void*, u64);
19229SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64);
19230SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, u64);
19231SQLITE_PRIVATE void sqlite3DbFree(sqlite3*, void*);
19232SQLITE_PRIVATE void sqlite3DbFreeNN(sqlite3*, void*);
19233SQLITE_PRIVATE int sqlite3MallocSize(const void*);
19234SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3*, const void*);
19235SQLITE_PRIVATE void *sqlite3PageMalloc(int);
19236SQLITE_PRIVATE void sqlite3PageFree(void*);
19237SQLITE_PRIVATE void sqlite3MemSetDefault(void);
19238#ifndef SQLITE_UNTESTABLE
19239SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
19240#endif
19241SQLITE_PRIVATE int sqlite3HeapNearlyFull(void);
19242
19243/*
19244** On systems with ample stack space and that support alloca(), make
19245** use of alloca() to obtain space for large automatic objects. By default,
19246** obtain space from malloc().
19247**
19248** The alloca() routine never returns NULL. This will cause code paths
19249** that deal with sqlite3StackAlloc() failures to be unreachable.
19250*/
19251#ifdef SQLITE_USE_ALLOCA
19252# define sqlite3StackAllocRaw(D,N) alloca(N)
19253# define sqlite3StackAllocZero(D,N) memset(alloca(N), 0, N)
19254# define sqlite3StackFree(D,P)
19255#else
19256# define sqlite3StackAllocRaw(D,N) sqlite3DbMallocRaw(D,N)
19257# define sqlite3StackAllocZero(D,N) sqlite3DbMallocZero(D,N)
19258# define sqlite3StackFree(D,P) sqlite3DbFree(D,P)
19259#endif
19260
19261/* Do not allow both MEMSYS5 and MEMSYS3 to be defined together. If they
19262** are, disable MEMSYS3
19263*/
19264#ifdef SQLITE_ENABLE_MEMSYS5
19265SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
19266#undef SQLITE_ENABLE_MEMSYS3
19267#endif
19268#ifdef SQLITE_ENABLE_MEMSYS3
19269SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
19270#endif
19271
19272
19273#ifndef SQLITE_MUTEX_OMIT
19274SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void);
19275SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void);
19276SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int);
19277SQLITE_PRIVATE int sqlite3MutexInit(void);
19278SQLITE_PRIVATE int sqlite3MutexEnd(void);
19279#endif
19280#if !defined(SQLITE_MUTEX_OMIT) && !defined(SQLITE_MUTEX_NOOP)
19281SQLITE_PRIVATE void sqlite3MemoryBarrier(void);
19282#else
19283# define sqlite3MemoryBarrier()
19284#endif
19285
19286SQLITE_PRIVATE sqlite3_int64 sqlite3StatusValue(int);
19287SQLITE_PRIVATE void sqlite3StatusUp(int, int);
19288SQLITE_PRIVATE void sqlite3StatusDown(int, int);
19289SQLITE_PRIVATE void sqlite3StatusHighwater(int, int);
19290SQLITE_PRIVATE int sqlite3LookasideUsed(sqlite3*,int*);
19291
19292/* Access to mutexes used by sqlite3_status() */
19293SQLITE_PRIVATE sqlite3_mutex *sqlite3Pcache1Mutex(void);
19294SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void);
19295
19296#if defined(SQLITE_ENABLE_MULTITHREADED_CHECKS) && !defined(SQLITE_MUTEX_OMIT)
19297SQLITE_PRIVATE void sqlite3MutexWarnOnContention(sqlite3_mutex*);
19298#else
19299# define sqlite3MutexWarnOnContention(x)
19300#endif
19301
19302#ifndef SQLITE_OMIT_FLOATING_POINT
19303# define EXP754 (((u64)0x7ff)<<52)
19304# define MAN754 ((((u64)1)<<52)-1)
19305# define IsNaN(X) (((X)&EXP754)==EXP754 && ((X)&MAN754)!=0)
19306SQLITE_PRIVATE int sqlite3IsNaN(double);
19307#else
19308# define IsNaN(X) 0
19309# define sqlite3IsNaN(X) 0
19310#endif
19311
19312/*
19313** An instance of the following structure holds information about SQL
19314** functions arguments that are the parameters to the printf() function.
19315*/
19316struct PrintfArguments {
19317 int nArg; /* Total number of arguments */
19318 int nUsed; /* Number of arguments used so far */
19319 sqlite3_value **apArg; /* The argument values */
19320};
19321
19322SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
19323SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
19324#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
19325SQLITE_PRIVATE void sqlite3DebugPrintf(const char*, ...);
19326#endif
19327#if defined(SQLITE_TEST)
19328SQLITE_PRIVATE void *sqlite3TestTextToPtr(const char*);
19329#endif
19330
19331#if defined(SQLITE_DEBUG)
19332SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView*, const Expr*, u8);
19333SQLITE_PRIVATE void sqlite3TreeViewBareExprList(TreeView*, const ExprList*, const char*);
19334SQLITE_PRIVATE void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*);
19335SQLITE_PRIVATE void sqlite3TreeViewSrcList(TreeView*, const SrcList*);
19336SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView*, const Select*, u8);
19337SQLITE_PRIVATE void sqlite3TreeViewWith(TreeView*, const With*, u8);
19338#ifndef SQLITE_OMIT_WINDOWFUNC
19339SQLITE_PRIVATE void sqlite3TreeViewWindow(TreeView*, const Window*, u8);
19340SQLITE_PRIVATE void sqlite3TreeViewWinFunc(TreeView*, const Window*, u8);
19341#endif
19342#endif
19343
19344
19345SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*);
19346SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
19347SQLITE_PRIVATE int sqlite3ErrorToParser(sqlite3*,int);
19348SQLITE_PRIVATE void sqlite3Dequote(char*);
19349SQLITE_PRIVATE void sqlite3DequoteExpr(Expr*);
19350SQLITE_PRIVATE void sqlite3DequoteToken(Token*);
19351SQLITE_PRIVATE void sqlite3TokenInit(Token*,char*);
19352SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
19353SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
19354SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
19355SQLITE_PRIVATE int sqlite3GetTempReg(Parse*);
19356SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int);
19357SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int);
19358SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int);
19359SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse*);
19360#ifdef SQLITE_DEBUG
19361SQLITE_PRIVATE int sqlite3NoTempsInRange(Parse*,int,int);
19362#endif
19363SQLITE_PRIVATE Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
19364SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*,int,const char*);
19365SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
19366SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*);
19367SQLITE_PRIVATE void sqlite3PExprAddSelect(Parse*, Expr*, Select*);
19368SQLITE_PRIVATE Expr *sqlite3ExprAnd(Parse*,Expr*, Expr*);
19369SQLITE_PRIVATE Expr *sqlite3ExprSimplifiedAndOr(Expr*);
19370SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, const Token*, int);
19371SQLITE_PRIVATE void sqlite3ExprFunctionUsable(Parse*,const Expr*,const FuncDef*);
19372SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*, u32);
19373SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3*, Expr*);
19374SQLITE_PRIVATE void sqlite3ExprDeferredDelete(Parse*, Expr*);
19375SQLITE_PRIVATE void sqlite3ExprUnmapAndDelete(Parse*, Expr*);
19376SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
19377SQLITE_PRIVATE ExprList *sqlite3ExprListAppendVector(Parse*,ExprList*,IdList*,Expr*);
19378SQLITE_PRIVATE Select *sqlite3ExprListToValues(Parse*, int, ExprList*);
19379SQLITE_PRIVATE void sqlite3ExprListSetSortOrder(ExprList*,int,int);
19380SQLITE_PRIVATE void sqlite3ExprListSetName(Parse*,ExprList*,const Token*,int);
19381SQLITE_PRIVATE void sqlite3ExprListSetSpan(Parse*,ExprList*,const char*,const char*);
19382SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3*, ExprList*);
19383SQLITE_PRIVATE u32 sqlite3ExprListFlags(const ExprList*);
19384SQLITE_PRIVATE int sqlite3IndexHasDuplicateRootPage(Index*);
19385SQLITE_PRIVATE int sqlite3Init(sqlite3*, char**);
19386SQLITE_PRIVATE int sqlite3InitCallback(void*, int, char**, char**);
19387SQLITE_PRIVATE int sqlite3InitOne(sqlite3*, int, char**, u32);
19388SQLITE_PRIVATE void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
19389#ifndef SQLITE_OMIT_VIRTUALTABLE
19390SQLITE_PRIVATE Module *sqlite3PragmaVtabRegister(sqlite3*,const char *zName);
19391#endif
19392SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3*);
19393SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3*,int);
19394SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3*);
19395SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3*);
19396SQLITE_PRIVATE void sqlite3ColumnSetExpr(Parse*,Table*,Column*,Expr*);
19397SQLITE_PRIVATE Expr *sqlite3ColumnExpr(Table*,Column*);
19398SQLITE_PRIVATE void sqlite3ColumnSetColl(sqlite3*,Column*,const char*zColl);
19399SQLITE_PRIVATE const char *sqlite3ColumnColl(Column*);
19400SQLITE_PRIVATE void sqlite3DeleteColumnNames(sqlite3*,Table*);
19401SQLITE_PRIVATE void sqlite3GenerateColumnNames(Parse *pParse, Select *pSelect);
19402SQLITE_PRIVATE int sqlite3ColumnsFromExprList(Parse*,ExprList*,i16*,Column**);
19403SQLITE_PRIVATE void sqlite3SelectAddColumnTypeAndCollation(Parse*,Table*,Select*,char);
19404SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse*,Select*,char);
19405SQLITE_PRIVATE void sqlite3OpenSchemaTable(Parse *, int);
19406SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table*);
19407SQLITE_PRIVATE i16 sqlite3TableColumnToIndex(Index*, i16);
19408#ifdef SQLITE_OMIT_GENERATED_COLUMNS
19409# define sqlite3TableColumnToStorage(T,X) (X) /* No-op pass-through */
19410# define sqlite3StorageColumnToTable(T,X) (X) /* No-op pass-through */
19411#else
19412SQLITE_PRIVATE i16 sqlite3TableColumnToStorage(Table*, i16);
19413SQLITE_PRIVATE i16 sqlite3StorageColumnToTable(Table*, i16);
19414#endif
19415SQLITE_PRIVATE void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
19416#if SQLITE_ENABLE_HIDDEN_COLUMNS
19417SQLITE_PRIVATE void sqlite3ColumnPropertiesFromName(Table*, Column*);
19418#else
19419# define sqlite3ColumnPropertiesFromName(T,C) /* no-op */
19420#endif
19421SQLITE_PRIVATE void sqlite3AddColumn(Parse*,Token,Token);
19422SQLITE_PRIVATE void sqlite3AddNotNull(Parse*, int);
19423SQLITE_PRIVATE void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
19424SQLITE_PRIVATE void sqlite3AddCheckConstraint(Parse*, Expr*, const char*, const char*);
19425SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,Expr*,const char*,const char*);
19426SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*);
19427SQLITE_PRIVATE void sqlite3AddGenerated(Parse*,Expr*,Token*);
19428SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,u32,Select*);
19429SQLITE_PRIVATE void sqlite3AddReturning(Parse*,ExprList*);
19430SQLITE_PRIVATE int sqlite3ParseUri(const char*,const char*,unsigned int*,
19431 sqlite3_vfs**,char**,char **);
19432#define sqlite3CodecQueryParameters(A,B,C) 0
19433SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
19434
19435#ifdef SQLITE_UNTESTABLE
19436# define sqlite3FaultSim(X) SQLITE_OK
19437#else
19438SQLITE_PRIVATE int sqlite3FaultSim(int);
19439#endif
19440
19441SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
19442SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
19443SQLITE_PRIVATE int sqlite3BitvecTestNotNull(Bitvec*, u32);
19444SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
19445SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*);
19446SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec*);
19447SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec*);
19448#ifndef SQLITE_UNTESTABLE
19449SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int,int*);
19450#endif
19451
19452SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*);
19453SQLITE_PRIVATE void sqlite3RowSetDelete(void*);
19454SQLITE_PRIVATE void sqlite3RowSetClear(void*);
19455SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet*, i64);
19456SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, int iBatch, i64);
19457SQLITE_PRIVATE int sqlite3RowSetNext(RowSet*, i64*);
19458
19459SQLITE_PRIVATE void sqlite3CreateView(Parse*,Token*,Token*,Token*,ExprList*,Select*,int,int);
19460
19461#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
19462SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse*,Table*);
19463#else
19464# define sqlite3ViewGetColumnNames(A,B) 0
19465#endif
19466
19467#if SQLITE_MAX_ATTACHED>30
19468SQLITE_PRIVATE int sqlite3DbMaskAllZero(yDbMask);
19469#endif
19470SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
19471SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int);
19472SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
19473SQLITE_PRIVATE void sqlite3FreeIndex(sqlite3*, Index*);
19474#ifndef SQLITE_OMIT_AUTOINCREMENT
19475SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse);
19476SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse);
19477#else
19478# define sqlite3AutoincrementBegin(X)
19479# define sqlite3AutoincrementEnd(X)
19480#endif
19481SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int, Upsert*);
19482#ifndef SQLITE_OMIT_GENERATED_COLUMNS
19483SQLITE_PRIVATE void sqlite3ComputeGeneratedColumns(Parse*, int, Table*);
19484#endif
19485SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
19486SQLITE_PRIVATE IdList *sqlite3IdListAppend(Parse*, IdList*, Token*);
19487SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
19488SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(Parse*, SrcList*, int, int);
19489SQLITE_PRIVATE SrcList *sqlite3SrcListAppendList(Parse *pParse, SrcList *p1, SrcList *p2);
19490SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(Parse*, SrcList*, Token*, Token*);
19491SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
19492 Token*, Select*, Expr*, IdList*);
19493SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
19494SQLITE_PRIVATE void sqlite3SrcListFuncArgs(Parse*, SrcList*, ExprList*);
19495SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *, SrcItem *);
19496SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList*);
19497SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse*, SrcList*);
19498SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3*, IdList*);
19499SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*);
19500SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(sqlite3*,i16,int,char**);
19501SQLITE_PRIVATE void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
19502 Expr*, int, int, u8);
19503SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
19504SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
19505SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
19506 Expr*,ExprList*,u32,Expr*);
19507SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*);
19508SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*);
19509SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int);
19510SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
19511#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
19512SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,char*);
19513#endif
19514SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*, ExprList*, Expr*);
19515SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*,Expr*,int,ExprList*,Expr*,
19516 Upsert*);
19517SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
19518SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
19519SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo*);
19520SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*);
19521SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*);
19522SQLITE_PRIVATE int sqlite3WhereOrderByLimitOptLabel(WhereInfo*);
19523SQLITE_PRIVATE void sqlite3WhereMinMaxOptEarlyOut(Vdbe*,WhereInfo*);
19524SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo*);
19525SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo*);
19526SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo*);
19527SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo*, int*);
19528#define ONEPASS_OFF 0 /* Use of ONEPASS not allowed */
19529#define ONEPASS_SINGLE 1 /* ONEPASS valid for a single row update */
19530#define ONEPASS_MULTI 2 /* ONEPASS is valid for multiple rows */
19531SQLITE_PRIVATE int sqlite3WhereUsesDeferredSeek(WhereInfo*);
19532SQLITE_PRIVATE void sqlite3ExprCodeLoadIndexColumn(Parse*, Index*, int, int, int);
19533SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
19534SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
19535SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
19536SQLITE_PRIVATE void sqlite3ExprCode(Parse*, Expr*, int);
19537#ifndef SQLITE_OMIT_GENERATED_COLUMNS
19538SQLITE_PRIVATE void sqlite3ExprCodeGeneratedColumn(Parse*, Table*, Column*, int);
19539#endif
19540SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, Expr*, int);
19541SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
19542SQLITE_PRIVATE int sqlite3ExprCodeRunJustOnce(Parse*, Expr*, int);
19543SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
19544SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
19545SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8);
19546#define SQLITE_ECEL_DUP 0x01 /* Deep, not shallow copies */
19547#define SQLITE_ECEL_FACTOR 0x02 /* Factor out constant terms */
19548#define SQLITE_ECEL_REF 0x04 /* Use ExprList.u.x.iOrderByCol */
19549#define SQLITE_ECEL_OMITREF 0x08 /* Omit if ExprList.u.x.iOrderByCol */
19550SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
19551SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
19552SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse*, Expr*, int, int);
19553SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*);
19554#define LOCATE_VIEW 0x01
19555#define LOCATE_NOERR 0x02
19556SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,u32 flags,const char*, const char*);
19557SQLITE_PRIVATE const char *sqlite3PreferredTableName(const char*);
19558SQLITE_PRIVATE Table *sqlite3LocateTableItem(Parse*,u32 flags,SrcItem *);
19559SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
19560SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
19561SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
19562SQLITE_PRIVATE void sqlite3Vacuum(Parse*,Token*,Expr*);
19563SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*, int, sqlite3_value*);
19564SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, const Token*);
19565SQLITE_PRIVATE int sqlite3ExprCompare(const Parse*,const Expr*,const Expr*, int);
19566SQLITE_PRIVATE int sqlite3ExprCompareSkip(Expr*,Expr*,int);
19567SQLITE_PRIVATE int sqlite3ExprListCompare(const ExprList*,const ExprList*, int);
19568SQLITE_PRIVATE int sqlite3ExprImpliesExpr(const Parse*,const Expr*,const Expr*, int);
19569SQLITE_PRIVATE int sqlite3ExprImpliesNonNullRow(Expr*,int);
19570SQLITE_PRIVATE void sqlite3AggInfoPersistWalkerInit(Walker*,Parse*);
19571SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
19572SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
19573SQLITE_PRIVATE int sqlite3ExprCoveredByIndex(Expr*, int iCur, Index *pIdx);
19574SQLITE_PRIVATE int sqlite3ReferencesSrcList(Parse*, Expr*, SrcList*);
19575SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
19576#ifndef SQLITE_UNTESTABLE
19577SQLITE_PRIVATE void sqlite3PrngSaveState(void);
19578SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
19579#endif
19580SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*,int);
19581SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
19582SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
19583SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
19584SQLITE_PRIVATE void sqlite3EndTransaction(Parse*,int);
19585SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
19586SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
19587SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
19588SQLITE_PRIVATE u32 sqlite3IsTrueOrFalse(const char*);
19589SQLITE_PRIVATE int sqlite3ExprIdToTrueFalse(Expr*);
19590SQLITE_PRIVATE int sqlite3ExprTruthValue(const Expr*);
19591SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
19592SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
19593SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*, u8);
19594SQLITE_PRIVATE int sqlite3ExprIsConstantOrGroupBy(Parse*, Expr*, ExprList*);
19595SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr*,int);
19596#ifdef SQLITE_ENABLE_CURSOR_HINTS
19597SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr*);
19598#endif
19599SQLITE_PRIVATE int sqlite3ExprIsInteger(const Expr*, int*);
19600SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
19601SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
19602SQLITE_PRIVATE int sqlite3IsRowid(const char*);
19603SQLITE_PRIVATE void sqlite3GenerateRowDelete(
19604 Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int);
19605SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
19606SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
19607SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse*,int);
19608SQLITE_PRIVATE int sqlite3ExprReferencesUpdatedColumn(Expr*,int*,int);
19609SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
19610 u8,u8,int,int*,int*,Upsert*);
19611#ifdef SQLITE_ENABLE_NULL_TRIM
19612SQLITE_PRIVATE void sqlite3SetMakeRecordP5(Vdbe*,Table*);
19613#else
19614# define sqlite3SetMakeRecordP5(A,B)
19615#endif
19616SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
19617SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, u8, int, u8*, int*, int*);
19618SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
19619SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
19620SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
19621SQLITE_PRIVATE void sqlite3HaltConstraint(Parse*, int, int, char*, i8, u8);
19622SQLITE_PRIVATE void sqlite3UniqueConstraint(Parse*, int, Index*);
19623SQLITE_PRIVATE void sqlite3RowidConstraint(Parse*, int, Table*);
19624SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,const Expr*,int);
19625SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,const ExprList*,int);
19626SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,const SrcList*,int);
19627SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,const IdList*);
19628SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,const Select*,int);
19629SQLITE_PRIVATE FuncDef *sqlite3FunctionSearch(int,const char*);
19630SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(FuncDef*,int);
19631SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8);
19632SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void);
19633SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
19634SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*);
19635SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
19636SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
19637SQLITE_PRIVATE void sqlite3ChangeCookie(Parse*, int);
19638SQLITE_PRIVATE With *sqlite3WithDup(sqlite3 *db, With *p);
19639
19640#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
19641SQLITE_PRIVATE void sqlite3MaterializeView(Parse*, Table*, Expr*, ExprList*,Expr*,int);
19642#endif
19643
19644#ifndef SQLITE_OMIT_TRIGGER
19645SQLITE_PRIVATE void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
19646 Expr*,int, int);
19647SQLITE_PRIVATE void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
19648SQLITE_PRIVATE void sqlite3DropTrigger(Parse*, SrcList*, int);
19649SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse*, Trigger*);
19650SQLITE_PRIVATE Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask);
19651SQLITE_PRIVATE Trigger *sqlite3TriggerList(Parse *, Table *);
19652SQLITE_PRIVATE void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
19653 int, int, int);
19654SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
19655 void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
19656SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
19657SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*,
19658 const char*,const char*);
19659SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(Parse*,Token*, IdList*,
19660 Select*,u8,Upsert*,
19661 const char*,const char*);
19662SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(Parse*,Token*,SrcList*,ExprList*,
19663 Expr*, u8, const char*,const char*);
19664SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(Parse*,Token*, Expr*,
19665 const char*,const char*);
19666SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3*, Trigger*);
19667SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
19668SQLITE_PRIVATE u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
19669SQLITE_PRIVATE SrcList *sqlite3TriggerStepSrc(Parse*, TriggerStep*);
19670# define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
19671# define sqlite3IsToplevel(p) ((p)->pToplevel==0)
19672#else
19673# define sqlite3TriggersExist(B,C,D,E,F) 0
19674# define sqlite3DeleteTrigger(A,B)
19675# define sqlite3DropTriggerPtr(A,B)
19676# define sqlite3UnlinkAndDeleteTrigger(A,B,C)
19677# define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I)
19678# define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F)
19679# define sqlite3TriggerList(X, Y) 0
19680# define sqlite3ParseToplevel(p) p
19681# define sqlite3IsToplevel(p) 1
19682# define sqlite3TriggerColmask(A,B,C,D,E,F,G) 0
19683# define sqlite3TriggerStepSrc(A,B) 0
19684#endif
19685
19686SQLITE_PRIVATE int sqlite3JoinType(Parse*, Token*, Token*, Token*);
19687SQLITE_PRIVATE int sqlite3ColumnIndex(Table *pTab, const char *zCol);
19688SQLITE_PRIVATE void sqlite3SetJoinExpr(Expr*,int);
19689SQLITE_PRIVATE void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int);
19690SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse*, int);
19691#ifndef SQLITE_OMIT_AUTHORIZATION
19692SQLITE_PRIVATE void sqlite3AuthRead(Parse*,Expr*,Schema*,SrcList*);
19693SQLITE_PRIVATE int sqlite3AuthCheck(Parse*,int, const char*, const char*, const char*);
19694SQLITE_PRIVATE void sqlite3AuthContextPush(Parse*, AuthContext*, const char*);
19695SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext*);
19696SQLITE_PRIVATE int sqlite3AuthReadCol(Parse*, const char *, const char *, int);
19697#else
19698# define sqlite3AuthRead(a,b,c,d)
19699# define sqlite3AuthCheck(a,b,c,d,e) SQLITE_OK
19700# define sqlite3AuthContextPush(a,b,c)
19701# define sqlite3AuthContextPop(a) ((void)(a))
19702#endif
19703SQLITE_PRIVATE int sqlite3DbIsNamed(sqlite3 *db, int iDb, const char *zName);
19704SQLITE_PRIVATE void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
19705SQLITE_PRIVATE void sqlite3Detach(Parse*, Expr*);
19706SQLITE_PRIVATE void sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
19707SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*);
19708SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*);
19709SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*);
19710SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
19711SQLITE_PRIVATE int sqlite3RealSameAsInt(double,sqlite3_int64);
19712SQLITE_PRIVATE void sqlite3Int64ToText(i64,char*);
19713SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*, int, u8);
19714SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*);
19715SQLITE_PRIVATE int sqlite3GetUInt32(const char*, u32*);
19716SQLITE_PRIVATE int sqlite3Atoi(const char*);
19717#ifndef SQLITE_OMIT_UTF16
19718SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *pData, int nChar);
19719#endif
19720SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *pData, int nByte);
19721SQLITE_PRIVATE u32 sqlite3Utf8Read(const u8**);
19722SQLITE_PRIVATE LogEst sqlite3LogEst(u64);
19723SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst,LogEst);
19724#ifndef SQLITE_OMIT_VIRTUALTABLE
19725SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double);
19726#endif
19727#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
19728 defined(SQLITE_ENABLE_STAT4) || \
19729 defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
19730SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst);
19731#endif
19732SQLITE_PRIVATE VList *sqlite3VListAdd(sqlite3*,VList*,const char*,int,int);
19733SQLITE_PRIVATE const char *sqlite3VListNumToName(VList*,int);
19734SQLITE_PRIVATE int sqlite3VListNameToNum(VList*,const char*,int);
19735
19736/*
19737** Routines to read and write variable-length integers. These used to
19738** be defined locally, but now we use the varint routines in the util.c
19739** file.
19740*/
19741SQLITE_PRIVATE int sqlite3PutVarint(unsigned char*, u64);
19742SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *, u64 *);
19743SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *, u32 *);
19744SQLITE_PRIVATE int sqlite3VarintLen(u64 v);
19745
19746/*
19747** The common case is for a varint to be a single byte. They following
19748** macros handle the common case without a procedure call, but then call
19749** the procedure for larger varints.
19750*/
19751#define getVarint32(A,B) \
19752 (u8)((*(A)<(u8)0x80)?((B)=(u32)*(A)),1:sqlite3GetVarint32((A),(u32 *)&(B)))
19753#define getVarint32NR(A,B) \
19754 B=(u32)*(A);if(B>=0x80)sqlite3GetVarint32((A),(u32*)&(B))
19755#define putVarint32(A,B) \
19756 (u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1:\
19757 sqlite3PutVarint((A),(B)))
19758#define getVarint sqlite3GetVarint
19759#define putVarint sqlite3PutVarint
19760
19761
19762SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(sqlite3*, Index*);
19763SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe*, Table*, int);
19764SQLITE_PRIVATE char sqlite3CompareAffinity(const Expr *pExpr, char aff2);
19765SQLITE_PRIVATE int sqlite3IndexAffinityOk(const Expr *pExpr, char idx_affinity);
19766SQLITE_PRIVATE char sqlite3TableColumnAffinity(const Table*,int);
19767SQLITE_PRIVATE char sqlite3ExprAffinity(const Expr *pExpr);
19768SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8);
19769SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char*, i64*);
19770SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...);
19771SQLITE_PRIVATE void sqlite3Error(sqlite3*,int);
19772SQLITE_PRIVATE void sqlite3ErrorClear(sqlite3*);
19773SQLITE_PRIVATE void sqlite3SystemError(sqlite3*,int);
19774SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
19775SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
19776SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
19777
19778#if defined(SQLITE_NEED_ERR_NAME)
19779SQLITE_PRIVATE const char *sqlite3ErrName(int);
19780#endif
19781
19782#ifndef SQLITE_OMIT_DESERIALIZE
19783SQLITE_PRIVATE int sqlite3MemdbInit(void);
19784#endif
19785
19786SQLITE_PRIVATE const char *sqlite3ErrStr(int);
19787SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
19788SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
19789SQLITE_PRIVATE int sqlite3IsBinary(const CollSeq*);
19790SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
19791SQLITE_PRIVATE void sqlite3SetTextEncoding(sqlite3 *db, u8);
19792SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, const Expr *pExpr);
19793SQLITE_PRIVATE CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, const Expr *pExpr);
19794SQLITE_PRIVATE int sqlite3ExprCollSeqMatch(Parse*,const Expr*,const Expr*);
19795SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(const Parse *pParse, Expr*, const Token*, int);
19796SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(const Parse*,Expr*,const char*);
19797SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*);
19798SQLITE_PRIVATE Expr *sqlite3ExprSkipCollateAndLikely(Expr*);
19799SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
19800SQLITE_PRIVATE int sqlite3WritableSchema(sqlite3*);
19801SQLITE_PRIVATE int sqlite3CheckObjectName(Parse*, const char*,const char*,const char*);
19802SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, i64);
19803SQLITE_PRIVATE int sqlite3AddInt64(i64*,i64);
19804SQLITE_PRIVATE int sqlite3SubInt64(i64*,i64);
19805SQLITE_PRIVATE int sqlite3MulInt64(i64*,i64);
19806SQLITE_PRIVATE int sqlite3AbsInt32(int);
19807#ifdef SQLITE_ENABLE_8_3_NAMES
19808SQLITE_PRIVATE void sqlite3FileSuffix3(const char*, char*);
19809#else
19810# define sqlite3FileSuffix3(X,Y)
19811#endif
19812SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z,u8);
19813
19814SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8);
19815SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
19816SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
19817 void(*)(void*));
19818SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value*);
19819SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
19820#ifndef SQLITE_UNTESTABLE
19821SQLITE_PRIVATE void sqlite3ResultIntReal(sqlite3_context*);
19822#endif
19823SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
19824#ifndef SQLITE_OMIT_UTF16
19825SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
19826#endif
19827SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, const Expr *, u8, u8, sqlite3_value **);
19828SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
19829#ifndef SQLITE_AMALGAMATION
19830SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
19831SQLITE_PRIVATE const char sqlite3StrBINARY[];
19832SQLITE_PRIVATE const unsigned char sqlite3StdTypeLen[];
19833SQLITE_PRIVATE const char sqlite3StdTypeAffinity[];
19834SQLITE_PRIVATE const char sqlite3StdTypeMap[];
19835SQLITE_PRIVATE const char *sqlite3StdType[];
19836SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
19837SQLITE_PRIVATE const unsigned char *sqlite3aLTb;
19838SQLITE_PRIVATE const unsigned char *sqlite3aEQb;
19839SQLITE_PRIVATE const unsigned char *sqlite3aGTb;
19840SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[];
19841SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config;
19842SQLITE_PRIVATE FuncDefHash sqlite3BuiltinFunctions;
19843#ifndef SQLITE_OMIT_WSD
19844SQLITE_PRIVATE int sqlite3PendingByte;
19845#endif
19846#endif /* SQLITE_AMALGAMATION */
19847#ifdef VDBE_PROFILE
19848SQLITE_PRIVATE sqlite3_uint64 sqlite3NProfileCnt;
19849#endif
19850SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, Pgno, Pgno);
19851SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
19852SQLITE_PRIVATE void sqlite3AlterFunctions(void);
19853SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
19854SQLITE_PRIVATE void sqlite3AlterRenameColumn(Parse*, SrcList*, Token*, Token*);
19855SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
19856SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
19857SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*, int);
19858SQLITE_PRIVATE void sqlite3CodeRhsOfIN(Parse*, Expr*, int);
19859SQLITE_PRIVATE int sqlite3CodeSubselect(Parse*, Expr*);
19860SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
19861SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse*, SrcItem*);
19862SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p);
19863SQLITE_PRIVATE int sqlite3MatchEName(
19864 const struct ExprList_item*,
19865 const char*,
19866 const char*,
19867 const char*
19868);
19869SQLITE_PRIVATE Bitmask sqlite3ExprColUsed(Expr*);
19870SQLITE_PRIVATE u8 sqlite3StrIHash(const char*);
19871SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
19872SQLITE_PRIVATE int sqlite3ResolveExprListNames(NameContext*, ExprList*);
19873SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
19874SQLITE_PRIVATE int sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
19875SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
19876SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
19877SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
19878SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
19879SQLITE_PRIVATE void sqlite3AlterDropColumn(Parse*, SrcList*, const Token*);
19880SQLITE_PRIVATE const void *sqlite3RenameTokenMap(Parse*, const void*, const Token*);
19881SQLITE_PRIVATE void sqlite3RenameTokenRemap(Parse*, const void *pTo, const void *pFrom);
19882SQLITE_PRIVATE void sqlite3RenameExprUnmap(Parse*, Expr*);
19883SQLITE_PRIVATE void sqlite3RenameExprlistUnmap(Parse*, ExprList*);
19884SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
19885SQLITE_PRIVATE char sqlite3AffinityType(const char*, Column*);
19886SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
19887SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*);
19888SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
19889SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
19890SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
19891SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*);
19892SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
19893SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int);
19894SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
19895SQLITE_PRIVATE void sqlite3SchemaClear(void *);
19896SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
19897SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
19898SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3*,int,int);
19899SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo*);
19900SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoRef(KeyInfo*);
19901SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoOfIndex(Parse*, Index*);
19902SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoFromExprList(Parse*, ExprList*, int, int);
19903SQLITE_PRIVATE const char *sqlite3SelectOpName(int);
19904SQLITE_PRIVATE int sqlite3HasExplicitNulls(Parse*, ExprList*);
19905
19906#ifdef SQLITE_DEBUG
19907SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo*);
19908#endif
19909SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
19910 void (*)(sqlite3_context*,int,sqlite3_value **),
19911 void (*)(sqlite3_context*,int,sqlite3_value **),
19912 void (*)(sqlite3_context*),
19913 void (*)(sqlite3_context*),
19914 void (*)(sqlite3_context*,int,sqlite3_value **),
19915 FuncDestructor *pDestructor
19916);
19917SQLITE_PRIVATE void sqlite3NoopDestructor(void*);
19918SQLITE_PRIVATE void sqlite3OomFault(sqlite3*);
19919SQLITE_PRIVATE void sqlite3OomClear(sqlite3*);
19920SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
19921SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
19922
19923SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int);
19924SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
19925SQLITE_PRIVATE void sqlite3StrAccumSetError(StrAccum*, u8);
19926SQLITE_PRIVATE void sqlite3ResultStrAccum(sqlite3_context*,StrAccum*);
19927SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
19928SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
19929
19930SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
19931SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
19932
19933#ifndef SQLITE_OMIT_SUBQUERY
19934SQLITE_PRIVATE int sqlite3ExprCheckIN(Parse*, Expr*);
19935#else
19936# define sqlite3ExprCheckIN(x,y) SQLITE_OK
19937#endif
19938
19939#ifdef SQLITE_ENABLE_STAT4
19940SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(
19941 Parse*,Index*,UnpackedRecord**,Expr*,int,int,int*);
19942SQLITE_PRIVATE int sqlite3Stat4ValueFromExpr(Parse*, Expr*, u8, sqlite3_value**);
19943SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord*);
19944SQLITE_PRIVATE int sqlite3Stat4Column(sqlite3*, const void*, int, int, sqlite3_value**);
19945SQLITE_PRIVATE char sqlite3IndexColumnAffinity(sqlite3*, Index*, int);
19946#endif
19947
19948/*
19949** The interface to the LEMON-generated parser
19950*/
19951#ifndef SQLITE_AMALGAMATION
19952SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(u64), Parse*);
19953SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
19954#endif
19955SQLITE_PRIVATE void sqlite3Parser(void*, int, Token);
19956SQLITE_PRIVATE int sqlite3ParserFallback(int);
19957#ifdef YYTRACKMAXSTACKDEPTH
19958SQLITE_PRIVATE int sqlite3ParserStackPeak(void*);
19959#endif
19960
19961SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3*);
19962#ifndef SQLITE_OMIT_LOAD_EXTENSION
19963SQLITE_PRIVATE void sqlite3CloseExtensions(sqlite3*);
19964#else
19965# define sqlite3CloseExtensions(X)
19966#endif
19967
19968#ifndef SQLITE_OMIT_SHARED_CACHE
19969SQLITE_PRIVATE void sqlite3TableLock(Parse *, int, Pgno, u8, const char *);
19970#else
19971 #define sqlite3TableLock(v,w,x,y,z)
19972#endif
19973
19974#ifdef SQLITE_TEST
19975SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char*);
19976#endif
19977
19978#ifdef SQLITE_OMIT_VIRTUALTABLE
19979# define sqlite3VtabClear(D,T)
19980# define sqlite3VtabSync(X,Y) SQLITE_OK
19981# define sqlite3VtabRollback(X)
19982# define sqlite3VtabCommit(X)
19983# define sqlite3VtabInSync(db) 0
19984# define sqlite3VtabLock(X)
19985# define sqlite3VtabUnlock(X)
19986# define sqlite3VtabModuleUnref(D,X)
19987# define sqlite3VtabUnlockList(X)
19988# define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK
19989# define sqlite3GetVTable(X,Y) ((VTable*)0)
19990#else
19991SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table*);
19992SQLITE_PRIVATE void sqlite3VtabDisconnect(sqlite3 *db, Table *p);
19993SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, Vdbe*);
19994SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db);
19995SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db);
19996SQLITE_PRIVATE void sqlite3VtabLock(VTable *);
19997SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *);
19998SQLITE_PRIVATE void sqlite3VtabModuleUnref(sqlite3*,Module*);
19999SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3*);
20000SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *, int, int);
20001SQLITE_PRIVATE void sqlite3VtabImportErrmsg(Vdbe*, sqlite3_vtab*);
20002SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3*, Table*);
20003SQLITE_PRIVATE Module *sqlite3VtabCreateModule(
20004 sqlite3*,
20005 const char*,
20006 const sqlite3_module*,
20007 void*,
20008 void(*)(void*)
20009 );
20010# define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
20011#endif
20012SQLITE_PRIVATE int sqlite3ReadOnlyShadowTables(sqlite3 *db);
20013#ifndef SQLITE_OMIT_VIRTUALTABLE
20014SQLITE_PRIVATE int sqlite3ShadowTableName(sqlite3 *db, const char *zName);
20015SQLITE_PRIVATE int sqlite3IsShadowTableOf(sqlite3*,Table*,const char*);
20016SQLITE_PRIVATE void sqlite3MarkAllShadowTablesOf(sqlite3*, Table*);
20017#else
20018# define sqlite3ShadowTableName(A,B) 0
20019# define sqlite3IsShadowTableOf(A,B,C) 0
20020# define sqlite3MarkAllShadowTablesOf(A,B)
20021#endif
20022SQLITE_PRIVATE int sqlite3VtabEponymousTableInit(Parse*,Module*);
20023SQLITE_PRIVATE void sqlite3VtabEponymousTableClear(sqlite3*,Module*);
20024SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*);
20025SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*, int);
20026SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse*, Token*);
20027SQLITE_PRIVATE void sqlite3VtabArgInit(Parse*);
20028SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse*, Token*);
20029SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
20030SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*);
20031SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
20032SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *);
20033SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
20034SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
20035SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
20036SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
20037SQLITE_PRIVATE void sqlite3ParserReset(Parse*);
20038SQLITE_PRIVATE void *sqlite3ParserAddCleanup(Parse*,void(*)(sqlite3*,void*),void*);
20039#ifdef SQLITE_ENABLE_NORMALIZE
20040SQLITE_PRIVATE char *sqlite3Normalize(Vdbe*, const char*);
20041#endif
20042SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
20043SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
20044SQLITE_PRIVATE CollSeq *sqlite3ExprCompareCollSeq(Parse*,const Expr*);
20045SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, const Expr*, const Expr*);
20046SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
20047SQLITE_PRIVATE const char *sqlite3JournalModename(int);
20048#ifndef SQLITE_OMIT_WAL
20049SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
20050SQLITE_PRIVATE int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
20051#endif
20052#ifndef SQLITE_OMIT_CTE
20053SQLITE_PRIVATE Cte *sqlite3CteNew(Parse*,Token*,ExprList*,Select*,u8);
20054SQLITE_PRIVATE void sqlite3CteDelete(sqlite3*,Cte*);
20055SQLITE_PRIVATE With *sqlite3WithAdd(Parse*,With*,Cte*);
20056SQLITE_PRIVATE void sqlite3WithDelete(sqlite3*,With*);
20057SQLITE_PRIVATE With *sqlite3WithPush(Parse*, With*, u8);
20058#else
20059# define sqlite3CteNew(P,T,E,S) ((void*)0)
20060# define sqlite3CteDelete(D,C)
20061# define sqlite3CteWithAdd(P,W,C) ((void*)0)
20062# define sqlite3WithDelete(x,y)
20063# define sqlite3WithPush(x,y,z) ((void*)0)
20064#endif
20065#ifndef SQLITE_OMIT_UPSERT
20066SQLITE_PRIVATE Upsert *sqlite3UpsertNew(sqlite3*,ExprList*,Expr*,ExprList*,Expr*,Upsert*);
20067SQLITE_PRIVATE void sqlite3UpsertDelete(sqlite3*,Upsert*);
20068SQLITE_PRIVATE Upsert *sqlite3UpsertDup(sqlite3*,Upsert*);
20069SQLITE_PRIVATE int sqlite3UpsertAnalyzeTarget(Parse*,SrcList*,Upsert*);
20070SQLITE_PRIVATE void sqlite3UpsertDoUpdate(Parse*,Upsert*,Table*,Index*,int);
20071SQLITE_PRIVATE Upsert *sqlite3UpsertOfIndex(Upsert*,Index*);
20072SQLITE_PRIVATE int sqlite3UpsertNextIsIPK(Upsert*);
20073#else
20074#define sqlite3UpsertNew(u,v,w,x,y,z) ((Upsert*)0)
20075#define sqlite3UpsertDelete(x,y)
20076#define sqlite3UpsertDup(x,y) ((Upsert*)0)
20077#define sqlite3UpsertOfIndex(x,y) ((Upsert*)0)
20078#define sqlite3UpsertNextIsIPK(x) 0
20079#endif
20080
20081
20082/* Declarations for functions in fkey.c. All of these are replaced by
20083** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
20084** key functionality is available. If OMIT_TRIGGER is defined but
20085** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
20086** this case foreign keys are parsed, but no other functionality is
20087** provided (enforcement of FK constraints requires the triggers sub-system).
20088*/
20089#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
20090SQLITE_PRIVATE void sqlite3FkCheck(Parse*, Table*, int, int, int*, int);
20091SQLITE_PRIVATE void sqlite3FkDropTable(Parse*, SrcList *, Table*);
20092SQLITE_PRIVATE void sqlite3FkActions(Parse*, Table*, ExprList*, int, int*, int);
20093SQLITE_PRIVATE int sqlite3FkRequired(Parse*, Table*, int*, int);
20094SQLITE_PRIVATE u32 sqlite3FkOldmask(Parse*, Table*);
20095SQLITE_PRIVATE FKey *sqlite3FkReferences(Table *);
20096SQLITE_PRIVATE void sqlite3FkClearTriggerCache(sqlite3*,int);
20097#else
20098 #define sqlite3FkActions(a,b,c,d,e,f)
20099 #define sqlite3FkCheck(a,b,c,d,e,f)
20100 #define sqlite3FkDropTable(a,b,c)
20101 #define sqlite3FkOldmask(a,b) 0
20102 #define sqlite3FkRequired(a,b,c,d) 0
20103 #define sqlite3FkReferences(a) 0
20104 #define sqlite3FkClearTriggerCache(a,b)
20105#endif
20106#ifndef SQLITE_OMIT_FOREIGN_KEY
20107SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *, Table*);
20108SQLITE_PRIVATE int sqlite3FkLocateIndex(Parse*,Table*,FKey*,Index**,int**);
20109#else
20110 #define sqlite3FkDelete(a,b)
20111 #define sqlite3FkLocateIndex(a,b,c,d,e)
20112#endif
20113
20114
20115/*
20116** Available fault injectors. Should be numbered beginning with 0.
20117*/
20118#define SQLITE_FAULTINJECTOR_MALLOC 0
20119#define SQLITE_FAULTINJECTOR_COUNT 1
20120
20121/*
20122** The interface to the code in fault.c used for identifying "benign"
20123** malloc failures. This is only present if SQLITE_UNTESTABLE
20124** is not defined.
20125*/
20126#ifndef SQLITE_UNTESTABLE
20127SQLITE_PRIVATE void sqlite3BeginBenignMalloc(void);
20128SQLITE_PRIVATE void sqlite3EndBenignMalloc(void);
20129#else
20130 #define sqlite3BeginBenignMalloc()
20131 #define sqlite3EndBenignMalloc()
20132#endif
20133
20134/*
20135** Allowed return values from sqlite3FindInIndex()
20136*/
20137#define IN_INDEX_ROWID 1 /* Search the rowid of the table */
20138#define IN_INDEX_EPH 2 /* Search an ephemeral b-tree */
20139#define IN_INDEX_INDEX_ASC 3 /* Existing index ASCENDING */
20140#define IN_INDEX_INDEX_DESC 4 /* Existing index DESCENDING */
20141#define IN_INDEX_NOOP 5 /* No table available. Use comparisons */
20142/*
20143** Allowed flags for the 3rd parameter to sqlite3FindInIndex().
20144*/
20145#define IN_INDEX_NOOP_OK 0x0001 /* OK to return IN_INDEX_NOOP */
20146#define IN_INDEX_MEMBERSHIP 0x0002 /* IN operator used for membership test */
20147#define IN_INDEX_LOOP 0x0004 /* IN operator used as a loop */
20148SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, u32, int*, int*, int*);
20149
20150SQLITE_PRIVATE int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
20151SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *);
20152#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
20153 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
20154SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *);
20155#endif
20156
20157SQLITE_PRIVATE int sqlite3JournalIsInMemory(sqlite3_file *p);
20158SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *);
20159
20160SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p);
20161#if SQLITE_MAX_EXPR_DEPTH>0
20162SQLITE_PRIVATE int sqlite3SelectExprHeight(const Select *);
20163SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse*, int);
20164#else
20165 #define sqlite3SelectExprHeight(x) 0
20166 #define sqlite3ExprCheckHeight(x,y)
20167#endif
20168
20169SQLITE_PRIVATE u32 sqlite3Get4byte(const u8*);
20170SQLITE_PRIVATE void sqlite3Put4byte(u8*, u32);
20171
20172#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
20173SQLITE_PRIVATE void sqlite3ConnectionBlocked(sqlite3 *, sqlite3 *);
20174SQLITE_PRIVATE void sqlite3ConnectionUnlocked(sqlite3 *db);
20175SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db);
20176#else
20177 #define sqlite3ConnectionBlocked(x,y)
20178 #define sqlite3ConnectionUnlocked(x)
20179 #define sqlite3ConnectionClosed(x)
20180#endif
20181
20182#ifdef SQLITE_DEBUG
20183SQLITE_PRIVATE void sqlite3ParserTrace(FILE*, char *);
20184#endif
20185#if defined(YYCOVERAGE)
20186SQLITE_PRIVATE int sqlite3ParserCoverage(FILE*);
20187#endif
20188
20189/*
20190** If the SQLITE_ENABLE IOTRACE exists then the global variable
20191** sqlite3IoTrace is a pointer to a printf-like routine used to
20192** print I/O tracing messages.
20193*/
20194#ifdef SQLITE_ENABLE_IOTRACE
20195# define IOTRACE(A) if( sqlite3IoTrace ){ sqlite3IoTrace A; }
20196SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe*);
20197SQLITE_API SQLITE_EXTERN void (SQLITE_CDECL *sqlite3IoTrace)(const char*,...);
20198#else
20199# define IOTRACE(A)
20200# define sqlite3VdbeIOTraceSql(X)
20201#endif
20202
20203/*
20204** These routines are available for the mem2.c debugging memory allocator
20205** only. They are used to verify that different "types" of memory
20206** allocations are properly tracked by the system.
20207**
20208** sqlite3MemdebugSetType() sets the "type" of an allocation to one of
20209** the MEMTYPE_* macros defined below. The type must be a bitmask with
20210** a single bit set.
20211**
20212** sqlite3MemdebugHasType() returns true if any of the bits in its second
20213** argument match the type set by the previous sqlite3MemdebugSetType().
20214** sqlite3MemdebugHasType() is intended for use inside assert() statements.
20215**
20216** sqlite3MemdebugNoType() returns true if none of the bits in its second
20217** argument match the type set by the previous sqlite3MemdebugSetType().
20218**
20219** Perhaps the most important point is the difference between MEMTYPE_HEAP
20220** and MEMTYPE_LOOKASIDE. If an allocation is MEMTYPE_LOOKASIDE, that means
20221** it might have been allocated by lookaside, except the allocation was
20222** too large or lookaside was already full. It is important to verify
20223** that allocations that might have been satisfied by lookaside are not
20224** passed back to non-lookaside free() routines. Asserts such as the
20225** example above are placed on the non-lookaside free() routines to verify
20226** this constraint.
20227**
20228** All of this is no-op for a production build. It only comes into
20229** play when the SQLITE_MEMDEBUG compile-time option is used.
20230*/
20231#ifdef SQLITE_MEMDEBUG
20232SQLITE_PRIVATE void sqlite3MemdebugSetType(void*,u8);
20233SQLITE_PRIVATE int sqlite3MemdebugHasType(const void*,u8);
20234SQLITE_PRIVATE int sqlite3MemdebugNoType(const void*,u8);
20235#else
20236# define sqlite3MemdebugSetType(X,Y) /* no-op */
20237# define sqlite3MemdebugHasType(X,Y) 1
20238# define sqlite3MemdebugNoType(X,Y) 1
20239#endif
20240#define MEMTYPE_HEAP 0x01 /* General heap allocations */
20241#define MEMTYPE_LOOKASIDE 0x02 /* Heap that might have been lookaside */
20242#define MEMTYPE_PCACHE 0x04 /* Page cache allocations */
20243
20244/*
20245** Threading interface
20246*/
20247#if SQLITE_MAX_WORKER_THREADS>0
20248SQLITE_PRIVATE int sqlite3ThreadCreate(SQLiteThread**,void*(*)(void*),void*);
20249SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread*, void**);
20250#endif
20251
20252#if defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)
20253SQLITE_PRIVATE int sqlite3DbpageRegister(sqlite3*);
20254#endif
20255#if defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)
20256SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3*);
20257#endif
20258
20259SQLITE_PRIVATE int sqlite3ExprVectorSize(const Expr *pExpr);
20260SQLITE_PRIVATE int sqlite3ExprIsVector(const Expr *pExpr);
20261SQLITE_PRIVATE Expr *sqlite3VectorFieldSubexpr(Expr*, int);
20262SQLITE_PRIVATE Expr *sqlite3ExprForVectorField(Parse*,Expr*,int,int);
20263SQLITE_PRIVATE void sqlite3VectorErrorMsg(Parse*, Expr*);
20264
20265#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
20266SQLITE_PRIVATE const char **sqlite3CompileOptions(int *pnOpt);
20267#endif
20268
20269#endif /* SQLITEINT_H */
20270
20271/************** End of sqliteInt.h *******************************************/
20272/************** Begin file os_common.h ***************************************/
20273/*
20274** 2004 May 22
20275**
20276** The author disclaims copyright to this source code. In place of
20277** a legal notice, here is a blessing:
20278**
20279** May you do good and not evil.
20280** May you find forgiveness for yourself and forgive others.
20281** May you share freely, never taking more than you give.
20282**
20283******************************************************************************
20284**
20285** This file contains macros and a little bit of code that is common to
20286** all of the platform-specific files (os_*.c) and is #included into those
20287** files.
20288**
20289** This file should be #included by the os_*.c files only. It is not a
20290** general purpose header file.
20291*/
20292#ifndef _OS_COMMON_H_
20293#define _OS_COMMON_H_
20294
20295/*
20296** At least two bugs have slipped in because we changed the MEMORY_DEBUG
20297** macro to SQLITE_DEBUG and some older makefiles have not yet made the
20298** switch. The following code should catch this problem at compile-time.
20299*/
20300#ifdef MEMORY_DEBUG
20301# error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
20302#endif
20303
20304/*
20305** Macros for performance tracing. Normally turned off. Only works
20306** on i486 hardware.
20307*/
20308#ifdef SQLITE_PERFORMANCE_TRACE
20309
20310/*
20311** hwtime.h contains inline assembler code for implementing
20312** high-performance timing routines.
20313*/
20314/************** Include hwtime.h in the middle of os_common.h ****************/
20315/************** Begin file hwtime.h ******************************************/
20316/*
20317** 2008 May 27
20318**
20319** The author disclaims copyright to this source code. In place of
20320** a legal notice, here is a blessing:
20321**
20322** May you do good and not evil.
20323** May you find forgiveness for yourself and forgive others.
20324** May you share freely, never taking more than you give.
20325**
20326******************************************************************************
20327**
20328** This file contains inline asm code for retrieving "high-performance"
20329** counters for x86 and x86_64 class CPUs.
20330*/
20331#ifndef SQLITE_HWTIME_H
20332#define SQLITE_HWTIME_H
20333
20334/*
20335** The following routine only works on pentium-class (or newer) processors.
20336** It uses the RDTSC opcode to read the cycle count value out of the
20337** processor and returns that value. This can be used for high-res
20338** profiling.
20339*/
20340#if !defined(__STRICT_ANSI__) && \
20341 (defined(__GNUC__) || defined(_MSC_VER)) && \
20342 (defined(i386) || defined(__i386__) || defined(_M_IX86))
20343
20344 #if defined(__GNUC__)
20345
20346 __inline__ sqlite_uint64 sqlite3Hwtime(void){
20347 unsigned int lo, hi;
20348 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
20349 return (sqlite_uint64)hi << 32 | lo;
20350 }
20351
20352 #elif defined(_MSC_VER)
20353
20354 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
20355 __asm {
20356 rdtsc
20357 ret ; return value at EDX:EAX
20358 }
20359 }
20360
20361 #endif
20362
20363#elif !defined(__STRICT_ANSI__) && (defined(__GNUC__) && defined(__x86_64__))
20364
20365 __inline__ sqlite_uint64 sqlite3Hwtime(void){
20366 unsigned long val;
20367 __asm__ __volatile__ ("rdtsc" : "=A" (val));
20368 return val;
20369 }
20370
20371#elif !defined(__STRICT_ANSI__) && (defined(__GNUC__) && defined(__ppc__))
20372
20373 __inline__ sqlite_uint64 sqlite3Hwtime(void){
20374 unsigned long long retval;
20375 unsigned long junk;
20376 __asm__ __volatile__ ("\n\
20377 1: mftbu %1\n\
20378 mftb %L0\n\
20379 mftbu %0\n\
20380 cmpw %0,%1\n\
20381 bne 1b"
20382 : "=r" (retval), "=r" (junk));
20383 return retval;
20384 }
20385
20386#else
20387
20388 /*
20389 ** asm() is needed for hardware timing support. Without asm(),
20390 ** disable the sqlite3Hwtime() routine.
20391 **
20392 ** sqlite3Hwtime() is only used for some obscure debugging
20393 ** and analysis configurations, not in any deliverable, so this
20394 ** should not be a great loss.
20395 */
20396SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
20397
20398#endif
20399
20400#endif /* !defined(SQLITE_HWTIME_H) */
20401
20402/************** End of hwtime.h **********************************************/
20403/************** Continuing where we left off in os_common.h ******************/
20404
20405static sqlite_uint64 g_start;
20406static sqlite_uint64 g_elapsed;
20407#define TIMER_START g_start=sqlite3Hwtime()
20408#define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
20409#define TIMER_ELAPSED g_elapsed
20410#else
20411#define TIMER_START
20412#define TIMER_END
20413#define TIMER_ELAPSED ((sqlite_uint64)0)
20414#endif
20415
20416/*
20417** If we compile with the SQLITE_TEST macro set, then the following block
20418** of code will give us the ability to simulate a disk I/O error. This
20419** is used for testing the I/O recovery logic.
20420*/
20421#if defined(SQLITE_TEST)
20422SQLITE_API extern int sqlite3_io_error_hit;
20423SQLITE_API extern int sqlite3_io_error_hardhit;
20424SQLITE_API extern int sqlite3_io_error_pending;
20425SQLITE_API extern int sqlite3_io_error_persist;
20426SQLITE_API extern int sqlite3_io_error_benign;
20427SQLITE_API extern int sqlite3_diskfull_pending;
20428SQLITE_API extern int sqlite3_diskfull;
20429#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
20430#define SimulateIOError(CODE) \
20431 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
20432 || sqlite3_io_error_pending-- == 1 ) \
20433 { local_ioerr(); CODE; }
20434static void local_ioerr(){
20435 IOTRACE(("IOERR\n"));
20436 sqlite3_io_error_hit++;
20437 if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
20438}
20439#define SimulateDiskfullError(CODE) \
20440 if( sqlite3_diskfull_pending ){ \
20441 if( sqlite3_diskfull_pending == 1 ){ \
20442 local_ioerr(); \
20443 sqlite3_diskfull = 1; \
20444 sqlite3_io_error_hit = 1; \
20445 CODE; \
20446 }else{ \
20447 sqlite3_diskfull_pending--; \
20448 } \
20449 }
20450#else
20451#define SimulateIOErrorBenign(X)
20452#define SimulateIOError(A)
20453#define SimulateDiskfullError(A)
20454#endif /* defined(SQLITE_TEST) */
20455
20456/*
20457** When testing, keep a count of the number of open files.
20458*/
20459#if defined(SQLITE_TEST)
20460SQLITE_API extern int sqlite3_open_file_count;
20461#define OpenCounter(X) sqlite3_open_file_count+=(X)
20462#else
20463#define OpenCounter(X)
20464#endif /* defined(SQLITE_TEST) */
20465
20466#endif /* !defined(_OS_COMMON_H_) */
20467
20468/************** End of os_common.h *******************************************/
20469/************** Begin file ctime.c *******************************************/
20470/*
20471** 2010 February 23
20472**
20473** The author disclaims copyright to this source code. In place of
20474** a legal notice, here is a blessing:
20475**
20476** May you do good and not evil.
20477** May you find forgiveness for yourself and forgive others.
20478** May you share freely, never taking more than you give.
20479**
20480*************************************************************************
20481**
20482** This file implements routines used to report what compile-time options
20483** SQLite was built with.
20484*/
20485#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS /* IMP: R-16824-07538 */
20486
20487/*
20488** Include the configuration header output by 'configure' if we're using the
20489** autoconf-based build
20490*/
20491#if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
20492/* #include "config.h" */
20493#define SQLITECONFIG_H 1
20494#endif
20495
20496/* These macros are provided to "stringify" the value of the define
20497** for those options in which the value is meaningful. */
20498#define CTIMEOPT_VAL_(opt) #opt
20499#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
20500
20501/* Like CTIMEOPT_VAL, but especially for SQLITE_DEFAULT_LOOKASIDE. This
20502** option requires a separate macro because legal values contain a single
20503** comma. e.g. (-DSQLITE_DEFAULT_LOOKASIDE="100,100") */
20504#define CTIMEOPT_VAL2_(opt1,opt2) #opt1 "," #opt2
20505#define CTIMEOPT_VAL2(opt) CTIMEOPT_VAL2_(opt)
20506/* #include "sqliteInt.h" */
20507
20508/*
20509** An array of names of all compile-time options. This array should
20510** be sorted A-Z.
20511**
20512** This array looks large, but in a typical installation actually uses
20513** only a handful of compile-time options, so most times this array is usually
20514** rather short and uses little memory space.
20515*/
20516static const char * const sqlite3azCompileOpt[] = {
20517
20518/*
20519** BEGIN CODE GENERATED BY tool/mkctime.tcl
20520*/
20521#ifdef SQLITE_32BIT_ROWID
20522 "32BIT_ROWID",
20523#endif
20524#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
20525 "4_BYTE_ALIGNED_MALLOC",
20526#endif
20527#ifdef SQLITE_64BIT_STATS
20528 "64BIT_STATS",
20529#endif
20530#ifdef SQLITE_ALLOW_COVERING_INDEX_SCAN
20531# if SQLITE_ALLOW_COVERING_INDEX_SCAN != 1
20532 "ALLOW_COVERING_INDEX_SCAN=" CTIMEOPT_VAL(SQLITE_ALLOW_COVERING_INDEX_SCAN),
20533# endif
20534#endif
20535#ifdef SQLITE_ALLOW_URI_AUTHORITY
20536 "ALLOW_URI_AUTHORITY",
20537#endif
20538#ifdef SQLITE_ATOMIC_INTRINSICS
20539 "ATOMIC_INTRINSICS=" CTIMEOPT_VAL(SQLITE_ATOMIC_INTRINSICS),
20540#endif
20541#ifdef SQLITE_BITMASK_TYPE
20542 "BITMASK_TYPE=" CTIMEOPT_VAL(SQLITE_BITMASK_TYPE),
20543#endif
20544#ifdef SQLITE_BUG_COMPATIBLE_20160819
20545 "BUG_COMPATIBLE_20160819",
20546#endif
20547#ifdef SQLITE_CASE_SENSITIVE_LIKE
20548 "CASE_SENSITIVE_LIKE",
20549#endif
20550#ifdef SQLITE_CHECK_PAGES
20551 "CHECK_PAGES",
20552#endif
20553#if defined(__clang__) && defined(__clang_major__)
20554 "COMPILER=clang-" CTIMEOPT_VAL(__clang_major__) "."
20555 CTIMEOPT_VAL(__clang_minor__) "."
20556 CTIMEOPT_VAL(__clang_patchlevel__),
20557#elif defined(_MSC_VER)
20558 "COMPILER=msvc-" CTIMEOPT_VAL(_MSC_VER),
20559#elif defined(__GNUC__) && defined(__VERSION__)
20560 "COMPILER=gcc-" __VERSION__,
20561#endif
20562#ifdef SQLITE_COVERAGE_TEST
20563 "COVERAGE_TEST",
20564#endif
20565#ifdef SQLITE_DEBUG
20566 "DEBUG",
20567#endif
20568#ifdef SQLITE_DEFAULT_AUTOMATIC_INDEX
20569 "DEFAULT_AUTOMATIC_INDEX",
20570#endif
20571#ifdef SQLITE_DEFAULT_AUTOVACUUM
20572 "DEFAULT_AUTOVACUUM",
20573#endif
20574#ifdef SQLITE_DEFAULT_CACHE_SIZE
20575 "DEFAULT_CACHE_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_CACHE_SIZE),
20576#endif
20577#ifdef SQLITE_DEFAULT_CKPTFULLFSYNC
20578 "DEFAULT_CKPTFULLFSYNC",
20579#endif
20580#ifdef SQLITE_DEFAULT_FILE_FORMAT
20581 "DEFAULT_FILE_FORMAT=" CTIMEOPT_VAL(SQLITE_DEFAULT_FILE_FORMAT),
20582#endif
20583#ifdef SQLITE_DEFAULT_FILE_PERMISSIONS
20584 "DEFAULT_FILE_PERMISSIONS=" CTIMEOPT_VAL(SQLITE_DEFAULT_FILE_PERMISSIONS),
20585#endif
20586#ifdef SQLITE_DEFAULT_FOREIGN_KEYS
20587 "DEFAULT_FOREIGN_KEYS",
20588#endif
20589#ifdef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT
20590 "DEFAULT_JOURNAL_SIZE_LIMIT=" CTIMEOPT_VAL(SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT),
20591#endif
20592#ifdef SQLITE_DEFAULT_LOCKING_MODE
20593 "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE),
20594#endif
20595#ifdef SQLITE_DEFAULT_LOOKASIDE
20596 "DEFAULT_LOOKASIDE=" CTIMEOPT_VAL2(SQLITE_DEFAULT_LOOKASIDE),
20597#endif
20598#ifdef SQLITE_DEFAULT_MEMSTATUS
20599# if SQLITE_DEFAULT_MEMSTATUS != 1
20600 "DEFAULT_MEMSTATUS=" CTIMEOPT_VAL(SQLITE_DEFAULT_MEMSTATUS),
20601# endif
20602#endif
20603#ifdef SQLITE_DEFAULT_MMAP_SIZE
20604 "DEFAULT_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_MMAP_SIZE),
20605#endif
20606#ifdef SQLITE_DEFAULT_PAGE_SIZE
20607 "DEFAULT_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_PAGE_SIZE),
20608#endif
20609#ifdef SQLITE_DEFAULT_PCACHE_INITSZ
20610 "DEFAULT_PCACHE_INITSZ=" CTIMEOPT_VAL(SQLITE_DEFAULT_PCACHE_INITSZ),
20611#endif
20612#ifdef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
20613 "DEFAULT_PROXYDIR_PERMISSIONS=" CTIMEOPT_VAL(SQLITE_DEFAULT_PROXYDIR_PERMISSIONS),
20614#endif
20615#ifdef SQLITE_DEFAULT_RECURSIVE_TRIGGERS
20616 "DEFAULT_RECURSIVE_TRIGGERS",
20617#endif
20618#ifdef SQLITE_DEFAULT_ROWEST
20619 "DEFAULT_ROWEST=" CTIMEOPT_VAL(SQLITE_DEFAULT_ROWEST),
20620#endif
20621#ifdef SQLITE_DEFAULT_SECTOR_SIZE
20622 "DEFAULT_SECTOR_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_SECTOR_SIZE),
20623#endif
20624#ifdef SQLITE_DEFAULT_SYNCHRONOUS
20625 "DEFAULT_SYNCHRONOUS=" CTIMEOPT_VAL(SQLITE_DEFAULT_SYNCHRONOUS),
20626#endif
20627#ifdef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT
20628 "DEFAULT_WAL_AUTOCHECKPOINT=" CTIMEOPT_VAL(SQLITE_DEFAULT_WAL_AUTOCHECKPOINT),
20629#endif
20630#ifdef SQLITE_DEFAULT_WAL_SYNCHRONOUS
20631 "DEFAULT_WAL_SYNCHRONOUS=" CTIMEOPT_VAL(SQLITE_DEFAULT_WAL_SYNCHRONOUS),
20632#endif
20633#ifdef SQLITE_DEFAULT_WORKER_THREADS
20634 "DEFAULT_WORKER_THREADS=" CTIMEOPT_VAL(SQLITE_DEFAULT_WORKER_THREADS),
20635#endif
20636#ifdef SQLITE_DIRECT_OVERFLOW_READ
20637 "DIRECT_OVERFLOW_READ",
20638#endif
20639#ifdef SQLITE_DISABLE_DIRSYNC
20640 "DISABLE_DIRSYNC",
20641#endif
20642#ifdef SQLITE_DISABLE_FTS3_UNICODE
20643 "DISABLE_FTS3_UNICODE",
20644#endif
20645#ifdef SQLITE_DISABLE_FTS4_DEFERRED
20646 "DISABLE_FTS4_DEFERRED",
20647#endif
20648#ifdef SQLITE_DISABLE_INTRINSIC
20649 "DISABLE_INTRINSIC",
20650#endif
20651#ifdef SQLITE_DISABLE_LFS
20652 "DISABLE_LFS",
20653#endif
20654#ifdef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
20655 "DISABLE_PAGECACHE_OVERFLOW_STATS",
20656#endif
20657#ifdef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
20658 "DISABLE_SKIPAHEAD_DISTINCT",
20659#endif
20660#ifdef SQLITE_ENABLE_8_3_NAMES
20661 "ENABLE_8_3_NAMES=" CTIMEOPT_VAL(SQLITE_ENABLE_8_3_NAMES),
20662#endif
20663#ifdef SQLITE_ENABLE_API_ARMOR
20664 "ENABLE_API_ARMOR",
20665#endif
20666#ifdef SQLITE_ENABLE_ATOMIC_WRITE
20667 "ENABLE_ATOMIC_WRITE",
20668#endif
20669#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
20670 "ENABLE_BATCH_ATOMIC_WRITE",
20671#endif
20672#ifdef SQLITE_ENABLE_BYTECODE_VTAB
20673 "ENABLE_BYTECODE_VTAB",
20674#endif
20675#ifdef SQLITE_ENABLE_CEROD
20676 "ENABLE_CEROD=" CTIMEOPT_VAL(SQLITE_ENABLE_CEROD),
20677#endif
20678#ifdef SQLITE_ENABLE_COLUMN_METADATA
20679 "ENABLE_COLUMN_METADATA",
20680#endif
20681#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
20682 "ENABLE_COLUMN_USED_MASK",
20683#endif
20684#ifdef SQLITE_ENABLE_COSTMULT
20685 "ENABLE_COSTMULT",
20686#endif
20687#ifdef SQLITE_ENABLE_CURSOR_HINTS
20688 "ENABLE_CURSOR_HINTS",
20689#endif
20690#ifdef SQLITE_ENABLE_DBPAGE_VTAB
20691 "ENABLE_DBPAGE_VTAB",
20692#endif
20693#ifdef SQLITE_ENABLE_DBSTAT_VTAB
20694 "ENABLE_DBSTAT_VTAB",
20695#endif
20696#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
20697 "ENABLE_EXPENSIVE_ASSERT",
20698#endif
20699#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
20700 "ENABLE_EXPLAIN_COMMENTS",
20701#endif
20702#ifdef SQLITE_ENABLE_FTS3
20703 "ENABLE_FTS3",
20704#endif
20705#ifdef SQLITE_ENABLE_FTS3_PARENTHESIS
20706 "ENABLE_FTS3_PARENTHESIS",
20707#endif
20708#ifdef SQLITE_ENABLE_FTS3_TOKENIZER
20709 "ENABLE_FTS3_TOKENIZER",
20710#endif
20711#ifdef SQLITE_ENABLE_FTS4
20712 "ENABLE_FTS4",
20713#endif
20714#ifdef SQLITE_ENABLE_FTS5
20715 "ENABLE_FTS5",
20716#endif
20717#ifdef SQLITE_ENABLE_GEOPOLY
20718 "ENABLE_GEOPOLY",
20719#endif
20720#ifdef SQLITE_ENABLE_HIDDEN_COLUMNS
20721 "ENABLE_HIDDEN_COLUMNS",
20722#endif
20723#ifdef SQLITE_ENABLE_ICU
20724 "ENABLE_ICU",
20725#endif
20726#ifdef SQLITE_ENABLE_IOTRACE
20727 "ENABLE_IOTRACE",
20728#endif
20729#ifdef SQLITE_ENABLE_JSON1
20730 "ENABLE_JSON1",
20731#endif
20732#ifdef SQLITE_ENABLE_LOAD_EXTENSION
20733 "ENABLE_LOAD_EXTENSION",
20734#endif
20735#ifdef SQLITE_ENABLE_LOCKING_STYLE
20736 "ENABLE_LOCKING_STYLE=" CTIMEOPT_VAL(SQLITE_ENABLE_LOCKING_STYLE),
20737#endif
20738#ifdef SQLITE_ENABLE_MATH_FUNCTIONS
20739 "ENABLE_MATH_FUNCTIONS",
20740#endif
20741#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
20742 "ENABLE_MEMORY_MANAGEMENT",
20743#endif
20744#ifdef SQLITE_ENABLE_MEMSYS3
20745 "ENABLE_MEMSYS3",
20746#endif
20747#ifdef SQLITE_ENABLE_MEMSYS5
20748 "ENABLE_MEMSYS5",
20749#endif
20750#ifdef SQLITE_ENABLE_MULTIPLEX
20751 "ENABLE_MULTIPLEX",
20752#endif
20753#ifdef SQLITE_ENABLE_NORMALIZE
20754 "ENABLE_NORMALIZE",
20755#endif
20756#ifdef SQLITE_ENABLE_NULL_TRIM
20757 "ENABLE_NULL_TRIM",
20758#endif
20759#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
20760 "ENABLE_OFFSET_SQL_FUNC",
20761#endif
20762#ifdef SQLITE_ENABLE_OVERSIZE_CELL_CHECK
20763 "ENABLE_OVERSIZE_CELL_CHECK",
20764#endif
20765#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
20766 "ENABLE_PREUPDATE_HOOK",
20767#endif
20768#ifdef SQLITE_ENABLE_QPSG
20769 "ENABLE_QPSG",
20770#endif
20771#ifdef SQLITE_ENABLE_RBU
20772 "ENABLE_RBU",
20773#endif
20774#ifdef SQLITE_ENABLE_RTREE
20775 "ENABLE_RTREE",
20776#endif
20777#ifdef SQLITE_ENABLE_SELECTTRACE
20778 "ENABLE_SELECTTRACE",
20779#endif
20780#ifdef SQLITE_ENABLE_SESSION
20781 "ENABLE_SESSION",
20782#endif
20783#ifdef SQLITE_ENABLE_SNAPSHOT
20784 "ENABLE_SNAPSHOT",
20785#endif
20786#ifdef SQLITE_ENABLE_SORTER_REFERENCES
20787 "ENABLE_SORTER_REFERENCES",
20788#endif
20789#ifdef SQLITE_ENABLE_SQLLOG
20790 "ENABLE_SQLLOG",
20791#endif
20792#ifdef SQLITE_ENABLE_STAT4
20793 "ENABLE_STAT4",
20794#endif
20795#ifdef SQLITE_ENABLE_STMTVTAB
20796 "ENABLE_STMTVTAB",
20797#endif
20798#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
20799 "ENABLE_STMT_SCANSTATUS",
20800#endif
20801#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
20802 "ENABLE_UNKNOWN_SQL_FUNCTION",
20803#endif
20804#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
20805 "ENABLE_UNLOCK_NOTIFY",
20806#endif
20807#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
20808 "ENABLE_UPDATE_DELETE_LIMIT",
20809#endif
20810#ifdef SQLITE_ENABLE_URI_00_ERROR
20811 "ENABLE_URI_00_ERROR",
20812#endif
20813#ifdef SQLITE_ENABLE_VFSTRACE
20814 "ENABLE_VFSTRACE",
20815#endif
20816#ifdef SQLITE_ENABLE_WHERETRACE
20817 "ENABLE_WHERETRACE",
20818#endif
20819#ifdef SQLITE_ENABLE_ZIPVFS
20820 "ENABLE_ZIPVFS",
20821#endif
20822#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
20823 "EXPLAIN_ESTIMATED_ROWS",
20824#endif
20825#ifdef SQLITE_EXTRA_IFNULLROW
20826 "EXTRA_IFNULLROW",
20827#endif
20828#ifdef SQLITE_EXTRA_INIT
20829 "EXTRA_INIT=" CTIMEOPT_VAL(SQLITE_EXTRA_INIT),
20830#endif
20831#ifdef SQLITE_EXTRA_SHUTDOWN
20832 "EXTRA_SHUTDOWN=" CTIMEOPT_VAL(SQLITE_EXTRA_SHUTDOWN),
20833#endif
20834#ifdef SQLITE_FTS3_MAX_EXPR_DEPTH
20835 "FTS3_MAX_EXPR_DEPTH=" CTIMEOPT_VAL(SQLITE_FTS3_MAX_EXPR_DEPTH),
20836#endif
20837#ifdef SQLITE_FTS5_ENABLE_TEST_MI
20838 "FTS5_ENABLE_TEST_MI",
20839#endif
20840#ifdef SQLITE_FTS5_NO_WITHOUT_ROWID
20841 "FTS5_NO_WITHOUT_ROWID",
20842#endif
20843#if HAVE_ISNAN || SQLITE_HAVE_ISNAN
20844 "HAVE_ISNAN",
20845#endif
20846#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
20847# if SQLITE_HOMEGROWN_RECURSIVE_MUTEX != 1
20848 "HOMEGROWN_RECURSIVE_MUTEX=" CTIMEOPT_VAL(SQLITE_HOMEGROWN_RECURSIVE_MUTEX),
20849# endif
20850#endif
20851#ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
20852 "IGNORE_AFP_LOCK_ERRORS",
20853#endif
20854#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
20855 "IGNORE_FLOCK_LOCK_ERRORS",
20856#endif
20857#ifdef SQLITE_INLINE_MEMCPY
20858 "INLINE_MEMCPY",
20859#endif
20860#ifdef SQLITE_INT64_TYPE
20861 "INT64_TYPE",
20862#endif
20863#ifdef SQLITE_INTEGRITY_CHECK_ERROR_MAX
20864 "INTEGRITY_CHECK_ERROR_MAX=" CTIMEOPT_VAL(SQLITE_INTEGRITY_CHECK_ERROR_MAX),
20865#endif
20866#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
20867 "LIKE_DOESNT_MATCH_BLOBS",
20868#endif
20869#ifdef SQLITE_LOCK_TRACE
20870 "LOCK_TRACE",
20871#endif
20872#ifdef SQLITE_LOG_CACHE_SPILL
20873 "LOG_CACHE_SPILL",
20874#endif
20875#ifdef SQLITE_MALLOC_SOFT_LIMIT
20876 "MALLOC_SOFT_LIMIT=" CTIMEOPT_VAL(SQLITE_MALLOC_SOFT_LIMIT),
20877#endif
20878#ifdef SQLITE_MAX_ATTACHED
20879 "MAX_ATTACHED=" CTIMEOPT_VAL(SQLITE_MAX_ATTACHED),
20880#endif
20881#ifdef SQLITE_MAX_COLUMN
20882 "MAX_COLUMN=" CTIMEOPT_VAL(SQLITE_MAX_COLUMN),
20883#endif
20884#ifdef SQLITE_MAX_COMPOUND_SELECT
20885 "MAX_COMPOUND_SELECT=" CTIMEOPT_VAL(SQLITE_MAX_COMPOUND_SELECT),
20886#endif
20887#ifdef SQLITE_MAX_DEFAULT_PAGE_SIZE
20888 "MAX_DEFAULT_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_DEFAULT_PAGE_SIZE),
20889#endif
20890#ifdef SQLITE_MAX_EXPR_DEPTH
20891 "MAX_EXPR_DEPTH=" CTIMEOPT_VAL(SQLITE_MAX_EXPR_DEPTH),
20892#endif
20893#ifdef SQLITE_MAX_FUNCTION_ARG
20894 "MAX_FUNCTION_ARG=" CTIMEOPT_VAL(SQLITE_MAX_FUNCTION_ARG),
20895#endif
20896#ifdef SQLITE_MAX_LENGTH
20897 "MAX_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_LENGTH),
20898#endif
20899#ifdef SQLITE_MAX_LIKE_PATTERN_LENGTH
20900 "MAX_LIKE_PATTERN_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_LIKE_PATTERN_LENGTH),
20901#endif
20902#ifdef SQLITE_MAX_MEMORY
20903 "MAX_MEMORY=" CTIMEOPT_VAL(SQLITE_MAX_MEMORY),
20904#endif
20905#ifdef SQLITE_MAX_MMAP_SIZE
20906 "MAX_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE),
20907#endif
20908#ifdef SQLITE_MAX_MMAP_SIZE_
20909 "MAX_MMAP_SIZE_=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE_),
20910#endif
20911#ifdef SQLITE_MAX_PAGE_COUNT
20912 "MAX_PAGE_COUNT=" CTIMEOPT_VAL(SQLITE_MAX_PAGE_COUNT),
20913#endif
20914#ifdef SQLITE_MAX_PAGE_SIZE
20915 "MAX_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_PAGE_SIZE),
20916#endif
20917#ifdef SQLITE_MAX_SCHEMA_RETRY
20918 "MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY),
20919#endif
20920#ifdef SQLITE_MAX_SQL_LENGTH
20921 "MAX_SQL_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_SQL_LENGTH),
20922#endif
20923#ifdef SQLITE_MAX_TRIGGER_DEPTH
20924 "MAX_TRIGGER_DEPTH=" CTIMEOPT_VAL(SQLITE_MAX_TRIGGER_DEPTH),
20925#endif
20926#ifdef SQLITE_MAX_VARIABLE_NUMBER
20927 "MAX_VARIABLE_NUMBER=" CTIMEOPT_VAL(SQLITE_MAX_VARIABLE_NUMBER),
20928#endif
20929#ifdef SQLITE_MAX_VDBE_OP
20930 "MAX_VDBE_OP=" CTIMEOPT_VAL(SQLITE_MAX_VDBE_OP),
20931#endif
20932#ifdef SQLITE_MAX_WORKER_THREADS
20933 "MAX_WORKER_THREADS=" CTIMEOPT_VAL(SQLITE_MAX_WORKER_THREADS),
20934#endif
20935#ifdef SQLITE_MEMDEBUG
20936 "MEMDEBUG",
20937#endif
20938#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
20939 "MIXED_ENDIAN_64BIT_FLOAT",
20940#endif
20941#ifdef SQLITE_MMAP_READWRITE
20942 "MMAP_READWRITE",
20943#endif
20944#ifdef SQLITE_MUTEX_NOOP
20945 "MUTEX_NOOP",
20946#endif
20947#ifdef SQLITE_MUTEX_OMIT
20948 "MUTEX_OMIT",
20949#endif
20950#ifdef SQLITE_MUTEX_PTHREADS
20951 "MUTEX_PTHREADS",
20952#endif
20953#ifdef SQLITE_MUTEX_W32
20954 "MUTEX_W32",
20955#endif
20956#ifdef SQLITE_NEED_ERR_NAME
20957 "NEED_ERR_NAME",
20958#endif
20959#ifdef SQLITE_NO_SYNC
20960 "NO_SYNC",
20961#endif
20962#ifdef SQLITE_OMIT_ALTERTABLE
20963 "OMIT_ALTERTABLE",
20964#endif
20965#ifdef SQLITE_OMIT_ANALYZE
20966 "OMIT_ANALYZE",
20967#endif
20968#ifdef SQLITE_OMIT_ATTACH
20969 "OMIT_ATTACH",
20970#endif
20971#ifdef SQLITE_OMIT_AUTHORIZATION
20972 "OMIT_AUTHORIZATION",
20973#endif
20974#ifdef SQLITE_OMIT_AUTOINCREMENT
20975 "OMIT_AUTOINCREMENT",
20976#endif
20977#ifdef SQLITE_OMIT_AUTOINIT
20978 "OMIT_AUTOINIT",
20979#endif
20980#ifdef SQLITE_OMIT_AUTOMATIC_INDEX
20981 "OMIT_AUTOMATIC_INDEX",
20982#endif
20983#ifdef SQLITE_OMIT_AUTORESET
20984 "OMIT_AUTORESET",
20985#endif
20986#ifdef SQLITE_OMIT_AUTOVACUUM
20987 "OMIT_AUTOVACUUM",
20988#endif
20989#ifdef SQLITE_OMIT_BETWEEN_OPTIMIZATION
20990 "OMIT_BETWEEN_OPTIMIZATION",
20991#endif
20992#ifdef SQLITE_OMIT_BLOB_LITERAL
20993 "OMIT_BLOB_LITERAL",
20994#endif
20995#ifdef SQLITE_OMIT_CAST
20996 "OMIT_CAST",
20997#endif
20998#ifdef SQLITE_OMIT_CHECK
20999 "OMIT_CHECK",
21000#endif
21001#ifdef SQLITE_OMIT_COMPLETE
21002 "OMIT_COMPLETE",
21003#endif
21004#ifdef SQLITE_OMIT_COMPOUND_SELECT
21005 "OMIT_COMPOUND_SELECT",
21006#endif
21007#ifdef SQLITE_OMIT_CONFLICT_CLAUSE
21008 "OMIT_CONFLICT_CLAUSE",
21009#endif
21010#ifdef SQLITE_OMIT_CTE
21011 "OMIT_CTE",
21012#endif
21013#if defined(SQLITE_OMIT_DATETIME_FUNCS) || defined(SQLITE_OMIT_FLOATING_POINT)
21014 "OMIT_DATETIME_FUNCS",
21015#endif
21016#ifdef SQLITE_OMIT_DECLTYPE
21017 "OMIT_DECLTYPE",
21018#endif
21019#ifdef SQLITE_OMIT_DEPRECATED
21020 "OMIT_DEPRECATED",
21021#endif
21022#ifdef SQLITE_OMIT_DESERIALIZE
21023 "OMIT_DESERIALIZE",
21024#endif
21025#ifdef SQLITE_OMIT_DISKIO
21026 "OMIT_DISKIO",
21027#endif
21028#ifdef SQLITE_OMIT_EXPLAIN
21029 "OMIT_EXPLAIN",
21030#endif
21031#ifdef SQLITE_OMIT_FLAG_PRAGMAS
21032 "OMIT_FLAG_PRAGMAS",
21033#endif
21034#ifdef SQLITE_OMIT_FLOATING_POINT
21035 "OMIT_FLOATING_POINT",
21036#endif
21037#ifdef SQLITE_OMIT_FOREIGN_KEY
21038 "OMIT_FOREIGN_KEY",
21039#endif
21040#ifdef SQLITE_OMIT_GET_TABLE
21041 "OMIT_GET_TABLE",
21042#endif
21043#ifdef SQLITE_OMIT_HEX_INTEGER
21044 "OMIT_HEX_INTEGER",
21045#endif
21046#ifdef SQLITE_OMIT_INCRBLOB
21047 "OMIT_INCRBLOB",
21048#endif
21049#ifdef SQLITE_OMIT_INTEGRITY_CHECK
21050 "OMIT_INTEGRITY_CHECK",
21051#endif
21052#ifdef SQLITE_OMIT_INTROSPECTION_PRAGMAS
21053 "OMIT_INTROSPECTION_PRAGMAS",
21054#endif
21055#ifdef SQLITE_OMIT_LIKE_OPTIMIZATION
21056 "OMIT_LIKE_OPTIMIZATION",
21057#endif
21058#ifdef SQLITE_OMIT_LOAD_EXTENSION
21059 "OMIT_LOAD_EXTENSION",
21060#endif
21061#ifdef SQLITE_OMIT_LOCALTIME
21062 "OMIT_LOCALTIME",
21063#endif
21064#ifdef SQLITE_OMIT_LOOKASIDE
21065 "OMIT_LOOKASIDE",
21066#endif
21067#ifdef SQLITE_OMIT_MEMORYDB
21068 "OMIT_MEMORYDB",
21069#endif
21070#ifdef SQLITE_OMIT_OR_OPTIMIZATION
21071 "OMIT_OR_OPTIMIZATION",
21072#endif
21073#ifdef SQLITE_OMIT_PAGER_PRAGMAS
21074 "OMIT_PAGER_PRAGMAS",
21075#endif
21076#ifdef SQLITE_OMIT_PARSER_TRACE
21077 "OMIT_PARSER_TRACE",
21078#endif
21079#ifdef SQLITE_OMIT_POPEN
21080 "OMIT_POPEN",
21081#endif
21082#ifdef SQLITE_OMIT_PRAGMA
21083 "OMIT_PRAGMA",
21084#endif
21085#ifdef SQLITE_OMIT_PROGRESS_CALLBACK
21086 "OMIT_PROGRESS_CALLBACK",
21087#endif
21088#ifdef SQLITE_OMIT_QUICKBALANCE
21089 "OMIT_QUICKBALANCE",
21090#endif
21091#ifdef SQLITE_OMIT_REINDEX
21092 "OMIT_REINDEX",
21093#endif
21094#ifdef SQLITE_OMIT_SCHEMA_PRAGMAS
21095 "OMIT_SCHEMA_PRAGMAS",
21096#endif
21097#ifdef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
21098 "OMIT_SCHEMA_VERSION_PRAGMAS",
21099#endif
21100#ifdef SQLITE_OMIT_SHARED_CACHE
21101 "OMIT_SHARED_CACHE",
21102#endif
21103#ifdef SQLITE_OMIT_SHUTDOWN_DIRECTORIES
21104 "OMIT_SHUTDOWN_DIRECTORIES",
21105#endif
21106#ifdef SQLITE_OMIT_SUBQUERY
21107 "OMIT_SUBQUERY",
21108#endif
21109#ifdef SQLITE_OMIT_TCL_VARIABLE
21110 "OMIT_TCL_VARIABLE",
21111#endif
21112#ifdef SQLITE_OMIT_TEMPDB
21113 "OMIT_TEMPDB",
21114#endif
21115#ifdef SQLITE_OMIT_TEST_CONTROL
21116 "OMIT_TEST_CONTROL",
21117#endif
21118#ifdef SQLITE_OMIT_TRACE
21119# if SQLITE_OMIT_TRACE != 1
21120 "OMIT_TRACE=" CTIMEOPT_VAL(SQLITE_OMIT_TRACE),
21121# endif
21122#endif
21123#ifdef SQLITE_OMIT_TRIGGER
21124 "OMIT_TRIGGER",
21125#endif
21126#ifdef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
21127 "OMIT_TRUNCATE_OPTIMIZATION",
21128#endif
21129#ifdef SQLITE_OMIT_UTF16
21130 "OMIT_UTF16",
21131#endif
21132#ifdef SQLITE_OMIT_VACUUM
21133 "OMIT_VACUUM",
21134#endif
21135#ifdef SQLITE_OMIT_VIEW
21136 "OMIT_VIEW",
21137#endif
21138#ifdef SQLITE_OMIT_VIRTUALTABLE
21139 "OMIT_VIRTUALTABLE",
21140#endif
21141#ifdef SQLITE_OMIT_WAL
21142 "OMIT_WAL",
21143#endif
21144#ifdef SQLITE_OMIT_WSD
21145 "OMIT_WSD",
21146#endif
21147#ifdef SQLITE_OMIT_XFER_OPT
21148 "OMIT_XFER_OPT",
21149#endif
21150#ifdef SQLITE_PCACHE_SEPARATE_HEADER
21151 "PCACHE_SEPARATE_HEADER",
21152#endif
21153#ifdef SQLITE_PERFORMANCE_TRACE
21154 "PERFORMANCE_TRACE",
21155#endif
21156#ifdef SQLITE_POWERSAFE_OVERWRITE
21157# if SQLITE_POWERSAFE_OVERWRITE != 1
21158 "POWERSAFE_OVERWRITE=" CTIMEOPT_VAL(SQLITE_POWERSAFE_OVERWRITE),
21159# endif
21160#endif
21161#ifdef SQLITE_PREFER_PROXY_LOCKING
21162 "PREFER_PROXY_LOCKING",
21163#endif
21164#ifdef SQLITE_PROXY_DEBUG
21165 "PROXY_DEBUG",
21166#endif
21167#ifdef SQLITE_REVERSE_UNORDERED_SELECTS
21168 "REVERSE_UNORDERED_SELECTS",
21169#endif
21170#ifdef SQLITE_RTREE_INT_ONLY
21171 "RTREE_INT_ONLY",
21172#endif
21173#ifdef SQLITE_SECURE_DELETE
21174 "SECURE_DELETE",
21175#endif
21176#ifdef SQLITE_SMALL_STACK
21177 "SMALL_STACK",
21178#endif
21179#ifdef SQLITE_SORTER_PMASZ
21180 "SORTER_PMASZ=" CTIMEOPT_VAL(SQLITE_SORTER_PMASZ),
21181#endif
21182#ifdef SQLITE_SOUNDEX
21183 "SOUNDEX",
21184#endif
21185#ifdef SQLITE_STAT4_SAMPLES
21186 "STAT4_SAMPLES=" CTIMEOPT_VAL(SQLITE_STAT4_SAMPLES),
21187#endif
21188#ifdef SQLITE_STMTJRNL_SPILL
21189 "STMTJRNL_SPILL=" CTIMEOPT_VAL(SQLITE_STMTJRNL_SPILL),
21190#endif
21191#ifdef SQLITE_SUBSTR_COMPATIBILITY
21192 "SUBSTR_COMPATIBILITY",
21193#endif
21194#if (!defined(SQLITE_WIN32_MALLOC) \
21195 && !defined(SQLITE_ZERO_MALLOC) \
21196 && !defined(SQLITE_MEMDEBUG) \
21197 ) || defined(SQLITE_SYSTEM_MALLOC)
21198 "SYSTEM_MALLOC",
21199#endif
21200#ifdef SQLITE_TCL
21201 "TCL",
21202#endif
21203#ifdef SQLITE_TEMP_STORE
21204 "TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE),
21205#endif
21206#ifdef SQLITE_TEST
21207 "TEST",
21208#endif
21209#if defined(SQLITE_THREADSAFE)
21210 "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE),
21211#elif defined(THREADSAFE)
21212 "THREADSAFE=" CTIMEOPT_VAL(THREADSAFE),
21213#else
21214 "THREADSAFE=1",
21215#endif
21216#ifdef SQLITE_UNLINK_AFTER_CLOSE
21217 "UNLINK_AFTER_CLOSE",
21218#endif
21219#ifdef SQLITE_UNTESTABLE
21220 "UNTESTABLE",
21221#endif
21222#ifdef SQLITE_USER_AUTHENTICATION
21223 "USER_AUTHENTICATION",
21224#endif
21225#ifdef SQLITE_USE_ALLOCA
21226 "USE_ALLOCA",
21227#endif
21228#ifdef SQLITE_USE_FCNTL_TRACE
21229 "USE_FCNTL_TRACE",
21230#endif
21231#ifdef SQLITE_USE_URI
21232 "USE_URI",
21233#endif
21234#ifdef SQLITE_VDBE_COVERAGE
21235 "VDBE_COVERAGE",
21236#endif
21237#ifdef SQLITE_WIN32_MALLOC
21238 "WIN32_MALLOC",
21239#endif
21240#ifdef SQLITE_ZERO_MALLOC
21241 "ZERO_MALLOC",
21242#endif
21243/*
21244** END CODE GENERATED BY tool/mkctime.tcl
21245*/
21246};
21247
21248SQLITE_PRIVATE const char **sqlite3CompileOptions(int *pnOpt){
21249 *pnOpt = sizeof(sqlite3azCompileOpt) / sizeof(sqlite3azCompileOpt[0]);
21250 return (const char**)sqlite3azCompileOpt;
21251}
21252
21253#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
21254
21255/************** End of ctime.c ***********************************************/
21256/************** Begin file global.c ******************************************/
21257/*
21258** 2008 June 13
21259**
21260** The author disclaims copyright to this source code. In place of
21261** a legal notice, here is a blessing:
21262**
21263** May you do good and not evil.
21264** May you find forgiveness for yourself and forgive others.
21265** May you share freely, never taking more than you give.
21266**
21267*************************************************************************
21268**
21269** This file contains definitions of global variables and constants.
21270*/
21271/* #include "sqliteInt.h" */
21272
21273/* An array to map all upper-case characters into their corresponding
21274** lower-case character.
21275**
21276** SQLite only considers US-ASCII (or EBCDIC) characters. We do not
21277** handle case conversions for the UTF character set since the tables
21278** involved are nearly as big or bigger than SQLite itself.
21279*/
21280SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[] = {
21281#ifdef SQLITE_ASCII
21282 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
21283 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
21284 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
21285 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
21286 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
21287 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
21288 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
21289 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
21290 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
21291 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
21292 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
21293 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
21294 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
21295 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
21296 252,253,254,255,
21297#endif
21298#ifdef SQLITE_EBCDIC
21299 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 0x */
21300 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
21301 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
21302 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
21303 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
21304 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
21305 96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111, /* 6x */
21306 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127, /* 7x */
21307 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
21308 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, /* 9x */
21309 160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
21310 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
21311 192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
21312 208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
21313 224,225,162,163,164,165,166,167,168,169,234,235,236,237,238,239, /* Ex */
21314 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255, /* Fx */
21315#endif
21316/* All of the upper-to-lower conversion data is above. The following
21317** 18 integers are completely unrelated. They are appended to the
21318** sqlite3UpperToLower[] array to avoid UBSAN warnings. Here's what is
21319** going on:
21320**
21321** The SQL comparison operators (<>, =, >, <=, <, and >=) are implemented
21322** by invoking sqlite3MemCompare(A,B) which compares values A and B and
21323** returns negative, zero, or positive if A is less then, equal to, or
21324** greater than B, respectively. Then the true false results is found by
21325** consulting sqlite3aLTb[opcode], sqlite3aEQb[opcode], or
21326** sqlite3aGTb[opcode] depending on whether the result of compare(A,B)
21327** is negative, zero, or positive, where opcode is the specific opcode.
21328** The only works because the comparison opcodes are consecutive and in
21329** this order: NE EQ GT LE LT GE. Various assert()s throughout the code
21330** ensure that is the case.
21331**
21332** These elements must be appended to another array. Otherwise the
21333** index (here shown as [256-OP_Ne]) would be out-of-bounds and thus
21334** be undefined behavior. That's goofy, but the C-standards people thought
21335** it was a good idea, so here we are.
21336*/
21337/* NE EQ GT LE LT GE */
21338 1, 0, 0, 1, 1, 0, /* aLTb[]: Use when compare(A,B) less than zero */
21339 0, 1, 0, 1, 0, 1, /* aEQb[]: Use when compare(A,B) equals zero */
21340 1, 0, 1, 0, 0, 1 /* aGTb[]: Use when compare(A,B) greater than zero*/
21341};
21342SQLITE_PRIVATE const unsigned char *sqlite3aLTb = &sqlite3UpperToLower[256-OP_Ne];
21343SQLITE_PRIVATE const unsigned char *sqlite3aEQb = &sqlite3UpperToLower[256+6-OP_Ne];
21344SQLITE_PRIVATE const unsigned char *sqlite3aGTb = &sqlite3UpperToLower[256+12-OP_Ne];
21345
21346/*
21347** The following 256 byte lookup table is used to support SQLites built-in
21348** equivalents to the following standard library functions:
21349**
21350** isspace() 0x01
21351** isalpha() 0x02
21352** isdigit() 0x04
21353** isalnum() 0x06
21354** isxdigit() 0x08
21355** toupper() 0x20
21356** SQLite identifier character 0x40
21357** Quote character 0x80
21358**
21359** Bit 0x20 is set if the mapped character requires translation to upper
21360** case. i.e. if the character is a lower-case ASCII character.
21361** If x is a lower-case ASCII character, then its upper-case equivalent
21362** is (x - 0x20). Therefore toupper() can be implemented as:
21363**
21364** (x & ~(map[x]&0x20))
21365**
21366** The equivalent of tolower() is implemented using the sqlite3UpperToLower[]
21367** array. tolower() is used more often than toupper() by SQLite.
21368**
21369** Bit 0x40 is set if the character is non-alphanumeric and can be used in an
21370** SQLite identifier. Identifiers are alphanumerics, "_", "$", and any
21371** non-ASCII UTF character. Hence the test for whether or not a character is
21372** part of an identifier is 0x46.
21373*/
21374SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[256] = {
21375 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00..07 ........ */
21376 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, /* 08..0f ........ */
21377 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10..17 ........ */
21378 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 18..1f ........ */
21379 0x01, 0x00, 0x80, 0x00, 0x40, 0x00, 0x00, 0x80, /* 20..27 !"#$%&' */
21380 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 28..2f ()*+,-./ */
21381 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, /* 30..37 01234567 */
21382 0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 38..3f 89:;<=>? */
21383
21384 0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02, /* 40..47 @ABCDEFG */
21385 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 48..4f HIJKLMNO */
21386 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 50..57 PQRSTUVW */
21387 0x02, 0x02, 0x02, 0x80, 0x00, 0x00, 0x00, 0x40, /* 58..5f XYZ[\]^_ */
21388 0x80, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22, /* 60..67 `abcdefg */
21389 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* 68..6f hijklmno */
21390 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* 70..77 pqrstuvw */
21391 0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, /* 78..7f xyz{|}~. */
21392
21393 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 80..87 ........ */
21394 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 88..8f ........ */
21395 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 90..97 ........ */
21396 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 98..9f ........ */
21397 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* a0..a7 ........ */
21398 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* a8..af ........ */
21399 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* b0..b7 ........ */
21400 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* b8..bf ........ */
21401
21402 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* c0..c7 ........ */
21403 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* c8..cf ........ */
21404 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* d0..d7 ........ */
21405 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* d8..df ........ */
21406 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* e0..e7 ........ */
21407 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* e8..ef ........ */
21408 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* f0..f7 ........ */
21409 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40 /* f8..ff ........ */
21410};
21411
21412/* EVIDENCE-OF: R-02982-34736 In order to maintain full backwards
21413** compatibility for legacy applications, the URI filename capability is
21414** disabled by default.
21415**
21416** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled
21417** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options.
21418**
21419** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally
21420** disabled. The default value may be changed by compiling with the
21421** SQLITE_USE_URI symbol defined.
21422*/
21423#ifndef SQLITE_USE_URI
21424# define SQLITE_USE_URI 0
21425#endif
21426
21427/* EVIDENCE-OF: R-38720-18127 The default setting is determined by the
21428** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if
21429** that compile-time option is omitted.
21430*/
21431#if !defined(SQLITE_ALLOW_COVERING_INDEX_SCAN)
21432# define SQLITE_ALLOW_COVERING_INDEX_SCAN 1
21433#else
21434# if !SQLITE_ALLOW_COVERING_INDEX_SCAN
21435# error "Compile-time disabling of covering index scan using the\
21436 -DSQLITE_ALLOW_COVERING_INDEX_SCAN=0 option is deprecated.\
21437 Contact SQLite developers if this is a problem for you, and\
21438 delete this #error macro to continue with your build."
21439# endif
21440#endif
21441
21442/* The minimum PMA size is set to this value multiplied by the database
21443** page size in bytes.
21444*/
21445#ifndef SQLITE_SORTER_PMASZ
21446# define SQLITE_SORTER_PMASZ 250
21447#endif
21448
21449/* Statement journals spill to disk when their size exceeds the following
21450** threshold (in bytes). 0 means that statement journals are created and
21451** written to disk immediately (the default behavior for SQLite versions
21452** before 3.12.0). -1 means always keep the entire statement journal in
21453** memory. (The statement journal is also always held entirely in memory
21454** if journal_mode=MEMORY or if temp_store=MEMORY, regardless of this
21455** setting.)
21456*/
21457#ifndef SQLITE_STMTJRNL_SPILL
21458# define SQLITE_STMTJRNL_SPILL (64*1024)
21459#endif
21460
21461/*
21462** The default lookaside-configuration, the format "SZ,N". SZ is the
21463** number of bytes in each lookaside slot (should be a multiple of 8)
21464** and N is the number of slots. The lookaside-configuration can be
21465** changed as start-time using sqlite3_config(SQLITE_CONFIG_LOOKASIDE)
21466** or at run-time for an individual database connection using
21467** sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE);
21468**
21469** With the two-size-lookaside enhancement, less lookaside is required.
21470** The default configuration of 1200,40 actually provides 30 1200-byte slots
21471** and 93 128-byte slots, which is more lookaside than is available
21472** using the older 1200,100 configuration without two-size-lookaside.
21473*/
21474#ifndef SQLITE_DEFAULT_LOOKASIDE
21475# ifdef SQLITE_OMIT_TWOSIZE_LOOKASIDE
21476# define SQLITE_DEFAULT_LOOKASIDE 1200,100 /* 120KB of memory */
21477# else
21478# define SQLITE_DEFAULT_LOOKASIDE 1200,40 /* 48KB of memory */
21479# endif
21480#endif
21481
21482
21483/* The default maximum size of an in-memory database created using
21484** sqlite3_deserialize()
21485*/
21486#ifndef SQLITE_MEMDB_DEFAULT_MAXSIZE
21487# define SQLITE_MEMDB_DEFAULT_MAXSIZE 1073741824
21488#endif
21489
21490/*
21491** The following singleton contains the global configuration for
21492** the SQLite library.
21493*/
21494SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = {
21495 SQLITE_DEFAULT_MEMSTATUS, /* bMemstat */
21496 1, /* bCoreMutex */
21497 SQLITE_THREADSAFE==1, /* bFullMutex */
21498 SQLITE_USE_URI, /* bOpenUri */
21499 SQLITE_ALLOW_COVERING_INDEX_SCAN, /* bUseCis */
21500 0, /* bSmallMalloc */
21501 1, /* bExtraSchemaChecks */
21502 0x7ffffffe, /* mxStrlen */
21503 0, /* neverCorrupt */
21504 SQLITE_DEFAULT_LOOKASIDE, /* szLookaside, nLookaside */
21505 SQLITE_STMTJRNL_SPILL, /* nStmtSpill */
21506 {0,0,0,0,0,0,0,0}, /* m */
21507 {0,0,0,0,0,0,0,0,0}, /* mutex */
21508 {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
21509 (void*)0, /* pHeap */
21510 0, /* nHeap */
21511 0, 0, /* mnHeap, mxHeap */
21512 SQLITE_DEFAULT_MMAP_SIZE, /* szMmap */
21513 SQLITE_MAX_MMAP_SIZE, /* mxMmap */
21514 (void*)0, /* pPage */
21515 0, /* szPage */
21516 SQLITE_DEFAULT_PCACHE_INITSZ, /* nPage */
21517 0, /* mxParserStack */
21518 0, /* sharedCacheEnabled */
21519 SQLITE_SORTER_PMASZ, /* szPma */
21520 /* All the rest should always be initialized to zero */
21521 0, /* isInit */
21522 0, /* inProgress */
21523 0, /* isMutexInit */
21524 0, /* isMallocInit */
21525 0, /* isPCacheInit */
21526 0, /* nRefInitMutex */
21527 0, /* pInitMutex */
21528 0, /* xLog */
21529 0, /* pLogArg */
21530#ifdef SQLITE_ENABLE_SQLLOG
21531 0, /* xSqllog */
21532 0, /* pSqllogArg */
21533#endif
21534#ifdef SQLITE_VDBE_COVERAGE
21535 0, /* xVdbeBranch */
21536 0, /* pVbeBranchArg */
21537#endif
21538#ifndef SQLITE_OMIT_DESERIALIZE
21539 SQLITE_MEMDB_DEFAULT_MAXSIZE, /* mxMemdbSize */
21540#endif
21541#ifndef SQLITE_UNTESTABLE
21542 0, /* xTestCallback */
21543#endif
21544 0, /* bLocaltimeFault */
21545 0x7ffffffe, /* iOnceResetThreshold */
21546 SQLITE_DEFAULT_SORTERREF_SIZE, /* szSorterRef */
21547 0, /* iPrngSeed */
21548};
21549
21550/*
21551** Hash table for global functions - functions common to all
21552** database connections. After initialization, this table is
21553** read-only.
21554*/
21555SQLITE_PRIVATE FuncDefHash sqlite3BuiltinFunctions;
21556
21557#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_DEBUG)
21558/*
21559** Counter used for coverage testing. Does not come into play for
21560** release builds.
21561**
21562** Access to this global variable is not mutex protected. This might
21563** result in TSAN warnings. But as the variable does not exist in
21564** release builds, that should not be a concern.
21565*/
21566SQLITE_PRIVATE unsigned int sqlite3CoverageCounter;
21567#endif /* SQLITE_COVERAGE_TEST || SQLITE_DEBUG */
21568
21569#ifdef VDBE_PROFILE
21570/*
21571** The following performance counter can be used in place of
21572** sqlite3Hwtime() for profiling. This is a no-op on standard builds.
21573*/
21574SQLITE_PRIVATE sqlite3_uint64 sqlite3NProfileCnt = 0;
21575#endif
21576
21577/*
21578** The value of the "pending" byte must be 0x40000000 (1 byte past the
21579** 1-gibabyte boundary) in a compatible database. SQLite never uses
21580** the database page that contains the pending byte. It never attempts
21581** to read or write that page. The pending byte page is set aside
21582** for use by the VFS layers as space for managing file locks.
21583**
21584** During testing, it is often desirable to move the pending byte to
21585** a different position in the file. This allows code that has to
21586** deal with the pending byte to run on files that are much smaller
21587** than 1 GiB. The sqlite3_test_control() interface can be used to
21588** move the pending byte.
21589**
21590** IMPORTANT: Changing the pending byte to any value other than
21591** 0x40000000 results in an incompatible database file format!
21592** Changing the pending byte during operation will result in undefined
21593** and incorrect behavior.
21594*/
21595#ifndef SQLITE_OMIT_WSD
21596SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000;
21597#endif
21598
21599/*
21600** Tracing flags set by SQLITE_TESTCTRL_TRACEFLAGS.
21601*/
21602SQLITE_PRIVATE u32 sqlite3SelectTrace = 0;
21603SQLITE_PRIVATE u32 sqlite3WhereTrace = 0;
21604
21605/* #include "opcodes.h" */
21606/*
21607** Properties of opcodes. The OPFLG_INITIALIZER macro is
21608** created by mkopcodeh.awk during compilation. Data is obtained
21609** from the comments following the "case OP_xxxx:" statements in
21610** the vdbe.c file.
21611*/
21612SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[] = OPFLG_INITIALIZER;
21613
21614/*
21615** Name of the default collating sequence
21616*/
21617SQLITE_PRIVATE const char sqlite3StrBINARY[] = "BINARY";
21618
21619/*
21620** Standard typenames. These names must match the COLTYPE_* definitions.
21621** Adjust the SQLITE_N_STDTYPE value if adding or removing entries.
21622**
21623** sqlite3StdType[] The actual names of the datatypes.
21624**
21625** sqlite3StdTypeLen[] The length (in bytes) of each entry
21626** in sqlite3StdType[].
21627**
21628** sqlite3StdTypeAffinity[] The affinity associated with each entry
21629** in sqlite3StdType[].
21630**
21631** sqlite3StdTypeMap[] The type value (as returned from
21632** sqlite3_column_type() or sqlite3_value_type())
21633** for each entry in sqlite3StdType[].
21634*/
21635SQLITE_PRIVATE const unsigned char sqlite3StdTypeLen[] = { 3, 4, 3, 7, 4, 4 };
21636SQLITE_PRIVATE const char sqlite3StdTypeAffinity[] = {
21637 SQLITE_AFF_NUMERIC,
21638 SQLITE_AFF_BLOB,
21639 SQLITE_AFF_INTEGER,
21640 SQLITE_AFF_INTEGER,
21641 SQLITE_AFF_REAL,
21642 SQLITE_AFF_TEXT
21643};
21644SQLITE_PRIVATE const char sqlite3StdTypeMap[] = {
21645 0,
21646 SQLITE_BLOB,
21647 SQLITE_INTEGER,
21648 SQLITE_INTEGER,
21649 SQLITE_FLOAT,
21650 SQLITE_TEXT
21651};
21652SQLITE_PRIVATE const char *sqlite3StdType[] = {
21653 "ANY",
21654 "BLOB",
21655 "INT",
21656 "INTEGER",
21657 "REAL",
21658 "TEXT"
21659};
21660
21661/************** End of global.c **********************************************/
21662/************** Begin file status.c ******************************************/
21663/*
21664** 2008 June 18
21665**
21666** The author disclaims copyright to this source code. In place of
21667** a legal notice, here is a blessing:
21668**
21669** May you do good and not evil.
21670** May you find forgiveness for yourself and forgive others.
21671** May you share freely, never taking more than you give.
21672**
21673*************************************************************************
21674**
21675** This module implements the sqlite3_status() interface and related
21676** functionality.
21677*/
21678/* #include "sqliteInt.h" */
21679/************** Include vdbeInt.h in the middle of status.c ******************/
21680/************** Begin file vdbeInt.h *****************************************/
21681/*
21682** 2003 September 6
21683**
21684** The author disclaims copyright to this source code. In place of
21685** a legal notice, here is a blessing:
21686**
21687** May you do good and not evil.
21688** May you find forgiveness for yourself and forgive others.
21689** May you share freely, never taking more than you give.
21690**
21691*************************************************************************
21692** This is the header file for information that is private to the
21693** VDBE. This information used to all be at the top of the single
21694** source code file "vdbe.c". When that file became too big (over
21695** 6000 lines long) it was split up into several smaller files and
21696** this header information was factored out.
21697*/
21698#ifndef SQLITE_VDBEINT_H
21699#define SQLITE_VDBEINT_H
21700
21701/*
21702** The maximum number of times that a statement will try to reparse
21703** itself before giving up and returning SQLITE_SCHEMA.
21704*/
21705#ifndef SQLITE_MAX_SCHEMA_RETRY
21706# define SQLITE_MAX_SCHEMA_RETRY 50
21707#endif
21708
21709/*
21710** VDBE_DISPLAY_P4 is true or false depending on whether or not the
21711** "explain" P4 display logic is enabled.
21712*/
21713#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
21714 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) \
21715 || defined(SQLITE_ENABLE_BYTECODE_VTAB)
21716# define VDBE_DISPLAY_P4 1
21717#else
21718# define VDBE_DISPLAY_P4 0
21719#endif
21720
21721/*
21722** SQL is translated into a sequence of instructions to be
21723** executed by a virtual machine. Each instruction is an instance
21724** of the following structure.
21725*/
21726typedef struct VdbeOp Op;
21727
21728/*
21729** Boolean values
21730*/
21731typedef unsigned Bool;
21732
21733/* Opaque type used by code in vdbesort.c */
21734typedef struct VdbeSorter VdbeSorter;
21735
21736/* Elements of the linked list at Vdbe.pAuxData */
21737typedef struct AuxData AuxData;
21738
21739/* Types of VDBE cursors */
21740#define CURTYPE_BTREE 0
21741#define CURTYPE_SORTER 1
21742#define CURTYPE_VTAB 2
21743#define CURTYPE_PSEUDO 3
21744
21745/*
21746** A VdbeCursor is an superclass (a wrapper) for various cursor objects:
21747**
21748** * A b-tree cursor
21749** - In the main database or in an ephemeral database
21750** - On either an index or a table
21751** * A sorter
21752** * A virtual table
21753** * A one-row "pseudotable" stored in a single register
21754*/
21755typedef struct VdbeCursor VdbeCursor;
21756struct VdbeCursor {
21757 u8 eCurType; /* One of the CURTYPE_* values above */
21758 i8 iDb; /* Index of cursor database in db->aDb[] (or -1) */
21759 u8 nullRow; /* True if pointing to a row with no data */
21760 u8 deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */
21761 u8 isTable; /* True for rowid tables. False for indexes */
21762#ifdef SQLITE_DEBUG
21763 u8 seekOp; /* Most recent seek operation on this cursor */
21764 u8 wrFlag; /* The wrFlag argument to sqlite3BtreeCursor() */
21765#endif
21766 Bool isEphemeral:1; /* True for an ephemeral table */
21767 Bool useRandomRowid:1; /* Generate new record numbers semi-randomly */
21768 Bool isOrdered:1; /* True if the table is not BTREE_UNORDERED */
21769 Bool hasBeenDuped:1; /* This cursor was source or target of OP_OpenDup */
21770 u16 seekHit; /* See the OP_SeekHit and OP_IfNoHope opcodes */
21771 Btree *pBtx; /* Separate file holding temporary table */
21772 i64 seqCount; /* Sequence counter */
21773 u32 *aAltMap; /* Mapping from table to index column numbers */
21774
21775 /* Cached OP_Column parse information is only valid if cacheStatus matches
21776 ** Vdbe.cacheCtr. Vdbe.cacheCtr will never take on the value of
21777 ** CACHE_STALE (0) and so setting cacheStatus=CACHE_STALE guarantees that
21778 ** the cache is out of date. */
21779 u32 cacheStatus; /* Cache is valid if this matches Vdbe.cacheCtr */
21780 int seekResult; /* Result of previous sqlite3BtreeMoveto() or 0
21781 ** if there have been no prior seeks on the cursor. */
21782 /* seekResult does not distinguish between "no seeks have ever occurred
21783 ** on this cursor" and "the most recent seek was an exact match".
21784 ** For CURTYPE_PSEUDO, seekResult is the register holding the record */
21785
21786 /* When a new VdbeCursor is allocated, only the fields above are zeroed.
21787 ** The fields that follow are uninitialized, and must be individually
21788 ** initialized prior to first use. */
21789 VdbeCursor *pAltCursor; /* Associated index cursor from which to read */
21790 union {
21791 BtCursor *pCursor; /* CURTYPE_BTREE or _PSEUDO. Btree cursor */
21792 sqlite3_vtab_cursor *pVCur; /* CURTYPE_VTAB. Vtab cursor */
21793 VdbeSorter *pSorter; /* CURTYPE_SORTER. Sorter object */
21794 } uc;
21795 KeyInfo *pKeyInfo; /* Info about index keys needed by index cursors */
21796 u32 iHdrOffset; /* Offset to next unparsed byte of the header */
21797 Pgno pgnoRoot; /* Root page of the open btree cursor */
21798 i16 nField; /* Number of fields in the header */
21799 u16 nHdrParsed; /* Number of header fields parsed so far */
21800 i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */
21801 u32 *aOffset; /* Pointer to aType[nField] */
21802 const u8 *aRow; /* Data for the current row, if all on one page */
21803 u32 payloadSize; /* Total number of bytes in the record */
21804 u32 szRow; /* Byte available in aRow */
21805#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
21806 u64 maskUsed; /* Mask of columns used by this cursor */
21807#endif
21808
21809 /* 2*nField extra array elements allocated for aType[], beyond the one
21810 ** static element declared in the structure. nField total array slots for
21811 ** aType[] and nField+1 array slots for aOffset[] */
21812 u32 aType[1]; /* Type values record decode. MUST BE LAST */
21813};
21814
21815
21816/*
21817** A value for VdbeCursor.cacheStatus that means the cache is always invalid.
21818*/
21819#define CACHE_STALE 0
21820
21821/*
21822** When a sub-program is executed (OP_Program), a structure of this type
21823** is allocated to store the current value of the program counter, as
21824** well as the current memory cell array and various other frame specific
21825** values stored in the Vdbe struct. When the sub-program is finished,
21826** these values are copied back to the Vdbe from the VdbeFrame structure,
21827** restoring the state of the VM to as it was before the sub-program
21828** began executing.
21829**
21830** The memory for a VdbeFrame object is allocated and managed by a memory
21831** cell in the parent (calling) frame. When the memory cell is deleted or
21832** overwritten, the VdbeFrame object is not freed immediately. Instead, it
21833** is linked into the Vdbe.pDelFrame list. The contents of the Vdbe.pDelFrame
21834** list is deleted when the VM is reset in VdbeHalt(). The reason for doing
21835** this instead of deleting the VdbeFrame immediately is to avoid recursive
21836** calls to sqlite3VdbeMemRelease() when the memory cells belonging to the
21837** child frame are released.
21838**
21839** The currently executing frame is stored in Vdbe.pFrame. Vdbe.pFrame is
21840** set to NULL if the currently executing frame is the main program.
21841*/
21842typedef struct VdbeFrame VdbeFrame;
21843struct VdbeFrame {
21844 Vdbe *v; /* VM this frame belongs to */
21845 VdbeFrame *pParent; /* Parent of this frame, or NULL if parent is main */
21846 Op *aOp; /* Program instructions for parent frame */
21847 i64 *anExec; /* Event counters from parent frame */
21848 Mem *aMem; /* Array of memory cells for parent frame */
21849 VdbeCursor **apCsr; /* Array of Vdbe cursors for parent frame */
21850 u8 *aOnce; /* Bitmask used by OP_Once */
21851 void *token; /* Copy of SubProgram.token */
21852 i64 lastRowid; /* Last insert rowid (sqlite3.lastRowid) */
21853 AuxData *pAuxData; /* Linked list of auxdata allocations */
21854#if SQLITE_DEBUG
21855 u32 iFrameMagic; /* magic number for sanity checking */
21856#endif
21857 int nCursor; /* Number of entries in apCsr */
21858 int pc; /* Program Counter in parent (calling) frame */
21859 int nOp; /* Size of aOp array */
21860 int nMem; /* Number of entries in aMem */
21861 int nChildMem; /* Number of memory cells for child frame */
21862 int nChildCsr; /* Number of cursors for child frame */
21863 i64 nChange; /* Statement changes (Vdbe.nChange) */
21864 i64 nDbChange; /* Value of db->nChange */
21865};
21866
21867/* Magic number for sanity checking on VdbeFrame objects */
21868#define SQLITE_FRAME_MAGIC 0x879fb71e
21869
21870/*
21871** Return a pointer to the array of registers allocated for use
21872** by a VdbeFrame.
21873*/
21874#define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))])
21875
21876/*
21877** Internally, the vdbe manipulates nearly all SQL values as Mem
21878** structures. Each Mem struct may cache multiple representations (string,
21879** integer etc.) of the same value.
21880*/
21881struct sqlite3_value {
21882 union MemValue {
21883 double r; /* Real value used when MEM_Real is set in flags */
21884 i64 i; /* Integer value used when MEM_Int is set in flags */
21885 int nZero; /* Extra zero bytes when MEM_Zero and MEM_Blob set */
21886 const char *zPType; /* Pointer type when MEM_Term|MEM_Subtype|MEM_Null */
21887 FuncDef *pDef; /* Used only when flags==MEM_Agg */
21888 } u;
21889 u16 flags; /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
21890 u8 enc; /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
21891 u8 eSubtype; /* Subtype for this value */
21892 int n; /* Number of characters in string value, excluding '\0' */
21893 char *z; /* String or BLOB value */
21894 /* ShallowCopy only needs to copy the information above */
21895 char *zMalloc; /* Space to hold MEM_Str or MEM_Blob if szMalloc>0 */
21896 int szMalloc; /* Size of the zMalloc allocation */
21897 u32 uTemp; /* Transient storage for serial_type in OP_MakeRecord */
21898 sqlite3 *db; /* The associated database connection */
21899 void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */
21900#ifdef SQLITE_DEBUG
21901 Mem *pScopyFrom; /* This Mem is a shallow copy of pScopyFrom */
21902 u16 mScopyFlags; /* flags value immediately after the shallow copy */
21903#endif
21904};
21905
21906/*
21907** Size of struct Mem not including the Mem.zMalloc member or anything that
21908** follows.
21909*/
21910#define MEMCELLSIZE offsetof(Mem,zMalloc)
21911
21912/* One or more of the following flags are set to indicate the validOK
21913** representations of the value stored in the Mem struct.
21914**
21915** If the MEM_Null flag is set, then the value is an SQL NULL value.
21916** For a pointer type created using sqlite3_bind_pointer() or
21917** sqlite3_result_pointer() the MEM_Term and MEM_Subtype flags are also set.
21918**
21919** If the MEM_Str flag is set then Mem.z points at a string representation.
21920** Usually this is encoded in the same unicode encoding as the main
21921** database (see below for exceptions). If the MEM_Term flag is also
21922** set, then the string is nul terminated. The MEM_Int and MEM_Real
21923** flags may coexist with the MEM_Str flag.
21924*/
21925#define MEM_Null 0x0001 /* Value is NULL (or a pointer) */
21926#define MEM_Str 0x0002 /* Value is a string */
21927#define MEM_Int 0x0004 /* Value is an integer */
21928#define MEM_Real 0x0008 /* Value is a real number */
21929#define MEM_Blob 0x0010 /* Value is a BLOB */
21930#define MEM_IntReal 0x0020 /* MEM_Int that stringifies like MEM_Real */
21931#define MEM_AffMask 0x003f /* Mask of affinity bits */
21932#define MEM_FromBind 0x0040 /* Value originates from sqlite3_bind() */
21933#define MEM_Undefined 0x0080 /* Value is undefined */
21934#define MEM_Cleared 0x0100 /* NULL set by OP_Null, not from data */
21935#define MEM_TypeMask 0xc1bf /* Mask of type bits */
21936
21937
21938/* Whenever Mem contains a valid string or blob representation, one of
21939** the following flags must be set to determine the memory management
21940** policy for Mem.z. The MEM_Term flag tells us whether or not the
21941** string is \000 or \u0000 terminated
21942*/
21943#define MEM_Term 0x0200 /* String in Mem.z is zero terminated */
21944#define MEM_Dyn 0x0400 /* Need to call Mem.xDel() on Mem.z */
21945#define MEM_Static 0x0800 /* Mem.z points to a static string */
21946#define MEM_Ephem 0x1000 /* Mem.z points to an ephemeral string */
21947#define MEM_Agg 0x2000 /* Mem.z points to an agg function context */
21948#define MEM_Zero 0x4000 /* Mem.i contains count of 0s appended to blob */
21949#define MEM_Subtype 0x8000 /* Mem.eSubtype is valid */
21950#ifdef SQLITE_OMIT_INCRBLOB
21951 #undef MEM_Zero
21952 #define MEM_Zero 0x0000
21953#endif
21954
21955/* Return TRUE if Mem X contains dynamically allocated content - anything
21956** that needs to be deallocated to avoid a leak.
21957*/
21958#define VdbeMemDynamic(X) \
21959 (((X)->flags&(MEM_Agg|MEM_Dyn))!=0)
21960
21961/*
21962** Clear any existing type flags from a Mem and replace them with f
21963*/
21964#define MemSetTypeFlag(p, f) \
21965 ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
21966
21967/*
21968** True if Mem X is a NULL-nochng type.
21969*/
21970#define MemNullNochng(X) \
21971 (((X)->flags&MEM_TypeMask)==(MEM_Null|MEM_Zero) \
21972 && (X)->n==0 && (X)->u.nZero==0)
21973
21974/*
21975** Return true if a memory cell is not marked as invalid. This macro
21976** is for use inside assert() statements only.
21977*/
21978#ifdef SQLITE_DEBUG
21979#define memIsValid(M) ((M)->flags & MEM_Undefined)==0
21980#endif
21981
21982/*
21983** Each auxiliary data pointer stored by a user defined function
21984** implementation calling sqlite3_set_auxdata() is stored in an instance
21985** of this structure. All such structures associated with a single VM
21986** are stored in a linked list headed at Vdbe.pAuxData. All are destroyed
21987** when the VM is halted (if not before).
21988*/
21989struct AuxData {
21990 int iAuxOp; /* Instruction number of OP_Function opcode */
21991 int iAuxArg; /* Index of function argument. */
21992 void *pAux; /* Aux data pointer */
21993 void (*xDeleteAux)(void*); /* Destructor for the aux data */
21994 AuxData *pNextAux; /* Next element in list */
21995};
21996
21997/*
21998** The "context" argument for an installable function. A pointer to an
21999** instance of this structure is the first argument to the routines used
22000** implement the SQL functions.
22001**
22002** There is a typedef for this structure in sqlite.h. So all routines,
22003** even the public interface to SQLite, can use a pointer to this structure.
22004** But this file is the only place where the internal details of this
22005** structure are known.
22006**
22007** This structure is defined inside of vdbeInt.h because it uses substructures
22008** (Mem) which are only defined there.
22009*/
22010struct sqlite3_context {
22011 Mem *pOut; /* The return value is stored here */
22012 FuncDef *pFunc; /* Pointer to function information */
22013 Mem *pMem; /* Memory cell used to store aggregate context */
22014 Vdbe *pVdbe; /* The VM that owns this context */
22015 int iOp; /* Instruction number of OP_Function */
22016 int isError; /* Error code returned by the function. */
22017 u8 skipFlag; /* Skip accumulator loading if true */
22018 u8 argc; /* Number of arguments */
22019 sqlite3_value *argv[1]; /* Argument set */
22020};
22021
22022/* A bitfield type for use inside of structures. Always follow with :N where
22023** N is the number of bits.
22024*/
22025typedef unsigned bft; /* Bit Field Type */
22026
22027/* The ScanStatus object holds a single value for the
22028** sqlite3_stmt_scanstatus() interface.
22029*/
22030typedef struct ScanStatus ScanStatus;
22031struct ScanStatus {
22032 int addrExplain; /* OP_Explain for loop */
22033 int addrLoop; /* Address of "loops" counter */
22034 int addrVisit; /* Address of "rows visited" counter */
22035 int iSelectID; /* The "Select-ID" for this loop */
22036 LogEst nEst; /* Estimated output rows per loop */
22037 char *zName; /* Name of table or index */
22038};
22039
22040/* The DblquoteStr object holds the text of a double-quoted
22041** string for a prepared statement. A linked list of these objects
22042** is constructed during statement parsing and is held on Vdbe.pDblStr.
22043** When computing a normalized SQL statement for an SQL statement, that
22044** list is consulted for each double-quoted identifier to see if the
22045** identifier should really be a string literal.
22046*/
22047typedef struct DblquoteStr DblquoteStr;
22048struct DblquoteStr {
22049 DblquoteStr *pNextStr; /* Next string literal in the list */
22050 char z[8]; /* Dequoted value for the string */
22051};
22052
22053/*
22054** An instance of the virtual machine. This structure contains the complete
22055** state of the virtual machine.
22056**
22057** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
22058** is really a pointer to an instance of this structure.
22059*/
22060struct Vdbe {
22061 sqlite3 *db; /* The database connection that owns this statement */
22062 Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */
22063 Parse *pParse; /* Parsing context used to create this Vdbe */
22064 ynVar nVar; /* Number of entries in aVar[] */
22065 u32 iVdbeMagic; /* Magic number defining state of the SQL statement */
22066 int nMem; /* Number of memory locations currently allocated */
22067 int nCursor; /* Number of slots in apCsr[] */
22068 u32 cacheCtr; /* VdbeCursor row cache generation counter */
22069 int pc; /* The program counter */
22070 int rc; /* Value to return */
22071 i64 nChange; /* Number of db changes made since last reset */
22072 int iStatement; /* Statement number (or 0 if has no opened stmt) */
22073 i64 iCurrentTime; /* Value of julianday('now') for this statement */
22074 i64 nFkConstraint; /* Number of imm. FK constraints this VM */
22075 i64 nStmtDefCons; /* Number of def. constraints when stmt started */
22076 i64 nStmtDefImmCons; /* Number of def. imm constraints when stmt started */
22077 Mem *aMem; /* The memory locations */
22078 Mem **apArg; /* Arguments to currently executing user function */
22079 VdbeCursor **apCsr; /* One element of this array for each open cursor */
22080 Mem *aVar; /* Values for the OP_Variable opcode. */
22081
22082 /* When allocating a new Vdbe object, all of the fields below should be
22083 ** initialized to zero or NULL */
22084
22085 Op *aOp; /* Space to hold the virtual machine's program */
22086 int nOp; /* Number of instructions in the program */
22087 int nOpAlloc; /* Slots allocated for aOp[] */
22088 Mem *aColName; /* Column names to return */
22089 Mem *pResultSet; /* Pointer to an array of results */
22090 char *zErrMsg; /* Error message written here */
22091 VList *pVList; /* Name of variables */
22092#ifndef SQLITE_OMIT_TRACE
22093 i64 startTime; /* Time when query started - used for profiling */
22094#endif
22095#ifdef SQLITE_DEBUG
22096 int rcApp; /* errcode set by sqlite3_result_error_code() */
22097 u32 nWrite; /* Number of write operations that have occurred */
22098#endif
22099 u16 nResColumn; /* Number of columns in one row of the result set */
22100 u8 errorAction; /* Recovery action to do in case of an error */
22101 u8 minWriteFileFormat; /* Minimum file format for writable database files */
22102 u8 prepFlags; /* SQLITE_PREPARE_* flags */
22103 u8 doingRerun; /* True if rerunning after an auto-reprepare */
22104 bft expired:2; /* 1: recompile VM immediately 2: when convenient */
22105 bft explain:2; /* True if EXPLAIN present on SQL command */
22106 bft changeCntOn:1; /* True to update the change-counter */
22107 bft runOnlyOnce:1; /* Automatically expire on reset */
22108 bft usesStmtJournal:1; /* True if uses a statement journal */
22109 bft readOnly:1; /* True for statements that do not write */
22110 bft bIsReader:1; /* True for statements that read */
22111 yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */
22112 yDbMask lockMask; /* Subset of btreeMask that requires a lock */
22113 u32 aCounter[7]; /* Counters used by sqlite3_stmt_status() */
22114 char *zSql; /* Text of the SQL statement that generated this */
22115#ifdef SQLITE_ENABLE_NORMALIZE
22116 char *zNormSql; /* Normalization of the associated SQL statement */
22117 DblquoteStr *pDblStr; /* List of double-quoted string literals */
22118#endif
22119 void *pFree; /* Free this when deleting the vdbe */
22120 VdbeFrame *pFrame; /* Parent frame */
22121 VdbeFrame *pDelFrame; /* List of frame objects to free on VM reset */
22122 int nFrame; /* Number of frames in pFrame list */
22123 u32 expmask; /* Binding to these vars invalidates VM */
22124 SubProgram *pProgram; /* Linked list of all sub-programs used by VM */
22125 AuxData *pAuxData; /* Linked list of auxdata allocations */
22126#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
22127 i64 *anExec; /* Number of times each op has been executed */
22128 int nScan; /* Entries in aScan[] */
22129 ScanStatus *aScan; /* Scan definitions for sqlite3_stmt_scanstatus() */
22130#endif
22131};
22132
22133/*
22134** The following are allowed values for Vdbe.magic
22135*/
22136#define VDBE_MAGIC_INIT 0x16bceaa5 /* Building a VDBE program */
22137#define VDBE_MAGIC_RUN 0x2df20da3 /* VDBE is ready to execute */
22138#define VDBE_MAGIC_HALT 0x319c2973 /* VDBE has completed execution */
22139#define VDBE_MAGIC_RESET 0x48fa9f76 /* Reset and ready to run again */
22140#define VDBE_MAGIC_DEAD 0x5606c3c8 /* The VDBE has been deallocated */
22141
22142/*
22143** Structure used to store the context required by the
22144** sqlite3_preupdate_*() API functions.
22145*/
22146struct PreUpdate {
22147 Vdbe *v;
22148 VdbeCursor *pCsr; /* Cursor to read old values from */
22149 int op; /* One of SQLITE_INSERT, UPDATE, DELETE */
22150 u8 *aRecord; /* old.* database record */
22151 KeyInfo keyinfo;
22152 UnpackedRecord *pUnpacked; /* Unpacked version of aRecord[] */
22153 UnpackedRecord *pNewUnpacked; /* Unpacked version of new.* record */
22154 int iNewReg; /* Register for new.* values */
22155 int iBlobWrite; /* Value returned by preupdate_blobwrite() */
22156 i64 iKey1; /* First key value passed to hook */
22157 i64 iKey2; /* Second key value passed to hook */
22158 Mem *aNew; /* Array of new.* values */
22159 Table *pTab; /* Schema object being upated */
22160 Index *pPk; /* PK index if pTab is WITHOUT ROWID */
22161};
22162
22163/*
22164** Function prototypes
22165*/
22166SQLITE_PRIVATE void sqlite3VdbeError(Vdbe*, const char *, ...);
22167SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
22168void sqliteVdbePopStack(Vdbe*,int);
22169SQLITE_PRIVATE int SQLITE_NOINLINE sqlite3VdbeFinishMoveto(VdbeCursor*);
22170SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor**, u32*);
22171SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor*);
22172SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32);
22173SQLITE_PRIVATE u8 sqlite3VdbeOneByteSerialTypeLen(u8);
22174SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32);
22175SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
22176SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(sqlite3*, AuxData**, int, int);
22177
22178int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
22179SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(sqlite3*,VdbeCursor*,UnpackedRecord*,int*);
22180SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor*, i64*);
22181SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
22182#if !defined(SQLITE_OMIT_EXPLAIN) || defined(SQLITE_ENABLE_BYTECODE_VTAB)
22183SQLITE_PRIVATE int sqlite3VdbeNextOpcode(Vdbe*,Mem*,int,int*,int*,Op**);
22184SQLITE_PRIVATE char *sqlite3VdbeDisplayP4(sqlite3*,Op*);
22185#endif
22186#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
22187SQLITE_PRIVATE char *sqlite3VdbeDisplayComment(sqlite3*,const Op*,const char*);
22188#endif
22189#if !defined(SQLITE_OMIT_EXPLAIN)
22190SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
22191#endif
22192SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
22193SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int);
22194SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*);
22195SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem*, const Mem*);
22196SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
22197SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem*, Mem*);
22198SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem*);
22199SQLITE_PRIVATE int sqlite3VdbeMemSetStr(Mem*, const char*, i64, u8, void(*)(void*));
22200SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem*, i64);
22201#ifdef SQLITE_OMIT_FLOATING_POINT
22202# define sqlite3VdbeMemSetDouble sqlite3VdbeMemSetInt64
22203#else
22204SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem*, double);
22205#endif
22206SQLITE_PRIVATE void sqlite3VdbeMemSetPointer(Mem*, void*, const char*, void(*)(void*));
22207SQLITE_PRIVATE void sqlite3VdbeMemInit(Mem*,sqlite3*,u16);
22208SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem*);
22209#ifndef SQLITE_OMIT_INCRBLOB
22210SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem*,int);
22211#else
22212SQLITE_PRIVATE int sqlite3VdbeMemSetZeroBlob(Mem*,int);
22213#endif
22214#ifdef SQLITE_DEBUG
22215SQLITE_PRIVATE int sqlite3VdbeMemIsRowSet(const Mem*);
22216#endif
22217SQLITE_PRIVATE int sqlite3VdbeMemSetRowSet(Mem*);
22218SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*);
22219SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, u8, u8);
22220SQLITE_PRIVATE int sqlite3IntFloatCompare(i64,double);
22221SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem*);
22222SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*);
22223SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*);
22224SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem*, int ifNull);
22225SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*);
22226SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
22227SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
22228SQLITE_PRIVATE int sqlite3VdbeMemCast(Mem*,u8,u8);
22229SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,Mem*);
22230SQLITE_PRIVATE int sqlite3VdbeMemFromBtreeZeroOffset(BtCursor*,u32,Mem*);
22231SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
22232SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
22233#ifndef SQLITE_OMIT_WINDOWFUNC
22234SQLITE_PRIVATE int sqlite3VdbeMemAggValue(Mem*, Mem*, FuncDef*);
22235#endif
22236#if !defined(SQLITE_OMIT_EXPLAIN) || defined(SQLITE_ENABLE_BYTECODE_VTAB)
22237SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
22238#endif
22239SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
22240SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int n);
22241SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
22242#ifdef SQLITE_DEBUG
22243SQLITE_PRIVATE int sqlite3VdbeFrameIsValid(VdbeFrame*);
22244#endif
22245SQLITE_PRIVATE void sqlite3VdbeFrameMemDel(void*); /* Destructor on Mem */
22246SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*); /* Actually deletes the Frame */
22247SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
22248#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
22249SQLITE_PRIVATE void sqlite3VdbePreUpdateHook(
22250 Vdbe*,VdbeCursor*,int,const char*,Table*,i64,int,int);
22251#endif
22252SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p);
22253
22254SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, int, VdbeCursor *);
22255SQLITE_PRIVATE void sqlite3VdbeSorterReset(sqlite3 *, VdbeSorter *);
22256SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
22257SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
22258SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *);
22259SQLITE_PRIVATE int sqlite3VdbeSorterRewind(const VdbeCursor *, int *);
22260SQLITE_PRIVATE int sqlite3VdbeSorterWrite(const VdbeCursor *, Mem *);
22261SQLITE_PRIVATE int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int, int *);
22262
22263#ifdef SQLITE_DEBUG
22264SQLITE_PRIVATE void sqlite3VdbeIncrWriteCounter(Vdbe*, VdbeCursor*);
22265SQLITE_PRIVATE void sqlite3VdbeAssertAbortable(Vdbe*);
22266#else
22267# define sqlite3VdbeIncrWriteCounter(V,C)
22268# define sqlite3VdbeAssertAbortable(V)
22269#endif
22270
22271#if !defined(SQLITE_OMIT_SHARED_CACHE)
22272SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe*);
22273#else
22274# define sqlite3VdbeEnter(X)
22275#endif
22276
22277#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
22278SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe*);
22279#else
22280# define sqlite3VdbeLeave(X)
22281#endif
22282
22283#ifdef SQLITE_DEBUG
22284SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*);
22285SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem*);
22286#endif
22287
22288#ifndef SQLITE_OMIT_FOREIGN_KEY
22289SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
22290#else
22291# define sqlite3VdbeCheckFk(p,i) 0
22292#endif
22293
22294#ifdef SQLITE_DEBUG
22295SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe*);
22296SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, StrAccum *pStr);
22297#endif
22298#ifndef SQLITE_OMIT_UTF16
22299SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem*, u8);
22300SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem);
22301#endif
22302
22303#ifndef SQLITE_OMIT_INCRBLOB
22304SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *);
22305 #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
22306#else
22307 #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK
22308 #define ExpandBlob(P) SQLITE_OK
22309#endif
22310
22311#endif /* !defined(SQLITE_VDBEINT_H) */
22312
22313/************** End of vdbeInt.h *********************************************/
22314/************** Continuing where we left off in status.c *********************/
22315
22316/*
22317** Variables in which to record status information.
22318*/
22319#if SQLITE_PTRSIZE>4
22320typedef sqlite3_int64 sqlite3StatValueType;
22321#else
22322typedef u32 sqlite3StatValueType;
22323#endif
22324typedef struct sqlite3StatType sqlite3StatType;
22325static SQLITE_WSD struct sqlite3StatType {
22326 sqlite3StatValueType nowValue[10]; /* Current value */
22327 sqlite3StatValueType mxValue[10]; /* Maximum value */
22328} sqlite3Stat = { {0,}, {0,} };
22329
22330/*
22331** Elements of sqlite3Stat[] are protected by either the memory allocator
22332** mutex, or by the pcache1 mutex. The following array determines which.
22333*/
22334static const char statMutex[] = {
22335 0, /* SQLITE_STATUS_MEMORY_USED */
22336 1, /* SQLITE_STATUS_PAGECACHE_USED */
22337 1, /* SQLITE_STATUS_PAGECACHE_OVERFLOW */
22338 0, /* SQLITE_STATUS_SCRATCH_USED */
22339 0, /* SQLITE_STATUS_SCRATCH_OVERFLOW */
22340 0, /* SQLITE_STATUS_MALLOC_SIZE */
22341 0, /* SQLITE_STATUS_PARSER_STACK */
22342 1, /* SQLITE_STATUS_PAGECACHE_SIZE */
22343 0, /* SQLITE_STATUS_SCRATCH_SIZE */
22344 0, /* SQLITE_STATUS_MALLOC_COUNT */
22345};
22346
22347
22348/* The "wsdStat" macro will resolve to the status information
22349** state vector. If writable static data is unsupported on the target,
22350** we have to locate the state vector at run-time. In the more common
22351** case where writable static data is supported, wsdStat can refer directly
22352** to the "sqlite3Stat" state vector declared above.
22353*/
22354#ifdef SQLITE_OMIT_WSD
22355# define wsdStatInit sqlite3StatType *x = &GLOBAL(sqlite3StatType,sqlite3Stat)
22356# define wsdStat x[0]
22357#else
22358# define wsdStatInit
22359# define wsdStat sqlite3Stat
22360#endif
22361
22362/*
22363** Return the current value of a status parameter. The caller must
22364** be holding the appropriate mutex.
22365*/
22366SQLITE_PRIVATE sqlite3_int64 sqlite3StatusValue(int op){
22367 wsdStatInit;
22368 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
22369 assert( op>=0 && op<ArraySize(statMutex) );
22370 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
22371 : sqlite3MallocMutex()) );
22372 return wsdStat.nowValue[op];
22373}
22374
22375/*
22376** Add N to the value of a status record. The caller must hold the
22377** appropriate mutex. (Locking is checked by assert()).
22378**
22379** The StatusUp() routine can accept positive or negative values for N.
22380** The value of N is added to the current status value and the high-water
22381** mark is adjusted if necessary.
22382**
22383** The StatusDown() routine lowers the current value by N. The highwater
22384** mark is unchanged. N must be non-negative for StatusDown().
22385*/
22386SQLITE_PRIVATE void sqlite3StatusUp(int op, int N){
22387 wsdStatInit;
22388 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
22389 assert( op>=0 && op<ArraySize(statMutex) );
22390 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
22391 : sqlite3MallocMutex()) );
22392 wsdStat.nowValue[op] += N;
22393 if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
22394 wsdStat.mxValue[op] = wsdStat.nowValue[op];
22395 }
22396}
22397SQLITE_PRIVATE void sqlite3StatusDown(int op, int N){
22398 wsdStatInit;
22399 assert( N>=0 );
22400 assert( op>=0 && op<ArraySize(statMutex) );
22401 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
22402 : sqlite3MallocMutex()) );
22403 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
22404 wsdStat.nowValue[op] -= N;
22405}
22406
22407/*
22408** Adjust the highwater mark if necessary.
22409** The caller must hold the appropriate mutex.
22410*/
22411SQLITE_PRIVATE void sqlite3StatusHighwater(int op, int X){
22412 sqlite3StatValueType newValue;
22413 wsdStatInit;
22414 assert( X>=0 );
22415 newValue = (sqlite3StatValueType)X;
22416 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
22417 assert( op>=0 && op<ArraySize(statMutex) );
22418 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
22419 : sqlite3MallocMutex()) );
22420 assert( op==SQLITE_STATUS_MALLOC_SIZE
22421 || op==SQLITE_STATUS_PAGECACHE_SIZE
22422 || op==SQLITE_STATUS_PARSER_STACK );
22423 if( newValue>wsdStat.mxValue[op] ){
22424 wsdStat.mxValue[op] = newValue;
22425 }
22426}
22427
22428/*
22429** Query status information.
22430*/
22431SQLITE_API int sqlite3_status64(
22432 int op,
22433 sqlite3_int64 *pCurrent,
22434 sqlite3_int64 *pHighwater,
22435 int resetFlag
22436){
22437 sqlite3_mutex *pMutex;
22438 wsdStatInit;
22439 if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
22440 return SQLITE_MISUSE_BKPT;
22441 }
22442#ifdef SQLITE_ENABLE_API_ARMOR
22443 if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
22444#endif
22445 pMutex = statMutex[op] ? sqlite3Pcache1Mutex() : sqlite3MallocMutex();
22446 sqlite3_mutex_enter(pMutex);
22447 *pCurrent = wsdStat.nowValue[op];
22448 *pHighwater = wsdStat.mxValue[op];
22449 if( resetFlag ){
22450 wsdStat.mxValue[op] = wsdStat.nowValue[op];
22451 }
22452 sqlite3_mutex_leave(pMutex);
22453 (void)pMutex; /* Prevent warning when SQLITE_THREADSAFE=0 */
22454 return SQLITE_OK;
22455}
22456SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
22457 sqlite3_int64 iCur = 0, iHwtr = 0;
22458 int rc;
22459#ifdef SQLITE_ENABLE_API_ARMOR
22460 if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
22461#endif
22462 rc = sqlite3_status64(op, pCurrent: &iCur, pHighwater: &iHwtr, resetFlag);
22463 if( rc==0 ){
22464 *pCurrent = (int)iCur;
22465 *pHighwater = (int)iHwtr;
22466 }
22467 return rc;
22468}
22469
22470/*
22471** Return the number of LookasideSlot elements on the linked list
22472*/
22473static u32 countLookasideSlots(LookasideSlot *p){
22474 u32 cnt = 0;
22475 while( p ){
22476 p = p->pNext;
22477 cnt++;
22478 }
22479 return cnt;
22480}
22481
22482/*
22483** Count the number of slots of lookaside memory that are outstanding
22484*/
22485SQLITE_PRIVATE int sqlite3LookasideUsed(sqlite3 *db, int *pHighwater){
22486 u32 nInit = countLookasideSlots(p: db->lookaside.pInit);
22487 u32 nFree = countLookasideSlots(p: db->lookaside.pFree);
22488#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
22489 nInit += countLookasideSlots(p: db->lookaside.pSmallInit);
22490 nFree += countLookasideSlots(p: db->lookaside.pSmallFree);
22491#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
22492 if( pHighwater ) *pHighwater = db->lookaside.nSlot - nInit;
22493 return db->lookaside.nSlot - (nInit+nFree);
22494}
22495
22496/*
22497** Query status information for a single database connection
22498*/
22499SQLITE_API int sqlite3_db_status(
22500 sqlite3 *db, /* The database connection whose status is desired */
22501 int op, /* Status verb */
22502 int *pCurrent, /* Write current value here */
22503 int *pHighwater, /* Write high-water mark here */
22504 int resetFlag /* Reset high-water mark if true */
22505){
22506 int rc = SQLITE_OK; /* Return code */
22507#ifdef SQLITE_ENABLE_API_ARMOR
22508 if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwater==0 ){
22509 return SQLITE_MISUSE_BKPT;
22510 }
22511#endif
22512 sqlite3_mutex_enter(db->mutex);
22513 switch( op ){
22514 case SQLITE_DBSTATUS_LOOKASIDE_USED: {
22515 *pCurrent = sqlite3LookasideUsed(db, pHighwater);
22516 if( resetFlag ){
22517 LookasideSlot *p = db->lookaside.pFree;
22518 if( p ){
22519 while( p->pNext ) p = p->pNext;
22520 p->pNext = db->lookaside.pInit;
22521 db->lookaside.pInit = db->lookaside.pFree;
22522 db->lookaside.pFree = 0;
22523 }
22524#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
22525 p = db->lookaside.pSmallFree;
22526 if( p ){
22527 while( p->pNext ) p = p->pNext;
22528 p->pNext = db->lookaside.pSmallInit;
22529 db->lookaside.pSmallInit = db->lookaside.pSmallFree;
22530 db->lookaside.pSmallFree = 0;
22531 }
22532#endif
22533 }
22534 break;
22535 }
22536
22537 case SQLITE_DBSTATUS_LOOKASIDE_HIT:
22538 case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE:
22539 case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL: {
22540 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT );
22541 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE );
22542 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL );
22543 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 );
22544 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 );
22545 *pCurrent = 0;
22546 *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT];
22547 if( resetFlag ){
22548 db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0;
22549 }
22550 break;
22551 }
22552
22553 /*
22554 ** Return an approximation for the amount of memory currently used
22555 ** by all pagers associated with the given database connection. The
22556 ** highwater mark is meaningless and is returned as zero.
22557 */
22558 case SQLITE_DBSTATUS_CACHE_USED_SHARED:
22559 case SQLITE_DBSTATUS_CACHE_USED: {
22560 int totalUsed = 0;
22561 int i;
22562 sqlite3BtreeEnterAll(db);
22563 for(i=0; i<db->nDb; i++){
22564 Btree *pBt = db->aDb[i].pBt;
22565 if( pBt ){
22566 Pager *pPager = sqlite3BtreePager(pBt);
22567 int nByte = sqlite3PagerMemUsed(pPager);
22568 if( op==SQLITE_DBSTATUS_CACHE_USED_SHARED ){
22569 nByte = nByte / sqlite3BtreeConnectionCount(pBt);
22570 }
22571 totalUsed += nByte;
22572 }
22573 }
22574 sqlite3BtreeLeaveAll(db);
22575 *pCurrent = totalUsed;
22576 *pHighwater = 0;
22577 break;
22578 }
22579
22580 /*
22581 ** *pCurrent gets an accurate estimate of the amount of memory used
22582 ** to store the schema for all databases (main, temp, and any ATTACHed
22583 ** databases. *pHighwater is set to zero.
22584 */
22585 case SQLITE_DBSTATUS_SCHEMA_USED: {
22586 int i; /* Used to iterate through schemas */
22587 int nByte = 0; /* Used to accumulate return value */
22588
22589 sqlite3BtreeEnterAll(db);
22590 db->pnBytesFreed = &nByte;
22591 for(i=0; i<db->nDb; i++){
22592 Schema *pSchema = db->aDb[i].pSchema;
22593 if( ALWAYS(pSchema!=0) ){
22594 HashElem *p;
22595
22596 nByte += sqlite3GlobalConfig.m.xRoundup(sizeof(HashElem)) * (
22597 pSchema->tblHash.count
22598 + pSchema->trigHash.count
22599 + pSchema->idxHash.count
22600 + pSchema->fkeyHash.count
22601 );
22602 nByte += sqlite3_msize(pSchema->tblHash.ht);
22603 nByte += sqlite3_msize(pSchema->trigHash.ht);
22604 nByte += sqlite3_msize(pSchema->idxHash.ht);
22605 nByte += sqlite3_msize(pSchema->fkeyHash.ht);
22606
22607 for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){
22608 sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p));
22609 }
22610 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
22611 sqlite3DeleteTable(db, (Table *)sqliteHashData(p));
22612 }
22613 }
22614 }
22615 db->pnBytesFreed = 0;
22616 sqlite3BtreeLeaveAll(db);
22617
22618 *pHighwater = 0;
22619 *pCurrent = nByte;
22620 break;
22621 }
22622
22623 /*
22624 ** *pCurrent gets an accurate estimate of the amount of memory used
22625 ** to store all prepared statements.
22626 ** *pHighwater is set to zero.
22627 */
22628 case SQLITE_DBSTATUS_STMT_USED: {
22629 struct Vdbe *pVdbe; /* Used to iterate through VMs */
22630 int nByte = 0; /* Used to accumulate return value */
22631
22632 db->pnBytesFreed = &nByte;
22633 for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
22634 sqlite3VdbeClearObject(db, pVdbe);
22635 sqlite3DbFree(db, pVdbe);
22636 }
22637 db->pnBytesFreed = 0;
22638
22639 *pHighwater = 0; /* IMP: R-64479-57858 */
22640 *pCurrent = nByte;
22641
22642 break;
22643 }
22644
22645 /*
22646 ** Set *pCurrent to the total cache hits or misses encountered by all
22647 ** pagers the database handle is connected to. *pHighwater is always set
22648 ** to zero.
22649 */
22650 case SQLITE_DBSTATUS_CACHE_SPILL:
22651 op = SQLITE_DBSTATUS_CACHE_WRITE+1;
22652 /* no break */ deliberate_fall_through
22653 case SQLITE_DBSTATUS_CACHE_HIT:
22654 case SQLITE_DBSTATUS_CACHE_MISS:
22655 case SQLITE_DBSTATUS_CACHE_WRITE:{
22656 int i;
22657 int nRet = 0;
22658 assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
22659 assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 );
22660
22661 for(i=0; i<db->nDb; i++){
22662 if( db->aDb[i].pBt ){
22663 Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
22664 sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
22665 }
22666 }
22667 *pHighwater = 0; /* IMP: R-42420-56072 */
22668 /* IMP: R-54100-20147 */
22669 /* IMP: R-29431-39229 */
22670 *pCurrent = nRet;
22671 break;
22672 }
22673
22674 /* Set *pCurrent to non-zero if there are unresolved deferred foreign
22675 ** key constraints. Set *pCurrent to zero if all foreign key constraints
22676 ** have been satisfied. The *pHighwater is always set to zero.
22677 */
22678 case SQLITE_DBSTATUS_DEFERRED_FKS: {
22679 *pHighwater = 0; /* IMP: R-11967-56545 */
22680 *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0;
22681 break;
22682 }
22683
22684 default: {
22685 rc = SQLITE_ERROR;
22686 }
22687 }
22688 sqlite3_mutex_leave(db->mutex);
22689 return rc;
22690}
22691
22692/************** End of status.c **********************************************/
22693/************** Begin file date.c ********************************************/
22694/*
22695** 2003 October 31
22696**
22697** The author disclaims copyright to this source code. In place of
22698** a legal notice, here is a blessing:
22699**
22700** May you do good and not evil.
22701** May you find forgiveness for yourself and forgive others.
22702** May you share freely, never taking more than you give.
22703**
22704*************************************************************************
22705** This file contains the C functions that implement date and time
22706** functions for SQLite.
22707**
22708** There is only one exported symbol in this file - the function
22709** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
22710** All other code has file scope.
22711**
22712** SQLite processes all times and dates as julian day numbers. The
22713** dates and times are stored as the number of days since noon
22714** in Greenwich on November 24, 4714 B.C. according to the Gregorian
22715** calendar system.
22716**
22717** 1970-01-01 00:00:00 is JD 2440587.5
22718** 2000-01-01 00:00:00 is JD 2451544.5
22719**
22720** This implementation requires years to be expressed as a 4-digit number
22721** which means that only dates between 0000-01-01 and 9999-12-31 can
22722** be represented, even though julian day numbers allow a much wider
22723** range of dates.
22724**
22725** The Gregorian calendar system is used for all dates and times,
22726** even those that predate the Gregorian calendar. Historians usually
22727** use the julian calendar for dates prior to 1582-10-15 and for some
22728** dates afterwards, depending on locale. Beware of this difference.
22729**
22730** The conversion algorithms are implemented based on descriptions
22731** in the following text:
22732**
22733** Jean Meeus
22734** Astronomical Algorithms, 2nd Edition, 1998
22735** ISBN 0-943396-61-1
22736** Willmann-Bell, Inc
22737** Richmond, Virginia (USA)
22738*/
22739/* #include "sqliteInt.h" */
22740/* #include <stdlib.h> */
22741/* #include <assert.h> */
22742#include <time.h>
22743
22744#ifndef SQLITE_OMIT_DATETIME_FUNCS
22745
22746/*
22747** The MSVC CRT on Windows CE may not have a localtime() function.
22748** So declare a substitute. The substitute function itself is
22749** defined in "os_win.c".
22750*/
22751#if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \
22752 (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API)
22753struct tm *__cdecl localtime(const time_t *);
22754#endif
22755
22756/*
22757** A structure for holding a single date and time.
22758*/
22759typedef struct DateTime DateTime;
22760struct DateTime {
22761 sqlite3_int64 iJD; /* The julian day number times 86400000 */
22762 int Y, M, D; /* Year, month, and day */
22763 int h, m; /* Hour and minutes */
22764 int tz; /* Timezone offset in minutes */
22765 double s; /* Seconds */
22766 char validJD; /* True (1) if iJD is valid */
22767 char rawS; /* Raw numeric value stored in s */
22768 char validYMD; /* True (1) if Y,M,D are valid */
22769 char validHMS; /* True (1) if h,m,s are valid */
22770 char validTZ; /* True (1) if tz is valid */
22771 char tzSet; /* Timezone was set explicitly */
22772 char isError; /* An overflow has occurred */
22773};
22774
22775
22776/*
22777** Convert zDate into one or more integers according to the conversion
22778** specifier zFormat.
22779**
22780** zFormat[] contains 4 characters for each integer converted, except for
22781** the last integer which is specified by three characters. The meaning
22782** of a four-character format specifiers ABCD is:
22783**
22784** A: number of digits to convert. Always "2" or "4".
22785** B: minimum value. Always "0" or "1".
22786** C: maximum value, decoded as:
22787** a: 12
22788** b: 14
22789** c: 24
22790** d: 31
22791** e: 59
22792** f: 9999
22793** D: the separator character, or \000 to indicate this is the
22794** last number to convert.
22795**
22796** Example: To translate an ISO-8601 date YYYY-MM-DD, the format would
22797** be "40f-21a-20c". The "40f-" indicates the 4-digit year followed by "-".
22798** The "21a-" indicates the 2-digit month followed by "-". The "20c" indicates
22799** the 2-digit day which is the last integer in the set.
22800**
22801** The function returns the number of successful conversions.
22802*/
22803static int getDigits(const char *zDate, const char *zFormat, ...){
22804 /* The aMx[] array translates the 3rd character of each format
22805 ** spec into a max size: a b c d e f */
22806 static const u16 aMx[] = { 12, 14, 24, 31, 59, 9999 };
22807 va_list ap;
22808 int cnt = 0;
22809 char nextC;
22810 va_start(ap, zFormat);
22811 do{
22812 char N = zFormat[0] - '0';
22813 char min = zFormat[1] - '0';
22814 int val = 0;
22815 u16 max;
22816
22817 assert( zFormat[2]>='a' && zFormat[2]<='f' );
22818 max = aMx[zFormat[2] - 'a'];
22819 nextC = zFormat[3];
22820 val = 0;
22821 while( N-- ){
22822 if( !sqlite3Isdigit(*zDate) ){
22823 goto end_getDigits;
22824 }
22825 val = val*10 + *zDate - '0';
22826 zDate++;
22827 }
22828 if( val<(int)min || val>(int)max || (nextC!=0 && nextC!=*zDate) ){
22829 goto end_getDigits;
22830 }
22831 *va_arg(ap,int*) = val;
22832 zDate++;
22833 cnt++;
22834 zFormat += 4;
22835 }while( nextC );
22836end_getDigits:
22837 va_end(ap);
22838 return cnt;
22839}
22840
22841/*
22842** Parse a timezone extension on the end of a date-time.
22843** The extension is of the form:
22844**
22845** (+/-)HH:MM
22846**
22847** Or the "zulu" notation:
22848**
22849** Z
22850**
22851** If the parse is successful, write the number of minutes
22852** of change in p->tz and return 0. If a parser error occurs,
22853** return non-zero.
22854**
22855** A missing specifier is not considered an error.
22856*/
22857static int parseTimezone(const char *zDate, DateTime *p){
22858 int sgn = 0;
22859 int nHr, nMn;
22860 int c;
22861 while( sqlite3Isspace(*zDate) ){ zDate++; }
22862 p->tz = 0;
22863 c = *zDate;
22864 if( c=='-' ){
22865 sgn = -1;
22866 }else if( c=='+' ){
22867 sgn = +1;
22868 }else if( c=='Z' || c=='z' ){
22869 zDate++;
22870 goto zulu_time;
22871 }else{
22872 return c!=0;
22873 }
22874 zDate++;
22875 if( getDigits(zDate, zFormat: "20b:20e", &nHr, &nMn)!=2 ){
22876 return 1;
22877 }
22878 zDate += 5;
22879 p->tz = sgn*(nMn + nHr*60);
22880zulu_time:
22881 while( sqlite3Isspace(*zDate) ){ zDate++; }
22882 p->tzSet = 1;
22883 return *zDate!=0;
22884}
22885
22886/*
22887** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
22888** The HH, MM, and SS must each be exactly 2 digits. The
22889** fractional seconds FFFF can be one or more digits.
22890**
22891** Return 1 if there is a parsing error and 0 on success.
22892*/
22893static int parseHhMmSs(const char *zDate, DateTime *p){
22894 int h, m, s;
22895 double ms = 0.0;
22896 if( getDigits(zDate, zFormat: "20c:20e", &h, &m)!=2 ){
22897 return 1;
22898 }
22899 zDate += 5;
22900 if( *zDate==':' ){
22901 zDate++;
22902 if( getDigits(zDate, zFormat: "20e", &s)!=1 ){
22903 return 1;
22904 }
22905 zDate += 2;
22906 if( *zDate=='.' && sqlite3Isdigit(zDate[1]) ){
22907 double rScale = 1.0;
22908 zDate++;
22909 while( sqlite3Isdigit(*zDate) ){
22910 ms = ms*10.0 + *zDate - '0';
22911 rScale *= 10.0;
22912 zDate++;
22913 }
22914 ms /= rScale;
22915 }
22916 }else{
22917 s = 0;
22918 }
22919 p->validJD = 0;
22920 p->rawS = 0;
22921 p->validHMS = 1;
22922 p->h = h;
22923 p->m = m;
22924 p->s = s + ms;
22925 if( parseTimezone(zDate, p) ) return 1;
22926 p->validTZ = (p->tz!=0)?1:0;
22927 return 0;
22928}
22929
22930/*
22931** Put the DateTime object into its error state.
22932*/
22933static void datetimeError(DateTime *p){
22934 memset(s: p, c: 0, n: sizeof(*p));
22935 p->isError = 1;
22936}
22937
22938/*
22939** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume
22940** that the YYYY-MM-DD is according to the Gregorian calendar.
22941**
22942** Reference: Meeus page 61
22943*/
22944static void computeJD(DateTime *p){
22945 int Y, M, D, A, B, X1, X2;
22946
22947 if( p->validJD ) return;
22948 if( p->validYMD ){
22949 Y = p->Y;
22950 M = p->M;
22951 D = p->D;
22952 }else{
22953 Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */
22954 M = 1;
22955 D = 1;
22956 }
22957 if( Y<-4713 || Y>9999 || p->rawS ){
22958 datetimeError(p);
22959 return;
22960 }
22961 if( M<=2 ){
22962 Y--;
22963 M += 12;
22964 }
22965 A = Y/100;
22966 B = 2 - A + (A/4);
22967 X1 = 36525*(Y+4716)/100;
22968 X2 = 306001*(M+1)/10000;
22969 p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
22970 p->validJD = 1;
22971 if( p->validHMS ){
22972 p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000);
22973 if( p->validTZ ){
22974 p->iJD -= p->tz*60000;
22975 p->validYMD = 0;
22976 p->validHMS = 0;
22977 p->validTZ = 0;
22978 }
22979 }
22980}
22981
22982/*
22983** Parse dates of the form
22984**
22985** YYYY-MM-DD HH:MM:SS.FFF
22986** YYYY-MM-DD HH:MM:SS
22987** YYYY-MM-DD HH:MM
22988** YYYY-MM-DD
22989**
22990** Write the result into the DateTime structure and return 0
22991** on success and 1 if the input string is not a well-formed
22992** date.
22993*/
22994static int parseYyyyMmDd(const char *zDate, DateTime *p){
22995 int Y, M, D, neg;
22996
22997 if( zDate[0]=='-' ){
22998 zDate++;
22999 neg = 1;
23000 }else{
23001 neg = 0;
23002 }
23003 if( getDigits(zDate, zFormat: "40f-21a-21d", &Y, &M, &D)!=3 ){
23004 return 1;
23005 }
23006 zDate += 10;
23007 while( sqlite3Isspace(*zDate) || 'T'==*(u8*)zDate ){ zDate++; }
23008 if( parseHhMmSs(zDate, p)==0 ){
23009 /* We got the time */
23010 }else if( *zDate==0 ){
23011 p->validHMS = 0;
23012 }else{
23013 return 1;
23014 }
23015 p->validJD = 0;
23016 p->validYMD = 1;
23017 p->Y = neg ? -Y : Y;
23018 p->M = M;
23019 p->D = D;
23020 if( p->validTZ ){
23021 computeJD(p);
23022 }
23023 return 0;
23024}
23025
23026/*
23027** Set the time to the current time reported by the VFS.
23028**
23029** Return the number of errors.
23030*/
23031static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
23032 p->iJD = sqlite3StmtCurrentTime(context);
23033 if( p->iJD>0 ){
23034 p->validJD = 1;
23035 return 0;
23036 }else{
23037 return 1;
23038 }
23039}
23040
23041/*
23042** Input "r" is a numeric quantity which might be a julian day number,
23043** or the number of seconds since 1970. If the value if r is within
23044** range of a julian day number, install it as such and set validJD.
23045** If the value is a valid unix timestamp, put it in p->s and set p->rawS.
23046*/
23047static void setRawDateNumber(DateTime *p, double r){
23048 p->s = r;
23049 p->rawS = 1;
23050 if( r>=0.0 && r<5373484.5 ){
23051 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
23052 p->validJD = 1;
23053 }
23054}
23055
23056/*
23057** Attempt to parse the given string into a julian day number. Return
23058** the number of errors.
23059**
23060** The following are acceptable forms for the input string:
23061**
23062** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
23063** DDDD.DD
23064** now
23065**
23066** In the first form, the +/-HH:MM is always optional. The fractional
23067** seconds extension (the ".FFF") is optional. The seconds portion
23068** (":SS.FFF") is option. The year and date can be omitted as long
23069** as there is a time string. The time string can be omitted as long
23070** as there is a year and date.
23071*/
23072static int parseDateOrTime(
23073 sqlite3_context *context,
23074 const char *zDate,
23075 DateTime *p
23076){
23077 double r;
23078 if( parseYyyyMmDd(zDate,p)==0 ){
23079 return 0;
23080 }else if( parseHhMmSs(zDate, p)==0 ){
23081 return 0;
23082 }else if( sqlite3StrICmp(zDate,"now")==0 && sqlite3NotPureFunc(context) ){
23083 return setDateTimeToCurrent(context, p);
23084 }else if( sqlite3AtoF(z: zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8)>0 ){
23085 setRawDateNumber(p, r);
23086 return 0;
23087 }
23088 return 1;
23089}
23090
23091/* The julian day number for 9999-12-31 23:59:59.999 is 5373484.4999999.
23092** Multiplying this by 86400000 gives 464269060799999 as the maximum value
23093** for DateTime.iJD.
23094**
23095** But some older compilers (ex: gcc 4.2.1 on older Macs) cannot deal with
23096** such a large integer literal, so we have to encode it.
23097*/
23098#define INT_464269060799999 ((((i64)0x1a640)<<32)|0x1072fdff)
23099
23100/*
23101** Return TRUE if the given julian day number is within range.
23102**
23103** The input is the JulianDay times 86400000.
23104*/
23105static int validJulianDay(sqlite3_int64 iJD){
23106 return iJD>=0 && iJD<=INT_464269060799999;
23107}
23108
23109/*
23110** Compute the Year, Month, and Day from the julian day number.
23111*/
23112static void computeYMD(DateTime *p){
23113 int Z, A, B, C, D, E, X1;
23114 if( p->validYMD ) return;
23115 if( !p->validJD ){
23116 p->Y = 2000;
23117 p->M = 1;
23118 p->D = 1;
23119 }else if( !validJulianDay(iJD: p->iJD) ){
23120 datetimeError(p);
23121 return;
23122 }else{
23123 Z = (int)((p->iJD + 43200000)/86400000);
23124 A = (int)((Z - 1867216.25)/36524.25);
23125 A = Z + 1 + A - (A/4);
23126 B = A + 1524;
23127 C = (int)((B - 122.1)/365.25);
23128 D = (36525*(C&32767))/100;
23129 E = (int)((B-D)/30.6001);
23130 X1 = (int)(30.6001*E);
23131 p->D = B - D - X1;
23132 p->M = E<14 ? E-1 : E-13;
23133 p->Y = p->M>2 ? C - 4716 : C - 4715;
23134 }
23135 p->validYMD = 1;
23136}
23137
23138/*
23139** Compute the Hour, Minute, and Seconds from the julian day number.
23140*/
23141static void computeHMS(DateTime *p){
23142 int s;
23143 if( p->validHMS ) return;
23144 computeJD(p);
23145 s = (int)((p->iJD + 43200000) % 86400000);
23146 p->s = s/1000.0;
23147 s = (int)p->s;
23148 p->s -= s;
23149 p->h = s/3600;
23150 s -= p->h*3600;
23151 p->m = s/60;
23152 p->s += s - p->m*60;
23153 p->rawS = 0;
23154 p->validHMS = 1;
23155}
23156
23157/*
23158** Compute both YMD and HMS
23159*/
23160static void computeYMD_HMS(DateTime *p){
23161 computeYMD(p);
23162 computeHMS(p);
23163}
23164
23165/*
23166** Clear the YMD and HMS and the TZ
23167*/
23168static void clearYMD_HMS_TZ(DateTime *p){
23169 p->validYMD = 0;
23170 p->validHMS = 0;
23171 p->validTZ = 0;
23172}
23173
23174#ifndef SQLITE_OMIT_LOCALTIME
23175/*
23176** On recent Windows platforms, the localtime_s() function is available
23177** as part of the "Secure CRT". It is essentially equivalent to
23178** localtime_r() available under most POSIX platforms, except that the
23179** order of the parameters is reversed.
23180**
23181** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
23182**
23183** If the user has not indicated to use localtime_r() or localtime_s()
23184** already, check for an MSVC build environment that provides
23185** localtime_s().
23186*/
23187#if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S \
23188 && defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE)
23189#undef HAVE_LOCALTIME_S
23190#define HAVE_LOCALTIME_S 1
23191#endif
23192
23193/*
23194** The following routine implements the rough equivalent of localtime_r()
23195** using whatever operating-system specific localtime facility that
23196** is available. This routine returns 0 on success and
23197** non-zero on any kind of error.
23198**
23199** If the sqlite3GlobalConfig.bLocaltimeFault variable is true then this
23200** routine will always fail.
23201**
23202** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C
23203** library function localtime_r() is used to assist in the calculation of
23204** local time.
23205*/
23206static int osLocaltime(time_t *t, struct tm *pTm){
23207 int rc;
23208#if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S
23209 struct tm *pX;
23210#if SQLITE_THREADSAFE>0
23211 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);
23212#endif
23213 sqlite3_mutex_enter(mutex);
23214 pX = localtime(timer: t);
23215#ifndef SQLITE_UNTESTABLE
23216 if( sqlite3GlobalConfig.bLocaltimeFault ) pX = 0;
23217#endif
23218 if( pX ) *pTm = *pX;
23219 sqlite3_mutex_leave(mutex);
23220 rc = pX==0;
23221#else
23222#ifndef SQLITE_UNTESTABLE
23223 if( sqlite3GlobalConfig.bLocaltimeFault ) return 1;
23224#endif
23225#if HAVE_LOCALTIME_R
23226 rc = localtime_r(t, pTm)==0;
23227#else
23228 rc = localtime_s(pTm, t);
23229#endif /* HAVE_LOCALTIME_R */
23230#endif /* HAVE_LOCALTIME_R || HAVE_LOCALTIME_S */
23231 return rc;
23232}
23233#endif /* SQLITE_OMIT_LOCALTIME */
23234
23235
23236#ifndef SQLITE_OMIT_LOCALTIME
23237/*
23238** Compute the difference (in milliseconds) between localtime and UTC
23239** (a.k.a. GMT) for the time value p where p is in UTC. If no error occurs,
23240** return this value and set *pRc to SQLITE_OK.
23241**
23242** Or, if an error does occur, set *pRc to SQLITE_ERROR. The returned value
23243** is undefined in this case.
23244*/
23245static sqlite3_int64 localtimeOffset(
23246 DateTime *p, /* Date at which to calculate offset */
23247 sqlite3_context *pCtx, /* Write error here if one occurs */
23248 int *pRc /* OUT: Error code. SQLITE_OK or ERROR */
23249){
23250 DateTime x, y;
23251 time_t t;
23252 struct tm sLocal;
23253
23254 /* Initialize the contents of sLocal to avoid a compiler warning. */
23255 memset(s: &sLocal, c: 0, n: sizeof(sLocal));
23256
23257 x = *p;
23258 computeYMD_HMS(p: &x);
23259 if( x.Y<1971 || x.Y>=2038 ){
23260 /* EVIDENCE-OF: R-55269-29598 The localtime_r() C function normally only
23261 ** works for years between 1970 and 2037. For dates outside this range,
23262 ** SQLite attempts to map the year into an equivalent year within this
23263 ** range, do the calculation, then map the year back.
23264 */
23265 x.Y = 2000;
23266 x.M = 1;
23267 x.D = 1;
23268 x.h = 0;
23269 x.m = 0;
23270 x.s = 0.0;
23271 } else {
23272 int s = (int)(x.s + 0.5);
23273 x.s = s;
23274 }
23275 x.tz = 0;
23276 x.validJD = 0;
23277 computeJD(p: &x);
23278 t = (time_t)(x.iJD/1000 - 21086676*(i64)10000);
23279 if( osLocaltime(t: &t, pTm: &sLocal) ){
23280 sqlite3_result_error(pCtx, "local time unavailable", -1);
23281 *pRc = SQLITE_ERROR;
23282 return 0;
23283 }
23284 y.Y = sLocal.tm_year + 1900;
23285 y.M = sLocal.tm_mon + 1;
23286 y.D = sLocal.tm_mday;
23287 y.h = sLocal.tm_hour;
23288 y.m = sLocal.tm_min;
23289 y.s = sLocal.tm_sec;
23290 y.validYMD = 1;
23291 y.validHMS = 1;
23292 y.validJD = 0;
23293 y.rawS = 0;
23294 y.validTZ = 0;
23295 y.isError = 0;
23296 computeJD(p: &y);
23297 *pRc = SQLITE_OK;
23298 return y.iJD - x.iJD;
23299}
23300#endif /* SQLITE_OMIT_LOCALTIME */
23301
23302/*
23303** The following table defines various date transformations of the form
23304**
23305** 'NNN days'
23306**
23307** Where NNN is an arbitrary floating-point number and "days" can be one
23308** of several units of time.
23309*/
23310static const struct {
23311 u8 eType; /* Transformation type code */
23312 u8 nName; /* Length of th name */
23313 char *zName; /* Name of the transformation */
23314 double rLimit; /* Maximum NNN value for this transform */
23315 double rXform; /* Constant used for this transform */
23316} aXformType[] = {
23317 { 0, 6, "second", 464269060800.0, 1000.0 },
23318 { 0, 6, "minute", 7737817680.0, 60000.0 },
23319 { 0, 4, "hour", 128963628.0, 3600000.0 },
23320 { 0, 3, "day", 5373485.0, 86400000.0 },
23321 { 1, 5, "month", 176546.0, 2592000000.0 },
23322 { 2, 4, "year", 14713.0, 31536000000.0 },
23323};
23324
23325/*
23326** Process a modifier to a date-time stamp. The modifiers are
23327** as follows:
23328**
23329** NNN days
23330** NNN hours
23331** NNN minutes
23332** NNN.NNNN seconds
23333** NNN months
23334** NNN years
23335** start of month
23336** start of year
23337** start of week
23338** start of day
23339** weekday N
23340** unixepoch
23341** localtime
23342** utc
23343**
23344** Return 0 on success and 1 if there is any kind of error. If the error
23345** is in a system call (i.e. localtime()), then an error message is written
23346** to context pCtx. If the error is an unrecognized modifier, no error is
23347** written to pCtx.
23348*/
23349static int parseModifier(
23350 sqlite3_context *pCtx, /* Function context */
23351 const char *z, /* The text of the modifier */
23352 int n, /* Length of zMod in bytes */
23353 DateTime *p /* The date/time value to be modified */
23354){
23355 int rc = 1;
23356 double r;
23357 switch(sqlite3UpperToLower[(u8)z[0]] ){
23358#ifndef SQLITE_OMIT_LOCALTIME
23359 case 'l': {
23360 /* localtime
23361 **
23362 ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
23363 ** show local time.
23364 */
23365 if( sqlite3_stricmp(z, "localtime")==0 && sqlite3NotPureFunc(pCtx) ){
23366 computeJD(p);
23367 p->iJD += localtimeOffset(p, pCtx, pRc: &rc);
23368 clearYMD_HMS_TZ(p);
23369 }
23370 break;
23371 }
23372#endif
23373 case 'u': {
23374 /*
23375 ** unixepoch
23376 **
23377 ** Treat the current value of p->s as the number of
23378 ** seconds since 1970. Convert to a real julian day number.
23379 */
23380 if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){
23381 r = p->s*1000.0 + 210866760000000.0;
23382 if( r>=0.0 && r<464269060800000.0 ){
23383 clearYMD_HMS_TZ(p);
23384 p->iJD = (sqlite3_int64)(r + 0.5);
23385 p->validJD = 1;
23386 p->rawS = 0;
23387 rc = 0;
23388 }
23389 }
23390#ifndef SQLITE_OMIT_LOCALTIME
23391 else if( sqlite3_stricmp(z, "utc")==0 && sqlite3NotPureFunc(pCtx) ){
23392 if( p->tzSet==0 ){
23393 sqlite3_int64 c1;
23394 computeJD(p);
23395 c1 = localtimeOffset(p, pCtx, pRc: &rc);
23396 if( rc==SQLITE_OK ){
23397 p->iJD -= c1;
23398 clearYMD_HMS_TZ(p);
23399 p->iJD += c1 - localtimeOffset(p, pCtx, pRc: &rc);
23400 }
23401 p->tzSet = 1;
23402 }else{
23403 rc = SQLITE_OK;
23404 }
23405 }
23406#endif
23407 break;
23408 }
23409 case 'w': {
23410 /*
23411 ** weekday N
23412 **
23413 ** Move the date to the same time on the next occurrence of
23414 ** weekday N where 0==Sunday, 1==Monday, and so forth. If the
23415 ** date is already on the appropriate weekday, this is a no-op.
23416 */
23417 if( sqlite3_strnicmp(z, "weekday ", 8)==0
23418 && sqlite3AtoF(z: &z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8)>0
23419 && (n=(int)r)==r && n>=0 && r<7 ){
23420 sqlite3_int64 Z;
23421 computeYMD_HMS(p);
23422 p->validTZ = 0;
23423 p->validJD = 0;
23424 computeJD(p);
23425 Z = ((p->iJD + 129600000)/86400000) % 7;
23426 if( Z>n ) Z -= 7;
23427 p->iJD += (n - Z)*86400000;
23428 clearYMD_HMS_TZ(p);
23429 rc = 0;
23430 }
23431 break;
23432 }
23433 case 's': {
23434 /*
23435 ** start of TTTTT
23436 **
23437 ** Move the date backwards to the beginning of the current day,
23438 ** or month or year.
23439 */
23440 if( sqlite3_strnicmp(z, "start of ", 9)!=0 ) break;
23441 if( !p->validJD && !p->validYMD && !p->validHMS ) break;
23442 z += 9;
23443 computeYMD(p);
23444 p->validHMS = 1;
23445 p->h = p->m = 0;
23446 p->s = 0.0;
23447 p->rawS = 0;
23448 p->validTZ = 0;
23449 p->validJD = 0;
23450 if( sqlite3_stricmp(z,"month")==0 ){
23451 p->D = 1;
23452 rc = 0;
23453 }else if( sqlite3_stricmp(z,"year")==0 ){
23454 p->M = 1;
23455 p->D = 1;
23456 rc = 0;
23457 }else if( sqlite3_stricmp(z,"day")==0 ){
23458 rc = 0;
23459 }
23460 break;
23461 }
23462 case '+':
23463 case '-':
23464 case '0':
23465 case '1':
23466 case '2':
23467 case '3':
23468 case '4':
23469 case '5':
23470 case '6':
23471 case '7':
23472 case '8':
23473 case '9': {
23474 double rRounder;
23475 int i;
23476 for(n=1; z[n] && z[n]!=':' && !sqlite3Isspace(z[n]); n++){}
23477 if( sqlite3AtoF(z, &r, n, SQLITE_UTF8)<=0 ){
23478 rc = 1;
23479 break;
23480 }
23481 if( z[n]==':' ){
23482 /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
23483 ** specified number of hours, minutes, seconds, and fractional seconds
23484 ** to the time. The ".FFF" may be omitted. The ":SS.FFF" may be
23485 ** omitted.
23486 */
23487 const char *z2 = z;
23488 DateTime tx;
23489 sqlite3_int64 day;
23490 if( !sqlite3Isdigit(*z2) ) z2++;
23491 memset(s: &tx, c: 0, n: sizeof(tx));
23492 if( parseHhMmSs(zDate: z2, p: &tx) ) break;
23493 computeJD(p: &tx);
23494 tx.iJD -= 43200000;
23495 day = tx.iJD/86400000;
23496 tx.iJD -= day*86400000;
23497 if( z[0]=='-' ) tx.iJD = -tx.iJD;
23498 computeJD(p);
23499 clearYMD_HMS_TZ(p);
23500 p->iJD += tx.iJD;
23501 rc = 0;
23502 break;
23503 }
23504
23505 /* If control reaches this point, it means the transformation is
23506 ** one of the forms like "+NNN days". */
23507 z += n;
23508 while( sqlite3Isspace(*z) ) z++;
23509 n = sqlite3Strlen30(z);
23510 if( n>10 || n<3 ) break;
23511 if( sqlite3UpperToLower[(u8)z[n-1]]=='s' ) n--;
23512 computeJD(p);
23513 rc = 1;
23514 rRounder = r<0 ? -0.5 : +0.5;
23515 for(i=0; i<ArraySize(aXformType); i++){
23516 if( aXformType[i].nName==n
23517 && sqlite3_strnicmp(aXformType[i].zName, z, n)==0
23518 && r>-aXformType[i].rLimit && r<aXformType[i].rLimit
23519 ){
23520 switch( aXformType[i].eType ){
23521 case 1: { /* Special processing to add months */
23522 int x;
23523 computeYMD_HMS(p);
23524 p->M += (int)r;
23525 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
23526 p->Y += x;
23527 p->M -= x*12;
23528 p->validJD = 0;
23529 r -= (int)r;
23530 break;
23531 }
23532 case 2: { /* Special processing to add years */
23533 int y = (int)r;
23534 computeYMD_HMS(p);
23535 p->Y += y;
23536 p->validJD = 0;
23537 r -= (int)r;
23538 break;
23539 }
23540 }
23541 computeJD(p);
23542 p->iJD += (sqlite3_int64)(r*aXformType[i].rXform + rRounder);
23543 rc = 0;
23544 break;
23545 }
23546 }
23547 clearYMD_HMS_TZ(p);
23548 break;
23549 }
23550 default: {
23551 break;
23552 }
23553 }
23554 return rc;
23555}
23556
23557/*
23558** Process time function arguments. argv[0] is a date-time stamp.
23559** argv[1] and following are modifiers. Parse them all and write
23560** the resulting time into the DateTime structure p. Return 0
23561** on success and 1 if there are any errors.
23562**
23563** If there are zero parameters (if even argv[0] is undefined)
23564** then assume a default value of "now" for argv[0].
23565*/
23566static int isDate(
23567 sqlite3_context *context,
23568 int argc,
23569 sqlite3_value **argv,
23570 DateTime *p
23571){
23572 int i, n;
23573 const unsigned char *z;
23574 int eType;
23575 memset(s: p, c: 0, n: sizeof(*p));
23576 if( argc==0 ){
23577 if( !sqlite3NotPureFunc(context) ) return 1;
23578 return setDateTimeToCurrent(context, p);
23579 }
23580 if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT
23581 || eType==SQLITE_INTEGER ){
23582 setRawDateNumber(p, r: sqlite3_value_double(argv[0]));
23583 }else{
23584 z = sqlite3_value_text(argv[0]);
23585 if( !z || parseDateOrTime(context, zDate: (char*)z, p) ){
23586 return 1;
23587 }
23588 }
23589 for(i=1; i<argc; i++){
23590 z = sqlite3_value_text(argv[i]);
23591 n = sqlite3_value_bytes(argv[i]);
23592 if( z==0 || parseModifier(pCtx: context, z: (char*)z, n, p) ) return 1;
23593 }
23594 computeJD(p);
23595 if( p->isError || !validJulianDay(iJD: p->iJD) ) return 1;
23596 return 0;
23597}
23598
23599
23600/*
23601** The following routines implement the various date and time functions
23602** of SQLite.
23603*/
23604
23605/*
23606** julianday( TIMESTRING, MOD, MOD, ...)
23607**
23608** Return the julian day number of the date specified in the arguments
23609*/
23610static void juliandayFunc(
23611 sqlite3_context *context,
23612 int argc,
23613 sqlite3_value **argv
23614){
23615 DateTime x;
23616 if( isDate(context, argc, argv, p: &x)==0 ){
23617 computeJD(p: &x);
23618 sqlite3_result_double(context, x.iJD/86400000.0);
23619 }
23620}
23621
23622/*
23623** datetime( TIMESTRING, MOD, MOD, ...)
23624**
23625** Return YYYY-MM-DD HH:MM:SS
23626*/
23627static void datetimeFunc(
23628 sqlite3_context *context,
23629 int argc,
23630 sqlite3_value **argv
23631){
23632 DateTime x;
23633 if( isDate(context, argc, argv, p: &x)==0 ){
23634 char zBuf[100];
23635 computeYMD_HMS(p: &x);
23636 sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d",
23637 x.Y, x.M, x.D, x.h, x.m, (int)(x.s));
23638 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
23639 }
23640}
23641
23642/*
23643** time( TIMESTRING, MOD, MOD, ...)
23644**
23645** Return HH:MM:SS
23646*/
23647static void timeFunc(
23648 sqlite3_context *context,
23649 int argc,
23650 sqlite3_value **argv
23651){
23652 DateTime x;
23653 if( isDate(context, argc, argv, p: &x)==0 ){
23654 char zBuf[100];
23655 computeHMS(p: &x);
23656 sqlite3_snprintf(sizeof(zBuf), zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s);
23657 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
23658 }
23659}
23660
23661/*
23662** date( TIMESTRING, MOD, MOD, ...)
23663**
23664** Return YYYY-MM-DD
23665*/
23666static void dateFunc(
23667 sqlite3_context *context,
23668 int argc,
23669 sqlite3_value **argv
23670){
23671 DateTime x;
23672 if( isDate(context, argc, argv, p: &x)==0 ){
23673 char zBuf[100];
23674 computeYMD(p: &x);
23675 sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
23676 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
23677 }
23678}
23679
23680/*
23681** strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
23682**
23683** Return a string described by FORMAT. Conversions as follows:
23684**
23685** %d day of month
23686** %f ** fractional seconds SS.SSS
23687** %H hour 00-24
23688** %j day of year 000-366
23689** %J ** julian day number
23690** %m month 01-12
23691** %M minute 00-59
23692** %s seconds since 1970-01-01
23693** %S seconds 00-59
23694** %w day of week 0-6 sunday==0
23695** %W week of year 00-53
23696** %Y year 0000-9999
23697** %% %
23698*/
23699static void strftimeFunc(
23700 sqlite3_context *context,
23701 int argc,
23702 sqlite3_value **argv
23703){
23704 DateTime x;
23705 size_t i,j;
23706 sqlite3 *db;
23707 const char *zFmt;
23708 sqlite3_str sRes;
23709
23710
23711 if( argc==0 ) return;
23712 zFmt = (const char*)sqlite3_value_text(argv[0]);
23713 if( zFmt==0 || isDate(context, argc: argc-1, argv: argv+1, p: &x) ) return;
23714 db = sqlite3_context_db_handle(context);
23715 sqlite3StrAccumInit(&sRes, 0, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
23716
23717 computeJD(p: &x);
23718 computeYMD_HMS(p: &x);
23719 for(i=j=0; zFmt[i]; i++){
23720 if( zFmt[i]!='%' ) continue;
23721 if( j<i ) sqlite3_str_append(&sRes, zIn: zFmt+j, N: (int)(i-j));
23722 i++;
23723 j = i + 1;
23724 switch( zFmt[i] ){
23725 case 'd': {
23726 sqlite3_str_appendf(&sRes, zFormat: "%02d", x.D);
23727 break;
23728 }
23729 case 'f': {
23730 double s = x.s;
23731 if( s>59.999 ) s = 59.999;
23732 sqlite3_str_appendf(&sRes, zFormat: "%06.3f", s);
23733 break;
23734 }
23735 case 'H': {
23736 sqlite3_str_appendf(&sRes, zFormat: "%02d", x.h);
23737 break;
23738 }
23739 case 'W': /* Fall thru */
23740 case 'j': {
23741 int nDay; /* Number of days since 1st day of year */
23742 DateTime y = x;
23743 y.validJD = 0;
23744 y.M = 1;
23745 y.D = 1;
23746 computeJD(p: &y);
23747 nDay = (int)((x.iJD-y.iJD+43200000)/86400000);
23748 if( zFmt[i]=='W' ){
23749 int wd; /* 0=Monday, 1=Tuesday, ... 6=Sunday */
23750 wd = (int)(((x.iJD+43200000)/86400000)%7);
23751 sqlite3_str_appendf(&sRes,zFormat: "%02d",(nDay+7-wd)/7);
23752 }else{
23753 sqlite3_str_appendf(&sRes,zFormat: "%03d",nDay+1);
23754 }
23755 break;
23756 }
23757 case 'J': {
23758 sqlite3_str_appendf(&sRes,zFormat: "%.16g",x.iJD/86400000.0);
23759 break;
23760 }
23761 case 'm': {
23762 sqlite3_str_appendf(&sRes,zFormat: "%02d",x.M);
23763 break;
23764 }
23765 case 'M': {
23766 sqlite3_str_appendf(&sRes,zFormat: "%02d",x.m);
23767 break;
23768 }
23769 case 's': {
23770 i64 iS = (i64)(x.iJD/1000 - 21086676*(i64)10000);
23771 sqlite3_str_appendf(&sRes,zFormat: "%lld",iS);
23772 break;
23773 }
23774 case 'S': {
23775 sqlite3_str_appendf(&sRes,zFormat: "%02d",(int)x.s);
23776 break;
23777 }
23778 case 'w': {
23779 sqlite3_str_appendchar(&sRes, N: 1,
23780 C: (char)(((x.iJD+129600000)/86400000) % 7) + '0');
23781 break;
23782 }
23783 case 'Y': {
23784 sqlite3_str_appendf(&sRes,zFormat: "%04d",x.Y);
23785 break;
23786 }
23787 case '%': {
23788 sqlite3_str_appendchar(&sRes, N: 1, C: '%');
23789 break;
23790 }
23791 default: {
23792 sqlite3_str_reset(&sRes);
23793 return;
23794 }
23795 }
23796 }
23797 if( j<i ) sqlite3_str_append(&sRes, zIn: zFmt+j, N: (int)(i-j));
23798 sqlite3ResultStrAccum(context, &sRes);
23799}
23800
23801/*
23802** current_time()
23803**
23804** This function returns the same value as time('now').
23805*/
23806static void ctimeFunc(
23807 sqlite3_context *context,
23808 int NotUsed,
23809 sqlite3_value **NotUsed2
23810){
23811 UNUSED_PARAMETER2(NotUsed, NotUsed2);
23812 timeFunc(context, argc: 0, argv: 0);
23813}
23814
23815/*
23816** current_date()
23817**
23818** This function returns the same value as date('now').
23819*/
23820static void cdateFunc(
23821 sqlite3_context *context,
23822 int NotUsed,
23823 sqlite3_value **NotUsed2
23824){
23825 UNUSED_PARAMETER2(NotUsed, NotUsed2);
23826 dateFunc(context, argc: 0, argv: 0);
23827}
23828
23829/*
23830** current_timestamp()
23831**
23832** This function returns the same value as datetime('now').
23833*/
23834static void ctimestampFunc(
23835 sqlite3_context *context,
23836 int NotUsed,
23837 sqlite3_value **NotUsed2
23838){
23839 UNUSED_PARAMETER2(NotUsed, NotUsed2);
23840 datetimeFunc(context, argc: 0, argv: 0);
23841}
23842#endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
23843
23844#ifdef SQLITE_OMIT_DATETIME_FUNCS
23845/*
23846** If the library is compiled to omit the full-scale date and time
23847** handling (to get a smaller binary), the following minimal version
23848** of the functions current_time(), current_date() and current_timestamp()
23849** are included instead. This is to support column declarations that
23850** include "DEFAULT CURRENT_TIME" etc.
23851**
23852** This function uses the C-library functions time(), gmtime()
23853** and strftime(). The format string to pass to strftime() is supplied
23854** as the user-data for the function.
23855*/
23856static void currentTimeFunc(
23857 sqlite3_context *context,
23858 int argc,
23859 sqlite3_value **argv
23860){
23861 time_t t;
23862 char *zFormat = (char *)sqlite3_user_data(context);
23863 sqlite3_int64 iT;
23864 struct tm *pTm;
23865 struct tm sNow;
23866 char zBuf[20];
23867
23868 UNUSED_PARAMETER(argc);
23869 UNUSED_PARAMETER(argv);
23870
23871 iT = sqlite3StmtCurrentTime(context);
23872 if( iT<=0 ) return;
23873 t = iT/1000 - 10000*(sqlite3_int64)21086676;
23874#if HAVE_GMTIME_R
23875 pTm = gmtime_r(&t, &sNow);
23876#else
23877 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN));
23878 pTm = gmtime(&t);
23879 if( pTm ) memcpy(&sNow, pTm, sizeof(sNow));
23880 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN));
23881#endif
23882 if( pTm ){
23883 strftime(zBuf, 20, zFormat, &sNow);
23884 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
23885 }
23886}
23887#endif
23888
23889/*
23890** This function registered all of the above C functions as SQL
23891** functions. This should be the only routine in this file with
23892** external linkage.
23893*/
23894SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void){
23895 static FuncDef aDateTimeFuncs[] = {
23896#ifndef SQLITE_OMIT_DATETIME_FUNCS
23897 PURE_DATE(julianday, -1, 0, 0, juliandayFunc ),
23898 PURE_DATE(date, -1, 0, 0, dateFunc ),
23899 PURE_DATE(time, -1, 0, 0, timeFunc ),
23900 PURE_DATE(datetime, -1, 0, 0, datetimeFunc ),
23901 PURE_DATE(strftime, -1, 0, 0, strftimeFunc ),
23902 DFUNCTION(current_time, 0, 0, 0, ctimeFunc ),
23903 DFUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc),
23904 DFUNCTION(current_date, 0, 0, 0, cdateFunc ),
23905#else
23906 STR_FUNCTION(current_time, 0, "%H:%M:%S", 0, currentTimeFunc),
23907 STR_FUNCTION(current_date, 0, "%Y-%m-%d", 0, currentTimeFunc),
23908 STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc),
23909#endif
23910 };
23911 sqlite3InsertBuiltinFuncs(aDateTimeFuncs, ArraySize(aDateTimeFuncs));
23912}
23913
23914/************** End of date.c ************************************************/
23915/************** Begin file os.c **********************************************/
23916/*
23917** 2005 November 29
23918**
23919** The author disclaims copyright to this source code. In place of
23920** a legal notice, here is a blessing:
23921**
23922** May you do good and not evil.
23923** May you find forgiveness for yourself and forgive others.
23924** May you share freely, never taking more than you give.
23925**
23926******************************************************************************
23927**
23928** This file contains OS interface code that is common to all
23929** architectures.
23930*/
23931/* #include "sqliteInt.h" */
23932
23933/*
23934** If we compile with the SQLITE_TEST macro set, then the following block
23935** of code will give us the ability to simulate a disk I/O error. This
23936** is used for testing the I/O recovery logic.
23937*/
23938#if defined(SQLITE_TEST)
23939SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */
23940SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
23941SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O error */
23942SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persist */
23943SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */
23944SQLITE_API int sqlite3_diskfull_pending = 0;
23945SQLITE_API int sqlite3_diskfull = 0;
23946#endif /* defined(SQLITE_TEST) */
23947
23948/*
23949** When testing, also keep a count of the number of open files.
23950*/
23951#if defined(SQLITE_TEST)
23952SQLITE_API int sqlite3_open_file_count = 0;
23953#endif /* defined(SQLITE_TEST) */
23954
23955/*
23956** The default SQLite sqlite3_vfs implementations do not allocate
23957** memory (actually, os_unix.c allocates a small amount of memory
23958** from within OsOpen()), but some third-party implementations may.
23959** So we test the effects of a malloc() failing and the sqlite3OsXXX()
23960** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
23961**
23962** The following functions are instrumented for malloc() failure
23963** testing:
23964**
23965** sqlite3OsRead()
23966** sqlite3OsWrite()
23967** sqlite3OsSync()
23968** sqlite3OsFileSize()
23969** sqlite3OsLock()
23970** sqlite3OsCheckReservedLock()
23971** sqlite3OsFileControl()
23972** sqlite3OsShmMap()
23973** sqlite3OsOpen()
23974** sqlite3OsDelete()
23975** sqlite3OsAccess()
23976** sqlite3OsFullPathname()
23977**
23978*/
23979#if defined(SQLITE_TEST)
23980SQLITE_API int sqlite3_memdebug_vfs_oom_test = 1;
23981 #define DO_OS_MALLOC_TEST(x) \
23982 if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3JournalIsInMemory(x))) { \
23983 void *pTstAlloc = sqlite3Malloc(10); \
23984 if (!pTstAlloc) return SQLITE_IOERR_NOMEM_BKPT; \
23985 sqlite3_free(pTstAlloc); \
23986 }
23987#else
23988 #define DO_OS_MALLOC_TEST(x)
23989#endif
23990
23991/*
23992** The following routines are convenience wrappers around methods
23993** of the sqlite3_file object. This is mostly just syntactic sugar. All
23994** of this would be completely automatic if SQLite were coded using
23995** C++ instead of plain old C.
23996*/
23997SQLITE_PRIVATE void sqlite3OsClose(sqlite3_file *pId){
23998 if( pId->pMethods ){
23999 pId->pMethods->xClose(pId);
24000 pId->pMethods = 0;
24001 }
24002}
24003SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
24004 DO_OS_MALLOC_TEST(id);
24005 return id->pMethods->xRead(id, pBuf, amt, offset);
24006}
24007SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
24008 DO_OS_MALLOC_TEST(id);
24009 return id->pMethods->xWrite(id, pBuf, amt, offset);
24010}
24011SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file *id, i64 size){
24012 return id->pMethods->xTruncate(id, size);
24013}
24014SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file *id, int flags){
24015 DO_OS_MALLOC_TEST(id);
24016 return flags ? id->pMethods->xSync(id, flags) : SQLITE_OK;
24017}
24018SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
24019 DO_OS_MALLOC_TEST(id);
24020 return id->pMethods->xFileSize(id, pSize);
24021}
24022SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file *id, int lockType){
24023 DO_OS_MALLOC_TEST(id);
24024 return id->pMethods->xLock(id, lockType);
24025}
24026SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file *id, int lockType){
24027 return id->pMethods->xUnlock(id, lockType);
24028}
24029SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
24030 DO_OS_MALLOC_TEST(id);
24031 return id->pMethods->xCheckReservedLock(id, pResOut);
24032}
24033
24034/*
24035** Use sqlite3OsFileControl() when we are doing something that might fail
24036** and we need to know about the failures. Use sqlite3OsFileControlHint()
24037** when simply tossing information over the wall to the VFS and we do not
24038** really care if the VFS receives and understands the information since it
24039** is only a hint and can be safely ignored. The sqlite3OsFileControlHint()
24040** routine has no return value since the return value would be meaningless.
24041*/
24042SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
24043 if( id->pMethods==0 ) return SQLITE_NOTFOUND;
24044#ifdef SQLITE_TEST
24045 if( op!=SQLITE_FCNTL_COMMIT_PHASETWO
24046 && op!=SQLITE_FCNTL_LOCK_TIMEOUT
24047 && op!=SQLITE_FCNTL_CKPT_DONE
24048 && op!=SQLITE_FCNTL_CKPT_START
24049 ){
24050 /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
24051 ** is using a regular VFS, it is called after the corresponding
24052 ** transaction has been committed. Injecting a fault at this point
24053 ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
24054 ** but the transaction is committed anyway.
24055 **
24056 ** The core must call OsFileControl() though, not OsFileControlHint(),
24057 ** as if a custom VFS (e.g. zipvfs) returns an error here, it probably
24058 ** means the commit really has failed and an error should be returned
24059 ** to the user.
24060 **
24061 ** The CKPT_DONE and CKPT_START file-controls are write-only signals
24062 ** to the cksumvfs. Their return code is meaningless and is ignored
24063 ** by the SQLite core, so there is no point in simulating OOMs for them.
24064 */
24065 DO_OS_MALLOC_TEST(id);
24066 }
24067#endif
24068 return id->pMethods->xFileControl(id, op, pArg);
24069}
24070SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){
24071 if( id->pMethods ) (void)id->pMethods->xFileControl(id, op, pArg);
24072}
24073
24074SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){
24075 int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
24076 return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
24077}
24078SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
24079 if( NEVER(id->pMethods==0) ) return 0;
24080 return id->pMethods->xDeviceCharacteristics(id);
24081}
24082#ifndef SQLITE_OMIT_WAL
24083SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){
24084 return id->pMethods->xShmLock(id, offset, n, flags);
24085}
24086SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id){
24087 id->pMethods->xShmBarrier(id);
24088}
24089SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){
24090 return id->pMethods->xShmUnmap(id, deleteFlag);
24091}
24092SQLITE_PRIVATE int sqlite3OsShmMap(
24093 sqlite3_file *id, /* Database file handle */
24094 int iPage,
24095 int pgsz,
24096 int bExtend, /* True to extend file if necessary */
24097 void volatile **pp /* OUT: Pointer to mapping */
24098){
24099 DO_OS_MALLOC_TEST(id);
24100 return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
24101}
24102#endif /* SQLITE_OMIT_WAL */
24103
24104#if SQLITE_MAX_MMAP_SIZE>0
24105/* The real implementation of xFetch and xUnfetch */
24106SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
24107 DO_OS_MALLOC_TEST(id);
24108 return id->pMethods->xFetch(id, iOff, iAmt, pp);
24109}
24110SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
24111 return id->pMethods->xUnfetch(id, iOff, p);
24112}
24113#else
24114/* No-op stubs to use when memory-mapped I/O is disabled */
24115SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
24116 *pp = 0;
24117 return SQLITE_OK;
24118}
24119SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
24120 return SQLITE_OK;
24121}
24122#endif
24123
24124/*
24125** The next group of routines are convenience wrappers around the
24126** VFS methods.
24127*/
24128SQLITE_PRIVATE int sqlite3OsOpen(
24129 sqlite3_vfs *pVfs,
24130 const char *zPath,
24131 sqlite3_file *pFile,
24132 int flags,
24133 int *pFlagsOut
24134){
24135 int rc;
24136 DO_OS_MALLOC_TEST(0);
24137 /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed
24138 ** down into the VFS layer. Some SQLITE_OPEN_ flags (for example,
24139 ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
24140 ** reaching the VFS. */
24141 rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x1087f7f, pFlagsOut);
24142 assert( rc==SQLITE_OK || pFile->pMethods==0 );
24143 return rc;
24144}
24145SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
24146 DO_OS_MALLOC_TEST(0);
24147 assert( dirSync==0 || dirSync==1 );
24148 return pVfs->xDelete!=0 ? pVfs->xDelete(pVfs, zPath, dirSync) : SQLITE_OK;
24149}
24150SQLITE_PRIVATE int sqlite3OsAccess(
24151 sqlite3_vfs *pVfs,
24152 const char *zPath,
24153 int flags,
24154 int *pResOut
24155){
24156 DO_OS_MALLOC_TEST(0);
24157 return pVfs->xAccess(pVfs, zPath, flags, pResOut);
24158}
24159SQLITE_PRIVATE int sqlite3OsFullPathname(
24160 sqlite3_vfs *pVfs,
24161 const char *zPath,
24162 int nPathOut,
24163 char *zPathOut
24164){
24165 DO_OS_MALLOC_TEST(0);
24166 zPathOut[0] = 0;
24167 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
24168}
24169#ifndef SQLITE_OMIT_LOAD_EXTENSION
24170SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
24171 assert( zPath!=0 );
24172 assert( strlen(zPath)<=SQLITE_MAX_PATHLEN ); /* tag-20210611-1 */
24173 return pVfs->xDlOpen(pVfs, zPath);
24174}
24175SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
24176 pVfs->xDlError(pVfs, nByte, zBufOut);
24177}
24178SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHdle, const char *zSym))(void){
24179 return pVfs->xDlSym(pVfs, pHdle, zSym);
24180}
24181SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
24182 pVfs->xDlClose(pVfs, pHandle);
24183}
24184#endif /* SQLITE_OMIT_LOAD_EXTENSION */
24185SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
24186 if( sqlite3Config.iPrngSeed ){
24187 memset(s: zBufOut, c: 0, n: nByte);
24188 if( ALWAYS(nByte>(signed)sizeof(unsigned)) ) nByte = sizeof(unsigned int);
24189 memcpy(dest: zBufOut, src: &sqlite3Config.iPrngSeed, n: nByte);
24190 return SQLITE_OK;
24191 }else{
24192 return pVfs->xRandomness(pVfs, nByte, zBufOut);
24193 }
24194
24195}
24196SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
24197 return pVfs->xSleep(pVfs, nMicro);
24198}
24199SQLITE_PRIVATE int sqlite3OsGetLastError(sqlite3_vfs *pVfs){
24200 return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0;
24201}
24202SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
24203 int rc;
24204 /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
24205 ** method to get the current date and time if that method is available
24206 ** (if iVersion is 2 or greater and the function pointer is not NULL) and
24207 ** will fall back to xCurrentTime() if xCurrentTimeInt64() is
24208 ** unavailable.
24209 */
24210 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
24211 rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
24212 }else{
24213 double r;
24214 rc = pVfs->xCurrentTime(pVfs, &r);
24215 *pTimeOut = (sqlite3_int64)(r*86400000.0);
24216 }
24217 return rc;
24218}
24219
24220SQLITE_PRIVATE int sqlite3OsOpenMalloc(
24221 sqlite3_vfs *pVfs,
24222 const char *zFile,
24223 sqlite3_file **ppFile,
24224 int flags,
24225 int *pOutFlags
24226){
24227 int rc;
24228 sqlite3_file *pFile;
24229 pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
24230 if( pFile ){
24231 rc = sqlite3OsOpen(pVfs, zPath: zFile, pFile, flags, pFlagsOut: pOutFlags);
24232 if( rc!=SQLITE_OK ){
24233 sqlite3_free(pFile);
24234 *ppFile = 0;
24235 }else{
24236 *ppFile = pFile;
24237 }
24238 }else{
24239 *ppFile = 0;
24240 rc = SQLITE_NOMEM_BKPT;
24241 }
24242 assert( *ppFile!=0 || rc!=SQLITE_OK );
24243 return rc;
24244}
24245SQLITE_PRIVATE void sqlite3OsCloseFree(sqlite3_file *pFile){
24246 assert( pFile );
24247 sqlite3OsClose(pId: pFile);
24248 sqlite3_free(pFile);
24249}
24250
24251/*
24252** This function is a wrapper around the OS specific implementation of
24253** sqlite3_os_init(). The purpose of the wrapper is to provide the
24254** ability to simulate a malloc failure, so that the handling of an
24255** error in sqlite3_os_init() by the upper layers can be tested.
24256*/
24257SQLITE_PRIVATE int sqlite3OsInit(void){
24258 void *p = sqlite3_malloc(10);
24259 if( p==0 ) return SQLITE_NOMEM_BKPT;
24260 sqlite3_free(p);
24261 return sqlite3_os_init();
24262}
24263
24264/*
24265** The list of all registered VFS implementations.
24266*/
24267static sqlite3_vfs * SQLITE_WSD vfsList = 0;
24268#define vfsList GLOBAL(sqlite3_vfs *, vfsList)
24269
24270/*
24271** Locate a VFS by name. If no name is given, simply return the
24272** first VFS on the list.
24273*/
24274SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
24275 sqlite3_vfs *pVfs = 0;
24276#if SQLITE_THREADSAFE
24277 sqlite3_mutex *mutex;
24278#endif
24279#ifndef SQLITE_OMIT_AUTOINIT
24280 int rc = sqlite3_initialize();
24281 if( rc ) return 0;
24282#endif
24283#if SQLITE_THREADSAFE
24284 mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);
24285#endif
24286 sqlite3_mutex_enter(mutex);
24287 for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
24288 if( zVfs==0 ) break;
24289 if( strcmp(s1: zVfs, s2: pVfs->zName)==0 ) break;
24290 }
24291 sqlite3_mutex_leave(mutex);
24292 return pVfs;
24293}
24294
24295/*
24296** Unlink a VFS from the linked list
24297*/
24298static void vfsUnlink(sqlite3_vfs *pVfs){
24299 assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN)) );
24300 if( pVfs==0 ){
24301 /* No-op */
24302 }else if( vfsList==pVfs ){
24303 vfsList = pVfs->pNext;
24304 }else if( vfsList ){
24305 sqlite3_vfs *p = vfsList;
24306 while( p->pNext && p->pNext!=pVfs ){
24307 p = p->pNext;
24308 }
24309 if( p->pNext==pVfs ){
24310 p->pNext = pVfs->pNext;
24311 }
24312 }
24313}
24314
24315/*
24316** Register a VFS with the system. It is harmless to register the same
24317** VFS multiple times. The new VFS becomes the default if makeDflt is
24318** true.
24319*/
24320SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
24321 MUTEX_LOGIC(sqlite3_mutex *mutex;)
24322#ifndef SQLITE_OMIT_AUTOINIT
24323 int rc = sqlite3_initialize();
24324 if( rc ) return rc;
24325#endif
24326#ifdef SQLITE_ENABLE_API_ARMOR
24327 if( pVfs==0 ) return SQLITE_MISUSE_BKPT;
24328#endif
24329
24330 MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )
24331 sqlite3_mutex_enter(mutex);
24332 vfsUnlink(pVfs);
24333 if( makeDflt || vfsList==0 ){
24334 pVfs->pNext = vfsList;
24335 vfsList = pVfs;
24336 }else{
24337 pVfs->pNext = vfsList->pNext;
24338 vfsList->pNext = pVfs;
24339 }
24340 assert(vfsList);
24341 sqlite3_mutex_leave(mutex);
24342 return SQLITE_OK;
24343}
24344
24345/*
24346** Unregister a VFS so that it is no longer accessible.
24347*/
24348SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
24349 MUTEX_LOGIC(sqlite3_mutex *mutex;)
24350#ifndef SQLITE_OMIT_AUTOINIT
24351 int rc = sqlite3_initialize();
24352 if( rc ) return rc;
24353#endif
24354 MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )
24355 sqlite3_mutex_enter(mutex);
24356 vfsUnlink(pVfs);
24357 sqlite3_mutex_leave(mutex);
24358 return SQLITE_OK;
24359}
24360
24361/************** End of os.c **************************************************/
24362/************** Begin file fault.c *******************************************/
24363/*
24364** 2008 Jan 22
24365**
24366** The author disclaims copyright to this source code. In place of
24367** a legal notice, here is a blessing:
24368**
24369** May you do good and not evil.
24370** May you find forgiveness for yourself and forgive others.
24371** May you share freely, never taking more than you give.
24372**
24373*************************************************************************
24374**
24375** This file contains code to support the concept of "benign"
24376** malloc failures (when the xMalloc() or xRealloc() method of the
24377** sqlite3_mem_methods structure fails to allocate a block of memory
24378** and returns 0).
24379**
24380** Most malloc failures are non-benign. After they occur, SQLite
24381** abandons the current operation and returns an error code (usually
24382** SQLITE_NOMEM) to the user. However, sometimes a fault is not necessarily
24383** fatal. For example, if a malloc fails while resizing a hash table, this
24384** is completely recoverable simply by not carrying out the resize. The
24385** hash table will continue to function normally. So a malloc failure
24386** during a hash table resize is a benign fault.
24387*/
24388
24389/* #include "sqliteInt.h" */
24390
24391#ifndef SQLITE_UNTESTABLE
24392
24393/*
24394** Global variables.
24395*/
24396typedef struct BenignMallocHooks BenignMallocHooks;
24397static SQLITE_WSD struct BenignMallocHooks {
24398 void (*xBenignBegin)(void);
24399 void (*xBenignEnd)(void);
24400} sqlite3Hooks = { 0, 0 };
24401
24402/* The "wsdHooks" macro will resolve to the appropriate BenignMallocHooks
24403** structure. If writable static data is unsupported on the target,
24404** we have to locate the state vector at run-time. In the more common
24405** case where writable static data is supported, wsdHooks can refer directly
24406** to the "sqlite3Hooks" state vector declared above.
24407*/
24408#ifdef SQLITE_OMIT_WSD
24409# define wsdHooksInit \
24410 BenignMallocHooks *x = &GLOBAL(BenignMallocHooks,sqlite3Hooks)
24411# define wsdHooks x[0]
24412#else
24413# define wsdHooksInit
24414# define wsdHooks sqlite3Hooks
24415#endif
24416
24417
24418/*
24419** Register hooks to call when sqlite3BeginBenignMalloc() and
24420** sqlite3EndBenignMalloc() are called, respectively.
24421*/
24422SQLITE_PRIVATE void sqlite3BenignMallocHooks(
24423 void (*xBenignBegin)(void),
24424 void (*xBenignEnd)(void)
24425){
24426 wsdHooksInit;
24427 wsdHooks.xBenignBegin = xBenignBegin;
24428 wsdHooks.xBenignEnd = xBenignEnd;
24429}
24430
24431/*
24432** This (sqlite3EndBenignMalloc()) is called by SQLite code to indicate that
24433** subsequent malloc failures are benign. A call to sqlite3EndBenignMalloc()
24434** indicates that subsequent malloc failures are non-benign.
24435*/
24436SQLITE_PRIVATE void sqlite3BeginBenignMalloc(void){
24437 wsdHooksInit;
24438 if( wsdHooks.xBenignBegin ){
24439 wsdHooks.xBenignBegin();
24440 }
24441}
24442SQLITE_PRIVATE void sqlite3EndBenignMalloc(void){
24443 wsdHooksInit;
24444 if( wsdHooks.xBenignEnd ){
24445 wsdHooks.xBenignEnd();
24446 }
24447}
24448
24449#endif /* #ifndef SQLITE_UNTESTABLE */
24450
24451/************** End of fault.c ***********************************************/
24452/************** Begin file mem0.c ********************************************/
24453/*
24454** 2008 October 28
24455**
24456** The author disclaims copyright to this source code. In place of
24457** a legal notice, here is a blessing:
24458**
24459** May you do good and not evil.
24460** May you find forgiveness for yourself and forgive others.
24461** May you share freely, never taking more than you give.
24462**
24463*************************************************************************
24464**
24465** This file contains a no-op memory allocation drivers for use when
24466** SQLITE_ZERO_MALLOC is defined. The allocation drivers implemented
24467** here always fail. SQLite will not operate with these drivers. These
24468** are merely placeholders. Real drivers must be substituted using
24469** sqlite3_config() before SQLite will operate.
24470*/
24471/* #include "sqliteInt.h" */
24472
24473/*
24474** This version of the memory allocator is the default. It is
24475** used when no other memory allocator is specified using compile-time
24476** macros.
24477*/
24478#ifdef SQLITE_ZERO_MALLOC
24479
24480/*
24481** No-op versions of all memory allocation routines
24482*/
24483static void *sqlite3MemMalloc(int nByte){ return 0; }
24484static void sqlite3MemFree(void *pPrior){ return; }
24485static void *sqlite3MemRealloc(void *pPrior, int nByte){ return 0; }
24486static int sqlite3MemSize(void *pPrior){ return 0; }
24487static int sqlite3MemRoundup(int n){ return n; }
24488static int sqlite3MemInit(void *NotUsed){ return SQLITE_OK; }
24489static void sqlite3MemShutdown(void *NotUsed){ return; }
24490
24491/*
24492** This routine is the only routine in this file with external linkage.
24493**
24494** Populate the low-level memory allocation function pointers in
24495** sqlite3GlobalConfig.m with pointers to the routines in this file.
24496*/
24497SQLITE_PRIVATE void sqlite3MemSetDefault(void){
24498 static const sqlite3_mem_methods defaultMethods = {
24499 sqlite3MemMalloc,
24500 sqlite3MemFree,
24501 sqlite3MemRealloc,
24502 sqlite3MemSize,
24503 sqlite3MemRoundup,
24504 sqlite3MemInit,
24505 sqlite3MemShutdown,
24506 0
24507 };
24508 sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
24509}
24510
24511#endif /* SQLITE_ZERO_MALLOC */
24512
24513/************** End of mem0.c ************************************************/
24514/************** Begin file mem1.c ********************************************/
24515/*
24516** 2007 August 14
24517**
24518** The author disclaims copyright to this source code. In place of
24519** a legal notice, here is a blessing:
24520**
24521** May you do good and not evil.
24522** May you find forgiveness for yourself and forgive others.
24523** May you share freely, never taking more than you give.
24524**
24525*************************************************************************
24526**
24527** This file contains low-level memory allocation drivers for when
24528** SQLite will use the standard C-library malloc/realloc/free interface
24529** to obtain the memory it needs.
24530**
24531** This file contains implementations of the low-level memory allocation
24532** routines specified in the sqlite3_mem_methods object. The content of
24533** this file is only used if SQLITE_SYSTEM_MALLOC is defined. The
24534** SQLITE_SYSTEM_MALLOC macro is defined automatically if neither the
24535** SQLITE_MEMDEBUG nor the SQLITE_WIN32_MALLOC macros are defined. The
24536** default configuration is to use memory allocation routines in this
24537** file.
24538**
24539** C-preprocessor macro summary:
24540**
24541** HAVE_MALLOC_USABLE_SIZE The configure script sets this symbol if
24542** the malloc_usable_size() interface exists
24543** on the target platform. Or, this symbol
24544** can be set manually, if desired.
24545** If an equivalent interface exists by
24546** a different name, using a separate -D
24547** option to rename it.
24548**
24549** SQLITE_WITHOUT_ZONEMALLOC Some older macs lack support for the zone
24550** memory allocator. Set this symbol to enable
24551** building on older macs.
24552**
24553** SQLITE_WITHOUT_MSIZE Set this symbol to disable the use of
24554** _msize() on windows systems. This might
24555** be necessary when compiling for Delphi,
24556** for example.
24557*/
24558/* #include "sqliteInt.h" */
24559
24560/*
24561** This version of the memory allocator is the default. It is
24562** used when no other memory allocator is specified using compile-time
24563** macros.
24564*/
24565#ifdef SQLITE_SYSTEM_MALLOC
24566#if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
24567
24568/*
24569** Use the zone allocator available on apple products unless the
24570** SQLITE_WITHOUT_ZONEMALLOC symbol is defined.
24571*/
24572#include <sys/sysctl.h>
24573#include <malloc/malloc.h>
24574#ifdef SQLITE_MIGHT_BE_SINGLE_CORE
24575#include <libkern/OSAtomic.h>
24576#endif /* SQLITE_MIGHT_BE_SINGLE_CORE */
24577static malloc_zone_t* _sqliteZone_;
24578#define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x))
24579#define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x));
24580#define SQLITE_REALLOC(x,y) malloc_zone_realloc(_sqliteZone_, (x), (y))
24581#define SQLITE_MALLOCSIZE(x) \
24582 (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
24583
24584#else /* if not __APPLE__ */
24585
24586/*
24587** Use standard C library malloc and free on non-Apple systems.
24588** Also used by Apple systems if SQLITE_WITHOUT_ZONEMALLOC is defined.
24589*/
24590#define SQLITE_MALLOC(x) malloc(x)
24591#define SQLITE_FREE(x) free(x)
24592#define SQLITE_REALLOC(x,y) realloc((x),(y))
24593
24594/*
24595** The malloc.h header file is needed for malloc_usable_size() function
24596** on some systems (e.g. Linux).
24597*/
24598#if HAVE_MALLOC_H && HAVE_MALLOC_USABLE_SIZE
24599# define SQLITE_USE_MALLOC_H 1
24600# define SQLITE_USE_MALLOC_USABLE_SIZE 1
24601/*
24602** The MSVCRT has malloc_usable_size(), but it is called _msize(). The
24603** use of _msize() is automatic, but can be disabled by compiling with
24604** -DSQLITE_WITHOUT_MSIZE. Using the _msize() function also requires
24605** the malloc.h header file.
24606*/
24607#elif defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE)
24608# define SQLITE_USE_MALLOC_H
24609# define SQLITE_USE_MSIZE
24610#endif
24611
24612/*
24613** Include the malloc.h header file, if necessary. Also set define macro
24614** SQLITE_MALLOCSIZE to the appropriate function name, which is _msize()
24615** for MSVC and malloc_usable_size() for most other systems (e.g. Linux).
24616** The memory size function can always be overridden manually by defining
24617** the macro SQLITE_MALLOCSIZE to the desired function name.
24618*/
24619#if defined(SQLITE_USE_MALLOC_H)
24620# include <malloc.h>
24621# if defined(SQLITE_USE_MALLOC_USABLE_SIZE)
24622# if !defined(SQLITE_MALLOCSIZE)
24623# define SQLITE_MALLOCSIZE(x) malloc_usable_size(x)
24624# endif
24625# elif defined(SQLITE_USE_MSIZE)
24626# if !defined(SQLITE_MALLOCSIZE)
24627# define SQLITE_MALLOCSIZE _msize
24628# endif
24629# endif
24630#endif /* defined(SQLITE_USE_MALLOC_H) */
24631
24632#endif /* __APPLE__ or not __APPLE__ */
24633
24634/*
24635** Like malloc(), but remember the size of the allocation
24636** so that we can find it later using sqlite3MemSize().
24637**
24638** For this low-level routine, we are guaranteed that nByte>0 because
24639** cases of nByte<=0 will be intercepted and dealt with by higher level
24640** routines.
24641*/
24642static void *sqlite3MemMalloc(int nByte){
24643#ifdef SQLITE_MALLOCSIZE
24644 void *p;
24645 testcase( ROUND8(nByte)==nByte );
24646 p = SQLITE_MALLOC( nByte );
24647 if( p==0 ){
24648 testcase( sqlite3GlobalConfig.xLog!=0 );
24649 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
24650 }
24651 return p;
24652#else
24653 sqlite3_int64 *p;
24654 assert( nByte>0 );
24655 testcase( ROUND8(nByte)!=nByte );
24656 p = SQLITE_MALLOC( nByte+8 );
24657 if( p ){
24658 p[0] = nByte;
24659 p++;
24660 }else{
24661 testcase( sqlite3GlobalConfig.xLog!=0 );
24662 sqlite3_log(SQLITE_NOMEM, zFormat: "failed to allocate %u bytes of memory", nByte);
24663 }
24664 return (void *)p;
24665#endif
24666}
24667
24668/*
24669** Like free() but works for allocations obtained from sqlite3MemMalloc()
24670** or sqlite3MemRealloc().
24671**
24672** For this low-level routine, we already know that pPrior!=0 since
24673** cases where pPrior==0 will have been intecepted and dealt with
24674** by higher-level routines.
24675*/
24676static void sqlite3MemFree(void *pPrior){
24677#ifdef SQLITE_MALLOCSIZE
24678 SQLITE_FREE(pPrior);
24679#else
24680 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
24681 assert( pPrior!=0 );
24682 p--;
24683 SQLITE_FREE(p);
24684#endif
24685}
24686
24687/*
24688** Report the allocated size of a prior return from xMalloc()
24689** or xRealloc().
24690*/
24691static int sqlite3MemSize(void *pPrior){
24692#ifdef SQLITE_MALLOCSIZE
24693 assert( pPrior!=0 );
24694 return (int)SQLITE_MALLOCSIZE(pPrior);
24695#else
24696 sqlite3_int64 *p;
24697 assert( pPrior!=0 );
24698 p = (sqlite3_int64*)pPrior;
24699 p--;
24700 return (int)p[0];
24701#endif
24702}
24703
24704/*
24705** Like realloc(). Resize an allocation previously obtained from
24706** sqlite3MemMalloc().
24707**
24708** For this low-level interface, we know that pPrior!=0. Cases where
24709** pPrior==0 while have been intercepted by higher-level routine and
24710** redirected to xMalloc. Similarly, we know that nByte>0 because
24711** cases where nByte<=0 will have been intercepted by higher-level
24712** routines and redirected to xFree.
24713*/
24714static void *sqlite3MemRealloc(void *pPrior, int nByte){
24715#ifdef SQLITE_MALLOCSIZE
24716 void *p = SQLITE_REALLOC(pPrior, nByte);
24717 if( p==0 ){
24718 testcase( sqlite3GlobalConfig.xLog!=0 );
24719 sqlite3_log(SQLITE_NOMEM,
24720 "failed memory resize %u to %u bytes",
24721 SQLITE_MALLOCSIZE(pPrior), nByte);
24722 }
24723 return p;
24724#else
24725 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
24726 assert( pPrior!=0 && nByte>0 );
24727 assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
24728 p--;
24729 p = SQLITE_REALLOC(p, nByte+8 );
24730 if( p ){
24731 p[0] = nByte;
24732 p++;
24733 }else{
24734 testcase( sqlite3GlobalConfig.xLog!=0 );
24735 sqlite3_log(SQLITE_NOMEM,
24736 zFormat: "failed memory resize %u to %u bytes",
24737 sqlite3MemSize(pPrior), nByte);
24738 }
24739 return (void*)p;
24740#endif
24741}
24742
24743/*
24744** Round up a request size to the next valid allocation size.
24745*/
24746static int sqlite3MemRoundup(int n){
24747 return ROUND8(n);
24748}
24749
24750/*
24751** Initialize this module.
24752*/
24753static int sqlite3MemInit(void *NotUsed){
24754#if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
24755 int cpuCount;
24756 size_t len;
24757 if( _sqliteZone_ ){
24758 return SQLITE_OK;
24759 }
24760 len = sizeof(cpuCount);
24761 /* One usually wants to use hw.acctivecpu for MT decisions, but not here */
24762 sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0);
24763 if( cpuCount>1 ){
24764 /* defer MT decisions to system malloc */
24765 _sqliteZone_ = malloc_default_zone();
24766 }else{
24767 /* only 1 core, use our own zone to contention over global locks,
24768 ** e.g. we have our own dedicated locks */
24769 _sqliteZone_ = malloc_create_zone(4096, 0);
24770 malloc_set_zone_name(_sqliteZone_, "Sqlite_Heap");
24771 }
24772#endif /* defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC) */
24773 UNUSED_PARAMETER(NotUsed);
24774 return SQLITE_OK;
24775}
24776
24777/*
24778** Deinitialize this module.
24779*/
24780static void sqlite3MemShutdown(void *NotUsed){
24781 UNUSED_PARAMETER(NotUsed);
24782 return;
24783}
24784
24785/*
24786** This routine is the only routine in this file with external linkage.
24787**
24788** Populate the low-level memory allocation function pointers in
24789** sqlite3GlobalConfig.m with pointers to the routines in this file.
24790*/
24791SQLITE_PRIVATE void sqlite3MemSetDefault(void){
24792 static const sqlite3_mem_methods defaultMethods = {
24793 sqlite3MemMalloc,
24794 sqlite3MemFree,
24795 sqlite3MemRealloc,
24796 sqlite3MemSize,
24797 sqlite3MemRoundup,
24798 sqlite3MemInit,
24799 sqlite3MemShutdown,
24800 0
24801 };
24802 sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
24803}
24804
24805#endif /* SQLITE_SYSTEM_MALLOC */
24806
24807/************** End of mem1.c ************************************************/
24808/************** Begin file mem2.c ********************************************/
24809/*
24810** 2007 August 15
24811**
24812** The author disclaims copyright to this source code. In place of
24813** a legal notice, here is a blessing:
24814**
24815** May you do good and not evil.
24816** May you find forgiveness for yourself and forgive others.
24817** May you share freely, never taking more than you give.
24818**
24819*************************************************************************
24820**
24821** This file contains low-level memory allocation drivers for when
24822** SQLite will use the standard C-library malloc/realloc/free interface
24823** to obtain the memory it needs while adding lots of additional debugging
24824** information to each allocation in order to help detect and fix memory
24825** leaks and memory usage errors.
24826**
24827** This file contains implementations of the low-level memory allocation
24828** routines specified in the sqlite3_mem_methods object.
24829*/
24830/* #include "sqliteInt.h" */
24831
24832/*
24833** This version of the memory allocator is used only if the
24834** SQLITE_MEMDEBUG macro is defined
24835*/
24836#ifdef SQLITE_MEMDEBUG
24837
24838/*
24839** The backtrace functionality is only available with GLIBC
24840*/
24841#ifdef __GLIBC__
24842 extern int backtrace(void**,int);
24843 extern void backtrace_symbols_fd(void*const*,int,int);
24844#else
24845# define backtrace(A,B) 1
24846# define backtrace_symbols_fd(A,B,C)
24847#endif
24848/* #include <stdio.h> */
24849
24850/*
24851** Each memory allocation looks like this:
24852**
24853** ------------------------------------------------------------------------
24854** | Title | backtrace pointers | MemBlockHdr | allocation | EndGuard |
24855** ------------------------------------------------------------------------
24856**
24857** The application code sees only a pointer to the allocation. We have
24858** to back up from the allocation pointer to find the MemBlockHdr. The
24859** MemBlockHdr tells us the size of the allocation and the number of
24860** backtrace pointers. There is also a guard word at the end of the
24861** MemBlockHdr.
24862*/
24863struct MemBlockHdr {
24864 i64 iSize; /* Size of this allocation */
24865 struct MemBlockHdr *pNext, *pPrev; /* Linked list of all unfreed memory */
24866 char nBacktrace; /* Number of backtraces on this alloc */
24867 char nBacktraceSlots; /* Available backtrace slots */
24868 u8 nTitle; /* Bytes of title; includes '\0' */
24869 u8 eType; /* Allocation type code */
24870 int iForeGuard; /* Guard word for sanity */
24871};
24872
24873/*
24874** Guard words
24875*/
24876#define FOREGUARD 0x80F5E153
24877#define REARGUARD 0xE4676B53
24878
24879/*
24880** Number of malloc size increments to track.
24881*/
24882#define NCSIZE 1000
24883
24884/*
24885** All of the static variables used by this module are collected
24886** into a single structure named "mem". This is to keep the
24887** static variables organized and to reduce namespace pollution
24888** when this module is combined with other in the amalgamation.
24889*/
24890static struct {
24891
24892 /*
24893 ** Mutex to control access to the memory allocation subsystem.
24894 */
24895 sqlite3_mutex *mutex;
24896
24897 /*
24898 ** Head and tail of a linked list of all outstanding allocations
24899 */
24900 struct MemBlockHdr *pFirst;
24901 struct MemBlockHdr *pLast;
24902
24903 /*
24904 ** The number of levels of backtrace to save in new allocations.
24905 */
24906 int nBacktrace;
24907 void (*xBacktrace)(int, int, void **);
24908
24909 /*
24910 ** Title text to insert in front of each block
24911 */
24912 int nTitle; /* Bytes of zTitle to save. Includes '\0' and padding */
24913 char zTitle[100]; /* The title text */
24914
24915 /*
24916 ** sqlite3MallocDisallow() increments the following counter.
24917 ** sqlite3MallocAllow() decrements it.
24918 */
24919 int disallow; /* Do not allow memory allocation */
24920
24921 /*
24922 ** Gather statistics on the sizes of memory allocations.
24923 ** nAlloc[i] is the number of allocation attempts of i*8
24924 ** bytes. i==NCSIZE is the number of allocation attempts for
24925 ** sizes more than NCSIZE*8 bytes.
24926 */
24927 int nAlloc[NCSIZE]; /* Total number of allocations */
24928 int nCurrent[NCSIZE]; /* Current number of allocations */
24929 int mxCurrent[NCSIZE]; /* Highwater mark for nCurrent */
24930
24931} mem;
24932
24933
24934/*
24935** Adjust memory usage statistics
24936*/
24937static void adjustStats(int iSize, int increment){
24938 int i = ROUND8(iSize)/8;
24939 if( i>NCSIZE-1 ){
24940 i = NCSIZE - 1;
24941 }
24942 if( increment>0 ){
24943 mem.nAlloc[i]++;
24944 mem.nCurrent[i]++;
24945 if( mem.nCurrent[i]>mem.mxCurrent[i] ){
24946 mem.mxCurrent[i] = mem.nCurrent[i];
24947 }
24948 }else{
24949 mem.nCurrent[i]--;
24950 assert( mem.nCurrent[i]>=0 );
24951 }
24952}
24953
24954/*
24955** Given an allocation, find the MemBlockHdr for that allocation.
24956**
24957** This routine checks the guards at either end of the allocation and
24958** if they are incorrect it asserts.
24959*/
24960static struct MemBlockHdr *sqlite3MemsysGetHeader(const void *pAllocation){
24961 struct MemBlockHdr *p;
24962 int *pInt;
24963 u8 *pU8;
24964 int nReserve;
24965
24966 p = (struct MemBlockHdr*)pAllocation;
24967 p--;
24968 assert( p->iForeGuard==(int)FOREGUARD );
24969 nReserve = ROUND8(p->iSize);
24970 pInt = (int*)pAllocation;
24971 pU8 = (u8*)pAllocation;
24972 assert( pInt[nReserve/sizeof(int)]==(int)REARGUARD );
24973 /* This checks any of the "extra" bytes allocated due
24974 ** to rounding up to an 8 byte boundary to ensure
24975 ** they haven't been overwritten.
24976 */
24977 while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 );
24978 return p;
24979}
24980
24981/*
24982** Return the number of bytes currently allocated at address p.
24983*/
24984static int sqlite3MemSize(void *p){
24985 struct MemBlockHdr *pHdr;
24986 if( !p ){
24987 return 0;
24988 }
24989 pHdr = sqlite3MemsysGetHeader(p);
24990 return (int)pHdr->iSize;
24991}
24992
24993/*
24994** Initialize the memory allocation subsystem.
24995*/
24996static int sqlite3MemInit(void *NotUsed){
24997 UNUSED_PARAMETER(NotUsed);
24998 assert( (sizeof(struct MemBlockHdr)&7) == 0 );
24999 if( !sqlite3GlobalConfig.bMemstat ){
25000 /* If memory status is enabled, then the malloc.c wrapper will already
25001 ** hold the STATIC_MEM mutex when the routines here are invoked. */
25002 mem.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
25003 }
25004 return SQLITE_OK;
25005}
25006
25007/*
25008** Deinitialize the memory allocation subsystem.
25009*/
25010static void sqlite3MemShutdown(void *NotUsed){
25011 UNUSED_PARAMETER(NotUsed);
25012 mem.mutex = 0;
25013}
25014
25015/*
25016** Round up a request size to the next valid allocation size.
25017*/
25018static int sqlite3MemRoundup(int n){
25019 return ROUND8(n);
25020}
25021
25022/*
25023** Fill a buffer with pseudo-random bytes. This is used to preset
25024** the content of a new memory allocation to unpredictable values and
25025** to clear the content of a freed allocation to unpredictable values.
25026*/
25027static void randomFill(char *pBuf, int nByte){
25028 unsigned int x, y, r;
25029 x = SQLITE_PTR_TO_INT(pBuf);
25030 y = nByte | 1;
25031 while( nByte >= 4 ){
25032 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
25033 y = y*1103515245 + 12345;
25034 r = x ^ y;
25035 *(int*)pBuf = r;
25036 pBuf += 4;
25037 nByte -= 4;
25038 }
25039 while( nByte-- > 0 ){
25040 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
25041 y = y*1103515245 + 12345;
25042 r = x ^ y;
25043 *(pBuf++) = r & 0xff;
25044 }
25045}
25046
25047/*
25048** Allocate nByte bytes of memory.
25049*/
25050static void *sqlite3MemMalloc(int nByte){
25051 struct MemBlockHdr *pHdr;
25052 void **pBt;
25053 char *z;
25054 int *pInt;
25055 void *p = 0;
25056 int totalSize;
25057 int nReserve;
25058 sqlite3_mutex_enter(mem.mutex);
25059 assert( mem.disallow==0 );
25060 nReserve = ROUND8(nByte);
25061 totalSize = nReserve + sizeof(*pHdr) + sizeof(int) +
25062 mem.nBacktrace*sizeof(void*) + mem.nTitle;
25063 p = malloc(totalSize);
25064 if( p ){
25065 z = p;
25066 pBt = (void**)&z[mem.nTitle];
25067 pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace];
25068 pHdr->pNext = 0;
25069 pHdr->pPrev = mem.pLast;
25070 if( mem.pLast ){
25071 mem.pLast->pNext = pHdr;
25072 }else{
25073 mem.pFirst = pHdr;
25074 }
25075 mem.pLast = pHdr;
25076 pHdr->iForeGuard = FOREGUARD;
25077 pHdr->eType = MEMTYPE_HEAP;
25078 pHdr->nBacktraceSlots = mem.nBacktrace;
25079 pHdr->nTitle = mem.nTitle;
25080 if( mem.nBacktrace ){
25081 void *aAddr[40];
25082 pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
25083 memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
25084 assert(pBt[0]);
25085 if( mem.xBacktrace ){
25086 mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]);
25087 }
25088 }else{
25089 pHdr->nBacktrace = 0;
25090 }
25091 if( mem.nTitle ){
25092 memcpy(z, mem.zTitle, mem.nTitle);
25093 }
25094 pHdr->iSize = nByte;
25095 adjustStats(nByte, +1);
25096 pInt = (int*)&pHdr[1];
25097 pInt[nReserve/sizeof(int)] = REARGUARD;
25098 randomFill((char*)pInt, nByte);
25099 memset(((char*)pInt)+nByte, 0x65, nReserve-nByte);
25100 p = (void*)pInt;
25101 }
25102 sqlite3_mutex_leave(mem.mutex);
25103 return p;
25104}
25105
25106/*
25107** Free memory.
25108*/
25109static void sqlite3MemFree(void *pPrior){
25110 struct MemBlockHdr *pHdr;
25111 void **pBt;
25112 char *z;
25113 assert( sqlite3GlobalConfig.bMemstat || sqlite3GlobalConfig.bCoreMutex==0
25114 || mem.mutex!=0 );
25115 pHdr = sqlite3MemsysGetHeader(pPrior);
25116 pBt = (void**)pHdr;
25117 pBt -= pHdr->nBacktraceSlots;
25118 sqlite3_mutex_enter(mem.mutex);
25119 if( pHdr->pPrev ){
25120 assert( pHdr->pPrev->pNext==pHdr );
25121 pHdr->pPrev->pNext = pHdr->pNext;
25122 }else{
25123 assert( mem.pFirst==pHdr );
25124 mem.pFirst = pHdr->pNext;
25125 }
25126 if( pHdr->pNext ){
25127 assert( pHdr->pNext->pPrev==pHdr );
25128 pHdr->pNext->pPrev = pHdr->pPrev;
25129 }else{
25130 assert( mem.pLast==pHdr );
25131 mem.pLast = pHdr->pPrev;
25132 }
25133 z = (char*)pBt;
25134 z -= pHdr->nTitle;
25135 adjustStats((int)pHdr->iSize, -1);
25136 randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
25137 (int)pHdr->iSize + sizeof(int) + pHdr->nTitle);
25138 free(z);
25139 sqlite3_mutex_leave(mem.mutex);
25140}
25141
25142/*
25143** Change the size of an existing memory allocation.
25144**
25145** For this debugging implementation, we *always* make a copy of the
25146** allocation into a new place in memory. In this way, if the
25147** higher level code is using pointer to the old allocation, it is
25148** much more likely to break and we are much more liking to find
25149** the error.
25150*/
25151static void *sqlite3MemRealloc(void *pPrior, int nByte){
25152 struct MemBlockHdr *pOldHdr;
25153 void *pNew;
25154 assert( mem.disallow==0 );
25155 assert( (nByte & 7)==0 ); /* EV: R-46199-30249 */
25156 pOldHdr = sqlite3MemsysGetHeader(pPrior);
25157 pNew = sqlite3MemMalloc(nByte);
25158 if( pNew ){
25159 memcpy(pNew, pPrior, (int)(nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize));
25160 if( nByte>pOldHdr->iSize ){
25161 randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize);
25162 }
25163 sqlite3MemFree(pPrior);
25164 }
25165 return pNew;
25166}
25167
25168/*
25169** Populate the low-level memory allocation function pointers in
25170** sqlite3GlobalConfig.m with pointers to the routines in this file.
25171*/
25172SQLITE_PRIVATE void sqlite3MemSetDefault(void){
25173 static const sqlite3_mem_methods defaultMethods = {
25174 sqlite3MemMalloc,
25175 sqlite3MemFree,
25176 sqlite3MemRealloc,
25177 sqlite3MemSize,
25178 sqlite3MemRoundup,
25179 sqlite3MemInit,
25180 sqlite3MemShutdown,
25181 0
25182 };
25183 sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
25184}
25185
25186/*
25187** Set the "type" of an allocation.
25188*/
25189SQLITE_PRIVATE void sqlite3MemdebugSetType(void *p, u8 eType){
25190 if( p && sqlite3GlobalConfig.m.xFree==sqlite3MemFree ){
25191 struct MemBlockHdr *pHdr;
25192 pHdr = sqlite3MemsysGetHeader(p);
25193 assert( pHdr->iForeGuard==FOREGUARD );
25194 pHdr->eType = eType;
25195 }
25196}
25197
25198/*
25199** Return TRUE if the mask of type in eType matches the type of the
25200** allocation p. Also return true if p==NULL.
25201**
25202** This routine is designed for use within an assert() statement, to
25203** verify the type of an allocation. For example:
25204**
25205** assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
25206*/
25207SQLITE_PRIVATE int sqlite3MemdebugHasType(const void *p, u8 eType){
25208 int rc = 1;
25209 if( p && sqlite3GlobalConfig.m.xFree==sqlite3MemFree ){
25210 struct MemBlockHdr *pHdr;
25211 pHdr = sqlite3MemsysGetHeader(p);
25212 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */
25213 if( (pHdr->eType&eType)==0 ){
25214 rc = 0;
25215 }
25216 }
25217 return rc;
25218}
25219
25220/*
25221** Return TRUE if the mask of type in eType matches no bits of the type of the
25222** allocation p. Also return true if p==NULL.
25223**
25224** This routine is designed for use within an assert() statement, to
25225** verify the type of an allocation. For example:
25226**
25227** assert( sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
25228*/
25229SQLITE_PRIVATE int sqlite3MemdebugNoType(const void *p, u8 eType){
25230 int rc = 1;
25231 if( p && sqlite3GlobalConfig.m.xFree==sqlite3MemFree ){
25232 struct MemBlockHdr *pHdr;
25233 pHdr = sqlite3MemsysGetHeader(p);
25234 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */
25235 if( (pHdr->eType&eType)!=0 ){
25236 rc = 0;
25237 }
25238 }
25239 return rc;
25240}
25241
25242/*
25243** Set the number of backtrace levels kept for each allocation.
25244** A value of zero turns off backtracing. The number is always rounded
25245** up to a multiple of 2.
25246*/
25247SQLITE_PRIVATE void sqlite3MemdebugBacktrace(int depth){
25248 if( depth<0 ){ depth = 0; }
25249 if( depth>20 ){ depth = 20; }
25250 depth = (depth+1)&0xfe;
25251 mem.nBacktrace = depth;
25252}
25253
25254SQLITE_PRIVATE void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int, void **)){
25255 mem.xBacktrace = xBacktrace;
25256}
25257
25258/*
25259** Set the title string for subsequent allocations.
25260*/
25261SQLITE_PRIVATE void sqlite3MemdebugSettitle(const char *zTitle){
25262 unsigned int n = sqlite3Strlen30(zTitle) + 1;
25263 sqlite3_mutex_enter(mem.mutex);
25264 if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
25265 memcpy(mem.zTitle, zTitle, n);
25266 mem.zTitle[n] = 0;
25267 mem.nTitle = ROUND8(n);
25268 sqlite3_mutex_leave(mem.mutex);
25269}
25270
25271SQLITE_PRIVATE void sqlite3MemdebugSync(){
25272 struct MemBlockHdr *pHdr;
25273 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
25274 void **pBt = (void**)pHdr;
25275 pBt -= pHdr->nBacktraceSlots;
25276 mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
25277 }
25278}
25279
25280/*
25281** Open the file indicated and write a log of all unfreed memory
25282** allocations into that log.
25283*/
25284SQLITE_PRIVATE void sqlite3MemdebugDump(const char *zFilename){
25285 FILE *out;
25286 struct MemBlockHdr *pHdr;
25287 void **pBt;
25288 int i;
25289 out = fopen(zFilename, "w");
25290 if( out==0 ){
25291 fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
25292 zFilename);
25293 return;
25294 }
25295 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
25296 char *z = (char*)pHdr;
25297 z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
25298 fprintf(out, "**** %lld bytes at %p from %s ****\n",
25299 pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
25300 if( pHdr->nBacktrace ){
25301 fflush(out);
25302 pBt = (void**)pHdr;
25303 pBt -= pHdr->nBacktraceSlots;
25304 backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
25305 fprintf(out, "\n");
25306 }
25307 }
25308 fprintf(out, "COUNTS:\n");
25309 for(i=0; i<NCSIZE-1; i++){
25310 if( mem.nAlloc[i] ){
25311 fprintf(out, " %5d: %10d %10d %10d\n",
25312 i*8, mem.nAlloc[i], mem.nCurrent[i], mem.mxCurrent[i]);
25313 }
25314 }
25315 if( mem.nAlloc[NCSIZE-1] ){
25316 fprintf(out, " %5d: %10d %10d %10d\n",
25317 NCSIZE*8-8, mem.nAlloc[NCSIZE-1],
25318 mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]);
25319 }
25320 fclose(out);
25321}
25322
25323/*
25324** Return the number of times sqlite3MemMalloc() has been called.
25325*/
25326SQLITE_PRIVATE int sqlite3MemdebugMallocCount(){
25327 int i;
25328 int nTotal = 0;
25329 for(i=0; i<NCSIZE; i++){
25330 nTotal += mem.nAlloc[i];
25331 }
25332 return nTotal;
25333}
25334
25335
25336#endif /* SQLITE_MEMDEBUG */
25337
25338/************** End of mem2.c ************************************************/
25339/************** Begin file mem3.c ********************************************/
25340/*
25341** 2007 October 14
25342**
25343** The author disclaims copyright to this source code. In place of
25344** a legal notice, here is a blessing:
25345**
25346** May you do good and not evil.
25347** May you find forgiveness for yourself and forgive others.
25348** May you share freely, never taking more than you give.
25349**
25350*************************************************************************
25351** This file contains the C functions that implement a memory
25352** allocation subsystem for use by SQLite.
25353**
25354** This version of the memory allocation subsystem omits all
25355** use of malloc(). The SQLite user supplies a block of memory
25356** before calling sqlite3_initialize() from which allocations
25357** are made and returned by the xMalloc() and xRealloc()
25358** implementations. Once sqlite3_initialize() has been called,
25359** the amount of memory available to SQLite is fixed and cannot
25360** be changed.
25361**
25362** This version of the memory allocation subsystem is included
25363** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
25364*/
25365/* #include "sqliteInt.h" */
25366
25367/*
25368** This version of the memory allocator is only built into the library
25369** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
25370** mean that the library will use a memory-pool by default, just that
25371** it is available. The mempool allocator is activated by calling
25372** sqlite3_config().
25373*/
25374#ifdef SQLITE_ENABLE_MEMSYS3
25375
25376/*
25377** Maximum size (in Mem3Blocks) of a "small" chunk.
25378*/
25379#define MX_SMALL 10
25380
25381
25382/*
25383** Number of freelist hash slots
25384*/
25385#define N_HASH 61
25386
25387/*
25388** A memory allocation (also called a "chunk") consists of two or
25389** more blocks where each block is 8 bytes. The first 8 bytes are
25390** a header that is not returned to the user.
25391**
25392** A chunk is two or more blocks that is either checked out or
25393** free. The first block has format u.hdr. u.hdr.size4x is 4 times the
25394** size of the allocation in blocks if the allocation is free.
25395** The u.hdr.size4x&1 bit is true if the chunk is checked out and
25396** false if the chunk is on the freelist. The u.hdr.size4x&2 bit
25397** is true if the previous chunk is checked out and false if the
25398** previous chunk is free. The u.hdr.prevSize field is the size of
25399** the previous chunk in blocks if the previous chunk is on the
25400** freelist. If the previous chunk is checked out, then
25401** u.hdr.prevSize can be part of the data for that chunk and should
25402** not be read or written.
25403**
25404** We often identify a chunk by its index in mem3.aPool[]. When
25405** this is done, the chunk index refers to the second block of
25406** the chunk. In this way, the first chunk has an index of 1.
25407** A chunk index of 0 means "no such chunk" and is the equivalent
25408** of a NULL pointer.
25409**
25410** The second block of free chunks is of the form u.list. The
25411** two fields form a double-linked list of chunks of related sizes.
25412** Pointers to the head of the list are stored in mem3.aiSmall[]
25413** for smaller chunks and mem3.aiHash[] for larger chunks.
25414**
25415** The second block of a chunk is user data if the chunk is checked
25416** out. If a chunk is checked out, the user data may extend into
25417** the u.hdr.prevSize value of the following chunk.
25418*/
25419typedef struct Mem3Block Mem3Block;
25420struct Mem3Block {
25421 union {
25422 struct {
25423 u32 prevSize; /* Size of previous chunk in Mem3Block elements */
25424 u32 size4x; /* 4x the size of current chunk in Mem3Block elements */
25425 } hdr;
25426 struct {
25427 u32 next; /* Index in mem3.aPool[] of next free chunk */
25428 u32 prev; /* Index in mem3.aPool[] of previous free chunk */
25429 } list;
25430 } u;
25431};
25432
25433/*
25434** All of the static variables used by this module are collected
25435** into a single structure named "mem3". This is to keep the
25436** static variables organized and to reduce namespace pollution
25437** when this module is combined with other in the amalgamation.
25438*/
25439static SQLITE_WSD struct Mem3Global {
25440 /*
25441 ** Memory available for allocation. nPool is the size of the array
25442 ** (in Mem3Blocks) pointed to by aPool less 2.
25443 */
25444 u32 nPool;
25445 Mem3Block *aPool;
25446
25447 /*
25448 ** True if we are evaluating an out-of-memory callback.
25449 */
25450 int alarmBusy;
25451
25452 /*
25453 ** Mutex to control access to the memory allocation subsystem.
25454 */
25455 sqlite3_mutex *mutex;
25456
25457 /*
25458 ** The minimum amount of free space that we have seen.
25459 */
25460 u32 mnKeyBlk;
25461
25462 /*
25463 ** iKeyBlk is the index of the key chunk. Most new allocations
25464 ** occur off of this chunk. szKeyBlk is the size (in Mem3Blocks)
25465 ** of the current key chunk. iKeyBlk is 0 if there is no key chunk.
25466 ** The key chunk is not in either the aiHash[] or aiSmall[].
25467 */
25468 u32 iKeyBlk;
25469 u32 szKeyBlk;
25470
25471 /*
25472 ** Array of lists of free blocks according to the block size
25473 ** for smaller chunks, or a hash on the block size for larger
25474 ** chunks.
25475 */
25476 u32 aiSmall[MX_SMALL-1]; /* For sizes 2 through MX_SMALL, inclusive */
25477 u32 aiHash[N_HASH]; /* For sizes MX_SMALL+1 and larger */
25478} mem3 = { 97535575 };
25479
25480#define mem3 GLOBAL(struct Mem3Global, mem3)
25481
25482/*
25483** Unlink the chunk at mem3.aPool[i] from list it is currently
25484** on. *pRoot is the list that i is a member of.
25485*/
25486static void memsys3UnlinkFromList(u32 i, u32 *pRoot){
25487 u32 next = mem3.aPool[i].u.list.next;
25488 u32 prev = mem3.aPool[i].u.list.prev;
25489 assert( sqlite3_mutex_held(mem3.mutex) );
25490 if( prev==0 ){
25491 *pRoot = next;
25492 }else{
25493 mem3.aPool[prev].u.list.next = next;
25494 }
25495 if( next ){
25496 mem3.aPool[next].u.list.prev = prev;
25497 }
25498 mem3.aPool[i].u.list.next = 0;
25499 mem3.aPool[i].u.list.prev = 0;
25500}
25501
25502/*
25503** Unlink the chunk at index i from
25504** whatever list is currently a member of.
25505*/
25506static void memsys3Unlink(u32 i){
25507 u32 size, hash;
25508 assert( sqlite3_mutex_held(mem3.mutex) );
25509 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
25510 assert( i>=1 );
25511 size = mem3.aPool[i-1].u.hdr.size4x/4;
25512 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
25513 assert( size>=2 );
25514 if( size <= MX_SMALL ){
25515 memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]);
25516 }else{
25517 hash = size % N_HASH;
25518 memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
25519 }
25520}
25521
25522/*
25523** Link the chunk at mem3.aPool[i] so that is on the list rooted
25524** at *pRoot.
25525*/
25526static void memsys3LinkIntoList(u32 i, u32 *pRoot){
25527 assert( sqlite3_mutex_held(mem3.mutex) );
25528 mem3.aPool[i].u.list.next = *pRoot;
25529 mem3.aPool[i].u.list.prev = 0;
25530 if( *pRoot ){
25531 mem3.aPool[*pRoot].u.list.prev = i;
25532 }
25533 *pRoot = i;
25534}
25535
25536/*
25537** Link the chunk at index i into either the appropriate
25538** small chunk list, or into the large chunk hash table.
25539*/
25540static void memsys3Link(u32 i){
25541 u32 size, hash;
25542 assert( sqlite3_mutex_held(mem3.mutex) );
25543 assert( i>=1 );
25544 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
25545 size = mem3.aPool[i-1].u.hdr.size4x/4;
25546 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
25547 assert( size>=2 );
25548 if( size <= MX_SMALL ){
25549 memsys3LinkIntoList(i, &mem3.aiSmall[size-2]);
25550 }else{
25551 hash = size % N_HASH;
25552 memsys3LinkIntoList(i, &mem3.aiHash[hash]);
25553 }
25554}
25555
25556/*
25557** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
25558** will already be held (obtained by code in malloc.c) if
25559** sqlite3GlobalConfig.bMemStat is true.
25560*/
25561static void memsys3Enter(void){
25562 if( sqlite3GlobalConfig.bMemstat==0 && mem3.mutex==0 ){
25563 mem3.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
25564 }
25565 sqlite3_mutex_enter(mem3.mutex);
25566}
25567static void memsys3Leave(void){
25568 sqlite3_mutex_leave(mem3.mutex);
25569}
25570
25571/*
25572** Called when we are unable to satisfy an allocation of nBytes.
25573*/
25574static void memsys3OutOfMemory(int nByte){
25575 if( !mem3.alarmBusy ){
25576 mem3.alarmBusy = 1;
25577 assert( sqlite3_mutex_held(mem3.mutex) );
25578 sqlite3_mutex_leave(mem3.mutex);
25579 sqlite3_release_memory(nByte);
25580 sqlite3_mutex_enter(mem3.mutex);
25581 mem3.alarmBusy = 0;
25582 }
25583}
25584
25585
25586/*
25587** Chunk i is a free chunk that has been unlinked. Adjust its
25588** size parameters for check-out and return a pointer to the
25589** user portion of the chunk.
25590*/
25591static void *memsys3Checkout(u32 i, u32 nBlock){
25592 u32 x;
25593 assert( sqlite3_mutex_held(mem3.mutex) );
25594 assert( i>=1 );
25595 assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock );
25596 assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock );
25597 x = mem3.aPool[i-1].u.hdr.size4x;
25598 mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2);
25599 mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock;
25600 mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2;
25601 return &mem3.aPool[i];
25602}
25603
25604/*
25605** Carve a piece off of the end of the mem3.iKeyBlk free chunk.
25606** Return a pointer to the new allocation. Or, if the key chunk
25607** is not large enough, return 0.
25608*/
25609static void *memsys3FromKeyBlk(u32 nBlock){
25610 assert( sqlite3_mutex_held(mem3.mutex) );
25611 assert( mem3.szKeyBlk>=nBlock );
25612 if( nBlock>=mem3.szKeyBlk-1 ){
25613 /* Use the entire key chunk */
25614 void *p = memsys3Checkout(mem3.iKeyBlk, mem3.szKeyBlk);
25615 mem3.iKeyBlk = 0;
25616 mem3.szKeyBlk = 0;
25617 mem3.mnKeyBlk = 0;
25618 return p;
25619 }else{
25620 /* Split the key block. Return the tail. */
25621 u32 newi, x;
25622 newi = mem3.iKeyBlk + mem3.szKeyBlk - nBlock;
25623 assert( newi > mem3.iKeyBlk+1 );
25624 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = nBlock;
25625 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x |= 2;
25626 mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1;
25627 mem3.szKeyBlk -= nBlock;
25628 mem3.aPool[newi-1].u.hdr.prevSize = mem3.szKeyBlk;
25629 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2;
25630 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x;
25631 if( mem3.szKeyBlk < mem3.mnKeyBlk ){
25632 mem3.mnKeyBlk = mem3.szKeyBlk;
25633 }
25634 return (void*)&mem3.aPool[newi];
25635 }
25636}
25637
25638/*
25639** *pRoot is the head of a list of free chunks of the same size
25640** or same size hash. In other words, *pRoot is an entry in either
25641** mem3.aiSmall[] or mem3.aiHash[].
25642**
25643** This routine examines all entries on the given list and tries
25644** to coalesce each entries with adjacent free chunks.
25645**
25646** If it sees a chunk that is larger than mem3.iKeyBlk, it replaces
25647** the current mem3.iKeyBlk with the new larger chunk. In order for
25648** this mem3.iKeyBlk replacement to work, the key chunk must be
25649** linked into the hash tables. That is not the normal state of
25650** affairs, of course. The calling routine must link the key
25651** chunk before invoking this routine, then must unlink the (possibly
25652** changed) key chunk once this routine has finished.
25653*/
25654static void memsys3Merge(u32 *pRoot){
25655 u32 iNext, prev, size, i, x;
25656
25657 assert( sqlite3_mutex_held(mem3.mutex) );
25658 for(i=*pRoot; i>0; i=iNext){
25659 iNext = mem3.aPool[i].u.list.next;
25660 size = mem3.aPool[i-1].u.hdr.size4x;
25661 assert( (size&1)==0 );
25662 if( (size&2)==0 ){
25663 memsys3UnlinkFromList(i, pRoot);
25664 assert( i > mem3.aPool[i-1].u.hdr.prevSize );
25665 prev = i - mem3.aPool[i-1].u.hdr.prevSize;
25666 if( prev==iNext ){
25667 iNext = mem3.aPool[prev].u.list.next;
25668 }
25669 memsys3Unlink(prev);
25670 size = i + size/4 - prev;
25671 x = mem3.aPool[prev-1].u.hdr.size4x & 2;
25672 mem3.aPool[prev-1].u.hdr.size4x = size*4 | x;
25673 mem3.aPool[prev+size-1].u.hdr.prevSize = size;
25674 memsys3Link(prev);
25675 i = prev;
25676 }else{
25677 size /= 4;
25678 }
25679 if( size>mem3.szKeyBlk ){
25680 mem3.iKeyBlk = i;
25681 mem3.szKeyBlk = size;
25682 }
25683 }
25684}
25685
25686/*
25687** Return a block of memory of at least nBytes in size.
25688** Return NULL if unable.
25689**
25690** This function assumes that the necessary mutexes, if any, are
25691** already held by the caller. Hence "Unsafe".
25692*/
25693static void *memsys3MallocUnsafe(int nByte){
25694 u32 i;
25695 u32 nBlock;
25696 u32 toFree;
25697
25698 assert( sqlite3_mutex_held(mem3.mutex) );
25699 assert( sizeof(Mem3Block)==8 );
25700 if( nByte<=12 ){
25701 nBlock = 2;
25702 }else{
25703 nBlock = (nByte + 11)/8;
25704 }
25705 assert( nBlock>=2 );
25706
25707 /* STEP 1:
25708 ** Look for an entry of the correct size in either the small
25709 ** chunk table or in the large chunk hash table. This is
25710 ** successful most of the time (about 9 times out of 10).
25711 */
25712 if( nBlock <= MX_SMALL ){
25713 i = mem3.aiSmall[nBlock-2];
25714 if( i>0 ){
25715 memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]);
25716 return memsys3Checkout(i, nBlock);
25717 }
25718 }else{
25719 int hash = nBlock % N_HASH;
25720 for(i=mem3.aiHash[hash]; i>0; i=mem3.aPool[i].u.list.next){
25721 if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){
25722 memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
25723 return memsys3Checkout(i, nBlock);
25724 }
25725 }
25726 }
25727
25728 /* STEP 2:
25729 ** Try to satisfy the allocation by carving a piece off of the end
25730 ** of the key chunk. This step usually works if step 1 fails.
25731 */
25732 if( mem3.szKeyBlk>=nBlock ){
25733 return memsys3FromKeyBlk(nBlock);
25734 }
25735
25736
25737 /* STEP 3:
25738 ** Loop through the entire memory pool. Coalesce adjacent free
25739 ** chunks. Recompute the key chunk as the largest free chunk.
25740 ** Then try again to satisfy the allocation by carving a piece off
25741 ** of the end of the key chunk. This step happens very
25742 ** rarely (we hope!)
25743 */
25744 for(toFree=nBlock*16; toFree<(mem3.nPool*16); toFree *= 2){
25745 memsys3OutOfMemory(toFree);
25746 if( mem3.iKeyBlk ){
25747 memsys3Link(mem3.iKeyBlk);
25748 mem3.iKeyBlk = 0;
25749 mem3.szKeyBlk = 0;
25750 }
25751 for(i=0; i<N_HASH; i++){
25752 memsys3Merge(&mem3.aiHash[i]);
25753 }
25754 for(i=0; i<MX_SMALL-1; i++){
25755 memsys3Merge(&mem3.aiSmall[i]);
25756 }
25757 if( mem3.szKeyBlk ){
25758 memsys3Unlink(mem3.iKeyBlk);
25759 if( mem3.szKeyBlk>=nBlock ){
25760 return memsys3FromKeyBlk(nBlock);
25761 }
25762 }
25763 }
25764
25765 /* If none of the above worked, then we fail. */
25766 return 0;
25767}
25768
25769/*
25770** Free an outstanding memory allocation.
25771**
25772** This function assumes that the necessary mutexes, if any, are
25773** already held by the caller. Hence "Unsafe".
25774*/
25775static void memsys3FreeUnsafe(void *pOld){
25776 Mem3Block *p = (Mem3Block*)pOld;
25777 int i;
25778 u32 size, x;
25779 assert( sqlite3_mutex_held(mem3.mutex) );
25780 assert( p>mem3.aPool && p<&mem3.aPool[mem3.nPool] );
25781 i = p - mem3.aPool;
25782 assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 );
25783 size = mem3.aPool[i-1].u.hdr.size4x/4;
25784 assert( i+size<=mem3.nPool+1 );
25785 mem3.aPool[i-1].u.hdr.size4x &= ~1;
25786 mem3.aPool[i+size-1].u.hdr.prevSize = size;
25787 mem3.aPool[i+size-1].u.hdr.size4x &= ~2;
25788 memsys3Link(i);
25789
25790 /* Try to expand the key using the newly freed chunk */
25791 if( mem3.iKeyBlk ){
25792 while( (mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x&2)==0 ){
25793 size = mem3.aPool[mem3.iKeyBlk-1].u.hdr.prevSize;
25794 mem3.iKeyBlk -= size;
25795 mem3.szKeyBlk += size;
25796 memsys3Unlink(mem3.iKeyBlk);
25797 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2;
25798 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x;
25799 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = mem3.szKeyBlk;
25800 }
25801 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2;
25802 while( (mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x&1)==0 ){
25803 memsys3Unlink(mem3.iKeyBlk+mem3.szKeyBlk);
25804 mem3.szKeyBlk += mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x/4;
25805 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x;
25806 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = mem3.szKeyBlk;
25807 }
25808 }
25809}
25810
25811/*
25812** Return the size of an outstanding allocation, in bytes. The
25813** size returned omits the 8-byte header overhead. This only
25814** works for chunks that are currently checked out.
25815*/
25816static int memsys3Size(void *p){
25817 Mem3Block *pBlock;
25818 assert( p!=0 );
25819 pBlock = (Mem3Block*)p;
25820 assert( (pBlock[-1].u.hdr.size4x&1)!=0 );
25821 return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
25822}
25823
25824/*
25825** Round up a request size to the next valid allocation size.
25826*/
25827static int memsys3Roundup(int n){
25828 if( n<=12 ){
25829 return 12;
25830 }else{
25831 return ((n+11)&~7) - 4;
25832 }
25833}
25834
25835/*
25836** Allocate nBytes of memory.
25837*/
25838static void *memsys3Malloc(int nBytes){
25839 sqlite3_int64 *p;
25840 assert( nBytes>0 ); /* malloc.c filters out 0 byte requests */
25841 memsys3Enter();
25842 p = memsys3MallocUnsafe(nBytes);
25843 memsys3Leave();
25844 return (void*)p;
25845}
25846
25847/*
25848** Free memory.
25849*/
25850static void memsys3Free(void *pPrior){
25851 assert( pPrior );
25852 memsys3Enter();
25853 memsys3FreeUnsafe(pPrior);
25854 memsys3Leave();
25855}
25856
25857/*
25858** Change the size of an existing memory allocation
25859*/
25860static void *memsys3Realloc(void *pPrior, int nBytes){
25861 int nOld;
25862 void *p;
25863 if( pPrior==0 ){
25864 return sqlite3_malloc(nBytes);
25865 }
25866 if( nBytes<=0 ){
25867 sqlite3_free(pPrior);
25868 return 0;
25869 }
25870 nOld = memsys3Size(pPrior);
25871 if( nBytes<=nOld && nBytes>=nOld-128 ){
25872 return pPrior;
25873 }
25874 memsys3Enter();
25875 p = memsys3MallocUnsafe(nBytes);
25876 if( p ){
25877 if( nOld<nBytes ){
25878 memcpy(p, pPrior, nOld);
25879 }else{
25880 memcpy(p, pPrior, nBytes);
25881 }
25882 memsys3FreeUnsafe(pPrior);
25883 }
25884 memsys3Leave();
25885 return p;
25886}
25887
25888/*
25889** Initialize this module.
25890*/
25891static int memsys3Init(void *NotUsed){
25892 UNUSED_PARAMETER(NotUsed);
25893 if( !sqlite3GlobalConfig.pHeap ){
25894 return SQLITE_ERROR;
25895 }
25896
25897 /* Store a pointer to the memory block in global structure mem3. */
25898 assert( sizeof(Mem3Block)==8 );
25899 mem3.aPool = (Mem3Block *)sqlite3GlobalConfig.pHeap;
25900 mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2;
25901
25902 /* Initialize the key block. */
25903 mem3.szKeyBlk = mem3.nPool;
25904 mem3.mnKeyBlk = mem3.szKeyBlk;
25905 mem3.iKeyBlk = 1;
25906 mem3.aPool[0].u.hdr.size4x = (mem3.szKeyBlk<<2) + 2;
25907 mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool;
25908 mem3.aPool[mem3.nPool].u.hdr.size4x = 1;
25909
25910 return SQLITE_OK;
25911}
25912
25913/*
25914** Deinitialize this module.
25915*/
25916static void memsys3Shutdown(void *NotUsed){
25917 UNUSED_PARAMETER(NotUsed);
25918 mem3.mutex = 0;
25919 return;
25920}
25921
25922
25923
25924/*
25925** Open the file indicated and write a log of all unfreed memory
25926** allocations into that log.
25927*/
25928SQLITE_PRIVATE void sqlite3Memsys3Dump(const char *zFilename){
25929#ifdef SQLITE_DEBUG
25930 FILE *out;
25931 u32 i, j;
25932 u32 size;
25933 if( zFilename==0 || zFilename[0]==0 ){
25934 out = stdout;
25935 }else{
25936 out = fopen(zFilename, "w");
25937 if( out==0 ){
25938 fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
25939 zFilename);
25940 return;
25941 }
25942 }
25943 memsys3Enter();
25944 fprintf(out, "CHUNKS:\n");
25945 for(i=1; i<=mem3.nPool; i+=size/4){
25946 size = mem3.aPool[i-1].u.hdr.size4x;
25947 if( size/4<=1 ){
25948 fprintf(out, "%p size error\n", &mem3.aPool[i]);
25949 assert( 0 );
25950 break;
25951 }
25952 if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){
25953 fprintf(out, "%p tail size does not match\n", &mem3.aPool[i]);
25954 assert( 0 );
25955 break;
25956 }
25957 if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){
25958 fprintf(out, "%p tail checkout bit is incorrect\n", &mem3.aPool[i]);
25959 assert( 0 );
25960 break;
25961 }
25962 if( size&1 ){
25963 fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8);
25964 }else{
25965 fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8,
25966 i==mem3.iKeyBlk ? " **key**" : "");
25967 }
25968 }
25969 for(i=0; i<MX_SMALL-1; i++){
25970 if( mem3.aiSmall[i]==0 ) continue;
25971 fprintf(out, "small(%2d):", i);
25972 for(j = mem3.aiSmall[i]; j>0; j=mem3.aPool[j].u.list.next){
25973 fprintf(out, " %p(%d)", &mem3.aPool[j],
25974 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
25975 }
25976 fprintf(out, "\n");
25977 }
25978 for(i=0; i<N_HASH; i++){
25979 if( mem3.aiHash[i]==0 ) continue;
25980 fprintf(out, "hash(%2d):", i);
25981 for(j = mem3.aiHash[i]; j>0; j=mem3.aPool[j].u.list.next){
25982 fprintf(out, " %p(%d)", &mem3.aPool[j],
25983 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
25984 }
25985 fprintf(out, "\n");
25986 }
25987 fprintf(out, "key=%d\n", mem3.iKeyBlk);
25988 fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szKeyBlk*8);
25989 fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnKeyBlk*8);
25990 sqlite3_mutex_leave(mem3.mutex);
25991 if( out==stdout ){
25992 fflush(stdout);
25993 }else{
25994 fclose(out);
25995 }
25996#else
25997 UNUSED_PARAMETER(zFilename);
25998#endif
25999}
26000
26001/*
26002** This routine is the only routine in this file with external
26003** linkage.
26004**
26005** Populate the low-level memory allocation function pointers in
26006** sqlite3GlobalConfig.m with pointers to the routines in this file. The
26007** arguments specify the block of memory to manage.
26008**
26009** This routine is only called by sqlite3_config(), and therefore
26010** is not required to be threadsafe (it is not).
26011*/
26012SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){
26013 static const sqlite3_mem_methods mempoolMethods = {
26014 memsys3Malloc,
26015 memsys3Free,
26016 memsys3Realloc,
26017 memsys3Size,
26018 memsys3Roundup,
26019 memsys3Init,
26020 memsys3Shutdown,
26021 0
26022 };
26023 return &mempoolMethods;
26024}
26025
26026#endif /* SQLITE_ENABLE_MEMSYS3 */
26027
26028/************** End of mem3.c ************************************************/
26029/************** Begin file mem5.c ********************************************/
26030/*
26031** 2007 October 14
26032**
26033** The author disclaims copyright to this source code. In place of
26034** a legal notice, here is a blessing:
26035**
26036** May you do good and not evil.
26037** May you find forgiveness for yourself and forgive others.
26038** May you share freely, never taking more than you give.
26039**
26040*************************************************************************
26041** This file contains the C functions that implement a memory
26042** allocation subsystem for use by SQLite.
26043**
26044** This version of the memory allocation subsystem omits all
26045** use of malloc(). The application gives SQLite a block of memory
26046** before calling sqlite3_initialize() from which allocations
26047** are made and returned by the xMalloc() and xRealloc()
26048** implementations. Once sqlite3_initialize() has been called,
26049** the amount of memory available to SQLite is fixed and cannot
26050** be changed.
26051**
26052** This version of the memory allocation subsystem is included
26053** in the build only if SQLITE_ENABLE_MEMSYS5 is defined.
26054**
26055** This memory allocator uses the following algorithm:
26056**
26057** 1. All memory allocation sizes are rounded up to a power of 2.
26058**
26059** 2. If two adjacent free blocks are the halves of a larger block,
26060** then the two blocks are coalesced into the single larger block.
26061**
26062** 3. New memory is allocated from the first available free block.
26063**
26064** This algorithm is described in: J. M. Robson. "Bounds for Some Functions
26065** Concerning Dynamic Storage Allocation". Journal of the Association for
26066** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
26067**
26068** Let n be the size of the largest allocation divided by the minimum
26069** allocation size (after rounding all sizes up to a power of 2.) Let M
26070** be the maximum amount of memory ever outstanding at one time. Let
26071** N be the total amount of memory available for allocation. Robson
26072** proved that this memory allocator will never breakdown due to
26073** fragmentation as long as the following constraint holds:
26074**
26075** N >= M*(1 + log2(n)/2) - n + 1
26076**
26077** The sqlite3_status() logic tracks the maximum values of n and M so
26078** that an application can, at any time, verify this constraint.
26079*/
26080/* #include "sqliteInt.h" */
26081
26082/*
26083** This version of the memory allocator is used only when
26084** SQLITE_ENABLE_MEMSYS5 is defined.
26085*/
26086#ifdef SQLITE_ENABLE_MEMSYS5
26087
26088/*
26089** A minimum allocation is an instance of the following structure.
26090** Larger allocations are an array of these structures where the
26091** size of the array is a power of 2.
26092**
26093** The size of this object must be a power of two. That fact is
26094** verified in memsys5Init().
26095*/
26096typedef struct Mem5Link Mem5Link;
26097struct Mem5Link {
26098 int next; /* Index of next free chunk */
26099 int prev; /* Index of previous free chunk */
26100};
26101
26102/*
26103** Maximum size of any allocation is ((1<<LOGMAX)*mem5.szAtom). Since
26104** mem5.szAtom is always at least 8 and 32-bit integers are used,
26105** it is not actually possible to reach this limit.
26106*/
26107#define LOGMAX 30
26108
26109/*
26110** Masks used for mem5.aCtrl[] elements.
26111*/
26112#define CTRL_LOGSIZE 0x1f /* Log2 Size of this block */
26113#define CTRL_FREE 0x20 /* True if not checked out */
26114
26115/*
26116** All of the static variables used by this module are collected
26117** into a single structure named "mem5". This is to keep the
26118** static variables organized and to reduce namespace pollution
26119** when this module is combined with other in the amalgamation.
26120*/
26121static SQLITE_WSD struct Mem5Global {
26122 /*
26123 ** Memory available for allocation
26124 */
26125 int szAtom; /* Smallest possible allocation in bytes */
26126 int nBlock; /* Number of szAtom sized blocks in zPool */
26127 u8 *zPool; /* Memory available to be allocated */
26128
26129 /*
26130 ** Mutex to control access to the memory allocation subsystem.
26131 */
26132 sqlite3_mutex *mutex;
26133
26134#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
26135 /*
26136 ** Performance statistics
26137 */
26138 u64 nAlloc; /* Total number of calls to malloc */
26139 u64 totalAlloc; /* Total of all malloc calls - includes internal frag */
26140 u64 totalExcess; /* Total internal fragmentation */
26141 u32 currentOut; /* Current checkout, including internal fragmentation */
26142 u32 currentCount; /* Current number of distinct checkouts */
26143 u32 maxOut; /* Maximum instantaneous currentOut */
26144 u32 maxCount; /* Maximum instantaneous currentCount */
26145 u32 maxRequest; /* Largest allocation (exclusive of internal frag) */
26146#endif
26147
26148 /*
26149 ** Lists of free blocks. aiFreelist[0] is a list of free blocks of
26150 ** size mem5.szAtom. aiFreelist[1] holds blocks of size szAtom*2.
26151 ** aiFreelist[2] holds free blocks of size szAtom*4. And so forth.
26152 */
26153 int aiFreelist[LOGMAX+1];
26154
26155 /*
26156 ** Space for tracking which blocks are checked out and the size
26157 ** of each block. One byte per block.
26158 */
26159 u8 *aCtrl;
26160
26161} mem5;
26162
26163/*
26164** Access the static variable through a macro for SQLITE_OMIT_WSD.
26165*/
26166#define mem5 GLOBAL(struct Mem5Global, mem5)
26167
26168/*
26169** Assuming mem5.zPool is divided up into an array of Mem5Link
26170** structures, return a pointer to the idx-th such link.
26171*/
26172#define MEM5LINK(idx) ((Mem5Link *)(&mem5.zPool[(idx)*mem5.szAtom]))
26173
26174/*
26175** Unlink the chunk at mem5.aPool[i] from list it is currently
26176** on. It should be found on mem5.aiFreelist[iLogsize].
26177*/
26178static void memsys5Unlink(int i, int iLogsize){
26179 int next, prev;
26180 assert( i>=0 && i<mem5.nBlock );
26181 assert( iLogsize>=0 && iLogsize<=LOGMAX );
26182 assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
26183
26184 next = MEM5LINK(i)->next;
26185 prev = MEM5LINK(i)->prev;
26186 if( prev<0 ){
26187 mem5.aiFreelist[iLogsize] = next;
26188 }else{
26189 MEM5LINK(prev)->next = next;
26190 }
26191 if( next>=0 ){
26192 MEM5LINK(next)->prev = prev;
26193 }
26194}
26195
26196/*
26197** Link the chunk at mem5.aPool[i] so that is on the iLogsize
26198** free list.
26199*/
26200static void memsys5Link(int i, int iLogsize){
26201 int x;
26202 assert( sqlite3_mutex_held(mem5.mutex) );
26203 assert( i>=0 && i<mem5.nBlock );
26204 assert( iLogsize>=0 && iLogsize<=LOGMAX );
26205 assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
26206
26207 x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize];
26208 MEM5LINK(i)->prev = -1;
26209 if( x>=0 ){
26210 assert( x<mem5.nBlock );
26211 MEM5LINK(x)->prev = i;
26212 }
26213 mem5.aiFreelist[iLogsize] = i;
26214}
26215
26216/*
26217** Obtain or release the mutex needed to access global data structures.
26218*/
26219static void memsys5Enter(void){
26220 sqlite3_mutex_enter(mem5.mutex);
26221}
26222static void memsys5Leave(void){
26223 sqlite3_mutex_leave(mem5.mutex);
26224}
26225
26226/*
26227** Return the size of an outstanding allocation, in bytes.
26228** This only works for chunks that are currently checked out.
26229*/
26230static int memsys5Size(void *p){
26231 int iSize, i;
26232 assert( p!=0 );
26233 i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom);
26234 assert( i>=0 && i<mem5.nBlock );
26235 iSize = mem5.szAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE));
26236 return iSize;
26237}
26238
26239/*
26240** Return a block of memory of at least nBytes in size.
26241** Return NULL if unable. Return NULL if nBytes==0.
26242**
26243** The caller guarantees that nByte is positive.
26244**
26245** The caller has obtained a mutex prior to invoking this
26246** routine so there is never any chance that two or more
26247** threads can be in this routine at the same time.
26248*/
26249static void *memsys5MallocUnsafe(int nByte){
26250 int i; /* Index of a mem5.aPool[] slot */
26251 int iBin; /* Index into mem5.aiFreelist[] */
26252 int iFullSz; /* Size of allocation rounded up to power of 2 */
26253 int iLogsize; /* Log2 of iFullSz/POW2_MIN */
26254
26255 /* nByte must be a positive */
26256 assert( nByte>0 );
26257
26258 /* No more than 1GiB per allocation */
26259 if( nByte > 0x40000000 ) return 0;
26260
26261#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
26262 /* Keep track of the maximum allocation request. Even unfulfilled
26263 ** requests are counted */
26264 if( (u32)nByte>mem5.maxRequest ){
26265 mem5.maxRequest = nByte;
26266 }
26267#endif
26268
26269
26270 /* Round nByte up to the next valid power of two */
26271 for(iFullSz=mem5.szAtom,iLogsize=0; iFullSz<nByte; iFullSz*=2,iLogsize++){}
26272
26273 /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
26274 ** block. If not, then split a block of the next larger power of
26275 ** two in order to create a new free block of size iLogsize.
26276 */
26277 for(iBin=iLogsize; iBin<=LOGMAX && mem5.aiFreelist[iBin]<0; iBin++){}
26278 if( iBin>LOGMAX ){
26279 testcase( sqlite3GlobalConfig.xLog!=0 );
26280 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes", nByte);
26281 return 0;
26282 }
26283 i = mem5.aiFreelist[iBin];
26284 memsys5Unlink(i, iBin);
26285 while( iBin>iLogsize ){
26286 int newSize;
26287
26288 iBin--;
26289 newSize = 1 << iBin;
26290 mem5.aCtrl[i+newSize] = CTRL_FREE | iBin;
26291 memsys5Link(i+newSize, iBin);
26292 }
26293 mem5.aCtrl[i] = iLogsize;
26294
26295#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
26296 /* Update allocator performance statistics. */
26297 mem5.nAlloc++;
26298 mem5.totalAlloc += iFullSz;
26299 mem5.totalExcess += iFullSz - nByte;
26300 mem5.currentCount++;
26301 mem5.currentOut += iFullSz;
26302 if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount;
26303 if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut;
26304#endif
26305
26306#ifdef SQLITE_DEBUG
26307 /* Make sure the allocated memory does not assume that it is set to zero
26308 ** or retains a value from a previous allocation */
26309 memset(&mem5.zPool[i*mem5.szAtom], 0xAA, iFullSz);
26310#endif
26311
26312 /* Return a pointer to the allocated memory. */
26313 return (void*)&mem5.zPool[i*mem5.szAtom];
26314}
26315
26316/*
26317** Free an outstanding memory allocation.
26318*/
26319static void memsys5FreeUnsafe(void *pOld){
26320 u32 size, iLogsize;
26321 int iBlock;
26322
26323 /* Set iBlock to the index of the block pointed to by pOld in
26324 ** the array of mem5.szAtom byte blocks pointed to by mem5.zPool.
26325 */
26326 iBlock = (int)(((u8 *)pOld-mem5.zPool)/mem5.szAtom);
26327
26328 /* Check that the pointer pOld points to a valid, non-free block. */
26329 assert( iBlock>=0 && iBlock<mem5.nBlock );
26330 assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 );
26331 assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 );
26332
26333 iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE;
26334 size = 1<<iLogsize;
26335 assert( iBlock+size-1<(u32)mem5.nBlock );
26336
26337 mem5.aCtrl[iBlock] |= CTRL_FREE;
26338 mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
26339
26340#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
26341 assert( mem5.currentCount>0 );
26342 assert( mem5.currentOut>=(size*mem5.szAtom) );
26343 mem5.currentCount--;
26344 mem5.currentOut -= size*mem5.szAtom;
26345 assert( mem5.currentOut>0 || mem5.currentCount==0 );
26346 assert( mem5.currentCount>0 || mem5.currentOut==0 );
26347#endif
26348
26349 mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
26350 while( ALWAYS(iLogsize<LOGMAX) ){
26351 int iBuddy;
26352 if( (iBlock>>iLogsize) & 1 ){
26353 iBuddy = iBlock - size;
26354 assert( iBuddy>=0 );
26355 }else{
26356 iBuddy = iBlock + size;
26357 if( iBuddy>=mem5.nBlock ) break;
26358 }
26359 if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
26360 memsys5Unlink(iBuddy, iLogsize);
26361 iLogsize++;
26362 if( iBuddy<iBlock ){
26363 mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize;
26364 mem5.aCtrl[iBlock] = 0;
26365 iBlock = iBuddy;
26366 }else{
26367 mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
26368 mem5.aCtrl[iBuddy] = 0;
26369 }
26370 size *= 2;
26371 }
26372
26373#ifdef SQLITE_DEBUG
26374 /* Overwrite freed memory with the 0x55 bit pattern to verify that it is
26375 ** not used after being freed */
26376 memset(&mem5.zPool[iBlock*mem5.szAtom], 0x55, size);
26377#endif
26378
26379 memsys5Link(iBlock, iLogsize);
26380}
26381
26382/*
26383** Allocate nBytes of memory.
26384*/
26385static void *memsys5Malloc(int nBytes){
26386 sqlite3_int64 *p = 0;
26387 if( nBytes>0 ){
26388 memsys5Enter();
26389 p = memsys5MallocUnsafe(nBytes);
26390 memsys5Leave();
26391 }
26392 return (void*)p;
26393}
26394
26395/*
26396** Free memory.
26397**
26398** The outer layer memory allocator prevents this routine from
26399** being called with pPrior==0.
26400*/
26401static void memsys5Free(void *pPrior){
26402 assert( pPrior!=0 );
26403 memsys5Enter();
26404 memsys5FreeUnsafe(pPrior);
26405 memsys5Leave();
26406}
26407
26408/*
26409** Change the size of an existing memory allocation.
26410**
26411** The outer layer memory allocator prevents this routine from
26412** being called with pPrior==0.
26413**
26414** nBytes is always a value obtained from a prior call to
26415** memsys5Round(). Hence nBytes is always a non-negative power
26416** of two. If nBytes==0 that means that an oversize allocation
26417** (an allocation larger than 0x40000000) was requested and this
26418** routine should return 0 without freeing pPrior.
26419*/
26420static void *memsys5Realloc(void *pPrior, int nBytes){
26421 int nOld;
26422 void *p;
26423 assert( pPrior!=0 );
26424 assert( (nBytes&(nBytes-1))==0 ); /* EV: R-46199-30249 */
26425 assert( nBytes>=0 );
26426 if( nBytes==0 ){
26427 return 0;
26428 }
26429 nOld = memsys5Size(pPrior);
26430 if( nBytes<=nOld ){
26431 return pPrior;
26432 }
26433 p = memsys5Malloc(nBytes);
26434 if( p ){
26435 memcpy(p, pPrior, nOld);
26436 memsys5Free(pPrior);
26437 }
26438 return p;
26439}
26440
26441/*
26442** Round up a request size to the next valid allocation size. If
26443** the allocation is too large to be handled by this allocation system,
26444** return 0.
26445**
26446** All allocations must be a power of two and must be expressed by a
26447** 32-bit signed integer. Hence the largest allocation is 0x40000000
26448** or 1073741824 bytes.
26449*/
26450static int memsys5Roundup(int n){
26451 int iFullSz;
26452 if( n > 0x40000000 ) return 0;
26453 for(iFullSz=mem5.szAtom; iFullSz<n; iFullSz *= 2);
26454 return iFullSz;
26455}
26456
26457/*
26458** Return the ceiling of the logarithm base 2 of iValue.
26459**
26460** Examples: memsys5Log(1) -> 0
26461** memsys5Log(2) -> 1
26462** memsys5Log(4) -> 2
26463** memsys5Log(5) -> 3
26464** memsys5Log(8) -> 3
26465** memsys5Log(9) -> 4
26466*/
26467static int memsys5Log(int iValue){
26468 int iLog;
26469 for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++);
26470 return iLog;
26471}
26472
26473/*
26474** Initialize the memory allocator.
26475**
26476** This routine is not threadsafe. The caller must be holding a mutex
26477** to prevent multiple threads from entering at the same time.
26478*/
26479static int memsys5Init(void *NotUsed){
26480 int ii; /* Loop counter */
26481 int nByte; /* Number of bytes of memory available to this allocator */
26482 u8 *zByte; /* Memory usable by this allocator */
26483 int nMinLog; /* Log base 2 of minimum allocation size in bytes */
26484 int iOffset; /* An offset into mem5.aCtrl[] */
26485
26486 UNUSED_PARAMETER(NotUsed);
26487
26488 /* For the purposes of this routine, disable the mutex */
26489 mem5.mutex = 0;
26490
26491 /* The size of a Mem5Link object must be a power of two. Verify that
26492 ** this is case.
26493 */
26494 assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 );
26495
26496 nByte = sqlite3GlobalConfig.nHeap;
26497 zByte = (u8*)sqlite3GlobalConfig.pHeap;
26498 assert( zByte!=0 ); /* sqlite3_config() does not allow otherwise */
26499
26500 /* boundaries on sqlite3GlobalConfig.mnReq are enforced in sqlite3_config() */
26501 nMinLog = memsys5Log(sqlite3GlobalConfig.mnReq);
26502 mem5.szAtom = (1<<nMinLog);
26503 while( (int)sizeof(Mem5Link)>mem5.szAtom ){
26504 mem5.szAtom = mem5.szAtom << 1;
26505 }
26506
26507 mem5.nBlock = (nByte / (mem5.szAtom+sizeof(u8)));
26508 mem5.zPool = zByte;
26509 mem5.aCtrl = (u8 *)&mem5.zPool[mem5.nBlock*mem5.szAtom];
26510
26511 for(ii=0; ii<=LOGMAX; ii++){
26512 mem5.aiFreelist[ii] = -1;
26513 }
26514
26515 iOffset = 0;
26516 for(ii=LOGMAX; ii>=0; ii--){
26517 int nAlloc = (1<<ii);
26518 if( (iOffset+nAlloc)<=mem5.nBlock ){
26519 mem5.aCtrl[iOffset] = ii | CTRL_FREE;
26520 memsys5Link(iOffset, ii);
26521 iOffset += nAlloc;
26522 }
26523 assert((iOffset+nAlloc)>mem5.nBlock);
26524 }
26525
26526 /* If a mutex is required for normal operation, allocate one */
26527 if( sqlite3GlobalConfig.bMemstat==0 ){
26528 mem5.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
26529 }
26530
26531 return SQLITE_OK;
26532}
26533
26534/*
26535** Deinitialize this module.
26536*/
26537static void memsys5Shutdown(void *NotUsed){
26538 UNUSED_PARAMETER(NotUsed);
26539 mem5.mutex = 0;
26540 return;
26541}
26542
26543#ifdef SQLITE_TEST
26544/*
26545** Open the file indicated and write a log of all unfreed memory
26546** allocations into that log.
26547*/
26548SQLITE_PRIVATE void sqlite3Memsys5Dump(const char *zFilename){
26549 FILE *out;
26550 int i, j, n;
26551 int nMinLog;
26552
26553 if( zFilename==0 || zFilename[0]==0 ){
26554 out = stdout;
26555 }else{
26556 out = fopen(zFilename, "w");
26557 if( out==0 ){
26558 fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
26559 zFilename);
26560 return;
26561 }
26562 }
26563 memsys5Enter();
26564 nMinLog = memsys5Log(mem5.szAtom);
26565 for(i=0; i<=LOGMAX && i+nMinLog<32; i++){
26566 for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){}
26567 fprintf(out, "freelist items of size %d: %d\n", mem5.szAtom << i, n);
26568 }
26569 fprintf(out, "mem5.nAlloc = %llu\n", mem5.nAlloc);
26570 fprintf(out, "mem5.totalAlloc = %llu\n", mem5.totalAlloc);
26571 fprintf(out, "mem5.totalExcess = %llu\n", mem5.totalExcess);
26572 fprintf(out, "mem5.currentOut = %u\n", mem5.currentOut);
26573 fprintf(out, "mem5.currentCount = %u\n", mem5.currentCount);
26574 fprintf(out, "mem5.maxOut = %u\n", mem5.maxOut);
26575 fprintf(out, "mem5.maxCount = %u\n", mem5.maxCount);
26576 fprintf(out, "mem5.maxRequest = %u\n", mem5.maxRequest);
26577 memsys5Leave();
26578 if( out==stdout ){
26579 fflush(stdout);
26580 }else{
26581 fclose(out);
26582 }
26583}
26584#endif
26585
26586/*
26587** This routine is the only routine in this file with external
26588** linkage. It returns a pointer to a static sqlite3_mem_methods
26589** struct populated with the memsys5 methods.
26590*/
26591SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){
26592 static const sqlite3_mem_methods memsys5Methods = {
26593 memsys5Malloc,
26594 memsys5Free,
26595 memsys5Realloc,
26596 memsys5Size,
26597 memsys5Roundup,
26598 memsys5Init,
26599 memsys5Shutdown,
26600 0
26601 };
26602 return &memsys5Methods;
26603}
26604
26605#endif /* SQLITE_ENABLE_MEMSYS5 */
26606
26607/************** End of mem5.c ************************************************/
26608/************** Begin file mutex.c *******************************************/
26609/*
26610** 2007 August 14
26611**
26612** The author disclaims copyright to this source code. In place of
26613** a legal notice, here is a blessing:
26614**
26615** May you do good and not evil.
26616** May you find forgiveness for yourself and forgive others.
26617** May you share freely, never taking more than you give.
26618**
26619*************************************************************************
26620** This file contains the C functions that implement mutexes.
26621**
26622** This file contains code that is common across all mutex implementations.
26623*/
26624/* #include "sqliteInt.h" */
26625
26626#if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT)
26627/*
26628** For debugging purposes, record when the mutex subsystem is initialized
26629** and uninitialized so that we can assert() if there is an attempt to
26630** allocate a mutex while the system is uninitialized.
26631*/
26632static SQLITE_WSD int mutexIsInit = 0;
26633#endif /* SQLITE_DEBUG && !defined(SQLITE_MUTEX_OMIT) */
26634
26635
26636#ifndef SQLITE_MUTEX_OMIT
26637
26638#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
26639/*
26640** This block (enclosed by SQLITE_ENABLE_MULTITHREADED_CHECKS) contains
26641** the implementation of a wrapper around the system default mutex
26642** implementation (sqlite3DefaultMutex()).
26643**
26644** Most calls are passed directly through to the underlying default
26645** mutex implementation. Except, if a mutex is configured by calling
26646** sqlite3MutexWarnOnContention() on it, then if contention is ever
26647** encountered within xMutexEnter() a warning is emitted via sqlite3_log().
26648**
26649** This type of mutex is used as the database handle mutex when testing
26650** apps that usually use SQLITE_CONFIG_MULTITHREAD mode.
26651*/
26652
26653/*
26654** Type for all mutexes used when SQLITE_ENABLE_MULTITHREADED_CHECKS
26655** is defined. Variable CheckMutex.mutex is a pointer to the real mutex
26656** allocated by the system mutex implementation. Variable iType is usually set
26657** to the type of mutex requested - SQLITE_MUTEX_RECURSIVE, SQLITE_MUTEX_FAST
26658** or one of the static mutex identifiers. Or, if this is a recursive mutex
26659** that has been configured using sqlite3MutexWarnOnContention(), it is
26660** set to SQLITE_MUTEX_WARNONCONTENTION.
26661*/
26662typedef struct CheckMutex CheckMutex;
26663struct CheckMutex {
26664 int iType;
26665 sqlite3_mutex *mutex;
26666};
26667
26668#define SQLITE_MUTEX_WARNONCONTENTION (-1)
26669
26670/*
26671** Pointer to real mutex methods object used by the CheckMutex
26672** implementation. Set by checkMutexInit().
26673*/
26674static SQLITE_WSD const sqlite3_mutex_methods *pGlobalMutexMethods;
26675
26676#ifdef SQLITE_DEBUG
26677static int checkMutexHeld(sqlite3_mutex *p){
26678 return pGlobalMutexMethods->xMutexHeld(((CheckMutex*)p)->mutex);
26679}
26680static int checkMutexNotheld(sqlite3_mutex *p){
26681 return pGlobalMutexMethods->xMutexNotheld(((CheckMutex*)p)->mutex);
26682}
26683#endif
26684
26685/*
26686** Initialize and deinitialize the mutex subsystem.
26687*/
26688static int checkMutexInit(void){
26689 pGlobalMutexMethods = sqlite3DefaultMutex();
26690 return SQLITE_OK;
26691}
26692static int checkMutexEnd(void){
26693 pGlobalMutexMethods = 0;
26694 return SQLITE_OK;
26695}
26696
26697/*
26698** Allocate a mutex.
26699*/
26700static sqlite3_mutex *checkMutexAlloc(int iType){
26701 static CheckMutex staticMutexes[] = {
26702 {2, 0}, {3, 0}, {4, 0}, {5, 0},
26703 {6, 0}, {7, 0}, {8, 0}, {9, 0},
26704 {10, 0}, {11, 0}, {12, 0}, {13, 0}
26705 };
26706 CheckMutex *p = 0;
26707
26708 assert( SQLITE_MUTEX_RECURSIVE==1 && SQLITE_MUTEX_FAST==0 );
26709 if( iType<2 ){
26710 p = sqlite3MallocZero(sizeof(CheckMutex));
26711 if( p==0 ) return 0;
26712 p->iType = iType;
26713 }else{
26714#ifdef SQLITE_ENABLE_API_ARMOR
26715 if( iType-2>=ArraySize(staticMutexes) ){
26716 (void)SQLITE_MISUSE_BKPT;
26717 return 0;
26718 }
26719#endif
26720 p = &staticMutexes[iType-2];
26721 }
26722
26723 if( p->mutex==0 ){
26724 p->mutex = pGlobalMutexMethods->xMutexAlloc(iType);
26725 if( p->mutex==0 ){
26726 if( iType<2 ){
26727 sqlite3_free(p);
26728 }
26729 p = 0;
26730 }
26731 }
26732
26733 return (sqlite3_mutex*)p;
26734}
26735
26736/*
26737** Free a mutex.
26738*/
26739static void checkMutexFree(sqlite3_mutex *p){
26740 assert( SQLITE_MUTEX_RECURSIVE<2 );
26741 assert( SQLITE_MUTEX_FAST<2 );
26742 assert( SQLITE_MUTEX_WARNONCONTENTION<2 );
26743
26744#if SQLITE_ENABLE_API_ARMOR
26745 if( ((CheckMutex*)p)->iType<2 )
26746#endif
26747 {
26748 CheckMutex *pCheck = (CheckMutex*)p;
26749 pGlobalMutexMethods->xMutexFree(pCheck->mutex);
26750 sqlite3_free(pCheck);
26751 }
26752#ifdef SQLITE_ENABLE_API_ARMOR
26753 else{
26754 (void)SQLITE_MISUSE_BKPT;
26755 }
26756#endif
26757}
26758
26759/*
26760** Enter the mutex.
26761*/
26762static void checkMutexEnter(sqlite3_mutex *p){
26763 CheckMutex *pCheck = (CheckMutex*)p;
26764 if( pCheck->iType==SQLITE_MUTEX_WARNONCONTENTION ){
26765 if( SQLITE_OK==pGlobalMutexMethods->xMutexTry(pCheck->mutex) ){
26766 return;
26767 }
26768 sqlite3_log(SQLITE_MISUSE,
26769 "illegal multi-threaded access to database connection"
26770 );
26771 }
26772 pGlobalMutexMethods->xMutexEnter(pCheck->mutex);
26773}
26774
26775/*
26776** Enter the mutex (do not block).
26777*/
26778static int checkMutexTry(sqlite3_mutex *p){
26779 CheckMutex *pCheck = (CheckMutex*)p;
26780 return pGlobalMutexMethods->xMutexTry(pCheck->mutex);
26781}
26782
26783/*
26784** Leave the mutex.
26785*/
26786static void checkMutexLeave(sqlite3_mutex *p){
26787 CheckMutex *pCheck = (CheckMutex*)p;
26788 pGlobalMutexMethods->xMutexLeave(pCheck->mutex);
26789}
26790
26791sqlite3_mutex_methods const *multiThreadedCheckMutex(void){
26792 static const sqlite3_mutex_methods sMutex = {
26793 checkMutexInit,
26794 checkMutexEnd,
26795 checkMutexAlloc,
26796 checkMutexFree,
26797 checkMutexEnter,
26798 checkMutexTry,
26799 checkMutexLeave,
26800#ifdef SQLITE_DEBUG
26801 checkMutexHeld,
26802 checkMutexNotheld
26803#else
26804 0,
26805 0
26806#endif
26807 };
26808 return &sMutex;
26809}
26810
26811/*
26812** Mark the SQLITE_MUTEX_RECURSIVE mutex passed as the only argument as
26813** one on which there should be no contention.
26814*/
26815SQLITE_PRIVATE void sqlite3MutexWarnOnContention(sqlite3_mutex *p){
26816 if( sqlite3GlobalConfig.mutex.xMutexAlloc==checkMutexAlloc ){
26817 CheckMutex *pCheck = (CheckMutex*)p;
26818 assert( pCheck->iType==SQLITE_MUTEX_RECURSIVE );
26819 pCheck->iType = SQLITE_MUTEX_WARNONCONTENTION;
26820 }
26821}
26822#endif /* ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS */
26823
26824/*
26825** Initialize the mutex system.
26826*/
26827SQLITE_PRIVATE int sqlite3MutexInit(void){
26828 int rc = SQLITE_OK;
26829 if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
26830 /* If the xMutexAlloc method has not been set, then the user did not
26831 ** install a mutex implementation via sqlite3_config() prior to
26832 ** sqlite3_initialize() being called. This block copies pointers to
26833 ** the default implementation into the sqlite3GlobalConfig structure.
26834 */
26835 sqlite3_mutex_methods const *pFrom;
26836 sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
26837
26838 if( sqlite3GlobalConfig.bCoreMutex ){
26839#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
26840 pFrom = multiThreadedCheckMutex();
26841#else
26842 pFrom = sqlite3DefaultMutex();
26843#endif
26844 }else{
26845 pFrom = sqlite3NoopMutex();
26846 }
26847 pTo->xMutexInit = pFrom->xMutexInit;
26848 pTo->xMutexEnd = pFrom->xMutexEnd;
26849 pTo->xMutexFree = pFrom->xMutexFree;
26850 pTo->xMutexEnter = pFrom->xMutexEnter;
26851 pTo->xMutexTry = pFrom->xMutexTry;
26852 pTo->xMutexLeave = pFrom->xMutexLeave;
26853 pTo->xMutexHeld = pFrom->xMutexHeld;
26854 pTo->xMutexNotheld = pFrom->xMutexNotheld;
26855 sqlite3MemoryBarrier();
26856 pTo->xMutexAlloc = pFrom->xMutexAlloc;
26857 }
26858 assert( sqlite3GlobalConfig.mutex.xMutexInit );
26859 rc = sqlite3GlobalConfig.mutex.xMutexInit();
26860
26861#ifdef SQLITE_DEBUG
26862 GLOBAL(int, mutexIsInit) = 1;
26863#endif
26864
26865 sqlite3MemoryBarrier();
26866 return rc;
26867}
26868
26869/*
26870** Shutdown the mutex system. This call frees resources allocated by
26871** sqlite3MutexInit().
26872*/
26873SQLITE_PRIVATE int sqlite3MutexEnd(void){
26874 int rc = SQLITE_OK;
26875 if( sqlite3GlobalConfig.mutex.xMutexEnd ){
26876 rc = sqlite3GlobalConfig.mutex.xMutexEnd();
26877 }
26878
26879#ifdef SQLITE_DEBUG
26880 GLOBAL(int, mutexIsInit) = 0;
26881#endif
26882
26883 return rc;
26884}
26885
26886/*
26887** Retrieve a pointer to a static mutex or allocate a new dynamic one.
26888*/
26889SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
26890#ifndef SQLITE_OMIT_AUTOINIT
26891 if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0;
26892 if( id>SQLITE_MUTEX_RECURSIVE && sqlite3MutexInit() ) return 0;
26893#endif
26894 assert( sqlite3GlobalConfig.mutex.xMutexAlloc );
26895 return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
26896}
26897
26898SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){
26899 if( !sqlite3GlobalConfig.bCoreMutex ){
26900 return 0;
26901 }
26902 assert( GLOBAL(int, mutexIsInit) );
26903 assert( sqlite3GlobalConfig.mutex.xMutexAlloc );
26904 return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
26905}
26906
26907/*
26908** Free a dynamic mutex.
26909*/
26910SQLITE_API void sqlite3_mutex_free(sqlite3_mutex *p){
26911 if( p ){
26912 assert( sqlite3GlobalConfig.mutex.xMutexFree );
26913 sqlite3GlobalConfig.mutex.xMutexFree(p);
26914 }
26915}
26916
26917/*
26918** Obtain the mutex p. If some other thread already has the mutex, block
26919** until it can be obtained.
26920*/
26921SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex *p){
26922 if( p ){
26923 assert( sqlite3GlobalConfig.mutex.xMutexEnter );
26924 sqlite3GlobalConfig.mutex.xMutexEnter(p);
26925 }
26926}
26927
26928/*
26929** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
26930** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
26931*/
26932SQLITE_API int sqlite3_mutex_try(sqlite3_mutex *p){
26933 int rc = SQLITE_OK;
26934 if( p ){
26935 assert( sqlite3GlobalConfig.mutex.xMutexTry );
26936 return sqlite3GlobalConfig.mutex.xMutexTry(p);
26937 }
26938 return rc;
26939}
26940
26941/*
26942** The sqlite3_mutex_leave() routine exits a mutex that was previously
26943** entered by the same thread. The behavior is undefined if the mutex
26944** is not currently entered. If a NULL pointer is passed as an argument
26945** this function is a no-op.
26946*/
26947SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex *p){
26948 if( p ){
26949 assert( sqlite3GlobalConfig.mutex.xMutexLeave );
26950 sqlite3GlobalConfig.mutex.xMutexLeave(p);
26951 }
26952}
26953
26954#ifndef NDEBUG
26955/*
26956** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
26957** intended for use inside assert() statements.
26958*/
26959SQLITE_API int sqlite3_mutex_held(sqlite3_mutex *p){
26960 assert( p==0 || sqlite3GlobalConfig.mutex.xMutexHeld );
26961 return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p);
26962}
26963SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
26964 assert( p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld );
26965 return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
26966}
26967#endif
26968
26969#endif /* !defined(SQLITE_MUTEX_OMIT) */
26970
26971/************** End of mutex.c ***********************************************/
26972/************** Begin file mutex_noop.c **************************************/
26973/*
26974** 2008 October 07
26975**
26976** The author disclaims copyright to this source code. In place of
26977** a legal notice, here is a blessing:
26978**
26979** May you do good and not evil.
26980** May you find forgiveness for yourself and forgive others.
26981** May you share freely, never taking more than you give.
26982**
26983*************************************************************************
26984** This file contains the C functions that implement mutexes.
26985**
26986** This implementation in this file does not provide any mutual
26987** exclusion and is thus suitable for use only in applications
26988** that use SQLite in a single thread. The routines defined
26989** here are place-holders. Applications can substitute working
26990** mutex routines at start-time using the
26991**
26992** sqlite3_config(SQLITE_CONFIG_MUTEX,...)
26993**
26994** interface.
26995**
26996** If compiled with SQLITE_DEBUG, then additional logic is inserted
26997** that does error checking on mutexes to make sure they are being
26998** called correctly.
26999*/
27000/* #include "sqliteInt.h" */
27001
27002#ifndef SQLITE_MUTEX_OMIT
27003
27004#ifndef SQLITE_DEBUG
27005/*
27006** Stub routines for all mutex methods.
27007**
27008** This routines provide no mutual exclusion or error checking.
27009*/
27010static int noopMutexInit(void){ return SQLITE_OK; }
27011static int noopMutexEnd(void){ return SQLITE_OK; }
27012static sqlite3_mutex *noopMutexAlloc(int id){
27013 UNUSED_PARAMETER(id);
27014 return (sqlite3_mutex*)8;
27015}
27016static void noopMutexFree(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
27017static void noopMutexEnter(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
27018static int noopMutexTry(sqlite3_mutex *p){
27019 UNUSED_PARAMETER(p);
27020 return SQLITE_OK;
27021}
27022static void noopMutexLeave(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
27023
27024SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
27025 static const sqlite3_mutex_methods sMutex = {
27026 noopMutexInit,
27027 noopMutexEnd,
27028 noopMutexAlloc,
27029 noopMutexFree,
27030 noopMutexEnter,
27031 noopMutexTry,
27032 noopMutexLeave,
27033
27034 0,
27035 0,
27036 };
27037
27038 return &sMutex;
27039}
27040#endif /* !SQLITE_DEBUG */
27041
27042#ifdef SQLITE_DEBUG
27043/*
27044** In this implementation, error checking is provided for testing
27045** and debugging purposes. The mutexes still do not provide any
27046** mutual exclusion.
27047*/
27048
27049/*
27050** The mutex object
27051*/
27052typedef struct sqlite3_debug_mutex {
27053 int id; /* The mutex type */
27054 int cnt; /* Number of entries without a matching leave */
27055} sqlite3_debug_mutex;
27056
27057/*
27058** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
27059** intended for use inside assert() statements.
27060*/
27061static int debugMutexHeld(sqlite3_mutex *pX){
27062 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
27063 return p==0 || p->cnt>0;
27064}
27065static int debugMutexNotheld(sqlite3_mutex *pX){
27066 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
27067 return p==0 || p->cnt==0;
27068}
27069
27070/*
27071** Initialize and deinitialize the mutex subsystem.
27072*/
27073static int debugMutexInit(void){ return SQLITE_OK; }
27074static int debugMutexEnd(void){ return SQLITE_OK; }
27075
27076/*
27077** The sqlite3_mutex_alloc() routine allocates a new
27078** mutex and returns a pointer to it. If it returns NULL
27079** that means that a mutex could not be allocated.
27080*/
27081static sqlite3_mutex *debugMutexAlloc(int id){
27082 static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_VFS3 - 1];
27083 sqlite3_debug_mutex *pNew = 0;
27084 switch( id ){
27085 case SQLITE_MUTEX_FAST:
27086 case SQLITE_MUTEX_RECURSIVE: {
27087 pNew = sqlite3Malloc(sizeof(*pNew));
27088 if( pNew ){
27089 pNew->id = id;
27090 pNew->cnt = 0;
27091 }
27092 break;
27093 }
27094 default: {
27095#ifdef SQLITE_ENABLE_API_ARMOR
27096 if( id-2<0 || id-2>=ArraySize(aStatic) ){
27097 (void)SQLITE_MISUSE_BKPT;
27098 return 0;
27099 }
27100#endif
27101 pNew = &aStatic[id-2];
27102 pNew->id = id;
27103 break;
27104 }
27105 }
27106 return (sqlite3_mutex*)pNew;
27107}
27108
27109/*
27110** This routine deallocates a previously allocated mutex.
27111*/
27112static void debugMutexFree(sqlite3_mutex *pX){
27113 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
27114 assert( p->cnt==0 );
27115 if( p->id==SQLITE_MUTEX_RECURSIVE || p->id==SQLITE_MUTEX_FAST ){
27116 sqlite3_free(p);
27117 }else{
27118#ifdef SQLITE_ENABLE_API_ARMOR
27119 (void)SQLITE_MISUSE_BKPT;
27120#endif
27121 }
27122}
27123
27124/*
27125** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
27126** to enter a mutex. If another thread is already within the mutex,
27127** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
27128** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
27129** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
27130** be entered multiple times by the same thread. In such cases the,
27131** mutex must be exited an equal number of times before another thread
27132** can enter. If the same thread tries to enter any other kind of mutex
27133** more than once, the behavior is undefined.
27134*/
27135static void debugMutexEnter(sqlite3_mutex *pX){
27136 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
27137 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
27138 p->cnt++;
27139}
27140static int debugMutexTry(sqlite3_mutex *pX){
27141 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
27142 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
27143 p->cnt++;
27144 return SQLITE_OK;
27145}
27146
27147/*
27148** The sqlite3_mutex_leave() routine exits a mutex that was
27149** previously entered by the same thread. The behavior
27150** is undefined if the mutex is not currently entered or
27151** is not currently allocated. SQLite will never do either.
27152*/
27153static void debugMutexLeave(sqlite3_mutex *pX){
27154 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
27155 assert( debugMutexHeld(pX) );
27156 p->cnt--;
27157 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
27158}
27159
27160SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
27161 static const sqlite3_mutex_methods sMutex = {
27162 debugMutexInit,
27163 debugMutexEnd,
27164 debugMutexAlloc,
27165 debugMutexFree,
27166 debugMutexEnter,
27167 debugMutexTry,
27168 debugMutexLeave,
27169
27170 debugMutexHeld,
27171 debugMutexNotheld
27172 };
27173
27174 return &sMutex;
27175}
27176#endif /* SQLITE_DEBUG */
27177
27178/*
27179** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
27180** is used regardless of the run-time threadsafety setting.
27181*/
27182#ifdef SQLITE_MUTEX_NOOP
27183SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
27184 return sqlite3NoopMutex();
27185}
27186#endif /* defined(SQLITE_MUTEX_NOOP) */
27187#endif /* !defined(SQLITE_MUTEX_OMIT) */
27188
27189/************** End of mutex_noop.c ******************************************/
27190/************** Begin file mutex_unix.c **************************************/
27191/*
27192** 2007 August 28
27193**
27194** The author disclaims copyright to this source code. In place of
27195** a legal notice, here is a blessing:
27196**
27197** May you do good and not evil.
27198** May you find forgiveness for yourself and forgive others.
27199** May you share freely, never taking more than you give.
27200**
27201*************************************************************************
27202** This file contains the C functions that implement mutexes for pthreads
27203*/
27204/* #include "sqliteInt.h" */
27205
27206/*
27207** The code in this file is only used if we are compiling threadsafe
27208** under unix with pthreads.
27209**
27210** Note that this implementation requires a version of pthreads that
27211** supports recursive mutexes.
27212*/
27213#ifdef SQLITE_MUTEX_PTHREADS
27214
27215#include <pthread.h>
27216
27217/*
27218** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields
27219** are necessary under two condidtions: (1) Debug builds and (2) using
27220** home-grown mutexes. Encapsulate these conditions into a single #define.
27221*/
27222#if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX)
27223# define SQLITE_MUTEX_NREF 1
27224#else
27225# define SQLITE_MUTEX_NREF 0
27226#endif
27227
27228/*
27229** Each recursive mutex is an instance of the following structure.
27230*/
27231struct sqlite3_mutex {
27232 pthread_mutex_t mutex; /* Mutex controlling the lock */
27233#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
27234 int id; /* Mutex type */
27235#endif
27236#if SQLITE_MUTEX_NREF
27237 volatile int nRef; /* Number of entrances */
27238 volatile pthread_t owner; /* Thread that is within this mutex */
27239 int trace; /* True to trace changes */
27240#endif
27241};
27242#if SQLITE_MUTEX_NREF
27243# define SQLITE3_MUTEX_INITIALIZER(id) \
27244 {PTHREAD_MUTEX_INITIALIZER,id,0,(pthread_t)0,0}
27245#elif defined(SQLITE_ENABLE_API_ARMOR)
27246# define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER, id }
27247#else
27248#define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER }
27249#endif
27250
27251/*
27252** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
27253** intended for use only inside assert() statements. On some platforms,
27254** there might be race conditions that can cause these routines to
27255** deliver incorrect results. In particular, if pthread_equal() is
27256** not an atomic operation, then these routines might delivery
27257** incorrect results. On most platforms, pthread_equal() is a
27258** comparison of two integers and is therefore atomic. But we are
27259** told that HPUX is not such a platform. If so, then these routines
27260** will not always work correctly on HPUX.
27261**
27262** On those platforms where pthread_equal() is not atomic, SQLite
27263** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
27264** make sure no assert() statements are evaluated and hence these
27265** routines are never called.
27266*/
27267#if !defined(NDEBUG) || defined(SQLITE_DEBUG)
27268static int pthreadMutexHeld(sqlite3_mutex *p){
27269 return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
27270}
27271static int pthreadMutexNotheld(sqlite3_mutex *p){
27272 return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
27273}
27274#endif
27275
27276/*
27277** Try to provide a memory barrier operation, needed for initialization
27278** and also for the implementation of xShmBarrier in the VFS in cases
27279** where SQLite is compiled without mutexes.
27280*/
27281SQLITE_PRIVATE void sqlite3MemoryBarrier(void){
27282#if defined(SQLITE_MEMORY_BARRIER)
27283 SQLITE_MEMORY_BARRIER;
27284#elif defined(__GNUC__) && GCC_VERSION>=4001000
27285 __sync_synchronize();
27286#endif
27287}
27288
27289/*
27290** Initialize and deinitialize the mutex subsystem.
27291*/
27292static int pthreadMutexInit(void){ return SQLITE_OK; }
27293static int pthreadMutexEnd(void){ return SQLITE_OK; }
27294
27295/*
27296** The sqlite3_mutex_alloc() routine allocates a new
27297** mutex and returns a pointer to it. If it returns NULL
27298** that means that a mutex could not be allocated. SQLite
27299** will unwind its stack and return an error. The argument
27300** to sqlite3_mutex_alloc() is one of these integer constants:
27301**
27302** <ul>
27303** <li> SQLITE_MUTEX_FAST
27304** <li> SQLITE_MUTEX_RECURSIVE
27305** <li> SQLITE_MUTEX_STATIC_MAIN
27306** <li> SQLITE_MUTEX_STATIC_MEM
27307** <li> SQLITE_MUTEX_STATIC_OPEN
27308** <li> SQLITE_MUTEX_STATIC_PRNG
27309** <li> SQLITE_MUTEX_STATIC_LRU
27310** <li> SQLITE_MUTEX_STATIC_PMEM
27311** <li> SQLITE_MUTEX_STATIC_APP1
27312** <li> SQLITE_MUTEX_STATIC_APP2
27313** <li> SQLITE_MUTEX_STATIC_APP3
27314** <li> SQLITE_MUTEX_STATIC_VFS1
27315** <li> SQLITE_MUTEX_STATIC_VFS2
27316** <li> SQLITE_MUTEX_STATIC_VFS3
27317** </ul>
27318**
27319** The first two constants cause sqlite3_mutex_alloc() to create
27320** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
27321** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
27322** The mutex implementation does not need to make a distinction
27323** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
27324** not want to. But SQLite will only request a recursive mutex in
27325** cases where it really needs one. If a faster non-recursive mutex
27326** implementation is available on the host platform, the mutex subsystem
27327** might return such a mutex in response to SQLITE_MUTEX_FAST.
27328**
27329** The other allowed parameters to sqlite3_mutex_alloc() each return
27330** a pointer to a static preexisting mutex. Six static mutexes are
27331** used by the current version of SQLite. Future versions of SQLite
27332** may add additional static mutexes. Static mutexes are for internal
27333** use by SQLite only. Applications that use SQLite mutexes should
27334** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
27335** SQLITE_MUTEX_RECURSIVE.
27336**
27337** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
27338** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
27339** returns a different mutex on every call. But for the static
27340** mutex types, the same mutex is returned on every call that has
27341** the same type number.
27342*/
27343static sqlite3_mutex *pthreadMutexAlloc(int iType){
27344 static sqlite3_mutex staticMutexes[] = {
27345 SQLITE3_MUTEX_INITIALIZER(2),
27346 SQLITE3_MUTEX_INITIALIZER(3),
27347 SQLITE3_MUTEX_INITIALIZER(4),
27348 SQLITE3_MUTEX_INITIALIZER(5),
27349 SQLITE3_MUTEX_INITIALIZER(6),
27350 SQLITE3_MUTEX_INITIALIZER(7),
27351 SQLITE3_MUTEX_INITIALIZER(8),
27352 SQLITE3_MUTEX_INITIALIZER(9),
27353 SQLITE3_MUTEX_INITIALIZER(10),
27354 SQLITE3_MUTEX_INITIALIZER(11),
27355 SQLITE3_MUTEX_INITIALIZER(12),
27356 SQLITE3_MUTEX_INITIALIZER(13)
27357 };
27358 sqlite3_mutex *p;
27359 switch( iType ){
27360 case SQLITE_MUTEX_RECURSIVE: {
27361 p = sqlite3MallocZero( sizeof(*p) );
27362 if( p ){
27363#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
27364 /* If recursive mutexes are not available, we will have to
27365 ** build our own. See below. */
27366 pthread_mutex_init(&p->mutex, 0);
27367#else
27368 /* Use a recursive mutex if it is available */
27369 pthread_mutexattr_t recursiveAttr;
27370 pthread_mutexattr_init(attr: &recursiveAttr);
27371 pthread_mutexattr_settype(attr: &recursiveAttr, kind: PTHREAD_MUTEX_RECURSIVE);
27372 pthread_mutex_init(mutex: &p->mutex, mutexattr: &recursiveAttr);
27373 pthread_mutexattr_destroy(attr: &recursiveAttr);
27374#endif
27375#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
27376 p->id = SQLITE_MUTEX_RECURSIVE;
27377#endif
27378 }
27379 break;
27380 }
27381 case SQLITE_MUTEX_FAST: {
27382 p = sqlite3MallocZero( sizeof(*p) );
27383 if( p ){
27384 pthread_mutex_init(mutex: &p->mutex, mutexattr: 0);
27385#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
27386 p->id = SQLITE_MUTEX_FAST;
27387#endif
27388 }
27389 break;
27390 }
27391 default: {
27392#ifdef SQLITE_ENABLE_API_ARMOR
27393 if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){
27394 (void)SQLITE_MISUSE_BKPT;
27395 return 0;
27396 }
27397#endif
27398 p = &staticMutexes[iType-2];
27399 break;
27400 }
27401 }
27402#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
27403 assert( p==0 || p->id==iType );
27404#endif
27405 return p;
27406}
27407
27408
27409/*
27410** This routine deallocates a previously
27411** allocated mutex. SQLite is careful to deallocate every
27412** mutex that it allocates.
27413*/
27414static void pthreadMutexFree(sqlite3_mutex *p){
27415 assert( p->nRef==0 );
27416#if SQLITE_ENABLE_API_ARMOR
27417 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE )
27418#endif
27419 {
27420 pthread_mutex_destroy(mutex: &p->mutex);
27421 sqlite3_free(p);
27422 }
27423#ifdef SQLITE_ENABLE_API_ARMOR
27424 else{
27425 (void)SQLITE_MISUSE_BKPT;
27426 }
27427#endif
27428}
27429
27430/*
27431** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
27432** to enter a mutex. If another thread is already within the mutex,
27433** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
27434** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
27435** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
27436** be entered multiple times by the same thread. In such cases the,
27437** mutex must be exited an equal number of times before another thread
27438** can enter. If the same thread tries to enter any other kind of mutex
27439** more than once, the behavior is undefined.
27440*/
27441static void pthreadMutexEnter(sqlite3_mutex *p){
27442 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
27443
27444#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
27445 /* If recursive mutexes are not available, then we have to grow
27446 ** our own. This implementation assumes that pthread_equal()
27447 ** is atomic - that it cannot be deceived into thinking self
27448 ** and p->owner are equal if p->owner changes between two values
27449 ** that are not equal to self while the comparison is taking place.
27450 ** This implementation also assumes a coherent cache - that
27451 ** separate processes cannot read different values from the same
27452 ** address at the same time. If either of these two conditions
27453 ** are not met, then the mutexes will fail and problems will result.
27454 */
27455 {
27456 pthread_t self = pthread_self();
27457 if( p->nRef>0 && pthread_equal(p->owner, self) ){
27458 p->nRef++;
27459 }else{
27460 pthread_mutex_lock(&p->mutex);
27461 assert( p->nRef==0 );
27462 p->owner = self;
27463 p->nRef = 1;
27464 }
27465 }
27466#else
27467 /* Use the built-in recursive mutexes if they are available.
27468 */
27469 pthread_mutex_lock(mutex: &p->mutex);
27470#if SQLITE_MUTEX_NREF
27471 assert( p->nRef>0 || p->owner==0 );
27472 p->owner = pthread_self();
27473 p->nRef++;
27474#endif
27475#endif
27476
27477#ifdef SQLITE_DEBUG
27478 if( p->trace ){
27479 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
27480 }
27481#endif
27482}
27483static int pthreadMutexTry(sqlite3_mutex *p){
27484 int rc;
27485 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
27486
27487#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
27488 /* If recursive mutexes are not available, then we have to grow
27489 ** our own. This implementation assumes that pthread_equal()
27490 ** is atomic - that it cannot be deceived into thinking self
27491 ** and p->owner are equal if p->owner changes between two values
27492 ** that are not equal to self while the comparison is taking place.
27493 ** This implementation also assumes a coherent cache - that
27494 ** separate processes cannot read different values from the same
27495 ** address at the same time. If either of these two conditions
27496 ** are not met, then the mutexes will fail and problems will result.
27497 */
27498 {
27499 pthread_t self = pthread_self();
27500 if( p->nRef>0 && pthread_equal(p->owner, self) ){
27501 p->nRef++;
27502 rc = SQLITE_OK;
27503 }else if( pthread_mutex_trylock(&p->mutex)==0 ){
27504 assert( p->nRef==0 );
27505 p->owner = self;
27506 p->nRef = 1;
27507 rc = SQLITE_OK;
27508 }else{
27509 rc = SQLITE_BUSY;
27510 }
27511 }
27512#else
27513 /* Use the built-in recursive mutexes if they are available.
27514 */
27515 if( pthread_mutex_trylock(mutex: &p->mutex)==0 ){
27516#if SQLITE_MUTEX_NREF
27517 p->owner = pthread_self();
27518 p->nRef++;
27519#endif
27520 rc = SQLITE_OK;
27521 }else{
27522 rc = SQLITE_BUSY;
27523 }
27524#endif
27525
27526#ifdef SQLITE_DEBUG
27527 if( rc==SQLITE_OK && p->trace ){
27528 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
27529 }
27530#endif
27531 return rc;
27532}
27533
27534/*
27535** The sqlite3_mutex_leave() routine exits a mutex that was
27536** previously entered by the same thread. The behavior
27537** is undefined if the mutex is not currently entered or
27538** is not currently allocated. SQLite will never do either.
27539*/
27540static void pthreadMutexLeave(sqlite3_mutex *p){
27541 assert( pthreadMutexHeld(p) );
27542#if SQLITE_MUTEX_NREF
27543 p->nRef--;
27544 if( p->nRef==0 ) p->owner = 0;
27545#endif
27546 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
27547
27548#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
27549 if( p->nRef==0 ){
27550 pthread_mutex_unlock(&p->mutex);
27551 }
27552#else
27553 pthread_mutex_unlock(mutex: &p->mutex);
27554#endif
27555
27556#ifdef SQLITE_DEBUG
27557 if( p->trace ){
27558 printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
27559 }
27560#endif
27561}
27562
27563SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
27564 static const sqlite3_mutex_methods sMutex = {
27565 pthreadMutexInit,
27566 pthreadMutexEnd,
27567 pthreadMutexAlloc,
27568 pthreadMutexFree,
27569 pthreadMutexEnter,
27570 pthreadMutexTry,
27571 pthreadMutexLeave,
27572#ifdef SQLITE_DEBUG
27573 pthreadMutexHeld,
27574 pthreadMutexNotheld
27575#else
27576 0,
27577 0
27578#endif
27579 };
27580
27581 return &sMutex;
27582}
27583
27584#endif /* SQLITE_MUTEX_PTHREADS */
27585
27586/************** End of mutex_unix.c ******************************************/
27587/************** Begin file mutex_w32.c ***************************************/
27588/*
27589** 2007 August 14
27590**
27591** The author disclaims copyright to this source code. In place of
27592** a legal notice, here is a blessing:
27593**
27594** May you do good and not evil.
27595** May you find forgiveness for yourself and forgive others.
27596** May you share freely, never taking more than you give.
27597**
27598*************************************************************************
27599** This file contains the C functions that implement mutexes for Win32.
27600*/
27601/* #include "sqliteInt.h" */
27602
27603#if SQLITE_OS_WIN
27604/*
27605** Include code that is common to all os_*.c files
27606*/
27607/* #include "os_common.h" */
27608
27609/*
27610** Include the header file for the Windows VFS.
27611*/
27612/************** Include os_win.h in the middle of mutex_w32.c ****************/
27613/************** Begin file os_win.h ******************************************/
27614/*
27615** 2013 November 25
27616**
27617** The author disclaims copyright to this source code. In place of
27618** a legal notice, here is a blessing:
27619**
27620** May you do good and not evil.
27621** May you find forgiveness for yourself and forgive others.
27622** May you share freely, never taking more than you give.
27623**
27624******************************************************************************
27625**
27626** This file contains code that is specific to Windows.
27627*/
27628#ifndef SQLITE_OS_WIN_H
27629#define SQLITE_OS_WIN_H
27630
27631/*
27632** Include the primary Windows SDK header file.
27633*/
27634#include "windows.h"
27635
27636#ifdef __CYGWIN__
27637# include <sys/cygwin.h>
27638# include <errno.h> /* amalgamator: dontcache */
27639#endif
27640
27641/*
27642** Determine if we are dealing with Windows NT.
27643**
27644** We ought to be able to determine if we are compiling for Windows 9x or
27645** Windows NT using the _WIN32_WINNT macro as follows:
27646**
27647** #if defined(_WIN32_WINNT)
27648** # define SQLITE_OS_WINNT 1
27649** #else
27650** # define SQLITE_OS_WINNT 0
27651** #endif
27652**
27653** However, Visual Studio 2005 does not set _WIN32_WINNT by default, as
27654** it ought to, so the above test does not work. We'll just assume that
27655** everything is Windows NT unless the programmer explicitly says otherwise
27656** by setting SQLITE_OS_WINNT to 0.
27657*/
27658#if SQLITE_OS_WIN && !defined(SQLITE_OS_WINNT)
27659# define SQLITE_OS_WINNT 1
27660#endif
27661
27662/*
27663** Determine if we are dealing with Windows CE - which has a much reduced
27664** API.
27665*/
27666#if defined(_WIN32_WCE)
27667# define SQLITE_OS_WINCE 1
27668#else
27669# define SQLITE_OS_WINCE 0
27670#endif
27671
27672/*
27673** Determine if we are dealing with WinRT, which provides only a subset of
27674** the full Win32 API.
27675*/
27676#if !defined(SQLITE_OS_WINRT)
27677# define SQLITE_OS_WINRT 0
27678#endif
27679
27680/*
27681** For WinCE, some API function parameters do not appear to be declared as
27682** volatile.
27683*/
27684#if SQLITE_OS_WINCE
27685# define SQLITE_WIN32_VOLATILE
27686#else
27687# define SQLITE_WIN32_VOLATILE volatile
27688#endif
27689
27690/*
27691** For some Windows sub-platforms, the _beginthreadex() / _endthreadex()
27692** functions are not available (e.g. those not using MSVC, Cygwin, etc).
27693*/
27694#if SQLITE_OS_WIN && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
27695 SQLITE_THREADSAFE>0 && !defined(__CYGWIN__)
27696# define SQLITE_OS_WIN_THREADS 1
27697#else
27698# define SQLITE_OS_WIN_THREADS 0
27699#endif
27700
27701#endif /* SQLITE_OS_WIN_H */
27702
27703/************** End of os_win.h **********************************************/
27704/************** Continuing where we left off in mutex_w32.c ******************/
27705#endif
27706
27707/*
27708** The code in this file is only used if we are compiling multithreaded
27709** on a Win32 system.
27710*/
27711#ifdef SQLITE_MUTEX_W32
27712
27713/*
27714** Each recursive mutex is an instance of the following structure.
27715*/
27716struct sqlite3_mutex {
27717 CRITICAL_SECTION mutex; /* Mutex controlling the lock */
27718 int id; /* Mutex type */
27719#ifdef SQLITE_DEBUG
27720 volatile int nRef; /* Number of enterances */
27721 volatile DWORD owner; /* Thread holding this mutex */
27722 volatile LONG trace; /* True to trace changes */
27723#endif
27724};
27725
27726/*
27727** These are the initializer values used when declaring a "static" mutex
27728** on Win32. It should be noted that all mutexes require initialization
27729** on the Win32 platform.
27730*/
27731#define SQLITE_W32_MUTEX_INITIALIZER { 0 }
27732
27733#ifdef SQLITE_DEBUG
27734#define SQLITE3_MUTEX_INITIALIZER(id) { SQLITE_W32_MUTEX_INITIALIZER, id, \
27735 0L, (DWORD)0, 0 }
27736#else
27737#define SQLITE3_MUTEX_INITIALIZER(id) { SQLITE_W32_MUTEX_INITIALIZER, id }
27738#endif
27739
27740#ifdef SQLITE_DEBUG
27741/*
27742** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
27743** intended for use only inside assert() statements.
27744*/
27745static int winMutexHeld(sqlite3_mutex *p){
27746 return p->nRef!=0 && p->owner==GetCurrentThreadId();
27747}
27748
27749static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){
27750 return p->nRef==0 || p->owner!=tid;
27751}
27752
27753static int winMutexNotheld(sqlite3_mutex *p){
27754 DWORD tid = GetCurrentThreadId();
27755 return winMutexNotheld2(p, tid);
27756}
27757#endif
27758
27759/*
27760** Try to provide a memory barrier operation, needed for initialization
27761** and also for the xShmBarrier method of the VFS in cases when SQLite is
27762** compiled without mutexes (SQLITE_THREADSAFE=0).
27763*/
27764SQLITE_PRIVATE void sqlite3MemoryBarrier(void){
27765#if defined(SQLITE_MEMORY_BARRIER)
27766 SQLITE_MEMORY_BARRIER;
27767#elif defined(__GNUC__)
27768 __sync_synchronize();
27769#elif MSVC_VERSION>=1300
27770 _ReadWriteBarrier();
27771#elif defined(MemoryBarrier)
27772 MemoryBarrier();
27773#endif
27774}
27775
27776/*
27777** Initialize and deinitialize the mutex subsystem.
27778*/
27779static sqlite3_mutex winMutex_staticMutexes[] = {
27780 SQLITE3_MUTEX_INITIALIZER(2),
27781 SQLITE3_MUTEX_INITIALIZER(3),
27782 SQLITE3_MUTEX_INITIALIZER(4),
27783 SQLITE3_MUTEX_INITIALIZER(5),
27784 SQLITE3_MUTEX_INITIALIZER(6),
27785 SQLITE3_MUTEX_INITIALIZER(7),
27786 SQLITE3_MUTEX_INITIALIZER(8),
27787 SQLITE3_MUTEX_INITIALIZER(9),
27788 SQLITE3_MUTEX_INITIALIZER(10),
27789 SQLITE3_MUTEX_INITIALIZER(11),
27790 SQLITE3_MUTEX_INITIALIZER(12),
27791 SQLITE3_MUTEX_INITIALIZER(13)
27792};
27793
27794static int winMutex_isInit = 0;
27795static int winMutex_isNt = -1; /* <0 means "need to query" */
27796
27797/* As the winMutexInit() and winMutexEnd() functions are called as part
27798** of the sqlite3_initialize() and sqlite3_shutdown() processing, the
27799** "interlocked" magic used here is probably not strictly necessary.
27800*/
27801static LONG SQLITE_WIN32_VOLATILE winMutex_lock = 0;
27802
27803SQLITE_API int sqlite3_win32_is_nt(void); /* os_win.c */
27804SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
27805
27806static int winMutexInit(void){
27807 /* The first to increment to 1 does actual initialization */
27808 if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
27809 int i;
27810 for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
27811#if SQLITE_OS_WINRT
27812 InitializeCriticalSectionEx(&winMutex_staticMutexes[i].mutex, 0, 0);
27813#else
27814 InitializeCriticalSection(&winMutex_staticMutexes[i].mutex);
27815#endif
27816 }
27817 winMutex_isInit = 1;
27818 }else{
27819 /* Another thread is (in the process of) initializing the static
27820 ** mutexes */
27821 while( !winMutex_isInit ){
27822 sqlite3_win32_sleep(1);
27823 }
27824 }
27825 return SQLITE_OK;
27826}
27827
27828static int winMutexEnd(void){
27829 /* The first to decrement to 0 does actual shutdown
27830 ** (which should be the last to shutdown.) */
27831 if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){
27832 if( winMutex_isInit==1 ){
27833 int i;
27834 for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
27835 DeleteCriticalSection(&winMutex_staticMutexes[i].mutex);
27836 }
27837 winMutex_isInit = 0;
27838 }
27839 }
27840 return SQLITE_OK;
27841}
27842
27843/*
27844** The sqlite3_mutex_alloc() routine allocates a new
27845** mutex and returns a pointer to it. If it returns NULL
27846** that means that a mutex could not be allocated. SQLite
27847** will unwind its stack and return an error. The argument
27848** to sqlite3_mutex_alloc() is one of these integer constants:
27849**
27850** <ul>
27851** <li> SQLITE_MUTEX_FAST
27852** <li> SQLITE_MUTEX_RECURSIVE
27853** <li> SQLITE_MUTEX_STATIC_MAIN
27854** <li> SQLITE_MUTEX_STATIC_MEM
27855** <li> SQLITE_MUTEX_STATIC_OPEN
27856** <li> SQLITE_MUTEX_STATIC_PRNG
27857** <li> SQLITE_MUTEX_STATIC_LRU
27858** <li> SQLITE_MUTEX_STATIC_PMEM
27859** <li> SQLITE_MUTEX_STATIC_APP1
27860** <li> SQLITE_MUTEX_STATIC_APP2
27861** <li> SQLITE_MUTEX_STATIC_APP3
27862** <li> SQLITE_MUTEX_STATIC_VFS1
27863** <li> SQLITE_MUTEX_STATIC_VFS2
27864** <li> SQLITE_MUTEX_STATIC_VFS3
27865** </ul>
27866**
27867** The first two constants cause sqlite3_mutex_alloc() to create
27868** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
27869** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
27870** The mutex implementation does not need to make a distinction
27871** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
27872** not want to. But SQLite will only request a recursive mutex in
27873** cases where it really needs one. If a faster non-recursive mutex
27874** implementation is available on the host platform, the mutex subsystem
27875** might return such a mutex in response to SQLITE_MUTEX_FAST.
27876**
27877** The other allowed parameters to sqlite3_mutex_alloc() each return
27878** a pointer to a static preexisting mutex. Six static mutexes are
27879** used by the current version of SQLite. Future versions of SQLite
27880** may add additional static mutexes. Static mutexes are for internal
27881** use by SQLite only. Applications that use SQLite mutexes should
27882** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
27883** SQLITE_MUTEX_RECURSIVE.
27884**
27885** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
27886** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
27887** returns a different mutex on every call. But for the static
27888** mutex types, the same mutex is returned on every call that has
27889** the same type number.
27890*/
27891static sqlite3_mutex *winMutexAlloc(int iType){
27892 sqlite3_mutex *p;
27893
27894 switch( iType ){
27895 case SQLITE_MUTEX_FAST:
27896 case SQLITE_MUTEX_RECURSIVE: {
27897 p = sqlite3MallocZero( sizeof(*p) );
27898 if( p ){
27899 p->id = iType;
27900#ifdef SQLITE_DEBUG
27901#ifdef SQLITE_WIN32_MUTEX_TRACE_DYNAMIC
27902 p->trace = 1;
27903#endif
27904#endif
27905#if SQLITE_OS_WINRT
27906 InitializeCriticalSectionEx(&p->mutex, 0, 0);
27907#else
27908 InitializeCriticalSection(&p->mutex);
27909#endif
27910 }
27911 break;
27912 }
27913 default: {
27914#ifdef SQLITE_ENABLE_API_ARMOR
27915 if( iType-2<0 || iType-2>=ArraySize(winMutex_staticMutexes) ){
27916 (void)SQLITE_MISUSE_BKPT;
27917 return 0;
27918 }
27919#endif
27920 p = &winMutex_staticMutexes[iType-2];
27921#ifdef SQLITE_DEBUG
27922#ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC
27923 InterlockedCompareExchange(&p->trace, 1, 0);
27924#endif
27925#endif
27926 break;
27927 }
27928 }
27929 assert( p==0 || p->id==iType );
27930 return p;
27931}
27932
27933
27934/*
27935** This routine deallocates a previously
27936** allocated mutex. SQLite is careful to deallocate every
27937** mutex that it allocates.
27938*/
27939static void winMutexFree(sqlite3_mutex *p){
27940 assert( p );
27941 assert( p->nRef==0 && p->owner==0 );
27942 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ){
27943 DeleteCriticalSection(&p->mutex);
27944 sqlite3_free(p);
27945 }else{
27946#ifdef SQLITE_ENABLE_API_ARMOR
27947 (void)SQLITE_MISUSE_BKPT;
27948#endif
27949 }
27950}
27951
27952/*
27953** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
27954** to enter a mutex. If another thread is already within the mutex,
27955** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
27956** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
27957** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
27958** be entered multiple times by the same thread. In such cases the,
27959** mutex must be exited an equal number of times before another thread
27960** can enter. If the same thread tries to enter any other kind of mutex
27961** more than once, the behavior is undefined.
27962*/
27963static void winMutexEnter(sqlite3_mutex *p){
27964#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
27965 DWORD tid = GetCurrentThreadId();
27966#endif
27967#ifdef SQLITE_DEBUG
27968 assert( p );
27969 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
27970#else
27971 assert( p );
27972#endif
27973 assert( winMutex_isInit==1 );
27974 EnterCriticalSection(&p->mutex);
27975#ifdef SQLITE_DEBUG
27976 assert( p->nRef>0 || p->owner==0 );
27977 p->owner = tid;
27978 p->nRef++;
27979 if( p->trace ){
27980 OSTRACE(("ENTER-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
27981 tid, p->id, p, p->trace, p->nRef));
27982 }
27983#endif
27984}
27985
27986static int winMutexTry(sqlite3_mutex *p){
27987#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
27988 DWORD tid = GetCurrentThreadId();
27989#endif
27990 int rc = SQLITE_BUSY;
27991 assert( p );
27992 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
27993 /*
27994 ** The sqlite3_mutex_try() routine is very rarely used, and when it
27995 ** is used it is merely an optimization. So it is OK for it to always
27996 ** fail.
27997 **
27998 ** The TryEnterCriticalSection() interface is only available on WinNT.
27999 ** And some windows compilers complain if you try to use it without
28000 ** first doing some #defines that prevent SQLite from building on Win98.
28001 ** For that reason, we will omit this optimization for now. See
28002 ** ticket #2685.
28003 */
28004#if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400
28005 assert( winMutex_isInit==1 );
28006 assert( winMutex_isNt>=-1 && winMutex_isNt<=1 );
28007 if( winMutex_isNt<0 ){
28008 winMutex_isNt = sqlite3_win32_is_nt();
28009 }
28010 assert( winMutex_isNt==0 || winMutex_isNt==1 );
28011 if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){
28012#ifdef SQLITE_DEBUG
28013 p->owner = tid;
28014 p->nRef++;
28015#endif
28016 rc = SQLITE_OK;
28017 }
28018#else
28019 UNUSED_PARAMETER(p);
28020#endif
28021#ifdef SQLITE_DEBUG
28022 if( p->trace ){
28023 OSTRACE(("TRY-MUTEX tid=%lu, mutex(%d)=%p (%d), owner=%lu, nRef=%d, rc=%s\n",
28024 tid, p->id, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc)));
28025 }
28026#endif
28027 return rc;
28028}
28029
28030/*
28031** The sqlite3_mutex_leave() routine exits a mutex that was
28032** previously entered by the same thread. The behavior
28033** is undefined if the mutex is not currently entered or
28034** is not currently allocated. SQLite will never do either.
28035*/
28036static void winMutexLeave(sqlite3_mutex *p){
28037#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
28038 DWORD tid = GetCurrentThreadId();
28039#endif
28040 assert( p );
28041#ifdef SQLITE_DEBUG
28042 assert( p->nRef>0 );
28043 assert( p->owner==tid );
28044 p->nRef--;
28045 if( p->nRef==0 ) p->owner = 0;
28046 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
28047#endif
28048 assert( winMutex_isInit==1 );
28049 LeaveCriticalSection(&p->mutex);
28050#ifdef SQLITE_DEBUG
28051 if( p->trace ){
28052 OSTRACE(("LEAVE-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
28053 tid, p->id, p, p->trace, p->nRef));
28054 }
28055#endif
28056}
28057
28058SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
28059 static const sqlite3_mutex_methods sMutex = {
28060 winMutexInit,
28061 winMutexEnd,
28062 winMutexAlloc,
28063 winMutexFree,
28064 winMutexEnter,
28065 winMutexTry,
28066 winMutexLeave,
28067#ifdef SQLITE_DEBUG
28068 winMutexHeld,
28069 winMutexNotheld
28070#else
28071 0,
28072 0
28073#endif
28074 };
28075 return &sMutex;
28076}
28077
28078#endif /* SQLITE_MUTEX_W32 */
28079
28080/************** End of mutex_w32.c *******************************************/
28081/************** Begin file malloc.c ******************************************/
28082/*
28083** 2001 September 15
28084**
28085** The author disclaims copyright to this source code. In place of
28086** a legal notice, here is a blessing:
28087**
28088** May you do good and not evil.
28089** May you find forgiveness for yourself and forgive others.
28090** May you share freely, never taking more than you give.
28091**
28092*************************************************************************
28093**
28094** Memory allocation functions used throughout sqlite.
28095*/
28096/* #include "sqliteInt.h" */
28097/* #include <stdarg.h> */
28098
28099/*
28100** Attempt to release up to n bytes of non-essential memory currently
28101** held by SQLite. An example of non-essential memory is memory used to
28102** cache database pages that are not currently in use.
28103*/
28104SQLITE_API int sqlite3_release_memory(int n){
28105#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
28106 return sqlite3PcacheReleaseMemory(n);
28107#else
28108 /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
28109 ** is a no-op returning zero if SQLite is not compiled with
28110 ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */
28111 UNUSED_PARAMETER(n);
28112 return 0;
28113#endif
28114}
28115
28116/*
28117** Default value of the hard heap limit. 0 means "no limit".
28118*/
28119#ifndef SQLITE_MAX_MEMORY
28120# define SQLITE_MAX_MEMORY 0
28121#endif
28122
28123/*
28124** State information local to the memory allocation subsystem.
28125*/
28126static SQLITE_WSD struct Mem0Global {
28127 sqlite3_mutex *mutex; /* Mutex to serialize access */
28128 sqlite3_int64 alarmThreshold; /* The soft heap limit */
28129 sqlite3_int64 hardLimit; /* The hard upper bound on memory */
28130
28131 /*
28132 ** True if heap is nearly "full" where "full" is defined by the
28133 ** sqlite3_soft_heap_limit() setting.
28134 */
28135 int nearlyFull;
28136} mem0 = { 0, SQLITE_MAX_MEMORY, SQLITE_MAX_MEMORY, 0 };
28137
28138#define mem0 GLOBAL(struct Mem0Global, mem0)
28139
28140/*
28141** Return the memory allocator mutex. sqlite3_status() needs it.
28142*/
28143SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void){
28144 return mem0.mutex;
28145}
28146
28147#ifndef SQLITE_OMIT_DEPRECATED
28148/*
28149** Deprecated external interface. It used to set an alarm callback
28150** that was invoked when memory usage grew too large. Now it is a
28151** no-op.
28152*/
28153SQLITE_API int sqlite3_memory_alarm(
28154 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
28155 void *pArg,
28156 sqlite3_int64 iThreshold
28157){
28158 (void)xCallback;
28159 (void)pArg;
28160 (void)iThreshold;
28161 return SQLITE_OK;
28162}
28163#endif
28164
28165/*
28166** Set the soft heap-size limit for the library. An argument of
28167** zero disables the limit. A negative argument is a no-op used to
28168** obtain the return value.
28169**
28170** The return value is the value of the heap limit just before this
28171** interface was called.
28172**
28173** If the hard heap limit is enabled, then the soft heap limit cannot
28174** be disabled nor raised above the hard heap limit.
28175*/
28176SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
28177 sqlite3_int64 priorLimit;
28178 sqlite3_int64 excess;
28179 sqlite3_int64 nUsed;
28180#ifndef SQLITE_OMIT_AUTOINIT
28181 int rc = sqlite3_initialize();
28182 if( rc ) return -1;
28183#endif
28184 sqlite3_mutex_enter(mem0.mutex);
28185 priorLimit = mem0.alarmThreshold;
28186 if( n<0 ){
28187 sqlite3_mutex_leave(mem0.mutex);
28188 return priorLimit;
28189 }
28190 if( mem0.hardLimit>0 && (n>mem0.hardLimit || n==0) ){
28191 n = mem0.hardLimit;
28192 }
28193 mem0.alarmThreshold = n;
28194 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
28195 AtomicStore(&mem0.nearlyFull, n>0 && n<=nUsed);
28196 sqlite3_mutex_leave(mem0.mutex);
28197 excess = sqlite3_memory_used() - n;
28198 if( excess>0 ) sqlite3_release_memory(n: (int)(excess & 0x7fffffff));
28199 return priorLimit;
28200}
28201SQLITE_API void sqlite3_soft_heap_limit(int n){
28202 if( n<0 ) n = 0;
28203 sqlite3_soft_heap_limit64(n);
28204}
28205
28206/*
28207** Set the hard heap-size limit for the library. An argument of zero
28208** disables the hard heap limit. A negative argument is a no-op used
28209** to obtain the return value without affecting the hard heap limit.
28210**
28211** The return value is the value of the hard heap limit just prior to
28212** calling this interface.
28213**
28214** Setting the hard heap limit will also activate the soft heap limit
28215** and constrain the soft heap limit to be no more than the hard heap
28216** limit.
28217*/
28218SQLITE_API sqlite3_int64 sqlite3_hard_heap_limit64(sqlite3_int64 n){
28219 sqlite3_int64 priorLimit;
28220#ifndef SQLITE_OMIT_AUTOINIT
28221 int rc = sqlite3_initialize();
28222 if( rc ) return -1;
28223#endif
28224 sqlite3_mutex_enter(mem0.mutex);
28225 priorLimit = mem0.hardLimit;
28226 if( n>=0 ){
28227 mem0.hardLimit = n;
28228 if( n<mem0.alarmThreshold || mem0.alarmThreshold==0 ){
28229 mem0.alarmThreshold = n;
28230 }
28231 }
28232 sqlite3_mutex_leave(mem0.mutex);
28233 return priorLimit;
28234}
28235
28236
28237/*
28238** Initialize the memory allocation subsystem.
28239*/
28240SQLITE_PRIVATE int sqlite3MallocInit(void){
28241 int rc;
28242 if( sqlite3GlobalConfig.m.xMalloc==0 ){
28243 sqlite3MemSetDefault();
28244 }
28245 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
28246 if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
28247 || sqlite3GlobalConfig.nPage<=0 ){
28248 sqlite3GlobalConfig.pPage = 0;
28249 sqlite3GlobalConfig.szPage = 0;
28250 }
28251 rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
28252 if( rc!=SQLITE_OK ) memset(s: &mem0, c: 0, n: sizeof(mem0));
28253 return rc;
28254}
28255
28256/*
28257** Return true if the heap is currently under memory pressure - in other
28258** words if the amount of heap used is close to the limit set by
28259** sqlite3_soft_heap_limit().
28260*/
28261SQLITE_PRIVATE int sqlite3HeapNearlyFull(void){
28262 return AtomicLoad(&mem0.nearlyFull);
28263}
28264
28265/*
28266** Deinitialize the memory allocation subsystem.
28267*/
28268SQLITE_PRIVATE void sqlite3MallocEnd(void){
28269 if( sqlite3GlobalConfig.m.xShutdown ){
28270 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
28271 }
28272 memset(s: &mem0, c: 0, n: sizeof(mem0));
28273}
28274
28275/*
28276** Return the amount of memory currently checked out.
28277*/
28278SQLITE_API sqlite3_int64 sqlite3_memory_used(void){
28279 sqlite3_int64 res, mx;
28280 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, pCurrent: &res, pHighwater: &mx, resetFlag: 0);
28281 return res;
28282}
28283
28284/*
28285** Return the maximum amount of memory that has ever been
28286** checked out since either the beginning of this process
28287** or since the most recent reset.
28288*/
28289SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
28290 sqlite3_int64 res, mx;
28291 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, pCurrent: &res, pHighwater: &mx, resetFlag);
28292 return mx;
28293}
28294
28295/*
28296** Trigger the alarm
28297*/
28298static void sqlite3MallocAlarm(int nByte){
28299 if( mem0.alarmThreshold<=0 ) return;
28300 sqlite3_mutex_leave(mem0.mutex);
28301 sqlite3_release_memory(n: nByte);
28302 sqlite3_mutex_enter(mem0.mutex);
28303}
28304
28305/*
28306** Do a memory allocation with statistics and alarms. Assume the
28307** lock is already held.
28308*/
28309static void mallocWithAlarm(int n, void **pp){
28310 void *p;
28311 int nFull;
28312 assert( sqlite3_mutex_held(mem0.mutex) );
28313 assert( n>0 );
28314
28315 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
28316 ** implementation of malloc_good_size(), which must be called in debug
28317 ** mode and specifically when the DMD "Dark Matter Detector" is enabled
28318 ** or else a crash results. Hence, do not attempt to optimize out the
28319 ** following xRoundup() call. */
28320 nFull = sqlite3GlobalConfig.m.xRoundup(n);
28321
28322 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, X: n);
28323 if( mem0.alarmThreshold>0 ){
28324 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
28325 if( nUsed >= mem0.alarmThreshold - nFull ){
28326 AtomicStore(&mem0.nearlyFull, 1);
28327 sqlite3MallocAlarm(nByte: nFull);
28328 if( mem0.hardLimit ){
28329 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
28330 if( nUsed >= mem0.hardLimit - nFull ){
28331 *pp = 0;
28332 return;
28333 }
28334 }
28335 }else{
28336 AtomicStore(&mem0.nearlyFull, 0);
28337 }
28338 }
28339 p = sqlite3GlobalConfig.m.xMalloc(nFull);
28340#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
28341 if( p==0 && mem0.alarmThreshold>0 ){
28342 sqlite3MallocAlarm(nFull);
28343 p = sqlite3GlobalConfig.m.xMalloc(nFull);
28344 }
28345#endif
28346 if( p ){
28347 nFull = sqlite3MallocSize(p);
28348 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, N: nFull);
28349 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, N: 1);
28350 }
28351 *pp = p;
28352}
28353
28354/*
28355** Allocate memory. This routine is like sqlite3_malloc() except that it
28356** assumes the memory subsystem has already been initialized.
28357*/
28358SQLITE_PRIVATE void *sqlite3Malloc(u64 n){
28359 void *p;
28360 if( n==0 || n>=0x7fffff00 ){
28361 /* A memory allocation of a number of bytes which is near the maximum
28362 ** signed integer value might cause an integer overflow inside of the
28363 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
28364 ** 255 bytes of overhead. SQLite itself will never use anything near
28365 ** this amount. The only way to reach the limit is with sqlite3_malloc() */
28366 p = 0;
28367 }else if( sqlite3GlobalConfig.bMemstat ){
28368 sqlite3_mutex_enter(mem0.mutex);
28369 mallocWithAlarm(n: (int)n, pp: &p);
28370 sqlite3_mutex_leave(mem0.mutex);
28371 }else{
28372 p = sqlite3GlobalConfig.m.xMalloc((int)n);
28373 }
28374 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
28375 return p;
28376}
28377
28378/*
28379** This version of the memory allocation is for use by the application.
28380** First make sure the memory subsystem is initialized, then do the
28381** allocation.
28382*/
28383SQLITE_API void *sqlite3_malloc(int n){
28384#ifndef SQLITE_OMIT_AUTOINIT
28385 if( sqlite3_initialize() ) return 0;
28386#endif
28387 return n<=0 ? 0 : sqlite3Malloc(n);
28388}
28389SQLITE_API void *sqlite3_malloc64(sqlite3_uint64 n){
28390#ifndef SQLITE_OMIT_AUTOINIT
28391 if( sqlite3_initialize() ) return 0;
28392#endif
28393 return sqlite3Malloc(n);
28394}
28395
28396/*
28397** TRUE if p is a lookaside memory allocation from db
28398*/
28399#ifndef SQLITE_OMIT_LOOKASIDE
28400static int isLookaside(sqlite3 *db, const void *p){
28401 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd);
28402}
28403#else
28404#define isLookaside(A,B) 0
28405#endif
28406
28407/*
28408** Return the size of a memory allocation previously obtained from
28409** sqlite3Malloc() or sqlite3_malloc().
28410*/
28411SQLITE_PRIVATE int sqlite3MallocSize(const void *p){
28412 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
28413 return sqlite3GlobalConfig.m.xSize((void*)p);
28414}
28415static int lookasideMallocSize(sqlite3 *db, const void *p){
28416#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
28417 return p<db->lookaside.pMiddle ? db->lookaside.szTrue : LOOKASIDE_SMALL;
28418#else
28419 return db->lookaside.szTrue;
28420#endif
28421}
28422SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, const void *p){
28423 assert( p!=0 );
28424#ifdef SQLITE_DEBUG
28425 if( db==0 || !isLookaside(db,p) ){
28426 if( db==0 ){
28427 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
28428 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
28429 }else{
28430 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
28431 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
28432 }
28433 }
28434#endif
28435 if( db ){
28436 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
28437#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
28438 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
28439 assert( sqlite3_mutex_held(db->mutex) );
28440 return LOOKASIDE_SMALL;
28441 }
28442#endif
28443 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
28444 assert( sqlite3_mutex_held(db->mutex) );
28445 return db->lookaside.szTrue;
28446 }
28447 }
28448 }
28449 return sqlite3GlobalConfig.m.xSize((void*)p);
28450}
28451SQLITE_API sqlite3_uint64 sqlite3_msize(void *p){
28452 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
28453 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
28454 return p ? sqlite3GlobalConfig.m.xSize(p) : 0;
28455}
28456
28457/*
28458** Free memory previously obtained from sqlite3Malloc().
28459*/
28460SQLITE_API void sqlite3_free(void *p){
28461 if( p==0 ) return; /* IMP: R-49053-54554 */
28462 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
28463 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
28464 if( sqlite3GlobalConfig.bMemstat ){
28465 sqlite3_mutex_enter(mem0.mutex);
28466 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, N: sqlite3MallocSize(p));
28467 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, N: 1);
28468 sqlite3GlobalConfig.m.xFree(p);
28469 sqlite3_mutex_leave(mem0.mutex);
28470 }else{
28471 sqlite3GlobalConfig.m.xFree(p);
28472 }
28473}
28474
28475/*
28476** Add the size of memory allocation "p" to the count in
28477** *db->pnBytesFreed.
28478*/
28479static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
28480 *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
28481}
28482
28483/*
28484** Free memory that might be associated with a particular database
28485** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
28486** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
28487*/
28488SQLITE_PRIVATE void sqlite3DbFreeNN(sqlite3 *db, void *p){
28489 assert( db==0 || sqlite3_mutex_held(db->mutex) );
28490 assert( p!=0 );
28491 if( db ){
28492 if( db->pnBytesFreed ){
28493 measureAllocationSize(db, p);
28494 return;
28495 }
28496 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
28497#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
28498 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
28499 LookasideSlot *pBuf = (LookasideSlot*)p;
28500#ifdef SQLITE_DEBUG
28501 memset(p, 0xaa, LOOKASIDE_SMALL); /* Trash freed content */
28502#endif
28503 pBuf->pNext = db->lookaside.pSmallFree;
28504 db->lookaside.pSmallFree = pBuf;
28505 return;
28506 }
28507#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
28508 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
28509 LookasideSlot *pBuf = (LookasideSlot*)p;
28510#ifdef SQLITE_DEBUG
28511 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */
28512#endif
28513 pBuf->pNext = db->lookaside.pFree;
28514 db->lookaside.pFree = pBuf;
28515 return;
28516 }
28517 }
28518 }
28519 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
28520 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
28521 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
28522 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
28523 sqlite3_free(p);
28524}
28525SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){
28526 assert( db==0 || sqlite3_mutex_held(db->mutex) );
28527 if( p ) sqlite3DbFreeNN(db, p);
28528}
28529
28530/*
28531** Change the size of an existing memory allocation
28532*/
28533SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, u64 nBytes){
28534 int nOld, nNew, nDiff;
28535 void *pNew;
28536 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
28537 assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
28538 if( pOld==0 ){
28539 return sqlite3Malloc(n: nBytes); /* IMP: R-04300-56712 */
28540 }
28541 if( nBytes==0 ){
28542 sqlite3_free(p: pOld); /* IMP: R-26507-47431 */
28543 return 0;
28544 }
28545 if( nBytes>=0x7fffff00 ){
28546 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
28547 return 0;
28548 }
28549 nOld = sqlite3MallocSize(p: pOld);
28550 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
28551 ** argument to xRealloc is always a value returned by a prior call to
28552 ** xRoundup. */
28553 nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
28554 if( nOld==nNew ){
28555 pNew = pOld;
28556 }else if( sqlite3GlobalConfig.bMemstat ){
28557 sqlite3_int64 nUsed;
28558 sqlite3_mutex_enter(mem0.mutex);
28559 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, X: (int)nBytes);
28560 nDiff = nNew - nOld;
28561 if( nDiff>0 && (nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)) >=
28562 mem0.alarmThreshold-nDiff ){
28563 sqlite3MallocAlarm(nByte: nDiff);
28564 if( mem0.hardLimit>0 && nUsed >= mem0.hardLimit - nDiff ){
28565 sqlite3_mutex_leave(mem0.mutex);
28566 return 0;
28567 }
28568 }
28569 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
28570#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
28571 if( pNew==0 && mem0.alarmThreshold>0 ){
28572 sqlite3MallocAlarm((int)nBytes);
28573 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
28574 }
28575#endif
28576 if( pNew ){
28577 nNew = sqlite3MallocSize(p: pNew);
28578 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, N: nNew-nOld);
28579 }
28580 sqlite3_mutex_leave(mem0.mutex);
28581 }else{
28582 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
28583 }
28584 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
28585 return pNew;
28586}
28587
28588/*
28589** The public interface to sqlite3Realloc. Make sure that the memory
28590** subsystem is initialized prior to invoking sqliteRealloc.
28591*/
28592SQLITE_API void *sqlite3_realloc(void *pOld, int n){
28593#ifndef SQLITE_OMIT_AUTOINIT
28594 if( sqlite3_initialize() ) return 0;
28595#endif
28596 if( n<0 ) n = 0; /* IMP: R-26507-47431 */
28597 return sqlite3Realloc(pOld, nBytes: n);
28598}
28599SQLITE_API void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
28600#ifndef SQLITE_OMIT_AUTOINIT
28601 if( sqlite3_initialize() ) return 0;
28602#endif
28603 return sqlite3Realloc(pOld, nBytes: n);
28604}
28605
28606
28607/*
28608** Allocate and zero memory.
28609*/
28610SQLITE_PRIVATE void *sqlite3MallocZero(u64 n){
28611 void *p = sqlite3Malloc(n);
28612 if( p ){
28613 memset(s: p, c: 0, n: (size_t)n);
28614 }
28615 return p;
28616}
28617
28618/*
28619** Allocate and zero memory. If the allocation fails, make
28620** the mallocFailed flag in the connection pointer.
28621*/
28622SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
28623 void *p;
28624 testcase( db==0 );
28625 p = sqlite3DbMallocRaw(db, n);
28626 if( p ) memset(s: p, c: 0, n: (size_t)n);
28627 return p;
28628}
28629
28630
28631/* Finish the work of sqlite3DbMallocRawNN for the unusual and
28632** slower case when the allocation cannot be fulfilled using lookaside.
28633*/
28634static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){
28635 void *p;
28636 assert( db!=0 );
28637 p = sqlite3Malloc(n);
28638 if( !p ) sqlite3OomFault(db);
28639 sqlite3MemdebugSetType(p,
28640 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
28641 return p;
28642}
28643
28644/*
28645** Allocate memory, either lookaside (if possible) or heap.
28646** If the allocation fails, set the mallocFailed flag in
28647** the connection pointer.
28648**
28649** If db!=0 and db->mallocFailed is true (indicating a prior malloc
28650** failure on the same database connection) then always return 0.
28651** Hence for a particular database connection, once malloc starts
28652** failing, it fails consistently until mallocFailed is reset.
28653** This is an important assumption. There are many places in the
28654** code that do things like this:
28655**
28656** int *a = (int*)sqlite3DbMallocRaw(db, 100);
28657** int *b = (int*)sqlite3DbMallocRaw(db, 200);
28658** if( b ) a[10] = 9;
28659**
28660** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
28661** that all prior mallocs (ex: "a") worked too.
28662**
28663** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
28664** not a NULL pointer.
28665*/
28666SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
28667 void *p;
28668 if( db ) return sqlite3DbMallocRawNN(db, n);
28669 p = sqlite3Malloc(n);
28670 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
28671 return p;
28672}
28673SQLITE_PRIVATE void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){
28674#ifndef SQLITE_OMIT_LOOKASIDE
28675 LookasideSlot *pBuf;
28676 assert( db!=0 );
28677 assert( sqlite3_mutex_held(db->mutex) );
28678 assert( db->pnBytesFreed==0 );
28679 if( n>db->lookaside.sz ){
28680 if( !db->lookaside.bDisable ){
28681 db->lookaside.anStat[1]++;
28682 }else if( db->mallocFailed ){
28683 return 0;
28684 }
28685 return dbMallocRawFinish(db, n);
28686 }
28687#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
28688 if( n<=LOOKASIDE_SMALL ){
28689 if( (pBuf = db->lookaside.pSmallFree)!=0 ){
28690 db->lookaside.pSmallFree = pBuf->pNext;
28691 db->lookaside.anStat[0]++;
28692 return (void*)pBuf;
28693 }else if( (pBuf = db->lookaside.pSmallInit)!=0 ){
28694 db->lookaside.pSmallInit = pBuf->pNext;
28695 db->lookaside.anStat[0]++;
28696 return (void*)pBuf;
28697 }
28698 }
28699#endif
28700 if( (pBuf = db->lookaside.pFree)!=0 ){
28701 db->lookaside.pFree = pBuf->pNext;
28702 db->lookaside.anStat[0]++;
28703 return (void*)pBuf;
28704 }else if( (pBuf = db->lookaside.pInit)!=0 ){
28705 db->lookaside.pInit = pBuf->pNext;
28706 db->lookaside.anStat[0]++;
28707 return (void*)pBuf;
28708 }else{
28709 db->lookaside.anStat[2]++;
28710 }
28711#else
28712 assert( db!=0 );
28713 assert( sqlite3_mutex_held(db->mutex) );
28714 assert( db->pnBytesFreed==0 );
28715 if( db->mallocFailed ){
28716 return 0;
28717 }
28718#endif
28719 return dbMallocRawFinish(db, n);
28720}
28721
28722/* Forward declaration */
28723static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n);
28724
28725/*
28726** Resize the block of memory pointed to by p to n bytes. If the
28727** resize fails, set the mallocFailed flag in the connection object.
28728*/
28729SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
28730 assert( db!=0 );
28731 if( p==0 ) return sqlite3DbMallocRawNN(db, n);
28732 assert( sqlite3_mutex_held(db->mutex) );
28733 if( ((uptr)p)<(uptr)db->lookaside.pEnd ){
28734#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
28735 if( ((uptr)p)>=(uptr)db->lookaside.pMiddle ){
28736 if( n<=LOOKASIDE_SMALL ) return p;
28737 }else
28738#endif
28739 if( ((uptr)p)>=(uptr)db->lookaside.pStart ){
28740 if( n<=db->lookaside.szTrue ) return p;
28741 }
28742 }
28743 return dbReallocFinish(db, p, n);
28744}
28745static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){
28746 void *pNew = 0;
28747 assert( db!=0 );
28748 assert( p!=0 );
28749 if( db->mallocFailed==0 ){
28750 if( isLookaside(db, p) ){
28751 pNew = sqlite3DbMallocRawNN(db, n);
28752 if( pNew ){
28753 memcpy(dest: pNew, src: p, n: lookasideMallocSize(db, p));
28754 sqlite3DbFree(db, p);
28755 }
28756 }else{
28757 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
28758 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
28759 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
28760 pNew = sqlite3Realloc(pOld: p, nBytes: n);
28761 if( !pNew ){
28762 sqlite3OomFault(db);
28763 }
28764 sqlite3MemdebugSetType(pNew,
28765 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
28766 }
28767 }
28768 return pNew;
28769}
28770
28771/*
28772** Attempt to reallocate p. If the reallocation fails, then free p
28773** and set the mallocFailed flag in the database connection.
28774*/
28775SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
28776 void *pNew;
28777 pNew = sqlite3DbRealloc(db, p, n);
28778 if( !pNew ){
28779 sqlite3DbFree(db, p);
28780 }
28781 return pNew;
28782}
28783
28784/*
28785** Make a copy of a string in memory obtained from sqliteMalloc(). These
28786** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
28787** is because when memory debugging is turned on, these two functions are
28788** called via macros that record the current file and line number in the
28789** ThreadData structure.
28790*/
28791SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3 *db, const char *z){
28792 char *zNew;
28793 size_t n;
28794 if( z==0 ){
28795 return 0;
28796 }
28797 n = strlen(s: z) + 1;
28798 zNew = sqlite3DbMallocRaw(db, n);
28799 if( zNew ){
28800 memcpy(dest: zNew, src: z, n: n);
28801 }
28802 return zNew;
28803}
28804SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
28805 char *zNew;
28806 assert( db!=0 );
28807 assert( z!=0 || n==0 );
28808 assert( (n&0x7fffffff)==n );
28809 zNew = z ? sqlite3DbMallocRawNN(db, n: n+1) : 0;
28810 if( zNew ){
28811 memcpy(dest: zNew, src: z, n: (size_t)n);
28812 zNew[n] = 0;
28813 }
28814 return zNew;
28815}
28816
28817/*
28818** The text between zStart and zEnd represents a phrase within a larger
28819** SQL statement. Make a copy of this phrase in space obtained form
28820** sqlite3DbMalloc(). Omit leading and trailing whitespace.
28821*/
28822SQLITE_PRIVATE char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
28823 int n;
28824 while( sqlite3Isspace(zStart[0]) ) zStart++;
28825 n = (int)(zEnd - zStart);
28826 while( ALWAYS(n>0) && sqlite3Isspace(zStart[n-1]) ) n--;
28827 return sqlite3DbStrNDup(db, z: zStart, n);
28828}
28829
28830/*
28831** Free any prior content in *pz and replace it with a copy of zNew.
28832*/
28833SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
28834 sqlite3DbFree(db, p: *pz);
28835 *pz = sqlite3DbStrDup(db, z: zNew);
28836}
28837
28838/*
28839** Call this routine to record the fact that an OOM (out-of-memory) error
28840** has happened. This routine will set db->mallocFailed, and also
28841** temporarily disable the lookaside memory allocator and interrupt
28842** any running VDBEs.
28843*/
28844SQLITE_PRIVATE void sqlite3OomFault(sqlite3 *db){
28845 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
28846 db->mallocFailed = 1;
28847 if( db->nVdbeExec>0 ){
28848 AtomicStore(&db->u1.isInterrupted, 1);
28849 }
28850 DisableLookaside;
28851 if( db->pParse ){
28852 db->pParse->rc = SQLITE_NOMEM_BKPT;
28853 }
28854 }
28855}
28856
28857/*
28858** This routine reactivates the memory allocator and clears the
28859** db->mallocFailed flag as necessary.
28860**
28861** The memory allocator is not restarted if there are running
28862** VDBEs.
28863*/
28864SQLITE_PRIVATE void sqlite3OomClear(sqlite3 *db){
28865 if( db->mallocFailed && db->nVdbeExec==0 ){
28866 db->mallocFailed = 0;
28867 AtomicStore(&db->u1.isInterrupted, 0);
28868 assert( db->lookaside.bDisable>0 );
28869 EnableLookaside;
28870 }
28871}
28872
28873/*
28874** Take actions at the end of an API call to deal with error codes.
28875*/
28876static SQLITE_NOINLINE int apiHandleError(sqlite3 *db, int rc){
28877 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
28878 sqlite3OomClear(db);
28879 sqlite3Error(db, SQLITE_NOMEM);
28880 return SQLITE_NOMEM_BKPT;
28881 }
28882 return rc & db->errMask;
28883}
28884
28885/*
28886** This function must be called before exiting any API function (i.e.
28887** returning control to the user) that has called sqlite3_malloc or
28888** sqlite3_realloc.
28889**
28890** The returned value is normally a copy of the second argument to this
28891** function. However, if a malloc() failure has occurred since the previous
28892** invocation SQLITE_NOMEM is returned instead.
28893**
28894** If an OOM as occurred, then the connection error-code (the value
28895** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
28896*/
28897SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){
28898 /* If the db handle must hold the connection handle mutex here.
28899 ** Otherwise the read (and possible write) of db->mallocFailed
28900 ** is unsafe, as is the call to sqlite3Error().
28901 */
28902 assert( db!=0 );
28903 assert( sqlite3_mutex_held(db->mutex) );
28904 if( db->mallocFailed || rc ){
28905 return apiHandleError(db, rc);
28906 }
28907 return rc & db->errMask;
28908}
28909
28910/************** End of malloc.c **********************************************/
28911/************** Begin file printf.c ******************************************/
28912/*
28913** The "printf" code that follows dates from the 1980's. It is in
28914** the public domain.
28915**
28916**************************************************************************
28917**
28918** This file contains code for a set of "printf"-like routines. These
28919** routines format strings much like the printf() from the standard C
28920** library, though the implementation here has enhancements to support
28921** SQLite.
28922*/
28923/* #include "sqliteInt.h" */
28924
28925/*
28926** Conversion types fall into various categories as defined by the
28927** following enumeration.
28928*/
28929#define etRADIX 0 /* non-decimal integer types. %x %o */
28930#define etFLOAT 1 /* Floating point. %f */
28931#define etEXP 2 /* Exponentional notation. %e and %E */
28932#define etGENERIC 3 /* Floating or exponential, depending on exponent. %g */
28933#define etSIZE 4 /* Return number of characters processed so far. %n */
28934#define etSTRING 5 /* Strings. %s */
28935#define etDYNSTRING 6 /* Dynamically allocated strings. %z */
28936#define etPERCENT 7 /* Percent symbol. %% */
28937#define etCHARX 8 /* Characters. %c */
28938/* The rest are extensions, not normally found in printf() */
28939#define etSQLESCAPE 9 /* Strings with '\'' doubled. %q */
28940#define etSQLESCAPE2 10 /* Strings with '\'' doubled and enclosed in '',
28941 NULL pointers replaced by SQL NULL. %Q */
28942#define etTOKEN 11 /* a pointer to a Token structure */
28943#define etSRCITEM 12 /* a pointer to a SrcItem */
28944#define etPOINTER 13 /* The %p conversion */
28945#define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */
28946#define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
28947#define etDECIMAL 16 /* %d or %u, but not %x, %o */
28948
28949#define etINVALID 17 /* Any unrecognized conversion type */
28950
28951
28952/*
28953** An "etByte" is an 8-bit unsigned value.
28954*/
28955typedef unsigned char etByte;
28956
28957/*
28958** Each builtin conversion character (ex: the 'd' in "%d") is described
28959** by an instance of the following structure
28960*/
28961typedef struct et_info { /* Information about each format field */
28962 char fmttype; /* The format field code letter */
28963 etByte base; /* The base for radix conversion */
28964 etByte flags; /* One or more of FLAG_ constants below */
28965 etByte type; /* Conversion paradigm */
28966 etByte charset; /* Offset into aDigits[] of the digits string */
28967 etByte prefix; /* Offset into aPrefix[] of the prefix string */
28968} et_info;
28969
28970/*
28971** Allowed values for et_info.flags
28972*/
28973#define FLAG_SIGNED 1 /* True if the value to convert is signed */
28974#define FLAG_STRING 4 /* Allow infinite precision */
28975
28976
28977/*
28978** The following table is searched linearly, so it is good to put the
28979** most frequently used conversion types first.
28980*/
28981static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
28982static const char aPrefix[] = "-x0\000X0";
28983static const et_info fmtinfo[] = {
28984 { 'd', 10, 1, etDECIMAL, 0, 0 },
28985 { 's', 0, 4, etSTRING, 0, 0 },
28986 { 'g', 0, 1, etGENERIC, 30, 0 },
28987 { 'z', 0, 4, etDYNSTRING, 0, 0 },
28988 { 'q', 0, 4, etSQLESCAPE, 0, 0 },
28989 { 'Q', 0, 4, etSQLESCAPE2, 0, 0 },
28990 { 'w', 0, 4, etSQLESCAPE3, 0, 0 },
28991 { 'c', 0, 0, etCHARX, 0, 0 },
28992 { 'o', 8, 0, etRADIX, 0, 2 },
28993 { 'u', 10, 0, etDECIMAL, 0, 0 },
28994 { 'x', 16, 0, etRADIX, 16, 1 },
28995 { 'X', 16, 0, etRADIX, 0, 4 },
28996#ifndef SQLITE_OMIT_FLOATING_POINT
28997 { 'f', 0, 1, etFLOAT, 0, 0 },
28998 { 'e', 0, 1, etEXP, 30, 0 },
28999 { 'E', 0, 1, etEXP, 14, 0 },
29000 { 'G', 0, 1, etGENERIC, 14, 0 },
29001#endif
29002 { 'i', 10, 1, etDECIMAL, 0, 0 },
29003 { 'n', 0, 0, etSIZE, 0, 0 },
29004 { '%', 0, 0, etPERCENT, 0, 0 },
29005 { 'p', 16, 0, etPOINTER, 0, 1 },
29006
29007 /* All the rest are undocumented and are for internal use only */
29008 { 'T', 0, 0, etTOKEN, 0, 0 },
29009 { 'S', 0, 0, etSRCITEM, 0, 0 },
29010 { 'r', 10, 1, etORDINAL, 0, 0 },
29011};
29012
29013/* Notes:
29014**
29015** %S Takes a pointer to SrcItem. Shows name or database.name
29016** %!S Like %S but prefer the zName over the zAlias
29017*/
29018
29019/* Floating point constants used for rounding */
29020static const double arRound[] = {
29021 5.0e-01, 5.0e-02, 5.0e-03, 5.0e-04, 5.0e-05,
29022 5.0e-06, 5.0e-07, 5.0e-08, 5.0e-09, 5.0e-10,
29023};
29024
29025/*
29026** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
29027** conversions will work.
29028*/
29029#ifndef SQLITE_OMIT_FLOATING_POINT
29030/*
29031** "*val" is a double such that 0.1 <= *val < 10.0
29032** Return the ascii code for the leading digit of *val, then
29033** multiply "*val" by 10.0 to renormalize.
29034**
29035** Example:
29036** input: *val = 3.14159
29037** output: *val = 1.4159 function return = '3'
29038**
29039** The counter *cnt is incremented each time. After counter exceeds
29040** 16 (the number of significant digits in a 64-bit float) '0' is
29041** always returned.
29042*/
29043static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
29044 int digit;
29045 LONGDOUBLE_TYPE d;
29046 if( (*cnt)<=0 ) return '0';
29047 (*cnt)--;
29048 digit = (int)*val;
29049 d = digit;
29050 digit += '0';
29051 *val = (*val - d)*10.0;
29052 return (char)digit;
29053}
29054#endif /* SQLITE_OMIT_FLOATING_POINT */
29055
29056/*
29057** Set the StrAccum object to an error mode.
29058*/
29059SQLITE_PRIVATE void sqlite3StrAccumSetError(StrAccum *p, u8 eError){
29060 assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG );
29061 p->accError = eError;
29062 if( p->mxAlloc ) sqlite3_str_reset(p);
29063 if( eError==SQLITE_TOOBIG ) sqlite3ErrorToParser(p->db, eError);
29064}
29065
29066/*
29067** Extra argument values from a PrintfArguments object
29068*/
29069static sqlite3_int64 getIntArg(PrintfArguments *p){
29070 if( p->nArg<=p->nUsed ) return 0;
29071 return sqlite3_value_int64(p->apArg[p->nUsed++]);
29072}
29073static double getDoubleArg(PrintfArguments *p){
29074 if( p->nArg<=p->nUsed ) return 0.0;
29075 return sqlite3_value_double(p->apArg[p->nUsed++]);
29076}
29077static char *getTextArg(PrintfArguments *p){
29078 if( p->nArg<=p->nUsed ) return 0;
29079 return (char*)sqlite3_value_text(p->apArg[p->nUsed++]);
29080}
29081
29082/*
29083** Allocate memory for a temporary buffer needed for printf rendering.
29084**
29085** If the requested size of the temp buffer is larger than the size
29086** of the output buffer in pAccum, then cause an SQLITE_TOOBIG error.
29087** Do the size check before the memory allocation to prevent rogue
29088** SQL from requesting large allocations using the precision or width
29089** field of the printf() function.
29090*/
29091static char *printfTempBuf(sqlite3_str *pAccum, sqlite3_int64 n){
29092 char *z;
29093 if( pAccum->accError ) return 0;
29094 if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){
29095 sqlite3StrAccumSetError(p: pAccum, SQLITE_TOOBIG);
29096 return 0;
29097 }
29098 z = sqlite3DbMallocRaw(db: pAccum->db, n);
29099 if( z==0 ){
29100 sqlite3StrAccumSetError(p: pAccum, SQLITE_NOMEM);
29101 }
29102 return z;
29103}
29104
29105/*
29106** On machines with a small stack size, you can redefine the
29107** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
29108*/
29109#ifndef SQLITE_PRINT_BUF_SIZE
29110# define SQLITE_PRINT_BUF_SIZE 70
29111#endif
29112#define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */
29113
29114/*
29115** Hard limit on the precision of floating-point conversions.
29116*/
29117#ifndef SQLITE_PRINTF_PRECISION_LIMIT
29118# define SQLITE_FP_PRECISION_LIMIT 100000000
29119#endif
29120
29121/*
29122** Render a string given by "fmt" into the StrAccum object.
29123*/
29124SQLITE_API void sqlite3_str_vappendf(
29125 sqlite3_str *pAccum, /* Accumulate results here */
29126 const char *fmt, /* Format string */
29127 va_list ap /* arguments */
29128){
29129 int c; /* Next character in the format string */
29130 char *bufpt; /* Pointer to the conversion buffer */
29131 int precision; /* Precision of the current field */
29132 int length; /* Length of the field */
29133 int idx; /* A general purpose loop counter */
29134 int width; /* Width of the current field */
29135 etByte flag_leftjustify; /* True if "-" flag is present */
29136 etByte flag_prefix; /* '+' or ' ' or 0 for prefix */
29137 etByte flag_alternateform; /* True if "#" flag is present */
29138 etByte flag_altform2; /* True if "!" flag is present */
29139 etByte flag_zeropad; /* True if field width constant starts with zero */
29140 etByte flag_long; /* 1 for the "l" flag, 2 for "ll", 0 by default */
29141 etByte done; /* Loop termination flag */
29142 etByte cThousand; /* Thousands separator for %d and %u */
29143 etByte xtype = etINVALID; /* Conversion paradigm */
29144 u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */
29145 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
29146 sqlite_uint64 longvalue; /* Value for integer types */
29147 LONGDOUBLE_TYPE realvalue; /* Value for real types */
29148 const et_info *infop; /* Pointer to the appropriate info structure */
29149 char *zOut; /* Rendering buffer */
29150 int nOut; /* Size of the rendering buffer */
29151 char *zExtra = 0; /* Malloced memory used by some conversion */
29152#ifndef SQLITE_OMIT_FLOATING_POINT
29153 int exp, e2; /* exponent of real numbers */
29154 int nsd; /* Number of significant digits returned */
29155 double rounder; /* Used for rounding floating point values */
29156 etByte flag_dp; /* True if decimal point should be shown */
29157 etByte flag_rtz; /* True if trailing zeros should be removed */
29158#endif
29159 PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
29160 char buf[etBUFSIZE]; /* Conversion buffer */
29161
29162 /* pAccum never starts out with an empty buffer that was obtained from
29163 ** malloc(). This precondition is required by the mprintf("%z...")
29164 ** optimization. */
29165 assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
29166
29167 bufpt = 0;
29168 if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){
29169 pArgList = va_arg(ap, PrintfArguments*);
29170 bArgList = 1;
29171 }else{
29172 bArgList = 0;
29173 }
29174 for(; (c=(*fmt))!=0; ++fmt){
29175 if( c!='%' ){
29176 bufpt = (char *)fmt;
29177#if HAVE_STRCHRNUL
29178 fmt = strchrnul(fmt, '%');
29179#else
29180 do{ fmt++; }while( *fmt && *fmt != '%' );
29181#endif
29182 sqlite3_str_append(pAccum, zIn: bufpt, N: (int)(fmt - bufpt));
29183 if( *fmt==0 ) break;
29184 }
29185 if( (c=(*++fmt))==0 ){
29186 sqlite3_str_append(pAccum, zIn: "%", N: 1);
29187 break;
29188 }
29189 /* Find out what flags are present */
29190 flag_leftjustify = flag_prefix = cThousand =
29191 flag_alternateform = flag_altform2 = flag_zeropad = 0;
29192 done = 0;
29193 width = 0;
29194 flag_long = 0;
29195 precision = -1;
29196 do{
29197 switch( c ){
29198 case '-': flag_leftjustify = 1; break;
29199 case '+': flag_prefix = '+'; break;
29200 case ' ': flag_prefix = ' '; break;
29201 case '#': flag_alternateform = 1; break;
29202 case '!': flag_altform2 = 1; break;
29203 case '0': flag_zeropad = 1; break;
29204 case ',': cThousand = ','; break;
29205 default: done = 1; break;
29206 case 'l': {
29207 flag_long = 1;
29208 c = *++fmt;
29209 if( c=='l' ){
29210 c = *++fmt;
29211 flag_long = 2;
29212 }
29213 done = 1;
29214 break;
29215 }
29216 case '1': case '2': case '3': case '4': case '5':
29217 case '6': case '7': case '8': case '9': {
29218 unsigned wx = c - '0';
29219 while( (c = *++fmt)>='0' && c<='9' ){
29220 wx = wx*10 + c - '0';
29221 }
29222 testcase( wx>0x7fffffff );
29223 width = wx & 0x7fffffff;
29224#ifdef SQLITE_PRINTF_PRECISION_LIMIT
29225 if( width>SQLITE_PRINTF_PRECISION_LIMIT ){
29226 width = SQLITE_PRINTF_PRECISION_LIMIT;
29227 }
29228#endif
29229 if( c!='.' && c!='l' ){
29230 done = 1;
29231 }else{
29232 fmt--;
29233 }
29234 break;
29235 }
29236 case '*': {
29237 if( bArgList ){
29238 width = (int)getIntArg(p: pArgList);
29239 }else{
29240 width = va_arg(ap,int);
29241 }
29242 if( width<0 ){
29243 flag_leftjustify = 1;
29244 width = width >= -2147483647 ? -width : 0;
29245 }
29246#ifdef SQLITE_PRINTF_PRECISION_LIMIT
29247 if( width>SQLITE_PRINTF_PRECISION_LIMIT ){
29248 width = SQLITE_PRINTF_PRECISION_LIMIT;
29249 }
29250#endif
29251 if( (c = fmt[1])!='.' && c!='l' ){
29252 c = *++fmt;
29253 done = 1;
29254 }
29255 break;
29256 }
29257 case '.': {
29258 c = *++fmt;
29259 if( c=='*' ){
29260 if( bArgList ){
29261 precision = (int)getIntArg(p: pArgList);
29262 }else{
29263 precision = va_arg(ap,int);
29264 }
29265 if( precision<0 ){
29266 precision = precision >= -2147483647 ? -precision : -1;
29267 }
29268 c = *++fmt;
29269 }else{
29270 unsigned px = 0;
29271 while( c>='0' && c<='9' ){
29272 px = px*10 + c - '0';
29273 c = *++fmt;
29274 }
29275 testcase( px>0x7fffffff );
29276 precision = px & 0x7fffffff;
29277 }
29278#ifdef SQLITE_PRINTF_PRECISION_LIMIT
29279 if( precision>SQLITE_PRINTF_PRECISION_LIMIT ){
29280 precision = SQLITE_PRINTF_PRECISION_LIMIT;
29281 }
29282#endif
29283 if( c=='l' ){
29284 --fmt;
29285 }else{
29286 done = 1;
29287 }
29288 break;
29289 }
29290 }
29291 }while( !done && (c=(*++fmt))!=0 );
29292
29293 /* Fetch the info entry for the field */
29294 infop = &fmtinfo[0];
29295 xtype = etINVALID;
29296 for(idx=0; idx<ArraySize(fmtinfo); idx++){
29297 if( c==fmtinfo[idx].fmttype ){
29298 infop = &fmtinfo[idx];
29299 xtype = infop->type;
29300 break;
29301 }
29302 }
29303
29304 /*
29305 ** At this point, variables are initialized as follows:
29306 **
29307 ** flag_alternateform TRUE if a '#' is present.
29308 ** flag_altform2 TRUE if a '!' is present.
29309 ** flag_prefix '+' or ' ' or zero
29310 ** flag_leftjustify TRUE if a '-' is present or if the
29311 ** field width was negative.
29312 ** flag_zeropad TRUE if the width began with 0.
29313 ** flag_long 1 for "l", 2 for "ll"
29314 ** width The specified field width. This is
29315 ** always non-negative. Zero is the default.
29316 ** precision The specified precision. The default
29317 ** is -1.
29318 ** xtype The class of the conversion.
29319 ** infop Pointer to the appropriate info struct.
29320 */
29321 assert( width>=0 );
29322 assert( precision>=(-1) );
29323 switch( xtype ){
29324 case etPOINTER:
29325 flag_long = sizeof(char*)==sizeof(i64) ? 2 :
29326 sizeof(char*)==sizeof(long int) ? 1 : 0;
29327 /* no break */ deliberate_fall_through
29328 case etORDINAL:
29329 case etRADIX:
29330 cThousand = 0;
29331 /* no break */ deliberate_fall_through
29332 case etDECIMAL:
29333 if( infop->flags & FLAG_SIGNED ){
29334 i64 v;
29335 if( bArgList ){
29336 v = getIntArg(p: pArgList);
29337 }else if( flag_long ){
29338 if( flag_long==2 ){
29339 v = va_arg(ap,i64) ;
29340 }else{
29341 v = va_arg(ap,long int);
29342 }
29343 }else{
29344 v = va_arg(ap,int);
29345 }
29346 if( v<0 ){
29347 testcase( v==SMALLEST_INT64 );
29348 testcase( v==(-1) );
29349 longvalue = ~v;
29350 longvalue++;
29351 prefix = '-';
29352 }else{
29353 longvalue = v;
29354 prefix = flag_prefix;
29355 }
29356 }else{
29357 if( bArgList ){
29358 longvalue = (u64)getIntArg(p: pArgList);
29359 }else if( flag_long ){
29360 if( flag_long==2 ){
29361 longvalue = va_arg(ap,u64);
29362 }else{
29363 longvalue = va_arg(ap,unsigned long int);
29364 }
29365 }else{
29366 longvalue = va_arg(ap,unsigned int);
29367 }
29368 prefix = 0;
29369 }
29370 if( longvalue==0 ) flag_alternateform = 0;
29371 if( flag_zeropad && precision<width-(prefix!=0) ){
29372 precision = width-(prefix!=0);
29373 }
29374 if( precision<etBUFSIZE-10-etBUFSIZE/3 ){
29375 nOut = etBUFSIZE;
29376 zOut = buf;
29377 }else{
29378 u64 n;
29379 n = (u64)precision + 10;
29380 if( cThousand ) n += precision/3;
29381 zOut = zExtra = printfTempBuf(pAccum, n);
29382 if( zOut==0 ) return;
29383 nOut = (int)n;
29384 }
29385 bufpt = &zOut[nOut-1];
29386 if( xtype==etORDINAL ){
29387 static const char zOrd[] = "thstndrd";
29388 int x = (int)(longvalue % 10);
29389 if( x>=4 || (longvalue/10)%10==1 ){
29390 x = 0;
29391 }
29392 *(--bufpt) = zOrd[x*2+1];
29393 *(--bufpt) = zOrd[x*2];
29394 }
29395 {
29396 const char *cset = &aDigits[infop->charset];
29397 u8 base = infop->base;
29398 do{ /* Convert to ascii */
29399 *(--bufpt) = cset[longvalue%base];
29400 longvalue = longvalue/base;
29401 }while( longvalue>0 );
29402 }
29403 length = (int)(&zOut[nOut-1]-bufpt);
29404 while( precision>length ){
29405 *(--bufpt) = '0'; /* Zero pad */
29406 length++;
29407 }
29408 if( cThousand ){
29409 int nn = (length - 1)/3; /* Number of "," to insert */
29410 int ix = (length - 1)%3 + 1;
29411 bufpt -= nn;
29412 for(idx=0; nn>0; idx++){
29413 bufpt[idx] = bufpt[idx+nn];
29414 ix--;
29415 if( ix==0 ){
29416 bufpt[++idx] = cThousand;
29417 nn--;
29418 ix = 3;
29419 }
29420 }
29421 }
29422 if( prefix ) *(--bufpt) = prefix; /* Add sign */
29423 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
29424 const char *pre;
29425 char x;
29426 pre = &aPrefix[infop->prefix];
29427 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
29428 }
29429 length = (int)(&zOut[nOut-1]-bufpt);
29430 break;
29431 case etFLOAT:
29432 case etEXP:
29433 case etGENERIC:
29434 if( bArgList ){
29435 realvalue = getDoubleArg(p: pArgList);
29436 }else{
29437 realvalue = va_arg(ap,double);
29438 }
29439#ifdef SQLITE_OMIT_FLOATING_POINT
29440 length = 0;
29441#else
29442 if( precision<0 ) precision = 6; /* Set default precision */
29443#ifdef SQLITE_FP_PRECISION_LIMIT
29444 if( precision>SQLITE_FP_PRECISION_LIMIT ){
29445 precision = SQLITE_FP_PRECISION_LIMIT;
29446 }
29447#endif
29448 if( realvalue<0.0 ){
29449 realvalue = -realvalue;
29450 prefix = '-';
29451 }else{
29452 prefix = flag_prefix;
29453 }
29454 if( xtype==etGENERIC && precision>0 ) precision--;
29455 testcase( precision>0xfff );
29456 idx = precision & 0xfff;
29457 rounder = arRound[idx%10];
29458 while( idx>=10 ){ rounder *= 1.0e-10; idx -= 10; }
29459 if( xtype==etFLOAT ){
29460 double rx = (double)realvalue;
29461 sqlite3_uint64 u;
29462 int ex;
29463 memcpy(dest: &u, src: &rx, n: sizeof(u));
29464 ex = -1023 + (int)((u>>52)&0x7ff);
29465 if( precision+(ex/3) < 15 ) rounder += realvalue*3e-16;
29466 realvalue += rounder;
29467 }
29468 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
29469 exp = 0;
29470 if( sqlite3IsNaN((double)realvalue) ){
29471 bufpt = "NaN";
29472 length = 3;
29473 break;
29474 }
29475 if( realvalue>0.0 ){
29476 LONGDOUBLE_TYPE scale = 1.0;
29477 while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;}
29478 while( realvalue>=1e10*scale && exp<=350 ){ scale *= 1e10; exp+=10; }
29479 while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; }
29480 realvalue /= scale;
29481 while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
29482 while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
29483 if( exp>350 ){
29484 bufpt = buf;
29485 buf[0] = prefix;
29486 memcpy(dest: buf+(prefix!=0),src: "Inf",n: 4);
29487 length = 3+(prefix!=0);
29488 break;
29489 }
29490 }
29491 bufpt = buf;
29492 /*
29493 ** If the field type is etGENERIC, then convert to either etEXP
29494 ** or etFLOAT, as appropriate.
29495 */
29496 if( xtype!=etFLOAT ){
29497 realvalue += rounder;
29498 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
29499 }
29500 if( xtype==etGENERIC ){
29501 flag_rtz = !flag_alternateform;
29502 if( exp<-4 || exp>precision ){
29503 xtype = etEXP;
29504 }else{
29505 precision = precision - exp;
29506 xtype = etFLOAT;
29507 }
29508 }else{
29509 flag_rtz = flag_altform2;
29510 }
29511 if( xtype==etEXP ){
29512 e2 = 0;
29513 }else{
29514 e2 = exp;
29515 }
29516 {
29517 i64 szBufNeeded; /* Size of a temporary buffer needed */
29518 szBufNeeded = MAX(e2,0)+(i64)precision+(i64)width+15;
29519 if( szBufNeeded > etBUFSIZE ){
29520 bufpt = zExtra = printfTempBuf(pAccum, n: szBufNeeded);
29521 if( bufpt==0 ) return;
29522 }
29523 }
29524 zOut = bufpt;
29525 nsd = 16 + flag_altform2*10;
29526 flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
29527 /* The sign in front of the number */
29528 if( prefix ){
29529 *(bufpt++) = prefix;
29530 }
29531 /* Digits prior to the decimal point */
29532 if( e2<0 ){
29533 *(bufpt++) = '0';
29534 }else{
29535 for(; e2>=0; e2--){
29536 *(bufpt++) = et_getdigit(val: &realvalue,cnt: &nsd);
29537 }
29538 }
29539 /* The decimal point */
29540 if( flag_dp ){
29541 *(bufpt++) = '.';
29542 }
29543 /* "0" digits after the decimal point but before the first
29544 ** significant digit of the number */
29545 for(e2++; e2<0; precision--, e2++){
29546 assert( precision>0 );
29547 *(bufpt++) = '0';
29548 }
29549 /* Significant digits after the decimal point */
29550 while( (precision--)>0 ){
29551 *(bufpt++) = et_getdigit(val: &realvalue,cnt: &nsd);
29552 }
29553 /* Remove trailing zeros and the "." if no digits follow the "." */
29554 if( flag_rtz && flag_dp ){
29555 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
29556 assert( bufpt>zOut );
29557 if( bufpt[-1]=='.' ){
29558 if( flag_altform2 ){
29559 *(bufpt++) = '0';
29560 }else{
29561 *(--bufpt) = 0;
29562 }
29563 }
29564 }
29565 /* Add the "eNNN" suffix */
29566 if( xtype==etEXP ){
29567 *(bufpt++) = aDigits[infop->charset];
29568 if( exp<0 ){
29569 *(bufpt++) = '-'; exp = -exp;
29570 }else{
29571 *(bufpt++) = '+';
29572 }
29573 if( exp>=100 ){
29574 *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */
29575 exp %= 100;
29576 }
29577 *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */
29578 *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */
29579 }
29580 *bufpt = 0;
29581
29582 /* The converted number is in buf[] and zero terminated. Output it.
29583 ** Note that the number is in the usual order, not reversed as with
29584 ** integer conversions. */
29585 length = (int)(bufpt-zOut);
29586 bufpt = zOut;
29587
29588 /* Special case: Add leading zeros if the flag_zeropad flag is
29589 ** set and we are not left justified */
29590 if( flag_zeropad && !flag_leftjustify && length < width){
29591 int i;
29592 int nPad = width - length;
29593 for(i=width; i>=nPad; i--){
29594 bufpt[i] = bufpt[i-nPad];
29595 }
29596 i = prefix!=0;
29597 while( nPad-- ) bufpt[i++] = '0';
29598 length = width;
29599 }
29600#endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
29601 break;
29602 case etSIZE:
29603 if( !bArgList ){
29604 *(va_arg(ap,int*)) = pAccum->nChar;
29605 }
29606 length = width = 0;
29607 break;
29608 case etPERCENT:
29609 buf[0] = '%';
29610 bufpt = buf;
29611 length = 1;
29612 break;
29613 case etCHARX:
29614 if( bArgList ){
29615 bufpt = getTextArg(p: pArgList);
29616 length = 1;
29617 if( bufpt ){
29618 buf[0] = c = *(bufpt++);
29619 if( (c&0xc0)==0xc0 ){
29620 while( length<4 && (bufpt[0]&0xc0)==0x80 ){
29621 buf[length++] = *(bufpt++);
29622 }
29623 }
29624 }else{
29625 buf[0] = 0;
29626 }
29627 }else{
29628 unsigned int ch = va_arg(ap,unsigned int);
29629 if( ch<0x00080 ){
29630 buf[0] = ch & 0xff;
29631 length = 1;
29632 }else if( ch<0x00800 ){
29633 buf[0] = 0xc0 + (u8)((ch>>6)&0x1f);
29634 buf[1] = 0x80 + (u8)(ch & 0x3f);
29635 length = 2;
29636 }else if( ch<0x10000 ){
29637 buf[0] = 0xe0 + (u8)((ch>>12)&0x0f);
29638 buf[1] = 0x80 + (u8)((ch>>6) & 0x3f);
29639 buf[2] = 0x80 + (u8)(ch & 0x3f);
29640 length = 3;
29641 }else{
29642 buf[0] = 0xf0 + (u8)((ch>>18) & 0x07);
29643 buf[1] = 0x80 + (u8)((ch>>12) & 0x3f);
29644 buf[2] = 0x80 + (u8)((ch>>6) & 0x3f);
29645 buf[3] = 0x80 + (u8)(ch & 0x3f);
29646 length = 4;
29647 }
29648 }
29649 if( precision>1 ){
29650 width -= precision-1;
29651 if( width>1 && !flag_leftjustify ){
29652 sqlite3_str_appendchar(pAccum, N: width-1, C: ' ');
29653 width = 0;
29654 }
29655 while( precision-- > 1 ){
29656 sqlite3_str_append(pAccum, zIn: buf, N: length);
29657 }
29658 }
29659 bufpt = buf;
29660 flag_altform2 = 1;
29661 goto adjust_width_for_utf8;
29662 case etSTRING:
29663 case etDYNSTRING:
29664 if( bArgList ){
29665 bufpt = getTextArg(p: pArgList);
29666 xtype = etSTRING;
29667 }else{
29668 bufpt = va_arg(ap,char*);
29669 }
29670 if( bufpt==0 ){
29671 bufpt = "";
29672 }else if( xtype==etDYNSTRING ){
29673 if( pAccum->nChar==0
29674 && pAccum->mxAlloc
29675 && width==0
29676 && precision<0
29677 && pAccum->accError==0
29678 ){
29679 /* Special optimization for sqlite3_mprintf("%z..."):
29680 ** Extend an existing memory allocation rather than creating
29681 ** a new one. */
29682 assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
29683 pAccum->zText = bufpt;
29684 pAccum->nAlloc = sqlite3DbMallocSize(db: pAccum->db, p: bufpt);
29685 pAccum->nChar = 0x7fffffff & (int)strlen(s: bufpt);
29686 pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED;
29687 length = 0;
29688 break;
29689 }
29690 zExtra = bufpt;
29691 }
29692 if( precision>=0 ){
29693 if( flag_altform2 ){
29694 /* Set length to the number of bytes needed in order to display
29695 ** precision characters */
29696 unsigned char *z = (unsigned char*)bufpt;
29697 while( precision-- > 0 && z[0] ){
29698 SQLITE_SKIP_UTF8(z);
29699 }
29700 length = (int)(z - (unsigned char*)bufpt);
29701 }else{
29702 for(length=0; length<precision && bufpt[length]; length++){}
29703 }
29704 }else{
29705 length = 0x7fffffff & (int)strlen(s: bufpt);
29706 }
29707 adjust_width_for_utf8:
29708 if( flag_altform2 && width>0 ){
29709 /* Adjust width to account for extra bytes in UTF-8 characters */
29710 int ii = length - 1;
29711 while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++;
29712 }
29713 break;
29714 case etSQLESCAPE: /* %q: Escape ' characters */
29715 case etSQLESCAPE2: /* %Q: Escape ' and enclose in '...' */
29716 case etSQLESCAPE3: { /* %w: Escape " characters */
29717 int i, j, k, n, isnull;
29718 int needQuote;
29719 char ch;
29720 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */
29721 char *escarg;
29722
29723 if( bArgList ){
29724 escarg = getTextArg(p: pArgList);
29725 }else{
29726 escarg = va_arg(ap,char*);
29727 }
29728 isnull = escarg==0;
29729 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
29730 /* For %q, %Q, and %w, the precision is the number of bytes (or
29731 ** characters if the ! flags is present) to use from the input.
29732 ** Because of the extra quoting characters inserted, the number
29733 ** of output characters may be larger than the precision.
29734 */
29735 k = precision;
29736 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
29737 if( ch==q ) n++;
29738 if( flag_altform2 && (ch&0xc0)==0xc0 ){
29739 while( (escarg[i+1]&0xc0)==0x80 ){ i++; }
29740 }
29741 }
29742 needQuote = !isnull && xtype==etSQLESCAPE2;
29743 n += i + 3;
29744 if( n>etBUFSIZE ){
29745 bufpt = zExtra = printfTempBuf(pAccum, n);
29746 if( bufpt==0 ) return;
29747 }else{
29748 bufpt = buf;
29749 }
29750 j = 0;
29751 if( needQuote ) bufpt[j++] = q;
29752 k = i;
29753 for(i=0; i<k; i++){
29754 bufpt[j++] = ch = escarg[i];
29755 if( ch==q ) bufpt[j++] = ch;
29756 }
29757 if( needQuote ) bufpt[j++] = q;
29758 bufpt[j] = 0;
29759 length = j;
29760 goto adjust_width_for_utf8;
29761 }
29762 case etTOKEN: {
29763 Token *pToken;
29764 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
29765 pToken = va_arg(ap, Token*);
29766 assert( bArgList==0 );
29767 if( pToken && pToken->n ){
29768 sqlite3_str_append(pAccum, zIn: (const char*)pToken->z, N: pToken->n);
29769 }
29770 length = width = 0;
29771 break;
29772 }
29773 case etSRCITEM: {
29774 SrcItem *pItem;
29775 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
29776 pItem = va_arg(ap, SrcItem*);
29777 assert( bArgList==0 );
29778 if( pItem->zAlias && !flag_altform2 ){
29779 sqlite3_str_appendall(pAccum, zIn: pItem->zAlias);
29780 }else if( pItem->zName ){
29781 if( pItem->zDatabase ){
29782 sqlite3_str_appendall(pAccum, zIn: pItem->zDatabase);
29783 sqlite3_str_append(pAccum, zIn: ".", N: 1);
29784 }
29785 sqlite3_str_appendall(pAccum, zIn: pItem->zName);
29786 }else if( pItem->zAlias ){
29787 sqlite3_str_appendall(pAccum, zIn: pItem->zAlias);
29788 }else if( ALWAYS(pItem->pSelect) ){
29789 sqlite3_str_appendf(pAccum, zFormat: "SUBQUERY %u", pItem->pSelect->selId);
29790 }
29791 length = width = 0;
29792 break;
29793 }
29794 default: {
29795 assert( xtype==etINVALID );
29796 return;
29797 }
29798 }/* End switch over the format type */
29799 /*
29800 ** The text of the conversion is pointed to by "bufpt" and is
29801 ** "length" characters long. The field width is "width". Do
29802 ** the output. Both length and width are in bytes, not characters,
29803 ** at this point. If the "!" flag was present on string conversions
29804 ** indicating that width and precision should be expressed in characters,
29805 ** then the values have been translated prior to reaching this point.
29806 */
29807 width -= length;
29808 if( width>0 ){
29809 if( !flag_leftjustify ) sqlite3_str_appendchar(pAccum, N: width, C: ' ');
29810 sqlite3_str_append(pAccum, zIn: bufpt, N: length);
29811 if( flag_leftjustify ) sqlite3_str_appendchar(pAccum, N: width, C: ' ');
29812 }else{
29813 sqlite3_str_append(pAccum, zIn: bufpt, N: length);
29814 }
29815
29816 if( zExtra ){
29817 sqlite3DbFree(db: pAccum->db, p: zExtra);
29818 zExtra = 0;
29819 }
29820 }/* End for loop over the format string */
29821} /* End of function */
29822
29823/*
29824** Enlarge the memory allocation on a StrAccum object so that it is
29825** able to accept at least N more bytes of text.
29826**
29827** Return the number of bytes of text that StrAccum is able to accept
29828** after the attempted enlargement. The value returned might be zero.
29829*/
29830static int sqlite3StrAccumEnlarge(StrAccum *p, int N){
29831 char *zNew;
29832 assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
29833 if( p->accError ){
29834 testcase(p->accError==SQLITE_TOOBIG);
29835 testcase(p->accError==SQLITE_NOMEM);
29836 return 0;
29837 }
29838 if( p->mxAlloc==0 ){
29839 sqlite3StrAccumSetError(p, SQLITE_TOOBIG);
29840 return p->nAlloc - p->nChar - 1;
29841 }else{
29842 char *zOld = isMalloced(p) ? p->zText : 0;
29843 i64 szNew = p->nChar;
29844 szNew += (sqlite3_int64)N + 1;
29845 if( szNew+p->nChar<=p->mxAlloc ){
29846 /* Force exponential buffer size growth as long as it does not overflow,
29847 ** to avoid having to call this routine too often */
29848 szNew += p->nChar;
29849 }
29850 if( szNew > p->mxAlloc ){
29851 sqlite3_str_reset(p);
29852 sqlite3StrAccumSetError(p, SQLITE_TOOBIG);
29853 return 0;
29854 }else{
29855 p->nAlloc = (int)szNew;
29856 }
29857 if( p->db ){
29858 zNew = sqlite3DbRealloc(db: p->db, p: zOld, n: p->nAlloc);
29859 }else{
29860 zNew = sqlite3Realloc(pOld: zOld, nBytes: p->nAlloc);
29861 }
29862 if( zNew ){
29863 assert( p->zText!=0 || p->nChar==0 );
29864 if( !isMalloced(p) && p->nChar>0 ) memcpy(dest: zNew, src: p->zText, n: p->nChar);
29865 p->zText = zNew;
29866 p->nAlloc = sqlite3DbMallocSize(db: p->db, p: zNew);
29867 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
29868 }else{
29869 sqlite3_str_reset(p);
29870 sqlite3StrAccumSetError(p, SQLITE_NOMEM);
29871 return 0;
29872 }
29873 }
29874 return N;
29875}
29876
29877/*
29878** Append N copies of character c to the given string buffer.
29879*/
29880SQLITE_API void sqlite3_str_appendchar(sqlite3_str *p, int N, char c){
29881 testcase( p->nChar + (i64)N > 0x7fffffff );
29882 if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
29883 return;
29884 }
29885 while( (N--)>0 ) p->zText[p->nChar++] = c;
29886}
29887
29888/*
29889** The StrAccum "p" is not large enough to accept N new bytes of z[].
29890** So enlarge if first, then do the append.
29891**
29892** This is a helper routine to sqlite3_str_append() that does special-case
29893** work (enlarging the buffer) using tail recursion, so that the
29894** sqlite3_str_append() routine can use fast calling semantics.
29895*/
29896static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){
29897 N = sqlite3StrAccumEnlarge(p, N);
29898 if( N>0 ){
29899 memcpy(dest: &p->zText[p->nChar], src: z, n: N);
29900 p->nChar += N;
29901 }
29902}
29903
29904/*
29905** Append N bytes of text from z to the StrAccum object. Increase the
29906** size of the memory allocation for StrAccum if necessary.
29907*/
29908SQLITE_API void sqlite3_str_append(sqlite3_str *p, const char *z, int N){
29909 assert( z!=0 || N==0 );
29910 assert( p->zText!=0 || p->nChar==0 || p->accError );
29911 assert( N>=0 );
29912 assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 );
29913 if( p->nChar+N >= p->nAlloc ){
29914 enlargeAndAppend(p,z,N);
29915 }else if( N ){
29916 assert( p->zText );
29917 p->nChar += N;
29918 memcpy(dest: &p->zText[p->nChar-N], src: z, n: N);
29919 }
29920}
29921
29922/*
29923** Append the complete text of zero-terminated string z[] to the p string.
29924*/
29925SQLITE_API void sqlite3_str_appendall(sqlite3_str *p, const char *z){
29926 sqlite3_str_append(p, z, N: sqlite3Strlen30(z));
29927}
29928
29929
29930/*
29931** Finish off a string by making sure it is zero-terminated.
29932** Return a pointer to the resulting string. Return a NULL
29933** pointer if any kind of error was encountered.
29934*/
29935static SQLITE_NOINLINE char *strAccumFinishRealloc(StrAccum *p){
29936 char *zText;
29937 assert( p->mxAlloc>0 && !isMalloced(p) );
29938 zText = sqlite3DbMallocRaw(db: p->db, n: p->nChar+1 );
29939 if( zText ){
29940 memcpy(dest: zText, src: p->zText, n: p->nChar+1);
29941 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
29942 }else{
29943 sqlite3StrAccumSetError(p, SQLITE_NOMEM);
29944 }
29945 p->zText = zText;
29946 return zText;
29947}
29948SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){
29949 if( p->zText ){
29950 p->zText[p->nChar] = 0;
29951 if( p->mxAlloc>0 && !isMalloced(p) ){
29952 return strAccumFinishRealloc(p);
29953 }
29954 }
29955 return p->zText;
29956}
29957
29958/*
29959** Use the content of the StrAccum passed as the second argument
29960** as the result of an SQL function.
29961*/
29962SQLITE_PRIVATE void sqlite3ResultStrAccum(sqlite3_context *pCtx, StrAccum *p){
29963 if( p->accError ){
29964 sqlite3_result_error_code(pCtx, p->accError);
29965 sqlite3_str_reset(p);
29966 }else if( isMalloced(p) ){
29967 sqlite3_result_text(pCtx, p->zText, p->nChar, SQLITE_DYNAMIC);
29968 }else{
29969 sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
29970 sqlite3_str_reset(p);
29971 }
29972}
29973
29974/*
29975** This singleton is an sqlite3_str object that is returned if
29976** sqlite3_malloc() fails to provide space for a real one. This
29977** sqlite3_str object accepts no new text and always returns
29978** an SQLITE_NOMEM error.
29979*/
29980static sqlite3_str sqlite3OomStr = {
29981 0, 0, 0, 0, 0, SQLITE_NOMEM, 0
29982};
29983
29984/* Finalize a string created using sqlite3_str_new().
29985*/
29986SQLITE_API char *sqlite3_str_finish(sqlite3_str *p){
29987 char *z;
29988 if( p!=0 && p!=&sqlite3OomStr ){
29989 z = sqlite3StrAccumFinish(p);
29990 sqlite3_free(p);
29991 }else{
29992 z = 0;
29993 }
29994 return z;
29995}
29996
29997/* Return any error code associated with p */
29998SQLITE_API int sqlite3_str_errcode(sqlite3_str *p){
29999 return p ? p->accError : SQLITE_NOMEM;
30000}
30001
30002/* Return the current length of p in bytes */
30003SQLITE_API int sqlite3_str_length(sqlite3_str *p){
30004 return p ? p->nChar : 0;
30005}
30006
30007/* Return the current value for p */
30008SQLITE_API char *sqlite3_str_value(sqlite3_str *p){
30009 if( p==0 || p->nChar==0 ) return 0;
30010 p->zText[p->nChar] = 0;
30011 return p->zText;
30012}
30013
30014/*
30015** Reset an StrAccum string. Reclaim all malloced memory.
30016*/
30017SQLITE_API void sqlite3_str_reset(StrAccum *p){
30018 if( isMalloced(p) ){
30019 sqlite3DbFree(db: p->db, p: p->zText);
30020 p->printfFlags &= ~SQLITE_PRINTF_MALLOCED;
30021 }
30022 p->nAlloc = 0;
30023 p->nChar = 0;
30024 p->zText = 0;
30025}
30026
30027/*
30028** Initialize a string accumulator.
30029**
30030** p: The accumulator to be initialized.
30031** db: Pointer to a database connection. May be NULL. Lookaside
30032** memory is used if not NULL. db->mallocFailed is set appropriately
30033** when not NULL.
30034** zBase: An initial buffer. May be NULL in which case the initial buffer
30035** is malloced.
30036** n: Size of zBase in bytes. If total space requirements never exceed
30037** n then no memory allocations ever occur.
30038** mx: Maximum number of bytes to accumulate. If mx==0 then no memory
30039** allocations will ever occur.
30040*/
30041SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){
30042 p->zText = zBase;
30043 p->db = db;
30044 p->nAlloc = n;
30045 p->mxAlloc = mx;
30046 p->nChar = 0;
30047 p->accError = 0;
30048 p->printfFlags = 0;
30049}
30050
30051/* Allocate and initialize a new dynamic string object */
30052SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3 *db){
30053 sqlite3_str *p = sqlite3_malloc64(n: sizeof(*p));
30054 if( p ){
30055 sqlite3StrAccumInit(p, db: 0, zBase: 0, n: 0,
30056 mx: db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH);
30057 }else{
30058 p = &sqlite3OomStr;
30059 }
30060 return p;
30061}
30062
30063/*
30064** Print into memory obtained from sqliteMalloc(). Use the internal
30065** %-conversion extensions.
30066*/
30067SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
30068 char *z;
30069 char zBase[SQLITE_PRINT_BUF_SIZE];
30070 StrAccum acc;
30071 assert( db!=0 );
30072 sqlite3StrAccumInit(p: &acc, db, zBase, n: sizeof(zBase),
30073 mx: db->aLimit[SQLITE_LIMIT_LENGTH]);
30074 acc.printfFlags = SQLITE_PRINTF_INTERNAL;
30075 sqlite3_str_vappendf(pAccum: &acc, fmt: zFormat, ap);
30076 z = sqlite3StrAccumFinish(p: &acc);
30077 if( acc.accError==SQLITE_NOMEM ){
30078 sqlite3OomFault(db);
30079 }
30080 return z;
30081}
30082
30083/*
30084** Print into memory obtained from sqliteMalloc(). Use the internal
30085** %-conversion extensions.
30086*/
30087SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
30088 va_list ap;
30089 char *z;
30090 va_start(ap, zFormat);
30091 z = sqlite3VMPrintf(db, zFormat, ap);
30092 va_end(ap);
30093 return z;
30094}
30095
30096/*
30097** Print into memory obtained from sqlite3_malloc(). Omit the internal
30098** %-conversion extensions.
30099*/
30100SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){
30101 char *z;
30102 char zBase[SQLITE_PRINT_BUF_SIZE];
30103 StrAccum acc;
30104
30105#ifdef SQLITE_ENABLE_API_ARMOR
30106 if( zFormat==0 ){
30107 (void)SQLITE_MISUSE_BKPT;
30108 return 0;
30109 }
30110#endif
30111#ifndef SQLITE_OMIT_AUTOINIT
30112 if( sqlite3_initialize() ) return 0;
30113#endif
30114 sqlite3StrAccumInit(p: &acc, db: 0, zBase, n: sizeof(zBase), SQLITE_MAX_LENGTH);
30115 sqlite3_str_vappendf(pAccum: &acc, fmt: zFormat, ap);
30116 z = sqlite3StrAccumFinish(p: &acc);
30117 return z;
30118}
30119
30120/*
30121** Print into memory obtained from sqlite3_malloc()(). Omit the internal
30122** %-conversion extensions.
30123*/
30124SQLITE_API char *sqlite3_mprintf(const char *zFormat, ...){
30125 va_list ap;
30126 char *z;
30127#ifndef SQLITE_OMIT_AUTOINIT
30128 if( sqlite3_initialize() ) return 0;
30129#endif
30130 va_start(ap, zFormat);
30131 z = sqlite3_vmprintf(zFormat, ap);
30132 va_end(ap);
30133 return z;
30134}
30135
30136/*
30137** sqlite3_snprintf() works like snprintf() except that it ignores the
30138** current locale settings. This is important for SQLite because we
30139** are not able to use a "," as the decimal point in place of "." as
30140** specified by some locales.
30141**
30142** Oops: The first two arguments of sqlite3_snprintf() are backwards
30143** from the snprintf() standard. Unfortunately, it is too late to change
30144** this without breaking compatibility, so we just have to live with the
30145** mistake.
30146**
30147** sqlite3_vsnprintf() is the varargs version.
30148*/
30149SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
30150 StrAccum acc;
30151 if( n<=0 ) return zBuf;
30152#ifdef SQLITE_ENABLE_API_ARMOR
30153 if( zBuf==0 || zFormat==0 ) {
30154 (void)SQLITE_MISUSE_BKPT;
30155 if( zBuf ) zBuf[0] = 0;
30156 return zBuf;
30157 }
30158#endif
30159 sqlite3StrAccumInit(p: &acc, db: 0, zBase: zBuf, n, mx: 0);
30160 sqlite3_str_vappendf(pAccum: &acc, fmt: zFormat, ap);
30161 zBuf[acc.nChar] = 0;
30162 return zBuf;
30163}
30164SQLITE_API char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
30165 char *z;
30166 va_list ap;
30167 va_start(ap,zFormat);
30168 z = sqlite3_vsnprintf(n, zBuf, zFormat, ap);
30169 va_end(ap);
30170 return z;
30171}
30172
30173/*
30174** This is the routine that actually formats the sqlite3_log() message.
30175** We house it in a separate routine from sqlite3_log() to avoid using
30176** stack space on small-stack systems when logging is disabled.
30177**
30178** sqlite3_log() must render into a static buffer. It cannot dynamically
30179** allocate memory because it might be called while the memory allocator
30180** mutex is held.
30181**
30182** sqlite3_str_vappendf() might ask for *temporary* memory allocations for
30183** certain format characters (%q) or for very large precisions or widths.
30184** Care must be taken that any sqlite3_log() calls that occur while the
30185** memory mutex is held do not use these mechanisms.
30186*/
30187static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
30188 StrAccum acc; /* String accumulator */
30189 char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */
30190
30191 sqlite3StrAccumInit(p: &acc, db: 0, zBase: zMsg, n: sizeof(zMsg), mx: 0);
30192 sqlite3_str_vappendf(pAccum: &acc, fmt: zFormat, ap);
30193 sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
30194 sqlite3StrAccumFinish(p: &acc));
30195}
30196
30197/*
30198** Format and write a message to the log if logging is enabled.
30199*/
30200SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...){
30201 va_list ap; /* Vararg list */
30202 if( sqlite3GlobalConfig.xLog ){
30203 va_start(ap, zFormat);
30204 renderLogMsg(iErrCode, zFormat, ap);
30205 va_end(ap);
30206 }
30207}
30208
30209#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
30210/*
30211** A version of printf() that understands %lld. Used for debugging.
30212** The printf() built into some versions of windows does not understand %lld
30213** and segfaults if you give it a long long int.
30214*/
30215SQLITE_PRIVATE void sqlite3DebugPrintf(const char *zFormat, ...){
30216 va_list ap;
30217 StrAccum acc;
30218 char zBuf[SQLITE_PRINT_BUF_SIZE*10];
30219 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
30220 va_start(ap,zFormat);
30221 sqlite3_str_vappendf(&acc, zFormat, ap);
30222 va_end(ap);
30223 sqlite3StrAccumFinish(&acc);
30224#ifdef SQLITE_OS_TRACE_PROC
30225 {
30226 extern void SQLITE_OS_TRACE_PROC(const char *zBuf, int nBuf);
30227 SQLITE_OS_TRACE_PROC(zBuf, sizeof(zBuf));
30228 }
30229#else
30230 fprintf(stdout,"%s", zBuf);
30231 fflush(stdout);
30232#endif
30233}
30234#endif
30235
30236
30237/*
30238** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument
30239** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats.
30240*/
30241SQLITE_API void sqlite3_str_appendf(StrAccum *p, const char *zFormat, ...){
30242 va_list ap;
30243 va_start(ap,zFormat);
30244 sqlite3_str_vappendf(pAccum: p, fmt: zFormat, ap);
30245 va_end(ap);
30246}
30247
30248/************** End of printf.c **********************************************/
30249/************** Begin file treeview.c ****************************************/
30250/*
30251** 2015-06-08
30252**
30253** The author disclaims copyright to this source code. In place of
30254** a legal notice, here is a blessing:
30255**
30256** May you do good and not evil.
30257** May you find forgiveness for yourself and forgive others.
30258** May you share freely, never taking more than you give.
30259**
30260*************************************************************************
30261**
30262** This file contains C code to implement the TreeView debugging routines.
30263** These routines print a parse tree to standard output for debugging and
30264** analysis.
30265**
30266** The interfaces in this file is only available when compiling
30267** with SQLITE_DEBUG.
30268*/
30269/* #include "sqliteInt.h" */
30270#ifdef SQLITE_DEBUG
30271
30272/*
30273** Add a new subitem to the tree. The moreToFollow flag indicates that this
30274** is not the last item in the tree.
30275*/
30276static TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){
30277 if( p==0 ){
30278 p = sqlite3_malloc64( sizeof(*p) );
30279 if( p==0 ) return 0;
30280 memset(p, 0, sizeof(*p));
30281 }else{
30282 p->iLevel++;
30283 }
30284 assert( moreToFollow==0 || moreToFollow==1 );
30285 if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
30286 return p;
30287}
30288
30289/*
30290** Finished with one layer of the tree
30291*/
30292static void sqlite3TreeViewPop(TreeView *p){
30293 if( p==0 ) return;
30294 p->iLevel--;
30295 if( p->iLevel<0 ) sqlite3_free(p);
30296}
30297
30298/*
30299** Generate a single line of output for the tree, with a prefix that contains
30300** all the appropriate tree lines
30301*/
30302static void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
30303 va_list ap;
30304 int i;
30305 StrAccum acc;
30306 char zBuf[500];
30307 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
30308 if( p ){
30309 for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){
30310 sqlite3_str_append(&acc, p->bLine[i] ? "| " : " ", 4);
30311 }
30312 sqlite3_str_append(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
30313 }
30314 if( zFormat!=0 ){
30315 va_start(ap, zFormat);
30316 sqlite3_str_vappendf(&acc, zFormat, ap);
30317 va_end(ap);
30318 assert( acc.nChar>0 || acc.accError );
30319 sqlite3_str_append(&acc, "\n", 1);
30320 }
30321 sqlite3StrAccumFinish(&acc);
30322 fprintf(stdout,"%s", zBuf);
30323 fflush(stdout);
30324}
30325
30326/*
30327** Shorthand for starting a new tree item that consists of a single label
30328*/
30329static void sqlite3TreeViewItem(TreeView *p, const char *zLabel,u8 moreFollows){
30330 p = sqlite3TreeViewPush(p, moreFollows);
30331 sqlite3TreeViewLine(p, "%s", zLabel);
30332}
30333
30334/*
30335** Generate a human-readable description of a WITH clause.
30336*/
30337SQLITE_PRIVATE void sqlite3TreeViewWith(TreeView *pView, const With *pWith, u8 moreToFollow){
30338 int i;
30339 if( pWith==0 ) return;
30340 if( pWith->nCte==0 ) return;
30341 if( pWith->pOuter ){
30342 sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter);
30343 }else{
30344 sqlite3TreeViewLine(pView, "WITH (0x%p)", pWith);
30345 }
30346 if( pWith->nCte>0 ){
30347 pView = sqlite3TreeViewPush(pView, 1);
30348 for(i=0; i<pWith->nCte; i++){
30349 StrAccum x;
30350 char zLine[1000];
30351 const struct Cte *pCte = &pWith->a[i];
30352 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
30353 sqlite3_str_appendf(&x, "%s", pCte->zName);
30354 if( pCte->pCols && pCte->pCols->nExpr>0 ){
30355 char cSep = '(';
30356 int j;
30357 for(j=0; j<pCte->pCols->nExpr; j++){
30358 sqlite3_str_appendf(&x, "%c%s", cSep, pCte->pCols->a[j].zEName);
30359 cSep = ',';
30360 }
30361 sqlite3_str_appendf(&x, ")");
30362 }
30363 if( pCte->pUse ){
30364 sqlite3_str_appendf(&x, " (pUse=0x%p, nUse=%d)", pCte->pUse,
30365 pCte->pUse->nUse);
30366 }
30367 sqlite3StrAccumFinish(&x);
30368 sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1);
30369 sqlite3TreeViewSelect(pView, pCte->pSelect, 0);
30370 sqlite3TreeViewPop(pView);
30371 }
30372 sqlite3TreeViewPop(pView);
30373 }
30374}
30375
30376/*
30377** Generate a human-readable description of a SrcList object.
30378*/
30379SQLITE_PRIVATE void sqlite3TreeViewSrcList(TreeView *pView, const SrcList *pSrc){
30380 int i;
30381 for(i=0; i<pSrc->nSrc; i++){
30382 const SrcItem *pItem = &pSrc->a[i];
30383 StrAccum x;
30384 char zLine[100];
30385 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
30386 x.printfFlags |= SQLITE_PRINTF_INTERNAL;
30387 sqlite3_str_appendf(&x, "{%d:*} %!S", pItem->iCursor, pItem);
30388 if( pItem->pTab ){
30389 sqlite3_str_appendf(&x, " tab=%Q nCol=%d ptr=%p used=%llx",
30390 pItem->pTab->zName, pItem->pTab->nCol, pItem->pTab, pItem->colUsed);
30391 }
30392 if( pItem->fg.jointype & JT_LEFT ){
30393 sqlite3_str_appendf(&x, " LEFT-JOIN");
30394 }else if( pItem->fg.jointype & JT_CROSS ){
30395 sqlite3_str_appendf(&x, " CROSS-JOIN");
30396 }
30397 if( pItem->fg.fromDDL ){
30398 sqlite3_str_appendf(&x, " DDL");
30399 }
30400 if( pItem->fg.isCte ){
30401 sqlite3_str_appendf(&x, " CteUse=0x%p", pItem->u2.pCteUse);
30402 }
30403 sqlite3StrAccumFinish(&x);
30404 sqlite3TreeViewItem(pView, zLine, i<pSrc->nSrc-1);
30405 if( pItem->pSelect ){
30406 sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
30407 }
30408 if( pItem->fg.isTabFunc ){
30409 sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
30410 }
30411 sqlite3TreeViewPop(pView);
30412 }
30413}
30414
30415/*
30416** Generate a human-readable description of a Select object.
30417*/
30418SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
30419 int n = 0;
30420 int cnt = 0;
30421 if( p==0 ){
30422 sqlite3TreeViewLine(pView, "nil-SELECT");
30423 return;
30424 }
30425 pView = sqlite3TreeViewPush(pView, moreToFollow);
30426 if( p->pWith ){
30427 sqlite3TreeViewWith(pView, p->pWith, 1);
30428 cnt = 1;
30429 sqlite3TreeViewPush(pView, 1);
30430 }
30431 do{
30432 if( p->selFlags & SF_WhereBegin ){
30433 sqlite3TreeViewLine(pView, "sqlite3WhereBegin()");
30434 }else{
30435 sqlite3TreeViewLine(pView,
30436 "SELECT%s%s (%u/%p) selFlags=0x%x nSelectRow=%d",
30437 ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
30438 ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""),
30439 p->selId, p, p->selFlags,
30440 (int)p->nSelectRow
30441 );
30442 }
30443 if( cnt++ ) sqlite3TreeViewPop(pView);
30444 if( p->pPrior ){
30445 n = 1000;
30446 }else{
30447 n = 0;
30448 if( p->pSrc && p->pSrc->nSrc ) n++;
30449 if( p->pWhere ) n++;
30450 if( p->pGroupBy ) n++;
30451 if( p->pHaving ) n++;
30452 if( p->pOrderBy ) n++;
30453 if( p->pLimit ) n++;
30454#ifndef SQLITE_OMIT_WINDOWFUNC
30455 if( p->pWin ) n++;
30456 if( p->pWinDefn ) n++;
30457#endif
30458 }
30459 if( p->pEList ){
30460 sqlite3TreeViewExprList(pView, p->pEList, n>0, "result-set");
30461 }
30462 n--;
30463#ifndef SQLITE_OMIT_WINDOWFUNC
30464 if( p->pWin ){
30465 Window *pX;
30466 pView = sqlite3TreeViewPush(pView, (n--)>0);
30467 sqlite3TreeViewLine(pView, "window-functions");
30468 for(pX=p->pWin; pX; pX=pX->pNextWin){
30469 sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0);
30470 }
30471 sqlite3TreeViewPop(pView);
30472 }
30473#endif
30474 if( p->pSrc && p->pSrc->nSrc ){
30475 pView = sqlite3TreeViewPush(pView, (n--)>0);
30476 sqlite3TreeViewLine(pView, "FROM");
30477 sqlite3TreeViewSrcList(pView, p->pSrc);
30478 sqlite3TreeViewPop(pView);
30479 }
30480 if( p->pWhere ){
30481 sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
30482 sqlite3TreeViewExpr(pView, p->pWhere, 0);
30483 sqlite3TreeViewPop(pView);
30484 }
30485 if( p->pGroupBy ){
30486 sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
30487 }
30488 if( p->pHaving ){
30489 sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
30490 sqlite3TreeViewExpr(pView, p->pHaving, 0);
30491 sqlite3TreeViewPop(pView);
30492 }
30493#ifndef SQLITE_OMIT_WINDOWFUNC
30494 if( p->pWinDefn ){
30495 Window *pX;
30496 sqlite3TreeViewItem(pView, "WINDOW", (n--)>0);
30497 for(pX=p->pWinDefn; pX; pX=pX->pNextWin){
30498 sqlite3TreeViewWindow(pView, pX, pX->pNextWin!=0);
30499 }
30500 sqlite3TreeViewPop(pView);
30501 }
30502#endif
30503 if( p->pOrderBy ){
30504 sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
30505 }
30506 if( p->pLimit ){
30507 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
30508 sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0);
30509 if( p->pLimit->pRight ){
30510 sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
30511 sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0);
30512 sqlite3TreeViewPop(pView);
30513 }
30514 sqlite3TreeViewPop(pView);
30515 }
30516 if( p->pPrior ){
30517 const char *zOp = "UNION";
30518 switch( p->op ){
30519 case TK_ALL: zOp = "UNION ALL"; break;
30520 case TK_INTERSECT: zOp = "INTERSECT"; break;
30521 case TK_EXCEPT: zOp = "EXCEPT"; break;
30522 }
30523 sqlite3TreeViewItem(pView, zOp, 1);
30524 }
30525 p = p->pPrior;
30526 }while( p!=0 );
30527 sqlite3TreeViewPop(pView);
30528}
30529
30530#ifndef SQLITE_OMIT_WINDOWFUNC
30531/*
30532** Generate a description of starting or stopping bounds
30533*/
30534SQLITE_PRIVATE void sqlite3TreeViewBound(
30535 TreeView *pView, /* View context */
30536 u8 eBound, /* UNBOUNDED, CURRENT, PRECEDING, FOLLOWING */
30537 Expr *pExpr, /* Value for PRECEDING or FOLLOWING */
30538 u8 moreToFollow /* True if more to follow */
30539){
30540 switch( eBound ){
30541 case TK_UNBOUNDED: {
30542 sqlite3TreeViewItem(pView, "UNBOUNDED", moreToFollow);
30543 sqlite3TreeViewPop(pView);
30544 break;
30545 }
30546 case TK_CURRENT: {
30547 sqlite3TreeViewItem(pView, "CURRENT", moreToFollow);
30548 sqlite3TreeViewPop(pView);
30549 break;
30550 }
30551 case TK_PRECEDING: {
30552 sqlite3TreeViewItem(pView, "PRECEDING", moreToFollow);
30553 sqlite3TreeViewExpr(pView, pExpr, 0);
30554 sqlite3TreeViewPop(pView);
30555 break;
30556 }
30557 case TK_FOLLOWING: {
30558 sqlite3TreeViewItem(pView, "FOLLOWING", moreToFollow);
30559 sqlite3TreeViewExpr(pView, pExpr, 0);
30560 sqlite3TreeViewPop(pView);
30561 break;
30562 }
30563 }
30564}
30565#endif /* SQLITE_OMIT_WINDOWFUNC */
30566
30567#ifndef SQLITE_OMIT_WINDOWFUNC
30568/*
30569** Generate a human-readable explanation for a Window object
30570*/
30571SQLITE_PRIVATE void sqlite3TreeViewWindow(TreeView *pView, const Window *pWin, u8 more){
30572 int nElement = 0;
30573 if( pWin->pFilter ){
30574 sqlite3TreeViewItem(pView, "FILTER", 1);
30575 sqlite3TreeViewExpr(pView, pWin->pFilter, 0);
30576 sqlite3TreeViewPop(pView);
30577 }
30578 pView = sqlite3TreeViewPush(pView, more);
30579 if( pWin->zName ){
30580 sqlite3TreeViewLine(pView, "OVER %s (%p)", pWin->zName, pWin);
30581 }else{
30582 sqlite3TreeViewLine(pView, "OVER (%p)", pWin);
30583 }
30584 if( pWin->zBase ) nElement++;
30585 if( pWin->pOrderBy ) nElement++;
30586 if( pWin->eFrmType ) nElement++;
30587 if( pWin->eExclude ) nElement++;
30588 if( pWin->zBase ){
30589 sqlite3TreeViewPush(pView, (--nElement)>0);
30590 sqlite3TreeViewLine(pView, "window: %s", pWin->zBase);
30591 sqlite3TreeViewPop(pView);
30592 }
30593 if( pWin->pPartition ){
30594 sqlite3TreeViewExprList(pView, pWin->pPartition, nElement>0,"PARTITION-BY");
30595 }
30596 if( pWin->pOrderBy ){
30597 sqlite3TreeViewExprList(pView, pWin->pOrderBy, (--nElement)>0, "ORDER-BY");
30598 }
30599 if( pWin->eFrmType ){
30600 char zBuf[30];
30601 const char *zFrmType = "ROWS";
30602 if( pWin->eFrmType==TK_RANGE ) zFrmType = "RANGE";
30603 if( pWin->eFrmType==TK_GROUPS ) zFrmType = "GROUPS";
30604 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s%s",zFrmType,
30605 pWin->bImplicitFrame ? " (implied)" : "");
30606 sqlite3TreeViewItem(pView, zBuf, (--nElement)>0);
30607 sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1);
30608 sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0);
30609 sqlite3TreeViewPop(pView);
30610 }
30611 if( pWin->eExclude ){
30612 char zBuf[30];
30613 const char *zExclude;
30614 switch( pWin->eExclude ){
30615 case TK_NO: zExclude = "NO OTHERS"; break;
30616 case TK_CURRENT: zExclude = "CURRENT ROW"; break;
30617 case TK_GROUP: zExclude = "GROUP"; break;
30618 case TK_TIES: zExclude = "TIES"; break;
30619 default:
30620 sqlite3_snprintf(sizeof(zBuf),zBuf,"invalid(%d)", pWin->eExclude);
30621 zExclude = zBuf;
30622 break;
30623 }
30624 sqlite3TreeViewPush(pView, 0);
30625 sqlite3TreeViewLine(pView, "EXCLUDE %s", zExclude);
30626 sqlite3TreeViewPop(pView);
30627 }
30628 sqlite3TreeViewPop(pView);
30629}
30630#endif /* SQLITE_OMIT_WINDOWFUNC */
30631
30632#ifndef SQLITE_OMIT_WINDOWFUNC
30633/*
30634** Generate a human-readable explanation for a Window Function object
30635*/
30636SQLITE_PRIVATE void sqlite3TreeViewWinFunc(TreeView *pView, const Window *pWin, u8 more){
30637 pView = sqlite3TreeViewPush(pView, more);
30638 sqlite3TreeViewLine(pView, "WINFUNC %s(%d)",
30639 pWin->pFunc->zName, pWin->pFunc->nArg);
30640 sqlite3TreeViewWindow(pView, pWin, 0);
30641 sqlite3TreeViewPop(pView);
30642}
30643#endif /* SQLITE_OMIT_WINDOWFUNC */
30644
30645/*
30646** Generate a human-readable explanation of an expression tree.
30647*/
30648SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
30649 const char *zBinOp = 0; /* Binary operator */
30650 const char *zUniOp = 0; /* Unary operator */
30651 char zFlgs[200];
30652 pView = sqlite3TreeViewPush(pView, moreToFollow);
30653 if( pExpr==0 ){
30654 sqlite3TreeViewLine(pView, "nil");
30655 sqlite3TreeViewPop(pView);
30656 return;
30657 }
30658 if( pExpr->flags || pExpr->affExpr || pExpr->vvaFlags ){
30659 StrAccum x;
30660 sqlite3StrAccumInit(&x, 0, zFlgs, sizeof(zFlgs), 0);
30661 sqlite3_str_appendf(&x, " fg.af=%x.%c",
30662 pExpr->flags, pExpr->affExpr ? pExpr->affExpr : 'n');
30663 if( ExprHasProperty(pExpr, EP_FromJoin) ){
30664 sqlite3_str_appendf(&x, " iRJT=%d", pExpr->iRightJoinTable);
30665 }
30666 if( ExprHasProperty(pExpr, EP_FromDDL) ){
30667 sqlite3_str_appendf(&x, " DDL");
30668 }
30669 if( ExprHasVVAProperty(pExpr, EP_Immutable) ){
30670 sqlite3_str_appendf(&x, " IMMUTABLE");
30671 }
30672 sqlite3StrAccumFinish(&x);
30673 }else{
30674 zFlgs[0] = 0;
30675 }
30676 switch( pExpr->op ){
30677 case TK_AGG_COLUMN: {
30678 sqlite3TreeViewLine(pView, "AGG{%d:%d}%s",
30679 pExpr->iTable, pExpr->iColumn, zFlgs);
30680 break;
30681 }
30682 case TK_COLUMN: {
30683 if( pExpr->iTable<0 ){
30684 /* This only happens when coding check constraints */
30685 char zOp2[16];
30686 if( pExpr->op2 ){
30687 sqlite3_snprintf(sizeof(zOp2),zOp2," op2=0x%02x",pExpr->op2);
30688 }else{
30689 zOp2[0] = 0;
30690 }
30691 sqlite3TreeViewLine(pView, "COLUMN(%d)%s%s",
30692 pExpr->iColumn, zFlgs, zOp2);
30693 }else{
30694 assert( ExprUseYTab(pExpr) );
30695 sqlite3TreeViewLine(pView, "{%d:%d} pTab=%p%s",
30696 pExpr->iTable, pExpr->iColumn,
30697 pExpr->y.pTab, zFlgs);
30698 }
30699 if( ExprHasProperty(pExpr, EP_FixedCol) ){
30700 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
30701 }
30702 break;
30703 }
30704 case TK_INTEGER: {
30705 if( pExpr->flags & EP_IntValue ){
30706 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
30707 }else{
30708 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
30709 }
30710 break;
30711 }
30712#ifndef SQLITE_OMIT_FLOATING_POINT
30713 case TK_FLOAT: {
30714 assert( !ExprHasProperty(pExpr, EP_IntValue) );
30715 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
30716 break;
30717 }
30718#endif
30719 case TK_STRING: {
30720 assert( !ExprHasProperty(pExpr, EP_IntValue) );
30721 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
30722 break;
30723 }
30724 case TK_NULL: {
30725 sqlite3TreeViewLine(pView,"NULL");
30726 break;
30727 }
30728 case TK_TRUEFALSE: {
30729 sqlite3TreeViewLine(pView,"%s%s",
30730 sqlite3ExprTruthValue(pExpr) ? "TRUE" : "FALSE", zFlgs);
30731 break;
30732 }
30733#ifndef SQLITE_OMIT_BLOB_LITERAL
30734 case TK_BLOB: {
30735 assert( !ExprHasProperty(pExpr, EP_IntValue) );
30736 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
30737 break;
30738 }
30739#endif
30740 case TK_VARIABLE: {
30741 assert( !ExprHasProperty(pExpr, EP_IntValue) );
30742 sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
30743 pExpr->u.zToken, pExpr->iColumn);
30744 break;
30745 }
30746 case TK_REGISTER: {
30747 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
30748 break;
30749 }
30750 case TK_ID: {
30751 assert( !ExprHasProperty(pExpr, EP_IntValue) );
30752 sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
30753 break;
30754 }
30755#ifndef SQLITE_OMIT_CAST
30756 case TK_CAST: {
30757 /* Expressions of the form: CAST(pLeft AS token) */
30758 assert( !ExprHasProperty(pExpr, EP_IntValue) );
30759 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
30760 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
30761 break;
30762 }
30763#endif /* SQLITE_OMIT_CAST */
30764 case TK_LT: zBinOp = "LT"; break;
30765 case TK_LE: zBinOp = "LE"; break;
30766 case TK_GT: zBinOp = "GT"; break;
30767 case TK_GE: zBinOp = "GE"; break;
30768 case TK_NE: zBinOp = "NE"; break;
30769 case TK_EQ: zBinOp = "EQ"; break;
30770 case TK_IS: zBinOp = "IS"; break;
30771 case TK_ISNOT: zBinOp = "ISNOT"; break;
30772 case TK_AND: zBinOp = "AND"; break;
30773 case TK_OR: zBinOp = "OR"; break;
30774 case TK_PLUS: zBinOp = "ADD"; break;
30775 case TK_STAR: zBinOp = "MUL"; break;
30776 case TK_MINUS: zBinOp = "SUB"; break;
30777 case TK_REM: zBinOp = "REM"; break;
30778 case TK_BITAND: zBinOp = "BITAND"; break;
30779 case TK_BITOR: zBinOp = "BITOR"; break;
30780 case TK_SLASH: zBinOp = "DIV"; break;
30781 case TK_LSHIFT: zBinOp = "LSHIFT"; break;
30782 case TK_RSHIFT: zBinOp = "RSHIFT"; break;
30783 case TK_CONCAT: zBinOp = "CONCAT"; break;
30784 case TK_DOT: zBinOp = "DOT"; break;
30785 case TK_LIMIT: zBinOp = "LIMIT"; break;
30786
30787 case TK_UMINUS: zUniOp = "UMINUS"; break;
30788 case TK_UPLUS: zUniOp = "UPLUS"; break;
30789 case TK_BITNOT: zUniOp = "BITNOT"; break;
30790 case TK_NOT: zUniOp = "NOT"; break;
30791 case TK_ISNULL: zUniOp = "ISNULL"; break;
30792 case TK_NOTNULL: zUniOp = "NOTNULL"; break;
30793
30794 case TK_TRUTH: {
30795 int x;
30796 const char *azOp[] = {
30797 "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE"
30798 };
30799 assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT );
30800 assert( pExpr->pRight );
30801 assert( sqlite3ExprSkipCollate(pExpr->pRight)->op==TK_TRUEFALSE );
30802 x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight);
30803 zUniOp = azOp[x];
30804 break;
30805 }
30806
30807 case TK_SPAN: {
30808 assert( !ExprHasProperty(pExpr, EP_IntValue) );
30809 sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
30810 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
30811 break;
30812 }
30813
30814 case TK_COLLATE: {
30815 /* COLLATE operators without the EP_Collate flag are intended to
30816 ** emulate collation associated with a table column. These show
30817 ** up in the treeview output as "SOFT-COLLATE". Explicit COLLATE
30818 ** operators that appear in the original SQL always have the
30819 ** EP_Collate bit set and appear in treeview output as just "COLLATE" */
30820 assert( !ExprHasProperty(pExpr, EP_IntValue) );
30821 sqlite3TreeViewLine(pView, "%sCOLLATE %Q%s",
30822 !ExprHasProperty(pExpr, EP_Collate) ? "SOFT-" : "",
30823 pExpr->u.zToken, zFlgs);
30824 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
30825 break;
30826 }
30827
30828 case TK_AGG_FUNCTION:
30829 case TK_FUNCTION: {
30830 ExprList *pFarg; /* List of function arguments */
30831 Window *pWin;
30832 if( ExprHasProperty(pExpr, EP_TokenOnly) ){
30833 pFarg = 0;
30834 pWin = 0;
30835 }else{
30836 assert( ExprUseXList(pExpr) );
30837 pFarg = pExpr->x.pList;
30838#ifndef SQLITE_OMIT_WINDOWFUNC
30839 pWin = ExprHasProperty(pExpr, EP_WinFunc) ? pExpr->y.pWin : 0;
30840#else
30841 pWin = 0;
30842#endif
30843 }
30844 assert( !ExprHasProperty(pExpr, EP_IntValue) );
30845 if( pExpr->op==TK_AGG_FUNCTION ){
30846 sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q%s agg=%d[%d]/%p",
30847 pExpr->op2, pExpr->u.zToken, zFlgs,
30848 pExpr->pAggInfo ? pExpr->pAggInfo->selId : 0,
30849 pExpr->iAgg, pExpr->pAggInfo);
30850 }else if( pExpr->op2!=0 ){
30851 const char *zOp2;
30852 char zBuf[8];
30853 sqlite3_snprintf(sizeof(zBuf),zBuf,"0x%02x",pExpr->op2);
30854 zOp2 = zBuf;
30855 if( pExpr->op2==NC_IsCheck ) zOp2 = "NC_IsCheck";
30856 if( pExpr->op2==NC_IdxExpr ) zOp2 = "NC_IdxExpr";
30857 if( pExpr->op2==NC_PartIdx ) zOp2 = "NC_PartIdx";
30858 if( pExpr->op2==NC_GenCol ) zOp2 = "NC_GenCol";
30859 sqlite3TreeViewLine(pView, "FUNCTION %Q%s op2=%s",
30860 pExpr->u.zToken, zFlgs, zOp2);
30861 }else{
30862 sqlite3TreeViewLine(pView, "FUNCTION %Q%s", pExpr->u.zToken, zFlgs);
30863 }
30864 if( pFarg ){
30865 sqlite3TreeViewExprList(pView, pFarg, pWin!=0, 0);
30866 }
30867#ifndef SQLITE_OMIT_WINDOWFUNC
30868 if( pWin ){
30869 sqlite3TreeViewWindow(pView, pWin, 0);
30870 }
30871#endif
30872 break;
30873 }
30874#ifndef SQLITE_OMIT_SUBQUERY
30875 case TK_EXISTS: {
30876 assert( ExprUseXSelect(pExpr) );
30877 sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags);
30878 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
30879 break;
30880 }
30881 case TK_SELECT: {
30882 assert( ExprUseXSelect(pExpr) );
30883 sqlite3TreeViewLine(pView, "subquery-expr flags=0x%x", pExpr->flags);
30884 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
30885 break;
30886 }
30887 case TK_IN: {
30888 sqlite3TreeViewLine(pView, "IN flags=0x%x", pExpr->flags);
30889 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
30890 if( ExprUseXSelect(pExpr) ){
30891 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
30892 }else{
30893 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
30894 }
30895 break;
30896 }
30897#endif /* SQLITE_OMIT_SUBQUERY */
30898
30899 /*
30900 ** x BETWEEN y AND z
30901 **
30902 ** This is equivalent to
30903 **
30904 ** x>=y AND x<=z
30905 **
30906 ** X is stored in pExpr->pLeft.
30907 ** Y is stored in pExpr->pList->a[0].pExpr.
30908 ** Z is stored in pExpr->pList->a[1].pExpr.
30909 */
30910 case TK_BETWEEN: {
30911 const Expr *pX, *pY, *pZ;
30912 pX = pExpr->pLeft;
30913 assert( ExprUseXList(pExpr) );
30914 assert( pExpr->x.pList->nExpr==2 );
30915 pY = pExpr->x.pList->a[0].pExpr;
30916 pZ = pExpr->x.pList->a[1].pExpr;
30917 sqlite3TreeViewLine(pView, "BETWEEN");
30918 sqlite3TreeViewExpr(pView, pX, 1);
30919 sqlite3TreeViewExpr(pView, pY, 1);
30920 sqlite3TreeViewExpr(pView, pZ, 0);
30921 break;
30922 }
30923 case TK_TRIGGER: {
30924 /* If the opcode is TK_TRIGGER, then the expression is a reference
30925 ** to a column in the new.* or old.* pseudo-tables available to
30926 ** trigger programs. In this case Expr.iTable is set to 1 for the
30927 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
30928 ** is set to the column of the pseudo-table to read, or to -1 to
30929 ** read the rowid field.
30930 */
30931 sqlite3TreeViewLine(pView, "%s(%d)",
30932 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
30933 break;
30934 }
30935 case TK_CASE: {
30936 sqlite3TreeViewLine(pView, "CASE");
30937 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
30938 assert( ExprUseXList(pExpr) );
30939 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
30940 break;
30941 }
30942#ifndef SQLITE_OMIT_TRIGGER
30943 case TK_RAISE: {
30944 const char *zType = "unk";
30945 switch( pExpr->affExpr ){
30946 case OE_Rollback: zType = "rollback"; break;
30947 case OE_Abort: zType = "abort"; break;
30948 case OE_Fail: zType = "fail"; break;
30949 case OE_Ignore: zType = "ignore"; break;
30950 }
30951 assert( !ExprHasProperty(pExpr, EP_IntValue) );
30952 sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
30953 break;
30954 }
30955#endif
30956 case TK_MATCH: {
30957 sqlite3TreeViewLine(pView, "MATCH {%d:%d}%s",
30958 pExpr->iTable, pExpr->iColumn, zFlgs);
30959 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
30960 break;
30961 }
30962 case TK_VECTOR: {
30963 char *z = sqlite3_mprintf("VECTOR%s",zFlgs);
30964 assert( ExprUseXList(pExpr) );
30965 sqlite3TreeViewBareExprList(pView, pExpr->x.pList, z);
30966 sqlite3_free(z);
30967 break;
30968 }
30969 case TK_SELECT_COLUMN: {
30970 sqlite3TreeViewLine(pView, "SELECT-COLUMN %d of [0..%d]%s",
30971 pExpr->iColumn, pExpr->iTable-1,
30972 pExpr->pRight==pExpr->pLeft ? " (SELECT-owner)" : "");
30973 assert( ExprUseXSelect(pExpr->pLeft) );
30974 sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0);
30975 break;
30976 }
30977 case TK_IF_NULL_ROW: {
30978 sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable);
30979 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
30980 break;
30981 }
30982 case TK_ERROR: {
30983 Expr tmp;
30984 sqlite3TreeViewLine(pView, "ERROR");
30985 tmp = *pExpr;
30986 tmp.op = pExpr->op2;
30987 sqlite3TreeViewExpr(pView, &tmp, 0);
30988 break;
30989 }
30990 case TK_ROW: {
30991 if( pExpr->iColumn<=0 ){
30992 sqlite3TreeViewLine(pView, "First FROM table rowid");
30993 }else{
30994 sqlite3TreeViewLine(pView, "First FROM table column %d",
30995 pExpr->iColumn-1);
30996 }
30997 break;
30998 }
30999 default: {
31000 sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
31001 break;
31002 }
31003 }
31004 if( zBinOp ){
31005 sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs);
31006 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
31007 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
31008 }else if( zUniOp ){
31009 sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs);
31010 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
31011 }
31012 sqlite3TreeViewPop(pView);
31013}
31014
31015
31016/*
31017** Generate a human-readable explanation of an expression list.
31018*/
31019SQLITE_PRIVATE void sqlite3TreeViewBareExprList(
31020 TreeView *pView,
31021 const ExprList *pList,
31022 const char *zLabel
31023){
31024 if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
31025 if( pList==0 ){
31026 sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
31027 }else{
31028 int i;
31029 sqlite3TreeViewLine(pView, "%s", zLabel);
31030 for(i=0; i<pList->nExpr; i++){
31031 int j = pList->a[i].u.x.iOrderByCol;
31032 char *zName = pList->a[i].zEName;
31033 int moreToFollow = i<pList->nExpr - 1;
31034 if( pList->a[i].eEName!=ENAME_NAME ) zName = 0;
31035 if( j || zName ){
31036 sqlite3TreeViewPush(pView, moreToFollow);
31037 moreToFollow = 0;
31038 sqlite3TreeViewLine(pView, 0);
31039 if( zName ){
31040 fprintf(stdout, "AS %s ", zName);
31041 }
31042 if( j ){
31043 fprintf(stdout, "iOrderByCol=%d", j);
31044 }
31045 fprintf(stdout, "\n");
31046 fflush(stdout);
31047 }
31048 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, moreToFollow);
31049 if( j || zName ){
31050 sqlite3TreeViewPop(pView);
31051 }
31052 }
31053 }
31054}
31055SQLITE_PRIVATE void sqlite3TreeViewExprList(
31056 TreeView *pView,
31057 const ExprList *pList,
31058 u8 moreToFollow,
31059 const char *zLabel
31060){
31061 pView = sqlite3TreeViewPush(pView, moreToFollow);
31062 sqlite3TreeViewBareExprList(pView, pList, zLabel);
31063 sqlite3TreeViewPop(pView);
31064}
31065
31066#endif /* SQLITE_DEBUG */
31067
31068/************** End of treeview.c ********************************************/
31069/************** Begin file random.c ******************************************/
31070/*
31071** 2001 September 15
31072**
31073** The author disclaims copyright to this source code. In place of
31074** a legal notice, here is a blessing:
31075**
31076** May you do good and not evil.
31077** May you find forgiveness for yourself and forgive others.
31078** May you share freely, never taking more than you give.
31079**
31080*************************************************************************
31081** This file contains code to implement a pseudo-random number
31082** generator (PRNG) for SQLite.
31083**
31084** Random numbers are used by some of the database backends in order
31085** to generate random integer keys for tables or random filenames.
31086*/
31087/* #include "sqliteInt.h" */
31088
31089
31090/* All threads share a single random number generator.
31091** This structure is the current state of the generator.
31092*/
31093static SQLITE_WSD struct sqlite3PrngType {
31094 unsigned char isInit; /* True if initialized */
31095 unsigned char i, j; /* State variables */
31096 unsigned char s[256]; /* State variables */
31097} sqlite3Prng;
31098
31099/*
31100** Return N random bytes.
31101*/
31102SQLITE_API void sqlite3_randomness(int N, void *pBuf){
31103 unsigned char t;
31104 unsigned char *zBuf = pBuf;
31105
31106 /* The "wsdPrng" macro will resolve to the pseudo-random number generator
31107 ** state vector. If writable static data is unsupported on the target,
31108 ** we have to locate the state vector at run-time. In the more common
31109 ** case where writable static data is supported, wsdPrng can refer directly
31110 ** to the "sqlite3Prng" state vector declared above.
31111 */
31112#ifdef SQLITE_OMIT_WSD
31113 struct sqlite3PrngType *p = &GLOBAL(struct sqlite3PrngType, sqlite3Prng);
31114# define wsdPrng p[0]
31115#else
31116# define wsdPrng sqlite3Prng
31117#endif
31118
31119#if SQLITE_THREADSAFE
31120 sqlite3_mutex *mutex;
31121#endif
31122
31123#ifndef SQLITE_OMIT_AUTOINIT
31124 if( sqlite3_initialize() ) return;
31125#endif
31126
31127#if SQLITE_THREADSAFE
31128 mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
31129#endif
31130
31131 sqlite3_mutex_enter(p: mutex);
31132 if( N<=0 || pBuf==0 ){
31133 wsdPrng.isInit = 0;
31134 sqlite3_mutex_leave(p: mutex);
31135 return;
31136 }
31137
31138 /* Initialize the state of the random number generator once,
31139 ** the first time this routine is called. The seed value does
31140 ** not need to contain a lot of randomness since we are not
31141 ** trying to do secure encryption or anything like that...
31142 **
31143 ** Nothing in this file or anywhere else in SQLite does any kind of
31144 ** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random
31145 ** number generator) not as an encryption device.
31146 */
31147 if( !wsdPrng.isInit ){
31148 sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs: 0);
31149 int i;
31150 char k[256];
31151 wsdPrng.j = 0;
31152 wsdPrng.i = 0;
31153 if( NEVER(pVfs==0) ){
31154 memset(s: k, c: 0, n: sizeof(k));
31155 }else{
31156 sqlite3OsRandomness(pVfs, nByte: 256, zBufOut: k);
31157 }
31158 for(i=0; i<256; i++){
31159 wsdPrng.s[i] = (u8)i;
31160 }
31161 for(i=0; i<256; i++){
31162 wsdPrng.j += wsdPrng.s[i] + k[i];
31163 t = wsdPrng.s[wsdPrng.j];
31164 wsdPrng.s[wsdPrng.j] = wsdPrng.s[i];
31165 wsdPrng.s[i] = t;
31166 }
31167 wsdPrng.isInit = 1;
31168 }
31169
31170 assert( N>0 );
31171 do{
31172 wsdPrng.i++;
31173 t = wsdPrng.s[wsdPrng.i];
31174 wsdPrng.j += t;
31175 wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
31176 wsdPrng.s[wsdPrng.j] = t;
31177 t += wsdPrng.s[wsdPrng.i];
31178 *(zBuf++) = wsdPrng.s[t];
31179 }while( --N );
31180 sqlite3_mutex_leave(p: mutex);
31181}
31182
31183#ifndef SQLITE_UNTESTABLE
31184/*
31185** For testing purposes, we sometimes want to preserve the state of
31186** PRNG and restore the PRNG to its saved state at a later time, or
31187** to reset the PRNG to its initial state. These routines accomplish
31188** those tasks.
31189**
31190** The sqlite3_test_control() interface calls these routines to
31191** control the PRNG.
31192*/
31193static SQLITE_WSD struct sqlite3PrngType sqlite3SavedPrng;
31194SQLITE_PRIVATE void sqlite3PrngSaveState(void){
31195 memcpy(
31196 dest: &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
31197 src: &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
31198 n: sizeof(sqlite3Prng)
31199 );
31200}
31201SQLITE_PRIVATE void sqlite3PrngRestoreState(void){
31202 memcpy(
31203 dest: &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
31204 src: &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
31205 n: sizeof(sqlite3Prng)
31206 );
31207}
31208#endif /* SQLITE_UNTESTABLE */
31209
31210/************** End of random.c **********************************************/
31211/************** Begin file threads.c *****************************************/
31212/*
31213** 2012 July 21
31214**
31215** The author disclaims copyright to this source code. In place of
31216** a legal notice, here is a blessing:
31217**
31218** May you do good and not evil.
31219** May you find forgiveness for yourself and forgive others.
31220** May you share freely, never taking more than you give.
31221**
31222******************************************************************************
31223**
31224** This file presents a simple cross-platform threading interface for
31225** use internally by SQLite.
31226**
31227** A "thread" can be created using sqlite3ThreadCreate(). This thread
31228** runs independently of its creator until it is joined using
31229** sqlite3ThreadJoin(), at which point it terminates.
31230**
31231** Threads do not have to be real. It could be that the work of the
31232** "thread" is done by the main thread at either the sqlite3ThreadCreate()
31233** or sqlite3ThreadJoin() call. This is, in fact, what happens in
31234** single threaded systems. Nothing in SQLite requires multiple threads.
31235** This interface exists so that applications that want to take advantage
31236** of multiple cores can do so, while also allowing applications to stay
31237** single-threaded if desired.
31238*/
31239/* #include "sqliteInt.h" */
31240#if SQLITE_OS_WIN
31241/* # include "os_win.h" */
31242#endif
31243
31244#if SQLITE_MAX_WORKER_THREADS>0
31245
31246/********************************* Unix Pthreads ****************************/
31247#if SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) && SQLITE_THREADSAFE>0
31248
31249#define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
31250/* #include <pthread.h> */
31251
31252/* A running thread */
31253struct SQLiteThread {
31254 pthread_t tid; /* Thread ID */
31255 int done; /* Set to true when thread finishes */
31256 void *pOut; /* Result returned by the thread */
31257 void *(*xTask)(void*); /* The thread routine */
31258 void *pIn; /* Argument to the thread */
31259};
31260
31261/* Create a new thread */
31262SQLITE_PRIVATE int sqlite3ThreadCreate(
31263 SQLiteThread **ppThread, /* OUT: Write the thread object here */
31264 void *(*xTask)(void*), /* Routine to run in a separate thread */
31265 void *pIn /* Argument passed into xTask() */
31266){
31267 SQLiteThread *p;
31268 int rc;
31269
31270 assert( ppThread!=0 );
31271 assert( xTask!=0 );
31272 /* This routine is never used in single-threaded mode */
31273 assert( sqlite3GlobalConfig.bCoreMutex!=0 );
31274
31275 *ppThread = 0;
31276 p = sqlite3Malloc(n: sizeof(*p));
31277 if( p==0 ) return SQLITE_NOMEM_BKPT;
31278 memset(s: p, c: 0, n: sizeof(*p));
31279 p->xTask = xTask;
31280 p->pIn = pIn;
31281 /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a
31282 ** function that returns SQLITE_ERROR when passed the argument 200, that
31283 ** forces worker threads to run sequentially and deterministically
31284 ** for testing purposes. */
31285 if( sqlite3FaultSim(200) ){
31286 rc = 1;
31287 }else{
31288 rc = pthread_create(newthread: &p->tid, attr: 0, start_routine: xTask, arg: pIn);
31289 }
31290 if( rc ){
31291 p->done = 1;
31292 p->pOut = xTask(pIn);
31293 }
31294 *ppThread = p;
31295 return SQLITE_OK;
31296}
31297
31298/* Get the results of the thread */
31299SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
31300 int rc;
31301
31302 assert( ppOut!=0 );
31303 if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
31304 if( p->done ){
31305 *ppOut = p->pOut;
31306 rc = SQLITE_OK;
31307 }else{
31308 rc = pthread_join(th: p->tid, thread_return: ppOut) ? SQLITE_ERROR : SQLITE_OK;
31309 }
31310 sqlite3_free(p);
31311 return rc;
31312}
31313
31314#endif /* SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) */
31315/******************************** End Unix Pthreads *************************/
31316
31317
31318/********************************* Win32 Threads ****************************/
31319#if SQLITE_OS_WIN_THREADS
31320
31321#define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
31322#include <process.h>
31323
31324/* A running thread */
31325struct SQLiteThread {
31326 void *tid; /* The thread handle */
31327 unsigned id; /* The thread identifier */
31328 void *(*xTask)(void*); /* The routine to run as a thread */
31329 void *pIn; /* Argument to xTask */
31330 void *pResult; /* Result of xTask */
31331};
31332
31333/* Thread procedure Win32 compatibility shim */
31334static unsigned __stdcall sqlite3ThreadProc(
31335 void *pArg /* IN: Pointer to the SQLiteThread structure */
31336){
31337 SQLiteThread *p = (SQLiteThread *)pArg;
31338
31339 assert( p!=0 );
31340#if 0
31341 /*
31342 ** This assert appears to trigger spuriously on certain
31343 ** versions of Windows, possibly due to _beginthreadex()
31344 ** and/or CreateThread() not fully setting their thread
31345 ** ID parameter before starting the thread.
31346 */
31347 assert( p->id==GetCurrentThreadId() );
31348#endif
31349 assert( p->xTask!=0 );
31350 p->pResult = p->xTask(p->pIn);
31351
31352 _endthreadex(0);
31353 return 0; /* NOT REACHED */
31354}
31355
31356/* Create a new thread */
31357SQLITE_PRIVATE int sqlite3ThreadCreate(
31358 SQLiteThread **ppThread, /* OUT: Write the thread object here */
31359 void *(*xTask)(void*), /* Routine to run in a separate thread */
31360 void *pIn /* Argument passed into xTask() */
31361){
31362 SQLiteThread *p;
31363
31364 assert( ppThread!=0 );
31365 assert( xTask!=0 );
31366 *ppThread = 0;
31367 p = sqlite3Malloc(sizeof(*p));
31368 if( p==0 ) return SQLITE_NOMEM_BKPT;
31369 /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a
31370 ** function that returns SQLITE_ERROR when passed the argument 200, that
31371 ** forces worker threads to run sequentially and deterministically
31372 ** (via the sqlite3FaultSim() term of the conditional) for testing
31373 ** purposes. */
31374 if( sqlite3GlobalConfig.bCoreMutex==0 || sqlite3FaultSim(200) ){
31375 memset(p, 0, sizeof(*p));
31376 }else{
31377 p->xTask = xTask;
31378 p->pIn = pIn;
31379 p->tid = (void*)_beginthreadex(0, 0, sqlite3ThreadProc, p, 0, &p->id);
31380 if( p->tid==0 ){
31381 memset(p, 0, sizeof(*p));
31382 }
31383 }
31384 if( p->xTask==0 ){
31385 p->id = GetCurrentThreadId();
31386 p->pResult = xTask(pIn);
31387 }
31388 *ppThread = p;
31389 return SQLITE_OK;
31390}
31391
31392SQLITE_PRIVATE DWORD sqlite3Win32Wait(HANDLE hObject); /* os_win.c */
31393
31394/* Get the results of the thread */
31395SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
31396 DWORD rc;
31397 BOOL bRc;
31398
31399 assert( ppOut!=0 );
31400 if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
31401 if( p->xTask==0 ){
31402 /* assert( p->id==GetCurrentThreadId() ); */
31403 rc = WAIT_OBJECT_0;
31404 assert( p->tid==0 );
31405 }else{
31406 assert( p->id!=0 && p->id!=GetCurrentThreadId() );
31407 rc = sqlite3Win32Wait((HANDLE)p->tid);
31408 assert( rc!=WAIT_IO_COMPLETION );
31409 bRc = CloseHandle((HANDLE)p->tid);
31410 assert( bRc );
31411 }
31412 if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult;
31413 sqlite3_free(p);
31414 return (rc==WAIT_OBJECT_0) ? SQLITE_OK : SQLITE_ERROR;
31415}
31416
31417#endif /* SQLITE_OS_WIN_THREADS */
31418/******************************** End Win32 Threads *************************/
31419
31420
31421/********************************* Single-Threaded **************************/
31422#ifndef SQLITE_THREADS_IMPLEMENTED
31423/*
31424** This implementation does not actually create a new thread. It does the
31425** work of the thread in the main thread, when either the thread is created
31426** or when it is joined
31427*/
31428
31429/* A running thread */
31430struct SQLiteThread {
31431 void *(*xTask)(void*); /* The routine to run as a thread */
31432 void *pIn; /* Argument to xTask */
31433 void *pResult; /* Result of xTask */
31434};
31435
31436/* Create a new thread */
31437SQLITE_PRIVATE int sqlite3ThreadCreate(
31438 SQLiteThread **ppThread, /* OUT: Write the thread object here */
31439 void *(*xTask)(void*), /* Routine to run in a separate thread */
31440 void *pIn /* Argument passed into xTask() */
31441){
31442 SQLiteThread *p;
31443
31444 assert( ppThread!=0 );
31445 assert( xTask!=0 );
31446 *ppThread = 0;
31447 p = sqlite3Malloc(sizeof(*p));
31448 if( p==0 ) return SQLITE_NOMEM_BKPT;
31449 if( (SQLITE_PTR_TO_INT(p)/17)&1 ){
31450 p->xTask = xTask;
31451 p->pIn = pIn;
31452 }else{
31453 p->xTask = 0;
31454 p->pResult = xTask(pIn);
31455 }
31456 *ppThread = p;
31457 return SQLITE_OK;
31458}
31459
31460/* Get the results of the thread */
31461SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
31462
31463 assert( ppOut!=0 );
31464 if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
31465 if( p->xTask ){
31466 *ppOut = p->xTask(p->pIn);
31467 }else{
31468 *ppOut = p->pResult;
31469 }
31470 sqlite3_free(p);
31471
31472#if defined(SQLITE_TEST)
31473 {
31474 void *pTstAlloc = sqlite3Malloc(10);
31475 if (!pTstAlloc) return SQLITE_NOMEM_BKPT;
31476 sqlite3_free(pTstAlloc);
31477 }
31478#endif
31479
31480 return SQLITE_OK;
31481}
31482
31483#endif /* !defined(SQLITE_THREADS_IMPLEMENTED) */
31484/****************************** End Single-Threaded *************************/
31485#endif /* SQLITE_MAX_WORKER_THREADS>0 */
31486
31487/************** End of threads.c *********************************************/
31488/************** Begin file utf.c *********************************************/
31489/*
31490** 2004 April 13
31491**
31492** The author disclaims copyright to this source code. In place of
31493** a legal notice, here is a blessing:
31494**
31495** May you do good and not evil.
31496** May you find forgiveness for yourself and forgive others.
31497** May you share freely, never taking more than you give.
31498**
31499*************************************************************************
31500** This file contains routines used to translate between UTF-8,
31501** UTF-16, UTF-16BE, and UTF-16LE.
31502**
31503** Notes on UTF-8:
31504**
31505** Byte-0 Byte-1 Byte-2 Byte-3 Value
31506** 0xxxxxxx 00000000 00000000 0xxxxxxx
31507** 110yyyyy 10xxxxxx 00000000 00000yyy yyxxxxxx
31508** 1110zzzz 10yyyyyy 10xxxxxx 00000000 zzzzyyyy yyxxxxxx
31509** 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx 000uuuuu zzzzyyyy yyxxxxxx
31510**
31511**
31512** Notes on UTF-16: (with wwww+1==uuuuu)
31513**
31514** Word-0 Word-1 Value
31515** 110110ww wwzzzzyy 110111yy yyxxxxxx 000uuuuu zzzzyyyy yyxxxxxx
31516** zzzzyyyy yyxxxxxx 00000000 zzzzyyyy yyxxxxxx
31517**
31518**
31519** BOM or Byte Order Mark:
31520** 0xff 0xfe little-endian utf-16 follows
31521** 0xfe 0xff big-endian utf-16 follows
31522**
31523*/
31524/* #include "sqliteInt.h" */
31525/* #include <assert.h> */
31526/* #include "vdbeInt.h" */
31527
31528#if !defined(SQLITE_AMALGAMATION) && SQLITE_BYTEORDER==0
31529/*
31530** The following constant value is used by the SQLITE_BIGENDIAN and
31531** SQLITE_LITTLEENDIAN macros.
31532*/
31533SQLITE_PRIVATE const int sqlite3one = 1;
31534#endif /* SQLITE_AMALGAMATION && SQLITE_BYTEORDER==0 */
31535
31536/*
31537** This lookup table is used to help decode the first byte of
31538** a multi-byte UTF8 character.
31539*/
31540static const unsigned char sqlite3Utf8Trans1[] = {
31541 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
31542 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
31543 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
31544 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
31545 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
31546 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
31547 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
31548 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
31549};
31550
31551
31552#define WRITE_UTF8(zOut, c) { \
31553 if( c<0x00080 ){ \
31554 *zOut++ = (u8)(c&0xFF); \
31555 } \
31556 else if( c<0x00800 ){ \
31557 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); \
31558 *zOut++ = 0x80 + (u8)(c & 0x3F); \
31559 } \
31560 else if( c<0x10000 ){ \
31561 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); \
31562 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
31563 *zOut++ = 0x80 + (u8)(c & 0x3F); \
31564 }else{ \
31565 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); \
31566 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); \
31567 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
31568 *zOut++ = 0x80 + (u8)(c & 0x3F); \
31569 } \
31570}
31571
31572#define WRITE_UTF16LE(zOut, c) { \
31573 if( c<=0xFFFF ){ \
31574 *zOut++ = (u8)(c&0x00FF); \
31575 *zOut++ = (u8)((c>>8)&0x00FF); \
31576 }else{ \
31577 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
31578 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
31579 *zOut++ = (u8)(c&0x00FF); \
31580 *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \
31581 } \
31582}
31583
31584#define WRITE_UTF16BE(zOut, c) { \
31585 if( c<=0xFFFF ){ \
31586 *zOut++ = (u8)((c>>8)&0x00FF); \
31587 *zOut++ = (u8)(c&0x00FF); \
31588 }else{ \
31589 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
31590 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
31591 *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \
31592 *zOut++ = (u8)(c&0x00FF); \
31593 } \
31594}
31595
31596/*
31597** Translate a single UTF-8 character. Return the unicode value.
31598**
31599** During translation, assume that the byte that zTerm points
31600** is a 0x00.
31601**
31602** Write a pointer to the next unread byte back into *pzNext.
31603**
31604** Notes On Invalid UTF-8:
31605**
31606** * This routine never allows a 7-bit character (0x00 through 0x7f) to
31607** be encoded as a multi-byte character. Any multi-byte character that
31608** attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd.
31609**
31610** * This routine never allows a UTF16 surrogate value to be encoded.
31611** If a multi-byte character attempts to encode a value between
31612** 0xd800 and 0xe000 then it is rendered as 0xfffd.
31613**
31614** * Bytes in the range of 0x80 through 0xbf which occur as the first
31615** byte of a character are interpreted as single-byte characters
31616** and rendered as themselves even though they are technically
31617** invalid characters.
31618**
31619** * This routine accepts over-length UTF8 encodings
31620** for unicode values 0x80 and greater. It does not change over-length
31621** encodings to 0xfffd as some systems recommend.
31622*/
31623#define READ_UTF8(zIn, zTerm, c) \
31624 c = *(zIn++); \
31625 if( c>=0xc0 ){ \
31626 c = sqlite3Utf8Trans1[c-0xc0]; \
31627 while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ \
31628 c = (c<<6) + (0x3f & *(zIn++)); \
31629 } \
31630 if( c<0x80 \
31631 || (c&0xFFFFF800)==0xD800 \
31632 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
31633 }
31634SQLITE_PRIVATE u32 sqlite3Utf8Read(
31635 const unsigned char **pz /* Pointer to string from which to read char */
31636){
31637 unsigned int c;
31638
31639 /* Same as READ_UTF8() above but without the zTerm parameter.
31640 ** For this routine, we assume the UTF8 string is always zero-terminated.
31641 */
31642 c = *((*pz)++);
31643 if( c>=0xc0 ){
31644 c = sqlite3Utf8Trans1[c-0xc0];
31645 while( (*(*pz) & 0xc0)==0x80 ){
31646 c = (c<<6) + (0x3f & *((*pz)++));
31647 }
31648 if( c<0x80
31649 || (c&0xFFFFF800)==0xD800
31650 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; }
31651 }
31652 return c;
31653}
31654
31655
31656
31657
31658/*
31659** If the TRANSLATE_TRACE macro is defined, the value of each Mem is
31660** printed on stderr on the way into and out of sqlite3VdbeMemTranslate().
31661*/
31662/* #define TRANSLATE_TRACE 1 */
31663
31664#ifndef SQLITE_OMIT_UTF16
31665/*
31666** This routine transforms the internal text encoding used by pMem to
31667** desiredEnc. It is an error if the string is already of the desired
31668** encoding, or if *pMem does not contain a string value.
31669*/
31670SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
31671 sqlite3_int64 len; /* Maximum length of output string in bytes */
31672 unsigned char *zOut; /* Output buffer */
31673 unsigned char *zIn; /* Input iterator */
31674 unsigned char *zTerm; /* End of input */
31675 unsigned char *z; /* Output iterator */
31676 unsigned int c;
31677
31678 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
31679 assert( pMem->flags&MEM_Str );
31680 assert( pMem->enc!=desiredEnc );
31681 assert( pMem->enc!=0 );
31682 assert( pMem->n>=0 );
31683
31684#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
31685 {
31686 StrAccum acc;
31687 char zBuf[1000];
31688 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
31689 sqlite3VdbeMemPrettyPrint(pMem, &acc);
31690 fprintf(stderr, "INPUT: %s\n", sqlite3StrAccumFinish(&acc));
31691 }
31692#endif
31693
31694 /* If the translation is between UTF-16 little and big endian, then
31695 ** all that is required is to swap the byte order. This case is handled
31696 ** differently from the others.
31697 */
31698 if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
31699 u8 temp;
31700 int rc;
31701 rc = sqlite3VdbeMemMakeWriteable(pMem);
31702 if( rc!=SQLITE_OK ){
31703 assert( rc==SQLITE_NOMEM );
31704 return SQLITE_NOMEM_BKPT;
31705 }
31706 zIn = (u8*)pMem->z;
31707 zTerm = &zIn[pMem->n&~1];
31708 while( zIn<zTerm ){
31709 temp = *zIn;
31710 *zIn = *(zIn+1);
31711 zIn++;
31712 *zIn++ = temp;
31713 }
31714 pMem->enc = desiredEnc;
31715 goto translate_out;
31716 }
31717
31718 /* Set len to the maximum number of bytes required in the output buffer. */
31719 if( desiredEnc==SQLITE_UTF8 ){
31720 /* When converting from UTF-16, the maximum growth results from
31721 ** translating a 2-byte character to a 4-byte UTF-8 character.
31722 ** A single byte is required for the output string
31723 ** nul-terminator.
31724 */
31725 pMem->n &= ~1;
31726 len = 2 * (sqlite3_int64)pMem->n + 1;
31727 }else{
31728 /* When converting from UTF-8 to UTF-16 the maximum growth is caused
31729 ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
31730 ** character. Two bytes are required in the output buffer for the
31731 ** nul-terminator.
31732 */
31733 len = 2 * (sqlite3_int64)pMem->n + 2;
31734 }
31735
31736 /* Set zIn to point at the start of the input buffer and zTerm to point 1
31737 ** byte past the end.
31738 **
31739 ** Variable zOut is set to point at the output buffer, space obtained
31740 ** from sqlite3_malloc().
31741 */
31742 zIn = (u8*)pMem->z;
31743 zTerm = &zIn[pMem->n];
31744 zOut = sqlite3DbMallocRaw(db: pMem->db, n: len);
31745 if( !zOut ){
31746 return SQLITE_NOMEM_BKPT;
31747 }
31748 z = zOut;
31749
31750 if( pMem->enc==SQLITE_UTF8 ){
31751 if( desiredEnc==SQLITE_UTF16LE ){
31752 /* UTF-8 -> UTF-16 Little-endian */
31753 while( zIn<zTerm ){
31754 READ_UTF8(zIn, zTerm, c);
31755 WRITE_UTF16LE(z, c);
31756 }
31757 }else{
31758 assert( desiredEnc==SQLITE_UTF16BE );
31759 /* UTF-8 -> UTF-16 Big-endian */
31760 while( zIn<zTerm ){
31761 READ_UTF8(zIn, zTerm, c);
31762 WRITE_UTF16BE(z, c);
31763 }
31764 }
31765 pMem->n = (int)(z - zOut);
31766 *z++ = 0;
31767 }else{
31768 assert( desiredEnc==SQLITE_UTF8 );
31769 if( pMem->enc==SQLITE_UTF16LE ){
31770 /* UTF-16 Little-endian -> UTF-8 */
31771 while( zIn<zTerm ){
31772 c = *(zIn++);
31773 c += (*(zIn++))<<8;
31774 if( c>=0xd800 && c<0xe000 ){
31775#ifdef SQLITE_REPLACE_INVALID_UTF
31776 if( c>=0xdc00 || zIn>=zTerm ){
31777 c = 0xfffd;
31778 }else{
31779 int c2 = *(zIn++);
31780 c2 += (*(zIn++))<<8;
31781 if( c2<0xdc00 || c2>=0xe000 ){
31782 zIn -= 2;
31783 c = 0xfffd;
31784 }else{
31785 c = ((c&0x3ff)<<10) + (c2&0x3ff) + 0x10000;
31786 }
31787 }
31788#else
31789 if( zIn<zTerm ){
31790 int c2 = (*zIn++);
31791 c2 += ((*zIn++)<<8);
31792 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);
31793 }
31794#endif
31795 }
31796 WRITE_UTF8(z, c);
31797 }
31798 }else{
31799 /* UTF-16 Big-endian -> UTF-8 */
31800 while( zIn<zTerm ){
31801 c = (*(zIn++))<<8;
31802 c += *(zIn++);
31803 if( c>=0xd800 && c<0xe000 ){
31804#ifdef SQLITE_REPLACE_INVALID_UTF
31805 if( c>=0xdc00 || zIn>=zTerm ){
31806 c = 0xfffd;
31807 }else{
31808 int c2 = (*(zIn++))<<8;
31809 c2 += *(zIn++);
31810 if( c2<0xdc00 || c2>=0xe000 ){
31811 zIn -= 2;
31812 c = 0xfffd;
31813 }else{
31814 c = ((c&0x3ff)<<10) + (c2&0x3ff) + 0x10000;
31815 }
31816 }
31817#else
31818 if( zIn<zTerm ){
31819 int c2 = ((*zIn++)<<8);
31820 c2 += (*zIn++);
31821 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);
31822 }
31823#endif
31824 }
31825 WRITE_UTF8(z, c);
31826 }
31827 }
31828 pMem->n = (int)(z - zOut);
31829 }
31830 *z = 0;
31831 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
31832
31833 c = MEM_Str|MEM_Term|(pMem->flags&(MEM_AffMask|MEM_Subtype));
31834 sqlite3VdbeMemRelease(p: pMem);
31835 pMem->flags = c;
31836 pMem->enc = desiredEnc;
31837 pMem->z = (char*)zOut;
31838 pMem->zMalloc = pMem->z;
31839 pMem->szMalloc = sqlite3DbMallocSize(db: pMem->db, p: pMem->z);
31840
31841translate_out:
31842#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
31843 {
31844 StrAccum acc;
31845 char zBuf[1000];
31846 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
31847 sqlite3VdbeMemPrettyPrint(pMem, &acc);
31848 fprintf(stderr, "OUTPUT: %s\n", sqlite3StrAccumFinish(&acc));
31849 }
31850#endif
31851 return SQLITE_OK;
31852}
31853#endif /* SQLITE_OMIT_UTF16 */
31854
31855#ifndef SQLITE_OMIT_UTF16
31856/*
31857** This routine checks for a byte-order mark at the beginning of the
31858** UTF-16 string stored in *pMem. If one is present, it is removed and
31859** the encoding of the Mem adjusted. This routine does not do any
31860** byte-swapping, it just sets Mem.enc appropriately.
31861**
31862** The allocation (static, dynamic etc.) and encoding of the Mem may be
31863** changed by this function.
31864*/
31865SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem){
31866 int rc = SQLITE_OK;
31867 u8 bom = 0;
31868
31869 assert( pMem->n>=0 );
31870 if( pMem->n>1 ){
31871 u8 b1 = *(u8 *)pMem->z;
31872 u8 b2 = *(((u8 *)pMem->z) + 1);
31873 if( b1==0xFE && b2==0xFF ){
31874 bom = SQLITE_UTF16BE;
31875 }
31876 if( b1==0xFF && b2==0xFE ){
31877 bom = SQLITE_UTF16LE;
31878 }
31879 }
31880
31881 if( bom ){
31882 rc = sqlite3VdbeMemMakeWriteable(pMem);
31883 if( rc==SQLITE_OK ){
31884 pMem->n -= 2;
31885 memmove(dest: pMem->z, src: &pMem->z[2], n: pMem->n);
31886 pMem->z[pMem->n] = '\0';
31887 pMem->z[pMem->n+1] = '\0';
31888 pMem->flags |= MEM_Term;
31889 pMem->enc = bom;
31890 }
31891 }
31892 return rc;
31893}
31894#endif /* SQLITE_OMIT_UTF16 */
31895
31896/*
31897** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
31898** return the number of unicode characters in pZ up to (but not including)
31899** the first 0x00 byte. If nByte is not less than zero, return the
31900** number of unicode characters in the first nByte of pZ (or up to
31901** the first 0x00, whichever comes first).
31902*/
31903SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *zIn, int nByte){
31904 int r = 0;
31905 const u8 *z = (const u8*)zIn;
31906 const u8 *zTerm;
31907 if( nByte>=0 ){
31908 zTerm = &z[nByte];
31909 }else{
31910 zTerm = (const u8*)(-1);
31911 }
31912 assert( z<=zTerm );
31913 while( *z!=0 && z<zTerm ){
31914 SQLITE_SKIP_UTF8(z);
31915 r++;
31916 }
31917 return r;
31918}
31919
31920/* This test function is not currently used by the automated test-suite.
31921** Hence it is only available in debug builds.
31922*/
31923#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
31924/*
31925** Translate UTF-8 to UTF-8.
31926**
31927** This has the effect of making sure that the string is well-formed
31928** UTF-8. Miscoded characters are removed.
31929**
31930** The translation is done in-place and aborted if the output
31931** overruns the input.
31932*/
31933SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char *zIn){
31934 unsigned char *zOut = zIn;
31935 unsigned char *zStart = zIn;
31936 u32 c;
31937
31938 while( zIn[0] && zOut<=zIn ){
31939 c = sqlite3Utf8Read((const u8**)&zIn);
31940 if( c!=0xfffd ){
31941 WRITE_UTF8(zOut, c);
31942 }
31943 }
31944 *zOut = 0;
31945 return (int)(zOut - zStart);
31946}
31947#endif
31948
31949#ifndef SQLITE_OMIT_UTF16
31950/*
31951** Convert a UTF-16 string in the native encoding into a UTF-8 string.
31952** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
31953** be freed by the calling function.
31954**
31955** NULL is returned if there is an allocation error.
31956*/
31957SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte, u8 enc){
31958 Mem m;
31959 memset(s: &m, c: 0, n: sizeof(m));
31960 m.db = db;
31961 sqlite3VdbeMemSetStr(&m, z, nByte, enc, SQLITE_STATIC);
31962 sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);
31963 if( db->mallocFailed ){
31964 sqlite3VdbeMemRelease(p: &m);
31965 m.z = 0;
31966 }
31967 assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
31968 assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
31969 assert( m.z || db->mallocFailed );
31970 return m.z;
31971}
31972
31973/*
31974** zIn is a UTF-16 encoded unicode string at least nChar characters long.
31975** Return the number of bytes in the first nChar unicode characters
31976** in pZ. nChar must be non-negative.
31977*/
31978SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *zIn, int nChar){
31979 int c;
31980 unsigned char const *z = zIn;
31981 int n = 0;
31982
31983 if( SQLITE_UTF16NATIVE==SQLITE_UTF16LE ) z++;
31984 while( n<nChar ){
31985 c = z[0];
31986 z += 2;
31987 if( c>=0xd8 && c<0xdc && z[0]>=0xdc && z[0]<0xe0 ) z += 2;
31988 n++;
31989 }
31990 return (int)(z-(unsigned char const *)zIn)
31991 - (SQLITE_UTF16NATIVE==SQLITE_UTF16LE);
31992}
31993
31994#if defined(SQLITE_TEST)
31995/*
31996** This routine is called from the TCL test function "translate_selftest".
31997** It checks that the primitives for serializing and deserializing
31998** characters in each encoding are inverses of each other.
31999*/
32000SQLITE_PRIVATE void sqlite3UtfSelfTest(void){
32001 unsigned int i, t;
32002 unsigned char zBuf[20];
32003 unsigned char *z;
32004 int n;
32005 unsigned int c;
32006
32007 for(i=0; i<0x00110000; i++){
32008 z = zBuf;
32009 WRITE_UTF8(z, i);
32010 n = (int)(z-zBuf);
32011 assert( n>0 && n<=4 );
32012 z[0] = 0;
32013 z = zBuf;
32014 c = sqlite3Utf8Read((const u8**)&z);
32015 t = i;
32016 if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
32017 if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
32018 assert( c==t );
32019 assert( (z-zBuf)==n );
32020 }
32021}
32022#endif /* SQLITE_TEST */
32023#endif /* SQLITE_OMIT_UTF16 */
32024
32025/************** End of utf.c *************************************************/
32026/************** Begin file util.c ********************************************/
32027/*
32028** 2001 September 15
32029**
32030** The author disclaims copyright to this source code. In place of
32031** a legal notice, here is a blessing:
32032**
32033** May you do good and not evil.
32034** May you find forgiveness for yourself and forgive others.
32035** May you share freely, never taking more than you give.
32036**
32037*************************************************************************
32038** Utility functions used throughout sqlite.
32039**
32040** This file contains functions for allocating memory, comparing
32041** strings, and stuff like that.
32042**
32043*/
32044/* #include "sqliteInt.h" */
32045/* #include <stdarg.h> */
32046#ifndef SQLITE_OMIT_FLOATING_POINT
32047#include <math.h>
32048#endif
32049
32050/*
32051** Calls to sqlite3FaultSim() are used to simulate a failure during testing,
32052** or to bypass normal error detection during testing in order to let
32053** execute proceed futher downstream.
32054**
32055** In deployment, sqlite3FaultSim() *always* return SQLITE_OK (0). The
32056** sqlite3FaultSim() function only returns non-zero during testing.
32057**
32058** During testing, if the test harness has set a fault-sim callback using
32059** a call to sqlite3_test_control(SQLITE_TESTCTRL_FAULT_INSTALL), then
32060** each call to sqlite3FaultSim() is relayed to that application-supplied
32061** callback and the integer return value form the application-supplied
32062** callback is returned by sqlite3FaultSim().
32063**
32064** The integer argument to sqlite3FaultSim() is a code to identify which
32065** sqlite3FaultSim() instance is being invoked. Each call to sqlite3FaultSim()
32066** should have a unique code. To prevent legacy testing applications from
32067** breaking, the codes should not be changed or reused.
32068*/
32069#ifndef SQLITE_UNTESTABLE
32070SQLITE_PRIVATE int sqlite3FaultSim(int iTest){
32071 int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback;
32072 return xCallback ? xCallback(iTest) : SQLITE_OK;
32073}
32074#endif
32075
32076#ifndef SQLITE_OMIT_FLOATING_POINT
32077/*
32078** Return true if the floating point value is Not a Number (NaN).
32079**
32080** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN.
32081** Otherwise, we have our own implementation that works on most systems.
32082*/
32083SQLITE_PRIVATE int sqlite3IsNaN(double x){
32084 int rc; /* The value return */
32085#if !SQLITE_HAVE_ISNAN && !HAVE_ISNAN
32086 u64 y;
32087 memcpy(dest: &y,src: &x,n: sizeof(y));
32088 rc = IsNaN(y);
32089#else
32090 rc = isnan(x);
32091#endif /* HAVE_ISNAN */
32092 testcase( rc );
32093 return rc;
32094}
32095#endif /* SQLITE_OMIT_FLOATING_POINT */
32096
32097/*
32098** Compute a string length that is limited to what can be stored in
32099** lower 30 bits of a 32-bit signed integer.
32100**
32101** The value returned will never be negative. Nor will it ever be greater
32102** than the actual length of the string. For very long strings (greater
32103** than 1GiB) the value returned might be less than the true string length.
32104*/
32105SQLITE_PRIVATE int sqlite3Strlen30(const char *z){
32106 if( z==0 ) return 0;
32107 return 0x3fffffff & (int)strlen(s: z);
32108}
32109
32110/*
32111** Return the declared type of a column. Or return zDflt if the column
32112** has no declared type.
32113**
32114** The column type is an extra string stored after the zero-terminator on
32115** the column name if and only if the COLFLAG_HASTYPE flag is set.
32116*/
32117SQLITE_PRIVATE char *sqlite3ColumnType(Column *pCol, char *zDflt){
32118 if( pCol->colFlags & COLFLAG_HASTYPE ){
32119 return pCol->zCnName + strlen(s: pCol->zCnName) + 1;
32120 }else if( pCol->eCType ){
32121 assert( pCol->eCType<=SQLITE_N_STDTYPE );
32122 return (char*)sqlite3StdType[pCol->eCType-1];
32123 }else{
32124 return zDflt;
32125 }
32126}
32127
32128/*
32129** Helper function for sqlite3Error() - called rarely. Broken out into
32130** a separate routine to avoid unnecessary register saves on entry to
32131** sqlite3Error().
32132*/
32133static SQLITE_NOINLINE void sqlite3ErrorFinish(sqlite3 *db, int err_code){
32134 if( db->pErr ) sqlite3ValueSetNull(db->pErr);
32135 sqlite3SystemError(db, err_code);
32136}
32137
32138/*
32139** Set the current error code to err_code and clear any prior error message.
32140** Also set iSysErrno (by calling sqlite3System) if the err_code indicates
32141** that would be appropriate.
32142*/
32143SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code){
32144 assert( db!=0 );
32145 db->errCode = err_code;
32146 if( err_code || db->pErr ) sqlite3ErrorFinish(db, err_code);
32147}
32148
32149/*
32150** The equivalent of sqlite3Error(db, SQLITE_OK). Clear the error state
32151** and error message.
32152*/
32153SQLITE_PRIVATE void sqlite3ErrorClear(sqlite3 *db){
32154 assert( db!=0 );
32155 db->errCode = SQLITE_OK;
32156 if( db->pErr ) sqlite3ValueSetNull(db->pErr);
32157}
32158
32159/*
32160** Load the sqlite3.iSysErrno field if that is an appropriate thing
32161** to do based on the SQLite error code in rc.
32162*/
32163SQLITE_PRIVATE void sqlite3SystemError(sqlite3 *db, int rc){
32164 if( rc==SQLITE_IOERR_NOMEM ) return;
32165 rc &= 0xff;
32166 if( rc==SQLITE_CANTOPEN || rc==SQLITE_IOERR ){
32167 db->iSysErrno = sqlite3OsGetLastError(pVfs: db->pVfs);
32168 }
32169}
32170
32171/*
32172** Set the most recent error code and error string for the sqlite
32173** handle "db". The error code is set to "err_code".
32174**
32175** If it is not NULL, string zFormat specifies the format of the
32176** error string in the style of the printf functions: The following
32177** format characters are allowed:
32178**
32179** %s Insert a string
32180** %z A string that should be freed after use
32181** %d Insert an integer
32182** %T Insert a token
32183** %S Insert the first element of a SrcList
32184**
32185** zFormat and any string tokens that follow it are assumed to be
32186** encoded in UTF-8.
32187**
32188** To clear the most recent error for sqlite handle "db", sqlite3Error
32189** should be called with err_code set to SQLITE_OK and zFormat set
32190** to NULL.
32191*/
32192SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3 *db, int err_code, const char *zFormat, ...){
32193 assert( db!=0 );
32194 db->errCode = err_code;
32195 sqlite3SystemError(db, rc: err_code);
32196 if( zFormat==0 ){
32197 sqlite3Error(db, err_code);
32198 }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){
32199 char *z;
32200 va_list ap;
32201 va_start(ap, zFormat);
32202 z = sqlite3VMPrintf(db, zFormat, ap);
32203 va_end(ap);
32204 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
32205 }
32206}
32207
32208/*
32209** Add an error message to pParse->zErrMsg and increment pParse->nErr.
32210** The following formatting characters are allowed:
32211**
32212** %s Insert a string
32213** %z A string that should be freed after use
32214** %d Insert an integer
32215** %T Insert a token
32216** %S Insert the first element of a SrcList
32217**
32218** This function should be used to report any error that occurs while
32219** compiling an SQL statement (i.e. within sqlite3_prepare()). The
32220** last thing the sqlite3_prepare() function does is copy the error
32221** stored by this function into the database handle using sqlite3Error().
32222** Functions sqlite3Error() or sqlite3ErrorWithMsg() should be used
32223** during statement execution (sqlite3_step() etc.).
32224*/
32225SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
32226 char *zMsg;
32227 va_list ap;
32228 sqlite3 *db = pParse->db;
32229 va_start(ap, zFormat);
32230 zMsg = sqlite3VMPrintf(db, zFormat, ap);
32231 va_end(ap);
32232 if( db->suppressErr ){
32233 sqlite3DbFree(db, p: zMsg);
32234 }else{
32235 pParse->nErr++;
32236 sqlite3DbFree(db, p: pParse->zErrMsg);
32237 pParse->zErrMsg = zMsg;
32238 pParse->rc = SQLITE_ERROR;
32239 pParse->pWith = 0;
32240 }
32241}
32242
32243/*
32244** If database connection db is currently parsing SQL, then transfer
32245** error code errCode to that parser if the parser has not already
32246** encountered some other kind of error.
32247*/
32248SQLITE_PRIVATE int sqlite3ErrorToParser(sqlite3 *db, int errCode){
32249 Parse *pParse;
32250 if( db==0 || (pParse = db->pParse)==0 ) return errCode;
32251 pParse->rc = errCode;
32252 pParse->nErr++;
32253 return errCode;
32254}
32255
32256/*
32257** Convert an SQL-style quoted string into a normal string by removing
32258** the quote characters. The conversion is done in-place. If the
32259** input does not begin with a quote character, then this routine
32260** is a no-op.
32261**
32262** The input string must be zero-terminated. A new zero-terminator
32263** is added to the dequoted string.
32264**
32265** The return value is -1 if no dequoting occurs or the length of the
32266** dequoted string, exclusive of the zero terminator, if dequoting does
32267** occur.
32268**
32269** 2002-02-14: This routine is extended to remove MS-Access style
32270** brackets from around identifiers. For example: "[a-b-c]" becomes
32271** "a-b-c".
32272*/
32273SQLITE_PRIVATE void sqlite3Dequote(char *z){
32274 char quote;
32275 int i, j;
32276 if( z==0 ) return;
32277 quote = z[0];
32278 if( !sqlite3Isquote(quote) ) return;
32279 if( quote=='[' ) quote = ']';
32280 for(i=1, j=0;; i++){
32281 assert( z[i] );
32282 if( z[i]==quote ){
32283 if( z[i+1]==quote ){
32284 z[j++] = quote;
32285 i++;
32286 }else{
32287 break;
32288 }
32289 }else{
32290 z[j++] = z[i];
32291 }
32292 }
32293 z[j] = 0;
32294}
32295SQLITE_PRIVATE void sqlite3DequoteExpr(Expr *p){
32296 assert( !ExprHasProperty(p, EP_IntValue) );
32297 assert( sqlite3Isquote(p->u.zToken[0]) );
32298 p->flags |= p->u.zToken[0]=='"' ? EP_Quoted|EP_DblQuoted : EP_Quoted;
32299 sqlite3Dequote(z: p->u.zToken);
32300}
32301
32302/*
32303** If the input token p is quoted, try to adjust the token to remove
32304** the quotes. This is not always possible:
32305**
32306** "abc" -> abc
32307** "ab""cd" -> (not possible because of the interior "")
32308**
32309** Remove the quotes if possible. This is a optimization. The overall
32310** system should still return the correct answer even if this routine
32311** is always a no-op.
32312*/
32313SQLITE_PRIVATE void sqlite3DequoteToken(Token *p){
32314 unsigned int i;
32315 if( p->n<2 ) return;
32316 if( !sqlite3Isquote(p->z[0]) ) return;
32317 for(i=1; i<p->n-1; i++){
32318 if( sqlite3Isquote(p->z[i]) ) return;
32319 }
32320 p->n -= 2;
32321 p->z++;
32322}
32323
32324/*
32325** Generate a Token object from a string
32326*/
32327SQLITE_PRIVATE void sqlite3TokenInit(Token *p, char *z){
32328 p->z = z;
32329 p->n = sqlite3Strlen30(z);
32330}
32331
32332/* Convenient short-hand */
32333#define UpperToLower sqlite3UpperToLower
32334
32335/*
32336** Some systems have stricmp(). Others have strcasecmp(). Because
32337** there is no consistency, we will define our own.
32338**
32339** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
32340** sqlite3_strnicmp() APIs allow applications and extensions to compare
32341** the contents of two buffers containing UTF-8 strings in a
32342** case-independent fashion, using the same definition of "case
32343** independence" that SQLite uses internally when comparing identifiers.
32344*/
32345SQLITE_API int sqlite3_stricmp(const char *zLeft, const char *zRight){
32346 if( zLeft==0 ){
32347 return zRight ? -1 : 0;
32348 }else if( zRight==0 ){
32349 return 1;
32350 }
32351 return sqlite3StrICmp(zLeft, zRight);
32352}
32353SQLITE_PRIVATE int sqlite3StrICmp(const char *zLeft, const char *zRight){
32354 unsigned char *a, *b;
32355 int c, x;
32356 a = (unsigned char *)zLeft;
32357 b = (unsigned char *)zRight;
32358 for(;;){
32359 c = *a;
32360 x = *b;
32361 if( c==x ){
32362 if( c==0 ) break;
32363 }else{
32364 c = (int)UpperToLower[c] - (int)UpperToLower[x];
32365 if( c ) break;
32366 }
32367 a++;
32368 b++;
32369 }
32370 return c;
32371}
32372SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
32373 register unsigned char *a, *b;
32374 if( zLeft==0 ){
32375 return zRight ? -1 : 0;
32376 }else if( zRight==0 ){
32377 return 1;
32378 }
32379 a = (unsigned char *)zLeft;
32380 b = (unsigned char *)zRight;
32381 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
32382 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
32383}
32384
32385/*
32386** Compute an 8-bit hash on a string that is insensitive to case differences
32387*/
32388SQLITE_PRIVATE u8 sqlite3StrIHash(const char *z){
32389 u8 h = 0;
32390 if( z==0 ) return 0;
32391 while( z[0] ){
32392 h += UpperToLower[(unsigned char)z[0]];
32393 z++;
32394 }
32395 return h;
32396}
32397
32398/*
32399** Compute 10 to the E-th power. Examples: E==1 results in 10.
32400** E==2 results in 100. E==50 results in 1.0e50.
32401**
32402** This routine only works for values of E between 1 and 341.
32403*/
32404static LONGDOUBLE_TYPE sqlite3Pow10(int E){
32405#if defined(_MSC_VER)
32406 static const LONGDOUBLE_TYPE x[] = {
32407 1.0e+001L,
32408 1.0e+002L,
32409 1.0e+004L,
32410 1.0e+008L,
32411 1.0e+016L,
32412 1.0e+032L,
32413 1.0e+064L,
32414 1.0e+128L,
32415 1.0e+256L
32416 };
32417 LONGDOUBLE_TYPE r = 1.0;
32418 int i;
32419 assert( E>=0 && E<=307 );
32420 for(i=0; E!=0; i++, E >>=1){
32421 if( E & 1 ) r *= x[i];
32422 }
32423 return r;
32424#else
32425 LONGDOUBLE_TYPE x = 10.0;
32426 LONGDOUBLE_TYPE r = 1.0;
32427 while(1){
32428 if( E & 1 ) r *= x;
32429 E >>= 1;
32430 if( E==0 ) break;
32431 x *= x;
32432 }
32433 return r;
32434#endif
32435}
32436
32437/*
32438** The string z[] is an text representation of a real number.
32439** Convert this string to a double and write it into *pResult.
32440**
32441** The string z[] is length bytes in length (bytes, not characters) and
32442** uses the encoding enc. The string is not necessarily zero-terminated.
32443**
32444** Return TRUE if the result is a valid real number (or integer) and FALSE
32445** if the string is empty or contains extraneous text. More specifically
32446** return
32447** 1 => The input string is a pure integer
32448** 2 or more => The input has a decimal point or eNNN clause
32449** 0 or less => The input string is not a valid number
32450** -1 => Not a valid number, but has a valid prefix which
32451** includes a decimal point and/or an eNNN clause
32452**
32453** Valid numbers are in one of these formats:
32454**
32455** [+-]digits[E[+-]digits]
32456** [+-]digits.[digits][E[+-]digits]
32457** [+-].digits[E[+-]digits]
32458**
32459** Leading and trailing whitespace is ignored for the purpose of determining
32460** validity.
32461**
32462** If some prefix of the input string is a valid number, this routine
32463** returns FALSE but it still converts the prefix and writes the result
32464** into *pResult.
32465*/
32466#if defined(_MSC_VER)
32467#pragma warning(disable : 4756)
32468#endif
32469SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){
32470#ifndef SQLITE_OMIT_FLOATING_POINT
32471 int incr;
32472 const char *zEnd;
32473 /* sign * significand * (10 ^ (esign * exponent)) */
32474 int sign = 1; /* sign of significand */
32475 i64 s = 0; /* significand */
32476 int d = 0; /* adjust exponent for shifting decimal point */
32477 int esign = 1; /* sign of exponent */
32478 int e = 0; /* exponent */
32479 int eValid = 1; /* True exponent is either not used or is well-formed */
32480 double result;
32481 int nDigit = 0; /* Number of digits processed */
32482 int eType = 1; /* 1: pure integer, 2+: fractional -1 or less: bad UTF16 */
32483
32484 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
32485 *pResult = 0.0; /* Default return value, in case of an error */
32486 if( length==0 ) return 0;
32487
32488 if( enc==SQLITE_UTF8 ){
32489 incr = 1;
32490 zEnd = z + length;
32491 }else{
32492 int i;
32493 incr = 2;
32494 length &= ~1;
32495 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
32496 testcase( enc==SQLITE_UTF16LE );
32497 testcase( enc==SQLITE_UTF16BE );
32498 for(i=3-enc; i<length && z[i]==0; i+=2){}
32499 if( i<length ) eType = -100;
32500 zEnd = &z[i^1];
32501 z += (enc&1);
32502 }
32503
32504 /* skip leading spaces */
32505 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
32506 if( z>=zEnd ) return 0;
32507
32508 /* get sign of significand */
32509 if( *z=='-' ){
32510 sign = -1;
32511 z+=incr;
32512 }else if( *z=='+' ){
32513 z+=incr;
32514 }
32515
32516 /* copy max significant digits to significand */
32517 while( z<zEnd && sqlite3Isdigit(*z) ){
32518 s = s*10 + (*z - '0');
32519 z+=incr; nDigit++;
32520 if( s>=((LARGEST_INT64-9)/10) ){
32521 /* skip non-significant significand digits
32522 ** (increase exponent by d to shift decimal left) */
32523 while( z<zEnd && sqlite3Isdigit(*z) ){ z+=incr; d++; }
32524 }
32525 }
32526 if( z>=zEnd ) goto do_atof_calc;
32527
32528 /* if decimal point is present */
32529 if( *z=='.' ){
32530 z+=incr;
32531 eType++;
32532 /* copy digits from after decimal to significand
32533 ** (decrease exponent by d to shift decimal right) */
32534 while( z<zEnd && sqlite3Isdigit(*z) ){
32535 if( s<((LARGEST_INT64-9)/10) ){
32536 s = s*10 + (*z - '0');
32537 d--;
32538 nDigit++;
32539 }
32540 z+=incr;
32541 }
32542 }
32543 if( z>=zEnd ) goto do_atof_calc;
32544
32545 /* if exponent is present */
32546 if( *z=='e' || *z=='E' ){
32547 z+=incr;
32548 eValid = 0;
32549 eType++;
32550
32551 /* This branch is needed to avoid a (harmless) buffer overread. The
32552 ** special comment alerts the mutation tester that the correct answer
32553 ** is obtained even if the branch is omitted */
32554 if( z>=zEnd ) goto do_atof_calc; /*PREVENTS-HARMLESS-OVERREAD*/
32555
32556 /* get sign of exponent */
32557 if( *z=='-' ){
32558 esign = -1;
32559 z+=incr;
32560 }else if( *z=='+' ){
32561 z+=incr;
32562 }
32563 /* copy digits to exponent */
32564 while( z<zEnd && sqlite3Isdigit(*z) ){
32565 e = e<10000 ? (e*10 + (*z - '0')) : 10000;
32566 z+=incr;
32567 eValid = 1;
32568 }
32569 }
32570
32571 /* skip trailing spaces */
32572 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
32573
32574do_atof_calc:
32575 /* adjust exponent by d, and update sign */
32576 e = (e*esign) + d;
32577 if( e<0 ) {
32578 esign = -1;
32579 e *= -1;
32580 } else {
32581 esign = 1;
32582 }
32583
32584 if( s==0 ) {
32585 /* In the IEEE 754 standard, zero is signed. */
32586 result = sign<0 ? -(double)0 : (double)0;
32587 } else {
32588 /* Attempt to reduce exponent.
32589 **
32590 ** Branches that are not required for the correct answer but which only
32591 ** help to obtain the correct answer faster are marked with special
32592 ** comments, as a hint to the mutation tester.
32593 */
32594 while( e>0 ){ /*OPTIMIZATION-IF-TRUE*/
32595 if( esign>0 ){
32596 if( s>=(LARGEST_INT64/10) ) break; /*OPTIMIZATION-IF-FALSE*/
32597 s *= 10;
32598 }else{
32599 if( s%10!=0 ) break; /*OPTIMIZATION-IF-FALSE*/
32600 s /= 10;
32601 }
32602 e--;
32603 }
32604
32605 /* adjust the sign of significand */
32606 s = sign<0 ? -s : s;
32607
32608 if( e==0 ){ /*OPTIMIZATION-IF-TRUE*/
32609 result = (double)s;
32610 }else{
32611 /* attempt to handle extremely small/large numbers better */
32612 if( e>307 ){ /*OPTIMIZATION-IF-TRUE*/
32613 if( e<342 ){ /*OPTIMIZATION-IF-TRUE*/
32614 LONGDOUBLE_TYPE scale = sqlite3Pow10(E: e-308);
32615 if( esign<0 ){
32616 result = s / scale;
32617 result /= 1.0e+308;
32618 }else{
32619 result = s * scale;
32620 result *= 1.0e+308;
32621 }
32622 }else{ assert( e>=342 );
32623 if( esign<0 ){
32624 result = 0.0*s;
32625 }else{
32626#ifdef INFINITY
32627 result = INFINITY*s;
32628#else
32629 result = 1e308*1e308*s; /* Infinity */
32630#endif
32631 }
32632 }
32633 }else{
32634 LONGDOUBLE_TYPE scale = sqlite3Pow10(E: e);
32635 if( esign<0 ){
32636 result = s / scale;
32637 }else{
32638 result = s * scale;
32639 }
32640 }
32641 }
32642 }
32643
32644 /* store the result */
32645 *pResult = result;
32646
32647 /* return true if number and no extra non-whitespace chracters after */
32648 if( z==zEnd && nDigit>0 && eValid && eType>0 ){
32649 return eType;
32650 }else if( eType>=2 && (eType==3 || eValid) && nDigit>0 ){
32651 return -1;
32652 }else{
32653 return 0;
32654 }
32655#else
32656 return !sqlite3Atoi64(z, pResult, length, enc);
32657#endif /* SQLITE_OMIT_FLOATING_POINT */
32658}
32659#if defined(_MSC_VER)
32660#pragma warning(default : 4756)
32661#endif
32662
32663/*
32664** Render an signed 64-bit integer as text. Store the result in zOut[].
32665**
32666** The caller must ensure that zOut[] is at least 21 bytes in size.
32667*/
32668SQLITE_PRIVATE void sqlite3Int64ToText(i64 v, char *zOut){
32669 int i;
32670 u64 x;
32671 char zTemp[22];
32672 if( v<0 ){
32673 x = (v==SMALLEST_INT64) ? ((u64)1)<<63 : (u64)-v;
32674 }else{
32675 x = v;
32676 }
32677 i = sizeof(zTemp)-2;
32678 zTemp[sizeof(zTemp)-1] = 0;
32679 do{
32680 zTemp[i--] = (x%10) + '0';
32681 x = x/10;
32682 }while( x );
32683 if( v<0 ) zTemp[i--] = '-';
32684 memcpy(dest: zOut, src: &zTemp[i+1], n: sizeof(zTemp)-1-i);
32685}
32686
32687/*
32688** Compare the 19-character string zNum against the text representation
32689** value 2^63: 9223372036854775808. Return negative, zero, or positive
32690** if zNum is less than, equal to, or greater than the string.
32691** Note that zNum must contain exactly 19 characters.
32692**
32693** Unlike memcmp() this routine is guaranteed to return the difference
32694** in the values of the last digit if the only difference is in the
32695** last digit. So, for example,
32696**
32697** compare2pow63("9223372036854775800", 1)
32698**
32699** will return -8.
32700*/
32701static int compare2pow63(const char *zNum, int incr){
32702 int c = 0;
32703 int i;
32704 /* 012345678901234567 */
32705 const char *pow63 = "922337203685477580";
32706 for(i=0; c==0 && i<18; i++){
32707 c = (zNum[i*incr]-pow63[i])*10;
32708 }
32709 if( c==0 ){
32710 c = zNum[18*incr] - '8';
32711 testcase( c==(-1) );
32712 testcase( c==0 );
32713 testcase( c==(+1) );
32714 }
32715 return c;
32716}
32717
32718/*
32719** Convert zNum to a 64-bit signed integer. zNum must be decimal. This
32720** routine does *not* accept hexadecimal notation.
32721**
32722** Returns:
32723**
32724** -1 Not even a prefix of the input text looks like an integer
32725** 0 Successful transformation. Fits in a 64-bit signed integer.
32726** 1 Excess non-space text after the integer value
32727** 2 Integer too large for a 64-bit signed integer or is malformed
32728** 3 Special case of 9223372036854775808
32729**
32730** length is the number of bytes in the string (bytes, not characters).
32731** The string is not necessarily zero-terminated. The encoding is
32732** given by enc.
32733*/
32734SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
32735 int incr;
32736 u64 u = 0;
32737 int neg = 0; /* assume positive */
32738 int i;
32739 int c = 0;
32740 int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */
32741 int rc; /* Baseline return code */
32742 const char *zStart;
32743 const char *zEnd = zNum + length;
32744 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
32745 if( enc==SQLITE_UTF8 ){
32746 incr = 1;
32747 }else{
32748 incr = 2;
32749 length &= ~1;
32750 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
32751 for(i=3-enc; i<length && zNum[i]==0; i+=2){}
32752 nonNum = i<length;
32753 zEnd = &zNum[i^1];
32754 zNum += (enc&1);
32755 }
32756 while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr;
32757 if( zNum<zEnd ){
32758 if( *zNum=='-' ){
32759 neg = 1;
32760 zNum+=incr;
32761 }else if( *zNum=='+' ){
32762 zNum+=incr;
32763 }
32764 }
32765 zStart = zNum;
32766 while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
32767 for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
32768 u = u*10 + c - '0';
32769 }
32770 testcase( i==18*incr );
32771 testcase( i==19*incr );
32772 testcase( i==20*incr );
32773 if( u>LARGEST_INT64 ){
32774 /* This test and assignment is needed only to suppress UB warnings
32775 ** from clang and -fsanitize=undefined. This test and assignment make
32776 ** the code a little larger and slower, and no harm comes from omitting
32777 ** them, but we must appaise the undefined-behavior pharisees. */
32778 *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
32779 }else if( neg ){
32780 *pNum = -(i64)u;
32781 }else{
32782 *pNum = (i64)u;
32783 }
32784 rc = 0;
32785 if( i==0 && zStart==zNum ){ /* No digits */
32786 rc = -1;
32787 }else if( nonNum ){ /* UTF16 with high-order bytes non-zero */
32788 rc = 1;
32789 }else if( &zNum[i]<zEnd ){ /* Extra bytes at the end */
32790 int jj = i;
32791 do{
32792 if( !sqlite3Isspace(zNum[jj]) ){
32793 rc = 1; /* Extra non-space text after the integer */
32794 break;
32795 }
32796 jj += incr;
32797 }while( &zNum[jj]<zEnd );
32798 }
32799 if( i<19*incr ){
32800 /* Less than 19 digits, so we know that it fits in 64 bits */
32801 assert( u<=LARGEST_INT64 );
32802 return rc;
32803 }else{
32804 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */
32805 c = i>19*incr ? 1 : compare2pow63(zNum, incr);
32806 if( c<0 ){
32807 /* zNum is less than 9223372036854775808 so it fits */
32808 assert( u<=LARGEST_INT64 );
32809 return rc;
32810 }else{
32811 *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
32812 if( c>0 ){
32813 /* zNum is greater than 9223372036854775808 so it overflows */
32814 return 2;
32815 }else{
32816 /* zNum is exactly 9223372036854775808. Fits if negative. The
32817 ** special case 2 overflow if positive */
32818 assert( u-1==LARGEST_INT64 );
32819 return neg ? rc : 3;
32820 }
32821 }
32822 }
32823}
32824
32825/*
32826** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
32827** into a 64-bit signed integer. This routine accepts hexadecimal literals,
32828** whereas sqlite3Atoi64() does not.
32829**
32830** Returns:
32831**
32832** 0 Successful transformation. Fits in a 64-bit signed integer.
32833** 1 Excess text after the integer value
32834** 2 Integer too large for a 64-bit signed integer or is malformed
32835** 3 Special case of 9223372036854775808
32836*/
32837SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char *z, i64 *pOut){
32838#ifndef SQLITE_OMIT_HEX_INTEGER
32839 if( z[0]=='0'
32840 && (z[1]=='x' || z[1]=='X')
32841 ){
32842 u64 u = 0;
32843 int i, k;
32844 for(i=2; z[i]=='0'; i++){}
32845 for(k=i; sqlite3Isxdigit(z[k]); k++){
32846 u = u*16 + sqlite3HexToInt(h: z[k]);
32847 }
32848 memcpy(dest: pOut, src: &u, n: 8);
32849 return (z[k]==0 && k-i<=16) ? 0 : 2;
32850 }else
32851#endif /* SQLITE_OMIT_HEX_INTEGER */
32852 {
32853 return sqlite3Atoi64(zNum: z, pNum: pOut, length: sqlite3Strlen30(z), SQLITE_UTF8);
32854 }
32855}
32856
32857/*
32858** If zNum represents an integer that will fit in 32-bits, then set
32859** *pValue to that integer and return true. Otherwise return false.
32860**
32861** This routine accepts both decimal and hexadecimal notation for integers.
32862**
32863** Any non-numeric characters that following zNum are ignored.
32864** This is different from sqlite3Atoi64() which requires the
32865** input number to be zero-terminated.
32866*/
32867SQLITE_PRIVATE int sqlite3GetInt32(const char *zNum, int *pValue){
32868 sqlite_int64 v = 0;
32869 int i, c;
32870 int neg = 0;
32871 if( zNum[0]=='-' ){
32872 neg = 1;
32873 zNum++;
32874 }else if( zNum[0]=='+' ){
32875 zNum++;
32876 }
32877#ifndef SQLITE_OMIT_HEX_INTEGER
32878 else if( zNum[0]=='0'
32879 && (zNum[1]=='x' || zNum[1]=='X')
32880 && sqlite3Isxdigit(zNum[2])
32881 ){
32882 u32 u = 0;
32883 zNum += 2;
32884 while( zNum[0]=='0' ) zNum++;
32885 for(i=0; sqlite3Isxdigit(zNum[i]) && i<8; i++){
32886 u = u*16 + sqlite3HexToInt(h: zNum[i]);
32887 }
32888 if( (u&0x80000000)==0 && sqlite3Isxdigit(zNum[i])==0 ){
32889 memcpy(dest: pValue, src: &u, n: 4);
32890 return 1;
32891 }else{
32892 return 0;
32893 }
32894 }
32895#endif
32896 if( !sqlite3Isdigit(zNum[0]) ) return 0;
32897 while( zNum[0]=='0' ) zNum++;
32898 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
32899 v = v*10 + c;
32900 }
32901
32902 /* The longest decimal representation of a 32 bit integer is 10 digits:
32903 **
32904 ** 1234567890
32905 ** 2^31 -> 2147483648
32906 */
32907 testcase( i==10 );
32908 if( i>10 ){
32909 return 0;
32910 }
32911 testcase( v-neg==2147483647 );
32912 if( v-neg>2147483647 ){
32913 return 0;
32914 }
32915 if( neg ){
32916 v = -v;
32917 }
32918 *pValue = (int)v;
32919 return 1;
32920}
32921
32922/*
32923** Return a 32-bit integer value extracted from a string. If the
32924** string is not an integer, just return 0.
32925*/
32926SQLITE_PRIVATE int sqlite3Atoi(const char *z){
32927 int x = 0;
32928 sqlite3GetInt32(zNum: z, pValue: &x);
32929 return x;
32930}
32931
32932/*
32933** Try to convert z into an unsigned 32-bit integer. Return true on
32934** success and false if there is an error.
32935**
32936** Only decimal notation is accepted.
32937*/
32938SQLITE_PRIVATE int sqlite3GetUInt32(const char *z, u32 *pI){
32939 u64 v = 0;
32940 int i;
32941 for(i=0; sqlite3Isdigit(z[i]); i++){
32942 v = v*10 + z[i] - '0';
32943 if( v>4294967296LL ){ *pI = 0; return 0; }
32944 }
32945 if( i==0 || z[i]!=0 ){ *pI = 0; return 0; }
32946 *pI = (u32)v;
32947 return 1;
32948}
32949
32950/*
32951** The variable-length integer encoding is as follows:
32952**
32953** KEY:
32954** A = 0xxxxxxx 7 bits of data and one flag bit
32955** B = 1xxxxxxx 7 bits of data and one flag bit
32956** C = xxxxxxxx 8 bits of data
32957**
32958** 7 bits - A
32959** 14 bits - BA
32960** 21 bits - BBA
32961** 28 bits - BBBA
32962** 35 bits - BBBBA
32963** 42 bits - BBBBBA
32964** 49 bits - BBBBBBA
32965** 56 bits - BBBBBBBA
32966** 64 bits - BBBBBBBBC
32967*/
32968
32969/*
32970** Write a 64-bit variable-length integer to memory starting at p[0].
32971** The length of data write will be between 1 and 9 bytes. The number
32972** of bytes written is returned.
32973**
32974** A variable-length integer consists of the lower 7 bits of each byte
32975** for all bytes that have the 8th bit set and one byte with the 8th
32976** bit clear. Except, if we get to the 9th byte, it stores the full
32977** 8 bits and is the last byte.
32978*/
32979static int SQLITE_NOINLINE putVarint64(unsigned char *p, u64 v){
32980 int i, j, n;
32981 u8 buf[10];
32982 if( v & (((u64)0xff000000)<<32) ){
32983 p[8] = (u8)v;
32984 v >>= 8;
32985 for(i=7; i>=0; i--){
32986 p[i] = (u8)((v & 0x7f) | 0x80);
32987 v >>= 7;
32988 }
32989 return 9;
32990 }
32991 n = 0;
32992 do{
32993 buf[n++] = (u8)((v & 0x7f) | 0x80);
32994 v >>= 7;
32995 }while( v!=0 );
32996 buf[0] &= 0x7f;
32997 assert( n<=9 );
32998 for(i=0, j=n-1; j>=0; j--, i++){
32999 p[i] = buf[j];
33000 }
33001 return n;
33002}
33003SQLITE_PRIVATE int sqlite3PutVarint(unsigned char *p, u64 v){
33004 if( v<=0x7f ){
33005 p[0] = v&0x7f;
33006 return 1;
33007 }
33008 if( v<=0x3fff ){
33009 p[0] = ((v>>7)&0x7f)|0x80;
33010 p[1] = v&0x7f;
33011 return 2;
33012 }
33013 return putVarint64(p,v);
33014}
33015
33016/*
33017** Bitmasks used by sqlite3GetVarint(). These precomputed constants
33018** are defined here rather than simply putting the constant expressions
33019** inline in order to work around bugs in the RVT compiler.
33020**
33021** SLOT_2_0 A mask for (0x7f<<14) | 0x7f
33022**
33023** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0
33024*/
33025#define SLOT_2_0 0x001fc07f
33026#define SLOT_4_2_0 0xf01fc07f
33027
33028
33029/*
33030** Read a 64-bit variable-length integer from memory starting at p[0].
33031** Return the number of bytes read. The value is stored in *v.
33032*/
33033SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
33034 u32 a,b,s;
33035
33036 if( ((signed char*)p)[0]>=0 ){
33037 *v = *p;
33038 return 1;
33039 }
33040 if( ((signed char*)p)[1]>=0 ){
33041 *v = ((u32)(p[0]&0x7f)<<7) | p[1];
33042 return 2;
33043 }
33044
33045 /* Verify that constants are precomputed correctly */
33046 assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
33047 assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
33048
33049 a = ((u32)p[0])<<14;
33050 b = p[1];
33051 p += 2;
33052 a |= *p;
33053 /* a: p0<<14 | p2 (unmasked) */
33054 if (!(a&0x80))
33055 {
33056 a &= SLOT_2_0;
33057 b &= 0x7f;
33058 b = b<<7;
33059 a |= b;
33060 *v = a;
33061 return 3;
33062 }
33063
33064 /* CSE1 from below */
33065 a &= SLOT_2_0;
33066 p++;
33067 b = b<<14;
33068 b |= *p;
33069 /* b: p1<<14 | p3 (unmasked) */
33070 if (!(b&0x80))
33071 {
33072 b &= SLOT_2_0;
33073 /* moved CSE1 up */
33074 /* a &= (0x7f<<14)|(0x7f); */
33075 a = a<<7;
33076 a |= b;
33077 *v = a;
33078 return 4;
33079 }
33080
33081 /* a: p0<<14 | p2 (masked) */
33082 /* b: p1<<14 | p3 (unmasked) */
33083 /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
33084 /* moved CSE1 up */
33085 /* a &= (0x7f<<14)|(0x7f); */
33086 b &= SLOT_2_0;
33087 s = a;
33088 /* s: p0<<14 | p2 (masked) */
33089
33090 p++;
33091 a = a<<14;
33092 a |= *p;
33093 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
33094 if (!(a&0x80))
33095 {
33096 /* we can skip these cause they were (effectively) done above
33097 ** while calculating s */
33098 /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
33099 /* b &= (0x7f<<14)|(0x7f); */
33100 b = b<<7;
33101 a |= b;
33102 s = s>>18;
33103 *v = ((u64)s)<<32 | a;
33104 return 5;
33105 }
33106
33107 /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
33108 s = s<<7;
33109 s |= b;
33110 /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
33111
33112 p++;
33113 b = b<<14;
33114 b |= *p;
33115 /* b: p1<<28 | p3<<14 | p5 (unmasked) */
33116 if (!(b&0x80))
33117 {
33118 /* we can skip this cause it was (effectively) done above in calc'ing s */
33119 /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
33120 a &= SLOT_2_0;
33121 a = a<<7;
33122 a |= b;
33123 s = s>>18;
33124 *v = ((u64)s)<<32 | a;
33125 return 6;
33126 }
33127
33128 p++;
33129 a = a<<14;
33130 a |= *p;
33131 /* a: p2<<28 | p4<<14 | p6 (unmasked) */
33132 if (!(a&0x80))
33133 {
33134 a &= SLOT_4_2_0;
33135 b &= SLOT_2_0;
33136 b = b<<7;
33137 a |= b;
33138 s = s>>11;
33139 *v = ((u64)s)<<32 | a;
33140 return 7;
33141 }
33142
33143 /* CSE2 from below */
33144 a &= SLOT_2_0;
33145 p++;
33146 b = b<<14;
33147 b |= *p;
33148 /* b: p3<<28 | p5<<14 | p7 (unmasked) */
33149 if (!(b&0x80))
33150 {
33151 b &= SLOT_4_2_0;
33152 /* moved CSE2 up */
33153 /* a &= (0x7f<<14)|(0x7f); */
33154 a = a<<7;
33155 a |= b;
33156 s = s>>4;
33157 *v = ((u64)s)<<32 | a;
33158 return 8;
33159 }
33160
33161 p++;
33162 a = a<<15;
33163 a |= *p;
33164 /* a: p4<<29 | p6<<15 | p8 (unmasked) */
33165
33166 /* moved CSE2 up */
33167 /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
33168 b &= SLOT_2_0;
33169 b = b<<8;
33170 a |= b;
33171
33172 s = s<<4;
33173 b = p[-4];
33174 b &= 0x7f;
33175 b = b>>3;
33176 s |= b;
33177
33178 *v = ((u64)s)<<32 | a;
33179
33180 return 9;
33181}
33182
33183/*
33184** Read a 32-bit variable-length integer from memory starting at p[0].
33185** Return the number of bytes read. The value is stored in *v.
33186**
33187** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
33188** integer, then set *v to 0xffffffff.
33189**
33190** A MACRO version, getVarint32, is provided which inlines the
33191** single-byte case. All code should use the MACRO version as
33192** this function assumes the single-byte case has already been handled.
33193*/
33194SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
33195 u32 a,b;
33196
33197 /* The 1-byte case. Overwhelmingly the most common. Handled inline
33198 ** by the getVarin32() macro */
33199 a = *p;
33200 /* a: p0 (unmasked) */
33201#ifndef getVarint32
33202 if (!(a&0x80))
33203 {
33204 /* Values between 0 and 127 */
33205 *v = a;
33206 return 1;
33207 }
33208#endif
33209
33210 /* The 2-byte case */
33211 p++;
33212 b = *p;
33213 /* b: p1 (unmasked) */
33214 if (!(b&0x80))
33215 {
33216 /* Values between 128 and 16383 */
33217 a &= 0x7f;
33218 a = a<<7;
33219 *v = a | b;
33220 return 2;
33221 }
33222
33223 /* The 3-byte case */
33224 p++;
33225 a = a<<14;
33226 a |= *p;
33227 /* a: p0<<14 | p2 (unmasked) */
33228 if (!(a&0x80))
33229 {
33230 /* Values between 16384 and 2097151 */
33231 a &= (0x7f<<14)|(0x7f);
33232 b &= 0x7f;
33233 b = b<<7;
33234 *v = a | b;
33235 return 3;
33236 }
33237
33238 /* A 32-bit varint is used to store size information in btrees.
33239 ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
33240 ** A 3-byte varint is sufficient, for example, to record the size
33241 ** of a 1048569-byte BLOB or string.
33242 **
33243 ** We only unroll the first 1-, 2-, and 3- byte cases. The very
33244 ** rare larger cases can be handled by the slower 64-bit varint
33245 ** routine.
33246 */
33247#if 1
33248 {
33249 u64 v64;
33250 u8 n;
33251
33252 n = sqlite3GetVarint(p: p-2, v: &v64);
33253 assert( n>3 && n<=9 );
33254 if( (v64 & SQLITE_MAX_U32)!=v64 ){
33255 *v = 0xffffffff;
33256 }else{
33257 *v = (u32)v64;
33258 }
33259 return n;
33260 }
33261
33262#else
33263 /* For following code (kept for historical record only) shows an
33264 ** unrolling for the 3- and 4-byte varint cases. This code is
33265 ** slightly faster, but it is also larger and much harder to test.
33266 */
33267 p++;
33268 b = b<<14;
33269 b |= *p;
33270 /* b: p1<<14 | p3 (unmasked) */
33271 if (!(b&0x80))
33272 {
33273 /* Values between 2097152 and 268435455 */
33274 b &= (0x7f<<14)|(0x7f);
33275 a &= (0x7f<<14)|(0x7f);
33276 a = a<<7;
33277 *v = a | b;
33278 return 4;
33279 }
33280
33281 p++;
33282 a = a<<14;
33283 a |= *p;
33284 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
33285 if (!(a&0x80))
33286 {
33287 /* Values between 268435456 and 34359738367 */
33288 a &= SLOT_4_2_0;
33289 b &= SLOT_4_2_0;
33290 b = b<<7;
33291 *v = a | b;
33292 return 5;
33293 }
33294
33295 /* We can only reach this point when reading a corrupt database
33296 ** file. In that case we are not in any hurry. Use the (relatively
33297 ** slow) general-purpose sqlite3GetVarint() routine to extract the
33298 ** value. */
33299 {
33300 u64 v64;
33301 u8 n;
33302
33303 p -= 4;
33304 n = sqlite3GetVarint(p, &v64);
33305 assert( n>5 && n<=9 );
33306 *v = (u32)v64;
33307 return n;
33308 }
33309#endif
33310}
33311
33312/*
33313** Return the number of bytes that will be needed to store the given
33314** 64-bit integer.
33315*/
33316SQLITE_PRIVATE int sqlite3VarintLen(u64 v){
33317 int i;
33318 for(i=1; (v >>= 7)!=0; i++){ assert( i<10 ); }
33319 return i;
33320}
33321
33322
33323/*
33324** Read or write a four-byte big-endian integer value.
33325*/
33326SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
33327#if SQLITE_BYTEORDER==4321
33328 u32 x;
33329 memcpy(&x,p,4);
33330 return x;
33331#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
33332 u32 x;
33333 memcpy(&x,p,4);
33334 return __builtin_bswap32(x);
33335#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
33336 u32 x;
33337 memcpy(&x,p,4);
33338 return _byteswap_ulong(x);
33339#else
33340 testcase( p[0]&0x80 );
33341 return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
33342#endif
33343}
33344SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
33345#if SQLITE_BYTEORDER==4321
33346 memcpy(p,&v,4);
33347#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
33348 u32 x = __builtin_bswap32(v);
33349 memcpy(p,&x,4);
33350#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
33351 u32 x = _byteswap_ulong(v);
33352 memcpy(p,&x,4);
33353#else
33354 p[0] = (u8)(v>>24);
33355 p[1] = (u8)(v>>16);
33356 p[2] = (u8)(v>>8);
33357 p[3] = (u8)v;
33358#endif
33359}
33360
33361
33362
33363/*
33364** Translate a single byte of Hex into an integer.
33365** This routine only works if h really is a valid hexadecimal
33366** character: 0..9a..fA..F
33367*/
33368SQLITE_PRIVATE u8 sqlite3HexToInt(int h){
33369 assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') );
33370#ifdef SQLITE_ASCII
33371 h += 9*(1&(h>>6));
33372#endif
33373#ifdef SQLITE_EBCDIC
33374 h += 9*(1&~(h>>4));
33375#endif
33376 return (u8)(h & 0xf);
33377}
33378
33379#if !defined(SQLITE_OMIT_BLOB_LITERAL)
33380/*
33381** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
33382** value. Return a pointer to its binary value. Space to hold the
33383** binary value has been obtained from malloc and must be freed by
33384** the calling routine.
33385*/
33386SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
33387 char *zBlob;
33388 int i;
33389
33390 zBlob = (char *)sqlite3DbMallocRawNN(db, n: n/2 + 1);
33391 n--;
33392 if( zBlob ){
33393 for(i=0; i<n; i+=2){
33394 zBlob[i/2] = (sqlite3HexToInt(h: z[i])<<4) | sqlite3HexToInt(h: z[i+1]);
33395 }
33396 zBlob[i/2] = 0;
33397 }
33398 return zBlob;
33399}
33400#endif /* !SQLITE_OMIT_BLOB_LITERAL */
33401
33402/*
33403** Log an error that is an API call on a connection pointer that should
33404** not have been used. The "type" of connection pointer is given as the
33405** argument. The zType is a word like "NULL" or "closed" or "invalid".
33406*/
33407static void logBadConnection(const char *zType){
33408 sqlite3_log(SQLITE_MISUSE,
33409 zFormat: "API call with %s database connection pointer",
33410 zType
33411 );
33412}
33413
33414/*
33415** Check to make sure we have a valid db pointer. This test is not
33416** foolproof but it does provide some measure of protection against
33417** misuse of the interface such as passing in db pointers that are
33418** NULL or which have been previously closed. If this routine returns
33419** 1 it means that the db pointer is valid and 0 if it should not be
33420** dereferenced for any reason. The calling function should invoke
33421** SQLITE_MISUSE immediately.
33422**
33423** sqlite3SafetyCheckOk() requires that the db pointer be valid for
33424** use. sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
33425** open properly and is not fit for general use but which can be
33426** used as an argument to sqlite3_errmsg() or sqlite3_close().
33427*/
33428SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3 *db){
33429 u8 eOpenState;
33430 if( db==0 ){
33431 logBadConnection(zType: "NULL");
33432 return 0;
33433 }
33434 eOpenState = db->eOpenState;
33435 if( eOpenState!=SQLITE_STATE_OPEN ){
33436 if( sqlite3SafetyCheckSickOrOk(db) ){
33437 testcase( sqlite3GlobalConfig.xLog!=0 );
33438 logBadConnection(zType: "unopened");
33439 }
33440 return 0;
33441 }else{
33442 return 1;
33443 }
33444}
33445SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
33446 u8 eOpenState;
33447 eOpenState = db->eOpenState;
33448 if( eOpenState!=SQLITE_STATE_SICK &&
33449 eOpenState!=SQLITE_STATE_OPEN &&
33450 eOpenState!=SQLITE_STATE_BUSY ){
33451 testcase( sqlite3GlobalConfig.xLog!=0 );
33452 logBadConnection(zType: "invalid");
33453 return 0;
33454 }else{
33455 return 1;
33456 }
33457}
33458
33459/*
33460** Attempt to add, substract, or multiply the 64-bit signed value iB against
33461** the other 64-bit signed integer at *pA and store the result in *pA.
33462** Return 0 on success. Or if the operation would have resulted in an
33463** overflow, leave *pA unchanged and return 1.
33464*/
33465SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){
33466#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
33467 return __builtin_add_overflow(*pA, iB, pA);
33468#else
33469 i64 iA = *pA;
33470 testcase( iA==0 ); testcase( iA==1 );
33471 testcase( iB==-1 ); testcase( iB==0 );
33472 if( iB>=0 ){
33473 testcase( iA>0 && LARGEST_INT64 - iA == iB );
33474 testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
33475 if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
33476 }else{
33477 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
33478 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
33479 if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
33480 }
33481 *pA += iB;
33482 return 0;
33483#endif
33484}
33485SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
33486#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
33487 return __builtin_sub_overflow(*pA, iB, pA);
33488#else
33489 testcase( iB==SMALLEST_INT64+1 );
33490 if( iB==SMALLEST_INT64 ){
33491 testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
33492 if( (*pA)>=0 ) return 1;
33493 *pA -= iB;
33494 return 0;
33495 }else{
33496 return sqlite3AddInt64(pA, iB: -iB);
33497 }
33498#endif
33499}
33500SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){
33501#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
33502 return __builtin_mul_overflow(*pA, iB, pA);
33503#else
33504 i64 iA = *pA;
33505 if( iB>0 ){
33506 if( iA>LARGEST_INT64/iB ) return 1;
33507 if( iA<SMALLEST_INT64/iB ) return 1;
33508 }else if( iB<0 ){
33509 if( iA>0 ){
33510 if( iB<SMALLEST_INT64/iA ) return 1;
33511 }else if( iA<0 ){
33512 if( iB==SMALLEST_INT64 ) return 1;
33513 if( iA==SMALLEST_INT64 ) return 1;
33514 if( -iA>LARGEST_INT64/-iB ) return 1;
33515 }
33516 }
33517 *pA = iA*iB;
33518 return 0;
33519#endif
33520}
33521
33522/*
33523** Compute the absolute value of a 32-bit signed integer, of possible. Or
33524** if the integer has a value of -2147483648, return +2147483647
33525*/
33526SQLITE_PRIVATE int sqlite3AbsInt32(int x){
33527 if( x>=0 ) return x;
33528 if( x==(int)0x80000000 ) return 0x7fffffff;
33529 return -x;
33530}
33531
33532#ifdef SQLITE_ENABLE_8_3_NAMES
33533/*
33534** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
33535** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
33536** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
33537** three characters, then shorten the suffix on z[] to be the last three
33538** characters of the original suffix.
33539**
33540** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
33541** do the suffix shortening regardless of URI parameter.
33542**
33543** Examples:
33544**
33545** test.db-journal => test.nal
33546** test.db-wal => test.wal
33547** test.db-shm => test.shm
33548** test.db-mj7f3319fa => test.9fa
33549*/
33550SQLITE_PRIVATE void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
33551#if SQLITE_ENABLE_8_3_NAMES<2
33552 if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) )
33553#endif
33554 {
33555 int i, sz;
33556 sz = sqlite3Strlen30(z);
33557 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
33558 if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
33559 }
33560}
33561#endif
33562
33563/*
33564** Find (an approximate) sum of two LogEst values. This computation is
33565** not a simple "+" operator because LogEst is stored as a logarithmic
33566** value.
33567**
33568*/
33569SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst a, LogEst b){
33570 static const unsigned char x[] = {
33571 10, 10, /* 0,1 */
33572 9, 9, /* 2,3 */
33573 8, 8, /* 4,5 */
33574 7, 7, 7, /* 6,7,8 */
33575 6, 6, 6, /* 9,10,11 */
33576 5, 5, 5, /* 12-14 */
33577 4, 4, 4, 4, /* 15-18 */
33578 3, 3, 3, 3, 3, 3, /* 19-24 */
33579 2, 2, 2, 2, 2, 2, 2, /* 25-31 */
33580 };
33581 if( a>=b ){
33582 if( a>b+49 ) return a;
33583 if( a>b+31 ) return a+1;
33584 return a+x[a-b];
33585 }else{
33586 if( b>a+49 ) return b;
33587 if( b>a+31 ) return b+1;
33588 return b+x[b-a];
33589 }
33590}
33591
33592/*
33593** Convert an integer into a LogEst. In other words, compute an
33594** approximation for 10*log2(x).
33595*/
33596SQLITE_PRIVATE LogEst sqlite3LogEst(u64 x){
33597 static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
33598 LogEst y = 40;
33599 if( x<8 ){
33600 if( x<2 ) return 0;
33601 while( x<8 ){ y -= 10; x <<= 1; }
33602 }else{
33603#if GCC_VERSION>=5004000
33604 int i = 60 - __builtin_clzll(x);
33605 y += i*10;
33606 x >>= i;
33607#else
33608 while( x>255 ){ y += 40; x >>= 4; } /*OPTIMIZATION-IF-TRUE*/
33609 while( x>15 ){ y += 10; x >>= 1; }
33610#endif
33611 }
33612 return a[x&7] + y - 10;
33613}
33614
33615#ifndef SQLITE_OMIT_VIRTUALTABLE
33616/*
33617** Convert a double into a LogEst
33618** In other words, compute an approximation for 10*log2(x).
33619*/
33620SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double x){
33621 u64 a;
33622 LogEst e;
33623 assert( sizeof(x)==8 && sizeof(a)==8 );
33624 if( x<=1 ) return 0;
33625 if( x<=2000000000 ) return sqlite3LogEst(x: (u64)x);
33626 memcpy(dest: &a, src: &x, n: 8);
33627 e = (a>>52) - 1022;
33628 return e*10;
33629}
33630#endif /* SQLITE_OMIT_VIRTUALTABLE */
33631
33632#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
33633 defined(SQLITE_ENABLE_STAT4) || \
33634 defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
33635/*
33636** Convert a LogEst into an integer.
33637**
33638** Note that this routine is only used when one or more of various
33639** non-standard compile-time options is enabled.
33640*/
33641SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst x){
33642 u64 n;
33643 n = x%10;
33644 x /= 10;
33645 if( n>=5 ) n -= 2;
33646 else if( n>=1 ) n -= 1;
33647#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
33648 defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
33649 if( x>60 ) return (u64)LARGEST_INT64;
33650#else
33651 /* If only SQLITE_ENABLE_STAT4 is on, then the largest input
33652 ** possible to this routine is 310, resulting in a maximum x of 31 */
33653 assert( x<=60 );
33654#endif
33655 return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x);
33656}
33657#endif /* defined SCANSTAT or STAT4 or ESTIMATED_ROWS */
33658
33659/*
33660** Add a new name/number pair to a VList. This might require that the
33661** VList object be reallocated, so return the new VList. If an OOM
33662** error occurs, the original VList returned and the
33663** db->mallocFailed flag is set.
33664**
33665** A VList is really just an array of integers. To destroy a VList,
33666** simply pass it to sqlite3DbFree().
33667**
33668** The first integer is the number of integers allocated for the whole
33669** VList. The second integer is the number of integers actually used.
33670** Each name/number pair is encoded by subsequent groups of 3 or more
33671** integers.
33672**
33673** Each name/number pair starts with two integers which are the numeric
33674** value for the pair and the size of the name/number pair, respectively.
33675** The text name overlays one or more following integers. The text name
33676** is always zero-terminated.
33677**
33678** Conceptually:
33679**
33680** struct VList {
33681** int nAlloc; // Number of allocated slots
33682** int nUsed; // Number of used slots
33683** struct VListEntry {
33684** int iValue; // Value for this entry
33685** int nSlot; // Slots used by this entry
33686** // ... variable name goes here
33687** } a[0];
33688** }
33689**
33690** During code generation, pointers to the variable names within the
33691** VList are taken. When that happens, nAlloc is set to zero as an
33692** indication that the VList may never again be enlarged, since the
33693** accompanying realloc() would invalidate the pointers.
33694*/
33695SQLITE_PRIVATE VList *sqlite3VListAdd(
33696 sqlite3 *db, /* The database connection used for malloc() */
33697 VList *pIn, /* The input VList. Might be NULL */
33698 const char *zName, /* Name of symbol to add */
33699 int nName, /* Bytes of text in zName */
33700 int iVal /* Value to associate with zName */
33701){
33702 int nInt; /* number of sizeof(int) objects needed for zName */
33703 char *z; /* Pointer to where zName will be stored */
33704 int i; /* Index in pIn[] where zName is stored */
33705
33706 nInt = nName/4 + 3;
33707 assert( pIn==0 || pIn[0]>=3 ); /* Verify ok to add new elements */
33708 if( pIn==0 || pIn[1]+nInt > pIn[0] ){
33709 /* Enlarge the allocation */
33710 sqlite3_int64 nAlloc = (pIn ? 2*(sqlite3_int64)pIn[0] : 10) + nInt;
33711 VList *pOut = sqlite3DbRealloc(db, p: pIn, n: nAlloc*sizeof(int));
33712 if( pOut==0 ) return pIn;
33713 if( pIn==0 ) pOut[1] = 2;
33714 pIn = pOut;
33715 pIn[0] = nAlloc;
33716 }
33717 i = pIn[1];
33718 pIn[i] = iVal;
33719 pIn[i+1] = nInt;
33720 z = (char*)&pIn[i+2];
33721 pIn[1] = i+nInt;
33722 assert( pIn[1]<=pIn[0] );
33723 memcpy(dest: z, src: zName, n: nName);
33724 z[nName] = 0;
33725 return pIn;
33726}
33727
33728/*
33729** Return a pointer to the name of a variable in the given VList that
33730** has the value iVal. Or return a NULL if there is no such variable in
33731** the list
33732*/
33733SQLITE_PRIVATE const char *sqlite3VListNumToName(VList *pIn, int iVal){
33734 int i, mx;
33735 if( pIn==0 ) return 0;
33736 mx = pIn[1];
33737 i = 2;
33738 do{
33739 if( pIn[i]==iVal ) return (char*)&pIn[i+2];
33740 i += pIn[i+1];
33741 }while( i<mx );
33742 return 0;
33743}
33744
33745/*
33746** Return the number of the variable named zName, if it is in VList.
33747** or return 0 if there is no such variable.
33748*/
33749SQLITE_PRIVATE int sqlite3VListNameToNum(VList *pIn, const char *zName, int nName){
33750 int i, mx;
33751 if( pIn==0 ) return 0;
33752 mx = pIn[1];
33753 i = 2;
33754 do{
33755 const char *z = (const char*)&pIn[i+2];
33756 if( strncmp(s1: z,s2: zName,n: nName)==0 && z[nName]==0 ) return pIn[i];
33757 i += pIn[i+1];
33758 }while( i<mx );
33759 return 0;
33760}
33761
33762/************** End of util.c ************************************************/
33763/************** Begin file hash.c ********************************************/
33764/*
33765** 2001 September 22
33766**
33767** The author disclaims copyright to this source code. In place of
33768** a legal notice, here is a blessing:
33769**
33770** May you do good and not evil.
33771** May you find forgiveness for yourself and forgive others.
33772** May you share freely, never taking more than you give.
33773**
33774*************************************************************************
33775** This is the implementation of generic hash-tables
33776** used in SQLite.
33777*/
33778/* #include "sqliteInt.h" */
33779/* #include <assert.h> */
33780
33781/* Turn bulk memory into a hash table object by initializing the
33782** fields of the Hash structure.
33783**
33784** "pNew" is a pointer to the hash table that is to be initialized.
33785*/
33786SQLITE_PRIVATE void sqlite3HashInit(Hash *pNew){
33787 assert( pNew!=0 );
33788 pNew->first = 0;
33789 pNew->count = 0;
33790 pNew->htsize = 0;
33791 pNew->ht = 0;
33792}
33793
33794/* Remove all entries from a hash table. Reclaim all memory.
33795** Call this routine to delete a hash table or to reset a hash table
33796** to the empty state.
33797*/
33798SQLITE_PRIVATE void sqlite3HashClear(Hash *pH){
33799 HashElem *elem; /* For looping over all elements of the table */
33800
33801 assert( pH!=0 );
33802 elem = pH->first;
33803 pH->first = 0;
33804 sqlite3_free(p: pH->ht);
33805 pH->ht = 0;
33806 pH->htsize = 0;
33807 while( elem ){
33808 HashElem *next_elem = elem->next;
33809 sqlite3_free(p: elem);
33810 elem = next_elem;
33811 }
33812 pH->count = 0;
33813}
33814
33815/*
33816** The hashing function.
33817*/
33818static unsigned int strHash(const char *z){
33819 unsigned int h = 0;
33820 unsigned char c;
33821 while( (c = (unsigned char)*z++)!=0 ){ /*OPTIMIZATION-IF-TRUE*/
33822 /* Knuth multiplicative hashing. (Sorting & Searching, p. 510).
33823 ** 0x9e3779b1 is 2654435761 which is the closest prime number to
33824 ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */
33825 h += sqlite3UpperToLower[c];
33826 h *= 0x9e3779b1;
33827 }
33828 return h;
33829}
33830
33831
33832/* Link pNew element into the hash table pH. If pEntry!=0 then also
33833** insert pNew into the pEntry hash bucket.
33834*/
33835static void insertElement(
33836 Hash *pH, /* The complete hash table */
33837 struct _ht *pEntry, /* The entry into which pNew is inserted */
33838 HashElem *pNew /* The element to be inserted */
33839){
33840 HashElem *pHead; /* First element already in pEntry */
33841 if( pEntry ){
33842 pHead = pEntry->count ? pEntry->chain : 0;
33843 pEntry->count++;
33844 pEntry->chain = pNew;
33845 }else{
33846 pHead = 0;
33847 }
33848 if( pHead ){
33849 pNew->next = pHead;
33850 pNew->prev = pHead->prev;
33851 if( pHead->prev ){ pHead->prev->next = pNew; }
33852 else { pH->first = pNew; }
33853 pHead->prev = pNew;
33854 }else{
33855 pNew->next = pH->first;
33856 if( pH->first ){ pH->first->prev = pNew; }
33857 pNew->prev = 0;
33858 pH->first = pNew;
33859 }
33860}
33861
33862
33863/* Resize the hash table so that it cantains "new_size" buckets.
33864**
33865** The hash table might fail to resize if sqlite3_malloc() fails or
33866** if the new size is the same as the prior size.
33867** Return TRUE if the resize occurs and false if not.
33868*/
33869static int rehash(Hash *pH, unsigned int new_size){
33870 struct _ht *new_ht; /* The new hash table */
33871 HashElem *elem, *next_elem; /* For looping over existing elements */
33872
33873#if SQLITE_MALLOC_SOFT_LIMIT>0
33874 if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){
33875 new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht);
33876 }
33877 if( new_size==pH->htsize ) return 0;
33878#endif
33879
33880 /* The inability to allocates space for a larger hash table is
33881 ** a performance hit but it is not a fatal error. So mark the
33882 ** allocation as a benign. Use sqlite3Malloc()/memset(0) instead of
33883 ** sqlite3MallocZero() to make the allocation, as sqlite3MallocZero()
33884 ** only zeroes the requested number of bytes whereas this module will
33885 ** use the actual amount of space allocated for the hash table (which
33886 ** may be larger than the requested amount).
33887 */
33888 sqlite3BeginBenignMalloc();
33889 new_ht = (struct _ht *)sqlite3Malloc( n: new_size*sizeof(struct _ht) );
33890 sqlite3EndBenignMalloc();
33891
33892 if( new_ht==0 ) return 0;
33893 sqlite3_free(p: pH->ht);
33894 pH->ht = new_ht;
33895 pH->htsize = new_size = sqlite3MallocSize(p: new_ht)/sizeof(struct _ht);
33896 memset(s: new_ht, c: 0, n: new_size*sizeof(struct _ht));
33897 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
33898 unsigned int h = strHash(z: elem->pKey) % new_size;
33899 next_elem = elem->next;
33900 insertElement(pH, pEntry: &new_ht[h], pNew: elem);
33901 }
33902 return 1;
33903}
33904
33905/* This function (for internal use only) locates an element in an
33906** hash table that matches the given key. If no element is found,
33907** a pointer to a static null element with HashElem.data==0 is returned.
33908** If pH is not NULL, then the hash for this key is written to *pH.
33909*/
33910static HashElem *findElementWithHash(
33911 const Hash *pH, /* The pH to be searched */
33912 const char *pKey, /* The key we are searching for */
33913 unsigned int *pHash /* Write the hash value here */
33914){
33915 HashElem *elem; /* Used to loop thru the element list */
33916 unsigned int count; /* Number of elements left to test */
33917 unsigned int h; /* The computed hash */
33918 static HashElem nullElement = { 0, 0, 0, 0 };
33919
33920 if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/
33921 struct _ht *pEntry;
33922 h = strHash(z: pKey) % pH->htsize;
33923 pEntry = &pH->ht[h];
33924 elem = pEntry->chain;
33925 count = pEntry->count;
33926 }else{
33927 h = 0;
33928 elem = pH->first;
33929 count = pH->count;
33930 }
33931 if( pHash ) *pHash = h;
33932 while( count-- ){
33933 assert( elem!=0 );
33934 if( sqlite3StrICmp(zLeft: elem->pKey,zRight: pKey)==0 ){
33935 return elem;
33936 }
33937 elem = elem->next;
33938 }
33939 return &nullElement;
33940}
33941
33942/* Remove a single entry from the hash table given a pointer to that
33943** element and a hash on the element's key.
33944*/
33945static void removeElementGivenHash(
33946 Hash *pH, /* The pH containing "elem" */
33947 HashElem* elem, /* The element to be removed from the pH */
33948 unsigned int h /* Hash value for the element */
33949){
33950 struct _ht *pEntry;
33951 if( elem->prev ){
33952 elem->prev->next = elem->next;
33953 }else{
33954 pH->first = elem->next;
33955 }
33956 if( elem->next ){
33957 elem->next->prev = elem->prev;
33958 }
33959 if( pH->ht ){
33960 pEntry = &pH->ht[h];
33961 if( pEntry->chain==elem ){
33962 pEntry->chain = elem->next;
33963 }
33964 assert( pEntry->count>0 );
33965 pEntry->count--;
33966 }
33967 sqlite3_free( p: elem );
33968 pH->count--;
33969 if( pH->count==0 ){
33970 assert( pH->first==0 );
33971 assert( pH->count==0 );
33972 sqlite3HashClear(pH);
33973 }
33974}
33975
33976/* Attempt to locate an element of the hash table pH with a key
33977** that matches pKey. Return the data for this element if it is
33978** found, or NULL if there is no match.
33979*/
33980SQLITE_PRIVATE void *sqlite3HashFind(const Hash *pH, const char *pKey){
33981 assert( pH!=0 );
33982 assert( pKey!=0 );
33983 return findElementWithHash(pH, pKey, pHash: 0)->data;
33984}
33985
33986/* Insert an element into the hash table pH. The key is pKey
33987** and the data is "data".
33988**
33989** If no element exists with a matching key, then a new
33990** element is created and NULL is returned.
33991**
33992** If another element already exists with the same key, then the
33993** new data replaces the old data and the old data is returned.
33994** The key is not copied in this instance. If a malloc fails, then
33995** the new data is returned and the hash table is unchanged.
33996**
33997** If the "data" parameter to this function is NULL, then the
33998** element corresponding to "key" is removed from the hash table.
33999*/
34000SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const char *pKey, void *data){
34001 unsigned int h; /* the hash of the key modulo hash table size */
34002 HashElem *elem; /* Used to loop thru the element list */
34003 HashElem *new_elem; /* New element added to the pH */
34004
34005 assert( pH!=0 );
34006 assert( pKey!=0 );
34007 elem = findElementWithHash(pH,pKey,pHash: &h);
34008 if( elem->data ){
34009 void *old_data = elem->data;
34010 if( data==0 ){
34011 removeElementGivenHash(pH,elem,h);
34012 }else{
34013 elem->data = data;
34014 elem->pKey = pKey;
34015 }
34016 return old_data;
34017 }
34018 if( data==0 ) return 0;
34019 new_elem = (HashElem*)sqlite3Malloc( n: sizeof(HashElem) );
34020 if( new_elem==0 ) return data;
34021 new_elem->pKey = pKey;
34022 new_elem->data = data;
34023 pH->count++;
34024 if( pH->count>=10 && pH->count > 2*pH->htsize ){
34025 if( rehash(pH, new_size: pH->count*2) ){
34026 assert( pH->htsize>0 );
34027 h = strHash(z: pKey) % pH->htsize;
34028 }
34029 }
34030 insertElement(pH, pEntry: pH->ht ? &pH->ht[h] : 0, pNew: new_elem);
34031 return 0;
34032}
34033
34034/************** End of hash.c ************************************************/
34035/************** Begin file opcodes.c *****************************************/
34036/* Automatically generated. Do not edit */
34037/* See the tool/mkopcodec.tcl script for details. */
34038#if !defined(SQLITE_OMIT_EXPLAIN) \
34039 || defined(VDBE_PROFILE) \
34040 || defined(SQLITE_DEBUG)
34041#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) || defined(SQLITE_DEBUG)
34042# define OpHelp(X) "\0" X
34043#else
34044# define OpHelp(X)
34045#endif
34046SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
34047 static const char *const azName[] = {
34048 /* 0 */ "Savepoint" OpHelp(""),
34049 /* 1 */ "AutoCommit" OpHelp(""),
34050 /* 2 */ "Transaction" OpHelp(""),
34051 /* 3 */ "SorterNext" OpHelp(""),
34052 /* 4 */ "Prev" OpHelp(""),
34053 /* 5 */ "Next" OpHelp(""),
34054 /* 6 */ "Checkpoint" OpHelp(""),
34055 /* 7 */ "JournalMode" OpHelp(""),
34056 /* 8 */ "Vacuum" OpHelp(""),
34057 /* 9 */ "VFilter" OpHelp("iplan=r[P3] zplan='P4'"),
34058 /* 10 */ "VUpdate" OpHelp("data=r[P3@P2]"),
34059 /* 11 */ "Goto" OpHelp(""),
34060 /* 12 */ "Gosub" OpHelp(""),
34061 /* 13 */ "InitCoroutine" OpHelp(""),
34062 /* 14 */ "Yield" OpHelp(""),
34063 /* 15 */ "MustBeInt" OpHelp(""),
34064 /* 16 */ "Jump" OpHelp(""),
34065 /* 17 */ "Once" OpHelp(""),
34066 /* 18 */ "If" OpHelp(""),
34067 /* 19 */ "Not" OpHelp("r[P2]= !r[P1]"),
34068 /* 20 */ "IfNot" OpHelp(""),
34069 /* 21 */ "IsNullOrType" OpHelp("if typeof(r[P1]) IN (P3,5) goto P2"),
34070 /* 22 */ "IfNullRow" OpHelp("if P1.nullRow then r[P3]=NULL, goto P2"),
34071 /* 23 */ "SeekLT" OpHelp("key=r[P3@P4]"),
34072 /* 24 */ "SeekLE" OpHelp("key=r[P3@P4]"),
34073 /* 25 */ "SeekGE" OpHelp("key=r[P3@P4]"),
34074 /* 26 */ "SeekGT" OpHelp("key=r[P3@P4]"),
34075 /* 27 */ "IfNotOpen" OpHelp("if( !csr[P1] ) goto P2"),
34076 /* 28 */ "IfNoHope" OpHelp("key=r[P3@P4]"),
34077 /* 29 */ "NoConflict" OpHelp("key=r[P3@P4]"),
34078 /* 30 */ "NotFound" OpHelp("key=r[P3@P4]"),
34079 /* 31 */ "Found" OpHelp("key=r[P3@P4]"),
34080 /* 32 */ "SeekRowid" OpHelp("intkey=r[P3]"),
34081 /* 33 */ "NotExists" OpHelp("intkey=r[P3]"),
34082 /* 34 */ "Last" OpHelp(""),
34083 /* 35 */ "IfSmaller" OpHelp(""),
34084 /* 36 */ "SorterSort" OpHelp(""),
34085 /* 37 */ "Sort" OpHelp(""),
34086 /* 38 */ "Rewind" OpHelp(""),
34087 /* 39 */ "IdxLE" OpHelp("key=r[P3@P4]"),
34088 /* 40 */ "IdxGT" OpHelp("key=r[P3@P4]"),
34089 /* 41 */ "IdxLT" OpHelp("key=r[P3@P4]"),
34090 /* 42 */ "IdxGE" OpHelp("key=r[P3@P4]"),
34091 /* 43 */ "Or" OpHelp("r[P3]=(r[P1] || r[P2])"),
34092 /* 44 */ "And" OpHelp("r[P3]=(r[P1] && r[P2])"),
34093 /* 45 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"),
34094 /* 46 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"),
34095 /* 47 */ "Program" OpHelp(""),
34096 /* 48 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"),
34097 /* 49 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
34098 /* 50 */ "IsNull" OpHelp("if r[P1]==NULL goto P2"),
34099 /* 51 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"),
34100 /* 52 */ "Ne" OpHelp("IF r[P3]!=r[P1]"),
34101 /* 53 */ "Eq" OpHelp("IF r[P3]==r[P1]"),
34102 /* 54 */ "Gt" OpHelp("IF r[P3]>r[P1]"),
34103 /* 55 */ "Le" OpHelp("IF r[P3]<=r[P1]"),
34104 /* 56 */ "Lt" OpHelp("IF r[P3]<r[P1]"),
34105 /* 57 */ "Ge" OpHelp("IF r[P3]>=r[P1]"),
34106 /* 58 */ "ElseEq" OpHelp(""),
34107 /* 59 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]--, goto P2"),
34108 /* 60 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
34109 /* 61 */ "IncrVacuum" OpHelp(""),
34110 /* 62 */ "VNext" OpHelp(""),
34111 /* 63 */ "Init" OpHelp("Start at P2"),
34112 /* 64 */ "PureFunc" OpHelp("r[P3]=func(r[P2@NP])"),
34113 /* 65 */ "Function" OpHelp("r[P3]=func(r[P2@NP])"),
34114 /* 66 */ "Return" OpHelp(""),
34115 /* 67 */ "EndCoroutine" OpHelp(""),
34116 /* 68 */ "HaltIfNull" OpHelp("if r[P3]=null halt"),
34117 /* 69 */ "Halt" OpHelp(""),
34118 /* 70 */ "Integer" OpHelp("r[P2]=P1"),
34119 /* 71 */ "Int64" OpHelp("r[P2]=P4"),
34120 /* 72 */ "String" OpHelp("r[P2]='P4' (len=P1)"),
34121 /* 73 */ "Null" OpHelp("r[P2..P3]=NULL"),
34122 /* 74 */ "SoftNull" OpHelp("r[P1]=NULL"),
34123 /* 75 */ "Blob" OpHelp("r[P2]=P4 (len=P1)"),
34124 /* 76 */ "Variable" OpHelp("r[P2]=parameter(P1,P4)"),
34125 /* 77 */ "Move" OpHelp("r[P2@P3]=r[P1@P3]"),
34126 /* 78 */ "Copy" OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
34127 /* 79 */ "SCopy" OpHelp("r[P2]=r[P1]"),
34128 /* 80 */ "IntCopy" OpHelp("r[P2]=r[P1]"),
34129 /* 81 */ "ChngCntRow" OpHelp("output=r[P1]"),
34130 /* 82 */ "ResultRow" OpHelp("output=r[P1@P2]"),
34131 /* 83 */ "CollSeq" OpHelp(""),
34132 /* 84 */ "AddImm" OpHelp("r[P1]=r[P1]+P2"),
34133 /* 85 */ "RealAffinity" OpHelp(""),
34134 /* 86 */ "Cast" OpHelp("affinity(r[P1])"),
34135 /* 87 */ "Permutation" OpHelp(""),
34136 /* 88 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"),
34137 /* 89 */ "IsTrue" OpHelp("r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4"),
34138 /* 90 */ "ZeroOrNull" OpHelp("r[P2] = 0 OR NULL"),
34139 /* 91 */ "Offset" OpHelp("r[P3] = sqlite_offset(P1)"),
34140 /* 92 */ "Column" OpHelp("r[P3]=PX"),
34141 /* 93 */ "TypeCheck" OpHelp("typecheck(r[P1@P2])"),
34142 /* 94 */ "Affinity" OpHelp("affinity(r[P1@P2])"),
34143 /* 95 */ "MakeRecord" OpHelp("r[P3]=mkrec(r[P1@P2])"),
34144 /* 96 */ "Count" OpHelp("r[P2]=count()"),
34145 /* 97 */ "ReadCookie" OpHelp(""),
34146 /* 98 */ "SetCookie" OpHelp(""),
34147 /* 99 */ "ReopenIdx" OpHelp("root=P2 iDb=P3"),
34148 /* 100 */ "OpenRead" OpHelp("root=P2 iDb=P3"),
34149 /* 101 */ "OpenWrite" OpHelp("root=P2 iDb=P3"),
34150 /* 102 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"),
34151 /* 103 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"),
34152 /* 104 */ "ShiftLeft" OpHelp("r[P3]=r[P2]<<r[P1]"),
34153 /* 105 */ "ShiftRight" OpHelp("r[P3]=r[P2]>>r[P1]"),
34154 /* 106 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"),
34155 /* 107 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
34156 /* 108 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"),
34157 /* 109 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"),
34158 /* 110 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"),
34159 /* 111 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"),
34160 /* 112 */ "OpenDup" OpHelp(""),
34161 /* 113 */ "BitNot" OpHelp("r[P2]= ~r[P1]"),
34162 /* 114 */ "OpenAutoindex" OpHelp("nColumn=P2"),
34163 /* 115 */ "OpenEphemeral" OpHelp("nColumn=P2"),
34164 /* 116 */ "String8" OpHelp("r[P2]='P4'"),
34165 /* 117 */ "SorterOpen" OpHelp(""),
34166 /* 118 */ "SequenceTest" OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
34167 /* 119 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"),
34168 /* 120 */ "Close" OpHelp(""),
34169 /* 121 */ "ColumnsUsed" OpHelp(""),
34170 /* 122 */ "SeekScan" OpHelp("Scan-ahead up to P1 rows"),
34171 /* 123 */ "SeekHit" OpHelp("set P2<=seekHit<=P3"),
34172 /* 124 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"),
34173 /* 125 */ "NewRowid" OpHelp("r[P2]=rowid"),
34174 /* 126 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"),
34175 /* 127 */ "RowCell" OpHelp(""),
34176 /* 128 */ "Delete" OpHelp(""),
34177 /* 129 */ "ResetCount" OpHelp(""),
34178 /* 130 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
34179 /* 131 */ "SorterData" OpHelp("r[P2]=data"),
34180 /* 132 */ "RowData" OpHelp("r[P2]=data"),
34181 /* 133 */ "Rowid" OpHelp("r[P2]=rowid"),
34182 /* 134 */ "NullRow" OpHelp(""),
34183 /* 135 */ "SeekEnd" OpHelp(""),
34184 /* 136 */ "IdxInsert" OpHelp("key=r[P2]"),
34185 /* 137 */ "SorterInsert" OpHelp("key=r[P2]"),
34186 /* 138 */ "IdxDelete" OpHelp("key=r[P2@P3]"),
34187 /* 139 */ "DeferredSeek" OpHelp("Move P3 to P1.rowid if needed"),
34188 /* 140 */ "IdxRowid" OpHelp("r[P2]=rowid"),
34189 /* 141 */ "FinishSeek" OpHelp(""),
34190 /* 142 */ "Destroy" OpHelp(""),
34191 /* 143 */ "Clear" OpHelp(""),
34192 /* 144 */ "ResetSorter" OpHelp(""),
34193 /* 145 */ "CreateBtree" OpHelp("r[P2]=root iDb=P1 flags=P3"),
34194 /* 146 */ "SqlExec" OpHelp(""),
34195 /* 147 */ "ParseSchema" OpHelp(""),
34196 /* 148 */ "LoadAnalysis" OpHelp(""),
34197 /* 149 */ "DropTable" OpHelp(""),
34198 /* 150 */ "DropIndex" OpHelp(""),
34199 /* 151 */ "DropTrigger" OpHelp(""),
34200 /* 152 */ "Real" OpHelp("r[P2]=P4"),
34201 /* 153 */ "IntegrityCk" OpHelp(""),
34202 /* 154 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"),
34203 /* 155 */ "Param" OpHelp(""),
34204 /* 156 */ "FkCounter" OpHelp("fkctr[P1]+=P2"),
34205 /* 157 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"),
34206 /* 158 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
34207 /* 159 */ "AggInverse" OpHelp("accum=r[P3] inverse(r[P2@P5])"),
34208 /* 160 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"),
34209 /* 161 */ "AggStep1" OpHelp("accum=r[P3] step(r[P2@P5])"),
34210 /* 162 */ "AggValue" OpHelp("r[P3]=value N=P2"),
34211 /* 163 */ "AggFinal" OpHelp("accum=r[P1] N=P2"),
34212 /* 164 */ "Expire" OpHelp(""),
34213 /* 165 */ "CursorLock" OpHelp(""),
34214 /* 166 */ "CursorUnlock" OpHelp(""),
34215 /* 167 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"),
34216 /* 168 */ "VBegin" OpHelp(""),
34217 /* 169 */ "VCreate" OpHelp(""),
34218 /* 170 */ "VDestroy" OpHelp(""),
34219 /* 171 */ "VOpen" OpHelp(""),
34220 /* 172 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"),
34221 /* 173 */ "VRename" OpHelp(""),
34222 /* 174 */ "Pagecount" OpHelp(""),
34223 /* 175 */ "MaxPgcnt" OpHelp(""),
34224 /* 176 */ "Trace" OpHelp(""),
34225 /* 177 */ "CursorHint" OpHelp(""),
34226 /* 178 */ "ReleaseReg" OpHelp("release r[P1@P2] mask P3"),
34227 /* 179 */ "Noop" OpHelp(""),
34228 /* 180 */ "Explain" OpHelp(""),
34229 /* 181 */ "Abortable" OpHelp(""),
34230 };
34231 return azName[i];
34232}
34233#endif
34234
34235/************** End of opcodes.c *********************************************/
34236/************** Begin file os_unix.c *****************************************/
34237/*
34238** 2004 May 22
34239**
34240** The author disclaims copyright to this source code. In place of
34241** a legal notice, here is a blessing:
34242**
34243** May you do good and not evil.
34244** May you find forgiveness for yourself and forgive others.
34245** May you share freely, never taking more than you give.
34246**
34247******************************************************************************
34248**
34249** This file contains the VFS implementation for unix-like operating systems
34250** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others.
34251**
34252** There are actually several different VFS implementations in this file.
34253** The differences are in the way that file locking is done. The default
34254** implementation uses Posix Advisory Locks. Alternative implementations
34255** use flock(), dot-files, various proprietary locking schemas, or simply
34256** skip locking all together.
34257**
34258** This source file is organized into divisions where the logic for various
34259** subfunctions is contained within the appropriate division. PLEASE
34260** KEEP THE STRUCTURE OF THIS FILE INTACT. New code should be placed
34261** in the correct division and should be clearly labeled.
34262**
34263** The layout of divisions is as follows:
34264**
34265** * General-purpose declarations and utility functions.
34266** * Unique file ID logic used by VxWorks.
34267** * Various locking primitive implementations (all except proxy locking):
34268** + for Posix Advisory Locks
34269** + for no-op locks
34270** + for dot-file locks
34271** + for flock() locking
34272** + for named semaphore locks (VxWorks only)
34273** + for AFP filesystem locks (MacOSX only)
34274** * sqlite3_file methods not associated with locking.
34275** * Definitions of sqlite3_io_methods objects for all locking
34276** methods plus "finder" functions for each locking method.
34277** * sqlite3_vfs method implementations.
34278** * Locking primitives for the proxy uber-locking-method. (MacOSX only)
34279** * Definitions of sqlite3_vfs objects for all locking methods
34280** plus implementations of sqlite3_os_init() and sqlite3_os_end().
34281*/
34282/* #include "sqliteInt.h" */
34283#if SQLITE_OS_UNIX /* This file is used on unix only */
34284
34285/*
34286** There are various methods for file locking used for concurrency
34287** control:
34288**
34289** 1. POSIX locking (the default),
34290** 2. No locking,
34291** 3. Dot-file locking,
34292** 4. flock() locking,
34293** 5. AFP locking (OSX only),
34294** 6. Named POSIX semaphores (VXWorks only),
34295** 7. proxy locking. (OSX only)
34296**
34297** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE
34298** is defined to 1. The SQLITE_ENABLE_LOCKING_STYLE also enables automatic
34299** selection of the appropriate locking style based on the filesystem
34300** where the database is located.
34301*/
34302#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
34303# if defined(__APPLE__)
34304# define SQLITE_ENABLE_LOCKING_STYLE 1
34305# else
34306# define SQLITE_ENABLE_LOCKING_STYLE 0
34307# endif
34308#endif
34309
34310/* Use pread() and pwrite() if they are available */
34311#if defined(__APPLE__)
34312# define HAVE_PREAD 1
34313# define HAVE_PWRITE 1
34314#endif
34315#if defined(HAVE_PREAD64) && defined(HAVE_PWRITE64)
34316# undef USE_PREAD
34317# define USE_PREAD64 1
34318#elif defined(HAVE_PREAD) && defined(HAVE_PWRITE)
34319# undef USE_PREAD64
34320# define USE_PREAD 1
34321#endif
34322
34323/*
34324** standard include files.
34325*/
34326#include <sys/types.h>
34327#include <sys/stat.h>
34328#include <fcntl.h>
34329#include <sys/ioctl.h>
34330#include <unistd.h>
34331/* #include <time.h> */
34332#include <sys/time.h>
34333#include <errno.h>
34334#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
34335# include <sys/mman.h>
34336#endif
34337
34338#if SQLITE_ENABLE_LOCKING_STYLE
34339/* # include <sys/ioctl.h> */
34340# include <sys/file.h>
34341# include <sys/param.h>
34342#endif /* SQLITE_ENABLE_LOCKING_STYLE */
34343
34344/*
34345** Try to determine if gethostuuid() is available based on standard
34346** macros. This might sometimes compute the wrong value for some
34347** obscure platforms. For those cases, simply compile with one of
34348** the following:
34349**
34350** -DHAVE_GETHOSTUUID=0
34351** -DHAVE_GETHOSTUUID=1
34352**
34353** None if this matters except when building on Apple products with
34354** -DSQLITE_ENABLE_LOCKING_STYLE.
34355*/
34356#ifndef HAVE_GETHOSTUUID
34357# define HAVE_GETHOSTUUID 0
34358# if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \
34359 (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000))
34360# if (!defined(TARGET_OS_EMBEDDED) || (TARGET_OS_EMBEDDED==0)) \
34361 && (!defined(TARGET_IPHONE_SIMULATOR) || (TARGET_IPHONE_SIMULATOR==0))\
34362 && (!defined(TARGET_OS_MACCATALYST) || (TARGET_OS_MACCATALYST==0))
34363# undef HAVE_GETHOSTUUID
34364# define HAVE_GETHOSTUUID 1
34365# else
34366# warning "gethostuuid() is disabled."
34367# endif
34368# endif
34369#endif
34370
34371
34372#if OS_VXWORKS
34373/* # include <sys/ioctl.h> */
34374# include <semaphore.h>
34375# include <limits.h>
34376#endif /* OS_VXWORKS */
34377
34378#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
34379# include <sys/mount.h>
34380#endif
34381
34382#ifdef HAVE_UTIME
34383# include <utime.h>
34384#endif
34385
34386/*
34387** Allowed values of unixFile.fsFlags
34388*/
34389#define SQLITE_FSFLAGS_IS_MSDOS 0x1
34390
34391/*
34392** If we are to be thread-safe, include the pthreads header.
34393*/
34394#if SQLITE_THREADSAFE
34395/* # include <pthread.h> */
34396#endif
34397
34398/*
34399** Default permissions when creating a new file
34400*/
34401#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
34402# define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
34403#endif
34404
34405/*
34406** Default permissions when creating auto proxy dir
34407*/
34408#ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
34409# define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
34410#endif
34411
34412/*
34413** Maximum supported path-length.
34414*/
34415#define MAX_PATHNAME 512
34416
34417/*
34418** Maximum supported symbolic links
34419*/
34420#define SQLITE_MAX_SYMLINKS 100
34421
34422/* Always cast the getpid() return type for compatibility with
34423** kernel modules in VxWorks. */
34424#define osGetpid(X) (pid_t)getpid()
34425
34426/*
34427** Only set the lastErrno if the error code is a real error and not
34428** a normal expected return code of SQLITE_BUSY or SQLITE_OK
34429*/
34430#define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY))
34431
34432/* Forward references */
34433typedef struct unixShm unixShm; /* Connection shared memory */
34434typedef struct unixShmNode unixShmNode; /* Shared memory instance */
34435typedef struct unixInodeInfo unixInodeInfo; /* An i-node */
34436typedef struct UnixUnusedFd UnixUnusedFd; /* An unused file descriptor */
34437
34438/*
34439** Sometimes, after a file handle is closed by SQLite, the file descriptor
34440** cannot be closed immediately. In these cases, instances of the following
34441** structure are used to store the file descriptor while waiting for an
34442** opportunity to either close or reuse it.
34443*/
34444struct UnixUnusedFd {
34445 int fd; /* File descriptor to close */
34446 int flags; /* Flags this file descriptor was opened with */
34447 UnixUnusedFd *pNext; /* Next unused file descriptor on same file */
34448};
34449
34450/*
34451** The unixFile structure is subclass of sqlite3_file specific to the unix
34452** VFS implementations.
34453*/
34454typedef struct unixFile unixFile;
34455struct unixFile {
34456 sqlite3_io_methods const *pMethod; /* Always the first entry */
34457 sqlite3_vfs *pVfs; /* The VFS that created this unixFile */
34458 unixInodeInfo *pInode; /* Info about locks on this inode */
34459 int h; /* The file descriptor */
34460 unsigned char eFileLock; /* The type of lock held on this fd */
34461 unsigned short int ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */
34462 int lastErrno; /* The unix errno from last I/O error */
34463 void *lockingContext; /* Locking style specific state */
34464 UnixUnusedFd *pPreallocatedUnused; /* Pre-allocated UnixUnusedFd */
34465 const char *zPath; /* Name of the file */
34466 unixShm *pShm; /* Shared memory segment information */
34467 int szChunk; /* Configured by FCNTL_CHUNK_SIZE */
34468#if SQLITE_MAX_MMAP_SIZE>0
34469 int nFetchOut; /* Number of outstanding xFetch refs */
34470 sqlite3_int64 mmapSize; /* Usable size of mapping at pMapRegion */
34471 sqlite3_int64 mmapSizeActual; /* Actual size of mapping at pMapRegion */
34472 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
34473 void *pMapRegion; /* Memory mapped region */
34474#endif
34475 int sectorSize; /* Device sector size */
34476 int deviceCharacteristics; /* Precomputed device characteristics */
34477#if SQLITE_ENABLE_LOCKING_STYLE
34478 int openFlags; /* The flags specified at open() */
34479#endif
34480#if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
34481 unsigned fsFlags; /* cached details from statfs() */
34482#endif
34483#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
34484 unsigned iBusyTimeout; /* Wait this many millisec on locks */
34485#endif
34486#if OS_VXWORKS
34487 struct vxworksFileId *pId; /* Unique file ID */
34488#endif
34489#ifdef SQLITE_DEBUG
34490 /* The next group of variables are used to track whether or not the
34491 ** transaction counter in bytes 24-27 of database files are updated
34492 ** whenever any part of the database changes. An assertion fault will
34493 ** occur if a file is updated without also updating the transaction
34494 ** counter. This test is made to avoid new problems similar to the
34495 ** one described by ticket #3584.
34496 */
34497 unsigned char transCntrChng; /* True if the transaction counter changed */
34498 unsigned char dbUpdate; /* True if any part of database file changed */
34499 unsigned char inNormalWrite; /* True if in a normal write operation */
34500
34501#endif
34502
34503#ifdef SQLITE_TEST
34504 /* In test mode, increase the size of this structure a bit so that
34505 ** it is larger than the struct CrashFile defined in test6.c.
34506 */
34507 char aPadding[32];
34508#endif
34509};
34510
34511/* This variable holds the process id (pid) from when the xRandomness()
34512** method was called. If xOpen() is called from a different process id,
34513** indicating that a fork() has occurred, the PRNG will be reset.
34514*/
34515static pid_t randomnessPid = 0;
34516
34517/*
34518** Allowed values for the unixFile.ctrlFlags bitmask:
34519*/
34520#define UNIXFILE_EXCL 0x01 /* Connections from one process only */
34521#define UNIXFILE_RDONLY 0x02 /* Connection is read only */
34522#define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
34523#ifndef SQLITE_DISABLE_DIRSYNC
34524# define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */
34525#else
34526# define UNIXFILE_DIRSYNC 0x00
34527#endif
34528#define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
34529#define UNIXFILE_DELETE 0x20 /* Delete on close */
34530#define UNIXFILE_URI 0x40 /* Filename might have query parameters */
34531#define UNIXFILE_NOLOCK 0x80 /* Do no file locking */
34532
34533/*
34534** Include code that is common to all os_*.c files
34535*/
34536/* #include "os_common.h" */
34537
34538/*
34539** Define various macros that are missing from some systems.
34540*/
34541#ifndef O_LARGEFILE
34542# define O_LARGEFILE 0
34543#endif
34544#ifdef SQLITE_DISABLE_LFS
34545# undef O_LARGEFILE
34546# define O_LARGEFILE 0
34547#endif
34548#ifndef O_NOFOLLOW
34549# define O_NOFOLLOW 0
34550#endif
34551#ifndef O_BINARY
34552# define O_BINARY 0
34553#endif
34554
34555/*
34556** The threadid macro resolves to the thread-id or to 0. Used for
34557** testing and debugging only.
34558*/
34559#if SQLITE_THREADSAFE
34560#define threadid pthread_self()
34561#else
34562#define threadid 0
34563#endif
34564
34565/*
34566** HAVE_MREMAP defaults to true on Linux and false everywhere else.
34567*/
34568#if !defined(HAVE_MREMAP)
34569# if defined(__linux__) && defined(_GNU_SOURCE)
34570# define HAVE_MREMAP 1
34571# else
34572# define HAVE_MREMAP 0
34573# endif
34574#endif
34575
34576/*
34577** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek()
34578** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined.
34579*/
34580#ifdef __ANDROID__
34581# define lseek lseek64
34582#endif
34583
34584#ifdef __linux__
34585/*
34586** Linux-specific IOCTL magic numbers used for controlling F2FS
34587*/
34588#define F2FS_IOCTL_MAGIC 0xf5
34589#define F2FS_IOC_START_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 1)
34590#define F2FS_IOC_COMMIT_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 2)
34591#define F2FS_IOC_START_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 3)
34592#define F2FS_IOC_ABORT_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 5)
34593#define F2FS_IOC_GET_FEATURES _IOR(F2FS_IOCTL_MAGIC, 12, u32)
34594#define F2FS_FEATURE_ATOMIC_WRITE 0x0004
34595#endif /* __linux__ */
34596
34597
34598/*
34599** Different Unix systems declare open() in different ways. Same use
34600** open(const char*,int,mode_t). Others use open(const char*,int,...).
34601** The difference is important when using a pointer to the function.
34602**
34603** The safest way to deal with the problem is to always use this wrapper
34604** which always has the same well-defined interface.
34605*/
34606static int posixOpen(const char *zFile, int flags, int mode){
34607 return open(file: zFile, oflag: flags, mode);
34608}
34609
34610/* Forward reference */
34611static int openDirectory(const char*, int*);
34612static int unixGetpagesize(void);
34613
34614/*
34615** Many system calls are accessed through pointer-to-functions so that
34616** they may be overridden at runtime to facilitate fault injection during
34617** testing and sandboxing. The following array holds the names and pointers
34618** to all overrideable system calls.
34619*/
34620static struct unix_syscall {
34621 const char *zName; /* Name of the system call */
34622 sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
34623 sqlite3_syscall_ptr pDefault; /* Default value */
34624} aSyscall[] = {
34625 { "open", (sqlite3_syscall_ptr)posixOpen, 0 },
34626#define osOpen ((int(*)(const char*,int,int))aSyscall[0].pCurrent)
34627
34628 { "close", (sqlite3_syscall_ptr)close, 0 },
34629#define osClose ((int(*)(int))aSyscall[1].pCurrent)
34630
34631 { "access", (sqlite3_syscall_ptr)access, 0 },
34632#define osAccess ((int(*)(const char*,int))aSyscall[2].pCurrent)
34633
34634 { "getcwd", (sqlite3_syscall_ptr)getcwd, 0 },
34635#define osGetcwd ((char*(*)(char*,size_t))aSyscall[3].pCurrent)
34636
34637 { "stat", (sqlite3_syscall_ptr)stat, 0 },
34638#define osStat ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)
34639
34640/*
34641** The DJGPP compiler environment looks mostly like Unix, but it
34642** lacks the fcntl() system call. So redefine fcntl() to be something
34643** that always succeeds. This means that locking does not occur under
34644** DJGPP. But it is DOS - what did you expect?
34645*/
34646#ifdef __DJGPP__
34647 { "fstat", 0, 0 },
34648#define osFstat(a,b,c) 0
34649#else
34650 { "fstat", (sqlite3_syscall_ptr)fstat, 0 },
34651#define osFstat ((int(*)(int,struct stat*))aSyscall[5].pCurrent)
34652#endif
34653
34654 { "ftruncate", (sqlite3_syscall_ptr)ftruncate, 0 },
34655#define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent)
34656
34657 { "fcntl", (sqlite3_syscall_ptr)fcntl, 0 },
34658#define osFcntl ((int(*)(int,int,...))aSyscall[7].pCurrent)
34659
34660 { "read", (sqlite3_syscall_ptr)read, 0 },
34661#define osRead ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
34662
34663#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
34664 { "pread", (sqlite3_syscall_ptr)pread, 0 },
34665#else
34666 { "pread", (sqlite3_syscall_ptr)0, 0 },
34667#endif
34668#define osPread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
34669
34670#if defined(USE_PREAD64)
34671 { "pread64", (sqlite3_syscall_ptr)pread64, 0 },
34672#else
34673 { "pread64", (sqlite3_syscall_ptr)0, 0 },
34674#endif
34675#define osPread64 ((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent)
34676
34677 { "write", (sqlite3_syscall_ptr)write, 0 },
34678#define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
34679
34680#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
34681 { "pwrite", (sqlite3_syscall_ptr)pwrite, 0 },
34682#else
34683 { "pwrite", (sqlite3_syscall_ptr)0, 0 },
34684#endif
34685#define osPwrite ((ssize_t(*)(int,const void*,size_t,off_t))\
34686 aSyscall[12].pCurrent)
34687
34688#if defined(USE_PREAD64)
34689 { "pwrite64", (sqlite3_syscall_ptr)pwrite64, 0 },
34690#else
34691 { "pwrite64", (sqlite3_syscall_ptr)0, 0 },
34692#endif
34693#define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off64_t))\
34694 aSyscall[13].pCurrent)
34695
34696 { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 },
34697#define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent)
34698
34699#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
34700 { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 },
34701#else
34702 { "fallocate", (sqlite3_syscall_ptr)0, 0 },
34703#endif
34704#define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
34705
34706 { "unlink", (sqlite3_syscall_ptr)unlink, 0 },
34707#define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent)
34708
34709 { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 },
34710#define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent)
34711
34712 { "mkdir", (sqlite3_syscall_ptr)mkdir, 0 },
34713#define osMkdir ((int(*)(const char*,mode_t))aSyscall[18].pCurrent)
34714
34715 { "rmdir", (sqlite3_syscall_ptr)rmdir, 0 },
34716#define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent)
34717
34718#if defined(HAVE_FCHOWN)
34719 { "fchown", (sqlite3_syscall_ptr)fchown, 0 },
34720#else
34721 { "fchown", (sqlite3_syscall_ptr)0, 0 },
34722#endif
34723#define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
34724
34725#if defined(HAVE_FCHOWN)
34726 { "geteuid", (sqlite3_syscall_ptr)geteuid, 0 },
34727#else
34728 { "geteuid", (sqlite3_syscall_ptr)0, 0 },
34729#endif
34730#define osGeteuid ((uid_t(*)(void))aSyscall[21].pCurrent)
34731
34732#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
34733 { "mmap", (sqlite3_syscall_ptr)mmap, 0 },
34734#else
34735 { "mmap", (sqlite3_syscall_ptr)0, 0 },
34736#endif
34737#define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent)
34738
34739#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
34740 { "munmap", (sqlite3_syscall_ptr)munmap, 0 },
34741#else
34742 { "munmap", (sqlite3_syscall_ptr)0, 0 },
34743#endif
34744#define osMunmap ((int(*)(void*,size_t))aSyscall[23].pCurrent)
34745
34746#if HAVE_MREMAP && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
34747 { "mremap", (sqlite3_syscall_ptr)mremap, 0 },
34748#else
34749 { "mremap", (sqlite3_syscall_ptr)0, 0 },
34750#endif
34751#define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent)
34752
34753#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
34754 { "getpagesize", (sqlite3_syscall_ptr)unixGetpagesize, 0 },
34755#else
34756 { "getpagesize", (sqlite3_syscall_ptr)0, 0 },
34757#endif
34758#define osGetpagesize ((int(*)(void))aSyscall[25].pCurrent)
34759
34760#if defined(HAVE_READLINK)
34761 { "readlink", (sqlite3_syscall_ptr)readlink, 0 },
34762#else
34763 { "readlink", (sqlite3_syscall_ptr)0, 0 },
34764#endif
34765#define osReadlink ((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent)
34766
34767#if defined(HAVE_LSTAT)
34768 { "lstat", (sqlite3_syscall_ptr)lstat, 0 },
34769#else
34770 { "lstat", (sqlite3_syscall_ptr)0, 0 },
34771#endif
34772#define osLstat ((int(*)(const char*,struct stat*))aSyscall[27].pCurrent)
34773
34774#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
34775# ifdef __ANDROID__
34776 { "ioctl", (sqlite3_syscall_ptr)(int(*)(int, int, ...))ioctl, 0 },
34777#define osIoctl ((int(*)(int,int,...))aSyscall[28].pCurrent)
34778# else
34779 { "ioctl", (sqlite3_syscall_ptr)ioctl, 0 },
34780#define osIoctl ((int(*)(int,unsigned long,...))aSyscall[28].pCurrent)
34781# endif
34782#else
34783 { "ioctl", (sqlite3_syscall_ptr)0, 0 },
34784#endif
34785
34786}; /* End of the overrideable system calls */
34787
34788
34789/*
34790** On some systems, calls to fchown() will trigger a message in a security
34791** log if they come from non-root processes. So avoid calling fchown() if
34792** we are not running as root.
34793*/
34794static int robustFchown(int fd, uid_t uid, gid_t gid){
34795#if defined(HAVE_FCHOWN)
34796 return osGeteuid() ? 0 : osFchown(fd,uid,gid);
34797#else
34798 return 0;
34799#endif
34800}
34801
34802/*
34803** This is the xSetSystemCall() method of sqlite3_vfs for all of the
34804** "unix" VFSes. Return SQLITE_OK opon successfully updating the
34805** system call pointer, or SQLITE_NOTFOUND if there is no configurable
34806** system call named zName.
34807*/
34808static int unixSetSystemCall(
34809 sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */
34810 const char *zName, /* Name of system call to override */
34811 sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */
34812){
34813 unsigned int i;
34814 int rc = SQLITE_NOTFOUND;
34815
34816 UNUSED_PARAMETER(pNotUsed);
34817 if( zName==0 ){
34818 /* If no zName is given, restore all system calls to their default
34819 ** settings and return NULL
34820 */
34821 rc = SQLITE_OK;
34822 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
34823 if( aSyscall[i].pDefault ){
34824 aSyscall[i].pCurrent = aSyscall[i].pDefault;
34825 }
34826 }
34827 }else{
34828 /* If zName is specified, operate on only the one system call
34829 ** specified.
34830 */
34831 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
34832 if( strcmp(s1: zName, s2: aSyscall[i].zName)==0 ){
34833 if( aSyscall[i].pDefault==0 ){
34834 aSyscall[i].pDefault = aSyscall[i].pCurrent;
34835 }
34836 rc = SQLITE_OK;
34837 if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
34838 aSyscall[i].pCurrent = pNewFunc;
34839 break;
34840 }
34841 }
34842 }
34843 return rc;
34844}
34845
34846/*
34847** Return the value of a system call. Return NULL if zName is not a
34848** recognized system call name. NULL is also returned if the system call
34849** is currently undefined.
34850*/
34851static sqlite3_syscall_ptr unixGetSystemCall(
34852 sqlite3_vfs *pNotUsed,
34853 const char *zName
34854){
34855 unsigned int i;
34856
34857 UNUSED_PARAMETER(pNotUsed);
34858 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
34859 if( strcmp(s1: zName, s2: aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
34860 }
34861 return 0;
34862}
34863
34864/*
34865** Return the name of the first system call after zName. If zName==NULL
34866** then return the name of the first system call. Return NULL if zName
34867** is the last system call or if zName is not the name of a valid
34868** system call.
34869*/
34870static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){
34871 int i = -1;
34872
34873 UNUSED_PARAMETER(p);
34874 if( zName ){
34875 for(i=0; i<ArraySize(aSyscall)-1; i++){
34876 if( strcmp(s1: zName, s2: aSyscall[i].zName)==0 ) break;
34877 }
34878 }
34879 for(i++; i<ArraySize(aSyscall); i++){
34880 if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
34881 }
34882 return 0;
34883}
34884
34885/*
34886** Do not accept any file descriptor less than this value, in order to avoid
34887** opening database file using file descriptors that are commonly used for
34888** standard input, output, and error.
34889*/
34890#ifndef SQLITE_MINIMUM_FILE_DESCRIPTOR
34891# define SQLITE_MINIMUM_FILE_DESCRIPTOR 3
34892#endif
34893
34894/*
34895** Invoke open(). Do so multiple times, until it either succeeds or
34896** fails for some reason other than EINTR.
34897**
34898** If the file creation mode "m" is 0 then set it to the default for
34899** SQLite. The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally
34900** 0644) as modified by the system umask. If m is not 0, then
34901** make the file creation mode be exactly m ignoring the umask.
34902**
34903** The m parameter will be non-zero only when creating -wal, -journal,
34904** and -shm files. We want those files to have *exactly* the same
34905** permissions as their original database, unadulterated by the umask.
34906** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
34907** transaction crashes and leaves behind hot journals, then any
34908** process that is able to write to the database will also be able to
34909** recover the hot journals.
34910*/
34911static int robust_open(const char *z, int f, mode_t m){
34912 int fd;
34913 mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS;
34914 while(1){
34915#if defined(O_CLOEXEC)
34916 fd = osOpen(z,f|O_CLOEXEC,m2);
34917#else
34918 fd = osOpen(z,f,m2);
34919#endif
34920 if( fd<0 ){
34921 if( errno==EINTR ) continue;
34922 break;
34923 }
34924 if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break;
34925 osClose(fd);
34926 sqlite3_log(SQLITE_WARNING,
34927 zFormat: "attempt to open \"%s\" as file descriptor %d", z, fd);
34928 fd = -1;
34929 if( osOpen("/dev/null", O_RDONLY, m)<0 ) break;
34930 }
34931 if( fd>=0 ){
34932 if( m!=0 ){
34933 struct stat statbuf;
34934 if( osFstat(fd, &statbuf)==0
34935 && statbuf.st_size==0
34936 && (statbuf.st_mode&0777)!=m
34937 ){
34938 osFchmod(fd, m);
34939 }
34940 }
34941#if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0)
34942 osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
34943#endif
34944 }
34945 return fd;
34946}
34947
34948/*
34949** Helper functions to obtain and relinquish the global mutex. The
34950** global mutex is used to protect the unixInodeInfo and
34951** vxworksFileId objects used by this file, all of which may be
34952** shared by multiple threads.
34953**
34954** Function unixMutexHeld() is used to assert() that the global mutex
34955** is held when required. This function is only used as part of assert()
34956** statements. e.g.
34957**
34958** unixEnterMutex()
34959** assert( unixMutexHeld() );
34960** unixEnterLeave()
34961**
34962** To prevent deadlock, the global unixBigLock must must be acquired
34963** before the unixInodeInfo.pLockMutex mutex, if both are held. It is
34964** OK to get the pLockMutex without holding unixBigLock first, but if
34965** that happens, the unixBigLock mutex must not be acquired until after
34966** pLockMutex is released.
34967**
34968** OK: enter(unixBigLock), enter(pLockInfo)
34969** OK: enter(unixBigLock)
34970** OK: enter(pLockInfo)
34971** ERROR: enter(pLockInfo), enter(unixBigLock)
34972*/
34973static sqlite3_mutex *unixBigLock = 0;
34974static void unixEnterMutex(void){
34975 assert( sqlite3_mutex_notheld(unixBigLock) ); /* Not a recursive mutex */
34976 sqlite3_mutex_enter(p: unixBigLock);
34977}
34978static void unixLeaveMutex(void){
34979 assert( sqlite3_mutex_held(unixBigLock) );
34980 sqlite3_mutex_leave(p: unixBigLock);
34981}
34982#ifdef SQLITE_DEBUG
34983static int unixMutexHeld(void) {
34984 return sqlite3_mutex_held(unixBigLock);
34985}
34986#endif
34987
34988
34989#ifdef SQLITE_HAVE_OS_TRACE
34990/*
34991** Helper function for printing out trace information from debugging
34992** binaries. This returns the string representation of the supplied
34993** integer lock-type.
34994*/
34995static const char *azFileLock(int eFileLock){
34996 switch( eFileLock ){
34997 case NO_LOCK: return "NONE";
34998 case SHARED_LOCK: return "SHARED";
34999 case RESERVED_LOCK: return "RESERVED";
35000 case PENDING_LOCK: return "PENDING";
35001 case EXCLUSIVE_LOCK: return "EXCLUSIVE";
35002 }
35003 return "ERROR";
35004}
35005#endif
35006
35007#ifdef SQLITE_LOCK_TRACE
35008/*
35009** Print out information about all locking operations.
35010**
35011** This routine is used for troubleshooting locks on multithreaded
35012** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
35013** command-line option on the compiler. This code is normally
35014** turned off.
35015*/
35016static int lockTrace(int fd, int op, struct flock *p){
35017 char *zOpName, *zType;
35018 int s;
35019 int savedErrno;
35020 if( op==F_GETLK ){
35021 zOpName = "GETLK";
35022 }else if( op==F_SETLK ){
35023 zOpName = "SETLK";
35024 }else{
35025 s = osFcntl(fd, op, p);
35026 sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
35027 return s;
35028 }
35029 if( p->l_type==F_RDLCK ){
35030 zType = "RDLCK";
35031 }else if( p->l_type==F_WRLCK ){
35032 zType = "WRLCK";
35033 }else if( p->l_type==F_UNLCK ){
35034 zType = "UNLCK";
35035 }else{
35036 assert( 0 );
35037 }
35038 assert( p->l_whence==SEEK_SET );
35039 s = osFcntl(fd, op, p);
35040 savedErrno = errno;
35041 sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
35042 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
35043 (int)p->l_pid, s);
35044 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
35045 struct flock l2;
35046 l2 = *p;
35047 osFcntl(fd, F_GETLK, &l2);
35048 if( l2.l_type==F_RDLCK ){
35049 zType = "RDLCK";
35050 }else if( l2.l_type==F_WRLCK ){
35051 zType = "WRLCK";
35052 }else if( l2.l_type==F_UNLCK ){
35053 zType = "UNLCK";
35054 }else{
35055 assert( 0 );
35056 }
35057 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
35058 zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
35059 }
35060 errno = savedErrno;
35061 return s;
35062}
35063#undef osFcntl
35064#define osFcntl lockTrace
35065#endif /* SQLITE_LOCK_TRACE */
35066
35067/*
35068** Retry ftruncate() calls that fail due to EINTR
35069**
35070** All calls to ftruncate() within this file should be made through
35071** this wrapper. On the Android platform, bypassing the logic below
35072** could lead to a corrupt database.
35073*/
35074static int robust_ftruncate(int h, sqlite3_int64 sz){
35075 int rc;
35076#ifdef __ANDROID__
35077 /* On Android, ftruncate() always uses 32-bit offsets, even if
35078 ** _FILE_OFFSET_BITS=64 is defined. This means it is unsafe to attempt to
35079 ** truncate a file to any size larger than 2GiB. Silently ignore any
35080 ** such attempts. */
35081 if( sz>(sqlite3_int64)0x7FFFFFFF ){
35082 rc = SQLITE_OK;
35083 }else
35084#endif
35085 do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR );
35086 return rc;
35087}
35088
35089/*
35090** This routine translates a standard POSIX errno code into something
35091** useful to the clients of the sqlite3 functions. Specifically, it is
35092** intended to translate a variety of "try again" errors into SQLITE_BUSY
35093** and a variety of "please close the file descriptor NOW" errors into
35094** SQLITE_IOERR
35095**
35096** Errors during initialization of locks, or file system support for locks,
35097** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
35098*/
35099static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
35100 assert( (sqliteIOErr == SQLITE_IOERR_LOCK) ||
35101 (sqliteIOErr == SQLITE_IOERR_UNLOCK) ||
35102 (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
35103 (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) );
35104 switch (posixError) {
35105 case EACCES:
35106 case EAGAIN:
35107 case ETIMEDOUT:
35108 case EBUSY:
35109 case EINTR:
35110 case ENOLCK:
35111 /* random NFS retry error, unless during file system support
35112 * introspection, in which it actually means what it says */
35113 return SQLITE_BUSY;
35114
35115 case EPERM:
35116 return SQLITE_PERM;
35117
35118 default:
35119 return sqliteIOErr;
35120 }
35121}
35122
35123
35124/******************************************************************************
35125****************** Begin Unique File ID Utility Used By VxWorks ***************
35126**
35127** On most versions of unix, we can get a unique ID for a file by concatenating
35128** the device number and the inode number. But this does not work on VxWorks.
35129** On VxWorks, a unique file id must be based on the canonical filename.
35130**
35131** A pointer to an instance of the following structure can be used as a
35132** unique file ID in VxWorks. Each instance of this structure contains
35133** a copy of the canonical filename. There is also a reference count.
35134** The structure is reclaimed when the number of pointers to it drops to
35135** zero.
35136**
35137** There are never very many files open at one time and lookups are not
35138** a performance-critical path, so it is sufficient to put these
35139** structures on a linked list.
35140*/
35141struct vxworksFileId {
35142 struct vxworksFileId *pNext; /* Next in a list of them all */
35143 int nRef; /* Number of references to this one */
35144 int nName; /* Length of the zCanonicalName[] string */
35145 char *zCanonicalName; /* Canonical filename */
35146};
35147
35148#if OS_VXWORKS
35149/*
35150** All unique filenames are held on a linked list headed by this
35151** variable:
35152*/
35153static struct vxworksFileId *vxworksFileList = 0;
35154
35155/*
35156** Simplify a filename into its canonical form
35157** by making the following changes:
35158**
35159** * removing any trailing and duplicate /
35160** * convert /./ into just /
35161** * convert /A/../ where A is any simple name into just /
35162**
35163** Changes are made in-place. Return the new name length.
35164**
35165** The original filename is in z[0..n-1]. Return the number of
35166** characters in the simplified name.
35167*/
35168static int vxworksSimplifyName(char *z, int n){
35169 int i, j;
35170 while( n>1 && z[n-1]=='/' ){ n--; }
35171 for(i=j=0; i<n; i++){
35172 if( z[i]=='/' ){
35173 if( z[i+1]=='/' ) continue;
35174 if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){
35175 i += 1;
35176 continue;
35177 }
35178 if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){
35179 while( j>0 && z[j-1]!='/' ){ j--; }
35180 if( j>0 ){ j--; }
35181 i += 2;
35182 continue;
35183 }
35184 }
35185 z[j++] = z[i];
35186 }
35187 z[j] = 0;
35188 return j;
35189}
35190
35191/*
35192** Find a unique file ID for the given absolute pathname. Return
35193** a pointer to the vxworksFileId object. This pointer is the unique
35194** file ID.
35195**
35196** The nRef field of the vxworksFileId object is incremented before
35197** the object is returned. A new vxworksFileId object is created
35198** and added to the global list if necessary.
35199**
35200** If a memory allocation error occurs, return NULL.
35201*/
35202static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){
35203 struct vxworksFileId *pNew; /* search key and new file ID */
35204 struct vxworksFileId *pCandidate; /* For looping over existing file IDs */
35205 int n; /* Length of zAbsoluteName string */
35206
35207 assert( zAbsoluteName[0]=='/' );
35208 n = (int)strlen(zAbsoluteName);
35209 pNew = sqlite3_malloc64( sizeof(*pNew) + (n+1) );
35210 if( pNew==0 ) return 0;
35211 pNew->zCanonicalName = (char*)&pNew[1];
35212 memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
35213 n = vxworksSimplifyName(pNew->zCanonicalName, n);
35214
35215 /* Search for an existing entry that matching the canonical name.
35216 ** If found, increment the reference count and return a pointer to
35217 ** the existing file ID.
35218 */
35219 unixEnterMutex();
35220 for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
35221 if( pCandidate->nName==n
35222 && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
35223 ){
35224 sqlite3_free(pNew);
35225 pCandidate->nRef++;
35226 unixLeaveMutex();
35227 return pCandidate;
35228 }
35229 }
35230
35231 /* No match was found. We will make a new file ID */
35232 pNew->nRef = 1;
35233 pNew->nName = n;
35234 pNew->pNext = vxworksFileList;
35235 vxworksFileList = pNew;
35236 unixLeaveMutex();
35237 return pNew;
35238}
35239
35240/*
35241** Decrement the reference count on a vxworksFileId object. Free
35242** the object when the reference count reaches zero.
35243*/
35244static void vxworksReleaseFileId(struct vxworksFileId *pId){
35245 unixEnterMutex();
35246 assert( pId->nRef>0 );
35247 pId->nRef--;
35248 if( pId->nRef==0 ){
35249 struct vxworksFileId **pp;
35250 for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
35251 assert( *pp==pId );
35252 *pp = pId->pNext;
35253 sqlite3_free(pId);
35254 }
35255 unixLeaveMutex();
35256}
35257#endif /* OS_VXWORKS */
35258/*************** End of Unique File ID Utility Used By VxWorks ****************
35259******************************************************************************/
35260
35261
35262/******************************************************************************
35263*************************** Posix Advisory Locking ****************************
35264**
35265** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996)
35266** section 6.5.2.2 lines 483 through 490 specify that when a process
35267** sets or clears a lock, that operation overrides any prior locks set
35268** by the same process. It does not explicitly say so, but this implies
35269** that it overrides locks set by the same process using a different
35270** file descriptor. Consider this test case:
35271**
35272** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
35273** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
35274**
35275** Suppose ./file1 and ./file2 are really the same file (because
35276** one is a hard or symbolic link to the other) then if you set
35277** an exclusive lock on fd1, then try to get an exclusive lock
35278** on fd2, it works. I would have expected the second lock to
35279** fail since there was already a lock on the file due to fd1.
35280** But not so. Since both locks came from the same process, the
35281** second overrides the first, even though they were on different
35282** file descriptors opened on different file names.
35283**
35284** This means that we cannot use POSIX locks to synchronize file access
35285** among competing threads of the same process. POSIX locks will work fine
35286** to synchronize access for threads in separate processes, but not
35287** threads within the same process.
35288**
35289** To work around the problem, SQLite has to manage file locks internally
35290** on its own. Whenever a new database is opened, we have to find the
35291** specific inode of the database file (the inode is determined by the
35292** st_dev and st_ino fields of the stat structure that fstat() fills in)
35293** and check for locks already existing on that inode. When locks are
35294** created or removed, we have to look at our own internal record of the
35295** locks to see if another thread has previously set a lock on that same
35296** inode.
35297**
35298** (Aside: The use of inode numbers as unique IDs does not work on VxWorks.
35299** For VxWorks, we have to use the alternative unique ID system based on
35300** canonical filename and implemented in the previous division.)
35301**
35302** The sqlite3_file structure for POSIX is no longer just an integer file
35303** descriptor. It is now a structure that holds the integer file
35304** descriptor and a pointer to a structure that describes the internal
35305** locks on the corresponding inode. There is one locking structure
35306** per inode, so if the same inode is opened twice, both unixFile structures
35307** point to the same locking structure. The locking structure keeps
35308** a reference count (so we will know when to delete it) and a "cnt"
35309** field that tells us its internal lock status. cnt==0 means the
35310** file is unlocked. cnt==-1 means the file has an exclusive lock.
35311** cnt>0 means there are cnt shared locks on the file.
35312**
35313** Any attempt to lock or unlock a file first checks the locking
35314** structure. The fcntl() system call is only invoked to set a
35315** POSIX lock if the internal lock structure transitions between
35316** a locked and an unlocked state.
35317**
35318** But wait: there are yet more problems with POSIX advisory locks.
35319**
35320** If you close a file descriptor that points to a file that has locks,
35321** all locks on that file that are owned by the current process are
35322** released. To work around this problem, each unixInodeInfo object
35323** maintains a count of the number of pending locks on tha inode.
35324** When an attempt is made to close an unixFile, if there are
35325** other unixFile open on the same inode that are holding locks, the call
35326** to close() the file descriptor is deferred until all of the locks clear.
35327** The unixInodeInfo structure keeps a list of file descriptors that need to
35328** be closed and that list is walked (and cleared) when the last lock
35329** clears.
35330**
35331** Yet another problem: LinuxThreads do not play well with posix locks.
35332**
35333** Many older versions of linux use the LinuxThreads library which is
35334** not posix compliant. Under LinuxThreads, a lock created by thread
35335** A cannot be modified or overridden by a different thread B.
35336** Only thread A can modify the lock. Locking behavior is correct
35337** if the appliation uses the newer Native Posix Thread Library (NPTL)
35338** on linux - with NPTL a lock created by thread A can override locks
35339** in thread B. But there is no way to know at compile-time which
35340** threading library is being used. So there is no way to know at
35341** compile-time whether or not thread A can override locks on thread B.
35342** One has to do a run-time check to discover the behavior of the
35343** current process.
35344**
35345** SQLite used to support LinuxThreads. But support for LinuxThreads
35346** was dropped beginning with version 3.7.0. SQLite will still work with
35347** LinuxThreads provided that (1) there is no more than one connection
35348** per database file in the same process and (2) database connections
35349** do not move across threads.
35350*/
35351
35352/*
35353** An instance of the following structure serves as the key used
35354** to locate a particular unixInodeInfo object.
35355*/
35356struct unixFileId {
35357 dev_t dev; /* Device number */
35358#if OS_VXWORKS
35359 struct vxworksFileId *pId; /* Unique file ID for vxworks. */
35360#else
35361 /* We are told that some versions of Android contain a bug that
35362 ** sizes ino_t at only 32-bits instead of 64-bits. (See
35363 ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c)
35364 ** To work around this, always allocate 64-bits for the inode number.
35365 ** On small machines that only have 32-bit inodes, this wastes 4 bytes,
35366 ** but that should not be a big deal. */
35367 /* WAS: ino_t ino; */
35368 u64 ino; /* Inode number */
35369#endif
35370};
35371
35372/*
35373** An instance of the following structure is allocated for each open
35374** inode.
35375**
35376** A single inode can have multiple file descriptors, so each unixFile
35377** structure contains a pointer to an instance of this object and this
35378** object keeps a count of the number of unixFile pointing to it.
35379**
35380** Mutex rules:
35381**
35382** (1) Only the pLockMutex mutex must be held in order to read or write
35383** any of the locking fields:
35384** nShared, nLock, eFileLock, bProcessLock, pUnused
35385**
35386** (2) When nRef>0, then the following fields are unchanging and can
35387** be read (but not written) without holding any mutex:
35388** fileId, pLockMutex
35389**
35390** (3) With the exceptions above, all the fields may only be read
35391** or written while holding the global unixBigLock mutex.
35392**
35393** Deadlock prevention: The global unixBigLock mutex may not
35394** be acquired while holding the pLockMutex mutex. If both unixBigLock
35395** and pLockMutex are needed, then unixBigLock must be acquired first.
35396*/
35397struct unixInodeInfo {
35398 struct unixFileId fileId; /* The lookup key */
35399 sqlite3_mutex *pLockMutex; /* Hold this mutex for... */
35400 int nShared; /* Number of SHARED locks held */
35401 int nLock; /* Number of outstanding file locks */
35402 unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
35403 unsigned char bProcessLock; /* An exclusive process lock is held */
35404 UnixUnusedFd *pUnused; /* Unused file descriptors to close */
35405 int nRef; /* Number of pointers to this structure */
35406 unixShmNode *pShmNode; /* Shared memory associated with this inode */
35407 unixInodeInfo *pNext; /* List of all unixInodeInfo objects */
35408 unixInodeInfo *pPrev; /* .... doubly linked */
35409#if SQLITE_ENABLE_LOCKING_STYLE
35410 unsigned long long sharedByte; /* for AFP simulated shared lock */
35411#endif
35412#if OS_VXWORKS
35413 sem_t *pSem; /* Named POSIX semaphore */
35414 char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */
35415#endif
35416};
35417
35418/*
35419** A lists of all unixInodeInfo objects.
35420**
35421** Must hold unixBigLock in order to read or write this variable.
35422*/
35423static unixInodeInfo *inodeList = 0; /* All unixInodeInfo objects */
35424
35425#ifdef SQLITE_DEBUG
35426/*
35427** True if the inode mutex (on the unixFile.pFileMutex field) is held, or not.
35428** This routine is used only within assert() to help verify correct mutex
35429** usage.
35430*/
35431int unixFileMutexHeld(unixFile *pFile){
35432 assert( pFile->pInode );
35433 return sqlite3_mutex_held(pFile->pInode->pLockMutex);
35434}
35435int unixFileMutexNotheld(unixFile *pFile){
35436 assert( pFile->pInode );
35437 return sqlite3_mutex_notheld(pFile->pInode->pLockMutex);
35438}
35439#endif
35440
35441/*
35442**
35443** This function - unixLogErrorAtLine(), is only ever called via the macro
35444** unixLogError().
35445**
35446** It is invoked after an error occurs in an OS function and errno has been
35447** set. It logs a message using sqlite3_log() containing the current value of
35448** errno and, if possible, the human-readable equivalent from strerror() or
35449** strerror_r().
35450**
35451** The first argument passed to the macro should be the error code that
35452** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
35453** The two subsequent arguments should be the name of the OS function that
35454** failed (e.g. "unlink", "open") and the associated file-system path,
35455** if any.
35456*/
35457#define unixLogError(a,b,c) unixLogErrorAtLine(a,b,c,__LINE__)
35458static int unixLogErrorAtLine(
35459 int errcode, /* SQLite error code */
35460 const char *zFunc, /* Name of OS function that failed */
35461 const char *zPath, /* File path associated with error */
35462 int iLine /* Source line number where error occurred */
35463){
35464 char *zErr; /* Message from strerror() or equivalent */
35465 int iErrno = errno; /* Saved syscall error number */
35466
35467 /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use
35468 ** the strerror() function to obtain the human-readable error message
35469 ** equivalent to errno. Otherwise, use strerror_r().
35470 */
35471#if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R)
35472 char aErr[80];
35473 memset(aErr, 0, sizeof(aErr));
35474 zErr = aErr;
35475
35476 /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined,
35477 ** assume that the system provides the GNU version of strerror_r() that
35478 ** returns a pointer to a buffer containing the error message. That pointer
35479 ** may point to aErr[], or it may point to some static storage somewhere.
35480 ** Otherwise, assume that the system provides the POSIX version of
35481 ** strerror_r(), which always writes an error message into aErr[].
35482 **
35483 ** If the code incorrectly assumes that it is the POSIX version that is
35484 ** available, the error message will often be an empty string. Not a
35485 ** huge problem. Incorrectly concluding that the GNU version is available
35486 ** could lead to a segfault though.
35487 */
35488#if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU)
35489 zErr =
35490# endif
35491 strerror_r(iErrno, aErr, sizeof(aErr)-1);
35492
35493#elif SQLITE_THREADSAFE
35494 /* This is a threadsafe build, but strerror_r() is not available. */
35495 zErr = "";
35496#else
35497 /* Non-threadsafe build, use strerror(). */
35498 zErr = strerror(iErrno);
35499#endif
35500
35501 if( zPath==0 ) zPath = "";
35502 sqlite3_log(iErrCode: errcode,
35503 zFormat: "os_unix.c:%d: (%d) %s(%s) - %s",
35504 iLine, iErrno, zFunc, zPath, zErr
35505 );
35506
35507 return errcode;
35508}
35509
35510/*
35511** Close a file descriptor.
35512**
35513** We assume that close() almost always works, since it is only in a
35514** very sick application or on a very sick platform that it might fail.
35515** If it does fail, simply leak the file descriptor, but do log the
35516** error.
35517**
35518** Note that it is not safe to retry close() after EINTR since the
35519** file descriptor might have already been reused by another thread.
35520** So we don't even try to recover from an EINTR. Just log the error
35521** and move on.
35522*/
35523static void robust_close(unixFile *pFile, int h, int lineno){
35524 if( osClose(h) ){
35525 unixLogErrorAtLine(SQLITE_IOERR_CLOSE, zFunc: "close",
35526 zPath: pFile ? pFile->zPath : 0, iLine: lineno);
35527 }
35528}
35529
35530/*
35531** Set the pFile->lastErrno. Do this in a subroutine as that provides
35532** a convenient place to set a breakpoint.
35533*/
35534static void storeLastErrno(unixFile *pFile, int error){
35535 pFile->lastErrno = error;
35536}
35537
35538/*
35539** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
35540*/
35541static void closePendingFds(unixFile *pFile){
35542 unixInodeInfo *pInode = pFile->pInode;
35543 UnixUnusedFd *p;
35544 UnixUnusedFd *pNext;
35545 assert( unixFileMutexHeld(pFile) );
35546 for(p=pInode->pUnused; p; p=pNext){
35547 pNext = p->pNext;
35548 robust_close(pFile, h: p->fd, __LINE__);
35549 sqlite3_free(p);
35550 }
35551 pInode->pUnused = 0;
35552}
35553
35554/*
35555** Release a unixInodeInfo structure previously allocated by findInodeInfo().
35556**
35557** The global mutex must be held when this routine is called, but the mutex
35558** on the inode being deleted must NOT be held.
35559*/
35560static void releaseInodeInfo(unixFile *pFile){
35561 unixInodeInfo *pInode = pFile->pInode;
35562 assert( unixMutexHeld() );
35563 assert( unixFileMutexNotheld(pFile) );
35564 if( ALWAYS(pInode) ){
35565 pInode->nRef--;
35566 if( pInode->nRef==0 ){
35567 assert( pInode->pShmNode==0 );
35568 sqlite3_mutex_enter(p: pInode->pLockMutex);
35569 closePendingFds(pFile);
35570 sqlite3_mutex_leave(p: pInode->pLockMutex);
35571 if( pInode->pPrev ){
35572 assert( pInode->pPrev->pNext==pInode );
35573 pInode->pPrev->pNext = pInode->pNext;
35574 }else{
35575 assert( inodeList==pInode );
35576 inodeList = pInode->pNext;
35577 }
35578 if( pInode->pNext ){
35579 assert( pInode->pNext->pPrev==pInode );
35580 pInode->pNext->pPrev = pInode->pPrev;
35581 }
35582 sqlite3_mutex_free(p: pInode->pLockMutex);
35583 sqlite3_free(p: pInode);
35584 }
35585 }
35586}
35587
35588/*
35589** Given a file descriptor, locate the unixInodeInfo object that
35590** describes that file descriptor. Create a new one if necessary. The
35591** return value might be uninitialized if an error occurs.
35592**
35593** The global mutex must held when calling this routine.
35594**
35595** Return an appropriate error code.
35596*/
35597static int findInodeInfo(
35598 unixFile *pFile, /* Unix file with file desc used in the key */
35599 unixInodeInfo **ppInode /* Return the unixInodeInfo object here */
35600){
35601 int rc; /* System call return code */
35602 int fd; /* The file descriptor for pFile */
35603 struct unixFileId fileId; /* Lookup key for the unixInodeInfo */
35604 struct stat statbuf; /* Low-level file information */
35605 unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */
35606
35607 assert( unixMutexHeld() );
35608
35609 /* Get low-level information about the file that we can used to
35610 ** create a unique name for the file.
35611 */
35612 fd = pFile->h;
35613 rc = osFstat(fd, &statbuf);
35614 if( rc!=0 ){
35615 storeLastErrno(pFile, errno);
35616#if defined(EOVERFLOW) && defined(SQLITE_DISABLE_LFS)
35617 if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
35618#endif
35619 return SQLITE_IOERR;
35620 }
35621
35622#ifdef __APPLE__
35623 /* On OS X on an msdos filesystem, the inode number is reported
35624 ** incorrectly for zero-size files. See ticket #3260. To work
35625 ** around this problem (we consider it a bug in OS X, not SQLite)
35626 ** we always increase the file size to 1 by writing a single byte
35627 ** prior to accessing the inode number. The one byte written is
35628 ** an ASCII 'S' character which also happens to be the first byte
35629 ** in the header of every SQLite database. In this way, if there
35630 ** is a race condition such that another thread has already populated
35631 ** the first page of the database, no damage is done.
35632 */
35633 if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
35634 do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR );
35635 if( rc!=1 ){
35636 storeLastErrno(pFile, errno);
35637 return SQLITE_IOERR;
35638 }
35639 rc = osFstat(fd, &statbuf);
35640 if( rc!=0 ){
35641 storeLastErrno(pFile, errno);
35642 return SQLITE_IOERR;
35643 }
35644 }
35645#endif
35646
35647 memset(s: &fileId, c: 0, n: sizeof(fileId));
35648 fileId.dev = statbuf.st_dev;
35649#if OS_VXWORKS
35650 fileId.pId = pFile->pId;
35651#else
35652 fileId.ino = (u64)statbuf.st_ino;
35653#endif
35654 assert( unixMutexHeld() );
35655 pInode = inodeList;
35656 while( pInode && memcmp(s1: &fileId, s2: &pInode->fileId, n: sizeof(fileId)) ){
35657 pInode = pInode->pNext;
35658 }
35659 if( pInode==0 ){
35660 pInode = sqlite3_malloc64( n: sizeof(*pInode) );
35661 if( pInode==0 ){
35662 return SQLITE_NOMEM_BKPT;
35663 }
35664 memset(s: pInode, c: 0, n: sizeof(*pInode));
35665 memcpy(dest: &pInode->fileId, src: &fileId, n: sizeof(fileId));
35666 if( sqlite3GlobalConfig.bCoreMutex ){
35667 pInode->pLockMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
35668 if( pInode->pLockMutex==0 ){
35669 sqlite3_free(p: pInode);
35670 return SQLITE_NOMEM_BKPT;
35671 }
35672 }
35673 pInode->nRef = 1;
35674 assert( unixMutexHeld() );
35675 pInode->pNext = inodeList;
35676 pInode->pPrev = 0;
35677 if( inodeList ) inodeList->pPrev = pInode;
35678 inodeList = pInode;
35679 }else{
35680 pInode->nRef++;
35681 }
35682 *ppInode = pInode;
35683 return SQLITE_OK;
35684}
35685
35686/*
35687** Return TRUE if pFile has been renamed or unlinked since it was first opened.
35688*/
35689static int fileHasMoved(unixFile *pFile){
35690#if OS_VXWORKS
35691 return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
35692#else
35693 struct stat buf;
35694 return pFile->pInode!=0 &&
35695 (osStat(pFile->zPath, &buf)!=0
35696 || (u64)buf.st_ino!=pFile->pInode->fileId.ino);
35697#endif
35698}
35699
35700
35701/*
35702** Check a unixFile that is a database. Verify the following:
35703**
35704** (1) There is exactly one hard link on the file
35705** (2) The file is not a symbolic link
35706** (3) The file has not been renamed or unlinked
35707**
35708** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right.
35709*/
35710static void verifyDbFile(unixFile *pFile){
35711 struct stat buf;
35712 int rc;
35713
35714 /* These verifications occurs for the main database only */
35715 if( pFile->ctrlFlags & UNIXFILE_NOLOCK ) return;
35716
35717 rc = osFstat(pFile->h, &buf);
35718 if( rc!=0 ){
35719 sqlite3_log(SQLITE_WARNING, zFormat: "cannot fstat db file %s", pFile->zPath);
35720 return;
35721 }
35722 if( buf.st_nlink==0 ){
35723 sqlite3_log(SQLITE_WARNING, zFormat: "file unlinked while open: %s", pFile->zPath);
35724 return;
35725 }
35726 if( buf.st_nlink>1 ){
35727 sqlite3_log(SQLITE_WARNING, zFormat: "multiple links to file: %s", pFile->zPath);
35728 return;
35729 }
35730 if( fileHasMoved(pFile) ){
35731 sqlite3_log(SQLITE_WARNING, zFormat: "file renamed while open: %s", pFile->zPath);
35732 return;
35733 }
35734}
35735
35736
35737/*
35738** This routine checks if there is a RESERVED lock held on the specified
35739** file by this or any other process. If such a lock is held, set *pResOut
35740** to a non-zero value otherwise *pResOut is set to zero. The return value
35741** is set to SQLITE_OK unless an I/O error occurs during lock checking.
35742*/
35743static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
35744 int rc = SQLITE_OK;
35745 int reserved = 0;
35746 unixFile *pFile = (unixFile*)id;
35747
35748 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
35749
35750 assert( pFile );
35751 assert( pFile->eFileLock<=SHARED_LOCK );
35752 sqlite3_mutex_enter(p: pFile->pInode->pLockMutex);
35753
35754 /* Check if a thread in this process holds such a lock */
35755 if( pFile->pInode->eFileLock>SHARED_LOCK ){
35756 reserved = 1;
35757 }
35758
35759 /* Otherwise see if some other process holds it.
35760 */
35761#ifndef __DJGPP__
35762 if( !reserved && !pFile->pInode->bProcessLock ){
35763 struct flock lock;
35764 lock.l_whence = SEEK_SET;
35765 lock.l_start = RESERVED_BYTE;
35766 lock.l_len = 1;
35767 lock.l_type = F_WRLCK;
35768 if( osFcntl(pFile->h, F_GETLK, &lock) ){
35769 rc = SQLITE_IOERR_CHECKRESERVEDLOCK;
35770 storeLastErrno(pFile, errno);
35771 } else if( lock.l_type!=F_UNLCK ){
35772 reserved = 1;
35773 }
35774 }
35775#endif
35776
35777 sqlite3_mutex_leave(p: pFile->pInode->pLockMutex);
35778 OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
35779
35780 *pResOut = reserved;
35781 return rc;
35782}
35783
35784/* Forward declaration*/
35785static int unixSleep(sqlite3_vfs*,int);
35786
35787/*
35788** Set a posix-advisory-lock.
35789**
35790** There are two versions of this routine. If compiled with
35791** SQLITE_ENABLE_SETLK_TIMEOUT then the routine has an extra parameter
35792** which is a pointer to a unixFile. If the unixFile->iBusyTimeout
35793** value is set, then it is the number of milliseconds to wait before
35794** failing the lock. The iBusyTimeout value is always reset back to
35795** zero on each call.
35796**
35797** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking
35798** attempt to set the lock.
35799*/
35800#ifndef SQLITE_ENABLE_SETLK_TIMEOUT
35801# define osSetPosixAdvisoryLock(h,x,t) osFcntl(h,F_SETLK,x)
35802#else
35803static int osSetPosixAdvisoryLock(
35804 int h, /* The file descriptor on which to take the lock */
35805 struct flock *pLock, /* The description of the lock */
35806 unixFile *pFile /* Structure holding timeout value */
35807){
35808 int tm = pFile->iBusyTimeout;
35809 int rc = osFcntl(h,F_SETLK,pLock);
35810 while( rc<0 && tm>0 ){
35811 /* On systems that support some kind of blocking file lock with a timeout,
35812 ** make appropriate changes here to invoke that blocking file lock. On
35813 ** generic posix, however, there is no such API. So we simply try the
35814 ** lock once every millisecond until either the timeout expires, or until
35815 ** the lock is obtained. */
35816 unixSleep(0,1000);
35817 rc = osFcntl(h,F_SETLK,pLock);
35818 tm--;
35819 }
35820 return rc;
35821}
35822#endif /* SQLITE_ENABLE_SETLK_TIMEOUT */
35823
35824
35825/*
35826** Attempt to set a system-lock on the file pFile. The lock is
35827** described by pLock.
35828**
35829** If the pFile was opened read/write from unix-excl, then the only lock
35830** ever obtained is an exclusive lock, and it is obtained exactly once
35831** the first time any lock is attempted. All subsequent system locking
35832** operations become no-ops. Locking operations still happen internally,
35833** in order to coordinate access between separate database connections
35834** within this process, but all of that is handled in memory and the
35835** operating system does not participate.
35836**
35837** This function is a pass-through to fcntl(F_SETLK) if pFile is using
35838** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
35839** and is read-only.
35840**
35841** Zero is returned if the call completes successfully, or -1 if a call
35842** to fcntl() fails. In this case, errno is set appropriately (by fcntl()).
35843*/
35844static int unixFileLock(unixFile *pFile, struct flock *pLock){
35845 int rc;
35846 unixInodeInfo *pInode = pFile->pInode;
35847 assert( pInode!=0 );
35848 assert( sqlite3_mutex_held(pInode->pLockMutex) );
35849 if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){
35850 if( pInode->bProcessLock==0 ){
35851 struct flock lock;
35852 assert( pInode->nLock==0 );
35853 lock.l_whence = SEEK_SET;
35854 lock.l_start = SHARED_FIRST;
35855 lock.l_len = SHARED_SIZE;
35856 lock.l_type = F_WRLCK;
35857 rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile);
35858 if( rc<0 ) return rc;
35859 pInode->bProcessLock = 1;
35860 pInode->nLock++;
35861 }else{
35862 rc = 0;
35863 }
35864 }else{
35865 rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile);
35866 }
35867 return rc;
35868}
35869
35870/*
35871** Lock the file with the lock specified by parameter eFileLock - one
35872** of the following:
35873**
35874** (1) SHARED_LOCK
35875** (2) RESERVED_LOCK
35876** (3) PENDING_LOCK
35877** (4) EXCLUSIVE_LOCK
35878**
35879** Sometimes when requesting one lock state, additional lock states
35880** are inserted in between. The locking might fail on one of the later
35881** transitions leaving the lock state different from what it started but
35882** still short of its goal. The following chart shows the allowed
35883** transitions and the inserted intermediate states:
35884**
35885** UNLOCKED -> SHARED
35886** SHARED -> RESERVED
35887** SHARED -> (PENDING) -> EXCLUSIVE
35888** RESERVED -> (PENDING) -> EXCLUSIVE
35889** PENDING -> EXCLUSIVE
35890**
35891** This routine will only increase a lock. Use the sqlite3OsUnlock()
35892** routine to lower a locking level.
35893*/
35894static int unixLock(sqlite3_file *id, int eFileLock){
35895 /* The following describes the implementation of the various locks and
35896 ** lock transitions in terms of the POSIX advisory shared and exclusive
35897 ** lock primitives (called read-locks and write-locks below, to avoid
35898 ** confusion with SQLite lock names). The algorithms are complicated
35899 ** slightly in order to be compatible with Windows95 systems simultaneously
35900 ** accessing the same database file, in case that is ever required.
35901 **
35902 ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
35903 ** byte', each single bytes at well known offsets, and the 'shared byte
35904 ** range', a range of 510 bytes at a well known offset.
35905 **
35906 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
35907 ** byte'. If this is successful, 'shared byte range' is read-locked
35908 ** and the lock on the 'pending byte' released. (Legacy note: When
35909 ** SQLite was first developed, Windows95 systems were still very common,
35910 ** and Widnows95 lacks a shared-lock capability. So on Windows95, a
35911 ** single randomly selected by from the 'shared byte range' is locked.
35912 ** Windows95 is now pretty much extinct, but this work-around for the
35913 ** lack of shared-locks on Windows95 lives on, for backwards
35914 ** compatibility.)
35915 **
35916 ** A process may only obtain a RESERVED lock after it has a SHARED lock.
35917 ** A RESERVED lock is implemented by grabbing a write-lock on the
35918 ** 'reserved byte'.
35919 **
35920 ** A process may only obtain a PENDING lock after it has obtained a
35921 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
35922 ** on the 'pending byte'. This ensures that no new SHARED locks can be
35923 ** obtained, but existing SHARED locks are allowed to persist. A process
35924 ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
35925 ** This property is used by the algorithm for rolling back a journal file
35926 ** after a crash.
35927 **
35928 ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
35929 ** implemented by obtaining a write-lock on the entire 'shared byte
35930 ** range'. Since all other locks require a read-lock on one of the bytes
35931 ** within this range, this ensures that no other locks are held on the
35932 ** database.
35933 */
35934 int rc = SQLITE_OK;
35935 unixFile *pFile = (unixFile*)id;
35936 unixInodeInfo *pInode;
35937 struct flock lock;
35938 int tErrno = 0;
35939
35940 assert( pFile );
35941 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
35942 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
35943 azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared,
35944 osGetpid(0)));
35945
35946 /* If there is already a lock of this type or more restrictive on the
35947 ** unixFile, do nothing. Don't use the end_lock: exit path, as
35948 ** unixEnterMutex() hasn't been called yet.
35949 */
35950 if( pFile->eFileLock>=eFileLock ){
35951 OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h,
35952 azFileLock(eFileLock)));
35953 return SQLITE_OK;
35954 }
35955
35956 /* Make sure the locking sequence is correct.
35957 ** (1) We never move from unlocked to anything higher than shared lock.
35958 ** (2) SQLite never explicitly requests a pendig lock.
35959 ** (3) A shared lock is always held when a reserve lock is requested.
35960 */
35961 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
35962 assert( eFileLock!=PENDING_LOCK );
35963 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
35964
35965 /* This mutex is needed because pFile->pInode is shared across threads
35966 */
35967 pInode = pFile->pInode;
35968 sqlite3_mutex_enter(p: pInode->pLockMutex);
35969
35970 /* If some thread using this PID has a lock via a different unixFile*
35971 ** handle that precludes the requested lock, return BUSY.
35972 */
35973 if( (pFile->eFileLock!=pInode->eFileLock &&
35974 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
35975 ){
35976 rc = SQLITE_BUSY;
35977 goto end_lock;
35978 }
35979
35980 /* If a SHARED lock is requested, and some thread using this PID already
35981 ** has a SHARED or RESERVED lock, then increment reference counts and
35982 ** return SQLITE_OK.
35983 */
35984 if( eFileLock==SHARED_LOCK &&
35985 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
35986 assert( eFileLock==SHARED_LOCK );
35987 assert( pFile->eFileLock==0 );
35988 assert( pInode->nShared>0 );
35989 pFile->eFileLock = SHARED_LOCK;
35990 pInode->nShared++;
35991 pInode->nLock++;
35992 goto end_lock;
35993 }
35994
35995
35996 /* A PENDING lock is needed before acquiring a SHARED lock and before
35997 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
35998 ** be released.
35999 */
36000 lock.l_len = 1L;
36001 lock.l_whence = SEEK_SET;
36002 if( eFileLock==SHARED_LOCK
36003 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
36004 ){
36005 lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK);
36006 lock.l_start = PENDING_BYTE;
36007 if( unixFileLock(pFile, pLock: &lock) ){
36008 tErrno = errno;
36009 rc = sqliteErrorFromPosixError(posixError: tErrno, SQLITE_IOERR_LOCK);
36010 if( rc!=SQLITE_BUSY ){
36011 storeLastErrno(pFile, error: tErrno);
36012 }
36013 goto end_lock;
36014 }
36015 }
36016
36017
36018 /* If control gets to this point, then actually go ahead and make
36019 ** operating system calls for the specified lock.
36020 */
36021 if( eFileLock==SHARED_LOCK ){
36022 assert( pInode->nShared==0 );
36023 assert( pInode->eFileLock==0 );
36024 assert( rc==SQLITE_OK );
36025
36026 /* Now get the read-lock */
36027 lock.l_start = SHARED_FIRST;
36028 lock.l_len = SHARED_SIZE;
36029 if( unixFileLock(pFile, pLock: &lock) ){
36030 tErrno = errno;
36031 rc = sqliteErrorFromPosixError(posixError: tErrno, SQLITE_IOERR_LOCK);
36032 }
36033
36034 /* Drop the temporary PENDING lock */
36035 lock.l_start = PENDING_BYTE;
36036 lock.l_len = 1L;
36037 lock.l_type = F_UNLCK;
36038 if( unixFileLock(pFile, pLock: &lock) && rc==SQLITE_OK ){
36039 /* This could happen with a network mount */
36040 tErrno = errno;
36041 rc = SQLITE_IOERR_UNLOCK;
36042 }
36043
36044 if( rc ){
36045 if( rc!=SQLITE_BUSY ){
36046 storeLastErrno(pFile, error: tErrno);
36047 }
36048 goto end_lock;
36049 }else{
36050 pFile->eFileLock = SHARED_LOCK;
36051 pInode->nLock++;
36052 pInode->nShared = 1;
36053 }
36054 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
36055 /* We are trying for an exclusive lock but another thread in this
36056 ** same process is still holding a shared lock. */
36057 rc = SQLITE_BUSY;
36058 }else{
36059 /* The request was for a RESERVED or EXCLUSIVE lock. It is
36060 ** assumed that there is a SHARED or greater lock on the file
36061 ** already.
36062 */
36063 assert( 0!=pFile->eFileLock );
36064 lock.l_type = F_WRLCK;
36065
36066 assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK );
36067 if( eFileLock==RESERVED_LOCK ){
36068 lock.l_start = RESERVED_BYTE;
36069 lock.l_len = 1L;
36070 }else{
36071 lock.l_start = SHARED_FIRST;
36072 lock.l_len = SHARED_SIZE;
36073 }
36074
36075 if( unixFileLock(pFile, pLock: &lock) ){
36076 tErrno = errno;
36077 rc = sqliteErrorFromPosixError(posixError: tErrno, SQLITE_IOERR_LOCK);
36078 if( rc!=SQLITE_BUSY ){
36079 storeLastErrno(pFile, error: tErrno);
36080 }
36081 }
36082 }
36083
36084
36085#ifdef SQLITE_DEBUG
36086 /* Set up the transaction-counter change checking flags when
36087 ** transitioning from a SHARED to a RESERVED lock. The change
36088 ** from SHARED to RESERVED marks the beginning of a normal
36089 ** write operation (not a hot journal rollback).
36090 */
36091 if( rc==SQLITE_OK
36092 && pFile->eFileLock<=SHARED_LOCK
36093 && eFileLock==RESERVED_LOCK
36094 ){
36095 pFile->transCntrChng = 0;
36096 pFile->dbUpdate = 0;
36097 pFile->inNormalWrite = 1;
36098 }
36099#endif
36100
36101
36102 if( rc==SQLITE_OK ){
36103 pFile->eFileLock = eFileLock;
36104 pInode->eFileLock = eFileLock;
36105 }else if( eFileLock==EXCLUSIVE_LOCK ){
36106 pFile->eFileLock = PENDING_LOCK;
36107 pInode->eFileLock = PENDING_LOCK;
36108 }
36109
36110end_lock:
36111 sqlite3_mutex_leave(p: pInode->pLockMutex);
36112 OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
36113 rc==SQLITE_OK ? "ok" : "failed"));
36114 return rc;
36115}
36116
36117/*
36118** Add the file descriptor used by file handle pFile to the corresponding
36119** pUnused list.
36120*/
36121static void setPendingFd(unixFile *pFile){
36122 unixInodeInfo *pInode = pFile->pInode;
36123 UnixUnusedFd *p = pFile->pPreallocatedUnused;
36124 assert( unixFileMutexHeld(pFile) );
36125 p->pNext = pInode->pUnused;
36126 pInode->pUnused = p;
36127 pFile->h = -1;
36128 pFile->pPreallocatedUnused = 0;
36129}
36130
36131/*
36132** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
36133** must be either NO_LOCK or SHARED_LOCK.
36134**
36135** If the locking level of the file descriptor is already at or below
36136** the requested locking level, this routine is a no-op.
36137**
36138** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED
36139** the byte range is divided into 2 parts and the first part is unlocked then
36140** set to a read lock, then the other part is simply unlocked. This works
36141** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to
36142** remove the write lock on a region when a read lock is set.
36143*/
36144static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
36145 unixFile *pFile = (unixFile*)id;
36146 unixInodeInfo *pInode;
36147 struct flock lock;
36148 int rc = SQLITE_OK;
36149
36150 assert( pFile );
36151 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
36152 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
36153 osGetpid(0)));
36154
36155 assert( eFileLock<=SHARED_LOCK );
36156 if( pFile->eFileLock<=eFileLock ){
36157 return SQLITE_OK;
36158 }
36159 pInode = pFile->pInode;
36160 sqlite3_mutex_enter(p: pInode->pLockMutex);
36161 assert( pInode->nShared!=0 );
36162 if( pFile->eFileLock>SHARED_LOCK ){
36163 assert( pInode->eFileLock==pFile->eFileLock );
36164
36165#ifdef SQLITE_DEBUG
36166 /* When reducing a lock such that other processes can start
36167 ** reading the database file again, make sure that the
36168 ** transaction counter was updated if any part of the database
36169 ** file changed. If the transaction counter is not updated,
36170 ** other connections to the same file might not realize that
36171 ** the file has changed and hence might not know to flush their
36172 ** cache. The use of a stale cache can lead to database corruption.
36173 */
36174 pFile->inNormalWrite = 0;
36175#endif
36176
36177 /* downgrading to a shared lock on NFS involves clearing the write lock
36178 ** before establishing the readlock - to avoid a race condition we downgrade
36179 ** the lock in 2 blocks, so that part of the range will be covered by a
36180 ** write lock until the rest is covered by a read lock:
36181 ** 1: [WWWWW]
36182 ** 2: [....W]
36183 ** 3: [RRRRW]
36184 ** 4: [RRRR.]
36185 */
36186 if( eFileLock==SHARED_LOCK ){
36187#if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE
36188 (void)handleNFSUnlock;
36189 assert( handleNFSUnlock==0 );
36190#endif
36191#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
36192 if( handleNFSUnlock ){
36193 int tErrno; /* Error code from system call errors */
36194 off_t divSize = SHARED_SIZE - 1;
36195
36196 lock.l_type = F_UNLCK;
36197 lock.l_whence = SEEK_SET;
36198 lock.l_start = SHARED_FIRST;
36199 lock.l_len = divSize;
36200 if( unixFileLock(pFile, &lock)==(-1) ){
36201 tErrno = errno;
36202 rc = SQLITE_IOERR_UNLOCK;
36203 storeLastErrno(pFile, tErrno);
36204 goto end_unlock;
36205 }
36206 lock.l_type = F_RDLCK;
36207 lock.l_whence = SEEK_SET;
36208 lock.l_start = SHARED_FIRST;
36209 lock.l_len = divSize;
36210 if( unixFileLock(pFile, &lock)==(-1) ){
36211 tErrno = errno;
36212 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
36213 if( IS_LOCK_ERROR(rc) ){
36214 storeLastErrno(pFile, tErrno);
36215 }
36216 goto end_unlock;
36217 }
36218 lock.l_type = F_UNLCK;
36219 lock.l_whence = SEEK_SET;
36220 lock.l_start = SHARED_FIRST+divSize;
36221 lock.l_len = SHARED_SIZE-divSize;
36222 if( unixFileLock(pFile, &lock)==(-1) ){
36223 tErrno = errno;
36224 rc = SQLITE_IOERR_UNLOCK;
36225 storeLastErrno(pFile, tErrno);
36226 goto end_unlock;
36227 }
36228 }else
36229#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
36230 {
36231 lock.l_type = F_RDLCK;
36232 lock.l_whence = SEEK_SET;
36233 lock.l_start = SHARED_FIRST;
36234 lock.l_len = SHARED_SIZE;
36235 if( unixFileLock(pFile, pLock: &lock) ){
36236 /* In theory, the call to unixFileLock() cannot fail because another
36237 ** process is holding an incompatible lock. If it does, this
36238 ** indicates that the other process is not following the locking
36239 ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning
36240 ** SQLITE_BUSY would confuse the upper layer (in practice it causes
36241 ** an assert to fail). */
36242 rc = SQLITE_IOERR_RDLOCK;
36243 storeLastErrno(pFile, errno);
36244 goto end_unlock;
36245 }
36246 }
36247 }
36248 lock.l_type = F_UNLCK;
36249 lock.l_whence = SEEK_SET;
36250 lock.l_start = PENDING_BYTE;
36251 lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
36252 if( unixFileLock(pFile, pLock: &lock)==0 ){
36253 pInode->eFileLock = SHARED_LOCK;
36254 }else{
36255 rc = SQLITE_IOERR_UNLOCK;
36256 storeLastErrno(pFile, errno);
36257 goto end_unlock;
36258 }
36259 }
36260 if( eFileLock==NO_LOCK ){
36261 /* Decrement the shared lock counter. Release the lock using an
36262 ** OS call only when all threads in this same process have released
36263 ** the lock.
36264 */
36265 pInode->nShared--;
36266 if( pInode->nShared==0 ){
36267 lock.l_type = F_UNLCK;
36268 lock.l_whence = SEEK_SET;
36269 lock.l_start = lock.l_len = 0L;
36270 if( unixFileLock(pFile, pLock: &lock)==0 ){
36271 pInode->eFileLock = NO_LOCK;
36272 }else{
36273 rc = SQLITE_IOERR_UNLOCK;
36274 storeLastErrno(pFile, errno);
36275 pInode->eFileLock = NO_LOCK;
36276 pFile->eFileLock = NO_LOCK;
36277 }
36278 }
36279
36280 /* Decrement the count of locks against this same file. When the
36281 ** count reaches zero, close any other file descriptors whose close
36282 ** was deferred because of outstanding locks.
36283 */
36284 pInode->nLock--;
36285 assert( pInode->nLock>=0 );
36286 if( pInode->nLock==0 ) closePendingFds(pFile);
36287 }
36288
36289end_unlock:
36290 sqlite3_mutex_leave(p: pInode->pLockMutex);
36291 if( rc==SQLITE_OK ){
36292 pFile->eFileLock = eFileLock;
36293 }
36294 return rc;
36295}
36296
36297/*
36298** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
36299** must be either NO_LOCK or SHARED_LOCK.
36300**
36301** If the locking level of the file descriptor is already at or below
36302** the requested locking level, this routine is a no-op.
36303*/
36304static int unixUnlock(sqlite3_file *id, int eFileLock){
36305#if SQLITE_MAX_MMAP_SIZE>0
36306 assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
36307#endif
36308 return posixUnlock(id, eFileLock, handleNFSUnlock: 0);
36309}
36310
36311#if SQLITE_MAX_MMAP_SIZE>0
36312static int unixMapfile(unixFile *pFd, i64 nByte);
36313static void unixUnmapfile(unixFile *pFd);
36314#endif
36315
36316/*
36317** This function performs the parts of the "close file" operation
36318** common to all locking schemes. It closes the directory and file
36319** handles, if they are valid, and sets all fields of the unixFile
36320** structure to 0.
36321**
36322** It is *not* necessary to hold the mutex when this routine is called,
36323** even on VxWorks. A mutex will be acquired on VxWorks by the
36324** vxworksReleaseFileId() routine.
36325*/
36326static int closeUnixFile(sqlite3_file *id){
36327 unixFile *pFile = (unixFile*)id;
36328#if SQLITE_MAX_MMAP_SIZE>0
36329 unixUnmapfile(pFd: pFile);
36330#endif
36331 if( pFile->h>=0 ){
36332 robust_close(pFile, h: pFile->h, __LINE__);
36333 pFile->h = -1;
36334 }
36335#if OS_VXWORKS
36336 if( pFile->pId ){
36337 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
36338 osUnlink(pFile->pId->zCanonicalName);
36339 }
36340 vxworksReleaseFileId(pFile->pId);
36341 pFile->pId = 0;
36342 }
36343#endif
36344#ifdef SQLITE_UNLINK_AFTER_CLOSE
36345 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
36346 osUnlink(pFile->zPath);
36347 sqlite3_free(*(char**)&pFile->zPath);
36348 pFile->zPath = 0;
36349 }
36350#endif
36351 OSTRACE(("CLOSE %-3d\n", pFile->h));
36352 OpenCounter(-1);
36353 sqlite3_free(p: pFile->pPreallocatedUnused);
36354 memset(s: pFile, c: 0, n: sizeof(unixFile));
36355 return SQLITE_OK;
36356}
36357
36358/*
36359** Close a file.
36360*/
36361static int unixClose(sqlite3_file *id){
36362 int rc = SQLITE_OK;
36363 unixFile *pFile = (unixFile *)id;
36364 unixInodeInfo *pInode = pFile->pInode;
36365
36366 assert( pInode!=0 );
36367 verifyDbFile(pFile);
36368 unixUnlock(id, NO_LOCK);
36369 assert( unixFileMutexNotheld(pFile) );
36370 unixEnterMutex();
36371
36372 /* unixFile.pInode is always valid here. Otherwise, a different close
36373 ** routine (e.g. nolockClose()) would be called instead.
36374 */
36375 assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
36376 sqlite3_mutex_enter(p: pInode->pLockMutex);
36377 if( pInode->nLock ){
36378 /* If there are outstanding locks, do not actually close the file just
36379 ** yet because that would clear those locks. Instead, add the file
36380 ** descriptor to pInode->pUnused list. It will be automatically closed
36381 ** when the last lock is cleared.
36382 */
36383 setPendingFd(pFile);
36384 }
36385 sqlite3_mutex_leave(p: pInode->pLockMutex);
36386 releaseInodeInfo(pFile);
36387 assert( pFile->pShm==0 );
36388 rc = closeUnixFile(id);
36389 unixLeaveMutex();
36390 return rc;
36391}
36392
36393/************** End of the posix advisory lock implementation *****************
36394******************************************************************************/
36395
36396/******************************************************************************
36397****************************** No-op Locking **********************************
36398**
36399** Of the various locking implementations available, this is by far the
36400** simplest: locking is ignored. No attempt is made to lock the database
36401** file for reading or writing.
36402**
36403** This locking mode is appropriate for use on read-only databases
36404** (ex: databases that are burned into CD-ROM, for example.) It can
36405** also be used if the application employs some external mechanism to
36406** prevent simultaneous access of the same database by two or more
36407** database connections. But there is a serious risk of database
36408** corruption if this locking mode is used in situations where multiple
36409** database connections are accessing the same database file at the same
36410** time and one or more of those connections are writing.
36411*/
36412
36413static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
36414 UNUSED_PARAMETER(NotUsed);
36415 *pResOut = 0;
36416 return SQLITE_OK;
36417}
36418static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
36419 UNUSED_PARAMETER2(NotUsed, NotUsed2);
36420 return SQLITE_OK;
36421}
36422static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
36423 UNUSED_PARAMETER2(NotUsed, NotUsed2);
36424 return SQLITE_OK;
36425}
36426
36427/*
36428** Close the file.
36429*/
36430static int nolockClose(sqlite3_file *id) {
36431 return closeUnixFile(id);
36432}
36433
36434/******************* End of the no-op lock implementation *********************
36435******************************************************************************/
36436
36437/******************************************************************************
36438************************* Begin dot-file Locking ******************************
36439**
36440** The dotfile locking implementation uses the existence of separate lock
36441** files (really a directory) to control access to the database. This works
36442** on just about every filesystem imaginable. But there are serious downsides:
36443**
36444** (1) There is zero concurrency. A single reader blocks all other
36445** connections from reading or writing the database.
36446**
36447** (2) An application crash or power loss can leave stale lock files
36448** sitting around that need to be cleared manually.
36449**
36450** Nevertheless, a dotlock is an appropriate locking mode for use if no
36451** other locking strategy is available.
36452**
36453** Dotfile locking works by creating a subdirectory in the same directory as
36454** the database and with the same name but with a ".lock" extension added.
36455** The existence of a lock directory implies an EXCLUSIVE lock. All other
36456** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
36457*/
36458
36459/*
36460** The file suffix added to the data base filename in order to create the
36461** lock directory.
36462*/
36463#define DOTLOCK_SUFFIX ".lock"
36464
36465/*
36466** This routine checks if there is a RESERVED lock held on the specified
36467** file by this or any other process. If such a lock is held, set *pResOut
36468** to a non-zero value otherwise *pResOut is set to zero. The return value
36469** is set to SQLITE_OK unless an I/O error occurs during lock checking.
36470**
36471** In dotfile locking, either a lock exists or it does not. So in this
36472** variation of CheckReservedLock(), *pResOut is set to true if any lock
36473** is held on the file and false if the file is unlocked.
36474*/
36475static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
36476 int rc = SQLITE_OK;
36477 int reserved = 0;
36478 unixFile *pFile = (unixFile*)id;
36479
36480 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
36481
36482 assert( pFile );
36483 reserved = osAccess((const char*)pFile->lockingContext, 0)==0;
36484 OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
36485 *pResOut = reserved;
36486 return rc;
36487}
36488
36489/*
36490** Lock the file with the lock specified by parameter eFileLock - one
36491** of the following:
36492**
36493** (1) SHARED_LOCK
36494** (2) RESERVED_LOCK
36495** (3) PENDING_LOCK
36496** (4) EXCLUSIVE_LOCK
36497**
36498** Sometimes when requesting one lock state, additional lock states
36499** are inserted in between. The locking might fail on one of the later
36500** transitions leaving the lock state different from what it started but
36501** still short of its goal. The following chart shows the allowed
36502** transitions and the inserted intermediate states:
36503**
36504** UNLOCKED -> SHARED
36505** SHARED -> RESERVED
36506** SHARED -> (PENDING) -> EXCLUSIVE
36507** RESERVED -> (PENDING) -> EXCLUSIVE
36508** PENDING -> EXCLUSIVE
36509**
36510** This routine will only increase a lock. Use the sqlite3OsUnlock()
36511** routine to lower a locking level.
36512**
36513** With dotfile locking, we really only support state (4): EXCLUSIVE.
36514** But we track the other locking levels internally.
36515*/
36516static int dotlockLock(sqlite3_file *id, int eFileLock) {
36517 unixFile *pFile = (unixFile*)id;
36518 char *zLockFile = (char *)pFile->lockingContext;
36519 int rc = SQLITE_OK;
36520
36521
36522 /* If we have any lock, then the lock file already exists. All we have
36523 ** to do is adjust our internal record of the lock level.
36524 */
36525 if( pFile->eFileLock > NO_LOCK ){
36526 pFile->eFileLock = eFileLock;
36527 /* Always update the timestamp on the old file */
36528#ifdef HAVE_UTIME
36529 utime(zLockFile, NULL);
36530#else
36531 utimes(file: zLockFile, NULL);
36532#endif
36533 return SQLITE_OK;
36534 }
36535
36536 /* grab an exclusive lock */
36537 rc = osMkdir(zLockFile, 0777);
36538 if( rc<0 ){
36539 /* failed to open/create the lock directory */
36540 int tErrno = errno;
36541 if( EEXIST == tErrno ){
36542 rc = SQLITE_BUSY;
36543 } else {
36544 rc = sqliteErrorFromPosixError(posixError: tErrno, SQLITE_IOERR_LOCK);
36545 if( rc!=SQLITE_BUSY ){
36546 storeLastErrno(pFile, error: tErrno);
36547 }
36548 }
36549 return rc;
36550 }
36551
36552 /* got it, set the type and return ok */
36553 pFile->eFileLock = eFileLock;
36554 return rc;
36555}
36556
36557/*
36558** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
36559** must be either NO_LOCK or SHARED_LOCK.
36560**
36561** If the locking level of the file descriptor is already at or below
36562** the requested locking level, this routine is a no-op.
36563**
36564** When the locking level reaches NO_LOCK, delete the lock file.
36565*/
36566static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
36567 unixFile *pFile = (unixFile*)id;
36568 char *zLockFile = (char *)pFile->lockingContext;
36569 int rc;
36570
36571 assert( pFile );
36572 OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
36573 pFile->eFileLock, osGetpid(0)));
36574 assert( eFileLock<=SHARED_LOCK );
36575
36576 /* no-op if possible */
36577 if( pFile->eFileLock==eFileLock ){
36578 return SQLITE_OK;
36579 }
36580
36581 /* To downgrade to shared, simply update our internal notion of the
36582 ** lock state. No need to mess with the file on disk.
36583 */
36584 if( eFileLock==SHARED_LOCK ){
36585 pFile->eFileLock = SHARED_LOCK;
36586 return SQLITE_OK;
36587 }
36588
36589 /* To fully unlock the database, delete the lock file */
36590 assert( eFileLock==NO_LOCK );
36591 rc = osRmdir(zLockFile);
36592 if( rc<0 ){
36593 int tErrno = errno;
36594 if( tErrno==ENOENT ){
36595 rc = SQLITE_OK;
36596 }else{
36597 rc = SQLITE_IOERR_UNLOCK;
36598 storeLastErrno(pFile, error: tErrno);
36599 }
36600 return rc;
36601 }
36602 pFile->eFileLock = NO_LOCK;
36603 return SQLITE_OK;
36604}
36605
36606/*
36607** Close a file. Make sure the lock has been released before closing.
36608*/
36609static int dotlockClose(sqlite3_file *id) {
36610 unixFile *pFile = (unixFile*)id;
36611 assert( id!=0 );
36612 dotlockUnlock(id, NO_LOCK);
36613 sqlite3_free(p: pFile->lockingContext);
36614 return closeUnixFile(id);
36615}
36616/****************** End of the dot-file lock implementation *******************
36617******************************************************************************/
36618
36619/******************************************************************************
36620************************** Begin flock Locking ********************************
36621**
36622** Use the flock() system call to do file locking.
36623**
36624** flock() locking is like dot-file locking in that the various
36625** fine-grain locking levels supported by SQLite are collapsed into
36626** a single exclusive lock. In other words, SHARED, RESERVED, and
36627** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite
36628** still works when you do this, but concurrency is reduced since
36629** only a single process can be reading the database at a time.
36630**
36631** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off
36632*/
36633#if SQLITE_ENABLE_LOCKING_STYLE
36634
36635/*
36636** Retry flock() calls that fail with EINTR
36637*/
36638#ifdef EINTR
36639static int robust_flock(int fd, int op){
36640 int rc;
36641 do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR );
36642 return rc;
36643}
36644#else
36645# define robust_flock(a,b) flock(a,b)
36646#endif
36647
36648
36649/*
36650** This routine checks if there is a RESERVED lock held on the specified
36651** file by this or any other process. If such a lock is held, set *pResOut
36652** to a non-zero value otherwise *pResOut is set to zero. The return value
36653** is set to SQLITE_OK unless an I/O error occurs during lock checking.
36654*/
36655static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
36656 int rc = SQLITE_OK;
36657 int reserved = 0;
36658 unixFile *pFile = (unixFile*)id;
36659
36660 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
36661
36662 assert( pFile );
36663
36664 /* Check if a thread in this process holds such a lock */
36665 if( pFile->eFileLock>SHARED_LOCK ){
36666 reserved = 1;
36667 }
36668
36669 /* Otherwise see if some other process holds it. */
36670 if( !reserved ){
36671 /* attempt to get the lock */
36672 int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
36673 if( !lrc ){
36674 /* got the lock, unlock it */
36675 lrc = robust_flock(pFile->h, LOCK_UN);
36676 if ( lrc ) {
36677 int tErrno = errno;
36678 /* unlock failed with an error */
36679 lrc = SQLITE_IOERR_UNLOCK;
36680 storeLastErrno(pFile, tErrno);
36681 rc = lrc;
36682 }
36683 } else {
36684 int tErrno = errno;
36685 reserved = 1;
36686 /* someone else might have it reserved */
36687 lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
36688 if( IS_LOCK_ERROR(lrc) ){
36689 storeLastErrno(pFile, tErrno);
36690 rc = lrc;
36691 }
36692 }
36693 }
36694 OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
36695
36696#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
36697 if( (rc & 0xff) == SQLITE_IOERR ){
36698 rc = SQLITE_OK;
36699 reserved=1;
36700 }
36701#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
36702 *pResOut = reserved;
36703 return rc;
36704}
36705
36706/*
36707** Lock the file with the lock specified by parameter eFileLock - one
36708** of the following:
36709**
36710** (1) SHARED_LOCK
36711** (2) RESERVED_LOCK
36712** (3) PENDING_LOCK
36713** (4) EXCLUSIVE_LOCK
36714**
36715** Sometimes when requesting one lock state, additional lock states
36716** are inserted in between. The locking might fail on one of the later
36717** transitions leaving the lock state different from what it started but
36718** still short of its goal. The following chart shows the allowed
36719** transitions and the inserted intermediate states:
36720**
36721** UNLOCKED -> SHARED
36722** SHARED -> RESERVED
36723** SHARED -> (PENDING) -> EXCLUSIVE
36724** RESERVED -> (PENDING) -> EXCLUSIVE
36725** PENDING -> EXCLUSIVE
36726**
36727** flock() only really support EXCLUSIVE locks. We track intermediate
36728** lock states in the sqlite3_file structure, but all locks SHARED or
36729** above are really EXCLUSIVE locks and exclude all other processes from
36730** access the file.
36731**
36732** This routine will only increase a lock. Use the sqlite3OsUnlock()
36733** routine to lower a locking level.
36734*/
36735static int flockLock(sqlite3_file *id, int eFileLock) {
36736 int rc = SQLITE_OK;
36737 unixFile *pFile = (unixFile*)id;
36738
36739 assert( pFile );
36740
36741 /* if we already have a lock, it is exclusive.
36742 ** Just adjust level and punt on outta here. */
36743 if (pFile->eFileLock > NO_LOCK) {
36744 pFile->eFileLock = eFileLock;
36745 return SQLITE_OK;
36746 }
36747
36748 /* grab an exclusive lock */
36749
36750 if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
36751 int tErrno = errno;
36752 /* didn't get, must be busy */
36753 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
36754 if( IS_LOCK_ERROR(rc) ){
36755 storeLastErrno(pFile, tErrno);
36756 }
36757 } else {
36758 /* got it, set the type and return ok */
36759 pFile->eFileLock = eFileLock;
36760 }
36761 OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
36762 rc==SQLITE_OK ? "ok" : "failed"));
36763#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
36764 if( (rc & 0xff) == SQLITE_IOERR ){
36765 rc = SQLITE_BUSY;
36766 }
36767#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
36768 return rc;
36769}
36770
36771
36772/*
36773** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
36774** must be either NO_LOCK or SHARED_LOCK.
36775**
36776** If the locking level of the file descriptor is already at or below
36777** the requested locking level, this routine is a no-op.
36778*/
36779static int flockUnlock(sqlite3_file *id, int eFileLock) {
36780 unixFile *pFile = (unixFile*)id;
36781
36782 assert( pFile );
36783 OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
36784 pFile->eFileLock, osGetpid(0)));
36785 assert( eFileLock<=SHARED_LOCK );
36786
36787 /* no-op if possible */
36788 if( pFile->eFileLock==eFileLock ){
36789 return SQLITE_OK;
36790 }
36791
36792 /* shared can just be set because we always have an exclusive */
36793 if (eFileLock==SHARED_LOCK) {
36794 pFile->eFileLock = eFileLock;
36795 return SQLITE_OK;
36796 }
36797
36798 /* no, really, unlock. */
36799 if( robust_flock(pFile->h, LOCK_UN) ){
36800#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
36801 return SQLITE_OK;
36802#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
36803 return SQLITE_IOERR_UNLOCK;
36804 }else{
36805 pFile->eFileLock = NO_LOCK;
36806 return SQLITE_OK;
36807 }
36808}
36809
36810/*
36811** Close a file.
36812*/
36813static int flockClose(sqlite3_file *id) {
36814 assert( id!=0 );
36815 flockUnlock(id, NO_LOCK);
36816 return closeUnixFile(id);
36817}
36818
36819#endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
36820
36821/******************* End of the flock lock implementation *********************
36822******************************************************************************/
36823
36824/******************************************************************************
36825************************ Begin Named Semaphore Locking ************************
36826**
36827** Named semaphore locking is only supported on VxWorks.
36828**
36829** Semaphore locking is like dot-lock and flock in that it really only
36830** supports EXCLUSIVE locking. Only a single process can read or write
36831** the database file at a time. This reduces potential concurrency, but
36832** makes the lock implementation much easier.
36833*/
36834#if OS_VXWORKS
36835
36836/*
36837** This routine checks if there is a RESERVED lock held on the specified
36838** file by this or any other process. If such a lock is held, set *pResOut
36839** to a non-zero value otherwise *pResOut is set to zero. The return value
36840** is set to SQLITE_OK unless an I/O error occurs during lock checking.
36841*/
36842static int semXCheckReservedLock(sqlite3_file *id, int *pResOut) {
36843 int rc = SQLITE_OK;
36844 int reserved = 0;
36845 unixFile *pFile = (unixFile*)id;
36846
36847 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
36848
36849 assert( pFile );
36850
36851 /* Check if a thread in this process holds such a lock */
36852 if( pFile->eFileLock>SHARED_LOCK ){
36853 reserved = 1;
36854 }
36855
36856 /* Otherwise see if some other process holds it. */
36857 if( !reserved ){
36858 sem_t *pSem = pFile->pInode->pSem;
36859
36860 if( sem_trywait(pSem)==-1 ){
36861 int tErrno = errno;
36862 if( EAGAIN != tErrno ){
36863 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
36864 storeLastErrno(pFile, tErrno);
36865 } else {
36866 /* someone else has the lock when we are in NO_LOCK */
36867 reserved = (pFile->eFileLock < SHARED_LOCK);
36868 }
36869 }else{
36870 /* we could have it if we want it */
36871 sem_post(pSem);
36872 }
36873 }
36874 OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
36875
36876 *pResOut = reserved;
36877 return rc;
36878}
36879
36880/*
36881** Lock the file with the lock specified by parameter eFileLock - one
36882** of the following:
36883**
36884** (1) SHARED_LOCK
36885** (2) RESERVED_LOCK
36886** (3) PENDING_LOCK
36887** (4) EXCLUSIVE_LOCK
36888**
36889** Sometimes when requesting one lock state, additional lock states
36890** are inserted in between. The locking might fail on one of the later
36891** transitions leaving the lock state different from what it started but
36892** still short of its goal. The following chart shows the allowed
36893** transitions and the inserted intermediate states:
36894**
36895** UNLOCKED -> SHARED
36896** SHARED -> RESERVED
36897** SHARED -> (PENDING) -> EXCLUSIVE
36898** RESERVED -> (PENDING) -> EXCLUSIVE
36899** PENDING -> EXCLUSIVE
36900**
36901** Semaphore locks only really support EXCLUSIVE locks. We track intermediate
36902** lock states in the sqlite3_file structure, but all locks SHARED or
36903** above are really EXCLUSIVE locks and exclude all other processes from
36904** access the file.
36905**
36906** This routine will only increase a lock. Use the sqlite3OsUnlock()
36907** routine to lower a locking level.
36908*/
36909static int semXLock(sqlite3_file *id, int eFileLock) {
36910 unixFile *pFile = (unixFile*)id;
36911 sem_t *pSem = pFile->pInode->pSem;
36912 int rc = SQLITE_OK;
36913
36914 /* if we already have a lock, it is exclusive.
36915 ** Just adjust level and punt on outta here. */
36916 if (pFile->eFileLock > NO_LOCK) {
36917 pFile->eFileLock = eFileLock;
36918 rc = SQLITE_OK;
36919 goto sem_end_lock;
36920 }
36921
36922 /* lock semaphore now but bail out when already locked. */
36923 if( sem_trywait(pSem)==-1 ){
36924 rc = SQLITE_BUSY;
36925 goto sem_end_lock;
36926 }
36927
36928 /* got it, set the type and return ok */
36929 pFile->eFileLock = eFileLock;
36930
36931 sem_end_lock:
36932 return rc;
36933}
36934
36935/*
36936** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
36937** must be either NO_LOCK or SHARED_LOCK.
36938**
36939** If the locking level of the file descriptor is already at or below
36940** the requested locking level, this routine is a no-op.
36941*/
36942static int semXUnlock(sqlite3_file *id, int eFileLock) {
36943 unixFile *pFile = (unixFile*)id;
36944 sem_t *pSem = pFile->pInode->pSem;
36945
36946 assert( pFile );
36947 assert( pSem );
36948 OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
36949 pFile->eFileLock, osGetpid(0)));
36950 assert( eFileLock<=SHARED_LOCK );
36951
36952 /* no-op if possible */
36953 if( pFile->eFileLock==eFileLock ){
36954 return SQLITE_OK;
36955 }
36956
36957 /* shared can just be set because we always have an exclusive */
36958 if (eFileLock==SHARED_LOCK) {
36959 pFile->eFileLock = eFileLock;
36960 return SQLITE_OK;
36961 }
36962
36963 /* no, really unlock. */
36964 if ( sem_post(pSem)==-1 ) {
36965 int rc, tErrno = errno;
36966 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
36967 if( IS_LOCK_ERROR(rc) ){
36968 storeLastErrno(pFile, tErrno);
36969 }
36970 return rc;
36971 }
36972 pFile->eFileLock = NO_LOCK;
36973 return SQLITE_OK;
36974}
36975
36976/*
36977 ** Close a file.
36978 */
36979static int semXClose(sqlite3_file *id) {
36980 if( id ){
36981 unixFile *pFile = (unixFile*)id;
36982 semXUnlock(id, NO_LOCK);
36983 assert( pFile );
36984 assert( unixFileMutexNotheld(pFile) );
36985 unixEnterMutex();
36986 releaseInodeInfo(pFile);
36987 unixLeaveMutex();
36988 closeUnixFile(id);
36989 }
36990 return SQLITE_OK;
36991}
36992
36993#endif /* OS_VXWORKS */
36994/*
36995** Named semaphore locking is only available on VxWorks.
36996**
36997*************** End of the named semaphore lock implementation ****************
36998******************************************************************************/
36999
37000
37001/******************************************************************************
37002*************************** Begin AFP Locking *********************************
37003**
37004** AFP is the Apple Filing Protocol. AFP is a network filesystem found
37005** on Apple Macintosh computers - both OS9 and OSX.
37006**
37007** Third-party implementations of AFP are available. But this code here
37008** only works on OSX.
37009*/
37010
37011#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
37012/*
37013** The afpLockingContext structure contains all afp lock specific state
37014*/
37015typedef struct afpLockingContext afpLockingContext;
37016struct afpLockingContext {
37017 int reserved;
37018 const char *dbPath; /* Name of the open file */
37019};
37020
37021struct ByteRangeLockPB2
37022{
37023 unsigned long long offset; /* offset to first byte to lock */
37024 unsigned long long length; /* nbr of bytes to lock */
37025 unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
37026 unsigned char unLockFlag; /* 1 = unlock, 0 = lock */
37027 unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */
37028 int fd; /* file desc to assoc this lock with */
37029};
37030
37031#define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2)
37032
37033/*
37034** This is a utility for setting or clearing a bit-range lock on an
37035** AFP filesystem.
37036**
37037** Return SQLITE_OK on success, SQLITE_BUSY on failure.
37038*/
37039static int afpSetLock(
37040 const char *path, /* Name of the file to be locked or unlocked */
37041 unixFile *pFile, /* Open file descriptor on path */
37042 unsigned long long offset, /* First byte to be locked */
37043 unsigned long long length, /* Number of bytes to lock */
37044 int setLockFlag /* True to set lock. False to clear lock */
37045){
37046 struct ByteRangeLockPB2 pb;
37047 int err;
37048
37049 pb.unLockFlag = setLockFlag ? 0 : 1;
37050 pb.startEndFlag = 0;
37051 pb.offset = offset;
37052 pb.length = length;
37053 pb.fd = pFile->h;
37054
37055 OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n",
37056 (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
37057 offset, length));
37058 err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
37059 if ( err==-1 ) {
37060 int rc;
37061 int tErrno = errno;
37062 OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
37063 path, tErrno, strerror(tErrno)));
37064#ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
37065 rc = SQLITE_BUSY;
37066#else
37067 rc = sqliteErrorFromPosixError(tErrno,
37068 setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
37069#endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
37070 if( IS_LOCK_ERROR(rc) ){
37071 storeLastErrno(pFile, tErrno);
37072 }
37073 return rc;
37074 } else {
37075 return SQLITE_OK;
37076 }
37077}
37078
37079/*
37080** This routine checks if there is a RESERVED lock held on the specified
37081** file by this or any other process. If such a lock is held, set *pResOut
37082** to a non-zero value otherwise *pResOut is set to zero. The return value
37083** is set to SQLITE_OK unless an I/O error occurs during lock checking.
37084*/
37085static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
37086 int rc = SQLITE_OK;
37087 int reserved = 0;
37088 unixFile *pFile = (unixFile*)id;
37089 afpLockingContext *context;
37090
37091 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
37092
37093 assert( pFile );
37094 context = (afpLockingContext *) pFile->lockingContext;
37095 if( context->reserved ){
37096 *pResOut = 1;
37097 return SQLITE_OK;
37098 }
37099 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
37100 /* Check if a thread in this process holds such a lock */
37101 if( pFile->pInode->eFileLock>SHARED_LOCK ){
37102 reserved = 1;
37103 }
37104
37105 /* Otherwise see if some other process holds it.
37106 */
37107 if( !reserved ){
37108 /* lock the RESERVED byte */
37109 int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
37110 if( SQLITE_OK==lrc ){
37111 /* if we succeeded in taking the reserved lock, unlock it to restore
37112 ** the original state */
37113 lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
37114 } else {
37115 /* if we failed to get the lock then someone else must have it */
37116 reserved = 1;
37117 }
37118 if( IS_LOCK_ERROR(lrc) ){
37119 rc=lrc;
37120 }
37121 }
37122
37123 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
37124 OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
37125
37126 *pResOut = reserved;
37127 return rc;
37128}
37129
37130/*
37131** Lock the file with the lock specified by parameter eFileLock - one
37132** of the following:
37133**
37134** (1) SHARED_LOCK
37135** (2) RESERVED_LOCK
37136** (3) PENDING_LOCK
37137** (4) EXCLUSIVE_LOCK
37138**
37139** Sometimes when requesting one lock state, additional lock states
37140** are inserted in between. The locking might fail on one of the later
37141** transitions leaving the lock state different from what it started but
37142** still short of its goal. The following chart shows the allowed
37143** transitions and the inserted intermediate states:
37144**
37145** UNLOCKED -> SHARED
37146** SHARED -> RESERVED
37147** SHARED -> (PENDING) -> EXCLUSIVE
37148** RESERVED -> (PENDING) -> EXCLUSIVE
37149** PENDING -> EXCLUSIVE
37150**
37151** This routine will only increase a lock. Use the sqlite3OsUnlock()
37152** routine to lower a locking level.
37153*/
37154static int afpLock(sqlite3_file *id, int eFileLock){
37155 int rc = SQLITE_OK;
37156 unixFile *pFile = (unixFile*)id;
37157 unixInodeInfo *pInode = pFile->pInode;
37158 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
37159
37160 assert( pFile );
37161 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
37162 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
37163 azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0)));
37164
37165 /* If there is already a lock of this type or more restrictive on the
37166 ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
37167 ** unixEnterMutex() hasn't been called yet.
37168 */
37169 if( pFile->eFileLock>=eFileLock ){
37170 OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h,
37171 azFileLock(eFileLock)));
37172 return SQLITE_OK;
37173 }
37174
37175 /* Make sure the locking sequence is correct
37176 ** (1) We never move from unlocked to anything higher than shared lock.
37177 ** (2) SQLite never explicitly requests a pendig lock.
37178 ** (3) A shared lock is always held when a reserve lock is requested.
37179 */
37180 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
37181 assert( eFileLock!=PENDING_LOCK );
37182 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
37183
37184 /* This mutex is needed because pFile->pInode is shared across threads
37185 */
37186 pInode = pFile->pInode;
37187 sqlite3_mutex_enter(pInode->pLockMutex);
37188
37189 /* If some thread using this PID has a lock via a different unixFile*
37190 ** handle that precludes the requested lock, return BUSY.
37191 */
37192 if( (pFile->eFileLock!=pInode->eFileLock &&
37193 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
37194 ){
37195 rc = SQLITE_BUSY;
37196 goto afp_end_lock;
37197 }
37198
37199 /* If a SHARED lock is requested, and some thread using this PID already
37200 ** has a SHARED or RESERVED lock, then increment reference counts and
37201 ** return SQLITE_OK.
37202 */
37203 if( eFileLock==SHARED_LOCK &&
37204 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
37205 assert( eFileLock==SHARED_LOCK );
37206 assert( pFile->eFileLock==0 );
37207 assert( pInode->nShared>0 );
37208 pFile->eFileLock = SHARED_LOCK;
37209 pInode->nShared++;
37210 pInode->nLock++;
37211 goto afp_end_lock;
37212 }
37213
37214 /* A PENDING lock is needed before acquiring a SHARED lock and before
37215 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
37216 ** be released.
37217 */
37218 if( eFileLock==SHARED_LOCK
37219 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
37220 ){
37221 int failed;
37222 failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
37223 if (failed) {
37224 rc = failed;
37225 goto afp_end_lock;
37226 }
37227 }
37228
37229 /* If control gets to this point, then actually go ahead and make
37230 ** operating system calls for the specified lock.
37231 */
37232 if( eFileLock==SHARED_LOCK ){
37233 int lrc1, lrc2, lrc1Errno = 0;
37234 long lk, mask;
37235
37236 assert( pInode->nShared==0 );
37237 assert( pInode->eFileLock==0 );
37238
37239 mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff;
37240 /* Now get the read-lock SHARED_LOCK */
37241 /* note that the quality of the randomness doesn't matter that much */
37242 lk = random();
37243 pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
37244 lrc1 = afpSetLock(context->dbPath, pFile,
37245 SHARED_FIRST+pInode->sharedByte, 1, 1);
37246 if( IS_LOCK_ERROR(lrc1) ){
37247 lrc1Errno = pFile->lastErrno;
37248 }
37249 /* Drop the temporary PENDING lock */
37250 lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
37251
37252 if( IS_LOCK_ERROR(lrc1) ) {
37253 storeLastErrno(pFile, lrc1Errno);
37254 rc = lrc1;
37255 goto afp_end_lock;
37256 } else if( IS_LOCK_ERROR(lrc2) ){
37257 rc = lrc2;
37258 goto afp_end_lock;
37259 } else if( lrc1 != SQLITE_OK ) {
37260 rc = lrc1;
37261 } else {
37262 pFile->eFileLock = SHARED_LOCK;
37263 pInode->nLock++;
37264 pInode->nShared = 1;
37265 }
37266 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
37267 /* We are trying for an exclusive lock but another thread in this
37268 ** same process is still holding a shared lock. */
37269 rc = SQLITE_BUSY;
37270 }else{
37271 /* The request was for a RESERVED or EXCLUSIVE lock. It is
37272 ** assumed that there is a SHARED or greater lock on the file
37273 ** already.
37274 */
37275 int failed = 0;
37276 assert( 0!=pFile->eFileLock );
37277 if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
37278 /* Acquire a RESERVED lock */
37279 failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
37280 if( !failed ){
37281 context->reserved = 1;
37282 }
37283 }
37284 if (!failed && eFileLock == EXCLUSIVE_LOCK) {
37285 /* Acquire an EXCLUSIVE lock */
37286
37287 /* Remove the shared lock before trying the range. we'll need to
37288 ** reestablish the shared lock if we can't get the afpUnlock
37289 */
37290 if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
37291 pInode->sharedByte, 1, 0)) ){
37292 int failed2 = SQLITE_OK;
37293 /* now attemmpt to get the exclusive lock range */
37294 failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
37295 SHARED_SIZE, 1);
37296 if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
37297 SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
37298 /* Can't reestablish the shared lock. Sqlite can't deal, this is
37299 ** a critical I/O error
37300 */
37301 rc = ((failed & 0xff) == SQLITE_IOERR) ? failed2 :
37302 SQLITE_IOERR_LOCK;
37303 goto afp_end_lock;
37304 }
37305 }else{
37306 rc = failed;
37307 }
37308 }
37309 if( failed ){
37310 rc = failed;
37311 }
37312 }
37313
37314 if( rc==SQLITE_OK ){
37315 pFile->eFileLock = eFileLock;
37316 pInode->eFileLock = eFileLock;
37317 }else if( eFileLock==EXCLUSIVE_LOCK ){
37318 pFile->eFileLock = PENDING_LOCK;
37319 pInode->eFileLock = PENDING_LOCK;
37320 }
37321
37322afp_end_lock:
37323 sqlite3_mutex_leave(pInode->pLockMutex);
37324 OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
37325 rc==SQLITE_OK ? "ok" : "failed"));
37326 return rc;
37327}
37328
37329/*
37330** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
37331** must be either NO_LOCK or SHARED_LOCK.
37332**
37333** If the locking level of the file descriptor is already at or below
37334** the requested locking level, this routine is a no-op.
37335*/
37336static int afpUnlock(sqlite3_file *id, int eFileLock) {
37337 int rc = SQLITE_OK;
37338 unixFile *pFile = (unixFile*)id;
37339 unixInodeInfo *pInode;
37340 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
37341 int skipShared = 0;
37342#ifdef SQLITE_TEST
37343 int h = pFile->h;
37344#endif
37345
37346 assert( pFile );
37347 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
37348 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
37349 osGetpid(0)));
37350
37351 assert( eFileLock<=SHARED_LOCK );
37352 if( pFile->eFileLock<=eFileLock ){
37353 return SQLITE_OK;
37354 }
37355 pInode = pFile->pInode;
37356 sqlite3_mutex_enter(pInode->pLockMutex);
37357 assert( pInode->nShared!=0 );
37358 if( pFile->eFileLock>SHARED_LOCK ){
37359 assert( pInode->eFileLock==pFile->eFileLock );
37360 SimulateIOErrorBenign(1);
37361 SimulateIOError( h=(-1) )
37362 SimulateIOErrorBenign(0);
37363
37364#ifdef SQLITE_DEBUG
37365 /* When reducing a lock such that other processes can start
37366 ** reading the database file again, make sure that the
37367 ** transaction counter was updated if any part of the database
37368 ** file changed. If the transaction counter is not updated,
37369 ** other connections to the same file might not realize that
37370 ** the file has changed and hence might not know to flush their
37371 ** cache. The use of a stale cache can lead to database corruption.
37372 */
37373 assert( pFile->inNormalWrite==0
37374 || pFile->dbUpdate==0
37375 || pFile->transCntrChng==1 );
37376 pFile->inNormalWrite = 0;
37377#endif
37378
37379 if( pFile->eFileLock==EXCLUSIVE_LOCK ){
37380 rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
37381 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
37382 /* only re-establish the shared lock if necessary */
37383 int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
37384 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
37385 } else {
37386 skipShared = 1;
37387 }
37388 }
37389 if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
37390 rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
37391 }
37392 if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
37393 rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
37394 if( !rc ){
37395 context->reserved = 0;
37396 }
37397 }
37398 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
37399 pInode->eFileLock = SHARED_LOCK;
37400 }
37401 }
37402 if( rc==SQLITE_OK && eFileLock==NO_LOCK ){
37403
37404 /* Decrement the shared lock counter. Release the lock using an
37405 ** OS call only when all threads in this same process have released
37406 ** the lock.
37407 */
37408 unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
37409 pInode->nShared--;
37410 if( pInode->nShared==0 ){
37411 SimulateIOErrorBenign(1);
37412 SimulateIOError( h=(-1) )
37413 SimulateIOErrorBenign(0);
37414 if( !skipShared ){
37415 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
37416 }
37417 if( !rc ){
37418 pInode->eFileLock = NO_LOCK;
37419 pFile->eFileLock = NO_LOCK;
37420 }
37421 }
37422 if( rc==SQLITE_OK ){
37423 pInode->nLock--;
37424 assert( pInode->nLock>=0 );
37425 if( pInode->nLock==0 ) closePendingFds(pFile);
37426 }
37427 }
37428
37429 sqlite3_mutex_leave(pInode->pLockMutex);
37430 if( rc==SQLITE_OK ){
37431 pFile->eFileLock = eFileLock;
37432 }
37433 return rc;
37434}
37435
37436/*
37437** Close a file & cleanup AFP specific locking context
37438*/
37439static int afpClose(sqlite3_file *id) {
37440 int rc = SQLITE_OK;
37441 unixFile *pFile = (unixFile*)id;
37442 assert( id!=0 );
37443 afpUnlock(id, NO_LOCK);
37444 assert( unixFileMutexNotheld(pFile) );
37445 unixEnterMutex();
37446 if( pFile->pInode ){
37447 unixInodeInfo *pInode = pFile->pInode;
37448 sqlite3_mutex_enter(pInode->pLockMutex);
37449 if( pInode->nLock ){
37450 /* If there are outstanding locks, do not actually close the file just
37451 ** yet because that would clear those locks. Instead, add the file
37452 ** descriptor to pInode->aPending. It will be automatically closed when
37453 ** the last lock is cleared.
37454 */
37455 setPendingFd(pFile);
37456 }
37457 sqlite3_mutex_leave(pInode->pLockMutex);
37458 }
37459 releaseInodeInfo(pFile);
37460 sqlite3_free(pFile->lockingContext);
37461 rc = closeUnixFile(id);
37462 unixLeaveMutex();
37463 return rc;
37464}
37465
37466#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
37467/*
37468** The code above is the AFP lock implementation. The code is specific
37469** to MacOSX and does not work on other unix platforms. No alternative
37470** is available. If you don't compile for a mac, then the "unix-afp"
37471** VFS is not available.
37472**
37473********************* End of the AFP lock implementation **********************
37474******************************************************************************/
37475
37476/******************************************************************************
37477*************************** Begin NFS Locking ********************************/
37478
37479#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
37480/*
37481 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
37482 ** must be either NO_LOCK or SHARED_LOCK.
37483 **
37484 ** If the locking level of the file descriptor is already at or below
37485 ** the requested locking level, this routine is a no-op.
37486 */
37487static int nfsUnlock(sqlite3_file *id, int eFileLock){
37488 return posixUnlock(id, eFileLock, 1);
37489}
37490
37491#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
37492/*
37493** The code above is the NFS lock implementation. The code is specific
37494** to MacOSX and does not work on other unix platforms. No alternative
37495** is available.
37496**
37497********************* End of the NFS lock implementation **********************
37498******************************************************************************/
37499
37500/******************************************************************************
37501**************** Non-locking sqlite3_file methods *****************************
37502**
37503** The next division contains implementations for all methods of the
37504** sqlite3_file object other than the locking methods. The locking
37505** methods were defined in divisions above (one locking method per
37506** division). Those methods that are common to all locking modes
37507** are gather together into this division.
37508*/
37509
37510/*
37511** Seek to the offset passed as the second argument, then read cnt
37512** bytes into pBuf. Return the number of bytes actually read.
37513**
37514** NB: If you define USE_PREAD or USE_PREAD64, then it might also
37515** be necessary to define _XOPEN_SOURCE to be 500. This varies from
37516** one system to another. Since SQLite does not define USE_PREAD
37517** in any form by default, we will not attempt to define _XOPEN_SOURCE.
37518** See tickets #2741 and #2681.
37519**
37520** To avoid stomping the errno value on a failed read the lastErrno value
37521** is set before returning.
37522*/
37523static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
37524 int got;
37525 int prior = 0;
37526#if (!defined(USE_PREAD) && !defined(USE_PREAD64))
37527 i64 newOffset;
37528#endif
37529 TIMER_START;
37530 assert( cnt==(cnt&0x1ffff) );
37531 assert( id->h>2 );
37532 do{
37533#if defined(USE_PREAD)
37534 got = osPread(id->h, pBuf, cnt, offset);
37535 SimulateIOError( got = -1 );
37536#elif defined(USE_PREAD64)
37537 got = osPread64(id->h, pBuf, cnt, offset);
37538 SimulateIOError( got = -1 );
37539#else
37540 newOffset = lseek(fd: id->h, offset: offset, SEEK_SET);
37541 SimulateIOError( newOffset = -1 );
37542 if( newOffset<0 ){
37543 storeLastErrno(pFile: (unixFile*)id, errno);
37544 return -1;
37545 }
37546 got = osRead(id->h, pBuf, cnt);
37547#endif
37548 if( got==cnt ) break;
37549 if( got<0 ){
37550 if( errno==EINTR ){ got = 1; continue; }
37551 prior = 0;
37552 storeLastErrno(pFile: (unixFile*)id, errno);
37553 break;
37554 }else if( got>0 ){
37555 cnt -= got;
37556 offset += got;
37557 prior += got;
37558 pBuf = (void*)(got + (char*)pBuf);
37559 }
37560 }while( got>0 );
37561 TIMER_END;
37562 OSTRACE(("READ %-3d %5d %7lld %llu\n",
37563 id->h, got+prior, offset-prior, TIMER_ELAPSED));
37564 return got+prior;
37565}
37566
37567/*
37568** Read data from a file into a buffer. Return SQLITE_OK if all
37569** bytes were read successfully and SQLITE_IOERR if anything goes
37570** wrong.
37571*/
37572static int unixRead(
37573 sqlite3_file *id,
37574 void *pBuf,
37575 int amt,
37576 sqlite3_int64 offset
37577){
37578 unixFile *pFile = (unixFile *)id;
37579 int got;
37580 assert( id );
37581 assert( offset>=0 );
37582 assert( amt>0 );
37583
37584 /* If this is a database file (not a journal, super-journal or temp
37585 ** file), the bytes in the locking range should never be read or written. */
37586#if 0
37587 assert( pFile->pPreallocatedUnused==0
37588 || offset>=PENDING_BYTE+512
37589 || offset+amt<=PENDING_BYTE
37590 );
37591#endif
37592
37593#if SQLITE_MAX_MMAP_SIZE>0
37594 /* Deal with as much of this read request as possible by transfering
37595 ** data from the memory mapping using memcpy(). */
37596 if( offset<pFile->mmapSize ){
37597 if( offset+amt <= pFile->mmapSize ){
37598 memcpy(dest: pBuf, src: &((u8 *)(pFile->pMapRegion))[offset], n: amt);
37599 return SQLITE_OK;
37600 }else{
37601 int nCopy = pFile->mmapSize - offset;
37602 memcpy(dest: pBuf, src: &((u8 *)(pFile->pMapRegion))[offset], n: nCopy);
37603 pBuf = &((u8 *)pBuf)[nCopy];
37604 amt -= nCopy;
37605 offset += nCopy;
37606 }
37607 }
37608#endif
37609
37610 got = seekAndRead(id: pFile, offset, pBuf, cnt: amt);
37611 if( got==amt ){
37612 return SQLITE_OK;
37613 }else if( got<0 ){
37614 /* pFile->lastErrno has been set by seekAndRead().
37615 ** Usually we return SQLITE_IOERR_READ here, though for some
37616 ** kinds of errors we return SQLITE_IOERR_CORRUPTFS. The
37617 ** SQLITE_IOERR_CORRUPTFS will be converted into SQLITE_CORRUPT
37618 ** prior to returning to the application by the sqlite3ApiExit()
37619 ** routine.
37620 */
37621 switch( pFile->lastErrno ){
37622 case ERANGE:
37623 case EIO:
37624#ifdef ENXIO
37625 case ENXIO:
37626#endif
37627#ifdef EDEVERR
37628 case EDEVERR:
37629#endif
37630 return SQLITE_IOERR_CORRUPTFS;
37631 }
37632 return SQLITE_IOERR_READ;
37633 }else{
37634 storeLastErrno(pFile, error: 0); /* not a system error */
37635 /* Unread parts of the buffer must be zero-filled */
37636 memset(s: &((char*)pBuf)[got], c: 0, n: amt-got);
37637 return SQLITE_IOERR_SHORT_READ;
37638 }
37639}
37640
37641/*
37642** Attempt to seek the file-descriptor passed as the first argument to
37643** absolute offset iOff, then attempt to write nBuf bytes of data from
37644** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise,
37645** return the actual number of bytes written (which may be less than
37646** nBuf).
37647*/
37648static int seekAndWriteFd(
37649 int fd, /* File descriptor to write to */
37650 i64 iOff, /* File offset to begin writing at */
37651 const void *pBuf, /* Copy data from this buffer to the file */
37652 int nBuf, /* Size of buffer pBuf in bytes */
37653 int *piErrno /* OUT: Error number if error occurs */
37654){
37655 int rc = 0; /* Value returned by system call */
37656
37657 assert( nBuf==(nBuf&0x1ffff) );
37658 assert( fd>2 );
37659 assert( piErrno!=0 );
37660 nBuf &= 0x1ffff;
37661 TIMER_START;
37662
37663#if defined(USE_PREAD)
37664 do{ rc = (int)osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR );
37665#elif defined(USE_PREAD64)
37666 do{ rc = (int)osPwrite64(fd, pBuf, nBuf, iOff);}while( rc<0 && errno==EINTR);
37667#else
37668 do{
37669 i64 iSeek = lseek(fd: fd, offset: iOff, SEEK_SET);
37670 SimulateIOError( iSeek = -1 );
37671 if( iSeek<0 ){
37672 rc = -1;
37673 break;
37674 }
37675 rc = osWrite(fd, pBuf, nBuf);
37676 }while( rc<0 && errno==EINTR );
37677#endif
37678
37679 TIMER_END;
37680 OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED));
37681
37682 if( rc<0 ) *piErrno = errno;
37683 return rc;
37684}
37685
37686
37687/*
37688** Seek to the offset in id->offset then read cnt bytes into pBuf.
37689** Return the number of bytes actually read. Update the offset.
37690**
37691** To avoid stomping the errno value on a failed write the lastErrno value
37692** is set before returning.
37693*/
37694static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
37695 return seekAndWriteFd(fd: id->h, iOff: offset, pBuf, nBuf: cnt, piErrno: &id->lastErrno);
37696}
37697
37698
37699/*
37700** Write data from a buffer into a file. Return SQLITE_OK on success
37701** or some other error code on failure.
37702*/
37703static int unixWrite(
37704 sqlite3_file *id,
37705 const void *pBuf,
37706 int amt,
37707 sqlite3_int64 offset
37708){
37709 unixFile *pFile = (unixFile*)id;
37710 int wrote = 0;
37711 assert( id );
37712 assert( amt>0 );
37713
37714 /* If this is a database file (not a journal, super-journal or temp
37715 ** file), the bytes in the locking range should never be read or written. */
37716#if 0
37717 assert( pFile->pPreallocatedUnused==0
37718 || offset>=PENDING_BYTE+512
37719 || offset+amt<=PENDING_BYTE
37720 );
37721#endif
37722
37723#ifdef SQLITE_DEBUG
37724 /* If we are doing a normal write to a database file (as opposed to
37725 ** doing a hot-journal rollback or a write to some file other than a
37726 ** normal database file) then record the fact that the database
37727 ** has changed. If the transaction counter is modified, record that
37728 ** fact too.
37729 */
37730 if( pFile->inNormalWrite ){
37731 pFile->dbUpdate = 1; /* The database has been modified */
37732 if( offset<=24 && offset+amt>=27 ){
37733 int rc;
37734 char oldCntr[4];
37735 SimulateIOErrorBenign(1);
37736 rc = seekAndRead(pFile, 24, oldCntr, 4);
37737 SimulateIOErrorBenign(0);
37738 if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
37739 pFile->transCntrChng = 1; /* The transaction counter has changed */
37740 }
37741 }
37742 }
37743#endif
37744
37745#if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0
37746 /* Deal with as much of this write request as possible by transfering
37747 ** data from the memory mapping using memcpy(). */
37748 if( offset<pFile->mmapSize ){
37749 if( offset+amt <= pFile->mmapSize ){
37750 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
37751 return SQLITE_OK;
37752 }else{
37753 int nCopy = pFile->mmapSize - offset;
37754 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
37755 pBuf = &((u8 *)pBuf)[nCopy];
37756 amt -= nCopy;
37757 offset += nCopy;
37758 }
37759 }
37760#endif
37761
37762 while( (wrote = seekAndWrite(id: pFile, offset, pBuf, cnt: amt))<amt && wrote>0 ){
37763 amt -= wrote;
37764 offset += wrote;
37765 pBuf = &((char*)pBuf)[wrote];
37766 }
37767 SimulateIOError(( wrote=(-1), amt=1 ));
37768 SimulateDiskfullError(( wrote=0, amt=1 ));
37769
37770 if( amt>wrote ){
37771 if( wrote<0 && pFile->lastErrno!=ENOSPC ){
37772 /* lastErrno set by seekAndWrite */
37773 return SQLITE_IOERR_WRITE;
37774 }else{
37775 storeLastErrno(pFile, error: 0); /* not a system error */
37776 return SQLITE_FULL;
37777 }
37778 }
37779
37780 return SQLITE_OK;
37781}
37782
37783#ifdef SQLITE_TEST
37784/*
37785** Count the number of fullsyncs and normal syncs. This is used to test
37786** that syncs and fullsyncs are occurring at the right times.
37787*/
37788SQLITE_API int sqlite3_sync_count = 0;
37789SQLITE_API int sqlite3_fullsync_count = 0;
37790#endif
37791
37792/*
37793** We do not trust systems to provide a working fdatasync(). Some do.
37794** Others do no. To be safe, we will stick with the (slightly slower)
37795** fsync(). If you know that your system does support fdatasync() correctly,
37796** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC
37797*/
37798#if !defined(fdatasync) && !HAVE_FDATASYNC
37799# define fdatasync fsync
37800#endif
37801
37802/*
37803** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
37804** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
37805** only available on Mac OS X. But that could change.
37806*/
37807#ifdef F_FULLFSYNC
37808# define HAVE_FULLFSYNC 1
37809#else
37810# define HAVE_FULLFSYNC 0
37811#endif
37812
37813
37814/*
37815** The fsync() system call does not work as advertised on many
37816** unix systems. The following procedure is an attempt to make
37817** it work better.
37818**
37819** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
37820** for testing when we want to run through the test suite quickly.
37821** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
37822** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
37823** or power failure will likely corrupt the database file.
37824**
37825** SQLite sets the dataOnly flag if the size of the file is unchanged.
37826** The idea behind dataOnly is that it should only write the file content
37827** to disk, not the inode. We only set dataOnly if the file size is
37828** unchanged since the file size is part of the inode. However,
37829** Ted Ts'o tells us that fdatasync() will also write the inode if the
37830** file size has changed. The only real difference between fdatasync()
37831** and fsync(), Ted tells us, is that fdatasync() will not flush the
37832** inode if the mtime or owner or other inode attributes have changed.
37833** We only care about the file size, not the other file attributes, so
37834** as far as SQLite is concerned, an fdatasync() is always adequate.
37835** So, we always use fdatasync() if it is available, regardless of
37836** the value of the dataOnly flag.
37837*/
37838static int full_fsync(int fd, int fullSync, int dataOnly){
37839 int rc;
37840
37841 /* The following "ifdef/elif/else/" block has the same structure as
37842 ** the one below. It is replicated here solely to avoid cluttering
37843 ** up the real code with the UNUSED_PARAMETER() macros.
37844 */
37845#ifdef SQLITE_NO_SYNC
37846 UNUSED_PARAMETER(fd);
37847 UNUSED_PARAMETER(fullSync);
37848 UNUSED_PARAMETER(dataOnly);
37849#elif HAVE_FULLFSYNC
37850 UNUSED_PARAMETER(dataOnly);
37851#else
37852 UNUSED_PARAMETER(fullSync);
37853 UNUSED_PARAMETER(dataOnly);
37854#endif
37855
37856 /* Record the number of times that we do a normal fsync() and
37857 ** FULLSYNC. This is used during testing to verify that this procedure
37858 ** gets called with the correct arguments.
37859 */
37860#ifdef SQLITE_TEST
37861 if( fullSync ) sqlite3_fullsync_count++;
37862 sqlite3_sync_count++;
37863#endif
37864
37865 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
37866 ** no-op. But go ahead and call fstat() to validate the file
37867 ** descriptor as we need a method to provoke a failure during
37868 ** coverate testing.
37869 */
37870#ifdef SQLITE_NO_SYNC
37871 {
37872 struct stat buf;
37873 rc = osFstat(fd, &buf);
37874 }
37875#elif HAVE_FULLFSYNC
37876 if( fullSync ){
37877 rc = osFcntl(fd, F_FULLFSYNC, 0);
37878 }else{
37879 rc = 1;
37880 }
37881 /* If the FULLFSYNC failed, fall back to attempting an fsync().
37882 ** It shouldn't be possible for fullfsync to fail on the local
37883 ** file system (on OSX), so failure indicates that FULLFSYNC
37884 ** isn't supported for this file system. So, attempt an fsync
37885 ** and (for now) ignore the overhead of a superfluous fcntl call.
37886 ** It'd be better to detect fullfsync support once and avoid
37887 ** the fcntl call every time sync is called.
37888 */
37889 if( rc ) rc = fsync(fd);
37890
37891#elif defined(__APPLE__)
37892 /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly
37893 ** so currently we default to the macro that redefines fdatasync to fsync
37894 */
37895 rc = fsync(fd);
37896#else
37897 rc = fdatasync(fd: fd);
37898#if OS_VXWORKS
37899 if( rc==-1 && errno==ENOTSUP ){
37900 rc = fsync(fd);
37901 }
37902#endif /* OS_VXWORKS */
37903#endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
37904
37905 if( OS_VXWORKS && rc!= -1 ){
37906 rc = 0;
37907 }
37908 return rc;
37909}
37910
37911/*
37912** Open a file descriptor to the directory containing file zFilename.
37913** If successful, *pFd is set to the opened file descriptor and
37914** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
37915** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
37916** value.
37917**
37918** The directory file descriptor is used for only one thing - to
37919** fsync() a directory to make sure file creation and deletion events
37920** are flushed to disk. Such fsyncs are not needed on newer
37921** journaling filesystems, but are required on older filesystems.
37922**
37923** This routine can be overridden using the xSetSysCall interface.
37924** The ability to override this routine was added in support of the
37925** chromium sandbox. Opening a directory is a security risk (we are
37926** told) so making it overrideable allows the chromium sandbox to
37927** replace this routine with a harmless no-op. To make this routine
37928** a no-op, replace it with a stub that returns SQLITE_OK but leaves
37929** *pFd set to a negative number.
37930**
37931** If SQLITE_OK is returned, the caller is responsible for closing
37932** the file descriptor *pFd using close().
37933*/
37934static int openDirectory(const char *zFilename, int *pFd){
37935 int ii;
37936 int fd = -1;
37937 char zDirname[MAX_PATHNAME+1];
37938
37939 sqlite3_snprintf(MAX_PATHNAME, zBuf: zDirname, zFormat: "%s", zFilename);
37940 for(ii=(int)strlen(s: zDirname); ii>0 && zDirname[ii]!='/'; ii--);
37941 if( ii>0 ){
37942 zDirname[ii] = '\0';
37943 }else{
37944 if( zDirname[0]!='/' ) zDirname[0] = '.';
37945 zDirname[1] = 0;
37946 }
37947 fd = robust_open(z: zDirname, O_RDONLY|O_BINARY, m: 0);
37948 if( fd>=0 ){
37949 OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
37950 }
37951 *pFd = fd;
37952 if( fd>=0 ) return SQLITE_OK;
37953 return unixLogError(SQLITE_CANTOPEN_BKPT, "openDirectory", zDirname);
37954}
37955
37956/*
37957** Make sure all writes to a particular file are committed to disk.
37958**
37959** If dataOnly==0 then both the file itself and its metadata (file
37960** size, access time, etc) are synced. If dataOnly!=0 then only the
37961** file data is synced.
37962**
37963** Under Unix, also make sure that the directory entry for the file
37964** has been created by fsync-ing the directory that contains the file.
37965** If we do not do this and we encounter a power failure, the directory
37966** entry for the journal might not exist after we reboot. The next
37967** SQLite to access the file will not know that the journal exists (because
37968** the directory entry for the journal was never created) and the transaction
37969** will not roll back - possibly leading to database corruption.
37970*/
37971static int unixSync(sqlite3_file *id, int flags){
37972 int rc;
37973 unixFile *pFile = (unixFile*)id;
37974
37975 int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
37976 int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
37977
37978 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
37979 assert((flags&0x0F)==SQLITE_SYNC_NORMAL
37980 || (flags&0x0F)==SQLITE_SYNC_FULL
37981 );
37982
37983 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
37984 ** line is to test that doing so does not cause any problems.
37985 */
37986 SimulateDiskfullError( return SQLITE_FULL );
37987
37988 assert( pFile );
37989 OSTRACE(("SYNC %-3d\n", pFile->h));
37990 rc = full_fsync(fd: pFile->h, fullSync: isFullsync, dataOnly: isDataOnly);
37991 SimulateIOError( rc=1 );
37992 if( rc ){
37993 storeLastErrno(pFile, errno);
37994 return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
37995 }
37996
37997 /* Also fsync the directory containing the file if the DIRSYNC flag
37998 ** is set. This is a one-time occurrence. Many systems (examples: AIX)
37999 ** are unable to fsync a directory, so ignore errors on the fsync.
38000 */
38001 if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
38002 int dirfd;
38003 OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
38004 HAVE_FULLFSYNC, isFullsync));
38005 rc = osOpenDirectory(pFile->zPath, &dirfd);
38006 if( rc==SQLITE_OK ){
38007 full_fsync(fd: dirfd, fullSync: 0, dataOnly: 0);
38008 robust_close(pFile, h: dirfd, __LINE__);
38009 }else{
38010 assert( rc==SQLITE_CANTOPEN );
38011 rc = SQLITE_OK;
38012 }
38013 pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
38014 }
38015 return rc;
38016}
38017
38018/*
38019** Truncate an open file to a specified size
38020*/
38021static int unixTruncate(sqlite3_file *id, i64 nByte){
38022 unixFile *pFile = (unixFile *)id;
38023 int rc;
38024 assert( pFile );
38025 SimulateIOError( return SQLITE_IOERR_TRUNCATE );
38026
38027 /* If the user has configured a chunk-size for this file, truncate the
38028 ** file so that it consists of an integer number of chunks (i.e. the
38029 ** actual file size after the operation may be larger than the requested
38030 ** size).
38031 */
38032 if( pFile->szChunk>0 ){
38033 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
38034 }
38035
38036 rc = robust_ftruncate(h: pFile->h, sz: nByte);
38037 if( rc ){
38038 storeLastErrno(pFile, errno);
38039 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
38040 }else{
38041#ifdef SQLITE_DEBUG
38042 /* If we are doing a normal write to a database file (as opposed to
38043 ** doing a hot-journal rollback or a write to some file other than a
38044 ** normal database file) and we truncate the file to zero length,
38045 ** that effectively updates the change counter. This might happen
38046 ** when restoring a database using the backup API from a zero-length
38047 ** source.
38048 */
38049 if( pFile->inNormalWrite && nByte==0 ){
38050 pFile->transCntrChng = 1;
38051 }
38052#endif
38053
38054#if SQLITE_MAX_MMAP_SIZE>0
38055 /* If the file was just truncated to a size smaller than the currently
38056 ** mapped region, reduce the effective mapping size as well. SQLite will
38057 ** use read() and write() to access data beyond this point from now on.
38058 */
38059 if( nByte<pFile->mmapSize ){
38060 pFile->mmapSize = nByte;
38061 }
38062#endif
38063
38064 return SQLITE_OK;
38065 }
38066}
38067
38068/*
38069** Determine the current size of a file in bytes
38070*/
38071static int unixFileSize(sqlite3_file *id, i64 *pSize){
38072 int rc;
38073 struct stat buf;
38074 assert( id );
38075 rc = osFstat(((unixFile*)id)->h, &buf);
38076 SimulateIOError( rc=1 );
38077 if( rc!=0 ){
38078 storeLastErrno(pFile: (unixFile*)id, errno);
38079 return SQLITE_IOERR_FSTAT;
38080 }
38081 *pSize = buf.st_size;
38082
38083 /* When opening a zero-size database, the findInodeInfo() procedure
38084 ** writes a single byte into that file in order to work around a bug
38085 ** in the OS-X msdos filesystem. In order to avoid problems with upper
38086 ** layers, we need to report this file size as zero even though it is
38087 ** really 1. Ticket #3260.
38088 */
38089 if( *pSize==1 ) *pSize = 0;
38090
38091
38092 return SQLITE_OK;
38093}
38094
38095#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
38096/*
38097** Handler for proxy-locking file-control verbs. Defined below in the
38098** proxying locking division.
38099*/
38100static int proxyFileControl(sqlite3_file*,int,void*);
38101#endif
38102
38103/*
38104** This function is called to handle the SQLITE_FCNTL_SIZE_HINT
38105** file-control operation. Enlarge the database to nBytes in size
38106** (rounded up to the next chunk-size). If the database is already
38107** nBytes or larger, this routine is a no-op.
38108*/
38109static int fcntlSizeHint(unixFile *pFile, i64 nByte){
38110 if( pFile->szChunk>0 ){
38111 i64 nSize; /* Required file size */
38112 struct stat buf; /* Used to hold return values of fstat() */
38113
38114 if( osFstat(pFile->h, &buf) ){
38115 return SQLITE_IOERR_FSTAT;
38116 }
38117
38118 nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
38119 if( nSize>(i64)buf.st_size ){
38120
38121#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
38122 /* The code below is handling the return value of osFallocate()
38123 ** correctly. posix_fallocate() is defined to "returns zero on success,
38124 ** or an error number on failure". See the manpage for details. */
38125 int err;
38126 do{
38127 err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
38128 }while( err==EINTR );
38129 if( err && err!=EINVAL ) return SQLITE_IOERR_WRITE;
38130#else
38131 /* If the OS does not have posix_fallocate(), fake it. Write a
38132 ** single byte to the last byte in each block that falls entirely
38133 ** within the extended region. Then, if required, a single byte
38134 ** at offset (nSize-1), to set the size of the file correctly.
38135 ** This is a similar technique to that used by glibc on systems
38136 ** that do not have a real fallocate() call.
38137 */
38138 int nBlk = buf.st_blksize; /* File-system block size */
38139 int nWrite = 0; /* Number of bytes written by seekAndWrite */
38140 i64 iWrite; /* Next offset to write to */
38141
38142 iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1;
38143 assert( iWrite>=buf.st_size );
38144 assert( ((iWrite+1)%nBlk)==0 );
38145 for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){
38146 if( iWrite>=nSize ) iWrite = nSize - 1;
38147 nWrite = seekAndWrite(pFile, iWrite, "", 1);
38148 if( nWrite!=1 ) return SQLITE_IOERR_WRITE;
38149 }
38150#endif
38151 }
38152 }
38153
38154#if SQLITE_MAX_MMAP_SIZE>0
38155 if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
38156 int rc;
38157 if( pFile->szChunk<=0 ){
38158 if( robust_ftruncate(h: pFile->h, sz: nByte) ){
38159 storeLastErrno(pFile, errno);
38160 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
38161 }
38162 }
38163
38164 rc = unixMapfile(pFd: pFile, nByte);
38165 return rc;
38166 }
38167#endif
38168
38169 return SQLITE_OK;
38170}
38171
38172/*
38173** If *pArg is initially negative then this is a query. Set *pArg to
38174** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
38175**
38176** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
38177*/
38178static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){
38179 if( *pArg<0 ){
38180 *pArg = (pFile->ctrlFlags & mask)!=0;
38181 }else if( (*pArg)==0 ){
38182 pFile->ctrlFlags &= ~mask;
38183 }else{
38184 pFile->ctrlFlags |= mask;
38185 }
38186}
38187
38188/* Forward declaration */
38189static int unixGetTempname(int nBuf, char *zBuf);
38190#ifndef SQLITE_OMIT_WAL
38191 static int unixFcntlExternalReader(unixFile*, int*);
38192#endif
38193
38194/*
38195** Information and control of an open file handle.
38196*/
38197static int unixFileControl(sqlite3_file *id, int op, void *pArg){
38198 unixFile *pFile = (unixFile*)id;
38199 switch( op ){
38200#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
38201 case SQLITE_FCNTL_BEGIN_ATOMIC_WRITE: {
38202 int rc = osIoctl(pFile->h, F2FS_IOC_START_ATOMIC_WRITE);
38203 return rc ? SQLITE_IOERR_BEGIN_ATOMIC : SQLITE_OK;
38204 }
38205 case SQLITE_FCNTL_COMMIT_ATOMIC_WRITE: {
38206 int rc = osIoctl(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE);
38207 return rc ? SQLITE_IOERR_COMMIT_ATOMIC : SQLITE_OK;
38208 }
38209 case SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE: {
38210 int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE);
38211 return rc ? SQLITE_IOERR_ROLLBACK_ATOMIC : SQLITE_OK;
38212 }
38213#endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
38214
38215 case SQLITE_FCNTL_LOCKSTATE: {
38216 *(int*)pArg = pFile->eFileLock;
38217 return SQLITE_OK;
38218 }
38219 case SQLITE_FCNTL_LAST_ERRNO: {
38220 *(int*)pArg = pFile->lastErrno;
38221 return SQLITE_OK;
38222 }
38223 case SQLITE_FCNTL_CHUNK_SIZE: {
38224 pFile->szChunk = *(int *)pArg;
38225 return SQLITE_OK;
38226 }
38227 case SQLITE_FCNTL_SIZE_HINT: {
38228 int rc;
38229 SimulateIOErrorBenign(1);
38230 rc = fcntlSizeHint(pFile, nByte: *(i64 *)pArg);
38231 SimulateIOErrorBenign(0);
38232 return rc;
38233 }
38234 case SQLITE_FCNTL_PERSIST_WAL: {
38235 unixModeBit(pFile, UNIXFILE_PERSIST_WAL, pArg: (int*)pArg);
38236 return SQLITE_OK;
38237 }
38238 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
38239 unixModeBit(pFile, UNIXFILE_PSOW, pArg: (int*)pArg);
38240 return SQLITE_OK;
38241 }
38242 case SQLITE_FCNTL_VFSNAME: {
38243 *(char**)pArg = sqlite3_mprintf(zFormat: "%s", pFile->pVfs->zName);
38244 return SQLITE_OK;
38245 }
38246 case SQLITE_FCNTL_TEMPFILENAME: {
38247 char *zTFile = sqlite3_malloc64( n: pFile->pVfs->mxPathname );
38248 if( zTFile ){
38249 unixGetTempname(nBuf: pFile->pVfs->mxPathname, zBuf: zTFile);
38250 *(char**)pArg = zTFile;
38251 }
38252 return SQLITE_OK;
38253 }
38254 case SQLITE_FCNTL_HAS_MOVED: {
38255 *(int*)pArg = fileHasMoved(pFile);
38256 return SQLITE_OK;
38257 }
38258#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
38259 case SQLITE_FCNTL_LOCK_TIMEOUT: {
38260 int iOld = pFile->iBusyTimeout;
38261 pFile->iBusyTimeout = *(int*)pArg;
38262 *(int*)pArg = iOld;
38263 return SQLITE_OK;
38264 }
38265#endif
38266#if SQLITE_MAX_MMAP_SIZE>0
38267 case SQLITE_FCNTL_MMAP_SIZE: {
38268 i64 newLimit = *(i64*)pArg;
38269 int rc = SQLITE_OK;
38270 if( newLimit>sqlite3GlobalConfig.mxMmap ){
38271 newLimit = sqlite3GlobalConfig.mxMmap;
38272 }
38273
38274 /* The value of newLimit may be eventually cast to (size_t) and passed
38275 ** to mmap(). Restrict its value to 2GB if (size_t) is not at least a
38276 ** 64-bit type. */
38277 if( newLimit>0 && sizeof(size_t)<8 ){
38278 newLimit = (newLimit & 0x7FFFFFFF);
38279 }
38280
38281 *(i64*)pArg = pFile->mmapSizeMax;
38282 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
38283 pFile->mmapSizeMax = newLimit;
38284 if( pFile->mmapSize>0 ){
38285 unixUnmapfile(pFd: pFile);
38286 rc = unixMapfile(pFd: pFile, nByte: -1);
38287 }
38288 }
38289 return rc;
38290 }
38291#endif
38292#ifdef SQLITE_DEBUG
38293 /* The pager calls this method to signal that it has done
38294 ** a rollback and that the database is therefore unchanged and
38295 ** it hence it is OK for the transaction change counter to be
38296 ** unchanged.
38297 */
38298 case SQLITE_FCNTL_DB_UNCHANGED: {
38299 ((unixFile*)id)->dbUpdate = 0;
38300 return SQLITE_OK;
38301 }
38302#endif
38303#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
38304 case SQLITE_FCNTL_SET_LOCKPROXYFILE:
38305 case SQLITE_FCNTL_GET_LOCKPROXYFILE: {
38306 return proxyFileControl(id,op,pArg);
38307 }
38308#endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
38309
38310 case SQLITE_FCNTL_EXTERNAL_READER: {
38311#ifndef SQLITE_OMIT_WAL
38312 return unixFcntlExternalReader((unixFile*)id, (int*)pArg);
38313#else
38314 *(int*)pArg = 0;
38315 return SQLITE_OK;
38316#endif
38317 }
38318 }
38319 return SQLITE_NOTFOUND;
38320}
38321
38322/*
38323** If pFd->sectorSize is non-zero when this function is called, it is a
38324** no-op. Otherwise, the values of pFd->sectorSize and
38325** pFd->deviceCharacteristics are set according to the file-system
38326** characteristics.
38327**
38328** There are two versions of this function. One for QNX and one for all
38329** other systems.
38330*/
38331#ifndef __QNXNTO__
38332static void setDeviceCharacteristics(unixFile *pFd){
38333 assert( pFd->deviceCharacteristics==0 || pFd->sectorSize!=0 );
38334 if( pFd->sectorSize==0 ){
38335#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
38336 int res;
38337 u32 f = 0;
38338
38339 /* Check for support for F2FS atomic batch writes. */
38340 res = osIoctl(pFd->h, F2FS_IOC_GET_FEATURES, &f);
38341 if( res==0 && (f & F2FS_FEATURE_ATOMIC_WRITE) ){
38342 pFd->deviceCharacteristics = SQLITE_IOCAP_BATCH_ATOMIC;
38343 }
38344#endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
38345
38346 /* Set the POWERSAFE_OVERWRITE flag if requested. */
38347 if( pFd->ctrlFlags & UNIXFILE_PSOW ){
38348 pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
38349 }
38350
38351 pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
38352 }
38353}
38354#else
38355#include <sys/dcmd_blk.h>
38356#include <sys/statvfs.h>
38357static void setDeviceCharacteristics(unixFile *pFile){
38358 if( pFile->sectorSize == 0 ){
38359 struct statvfs fsInfo;
38360
38361 /* Set defaults for non-supported filesystems */
38362 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
38363 pFile->deviceCharacteristics = 0;
38364 if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
38365 return;
38366 }
38367
38368 if( !strcmp(fsInfo.f_basetype, "tmp") ) {
38369 pFile->sectorSize = fsInfo.f_bsize;
38370 pFile->deviceCharacteristics =
38371 SQLITE_IOCAP_ATOMIC4K | /* All ram filesystem writes are atomic */
38372 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
38373 ** the write succeeds */
38374 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
38375 ** so it is ordered */
38376 0;
38377 }else if( strstr(fsInfo.f_basetype, "etfs") ){
38378 pFile->sectorSize = fsInfo.f_bsize;
38379 pFile->deviceCharacteristics =
38380 /* etfs cluster size writes are atomic */
38381 (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) |
38382 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
38383 ** the write succeeds */
38384 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
38385 ** so it is ordered */
38386 0;
38387 }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){
38388 pFile->sectorSize = fsInfo.f_bsize;
38389 pFile->deviceCharacteristics =
38390 SQLITE_IOCAP_ATOMIC | /* All filesystem writes are atomic */
38391 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
38392 ** the write succeeds */
38393 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
38394 ** so it is ordered */
38395 0;
38396 }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){
38397 pFile->sectorSize = fsInfo.f_bsize;
38398 pFile->deviceCharacteristics =
38399 /* full bitset of atomics from max sector size and smaller */
38400 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
38401 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
38402 ** so it is ordered */
38403 0;
38404 }else if( strstr(fsInfo.f_basetype, "dos") ){
38405 pFile->sectorSize = fsInfo.f_bsize;
38406 pFile->deviceCharacteristics =
38407 /* full bitset of atomics from max sector size and smaller */
38408 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
38409 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
38410 ** so it is ordered */
38411 0;
38412 }else{
38413 pFile->deviceCharacteristics =
38414 SQLITE_IOCAP_ATOMIC512 | /* blocks are atomic */
38415 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
38416 ** the write succeeds */
38417 0;
38418 }
38419 }
38420 /* Last chance verification. If the sector size isn't a multiple of 512
38421 ** then it isn't valid.*/
38422 if( pFile->sectorSize % 512 != 0 ){
38423 pFile->deviceCharacteristics = 0;
38424 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
38425 }
38426}
38427#endif
38428
38429/*
38430** Return the sector size in bytes of the underlying block device for
38431** the specified file. This is almost always 512 bytes, but may be
38432** larger for some devices.
38433**
38434** SQLite code assumes this function cannot fail. It also assumes that
38435** if two files are created in the same file-system directory (i.e.
38436** a database and its journal file) that the sector size will be the
38437** same for both.
38438*/
38439static int unixSectorSize(sqlite3_file *id){
38440 unixFile *pFd = (unixFile*)id;
38441 setDeviceCharacteristics(pFd);
38442 return pFd->sectorSize;
38443}
38444
38445/*
38446** Return the device characteristics for the file.
38447**
38448** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default.
38449** However, that choice is controversial since technically the underlying
38450** file system does not always provide powersafe overwrites. (In other
38451** words, after a power-loss event, parts of the file that were never
38452** written might end up being altered.) However, non-PSOW behavior is very,
38453** very rare. And asserting PSOW makes a large reduction in the amount
38454** of required I/O for journaling, since a lot of padding is eliminated.
38455** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
38456** available to turn it off and URI query parameter available to turn it off.
38457*/
38458static int unixDeviceCharacteristics(sqlite3_file *id){
38459 unixFile *pFd = (unixFile*)id;
38460 setDeviceCharacteristics(pFd);
38461 return pFd->deviceCharacteristics;
38462}
38463
38464#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
38465
38466/*
38467** Return the system page size.
38468**
38469** This function should not be called directly by other code in this file.
38470** Instead, it should be called via macro osGetpagesize().
38471*/
38472static int unixGetpagesize(void){
38473#if OS_VXWORKS
38474 return 1024;
38475#elif defined(_BSD_SOURCE)
38476 return getpagesize();
38477#else
38478 return (int)sysconf(_SC_PAGESIZE);
38479#endif
38480}
38481
38482#endif /* !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 */
38483
38484#ifndef SQLITE_OMIT_WAL
38485
38486/*
38487** Object used to represent an shared memory buffer.
38488**
38489** When multiple threads all reference the same wal-index, each thread
38490** has its own unixShm object, but they all point to a single instance
38491** of this unixShmNode object. In other words, each wal-index is opened
38492** only once per process.
38493**
38494** Each unixShmNode object is connected to a single unixInodeInfo object.
38495** We could coalesce this object into unixInodeInfo, but that would mean
38496** every open file that does not use shared memory (in other words, most
38497** open files) would have to carry around this extra information. So
38498** the unixInodeInfo object contains a pointer to this unixShmNode object
38499** and the unixShmNode object is created only when needed.
38500**
38501** unixMutexHeld() must be true when creating or destroying
38502** this object or while reading or writing the following fields:
38503**
38504** nRef
38505**
38506** The following fields are read-only after the object is created:
38507**
38508** hShm
38509** zFilename
38510**
38511** Either unixShmNode.pShmMutex must be held or unixShmNode.nRef==0 and
38512** unixMutexHeld() is true when reading or writing any other field
38513** in this structure.
38514*/
38515struct unixShmNode {
38516 unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */
38517 sqlite3_mutex *pShmMutex; /* Mutex to access this object */
38518 char *zFilename; /* Name of the mmapped file */
38519 int hShm; /* Open file descriptor */
38520 int szRegion; /* Size of shared-memory regions */
38521 u16 nRegion; /* Size of array apRegion */
38522 u8 isReadonly; /* True if read-only */
38523 u8 isUnlocked; /* True if no DMS lock held */
38524 char **apRegion; /* Array of mapped shared-memory regions */
38525 int nRef; /* Number of unixShm objects pointing to this */
38526 unixShm *pFirst; /* All unixShm objects pointing to this */
38527 int aLock[SQLITE_SHM_NLOCK]; /* # shared locks on slot, -1==excl lock */
38528#ifdef SQLITE_DEBUG
38529 u8 exclMask; /* Mask of exclusive locks held */
38530 u8 sharedMask; /* Mask of shared locks held */
38531 u8 nextShmId; /* Next available unixShm.id value */
38532#endif
38533};
38534
38535/*
38536** Structure used internally by this VFS to record the state of an
38537** open shared memory connection.
38538**
38539** The following fields are initialized when this object is created and
38540** are read-only thereafter:
38541**
38542** unixShm.pShmNode
38543** unixShm.id
38544**
38545** All other fields are read/write. The unixShm.pShmNode->pShmMutex must
38546** be held while accessing any read/write fields.
38547*/
38548struct unixShm {
38549 unixShmNode *pShmNode; /* The underlying unixShmNode object */
38550 unixShm *pNext; /* Next unixShm with the same unixShmNode */
38551 u8 hasMutex; /* True if holding the unixShmNode->pShmMutex */
38552 u8 id; /* Id of this connection within its unixShmNode */
38553 u16 sharedMask; /* Mask of shared locks held */
38554 u16 exclMask; /* Mask of exclusive locks held */
38555};
38556
38557/*
38558** Constants used for locking
38559*/
38560#define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
38561#define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
38562
38563/*
38564** Use F_GETLK to check whether or not there are any readers with open
38565** wal-mode transactions in other processes on database file pFile. If
38566** no error occurs, return SQLITE_OK and set (*piOut) to 1 if there are
38567** such transactions, or 0 otherwise. If an error occurs, return an
38568** SQLite error code. The final value of *piOut is undefined in this
38569** case.
38570*/
38571static int unixFcntlExternalReader(unixFile *pFile, int *piOut){
38572 int rc = SQLITE_OK;
38573 *piOut = 0;
38574 if( pFile->pShm){
38575 unixShmNode *pShmNode = pFile->pShm->pShmNode;
38576 struct flock f;
38577
38578 memset(s: &f, c: 0, n: sizeof(f));
38579 f.l_type = F_WRLCK;
38580 f.l_whence = SEEK_SET;
38581 f.l_start = UNIX_SHM_BASE + 3;
38582 f.l_len = SQLITE_SHM_NLOCK - 3;
38583
38584 sqlite3_mutex_enter(p: pShmNode->pShmMutex);
38585 if( osFcntl(pShmNode->hShm, F_GETLK, &f)<0 ){
38586 rc = SQLITE_IOERR_LOCK;
38587 }else{
38588 *piOut = (f.l_type!=F_UNLCK);
38589 }
38590 sqlite3_mutex_leave(p: pShmNode->pShmMutex);
38591 }
38592
38593 return rc;
38594}
38595
38596
38597/*
38598** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
38599**
38600** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
38601** otherwise.
38602*/
38603static int unixShmSystemLock(
38604 unixFile *pFile, /* Open connection to the WAL file */
38605 int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */
38606 int ofst, /* First byte of the locking range */
38607 int n /* Number of bytes to lock */
38608){
38609 unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */
38610 struct flock f; /* The posix advisory locking structure */
38611 int rc = SQLITE_OK; /* Result code form fcntl() */
38612
38613 /* Access to the unixShmNode object is serialized by the caller */
38614 pShmNode = pFile->pInode->pShmNode;
38615 assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) );
38616 assert( pShmNode->nRef>0 || unixMutexHeld() );
38617
38618 /* Shared locks never span more than one byte */
38619 assert( n==1 || lockType!=F_RDLCK );
38620
38621 /* Locks are within range */
38622 assert( n>=1 && n<=SQLITE_SHM_NLOCK );
38623
38624 if( pShmNode->hShm>=0 ){
38625 int res;
38626 /* Initialize the locking parameters */
38627 f.l_type = lockType;
38628 f.l_whence = SEEK_SET;
38629 f.l_start = ofst;
38630 f.l_len = n;
38631 res = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile);
38632 if( res==-1 ){
38633#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
38634 rc = (pFile->iBusyTimeout ? SQLITE_BUSY_TIMEOUT : SQLITE_BUSY);
38635#else
38636 rc = SQLITE_BUSY;
38637#endif
38638 }
38639 }
38640
38641 /* Update the global lock state and do debug tracing */
38642#ifdef SQLITE_DEBUG
38643 { u16 mask;
38644 OSTRACE(("SHM-LOCK "));
38645 mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst);
38646 if( rc==SQLITE_OK ){
38647 if( lockType==F_UNLCK ){
38648 OSTRACE(("unlock %d ok", ofst));
38649 pShmNode->exclMask &= ~mask;
38650 pShmNode->sharedMask &= ~mask;
38651 }else if( lockType==F_RDLCK ){
38652 OSTRACE(("read-lock %d ok", ofst));
38653 pShmNode->exclMask &= ~mask;
38654 pShmNode->sharedMask |= mask;
38655 }else{
38656 assert( lockType==F_WRLCK );
38657 OSTRACE(("write-lock %d ok", ofst));
38658 pShmNode->exclMask |= mask;
38659 pShmNode->sharedMask &= ~mask;
38660 }
38661 }else{
38662 if( lockType==F_UNLCK ){
38663 OSTRACE(("unlock %d failed", ofst));
38664 }else if( lockType==F_RDLCK ){
38665 OSTRACE(("read-lock failed"));
38666 }else{
38667 assert( lockType==F_WRLCK );
38668 OSTRACE(("write-lock %d failed", ofst));
38669 }
38670 }
38671 OSTRACE((" - afterwards %03x,%03x\n",
38672 pShmNode->sharedMask, pShmNode->exclMask));
38673 }
38674#endif
38675
38676 return rc;
38677}
38678
38679/*
38680** Return the minimum number of 32KB shm regions that should be mapped at
38681** a time, assuming that each mapping must be an integer multiple of the
38682** current system page-size.
38683**
38684** Usually, this is 1. The exception seems to be systems that are configured
38685** to use 64KB pages - in this case each mapping must cover at least two
38686** shm regions.
38687*/
38688static int unixShmRegionPerMap(void){
38689 int shmsz = 32*1024; /* SHM region size */
38690 int pgsz = osGetpagesize(); /* System page size */
38691 assert( ((pgsz-1)&pgsz)==0 ); /* Page size must be a power of 2 */
38692 if( pgsz<shmsz ) return 1;
38693 return pgsz/shmsz;
38694}
38695
38696/*
38697** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0.
38698**
38699** This is not a VFS shared-memory method; it is a utility function called
38700** by VFS shared-memory methods.
38701*/
38702static void unixShmPurge(unixFile *pFd){
38703 unixShmNode *p = pFd->pInode->pShmNode;
38704 assert( unixMutexHeld() );
38705 if( p && ALWAYS(p->nRef==0) ){
38706 int nShmPerMap = unixShmRegionPerMap();
38707 int i;
38708 assert( p->pInode==pFd->pInode );
38709 sqlite3_mutex_free(p: p->pShmMutex);
38710 for(i=0; i<p->nRegion; i+=nShmPerMap){
38711 if( p->hShm>=0 ){
38712 osMunmap(p->apRegion[i], p->szRegion);
38713 }else{
38714 sqlite3_free(p: p->apRegion[i]);
38715 }
38716 }
38717 sqlite3_free(p: p->apRegion);
38718 if( p->hShm>=0 ){
38719 robust_close(pFile: pFd, h: p->hShm, __LINE__);
38720 p->hShm = -1;
38721 }
38722 p->pInode->pShmNode = 0;
38723 sqlite3_free(p);
38724 }
38725}
38726
38727/*
38728** The DMS lock has not yet been taken on shm file pShmNode. Attempt to
38729** take it now. Return SQLITE_OK if successful, or an SQLite error
38730** code otherwise.
38731**
38732** If the DMS cannot be locked because this is a readonly_shm=1
38733** connection and no other process already holds a lock, return
38734** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
38735*/
38736static int unixLockSharedMemory(unixFile *pDbFd, unixShmNode *pShmNode){
38737 struct flock lock;
38738 int rc = SQLITE_OK;
38739
38740 /* Use F_GETLK to determine the locks other processes are holding
38741 ** on the DMS byte. If it indicates that another process is holding
38742 ** a SHARED lock, then this process may also take a SHARED lock
38743 ** and proceed with opening the *-shm file.
38744 **
38745 ** Or, if no other process is holding any lock, then this process
38746 ** is the first to open it. In this case take an EXCLUSIVE lock on the
38747 ** DMS byte and truncate the *-shm file to zero bytes in size. Then
38748 ** downgrade to a SHARED lock on the DMS byte.
38749 **
38750 ** If another process is holding an EXCLUSIVE lock on the DMS byte,
38751 ** return SQLITE_BUSY to the caller (it will try again). An earlier
38752 ** version of this code attempted the SHARED lock at this point. But
38753 ** this introduced a subtle race condition: if the process holding
38754 ** EXCLUSIVE failed just before truncating the *-shm file, then this
38755 ** process might open and use the *-shm file without truncating it.
38756 ** And if the *-shm file has been corrupted by a power failure or
38757 ** system crash, the database itself may also become corrupt. */
38758 lock.l_whence = SEEK_SET;
38759 lock.l_start = UNIX_SHM_DMS;
38760 lock.l_len = 1;
38761 lock.l_type = F_WRLCK;
38762 if( osFcntl(pShmNode->hShm, F_GETLK, &lock)!=0 ) {
38763 rc = SQLITE_IOERR_LOCK;
38764 }else if( lock.l_type==F_UNLCK ){
38765 if( pShmNode->isReadonly ){
38766 pShmNode->isUnlocked = 1;
38767 rc = SQLITE_READONLY_CANTINIT;
38768 }else{
38769 rc = unixShmSystemLock(pFile: pDbFd, F_WRLCK, UNIX_SHM_DMS, n: 1);
38770 /* The first connection to attach must truncate the -shm file. We
38771 ** truncate to 3 bytes (an arbitrary small number, less than the
38772 ** -shm header size) rather than 0 as a system debugging aid, to
38773 ** help detect if a -shm file truncation is legitimate or is the work
38774 ** or a rogue process. */
38775 if( rc==SQLITE_OK && robust_ftruncate(h: pShmNode->hShm, sz: 3) ){
38776 rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename);
38777 }
38778 }
38779 }else if( lock.l_type==F_WRLCK ){
38780 rc = SQLITE_BUSY;
38781 }
38782
38783 if( rc==SQLITE_OK ){
38784 assert( lock.l_type==F_UNLCK || lock.l_type==F_RDLCK );
38785 rc = unixShmSystemLock(pFile: pDbFd, F_RDLCK, UNIX_SHM_DMS, n: 1);
38786 }
38787 return rc;
38788}
38789
38790/*
38791** Open a shared-memory area associated with open database file pDbFd.
38792** This particular implementation uses mmapped files.
38793**
38794** The file used to implement shared-memory is in the same directory
38795** as the open database file and has the same name as the open database
38796** file with the "-shm" suffix added. For example, if the database file
38797** is "/home/user1/config.db" then the file that is created and mmapped
38798** for shared memory will be called "/home/user1/config.db-shm".
38799**
38800** Another approach to is to use files in /dev/shm or /dev/tmp or an
38801** some other tmpfs mount. But if a file in a different directory
38802** from the database file is used, then differing access permissions
38803** or a chroot() might cause two different processes on the same
38804** database to end up using different files for shared memory -
38805** meaning that their memory would not really be shared - resulting
38806** in database corruption. Nevertheless, this tmpfs file usage
38807** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
38808** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time
38809** option results in an incompatible build of SQLite; builds of SQLite
38810** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the
38811** same database file at the same time, database corruption will likely
38812** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
38813** "unsupported" and may go away in a future SQLite release.
38814**
38815** When opening a new shared-memory file, if no other instances of that
38816** file are currently open, in this process or in other processes, then
38817** the file must be truncated to zero length or have its header cleared.
38818**
38819** If the original database file (pDbFd) is using the "unix-excl" VFS
38820** that means that an exclusive lock is held on the database file and
38821** that no other processes are able to read or write the database. In
38822** that case, we do not really need shared memory. No shared memory
38823** file is created. The shared memory will be simulated with heap memory.
38824*/
38825static int unixOpenSharedMemory(unixFile *pDbFd){
38826 struct unixShm *p = 0; /* The connection to be opened */
38827 struct unixShmNode *pShmNode; /* The underlying mmapped file */
38828 int rc = SQLITE_OK; /* Result code */
38829 unixInodeInfo *pInode; /* The inode of fd */
38830 char *zShm; /* Name of the file used for SHM */
38831 int nShmFilename; /* Size of the SHM filename in bytes */
38832
38833 /* Allocate space for the new unixShm object. */
38834 p = sqlite3_malloc64( n: sizeof(*p) );
38835 if( p==0 ) return SQLITE_NOMEM_BKPT;
38836 memset(s: p, c: 0, n: sizeof(*p));
38837 assert( pDbFd->pShm==0 );
38838
38839 /* Check to see if a unixShmNode object already exists. Reuse an existing
38840 ** one if present. Create a new one if necessary.
38841 */
38842 assert( unixFileMutexNotheld(pDbFd) );
38843 unixEnterMutex();
38844 pInode = pDbFd->pInode;
38845 pShmNode = pInode->pShmNode;
38846 if( pShmNode==0 ){
38847 struct stat sStat; /* fstat() info for database file */
38848#ifndef SQLITE_SHM_DIRECTORY
38849 const char *zBasePath = pDbFd->zPath;
38850#endif
38851
38852 /* Call fstat() to figure out the permissions on the database file. If
38853 ** a new *-shm file is created, an attempt will be made to create it
38854 ** with the same permissions.
38855 */
38856 if( osFstat(pDbFd->h, &sStat) ){
38857 rc = SQLITE_IOERR_FSTAT;
38858 goto shm_open_err;
38859 }
38860
38861#ifdef SQLITE_SHM_DIRECTORY
38862 nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31;
38863#else
38864 nShmFilename = 6 + (int)strlen(s: zBasePath);
38865#endif
38866 pShmNode = sqlite3_malloc64( n: sizeof(*pShmNode) + nShmFilename );
38867 if( pShmNode==0 ){
38868 rc = SQLITE_NOMEM_BKPT;
38869 goto shm_open_err;
38870 }
38871 memset(s: pShmNode, c: 0, n: sizeof(*pShmNode)+nShmFilename);
38872 zShm = pShmNode->zFilename = (char*)&pShmNode[1];
38873#ifdef SQLITE_SHM_DIRECTORY
38874 sqlite3_snprintf(nShmFilename, zShm,
38875 SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
38876 (u32)sStat.st_ino, (u32)sStat.st_dev);
38877#else
38878 sqlite3_snprintf(n: nShmFilename, zBuf: zShm, zFormat: "%s-shm", zBasePath);
38879 sqlite3FileSuffix3(pDbFd->zPath, zShm);
38880#endif
38881 pShmNode->hShm = -1;
38882 pDbFd->pInode->pShmNode = pShmNode;
38883 pShmNode->pInode = pDbFd->pInode;
38884 if( sqlite3GlobalConfig.bCoreMutex ){
38885 pShmNode->pShmMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
38886 if( pShmNode->pShmMutex==0 ){
38887 rc = SQLITE_NOMEM_BKPT;
38888 goto shm_open_err;
38889 }
38890 }
38891
38892 if( pInode->bProcessLock==0 ){
38893 if( 0==sqlite3_uri_boolean(zFile: pDbFd->zPath, zParam: "readonly_shm", bDefault: 0) ){
38894 pShmNode->hShm = robust_open(z: zShm, O_RDWR|O_CREAT|O_NOFOLLOW,
38895 m: (sStat.st_mode&0777));
38896 }
38897 if( pShmNode->hShm<0 ){
38898 pShmNode->hShm = robust_open(z: zShm, O_RDONLY|O_NOFOLLOW,
38899 m: (sStat.st_mode&0777));
38900 if( pShmNode->hShm<0 ){
38901 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShm);
38902 goto shm_open_err;
38903 }
38904 pShmNode->isReadonly = 1;
38905 }
38906
38907 /* If this process is running as root, make sure that the SHM file
38908 ** is owned by the same user that owns the original database. Otherwise,
38909 ** the original owner will not be able to connect.
38910 */
38911 robustFchown(fd: pShmNode->hShm, uid: sStat.st_uid, gid: sStat.st_gid);
38912
38913 rc = unixLockSharedMemory(pDbFd, pShmNode);
38914 if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err;
38915 }
38916 }
38917
38918 /* Make the new connection a child of the unixShmNode */
38919 p->pShmNode = pShmNode;
38920#ifdef SQLITE_DEBUG
38921 p->id = pShmNode->nextShmId++;
38922#endif
38923 pShmNode->nRef++;
38924 pDbFd->pShm = p;
38925 unixLeaveMutex();
38926
38927 /* The reference count on pShmNode has already been incremented under
38928 ** the cover of the unixEnterMutex() mutex and the pointer from the
38929 ** new (struct unixShm) object to the pShmNode has been set. All that is
38930 ** left to do is to link the new object into the linked list starting
38931 ** at pShmNode->pFirst. This must be done while holding the
38932 ** pShmNode->pShmMutex.
38933 */
38934 sqlite3_mutex_enter(p: pShmNode->pShmMutex);
38935 p->pNext = pShmNode->pFirst;
38936 pShmNode->pFirst = p;
38937 sqlite3_mutex_leave(p: pShmNode->pShmMutex);
38938 return rc;
38939
38940 /* Jump here on any error */
38941shm_open_err:
38942 unixShmPurge(pFd: pDbFd); /* This call frees pShmNode if required */
38943 sqlite3_free(p);
38944 unixLeaveMutex();
38945 return rc;
38946}
38947
38948/*
38949** This function is called to obtain a pointer to region iRegion of the
38950** shared-memory associated with the database file fd. Shared-memory regions
38951** are numbered starting from zero. Each shared-memory region is szRegion
38952** bytes in size.
38953**
38954** If an error occurs, an error code is returned and *pp is set to NULL.
38955**
38956** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
38957** region has not been allocated (by any client, including one running in a
38958** separate process), then *pp is set to NULL and SQLITE_OK returned. If
38959** bExtend is non-zero and the requested shared-memory region has not yet
38960** been allocated, it is allocated by this function.
38961**
38962** If the shared-memory region has already been allocated or is allocated by
38963** this call as described above, then it is mapped into this processes
38964** address space (if it is not already), *pp is set to point to the mapped
38965** memory and SQLITE_OK returned.
38966*/
38967static int unixShmMap(
38968 sqlite3_file *fd, /* Handle open on database file */
38969 int iRegion, /* Region to retrieve */
38970 int szRegion, /* Size of regions */
38971 int bExtend, /* True to extend file if necessary */
38972 void volatile **pp /* OUT: Mapped memory */
38973){
38974 unixFile *pDbFd = (unixFile*)fd;
38975 unixShm *p;
38976 unixShmNode *pShmNode;
38977 int rc = SQLITE_OK;
38978 int nShmPerMap = unixShmRegionPerMap();
38979 int nReqRegion;
38980
38981 /* If the shared-memory file has not yet been opened, open it now. */
38982 if( pDbFd->pShm==0 ){
38983 rc = unixOpenSharedMemory(pDbFd);
38984 if( rc!=SQLITE_OK ) return rc;
38985 }
38986
38987 p = pDbFd->pShm;
38988 pShmNode = p->pShmNode;
38989 sqlite3_mutex_enter(p: pShmNode->pShmMutex);
38990 if( pShmNode->isUnlocked ){
38991 rc = unixLockSharedMemory(pDbFd, pShmNode);
38992 if( rc!=SQLITE_OK ) goto shmpage_out;
38993 pShmNode->isUnlocked = 0;
38994 }
38995 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
38996 assert( pShmNode->pInode==pDbFd->pInode );
38997 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
38998 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );
38999
39000 /* Minimum number of regions required to be mapped. */
39001 nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap;
39002
39003 if( pShmNode->nRegion<nReqRegion ){
39004 char **apNew; /* New apRegion[] array */
39005 int nByte = nReqRegion*szRegion; /* Minimum required file size */
39006 struct stat sStat; /* Used by fstat() */
39007
39008 pShmNode->szRegion = szRegion;
39009
39010 if( pShmNode->hShm>=0 ){
39011 /* The requested region is not mapped into this processes address space.
39012 ** Check to see if it has been allocated (i.e. if the wal-index file is
39013 ** large enough to contain the requested region).
39014 */
39015 if( osFstat(pShmNode->hShm, &sStat) ){
39016 rc = SQLITE_IOERR_SHMSIZE;
39017 goto shmpage_out;
39018 }
39019
39020 if( sStat.st_size<nByte ){
39021 /* The requested memory region does not exist. If bExtend is set to
39022 ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
39023 */
39024 if( !bExtend ){
39025 goto shmpage_out;
39026 }
39027
39028 /* Alternatively, if bExtend is true, extend the file. Do this by
39029 ** writing a single byte to the end of each (OS) page being
39030 ** allocated or extended. Technically, we need only write to the
39031 ** last page in order to extend the file. But writing to all new
39032 ** pages forces the OS to allocate them immediately, which reduces
39033 ** the chances of SIGBUS while accessing the mapped region later on.
39034 */
39035 else{
39036 static const int pgsz = 4096;
39037 int iPg;
39038
39039 /* Write to the last byte of each newly allocated or extended page */
39040 assert( (nByte % pgsz)==0 );
39041 for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){
39042 int x = 0;
39043 if( seekAndWriteFd(fd: pShmNode->hShm, iOff: iPg*pgsz + pgsz-1,pBuf: "",nBuf: 1,piErrno: &x)!=1 ){
39044 const char *zFile = pShmNode->zFilename;
39045 rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile);
39046 goto shmpage_out;
39047 }
39048 }
39049 }
39050 }
39051 }
39052
39053 /* Map the requested memory region into this processes address space. */
39054 apNew = (char **)sqlite3_realloc(
39055 pOld: pShmNode->apRegion, n: nReqRegion*sizeof(char *)
39056 );
39057 if( !apNew ){
39058 rc = SQLITE_IOERR_NOMEM_BKPT;
39059 goto shmpage_out;
39060 }
39061 pShmNode->apRegion = apNew;
39062 while( pShmNode->nRegion<nReqRegion ){
39063 int nMap = szRegion*nShmPerMap;
39064 int i;
39065 void *pMem;
39066 if( pShmNode->hShm>=0 ){
39067 pMem = osMmap(0, nMap,
39068 pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE,
39069 MAP_SHARED, pShmNode->hShm, szRegion*(i64)pShmNode->nRegion
39070 );
39071 if( pMem==MAP_FAILED ){
39072 rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
39073 goto shmpage_out;
39074 }
39075 }else{
39076 pMem = sqlite3_malloc64(n: nMap);
39077 if( pMem==0 ){
39078 rc = SQLITE_NOMEM_BKPT;
39079 goto shmpage_out;
39080 }
39081 memset(s: pMem, c: 0, n: nMap);
39082 }
39083
39084 for(i=0; i<nShmPerMap; i++){
39085 pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
39086 }
39087 pShmNode->nRegion += nShmPerMap;
39088 }
39089 }
39090
39091shmpage_out:
39092 if( pShmNode->nRegion>iRegion ){
39093 *pp = pShmNode->apRegion[iRegion];
39094 }else{
39095 *pp = 0;
39096 }
39097 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
39098 sqlite3_mutex_leave(p: pShmNode->pShmMutex);
39099 return rc;
39100}
39101
39102/*
39103** Check that the pShmNode->aLock[] array comports with the locking bitmasks
39104** held by each client. Return true if it does, or false otherwise. This
39105** is to be used in an assert(). e.g.
39106**
39107** assert( assertLockingArrayOk(pShmNode) );
39108*/
39109#ifdef SQLITE_DEBUG
39110static int assertLockingArrayOk(unixShmNode *pShmNode){
39111 unixShm *pX;
39112 int aLock[SQLITE_SHM_NLOCK];
39113 assert( sqlite3_mutex_held(pShmNode->pShmMutex) );
39114
39115 memset(aLock, 0, sizeof(aLock));
39116 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
39117 int i;
39118 for(i=0; i<SQLITE_SHM_NLOCK; i++){
39119 if( pX->exclMask & (1<<i) ){
39120 assert( aLock[i]==0 );
39121 aLock[i] = -1;
39122 }else if( pX->sharedMask & (1<<i) ){
39123 assert( aLock[i]>=0 );
39124 aLock[i]++;
39125 }
39126 }
39127 }
39128
39129 assert( 0==memcmp(pShmNode->aLock, aLock, sizeof(aLock)) );
39130 return (memcmp(pShmNode->aLock, aLock, sizeof(aLock))==0);
39131}
39132#endif
39133
39134/*
39135** Change the lock state for a shared-memory segment.
39136**
39137** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
39138** different here than in posix. In xShmLock(), one can go from unlocked
39139** to shared and back or from unlocked to exclusive and back. But one may
39140** not go from shared to exclusive or from exclusive to shared.
39141*/
39142static int unixShmLock(
39143 sqlite3_file *fd, /* Database file holding the shared memory */
39144 int ofst, /* First lock to acquire or release */
39145 int n, /* Number of locks to acquire or release */
39146 int flags /* What to do with the lock */
39147){
39148 unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */
39149 unixShm *p = pDbFd->pShm; /* The shared memory being locked */
39150 unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */
39151 int rc = SQLITE_OK; /* Result code */
39152 u16 mask; /* Mask of locks to take or release */
39153 int *aLock = pShmNode->aLock;
39154
39155 assert( pShmNode==pDbFd->pInode->pShmNode );
39156 assert( pShmNode->pInode==pDbFd->pInode );
39157 assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
39158 assert( n>=1 );
39159 assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
39160 || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
39161 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
39162 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
39163 assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
39164 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
39165 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );
39166
39167 /* Check that, if this to be a blocking lock, no locks that occur later
39168 ** in the following list than the lock being obtained are already held:
39169 **
39170 ** 1. Checkpointer lock (ofst==1).
39171 ** 2. Write lock (ofst==0).
39172 ** 3. Read locks (ofst>=3 && ofst<SQLITE_SHM_NLOCK).
39173 **
39174 ** In other words, if this is a blocking lock, none of the locks that
39175 ** occur later in the above list than the lock being obtained may be
39176 ** held.
39177 **
39178 ** It is not permitted to block on the RECOVER lock.
39179 */
39180#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
39181 assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || (
39182 (ofst!=2) /* not RECOVER */
39183 && (ofst!=1 || (p->exclMask|p->sharedMask)==0)
39184 && (ofst!=0 || (p->exclMask|p->sharedMask)<3)
39185 && (ofst<3 || (p->exclMask|p->sharedMask)<(1<<ofst))
39186 ));
39187#endif
39188
39189 mask = (1<<(ofst+n)) - (1<<ofst);
39190 assert( n>1 || mask==(1<<ofst) );
39191 sqlite3_mutex_enter(p: pShmNode->pShmMutex);
39192 assert( assertLockingArrayOk(pShmNode) );
39193 if( flags & SQLITE_SHM_UNLOCK ){
39194 if( (p->exclMask|p->sharedMask) & mask ){
39195 int ii;
39196 int bUnlock = 1;
39197
39198 for(ii=ofst; ii<ofst+n; ii++){
39199 if( aLock[ii]>((p->sharedMask & (1<<ii)) ? 1 : 0) ){
39200 bUnlock = 0;
39201 }
39202 }
39203
39204 if( bUnlock ){
39205 rc = unixShmSystemLock(pFile: pDbFd, F_UNLCK, ofst: ofst+UNIX_SHM_BASE, n);
39206 if( rc==SQLITE_OK ){
39207 memset(s: &aLock[ofst], c: 0, n: sizeof(int)*n);
39208 }
39209 }else if( ALWAYS(p->sharedMask & (1<<ofst)) ){
39210 assert( n==1 && aLock[ofst]>1 );
39211 aLock[ofst]--;
39212 }
39213
39214 /* Undo the local locks */
39215 if( rc==SQLITE_OK ){
39216 p->exclMask &= ~mask;
39217 p->sharedMask &= ~mask;
39218 }
39219 }
39220 }else if( flags & SQLITE_SHM_SHARED ){
39221 assert( n==1 );
39222 assert( (p->exclMask & (1<<ofst))==0 );
39223 if( (p->sharedMask & mask)==0 ){
39224 if( aLock[ofst]<0 ){
39225 rc = SQLITE_BUSY;
39226 }else if( aLock[ofst]==0 ){
39227 rc = unixShmSystemLock(pFile: pDbFd, F_RDLCK, ofst: ofst+UNIX_SHM_BASE, n);
39228 }
39229
39230 /* Get the local shared locks */
39231 if( rc==SQLITE_OK ){
39232 p->sharedMask |= mask;
39233 aLock[ofst]++;
39234 }
39235 }
39236 }else{
39237 /* Make sure no sibling connections hold locks that will block this
39238 ** lock. If any do, return SQLITE_BUSY right away. */
39239 int ii;
39240 for(ii=ofst; ii<ofst+n; ii++){
39241 assert( (p->sharedMask & mask)==0 );
39242 if( ALWAYS((p->exclMask & (1<<ii))==0) && aLock[ii] ){
39243 rc = SQLITE_BUSY;
39244 break;
39245 }
39246 }
39247
39248 /* Get the exclusive locks at the system level. Then if successful
39249 ** also update the in-memory values. */
39250 if( rc==SQLITE_OK ){
39251 rc = unixShmSystemLock(pFile: pDbFd, F_WRLCK, ofst: ofst+UNIX_SHM_BASE, n);
39252 if( rc==SQLITE_OK ){
39253 assert( (p->sharedMask & mask)==0 );
39254 p->exclMask |= mask;
39255 for(ii=ofst; ii<ofst+n; ii++){
39256 aLock[ii] = -1;
39257 }
39258 }
39259 }
39260 }
39261 assert( assertLockingArrayOk(pShmNode) );
39262 sqlite3_mutex_leave(p: pShmNode->pShmMutex);
39263 OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
39264 p->id, osGetpid(0), p->sharedMask, p->exclMask));
39265 return rc;
39266}
39267
39268/*
39269** Implement a memory barrier or memory fence on shared memory.
39270**
39271** All loads and stores begun before the barrier must complete before
39272** any load or store begun after the barrier.
39273*/
39274static void unixShmBarrier(
39275 sqlite3_file *fd /* Database file holding the shared memory */
39276){
39277 UNUSED_PARAMETER(fd);
39278 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
39279 assert( fd->pMethods->xLock==nolockLock
39280 || unixFileMutexNotheld((unixFile*)fd)
39281 );
39282 unixEnterMutex(); /* Also mutex, for redundancy */
39283 unixLeaveMutex();
39284}
39285
39286/*
39287** Close a connection to shared-memory. Delete the underlying
39288** storage if deleteFlag is true.
39289**
39290** If there is no shared memory associated with the connection then this
39291** routine is a harmless no-op.
39292*/
39293static int unixShmUnmap(
39294 sqlite3_file *fd, /* The underlying database file */
39295 int deleteFlag /* Delete shared-memory if true */
39296){
39297 unixShm *p; /* The connection to be closed */
39298 unixShmNode *pShmNode; /* The underlying shared-memory file */
39299 unixShm **pp; /* For looping over sibling connections */
39300 unixFile *pDbFd; /* The underlying database file */
39301
39302 pDbFd = (unixFile*)fd;
39303 p = pDbFd->pShm;
39304 if( p==0 ) return SQLITE_OK;
39305 pShmNode = p->pShmNode;
39306
39307 assert( pShmNode==pDbFd->pInode->pShmNode );
39308 assert( pShmNode->pInode==pDbFd->pInode );
39309
39310 /* Remove connection p from the set of connections associated
39311 ** with pShmNode */
39312 sqlite3_mutex_enter(p: pShmNode->pShmMutex);
39313 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
39314 *pp = p->pNext;
39315
39316 /* Free the connection p */
39317 sqlite3_free(p);
39318 pDbFd->pShm = 0;
39319 sqlite3_mutex_leave(p: pShmNode->pShmMutex);
39320
39321 /* If pShmNode->nRef has reached 0, then close the underlying
39322 ** shared-memory file, too */
39323 assert( unixFileMutexNotheld(pDbFd) );
39324 unixEnterMutex();
39325 assert( pShmNode->nRef>0 );
39326 pShmNode->nRef--;
39327 if( pShmNode->nRef==0 ){
39328 if( deleteFlag && pShmNode->hShm>=0 ){
39329 osUnlink(pShmNode->zFilename);
39330 }
39331 unixShmPurge(pFd: pDbFd);
39332 }
39333 unixLeaveMutex();
39334
39335 return SQLITE_OK;
39336}
39337
39338
39339#else
39340# define unixShmMap 0
39341# define unixShmLock 0
39342# define unixShmBarrier 0
39343# define unixShmUnmap 0
39344#endif /* #ifndef SQLITE_OMIT_WAL */
39345
39346#if SQLITE_MAX_MMAP_SIZE>0
39347/*
39348** If it is currently memory mapped, unmap file pFd.
39349*/
39350static void unixUnmapfile(unixFile *pFd){
39351 assert( pFd->nFetchOut==0 );
39352 if( pFd->pMapRegion ){
39353 osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
39354 pFd->pMapRegion = 0;
39355 pFd->mmapSize = 0;
39356 pFd->mmapSizeActual = 0;
39357 }
39358}
39359
39360/*
39361** Attempt to set the size of the memory mapping maintained by file
39362** descriptor pFd to nNew bytes. Any existing mapping is discarded.
39363**
39364** If successful, this function sets the following variables:
39365**
39366** unixFile.pMapRegion
39367** unixFile.mmapSize
39368** unixFile.mmapSizeActual
39369**
39370** If unsuccessful, an error message is logged via sqlite3_log() and
39371** the three variables above are zeroed. In this case SQLite should
39372** continue accessing the database using the xRead() and xWrite()
39373** methods.
39374*/
39375static void unixRemapfile(
39376 unixFile *pFd, /* File descriptor object */
39377 i64 nNew /* Required mapping size */
39378){
39379 const char *zErr = "mmap";
39380 int h = pFd->h; /* File descriptor open on db file */
39381 u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */
39382 i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */
39383 u8 *pNew = 0; /* Location of new mapping */
39384 int flags = PROT_READ; /* Flags to pass to mmap() */
39385
39386 assert( pFd->nFetchOut==0 );
39387 assert( nNew>pFd->mmapSize );
39388 assert( nNew<=pFd->mmapSizeMax );
39389 assert( nNew>0 );
39390 assert( pFd->mmapSizeActual>=pFd->mmapSize );
39391 assert( MAP_FAILED!=0 );
39392
39393#ifdef SQLITE_MMAP_READWRITE
39394 if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
39395#endif
39396
39397 if( pOrig ){
39398#if HAVE_MREMAP
39399 i64 nReuse = pFd->mmapSize;
39400#else
39401 const int szSyspage = osGetpagesize();
39402 i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
39403#endif
39404 u8 *pReq = &pOrig[nReuse];
39405
39406 /* Unmap any pages of the existing mapping that cannot be reused. */
39407 if( nReuse!=nOrig ){
39408 osMunmap(pReq, nOrig-nReuse);
39409 }
39410
39411#if HAVE_MREMAP
39412 pNew = osMremap(pOrig, nReuse, nNew, MREMAP_MAYMOVE);
39413 zErr = "mremap";
39414#else
39415 pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse);
39416 if( pNew!=MAP_FAILED ){
39417 if( pNew!=pReq ){
39418 osMunmap(pNew, nNew - nReuse);
39419 pNew = 0;
39420 }else{
39421 pNew = pOrig;
39422 }
39423 }
39424#endif
39425
39426 /* The attempt to extend the existing mapping failed. Free it. */
39427 if( pNew==MAP_FAILED || pNew==0 ){
39428 osMunmap(pOrig, nReuse);
39429 }
39430 }
39431
39432 /* If pNew is still NULL, try to create an entirely new mapping. */
39433 if( pNew==0 ){
39434 pNew = osMmap(0, nNew, flags, MAP_SHARED, h, 0);
39435 }
39436
39437 if( pNew==MAP_FAILED ){
39438 pNew = 0;
39439 nNew = 0;
39440 unixLogError(SQLITE_OK, zErr, pFd->zPath);
39441
39442 /* If the mmap() above failed, assume that all subsequent mmap() calls
39443 ** will probably fail too. Fall back to using xRead/xWrite exclusively
39444 ** in this case. */
39445 pFd->mmapSizeMax = 0;
39446 }
39447 pFd->pMapRegion = (void *)pNew;
39448 pFd->mmapSize = pFd->mmapSizeActual = nNew;
39449}
39450
39451/*
39452** Memory map or remap the file opened by file-descriptor pFd (if the file
39453** is already mapped, the existing mapping is replaced by the new). Or, if
39454** there already exists a mapping for this file, and there are still
39455** outstanding xFetch() references to it, this function is a no-op.
39456**
39457** If parameter nByte is non-negative, then it is the requested size of
39458** the mapping to create. Otherwise, if nByte is less than zero, then the
39459** requested size is the size of the file on disk. The actual size of the
39460** created mapping is either the requested size or the value configured
39461** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller.
39462**
39463** SQLITE_OK is returned if no error occurs (even if the mapping is not
39464** recreated as a result of outstanding references) or an SQLite error
39465** code otherwise.
39466*/
39467static int unixMapfile(unixFile *pFd, i64 nMap){
39468 assert( nMap>=0 || pFd->nFetchOut==0 );
39469 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
39470 if( pFd->nFetchOut>0 ) return SQLITE_OK;
39471
39472 if( nMap<0 ){
39473 struct stat statbuf; /* Low-level file information */
39474 if( osFstat(pFd->h, &statbuf) ){
39475 return SQLITE_IOERR_FSTAT;
39476 }
39477 nMap = statbuf.st_size;
39478 }
39479 if( nMap>pFd->mmapSizeMax ){
39480 nMap = pFd->mmapSizeMax;
39481 }
39482
39483 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
39484 if( nMap!=pFd->mmapSize ){
39485 unixRemapfile(pFd, nNew: nMap);
39486 }
39487
39488 return SQLITE_OK;
39489}
39490#endif /* SQLITE_MAX_MMAP_SIZE>0 */
39491
39492/*
39493** If possible, return a pointer to a mapping of file fd starting at offset
39494** iOff. The mapping must be valid for at least nAmt bytes.
39495**
39496** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
39497** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
39498** Finally, if an error does occur, return an SQLite error code. The final
39499** value of *pp is undefined in this case.
39500**
39501** If this function does return a pointer, the caller must eventually
39502** release the reference by calling unixUnfetch().
39503*/
39504static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
39505#if SQLITE_MAX_MMAP_SIZE>0
39506 unixFile *pFd = (unixFile *)fd; /* The underlying database file */
39507#endif
39508 *pp = 0;
39509
39510#if SQLITE_MAX_MMAP_SIZE>0
39511 if( pFd->mmapSizeMax>0 ){
39512 if( pFd->pMapRegion==0 ){
39513 int rc = unixMapfile(pFd, nMap: -1);
39514 if( rc!=SQLITE_OK ) return rc;
39515 }
39516 if( pFd->mmapSize >= iOff+nAmt ){
39517 *pp = &((u8 *)pFd->pMapRegion)[iOff];
39518 pFd->nFetchOut++;
39519 }
39520 }
39521#endif
39522 return SQLITE_OK;
39523}
39524
39525/*
39526** If the third argument is non-NULL, then this function releases a
39527** reference obtained by an earlier call to unixFetch(). The second
39528** argument passed to this function must be the same as the corresponding
39529** argument that was passed to the unixFetch() invocation.
39530**
39531** Or, if the third argument is NULL, then this function is being called
39532** to inform the VFS layer that, according to POSIX, any existing mapping
39533** may now be invalid and should be unmapped.
39534*/
39535static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
39536#if SQLITE_MAX_MMAP_SIZE>0
39537 unixFile *pFd = (unixFile *)fd; /* The underlying database file */
39538 UNUSED_PARAMETER(iOff);
39539
39540 /* If p==0 (unmap the entire file) then there must be no outstanding
39541 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
39542 ** then there must be at least one outstanding. */
39543 assert( (p==0)==(pFd->nFetchOut==0) );
39544
39545 /* If p!=0, it must match the iOff value. */
39546 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
39547
39548 if( p ){
39549 pFd->nFetchOut--;
39550 }else{
39551 unixUnmapfile(pFd);
39552 }
39553
39554 assert( pFd->nFetchOut>=0 );
39555#else
39556 UNUSED_PARAMETER(fd);
39557 UNUSED_PARAMETER(p);
39558 UNUSED_PARAMETER(iOff);
39559#endif
39560 return SQLITE_OK;
39561}
39562
39563/*
39564** Here ends the implementation of all sqlite3_file methods.
39565**
39566********************** End sqlite3_file Methods *******************************
39567******************************************************************************/
39568
39569/*
39570** This division contains definitions of sqlite3_io_methods objects that
39571** implement various file locking strategies. It also contains definitions
39572** of "finder" functions. A finder-function is used to locate the appropriate
39573** sqlite3_io_methods object for a particular database file. The pAppData
39574** field of the sqlite3_vfs VFS objects are initialized to be pointers to
39575** the correct finder-function for that VFS.
39576**
39577** Most finder functions return a pointer to a fixed sqlite3_io_methods
39578** object. The only interesting finder-function is autolockIoFinder, which
39579** looks at the filesystem type and tries to guess the best locking
39580** strategy from that.
39581**
39582** For finder-function F, two objects are created:
39583**
39584** (1) The real finder-function named "FImpt()".
39585**
39586** (2) A constant pointer to this function named just "F".
39587**
39588**
39589** A pointer to the F pointer is used as the pAppData value for VFS
39590** objects. We have to do this instead of letting pAppData point
39591** directly at the finder-function since C90 rules prevent a void*
39592** from be cast into a function pointer.
39593**
39594**
39595** Each instance of this macro generates two objects:
39596**
39597** * A constant sqlite3_io_methods object call METHOD that has locking
39598** methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
39599**
39600** * An I/O method finder function called FINDER that returns a pointer
39601** to the METHOD object in the previous bullet.
39602*/
39603#define IOMETHODS(FINDER,METHOD,VERSION,CLOSE,LOCK,UNLOCK,CKLOCK,SHMMAP) \
39604static const sqlite3_io_methods METHOD = { \
39605 VERSION, /* iVersion */ \
39606 CLOSE, /* xClose */ \
39607 unixRead, /* xRead */ \
39608 unixWrite, /* xWrite */ \
39609 unixTruncate, /* xTruncate */ \
39610 unixSync, /* xSync */ \
39611 unixFileSize, /* xFileSize */ \
39612 LOCK, /* xLock */ \
39613 UNLOCK, /* xUnlock */ \
39614 CKLOCK, /* xCheckReservedLock */ \
39615 unixFileControl, /* xFileControl */ \
39616 unixSectorSize, /* xSectorSize */ \
39617 unixDeviceCharacteristics, /* xDeviceCapabilities */ \
39618 SHMMAP, /* xShmMap */ \
39619 unixShmLock, /* xShmLock */ \
39620 unixShmBarrier, /* xShmBarrier */ \
39621 unixShmUnmap, /* xShmUnmap */ \
39622 unixFetch, /* xFetch */ \
39623 unixUnfetch, /* xUnfetch */ \
39624}; \
39625static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \
39626 UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \
39627 return &METHOD; \
39628} \
39629static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \
39630 = FINDER##Impl;
39631
39632/*
39633** Here are all of the sqlite3_io_methods objects for each of the
39634** locking strategies. Functions that return pointers to these methods
39635** are also created.
39636*/
39637IOMETHODS(
39638 posixIoFinder, /* Finder function name */
39639 posixIoMethods, /* sqlite3_io_methods object name */
39640 3, /* shared memory and mmap are enabled */
39641 unixClose, /* xClose method */
39642 unixLock, /* xLock method */
39643 unixUnlock, /* xUnlock method */
39644 unixCheckReservedLock, /* xCheckReservedLock method */
39645 unixShmMap /* xShmMap method */
39646)
39647IOMETHODS(
39648 nolockIoFinder, /* Finder function name */
39649 nolockIoMethods, /* sqlite3_io_methods object name */
39650 3, /* shared memory and mmap are enabled */
39651 nolockClose, /* xClose method */
39652 nolockLock, /* xLock method */
39653 nolockUnlock, /* xUnlock method */
39654 nolockCheckReservedLock, /* xCheckReservedLock method */
39655 0 /* xShmMap method */
39656)
39657IOMETHODS(
39658 dotlockIoFinder, /* Finder function name */
39659 dotlockIoMethods, /* sqlite3_io_methods object name */
39660 1, /* shared memory is disabled */
39661 dotlockClose, /* xClose method */
39662 dotlockLock, /* xLock method */
39663 dotlockUnlock, /* xUnlock method */
39664 dotlockCheckReservedLock, /* xCheckReservedLock method */
39665 0 /* xShmMap method */
39666)
39667
39668#if SQLITE_ENABLE_LOCKING_STYLE
39669IOMETHODS(
39670 flockIoFinder, /* Finder function name */
39671 flockIoMethods, /* sqlite3_io_methods object name */
39672 1, /* shared memory is disabled */
39673 flockClose, /* xClose method */
39674 flockLock, /* xLock method */
39675 flockUnlock, /* xUnlock method */
39676 flockCheckReservedLock, /* xCheckReservedLock method */
39677 0 /* xShmMap method */
39678)
39679#endif
39680
39681#if OS_VXWORKS
39682IOMETHODS(
39683 semIoFinder, /* Finder function name */
39684 semIoMethods, /* sqlite3_io_methods object name */
39685 1, /* shared memory is disabled */
39686 semXClose, /* xClose method */
39687 semXLock, /* xLock method */
39688 semXUnlock, /* xUnlock method */
39689 semXCheckReservedLock, /* xCheckReservedLock method */
39690 0 /* xShmMap method */
39691)
39692#endif
39693
39694#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
39695IOMETHODS(
39696 afpIoFinder, /* Finder function name */
39697 afpIoMethods, /* sqlite3_io_methods object name */
39698 1, /* shared memory is disabled */
39699 afpClose, /* xClose method */
39700 afpLock, /* xLock method */
39701 afpUnlock, /* xUnlock method */
39702 afpCheckReservedLock, /* xCheckReservedLock method */
39703 0 /* xShmMap method */
39704)
39705#endif
39706
39707/*
39708** The proxy locking method is a "super-method" in the sense that it
39709** opens secondary file descriptors for the conch and lock files and
39710** it uses proxy, dot-file, AFP, and flock() locking methods on those
39711** secondary files. For this reason, the division that implements
39712** proxy locking is located much further down in the file. But we need
39713** to go ahead and define the sqlite3_io_methods and finder function
39714** for proxy locking here. So we forward declare the I/O methods.
39715*/
39716#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
39717static int proxyClose(sqlite3_file*);
39718static int proxyLock(sqlite3_file*, int);
39719static int proxyUnlock(sqlite3_file*, int);
39720static int proxyCheckReservedLock(sqlite3_file*, int*);
39721IOMETHODS(
39722 proxyIoFinder, /* Finder function name */
39723 proxyIoMethods, /* sqlite3_io_methods object name */
39724 1, /* shared memory is disabled */
39725 proxyClose, /* xClose method */
39726 proxyLock, /* xLock method */
39727 proxyUnlock, /* xUnlock method */
39728 proxyCheckReservedLock, /* xCheckReservedLock method */
39729 0 /* xShmMap method */
39730)
39731#endif
39732
39733/* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
39734#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
39735IOMETHODS(
39736 nfsIoFinder, /* Finder function name */
39737 nfsIoMethods, /* sqlite3_io_methods object name */
39738 1, /* shared memory is disabled */
39739 unixClose, /* xClose method */
39740 unixLock, /* xLock method */
39741 nfsUnlock, /* xUnlock method */
39742 unixCheckReservedLock, /* xCheckReservedLock method */
39743 0 /* xShmMap method */
39744)
39745#endif
39746
39747#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
39748/*
39749** This "finder" function attempts to determine the best locking strategy
39750** for the database file "filePath". It then returns the sqlite3_io_methods
39751** object that implements that strategy.
39752**
39753** This is for MacOSX only.
39754*/
39755static const sqlite3_io_methods *autolockIoFinderImpl(
39756 const char *filePath, /* name of the database file */
39757 unixFile *pNew /* open file object for the database file */
39758){
39759 static const struct Mapping {
39760 const char *zFilesystem; /* Filesystem type name */
39761 const sqlite3_io_methods *pMethods; /* Appropriate locking method */
39762 } aMap[] = {
39763 { "hfs", &posixIoMethods },
39764 { "ufs", &posixIoMethods },
39765 { "afpfs", &afpIoMethods },
39766 { "smbfs", &afpIoMethods },
39767 { "webdav", &nolockIoMethods },
39768 { 0, 0 }
39769 };
39770 int i;
39771 struct statfs fsInfo;
39772 struct flock lockInfo;
39773
39774 if( !filePath ){
39775 /* If filePath==NULL that means we are dealing with a transient file
39776 ** that does not need to be locked. */
39777 return &nolockIoMethods;
39778 }
39779 if( statfs(filePath, &fsInfo) != -1 ){
39780 if( fsInfo.f_flags & MNT_RDONLY ){
39781 return &nolockIoMethods;
39782 }
39783 for(i=0; aMap[i].zFilesystem; i++){
39784 if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
39785 return aMap[i].pMethods;
39786 }
39787 }
39788 }
39789
39790 /* Default case. Handles, amongst others, "nfs".
39791 ** Test byte-range lock using fcntl(). If the call succeeds,
39792 ** assume that the file-system supports POSIX style locks.
39793 */
39794 lockInfo.l_len = 1;
39795 lockInfo.l_start = 0;
39796 lockInfo.l_whence = SEEK_SET;
39797 lockInfo.l_type = F_RDLCK;
39798 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
39799 if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){
39800 return &nfsIoMethods;
39801 } else {
39802 return &posixIoMethods;
39803 }
39804 }else{
39805 return &dotlockIoMethods;
39806 }
39807}
39808static const sqlite3_io_methods
39809 *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
39810
39811#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
39812
39813#if OS_VXWORKS
39814/*
39815** This "finder" function for VxWorks checks to see if posix advisory
39816** locking works. If it does, then that is what is used. If it does not
39817** work, then fallback to named semaphore locking.
39818*/
39819static const sqlite3_io_methods *vxworksIoFinderImpl(
39820 const char *filePath, /* name of the database file */
39821 unixFile *pNew /* the open file object */
39822){
39823 struct flock lockInfo;
39824
39825 if( !filePath ){
39826 /* If filePath==NULL that means we are dealing with a transient file
39827 ** that does not need to be locked. */
39828 return &nolockIoMethods;
39829 }
39830
39831 /* Test if fcntl() is supported and use POSIX style locks.
39832 ** Otherwise fall back to the named semaphore method.
39833 */
39834 lockInfo.l_len = 1;
39835 lockInfo.l_start = 0;
39836 lockInfo.l_whence = SEEK_SET;
39837 lockInfo.l_type = F_RDLCK;
39838 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
39839 return &posixIoMethods;
39840 }else{
39841 return &semIoMethods;
39842 }
39843}
39844static const sqlite3_io_methods
39845 *(*const vxworksIoFinder)(const char*,unixFile*) = vxworksIoFinderImpl;
39846
39847#endif /* OS_VXWORKS */
39848
39849/*
39850** An abstract type for a pointer to an IO method finder function:
39851*/
39852typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*);
39853
39854
39855/****************************************************************************
39856**************************** sqlite3_vfs methods ****************************
39857**
39858** This division contains the implementation of methods on the
39859** sqlite3_vfs object.
39860*/
39861
39862/*
39863** Initialize the contents of the unixFile structure pointed to by pId.
39864*/
39865static int fillInUnixFile(
39866 sqlite3_vfs *pVfs, /* Pointer to vfs object */
39867 int h, /* Open file descriptor of file being opened */
39868 sqlite3_file *pId, /* Write to the unixFile structure here */
39869 const char *zFilename, /* Name of the file being opened */
39870 int ctrlFlags /* Zero or more UNIXFILE_* values */
39871){
39872 const sqlite3_io_methods *pLockingStyle;
39873 unixFile *pNew = (unixFile *)pId;
39874 int rc = SQLITE_OK;
39875
39876 assert( pNew->pInode==NULL );
39877
39878 /* No locking occurs in temporary files */
39879 assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 );
39880
39881 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
39882 pNew->h = h;
39883 pNew->pVfs = pVfs;
39884 pNew->zPath = zFilename;
39885 pNew->ctrlFlags = (u8)ctrlFlags;
39886#if SQLITE_MAX_MMAP_SIZE>0
39887 pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
39888#endif
39889 if( sqlite3_uri_boolean(zFile: ((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
39890 zParam: "psow", SQLITE_POWERSAFE_OVERWRITE) ){
39891 pNew->ctrlFlags |= UNIXFILE_PSOW;
39892 }
39893 if( strcmp(s1: pVfs->zName,s2: "unix-excl")==0 ){
39894 pNew->ctrlFlags |= UNIXFILE_EXCL;
39895 }
39896
39897#if OS_VXWORKS
39898 pNew->pId = vxworksFindFileId(zFilename);
39899 if( pNew->pId==0 ){
39900 ctrlFlags |= UNIXFILE_NOLOCK;
39901 rc = SQLITE_NOMEM_BKPT;
39902 }
39903#endif
39904
39905 if( ctrlFlags & UNIXFILE_NOLOCK ){
39906 pLockingStyle = &nolockIoMethods;
39907 }else{
39908 pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
39909#if SQLITE_ENABLE_LOCKING_STYLE
39910 /* Cache zFilename in the locking context (AFP and dotlock override) for
39911 ** proxyLock activation is possible (remote proxy is based on db name)
39912 ** zFilename remains valid until file is closed, to support */
39913 pNew->lockingContext = (void*)zFilename;
39914#endif
39915 }
39916
39917 if( pLockingStyle == &posixIoMethods
39918#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
39919 || pLockingStyle == &nfsIoMethods
39920#endif
39921 ){
39922 unixEnterMutex();
39923 rc = findInodeInfo(pFile: pNew, ppInode: &pNew->pInode);
39924 if( rc!=SQLITE_OK ){
39925 /* If an error occurred in findInodeInfo(), close the file descriptor
39926 ** immediately, before releasing the mutex. findInodeInfo() may fail
39927 ** in two scenarios:
39928 **
39929 ** (a) A call to fstat() failed.
39930 ** (b) A malloc failed.
39931 **
39932 ** Scenario (b) may only occur if the process is holding no other
39933 ** file descriptors open on the same file. If there were other file
39934 ** descriptors on this file, then no malloc would be required by
39935 ** findInodeInfo(). If this is the case, it is quite safe to close
39936 ** handle h - as it is guaranteed that no posix locks will be released
39937 ** by doing so.
39938 **
39939 ** If scenario (a) caused the error then things are not so safe. The
39940 ** implicit assumption here is that if fstat() fails, things are in
39941 ** such bad shape that dropping a lock or two doesn't matter much.
39942 */
39943 robust_close(pFile: pNew, h, __LINE__);
39944 h = -1;
39945 }
39946 unixLeaveMutex();
39947 }
39948
39949#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
39950 else if( pLockingStyle == &afpIoMethods ){
39951 /* AFP locking uses the file path so it needs to be included in
39952 ** the afpLockingContext.
39953 */
39954 afpLockingContext *pCtx;
39955 pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) );
39956 if( pCtx==0 ){
39957 rc = SQLITE_NOMEM_BKPT;
39958 }else{
39959 /* NB: zFilename exists and remains valid until the file is closed
39960 ** according to requirement F11141. So we do not need to make a
39961 ** copy of the filename. */
39962 pCtx->dbPath = zFilename;
39963 pCtx->reserved = 0;
39964 srandomdev();
39965 unixEnterMutex();
39966 rc = findInodeInfo(pNew, &pNew->pInode);
39967 if( rc!=SQLITE_OK ){
39968 sqlite3_free(pNew->lockingContext);
39969 robust_close(pNew, h, __LINE__);
39970 h = -1;
39971 }
39972 unixLeaveMutex();
39973 }
39974 }
39975#endif
39976
39977 else if( pLockingStyle == &dotlockIoMethods ){
39978 /* Dotfile locking uses the file path so it needs to be included in
39979 ** the dotlockLockingContext
39980 */
39981 char *zLockFile;
39982 int nFilename;
39983 assert( zFilename!=0 );
39984 nFilename = (int)strlen(s: zFilename) + 6;
39985 zLockFile = (char *)sqlite3_malloc64(n: nFilename);
39986 if( zLockFile==0 ){
39987 rc = SQLITE_NOMEM_BKPT;
39988 }else{
39989 sqlite3_snprintf(n: nFilename, zBuf: zLockFile, zFormat: "%s" DOTLOCK_SUFFIX, zFilename);
39990 }
39991 pNew->lockingContext = zLockFile;
39992 }
39993
39994#if OS_VXWORKS
39995 else if( pLockingStyle == &semIoMethods ){
39996 /* Named semaphore locking uses the file path so it needs to be
39997 ** included in the semLockingContext
39998 */
39999 unixEnterMutex();
40000 rc = findInodeInfo(pNew, &pNew->pInode);
40001 if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
40002 char *zSemName = pNew->pInode->aSemName;
40003 int n;
40004 sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem",
40005 pNew->pId->zCanonicalName);
40006 for( n=1; zSemName[n]; n++ )
40007 if( zSemName[n]=='/' ) zSemName[n] = '_';
40008 pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
40009 if( pNew->pInode->pSem == SEM_FAILED ){
40010 rc = SQLITE_NOMEM_BKPT;
40011 pNew->pInode->aSemName[0] = '\0';
40012 }
40013 }
40014 unixLeaveMutex();
40015 }
40016#endif
40017
40018 storeLastErrno(pFile: pNew, error: 0);
40019#if OS_VXWORKS
40020 if( rc!=SQLITE_OK ){
40021 if( h>=0 ) robust_close(pNew, h, __LINE__);
40022 h = -1;
40023 osUnlink(zFilename);
40024 pNew->ctrlFlags |= UNIXFILE_DELETE;
40025 }
40026#endif
40027 if( rc!=SQLITE_OK ){
40028 if( h>=0 ) robust_close(pFile: pNew, h, __LINE__);
40029 }else{
40030 pId->pMethods = pLockingStyle;
40031 OpenCounter(+1);
40032 verifyDbFile(pFile: pNew);
40033 }
40034 return rc;
40035}
40036
40037/*
40038** Directories to consider for temp files.
40039*/
40040static const char *azTempDirs[] = {
40041 0,
40042 0,
40043 "/var/tmp",
40044 "/usr/tmp",
40045 "/tmp",
40046 "."
40047};
40048
40049/*
40050** Initialize first two members of azTempDirs[] array.
40051*/
40052static void unixTempFileInit(void){
40053 azTempDirs[0] = getenv(name: "SQLITE_TMPDIR");
40054 azTempDirs[1] = getenv(name: "TMPDIR");
40055}
40056
40057/*
40058** Return the name of a directory in which to put temporary files.
40059** If no suitable temporary file directory can be found, return NULL.
40060*/
40061static const char *unixTempFileDir(void){
40062 unsigned int i = 0;
40063 struct stat buf;
40064 const char *zDir = sqlite3_temp_directory;
40065
40066 while(1){
40067 if( zDir!=0
40068 && osStat(zDir, &buf)==0
40069 && S_ISDIR(buf.st_mode)
40070 && osAccess(zDir, 03)==0
40071 ){
40072 return zDir;
40073 }
40074 if( i>=sizeof(azTempDirs)/sizeof(azTempDirs[0]) ) break;
40075 zDir = azTempDirs[i++];
40076 }
40077 return 0;
40078}
40079
40080/*
40081** Create a temporary file name in zBuf. zBuf must be allocated
40082** by the calling process and must be big enough to hold at least
40083** pVfs->mxPathname bytes.
40084*/
40085static int unixGetTempname(int nBuf, char *zBuf){
40086 const char *zDir;
40087 int iLimit = 0;
40088
40089 /* It's odd to simulate an io-error here, but really this is just
40090 ** using the io-error infrastructure to test that SQLite handles this
40091 ** function failing.
40092 */
40093 zBuf[0] = 0;
40094 SimulateIOError( return SQLITE_IOERR );
40095
40096 zDir = unixTempFileDir();
40097 if( zDir==0 ) return SQLITE_IOERR_GETTEMPPATH;
40098 do{
40099 u64 r;
40100 sqlite3_randomness(N: sizeof(r), pBuf: &r);
40101 assert( nBuf>2 );
40102 zBuf[nBuf-2] = 0;
40103 sqlite3_snprintf(n: nBuf, zBuf, zFormat: "%s/"SQLITE_TEMP_FILE_PREFIX"%llx%c",
40104 zDir, r, 0);
40105 if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ) return SQLITE_ERROR;
40106 }while( osAccess(zBuf,0)==0 );
40107 return SQLITE_OK;
40108}
40109
40110#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
40111/*
40112** Routine to transform a unixFile into a proxy-locking unixFile.
40113** Implementation in the proxy-lock division, but used by unixOpen()
40114** if SQLITE_PREFER_PROXY_LOCKING is defined.
40115*/
40116static int proxyTransformUnixFile(unixFile*, const char*);
40117#endif
40118
40119/*
40120** Search for an unused file descriptor that was opened on the database
40121** file (not a journal or super-journal file) identified by pathname
40122** zPath with SQLITE_OPEN_XXX flags matching those passed as the second
40123** argument to this function.
40124**
40125** Such a file descriptor may exist if a database connection was closed
40126** but the associated file descriptor could not be closed because some
40127** other file descriptor open on the same file is holding a file-lock.
40128** Refer to comments in the unixClose() function and the lengthy comment
40129** describing "Posix Advisory Locking" at the start of this file for
40130** further details. Also, ticket #4018.
40131**
40132** If a suitable file descriptor is found, then it is returned. If no
40133** such file descriptor is located, -1 is returned.
40134*/
40135static UnixUnusedFd *findReusableFd(const char *zPath, int flags){
40136 UnixUnusedFd *pUnused = 0;
40137
40138 /* Do not search for an unused file descriptor on vxworks. Not because
40139 ** vxworks would not benefit from the change (it might, we're not sure),
40140 ** but because no way to test it is currently available. It is better
40141 ** not to risk breaking vxworks support for the sake of such an obscure
40142 ** feature. */
40143#if !OS_VXWORKS
40144 struct stat sStat; /* Results of stat() call */
40145
40146 unixEnterMutex();
40147
40148 /* A stat() call may fail for various reasons. If this happens, it is
40149 ** almost certain that an open() call on the same path will also fail.
40150 ** For this reason, if an error occurs in the stat() call here, it is
40151 ** ignored and -1 is returned. The caller will try to open a new file
40152 ** descriptor on the same path, fail, and return an error to SQLite.
40153 **
40154 ** Even if a subsequent open() call does succeed, the consequences of
40155 ** not searching for a reusable file descriptor are not dire. */
40156 if( inodeList!=0 && 0==osStat(zPath, &sStat) ){
40157 unixInodeInfo *pInode;
40158
40159 pInode = inodeList;
40160 while( pInode && (pInode->fileId.dev!=sStat.st_dev
40161 || pInode->fileId.ino!=(u64)sStat.st_ino) ){
40162 pInode = pInode->pNext;
40163 }
40164 if( pInode ){
40165 UnixUnusedFd **pp;
40166 assert( sqlite3_mutex_notheld(pInode->pLockMutex) );
40167 sqlite3_mutex_enter(p: pInode->pLockMutex);
40168 flags &= (SQLITE_OPEN_READONLY|SQLITE_OPEN_READWRITE);
40169 for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
40170 pUnused = *pp;
40171 if( pUnused ){
40172 *pp = pUnused->pNext;
40173 }
40174 sqlite3_mutex_leave(p: pInode->pLockMutex);
40175 }
40176 }
40177 unixLeaveMutex();
40178#endif /* if !OS_VXWORKS */
40179 return pUnused;
40180}
40181
40182/*
40183** Find the mode, uid and gid of file zFile.
40184*/
40185static int getFileMode(
40186 const char *zFile, /* File name */
40187 mode_t *pMode, /* OUT: Permissions of zFile */
40188 uid_t *pUid, /* OUT: uid of zFile. */
40189 gid_t *pGid /* OUT: gid of zFile. */
40190){
40191 struct stat sStat; /* Output of stat() on database file */
40192 int rc = SQLITE_OK;
40193 if( 0==osStat(zFile, &sStat) ){
40194 *pMode = sStat.st_mode & 0777;
40195 *pUid = sStat.st_uid;
40196 *pGid = sStat.st_gid;
40197 }else{
40198 rc = SQLITE_IOERR_FSTAT;
40199 }
40200 return rc;
40201}
40202
40203/*
40204** This function is called by unixOpen() to determine the unix permissions
40205** to create new files with. If no error occurs, then SQLITE_OK is returned
40206** and a value suitable for passing as the third argument to open(2) is
40207** written to *pMode. If an IO error occurs, an SQLite error code is
40208** returned and the value of *pMode is not modified.
40209**
40210** In most cases, this routine sets *pMode to 0, which will become
40211** an indication to robust_open() to create the file using
40212** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask.
40213** But if the file being opened is a WAL or regular journal file, then
40214** this function queries the file-system for the permissions on the
40215** corresponding database file and sets *pMode to this value. Whenever
40216** possible, WAL and journal files are created using the same permissions
40217** as the associated database file.
40218**
40219** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the
40220** original filename is unavailable. But 8_3_NAMES is only used for
40221** FAT filesystems and permissions do not matter there, so just use
40222** the default permissions. In 8_3_NAMES mode, leave *pMode set to zero.
40223*/
40224static int findCreateFileMode(
40225 const char *zPath, /* Path of file (possibly) being created */
40226 int flags, /* Flags passed as 4th argument to xOpen() */
40227 mode_t *pMode, /* OUT: Permissions to open file with */
40228 uid_t *pUid, /* OUT: uid to set on the file */
40229 gid_t *pGid /* OUT: gid to set on the file */
40230){
40231 int rc = SQLITE_OK; /* Return Code */
40232 *pMode = 0;
40233 *pUid = 0;
40234 *pGid = 0;
40235 if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
40236 char zDb[MAX_PATHNAME+1]; /* Database file path */
40237 int nDb; /* Number of valid bytes in zDb */
40238
40239 /* zPath is a path to a WAL or journal file. The following block derives
40240 ** the path to the associated database file from zPath. This block handles
40241 ** the following naming conventions:
40242 **
40243 ** "<path to db>-journal"
40244 ** "<path to db>-wal"
40245 ** "<path to db>-journalNN"
40246 ** "<path to db>-walNN"
40247 **
40248 ** where NN is a decimal number. The NN naming schemes are
40249 ** used by the test_multiplex.c module.
40250 */
40251 nDb = sqlite3Strlen30(z: zPath) - 1;
40252 while( zPath[nDb]!='-' ){
40253 /* In normal operation, the journal file name will always contain
40254 ** a '-' character. However in 8+3 filename mode, or if a corrupt
40255 ** rollback journal specifies a super-journal with a goofy name, then
40256 ** the '-' might be missing. */
40257 if( nDb==0 || zPath[nDb]=='.' ) return SQLITE_OK;
40258 nDb--;
40259 }
40260 memcpy(dest: zDb, src: zPath, n: nDb);
40261 zDb[nDb] = '\0';
40262
40263 rc = getFileMode(zFile: zDb, pMode, pUid, pGid);
40264 }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
40265 *pMode = 0600;
40266 }else if( flags & SQLITE_OPEN_URI ){
40267 /* If this is a main database file and the file was opened using a URI
40268 ** filename, check for the "modeof" parameter. If present, interpret
40269 ** its value as a filename and try to copy the mode, uid and gid from
40270 ** that file. */
40271 const char *z = sqlite3_uri_parameter(zFilename: zPath, zParam: "modeof");
40272 if( z ){
40273 rc = getFileMode(zFile: z, pMode, pUid, pGid);
40274 }
40275 }
40276 return rc;
40277}
40278
40279/*
40280** Open the file zPath.
40281**
40282** Previously, the SQLite OS layer used three functions in place of this
40283** one:
40284**
40285** sqlite3OsOpenReadWrite();
40286** sqlite3OsOpenReadOnly();
40287** sqlite3OsOpenExclusive();
40288**
40289** These calls correspond to the following combinations of flags:
40290**
40291** ReadWrite() -> (READWRITE | CREATE)
40292** ReadOnly() -> (READONLY)
40293** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
40294**
40295** The old OpenExclusive() accepted a boolean argument - "delFlag". If
40296** true, the file was configured to be automatically deleted when the
40297** file handle closed. To achieve the same effect using this new
40298** interface, add the DELETEONCLOSE flag to those specified above for
40299** OpenExclusive().
40300*/
40301static int unixOpen(
40302 sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */
40303 const char *zPath, /* Pathname of file to be opened */
40304 sqlite3_file *pFile, /* The file descriptor to be filled in */
40305 int flags, /* Input flags to control the opening */
40306 int *pOutFlags /* Output flags returned to SQLite core */
40307){
40308 unixFile *p = (unixFile *)pFile;
40309 int fd = -1; /* File descriptor returned by open() */
40310 int openFlags = 0; /* Flags to pass to open() */
40311 int eType = flags&0x0FFF00; /* Type of file to open */
40312 int noLock; /* True to omit locking primitives */
40313 int rc = SQLITE_OK; /* Function Return Code */
40314 int ctrlFlags = 0; /* UNIXFILE_* flags */
40315
40316 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
40317 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
40318 int isCreate = (flags & SQLITE_OPEN_CREATE);
40319 int isReadonly = (flags & SQLITE_OPEN_READONLY);
40320 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
40321#if SQLITE_ENABLE_LOCKING_STYLE
40322 int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY);
40323#endif
40324#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
40325 struct statfs fsInfo;
40326#endif
40327
40328 /* If creating a super- or main-file journal, this function will open
40329 ** a file-descriptor on the directory too. The first time unixSync()
40330 ** is called the directory file descriptor will be fsync()ed and close()d.
40331 */
40332 int isNewJrnl = (isCreate && (
40333 eType==SQLITE_OPEN_SUPER_JOURNAL
40334 || eType==SQLITE_OPEN_MAIN_JOURNAL
40335 || eType==SQLITE_OPEN_WAL
40336 ));
40337
40338 /* If argument zPath is a NULL pointer, this function is required to open
40339 ** a temporary file. Use this buffer to store the file name in.
40340 */
40341 char zTmpname[MAX_PATHNAME+2];
40342 const char *zName = zPath;
40343
40344 /* Check the following statements are true:
40345 **
40346 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
40347 ** (b) if CREATE is set, then READWRITE must also be set, and
40348 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
40349 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
40350 */
40351 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
40352 assert(isCreate==0 || isReadWrite);
40353 assert(isExclusive==0 || isCreate);
40354 assert(isDelete==0 || isCreate);
40355
40356 /* The main DB, main journal, WAL file and super-journal are never
40357 ** automatically deleted. Nor are they ever temporary files. */
40358 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
40359 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
40360 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_SUPER_JOURNAL );
40361 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
40362
40363 /* Assert that the upper layer has set one of the "file-type" flags. */
40364 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
40365 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
40366 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_SUPER_JOURNAL
40367 || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
40368 );
40369
40370 /* Detect a pid change and reset the PRNG. There is a race condition
40371 ** here such that two or more threads all trying to open databases at
40372 ** the same instant might all reset the PRNG. But multiple resets
40373 ** are harmless.
40374 */
40375 if( randomnessPid!=osGetpid(0) ){
40376 randomnessPid = osGetpid(0);
40377 sqlite3_randomness(N: 0,pBuf: 0);
40378 }
40379 memset(s: p, c: 0, n: sizeof(unixFile));
40380
40381#ifdef SQLITE_ASSERT_NO_FILES
40382 /* Applications that never read or write a persistent disk files */
40383 assert( zName==0 );
40384#endif
40385
40386 if( eType==SQLITE_OPEN_MAIN_DB ){
40387 UnixUnusedFd *pUnused;
40388 pUnused = findReusableFd(zPath: zName, flags);
40389 if( pUnused ){
40390 fd = pUnused->fd;
40391 }else{
40392 pUnused = sqlite3_malloc64(n: sizeof(*pUnused));
40393 if( !pUnused ){
40394 return SQLITE_NOMEM_BKPT;
40395 }
40396 }
40397 p->pPreallocatedUnused = pUnused;
40398
40399 /* Database filenames are double-zero terminated if they are not
40400 ** URIs with parameters. Hence, they can always be passed into
40401 ** sqlite3_uri_parameter(). */
40402 assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 );
40403
40404 }else if( !zName ){
40405 /* If zName is NULL, the upper layer is requesting a temp file. */
40406 assert(isDelete && !isNewJrnl);
40407 rc = unixGetTempname(nBuf: pVfs->mxPathname, zBuf: zTmpname);
40408 if( rc!=SQLITE_OK ){
40409 return rc;
40410 }
40411 zName = zTmpname;
40412
40413 /* Generated temporary filenames are always double-zero terminated
40414 ** for use by sqlite3_uri_parameter(). */
40415 assert( zName[strlen(zName)+1]==0 );
40416 }
40417
40418 /* Determine the value of the flags parameter passed to POSIX function
40419 ** open(). These must be calculated even if open() is not called, as
40420 ** they may be stored as part of the file handle and used by the
40421 ** 'conch file' locking functions later on. */
40422 if( isReadonly ) openFlags |= O_RDONLY;
40423 if( isReadWrite ) openFlags |= O_RDWR;
40424 if( isCreate ) openFlags |= O_CREAT;
40425 if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW);
40426 openFlags |= (O_LARGEFILE|O_BINARY|O_NOFOLLOW);
40427
40428 if( fd<0 ){
40429 mode_t openMode; /* Permissions to create file with */
40430 uid_t uid; /* Userid for the file */
40431 gid_t gid; /* Groupid for the file */
40432 rc = findCreateFileMode(zPath: zName, flags, pMode: &openMode, pUid: &uid, pGid: &gid);
40433 if( rc!=SQLITE_OK ){
40434 assert( !p->pPreallocatedUnused );
40435 assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL );
40436 return rc;
40437 }
40438 fd = robust_open(z: zName, f: openFlags, m: openMode);
40439 OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags));
40440 assert( !isExclusive || (openFlags & O_CREAT)!=0 );
40441 if( fd<0 ){
40442 if( isNewJrnl && errno==EACCES && osAccess(zName, F_OK) ){
40443 /* If unable to create a journal because the directory is not
40444 ** writable, change the error code to indicate that. */
40445 rc = SQLITE_READONLY_DIRECTORY;
40446 }else if( errno!=EISDIR && isReadWrite ){
40447 /* Failed to open the file for read/write access. Try read-only. */
40448 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
40449 openFlags &= ~(O_RDWR|O_CREAT);
40450 flags |= SQLITE_OPEN_READONLY;
40451 openFlags |= O_RDONLY;
40452 isReadonly = 1;
40453 fd = robust_open(z: zName, f: openFlags, m: openMode);
40454 }
40455 }
40456 if( fd<0 ){
40457 int rc2 = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
40458 if( rc==SQLITE_OK ) rc = rc2;
40459 goto open_finished;
40460 }
40461
40462 /* The owner of the rollback journal or WAL file should always be the
40463 ** same as the owner of the database file. Try to ensure that this is
40464 ** the case. The chown() system call will be a no-op if the current
40465 ** process lacks root privileges, be we should at least try. Without
40466 ** this step, if a root process opens a database file, it can leave
40467 ** behinds a journal/WAL that is owned by root and hence make the
40468 ** database inaccessible to unprivileged processes.
40469 **
40470 ** If openMode==0, then that means uid and gid are not set correctly
40471 ** (probably because SQLite is configured to use 8+3 filename mode) and
40472 ** in that case we do not want to attempt the chown().
40473 */
40474 if( openMode && (flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL))!=0 ){
40475 robustFchown(fd, uid, gid);
40476 }
40477 }
40478 assert( fd>=0 );
40479 if( pOutFlags ){
40480 *pOutFlags = flags;
40481 }
40482
40483 if( p->pPreallocatedUnused ){
40484 p->pPreallocatedUnused->fd = fd;
40485 p->pPreallocatedUnused->flags =
40486 flags & (SQLITE_OPEN_READONLY|SQLITE_OPEN_READWRITE);
40487 }
40488
40489 if( isDelete ){
40490#if OS_VXWORKS
40491 zPath = zName;
40492#elif defined(SQLITE_UNLINK_AFTER_CLOSE)
40493 zPath = sqlite3_mprintf("%s", zName);
40494 if( zPath==0 ){
40495 robust_close(p, fd, __LINE__);
40496 return SQLITE_NOMEM_BKPT;
40497 }
40498#else
40499 osUnlink(zName);
40500#endif
40501 }
40502#if SQLITE_ENABLE_LOCKING_STYLE
40503 else{
40504 p->openFlags = openFlags;
40505 }
40506#endif
40507
40508#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
40509 if( fstatfs(fd, &fsInfo) == -1 ){
40510 storeLastErrno(p, errno);
40511 robust_close(p, fd, __LINE__);
40512 return SQLITE_IOERR_ACCESS;
40513 }
40514 if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
40515 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
40516 }
40517 if (0 == strncmp("exfat", fsInfo.f_fstypename, 5)) {
40518 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
40519 }
40520#endif
40521
40522 /* Set up appropriate ctrlFlags */
40523 if( isDelete ) ctrlFlags |= UNIXFILE_DELETE;
40524 if( isReadonly ) ctrlFlags |= UNIXFILE_RDONLY;
40525 noLock = eType!=SQLITE_OPEN_MAIN_DB;
40526 if( noLock ) ctrlFlags |= UNIXFILE_NOLOCK;
40527 if( isNewJrnl ) ctrlFlags |= UNIXFILE_DIRSYNC;
40528 if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI;
40529
40530#if SQLITE_ENABLE_LOCKING_STYLE
40531#if SQLITE_PREFER_PROXY_LOCKING
40532 isAutoProxy = 1;
40533#endif
40534 if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
40535 char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
40536 int useProxy = 0;
40537
40538 /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means
40539 ** never use proxy, NULL means use proxy for non-local files only. */
40540 if( envforce!=NULL ){
40541 useProxy = atoi(envforce)>0;
40542 }else{
40543 useProxy = !(fsInfo.f_flags&MNT_LOCAL);
40544 }
40545 if( useProxy ){
40546 rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
40547 if( rc==SQLITE_OK ){
40548 rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
40549 if( rc!=SQLITE_OK ){
40550 /* Use unixClose to clean up the resources added in fillInUnixFile
40551 ** and clear all the structure's references. Specifically,
40552 ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
40553 */
40554 unixClose(pFile);
40555 return rc;
40556 }
40557 }
40558 goto open_finished;
40559 }
40560 }
40561#endif
40562
40563 assert( zPath==0 || zPath[0]=='/'
40564 || eType==SQLITE_OPEN_SUPER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL
40565 );
40566 rc = fillInUnixFile(pVfs, h: fd, pId: pFile, zFilename: zPath, ctrlFlags);
40567
40568open_finished:
40569 if( rc!=SQLITE_OK ){
40570 sqlite3_free(p: p->pPreallocatedUnused);
40571 }
40572 return rc;
40573}
40574
40575
40576/*
40577** Delete the file at zPath. If the dirSync argument is true, fsync()
40578** the directory after deleting the file.
40579*/
40580static int unixDelete(
40581 sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */
40582 const char *zPath, /* Name of file to be deleted */
40583 int dirSync /* If true, fsync() directory after deleting file */
40584){
40585 int rc = SQLITE_OK;
40586 UNUSED_PARAMETER(NotUsed);
40587 SimulateIOError(return SQLITE_IOERR_DELETE);
40588 if( osUnlink(zPath)==(-1) ){
40589 if( errno==ENOENT
40590#if OS_VXWORKS
40591 || osAccess(zPath,0)!=0
40592#endif
40593 ){
40594 rc = SQLITE_IOERR_DELETE_NOENT;
40595 }else{
40596 rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
40597 }
40598 return rc;
40599 }
40600#ifndef SQLITE_DISABLE_DIRSYNC
40601 if( (dirSync & 1)!=0 ){
40602 int fd;
40603 rc = osOpenDirectory(zPath, &fd);
40604 if( rc==SQLITE_OK ){
40605 if( full_fsync(fd,fullSync: 0,dataOnly: 0) ){
40606 rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
40607 }
40608 robust_close(pFile: 0, h: fd, __LINE__);
40609 }else{
40610 assert( rc==SQLITE_CANTOPEN );
40611 rc = SQLITE_OK;
40612 }
40613 }
40614#endif
40615 return rc;
40616}
40617
40618/*
40619** Test the existence of or access permissions of file zPath. The
40620** test performed depends on the value of flags:
40621**
40622** SQLITE_ACCESS_EXISTS: Return 1 if the file exists
40623** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
40624** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
40625**
40626** Otherwise return 0.
40627*/
40628static int unixAccess(
40629 sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */
40630 const char *zPath, /* Path of the file to examine */
40631 int flags, /* What do we want to learn about the zPath file? */
40632 int *pResOut /* Write result boolean here */
40633){
40634 UNUSED_PARAMETER(NotUsed);
40635 SimulateIOError( return SQLITE_IOERR_ACCESS; );
40636 assert( pResOut!=0 );
40637
40638 /* The spec says there are three possible values for flags. But only
40639 ** two of them are actually used */
40640 assert( flags==SQLITE_ACCESS_EXISTS || flags==SQLITE_ACCESS_READWRITE );
40641
40642 if( flags==SQLITE_ACCESS_EXISTS ){
40643 struct stat buf;
40644 *pResOut = 0==osStat(zPath, &buf) &&
40645 (!S_ISREG(buf.st_mode) || buf.st_size>0);
40646 }else{
40647 *pResOut = osAccess(zPath, W_OK|R_OK)==0;
40648 }
40649 return SQLITE_OK;
40650}
40651
40652/*
40653** If the last component of the pathname in z[0]..z[j-1] is something
40654** other than ".." then back it out and return true. If the last
40655** component is empty or if it is ".." then return false.
40656*/
40657static int unixBackupDir(const char *z, int *pJ){
40658 int j = *pJ;
40659 int i;
40660 if( j<=0 ) return 0;
40661 for(i=j-1; i>0 && z[i-1]!='/'; i--){}
40662 if( i==0 ) return 0;
40663 if( z[i]=='.' && i==j-2 && z[i+1]=='.' ) return 0;
40664 *pJ = i-1;
40665 return 1;
40666}
40667
40668/*
40669** Convert a relative pathname into a full pathname. Also
40670** simplify the pathname as follows:
40671**
40672** Remove all instances of /./
40673** Remove all isntances of /X/../ for any X
40674*/
40675static int mkFullPathname(
40676 const char *zPath, /* Input path */
40677 char *zOut, /* Output buffer */
40678 int nOut /* Allocated size of buffer zOut */
40679){
40680 int nPath = sqlite3Strlen30(z: zPath);
40681 int iOff = 0;
40682 int i, j;
40683 if( zPath[0]!='/' ){
40684 if( osGetcwd(zOut, nOut-2)==0 ){
40685 return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath);
40686 }
40687 iOff = sqlite3Strlen30(z: zOut);
40688 zOut[iOff++] = '/';
40689 }
40690 if( (iOff+nPath+1)>nOut ){
40691 /* SQLite assumes that xFullPathname() nul-terminates the output buffer
40692 ** even if it returns an error. */
40693 zOut[iOff] = '\0';
40694 return SQLITE_CANTOPEN_BKPT;
40695 }
40696 sqlite3_snprintf(n: nOut-iOff, zBuf: &zOut[iOff], zFormat: "%s", zPath);
40697
40698 /* Remove duplicate '/' characters. Except, two // at the beginning
40699 ** of a pathname is allowed since this is important on windows. */
40700 for(i=j=1; zOut[i]; i++){
40701 zOut[j++] = zOut[i];
40702 while( zOut[i]=='/' && zOut[i+1]=='/' ) i++;
40703 }
40704 zOut[j] = 0;
40705
40706 assert( zOut[0]=='/' );
40707 for(i=j=0; zOut[i]; i++){
40708 if( zOut[i]=='/' ){
40709 /* Skip over internal "/." directory components */
40710 if( zOut[i+1]=='.' && zOut[i+2]=='/' ){
40711 i += 1;
40712 continue;
40713 }
40714
40715 /* If this is a "/.." directory component then back out the
40716 ** previous term of the directory if it is something other than "..".
40717 */
40718 if( zOut[i+1]=='.'
40719 && zOut[i+2]=='.'
40720 && zOut[i+3]=='/'
40721 && unixBackupDir(z: zOut, pJ: &j)
40722 ){
40723 i += 2;
40724 continue;
40725 }
40726 }
40727 if( ALWAYS(j>=0) ) zOut[j] = zOut[i];
40728 j++;
40729 }
40730 if( NEVER(j==0) ) zOut[j++] = '/';
40731 zOut[j] = 0;
40732 return SQLITE_OK;
40733}
40734
40735/*
40736** Turn a relative pathname into a full pathname. The relative path
40737** is stored as a nul-terminated string in the buffer pointed to by
40738** zPath.
40739**
40740** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
40741** (in this case, MAX_PATHNAME bytes). The full-path is written to
40742** this buffer before returning.
40743*/
40744static int unixFullPathname(
40745 sqlite3_vfs *pVfs, /* Pointer to vfs object */
40746 const char *zPath, /* Possibly relative input path */
40747 int nOut, /* Size of output buffer in bytes */
40748 char *zOut /* Output buffer */
40749){
40750#if !defined(HAVE_READLINK) || !defined(HAVE_LSTAT)
40751 return mkFullPathname(zPath, zOut, nOut);
40752#else
40753 int rc = SQLITE_OK;
40754 int nByte;
40755 int nLink = 0; /* Number of symbolic links followed so far */
40756 const char *zIn = zPath; /* Input path for each iteration of loop */
40757 char *zDel = 0;
40758
40759 assert( pVfs->mxPathname==MAX_PATHNAME );
40760 UNUSED_PARAMETER(pVfs);
40761
40762 /* It's odd to simulate an io-error here, but really this is just
40763 ** using the io-error infrastructure to test that SQLite handles this
40764 ** function failing. This function could fail if, for example, the
40765 ** current working directory has been unlinked.
40766 */
40767 SimulateIOError( return SQLITE_ERROR );
40768
40769 do {
40770
40771 /* Call stat() on path zIn. Set bLink to true if the path is a symbolic
40772 ** link, or false otherwise. */
40773 int bLink = 0;
40774 struct stat buf;
40775 if( osLstat(zIn, &buf)!=0 ){
40776 if( errno!=ENOENT ){
40777 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "lstat", zIn);
40778 }
40779 }else{
40780 bLink = S_ISLNK(buf.st_mode);
40781 }
40782
40783 if( bLink ){
40784 nLink++;
40785 if( zDel==0 ){
40786 zDel = sqlite3_malloc(n: nOut);
40787 if( zDel==0 ) rc = SQLITE_NOMEM_BKPT;
40788 }else if( nLink>=SQLITE_MAX_SYMLINKS ){
40789 rc = SQLITE_CANTOPEN_BKPT;
40790 }
40791
40792 if( rc==SQLITE_OK ){
40793 nByte = osReadlink(zIn, zDel, nOut-1);
40794 if( nByte<0 ){
40795 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn);
40796 }else{
40797 if( zDel[0]!='/' ){
40798 int n;
40799 for(n = sqlite3Strlen30(z: zIn); n>0 && zIn[n-1]!='/'; n--);
40800 if( nByte+n+1>nOut ){
40801 rc = SQLITE_CANTOPEN_BKPT;
40802 }else{
40803 memmove(dest: &zDel[n], src: zDel, n: nByte+1);
40804 memcpy(dest: zDel, src: zIn, n: n);
40805 nByte += n;
40806 }
40807 }
40808 zDel[nByte] = '\0';
40809 }
40810 }
40811
40812 zIn = zDel;
40813 }
40814
40815 assert( rc!=SQLITE_OK || zIn!=zOut || zIn[0]=='/' );
40816 if( rc==SQLITE_OK && zIn!=zOut ){
40817 rc = mkFullPathname(zPath: zIn, zOut, nOut);
40818 }
40819 if( bLink==0 ) break;
40820 zIn = zOut;
40821 }while( rc==SQLITE_OK );
40822
40823 sqlite3_free(p: zDel);
40824 if( rc==SQLITE_OK && nLink ) rc = SQLITE_OK_SYMLINK;
40825 return rc;
40826#endif /* HAVE_READLINK && HAVE_LSTAT */
40827}
40828
40829
40830#ifndef SQLITE_OMIT_LOAD_EXTENSION
40831/*
40832** Interfaces for opening a shared library, finding entry points
40833** within the shared library, and closing the shared library.
40834*/
40835#include <dlfcn.h>
40836static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
40837 UNUSED_PARAMETER(NotUsed);
40838 return dlopen(file: zFilename, RTLD_NOW | RTLD_GLOBAL);
40839}
40840
40841/*
40842** SQLite calls this function immediately after a call to unixDlSym() or
40843** unixDlOpen() fails (returns a null pointer). If a more detailed error
40844** message is available, it is written to zBufOut. If no error message
40845** is available, zBufOut is left unmodified and SQLite uses a default
40846** error message.
40847*/
40848static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
40849 const char *zErr;
40850 UNUSED_PARAMETER(NotUsed);
40851 unixEnterMutex();
40852 zErr = dlerror();
40853 if( zErr ){
40854 sqlite3_snprintf(n: nBuf, zBuf: zBufOut, zFormat: "%s", zErr);
40855 }
40856 unixLeaveMutex();
40857}
40858static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){
40859 /*
40860 ** GCC with -pedantic-errors says that C90 does not allow a void* to be
40861 ** cast into a pointer to a function. And yet the library dlsym() routine
40862 ** returns a void* which is really a pointer to a function. So how do we
40863 ** use dlsym() with -pedantic-errors?
40864 **
40865 ** Variable x below is defined to be a pointer to a function taking
40866 ** parameters void* and const char* and returning a pointer to a function.
40867 ** We initialize x by assigning it a pointer to the dlsym() function.
40868 ** (That assignment requires a cast.) Then we call the function that
40869 ** x points to.
40870 **
40871 ** This work-around is unlikely to work correctly on any system where
40872 ** you really cannot cast a function pointer into void*. But then, on the
40873 ** other hand, dlsym() will not work on such a system either, so we have
40874 ** not really lost anything.
40875 */
40876 void (*(*x)(void*,const char*))(void);
40877 UNUSED_PARAMETER(NotUsed);
40878 x = (void(*(*)(void*,const char*))(void))dlsym;
40879 return (*x)(p, zSym);
40880}
40881static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
40882 UNUSED_PARAMETER(NotUsed);
40883 dlclose(handle: pHandle);
40884}
40885#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
40886 #define unixDlOpen 0
40887 #define unixDlError 0
40888 #define unixDlSym 0
40889 #define unixDlClose 0
40890#endif
40891
40892/*
40893** Write nBuf bytes of random data to the supplied buffer zBuf.
40894*/
40895static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
40896 UNUSED_PARAMETER(NotUsed);
40897 assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
40898
40899 /* We have to initialize zBuf to prevent valgrind from reporting
40900 ** errors. The reports issued by valgrind are incorrect - we would
40901 ** prefer that the randomness be increased by making use of the
40902 ** uninitialized space in zBuf - but valgrind errors tend to worry
40903 ** some users. Rather than argue, it seems easier just to initialize
40904 ** the whole array and silence valgrind, even if that means less randomness
40905 ** in the random seed.
40906 **
40907 ** When testing, initializing zBuf[] to zero is all we do. That means
40908 ** that we always use the same random number sequence. This makes the
40909 ** tests repeatable.
40910 */
40911 memset(s: zBuf, c: 0, n: nBuf);
40912 randomnessPid = osGetpid(0);
40913#if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS)
40914 {
40915 int fd, got;
40916 fd = robust_open(z: "/dev/urandom", O_RDONLY, m: 0);
40917 if( fd<0 ){
40918 time_t t;
40919 time(timer: &t);
40920 memcpy(dest: zBuf, src: &t, n: sizeof(t));
40921 memcpy(dest: &zBuf[sizeof(t)], src: &randomnessPid, n: sizeof(randomnessPid));
40922 assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf );
40923 nBuf = sizeof(t) + sizeof(randomnessPid);
40924 }else{
40925 do{ got = osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR );
40926 robust_close(pFile: 0, h: fd, __LINE__);
40927 }
40928 }
40929#endif
40930 return nBuf;
40931}
40932
40933
40934/*
40935** Sleep for a little while. Return the amount of time slept.
40936** The argument is the number of microseconds we want to sleep.
40937** The return value is the number of microseconds of sleep actually
40938** requested from the underlying operating system, a number which
40939** might be greater than or equal to the argument, but not less
40940** than the argument.
40941*/
40942static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
40943#if OS_VXWORKS
40944 struct timespec sp;
40945
40946 sp.tv_sec = microseconds / 1000000;
40947 sp.tv_nsec = (microseconds % 1000000) * 1000;
40948 nanosleep(&sp, NULL);
40949 UNUSED_PARAMETER(NotUsed);
40950 return microseconds;
40951#elif defined(HAVE_USLEEP) && HAVE_USLEEP
40952 if( microseconds>=1000000 ) sleep(seconds: microseconds/1000000);
40953 if( microseconds%1000000 ) usleep(useconds: microseconds%1000000);
40954 UNUSED_PARAMETER(NotUsed);
40955 return microseconds;
40956#else
40957 int seconds = (microseconds+999999)/1000000;
40958 sleep(seconds);
40959 UNUSED_PARAMETER(NotUsed);
40960 return seconds*1000000;
40961#endif
40962}
40963
40964/*
40965** The following variable, if set to a non-zero value, is interpreted as
40966** the number of seconds since 1970 and is used to set the result of
40967** sqlite3OsCurrentTime() during testing.
40968*/
40969#ifdef SQLITE_TEST
40970SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
40971#endif
40972
40973/*
40974** Find the current time (in Universal Coordinated Time). Write into *piNow
40975** the current time and date as a Julian Day number times 86_400_000. In
40976** other words, write into *piNow the number of milliseconds since the Julian
40977** epoch of noon in Greenwich on November 24, 4714 B.C according to the
40978** proleptic Gregorian calendar.
40979**
40980** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
40981** cannot be found.
40982*/
40983static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
40984 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
40985 int rc = SQLITE_OK;
40986#if defined(NO_GETTOD)
40987 time_t t;
40988 time(&t);
40989 *piNow = ((sqlite3_int64)t)*1000 + unixEpoch;
40990#elif OS_VXWORKS
40991 struct timespec sNow;
40992 clock_gettime(CLOCK_REALTIME, &sNow);
40993 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
40994#else
40995 struct timeval sNow;
40996 (void)gettimeofday(tv: &sNow, tz: 0); /* Cannot fail given valid arguments */
40997 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
40998#endif
40999
41000#ifdef SQLITE_TEST
41001 if( sqlite3_current_time ){
41002 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
41003 }
41004#endif
41005 UNUSED_PARAMETER(NotUsed);
41006 return rc;
41007}
41008
41009#ifndef SQLITE_OMIT_DEPRECATED
41010/*
41011** Find the current time (in Universal Coordinated Time). Write the
41012** current time and date as a Julian Day number into *prNow and
41013** return 0. Return 1 if the time and date cannot be found.
41014*/
41015static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
41016 sqlite3_int64 i = 0;
41017 int rc;
41018 UNUSED_PARAMETER(NotUsed);
41019 rc = unixCurrentTimeInt64(NotUsed: 0, piNow: &i);
41020 *prNow = i/86400000.0;
41021 return rc;
41022}
41023#else
41024# define unixCurrentTime 0
41025#endif
41026
41027/*
41028** The xGetLastError() method is designed to return a better
41029** low-level error message when operating-system problems come up
41030** during SQLite operation. Only the integer return code is currently
41031** used.
41032*/
41033static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
41034 UNUSED_PARAMETER(NotUsed);
41035 UNUSED_PARAMETER(NotUsed2);
41036 UNUSED_PARAMETER(NotUsed3);
41037 return errno;
41038}
41039
41040
41041/*
41042************************ End of sqlite3_vfs methods ***************************
41043******************************************************************************/
41044
41045/******************************************************************************
41046************************** Begin Proxy Locking ********************************
41047**
41048** Proxy locking is a "uber-locking-method" in this sense: It uses the
41049** other locking methods on secondary lock files. Proxy locking is a
41050** meta-layer over top of the primitive locking implemented above. For
41051** this reason, the division that implements of proxy locking is deferred
41052** until late in the file (here) after all of the other I/O methods have
41053** been defined - so that the primitive locking methods are available
41054** as services to help with the implementation of proxy locking.
41055**
41056****
41057**
41058** The default locking schemes in SQLite use byte-range locks on the
41059** database file to coordinate safe, concurrent access by multiple readers
41060** and writers [http://sqlite.org/lockingv3.html]. The five file locking
41061** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented
41062** as POSIX read & write locks over fixed set of locations (via fsctl),
41063** on AFP and SMB only exclusive byte-range locks are available via fsctl
41064** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
41065** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
41066** address in the shared range is taken for a SHARED lock, the entire
41067** shared range is taken for an EXCLUSIVE lock):
41068**
41069** PENDING_BYTE 0x40000000
41070** RESERVED_BYTE 0x40000001
41071** SHARED_RANGE 0x40000002 -> 0x40000200
41072**
41073** This works well on the local file system, but shows a nearly 100x
41074** slowdown in read performance on AFP because the AFP client disables
41075** the read cache when byte-range locks are present. Enabling the read
41076** cache exposes a cache coherency problem that is present on all OS X
41077** supported network file systems. NFS and AFP both observe the
41078** close-to-open semantics for ensuring cache coherency
41079** [http://nfs.sourceforge.net/#faq_a8], which does not effectively
41080** address the requirements for concurrent database access by multiple
41081** readers and writers
41082** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
41083**
41084** To address the performance and cache coherency issues, proxy file locking
41085** changes the way database access is controlled by limiting access to a
41086** single host at a time and moving file locks off of the database file
41087** and onto a proxy file on the local file system.
41088**
41089**
41090** Using proxy locks
41091** -----------------
41092**
41093** C APIs
41094**
41095** sqlite3_file_control(db, dbname, SQLITE_FCNTL_SET_LOCKPROXYFILE,
41096** <proxy_path> | ":auto:");
41097** sqlite3_file_control(db, dbname, SQLITE_FCNTL_GET_LOCKPROXYFILE,
41098** &<proxy_path>);
41099**
41100**
41101** SQL pragmas
41102**
41103** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto:
41104** PRAGMA [database.]lock_proxy_file
41105**
41106** Specifying ":auto:" means that if there is a conch file with a matching
41107** host ID in it, the proxy path in the conch file will be used, otherwise
41108** a proxy path based on the user's temp dir
41109** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the
41110** actual proxy file name is generated from the name and path of the
41111** database file. For example:
41112**
41113** For database path "/Users/me/foo.db"
41114** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:")
41115**
41116** Once a lock proxy is configured for a database connection, it can not
41117** be removed, however it may be switched to a different proxy path via
41118** the above APIs (assuming the conch file is not being held by another
41119** connection or process).
41120**
41121**
41122** How proxy locking works
41123** -----------------------
41124**
41125** Proxy file locking relies primarily on two new supporting files:
41126**
41127** * conch file to limit access to the database file to a single host
41128** at a time
41129**
41130** * proxy file to act as a proxy for the advisory locks normally
41131** taken on the database
41132**
41133** The conch file - to use a proxy file, sqlite must first "hold the conch"
41134** by taking an sqlite-style shared lock on the conch file, reading the
41135** contents and comparing the host's unique host ID (see below) and lock
41136** proxy path against the values stored in the conch. The conch file is
41137** stored in the same directory as the database file and the file name
41138** is patterned after the database file name as ".<databasename>-conch".
41139** If the conch file does not exist, or its contents do not match the
41140** host ID and/or proxy path, then the lock is escalated to an exclusive
41141** lock and the conch file contents is updated with the host ID and proxy
41142** path and the lock is downgraded to a shared lock again. If the conch
41143** is held by another process (with a shared lock), the exclusive lock
41144** will fail and SQLITE_BUSY is returned.
41145**
41146** The proxy file - a single-byte file used for all advisory file locks
41147** normally taken on the database file. This allows for safe sharing
41148** of the database file for multiple readers and writers on the same
41149** host (the conch ensures that they all use the same local lock file).
41150**
41151** Requesting the lock proxy does not immediately take the conch, it is
41152** only taken when the first request to lock database file is made.
41153** This matches the semantics of the traditional locking behavior, where
41154** opening a connection to a database file does not take a lock on it.
41155** The shared lock and an open file descriptor are maintained until
41156** the connection to the database is closed.
41157**
41158** The proxy file and the lock file are never deleted so they only need
41159** to be created the first time they are used.
41160**
41161** Configuration options
41162** ---------------------
41163**
41164** SQLITE_PREFER_PROXY_LOCKING
41165**
41166** Database files accessed on non-local file systems are
41167** automatically configured for proxy locking, lock files are
41168** named automatically using the same logic as
41169** PRAGMA lock_proxy_file=":auto:"
41170**
41171** SQLITE_PROXY_DEBUG
41172**
41173** Enables the logging of error messages during host id file
41174** retrieval and creation
41175**
41176** LOCKPROXYDIR
41177**
41178** Overrides the default directory used for lock proxy files that
41179** are named automatically via the ":auto:" setting
41180**
41181** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
41182**
41183** Permissions to use when creating a directory for storing the
41184** lock proxy files, only used when LOCKPROXYDIR is not set.
41185**
41186**
41187** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
41188** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
41189** force proxy locking to be used for every database file opened, and 0
41190** will force automatic proxy locking to be disabled for all database
41191** files (explicitly calling the SQLITE_FCNTL_SET_LOCKPROXYFILE pragma or
41192** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
41193*/
41194
41195/*
41196** Proxy locking is only available on MacOSX
41197*/
41198#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
41199
41200/*
41201** The proxyLockingContext has the path and file structures for the remote
41202** and local proxy files in it
41203*/
41204typedef struct proxyLockingContext proxyLockingContext;
41205struct proxyLockingContext {
41206 unixFile *conchFile; /* Open conch file */
41207 char *conchFilePath; /* Name of the conch file */
41208 unixFile *lockProxy; /* Open proxy lock file */
41209 char *lockProxyPath; /* Name of the proxy lock file */
41210 char *dbPath; /* Name of the open file */
41211 int conchHeld; /* 1 if the conch is held, -1 if lockless */
41212 int nFails; /* Number of conch taking failures */
41213 void *oldLockingContext; /* Original lockingcontext to restore on close */
41214 sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */
41215};
41216
41217/*
41218** The proxy lock file path for the database at dbPath is written into lPath,
41219** which must point to valid, writable memory large enough for a maxLen length
41220** file path.
41221*/
41222static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){
41223 int len;
41224 int dbLen;
41225 int i;
41226
41227#ifdef LOCKPROXYDIR
41228 len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
41229#else
41230# ifdef _CS_DARWIN_USER_TEMP_DIR
41231 {
41232 if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
41233 OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n",
41234 lPath, errno, osGetpid(0)));
41235 return SQLITE_IOERR_LOCK;
41236 }
41237 len = strlcat(lPath, "sqliteplocks", maxLen);
41238 }
41239# else
41240 len = strlcpy(lPath, "/tmp/", maxLen);
41241# endif
41242#endif
41243
41244 if( lPath[len-1]!='/' ){
41245 len = strlcat(lPath, "/", maxLen);
41246 }
41247
41248 /* transform the db path to a unique cache name */
41249 dbLen = (int)strlen(dbPath);
41250 for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){
41251 char c = dbPath[i];
41252 lPath[i+len] = (c=='/')?'_':c;
41253 }
41254 lPath[i+len]='\0';
41255 strlcat(lPath, ":auto:", maxLen);
41256 OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, osGetpid(0)));
41257 return SQLITE_OK;
41258}
41259
41260/*
41261 ** Creates the lock file and any missing directories in lockPath
41262 */
41263static int proxyCreateLockPath(const char *lockPath){
41264 int i, len;
41265 char buf[MAXPATHLEN];
41266 int start = 0;
41267
41268 assert(lockPath!=NULL);
41269 /* try to create all the intermediate directories */
41270 len = (int)strlen(lockPath);
41271 buf[0] = lockPath[0];
41272 for( i=1; i<len; i++ ){
41273 if( lockPath[i] == '/' && (i - start > 0) ){
41274 /* only mkdir if leaf dir != "." or "/" or ".." */
41275 if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
41276 || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
41277 buf[i]='\0';
41278 if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
41279 int err=errno;
41280 if( err!=EEXIST ) {
41281 OSTRACE(("CREATELOCKPATH FAILED creating %s, "
41282 "'%s' proxy lock path=%s pid=%d\n",
41283 buf, strerror(err), lockPath, osGetpid(0)));
41284 return err;
41285 }
41286 }
41287 }
41288 start=i+1;
41289 }
41290 buf[i] = lockPath[i];
41291 }
41292 OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n",lockPath,osGetpid(0)));
41293 return 0;
41294}
41295
41296/*
41297** Create a new VFS file descriptor (stored in memory obtained from
41298** sqlite3_malloc) and open the file named "path" in the file descriptor.
41299**
41300** The caller is responsible not only for closing the file descriptor
41301** but also for freeing the memory associated with the file descriptor.
41302*/
41303static int proxyCreateUnixFile(
41304 const char *path, /* path for the new unixFile */
41305 unixFile **ppFile, /* unixFile created and returned by ref */
41306 int islockfile /* if non zero missing dirs will be created */
41307) {
41308 int fd = -1;
41309 unixFile *pNew;
41310 int rc = SQLITE_OK;
41311 int openFlags = O_RDWR | O_CREAT | O_NOFOLLOW;
41312 sqlite3_vfs dummyVfs;
41313 int terrno = 0;
41314 UnixUnusedFd *pUnused = NULL;
41315
41316 /* 1. first try to open/create the file
41317 ** 2. if that fails, and this is a lock file (not-conch), try creating
41318 ** the parent directories and then try again.
41319 ** 3. if that fails, try to open the file read-only
41320 ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file
41321 */
41322 pUnused = findReusableFd(path, openFlags);
41323 if( pUnused ){
41324 fd = pUnused->fd;
41325 }else{
41326 pUnused = sqlite3_malloc64(sizeof(*pUnused));
41327 if( !pUnused ){
41328 return SQLITE_NOMEM_BKPT;
41329 }
41330 }
41331 if( fd<0 ){
41332 fd = robust_open(path, openFlags, 0);
41333 terrno = errno;
41334 if( fd<0 && errno==ENOENT && islockfile ){
41335 if( proxyCreateLockPath(path) == SQLITE_OK ){
41336 fd = robust_open(path, openFlags, 0);
41337 }
41338 }
41339 }
41340 if( fd<0 ){
41341 openFlags = O_RDONLY | O_NOFOLLOW;
41342 fd = robust_open(path, openFlags, 0);
41343 terrno = errno;
41344 }
41345 if( fd<0 ){
41346 if( islockfile ){
41347 return SQLITE_BUSY;
41348 }
41349 switch (terrno) {
41350 case EACCES:
41351 return SQLITE_PERM;
41352 case EIO:
41353 return SQLITE_IOERR_LOCK; /* even though it is the conch */
41354 default:
41355 return SQLITE_CANTOPEN_BKPT;
41356 }
41357 }
41358
41359 pNew = (unixFile *)sqlite3_malloc64(sizeof(*pNew));
41360 if( pNew==NULL ){
41361 rc = SQLITE_NOMEM_BKPT;
41362 goto end_create_proxy;
41363 }
41364 memset(pNew, 0, sizeof(unixFile));
41365 pNew->openFlags = openFlags;
41366 memset(&dummyVfs, 0, sizeof(dummyVfs));
41367 dummyVfs.pAppData = (void*)&autolockIoFinder;
41368 dummyVfs.zName = "dummy";
41369 pUnused->fd = fd;
41370 pUnused->flags = openFlags;
41371 pNew->pPreallocatedUnused = pUnused;
41372
41373 rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0);
41374 if( rc==SQLITE_OK ){
41375 *ppFile = pNew;
41376 return SQLITE_OK;
41377 }
41378end_create_proxy:
41379 robust_close(pNew, fd, __LINE__);
41380 sqlite3_free(pNew);
41381 sqlite3_free(pUnused);
41382 return rc;
41383}
41384
41385#ifdef SQLITE_TEST
41386/* simulate multiple hosts by creating unique hostid file paths */
41387SQLITE_API int sqlite3_hostid_num = 0;
41388#endif
41389
41390#define PROXY_HOSTIDLEN 16 /* conch file host id length */
41391
41392#if HAVE_GETHOSTUUID
41393/* Not always defined in the headers as it ought to be */
41394extern int gethostuuid(uuid_t id, const struct timespec *wait);
41395#endif
41396
41397/* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN
41398** bytes of writable memory.
41399*/
41400static int proxyGetHostID(unsigned char *pHostID, int *pError){
41401 assert(PROXY_HOSTIDLEN == sizeof(uuid_t));
41402 memset(pHostID, 0, PROXY_HOSTIDLEN);
41403#if HAVE_GETHOSTUUID
41404 {
41405 struct timespec timeout = {1, 0}; /* 1 sec timeout */
41406 if( gethostuuid(pHostID, &timeout) ){
41407 int err = errno;
41408 if( pError ){
41409 *pError = err;
41410 }
41411 return SQLITE_IOERR;
41412 }
41413 }
41414#else
41415 UNUSED_PARAMETER(pError);
41416#endif
41417#ifdef SQLITE_TEST
41418 /* simulate multiple hosts by creating unique hostid file paths */
41419 if( sqlite3_hostid_num != 0){
41420 pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
41421 }
41422#endif
41423
41424 return SQLITE_OK;
41425}
41426
41427/* The conch file contains the header, host id and lock file path
41428 */
41429#define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */
41430#define PROXY_HEADERLEN 1 /* conch file header length */
41431#define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN)
41432#define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN)
41433
41434/*
41435** Takes an open conch file, copies the contents to a new path and then moves
41436** it back. The newly created file's file descriptor is assigned to the
41437** conch file structure and finally the original conch file descriptor is
41438** closed. Returns zero if successful.
41439*/
41440static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){
41441 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
41442 unixFile *conchFile = pCtx->conchFile;
41443 char tPath[MAXPATHLEN];
41444 char buf[PROXY_MAXCONCHLEN];
41445 char *cPath = pCtx->conchFilePath;
41446 size_t readLen = 0;
41447 size_t pathLen = 0;
41448 char errmsg[64] = "";
41449 int fd = -1;
41450 int rc = -1;
41451 UNUSED_PARAMETER(myHostID);
41452
41453 /* create a new path by replace the trailing '-conch' with '-break' */
41454 pathLen = strlcpy(tPath, cPath, MAXPATHLEN);
41455 if( pathLen>MAXPATHLEN || pathLen<6 ||
41456 (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
41457 sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen);
41458 goto end_breaklock;
41459 }
41460 /* read the conch content */
41461 readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
41462 if( readLen<PROXY_PATHINDEX ){
41463 sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen);
41464 goto end_breaklock;
41465 }
41466 /* write it out to the temporary break file */
41467 fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW), 0);
41468 if( fd<0 ){
41469 sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno);
41470 goto end_breaklock;
41471 }
41472 if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){
41473 sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno);
41474 goto end_breaklock;
41475 }
41476 if( rename(tPath, cPath) ){
41477 sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno);
41478 goto end_breaklock;
41479 }
41480 rc = 0;
41481 fprintf(stderr, "broke stale lock on %s\n", cPath);
41482 robust_close(pFile, conchFile->h, __LINE__);
41483 conchFile->h = fd;
41484 conchFile->openFlags = O_RDWR | O_CREAT;
41485
41486end_breaklock:
41487 if( rc ){
41488 if( fd>=0 ){
41489 osUnlink(tPath);
41490 robust_close(pFile, fd, __LINE__);
41491 }
41492 fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
41493 }
41494 return rc;
41495}
41496
41497/* Take the requested lock on the conch file and break a stale lock if the
41498** host id matches.
41499*/
41500static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){
41501 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
41502 unixFile *conchFile = pCtx->conchFile;
41503 int rc = SQLITE_OK;
41504 int nTries = 0;
41505 struct timespec conchModTime;
41506
41507 memset(&conchModTime, 0, sizeof(conchModTime));
41508 do {
41509 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
41510 nTries ++;
41511 if( rc==SQLITE_BUSY ){
41512 /* If the lock failed (busy):
41513 * 1st try: get the mod time of the conch, wait 0.5s and try again.
41514 * 2nd try: fail if the mod time changed or host id is different, wait
41515 * 10 sec and try again
41516 * 3rd try: break the lock unless the mod time has changed.
41517 */
41518 struct stat buf;
41519 if( osFstat(conchFile->h, &buf) ){
41520 storeLastErrno(pFile, errno);
41521 return SQLITE_IOERR_LOCK;
41522 }
41523
41524 if( nTries==1 ){
41525 conchModTime = buf.st_mtimespec;
41526 unixSleep(0,500000); /* wait 0.5 sec and try the lock again*/
41527 continue;
41528 }
41529
41530 assert( nTries>1 );
41531 if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec ||
41532 conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){
41533 return SQLITE_BUSY;
41534 }
41535
41536 if( nTries==2 ){
41537 char tBuf[PROXY_MAXCONCHLEN];
41538 int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
41539 if( len<0 ){
41540 storeLastErrno(pFile, errno);
41541 return SQLITE_IOERR_LOCK;
41542 }
41543 if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){
41544 /* don't break the lock if the host id doesn't match */
41545 if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){
41546 return SQLITE_BUSY;
41547 }
41548 }else{
41549 /* don't break the lock on short read or a version mismatch */
41550 return SQLITE_BUSY;
41551 }
41552 unixSleep(0,10000000); /* wait 10 sec and try the lock again */
41553 continue;
41554 }
41555
41556 assert( nTries==3 );
41557 if( 0==proxyBreakConchLock(pFile, myHostID) ){
41558 rc = SQLITE_OK;
41559 if( lockType==EXCLUSIVE_LOCK ){
41560 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
41561 }
41562 if( !rc ){
41563 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
41564 }
41565 }
41566 }
41567 } while( rc==SQLITE_BUSY && nTries<3 );
41568
41569 return rc;
41570}
41571
41572/* Takes the conch by taking a shared lock and read the contents conch, if
41573** lockPath is non-NULL, the host ID and lock file path must match. A NULL
41574** lockPath means that the lockPath in the conch file will be used if the
41575** host IDs match, or a new lock path will be generated automatically
41576** and written to the conch file.
41577*/
41578static int proxyTakeConch(unixFile *pFile){
41579 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
41580
41581 if( pCtx->conchHeld!=0 ){
41582 return SQLITE_OK;
41583 }else{
41584 unixFile *conchFile = pCtx->conchFile;
41585 uuid_t myHostID;
41586 int pError = 0;
41587 char readBuf[PROXY_MAXCONCHLEN];
41588 char lockPath[MAXPATHLEN];
41589 char *tempLockPath = NULL;
41590 int rc = SQLITE_OK;
41591 int createConch = 0;
41592 int hostIdMatch = 0;
41593 int readLen = 0;
41594 int tryOldLockPath = 0;
41595 int forceNewLockPath = 0;
41596
41597 OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h,
41598 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
41599 osGetpid(0)));
41600
41601 rc = proxyGetHostID(myHostID, &pError);
41602 if( (rc&0xff)==SQLITE_IOERR ){
41603 storeLastErrno(pFile, pError);
41604 goto end_takeconch;
41605 }
41606 rc = proxyConchLock(pFile, myHostID, SHARED_LOCK);
41607 if( rc!=SQLITE_OK ){
41608 goto end_takeconch;
41609 }
41610 /* read the existing conch file */
41611 readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN);
41612 if( readLen<0 ){
41613 /* I/O error: lastErrno set by seekAndRead */
41614 storeLastErrno(pFile, conchFile->lastErrno);
41615 rc = SQLITE_IOERR_READ;
41616 goto end_takeconch;
41617 }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) ||
41618 readBuf[0]!=(char)PROXY_CONCHVERSION ){
41619 /* a short read or version format mismatch means we need to create a new
41620 ** conch file.
41621 */
41622 createConch = 1;
41623 }
41624 /* if the host id matches and the lock path already exists in the conch
41625 ** we'll try to use the path there, if we can't open that path, we'll
41626 ** retry with a new auto-generated path
41627 */
41628 do { /* in case we need to try again for an :auto: named lock file */
41629
41630 if( !createConch && !forceNewLockPath ){
41631 hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID,
41632 PROXY_HOSTIDLEN);
41633 /* if the conch has data compare the contents */
41634 if( !pCtx->lockProxyPath ){
41635 /* for auto-named local lock file, just check the host ID and we'll
41636 ** use the local lock file path that's already in there
41637 */
41638 if( hostIdMatch ){
41639 size_t pathLen = (readLen - PROXY_PATHINDEX);
41640
41641 if( pathLen>=MAXPATHLEN ){
41642 pathLen=MAXPATHLEN-1;
41643 }
41644 memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen);
41645 lockPath[pathLen] = 0;
41646 tempLockPath = lockPath;
41647 tryOldLockPath = 1;
41648 /* create a copy of the lock path if the conch is taken */
41649 goto end_takeconch;
41650 }
41651 }else if( hostIdMatch
41652 && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
41653 readLen-PROXY_PATHINDEX)
41654 ){
41655 /* conch host and lock path match */
41656 goto end_takeconch;
41657 }
41658 }
41659
41660 /* if the conch isn't writable and doesn't match, we can't take it */
41661 if( (conchFile->openFlags&O_RDWR) == 0 ){
41662 rc = SQLITE_BUSY;
41663 goto end_takeconch;
41664 }
41665
41666 /* either the conch didn't match or we need to create a new one */
41667 if( !pCtx->lockProxyPath ){
41668 proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
41669 tempLockPath = lockPath;
41670 /* create a copy of the lock path _only_ if the conch is taken */
41671 }
41672
41673 /* update conch with host and path (this will fail if other process
41674 ** has a shared lock already), if the host id matches, use the big
41675 ** stick.
41676 */
41677 futimes(conchFile->h, NULL);
41678 if( hostIdMatch && !createConch ){
41679 if( conchFile->pInode && conchFile->pInode->nShared>1 ){
41680 /* We are trying for an exclusive lock but another thread in this
41681 ** same process is still holding a shared lock. */
41682 rc = SQLITE_BUSY;
41683 } else {
41684 rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
41685 }
41686 }else{
41687 rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
41688 }
41689 if( rc==SQLITE_OK ){
41690 char writeBuffer[PROXY_MAXCONCHLEN];
41691 int writeSize = 0;
41692
41693 writeBuffer[0] = (char)PROXY_CONCHVERSION;
41694 memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN);
41695 if( pCtx->lockProxyPath!=NULL ){
41696 strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath,
41697 MAXPATHLEN);
41698 }else{
41699 strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN);
41700 }
41701 writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]);
41702 robust_ftruncate(conchFile->h, writeSize);
41703 rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0);
41704 full_fsync(conchFile->h,0,0);
41705 /* If we created a new conch file (not just updated the contents of a
41706 ** valid conch file), try to match the permissions of the database
41707 */
41708 if( rc==SQLITE_OK && createConch ){
41709 struct stat buf;
41710 int err = osFstat(pFile->h, &buf);
41711 if( err==0 ){
41712 mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP |
41713 S_IROTH|S_IWOTH);
41714 /* try to match the database file R/W permissions, ignore failure */
41715#ifndef SQLITE_PROXY_DEBUG
41716 osFchmod(conchFile->h, cmode);
41717#else
41718 do{
41719 rc = osFchmod(conchFile->h, cmode);
41720 }while( rc==(-1) && errno==EINTR );
41721 if( rc!=0 ){
41722 int code = errno;
41723 fprintf(stderr, "fchmod %o FAILED with %d %s\n",
41724 cmode, code, strerror(code));
41725 } else {
41726 fprintf(stderr, "fchmod %o SUCCEDED\n",cmode);
41727 }
41728 }else{
41729 int code = errno;
41730 fprintf(stderr, "STAT FAILED[%d] with %d %s\n",
41731 err, code, strerror(code));
41732#endif
41733 }
41734 }
41735 }
41736 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
41737
41738 end_takeconch:
41739 OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h));
41740 if( rc==SQLITE_OK && pFile->openFlags ){
41741 int fd;
41742 if( pFile->h>=0 ){
41743 robust_close(pFile, pFile->h, __LINE__);
41744 }
41745 pFile->h = -1;
41746 fd = robust_open(pCtx->dbPath, pFile->openFlags, 0);
41747 OSTRACE(("TRANSPROXY: OPEN %d\n", fd));
41748 if( fd>=0 ){
41749 pFile->h = fd;
41750 }else{
41751 rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called
41752 during locking */
41753 }
41754 }
41755 if( rc==SQLITE_OK && !pCtx->lockProxy ){
41756 char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
41757 rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
41758 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){
41759 /* we couldn't create the proxy lock file with the old lock file path
41760 ** so try again via auto-naming
41761 */
41762 forceNewLockPath = 1;
41763 tryOldLockPath = 0;
41764 continue; /* go back to the do {} while start point, try again */
41765 }
41766 }
41767 if( rc==SQLITE_OK ){
41768 /* Need to make a copy of path if we extracted the value
41769 ** from the conch file or the path was allocated on the stack
41770 */
41771 if( tempLockPath ){
41772 pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
41773 if( !pCtx->lockProxyPath ){
41774 rc = SQLITE_NOMEM_BKPT;
41775 }
41776 }
41777 }
41778 if( rc==SQLITE_OK ){
41779 pCtx->conchHeld = 1;
41780
41781 if( pCtx->lockProxy->pMethod == &afpIoMethods ){
41782 afpLockingContext *afpCtx;
41783 afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
41784 afpCtx->dbPath = pCtx->lockProxyPath;
41785 }
41786 } else {
41787 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
41788 }
41789 OSTRACE(("TAKECONCH %d %s\n", conchFile->h,
41790 rc==SQLITE_OK?"ok":"failed"));
41791 return rc;
41792 } while (1); /* in case we need to retry the :auto: lock file -
41793 ** we should never get here except via the 'continue' call. */
41794 }
41795}
41796
41797/*
41798** If pFile holds a lock on a conch file, then release that lock.
41799*/
41800static int proxyReleaseConch(unixFile *pFile){
41801 int rc = SQLITE_OK; /* Subroutine return code */
41802 proxyLockingContext *pCtx; /* The locking context for the proxy lock */
41803 unixFile *conchFile; /* Name of the conch file */
41804
41805 pCtx = (proxyLockingContext *)pFile->lockingContext;
41806 conchFile = pCtx->conchFile;
41807 OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
41808 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
41809 osGetpid(0)));
41810 if( pCtx->conchHeld>0 ){
41811 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
41812 }
41813 pCtx->conchHeld = 0;
41814 OSTRACE(("RELEASECONCH %d %s\n", conchFile->h,
41815 (rc==SQLITE_OK ? "ok" : "failed")));
41816 return rc;
41817}
41818
41819/*
41820** Given the name of a database file, compute the name of its conch file.
41821** Store the conch filename in memory obtained from sqlite3_malloc64().
41822** Make *pConchPath point to the new name. Return SQLITE_OK on success
41823** or SQLITE_NOMEM if unable to obtain memory.
41824**
41825** The caller is responsible for ensuring that the allocated memory
41826** space is eventually freed.
41827**
41828** *pConchPath is set to NULL if a memory allocation error occurs.
41829*/
41830static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
41831 int i; /* Loop counter */
41832 int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
41833 char *conchPath; /* buffer in which to construct conch name */
41834
41835 /* Allocate space for the conch filename and initialize the name to
41836 ** the name of the original database file. */
41837 *pConchPath = conchPath = (char *)sqlite3_malloc64(len + 8);
41838 if( conchPath==0 ){
41839 return SQLITE_NOMEM_BKPT;
41840 }
41841 memcpy(conchPath, dbPath, len+1);
41842
41843 /* now insert a "." before the last / character */
41844 for( i=(len-1); i>=0; i-- ){
41845 if( conchPath[i]=='/' ){
41846 i++;
41847 break;
41848 }
41849 }
41850 conchPath[i]='.';
41851 while ( i<len ){
41852 conchPath[i+1]=dbPath[i];
41853 i++;
41854 }
41855
41856 /* append the "-conch" suffix to the file */
41857 memcpy(&conchPath[i+1], "-conch", 7);
41858 assert( (int)strlen(conchPath) == len+7 );
41859
41860 return SQLITE_OK;
41861}
41862
41863
41864/* Takes a fully configured proxy locking-style unix file and switches
41865** the local lock file path
41866*/
41867static int switchLockProxyPath(unixFile *pFile, const char *path) {
41868 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
41869 char *oldPath = pCtx->lockProxyPath;
41870 int rc = SQLITE_OK;
41871
41872 if( pFile->eFileLock!=NO_LOCK ){
41873 return SQLITE_BUSY;
41874 }
41875
41876 /* nothing to do if the path is NULL, :auto: or matches the existing path */
41877 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
41878 (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
41879 return SQLITE_OK;
41880 }else{
41881 unixFile *lockProxy = pCtx->lockProxy;
41882 pCtx->lockProxy=NULL;
41883 pCtx->conchHeld = 0;
41884 if( lockProxy!=NULL ){
41885 rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
41886 if( rc ) return rc;
41887 sqlite3_free(lockProxy);
41888 }
41889 sqlite3_free(oldPath);
41890 pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
41891 }
41892
41893 return rc;
41894}
41895
41896/*
41897** pFile is a file that has been opened by a prior xOpen call. dbPath
41898** is a string buffer at least MAXPATHLEN+1 characters in size.
41899**
41900** This routine find the filename associated with pFile and writes it
41901** int dbPath.
41902*/
41903static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){
41904#if defined(__APPLE__)
41905 if( pFile->pMethod == &afpIoMethods ){
41906 /* afp style keeps a reference to the db path in the filePath field
41907 ** of the struct */
41908 assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
41909 strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath,
41910 MAXPATHLEN);
41911 } else
41912#endif
41913 if( pFile->pMethod == &dotlockIoMethods ){
41914 /* dot lock style uses the locking context to store the dot lock
41915 ** file path */
41916 int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
41917 memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
41918 }else{
41919 /* all other styles use the locking context to store the db file path */
41920 assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
41921 strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
41922 }
41923 return SQLITE_OK;
41924}
41925
41926/*
41927** Takes an already filled in unix file and alters it so all file locking
41928** will be performed on the local proxy lock file. The following fields
41929** are preserved in the locking context so that they can be restored and
41930** the unix structure properly cleaned up at close time:
41931** ->lockingContext
41932** ->pMethod
41933*/
41934static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
41935 proxyLockingContext *pCtx;
41936 char dbPath[MAXPATHLEN+1]; /* Name of the database file */
41937 char *lockPath=NULL;
41938 int rc = SQLITE_OK;
41939
41940 if( pFile->eFileLock!=NO_LOCK ){
41941 return SQLITE_BUSY;
41942 }
41943 proxyGetDbPathForUnixFile(pFile, dbPath);
41944 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
41945 lockPath=NULL;
41946 }else{
41947 lockPath=(char *)path;
41948 }
41949
41950 OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h,
41951 (lockPath ? lockPath : ":auto:"), osGetpid(0)));
41952
41953 pCtx = sqlite3_malloc64( sizeof(*pCtx) );
41954 if( pCtx==0 ){
41955 return SQLITE_NOMEM_BKPT;
41956 }
41957 memset(pCtx, 0, sizeof(*pCtx));
41958
41959 rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
41960 if( rc==SQLITE_OK ){
41961 rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
41962 if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){
41963 /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and
41964 ** (c) the file system is read-only, then enable no-locking access.
41965 ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts
41966 ** that openFlags will have only one of O_RDONLY or O_RDWR.
41967 */
41968 struct statfs fsInfo;
41969 struct stat conchInfo;
41970 int goLockless = 0;
41971
41972 if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
41973 int err = errno;
41974 if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
41975 goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY;
41976 }
41977 }
41978 if( goLockless ){
41979 pCtx->conchHeld = -1; /* read only FS/ lockless */
41980 rc = SQLITE_OK;
41981 }
41982 }
41983 }
41984 if( rc==SQLITE_OK && lockPath ){
41985 pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
41986 }
41987
41988 if( rc==SQLITE_OK ){
41989 pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
41990 if( pCtx->dbPath==NULL ){
41991 rc = SQLITE_NOMEM_BKPT;
41992 }
41993 }
41994 if( rc==SQLITE_OK ){
41995 /* all memory is allocated, proxys are created and assigned,
41996 ** switch the locking context and pMethod then return.
41997 */
41998 pCtx->oldLockingContext = pFile->lockingContext;
41999 pFile->lockingContext = pCtx;
42000 pCtx->pOldMethod = pFile->pMethod;
42001 pFile->pMethod = &proxyIoMethods;
42002 }else{
42003 if( pCtx->conchFile ){
42004 pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
42005 sqlite3_free(pCtx->conchFile);
42006 }
42007 sqlite3DbFree(0, pCtx->lockProxyPath);
42008 sqlite3_free(pCtx->conchFilePath);
42009 sqlite3_free(pCtx);
42010 }
42011 OSTRACE(("TRANSPROXY %d %s\n", pFile->h,
42012 (rc==SQLITE_OK ? "ok" : "failed")));
42013 return rc;
42014}
42015
42016
42017/*
42018** This routine handles sqlite3_file_control() calls that are specific
42019** to proxy locking.
42020*/
42021static int proxyFileControl(sqlite3_file *id, int op, void *pArg){
42022 switch( op ){
42023 case SQLITE_FCNTL_GET_LOCKPROXYFILE: {
42024 unixFile *pFile = (unixFile*)id;
42025 if( pFile->pMethod == &proxyIoMethods ){
42026 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
42027 proxyTakeConch(pFile);
42028 if( pCtx->lockProxyPath ){
42029 *(const char **)pArg = pCtx->lockProxyPath;
42030 }else{
42031 *(const char **)pArg = ":auto: (not held)";
42032 }
42033 } else {
42034 *(const char **)pArg = NULL;
42035 }
42036 return SQLITE_OK;
42037 }
42038 case SQLITE_FCNTL_SET_LOCKPROXYFILE: {
42039 unixFile *pFile = (unixFile*)id;
42040 int rc = SQLITE_OK;
42041 int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
42042 if( pArg==NULL || (const char *)pArg==0 ){
42043 if( isProxyStyle ){
42044 /* turn off proxy locking - not supported. If support is added for
42045 ** switching proxy locking mode off then it will need to fail if
42046 ** the journal mode is WAL mode.
42047 */
42048 rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
42049 }else{
42050 /* turn off proxy locking - already off - NOOP */
42051 rc = SQLITE_OK;
42052 }
42053 }else{
42054 const char *proxyPath = (const char *)pArg;
42055 if( isProxyStyle ){
42056 proxyLockingContext *pCtx =
42057 (proxyLockingContext*)pFile->lockingContext;
42058 if( !strcmp(pArg, ":auto:")
42059 || (pCtx->lockProxyPath &&
42060 !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
42061 ){
42062 rc = SQLITE_OK;
42063 }else{
42064 rc = switchLockProxyPath(pFile, proxyPath);
42065 }
42066 }else{
42067 /* turn on proxy file locking */
42068 rc = proxyTransformUnixFile(pFile, proxyPath);
42069 }
42070 }
42071 return rc;
42072 }
42073 default: {
42074 assert( 0 ); /* The call assures that only valid opcodes are sent */
42075 }
42076 }
42077 /*NOTREACHED*/ assert(0);
42078 return SQLITE_ERROR;
42079}
42080
42081/*
42082** Within this division (the proxying locking implementation) the procedures
42083** above this point are all utilities. The lock-related methods of the
42084** proxy-locking sqlite3_io_method object follow.
42085*/
42086
42087
42088/*
42089** This routine checks if there is a RESERVED lock held on the specified
42090** file by this or any other process. If such a lock is held, set *pResOut
42091** to a non-zero value otherwise *pResOut is set to zero. The return value
42092** is set to SQLITE_OK unless an I/O error occurs during lock checking.
42093*/
42094static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
42095 unixFile *pFile = (unixFile*)id;
42096 int rc = proxyTakeConch(pFile);
42097 if( rc==SQLITE_OK ){
42098 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
42099 if( pCtx->conchHeld>0 ){
42100 unixFile *proxy = pCtx->lockProxy;
42101 return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
42102 }else{ /* conchHeld < 0 is lockless */
42103 pResOut=0;
42104 }
42105 }
42106 return rc;
42107}
42108
42109/*
42110** Lock the file with the lock specified by parameter eFileLock - one
42111** of the following:
42112**
42113** (1) SHARED_LOCK
42114** (2) RESERVED_LOCK
42115** (3) PENDING_LOCK
42116** (4) EXCLUSIVE_LOCK
42117**
42118** Sometimes when requesting one lock state, additional lock states
42119** are inserted in between. The locking might fail on one of the later
42120** transitions leaving the lock state different from what it started but
42121** still short of its goal. The following chart shows the allowed
42122** transitions and the inserted intermediate states:
42123**
42124** UNLOCKED -> SHARED
42125** SHARED -> RESERVED
42126** SHARED -> (PENDING) -> EXCLUSIVE
42127** RESERVED -> (PENDING) -> EXCLUSIVE
42128** PENDING -> EXCLUSIVE
42129**
42130** This routine will only increase a lock. Use the sqlite3OsUnlock()
42131** routine to lower a locking level.
42132*/
42133static int proxyLock(sqlite3_file *id, int eFileLock) {
42134 unixFile *pFile = (unixFile*)id;
42135 int rc = proxyTakeConch(pFile);
42136 if( rc==SQLITE_OK ){
42137 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
42138 if( pCtx->conchHeld>0 ){
42139 unixFile *proxy = pCtx->lockProxy;
42140 rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
42141 pFile->eFileLock = proxy->eFileLock;
42142 }else{
42143 /* conchHeld < 0 is lockless */
42144 }
42145 }
42146 return rc;
42147}
42148
42149
42150/*
42151** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
42152** must be either NO_LOCK or SHARED_LOCK.
42153**
42154** If the locking level of the file descriptor is already at or below
42155** the requested locking level, this routine is a no-op.
42156*/
42157static int proxyUnlock(sqlite3_file *id, int eFileLock) {
42158 unixFile *pFile = (unixFile*)id;
42159 int rc = proxyTakeConch(pFile);
42160 if( rc==SQLITE_OK ){
42161 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
42162 if( pCtx->conchHeld>0 ){
42163 unixFile *proxy = pCtx->lockProxy;
42164 rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
42165 pFile->eFileLock = proxy->eFileLock;
42166 }else{
42167 /* conchHeld < 0 is lockless */
42168 }
42169 }
42170 return rc;
42171}
42172
42173/*
42174** Close a file that uses proxy locks.
42175*/
42176static int proxyClose(sqlite3_file *id) {
42177 if( ALWAYS(id) ){
42178 unixFile *pFile = (unixFile*)id;
42179 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
42180 unixFile *lockProxy = pCtx->lockProxy;
42181 unixFile *conchFile = pCtx->conchFile;
42182 int rc = SQLITE_OK;
42183
42184 if( lockProxy ){
42185 rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
42186 if( rc ) return rc;
42187 rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
42188 if( rc ) return rc;
42189 sqlite3_free(lockProxy);
42190 pCtx->lockProxy = 0;
42191 }
42192 if( conchFile ){
42193 if( pCtx->conchHeld ){
42194 rc = proxyReleaseConch(pFile);
42195 if( rc ) return rc;
42196 }
42197 rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
42198 if( rc ) return rc;
42199 sqlite3_free(conchFile);
42200 }
42201 sqlite3DbFree(0, pCtx->lockProxyPath);
42202 sqlite3_free(pCtx->conchFilePath);
42203 sqlite3DbFree(0, pCtx->dbPath);
42204 /* restore the original locking context and pMethod then close it */
42205 pFile->lockingContext = pCtx->oldLockingContext;
42206 pFile->pMethod = pCtx->pOldMethod;
42207 sqlite3_free(pCtx);
42208 return pFile->pMethod->xClose(id);
42209 }
42210 return SQLITE_OK;
42211}
42212
42213
42214
42215#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
42216/*
42217** The proxy locking style is intended for use with AFP filesystems.
42218** And since AFP is only supported on MacOSX, the proxy locking is also
42219** restricted to MacOSX.
42220**
42221**
42222******************* End of the proxy lock implementation **********************
42223******************************************************************************/
42224
42225/*
42226** Initialize the operating system interface.
42227**
42228** This routine registers all VFS implementations for unix-like operating
42229** systems. This routine, and the sqlite3_os_end() routine that follows,
42230** should be the only routines in this file that are visible from other
42231** files.
42232**
42233** This routine is called once during SQLite initialization and by a
42234** single thread. The memory allocation and mutex subsystems have not
42235** necessarily been initialized when this routine is called, and so they
42236** should not be used.
42237*/
42238SQLITE_API int sqlite3_os_init(void){
42239 /*
42240 ** The following macro defines an initializer for an sqlite3_vfs object.
42241 ** The name of the VFS is NAME. The pAppData is a pointer to a pointer
42242 ** to the "finder" function. (pAppData is a pointer to a pointer because
42243 ** silly C90 rules prohibit a void* from being cast to a function pointer
42244 ** and so we have to go through the intermediate pointer to avoid problems
42245 ** when compiling with -pedantic-errors on GCC.)
42246 **
42247 ** The FINDER parameter to this macro is the name of the pointer to the
42248 ** finder-function. The finder-function returns a pointer to the
42249 ** sqlite_io_methods object that implements the desired locking
42250 ** behaviors. See the division above that contains the IOMETHODS
42251 ** macro for addition information on finder-functions.
42252 **
42253 ** Most finders simply return a pointer to a fixed sqlite3_io_methods
42254 ** object. But the "autolockIoFinder" available on MacOSX does a little
42255 ** more than that; it looks at the filesystem type that hosts the
42256 ** database file and tries to choose an locking method appropriate for
42257 ** that filesystem time.
42258 */
42259 #define UNIXVFS(VFSNAME, FINDER) { \
42260 3, /* iVersion */ \
42261 sizeof(unixFile), /* szOsFile */ \
42262 MAX_PATHNAME, /* mxPathname */ \
42263 0, /* pNext */ \
42264 VFSNAME, /* zName */ \
42265 (void*)&FINDER, /* pAppData */ \
42266 unixOpen, /* xOpen */ \
42267 unixDelete, /* xDelete */ \
42268 unixAccess, /* xAccess */ \
42269 unixFullPathname, /* xFullPathname */ \
42270 unixDlOpen, /* xDlOpen */ \
42271 unixDlError, /* xDlError */ \
42272 unixDlSym, /* xDlSym */ \
42273 unixDlClose, /* xDlClose */ \
42274 unixRandomness, /* xRandomness */ \
42275 unixSleep, /* xSleep */ \
42276 unixCurrentTime, /* xCurrentTime */ \
42277 unixGetLastError, /* xGetLastError */ \
42278 unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \
42279 unixSetSystemCall, /* xSetSystemCall */ \
42280 unixGetSystemCall, /* xGetSystemCall */ \
42281 unixNextSystemCall, /* xNextSystemCall */ \
42282 }
42283
42284 /*
42285 ** All default VFSes for unix are contained in the following array.
42286 **
42287 ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
42288 ** by the SQLite core when the VFS is registered. So the following
42289 ** array cannot be const.
42290 */
42291 static sqlite3_vfs aVfs[] = {
42292#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
42293 UNIXVFS("unix", autolockIoFinder ),
42294#elif OS_VXWORKS
42295 UNIXVFS("unix", vxworksIoFinder ),
42296#else
42297 UNIXVFS("unix", posixIoFinder ),
42298#endif
42299 UNIXVFS("unix-none", nolockIoFinder ),
42300 UNIXVFS("unix-dotfile", dotlockIoFinder ),
42301 UNIXVFS("unix-excl", posixIoFinder ),
42302#if OS_VXWORKS
42303 UNIXVFS("unix-namedsem", semIoFinder ),
42304#endif
42305#if SQLITE_ENABLE_LOCKING_STYLE || OS_VXWORKS
42306 UNIXVFS("unix-posix", posixIoFinder ),
42307#endif
42308#if SQLITE_ENABLE_LOCKING_STYLE
42309 UNIXVFS("unix-flock", flockIoFinder ),
42310#endif
42311#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
42312 UNIXVFS("unix-afp", afpIoFinder ),
42313 UNIXVFS("unix-nfs", nfsIoFinder ),
42314 UNIXVFS("unix-proxy", proxyIoFinder ),
42315#endif
42316 };
42317 unsigned int i; /* Loop counter */
42318
42319 /* Double-check that the aSyscall[] array has been constructed
42320 ** correctly. See ticket [bb3a86e890c8e96ab] */
42321 assert( ArraySize(aSyscall)==29 );
42322
42323 /* Register all VFSes defined in the aVfs[] array */
42324 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
42325 sqlite3_vfs_register(pVfs: &aVfs[i], makeDflt: i==0);
42326 }
42327 unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
42328
42329#ifndef SQLITE_OMIT_WAL
42330 /* Validate lock assumptions */
42331 assert( SQLITE_SHM_NLOCK==8 ); /* Number of available locks */
42332 assert( UNIX_SHM_BASE==120 ); /* Start of locking area */
42333 /* Locks:
42334 ** WRITE UNIX_SHM_BASE 120
42335 ** CKPT UNIX_SHM_BASE+1 121
42336 ** RECOVER UNIX_SHM_BASE+2 122
42337 ** READ-0 UNIX_SHM_BASE+3 123
42338 ** READ-1 UNIX_SHM_BASE+4 124
42339 ** READ-2 UNIX_SHM_BASE+5 125
42340 ** READ-3 UNIX_SHM_BASE+6 126
42341 ** READ-4 UNIX_SHM_BASE+7 127
42342 ** DMS UNIX_SHM_BASE+8 128
42343 */
42344 assert( UNIX_SHM_DMS==128 ); /* Byte offset of the deadman-switch */
42345#endif
42346
42347 /* Initialize temp file dir array. */
42348 unixTempFileInit();
42349
42350 return SQLITE_OK;
42351}
42352
42353/*
42354** Shutdown the operating system interface.
42355**
42356** Some operating systems might need to do some cleanup in this routine,
42357** to release dynamically allocated objects. But not on unix.
42358** This routine is a no-op for unix.
42359*/
42360SQLITE_API int sqlite3_os_end(void){
42361 unixBigLock = 0;
42362 return SQLITE_OK;
42363}
42364
42365#endif /* SQLITE_OS_UNIX */
42366
42367/************** End of os_unix.c *********************************************/
42368/************** Begin file os_win.c ******************************************/
42369/*
42370** 2004 May 22
42371**
42372** The author disclaims copyright to this source code. In place of
42373** a legal notice, here is a blessing:
42374**
42375** May you do good and not evil.
42376** May you find forgiveness for yourself and forgive others.
42377** May you share freely, never taking more than you give.
42378**
42379******************************************************************************
42380**
42381** This file contains code that is specific to Windows.
42382*/
42383/* #include "sqliteInt.h" */
42384#if SQLITE_OS_WIN /* This file is used for Windows only */
42385
42386/*
42387** Include code that is common to all os_*.c files
42388*/
42389/* #include "os_common.h" */
42390
42391/*
42392** Include the header file for the Windows VFS.
42393*/
42394/* #include "os_win.h" */
42395
42396/*
42397** Compiling and using WAL mode requires several APIs that are only
42398** available in Windows platforms based on the NT kernel.
42399*/
42400#if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
42401# error "WAL mode requires support from the Windows NT kernel, compile\
42402 with SQLITE_OMIT_WAL."
42403#endif
42404
42405#if !SQLITE_OS_WINNT && SQLITE_MAX_MMAP_SIZE>0
42406# error "Memory mapped files require support from the Windows NT kernel,\
42407 compile with SQLITE_MAX_MMAP_SIZE=0."
42408#endif
42409
42410/*
42411** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions
42412** based on the sub-platform)?
42413*/
42414#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(SQLITE_WIN32_NO_ANSI)
42415# define SQLITE_WIN32_HAS_ANSI
42416#endif
42417
42418/*
42419** Are most of the Win32 Unicode APIs available (i.e. with certain exceptions
42420** based on the sub-platform)?
42421*/
42422#if (SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT) && \
42423 !defined(SQLITE_WIN32_NO_WIDE)
42424# define SQLITE_WIN32_HAS_WIDE
42425#endif
42426
42427/*
42428** Make sure at least one set of Win32 APIs is available.
42429*/
42430#if !defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_WIN32_HAS_WIDE)
42431# error "At least one of SQLITE_WIN32_HAS_ANSI and SQLITE_WIN32_HAS_WIDE\
42432 must be defined."
42433#endif
42434
42435/*
42436** Define the required Windows SDK version constants if they are not
42437** already available.
42438*/
42439#ifndef NTDDI_WIN8
42440# define NTDDI_WIN8 0x06020000
42441#endif
42442
42443#ifndef NTDDI_WINBLUE
42444# define NTDDI_WINBLUE 0x06030000
42445#endif
42446
42447#ifndef NTDDI_WINTHRESHOLD
42448# define NTDDI_WINTHRESHOLD 0x06040000
42449#endif
42450
42451/*
42452** Check to see if the GetVersionEx[AW] functions are deprecated on the
42453** target system. GetVersionEx was first deprecated in Win8.1.
42454*/
42455#ifndef SQLITE_WIN32_GETVERSIONEX
42456# if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINBLUE
42457# define SQLITE_WIN32_GETVERSIONEX 0 /* GetVersionEx() is deprecated */
42458# else
42459# define SQLITE_WIN32_GETVERSIONEX 1 /* GetVersionEx() is current */
42460# endif
42461#endif
42462
42463/*
42464** Check to see if the CreateFileMappingA function is supported on the
42465** target system. It is unavailable when using "mincore.lib" on Win10.
42466** When compiling for Windows 10, always assume "mincore.lib" is in use.
42467*/
42468#ifndef SQLITE_WIN32_CREATEFILEMAPPINGA
42469# if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINTHRESHOLD
42470# define SQLITE_WIN32_CREATEFILEMAPPINGA 0
42471# else
42472# define SQLITE_WIN32_CREATEFILEMAPPINGA 1
42473# endif
42474#endif
42475
42476/*
42477** This constant should already be defined (in the "WinDef.h" SDK file).
42478*/
42479#ifndef MAX_PATH
42480# define MAX_PATH (260)
42481#endif
42482
42483/*
42484** Maximum pathname length (in chars) for Win32. This should normally be
42485** MAX_PATH.
42486*/
42487#ifndef SQLITE_WIN32_MAX_PATH_CHARS
42488# define SQLITE_WIN32_MAX_PATH_CHARS (MAX_PATH)
42489#endif
42490
42491/*
42492** This constant should already be defined (in the "WinNT.h" SDK file).
42493*/
42494#ifndef UNICODE_STRING_MAX_CHARS
42495# define UNICODE_STRING_MAX_CHARS (32767)
42496#endif
42497
42498/*
42499** Maximum pathname length (in chars) for WinNT. This should normally be
42500** UNICODE_STRING_MAX_CHARS.
42501*/
42502#ifndef SQLITE_WINNT_MAX_PATH_CHARS
42503# define SQLITE_WINNT_MAX_PATH_CHARS (UNICODE_STRING_MAX_CHARS)
42504#endif
42505
42506/*
42507** Maximum pathname length (in bytes) for Win32. The MAX_PATH macro is in
42508** characters, so we allocate 4 bytes per character assuming worst-case of
42509** 4-bytes-per-character for UTF8.
42510*/
42511#ifndef SQLITE_WIN32_MAX_PATH_BYTES
42512# define SQLITE_WIN32_MAX_PATH_BYTES (SQLITE_WIN32_MAX_PATH_CHARS*4)
42513#endif
42514
42515/*
42516** Maximum pathname length (in bytes) for WinNT. This should normally be
42517** UNICODE_STRING_MAX_CHARS * sizeof(WCHAR).
42518*/
42519#ifndef SQLITE_WINNT_MAX_PATH_BYTES
42520# define SQLITE_WINNT_MAX_PATH_BYTES \
42521 (sizeof(WCHAR) * SQLITE_WINNT_MAX_PATH_CHARS)
42522#endif
42523
42524/*
42525** Maximum error message length (in chars) for WinRT.
42526*/
42527#ifndef SQLITE_WIN32_MAX_ERRMSG_CHARS
42528# define SQLITE_WIN32_MAX_ERRMSG_CHARS (1024)
42529#endif
42530
42531/*
42532** Returns non-zero if the character should be treated as a directory
42533** separator.
42534*/
42535#ifndef winIsDirSep
42536# define winIsDirSep(a) (((a) == '/') || ((a) == '\\'))
42537#endif
42538
42539/*
42540** This macro is used when a local variable is set to a value that is
42541** [sometimes] not used by the code (e.g. via conditional compilation).
42542*/
42543#ifndef UNUSED_VARIABLE_VALUE
42544# define UNUSED_VARIABLE_VALUE(x) (void)(x)
42545#endif
42546
42547/*
42548** Returns the character that should be used as the directory separator.
42549*/
42550#ifndef winGetDirSep
42551# define winGetDirSep() '\\'
42552#endif
42553
42554/*
42555** Do we need to manually define the Win32 file mapping APIs for use with WAL
42556** mode or memory mapped files (e.g. these APIs are available in the Windows
42557** CE SDK; however, they are not present in the header file)?
42558*/
42559#if SQLITE_WIN32_FILEMAPPING_API && \
42560 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
42561/*
42562** Two of the file mapping APIs are different under WinRT. Figure out which
42563** set we need.
42564*/
42565#if SQLITE_OS_WINRT
42566WINBASEAPI HANDLE WINAPI CreateFileMappingFromApp(HANDLE, \
42567 LPSECURITY_ATTRIBUTES, ULONG, ULONG64, LPCWSTR);
42568
42569WINBASEAPI LPVOID WINAPI MapViewOfFileFromApp(HANDLE, ULONG, ULONG64, SIZE_T);
42570#else
42571#if defined(SQLITE_WIN32_HAS_ANSI)
42572WINBASEAPI HANDLE WINAPI CreateFileMappingA(HANDLE, LPSECURITY_ATTRIBUTES, \
42573 DWORD, DWORD, DWORD, LPCSTR);
42574#endif /* defined(SQLITE_WIN32_HAS_ANSI) */
42575
42576#if defined(SQLITE_WIN32_HAS_WIDE)
42577WINBASEAPI HANDLE WINAPI CreateFileMappingW(HANDLE, LPSECURITY_ATTRIBUTES, \
42578 DWORD, DWORD, DWORD, LPCWSTR);
42579#endif /* defined(SQLITE_WIN32_HAS_WIDE) */
42580
42581WINBASEAPI LPVOID WINAPI MapViewOfFile(HANDLE, DWORD, DWORD, DWORD, SIZE_T);
42582#endif /* SQLITE_OS_WINRT */
42583
42584/*
42585** These file mapping APIs are common to both Win32 and WinRT.
42586*/
42587
42588WINBASEAPI BOOL WINAPI FlushViewOfFile(LPCVOID, SIZE_T);
42589WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID);
42590#endif /* SQLITE_WIN32_FILEMAPPING_API */
42591
42592/*
42593** Some Microsoft compilers lack this definition.
42594*/
42595#ifndef INVALID_FILE_ATTRIBUTES
42596# define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
42597#endif
42598
42599#ifndef FILE_FLAG_MASK
42600# define FILE_FLAG_MASK (0xFF3C0000)
42601#endif
42602
42603#ifndef FILE_ATTRIBUTE_MASK
42604# define FILE_ATTRIBUTE_MASK (0x0003FFF7)
42605#endif
42606
42607#ifndef SQLITE_OMIT_WAL
42608/* Forward references to structures used for WAL */
42609typedef struct winShm winShm; /* A connection to shared-memory */
42610typedef struct winShmNode winShmNode; /* A region of shared-memory */
42611#endif
42612
42613/*
42614** WinCE lacks native support for file locking so we have to fake it
42615** with some code of our own.
42616*/
42617#if SQLITE_OS_WINCE
42618typedef struct winceLock {
42619 int nReaders; /* Number of reader locks obtained */
42620 BOOL bPending; /* Indicates a pending lock has been obtained */
42621 BOOL bReserved; /* Indicates a reserved lock has been obtained */
42622 BOOL bExclusive; /* Indicates an exclusive lock has been obtained */
42623} winceLock;
42624#endif
42625
42626/*
42627** The winFile structure is a subclass of sqlite3_file* specific to the win32
42628** portability layer.
42629*/
42630typedef struct winFile winFile;
42631struct winFile {
42632 const sqlite3_io_methods *pMethod; /*** Must be first ***/
42633 sqlite3_vfs *pVfs; /* The VFS used to open this file */
42634 HANDLE h; /* Handle for accessing the file */
42635 u8 locktype; /* Type of lock currently held on this file */
42636 short sharedLockByte; /* Randomly chosen byte used as a shared lock */
42637 u8 ctrlFlags; /* Flags. See WINFILE_* below */
42638 DWORD lastErrno; /* The Windows errno from the last I/O error */
42639#ifndef SQLITE_OMIT_WAL
42640 winShm *pShm; /* Instance of shared memory on this file */
42641#endif
42642 const char *zPath; /* Full pathname of this file */
42643 int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */
42644#if SQLITE_OS_WINCE
42645 LPWSTR zDeleteOnClose; /* Name of file to delete when closing */
42646 HANDLE hMutex; /* Mutex used to control access to shared lock */
42647 HANDLE hShared; /* Shared memory segment used for locking */
42648 winceLock local; /* Locks obtained by this instance of winFile */
42649 winceLock *shared; /* Global shared lock memory for the file */
42650#endif
42651#if SQLITE_MAX_MMAP_SIZE>0
42652 int nFetchOut; /* Number of outstanding xFetch references */
42653 HANDLE hMap; /* Handle for accessing memory mapping */
42654 void *pMapRegion; /* Area memory mapped */
42655 sqlite3_int64 mmapSize; /* Size of mapped region */
42656 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
42657#endif
42658};
42659
42660/*
42661** The winVfsAppData structure is used for the pAppData member for all of the
42662** Win32 VFS variants.
42663*/
42664typedef struct winVfsAppData winVfsAppData;
42665struct winVfsAppData {
42666 const sqlite3_io_methods *pMethod; /* The file I/O methods to use. */
42667 void *pAppData; /* The extra pAppData, if any. */
42668 BOOL bNoLock; /* Non-zero if locking is disabled. */
42669};
42670
42671/*
42672** Allowed values for winFile.ctrlFlags
42673*/
42674#define WINFILE_RDONLY 0x02 /* Connection is read only */
42675#define WINFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
42676#define WINFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
42677
42678/*
42679 * The size of the buffer used by sqlite3_win32_write_debug().
42680 */
42681#ifndef SQLITE_WIN32_DBG_BUF_SIZE
42682# define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD)))
42683#endif
42684
42685/*
42686 * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
42687 * various Win32 API heap functions instead of our own.
42688 */
42689#ifdef SQLITE_WIN32_MALLOC
42690
42691/*
42692 * If this is non-zero, an isolated heap will be created by the native Win32
42693 * allocator subsystem; otherwise, the default process heap will be used. This
42694 * setting has no effect when compiling for WinRT. By default, this is enabled
42695 * and an isolated heap will be created to store all allocated data.
42696 *
42697 ******************************************************************************
42698 * WARNING: It is important to note that when this setting is non-zero and the
42699 * winMemShutdown function is called (e.g. by the sqlite3_shutdown
42700 * function), all data that was allocated using the isolated heap will
42701 * be freed immediately and any attempt to access any of that freed
42702 * data will almost certainly result in an immediate access violation.
42703 ******************************************************************************
42704 */
42705#ifndef SQLITE_WIN32_HEAP_CREATE
42706# define SQLITE_WIN32_HEAP_CREATE (TRUE)
42707#endif
42708
42709/*
42710 * This is the maximum possible initial size of the Win32-specific heap, in
42711 * bytes.
42712 */
42713#ifndef SQLITE_WIN32_HEAP_MAX_INIT_SIZE
42714# define SQLITE_WIN32_HEAP_MAX_INIT_SIZE (4294967295U)
42715#endif
42716
42717/*
42718 * This is the extra space for the initial size of the Win32-specific heap,
42719 * in bytes. This value may be zero.
42720 */
42721#ifndef SQLITE_WIN32_HEAP_INIT_EXTRA
42722# define SQLITE_WIN32_HEAP_INIT_EXTRA (4194304)
42723#endif
42724
42725/*
42726 * Calculate the maximum legal cache size, in pages, based on the maximum
42727 * possible initial heap size and the default page size, setting aside the
42728 * needed extra space.
42729 */
42730#ifndef SQLITE_WIN32_MAX_CACHE_SIZE
42731# define SQLITE_WIN32_MAX_CACHE_SIZE (((SQLITE_WIN32_HEAP_MAX_INIT_SIZE) - \
42732 (SQLITE_WIN32_HEAP_INIT_EXTRA)) / \
42733 (SQLITE_DEFAULT_PAGE_SIZE))
42734#endif
42735
42736/*
42737 * This is cache size used in the calculation of the initial size of the
42738 * Win32-specific heap. It cannot be negative.
42739 */
42740#ifndef SQLITE_WIN32_CACHE_SIZE
42741# if SQLITE_DEFAULT_CACHE_SIZE>=0
42742# define SQLITE_WIN32_CACHE_SIZE (SQLITE_DEFAULT_CACHE_SIZE)
42743# else
42744# define SQLITE_WIN32_CACHE_SIZE (-(SQLITE_DEFAULT_CACHE_SIZE))
42745# endif
42746#endif
42747
42748/*
42749 * Make sure that the calculated cache size, in pages, cannot cause the
42750 * initial size of the Win32-specific heap to exceed the maximum amount
42751 * of memory that can be specified in the call to HeapCreate.
42752 */
42753#if SQLITE_WIN32_CACHE_SIZE>SQLITE_WIN32_MAX_CACHE_SIZE
42754# undef SQLITE_WIN32_CACHE_SIZE
42755# define SQLITE_WIN32_CACHE_SIZE (2000)
42756#endif
42757
42758/*
42759 * The initial size of the Win32-specific heap. This value may be zero.
42760 */
42761#ifndef SQLITE_WIN32_HEAP_INIT_SIZE
42762# define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_WIN32_CACHE_SIZE) * \
42763 (SQLITE_DEFAULT_PAGE_SIZE) + \
42764 (SQLITE_WIN32_HEAP_INIT_EXTRA))
42765#endif
42766
42767/*
42768 * The maximum size of the Win32-specific heap. This value may be zero.
42769 */
42770#ifndef SQLITE_WIN32_HEAP_MAX_SIZE
42771# define SQLITE_WIN32_HEAP_MAX_SIZE (0)
42772#endif
42773
42774/*
42775 * The extra flags to use in calls to the Win32 heap APIs. This value may be
42776 * zero for the default behavior.
42777 */
42778#ifndef SQLITE_WIN32_HEAP_FLAGS
42779# define SQLITE_WIN32_HEAP_FLAGS (0)
42780#endif
42781
42782
42783/*
42784** The winMemData structure stores information required by the Win32-specific
42785** sqlite3_mem_methods implementation.
42786*/
42787typedef struct winMemData winMemData;
42788struct winMemData {
42789#ifndef NDEBUG
42790 u32 magic1; /* Magic number to detect structure corruption. */
42791#endif
42792 HANDLE hHeap; /* The handle to our heap. */
42793 BOOL bOwned; /* Do we own the heap (i.e. destroy it on shutdown)? */
42794#ifndef NDEBUG
42795 u32 magic2; /* Magic number to detect structure corruption. */
42796#endif
42797};
42798
42799#ifndef NDEBUG
42800#define WINMEM_MAGIC1 0x42b2830b
42801#define WINMEM_MAGIC2 0xbd4d7cf4
42802#endif
42803
42804static struct winMemData win_mem_data = {
42805#ifndef NDEBUG
42806 WINMEM_MAGIC1,
42807#endif
42808 NULL, FALSE
42809#ifndef NDEBUG
42810 ,WINMEM_MAGIC2
42811#endif
42812};
42813
42814#ifndef NDEBUG
42815#define winMemAssertMagic1() assert( win_mem_data.magic1==WINMEM_MAGIC1 )
42816#define winMemAssertMagic2() assert( win_mem_data.magic2==WINMEM_MAGIC2 )
42817#define winMemAssertMagic() winMemAssertMagic1(); winMemAssertMagic2();
42818#else
42819#define winMemAssertMagic()
42820#endif
42821
42822#define winMemGetDataPtr() &win_mem_data
42823#define winMemGetHeap() win_mem_data.hHeap
42824#define winMemGetOwned() win_mem_data.bOwned
42825
42826static void *winMemMalloc(int nBytes);
42827static void winMemFree(void *pPrior);
42828static void *winMemRealloc(void *pPrior, int nBytes);
42829static int winMemSize(void *p);
42830static int winMemRoundup(int n);
42831static int winMemInit(void *pAppData);
42832static void winMemShutdown(void *pAppData);
42833
42834SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void);
42835#endif /* SQLITE_WIN32_MALLOC */
42836
42837/*
42838** The following variable is (normally) set once and never changes
42839** thereafter. It records whether the operating system is Win9x
42840** or WinNT.
42841**
42842** 0: Operating system unknown.
42843** 1: Operating system is Win9x.
42844** 2: Operating system is WinNT.
42845**
42846** In order to facilitate testing on a WinNT system, the test fixture
42847** can manually set this value to 1 to emulate Win98 behavior.
42848*/
42849#ifdef SQLITE_TEST
42850SQLITE_API LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0;
42851#else
42852static LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0;
42853#endif
42854
42855#ifndef SYSCALL
42856# define SYSCALL sqlite3_syscall_ptr
42857#endif
42858
42859/*
42860** This function is not available on Windows CE or WinRT.
42861 */
42862
42863#if SQLITE_OS_WINCE || SQLITE_OS_WINRT
42864# define osAreFileApisANSI() 1
42865#endif
42866
42867/*
42868** Many system calls are accessed through pointer-to-functions so that
42869** they may be overridden at runtime to facilitate fault injection during
42870** testing and sandboxing. The following array holds the names and pointers
42871** to all overrideable system calls.
42872*/
42873static struct win_syscall {
42874 const char *zName; /* Name of the system call */
42875 sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
42876 sqlite3_syscall_ptr pDefault; /* Default value */
42877} aSyscall[] = {
42878#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
42879 { "AreFileApisANSI", (SYSCALL)AreFileApisANSI, 0 },
42880#else
42881 { "AreFileApisANSI", (SYSCALL)0, 0 },
42882#endif
42883
42884#ifndef osAreFileApisANSI
42885#define osAreFileApisANSI ((BOOL(WINAPI*)(VOID))aSyscall[0].pCurrent)
42886#endif
42887
42888#if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
42889 { "CharLowerW", (SYSCALL)CharLowerW, 0 },
42890#else
42891 { "CharLowerW", (SYSCALL)0, 0 },
42892#endif
42893
42894#define osCharLowerW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[1].pCurrent)
42895
42896#if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
42897 { "CharUpperW", (SYSCALL)CharUpperW, 0 },
42898#else
42899 { "CharUpperW", (SYSCALL)0, 0 },
42900#endif
42901
42902#define osCharUpperW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[2].pCurrent)
42903
42904 { "CloseHandle", (SYSCALL)CloseHandle, 0 },
42905
42906#define osCloseHandle ((BOOL(WINAPI*)(HANDLE))aSyscall[3].pCurrent)
42907
42908#if defined(SQLITE_WIN32_HAS_ANSI)
42909 { "CreateFileA", (SYSCALL)CreateFileA, 0 },
42910#else
42911 { "CreateFileA", (SYSCALL)0, 0 },
42912#endif
42913
42914#define osCreateFileA ((HANDLE(WINAPI*)(LPCSTR,DWORD,DWORD, \
42915 LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[4].pCurrent)
42916
42917#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
42918 { "CreateFileW", (SYSCALL)CreateFileW, 0 },
42919#else
42920 { "CreateFileW", (SYSCALL)0, 0 },
42921#endif
42922
42923#define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
42924 LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
42925
42926#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \
42927 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) && \
42928 SQLITE_WIN32_CREATEFILEMAPPINGA
42929 { "CreateFileMappingA", (SYSCALL)CreateFileMappingA, 0 },
42930#else
42931 { "CreateFileMappingA", (SYSCALL)0, 0 },
42932#endif
42933
42934#define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
42935 DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent)
42936
42937#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
42938 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0))
42939 { "CreateFileMappingW", (SYSCALL)CreateFileMappingW, 0 },
42940#else
42941 { "CreateFileMappingW", (SYSCALL)0, 0 },
42942#endif
42943
42944#define osCreateFileMappingW ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
42945 DWORD,DWORD,DWORD,LPCWSTR))aSyscall[7].pCurrent)
42946
42947#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
42948 { "CreateMutexW", (SYSCALL)CreateMutexW, 0 },
42949#else
42950 { "CreateMutexW", (SYSCALL)0, 0 },
42951#endif
42952
42953#define osCreateMutexW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,BOOL, \
42954 LPCWSTR))aSyscall[8].pCurrent)
42955
42956#if defined(SQLITE_WIN32_HAS_ANSI)
42957 { "DeleteFileA", (SYSCALL)DeleteFileA, 0 },
42958#else
42959 { "DeleteFileA", (SYSCALL)0, 0 },
42960#endif
42961
42962#define osDeleteFileA ((BOOL(WINAPI*)(LPCSTR))aSyscall[9].pCurrent)
42963
42964#if defined(SQLITE_WIN32_HAS_WIDE)
42965 { "DeleteFileW", (SYSCALL)DeleteFileW, 0 },
42966#else
42967 { "DeleteFileW", (SYSCALL)0, 0 },
42968#endif
42969
42970#define osDeleteFileW ((BOOL(WINAPI*)(LPCWSTR))aSyscall[10].pCurrent)
42971
42972#if SQLITE_OS_WINCE
42973 { "FileTimeToLocalFileTime", (SYSCALL)FileTimeToLocalFileTime, 0 },
42974#else
42975 { "FileTimeToLocalFileTime", (SYSCALL)0, 0 },
42976#endif
42977
42978#define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(CONST FILETIME*, \
42979 LPFILETIME))aSyscall[11].pCurrent)
42980
42981#if SQLITE_OS_WINCE
42982 { "FileTimeToSystemTime", (SYSCALL)FileTimeToSystemTime, 0 },
42983#else
42984 { "FileTimeToSystemTime", (SYSCALL)0, 0 },
42985#endif
42986
42987#define osFileTimeToSystemTime ((BOOL(WINAPI*)(CONST FILETIME*, \
42988 LPSYSTEMTIME))aSyscall[12].pCurrent)
42989
42990 { "FlushFileBuffers", (SYSCALL)FlushFileBuffers, 0 },
42991
42992#define osFlushFileBuffers ((BOOL(WINAPI*)(HANDLE))aSyscall[13].pCurrent)
42993
42994#if defined(SQLITE_WIN32_HAS_ANSI)
42995 { "FormatMessageA", (SYSCALL)FormatMessageA, 0 },
42996#else
42997 { "FormatMessageA", (SYSCALL)0, 0 },
42998#endif
42999
43000#define osFormatMessageA ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPSTR, \
43001 DWORD,va_list*))aSyscall[14].pCurrent)
43002
43003#if defined(SQLITE_WIN32_HAS_WIDE)
43004 { "FormatMessageW", (SYSCALL)FormatMessageW, 0 },
43005#else
43006 { "FormatMessageW", (SYSCALL)0, 0 },
43007#endif
43008
43009#define osFormatMessageW ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPWSTR, \
43010 DWORD,va_list*))aSyscall[15].pCurrent)
43011
43012#if !defined(SQLITE_OMIT_LOAD_EXTENSION)
43013 { "FreeLibrary", (SYSCALL)FreeLibrary, 0 },
43014#else
43015 { "FreeLibrary", (SYSCALL)0, 0 },
43016#endif
43017
43018#define osFreeLibrary ((BOOL(WINAPI*)(HMODULE))aSyscall[16].pCurrent)
43019
43020 { "GetCurrentProcessId", (SYSCALL)GetCurrentProcessId, 0 },
43021
43022#define osGetCurrentProcessId ((DWORD(WINAPI*)(VOID))aSyscall[17].pCurrent)
43023
43024#if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
43025 { "GetDiskFreeSpaceA", (SYSCALL)GetDiskFreeSpaceA, 0 },
43026#else
43027 { "GetDiskFreeSpaceA", (SYSCALL)0, 0 },
43028#endif
43029
43030#define osGetDiskFreeSpaceA ((BOOL(WINAPI*)(LPCSTR,LPDWORD,LPDWORD,LPDWORD, \
43031 LPDWORD))aSyscall[18].pCurrent)
43032
43033#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
43034 { "GetDiskFreeSpaceW", (SYSCALL)GetDiskFreeSpaceW, 0 },
43035#else
43036 { "GetDiskFreeSpaceW", (SYSCALL)0, 0 },
43037#endif
43038
43039#define osGetDiskFreeSpaceW ((BOOL(WINAPI*)(LPCWSTR,LPDWORD,LPDWORD,LPDWORD, \
43040 LPDWORD))aSyscall[19].pCurrent)
43041
43042#if defined(SQLITE_WIN32_HAS_ANSI)
43043 { "GetFileAttributesA", (SYSCALL)GetFileAttributesA, 0 },
43044#else
43045 { "GetFileAttributesA", (SYSCALL)0, 0 },
43046#endif
43047
43048#define osGetFileAttributesA ((DWORD(WINAPI*)(LPCSTR))aSyscall[20].pCurrent)
43049
43050#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
43051 { "GetFileAttributesW", (SYSCALL)GetFileAttributesW, 0 },
43052#else
43053 { "GetFileAttributesW", (SYSCALL)0, 0 },
43054#endif
43055
43056#define osGetFileAttributesW ((DWORD(WINAPI*)(LPCWSTR))aSyscall[21].pCurrent)
43057
43058#if defined(SQLITE_WIN32_HAS_WIDE)
43059 { "GetFileAttributesExW", (SYSCALL)GetFileAttributesExW, 0 },
43060#else
43061 { "GetFileAttributesExW", (SYSCALL)0, 0 },
43062#endif
43063
43064#define osGetFileAttributesExW ((BOOL(WINAPI*)(LPCWSTR,GET_FILEEX_INFO_LEVELS, \
43065 LPVOID))aSyscall[22].pCurrent)
43066
43067#if !SQLITE_OS_WINRT
43068 { "GetFileSize", (SYSCALL)GetFileSize, 0 },
43069#else
43070 { "GetFileSize", (SYSCALL)0, 0 },
43071#endif
43072
43073#define osGetFileSize ((DWORD(WINAPI*)(HANDLE,LPDWORD))aSyscall[23].pCurrent)
43074
43075#if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
43076 { "GetFullPathNameA", (SYSCALL)GetFullPathNameA, 0 },
43077#else
43078 { "GetFullPathNameA", (SYSCALL)0, 0 },
43079#endif
43080
43081#define osGetFullPathNameA ((DWORD(WINAPI*)(LPCSTR,DWORD,LPSTR, \
43082 LPSTR*))aSyscall[24].pCurrent)
43083
43084#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
43085 { "GetFullPathNameW", (SYSCALL)GetFullPathNameW, 0 },
43086#else
43087 { "GetFullPathNameW", (SYSCALL)0, 0 },
43088#endif
43089
43090#define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \
43091 LPWSTR*))aSyscall[25].pCurrent)
43092
43093 { "GetLastError", (SYSCALL)GetLastError, 0 },
43094
43095#define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent)
43096
43097#if !defined(SQLITE_OMIT_LOAD_EXTENSION)
43098#if SQLITE_OS_WINCE
43099 /* The GetProcAddressA() routine is only available on Windows CE. */
43100 { "GetProcAddressA", (SYSCALL)GetProcAddressA, 0 },
43101#else
43102 /* All other Windows platforms expect GetProcAddress() to take
43103 ** an ANSI string regardless of the _UNICODE setting */
43104 { "GetProcAddressA", (SYSCALL)GetProcAddress, 0 },
43105#endif
43106#else
43107 { "GetProcAddressA", (SYSCALL)0, 0 },
43108#endif
43109
43110#define osGetProcAddressA ((FARPROC(WINAPI*)(HMODULE, \
43111 LPCSTR))aSyscall[27].pCurrent)
43112
43113#if !SQLITE_OS_WINRT
43114 { "GetSystemInfo", (SYSCALL)GetSystemInfo, 0 },
43115#else
43116 { "GetSystemInfo", (SYSCALL)0, 0 },
43117#endif
43118
43119#define osGetSystemInfo ((VOID(WINAPI*)(LPSYSTEM_INFO))aSyscall[28].pCurrent)
43120
43121 { "GetSystemTime", (SYSCALL)GetSystemTime, 0 },
43122
43123#define osGetSystemTime ((VOID(WINAPI*)(LPSYSTEMTIME))aSyscall[29].pCurrent)
43124
43125#if !SQLITE_OS_WINCE
43126 { "GetSystemTimeAsFileTime", (SYSCALL)GetSystemTimeAsFileTime, 0 },
43127#else
43128 { "GetSystemTimeAsFileTime", (SYSCALL)0, 0 },
43129#endif
43130
43131#define osGetSystemTimeAsFileTime ((VOID(WINAPI*)( \
43132 LPFILETIME))aSyscall[30].pCurrent)
43133
43134#if defined(SQLITE_WIN32_HAS_ANSI)
43135 { "GetTempPathA", (SYSCALL)GetTempPathA, 0 },
43136#else
43137 { "GetTempPathA", (SYSCALL)0, 0 },
43138#endif
43139
43140#define osGetTempPathA ((DWORD(WINAPI*)(DWORD,LPSTR))aSyscall[31].pCurrent)
43141
43142#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
43143 { "GetTempPathW", (SYSCALL)GetTempPathW, 0 },
43144#else
43145 { "GetTempPathW", (SYSCALL)0, 0 },
43146#endif
43147
43148#define osGetTempPathW ((DWORD(WINAPI*)(DWORD,LPWSTR))aSyscall[32].pCurrent)
43149
43150#if !SQLITE_OS_WINRT
43151 { "GetTickCount", (SYSCALL)GetTickCount, 0 },
43152#else
43153 { "GetTickCount", (SYSCALL)0, 0 },
43154#endif
43155
43156#define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent)
43157
43158#if defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_GETVERSIONEX
43159 { "GetVersionExA", (SYSCALL)GetVersionExA, 0 },
43160#else
43161 { "GetVersionExA", (SYSCALL)0, 0 },
43162#endif
43163
43164#define osGetVersionExA ((BOOL(WINAPI*)( \
43165 LPOSVERSIONINFOA))aSyscall[34].pCurrent)
43166
43167#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
43168 SQLITE_WIN32_GETVERSIONEX
43169 { "GetVersionExW", (SYSCALL)GetVersionExW, 0 },
43170#else
43171 { "GetVersionExW", (SYSCALL)0, 0 },
43172#endif
43173
43174#define osGetVersionExW ((BOOL(WINAPI*)( \
43175 LPOSVERSIONINFOW))aSyscall[35].pCurrent)
43176
43177 { "HeapAlloc", (SYSCALL)HeapAlloc, 0 },
43178
43179#define osHeapAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD, \
43180 SIZE_T))aSyscall[36].pCurrent)
43181
43182#if !SQLITE_OS_WINRT
43183 { "HeapCreate", (SYSCALL)HeapCreate, 0 },
43184#else
43185 { "HeapCreate", (SYSCALL)0, 0 },
43186#endif
43187
43188#define osHeapCreate ((HANDLE(WINAPI*)(DWORD,SIZE_T, \
43189 SIZE_T))aSyscall[37].pCurrent)
43190
43191#if !SQLITE_OS_WINRT
43192 { "HeapDestroy", (SYSCALL)HeapDestroy, 0 },
43193#else
43194 { "HeapDestroy", (SYSCALL)0, 0 },
43195#endif
43196
43197#define osHeapDestroy ((BOOL(WINAPI*)(HANDLE))aSyscall[38].pCurrent)
43198
43199 { "HeapFree", (SYSCALL)HeapFree, 0 },
43200
43201#define osHeapFree ((BOOL(WINAPI*)(HANDLE,DWORD,LPVOID))aSyscall[39].pCurrent)
43202
43203 { "HeapReAlloc", (SYSCALL)HeapReAlloc, 0 },
43204
43205#define osHeapReAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD,LPVOID, \
43206 SIZE_T))aSyscall[40].pCurrent)
43207
43208 { "HeapSize", (SYSCALL)HeapSize, 0 },
43209
43210#define osHeapSize ((SIZE_T(WINAPI*)(HANDLE,DWORD, \
43211 LPCVOID))aSyscall[41].pCurrent)
43212
43213#if !SQLITE_OS_WINRT
43214 { "HeapValidate", (SYSCALL)HeapValidate, 0 },
43215#else
43216 { "HeapValidate", (SYSCALL)0, 0 },
43217#endif
43218
43219#define osHeapValidate ((BOOL(WINAPI*)(HANDLE,DWORD, \
43220 LPCVOID))aSyscall[42].pCurrent)
43221
43222#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
43223 { "HeapCompact", (SYSCALL)HeapCompact, 0 },
43224#else
43225 { "HeapCompact", (SYSCALL)0, 0 },
43226#endif
43227
43228#define osHeapCompact ((UINT(WINAPI*)(HANDLE,DWORD))aSyscall[43].pCurrent)
43229
43230#if defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
43231 { "LoadLibraryA", (SYSCALL)LoadLibraryA, 0 },
43232#else
43233 { "LoadLibraryA", (SYSCALL)0, 0 },
43234#endif
43235
43236#define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[44].pCurrent)
43237
43238#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
43239 !defined(SQLITE_OMIT_LOAD_EXTENSION)
43240 { "LoadLibraryW", (SYSCALL)LoadLibraryW, 0 },
43241#else
43242 { "LoadLibraryW", (SYSCALL)0, 0 },
43243#endif
43244
43245#define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[45].pCurrent)
43246
43247#if !SQLITE_OS_WINRT
43248 { "LocalFree", (SYSCALL)LocalFree, 0 },
43249#else
43250 { "LocalFree", (SYSCALL)0, 0 },
43251#endif
43252
43253#define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[46].pCurrent)
43254
43255#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
43256 { "LockFile", (SYSCALL)LockFile, 0 },
43257#else
43258 { "LockFile", (SYSCALL)0, 0 },
43259#endif
43260
43261#ifndef osLockFile
43262#define osLockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
43263 DWORD))aSyscall[47].pCurrent)
43264#endif
43265
43266#if !SQLITE_OS_WINCE
43267 { "LockFileEx", (SYSCALL)LockFileEx, 0 },
43268#else
43269 { "LockFileEx", (SYSCALL)0, 0 },
43270#endif
43271
43272#ifndef osLockFileEx
43273#define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \
43274 LPOVERLAPPED))aSyscall[48].pCurrent)
43275#endif
43276
43277#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && \
43278 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0))
43279 { "MapViewOfFile", (SYSCALL)MapViewOfFile, 0 },
43280#else
43281 { "MapViewOfFile", (SYSCALL)0, 0 },
43282#endif
43283
43284#define osMapViewOfFile ((LPVOID(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
43285 SIZE_T))aSyscall[49].pCurrent)
43286
43287 { "MultiByteToWideChar", (SYSCALL)MultiByteToWideChar, 0 },
43288
43289#define osMultiByteToWideChar ((int(WINAPI*)(UINT,DWORD,LPCSTR,int,LPWSTR, \
43290 int))aSyscall[50].pCurrent)
43291
43292 { "QueryPerformanceCounter", (SYSCALL)QueryPerformanceCounter, 0 },
43293
43294#define osQueryPerformanceCounter ((BOOL(WINAPI*)( \
43295 LARGE_INTEGER*))aSyscall[51].pCurrent)
43296
43297 { "ReadFile", (SYSCALL)ReadFile, 0 },
43298
43299#define osReadFile ((BOOL(WINAPI*)(HANDLE,LPVOID,DWORD,LPDWORD, \
43300 LPOVERLAPPED))aSyscall[52].pCurrent)
43301
43302 { "SetEndOfFile", (SYSCALL)SetEndOfFile, 0 },
43303
43304#define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[53].pCurrent)
43305
43306#if !SQLITE_OS_WINRT
43307 { "SetFilePointer", (SYSCALL)SetFilePointer, 0 },
43308#else
43309 { "SetFilePointer", (SYSCALL)0, 0 },
43310#endif
43311
43312#define osSetFilePointer ((DWORD(WINAPI*)(HANDLE,LONG,PLONG, \
43313 DWORD))aSyscall[54].pCurrent)
43314
43315#if !SQLITE_OS_WINRT
43316 { "Sleep", (SYSCALL)Sleep, 0 },
43317#else
43318 { "Sleep", (SYSCALL)0, 0 },
43319#endif
43320
43321#define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[55].pCurrent)
43322
43323 { "SystemTimeToFileTime", (SYSCALL)SystemTimeToFileTime, 0 },
43324
43325#define osSystemTimeToFileTime ((BOOL(WINAPI*)(CONST SYSTEMTIME*, \
43326 LPFILETIME))aSyscall[56].pCurrent)
43327
43328#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
43329 { "UnlockFile", (SYSCALL)UnlockFile, 0 },
43330#else
43331 { "UnlockFile", (SYSCALL)0, 0 },
43332#endif
43333
43334#ifndef osUnlockFile
43335#define osUnlockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
43336 DWORD))aSyscall[57].pCurrent)
43337#endif
43338
43339#if !SQLITE_OS_WINCE
43340 { "UnlockFileEx", (SYSCALL)UnlockFileEx, 0 },
43341#else
43342 { "UnlockFileEx", (SYSCALL)0, 0 },
43343#endif
43344
43345#define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
43346 LPOVERLAPPED))aSyscall[58].pCurrent)
43347
43348#if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
43349 { "UnmapViewOfFile", (SYSCALL)UnmapViewOfFile, 0 },
43350#else
43351 { "UnmapViewOfFile", (SYSCALL)0, 0 },
43352#endif
43353
43354#define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[59].pCurrent)
43355
43356 { "WideCharToMultiByte", (SYSCALL)WideCharToMultiByte, 0 },
43357
43358#define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \
43359 LPCSTR,LPBOOL))aSyscall[60].pCurrent)
43360
43361 { "WriteFile", (SYSCALL)WriteFile, 0 },
43362
43363#define osWriteFile ((BOOL(WINAPI*)(HANDLE,LPCVOID,DWORD,LPDWORD, \
43364 LPOVERLAPPED))aSyscall[61].pCurrent)
43365
43366#if SQLITE_OS_WINRT
43367 { "CreateEventExW", (SYSCALL)CreateEventExW, 0 },
43368#else
43369 { "CreateEventExW", (SYSCALL)0, 0 },
43370#endif
43371
43372#define osCreateEventExW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,LPCWSTR, \
43373 DWORD,DWORD))aSyscall[62].pCurrent)
43374
43375#if !SQLITE_OS_WINRT
43376 { "WaitForSingleObject", (SYSCALL)WaitForSingleObject, 0 },
43377#else
43378 { "WaitForSingleObject", (SYSCALL)0, 0 },
43379#endif
43380
43381#define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \
43382 DWORD))aSyscall[63].pCurrent)
43383
43384#if !SQLITE_OS_WINCE
43385 { "WaitForSingleObjectEx", (SYSCALL)WaitForSingleObjectEx, 0 },
43386#else
43387 { "WaitForSingleObjectEx", (SYSCALL)0, 0 },
43388#endif
43389
43390#define osWaitForSingleObjectEx ((DWORD(WINAPI*)(HANDLE,DWORD, \
43391 BOOL))aSyscall[64].pCurrent)
43392
43393#if SQLITE_OS_WINRT
43394 { "SetFilePointerEx", (SYSCALL)SetFilePointerEx, 0 },
43395#else
43396 { "SetFilePointerEx", (SYSCALL)0, 0 },
43397#endif
43398
43399#define osSetFilePointerEx ((BOOL(WINAPI*)(HANDLE,LARGE_INTEGER, \
43400 PLARGE_INTEGER,DWORD))aSyscall[65].pCurrent)
43401
43402#if SQLITE_OS_WINRT
43403 { "GetFileInformationByHandleEx", (SYSCALL)GetFileInformationByHandleEx, 0 },
43404#else
43405 { "GetFileInformationByHandleEx", (SYSCALL)0, 0 },
43406#endif
43407
43408#define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \
43409 FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[66].pCurrent)
43410
43411#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
43412 { "MapViewOfFileFromApp", (SYSCALL)MapViewOfFileFromApp, 0 },
43413#else
43414 { "MapViewOfFileFromApp", (SYSCALL)0, 0 },
43415#endif
43416
43417#define osMapViewOfFileFromApp ((LPVOID(WINAPI*)(HANDLE,ULONG,ULONG64, \
43418 SIZE_T))aSyscall[67].pCurrent)
43419
43420#if SQLITE_OS_WINRT
43421 { "CreateFile2", (SYSCALL)CreateFile2, 0 },
43422#else
43423 { "CreateFile2", (SYSCALL)0, 0 },
43424#endif
43425
43426#define osCreateFile2 ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD,DWORD, \
43427 LPCREATEFILE2_EXTENDED_PARAMETERS))aSyscall[68].pCurrent)
43428
43429#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_LOAD_EXTENSION)
43430 { "LoadPackagedLibrary", (SYSCALL)LoadPackagedLibrary, 0 },
43431#else
43432 { "LoadPackagedLibrary", (SYSCALL)0, 0 },
43433#endif
43434
43435#define osLoadPackagedLibrary ((HMODULE(WINAPI*)(LPCWSTR, \
43436 DWORD))aSyscall[69].pCurrent)
43437
43438#if SQLITE_OS_WINRT
43439 { "GetTickCount64", (SYSCALL)GetTickCount64, 0 },
43440#else
43441 { "GetTickCount64", (SYSCALL)0, 0 },
43442#endif
43443
43444#define osGetTickCount64 ((ULONGLONG(WINAPI*)(VOID))aSyscall[70].pCurrent)
43445
43446#if SQLITE_OS_WINRT
43447 { "GetNativeSystemInfo", (SYSCALL)GetNativeSystemInfo, 0 },
43448#else
43449 { "GetNativeSystemInfo", (SYSCALL)0, 0 },
43450#endif
43451
43452#define osGetNativeSystemInfo ((VOID(WINAPI*)( \
43453 LPSYSTEM_INFO))aSyscall[71].pCurrent)
43454
43455#if defined(SQLITE_WIN32_HAS_ANSI)
43456 { "OutputDebugStringA", (SYSCALL)OutputDebugStringA, 0 },
43457#else
43458 { "OutputDebugStringA", (SYSCALL)0, 0 },
43459#endif
43460
43461#define osOutputDebugStringA ((VOID(WINAPI*)(LPCSTR))aSyscall[72].pCurrent)
43462
43463#if defined(SQLITE_WIN32_HAS_WIDE)
43464 { "OutputDebugStringW", (SYSCALL)OutputDebugStringW, 0 },
43465#else
43466 { "OutputDebugStringW", (SYSCALL)0, 0 },
43467#endif
43468
43469#define osOutputDebugStringW ((VOID(WINAPI*)(LPCWSTR))aSyscall[73].pCurrent)
43470
43471 { "GetProcessHeap", (SYSCALL)GetProcessHeap, 0 },
43472
43473#define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[74].pCurrent)
43474
43475#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
43476 { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 },
43477#else
43478 { "CreateFileMappingFromApp", (SYSCALL)0, 0 },
43479#endif
43480
43481#define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
43482 LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[75].pCurrent)
43483
43484/*
43485** NOTE: On some sub-platforms, the InterlockedCompareExchange "function"
43486** is really just a macro that uses a compiler intrinsic (e.g. x64).
43487** So do not try to make this is into a redefinable interface.
43488*/
43489#if defined(InterlockedCompareExchange)
43490 { "InterlockedCompareExchange", (SYSCALL)0, 0 },
43491
43492#define osInterlockedCompareExchange InterlockedCompareExchange
43493#else
43494 { "InterlockedCompareExchange", (SYSCALL)InterlockedCompareExchange, 0 },
43495
43496#define osInterlockedCompareExchange ((LONG(WINAPI*)(LONG \
43497 SQLITE_WIN32_VOLATILE*, LONG,LONG))aSyscall[76].pCurrent)
43498#endif /* defined(InterlockedCompareExchange) */
43499
43500#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
43501 { "UuidCreate", (SYSCALL)UuidCreate, 0 },
43502#else
43503 { "UuidCreate", (SYSCALL)0, 0 },
43504#endif
43505
43506#define osUuidCreate ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[77].pCurrent)
43507
43508#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
43509 { "UuidCreateSequential", (SYSCALL)UuidCreateSequential, 0 },
43510#else
43511 { "UuidCreateSequential", (SYSCALL)0, 0 },
43512#endif
43513
43514#define osUuidCreateSequential \
43515 ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[78].pCurrent)
43516
43517#if !defined(SQLITE_NO_SYNC) && SQLITE_MAX_MMAP_SIZE>0
43518 { "FlushViewOfFile", (SYSCALL)FlushViewOfFile, 0 },
43519#else
43520 { "FlushViewOfFile", (SYSCALL)0, 0 },
43521#endif
43522
43523#define osFlushViewOfFile \
43524 ((BOOL(WINAPI*)(LPCVOID,SIZE_T))aSyscall[79].pCurrent)
43525
43526}; /* End of the overrideable system calls */
43527
43528/*
43529** This is the xSetSystemCall() method of sqlite3_vfs for all of the
43530** "win32" VFSes. Return SQLITE_OK opon successfully updating the
43531** system call pointer, or SQLITE_NOTFOUND if there is no configurable
43532** system call named zName.
43533*/
43534static int winSetSystemCall(
43535 sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */
43536 const char *zName, /* Name of system call to override */
43537 sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */
43538){
43539 unsigned int i;
43540 int rc = SQLITE_NOTFOUND;
43541
43542 UNUSED_PARAMETER(pNotUsed);
43543 if( zName==0 ){
43544 /* If no zName is given, restore all system calls to their default
43545 ** settings and return NULL
43546 */
43547 rc = SQLITE_OK;
43548 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
43549 if( aSyscall[i].pDefault ){
43550 aSyscall[i].pCurrent = aSyscall[i].pDefault;
43551 }
43552 }
43553 }else{
43554 /* If zName is specified, operate on only the one system call
43555 ** specified.
43556 */
43557 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
43558 if( strcmp(zName, aSyscall[i].zName)==0 ){
43559 if( aSyscall[i].pDefault==0 ){
43560 aSyscall[i].pDefault = aSyscall[i].pCurrent;
43561 }
43562 rc = SQLITE_OK;
43563 if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
43564 aSyscall[i].pCurrent = pNewFunc;
43565 break;
43566 }
43567 }
43568 }
43569 return rc;
43570}
43571
43572/*
43573** Return the value of a system call. Return NULL if zName is not a
43574** recognized system call name. NULL is also returned if the system call
43575** is currently undefined.
43576*/
43577static sqlite3_syscall_ptr winGetSystemCall(
43578 sqlite3_vfs *pNotUsed,
43579 const char *zName
43580){
43581 unsigned int i;
43582
43583 UNUSED_PARAMETER(pNotUsed);
43584 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
43585 if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
43586 }
43587 return 0;
43588}
43589
43590/*
43591** Return the name of the first system call after zName. If zName==NULL
43592** then return the name of the first system call. Return NULL if zName
43593** is the last system call or if zName is not the name of a valid
43594** system call.
43595*/
43596static const char *winNextSystemCall(sqlite3_vfs *p, const char *zName){
43597 int i = -1;
43598
43599 UNUSED_PARAMETER(p);
43600 if( zName ){
43601 for(i=0; i<ArraySize(aSyscall)-1; i++){
43602 if( strcmp(zName, aSyscall[i].zName)==0 ) break;
43603 }
43604 }
43605 for(i++; i<ArraySize(aSyscall); i++){
43606 if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
43607 }
43608 return 0;
43609}
43610
43611#ifdef SQLITE_WIN32_MALLOC
43612/*
43613** If a Win32 native heap has been configured, this function will attempt to
43614** compact it. Upon success, SQLITE_OK will be returned. Upon failure, one
43615** of SQLITE_NOMEM, SQLITE_ERROR, or SQLITE_NOTFOUND will be returned. The
43616** "pnLargest" argument, if non-zero, will be used to return the size of the
43617** largest committed free block in the heap, in bytes.
43618*/
43619SQLITE_API int sqlite3_win32_compact_heap(LPUINT pnLargest){
43620 int rc = SQLITE_OK;
43621 UINT nLargest = 0;
43622 HANDLE hHeap;
43623
43624 winMemAssertMagic();
43625 hHeap = winMemGetHeap();
43626 assert( hHeap!=0 );
43627 assert( hHeap!=INVALID_HANDLE_VALUE );
43628#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
43629 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
43630#endif
43631#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
43632 if( (nLargest=osHeapCompact(hHeap, SQLITE_WIN32_HEAP_FLAGS))==0 ){
43633 DWORD lastErrno = osGetLastError();
43634 if( lastErrno==NO_ERROR ){
43635 sqlite3_log(SQLITE_NOMEM, "failed to HeapCompact (no space), heap=%p",
43636 (void*)hHeap);
43637 rc = SQLITE_NOMEM_BKPT;
43638 }else{
43639 sqlite3_log(SQLITE_ERROR, "failed to HeapCompact (%lu), heap=%p",
43640 osGetLastError(), (void*)hHeap);
43641 rc = SQLITE_ERROR;
43642 }
43643 }
43644#else
43645 sqlite3_log(SQLITE_NOTFOUND, "failed to HeapCompact, heap=%p",
43646 (void*)hHeap);
43647 rc = SQLITE_NOTFOUND;
43648#endif
43649 if( pnLargest ) *pnLargest = nLargest;
43650 return rc;
43651}
43652
43653/*
43654** If a Win32 native heap has been configured, this function will attempt to
43655** destroy and recreate it. If the Win32 native heap is not isolated and/or
43656** the sqlite3_memory_used() function does not return zero, SQLITE_BUSY will
43657** be returned and no changes will be made to the Win32 native heap.
43658*/
43659SQLITE_API int sqlite3_win32_reset_heap(){
43660 int rc;
43661 MUTEX_LOGIC( sqlite3_mutex *pMainMtx; ) /* The main static mutex */
43662 MUTEX_LOGIC( sqlite3_mutex *pMem; ) /* The memsys static mutex */
43663 MUTEX_LOGIC( pMainMtx = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )
43664 MUTEX_LOGIC( pMem = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); )
43665 sqlite3_mutex_enter(pMainMtx);
43666 sqlite3_mutex_enter(pMem);
43667 winMemAssertMagic();
43668 if( winMemGetHeap()!=NULL && winMemGetOwned() && sqlite3_memory_used()==0 ){
43669 /*
43670 ** At this point, there should be no outstanding memory allocations on
43671 ** the heap. Also, since both the main and memsys locks are currently
43672 ** being held by us, no other function (i.e. from another thread) should
43673 ** be able to even access the heap. Attempt to destroy and recreate our
43674 ** isolated Win32 native heap now.
43675 */
43676 assert( winMemGetHeap()!=NULL );
43677 assert( winMemGetOwned() );
43678 assert( sqlite3_memory_used()==0 );
43679 winMemShutdown(winMemGetDataPtr());
43680 assert( winMemGetHeap()==NULL );
43681 assert( !winMemGetOwned() );
43682 assert( sqlite3_memory_used()==0 );
43683 rc = winMemInit(winMemGetDataPtr());
43684 assert( rc!=SQLITE_OK || winMemGetHeap()!=NULL );
43685 assert( rc!=SQLITE_OK || winMemGetOwned() );
43686 assert( rc!=SQLITE_OK || sqlite3_memory_used()==0 );
43687 }else{
43688 /*
43689 ** The Win32 native heap cannot be modified because it may be in use.
43690 */
43691 rc = SQLITE_BUSY;
43692 }
43693 sqlite3_mutex_leave(pMem);
43694 sqlite3_mutex_leave(pMainMtx);
43695 return rc;
43696}
43697#endif /* SQLITE_WIN32_MALLOC */
43698
43699/*
43700** This function outputs the specified (ANSI) string to the Win32 debugger
43701** (if available).
43702*/
43703
43704SQLITE_API void sqlite3_win32_write_debug(const char *zBuf, int nBuf){
43705 char zDbgBuf[SQLITE_WIN32_DBG_BUF_SIZE];
43706 int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */
43707 if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
43708 assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE );
43709#ifdef SQLITE_ENABLE_API_ARMOR
43710 if( !zBuf ){
43711 (void)SQLITE_MISUSE_BKPT;
43712 return;
43713 }
43714#endif
43715#if defined(SQLITE_WIN32_HAS_ANSI)
43716 if( nMin>0 ){
43717 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
43718 memcpy(zDbgBuf, zBuf, nMin);
43719 osOutputDebugStringA(zDbgBuf);
43720 }else{
43721 osOutputDebugStringA(zBuf);
43722 }
43723#elif defined(SQLITE_WIN32_HAS_WIDE)
43724 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
43725 if ( osMultiByteToWideChar(
43726 osAreFileApisANSI() ? CP_ACP : CP_OEMCP, 0, zBuf,
43727 nMin, (LPWSTR)zDbgBuf, SQLITE_WIN32_DBG_BUF_SIZE/sizeof(WCHAR))<=0 ){
43728 return;
43729 }
43730 osOutputDebugStringW((LPCWSTR)zDbgBuf);
43731#else
43732 if( nMin>0 ){
43733 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
43734 memcpy(zDbgBuf, zBuf, nMin);
43735 fprintf(stderr, "%s", zDbgBuf);
43736 }else{
43737 fprintf(stderr, "%s", zBuf);
43738 }
43739#endif
43740}
43741
43742/*
43743** The following routine suspends the current thread for at least ms
43744** milliseconds. This is equivalent to the Win32 Sleep() interface.
43745*/
43746#if SQLITE_OS_WINRT
43747static HANDLE sleepObj = NULL;
43748#endif
43749
43750SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds){
43751#if SQLITE_OS_WINRT
43752 if ( sleepObj==NULL ){
43753 sleepObj = osCreateEventExW(NULL, NULL, CREATE_EVENT_MANUAL_RESET,
43754 SYNCHRONIZE);
43755 }
43756 assert( sleepObj!=NULL );
43757 osWaitForSingleObjectEx(sleepObj, milliseconds, FALSE);
43758#else
43759 osSleep(milliseconds);
43760#endif
43761}
43762
43763#if SQLITE_MAX_WORKER_THREADS>0 && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
43764 SQLITE_THREADSAFE>0
43765SQLITE_PRIVATE DWORD sqlite3Win32Wait(HANDLE hObject){
43766 DWORD rc;
43767 while( (rc = osWaitForSingleObjectEx(hObject, INFINITE,
43768 TRUE))==WAIT_IO_COMPLETION ){}
43769 return rc;
43770}
43771#endif
43772
43773/*
43774** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
43775** or WinCE. Return false (zero) for Win95, Win98, or WinME.
43776**
43777** Here is an interesting observation: Win95, Win98, and WinME lack
43778** the LockFileEx() API. But we can still statically link against that
43779** API as long as we don't call it when running Win95/98/ME. A call to
43780** this routine is used to determine if the host is Win95/98/ME or
43781** WinNT/2K/XP so that we will know whether or not we can safely call
43782** the LockFileEx() API.
43783*/
43784
43785#if !SQLITE_WIN32_GETVERSIONEX
43786# define osIsNT() (1)
43787#elif SQLITE_OS_WINCE || SQLITE_OS_WINRT || !defined(SQLITE_WIN32_HAS_ANSI)
43788# define osIsNT() (1)
43789#elif !defined(SQLITE_WIN32_HAS_WIDE)
43790# define osIsNT() (0)
43791#else
43792# define osIsNT() ((sqlite3_os_type==2) || sqlite3_win32_is_nt())
43793#endif
43794
43795/*
43796** This function determines if the machine is running a version of Windows
43797** based on the NT kernel.
43798*/
43799SQLITE_API int sqlite3_win32_is_nt(void){
43800#if SQLITE_OS_WINRT
43801 /*
43802 ** NOTE: The WinRT sub-platform is always assumed to be based on the NT
43803 ** kernel.
43804 */
43805 return 1;
43806#elif SQLITE_WIN32_GETVERSIONEX
43807 if( osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0 ){
43808#if defined(SQLITE_WIN32_HAS_ANSI)
43809 OSVERSIONINFOA sInfo;
43810 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
43811 osGetVersionExA(&sInfo);
43812 osInterlockedCompareExchange(&sqlite3_os_type,
43813 (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
43814#elif defined(SQLITE_WIN32_HAS_WIDE)
43815 OSVERSIONINFOW sInfo;
43816 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
43817 osGetVersionExW(&sInfo);
43818 osInterlockedCompareExchange(&sqlite3_os_type,
43819 (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
43820#endif
43821 }
43822 return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
43823#elif SQLITE_TEST
43824 return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
43825#else
43826 /*
43827 ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are
43828 ** deprecated are always assumed to be based on the NT kernel.
43829 */
43830 return 1;
43831#endif
43832}
43833
43834#ifdef SQLITE_WIN32_MALLOC
43835/*
43836** Allocate nBytes of memory.
43837*/
43838static void *winMemMalloc(int nBytes){
43839 HANDLE hHeap;
43840 void *p;
43841
43842 winMemAssertMagic();
43843 hHeap = winMemGetHeap();
43844 assert( hHeap!=0 );
43845 assert( hHeap!=INVALID_HANDLE_VALUE );
43846#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
43847 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
43848#endif
43849 assert( nBytes>=0 );
43850 p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
43851 if( !p ){
43852 sqlite3_log(SQLITE_NOMEM, "failed to HeapAlloc %u bytes (%lu), heap=%p",
43853 nBytes, osGetLastError(), (void*)hHeap);
43854 }
43855 return p;
43856}
43857
43858/*
43859** Free memory.
43860*/
43861static void winMemFree(void *pPrior){
43862 HANDLE hHeap;
43863
43864 winMemAssertMagic();
43865 hHeap = winMemGetHeap();
43866 assert( hHeap!=0 );
43867 assert( hHeap!=INVALID_HANDLE_VALUE );
43868#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
43869 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
43870#endif
43871 if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */
43872 if( !osHeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){
43873 sqlite3_log(SQLITE_NOMEM, "failed to HeapFree block %p (%lu), heap=%p",
43874 pPrior, osGetLastError(), (void*)hHeap);
43875 }
43876}
43877
43878/*
43879** Change the size of an existing memory allocation
43880*/
43881static void *winMemRealloc(void *pPrior, int nBytes){
43882 HANDLE hHeap;
43883 void *p;
43884
43885 winMemAssertMagic();
43886 hHeap = winMemGetHeap();
43887 assert( hHeap!=0 );
43888 assert( hHeap!=INVALID_HANDLE_VALUE );
43889#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
43890 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
43891#endif
43892 assert( nBytes>=0 );
43893 if( !pPrior ){
43894 p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
43895 }else{
43896 p = osHeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes);
43897 }
43898 if( !p ){
43899 sqlite3_log(SQLITE_NOMEM, "failed to %s %u bytes (%lu), heap=%p",
43900 pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, osGetLastError(),
43901 (void*)hHeap);
43902 }
43903 return p;
43904}
43905
43906/*
43907** Return the size of an outstanding allocation, in bytes.
43908*/
43909static int winMemSize(void *p){
43910 HANDLE hHeap;
43911 SIZE_T n;
43912
43913 winMemAssertMagic();
43914 hHeap = winMemGetHeap();
43915 assert( hHeap!=0 );
43916 assert( hHeap!=INVALID_HANDLE_VALUE );
43917#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
43918 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, p) );
43919#endif
43920 if( !p ) return 0;
43921 n = osHeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p);
43922 if( n==(SIZE_T)-1 ){
43923 sqlite3_log(SQLITE_NOMEM, "failed to HeapSize block %p (%lu), heap=%p",
43924 p, osGetLastError(), (void*)hHeap);
43925 return 0;
43926 }
43927 return (int)n;
43928}
43929
43930/*
43931** Round up a request size to the next valid allocation size.
43932*/
43933static int winMemRoundup(int n){
43934 return n;
43935}
43936
43937/*
43938** Initialize this module.
43939*/
43940static int winMemInit(void *pAppData){
43941 winMemData *pWinMemData = (winMemData *)pAppData;
43942
43943 if( !pWinMemData ) return SQLITE_ERROR;
43944 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
43945 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
43946
43947#if !SQLITE_OS_WINRT && SQLITE_WIN32_HEAP_CREATE
43948 if( !pWinMemData->hHeap ){
43949 DWORD dwInitialSize = SQLITE_WIN32_HEAP_INIT_SIZE;
43950 DWORD dwMaximumSize = (DWORD)sqlite3GlobalConfig.nHeap;
43951 if( dwMaximumSize==0 ){
43952 dwMaximumSize = SQLITE_WIN32_HEAP_MAX_SIZE;
43953 }else if( dwInitialSize>dwMaximumSize ){
43954 dwInitialSize = dwMaximumSize;
43955 }
43956 pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
43957 dwInitialSize, dwMaximumSize);
43958 if( !pWinMemData->hHeap ){
43959 sqlite3_log(SQLITE_NOMEM,
43960 "failed to HeapCreate (%lu), flags=%u, initSize=%lu, maxSize=%lu",
43961 osGetLastError(), SQLITE_WIN32_HEAP_FLAGS, dwInitialSize,
43962 dwMaximumSize);
43963 return SQLITE_NOMEM_BKPT;
43964 }
43965 pWinMemData->bOwned = TRUE;
43966 assert( pWinMemData->bOwned );
43967 }
43968#else
43969 pWinMemData->hHeap = osGetProcessHeap();
43970 if( !pWinMemData->hHeap ){
43971 sqlite3_log(SQLITE_NOMEM,
43972 "failed to GetProcessHeap (%lu)", osGetLastError());
43973 return SQLITE_NOMEM_BKPT;
43974 }
43975 pWinMemData->bOwned = FALSE;
43976 assert( !pWinMemData->bOwned );
43977#endif
43978 assert( pWinMemData->hHeap!=0 );
43979 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
43980#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
43981 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
43982#endif
43983 return SQLITE_OK;
43984}
43985
43986/*
43987** Deinitialize this module.
43988*/
43989static void winMemShutdown(void *pAppData){
43990 winMemData *pWinMemData = (winMemData *)pAppData;
43991
43992 if( !pWinMemData ) return;
43993 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
43994 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
43995
43996 if( pWinMemData->hHeap ){
43997 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
43998#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
43999 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
44000#endif
44001 if( pWinMemData->bOwned ){
44002 if( !osHeapDestroy(pWinMemData->hHeap) ){
44003 sqlite3_log(SQLITE_NOMEM, "failed to HeapDestroy (%lu), heap=%p",
44004 osGetLastError(), (void*)pWinMemData->hHeap);
44005 }
44006 pWinMemData->bOwned = FALSE;
44007 }
44008 pWinMemData->hHeap = NULL;
44009 }
44010}
44011
44012/*
44013** Populate the low-level memory allocation function pointers in
44014** sqlite3GlobalConfig.m with pointers to the routines in this file. The
44015** arguments specify the block of memory to manage.
44016**
44017** This routine is only called by sqlite3_config(), and therefore
44018** is not required to be threadsafe (it is not).
44019*/
44020SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void){
44021 static const sqlite3_mem_methods winMemMethods = {
44022 winMemMalloc,
44023 winMemFree,
44024 winMemRealloc,
44025 winMemSize,
44026 winMemRoundup,
44027 winMemInit,
44028 winMemShutdown,
44029 &win_mem_data
44030 };
44031 return &winMemMethods;
44032}
44033
44034SQLITE_PRIVATE void sqlite3MemSetDefault(void){
44035 sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetWin32());
44036}
44037#endif /* SQLITE_WIN32_MALLOC */
44038
44039/*
44040** Convert a UTF-8 string to Microsoft Unicode.
44041**
44042** Space to hold the returned string is obtained from sqlite3_malloc().
44043*/
44044static LPWSTR winUtf8ToUnicode(const char *zText){
44045 int nChar;
44046 LPWSTR zWideText;
44047
44048 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL, 0);
44049 if( nChar==0 ){
44050 return 0;
44051 }
44052 zWideText = sqlite3MallocZero( nChar*sizeof(WCHAR) );
44053 if( zWideText==0 ){
44054 return 0;
44055 }
44056 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText,
44057 nChar);
44058 if( nChar==0 ){
44059 sqlite3_free(zWideText);
44060 zWideText = 0;
44061 }
44062 return zWideText;
44063}
44064
44065/*
44066** Convert a Microsoft Unicode string to UTF-8.
44067**
44068** Space to hold the returned string is obtained from sqlite3_malloc().
44069*/
44070static char *winUnicodeToUtf8(LPCWSTR zWideText){
44071 int nByte;
44072 char *zText;
44073
44074 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0);
44075 if( nByte == 0 ){
44076 return 0;
44077 }
44078 zText = sqlite3MallocZero( nByte );
44079 if( zText==0 ){
44080 return 0;
44081 }
44082 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte,
44083 0, 0);
44084 if( nByte == 0 ){
44085 sqlite3_free(zText);
44086 zText = 0;
44087 }
44088 return zText;
44089}
44090
44091/*
44092** Convert an ANSI string to Microsoft Unicode, using the ANSI or OEM
44093** code page.
44094**
44095** Space to hold the returned string is obtained from sqlite3_malloc().
44096*/
44097static LPWSTR winMbcsToUnicode(const char *zText, int useAnsi){
44098 int nByte;
44099 LPWSTR zMbcsText;
44100 int codepage = useAnsi ? CP_ACP : CP_OEMCP;
44101
44102 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, NULL,
44103 0)*sizeof(WCHAR);
44104 if( nByte==0 ){
44105 return 0;
44106 }
44107 zMbcsText = sqlite3MallocZero( nByte*sizeof(WCHAR) );
44108 if( zMbcsText==0 ){
44109 return 0;
44110 }
44111 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText,
44112 nByte);
44113 if( nByte==0 ){
44114 sqlite3_free(zMbcsText);
44115 zMbcsText = 0;
44116 }
44117 return zMbcsText;
44118}
44119
44120/*
44121** Convert a Microsoft Unicode string to a multi-byte character string,
44122** using the ANSI or OEM code page.
44123**
44124** Space to hold the returned string is obtained from sqlite3_malloc().
44125*/
44126static char *winUnicodeToMbcs(LPCWSTR zWideText, int useAnsi){
44127 int nByte;
44128 char *zText;
44129 int codepage = useAnsi ? CP_ACP : CP_OEMCP;
44130
44131 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0);
44132 if( nByte == 0 ){
44133 return 0;
44134 }
44135 zText = sqlite3MallocZero( nByte );
44136 if( zText==0 ){
44137 return 0;
44138 }
44139 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText,
44140 nByte, 0, 0);
44141 if( nByte == 0 ){
44142 sqlite3_free(zText);
44143 zText = 0;
44144 }
44145 return zText;
44146}
44147
44148/*
44149** Convert a multi-byte character string to UTF-8.
44150**
44151** Space to hold the returned string is obtained from sqlite3_malloc().
44152*/
44153static char *winMbcsToUtf8(const char *zText, int useAnsi){
44154 char *zTextUtf8;
44155 LPWSTR zTmpWide;
44156
44157 zTmpWide = winMbcsToUnicode(zText, useAnsi);
44158 if( zTmpWide==0 ){
44159 return 0;
44160 }
44161 zTextUtf8 = winUnicodeToUtf8(zTmpWide);
44162 sqlite3_free(zTmpWide);
44163 return zTextUtf8;
44164}
44165
44166/*
44167** Convert a UTF-8 string to a multi-byte character string.
44168**
44169** Space to hold the returned string is obtained from sqlite3_malloc().
44170*/
44171static char *winUtf8ToMbcs(const char *zText, int useAnsi){
44172 char *zTextMbcs;
44173 LPWSTR zTmpWide;
44174
44175 zTmpWide = winUtf8ToUnicode(zText);
44176 if( zTmpWide==0 ){
44177 return 0;
44178 }
44179 zTextMbcs = winUnicodeToMbcs(zTmpWide, useAnsi);
44180 sqlite3_free(zTmpWide);
44181 return zTextMbcs;
44182}
44183
44184/*
44185** This is a public wrapper for the winUtf8ToUnicode() function.
44186*/
44187SQLITE_API LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText){
44188#ifdef SQLITE_ENABLE_API_ARMOR
44189 if( !zText ){
44190 (void)SQLITE_MISUSE_BKPT;
44191 return 0;
44192 }
44193#endif
44194#ifndef SQLITE_OMIT_AUTOINIT
44195 if( sqlite3_initialize() ) return 0;
44196#endif
44197 return winUtf8ToUnicode(zText);
44198}
44199
44200/*
44201** This is a public wrapper for the winUnicodeToUtf8() function.
44202*/
44203SQLITE_API char *sqlite3_win32_unicode_to_utf8(LPCWSTR zWideText){
44204#ifdef SQLITE_ENABLE_API_ARMOR
44205 if( !zWideText ){
44206 (void)SQLITE_MISUSE_BKPT;
44207 return 0;
44208 }
44209#endif
44210#ifndef SQLITE_OMIT_AUTOINIT
44211 if( sqlite3_initialize() ) return 0;
44212#endif
44213 return winUnicodeToUtf8(zWideText);
44214}
44215
44216/*
44217** This is a public wrapper for the winMbcsToUtf8() function.
44218*/
44219SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zText){
44220#ifdef SQLITE_ENABLE_API_ARMOR
44221 if( !zText ){
44222 (void)SQLITE_MISUSE_BKPT;
44223 return 0;
44224 }
44225#endif
44226#ifndef SQLITE_OMIT_AUTOINIT
44227 if( sqlite3_initialize() ) return 0;
44228#endif
44229 return winMbcsToUtf8(zText, osAreFileApisANSI());
44230}
44231
44232/*
44233** This is a public wrapper for the winMbcsToUtf8() function.
44234*/
44235SQLITE_API char *sqlite3_win32_mbcs_to_utf8_v2(const char *zText, int useAnsi){
44236#ifdef SQLITE_ENABLE_API_ARMOR
44237 if( !zText ){
44238 (void)SQLITE_MISUSE_BKPT;
44239 return 0;
44240 }
44241#endif
44242#ifndef SQLITE_OMIT_AUTOINIT
44243 if( sqlite3_initialize() ) return 0;
44244#endif
44245 return winMbcsToUtf8(zText, useAnsi);
44246}
44247
44248/*
44249** This is a public wrapper for the winUtf8ToMbcs() function.
44250*/
44251SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zText){
44252#ifdef SQLITE_ENABLE_API_ARMOR
44253 if( !zText ){
44254 (void)SQLITE_MISUSE_BKPT;
44255 return 0;
44256 }
44257#endif
44258#ifndef SQLITE_OMIT_AUTOINIT
44259 if( sqlite3_initialize() ) return 0;
44260#endif
44261 return winUtf8ToMbcs(zText, osAreFileApisANSI());
44262}
44263
44264/*
44265** This is a public wrapper for the winUtf8ToMbcs() function.
44266*/
44267SQLITE_API char *sqlite3_win32_utf8_to_mbcs_v2(const char *zText, int useAnsi){
44268#ifdef SQLITE_ENABLE_API_ARMOR
44269 if( !zText ){
44270 (void)SQLITE_MISUSE_BKPT;
44271 return 0;
44272 }
44273#endif
44274#ifndef SQLITE_OMIT_AUTOINIT
44275 if( sqlite3_initialize() ) return 0;
44276#endif
44277 return winUtf8ToMbcs(zText, useAnsi);
44278}
44279
44280/*
44281** This function is the same as sqlite3_win32_set_directory (below); however,
44282** it accepts a UTF-8 string.
44283*/
44284SQLITE_API int sqlite3_win32_set_directory8(
44285 unsigned long type, /* Identifier for directory being set or reset */
44286 const char *zValue /* New value for directory being set or reset */
44287){
44288 char **ppDirectory = 0;
44289#ifndef SQLITE_OMIT_AUTOINIT
44290 int rc = sqlite3_initialize();
44291 if( rc ) return rc;
44292#endif
44293 if( type==SQLITE_WIN32_DATA_DIRECTORY_TYPE ){
44294 ppDirectory = &sqlite3_data_directory;
44295 }else if( type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE ){
44296 ppDirectory = &sqlite3_temp_directory;
44297 }
44298 assert( !ppDirectory || type==SQLITE_WIN32_DATA_DIRECTORY_TYPE
44299 || type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE
44300 );
44301 assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) );
44302 if( ppDirectory ){
44303 char *zCopy = 0;
44304 if( zValue && zValue[0] ){
44305 zCopy = sqlite3_mprintf("%s", zValue);
44306 if ( zCopy==0 ){
44307 return SQLITE_NOMEM_BKPT;
44308 }
44309 }
44310 sqlite3_free(*ppDirectory);
44311 *ppDirectory = zCopy;
44312 return SQLITE_OK;
44313 }
44314 return SQLITE_ERROR;
44315}
44316
44317/*
44318** This function is the same as sqlite3_win32_set_directory (below); however,
44319** it accepts a UTF-16 string.
44320*/
44321SQLITE_API int sqlite3_win32_set_directory16(
44322 unsigned long type, /* Identifier for directory being set or reset */
44323 const void *zValue /* New value for directory being set or reset */
44324){
44325 int rc;
44326 char *zUtf8 = 0;
44327 if( zValue ){
44328 zUtf8 = sqlite3_win32_unicode_to_utf8(zValue);
44329 if( zUtf8==0 ) return SQLITE_NOMEM_BKPT;
44330 }
44331 rc = sqlite3_win32_set_directory8(type, zUtf8);
44332 if( zUtf8 ) sqlite3_free(zUtf8);
44333 return rc;
44334}
44335
44336/*
44337** This function sets the data directory or the temporary directory based on
44338** the provided arguments. The type argument must be 1 in order to set the
44339** data directory or 2 in order to set the temporary directory. The zValue
44340** argument is the name of the directory to use. The return value will be
44341** SQLITE_OK if successful.
44342*/
44343SQLITE_API int sqlite3_win32_set_directory(
44344 unsigned long type, /* Identifier for directory being set or reset */
44345 void *zValue /* New value for directory being set or reset */
44346){
44347 return sqlite3_win32_set_directory16(type, zValue);
44348}
44349
44350/*
44351** The return value of winGetLastErrorMsg
44352** is zero if the error message fits in the buffer, or non-zero
44353** otherwise (if the message was truncated).
44354*/
44355static int winGetLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
44356 /* FormatMessage returns 0 on failure. Otherwise it
44357 ** returns the number of TCHARs written to the output
44358 ** buffer, excluding the terminating null char.
44359 */
44360 DWORD dwLen = 0;
44361 char *zOut = 0;
44362
44363 if( osIsNT() ){
44364#if SQLITE_OS_WINRT
44365 WCHAR zTempWide[SQLITE_WIN32_MAX_ERRMSG_CHARS+1];
44366 dwLen = osFormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
44367 FORMAT_MESSAGE_IGNORE_INSERTS,
44368 NULL,
44369 lastErrno,
44370 0,
44371 zTempWide,
44372 SQLITE_WIN32_MAX_ERRMSG_CHARS,
44373 0);
44374#else
44375 LPWSTR zTempWide = NULL;
44376 dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
44377 FORMAT_MESSAGE_FROM_SYSTEM |
44378 FORMAT_MESSAGE_IGNORE_INSERTS,
44379 NULL,
44380 lastErrno,
44381 0,
44382 (LPWSTR) &zTempWide,
44383 0,
44384 0);
44385#endif
44386 if( dwLen > 0 ){
44387 /* allocate a buffer and convert to UTF8 */
44388 sqlite3BeginBenignMalloc();
44389 zOut = winUnicodeToUtf8(zTempWide);
44390 sqlite3EndBenignMalloc();
44391#if !SQLITE_OS_WINRT
44392 /* free the system buffer allocated by FormatMessage */
44393 osLocalFree(zTempWide);
44394#endif
44395 }
44396 }
44397#ifdef SQLITE_WIN32_HAS_ANSI
44398 else{
44399 char *zTemp = NULL;
44400 dwLen = osFormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
44401 FORMAT_MESSAGE_FROM_SYSTEM |
44402 FORMAT_MESSAGE_IGNORE_INSERTS,
44403 NULL,
44404 lastErrno,
44405 0,
44406 (LPSTR) &zTemp,
44407 0,
44408 0);
44409 if( dwLen > 0 ){
44410 /* allocate a buffer and convert to UTF8 */
44411 sqlite3BeginBenignMalloc();
44412 zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
44413 sqlite3EndBenignMalloc();
44414 /* free the system buffer allocated by FormatMessage */
44415 osLocalFree(zTemp);
44416 }
44417 }
44418#endif
44419 if( 0 == dwLen ){
44420 sqlite3_snprintf(nBuf, zBuf, "OsError 0x%lx (%lu)", lastErrno, lastErrno);
44421 }else{
44422 /* copy a maximum of nBuf chars to output buffer */
44423 sqlite3_snprintf(nBuf, zBuf, "%s", zOut);
44424 /* free the UTF8 buffer */
44425 sqlite3_free(zOut);
44426 }
44427 return 0;
44428}
44429
44430/*
44431**
44432** This function - winLogErrorAtLine() - is only ever called via the macro
44433** winLogError().
44434**
44435** This routine is invoked after an error occurs in an OS function.
44436** It logs a message using sqlite3_log() containing the current value of
44437** error code and, if possible, the human-readable equivalent from
44438** FormatMessage.
44439**
44440** The first argument passed to the macro should be the error code that
44441** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
44442** The two subsequent arguments should be the name of the OS function that
44443** failed and the associated file-system path, if any.
44444*/
44445#define winLogError(a,b,c,d) winLogErrorAtLine(a,b,c,d,__LINE__)
44446static int winLogErrorAtLine(
44447 int errcode, /* SQLite error code */
44448 DWORD lastErrno, /* Win32 last error */
44449 const char *zFunc, /* Name of OS function that failed */
44450 const char *zPath, /* File path associated with error */
44451 int iLine /* Source line number where error occurred */
44452){
44453 char zMsg[500]; /* Human readable error text */
44454 int i; /* Loop counter */
44455
44456 zMsg[0] = 0;
44457 winGetLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
44458 assert( errcode!=SQLITE_OK );
44459 if( zPath==0 ) zPath = "";
44460 for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
44461 zMsg[i] = 0;
44462 sqlite3_log(errcode,
44463 "os_win.c:%d: (%lu) %s(%s) - %s",
44464 iLine, lastErrno, zFunc, zPath, zMsg
44465 );
44466
44467 return errcode;
44468}
44469
44470/*
44471** The number of times that a ReadFile(), WriteFile(), and DeleteFile()
44472** will be retried following a locking error - probably caused by
44473** antivirus software. Also the initial delay before the first retry.
44474** The delay increases linearly with each retry.
44475*/
44476#ifndef SQLITE_WIN32_IOERR_RETRY
44477# define SQLITE_WIN32_IOERR_RETRY 10
44478#endif
44479#ifndef SQLITE_WIN32_IOERR_RETRY_DELAY
44480# define SQLITE_WIN32_IOERR_RETRY_DELAY 25
44481#endif
44482static int winIoerrRetry = SQLITE_WIN32_IOERR_RETRY;
44483static int winIoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
44484
44485/*
44486** The "winIoerrCanRetry1" macro is used to determine if a particular I/O
44487** error code obtained via GetLastError() is eligible to be retried. It
44488** must accept the error code DWORD as its only argument and should return
44489** non-zero if the error code is transient in nature and the operation
44490** responsible for generating the original error might succeed upon being
44491** retried. The argument to this macro should be a variable.
44492**
44493** Additionally, a macro named "winIoerrCanRetry2" may be defined. If it
44494** is defined, it will be consulted only when the macro "winIoerrCanRetry1"
44495** returns zero. The "winIoerrCanRetry2" macro is completely optional and
44496** may be used to include additional error codes in the set that should
44497** result in the failing I/O operation being retried by the caller. If
44498** defined, the "winIoerrCanRetry2" macro must exhibit external semantics
44499** identical to those of the "winIoerrCanRetry1" macro.
44500*/
44501#if !defined(winIoerrCanRetry1)
44502#define winIoerrCanRetry1(a) (((a)==ERROR_ACCESS_DENIED) || \
44503 ((a)==ERROR_SHARING_VIOLATION) || \
44504 ((a)==ERROR_LOCK_VIOLATION) || \
44505 ((a)==ERROR_DEV_NOT_EXIST) || \
44506 ((a)==ERROR_NETNAME_DELETED) || \
44507 ((a)==ERROR_SEM_TIMEOUT) || \
44508 ((a)==ERROR_NETWORK_UNREACHABLE))
44509#endif
44510
44511/*
44512** If a ReadFile() or WriteFile() error occurs, invoke this routine
44513** to see if it should be retried. Return TRUE to retry. Return FALSE
44514** to give up with an error.
44515*/
44516static int winRetryIoerr(int *pnRetry, DWORD *pError){
44517 DWORD e = osGetLastError();
44518 if( *pnRetry>=winIoerrRetry ){
44519 if( pError ){
44520 *pError = e;
44521 }
44522 return 0;
44523 }
44524 if( winIoerrCanRetry1(e) ){
44525 sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
44526 ++*pnRetry;
44527 return 1;
44528 }
44529#if defined(winIoerrCanRetry2)
44530 else if( winIoerrCanRetry2(e) ){
44531 sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
44532 ++*pnRetry;
44533 return 1;
44534 }
44535#endif
44536 if( pError ){
44537 *pError = e;
44538 }
44539 return 0;
44540}
44541
44542/*
44543** Log a I/O error retry episode.
44544*/
44545static void winLogIoerr(int nRetry, int lineno){
44546 if( nRetry ){
44547 sqlite3_log(SQLITE_NOTICE,
44548 "delayed %dms for lock/sharing conflict at line %d",
44549 winIoerrRetryDelay*nRetry*(nRetry+1)/2, lineno
44550 );
44551 }
44552}
44553
44554/*
44555** This #if does not rely on the SQLITE_OS_WINCE define because the
44556** corresponding section in "date.c" cannot use it.
44557*/
44558#if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \
44559 (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API)
44560/*
44561** The MSVC CRT on Windows CE may not have a localtime() function.
44562** So define a substitute.
44563*/
44564/* # include <time.h> */
44565struct tm *__cdecl localtime(const time_t *t)
44566{
44567 static struct tm y;
44568 FILETIME uTm, lTm;
44569 SYSTEMTIME pTm;
44570 sqlite3_int64 t64;
44571 t64 = *t;
44572 t64 = (t64 + 11644473600)*10000000;
44573 uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF);
44574 uTm.dwHighDateTime= (DWORD)(t64 >> 32);
44575 osFileTimeToLocalFileTime(&uTm,&lTm);
44576 osFileTimeToSystemTime(&lTm,&pTm);
44577 y.tm_year = pTm.wYear - 1900;
44578 y.tm_mon = pTm.wMonth - 1;
44579 y.tm_wday = pTm.wDayOfWeek;
44580 y.tm_mday = pTm.wDay;
44581 y.tm_hour = pTm.wHour;
44582 y.tm_min = pTm.wMinute;
44583 y.tm_sec = pTm.wSecond;
44584 return &y;
44585}
44586#endif
44587
44588#if SQLITE_OS_WINCE
44589/*************************************************************************
44590** This section contains code for WinCE only.
44591*/
44592#define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
44593
44594/*
44595** Acquire a lock on the handle h
44596*/
44597static void winceMutexAcquire(HANDLE h){
44598 DWORD dwErr;
44599 do {
44600 dwErr = osWaitForSingleObject(h, INFINITE);
44601 } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED);
44602}
44603/*
44604** Release a lock acquired by winceMutexAcquire()
44605*/
44606#define winceMutexRelease(h) ReleaseMutex(h)
44607
44608/*
44609** Create the mutex and shared memory used for locking in the file
44610** descriptor pFile
44611*/
44612static int winceCreateLock(const char *zFilename, winFile *pFile){
44613 LPWSTR zTok;
44614 LPWSTR zName;
44615 DWORD lastErrno;
44616 BOOL bLogged = FALSE;
44617 BOOL bInit = TRUE;
44618
44619 zName = winUtf8ToUnicode(zFilename);
44620 if( zName==0 ){
44621 /* out of memory */
44622 return SQLITE_IOERR_NOMEM_BKPT;
44623 }
44624
44625 /* Initialize the local lockdata */
44626 memset(&pFile->local, 0, sizeof(pFile->local));
44627
44628 /* Replace the backslashes from the filename and lowercase it
44629 ** to derive a mutex name. */
44630 zTok = osCharLowerW(zName);
44631 for (;*zTok;zTok++){
44632 if (*zTok == '\\') *zTok = '_';
44633 }
44634
44635 /* Create/open the named mutex */
44636 pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
44637 if (!pFile->hMutex){
44638 pFile->lastErrno = osGetLastError();
44639 sqlite3_free(zName);
44640 return winLogError(SQLITE_IOERR, pFile->lastErrno,
44641 "winceCreateLock1", zFilename);
44642 }
44643
44644 /* Acquire the mutex before continuing */
44645 winceMutexAcquire(pFile->hMutex);
44646
44647 /* Since the names of named mutexes, semaphores, file mappings etc are
44648 ** case-sensitive, take advantage of that by uppercasing the mutex name
44649 ** and using that as the shared filemapping name.
44650 */
44651 osCharUpperW(zName);
44652 pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
44653 PAGE_READWRITE, 0, sizeof(winceLock),
44654 zName);
44655
44656 /* Set a flag that indicates we're the first to create the memory so it
44657 ** must be zero-initialized */
44658 lastErrno = osGetLastError();
44659 if (lastErrno == ERROR_ALREADY_EXISTS){
44660 bInit = FALSE;
44661 }
44662
44663 sqlite3_free(zName);
44664
44665 /* If we succeeded in making the shared memory handle, map it. */
44666 if( pFile->hShared ){
44667 pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
44668 FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
44669 /* If mapping failed, close the shared memory handle and erase it */
44670 if( !pFile->shared ){
44671 pFile->lastErrno = osGetLastError();
44672 winLogError(SQLITE_IOERR, pFile->lastErrno,
44673 "winceCreateLock2", zFilename);
44674 bLogged = TRUE;
44675 osCloseHandle(pFile->hShared);
44676 pFile->hShared = NULL;
44677 }
44678 }
44679
44680 /* If shared memory could not be created, then close the mutex and fail */
44681 if( pFile->hShared==NULL ){
44682 if( !bLogged ){
44683 pFile->lastErrno = lastErrno;
44684 winLogError(SQLITE_IOERR, pFile->lastErrno,
44685 "winceCreateLock3", zFilename);
44686 bLogged = TRUE;
44687 }
44688 winceMutexRelease(pFile->hMutex);
44689 osCloseHandle(pFile->hMutex);
44690 pFile->hMutex = NULL;
44691 return SQLITE_IOERR;
44692 }
44693
44694 /* Initialize the shared memory if we're supposed to */
44695 if( bInit ){
44696 memset(pFile->shared, 0, sizeof(winceLock));
44697 }
44698
44699 winceMutexRelease(pFile->hMutex);
44700 return SQLITE_OK;
44701}
44702
44703/*
44704** Destroy the part of winFile that deals with wince locks
44705*/
44706static void winceDestroyLock(winFile *pFile){
44707 if (pFile->hMutex){
44708 /* Acquire the mutex */
44709 winceMutexAcquire(pFile->hMutex);
44710
44711 /* The following blocks should probably assert in debug mode, but they
44712 are to cleanup in case any locks remained open */
44713 if (pFile->local.nReaders){
44714 pFile->shared->nReaders --;
44715 }
44716 if (pFile->local.bReserved){
44717 pFile->shared->bReserved = FALSE;
44718 }
44719 if (pFile->local.bPending){
44720 pFile->shared->bPending = FALSE;
44721 }
44722 if (pFile->local.bExclusive){
44723 pFile->shared->bExclusive = FALSE;
44724 }
44725
44726 /* De-reference and close our copy of the shared memory handle */
44727 osUnmapViewOfFile(pFile->shared);
44728 osCloseHandle(pFile->hShared);
44729
44730 /* Done with the mutex */
44731 winceMutexRelease(pFile->hMutex);
44732 osCloseHandle(pFile->hMutex);
44733 pFile->hMutex = NULL;
44734 }
44735}
44736
44737/*
44738** An implementation of the LockFile() API of Windows for CE
44739*/
44740static BOOL winceLockFile(
44741 LPHANDLE phFile,
44742 DWORD dwFileOffsetLow,
44743 DWORD dwFileOffsetHigh,
44744 DWORD nNumberOfBytesToLockLow,
44745 DWORD nNumberOfBytesToLockHigh
44746){
44747 winFile *pFile = HANDLE_TO_WINFILE(phFile);
44748 BOOL bReturn = FALSE;
44749
44750 UNUSED_PARAMETER(dwFileOffsetHigh);
44751 UNUSED_PARAMETER(nNumberOfBytesToLockHigh);
44752
44753 if (!pFile->hMutex) return TRUE;
44754 winceMutexAcquire(pFile->hMutex);
44755
44756 /* Wanting an exclusive lock? */
44757 if (dwFileOffsetLow == (DWORD)SHARED_FIRST
44758 && nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE){
44759 if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
44760 pFile->shared->bExclusive = TRUE;
44761 pFile->local.bExclusive = TRUE;
44762 bReturn = TRUE;
44763 }
44764 }
44765
44766 /* Want a read-only lock? */
44767 else if (dwFileOffsetLow == (DWORD)SHARED_FIRST &&
44768 nNumberOfBytesToLockLow == 1){
44769 if (pFile->shared->bExclusive == 0){
44770 pFile->local.nReaders ++;
44771 if (pFile->local.nReaders == 1){
44772 pFile->shared->nReaders ++;
44773 }
44774 bReturn = TRUE;
44775 }
44776 }
44777
44778 /* Want a pending lock? */
44779 else if (dwFileOffsetLow == (DWORD)PENDING_BYTE
44780 && nNumberOfBytesToLockLow == 1){
44781 /* If no pending lock has been acquired, then acquire it */
44782 if (pFile->shared->bPending == 0) {
44783 pFile->shared->bPending = TRUE;
44784 pFile->local.bPending = TRUE;
44785 bReturn = TRUE;
44786 }
44787 }
44788
44789 /* Want a reserved lock? */
44790 else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE
44791 && nNumberOfBytesToLockLow == 1){
44792 if (pFile->shared->bReserved == 0) {
44793 pFile->shared->bReserved = TRUE;
44794 pFile->local.bReserved = TRUE;
44795 bReturn = TRUE;
44796 }
44797 }
44798
44799 winceMutexRelease(pFile->hMutex);
44800 return bReturn;
44801}
44802
44803/*
44804** An implementation of the UnlockFile API of Windows for CE
44805*/
44806static BOOL winceUnlockFile(
44807 LPHANDLE phFile,
44808 DWORD dwFileOffsetLow,
44809 DWORD dwFileOffsetHigh,
44810 DWORD nNumberOfBytesToUnlockLow,
44811 DWORD nNumberOfBytesToUnlockHigh
44812){
44813 winFile *pFile = HANDLE_TO_WINFILE(phFile);
44814 BOOL bReturn = FALSE;
44815
44816 UNUSED_PARAMETER(dwFileOffsetHigh);
44817 UNUSED_PARAMETER(nNumberOfBytesToUnlockHigh);
44818
44819 if (!pFile->hMutex) return TRUE;
44820 winceMutexAcquire(pFile->hMutex);
44821
44822 /* Releasing a reader lock or an exclusive lock */
44823 if (dwFileOffsetLow == (DWORD)SHARED_FIRST){
44824 /* Did we have an exclusive lock? */
44825 if (pFile->local.bExclusive){
44826 assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE);
44827 pFile->local.bExclusive = FALSE;
44828 pFile->shared->bExclusive = FALSE;
44829 bReturn = TRUE;
44830 }
44831
44832 /* Did we just have a reader lock? */
44833 else if (pFile->local.nReaders){
44834 assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE
44835 || nNumberOfBytesToUnlockLow == 1);
44836 pFile->local.nReaders --;
44837 if (pFile->local.nReaders == 0)
44838 {
44839 pFile->shared->nReaders --;
44840 }
44841 bReturn = TRUE;
44842 }
44843 }
44844
44845 /* Releasing a pending lock */
44846 else if (dwFileOffsetLow == (DWORD)PENDING_BYTE
44847 && nNumberOfBytesToUnlockLow == 1){
44848 if (pFile->local.bPending){
44849 pFile->local.bPending = FALSE;
44850 pFile->shared->bPending = FALSE;
44851 bReturn = TRUE;
44852 }
44853 }
44854 /* Releasing a reserved lock */
44855 else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE
44856 && nNumberOfBytesToUnlockLow == 1){
44857 if (pFile->local.bReserved) {
44858 pFile->local.bReserved = FALSE;
44859 pFile->shared->bReserved = FALSE;
44860 bReturn = TRUE;
44861 }
44862 }
44863
44864 winceMutexRelease(pFile->hMutex);
44865 return bReturn;
44866}
44867/*
44868** End of the special code for wince
44869*****************************************************************************/
44870#endif /* SQLITE_OS_WINCE */
44871
44872/*
44873** Lock a file region.
44874*/
44875static BOOL winLockFile(
44876 LPHANDLE phFile,
44877 DWORD flags,
44878 DWORD offsetLow,
44879 DWORD offsetHigh,
44880 DWORD numBytesLow,
44881 DWORD numBytesHigh
44882){
44883#if SQLITE_OS_WINCE
44884 /*
44885 ** NOTE: Windows CE is handled differently here due its lack of the Win32
44886 ** API LockFile.
44887 */
44888 return winceLockFile(phFile, offsetLow, offsetHigh,
44889 numBytesLow, numBytesHigh);
44890#else
44891 if( osIsNT() ){
44892 OVERLAPPED ovlp;
44893 memset(&ovlp, 0, sizeof(OVERLAPPED));
44894 ovlp.Offset = offsetLow;
44895 ovlp.OffsetHigh = offsetHigh;
44896 return osLockFileEx(*phFile, flags, 0, numBytesLow, numBytesHigh, &ovlp);
44897 }else{
44898 return osLockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
44899 numBytesHigh);
44900 }
44901#endif
44902}
44903
44904/*
44905** Unlock a file region.
44906 */
44907static BOOL winUnlockFile(
44908 LPHANDLE phFile,
44909 DWORD offsetLow,
44910 DWORD offsetHigh,
44911 DWORD numBytesLow,
44912 DWORD numBytesHigh
44913){
44914#if SQLITE_OS_WINCE
44915 /*
44916 ** NOTE: Windows CE is handled differently here due its lack of the Win32
44917 ** API UnlockFile.
44918 */
44919 return winceUnlockFile(phFile, offsetLow, offsetHigh,
44920 numBytesLow, numBytesHigh);
44921#else
44922 if( osIsNT() ){
44923 OVERLAPPED ovlp;
44924 memset(&ovlp, 0, sizeof(OVERLAPPED));
44925 ovlp.Offset = offsetLow;
44926 ovlp.OffsetHigh = offsetHigh;
44927 return osUnlockFileEx(*phFile, 0, numBytesLow, numBytesHigh, &ovlp);
44928 }else{
44929 return osUnlockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
44930 numBytesHigh);
44931 }
44932#endif
44933}
44934
44935/*****************************************************************************
44936** The next group of routines implement the I/O methods specified
44937** by the sqlite3_io_methods object.
44938******************************************************************************/
44939
44940/*
44941** Some Microsoft compilers lack this definition.
44942*/
44943#ifndef INVALID_SET_FILE_POINTER
44944# define INVALID_SET_FILE_POINTER ((DWORD)-1)
44945#endif
44946
44947/*
44948** Move the current position of the file handle passed as the first
44949** argument to offset iOffset within the file. If successful, return 0.
44950** Otherwise, set pFile->lastErrno and return non-zero.
44951*/
44952static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
44953#if !SQLITE_OS_WINRT
44954 LONG upperBits; /* Most sig. 32 bits of new offset */
44955 LONG lowerBits; /* Least sig. 32 bits of new offset */
44956 DWORD dwRet; /* Value returned by SetFilePointer() */
44957 DWORD lastErrno; /* Value returned by GetLastError() */
44958
44959 OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
44960
44961 upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
44962 lowerBits = (LONG)(iOffset & 0xffffffff);
44963
44964 /* API oddity: If successful, SetFilePointer() returns a dword
44965 ** containing the lower 32-bits of the new file-offset. Or, if it fails,
44966 ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
44967 ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
44968 ** whether an error has actually occurred, it is also necessary to call
44969 ** GetLastError().
44970 */
44971 dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
44972
44973 if( (dwRet==INVALID_SET_FILE_POINTER
44974 && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
44975 pFile->lastErrno = lastErrno;
44976 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
44977 "winSeekFile", pFile->zPath);
44978 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
44979 return 1;
44980 }
44981
44982 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
44983 return 0;
44984#else
44985 /*
44986 ** Same as above, except that this implementation works for WinRT.
44987 */
44988
44989 LARGE_INTEGER x; /* The new offset */
44990 BOOL bRet; /* Value returned by SetFilePointerEx() */
44991
44992 x.QuadPart = iOffset;
44993 bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
44994
44995 if(!bRet){
44996 pFile->lastErrno = osGetLastError();
44997 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
44998 "winSeekFile", pFile->zPath);
44999 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
45000 return 1;
45001 }
45002
45003 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
45004 return 0;
45005#endif
45006}
45007
45008#if SQLITE_MAX_MMAP_SIZE>0
45009/* Forward references to VFS helper methods used for memory mapped files */
45010static int winMapfile(winFile*, sqlite3_int64);
45011static int winUnmapfile(winFile*);
45012#endif
45013
45014/*
45015** Close a file.
45016**
45017** It is reported that an attempt to close a handle might sometimes
45018** fail. This is a very unreasonable result, but Windows is notorious
45019** for being unreasonable so I do not doubt that it might happen. If
45020** the close fails, we pause for 100 milliseconds and try again. As
45021** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
45022** giving up and returning an error.
45023*/
45024#define MX_CLOSE_ATTEMPT 3
45025static int winClose(sqlite3_file *id){
45026 int rc, cnt = 0;
45027 winFile *pFile = (winFile*)id;
45028
45029 assert( id!=0 );
45030#ifndef SQLITE_OMIT_WAL
45031 assert( pFile->pShm==0 );
45032#endif
45033 assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
45034 OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p\n",
45035 osGetCurrentProcessId(), pFile, pFile->h));
45036
45037#if SQLITE_MAX_MMAP_SIZE>0
45038 winUnmapfile(pFile);
45039#endif
45040
45041 do{
45042 rc = osCloseHandle(pFile->h);
45043 /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
45044 }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) );
45045#if SQLITE_OS_WINCE
45046#define WINCE_DELETION_ATTEMPTS 3
45047 {
45048 winVfsAppData *pAppData = (winVfsAppData*)pFile->pVfs->pAppData;
45049 if( pAppData==NULL || !pAppData->bNoLock ){
45050 winceDestroyLock(pFile);
45051 }
45052 }
45053 if( pFile->zDeleteOnClose ){
45054 int cnt = 0;
45055 while(
45056 osDeleteFileW(pFile->zDeleteOnClose)==0
45057 && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
45058 && cnt++ < WINCE_DELETION_ATTEMPTS
45059 ){
45060 sqlite3_win32_sleep(100); /* Wait a little before trying again */
45061 }
45062 sqlite3_free(pFile->zDeleteOnClose);
45063 }
45064#endif
45065 if( rc ){
45066 pFile->h = NULL;
45067 }
45068 OpenCounter(-1);
45069 OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p, rc=%s\n",
45070 osGetCurrentProcessId(), pFile, pFile->h, rc ? "ok" : "failed"));
45071 return rc ? SQLITE_OK
45072 : winLogError(SQLITE_IOERR_CLOSE, osGetLastError(),
45073 "winClose", pFile->zPath);
45074}
45075
45076/*
45077** Read data from a file into a buffer. Return SQLITE_OK if all
45078** bytes were read successfully and SQLITE_IOERR if anything goes
45079** wrong.
45080*/
45081static int winRead(
45082 sqlite3_file *id, /* File to read from */
45083 void *pBuf, /* Write content into this buffer */
45084 int amt, /* Number of bytes to read */
45085 sqlite3_int64 offset /* Begin reading at this offset */
45086){
45087#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
45088 OVERLAPPED overlapped; /* The offset for ReadFile. */
45089#endif
45090 winFile *pFile = (winFile*)id; /* file handle */
45091 DWORD nRead; /* Number of bytes actually read from file */
45092 int nRetry = 0; /* Number of retrys */
45093
45094 assert( id!=0 );
45095 assert( amt>0 );
45096 assert( offset>=0 );
45097 SimulateIOError(return SQLITE_IOERR_READ);
45098 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
45099 "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
45100 pFile->h, pBuf, amt, offset, pFile->locktype));
45101
45102#if SQLITE_MAX_MMAP_SIZE>0
45103 /* Deal with as much of this read request as possible by transfering
45104 ** data from the memory mapping using memcpy(). */
45105 if( offset<pFile->mmapSize ){
45106 if( offset+amt <= pFile->mmapSize ){
45107 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
45108 OSTRACE(("READ-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
45109 osGetCurrentProcessId(), pFile, pFile->h));
45110 return SQLITE_OK;
45111 }else{
45112 int nCopy = (int)(pFile->mmapSize - offset);
45113 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
45114 pBuf = &((u8 *)pBuf)[nCopy];
45115 amt -= nCopy;
45116 offset += nCopy;
45117 }
45118 }
45119#endif
45120
45121#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
45122 if( winSeekFile(pFile, offset) ){
45123 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n",
45124 osGetCurrentProcessId(), pFile, pFile->h));
45125 return SQLITE_FULL;
45126 }
45127 while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
45128#else
45129 memset(&overlapped, 0, sizeof(OVERLAPPED));
45130 overlapped.Offset = (LONG)(offset & 0xffffffff);
45131 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
45132 while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
45133 osGetLastError()!=ERROR_HANDLE_EOF ){
45134#endif
45135 DWORD lastErrno;
45136 if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
45137 pFile->lastErrno = lastErrno;
45138 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_READ\n",
45139 osGetCurrentProcessId(), pFile, pFile->h));
45140 return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
45141 "winRead", pFile->zPath);
45142 }
45143 winLogIoerr(nRetry, __LINE__);
45144 if( nRead<(DWORD)amt ){
45145 /* Unread parts of the buffer must be zero-filled */
45146 memset(&((char*)pBuf)[nRead], 0, amt-nRead);
45147 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_SHORT_READ\n",
45148 osGetCurrentProcessId(), pFile, pFile->h));
45149 return SQLITE_IOERR_SHORT_READ;
45150 }
45151
45152 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
45153 osGetCurrentProcessId(), pFile, pFile->h));
45154 return SQLITE_OK;
45155}
45156
45157/*
45158** Write data from a buffer into a file. Return SQLITE_OK on success
45159** or some other error code on failure.
45160*/
45161static int winWrite(
45162 sqlite3_file *id, /* File to write into */
45163 const void *pBuf, /* The bytes to be written */
45164 int amt, /* Number of bytes to write */
45165 sqlite3_int64 offset /* Offset into the file to begin writing at */
45166){
45167 int rc = 0; /* True if error has occurred, else false */
45168 winFile *pFile = (winFile*)id; /* File handle */
45169 int nRetry = 0; /* Number of retries */
45170
45171 assert( amt>0 );
45172 assert( pFile );
45173 SimulateIOError(return SQLITE_IOERR_WRITE);
45174 SimulateDiskfullError(return SQLITE_FULL);
45175
45176 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
45177 "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
45178 pFile->h, pBuf, amt, offset, pFile->locktype));
45179
45180#if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0
45181 /* Deal with as much of this write request as possible by transfering
45182 ** data from the memory mapping using memcpy(). */
45183 if( offset<pFile->mmapSize ){
45184 if( offset+amt <= pFile->mmapSize ){
45185 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
45186 OSTRACE(("WRITE-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
45187 osGetCurrentProcessId(), pFile, pFile->h));
45188 return SQLITE_OK;
45189 }else{
45190 int nCopy = (int)(pFile->mmapSize - offset);
45191 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
45192 pBuf = &((u8 *)pBuf)[nCopy];
45193 amt -= nCopy;
45194 offset += nCopy;
45195 }
45196 }
45197#endif
45198
45199#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
45200 rc = winSeekFile(pFile, offset);
45201 if( rc==0 ){
45202#else
45203 {
45204#endif
45205#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
45206 OVERLAPPED overlapped; /* The offset for WriteFile. */
45207#endif
45208 u8 *aRem = (u8 *)pBuf; /* Data yet to be written */
45209 int nRem = amt; /* Number of bytes yet to be written */
45210 DWORD nWrite; /* Bytes written by each WriteFile() call */
45211 DWORD lastErrno = NO_ERROR; /* Value returned by GetLastError() */
45212
45213#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
45214 memset(&overlapped, 0, sizeof(OVERLAPPED));
45215 overlapped.Offset = (LONG)(offset & 0xffffffff);
45216 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
45217#endif
45218
45219 while( nRem>0 ){
45220#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
45221 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
45222#else
45223 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
45224#endif
45225 if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
45226 break;
45227 }
45228 assert( nWrite==0 || nWrite<=(DWORD)nRem );
45229 if( nWrite==0 || nWrite>(DWORD)nRem ){
45230 lastErrno = osGetLastError();
45231 break;
45232 }
45233#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
45234 offset += nWrite;
45235 overlapped.Offset = (LONG)(offset & 0xffffffff);
45236 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
45237#endif
45238 aRem += nWrite;
45239 nRem -= nWrite;
45240 }
45241 if( nRem>0 ){
45242 pFile->lastErrno = lastErrno;
45243 rc = 1;
45244 }
45245 }
45246
45247 if( rc ){
45248 if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
45249 || ( pFile->lastErrno==ERROR_DISK_FULL )){
45250 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n",
45251 osGetCurrentProcessId(), pFile, pFile->h));
45252 return winLogError(SQLITE_FULL, pFile->lastErrno,
45253 "winWrite1", pFile->zPath);
45254 }
45255 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_WRITE\n",
45256 osGetCurrentProcessId(), pFile, pFile->h));
45257 return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
45258 "winWrite2", pFile->zPath);
45259 }else{
45260 winLogIoerr(nRetry, __LINE__);
45261 }
45262 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
45263 osGetCurrentProcessId(), pFile, pFile->h));
45264 return SQLITE_OK;
45265}
45266
45267/*
45268** Truncate an open file to a specified size
45269*/
45270static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
45271 winFile *pFile = (winFile*)id; /* File handle object */
45272 int rc = SQLITE_OK; /* Return code for this function */
45273 DWORD lastErrno;
45274#if SQLITE_MAX_MMAP_SIZE>0
45275 sqlite3_int64 oldMmapSize;
45276 if( pFile->nFetchOut>0 ){
45277 /* File truncation is a no-op if there are outstanding memory mapped
45278 ** pages. This is because truncating the file means temporarily unmapping
45279 ** the file, and that might delete memory out from under existing cursors.
45280 **
45281 ** This can result in incremental vacuum not truncating the file,
45282 ** if there is an active read cursor when the incremental vacuum occurs.
45283 ** No real harm comes of this - the database file is not corrupted,
45284 ** though some folks might complain that the file is bigger than it
45285 ** needs to be.
45286 **
45287 ** The only feasible work-around is to defer the truncation until after
45288 ** all references to memory-mapped content are closed. That is doable,
45289 ** but involves adding a few branches in the common write code path which
45290 ** could slow down normal operations slightly. Hence, we have decided for
45291 ** now to simply make trancations a no-op if there are pending reads. We
45292 ** can maybe revisit this decision in the future.
45293 */
45294 return SQLITE_OK;
45295 }
45296#endif
45297
45298 assert( pFile );
45299 SimulateIOError(return SQLITE_IOERR_TRUNCATE);
45300 OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, size=%lld, lock=%d\n",
45301 osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype));
45302
45303 /* If the user has configured a chunk-size for this file, truncate the
45304 ** file so that it consists of an integer number of chunks (i.e. the
45305 ** actual file size after the operation may be larger than the requested
45306 ** size).
45307 */
45308 if( pFile->szChunk>0 ){
45309 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
45310 }
45311
45312#if SQLITE_MAX_MMAP_SIZE>0
45313 if( pFile->pMapRegion ){
45314 oldMmapSize = pFile->mmapSize;
45315 }else{
45316 oldMmapSize = 0;
45317 }
45318 winUnmapfile(pFile);
45319#endif
45320
45321 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
45322 if( winSeekFile(pFile, nByte) ){
45323 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
45324 "winTruncate1", pFile->zPath);
45325 }else if( 0==osSetEndOfFile(pFile->h) &&
45326 ((lastErrno = osGetLastError())!=ERROR_USER_MAPPED_FILE) ){
45327 pFile->lastErrno = lastErrno;
45328 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
45329 "winTruncate2", pFile->zPath);
45330 }
45331
45332#if SQLITE_MAX_MMAP_SIZE>0
45333 if( rc==SQLITE_OK && oldMmapSize>0 ){
45334 if( oldMmapSize>nByte ){
45335 winMapfile(pFile, -1);
45336 }else{
45337 winMapfile(pFile, oldMmapSize);
45338 }
45339 }
45340#endif
45341
45342 OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, rc=%s\n",
45343 osGetCurrentProcessId(), pFile, pFile->h, sqlite3ErrName(rc)));
45344 return rc;
45345}
45346
45347#ifdef SQLITE_TEST
45348/*
45349** Count the number of fullsyncs and normal syncs. This is used to test
45350** that syncs and fullsyncs are occuring at the right times.
45351*/
45352SQLITE_API int sqlite3_sync_count = 0;
45353SQLITE_API int sqlite3_fullsync_count = 0;
45354#endif
45355
45356/*
45357** Make sure all writes to a particular file are committed to disk.
45358*/
45359static int winSync(sqlite3_file *id, int flags){
45360#ifndef SQLITE_NO_SYNC
45361 /*
45362 ** Used only when SQLITE_NO_SYNC is not defined.
45363 */
45364 BOOL rc;
45365#endif
45366#if !defined(NDEBUG) || !defined(SQLITE_NO_SYNC) || \
45367 defined(SQLITE_HAVE_OS_TRACE)
45368 /*
45369 ** Used when SQLITE_NO_SYNC is not defined and by the assert() and/or
45370 ** OSTRACE() macros.
45371 */
45372 winFile *pFile = (winFile*)id;
45373#else
45374 UNUSED_PARAMETER(id);
45375#endif
45376
45377 assert( pFile );
45378 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
45379 assert((flags&0x0F)==SQLITE_SYNC_NORMAL
45380 || (flags&0x0F)==SQLITE_SYNC_FULL
45381 );
45382
45383 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
45384 ** line is to test that doing so does not cause any problems.
45385 */
45386 SimulateDiskfullError( return SQLITE_FULL );
45387
45388 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, flags=%x, lock=%d\n",
45389 osGetCurrentProcessId(), pFile, pFile->h, flags,
45390 pFile->locktype));
45391
45392#ifndef SQLITE_TEST
45393 UNUSED_PARAMETER(flags);
45394#else
45395 if( (flags&0x0F)==SQLITE_SYNC_FULL ){
45396 sqlite3_fullsync_count++;
45397 }
45398 sqlite3_sync_count++;
45399#endif
45400
45401 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
45402 ** no-op
45403 */
45404#ifdef SQLITE_NO_SYNC
45405 OSTRACE(("SYNC-NOP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
45406 osGetCurrentProcessId(), pFile, pFile->h));
45407 return SQLITE_OK;
45408#else
45409#if SQLITE_MAX_MMAP_SIZE>0
45410 if( pFile->pMapRegion ){
45411 if( osFlushViewOfFile(pFile->pMapRegion, 0) ){
45412 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
45413 "rc=SQLITE_OK\n", osGetCurrentProcessId(),
45414 pFile, pFile->pMapRegion));
45415 }else{
45416 pFile->lastErrno = osGetLastError();
45417 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
45418 "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(),
45419 pFile, pFile->pMapRegion));
45420 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
45421 "winSync1", pFile->zPath);
45422 }
45423 }
45424#endif
45425 rc = osFlushFileBuffers(pFile->h);
45426 SimulateIOError( rc=FALSE );
45427 if( rc ){
45428 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
45429 osGetCurrentProcessId(), pFile, pFile->h));
45430 return SQLITE_OK;
45431 }else{
45432 pFile->lastErrno = osGetLastError();
45433 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_FSYNC\n",
45434 osGetCurrentProcessId(), pFile, pFile->h));
45435 return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
45436 "winSync2", pFile->zPath);
45437 }
45438#endif
45439}
45440
45441/*
45442** Determine the current size of a file in bytes
45443*/
45444static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
45445 winFile *pFile = (winFile*)id;
45446 int rc = SQLITE_OK;
45447
45448 assert( id!=0 );
45449 assert( pSize!=0 );
45450 SimulateIOError(return SQLITE_IOERR_FSTAT);
45451 OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize));
45452
45453#if SQLITE_OS_WINRT
45454 {
45455 FILE_STANDARD_INFO info;
45456 if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo,
45457 &info, sizeof(info)) ){
45458 *pSize = info.EndOfFile.QuadPart;
45459 }else{
45460 pFile->lastErrno = osGetLastError();
45461 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
45462 "winFileSize", pFile->zPath);
45463 }
45464 }
45465#else
45466 {
45467 DWORD upperBits;
45468 DWORD lowerBits;
45469 DWORD lastErrno;
45470
45471 lowerBits = osGetFileSize(pFile->h, &upperBits);
45472 *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
45473 if( (lowerBits == INVALID_FILE_SIZE)
45474 && ((lastErrno = osGetLastError())!=NO_ERROR) ){
45475 pFile->lastErrno = lastErrno;
45476 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
45477 "winFileSize", pFile->zPath);
45478 }
45479 }
45480#endif
45481 OSTRACE(("SIZE file=%p, pSize=%p, *pSize=%lld, rc=%s\n",
45482 pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
45483 return rc;
45484}
45485
45486/*
45487** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems.
45488*/
45489#ifndef LOCKFILE_FAIL_IMMEDIATELY
45490# define LOCKFILE_FAIL_IMMEDIATELY 1
45491#endif
45492
45493#ifndef LOCKFILE_EXCLUSIVE_LOCK
45494# define LOCKFILE_EXCLUSIVE_LOCK 2
45495#endif
45496
45497/*
45498** Historically, SQLite has used both the LockFile and LockFileEx functions.
45499** When the LockFile function was used, it was always expected to fail
45500** immediately if the lock could not be obtained. Also, it always expected to
45501** obtain an exclusive lock. These flags are used with the LockFileEx function
45502** and reflect those expectations; therefore, they should not be changed.
45503*/
45504#ifndef SQLITE_LOCKFILE_FLAGS
45505# define SQLITE_LOCKFILE_FLAGS (LOCKFILE_FAIL_IMMEDIATELY | \
45506 LOCKFILE_EXCLUSIVE_LOCK)
45507#endif
45508
45509/*
45510** Currently, SQLite never calls the LockFileEx function without wanting the
45511** call to fail immediately if the lock cannot be obtained.
45512*/
45513#ifndef SQLITE_LOCKFILEEX_FLAGS
45514# define SQLITE_LOCKFILEEX_FLAGS (LOCKFILE_FAIL_IMMEDIATELY)
45515#endif
45516
45517/*
45518** Acquire a reader lock.
45519** Different API routines are called depending on whether or not this
45520** is Win9x or WinNT.
45521*/
45522static int winGetReadLock(winFile *pFile){
45523 int res;
45524 OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
45525 if( osIsNT() ){
45526#if SQLITE_OS_WINCE
45527 /*
45528 ** NOTE: Windows CE is handled differently here due its lack of the Win32
45529 ** API LockFileEx.
45530 */
45531 res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0);
45532#else
45533 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0,
45534 SHARED_SIZE, 0);
45535#endif
45536 }
45537#ifdef SQLITE_WIN32_HAS_ANSI
45538 else{
45539 int lk;
45540 sqlite3_randomness(sizeof(lk), &lk);
45541 pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
45542 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
45543 SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
45544 }
45545#endif
45546 if( res == 0 ){
45547 pFile->lastErrno = osGetLastError();
45548 /* No need to log a failure to lock */
45549 }
45550 OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res));
45551 return res;
45552}
45553
45554/*
45555** Undo a readlock
45556*/
45557static int winUnlockReadLock(winFile *pFile){
45558 int res;
45559 DWORD lastErrno;
45560 OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
45561 if( osIsNT() ){
45562 res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
45563 }
45564#ifdef SQLITE_WIN32_HAS_ANSI
45565 else{
45566 res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
45567 }
45568#endif
45569 if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
45570 pFile->lastErrno = lastErrno;
45571 winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
45572 "winUnlockReadLock", pFile->zPath);
45573 }
45574 OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res));
45575 return res;
45576}
45577
45578/*
45579** Lock the file with the lock specified by parameter locktype - one
45580** of the following:
45581**
45582** (1) SHARED_LOCK
45583** (2) RESERVED_LOCK
45584** (3) PENDING_LOCK
45585** (4) EXCLUSIVE_LOCK
45586**
45587** Sometimes when requesting one lock state, additional lock states
45588** are inserted in between. The locking might fail on one of the later
45589** transitions leaving the lock state different from what it started but
45590** still short of its goal. The following chart shows the allowed
45591** transitions and the inserted intermediate states:
45592**
45593** UNLOCKED -> SHARED
45594** SHARED -> RESERVED
45595** SHARED -> (PENDING) -> EXCLUSIVE
45596** RESERVED -> (PENDING) -> EXCLUSIVE
45597** PENDING -> EXCLUSIVE
45598**
45599** This routine will only increase a lock. The winUnlock() routine
45600** erases all locks at once and returns us immediately to locking level 0.
45601** It is not possible to lower the locking level one step at a time. You
45602** must go straight to locking level 0.
45603*/
45604static int winLock(sqlite3_file *id, int locktype){
45605 int rc = SQLITE_OK; /* Return code from subroutines */
45606 int res = 1; /* Result of a Windows lock call */
45607 int newLocktype; /* Set pFile->locktype to this value before exiting */
45608 int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
45609 winFile *pFile = (winFile*)id;
45610 DWORD lastErrno = NO_ERROR;
45611
45612 assert( id!=0 );
45613 OSTRACE(("LOCK file=%p, oldLock=%d(%d), newLock=%d\n",
45614 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
45615
45616 /* If there is already a lock of this type or more restrictive on the
45617 ** OsFile, do nothing. Don't use the end_lock: exit path, as
45618 ** sqlite3OsEnterMutex() hasn't been called yet.
45619 */
45620 if( pFile->locktype>=locktype ){
45621 OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
45622 return SQLITE_OK;
45623 }
45624
45625 /* Do not allow any kind of write-lock on a read-only database
45626 */
45627 if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK ){
45628 return SQLITE_IOERR_LOCK;
45629 }
45630
45631 /* Make sure the locking sequence is correct
45632 */
45633 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
45634 assert( locktype!=PENDING_LOCK );
45635 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
45636
45637 /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
45638 ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of
45639 ** the PENDING_LOCK byte is temporary.
45640 */
45641 newLocktype = pFile->locktype;
45642 if( pFile->locktype==NO_LOCK
45643 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<=RESERVED_LOCK)
45644 ){
45645 int cnt = 3;
45646 while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
45647 PENDING_BYTE, 0, 1, 0))==0 ){
45648 /* Try 3 times to get the pending lock. This is needed to work
45649 ** around problems caused by indexing and/or anti-virus software on
45650 ** Windows systems.
45651 ** If you are using this code as a model for alternative VFSes, do not
45652 ** copy this retry logic. It is a hack intended for Windows only.
45653 */
45654 lastErrno = osGetLastError();
45655 OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
45656 pFile->h, cnt, res));
45657 if( lastErrno==ERROR_INVALID_HANDLE ){
45658 pFile->lastErrno = lastErrno;
45659 rc = SQLITE_IOERR_LOCK;
45660 OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
45661 pFile->h, cnt, sqlite3ErrName(rc)));
45662 return rc;
45663 }
45664 if( cnt ) sqlite3_win32_sleep(1);
45665 }
45666 gotPendingLock = res;
45667 if( !res ){
45668 lastErrno = osGetLastError();
45669 }
45670 }
45671
45672 /* Acquire a shared lock
45673 */
45674 if( locktype==SHARED_LOCK && res ){
45675 assert( pFile->locktype==NO_LOCK );
45676 res = winGetReadLock(pFile);
45677 if( res ){
45678 newLocktype = SHARED_LOCK;
45679 }else{
45680 lastErrno = osGetLastError();
45681 }
45682 }
45683
45684 /* Acquire a RESERVED lock
45685 */
45686 if( locktype==RESERVED_LOCK && res ){
45687 assert( pFile->locktype==SHARED_LOCK );
45688 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
45689 if( res ){
45690 newLocktype = RESERVED_LOCK;
45691 }else{
45692 lastErrno = osGetLastError();
45693 }
45694 }
45695
45696 /* Acquire a PENDING lock
45697 */
45698 if( locktype==EXCLUSIVE_LOCK && res ){
45699 newLocktype = PENDING_LOCK;
45700 gotPendingLock = 0;
45701 }
45702
45703 /* Acquire an EXCLUSIVE lock
45704 */
45705 if( locktype==EXCLUSIVE_LOCK && res ){
45706 assert( pFile->locktype>=SHARED_LOCK );
45707 res = winUnlockReadLock(pFile);
45708 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
45709 SHARED_SIZE, 0);
45710 if( res ){
45711 newLocktype = EXCLUSIVE_LOCK;
45712 }else{
45713 lastErrno = osGetLastError();
45714 winGetReadLock(pFile);
45715 }
45716 }
45717
45718 /* If we are holding a PENDING lock that ought to be released, then
45719 ** release it now.
45720 */
45721 if( gotPendingLock && locktype==SHARED_LOCK ){
45722 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
45723 }
45724
45725 /* Update the state of the lock has held in the file descriptor then
45726 ** return the appropriate result code.
45727 */
45728 if( res ){
45729 rc = SQLITE_OK;
45730 }else{
45731 pFile->lastErrno = lastErrno;
45732 rc = SQLITE_BUSY;
45733 OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
45734 pFile->h, locktype, newLocktype));
45735 }
45736 pFile->locktype = (u8)newLocktype;
45737 OSTRACE(("LOCK file=%p, lock=%d, rc=%s\n",
45738 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
45739 return rc;
45740}
45741
45742/*
45743** This routine checks if there is a RESERVED lock held on the specified
45744** file by this or any other process. If such a lock is held, return
45745** non-zero, otherwise zero.
45746*/
45747static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
45748 int res;
45749 winFile *pFile = (winFile*)id;
45750
45751 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
45752 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
45753
45754 assert( id!=0 );
45755 if( pFile->locktype>=RESERVED_LOCK ){
45756 res = 1;
45757 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
45758 }else{
45759 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE,0,1,0);
45760 if( res ){
45761 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
45762 }
45763 res = !res;
45764 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res));
45765 }
45766 *pResOut = res;
45767 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
45768 pFile->h, pResOut, *pResOut));
45769 return SQLITE_OK;
45770}
45771
45772/*
45773** Lower the locking level on file descriptor id to locktype. locktype
45774** must be either NO_LOCK or SHARED_LOCK.
45775**
45776** If the locking level of the file descriptor is already at or below
45777** the requested locking level, this routine is a no-op.
45778**
45779** It is not possible for this routine to fail if the second argument
45780** is NO_LOCK. If the second argument is SHARED_LOCK then this routine
45781** might return SQLITE_IOERR;
45782*/
45783static int winUnlock(sqlite3_file *id, int locktype){
45784 int type;
45785 winFile *pFile = (winFile*)id;
45786 int rc = SQLITE_OK;
45787 assert( pFile!=0 );
45788 assert( locktype<=SHARED_LOCK );
45789 OSTRACE(("UNLOCK file=%p, oldLock=%d(%d), newLock=%d\n",
45790 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
45791 type = pFile->locktype;
45792 if( type>=EXCLUSIVE_LOCK ){
45793 winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
45794 if( locktype==SHARED_LOCK && !winGetReadLock(pFile) ){
45795 /* This should never happen. We should always be able to
45796 ** reacquire the read lock */
45797 rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(),
45798 "winUnlock", pFile->zPath);
45799 }
45800 }
45801 if( type>=RESERVED_LOCK ){
45802 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
45803 }
45804 if( locktype==NO_LOCK && type>=SHARED_LOCK ){
45805 winUnlockReadLock(pFile);
45806 }
45807 if( type>=PENDING_LOCK ){
45808 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
45809 }
45810 pFile->locktype = (u8)locktype;
45811 OSTRACE(("UNLOCK file=%p, lock=%d, rc=%s\n",
45812 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
45813 return rc;
45814}
45815
45816/******************************************************************************
45817****************************** No-op Locking **********************************
45818**
45819** Of the various locking implementations available, this is by far the
45820** simplest: locking is ignored. No attempt is made to lock the database
45821** file for reading or writing.
45822**
45823** This locking mode is appropriate for use on read-only databases
45824** (ex: databases that are burned into CD-ROM, for example.) It can
45825** also be used if the application employs some external mechanism to
45826** prevent simultaneous access of the same database by two or more
45827** database connections. But there is a serious risk of database
45828** corruption if this locking mode is used in situations where multiple
45829** database connections are accessing the same database file at the same
45830** time and one or more of those connections are writing.
45831*/
45832
45833static int winNolockLock(sqlite3_file *id, int locktype){
45834 UNUSED_PARAMETER(id);
45835 UNUSED_PARAMETER(locktype);
45836 return SQLITE_OK;
45837}
45838
45839static int winNolockCheckReservedLock(sqlite3_file *id, int *pResOut){
45840 UNUSED_PARAMETER(id);
45841 UNUSED_PARAMETER(pResOut);
45842 return SQLITE_OK;
45843}
45844
45845static int winNolockUnlock(sqlite3_file *id, int locktype){
45846 UNUSED_PARAMETER(id);
45847 UNUSED_PARAMETER(locktype);
45848 return SQLITE_OK;
45849}
45850
45851/******************* End of the no-op lock implementation *********************
45852******************************************************************************/
45853
45854/*
45855** If *pArg is initially negative then this is a query. Set *pArg to
45856** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
45857**
45858** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
45859*/
45860static void winModeBit(winFile *pFile, unsigned char mask, int *pArg){
45861 if( *pArg<0 ){
45862 *pArg = (pFile->ctrlFlags & mask)!=0;
45863 }else if( (*pArg)==0 ){
45864 pFile->ctrlFlags &= ~mask;
45865 }else{
45866 pFile->ctrlFlags |= mask;
45867 }
45868}
45869
45870/* Forward references to VFS helper methods used for temporary files */
45871static int winGetTempname(sqlite3_vfs *, char **);
45872static int winIsDir(const void *);
45873static BOOL winIsLongPathPrefix(const char *);
45874static BOOL winIsDriveLetterAndColon(const char *);
45875
45876/*
45877** Control and query of the open file handle.
45878*/
45879static int winFileControl(sqlite3_file *id, int op, void *pArg){
45880 winFile *pFile = (winFile*)id;
45881 OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg));
45882 switch( op ){
45883 case SQLITE_FCNTL_LOCKSTATE: {
45884 *(int*)pArg = pFile->locktype;
45885 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45886 return SQLITE_OK;
45887 }
45888 case SQLITE_FCNTL_LAST_ERRNO: {
45889 *(int*)pArg = (int)pFile->lastErrno;
45890 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45891 return SQLITE_OK;
45892 }
45893 case SQLITE_FCNTL_CHUNK_SIZE: {
45894 pFile->szChunk = *(int *)pArg;
45895 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45896 return SQLITE_OK;
45897 }
45898 case SQLITE_FCNTL_SIZE_HINT: {
45899 if( pFile->szChunk>0 ){
45900 sqlite3_int64 oldSz;
45901 int rc = winFileSize(id, &oldSz);
45902 if( rc==SQLITE_OK ){
45903 sqlite3_int64 newSz = *(sqlite3_int64*)pArg;
45904 if( newSz>oldSz ){
45905 SimulateIOErrorBenign(1);
45906 rc = winTruncate(id, newSz);
45907 SimulateIOErrorBenign(0);
45908 }
45909 }
45910 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
45911 return rc;
45912 }
45913 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45914 return SQLITE_OK;
45915 }
45916 case SQLITE_FCNTL_PERSIST_WAL: {
45917 winModeBit(pFile, WINFILE_PERSIST_WAL, (int*)pArg);
45918 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45919 return SQLITE_OK;
45920 }
45921 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
45922 winModeBit(pFile, WINFILE_PSOW, (int*)pArg);
45923 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45924 return SQLITE_OK;
45925 }
45926 case SQLITE_FCNTL_VFSNAME: {
45927 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
45928 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45929 return SQLITE_OK;
45930 }
45931 case SQLITE_FCNTL_WIN32_AV_RETRY: {
45932 int *a = (int*)pArg;
45933 if( a[0]>0 ){
45934 winIoerrRetry = a[0];
45935 }else{
45936 a[0] = winIoerrRetry;
45937 }
45938 if( a[1]>0 ){
45939 winIoerrRetryDelay = a[1];
45940 }else{
45941 a[1] = winIoerrRetryDelay;
45942 }
45943 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45944 return SQLITE_OK;
45945 }
45946 case SQLITE_FCNTL_WIN32_GET_HANDLE: {
45947 LPHANDLE phFile = (LPHANDLE)pArg;
45948 *phFile = pFile->h;
45949 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45950 return SQLITE_OK;
45951 }
45952#ifdef SQLITE_TEST
45953 case SQLITE_FCNTL_WIN32_SET_HANDLE: {
45954 LPHANDLE phFile = (LPHANDLE)pArg;
45955 HANDLE hOldFile = pFile->h;
45956 pFile->h = *phFile;
45957 *phFile = hOldFile;
45958 OSTRACE(("FCNTL oldFile=%p, newFile=%p, rc=SQLITE_OK\n",
45959 hOldFile, pFile->h));
45960 return SQLITE_OK;
45961 }
45962#endif
45963 case SQLITE_FCNTL_TEMPFILENAME: {
45964 char *zTFile = 0;
45965 int rc = winGetTempname(pFile->pVfs, &zTFile);
45966 if( rc==SQLITE_OK ){
45967 *(char**)pArg = zTFile;
45968 }
45969 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
45970 return rc;
45971 }
45972#if SQLITE_MAX_MMAP_SIZE>0
45973 case SQLITE_FCNTL_MMAP_SIZE: {
45974 i64 newLimit = *(i64*)pArg;
45975 int rc = SQLITE_OK;
45976 if( newLimit>sqlite3GlobalConfig.mxMmap ){
45977 newLimit = sqlite3GlobalConfig.mxMmap;
45978 }
45979
45980 /* The value of newLimit may be eventually cast to (SIZE_T) and passed
45981 ** to MapViewOfFile(). Restrict its value to 2GB if (SIZE_T) is not at
45982 ** least a 64-bit type. */
45983 if( newLimit>0 && sizeof(SIZE_T)<8 ){
45984 newLimit = (newLimit & 0x7FFFFFFF);
45985 }
45986
45987 *(i64*)pArg = pFile->mmapSizeMax;
45988 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
45989 pFile->mmapSizeMax = newLimit;
45990 if( pFile->mmapSize>0 ){
45991 winUnmapfile(pFile);
45992 rc = winMapfile(pFile, -1);
45993 }
45994 }
45995 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
45996 return rc;
45997 }
45998#endif
45999 }
46000 OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
46001 return SQLITE_NOTFOUND;
46002}
46003
46004/*
46005** Return the sector size in bytes of the underlying block device for
46006** the specified file. This is almost always 512 bytes, but may be
46007** larger for some devices.
46008**
46009** SQLite code assumes this function cannot fail. It also assumes that
46010** if two files are created in the same file-system directory (i.e.
46011** a database and its journal file) that the sector size will be the
46012** same for both.
46013*/
46014static int winSectorSize(sqlite3_file *id){
46015 (void)id;
46016 return SQLITE_DEFAULT_SECTOR_SIZE;
46017}
46018
46019/*
46020** Return a vector of device characteristics.
46021*/
46022static int winDeviceCharacteristics(sqlite3_file *id){
46023 winFile *p = (winFile*)id;
46024 return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
46025 ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
46026}
46027
46028/*
46029** Windows will only let you create file view mappings
46030** on allocation size granularity boundaries.
46031** During sqlite3_os_init() we do a GetSystemInfo()
46032** to get the granularity size.
46033*/
46034static SYSTEM_INFO winSysInfo;
46035
46036#ifndef SQLITE_OMIT_WAL
46037
46038/*
46039** Helper functions to obtain and relinquish the global mutex. The
46040** global mutex is used to protect the winLockInfo objects used by
46041** this file, all of which may be shared by multiple threads.
46042**
46043** Function winShmMutexHeld() is used to assert() that the global mutex
46044** is held when required. This function is only used as part of assert()
46045** statements. e.g.
46046**
46047** winShmEnterMutex()
46048** assert( winShmMutexHeld() );
46049** winShmLeaveMutex()
46050*/
46051static sqlite3_mutex *winBigLock = 0;
46052static void winShmEnterMutex(void){
46053 sqlite3_mutex_enter(winBigLock);
46054}
46055static void winShmLeaveMutex(void){
46056 sqlite3_mutex_leave(winBigLock);
46057}
46058#ifndef NDEBUG
46059static int winShmMutexHeld(void) {
46060 return sqlite3_mutex_held(winBigLock);
46061}
46062#endif
46063
46064/*
46065** Object used to represent a single file opened and mmapped to provide
46066** shared memory. When multiple threads all reference the same
46067** log-summary, each thread has its own winFile object, but they all
46068** point to a single instance of this object. In other words, each
46069** log-summary is opened only once per process.
46070**
46071** winShmMutexHeld() must be true when creating or destroying
46072** this object or while reading or writing the following fields:
46073**
46074** nRef
46075** pNext
46076**
46077** The following fields are read-only after the object is created:
46078**
46079** fid
46080** zFilename
46081**
46082** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
46083** winShmMutexHeld() is true when reading or writing any other field
46084** in this structure.
46085**
46086*/
46087struct winShmNode {
46088 sqlite3_mutex *mutex; /* Mutex to access this object */
46089 char *zFilename; /* Name of the file */
46090 winFile hFile; /* File handle from winOpen */
46091
46092 int szRegion; /* Size of shared-memory regions */
46093 int nRegion; /* Size of array apRegion */
46094 u8 isReadonly; /* True if read-only */
46095 u8 isUnlocked; /* True if no DMS lock held */
46096
46097 struct ShmRegion {
46098 HANDLE hMap; /* File handle from CreateFileMapping */
46099 void *pMap;
46100 } *aRegion;
46101 DWORD lastErrno; /* The Windows errno from the last I/O error */
46102
46103 int nRef; /* Number of winShm objects pointing to this */
46104 winShm *pFirst; /* All winShm objects pointing to this */
46105 winShmNode *pNext; /* Next in list of all winShmNode objects */
46106#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
46107 u8 nextShmId; /* Next available winShm.id value */
46108#endif
46109};
46110
46111/*
46112** A global array of all winShmNode objects.
46113**
46114** The winShmMutexHeld() must be true while reading or writing this list.
46115*/
46116static winShmNode *winShmNodeList = 0;
46117
46118/*
46119** Structure used internally by this VFS to record the state of an
46120** open shared memory connection.
46121**
46122** The following fields are initialized when this object is created and
46123** are read-only thereafter:
46124**
46125** winShm.pShmNode
46126** winShm.id
46127**
46128** All other fields are read/write. The winShm.pShmNode->mutex must be held
46129** while accessing any read/write fields.
46130*/
46131struct winShm {
46132 winShmNode *pShmNode; /* The underlying winShmNode object */
46133 winShm *pNext; /* Next winShm with the same winShmNode */
46134 u8 hasMutex; /* True if holding the winShmNode mutex */
46135 u16 sharedMask; /* Mask of shared locks held */
46136 u16 exclMask; /* Mask of exclusive locks held */
46137#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
46138 u8 id; /* Id of this connection with its winShmNode */
46139#endif
46140};
46141
46142/*
46143** Constants used for locking
46144*/
46145#define WIN_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
46146#define WIN_SHM_DMS (WIN_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
46147
46148/*
46149** Apply advisory locks for all n bytes beginning at ofst.
46150*/
46151#define WINSHM_UNLCK 1
46152#define WINSHM_RDLCK 2
46153#define WINSHM_WRLCK 3
46154static int winShmSystemLock(
46155 winShmNode *pFile, /* Apply locks to this open shared-memory segment */
46156 int lockType, /* WINSHM_UNLCK, WINSHM_RDLCK, or WINSHM_WRLCK */
46157 int ofst, /* Offset to first byte to be locked/unlocked */
46158 int nByte /* Number of bytes to lock or unlock */
46159){
46160 int rc = 0; /* Result code form Lock/UnlockFileEx() */
46161
46162 /* Access to the winShmNode object is serialized by the caller */
46163 assert( pFile->nRef==0 || sqlite3_mutex_held(pFile->mutex) );
46164
46165 OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
46166 pFile->hFile.h, lockType, ofst, nByte));
46167
46168 /* Release/Acquire the system-level lock */
46169 if( lockType==WINSHM_UNLCK ){
46170 rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
46171 }else{
46172 /* Initialize the locking parameters */
46173 DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
46174 if( lockType == WINSHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
46175 rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
46176 }
46177
46178 if( rc!= 0 ){
46179 rc = SQLITE_OK;
46180 }else{
46181 pFile->lastErrno = osGetLastError();
46182 rc = SQLITE_BUSY;
46183 }
46184
46185 OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
46186 pFile->hFile.h, (lockType == WINSHM_UNLCK) ? "winUnlockFile" :
46187 "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
46188
46189 return rc;
46190}
46191
46192/* Forward references to VFS methods */
46193static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
46194static int winDelete(sqlite3_vfs *,const char*,int);
46195
46196/*
46197** Purge the winShmNodeList list of all entries with winShmNode.nRef==0.
46198**
46199** This is not a VFS shared-memory method; it is a utility function called
46200** by VFS shared-memory methods.
46201*/
46202static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
46203 winShmNode **pp;
46204 winShmNode *p;
46205 assert( winShmMutexHeld() );
46206 OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n",
46207 osGetCurrentProcessId(), deleteFlag));
46208 pp = &winShmNodeList;
46209 while( (p = *pp)!=0 ){
46210 if( p->nRef==0 ){
46211 int i;
46212 if( p->mutex ){ sqlite3_mutex_free(p->mutex); }
46213 for(i=0; i<p->nRegion; i++){
46214 BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
46215 OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n",
46216 osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
46217 UNUSED_VARIABLE_VALUE(bRc);
46218 bRc = osCloseHandle(p->aRegion[i].hMap);
46219 OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
46220 osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
46221 UNUSED_VARIABLE_VALUE(bRc);
46222 }
46223 if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){
46224 SimulateIOErrorBenign(1);
46225 winClose((sqlite3_file *)&p->hFile);
46226 SimulateIOErrorBenign(0);
46227 }
46228 if( deleteFlag ){
46229 SimulateIOErrorBenign(1);
46230 sqlite3BeginBenignMalloc();
46231 winDelete(pVfs, p->zFilename, 0);
46232 sqlite3EndBenignMalloc();
46233 SimulateIOErrorBenign(0);
46234 }
46235 *pp = p->pNext;
46236 sqlite3_free(p->aRegion);
46237 sqlite3_free(p);
46238 }else{
46239 pp = &p->pNext;
46240 }
46241 }
46242}
46243
46244/*
46245** The DMS lock has not yet been taken on shm file pShmNode. Attempt to
46246** take it now. Return SQLITE_OK if successful, or an SQLite error
46247** code otherwise.
46248**
46249** If the DMS cannot be locked because this is a readonly_shm=1
46250** connection and no other process already holds a lock, return
46251** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
46252*/
46253static int winLockSharedMemory(winShmNode *pShmNode){
46254 int rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, WIN_SHM_DMS, 1);
46255
46256 if( rc==SQLITE_OK ){
46257 if( pShmNode->isReadonly ){
46258 pShmNode->isUnlocked = 1;
46259 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
46260 return SQLITE_READONLY_CANTINIT;
46261 }else if( winTruncate((sqlite3_file*)&pShmNode->hFile, 0) ){
46262 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
46263 return winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
46264 "winLockSharedMemory", pShmNode->zFilename);
46265 }
46266 }
46267
46268 if( rc==SQLITE_OK ){
46269 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
46270 }
46271
46272 return winShmSystemLock(pShmNode, WINSHM_RDLCK, WIN_SHM_DMS, 1);
46273}
46274
46275/*
46276** Open the shared-memory area associated with database file pDbFd.
46277**
46278** When opening a new shared-memory file, if no other instances of that
46279** file are currently open, in this process or in other processes, then
46280** the file must be truncated to zero length or have its header cleared.
46281*/
46282static int winOpenSharedMemory(winFile *pDbFd){
46283 struct winShm *p; /* The connection to be opened */
46284 winShmNode *pShmNode = 0; /* The underlying mmapped file */
46285 int rc = SQLITE_OK; /* Result code */
46286 winShmNode *pNew; /* Newly allocated winShmNode */
46287 int nName; /* Size of zName in bytes */
46288
46289 assert( pDbFd->pShm==0 ); /* Not previously opened */
46290
46291 /* Allocate space for the new sqlite3_shm object. Also speculatively
46292 ** allocate space for a new winShmNode and filename.
46293 */
46294 p = sqlite3MallocZero( sizeof(*p) );
46295 if( p==0 ) return SQLITE_IOERR_NOMEM_BKPT;
46296 nName = sqlite3Strlen30(pDbFd->zPath);
46297 pNew = sqlite3MallocZero( sizeof(*pShmNode) + nName + 17 );
46298 if( pNew==0 ){
46299 sqlite3_free(p);
46300 return SQLITE_IOERR_NOMEM_BKPT;
46301 }
46302 pNew->zFilename = (char*)&pNew[1];
46303 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
46304 sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
46305
46306 /* Look to see if there is an existing winShmNode that can be used.
46307 ** If no matching winShmNode currently exists, create a new one.
46308 */
46309 winShmEnterMutex();
46310 for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
46311 /* TBD need to come up with better match here. Perhaps
46312 ** use FILE_ID_BOTH_DIR_INFO Structure.
46313 */
46314 if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
46315 }
46316 if( pShmNode ){
46317 sqlite3_free(pNew);
46318 }else{
46319 int inFlags = SQLITE_OPEN_WAL;
46320 int outFlags = 0;
46321
46322 pShmNode = pNew;
46323 pNew = 0;
46324 ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
46325 pShmNode->pNext = winShmNodeList;
46326 winShmNodeList = pShmNode;
46327
46328 if( sqlite3GlobalConfig.bCoreMutex ){
46329 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
46330 if( pShmNode->mutex==0 ){
46331 rc = SQLITE_IOERR_NOMEM_BKPT;
46332 goto shm_open_err;
46333 }
46334 }
46335
46336 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
46337 inFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
46338 }else{
46339 inFlags |= SQLITE_OPEN_READONLY;
46340 }
46341 rc = winOpen(pDbFd->pVfs, pShmNode->zFilename,
46342 (sqlite3_file*)&pShmNode->hFile,
46343 inFlags, &outFlags);
46344 if( rc!=SQLITE_OK ){
46345 rc = winLogError(rc, osGetLastError(), "winOpenShm",
46346 pShmNode->zFilename);
46347 goto shm_open_err;
46348 }
46349 if( outFlags==SQLITE_OPEN_READONLY ) pShmNode->isReadonly = 1;
46350
46351 rc = winLockSharedMemory(pShmNode);
46352 if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err;
46353 }
46354
46355 /* Make the new connection a child of the winShmNode */
46356 p->pShmNode = pShmNode;
46357#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
46358 p->id = pShmNode->nextShmId++;
46359#endif
46360 pShmNode->nRef++;
46361 pDbFd->pShm = p;
46362 winShmLeaveMutex();
46363
46364 /* The reference count on pShmNode has already been incremented under
46365 ** the cover of the winShmEnterMutex() mutex and the pointer from the
46366 ** new (struct winShm) object to the pShmNode has been set. All that is
46367 ** left to do is to link the new object into the linked list starting
46368 ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
46369 ** mutex.
46370 */
46371 sqlite3_mutex_enter(pShmNode->mutex);
46372 p->pNext = pShmNode->pFirst;
46373 pShmNode->pFirst = p;
46374 sqlite3_mutex_leave(pShmNode->mutex);
46375 return rc;
46376
46377 /* Jump here on any error */
46378shm_open_err:
46379 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
46380 winShmPurge(pDbFd->pVfs, 0); /* This call frees pShmNode if required */
46381 sqlite3_free(p);
46382 sqlite3_free(pNew);
46383 winShmLeaveMutex();
46384 return rc;
46385}
46386
46387/*
46388** Close a connection to shared-memory. Delete the underlying
46389** storage if deleteFlag is true.
46390*/
46391static int winShmUnmap(
46392 sqlite3_file *fd, /* Database holding shared memory */
46393 int deleteFlag /* Delete after closing if true */
46394){
46395 winFile *pDbFd; /* Database holding shared-memory */
46396 winShm *p; /* The connection to be closed */
46397 winShmNode *pShmNode; /* The underlying shared-memory file */
46398 winShm **pp; /* For looping over sibling connections */
46399
46400 pDbFd = (winFile*)fd;
46401 p = pDbFd->pShm;
46402 if( p==0 ) return SQLITE_OK;
46403 pShmNode = p->pShmNode;
46404
46405 /* Remove connection p from the set of connections associated
46406 ** with pShmNode */
46407 sqlite3_mutex_enter(pShmNode->mutex);
46408 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
46409 *pp = p->pNext;
46410
46411 /* Free the connection p */
46412 sqlite3_free(p);
46413 pDbFd->pShm = 0;
46414 sqlite3_mutex_leave(pShmNode->mutex);
46415
46416 /* If pShmNode->nRef has reached 0, then close the underlying
46417 ** shared-memory file, too */
46418 winShmEnterMutex();
46419 assert( pShmNode->nRef>0 );
46420 pShmNode->nRef--;
46421 if( pShmNode->nRef==0 ){
46422 winShmPurge(pDbFd->pVfs, deleteFlag);
46423 }
46424 winShmLeaveMutex();
46425
46426 return SQLITE_OK;
46427}
46428
46429/*
46430** Change the lock state for a shared-memory segment.
46431*/
46432static int winShmLock(
46433 sqlite3_file *fd, /* Database file holding the shared memory */
46434 int ofst, /* First lock to acquire or release */
46435 int n, /* Number of locks to acquire or release */
46436 int flags /* What to do with the lock */
46437){
46438 winFile *pDbFd = (winFile*)fd; /* Connection holding shared memory */
46439 winShm *p = pDbFd->pShm; /* The shared memory being locked */
46440 winShm *pX; /* For looping over all siblings */
46441 winShmNode *pShmNode = p->pShmNode;
46442 int rc = SQLITE_OK; /* Result code */
46443 u16 mask; /* Mask of locks to take or release */
46444
46445 assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
46446 assert( n>=1 );
46447 assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
46448 || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
46449 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
46450 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
46451 assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
46452
46453 mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
46454 assert( n>1 || mask==(1<<ofst) );
46455 sqlite3_mutex_enter(pShmNode->mutex);
46456 if( flags & SQLITE_SHM_UNLOCK ){
46457 u16 allMask = 0; /* Mask of locks held by siblings */
46458
46459 /* See if any siblings hold this same lock */
46460 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
46461 if( pX==p ) continue;
46462 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
46463 allMask |= pX->sharedMask;
46464 }
46465
46466 /* Unlock the system-level locks */
46467 if( (mask & allMask)==0 ){
46468 rc = winShmSystemLock(pShmNode, WINSHM_UNLCK, ofst+WIN_SHM_BASE, n);
46469 }else{
46470 rc = SQLITE_OK;
46471 }
46472
46473 /* Undo the local locks */
46474 if( rc==SQLITE_OK ){
46475 p->exclMask &= ~mask;
46476 p->sharedMask &= ~mask;
46477 }
46478 }else if( flags & SQLITE_SHM_SHARED ){
46479 u16 allShared = 0; /* Union of locks held by connections other than "p" */
46480
46481 /* Find out which shared locks are already held by sibling connections.
46482 ** If any sibling already holds an exclusive lock, go ahead and return
46483 ** SQLITE_BUSY.
46484 */
46485 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
46486 if( (pX->exclMask & mask)!=0 ){
46487 rc = SQLITE_BUSY;
46488 break;
46489 }
46490 allShared |= pX->sharedMask;
46491 }
46492
46493 /* Get shared locks at the system level, if necessary */
46494 if( rc==SQLITE_OK ){
46495 if( (allShared & mask)==0 ){
46496 rc = winShmSystemLock(pShmNode, WINSHM_RDLCK, ofst+WIN_SHM_BASE, n);
46497 }else{
46498 rc = SQLITE_OK;
46499 }
46500 }
46501
46502 /* Get the local shared locks */
46503 if( rc==SQLITE_OK ){
46504 p->sharedMask |= mask;
46505 }
46506 }else{
46507 /* Make sure no sibling connections hold locks that will block this
46508 ** lock. If any do, return SQLITE_BUSY right away.
46509 */
46510 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
46511 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
46512 rc = SQLITE_BUSY;
46513 break;
46514 }
46515 }
46516
46517 /* Get the exclusive locks at the system level. Then if successful
46518 ** also mark the local connection as being locked.
46519 */
46520 if( rc==SQLITE_OK ){
46521 rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, ofst+WIN_SHM_BASE, n);
46522 if( rc==SQLITE_OK ){
46523 assert( (p->sharedMask & mask)==0 );
46524 p->exclMask |= mask;
46525 }
46526 }
46527 }
46528 sqlite3_mutex_leave(pShmNode->mutex);
46529 OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n",
46530 osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
46531 sqlite3ErrName(rc)));
46532 return rc;
46533}
46534
46535/*
46536** Implement a memory barrier or memory fence on shared memory.
46537**
46538** All loads and stores begun before the barrier must complete before
46539** any load or store begun after the barrier.
46540*/
46541static void winShmBarrier(
46542 sqlite3_file *fd /* Database holding the shared memory */
46543){
46544 UNUSED_PARAMETER(fd);
46545 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
46546 winShmEnterMutex(); /* Also mutex, for redundancy */
46547 winShmLeaveMutex();
46548}
46549
46550/*
46551** This function is called to obtain a pointer to region iRegion of the
46552** shared-memory associated with the database file fd. Shared-memory regions
46553** are numbered starting from zero. Each shared-memory region is szRegion
46554** bytes in size.
46555**
46556** If an error occurs, an error code is returned and *pp is set to NULL.
46557**
46558** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
46559** region has not been allocated (by any client, including one running in a
46560** separate process), then *pp is set to NULL and SQLITE_OK returned. If
46561** isWrite is non-zero and the requested shared-memory region has not yet
46562** been allocated, it is allocated by this function.
46563**
46564** If the shared-memory region has already been allocated or is allocated by
46565** this call as described above, then it is mapped into this processes
46566** address space (if it is not already), *pp is set to point to the mapped
46567** memory and SQLITE_OK returned.
46568*/
46569static int winShmMap(
46570 sqlite3_file *fd, /* Handle open on database file */
46571 int iRegion, /* Region to retrieve */
46572 int szRegion, /* Size of regions */
46573 int isWrite, /* True to extend file if necessary */
46574 void volatile **pp /* OUT: Mapped memory */
46575){
46576 winFile *pDbFd = (winFile*)fd;
46577 winShm *pShm = pDbFd->pShm;
46578 winShmNode *pShmNode;
46579 DWORD protect = PAGE_READWRITE;
46580 DWORD flags = FILE_MAP_WRITE | FILE_MAP_READ;
46581 int rc = SQLITE_OK;
46582
46583 if( !pShm ){
46584 rc = winOpenSharedMemory(pDbFd);
46585 if( rc!=SQLITE_OK ) return rc;
46586 pShm = pDbFd->pShm;
46587 assert( pShm!=0 );
46588 }
46589 pShmNode = pShm->pShmNode;
46590
46591 sqlite3_mutex_enter(pShmNode->mutex);
46592 if( pShmNode->isUnlocked ){
46593 rc = winLockSharedMemory(pShmNode);
46594 if( rc!=SQLITE_OK ) goto shmpage_out;
46595 pShmNode->isUnlocked = 0;
46596 }
46597 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
46598
46599 if( pShmNode->nRegion<=iRegion ){
46600 struct ShmRegion *apNew; /* New aRegion[] array */
46601 int nByte = (iRegion+1)*szRegion; /* Minimum required file size */
46602 sqlite3_int64 sz; /* Current size of wal-index file */
46603
46604 pShmNode->szRegion = szRegion;
46605
46606 /* The requested region is not mapped into this processes address space.
46607 ** Check to see if it has been allocated (i.e. if the wal-index file is
46608 ** large enough to contain the requested region).
46609 */
46610 rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
46611 if( rc!=SQLITE_OK ){
46612 rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
46613 "winShmMap1", pDbFd->zPath);
46614 goto shmpage_out;
46615 }
46616
46617 if( sz<nByte ){
46618 /* The requested memory region does not exist. If isWrite is set to
46619 ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
46620 **
46621 ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
46622 ** the requested memory region.
46623 */
46624 if( !isWrite ) goto shmpage_out;
46625 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
46626 if( rc!=SQLITE_OK ){
46627 rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
46628 "winShmMap2", pDbFd->zPath);
46629 goto shmpage_out;
46630 }
46631 }
46632
46633 /* Map the requested memory region into this processes address space. */
46634 apNew = (struct ShmRegion *)sqlite3_realloc64(
46635 pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
46636 );
46637 if( !apNew ){
46638 rc = SQLITE_IOERR_NOMEM_BKPT;
46639 goto shmpage_out;
46640 }
46641 pShmNode->aRegion = apNew;
46642
46643 if( pShmNode->isReadonly ){
46644 protect = PAGE_READONLY;
46645 flags = FILE_MAP_READ;
46646 }
46647
46648 while( pShmNode->nRegion<=iRegion ){
46649 HANDLE hMap = NULL; /* file-mapping handle */
46650 void *pMap = 0; /* Mapped memory region */
46651
46652#if SQLITE_OS_WINRT
46653 hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
46654 NULL, protect, nByte, NULL
46655 );
46656#elif defined(SQLITE_WIN32_HAS_WIDE)
46657 hMap = osCreateFileMappingW(pShmNode->hFile.h,
46658 NULL, protect, 0, nByte, NULL
46659 );
46660#elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
46661 hMap = osCreateFileMappingA(pShmNode->hFile.h,
46662 NULL, protect, 0, nByte, NULL
46663 );
46664#endif
46665 OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
46666 osGetCurrentProcessId(), pShmNode->nRegion, nByte,
46667 hMap ? "ok" : "failed"));
46668 if( hMap ){
46669 int iOffset = pShmNode->nRegion*szRegion;
46670 int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
46671#if SQLITE_OS_WINRT
46672 pMap = osMapViewOfFileFromApp(hMap, flags,
46673 iOffset - iOffsetShift, szRegion + iOffsetShift
46674 );
46675#else
46676 pMap = osMapViewOfFile(hMap, flags,
46677 0, iOffset - iOffsetShift, szRegion + iOffsetShift
46678 );
46679#endif
46680 OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n",
46681 osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
46682 szRegion, pMap ? "ok" : "failed"));
46683 }
46684 if( !pMap ){
46685 pShmNode->lastErrno = osGetLastError();
46686 rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
46687 "winShmMap3", pDbFd->zPath);
46688 if( hMap ) osCloseHandle(hMap);
46689 goto shmpage_out;
46690 }
46691
46692 pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
46693 pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
46694 pShmNode->nRegion++;
46695 }
46696 }
46697
46698shmpage_out:
46699 if( pShmNode->nRegion>iRegion ){
46700 int iOffset = iRegion*szRegion;
46701 int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
46702 char *p = (char *)pShmNode->aRegion[iRegion].pMap;
46703 *pp = (void *)&p[iOffsetShift];
46704 }else{
46705 *pp = 0;
46706 }
46707 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
46708 sqlite3_mutex_leave(pShmNode->mutex);
46709 return rc;
46710}
46711
46712#else
46713# define winShmMap 0
46714# define winShmLock 0
46715# define winShmBarrier 0
46716# define winShmUnmap 0
46717#endif /* #ifndef SQLITE_OMIT_WAL */
46718
46719/*
46720** Cleans up the mapped region of the specified file, if any.
46721*/
46722#if SQLITE_MAX_MMAP_SIZE>0
46723static int winUnmapfile(winFile *pFile){
46724 assert( pFile!=0 );
46725 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
46726 "mmapSize=%lld, mmapSizeMax=%lld\n",
46727 osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
46728 pFile->mmapSize, pFile->mmapSizeMax));
46729 if( pFile->pMapRegion ){
46730 if( !osUnmapViewOfFile(pFile->pMapRegion) ){
46731 pFile->lastErrno = osGetLastError();
46732 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
46733 "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(), pFile,
46734 pFile->pMapRegion));
46735 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
46736 "winUnmapfile1", pFile->zPath);
46737 }
46738 pFile->pMapRegion = 0;
46739 pFile->mmapSize = 0;
46740 }
46741 if( pFile->hMap!=NULL ){
46742 if( !osCloseHandle(pFile->hMap) ){
46743 pFile->lastErrno = osGetLastError();
46744 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
46745 osGetCurrentProcessId(), pFile, pFile->hMap));
46746 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
46747 "winUnmapfile2", pFile->zPath);
46748 }
46749 pFile->hMap = NULL;
46750 }
46751 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
46752 osGetCurrentProcessId(), pFile));
46753 return SQLITE_OK;
46754}
46755
46756/*
46757** Memory map or remap the file opened by file-descriptor pFd (if the file
46758** is already mapped, the existing mapping is replaced by the new). Or, if
46759** there already exists a mapping for this file, and there are still
46760** outstanding xFetch() references to it, this function is a no-op.
46761**
46762** If parameter nByte is non-negative, then it is the requested size of
46763** the mapping to create. Otherwise, if nByte is less than zero, then the
46764** requested size is the size of the file on disk. The actual size of the
46765** created mapping is either the requested size or the value configured
46766** using SQLITE_FCNTL_MMAP_SIZE, whichever is smaller.
46767**
46768** SQLITE_OK is returned if no error occurs (even if the mapping is not
46769** recreated as a result of outstanding references) or an SQLite error
46770** code otherwise.
46771*/
46772static int winMapfile(winFile *pFd, sqlite3_int64 nByte){
46773 sqlite3_int64 nMap = nByte;
46774 int rc;
46775
46776 assert( nMap>=0 || pFd->nFetchOut==0 );
46777 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n",
46778 osGetCurrentProcessId(), pFd, nByte));
46779
46780 if( pFd->nFetchOut>0 ) return SQLITE_OK;
46781
46782 if( nMap<0 ){
46783 rc = winFileSize((sqlite3_file*)pFd, &nMap);
46784 if( rc ){
46785 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n",
46786 osGetCurrentProcessId(), pFd));
46787 return SQLITE_IOERR_FSTAT;
46788 }
46789 }
46790 if( nMap>pFd->mmapSizeMax ){
46791 nMap = pFd->mmapSizeMax;
46792 }
46793 nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
46794
46795 if( nMap==0 && pFd->mmapSize>0 ){
46796 winUnmapfile(pFd);
46797 }
46798 if( nMap!=pFd->mmapSize ){
46799 void *pNew = 0;
46800 DWORD protect = PAGE_READONLY;
46801 DWORD flags = FILE_MAP_READ;
46802
46803 winUnmapfile(pFd);
46804#ifdef SQLITE_MMAP_READWRITE
46805 if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
46806 protect = PAGE_READWRITE;
46807 flags |= FILE_MAP_WRITE;
46808 }
46809#endif
46810#if SQLITE_OS_WINRT
46811 pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
46812#elif defined(SQLITE_WIN32_HAS_WIDE)
46813 pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
46814 (DWORD)((nMap>>32) & 0xffffffff),
46815 (DWORD)(nMap & 0xffffffff), NULL);
46816#elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
46817 pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect,
46818 (DWORD)((nMap>>32) & 0xffffffff),
46819 (DWORD)(nMap & 0xffffffff), NULL);
46820#endif
46821 if( pFd->hMap==NULL ){
46822 pFd->lastErrno = osGetLastError();
46823 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
46824 "winMapfile1", pFd->zPath);
46825 /* Log the error, but continue normal operation using xRead/xWrite */
46826 OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n",
46827 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
46828 return SQLITE_OK;
46829 }
46830 assert( (nMap % winSysInfo.dwPageSize)==0 );
46831 assert( sizeof(SIZE_T)==sizeof(sqlite3_int64) || nMap<=0xffffffff );
46832#if SQLITE_OS_WINRT
46833 pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap);
46834#else
46835 pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
46836#endif
46837 if( pNew==NULL ){
46838 osCloseHandle(pFd->hMap);
46839 pFd->hMap = NULL;
46840 pFd->lastErrno = osGetLastError();
46841 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
46842 "winMapfile2", pFd->zPath);
46843 /* Log the error, but continue normal operation using xRead/xWrite */
46844 OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
46845 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
46846 return SQLITE_OK;
46847 }
46848 pFd->pMapRegion = pNew;
46849 pFd->mmapSize = nMap;
46850 }
46851
46852 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
46853 osGetCurrentProcessId(), pFd));
46854 return SQLITE_OK;
46855}
46856#endif /* SQLITE_MAX_MMAP_SIZE>0 */
46857
46858/*
46859** If possible, return a pointer to a mapping of file fd starting at offset
46860** iOff. The mapping must be valid for at least nAmt bytes.
46861**
46862** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
46863** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
46864** Finally, if an error does occur, return an SQLite error code. The final
46865** value of *pp is undefined in this case.
46866**
46867** If this function does return a pointer, the caller must eventually
46868** release the reference by calling winUnfetch().
46869*/
46870static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
46871#if SQLITE_MAX_MMAP_SIZE>0
46872 winFile *pFd = (winFile*)fd; /* The underlying database file */
46873#endif
46874 *pp = 0;
46875
46876 OSTRACE(("FETCH pid=%lu, pFile=%p, offset=%lld, amount=%d, pp=%p\n",
46877 osGetCurrentProcessId(), fd, iOff, nAmt, pp));
46878
46879#if SQLITE_MAX_MMAP_SIZE>0
46880 if( pFd->mmapSizeMax>0 ){
46881 if( pFd->pMapRegion==0 ){
46882 int rc = winMapfile(pFd, -1);
46883 if( rc!=SQLITE_OK ){
46884 OSTRACE(("FETCH pid=%lu, pFile=%p, rc=%s\n",
46885 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
46886 return rc;
46887 }
46888 }
46889 if( pFd->mmapSize >= iOff+nAmt ){
46890 assert( pFd->pMapRegion!=0 );
46891 *pp = &((u8 *)pFd->pMapRegion)[iOff];
46892 pFd->nFetchOut++;
46893 }
46894 }
46895#endif
46896
46897 OSTRACE(("FETCH pid=%lu, pFile=%p, pp=%p, *pp=%p, rc=SQLITE_OK\n",
46898 osGetCurrentProcessId(), fd, pp, *pp));
46899 return SQLITE_OK;
46900}
46901
46902/*
46903** If the third argument is non-NULL, then this function releases a
46904** reference obtained by an earlier call to winFetch(). The second
46905** argument passed to this function must be the same as the corresponding
46906** argument that was passed to the winFetch() invocation.
46907**
46908** Or, if the third argument is NULL, then this function is being called
46909** to inform the VFS layer that, according to POSIX, any existing mapping
46910** may now be invalid and should be unmapped.
46911*/
46912static int winUnfetch(sqlite3_file *fd, i64 iOff, void *p){
46913#if SQLITE_MAX_MMAP_SIZE>0
46914 winFile *pFd = (winFile*)fd; /* The underlying database file */
46915
46916 /* If p==0 (unmap the entire file) then there must be no outstanding
46917 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
46918 ** then there must be at least one outstanding. */
46919 assert( (p==0)==(pFd->nFetchOut==0) );
46920
46921 /* If p!=0, it must match the iOff value. */
46922 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
46923
46924 OSTRACE(("UNFETCH pid=%lu, pFile=%p, offset=%lld, p=%p\n",
46925 osGetCurrentProcessId(), pFd, iOff, p));
46926
46927 if( p ){
46928 pFd->nFetchOut--;
46929 }else{
46930 /* FIXME: If Windows truly always prevents truncating or deleting a
46931 ** file while a mapping is held, then the following winUnmapfile() call
46932 ** is unnecessary can be omitted - potentially improving
46933 ** performance. */
46934 winUnmapfile(pFd);
46935 }
46936
46937 assert( pFd->nFetchOut>=0 );
46938#endif
46939
46940 OSTRACE(("UNFETCH pid=%lu, pFile=%p, rc=SQLITE_OK\n",
46941 osGetCurrentProcessId(), fd));
46942 return SQLITE_OK;
46943}
46944
46945/*
46946** Here ends the implementation of all sqlite3_file methods.
46947**
46948********************** End sqlite3_file Methods *******************************
46949******************************************************************************/
46950
46951/*
46952** This vector defines all the methods that can operate on an
46953** sqlite3_file for win32.
46954*/
46955static const sqlite3_io_methods winIoMethod = {
46956 3, /* iVersion */
46957 winClose, /* xClose */
46958 winRead, /* xRead */
46959 winWrite, /* xWrite */
46960 winTruncate, /* xTruncate */
46961 winSync, /* xSync */
46962 winFileSize, /* xFileSize */
46963 winLock, /* xLock */
46964 winUnlock, /* xUnlock */
46965 winCheckReservedLock, /* xCheckReservedLock */
46966 winFileControl, /* xFileControl */
46967 winSectorSize, /* xSectorSize */
46968 winDeviceCharacteristics, /* xDeviceCharacteristics */
46969 winShmMap, /* xShmMap */
46970 winShmLock, /* xShmLock */
46971 winShmBarrier, /* xShmBarrier */
46972 winShmUnmap, /* xShmUnmap */
46973 winFetch, /* xFetch */
46974 winUnfetch /* xUnfetch */
46975};
46976
46977/*
46978** This vector defines all the methods that can operate on an
46979** sqlite3_file for win32 without performing any locking.
46980*/
46981static const sqlite3_io_methods winIoNolockMethod = {
46982 3, /* iVersion */
46983 winClose, /* xClose */
46984 winRead, /* xRead */
46985 winWrite, /* xWrite */
46986 winTruncate, /* xTruncate */
46987 winSync, /* xSync */
46988 winFileSize, /* xFileSize */
46989 winNolockLock, /* xLock */
46990 winNolockUnlock, /* xUnlock */
46991 winNolockCheckReservedLock, /* xCheckReservedLock */
46992 winFileControl, /* xFileControl */
46993 winSectorSize, /* xSectorSize */
46994 winDeviceCharacteristics, /* xDeviceCharacteristics */
46995 winShmMap, /* xShmMap */
46996 winShmLock, /* xShmLock */
46997 winShmBarrier, /* xShmBarrier */
46998 winShmUnmap, /* xShmUnmap */
46999 winFetch, /* xFetch */
47000 winUnfetch /* xUnfetch */
47001};
47002
47003static winVfsAppData winAppData = {
47004 &winIoMethod, /* pMethod */
47005 0, /* pAppData */
47006 0 /* bNoLock */
47007};
47008
47009static winVfsAppData winNolockAppData = {
47010 &winIoNolockMethod, /* pMethod */
47011 0, /* pAppData */
47012 1 /* bNoLock */
47013};
47014
47015/****************************************************************************
47016**************************** sqlite3_vfs methods ****************************
47017**
47018** This division contains the implementation of methods on the
47019** sqlite3_vfs object.
47020*/
47021
47022#if defined(__CYGWIN__)
47023/*
47024** Convert a filename from whatever the underlying operating system
47025** supports for filenames into UTF-8. Space to hold the result is
47026** obtained from malloc and must be freed by the calling function.
47027*/
47028static char *winConvertToUtf8Filename(const void *zFilename){
47029 char *zConverted = 0;
47030 if( osIsNT() ){
47031 zConverted = winUnicodeToUtf8(zFilename);
47032 }
47033#ifdef SQLITE_WIN32_HAS_ANSI
47034 else{
47035 zConverted = winMbcsToUtf8(zFilename, osAreFileApisANSI());
47036 }
47037#endif
47038 /* caller will handle out of memory */
47039 return zConverted;
47040}
47041#endif
47042
47043/*
47044** Convert a UTF-8 filename into whatever form the underlying
47045** operating system wants filenames in. Space to hold the result
47046** is obtained from malloc and must be freed by the calling
47047** function.
47048*/
47049static void *winConvertFromUtf8Filename(const char *zFilename){
47050 void *zConverted = 0;
47051 if( osIsNT() ){
47052 zConverted = winUtf8ToUnicode(zFilename);
47053 }
47054#ifdef SQLITE_WIN32_HAS_ANSI
47055 else{
47056 zConverted = winUtf8ToMbcs(zFilename, osAreFileApisANSI());
47057 }
47058#endif
47059 /* caller will handle out of memory */
47060 return zConverted;
47061}
47062
47063/*
47064** This function returns non-zero if the specified UTF-8 string buffer
47065** ends with a directory separator character or one was successfully
47066** added to it.
47067*/
47068static int winMakeEndInDirSep(int nBuf, char *zBuf){
47069 if( zBuf ){
47070 int nLen = sqlite3Strlen30(zBuf);
47071 if( nLen>0 ){
47072 if( winIsDirSep(zBuf[nLen-1]) ){
47073 return 1;
47074 }else if( nLen+1<nBuf ){
47075 zBuf[nLen] = winGetDirSep();
47076 zBuf[nLen+1] = '\0';
47077 return 1;
47078 }
47079 }
47080 }
47081 return 0;
47082}
47083
47084/*
47085** Create a temporary file name and store the resulting pointer into pzBuf.
47086** The pointer returned in pzBuf must be freed via sqlite3_free().
47087*/
47088static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
47089 static char zChars[] =
47090 "abcdefghijklmnopqrstuvwxyz"
47091 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
47092 "0123456789";
47093 size_t i, j;
47094 int nPre = sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX);
47095 int nMax, nBuf, nDir, nLen;
47096 char *zBuf;
47097
47098 /* It's odd to simulate an io-error here, but really this is just
47099 ** using the io-error infrastructure to test that SQLite handles this
47100 ** function failing.
47101 */
47102 SimulateIOError( return SQLITE_IOERR );
47103
47104 /* Allocate a temporary buffer to store the fully qualified file
47105 ** name for the temporary file. If this fails, we cannot continue.
47106 */
47107 nMax = pVfs->mxPathname; nBuf = nMax + 2;
47108 zBuf = sqlite3MallocZero( nBuf );
47109 if( !zBuf ){
47110 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
47111 return SQLITE_IOERR_NOMEM_BKPT;
47112 }
47113
47114 /* Figure out the effective temporary directory. First, check if one
47115 ** has been explicitly set by the application; otherwise, use the one
47116 ** configured by the operating system.
47117 */
47118 nDir = nMax - (nPre + 15);
47119 assert( nDir>0 );
47120 if( sqlite3_temp_directory ){
47121 int nDirLen = sqlite3Strlen30(sqlite3_temp_directory);
47122 if( nDirLen>0 ){
47123 if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
47124 nDirLen++;
47125 }
47126 if( nDirLen>nDir ){
47127 sqlite3_free(zBuf);
47128 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
47129 return winLogError(SQLITE_ERROR, 0, "winGetTempname1", 0);
47130 }
47131 sqlite3_snprintf(nMax, zBuf, "%s", sqlite3_temp_directory);
47132 }
47133 }
47134#if defined(__CYGWIN__)
47135 else{
47136 static const char *azDirs[] = {
47137 0, /* getenv("SQLITE_TMPDIR") */
47138 0, /* getenv("TMPDIR") */
47139 0, /* getenv("TMP") */
47140 0, /* getenv("TEMP") */
47141 0, /* getenv("USERPROFILE") */
47142 "/var/tmp",
47143 "/usr/tmp",
47144 "/tmp",
47145 ".",
47146 0 /* List terminator */
47147 };
47148 unsigned int i;
47149 const char *zDir = 0;
47150
47151 if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR");
47152 if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
47153 if( !azDirs[2] ) azDirs[2] = getenv("TMP");
47154 if( !azDirs[3] ) azDirs[3] = getenv("TEMP");
47155 if( !azDirs[4] ) azDirs[4] = getenv("USERPROFILE");
47156 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
47157 void *zConverted;
47158 if( zDir==0 ) continue;
47159 /* If the path starts with a drive letter followed by the colon
47160 ** character, assume it is already a native Win32 path; otherwise,
47161 ** it must be converted to a native Win32 path via the Cygwin API
47162 ** prior to using it.
47163 */
47164 if( winIsDriveLetterAndColon(zDir) ){
47165 zConverted = winConvertFromUtf8Filename(zDir);
47166 if( !zConverted ){
47167 sqlite3_free(zBuf);
47168 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
47169 return SQLITE_IOERR_NOMEM_BKPT;
47170 }
47171 if( winIsDir(zConverted) ){
47172 sqlite3_snprintf(nMax, zBuf, "%s", zDir);
47173 sqlite3_free(zConverted);
47174 break;
47175 }
47176 sqlite3_free(zConverted);
47177 }else{
47178 zConverted = sqlite3MallocZero( nMax+1 );
47179 if( !zConverted ){
47180 sqlite3_free(zBuf);
47181 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
47182 return SQLITE_IOERR_NOMEM_BKPT;
47183 }
47184 if( cygwin_conv_path(
47185 osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A, zDir,
47186 zConverted, nMax+1)<0 ){
47187 sqlite3_free(zConverted);
47188 sqlite3_free(zBuf);
47189 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_CONVPATH\n"));
47190 return winLogError(SQLITE_IOERR_CONVPATH, (DWORD)errno,
47191 "winGetTempname2", zDir);
47192 }
47193 if( winIsDir(zConverted) ){
47194 /* At this point, we know the candidate directory exists and should
47195 ** be used. However, we may need to convert the string containing
47196 ** its name into UTF-8 (i.e. if it is UTF-16 right now).
47197 */
47198 char *zUtf8 = winConvertToUtf8Filename(zConverted);
47199 if( !zUtf8 ){
47200 sqlite3_free(zConverted);
47201 sqlite3_free(zBuf);
47202 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
47203 return SQLITE_IOERR_NOMEM_BKPT;
47204 }
47205 sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
47206 sqlite3_free(zUtf8);
47207 sqlite3_free(zConverted);
47208 break;
47209 }
47210 sqlite3_free(zConverted);
47211 }
47212 }
47213 }
47214#elif !SQLITE_OS_WINRT && !defined(__CYGWIN__)
47215 else if( osIsNT() ){
47216 char *zMulti;
47217 LPWSTR zWidePath = sqlite3MallocZero( nMax*sizeof(WCHAR) );
47218 if( !zWidePath ){
47219 sqlite3_free(zBuf);
47220 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
47221 return SQLITE_IOERR_NOMEM_BKPT;
47222 }
47223 if( osGetTempPathW(nMax, zWidePath)==0 ){
47224 sqlite3_free(zWidePath);
47225 sqlite3_free(zBuf);
47226 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
47227 return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
47228 "winGetTempname2", 0);
47229 }
47230 zMulti = winUnicodeToUtf8(zWidePath);
47231 if( zMulti ){
47232 sqlite3_snprintf(nMax, zBuf, "%s", zMulti);
47233 sqlite3_free(zMulti);
47234 sqlite3_free(zWidePath);
47235 }else{
47236 sqlite3_free(zWidePath);
47237 sqlite3_free(zBuf);
47238 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
47239 return SQLITE_IOERR_NOMEM_BKPT;
47240 }
47241 }
47242#ifdef SQLITE_WIN32_HAS_ANSI
47243 else{
47244 char *zUtf8;
47245 char *zMbcsPath = sqlite3MallocZero( nMax );
47246 if( !zMbcsPath ){
47247 sqlite3_free(zBuf);
47248 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
47249 return SQLITE_IOERR_NOMEM_BKPT;
47250 }
47251 if( osGetTempPathA(nMax, zMbcsPath)==0 ){
47252 sqlite3_free(zBuf);
47253 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
47254 return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
47255 "winGetTempname3", 0);
47256 }
47257 zUtf8 = winMbcsToUtf8(zMbcsPath, osAreFileApisANSI());
47258 if( zUtf8 ){
47259 sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
47260 sqlite3_free(zUtf8);
47261 }else{
47262 sqlite3_free(zBuf);
47263 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
47264 return SQLITE_IOERR_NOMEM_BKPT;
47265 }
47266 }
47267#endif /* SQLITE_WIN32_HAS_ANSI */
47268#endif /* !SQLITE_OS_WINRT */
47269
47270 /*
47271 ** Check to make sure the temporary directory ends with an appropriate
47272 ** separator. If it does not and there is not enough space left to add
47273 ** one, fail.
47274 */
47275 if( !winMakeEndInDirSep(nDir+1, zBuf) ){
47276 sqlite3_free(zBuf);
47277 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
47278 return winLogError(SQLITE_ERROR, 0, "winGetTempname4", 0);
47279 }
47280
47281 /*
47282 ** Check that the output buffer is large enough for the temporary file
47283 ** name in the following format:
47284 **
47285 ** "<temporary_directory>/etilqs_XXXXXXXXXXXXXXX\0\0"
47286 **
47287 ** If not, return SQLITE_ERROR. The number 17 is used here in order to
47288 ** account for the space used by the 15 character random suffix and the
47289 ** two trailing NUL characters. The final directory separator character
47290 ** has already added if it was not already present.
47291 */
47292 nLen = sqlite3Strlen30(zBuf);
47293 if( (nLen + nPre + 17) > nBuf ){
47294 sqlite3_free(zBuf);
47295 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
47296 return winLogError(SQLITE_ERROR, 0, "winGetTempname5", 0);
47297 }
47298
47299 sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX);
47300
47301 j = sqlite3Strlen30(zBuf);
47302 sqlite3_randomness(15, &zBuf[j]);
47303 for(i=0; i<15; i++, j++){
47304 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
47305 }
47306 zBuf[j] = 0;
47307 zBuf[j+1] = 0;
47308 *pzBuf = zBuf;
47309
47310 OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
47311 return SQLITE_OK;
47312}
47313
47314/*
47315** Return TRUE if the named file is really a directory. Return false if
47316** it is something other than a directory, or if there is any kind of memory
47317** allocation failure.
47318*/
47319static int winIsDir(const void *zConverted){
47320 DWORD attr;
47321 int rc = 0;
47322 DWORD lastErrno;
47323
47324 if( osIsNT() ){
47325 int cnt = 0;
47326 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
47327 memset(&sAttrData, 0, sizeof(sAttrData));
47328 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
47329 GetFileExInfoStandard,
47330 &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
47331 if( !rc ){
47332 return 0; /* Invalid name? */
47333 }
47334 attr = sAttrData.dwFileAttributes;
47335#if SQLITE_OS_WINCE==0
47336 }else{
47337 attr = osGetFileAttributesA((char*)zConverted);
47338#endif
47339 }
47340 return (attr!=INVALID_FILE_ATTRIBUTES) && (attr&FILE_ATTRIBUTE_DIRECTORY);
47341}
47342
47343/* forward reference */
47344static int winAccess(
47345 sqlite3_vfs *pVfs, /* Not used on win32 */
47346 const char *zFilename, /* Name of file to check */
47347 int flags, /* Type of test to make on this file */
47348 int *pResOut /* OUT: Result */
47349);
47350
47351/*
47352** Open a file.
47353*/
47354static int winOpen(
47355 sqlite3_vfs *pVfs, /* Used to get maximum path length and AppData */
47356 const char *zName, /* Name of the file (UTF-8) */
47357 sqlite3_file *id, /* Write the SQLite file handle here */
47358 int flags, /* Open mode flags */
47359 int *pOutFlags /* Status return flags */
47360){
47361 HANDLE h;
47362 DWORD lastErrno = 0;
47363 DWORD dwDesiredAccess;
47364 DWORD dwShareMode;
47365 DWORD dwCreationDisposition;
47366 DWORD dwFlagsAndAttributes = 0;
47367#if SQLITE_OS_WINCE
47368 int isTemp = 0;
47369#endif
47370 winVfsAppData *pAppData;
47371 winFile *pFile = (winFile*)id;
47372 void *zConverted; /* Filename in OS encoding */
47373 const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
47374 int cnt = 0;
47375
47376 /* If argument zPath is a NULL pointer, this function is required to open
47377 ** a temporary file. Use this buffer to store the file name in.
47378 */
47379 char *zTmpname = 0; /* For temporary filename, if necessary. */
47380
47381 int rc = SQLITE_OK; /* Function Return Code */
47382#if !defined(NDEBUG) || SQLITE_OS_WINCE
47383 int eType = flags&0xFFFFFF00; /* Type of file to open */
47384#endif
47385
47386 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
47387 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
47388 int isCreate = (flags & SQLITE_OPEN_CREATE);
47389 int isReadonly = (flags & SQLITE_OPEN_READONLY);
47390 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
47391
47392#ifndef NDEBUG
47393 int isOpenJournal = (isCreate && (
47394 eType==SQLITE_OPEN_SUPER_JOURNAL
47395 || eType==SQLITE_OPEN_MAIN_JOURNAL
47396 || eType==SQLITE_OPEN_WAL
47397 ));
47398#endif
47399
47400 OSTRACE(("OPEN name=%s, pFile=%p, flags=%x, pOutFlags=%p\n",
47401 zUtf8Name, id, flags, pOutFlags));
47402
47403 /* Check the following statements are true:
47404 **
47405 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
47406 ** (b) if CREATE is set, then READWRITE must also be set, and
47407 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
47408 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
47409 */
47410 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
47411 assert(isCreate==0 || isReadWrite);
47412 assert(isExclusive==0 || isCreate);
47413 assert(isDelete==0 || isCreate);
47414
47415 /* The main DB, main journal, WAL file and super-journal are never
47416 ** automatically deleted. Nor are they ever temporary files. */
47417 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
47418 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
47419 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_SUPER_JOURNAL );
47420 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
47421
47422 /* Assert that the upper layer has set one of the "file-type" flags. */
47423 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
47424 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
47425 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_SUPER_JOURNAL
47426 || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
47427 );
47428
47429 assert( pFile!=0 );
47430 memset(pFile, 0, sizeof(winFile));
47431 pFile->h = INVALID_HANDLE_VALUE;
47432
47433#if SQLITE_OS_WINRT
47434 if( !zUtf8Name && !sqlite3_temp_directory ){
47435 sqlite3_log(SQLITE_ERROR,
47436 "sqlite3_temp_directory variable should be set for WinRT");
47437 }
47438#endif
47439
47440 /* If the second argument to this function is NULL, generate a
47441 ** temporary file name to use
47442 */
47443 if( !zUtf8Name ){
47444 assert( isDelete && !isOpenJournal );
47445 rc = winGetTempname(pVfs, &zTmpname);
47446 if( rc!=SQLITE_OK ){
47447 OSTRACE(("OPEN name=%s, rc=%s", zUtf8Name, sqlite3ErrName(rc)));
47448 return rc;
47449 }
47450 zUtf8Name = zTmpname;
47451 }
47452
47453 /* Database filenames are double-zero terminated if they are not
47454 ** URIs with parameters. Hence, they can always be passed into
47455 ** sqlite3_uri_parameter().
47456 */
47457 assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) ||
47458 zUtf8Name[sqlite3Strlen30(zUtf8Name)+1]==0 );
47459
47460 /* Convert the filename to the system encoding. */
47461 zConverted = winConvertFromUtf8Filename(zUtf8Name);
47462 if( zConverted==0 ){
47463 sqlite3_free(zTmpname);
47464 OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name));
47465 return SQLITE_IOERR_NOMEM_BKPT;
47466 }
47467
47468 if( winIsDir(zConverted) ){
47469 sqlite3_free(zConverted);
47470 sqlite3_free(zTmpname);
47471 OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8Name));
47472 return SQLITE_CANTOPEN_ISDIR;
47473 }
47474
47475 if( isReadWrite ){
47476 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
47477 }else{
47478 dwDesiredAccess = GENERIC_READ;
47479 }
47480
47481 /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
47482 ** created. SQLite doesn't use it to indicate "exclusive access"
47483 ** as it is usually understood.
47484 */
47485 if( isExclusive ){
47486 /* Creates a new file, only if it does not already exist. */
47487 /* If the file exists, it fails. */
47488 dwCreationDisposition = CREATE_NEW;
47489 }else if( isCreate ){
47490 /* Open existing file, or create if it doesn't exist */
47491 dwCreationDisposition = OPEN_ALWAYS;
47492 }else{
47493 /* Opens a file, only if it exists. */
47494 dwCreationDisposition = OPEN_EXISTING;
47495 }
47496
47497 if( 0==sqlite3_uri_boolean(zName, "exclusive", 0) ){
47498 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
47499 }else{
47500 dwShareMode = 0;
47501 }
47502
47503 if( isDelete ){
47504#if SQLITE_OS_WINCE
47505 dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN;
47506 isTemp = 1;
47507#else
47508 dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY
47509 | FILE_ATTRIBUTE_HIDDEN
47510 | FILE_FLAG_DELETE_ON_CLOSE;
47511#endif
47512 }else{
47513 dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
47514 }
47515 /* Reports from the internet are that performance is always
47516 ** better if FILE_FLAG_RANDOM_ACCESS is used. Ticket #2699. */
47517#if SQLITE_OS_WINCE
47518 dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
47519#endif
47520
47521 if( osIsNT() ){
47522#if SQLITE_OS_WINRT
47523 CREATEFILE2_EXTENDED_PARAMETERS extendedParameters;
47524 extendedParameters.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
47525 extendedParameters.dwFileAttributes =
47526 dwFlagsAndAttributes & FILE_ATTRIBUTE_MASK;
47527 extendedParameters.dwFileFlags = dwFlagsAndAttributes & FILE_FLAG_MASK;
47528 extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS;
47529 extendedParameters.lpSecurityAttributes = NULL;
47530 extendedParameters.hTemplateFile = NULL;
47531 do{
47532 h = osCreateFile2((LPCWSTR)zConverted,
47533 dwDesiredAccess,
47534 dwShareMode,
47535 dwCreationDisposition,
47536 &extendedParameters);
47537 if( h!=INVALID_HANDLE_VALUE ) break;
47538 if( isReadWrite ){
47539 int rc2, isRO = 0;
47540 sqlite3BeginBenignMalloc();
47541 rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
47542 sqlite3EndBenignMalloc();
47543 if( rc2==SQLITE_OK && isRO ) break;
47544 }
47545 }while( winRetryIoerr(&cnt, &lastErrno) );
47546#else
47547 do{
47548 h = osCreateFileW((LPCWSTR)zConverted,
47549 dwDesiredAccess,
47550 dwShareMode, NULL,
47551 dwCreationDisposition,
47552 dwFlagsAndAttributes,
47553 NULL);
47554 if( h!=INVALID_HANDLE_VALUE ) break;
47555 if( isReadWrite ){
47556 int rc2, isRO = 0;
47557 sqlite3BeginBenignMalloc();
47558 rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
47559 sqlite3EndBenignMalloc();
47560 if( rc2==SQLITE_OK && isRO ) break;
47561 }
47562 }while( winRetryIoerr(&cnt, &lastErrno) );
47563#endif
47564 }
47565#ifdef SQLITE_WIN32_HAS_ANSI
47566 else{
47567 do{
47568 h = osCreateFileA((LPCSTR)zConverted,
47569 dwDesiredAccess,
47570 dwShareMode, NULL,
47571 dwCreationDisposition,
47572 dwFlagsAndAttributes,
47573 NULL);
47574 if( h!=INVALID_HANDLE_VALUE ) break;
47575 if( isReadWrite ){
47576 int rc2, isRO = 0;
47577 sqlite3BeginBenignMalloc();
47578 rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
47579 sqlite3EndBenignMalloc();
47580 if( rc2==SQLITE_OK && isRO ) break;
47581 }
47582 }while( winRetryIoerr(&cnt, &lastErrno) );
47583 }
47584#endif
47585 winLogIoerr(cnt, __LINE__);
47586
47587 OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name,
47588 dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
47589
47590 if( h==INVALID_HANDLE_VALUE ){
47591 sqlite3_free(zConverted);
47592 sqlite3_free(zTmpname);
47593 if( isReadWrite && !isExclusive ){
47594 return winOpen(pVfs, zName, id,
47595 ((flags|SQLITE_OPEN_READONLY) &
47596 ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
47597 pOutFlags);
47598 }else{
47599 pFile->lastErrno = lastErrno;
47600 winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
47601 return SQLITE_CANTOPEN_BKPT;
47602 }
47603 }
47604
47605 if( pOutFlags ){
47606 if( isReadWrite ){
47607 *pOutFlags = SQLITE_OPEN_READWRITE;
47608 }else{
47609 *pOutFlags = SQLITE_OPEN_READONLY;
47610 }
47611 }
47612
47613 OSTRACE(("OPEN file=%p, name=%s, access=%lx, pOutFlags=%p, *pOutFlags=%d, "
47614 "rc=%s\n", h, zUtf8Name, dwDesiredAccess, pOutFlags, pOutFlags ?
47615 *pOutFlags : 0, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
47616
47617 pAppData = (winVfsAppData*)pVfs->pAppData;
47618
47619#if SQLITE_OS_WINCE
47620 {
47621 if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
47622 && ((pAppData==NULL) || !pAppData->bNoLock)
47623 && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK
47624 ){
47625 osCloseHandle(h);
47626 sqlite3_free(zConverted);
47627 sqlite3_free(zTmpname);
47628 OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
47629 return rc;
47630 }
47631 }
47632 if( isTemp ){
47633 pFile->zDeleteOnClose = zConverted;
47634 }else
47635#endif
47636 {
47637 sqlite3_free(zConverted);
47638 }
47639
47640 sqlite3_free(zTmpname);
47641 id->pMethods = pAppData ? pAppData->pMethod : &winIoMethod;
47642 pFile->pVfs = pVfs;
47643 pFile->h = h;
47644 if( isReadonly ){
47645 pFile->ctrlFlags |= WINFILE_RDONLY;
47646 }
47647 if( (flags & SQLITE_OPEN_MAIN_DB)
47648 && sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE)
47649 ){
47650 pFile->ctrlFlags |= WINFILE_PSOW;
47651 }
47652 pFile->lastErrno = NO_ERROR;
47653 pFile->zPath = zName;
47654#if SQLITE_MAX_MMAP_SIZE>0
47655 pFile->hMap = NULL;
47656 pFile->pMapRegion = 0;
47657 pFile->mmapSize = 0;
47658 pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap;
47659#endif
47660
47661 OpenCounter(+1);
47662 return rc;
47663}
47664
47665/*
47666** Delete the named file.
47667**
47668** Note that Windows does not allow a file to be deleted if some other
47669** process has it open. Sometimes a virus scanner or indexing program
47670** will open a journal file shortly after it is created in order to do
47671** whatever it does. While this other process is holding the
47672** file open, we will be unable to delete it. To work around this
47673** problem, we delay 100 milliseconds and try to delete again. Up
47674** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
47675** up and returning an error.
47676*/
47677static int winDelete(
47678 sqlite3_vfs *pVfs, /* Not used on win32 */
47679 const char *zFilename, /* Name of file to delete */
47680 int syncDir /* Not used on win32 */
47681){
47682 int cnt = 0;
47683 int rc;
47684 DWORD attr;
47685 DWORD lastErrno = 0;
47686 void *zConverted;
47687 UNUSED_PARAMETER(pVfs);
47688 UNUSED_PARAMETER(syncDir);
47689
47690 SimulateIOError(return SQLITE_IOERR_DELETE);
47691 OSTRACE(("DELETE name=%s, syncDir=%d\n", zFilename, syncDir));
47692
47693 zConverted = winConvertFromUtf8Filename(zFilename);
47694 if( zConverted==0 ){
47695 OSTRACE(("DELETE name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
47696 return SQLITE_IOERR_NOMEM_BKPT;
47697 }
47698 if( osIsNT() ){
47699 do {
47700#if SQLITE_OS_WINRT
47701 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
47702 memset(&sAttrData, 0, sizeof(sAttrData));
47703 if ( osGetFileAttributesExW(zConverted, GetFileExInfoStandard,
47704 &sAttrData) ){
47705 attr = sAttrData.dwFileAttributes;
47706 }else{
47707 lastErrno = osGetLastError();
47708 if( lastErrno==ERROR_FILE_NOT_FOUND
47709 || lastErrno==ERROR_PATH_NOT_FOUND ){
47710 rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
47711 }else{
47712 rc = SQLITE_ERROR;
47713 }
47714 break;
47715 }
47716#else
47717 attr = osGetFileAttributesW(zConverted);
47718#endif
47719 if ( attr==INVALID_FILE_ATTRIBUTES ){
47720 lastErrno = osGetLastError();
47721 if( lastErrno==ERROR_FILE_NOT_FOUND
47722 || lastErrno==ERROR_PATH_NOT_FOUND ){
47723 rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
47724 }else{
47725 rc = SQLITE_ERROR;
47726 }
47727 break;
47728 }
47729 if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
47730 rc = SQLITE_ERROR; /* Files only. */
47731 break;
47732 }
47733 if ( osDeleteFileW(zConverted) ){
47734 rc = SQLITE_OK; /* Deleted OK. */
47735 break;
47736 }
47737 if ( !winRetryIoerr(&cnt, &lastErrno) ){
47738 rc = SQLITE_ERROR; /* No more retries. */
47739 break;
47740 }
47741 } while(1);
47742 }
47743#ifdef SQLITE_WIN32_HAS_ANSI
47744 else{
47745 do {
47746 attr = osGetFileAttributesA(zConverted);
47747 if ( attr==INVALID_FILE_ATTRIBUTES ){
47748 lastErrno = osGetLastError();
47749 if( lastErrno==ERROR_FILE_NOT_FOUND
47750 || lastErrno==ERROR_PATH_NOT_FOUND ){
47751 rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
47752 }else{
47753 rc = SQLITE_ERROR;
47754 }
47755 break;
47756 }
47757 if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
47758 rc = SQLITE_ERROR; /* Files only. */
47759 break;
47760 }
47761 if ( osDeleteFileA(zConverted) ){
47762 rc = SQLITE_OK; /* Deleted OK. */
47763 break;
47764 }
47765 if ( !winRetryIoerr(&cnt, &lastErrno) ){
47766 rc = SQLITE_ERROR; /* No more retries. */
47767 break;
47768 }
47769 } while(1);
47770 }
47771#endif
47772 if( rc && rc!=SQLITE_IOERR_DELETE_NOENT ){
47773 rc = winLogError(SQLITE_IOERR_DELETE, lastErrno, "winDelete", zFilename);
47774 }else{
47775 winLogIoerr(cnt, __LINE__);
47776 }
47777 sqlite3_free(zConverted);
47778 OSTRACE(("DELETE name=%s, rc=%s\n", zFilename, sqlite3ErrName(rc)));
47779 return rc;
47780}
47781
47782/*
47783** Check the existence and status of a file.
47784*/
47785static int winAccess(
47786 sqlite3_vfs *pVfs, /* Not used on win32 */
47787 const char *zFilename, /* Name of file to check */
47788 int flags, /* Type of test to make on this file */
47789 int *pResOut /* OUT: Result */
47790){
47791 DWORD attr;
47792 int rc = 0;
47793 DWORD lastErrno = 0;
47794 void *zConverted;
47795 UNUSED_PARAMETER(pVfs);
47796
47797 SimulateIOError( return SQLITE_IOERR_ACCESS; );
47798 OSTRACE(("ACCESS name=%s, flags=%x, pResOut=%p\n",
47799 zFilename, flags, pResOut));
47800
47801 zConverted = winConvertFromUtf8Filename(zFilename);
47802 if( zConverted==0 ){
47803 OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
47804 return SQLITE_IOERR_NOMEM_BKPT;
47805 }
47806 if( osIsNT() ){
47807 int cnt = 0;
47808 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
47809 memset(&sAttrData, 0, sizeof(sAttrData));
47810 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
47811 GetFileExInfoStandard,
47812 &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
47813 if( rc ){
47814 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
47815 ** as if it does not exist.
47816 */
47817 if( flags==SQLITE_ACCESS_EXISTS
47818 && sAttrData.nFileSizeHigh==0
47819 && sAttrData.nFileSizeLow==0 ){
47820 attr = INVALID_FILE_ATTRIBUTES;
47821 }else{
47822 attr = sAttrData.dwFileAttributes;
47823 }
47824 }else{
47825 winLogIoerr(cnt, __LINE__);
47826 if( lastErrno!=ERROR_FILE_NOT_FOUND && lastErrno!=ERROR_PATH_NOT_FOUND ){
47827 sqlite3_free(zConverted);
47828 return winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess",
47829 zFilename);
47830 }else{
47831 attr = INVALID_FILE_ATTRIBUTES;
47832 }
47833 }
47834 }
47835#ifdef SQLITE_WIN32_HAS_ANSI
47836 else{
47837 attr = osGetFileAttributesA((char*)zConverted);
47838 }
47839#endif
47840 sqlite3_free(zConverted);
47841 switch( flags ){
47842 case SQLITE_ACCESS_READ:
47843 case SQLITE_ACCESS_EXISTS:
47844 rc = attr!=INVALID_FILE_ATTRIBUTES;
47845 break;
47846 case SQLITE_ACCESS_READWRITE:
47847 rc = attr!=INVALID_FILE_ATTRIBUTES &&
47848 (attr & FILE_ATTRIBUTE_READONLY)==0;
47849 break;
47850 default:
47851 assert(!"Invalid flags argument");
47852 }
47853 *pResOut = rc;
47854 OSTRACE(("ACCESS name=%s, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
47855 zFilename, pResOut, *pResOut));
47856 return SQLITE_OK;
47857}
47858
47859/*
47860** Returns non-zero if the specified path name starts with the "long path"
47861** prefix.
47862*/
47863static BOOL winIsLongPathPrefix(
47864 const char *zPathname
47865){
47866 return ( zPathname[0]=='\\' && zPathname[1]=='\\'
47867 && zPathname[2]=='?' && zPathname[3]=='\\' );
47868}
47869
47870/*
47871** Returns non-zero if the specified path name starts with a drive letter
47872** followed by a colon character.
47873*/
47874static BOOL winIsDriveLetterAndColon(
47875 const char *zPathname
47876){
47877 return ( sqlite3Isalpha(zPathname[0]) && zPathname[1]==':' );
47878}
47879
47880/*
47881** Returns non-zero if the specified path name should be used verbatim. If
47882** non-zero is returned from this function, the calling function must simply
47883** use the provided path name verbatim -OR- resolve it into a full path name
47884** using the GetFullPathName Win32 API function (if available).
47885*/
47886static BOOL winIsVerbatimPathname(
47887 const char *zPathname
47888){
47889 /*
47890 ** If the path name starts with a forward slash or a backslash, it is either
47891 ** a legal UNC name, a volume relative path, or an absolute path name in the
47892 ** "Unix" format on Windows. There is no easy way to differentiate between
47893 ** the final two cases; therefore, we return the safer return value of TRUE
47894 ** so that callers of this function will simply use it verbatim.
47895 */
47896 if ( winIsDirSep(zPathname[0]) ){
47897 return TRUE;
47898 }
47899
47900 /*
47901 ** If the path name starts with a letter and a colon it is either a volume
47902 ** relative path or an absolute path. Callers of this function must not
47903 ** attempt to treat it as a relative path name (i.e. they should simply use
47904 ** it verbatim).
47905 */
47906 if ( winIsDriveLetterAndColon(zPathname) ){
47907 return TRUE;
47908 }
47909
47910 /*
47911 ** If we get to this point, the path name should almost certainly be a purely
47912 ** relative one (i.e. not a UNC name, not absolute, and not volume relative).
47913 */
47914 return FALSE;
47915}
47916
47917/*
47918** Turn a relative pathname into a full pathname. Write the full
47919** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname
47920** bytes in size.
47921*/
47922static int winFullPathname(
47923 sqlite3_vfs *pVfs, /* Pointer to vfs object */
47924 const char *zRelative, /* Possibly relative input path */
47925 int nFull, /* Size of output buffer in bytes */
47926 char *zFull /* Output buffer */
47927){
47928#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
47929 DWORD nByte;
47930 void *zConverted;
47931 char *zOut;
47932#endif
47933
47934 /* If this path name begins with "/X:" or "\\?\", where "X" is any
47935 ** alphabetic character, discard the initial "/" from the pathname.
47936 */
47937 if( zRelative[0]=='/' && (winIsDriveLetterAndColon(zRelative+1)
47938 || winIsLongPathPrefix(zRelative+1)) ){
47939 zRelative++;
47940 }
47941
47942#if defined(__CYGWIN__)
47943 SimulateIOError( return SQLITE_ERROR );
47944 UNUSED_PARAMETER(nFull);
47945 assert( nFull>=pVfs->mxPathname );
47946 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
47947 /*
47948 ** NOTE: We are dealing with a relative path name and the data
47949 ** directory has been set. Therefore, use it as the basis
47950 ** for converting the relative path name to an absolute
47951 ** one by prepending the data directory and a slash.
47952 */
47953 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
47954 if( !zOut ){
47955 return SQLITE_IOERR_NOMEM_BKPT;
47956 }
47957 if( cygwin_conv_path(
47958 (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A) |
47959 CCP_RELATIVE, zRelative, zOut, pVfs->mxPathname+1)<0 ){
47960 sqlite3_free(zOut);
47961 return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
47962 "winFullPathname1", zRelative);
47963 }else{
47964 char *zUtf8 = winConvertToUtf8Filename(zOut);
47965 if( !zUtf8 ){
47966 sqlite3_free(zOut);
47967 return SQLITE_IOERR_NOMEM_BKPT;
47968 }
47969 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
47970 sqlite3_data_directory, winGetDirSep(), zUtf8);
47971 sqlite3_free(zUtf8);
47972 sqlite3_free(zOut);
47973 }
47974 }else{
47975 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
47976 if( !zOut ){
47977 return SQLITE_IOERR_NOMEM_BKPT;
47978 }
47979 if( cygwin_conv_path(
47980 (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A),
47981 zRelative, zOut, pVfs->mxPathname+1)<0 ){
47982 sqlite3_free(zOut);
47983 return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
47984 "winFullPathname2", zRelative);
47985 }else{
47986 char *zUtf8 = winConvertToUtf8Filename(zOut);
47987 if( !zUtf8 ){
47988 sqlite3_free(zOut);
47989 return SQLITE_IOERR_NOMEM_BKPT;
47990 }
47991 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8);
47992 sqlite3_free(zUtf8);
47993 sqlite3_free(zOut);
47994 }
47995 }
47996 return SQLITE_OK;
47997#endif
47998
47999#if (SQLITE_OS_WINCE || SQLITE_OS_WINRT) && !defined(__CYGWIN__)
48000 SimulateIOError( return SQLITE_ERROR );
48001 /* WinCE has no concept of a relative pathname, or so I am told. */
48002 /* WinRT has no way to convert a relative path to an absolute one. */
48003 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
48004 /*
48005 ** NOTE: We are dealing with a relative path name and the data
48006 ** directory has been set. Therefore, use it as the basis
48007 ** for converting the relative path name to an absolute
48008 ** one by prepending the data directory and a backslash.
48009 */
48010 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
48011 sqlite3_data_directory, winGetDirSep(), zRelative);
48012 }else{
48013 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
48014 }
48015 return SQLITE_OK;
48016#endif
48017
48018#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
48019 /* It's odd to simulate an io-error here, but really this is just
48020 ** using the io-error infrastructure to test that SQLite handles this
48021 ** function failing. This function could fail if, for example, the
48022 ** current working directory has been unlinked.
48023 */
48024 SimulateIOError( return SQLITE_ERROR );
48025 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
48026 /*
48027 ** NOTE: We are dealing with a relative path name and the data
48028 ** directory has been set. Therefore, use it as the basis
48029 ** for converting the relative path name to an absolute
48030 ** one by prepending the data directory and a backslash.
48031 */
48032 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
48033 sqlite3_data_directory, winGetDirSep(), zRelative);
48034 return SQLITE_OK;
48035 }
48036 zConverted = winConvertFromUtf8Filename(zRelative);
48037 if( zConverted==0 ){
48038 return SQLITE_IOERR_NOMEM_BKPT;
48039 }
48040 if( osIsNT() ){
48041 LPWSTR zTemp;
48042 nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0);
48043 if( nByte==0 ){
48044 sqlite3_free(zConverted);
48045 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
48046 "winFullPathname1", zRelative);
48047 }
48048 nByte += 3;
48049 zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
48050 if( zTemp==0 ){
48051 sqlite3_free(zConverted);
48052 return SQLITE_IOERR_NOMEM_BKPT;
48053 }
48054 nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
48055 if( nByte==0 ){
48056 sqlite3_free(zConverted);
48057 sqlite3_free(zTemp);
48058 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
48059 "winFullPathname2", zRelative);
48060 }
48061 sqlite3_free(zConverted);
48062 zOut = winUnicodeToUtf8(zTemp);
48063 sqlite3_free(zTemp);
48064 }
48065#ifdef SQLITE_WIN32_HAS_ANSI
48066 else{
48067 char *zTemp;
48068 nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0);
48069 if( nByte==0 ){
48070 sqlite3_free(zConverted);
48071 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
48072 "winFullPathname3", zRelative);
48073 }
48074 nByte += 3;
48075 zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
48076 if( zTemp==0 ){
48077 sqlite3_free(zConverted);
48078 return SQLITE_IOERR_NOMEM_BKPT;
48079 }
48080 nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
48081 if( nByte==0 ){
48082 sqlite3_free(zConverted);
48083 sqlite3_free(zTemp);
48084 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
48085 "winFullPathname4", zRelative);
48086 }
48087 sqlite3_free(zConverted);
48088 zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
48089 sqlite3_free(zTemp);
48090 }
48091#endif
48092 if( zOut ){
48093 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
48094 sqlite3_free(zOut);
48095 return SQLITE_OK;
48096 }else{
48097 return SQLITE_IOERR_NOMEM_BKPT;
48098 }
48099#endif
48100}
48101
48102#ifndef SQLITE_OMIT_LOAD_EXTENSION
48103/*
48104** Interfaces for opening a shared library, finding entry points
48105** within the shared library, and closing the shared library.
48106*/
48107static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
48108 HANDLE h;
48109#if defined(__CYGWIN__)
48110 int nFull = pVfs->mxPathname+1;
48111 char *zFull = sqlite3MallocZero( nFull );
48112 void *zConverted = 0;
48113 if( zFull==0 ){
48114 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
48115 return 0;
48116 }
48117 if( winFullPathname(pVfs, zFilename, nFull, zFull)!=SQLITE_OK ){
48118 sqlite3_free(zFull);
48119 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
48120 return 0;
48121 }
48122 zConverted = winConvertFromUtf8Filename(zFull);
48123 sqlite3_free(zFull);
48124#else
48125 void *zConverted = winConvertFromUtf8Filename(zFilename);
48126 UNUSED_PARAMETER(pVfs);
48127#endif
48128 if( zConverted==0 ){
48129 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
48130 return 0;
48131 }
48132 if( osIsNT() ){
48133#if SQLITE_OS_WINRT
48134 h = osLoadPackagedLibrary((LPCWSTR)zConverted, 0);
48135#else
48136 h = osLoadLibraryW((LPCWSTR)zConverted);
48137#endif
48138 }
48139#ifdef SQLITE_WIN32_HAS_ANSI
48140 else{
48141 h = osLoadLibraryA((char*)zConverted);
48142 }
48143#endif
48144 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)h));
48145 sqlite3_free(zConverted);
48146 return (void*)h;
48147}
48148static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
48149 UNUSED_PARAMETER(pVfs);
48150 winGetLastErrorMsg(osGetLastError(), nBuf, zBufOut);
48151}
48152static void (*winDlSym(sqlite3_vfs *pVfs,void *pH,const char *zSym))(void){
48153 FARPROC proc;
48154 UNUSED_PARAMETER(pVfs);
48155 proc = osGetProcAddressA((HANDLE)pH, zSym);
48156 OSTRACE(("DLSYM handle=%p, symbol=%s, address=%p\n",
48157 (void*)pH, zSym, (void*)proc));
48158 return (void(*)(void))proc;
48159}
48160static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
48161 UNUSED_PARAMETER(pVfs);
48162 osFreeLibrary((HANDLE)pHandle);
48163 OSTRACE(("DLCLOSE handle=%p\n", (void*)pHandle));
48164}
48165#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
48166 #define winDlOpen 0
48167 #define winDlError 0
48168 #define winDlSym 0
48169 #define winDlClose 0
48170#endif
48171
48172/* State information for the randomness gatherer. */
48173typedef struct EntropyGatherer EntropyGatherer;
48174struct EntropyGatherer {
48175 unsigned char *a; /* Gather entropy into this buffer */
48176 int na; /* Size of a[] in bytes */
48177 int i; /* XOR next input into a[i] */
48178 int nXor; /* Number of XOR operations done */
48179};
48180
48181#if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS)
48182/* Mix sz bytes of entropy into p. */
48183static void xorMemory(EntropyGatherer *p, unsigned char *x, int sz){
48184 int j, k;
48185 for(j=0, k=p->i; j<sz; j++){
48186 p->a[k++] ^= x[j];
48187 if( k>=p->na ) k = 0;
48188 }
48189 p->i = k;
48190 p->nXor += sz;
48191}
48192#endif /* !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) */
48193
48194/*
48195** Write up to nBuf bytes of randomness into zBuf.
48196*/
48197static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
48198#if defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS)
48199 UNUSED_PARAMETER(pVfs);
48200 memset(zBuf, 0, nBuf);
48201 return nBuf;
48202#else
48203 EntropyGatherer e;
48204 UNUSED_PARAMETER(pVfs);
48205 memset(zBuf, 0, nBuf);
48206 e.a = (unsigned char*)zBuf;
48207 e.na = nBuf;
48208 e.nXor = 0;
48209 e.i = 0;
48210 {
48211 SYSTEMTIME x;
48212 osGetSystemTime(&x);
48213 xorMemory(&e, (unsigned char*)&x, sizeof(SYSTEMTIME));
48214 }
48215 {
48216 DWORD pid = osGetCurrentProcessId();
48217 xorMemory(&e, (unsigned char*)&pid, sizeof(DWORD));
48218 }
48219#if SQLITE_OS_WINRT
48220 {
48221 ULONGLONG cnt = osGetTickCount64();
48222 xorMemory(&e, (unsigned char*)&cnt, sizeof(ULONGLONG));
48223 }
48224#else
48225 {
48226 DWORD cnt = osGetTickCount();
48227 xorMemory(&e, (unsigned char*)&cnt, sizeof(DWORD));
48228 }
48229#endif /* SQLITE_OS_WINRT */
48230 {
48231 LARGE_INTEGER i;
48232 osQueryPerformanceCounter(&i);
48233 xorMemory(&e, (unsigned char*)&i, sizeof(LARGE_INTEGER));
48234 }
48235#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
48236 {
48237 UUID id;
48238 memset(&id, 0, sizeof(UUID));
48239 osUuidCreate(&id);
48240 xorMemory(&e, (unsigned char*)&id, sizeof(UUID));
48241 memset(&id, 0, sizeof(UUID));
48242 osUuidCreateSequential(&id);
48243 xorMemory(&e, (unsigned char*)&id, sizeof(UUID));
48244 }
48245#endif /* !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID */
48246 return e.nXor>nBuf ? nBuf : e.nXor;
48247#endif /* defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS) */
48248}
48249
48250
48251/*
48252** Sleep for a little while. Return the amount of time slept.
48253*/
48254static int winSleep(sqlite3_vfs *pVfs, int microsec){
48255 sqlite3_win32_sleep((microsec+999)/1000);
48256 UNUSED_PARAMETER(pVfs);
48257 return ((microsec+999)/1000)*1000;
48258}
48259
48260/*
48261** The following variable, if set to a non-zero value, is interpreted as
48262** the number of seconds since 1970 and is used to set the result of
48263** sqlite3OsCurrentTime() during testing.
48264*/
48265#ifdef SQLITE_TEST
48266SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
48267#endif
48268
48269/*
48270** Find the current time (in Universal Coordinated Time). Write into *piNow
48271** the current time and date as a Julian Day number times 86_400_000. In
48272** other words, write into *piNow the number of milliseconds since the Julian
48273** epoch of noon in Greenwich on November 24, 4714 B.C according to the
48274** proleptic Gregorian calendar.
48275**
48276** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
48277** cannot be found.
48278*/
48279static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
48280 /* FILETIME structure is a 64-bit value representing the number of
48281 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
48282 */
48283 FILETIME ft;
48284 static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
48285#ifdef SQLITE_TEST
48286 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
48287#endif
48288 /* 2^32 - to avoid use of LL and warnings in gcc */
48289 static const sqlite3_int64 max32BitValue =
48290 (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 +
48291 (sqlite3_int64)294967296;
48292
48293#if SQLITE_OS_WINCE
48294 SYSTEMTIME time;
48295 osGetSystemTime(&time);
48296 /* if SystemTimeToFileTime() fails, it returns zero. */
48297 if (!osSystemTimeToFileTime(&time,&ft)){
48298 return SQLITE_ERROR;
48299 }
48300#else
48301 osGetSystemTimeAsFileTime( &ft );
48302#endif
48303
48304 *piNow = winFiletimeEpoch +
48305 ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
48306 (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
48307
48308#ifdef SQLITE_TEST
48309 if( sqlite3_current_time ){
48310 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
48311 }
48312#endif
48313 UNUSED_PARAMETER(pVfs);
48314 return SQLITE_OK;
48315}
48316
48317/*
48318** Find the current time (in Universal Coordinated Time). Write the
48319** current time and date as a Julian Day number into *prNow and
48320** return 0. Return 1 if the time and date cannot be found.
48321*/
48322static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
48323 int rc;
48324 sqlite3_int64 i;
48325 rc = winCurrentTimeInt64(pVfs, &i);
48326 if( !rc ){
48327 *prNow = i/86400000.0;
48328 }
48329 return rc;
48330}
48331
48332/*
48333** The idea is that this function works like a combination of
48334** GetLastError() and FormatMessage() on Windows (or errno and
48335** strerror_r() on Unix). After an error is returned by an OS
48336** function, SQLite calls this function with zBuf pointing to
48337** a buffer of nBuf bytes. The OS layer should populate the
48338** buffer with a nul-terminated UTF-8 encoded error message
48339** describing the last IO error to have occurred within the calling
48340** thread.
48341**
48342** If the error message is too large for the supplied buffer,
48343** it should be truncated. The return value of xGetLastError
48344** is zero if the error message fits in the buffer, or non-zero
48345** otherwise (if the message was truncated). If non-zero is returned,
48346** then it is not necessary to include the nul-terminator character
48347** in the output buffer.
48348**
48349** Not supplying an error message will have no adverse effect
48350** on SQLite. It is fine to have an implementation that never
48351** returns an error message:
48352**
48353** int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
48354** assert(zBuf[0]=='\0');
48355** return 0;
48356** }
48357**
48358** However if an error message is supplied, it will be incorporated
48359** by sqlite into the error message available to the user using
48360** sqlite3_errmsg(), possibly making IO errors easier to debug.
48361*/
48362static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
48363 DWORD e = osGetLastError();
48364 UNUSED_PARAMETER(pVfs);
48365 if( nBuf>0 ) winGetLastErrorMsg(e, nBuf, zBuf);
48366 return e;
48367}
48368
48369/*
48370** Initialize and deinitialize the operating system interface.
48371*/
48372SQLITE_API int sqlite3_os_init(void){
48373 static sqlite3_vfs winVfs = {
48374 3, /* iVersion */
48375 sizeof(winFile), /* szOsFile */
48376 SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
48377 0, /* pNext */
48378 "win32", /* zName */
48379 &winAppData, /* pAppData */
48380 winOpen, /* xOpen */
48381 winDelete, /* xDelete */
48382 winAccess, /* xAccess */
48383 winFullPathname, /* xFullPathname */
48384 winDlOpen, /* xDlOpen */
48385 winDlError, /* xDlError */
48386 winDlSym, /* xDlSym */
48387 winDlClose, /* xDlClose */
48388 winRandomness, /* xRandomness */
48389 winSleep, /* xSleep */
48390 winCurrentTime, /* xCurrentTime */
48391 winGetLastError, /* xGetLastError */
48392 winCurrentTimeInt64, /* xCurrentTimeInt64 */
48393 winSetSystemCall, /* xSetSystemCall */
48394 winGetSystemCall, /* xGetSystemCall */
48395 winNextSystemCall, /* xNextSystemCall */
48396 };
48397#if defined(SQLITE_WIN32_HAS_WIDE)
48398 static sqlite3_vfs winLongPathVfs = {
48399 3, /* iVersion */
48400 sizeof(winFile), /* szOsFile */
48401 SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
48402 0, /* pNext */
48403 "win32-longpath", /* zName */
48404 &winAppData, /* pAppData */
48405 winOpen, /* xOpen */
48406 winDelete, /* xDelete */
48407 winAccess, /* xAccess */
48408 winFullPathname, /* xFullPathname */
48409 winDlOpen, /* xDlOpen */
48410 winDlError, /* xDlError */
48411 winDlSym, /* xDlSym */
48412 winDlClose, /* xDlClose */
48413 winRandomness, /* xRandomness */
48414 winSleep, /* xSleep */
48415 winCurrentTime, /* xCurrentTime */
48416 winGetLastError, /* xGetLastError */
48417 winCurrentTimeInt64, /* xCurrentTimeInt64 */
48418 winSetSystemCall, /* xSetSystemCall */
48419 winGetSystemCall, /* xGetSystemCall */
48420 winNextSystemCall, /* xNextSystemCall */
48421 };
48422#endif
48423 static sqlite3_vfs winNolockVfs = {
48424 3, /* iVersion */
48425 sizeof(winFile), /* szOsFile */
48426 SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
48427 0, /* pNext */
48428 "win32-none", /* zName */
48429 &winNolockAppData, /* pAppData */
48430 winOpen, /* xOpen */
48431 winDelete, /* xDelete */
48432 winAccess, /* xAccess */
48433 winFullPathname, /* xFullPathname */
48434 winDlOpen, /* xDlOpen */
48435 winDlError, /* xDlError */
48436 winDlSym, /* xDlSym */
48437 winDlClose, /* xDlClose */
48438 winRandomness, /* xRandomness */
48439 winSleep, /* xSleep */
48440 winCurrentTime, /* xCurrentTime */
48441 winGetLastError, /* xGetLastError */
48442 winCurrentTimeInt64, /* xCurrentTimeInt64 */
48443 winSetSystemCall, /* xSetSystemCall */
48444 winGetSystemCall, /* xGetSystemCall */
48445 winNextSystemCall, /* xNextSystemCall */
48446 };
48447#if defined(SQLITE_WIN32_HAS_WIDE)
48448 static sqlite3_vfs winLongPathNolockVfs = {
48449 3, /* iVersion */
48450 sizeof(winFile), /* szOsFile */
48451 SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
48452 0, /* pNext */
48453 "win32-longpath-none", /* zName */
48454 &winNolockAppData, /* pAppData */
48455 winOpen, /* xOpen */
48456 winDelete, /* xDelete */
48457 winAccess, /* xAccess */
48458 winFullPathname, /* xFullPathname */
48459 winDlOpen, /* xDlOpen */
48460 winDlError, /* xDlError */
48461 winDlSym, /* xDlSym */
48462 winDlClose, /* xDlClose */
48463 winRandomness, /* xRandomness */
48464 winSleep, /* xSleep */
48465 winCurrentTime, /* xCurrentTime */
48466 winGetLastError, /* xGetLastError */
48467 winCurrentTimeInt64, /* xCurrentTimeInt64 */
48468 winSetSystemCall, /* xSetSystemCall */
48469 winGetSystemCall, /* xGetSystemCall */
48470 winNextSystemCall, /* xNextSystemCall */
48471 };
48472#endif
48473
48474 /* Double-check that the aSyscall[] array has been constructed
48475 ** correctly. See ticket [bb3a86e890c8e96ab] */
48476 assert( ArraySize(aSyscall)==80 );
48477
48478 /* get memory map allocation granularity */
48479 memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
48480#if SQLITE_OS_WINRT
48481 osGetNativeSystemInfo(&winSysInfo);
48482#else
48483 osGetSystemInfo(&winSysInfo);
48484#endif
48485 assert( winSysInfo.dwAllocationGranularity>0 );
48486 assert( winSysInfo.dwPageSize>0 );
48487
48488 sqlite3_vfs_register(&winVfs, 1);
48489
48490#if defined(SQLITE_WIN32_HAS_WIDE)
48491 sqlite3_vfs_register(&winLongPathVfs, 0);
48492#endif
48493
48494 sqlite3_vfs_register(&winNolockVfs, 0);
48495
48496#if defined(SQLITE_WIN32_HAS_WIDE)
48497 sqlite3_vfs_register(&winLongPathNolockVfs, 0);
48498#endif
48499
48500#ifndef SQLITE_OMIT_WAL
48501 winBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
48502#endif
48503
48504 return SQLITE_OK;
48505}
48506
48507SQLITE_API int sqlite3_os_end(void){
48508#if SQLITE_OS_WINRT
48509 if( sleepObj!=NULL ){
48510 osCloseHandle(sleepObj);
48511 sleepObj = NULL;
48512 }
48513#endif
48514
48515#ifndef SQLITE_OMIT_WAL
48516 winBigLock = 0;
48517#endif
48518
48519 return SQLITE_OK;
48520}
48521
48522#endif /* SQLITE_OS_WIN */
48523
48524/************** End of os_win.c **********************************************/
48525/************** Begin file memdb.c *******************************************/
48526/*
48527** 2016-09-07
48528**
48529** The author disclaims copyright to this source code. In place of
48530** a legal notice, here is a blessing:
48531**
48532** May you do good and not evil.
48533** May you find forgiveness for yourself and forgive others.
48534** May you share freely, never taking more than you give.
48535**
48536******************************************************************************
48537**
48538** This file implements an in-memory VFS. A database is held as a contiguous
48539** block of memory.
48540**
48541** This file also implements interface sqlite3_serialize() and
48542** sqlite3_deserialize().
48543*/
48544/* #include "sqliteInt.h" */
48545#ifndef SQLITE_OMIT_DESERIALIZE
48546
48547/*
48548** Forward declaration of objects used by this utility
48549*/
48550typedef struct sqlite3_vfs MemVfs;
48551typedef struct MemFile MemFile;
48552typedef struct MemStore MemStore;
48553
48554/* Access to a lower-level VFS that (might) implement dynamic loading,
48555** access to randomness, etc.
48556*/
48557#define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
48558
48559/* Storage for a memdb file.
48560**
48561** An memdb object can be shared or separate. Shared memdb objects can be
48562** used by more than one database connection. Mutexes are used by shared
48563** memdb objects to coordinate access. Separate memdb objects are only
48564** connected to a single database connection and do not require additional
48565** mutexes.
48566**
48567** Shared memdb objects have .zFName!=0 and .pMutex!=0. They are created
48568** using "file:/name?vfs=memdb". The first character of the name must be
48569** "/" or else the object will be a separate memdb object. All shared
48570** memdb objects are stored in memdb_g.apMemStore[] in an arbitrary order.
48571**
48572** Separate memdb objects are created using a name that does not begin
48573** with "/" or using sqlite3_deserialize().
48574**
48575** Access rules for shared MemStore objects:
48576**
48577** * .zFName is initialized when the object is created and afterwards
48578** is unchanged until the object is destroyed. So it can be accessed
48579** at any time as long as we know the object is not being destroyed,
48580** which means while either the SQLITE_MUTEX_STATIC_VFS1 or
48581** .pMutex is held or the object is not part of memdb_g.apMemStore[].
48582**
48583** * Can .pMutex can only be changed while holding the
48584** SQLITE_MUTEX_STATIC_VFS1 mutex or while the object is not part
48585** of memdb_g.apMemStore[].
48586**
48587** * Other fields can only be changed while holding the .pMutex mutex
48588** or when the .nRef is less than zero and the object is not part of
48589** memdb_g.apMemStore[].
48590**
48591** * The .aData pointer has the added requirement that it can can only
48592** be changed (for resizing) when nMmap is zero.
48593**
48594*/
48595struct MemStore {
48596 sqlite3_int64 sz; /* Size of the file */
48597 sqlite3_int64 szAlloc; /* Space allocated to aData */
48598 sqlite3_int64 szMax; /* Maximum allowed size of the file */
48599 unsigned char *aData; /* content of the file */
48600 sqlite3_mutex *pMutex; /* Used by shared stores only */
48601 int nMmap; /* Number of memory mapped pages */
48602 unsigned mFlags; /* Flags */
48603 int nRdLock; /* Number of readers */
48604 int nWrLock; /* Number of writers. (Always 0 or 1) */
48605 int nRef; /* Number of users of this MemStore */
48606 char *zFName; /* The filename for shared stores */
48607};
48608
48609/* An open file */
48610struct MemFile {
48611 sqlite3_file base; /* IO methods */
48612 MemStore *pStore; /* The storage */
48613 int eLock; /* Most recent lock against this file */
48614};
48615
48616/*
48617** File-scope variables for holding the memdb files that are accessible
48618** to multiple database connections in separate threads.
48619**
48620** Must hold SQLITE_MUTEX_STATIC_VFS1 to access any part of this object.
48621*/
48622static struct MemFS {
48623 int nMemStore; /* Number of shared MemStore objects */
48624 MemStore **apMemStore; /* Array of all shared MemStore objects */
48625} memdb_g;
48626
48627/*
48628** Methods for MemFile
48629*/
48630static int memdbClose(sqlite3_file*);
48631static int memdbRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
48632static int memdbWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
48633static int memdbTruncate(sqlite3_file*, sqlite3_int64 size);
48634static int memdbSync(sqlite3_file*, int flags);
48635static int memdbFileSize(sqlite3_file*, sqlite3_int64 *pSize);
48636static int memdbLock(sqlite3_file*, int);
48637/* static int memdbCheckReservedLock(sqlite3_file*, int *pResOut);// not used */
48638static int memdbFileControl(sqlite3_file*, int op, void *pArg);
48639/* static int memdbSectorSize(sqlite3_file*); // not used */
48640static int memdbDeviceCharacteristics(sqlite3_file*);
48641static int memdbFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
48642static int memdbUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
48643
48644/*
48645** Methods for MemVfs
48646*/
48647static int memdbOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
48648/* static int memdbDelete(sqlite3_vfs*, const char *zName, int syncDir); */
48649static int memdbAccess(sqlite3_vfs*, const char *zName, int flags, int *);
48650static int memdbFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
48651static void *memdbDlOpen(sqlite3_vfs*, const char *zFilename);
48652static void memdbDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
48653static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
48654static void memdbDlClose(sqlite3_vfs*, void*);
48655static int memdbRandomness(sqlite3_vfs*, int nByte, char *zOut);
48656static int memdbSleep(sqlite3_vfs*, int microseconds);
48657/* static int memdbCurrentTime(sqlite3_vfs*, double*); */
48658static int memdbGetLastError(sqlite3_vfs*, int, char *);
48659static int memdbCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
48660
48661static sqlite3_vfs memdb_vfs = {
48662 2, /* iVersion */
48663 0, /* szOsFile (set when registered) */
48664 1024, /* mxPathname */
48665 0, /* pNext */
48666 "memdb", /* zName */
48667 0, /* pAppData (set when registered) */
48668 memdbOpen, /* xOpen */
48669 0, /* memdbDelete, */ /* xDelete */
48670 memdbAccess, /* xAccess */
48671 memdbFullPathname, /* xFullPathname */
48672 memdbDlOpen, /* xDlOpen */
48673 memdbDlError, /* xDlError */
48674 memdbDlSym, /* xDlSym */
48675 memdbDlClose, /* xDlClose */
48676 memdbRandomness, /* xRandomness */
48677 memdbSleep, /* xSleep */
48678 0, /* memdbCurrentTime, */ /* xCurrentTime */
48679 memdbGetLastError, /* xGetLastError */
48680 memdbCurrentTimeInt64, /* xCurrentTimeInt64 */
48681 0, /* xSetSystemCall */
48682 0, /* xGetSystemCall */
48683 0, /* xNextSystemCall */
48684};
48685
48686static const sqlite3_io_methods memdb_io_methods = {
48687 3, /* iVersion */
48688 memdbClose, /* xClose */
48689 memdbRead, /* xRead */
48690 memdbWrite, /* xWrite */
48691 memdbTruncate, /* xTruncate */
48692 memdbSync, /* xSync */
48693 memdbFileSize, /* xFileSize */
48694 memdbLock, /* xLock */
48695 memdbLock, /* xUnlock - same as xLock in this case */
48696 0, /* memdbCheckReservedLock, */ /* xCheckReservedLock */
48697 memdbFileControl, /* xFileControl */
48698 0, /* memdbSectorSize,*/ /* xSectorSize */
48699 memdbDeviceCharacteristics, /* xDeviceCharacteristics */
48700 0, /* xShmMap */
48701 0, /* xShmLock */
48702 0, /* xShmBarrier */
48703 0, /* xShmUnmap */
48704 memdbFetch, /* xFetch */
48705 memdbUnfetch /* xUnfetch */
48706};
48707
48708/*
48709** Enter/leave the mutex on a MemStore
48710*/
48711#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE==0
48712static void memdbEnter(MemStore *p){
48713 UNUSED_PARAMETER(p);
48714}
48715static void memdbLeave(MemStore *p){
48716 UNUSED_PARAMETER(p);
48717}
48718#else
48719static void memdbEnter(MemStore *p){
48720 sqlite3_mutex_enter(p: p->pMutex);
48721}
48722static void memdbLeave(MemStore *p){
48723 sqlite3_mutex_leave(p: p->pMutex);
48724}
48725#endif
48726
48727
48728
48729/*
48730** Close an memdb-file.
48731** Free the underlying MemStore object when its refcount drops to zero
48732** or less.
48733*/
48734static int memdbClose(sqlite3_file *pFile){
48735 MemStore *p = ((MemFile*)pFile)->pStore;
48736 if( p->zFName ){
48737 int i;
48738#ifndef SQLITE_MUTEX_OMIT
48739 sqlite3_mutex *pVfsMutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
48740#endif
48741 sqlite3_mutex_enter(p: pVfsMutex);
48742 for(i=0; ALWAYS(i<memdb_g.nMemStore); i++){
48743 if( memdb_g.apMemStore[i]==p ){
48744 memdbEnter(p);
48745 if( p->nRef==1 ){
48746 memdb_g.apMemStore[i] = memdb_g.apMemStore[--memdb_g.nMemStore];
48747 if( memdb_g.nMemStore==0 ){
48748 sqlite3_free(p: memdb_g.apMemStore);
48749 memdb_g.apMemStore = 0;
48750 }
48751 }
48752 break;
48753 }
48754 }
48755 sqlite3_mutex_leave(p: pVfsMutex);
48756 }else{
48757 memdbEnter(p);
48758 }
48759 p->nRef--;
48760 if( p->nRef<=0 ){
48761 if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE ){
48762 sqlite3_free(p: p->aData);
48763 }
48764 memdbLeave(p);
48765 sqlite3_mutex_free(p: p->pMutex);
48766 sqlite3_free(p);
48767 }else{
48768 memdbLeave(p);
48769 }
48770 return SQLITE_OK;
48771}
48772
48773/*
48774** Read data from an memdb-file.
48775*/
48776static int memdbRead(
48777 sqlite3_file *pFile,
48778 void *zBuf,
48779 int iAmt,
48780 sqlite_int64 iOfst
48781){
48782 MemStore *p = ((MemFile*)pFile)->pStore;
48783 memdbEnter(p);
48784 if( iOfst+iAmt>p->sz ){
48785 memset(s: zBuf, c: 0, n: iAmt);
48786 if( iOfst<p->sz ) memcpy(dest: zBuf, src: p->aData+iOfst, n: p->sz - iOfst);
48787 memdbLeave(p);
48788 return SQLITE_IOERR_SHORT_READ;
48789 }
48790 memcpy(dest: zBuf, src: p->aData+iOfst, n: iAmt);
48791 memdbLeave(p);
48792 return SQLITE_OK;
48793}
48794
48795/*
48796** Try to enlarge the memory allocation to hold at least sz bytes
48797*/
48798static int memdbEnlarge(MemStore *p, sqlite3_int64 newSz){
48799 unsigned char *pNew;
48800 if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)==0 || NEVER(p->nMmap>0) ){
48801 return SQLITE_FULL;
48802 }
48803 if( newSz>p->szMax ){
48804 return SQLITE_FULL;
48805 }
48806 newSz *= 2;
48807 if( newSz>p->szMax ) newSz = p->szMax;
48808 pNew = sqlite3Realloc(pOld: p->aData, nBytes: newSz);
48809 if( pNew==0 ) return SQLITE_IOERR_NOMEM;
48810 p->aData = pNew;
48811 p->szAlloc = newSz;
48812 return SQLITE_OK;
48813}
48814
48815/*
48816** Write data to an memdb-file.
48817*/
48818static int memdbWrite(
48819 sqlite3_file *pFile,
48820 const void *z,
48821 int iAmt,
48822 sqlite_int64 iOfst
48823){
48824 MemStore *p = ((MemFile*)pFile)->pStore;
48825 memdbEnter(p);
48826 if( NEVER(p->mFlags & SQLITE_DESERIALIZE_READONLY) ){
48827 /* Can't happen: memdbLock() will return SQLITE_READONLY before
48828 ** reaching this point */
48829 memdbLeave(p);
48830 return SQLITE_IOERR_WRITE;
48831 }
48832 if( iOfst+iAmt>p->sz ){
48833 int rc;
48834 if( iOfst+iAmt>p->szAlloc
48835 && (rc = memdbEnlarge(p, newSz: iOfst+iAmt))!=SQLITE_OK
48836 ){
48837 memdbLeave(p);
48838 return rc;
48839 }
48840 if( iOfst>p->sz ) memset(s: p->aData+p->sz, c: 0, n: iOfst-p->sz);
48841 p->sz = iOfst+iAmt;
48842 }
48843 memcpy(dest: p->aData+iOfst, src: z, n: iAmt);
48844 memdbLeave(p);
48845 return SQLITE_OK;
48846}
48847
48848/*
48849** Truncate an memdb-file.
48850**
48851** In rollback mode (which is always the case for memdb, as it does not
48852** support WAL mode) the truncate() method is only used to reduce
48853** the size of a file, never to increase the size.
48854*/
48855static int memdbTruncate(sqlite3_file *pFile, sqlite_int64 size){
48856 MemStore *p = ((MemFile*)pFile)->pStore;
48857 int rc = SQLITE_OK;
48858 memdbEnter(p);
48859 if( size>p->sz ){
48860 /* This can only happen with a corrupt wal mode db */
48861 rc = SQLITE_CORRUPT;
48862 }else{
48863 p->sz = size;
48864 }
48865 memdbLeave(p);
48866 return rc;
48867}
48868
48869/*
48870** Sync an memdb-file.
48871*/
48872static int memdbSync(sqlite3_file *pFile, int flags){
48873 UNUSED_PARAMETER(pFile);
48874 UNUSED_PARAMETER(flags);
48875 return SQLITE_OK;
48876}
48877
48878/*
48879** Return the current file-size of an memdb-file.
48880*/
48881static int memdbFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
48882 MemStore *p = ((MemFile*)pFile)->pStore;
48883 memdbEnter(p);
48884 *pSize = p->sz;
48885 memdbLeave(p);
48886 return SQLITE_OK;
48887}
48888
48889/*
48890** Lock an memdb-file.
48891*/
48892static int memdbLock(sqlite3_file *pFile, int eLock){
48893 MemFile *pThis = (MemFile*)pFile;
48894 MemStore *p = pThis->pStore;
48895 int rc = SQLITE_OK;
48896 if( eLock==pThis->eLock ) return SQLITE_OK;
48897 memdbEnter(p);
48898 if( eLock>SQLITE_LOCK_SHARED ){
48899 if( p->mFlags & SQLITE_DESERIALIZE_READONLY ){
48900 rc = SQLITE_READONLY;
48901 }else if( pThis->eLock<=SQLITE_LOCK_SHARED ){
48902 if( p->nWrLock ){
48903 rc = SQLITE_BUSY;
48904 }else{
48905 p->nWrLock = 1;
48906 }
48907 }
48908 }else if( eLock==SQLITE_LOCK_SHARED ){
48909 if( pThis->eLock > SQLITE_LOCK_SHARED ){
48910 assert( p->nWrLock==1 );
48911 p->nWrLock = 0;
48912 }else if( p->nWrLock ){
48913 rc = SQLITE_BUSY;
48914 }else{
48915 p->nRdLock++;
48916 }
48917 }else{
48918 assert( eLock==SQLITE_LOCK_NONE );
48919 if( pThis->eLock>SQLITE_LOCK_SHARED ){
48920 assert( p->nWrLock==1 );
48921 p->nWrLock = 0;
48922 }
48923 assert( p->nRdLock>0 );
48924 p->nRdLock--;
48925 }
48926 if( rc==SQLITE_OK ) pThis->eLock = eLock;
48927 memdbLeave(p);
48928 return rc;
48929}
48930
48931#if 0
48932/*
48933** This interface is only used for crash recovery, which does not
48934** occur on an in-memory database.
48935*/
48936static int memdbCheckReservedLock(sqlite3_file *pFile, int *pResOut){
48937 *pResOut = 0;
48938 return SQLITE_OK;
48939}
48940#endif
48941
48942
48943/*
48944** File control method. For custom operations on an memdb-file.
48945*/
48946static int memdbFileControl(sqlite3_file *pFile, int op, void *pArg){
48947 MemStore *p = ((MemFile*)pFile)->pStore;
48948 int rc = SQLITE_NOTFOUND;
48949 memdbEnter(p);
48950 if( op==SQLITE_FCNTL_VFSNAME ){
48951 *(char**)pArg = sqlite3_mprintf(zFormat: "memdb(%p,%lld)", p->aData, p->sz);
48952 rc = SQLITE_OK;
48953 }
48954 if( op==SQLITE_FCNTL_SIZE_LIMIT ){
48955 sqlite3_int64 iLimit = *(sqlite3_int64*)pArg;
48956 if( iLimit<p->sz ){
48957 if( iLimit<0 ){
48958 iLimit = p->szMax;
48959 }else{
48960 iLimit = p->sz;
48961 }
48962 }
48963 p->szMax = iLimit;
48964 *(sqlite3_int64*)pArg = iLimit;
48965 rc = SQLITE_OK;
48966 }
48967 memdbLeave(p);
48968 return rc;
48969}
48970
48971#if 0 /* Not used because of SQLITE_IOCAP_POWERSAFE_OVERWRITE */
48972/*
48973** Return the sector-size in bytes for an memdb-file.
48974*/
48975static int memdbSectorSize(sqlite3_file *pFile){
48976 return 1024;
48977}
48978#endif
48979
48980/*
48981** Return the device characteristic flags supported by an memdb-file.
48982*/
48983static int memdbDeviceCharacteristics(sqlite3_file *pFile){
48984 UNUSED_PARAMETER(pFile);
48985 return SQLITE_IOCAP_ATOMIC |
48986 SQLITE_IOCAP_POWERSAFE_OVERWRITE |
48987 SQLITE_IOCAP_SAFE_APPEND |
48988 SQLITE_IOCAP_SEQUENTIAL;
48989}
48990
48991/* Fetch a page of a memory-mapped file */
48992static int memdbFetch(
48993 sqlite3_file *pFile,
48994 sqlite3_int64 iOfst,
48995 int iAmt,
48996 void **pp
48997){
48998 MemStore *p = ((MemFile*)pFile)->pStore;
48999 memdbEnter(p);
49000 if( iOfst+iAmt>p->sz || (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)!=0 ){
49001 *pp = 0;
49002 }else{
49003 p->nMmap++;
49004 *pp = (void*)(p->aData + iOfst);
49005 }
49006 memdbLeave(p);
49007 return SQLITE_OK;
49008}
49009
49010/* Release a memory-mapped page */
49011static int memdbUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
49012 MemStore *p = ((MemFile*)pFile)->pStore;
49013 UNUSED_PARAMETER(iOfst);
49014 UNUSED_PARAMETER(pPage);
49015 memdbEnter(p);
49016 p->nMmap--;
49017 memdbLeave(p);
49018 return SQLITE_OK;
49019}
49020
49021/*
49022** Open an mem file handle.
49023*/
49024static int memdbOpen(
49025 sqlite3_vfs *pVfs,
49026 const char *zName,
49027 sqlite3_file *pFd,
49028 int flags,
49029 int *pOutFlags
49030){
49031 MemFile *pFile = (MemFile*)pFd;
49032 MemStore *p = 0;
49033 int szName;
49034 UNUSED_PARAMETER(pVfs);
49035
49036 memset(s: pFile, c: 0, n: sizeof(*pFile));
49037 szName = sqlite3Strlen30(z: zName);
49038 if( szName>1 && zName[0]=='/' ){
49039 int i;
49040#ifndef SQLITE_MUTEX_OMIT
49041 sqlite3_mutex *pVfsMutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
49042#endif
49043 sqlite3_mutex_enter(p: pVfsMutex);
49044 for(i=0; i<memdb_g.nMemStore; i++){
49045 if( strcmp(s1: memdb_g.apMemStore[i]->zFName,s2: zName)==0 ){
49046 p = memdb_g.apMemStore[i];
49047 break;
49048 }
49049 }
49050 if( p==0 ){
49051 MemStore **apNew;
49052 p = sqlite3Malloc( n: sizeof(*p) + szName + 3 );
49053 if( p==0 ){
49054 sqlite3_mutex_leave(p: pVfsMutex);
49055 return SQLITE_NOMEM;
49056 }
49057 apNew = sqlite3Realloc(pOld: memdb_g.apMemStore,
49058 nBytes: sizeof(apNew[0])*(memdb_g.nMemStore+1) );
49059 if( apNew==0 ){
49060 sqlite3_free(p);
49061 sqlite3_mutex_leave(p: pVfsMutex);
49062 return SQLITE_NOMEM;
49063 }
49064 apNew[memdb_g.nMemStore++] = p;
49065 memdb_g.apMemStore = apNew;
49066 memset(s: p, c: 0, n: sizeof(*p));
49067 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE|SQLITE_DESERIALIZE_FREEONCLOSE;
49068 p->szMax = sqlite3GlobalConfig.mxMemdbSize;
49069 p->zFName = (char*)&p[1];
49070 memcpy(dest: p->zFName, src: zName, n: szName+1);
49071 p->pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
49072 if( p->pMutex==0 ){
49073 memdb_g.nMemStore--;
49074 sqlite3_free(p);
49075 sqlite3_mutex_leave(p: pVfsMutex);
49076 return SQLITE_NOMEM;
49077 }
49078 p->nRef = 1;
49079 memdbEnter(p);
49080 }else{
49081 memdbEnter(p);
49082 p->nRef++;
49083 }
49084 sqlite3_mutex_leave(p: pVfsMutex);
49085 }else{
49086 p = sqlite3Malloc( n: sizeof(*p) );
49087 if( p==0 ){
49088 return SQLITE_NOMEM;
49089 }
49090 memset(s: p, c: 0, n: sizeof(*p));
49091 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE;
49092 p->szMax = sqlite3GlobalConfig.mxMemdbSize;
49093 }
49094 pFile->pStore = p;
49095 if( pOutFlags!=0 ){
49096 *pOutFlags = flags | SQLITE_OPEN_MEMORY;
49097 }
49098 pFd->pMethods = &memdb_io_methods;
49099 memdbLeave(p);
49100 return SQLITE_OK;
49101}
49102
49103#if 0 /* Only used to delete rollback journals, super-journals, and WAL
49104 ** files, none of which exist in memdb. So this routine is never used */
49105/*
49106** Delete the file located at zPath. If the dirSync argument is true,
49107** ensure the file-system modifications are synced to disk before
49108** returning.
49109*/
49110static int memdbDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
49111 return SQLITE_IOERR_DELETE;
49112}
49113#endif
49114
49115/*
49116** Test for access permissions. Return true if the requested permission
49117** is available, or false otherwise.
49118**
49119** With memdb, no files ever exist on disk. So always return false.
49120*/
49121static int memdbAccess(
49122 sqlite3_vfs *pVfs,
49123 const char *zPath,
49124 int flags,
49125 int *pResOut
49126){
49127 UNUSED_PARAMETER(pVfs);
49128 UNUSED_PARAMETER(zPath);
49129 UNUSED_PARAMETER(flags);
49130 *pResOut = 0;
49131 return SQLITE_OK;
49132}
49133
49134/*
49135** Populate buffer zOut with the full canonical pathname corresponding
49136** to the pathname in zPath. zOut is guaranteed to point to a buffer
49137** of at least (INST_MAX_PATHNAME+1) bytes.
49138*/
49139static int memdbFullPathname(
49140 sqlite3_vfs *pVfs,
49141 const char *zPath,
49142 int nOut,
49143 char *zOut
49144){
49145 UNUSED_PARAMETER(pVfs);
49146 sqlite3_snprintf(n: nOut, zBuf: zOut, zFormat: "%s", zPath);
49147 return SQLITE_OK;
49148}
49149
49150/*
49151** Open the dynamic library located at zPath and return a handle.
49152*/
49153static void *memdbDlOpen(sqlite3_vfs *pVfs, const char *zPath){
49154 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
49155}
49156
49157/*
49158** Populate the buffer zErrMsg (size nByte bytes) with a human readable
49159** utf-8 string describing the most recent error encountered associated
49160** with dynamic libraries.
49161*/
49162static void memdbDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
49163 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
49164}
49165
49166/*
49167** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
49168*/
49169static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
49170 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
49171}
49172
49173/*
49174** Close the dynamic library handle pHandle.
49175*/
49176static void memdbDlClose(sqlite3_vfs *pVfs, void *pHandle){
49177 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
49178}
49179
49180/*
49181** Populate the buffer pointed to by zBufOut with nByte bytes of
49182** random data.
49183*/
49184static int memdbRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
49185 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
49186}
49187
49188/*
49189** Sleep for nMicro microseconds. Return the number of microseconds
49190** actually slept.
49191*/
49192static int memdbSleep(sqlite3_vfs *pVfs, int nMicro){
49193 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
49194}
49195
49196#if 0 /* Never used. Modern cores only call xCurrentTimeInt64() */
49197/*
49198** Return the current time as a Julian Day number in *pTimeOut.
49199*/
49200static int memdbCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
49201 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
49202}
49203#endif
49204
49205static int memdbGetLastError(sqlite3_vfs *pVfs, int a, char *b){
49206 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
49207}
49208static int memdbCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
49209 return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
49210}
49211
49212/*
49213** Translate a database connection pointer and schema name into a
49214** MemFile pointer.
49215*/
49216static MemFile *memdbFromDbSchema(sqlite3 *db, const char *zSchema){
49217 MemFile *p = 0;
49218 MemStore *pStore;
49219 int rc = sqlite3_file_control(db, zDbName: zSchema, SQLITE_FCNTL_FILE_POINTER, &p);
49220 if( rc ) return 0;
49221 if( p->base.pMethods!=&memdb_io_methods ) return 0;
49222 pStore = p->pStore;
49223 memdbEnter(p: pStore);
49224 if( pStore->zFName!=0 ) p = 0;
49225 memdbLeave(p: pStore);
49226 return p;
49227}
49228
49229/*
49230** Return the serialization of a database
49231*/
49232SQLITE_API unsigned char *sqlite3_serialize(
49233 sqlite3 *db, /* The database connection */
49234 const char *zSchema, /* Which database within the connection */
49235 sqlite3_int64 *piSize, /* Write size here, if not NULL */
49236 unsigned int mFlags /* Maybe SQLITE_SERIALIZE_NOCOPY */
49237){
49238 MemFile *p;
49239 int iDb;
49240 Btree *pBt;
49241 sqlite3_int64 sz;
49242 int szPage = 0;
49243 sqlite3_stmt *pStmt = 0;
49244 unsigned char *pOut;
49245 char *zSql;
49246 int rc;
49247
49248#ifdef SQLITE_ENABLE_API_ARMOR
49249 if( !sqlite3SafetyCheckOk(db) ){
49250 (void)SQLITE_MISUSE_BKPT;
49251 return 0;
49252 }
49253#endif
49254
49255 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
49256 p = memdbFromDbSchema(db, zSchema);
49257 iDb = sqlite3FindDbName(db, zSchema);
49258 if( piSize ) *piSize = -1;
49259 if( iDb<0 ) return 0;
49260 if( p ){
49261 MemStore *pStore = p->pStore;
49262 assert( pStore->pMutex==0 );
49263 if( piSize ) *piSize = pStore->sz;
49264 if( mFlags & SQLITE_SERIALIZE_NOCOPY ){
49265 pOut = pStore->aData;
49266 }else{
49267 pOut = sqlite3_malloc64( n: pStore->sz );
49268 if( pOut ) memcpy(dest: pOut, src: pStore->aData, n: pStore->sz);
49269 }
49270 return pOut;
49271 }
49272 pBt = db->aDb[iDb].pBt;
49273 if( pBt==0 ) return 0;
49274 szPage = sqlite3BtreeGetPageSize(pBt);
49275 zSql = sqlite3_mprintf(zFormat: "PRAGMA \"%w\".page_count", zSchema);
49276 rc = zSql ? sqlite3_prepare_v2(db, zSql, nByte: -1, ppStmt: &pStmt, pzTail: 0) : SQLITE_NOMEM;
49277 sqlite3_free(p: zSql);
49278 if( rc ) return 0;
49279 rc = sqlite3_step(pStmt);
49280 if( rc!=SQLITE_ROW ){
49281 pOut = 0;
49282 }else{
49283 sz = sqlite3_column_int64(pStmt, iCol: 0)*szPage;
49284 if( piSize ) *piSize = sz;
49285 if( mFlags & SQLITE_SERIALIZE_NOCOPY ){
49286 pOut = 0;
49287 }else{
49288 pOut = sqlite3_malloc64( n: sz );
49289 if( pOut ){
49290 int nPage = sqlite3_column_int(pStmt, iCol: 0);
49291 Pager *pPager = sqlite3BtreePager(pBt);
49292 int pgno;
49293 for(pgno=1; pgno<=nPage; pgno++){
49294 DbPage *pPage = 0;
49295 unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1);
49296 rc = sqlite3PagerGet(pPager, pgno, ppPage: (DbPage**)&pPage, clrFlag: 0);
49297 if( rc==SQLITE_OK ){
49298 memcpy(dest: pTo, src: sqlite3PagerGetData(pPage), n: szPage);
49299 }else{
49300 memset(s: pTo, c: 0, n: szPage);
49301 }
49302 sqlite3PagerUnref(pPage);
49303 }
49304 }
49305 }
49306 }
49307 sqlite3_finalize(pStmt);
49308 return pOut;
49309}
49310
49311/* Convert zSchema to a MemDB and initialize its content.
49312*/
49313SQLITE_API int sqlite3_deserialize(
49314 sqlite3 *db, /* The database connection */
49315 const char *zSchema, /* Which DB to reopen with the deserialization */
49316 unsigned char *pData, /* The serialized database content */
49317 sqlite3_int64 szDb, /* Number bytes in the deserialization */
49318 sqlite3_int64 szBuf, /* Total size of buffer pData[] */
49319 unsigned mFlags /* Zero or more SQLITE_DESERIALIZE_* flags */
49320){
49321 MemFile *p;
49322 char *zSql;
49323 sqlite3_stmt *pStmt = 0;
49324 int rc;
49325 int iDb;
49326
49327#ifdef SQLITE_ENABLE_API_ARMOR
49328 if( !sqlite3SafetyCheckOk(db) ){
49329 return SQLITE_MISUSE_BKPT;
49330 }
49331 if( szDb<0 ) return SQLITE_MISUSE_BKPT;
49332 if( szBuf<0 ) return SQLITE_MISUSE_BKPT;
49333#endif
49334
49335 sqlite3_mutex_enter(p: db->mutex);
49336 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
49337 iDb = sqlite3FindDbName(db, zSchema);
49338 testcase( iDb==1 );
49339 if( iDb<2 && iDb!=0 ){
49340 rc = SQLITE_ERROR;
49341 goto end_deserialize;
49342 }
49343 zSql = sqlite3_mprintf(zFormat: "ATTACH x AS %Q", zSchema);
49344 if( zSql==0 ){
49345 rc = SQLITE_NOMEM;
49346 }else{
49347 rc = sqlite3_prepare_v2(db, zSql, nByte: -1, ppStmt: &pStmt, pzTail: 0);
49348 sqlite3_free(p: zSql);
49349 }
49350 if( rc ) goto end_deserialize;
49351 db->init.iDb = (u8)iDb;
49352 db->init.reopenMemdb = 1;
49353 rc = sqlite3_step(pStmt);
49354 db->init.reopenMemdb = 0;
49355 if( rc!=SQLITE_DONE ){
49356 rc = SQLITE_ERROR;
49357 goto end_deserialize;
49358 }
49359 p = memdbFromDbSchema(db, zSchema);
49360 if( p==0 ){
49361 rc = SQLITE_ERROR;
49362 }else{
49363 MemStore *pStore = p->pStore;
49364 pStore->aData = pData;
49365 pData = 0;
49366 pStore->sz = szDb;
49367 pStore->szAlloc = szBuf;
49368 pStore->szMax = szBuf;
49369 if( pStore->szMax<sqlite3GlobalConfig.mxMemdbSize ){
49370 pStore->szMax = sqlite3GlobalConfig.mxMemdbSize;
49371 }
49372 pStore->mFlags = mFlags;
49373 rc = SQLITE_OK;
49374 }
49375
49376end_deserialize:
49377 sqlite3_finalize(pStmt);
49378 if( pData && (mFlags & SQLITE_DESERIALIZE_FREEONCLOSE)!=0 ){
49379 sqlite3_free(p: pData);
49380 }
49381 sqlite3_mutex_leave(p: db->mutex);
49382 return rc;
49383}
49384
49385/*
49386** This routine is called when the extension is loaded.
49387** Register the new VFS.
49388*/
49389SQLITE_PRIVATE int sqlite3MemdbInit(void){
49390 sqlite3_vfs *pLower = sqlite3_vfs_find(zVfs: 0);
49391 unsigned int sz;
49392 if( NEVER(pLower==0) ) return SQLITE_ERROR;
49393 sz = pLower->szOsFile;
49394 memdb_vfs.pAppData = pLower;
49395 /* The following conditional can only be true when compiled for
49396 ** Windows x86 and SQLITE_MAX_MMAP_SIZE=0. We always leave
49397 ** it in, to be safe, but it is marked as NO_TEST since there
49398 ** is no way to reach it under most builds. */
49399 if( sz<sizeof(MemFile) ) sz = sizeof(MemFile); /*NO_TEST*/
49400 memdb_vfs.szOsFile = sz;
49401 return sqlite3_vfs_register(pVfs: &memdb_vfs, makeDflt: 0);
49402}
49403#endif /* SQLITE_OMIT_DESERIALIZE */
49404
49405/************** End of memdb.c ***********************************************/
49406/************** Begin file bitvec.c ******************************************/
49407/*
49408** 2008 February 16
49409**
49410** The author disclaims copyright to this source code. In place of
49411** a legal notice, here is a blessing:
49412**
49413** May you do good and not evil.
49414** May you find forgiveness for yourself and forgive others.
49415** May you share freely, never taking more than you give.
49416**
49417*************************************************************************
49418** This file implements an object that represents a fixed-length
49419** bitmap. Bits are numbered starting with 1.
49420**
49421** A bitmap is used to record which pages of a database file have been
49422** journalled during a transaction, or which pages have the "dont-write"
49423** property. Usually only a few pages are meet either condition.
49424** So the bitmap is usually sparse and has low cardinality.
49425** But sometimes (for example when during a DROP of a large table) most
49426** or all of the pages in a database can get journalled. In those cases,
49427** the bitmap becomes dense with high cardinality. The algorithm needs
49428** to handle both cases well.
49429**
49430** The size of the bitmap is fixed when the object is created.
49431**
49432** All bits are clear when the bitmap is created. Individual bits
49433** may be set or cleared one at a time.
49434**
49435** Test operations are about 100 times more common that set operations.
49436** Clear operations are exceedingly rare. There are usually between
49437** 5 and 500 set operations per Bitvec object, though the number of sets can
49438** sometimes grow into tens of thousands or larger. The size of the
49439** Bitvec object is the number of pages in the database file at the
49440** start of a transaction, and is thus usually less than a few thousand,
49441** but can be as large as 2 billion for a really big database.
49442*/
49443/* #include "sqliteInt.h" */
49444
49445/* Size of the Bitvec structure in bytes. */
49446#define BITVEC_SZ 512
49447
49448/* Round the union size down to the nearest pointer boundary, since that's how
49449** it will be aligned within the Bitvec struct. */
49450#define BITVEC_USIZE \
49451 (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
49452
49453/* Type of the array "element" for the bitmap representation.
49454** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE.
49455** Setting this to the "natural word" size of your CPU may improve
49456** performance. */
49457#define BITVEC_TELEM u8
49458/* Size, in bits, of the bitmap element. */
49459#define BITVEC_SZELEM 8
49460/* Number of elements in a bitmap array. */
49461#define BITVEC_NELEM (BITVEC_USIZE/sizeof(BITVEC_TELEM))
49462/* Number of bits in the bitmap array. */
49463#define BITVEC_NBIT (BITVEC_NELEM*BITVEC_SZELEM)
49464
49465/* Number of u32 values in hash table. */
49466#define BITVEC_NINT (BITVEC_USIZE/sizeof(u32))
49467/* Maximum number of entries in hash table before
49468** sub-dividing and re-hashing. */
49469#define BITVEC_MXHASH (BITVEC_NINT/2)
49470/* Hashing function for the aHash representation.
49471** Empirical testing showed that the *37 multiplier
49472** (an arbitrary prime)in the hash function provided
49473** no fewer collisions than the no-op *1. */
49474#define BITVEC_HASH(X) (((X)*1)%BITVEC_NINT)
49475
49476#define BITVEC_NPTR (BITVEC_USIZE/sizeof(Bitvec *))
49477
49478
49479/*
49480** A bitmap is an instance of the following structure.
49481**
49482** This bitmap records the existence of zero or more bits
49483** with values between 1 and iSize, inclusive.
49484**
49485** There are three possible representations of the bitmap.
49486** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
49487** bitmap. The least significant bit is bit 1.
49488**
49489** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is
49490** a hash table that will hold up to BITVEC_MXHASH distinct values.
49491**
49492** Otherwise, the value i is redirected into one of BITVEC_NPTR
49493** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap
49494** handles up to iDivisor separate values of i. apSub[0] holds
49495** values between 1 and iDivisor. apSub[1] holds values between
49496** iDivisor+1 and 2*iDivisor. apSub[N] holds values between
49497** N*iDivisor+1 and (N+1)*iDivisor. Each subbitmap is normalized
49498** to hold deal with values between 1 and iDivisor.
49499*/
49500struct Bitvec {
49501 u32 iSize; /* Maximum bit index. Max iSize is 4,294,967,296. */
49502 u32 nSet; /* Number of bits that are set - only valid for aHash
49503 ** element. Max is BITVEC_NINT. For BITVEC_SZ of 512,
49504 ** this would be 125. */
49505 u32 iDivisor; /* Number of bits handled by each apSub[] entry. */
49506 /* Should >=0 for apSub element. */
49507 /* Max iDivisor is max(u32) / BITVEC_NPTR + 1. */
49508 /* For a BITVEC_SZ of 512, this would be 34,359,739. */
49509 union {
49510 BITVEC_TELEM aBitmap[BITVEC_NELEM]; /* Bitmap representation */
49511 u32 aHash[BITVEC_NINT]; /* Hash table representation */
49512 Bitvec *apSub[BITVEC_NPTR]; /* Recursive representation */
49513 } u;
49514};
49515
49516/*
49517** Create a new bitmap object able to handle bits between 0 and iSize,
49518** inclusive. Return a pointer to the new object. Return NULL if
49519** malloc fails.
49520*/
49521SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32 iSize){
49522 Bitvec *p;
49523 assert( sizeof(*p)==BITVEC_SZ );
49524 p = sqlite3MallocZero( n: sizeof(*p) );
49525 if( p ){
49526 p->iSize = iSize;
49527 }
49528 return p;
49529}
49530
49531/*
49532** Check to see if the i-th bit is set. Return true or false.
49533** If p is NULL (if the bitmap has not been created) or if
49534** i is out of range, then return false.
49535*/
49536SQLITE_PRIVATE int sqlite3BitvecTestNotNull(Bitvec *p, u32 i){
49537 assert( p!=0 );
49538 i--;
49539 if( i>=p->iSize ) return 0;
49540 while( p->iDivisor ){
49541 u32 bin = i/p->iDivisor;
49542 i = i%p->iDivisor;
49543 p = p->u.apSub[bin];
49544 if (!p) {
49545 return 0;
49546 }
49547 }
49548 if( p->iSize<=BITVEC_NBIT ){
49549 return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
49550 } else{
49551 u32 h = BITVEC_HASH(i++);
49552 while( p->u.aHash[h] ){
49553 if( p->u.aHash[h]==i ) return 1;
49554 h = (h+1) % BITVEC_NINT;
49555 }
49556 return 0;
49557 }
49558}
49559SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){
49560 return p!=0 && sqlite3BitvecTestNotNull(p,i);
49561}
49562
49563/*
49564** Set the i-th bit. Return 0 on success and an error code if
49565** anything goes wrong.
49566**
49567** This routine might cause sub-bitmaps to be allocated. Failing
49568** to get the memory needed to hold the sub-bitmap is the only
49569** that can go wrong with an insert, assuming p and i are valid.
49570**
49571** The calling function must ensure that p is a valid Bitvec object
49572** and that the value for "i" is within range of the Bitvec object.
49573** Otherwise the behavior is undefined.
49574*/
49575SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec *p, u32 i){
49576 u32 h;
49577 if( p==0 ) return SQLITE_OK;
49578 assert( i>0 );
49579 assert( i<=p->iSize );
49580 i--;
49581 while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
49582 u32 bin = i/p->iDivisor;
49583 i = i%p->iDivisor;
49584 if( p->u.apSub[bin]==0 ){
49585 p->u.apSub[bin] = sqlite3BitvecCreate( iSize: p->iDivisor );
49586 if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT;
49587 }
49588 p = p->u.apSub[bin];
49589 }
49590 if( p->iSize<=BITVEC_NBIT ){
49591 p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
49592 return SQLITE_OK;
49593 }
49594 h = BITVEC_HASH(i++);
49595 /* if there wasn't a hash collision, and this doesn't */
49596 /* completely fill the hash, then just add it without */
49597 /* worring about sub-dividing and re-hashing. */
49598 if( !p->u.aHash[h] ){
49599 if (p->nSet<(BITVEC_NINT-1)) {
49600 goto bitvec_set_end;
49601 } else {
49602 goto bitvec_set_rehash;
49603 }
49604 }
49605 /* there was a collision, check to see if it's already */
49606 /* in hash, if not, try to find a spot for it */
49607 do {
49608 if( p->u.aHash[h]==i ) return SQLITE_OK;
49609 h++;
49610 if( h>=BITVEC_NINT ) h = 0;
49611 } while( p->u.aHash[h] );
49612 /* we didn't find it in the hash. h points to the first */
49613 /* available free spot. check to see if this is going to */
49614 /* make our hash too "full". */
49615bitvec_set_rehash:
49616 if( p->nSet>=BITVEC_MXHASH ){
49617 unsigned int j;
49618 int rc;
49619 u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
49620 if( aiValues==0 ){
49621 return SQLITE_NOMEM_BKPT;
49622 }else{
49623 memcpy(dest: aiValues, src: p->u.aHash, n: sizeof(p->u.aHash));
49624 memset(s: p->u.apSub, c: 0, n: sizeof(p->u.apSub));
49625 p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
49626 rc = sqlite3BitvecSet(p, i);
49627 for(j=0; j<BITVEC_NINT; j++){
49628 if( aiValues[j] ) rc |= sqlite3BitvecSet(p, i: aiValues[j]);
49629 }
49630 sqlite3StackFree(0, aiValues);
49631 return rc;
49632 }
49633 }
49634bitvec_set_end:
49635 p->nSet++;
49636 p->u.aHash[h] = i;
49637 return SQLITE_OK;
49638}
49639
49640/*
49641** Clear the i-th bit.
49642**
49643** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage
49644** that BitvecClear can use to rebuilt its hash table.
49645*/
49646SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){
49647 if( p==0 ) return;
49648 assert( i>0 );
49649 i--;
49650 while( p->iDivisor ){
49651 u32 bin = i/p->iDivisor;
49652 i = i%p->iDivisor;
49653 p = p->u.apSub[bin];
49654 if (!p) {
49655 return;
49656 }
49657 }
49658 if( p->iSize<=BITVEC_NBIT ){
49659 p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
49660 }else{
49661 unsigned int j;
49662 u32 *aiValues = pBuf;
49663 memcpy(dest: aiValues, src: p->u.aHash, n: sizeof(p->u.aHash));
49664 memset(s: p->u.aHash, c: 0, n: sizeof(p->u.aHash));
49665 p->nSet = 0;
49666 for(j=0; j<BITVEC_NINT; j++){
49667 if( aiValues[j] && aiValues[j]!=(i+1) ){
49668 u32 h = BITVEC_HASH(aiValues[j]-1);
49669 p->nSet++;
49670 while( p->u.aHash[h] ){
49671 h++;
49672 if( h>=BITVEC_NINT ) h = 0;
49673 }
49674 p->u.aHash[h] = aiValues[j];
49675 }
49676 }
49677 }
49678}
49679
49680/*
49681** Destroy a bitmap object. Reclaim all memory used.
49682*/
49683SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec *p){
49684 if( p==0 ) return;
49685 if( p->iDivisor ){
49686 unsigned int i;
49687 for(i=0; i<BITVEC_NPTR; i++){
49688 sqlite3BitvecDestroy(p: p->u.apSub[i]);
49689 }
49690 }
49691 sqlite3_free(p);
49692}
49693
49694/*
49695** Return the value of the iSize parameter specified when Bitvec *p
49696** was created.
49697*/
49698SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec *p){
49699 return p->iSize;
49700}
49701
49702#ifndef SQLITE_UNTESTABLE
49703/*
49704** Let V[] be an array of unsigned characters sufficient to hold
49705** up to N bits. Let I be an integer between 0 and N. 0<=I<N.
49706** Then the following macros can be used to set, clear, or test
49707** individual bits within V.
49708*/
49709#define SETBIT(V,I) V[I>>3] |= (1<<(I&7))
49710#define CLEARBIT(V,I) V[I>>3] &= ~(1<<(I&7))
49711#define TESTBIT(V,I) (V[I>>3]&(1<<(I&7)))!=0
49712
49713/*
49714** This routine runs an extensive test of the Bitvec code.
49715**
49716** The input is an array of integers that acts as a program
49717** to test the Bitvec. The integers are opcodes followed
49718** by 0, 1, or 3 operands, depending on the opcode. Another
49719** opcode follows immediately after the last operand.
49720**
49721** There are 6 opcodes numbered from 0 through 5. 0 is the
49722** "halt" opcode and causes the test to end.
49723**
49724** 0 Halt and return the number of errors
49725** 1 N S X Set N bits beginning with S and incrementing by X
49726** 2 N S X Clear N bits beginning with S and incrementing by X
49727** 3 N Set N randomly chosen bits
49728** 4 N Clear N randomly chosen bits
49729** 5 N S X Set N bits from S increment X in array only, not in bitvec
49730**
49731** The opcodes 1 through 4 perform set and clear operations are performed
49732** on both a Bitvec object and on a linear array of bits obtained from malloc.
49733** Opcode 5 works on the linear array only, not on the Bitvec.
49734** Opcode 5 is used to deliberately induce a fault in order to
49735** confirm that error detection works.
49736**
49737** At the conclusion of the test the linear array is compared
49738** against the Bitvec object. If there are any differences,
49739** an error is returned. If they are the same, zero is returned.
49740**
49741** If a memory allocation error occurs, return -1.
49742*/
49743SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int sz, int *aOp){
49744 Bitvec *pBitvec = 0;
49745 unsigned char *pV = 0;
49746 int rc = -1;
49747 int i, nx, pc, op;
49748 void *pTmpSpace;
49749
49750 /* Allocate the Bitvec to be tested and a linear array of
49751 ** bits to act as the reference */
49752 pBitvec = sqlite3BitvecCreate( iSize: sz );
49753 pV = sqlite3MallocZero( n: (sz+7)/8 + 1 );
49754 pTmpSpace = sqlite3_malloc64(BITVEC_SZ);
49755 if( pBitvec==0 || pV==0 || pTmpSpace==0 ) goto bitvec_end;
49756
49757 /* NULL pBitvec tests */
49758 sqlite3BitvecSet(p: 0, i: 1);
49759 sqlite3BitvecClear(p: 0, i: 1, pBuf: pTmpSpace);
49760
49761 /* Run the program */
49762 pc = i = 0;
49763 while( (op = aOp[pc])!=0 ){
49764 switch( op ){
49765 case 1:
49766 case 2:
49767 case 5: {
49768 nx = 4;
49769 i = aOp[pc+2] - 1;
49770 aOp[pc+2] += aOp[pc+3];
49771 break;
49772 }
49773 case 3:
49774 case 4:
49775 default: {
49776 nx = 2;
49777 sqlite3_randomness(N: sizeof(i), pBuf: &i);
49778 break;
49779 }
49780 }
49781 if( (--aOp[pc+1]) > 0 ) nx = 0;
49782 pc += nx;
49783 i = (i & 0x7fffffff)%sz;
49784 if( (op & 1)!=0 ){
49785 SETBIT(pV, (i+1));
49786 if( op!=5 ){
49787 if( sqlite3BitvecSet(p: pBitvec, i: i+1) ) goto bitvec_end;
49788 }
49789 }else{
49790 CLEARBIT(pV, (i+1));
49791 sqlite3BitvecClear(p: pBitvec, i: i+1, pBuf: pTmpSpace);
49792 }
49793 }
49794
49795 /* Test to make sure the linear array exactly matches the
49796 ** Bitvec object. Start with the assumption that they do
49797 ** match (rc==0). Change rc to non-zero if a discrepancy
49798 ** is found.
49799 */
49800 rc = sqlite3BitvecTest(p: 0,i: 0) + sqlite3BitvecTest(p: pBitvec, i: sz+1)
49801 + sqlite3BitvecTest(p: pBitvec, i: 0)
49802 + (sqlite3BitvecSize(p: pBitvec) - sz);
49803 for(i=1; i<=sz; i++){
49804 if( (TESTBIT(pV,i))!=sqlite3BitvecTest(p: pBitvec,i) ){
49805 rc = i;
49806 break;
49807 }
49808 }
49809
49810 /* Free allocated structure */
49811bitvec_end:
49812 sqlite3_free(p: pTmpSpace);
49813 sqlite3_free(p: pV);
49814 sqlite3BitvecDestroy(p: pBitvec);
49815 return rc;
49816}
49817#endif /* SQLITE_UNTESTABLE */
49818
49819/************** End of bitvec.c **********************************************/
49820/************** Begin file pcache.c ******************************************/
49821/*
49822** 2008 August 05
49823**
49824** The author disclaims copyright to this source code. In place of
49825** a legal notice, here is a blessing:
49826**
49827** May you do good and not evil.
49828** May you find forgiveness for yourself and forgive others.
49829** May you share freely, never taking more than you give.
49830**
49831*************************************************************************
49832** This file implements that page cache.
49833*/
49834/* #include "sqliteInt.h" */
49835
49836/*
49837** A complete page cache is an instance of this structure. Every
49838** entry in the cache holds a single page of the database file. The
49839** btree layer only operates on the cached copy of the database pages.
49840**
49841** A page cache entry is "clean" if it exactly matches what is currently
49842** on disk. A page is "dirty" if it has been modified and needs to be
49843** persisted to disk.
49844**
49845** pDirty, pDirtyTail, pSynced:
49846** All dirty pages are linked into the doubly linked list using
49847** PgHdr.pDirtyNext and pDirtyPrev. The list is maintained in LRU order
49848** such that p was added to the list more recently than p->pDirtyNext.
49849** PCache.pDirty points to the first (newest) element in the list and
49850** pDirtyTail to the last (oldest).
49851**
49852** The PCache.pSynced variable is used to optimize searching for a dirty
49853** page to eject from the cache mid-transaction. It is better to eject
49854** a page that does not require a journal sync than one that does.
49855** Therefore, pSynced is maintained so that it *almost* always points
49856** to either the oldest page in the pDirty/pDirtyTail list that has a
49857** clear PGHDR_NEED_SYNC flag or to a page that is older than this one
49858** (so that the right page to eject can be found by following pDirtyPrev
49859** pointers).
49860*/
49861struct PCache {
49862 PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */
49863 PgHdr *pSynced; /* Last synced page in dirty page list */
49864 int nRefSum; /* Sum of ref counts over all pages */
49865 int szCache; /* Configured cache size */
49866 int szSpill; /* Size before spilling occurs */
49867 int szPage; /* Size of every page in this cache */
49868 int szExtra; /* Size of extra space for each page */
49869 u8 bPurgeable; /* True if pages are on backing store */
49870 u8 eCreate; /* eCreate value for for xFetch() */
49871 int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
49872 void *pStress; /* Argument to xStress */
49873 sqlite3_pcache *pCache; /* Pluggable cache module */
49874};
49875
49876/********************************** Test and Debug Logic **********************/
49877/*
49878** Debug tracing macros. Enable by by changing the "0" to "1" and
49879** recompiling.
49880**
49881** When sqlite3PcacheTrace is 1, single line trace messages are issued.
49882** When sqlite3PcacheTrace is 2, a dump of the pcache showing all cache entries
49883** is displayed for many operations, resulting in a lot of output.
49884*/
49885#if defined(SQLITE_DEBUG) && 0
49886 int sqlite3PcacheTrace = 2; /* 0: off 1: simple 2: cache dumps */
49887 int sqlite3PcacheMxDump = 9999; /* Max cache entries for pcacheDump() */
49888# define pcacheTrace(X) if(sqlite3PcacheTrace){sqlite3DebugPrintf X;}
49889 void pcacheDump(PCache *pCache){
49890 int N;
49891 int i, j;
49892 sqlite3_pcache_page *pLower;
49893 PgHdr *pPg;
49894 unsigned char *a;
49895
49896 if( sqlite3PcacheTrace<2 ) return;
49897 if( pCache->pCache==0 ) return;
49898 N = sqlite3PcachePagecount(pCache);
49899 if( N>sqlite3PcacheMxDump ) N = sqlite3PcacheMxDump;
49900 for(i=1; i<=N; i++){
49901 pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);
49902 if( pLower==0 ) continue;
49903 pPg = (PgHdr*)pLower->pExtra;
49904 printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags);
49905 a = (unsigned char *)pLower->pBuf;
49906 for(j=0; j<12; j++) printf("%02x", a[j]);
49907 printf("\n");
49908 if( pPg->pPage==0 ){
49909 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
49910 }
49911 }
49912 }
49913 #else
49914# define pcacheTrace(X)
49915# define pcacheDump(X)
49916#endif
49917
49918/*
49919** Check invariants on a PgHdr entry. Return true if everything is OK.
49920** Return false if any invariant is violated.
49921**
49922** This routine is for use inside of assert() statements only. For
49923** example:
49924**
49925** assert( sqlite3PcachePageSanity(pPg) );
49926*/
49927#ifdef SQLITE_DEBUG
49928SQLITE_PRIVATE int sqlite3PcachePageSanity(PgHdr *pPg){
49929 PCache *pCache;
49930 assert( pPg!=0 );
49931 assert( pPg->pgno>0 || pPg->pPager==0 ); /* Page number is 1 or more */
49932 pCache = pPg->pCache;
49933 assert( pCache!=0 ); /* Every page has an associated PCache */
49934 if( pPg->flags & PGHDR_CLEAN ){
49935 assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
49936 assert( pCache->pDirty!=pPg ); /* CLEAN pages not on dirty list */
49937 assert( pCache->pDirtyTail!=pPg );
49938 }
49939 /* WRITEABLE pages must also be DIRTY */
49940 if( pPg->flags & PGHDR_WRITEABLE ){
49941 assert( pPg->flags & PGHDR_DIRTY ); /* WRITEABLE implies DIRTY */
49942 }
49943 /* NEED_SYNC can be set independently of WRITEABLE. This can happen,
49944 ** for example, when using the sqlite3PagerDontWrite() optimization:
49945 ** (1) Page X is journalled, and gets WRITEABLE and NEED_SEEK.
49946 ** (2) Page X moved to freelist, WRITEABLE is cleared
49947 ** (3) Page X reused, WRITEABLE is set again
49948 ** If NEED_SYNC had been cleared in step 2, then it would not be reset
49949 ** in step 3, and page might be written into the database without first
49950 ** syncing the rollback journal, which might cause corruption on a power
49951 ** loss.
49952 **
49953 ** Another example is when the database page size is smaller than the
49954 ** disk sector size. When any page of a sector is journalled, all pages
49955 ** in that sector are marked NEED_SYNC even if they are still CLEAN, just
49956 ** in case they are later modified, since all pages in the same sector
49957 ** must be journalled and synced before any of those pages can be safely
49958 ** written.
49959 */
49960 return 1;
49961}
49962#endif /* SQLITE_DEBUG */
49963
49964
49965/********************************** Linked List Management ********************/
49966
49967/* Allowed values for second argument to pcacheManageDirtyList() */
49968#define PCACHE_DIRTYLIST_REMOVE 1 /* Remove pPage from dirty list */
49969#define PCACHE_DIRTYLIST_ADD 2 /* Add pPage to the dirty list */
49970#define PCACHE_DIRTYLIST_FRONT 3 /* Move pPage to the front of the list */
49971
49972/*
49973** Manage pPage's participation on the dirty list. Bits of the addRemove
49974** argument determines what operation to do. The 0x01 bit means first
49975** remove pPage from the dirty list. The 0x02 means add pPage back to
49976** the dirty list. Doing both moves pPage to the front of the dirty list.
49977*/
49978static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){
49979 PCache *p = pPage->pCache;
49980
49981 pcacheTrace(("%p.DIRTYLIST.%s %d\n", p,
49982 addRemove==1 ? "REMOVE" : addRemove==2 ? "ADD" : "FRONT",
49983 pPage->pgno));
49984 if( addRemove & PCACHE_DIRTYLIST_REMOVE ){
49985 assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
49986 assert( pPage->pDirtyPrev || pPage==p->pDirty );
49987
49988 /* Update the PCache1.pSynced variable if necessary. */
49989 if( p->pSynced==pPage ){
49990 p->pSynced = pPage->pDirtyPrev;
49991 }
49992
49993 if( pPage->pDirtyNext ){
49994 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
49995 }else{
49996 assert( pPage==p->pDirtyTail );
49997 p->pDirtyTail = pPage->pDirtyPrev;
49998 }
49999 if( pPage->pDirtyPrev ){
50000 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
50001 }else{
50002 /* If there are now no dirty pages in the cache, set eCreate to 2.
50003 ** This is an optimization that allows sqlite3PcacheFetch() to skip
50004 ** searching for a dirty page to eject from the cache when it might
50005 ** otherwise have to. */
50006 assert( pPage==p->pDirty );
50007 p->pDirty = pPage->pDirtyNext;
50008 assert( p->bPurgeable || p->eCreate==2 );
50009 if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/
50010 assert( p->bPurgeable==0 || p->eCreate==1 );
50011 p->eCreate = 2;
50012 }
50013 }
50014 }
50015 if( addRemove & PCACHE_DIRTYLIST_ADD ){
50016 pPage->pDirtyPrev = 0;
50017 pPage->pDirtyNext = p->pDirty;
50018 if( pPage->pDirtyNext ){
50019 assert( pPage->pDirtyNext->pDirtyPrev==0 );
50020 pPage->pDirtyNext->pDirtyPrev = pPage;
50021 }else{
50022 p->pDirtyTail = pPage;
50023 if( p->bPurgeable ){
50024 assert( p->eCreate==2 );
50025 p->eCreate = 1;
50026 }
50027 }
50028 p->pDirty = pPage;
50029
50030 /* If pSynced is NULL and this page has a clear NEED_SYNC flag, set
50031 ** pSynced to point to it. Checking the NEED_SYNC flag is an
50032 ** optimization, as if pSynced points to a page with the NEED_SYNC
50033 ** flag set sqlite3PcacheFetchStress() searches through all newer
50034 ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */
50035 if( !p->pSynced
50036 && 0==(pPage->flags&PGHDR_NEED_SYNC) /*OPTIMIZATION-IF-FALSE*/
50037 ){
50038 p->pSynced = pPage;
50039 }
50040 }
50041 pcacheDump(p);
50042}
50043
50044/*
50045** Wrapper around the pluggable caches xUnpin method. If the cache is
50046** being used for an in-memory database, this function is a no-op.
50047*/
50048static void pcacheUnpin(PgHdr *p){
50049 if( p->pCache->bPurgeable ){
50050 pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
50051 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
50052 pcacheDump(p->pCache);
50053 }
50054}
50055
50056/*
50057** Compute the number of pages of cache requested. p->szCache is the
50058** cache size requested by the "PRAGMA cache_size" statement.
50059*/
50060static int numberOfCachePages(PCache *p){
50061 if( p->szCache>=0 ){
50062 /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
50063 ** suggested cache size is set to N. */
50064 return p->szCache;
50065 }else{
50066 i64 n;
50067 /* IMPLEMANTATION-OF: R-59858-46238 If the argument N is negative, then the
50068 ** number of cache pages is adjusted to be a number of pages that would
50069 ** use approximately abs(N*1024) bytes of memory based on the current
50070 ** page size. */
50071 n = ((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
50072 if( n>1000000000 ) n = 1000000000;
50073 return (int)n;
50074 }
50075}
50076
50077/*************************************************** General Interfaces ******
50078**
50079** Initialize and shutdown the page cache subsystem. Neither of these
50080** functions are threadsafe.
50081*/
50082SQLITE_PRIVATE int sqlite3PcacheInitialize(void){
50083 if( sqlite3GlobalConfig.pcache2.xInit==0 ){
50084 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
50085 ** built-in default page cache is used instead of the application defined
50086 ** page cache. */
50087 sqlite3PCacheSetDefault();
50088 assert( sqlite3GlobalConfig.pcache2.xInit!=0 );
50089 }
50090 return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg);
50091}
50092SQLITE_PRIVATE void sqlite3PcacheShutdown(void){
50093 if( sqlite3GlobalConfig.pcache2.xShutdown ){
50094 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
50095 sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg);
50096 }
50097}
50098
50099/*
50100** Return the size in bytes of a PCache object.
50101*/
50102SQLITE_PRIVATE int sqlite3PcacheSize(void){ return sizeof(PCache); }
50103
50104/*
50105** Create a new PCache object. Storage space to hold the object
50106** has already been allocated and is passed in as the p pointer.
50107** The caller discovers how much space needs to be allocated by
50108** calling sqlite3PcacheSize().
50109**
50110** szExtra is some extra space allocated for each page. The first
50111** 8 bytes of the extra space will be zeroed as the page is allocated,
50112** but remaining content will be uninitialized. Though it is opaque
50113** to this module, the extra space really ends up being the MemPage
50114** structure in the pager.
50115*/
50116SQLITE_PRIVATE int sqlite3PcacheOpen(
50117 int szPage, /* Size of every page */
50118 int szExtra, /* Extra space associated with each page */
50119 int bPurgeable, /* True if pages are on backing store */
50120 int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
50121 void *pStress, /* Argument to xStress */
50122 PCache *p /* Preallocated space for the PCache */
50123){
50124 memset(s: p, c: 0, n: sizeof(PCache));
50125 p->szPage = 1;
50126 p->szExtra = szExtra;
50127 assert( szExtra>=8 ); /* First 8 bytes will be zeroed */
50128 p->bPurgeable = bPurgeable;
50129 p->eCreate = 2;
50130 p->xStress = xStress;
50131 p->pStress = pStress;
50132 p->szCache = 100;
50133 p->szSpill = 1;
50134 pcacheTrace(("%p.OPEN szPage %d bPurgeable %d\n",p,szPage,bPurgeable));
50135 return sqlite3PcacheSetPageSize(p, szPage);
50136}
50137
50138/*
50139** Change the page size for PCache object. The caller must ensure that there
50140** are no outstanding page references when this function is called.
50141*/
50142SQLITE_PRIVATE int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
50143 assert( pCache->nRefSum==0 && pCache->pDirty==0 );
50144 if( pCache->szPage ){
50145 sqlite3_pcache *pNew;
50146 pNew = sqlite3GlobalConfig.pcache2.xCreate(
50147 szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),
50148 pCache->bPurgeable
50149 );
50150 if( pNew==0 ) return SQLITE_NOMEM_BKPT;
50151 sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(p: pCache));
50152 if( pCache->pCache ){
50153 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
50154 }
50155 pCache->pCache = pNew;
50156 pCache->szPage = szPage;
50157 pcacheTrace(("%p.PAGESIZE %d\n",pCache,szPage));
50158 }
50159 return SQLITE_OK;
50160}
50161
50162/*
50163** Try to obtain a page from the cache.
50164**
50165** This routine returns a pointer to an sqlite3_pcache_page object if
50166** such an object is already in cache, or if a new one is created.
50167** This routine returns a NULL pointer if the object was not in cache
50168** and could not be created.
50169**
50170** The createFlags should be 0 to check for existing pages and should
50171** be 3 (not 1, but 3) to try to create a new page.
50172**
50173** If the createFlag is 0, then NULL is always returned if the page
50174** is not already in the cache. If createFlag is 1, then a new page
50175** is created only if that can be done without spilling dirty pages
50176** and without exceeding the cache size limit.
50177**
50178** The caller needs to invoke sqlite3PcacheFetchFinish() to properly
50179** initialize the sqlite3_pcache_page object and convert it into a
50180** PgHdr object. The sqlite3PcacheFetch() and sqlite3PcacheFetchFinish()
50181** routines are split this way for performance reasons. When separated
50182** they can both (usually) operate without having to push values to
50183** the stack on entry and pop them back off on exit, which saves a
50184** lot of pushing and popping.
50185*/
50186SQLITE_PRIVATE sqlite3_pcache_page *sqlite3PcacheFetch(
50187 PCache *pCache, /* Obtain the page from this cache */
50188 Pgno pgno, /* Page number to obtain */
50189 int createFlag /* If true, create page if it does not exist already */
50190){
50191 int eCreate;
50192 sqlite3_pcache_page *pRes;
50193
50194 assert( pCache!=0 );
50195 assert( pCache->pCache!=0 );
50196 assert( createFlag==3 || createFlag==0 );
50197 assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );
50198
50199 /* eCreate defines what to do if the page does not exist.
50200 ** 0 Do not allocate a new page. (createFlag==0)
50201 ** 1 Allocate a new page if doing so is inexpensive.
50202 ** (createFlag==1 AND bPurgeable AND pDirty)
50203 ** 2 Allocate a new page even it doing so is difficult.
50204 ** (createFlag==1 AND !(bPurgeable AND pDirty)
50205 */
50206 eCreate = createFlag & pCache->eCreate;
50207 assert( eCreate==0 || eCreate==1 || eCreate==2 );
50208 assert( createFlag==0 || pCache->eCreate==eCreate );
50209 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
50210 pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
50211 pcacheTrace(("%p.FETCH %d%s (result: %p)\n",pCache,pgno,
50212 createFlag?" create":"",pRes));
50213 return pRes;
50214}
50215
50216/*
50217** If the sqlite3PcacheFetch() routine is unable to allocate a new
50218** page because no clean pages are available for reuse and the cache
50219** size limit has been reached, then this routine can be invoked to
50220** try harder to allocate a page. This routine might invoke the stress
50221** callback to spill dirty pages to the journal. It will then try to
50222** allocate the new page and will only fail to allocate a new page on
50223** an OOM error.
50224**
50225** This routine should be invoked only after sqlite3PcacheFetch() fails.
50226*/
50227SQLITE_PRIVATE int sqlite3PcacheFetchStress(
50228 PCache *pCache, /* Obtain the page from this cache */
50229 Pgno pgno, /* Page number to obtain */
50230 sqlite3_pcache_page **ppPage /* Write result here */
50231){
50232 PgHdr *pPg;
50233 if( pCache->eCreate==2 ) return 0;
50234
50235 if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
50236 /* Find a dirty page to write-out and recycle. First try to find a
50237 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
50238 ** cleared), but if that is not possible settle for any other
50239 ** unreferenced dirty page.
50240 **
50241 ** If the LRU page in the dirty list that has a clear PGHDR_NEED_SYNC
50242 ** flag is currently referenced, then the following may leave pSynced
50243 ** set incorrectly (pointing to other than the LRU page with NEED_SYNC
50244 ** cleared). This is Ok, as pSynced is just an optimization. */
50245 for(pPg=pCache->pSynced;
50246 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
50247 pPg=pPg->pDirtyPrev
50248 );
50249 pCache->pSynced = pPg;
50250 if( !pPg ){
50251 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
50252 }
50253 if( pPg ){
50254 int rc;
50255#ifdef SQLITE_LOG_CACHE_SPILL
50256 sqlite3_log(SQLITE_FULL,
50257 "spill page %d making room for %d - cache used: %d/%d",
50258 pPg->pgno, pgno,
50259 sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache),
50260 numberOfCachePages(pCache));
50261#endif
50262 pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
50263 rc = pCache->xStress(pCache->pStress, pPg);
50264 pcacheDump(pCache);
50265 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
50266 return rc;
50267 }
50268 }
50269 }
50270 *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
50271 return *ppPage==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK;
50272}
50273
50274/*
50275** This is a helper routine for sqlite3PcacheFetchFinish()
50276**
50277** In the uncommon case where the page being fetched has not been
50278** initialized, this routine is invoked to do the initialization.
50279** This routine is broken out into a separate function since it
50280** requires extra stack manipulation that can be avoided in the common
50281** case.
50282*/
50283static SQLITE_NOINLINE PgHdr *pcacheFetchFinishWithInit(
50284 PCache *pCache, /* Obtain the page from this cache */
50285 Pgno pgno, /* Page number obtained */
50286 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
50287){
50288 PgHdr *pPgHdr;
50289 assert( pPage!=0 );
50290 pPgHdr = (PgHdr*)pPage->pExtra;
50291 assert( pPgHdr->pPage==0 );
50292 memset(s: &pPgHdr->pDirty, c: 0, n: sizeof(PgHdr) - offsetof(PgHdr,pDirty));
50293 pPgHdr->pPage = pPage;
50294 pPgHdr->pData = pPage->pBuf;
50295 pPgHdr->pExtra = (void *)&pPgHdr[1];
50296 memset(s: pPgHdr->pExtra, c: 0, n: 8);
50297 pPgHdr->pCache = pCache;
50298 pPgHdr->pgno = pgno;
50299 pPgHdr->flags = PGHDR_CLEAN;
50300 return sqlite3PcacheFetchFinish(pCache,pgno,pPage);
50301}
50302
50303/*
50304** This routine converts the sqlite3_pcache_page object returned by
50305** sqlite3PcacheFetch() into an initialized PgHdr object. This routine
50306** must be called after sqlite3PcacheFetch() in order to get a usable
50307** result.
50308*/
50309SQLITE_PRIVATE PgHdr *sqlite3PcacheFetchFinish(
50310 PCache *pCache, /* Obtain the page from this cache */
50311 Pgno pgno, /* Page number obtained */
50312 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
50313){
50314 PgHdr *pPgHdr;
50315
50316 assert( pPage!=0 );
50317 pPgHdr = (PgHdr *)pPage->pExtra;
50318
50319 if( !pPgHdr->pPage ){
50320 return pcacheFetchFinishWithInit(pCache, pgno, pPage);
50321 }
50322 pCache->nRefSum++;
50323 pPgHdr->nRef++;
50324 assert( sqlite3PcachePageSanity(pPgHdr) );
50325 return pPgHdr;
50326}
50327
50328/*
50329** Decrement the reference count on a page. If the page is clean and the
50330** reference count drops to 0, then it is made eligible for recycling.
50331*/
50332SQLITE_PRIVATE void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){
50333 assert( p->nRef>0 );
50334 p->pCache->nRefSum--;
50335 if( (--p->nRef)==0 ){
50336 if( p->flags&PGHDR_CLEAN ){
50337 pcacheUnpin(p);
50338 }else{
50339 pcacheManageDirtyList(pPage: p, PCACHE_DIRTYLIST_FRONT);
50340 }
50341 }
50342}
50343
50344/*
50345** Increase the reference count of a supplied page by 1.
50346*/
50347SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){
50348 assert(p->nRef>0);
50349 assert( sqlite3PcachePageSanity(p) );
50350 p->nRef++;
50351 p->pCache->nRefSum++;
50352}
50353
50354/*
50355** Drop a page from the cache. There must be exactly one reference to the
50356** page. This function deletes that reference, so after it returns the
50357** page pointed to by p is invalid.
50358*/
50359SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){
50360 assert( p->nRef==1 );
50361 assert( sqlite3PcachePageSanity(p) );
50362 if( p->flags&PGHDR_DIRTY ){
50363 pcacheManageDirtyList(pPage: p, PCACHE_DIRTYLIST_REMOVE);
50364 }
50365 p->pCache->nRefSum--;
50366 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
50367}
50368
50369/*
50370** Make sure the page is marked as dirty. If it isn't dirty already,
50371** make it so.
50372*/
50373SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
50374 assert( p->nRef>0 );
50375 assert( sqlite3PcachePageSanity(p) );
50376 if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ /*OPTIMIZATION-IF-FALSE*/
50377 p->flags &= ~PGHDR_DONT_WRITE;
50378 if( p->flags & PGHDR_CLEAN ){
50379 p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
50380 pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
50381 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
50382 pcacheManageDirtyList(pPage: p, PCACHE_DIRTYLIST_ADD);
50383 }
50384 assert( sqlite3PcachePageSanity(p) );
50385 }
50386}
50387
50388/*
50389** Make sure the page is marked as clean. If it isn't clean already,
50390** make it so.
50391*/
50392SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){
50393 assert( sqlite3PcachePageSanity(p) );
50394 assert( (p->flags & PGHDR_DIRTY)!=0 );
50395 assert( (p->flags & PGHDR_CLEAN)==0 );
50396 pcacheManageDirtyList(pPage: p, PCACHE_DIRTYLIST_REMOVE);
50397 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
50398 p->flags |= PGHDR_CLEAN;
50399 pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
50400 assert( sqlite3PcachePageSanity(p) );
50401 if( p->nRef==0 ){
50402 pcacheUnpin(p);
50403 }
50404}
50405
50406/*
50407** Make every page in the cache clean.
50408*/
50409SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){
50410 PgHdr *p;
50411 pcacheTrace(("%p.CLEAN-ALL\n",pCache));
50412 while( (p = pCache->pDirty)!=0 ){
50413 sqlite3PcacheMakeClean(p);
50414 }
50415}
50416
50417/*
50418** Clear the PGHDR_NEED_SYNC and PGHDR_WRITEABLE flag from all dirty pages.
50419*/
50420SQLITE_PRIVATE void sqlite3PcacheClearWritable(PCache *pCache){
50421 PgHdr *p;
50422 pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
50423 for(p=pCache->pDirty; p; p=p->pDirtyNext){
50424 p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
50425 }
50426 pCache->pSynced = pCache->pDirtyTail;
50427}
50428
50429/*
50430** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
50431*/
50432SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *pCache){
50433 PgHdr *p;
50434 for(p=pCache->pDirty; p; p=p->pDirtyNext){
50435 p->flags &= ~PGHDR_NEED_SYNC;
50436 }
50437 pCache->pSynced = pCache->pDirtyTail;
50438}
50439
50440/*
50441** Change the page number of page p to newPgno.
50442*/
50443SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
50444 PCache *pCache = p->pCache;
50445 assert( p->nRef>0 );
50446 assert( newPgno>0 );
50447 assert( sqlite3PcachePageSanity(p) );
50448 pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
50449 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
50450 p->pgno = newPgno;
50451 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
50452 pcacheManageDirtyList(pPage: p, PCACHE_DIRTYLIST_FRONT);
50453 }
50454}
50455
50456/*
50457** Drop every cache entry whose page number is greater than "pgno". The
50458** caller must ensure that there are no outstanding references to any pages
50459** other than page 1 with a page number greater than pgno.
50460**
50461** If there is a reference to page 1 and the pgno parameter passed to this
50462** function is 0, then the data area associated with page 1 is zeroed, but
50463** the page object is not dropped.
50464*/
50465SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
50466 if( pCache->pCache ){
50467 PgHdr *p;
50468 PgHdr *pNext;
50469 pcacheTrace(("%p.TRUNCATE %d\n",pCache,pgno));
50470 for(p=pCache->pDirty; p; p=pNext){
50471 pNext = p->pDirtyNext;
50472 /* This routine never gets call with a positive pgno except right
50473 ** after sqlite3PcacheCleanAll(). So if there are dirty pages,
50474 ** it must be that pgno==0.
50475 */
50476 assert( p->pgno>0 );
50477 if( p->pgno>pgno ){
50478 assert( p->flags&PGHDR_DIRTY );
50479 sqlite3PcacheMakeClean(p);
50480 }
50481 }
50482 if( pgno==0 && pCache->nRefSum ){
50483 sqlite3_pcache_page *pPage1;
50484 pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
50485 if( ALWAYS(pPage1) ){ /* Page 1 is always available in cache, because
50486 ** pCache->nRefSum>0 */
50487 memset(s: pPage1->pBuf, c: 0, n: pCache->szPage);
50488 pgno = 1;
50489 }
50490 }
50491 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
50492 }
50493}
50494
50495/*
50496** Close a cache.
50497*/
50498SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){
50499 assert( pCache->pCache!=0 );
50500 pcacheTrace(("%p.CLOSE\n",pCache));
50501 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
50502}
50503
50504/*
50505** Discard the contents of the cache.
50506*/
50507SQLITE_PRIVATE void sqlite3PcacheClear(PCache *pCache){
50508 sqlite3PcacheTruncate(pCache, pgno: 0);
50509}
50510
50511/*
50512** Merge two lists of pages connected by pDirty and in pgno order.
50513** Do not bother fixing the pDirtyPrev pointers.
50514*/
50515static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
50516 PgHdr result, *pTail;
50517 pTail = &result;
50518 assert( pA!=0 && pB!=0 );
50519 for(;;){
50520 if( pA->pgno<pB->pgno ){
50521 pTail->pDirty = pA;
50522 pTail = pA;
50523 pA = pA->pDirty;
50524 if( pA==0 ){
50525 pTail->pDirty = pB;
50526 break;
50527 }
50528 }else{
50529 pTail->pDirty = pB;
50530 pTail = pB;
50531 pB = pB->pDirty;
50532 if( pB==0 ){
50533 pTail->pDirty = pA;
50534 break;
50535 }
50536 }
50537 }
50538 return result.pDirty;
50539}
50540
50541/*
50542** Sort the list of pages in accending order by pgno. Pages are
50543** connected by pDirty pointers. The pDirtyPrev pointers are
50544** corrupted by this sort.
50545**
50546** Since there cannot be more than 2^31 distinct pages in a database,
50547** there cannot be more than 31 buckets required by the merge sorter.
50548** One extra bucket is added to catch overflow in case something
50549** ever changes to make the previous sentence incorrect.
50550*/
50551#define N_SORT_BUCKET 32
50552static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
50553 PgHdr *a[N_SORT_BUCKET], *p;
50554 int i;
50555 memset(s: a, c: 0, n: sizeof(a));
50556 while( pIn ){
50557 p = pIn;
50558 pIn = p->pDirty;
50559 p->pDirty = 0;
50560 for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
50561 if( a[i]==0 ){
50562 a[i] = p;
50563 break;
50564 }else{
50565 p = pcacheMergeDirtyList(pA: a[i], pB: p);
50566 a[i] = 0;
50567 }
50568 }
50569 if( NEVER(i==N_SORT_BUCKET-1) ){
50570 /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
50571 ** the input list. But that is impossible.
50572 */
50573 a[i] = pcacheMergeDirtyList(pA: a[i], pB: p);
50574 }
50575 }
50576 p = a[0];
50577 for(i=1; i<N_SORT_BUCKET; i++){
50578 if( a[i]==0 ) continue;
50579 p = p ? pcacheMergeDirtyList(pA: p, pB: a[i]) : a[i];
50580 }
50581 return p;
50582}
50583
50584/*
50585** Return a list of all dirty pages in the cache, sorted by page number.
50586*/
50587SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
50588 PgHdr *p;
50589 for(p=pCache->pDirty; p; p=p->pDirtyNext){
50590 p->pDirty = p->pDirtyNext;
50591 }
50592 return pcacheSortDirtyList(pIn: pCache->pDirty);
50593}
50594
50595/*
50596** Return the total number of references to all pages held by the cache.
50597**
50598** This is not the total number of pages referenced, but the sum of the
50599** reference count for all pages.
50600*/
50601SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache *pCache){
50602 return pCache->nRefSum;
50603}
50604
50605/*
50606** Return the number of references to the page supplied as an argument.
50607*/
50608SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr *p){
50609 return p->nRef;
50610}
50611
50612/*
50613** Return the total number of pages in the cache.
50614*/
50615SQLITE_PRIVATE int sqlite3PcachePagecount(PCache *pCache){
50616 assert( pCache->pCache!=0 );
50617 return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
50618}
50619
50620#ifdef SQLITE_TEST
50621/*
50622** Get the suggested cache-size value.
50623*/
50624SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){
50625 return numberOfCachePages(pCache);
50626}
50627#endif
50628
50629/*
50630** Set the suggested cache-size value.
50631*/
50632SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
50633 assert( pCache->pCache!=0 );
50634 pCache->szCache = mxPage;
50635 sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
50636 numberOfCachePages(p: pCache));
50637}
50638
50639/*
50640** Set the suggested cache-spill value. Make no changes if if the
50641** argument is zero. Return the effective cache-spill size, which will
50642** be the larger of the szSpill and szCache.
50643*/
50644SQLITE_PRIVATE int sqlite3PcacheSetSpillsize(PCache *p, int mxPage){
50645 int res;
50646 assert( p->pCache!=0 );
50647 if( mxPage ){
50648 if( mxPage<0 ){
50649 mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
50650 }
50651 p->szSpill = mxPage;
50652 }
50653 res = numberOfCachePages(p);
50654 if( res<p->szSpill ) res = p->szSpill;
50655 return res;
50656}
50657
50658/*
50659** Free up as much memory as possible from the page cache.
50660*/
50661SQLITE_PRIVATE void sqlite3PcacheShrink(PCache *pCache){
50662 assert( pCache->pCache!=0 );
50663 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
50664}
50665
50666/*
50667** Return the size of the header added by this middleware layer
50668** in the page-cache hierarchy.
50669*/
50670SQLITE_PRIVATE int sqlite3HeaderSizePcache(void){ return ROUND8(sizeof(PgHdr)); }
50671
50672/*
50673** Return the number of dirty pages currently in the cache, as a percentage
50674** of the configured cache size.
50675*/
50676SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache *pCache){
50677 PgHdr *pDirty;
50678 int nDirty = 0;
50679 int nCache = numberOfCachePages(p: pCache);
50680 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
50681 return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0;
50682}
50683
50684#ifdef SQLITE_DIRECT_OVERFLOW_READ
50685/*
50686** Return true if there are one or more dirty pages in the cache. Else false.
50687*/
50688SQLITE_PRIVATE int sqlite3PCacheIsDirty(PCache *pCache){
50689 return (pCache->pDirty!=0);
50690}
50691#endif
50692
50693#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
50694/*
50695** For all dirty pages currently in the cache, invoke the specified
50696** callback. This is only used if the SQLITE_CHECK_PAGES macro is
50697** defined.
50698*/
50699SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
50700 PgHdr *pDirty;
50701 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
50702 xIter(pDirty);
50703 }
50704}
50705#endif
50706
50707/************** End of pcache.c **********************************************/
50708/************** Begin file pcache1.c *****************************************/
50709/*
50710** 2008 November 05
50711**
50712** The author disclaims copyright to this source code. In place of
50713** a legal notice, here is a blessing:
50714**
50715** May you do good and not evil.
50716** May you find forgiveness for yourself and forgive others.
50717** May you share freely, never taking more than you give.
50718**
50719*************************************************************************
50720**
50721** This file implements the default page cache implementation (the
50722** sqlite3_pcache interface). It also contains part of the implementation
50723** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
50724** If the default page cache implementation is overridden, then neither of
50725** these two features are available.
50726**
50727** A Page cache line looks like this:
50728**
50729** -------------------------------------------------------------
50730** | database page content | PgHdr1 | MemPage | PgHdr |
50731** -------------------------------------------------------------
50732**
50733** The database page content is up front (so that buffer overreads tend to
50734** flow harmlessly into the PgHdr1, MemPage, and PgHdr extensions). MemPage
50735** is the extension added by the btree.c module containing information such
50736** as the database page number and how that database page is used. PgHdr
50737** is added by the pcache.c layer and contains information used to keep track
50738** of which pages are "dirty". PgHdr1 is an extension added by this
50739** module (pcache1.c). The PgHdr1 header is a subclass of sqlite3_pcache_page.
50740** PgHdr1 contains information needed to look up a page by its page number.
50741** The superclass sqlite3_pcache_page.pBuf points to the start of the
50742** database page content and sqlite3_pcache_page.pExtra points to PgHdr.
50743**
50744** The size of the extension (MemPage+PgHdr+PgHdr1) can be determined at
50745** runtime using sqlite3_config(SQLITE_CONFIG_PCACHE_HDRSZ, &size). The
50746** sizes of the extensions sum to 272 bytes on x64 for 3.8.10, but this
50747** size can vary according to architecture, compile-time options, and
50748** SQLite library version number.
50749**
50750** If SQLITE_PCACHE_SEPARATE_HEADER is defined, then the extension is obtained
50751** using a separate memory allocation from the database page content. This
50752** seeks to overcome the "clownshoe" problem (also called "internal
50753** fragmentation" in academic literature) of allocating a few bytes more
50754** than a power of two with the memory allocator rounding up to the next
50755** power of two, and leaving the rounded-up space unused.
50756**
50757** This module tracks pointers to PgHdr1 objects. Only pcache.c communicates
50758** with this module. Information is passed back and forth as PgHdr1 pointers.
50759**
50760** The pcache.c and pager.c modules deal pointers to PgHdr objects.
50761** The btree.c module deals with pointers to MemPage objects.
50762**
50763** SOURCE OF PAGE CACHE MEMORY:
50764**
50765** Memory for a page might come from any of three sources:
50766**
50767** (1) The general-purpose memory allocator - sqlite3Malloc()
50768** (2) Global page-cache memory provided using sqlite3_config() with
50769** SQLITE_CONFIG_PAGECACHE.
50770** (3) PCache-local bulk allocation.
50771**
50772** The third case is a chunk of heap memory (defaulting to 100 pages worth)
50773** that is allocated when the page cache is created. The size of the local
50774** bulk allocation can be adjusted using
50775**
50776** sqlite3_config(SQLITE_CONFIG_PAGECACHE, (void*)0, 0, N).
50777**
50778** If N is positive, then N pages worth of memory are allocated using a single
50779** sqlite3Malloc() call and that memory is used for the first N pages allocated.
50780** Or if N is negative, then -1024*N bytes of memory are allocated and used
50781** for as many pages as can be accomodated.
50782**
50783** Only one of (2) or (3) can be used. Once the memory available to (2) or
50784** (3) is exhausted, subsequent allocations fail over to the general-purpose
50785** memory allocator (1).
50786**
50787** Earlier versions of SQLite used only methods (1) and (2). But experiments
50788** show that method (3) with N==100 provides about a 5% performance boost for
50789** common workloads.
50790*/
50791/* #include "sqliteInt.h" */
50792
50793typedef struct PCache1 PCache1;
50794typedef struct PgHdr1 PgHdr1;
50795typedef struct PgFreeslot PgFreeslot;
50796typedef struct PGroup PGroup;
50797
50798/*
50799** Each cache entry is represented by an instance of the following
50800** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of
50801** PgHdr1.pCache->szPage bytes is allocated directly before this structure
50802** in memory.
50803**
50804** Note: Variables isBulkLocal and isAnchor were once type "u8". That works,
50805** but causes a 2-byte gap in the structure for most architectures (since
50806** pointers must be either 4 or 8-byte aligned). As this structure is located
50807** in memory directly after the associated page data, if the database is
50808** corrupt, code at the b-tree layer may overread the page buffer and
50809** read part of this structure before the corruption is detected. This
50810** can cause a valgrind error if the unitialized gap is accessed. Using u16
50811** ensures there is no such gap, and therefore no bytes of unitialized memory
50812** in the structure.
50813*/
50814struct PgHdr1 {
50815 sqlite3_pcache_page page; /* Base class. Must be first. pBuf & pExtra */
50816 unsigned int iKey; /* Key value (page number) */
50817 u16 isBulkLocal; /* This page from bulk local storage */
50818 u16 isAnchor; /* This is the PGroup.lru element */
50819 PgHdr1 *pNext; /* Next in hash table chain */
50820 PCache1 *pCache; /* Cache that currently owns this page */
50821 PgHdr1 *pLruNext; /* Next in LRU list of unpinned pages */
50822 PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */
50823 /* NB: pLruPrev is only valid if pLruNext!=0 */
50824};
50825
50826/*
50827** A page is pinned if it is not on the LRU list. To be "pinned" means
50828** that the page is in active use and must not be deallocated.
50829*/
50830#define PAGE_IS_PINNED(p) ((p)->pLruNext==0)
50831#define PAGE_IS_UNPINNED(p) ((p)->pLruNext!=0)
50832
50833/* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
50834** of one or more PCaches that are able to recycle each other's unpinned
50835** pages when they are under memory pressure. A PGroup is an instance of
50836** the following object.
50837**
50838** This page cache implementation works in one of two modes:
50839**
50840** (1) Every PCache is the sole member of its own PGroup. There is
50841** one PGroup per PCache.
50842**
50843** (2) There is a single global PGroup that all PCaches are a member
50844** of.
50845**
50846** Mode 1 uses more memory (since PCache instances are not able to rob
50847** unused pages from other PCaches) but it also operates without a mutex,
50848** and is therefore often faster. Mode 2 requires a mutex in order to be
50849** threadsafe, but recycles pages more efficiently.
50850**
50851** For mode (1), PGroup.mutex is NULL. For mode (2) there is only a single
50852** PGroup which is the pcache1.grp global variable and its mutex is
50853** SQLITE_MUTEX_STATIC_LRU.
50854*/
50855struct PGroup {
50856 sqlite3_mutex *mutex; /* MUTEX_STATIC_LRU or NULL */
50857 unsigned int nMaxPage; /* Sum of nMax for purgeable caches */
50858 unsigned int nMinPage; /* Sum of nMin for purgeable caches */
50859 unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */
50860 unsigned int nPurgeable; /* Number of purgeable pages allocated */
50861 PgHdr1 lru; /* The beginning and end of the LRU list */
50862};
50863
50864/* Each page cache is an instance of the following object. Every
50865** open database file (including each in-memory database and each
50866** temporary or transient database) has a single page cache which
50867** is an instance of this object.
50868**
50869** Pointers to structures of this type are cast and returned as
50870** opaque sqlite3_pcache* handles.
50871*/
50872struct PCache1 {
50873 /* Cache configuration parameters. Page size (szPage) and the purgeable
50874 ** flag (bPurgeable) and the pnPurgeable pointer are all set when the
50875 ** cache is created and are never changed thereafter. nMax may be
50876 ** modified at any time by a call to the pcache1Cachesize() method.
50877 ** The PGroup mutex must be held when accessing nMax.
50878 */
50879 PGroup *pGroup; /* PGroup this cache belongs to */
50880 unsigned int *pnPurgeable; /* Pointer to pGroup->nPurgeable */
50881 int szPage; /* Size of database content section */
50882 int szExtra; /* sizeof(MemPage)+sizeof(PgHdr) */
50883 int szAlloc; /* Total size of one pcache line */
50884 int bPurgeable; /* True if cache is purgeable */
50885 unsigned int nMin; /* Minimum number of pages reserved */
50886 unsigned int nMax; /* Configured "cache_size" value */
50887 unsigned int n90pct; /* nMax*9/10 */
50888 unsigned int iMaxKey; /* Largest key seen since xTruncate() */
50889 unsigned int nPurgeableDummy; /* pnPurgeable points here when not used*/
50890
50891 /* Hash table of all pages. The following variables may only be accessed
50892 ** when the accessor is holding the PGroup mutex.
50893 */
50894 unsigned int nRecyclable; /* Number of pages in the LRU list */
50895 unsigned int nPage; /* Total number of pages in apHash */
50896 unsigned int nHash; /* Number of slots in apHash[] */
50897 PgHdr1 **apHash; /* Hash table for fast lookup by key */
50898 PgHdr1 *pFree; /* List of unused pcache-local pages */
50899 void *pBulk; /* Bulk memory used by pcache-local */
50900};
50901
50902/*
50903** Free slots in the allocator used to divide up the global page cache
50904** buffer provided using the SQLITE_CONFIG_PAGECACHE mechanism.
50905*/
50906struct PgFreeslot {
50907 PgFreeslot *pNext; /* Next free slot */
50908};
50909
50910/*
50911** Global data used by this cache.
50912*/
50913static SQLITE_WSD struct PCacheGlobal {
50914 PGroup grp; /* The global PGroup for mode (2) */
50915
50916 /* Variables related to SQLITE_CONFIG_PAGECACHE settings. The
50917 ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
50918 ** fixed at sqlite3_initialize() time and do not require mutex protection.
50919 ** The nFreeSlot and pFree values do require mutex protection.
50920 */
50921 int isInit; /* True if initialized */
50922 int separateCache; /* Use a new PGroup for each PCache */
50923 int nInitPage; /* Initial bulk allocation size */
50924 int szSlot; /* Size of each free slot */
50925 int nSlot; /* The number of pcache slots */
50926 int nReserve; /* Try to keep nFreeSlot above this */
50927 void *pStart, *pEnd; /* Bounds of global page cache memory */
50928 /* Above requires no mutex. Use mutex below for variable that follow. */
50929 sqlite3_mutex *mutex; /* Mutex for accessing the following: */
50930 PgFreeslot *pFree; /* Free page blocks */
50931 int nFreeSlot; /* Number of unused pcache slots */
50932 /* The following value requires a mutex to change. We skip the mutex on
50933 ** reading because (1) most platforms read a 32-bit integer atomically and
50934 ** (2) even if an incorrect value is read, no great harm is done since this
50935 ** is really just an optimization. */
50936 int bUnderPressure; /* True if low on PAGECACHE memory */
50937} pcache1_g;
50938
50939/*
50940** All code in this file should access the global structure above via the
50941** alias "pcache1". This ensures that the WSD emulation is used when
50942** compiling for systems that do not support real WSD.
50943*/
50944#define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
50945
50946/*
50947** Macros to enter and leave the PCache LRU mutex.
50948*/
50949#if !defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
50950# define pcache1EnterMutex(X) assert((X)->mutex==0)
50951# define pcache1LeaveMutex(X) assert((X)->mutex==0)
50952# define PCACHE1_MIGHT_USE_GROUP_MUTEX 0
50953#else
50954# define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
50955# define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
50956# define PCACHE1_MIGHT_USE_GROUP_MUTEX 1
50957#endif
50958
50959/******************************************************************************/
50960/******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
50961
50962
50963/*
50964** This function is called during initialization if a static buffer is
50965** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
50966** verb to sqlite3_config(). Parameter pBuf points to an allocation large
50967** enough to contain 'n' buffers of 'sz' bytes each.
50968**
50969** This routine is called from sqlite3_initialize() and so it is guaranteed
50970** to be serialized already. There is no need for further mutexing.
50971*/
50972SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
50973 if( pcache1.isInit ){
50974 PgFreeslot *p;
50975 if( pBuf==0 ) sz = n = 0;
50976 if( n==0 ) sz = 0;
50977 sz = ROUNDDOWN8(sz);
50978 pcache1.szSlot = sz;
50979 pcache1.nSlot = pcache1.nFreeSlot = n;
50980 pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
50981 pcache1.pStart = pBuf;
50982 pcache1.pFree = 0;
50983 pcache1.bUnderPressure = 0;
50984 while( n-- ){
50985 p = (PgFreeslot*)pBuf;
50986 p->pNext = pcache1.pFree;
50987 pcache1.pFree = p;
50988 pBuf = (void*)&((char*)pBuf)[sz];
50989 }
50990 pcache1.pEnd = pBuf;
50991 }
50992}
50993
50994/*
50995** Try to initialize the pCache->pFree and pCache->pBulk fields. Return
50996** true if pCache->pFree ends up containing one or more free pages.
50997*/
50998static int pcache1InitBulk(PCache1 *pCache){
50999 i64 szBulk;
51000 char *zBulk;
51001 if( pcache1.nInitPage==0 ) return 0;
51002 /* Do not bother with a bulk allocation if the cache size very small */
51003 if( pCache->nMax<3 ) return 0;
51004 sqlite3BeginBenignMalloc();
51005 if( pcache1.nInitPage>0 ){
51006 szBulk = pCache->szAlloc * (i64)pcache1.nInitPage;
51007 }else{
51008 szBulk = -1024 * (i64)pcache1.nInitPage;
51009 }
51010 if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
51011 szBulk = pCache->szAlloc*(i64)pCache->nMax;
51012 }
51013 zBulk = pCache->pBulk = sqlite3Malloc( n: szBulk );
51014 sqlite3EndBenignMalloc();
51015 if( zBulk ){
51016 int nBulk = sqlite3MallocSize(p: zBulk)/pCache->szAlloc;
51017 do{
51018 PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
51019 pX->page.pBuf = zBulk;
51020 pX->page.pExtra = &pX[1];
51021 pX->isBulkLocal = 1;
51022 pX->isAnchor = 0;
51023 pX->pNext = pCache->pFree;
51024 pX->pLruPrev = 0; /* Initializing this saves a valgrind error */
51025 pCache->pFree = pX;
51026 zBulk += pCache->szAlloc;
51027 }while( --nBulk );
51028 }
51029 return pCache->pFree!=0;
51030}
51031
51032/*
51033** Malloc function used within this file to allocate space from the buffer
51034** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
51035** such buffer exists or there is no space left in it, this function falls
51036** back to sqlite3Malloc().
51037**
51038** Multiple threads can run this routine at the same time. Global variables
51039** in pcache1 need to be protected via mutex.
51040*/
51041static void *pcache1Alloc(int nByte){
51042 void *p = 0;
51043 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
51044 if( nByte<=pcache1.szSlot ){
51045 sqlite3_mutex_enter(pcache1.mutex);
51046 p = (PgHdr1 *)pcache1.pFree;
51047 if( p ){
51048 pcache1.pFree = pcache1.pFree->pNext;
51049 pcache1.nFreeSlot--;
51050 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
51051 assert( pcache1.nFreeSlot>=0 );
51052 sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE, X: nByte);
51053 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_USED, N: 1);
51054 }
51055 sqlite3_mutex_leave(pcache1.mutex);
51056 }
51057 if( p==0 ){
51058 /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool. Get
51059 ** it from sqlite3Malloc instead.
51060 */
51061 p = sqlite3Malloc(n: nByte);
51062#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
51063 if( p ){
51064 int sz = sqlite3MallocSize(p);
51065 sqlite3_mutex_enter(pcache1.mutex);
51066 sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE, X: nByte);
51067 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_OVERFLOW, N: sz);
51068 sqlite3_mutex_leave(pcache1.mutex);
51069 }
51070#endif
51071 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
51072 }
51073 return p;
51074}
51075
51076/*
51077** Free an allocated buffer obtained from pcache1Alloc().
51078*/
51079static void pcache1Free(void *p){
51080 if( p==0 ) return;
51081 if( SQLITE_WITHIN(p, pcache1.pStart, pcache1.pEnd) ){
51082 PgFreeslot *pSlot;
51083 sqlite3_mutex_enter(pcache1.mutex);
51084 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_USED, N: 1);
51085 pSlot = (PgFreeslot*)p;
51086 pSlot->pNext = pcache1.pFree;
51087 pcache1.pFree = pSlot;
51088 pcache1.nFreeSlot++;
51089 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
51090 assert( pcache1.nFreeSlot<=pcache1.nSlot );
51091 sqlite3_mutex_leave(pcache1.mutex);
51092 }else{
51093 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
51094 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
51095#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
51096 {
51097 int nFreed = 0;
51098 nFreed = sqlite3MallocSize(p);
51099 sqlite3_mutex_enter(pcache1.mutex);
51100 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW, N: nFreed);
51101 sqlite3_mutex_leave(pcache1.mutex);
51102 }
51103#endif
51104 sqlite3_free(p);
51105 }
51106}
51107
51108#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
51109/*
51110** Return the size of a pcache allocation
51111*/
51112static int pcache1MemSize(void *p){
51113 if( p>=pcache1.pStart && p<pcache1.pEnd ){
51114 return pcache1.szSlot;
51115 }else{
51116 int iSize;
51117 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
51118 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
51119 iSize = sqlite3MallocSize(p);
51120 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
51121 return iSize;
51122 }
51123}
51124#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
51125
51126/*
51127** Allocate a new page object initially associated with cache pCache.
51128*/
51129static PgHdr1 *pcache1AllocPage(PCache1 *pCache, int benignMalloc){
51130 PgHdr1 *p = 0;
51131 void *pPg;
51132
51133 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
51134 if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){
51135 assert( pCache->pFree!=0 );
51136 p = pCache->pFree;
51137 pCache->pFree = p->pNext;
51138 p->pNext = 0;
51139 }else{
51140#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
51141 /* The group mutex must be released before pcache1Alloc() is called. This
51142 ** is because it might call sqlite3_release_memory(), which assumes that
51143 ** this mutex is not held. */
51144 assert( pcache1.separateCache==0 );
51145 assert( pCache->pGroup==&pcache1.grp );
51146 pcache1LeaveMutex(pCache->pGroup);
51147#endif
51148 if( benignMalloc ){ sqlite3BeginBenignMalloc(); }
51149#ifdef SQLITE_PCACHE_SEPARATE_HEADER
51150 pPg = pcache1Alloc(pCache->szPage);
51151 p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
51152 if( !pPg || !p ){
51153 pcache1Free(pPg);
51154 sqlite3_free(p);
51155 pPg = 0;
51156 }
51157#else
51158 pPg = pcache1Alloc(nByte: pCache->szAlloc);
51159#endif
51160 if( benignMalloc ){ sqlite3EndBenignMalloc(); }
51161#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
51162 pcache1EnterMutex(pCache->pGroup);
51163#endif
51164 if( pPg==0 ) return 0;
51165#ifndef SQLITE_PCACHE_SEPARATE_HEADER
51166 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
51167#endif
51168 p->page.pBuf = pPg;
51169 p->page.pExtra = &p[1];
51170 p->isBulkLocal = 0;
51171 p->isAnchor = 0;
51172 p->pLruPrev = 0; /* Initializing this saves a valgrind error */
51173 }
51174 (*pCache->pnPurgeable)++;
51175 return p;
51176}
51177
51178/*
51179** Free a page object allocated by pcache1AllocPage().
51180*/
51181static void pcache1FreePage(PgHdr1 *p){
51182 PCache1 *pCache;
51183 assert( p!=0 );
51184 pCache = p->pCache;
51185 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
51186 if( p->isBulkLocal ){
51187 p->pNext = pCache->pFree;
51188 pCache->pFree = p;
51189 }else{
51190 pcache1Free(p: p->page.pBuf);
51191#ifdef SQLITE_PCACHE_SEPARATE_HEADER
51192 sqlite3_free(p);
51193#endif
51194 }
51195 (*pCache->pnPurgeable)--;
51196}
51197
51198/*
51199** Malloc function used by SQLite to obtain space from the buffer configured
51200** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
51201** exists, this function falls back to sqlite3Malloc().
51202*/
51203SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){
51204 assert( sz<=65536+8 ); /* These allocations are never very large */
51205 return pcache1Alloc(nByte: sz);
51206}
51207
51208/*
51209** Free an allocated buffer obtained from sqlite3PageMalloc().
51210*/
51211SQLITE_PRIVATE void sqlite3PageFree(void *p){
51212 pcache1Free(p);
51213}
51214
51215
51216/*
51217** Return true if it desirable to avoid allocating a new page cache
51218** entry.
51219**
51220** If memory was allocated specifically to the page cache using
51221** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
51222** it is desirable to avoid allocating a new page cache entry because
51223** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
51224** for all page cache needs and we should not need to spill the
51225** allocation onto the heap.
51226**
51227** Or, the heap is used for all page cache memory but the heap is
51228** under memory pressure, then again it is desirable to avoid
51229** allocating a new page cache entry in order to avoid stressing
51230** the heap even further.
51231*/
51232static int pcache1UnderMemoryPressure(PCache1 *pCache){
51233 if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
51234 return pcache1.bUnderPressure;
51235 }else{
51236 return sqlite3HeapNearlyFull();
51237 }
51238}
51239
51240/******************************************************************************/
51241/******** General Implementation Functions ************************************/
51242
51243/*
51244** This function is used to resize the hash table used by the cache passed
51245** as the first argument.
51246**
51247** The PCache mutex must be held when this function is called.
51248*/
51249static void pcache1ResizeHash(PCache1 *p){
51250 PgHdr1 **apNew;
51251 unsigned int nNew;
51252 unsigned int i;
51253
51254 assert( sqlite3_mutex_held(p->pGroup->mutex) );
51255
51256 nNew = p->nHash*2;
51257 if( nNew<256 ){
51258 nNew = 256;
51259 }
51260
51261 pcache1LeaveMutex(p->pGroup);
51262 if( p->nHash ){ sqlite3BeginBenignMalloc(); }
51263 apNew = (PgHdr1 **)sqlite3MallocZero(n: sizeof(PgHdr1 *)*nNew);
51264 if( p->nHash ){ sqlite3EndBenignMalloc(); }
51265 pcache1EnterMutex(p->pGroup);
51266 if( apNew ){
51267 for(i=0; i<p->nHash; i++){
51268 PgHdr1 *pPage;
51269 PgHdr1 *pNext = p->apHash[i];
51270 while( (pPage = pNext)!=0 ){
51271 unsigned int h = pPage->iKey % nNew;
51272 pNext = pPage->pNext;
51273 pPage->pNext = apNew[h];
51274 apNew[h] = pPage;
51275 }
51276 }
51277 sqlite3_free(p: p->apHash);
51278 p->apHash = apNew;
51279 p->nHash = nNew;
51280 }
51281}
51282
51283/*
51284** This function is used internally to remove the page pPage from the
51285** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
51286** LRU list, then this function is a no-op.
51287**
51288** The PGroup mutex must be held when this function is called.
51289*/
51290static PgHdr1 *pcache1PinPage(PgHdr1 *pPage){
51291 assert( pPage!=0 );
51292 assert( PAGE_IS_UNPINNED(pPage) );
51293 assert( pPage->pLruNext );
51294 assert( pPage->pLruPrev );
51295 assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) );
51296 pPage->pLruPrev->pLruNext = pPage->pLruNext;
51297 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
51298 pPage->pLruNext = 0;
51299 /* pPage->pLruPrev = 0;
51300 ** No need to clear pLruPrev as it is never accessed if pLruNext is 0 */
51301 assert( pPage->isAnchor==0 );
51302 assert( pPage->pCache->pGroup->lru.isAnchor==1 );
51303 pPage->pCache->nRecyclable--;
51304 return pPage;
51305}
51306
51307
51308/*
51309** Remove the page supplied as an argument from the hash table
51310** (PCache1.apHash structure) that it is currently stored in.
51311** Also free the page if freePage is true.
51312**
51313** The PGroup mutex must be held when this function is called.
51314*/
51315static void pcache1RemoveFromHash(PgHdr1 *pPage, int freeFlag){
51316 unsigned int h;
51317 PCache1 *pCache = pPage->pCache;
51318 PgHdr1 **pp;
51319
51320 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
51321 h = pPage->iKey % pCache->nHash;
51322 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
51323 *pp = (*pp)->pNext;
51324
51325 pCache->nPage--;
51326 if( freeFlag ) pcache1FreePage(p: pPage);
51327}
51328
51329/*
51330** If there are currently more than nMaxPage pages allocated, try
51331** to recycle pages to reduce the number allocated to nMaxPage.
51332*/
51333static void pcache1EnforceMaxPage(PCache1 *pCache){
51334 PGroup *pGroup = pCache->pGroup;
51335 PgHdr1 *p;
51336 assert( sqlite3_mutex_held(pGroup->mutex) );
51337 while( pGroup->nPurgeable>pGroup->nMaxPage
51338 && (p=pGroup->lru.pLruPrev)->isAnchor==0
51339 ){
51340 assert( p->pCache->pGroup==pGroup );
51341 assert( PAGE_IS_UNPINNED(p) );
51342 pcache1PinPage(pPage: p);
51343 pcache1RemoveFromHash(pPage: p, freeFlag: 1);
51344 }
51345 if( pCache->nPage==0 && pCache->pBulk ){
51346 sqlite3_free(p: pCache->pBulk);
51347 pCache->pBulk = pCache->pFree = 0;
51348 }
51349}
51350
51351/*
51352** Discard all pages from cache pCache with a page number (key value)
51353** greater than or equal to iLimit. Any pinned pages that meet this
51354** criteria are unpinned before they are discarded.
51355**
51356** The PCache mutex must be held when this function is called.
51357*/
51358static void pcache1TruncateUnsafe(
51359 PCache1 *pCache, /* The cache to truncate */
51360 unsigned int iLimit /* Drop pages with this pgno or larger */
51361){
51362 TESTONLY( int nPage = 0; ) /* To assert pCache->nPage is correct */
51363 unsigned int h, iStop;
51364 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
51365 assert( pCache->iMaxKey >= iLimit );
51366 assert( pCache->nHash > 0 );
51367 if( pCache->iMaxKey - iLimit < pCache->nHash ){
51368 /* If we are just shaving the last few pages off the end of the
51369 ** cache, then there is no point in scanning the entire hash table.
51370 ** Only scan those hash slots that might contain pages that need to
51371 ** be removed. */
51372 h = iLimit % pCache->nHash;
51373 iStop = pCache->iMaxKey % pCache->nHash;
51374 TESTONLY( nPage = -10; ) /* Disable the pCache->nPage validity check */
51375 }else{
51376 /* This is the general case where many pages are being removed.
51377 ** It is necessary to scan the entire hash table */
51378 h = pCache->nHash/2;
51379 iStop = h - 1;
51380 }
51381 for(;;){
51382 PgHdr1 **pp;
51383 PgHdr1 *pPage;
51384 assert( h<pCache->nHash );
51385 pp = &pCache->apHash[h];
51386 while( (pPage = *pp)!=0 ){
51387 if( pPage->iKey>=iLimit ){
51388 pCache->nPage--;
51389 *pp = pPage->pNext;
51390 if( PAGE_IS_UNPINNED(pPage) ) pcache1PinPage(pPage);
51391 pcache1FreePage(p: pPage);
51392 }else{
51393 pp = &pPage->pNext;
51394 TESTONLY( if( nPage>=0 ) nPage++; )
51395 }
51396 }
51397 if( h==iStop ) break;
51398 h = (h+1) % pCache->nHash;
51399 }
51400 assert( nPage<0 || pCache->nPage==(unsigned)nPage );
51401}
51402
51403/******************************************************************************/
51404/******** sqlite3_pcache Methods **********************************************/
51405
51406/*
51407** Implementation of the sqlite3_pcache.xInit method.
51408*/
51409static int pcache1Init(void *NotUsed){
51410 UNUSED_PARAMETER(NotUsed);
51411 assert( pcache1.isInit==0 );
51412 memset(s: &pcache1, c: 0, n: sizeof(pcache1));
51413
51414
51415 /*
51416 ** The pcache1.separateCache variable is true if each PCache has its own
51417 ** private PGroup (mode-1). pcache1.separateCache is false if the single
51418 ** PGroup in pcache1.grp is used for all page caches (mode-2).
51419 **
51420 ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
51421 **
51422 ** * Use a unified cache in single-threaded applications that have
51423 ** configured a start-time buffer for use as page-cache memory using
51424 ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL
51425 ** pBuf argument.
51426 **
51427 ** * Otherwise use separate caches (mode-1)
51428 */
51429#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT)
51430 pcache1.separateCache = 0;
51431#elif SQLITE_THREADSAFE
51432 pcache1.separateCache = sqlite3GlobalConfig.pPage==0
51433 || sqlite3GlobalConfig.bCoreMutex>0;
51434#else
51435 pcache1.separateCache = sqlite3GlobalConfig.pPage==0;
51436#endif
51437
51438#if SQLITE_THREADSAFE
51439 if( sqlite3GlobalConfig.bCoreMutex ){
51440 pcache1.grp.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU);
51441 pcache1.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PMEM);
51442 }
51443#endif
51444 if( pcache1.separateCache
51445 && sqlite3GlobalConfig.nPage!=0
51446 && sqlite3GlobalConfig.pPage==0
51447 ){
51448 pcache1.nInitPage = sqlite3GlobalConfig.nPage;
51449 }else{
51450 pcache1.nInitPage = 0;
51451 }
51452 pcache1.grp.mxPinned = 10;
51453 pcache1.isInit = 1;
51454 return SQLITE_OK;
51455}
51456
51457/*
51458** Implementation of the sqlite3_pcache.xShutdown method.
51459** Note that the static mutex allocated in xInit does
51460** not need to be freed.
51461*/
51462static void pcache1Shutdown(void *NotUsed){
51463 UNUSED_PARAMETER(NotUsed);
51464 assert( pcache1.isInit!=0 );
51465 memset(s: &pcache1, c: 0, n: sizeof(pcache1));
51466}
51467
51468/* forward declaration */
51469static void pcache1Destroy(sqlite3_pcache *p);
51470
51471/*
51472** Implementation of the sqlite3_pcache.xCreate method.
51473**
51474** Allocate a new cache.
51475*/
51476static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
51477 PCache1 *pCache; /* The newly created page cache */
51478 PGroup *pGroup; /* The group the new page cache will belong to */
51479 int sz; /* Bytes of memory required to allocate the new cache */
51480
51481 assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
51482 assert( szExtra < 300 );
51483
51484 sz = sizeof(PCache1) + sizeof(PGroup)*pcache1.separateCache;
51485 pCache = (PCache1 *)sqlite3MallocZero(n: sz);
51486 if( pCache ){
51487 if( pcache1.separateCache ){
51488 pGroup = (PGroup*)&pCache[1];
51489 pGroup->mxPinned = 10;
51490 }else{
51491 pGroup = &pcache1.grp;
51492 }
51493 pcache1EnterMutex(pGroup);
51494 if( pGroup->lru.isAnchor==0 ){
51495 pGroup->lru.isAnchor = 1;
51496 pGroup->lru.pLruPrev = pGroup->lru.pLruNext = &pGroup->lru;
51497 }
51498 pCache->pGroup = pGroup;
51499 pCache->szPage = szPage;
51500 pCache->szExtra = szExtra;
51501 pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1));
51502 pCache->bPurgeable = (bPurgeable ? 1 : 0);
51503 pcache1ResizeHash(p: pCache);
51504 if( bPurgeable ){
51505 pCache->nMin = 10;
51506 pGroup->nMinPage += pCache->nMin;
51507 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
51508 pCache->pnPurgeable = &pGroup->nPurgeable;
51509 }else{
51510 pCache->pnPurgeable = &pCache->nPurgeableDummy;
51511 }
51512 pcache1LeaveMutex(pGroup);
51513 if( pCache->nHash==0 ){
51514 pcache1Destroy(p: (sqlite3_pcache*)pCache);
51515 pCache = 0;
51516 }
51517 }
51518 return (sqlite3_pcache *)pCache;
51519}
51520
51521/*
51522** Implementation of the sqlite3_pcache.xCachesize method.
51523**
51524** Configure the cache_size limit for a cache.
51525*/
51526static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
51527 PCache1 *pCache = (PCache1 *)p;
51528 u32 n;
51529 assert( nMax>=0 );
51530 if( pCache->bPurgeable ){
51531 PGroup *pGroup = pCache->pGroup;
51532 pcache1EnterMutex(pGroup);
51533 n = (u32)nMax;
51534 if( n > 0x7fff0000 - pGroup->nMaxPage + pCache->nMax ){
51535 n = 0x7fff0000 - pGroup->nMaxPage + pCache->nMax;
51536 }
51537 pGroup->nMaxPage += (n - pCache->nMax);
51538 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
51539 pCache->nMax = n;
51540 pCache->n90pct = pCache->nMax*9/10;
51541 pcache1EnforceMaxPage(pCache);
51542 pcache1LeaveMutex(pGroup);
51543 }
51544}
51545
51546/*
51547** Implementation of the sqlite3_pcache.xShrink method.
51548**
51549** Free up as much memory as possible.
51550*/
51551static void pcache1Shrink(sqlite3_pcache *p){
51552 PCache1 *pCache = (PCache1*)p;
51553 if( pCache->bPurgeable ){
51554 PGroup *pGroup = pCache->pGroup;
51555 unsigned int savedMaxPage;
51556 pcache1EnterMutex(pGroup);
51557 savedMaxPage = pGroup->nMaxPage;
51558 pGroup->nMaxPage = 0;
51559 pcache1EnforceMaxPage(pCache);
51560 pGroup->nMaxPage = savedMaxPage;
51561 pcache1LeaveMutex(pGroup);
51562 }
51563}
51564
51565/*
51566** Implementation of the sqlite3_pcache.xPagecount method.
51567*/
51568static int pcache1Pagecount(sqlite3_pcache *p){
51569 int n;
51570 PCache1 *pCache = (PCache1*)p;
51571 pcache1EnterMutex(pCache->pGroup);
51572 n = pCache->nPage;
51573 pcache1LeaveMutex(pCache->pGroup);
51574 return n;
51575}
51576
51577
51578/*
51579** Implement steps 3, 4, and 5 of the pcache1Fetch() algorithm described
51580** in the header of the pcache1Fetch() procedure.
51581**
51582** This steps are broken out into a separate procedure because they are
51583** usually not needed, and by avoiding the stack initialization required
51584** for these steps, the main pcache1Fetch() procedure can run faster.
51585*/
51586static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2(
51587 PCache1 *pCache,
51588 unsigned int iKey,
51589 int createFlag
51590){
51591 unsigned int nPinned;
51592 PGroup *pGroup = pCache->pGroup;
51593 PgHdr1 *pPage = 0;
51594
51595 /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
51596 assert( pCache->nPage >= pCache->nRecyclable );
51597 nPinned = pCache->nPage - pCache->nRecyclable;
51598 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
51599 assert( pCache->n90pct == pCache->nMax*9/10 );
51600 if( createFlag==1 && (
51601 nPinned>=pGroup->mxPinned
51602 || nPinned>=pCache->n90pct
51603 || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned)
51604 )){
51605 return 0;
51606 }
51607
51608 if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(p: pCache);
51609 assert( pCache->nHash>0 && pCache->apHash );
51610
51611 /* Step 4. Try to recycle a page. */
51612 if( pCache->bPurgeable
51613 && !pGroup->lru.pLruPrev->isAnchor
51614 && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache))
51615 ){
51616 PCache1 *pOther;
51617 pPage = pGroup->lru.pLruPrev;
51618 assert( PAGE_IS_UNPINNED(pPage) );
51619 pcache1RemoveFromHash(pPage, freeFlag: 0);
51620 pcache1PinPage(pPage);
51621 pOther = pPage->pCache;
51622 if( pOther->szAlloc != pCache->szAlloc ){
51623 pcache1FreePage(p: pPage);
51624 pPage = 0;
51625 }else{
51626 pGroup->nPurgeable -= (pOther->bPurgeable - pCache->bPurgeable);
51627 }
51628 }
51629
51630 /* Step 5. If a usable page buffer has still not been found,
51631 ** attempt to allocate a new one.
51632 */
51633 if( !pPage ){
51634 pPage = pcache1AllocPage(pCache, benignMalloc: createFlag==1);
51635 }
51636
51637 if( pPage ){
51638 unsigned int h = iKey % pCache->nHash;
51639 pCache->nPage++;
51640 pPage->iKey = iKey;
51641 pPage->pNext = pCache->apHash[h];
51642 pPage->pCache = pCache;
51643 pPage->pLruNext = 0;
51644 /* pPage->pLruPrev = 0;
51645 ** No need to clear pLruPrev since it is not accessed when pLruNext==0 */
51646 *(void **)pPage->page.pExtra = 0;
51647 pCache->apHash[h] = pPage;
51648 if( iKey>pCache->iMaxKey ){
51649 pCache->iMaxKey = iKey;
51650 }
51651 }
51652 return pPage;
51653}
51654
51655/*
51656** Implementation of the sqlite3_pcache.xFetch method.
51657**
51658** Fetch a page by key value.
51659**
51660** Whether or not a new page may be allocated by this function depends on
51661** the value of the createFlag argument. 0 means do not allocate a new
51662** page. 1 means allocate a new page if space is easily available. 2
51663** means to try really hard to allocate a new page.
51664**
51665** For a non-purgeable cache (a cache used as the storage for an in-memory
51666** database) there is really no difference between createFlag 1 and 2. So
51667** the calling function (pcache.c) will never have a createFlag of 1 on
51668** a non-purgeable cache.
51669**
51670** There are three different approaches to obtaining space for a page,
51671** depending on the value of parameter createFlag (which may be 0, 1 or 2).
51672**
51673** 1. Regardless of the value of createFlag, the cache is searched for a
51674** copy of the requested page. If one is found, it is returned.
51675**
51676** 2. If createFlag==0 and the page is not already in the cache, NULL is
51677** returned.
51678**
51679** 3. If createFlag is 1, and the page is not already in the cache, then
51680** return NULL (do not allocate a new page) if any of the following
51681** conditions are true:
51682**
51683** (a) the number of pages pinned by the cache is greater than
51684** PCache1.nMax, or
51685**
51686** (b) the number of pages pinned by the cache is greater than
51687** the sum of nMax for all purgeable caches, less the sum of
51688** nMin for all other purgeable caches, or
51689**
51690** 4. If none of the first three conditions apply and the cache is marked
51691** as purgeable, and if one of the following is true:
51692**
51693** (a) The number of pages allocated for the cache is already
51694** PCache1.nMax, or
51695**
51696** (b) The number of pages allocated for all purgeable caches is
51697** already equal to or greater than the sum of nMax for all
51698** purgeable caches,
51699**
51700** (c) The system is under memory pressure and wants to avoid
51701** unnecessary pages cache entry allocations
51702**
51703** then attempt to recycle a page from the LRU list. If it is the right
51704** size, return the recycled buffer. Otherwise, free the buffer and
51705** proceed to step 5.
51706**
51707** 5. Otherwise, allocate and return a new page buffer.
51708**
51709** There are two versions of this routine. pcache1FetchWithMutex() is
51710** the general case. pcache1FetchNoMutex() is a faster implementation for
51711** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper
51712** invokes the appropriate routine.
51713*/
51714static PgHdr1 *pcache1FetchNoMutex(
51715 sqlite3_pcache *p,
51716 unsigned int iKey,
51717 int createFlag
51718){
51719 PCache1 *pCache = (PCache1 *)p;
51720 PgHdr1 *pPage = 0;
51721
51722 /* Step 1: Search the hash table for an existing entry. */
51723 pPage = pCache->apHash[iKey % pCache->nHash];
51724 while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
51725
51726 /* Step 2: If the page was found in the hash table, then return it.
51727 ** If the page was not in the hash table and createFlag is 0, abort.
51728 ** Otherwise (page not in hash and createFlag!=0) continue with
51729 ** subsequent steps to try to create the page. */
51730 if( pPage ){
51731 if( PAGE_IS_UNPINNED(pPage) ){
51732 return pcache1PinPage(pPage);
51733 }else{
51734 return pPage;
51735 }
51736 }else if( createFlag ){
51737 /* Steps 3, 4, and 5 implemented by this subroutine */
51738 return pcache1FetchStage2(pCache, iKey, createFlag);
51739 }else{
51740 return 0;
51741 }
51742}
51743#if PCACHE1_MIGHT_USE_GROUP_MUTEX
51744static PgHdr1 *pcache1FetchWithMutex(
51745 sqlite3_pcache *p,
51746 unsigned int iKey,
51747 int createFlag
51748){
51749 PCache1 *pCache = (PCache1 *)p;
51750 PgHdr1 *pPage;
51751
51752 pcache1EnterMutex(pCache->pGroup);
51753 pPage = pcache1FetchNoMutex(p, iKey, createFlag);
51754 assert( pPage==0 || pCache->iMaxKey>=iKey );
51755 pcache1LeaveMutex(pCache->pGroup);
51756 return pPage;
51757}
51758#endif
51759static sqlite3_pcache_page *pcache1Fetch(
51760 sqlite3_pcache *p,
51761 unsigned int iKey,
51762 int createFlag
51763){
51764#if PCACHE1_MIGHT_USE_GROUP_MUTEX || defined(SQLITE_DEBUG)
51765 PCache1 *pCache = (PCache1 *)p;
51766#endif
51767
51768 assert( offsetof(PgHdr1,page)==0 );
51769 assert( pCache->bPurgeable || createFlag!=1 );
51770 assert( pCache->bPurgeable || pCache->nMin==0 );
51771 assert( pCache->bPurgeable==0 || pCache->nMin==10 );
51772 assert( pCache->nMin==0 || pCache->bPurgeable );
51773 assert( pCache->nHash>0 );
51774#if PCACHE1_MIGHT_USE_GROUP_MUTEX
51775 if( pCache->pGroup->mutex ){
51776 return (sqlite3_pcache_page*)pcache1FetchWithMutex(p, iKey, createFlag);
51777 }else
51778#endif
51779 {
51780 return (sqlite3_pcache_page*)pcache1FetchNoMutex(p, iKey, createFlag);
51781 }
51782}
51783
51784
51785/*
51786** Implementation of the sqlite3_pcache.xUnpin method.
51787**
51788** Mark a page as unpinned (eligible for asynchronous recycling).
51789*/
51790static void pcache1Unpin(
51791 sqlite3_pcache *p,
51792 sqlite3_pcache_page *pPg,
51793 int reuseUnlikely
51794){
51795 PCache1 *pCache = (PCache1 *)p;
51796 PgHdr1 *pPage = (PgHdr1 *)pPg;
51797 PGroup *pGroup = pCache->pGroup;
51798
51799 assert( pPage->pCache==pCache );
51800 pcache1EnterMutex(pGroup);
51801
51802 /* It is an error to call this function if the page is already
51803 ** part of the PGroup LRU list.
51804 */
51805 assert( pPage->pLruNext==0 );
51806 assert( PAGE_IS_PINNED(pPage) );
51807
51808 if( reuseUnlikely || pGroup->nPurgeable>pGroup->nMaxPage ){
51809 pcache1RemoveFromHash(pPage, freeFlag: 1);
51810 }else{
51811 /* Add the page to the PGroup LRU list. */
51812 PgHdr1 **ppFirst = &pGroup->lru.pLruNext;
51813 pPage->pLruPrev = &pGroup->lru;
51814 (pPage->pLruNext = *ppFirst)->pLruPrev = pPage;
51815 *ppFirst = pPage;
51816 pCache->nRecyclable++;
51817 }
51818
51819 pcache1LeaveMutex(pCache->pGroup);
51820}
51821
51822/*
51823** Implementation of the sqlite3_pcache.xRekey method.
51824*/
51825static void pcache1Rekey(
51826 sqlite3_pcache *p,
51827 sqlite3_pcache_page *pPg,
51828 unsigned int iOld,
51829 unsigned int iNew
51830){
51831 PCache1 *pCache = (PCache1 *)p;
51832 PgHdr1 *pPage = (PgHdr1 *)pPg;
51833 PgHdr1 **pp;
51834 unsigned int h;
51835 assert( pPage->iKey==iOld );
51836 assert( pPage->pCache==pCache );
51837
51838 pcache1EnterMutex(pCache->pGroup);
51839
51840 h = iOld%pCache->nHash;
51841 pp = &pCache->apHash[h];
51842 while( (*pp)!=pPage ){
51843 pp = &(*pp)->pNext;
51844 }
51845 *pp = pPage->pNext;
51846
51847 h = iNew%pCache->nHash;
51848 pPage->iKey = iNew;
51849 pPage->pNext = pCache->apHash[h];
51850 pCache->apHash[h] = pPage;
51851 if( iNew>pCache->iMaxKey ){
51852 pCache->iMaxKey = iNew;
51853 }
51854
51855 pcache1LeaveMutex(pCache->pGroup);
51856}
51857
51858/*
51859** Implementation of the sqlite3_pcache.xTruncate method.
51860**
51861** Discard all unpinned pages in the cache with a page number equal to
51862** or greater than parameter iLimit. Any pinned pages with a page number
51863** equal to or greater than iLimit are implicitly unpinned.
51864*/
51865static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
51866 PCache1 *pCache = (PCache1 *)p;
51867 pcache1EnterMutex(pCache->pGroup);
51868 if( iLimit<=pCache->iMaxKey ){
51869 pcache1TruncateUnsafe(pCache, iLimit);
51870 pCache->iMaxKey = iLimit-1;
51871 }
51872 pcache1LeaveMutex(pCache->pGroup);
51873}
51874
51875/*
51876** Implementation of the sqlite3_pcache.xDestroy method.
51877**
51878** Destroy a cache allocated using pcache1Create().
51879*/
51880static void pcache1Destroy(sqlite3_pcache *p){
51881 PCache1 *pCache = (PCache1 *)p;
51882 PGroup *pGroup = pCache->pGroup;
51883 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
51884 pcache1EnterMutex(pGroup);
51885 if( pCache->nPage ) pcache1TruncateUnsafe(pCache, iLimit: 0);
51886 assert( pGroup->nMaxPage >= pCache->nMax );
51887 pGroup->nMaxPage -= pCache->nMax;
51888 assert( pGroup->nMinPage >= pCache->nMin );
51889 pGroup->nMinPage -= pCache->nMin;
51890 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
51891 pcache1EnforceMaxPage(pCache);
51892 pcache1LeaveMutex(pGroup);
51893 sqlite3_free(p: pCache->pBulk);
51894 sqlite3_free(p: pCache->apHash);
51895 sqlite3_free(p: pCache);
51896}
51897
51898/*
51899** This function is called during initialization (sqlite3_initialize()) to
51900** install the default pluggable cache module, assuming the user has not
51901** already provided an alternative.
51902*/
51903SQLITE_PRIVATE void sqlite3PCacheSetDefault(void){
51904 static const sqlite3_pcache_methods2 defaultMethods = {
51905 1, /* iVersion */
51906 0, /* pArg */
51907 pcache1Init, /* xInit */
51908 pcache1Shutdown, /* xShutdown */
51909 pcache1Create, /* xCreate */
51910 pcache1Cachesize, /* xCachesize */
51911 pcache1Pagecount, /* xPagecount */
51912 pcache1Fetch, /* xFetch */
51913 pcache1Unpin, /* xUnpin */
51914 pcache1Rekey, /* xRekey */
51915 pcache1Truncate, /* xTruncate */
51916 pcache1Destroy, /* xDestroy */
51917 pcache1Shrink /* xShrink */
51918 };
51919 sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods);
51920}
51921
51922/*
51923** Return the size of the header on each page of this PCACHE implementation.
51924*/
51925SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void){ return ROUND8(sizeof(PgHdr1)); }
51926
51927/*
51928** Return the global mutex used by this PCACHE implementation. The
51929** sqlite3_status() routine needs access to this mutex.
51930*/
51931SQLITE_PRIVATE sqlite3_mutex *sqlite3Pcache1Mutex(void){
51932 return pcache1.mutex;
51933}
51934
51935#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
51936/*
51937** This function is called to free superfluous dynamically allocated memory
51938** held by the pager system. Memory in use by any SQLite pager allocated
51939** by the current thread may be sqlite3_free()ed.
51940**
51941** nReq is the number of bytes of memory required. Once this much has
51942** been released, the function returns. The return value is the total number
51943** of bytes of memory released.
51944*/
51945SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
51946 int nFree = 0;
51947 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
51948 assert( sqlite3_mutex_notheld(pcache1.mutex) );
51949 if( sqlite3GlobalConfig.pPage==0 ){
51950 PgHdr1 *p;
51951 pcache1EnterMutex(&pcache1.grp);
51952 while( (nReq<0 || nFree<nReq)
51953 && (p=pcache1.grp.lru.pLruPrev)!=0
51954 && p->isAnchor==0
51955 ){
51956 nFree += pcache1MemSize(p->page.pBuf);
51957#ifdef SQLITE_PCACHE_SEPARATE_HEADER
51958 nFree += sqlite3MemSize(p);
51959#endif
51960 assert( PAGE_IS_UNPINNED(p) );
51961 pcache1PinPage(p);
51962 pcache1RemoveFromHash(p, 1);
51963 }
51964 pcache1LeaveMutex(&pcache1.grp);
51965 }
51966 return nFree;
51967}
51968#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
51969
51970#ifdef SQLITE_TEST
51971/*
51972** This function is used by test procedures to inspect the internal state
51973** of the global cache.
51974*/
51975SQLITE_PRIVATE void sqlite3PcacheStats(
51976 int *pnCurrent, /* OUT: Total number of pages cached */
51977 int *pnMax, /* OUT: Global maximum cache size */
51978 int *pnMin, /* OUT: Sum of PCache1.nMin for purgeable caches */
51979 int *pnRecyclable /* OUT: Total number of pages available for recycling */
51980){
51981 PgHdr1 *p;
51982 int nRecyclable = 0;
51983 for(p=pcache1.grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){
51984 assert( PAGE_IS_UNPINNED(p) );
51985 nRecyclable++;
51986 }
51987 *pnCurrent = pcache1.grp.nPurgeable;
51988 *pnMax = (int)pcache1.grp.nMaxPage;
51989 *pnMin = (int)pcache1.grp.nMinPage;
51990 *pnRecyclable = nRecyclable;
51991}
51992#endif
51993
51994/************** End of pcache1.c *********************************************/
51995/************** Begin file rowset.c ******************************************/
51996/*
51997** 2008 December 3
51998**
51999** The author disclaims copyright to this source code. In place of
52000** a legal notice, here is a blessing:
52001**
52002** May you do good and not evil.
52003** May you find forgiveness for yourself and forgive others.
52004** May you share freely, never taking more than you give.
52005**
52006*************************************************************************
52007**
52008** This module implements an object we call a "RowSet".
52009**
52010** The RowSet object is a collection of rowids. Rowids
52011** are inserted into the RowSet in an arbitrary order. Inserts
52012** can be intermixed with tests to see if a given rowid has been
52013** previously inserted into the RowSet.
52014**
52015** After all inserts are finished, it is possible to extract the
52016** elements of the RowSet in sorted order. Once this extraction
52017** process has started, no new elements may be inserted.
52018**
52019** Hence, the primitive operations for a RowSet are:
52020**
52021** CREATE
52022** INSERT
52023** TEST
52024** SMALLEST
52025** DESTROY
52026**
52027** The CREATE and DESTROY primitives are the constructor and destructor,
52028** obviously. The INSERT primitive adds a new element to the RowSet.
52029** TEST checks to see if an element is already in the RowSet. SMALLEST
52030** extracts the least value from the RowSet.
52031**
52032** The INSERT primitive might allocate additional memory. Memory is
52033** allocated in chunks so most INSERTs do no allocation. There is an
52034** upper bound on the size of allocated memory. No memory is freed
52035** until DESTROY.
52036**
52037** The TEST primitive includes a "batch" number. The TEST primitive
52038** will only see elements that were inserted before the last change
52039** in the batch number. In other words, if an INSERT occurs between
52040** two TESTs where the TESTs have the same batch nubmer, then the
52041** value added by the INSERT will not be visible to the second TEST.
52042** The initial batch number is zero, so if the very first TEST contains
52043** a non-zero batch number, it will see all prior INSERTs.
52044**
52045** No INSERTs may occurs after a SMALLEST. An assertion will fail if
52046** that is attempted.
52047**
52048** The cost of an INSERT is roughly constant. (Sometimes new memory
52049** has to be allocated on an INSERT.) The cost of a TEST with a new
52050** batch number is O(NlogN) where N is the number of elements in the RowSet.
52051** The cost of a TEST using the same batch number is O(logN). The cost
52052** of the first SMALLEST is O(NlogN). Second and subsequent SMALLEST
52053** primitives are constant time. The cost of DESTROY is O(N).
52054**
52055** TEST and SMALLEST may not be used by the same RowSet. This used to
52056** be possible, but the feature was not used, so it was removed in order
52057** to simplify the code.
52058*/
52059/* #include "sqliteInt.h" */
52060
52061
52062/*
52063** Target size for allocation chunks.
52064*/
52065#define ROWSET_ALLOCATION_SIZE 1024
52066
52067/*
52068** The number of rowset entries per allocation chunk.
52069*/
52070#define ROWSET_ENTRY_PER_CHUNK \
52071 ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
52072
52073/*
52074** Each entry in a RowSet is an instance of the following object.
52075**
52076** This same object is reused to store a linked list of trees of RowSetEntry
52077** objects. In that alternative use, pRight points to the next entry
52078** in the list, pLeft points to the tree, and v is unused. The
52079** RowSet.pForest value points to the head of this forest list.
52080*/
52081struct RowSetEntry {
52082 i64 v; /* ROWID value for this entry */
52083 struct RowSetEntry *pRight; /* Right subtree (larger entries) or list */
52084 struct RowSetEntry *pLeft; /* Left subtree (smaller entries) */
52085};
52086
52087/*
52088** RowSetEntry objects are allocated in large chunks (instances of the
52089** following structure) to reduce memory allocation overhead. The
52090** chunks are kept on a linked list so that they can be deallocated
52091** when the RowSet is destroyed.
52092*/
52093struct RowSetChunk {
52094 struct RowSetChunk *pNextChunk; /* Next chunk on list of them all */
52095 struct RowSetEntry aEntry[ROWSET_ENTRY_PER_CHUNK]; /* Allocated entries */
52096};
52097
52098/*
52099** A RowSet in an instance of the following structure.
52100**
52101** A typedef of this structure if found in sqliteInt.h.
52102*/
52103struct RowSet {
52104 struct RowSetChunk *pChunk; /* List of all chunk allocations */
52105 sqlite3 *db; /* The database connection */
52106 struct RowSetEntry *pEntry; /* List of entries using pRight */
52107 struct RowSetEntry *pLast; /* Last entry on the pEntry list */
52108 struct RowSetEntry *pFresh; /* Source of new entry objects */
52109 struct RowSetEntry *pForest; /* List of binary trees of entries */
52110 u16 nFresh; /* Number of objects on pFresh */
52111 u16 rsFlags; /* Various flags */
52112 int iBatch; /* Current insert batch */
52113};
52114
52115/*
52116** Allowed values for RowSet.rsFlags
52117*/
52118#define ROWSET_SORTED 0x01 /* True if RowSet.pEntry is sorted */
52119#define ROWSET_NEXT 0x02 /* True if sqlite3RowSetNext() has been called */
52120
52121/*
52122** Allocate a RowSet object. Return NULL if a memory allocation
52123** error occurs.
52124*/
52125SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3 *db){
52126 RowSet *p = sqlite3DbMallocRawNN(db, n: sizeof(*p));
52127 if( p ){
52128 int N = sqlite3DbMallocSize(db, p);
52129 p->pChunk = 0;
52130 p->db = db;
52131 p->pEntry = 0;
52132 p->pLast = 0;
52133 p->pForest = 0;
52134 p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
52135 p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
52136 p->rsFlags = ROWSET_SORTED;
52137 p->iBatch = 0;
52138 }
52139 return p;
52140}
52141
52142/*
52143** Deallocate all chunks from a RowSet. This frees all memory that
52144** the RowSet has allocated over its lifetime. This routine is
52145** the destructor for the RowSet.
52146*/
52147SQLITE_PRIVATE void sqlite3RowSetClear(void *pArg){
52148 RowSet *p = (RowSet*)pArg;
52149 struct RowSetChunk *pChunk, *pNextChunk;
52150 for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
52151 pNextChunk = pChunk->pNextChunk;
52152 sqlite3DbFree(db: p->db, p: pChunk);
52153 }
52154 p->pChunk = 0;
52155 p->nFresh = 0;
52156 p->pEntry = 0;
52157 p->pLast = 0;
52158 p->pForest = 0;
52159 p->rsFlags = ROWSET_SORTED;
52160}
52161
52162/*
52163** Deallocate all chunks from a RowSet. This frees all memory that
52164** the RowSet has allocated over its lifetime. This routine is
52165** the destructor for the RowSet.
52166*/
52167SQLITE_PRIVATE void sqlite3RowSetDelete(void *pArg){
52168 sqlite3RowSetClear(pArg);
52169 sqlite3DbFree(db: ((RowSet*)pArg)->db, p: pArg);
52170}
52171
52172/*
52173** Allocate a new RowSetEntry object that is associated with the
52174** given RowSet. Return a pointer to the new and completely uninitialized
52175** object.
52176**
52177** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
52178** routine returns NULL.
52179*/
52180static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){
52181 assert( p!=0 );
52182 if( p->nFresh==0 ){ /*OPTIMIZATION-IF-FALSE*/
52183 /* We could allocate a fresh RowSetEntry each time one is needed, but it
52184 ** is more efficient to pull a preallocated entry from the pool */
52185 struct RowSetChunk *pNew;
52186 pNew = sqlite3DbMallocRawNN(db: p->db, n: sizeof(*pNew));
52187 if( pNew==0 ){
52188 return 0;
52189 }
52190 pNew->pNextChunk = p->pChunk;
52191 p->pChunk = pNew;
52192 p->pFresh = pNew->aEntry;
52193 p->nFresh = ROWSET_ENTRY_PER_CHUNK;
52194 }
52195 p->nFresh--;
52196 return p->pFresh++;
52197}
52198
52199/*
52200** Insert a new value into a RowSet.
52201**
52202** The mallocFailed flag of the database connection is set if a
52203** memory allocation fails.
52204*/
52205SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet *p, i64 rowid){
52206 struct RowSetEntry *pEntry; /* The new entry */
52207 struct RowSetEntry *pLast; /* The last prior entry */
52208
52209 /* This routine is never called after sqlite3RowSetNext() */
52210 assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
52211
52212 pEntry = rowSetEntryAlloc(p);
52213 if( pEntry==0 ) return;
52214 pEntry->v = rowid;
52215 pEntry->pRight = 0;
52216 pLast = p->pLast;
52217 if( pLast ){
52218 if( rowid<=pLast->v ){ /*OPTIMIZATION-IF-FALSE*/
52219 /* Avoid unnecessary sorts by preserving the ROWSET_SORTED flags
52220 ** where possible */
52221 p->rsFlags &= ~ROWSET_SORTED;
52222 }
52223 pLast->pRight = pEntry;
52224 }else{
52225 p->pEntry = pEntry;
52226 }
52227 p->pLast = pEntry;
52228}
52229
52230/*
52231** Merge two lists of RowSetEntry objects. Remove duplicates.
52232**
52233** The input lists are connected via pRight pointers and are
52234** assumed to each already be in sorted order.
52235*/
52236static struct RowSetEntry *rowSetEntryMerge(
52237 struct RowSetEntry *pA, /* First sorted list to be merged */
52238 struct RowSetEntry *pB /* Second sorted list to be merged */
52239){
52240 struct RowSetEntry head;
52241 struct RowSetEntry *pTail;
52242
52243 pTail = &head;
52244 assert( pA!=0 && pB!=0 );
52245 for(;;){
52246 assert( pA->pRight==0 || pA->v<=pA->pRight->v );
52247 assert( pB->pRight==0 || pB->v<=pB->pRight->v );
52248 if( pA->v<=pB->v ){
52249 if( pA->v<pB->v ) pTail = pTail->pRight = pA;
52250 pA = pA->pRight;
52251 if( pA==0 ){
52252 pTail->pRight = pB;
52253 break;
52254 }
52255 }else{
52256 pTail = pTail->pRight = pB;
52257 pB = pB->pRight;
52258 if( pB==0 ){
52259 pTail->pRight = pA;
52260 break;
52261 }
52262 }
52263 }
52264 return head.pRight;
52265}
52266
52267/*
52268** Sort all elements on the list of RowSetEntry objects into order of
52269** increasing v.
52270*/
52271static struct RowSetEntry *rowSetEntrySort(struct RowSetEntry *pIn){
52272 unsigned int i;
52273 struct RowSetEntry *pNext, *aBucket[40];
52274
52275 memset(s: aBucket, c: 0, n: sizeof(aBucket));
52276 while( pIn ){
52277 pNext = pIn->pRight;
52278 pIn->pRight = 0;
52279 for(i=0; aBucket[i]; i++){
52280 pIn = rowSetEntryMerge(pA: aBucket[i], pB: pIn);
52281 aBucket[i] = 0;
52282 }
52283 aBucket[i] = pIn;
52284 pIn = pNext;
52285 }
52286 pIn = aBucket[0];
52287 for(i=1; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
52288 if( aBucket[i]==0 ) continue;
52289 pIn = pIn ? rowSetEntryMerge(pA: pIn, pB: aBucket[i]) : aBucket[i];
52290 }
52291 return pIn;
52292}
52293
52294
52295/*
52296** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects.
52297** Convert this tree into a linked list connected by the pRight pointers
52298** and return pointers to the first and last elements of the new list.
52299*/
52300static void rowSetTreeToList(
52301 struct RowSetEntry *pIn, /* Root of the input tree */
52302 struct RowSetEntry **ppFirst, /* Write head of the output list here */
52303 struct RowSetEntry **ppLast /* Write tail of the output list here */
52304){
52305 assert( pIn!=0 );
52306 if( pIn->pLeft ){
52307 struct RowSetEntry *p;
52308 rowSetTreeToList(pIn: pIn->pLeft, ppFirst, ppLast: &p);
52309 p->pRight = pIn;
52310 }else{
52311 *ppFirst = pIn;
52312 }
52313 if( pIn->pRight ){
52314 rowSetTreeToList(pIn: pIn->pRight, ppFirst: &pIn->pRight, ppLast);
52315 }else{
52316 *ppLast = pIn;
52317 }
52318 assert( (*ppLast)->pRight==0 );
52319}
52320
52321
52322/*
52323** Convert a sorted list of elements (connected by pRight) into a binary
52324** tree with depth of iDepth. A depth of 1 means the tree contains a single
52325** node taken from the head of *ppList. A depth of 2 means a tree with
52326** three nodes. And so forth.
52327**
52328** Use as many entries from the input list as required and update the
52329** *ppList to point to the unused elements of the list. If the input
52330** list contains too few elements, then construct an incomplete tree
52331** and leave *ppList set to NULL.
52332**
52333** Return a pointer to the root of the constructed binary tree.
52334*/
52335static struct RowSetEntry *rowSetNDeepTree(
52336 struct RowSetEntry **ppList,
52337 int iDepth
52338){
52339 struct RowSetEntry *p; /* Root of the new tree */
52340 struct RowSetEntry *pLeft; /* Left subtree */
52341 if( *ppList==0 ){ /*OPTIMIZATION-IF-TRUE*/
52342 /* Prevent unnecessary deep recursion when we run out of entries */
52343 return 0;
52344 }
52345 if( iDepth>1 ){ /*OPTIMIZATION-IF-TRUE*/
52346 /* This branch causes a *balanced* tree to be generated. A valid tree
52347 ** is still generated without this branch, but the tree is wildly
52348 ** unbalanced and inefficient. */
52349 pLeft = rowSetNDeepTree(ppList, iDepth: iDepth-1);
52350 p = *ppList;
52351 if( p==0 ){ /*OPTIMIZATION-IF-FALSE*/
52352 /* It is safe to always return here, but the resulting tree
52353 ** would be unbalanced */
52354 return pLeft;
52355 }
52356 p->pLeft = pLeft;
52357 *ppList = p->pRight;
52358 p->pRight = rowSetNDeepTree(ppList, iDepth: iDepth-1);
52359 }else{
52360 p = *ppList;
52361 *ppList = p->pRight;
52362 p->pLeft = p->pRight = 0;
52363 }
52364 return p;
52365}
52366
52367/*
52368** Convert a sorted list of elements into a binary tree. Make the tree
52369** as deep as it needs to be in order to contain the entire list.
52370*/
52371static struct RowSetEntry *rowSetListToTree(struct RowSetEntry *pList){
52372 int iDepth; /* Depth of the tree so far */
52373 struct RowSetEntry *p; /* Current tree root */
52374 struct RowSetEntry *pLeft; /* Left subtree */
52375
52376 assert( pList!=0 );
52377 p = pList;
52378 pList = p->pRight;
52379 p->pLeft = p->pRight = 0;
52380 for(iDepth=1; pList; iDepth++){
52381 pLeft = p;
52382 p = pList;
52383 pList = p->pRight;
52384 p->pLeft = pLeft;
52385 p->pRight = rowSetNDeepTree(ppList: &pList, iDepth);
52386 }
52387 return p;
52388}
52389
52390/*
52391** Extract the smallest element from the RowSet.
52392** Write the element into *pRowid. Return 1 on success. Return
52393** 0 if the RowSet is already empty.
52394**
52395** After this routine has been called, the sqlite3RowSetInsert()
52396** routine may not be called again.
52397**
52398** This routine may not be called after sqlite3RowSetTest() has
52399** been used. Older versions of RowSet allowed that, but as the
52400** capability was not used by the code generator, it was removed
52401** for code economy.
52402*/
52403SQLITE_PRIVATE int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
52404 assert( p!=0 );
52405 assert( p->pForest==0 ); /* Cannot be used with sqlite3RowSetText() */
52406
52407 /* Merge the forest into a single sorted list on first call */
52408 if( (p->rsFlags & ROWSET_NEXT)==0 ){ /*OPTIMIZATION-IF-FALSE*/
52409 if( (p->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
52410 p->pEntry = rowSetEntrySort(pIn: p->pEntry);
52411 }
52412 p->rsFlags |= ROWSET_SORTED|ROWSET_NEXT;
52413 }
52414
52415 /* Return the next entry on the list */
52416 if( p->pEntry ){
52417 *pRowid = p->pEntry->v;
52418 p->pEntry = p->pEntry->pRight;
52419 if( p->pEntry==0 ){ /*OPTIMIZATION-IF-TRUE*/
52420 /* Free memory immediately, rather than waiting on sqlite3_finalize() */
52421 sqlite3RowSetClear(pArg: p);
52422 }
52423 return 1;
52424 }else{
52425 return 0;
52426 }
52427}
52428
52429/*
52430** Check to see if element iRowid was inserted into the rowset as
52431** part of any insert batch prior to iBatch. Return 1 or 0.
52432**
52433** If this is the first test of a new batch and if there exist entries
52434** on pRowSet->pEntry, then sort those entries into the forest at
52435** pRowSet->pForest so that they can be tested.
52436*/
52437SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, int iBatch, sqlite3_int64 iRowid){
52438 struct RowSetEntry *p, *pTree;
52439
52440 /* This routine is never called after sqlite3RowSetNext() */
52441 assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
52442
52443 /* Sort entries into the forest on the first test of a new batch.
52444 ** To save unnecessary work, only do this when the batch number changes.
52445 */
52446 if( iBatch!=pRowSet->iBatch ){ /*OPTIMIZATION-IF-FALSE*/
52447 p = pRowSet->pEntry;
52448 if( p ){
52449 struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
52450 if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
52451 /* Only sort the current set of entries if they need it */
52452 p = rowSetEntrySort(pIn: p);
52453 }
52454 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
52455 ppPrevTree = &pTree->pRight;
52456 if( pTree->pLeft==0 ){
52457 pTree->pLeft = rowSetListToTree(pList: p);
52458 break;
52459 }else{
52460 struct RowSetEntry *pAux, *pTail;
52461 rowSetTreeToList(pIn: pTree->pLeft, ppFirst: &pAux, ppLast: &pTail);
52462 pTree->pLeft = 0;
52463 p = rowSetEntryMerge(pA: pAux, pB: p);
52464 }
52465 }
52466 if( pTree==0 ){
52467 *ppPrevTree = pTree = rowSetEntryAlloc(p: pRowSet);
52468 if( pTree ){
52469 pTree->v = 0;
52470 pTree->pRight = 0;
52471 pTree->pLeft = rowSetListToTree(pList: p);
52472 }
52473 }
52474 pRowSet->pEntry = 0;
52475 pRowSet->pLast = 0;
52476 pRowSet->rsFlags |= ROWSET_SORTED;
52477 }
52478 pRowSet->iBatch = iBatch;
52479 }
52480
52481 /* Test to see if the iRowid value appears anywhere in the forest.
52482 ** Return 1 if it does and 0 if not.
52483 */
52484 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
52485 p = pTree->pLeft;
52486 while( p ){
52487 if( p->v<iRowid ){
52488 p = p->pRight;
52489 }else if( p->v>iRowid ){
52490 p = p->pLeft;
52491 }else{
52492 return 1;
52493 }
52494 }
52495 }
52496 return 0;
52497}
52498
52499/************** End of rowset.c **********************************************/
52500/************** Begin file pager.c *******************************************/
52501/*
52502** 2001 September 15
52503**
52504** The author disclaims copyright to this source code. In place of
52505** a legal notice, here is a blessing:
52506**
52507** May you do good and not evil.
52508** May you find forgiveness for yourself and forgive others.
52509** May you share freely, never taking more than you give.
52510**
52511*************************************************************************
52512** This is the implementation of the page cache subsystem or "pager".
52513**
52514** The pager is used to access a database disk file. It implements
52515** atomic commit and rollback through the use of a journal file that
52516** is separate from the database file. The pager also implements file
52517** locking to prevent two processes from writing the same database
52518** file simultaneously, or one process from reading the database while
52519** another is writing.
52520*/
52521#ifndef SQLITE_OMIT_DISKIO
52522/* #include "sqliteInt.h" */
52523/************** Include wal.h in the middle of pager.c ***********************/
52524/************** Begin file wal.h *********************************************/
52525/*
52526** 2010 February 1
52527**
52528** The author disclaims copyright to this source code. In place of
52529** a legal notice, here is a blessing:
52530**
52531** May you do good and not evil.
52532** May you find forgiveness for yourself and forgive others.
52533** May you share freely, never taking more than you give.
52534**
52535*************************************************************************
52536** This header file defines the interface to the write-ahead logging
52537** system. Refer to the comments below and the header comment attached to
52538** the implementation of each function in log.c for further details.
52539*/
52540
52541#ifndef SQLITE_WAL_H
52542#define SQLITE_WAL_H
52543
52544/* #include "sqliteInt.h" */
52545
52546/* Macros for extracting appropriate sync flags for either transaction
52547** commits (WAL_SYNC_FLAGS(X)) or for checkpoint ops (CKPT_SYNC_FLAGS(X)):
52548*/
52549#define WAL_SYNC_FLAGS(X) ((X)&0x03)
52550#define CKPT_SYNC_FLAGS(X) (((X)>>2)&0x03)
52551
52552#ifdef SQLITE_OMIT_WAL
52553# define sqlite3WalOpen(x,y,z) 0
52554# define sqlite3WalLimit(x,y)
52555# define sqlite3WalClose(v,w,x,y,z) 0
52556# define sqlite3WalBeginReadTransaction(y,z) 0
52557# define sqlite3WalEndReadTransaction(z)
52558# define sqlite3WalDbsize(y) 0
52559# define sqlite3WalBeginWriteTransaction(y) 0
52560# define sqlite3WalEndWriteTransaction(x) 0
52561# define sqlite3WalUndo(x,y,z) 0
52562# define sqlite3WalSavepoint(y,z)
52563# define sqlite3WalSavepointUndo(y,z) 0
52564# define sqlite3WalFrames(u,v,w,x,y,z) 0
52565# define sqlite3WalCheckpoint(q,r,s,t,u,v,w,x,y,z) 0
52566# define sqlite3WalCallback(z) 0
52567# define sqlite3WalExclusiveMode(y,z) 0
52568# define sqlite3WalHeapMemory(z) 0
52569# define sqlite3WalFramesize(z) 0
52570# define sqlite3WalFindFrame(x,y,z) 0
52571# define sqlite3WalFile(x) 0
52572#else
52573
52574#define WAL_SAVEPOINT_NDATA 4
52575
52576/* Connection to a write-ahead log (WAL) file.
52577** There is one object of this type for each pager.
52578*/
52579typedef struct Wal Wal;
52580
52581/* Open and close a connection to a write-ahead log. */
52582SQLITE_PRIVATE int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *, int, i64, Wal**);
52583SQLITE_PRIVATE int sqlite3WalClose(Wal *pWal, sqlite3*, int sync_flags, int, u8 *);
52584
52585/* Set the limiting size of a WAL file. */
52586SQLITE_PRIVATE void sqlite3WalLimit(Wal*, i64);
52587
52588/* Used by readers to open (lock) and close (unlock) a snapshot. A
52589** snapshot is like a read-transaction. It is the state of the database
52590** at an instant in time. sqlite3WalOpenSnapshot gets a read lock and
52591** preserves the current state even if the other threads or processes
52592** write to or checkpoint the WAL. sqlite3WalCloseSnapshot() closes the
52593** transaction and releases the lock.
52594*/
52595SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *);
52596SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal);
52597
52598/* Read a page from the write-ahead log, if it is present. */
52599SQLITE_PRIVATE int sqlite3WalFindFrame(Wal *, Pgno, u32 *);
52600SQLITE_PRIVATE int sqlite3WalReadFrame(Wal *, u32, int, u8 *);
52601
52602/* If the WAL is not empty, return the size of the database. */
52603SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal);
52604
52605/* Obtain or release the WRITER lock. */
52606SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal);
52607SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal);
52608
52609/* Undo any frames written (but not committed) to the log */
52610SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx);
52611
52612/* Return an integer that records the current (uncommitted) write
52613** position in the WAL */
52614SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData);
52615
52616/* Move the write position of the WAL back to iFrame. Called in
52617** response to a ROLLBACK TO command. */
52618SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData);
52619
52620/* Write a frame or frames to the log. */
52621SQLITE_PRIVATE int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int);
52622
52623/* Copy pages from the log to the database file */
52624SQLITE_PRIVATE int sqlite3WalCheckpoint(
52625 Wal *pWal, /* Write-ahead log connection */
52626 sqlite3 *db, /* Check this handle's interrupt flag */
52627 int eMode, /* One of PASSIVE, FULL and RESTART */
52628 int (*xBusy)(void*), /* Function to call when busy */
52629 void *pBusyArg, /* Context argument for xBusyHandler */
52630 int sync_flags, /* Flags to sync db file with (or 0) */
52631 int nBuf, /* Size of buffer nBuf */
52632 u8 *zBuf, /* Temporary buffer to use */
52633 int *pnLog, /* OUT: Number of frames in WAL */
52634 int *pnCkpt /* OUT: Number of backfilled frames in WAL */
52635);
52636
52637/* Return the value to pass to a sqlite3_wal_hook callback, the
52638** number of frames in the WAL at the point of the last commit since
52639** sqlite3WalCallback() was called. If no commits have occurred since
52640** the last call, then return 0.
52641*/
52642SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal);
52643
52644/* Tell the wal layer that an EXCLUSIVE lock has been obtained (or released)
52645** by the pager layer on the database file.
52646*/
52647SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op);
52648
52649/* Return true if the argument is non-NULL and the WAL module is using
52650** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
52651** WAL module is using shared-memory, return false.
52652*/
52653SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal);
52654
52655#ifdef SQLITE_ENABLE_SNAPSHOT
52656SQLITE_PRIVATE int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot);
52657SQLITE_PRIVATE void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot);
52658SQLITE_PRIVATE int sqlite3WalSnapshotRecover(Wal *pWal);
52659SQLITE_PRIVATE int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot);
52660SQLITE_PRIVATE void sqlite3WalSnapshotUnlock(Wal *pWal);
52661#endif
52662
52663#ifdef SQLITE_ENABLE_ZIPVFS
52664/* If the WAL file is not empty, return the number of bytes of content
52665** stored in each frame (i.e. the db page-size when the WAL was created).
52666*/
52667SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal);
52668#endif
52669
52670/* Return the sqlite3_file object for the WAL file */
52671SQLITE_PRIVATE sqlite3_file *sqlite3WalFile(Wal *pWal);
52672
52673#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
52674SQLITE_PRIVATE int sqlite3WalWriteLock(Wal *pWal, int bLock);
52675SQLITE_PRIVATE void sqlite3WalDb(Wal *pWal, sqlite3 *db);
52676#endif
52677
52678#endif /* ifndef SQLITE_OMIT_WAL */
52679#endif /* SQLITE_WAL_H */
52680
52681/************** End of wal.h *************************************************/
52682/************** Continuing where we left off in pager.c **********************/
52683
52684
52685/******************* NOTES ON THE DESIGN OF THE PAGER ************************
52686**
52687** This comment block describes invariants that hold when using a rollback
52688** journal. These invariants do not apply for journal_mode=WAL,
52689** journal_mode=MEMORY, or journal_mode=OFF.
52690**
52691** Within this comment block, a page is deemed to have been synced
52692** automatically as soon as it is written when PRAGMA synchronous=OFF.
52693** Otherwise, the page is not synced until the xSync method of the VFS
52694** is called successfully on the file containing the page.
52695**
52696** Definition: A page of the database file is said to be "overwriteable" if
52697** one or more of the following are true about the page:
52698**
52699** (a) The original content of the page as it was at the beginning of
52700** the transaction has been written into the rollback journal and
52701** synced.
52702**
52703** (b) The page was a freelist leaf page at the start of the transaction.
52704**
52705** (c) The page number is greater than the largest page that existed in
52706** the database file at the start of the transaction.
52707**
52708** (1) A page of the database file is never overwritten unless one of the
52709** following are true:
52710**
52711** (a) The page and all other pages on the same sector are overwriteable.
52712**
52713** (b) The atomic page write optimization is enabled, and the entire
52714** transaction other than the update of the transaction sequence
52715** number consists of a single page change.
52716**
52717** (2) The content of a page written into the rollback journal exactly matches
52718** both the content in the database when the rollback journal was written
52719** and the content in the database at the beginning of the current
52720** transaction.
52721**
52722** (3) Writes to the database file are an integer multiple of the page size
52723** in length and are aligned on a page boundary.
52724**
52725** (4) Reads from the database file are either aligned on a page boundary and
52726** an integer multiple of the page size in length or are taken from the
52727** first 100 bytes of the database file.
52728**
52729** (5) All writes to the database file are synced prior to the rollback journal
52730** being deleted, truncated, or zeroed.
52731**
52732** (6) If a super-journal file is used, then all writes to the database file
52733** are synced prior to the super-journal being deleted.
52734**
52735** Definition: Two databases (or the same database at two points it time)
52736** are said to be "logically equivalent" if they give the same answer to
52737** all queries. Note in particular the content of freelist leaf
52738** pages can be changed arbitrarily without affecting the logical equivalence
52739** of the database.
52740**
52741** (7) At any time, if any subset, including the empty set and the total set,
52742** of the unsynced changes to a rollback journal are removed and the
52743** journal is rolled back, the resulting database file will be logically
52744** equivalent to the database file at the beginning of the transaction.
52745**
52746** (8) When a transaction is rolled back, the xTruncate method of the VFS
52747** is called to restore the database file to the same size it was at
52748** the beginning of the transaction. (In some VFSes, the xTruncate
52749** method is a no-op, but that does not change the fact the SQLite will
52750** invoke it.)
52751**
52752** (9) Whenever the database file is modified, at least one bit in the range
52753** of bytes from 24 through 39 inclusive will be changed prior to releasing
52754** the EXCLUSIVE lock, thus signaling other connections on the same
52755** database to flush their caches.
52756**
52757** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less
52758** than one billion transactions.
52759**
52760** (11) A database file is well-formed at the beginning and at the conclusion
52761** of every transaction.
52762**
52763** (12) An EXCLUSIVE lock is held on the database file when writing to
52764** the database file.
52765**
52766** (13) A SHARED lock is held on the database file while reading any
52767** content out of the database file.
52768**
52769******************************************************************************/
52770
52771/*
52772** Macros for troubleshooting. Normally turned off
52773*/
52774#if 0
52775int sqlite3PagerTrace=1; /* True to enable tracing */
52776#define sqlite3DebugPrintf printf
52777#define PAGERTRACE(X) if( sqlite3PagerTrace ){ sqlite3DebugPrintf X; }
52778#else
52779#define PAGERTRACE(X)
52780#endif
52781
52782/*
52783** The following two macros are used within the PAGERTRACE() macros above
52784** to print out file-descriptors.
52785**
52786** PAGERID() takes a pointer to a Pager struct as its argument. The
52787** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
52788** struct as its argument.
52789*/
52790#define PAGERID(p) (SQLITE_PTR_TO_INT(p->fd))
52791#define FILEHANDLEID(fd) (SQLITE_PTR_TO_INT(fd))
52792
52793/*
52794** The Pager.eState variable stores the current 'state' of a pager. A
52795** pager may be in any one of the seven states shown in the following
52796** state diagram.
52797**
52798** OPEN <------+------+
52799** | | |
52800** V | |
52801** +---------> READER-------+ |
52802** | | |
52803** | V |
52804** |<-------WRITER_LOCKED------> ERROR
52805** | | ^
52806** | V |
52807** |<------WRITER_CACHEMOD-------->|
52808** | | |
52809** | V |
52810** |<-------WRITER_DBMOD---------->|
52811** | | |
52812** | V |
52813** +<------WRITER_FINISHED-------->+
52814**
52815**
52816** List of state transitions and the C [function] that performs each:
52817**
52818** OPEN -> READER [sqlite3PagerSharedLock]
52819** READER -> OPEN [pager_unlock]
52820**
52821** READER -> WRITER_LOCKED [sqlite3PagerBegin]
52822** WRITER_LOCKED -> WRITER_CACHEMOD [pager_open_journal]
52823** WRITER_CACHEMOD -> WRITER_DBMOD [syncJournal]
52824** WRITER_DBMOD -> WRITER_FINISHED [sqlite3PagerCommitPhaseOne]
52825** WRITER_*** -> READER [pager_end_transaction]
52826**
52827** WRITER_*** -> ERROR [pager_error]
52828** ERROR -> OPEN [pager_unlock]
52829**
52830**
52831** OPEN:
52832**
52833** The pager starts up in this state. Nothing is guaranteed in this
52834** state - the file may or may not be locked and the database size is
52835** unknown. The database may not be read or written.
52836**
52837** * No read or write transaction is active.
52838** * Any lock, or no lock at all, may be held on the database file.
52839** * The dbSize, dbOrigSize and dbFileSize variables may not be trusted.
52840**
52841** READER:
52842**
52843** In this state all the requirements for reading the database in
52844** rollback (non-WAL) mode are met. Unless the pager is (or recently
52845** was) in exclusive-locking mode, a user-level read transaction is
52846** open. The database size is known in this state.
52847**
52848** A connection running with locking_mode=normal enters this state when
52849** it opens a read-transaction on the database and returns to state
52850** OPEN after the read-transaction is completed. However a connection
52851** running in locking_mode=exclusive (including temp databases) remains in
52852** this state even after the read-transaction is closed. The only way
52853** a locking_mode=exclusive connection can transition from READER to OPEN
52854** is via the ERROR state (see below).
52855**
52856** * A read transaction may be active (but a write-transaction cannot).
52857** * A SHARED or greater lock is held on the database file.
52858** * The dbSize variable may be trusted (even if a user-level read
52859** transaction is not active). The dbOrigSize and dbFileSize variables
52860** may not be trusted at this point.
52861** * If the database is a WAL database, then the WAL connection is open.
52862** * Even if a read-transaction is not open, it is guaranteed that
52863** there is no hot-journal in the file-system.
52864**
52865** WRITER_LOCKED:
52866**
52867** The pager moves to this state from READER when a write-transaction
52868** is first opened on the database. In WRITER_LOCKED state, all locks
52869** required to start a write-transaction are held, but no actual
52870** modifications to the cache or database have taken place.
52871**
52872** In rollback mode, a RESERVED or (if the transaction was opened with
52873** BEGIN EXCLUSIVE) EXCLUSIVE lock is obtained on the database file when
52874** moving to this state, but the journal file is not written to or opened
52875** to in this state. If the transaction is committed or rolled back while
52876** in WRITER_LOCKED state, all that is required is to unlock the database
52877** file.
52878**
52879** IN WAL mode, WalBeginWriteTransaction() is called to lock the log file.
52880** If the connection is running with locking_mode=exclusive, an attempt
52881** is made to obtain an EXCLUSIVE lock on the database file.
52882**
52883** * A write transaction is active.
52884** * If the connection is open in rollback-mode, a RESERVED or greater
52885** lock is held on the database file.
52886** * If the connection is open in WAL-mode, a WAL write transaction
52887** is open (i.e. sqlite3WalBeginWriteTransaction() has been successfully
52888** called).
52889** * The dbSize, dbOrigSize and dbFileSize variables are all valid.
52890** * The contents of the pager cache have not been modified.
52891** * The journal file may or may not be open.
52892** * Nothing (not even the first header) has been written to the journal.
52893**
52894** WRITER_CACHEMOD:
52895**
52896** A pager moves from WRITER_LOCKED state to this state when a page is
52897** first modified by the upper layer. In rollback mode the journal file
52898** is opened (if it is not already open) and a header written to the
52899** start of it. The database file on disk has not been modified.
52900**
52901** * A write transaction is active.
52902** * A RESERVED or greater lock is held on the database file.
52903** * The journal file is open and the first header has been written
52904** to it, but the header has not been synced to disk.
52905** * The contents of the page cache have been modified.
52906**
52907** WRITER_DBMOD:
52908**
52909** The pager transitions from WRITER_CACHEMOD into WRITER_DBMOD state
52910** when it modifies the contents of the database file. WAL connections
52911** never enter this state (since they do not modify the database file,
52912** just the log file).
52913**
52914** * A write transaction is active.
52915** * An EXCLUSIVE or greater lock is held on the database file.
52916** * The journal file is open and the first header has been written
52917** and synced to disk.
52918** * The contents of the page cache have been modified (and possibly
52919** written to disk).
52920**
52921** WRITER_FINISHED:
52922**
52923** It is not possible for a WAL connection to enter this state.
52924**
52925** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
52926** state after the entire transaction has been successfully written into the
52927** database file. In this state the transaction may be committed simply
52928** by finalizing the journal file. Once in WRITER_FINISHED state, it is
52929** not possible to modify the database further. At this point, the upper
52930** layer must either commit or rollback the transaction.
52931**
52932** * A write transaction is active.
52933** * An EXCLUSIVE or greater lock is held on the database file.
52934** * All writing and syncing of journal and database data has finished.
52935** If no error occurred, all that remains is to finalize the journal to
52936** commit the transaction. If an error did occur, the caller will need
52937** to rollback the transaction.
52938**
52939** ERROR:
52940**
52941** The ERROR state is entered when an IO or disk-full error (including
52942** SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it
52943** difficult to be sure that the in-memory pager state (cache contents,
52944** db size etc.) are consistent with the contents of the file-system.
52945**
52946** Temporary pager files may enter the ERROR state, but in-memory pagers
52947** cannot.
52948**
52949** For example, if an IO error occurs while performing a rollback,
52950** the contents of the page-cache may be left in an inconsistent state.
52951** At this point it would be dangerous to change back to READER state
52952** (as usually happens after a rollback). Any subsequent readers might
52953** report database corruption (due to the inconsistent cache), and if
52954** they upgrade to writers, they may inadvertently corrupt the database
52955** file. To avoid this hazard, the pager switches into the ERROR state
52956** instead of READER following such an error.
52957**
52958** Once it has entered the ERROR state, any attempt to use the pager
52959** to read or write data returns an error. Eventually, once all
52960** outstanding transactions have been abandoned, the pager is able to
52961** transition back to OPEN state, discarding the contents of the
52962** page-cache and any other in-memory state at the same time. Everything
52963** is reloaded from disk (and, if necessary, hot-journal rollback peformed)
52964** when a read-transaction is next opened on the pager (transitioning
52965** the pager into READER state). At that point the system has recovered
52966** from the error.
52967**
52968** Specifically, the pager jumps into the ERROR state if:
52969**
52970** 1. An error occurs while attempting a rollback. This happens in
52971** function sqlite3PagerRollback().
52972**
52973** 2. An error occurs while attempting to finalize a journal file
52974** following a commit in function sqlite3PagerCommitPhaseTwo().
52975**
52976** 3. An error occurs while attempting to write to the journal or
52977** database file in function pagerStress() in order to free up
52978** memory.
52979**
52980** In other cases, the error is returned to the b-tree layer. The b-tree
52981** layer then attempts a rollback operation. If the error condition
52982** persists, the pager enters the ERROR state via condition (1) above.
52983**
52984** Condition (3) is necessary because it can be triggered by a read-only
52985** statement executed within a transaction. In this case, if the error
52986** code were simply returned to the user, the b-tree layer would not
52987** automatically attempt a rollback, as it assumes that an error in a
52988** read-only statement cannot leave the pager in an internally inconsistent
52989** state.
52990**
52991** * The Pager.errCode variable is set to something other than SQLITE_OK.
52992** * There are one or more outstanding references to pages (after the
52993** last reference is dropped the pager should move back to OPEN state).
52994** * The pager is not an in-memory pager.
52995**
52996**
52997** Notes:
52998**
52999** * A pager is never in WRITER_DBMOD or WRITER_FINISHED state if the
53000** connection is open in WAL mode. A WAL connection is always in one
53001** of the first four states.
53002**
53003** * Normally, a connection open in exclusive mode is never in PAGER_OPEN
53004** state. There are two exceptions: immediately after exclusive-mode has
53005** been turned on (and before any read or write transactions are
53006** executed), and when the pager is leaving the "error state".
53007**
53008** * See also: assert_pager_state().
53009*/
53010#define PAGER_OPEN 0
53011#define PAGER_READER 1
53012#define PAGER_WRITER_LOCKED 2
53013#define PAGER_WRITER_CACHEMOD 3
53014#define PAGER_WRITER_DBMOD 4
53015#define PAGER_WRITER_FINISHED 5
53016#define PAGER_ERROR 6
53017
53018/*
53019** The Pager.eLock variable is almost always set to one of the
53020** following locking-states, according to the lock currently held on
53021** the database file: NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
53022** This variable is kept up to date as locks are taken and released by
53023** the pagerLockDb() and pagerUnlockDb() wrappers.
53024**
53025** If the VFS xLock() or xUnlock() returns an error other than SQLITE_BUSY
53026** (i.e. one of the SQLITE_IOERR subtypes), it is not clear whether or not
53027** the operation was successful. In these circumstances pagerLockDb() and
53028** pagerUnlockDb() take a conservative approach - eLock is always updated
53029** when unlocking the file, and only updated when locking the file if the
53030** VFS call is successful. This way, the Pager.eLock variable may be set
53031** to a less exclusive (lower) value than the lock that is actually held
53032** at the system level, but it is never set to a more exclusive value.
53033**
53034** This is usually safe. If an xUnlock fails or appears to fail, there may
53035** be a few redundant xLock() calls or a lock may be held for longer than
53036** required, but nothing really goes wrong.
53037**
53038** The exception is when the database file is unlocked as the pager moves
53039** from ERROR to OPEN state. At this point there may be a hot-journal file
53040** in the file-system that needs to be rolled back (as part of an OPEN->SHARED
53041** transition, by the same pager or any other). If the call to xUnlock()
53042** fails at this point and the pager is left holding an EXCLUSIVE lock, this
53043** can confuse the call to xCheckReservedLock() call made later as part
53044** of hot-journal detection.
53045**
53046** xCheckReservedLock() is defined as returning true "if there is a RESERVED
53047** lock held by this process or any others". So xCheckReservedLock may
53048** return true because the caller itself is holding an EXCLUSIVE lock (but
53049** doesn't know it because of a previous error in xUnlock). If this happens
53050** a hot-journal may be mistaken for a journal being created by an active
53051** transaction in another process, causing SQLite to read from the database
53052** without rolling it back.
53053**
53054** To work around this, if a call to xUnlock() fails when unlocking the
53055** database in the ERROR state, Pager.eLock is set to UNKNOWN_LOCK. It
53056** is only changed back to a real locking state after a successful call
53057** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
53058** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK
53059** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
53060** lock on the database file before attempting to roll it back. See function
53061** PagerSharedLock() for more detail.
53062**
53063** Pager.eLock may only be set to UNKNOWN_LOCK when the pager is in
53064** PAGER_OPEN state.
53065*/
53066#define UNKNOWN_LOCK (EXCLUSIVE_LOCK+1)
53067
53068/*
53069** The maximum allowed sector size. 64KiB. If the xSectorsize() method
53070** returns a value larger than this, then MAX_SECTOR_SIZE is used instead.
53071** This could conceivably cause corruption following a power failure on
53072** such a system. This is currently an undocumented limit.
53073*/
53074#define MAX_SECTOR_SIZE 0x10000
53075
53076
53077/*
53078** An instance of the following structure is allocated for each active
53079** savepoint and statement transaction in the system. All such structures
53080** are stored in the Pager.aSavepoint[] array, which is allocated and
53081** resized using sqlite3Realloc().
53082**
53083** When a savepoint is created, the PagerSavepoint.iHdrOffset field is
53084** set to 0. If a journal-header is written into the main journal while
53085** the savepoint is active, then iHdrOffset is set to the byte offset
53086** immediately following the last journal record written into the main
53087** journal before the journal-header. This is required during savepoint
53088** rollback (see pagerPlaybackSavepoint()).
53089*/
53090typedef struct PagerSavepoint PagerSavepoint;
53091struct PagerSavepoint {
53092 i64 iOffset; /* Starting offset in main journal */
53093 i64 iHdrOffset; /* See above */
53094 Bitvec *pInSavepoint; /* Set of pages in this savepoint */
53095 Pgno nOrig; /* Original number of pages in file */
53096 Pgno iSubRec; /* Index of first record in sub-journal */
53097 int bTruncateOnRelease; /* If stmt journal may be truncated on RELEASE */
53098#ifndef SQLITE_OMIT_WAL
53099 u32 aWalData[WAL_SAVEPOINT_NDATA]; /* WAL savepoint context */
53100#endif
53101};
53102
53103/*
53104** Bits of the Pager.doNotSpill flag. See further description below.
53105*/
53106#define SPILLFLAG_OFF 0x01 /* Never spill cache. Set via pragma */
53107#define SPILLFLAG_ROLLBACK 0x02 /* Current rolling back, so do not spill */
53108#define SPILLFLAG_NOSYNC 0x04 /* Spill is ok, but do not sync */
53109
53110/*
53111** An open page cache is an instance of struct Pager. A description of
53112** some of the more important member variables follows:
53113**
53114** eState
53115**
53116** The current 'state' of the pager object. See the comment and state
53117** diagram above for a description of the pager state.
53118**
53119** eLock
53120**
53121** For a real on-disk database, the current lock held on the database file -
53122** NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
53123**
53124** For a temporary or in-memory database (neither of which require any
53125** locks), this variable is always set to EXCLUSIVE_LOCK. Since such
53126** databases always have Pager.exclusiveMode==1, this tricks the pager
53127** logic into thinking that it already has all the locks it will ever
53128** need (and no reason to release them).
53129**
53130** In some (obscure) circumstances, this variable may also be set to
53131** UNKNOWN_LOCK. See the comment above the #define of UNKNOWN_LOCK for
53132** details.
53133**
53134** changeCountDone
53135**
53136** This boolean variable is used to make sure that the change-counter
53137** (the 4-byte header field at byte offset 24 of the database file) is
53138** not updated more often than necessary.
53139**
53140** It is set to true when the change-counter field is updated, which
53141** can only happen if an exclusive lock is held on the database file.
53142** It is cleared (set to false) whenever an exclusive lock is
53143** relinquished on the database file. Each time a transaction is committed,
53144** The changeCountDone flag is inspected. If it is true, the work of
53145** updating the change-counter is omitted for the current transaction.
53146**
53147** This mechanism means that when running in exclusive mode, a connection
53148** need only update the change-counter once, for the first transaction
53149** committed.
53150**
53151** setSuper
53152**
53153** When PagerCommitPhaseOne() is called to commit a transaction, it may
53154** (or may not) specify a super-journal name to be written into the
53155** journal file before it is synced to disk.
53156**
53157** Whether or not a journal file contains a super-journal pointer affects
53158** the way in which the journal file is finalized after the transaction is
53159** committed or rolled back when running in "journal_mode=PERSIST" mode.
53160** If a journal file does not contain a super-journal pointer, it is
53161** finalized by overwriting the first journal header with zeroes. If
53162** it does contain a super-journal pointer the journal file is finalized
53163** by truncating it to zero bytes, just as if the connection were
53164** running in "journal_mode=truncate" mode.
53165**
53166** Journal files that contain super-journal pointers cannot be finalized
53167** simply by overwriting the first journal-header with zeroes, as the
53168** super-journal pointer could interfere with hot-journal rollback of any
53169** subsequently interrupted transaction that reuses the journal file.
53170**
53171** The flag is cleared as soon as the journal file is finalized (either
53172** by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the
53173** journal file from being successfully finalized, the setSuper flag
53174** is cleared anyway (and the pager will move to ERROR state).
53175**
53176** doNotSpill
53177**
53178** This variables control the behavior of cache-spills (calls made by
53179** the pcache module to the pagerStress() routine to write cached data
53180** to the file-system in order to free up memory).
53181**
53182** When bits SPILLFLAG_OFF or SPILLFLAG_ROLLBACK of doNotSpill are set,
53183** writing to the database from pagerStress() is disabled altogether.
53184** The SPILLFLAG_ROLLBACK case is done in a very obscure case that
53185** comes up during savepoint rollback that requires the pcache module
53186** to allocate a new page to prevent the journal file from being written
53187** while it is being traversed by code in pager_playback(). The SPILLFLAG_OFF
53188** case is a user preference.
53189**
53190** If the SPILLFLAG_NOSYNC bit is set, writing to the database from
53191** pagerStress() is permitted, but syncing the journal file is not.
53192** This flag is set by sqlite3PagerWrite() when the file-system sector-size
53193** is larger than the database page-size in order to prevent a journal sync
53194** from happening in between the journalling of two pages on the same sector.
53195**
53196** subjInMemory
53197**
53198** This is a boolean variable. If true, then any required sub-journal
53199** is opened as an in-memory journal file. If false, then in-memory
53200** sub-journals are only used for in-memory pager files.
53201**
53202** This variable is updated by the upper layer each time a new
53203** write-transaction is opened.
53204**
53205** dbSize, dbOrigSize, dbFileSize
53206**
53207** Variable dbSize is set to the number of pages in the database file.
53208** It is valid in PAGER_READER and higher states (all states except for
53209** OPEN and ERROR).
53210**
53211** dbSize is set based on the size of the database file, which may be
53212** larger than the size of the database (the value stored at offset
53213** 28 of the database header by the btree). If the size of the file
53214** is not an integer multiple of the page-size, the value stored in
53215** dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
53216** Except, any file that is greater than 0 bytes in size is considered
53217** to have at least one page. (i.e. a 1KB file with 2K page-size leads
53218** to dbSize==1).
53219**
53220** During a write-transaction, if pages with page-numbers greater than
53221** dbSize are modified in the cache, dbSize is updated accordingly.
53222** Similarly, if the database is truncated using PagerTruncateImage(),
53223** dbSize is updated.
53224**
53225** Variables dbOrigSize and dbFileSize are valid in states
53226** PAGER_WRITER_LOCKED and higher. dbOrigSize is a copy of the dbSize
53227** variable at the start of the transaction. It is used during rollback,
53228** and to determine whether or not pages need to be journalled before
53229** being modified.
53230**
53231** Throughout a write-transaction, dbFileSize contains the size of
53232** the file on disk in pages. It is set to a copy of dbSize when the
53233** write-transaction is first opened, and updated when VFS calls are made
53234** to write or truncate the database file on disk.
53235**
53236** The only reason the dbFileSize variable is required is to suppress
53237** unnecessary calls to xTruncate() after committing a transaction. If,
53238** when a transaction is committed, the dbFileSize variable indicates
53239** that the database file is larger than the database image (Pager.dbSize),
53240** pager_truncate() is called. The pager_truncate() call uses xFilesize()
53241** to measure the database file on disk, and then truncates it if required.
53242** dbFileSize is not used when rolling back a transaction. In this case
53243** pager_truncate() is called unconditionally (which means there may be
53244** a call to xFilesize() that is not strictly required). In either case,
53245** pager_truncate() may cause the file to become smaller or larger.
53246**
53247** dbHintSize
53248**
53249** The dbHintSize variable is used to limit the number of calls made to
53250** the VFS xFileControl(FCNTL_SIZE_HINT) method.
53251**
53252** dbHintSize is set to a copy of the dbSize variable when a
53253** write-transaction is opened (at the same time as dbFileSize and
53254** dbOrigSize). If the xFileControl(FCNTL_SIZE_HINT) method is called,
53255** dbHintSize is increased to the number of pages that correspond to the
53256** size-hint passed to the method call. See pager_write_pagelist() for
53257** details.
53258**
53259** errCode
53260**
53261** The Pager.errCode variable is only ever used in PAGER_ERROR state. It
53262** is set to zero in all other states. In PAGER_ERROR state, Pager.errCode
53263** is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX
53264** sub-codes.
53265**
53266** syncFlags, walSyncFlags
53267**
53268** syncFlags is either SQLITE_SYNC_NORMAL (0x02) or SQLITE_SYNC_FULL (0x03).
53269** syncFlags is used for rollback mode. walSyncFlags is used for WAL mode
53270** and contains the flags used to sync the checkpoint operations in the
53271** lower two bits, and sync flags used for transaction commits in the WAL
53272** file in bits 0x04 and 0x08. In other words, to get the correct sync flags
53273** for checkpoint operations, use (walSyncFlags&0x03) and to get the correct
53274** sync flags for transaction commit, use ((walSyncFlags>>2)&0x03). Note
53275** that with synchronous=NORMAL in WAL mode, transaction commit is not synced
53276** meaning that the 0x04 and 0x08 bits are both zero.
53277*/
53278struct Pager {
53279 sqlite3_vfs *pVfs; /* OS functions to use for IO */
53280 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
53281 u8 journalMode; /* One of the PAGER_JOURNALMODE_* values */
53282 u8 useJournal; /* Use a rollback journal on this file */
53283 u8 noSync; /* Do not sync the journal if true */
53284 u8 fullSync; /* Do extra syncs of the journal for robustness */
53285 u8 extraSync; /* sync directory after journal delete */
53286 u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */
53287 u8 walSyncFlags; /* See description above */
53288 u8 tempFile; /* zFilename is a temporary or immutable file */
53289 u8 noLock; /* Do not lock (except in WAL mode) */
53290 u8 readOnly; /* True for a read-only database */
53291 u8 memDb; /* True to inhibit all file I/O */
53292 u8 memVfs; /* VFS-implemented memory database */
53293
53294 /**************************************************************************
53295 ** The following block contains those class members that change during
53296 ** routine operation. Class members not in this block are either fixed
53297 ** when the pager is first created or else only change when there is a
53298 ** significant mode change (such as changing the page_size, locking_mode,
53299 ** or the journal_mode). From another view, these class members describe
53300 ** the "state" of the pager, while other class members describe the
53301 ** "configuration" of the pager.
53302 */
53303 u8 eState; /* Pager state (OPEN, READER, WRITER_LOCKED..) */
53304 u8 eLock; /* Current lock held on database file */
53305 u8 changeCountDone; /* Set after incrementing the change-counter */
53306 u8 setSuper; /* Super-jrnl name is written into jrnl */
53307 u8 doNotSpill; /* Do not spill the cache when non-zero */
53308 u8 subjInMemory; /* True to use in-memory sub-journals */
53309 u8 bUseFetch; /* True to use xFetch() */
53310 u8 hasHeldSharedLock; /* True if a shared lock has ever been held */
53311 Pgno dbSize; /* Number of pages in the database */
53312 Pgno dbOrigSize; /* dbSize before the current transaction */
53313 Pgno dbFileSize; /* Number of pages in the database file */
53314 Pgno dbHintSize; /* Value passed to FCNTL_SIZE_HINT call */
53315 int errCode; /* One of several kinds of errors */
53316 int nRec; /* Pages journalled since last j-header written */
53317 u32 cksumInit; /* Quasi-random value added to every checksum */
53318 u32 nSubRec; /* Number of records written to sub-journal */
53319 Bitvec *pInJournal; /* One bit for each page in the database file */
53320 sqlite3_file *fd; /* File descriptor for database */
53321 sqlite3_file *jfd; /* File descriptor for main journal */
53322 sqlite3_file *sjfd; /* File descriptor for sub-journal */
53323 i64 journalOff; /* Current write offset in the journal file */
53324 i64 journalHdr; /* Byte offset to previous journal header */
53325 sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */
53326 PagerSavepoint *aSavepoint; /* Array of active savepoints */
53327 int nSavepoint; /* Number of elements in aSavepoint[] */
53328 u32 iDataVersion; /* Changes whenever database content changes */
53329 char dbFileVers[16]; /* Changes whenever database file changes */
53330
53331 int nMmapOut; /* Number of mmap pages currently outstanding */
53332 sqlite3_int64 szMmap; /* Desired maximum mmap size */
53333 PgHdr *pMmapFreelist; /* List of free mmap page headers (pDirty) */
53334 /*
53335 ** End of the routinely-changing class members
53336 ***************************************************************************/
53337
53338 u16 nExtra; /* Add this many bytes to each in-memory page */
53339 i16 nReserve; /* Number of unused bytes at end of each page */
53340 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
53341 u32 sectorSize; /* Assumed sector size during rollback */
53342 Pgno mxPgno; /* Maximum allowed size of the database */
53343 i64 pageSize; /* Number of bytes in a page */
53344 i64 journalSizeLimit; /* Size limit for persistent journal files */
53345 char *zFilename; /* Name of the database file */
53346 char *zJournal; /* Name of the journal file */
53347 int (*xBusyHandler)(void*); /* Function to call when busy */
53348 void *pBusyHandlerArg; /* Context argument for xBusyHandler */
53349 int aStat[4]; /* Total cache hits, misses, writes, spills */
53350#ifdef SQLITE_TEST
53351 int nRead; /* Database pages read */
53352#endif
53353 void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
53354 int (*xGet)(Pager*,Pgno,DbPage**,int); /* Routine to fetch a patch */
53355 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
53356 PCache *pPCache; /* Pointer to page cache object */
53357#ifndef SQLITE_OMIT_WAL
53358 Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */
53359 char *zWal; /* File name for write-ahead log */
53360#endif
53361};
53362
53363/*
53364** Indexes for use with Pager.aStat[]. The Pager.aStat[] array contains
53365** the values accessed by passing SQLITE_DBSTATUS_CACHE_HIT, CACHE_MISS
53366** or CACHE_WRITE to sqlite3_db_status().
53367*/
53368#define PAGER_STAT_HIT 0
53369#define PAGER_STAT_MISS 1
53370#define PAGER_STAT_WRITE 2
53371#define PAGER_STAT_SPILL 3
53372
53373/*
53374** The following global variables hold counters used for
53375** testing purposes only. These variables do not exist in
53376** a non-testing build. These variables are not thread-safe.
53377*/
53378#ifdef SQLITE_TEST
53379SQLITE_API int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
53380SQLITE_API int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
53381SQLITE_API int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
53382# define PAGER_INCR(v) v++
53383#else
53384# define PAGER_INCR(v)
53385#endif
53386
53387
53388
53389/*
53390** Journal files begin with the following magic string. The data
53391** was obtained from /dev/random. It is used only as a sanity check.
53392**
53393** Since version 2.8.0, the journal format contains additional sanity
53394** checking information. If the power fails while the journal is being
53395** written, semi-random garbage data might appear in the journal
53396** file after power is restored. If an attempt is then made
53397** to roll the journal back, the database could be corrupted. The additional
53398** sanity checking data is an attempt to discover the garbage in the
53399** journal and ignore it.
53400**
53401** The sanity checking information for the new journal format consists
53402** of a 32-bit checksum on each page of data. The checksum covers both
53403** the page number and the pPager->pageSize bytes of data for the page.
53404** This cksum is initialized to a 32-bit random value that appears in the
53405** journal file right after the header. The random initializer is important,
53406** because garbage data that appears at the end of a journal is likely
53407** data that was once in other files that have now been deleted. If the
53408** garbage data came from an obsolete journal file, the checksums might
53409** be correct. But by initializing the checksum to random value which
53410** is different for every journal, we minimize that risk.
53411*/
53412static const unsigned char aJournalMagic[] = {
53413 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
53414};
53415
53416/*
53417** The size of the of each page record in the journal is given by
53418** the following macro.
53419*/
53420#define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
53421
53422/*
53423** The journal header size for this pager. This is usually the same
53424** size as a single disk sector. See also setSectorSize().
53425*/
53426#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
53427
53428/*
53429** The macro MEMDB is true if we are dealing with an in-memory database.
53430** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
53431** the value of MEMDB will be a constant and the compiler will optimize
53432** out code that would never execute.
53433*/
53434#ifdef SQLITE_OMIT_MEMORYDB
53435# define MEMDB 0
53436#else
53437# define MEMDB pPager->memDb
53438#endif
53439
53440/*
53441** The macro USEFETCH is true if we are allowed to use the xFetch and xUnfetch
53442** interfaces to access the database using memory-mapped I/O.
53443*/
53444#if SQLITE_MAX_MMAP_SIZE>0
53445# define USEFETCH(x) ((x)->bUseFetch)
53446#else
53447# define USEFETCH(x) 0
53448#endif
53449
53450/*
53451** The argument to this macro is a file descriptor (type sqlite3_file*).
53452** Return 0 if it is not open, or non-zero (but not 1) if it is.
53453**
53454** This is so that expressions can be written as:
53455**
53456** if( isOpen(pPager->jfd) ){ ...
53457**
53458** instead of
53459**
53460** if( pPager->jfd->pMethods ){ ...
53461*/
53462#define isOpen(pFd) ((pFd)->pMethods!=0)
53463
53464#ifdef SQLITE_DIRECT_OVERFLOW_READ
53465/*
53466** Return true if page pgno can be read directly from the database file
53467** by the b-tree layer. This is the case if:
53468**
53469** * the database file is open,
53470** * there are no dirty pages in the cache, and
53471** * the desired page is not currently in the wal file.
53472*/
53473SQLITE_PRIVATE int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno){
53474 if( pPager->fd->pMethods==0 ) return 0;
53475 if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0;
53476#ifndef SQLITE_OMIT_WAL
53477 if( pPager->pWal ){
53478 u32 iRead = 0;
53479 int rc;
53480 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
53481 return (rc==SQLITE_OK && iRead==0);
53482 }
53483#endif
53484 return 1;
53485}
53486#endif
53487
53488#ifndef SQLITE_OMIT_WAL
53489# define pagerUseWal(x) ((x)->pWal!=0)
53490#else
53491# define pagerUseWal(x) 0
53492# define pagerRollbackWal(x) 0
53493# define pagerWalFrames(v,w,x,y) 0
53494# define pagerOpenWalIfPresent(z) SQLITE_OK
53495# define pagerBeginReadTransaction(z) SQLITE_OK
53496#endif
53497
53498#ifndef NDEBUG
53499/*
53500** Usage:
53501**
53502** assert( assert_pager_state(pPager) );
53503**
53504** This function runs many asserts to try to find inconsistencies in
53505** the internal state of the Pager object.
53506*/
53507static int assert_pager_state(Pager *p){
53508 Pager *pPager = p;
53509
53510 /* State must be valid. */
53511 assert( p->eState==PAGER_OPEN
53512 || p->eState==PAGER_READER
53513 || p->eState==PAGER_WRITER_LOCKED
53514 || p->eState==PAGER_WRITER_CACHEMOD
53515 || p->eState==PAGER_WRITER_DBMOD
53516 || p->eState==PAGER_WRITER_FINISHED
53517 || p->eState==PAGER_ERROR
53518 );
53519
53520 /* Regardless of the current state, a temp-file connection always behaves
53521 ** as if it has an exclusive lock on the database file. It never updates
53522 ** the change-counter field, so the changeCountDone flag is always set.
53523 */
53524 assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
53525 assert( p->tempFile==0 || pPager->changeCountDone );
53526
53527 /* If the useJournal flag is clear, the journal-mode must be "OFF".
53528 ** And if the journal-mode is "OFF", the journal file must not be open.
53529 */
53530 assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal );
53531 assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) );
53532
53533 /* Check that MEMDB implies noSync. And an in-memory journal. Since
53534 ** this means an in-memory pager performs no IO at all, it cannot encounter
53535 ** either SQLITE_IOERR or SQLITE_FULL during rollback or while finalizing
53536 ** a journal file. (although the in-memory journal implementation may
53537 ** return SQLITE_IOERR_NOMEM while the journal file is being written). It
53538 ** is therefore not possible for an in-memory pager to enter the ERROR
53539 ** state.
53540 */
53541 if( MEMDB ){
53542 assert( !isOpen(p->fd) );
53543 assert( p->noSync );
53544 assert( p->journalMode==PAGER_JOURNALMODE_OFF
53545 || p->journalMode==PAGER_JOURNALMODE_MEMORY
53546 );
53547 assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
53548 assert( pagerUseWal(p)==0 );
53549 }
53550
53551 /* If changeCountDone is set, a RESERVED lock or greater must be held
53552 ** on the file.
53553 */
53554 assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
53555 assert( p->eLock!=PENDING_LOCK );
53556
53557 switch( p->eState ){
53558 case PAGER_OPEN:
53559 assert( !MEMDB );
53560 assert( pPager->errCode==SQLITE_OK );
53561 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
53562 break;
53563
53564 case PAGER_READER:
53565 assert( pPager->errCode==SQLITE_OK );
53566 assert( p->eLock!=UNKNOWN_LOCK );
53567 assert( p->eLock>=SHARED_LOCK );
53568 break;
53569
53570 case PAGER_WRITER_LOCKED:
53571 assert( p->eLock!=UNKNOWN_LOCK );
53572 assert( pPager->errCode==SQLITE_OK );
53573 if( !pagerUseWal(pPager) ){
53574 assert( p->eLock>=RESERVED_LOCK );
53575 }
53576 assert( pPager->dbSize==pPager->dbOrigSize );
53577 assert( pPager->dbOrigSize==pPager->dbFileSize );
53578 assert( pPager->dbOrigSize==pPager->dbHintSize );
53579 assert( pPager->setSuper==0 );
53580 break;
53581
53582 case PAGER_WRITER_CACHEMOD:
53583 assert( p->eLock!=UNKNOWN_LOCK );
53584 assert( pPager->errCode==SQLITE_OK );
53585 if( !pagerUseWal(pPager) ){
53586 /* It is possible that if journal_mode=wal here that neither the
53587 ** journal file nor the WAL file are open. This happens during
53588 ** a rollback transaction that switches from journal_mode=off
53589 ** to journal_mode=wal.
53590 */
53591 assert( p->eLock>=RESERVED_LOCK );
53592 assert( isOpen(p->jfd)
53593 || p->journalMode==PAGER_JOURNALMODE_OFF
53594 || p->journalMode==PAGER_JOURNALMODE_WAL
53595 );
53596 }
53597 assert( pPager->dbOrigSize==pPager->dbFileSize );
53598 assert( pPager->dbOrigSize==pPager->dbHintSize );
53599 break;
53600
53601 case PAGER_WRITER_DBMOD:
53602 assert( p->eLock==EXCLUSIVE_LOCK );
53603 assert( pPager->errCode==SQLITE_OK );
53604 assert( !pagerUseWal(pPager) );
53605 assert( p->eLock>=EXCLUSIVE_LOCK );
53606 assert( isOpen(p->jfd)
53607 || p->journalMode==PAGER_JOURNALMODE_OFF
53608 || p->journalMode==PAGER_JOURNALMODE_WAL
53609 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
53610 );
53611 assert( pPager->dbOrigSize<=pPager->dbHintSize );
53612 break;
53613
53614 case PAGER_WRITER_FINISHED:
53615 assert( p->eLock==EXCLUSIVE_LOCK );
53616 assert( pPager->errCode==SQLITE_OK );
53617 assert( !pagerUseWal(pPager) );
53618 assert( isOpen(p->jfd)
53619 || p->journalMode==PAGER_JOURNALMODE_OFF
53620 || p->journalMode==PAGER_JOURNALMODE_WAL
53621 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
53622 );
53623 break;
53624
53625 case PAGER_ERROR:
53626 /* There must be at least one outstanding reference to the pager if
53627 ** in ERROR state. Otherwise the pager should have already dropped
53628 ** back to OPEN state.
53629 */
53630 assert( pPager->errCode!=SQLITE_OK );
53631 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 || pPager->tempFile );
53632 break;
53633 }
53634
53635 return 1;
53636}
53637#endif /* ifndef NDEBUG */
53638
53639#ifdef SQLITE_DEBUG
53640/*
53641** Return a pointer to a human readable string in a static buffer
53642** containing the state of the Pager object passed as an argument. This
53643** is intended to be used within debuggers. For example, as an alternative
53644** to "print *pPager" in gdb:
53645**
53646** (gdb) printf "%s", print_pager_state(pPager)
53647**
53648** This routine has external linkage in order to suppress compiler warnings
53649** about an unused function. It is enclosed within SQLITE_DEBUG and so does
53650** not appear in normal builds.
53651*/
53652char *print_pager_state(Pager *p){
53653 static char zRet[1024];
53654
53655 sqlite3_snprintf(1024, zRet,
53656 "Filename: %s\n"
53657 "State: %s errCode=%d\n"
53658 "Lock: %s\n"
53659 "Locking mode: locking_mode=%s\n"
53660 "Journal mode: journal_mode=%s\n"
53661 "Backing store: tempFile=%d memDb=%d useJournal=%d\n"
53662 "Journal: journalOff=%lld journalHdr=%lld\n"
53663 "Size: dbsize=%d dbOrigSize=%d dbFileSize=%d\n"
53664 , p->zFilename
53665 , p->eState==PAGER_OPEN ? "OPEN" :
53666 p->eState==PAGER_READER ? "READER" :
53667 p->eState==PAGER_WRITER_LOCKED ? "WRITER_LOCKED" :
53668 p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
53669 p->eState==PAGER_WRITER_DBMOD ? "WRITER_DBMOD" :
53670 p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
53671 p->eState==PAGER_ERROR ? "ERROR" : "?error?"
53672 , (int)p->errCode
53673 , p->eLock==NO_LOCK ? "NO_LOCK" :
53674 p->eLock==RESERVED_LOCK ? "RESERVED" :
53675 p->eLock==EXCLUSIVE_LOCK ? "EXCLUSIVE" :
53676 p->eLock==SHARED_LOCK ? "SHARED" :
53677 p->eLock==UNKNOWN_LOCK ? "UNKNOWN" : "?error?"
53678 , p->exclusiveMode ? "exclusive" : "normal"
53679 , p->journalMode==PAGER_JOURNALMODE_MEMORY ? "memory" :
53680 p->journalMode==PAGER_JOURNALMODE_OFF ? "off" :
53681 p->journalMode==PAGER_JOURNALMODE_DELETE ? "delete" :
53682 p->journalMode==PAGER_JOURNALMODE_PERSIST ? "persist" :
53683 p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" :
53684 p->journalMode==PAGER_JOURNALMODE_WAL ? "wal" : "?error?"
53685 , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
53686 , p->journalOff, p->journalHdr
53687 , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
53688 );
53689
53690 return zRet;
53691}
53692#endif
53693
53694/* Forward references to the various page getters */
53695static int getPageNormal(Pager*,Pgno,DbPage**,int);
53696static int getPageError(Pager*,Pgno,DbPage**,int);
53697#if SQLITE_MAX_MMAP_SIZE>0
53698static int getPageMMap(Pager*,Pgno,DbPage**,int);
53699#endif
53700
53701/*
53702** Set the Pager.xGet method for the appropriate routine used to fetch
53703** content from the pager.
53704*/
53705static void setGetterMethod(Pager *pPager){
53706 if( pPager->errCode ){
53707 pPager->xGet = getPageError;
53708#if SQLITE_MAX_MMAP_SIZE>0
53709 }else if( USEFETCH(pPager) ){
53710 pPager->xGet = getPageMMap;
53711#endif /* SQLITE_MAX_MMAP_SIZE>0 */
53712 }else{
53713 pPager->xGet = getPageNormal;
53714 }
53715}
53716
53717/*
53718** Return true if it is necessary to write page *pPg into the sub-journal.
53719** A page needs to be written into the sub-journal if there exists one
53720** or more open savepoints for which:
53721**
53722** * The page-number is less than or equal to PagerSavepoint.nOrig, and
53723** * The bit corresponding to the page-number is not set in
53724** PagerSavepoint.pInSavepoint.
53725*/
53726static int subjRequiresPage(PgHdr *pPg){
53727 Pager *pPager = pPg->pPager;
53728 PagerSavepoint *p;
53729 Pgno pgno = pPg->pgno;
53730 int i;
53731 for(i=0; i<pPager->nSavepoint; i++){
53732 p = &pPager->aSavepoint[i];
53733 if( p->nOrig>=pgno && 0==sqlite3BitvecTestNotNull(p: p->pInSavepoint, i: pgno) ){
53734 for(i=i+1; i<pPager->nSavepoint; i++){
53735 pPager->aSavepoint[i].bTruncateOnRelease = 0;
53736 }
53737 return 1;
53738 }
53739 }
53740 return 0;
53741}
53742
53743#ifdef SQLITE_DEBUG
53744/*
53745** Return true if the page is already in the journal file.
53746*/
53747static int pageInJournal(Pager *pPager, PgHdr *pPg){
53748 return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
53749}
53750#endif
53751
53752/*
53753** Read a 32-bit integer from the given file descriptor. Store the integer
53754** that is read in *pRes. Return SQLITE_OK if everything worked, or an
53755** error code is something goes wrong.
53756**
53757** All values are stored on disk as big-endian.
53758*/
53759static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
53760 unsigned char ac[4];
53761 int rc = sqlite3OsRead(id: fd, pBuf: ac, amt: sizeof(ac), offset);
53762 if( rc==SQLITE_OK ){
53763 *pRes = sqlite3Get4byte(p: ac);
53764 }
53765 return rc;
53766}
53767
53768/*
53769** Write a 32-bit integer into a string buffer in big-endian byte order.
53770*/
53771#define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
53772
53773
53774/*
53775** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
53776** on success or an error code is something goes wrong.
53777*/
53778static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
53779 char ac[4];
53780 put32bits(ac, val);
53781 return sqlite3OsWrite(id: fd, pBuf: ac, amt: 4, offset);
53782}
53783
53784/*
53785** Unlock the database file to level eLock, which must be either NO_LOCK
53786** or SHARED_LOCK. Regardless of whether or not the call to xUnlock()
53787** succeeds, set the Pager.eLock variable to match the (attempted) new lock.
53788**
53789** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
53790** called, do not modify it. See the comment above the #define of
53791** UNKNOWN_LOCK for an explanation of this.
53792*/
53793static int pagerUnlockDb(Pager *pPager, int eLock){
53794 int rc = SQLITE_OK;
53795
53796 assert( !pPager->exclusiveMode || pPager->eLock==eLock );
53797 assert( eLock==NO_LOCK || eLock==SHARED_LOCK );
53798 assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 );
53799 if( isOpen(pPager->fd) ){
53800 assert( pPager->eLock>=eLock );
53801 rc = pPager->noLock ? SQLITE_OK : sqlite3OsUnlock(id: pPager->fd, lockType: eLock);
53802 if( pPager->eLock!=UNKNOWN_LOCK ){
53803 pPager->eLock = (u8)eLock;
53804 }
53805 IOTRACE(("UNLOCK %p %d\n", pPager, eLock))
53806 }
53807 pPager->changeCountDone = pPager->tempFile; /* ticket fb3b3024ea238d5c */
53808 return rc;
53809}
53810
53811/*
53812** Lock the database file to level eLock, which must be either SHARED_LOCK,
53813** RESERVED_LOCK or EXCLUSIVE_LOCK. If the caller is successful, set the
53814** Pager.eLock variable to the new locking state.
53815**
53816** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
53817** called, do not modify it unless the new locking state is EXCLUSIVE_LOCK.
53818** See the comment above the #define of UNKNOWN_LOCK for an explanation
53819** of this.
53820*/
53821static int pagerLockDb(Pager *pPager, int eLock){
53822 int rc = SQLITE_OK;
53823
53824 assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK );
53825 if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
53826 rc = pPager->noLock ? SQLITE_OK : sqlite3OsLock(id: pPager->fd, lockType: eLock);
53827 if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
53828 pPager->eLock = (u8)eLock;
53829 IOTRACE(("LOCK %p %d\n", pPager, eLock))
53830 }
53831 }
53832 return rc;
53833}
53834
53835/*
53836** This function determines whether or not the atomic-write or
53837** atomic-batch-write optimizations can be used with this pager. The
53838** atomic-write optimization can be used if:
53839**
53840** (a) the value returned by OsDeviceCharacteristics() indicates that
53841** a database page may be written atomically, and
53842** (b) the value returned by OsSectorSize() is less than or equal
53843** to the page size.
53844**
53845** If it can be used, then the value returned is the size of the journal
53846** file when it contains rollback data for exactly one page.
53847**
53848** The atomic-batch-write optimization can be used if OsDeviceCharacteristics()
53849** returns a value with the SQLITE_IOCAP_BATCH_ATOMIC bit set. -1 is
53850** returned in this case.
53851**
53852** If neither optimization can be used, 0 is returned.
53853*/
53854static int jrnlBufferSize(Pager *pPager){
53855 assert( !MEMDB );
53856
53857#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
53858 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
53859 int dc; /* Device characteristics */
53860
53861 assert( isOpen(pPager->fd) );
53862 dc = sqlite3OsDeviceCharacteristics(pPager->fd);
53863#else
53864 UNUSED_PARAMETER(pPager);
53865#endif
53866
53867#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
53868 if( pPager->dbSize>0 && (dc&SQLITE_IOCAP_BATCH_ATOMIC) ){
53869 return -1;
53870 }
53871#endif
53872
53873#ifdef SQLITE_ENABLE_ATOMIC_WRITE
53874 {
53875 int nSector = pPager->sectorSize;
53876 int szPage = pPager->pageSize;
53877
53878 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
53879 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
53880 if( 0==(dc&(SQLITE_IOCAP_ATOMIC|(szPage>>8)) || nSector>szPage) ){
53881 return 0;
53882 }
53883 }
53884
53885 return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
53886#endif
53887
53888 return 0;
53889}
53890
53891/*
53892** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
53893** on the cache using a hash function. This is used for testing
53894** and debugging only.
53895*/
53896#ifdef SQLITE_CHECK_PAGES
53897/*
53898** Return a 32-bit hash of the page data for pPage.
53899*/
53900static u32 pager_datahash(int nByte, unsigned char *pData){
53901 u32 hash = 0;
53902 int i;
53903 for(i=0; i<nByte; i++){
53904 hash = (hash*1039) + pData[i];
53905 }
53906 return hash;
53907}
53908static u32 pager_pagehash(PgHdr *pPage){
53909 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
53910}
53911static void pager_set_pagehash(PgHdr *pPage){
53912 pPage->pageHash = pager_pagehash(pPage);
53913}
53914
53915/*
53916** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
53917** is defined, and NDEBUG is not defined, an assert() statement checks
53918** that the page is either dirty or still matches the calculated page-hash.
53919*/
53920#define CHECK_PAGE(x) checkPage(x)
53921static void checkPage(PgHdr *pPg){
53922 Pager *pPager = pPg->pPager;
53923 assert( pPager->eState!=PAGER_ERROR );
53924 assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
53925}
53926
53927#else
53928#define pager_datahash(X,Y) 0
53929#define pager_pagehash(X) 0
53930#define pager_set_pagehash(X)
53931#define CHECK_PAGE(x)
53932#endif /* SQLITE_CHECK_PAGES */
53933
53934/*
53935** When this is called the journal file for pager pPager must be open.
53936** This function attempts to read a super-journal file name from the
53937** end of the file and, if successful, copies it into memory supplied
53938** by the caller. See comments above writeSuperJournal() for the format
53939** used to store a super-journal file name at the end of a journal file.
53940**
53941** zSuper must point to a buffer of at least nSuper bytes allocated by
53942** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
53943** enough space to write the super-journal name). If the super-journal
53944** name in the journal is longer than nSuper bytes (including a
53945** nul-terminator), then this is handled as if no super-journal name
53946** were present in the journal.
53947**
53948** If a super-journal file name is present at the end of the journal
53949** file, then it is copied into the buffer pointed to by zSuper. A
53950** nul-terminator byte is appended to the buffer following the
53951** super-journal file name.
53952**
53953** If it is determined that no super-journal file name is present
53954** zSuper[0] is set to 0 and SQLITE_OK returned.
53955**
53956** If an error occurs while reading from the journal file, an SQLite
53957** error code is returned.
53958*/
53959static int readSuperJournal(sqlite3_file *pJrnl, char *zSuper, u32 nSuper){
53960 int rc; /* Return code */
53961 u32 len; /* Length in bytes of super-journal name */
53962 i64 szJ; /* Total size in bytes of journal file pJrnl */
53963 u32 cksum; /* MJ checksum value read from journal */
53964 u32 u; /* Unsigned loop counter */
53965 unsigned char aMagic[8]; /* A buffer to hold the magic header */
53966 zSuper[0] = '\0';
53967
53968 if( SQLITE_OK!=(rc = sqlite3OsFileSize(id: pJrnl, pSize: &szJ))
53969 || szJ<16
53970 || SQLITE_OK!=(rc = read32bits(fd: pJrnl, offset: szJ-16, pRes: &len))
53971 || len>=nSuper
53972 || len>szJ-16
53973 || len==0
53974 || SQLITE_OK!=(rc = read32bits(fd: pJrnl, offset: szJ-12, pRes: &cksum))
53975 || SQLITE_OK!=(rc = sqlite3OsRead(id: pJrnl, pBuf: aMagic, amt: 8, offset: szJ-8))
53976 || memcmp(s1: aMagic, s2: aJournalMagic, n: 8)
53977 || SQLITE_OK!=(rc = sqlite3OsRead(id: pJrnl, pBuf: zSuper, amt: len, offset: szJ-16-len))
53978 ){
53979 return rc;
53980 }
53981
53982 /* See if the checksum matches the super-journal name */
53983 for(u=0; u<len; u++){
53984 cksum -= zSuper[u];
53985 }
53986 if( cksum ){
53987 /* If the checksum doesn't add up, then one or more of the disk sectors
53988 ** containing the super-journal filename is corrupted. This means
53989 ** definitely roll back, so just return SQLITE_OK and report a (nul)
53990 ** super-journal filename.
53991 */
53992 len = 0;
53993 }
53994 zSuper[len] = '\0';
53995 zSuper[len+1] = '\0';
53996
53997 return SQLITE_OK;
53998}
53999
54000/*
54001** Return the offset of the sector boundary at or immediately
54002** following the value in pPager->journalOff, assuming a sector
54003** size of pPager->sectorSize bytes.
54004**
54005** i.e for a sector size of 512:
54006**
54007** Pager.journalOff Return value
54008** ---------------------------------------
54009** 0 0
54010** 512 512
54011** 100 512
54012** 2000 2048
54013**
54014*/
54015static i64 journalHdrOffset(Pager *pPager){
54016 i64 offset = 0;
54017 i64 c = pPager->journalOff;
54018 if( c ){
54019 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
54020 }
54021 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
54022 assert( offset>=c );
54023 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
54024 return offset;
54025}
54026
54027/*
54028** The journal file must be open when this function is called.
54029**
54030** This function is a no-op if the journal file has not been written to
54031** within the current transaction (i.e. if Pager.journalOff==0).
54032**
54033** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
54034** set to 0, then truncate the journal file to zero bytes in size. Otherwise,
54035** zero the 28-byte header at the start of the journal file. In either case,
54036** if the pager is not in no-sync mode, sync the journal file immediately
54037** after writing or truncating it.
54038**
54039** If Pager.journalSizeLimit is set to a positive, non-zero value, and
54040** following the truncation or zeroing described above the size of the
54041** journal file in bytes is larger than this value, then truncate the
54042** journal file to Pager.journalSizeLimit bytes. The journal file does
54043** not need to be synced following this operation.
54044**
54045** If an IO error occurs, abandon processing and return the IO error code.
54046** Otherwise, return SQLITE_OK.
54047*/
54048static int zeroJournalHdr(Pager *pPager, int doTruncate){
54049 int rc = SQLITE_OK; /* Return code */
54050 assert( isOpen(pPager->jfd) );
54051 assert( !sqlite3JournalIsInMemory(pPager->jfd) );
54052 if( pPager->journalOff ){
54053 const i64 iLimit = pPager->journalSizeLimit; /* Local cache of jsl */
54054
54055 IOTRACE(("JZEROHDR %p\n", pPager))
54056 if( doTruncate || iLimit==0 ){
54057 rc = sqlite3OsTruncate(id: pPager->jfd, size: 0);
54058 }else{
54059 static const char zeroHdr[28] = {0};
54060 rc = sqlite3OsWrite(id: pPager->jfd, pBuf: zeroHdr, amt: sizeof(zeroHdr), offset: 0);
54061 }
54062 if( rc==SQLITE_OK && !pPager->noSync ){
54063 rc = sqlite3OsSync(id: pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags);
54064 }
54065
54066 /* At this point the transaction is committed but the write lock
54067 ** is still held on the file. If there is a size limit configured for
54068 ** the persistent journal and the journal file currently consumes more
54069 ** space than that limit allows for, truncate it now. There is no need
54070 ** to sync the file following this operation.
54071 */
54072 if( rc==SQLITE_OK && iLimit>0 ){
54073 i64 sz;
54074 rc = sqlite3OsFileSize(id: pPager->jfd, pSize: &sz);
54075 if( rc==SQLITE_OK && sz>iLimit ){
54076 rc = sqlite3OsTruncate(id: pPager->jfd, size: iLimit);
54077 }
54078 }
54079 }
54080 return rc;
54081}
54082
54083/*
54084** The journal file must be open when this routine is called. A journal
54085** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
54086** current location.
54087**
54088** The format for the journal header is as follows:
54089** - 8 bytes: Magic identifying journal format.
54090** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
54091** - 4 bytes: Random number used for page hash.
54092** - 4 bytes: Initial database page count.
54093** - 4 bytes: Sector size used by the process that wrote this journal.
54094** - 4 bytes: Database page size.
54095**
54096** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
54097*/
54098static int writeJournalHdr(Pager *pPager){
54099 int rc = SQLITE_OK; /* Return code */
54100 char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */
54101 u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
54102 u32 nWrite; /* Bytes of header sector written */
54103 int ii; /* Loop counter */
54104
54105 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
54106
54107 if( nHeader>JOURNAL_HDR_SZ(pPager) ){
54108 nHeader = JOURNAL_HDR_SZ(pPager);
54109 }
54110
54111 /* If there are active savepoints and any of them were created
54112 ** since the most recent journal header was written, update the
54113 ** PagerSavepoint.iHdrOffset fields now.
54114 */
54115 for(ii=0; ii<pPager->nSavepoint; ii++){
54116 if( pPager->aSavepoint[ii].iHdrOffset==0 ){
54117 pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
54118 }
54119 }
54120
54121 pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
54122
54123 /*
54124 ** Write the nRec Field - the number of page records that follow this
54125 ** journal header. Normally, zero is written to this value at this time.
54126 ** After the records are added to the journal (and the journal synced,
54127 ** if in full-sync mode), the zero is overwritten with the true number
54128 ** of records (see syncJournal()).
54129 **
54130 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
54131 ** reading the journal this value tells SQLite to assume that the
54132 ** rest of the journal file contains valid page records. This assumption
54133 ** is dangerous, as if a failure occurred whilst writing to the journal
54134 ** file it may contain some garbage data. There are two scenarios
54135 ** where this risk can be ignored:
54136 **
54137 ** * When the pager is in no-sync mode. Corruption can follow a
54138 ** power failure in this case anyway.
54139 **
54140 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
54141 ** that garbage data is never appended to the journal file.
54142 */
54143 assert( isOpen(pPager->fd) || pPager->noSync );
54144 if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
54145 || (sqlite3OsDeviceCharacteristics(id: pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
54146 ){
54147 memcpy(dest: zHeader, src: aJournalMagic, n: sizeof(aJournalMagic));
54148 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
54149 }else{
54150 memset(s: zHeader, c: 0, n: sizeof(aJournalMagic)+4);
54151 }
54152
54153 /* The random check-hash initializer */
54154 sqlite3_randomness(N: sizeof(pPager->cksumInit), pBuf: &pPager->cksumInit);
54155 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
54156 /* The initial database size */
54157 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
54158 /* The assumed sector size for this process */
54159 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
54160
54161 /* The page size */
54162 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
54163
54164 /* Initializing the tail of the buffer is not necessary. Everything
54165 ** works find if the following memset() is omitted. But initializing
54166 ** the memory prevents valgrind from complaining, so we are willing to
54167 ** take the performance hit.
54168 */
54169 memset(s: &zHeader[sizeof(aJournalMagic)+20], c: 0,
54170 n: nHeader-(sizeof(aJournalMagic)+20));
54171
54172 /* In theory, it is only necessary to write the 28 bytes that the
54173 ** journal header consumes to the journal file here. Then increment the
54174 ** Pager.journalOff variable by JOURNAL_HDR_SZ so that the next
54175 ** record is written to the following sector (leaving a gap in the file
54176 ** that will be implicitly filled in by the OS).
54177 **
54178 ** However it has been discovered that on some systems this pattern can
54179 ** be significantly slower than contiguously writing data to the file,
54180 ** even if that means explicitly writing data to the block of
54181 ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
54182 ** is done.
54183 **
54184 ** The loop is required here in case the sector-size is larger than the
54185 ** database page size. Since the zHeader buffer is only Pager.pageSize
54186 ** bytes in size, more than one call to sqlite3OsWrite() may be required
54187 ** to populate the entire journal header sector.
54188 */
54189 for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
54190 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
54191 rc = sqlite3OsWrite(id: pPager->jfd, pBuf: zHeader, amt: nHeader, offset: pPager->journalOff);
54192 assert( pPager->journalHdr <= pPager->journalOff );
54193 pPager->journalOff += nHeader;
54194 }
54195
54196 return rc;
54197}
54198
54199/*
54200** The journal file must be open when this is called. A journal header file
54201** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
54202** file. The current location in the journal file is given by
54203** pPager->journalOff. See comments above function writeJournalHdr() for
54204** a description of the journal header format.
54205**
54206** If the header is read successfully, *pNRec is set to the number of
54207** page records following this header and *pDbSize is set to the size of the
54208** database before the transaction began, in pages. Also, pPager->cksumInit
54209** is set to the value read from the journal header. SQLITE_OK is returned
54210** in this case.
54211**
54212** If the journal header file appears to be corrupted, SQLITE_DONE is
54213** returned and *pNRec and *PDbSize are undefined. If JOURNAL_HDR_SZ bytes
54214** cannot be read from the journal file an error code is returned.
54215*/
54216static int readJournalHdr(
54217 Pager *pPager, /* Pager object */
54218 int isHot,
54219 i64 journalSize, /* Size of the open journal file in bytes */
54220 u32 *pNRec, /* OUT: Value read from the nRec field */
54221 u32 *pDbSize /* OUT: Value of original database size field */
54222){
54223 int rc; /* Return code */
54224 unsigned char aMagic[8]; /* A buffer to hold the magic header */
54225 i64 iHdrOff; /* Offset of journal header being read */
54226
54227 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
54228
54229 /* Advance Pager.journalOff to the start of the next sector. If the
54230 ** journal file is too small for there to be a header stored at this
54231 ** point, return SQLITE_DONE.
54232 */
54233 pPager->journalOff = journalHdrOffset(pPager);
54234 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
54235 return SQLITE_DONE;
54236 }
54237 iHdrOff = pPager->journalOff;
54238
54239 /* Read in the first 8 bytes of the journal header. If they do not match
54240 ** the magic string found at the start of each journal header, return
54241 ** SQLITE_DONE. If an IO error occurs, return an error code. Otherwise,
54242 ** proceed.
54243 */
54244 if( isHot || iHdrOff!=pPager->journalHdr ){
54245 rc = sqlite3OsRead(id: pPager->jfd, pBuf: aMagic, amt: sizeof(aMagic), offset: iHdrOff);
54246 if( rc ){
54247 return rc;
54248 }
54249 if( memcmp(s1: aMagic, s2: aJournalMagic, n: sizeof(aMagic))!=0 ){
54250 return SQLITE_DONE;
54251 }
54252 }
54253
54254 /* Read the first three 32-bit fields of the journal header: The nRec
54255 ** field, the checksum-initializer and the database size at the start
54256 ** of the transaction. Return an error code if anything goes wrong.
54257 */
54258 if( SQLITE_OK!=(rc = read32bits(fd: pPager->jfd, offset: iHdrOff+8, pRes: pNRec))
54259 || SQLITE_OK!=(rc = read32bits(fd: pPager->jfd, offset: iHdrOff+12, pRes: &pPager->cksumInit))
54260 || SQLITE_OK!=(rc = read32bits(fd: pPager->jfd, offset: iHdrOff+16, pRes: pDbSize))
54261 ){
54262 return rc;
54263 }
54264
54265 if( pPager->journalOff==0 ){
54266 u32 iPageSize; /* Page-size field of journal header */
54267 u32 iSectorSize; /* Sector-size field of journal header */
54268
54269 /* Read the page-size and sector-size journal header fields. */
54270 if( SQLITE_OK!=(rc = read32bits(fd: pPager->jfd, offset: iHdrOff+20, pRes: &iSectorSize))
54271 || SQLITE_OK!=(rc = read32bits(fd: pPager->jfd, offset: iHdrOff+24, pRes: &iPageSize))
54272 ){
54273 return rc;
54274 }
54275
54276 /* Versions of SQLite prior to 3.5.8 set the page-size field of the
54277 ** journal header to zero. In this case, assume that the Pager.pageSize
54278 ** variable is already set to the correct page size.
54279 */
54280 if( iPageSize==0 ){
54281 iPageSize = pPager->pageSize;
54282 }
54283
54284 /* Check that the values read from the page-size and sector-size fields
54285 ** are within range. To be 'in range', both values need to be a power
54286 ** of two greater than or equal to 512 or 32, and not greater than their
54287 ** respective compile time maximum limits.
54288 */
54289 if( iPageSize<512 || iSectorSize<32
54290 || iPageSize>SQLITE_MAX_PAGE_SIZE || iSectorSize>MAX_SECTOR_SIZE
54291 || ((iPageSize-1)&iPageSize)!=0 || ((iSectorSize-1)&iSectorSize)!=0
54292 ){
54293 /* If the either the page-size or sector-size in the journal-header is
54294 ** invalid, then the process that wrote the journal-header must have
54295 ** crashed before the header was synced. In this case stop reading
54296 ** the journal file here.
54297 */
54298 return SQLITE_DONE;
54299 }
54300
54301 /* Update the page-size to match the value read from the journal.
54302 ** Use a testcase() macro to make sure that malloc failure within
54303 ** PagerSetPagesize() is tested.
54304 */
54305 rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
54306 testcase( rc!=SQLITE_OK );
54307
54308 /* Update the assumed sector-size to match the value used by
54309 ** the process that created this journal. If this journal was
54310 ** created by a process other than this one, then this routine
54311 ** is being called from within pager_playback(). The local value
54312 ** of Pager.sectorSize is restored at the end of that routine.
54313 */
54314 pPager->sectorSize = iSectorSize;
54315 }
54316
54317 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
54318 return rc;
54319}
54320
54321
54322/*
54323** Write the supplied super-journal name into the journal file for pager
54324** pPager at the current location. The super-journal name must be the last
54325** thing written to a journal file. If the pager is in full-sync mode, the
54326** journal file descriptor is advanced to the next sector boundary before
54327** anything is written. The format is:
54328**
54329** + 4 bytes: PAGER_MJ_PGNO.
54330** + N bytes: super-journal filename in utf-8.
54331** + 4 bytes: N (length of super-journal name in bytes, no nul-terminator).
54332** + 4 bytes: super-journal name checksum.
54333** + 8 bytes: aJournalMagic[].
54334**
54335** The super-journal page checksum is the sum of the bytes in thesuper-journal
54336** name, where each byte is interpreted as a signed 8-bit integer.
54337**
54338** If zSuper is a NULL pointer (occurs for a single database transaction),
54339** this call is a no-op.
54340*/
54341static int writeSuperJournal(Pager *pPager, const char *zSuper){
54342 int rc; /* Return code */
54343 int nSuper; /* Length of string zSuper */
54344 i64 iHdrOff; /* Offset of header in journal file */
54345 i64 jrnlSize; /* Size of journal file on disk */
54346 u32 cksum = 0; /* Checksum of string zSuper */
54347
54348 assert( pPager->setSuper==0 );
54349 assert( !pagerUseWal(pPager) );
54350
54351 if( !zSuper
54352 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
54353 || !isOpen(pPager->jfd)
54354 ){
54355 return SQLITE_OK;
54356 }
54357 pPager->setSuper = 1;
54358 assert( pPager->journalHdr <= pPager->journalOff );
54359
54360 /* Calculate the length in bytes and the checksum of zSuper */
54361 for(nSuper=0; zSuper[nSuper]; nSuper++){
54362 cksum += zSuper[nSuper];
54363 }
54364
54365 /* If in full-sync mode, advance to the next disk sector before writing
54366 ** the super-journal name. This is in case the previous page written to
54367 ** the journal has already been synced.
54368 */
54369 if( pPager->fullSync ){
54370 pPager->journalOff = journalHdrOffset(pPager);
54371 }
54372 iHdrOff = pPager->journalOff;
54373
54374 /* Write the super-journal data to the end of the journal file. If
54375 ** an error occurs, return the error code to the caller.
54376 */
54377 if( (0 != (rc = write32bits(fd: pPager->jfd, offset: iHdrOff, PAGER_MJ_PGNO(pPager))))
54378 || (0 != (rc = sqlite3OsWrite(id: pPager->jfd, pBuf: zSuper, amt: nSuper, offset: iHdrOff+4)))
54379 || (0 != (rc = write32bits(fd: pPager->jfd, offset: iHdrOff+4+nSuper, val: nSuper)))
54380 || (0 != (rc = write32bits(fd: pPager->jfd, offset: iHdrOff+4+nSuper+4, val: cksum)))
54381 || (0 != (rc = sqlite3OsWrite(id: pPager->jfd, pBuf: aJournalMagic, amt: 8,
54382 offset: iHdrOff+4+nSuper+8)))
54383 ){
54384 return rc;
54385 }
54386 pPager->journalOff += (nSuper+20);
54387
54388 /* If the pager is in peristent-journal mode, then the physical
54389 ** journal-file may extend past the end of the super-journal name
54390 ** and 8 bytes of magic data just written to the file. This is
54391 ** dangerous because the code to rollback a hot-journal file
54392 ** will not be able to find the super-journal name to determine
54393 ** whether or not the journal is hot.
54394 **
54395 ** Easiest thing to do in this scenario is to truncate the journal
54396 ** file to the required size.
54397 */
54398 if( SQLITE_OK==(rc = sqlite3OsFileSize(id: pPager->jfd, pSize: &jrnlSize))
54399 && jrnlSize>pPager->journalOff
54400 ){
54401 rc = sqlite3OsTruncate(id: pPager->jfd, size: pPager->journalOff);
54402 }
54403 return rc;
54404}
54405
54406/*
54407** Discard the entire contents of the in-memory page-cache.
54408*/
54409static void pager_reset(Pager *pPager){
54410 pPager->iDataVersion++;
54411 sqlite3BackupRestart(pPager->pBackup);
54412 sqlite3PcacheClear(pCache: pPager->pPCache);
54413}
54414
54415/*
54416** Return the pPager->iDataVersion value
54417*/
54418SQLITE_PRIVATE u32 sqlite3PagerDataVersion(Pager *pPager){
54419 return pPager->iDataVersion;
54420}
54421
54422/*
54423** Free all structures in the Pager.aSavepoint[] array and set both
54424** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
54425** if it is open and the pager is not in exclusive mode.
54426*/
54427static void releaseAllSavepoints(Pager *pPager){
54428 int ii; /* Iterator for looping through Pager.aSavepoint */
54429 for(ii=0; ii<pPager->nSavepoint; ii++){
54430 sqlite3BitvecDestroy(p: pPager->aSavepoint[ii].pInSavepoint);
54431 }
54432 if( !pPager->exclusiveMode || sqlite3JournalIsInMemory(p: pPager->sjfd) ){
54433 sqlite3OsClose(pId: pPager->sjfd);
54434 }
54435 sqlite3_free(p: pPager->aSavepoint);
54436 pPager->aSavepoint = 0;
54437 pPager->nSavepoint = 0;
54438 pPager->nSubRec = 0;
54439}
54440
54441/*
54442** Set the bit number pgno in the PagerSavepoint.pInSavepoint
54443** bitvecs of all open savepoints. Return SQLITE_OK if successful
54444** or SQLITE_NOMEM if a malloc failure occurs.
54445*/
54446static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){
54447 int ii; /* Loop counter */
54448 int rc = SQLITE_OK; /* Result code */
54449
54450 for(ii=0; ii<pPager->nSavepoint; ii++){
54451 PagerSavepoint *p = &pPager->aSavepoint[ii];
54452 if( pgno<=p->nOrig ){
54453 rc |= sqlite3BitvecSet(p: p->pInSavepoint, i: pgno);
54454 testcase( rc==SQLITE_NOMEM );
54455 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
54456 }
54457 }
54458 return rc;
54459}
54460
54461/*
54462** This function is a no-op if the pager is in exclusive mode and not
54463** in the ERROR state. Otherwise, it switches the pager to PAGER_OPEN
54464** state.
54465**
54466** If the pager is not in exclusive-access mode, the database file is
54467** completely unlocked. If the file is unlocked and the file-system does
54468** not exhibit the UNDELETABLE_WHEN_OPEN property, the journal file is
54469** closed (if it is open).
54470**
54471** If the pager is in ERROR state when this function is called, the
54472** contents of the pager cache are discarded before switching back to
54473** the OPEN state. Regardless of whether the pager is in exclusive-mode
54474** or not, any journal file left in the file-system will be treated
54475** as a hot-journal and rolled back the next time a read-transaction
54476** is opened (by this or by any other connection).
54477*/
54478static void pager_unlock(Pager *pPager){
54479
54480 assert( pPager->eState==PAGER_READER
54481 || pPager->eState==PAGER_OPEN
54482 || pPager->eState==PAGER_ERROR
54483 );
54484
54485 sqlite3BitvecDestroy(p: pPager->pInJournal);
54486 pPager->pInJournal = 0;
54487 releaseAllSavepoints(pPager);
54488
54489 if( pagerUseWal(pPager) ){
54490 assert( !isOpen(pPager->jfd) );
54491 sqlite3WalEndReadTransaction(pWal: pPager->pWal);
54492 pPager->eState = PAGER_OPEN;
54493 }else if( !pPager->exclusiveMode ){
54494 int rc; /* Error code returned by pagerUnlockDb() */
54495 int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(id: pPager->fd):0;
54496
54497 /* If the operating system support deletion of open files, then
54498 ** close the journal file when dropping the database lock. Otherwise
54499 ** another connection with journal_mode=delete might delete the file
54500 ** out from under us.
54501 */
54502 assert( (PAGER_JOURNALMODE_MEMORY & 5)!=1 );
54503 assert( (PAGER_JOURNALMODE_OFF & 5)!=1 );
54504 assert( (PAGER_JOURNALMODE_WAL & 5)!=1 );
54505 assert( (PAGER_JOURNALMODE_DELETE & 5)!=1 );
54506 assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
54507 assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
54508 if( 0==(iDc & SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN)
54509 || 1!=(pPager->journalMode & 5)
54510 ){
54511 sqlite3OsClose(pId: pPager->jfd);
54512 }
54513
54514 /* If the pager is in the ERROR state and the call to unlock the database
54515 ** file fails, set the current lock to UNKNOWN_LOCK. See the comment
54516 ** above the #define for UNKNOWN_LOCK for an explanation of why this
54517 ** is necessary.
54518 */
54519 rc = pagerUnlockDb(pPager, NO_LOCK);
54520 if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
54521 pPager->eLock = UNKNOWN_LOCK;
54522 }
54523
54524 /* The pager state may be changed from PAGER_ERROR to PAGER_OPEN here
54525 ** without clearing the error code. This is intentional - the error
54526 ** code is cleared and the cache reset in the block below.
54527 */
54528 assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
54529 pPager->eState = PAGER_OPEN;
54530 }
54531
54532 /* If Pager.errCode is set, the contents of the pager cache cannot be
54533 ** trusted. Now that there are no outstanding references to the pager,
54534 ** it can safely move back to PAGER_OPEN state. This happens in both
54535 ** normal and exclusive-locking mode.
54536 */
54537 assert( pPager->errCode==SQLITE_OK || !MEMDB );
54538 if( pPager->errCode ){
54539 if( pPager->tempFile==0 ){
54540 pager_reset(pPager);
54541 pPager->changeCountDone = 0;
54542 pPager->eState = PAGER_OPEN;
54543 }else{
54544 pPager->eState = (isOpen(pPager->jfd) ? PAGER_OPEN : PAGER_READER);
54545 }
54546 if( USEFETCH(pPager) ) sqlite3OsUnfetch(id: pPager->fd, iOff: 0, p: 0);
54547 pPager->errCode = SQLITE_OK;
54548 setGetterMethod(pPager);
54549 }
54550
54551 pPager->journalOff = 0;
54552 pPager->journalHdr = 0;
54553 pPager->setSuper = 0;
54554}
54555
54556/*
54557** This function is called whenever an IOERR or FULL error that requires
54558** the pager to transition into the ERROR state may ahve occurred.
54559** The first argument is a pointer to the pager structure, the second
54560** the error-code about to be returned by a pager API function. The
54561** value returned is a copy of the second argument to this function.
54562**
54563** If the second argument is SQLITE_FULL, SQLITE_IOERR or one of the
54564** IOERR sub-codes, the pager enters the ERROR state and the error code
54565** is stored in Pager.errCode. While the pager remains in the ERROR state,
54566** all major API calls on the Pager will immediately return Pager.errCode.
54567**
54568** The ERROR state indicates that the contents of the pager-cache
54569** cannot be trusted. This state can be cleared by completely discarding
54570** the contents of the pager-cache. If a transaction was active when
54571** the persistent error occurred, then the rollback journal may need
54572** to be replayed to restore the contents of the database file (as if
54573** it were a hot-journal).
54574*/
54575static int pager_error(Pager *pPager, int rc){
54576 int rc2 = rc & 0xff;
54577 assert( rc==SQLITE_OK || !MEMDB );
54578 assert(
54579 pPager->errCode==SQLITE_FULL ||
54580 pPager->errCode==SQLITE_OK ||
54581 (pPager->errCode & 0xff)==SQLITE_IOERR
54582 );
54583 if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){
54584 pPager->errCode = rc;
54585 pPager->eState = PAGER_ERROR;
54586 setGetterMethod(pPager);
54587 }
54588 return rc;
54589}
54590
54591static int pager_truncate(Pager *pPager, Pgno nPage);
54592
54593/*
54594** The write transaction open on pPager is being committed (bCommit==1)
54595** or rolled back (bCommit==0).
54596**
54597** Return TRUE if and only if all dirty pages should be flushed to disk.
54598**
54599** Rules:
54600**
54601** * For non-TEMP databases, always sync to disk. This is necessary
54602** for transactions to be durable.
54603**
54604** * Sync TEMP database only on a COMMIT (not a ROLLBACK) when the backing
54605** file has been created already (via a spill on pagerStress()) and
54606** when the number of dirty pages in memory exceeds 25% of the total
54607** cache size.
54608*/
54609static int pagerFlushOnCommit(Pager *pPager, int bCommit){
54610 if( pPager->tempFile==0 ) return 1;
54611 if( !bCommit ) return 0;
54612 if( !isOpen(pPager->fd) ) return 0;
54613 return (sqlite3PCachePercentDirty(pCache: pPager->pPCache)>=25);
54614}
54615
54616/*
54617** This routine ends a transaction. A transaction is usually ended by
54618** either a COMMIT or a ROLLBACK operation. This routine may be called
54619** after rollback of a hot-journal, or if an error occurs while opening
54620** the journal file or writing the very first journal-header of a
54621** database transaction.
54622**
54623** This routine is never called in PAGER_ERROR state. If it is called
54624** in PAGER_NONE or PAGER_SHARED state and the lock held is less
54625** exclusive than a RESERVED lock, it is a no-op.
54626**
54627** Otherwise, any active savepoints are released.
54628**
54629** If the journal file is open, then it is "finalized". Once a journal
54630** file has been finalized it is not possible to use it to roll back a
54631** transaction. Nor will it be considered to be a hot-journal by this
54632** or any other database connection. Exactly how a journal is finalized
54633** depends on whether or not the pager is running in exclusive mode and
54634** the current journal-mode (Pager.journalMode value), as follows:
54635**
54636** journalMode==MEMORY
54637** Journal file descriptor is simply closed. This destroys an
54638** in-memory journal.
54639**
54640** journalMode==TRUNCATE
54641** Journal file is truncated to zero bytes in size.
54642**
54643** journalMode==PERSIST
54644** The first 28 bytes of the journal file are zeroed. This invalidates
54645** the first journal header in the file, and hence the entire journal
54646** file. An invalid journal file cannot be rolled back.
54647**
54648** journalMode==DELETE
54649** The journal file is closed and deleted using sqlite3OsDelete().
54650**
54651** If the pager is running in exclusive mode, this method of finalizing
54652** the journal file is never used. Instead, if the journalMode is
54653** DELETE and the pager is in exclusive mode, the method described under
54654** journalMode==PERSIST is used instead.
54655**
54656** After the journal is finalized, the pager moves to PAGER_READER state.
54657** If running in non-exclusive rollback mode, the lock on the file is
54658** downgraded to a SHARED_LOCK.
54659**
54660** SQLITE_OK is returned if no error occurs. If an error occurs during
54661** any of the IO operations to finalize the journal file or unlock the
54662** database then the IO error code is returned to the user. If the
54663** operation to finalize the journal file fails, then the code still
54664** tries to unlock the database file if not in exclusive mode. If the
54665** unlock operation fails as well, then the first error code related
54666** to the first error encountered (the journal finalization one) is
54667** returned.
54668*/
54669static int pager_end_transaction(Pager *pPager, int hasSuper, int bCommit){
54670 int rc = SQLITE_OK; /* Error code from journal finalization operation */
54671 int rc2 = SQLITE_OK; /* Error code from db file unlock operation */
54672
54673 /* Do nothing if the pager does not have an open write transaction
54674 ** or at least a RESERVED lock. This function may be called when there
54675 ** is no write-transaction active but a RESERVED or greater lock is
54676 ** held under two circumstances:
54677 **
54678 ** 1. After a successful hot-journal rollback, it is called with
54679 ** eState==PAGER_NONE and eLock==EXCLUSIVE_LOCK.
54680 **
54681 ** 2. If a connection with locking_mode=exclusive holding an EXCLUSIVE
54682 ** lock switches back to locking_mode=normal and then executes a
54683 ** read-transaction, this function is called with eState==PAGER_READER
54684 ** and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
54685 */
54686 assert( assert_pager_state(pPager) );
54687 assert( pPager->eState!=PAGER_ERROR );
54688 if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
54689 return SQLITE_OK;
54690 }
54691
54692 releaseAllSavepoints(pPager);
54693 assert( isOpen(pPager->jfd) || pPager->pInJournal==0
54694 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
54695 );
54696 if( isOpen(pPager->jfd) ){
54697 assert( !pagerUseWal(pPager) );
54698
54699 /* Finalize the journal file. */
54700 if( sqlite3JournalIsInMemory(p: pPager->jfd) ){
54701 /* assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); */
54702 sqlite3OsClose(pId: pPager->jfd);
54703 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
54704 if( pPager->journalOff==0 ){
54705 rc = SQLITE_OK;
54706 }else{
54707 rc = sqlite3OsTruncate(id: pPager->jfd, size: 0);
54708 if( rc==SQLITE_OK && pPager->fullSync ){
54709 /* Make sure the new file size is written into the inode right away.
54710 ** Otherwise the journal might resurrect following a power loss and
54711 ** cause the last transaction to roll back. See
54712 ** https://bugzilla.mozilla.org/show_bug.cgi?id=1072773
54713 */
54714 rc = sqlite3OsSync(id: pPager->jfd, flags: pPager->syncFlags);
54715 }
54716 }
54717 pPager->journalOff = 0;
54718 }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
54719 || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
54720 ){
54721 rc = zeroJournalHdr(pPager, doTruncate: hasSuper||pPager->tempFile);
54722 pPager->journalOff = 0;
54723 }else{
54724 /* This branch may be executed with Pager.journalMode==MEMORY if
54725 ** a hot-journal was just rolled back. In this case the journal
54726 ** file should be closed and deleted. If this connection writes to
54727 ** the database file, it will do so using an in-memory journal.
54728 */
54729 int bDelete = !pPager->tempFile;
54730 assert( sqlite3JournalIsInMemory(pPager->jfd)==0 );
54731 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE
54732 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
54733 || pPager->journalMode==PAGER_JOURNALMODE_WAL
54734 );
54735 sqlite3OsClose(pId: pPager->jfd);
54736 if( bDelete ){
54737 rc = sqlite3OsDelete(pVfs: pPager->pVfs, zPath: pPager->zJournal, dirSync: pPager->extraSync);
54738 }
54739 }
54740 }
54741
54742#ifdef SQLITE_CHECK_PAGES
54743 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
54744 if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
54745 PgHdr *p = sqlite3PagerLookup(pPager, 1);
54746 if( p ){
54747 p->pageHash = 0;
54748 sqlite3PagerUnrefNotNull(p);
54749 }
54750 }
54751#endif
54752
54753 sqlite3BitvecDestroy(p: pPager->pInJournal);
54754 pPager->pInJournal = 0;
54755 pPager->nRec = 0;
54756 if( rc==SQLITE_OK ){
54757 if( MEMDB || pagerFlushOnCommit(pPager, bCommit) ){
54758 sqlite3PcacheCleanAll(pCache: pPager->pPCache);
54759 }else{
54760 sqlite3PcacheClearWritable(pCache: pPager->pPCache);
54761 }
54762 sqlite3PcacheTruncate(pCache: pPager->pPCache, pgno: pPager->dbSize);
54763 }
54764
54765 if( pagerUseWal(pPager) ){
54766 /* Drop the WAL write-lock, if any. Also, if the connection was in
54767 ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE
54768 ** lock held on the database file.
54769 */
54770 rc2 = sqlite3WalEndWriteTransaction(pWal: pPager->pWal);
54771 assert( rc2==SQLITE_OK );
54772 }else if( rc==SQLITE_OK && bCommit && pPager->dbFileSize>pPager->dbSize ){
54773 /* This branch is taken when committing a transaction in rollback-journal
54774 ** mode if the database file on disk is larger than the database image.
54775 ** At this point the journal has been finalized and the transaction
54776 ** successfully committed, but the EXCLUSIVE lock is still held on the
54777 ** file. So it is safe to truncate the database file to its minimum
54778 ** required size. */
54779 assert( pPager->eLock==EXCLUSIVE_LOCK );
54780 rc = pager_truncate(pPager, nPage: pPager->dbSize);
54781 }
54782
54783 if( rc==SQLITE_OK && bCommit ){
54784 rc = sqlite3OsFileControl(id: pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, pArg: 0);
54785 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
54786 }
54787
54788 if( !pPager->exclusiveMode
54789 && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pWal: pPager->pWal, op: 0))
54790 ){
54791 rc2 = pagerUnlockDb(pPager, SHARED_LOCK);
54792 }
54793 pPager->eState = PAGER_READER;
54794 pPager->setSuper = 0;
54795
54796 return (rc==SQLITE_OK?rc2:rc);
54797}
54798
54799/*
54800** Execute a rollback if a transaction is active and unlock the
54801** database file.
54802**
54803** If the pager has already entered the ERROR state, do not attempt
54804** the rollback at this time. Instead, pager_unlock() is called. The
54805** call to pager_unlock() will discard all in-memory pages, unlock
54806** the database file and move the pager back to OPEN state. If this
54807** means that there is a hot-journal left in the file-system, the next
54808** connection to obtain a shared lock on the pager (which may be this one)
54809** will roll it back.
54810**
54811** If the pager has not already entered the ERROR state, but an IO or
54812** malloc error occurs during a rollback, then this will itself cause
54813** the pager to enter the ERROR state. Which will be cleared by the
54814** call to pager_unlock(), as described above.
54815*/
54816static void pagerUnlockAndRollback(Pager *pPager){
54817 if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
54818 assert( assert_pager_state(pPager) );
54819 if( pPager->eState>=PAGER_WRITER_LOCKED ){
54820 sqlite3BeginBenignMalloc();
54821 sqlite3PagerRollback(pPager);
54822 sqlite3EndBenignMalloc();
54823 }else if( !pPager->exclusiveMode ){
54824 assert( pPager->eState==PAGER_READER );
54825 pager_end_transaction(pPager, hasSuper: 0, bCommit: 0);
54826 }
54827 }
54828 pager_unlock(pPager);
54829}
54830
54831/*
54832** Parameter aData must point to a buffer of pPager->pageSize bytes
54833** of data. Compute and return a checksum based ont the contents of the
54834** page of data and the current value of pPager->cksumInit.
54835**
54836** This is not a real checksum. It is really just the sum of the
54837** random initial value (pPager->cksumInit) and every 200th byte
54838** of the page data, starting with byte offset (pPager->pageSize%200).
54839** Each byte is interpreted as an 8-bit unsigned integer.
54840**
54841** Changing the formula used to compute this checksum results in an
54842** incompatible journal file format.
54843**
54844** If journal corruption occurs due to a power failure, the most likely
54845** scenario is that one end or the other of the record will be changed.
54846** It is much less likely that the two ends of the journal record will be
54847** correct and the middle be corrupt. Thus, this "checksum" scheme,
54848** though fast and simple, catches the mostly likely kind of corruption.
54849*/
54850static u32 pager_cksum(Pager *pPager, const u8 *aData){
54851 u32 cksum = pPager->cksumInit; /* Checksum value to return */
54852 int i = pPager->pageSize-200; /* Loop counter */
54853 while( i>0 ){
54854 cksum += aData[i];
54855 i -= 200;
54856 }
54857 return cksum;
54858}
54859
54860/*
54861** Read a single page from either the journal file (if isMainJrnl==1) or
54862** from the sub-journal (if isMainJrnl==0) and playback that page.
54863** The page begins at offset *pOffset into the file. The *pOffset
54864** value is increased to the start of the next page in the journal.
54865**
54866** The main rollback journal uses checksums - the statement journal does
54867** not.
54868**
54869** If the page number of the page record read from the (sub-)journal file
54870** is greater than the current value of Pager.dbSize, then playback is
54871** skipped and SQLITE_OK is returned.
54872**
54873** If pDone is not NULL, then it is a record of pages that have already
54874** been played back. If the page at *pOffset has already been played back
54875** (if the corresponding pDone bit is set) then skip the playback.
54876** Make sure the pDone bit corresponding to the *pOffset page is set
54877** prior to returning.
54878**
54879** If the page record is successfully read from the (sub-)journal file
54880** and played back, then SQLITE_OK is returned. If an IO error occurs
54881** while reading the record from the (sub-)journal file or while writing
54882** to the database file, then the IO error code is returned. If data
54883** is successfully read from the (sub-)journal file but appears to be
54884** corrupted, SQLITE_DONE is returned. Data is considered corrupted in
54885** two circumstances:
54886**
54887** * If the record page-number is illegal (0 or PAGER_MJ_PGNO), or
54888** * If the record is being rolled back from the main journal file
54889** and the checksum field does not match the record content.
54890**
54891** Neither of these two scenarios are possible during a savepoint rollback.
54892**
54893** If this is a savepoint rollback, then memory may have to be dynamically
54894** allocated by this function. If this is the case and an allocation fails,
54895** SQLITE_NOMEM is returned.
54896*/
54897static int pager_playback_one_page(
54898 Pager *pPager, /* The pager being played back */
54899 i64 *pOffset, /* Offset of record to playback */
54900 Bitvec *pDone, /* Bitvec of pages already played back */
54901 int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
54902 int isSavepnt /* True for a savepoint rollback */
54903){
54904 int rc;
54905 PgHdr *pPg; /* An existing page in the cache */
54906 Pgno pgno; /* The page number of a page in journal */
54907 u32 cksum; /* Checksum used for sanity checking */
54908 char *aData; /* Temporary storage for the page */
54909 sqlite3_file *jfd; /* The file descriptor for the journal file */
54910 int isSynced; /* True if journal page is synced */
54911
54912 assert( (isMainJrnl&~1)==0 ); /* isMainJrnl is 0 or 1 */
54913 assert( (isSavepnt&~1)==0 ); /* isSavepnt is 0 or 1 */
54914 assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */
54915 assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
54916
54917 aData = pPager->pTmpSpace;
54918 assert( aData ); /* Temp storage must have already been allocated */
54919 assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) );
54920
54921 /* Either the state is greater than PAGER_WRITER_CACHEMOD (a transaction
54922 ** or savepoint rollback done at the request of the caller) or this is
54923 ** a hot-journal rollback. If it is a hot-journal rollback, the pager
54924 ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
54925 ** only reads from the main journal, not the sub-journal.
54926 */
54927 assert( pPager->eState>=PAGER_WRITER_CACHEMOD
54928 || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
54929 );
54930 assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
54931
54932 /* Read the page number and page data from the journal or sub-journal
54933 ** file. Return an error code to the caller if an IO error occurs.
54934 */
54935 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
54936 rc = read32bits(fd: jfd, offset: *pOffset, pRes: &pgno);
54937 if( rc!=SQLITE_OK ) return rc;
54938 rc = sqlite3OsRead(id: jfd, pBuf: (u8*)aData, amt: pPager->pageSize, offset: (*pOffset)+4);
54939 if( rc!=SQLITE_OK ) return rc;
54940 *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
54941
54942 /* Sanity checking on the page. This is more important that I originally
54943 ** thought. If a power failure occurs while the journal is being written,
54944 ** it could cause invalid data to be written into the journal. We need to
54945 ** detect this invalid data (with high probability) and ignore it.
54946 */
54947 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
54948 assert( !isSavepnt );
54949 return SQLITE_DONE;
54950 }
54951 if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(p: pDone, i: pgno) ){
54952 return SQLITE_OK;
54953 }
54954 if( isMainJrnl ){
54955 rc = read32bits(fd: jfd, offset: (*pOffset)-4, pRes: &cksum);
54956 if( rc ) return rc;
54957 if( !isSavepnt && pager_cksum(pPager, aData: (u8*)aData)!=cksum ){
54958 return SQLITE_DONE;
54959 }
54960 }
54961
54962 /* If this page has already been played back before during the current
54963 ** rollback, then don't bother to play it back again.
54964 */
54965 if( pDone && (rc = sqlite3BitvecSet(p: pDone, i: pgno))!=SQLITE_OK ){
54966 return rc;
54967 }
54968
54969 /* When playing back page 1, restore the nReserve setting
54970 */
54971 if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
54972 pPager->nReserve = ((u8*)aData)[20];
54973 }
54974
54975 /* If the pager is in CACHEMOD state, then there must be a copy of this
54976 ** page in the pager cache. In this case just update the pager cache,
54977 ** not the database file. The page is left marked dirty in this case.
54978 **
54979 ** An exception to the above rule: If the database is in no-sync mode
54980 ** and a page is moved during an incremental vacuum then the page may
54981 ** not be in the pager cache. Later: if a malloc() or IO error occurs
54982 ** during a Movepage() call, then the page may not be in the cache
54983 ** either. So the condition described in the above paragraph is not
54984 ** assert()able.
54985 **
54986 ** If in WRITER_DBMOD, WRITER_FINISHED or OPEN state, then we update the
54987 ** pager cache if it exists and the main file. The page is then marked
54988 ** not dirty. Since this code is only executed in PAGER_OPEN state for
54989 ** a hot-journal rollback, it is guaranteed that the page-cache is empty
54990 ** if the pager is in OPEN state.
54991 **
54992 ** Ticket #1171: The statement journal might contain page content that is
54993 ** different from the page content at the start of the transaction.
54994 ** This occurs when a page is changed prior to the start of a statement
54995 ** then changed again within the statement. When rolling back such a
54996 ** statement we must not write to the original database unless we know
54997 ** for certain that original page contents are synced into the main rollback
54998 ** journal. Otherwise, a power loss might leave modified data in the
54999 ** database file without an entry in the rollback journal that can
55000 ** restore the database to its original form. Two conditions must be
55001 ** met before writing to the database files. (1) the database must be
55002 ** locked. (2) we know that the original page content is fully synced
55003 ** in the main journal either because the page is not in cache or else
55004 ** the page is marked as needSync==0.
55005 **
55006 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
55007 ** is possible to fail a statement on a database that does not yet exist.
55008 ** Do not attempt to write if database file has never been opened.
55009 */
55010 if( pagerUseWal(pPager) ){
55011 pPg = 0;
55012 }else{
55013 pPg = sqlite3PagerLookup(pPager, pgno);
55014 }
55015 assert( pPg || !MEMDB );
55016 assert( pPager->eState!=PAGER_OPEN || pPg==0 || pPager->tempFile );
55017 PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
55018 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
55019 (isMainJrnl?"main-journal":"sub-journal")
55020 ));
55021 if( isMainJrnl ){
55022 isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
55023 }else{
55024 isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
55025 }
55026 if( isOpen(pPager->fd)
55027 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
55028 && isSynced
55029 ){
55030 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
55031 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
55032 assert( !pagerUseWal(pPager) );
55033
55034 /* Write the data read from the journal back into the database file.
55035 ** This is usually safe even for an encrypted database - as the data
55036 ** was encrypted before it was written to the journal file. The exception
55037 ** is if the data was just read from an in-memory sub-journal. In that
55038 ** case it must be encrypted here before it is copied into the database
55039 ** file. */
55040 rc = sqlite3OsWrite(id: pPager->fd, pBuf: (u8 *)aData, amt: pPager->pageSize, offset: ofst);
55041
55042 if( pgno>pPager->dbFileSize ){
55043 pPager->dbFileSize = pgno;
55044 }
55045 if( pPager->pBackup ){
55046 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
55047 }
55048 }else if( !isMainJrnl && pPg==0 ){
55049 /* If this is a rollback of a savepoint and data was not written to
55050 ** the database and the page is not in-memory, there is a potential
55051 ** problem. When the page is next fetched by the b-tree layer, it
55052 ** will be read from the database file, which may or may not be
55053 ** current.
55054 **
55055 ** There are a couple of different ways this can happen. All are quite
55056 ** obscure. When running in synchronous mode, this can only happen
55057 ** if the page is on the free-list at the start of the transaction, then
55058 ** populated, then moved using sqlite3PagerMovepage().
55059 **
55060 ** The solution is to add an in-memory page to the cache containing
55061 ** the data just read from the sub-journal. Mark the page as dirty
55062 ** and if the pager requires a journal-sync, then mark the page as
55063 ** requiring a journal-sync before it is written.
55064 */
55065 assert( isSavepnt );
55066 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 );
55067 pPager->doNotSpill |= SPILLFLAG_ROLLBACK;
55068 rc = sqlite3PagerGet(pPager, pgno, ppPage: &pPg, clrFlag: 1);
55069 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 );
55070 pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK;
55071 if( rc!=SQLITE_OK ) return rc;
55072 sqlite3PcacheMakeDirty(p: pPg);
55073 }
55074 if( pPg ){
55075 /* No page should ever be explicitly rolled back that is in use, except
55076 ** for page 1 which is held in use in order to keep the lock on the
55077 ** database active. However such a page may be rolled back as a result
55078 ** of an internal error resulting in an automatic call to
55079 ** sqlite3PagerRollback().
55080 */
55081 void *pData;
55082 pData = pPg->pData;
55083 memcpy(dest: pData, src: (u8*)aData, n: pPager->pageSize);
55084 pPager->xReiniter(pPg);
55085 /* It used to be that sqlite3PcacheMakeClean(pPg) was called here. But
55086 ** that call was dangerous and had no detectable benefit since the cache
55087 ** is normally cleaned by sqlite3PcacheCleanAll() after rollback and so
55088 ** has been removed. */
55089 pager_set_pagehash(pPg);
55090
55091 /* If this was page 1, then restore the value of Pager.dbFileVers.
55092 ** Do this before any decoding. */
55093 if( pgno==1 ){
55094 memcpy(dest: &pPager->dbFileVers, src: &((u8*)pData)[24],n: sizeof(pPager->dbFileVers));
55095 }
55096 sqlite3PcacheRelease(p: pPg);
55097 }
55098 return rc;
55099}
55100
55101/*
55102** Parameter zSuper is the name of a super-journal file. A single journal
55103** file that referred to the super-journal file has just been rolled back.
55104** This routine checks if it is possible to delete the super-journal file,
55105** and does so if it is.
55106**
55107** Argument zSuper may point to Pager.pTmpSpace. So that buffer is not
55108** available for use within this function.
55109**
55110** When a super-journal file is created, it is populated with the names
55111** of all of its child journals, one after another, formatted as utf-8
55112** encoded text. The end of each child journal file is marked with a
55113** nul-terminator byte (0x00). i.e. the entire contents of a super-journal
55114** file for a transaction involving two databases might be:
55115**
55116** "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
55117**
55118** A super-journal file may only be deleted once all of its child
55119** journals have been rolled back.
55120**
55121** This function reads the contents of the super-journal file into
55122** memory and loops through each of the child journal names. For
55123** each child journal, it checks if:
55124**
55125** * if the child journal exists, and if so
55126** * if the child journal contains a reference to super-journal
55127** file zSuper
55128**
55129** If a child journal can be found that matches both of the criteria
55130** above, this function returns without doing anything. Otherwise, if
55131** no such child journal can be found, file zSuper is deleted from
55132** the file-system using sqlite3OsDelete().
55133**
55134** If an IO error within this function, an error code is returned. This
55135** function allocates memory by calling sqlite3Malloc(). If an allocation
55136** fails, SQLITE_NOMEM is returned. Otherwise, if no IO or malloc errors
55137** occur, SQLITE_OK is returned.
55138**
55139** TODO: This function allocates a single block of memory to load
55140** the entire contents of the super-journal file. This could be
55141** a couple of kilobytes or so - potentially larger than the page
55142** size.
55143*/
55144static int pager_delsuper(Pager *pPager, const char *zSuper){
55145 sqlite3_vfs *pVfs = pPager->pVfs;
55146 int rc; /* Return code */
55147 sqlite3_file *pSuper; /* Malloc'd super-journal file descriptor */
55148 sqlite3_file *pJournal; /* Malloc'd child-journal file descriptor */
55149 char *zSuperJournal = 0; /* Contents of super-journal file */
55150 i64 nSuperJournal; /* Size of super-journal file */
55151 char *zJournal; /* Pointer to one journal within MJ file */
55152 char *zSuperPtr; /* Space to hold super-journal filename */
55153 char *zFree = 0; /* Free this buffer */
55154 int nSuperPtr; /* Amount of space allocated to zSuperPtr[] */
55155
55156 /* Allocate space for both the pJournal and pSuper file descriptors.
55157 ** If successful, open the super-journal file for reading.
55158 */
55159 pSuper = (sqlite3_file *)sqlite3MallocZero(n: pVfs->szOsFile * 2);
55160 if( !pSuper ){
55161 rc = SQLITE_NOMEM_BKPT;
55162 pJournal = 0;
55163 }else{
55164 const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_SUPER_JOURNAL);
55165 rc = sqlite3OsOpen(pVfs, zPath: zSuper, pFile: pSuper, flags, pFlagsOut: 0);
55166 pJournal = (sqlite3_file *)(((u8 *)pSuper) + pVfs->szOsFile);
55167 }
55168 if( rc!=SQLITE_OK ) goto delsuper_out;
55169
55170 /* Load the entire super-journal file into space obtained from
55171 ** sqlite3_malloc() and pointed to by zSuperJournal. Also obtain
55172 ** sufficient space (in zSuperPtr) to hold the names of super-journal
55173 ** files extracted from regular rollback-journals.
55174 */
55175 rc = sqlite3OsFileSize(id: pSuper, pSize: &nSuperJournal);
55176 if( rc!=SQLITE_OK ) goto delsuper_out;
55177 nSuperPtr = pVfs->mxPathname+1;
55178 zFree = sqlite3Malloc(n: 4 + nSuperJournal + nSuperPtr + 2);
55179 if( !zFree ){
55180 rc = SQLITE_NOMEM_BKPT;
55181 goto delsuper_out;
55182 }
55183 zFree[0] = zFree[1] = zFree[2] = zFree[3] = 0;
55184 zSuperJournal = &zFree[4];
55185 zSuperPtr = &zSuperJournal[nSuperJournal+2];
55186 rc = sqlite3OsRead(id: pSuper, pBuf: zSuperJournal, amt: (int)nSuperJournal, offset: 0);
55187 if( rc!=SQLITE_OK ) goto delsuper_out;
55188 zSuperJournal[nSuperJournal] = 0;
55189 zSuperJournal[nSuperJournal+1] = 0;
55190
55191 zJournal = zSuperJournal;
55192 while( (zJournal-zSuperJournal)<nSuperJournal ){
55193 int exists;
55194 rc = sqlite3OsAccess(pVfs, zPath: zJournal, SQLITE_ACCESS_EXISTS, pResOut: &exists);
55195 if( rc!=SQLITE_OK ){
55196 goto delsuper_out;
55197 }
55198 if( exists ){
55199 /* One of the journals pointed to by the super-journal exists.
55200 ** Open it and check if it points at the super-journal. If
55201 ** so, return without deleting the super-journal file.
55202 ** NB: zJournal is really a MAIN_JOURNAL. But call it a
55203 ** SUPER_JOURNAL here so that the VFS will not send the zJournal
55204 ** name into sqlite3_database_file_object().
55205 */
55206 int c;
55207 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_SUPER_JOURNAL);
55208 rc = sqlite3OsOpen(pVfs, zPath: zJournal, pFile: pJournal, flags, pFlagsOut: 0);
55209 if( rc!=SQLITE_OK ){
55210 goto delsuper_out;
55211 }
55212
55213 rc = readSuperJournal(pJrnl: pJournal, zSuper: zSuperPtr, nSuper: nSuperPtr);
55214 sqlite3OsClose(pId: pJournal);
55215 if( rc!=SQLITE_OK ){
55216 goto delsuper_out;
55217 }
55218
55219 c = zSuperPtr[0]!=0 && strcmp(s1: zSuperPtr, s2: zSuper)==0;
55220 if( c ){
55221 /* We have a match. Do not delete the super-journal file. */
55222 goto delsuper_out;
55223 }
55224 }
55225 zJournal += (sqlite3Strlen30(z: zJournal)+1);
55226 }
55227
55228 sqlite3OsClose(pId: pSuper);
55229 rc = sqlite3OsDelete(pVfs, zPath: zSuper, dirSync: 0);
55230
55231delsuper_out:
55232 sqlite3_free(p: zFree);
55233 if( pSuper ){
55234 sqlite3OsClose(pId: pSuper);
55235 assert( !isOpen(pJournal) );
55236 sqlite3_free(p: pSuper);
55237 }
55238 return rc;
55239}
55240
55241
55242/*
55243** This function is used to change the actual size of the database
55244** file in the file-system. This only happens when committing a transaction,
55245** or rolling back a transaction (including rolling back a hot-journal).
55246**
55247** If the main database file is not open, or the pager is not in either
55248** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
55249** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
55250** If the file on disk is currently larger than nPage pages, then use the VFS
55251** xTruncate() method to truncate it.
55252**
55253** Or, it might be the case that the file on disk is smaller than
55254** nPage pages. Some operating system implementations can get confused if
55255** you try to truncate a file to some size that is larger than it
55256** currently is, so detect this case and write a single zero byte to
55257** the end of the new file instead.
55258**
55259** If successful, return SQLITE_OK. If an IO error occurs while modifying
55260** the database file, return the error code to the caller.
55261*/
55262static int pager_truncate(Pager *pPager, Pgno nPage){
55263 int rc = SQLITE_OK;
55264 assert( pPager->eState!=PAGER_ERROR );
55265 assert( pPager->eState!=PAGER_READER );
55266
55267 if( isOpen(pPager->fd)
55268 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
55269 ){
55270 i64 currentSize, newSize;
55271 int szPage = pPager->pageSize;
55272 assert( pPager->eLock==EXCLUSIVE_LOCK );
55273 /* TODO: Is it safe to use Pager.dbFileSize here? */
55274 rc = sqlite3OsFileSize(id: pPager->fd, pSize: &currentSize);
55275 newSize = szPage*(i64)nPage;
55276 if( rc==SQLITE_OK && currentSize!=newSize ){
55277 if( currentSize>newSize ){
55278 rc = sqlite3OsTruncate(id: pPager->fd, size: newSize);
55279 }else if( (currentSize+szPage)<=newSize ){
55280 char *pTmp = pPager->pTmpSpace;
55281 memset(s: pTmp, c: 0, n: szPage);
55282 testcase( (newSize-szPage) == currentSize );
55283 testcase( (newSize-szPage) > currentSize );
55284 rc = sqlite3OsWrite(id: pPager->fd, pBuf: pTmp, amt: szPage, offset: newSize-szPage);
55285 }
55286 if( rc==SQLITE_OK ){
55287 pPager->dbFileSize = nPage;
55288 }
55289 }
55290 }
55291 return rc;
55292}
55293
55294/*
55295** Return a sanitized version of the sector-size of OS file pFile. The
55296** return value is guaranteed to lie between 32 and MAX_SECTOR_SIZE.
55297*/
55298SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *pFile){
55299 int iRet = sqlite3OsSectorSize(id: pFile);
55300 if( iRet<32 ){
55301 iRet = 512;
55302 }else if( iRet>MAX_SECTOR_SIZE ){
55303 assert( MAX_SECTOR_SIZE>=512 );
55304 iRet = MAX_SECTOR_SIZE;
55305 }
55306 return iRet;
55307}
55308
55309/*
55310** Set the value of the Pager.sectorSize variable for the given
55311** pager based on the value returned by the xSectorSize method
55312** of the open database file. The sector size will be used
55313** to determine the size and alignment of journal header and
55314** super-journal pointers within created journal files.
55315**
55316** For temporary files the effective sector size is always 512 bytes.
55317**
55318** Otherwise, for non-temporary files, the effective sector size is
55319** the value returned by the xSectorSize() method rounded up to 32 if
55320** it is less than 32, or rounded down to MAX_SECTOR_SIZE if it
55321** is greater than MAX_SECTOR_SIZE.
55322**
55323** If the file has the SQLITE_IOCAP_POWERSAFE_OVERWRITE property, then set
55324** the effective sector size to its minimum value (512). The purpose of
55325** pPager->sectorSize is to define the "blast radius" of bytes that
55326** might change if a crash occurs while writing to a single byte in
55327** that range. But with POWERSAFE_OVERWRITE, the blast radius is zero
55328** (that is what POWERSAFE_OVERWRITE means), so we minimize the sector
55329** size. For backwards compatibility of the rollback journal file format,
55330** we cannot reduce the effective sector size below 512.
55331*/
55332static void setSectorSize(Pager *pPager){
55333 assert( isOpen(pPager->fd) || pPager->tempFile );
55334
55335 if( pPager->tempFile
55336 || (sqlite3OsDeviceCharacteristics(id: pPager->fd) &
55337 SQLITE_IOCAP_POWERSAFE_OVERWRITE)!=0
55338 ){
55339 /* Sector size doesn't matter for temporary files. Also, the file
55340 ** may not have been opened yet, in which case the OsSectorSize()
55341 ** call will segfault. */
55342 pPager->sectorSize = 512;
55343 }else{
55344 pPager->sectorSize = sqlite3SectorSize(pFile: pPager->fd);
55345 }
55346}
55347
55348/*
55349** Playback the journal and thus restore the database file to
55350** the state it was in before we started making changes.
55351**
55352** The journal file format is as follows:
55353**
55354** (1) 8 byte prefix. A copy of aJournalMagic[].
55355** (2) 4 byte big-endian integer which is the number of valid page records
55356** in the journal. If this value is 0xffffffff, then compute the
55357** number of page records from the journal size.
55358** (3) 4 byte big-endian integer which is the initial value for the
55359** sanity checksum.
55360** (4) 4 byte integer which is the number of pages to truncate the
55361** database to during a rollback.
55362** (5) 4 byte big-endian integer which is the sector size. The header
55363** is this many bytes in size.
55364** (6) 4 byte big-endian integer which is the page size.
55365** (7) zero padding out to the next sector size.
55366** (8) Zero or more pages instances, each as follows:
55367** + 4 byte page number.
55368** + pPager->pageSize bytes of data.
55369** + 4 byte checksum
55370**
55371** When we speak of the journal header, we mean the first 7 items above.
55372** Each entry in the journal is an instance of the 8th item.
55373**
55374** Call the value from the second bullet "nRec". nRec is the number of
55375** valid page entries in the journal. In most cases, you can compute the
55376** value of nRec from the size of the journal file. But if a power
55377** failure occurred while the journal was being written, it could be the
55378** case that the size of the journal file had already been increased but
55379** the extra entries had not yet made it safely to disk. In such a case,
55380** the value of nRec computed from the file size would be too large. For
55381** that reason, we always use the nRec value in the header.
55382**
55383** If the nRec value is 0xffffffff it means that nRec should be computed
55384** from the file size. This value is used when the user selects the
55385** no-sync option for the journal. A power failure could lead to corruption
55386** in this case. But for things like temporary table (which will be
55387** deleted when the power is restored) we don't care.
55388**
55389** If the file opened as the journal file is not a well-formed
55390** journal file then all pages up to the first corrupted page are rolled
55391** back (or no pages if the journal header is corrupted). The journal file
55392** is then deleted and SQLITE_OK returned, just as if no corruption had
55393** been encountered.
55394**
55395** If an I/O or malloc() error occurs, the journal-file is not deleted
55396** and an error code is returned.
55397**
55398** The isHot parameter indicates that we are trying to rollback a journal
55399** that might be a hot journal. Or, it could be that the journal is
55400** preserved because of JOURNALMODE_PERSIST or JOURNALMODE_TRUNCATE.
55401** If the journal really is hot, reset the pager cache prior rolling
55402** back any content. If the journal is merely persistent, no reset is
55403** needed.
55404*/
55405static int pager_playback(Pager *pPager, int isHot){
55406 sqlite3_vfs *pVfs = pPager->pVfs;
55407 i64 szJ; /* Size of the journal file in bytes */
55408 u32 nRec; /* Number of Records in the journal */
55409 u32 u; /* Unsigned loop counter */
55410 Pgno mxPg = 0; /* Size of the original file in pages */
55411 int rc; /* Result code of a subroutine */
55412 int res = 1; /* Value returned by sqlite3OsAccess() */
55413 char *zSuper = 0; /* Name of super-journal file if any */
55414 int needPagerReset; /* True to reset page prior to first page rollback */
55415 int nPlayback = 0; /* Total number of pages restored from journal */
55416 u32 savedPageSize = pPager->pageSize;
55417
55418 /* Figure out how many records are in the journal. Abort early if
55419 ** the journal is empty.
55420 */
55421 assert( isOpen(pPager->jfd) );
55422 rc = sqlite3OsFileSize(id: pPager->jfd, pSize: &szJ);
55423 if( rc!=SQLITE_OK ){
55424 goto end_playback;
55425 }
55426
55427 /* Read the super-journal name from the journal, if it is present.
55428 ** If a super-journal file name is specified, but the file is not
55429 ** present on disk, then the journal is not hot and does not need to be
55430 ** played back.
55431 **
55432 ** TODO: Technically the following is an error because it assumes that
55433 ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that
55434 ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
55435 ** mxPathname is 512, which is the same as the minimum allowable value
55436 ** for pageSize.
55437 */
55438 zSuper = pPager->pTmpSpace;
55439 rc = readSuperJournal(pJrnl: pPager->jfd, zSuper, nSuper: pPager->pVfs->mxPathname+1);
55440 if( rc==SQLITE_OK && zSuper[0] ){
55441 rc = sqlite3OsAccess(pVfs, zPath: zSuper, SQLITE_ACCESS_EXISTS, pResOut: &res);
55442 }
55443 zSuper = 0;
55444 if( rc!=SQLITE_OK || !res ){
55445 goto end_playback;
55446 }
55447 pPager->journalOff = 0;
55448 needPagerReset = isHot;
55449
55450 /* This loop terminates either when a readJournalHdr() or
55451 ** pager_playback_one_page() call returns SQLITE_DONE or an IO error
55452 ** occurs.
55453 */
55454 while( 1 ){
55455 /* Read the next journal header from the journal file. If there are
55456 ** not enough bytes left in the journal file for a complete header, or
55457 ** it is corrupted, then a process must have failed while writing it.
55458 ** This indicates nothing more needs to be rolled back.
55459 */
55460 rc = readJournalHdr(pPager, isHot, journalSize: szJ, pNRec: &nRec, pDbSize: &mxPg);
55461 if( rc!=SQLITE_OK ){
55462 if( rc==SQLITE_DONE ){
55463 rc = SQLITE_OK;
55464 }
55465 goto end_playback;
55466 }
55467
55468 /* If nRec is 0xffffffff, then this journal was created by a process
55469 ** working in no-sync mode. This means that the rest of the journal
55470 ** file consists of pages, there are no more journal headers. Compute
55471 ** the value of nRec based on this assumption.
55472 */
55473 if( nRec==0xffffffff ){
55474 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
55475 nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
55476 }
55477
55478 /* If nRec is 0 and this rollback is of a transaction created by this
55479 ** process and if this is the final header in the journal, then it means
55480 ** that this part of the journal was being filled but has not yet been
55481 ** synced to disk. Compute the number of pages based on the remaining
55482 ** size of the file.
55483 **
55484 ** The third term of the test was added to fix ticket #2565.
55485 ** When rolling back a hot journal, nRec==0 always means that the next
55486 ** chunk of the journal contains zero pages to be rolled back. But
55487 ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in
55488 ** the journal, it means that the journal might contain additional
55489 ** pages that need to be rolled back and that the number of pages
55490 ** should be computed based on the journal file size.
55491 */
55492 if( nRec==0 && !isHot &&
55493 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
55494 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
55495 }
55496
55497 /* If this is the first header read from the journal, truncate the
55498 ** database file back to its original size.
55499 */
55500 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
55501 rc = pager_truncate(pPager, nPage: mxPg);
55502 if( rc!=SQLITE_OK ){
55503 goto end_playback;
55504 }
55505 pPager->dbSize = mxPg;
55506 }
55507
55508 /* Copy original pages out of the journal and back into the
55509 ** database file and/or page cache.
55510 */
55511 for(u=0; u<nRec; u++){
55512 if( needPagerReset ){
55513 pager_reset(pPager);
55514 needPagerReset = 0;
55515 }
55516 rc = pager_playback_one_page(pPager,pOffset: &pPager->journalOff,pDone: 0,isMainJrnl: 1,isSavepnt: 0);
55517 if( rc==SQLITE_OK ){
55518 nPlayback++;
55519 }else{
55520 if( rc==SQLITE_DONE ){
55521 pPager->journalOff = szJ;
55522 break;
55523 }else if( rc==SQLITE_IOERR_SHORT_READ ){
55524 /* If the journal has been truncated, simply stop reading and
55525 ** processing the journal. This might happen if the journal was
55526 ** not completely written and synced prior to a crash. In that
55527 ** case, the database should have never been written in the
55528 ** first place so it is OK to simply abandon the rollback. */
55529 rc = SQLITE_OK;
55530 goto end_playback;
55531 }else{
55532 /* If we are unable to rollback, quit and return the error
55533 ** code. This will cause the pager to enter the error state
55534 ** so that no further harm will be done. Perhaps the next
55535 ** process to come along will be able to rollback the database.
55536 */
55537 goto end_playback;
55538 }
55539 }
55540 }
55541 }
55542 /*NOTREACHED*/
55543 assert( 0 );
55544
55545end_playback:
55546 if( rc==SQLITE_OK ){
55547 rc = sqlite3PagerSetPagesize(pPager, &savedPageSize, -1);
55548 }
55549 /* Following a rollback, the database file should be back in its original
55550 ** state prior to the start of the transaction, so invoke the
55551 ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
55552 ** assertion that the transaction counter was modified.
55553 */
55554#ifdef SQLITE_DEBUG
55555 sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
55556#endif
55557
55558 /* If this playback is happening automatically as a result of an IO or
55559 ** malloc error that occurred after the change-counter was updated but
55560 ** before the transaction was committed, then the change-counter
55561 ** modification may just have been reverted. If this happens in exclusive
55562 ** mode, then subsequent transactions performed by the connection will not
55563 ** update the change-counter at all. This may lead to cache inconsistency
55564 ** problems for other processes at some point in the future. So, just
55565 ** in case this has happened, clear the changeCountDone flag now.
55566 */
55567 pPager->changeCountDone = pPager->tempFile;
55568
55569 if( rc==SQLITE_OK ){
55570 /* Leave 4 bytes of space before the super-journal filename in memory.
55571 ** This is because it may end up being passed to sqlite3OsOpen(), in
55572 ** which case it requires 4 0x00 bytes in memory immediately before
55573 ** the filename. */
55574 zSuper = &pPager->pTmpSpace[4];
55575 rc = readSuperJournal(pJrnl: pPager->jfd, zSuper, nSuper: pPager->pVfs->mxPathname+1);
55576 testcase( rc!=SQLITE_OK );
55577 }
55578 if( rc==SQLITE_OK
55579 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
55580 ){
55581 rc = sqlite3PagerSync(pPager, zSuper: 0);
55582 }
55583 if( rc==SQLITE_OK ){
55584 rc = pager_end_transaction(pPager, hasSuper: zSuper[0]!='\0', bCommit: 0);
55585 testcase( rc!=SQLITE_OK );
55586 }
55587 if( rc==SQLITE_OK && zSuper[0] && res ){
55588 /* If there was a super-journal and this routine will return success,
55589 ** see if it is possible to delete the super-journal.
55590 */
55591 assert( zSuper==&pPager->pTmpSpace[4] );
55592 memset(s: &zSuper[-4], c: 0, n: 4);
55593 rc = pager_delsuper(pPager, zSuper);
55594 testcase( rc!=SQLITE_OK );
55595 }
55596 if( isHot && nPlayback ){
55597 sqlite3_log(SQLITE_NOTICE_RECOVER_ROLLBACK, zFormat: "recovered %d pages from %s",
55598 nPlayback, pPager->zJournal);
55599 }
55600
55601 /* The Pager.sectorSize variable may have been updated while rolling
55602 ** back a journal created by a process with a different sector size
55603 ** value. Reset it to the correct value for this process.
55604 */
55605 setSectorSize(pPager);
55606 return rc;
55607}
55608
55609
55610/*
55611** Read the content for page pPg out of the database file (or out of
55612** the WAL if that is where the most recent copy if found) into
55613** pPg->pData. A shared lock or greater must be held on the database
55614** file before this function is called.
55615**
55616** If page 1 is read, then the value of Pager.dbFileVers[] is set to
55617** the value read from the database file.
55618**
55619** If an IO error occurs, then the IO error is returned to the caller.
55620** Otherwise, SQLITE_OK is returned.
55621*/
55622static int readDbPage(PgHdr *pPg){
55623 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
55624 int rc = SQLITE_OK; /* Return code */
55625
55626#ifndef SQLITE_OMIT_WAL
55627 u32 iFrame = 0; /* Frame of WAL containing pgno */
55628
55629 assert( pPager->eState>=PAGER_READER && !MEMDB );
55630 assert( isOpen(pPager->fd) );
55631
55632 if( pagerUseWal(pPager) ){
55633 rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
55634 if( rc ) return rc;
55635 }
55636 if( iFrame ){
55637 rc = sqlite3WalReadFrame(pPager->pWal, iFrame,pPager->pageSize,pPg->pData);
55638 }else
55639#endif
55640 {
55641 i64 iOffset = (pPg->pgno-1)*(i64)pPager->pageSize;
55642 rc = sqlite3OsRead(id: pPager->fd, pBuf: pPg->pData, amt: pPager->pageSize, offset: iOffset);
55643 if( rc==SQLITE_IOERR_SHORT_READ ){
55644 rc = SQLITE_OK;
55645 }
55646 }
55647
55648 if( pPg->pgno==1 ){
55649 if( rc ){
55650 /* If the read is unsuccessful, set the dbFileVers[] to something
55651 ** that will never be a valid file version. dbFileVers[] is a copy
55652 ** of bytes 24..39 of the database. Bytes 28..31 should always be
55653 ** zero or the size of the database in page. Bytes 32..35 and 35..39
55654 ** should be page numbers which are never 0xffffffff. So filling
55655 ** pPager->dbFileVers[] with all 0xff bytes should suffice.
55656 **
55657 ** For an encrypted database, the situation is more complex: bytes
55658 ** 24..39 of the database are white noise. But the probability of
55659 ** white noise equaling 16 bytes of 0xff is vanishingly small so
55660 ** we should still be ok.
55661 */
55662 memset(s: pPager->dbFileVers, c: 0xff, n: sizeof(pPager->dbFileVers));
55663 }else{
55664 u8 *dbFileVers = &((u8*)pPg->pData)[24];
55665 memcpy(dest: &pPager->dbFileVers, src: dbFileVers, n: sizeof(pPager->dbFileVers));
55666 }
55667 }
55668 PAGER_INCR(sqlite3_pager_readdb_count);
55669 PAGER_INCR(pPager->nRead);
55670 IOTRACE(("PGIN %p %d\n", pPager, pPg->pgno));
55671 PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
55672 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)));
55673
55674 return rc;
55675}
55676
55677/*
55678** Update the value of the change-counter at offsets 24 and 92 in
55679** the header and the sqlite version number at offset 96.
55680**
55681** This is an unconditional update. See also the pager_incr_changecounter()
55682** routine which only updates the change-counter if the update is actually
55683** needed, as determined by the pPager->changeCountDone state variable.
55684*/
55685static void pager_write_changecounter(PgHdr *pPg){
55686 u32 change_counter;
55687 if( NEVER(pPg==0) ) return;
55688
55689 /* Increment the value just read and write it back to byte 24. */
55690 change_counter = sqlite3Get4byte(p: (u8*)pPg->pPager->dbFileVers)+1;
55691 put32bits(((char*)pPg->pData)+24, change_counter);
55692
55693 /* Also store the SQLite version number in bytes 96..99 and in
55694 ** bytes 92..95 store the change counter for which the version number
55695 ** is valid. */
55696 put32bits(((char*)pPg->pData)+92, change_counter);
55697 put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
55698}
55699
55700#ifndef SQLITE_OMIT_WAL
55701/*
55702** This function is invoked once for each page that has already been
55703** written into the log file when a WAL transaction is rolled back.
55704** Parameter iPg is the page number of said page. The pCtx argument
55705** is actually a pointer to the Pager structure.
55706**
55707** If page iPg is present in the cache, and has no outstanding references,
55708** it is discarded. Otherwise, if there are one or more outstanding
55709** references, the page content is reloaded from the database. If the
55710** attempt to reload content from the database is required and fails,
55711** return an SQLite error code. Otherwise, SQLITE_OK.
55712*/
55713static int pagerUndoCallback(void *pCtx, Pgno iPg){
55714 int rc = SQLITE_OK;
55715 Pager *pPager = (Pager *)pCtx;
55716 PgHdr *pPg;
55717
55718 assert( pagerUseWal(pPager) );
55719 pPg = sqlite3PagerLookup(pPager, pgno: iPg);
55720 if( pPg ){
55721 if( sqlite3PcachePageRefcount(p: pPg)==1 ){
55722 sqlite3PcacheDrop(p: pPg);
55723 }else{
55724 rc = readDbPage(pPg);
55725 if( rc==SQLITE_OK ){
55726 pPager->xReiniter(pPg);
55727 }
55728 sqlite3PagerUnrefNotNull(pPg);
55729 }
55730 }
55731
55732 /* Normally, if a transaction is rolled back, any backup processes are
55733 ** updated as data is copied out of the rollback journal and into the
55734 ** database. This is not generally possible with a WAL database, as
55735 ** rollback involves simply truncating the log file. Therefore, if one
55736 ** or more frames have already been written to the log (and therefore
55737 ** also copied into the backup databases) as part of this transaction,
55738 ** the backups must be restarted.
55739 */
55740 sqlite3BackupRestart(pPager->pBackup);
55741
55742 return rc;
55743}
55744
55745/*
55746** This function is called to rollback a transaction on a WAL database.
55747*/
55748static int pagerRollbackWal(Pager *pPager){
55749 int rc; /* Return Code */
55750 PgHdr *pList; /* List of dirty pages to revert */
55751
55752 /* For all pages in the cache that are currently dirty or have already
55753 ** been written (but not committed) to the log file, do one of the
55754 ** following:
55755 **
55756 ** + Discard the cached page (if refcount==0), or
55757 ** + Reload page content from the database (if refcount>0).
55758 */
55759 pPager->dbSize = pPager->dbOrigSize;
55760 rc = sqlite3WalUndo(pWal: pPager->pWal, xUndo: pagerUndoCallback, pUndoCtx: (void *)pPager);
55761 pList = sqlite3PcacheDirtyList(pCache: pPager->pPCache);
55762 while( pList && rc==SQLITE_OK ){
55763 PgHdr *pNext = pList->pDirty;
55764 rc = pagerUndoCallback(pCtx: (void *)pPager, iPg: pList->pgno);
55765 pList = pNext;
55766 }
55767
55768 return rc;
55769}
55770
55771/*
55772** This function is a wrapper around sqlite3WalFrames(). As well as logging
55773** the contents of the list of pages headed by pList (connected by pDirty),
55774** this function notifies any active backup processes that the pages have
55775** changed.
55776**
55777** The list of pages passed into this routine is always sorted by page number.
55778** Hence, if page 1 appears anywhere on the list, it will be the first page.
55779*/
55780static int pagerWalFrames(
55781 Pager *pPager, /* Pager object */
55782 PgHdr *pList, /* List of frames to log */
55783 Pgno nTruncate, /* Database size after this commit */
55784 int isCommit /* True if this is a commit */
55785){
55786 int rc; /* Return code */
55787 int nList; /* Number of pages in pList */
55788 PgHdr *p; /* For looping over pages */
55789
55790 assert( pPager->pWal );
55791 assert( pList );
55792#ifdef SQLITE_DEBUG
55793 /* Verify that the page list is in accending order */
55794 for(p=pList; p && p->pDirty; p=p->pDirty){
55795 assert( p->pgno < p->pDirty->pgno );
55796 }
55797#endif
55798
55799 assert( pList->pDirty==0 || isCommit );
55800 if( isCommit ){
55801 /* If a WAL transaction is being committed, there is no point in writing
55802 ** any pages with page numbers greater than nTruncate into the WAL file.
55803 ** They will never be read by any client. So remove them from the pDirty
55804 ** list here. */
55805 PgHdr **ppNext = &pList;
55806 nList = 0;
55807 for(p=pList; (*ppNext = p)!=0; p=p->pDirty){
55808 if( p->pgno<=nTruncate ){
55809 ppNext = &p->pDirty;
55810 nList++;
55811 }
55812 }
55813 assert( pList );
55814 }else{
55815 nList = 1;
55816 }
55817 pPager->aStat[PAGER_STAT_WRITE] += nList;
55818
55819 if( pList->pgno==1 ) pager_write_changecounter(pPg: pList);
55820 rc = sqlite3WalFrames(pWal: pPager->pWal,
55821 pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
55822 );
55823 if( rc==SQLITE_OK && pPager->pBackup ){
55824 for(p=pList; p; p=p->pDirty){
55825 sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
55826 }
55827 }
55828
55829#ifdef SQLITE_CHECK_PAGES
55830 pList = sqlite3PcacheDirtyList(pPager->pPCache);
55831 for(p=pList; p; p=p->pDirty){
55832 pager_set_pagehash(p);
55833 }
55834#endif
55835
55836 return rc;
55837}
55838
55839/*
55840** Begin a read transaction on the WAL.
55841**
55842** This routine used to be called "pagerOpenSnapshot()" because it essentially
55843** makes a snapshot of the database at the current point in time and preserves
55844** that snapshot for use by the reader in spite of concurrently changes by
55845** other writers or checkpointers.
55846*/
55847static int pagerBeginReadTransaction(Pager *pPager){
55848 int rc; /* Return code */
55849 int changed = 0; /* True if cache must be reset */
55850
55851 assert( pagerUseWal(pPager) );
55852 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
55853
55854 /* sqlite3WalEndReadTransaction() was not called for the previous
55855 ** transaction in locking_mode=EXCLUSIVE. So call it now. If we
55856 ** are in locking_mode=NORMAL and EndRead() was previously called,
55857 ** the duplicate call is harmless.
55858 */
55859 sqlite3WalEndReadTransaction(pWal: pPager->pWal);
55860
55861 rc = sqlite3WalBeginReadTransaction(pWal: pPager->pWal, &changed);
55862 if( rc!=SQLITE_OK || changed ){
55863 pager_reset(pPager);
55864 if( USEFETCH(pPager) ) sqlite3OsUnfetch(id: pPager->fd, iOff: 0, p: 0);
55865 }
55866
55867 return rc;
55868}
55869#endif
55870
55871/*
55872** This function is called as part of the transition from PAGER_OPEN
55873** to PAGER_READER state to determine the size of the database file
55874** in pages (assuming the page size currently stored in Pager.pageSize).
55875**
55876** If no error occurs, SQLITE_OK is returned and the size of the database
55877** in pages is stored in *pnPage. Otherwise, an error code (perhaps
55878** SQLITE_IOERR_FSTAT) is returned and *pnPage is left unmodified.
55879*/
55880static int pagerPagecount(Pager *pPager, Pgno *pnPage){
55881 Pgno nPage; /* Value to return via *pnPage */
55882
55883 /* Query the WAL sub-system for the database size. The WalDbsize()
55884 ** function returns zero if the WAL is not open (i.e. Pager.pWal==0), or
55885 ** if the database size is not available. The database size is not
55886 ** available from the WAL sub-system if the log file is empty or
55887 ** contains no valid committed transactions.
55888 */
55889 assert( pPager->eState==PAGER_OPEN );
55890 assert( pPager->eLock>=SHARED_LOCK );
55891 assert( isOpen(pPager->fd) );
55892 assert( pPager->tempFile==0 );
55893 nPage = sqlite3WalDbsize(pWal: pPager->pWal);
55894
55895 /* If the number of pages in the database is not available from the
55896 ** WAL sub-system, determine the page count based on the size of
55897 ** the database file. If the size of the database file is not an
55898 ** integer multiple of the page-size, round up the result.
55899 */
55900 if( nPage==0 && ALWAYS(isOpen(pPager->fd)) ){
55901 i64 n = 0; /* Size of db file in bytes */
55902 int rc = sqlite3OsFileSize(id: pPager->fd, pSize: &n);
55903 if( rc!=SQLITE_OK ){
55904 return rc;
55905 }
55906 nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
55907 }
55908
55909 /* If the current number of pages in the file is greater than the
55910 ** configured maximum pager number, increase the allowed limit so
55911 ** that the file can be read.
55912 */
55913 if( nPage>pPager->mxPgno ){
55914 pPager->mxPgno = (Pgno)nPage;
55915 }
55916
55917 *pnPage = nPage;
55918 return SQLITE_OK;
55919}
55920
55921#ifndef SQLITE_OMIT_WAL
55922/*
55923** Check if the *-wal file that corresponds to the database opened by pPager
55924** exists if the database is not empy, or verify that the *-wal file does
55925** not exist (by deleting it) if the database file is empty.
55926**
55927** If the database is not empty and the *-wal file exists, open the pager
55928** in WAL mode. If the database is empty or if no *-wal file exists and
55929** if no error occurs, make sure Pager.journalMode is not set to
55930** PAGER_JOURNALMODE_WAL.
55931**
55932** Return SQLITE_OK or an error code.
55933**
55934** The caller must hold a SHARED lock on the database file to call this
55935** function. Because an EXCLUSIVE lock on the db file is required to delete
55936** a WAL on a none-empty database, this ensures there is no race condition
55937** between the xAccess() below and an xDelete() being executed by some
55938** other connection.
55939*/
55940static int pagerOpenWalIfPresent(Pager *pPager){
55941 int rc = SQLITE_OK;
55942 assert( pPager->eState==PAGER_OPEN );
55943 assert( pPager->eLock>=SHARED_LOCK );
55944
55945 if( !pPager->tempFile ){
55946 int isWal; /* True if WAL file exists */
55947 rc = sqlite3OsAccess(
55948 pVfs: pPager->pVfs, zPath: pPager->zWal, SQLITE_ACCESS_EXISTS, pResOut: &isWal
55949 );
55950 if( rc==SQLITE_OK ){
55951 if( isWal ){
55952 Pgno nPage; /* Size of the database file */
55953
55954 rc = pagerPagecount(pPager, pnPage: &nPage);
55955 if( rc ) return rc;
55956 if( nPage==0 ){
55957 rc = sqlite3OsDelete(pVfs: pPager->pVfs, zPath: pPager->zWal, dirSync: 0);
55958 }else{
55959 testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
55960 rc = sqlite3PagerOpenWal(pPager, pisOpen: 0);
55961 }
55962 }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
55963 pPager->journalMode = PAGER_JOURNALMODE_DELETE;
55964 }
55965 }
55966 }
55967 return rc;
55968}
55969#endif
55970
55971/*
55972** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback
55973** the entire super-journal file. The case pSavepoint==NULL occurs when
55974** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction
55975** savepoint.
55976**
55977** When pSavepoint is not NULL (meaning a non-transaction savepoint is
55978** being rolled back), then the rollback consists of up to three stages,
55979** performed in the order specified:
55980**
55981** * Pages are played back from the main journal starting at byte
55982** offset PagerSavepoint.iOffset and continuing to
55983** PagerSavepoint.iHdrOffset, or to the end of the main journal
55984** file if PagerSavepoint.iHdrOffset is zero.
55985**
55986** * If PagerSavepoint.iHdrOffset is not zero, then pages are played
55987** back starting from the journal header immediately following
55988** PagerSavepoint.iHdrOffset to the end of the main journal file.
55989**
55990** * Pages are then played back from the sub-journal file, starting
55991** with the PagerSavepoint.iSubRec and continuing to the end of
55992** the journal file.
55993**
55994** Throughout the rollback process, each time a page is rolled back, the
55995** corresponding bit is set in a bitvec structure (variable pDone in the
55996** implementation below). This is used to ensure that a page is only
55997** rolled back the first time it is encountered in either journal.
55998**
55999** If pSavepoint is NULL, then pages are only played back from the main
56000** journal file. There is no need for a bitvec in this case.
56001**
56002** In either case, before playback commences the Pager.dbSize variable
56003** is reset to the value that it held at the start of the savepoint
56004** (or transaction). No page with a page-number greater than this value
56005** is played back. If one is encountered it is simply skipped.
56006*/
56007static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
56008 i64 szJ; /* Effective size of the main journal */
56009 i64 iHdrOff; /* End of first segment of main-journal records */
56010 int rc = SQLITE_OK; /* Return code */
56011 Bitvec *pDone = 0; /* Bitvec to ensure pages played back only once */
56012
56013 assert( pPager->eState!=PAGER_ERROR );
56014 assert( pPager->eState>=PAGER_WRITER_LOCKED );
56015
56016 /* Allocate a bitvec to use to store the set of pages rolled back */
56017 if( pSavepoint ){
56018 pDone = sqlite3BitvecCreate(iSize: pSavepoint->nOrig);
56019 if( !pDone ){
56020 return SQLITE_NOMEM_BKPT;
56021 }
56022 }
56023
56024 /* Set the database size back to the value it was before the savepoint
56025 ** being reverted was opened.
56026 */
56027 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
56028 pPager->changeCountDone = pPager->tempFile;
56029
56030 if( !pSavepoint && pagerUseWal(pPager) ){
56031 return pagerRollbackWal(pPager);
56032 }
56033
56034 /* Use pPager->journalOff as the effective size of the main rollback
56035 ** journal. The actual file might be larger than this in
56036 ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST. But anything
56037 ** past pPager->journalOff is off-limits to us.
56038 */
56039 szJ = pPager->journalOff;
56040 assert( pagerUseWal(pPager)==0 || szJ==0 );
56041
56042 /* Begin by rolling back records from the main journal starting at
56043 ** PagerSavepoint.iOffset and continuing to the next journal header.
56044 ** There might be records in the main journal that have a page number
56045 ** greater than the current database size (pPager->dbSize) but those
56046 ** will be skipped automatically. Pages are added to pDone as they
56047 ** are played back.
56048 */
56049 if( pSavepoint && !pagerUseWal(pPager) ){
56050 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
56051 pPager->journalOff = pSavepoint->iOffset;
56052 while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
56053 rc = pager_playback_one_page(pPager, pOffset: &pPager->journalOff, pDone, isMainJrnl: 1, isSavepnt: 1);
56054 }
56055 assert( rc!=SQLITE_DONE );
56056 }else{
56057 pPager->journalOff = 0;
56058 }
56059
56060 /* Continue rolling back records out of the main journal starting at
56061 ** the first journal header seen and continuing until the effective end
56062 ** of the main journal file. Continue to skip out-of-range pages and
56063 ** continue adding pages rolled back to pDone.
56064 */
56065 while( rc==SQLITE_OK && pPager->journalOff<szJ ){
56066 u32 ii; /* Loop counter */
56067 u32 nJRec = 0; /* Number of Journal Records */
56068 u32 dummy;
56069 rc = readJournalHdr(pPager, isHot: 0, journalSize: szJ, pNRec: &nJRec, pDbSize: &dummy);
56070 assert( rc!=SQLITE_DONE );
56071
56072 /*
56073 ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
56074 ** test is related to ticket #2565. See the discussion in the
56075 ** pager_playback() function for additional information.
56076 */
56077 if( nJRec==0
56078 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
56079 ){
56080 nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
56081 }
56082 for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
56083 rc = pager_playback_one_page(pPager, pOffset: &pPager->journalOff, pDone, isMainJrnl: 1, isSavepnt: 1);
56084 }
56085 assert( rc!=SQLITE_DONE );
56086 }
56087 assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
56088
56089 /* Finally, rollback pages from the sub-journal. Page that were
56090 ** previously rolled back out of the main journal (and are hence in pDone)
56091 ** will be skipped. Out-of-range pages are also skipped.
56092 */
56093 if( pSavepoint ){
56094 u32 ii; /* Loop counter */
56095 i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
56096
56097 if( pagerUseWal(pPager) ){
56098 rc = sqlite3WalSavepointUndo(pWal: pPager->pWal, aWalData: pSavepoint->aWalData);
56099 }
56100 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
56101 assert( offset==(i64)ii*(4+pPager->pageSize) );
56102 rc = pager_playback_one_page(pPager, pOffset: &offset, pDone, isMainJrnl: 0, isSavepnt: 1);
56103 }
56104 assert( rc!=SQLITE_DONE );
56105 }
56106
56107 sqlite3BitvecDestroy(p: pDone);
56108 if( rc==SQLITE_OK ){
56109 pPager->journalOff = szJ;
56110 }
56111
56112 return rc;
56113}
56114
56115/*
56116** Change the maximum number of in-memory pages that are allowed
56117** before attempting to recycle clean and unused pages.
56118*/
56119SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
56120 sqlite3PcacheSetCachesize(pCache: pPager->pPCache, mxPage);
56121}
56122
56123/*
56124** Change the maximum number of in-memory pages that are allowed
56125** before attempting to spill pages to journal.
56126*/
56127SQLITE_PRIVATE int sqlite3PagerSetSpillsize(Pager *pPager, int mxPage){
56128 return sqlite3PcacheSetSpillsize(p: pPager->pPCache, mxPage);
56129}
56130
56131/*
56132** Invoke SQLITE_FCNTL_MMAP_SIZE based on the current value of szMmap.
56133*/
56134static void pagerFixMaplimit(Pager *pPager){
56135#if SQLITE_MAX_MMAP_SIZE>0
56136 sqlite3_file *fd = pPager->fd;
56137 if( isOpen(fd) && fd->pMethods->iVersion>=3 ){
56138 sqlite3_int64 sz;
56139 sz = pPager->szMmap;
56140 pPager->bUseFetch = (sz>0);
56141 setGetterMethod(pPager);
56142 sqlite3OsFileControlHint(id: pPager->fd, SQLITE_FCNTL_MMAP_SIZE, pArg: &sz);
56143 }
56144#endif
56145}
56146
56147/*
56148** Change the maximum size of any memory mapping made of the database file.
56149*/
56150SQLITE_PRIVATE void sqlite3PagerSetMmapLimit(Pager *pPager, sqlite3_int64 szMmap){
56151 pPager->szMmap = szMmap;
56152 pagerFixMaplimit(pPager);
56153}
56154
56155/*
56156** Free as much memory as possible from the pager.
56157*/
56158SQLITE_PRIVATE void sqlite3PagerShrink(Pager *pPager){
56159 sqlite3PcacheShrink(pCache: pPager->pPCache);
56160}
56161
56162/*
56163** Adjust settings of the pager to those specified in the pgFlags parameter.
56164**
56165** The "level" in pgFlags & PAGER_SYNCHRONOUS_MASK sets the robustness
56166** of the database to damage due to OS crashes or power failures by
56167** changing the number of syncs()s when writing the journals.
56168** There are four levels:
56169**
56170** OFF sqlite3OsSync() is never called. This is the default
56171** for temporary and transient files.
56172**
56173** NORMAL The journal is synced once before writes begin on the
56174** database. This is normally adequate protection, but
56175** it is theoretically possible, though very unlikely,
56176** that an inopertune power failure could leave the journal
56177** in a state which would cause damage to the database
56178** when it is rolled back.
56179**
56180** FULL The journal is synced twice before writes begin on the
56181** database (with some additional information - the nRec field
56182** of the journal header - being written in between the two
56183** syncs). If we assume that writing a
56184** single disk sector is atomic, then this mode provides
56185** assurance that the journal will not be corrupted to the
56186** point of causing damage to the database during rollback.
56187**
56188** EXTRA This is like FULL except that is also syncs the directory
56189** that contains the rollback journal after the rollback
56190** journal is unlinked.
56191**
56192** The above is for a rollback-journal mode. For WAL mode, OFF continues
56193** to mean that no syncs ever occur. NORMAL means that the WAL is synced
56194** prior to the start of checkpoint and that the database file is synced
56195** at the conclusion of the checkpoint if the entire content of the WAL
56196** was written back into the database. But no sync operations occur for
56197** an ordinary commit in NORMAL mode with WAL. FULL means that the WAL
56198** file is synced following each commit operation, in addition to the
56199** syncs associated with NORMAL. There is no difference between FULL
56200** and EXTRA for WAL mode.
56201**
56202** Do not confuse synchronous=FULL with SQLITE_SYNC_FULL. The
56203** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
56204** using fcntl(F_FULLFSYNC). SQLITE_SYNC_NORMAL means to do an
56205** ordinary fsync() call. There is no difference between SQLITE_SYNC_FULL
56206** and SQLITE_SYNC_NORMAL on platforms other than MacOSX. But the
56207** synchronous=FULL versus synchronous=NORMAL setting determines when
56208** the xSync primitive is called and is relevant to all platforms.
56209**
56210** Numeric values associated with these states are OFF==1, NORMAL=2,
56211** and FULL=3.
56212*/
56213#ifndef SQLITE_OMIT_PAGER_PRAGMAS
56214SQLITE_PRIVATE void sqlite3PagerSetFlags(
56215 Pager *pPager, /* The pager to set safety level for */
56216 unsigned pgFlags /* Various flags */
56217){
56218 unsigned level = pgFlags & PAGER_SYNCHRONOUS_MASK;
56219 if( pPager->tempFile ){
56220 pPager->noSync = 1;
56221 pPager->fullSync = 0;
56222 pPager->extraSync = 0;
56223 }else{
56224 pPager->noSync = level==PAGER_SYNCHRONOUS_OFF ?1:0;
56225 pPager->fullSync = level>=PAGER_SYNCHRONOUS_FULL ?1:0;
56226 pPager->extraSync = level==PAGER_SYNCHRONOUS_EXTRA ?1:0;
56227 }
56228 if( pPager->noSync ){
56229 pPager->syncFlags = 0;
56230 }else if( pgFlags & PAGER_FULLFSYNC ){
56231 pPager->syncFlags = SQLITE_SYNC_FULL;
56232 }else{
56233 pPager->syncFlags = SQLITE_SYNC_NORMAL;
56234 }
56235 pPager->walSyncFlags = (pPager->syncFlags<<2);
56236 if( pPager->fullSync ){
56237 pPager->walSyncFlags |= pPager->syncFlags;
56238 }
56239 if( (pgFlags & PAGER_CKPT_FULLFSYNC) && !pPager->noSync ){
56240 pPager->walSyncFlags |= (SQLITE_SYNC_FULL<<2);
56241 }
56242 if( pgFlags & PAGER_CACHESPILL ){
56243 pPager->doNotSpill &= ~SPILLFLAG_OFF;
56244 }else{
56245 pPager->doNotSpill |= SPILLFLAG_OFF;
56246 }
56247}
56248#endif
56249
56250/*
56251** The following global variable is incremented whenever the library
56252** attempts to open a temporary file. This information is used for
56253** testing and analysis only.
56254*/
56255#ifdef SQLITE_TEST
56256SQLITE_API int sqlite3_opentemp_count = 0;
56257#endif
56258
56259/*
56260** Open a temporary file.
56261**
56262** Write the file descriptor into *pFile. Return SQLITE_OK on success
56263** or some other error code if we fail. The OS will automatically
56264** delete the temporary file when it is closed.
56265**
56266** The flags passed to the VFS layer xOpen() call are those specified
56267** by parameter vfsFlags ORed with the following:
56268**
56269** SQLITE_OPEN_READWRITE
56270** SQLITE_OPEN_CREATE
56271** SQLITE_OPEN_EXCLUSIVE
56272** SQLITE_OPEN_DELETEONCLOSE
56273*/
56274static int pagerOpentemp(
56275 Pager *pPager, /* The pager object */
56276 sqlite3_file *pFile, /* Write the file descriptor here */
56277 int vfsFlags /* Flags passed through to the VFS */
56278){
56279 int rc; /* Return code */
56280
56281#ifdef SQLITE_TEST
56282 sqlite3_opentemp_count++; /* Used for testing and analysis only */
56283#endif
56284
56285 vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
56286 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
56287 rc = sqlite3OsOpen(pVfs: pPager->pVfs, zPath: 0, pFile, flags: vfsFlags, pFlagsOut: 0);
56288 assert( rc!=SQLITE_OK || isOpen(pFile) );
56289 return rc;
56290}
56291
56292/*
56293** Set the busy handler function.
56294**
56295** The pager invokes the busy-handler if sqlite3OsLock() returns
56296** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
56297** or when trying to upgrade from a RESERVED lock to an EXCLUSIVE
56298** lock. It does *not* invoke the busy handler when upgrading from
56299** SHARED to RESERVED, or when upgrading from SHARED to EXCLUSIVE
56300** (which occurs during hot-journal rollback). Summary:
56301**
56302** Transition | Invokes xBusyHandler
56303** --------------------------------------------------------
56304** NO_LOCK -> SHARED_LOCK | Yes
56305** SHARED_LOCK -> RESERVED_LOCK | No
56306** SHARED_LOCK -> EXCLUSIVE_LOCK | No
56307** RESERVED_LOCK -> EXCLUSIVE_LOCK | Yes
56308**
56309** If the busy-handler callback returns non-zero, the lock is
56310** retried. If it returns zero, then the SQLITE_BUSY error is
56311** returned to the caller of the pager API function.
56312*/
56313SQLITE_PRIVATE void sqlite3PagerSetBusyHandler(
56314 Pager *pPager, /* Pager object */
56315 int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
56316 void *pBusyHandlerArg /* Argument to pass to xBusyHandler */
56317){
56318 void **ap;
56319 pPager->xBusyHandler = xBusyHandler;
56320 pPager->pBusyHandlerArg = pBusyHandlerArg;
56321 ap = (void **)&pPager->xBusyHandler;
56322 assert( ((int(*)(void *))(ap[0]))==xBusyHandler );
56323 assert( ap[1]==pBusyHandlerArg );
56324 sqlite3OsFileControlHint(id: pPager->fd, SQLITE_FCNTL_BUSYHANDLER, pArg: (void *)ap);
56325}
56326
56327/*
56328** Change the page size used by the Pager object. The new page size
56329** is passed in *pPageSize.
56330**
56331** If the pager is in the error state when this function is called, it
56332** is a no-op. The value returned is the error state error code (i.e.
56333** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
56334**
56335** Otherwise, if all of the following are true:
56336**
56337** * the new page size (value of *pPageSize) is valid (a power
56338** of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and
56339**
56340** * there are no outstanding page references, and
56341**
56342** * the database is either not an in-memory database or it is
56343** an in-memory database that currently consists of zero pages.
56344**
56345** then the pager object page size is set to *pPageSize.
56346**
56347** If the page size is changed, then this function uses sqlite3PagerMalloc()
56348** to obtain a new Pager.pTmpSpace buffer. If this allocation attempt
56349** fails, SQLITE_NOMEM is returned and the page size remains unchanged.
56350** In all other cases, SQLITE_OK is returned.
56351**
56352** If the page size is not changed, either because one of the enumerated
56353** conditions above is not true, the pager was in error state when this
56354** function was called, or because the memory allocation attempt failed,
56355** then *pPageSize is set to the old, retained page size before returning.
56356*/
56357SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){
56358 int rc = SQLITE_OK;
56359
56360 /* It is not possible to do a full assert_pager_state() here, as this
56361 ** function may be called from within PagerOpen(), before the state
56362 ** of the Pager object is internally consistent.
56363 **
56364 ** At one point this function returned an error if the pager was in
56365 ** PAGER_ERROR state. But since PAGER_ERROR state guarantees that
56366 ** there is at least one outstanding page reference, this function
56367 ** is a no-op for that case anyhow.
56368 */
56369
56370 u32 pageSize = *pPageSize;
56371 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
56372 if( (pPager->memDb==0 || pPager->dbSize==0)
56373 && sqlite3PcacheRefCount(pCache: pPager->pPCache)==0
56374 && pageSize && pageSize!=(u32)pPager->pageSize
56375 ){
56376 char *pNew = NULL; /* New temp space */
56377 i64 nByte = 0;
56378
56379 if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
56380 rc = sqlite3OsFileSize(id: pPager->fd, pSize: &nByte);
56381 }
56382 if( rc==SQLITE_OK ){
56383 /* 8 bytes of zeroed overrun space is sufficient so that the b-tree
56384 * cell header parser will never run off the end of the allocation */
56385 pNew = (char *)sqlite3PageMalloc(sz: pageSize+8);
56386 if( !pNew ){
56387 rc = SQLITE_NOMEM_BKPT;
56388 }else{
56389 memset(s: pNew+pageSize, c: 0, n: 8);
56390 }
56391 }
56392
56393 if( rc==SQLITE_OK ){
56394 pager_reset(pPager);
56395 rc = sqlite3PcacheSetPageSize(pCache: pPager->pPCache, szPage: pageSize);
56396 }
56397 if( rc==SQLITE_OK ){
56398 sqlite3PageFree(p: pPager->pTmpSpace);
56399 pPager->pTmpSpace = pNew;
56400 pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
56401 pPager->pageSize = pageSize;
56402 }else{
56403 sqlite3PageFree(p: pNew);
56404 }
56405 }
56406
56407 *pPageSize = pPager->pageSize;
56408 if( rc==SQLITE_OK ){
56409 if( nReserve<0 ) nReserve = pPager->nReserve;
56410 assert( nReserve>=0 && nReserve<1000 );
56411 pPager->nReserve = (i16)nReserve;
56412 pagerFixMaplimit(pPager);
56413 }
56414 return rc;
56415}
56416
56417/*
56418** Return a pointer to the "temporary page" buffer held internally
56419** by the pager. This is a buffer that is big enough to hold the
56420** entire content of a database page. This buffer is used internally
56421** during rollback and will be overwritten whenever a rollback
56422** occurs. But other modules are free to use it too, as long as
56423** no rollbacks are happening.
56424*/
56425SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager *pPager){
56426 return pPager->pTmpSpace;
56427}
56428
56429/*
56430** Attempt to set the maximum database page count if mxPage is positive.
56431** Make no changes if mxPage is zero or negative. And never reduce the
56432** maximum page count below the current size of the database.
56433**
56434** Regardless of mxPage, return the current maximum page count.
56435*/
56436SQLITE_PRIVATE Pgno sqlite3PagerMaxPageCount(Pager *pPager, Pgno mxPage){
56437 if( mxPage>0 ){
56438 pPager->mxPgno = mxPage;
56439 }
56440 assert( pPager->eState!=PAGER_OPEN ); /* Called only by OP_MaxPgcnt */
56441 /* assert( pPager->mxPgno>=pPager->dbSize ); */
56442 /* OP_MaxPgcnt ensures that the parameter passed to this function is not
56443 ** less than the total number of valid pages in the database. But this
56444 ** may be less than Pager.dbSize, and so the assert() above is not valid */
56445 return pPager->mxPgno;
56446}
56447
56448/*
56449** The following set of routines are used to disable the simulated
56450** I/O error mechanism. These routines are used to avoid simulated
56451** errors in places where we do not care about errors.
56452**
56453** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
56454** and generate no code.
56455*/
56456#ifdef SQLITE_TEST
56457SQLITE_API extern int sqlite3_io_error_pending;
56458SQLITE_API extern int sqlite3_io_error_hit;
56459static int saved_cnt;
56460void disable_simulated_io_errors(void){
56461 saved_cnt = sqlite3_io_error_pending;
56462 sqlite3_io_error_pending = -1;
56463}
56464void enable_simulated_io_errors(void){
56465 sqlite3_io_error_pending = saved_cnt;
56466}
56467#else
56468# define disable_simulated_io_errors()
56469# define enable_simulated_io_errors()
56470#endif
56471
56472/*
56473** Read the first N bytes from the beginning of the file into memory
56474** that pDest points to.
56475**
56476** If the pager was opened on a transient file (zFilename==""), or
56477** opened on a file less than N bytes in size, the output buffer is
56478** zeroed and SQLITE_OK returned. The rationale for this is that this
56479** function is used to read database headers, and a new transient or
56480** zero sized database has a header than consists entirely of zeroes.
56481**
56482** If any IO error apart from SQLITE_IOERR_SHORT_READ is encountered,
56483** the error code is returned to the caller and the contents of the
56484** output buffer undefined.
56485*/
56486SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
56487 int rc = SQLITE_OK;
56488 memset(s: pDest, c: 0, n: N);
56489 assert( isOpen(pPager->fd) || pPager->tempFile );
56490
56491 /* This routine is only called by btree immediately after creating
56492 ** the Pager object. There has not been an opportunity to transition
56493 ** to WAL mode yet.
56494 */
56495 assert( !pagerUseWal(pPager) );
56496
56497 if( isOpen(pPager->fd) ){
56498 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
56499 rc = sqlite3OsRead(id: pPager->fd, pBuf: pDest, amt: N, offset: 0);
56500 if( rc==SQLITE_IOERR_SHORT_READ ){
56501 rc = SQLITE_OK;
56502 }
56503 }
56504 return rc;
56505}
56506
56507/*
56508** This function may only be called when a read-transaction is open on
56509** the pager. It returns the total number of pages in the database.
56510**
56511** However, if the file is between 1 and <page-size> bytes in size, then
56512** this is considered a 1 page file.
56513*/
56514SQLITE_PRIVATE void sqlite3PagerPagecount(Pager *pPager, int *pnPage){
56515 assert( pPager->eState>=PAGER_READER );
56516 assert( pPager->eState!=PAGER_WRITER_FINISHED );
56517 *pnPage = (int)pPager->dbSize;
56518}
56519
56520
56521/*
56522** Try to obtain a lock of type locktype on the database file. If
56523** a similar or greater lock is already held, this function is a no-op
56524** (returning SQLITE_OK immediately).
56525**
56526** Otherwise, attempt to obtain the lock using sqlite3OsLock(). Invoke
56527** the busy callback if the lock is currently not available. Repeat
56528** until the busy callback returns false or until the attempt to
56529** obtain the lock succeeds.
56530**
56531** Return SQLITE_OK on success and an error code if we cannot obtain
56532** the lock. If the lock is obtained successfully, set the Pager.state
56533** variable to locktype before returning.
56534*/
56535static int pager_wait_on_lock(Pager *pPager, int locktype){
56536 int rc; /* Return code */
56537
56538 /* Check that this is either a no-op (because the requested lock is
56539 ** already held), or one of the transitions that the busy-handler
56540 ** may be invoked during, according to the comment above
56541 ** sqlite3PagerSetBusyhandler().
56542 */
56543 assert( (pPager->eLock>=locktype)
56544 || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
56545 || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
56546 );
56547
56548 do {
56549 rc = pagerLockDb(pPager, eLock: locktype);
56550 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
56551 return rc;
56552}
56553
56554/*
56555** Function assertTruncateConstraint(pPager) checks that one of the
56556** following is true for all dirty pages currently in the page-cache:
56557**
56558** a) The page number is less than or equal to the size of the
56559** current database image, in pages, OR
56560**
56561** b) if the page content were written at this time, it would not
56562** be necessary to write the current content out to the sub-journal
56563** (as determined by function subjRequiresPage()).
56564**
56565** If the condition asserted by this function were not true, and the
56566** dirty page were to be discarded from the cache via the pagerStress()
56567** routine, pagerStress() would not write the current page content to
56568** the database file. If a savepoint transaction were rolled back after
56569** this happened, the correct behavior would be to restore the current
56570** content of the page. However, since this content is not present in either
56571** the database file or the portion of the rollback journal and
56572** sub-journal rolled back the content could not be restored and the
56573** database image would become corrupt. It is therefore fortunate that
56574** this circumstance cannot arise.
56575*/
56576#if defined(SQLITE_DEBUG)
56577static void assertTruncateConstraintCb(PgHdr *pPg){
56578 assert( pPg->flags&PGHDR_DIRTY );
56579 assert( pPg->pgno<=pPg->pPager->dbSize || !subjRequiresPage(pPg) );
56580}
56581static void assertTruncateConstraint(Pager *pPager){
56582 sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
56583}
56584#else
56585# define assertTruncateConstraint(pPager)
56586#endif
56587
56588/*
56589** Truncate the in-memory database file image to nPage pages. This
56590** function does not actually modify the database file on disk. It
56591** just sets the internal state of the pager object so that the
56592** truncation will be done when the current transaction is committed.
56593**
56594** This function is only called right before committing a transaction.
56595** Once this function has been called, the transaction must either be
56596** rolled back or committed. It is not safe to call this function and
56597** then continue writing to the database.
56598*/
56599SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
56600 assert( pPager->dbSize>=nPage || CORRUPT_DB );
56601 testcase( pPager->dbSize<nPage );
56602 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
56603 pPager->dbSize = nPage;
56604
56605 /* At one point the code here called assertTruncateConstraint() to
56606 ** ensure that all pages being truncated away by this operation are,
56607 ** if one or more savepoints are open, present in the savepoint
56608 ** journal so that they can be restored if the savepoint is rolled
56609 ** back. This is no longer necessary as this function is now only
56610 ** called right before committing a transaction. So although the
56611 ** Pager object may still have open savepoints (Pager.nSavepoint!=0),
56612 ** they cannot be rolled back. So the assertTruncateConstraint() call
56613 ** is no longer correct. */
56614}
56615
56616
56617/*
56618** This function is called before attempting a hot-journal rollback. It
56619** syncs the journal file to disk, then sets pPager->journalHdr to the
56620** size of the journal file so that the pager_playback() routine knows
56621** that the entire journal file has been synced.
56622**
56623** Syncing a hot-journal to disk before attempting to roll it back ensures
56624** that if a power-failure occurs during the rollback, the process that
56625** attempts rollback following system recovery sees the same journal
56626** content as this process.
56627**
56628** If everything goes as planned, SQLITE_OK is returned. Otherwise,
56629** an SQLite error code.
56630*/
56631static int pagerSyncHotJournal(Pager *pPager){
56632 int rc = SQLITE_OK;
56633 if( !pPager->noSync ){
56634 rc = sqlite3OsSync(id: pPager->jfd, SQLITE_SYNC_NORMAL);
56635 }
56636 if( rc==SQLITE_OK ){
56637 rc = sqlite3OsFileSize(id: pPager->jfd, pSize: &pPager->journalHdr);
56638 }
56639 return rc;
56640}
56641
56642#if SQLITE_MAX_MMAP_SIZE>0
56643/*
56644** Obtain a reference to a memory mapped page object for page number pgno.
56645** The new object will use the pointer pData, obtained from xFetch().
56646** If successful, set *ppPage to point to the new page reference
56647** and return SQLITE_OK. Otherwise, return an SQLite error code and set
56648** *ppPage to zero.
56649**
56650** Page references obtained by calling this function should be released
56651** by calling pagerReleaseMapPage().
56652*/
56653static int pagerAcquireMapPage(
56654 Pager *pPager, /* Pager object */
56655 Pgno pgno, /* Page number */
56656 void *pData, /* xFetch()'d data for this page */
56657 PgHdr **ppPage /* OUT: Acquired page object */
56658){
56659 PgHdr *p; /* Memory mapped page to return */
56660
56661 if( pPager->pMmapFreelist ){
56662 *ppPage = p = pPager->pMmapFreelist;
56663 pPager->pMmapFreelist = p->pDirty;
56664 p->pDirty = 0;
56665 assert( pPager->nExtra>=8 );
56666 memset(s: p->pExtra, c: 0, n: 8);
56667 }else{
56668 *ppPage = p = (PgHdr *)sqlite3MallocZero(n: sizeof(PgHdr) + pPager->nExtra);
56669 if( p==0 ){
56670 sqlite3OsUnfetch(id: pPager->fd, iOff: (i64)(pgno-1) * pPager->pageSize, p: pData);
56671 return SQLITE_NOMEM_BKPT;
56672 }
56673 p->pExtra = (void *)&p[1];
56674 p->flags = PGHDR_MMAP;
56675 p->nRef = 1;
56676 p->pPager = pPager;
56677 }
56678
56679 assert( p->pExtra==(void *)&p[1] );
56680 assert( p->pPage==0 );
56681 assert( p->flags==PGHDR_MMAP );
56682 assert( p->pPager==pPager );
56683 assert( p->nRef==1 );
56684
56685 p->pgno = pgno;
56686 p->pData = pData;
56687 pPager->nMmapOut++;
56688
56689 return SQLITE_OK;
56690}
56691#endif
56692
56693/*
56694** Release a reference to page pPg. pPg must have been returned by an
56695** earlier call to pagerAcquireMapPage().
56696*/
56697static void pagerReleaseMapPage(PgHdr *pPg){
56698 Pager *pPager = pPg->pPager;
56699 pPager->nMmapOut--;
56700 pPg->pDirty = pPager->pMmapFreelist;
56701 pPager->pMmapFreelist = pPg;
56702
56703 assert( pPager->fd->pMethods->iVersion>=3 );
56704 sqlite3OsUnfetch(id: pPager->fd, iOff: (i64)(pPg->pgno-1)*pPager->pageSize, p: pPg->pData);
56705}
56706
56707/*
56708** Free all PgHdr objects stored in the Pager.pMmapFreelist list.
56709*/
56710static void pagerFreeMapHdrs(Pager *pPager){
56711 PgHdr *p;
56712 PgHdr *pNext;
56713 for(p=pPager->pMmapFreelist; p; p=pNext){
56714 pNext = p->pDirty;
56715 sqlite3_free(p);
56716 }
56717}
56718
56719/* Verify that the database file has not be deleted or renamed out from
56720** under the pager. Return SQLITE_OK if the database is still where it ought
56721** to be on disk. Return non-zero (SQLITE_READONLY_DBMOVED or some other error
56722** code from sqlite3OsAccess()) if the database has gone missing.
56723*/
56724static int databaseIsUnmoved(Pager *pPager){
56725 int bHasMoved = 0;
56726 int rc;
56727
56728 if( pPager->tempFile ) return SQLITE_OK;
56729 if( pPager->dbSize==0 ) return SQLITE_OK;
56730 assert( pPager->zFilename && pPager->zFilename[0] );
56731 rc = sqlite3OsFileControl(id: pPager->fd, SQLITE_FCNTL_HAS_MOVED, pArg: &bHasMoved);
56732 if( rc==SQLITE_NOTFOUND ){
56733 /* If the HAS_MOVED file-control is unimplemented, assume that the file
56734 ** has not been moved. That is the historical behavior of SQLite: prior to
56735 ** version 3.8.3, it never checked */
56736 rc = SQLITE_OK;
56737 }else if( rc==SQLITE_OK && bHasMoved ){
56738 rc = SQLITE_READONLY_DBMOVED;
56739 }
56740 return rc;
56741}
56742
56743
56744/*
56745** Shutdown the page cache. Free all memory and close all files.
56746**
56747** If a transaction was in progress when this routine is called, that
56748** transaction is rolled back. All outstanding pages are invalidated
56749** and their memory is freed. Any attempt to use a page associated
56750** with this page cache after this function returns will likely
56751** result in a coredump.
56752**
56753** This function always succeeds. If a transaction is active an attempt
56754** is made to roll it back. If an error occurs during the rollback
56755** a hot journal may be left in the filesystem but no error is returned
56756** to the caller.
56757*/
56758SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3 *db){
56759 u8 *pTmp = (u8*)pPager->pTmpSpace;
56760 assert( db || pagerUseWal(pPager)==0 );
56761 assert( assert_pager_state(pPager) );
56762 disable_simulated_io_errors();
56763 sqlite3BeginBenignMalloc();
56764 pagerFreeMapHdrs(pPager);
56765 /* pPager->errCode = 0; */
56766 pPager->exclusiveMode = 0;
56767#ifndef SQLITE_OMIT_WAL
56768 {
56769 u8 *a = 0;
56770 assert( db || pPager->pWal==0 );
56771 if( db && 0==(db->flags & SQLITE_NoCkptOnClose)
56772 && SQLITE_OK==databaseIsUnmoved(pPager)
56773 ){
56774 a = pTmp;
56775 }
56776 sqlite3WalClose(pWal: pPager->pWal, db, sync_flags: pPager->walSyncFlags, pPager->pageSize,a);
56777 pPager->pWal = 0;
56778 }
56779#endif
56780 pager_reset(pPager);
56781 if( MEMDB ){
56782 pager_unlock(pPager);
56783 }else{
56784 /* If it is open, sync the journal file before calling UnlockAndRollback.
56785 ** If this is not done, then an unsynced portion of the open journal
56786 ** file may be played back into the database. If a power failure occurs
56787 ** while this is happening, the database could become corrupt.
56788 **
56789 ** If an error occurs while trying to sync the journal, shift the pager
56790 ** into the ERROR state. This causes UnlockAndRollback to unlock the
56791 ** database and close the journal file without attempting to roll it
56792 ** back or finalize it. The next database user will have to do hot-journal
56793 ** rollback before accessing the database file.
56794 */
56795 if( isOpen(pPager->jfd) ){
56796 pager_error(pPager, rc: pagerSyncHotJournal(pPager));
56797 }
56798 pagerUnlockAndRollback(pPager);
56799 }
56800 sqlite3EndBenignMalloc();
56801 enable_simulated_io_errors();
56802 PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
56803 IOTRACE(("CLOSE %p\n", pPager))
56804 sqlite3OsClose(pId: pPager->jfd);
56805 sqlite3OsClose(pId: pPager->fd);
56806 sqlite3PageFree(p: pTmp);
56807 sqlite3PcacheClose(pCache: pPager->pPCache);
56808 assert( !pPager->aSavepoint && !pPager->pInJournal );
56809 assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
56810
56811 sqlite3_free(p: pPager);
56812 return SQLITE_OK;
56813}
56814
56815#if !defined(NDEBUG) || defined(SQLITE_TEST)
56816/*
56817** Return the page number for page pPg.
56818*/
56819SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage *pPg){
56820 return pPg->pgno;
56821}
56822#endif
56823
56824/*
56825** Increment the reference count for page pPg.
56826*/
56827SQLITE_PRIVATE void sqlite3PagerRef(DbPage *pPg){
56828 sqlite3PcacheRef(p: pPg);
56829}
56830
56831/*
56832** Sync the journal. In other words, make sure all the pages that have
56833** been written to the journal have actually reached the surface of the
56834** disk and can be restored in the event of a hot-journal rollback.
56835**
56836** If the Pager.noSync flag is set, then this function is a no-op.
56837** Otherwise, the actions required depend on the journal-mode and the
56838** device characteristics of the file-system, as follows:
56839**
56840** * If the journal file is an in-memory journal file, no action need
56841** be taken.
56842**
56843** * Otherwise, if the device does not support the SAFE_APPEND property,
56844** then the nRec field of the most recently written journal header
56845** is updated to contain the number of journal records that have
56846** been written following it. If the pager is operating in full-sync
56847** mode, then the journal file is synced before this field is updated.
56848**
56849** * If the device does not support the SEQUENTIAL property, then
56850** journal file is synced.
56851**
56852** Or, in pseudo-code:
56853**
56854** if( NOT <in-memory journal> ){
56855** if( NOT SAFE_APPEND ){
56856** if( <full-sync mode> ) xSync(<journal file>);
56857** <update nRec field>
56858** }
56859** if( NOT SEQUENTIAL ) xSync(<journal file>);
56860** }
56861**
56862** If successful, this routine clears the PGHDR_NEED_SYNC flag of every
56863** page currently held in memory before returning SQLITE_OK. If an IO
56864** error is encountered, then the IO error code is returned to the caller.
56865*/
56866static int syncJournal(Pager *pPager, int newHdr){
56867 int rc; /* Return code */
56868
56869 assert( pPager->eState==PAGER_WRITER_CACHEMOD
56870 || pPager->eState==PAGER_WRITER_DBMOD
56871 );
56872 assert( assert_pager_state(pPager) );
56873 assert( !pagerUseWal(pPager) );
56874
56875 rc = sqlite3PagerExclusiveLock(pPager);
56876 if( rc!=SQLITE_OK ) return rc;
56877
56878 if( !pPager->noSync ){
56879 assert( !pPager->tempFile );
56880 if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
56881 const int iDc = sqlite3OsDeviceCharacteristics(id: pPager->fd);
56882 assert( isOpen(pPager->jfd) );
56883
56884 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
56885 /* This block deals with an obscure problem. If the last connection
56886 ** that wrote to this database was operating in persistent-journal
56887 ** mode, then the journal file may at this point actually be larger
56888 ** than Pager.journalOff bytes. If the next thing in the journal
56889 ** file happens to be a journal-header (written as part of the
56890 ** previous connection's transaction), and a crash or power-failure
56891 ** occurs after nRec is updated but before this connection writes
56892 ** anything else to the journal file (or commits/rolls back its
56893 ** transaction), then SQLite may become confused when doing the
56894 ** hot-journal rollback following recovery. It may roll back all
56895 ** of this connections data, then proceed to rolling back the old,
56896 ** out-of-date data that follows it. Database corruption.
56897 **
56898 ** To work around this, if the journal file does appear to contain
56899 ** a valid header following Pager.journalOff, then write a 0x00
56900 ** byte to the start of it to prevent it from being recognized.
56901 **
56902 ** Variable iNextHdrOffset is set to the offset at which this
56903 ** problematic header will occur, if it exists. aMagic is used
56904 ** as a temporary buffer to inspect the first couple of bytes of
56905 ** the potential journal header.
56906 */
56907 i64 iNextHdrOffset;
56908 u8 aMagic[8];
56909 u8 zHeader[sizeof(aJournalMagic)+4];
56910
56911 memcpy(dest: zHeader, src: aJournalMagic, n: sizeof(aJournalMagic));
56912 put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
56913
56914 iNextHdrOffset = journalHdrOffset(pPager);
56915 rc = sqlite3OsRead(id: pPager->jfd, pBuf: aMagic, amt: 8, offset: iNextHdrOffset);
56916 if( rc==SQLITE_OK && 0==memcmp(s1: aMagic, s2: aJournalMagic, n: 8) ){
56917 static const u8 zerobyte = 0;
56918 rc = sqlite3OsWrite(id: pPager->jfd, pBuf: &zerobyte, amt: 1, offset: iNextHdrOffset);
56919 }
56920 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
56921 return rc;
56922 }
56923
56924 /* Write the nRec value into the journal file header. If in
56925 ** full-synchronous mode, sync the journal first. This ensures that
56926 ** all data has really hit the disk before nRec is updated to mark
56927 ** it as a candidate for rollback.
56928 **
56929 ** This is not required if the persistent media supports the
56930 ** SAFE_APPEND property. Because in this case it is not possible
56931 ** for garbage data to be appended to the file, the nRec field
56932 ** is populated with 0xFFFFFFFF when the journal header is written
56933 ** and never needs to be updated.
56934 */
56935 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
56936 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
56937 IOTRACE(("JSYNC %p\n", pPager))
56938 rc = sqlite3OsSync(id: pPager->jfd, flags: pPager->syncFlags);
56939 if( rc!=SQLITE_OK ) return rc;
56940 }
56941 IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
56942 rc = sqlite3OsWrite(
56943 id: pPager->jfd, pBuf: zHeader, amt: sizeof(zHeader), offset: pPager->journalHdr
56944 );
56945 if( rc!=SQLITE_OK ) return rc;
56946 }
56947 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
56948 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
56949 IOTRACE(("JSYNC %p\n", pPager))
56950 rc = sqlite3OsSync(id: pPager->jfd, flags: pPager->syncFlags|
56951 (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
56952 );
56953 if( rc!=SQLITE_OK ) return rc;
56954 }
56955
56956 pPager->journalHdr = pPager->journalOff;
56957 if( newHdr && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
56958 pPager->nRec = 0;
56959 rc = writeJournalHdr(pPager);
56960 if( rc!=SQLITE_OK ) return rc;
56961 }
56962 }else{
56963 pPager->journalHdr = pPager->journalOff;
56964 }
56965 }
56966
56967 /* Unless the pager is in noSync mode, the journal file was just
56968 ** successfully synced. Either way, clear the PGHDR_NEED_SYNC flag on
56969 ** all pages.
56970 */
56971 sqlite3PcacheClearSyncFlags(pCache: pPager->pPCache);
56972 pPager->eState = PAGER_WRITER_DBMOD;
56973 assert( assert_pager_state(pPager) );
56974 return SQLITE_OK;
56975}
56976
56977/*
56978** The argument is the first in a linked list of dirty pages connected
56979** by the PgHdr.pDirty pointer. This function writes each one of the
56980** in-memory pages in the list to the database file. The argument may
56981** be NULL, representing an empty list. In this case this function is
56982** a no-op.
56983**
56984** The pager must hold at least a RESERVED lock when this function
56985** is called. Before writing anything to the database file, this lock
56986** is upgraded to an EXCLUSIVE lock. If the lock cannot be obtained,
56987** SQLITE_BUSY is returned and no data is written to the database file.
56988**
56989** If the pager is a temp-file pager and the actual file-system file
56990** is not yet open, it is created and opened before any data is
56991** written out.
56992**
56993** Once the lock has been upgraded and, if necessary, the file opened,
56994** the pages are written out to the database file in list order. Writing
56995** a page is skipped if it meets either of the following criteria:
56996**
56997** * The page number is greater than Pager.dbSize, or
56998** * The PGHDR_DONT_WRITE flag is set on the page.
56999**
57000** If writing out a page causes the database file to grow, Pager.dbFileSize
57001** is updated accordingly. If page 1 is written out, then the value cached
57002** in Pager.dbFileVers[] is updated to match the new value stored in
57003** the database file.
57004**
57005** If everything is successful, SQLITE_OK is returned. If an IO error
57006** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot
57007** be obtained, SQLITE_BUSY is returned.
57008*/
57009static int pager_write_pagelist(Pager *pPager, PgHdr *pList){
57010 int rc = SQLITE_OK; /* Return code */
57011
57012 /* This function is only called for rollback pagers in WRITER_DBMOD state. */
57013 assert( !pagerUseWal(pPager) );
57014 assert( pPager->tempFile || pPager->eState==PAGER_WRITER_DBMOD );
57015 assert( pPager->eLock==EXCLUSIVE_LOCK );
57016 assert( isOpen(pPager->fd) || pList->pDirty==0 );
57017
57018 /* If the file is a temp-file has not yet been opened, open it now. It
57019 ** is not possible for rc to be other than SQLITE_OK if this branch
57020 ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
57021 */
57022 if( !isOpen(pPager->fd) ){
57023 assert( pPager->tempFile && rc==SQLITE_OK );
57024 rc = pagerOpentemp(pPager, pFile: pPager->fd, vfsFlags: pPager->vfsFlags);
57025 }
57026
57027 /* Before the first write, give the VFS a hint of what the final
57028 ** file size will be.
57029 */
57030 assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
57031 if( rc==SQLITE_OK
57032 && pPager->dbHintSize<pPager->dbSize
57033 && (pList->pDirty || pList->pgno>pPager->dbHintSize)
57034 ){
57035 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
57036 sqlite3OsFileControlHint(id: pPager->fd, SQLITE_FCNTL_SIZE_HINT, pArg: &szFile);
57037 pPager->dbHintSize = pPager->dbSize;
57038 }
57039
57040 while( rc==SQLITE_OK && pList ){
57041 Pgno pgno = pList->pgno;
57042
57043 /* If there are dirty pages in the page cache with page numbers greater
57044 ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to
57045 ** make the file smaller (presumably by auto-vacuum code). Do not write
57046 ** any such pages to the file.
57047 **
57048 ** Also, do not write out any page that has the PGHDR_DONT_WRITE flag
57049 ** set (set by sqlite3PagerDontWrite()).
57050 */
57051 if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
57052 i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
57053 char *pData; /* Data to write */
57054
57055 assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
57056 if( pList->pgno==1 ) pager_write_changecounter(pPg: pList);
57057
57058 pData = pList->pData;
57059
57060 /* Write out the page data. */
57061 rc = sqlite3OsWrite(id: pPager->fd, pBuf: pData, amt: pPager->pageSize, offset);
57062
57063 /* If page 1 was just written, update Pager.dbFileVers to match
57064 ** the value now stored in the database file. If writing this
57065 ** page caused the database file to grow, update dbFileSize.
57066 */
57067 if( pgno==1 ){
57068 memcpy(dest: &pPager->dbFileVers, src: &pData[24], n: sizeof(pPager->dbFileVers));
57069 }
57070 if( pgno>pPager->dbFileSize ){
57071 pPager->dbFileSize = pgno;
57072 }
57073 pPager->aStat[PAGER_STAT_WRITE]++;
57074
57075 /* Update any backup objects copying the contents of this pager. */
57076 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
57077
57078 PAGERTRACE(("STORE %d page %d hash(%08x)\n",
57079 PAGERID(pPager), pgno, pager_pagehash(pList)));
57080 IOTRACE(("PGOUT %p %d\n", pPager, pgno));
57081 PAGER_INCR(sqlite3_pager_writedb_count);
57082 }else{
57083 PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno));
57084 }
57085 pager_set_pagehash(pList);
57086 pList = pList->pDirty;
57087 }
57088
57089 return rc;
57090}
57091
57092/*
57093** Ensure that the sub-journal file is open. If it is already open, this
57094** function is a no-op.
57095**
57096** SQLITE_OK is returned if everything goes according to plan. An
57097** SQLITE_IOERR_XXX error code is returned if a call to sqlite3OsOpen()
57098** fails.
57099*/
57100static int openSubJournal(Pager *pPager){
57101 int rc = SQLITE_OK;
57102 if( !isOpen(pPager->sjfd) ){
57103 const int flags = SQLITE_OPEN_SUBJOURNAL | SQLITE_OPEN_READWRITE
57104 | SQLITE_OPEN_CREATE | SQLITE_OPEN_EXCLUSIVE
57105 | SQLITE_OPEN_DELETEONCLOSE;
57106 int nStmtSpill = sqlite3Config.nStmtSpill;
57107 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
57108 nStmtSpill = -1;
57109 }
57110 rc = sqlite3JournalOpen(pPager->pVfs, 0, pPager->sjfd, flags, nStmtSpill);
57111 }
57112 return rc;
57113}
57114
57115/*
57116** Append a record of the current state of page pPg to the sub-journal.
57117**
57118** If successful, set the bit corresponding to pPg->pgno in the bitvecs
57119** for all open savepoints before returning.
57120**
57121** This function returns SQLITE_OK if everything is successful, an IO
57122** error code if the attempt to write to the sub-journal fails, or
57123** SQLITE_NOMEM if a malloc fails while setting a bit in a savepoint
57124** bitvec.
57125*/
57126static int subjournalPage(PgHdr *pPg){
57127 int rc = SQLITE_OK;
57128 Pager *pPager = pPg->pPager;
57129 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
57130
57131 /* Open the sub-journal, if it has not already been opened */
57132 assert( pPager->useJournal );
57133 assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
57134 assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
57135 assert( pagerUseWal(pPager)
57136 || pageInJournal(pPager, pPg)
57137 || pPg->pgno>pPager->dbOrigSize
57138 );
57139 rc = openSubJournal(pPager);
57140
57141 /* If the sub-journal was opened successfully (or was already open),
57142 ** write the journal record into the file. */
57143 if( rc==SQLITE_OK ){
57144 void *pData = pPg->pData;
57145 i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
57146 char *pData2;
57147 pData2 = pData;
57148 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
57149 rc = write32bits(fd: pPager->sjfd, offset, val: pPg->pgno);
57150 if( rc==SQLITE_OK ){
57151 rc = sqlite3OsWrite(id: pPager->sjfd, pBuf: pData2, amt: pPager->pageSize, offset: offset+4);
57152 }
57153 }
57154 }
57155 if( rc==SQLITE_OK ){
57156 pPager->nSubRec++;
57157 assert( pPager->nSavepoint>0 );
57158 rc = addToSavepointBitvecs(pPager, pgno: pPg->pgno);
57159 }
57160 return rc;
57161}
57162static int subjournalPageIfRequired(PgHdr *pPg){
57163 if( subjRequiresPage(pPg) ){
57164 return subjournalPage(pPg);
57165 }else{
57166 return SQLITE_OK;
57167 }
57168}
57169
57170/*
57171** This function is called by the pcache layer when it has reached some
57172** soft memory limit. The first argument is a pointer to a Pager object
57173** (cast as a void*). The pager is always 'purgeable' (not an in-memory
57174** database). The second argument is a reference to a page that is
57175** currently dirty but has no outstanding references. The page
57176** is always associated with the Pager object passed as the first
57177** argument.
57178**
57179** The job of this function is to make pPg clean by writing its contents
57180** out to the database file, if possible. This may involve syncing the
57181** journal file.
57182**
57183** If successful, sqlite3PcacheMakeClean() is called on the page and
57184** SQLITE_OK returned. If an IO error occurs while trying to make the
57185** page clean, the IO error code is returned. If the page cannot be
57186** made clean for some other reason, but no error occurs, then SQLITE_OK
57187** is returned by sqlite3PcacheMakeClean() is not called.
57188*/
57189static int pagerStress(void *p, PgHdr *pPg){
57190 Pager *pPager = (Pager *)p;
57191 int rc = SQLITE_OK;
57192
57193 assert( pPg->pPager==pPager );
57194 assert( pPg->flags&PGHDR_DIRTY );
57195
57196 /* The doNotSpill NOSYNC bit is set during times when doing a sync of
57197 ** journal (and adding a new header) is not allowed. This occurs
57198 ** during calls to sqlite3PagerWrite() while trying to journal multiple
57199 ** pages belonging to the same sector.
57200 **
57201 ** The doNotSpill ROLLBACK and OFF bits inhibits all cache spilling
57202 ** regardless of whether or not a sync is required. This is set during
57203 ** a rollback or by user request, respectively.
57204 **
57205 ** Spilling is also prohibited when in an error state since that could
57206 ** lead to database corruption. In the current implementation it
57207 ** is impossible for sqlite3PcacheFetch() to be called with createFlag==3
57208 ** while in the error state, hence it is impossible for this routine to
57209 ** be called in the error state. Nevertheless, we include a NEVER()
57210 ** test for the error state as a safeguard against future changes.
57211 */
57212 if( NEVER(pPager->errCode) ) return SQLITE_OK;
57213 testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK );
57214 testcase( pPager->doNotSpill & SPILLFLAG_OFF );
57215 testcase( pPager->doNotSpill & SPILLFLAG_NOSYNC );
57216 if( pPager->doNotSpill
57217 && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK|SPILLFLAG_OFF))!=0
57218 || (pPg->flags & PGHDR_NEED_SYNC)!=0)
57219 ){
57220 return SQLITE_OK;
57221 }
57222
57223 pPager->aStat[PAGER_STAT_SPILL]++;
57224 pPg->pDirty = 0;
57225 if( pagerUseWal(pPager) ){
57226 /* Write a single frame for this page to the log. */
57227 rc = subjournalPageIfRequired(pPg);
57228 if( rc==SQLITE_OK ){
57229 rc = pagerWalFrames(pPager, pList: pPg, nTruncate: 0, isCommit: 0);
57230 }
57231 }else{
57232
57233#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
57234 if( pPager->tempFile==0 ){
57235 rc = sqlite3JournalCreate(pPager->jfd);
57236 if( rc!=SQLITE_OK ) return pager_error(pPager, rc);
57237 }
57238#endif
57239
57240 /* Sync the journal file if required. */
57241 if( pPg->flags&PGHDR_NEED_SYNC
57242 || pPager->eState==PAGER_WRITER_CACHEMOD
57243 ){
57244 rc = syncJournal(pPager, newHdr: 1);
57245 }
57246
57247 /* Write the contents of the page out to the database file. */
57248 if( rc==SQLITE_OK ){
57249 assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
57250 rc = pager_write_pagelist(pPager, pList: pPg);
57251 }
57252 }
57253
57254 /* Mark the page as clean. */
57255 if( rc==SQLITE_OK ){
57256 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
57257 sqlite3PcacheMakeClean(p: pPg);
57258 }
57259
57260 return pager_error(pPager, rc);
57261}
57262
57263/*
57264** Flush all unreferenced dirty pages to disk.
57265*/
57266SQLITE_PRIVATE int sqlite3PagerFlush(Pager *pPager){
57267 int rc = pPager->errCode;
57268 if( !MEMDB ){
57269 PgHdr *pList = sqlite3PcacheDirtyList(pCache: pPager->pPCache);
57270 assert( assert_pager_state(pPager) );
57271 while( rc==SQLITE_OK && pList ){
57272 PgHdr *pNext = pList->pDirty;
57273 if( pList->nRef==0 ){
57274 rc = pagerStress(p: (void*)pPager, pPg: pList);
57275 }
57276 pList = pNext;
57277 }
57278 }
57279
57280 return rc;
57281}
57282
57283/*
57284** Allocate and initialize a new Pager object and put a pointer to it
57285** in *ppPager. The pager should eventually be freed by passing it
57286** to sqlite3PagerClose().
57287**
57288** The zFilename argument is the path to the database file to open.
57289** If zFilename is NULL then a randomly-named temporary file is created
57290** and used as the file to be cached. Temporary files are be deleted
57291** automatically when they are closed. If zFilename is ":memory:" then
57292** all information is held in cache. It is never written to disk.
57293** This can be used to implement an in-memory database.
57294**
57295** The nExtra parameter specifies the number of bytes of space allocated
57296** along with each page reference. This space is available to the user
57297** via the sqlite3PagerGetExtra() API. When a new page is allocated, the
57298** first 8 bytes of this space are zeroed but the remainder is uninitialized.
57299** (The extra space is used by btree as the MemPage object.)
57300**
57301** The flags argument is used to specify properties that affect the
57302** operation of the pager. It should be passed some bitwise combination
57303** of the PAGER_* flags.
57304**
57305** The vfsFlags parameter is a bitmask to pass to the flags parameter
57306** of the xOpen() method of the supplied VFS when opening files.
57307**
57308** If the pager object is allocated and the specified file opened
57309** successfully, SQLITE_OK is returned and *ppPager set to point to
57310** the new pager object. If an error occurs, *ppPager is set to NULL
57311** and error code returned. This function may return SQLITE_NOMEM
57312** (sqlite3Malloc() is used to allocate memory), SQLITE_CANTOPEN or
57313** various SQLITE_IO_XXX errors.
57314*/
57315SQLITE_PRIVATE int sqlite3PagerOpen(
57316 sqlite3_vfs *pVfs, /* The virtual file system to use */
57317 Pager **ppPager, /* OUT: Return the Pager structure here */
57318 const char *zFilename, /* Name of the database file to open */
57319 int nExtra, /* Extra bytes append to each in-memory page */
57320 int flags, /* flags controlling this file */
57321 int vfsFlags, /* flags passed through to sqlite3_vfs.xOpen() */
57322 void (*xReinit)(DbPage*) /* Function to reinitialize pages */
57323){
57324 u8 *pPtr;
57325 Pager *pPager = 0; /* Pager object to allocate and return */
57326 int rc = SQLITE_OK; /* Return code */
57327 int tempFile = 0; /* True for temp files (incl. in-memory files) */
57328 int memDb = 0; /* True if this is an in-memory file */
57329#ifndef SQLITE_OMIT_DESERIALIZE
57330 int memJM = 0; /* Memory journal mode */
57331#else
57332# define memJM 0
57333#endif
57334 int readOnly = 0; /* True if this is a read-only file */
57335 int journalFileSize; /* Bytes to allocate for each journal fd */
57336 char *zPathname = 0; /* Full path to database file */
57337 int nPathname = 0; /* Number of bytes in zPathname */
57338 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
57339 int pcacheSize = sqlite3PcacheSize(); /* Bytes to allocate for PCache */
57340 u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE; /* Default page size */
57341 const char *zUri = 0; /* URI args to copy */
57342 int nUriByte = 1; /* Number of bytes of URI args at *zUri */
57343 int nUri = 0; /* Number of URI parameters */
57344
57345 /* Figure out how much space is required for each journal file-handle
57346 ** (there are two of them, the main journal and the sub-journal). */
57347 journalFileSize = ROUND8(sqlite3JournalSize(pVfs));
57348
57349 /* Set the output variable to NULL in case an error occurs. */
57350 *ppPager = 0;
57351
57352#ifndef SQLITE_OMIT_MEMORYDB
57353 if( flags & PAGER_MEMORY ){
57354 memDb = 1;
57355 if( zFilename && zFilename[0] ){
57356 zPathname = sqlite3DbStrDup(db: 0, z: zFilename);
57357 if( zPathname==0 ) return SQLITE_NOMEM_BKPT;
57358 nPathname = sqlite3Strlen30(z: zPathname);
57359 zFilename = 0;
57360 }
57361 }
57362#endif
57363
57364 /* Compute and store the full pathname in an allocated buffer pointed
57365 ** to by zPathname, length nPathname. Or, if this is a temporary file,
57366 ** leave both nPathname and zPathname set to 0.
57367 */
57368 if( zFilename && zFilename[0] ){
57369 const char *z;
57370 nPathname = pVfs->mxPathname+1;
57371 zPathname = sqlite3DbMallocRaw(db: 0, n: nPathname*2);
57372 if( zPathname==0 ){
57373 return SQLITE_NOMEM_BKPT;
57374 }
57375 zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */
57376 rc = sqlite3OsFullPathname(pVfs, zPath: zFilename, nPathOut: nPathname, zPathOut: zPathname);
57377 if( rc!=SQLITE_OK ){
57378 if( rc==SQLITE_OK_SYMLINK ){
57379 if( vfsFlags & SQLITE_OPEN_NOFOLLOW ){
57380 rc = SQLITE_CANTOPEN_SYMLINK;
57381 }else{
57382 rc = SQLITE_OK;
57383 }
57384 }
57385 }
57386 nPathname = sqlite3Strlen30(z: zPathname);
57387 z = zUri = &zFilename[sqlite3Strlen30(z: zFilename)+1];
57388 while( *z ){
57389 z += strlen(s: z)+1;
57390 z += strlen(s: z)+1;
57391 nUri++;
57392 }
57393 nUriByte = (int)(&z[1] - zUri);
57394 assert( nUriByte>=1 );
57395 if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
57396 /* This branch is taken when the journal path required by
57397 ** the database being opened will be more than pVfs->mxPathname
57398 ** bytes in length. This means the database cannot be opened,
57399 ** as it will not be possible to open the journal file or even
57400 ** check for a hot-journal before reading.
57401 */
57402 rc = SQLITE_CANTOPEN_BKPT;
57403 }
57404 if( rc!=SQLITE_OK ){
57405 sqlite3DbFree(db: 0, p: zPathname);
57406 return rc;
57407 }
57408 }
57409
57410 /* Allocate memory for the Pager structure, PCache object, the
57411 ** three file descriptors, the database file name and the journal
57412 ** file name. The layout in memory is as follows:
57413 **
57414 ** Pager object (sizeof(Pager) bytes)
57415 ** PCache object (sqlite3PcacheSize() bytes)
57416 ** Database file handle (pVfs->szOsFile bytes)
57417 ** Sub-journal file handle (journalFileSize bytes)
57418 ** Main journal file handle (journalFileSize bytes)
57419 ** Ptr back to the Pager (sizeof(Pager*) bytes)
57420 ** \0\0\0\0 database prefix (4 bytes)
57421 ** Database file name (nPathname+1 bytes)
57422 ** URI query parameters (nUriByte bytes)
57423 ** Journal filename (nPathname+8+1 bytes)
57424 ** WAL filename (nPathname+4+1 bytes)
57425 ** \0\0\0 terminator (3 bytes)
57426 **
57427 ** Some 3rd-party software, over which we have no control, depends on
57428 ** the specific order of the filenames and the \0 separators between them
57429 ** so that it can (for example) find the database filename given the WAL
57430 ** filename without using the sqlite3_filename_database() API. This is a
57431 ** misuse of SQLite and a bug in the 3rd-party software, but the 3rd-party
57432 ** software is in widespread use, so we try to avoid changing the filename
57433 ** order and formatting if possible. In particular, the details of the
57434 ** filename format expected by 3rd-party software should be as follows:
57435 **
57436 ** - Main Database Path
57437 ** - \0
57438 ** - Multiple URI components consisting of:
57439 ** - Key
57440 ** - \0
57441 ** - Value
57442 ** - \0
57443 ** - \0
57444 ** - Journal Path
57445 ** - \0
57446 ** - WAL Path (zWALName)
57447 ** - \0
57448 **
57449 ** The sqlite3_create_filename() interface and the databaseFilename() utility
57450 ** that is used by sqlite3_filename_database() and kin also depend on the
57451 ** specific formatting and order of the various filenames, so if the format
57452 ** changes here, be sure to change it there as well.
57453 */
57454 pPtr = (u8 *)sqlite3MallocZero(
57455 ROUND8(sizeof(*pPager)) + /* Pager structure */
57456 ROUND8(pcacheSize) + /* PCache object */
57457 ROUND8(pVfs->szOsFile) + /* The main db file */
57458 journalFileSize * 2 + /* The two journal files */
57459 sizeof(pPager) + /* Space to hold a pointer */
57460 4 + /* Database prefix */
57461 nPathname + 1 + /* database filename */
57462 nUriByte + /* query parameters */
57463 nPathname + 8 + 1 + /* Journal filename */
57464#ifndef SQLITE_OMIT_WAL
57465 nPathname + 4 + 1 + /* WAL filename */
57466#endif
57467 3 /* Terminator */
57468 );
57469 assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) );
57470 if( !pPtr ){
57471 sqlite3DbFree(db: 0, p: zPathname);
57472 return SQLITE_NOMEM_BKPT;
57473 }
57474 pPager = (Pager*)pPtr; pPtr += ROUND8(sizeof(*pPager));
57475 pPager->pPCache = (PCache*)pPtr; pPtr += ROUND8(pcacheSize);
57476 pPager->fd = (sqlite3_file*)pPtr; pPtr += ROUND8(pVfs->szOsFile);
57477 pPager->sjfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
57478 pPager->jfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
57479 assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
57480 memcpy(dest: pPtr, src: &pPager, n: sizeof(pPager)); pPtr += sizeof(pPager);
57481
57482 /* Fill in the Pager.zFilename and pPager.zQueryParam fields */
57483 pPtr += 4; /* Skip zero prefix */
57484 pPager->zFilename = (char*)pPtr;
57485 if( nPathname>0 ){
57486 memcpy(dest: pPtr, src: zPathname, n: nPathname); pPtr += nPathname + 1;
57487 if( zUri ){
57488 memcpy(dest: pPtr, src: zUri, n: nUriByte); pPtr += nUriByte;
57489 }else{
57490 pPtr++;
57491 }
57492 }
57493
57494
57495 /* Fill in Pager.zJournal */
57496 if( nPathname>0 ){
57497 pPager->zJournal = (char*)pPtr;
57498 memcpy(dest: pPtr, src: zPathname, n: nPathname); pPtr += nPathname;
57499 memcpy(dest: pPtr, src: "-journal",n: 8); pPtr += 8 + 1;
57500#ifdef SQLITE_ENABLE_8_3_NAMES
57501 sqlite3FileSuffix3(zFilename,pPager->zJournal);
57502 pPtr = (u8*)(pPager->zJournal + sqlite3Strlen30(pPager->zJournal)+1);
57503#endif
57504 }else{
57505 pPager->zJournal = 0;
57506 }
57507
57508#ifndef SQLITE_OMIT_WAL
57509 /* Fill in Pager.zWal */
57510 if( nPathname>0 ){
57511 pPager->zWal = (char*)pPtr;
57512 memcpy(dest: pPtr, src: zPathname, n: nPathname); pPtr += nPathname;
57513 memcpy(dest: pPtr, src: "-wal", n: 4); pPtr += 4 + 1;
57514#ifdef SQLITE_ENABLE_8_3_NAMES
57515 sqlite3FileSuffix3(zFilename, pPager->zWal);
57516 pPtr = (u8*)(pPager->zWal + sqlite3Strlen30(pPager->zWal)+1);
57517#endif
57518 }else{
57519 pPager->zWal = 0;
57520 }
57521#endif
57522 (void)pPtr; /* Suppress warning about unused pPtr value */
57523
57524 if( nPathname ) sqlite3DbFree(db: 0, p: zPathname);
57525 pPager->pVfs = pVfs;
57526 pPager->vfsFlags = vfsFlags;
57527
57528 /* Open the pager file.
57529 */
57530 if( zFilename && zFilename[0] ){
57531 int fout = 0; /* VFS flags returned by xOpen() */
57532 rc = sqlite3OsOpen(pVfs, zPath: pPager->zFilename, pFile: pPager->fd, flags: vfsFlags, pFlagsOut: &fout);
57533 assert( !memDb );
57534#ifndef SQLITE_OMIT_DESERIALIZE
57535 pPager->memVfs = memJM = (fout&SQLITE_OPEN_MEMORY)!=0;
57536#endif
57537 readOnly = (fout&SQLITE_OPEN_READONLY)!=0;
57538
57539 /* If the file was successfully opened for read/write access,
57540 ** choose a default page size in case we have to create the
57541 ** database file. The default page size is the maximum of:
57542 **
57543 ** + SQLITE_DEFAULT_PAGE_SIZE,
57544 ** + The value returned by sqlite3OsSectorSize()
57545 ** + The largest page size that can be written atomically.
57546 */
57547 if( rc==SQLITE_OK ){
57548 int iDc = sqlite3OsDeviceCharacteristics(id: pPager->fd);
57549 if( !readOnly ){
57550 setSectorSize(pPager);
57551 assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE);
57552 if( szPageDflt<pPager->sectorSize ){
57553 if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
57554 szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
57555 }else{
57556 szPageDflt = (u32)pPager->sectorSize;
57557 }
57558 }
57559#ifdef SQLITE_ENABLE_ATOMIC_WRITE
57560 {
57561 int ii;
57562 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
57563 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
57564 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
57565 for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
57566 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){
57567 szPageDflt = ii;
57568 }
57569 }
57570 }
57571#endif
57572 }
57573 pPager->noLock = sqlite3_uri_boolean(zFile: pPager->zFilename, zParam: "nolock", bDefault: 0);
57574 if( (iDc & SQLITE_IOCAP_IMMUTABLE)!=0
57575 || sqlite3_uri_boolean(zFile: pPager->zFilename, zParam: "immutable", bDefault: 0) ){
57576 vfsFlags |= SQLITE_OPEN_READONLY;
57577 goto act_like_temp_file;
57578 }
57579 }
57580 }else{
57581 /* If a temporary file is requested, it is not opened immediately.
57582 ** In this case we accept the default page size and delay actually
57583 ** opening the file until the first call to OsWrite().
57584 **
57585 ** This branch is also run for an in-memory database. An in-memory
57586 ** database is the same as a temp-file that is never written out to
57587 ** disk and uses an in-memory rollback journal.
57588 **
57589 ** This branch also runs for files marked as immutable.
57590 */
57591act_like_temp_file:
57592 tempFile = 1;
57593 pPager->eState = PAGER_READER; /* Pretend we already have a lock */
57594 pPager->eLock = EXCLUSIVE_LOCK; /* Pretend we are in EXCLUSIVE mode */
57595 pPager->noLock = 1; /* Do no locking */
57596 readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
57597 }
57598
57599 /* The following call to PagerSetPagesize() serves to set the value of
57600 ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
57601 */
57602 if( rc==SQLITE_OK ){
57603 assert( pPager->memDb==0 );
57604 rc = sqlite3PagerSetPagesize(pPager, pPageSize: &szPageDflt, nReserve: -1);
57605 testcase( rc!=SQLITE_OK );
57606 }
57607
57608 /* Initialize the PCache object. */
57609 if( rc==SQLITE_OK ){
57610 nExtra = ROUND8(nExtra);
57611 assert( nExtra>=8 && nExtra<1000 );
57612 rc = sqlite3PcacheOpen(szPage: szPageDflt, szExtra: nExtra, bPurgeable: !memDb,
57613 xStress: !memDb?pagerStress:0, pStress: (void *)pPager, p: pPager->pPCache);
57614 }
57615
57616 /* If an error occurred above, free the Pager structure and close the file.
57617 */
57618 if( rc!=SQLITE_OK ){
57619 sqlite3OsClose(pId: pPager->fd);
57620 sqlite3PageFree(p: pPager->pTmpSpace);
57621 sqlite3_free(p: pPager);
57622 return rc;
57623 }
57624
57625 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
57626 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
57627
57628 pPager->useJournal = (u8)useJournal;
57629 /* pPager->stmtOpen = 0; */
57630 /* pPager->stmtInUse = 0; */
57631 /* pPager->nRef = 0; */
57632 /* pPager->stmtSize = 0; */
57633 /* pPager->stmtJSize = 0; */
57634 /* pPager->nPage = 0; */
57635 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
57636 /* pPager->state = PAGER_UNLOCK; */
57637 /* pPager->errMask = 0; */
57638 pPager->tempFile = (u8)tempFile;
57639 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
57640 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
57641 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
57642 pPager->exclusiveMode = (u8)tempFile;
57643 pPager->changeCountDone = pPager->tempFile;
57644 pPager->memDb = (u8)memDb;
57645 pPager->readOnly = (u8)readOnly;
57646 assert( useJournal || pPager->tempFile );
57647 pPager->noSync = pPager->tempFile;
57648 if( pPager->noSync ){
57649 assert( pPager->fullSync==0 );
57650 assert( pPager->extraSync==0 );
57651 assert( pPager->syncFlags==0 );
57652 assert( pPager->walSyncFlags==0 );
57653 }else{
57654 pPager->fullSync = 1;
57655 pPager->extraSync = 0;
57656 pPager->syncFlags = SQLITE_SYNC_NORMAL;
57657 pPager->walSyncFlags = SQLITE_SYNC_NORMAL | (SQLITE_SYNC_NORMAL<<2);
57658 }
57659 /* pPager->pFirst = 0; */
57660 /* pPager->pFirstSynced = 0; */
57661 /* pPager->pLast = 0; */
57662 pPager->nExtra = (u16)nExtra;
57663 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
57664 assert( isOpen(pPager->fd) || tempFile );
57665 setSectorSize(pPager);
57666 if( !useJournal ){
57667 pPager->journalMode = PAGER_JOURNALMODE_OFF;
57668 }else if( memDb || memJM ){
57669 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
57670 }
57671 /* pPager->xBusyHandler = 0; */
57672 /* pPager->pBusyHandlerArg = 0; */
57673 pPager->xReiniter = xReinit;
57674 setGetterMethod(pPager);
57675 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
57676 /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
57677
57678 *ppPager = pPager;
57679 return SQLITE_OK;
57680}
57681
57682/*
57683** Return the sqlite3_file for the main database given the name
57684** of the corresonding WAL or Journal name as passed into
57685** xOpen.
57686*/
57687SQLITE_API sqlite3_file *sqlite3_database_file_object(const char *zName){
57688 Pager *pPager;
57689 while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
57690 zName--;
57691 }
57692 pPager = *(Pager**)(zName - 4 - sizeof(Pager*));
57693 return pPager->fd;
57694}
57695
57696
57697/*
57698** This function is called after transitioning from PAGER_UNLOCK to
57699** PAGER_SHARED state. It tests if there is a hot journal present in
57700** the file-system for the given pager. A hot journal is one that
57701** needs to be played back. According to this function, a hot-journal
57702** file exists if the following criteria are met:
57703**
57704** * The journal file exists in the file system, and
57705** * No process holds a RESERVED or greater lock on the database file, and
57706** * The database file itself is greater than 0 bytes in size, and
57707** * The first byte of the journal file exists and is not 0x00.
57708**
57709** If the current size of the database file is 0 but a journal file
57710** exists, that is probably an old journal left over from a prior
57711** database with the same name. In this case the journal file is
57712** just deleted using OsDelete, *pExists is set to 0 and SQLITE_OK
57713** is returned.
57714**
57715** This routine does not check if there is a super-journal filename
57716** at the end of the file. If there is, and that super-journal file
57717** does not exist, then the journal file is not really hot. In this
57718** case this routine will return a false-positive. The pager_playback()
57719** routine will discover that the journal file is not really hot and
57720** will not roll it back.
57721**
57722** If a hot-journal file is found to exist, *pExists is set to 1 and
57723** SQLITE_OK returned. If no hot-journal file is present, *pExists is
57724** set to 0 and SQLITE_OK returned. If an IO error occurs while trying
57725** to determine whether or not a hot-journal file exists, the IO error
57726** code is returned and the value of *pExists is undefined.
57727*/
57728static int hasHotJournal(Pager *pPager, int *pExists){
57729 sqlite3_vfs * const pVfs = pPager->pVfs;
57730 int rc = SQLITE_OK; /* Return code */
57731 int exists = 1; /* True if a journal file is present */
57732 int jrnlOpen = !!isOpen(pPager->jfd);
57733
57734 assert( pPager->useJournal );
57735 assert( isOpen(pPager->fd) );
57736 assert( pPager->eState==PAGER_OPEN );
57737
57738 assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
57739 SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
57740 ));
57741
57742 *pExists = 0;
57743 if( !jrnlOpen ){
57744 rc = sqlite3OsAccess(pVfs, zPath: pPager->zJournal, SQLITE_ACCESS_EXISTS, pResOut: &exists);
57745 }
57746 if( rc==SQLITE_OK && exists ){
57747 int locked = 0; /* True if some process holds a RESERVED lock */
57748
57749 /* Race condition here: Another process might have been holding the
57750 ** the RESERVED lock and have a journal open at the sqlite3OsAccess()
57751 ** call above, but then delete the journal and drop the lock before
57752 ** we get to the following sqlite3OsCheckReservedLock() call. If that
57753 ** is the case, this routine might think there is a hot journal when
57754 ** in fact there is none. This results in a false-positive which will
57755 ** be dealt with by the playback routine. Ticket #3883.
57756 */
57757 rc = sqlite3OsCheckReservedLock(id: pPager->fd, pResOut: &locked);
57758 if( rc==SQLITE_OK && !locked ){
57759 Pgno nPage; /* Number of pages in database file */
57760
57761 assert( pPager->tempFile==0 );
57762 rc = pagerPagecount(pPager, pnPage: &nPage);
57763 if( rc==SQLITE_OK ){
57764 /* If the database is zero pages in size, that means that either (1) the
57765 ** journal is a remnant from a prior database with the same name where
57766 ** the database file but not the journal was deleted, or (2) the initial
57767 ** transaction that populates a new database is being rolled back.
57768 ** In either case, the journal file can be deleted. However, take care
57769 ** not to delete the journal file if it is already open due to
57770 ** journal_mode=PERSIST.
57771 */
57772 if( nPage==0 && !jrnlOpen ){
57773 sqlite3BeginBenignMalloc();
57774 if( pagerLockDb(pPager, RESERVED_LOCK)==SQLITE_OK ){
57775 sqlite3OsDelete(pVfs, zPath: pPager->zJournal, dirSync: 0);
57776 if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
57777 }
57778 sqlite3EndBenignMalloc();
57779 }else{
57780 /* The journal file exists and no other connection has a reserved
57781 ** or greater lock on the database file. Now check that there is
57782 ** at least one non-zero bytes at the start of the journal file.
57783 ** If there is, then we consider this journal to be hot. If not,
57784 ** it can be ignored.
57785 */
57786 if( !jrnlOpen ){
57787 int f = SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL;
57788 rc = sqlite3OsOpen(pVfs, zPath: pPager->zJournal, pFile: pPager->jfd, flags: f, pFlagsOut: &f);
57789 }
57790 if( rc==SQLITE_OK ){
57791 u8 first = 0;
57792 rc = sqlite3OsRead(id: pPager->jfd, pBuf: (void *)&first, amt: 1, offset: 0);
57793 if( rc==SQLITE_IOERR_SHORT_READ ){
57794 rc = SQLITE_OK;
57795 }
57796 if( !jrnlOpen ){
57797 sqlite3OsClose(pId: pPager->jfd);
57798 }
57799 *pExists = (first!=0);
57800 }else if( rc==SQLITE_CANTOPEN ){
57801 /* If we cannot open the rollback journal file in order to see if
57802 ** it has a zero header, that might be due to an I/O error, or
57803 ** it might be due to the race condition described above and in
57804 ** ticket #3883. Either way, assume that the journal is hot.
57805 ** This might be a false positive. But if it is, then the
57806 ** automatic journal playback and recovery mechanism will deal
57807 ** with it under an EXCLUSIVE lock where we do not need to
57808 ** worry so much with race conditions.
57809 */
57810 *pExists = 1;
57811 rc = SQLITE_OK;
57812 }
57813 }
57814 }
57815 }
57816 }
57817
57818 return rc;
57819}
57820
57821/*
57822** This function is called to obtain a shared lock on the database file.
57823** It is illegal to call sqlite3PagerGet() until after this function
57824** has been successfully called. If a shared-lock is already held when
57825** this function is called, it is a no-op.
57826**
57827** The following operations are also performed by this function.
57828**
57829** 1) If the pager is currently in PAGER_OPEN state (no lock held
57830** on the database file), then an attempt is made to obtain a
57831** SHARED lock on the database file. Immediately after obtaining
57832** the SHARED lock, the file-system is checked for a hot-journal,
57833** which is played back if present. Following any hot-journal
57834** rollback, the contents of the cache are validated by checking
57835** the 'change-counter' field of the database file header and
57836** discarded if they are found to be invalid.
57837**
57838** 2) If the pager is running in exclusive-mode, and there are currently
57839** no outstanding references to any pages, and is in the error state,
57840** then an attempt is made to clear the error state by discarding
57841** the contents of the page cache and rolling back any open journal
57842** file.
57843**
57844** If everything is successful, SQLITE_OK is returned. If an IO error
57845** occurs while locking the database, checking for a hot-journal file or
57846** rolling back a journal file, the IO error code is returned.
57847*/
57848SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){
57849 int rc = SQLITE_OK; /* Return code */
57850
57851 /* This routine is only called from b-tree and only when there are no
57852 ** outstanding pages. This implies that the pager state should either
57853 ** be OPEN or READER. READER is only possible if the pager is or was in
57854 ** exclusive access mode. */
57855 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
57856 assert( assert_pager_state(pPager) );
57857 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
57858 assert( pPager->errCode==SQLITE_OK );
57859
57860 if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
57861 int bHotJournal = 1; /* True if there exists a hot journal-file */
57862
57863 assert( !MEMDB );
57864 assert( pPager->tempFile==0 || pPager->eLock==EXCLUSIVE_LOCK );
57865
57866 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
57867 if( rc!=SQLITE_OK ){
57868 assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
57869 goto failed;
57870 }
57871
57872 /* If a journal file exists, and there is no RESERVED lock on the
57873 ** database file, then it either needs to be played back or deleted.
57874 */
57875 if( pPager->eLock<=SHARED_LOCK ){
57876 rc = hasHotJournal(pPager, pExists: &bHotJournal);
57877 }
57878 if( rc!=SQLITE_OK ){
57879 goto failed;
57880 }
57881 if( bHotJournal ){
57882 if( pPager->readOnly ){
57883 rc = SQLITE_READONLY_ROLLBACK;
57884 goto failed;
57885 }
57886
57887 /* Get an EXCLUSIVE lock on the database file. At this point it is
57888 ** important that a RESERVED lock is not obtained on the way to the
57889 ** EXCLUSIVE lock. If it were, another process might open the
57890 ** database file, detect the RESERVED lock, and conclude that the
57891 ** database is safe to read while this process is still rolling the
57892 ** hot-journal back.
57893 **
57894 ** Because the intermediate RESERVED lock is not requested, any
57895 ** other process attempting to access the database file will get to
57896 ** this point in the code and fail to obtain its own EXCLUSIVE lock
57897 ** on the database file.
57898 **
57899 ** Unless the pager is in locking_mode=exclusive mode, the lock is
57900 ** downgraded to SHARED_LOCK before this function returns.
57901 */
57902 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
57903 if( rc!=SQLITE_OK ){
57904 goto failed;
57905 }
57906
57907 /* If it is not already open and the file exists on disk, open the
57908 ** journal for read/write access. Write access is required because
57909 ** in exclusive-access mode the file descriptor will be kept open
57910 ** and possibly used for a transaction later on. Also, write-access
57911 ** is usually required to finalize the journal in journal_mode=persist
57912 ** mode (and also for journal_mode=truncate on some systems).
57913 **
57914 ** If the journal does not exist, it usually means that some
57915 ** other connection managed to get in and roll it back before
57916 ** this connection obtained the exclusive lock above. Or, it
57917 ** may mean that the pager was in the error-state when this
57918 ** function was called and the journal file does not exist.
57919 */
57920 if( !isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
57921 sqlite3_vfs * const pVfs = pPager->pVfs;
57922 int bExists; /* True if journal file exists */
57923 rc = sqlite3OsAccess(
57924 pVfs, zPath: pPager->zJournal, SQLITE_ACCESS_EXISTS, pResOut: &bExists);
57925 if( rc==SQLITE_OK && bExists ){
57926 int fout = 0;
57927 int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
57928 assert( !pPager->tempFile );
57929 rc = sqlite3OsOpen(pVfs, zPath: pPager->zJournal, pFile: pPager->jfd, flags: f, pFlagsOut: &fout);
57930 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
57931 if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
57932 rc = SQLITE_CANTOPEN_BKPT;
57933 sqlite3OsClose(pId: pPager->jfd);
57934 }
57935 }
57936 }
57937
57938 /* Playback and delete the journal. Drop the database write
57939 ** lock and reacquire the read lock. Purge the cache before
57940 ** playing back the hot-journal so that we don't end up with
57941 ** an inconsistent cache. Sync the hot journal before playing
57942 ** it back since the process that crashed and left the hot journal
57943 ** probably did not sync it and we are required to always sync
57944 ** the journal before playing it back.
57945 */
57946 if( isOpen(pPager->jfd) ){
57947 assert( rc==SQLITE_OK );
57948 rc = pagerSyncHotJournal(pPager);
57949 if( rc==SQLITE_OK ){
57950 rc = pager_playback(pPager, isHot: !pPager->tempFile);
57951 pPager->eState = PAGER_OPEN;
57952 }
57953 }else if( !pPager->exclusiveMode ){
57954 pagerUnlockDb(pPager, SHARED_LOCK);
57955 }
57956
57957 if( rc!=SQLITE_OK ){
57958 /* This branch is taken if an error occurs while trying to open
57959 ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
57960 ** pager_unlock() routine will be called before returning to unlock
57961 ** the file. If the unlock attempt fails, then Pager.eLock must be
57962 ** set to UNKNOWN_LOCK (see the comment above the #define for
57963 ** UNKNOWN_LOCK above for an explanation).
57964 **
57965 ** In order to get pager_unlock() to do this, set Pager.eState to
57966 ** PAGER_ERROR now. This is not actually counted as a transition
57967 ** to ERROR state in the state diagram at the top of this file,
57968 ** since we know that the same call to pager_unlock() will very
57969 ** shortly transition the pager object to the OPEN state. Calling
57970 ** assert_pager_state() would fail now, as it should not be possible
57971 ** to be in ERROR state when there are zero outstanding page
57972 ** references.
57973 */
57974 pager_error(pPager, rc);
57975 goto failed;
57976 }
57977
57978 assert( pPager->eState==PAGER_OPEN );
57979 assert( (pPager->eLock==SHARED_LOCK)
57980 || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
57981 );
57982 }
57983
57984 if( !pPager->tempFile && pPager->hasHeldSharedLock ){
57985 /* The shared-lock has just been acquired then check to
57986 ** see if the database has been modified. If the database has changed,
57987 ** flush the cache. The hasHeldSharedLock flag prevents this from
57988 ** occurring on the very first access to a file, in order to save a
57989 ** single unnecessary sqlite3OsRead() call at the start-up.
57990 **
57991 ** Database changes are detected by looking at 15 bytes beginning
57992 ** at offset 24 into the file. The first 4 of these 16 bytes are
57993 ** a 32-bit counter that is incremented with each change. The
57994 ** other bytes change randomly with each file change when
57995 ** a codec is in use.
57996 **
57997 ** There is a vanishingly small chance that a change will not be
57998 ** detected. The chance of an undetected change is so small that
57999 ** it can be neglected.
58000 */
58001 char dbFileVers[sizeof(pPager->dbFileVers)];
58002
58003 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
58004 rc = sqlite3OsRead(id: pPager->fd, pBuf: &dbFileVers, amt: sizeof(dbFileVers), offset: 24);
58005 if( rc!=SQLITE_OK ){
58006 if( rc!=SQLITE_IOERR_SHORT_READ ){
58007 goto failed;
58008 }
58009 memset(s: dbFileVers, c: 0, n: sizeof(dbFileVers));
58010 }
58011
58012 if( memcmp(s1: pPager->dbFileVers, s2: dbFileVers, n: sizeof(dbFileVers))!=0 ){
58013 pager_reset(pPager);
58014
58015 /* Unmap the database file. It is possible that external processes
58016 ** may have truncated the database file and then extended it back
58017 ** to its original size while this process was not holding a lock.
58018 ** In this case there may exist a Pager.pMap mapping that appears
58019 ** to be the right size but is not actually valid. Avoid this
58020 ** possibility by unmapping the db here. */
58021 if( USEFETCH(pPager) ){
58022 sqlite3OsUnfetch(id: pPager->fd, iOff: 0, p: 0);
58023 }
58024 }
58025 }
58026
58027 /* If there is a WAL file in the file-system, open this database in WAL
58028 ** mode. Otherwise, the following function call is a no-op.
58029 */
58030 rc = pagerOpenWalIfPresent(pPager);
58031#ifndef SQLITE_OMIT_WAL
58032 assert( pPager->pWal==0 || rc==SQLITE_OK );
58033#endif
58034 }
58035
58036 if( pagerUseWal(pPager) ){
58037 assert( rc==SQLITE_OK );
58038 rc = pagerBeginReadTransaction(pPager);
58039 }
58040
58041 if( pPager->tempFile==0 && pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
58042 rc = pagerPagecount(pPager, pnPage: &pPager->dbSize);
58043 }
58044
58045 failed:
58046 if( rc!=SQLITE_OK ){
58047 assert( !MEMDB );
58048 pager_unlock(pPager);
58049 assert( pPager->eState==PAGER_OPEN );
58050 }else{
58051 pPager->eState = PAGER_READER;
58052 pPager->hasHeldSharedLock = 1;
58053 }
58054 return rc;
58055}
58056
58057/*
58058** If the reference count has reached zero, rollback any active
58059** transaction and unlock the pager.
58060**
58061** Except, in locking_mode=EXCLUSIVE when there is nothing to in
58062** the rollback journal, the unlock is not performed and there is
58063** nothing to rollback, so this routine is a no-op.
58064*/
58065static void pagerUnlockIfUnused(Pager *pPager){
58066 if( sqlite3PcacheRefCount(pCache: pPager->pPCache)==0 ){
58067 assert( pPager->nMmapOut==0 ); /* because page1 is never memory mapped */
58068 pagerUnlockAndRollback(pPager);
58069 }
58070}
58071
58072/*
58073** The page getter methods each try to acquire a reference to a
58074** page with page number pgno. If the requested reference is
58075** successfully obtained, it is copied to *ppPage and SQLITE_OK returned.
58076**
58077** There are different implementations of the getter method depending
58078** on the current state of the pager.
58079**
58080** getPageNormal() -- The normal getter
58081** getPageError() -- Used if the pager is in an error state
58082** getPageMmap() -- Used if memory-mapped I/O is enabled
58083**
58084** If the requested page is already in the cache, it is returned.
58085** Otherwise, a new page object is allocated and populated with data
58086** read from the database file. In some cases, the pcache module may
58087** choose not to allocate a new page object and may reuse an existing
58088** object with no outstanding references.
58089**
58090** The extra data appended to a page is always initialized to zeros the
58091** first time a page is loaded into memory. If the page requested is
58092** already in the cache when this function is called, then the extra
58093** data is left as it was when the page object was last used.
58094**
58095** If the database image is smaller than the requested page or if
58096** the flags parameter contains the PAGER_GET_NOCONTENT bit and the
58097** requested page is not already stored in the cache, then no
58098** actual disk read occurs. In this case the memory image of the
58099** page is initialized to all zeros.
58100**
58101** If PAGER_GET_NOCONTENT is true, it means that we do not care about
58102** the contents of the page. This occurs in two scenarios:
58103**
58104** a) When reading a free-list leaf page from the database, and
58105**
58106** b) When a savepoint is being rolled back and we need to load
58107** a new page into the cache to be filled with the data read
58108** from the savepoint journal.
58109**
58110** If PAGER_GET_NOCONTENT is true, then the data returned is zeroed instead
58111** of being read from the database. Additionally, the bits corresponding
58112** to pgno in Pager.pInJournal (bitvec of pages already written to the
58113** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open
58114** savepoints are set. This means if the page is made writable at any
58115** point in the future, using a call to sqlite3PagerWrite(), its contents
58116** will not be journaled. This saves IO.
58117**
58118** The acquisition might fail for several reasons. In all cases,
58119** an appropriate error code is returned and *ppPage is set to NULL.
58120**
58121** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
58122** to find a page in the in-memory cache first. If the page is not already
58123** in memory, this routine goes to disk to read it in whereas Lookup()
58124** just returns 0. This routine acquires a read-lock the first time it
58125** has to go to disk, and could also playback an old journal if necessary.
58126** Since Lookup() never goes to disk, it never has to deal with locks
58127** or journal files.
58128*/
58129static int getPageNormal(
58130 Pager *pPager, /* The pager open on the database file */
58131 Pgno pgno, /* Page number to fetch */
58132 DbPage **ppPage, /* Write a pointer to the page here */
58133 int flags /* PAGER_GET_XXX flags */
58134){
58135 int rc = SQLITE_OK;
58136 PgHdr *pPg;
58137 u8 noContent; /* True if PAGER_GET_NOCONTENT is set */
58138 sqlite3_pcache_page *pBase;
58139
58140 assert( pPager->errCode==SQLITE_OK );
58141 assert( pPager->eState>=PAGER_READER );
58142 assert( assert_pager_state(pPager) );
58143 assert( pPager->hasHeldSharedLock==1 );
58144
58145 if( pgno==0 ) return SQLITE_CORRUPT_BKPT;
58146 pBase = sqlite3PcacheFetch(pCache: pPager->pPCache, pgno, createFlag: 3);
58147 if( pBase==0 ){
58148 pPg = 0;
58149 rc = sqlite3PcacheFetchStress(pCache: pPager->pPCache, pgno, ppPage: &pBase);
58150 if( rc!=SQLITE_OK ) goto pager_acquire_err;
58151 if( pBase==0 ){
58152 rc = SQLITE_NOMEM_BKPT;
58153 goto pager_acquire_err;
58154 }
58155 }
58156 pPg = *ppPage = sqlite3PcacheFetchFinish(pCache: pPager->pPCache, pgno, pPage: pBase);
58157 assert( pPg==(*ppPage) );
58158 assert( pPg->pgno==pgno );
58159 assert( pPg->pPager==pPager || pPg->pPager==0 );
58160
58161 noContent = (flags & PAGER_GET_NOCONTENT)!=0;
58162 if( pPg->pPager && !noContent ){
58163 /* In this case the pcache already contains an initialized copy of
58164 ** the page. Return without further ado. */
58165 assert( pgno!=PAGER_MJ_PGNO(pPager) );
58166 pPager->aStat[PAGER_STAT_HIT]++;
58167 return SQLITE_OK;
58168
58169 }else{
58170 /* The pager cache has created a new page. Its content needs to
58171 ** be initialized. But first some error checks:
58172 **
58173 ** (*) obsolete. Was: maximum page number is 2^31
58174 ** (2) Never try to fetch the locking page
58175 */
58176 if( pgno==PAGER_MJ_PGNO(pPager) ){
58177 rc = SQLITE_CORRUPT_BKPT;
58178 goto pager_acquire_err;
58179 }
58180
58181 pPg->pPager = pPager;
58182
58183 assert( !isOpen(pPager->fd) || !MEMDB );
58184 if( !isOpen(pPager->fd) || pPager->dbSize<pgno || noContent ){
58185 if( pgno>pPager->mxPgno ){
58186 rc = SQLITE_FULL;
58187 goto pager_acquire_err;
58188 }
58189 if( noContent ){
58190 /* Failure to set the bits in the InJournal bit-vectors is benign.
58191 ** It merely means that we might do some extra work to journal a
58192 ** page that does not need to be journaled. Nevertheless, be sure
58193 ** to test the case where a malloc error occurs while trying to set
58194 ** a bit in a bit vector.
58195 */
58196 sqlite3BeginBenignMalloc();
58197 if( pgno<=pPager->dbOrigSize ){
58198 TESTONLY( rc = ) sqlite3BitvecSet(p: pPager->pInJournal, i: pgno);
58199 testcase( rc==SQLITE_NOMEM );
58200 }
58201 TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno);
58202 testcase( rc==SQLITE_NOMEM );
58203 sqlite3EndBenignMalloc();
58204 }
58205 memset(s: pPg->pData, c: 0, n: pPager->pageSize);
58206 IOTRACE(("ZERO %p %d\n", pPager, pgno));
58207 }else{
58208 assert( pPg->pPager==pPager );
58209 pPager->aStat[PAGER_STAT_MISS]++;
58210 rc = readDbPage(pPg);
58211 if( rc!=SQLITE_OK ){
58212 goto pager_acquire_err;
58213 }
58214 }
58215 pager_set_pagehash(pPg);
58216 }
58217 return SQLITE_OK;
58218
58219pager_acquire_err:
58220 assert( rc!=SQLITE_OK );
58221 if( pPg ){
58222 sqlite3PcacheDrop(p: pPg);
58223 }
58224 pagerUnlockIfUnused(pPager);
58225 *ppPage = 0;
58226 return rc;
58227}
58228
58229#if SQLITE_MAX_MMAP_SIZE>0
58230/* The page getter for when memory-mapped I/O is enabled */
58231static int getPageMMap(
58232 Pager *pPager, /* The pager open on the database file */
58233 Pgno pgno, /* Page number to fetch */
58234 DbPage **ppPage, /* Write a pointer to the page here */
58235 int flags /* PAGER_GET_XXX flags */
58236){
58237 int rc = SQLITE_OK;
58238 PgHdr *pPg = 0;
58239 u32 iFrame = 0; /* Frame to read from WAL file */
58240
58241 /* It is acceptable to use a read-only (mmap) page for any page except
58242 ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
58243 ** flag was specified by the caller. And so long as the db is not a
58244 ** temporary or in-memory database. */
58245 const int bMmapOk = (pgno>1
58246 && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY))
58247 );
58248
58249 assert( USEFETCH(pPager) );
58250
58251 /* Optimization note: Adding the "pgno<=1" term before "pgno==0" here
58252 ** allows the compiler optimizer to reuse the results of the "pgno>1"
58253 ** test in the previous statement, and avoid testing pgno==0 in the
58254 ** common case where pgno is large. */
58255 if( pgno<=1 && pgno==0 ){
58256 return SQLITE_CORRUPT_BKPT;
58257 }
58258 assert( pPager->eState>=PAGER_READER );
58259 assert( assert_pager_state(pPager) );
58260 assert( pPager->hasHeldSharedLock==1 );
58261 assert( pPager->errCode==SQLITE_OK );
58262
58263 if( bMmapOk && pagerUseWal(pPager) ){
58264 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
58265 if( rc!=SQLITE_OK ){
58266 *ppPage = 0;
58267 return rc;
58268 }
58269 }
58270 if( bMmapOk && iFrame==0 ){
58271 void *pData = 0;
58272 rc = sqlite3OsFetch(id: pPager->fd,
58273 iOff: (i64)(pgno-1) * pPager->pageSize, iAmt: pPager->pageSize, pp: &pData
58274 );
58275 if( rc==SQLITE_OK && pData ){
58276 if( pPager->eState>PAGER_READER || pPager->tempFile ){
58277 pPg = sqlite3PagerLookup(pPager, pgno);
58278 }
58279 if( pPg==0 ){
58280 rc = pagerAcquireMapPage(pPager, pgno, pData, ppPage: &pPg);
58281 }else{
58282 sqlite3OsUnfetch(id: pPager->fd, iOff: (i64)(pgno-1)*pPager->pageSize, p: pData);
58283 }
58284 if( pPg ){
58285 assert( rc==SQLITE_OK );
58286 *ppPage = pPg;
58287 return SQLITE_OK;
58288 }
58289 }
58290 if( rc!=SQLITE_OK ){
58291 *ppPage = 0;
58292 return rc;
58293 }
58294 }
58295 return getPageNormal(pPager, pgno, ppPage, flags);
58296}
58297#endif /* SQLITE_MAX_MMAP_SIZE>0 */
58298
58299/* The page getter method for when the pager is an error state */
58300static int getPageError(
58301 Pager *pPager, /* The pager open on the database file */
58302 Pgno pgno, /* Page number to fetch */
58303 DbPage **ppPage, /* Write a pointer to the page here */
58304 int flags /* PAGER_GET_XXX flags */
58305){
58306 UNUSED_PARAMETER(pgno);
58307 UNUSED_PARAMETER(flags);
58308 assert( pPager->errCode!=SQLITE_OK );
58309 *ppPage = 0;
58310 return pPager->errCode;
58311}
58312
58313
58314/* Dispatch all page fetch requests to the appropriate getter method.
58315*/
58316SQLITE_PRIVATE int sqlite3PagerGet(
58317 Pager *pPager, /* The pager open on the database file */
58318 Pgno pgno, /* Page number to fetch */
58319 DbPage **ppPage, /* Write a pointer to the page here */
58320 int flags /* PAGER_GET_XXX flags */
58321){
58322 return pPager->xGet(pPager, pgno, ppPage, flags);
58323}
58324
58325/*
58326** Acquire a page if it is already in the in-memory cache. Do
58327** not read the page from disk. Return a pointer to the page,
58328** or 0 if the page is not in cache.
58329**
58330** See also sqlite3PagerGet(). The difference between this routine
58331** and sqlite3PagerGet() is that _get() will go to the disk and read
58332** in the page if the page is not already in cache. This routine
58333** returns NULL if the page is not in cache or if a disk I/O error
58334** has ever happened.
58335*/
58336SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
58337 sqlite3_pcache_page *pPage;
58338 assert( pPager!=0 );
58339 assert( pgno!=0 );
58340 assert( pPager->pPCache!=0 );
58341 pPage = sqlite3PcacheFetch(pCache: pPager->pPCache, pgno, createFlag: 0);
58342 assert( pPage==0 || pPager->hasHeldSharedLock );
58343 if( pPage==0 ) return 0;
58344 return sqlite3PcacheFetchFinish(pCache: pPager->pPCache, pgno, pPage);
58345}
58346
58347/*
58348** Release a page reference.
58349**
58350** The sqlite3PagerUnref() and sqlite3PagerUnrefNotNull() may only be
58351** used if we know that the page being released is not the last page.
58352** The btree layer always holds page1 open until the end, so these first
58353** to routines can be used to release any page other than BtShared.pPage1.
58354**
58355** Use sqlite3PagerUnrefPageOne() to release page1. This latter routine
58356** checks the total number of outstanding pages and if the number of
58357** pages reaches zero it drops the database lock.
58358*/
58359SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage *pPg){
58360 TESTONLY( Pager *pPager = pPg->pPager; )
58361 assert( pPg!=0 );
58362 if( pPg->flags & PGHDR_MMAP ){
58363 assert( pPg->pgno!=1 ); /* Page1 is never memory mapped */
58364 pagerReleaseMapPage(pPg);
58365 }else{
58366 sqlite3PcacheRelease(p: pPg);
58367 }
58368 /* Do not use this routine to release the last reference to page1 */
58369 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
58370}
58371SQLITE_PRIVATE void sqlite3PagerUnref(DbPage *pPg){
58372 if( pPg ) sqlite3PagerUnrefNotNull(pPg);
58373}
58374SQLITE_PRIVATE void sqlite3PagerUnrefPageOne(DbPage *pPg){
58375 Pager *pPager;
58376 assert( pPg!=0 );
58377 assert( pPg->pgno==1 );
58378 assert( (pPg->flags & PGHDR_MMAP)==0 ); /* Page1 is never memory mapped */
58379 pPager = pPg->pPager;
58380 sqlite3PcacheRelease(p: pPg);
58381 pagerUnlockIfUnused(pPager);
58382}
58383
58384/*
58385** This function is called at the start of every write transaction.
58386** There must already be a RESERVED or EXCLUSIVE lock on the database
58387** file when this routine is called.
58388**
58389** Open the journal file for pager pPager and write a journal header
58390** to the start of it. If there are active savepoints, open the sub-journal
58391** as well. This function is only used when the journal file is being
58392** opened to write a rollback log for a transaction. It is not used
58393** when opening a hot journal file to roll it back.
58394**
58395** If the journal file is already open (as it may be in exclusive mode),
58396** then this function just writes a journal header to the start of the
58397** already open file.
58398**
58399** Whether or not the journal file is opened by this function, the
58400** Pager.pInJournal bitvec structure is allocated.
58401**
58402** Return SQLITE_OK if everything is successful. Otherwise, return
58403** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or
58404** an IO error code if opening or writing the journal file fails.
58405*/
58406static int pager_open_journal(Pager *pPager){
58407 int rc = SQLITE_OK; /* Return code */
58408 sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */
58409
58410 assert( pPager->eState==PAGER_WRITER_LOCKED );
58411 assert( assert_pager_state(pPager) );
58412 assert( pPager->pInJournal==0 );
58413
58414 /* If already in the error state, this function is a no-op. But on
58415 ** the other hand, this routine is never called if we are already in
58416 ** an error state. */
58417 if( NEVER(pPager->errCode) ) return pPager->errCode;
58418
58419 if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
58420 pPager->pInJournal = sqlite3BitvecCreate(iSize: pPager->dbSize);
58421 if( pPager->pInJournal==0 ){
58422 return SQLITE_NOMEM_BKPT;
58423 }
58424
58425 /* Open the journal file if it is not already open. */
58426 if( !isOpen(pPager->jfd) ){
58427 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
58428 sqlite3MemJournalOpen(pPager->jfd);
58429 }else{
58430 int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
58431 int nSpill;
58432
58433 if( pPager->tempFile ){
58434 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
58435 nSpill = sqlite3Config.nStmtSpill;
58436 }else{
58437 flags |= SQLITE_OPEN_MAIN_JOURNAL;
58438 nSpill = jrnlBufferSize(pPager);
58439 }
58440
58441 /* Verify that the database still has the same name as it did when
58442 ** it was originally opened. */
58443 rc = databaseIsUnmoved(pPager);
58444 if( rc==SQLITE_OK ){
58445 rc = sqlite3JournalOpen (
58446 pVfs, pPager->zJournal, pPager->jfd, flags, nSpill
58447 );
58448 }
58449 }
58450 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
58451 }
58452
58453
58454 /* Write the first journal header to the journal file and open
58455 ** the sub-journal if necessary.
58456 */
58457 if( rc==SQLITE_OK ){
58458 /* TODO: Check if all of these are really required. */
58459 pPager->nRec = 0;
58460 pPager->journalOff = 0;
58461 pPager->setSuper = 0;
58462 pPager->journalHdr = 0;
58463 rc = writeJournalHdr(pPager);
58464 }
58465 }
58466
58467 if( rc!=SQLITE_OK ){
58468 sqlite3BitvecDestroy(p: pPager->pInJournal);
58469 pPager->pInJournal = 0;
58470 }else{
58471 assert( pPager->eState==PAGER_WRITER_LOCKED );
58472 pPager->eState = PAGER_WRITER_CACHEMOD;
58473 }
58474
58475 return rc;
58476}
58477
58478/*
58479** Begin a write-transaction on the specified pager object. If a
58480** write-transaction has already been opened, this function is a no-op.
58481**
58482** If the exFlag argument is false, then acquire at least a RESERVED
58483** lock on the database file. If exFlag is true, then acquire at least
58484** an EXCLUSIVE lock. If such a lock is already held, no locking
58485** functions need be called.
58486**
58487** If the subjInMemory argument is non-zero, then any sub-journal opened
58488** within this transaction will be opened as an in-memory file. This
58489** has no effect if the sub-journal is already opened (as it may be when
58490** running in exclusive mode) or if the transaction does not require a
58491** sub-journal. If the subjInMemory argument is zero, then any required
58492** sub-journal is implemented in-memory if pPager is an in-memory database,
58493** or using a temporary file otherwise.
58494*/
58495SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){
58496 int rc = SQLITE_OK;
58497
58498 if( pPager->errCode ) return pPager->errCode;
58499 assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
58500 pPager->subjInMemory = (u8)subjInMemory;
58501
58502 if( pPager->eState==PAGER_READER ){
58503 assert( pPager->pInJournal==0 );
58504
58505 if( pagerUseWal(pPager) ){
58506 /* If the pager is configured to use locking_mode=exclusive, and an
58507 ** exclusive lock on the database is not already held, obtain it now.
58508 */
58509 if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pWal: pPager->pWal, op: -1) ){
58510 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
58511 if( rc!=SQLITE_OK ){
58512 return rc;
58513 }
58514 (void)sqlite3WalExclusiveMode(pWal: pPager->pWal, op: 1);
58515 }
58516
58517 /* Grab the write lock on the log file. If successful, upgrade to
58518 ** PAGER_RESERVED state. Otherwise, return an error code to the caller.
58519 ** The busy-handler is not invoked if another connection already
58520 ** holds the write-lock. If possible, the upper layer will call it.
58521 */
58522 rc = sqlite3WalBeginWriteTransaction(pWal: pPager->pWal);
58523 }else{
58524 /* Obtain a RESERVED lock on the database file. If the exFlag parameter
58525 ** is true, then immediately upgrade this to an EXCLUSIVE lock. The
58526 ** busy-handler callback can be used when upgrading to the EXCLUSIVE
58527 ** lock, but not when obtaining the RESERVED lock.
58528 */
58529 rc = pagerLockDb(pPager, RESERVED_LOCK);
58530 if( rc==SQLITE_OK && exFlag ){
58531 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
58532 }
58533 }
58534
58535 if( rc==SQLITE_OK ){
58536 /* Change to WRITER_LOCKED state.
58537 **
58538 ** WAL mode sets Pager.eState to PAGER_WRITER_LOCKED or CACHEMOD
58539 ** when it has an open transaction, but never to DBMOD or FINISHED.
58540 ** This is because in those states the code to roll back savepoint
58541 ** transactions may copy data from the sub-journal into the database
58542 ** file as well as into the page cache. Which would be incorrect in
58543 ** WAL mode.
58544 */
58545 pPager->eState = PAGER_WRITER_LOCKED;
58546 pPager->dbHintSize = pPager->dbSize;
58547 pPager->dbFileSize = pPager->dbSize;
58548 pPager->dbOrigSize = pPager->dbSize;
58549 pPager->journalOff = 0;
58550 }
58551
58552 assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
58553 assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
58554 assert( assert_pager_state(pPager) );
58555 }
58556
58557 PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
58558 return rc;
58559}
58560
58561/*
58562** Write page pPg onto the end of the rollback journal.
58563*/
58564static SQLITE_NOINLINE int pagerAddPageToRollbackJournal(PgHdr *pPg){
58565 Pager *pPager = pPg->pPager;
58566 int rc;
58567 u32 cksum;
58568 char *pData2;
58569 i64 iOff = pPager->journalOff;
58570
58571 /* We should never write to the journal file the page that
58572 ** contains the database locks. The following assert verifies
58573 ** that we do not. */
58574 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
58575
58576 assert( pPager->journalHdr<=pPager->journalOff );
58577 pData2 = pPg->pData;
58578 cksum = pager_cksum(pPager, aData: (u8*)pData2);
58579
58580 /* Even if an IO or diskfull error occurs while journalling the
58581 ** page in the block above, set the need-sync flag for the page.
58582 ** Otherwise, when the transaction is rolled back, the logic in
58583 ** playback_one_page() will think that the page needs to be restored
58584 ** in the database file. And if an IO error occurs while doing so,
58585 ** then corruption may follow.
58586 */
58587 pPg->flags |= PGHDR_NEED_SYNC;
58588
58589 rc = write32bits(fd: pPager->jfd, offset: iOff, val: pPg->pgno);
58590 if( rc!=SQLITE_OK ) return rc;
58591 rc = sqlite3OsWrite(id: pPager->jfd, pBuf: pData2, amt: pPager->pageSize, offset: iOff+4);
58592 if( rc!=SQLITE_OK ) return rc;
58593 rc = write32bits(fd: pPager->jfd, offset: iOff+pPager->pageSize+4, val: cksum);
58594 if( rc!=SQLITE_OK ) return rc;
58595
58596 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
58597 pPager->journalOff, pPager->pageSize));
58598 PAGER_INCR(sqlite3_pager_writej_count);
58599 PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
58600 PAGERID(pPager), pPg->pgno,
58601 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
58602
58603 pPager->journalOff += 8 + pPager->pageSize;
58604 pPager->nRec++;
58605 assert( pPager->pInJournal!=0 );
58606 rc = sqlite3BitvecSet(p: pPager->pInJournal, i: pPg->pgno);
58607 testcase( rc==SQLITE_NOMEM );
58608 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
58609 rc |= addToSavepointBitvecs(pPager, pgno: pPg->pgno);
58610 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
58611 return rc;
58612}
58613
58614/*
58615** Mark a single data page as writeable. The page is written into the
58616** main journal or sub-journal as required. If the page is written into
58617** one of the journals, the corresponding bit is set in the
58618** Pager.pInJournal bitvec and the PagerSavepoint.pInSavepoint bitvecs
58619** of any open savepoints as appropriate.
58620*/
58621static int pager_write(PgHdr *pPg){
58622 Pager *pPager = pPg->pPager;
58623 int rc = SQLITE_OK;
58624
58625 /* This routine is not called unless a write-transaction has already
58626 ** been started. The journal file may or may not be open at this point.
58627 ** It is never called in the ERROR state.
58628 */
58629 assert( pPager->eState==PAGER_WRITER_LOCKED
58630 || pPager->eState==PAGER_WRITER_CACHEMOD
58631 || pPager->eState==PAGER_WRITER_DBMOD
58632 );
58633 assert( assert_pager_state(pPager) );
58634 assert( pPager->errCode==0 );
58635 assert( pPager->readOnly==0 );
58636 CHECK_PAGE(pPg);
58637
58638 /* The journal file needs to be opened. Higher level routines have already
58639 ** obtained the necessary locks to begin the write-transaction, but the
58640 ** rollback journal might not yet be open. Open it now if this is the case.
58641 **
58642 ** This is done before calling sqlite3PcacheMakeDirty() on the page.
58643 ** Otherwise, if it were done after calling sqlite3PcacheMakeDirty(), then
58644 ** an error might occur and the pager would end up in WRITER_LOCKED state
58645 ** with pages marked as dirty in the cache.
58646 */
58647 if( pPager->eState==PAGER_WRITER_LOCKED ){
58648 rc = pager_open_journal(pPager);
58649 if( rc!=SQLITE_OK ) return rc;
58650 }
58651 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
58652 assert( assert_pager_state(pPager) );
58653
58654 /* Mark the page that is about to be modified as dirty. */
58655 sqlite3PcacheMakeDirty(p: pPg);
58656
58657 /* If a rollback journal is in use, them make sure the page that is about
58658 ** to change is in the rollback journal, or if the page is a new page off
58659 ** then end of the file, make sure it is marked as PGHDR_NEED_SYNC.
58660 */
58661 assert( (pPager->pInJournal!=0) == isOpen(pPager->jfd) );
58662 if( pPager->pInJournal!=0
58663 && sqlite3BitvecTestNotNull(p: pPager->pInJournal, i: pPg->pgno)==0
58664 ){
58665 assert( pagerUseWal(pPager)==0 );
58666 if( pPg->pgno<=pPager->dbOrigSize ){
58667 rc = pagerAddPageToRollbackJournal(pPg);
58668 if( rc!=SQLITE_OK ){
58669 return rc;
58670 }
58671 }else{
58672 if( pPager->eState!=PAGER_WRITER_DBMOD ){
58673 pPg->flags |= PGHDR_NEED_SYNC;
58674 }
58675 PAGERTRACE(("APPEND %d page %d needSync=%d\n",
58676 PAGERID(pPager), pPg->pgno,
58677 ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
58678 }
58679 }
58680
58681 /* The PGHDR_DIRTY bit is set above when the page was added to the dirty-list
58682 ** and before writing the page into the rollback journal. Wait until now,
58683 ** after the page has been successfully journalled, before setting the
58684 ** PGHDR_WRITEABLE bit that indicates that the page can be safely modified.
58685 */
58686 pPg->flags |= PGHDR_WRITEABLE;
58687
58688 /* If the statement journal is open and the page is not in it,
58689 ** then write the page into the statement journal.
58690 */
58691 if( pPager->nSavepoint>0 ){
58692 rc = subjournalPageIfRequired(pPg);
58693 }
58694
58695 /* Update the database size and return. */
58696 if( pPager->dbSize<pPg->pgno ){
58697 pPager->dbSize = pPg->pgno;
58698 }
58699 return rc;
58700}
58701
58702/*
58703** This is a variant of sqlite3PagerWrite() that runs when the sector size
58704** is larger than the page size. SQLite makes the (reasonable) assumption that
58705** all bytes of a sector are written together by hardware. Hence, all bytes of
58706** a sector need to be journalled in case of a power loss in the middle of
58707** a write.
58708**
58709** Usually, the sector size is less than or equal to the page size, in which
58710** case pages can be individually written. This routine only runs in the
58711** exceptional case where the page size is smaller than the sector size.
58712*/
58713static SQLITE_NOINLINE int pagerWriteLargeSector(PgHdr *pPg){
58714 int rc = SQLITE_OK; /* Return code */
58715 Pgno nPageCount; /* Total number of pages in database file */
58716 Pgno pg1; /* First page of the sector pPg is located on. */
58717 int nPage = 0; /* Number of pages starting at pg1 to journal */
58718 int ii; /* Loop counter */
58719 int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */
58720 Pager *pPager = pPg->pPager; /* The pager that owns pPg */
58721 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
58722
58723 /* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow
58724 ** a journal header to be written between the pages journaled by
58725 ** this function.
58726 */
58727 assert( !MEMDB );
58728 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 );
58729 pPager->doNotSpill |= SPILLFLAG_NOSYNC;
58730
58731 /* This trick assumes that both the page-size and sector-size are
58732 ** an integer power of 2. It sets variable pg1 to the identifier
58733 ** of the first page of the sector pPg is located on.
58734 */
58735 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
58736
58737 nPageCount = pPager->dbSize;
58738 if( pPg->pgno>nPageCount ){
58739 nPage = (pPg->pgno - pg1)+1;
58740 }else if( (pg1+nPagePerSector-1)>nPageCount ){
58741 nPage = nPageCount+1-pg1;
58742 }else{
58743 nPage = nPagePerSector;
58744 }
58745 assert(nPage>0);
58746 assert(pg1<=pPg->pgno);
58747 assert((pg1+nPage)>pPg->pgno);
58748
58749 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
58750 Pgno pg = pg1+ii;
58751 PgHdr *pPage;
58752 if( pg==pPg->pgno || !sqlite3BitvecTest(p: pPager->pInJournal, i: pg) ){
58753 if( pg!=PAGER_MJ_PGNO(pPager) ){
58754 rc = sqlite3PagerGet(pPager, pgno: pg, ppPage: &pPage, flags: 0);
58755 if( rc==SQLITE_OK ){
58756 rc = pager_write(pPg: pPage);
58757 if( pPage->flags&PGHDR_NEED_SYNC ){
58758 needSync = 1;
58759 }
58760 sqlite3PagerUnrefNotNull(pPg: pPage);
58761 }
58762 }
58763 }else if( (pPage = sqlite3PagerLookup(pPager, pgno: pg))!=0 ){
58764 if( pPage->flags&PGHDR_NEED_SYNC ){
58765 needSync = 1;
58766 }
58767 sqlite3PagerUnrefNotNull(pPg: pPage);
58768 }
58769 }
58770
58771 /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages
58772 ** starting at pg1, then it needs to be set for all of them. Because
58773 ** writing to any of these nPage pages may damage the others, the
58774 ** journal file must contain sync()ed copies of all of them
58775 ** before any of them can be written out to the database file.
58776 */
58777 if( rc==SQLITE_OK && needSync ){
58778 assert( !MEMDB );
58779 for(ii=0; ii<nPage; ii++){
58780 PgHdr *pPage = sqlite3PagerLookup(pPager, pgno: pg1+ii);
58781 if( pPage ){
58782 pPage->flags |= PGHDR_NEED_SYNC;
58783 sqlite3PagerUnrefNotNull(pPg: pPage);
58784 }
58785 }
58786 }
58787
58788 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 );
58789 pPager->doNotSpill &= ~SPILLFLAG_NOSYNC;
58790 return rc;
58791}
58792
58793/*
58794** Mark a data page as writeable. This routine must be called before
58795** making changes to a page. The caller must check the return value
58796** of this function and be careful not to change any page data unless
58797** this routine returns SQLITE_OK.
58798**
58799** The difference between this function and pager_write() is that this
58800** function also deals with the special case where 2 or more pages
58801** fit on a single disk sector. In this case all co-resident pages
58802** must have been written to the journal file before returning.
58803**
58804** If an error occurs, SQLITE_NOMEM or an IO error code is returned
58805** as appropriate. Otherwise, SQLITE_OK.
58806*/
58807SQLITE_PRIVATE int sqlite3PagerWrite(PgHdr *pPg){
58808 Pager *pPager = pPg->pPager;
58809 assert( (pPg->flags & PGHDR_MMAP)==0 );
58810 assert( pPager->eState>=PAGER_WRITER_LOCKED );
58811 assert( assert_pager_state(pPager) );
58812 if( (pPg->flags & PGHDR_WRITEABLE)!=0 && pPager->dbSize>=pPg->pgno ){
58813 if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg);
58814 return SQLITE_OK;
58815 }else if( pPager->errCode ){
58816 return pPager->errCode;
58817 }else if( pPager->sectorSize > (u32)pPager->pageSize ){
58818 assert( pPager->tempFile==0 );
58819 return pagerWriteLargeSector(pPg);
58820 }else{
58821 return pager_write(pPg);
58822 }
58823}
58824
58825/*
58826** Return TRUE if the page given in the argument was previously passed
58827** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
58828** to change the content of the page.
58829*/
58830#ifndef NDEBUG
58831SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage *pPg){
58832 return pPg->flags & PGHDR_WRITEABLE;
58833}
58834#endif
58835
58836/*
58837** A call to this routine tells the pager that it is not necessary to
58838** write the information on page pPg back to the disk, even though
58839** that page might be marked as dirty. This happens, for example, when
58840** the page has been added as a leaf of the freelist and so its
58841** content no longer matters.
58842**
58843** The overlying software layer calls this routine when all of the data
58844** on the given page is unused. The pager marks the page as clean so
58845** that it does not get written to disk.
58846**
58847** Tests show that this optimization can quadruple the speed of large
58848** DELETE operations.
58849**
58850** This optimization cannot be used with a temp-file, as the page may
58851** have been dirty at the start of the transaction. In that case, if
58852** memory pressure forces page pPg out of the cache, the data does need
58853** to be written out to disk so that it may be read back in if the
58854** current transaction is rolled back.
58855*/
58856SQLITE_PRIVATE void sqlite3PagerDontWrite(PgHdr *pPg){
58857 Pager *pPager = pPg->pPager;
58858 if( !pPager->tempFile && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
58859 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
58860 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
58861 pPg->flags |= PGHDR_DONT_WRITE;
58862 pPg->flags &= ~PGHDR_WRITEABLE;
58863 testcase( pPg->flags & PGHDR_NEED_SYNC );
58864 pager_set_pagehash(pPg);
58865 }
58866}
58867
58868/*
58869** This routine is called to increment the value of the database file
58870** change-counter, stored as a 4-byte big-endian integer starting at
58871** byte offset 24 of the pager file. The secondary change counter at
58872** 92 is also updated, as is the SQLite version number at offset 96.
58873**
58874** But this only happens if the pPager->changeCountDone flag is false.
58875** To avoid excess churning of page 1, the update only happens once.
58876** See also the pager_write_changecounter() routine that does an
58877** unconditional update of the change counters.
58878**
58879** If the isDirectMode flag is zero, then this is done by calling
58880** sqlite3PagerWrite() on page 1, then modifying the contents of the
58881** page data. In this case the file will be updated when the current
58882** transaction is committed.
58883**
58884** The isDirectMode flag may only be non-zero if the library was compiled
58885** with the SQLITE_ENABLE_ATOMIC_WRITE macro defined. In this case,
58886** if isDirect is non-zero, then the database file is updated directly
58887** by writing an updated version of page 1 using a call to the
58888** sqlite3OsWrite() function.
58889*/
58890static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
58891 int rc = SQLITE_OK;
58892
58893 assert( pPager->eState==PAGER_WRITER_CACHEMOD
58894 || pPager->eState==PAGER_WRITER_DBMOD
58895 );
58896 assert( assert_pager_state(pPager) );
58897
58898 /* Declare and initialize constant integer 'isDirect'. If the
58899 ** atomic-write optimization is enabled in this build, then isDirect
58900 ** is initialized to the value passed as the isDirectMode parameter
58901 ** to this function. Otherwise, it is always set to zero.
58902 **
58903 ** The idea is that if the atomic-write optimization is not
58904 ** enabled at compile time, the compiler can omit the tests of
58905 ** 'isDirect' below, as well as the block enclosed in the
58906 ** "if( isDirect )" condition.
58907 */
58908#ifndef SQLITE_ENABLE_ATOMIC_WRITE
58909# define DIRECT_MODE 0
58910 assert( isDirectMode==0 );
58911 UNUSED_PARAMETER(isDirectMode);
58912#else
58913# define DIRECT_MODE isDirectMode
58914#endif
58915
58916 if( !pPager->changeCountDone && ALWAYS(pPager->dbSize>0) ){
58917 PgHdr *pPgHdr; /* Reference to page 1 */
58918
58919 assert( !pPager->tempFile && isOpen(pPager->fd) );
58920
58921 /* Open page 1 of the file for writing. */
58922 rc = sqlite3PagerGet(pPager, pgno: 1, ppPage: &pPgHdr, flags: 0);
58923 assert( pPgHdr==0 || rc==SQLITE_OK );
58924
58925 /* If page one was fetched successfully, and this function is not
58926 ** operating in direct-mode, make page 1 writable. When not in
58927 ** direct mode, page 1 is always held in cache and hence the PagerGet()
58928 ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
58929 */
58930 if( !DIRECT_MODE && ALWAYS(rc==SQLITE_OK) ){
58931 rc = sqlite3PagerWrite(pPg: pPgHdr);
58932 }
58933
58934 if( rc==SQLITE_OK ){
58935 /* Actually do the update of the change counter */
58936 pager_write_changecounter(pPg: pPgHdr);
58937
58938 /* If running in direct mode, write the contents of page 1 to the file. */
58939 if( DIRECT_MODE ){
58940 const void *zBuf;
58941 assert( pPager->dbFileSize>0 );
58942 zBuf = pPgHdr->pData;
58943 if( rc==SQLITE_OK ){
58944 rc = sqlite3OsWrite(id: pPager->fd, pBuf: zBuf, amt: pPager->pageSize, offset: 0);
58945 pPager->aStat[PAGER_STAT_WRITE]++;
58946 }
58947 if( rc==SQLITE_OK ){
58948 /* Update the pager's copy of the change-counter. Otherwise, the
58949 ** next time a read transaction is opened the cache will be
58950 ** flushed (as the change-counter values will not match). */
58951 const void *pCopy = (const void *)&((const char *)zBuf)[24];
58952 memcpy(dest: &pPager->dbFileVers, src: pCopy, n: sizeof(pPager->dbFileVers));
58953 pPager->changeCountDone = 1;
58954 }
58955 }else{
58956 pPager->changeCountDone = 1;
58957 }
58958 }
58959
58960 /* Release the page reference. */
58961 sqlite3PagerUnref(pPg: pPgHdr);
58962 }
58963 return rc;
58964}
58965
58966/*
58967** Sync the database file to disk. This is a no-op for in-memory databases
58968** or pages with the Pager.noSync flag set.
58969**
58970** If successful, or if called on a pager for which it is a no-op, this
58971** function returns SQLITE_OK. Otherwise, an IO error code is returned.
58972*/
58973SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zSuper){
58974 int rc = SQLITE_OK;
58975 void *pArg = (void*)zSuper;
58976 rc = sqlite3OsFileControl(id: pPager->fd, SQLITE_FCNTL_SYNC, pArg);
58977 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
58978 if( rc==SQLITE_OK && !pPager->noSync ){
58979 assert( !MEMDB );
58980 rc = sqlite3OsSync(id: pPager->fd, flags: pPager->syncFlags);
58981 }
58982 return rc;
58983}
58984
58985/*
58986** This function may only be called while a write-transaction is active in
58987** rollback. If the connection is in WAL mode, this call is a no-op.
58988** Otherwise, if the connection does not already have an EXCLUSIVE lock on
58989** the database file, an attempt is made to obtain one.
58990**
58991** If the EXCLUSIVE lock is already held or the attempt to obtain it is
58992** successful, or the connection is in WAL mode, SQLITE_OK is returned.
58993** Otherwise, either SQLITE_BUSY or an SQLITE_IOERR_XXX error code is
58994** returned.
58995*/
58996SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager *pPager){
58997 int rc = pPager->errCode;
58998 assert( assert_pager_state(pPager) );
58999 if( rc==SQLITE_OK ){
59000 assert( pPager->eState==PAGER_WRITER_CACHEMOD
59001 || pPager->eState==PAGER_WRITER_DBMOD
59002 || pPager->eState==PAGER_WRITER_LOCKED
59003 );
59004 assert( assert_pager_state(pPager) );
59005 if( 0==pagerUseWal(pPager) ){
59006 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
59007 }
59008 }
59009 return rc;
59010}
59011
59012/*
59013** Sync the database file for the pager pPager. zSuper points to the name
59014** of a super-journal file that should be written into the individual
59015** journal file. zSuper may be NULL, which is interpreted as no
59016** super-journal (a single database transaction).
59017**
59018** This routine ensures that:
59019**
59020** * The database file change-counter is updated,
59021** * the journal is synced (unless the atomic-write optimization is used),
59022** * all dirty pages are written to the database file,
59023** * the database file is truncated (if required), and
59024** * the database file synced.
59025**
59026** The only thing that remains to commit the transaction is to finalize
59027** (delete, truncate or zero the first part of) the journal file (or
59028** delete the super-journal file if specified).
59029**
59030** Note that if zSuper==NULL, this does not overwrite a previous value
59031** passed to an sqlite3PagerCommitPhaseOne() call.
59032**
59033** If the final parameter - noSync - is true, then the database file itself
59034** is not synced. The caller must call sqlite3PagerSync() directly to
59035** sync the database file before calling CommitPhaseTwo() to delete the
59036** journal file in this case.
59037*/
59038SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(
59039 Pager *pPager, /* Pager object */
59040 const char *zSuper, /* If not NULL, the super-journal name */
59041 int noSync /* True to omit the xSync on the db file */
59042){
59043 int rc = SQLITE_OK; /* Return code */
59044
59045 assert( pPager->eState==PAGER_WRITER_LOCKED
59046 || pPager->eState==PAGER_WRITER_CACHEMOD
59047 || pPager->eState==PAGER_WRITER_DBMOD
59048 || pPager->eState==PAGER_ERROR
59049 );
59050 assert( assert_pager_state(pPager) );
59051
59052 /* If a prior error occurred, report that error again. */
59053 if( NEVER(pPager->errCode) ) return pPager->errCode;
59054
59055 /* Provide the ability to easily simulate an I/O error during testing */
59056 if( sqlite3FaultSim(iTest: 400) ) return SQLITE_IOERR;
59057
59058 PAGERTRACE(("DATABASE SYNC: File=%s zSuper=%s nSize=%d\n",
59059 pPager->zFilename, zSuper, pPager->dbSize));
59060
59061 /* If no database changes have been made, return early. */
59062 if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
59063
59064 assert( MEMDB==0 || pPager->tempFile );
59065 assert( isOpen(pPager->fd) || pPager->tempFile );
59066 if( 0==pagerFlushOnCommit(pPager, bCommit: 1) ){
59067 /* If this is an in-memory db, or no pages have been written to, or this
59068 ** function has already been called, it is mostly a no-op. However, any
59069 ** backup in progress needs to be restarted. */
59070 sqlite3BackupRestart(pPager->pBackup);
59071 }else{
59072 PgHdr *pList;
59073 if( pagerUseWal(pPager) ){
59074 PgHdr *pPageOne = 0;
59075 pList = sqlite3PcacheDirtyList(pCache: pPager->pPCache);
59076 if( pList==0 ){
59077 /* Must have at least one page for the WAL commit flag.
59078 ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
59079 rc = sqlite3PagerGet(pPager, pgno: 1, ppPage: &pPageOne, flags: 0);
59080 pList = pPageOne;
59081 pList->pDirty = 0;
59082 }
59083 assert( rc==SQLITE_OK );
59084 if( ALWAYS(pList) ){
59085 rc = pagerWalFrames(pPager, pList, nTruncate: pPager->dbSize, isCommit: 1);
59086 }
59087 sqlite3PagerUnref(pPg: pPageOne);
59088 if( rc==SQLITE_OK ){
59089 sqlite3PcacheCleanAll(pCache: pPager->pPCache);
59090 }
59091 }else{
59092 /* The bBatch boolean is true if the batch-atomic-write commit method
59093 ** should be used. No rollback journal is created if batch-atomic-write
59094 ** is enabled.
59095 */
59096#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
59097 sqlite3_file *fd = pPager->fd;
59098 int bBatch = zSuper==0 /* An SQLITE_IOCAP_BATCH_ATOMIC commit */
59099 && (sqlite3OsDeviceCharacteristics(fd) & SQLITE_IOCAP_BATCH_ATOMIC)
59100 && !pPager->noSync
59101 && sqlite3JournalIsInMemory(pPager->jfd);
59102#else
59103# define bBatch 0
59104#endif
59105
59106#ifdef SQLITE_ENABLE_ATOMIC_WRITE
59107 /* The following block updates the change-counter. Exactly how it
59108 ** does this depends on whether or not the atomic-update optimization
59109 ** was enabled at compile time, and if this transaction meets the
59110 ** runtime criteria to use the operation:
59111 **
59112 ** * The file-system supports the atomic-write property for
59113 ** blocks of size page-size, and
59114 ** * This commit is not part of a multi-file transaction, and
59115 ** * Exactly one page has been modified and store in the journal file.
59116 **
59117 ** If the optimization was not enabled at compile time, then the
59118 ** pager_incr_changecounter() function is called to update the change
59119 ** counter in 'indirect-mode'. If the optimization is compiled in but
59120 ** is not applicable to this transaction, call sqlite3JournalCreate()
59121 ** to make sure the journal file has actually been created, then call
59122 ** pager_incr_changecounter() to update the change-counter in indirect
59123 ** mode.
59124 **
59125 ** Otherwise, if the optimization is both enabled and applicable,
59126 ** then call pager_incr_changecounter() to update the change-counter
59127 ** in 'direct' mode. In this case the journal file will never be
59128 ** created for this transaction.
59129 */
59130 if( bBatch==0 ){
59131 PgHdr *pPg;
59132 assert( isOpen(pPager->jfd)
59133 || pPager->journalMode==PAGER_JOURNALMODE_OFF
59134 || pPager->journalMode==PAGER_JOURNALMODE_WAL
59135 );
59136 if( !zSuper && isOpen(pPager->jfd)
59137 && pPager->journalOff==jrnlBufferSize(pPager)
59138 && pPager->dbSize>=pPager->dbOrigSize
59139 && (!(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
59140 ){
59141 /* Update the db file change counter via the direct-write method. The
59142 ** following call will modify the in-memory representation of page 1
59143 ** to include the updated change counter and then write page 1
59144 ** directly to the database file. Because of the atomic-write
59145 ** property of the host file-system, this is safe.
59146 */
59147 rc = pager_incr_changecounter(pPager, 1);
59148 }else{
59149 rc = sqlite3JournalCreate(pPager->jfd);
59150 if( rc==SQLITE_OK ){
59151 rc = pager_incr_changecounter(pPager, 0);
59152 }
59153 }
59154 }
59155#else /* SQLITE_ENABLE_ATOMIC_WRITE */
59156#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
59157 if( zSuper ){
59158 rc = sqlite3JournalCreate(pPager->jfd);
59159 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
59160 assert( bBatch==0 );
59161 }
59162#endif
59163 rc = pager_incr_changecounter(pPager, isDirectMode: 0);
59164#endif /* !SQLITE_ENABLE_ATOMIC_WRITE */
59165 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
59166
59167 /* Write the super-journal name into the journal file. If a
59168 ** super-journal file name has already been written to the journal file,
59169 ** or if zSuper is NULL (no super-journal), then this call is a no-op.
59170 */
59171 rc = writeSuperJournal(pPager, zSuper);
59172 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
59173
59174 /* Sync the journal file and write all dirty pages to the database.
59175 ** If the atomic-update optimization is being used, this sync will not
59176 ** create the journal file or perform any real IO.
59177 **
59178 ** Because the change-counter page was just modified, unless the
59179 ** atomic-update optimization is used it is almost certain that the
59180 ** journal requires a sync here. However, in locking_mode=exclusive
59181 ** on a system under memory pressure it is just possible that this is
59182 ** not the case. In this case it is likely enough that the redundant
59183 ** xSync() call will be changed to a no-op by the OS anyhow.
59184 */
59185 rc = syncJournal(pPager, newHdr: 0);
59186 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
59187
59188 pList = sqlite3PcacheDirtyList(pCache: pPager->pPCache);
59189#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
59190 if( bBatch ){
59191 rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_BEGIN_ATOMIC_WRITE, 0);
59192 if( rc==SQLITE_OK ){
59193 rc = pager_write_pagelist(pPager, pList);
59194 if( rc==SQLITE_OK ){
59195 rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_COMMIT_ATOMIC_WRITE, 0);
59196 }
59197 if( rc!=SQLITE_OK ){
59198 sqlite3OsFileControlHint(fd, SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE, 0);
59199 }
59200 }
59201
59202 if( (rc&0xFF)==SQLITE_IOERR && rc!=SQLITE_IOERR_NOMEM ){
59203 rc = sqlite3JournalCreate(pPager->jfd);
59204 if( rc!=SQLITE_OK ){
59205 sqlite3OsClose(pPager->jfd);
59206 goto commit_phase_one_exit;
59207 }
59208 bBatch = 0;
59209 }else{
59210 sqlite3OsClose(pPager->jfd);
59211 }
59212 }
59213#endif /* SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
59214
59215 if( bBatch==0 ){
59216 rc = pager_write_pagelist(pPager, pList);
59217 }
59218 if( rc!=SQLITE_OK ){
59219 assert( rc!=SQLITE_IOERR_BLOCKED );
59220 goto commit_phase_one_exit;
59221 }
59222 sqlite3PcacheCleanAll(pCache: pPager->pPCache);
59223
59224 /* If the file on disk is smaller than the database image, use
59225 ** pager_truncate to grow the file here. This can happen if the database
59226 ** image was extended as part of the current transaction and then the
59227 ** last page in the db image moved to the free-list. In this case the
59228 ** last page is never written out to disk, leaving the database file
59229 ** undersized. Fix this now if it is the case. */
59230 if( pPager->dbSize>pPager->dbFileSize ){
59231 Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
59232 assert( pPager->eState==PAGER_WRITER_DBMOD );
59233 rc = pager_truncate(pPager, nPage: nNew);
59234 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
59235 }
59236
59237 /* Finally, sync the database file. */
59238 if( !noSync ){
59239 rc = sqlite3PagerSync(pPager, zSuper);
59240 }
59241 IOTRACE(("DBSYNC %p\n", pPager))
59242 }
59243 }
59244
59245commit_phase_one_exit:
59246 if( rc==SQLITE_OK && !pagerUseWal(pPager) ){
59247 pPager->eState = PAGER_WRITER_FINISHED;
59248 }
59249 return rc;
59250}
59251
59252
59253/*
59254** When this function is called, the database file has been completely
59255** updated to reflect the changes made by the current transaction and
59256** synced to disk. The journal file still exists in the file-system
59257** though, and if a failure occurs at this point it will eventually
59258** be used as a hot-journal and the current transaction rolled back.
59259**
59260** This function finalizes the journal file, either by deleting,
59261** truncating or partially zeroing it, so that it cannot be used
59262** for hot-journal rollback. Once this is done the transaction is
59263** irrevocably committed.
59264**
59265** If an error occurs, an IO error code is returned and the pager
59266** moves into the error state. Otherwise, SQLITE_OK is returned.
59267*/
59268SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager *pPager){
59269 int rc = SQLITE_OK; /* Return code */
59270
59271 /* This routine should not be called if a prior error has occurred.
59272 ** But if (due to a coding error elsewhere in the system) it does get
59273 ** called, just return the same error code without doing anything. */
59274 if( NEVER(pPager->errCode) ) return pPager->errCode;
59275 pPager->iDataVersion++;
59276
59277 assert( pPager->eState==PAGER_WRITER_LOCKED
59278 || pPager->eState==PAGER_WRITER_FINISHED
59279 || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
59280 );
59281 assert( assert_pager_state(pPager) );
59282
59283 /* An optimization. If the database was not actually modified during
59284 ** this transaction, the pager is running in exclusive-mode and is
59285 ** using persistent journals, then this function is a no-op.
59286 **
59287 ** The start of the journal file currently contains a single journal
59288 ** header with the nRec field set to 0. If such a journal is used as
59289 ** a hot-journal during hot-journal rollback, 0 changes will be made
59290 ** to the database file. So there is no need to zero the journal
59291 ** header. Since the pager is in exclusive mode, there is no need
59292 ** to drop any locks either.
59293 */
59294 if( pPager->eState==PAGER_WRITER_LOCKED
59295 && pPager->exclusiveMode
59296 && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
59297 ){
59298 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
59299 pPager->eState = PAGER_READER;
59300 return SQLITE_OK;
59301 }
59302
59303 PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
59304 rc = pager_end_transaction(pPager, hasSuper: pPager->setSuper, bCommit: 1);
59305 return pager_error(pPager, rc);
59306}
59307
59308/*
59309** If a write transaction is open, then all changes made within the
59310** transaction are reverted and the current write-transaction is closed.
59311** The pager falls back to PAGER_READER state if successful, or PAGER_ERROR
59312** state if an error occurs.
59313**
59314** If the pager is already in PAGER_ERROR state when this function is called,
59315** it returns Pager.errCode immediately. No work is performed in this case.
59316**
59317** Otherwise, in rollback mode, this function performs two functions:
59318**
59319** 1) It rolls back the journal file, restoring all database file and
59320** in-memory cache pages to the state they were in when the transaction
59321** was opened, and
59322**
59323** 2) It finalizes the journal file, so that it is not used for hot
59324** rollback at any point in the future.
59325**
59326** Finalization of the journal file (task 2) is only performed if the
59327** rollback is successful.
59328**
59329** In WAL mode, all cache-entries containing data modified within the
59330** current transaction are either expelled from the cache or reverted to
59331** their pre-transaction state by re-reading data from the database or
59332** WAL files. The WAL transaction is then closed.
59333*/
59334SQLITE_PRIVATE int sqlite3PagerRollback(Pager *pPager){
59335 int rc = SQLITE_OK; /* Return code */
59336 PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
59337
59338 /* PagerRollback() is a no-op if called in READER or OPEN state. If
59339 ** the pager is already in the ERROR state, the rollback is not
59340 ** attempted here. Instead, the error code is returned to the caller.
59341 */
59342 assert( assert_pager_state(pPager) );
59343 if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
59344 if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
59345
59346 if( pagerUseWal(pPager) ){
59347 int rc2;
59348 rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, iSavepoint: -1);
59349 rc2 = pager_end_transaction(pPager, hasSuper: pPager->setSuper, bCommit: 0);
59350 if( rc==SQLITE_OK ) rc = rc2;
59351 }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
59352 int eState = pPager->eState;
59353 rc = pager_end_transaction(pPager, hasSuper: 0, bCommit: 0);
59354 if( !MEMDB && eState>PAGER_WRITER_LOCKED ){
59355 /* This can happen using journal_mode=off. Move the pager to the error
59356 ** state to indicate that the contents of the cache may not be trusted.
59357 ** Any active readers will get SQLITE_ABORT.
59358 */
59359 pPager->errCode = SQLITE_ABORT;
59360 pPager->eState = PAGER_ERROR;
59361 setGetterMethod(pPager);
59362 return rc;
59363 }
59364 }else{
59365 rc = pager_playback(pPager, isHot: 0);
59366 }
59367
59368 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
59369 assert( rc==SQLITE_OK || rc==SQLITE_FULL || rc==SQLITE_CORRUPT
59370 || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR
59371 || rc==SQLITE_CANTOPEN
59372 );
59373
59374 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
59375 ** cache. So call pager_error() on the way out to make any error persistent.
59376 */
59377 return pager_error(pPager, rc);
59378}
59379
59380/*
59381** Return TRUE if the database file is opened read-only. Return FALSE
59382** if the database is (in theory) writable.
59383*/
59384SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager *pPager){
59385 return pPager->readOnly;
59386}
59387
59388#ifdef SQLITE_DEBUG
59389/*
59390** Return the sum of the reference counts for all pages held by pPager.
59391*/
59392SQLITE_PRIVATE int sqlite3PagerRefcount(Pager *pPager){
59393 return sqlite3PcacheRefCount(pPager->pPCache);
59394}
59395#endif
59396
59397/*
59398** Return the approximate number of bytes of memory currently
59399** used by the pager and its associated cache.
59400*/
59401SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager *pPager){
59402 int perPageSize = pPager->pageSize + pPager->nExtra
59403 + (int)(sizeof(PgHdr) + 5*sizeof(void*));
59404 return perPageSize*sqlite3PcachePagecount(pCache: pPager->pPCache)
59405 + sqlite3MallocSize(p: pPager)
59406 + pPager->pageSize;
59407}
59408
59409/*
59410** Return the number of references to the specified page.
59411*/
59412SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage *pPage){
59413 return sqlite3PcachePageRefcount(p: pPage);
59414}
59415
59416#ifdef SQLITE_TEST
59417/*
59418** This routine is used for testing and analysis only.
59419*/
59420SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){
59421 static int a[11];
59422 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
59423 a[1] = sqlite3PcachePagecount(pPager->pPCache);
59424 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
59425 a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
59426 a[4] = pPager->eState;
59427 a[5] = pPager->errCode;
59428 a[6] = pPager->aStat[PAGER_STAT_HIT];
59429 a[7] = pPager->aStat[PAGER_STAT_MISS];
59430 a[8] = 0; /* Used to be pPager->nOvfl */
59431 a[9] = pPager->nRead;
59432 a[10] = pPager->aStat[PAGER_STAT_WRITE];
59433 return a;
59434}
59435#endif
59436
59437/*
59438** Parameter eStat must be one of SQLITE_DBSTATUS_CACHE_HIT, _MISS, _WRITE,
59439** or _WRITE+1. The SQLITE_DBSTATUS_CACHE_WRITE+1 case is a translation
59440** of SQLITE_DBSTATUS_CACHE_SPILL. The _SPILL case is not contiguous because
59441** it was added later.
59442**
59443** Before returning, *pnVal is incremented by the
59444** current cache hit or miss count, according to the value of eStat. If the
59445** reset parameter is non-zero, the cache hit or miss count is zeroed before
59446** returning.
59447*/
59448SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
59449
59450 assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
59451 || eStat==SQLITE_DBSTATUS_CACHE_MISS
59452 || eStat==SQLITE_DBSTATUS_CACHE_WRITE
59453 || eStat==SQLITE_DBSTATUS_CACHE_WRITE+1
59454 );
59455
59456 assert( SQLITE_DBSTATUS_CACHE_HIT+1==SQLITE_DBSTATUS_CACHE_MISS );
59457 assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE );
59458 assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1
59459 && PAGER_STAT_WRITE==2 && PAGER_STAT_SPILL==3 );
59460
59461 eStat -= SQLITE_DBSTATUS_CACHE_HIT;
59462 *pnVal += pPager->aStat[eStat];
59463 if( reset ){
59464 pPager->aStat[eStat] = 0;
59465 }
59466}
59467
59468/*
59469** Return true if this is an in-memory or temp-file backed pager.
59470*/
59471SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
59472 return pPager->tempFile || pPager->memVfs;
59473}
59474
59475/*
59476** Check that there are at least nSavepoint savepoints open. If there are
59477** currently less than nSavepoints open, then open one or more savepoints
59478** to make up the difference. If the number of savepoints is already
59479** equal to nSavepoint, then this function is a no-op.
59480**
59481** If a memory allocation fails, SQLITE_NOMEM is returned. If an error
59482** occurs while opening the sub-journal file, then an IO error code is
59483** returned. Otherwise, SQLITE_OK.
59484*/
59485static SQLITE_NOINLINE int pagerOpenSavepoint(Pager *pPager, int nSavepoint){
59486 int rc = SQLITE_OK; /* Return code */
59487 int nCurrent = pPager->nSavepoint; /* Current number of savepoints */
59488 int ii; /* Iterator variable */
59489 PagerSavepoint *aNew; /* New Pager.aSavepoint array */
59490
59491 assert( pPager->eState>=PAGER_WRITER_LOCKED );
59492 assert( assert_pager_state(pPager) );
59493 assert( nSavepoint>nCurrent && pPager->useJournal );
59494
59495 /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
59496 ** if the allocation fails. Otherwise, zero the new portion in case a
59497 ** malloc failure occurs while populating it in the for(...) loop below.
59498 */
59499 aNew = (PagerSavepoint *)sqlite3Realloc(
59500 pOld: pPager->aSavepoint, nBytes: sizeof(PagerSavepoint)*nSavepoint
59501 );
59502 if( !aNew ){
59503 return SQLITE_NOMEM_BKPT;
59504 }
59505 memset(s: &aNew[nCurrent], c: 0, n: (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
59506 pPager->aSavepoint = aNew;
59507
59508 /* Populate the PagerSavepoint structures just allocated. */
59509 for(ii=nCurrent; ii<nSavepoint; ii++){
59510 aNew[ii].nOrig = pPager->dbSize;
59511 if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
59512 aNew[ii].iOffset = pPager->journalOff;
59513 }else{
59514 aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
59515 }
59516 aNew[ii].iSubRec = pPager->nSubRec;
59517 aNew[ii].pInSavepoint = sqlite3BitvecCreate(iSize: pPager->dbSize);
59518 aNew[ii].bTruncateOnRelease = 1;
59519 if( !aNew[ii].pInSavepoint ){
59520 return SQLITE_NOMEM_BKPT;
59521 }
59522 if( pagerUseWal(pPager) ){
59523 sqlite3WalSavepoint(pWal: pPager->pWal, aWalData: aNew[ii].aWalData);
59524 }
59525 pPager->nSavepoint = ii+1;
59526 }
59527 assert( pPager->nSavepoint==nSavepoint );
59528 assertTruncateConstraint(pPager);
59529 return rc;
59530}
59531SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
59532 assert( pPager->eState>=PAGER_WRITER_LOCKED );
59533 assert( assert_pager_state(pPager) );
59534
59535 if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
59536 return pagerOpenSavepoint(pPager, nSavepoint);
59537 }else{
59538 return SQLITE_OK;
59539 }
59540}
59541
59542
59543/*
59544** This function is called to rollback or release (commit) a savepoint.
59545** The savepoint to release or rollback need not be the most recently
59546** created savepoint.
59547**
59548** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE.
59549** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with
59550** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes
59551** that have occurred since the specified savepoint was created.
59552**
59553** The savepoint to rollback or release is identified by parameter
59554** iSavepoint. A value of 0 means to operate on the outermost savepoint
59555** (the first created). A value of (Pager.nSavepoint-1) means operate
59556** on the most recently created savepoint. If iSavepoint is greater than
59557** (Pager.nSavepoint-1), then this function is a no-op.
59558**
59559** If a negative value is passed to this function, then the current
59560** transaction is rolled back. This is different to calling
59561** sqlite3PagerRollback() because this function does not terminate
59562** the transaction or unlock the database, it just restores the
59563** contents of the database to its original state.
59564**
59565** In any case, all savepoints with an index greater than iSavepoint
59566** are destroyed. If this is a release operation (op==SAVEPOINT_RELEASE),
59567** then savepoint iSavepoint is also destroyed.
59568**
59569** This function may return SQLITE_NOMEM if a memory allocation fails,
59570** or an IO error code if an IO error occurs while rolling back a
59571** savepoint. If no errors occur, SQLITE_OK is returned.
59572*/
59573SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
59574 int rc = pPager->errCode;
59575
59576#ifdef SQLITE_ENABLE_ZIPVFS
59577 if( op==SAVEPOINT_RELEASE ) rc = SQLITE_OK;
59578#endif
59579
59580 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
59581 assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK );
59582
59583 if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
59584 int ii; /* Iterator variable */
59585 int nNew; /* Number of remaining savepoints after this op. */
59586
59587 /* Figure out how many savepoints will still be active after this
59588 ** operation. Store this value in nNew. Then free resources associated
59589 ** with any savepoints that are destroyed by this operation.
59590 */
59591 nNew = iSavepoint + (( op==SAVEPOINT_RELEASE ) ? 0 : 1);
59592 for(ii=nNew; ii<pPager->nSavepoint; ii++){
59593 sqlite3BitvecDestroy(p: pPager->aSavepoint[ii].pInSavepoint);
59594 }
59595 pPager->nSavepoint = nNew;
59596
59597 /* Truncate the sub-journal so that it only includes the parts
59598 ** that are still in use. */
59599 if( op==SAVEPOINT_RELEASE ){
59600 PagerSavepoint *pRel = &pPager->aSavepoint[nNew];
59601 if( pRel->bTruncateOnRelease && isOpen(pPager->sjfd) ){
59602 /* Only truncate if it is an in-memory sub-journal. */
59603 if( sqlite3JournalIsInMemory(p: pPager->sjfd) ){
59604 i64 sz = (pPager->pageSize+4)*(i64)pRel->iSubRec;
59605 rc = sqlite3OsTruncate(id: pPager->sjfd, size: sz);
59606 assert( rc==SQLITE_OK );
59607 }
59608 pPager->nSubRec = pRel->iSubRec;
59609 }
59610 }
59611 /* Else this is a rollback operation, playback the specified savepoint.
59612 ** If this is a temp-file, it is possible that the journal file has
59613 ** not yet been opened. In this case there have been no changes to
59614 ** the database file, so the playback operation can be skipped.
59615 */
59616 else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
59617 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
59618 rc = pagerPlaybackSavepoint(pPager, pSavepoint);
59619 assert(rc!=SQLITE_DONE);
59620 }
59621
59622#ifdef SQLITE_ENABLE_ZIPVFS
59623 /* If the cache has been modified but the savepoint cannot be rolled
59624 ** back journal_mode=off, put the pager in the error state. This way,
59625 ** if the VFS used by this pager includes ZipVFS, the entire transaction
59626 ** can be rolled back at the ZipVFS level. */
59627 else if(
59628 pPager->journalMode==PAGER_JOURNALMODE_OFF
59629 && pPager->eState>=PAGER_WRITER_CACHEMOD
59630 ){
59631 pPager->errCode = SQLITE_ABORT;
59632 pPager->eState = PAGER_ERROR;
59633 setGetterMethod(pPager);
59634 }
59635#endif
59636 }
59637
59638 return rc;
59639}
59640
59641/*
59642** Return the full pathname of the database file.
59643**
59644** Except, if the pager is in-memory only, then return an empty string if
59645** nullIfMemDb is true. This routine is called with nullIfMemDb==1 when
59646** used to report the filename to the user, for compatibility with legacy
59647** behavior. But when the Btree needs to know the filename for matching to
59648** shared cache, it uses nullIfMemDb==0 so that in-memory databases can
59649** participate in shared-cache.
59650**
59651** The return value to this routine is always safe to use with
59652** sqlite3_uri_parameter() and sqlite3_filename_database() and friends.
59653*/
59654SQLITE_PRIVATE const char *sqlite3PagerFilename(const Pager *pPager, int nullIfMemDb){
59655 static const char zFake[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
59656 return (nullIfMemDb && pPager->memDb) ? &zFake[4] : pPager->zFilename;
59657}
59658
59659/*
59660** Return the VFS structure for the pager.
59661*/
59662SQLITE_PRIVATE sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
59663 return pPager->pVfs;
59664}
59665
59666/*
59667** Return the file handle for the database file associated
59668** with the pager. This might return NULL if the file has
59669** not yet been opened.
59670*/
59671SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){
59672 return pPager->fd;
59673}
59674
59675/*
59676** Return the file handle for the journal file (if it exists).
59677** This will be either the rollback journal or the WAL file.
59678*/
59679SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager *pPager){
59680#if SQLITE_OMIT_WAL
59681 return pPager->jfd;
59682#else
59683 return pPager->pWal ? sqlite3WalFile(pWal: pPager->pWal) : pPager->jfd;
59684#endif
59685}
59686
59687/*
59688** Return the full pathname of the journal file.
59689*/
59690SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager *pPager){
59691 return pPager->zJournal;
59692}
59693
59694#ifndef SQLITE_OMIT_AUTOVACUUM
59695/*
59696** Move the page pPg to location pgno in the file.
59697**
59698** There must be no references to the page previously located at
59699** pgno (which we call pPgOld) though that page is allowed to be
59700** in cache. If the page previously located at pgno is not already
59701** in the rollback journal, it is not put there by by this routine.
59702**
59703** References to the page pPg remain valid. Updating any
59704** meta-data associated with pPg (i.e. data stored in the nExtra bytes
59705** allocated along with the page) is the responsibility of the caller.
59706**
59707** A transaction must be active when this routine is called. It used to be
59708** required that a statement transaction was not active, but this restriction
59709** has been removed (CREATE INDEX needs to move a page when a statement
59710** transaction is active).
59711**
59712** If the fourth argument, isCommit, is non-zero, then this page is being
59713** moved as part of a database reorganization just before the transaction
59714** is being committed. In this case, it is guaranteed that the database page
59715** pPg refers to will not be written to again within this transaction.
59716**
59717** This function may return SQLITE_NOMEM or an IO error code if an error
59718** occurs. Otherwise, it returns SQLITE_OK.
59719*/
59720SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
59721 PgHdr *pPgOld; /* The page being overwritten. */
59722 Pgno needSyncPgno = 0; /* Old value of pPg->pgno, if sync is required */
59723 int rc; /* Return code */
59724 Pgno origPgno; /* The original page number */
59725
59726 assert( pPg->nRef>0 );
59727 assert( pPager->eState==PAGER_WRITER_CACHEMOD
59728 || pPager->eState==PAGER_WRITER_DBMOD
59729 );
59730 assert( assert_pager_state(pPager) );
59731
59732 /* In order to be able to rollback, an in-memory database must journal
59733 ** the page we are moving from.
59734 */
59735 assert( pPager->tempFile || !MEMDB );
59736 if( pPager->tempFile ){
59737 rc = sqlite3PagerWrite(pPg);
59738 if( rc ) return rc;
59739 }
59740
59741 /* If the page being moved is dirty and has not been saved by the latest
59742 ** savepoint, then save the current contents of the page into the
59743 ** sub-journal now. This is required to handle the following scenario:
59744 **
59745 ** BEGIN;
59746 ** <journal page X, then modify it in memory>
59747 ** SAVEPOINT one;
59748 ** <Move page X to location Y>
59749 ** ROLLBACK TO one;
59750 **
59751 ** If page X were not written to the sub-journal here, it would not
59752 ** be possible to restore its contents when the "ROLLBACK TO one"
59753 ** statement were is processed.
59754 **
59755 ** subjournalPage() may need to allocate space to store pPg->pgno into
59756 ** one or more savepoint bitvecs. This is the reason this function
59757 ** may return SQLITE_NOMEM.
59758 */
59759 if( (pPg->flags & PGHDR_DIRTY)!=0
59760 && SQLITE_OK!=(rc = subjournalPageIfRequired(pPg))
59761 ){
59762 return rc;
59763 }
59764
59765 PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n",
59766 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
59767 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
59768
59769 /* If the journal needs to be sync()ed before page pPg->pgno can
59770 ** be written to, store pPg->pgno in local variable needSyncPgno.
59771 **
59772 ** If the isCommit flag is set, there is no need to remember that
59773 ** the journal needs to be sync()ed before database page pPg->pgno
59774 ** can be written to. The caller has already promised not to write to it.
59775 */
59776 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
59777 needSyncPgno = pPg->pgno;
59778 assert( pPager->journalMode==PAGER_JOURNALMODE_OFF ||
59779 pageInJournal(pPager, pPg) || pPg->pgno>pPager->dbOrigSize );
59780 assert( pPg->flags&PGHDR_DIRTY );
59781 }
59782
59783 /* If the cache contains a page with page-number pgno, remove it
59784 ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for
59785 ** page pgno before the 'move' operation, it needs to be retained
59786 ** for the page moved there.
59787 */
59788 pPg->flags &= ~PGHDR_NEED_SYNC;
59789 pPgOld = sqlite3PagerLookup(pPager, pgno);
59790 assert( !pPgOld || pPgOld->nRef==1 || CORRUPT_DB );
59791 if( pPgOld ){
59792 if( NEVER(pPgOld->nRef>1) ){
59793 sqlite3PagerUnrefNotNull(pPg: pPgOld);
59794 return SQLITE_CORRUPT_BKPT;
59795 }
59796 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
59797 if( pPager->tempFile ){
59798 /* Do not discard pages from an in-memory database since we might
59799 ** need to rollback later. Just move the page out of the way. */
59800 sqlite3PcacheMove(p: pPgOld, newPgno: pPager->dbSize+1);
59801 }else{
59802 sqlite3PcacheDrop(p: pPgOld);
59803 }
59804 }
59805
59806 origPgno = pPg->pgno;
59807 sqlite3PcacheMove(p: pPg, newPgno: pgno);
59808 sqlite3PcacheMakeDirty(p: pPg);
59809
59810 /* For an in-memory database, make sure the original page continues
59811 ** to exist, in case the transaction needs to roll back. Use pPgOld
59812 ** as the original page since it has already been allocated.
59813 */
59814 if( pPager->tempFile && pPgOld ){
59815 sqlite3PcacheMove(p: pPgOld, newPgno: origPgno);
59816 sqlite3PagerUnrefNotNull(pPg: pPgOld);
59817 }
59818
59819 if( needSyncPgno ){
59820 /* If needSyncPgno is non-zero, then the journal file needs to be
59821 ** sync()ed before any data is written to database file page needSyncPgno.
59822 ** Currently, no such page exists in the page-cache and the
59823 ** "is journaled" bitvec flag has been set. This needs to be remedied by
59824 ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
59825 ** flag.
59826 **
59827 ** If the attempt to load the page into the page-cache fails, (due
59828 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
59829 ** array. Otherwise, if the page is loaded and written again in
59830 ** this transaction, it may be written to the database file before
59831 ** it is synced into the journal file. This way, it may end up in
59832 ** the journal file twice, but that is not a problem.
59833 */
59834 PgHdr *pPgHdr;
59835 rc = sqlite3PagerGet(pPager, pgno: needSyncPgno, ppPage: &pPgHdr, flags: 0);
59836 if( rc!=SQLITE_OK ){
59837 if( needSyncPgno<=pPager->dbOrigSize ){
59838 assert( pPager->pTmpSpace!=0 );
59839 sqlite3BitvecClear(p: pPager->pInJournal, i: needSyncPgno, pBuf: pPager->pTmpSpace);
59840 }
59841 return rc;
59842 }
59843 pPgHdr->flags |= PGHDR_NEED_SYNC;
59844 sqlite3PcacheMakeDirty(p: pPgHdr);
59845 sqlite3PagerUnrefNotNull(pPg: pPgHdr);
59846 }
59847
59848 return SQLITE_OK;
59849}
59850#endif
59851
59852/*
59853** The page handle passed as the first argument refers to a dirty page
59854** with a page number other than iNew. This function changes the page's
59855** page number to iNew and sets the value of the PgHdr.flags field to
59856** the value passed as the third parameter.
59857*/
59858SQLITE_PRIVATE void sqlite3PagerRekey(DbPage *pPg, Pgno iNew, u16 flags){
59859 assert( pPg->pgno!=iNew );
59860 pPg->flags = flags;
59861 sqlite3PcacheMove(p: pPg, newPgno: iNew);
59862}
59863
59864/*
59865** Return a pointer to the data for the specified page.
59866*/
59867SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *pPg){
59868 assert( pPg->nRef>0 || pPg->pPager->memDb );
59869 return pPg->pData;
59870}
59871
59872/*
59873** Return a pointer to the Pager.nExtra bytes of "extra" space
59874** allocated along with the specified page.
59875*/
59876SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *pPg){
59877 return pPg->pExtra;
59878}
59879
59880/*
59881** Get/set the locking-mode for this pager. Parameter eMode must be one
59882** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
59883** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
59884** the locking-mode is set to the value specified.
59885**
59886** The returned value is either PAGER_LOCKINGMODE_NORMAL or
59887** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
59888** locking-mode.
59889*/
59890SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *pPager, int eMode){
59891 assert( eMode==PAGER_LOCKINGMODE_QUERY
59892 || eMode==PAGER_LOCKINGMODE_NORMAL
59893 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
59894 assert( PAGER_LOCKINGMODE_QUERY<0 );
59895 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
59896 assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) );
59897 if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pWal: pPager->pWal) ){
59898 pPager->exclusiveMode = (u8)eMode;
59899 }
59900 return (int)pPager->exclusiveMode;
59901}
59902
59903/*
59904** Set the journal-mode for this pager. Parameter eMode must be one of:
59905**
59906** PAGER_JOURNALMODE_DELETE
59907** PAGER_JOURNALMODE_TRUNCATE
59908** PAGER_JOURNALMODE_PERSIST
59909** PAGER_JOURNALMODE_OFF
59910** PAGER_JOURNALMODE_MEMORY
59911** PAGER_JOURNALMODE_WAL
59912**
59913** The journalmode is set to the value specified if the change is allowed.
59914** The change may be disallowed for the following reasons:
59915**
59916** * An in-memory database can only have its journal_mode set to _OFF
59917** or _MEMORY.
59918**
59919** * Temporary databases cannot have _WAL journalmode.
59920**
59921** The returned indicate the current (possibly updated) journal-mode.
59922*/
59923SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
59924 u8 eOld = pPager->journalMode; /* Prior journalmode */
59925
59926 /* The eMode parameter is always valid */
59927 assert( eMode==PAGER_JOURNALMODE_DELETE
59928 || eMode==PAGER_JOURNALMODE_TRUNCATE
59929 || eMode==PAGER_JOURNALMODE_PERSIST
59930 || eMode==PAGER_JOURNALMODE_OFF
59931 || eMode==PAGER_JOURNALMODE_WAL
59932 || eMode==PAGER_JOURNALMODE_MEMORY );
59933
59934 /* This routine is only called from the OP_JournalMode opcode, and
59935 ** the logic there will never allow a temporary file to be changed
59936 ** to WAL mode.
59937 */
59938 assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );
59939
59940 /* Do allow the journalmode of an in-memory database to be set to
59941 ** anything other than MEMORY or OFF
59942 */
59943 if( MEMDB ){
59944 assert( eOld==PAGER_JOURNALMODE_MEMORY || eOld==PAGER_JOURNALMODE_OFF );
59945 if( eMode!=PAGER_JOURNALMODE_MEMORY && eMode!=PAGER_JOURNALMODE_OFF ){
59946 eMode = eOld;
59947 }
59948 }
59949
59950 if( eMode!=eOld ){
59951
59952 /* Change the journal mode. */
59953 assert( pPager->eState!=PAGER_ERROR );
59954 pPager->journalMode = (u8)eMode;
59955
59956 /* When transistioning from TRUNCATE or PERSIST to any other journal
59957 ** mode except WAL, unless the pager is in locking_mode=exclusive mode,
59958 ** delete the journal file.
59959 */
59960 assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
59961 assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
59962 assert( (PAGER_JOURNALMODE_DELETE & 5)==0 );
59963 assert( (PAGER_JOURNALMODE_MEMORY & 5)==4 );
59964 assert( (PAGER_JOURNALMODE_OFF & 5)==0 );
59965 assert( (PAGER_JOURNALMODE_WAL & 5)==5 );
59966
59967 assert( isOpen(pPager->fd) || pPager->exclusiveMode );
59968 if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
59969
59970 /* In this case we would like to delete the journal file. If it is
59971 ** not possible, then that is not a problem. Deleting the journal file
59972 ** here is an optimization only.
59973 **
59974 ** Before deleting the journal file, obtain a RESERVED lock on the
59975 ** database file. This ensures that the journal file is not deleted
59976 ** while it is in use by some other client.
59977 */
59978 sqlite3OsClose(pId: pPager->jfd);
59979 if( pPager->eLock>=RESERVED_LOCK ){
59980 sqlite3OsDelete(pVfs: pPager->pVfs, zPath: pPager->zJournal, dirSync: 0);
59981 }else{
59982 int rc = SQLITE_OK;
59983 int state = pPager->eState;
59984 assert( state==PAGER_OPEN || state==PAGER_READER );
59985 if( state==PAGER_OPEN ){
59986 rc = sqlite3PagerSharedLock(pPager);
59987 }
59988 if( pPager->eState==PAGER_READER ){
59989 assert( rc==SQLITE_OK );
59990 rc = pagerLockDb(pPager, RESERVED_LOCK);
59991 }
59992 if( rc==SQLITE_OK ){
59993 sqlite3OsDelete(pVfs: pPager->pVfs, zPath: pPager->zJournal, dirSync: 0);
59994 }
59995 if( rc==SQLITE_OK && state==PAGER_READER ){
59996 pagerUnlockDb(pPager, SHARED_LOCK);
59997 }else if( state==PAGER_OPEN ){
59998 pager_unlock(pPager);
59999 }
60000 assert( state==pPager->eState );
60001 }
60002 }else if( eMode==PAGER_JOURNALMODE_OFF ){
60003 sqlite3OsClose(pId: pPager->jfd);
60004 }
60005 }
60006
60007 /* Return the new journal mode */
60008 return (int)pPager->journalMode;
60009}
60010
60011/*
60012** Return the current journal mode.
60013*/
60014SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager *pPager){
60015 return (int)pPager->journalMode;
60016}
60017
60018/*
60019** Return TRUE if the pager is in a state where it is OK to change the
60020** journalmode. Journalmode changes can only happen when the database
60021** is unmodified.
60022*/
60023SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
60024 assert( assert_pager_state(pPager) );
60025 if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
60026 if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
60027 return 1;
60028}
60029
60030/*
60031** Get/set the size-limit used for persistent journal files.
60032**
60033** Setting the size limit to -1 means no limit is enforced.
60034** An attempt to set a limit smaller than -1 is a no-op.
60035*/
60036SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
60037 if( iLimit>=-1 ){
60038 pPager->journalSizeLimit = iLimit;
60039 sqlite3WalLimit(pPager->pWal, iLimit);
60040 }
60041 return pPager->journalSizeLimit;
60042}
60043
60044/*
60045** Return a pointer to the pPager->pBackup variable. The backup module
60046** in backup.c maintains the content of this variable. This module
60047** uses it opaquely as an argument to sqlite3BackupRestart() and
60048** sqlite3BackupUpdate() only.
60049*/
60050SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
60051 return &pPager->pBackup;
60052}
60053
60054#ifndef SQLITE_OMIT_VACUUM
60055/*
60056** Unless this is an in-memory or temporary database, clear the pager cache.
60057*/
60058SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *pPager){
60059 assert( MEMDB==0 || pPager->tempFile );
60060 if( pPager->tempFile==0 ) pager_reset(pPager);
60061}
60062#endif
60063
60064
60065#ifndef SQLITE_OMIT_WAL
60066/*
60067** This function is called when the user invokes "PRAGMA wal_checkpoint",
60068** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint()
60069** or wal_blocking_checkpoint() API functions.
60070**
60071** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
60072*/
60073SQLITE_PRIVATE int sqlite3PagerCheckpoint(
60074 Pager *pPager, /* Checkpoint on this pager */
60075 sqlite3 *db, /* Db handle used to check for interrupts */
60076 int eMode, /* Type of checkpoint */
60077 int *pnLog, /* OUT: Final number of frames in log */
60078 int *pnCkpt /* OUT: Final number of checkpointed frames */
60079){
60080 int rc = SQLITE_OK;
60081 if( pPager->pWal ){
60082 rc = sqlite3WalCheckpoint(pWal: pPager->pWal, db, eMode,
60083 xBusy: (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler),
60084 pBusyArg: pPager->pBusyHandlerArg,
60085 sync_flags: pPager->walSyncFlags, nBuf: pPager->pageSize, zBuf: (u8 *)pPager->pTmpSpace,
60086 pnLog, pnCkpt
60087 );
60088 }
60089 return rc;
60090}
60091
60092SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
60093 return sqlite3WalCallback(pWal: pPager->pWal);
60094}
60095
60096/*
60097** Return true if the underlying VFS for the given pager supports the
60098** primitives necessary for write-ahead logging.
60099*/
60100SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager){
60101 const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
60102 if( pPager->noLock ) return 0;
60103 return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
60104}
60105
60106/*
60107** Attempt to take an exclusive lock on the database file. If a PENDING lock
60108** is obtained instead, immediately release it.
60109*/
60110static int pagerExclusiveLock(Pager *pPager){
60111 int rc; /* Return code */
60112
60113 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
60114 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
60115 if( rc!=SQLITE_OK ){
60116 /* If the attempt to grab the exclusive lock failed, release the
60117 ** pending lock that may have been obtained instead. */
60118 pagerUnlockDb(pPager, SHARED_LOCK);
60119 }
60120
60121 return rc;
60122}
60123
60124/*
60125** Call sqlite3WalOpen() to open the WAL handle. If the pager is in
60126** exclusive-locking mode when this function is called, take an EXCLUSIVE
60127** lock on the database file and use heap-memory to store the wal-index
60128** in. Otherwise, use the normal shared-memory.
60129*/
60130static int pagerOpenWal(Pager *pPager){
60131 int rc = SQLITE_OK;
60132
60133 assert( pPager->pWal==0 && pPager->tempFile==0 );
60134 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
60135
60136 /* If the pager is already in exclusive-mode, the WAL module will use
60137 ** heap-memory for the wal-index instead of the VFS shared-memory
60138 ** implementation. Take the exclusive lock now, before opening the WAL
60139 ** file, to make sure this is safe.
60140 */
60141 if( pPager->exclusiveMode ){
60142 rc = pagerExclusiveLock(pPager);
60143 }
60144
60145 /* Open the connection to the log file. If this operation fails,
60146 ** (e.g. due to malloc() failure), return an error code.
60147 */
60148 if( rc==SQLITE_OK ){
60149 rc = sqlite3WalOpen(pPager->pVfs,
60150 pPager->fd, pPager->zWal, pPager->exclusiveMode,
60151 pPager->journalSizeLimit, &pPager->pWal
60152 );
60153 }
60154 pagerFixMaplimit(pPager);
60155
60156 return rc;
60157}
60158
60159
60160/*
60161** The caller must be holding a SHARED lock on the database file to call
60162** this function.
60163**
60164** If the pager passed as the first argument is open on a real database
60165** file (not a temp file or an in-memory database), and the WAL file
60166** is not already open, make an attempt to open it now. If successful,
60167** return SQLITE_OK. If an error occurs or the VFS used by the pager does
60168** not support the xShmXXX() methods, return an error code. *pbOpen is
60169** not modified in either case.
60170**
60171** If the pager is open on a temp-file (or in-memory database), or if
60172** the WAL file is already open, set *pbOpen to 1 and return SQLITE_OK
60173** without doing anything.
60174*/
60175SQLITE_PRIVATE int sqlite3PagerOpenWal(
60176 Pager *pPager, /* Pager object */
60177 int *pbOpen /* OUT: Set to true if call is a no-op */
60178){
60179 int rc = SQLITE_OK; /* Return code */
60180
60181 assert( assert_pager_state(pPager) );
60182 assert( pPager->eState==PAGER_OPEN || pbOpen );
60183 assert( pPager->eState==PAGER_READER || !pbOpen );
60184 assert( pbOpen==0 || *pbOpen==0 );
60185 assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
60186
60187 if( !pPager->tempFile && !pPager->pWal ){
60188 if( !sqlite3PagerWalSupported(pPager) ) return SQLITE_CANTOPEN;
60189
60190 /* Close any rollback journal previously open */
60191 sqlite3OsClose(pId: pPager->jfd);
60192
60193 rc = pagerOpenWal(pPager);
60194 if( rc==SQLITE_OK ){
60195 pPager->journalMode = PAGER_JOURNALMODE_WAL;
60196 pPager->eState = PAGER_OPEN;
60197 }
60198 }else{
60199 *pbOpen = 1;
60200 }
60201
60202 return rc;
60203}
60204
60205/*
60206** This function is called to close the connection to the log file prior
60207** to switching from WAL to rollback mode.
60208**
60209** Before closing the log file, this function attempts to take an
60210** EXCLUSIVE lock on the database file. If this cannot be obtained, an
60211** error (SQLITE_BUSY) is returned and the log connection is not closed.
60212** If successful, the EXCLUSIVE lock is not released before returning.
60213*/
60214SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager, sqlite3 *db){
60215 int rc = SQLITE_OK;
60216
60217 assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
60218
60219 /* If the log file is not already open, but does exist in the file-system,
60220 ** it may need to be checkpointed before the connection can switch to
60221 ** rollback mode. Open it now so this can happen.
60222 */
60223 if( !pPager->pWal ){
60224 int logexists = 0;
60225 rc = pagerLockDb(pPager, SHARED_LOCK);
60226 if( rc==SQLITE_OK ){
60227 rc = sqlite3OsAccess(
60228 pVfs: pPager->pVfs, zPath: pPager->zWal, SQLITE_ACCESS_EXISTS, pResOut: &logexists
60229 );
60230 }
60231 if( rc==SQLITE_OK && logexists ){
60232 rc = pagerOpenWal(pPager);
60233 }
60234 }
60235
60236 /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on
60237 ** the database file, the log and log-summary files will be deleted.
60238 */
60239 if( rc==SQLITE_OK && pPager->pWal ){
60240 rc = pagerExclusiveLock(pPager);
60241 if( rc==SQLITE_OK ){
60242 rc = sqlite3WalClose(pWal: pPager->pWal, db, sync_flags: pPager->walSyncFlags,
60243 pPager->pageSize, (u8*)pPager->pTmpSpace);
60244 pPager->pWal = 0;
60245 pagerFixMaplimit(pPager);
60246 if( rc && !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
60247 }
60248 }
60249 return rc;
60250}
60251
60252#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
60253/*
60254** If pager pPager is a wal-mode database not in exclusive locking mode,
60255** invoke the sqlite3WalWriteLock() function on the associated Wal object
60256** with the same db and bLock parameters as were passed to this function.
60257** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
60258*/
60259SQLITE_PRIVATE int sqlite3PagerWalWriteLock(Pager *pPager, int bLock){
60260 int rc = SQLITE_OK;
60261 if( pagerUseWal(pPager) && pPager->exclusiveMode==0 ){
60262 rc = sqlite3WalWriteLock(pPager->pWal, bLock);
60263 }
60264 return rc;
60265}
60266
60267/*
60268** Set the database handle used by the wal layer to determine if
60269** blocking locks are required.
60270*/
60271SQLITE_PRIVATE void sqlite3PagerWalDb(Pager *pPager, sqlite3 *db){
60272 if( pagerUseWal(pPager) ){
60273 sqlite3WalDb(pPager->pWal, db);
60274 }
60275}
60276#endif
60277
60278#ifdef SQLITE_ENABLE_SNAPSHOT
60279/*
60280** If this is a WAL database, obtain a snapshot handle for the snapshot
60281** currently open. Otherwise, return an error.
60282*/
60283SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot){
60284 int rc = SQLITE_ERROR;
60285 if( pPager->pWal ){
60286 rc = sqlite3WalSnapshotGet(pPager->pWal, ppSnapshot);
60287 }
60288 return rc;
60289}
60290
60291/*
60292** If this is a WAL database, store a pointer to pSnapshot. Next time a
60293** read transaction is opened, attempt to read from the snapshot it
60294** identifies. If this is not a WAL database, return an error.
60295*/
60296SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(
60297 Pager *pPager,
60298 sqlite3_snapshot *pSnapshot
60299){
60300 int rc = SQLITE_OK;
60301 if( pPager->pWal ){
60302 sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot);
60303 }else{
60304 rc = SQLITE_ERROR;
60305 }
60306 return rc;
60307}
60308
60309/*
60310** If this is a WAL database, call sqlite3WalSnapshotRecover(). If this
60311** is not a WAL database, return an error.
60312*/
60313SQLITE_PRIVATE int sqlite3PagerSnapshotRecover(Pager *pPager){
60314 int rc;
60315 if( pPager->pWal ){
60316 rc = sqlite3WalSnapshotRecover(pPager->pWal);
60317 }else{
60318 rc = SQLITE_ERROR;
60319 }
60320 return rc;
60321}
60322
60323/*
60324** The caller currently has a read transaction open on the database.
60325** If this is not a WAL database, SQLITE_ERROR is returned. Otherwise,
60326** this function takes a SHARED lock on the CHECKPOINTER slot and then
60327** checks if the snapshot passed as the second argument is still
60328** available. If so, SQLITE_OK is returned.
60329**
60330** If the snapshot is not available, SQLITE_ERROR is returned. Or, if
60331** the CHECKPOINTER lock cannot be obtained, SQLITE_BUSY. If any error
60332** occurs (any value other than SQLITE_OK is returned), the CHECKPOINTER
60333** lock is released before returning.
60334*/
60335SQLITE_PRIVATE int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot){
60336 int rc;
60337 if( pPager->pWal ){
60338 rc = sqlite3WalSnapshotCheck(pPager->pWal, pSnapshot);
60339 }else{
60340 rc = SQLITE_ERROR;
60341 }
60342 return rc;
60343}
60344
60345/*
60346** Release a lock obtained by an earlier successful call to
60347** sqlite3PagerSnapshotCheck().
60348*/
60349SQLITE_PRIVATE void sqlite3PagerSnapshotUnlock(Pager *pPager){
60350 assert( pPager->pWal );
60351 sqlite3WalSnapshotUnlock(pPager->pWal);
60352}
60353
60354#endif /* SQLITE_ENABLE_SNAPSHOT */
60355#endif /* !SQLITE_OMIT_WAL */
60356
60357#ifdef SQLITE_ENABLE_ZIPVFS
60358/*
60359** A read-lock must be held on the pager when this function is called. If
60360** the pager is in WAL mode and the WAL file currently contains one or more
60361** frames, return the size in bytes of the page images stored within the
60362** WAL frames. Otherwise, if this is not a WAL database or the WAL file
60363** is empty, return 0.
60364*/
60365SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){
60366 assert( pPager->eState>=PAGER_READER );
60367 return sqlite3WalFramesize(pPager->pWal);
60368}
60369#endif
60370
60371#endif /* SQLITE_OMIT_DISKIO */
60372
60373/************** End of pager.c ***********************************************/
60374/************** Begin file wal.c *********************************************/
60375/*
60376** 2010 February 1
60377**
60378** The author disclaims copyright to this source code. In place of
60379** a legal notice, here is a blessing:
60380**
60381** May you do good and not evil.
60382** May you find forgiveness for yourself and forgive others.
60383** May you share freely, never taking more than you give.
60384**
60385*************************************************************************
60386**
60387** This file contains the implementation of a write-ahead log (WAL) used in
60388** "journal_mode=WAL" mode.
60389**
60390** WRITE-AHEAD LOG (WAL) FILE FORMAT
60391**
60392** A WAL file consists of a header followed by zero or more "frames".
60393** Each frame records the revised content of a single page from the
60394** database file. All changes to the database are recorded by writing
60395** frames into the WAL. Transactions commit when a frame is written that
60396** contains a commit marker. A single WAL can and usually does record
60397** multiple transactions. Periodically, the content of the WAL is
60398** transferred back into the database file in an operation called a
60399** "checkpoint".
60400**
60401** A single WAL file can be used multiple times. In other words, the
60402** WAL can fill up with frames and then be checkpointed and then new
60403** frames can overwrite the old ones. A WAL always grows from beginning
60404** toward the end. Checksums and counters attached to each frame are
60405** used to determine which frames within the WAL are valid and which
60406** are leftovers from prior checkpoints.
60407**
60408** The WAL header is 32 bytes in size and consists of the following eight
60409** big-endian 32-bit unsigned integer values:
60410**
60411** 0: Magic number. 0x377f0682 or 0x377f0683
60412** 4: File format version. Currently 3007000
60413** 8: Database page size. Example: 1024
60414** 12: Checkpoint sequence number
60415** 16: Salt-1, random integer incremented with each checkpoint
60416** 20: Salt-2, a different random integer changing with each ckpt
60417** 24: Checksum-1 (first part of checksum for first 24 bytes of header).
60418** 28: Checksum-2 (second part of checksum for first 24 bytes of header).
60419**
60420** Immediately following the wal-header are zero or more frames. Each
60421** frame consists of a 24-byte frame-header followed by a <page-size> bytes
60422** of page data. The frame-header is six big-endian 32-bit unsigned
60423** integer values, as follows:
60424**
60425** 0: Page number.
60426** 4: For commit records, the size of the database image in pages
60427** after the commit. For all other records, zero.
60428** 8: Salt-1 (copied from the header)
60429** 12: Salt-2 (copied from the header)
60430** 16: Checksum-1.
60431** 20: Checksum-2.
60432**
60433** A frame is considered valid if and only if the following conditions are
60434** true:
60435**
60436** (1) The salt-1 and salt-2 values in the frame-header match
60437** salt values in the wal-header
60438**
60439** (2) The checksum values in the final 8 bytes of the frame-header
60440** exactly match the checksum computed consecutively on the
60441** WAL header and the first 8 bytes and the content of all frames
60442** up to and including the current frame.
60443**
60444** The checksum is computed using 32-bit big-endian integers if the
60445** magic number in the first 4 bytes of the WAL is 0x377f0683 and it
60446** is computed using little-endian if the magic number is 0x377f0682.
60447** The checksum values are always stored in the frame header in a
60448** big-endian format regardless of which byte order is used to compute
60449** the checksum. The checksum is computed by interpreting the input as
60450** an even number of unsigned 32-bit integers: x[0] through x[N]. The
60451** algorithm used for the checksum is as follows:
60452**
60453** for i from 0 to n-1 step 2:
60454** s0 += x[i] + s1;
60455** s1 += x[i+1] + s0;
60456** endfor
60457**
60458** Note that s0 and s1 are both weighted checksums using fibonacci weights
60459** in reverse order (the largest fibonacci weight occurs on the first element
60460** of the sequence being summed.) The s1 value spans all 32-bit
60461** terms of the sequence whereas s0 omits the final term.
60462**
60463** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
60464** WAL is transferred into the database, then the database is VFS.xSync-ed.
60465** The VFS.xSync operations serve as write barriers - all writes launched
60466** before the xSync must complete before any write that launches after the
60467** xSync begins.
60468**
60469** After each checkpoint, the salt-1 value is incremented and the salt-2
60470** value is randomized. This prevents old and new frames in the WAL from
60471** being considered valid at the same time and being checkpointing together
60472** following a crash.
60473**
60474** READER ALGORITHM
60475**
60476** To read a page from the database (call it page number P), a reader
60477** first checks the WAL to see if it contains page P. If so, then the
60478** last valid instance of page P that is a followed by a commit frame
60479** or is a commit frame itself becomes the value read. If the WAL
60480** contains no copies of page P that are valid and which are a commit
60481** frame or are followed by a commit frame, then page P is read from
60482** the database file.
60483**
60484** To start a read transaction, the reader records the index of the last
60485** valid frame in the WAL. The reader uses this recorded "mxFrame" value
60486** for all subsequent read operations. New transactions can be appended
60487** to the WAL, but as long as the reader uses its original mxFrame value
60488** and ignores the newly appended content, it will see a consistent snapshot
60489** of the database from a single point in time. This technique allows
60490** multiple concurrent readers to view different versions of the database
60491** content simultaneously.
60492**
60493** The reader algorithm in the previous paragraphs works correctly, but
60494** because frames for page P can appear anywhere within the WAL, the
60495** reader has to scan the entire WAL looking for page P frames. If the
60496** WAL is large (multiple megabytes is typical) that scan can be slow,
60497** and read performance suffers. To overcome this problem, a separate
60498** data structure called the wal-index is maintained to expedite the
60499** search for frames of a particular page.
60500**
60501** WAL-INDEX FORMAT
60502**
60503** Conceptually, the wal-index is shared memory, though VFS implementations
60504** might choose to implement the wal-index using a mmapped file. Because
60505** the wal-index is shared memory, SQLite does not support journal_mode=WAL
60506** on a network filesystem. All users of the database must be able to
60507** share memory.
60508**
60509** In the default unix and windows implementation, the wal-index is a mmapped
60510** file whose name is the database name with a "-shm" suffix added. For that
60511** reason, the wal-index is sometimes called the "shm" file.
60512**
60513** The wal-index is transient. After a crash, the wal-index can (and should
60514** be) reconstructed from the original WAL file. In fact, the VFS is required
60515** to either truncate or zero the header of the wal-index when the last
60516** connection to it closes. Because the wal-index is transient, it can
60517** use an architecture-specific format; it does not have to be cross-platform.
60518** Hence, unlike the database and WAL file formats which store all values
60519** as big endian, the wal-index can store multi-byte values in the native
60520** byte order of the host computer.
60521**
60522** The purpose of the wal-index is to answer this question quickly: Given
60523** a page number P and a maximum frame index M, return the index of the
60524** last frame in the wal before frame M for page P in the WAL, or return
60525** NULL if there are no frames for page P in the WAL prior to M.
60526**
60527** The wal-index consists of a header region, followed by an one or
60528** more index blocks.
60529**
60530** The wal-index header contains the total number of frames within the WAL
60531** in the mxFrame field.
60532**
60533** Each index block except for the first contains information on
60534** HASHTABLE_NPAGE frames. The first index block contains information on
60535** HASHTABLE_NPAGE_ONE frames. The values of HASHTABLE_NPAGE_ONE and
60536** HASHTABLE_NPAGE are selected so that together the wal-index header and
60537** first index block are the same size as all other index blocks in the
60538** wal-index. The values are:
60539**
60540** HASHTABLE_NPAGE 4096
60541** HASHTABLE_NPAGE_ONE 4062
60542**
60543** Each index block contains two sections, a page-mapping that contains the
60544** database page number associated with each wal frame, and a hash-table
60545** that allows readers to query an index block for a specific page number.
60546** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
60547** for the first index block) 32-bit page numbers. The first entry in the
60548** first index-block contains the database page number corresponding to the
60549** first frame in the WAL file. The first entry in the second index block
60550** in the WAL file corresponds to the (HASHTABLE_NPAGE_ONE+1)th frame in
60551** the log, and so on.
60552**
60553** The last index block in a wal-index usually contains less than the full
60554** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
60555** depending on the contents of the WAL file. This does not change the
60556** allocated size of the page-mapping array - the page-mapping array merely
60557** contains unused entries.
60558**
60559** Even without using the hash table, the last frame for page P
60560** can be found by scanning the page-mapping sections of each index block
60561** starting with the last index block and moving toward the first, and
60562** within each index block, starting at the end and moving toward the
60563** beginning. The first entry that equals P corresponds to the frame
60564** holding the content for that page.
60565**
60566** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
60567** HASHTABLE_NSLOT = 2*HASHTABLE_NPAGE, and there is one entry in the
60568** hash table for each page number in the mapping section, so the hash
60569** table is never more than half full. The expected number of collisions
60570** prior to finding a match is 1. Each entry of the hash table is an
60571** 1-based index of an entry in the mapping section of the same
60572** index block. Let K be the 1-based index of the largest entry in
60573** the mapping section. (For index blocks other than the last, K will
60574** always be exactly HASHTABLE_NPAGE (4096) and for the last index block
60575** K will be (mxFrame%HASHTABLE_NPAGE).) Unused slots of the hash table
60576** contain a value of 0.
60577**
60578** To look for page P in the hash table, first compute a hash iKey on
60579** P as follows:
60580**
60581** iKey = (P * 383) % HASHTABLE_NSLOT
60582**
60583** Then start scanning entries of the hash table, starting with iKey
60584** (wrapping around to the beginning when the end of the hash table is
60585** reached) until an unused hash slot is found. Let the first unused slot
60586** be at index iUnused. (iUnused might be less than iKey if there was
60587** wrap-around.) Because the hash table is never more than half full,
60588** the search is guaranteed to eventually hit an unused entry. Let
60589** iMax be the value between iKey and iUnused, closest to iUnused,
60590** where aHash[iMax]==P. If there is no iMax entry (if there exists
60591** no hash slot such that aHash[i]==p) then page P is not in the
60592** current index block. Otherwise the iMax-th mapping entry of the
60593** current index block corresponds to the last entry that references
60594** page P.
60595**
60596** A hash search begins with the last index block and moves toward the
60597** first index block, looking for entries corresponding to page P. On
60598** average, only two or three slots in each index block need to be
60599** examined in order to either find the last entry for page P, or to
60600** establish that no such entry exists in the block. Each index block
60601** holds over 4000 entries. So two or three index blocks are sufficient
60602** to cover a typical 10 megabyte WAL file, assuming 1K pages. 8 or 10
60603** comparisons (on average) suffice to either locate a frame in the
60604** WAL or to establish that the frame does not exist in the WAL. This
60605** is much faster than scanning the entire 10MB WAL.
60606**
60607** Note that entries are added in order of increasing K. Hence, one
60608** reader might be using some value K0 and a second reader that started
60609** at a later time (after additional transactions were added to the WAL
60610** and to the wal-index) might be using a different value K1, where K1>K0.
60611** Both readers can use the same hash table and mapping section to get
60612** the correct result. There may be entries in the hash table with
60613** K>K0 but to the first reader, those entries will appear to be unused
60614** slots in the hash table and so the first reader will get an answer as
60615** if no values greater than K0 had ever been inserted into the hash table
60616** in the first place - which is what reader one wants. Meanwhile, the
60617** second reader using K1 will see additional values that were inserted
60618** later, which is exactly what reader two wants.
60619**
60620** When a rollback occurs, the value of K is decreased. Hash table entries
60621** that correspond to frames greater than the new K value are removed
60622** from the hash table at this point.
60623*/
60624#ifndef SQLITE_OMIT_WAL
60625
60626/* #include "wal.h" */
60627
60628/*
60629** Trace output macros
60630*/
60631#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
60632SQLITE_PRIVATE int sqlite3WalTrace = 0;
60633# define WALTRACE(X) if(sqlite3WalTrace) sqlite3DebugPrintf X
60634#else
60635# define WALTRACE(X)
60636#endif
60637
60638/*
60639** The maximum (and only) versions of the wal and wal-index formats
60640** that may be interpreted by this version of SQLite.
60641**
60642** If a client begins recovering a WAL file and finds that (a) the checksum
60643** values in the wal-header are correct and (b) the version field is not
60644** WAL_MAX_VERSION, recovery fails and SQLite returns SQLITE_CANTOPEN.
60645**
60646** Similarly, if a client successfully reads a wal-index header (i.e. the
60647** checksum test is successful) and finds that the version field is not
60648** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
60649** returns SQLITE_CANTOPEN.
60650*/
60651#define WAL_MAX_VERSION 3007000
60652#define WALINDEX_MAX_VERSION 3007000
60653
60654/*
60655** Index numbers for various locking bytes. WAL_NREADER is the number
60656** of available reader locks and should be at least 3. The default
60657** is SQLITE_SHM_NLOCK==8 and WAL_NREADER==5.
60658**
60659** Technically, the various VFSes are free to implement these locks however
60660** they see fit. However, compatibility is encouraged so that VFSes can
60661** interoperate. The standard implemention used on both unix and windows
60662** is for the index number to indicate a byte offset into the
60663** WalCkptInfo.aLock[] array in the wal-index header. In other words, all
60664** locks are on the shm file. The WALINDEX_LOCK_OFFSET constant (which
60665** should be 120) is the location in the shm file for the first locking
60666** byte.
60667*/
60668#define WAL_WRITE_LOCK 0
60669#define WAL_ALL_BUT_WRITE 1
60670#define WAL_CKPT_LOCK 1
60671#define WAL_RECOVER_LOCK 2
60672#define WAL_READ_LOCK(I) (3+(I))
60673#define WAL_NREADER (SQLITE_SHM_NLOCK-3)
60674
60675
60676/* Object declarations */
60677typedef struct WalIndexHdr WalIndexHdr;
60678typedef struct WalIterator WalIterator;
60679typedef struct WalCkptInfo WalCkptInfo;
60680
60681
60682/*
60683** The following object holds a copy of the wal-index header content.
60684**
60685** The actual header in the wal-index consists of two copies of this
60686** object followed by one instance of the WalCkptInfo object.
60687** For all versions of SQLite through 3.10.0 and probably beyond,
60688** the locking bytes (WalCkptInfo.aLock) start at offset 120 and
60689** the total header size is 136 bytes.
60690**
60691** The szPage value can be any power of 2 between 512 and 32768, inclusive.
60692** Or it can be 1 to represent a 65536-byte page. The latter case was
60693** added in 3.7.1 when support for 64K pages was added.
60694*/
60695struct WalIndexHdr {
60696 u32 iVersion; /* Wal-index version */
60697 u32 unused; /* Unused (padding) field */
60698 u32 iChange; /* Counter incremented each transaction */
60699 u8 isInit; /* 1 when initialized */
60700 u8 bigEndCksum; /* True if checksums in WAL are big-endian */
60701 u16 szPage; /* Database page size in bytes. 1==64K */
60702 u32 mxFrame; /* Index of last valid frame in the WAL */
60703 u32 nPage; /* Size of database in pages */
60704 u32 aFrameCksum[2]; /* Checksum of last frame in log */
60705 u32 aSalt[2]; /* Two salt values copied from WAL header */
60706 u32 aCksum[2]; /* Checksum over all prior fields */
60707};
60708
60709/*
60710** A copy of the following object occurs in the wal-index immediately
60711** following the second copy of the WalIndexHdr. This object stores
60712** information used by checkpoint.
60713**
60714** nBackfill is the number of frames in the WAL that have been written
60715** back into the database. (We call the act of moving content from WAL to
60716** database "backfilling".) The nBackfill number is never greater than
60717** WalIndexHdr.mxFrame. nBackfill can only be increased by threads
60718** holding the WAL_CKPT_LOCK lock (which includes a recovery thread).
60719** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from
60720** mxFrame back to zero when the WAL is reset.
60721**
60722** nBackfillAttempted is the largest value of nBackfill that a checkpoint
60723** has attempted to achieve. Normally nBackfill==nBackfillAtempted, however
60724** the nBackfillAttempted is set before any backfilling is done and the
60725** nBackfill is only set after all backfilling completes. So if a checkpoint
60726** crashes, nBackfillAttempted might be larger than nBackfill. The
60727** WalIndexHdr.mxFrame must never be less than nBackfillAttempted.
60728**
60729** The aLock[] field is a set of bytes used for locking. These bytes should
60730** never be read or written.
60731**
60732** There is one entry in aReadMark[] for each reader lock. If a reader
60733** holds read-lock K, then the value in aReadMark[K] is no greater than
60734** the mxFrame for that reader. The value READMARK_NOT_USED (0xffffffff)
60735** for any aReadMark[] means that entry is unused. aReadMark[0] is
60736** a special case; its value is never used and it exists as a place-holder
60737** to avoid having to offset aReadMark[] indexs by one. Readers holding
60738** WAL_READ_LOCK(0) always ignore the entire WAL and read all content
60739** directly from the database.
60740**
60741** The value of aReadMark[K] may only be changed by a thread that
60742** is holding an exclusive lock on WAL_READ_LOCK(K). Thus, the value of
60743** aReadMark[K] cannot changed while there is a reader is using that mark
60744** since the reader will be holding a shared lock on WAL_READ_LOCK(K).
60745**
60746** The checkpointer may only transfer frames from WAL to database where
60747** the frame numbers are less than or equal to every aReadMark[] that is
60748** in use (that is, every aReadMark[j] for which there is a corresponding
60749** WAL_READ_LOCK(j)). New readers (usually) pick the aReadMark[] with the
60750** largest value and will increase an unused aReadMark[] to mxFrame if there
60751** is not already an aReadMark[] equal to mxFrame. The exception to the
60752** previous sentence is when nBackfill equals mxFrame (meaning that everything
60753** in the WAL has been backfilled into the database) then new readers
60754** will choose aReadMark[0] which has value 0 and hence such reader will
60755** get all their all content directly from the database file and ignore
60756** the WAL.
60757**
60758** Writers normally append new frames to the end of the WAL. However,
60759** if nBackfill equals mxFrame (meaning that all WAL content has been
60760** written back into the database) and if no readers are using the WAL
60761** (in other words, if there are no WAL_READ_LOCK(i) where i>0) then
60762** the writer will first "reset" the WAL back to the beginning and start
60763** writing new content beginning at frame 1.
60764**
60765** We assume that 32-bit loads are atomic and so no locks are needed in
60766** order to read from any aReadMark[] entries.
60767*/
60768struct WalCkptInfo {
60769 u32 nBackfill; /* Number of WAL frames backfilled into DB */
60770 u32 aReadMark[WAL_NREADER]; /* Reader marks */
60771 u8 aLock[SQLITE_SHM_NLOCK]; /* Reserved space for locks */
60772 u32 nBackfillAttempted; /* WAL frames perhaps written, or maybe not */
60773 u32 notUsed0; /* Available for future enhancements */
60774};
60775#define READMARK_NOT_USED 0xffffffff
60776
60777/*
60778** This is a schematic view of the complete 136-byte header of the
60779** wal-index file (also known as the -shm file):
60780**
60781** +-----------------------------+
60782** 0: | iVersion | \
60783** +-----------------------------+ |
60784** 4: | (unused padding) | |
60785** +-----------------------------+ |
60786** 8: | iChange | |
60787** +-------+-------+-------------+ |
60788** 12: | bInit | bBig | szPage | |
60789** +-------+-------+-------------+ |
60790** 16: | mxFrame | | First copy of the
60791** +-----------------------------+ | WalIndexHdr object
60792** 20: | nPage | |
60793** +-----------------------------+ |
60794** 24: | aFrameCksum | |
60795** | | |
60796** +-----------------------------+ |
60797** 32: | aSalt | |
60798** | | |
60799** +-----------------------------+ |
60800** 40: | aCksum | |
60801** | | /
60802** +-----------------------------+
60803** 48: | iVersion | \
60804** +-----------------------------+ |
60805** 52: | (unused padding) | |
60806** +-----------------------------+ |
60807** 56: | iChange | |
60808** +-------+-------+-------------+ |
60809** 60: | bInit | bBig | szPage | |
60810** +-------+-------+-------------+ | Second copy of the
60811** 64: | mxFrame | | WalIndexHdr
60812** +-----------------------------+ |
60813** 68: | nPage | |
60814** +-----------------------------+ |
60815** 72: | aFrameCksum | |
60816** | | |
60817** +-----------------------------+ |
60818** 80: | aSalt | |
60819** | | |
60820** +-----------------------------+ |
60821** 88: | aCksum | |
60822** | | /
60823** +-----------------------------+
60824** 96: | nBackfill |
60825** +-----------------------------+
60826** 100: | 5 read marks |
60827** | |
60828** | |
60829** | |
60830** | |
60831** +-------+-------+------+------+
60832** 120: | Write | Ckpt | Rcvr | Rd0 | \
60833** +-------+-------+------+------+ ) 8 lock bytes
60834** | Read1 | Read2 | Rd3 | Rd4 | /
60835** +-------+-------+------+------+
60836** 128: | nBackfillAttempted |
60837** +-----------------------------+
60838** 132: | (unused padding) |
60839** +-----------------------------+
60840*/
60841
60842/* A block of WALINDEX_LOCK_RESERVED bytes beginning at
60843** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems
60844** only support mandatory file-locks, we do not read or write data
60845** from the region of the file on which locks are applied.
60846*/
60847#define WALINDEX_LOCK_OFFSET (sizeof(WalIndexHdr)*2+offsetof(WalCkptInfo,aLock))
60848#define WALINDEX_HDR_SIZE (sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))
60849
60850/* Size of header before each frame in wal */
60851#define WAL_FRAME_HDRSIZE 24
60852
60853/* Size of write ahead log header, including checksum. */
60854#define WAL_HDRSIZE 32
60855
60856/* WAL magic value. Either this value, or the same value with the least
60857** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
60858** big-endian format in the first 4 bytes of a WAL file.
60859**
60860** If the LSB is set, then the checksums for each frame within the WAL
60861** file are calculated by treating all data as an array of 32-bit
60862** big-endian words. Otherwise, they are calculated by interpreting
60863** all data as 32-bit little-endian words.
60864*/
60865#define WAL_MAGIC 0x377f0682
60866
60867/*
60868** Return the offset of frame iFrame in the write-ahead log file,
60869** assuming a database page size of szPage bytes. The offset returned
60870** is to the start of the write-ahead log frame-header.
60871*/
60872#define walFrameOffset(iFrame, szPage) ( \
60873 WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE) \
60874)
60875
60876/*
60877** An open write-ahead log file is represented by an instance of the
60878** following object.
60879*/
60880struct Wal {
60881 sqlite3_vfs *pVfs; /* The VFS used to create pDbFd */
60882 sqlite3_file *pDbFd; /* File handle for the database file */
60883 sqlite3_file *pWalFd; /* File handle for WAL file */
60884 u32 iCallback; /* Value to pass to log callback (or 0) */
60885 i64 mxWalSize; /* Truncate WAL to this size upon reset */
60886 int nWiData; /* Size of array apWiData */
60887 int szFirstBlock; /* Size of first block written to WAL file */
60888 volatile u32 **apWiData; /* Pointer to wal-index content in memory */
60889 u32 szPage; /* Database page size */
60890 i16 readLock; /* Which read lock is being held. -1 for none */
60891 u8 syncFlags; /* Flags to use to sync header writes */
60892 u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */
60893 u8 writeLock; /* True if in a write transaction */
60894 u8 ckptLock; /* True if holding a checkpoint lock */
60895 u8 readOnly; /* WAL_RDWR, WAL_RDONLY, or WAL_SHM_RDONLY */
60896 u8 truncateOnCommit; /* True to truncate WAL file on commit */
60897 u8 syncHeader; /* Fsync the WAL header if true */
60898 u8 padToSectorBoundary; /* Pad transactions out to the next sector */
60899 u8 bShmUnreliable; /* SHM content is read-only and unreliable */
60900 WalIndexHdr hdr; /* Wal-index header for current transaction */
60901 u32 minFrame; /* Ignore wal frames before this one */
60902 u32 iReCksum; /* On commit, recalculate checksums from here */
60903 const char *zWalName; /* Name of WAL file */
60904 u32 nCkpt; /* Checkpoint sequence counter in the wal-header */
60905#ifdef SQLITE_DEBUG
60906 u8 lockError; /* True if a locking error has occurred */
60907#endif
60908#ifdef SQLITE_ENABLE_SNAPSHOT
60909 WalIndexHdr *pSnapshot; /* Start transaction here if not NULL */
60910#endif
60911#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
60912 sqlite3 *db;
60913#endif
60914};
60915
60916/*
60917** Candidate values for Wal.exclusiveMode.
60918*/
60919#define WAL_NORMAL_MODE 0
60920#define WAL_EXCLUSIVE_MODE 1
60921#define WAL_HEAPMEMORY_MODE 2
60922
60923/*
60924** Possible values for WAL.readOnly
60925*/
60926#define WAL_RDWR 0 /* Normal read/write connection */
60927#define WAL_RDONLY 1 /* The WAL file is readonly */
60928#define WAL_SHM_RDONLY 2 /* The SHM file is readonly */
60929
60930/*
60931** Each page of the wal-index mapping contains a hash-table made up of
60932** an array of HASHTABLE_NSLOT elements of the following type.
60933*/
60934typedef u16 ht_slot;
60935
60936/*
60937** This structure is used to implement an iterator that loops through
60938** all frames in the WAL in database page order. Where two or more frames
60939** correspond to the same database page, the iterator visits only the
60940** frame most recently written to the WAL (in other words, the frame with
60941** the largest index).
60942**
60943** The internals of this structure are only accessed by:
60944**
60945** walIteratorInit() - Create a new iterator,
60946** walIteratorNext() - Step an iterator,
60947** walIteratorFree() - Free an iterator.
60948**
60949** This functionality is used by the checkpoint code (see walCheckpoint()).
60950*/
60951struct WalIterator {
60952 u32 iPrior; /* Last result returned from the iterator */
60953 int nSegment; /* Number of entries in aSegment[] */
60954 struct WalSegment {
60955 int iNext; /* Next slot in aIndex[] not yet returned */
60956 ht_slot *aIndex; /* i0, i1, i2... such that aPgno[iN] ascend */
60957 u32 *aPgno; /* Array of page numbers. */
60958 int nEntry; /* Nr. of entries in aPgno[] and aIndex[] */
60959 int iZero; /* Frame number associated with aPgno[0] */
60960 } aSegment[1]; /* One for every 32KB page in the wal-index */
60961};
60962
60963/*
60964** Define the parameters of the hash tables in the wal-index file. There
60965** is a hash-table following every HASHTABLE_NPAGE page numbers in the
60966** wal-index.
60967**
60968** Changing any of these constants will alter the wal-index format and
60969** create incompatibilities.
60970*/
60971#define HASHTABLE_NPAGE 4096 /* Must be power of 2 */
60972#define HASHTABLE_HASH_1 383 /* Should be prime */
60973#define HASHTABLE_NSLOT (HASHTABLE_NPAGE*2) /* Must be a power of 2 */
60974
60975/*
60976** The block of page numbers associated with the first hash-table in a
60977** wal-index is smaller than usual. This is so that there is a complete
60978** hash-table on each aligned 32KB page of the wal-index.
60979*/
60980#define HASHTABLE_NPAGE_ONE (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
60981
60982/* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
60983#define WALINDEX_PGSZ ( \
60984 sizeof(ht_slot)*HASHTABLE_NSLOT + HASHTABLE_NPAGE*sizeof(u32) \
60985)
60986
60987/*
60988** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
60989** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
60990** numbered from zero.
60991**
60992** If the wal-index is currently smaller the iPage pages then the size
60993** of the wal-index might be increased, but only if it is safe to do
60994** so. It is safe to enlarge the wal-index if pWal->writeLock is true
60995** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE.
60996**
60997** Three possible result scenarios:
60998**
60999** (1) rc==SQLITE_OK and *ppPage==Requested-Wal-Index-Page
61000** (2) rc>=SQLITE_ERROR and *ppPage==NULL
61001** (3) rc==SQLITE_OK and *ppPage==NULL // only if iPage==0
61002**
61003** Scenario (3) can only occur when pWal->writeLock is false and iPage==0
61004*/
61005static SQLITE_NOINLINE int walIndexPageRealloc(
61006 Wal *pWal, /* The WAL context */
61007 int iPage, /* The page we seek */
61008 volatile u32 **ppPage /* Write the page pointer here */
61009){
61010 int rc = SQLITE_OK;
61011
61012 /* Enlarge the pWal->apWiData[] array if required */
61013 if( pWal->nWiData<=iPage ){
61014 sqlite3_int64 nByte = sizeof(u32*)*(iPage+1);
61015 volatile u32 **apNew;
61016 apNew = (volatile u32 **)sqlite3Realloc(pOld: (void *)pWal->apWiData, nBytes: nByte);
61017 if( !apNew ){
61018 *ppPage = 0;
61019 return SQLITE_NOMEM_BKPT;
61020 }
61021 memset(s: (void*)&apNew[pWal->nWiData], c: 0,
61022 n: sizeof(u32*)*(iPage+1-pWal->nWiData));
61023 pWal->apWiData = apNew;
61024 pWal->nWiData = iPage+1;
61025 }
61026
61027 /* Request a pointer to the required page from the VFS */
61028 assert( pWal->apWiData[iPage]==0 );
61029 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
61030 pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
61031 if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
61032 }else{
61033 rc = sqlite3OsShmMap(id: pWal->pDbFd, iPage, WALINDEX_PGSZ,
61034 bExtend: pWal->writeLock, pp: (void volatile **)&pWal->apWiData[iPage]
61035 );
61036 assert( pWal->apWiData[iPage]!=0
61037 || rc!=SQLITE_OK
61038 || (pWal->writeLock==0 && iPage==0) );
61039 testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
61040 if( rc==SQLITE_OK ){
61041 if( iPage>0 && sqlite3FaultSim(iTest: 600) ) rc = SQLITE_NOMEM;
61042 }else if( (rc&0xff)==SQLITE_READONLY ){
61043 pWal->readOnly |= WAL_SHM_RDONLY;
61044 if( rc==SQLITE_READONLY ){
61045 rc = SQLITE_OK;
61046 }
61047 }
61048 }
61049
61050 *ppPage = pWal->apWiData[iPage];
61051 assert( iPage==0 || *ppPage || rc!=SQLITE_OK );
61052 return rc;
61053}
61054static int walIndexPage(
61055 Wal *pWal, /* The WAL context */
61056 int iPage, /* The page we seek */
61057 volatile u32 **ppPage /* Write the page pointer here */
61058){
61059 if( pWal->nWiData<=iPage || (*ppPage = pWal->apWiData[iPage])==0 ){
61060 return walIndexPageRealloc(pWal, iPage, ppPage);
61061 }
61062 return SQLITE_OK;
61063}
61064
61065/*
61066** Return a pointer to the WalCkptInfo structure in the wal-index.
61067*/
61068static volatile WalCkptInfo *walCkptInfo(Wal *pWal){
61069 assert( pWal->nWiData>0 && pWal->apWiData[0] );
61070 return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
61071}
61072
61073/*
61074** Return a pointer to the WalIndexHdr structure in the wal-index.
61075*/
61076static volatile WalIndexHdr *walIndexHdr(Wal *pWal){
61077 assert( pWal->nWiData>0 && pWal->apWiData[0] );
61078 return (volatile WalIndexHdr*)pWal->apWiData[0];
61079}
61080
61081/*
61082** The argument to this macro must be of type u32. On a little-endian
61083** architecture, it returns the u32 value that results from interpreting
61084** the 4 bytes as a big-endian value. On a big-endian architecture, it
61085** returns the value that would be produced by interpreting the 4 bytes
61086** of the input value as a little-endian integer.
61087*/
61088#define BYTESWAP32(x) ( \
61089 (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8) \
61090 + (((x)&0x00FF0000)>>8) + (((x)&0xFF000000)>>24) \
61091)
61092
61093/*
61094** Generate or extend an 8 byte checksum based on the data in
61095** array aByte[] and the initial values of aIn[0] and aIn[1] (or
61096** initial values of 0 and 0 if aIn==NULL).
61097**
61098** The checksum is written back into aOut[] before returning.
61099**
61100** nByte must be a positive multiple of 8.
61101*/
61102static void walChecksumBytes(
61103 int nativeCksum, /* True for native byte-order, false for non-native */
61104 u8 *a, /* Content to be checksummed */
61105 int nByte, /* Bytes of content in a[]. Must be a multiple of 8. */
61106 const u32 *aIn, /* Initial checksum value input */
61107 u32 *aOut /* OUT: Final checksum value output */
61108){
61109 u32 s1, s2;
61110 u32 *aData = (u32 *)a;
61111 u32 *aEnd = (u32 *)&a[nByte];
61112
61113 if( aIn ){
61114 s1 = aIn[0];
61115 s2 = aIn[1];
61116 }else{
61117 s1 = s2 = 0;
61118 }
61119
61120 assert( nByte>=8 );
61121 assert( (nByte&0x00000007)==0 );
61122 assert( nByte<=65536 );
61123
61124 if( nativeCksum ){
61125 do {
61126 s1 += *aData++ + s2;
61127 s2 += *aData++ + s1;
61128 }while( aData<aEnd );
61129 }else{
61130 do {
61131 s1 += BYTESWAP32(aData[0]) + s2;
61132 s2 += BYTESWAP32(aData[1]) + s1;
61133 aData += 2;
61134 }while( aData<aEnd );
61135 }
61136
61137 aOut[0] = s1;
61138 aOut[1] = s2;
61139}
61140
61141/*
61142** If there is the possibility of concurrent access to the SHM file
61143** from multiple threads and/or processes, then do a memory barrier.
61144*/
61145static void walShmBarrier(Wal *pWal){
61146 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
61147 sqlite3OsShmBarrier(id: pWal->pDbFd);
61148 }
61149}
61150
61151/*
61152** Add the SQLITE_NO_TSAN as part of the return-type of a function
61153** definition as a hint that the function contains constructs that
61154** might give false-positive TSAN warnings.
61155**
61156** See tag-20200519-1.
61157*/
61158#if defined(__clang__) && !defined(SQLITE_NO_TSAN)
61159# define SQLITE_NO_TSAN __attribute__((no_sanitize_thread))
61160#else
61161# define SQLITE_NO_TSAN
61162#endif
61163
61164/*
61165** Write the header information in pWal->hdr into the wal-index.
61166**
61167** The checksum on pWal->hdr is updated before it is written.
61168*/
61169static SQLITE_NO_TSAN void walIndexWriteHdr(Wal *pWal){
61170 volatile WalIndexHdr *aHdr = walIndexHdr(pWal);
61171 const int nCksum = offsetof(WalIndexHdr, aCksum);
61172
61173 assert( pWal->writeLock );
61174 pWal->hdr.isInit = 1;
61175 pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
61176 walChecksumBytes(nativeCksum: 1, a: (u8*)&pWal->hdr, nByte: nCksum, aIn: 0, aOut: pWal->hdr.aCksum);
61177 /* Possible TSAN false-positive. See tag-20200519-1 */
61178 memcpy(dest: (void*)&aHdr[1], src: (const void*)&pWal->hdr, n: sizeof(WalIndexHdr));
61179 walShmBarrier(pWal);
61180 memcpy(dest: (void*)&aHdr[0], src: (const void*)&pWal->hdr, n: sizeof(WalIndexHdr));
61181}
61182
61183/*
61184** This function encodes a single frame header and writes it to a buffer
61185** supplied by the caller. A frame-header is made up of a series of
61186** 4-byte big-endian integers, as follows:
61187**
61188** 0: Page number.
61189** 4: For commit records, the size of the database image in pages
61190** after the commit. For all other records, zero.
61191** 8: Salt-1 (copied from the wal-header)
61192** 12: Salt-2 (copied from the wal-header)
61193** 16: Checksum-1.
61194** 20: Checksum-2.
61195*/
61196static void walEncodeFrame(
61197 Wal *pWal, /* The write-ahead log */
61198 u32 iPage, /* Database page number for frame */
61199 u32 nTruncate, /* New db size (or 0 for non-commit frames) */
61200 u8 *aData, /* Pointer to page data */
61201 u8 *aFrame /* OUT: Write encoded frame here */
61202){
61203 int nativeCksum; /* True for native byte-order checksums */
61204 u32 *aCksum = pWal->hdr.aFrameCksum;
61205 assert( WAL_FRAME_HDRSIZE==24 );
61206 sqlite3Put4byte(p: &aFrame[0], v: iPage);
61207 sqlite3Put4byte(p: &aFrame[4], v: nTruncate);
61208 if( pWal->iReCksum==0 ){
61209 memcpy(dest: &aFrame[8], src: pWal->hdr.aSalt, n: 8);
61210
61211 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
61212 walChecksumBytes(nativeCksum, a: aFrame, nByte: 8, aIn: aCksum, aOut: aCksum);
61213 walChecksumBytes(nativeCksum, a: aData, nByte: pWal->szPage, aIn: aCksum, aOut: aCksum);
61214
61215 sqlite3Put4byte(p: &aFrame[16], v: aCksum[0]);
61216 sqlite3Put4byte(p: &aFrame[20], v: aCksum[1]);
61217 }else{
61218 memset(s: &aFrame[8], c: 0, n: 16);
61219 }
61220}
61221
61222/*
61223** Check to see if the frame with header in aFrame[] and content
61224** in aData[] is valid. If it is a valid frame, fill *piPage and
61225** *pnTruncate and return true. Return if the frame is not valid.
61226*/
61227static int walDecodeFrame(
61228 Wal *pWal, /* The write-ahead log */
61229 u32 *piPage, /* OUT: Database page number for frame */
61230 u32 *pnTruncate, /* OUT: New db size (or 0 if not commit) */
61231 u8 *aData, /* Pointer to page data (for checksum) */
61232 u8 *aFrame /* Frame data */
61233){
61234 int nativeCksum; /* True for native byte-order checksums */
61235 u32 *aCksum = pWal->hdr.aFrameCksum;
61236 u32 pgno; /* Page number of the frame */
61237 assert( WAL_FRAME_HDRSIZE==24 );
61238
61239 /* A frame is only valid if the salt values in the frame-header
61240 ** match the salt values in the wal-header.
61241 */
61242 if( memcmp(s1: &pWal->hdr.aSalt, s2: &aFrame[8], n: 8)!=0 ){
61243 return 0;
61244 }
61245
61246 /* A frame is only valid if the page number is creater than zero.
61247 */
61248 pgno = sqlite3Get4byte(p: &aFrame[0]);
61249 if( pgno==0 ){
61250 return 0;
61251 }
61252
61253 /* A frame is only valid if a checksum of the WAL header,
61254 ** all prior frams, the first 16 bytes of this frame-header,
61255 ** and the frame-data matches the checksum in the last 8
61256 ** bytes of this frame-header.
61257 */
61258 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
61259 walChecksumBytes(nativeCksum, a: aFrame, nByte: 8, aIn: aCksum, aOut: aCksum);
61260 walChecksumBytes(nativeCksum, a: aData, nByte: pWal->szPage, aIn: aCksum, aOut: aCksum);
61261 if( aCksum[0]!=sqlite3Get4byte(p: &aFrame[16])
61262 || aCksum[1]!=sqlite3Get4byte(p: &aFrame[20])
61263 ){
61264 /* Checksum failed. */
61265 return 0;
61266 }
61267
61268 /* If we reach this point, the frame is valid. Return the page number
61269 ** and the new database size.
61270 */
61271 *piPage = pgno;
61272 *pnTruncate = sqlite3Get4byte(p: &aFrame[4]);
61273 return 1;
61274}
61275
61276
61277#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
61278/*
61279** Names of locks. This routine is used to provide debugging output and is not
61280** a part of an ordinary build.
61281*/
61282static const char *walLockName(int lockIdx){
61283 if( lockIdx==WAL_WRITE_LOCK ){
61284 return "WRITE-LOCK";
61285 }else if( lockIdx==WAL_CKPT_LOCK ){
61286 return "CKPT-LOCK";
61287 }else if( lockIdx==WAL_RECOVER_LOCK ){
61288 return "RECOVER-LOCK";
61289 }else{
61290 static char zName[15];
61291 sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
61292 lockIdx-WAL_READ_LOCK(0));
61293 return zName;
61294 }
61295}
61296#endif /*defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
61297
61298
61299/*
61300** Set or release locks on the WAL. Locks are either shared or exclusive.
61301** A lock cannot be moved directly between shared and exclusive - it must go
61302** through the unlocked state first.
61303**
61304** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
61305*/
61306static int walLockShared(Wal *pWal, int lockIdx){
61307 int rc;
61308 if( pWal->exclusiveMode ) return SQLITE_OK;
61309 rc = sqlite3OsShmLock(id: pWal->pDbFd, offset: lockIdx, n: 1,
61310 SQLITE_SHM_LOCK | SQLITE_SHM_SHARED);
61311 WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
61312 walLockName(lockIdx), rc ? "failed" : "ok"));
61313 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
61314 return rc;
61315}
61316static void walUnlockShared(Wal *pWal, int lockIdx){
61317 if( pWal->exclusiveMode ) return;
61318 (void)sqlite3OsShmLock(id: pWal->pDbFd, offset: lockIdx, n: 1,
61319 SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED);
61320 WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
61321}
61322static int walLockExclusive(Wal *pWal, int lockIdx, int n){
61323 int rc;
61324 if( pWal->exclusiveMode ) return SQLITE_OK;
61325 rc = sqlite3OsShmLock(id: pWal->pDbFd, offset: lockIdx, n,
61326 SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE);
61327 WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
61328 walLockName(lockIdx), n, rc ? "failed" : "ok"));
61329 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
61330 return rc;
61331}
61332static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){
61333 if( pWal->exclusiveMode ) return;
61334 (void)sqlite3OsShmLock(id: pWal->pDbFd, offset: lockIdx, n,
61335 SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE);
61336 WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
61337 walLockName(lockIdx), n));
61338}
61339
61340/*
61341** Compute a hash on a page number. The resulting hash value must land
61342** between 0 and (HASHTABLE_NSLOT-1). The walHashNext() function advances
61343** the hash to the next value in the event of a collision.
61344*/
61345static int walHash(u32 iPage){
61346 assert( iPage>0 );
61347 assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
61348 return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
61349}
61350static int walNextHash(int iPriorHash){
61351 return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
61352}
61353
61354/*
61355** An instance of the WalHashLoc object is used to describe the location
61356** of a page hash table in the wal-index. This becomes the return value
61357** from walHashGet().
61358*/
61359typedef struct WalHashLoc WalHashLoc;
61360struct WalHashLoc {
61361 volatile ht_slot *aHash; /* Start of the wal-index hash table */
61362 volatile u32 *aPgno; /* aPgno[1] is the page of first frame indexed */
61363 u32 iZero; /* One less than the frame number of first indexed*/
61364};
61365
61366/*
61367** Return pointers to the hash table and page number array stored on
61368** page iHash of the wal-index. The wal-index is broken into 32KB pages
61369** numbered starting from 0.
61370**
61371** Set output variable pLoc->aHash to point to the start of the hash table
61372** in the wal-index file. Set pLoc->iZero to one less than the frame
61373** number of the first frame indexed by this hash table. If a
61374** slot in the hash table is set to N, it refers to frame number
61375** (pLoc->iZero+N) in the log.
61376**
61377** Finally, set pLoc->aPgno so that pLoc->aPgno[0] is the page number of the
61378** first frame indexed by the hash table, frame (pLoc->iZero).
61379*/
61380static int walHashGet(
61381 Wal *pWal, /* WAL handle */
61382 int iHash, /* Find the iHash'th table */
61383 WalHashLoc *pLoc /* OUT: Hash table location */
61384){
61385 int rc; /* Return code */
61386
61387 rc = walIndexPage(pWal, iPage: iHash, ppPage: &pLoc->aPgno);
61388 assert( rc==SQLITE_OK || iHash>0 );
61389
61390 if( pLoc->aPgno ){
61391 pLoc->aHash = (volatile ht_slot *)&pLoc->aPgno[HASHTABLE_NPAGE];
61392 if( iHash==0 ){
61393 pLoc->aPgno = &pLoc->aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
61394 pLoc->iZero = 0;
61395 }else{
61396 pLoc->iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
61397 }
61398 }else if( NEVER(rc==SQLITE_OK) ){
61399 rc = SQLITE_ERROR;
61400 }
61401 return rc;
61402}
61403
61404/*
61405** Return the number of the wal-index page that contains the hash-table
61406** and page-number array that contain entries corresponding to WAL frame
61407** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
61408** are numbered starting from 0.
61409*/
61410static int walFramePage(u32 iFrame){
61411 int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
61412 assert( (iHash==0 || iFrame>HASHTABLE_NPAGE_ONE)
61413 && (iHash>=1 || iFrame<=HASHTABLE_NPAGE_ONE)
61414 && (iHash<=1 || iFrame>(HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE))
61415 && (iHash>=2 || iFrame<=HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE)
61416 && (iHash<=2 || iFrame>(HASHTABLE_NPAGE_ONE+2*HASHTABLE_NPAGE))
61417 );
61418 assert( iHash>=0 );
61419 return iHash;
61420}
61421
61422/*
61423** Return the page number associated with frame iFrame in this WAL.
61424*/
61425static u32 walFramePgno(Wal *pWal, u32 iFrame){
61426 int iHash = walFramePage(iFrame);
61427 if( iHash==0 ){
61428 return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
61429 }
61430 return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
61431}
61432
61433/*
61434** Remove entries from the hash table that point to WAL slots greater
61435** than pWal->hdr.mxFrame.
61436**
61437** This function is called whenever pWal->hdr.mxFrame is decreased due
61438** to a rollback or savepoint.
61439**
61440** At most only the hash table containing pWal->hdr.mxFrame needs to be
61441** updated. Any later hash tables will be automatically cleared when
61442** pWal->hdr.mxFrame advances to the point where those hash tables are
61443** actually needed.
61444*/
61445static void walCleanupHash(Wal *pWal){
61446 WalHashLoc sLoc; /* Hash table location */
61447 int iLimit = 0; /* Zero values greater than this */
61448 int nByte; /* Number of bytes to zero in aPgno[] */
61449 int i; /* Used to iterate through aHash[] */
61450
61451 assert( pWal->writeLock );
61452 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
61453 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
61454 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
61455
61456 if( pWal->hdr.mxFrame==0 ) return;
61457
61458 /* Obtain pointers to the hash-table and page-number array containing
61459 ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
61460 ** that the page said hash-table and array reside on is already mapped.(1)
61461 */
61462 assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
61463 assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
61464 i = walHashGet(pWal, iHash: walFramePage(iFrame: pWal->hdr.mxFrame), pLoc: &sLoc);
61465 if( NEVER(i) ) return; /* Defense-in-depth, in case (1) above is wrong */
61466
61467 /* Zero all hash-table entries that correspond to frame numbers greater
61468 ** than pWal->hdr.mxFrame.
61469 */
61470 iLimit = pWal->hdr.mxFrame - sLoc.iZero;
61471 assert( iLimit>0 );
61472 for(i=0; i<HASHTABLE_NSLOT; i++){
61473 if( sLoc.aHash[i]>iLimit ){
61474 sLoc.aHash[i] = 0;
61475 }
61476 }
61477
61478 /* Zero the entries in the aPgno array that correspond to frames with
61479 ** frame numbers greater than pWal->hdr.mxFrame.
61480 */
61481 nByte = (int)((char *)sLoc.aHash - (char *)&sLoc.aPgno[iLimit]);
61482 assert( nByte>=0 );
61483 memset(s: (void *)&sLoc.aPgno[iLimit], c: 0, n: nByte);
61484
61485#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
61486 /* Verify that the every entry in the mapping region is still reachable
61487 ** via the hash table even after the cleanup.
61488 */
61489 if( iLimit ){
61490 int j; /* Loop counter */
61491 int iKey; /* Hash key */
61492 for(j=0; j<iLimit; j++){
61493 for(iKey=walHash(sLoc.aPgno[j]);sLoc.aHash[iKey];iKey=walNextHash(iKey)){
61494 if( sLoc.aHash[iKey]==j+1 ) break;
61495 }
61496 assert( sLoc.aHash[iKey]==j+1 );
61497 }
61498 }
61499#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
61500}
61501
61502
61503/*
61504** Set an entry in the wal-index that will map database page number
61505** pPage into WAL frame iFrame.
61506*/
61507static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
61508 int rc; /* Return code */
61509 WalHashLoc sLoc; /* Wal-index hash table location */
61510
61511 rc = walHashGet(pWal, iHash: walFramePage(iFrame), pLoc: &sLoc);
61512
61513 /* Assuming the wal-index file was successfully mapped, populate the
61514 ** page number array and hash table entry.
61515 */
61516 if( rc==SQLITE_OK ){
61517 int iKey; /* Hash table key */
61518 int idx; /* Value to write to hash-table slot */
61519 int nCollide; /* Number of hash collisions */
61520
61521 idx = iFrame - sLoc.iZero;
61522 assert( idx <= HASHTABLE_NSLOT/2 + 1 );
61523
61524 /* If this is the first entry to be added to this hash-table, zero the
61525 ** entire hash table and aPgno[] array before proceeding.
61526 */
61527 if( idx==1 ){
61528 int nByte = (int)((u8*)&sLoc.aHash[HASHTABLE_NSLOT] - (u8*)sLoc.aPgno);
61529 assert( nByte>=0 );
61530 memset(s: (void*)sLoc.aPgno, c: 0, n: nByte);
61531 }
61532
61533 /* If the entry in aPgno[] is already set, then the previous writer
61534 ** must have exited unexpectedly in the middle of a transaction (after
61535 ** writing one or more dirty pages to the WAL to free up memory).
61536 ** Remove the remnants of that writers uncommitted transaction from
61537 ** the hash-table before writing any new entries.
61538 */
61539 if( sLoc.aPgno[idx-1] ){
61540 walCleanupHash(pWal);
61541 assert( !sLoc.aPgno[idx-1] );
61542 }
61543
61544 /* Write the aPgno[] array entry and the hash-table slot. */
61545 nCollide = idx;
61546 for(iKey=walHash(iPage); sLoc.aHash[iKey]; iKey=walNextHash(iPriorHash: iKey)){
61547 if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
61548 }
61549 sLoc.aPgno[idx-1] = iPage;
61550 AtomicStore(&sLoc.aHash[iKey], (ht_slot)idx);
61551
61552#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
61553 /* Verify that the number of entries in the hash table exactly equals
61554 ** the number of entries in the mapping region.
61555 */
61556 {
61557 int i; /* Loop counter */
61558 int nEntry = 0; /* Number of entries in the hash table */
61559 for(i=0; i<HASHTABLE_NSLOT; i++){ if( sLoc.aHash[i] ) nEntry++; }
61560 assert( nEntry==idx );
61561 }
61562
61563 /* Verify that the every entry in the mapping region is reachable
61564 ** via the hash table. This turns out to be a really, really expensive
61565 ** thing to check, so only do this occasionally - not on every
61566 ** iteration.
61567 */
61568 if( (idx&0x3ff)==0 ){
61569 int i; /* Loop counter */
61570 for(i=0; i<idx; i++){
61571 for(iKey=walHash(sLoc.aPgno[i]);
61572 sLoc.aHash[iKey];
61573 iKey=walNextHash(iKey)){
61574 if( sLoc.aHash[iKey]==i+1 ) break;
61575 }
61576 assert( sLoc.aHash[iKey]==i+1 );
61577 }
61578 }
61579#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
61580 }
61581
61582 return rc;
61583}
61584
61585
61586/*
61587** Recover the wal-index by reading the write-ahead log file.
61588**
61589** This routine first tries to establish an exclusive lock on the
61590** wal-index to prevent other threads/processes from doing anything
61591** with the WAL or wal-index while recovery is running. The
61592** WAL_RECOVER_LOCK is also held so that other threads will know
61593** that this thread is running recovery. If unable to establish
61594** the necessary locks, this routine returns SQLITE_BUSY.
61595*/
61596static int walIndexRecover(Wal *pWal){
61597 int rc; /* Return Code */
61598 i64 nSize; /* Size of log file */
61599 u32 aFrameCksum[2] = {0, 0};
61600 int iLock; /* Lock offset to lock for checkpoint */
61601
61602 /* Obtain an exclusive lock on all byte in the locking range not already
61603 ** locked by the caller. The caller is guaranteed to have locked the
61604 ** WAL_WRITE_LOCK byte, and may have also locked the WAL_CKPT_LOCK byte.
61605 ** If successful, the same bytes that are locked here are unlocked before
61606 ** this function returns.
61607 */
61608 assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
61609 assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 );
61610 assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE );
61611 assert( pWal->writeLock );
61612 iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
61613 rc = walLockExclusive(pWal, lockIdx: iLock, WAL_READ_LOCK(0)-iLock);
61614 if( rc ){
61615 return rc;
61616 }
61617
61618 WALTRACE(("WAL%p: recovery begin...\n", pWal));
61619
61620 memset(s: &pWal->hdr, c: 0, n: sizeof(WalIndexHdr));
61621
61622 rc = sqlite3OsFileSize(id: pWal->pWalFd, pSize: &nSize);
61623 if( rc!=SQLITE_OK ){
61624 goto recovery_error;
61625 }
61626
61627 if( nSize>WAL_HDRSIZE ){
61628 u8 aBuf[WAL_HDRSIZE]; /* Buffer to load WAL header into */
61629 u32 *aPrivate = 0; /* Heap copy of *-shm hash being populated */
61630 u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */
61631 int szFrame; /* Number of bytes in buffer aFrame[] */
61632 u8 *aData; /* Pointer to data part of aFrame buffer */
61633 int szPage; /* Page size according to the log */
61634 u32 magic; /* Magic value read from WAL header */
61635 u32 version; /* Magic value read from WAL header */
61636 int isValid; /* True if this frame is valid */
61637 u32 iPg; /* Current 32KB wal-index page */
61638 u32 iLastFrame; /* Last frame in wal, based on nSize alone */
61639
61640 /* Read in the WAL header. */
61641 rc = sqlite3OsRead(id: pWal->pWalFd, pBuf: aBuf, WAL_HDRSIZE, offset: 0);
61642 if( rc!=SQLITE_OK ){
61643 goto recovery_error;
61644 }
61645
61646 /* If the database page size is not a power of two, or is greater than
61647 ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid
61648 ** data. Similarly, if the 'magic' value is invalid, ignore the whole
61649 ** WAL file.
61650 */
61651 magic = sqlite3Get4byte(p: &aBuf[0]);
61652 szPage = sqlite3Get4byte(p: &aBuf[8]);
61653 if( (magic&0xFFFFFFFE)!=WAL_MAGIC
61654 || szPage&(szPage-1)
61655 || szPage>SQLITE_MAX_PAGE_SIZE
61656 || szPage<512
61657 ){
61658 goto finished;
61659 }
61660 pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
61661 pWal->szPage = szPage;
61662 pWal->nCkpt = sqlite3Get4byte(p: &aBuf[12]);
61663 memcpy(dest: &pWal->hdr.aSalt, src: &aBuf[16], n: 8);
61664
61665 /* Verify that the WAL header checksum is correct */
61666 walChecksumBytes(nativeCksum: pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
61667 a: aBuf, WAL_HDRSIZE-2*4, aIn: 0, aOut: pWal->hdr.aFrameCksum
61668 );
61669 if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(p: &aBuf[24])
61670 || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(p: &aBuf[28])
61671 ){
61672 goto finished;
61673 }
61674
61675 /* Verify that the version number on the WAL format is one that
61676 ** are able to understand */
61677 version = sqlite3Get4byte(p: &aBuf[4]);
61678 if( version!=WAL_MAX_VERSION ){
61679 rc = SQLITE_CANTOPEN_BKPT;
61680 goto finished;
61681 }
61682
61683 /* Malloc a buffer to read frames into. */
61684 szFrame = szPage + WAL_FRAME_HDRSIZE;
61685 aFrame = (u8 *)sqlite3_malloc64(n: szFrame + WALINDEX_PGSZ);
61686 if( !aFrame ){
61687 rc = SQLITE_NOMEM_BKPT;
61688 goto recovery_error;
61689 }
61690 aData = &aFrame[WAL_FRAME_HDRSIZE];
61691 aPrivate = (u32*)&aData[szPage];
61692
61693 /* Read all frames from the log file. */
61694 iLastFrame = (nSize - WAL_HDRSIZE) / szFrame;
61695 for(iPg=0; iPg<=(u32)walFramePage(iFrame: iLastFrame); iPg++){
61696 u32 *aShare;
61697 u32 iFrame; /* Index of last frame read */
61698 u32 iLast = MIN(iLastFrame, HASHTABLE_NPAGE_ONE+iPg*HASHTABLE_NPAGE);
61699 u32 iFirst = 1 + (iPg==0?0:HASHTABLE_NPAGE_ONE+(iPg-1)*HASHTABLE_NPAGE);
61700 u32 nHdr, nHdr32;
61701 rc = walIndexPage(pWal, iPage: iPg, ppPage: (volatile u32**)&aShare);
61702 assert( aShare!=0 || rc!=SQLITE_OK );
61703 if( aShare==0 ) break;
61704 pWal->apWiData[iPg] = aPrivate;
61705
61706 for(iFrame=iFirst; iFrame<=iLast; iFrame++){
61707 i64 iOffset = walFrameOffset(iFrame, szPage);
61708 u32 pgno; /* Database page number for frame */
61709 u32 nTruncate; /* dbsize field from frame header */
61710
61711 /* Read and decode the next log frame. */
61712 rc = sqlite3OsRead(id: pWal->pWalFd, pBuf: aFrame, amt: szFrame, offset: iOffset);
61713 if( rc!=SQLITE_OK ) break;
61714 isValid = walDecodeFrame(pWal, piPage: &pgno, pnTruncate: &nTruncate, aData, aFrame);
61715 if( !isValid ) break;
61716 rc = walIndexAppend(pWal, iFrame, iPage: pgno);
61717 if( NEVER(rc!=SQLITE_OK) ) break;
61718
61719 /* If nTruncate is non-zero, this is a commit record. */
61720 if( nTruncate ){
61721 pWal->hdr.mxFrame = iFrame;
61722 pWal->hdr.nPage = nTruncate;
61723 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
61724 testcase( szPage<=32768 );
61725 testcase( szPage>=65536 );
61726 aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
61727 aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
61728 }
61729 }
61730 pWal->apWiData[iPg] = aShare;
61731 nHdr = (iPg==0 ? WALINDEX_HDR_SIZE : 0);
61732 nHdr32 = nHdr / sizeof(u32);
61733#ifndef SQLITE_SAFER_WALINDEX_RECOVERY
61734 /* Memcpy() should work fine here, on all reasonable implementations.
61735 ** Technically, memcpy() might change the destination to some
61736 ** intermediate value before setting to the final value, and that might
61737 ** cause a concurrent reader to malfunction. Memcpy() is allowed to
61738 ** do that, according to the spec, but no memcpy() implementation that
61739 ** we know of actually does that, which is why we say that memcpy()
61740 ** is safe for this. Memcpy() is certainly a lot faster.
61741 */
61742 memcpy(dest: &aShare[nHdr32], src: &aPrivate[nHdr32], WALINDEX_PGSZ-nHdr);
61743#else
61744 /* In the event that some platform is found for which memcpy()
61745 ** changes the destination to some intermediate value before
61746 ** setting the final value, this alternative copy routine is
61747 ** provided.
61748 */
61749 {
61750 int i;
61751 for(i=nHdr32; i<WALINDEX_PGSZ/sizeof(u32); i++){
61752 if( aShare[i]!=aPrivate[i] ){
61753 /* Atomic memory operations are not required here because if
61754 ** the value needs to be changed, that means it is not being
61755 ** accessed concurrently. */
61756 aShare[i] = aPrivate[i];
61757 }
61758 }
61759 }
61760#endif
61761 if( iFrame<=iLast ) break;
61762 }
61763
61764 sqlite3_free(p: aFrame);
61765 }
61766
61767finished:
61768 if( rc==SQLITE_OK ){
61769 volatile WalCkptInfo *pInfo;
61770 int i;
61771 pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
61772 pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
61773 walIndexWriteHdr(pWal);
61774
61775 /* Reset the checkpoint-header. This is safe because this thread is
61776 ** currently holding locks that exclude all other writers and
61777 ** checkpointers. Then set the values of read-mark slots 1 through N.
61778 */
61779 pInfo = walCkptInfo(pWal);
61780 pInfo->nBackfill = 0;
61781 pInfo->nBackfillAttempted = pWal->hdr.mxFrame;
61782 pInfo->aReadMark[0] = 0;
61783 for(i=1; i<WAL_NREADER; i++){
61784 rc = walLockExclusive(pWal, WAL_READ_LOCK(i), n: 1);
61785 if( rc==SQLITE_OK ){
61786 if( i==1 && pWal->hdr.mxFrame ){
61787 pInfo->aReadMark[i] = pWal->hdr.mxFrame;
61788 }else{
61789 pInfo->aReadMark[i] = READMARK_NOT_USED;
61790 }
61791 walUnlockExclusive(pWal, WAL_READ_LOCK(i), n: 1);
61792 }else if( rc!=SQLITE_BUSY ){
61793 goto recovery_error;
61794 }
61795 }
61796
61797 /* If more than one frame was recovered from the log file, report an
61798 ** event via sqlite3_log(). This is to help with identifying performance
61799 ** problems caused by applications routinely shutting down without
61800 ** checkpointing the log file.
61801 */
61802 if( pWal->hdr.nPage ){
61803 sqlite3_log(SQLITE_NOTICE_RECOVER_WAL,
61804 zFormat: "recovered %d frames from WAL file %s",
61805 pWal->hdr.mxFrame, pWal->zWalName
61806 );
61807 }
61808 }
61809
61810recovery_error:
61811 WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok"));
61812 walUnlockExclusive(pWal, lockIdx: iLock, WAL_READ_LOCK(0)-iLock);
61813 return rc;
61814}
61815
61816/*
61817** Close an open wal-index.
61818*/
61819static void walIndexClose(Wal *pWal, int isDelete){
61820 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE || pWal->bShmUnreliable ){
61821 int i;
61822 for(i=0; i<pWal->nWiData; i++){
61823 sqlite3_free(p: (void *)pWal->apWiData[i]);
61824 pWal->apWiData[i] = 0;
61825 }
61826 }
61827 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
61828 sqlite3OsShmUnmap(id: pWal->pDbFd, deleteFlag: isDelete);
61829 }
61830}
61831
61832/*
61833** Open a connection to the WAL file zWalName. The database file must
61834** already be opened on connection pDbFd. The buffer that zWalName points
61835** to must remain valid for the lifetime of the returned Wal* handle.
61836**
61837** A SHARED lock should be held on the database file when this function
61838** is called. The purpose of this SHARED lock is to prevent any other
61839** client from unlinking the WAL or wal-index file. If another process
61840** were to do this just after this client opened one of these files, the
61841** system would be badly broken.
61842**
61843** If the log file is successfully opened, SQLITE_OK is returned and
61844** *ppWal is set to point to a new WAL handle. If an error occurs,
61845** an SQLite error code is returned and *ppWal is left unmodified.
61846*/
61847SQLITE_PRIVATE int sqlite3WalOpen(
61848 sqlite3_vfs *pVfs, /* vfs module to open wal and wal-index */
61849 sqlite3_file *pDbFd, /* The open database file */
61850 const char *zWalName, /* Name of the WAL file */
61851 int bNoShm, /* True to run in heap-memory mode */
61852 i64 mxWalSize, /* Truncate WAL to this size on reset */
61853 Wal **ppWal /* OUT: Allocated Wal handle */
61854){
61855 int rc; /* Return Code */
61856 Wal *pRet; /* Object to allocate and return */
61857 int flags; /* Flags passed to OsOpen() */
61858
61859 assert( zWalName && zWalName[0] );
61860 assert( pDbFd );
61861
61862 /* Verify the values of various constants. Any changes to the values
61863 ** of these constants would result in an incompatible on-disk format
61864 ** for the -shm file. Any change that causes one of these asserts to
61865 ** fail is a backward compatibility problem, even if the change otherwise
61866 ** works.
61867 **
61868 ** This table also serves as a helpful cross-reference when trying to
61869 ** interpret hex dumps of the -shm file.
61870 */
61871 assert( 48 == sizeof(WalIndexHdr) );
61872 assert( 40 == sizeof(WalCkptInfo) );
61873 assert( 120 == WALINDEX_LOCK_OFFSET );
61874 assert( 136 == WALINDEX_HDR_SIZE );
61875 assert( 4096 == HASHTABLE_NPAGE );
61876 assert( 4062 == HASHTABLE_NPAGE_ONE );
61877 assert( 8192 == HASHTABLE_NSLOT );
61878 assert( 383 == HASHTABLE_HASH_1 );
61879 assert( 32768 == WALINDEX_PGSZ );
61880 assert( 8 == SQLITE_SHM_NLOCK );
61881 assert( 5 == WAL_NREADER );
61882 assert( 24 == WAL_FRAME_HDRSIZE );
61883 assert( 32 == WAL_HDRSIZE );
61884 assert( 120 == WALINDEX_LOCK_OFFSET + WAL_WRITE_LOCK );
61885 assert( 121 == WALINDEX_LOCK_OFFSET + WAL_CKPT_LOCK );
61886 assert( 122 == WALINDEX_LOCK_OFFSET + WAL_RECOVER_LOCK );
61887 assert( 123 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(0) );
61888 assert( 124 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(1) );
61889 assert( 125 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(2) );
61890 assert( 126 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(3) );
61891 assert( 127 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(4) );
61892
61893 /* In the amalgamation, the os_unix.c and os_win.c source files come before
61894 ** this source file. Verify that the #defines of the locking byte offsets
61895 ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value.
61896 ** For that matter, if the lock offset ever changes from its initial design
61897 ** value of 120, we need to know that so there is an assert() to check it.
61898 */
61899#ifdef WIN_SHM_BASE
61900 assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET );
61901#endif
61902#ifdef UNIX_SHM_BASE
61903 assert( UNIX_SHM_BASE==WALINDEX_LOCK_OFFSET );
61904#endif
61905
61906
61907 /* Allocate an instance of struct Wal to return. */
61908 *ppWal = 0;
61909 pRet = (Wal*)sqlite3MallocZero(n: sizeof(Wal) + pVfs->szOsFile);
61910 if( !pRet ){
61911 return SQLITE_NOMEM_BKPT;
61912 }
61913
61914 pRet->pVfs = pVfs;
61915 pRet->pWalFd = (sqlite3_file *)&pRet[1];
61916 pRet->pDbFd = pDbFd;
61917 pRet->readLock = -1;
61918 pRet->mxWalSize = mxWalSize;
61919 pRet->zWalName = zWalName;
61920 pRet->syncHeader = 1;
61921 pRet->padToSectorBoundary = 1;
61922 pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
61923
61924 /* Open file handle on the write-ahead log file. */
61925 flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_WAL);
61926 rc = sqlite3OsOpen(pVfs, zPath: zWalName, pFile: pRet->pWalFd, flags, pFlagsOut: &flags);
61927 if( rc==SQLITE_OK && flags&SQLITE_OPEN_READONLY ){
61928 pRet->readOnly = WAL_RDONLY;
61929 }
61930
61931 if( rc!=SQLITE_OK ){
61932 walIndexClose(pWal: pRet, isDelete: 0);
61933 sqlite3OsClose(pId: pRet->pWalFd);
61934 sqlite3_free(p: pRet);
61935 }else{
61936 int iDC = sqlite3OsDeviceCharacteristics(id: pDbFd);
61937 if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; }
61938 if( iDC & SQLITE_IOCAP_POWERSAFE_OVERWRITE ){
61939 pRet->padToSectorBoundary = 0;
61940 }
61941 *ppWal = pRet;
61942 WALTRACE(("WAL%d: opened\n", pRet));
61943 }
61944 return rc;
61945}
61946
61947/*
61948** Change the size to which the WAL file is trucated on each reset.
61949*/
61950SQLITE_PRIVATE void sqlite3WalLimit(Wal *pWal, i64 iLimit){
61951 if( pWal ) pWal->mxWalSize = iLimit;
61952}
61953
61954/*
61955** Find the smallest page number out of all pages held in the WAL that
61956** has not been returned by any prior invocation of this method on the
61957** same WalIterator object. Write into *piFrame the frame index where
61958** that page was last written into the WAL. Write into *piPage the page
61959** number.
61960**
61961** Return 0 on success. If there are no pages in the WAL with a page
61962** number larger than *piPage, then return 1.
61963*/
61964static int walIteratorNext(
61965 WalIterator *p, /* Iterator */
61966 u32 *piPage, /* OUT: The page number of the next page */
61967 u32 *piFrame /* OUT: Wal frame index of next page */
61968){
61969 u32 iMin; /* Result pgno must be greater than iMin */
61970 u32 iRet = 0xFFFFFFFF; /* 0xffffffff is never a valid page number */
61971 int i; /* For looping through segments */
61972
61973 iMin = p->iPrior;
61974 assert( iMin<0xffffffff );
61975 for(i=p->nSegment-1; i>=0; i--){
61976 struct WalSegment *pSegment = &p->aSegment[i];
61977 while( pSegment->iNext<pSegment->nEntry ){
61978 u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
61979 if( iPg>iMin ){
61980 if( iPg<iRet ){
61981 iRet = iPg;
61982 *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
61983 }
61984 break;
61985 }
61986 pSegment->iNext++;
61987 }
61988 }
61989
61990 *piPage = p->iPrior = iRet;
61991 return (iRet==0xFFFFFFFF);
61992}
61993
61994/*
61995** This function merges two sorted lists into a single sorted list.
61996**
61997** aLeft[] and aRight[] are arrays of indices. The sort key is
61998** aContent[aLeft[]] and aContent[aRight[]]. Upon entry, the following
61999** is guaranteed for all J<K:
62000**
62001** aContent[aLeft[J]] < aContent[aLeft[K]]
62002** aContent[aRight[J]] < aContent[aRight[K]]
62003**
62004** This routine overwrites aRight[] with a new (probably longer) sequence
62005** of indices such that the aRight[] contains every index that appears in
62006** either aLeft[] or the old aRight[] and such that the second condition
62007** above is still met.
62008**
62009** The aContent[aLeft[X]] values will be unique for all X. And the
62010** aContent[aRight[X]] values will be unique too. But there might be
62011** one or more combinations of X and Y such that
62012**
62013** aLeft[X]!=aRight[Y] && aContent[aLeft[X]] == aContent[aRight[Y]]
62014**
62015** When that happens, omit the aLeft[X] and use the aRight[Y] index.
62016*/
62017static void walMerge(
62018 const u32 *aContent, /* Pages in wal - keys for the sort */
62019 ht_slot *aLeft, /* IN: Left hand input list */
62020 int nLeft, /* IN: Elements in array *paLeft */
62021 ht_slot **paRight, /* IN/OUT: Right hand input list */
62022 int *pnRight, /* IN/OUT: Elements in *paRight */
62023 ht_slot *aTmp /* Temporary buffer */
62024){
62025 int iLeft = 0; /* Current index in aLeft */
62026 int iRight = 0; /* Current index in aRight */
62027 int iOut = 0; /* Current index in output buffer */
62028 int nRight = *pnRight;
62029 ht_slot *aRight = *paRight;
62030
62031 assert( nLeft>0 && nRight>0 );
62032 while( iRight<nRight || iLeft<nLeft ){
62033 ht_slot logpage;
62034 Pgno dbpage;
62035
62036 if( (iLeft<nLeft)
62037 && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]])
62038 ){
62039 logpage = aLeft[iLeft++];
62040 }else{
62041 logpage = aRight[iRight++];
62042 }
62043 dbpage = aContent[logpage];
62044
62045 aTmp[iOut++] = logpage;
62046 if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++;
62047
62048 assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage );
62049 assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage );
62050 }
62051
62052 *paRight = aLeft;
62053 *pnRight = iOut;
62054 memcpy(dest: aLeft, src: aTmp, n: sizeof(aTmp[0])*iOut);
62055}
62056
62057/*
62058** Sort the elements in list aList using aContent[] as the sort key.
62059** Remove elements with duplicate keys, preferring to keep the
62060** larger aList[] values.
62061**
62062** The aList[] entries are indices into aContent[]. The values in
62063** aList[] are to be sorted so that for all J<K:
62064**
62065** aContent[aList[J]] < aContent[aList[K]]
62066**
62067** For any X and Y such that
62068**
62069** aContent[aList[X]] == aContent[aList[Y]]
62070**
62071** Keep the larger of the two values aList[X] and aList[Y] and discard
62072** the smaller.
62073*/
62074static void walMergesort(
62075 const u32 *aContent, /* Pages in wal */
62076 ht_slot *aBuffer, /* Buffer of at least *pnList items to use */
62077 ht_slot *aList, /* IN/OUT: List to sort */
62078 int *pnList /* IN/OUT: Number of elements in aList[] */
62079){
62080 struct Sublist {
62081 int nList; /* Number of elements in aList */
62082 ht_slot *aList; /* Pointer to sub-list content */
62083 };
62084
62085 const int nList = *pnList; /* Size of input list */
62086 int nMerge = 0; /* Number of elements in list aMerge */
62087 ht_slot *aMerge = 0; /* List to be merged */
62088 int iList; /* Index into input list */
62089 u32 iSub = 0; /* Index into aSub array */
62090 struct Sublist aSub[13]; /* Array of sub-lists */
62091
62092 memset(s: aSub, c: 0, n: sizeof(aSub));
62093 assert( nList<=HASHTABLE_NPAGE && nList>0 );
62094 assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
62095
62096 for(iList=0; iList<nList; iList++){
62097 nMerge = 1;
62098 aMerge = &aList[iList];
62099 for(iSub=0; iList & (1<<iSub); iSub++){
62100 struct Sublist *p;
62101 assert( iSub<ArraySize(aSub) );
62102 p = &aSub[iSub];
62103 assert( p->aList && p->nList<=(1<<iSub) );
62104 assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
62105 walMerge(aContent, aLeft: p->aList, nLeft: p->nList, paRight: &aMerge, pnRight: &nMerge, aTmp: aBuffer);
62106 }
62107 aSub[iSub].aList = aMerge;
62108 aSub[iSub].nList = nMerge;
62109 }
62110
62111 for(iSub++; iSub<ArraySize(aSub); iSub++){
62112 if( nList & (1<<iSub) ){
62113 struct Sublist *p;
62114 assert( iSub<ArraySize(aSub) );
62115 p = &aSub[iSub];
62116 assert( p->nList<=(1<<iSub) );
62117 assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
62118 walMerge(aContent, aLeft: p->aList, nLeft: p->nList, paRight: &aMerge, pnRight: &nMerge, aTmp: aBuffer);
62119 }
62120 }
62121 assert( aMerge==aList );
62122 *pnList = nMerge;
62123
62124#ifdef SQLITE_DEBUG
62125 {
62126 int i;
62127 for(i=1; i<*pnList; i++){
62128 assert( aContent[aList[i]] > aContent[aList[i-1]] );
62129 }
62130 }
62131#endif
62132}
62133
62134/*
62135** Free an iterator allocated by walIteratorInit().
62136*/
62137static void walIteratorFree(WalIterator *p){
62138 sqlite3_free(p);
62139}
62140
62141/*
62142** Construct a WalInterator object that can be used to loop over all
62143** pages in the WAL following frame nBackfill in ascending order. Frames
62144** nBackfill or earlier may be included - excluding them is an optimization
62145** only. The caller must hold the checkpoint lock.
62146**
62147** On success, make *pp point to the newly allocated WalInterator object
62148** return SQLITE_OK. Otherwise, return an error code. If this routine
62149** returns an error, the value of *pp is undefined.
62150**
62151** The calling routine should invoke walIteratorFree() to destroy the
62152** WalIterator object when it has finished with it.
62153*/
62154static int walIteratorInit(Wal *pWal, u32 nBackfill, WalIterator **pp){
62155 WalIterator *p; /* Return value */
62156 int nSegment; /* Number of segments to merge */
62157 u32 iLast; /* Last frame in log */
62158 sqlite3_int64 nByte; /* Number of bytes to allocate */
62159 int i; /* Iterator variable */
62160 ht_slot *aTmp; /* Temp space used by merge-sort */
62161 int rc = SQLITE_OK; /* Return Code */
62162
62163 /* This routine only runs while holding the checkpoint lock. And
62164 ** it only runs if there is actually content in the log (mxFrame>0).
62165 */
62166 assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
62167 iLast = pWal->hdr.mxFrame;
62168
62169 /* Allocate space for the WalIterator object. */
62170 nSegment = walFramePage(iFrame: iLast) + 1;
62171 nByte = sizeof(WalIterator)
62172 + (nSegment-1)*sizeof(struct WalSegment)
62173 + iLast*sizeof(ht_slot);
62174 p = (WalIterator *)sqlite3_malloc64(n: nByte);
62175 if( !p ){
62176 return SQLITE_NOMEM_BKPT;
62177 }
62178 memset(s: p, c: 0, n: nByte);
62179 p->nSegment = nSegment;
62180
62181 /* Allocate temporary space used by the merge-sort routine. This block
62182 ** of memory will be freed before this function returns.
62183 */
62184 aTmp = (ht_slot *)sqlite3_malloc64(
62185 n: sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
62186 );
62187 if( !aTmp ){
62188 rc = SQLITE_NOMEM_BKPT;
62189 }
62190
62191 for(i=walFramePage(iFrame: nBackfill+1); rc==SQLITE_OK && i<nSegment; i++){
62192 WalHashLoc sLoc;
62193
62194 rc = walHashGet(pWal, iHash: i, pLoc: &sLoc);
62195 if( rc==SQLITE_OK ){
62196 int j; /* Counter variable */
62197 int nEntry; /* Number of entries in this segment */
62198 ht_slot *aIndex; /* Sorted index for this segment */
62199
62200 if( (i+1)==nSegment ){
62201 nEntry = (int)(iLast - sLoc.iZero);
62202 }else{
62203 nEntry = (int)((u32*)sLoc.aHash - (u32*)sLoc.aPgno);
62204 }
62205 aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[sLoc.iZero];
62206 sLoc.iZero++;
62207
62208 for(j=0; j<nEntry; j++){
62209 aIndex[j] = (ht_slot)j;
62210 }
62211 walMergesort(aContent: (u32 *)sLoc.aPgno, aBuffer: aTmp, aList: aIndex, pnList: &nEntry);
62212 p->aSegment[i].iZero = sLoc.iZero;
62213 p->aSegment[i].nEntry = nEntry;
62214 p->aSegment[i].aIndex = aIndex;
62215 p->aSegment[i].aPgno = (u32 *)sLoc.aPgno;
62216 }
62217 }
62218 sqlite3_free(p: aTmp);
62219
62220 if( rc!=SQLITE_OK ){
62221 walIteratorFree(p);
62222 p = 0;
62223 }
62224 *pp = p;
62225 return rc;
62226}
62227
62228#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
62229/*
62230** Attempt to enable blocking locks. Blocking locks are enabled only if (a)
62231** they are supported by the VFS, and (b) the database handle is configured
62232** with a busy-timeout. Return 1 if blocking locks are successfully enabled,
62233** or 0 otherwise.
62234*/
62235static int walEnableBlocking(Wal *pWal){
62236 int res = 0;
62237 if( pWal->db ){
62238 int tmout = pWal->db->busyTimeout;
62239 if( tmout ){
62240 int rc;
62241 rc = sqlite3OsFileControl(
62242 pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout
62243 );
62244 res = (rc==SQLITE_OK);
62245 }
62246 }
62247 return res;
62248}
62249
62250/*
62251** Disable blocking locks.
62252*/
62253static void walDisableBlocking(Wal *pWal){
62254 int tmout = 0;
62255 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout);
62256}
62257
62258/*
62259** If parameter bLock is true, attempt to enable blocking locks, take
62260** the WRITER lock, and then disable blocking locks. If blocking locks
62261** cannot be enabled, no attempt to obtain the WRITER lock is made. Return
62262** an SQLite error code if an error occurs, or SQLITE_OK otherwise. It is not
62263** an error if blocking locks can not be enabled.
62264**
62265** If the bLock parameter is false and the WRITER lock is held, release it.
62266*/
62267SQLITE_PRIVATE int sqlite3WalWriteLock(Wal *pWal, int bLock){
62268 int rc = SQLITE_OK;
62269 assert( pWal->readLock<0 || bLock==0 );
62270 if( bLock ){
62271 assert( pWal->db );
62272 if( walEnableBlocking(pWal) ){
62273 rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1);
62274 if( rc==SQLITE_OK ){
62275 pWal->writeLock = 1;
62276 }
62277 walDisableBlocking(pWal);
62278 }
62279 }else if( pWal->writeLock ){
62280 walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
62281 pWal->writeLock = 0;
62282 }
62283 return rc;
62284}
62285
62286/*
62287** Set the database handle used to determine if blocking locks are required.
62288*/
62289SQLITE_PRIVATE void sqlite3WalDb(Wal *pWal, sqlite3 *db){
62290 pWal->db = db;
62291}
62292
62293/*
62294** Take an exclusive WRITE lock. Blocking if so configured.
62295*/
62296static int walLockWriter(Wal *pWal){
62297 int rc;
62298 walEnableBlocking(pWal);
62299 rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1);
62300 walDisableBlocking(pWal);
62301 return rc;
62302}
62303#else
62304# define walEnableBlocking(x) 0
62305# define walDisableBlocking(x)
62306# define walLockWriter(pWal) walLockExclusive((pWal), WAL_WRITE_LOCK, 1)
62307# define sqlite3WalDb(pWal, db)
62308#endif /* ifdef SQLITE_ENABLE_SETLK_TIMEOUT */
62309
62310
62311/*
62312** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and
62313** n. If the attempt fails and parameter xBusy is not NULL, then it is a
62314** busy-handler function. Invoke it and retry the lock until either the
62315** lock is successfully obtained or the busy-handler returns 0.
62316*/
62317static int walBusyLock(
62318 Wal *pWal, /* WAL connection */
62319 int (*xBusy)(void*), /* Function to call when busy */
62320 void *pBusyArg, /* Context argument for xBusyHandler */
62321 int lockIdx, /* Offset of first byte to lock */
62322 int n /* Number of bytes to lock */
62323){
62324 int rc;
62325 do {
62326 rc = walLockExclusive(pWal, lockIdx, n);
62327 }while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) );
62328#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
62329 if( rc==SQLITE_BUSY_TIMEOUT ){
62330 walDisableBlocking(pWal);
62331 rc = SQLITE_BUSY;
62332 }
62333#endif
62334 return rc;
62335}
62336
62337/*
62338** The cache of the wal-index header must be valid to call this function.
62339** Return the page-size in bytes used by the database.
62340*/
62341static int walPagesize(Wal *pWal){
62342 return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
62343}
62344
62345/*
62346** The following is guaranteed when this function is called:
62347**
62348** a) the WRITER lock is held,
62349** b) the entire log file has been checkpointed, and
62350** c) any existing readers are reading exclusively from the database
62351** file - there are no readers that may attempt to read a frame from
62352** the log file.
62353**
62354** This function updates the shared-memory structures so that the next
62355** client to write to the database (which may be this one) does so by
62356** writing frames into the start of the log file.
62357**
62358** The value of parameter salt1 is used as the aSalt[1] value in the
62359** new wal-index header. It should be passed a pseudo-random value (i.e.
62360** one obtained from sqlite3_randomness()).
62361*/
62362static void walRestartHdr(Wal *pWal, u32 salt1){
62363 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
62364 int i; /* Loop counter */
62365 u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */
62366 pWal->nCkpt++;
62367 pWal->hdr.mxFrame = 0;
62368 sqlite3Put4byte(p: (u8*)&aSalt[0], v: 1 + sqlite3Get4byte(p: (u8*)&aSalt[0]));
62369 memcpy(dest: &pWal->hdr.aSalt[1], src: &salt1, n: 4);
62370 walIndexWriteHdr(pWal);
62371 AtomicStore(&pInfo->nBackfill, 0);
62372 pInfo->nBackfillAttempted = 0;
62373 pInfo->aReadMark[1] = 0;
62374 for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
62375 assert( pInfo->aReadMark[0]==0 );
62376}
62377
62378/*
62379** Copy as much content as we can from the WAL back into the database file
62380** in response to an sqlite3_wal_checkpoint() request or the equivalent.
62381**
62382** The amount of information copies from WAL to database might be limited
62383** by active readers. This routine will never overwrite a database page
62384** that a concurrent reader might be using.
62385**
62386** All I/O barrier operations (a.k.a fsyncs) occur in this routine when
62387** SQLite is in WAL-mode in synchronous=NORMAL. That means that if
62388** checkpoints are always run by a background thread or background
62389** process, foreground threads will never block on a lengthy fsync call.
62390**
62391** Fsync is called on the WAL before writing content out of the WAL and
62392** into the database. This ensures that if the new content is persistent
62393** in the WAL and can be recovered following a power-loss or hard reset.
62394**
62395** Fsync is also called on the database file if (and only if) the entire
62396** WAL content is copied into the database file. This second fsync makes
62397** it safe to delete the WAL since the new content will persist in the
62398** database file.
62399**
62400** This routine uses and updates the nBackfill field of the wal-index header.
62401** This is the only routine that will increase the value of nBackfill.
62402** (A WAL reset or recovery will revert nBackfill to zero, but not increase
62403** its value.)
62404**
62405** The caller must be holding sufficient locks to ensure that no other
62406** checkpoint is running (in any other thread or process) at the same
62407** time.
62408*/
62409static int walCheckpoint(
62410 Wal *pWal, /* Wal connection */
62411 sqlite3 *db, /* Check for interrupts on this handle */
62412 int eMode, /* One of PASSIVE, FULL or RESTART */
62413 int (*xBusy)(void*), /* Function to call when busy */
62414 void *pBusyArg, /* Context argument for xBusyHandler */
62415 int sync_flags, /* Flags for OsSync() (or 0) */
62416 u8 *zBuf /* Temporary buffer to use */
62417){
62418 int rc = SQLITE_OK; /* Return code */
62419 int szPage; /* Database page-size */
62420 WalIterator *pIter = 0; /* Wal iterator context */
62421 u32 iDbpage = 0; /* Next database page to write */
62422 u32 iFrame = 0; /* Wal frame containing data for iDbpage */
62423 u32 mxSafeFrame; /* Max frame that can be backfilled */
62424 u32 mxPage; /* Max database page to write */
62425 int i; /* Loop counter */
62426 volatile WalCkptInfo *pInfo; /* The checkpoint status information */
62427
62428 szPage = walPagesize(pWal);
62429 testcase( szPage<=32768 );
62430 testcase( szPage>=65536 );
62431 pInfo = walCkptInfo(pWal);
62432 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
62433
62434 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
62435 ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
62436 assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 );
62437
62438 /* Compute in mxSafeFrame the index of the last frame of the WAL that is
62439 ** safe to write into the database. Frames beyond mxSafeFrame might
62440 ** overwrite database pages that are in use by active readers and thus
62441 ** cannot be backfilled from the WAL.
62442 */
62443 mxSafeFrame = pWal->hdr.mxFrame;
62444 mxPage = pWal->hdr.nPage;
62445 for(i=1; i<WAL_NREADER; i++){
62446 u32 y = AtomicLoad(pInfo->aReadMark+i);
62447 if( mxSafeFrame>y ){
62448 assert( y<=pWal->hdr.mxFrame );
62449 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), n: 1);
62450 if( rc==SQLITE_OK ){
62451 u32 iMark = (i==1 ? mxSafeFrame : READMARK_NOT_USED);
62452 AtomicStore(pInfo->aReadMark+i, iMark);
62453 walUnlockExclusive(pWal, WAL_READ_LOCK(i), n: 1);
62454 }else if( rc==SQLITE_BUSY ){
62455 mxSafeFrame = y;
62456 xBusy = 0;
62457 }else{
62458 goto walcheckpoint_out;
62459 }
62460 }
62461 }
62462
62463 /* Allocate the iterator */
62464 if( pInfo->nBackfill<mxSafeFrame ){
62465 rc = walIteratorInit(pWal, nBackfill: pInfo->nBackfill, pp: &pIter);
62466 assert( rc==SQLITE_OK || pIter==0 );
62467 }
62468
62469 if( pIter
62470 && (rc = walBusyLock(pWal,xBusy,pBusyArg,WAL_READ_LOCK(0),n: 1))==SQLITE_OK
62471 ){
62472 u32 nBackfill = pInfo->nBackfill;
62473
62474 pInfo->nBackfillAttempted = mxSafeFrame;
62475
62476 /* Sync the WAL to disk */
62477 rc = sqlite3OsSync(id: pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
62478
62479 /* If the database may grow as a result of this checkpoint, hint
62480 ** about the eventual size of the db file to the VFS layer.
62481 */
62482 if( rc==SQLITE_OK ){
62483 i64 nReq = ((i64)mxPage * szPage);
62484 i64 nSize; /* Current size of database file */
62485 sqlite3OsFileControl(id: pWal->pDbFd, SQLITE_FCNTL_CKPT_START, pArg: 0);
62486 rc = sqlite3OsFileSize(id: pWal->pDbFd, pSize: &nSize);
62487 if( rc==SQLITE_OK && nSize<nReq ){
62488 if( (nSize+65536+(i64)pWal->hdr.mxFrame*szPage)<nReq ){
62489 /* If the size of the final database is larger than the current
62490 ** database plus the amount of data in the wal file, plus the
62491 ** maximum size of the pending-byte page (65536 bytes), then
62492 ** must be corruption somewhere. */
62493 rc = SQLITE_CORRUPT_BKPT;
62494 }else{
62495 sqlite3OsFileControlHint(id: pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT,pArg: &nReq);
62496 }
62497 }
62498
62499 }
62500
62501 /* Iterate through the contents of the WAL, copying data to the db file */
62502 while( rc==SQLITE_OK && 0==walIteratorNext(p: pIter, piPage: &iDbpage, piFrame: &iFrame) ){
62503 i64 iOffset;
62504 assert( walFramePgno(pWal, iFrame)==iDbpage );
62505 if( AtomicLoad(&db->u1.isInterrupted) ){
62506 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
62507 break;
62508 }
62509 if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ){
62510 continue;
62511 }
62512 iOffset = walFrameOffset(iFrame, szPage) + WAL_FRAME_HDRSIZE;
62513 /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */
62514 rc = sqlite3OsRead(id: pWal->pWalFd, pBuf: zBuf, amt: szPage, offset: iOffset);
62515 if( rc!=SQLITE_OK ) break;
62516 iOffset = (iDbpage-1)*(i64)szPage;
62517 testcase( IS_BIG_INT(iOffset) );
62518 rc = sqlite3OsWrite(id: pWal->pDbFd, pBuf: zBuf, amt: szPage, offset: iOffset);
62519 if( rc!=SQLITE_OK ) break;
62520 }
62521 sqlite3OsFileControl(id: pWal->pDbFd, SQLITE_FCNTL_CKPT_DONE, pArg: 0);
62522
62523 /* If work was actually accomplished... */
62524 if( rc==SQLITE_OK ){
62525 if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
62526 i64 szDb = pWal->hdr.nPage*(i64)szPage;
62527 testcase( IS_BIG_INT(szDb) );
62528 rc = sqlite3OsTruncate(id: pWal->pDbFd, size: szDb);
62529 if( rc==SQLITE_OK ){
62530 rc = sqlite3OsSync(id: pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags));
62531 }
62532 }
62533 if( rc==SQLITE_OK ){
62534 AtomicStore(&pInfo->nBackfill, mxSafeFrame);
62535 }
62536 }
62537
62538 /* Release the reader lock held while backfilling */
62539 walUnlockExclusive(pWal, WAL_READ_LOCK(0), n: 1);
62540 }
62541
62542 if( rc==SQLITE_BUSY ){
62543 /* Reset the return code so as not to report a checkpoint failure
62544 ** just because there are active readers. */
62545 rc = SQLITE_OK;
62546 }
62547 }
62548
62549 /* If this is an SQLITE_CHECKPOINT_RESTART or TRUNCATE operation, and the
62550 ** entire wal file has been copied into the database file, then block
62551 ** until all readers have finished using the wal file. This ensures that
62552 ** the next process to write to the database restarts the wal file.
62553 */
62554 if( rc==SQLITE_OK && eMode!=SQLITE_CHECKPOINT_PASSIVE ){
62555 assert( pWal->writeLock );
62556 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
62557 rc = SQLITE_BUSY;
62558 }else if( eMode>=SQLITE_CHECKPOINT_RESTART ){
62559 u32 salt1;
62560 sqlite3_randomness(N: 4, pBuf: &salt1);
62561 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
62562 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
62563 if( rc==SQLITE_OK ){
62564 if( eMode==SQLITE_CHECKPOINT_TRUNCATE ){
62565 /* IMPLEMENTATION-OF: R-44699-57140 This mode works the same way as
62566 ** SQLITE_CHECKPOINT_RESTART with the addition that it also
62567 ** truncates the log file to zero bytes just prior to a
62568 ** successful return.
62569 **
62570 ** In theory, it might be safe to do this without updating the
62571 ** wal-index header in shared memory, as all subsequent reader or
62572 ** writer clients should see that the entire log file has been
62573 ** checkpointed and behave accordingly. This seems unsafe though,
62574 ** as it would leave the system in a state where the contents of
62575 ** the wal-index header do not match the contents of the
62576 ** file-system. To avoid this, update the wal-index header to
62577 ** indicate that the log file contains zero valid frames. */
62578 walRestartHdr(pWal, salt1);
62579 rc = sqlite3OsTruncate(id: pWal->pWalFd, size: 0);
62580 }
62581 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
62582 }
62583 }
62584 }
62585
62586 walcheckpoint_out:
62587 walIteratorFree(p: pIter);
62588 return rc;
62589}
62590
62591/*
62592** If the WAL file is currently larger than nMax bytes in size, truncate
62593** it to exactly nMax bytes. If an error occurs while doing so, ignore it.
62594*/
62595static void walLimitSize(Wal *pWal, i64 nMax){
62596 i64 sz;
62597 int rx;
62598 sqlite3BeginBenignMalloc();
62599 rx = sqlite3OsFileSize(id: pWal->pWalFd, pSize: &sz);
62600 if( rx==SQLITE_OK && (sz > nMax ) ){
62601 rx = sqlite3OsTruncate(id: pWal->pWalFd, size: nMax);
62602 }
62603 sqlite3EndBenignMalloc();
62604 if( rx ){
62605 sqlite3_log(iErrCode: rx, zFormat: "cannot limit WAL size: %s", pWal->zWalName);
62606 }
62607}
62608
62609/*
62610** Close a connection to a log file.
62611*/
62612SQLITE_PRIVATE int sqlite3WalClose(
62613 Wal *pWal, /* Wal to close */
62614 sqlite3 *db, /* For interrupt flag */
62615 int sync_flags, /* Flags to pass to OsSync() (or 0) */
62616 int nBuf,
62617 u8 *zBuf /* Buffer of at least nBuf bytes */
62618){
62619 int rc = SQLITE_OK;
62620 if( pWal ){
62621 int isDelete = 0; /* True to unlink wal and wal-index files */
62622
62623 /* If an EXCLUSIVE lock can be obtained on the database file (using the
62624 ** ordinary, rollback-mode locking methods, this guarantees that the
62625 ** connection associated with this log file is the only connection to
62626 ** the database. In this case checkpoint the database and unlink both
62627 ** the wal and wal-index files.
62628 **
62629 ** The EXCLUSIVE lock is not released before returning.
62630 */
62631 if( zBuf!=0
62632 && SQLITE_OK==(rc = sqlite3OsLock(id: pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE))
62633 ){
62634 if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
62635 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
62636 }
62637 rc = sqlite3WalCheckpoint(pWal, db,
62638 SQLITE_CHECKPOINT_PASSIVE, xBusy: 0, pBusyArg: 0, sync_flags, nBuf, zBuf, pnLog: 0, pnCkpt: 0
62639 );
62640 if( rc==SQLITE_OK ){
62641 int bPersist = -1;
62642 sqlite3OsFileControlHint(
62643 id: pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, pArg: &bPersist
62644 );
62645 if( bPersist!=1 ){
62646 /* Try to delete the WAL file if the checkpoint completed and
62647 ** fsyned (rc==SQLITE_OK) and if we are not in persistent-wal
62648 ** mode (!bPersist) */
62649 isDelete = 1;
62650 }else if( pWal->mxWalSize>=0 ){
62651 /* Try to truncate the WAL file to zero bytes if the checkpoint
62652 ** completed and fsynced (rc==SQLITE_OK) and we are in persistent
62653 ** WAL mode (bPersist) and if the PRAGMA journal_size_limit is a
62654 ** non-negative value (pWal->mxWalSize>=0). Note that we truncate
62655 ** to zero bytes as truncating to the journal_size_limit might
62656 ** leave a corrupt WAL file on disk. */
62657 walLimitSize(pWal, nMax: 0);
62658 }
62659 }
62660 }
62661
62662 walIndexClose(pWal, isDelete);
62663 sqlite3OsClose(pId: pWal->pWalFd);
62664 if( isDelete ){
62665 sqlite3BeginBenignMalloc();
62666 sqlite3OsDelete(pVfs: pWal->pVfs, zPath: pWal->zWalName, dirSync: 0);
62667 sqlite3EndBenignMalloc();
62668 }
62669 WALTRACE(("WAL%p: closed\n", pWal));
62670 sqlite3_free(p: (void *)pWal->apWiData);
62671 sqlite3_free(p: pWal);
62672 }
62673 return rc;
62674}
62675
62676/*
62677** Try to read the wal-index header. Return 0 on success and 1 if
62678** there is a problem.
62679**
62680** The wal-index is in shared memory. Another thread or process might
62681** be writing the header at the same time this procedure is trying to
62682** read it, which might result in inconsistency. A dirty read is detected
62683** by verifying that both copies of the header are the same and also by
62684** a checksum on the header.
62685**
62686** If and only if the read is consistent and the header is different from
62687** pWal->hdr, then pWal->hdr is updated to the content of the new header
62688** and *pChanged is set to 1.
62689**
62690** If the checksum cannot be verified return non-zero. If the header
62691** is read successfully and the checksum verified, return zero.
62692*/
62693static SQLITE_NO_TSAN int walIndexTryHdr(Wal *pWal, int *pChanged){
62694 u32 aCksum[2]; /* Checksum on the header content */
62695 WalIndexHdr h1, h2; /* Two copies of the header content */
62696 WalIndexHdr volatile *aHdr; /* Header in shared memory */
62697
62698 /* The first page of the wal-index must be mapped at this point. */
62699 assert( pWal->nWiData>0 && pWal->apWiData[0] );
62700
62701 /* Read the header. This might happen concurrently with a write to the
62702 ** same area of shared memory on a different CPU in a SMP,
62703 ** meaning it is possible that an inconsistent snapshot is read
62704 ** from the file. If this happens, return non-zero.
62705 **
62706 ** tag-20200519-1:
62707 ** There are two copies of the header at the beginning of the wal-index.
62708 ** When reading, read [0] first then [1]. Writes are in the reverse order.
62709 ** Memory barriers are used to prevent the compiler or the hardware from
62710 ** reordering the reads and writes. TSAN and similar tools can sometimes
62711 ** give false-positive warnings about these accesses because the tools do not
62712 ** account for the double-read and the memory barrier. The use of mutexes
62713 ** here would be problematic as the memory being accessed is potentially
62714 ** shared among multiple processes and not all mutex implementions work
62715 ** reliably in that environment.
62716 */
62717 aHdr = walIndexHdr(pWal);
62718 memcpy(dest: &h1, src: (void *)&aHdr[0], n: sizeof(h1)); /* Possible TSAN false-positive */
62719 walShmBarrier(pWal);
62720 memcpy(dest: &h2, src: (void *)&aHdr[1], n: sizeof(h2));
62721
62722 if( memcmp(s1: &h1, s2: &h2, n: sizeof(h1))!=0 ){
62723 return 1; /* Dirty read */
62724 }
62725 if( h1.isInit==0 ){
62726 return 1; /* Malformed header - probably all zeros */
62727 }
62728 walChecksumBytes(nativeCksum: 1, a: (u8*)&h1, nByte: sizeof(h1)-sizeof(h1.aCksum), aIn: 0, aOut: aCksum);
62729 if( aCksum[0]!=h1.aCksum[0] || aCksum[1]!=h1.aCksum[1] ){
62730 return 1; /* Checksum does not match */
62731 }
62732
62733 if( memcmp(s1: &pWal->hdr, s2: &h1, n: sizeof(WalIndexHdr)) ){
62734 *pChanged = 1;
62735 memcpy(dest: &pWal->hdr, src: &h1, n: sizeof(WalIndexHdr));
62736 pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
62737 testcase( pWal->szPage<=32768 );
62738 testcase( pWal->szPage>=65536 );
62739 }
62740
62741 /* The header was successfully read. Return zero. */
62742 return 0;
62743}
62744
62745/*
62746** This is the value that walTryBeginRead returns when it needs to
62747** be retried.
62748*/
62749#define WAL_RETRY (-1)
62750
62751/*
62752** Read the wal-index header from the wal-index and into pWal->hdr.
62753** If the wal-header appears to be corrupt, try to reconstruct the
62754** wal-index from the WAL before returning.
62755**
62756** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
62757** changed by this operation. If pWal->hdr is unchanged, set *pChanged
62758** to 0.
62759**
62760** If the wal-index header is successfully read, return SQLITE_OK.
62761** Otherwise an SQLite error code.
62762*/
62763static int walIndexReadHdr(Wal *pWal, int *pChanged){
62764 int rc; /* Return code */
62765 int badHdr; /* True if a header read failed */
62766 volatile u32 *page0; /* Chunk of wal-index containing header */
62767
62768 /* Ensure that page 0 of the wal-index (the page that contains the
62769 ** wal-index header) is mapped. Return early if an error occurs here.
62770 */
62771 assert( pChanged );
62772 rc = walIndexPage(pWal, iPage: 0, ppPage: &page0);
62773 if( rc!=SQLITE_OK ){
62774 assert( rc!=SQLITE_READONLY ); /* READONLY changed to OK in walIndexPage */
62775 if( rc==SQLITE_READONLY_CANTINIT ){
62776 /* The SQLITE_READONLY_CANTINIT return means that the shared-memory
62777 ** was openable but is not writable, and this thread is unable to
62778 ** confirm that another write-capable connection has the shared-memory
62779 ** open, and hence the content of the shared-memory is unreliable,
62780 ** since the shared-memory might be inconsistent with the WAL file
62781 ** and there is no writer on hand to fix it. */
62782 assert( page0==0 );
62783 assert( pWal->writeLock==0 );
62784 assert( pWal->readOnly & WAL_SHM_RDONLY );
62785 pWal->bShmUnreliable = 1;
62786 pWal->exclusiveMode = WAL_HEAPMEMORY_MODE;
62787 *pChanged = 1;
62788 }else{
62789 return rc; /* Any other non-OK return is just an error */
62790 }
62791 }else{
62792 /* page0 can be NULL if the SHM is zero bytes in size and pWal->writeLock
62793 ** is zero, which prevents the SHM from growing */
62794 testcase( page0!=0 );
62795 }
62796 assert( page0!=0 || pWal->writeLock==0 );
62797
62798 /* If the first page of the wal-index has been mapped, try to read the
62799 ** wal-index header immediately, without holding any lock. This usually
62800 ** works, but may fail if the wal-index header is corrupt or currently
62801 ** being modified by another thread or process.
62802 */
62803 badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);
62804
62805 /* If the first attempt failed, it might have been due to a race
62806 ** with a writer. So get a WRITE lock and try again.
62807 */
62808 if( badHdr ){
62809 if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY) ){
62810 if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){
62811 walUnlockShared(pWal, WAL_WRITE_LOCK);
62812 rc = SQLITE_READONLY_RECOVERY;
62813 }
62814 }else{
62815 int bWriteLock = pWal->writeLock;
62816 if( bWriteLock || SQLITE_OK==(rc = walLockWriter(pWal)) ){
62817 pWal->writeLock = 1;
62818 if( SQLITE_OK==(rc = walIndexPage(pWal, iPage: 0, ppPage: &page0)) ){
62819 badHdr = walIndexTryHdr(pWal, pChanged);
62820 if( badHdr ){
62821 /* If the wal-index header is still malformed even while holding
62822 ** a WRITE lock, it can only mean that the header is corrupted and
62823 ** needs to be reconstructed. So run recovery to do exactly that.
62824 */
62825 rc = walIndexRecover(pWal);
62826 *pChanged = 1;
62827 }
62828 }
62829 if( bWriteLock==0 ){
62830 pWal->writeLock = 0;
62831 walUnlockExclusive(pWal, WAL_WRITE_LOCK, n: 1);
62832 }
62833 }
62834 }
62835 }
62836
62837 /* If the header is read successfully, check the version number to make
62838 ** sure the wal-index was not constructed with some future format that
62839 ** this version of SQLite cannot understand.
62840 */
62841 if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
62842 rc = SQLITE_CANTOPEN_BKPT;
62843 }
62844 if( pWal->bShmUnreliable ){
62845 if( rc!=SQLITE_OK ){
62846 walIndexClose(pWal, isDelete: 0);
62847 pWal->bShmUnreliable = 0;
62848 assert( pWal->nWiData>0 && pWal->apWiData[0]==0 );
62849 /* walIndexRecover() might have returned SHORT_READ if a concurrent
62850 ** writer truncated the WAL out from under it. If that happens, it
62851 ** indicates that a writer has fixed the SHM file for us, so retry */
62852 if( rc==SQLITE_IOERR_SHORT_READ ) rc = WAL_RETRY;
62853 }
62854 pWal->exclusiveMode = WAL_NORMAL_MODE;
62855 }
62856
62857 return rc;
62858}
62859
62860/*
62861** Open a transaction in a connection where the shared-memory is read-only
62862** and where we cannot verify that there is a separate write-capable connection
62863** on hand to keep the shared-memory up-to-date with the WAL file.
62864**
62865** This can happen, for example, when the shared-memory is implemented by
62866** memory-mapping a *-shm file, where a prior writer has shut down and
62867** left the *-shm file on disk, and now the present connection is trying
62868** to use that database but lacks write permission on the *-shm file.
62869** Other scenarios are also possible, depending on the VFS implementation.
62870**
62871** Precondition:
62872**
62873** The *-wal file has been read and an appropriate wal-index has been
62874** constructed in pWal->apWiData[] using heap memory instead of shared
62875** memory.
62876**
62877** If this function returns SQLITE_OK, then the read transaction has
62878** been successfully opened. In this case output variable (*pChanged)
62879** is set to true before returning if the caller should discard the
62880** contents of the page cache before proceeding. Or, if it returns
62881** WAL_RETRY, then the heap memory wal-index has been discarded and
62882** the caller should retry opening the read transaction from the
62883** beginning (including attempting to map the *-shm file).
62884**
62885** If an error occurs, an SQLite error code is returned.
62886*/
62887static int walBeginShmUnreliable(Wal *pWal, int *pChanged){
62888 i64 szWal; /* Size of wal file on disk in bytes */
62889 i64 iOffset; /* Current offset when reading wal file */
62890 u8 aBuf[WAL_HDRSIZE]; /* Buffer to load WAL header into */
62891 u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */
62892 int szFrame; /* Number of bytes in buffer aFrame[] */
62893 u8 *aData; /* Pointer to data part of aFrame buffer */
62894 volatile void *pDummy; /* Dummy argument for xShmMap */
62895 int rc; /* Return code */
62896 u32 aSaveCksum[2]; /* Saved copy of pWal->hdr.aFrameCksum */
62897
62898 assert( pWal->bShmUnreliable );
62899 assert( pWal->readOnly & WAL_SHM_RDONLY );
62900 assert( pWal->nWiData>0 && pWal->apWiData[0] );
62901
62902 /* Take WAL_READ_LOCK(0). This has the effect of preventing any
62903 ** writers from running a checkpoint, but does not stop them
62904 ** from running recovery. */
62905 rc = walLockShared(pWal, WAL_READ_LOCK(0));
62906 if( rc!=SQLITE_OK ){
62907 if( rc==SQLITE_BUSY ) rc = WAL_RETRY;
62908 goto begin_unreliable_shm_out;
62909 }
62910 pWal->readLock = 0;
62911
62912 /* Check to see if a separate writer has attached to the shared-memory area,
62913 ** thus making the shared-memory "reliable" again. Do this by invoking
62914 ** the xShmMap() routine of the VFS and looking to see if the return
62915 ** is SQLITE_READONLY instead of SQLITE_READONLY_CANTINIT.
62916 **
62917 ** If the shared-memory is now "reliable" return WAL_RETRY, which will
62918 ** cause the heap-memory WAL-index to be discarded and the actual
62919 ** shared memory to be used in its place.
62920 **
62921 ** This step is important because, even though this connection is holding
62922 ** the WAL_READ_LOCK(0) which prevents a checkpoint, a writer might
62923 ** have already checkpointed the WAL file and, while the current
62924 ** is active, wrap the WAL and start overwriting frames that this
62925 ** process wants to use.
62926 **
62927 ** Once sqlite3OsShmMap() has been called for an sqlite3_file and has
62928 ** returned any SQLITE_READONLY value, it must return only SQLITE_READONLY
62929 ** or SQLITE_READONLY_CANTINIT or some error for all subsequent invocations,
62930 ** even if some external agent does a "chmod" to make the shared-memory
62931 ** writable by us, until sqlite3OsShmUnmap() has been called.
62932 ** This is a requirement on the VFS implementation.
62933 */
62934 rc = sqlite3OsShmMap(id: pWal->pDbFd, iPage: 0, WALINDEX_PGSZ, bExtend: 0, pp: &pDummy);
62935 assert( rc!=SQLITE_OK ); /* SQLITE_OK not possible for read-only connection */
62936 if( rc!=SQLITE_READONLY_CANTINIT ){
62937 rc = (rc==SQLITE_READONLY ? WAL_RETRY : rc);
62938 goto begin_unreliable_shm_out;
62939 }
62940
62941 /* We reach this point only if the real shared-memory is still unreliable.
62942 ** Assume the in-memory WAL-index substitute is correct and load it
62943 ** into pWal->hdr.
62944 */
62945 memcpy(dest: &pWal->hdr, src: (void*)walIndexHdr(pWal), n: sizeof(WalIndexHdr));
62946
62947 /* Make sure some writer hasn't come in and changed the WAL file out
62948 ** from under us, then disconnected, while we were not looking.
62949 */
62950 rc = sqlite3OsFileSize(id: pWal->pWalFd, pSize: &szWal);
62951 if( rc!=SQLITE_OK ){
62952 goto begin_unreliable_shm_out;
62953 }
62954 if( szWal<WAL_HDRSIZE ){
62955 /* If the wal file is too small to contain a wal-header and the
62956 ** wal-index header has mxFrame==0, then it must be safe to proceed
62957 ** reading the database file only. However, the page cache cannot
62958 ** be trusted, as a read/write connection may have connected, written
62959 ** the db, run a checkpoint, truncated the wal file and disconnected
62960 ** since this client's last read transaction. */
62961 *pChanged = 1;
62962 rc = (pWal->hdr.mxFrame==0 ? SQLITE_OK : WAL_RETRY);
62963 goto begin_unreliable_shm_out;
62964 }
62965
62966 /* Check the salt keys at the start of the wal file still match. */
62967 rc = sqlite3OsRead(id: pWal->pWalFd, pBuf: aBuf, WAL_HDRSIZE, offset: 0);
62968 if( rc!=SQLITE_OK ){
62969 goto begin_unreliable_shm_out;
62970 }
62971 if( memcmp(s1: &pWal->hdr.aSalt, s2: &aBuf[16], n: 8) ){
62972 /* Some writer has wrapped the WAL file while we were not looking.
62973 ** Return WAL_RETRY which will cause the in-memory WAL-index to be
62974 ** rebuilt. */
62975 rc = WAL_RETRY;
62976 goto begin_unreliable_shm_out;
62977 }
62978
62979 /* Allocate a buffer to read frames into */
62980 szFrame = pWal->hdr.szPage + WAL_FRAME_HDRSIZE;
62981 aFrame = (u8 *)sqlite3_malloc64(n: szFrame);
62982 if( aFrame==0 ){
62983 rc = SQLITE_NOMEM_BKPT;
62984 goto begin_unreliable_shm_out;
62985 }
62986 aData = &aFrame[WAL_FRAME_HDRSIZE];
62987
62988 /* Check to see if a complete transaction has been appended to the
62989 ** wal file since the heap-memory wal-index was created. If so, the
62990 ** heap-memory wal-index is discarded and WAL_RETRY returned to
62991 ** the caller. */
62992 aSaveCksum[0] = pWal->hdr.aFrameCksum[0];
62993 aSaveCksum[1] = pWal->hdr.aFrameCksum[1];
62994 for(iOffset=walFrameOffset(pWal->hdr.mxFrame+1, pWal->hdr.szPage);
62995 iOffset+szFrame<=szWal;
62996 iOffset+=szFrame
62997 ){
62998 u32 pgno; /* Database page number for frame */
62999 u32 nTruncate; /* dbsize field from frame header */
63000
63001 /* Read and decode the next log frame. */
63002 rc = sqlite3OsRead(id: pWal->pWalFd, pBuf: aFrame, amt: szFrame, offset: iOffset);
63003 if( rc!=SQLITE_OK ) break;
63004 if( !walDecodeFrame(pWal, piPage: &pgno, pnTruncate: &nTruncate, aData, aFrame) ) break;
63005
63006 /* If nTruncate is non-zero, then a complete transaction has been
63007 ** appended to this wal file. Set rc to WAL_RETRY and break out of
63008 ** the loop. */
63009 if( nTruncate ){
63010 rc = WAL_RETRY;
63011 break;
63012 }
63013 }
63014 pWal->hdr.aFrameCksum[0] = aSaveCksum[0];
63015 pWal->hdr.aFrameCksum[1] = aSaveCksum[1];
63016
63017 begin_unreliable_shm_out:
63018 sqlite3_free(p: aFrame);
63019 if( rc!=SQLITE_OK ){
63020 int i;
63021 for(i=0; i<pWal->nWiData; i++){
63022 sqlite3_free(p: (void*)pWal->apWiData[i]);
63023 pWal->apWiData[i] = 0;
63024 }
63025 pWal->bShmUnreliable = 0;
63026 sqlite3WalEndReadTransaction(pWal);
63027 *pChanged = 1;
63028 }
63029 return rc;
63030}
63031
63032/*
63033** Attempt to start a read transaction. This might fail due to a race or
63034** other transient condition. When that happens, it returns WAL_RETRY to
63035** indicate to the caller that it is safe to retry immediately.
63036**
63037** On success return SQLITE_OK. On a permanent failure (such an
63038** I/O error or an SQLITE_BUSY because another process is running
63039** recovery) return a positive error code.
63040**
63041** The useWal parameter is true to force the use of the WAL and disable
63042** the case where the WAL is bypassed because it has been completely
63043** checkpointed. If useWal==0 then this routine calls walIndexReadHdr()
63044** to make a copy of the wal-index header into pWal->hdr. If the
63045** wal-index header has changed, *pChanged is set to 1 (as an indication
63046** to the caller that the local page cache is obsolete and needs to be
63047** flushed.) When useWal==1, the wal-index header is assumed to already
63048** be loaded and the pChanged parameter is unused.
63049**
63050** The caller must set the cnt parameter to the number of prior calls to
63051** this routine during the current read attempt that returned WAL_RETRY.
63052** This routine will start taking more aggressive measures to clear the
63053** race conditions after multiple WAL_RETRY returns, and after an excessive
63054** number of errors will ultimately return SQLITE_PROTOCOL. The
63055** SQLITE_PROTOCOL return indicates that some other process has gone rogue
63056** and is not honoring the locking protocol. There is a vanishingly small
63057** chance that SQLITE_PROTOCOL could be returned because of a run of really
63058** bad luck when there is lots of contention for the wal-index, but that
63059** possibility is so small that it can be safely neglected, we believe.
63060**
63061** On success, this routine obtains a read lock on
63062** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is
63063** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1)
63064** that means the Wal does not hold any read lock. The reader must not
63065** access any database page that is modified by a WAL frame up to and
63066** including frame number aReadMark[pWal->readLock]. The reader will
63067** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
63068** Or if pWal->readLock==0, then the reader will ignore the WAL
63069** completely and get all content directly from the database file.
63070** If the useWal parameter is 1 then the WAL will never be ignored and
63071** this routine will always set pWal->readLock>0 on success.
63072** When the read transaction is completed, the caller must release the
63073** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
63074**
63075** This routine uses the nBackfill and aReadMark[] fields of the header
63076** to select a particular WAL_READ_LOCK() that strives to let the
63077** checkpoint process do as much work as possible. This routine might
63078** update values of the aReadMark[] array in the header, but if it does
63079** so it takes care to hold an exclusive lock on the corresponding
63080** WAL_READ_LOCK() while changing values.
63081*/
63082static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){
63083 volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */
63084 u32 mxReadMark; /* Largest aReadMark[] value */
63085 int mxI; /* Index of largest aReadMark[] value */
63086 int i; /* Loop counter */
63087 int rc = SQLITE_OK; /* Return code */
63088 u32 mxFrame; /* Wal frame to lock to */
63089
63090 assert( pWal->readLock<0 ); /* Not currently locked */
63091
63092 /* useWal may only be set for read/write connections */
63093 assert( (pWal->readOnly & WAL_SHM_RDONLY)==0 || useWal==0 );
63094
63095 /* Take steps to avoid spinning forever if there is a protocol error.
63096 **
63097 ** Circumstances that cause a RETRY should only last for the briefest
63098 ** instances of time. No I/O or other system calls are done while the
63099 ** locks are held, so the locks should not be held for very long. But
63100 ** if we are unlucky, another process that is holding a lock might get
63101 ** paged out or take a page-fault that is time-consuming to resolve,
63102 ** during the few nanoseconds that it is holding the lock. In that case,
63103 ** it might take longer than normal for the lock to free.
63104 **
63105 ** After 5 RETRYs, we begin calling sqlite3OsSleep(). The first few
63106 ** calls to sqlite3OsSleep() have a delay of 1 microsecond. Really this
63107 ** is more of a scheduler yield than an actual delay. But on the 10th
63108 ** an subsequent retries, the delays start becoming longer and longer,
63109 ** so that on the 100th (and last) RETRY we delay for 323 milliseconds.
63110 ** The total delay time before giving up is less than 10 seconds.
63111 */
63112 if( cnt>5 ){
63113 int nDelay = 1; /* Pause time in microseconds */
63114 if( cnt>100 ){
63115 VVA_ONLY( pWal->lockError = 1; )
63116 return SQLITE_PROTOCOL;
63117 }
63118 if( cnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39;
63119 sqlite3OsSleep(pVfs: pWal->pVfs, nMicro: nDelay);
63120 }
63121
63122 if( !useWal ){
63123 assert( rc==SQLITE_OK );
63124 if( pWal->bShmUnreliable==0 ){
63125 rc = walIndexReadHdr(pWal, pChanged);
63126 }
63127 if( rc==SQLITE_BUSY ){
63128 /* If there is not a recovery running in another thread or process
63129 ** then convert BUSY errors to WAL_RETRY. If recovery is known to
63130 ** be running, convert BUSY to BUSY_RECOVERY. There is a race here
63131 ** which might cause WAL_RETRY to be returned even if BUSY_RECOVERY
63132 ** would be technically correct. But the race is benign since with
63133 ** WAL_RETRY this routine will be called again and will probably be
63134 ** right on the second iteration.
63135 */
63136 if( pWal->apWiData[0]==0 ){
63137 /* This branch is taken when the xShmMap() method returns SQLITE_BUSY.
63138 ** We assume this is a transient condition, so return WAL_RETRY. The
63139 ** xShmMap() implementation used by the default unix and win32 VFS
63140 ** modules may return SQLITE_BUSY due to a race condition in the
63141 ** code that determines whether or not the shared-memory region
63142 ** must be zeroed before the requested page is returned.
63143 */
63144 rc = WAL_RETRY;
63145 }else if( SQLITE_OK==(rc = walLockShared(pWal, WAL_RECOVER_LOCK)) ){
63146 walUnlockShared(pWal, WAL_RECOVER_LOCK);
63147 rc = WAL_RETRY;
63148 }else if( rc==SQLITE_BUSY ){
63149 rc = SQLITE_BUSY_RECOVERY;
63150 }
63151 }
63152 if( rc!=SQLITE_OK ){
63153 return rc;
63154 }
63155 else if( pWal->bShmUnreliable ){
63156 return walBeginShmUnreliable(pWal, pChanged);
63157 }
63158 }
63159
63160 assert( pWal->nWiData>0 );
63161 assert( pWal->apWiData[0]!=0 );
63162 pInfo = walCkptInfo(pWal);
63163 if( !useWal && AtomicLoad(&pInfo->nBackfill)==pWal->hdr.mxFrame
63164#ifdef SQLITE_ENABLE_SNAPSHOT
63165 && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0)
63166#endif
63167 ){
63168 /* The WAL has been completely backfilled (or it is empty).
63169 ** and can be safely ignored.
63170 */
63171 rc = walLockShared(pWal, WAL_READ_LOCK(0));
63172 walShmBarrier(pWal);
63173 if( rc==SQLITE_OK ){
63174 if( memcmp(s1: (void *)walIndexHdr(pWal), s2: &pWal->hdr, n: sizeof(WalIndexHdr)) ){
63175 /* It is not safe to allow the reader to continue here if frames
63176 ** may have been appended to the log before READ_LOCK(0) was obtained.
63177 ** When holding READ_LOCK(0), the reader ignores the entire log file,
63178 ** which implies that the database file contains a trustworthy
63179 ** snapshot. Since holding READ_LOCK(0) prevents a checkpoint from
63180 ** happening, this is usually correct.
63181 **
63182 ** However, if frames have been appended to the log (or if the log
63183 ** is wrapped and written for that matter) before the READ_LOCK(0)
63184 ** is obtained, that is not necessarily true. A checkpointer may
63185 ** have started to backfill the appended frames but crashed before
63186 ** it finished. Leaving a corrupt image in the database file.
63187 */
63188 walUnlockShared(pWal, WAL_READ_LOCK(0));
63189 return WAL_RETRY;
63190 }
63191 pWal->readLock = 0;
63192 return SQLITE_OK;
63193 }else if( rc!=SQLITE_BUSY ){
63194 return rc;
63195 }
63196 }
63197
63198 /* If we get this far, it means that the reader will want to use
63199 ** the WAL to get at content from recent commits. The job now is
63200 ** to select one of the aReadMark[] entries that is closest to
63201 ** but not exceeding pWal->hdr.mxFrame and lock that entry.
63202 */
63203 mxReadMark = 0;
63204 mxI = 0;
63205 mxFrame = pWal->hdr.mxFrame;
63206#ifdef SQLITE_ENABLE_SNAPSHOT
63207 if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){
63208 mxFrame = pWal->pSnapshot->mxFrame;
63209 }
63210#endif
63211 for(i=1; i<WAL_NREADER; i++){
63212 u32 thisMark = AtomicLoad(pInfo->aReadMark+i);
63213 if( mxReadMark<=thisMark && thisMark<=mxFrame ){
63214 assert( thisMark!=READMARK_NOT_USED );
63215 mxReadMark = thisMark;
63216 mxI = i;
63217 }
63218 }
63219 if( (pWal->readOnly & WAL_SHM_RDONLY)==0
63220 && (mxReadMark<mxFrame || mxI==0)
63221 ){
63222 for(i=1; i<WAL_NREADER; i++){
63223 rc = walLockExclusive(pWal, WAL_READ_LOCK(i), n: 1);
63224 if( rc==SQLITE_OK ){
63225 AtomicStore(pInfo->aReadMark+i,mxFrame);
63226 mxReadMark = mxFrame;
63227 mxI = i;
63228 walUnlockExclusive(pWal, WAL_READ_LOCK(i), n: 1);
63229 break;
63230 }else if( rc!=SQLITE_BUSY ){
63231 return rc;
63232 }
63233 }
63234 }
63235 if( mxI==0 ){
63236 assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
63237 return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTINIT;
63238 }
63239
63240 rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
63241 if( rc ){
63242 return rc==SQLITE_BUSY ? WAL_RETRY : rc;
63243 }
63244 /* Now that the read-lock has been obtained, check that neither the
63245 ** value in the aReadMark[] array or the contents of the wal-index
63246 ** header have changed.
63247 **
63248 ** It is necessary to check that the wal-index header did not change
63249 ** between the time it was read and when the shared-lock was obtained
63250 ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
63251 ** that the log file may have been wrapped by a writer, or that frames
63252 ** that occur later in the log than pWal->hdr.mxFrame may have been
63253 ** copied into the database by a checkpointer. If either of these things
63254 ** happened, then reading the database with the current value of
63255 ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
63256 ** instead.
63257 **
63258 ** Before checking that the live wal-index header has not changed
63259 ** since it was read, set Wal.minFrame to the first frame in the wal
63260 ** file that has not yet been checkpointed. This client will not need
63261 ** to read any frames earlier than minFrame from the wal file - they
63262 ** can be safely read directly from the database file.
63263 **
63264 ** Because a ShmBarrier() call is made between taking the copy of
63265 ** nBackfill and checking that the wal-header in shared-memory still
63266 ** matches the one cached in pWal->hdr, it is guaranteed that the
63267 ** checkpointer that set nBackfill was not working with a wal-index
63268 ** header newer than that cached in pWal->hdr. If it were, that could
63269 ** cause a problem. The checkpointer could omit to checkpoint
63270 ** a version of page X that lies before pWal->minFrame (call that version
63271 ** A) on the basis that there is a newer version (version B) of the same
63272 ** page later in the wal file. But if version B happens to like past
63273 ** frame pWal->hdr.mxFrame - then the client would incorrectly assume
63274 ** that it can read version A from the database file. However, since
63275 ** we can guarantee that the checkpointer that set nBackfill could not
63276 ** see any pages past pWal->hdr.mxFrame, this problem does not come up.
63277 */
63278 pWal->minFrame = AtomicLoad(&pInfo->nBackfill)+1;
63279 walShmBarrier(pWal);
63280 if( AtomicLoad(pInfo->aReadMark+mxI)!=mxReadMark
63281 || memcmp(s1: (void *)walIndexHdr(pWal), s2: &pWal->hdr, n: sizeof(WalIndexHdr))
63282 ){
63283 walUnlockShared(pWal, WAL_READ_LOCK(mxI));
63284 return WAL_RETRY;
63285 }else{
63286 assert( mxReadMark<=pWal->hdr.mxFrame );
63287 pWal->readLock = (i16)mxI;
63288 }
63289 return rc;
63290}
63291
63292#ifdef SQLITE_ENABLE_SNAPSHOT
63293/*
63294** Attempt to reduce the value of the WalCkptInfo.nBackfillAttempted
63295** variable so that older snapshots can be accessed. To do this, loop
63296** through all wal frames from nBackfillAttempted to (nBackfill+1),
63297** comparing their content to the corresponding page with the database
63298** file, if any. Set nBackfillAttempted to the frame number of the
63299** first frame for which the wal file content matches the db file.
63300**
63301** This is only really safe if the file-system is such that any page
63302** writes made by earlier checkpointers were atomic operations, which
63303** is not always true. It is also possible that nBackfillAttempted
63304** may be left set to a value larger than expected, if a wal frame
63305** contains content that duplicate of an earlier version of the same
63306** page.
63307**
63308** SQLITE_OK is returned if successful, or an SQLite error code if an
63309** error occurs. It is not an error if nBackfillAttempted cannot be
63310** decreased at all.
63311*/
63312SQLITE_PRIVATE int sqlite3WalSnapshotRecover(Wal *pWal){
63313 int rc;
63314
63315 assert( pWal->readLock>=0 );
63316 rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
63317 if( rc==SQLITE_OK ){
63318 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
63319 int szPage = (int)pWal->szPage;
63320 i64 szDb; /* Size of db file in bytes */
63321
63322 rc = sqlite3OsFileSize(pWal->pDbFd, &szDb);
63323 if( rc==SQLITE_OK ){
63324 void *pBuf1 = sqlite3_malloc(szPage);
63325 void *pBuf2 = sqlite3_malloc(szPage);
63326 if( pBuf1==0 || pBuf2==0 ){
63327 rc = SQLITE_NOMEM;
63328 }else{
63329 u32 i = pInfo->nBackfillAttempted;
63330 for(i=pInfo->nBackfillAttempted; i>AtomicLoad(&pInfo->nBackfill); i--){
63331 WalHashLoc sLoc; /* Hash table location */
63332 u32 pgno; /* Page number in db file */
63333 i64 iDbOff; /* Offset of db file entry */
63334 i64 iWalOff; /* Offset of wal file entry */
63335
63336 rc = walHashGet(pWal, walFramePage(i), &sLoc);
63337 if( rc!=SQLITE_OK ) break;
63338 assert( i - sLoc.iZero - 1 >=0 );
63339 pgno = sLoc.aPgno[i-sLoc.iZero-1];
63340 iDbOff = (i64)(pgno-1) * szPage;
63341
63342 if( iDbOff+szPage<=szDb ){
63343 iWalOff = walFrameOffset(i, szPage) + WAL_FRAME_HDRSIZE;
63344 rc = sqlite3OsRead(pWal->pWalFd, pBuf1, szPage, iWalOff);
63345
63346 if( rc==SQLITE_OK ){
63347 rc = sqlite3OsRead(pWal->pDbFd, pBuf2, szPage, iDbOff);
63348 }
63349
63350 if( rc!=SQLITE_OK || 0==memcmp(pBuf1, pBuf2, szPage) ){
63351 break;
63352 }
63353 }
63354
63355 pInfo->nBackfillAttempted = i-1;
63356 }
63357 }
63358
63359 sqlite3_free(pBuf1);
63360 sqlite3_free(pBuf2);
63361 }
63362 walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1);
63363 }
63364
63365 return rc;
63366}
63367#endif /* SQLITE_ENABLE_SNAPSHOT */
63368
63369/*
63370** Begin a read transaction on the database.
63371**
63372** This routine used to be called sqlite3OpenSnapshot() and with good reason:
63373** it takes a snapshot of the state of the WAL and wal-index for the current
63374** instant in time. The current thread will continue to use this snapshot.
63375** Other threads might append new content to the WAL and wal-index but
63376** that extra content is ignored by the current thread.
63377**
63378** If the database contents have changes since the previous read
63379** transaction, then *pChanged is set to 1 before returning. The
63380** Pager layer will use this to know that its cache is stale and
63381** needs to be flushed.
63382*/
63383SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){
63384 int rc; /* Return code */
63385 int cnt = 0; /* Number of TryBeginRead attempts */
63386#ifdef SQLITE_ENABLE_SNAPSHOT
63387 int bChanged = 0;
63388 WalIndexHdr *pSnapshot = pWal->pSnapshot;
63389#endif
63390
63391 assert( pWal->ckptLock==0 );
63392
63393#ifdef SQLITE_ENABLE_SNAPSHOT
63394 if( pSnapshot ){
63395 if( memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
63396 bChanged = 1;
63397 }
63398
63399 /* It is possible that there is a checkpointer thread running
63400 ** concurrent with this code. If this is the case, it may be that the
63401 ** checkpointer has already determined that it will checkpoint
63402 ** snapshot X, where X is later in the wal file than pSnapshot, but
63403 ** has not yet set the pInfo->nBackfillAttempted variable to indicate
63404 ** its intent. To avoid the race condition this leads to, ensure that
63405 ** there is no checkpointer process by taking a shared CKPT lock
63406 ** before checking pInfo->nBackfillAttempted. */
63407 (void)walEnableBlocking(pWal);
63408 rc = walLockShared(pWal, WAL_CKPT_LOCK);
63409 walDisableBlocking(pWal);
63410
63411 if( rc!=SQLITE_OK ){
63412 return rc;
63413 }
63414 pWal->ckptLock = 1;
63415 }
63416#endif
63417
63418 do{
63419 rc = walTryBeginRead(pWal, pChanged, useWal: 0, cnt: ++cnt);
63420 }while( rc==WAL_RETRY );
63421 testcase( (rc&0xff)==SQLITE_BUSY );
63422 testcase( (rc&0xff)==SQLITE_IOERR );
63423 testcase( rc==SQLITE_PROTOCOL );
63424 testcase( rc==SQLITE_OK );
63425
63426#ifdef SQLITE_ENABLE_SNAPSHOT
63427 if( rc==SQLITE_OK ){
63428 if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
63429 /* At this point the client has a lock on an aReadMark[] slot holding
63430 ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr
63431 ** is populated with the wal-index header corresponding to the head
63432 ** of the wal file. Verify that pSnapshot is still valid before
63433 ** continuing. Reasons why pSnapshot might no longer be valid:
63434 **
63435 ** (1) The WAL file has been reset since the snapshot was taken.
63436 ** In this case, the salt will have changed.
63437 **
63438 ** (2) A checkpoint as been attempted that wrote frames past
63439 ** pSnapshot->mxFrame into the database file. Note that the
63440 ** checkpoint need not have completed for this to cause problems.
63441 */
63442 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
63443
63444 assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 );
63445 assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame );
63446
63447 /* Check that the wal file has not been wrapped. Assuming that it has
63448 ** not, also check that no checkpointer has attempted to checkpoint any
63449 ** frames beyond pSnapshot->mxFrame. If either of these conditions are
63450 ** true, return SQLITE_ERROR_SNAPSHOT. Otherwise, overwrite pWal->hdr
63451 ** with *pSnapshot and set *pChanged as appropriate for opening the
63452 ** snapshot. */
63453 if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
63454 && pSnapshot->mxFrame>=pInfo->nBackfillAttempted
63455 ){
63456 assert( pWal->readLock>0 );
63457 memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr));
63458 *pChanged = bChanged;
63459 }else{
63460 rc = SQLITE_ERROR_SNAPSHOT;
63461 }
63462
63463 /* A client using a non-current snapshot may not ignore any frames
63464 ** from the start of the wal file. This is because, for a system
63465 ** where (minFrame < iSnapshot < maxFrame), a checkpointer may
63466 ** have omitted to checkpoint a frame earlier than minFrame in
63467 ** the file because there exists a frame after iSnapshot that
63468 ** is the same database page. */
63469 pWal->minFrame = 1;
63470
63471 if( rc!=SQLITE_OK ){
63472 sqlite3WalEndReadTransaction(pWal);
63473 }
63474 }
63475 }
63476
63477 /* Release the shared CKPT lock obtained above. */
63478 if( pWal->ckptLock ){
63479 assert( pSnapshot );
63480 walUnlockShared(pWal, WAL_CKPT_LOCK);
63481 pWal->ckptLock = 0;
63482 }
63483#endif
63484 return rc;
63485}
63486
63487/*
63488** Finish with a read transaction. All this does is release the
63489** read-lock.
63490*/
63491SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal){
63492 sqlite3WalEndWriteTransaction(pWal);
63493 if( pWal->readLock>=0 ){
63494 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
63495 pWal->readLock = -1;
63496 }
63497}
63498
63499/*
63500** Search the wal file for page pgno. If found, set *piRead to the frame that
63501** contains the page. Otherwise, if pgno is not in the wal file, set *piRead
63502** to zero.
63503**
63504** Return SQLITE_OK if successful, or an error code if an error occurs. If an
63505** error does occur, the final value of *piRead is undefined.
63506*/
63507SQLITE_PRIVATE int sqlite3WalFindFrame(
63508 Wal *pWal, /* WAL handle */
63509 Pgno pgno, /* Database page number to read data for */
63510 u32 *piRead /* OUT: Frame number (or zero) */
63511){
63512 u32 iRead = 0; /* If !=0, WAL frame to return data from */
63513 u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */
63514 int iHash; /* Used to loop through N hash tables */
63515 int iMinHash;
63516
63517 /* This routine is only be called from within a read transaction. */
63518 assert( pWal->readLock>=0 || pWal->lockError );
63519
63520 /* If the "last page" field of the wal-index header snapshot is 0, then
63521 ** no data will be read from the wal under any circumstances. Return early
63522 ** in this case as an optimization. Likewise, if pWal->readLock==0,
63523 ** then the WAL is ignored by the reader so return early, as if the
63524 ** WAL were empty.
63525 */
63526 if( iLast==0 || (pWal->readLock==0 && pWal->bShmUnreliable==0) ){
63527 *piRead = 0;
63528 return SQLITE_OK;
63529 }
63530
63531 /* Search the hash table or tables for an entry matching page number
63532 ** pgno. Each iteration of the following for() loop searches one
63533 ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
63534 **
63535 ** This code might run concurrently to the code in walIndexAppend()
63536 ** that adds entries to the wal-index (and possibly to this hash
63537 ** table). This means the value just read from the hash
63538 ** slot (aHash[iKey]) may have been added before or after the
63539 ** current read transaction was opened. Values added after the
63540 ** read transaction was opened may have been written incorrectly -
63541 ** i.e. these slots may contain garbage data. However, we assume
63542 ** that any slots written before the current read transaction was
63543 ** opened remain unmodified.
63544 **
63545 ** For the reasons above, the if(...) condition featured in the inner
63546 ** loop of the following block is more stringent that would be required
63547 ** if we had exclusive access to the hash-table:
63548 **
63549 ** (aPgno[iFrame]==pgno):
63550 ** This condition filters out normal hash-table collisions.
63551 **
63552 ** (iFrame<=iLast):
63553 ** This condition filters out entries that were added to the hash
63554 ** table after the current read-transaction had started.
63555 */
63556 iMinHash = walFramePage(iFrame: pWal->minFrame);
63557 for(iHash=walFramePage(iFrame: iLast); iHash>=iMinHash; iHash--){
63558 WalHashLoc sLoc; /* Hash table location */
63559 int iKey; /* Hash slot index */
63560 int nCollide; /* Number of hash collisions remaining */
63561 int rc; /* Error code */
63562 u32 iH;
63563
63564 rc = walHashGet(pWal, iHash, pLoc: &sLoc);
63565 if( rc!=SQLITE_OK ){
63566 return rc;
63567 }
63568 nCollide = HASHTABLE_NSLOT;
63569 iKey = walHash(iPage: pgno);
63570 while( (iH = AtomicLoad(&sLoc.aHash[iKey]))!=0 ){
63571 u32 iFrame = iH + sLoc.iZero;
63572 if( iFrame<=iLast && iFrame>=pWal->minFrame && sLoc.aPgno[iH-1]==pgno ){
63573 assert( iFrame>iRead || CORRUPT_DB );
63574 iRead = iFrame;
63575 }
63576 if( (nCollide--)==0 ){
63577 return SQLITE_CORRUPT_BKPT;
63578 }
63579 iKey = walNextHash(iPriorHash: iKey);
63580 }
63581 if( iRead ) break;
63582 }
63583
63584#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
63585 /* If expensive assert() statements are available, do a linear search
63586 ** of the wal-index file content. Make sure the results agree with the
63587 ** result obtained using the hash indexes above. */
63588 {
63589 u32 iRead2 = 0;
63590 u32 iTest;
63591 assert( pWal->bShmUnreliable || pWal->minFrame>0 );
63592 for(iTest=iLast; iTest>=pWal->minFrame && iTest>0; iTest--){
63593 if( walFramePgno(pWal, iTest)==pgno ){
63594 iRead2 = iTest;
63595 break;
63596 }
63597 }
63598 assert( iRead==iRead2 );
63599 }
63600#endif
63601
63602 *piRead = iRead;
63603 return SQLITE_OK;
63604}
63605
63606/*
63607** Read the contents of frame iRead from the wal file into buffer pOut
63608** (which is nOut bytes in size). Return SQLITE_OK if successful, or an
63609** error code otherwise.
63610*/
63611SQLITE_PRIVATE int sqlite3WalReadFrame(
63612 Wal *pWal, /* WAL handle */
63613 u32 iRead, /* Frame to read */
63614 int nOut, /* Size of buffer pOut in bytes */
63615 u8 *pOut /* Buffer to write page data to */
63616){
63617 int sz;
63618 i64 iOffset;
63619 sz = pWal->hdr.szPage;
63620 sz = (sz&0xfe00) + ((sz&0x0001)<<16);
63621 testcase( sz<=32768 );
63622 testcase( sz>=65536 );
63623 iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE;
63624 /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
63625 return sqlite3OsRead(id: pWal->pWalFd, pBuf: pOut, amt: (nOut>sz ? sz : nOut), offset: iOffset);
63626}
63627
63628/*
63629** Return the size of the database in pages (or zero, if unknown).
63630*/
63631SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal){
63632 if( pWal && ALWAYS(pWal->readLock>=0) ){
63633 return pWal->hdr.nPage;
63634 }
63635 return 0;
63636}
63637
63638
63639/*
63640** This function starts a write transaction on the WAL.
63641**
63642** A read transaction must have already been started by a prior call
63643** to sqlite3WalBeginReadTransaction().
63644**
63645** If another thread or process has written into the database since
63646** the read transaction was started, then it is not possible for this
63647** thread to write as doing so would cause a fork. So this routine
63648** returns SQLITE_BUSY in that case and no write transaction is started.
63649**
63650** There can only be a single writer active at a time.
63651*/
63652SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal){
63653 int rc;
63654
63655#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
63656 /* If the write-lock is already held, then it was obtained before the
63657 ** read-transaction was even opened, making this call a no-op.
63658 ** Return early. */
63659 if( pWal->writeLock ){
63660 assert( !memcmp(&pWal->hdr,(void *)walIndexHdr(pWal),sizeof(WalIndexHdr)) );
63661 return SQLITE_OK;
63662 }
63663#endif
63664
63665 /* Cannot start a write transaction without first holding a read
63666 ** transaction. */
63667 assert( pWal->readLock>=0 );
63668 assert( pWal->writeLock==0 && pWal->iReCksum==0 );
63669
63670 if( pWal->readOnly ){
63671 return SQLITE_READONLY;
63672 }
63673
63674 /* Only one writer allowed at a time. Get the write lock. Return
63675 ** SQLITE_BUSY if unable.
63676 */
63677 rc = walLockExclusive(pWal, WAL_WRITE_LOCK, n: 1);
63678 if( rc ){
63679 return rc;
63680 }
63681 pWal->writeLock = 1;
63682
63683 /* If another connection has written to the database file since the
63684 ** time the read transaction on this connection was started, then
63685 ** the write is disallowed.
63686 */
63687 if( memcmp(s1: &pWal->hdr, s2: (void *)walIndexHdr(pWal), n: sizeof(WalIndexHdr))!=0 ){
63688 walUnlockExclusive(pWal, WAL_WRITE_LOCK, n: 1);
63689 pWal->writeLock = 0;
63690 rc = SQLITE_BUSY_SNAPSHOT;
63691 }
63692
63693 return rc;
63694}
63695
63696/*
63697** End a write transaction. The commit has already been done. This
63698** routine merely releases the lock.
63699*/
63700SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal){
63701 if( pWal->writeLock ){
63702 walUnlockExclusive(pWal, WAL_WRITE_LOCK, n: 1);
63703 pWal->writeLock = 0;
63704 pWal->iReCksum = 0;
63705 pWal->truncateOnCommit = 0;
63706 }
63707 return SQLITE_OK;
63708}
63709
63710/*
63711** If any data has been written (but not committed) to the log file, this
63712** function moves the write-pointer back to the start of the transaction.
63713**
63714** Additionally, the callback function is invoked for each frame written
63715** to the WAL since the start of the transaction. If the callback returns
63716** other than SQLITE_OK, it is not invoked again and the error code is
63717** returned to the caller.
63718**
63719** Otherwise, if the callback function does not return an error, this
63720** function returns SQLITE_OK.
63721*/
63722SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){
63723 int rc = SQLITE_OK;
63724 if( ALWAYS(pWal->writeLock) ){
63725 Pgno iMax = pWal->hdr.mxFrame;
63726 Pgno iFrame;
63727
63728 /* Restore the clients cache of the wal-index header to the state it
63729 ** was in before the client began writing to the database.
63730 */
63731 memcpy(dest: &pWal->hdr, src: (void *)walIndexHdr(pWal), n: sizeof(WalIndexHdr));
63732
63733 for(iFrame=pWal->hdr.mxFrame+1;
63734 ALWAYS(rc==SQLITE_OK) && iFrame<=iMax;
63735 iFrame++
63736 ){
63737 /* This call cannot fail. Unless the page for which the page number
63738 ** is passed as the second argument is (a) in the cache and
63739 ** (b) has an outstanding reference, then xUndo is either a no-op
63740 ** (if (a) is false) or simply expels the page from the cache (if (b)
63741 ** is false).
63742 **
63743 ** If the upper layer is doing a rollback, it is guaranteed that there
63744 ** are no outstanding references to any page other than page 1. And
63745 ** page 1 is never written to the log until the transaction is
63746 ** committed. As a result, the call to xUndo may not fail.
63747 */
63748 assert( walFramePgno(pWal, iFrame)!=1 );
63749 rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame));
63750 }
63751 if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
63752 }
63753 return rc;
63754}
63755
63756/*
63757** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32
63758** values. This function populates the array with values required to
63759** "rollback" the write position of the WAL handle back to the current
63760** point in the event of a savepoint rollback (via WalSavepointUndo()).
63761*/
63762SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData){
63763 assert( pWal->writeLock );
63764 aWalData[0] = pWal->hdr.mxFrame;
63765 aWalData[1] = pWal->hdr.aFrameCksum[0];
63766 aWalData[2] = pWal->hdr.aFrameCksum[1];
63767 aWalData[3] = pWal->nCkpt;
63768}
63769
63770/*
63771** Move the write position of the WAL back to the point identified by
63772** the values in the aWalData[] array. aWalData must point to an array
63773** of WAL_SAVEPOINT_NDATA u32 values that has been previously populated
63774** by a call to WalSavepoint().
63775*/
63776SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData){
63777 int rc = SQLITE_OK;
63778
63779 assert( pWal->writeLock );
63780 assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame );
63781
63782 if( aWalData[3]!=pWal->nCkpt ){
63783 /* This savepoint was opened immediately after the write-transaction
63784 ** was started. Right after that, the writer decided to wrap around
63785 ** to the start of the log. Update the savepoint values to match.
63786 */
63787 aWalData[0] = 0;
63788 aWalData[3] = pWal->nCkpt;
63789 }
63790
63791 if( aWalData[0]<pWal->hdr.mxFrame ){
63792 pWal->hdr.mxFrame = aWalData[0];
63793 pWal->hdr.aFrameCksum[0] = aWalData[1];
63794 pWal->hdr.aFrameCksum[1] = aWalData[2];
63795 walCleanupHash(pWal);
63796 }
63797
63798 return rc;
63799}
63800
63801/*
63802** This function is called just before writing a set of frames to the log
63803** file (see sqlite3WalFrames()). It checks to see if, instead of appending
63804** to the current log file, it is possible to overwrite the start of the
63805** existing log file with the new frames (i.e. "reset" the log). If so,
63806** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
63807** unchanged.
63808**
63809** SQLITE_OK is returned if no error is encountered (regardless of whether
63810** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
63811** if an error occurs.
63812*/
63813static int walRestartLog(Wal *pWal){
63814 int rc = SQLITE_OK;
63815 int cnt;
63816
63817 if( pWal->readLock==0 ){
63818 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
63819 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
63820 if( pInfo->nBackfill>0 ){
63821 u32 salt1;
63822 sqlite3_randomness(N: 4, pBuf: &salt1);
63823 rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
63824 if( rc==SQLITE_OK ){
63825 /* If all readers are using WAL_READ_LOCK(0) (in other words if no
63826 ** readers are currently using the WAL), then the transactions
63827 ** frames will overwrite the start of the existing log. Update the
63828 ** wal-index header to reflect this.
63829 **
63830 ** In theory it would be Ok to update the cache of the header only
63831 ** at this point. But updating the actual wal-index header is also
63832 ** safe and means there is no special case for sqlite3WalUndo()
63833 ** to handle if this transaction is rolled back. */
63834 walRestartHdr(pWal, salt1);
63835 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
63836 }else if( rc!=SQLITE_BUSY ){
63837 return rc;
63838 }
63839 }
63840 walUnlockShared(pWal, WAL_READ_LOCK(0));
63841 pWal->readLock = -1;
63842 cnt = 0;
63843 do{
63844 int notUsed;
63845 rc = walTryBeginRead(pWal, pChanged: &notUsed, useWal: 1, cnt: ++cnt);
63846 }while( rc==WAL_RETRY );
63847 assert( (rc&0xff)!=SQLITE_BUSY ); /* BUSY not possible when useWal==1 */
63848 testcase( (rc&0xff)==SQLITE_IOERR );
63849 testcase( rc==SQLITE_PROTOCOL );
63850 testcase( rc==SQLITE_OK );
63851 }
63852 return rc;
63853}
63854
63855/*
63856** Information about the current state of the WAL file and where
63857** the next fsync should occur - passed from sqlite3WalFrames() into
63858** walWriteToLog().
63859*/
63860typedef struct WalWriter {
63861 Wal *pWal; /* The complete WAL information */
63862 sqlite3_file *pFd; /* The WAL file to which we write */
63863 sqlite3_int64 iSyncPoint; /* Fsync at this offset */
63864 int syncFlags; /* Flags for the fsync */
63865 int szPage; /* Size of one page */
63866} WalWriter;
63867
63868/*
63869** Write iAmt bytes of content into the WAL file beginning at iOffset.
63870** Do a sync when crossing the p->iSyncPoint boundary.
63871**
63872** In other words, if iSyncPoint is in between iOffset and iOffset+iAmt,
63873** first write the part before iSyncPoint, then sync, then write the
63874** rest.
63875*/
63876static int walWriteToLog(
63877 WalWriter *p, /* WAL to write to */
63878 void *pContent, /* Content to be written */
63879 int iAmt, /* Number of bytes to write */
63880 sqlite3_int64 iOffset /* Start writing at this offset */
63881){
63882 int rc;
63883 if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
63884 int iFirstAmt = (int)(p->iSyncPoint - iOffset);
63885 rc = sqlite3OsWrite(id: p->pFd, pBuf: pContent, amt: iFirstAmt, offset: iOffset);
63886 if( rc ) return rc;
63887 iOffset += iFirstAmt;
63888 iAmt -= iFirstAmt;
63889 pContent = (void*)(iFirstAmt + (char*)pContent);
63890 assert( WAL_SYNC_FLAGS(p->syncFlags)!=0 );
63891 rc = sqlite3OsSync(id: p->pFd, WAL_SYNC_FLAGS(p->syncFlags));
63892 if( iAmt==0 || rc ) return rc;
63893 }
63894 rc = sqlite3OsWrite(id: p->pFd, pBuf: pContent, amt: iAmt, offset: iOffset);
63895 return rc;
63896}
63897
63898/*
63899** Write out a single frame of the WAL
63900*/
63901static int walWriteOneFrame(
63902 WalWriter *p, /* Where to write the frame */
63903 PgHdr *pPage, /* The page of the frame to be written */
63904 int nTruncate, /* The commit flag. Usually 0. >0 for commit */
63905 sqlite3_int64 iOffset /* Byte offset at which to write */
63906){
63907 int rc; /* Result code from subfunctions */
63908 void *pData; /* Data actually written */
63909 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */
63910 pData = pPage->pData;
63911 walEncodeFrame(pWal: p->pWal, iPage: pPage->pgno, nTruncate, aData: pData, aFrame);
63912 rc = walWriteToLog(p, pContent: aFrame, iAmt: sizeof(aFrame), iOffset);
63913 if( rc ) return rc;
63914 /* Write the page data */
63915 rc = walWriteToLog(p, pContent: pData, iAmt: p->szPage, iOffset: iOffset+sizeof(aFrame));
63916 return rc;
63917}
63918
63919/*
63920** This function is called as part of committing a transaction within which
63921** one or more frames have been overwritten. It updates the checksums for
63922** all frames written to the wal file by the current transaction starting
63923** with the earliest to have been overwritten.
63924**
63925** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
63926*/
63927static int walRewriteChecksums(Wal *pWal, u32 iLast){
63928 const int szPage = pWal->szPage;/* Database page size */
63929 int rc = SQLITE_OK; /* Return code */
63930 u8 *aBuf; /* Buffer to load data from wal file into */
63931 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-headers in */
63932 u32 iRead; /* Next frame to read from wal file */
63933 i64 iCksumOff;
63934
63935 aBuf = sqlite3_malloc(n: szPage + WAL_FRAME_HDRSIZE);
63936 if( aBuf==0 ) return SQLITE_NOMEM_BKPT;
63937
63938 /* Find the checksum values to use as input for the recalculating the
63939 ** first checksum. If the first frame is frame 1 (implying that the current
63940 ** transaction restarted the wal file), these values must be read from the
63941 ** wal-file header. Otherwise, read them from the frame header of the
63942 ** previous frame. */
63943 assert( pWal->iReCksum>0 );
63944 if( pWal->iReCksum==1 ){
63945 iCksumOff = 24;
63946 }else{
63947 iCksumOff = walFrameOffset(pWal->iReCksum-1, szPage) + 16;
63948 }
63949 rc = sqlite3OsRead(id: pWal->pWalFd, pBuf: aBuf, amt: sizeof(u32)*2, offset: iCksumOff);
63950 pWal->hdr.aFrameCksum[0] = sqlite3Get4byte(p: aBuf);
63951 pWal->hdr.aFrameCksum[1] = sqlite3Get4byte(p: &aBuf[sizeof(u32)]);
63952
63953 iRead = pWal->iReCksum;
63954 pWal->iReCksum = 0;
63955 for(; rc==SQLITE_OK && iRead<=iLast; iRead++){
63956 i64 iOff = walFrameOffset(iRead, szPage);
63957 rc = sqlite3OsRead(id: pWal->pWalFd, pBuf: aBuf, amt: szPage+WAL_FRAME_HDRSIZE, offset: iOff);
63958 if( rc==SQLITE_OK ){
63959 u32 iPgno, nDbSize;
63960 iPgno = sqlite3Get4byte(p: aBuf);
63961 nDbSize = sqlite3Get4byte(p: &aBuf[4]);
63962
63963 walEncodeFrame(pWal, iPage: iPgno, nTruncate: nDbSize, aData: &aBuf[WAL_FRAME_HDRSIZE], aFrame);
63964 rc = sqlite3OsWrite(id: pWal->pWalFd, pBuf: aFrame, amt: sizeof(aFrame), offset: iOff);
63965 }
63966 }
63967
63968 sqlite3_free(p: aBuf);
63969 return rc;
63970}
63971
63972/*
63973** Write a set of frames to the log. The caller must hold the write-lock
63974** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
63975*/
63976SQLITE_PRIVATE int sqlite3WalFrames(
63977 Wal *pWal, /* Wal handle to write to */
63978 int szPage, /* Database page-size in bytes */
63979 PgHdr *pList, /* List of dirty pages to write */
63980 Pgno nTruncate, /* Database size after this commit */
63981 int isCommit, /* True if this is a commit */
63982 int sync_flags /* Flags to pass to OsSync() (or 0) */
63983){
63984 int rc; /* Used to catch return codes */
63985 u32 iFrame; /* Next frame address */
63986 PgHdr *p; /* Iterator to run through pList with. */
63987 PgHdr *pLast = 0; /* Last frame in list */
63988 int nExtra = 0; /* Number of extra copies of last page */
63989 int szFrame; /* The size of a single frame */
63990 i64 iOffset; /* Next byte to write in WAL file */
63991 WalWriter w; /* The writer */
63992 u32 iFirst = 0; /* First frame that may be overwritten */
63993 WalIndexHdr *pLive; /* Pointer to shared header */
63994
63995 assert( pList );
63996 assert( pWal->writeLock );
63997
63998 /* If this frame set completes a transaction, then nTruncate>0. If
63999 ** nTruncate==0 then this frame set does not complete the transaction. */
64000 assert( (isCommit!=0)==(nTruncate!=0) );
64001
64002#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
64003 { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
64004 WALTRACE(("WAL%p: frame write begin. %d frames. mxFrame=%d. %s\n",
64005 pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
64006 }
64007#endif
64008
64009 pLive = (WalIndexHdr*)walIndexHdr(pWal);
64010 if( memcmp(s1: &pWal->hdr, s2: (void *)pLive, n: sizeof(WalIndexHdr))!=0 ){
64011 iFirst = pLive->mxFrame+1;
64012 }
64013
64014 /* See if it is possible to write these frames into the start of the
64015 ** log file, instead of appending to it at pWal->hdr.mxFrame.
64016 */
64017 if( SQLITE_OK!=(rc = walRestartLog(pWal)) ){
64018 return rc;
64019 }
64020
64021 /* If this is the first frame written into the log, write the WAL
64022 ** header to the start of the WAL file. See comments at the top of
64023 ** this source file for a description of the WAL header format.
64024 */
64025 iFrame = pWal->hdr.mxFrame;
64026 if( iFrame==0 ){
64027 u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assemble wal-header in */
64028 u32 aCksum[2]; /* Checksum for wal-header */
64029
64030 sqlite3Put4byte(p: &aWalHdr[0], v: (WAL_MAGIC | SQLITE_BIGENDIAN));
64031 sqlite3Put4byte(p: &aWalHdr[4], WAL_MAX_VERSION);
64032 sqlite3Put4byte(p: &aWalHdr[8], v: szPage);
64033 sqlite3Put4byte(p: &aWalHdr[12], v: pWal->nCkpt);
64034 if( pWal->nCkpt==0 ) sqlite3_randomness(N: 8, pBuf: pWal->hdr.aSalt);
64035 memcpy(dest: &aWalHdr[16], src: pWal->hdr.aSalt, n: 8);
64036 walChecksumBytes(nativeCksum: 1, a: aWalHdr, WAL_HDRSIZE-2*4, aIn: 0, aOut: aCksum);
64037 sqlite3Put4byte(p: &aWalHdr[24], v: aCksum[0]);
64038 sqlite3Put4byte(p: &aWalHdr[28], v: aCksum[1]);
64039
64040 pWal->szPage = szPage;
64041 pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
64042 pWal->hdr.aFrameCksum[0] = aCksum[0];
64043 pWal->hdr.aFrameCksum[1] = aCksum[1];
64044 pWal->truncateOnCommit = 1;
64045
64046 rc = sqlite3OsWrite(id: pWal->pWalFd, pBuf: aWalHdr, amt: sizeof(aWalHdr), offset: 0);
64047 WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
64048 if( rc!=SQLITE_OK ){
64049 return rc;
64050 }
64051
64052 /* Sync the header (unless SQLITE_IOCAP_SEQUENTIAL is true or unless
64053 ** all syncing is turned off by PRAGMA synchronous=OFF). Otherwise
64054 ** an out-of-order write following a WAL restart could result in
64055 ** database corruption. See the ticket:
64056 **
64057 ** https://sqlite.org/src/info/ff5be73dee
64058 */
64059 if( pWal->syncHeader ){
64060 rc = sqlite3OsSync(id: pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
64061 if( rc ) return rc;
64062 }
64063 }
64064 assert( (int)pWal->szPage==szPage );
64065
64066 /* Setup information needed to write frames into the WAL */
64067 w.pWal = pWal;
64068 w.pFd = pWal->pWalFd;
64069 w.iSyncPoint = 0;
64070 w.syncFlags = sync_flags;
64071 w.szPage = szPage;
64072 iOffset = walFrameOffset(iFrame+1, szPage);
64073 szFrame = szPage + WAL_FRAME_HDRSIZE;
64074
64075 /* Write all frames into the log file exactly once */
64076 for(p=pList; p; p=p->pDirty){
64077 int nDbSize; /* 0 normally. Positive == commit flag */
64078
64079 /* Check if this page has already been written into the wal file by
64080 ** the current transaction. If so, overwrite the existing frame and
64081 ** set Wal.writeLock to WAL_WRITELOCK_RECKSUM - indicating that
64082 ** checksums must be recomputed when the transaction is committed. */
64083 if( iFirst && (p->pDirty || isCommit==0) ){
64084 u32 iWrite = 0;
64085 VVA_ONLY(rc =) sqlite3WalFindFrame(pWal, pgno: p->pgno, piRead: &iWrite);
64086 assert( rc==SQLITE_OK || iWrite==0 );
64087 if( iWrite>=iFirst ){
64088 i64 iOff = walFrameOffset(iWrite, szPage) + WAL_FRAME_HDRSIZE;
64089 void *pData;
64090 if( pWal->iReCksum==0 || iWrite<pWal->iReCksum ){
64091 pWal->iReCksum = iWrite;
64092 }
64093 pData = p->pData;
64094 rc = sqlite3OsWrite(id: pWal->pWalFd, pBuf: pData, amt: szPage, offset: iOff);
64095 if( rc ) return rc;
64096 p->flags &= ~PGHDR_WAL_APPEND;
64097 continue;
64098 }
64099 }
64100
64101 iFrame++;
64102 assert( iOffset==walFrameOffset(iFrame, szPage) );
64103 nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
64104 rc = walWriteOneFrame(p: &w, pPage: p, nTruncate: nDbSize, iOffset);
64105 if( rc ) return rc;
64106 pLast = p;
64107 iOffset += szFrame;
64108 p->flags |= PGHDR_WAL_APPEND;
64109 }
64110
64111 /* Recalculate checksums within the wal file if required. */
64112 if( isCommit && pWal->iReCksum ){
64113 rc = walRewriteChecksums(pWal, iLast: iFrame);
64114 if( rc ) return rc;
64115 }
64116
64117 /* If this is the end of a transaction, then we might need to pad
64118 ** the transaction and/or sync the WAL file.
64119 **
64120 ** Padding and syncing only occur if this set of frames complete a
64121 ** transaction and if PRAGMA synchronous=FULL. If synchronous==NORMAL
64122 ** or synchronous==OFF, then no padding or syncing are needed.
64123 **
64124 ** If SQLITE_IOCAP_POWERSAFE_OVERWRITE is defined, then padding is not
64125 ** needed and only the sync is done. If padding is needed, then the
64126 ** final frame is repeated (with its commit mark) until the next sector
64127 ** boundary is crossed. Only the part of the WAL prior to the last
64128 ** sector boundary is synced; the part of the last frame that extends
64129 ** past the sector boundary is written after the sync.
64130 */
64131 if( isCommit && WAL_SYNC_FLAGS(sync_flags)!=0 ){
64132 int bSync = 1;
64133 if( pWal->padToSectorBoundary ){
64134 int sectorSize = sqlite3SectorSize(pFile: pWal->pWalFd);
64135 w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
64136 bSync = (w.iSyncPoint==iOffset);
64137 testcase( bSync );
64138 while( iOffset<w.iSyncPoint ){
64139 rc = walWriteOneFrame(p: &w, pPage: pLast, nTruncate, iOffset);
64140 if( rc ) return rc;
64141 iOffset += szFrame;
64142 nExtra++;
64143 assert( pLast!=0 );
64144 }
64145 }
64146 if( bSync ){
64147 assert( rc==SQLITE_OK );
64148 rc = sqlite3OsSync(id: w.pFd, WAL_SYNC_FLAGS(sync_flags));
64149 }
64150 }
64151
64152 /* If this frame set completes the first transaction in the WAL and
64153 ** if PRAGMA journal_size_limit is set, then truncate the WAL to the
64154 ** journal size limit, if possible.
64155 */
64156 if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
64157 i64 sz = pWal->mxWalSize;
64158 if( walFrameOffset(iFrame+nExtra+1, szPage)>pWal->mxWalSize ){
64159 sz = walFrameOffset(iFrame+nExtra+1, szPage);
64160 }
64161 walLimitSize(pWal, nMax: sz);
64162 pWal->truncateOnCommit = 0;
64163 }
64164
64165 /* Append data to the wal-index. It is not necessary to lock the
64166 ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
64167 ** guarantees that there are no other writers, and no data that may
64168 ** be in use by existing readers is being overwritten.
64169 */
64170 iFrame = pWal->hdr.mxFrame;
64171 for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
64172 if( (p->flags & PGHDR_WAL_APPEND)==0 ) continue;
64173 iFrame++;
64174 rc = walIndexAppend(pWal, iFrame, iPage: p->pgno);
64175 }
64176 assert( pLast!=0 || nExtra==0 );
64177 while( rc==SQLITE_OK && nExtra>0 ){
64178 iFrame++;
64179 nExtra--;
64180 rc = walIndexAppend(pWal, iFrame, iPage: pLast->pgno);
64181 }
64182
64183 if( rc==SQLITE_OK ){
64184 /* Update the private copy of the header. */
64185 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
64186 testcase( szPage<=32768 );
64187 testcase( szPage>=65536 );
64188 pWal->hdr.mxFrame = iFrame;
64189 if( isCommit ){
64190 pWal->hdr.iChange++;
64191 pWal->hdr.nPage = nTruncate;
64192 }
64193 /* If this is a commit, update the wal-index header too. */
64194 if( isCommit ){
64195 walIndexWriteHdr(pWal);
64196 pWal->iCallback = iFrame;
64197 }
64198 }
64199
64200 WALTRACE(("WAL%p: frame write %s\n", pWal, rc ? "failed" : "ok"));
64201 return rc;
64202}
64203
64204/*
64205** This routine is called to implement sqlite3_wal_checkpoint() and
64206** related interfaces.
64207**
64208** Obtain a CHECKPOINT lock and then backfill as much information as
64209** we can from WAL into the database.
64210**
64211** If parameter xBusy is not NULL, it is a pointer to a busy-handler
64212** callback. In this case this function runs a blocking checkpoint.
64213*/
64214SQLITE_PRIVATE int sqlite3WalCheckpoint(
64215 Wal *pWal, /* Wal connection */
64216 sqlite3 *db, /* Check this handle's interrupt flag */
64217 int eMode, /* PASSIVE, FULL, RESTART, or TRUNCATE */
64218 int (*xBusy)(void*), /* Function to call when busy */
64219 void *pBusyArg, /* Context argument for xBusyHandler */
64220 int sync_flags, /* Flags to sync db file with (or 0) */
64221 int nBuf, /* Size of temporary buffer */
64222 u8 *zBuf, /* Temporary buffer to use */
64223 int *pnLog, /* OUT: Number of frames in WAL */
64224 int *pnCkpt /* OUT: Number of backfilled frames in WAL */
64225){
64226 int rc; /* Return code */
64227 int isChanged = 0; /* True if a new wal-index header is loaded */
64228 int eMode2 = eMode; /* Mode to pass to walCheckpoint() */
64229 int (*xBusy2)(void*) = xBusy; /* Busy handler for eMode2 */
64230
64231 assert( pWal->ckptLock==0 );
64232 assert( pWal->writeLock==0 );
64233
64234 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
64235 ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
64236 assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 );
64237
64238 if( pWal->readOnly ) return SQLITE_READONLY;
64239 WALTRACE(("WAL%p: checkpoint begins\n", pWal));
64240
64241 /* Enable blocking locks, if possible. If blocking locks are successfully
64242 ** enabled, set xBusy2=0 so that the busy-handler is never invoked. */
64243 sqlite3WalDb(pWal, db);
64244 (void)walEnableBlocking(pWal);
64245
64246 /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive
64247 ** "checkpoint" lock on the database file.
64248 ** EVIDENCE-OF: R-10421-19736 If any other process is running a
64249 ** checkpoint operation at the same time, the lock cannot be obtained and
64250 ** SQLITE_BUSY is returned.
64251 ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
64252 ** it will not be invoked in this case.
64253 */
64254 rc = walLockExclusive(pWal, WAL_CKPT_LOCK, n: 1);
64255 testcase( rc==SQLITE_BUSY );
64256 testcase( rc!=SQLITE_OK && xBusy2!=0 );
64257 if( rc==SQLITE_OK ){
64258 pWal->ckptLock = 1;
64259
64260 /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and
64261 ** TRUNCATE modes also obtain the exclusive "writer" lock on the database
64262 ** file.
64263 **
64264 ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained
64265 ** immediately, and a busy-handler is configured, it is invoked and the
64266 ** writer lock retried until either the busy-handler returns 0 or the
64267 ** lock is successfully obtained.
64268 */
64269 if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){
64270 rc = walBusyLock(pWal, xBusy: xBusy2, pBusyArg, WAL_WRITE_LOCK, n: 1);
64271 if( rc==SQLITE_OK ){
64272 pWal->writeLock = 1;
64273 }else if( rc==SQLITE_BUSY ){
64274 eMode2 = SQLITE_CHECKPOINT_PASSIVE;
64275 xBusy2 = 0;
64276 rc = SQLITE_OK;
64277 }
64278 }
64279 }
64280
64281
64282 /* Read the wal-index header. */
64283 if( rc==SQLITE_OK ){
64284 walDisableBlocking(pWal);
64285 rc = walIndexReadHdr(pWal, pChanged: &isChanged);
64286 (void)walEnableBlocking(pWal);
64287 if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
64288 sqlite3OsUnfetch(id: pWal->pDbFd, iOff: 0, p: 0);
64289 }
64290 }
64291
64292 /* Copy data from the log to the database file. */
64293 if( rc==SQLITE_OK ){
64294
64295 if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
64296 rc = SQLITE_CORRUPT_BKPT;
64297 }else{
64298 rc = walCheckpoint(pWal, db, eMode: eMode2, xBusy: xBusy2, pBusyArg, sync_flags, zBuf);
64299 }
64300
64301 /* If no error occurred, set the output variables. */
64302 if( rc==SQLITE_OK || rc==SQLITE_BUSY ){
64303 if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
64304 if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
64305 }
64306 }
64307
64308 if( isChanged ){
64309 /* If a new wal-index header was loaded before the checkpoint was
64310 ** performed, then the pager-cache associated with pWal is now
64311 ** out of date. So zero the cached wal-index header to ensure that
64312 ** next time the pager opens a snapshot on this database it knows that
64313 ** the cache needs to be reset.
64314 */
64315 memset(s: &pWal->hdr, c: 0, n: sizeof(WalIndexHdr));
64316 }
64317
64318 walDisableBlocking(pWal);
64319 sqlite3WalDb(pWal, 0);
64320
64321 /* Release the locks. */
64322 sqlite3WalEndWriteTransaction(pWal);
64323 if( pWal->ckptLock ){
64324 walUnlockExclusive(pWal, WAL_CKPT_LOCK, n: 1);
64325 pWal->ckptLock = 0;
64326 }
64327 WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok"));
64328#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
64329 if( rc==SQLITE_BUSY_TIMEOUT ) rc = SQLITE_BUSY;
64330#endif
64331 return (rc==SQLITE_OK && eMode!=eMode2 ? SQLITE_BUSY : rc);
64332}
64333
64334/* Return the value to pass to a sqlite3_wal_hook callback, the
64335** number of frames in the WAL at the point of the last commit since
64336** sqlite3WalCallback() was called. If no commits have occurred since
64337** the last call, then return 0.
64338*/
64339SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal){
64340 u32 ret = 0;
64341 if( pWal ){
64342 ret = pWal->iCallback;
64343 pWal->iCallback = 0;
64344 }
64345 return (int)ret;
64346}
64347
64348/*
64349** This function is called to change the WAL subsystem into or out
64350** of locking_mode=EXCLUSIVE.
64351**
64352** If op is zero, then attempt to change from locking_mode=EXCLUSIVE
64353** into locking_mode=NORMAL. This means that we must acquire a lock
64354** on the pWal->readLock byte. If the WAL is already in locking_mode=NORMAL
64355** or if the acquisition of the lock fails, then return 0. If the
64356** transition out of exclusive-mode is successful, return 1. This
64357** operation must occur while the pager is still holding the exclusive
64358** lock on the main database file.
64359**
64360** If op is one, then change from locking_mode=NORMAL into
64361** locking_mode=EXCLUSIVE. This means that the pWal->readLock must
64362** be released. Return 1 if the transition is made and 0 if the
64363** WAL is already in exclusive-locking mode - meaning that this
64364** routine is a no-op. The pager must already hold the exclusive lock
64365** on the main database file before invoking this operation.
64366**
64367** If op is negative, then do a dry-run of the op==1 case but do
64368** not actually change anything. The pager uses this to see if it
64369** should acquire the database exclusive lock prior to invoking
64370** the op==1 case.
64371*/
64372SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op){
64373 int rc;
64374 assert( pWal->writeLock==0 );
64375 assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 );
64376
64377 /* pWal->readLock is usually set, but might be -1 if there was a
64378 ** prior error while attempting to acquire are read-lock. This cannot
64379 ** happen if the connection is actually in exclusive mode (as no xShmLock
64380 ** locks are taken in this case). Nor should the pager attempt to
64381 ** upgrade to exclusive-mode following such an error.
64382 */
64383 assert( pWal->readLock>=0 || pWal->lockError );
64384 assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
64385
64386 if( op==0 ){
64387 if( pWal->exclusiveMode!=WAL_NORMAL_MODE ){
64388 pWal->exclusiveMode = WAL_NORMAL_MODE;
64389 if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
64390 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
64391 }
64392 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
64393 }else{
64394 /* Already in locking_mode=NORMAL */
64395 rc = 0;
64396 }
64397 }else if( op>0 ){
64398 assert( pWal->exclusiveMode==WAL_NORMAL_MODE );
64399 assert( pWal->readLock>=0 );
64400 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
64401 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
64402 rc = 1;
64403 }else{
64404 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
64405 }
64406 return rc;
64407}
64408
64409/*
64410** Return true if the argument is non-NULL and the WAL module is using
64411** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
64412** WAL module is using shared-memory, return false.
64413*/
64414SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal){
64415 return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE );
64416}
64417
64418#ifdef SQLITE_ENABLE_SNAPSHOT
64419/* Create a snapshot object. The content of a snapshot is opaque to
64420** every other subsystem, so the WAL module can put whatever it needs
64421** in the object.
64422*/
64423SQLITE_PRIVATE int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot){
64424 int rc = SQLITE_OK;
64425 WalIndexHdr *pRet;
64426 static const u32 aZero[4] = { 0, 0, 0, 0 };
64427
64428 assert( pWal->readLock>=0 && pWal->writeLock==0 );
64429
64430 if( memcmp(&pWal->hdr.aFrameCksum[0],aZero,16)==0 ){
64431 *ppSnapshot = 0;
64432 return SQLITE_ERROR;
64433 }
64434 pRet = (WalIndexHdr*)sqlite3_malloc(sizeof(WalIndexHdr));
64435 if( pRet==0 ){
64436 rc = SQLITE_NOMEM_BKPT;
64437 }else{
64438 memcpy(pRet, &pWal->hdr, sizeof(WalIndexHdr));
64439 *ppSnapshot = (sqlite3_snapshot*)pRet;
64440 }
64441
64442 return rc;
64443}
64444
64445/* Try to open on pSnapshot when the next read-transaction starts
64446*/
64447SQLITE_PRIVATE void sqlite3WalSnapshotOpen(
64448 Wal *pWal,
64449 sqlite3_snapshot *pSnapshot
64450){
64451 pWal->pSnapshot = (WalIndexHdr*)pSnapshot;
64452}
64453
64454/*
64455** Return a +ve value if snapshot p1 is newer than p2. A -ve value if
64456** p1 is older than p2 and zero if p1 and p2 are the same snapshot.
64457*/
64458SQLITE_API int sqlite3_snapshot_cmp(sqlite3_snapshot *p1, sqlite3_snapshot *p2){
64459 WalIndexHdr *pHdr1 = (WalIndexHdr*)p1;
64460 WalIndexHdr *pHdr2 = (WalIndexHdr*)p2;
64461
64462 /* aSalt[0] is a copy of the value stored in the wal file header. It
64463 ** is incremented each time the wal file is restarted. */
64464 if( pHdr1->aSalt[0]<pHdr2->aSalt[0] ) return -1;
64465 if( pHdr1->aSalt[0]>pHdr2->aSalt[0] ) return +1;
64466 if( pHdr1->mxFrame<pHdr2->mxFrame ) return -1;
64467 if( pHdr1->mxFrame>pHdr2->mxFrame ) return +1;
64468 return 0;
64469}
64470
64471/*
64472** The caller currently has a read transaction open on the database.
64473** This function takes a SHARED lock on the CHECKPOINTER slot and then
64474** checks if the snapshot passed as the second argument is still
64475** available. If so, SQLITE_OK is returned.
64476**
64477** If the snapshot is not available, SQLITE_ERROR is returned. Or, if
64478** the CHECKPOINTER lock cannot be obtained, SQLITE_BUSY. If any error
64479** occurs (any value other than SQLITE_OK is returned), the CHECKPOINTER
64480** lock is released before returning.
64481*/
64482SQLITE_PRIVATE int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot){
64483 int rc;
64484 rc = walLockShared(pWal, WAL_CKPT_LOCK);
64485 if( rc==SQLITE_OK ){
64486 WalIndexHdr *pNew = (WalIndexHdr*)pSnapshot;
64487 if( memcmp(pNew->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
64488 || pNew->mxFrame<walCkptInfo(pWal)->nBackfillAttempted
64489 ){
64490 rc = SQLITE_ERROR_SNAPSHOT;
64491 walUnlockShared(pWal, WAL_CKPT_LOCK);
64492 }
64493 }
64494 return rc;
64495}
64496
64497/*
64498** Release a lock obtained by an earlier successful call to
64499** sqlite3WalSnapshotCheck().
64500*/
64501SQLITE_PRIVATE void sqlite3WalSnapshotUnlock(Wal *pWal){
64502 assert( pWal );
64503 walUnlockShared(pWal, WAL_CKPT_LOCK);
64504}
64505
64506
64507#endif /* SQLITE_ENABLE_SNAPSHOT */
64508
64509#ifdef SQLITE_ENABLE_ZIPVFS
64510/*
64511** If the argument is not NULL, it points to a Wal object that holds a
64512** read-lock. This function returns the database page-size if it is known,
64513** or zero if it is not (or if pWal is NULL).
64514*/
64515SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal){
64516 assert( pWal==0 || pWal->readLock>=0 );
64517 return (pWal ? pWal->szPage : 0);
64518}
64519#endif
64520
64521/* Return the sqlite3_file object for the WAL file
64522*/
64523SQLITE_PRIVATE sqlite3_file *sqlite3WalFile(Wal *pWal){
64524 return pWal->pWalFd;
64525}
64526
64527#endif /* #ifndef SQLITE_OMIT_WAL */
64528
64529/************** End of wal.c *************************************************/
64530/************** Begin file btmutex.c *****************************************/
64531/*
64532** 2007 August 27
64533**
64534** The author disclaims copyright to this source code. In place of
64535** a legal notice, here is a blessing:
64536**
64537** May you do good and not evil.
64538** May you find forgiveness for yourself and forgive others.
64539** May you share freely, never taking more than you give.
64540**
64541*************************************************************************
64542**
64543** This file contains code used to implement mutexes on Btree objects.
64544** This code really belongs in btree.c. But btree.c is getting too
64545** big and we want to break it down some. This packaged seemed like
64546** a good breakout.
64547*/
64548/************** Include btreeInt.h in the middle of btmutex.c ****************/
64549/************** Begin file btreeInt.h ****************************************/
64550/*
64551** 2004 April 6
64552**
64553** The author disclaims copyright to this source code. In place of
64554** a legal notice, here is a blessing:
64555**
64556** May you do good and not evil.
64557** May you find forgiveness for yourself and forgive others.
64558** May you share freely, never taking more than you give.
64559**
64560*************************************************************************
64561** This file implements an external (disk-based) database using BTrees.
64562** For a detailed discussion of BTrees, refer to
64563**
64564** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
64565** "Sorting And Searching", pages 473-480. Addison-Wesley
64566** Publishing Company, Reading, Massachusetts.
64567**
64568** The basic idea is that each page of the file contains N database
64569** entries and N+1 pointers to subpages.
64570**
64571** ----------------------------------------------------------------
64572** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
64573** ----------------------------------------------------------------
64574**
64575** All of the keys on the page that Ptr(0) points to have values less
64576** than Key(0). All of the keys on page Ptr(1) and its subpages have
64577** values greater than Key(0) and less than Key(1). All of the keys
64578** on Ptr(N) and its subpages have values greater than Key(N-1). And
64579** so forth.
64580**
64581** Finding a particular key requires reading O(log(M)) pages from the
64582** disk where M is the number of entries in the tree.
64583**
64584** In this implementation, a single file can hold one or more separate
64585** BTrees. Each BTree is identified by the index of its root page. The
64586** key and data for any entry are combined to form the "payload". A
64587** fixed amount of payload can be carried directly on the database
64588** page. If the payload is larger than the preset amount then surplus
64589** bytes are stored on overflow pages. The payload for an entry
64590** and the preceding pointer are combined to form a "Cell". Each
64591** page has a small header which contains the Ptr(N) pointer and other
64592** information such as the size of key and data.
64593**
64594** FORMAT DETAILS
64595**
64596** The file is divided into pages. The first page is called page 1,
64597** the second is page 2, and so forth. A page number of zero indicates
64598** "no such page". The page size can be any power of 2 between 512 and 65536.
64599** Each page can be either a btree page, a freelist page, an overflow
64600** page, or a pointer-map page.
64601**
64602** The first page is always a btree page. The first 100 bytes of the first
64603** page contain a special header (the "file header") that describes the file.
64604** The format of the file header is as follows:
64605**
64606** OFFSET SIZE DESCRIPTION
64607** 0 16 Header string: "SQLite format 3\000"
64608** 16 2 Page size in bytes. (1 means 65536)
64609** 18 1 File format write version
64610** 19 1 File format read version
64611** 20 1 Bytes of unused space at the end of each page
64612** 21 1 Max embedded payload fraction (must be 64)
64613** 22 1 Min embedded payload fraction (must be 32)
64614** 23 1 Min leaf payload fraction (must be 32)
64615** 24 4 File change counter
64616** 28 4 Reserved for future use
64617** 32 4 First freelist page
64618** 36 4 Number of freelist pages in the file
64619** 40 60 15 4-byte meta values passed to higher layers
64620**
64621** 40 4 Schema cookie
64622** 44 4 File format of schema layer
64623** 48 4 Size of page cache
64624** 52 4 Largest root-page (auto/incr_vacuum)
64625** 56 4 1=UTF-8 2=UTF16le 3=UTF16be
64626** 60 4 User version
64627** 64 4 Incremental vacuum mode
64628** 68 4 Application-ID
64629** 72 20 unused
64630** 92 4 The version-valid-for number
64631** 96 4 SQLITE_VERSION_NUMBER
64632**
64633** All of the integer values are big-endian (most significant byte first).
64634**
64635** The file change counter is incremented when the database is changed
64636** This counter allows other processes to know when the file has changed
64637** and thus when they need to flush their cache.
64638**
64639** The max embedded payload fraction is the amount of the total usable
64640** space in a page that can be consumed by a single cell for standard
64641** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default
64642** is to limit the maximum cell size so that at least 4 cells will fit
64643** on one page. Thus the default max embedded payload fraction is 64.
64644**
64645** If the payload for a cell is larger than the max payload, then extra
64646** payload is spilled to overflow pages. Once an overflow page is allocated,
64647** as many bytes as possible are moved into the overflow pages without letting
64648** the cell size drop below the min embedded payload fraction.
64649**
64650** The min leaf payload fraction is like the min embedded payload fraction
64651** except that it applies to leaf nodes in a LEAFDATA tree. The maximum
64652** payload fraction for a LEAFDATA tree is always 100% (or 255) and it
64653** not specified in the header.
64654**
64655** Each btree pages is divided into three sections: The header, the
64656** cell pointer array, and the cell content area. Page 1 also has a 100-byte
64657** file header that occurs before the page header.
64658**
64659** |----------------|
64660** | file header | 100 bytes. Page 1 only.
64661** |----------------|
64662** | page header | 8 bytes for leaves. 12 bytes for interior nodes
64663** |----------------|
64664** | cell pointer | | 2 bytes per cell. Sorted order.
64665** | array | | Grows downward
64666** | | v
64667** |----------------|
64668** | unallocated |
64669** | space |
64670** |----------------| ^ Grows upwards
64671** | cell content | | Arbitrary order interspersed with freeblocks.
64672** | area | | and free space fragments.
64673** |----------------|
64674**
64675** The page headers looks like this:
64676**
64677** OFFSET SIZE DESCRIPTION
64678** 0 1 Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf
64679** 1 2 byte offset to the first freeblock
64680** 3 2 number of cells on this page
64681** 5 2 first byte of the cell content area
64682** 7 1 number of fragmented free bytes
64683** 8 4 Right child (the Ptr(N) value). Omitted on leaves.
64684**
64685** The flags define the format of this btree page. The leaf flag means that
64686** this page has no children. The zerodata flag means that this page carries
64687** only keys and no data. The intkey flag means that the key is an integer
64688** which is stored in the key size entry of the cell header rather than in
64689** the payload area.
64690**
64691** The cell pointer array begins on the first byte after the page header.
64692** The cell pointer array contains zero or more 2-byte numbers which are
64693** offsets from the beginning of the page to the cell content in the cell
64694** content area. The cell pointers occur in sorted order. The system strives
64695** to keep free space after the last cell pointer so that new cells can
64696** be easily added without having to defragment the page.
64697**
64698** Cell content is stored at the very end of the page and grows toward the
64699** beginning of the page.
64700**
64701** Unused space within the cell content area is collected into a linked list of
64702** freeblocks. Each freeblock is at least 4 bytes in size. The byte offset
64703** to the first freeblock is given in the header. Freeblocks occur in
64704** increasing order. Because a freeblock must be at least 4 bytes in size,
64705** any group of 3 or fewer unused bytes in the cell content area cannot
64706** exist on the freeblock chain. A group of 3 or fewer free bytes is called
64707** a fragment. The total number of bytes in all fragments is recorded.
64708** in the page header at offset 7.
64709**
64710** SIZE DESCRIPTION
64711** 2 Byte offset of the next freeblock
64712** 2 Bytes in this freeblock
64713**
64714** Cells are of variable length. Cells are stored in the cell content area at
64715** the end of the page. Pointers to the cells are in the cell pointer array
64716** that immediately follows the page header. Cells is not necessarily
64717** contiguous or in order, but cell pointers are contiguous and in order.
64718**
64719** Cell content makes use of variable length integers. A variable
64720** length integer is 1 to 9 bytes where the lower 7 bits of each
64721** byte are used. The integer consists of all bytes that have bit 8 set and
64722** the first byte with bit 8 clear. The most significant byte of the integer
64723** appears first. A variable-length integer may not be more than 9 bytes long.
64724** As a special case, all 8 bytes of the 9th byte are used as data. This
64725** allows a 64-bit integer to be encoded in 9 bytes.
64726**
64727** 0x00 becomes 0x00000000
64728** 0x7f becomes 0x0000007f
64729** 0x81 0x00 becomes 0x00000080
64730** 0x82 0x00 becomes 0x00000100
64731** 0x80 0x7f becomes 0x0000007f
64732** 0x8a 0x91 0xd1 0xac 0x78 becomes 0x12345678
64733** 0x81 0x81 0x81 0x81 0x01 becomes 0x10204081
64734**
64735** Variable length integers are used for rowids and to hold the number of
64736** bytes of key and data in a btree cell.
64737**
64738** The content of a cell looks like this:
64739**
64740** SIZE DESCRIPTION
64741** 4 Page number of the left child. Omitted if leaf flag is set.
64742** var Number of bytes of data. Omitted if the zerodata flag is set.
64743** var Number of bytes of key. Or the key itself if intkey flag is set.
64744** * Payload
64745** 4 First page of the overflow chain. Omitted if no overflow
64746**
64747** Overflow pages form a linked list. Each page except the last is completely
64748** filled with data (pagesize - 4 bytes). The last page can have as little
64749** as 1 byte of data.
64750**
64751** SIZE DESCRIPTION
64752** 4 Page number of next overflow page
64753** * Data
64754**
64755** Freelist pages come in two subtypes: trunk pages and leaf pages. The
64756** file header points to the first in a linked list of trunk page. Each trunk
64757** page points to multiple leaf pages. The content of a leaf page is
64758** unspecified. A trunk page looks like this:
64759**
64760** SIZE DESCRIPTION
64761** 4 Page number of next trunk page
64762** 4 Number of leaf pointers on this page
64763** * zero or more pages numbers of leaves
64764*/
64765/* #include "sqliteInt.h" */
64766
64767
64768/* The following value is the maximum cell size assuming a maximum page
64769** size give above.
64770*/
64771#define MX_CELL_SIZE(pBt) ((int)(pBt->pageSize-8))
64772
64773/* The maximum number of cells on a single page of the database. This
64774** assumes a minimum cell size of 6 bytes (4 bytes for the cell itself
64775** plus 2 bytes for the index to the cell in the page header). Such
64776** small cells will be rare, but they are possible.
64777*/
64778#define MX_CELL(pBt) ((pBt->pageSize-8)/6)
64779
64780/* Forward declarations */
64781typedef struct MemPage MemPage;
64782typedef struct BtLock BtLock;
64783typedef struct CellInfo CellInfo;
64784
64785/*
64786** This is a magic string that appears at the beginning of every
64787** SQLite database in order to identify the file as a real database.
64788**
64789** You can change this value at compile-time by specifying a
64790** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The
64791** header must be exactly 16 bytes including the zero-terminator so
64792** the string itself should be 15 characters long. If you change
64793** the header, then your custom library will not be able to read
64794** databases generated by the standard tools and the standard tools
64795** will not be able to read databases created by your custom library.
64796*/
64797#ifndef SQLITE_FILE_HEADER /* 123456789 123456 */
64798# define SQLITE_FILE_HEADER "SQLite format 3"
64799#endif
64800
64801/*
64802** Page type flags. An ORed combination of these flags appear as the
64803** first byte of on-disk image of every BTree page.
64804*/
64805#define PTF_INTKEY 0x01
64806#define PTF_ZERODATA 0x02
64807#define PTF_LEAFDATA 0x04
64808#define PTF_LEAF 0x08
64809
64810/*
64811** An instance of this object stores information about each a single database
64812** page that has been loaded into memory. The information in this object
64813** is derived from the raw on-disk page content.
64814**
64815** As each database page is loaded into memory, the pager allocats an
64816** instance of this object and zeros the first 8 bytes. (This is the
64817** "extra" information associated with each page of the pager.)
64818**
64819** Access to all fields of this structure is controlled by the mutex
64820** stored in MemPage.pBt->mutex.
64821*/
64822struct MemPage {
64823 u8 isInit; /* True if previously initialized. MUST BE FIRST! */
64824 u8 intKey; /* True if table b-trees. False for index b-trees */
64825 u8 intKeyLeaf; /* True if the leaf of an intKey table */
64826 Pgno pgno; /* Page number for this page */
64827 /* Only the first 8 bytes (above) are zeroed by pager.c when a new page
64828 ** is allocated. All fields that follow must be initialized before use */
64829 u8 leaf; /* True if a leaf page */
64830 u8 hdrOffset; /* 100 for page 1. 0 otherwise */
64831 u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */
64832 u8 max1bytePayload; /* min(maxLocal,127) */
64833 u8 nOverflow; /* Number of overflow cell bodies in aCell[] */
64834 u16 maxLocal; /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
64835 u16 minLocal; /* Copy of BtShared.minLocal or BtShared.minLeaf */
64836 u16 cellOffset; /* Index in aData of first cell pointer */
64837 int nFree; /* Number of free bytes on the page. -1 for unknown */
64838 u16 nCell; /* Number of cells on this page, local and ovfl */
64839 u16 maskPage; /* Mask for page offset */
64840 u16 aiOvfl[4]; /* Insert the i-th overflow cell before the aiOvfl-th
64841 ** non-overflow cell */
64842 u8 *apOvfl[4]; /* Pointers to the body of overflow cells */
64843 BtShared *pBt; /* Pointer to BtShared that this page is part of */
64844 u8 *aData; /* Pointer to disk image of the page data */
64845 u8 *aDataEnd; /* One byte past the end of usable data */
64846 u8 *aCellIdx; /* The cell index area */
64847 u8 *aDataOfst; /* Same as aData for leaves. aData+4 for interior */
64848 DbPage *pDbPage; /* Pager page handle */
64849 u16 (*xCellSize)(MemPage*,u8*); /* cellSizePtr method */
64850 void (*xParseCell)(MemPage*,u8*,CellInfo*); /* btreeParseCell method */
64851};
64852
64853/*
64854** A linked list of the following structures is stored at BtShared.pLock.
64855** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor
64856** is opened on the table with root page BtShared.iTable. Locks are removed
64857** from this list when a transaction is committed or rolled back, or when
64858** a btree handle is closed.
64859*/
64860struct BtLock {
64861 Btree *pBtree; /* Btree handle holding this lock */
64862 Pgno iTable; /* Root page of table */
64863 u8 eLock; /* READ_LOCK or WRITE_LOCK */
64864 BtLock *pNext; /* Next in BtShared.pLock list */
64865};
64866
64867/* Candidate values for BtLock.eLock */
64868#define READ_LOCK 1
64869#define WRITE_LOCK 2
64870
64871/* A Btree handle
64872**
64873** A database connection contains a pointer to an instance of
64874** this object for every database file that it has open. This structure
64875** is opaque to the database connection. The database connection cannot
64876** see the internals of this structure and only deals with pointers to
64877** this structure.
64878**
64879** For some database files, the same underlying database cache might be
64880** shared between multiple connections. In that case, each connection
64881** has it own instance of this object. But each instance of this object
64882** points to the same BtShared object. The database cache and the
64883** schema associated with the database file are all contained within
64884** the BtShared object.
64885**
64886** All fields in this structure are accessed under sqlite3.mutex.
64887** The pBt pointer itself may not be changed while there exists cursors
64888** in the referenced BtShared that point back to this Btree since those
64889** cursors have to go through this Btree to find their BtShared and
64890** they often do so without holding sqlite3.mutex.
64891*/
64892struct Btree {
64893 sqlite3 *db; /* The database connection holding this btree */
64894 BtShared *pBt; /* Sharable content of this btree */
64895 u8 inTrans; /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
64896 u8 sharable; /* True if we can share pBt with another db */
64897 u8 locked; /* True if db currently has pBt locked */
64898 u8 hasIncrblobCur; /* True if there are one or more Incrblob cursors */
64899 int wantToLock; /* Number of nested calls to sqlite3BtreeEnter() */
64900 int nBackup; /* Number of backup operations reading this btree */
64901 u32 iBDataVersion; /* Combines with pBt->pPager->iDataVersion */
64902 Btree *pNext; /* List of other sharable Btrees from the same db */
64903 Btree *pPrev; /* Back pointer of the same list */
64904#ifdef SQLITE_DEBUG
64905 u64 nSeek; /* Calls to sqlite3BtreeMovetoUnpacked() */
64906#endif
64907#ifndef SQLITE_OMIT_SHARED_CACHE
64908 BtLock lock; /* Object used to lock page 1 */
64909#endif
64910};
64911
64912/*
64913** Btree.inTrans may take one of the following values.
64914**
64915** If the shared-data extension is enabled, there may be multiple users
64916** of the Btree structure. At most one of these may open a write transaction,
64917** but any number may have active read transactions.
64918**
64919** These values must match SQLITE_TXN_NONE, SQLITE_TXN_READ, and
64920** SQLITE_TXN_WRITE
64921*/
64922#define TRANS_NONE 0
64923#define TRANS_READ 1
64924#define TRANS_WRITE 2
64925
64926#if TRANS_NONE!=SQLITE_TXN_NONE
64927# error wrong numeric code for no-transaction
64928#endif
64929#if TRANS_READ!=SQLITE_TXN_READ
64930# error wrong numeric code for read-transaction
64931#endif
64932#if TRANS_WRITE!=SQLITE_TXN_WRITE
64933# error wrong numeric code for write-transaction
64934#endif
64935
64936
64937/*
64938** An instance of this object represents a single database file.
64939**
64940** A single database file can be in use at the same time by two
64941** or more database connections. When two or more connections are
64942** sharing the same database file, each connection has it own
64943** private Btree object for the file and each of those Btrees points
64944** to this one BtShared object. BtShared.nRef is the number of
64945** connections currently sharing this database file.
64946**
64947** Fields in this structure are accessed under the BtShared.mutex
64948** mutex, except for nRef and pNext which are accessed under the
64949** global SQLITE_MUTEX_STATIC_MAIN mutex. The pPager field
64950** may not be modified once it is initially set as long as nRef>0.
64951** The pSchema field may be set once under BtShared.mutex and
64952** thereafter is unchanged as long as nRef>0.
64953**
64954** isPending:
64955**
64956** If a BtShared client fails to obtain a write-lock on a database
64957** table (because there exists one or more read-locks on the table),
64958** the shared-cache enters 'pending-lock' state and isPending is
64959** set to true.
64960**
64961** The shared-cache leaves the 'pending lock' state when either of
64962** the following occur:
64963**
64964** 1) The current writer (BtShared.pWriter) concludes its transaction, OR
64965** 2) The number of locks held by other connections drops to zero.
64966**
64967** while in the 'pending-lock' state, no connection may start a new
64968** transaction.
64969**
64970** This feature is included to help prevent writer-starvation.
64971*/
64972struct BtShared {
64973 Pager *pPager; /* The page cache */
64974 sqlite3 *db; /* Database connection currently using this Btree */
64975 BtCursor *pCursor; /* A list of all open cursors */
64976 MemPage *pPage1; /* First page of the database */
64977 u8 openFlags; /* Flags to sqlite3BtreeOpen() */
64978#ifndef SQLITE_OMIT_AUTOVACUUM
64979 u8 autoVacuum; /* True if auto-vacuum is enabled */
64980 u8 incrVacuum; /* True if incr-vacuum is enabled */
64981 u8 bDoTruncate; /* True to truncate db on commit */
64982#endif
64983 u8 inTransaction; /* Transaction state */
64984 u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */
64985 u8 nReserveWanted; /* Desired number of extra bytes per page */
64986 u16 btsFlags; /* Boolean parameters. See BTS_* macros below */
64987 u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
64988 u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */
64989 u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */
64990 u16 minLeaf; /* Minimum local payload in a LEAFDATA table */
64991 u32 pageSize; /* Total number of bytes on a page */
64992 u32 usableSize; /* Number of usable bytes on each page */
64993 int nTransaction; /* Number of open transactions (read + write) */
64994 u32 nPage; /* Number of pages in the database */
64995 void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */
64996 void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */
64997 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
64998 Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */
64999#ifndef SQLITE_OMIT_SHARED_CACHE
65000 int nRef; /* Number of references to this structure */
65001 BtShared *pNext; /* Next on a list of sharable BtShared structs */
65002 BtLock *pLock; /* List of locks held on this shared-btree struct */
65003 Btree *pWriter; /* Btree with currently open write transaction */
65004#endif
65005 u8 *pTmpSpace; /* Temp space sufficient to hold a single cell */
65006 int nPreformatSize; /* Size of last cell written by TransferRow() */
65007};
65008
65009/*
65010** Allowed values for BtShared.btsFlags
65011*/
65012#define BTS_READ_ONLY 0x0001 /* Underlying file is readonly */
65013#define BTS_PAGESIZE_FIXED 0x0002 /* Page size can no longer be changed */
65014#define BTS_SECURE_DELETE 0x0004 /* PRAGMA secure_delete is enabled */
65015#define BTS_OVERWRITE 0x0008 /* Overwrite deleted content with zeros */
65016#define BTS_FAST_SECURE 0x000c /* Combination of the previous two */
65017#define BTS_INITIALLY_EMPTY 0x0010 /* Database was empty at trans start */
65018#define BTS_NO_WAL 0x0020 /* Do not open write-ahead-log files */
65019#define BTS_EXCLUSIVE 0x0040 /* pWriter has an exclusive lock */
65020#define BTS_PENDING 0x0080 /* Waiting for read-locks to clear */
65021
65022/*
65023** An instance of the following structure is used to hold information
65024** about a cell. The parseCellPtr() function fills in this structure
65025** based on information extract from the raw disk page.
65026*/
65027struct CellInfo {
65028 i64 nKey; /* The key for INTKEY tables, or nPayload otherwise */
65029 u8 *pPayload; /* Pointer to the start of payload */
65030 u32 nPayload; /* Bytes of payload */
65031 u16 nLocal; /* Amount of payload held locally, not on overflow */
65032 u16 nSize; /* Size of the cell content on the main b-tree page */
65033};
65034
65035/*
65036** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
65037** this will be declared corrupt. This value is calculated based on a
65038** maximum database size of 2^31 pages a minimum fanout of 2 for a
65039** root-node and 3 for all other internal nodes.
65040**
65041** If a tree that appears to be taller than this is encountered, it is
65042** assumed that the database is corrupt.
65043*/
65044#define BTCURSOR_MAX_DEPTH 20
65045
65046/*
65047** A cursor is a pointer to a particular entry within a particular
65048** b-tree within a database file.
65049**
65050** The entry is identified by its MemPage and the index in
65051** MemPage.aCell[] of the entry.
65052**
65053** A single database file can be shared by two more database connections,
65054** but cursors cannot be shared. Each cursor is associated with a
65055** particular database connection identified BtCursor.pBtree.db.
65056**
65057** Fields in this structure are accessed under the BtShared.mutex
65058** found at self->pBt->mutex.
65059**
65060** skipNext meaning:
65061** The meaning of skipNext depends on the value of eState:
65062**
65063** eState Meaning of skipNext
65064** VALID skipNext is meaningless and is ignored
65065** INVALID skipNext is meaningless and is ignored
65066** SKIPNEXT sqlite3BtreeNext() is a no-op if skipNext>0 and
65067** sqlite3BtreePrevious() is no-op if skipNext<0.
65068** REQUIRESEEK restoreCursorPosition() restores the cursor to
65069** eState=SKIPNEXT if skipNext!=0
65070** FAULT skipNext holds the cursor fault error code.
65071*/
65072struct BtCursor {
65073 u8 eState; /* One of the CURSOR_XXX constants (see below) */
65074 u8 curFlags; /* zero or more BTCF_* flags defined below */
65075 u8 curPagerFlags; /* Flags to send to sqlite3PagerGet() */
65076 u8 hints; /* As configured by CursorSetHints() */
65077 int skipNext; /* Prev() is noop if negative. Next() is noop if positive.
65078 ** Error code if eState==CURSOR_FAULT */
65079 Btree *pBtree; /* The Btree to which this cursor belongs */
65080 Pgno *aOverflow; /* Cache of overflow page locations */
65081 void *pKey; /* Saved key that was cursor last known position */
65082 /* All fields above are zeroed when the cursor is allocated. See
65083 ** sqlite3BtreeCursorZero(). Fields that follow must be manually
65084 ** initialized. */
65085#define BTCURSOR_FIRST_UNINIT pBt /* Name of first uninitialized field */
65086 BtShared *pBt; /* The BtShared this cursor points to */
65087 BtCursor *pNext; /* Forms a linked list of all cursors */
65088 CellInfo info; /* A parse of the cell we are pointing at */
65089 i64 nKey; /* Size of pKey, or last integer key */
65090 Pgno pgnoRoot; /* The root page of this tree */
65091 i8 iPage; /* Index of current page in apPage */
65092 u8 curIntKey; /* Value of apPage[0]->intKey */
65093 u16 ix; /* Current index for apPage[iPage] */
65094 u16 aiIdx[BTCURSOR_MAX_DEPTH-1]; /* Current index in apPage[i] */
65095 struct KeyInfo *pKeyInfo; /* Arg passed to comparison function */
65096 MemPage *pPage; /* Current page */
65097 MemPage *apPage[BTCURSOR_MAX_DEPTH-1]; /* Stack of parents of current page */
65098};
65099
65100/*
65101** Legal values for BtCursor.curFlags
65102*/
65103#define BTCF_WriteFlag 0x01 /* True if a write cursor */
65104#define BTCF_ValidNKey 0x02 /* True if info.nKey is valid */
65105#define BTCF_ValidOvfl 0x04 /* True if aOverflow is valid */
65106#define BTCF_AtLast 0x08 /* Cursor is pointing ot the last entry */
65107#define BTCF_Incrblob 0x10 /* True if an incremental I/O handle */
65108#define BTCF_Multiple 0x20 /* Maybe another cursor on the same btree */
65109#define BTCF_Pinned 0x40 /* Cursor is busy and cannot be moved */
65110
65111/*
65112** Potential values for BtCursor.eState.
65113**
65114** CURSOR_INVALID:
65115** Cursor does not point to a valid entry. This can happen (for example)
65116** because the table is empty or because BtreeCursorFirst() has not been
65117** called.
65118**
65119** CURSOR_VALID:
65120** Cursor points to a valid entry. getPayload() etc. may be called.
65121**
65122** CURSOR_SKIPNEXT:
65123** Cursor is valid except that the Cursor.skipNext field is non-zero
65124** indicating that the next sqlite3BtreeNext() or sqlite3BtreePrevious()
65125** operation should be a no-op.
65126**
65127** CURSOR_REQUIRESEEK:
65128** The table that this cursor was opened on still exists, but has been
65129** modified since the cursor was last used. The cursor position is saved
65130** in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in
65131** this state, restoreCursorPosition() can be called to attempt to
65132** seek the cursor to the saved position.
65133**
65134** CURSOR_FAULT:
65135** An unrecoverable error (an I/O error or a malloc failure) has occurred
65136** on a different connection that shares the BtShared cache with this
65137** cursor. The error has left the cache in an inconsistent state.
65138** Do nothing else with this cursor. Any attempt to use the cursor
65139** should return the error code stored in BtCursor.skipNext
65140*/
65141#define CURSOR_VALID 0
65142#define CURSOR_INVALID 1
65143#define CURSOR_SKIPNEXT 2
65144#define CURSOR_REQUIRESEEK 3
65145#define CURSOR_FAULT 4
65146
65147/*
65148** The database page the PENDING_BYTE occupies. This page is never used.
65149*/
65150# define PENDING_BYTE_PAGE(pBt) PAGER_MJ_PGNO(pBt)
65151
65152/*
65153** These macros define the location of the pointer-map entry for a
65154** database page. The first argument to each is the number of usable
65155** bytes on each page of the database (often 1024). The second is the
65156** page number to look up in the pointer map.
65157**
65158** PTRMAP_PAGENO returns the database page number of the pointer-map
65159** page that stores the required pointer. PTRMAP_PTROFFSET returns
65160** the offset of the requested map entry.
65161**
65162** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
65163** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be
65164** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
65165** this test.
65166*/
65167#define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno)
65168#define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1))
65169#define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno))
65170
65171/*
65172** The pointer map is a lookup table that identifies the parent page for
65173** each child page in the database file. The parent page is the page that
65174** contains a pointer to the child. Every page in the database contains
65175** 0 or 1 parent pages. (In this context 'database page' refers
65176** to any page that is not part of the pointer map itself.) Each pointer map
65177** entry consists of a single byte 'type' and a 4 byte parent page number.
65178** The PTRMAP_XXX identifiers below are the valid types.
65179**
65180** The purpose of the pointer map is to facility moving pages from one
65181** position in the file to another as part of autovacuum. When a page
65182** is moved, the pointer in its parent must be updated to point to the
65183** new location. The pointer map is used to locate the parent page quickly.
65184**
65185** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
65186** used in this case.
65187**
65188** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
65189** is not used in this case.
65190**
65191** PTRMAP_OVERFLOW1: The database page is the first page in a list of
65192** overflow pages. The page number identifies the page that
65193** contains the cell with a pointer to this overflow page.
65194**
65195** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
65196** overflow pages. The page-number identifies the previous
65197** page in the overflow page list.
65198**
65199** PTRMAP_BTREE: The database page is a non-root btree page. The page number
65200** identifies the parent page in the btree.
65201*/
65202#define PTRMAP_ROOTPAGE 1
65203#define PTRMAP_FREEPAGE 2
65204#define PTRMAP_OVERFLOW1 3
65205#define PTRMAP_OVERFLOW2 4
65206#define PTRMAP_BTREE 5
65207
65208/* A bunch of assert() statements to check the transaction state variables
65209** of handle p (type Btree*) are internally consistent.
65210*/
65211#define btreeIntegrity(p) \
65212 assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
65213 assert( p->pBt->inTransaction>=p->inTrans );
65214
65215
65216/*
65217** The ISAUTOVACUUM macro is used within balance_nonroot() to determine
65218** if the database supports auto-vacuum or not. Because it is used
65219** within an expression that is an argument to another macro
65220** (sqliteMallocRaw), it is not possible to use conditional compilation.
65221** So, this macro is defined instead.
65222*/
65223#ifndef SQLITE_OMIT_AUTOVACUUM
65224#define ISAUTOVACUUM (pBt->autoVacuum)
65225#else
65226#define ISAUTOVACUUM 0
65227#endif
65228
65229
65230/*
65231** This structure is passed around through all the sanity checking routines
65232** in order to keep track of some global state information.
65233**
65234** The aRef[] array is allocated so that there is 1 bit for each page in
65235** the database. As the integrity-check proceeds, for each page used in
65236** the database the corresponding bit is set. This allows integrity-check to
65237** detect pages that are used twice and orphaned pages (both of which
65238** indicate corruption).
65239*/
65240typedef struct IntegrityCk IntegrityCk;
65241struct IntegrityCk {
65242 BtShared *pBt; /* The tree being checked out */
65243 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
65244 u8 *aPgRef; /* 1 bit per page in the db (see above) */
65245 Pgno nPage; /* Number of pages in the database */
65246 int mxErr; /* Stop accumulating errors when this reaches zero */
65247 int nErr; /* Number of messages written to zErrMsg so far */
65248 int bOomFault; /* A memory allocation error has occurred */
65249 const char *zPfx; /* Error message prefix */
65250 Pgno v1; /* Value for first %u substitution in zPfx */
65251 int v2; /* Value for second %d substitution in zPfx */
65252 StrAccum errMsg; /* Accumulate the error message text here */
65253 u32 *heap; /* Min-heap used for analyzing cell coverage */
65254 sqlite3 *db; /* Database connection running the check */
65255};
65256
65257/*
65258** Routines to read or write a two- and four-byte big-endian integer values.
65259*/
65260#define get2byte(x) ((x)[0]<<8 | (x)[1])
65261#define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v))
65262#define get4byte sqlite3Get4byte
65263#define put4byte sqlite3Put4byte
65264
65265/*
65266** get2byteAligned(), unlike get2byte(), requires that its argument point to a
65267** two-byte aligned address. get2bytea() is only used for accessing the
65268** cell addresses in a btree header.
65269*/
65270#if SQLITE_BYTEORDER==4321
65271# define get2byteAligned(x) (*(u16*)(x))
65272#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4008000
65273# define get2byteAligned(x) __builtin_bswap16(*(u16*)(x))
65274#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
65275# define get2byteAligned(x) _byteswap_ushort(*(u16*)(x))
65276#else
65277# define get2byteAligned(x) ((x)[0]<<8 | (x)[1])
65278#endif
65279
65280/************** End of btreeInt.h ********************************************/
65281/************** Continuing where we left off in btmutex.c ********************/
65282#ifndef SQLITE_OMIT_SHARED_CACHE
65283#if SQLITE_THREADSAFE
65284
65285/*
65286** Obtain the BtShared mutex associated with B-Tree handle p. Also,
65287** set BtShared.db to the database handle associated with p and the
65288** p->locked boolean to true.
65289*/
65290static void lockBtreeMutex(Btree *p){
65291 assert( p->locked==0 );
65292 assert( sqlite3_mutex_notheld(p->pBt->mutex) );
65293 assert( sqlite3_mutex_held(p->db->mutex) );
65294
65295 sqlite3_mutex_enter(p: p->pBt->mutex);
65296 p->pBt->db = p->db;
65297 p->locked = 1;
65298}
65299
65300/*
65301** Release the BtShared mutex associated with B-Tree handle p and
65302** clear the p->locked boolean.
65303*/
65304static void SQLITE_NOINLINE unlockBtreeMutex(Btree *p){
65305 BtShared *pBt = p->pBt;
65306 assert( p->locked==1 );
65307 assert( sqlite3_mutex_held(pBt->mutex) );
65308 assert( sqlite3_mutex_held(p->db->mutex) );
65309 assert( p->db==pBt->db );
65310
65311 sqlite3_mutex_leave(p: pBt->mutex);
65312 p->locked = 0;
65313}
65314
65315/* Forward reference */
65316static void SQLITE_NOINLINE btreeLockCarefully(Btree *p);
65317
65318/*
65319** Enter a mutex on the given BTree object.
65320**
65321** If the object is not sharable, then no mutex is ever required
65322** and this routine is a no-op. The underlying mutex is non-recursive.
65323** But we keep a reference count in Btree.wantToLock so the behavior
65324** of this interface is recursive.
65325**
65326** To avoid deadlocks, multiple Btrees are locked in the same order
65327** by all database connections. The p->pNext is a list of other
65328** Btrees belonging to the same database connection as the p Btree
65329** which need to be locked after p. If we cannot get a lock on
65330** p, then first unlock all of the others on p->pNext, then wait
65331** for the lock to become available on p, then relock all of the
65332** subsequent Btrees that desire a lock.
65333*/
65334SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
65335 /* Some basic sanity checking on the Btree. The list of Btrees
65336 ** connected by pNext and pPrev should be in sorted order by
65337 ** Btree.pBt value. All elements of the list should belong to
65338 ** the same connection. Only shared Btrees are on the list. */
65339 assert( p->pNext==0 || p->pNext->pBt>p->pBt );
65340 assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
65341 assert( p->pNext==0 || p->pNext->db==p->db );
65342 assert( p->pPrev==0 || p->pPrev->db==p->db );
65343 assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
65344
65345 /* Check for locking consistency */
65346 assert( !p->locked || p->wantToLock>0 );
65347 assert( p->sharable || p->wantToLock==0 );
65348
65349 /* We should already hold a lock on the database connection */
65350 assert( sqlite3_mutex_held(p->db->mutex) );
65351
65352 /* Unless the database is sharable and unlocked, then BtShared.db
65353 ** should already be set correctly. */
65354 assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
65355
65356 if( !p->sharable ) return;
65357 p->wantToLock++;
65358 if( p->locked ) return;
65359 btreeLockCarefully(p);
65360}
65361
65362/* This is a helper function for sqlite3BtreeLock(). By moving
65363** complex, but seldom used logic, out of sqlite3BtreeLock() and
65364** into this routine, we avoid unnecessary stack pointer changes
65365** and thus help the sqlite3BtreeLock() routine to run much faster
65366** in the common case.
65367*/
65368static void SQLITE_NOINLINE btreeLockCarefully(Btree *p){
65369 Btree *pLater;
65370
65371 /* In most cases, we should be able to acquire the lock we
65372 ** want without having to go through the ascending lock
65373 ** procedure that follows. Just be sure not to block.
65374 */
65375 if( sqlite3_mutex_try(p: p->pBt->mutex)==SQLITE_OK ){
65376 p->pBt->db = p->db;
65377 p->locked = 1;
65378 return;
65379 }
65380
65381 /* To avoid deadlock, first release all locks with a larger
65382 ** BtShared address. Then acquire our lock. Then reacquire
65383 ** the other BtShared locks that we used to hold in ascending
65384 ** order.
65385 */
65386 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
65387 assert( pLater->sharable );
65388 assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
65389 assert( !pLater->locked || pLater->wantToLock>0 );
65390 if( pLater->locked ){
65391 unlockBtreeMutex(p: pLater);
65392 }
65393 }
65394 lockBtreeMutex(p);
65395 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
65396 if( pLater->wantToLock ){
65397 lockBtreeMutex(p: pLater);
65398 }
65399 }
65400}
65401
65402
65403/*
65404** Exit the recursive mutex on a Btree.
65405*/
65406SQLITE_PRIVATE void sqlite3BtreeLeave(Btree *p){
65407 assert( sqlite3_mutex_held(p->db->mutex) );
65408 if( p->sharable ){
65409 assert( p->wantToLock>0 );
65410 p->wantToLock--;
65411 if( p->wantToLock==0 ){
65412 unlockBtreeMutex(p);
65413 }
65414 }
65415}
65416
65417#ifndef NDEBUG
65418/*
65419** Return true if the BtShared mutex is held on the btree, or if the
65420** B-Tree is not marked as sharable.
65421**
65422** This routine is used only from within assert() statements.
65423*/
65424SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree *p){
65425 assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
65426 assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
65427 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
65428 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
65429
65430 return (p->sharable==0 || p->locked);
65431}
65432#endif
65433
65434
65435/*
65436** Enter the mutex on every Btree associated with a database
65437** connection. This is needed (for example) prior to parsing
65438** a statement since we will be comparing table and column names
65439** against all schemas and we do not want those schemas being
65440** reset out from under us.
65441**
65442** There is a corresponding leave-all procedures.
65443**
65444** Enter the mutexes in accending order by BtShared pointer address
65445** to avoid the possibility of deadlock when two threads with
65446** two or more btrees in common both try to lock all their btrees
65447** at the same instant.
65448*/
65449static void SQLITE_NOINLINE btreeEnterAll(sqlite3 *db){
65450 int i;
65451 int skipOk = 1;
65452 Btree *p;
65453 assert( sqlite3_mutex_held(db->mutex) );
65454 for(i=0; i<db->nDb; i++){
65455 p = db->aDb[i].pBt;
65456 if( p && p->sharable ){
65457 sqlite3BtreeEnter(p);
65458 skipOk = 0;
65459 }
65460 }
65461 db->noSharedCache = skipOk;
65462}
65463SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
65464 if( db->noSharedCache==0 ) btreeEnterAll(db);
65465}
65466static void SQLITE_NOINLINE btreeLeaveAll(sqlite3 *db){
65467 int i;
65468 Btree *p;
65469 assert( sqlite3_mutex_held(db->mutex) );
65470 for(i=0; i<db->nDb; i++){
65471 p = db->aDb[i].pBt;
65472 if( p ) sqlite3BtreeLeave(p);
65473 }
65474}
65475SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
65476 if( db->noSharedCache==0 ) btreeLeaveAll(db);
65477}
65478
65479#ifndef NDEBUG
65480/*
65481** Return true if the current thread holds the database connection
65482** mutex and all required BtShared mutexes.
65483**
65484** This routine is used inside assert() statements only.
65485*/
65486SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3 *db){
65487 int i;
65488 if( !sqlite3_mutex_held(db->mutex) ){
65489 return 0;
65490 }
65491 for(i=0; i<db->nDb; i++){
65492 Btree *p;
65493 p = db->aDb[i].pBt;
65494 if( p && p->sharable &&
65495 (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
65496 return 0;
65497 }
65498 }
65499 return 1;
65500}
65501#endif /* NDEBUG */
65502
65503#ifndef NDEBUG
65504/*
65505** Return true if the correct mutexes are held for accessing the
65506** db->aDb[iDb].pSchema structure. The mutexes required for schema
65507** access are:
65508**
65509** (1) The mutex on db
65510** (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
65511**
65512** If pSchema is not NULL, then iDb is computed from pSchema and
65513** db using sqlite3SchemaToIndex().
65514*/
65515SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3 *db, int iDb, Schema *pSchema){
65516 Btree *p;
65517 assert( db!=0 );
65518 if( pSchema ) iDb = sqlite3SchemaToIndex(db, pSchema);
65519 assert( iDb>=0 && iDb<db->nDb );
65520 if( !sqlite3_mutex_held(db->mutex) ) return 0;
65521 if( iDb==1 ) return 1;
65522 p = db->aDb[iDb].pBt;
65523 assert( p!=0 );
65524 return p->sharable==0 || p->locked==1;
65525}
65526#endif /* NDEBUG */
65527
65528#else /* SQLITE_THREADSAFE>0 above. SQLITE_THREADSAFE==0 below */
65529/*
65530** The following are special cases for mutex enter routines for use
65531** in single threaded applications that use shared cache. Except for
65532** these two routines, all mutex operations are no-ops in that case and
65533** are null #defines in btree.h.
65534**
65535** If shared cache is disabled, then all btree mutex routines, including
65536** the ones below, are no-ops and are null #defines in btree.h.
65537*/
65538
65539SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
65540 p->pBt->db = p->db;
65541}
65542SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
65543 int i;
65544 for(i=0; i<db->nDb; i++){
65545 Btree *p = db->aDb[i].pBt;
65546 if( p ){
65547 p->pBt->db = p->db;
65548 }
65549 }
65550}
65551#endif /* if SQLITE_THREADSAFE */
65552
65553#ifndef SQLITE_OMIT_INCRBLOB
65554/*
65555** Enter a mutex on a Btree given a cursor owned by that Btree.
65556**
65557** These entry points are used by incremental I/O only. Enter() is required
65558** any time OMIT_SHARED_CACHE is not defined, regardless of whether or not
65559** the build is threadsafe. Leave() is only required by threadsafe builds.
65560*/
65561SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor *pCur){
65562 sqlite3BtreeEnter(p: pCur->pBtree);
65563}
65564# if SQLITE_THREADSAFE
65565SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor *pCur){
65566 sqlite3BtreeLeave(p: pCur->pBtree);
65567}
65568# endif
65569#endif /* ifndef SQLITE_OMIT_INCRBLOB */
65570
65571#endif /* ifndef SQLITE_OMIT_SHARED_CACHE */
65572
65573/************** End of btmutex.c *********************************************/
65574/************** Begin file btree.c *******************************************/
65575/*
65576** 2004 April 6
65577**
65578** The author disclaims copyright to this source code. In place of
65579** a legal notice, here is a blessing:
65580**
65581** May you do good and not evil.
65582** May you find forgiveness for yourself and forgive others.
65583** May you share freely, never taking more than you give.
65584**
65585*************************************************************************
65586** This file implements an external (disk-based) database using BTrees.
65587** See the header comment on "btreeInt.h" for additional information.
65588** Including a description of file format and an overview of operation.
65589*/
65590/* #include "btreeInt.h" */
65591
65592/*
65593** The header string that appears at the beginning of every
65594** SQLite database.
65595*/
65596static const char zMagicHeader[] = SQLITE_FILE_HEADER;
65597
65598/*
65599** Set this global variable to 1 to enable tracing using the TRACE
65600** macro.
65601*/
65602#if 0
65603int sqlite3BtreeTrace=1; /* True to enable tracing */
65604# define TRACE(X) if(sqlite3BtreeTrace){printf X;fflush(stdout);}
65605#else
65606# define TRACE(X)
65607#endif
65608
65609/*
65610** Extract a 2-byte big-endian integer from an array of unsigned bytes.
65611** But if the value is zero, make it 65536.
65612**
65613** This routine is used to extract the "offset to cell content area" value
65614** from the header of a btree page. If the page size is 65536 and the page
65615** is empty, the offset should be 65536, but the 2-byte value stores zero.
65616** This routine makes the necessary adjustment to 65536.
65617*/
65618#define get2byteNotZero(X) (((((int)get2byte(X))-1)&0xffff)+1)
65619
65620/*
65621** Values passed as the 5th argument to allocateBtreePage()
65622*/
65623#define BTALLOC_ANY 0 /* Allocate any page */
65624#define BTALLOC_EXACT 1 /* Allocate exact page if possible */
65625#define BTALLOC_LE 2 /* Allocate any page <= the parameter */
65626
65627/*
65628** Macro IfNotOmitAV(x) returns (x) if SQLITE_OMIT_AUTOVACUUM is not
65629** defined, or 0 if it is. For example:
65630**
65631** bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum);
65632*/
65633#ifndef SQLITE_OMIT_AUTOVACUUM
65634#define IfNotOmitAV(expr) (expr)
65635#else
65636#define IfNotOmitAV(expr) 0
65637#endif
65638
65639#ifndef SQLITE_OMIT_SHARED_CACHE
65640/*
65641** A list of BtShared objects that are eligible for participation
65642** in shared cache. This variable has file scope during normal builds,
65643** but the test harness needs to access it so we make it global for
65644** test builds.
65645**
65646** Access to this variable is protected by SQLITE_MUTEX_STATIC_MAIN.
65647*/
65648#ifdef SQLITE_TEST
65649SQLITE_PRIVATE BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
65650#else
65651static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
65652#endif
65653#endif /* SQLITE_OMIT_SHARED_CACHE */
65654
65655#ifndef SQLITE_OMIT_SHARED_CACHE
65656/*
65657** Enable or disable the shared pager and schema features.
65658**
65659** This routine has no effect on existing database connections.
65660** The shared cache setting effects only future calls to
65661** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
65662*/
65663SQLITE_API int sqlite3_enable_shared_cache(int enable){
65664 sqlite3GlobalConfig.sharedCacheEnabled = enable;
65665 return SQLITE_OK;
65666}
65667#endif
65668
65669
65670
65671#ifdef SQLITE_OMIT_SHARED_CACHE
65672 /*
65673 ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(),
65674 ** and clearAllSharedCacheTableLocks()
65675 ** manipulate entries in the BtShared.pLock linked list used to store
65676 ** shared-cache table level locks. If the library is compiled with the
65677 ** shared-cache feature disabled, then there is only ever one user
65678 ** of each BtShared structure and so this locking is not necessary.
65679 ** So define the lock related functions as no-ops.
65680 */
65681 #define querySharedCacheTableLock(a,b,c) SQLITE_OK
65682 #define setSharedCacheTableLock(a,b,c) SQLITE_OK
65683 #define clearAllSharedCacheTableLocks(a)
65684 #define downgradeAllSharedCacheTableLocks(a)
65685 #define hasSharedCacheTableLock(a,b,c,d) 1
65686 #define hasReadConflicts(a, b) 0
65687#endif
65688
65689#ifdef SQLITE_DEBUG
65690/*
65691** Return and reset the seek counter for a Btree object.
65692*/
65693SQLITE_PRIVATE sqlite3_uint64 sqlite3BtreeSeekCount(Btree *pBt){
65694 u64 n = pBt->nSeek;
65695 pBt->nSeek = 0;
65696 return n;
65697}
65698#endif
65699
65700/*
65701** Implementation of the SQLITE_CORRUPT_PAGE() macro. Takes a single
65702** (MemPage*) as an argument. The (MemPage*) must not be NULL.
65703**
65704** If SQLITE_DEBUG is not defined, then this macro is equivalent to
65705** SQLITE_CORRUPT_BKPT. Or, if SQLITE_DEBUG is set, then the log message
65706** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented
65707** with the page number and filename associated with the (MemPage*).
65708*/
65709#ifdef SQLITE_DEBUG
65710int corruptPageError(int lineno, MemPage *p){
65711 char *zMsg;
65712 sqlite3BeginBenignMalloc();
65713 zMsg = sqlite3_mprintf("database corruption page %d of %s",
65714 (int)p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0)
65715 );
65716 sqlite3EndBenignMalloc();
65717 if( zMsg ){
65718 sqlite3ReportError(SQLITE_CORRUPT, lineno, zMsg);
65719 }
65720 sqlite3_free(zMsg);
65721 return SQLITE_CORRUPT_BKPT;
65722}
65723# define SQLITE_CORRUPT_PAGE(pMemPage) corruptPageError(__LINE__, pMemPage)
65724#else
65725# define SQLITE_CORRUPT_PAGE(pMemPage) SQLITE_CORRUPT_PGNO(pMemPage->pgno)
65726#endif
65727
65728#ifndef SQLITE_OMIT_SHARED_CACHE
65729
65730#ifdef SQLITE_DEBUG
65731/*
65732**** This function is only used as part of an assert() statement. ***
65733**
65734** Check to see if pBtree holds the required locks to read or write to the
65735** table with root page iRoot. Return 1 if it does and 0 if not.
65736**
65737** For example, when writing to a table with root-page iRoot via
65738** Btree connection pBtree:
65739**
65740** assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) );
65741**
65742** When writing to an index that resides in a sharable database, the
65743** caller should have first obtained a lock specifying the root page of
65744** the corresponding table. This makes things a bit more complicated,
65745** as this module treats each table as a separate structure. To determine
65746** the table corresponding to the index being written, this
65747** function has to search through the database schema.
65748**
65749** Instead of a lock on the table/index rooted at page iRoot, the caller may
65750** hold a write-lock on the schema table (root page 1). This is also
65751** acceptable.
65752*/
65753static int hasSharedCacheTableLock(
65754 Btree *pBtree, /* Handle that must hold lock */
65755 Pgno iRoot, /* Root page of b-tree */
65756 int isIndex, /* True if iRoot is the root of an index b-tree */
65757 int eLockType /* Required lock type (READ_LOCK or WRITE_LOCK) */
65758){
65759 Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
65760 Pgno iTab = 0;
65761 BtLock *pLock;
65762
65763 /* If this database is not shareable, or if the client is reading
65764 ** and has the read-uncommitted flag set, then no lock is required.
65765 ** Return true immediately.
65766 */
65767 if( (pBtree->sharable==0)
65768 || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommit))
65769 ){
65770 return 1;
65771 }
65772
65773 /* If the client is reading or writing an index and the schema is
65774 ** not loaded, then it is too difficult to actually check to see if
65775 ** the correct locks are held. So do not bother - just return true.
65776 ** This case does not come up very often anyhow.
65777 */
65778 if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){
65779 return 1;
65780 }
65781
65782 /* Figure out the root-page that the lock should be held on. For table
65783 ** b-trees, this is just the root page of the b-tree being read or
65784 ** written. For index b-trees, it is the root page of the associated
65785 ** table. */
65786 if( isIndex ){
65787 HashElem *p;
65788 int bSeen = 0;
65789 for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
65790 Index *pIdx = (Index *)sqliteHashData(p);
65791 if( pIdx->tnum==(int)iRoot ){
65792 if( bSeen ){
65793 /* Two or more indexes share the same root page. There must
65794 ** be imposter tables. So just return true. The assert is not
65795 ** useful in that case. */
65796 return 1;
65797 }
65798 iTab = pIdx->pTable->tnum;
65799 bSeen = 1;
65800 }
65801 }
65802 }else{
65803 iTab = iRoot;
65804 }
65805
65806 /* Search for the required lock. Either a write-lock on root-page iTab, a
65807 ** write-lock on the schema table, or (if the client is reading) a
65808 ** read-lock on iTab will suffice. Return 1 if any of these are found. */
65809 for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
65810 if( pLock->pBtree==pBtree
65811 && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
65812 && pLock->eLock>=eLockType
65813 ){
65814 return 1;
65815 }
65816 }
65817
65818 /* Failed to find the required lock. */
65819 return 0;
65820}
65821#endif /* SQLITE_DEBUG */
65822
65823#ifdef SQLITE_DEBUG
65824/*
65825**** This function may be used as part of assert() statements only. ****
65826**
65827** Return true if it would be illegal for pBtree to write into the
65828** table or index rooted at iRoot because other shared connections are
65829** simultaneously reading that same table or index.
65830**
65831** It is illegal for pBtree to write if some other Btree object that
65832** shares the same BtShared object is currently reading or writing
65833** the iRoot table. Except, if the other Btree object has the
65834** read-uncommitted flag set, then it is OK for the other object to
65835** have a read cursor.
65836**
65837** For example, before writing to any part of the table or index
65838** rooted at page iRoot, one should call:
65839**
65840** assert( !hasReadConflicts(pBtree, iRoot) );
65841*/
65842static int hasReadConflicts(Btree *pBtree, Pgno iRoot){
65843 BtCursor *p;
65844 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
65845 if( p->pgnoRoot==iRoot
65846 && p->pBtree!=pBtree
65847 && 0==(p->pBtree->db->flags & SQLITE_ReadUncommit)
65848 ){
65849 return 1;
65850 }
65851 }
65852 return 0;
65853}
65854#endif /* #ifdef SQLITE_DEBUG */
65855
65856/*
65857** Query to see if Btree handle p may obtain a lock of type eLock
65858** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
65859** SQLITE_OK if the lock may be obtained (by calling
65860** setSharedCacheTableLock()), or SQLITE_LOCKED if not.
65861*/
65862static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){
65863 BtShared *pBt = p->pBt;
65864 BtLock *pIter;
65865
65866 assert( sqlite3BtreeHoldsMutex(p) );
65867 assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
65868 assert( p->db!=0 );
65869 assert( !(p->db->flags&SQLITE_ReadUncommit)||eLock==WRITE_LOCK||iTab==1 );
65870
65871 /* If requesting a write-lock, then the Btree must have an open write
65872 ** transaction on this file. And, obviously, for this to be so there
65873 ** must be an open write transaction on the file itself.
65874 */
65875 assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
65876 assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
65877
65878 /* This routine is a no-op if the shared-cache is not enabled */
65879 if( !p->sharable ){
65880 return SQLITE_OK;
65881 }
65882
65883 /* If some other connection is holding an exclusive lock, the
65884 ** requested lock may not be obtained.
65885 */
65886 if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
65887 sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
65888 return SQLITE_LOCKED_SHAREDCACHE;
65889 }
65890
65891 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
65892 /* The condition (pIter->eLock!=eLock) in the following if(...)
65893 ** statement is a simplification of:
65894 **
65895 ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
65896 **
65897 ** since we know that if eLock==WRITE_LOCK, then no other connection
65898 ** may hold a WRITE_LOCK on any table in this file (since there can
65899 ** only be a single writer).
65900 */
65901 assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
65902 assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
65903 if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
65904 sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
65905 if( eLock==WRITE_LOCK ){
65906 assert( p==pBt->pWriter );
65907 pBt->btsFlags |= BTS_PENDING;
65908 }
65909 return SQLITE_LOCKED_SHAREDCACHE;
65910 }
65911 }
65912 return SQLITE_OK;
65913}
65914#endif /* !SQLITE_OMIT_SHARED_CACHE */
65915
65916#ifndef SQLITE_OMIT_SHARED_CACHE
65917/*
65918** Add a lock on the table with root-page iTable to the shared-btree used
65919** by Btree handle p. Parameter eLock must be either READ_LOCK or
65920** WRITE_LOCK.
65921**
65922** This function assumes the following:
65923**
65924** (a) The specified Btree object p is connected to a sharable
65925** database (one with the BtShared.sharable flag set), and
65926**
65927** (b) No other Btree objects hold a lock that conflicts
65928** with the requested lock (i.e. querySharedCacheTableLock() has
65929** already been called and returned SQLITE_OK).
65930**
65931** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM
65932** is returned if a malloc attempt fails.
65933*/
65934static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){
65935 BtShared *pBt = p->pBt;
65936 BtLock *pLock = 0;
65937 BtLock *pIter;
65938
65939 assert( sqlite3BtreeHoldsMutex(p) );
65940 assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
65941 assert( p->db!=0 );
65942
65943 /* A connection with the read-uncommitted flag set will never try to
65944 ** obtain a read-lock using this function. The only read-lock obtained
65945 ** by a connection in read-uncommitted mode is on the sqlite_schema
65946 ** table, and that lock is obtained in BtreeBeginTrans(). */
65947 assert( 0==(p->db->flags&SQLITE_ReadUncommit) || eLock==WRITE_LOCK );
65948
65949 /* This function should only be called on a sharable b-tree after it
65950 ** has been determined that no other b-tree holds a conflicting lock. */
65951 assert( p->sharable );
65952 assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) );
65953
65954 /* First search the list for an existing lock on this table. */
65955 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
65956 if( pIter->iTable==iTable && pIter->pBtree==p ){
65957 pLock = pIter;
65958 break;
65959 }
65960 }
65961
65962 /* If the above search did not find a BtLock struct associating Btree p
65963 ** with table iTable, allocate one and link it into the list.
65964 */
65965 if( !pLock ){
65966 pLock = (BtLock *)sqlite3MallocZero(n: sizeof(BtLock));
65967 if( !pLock ){
65968 return SQLITE_NOMEM_BKPT;
65969 }
65970 pLock->iTable = iTable;
65971 pLock->pBtree = p;
65972 pLock->pNext = pBt->pLock;
65973 pBt->pLock = pLock;
65974 }
65975
65976 /* Set the BtLock.eLock variable to the maximum of the current lock
65977 ** and the requested lock. This means if a write-lock was already held
65978 ** and a read-lock requested, we don't incorrectly downgrade the lock.
65979 */
65980 assert( WRITE_LOCK>READ_LOCK );
65981 if( eLock>pLock->eLock ){
65982 pLock->eLock = eLock;
65983 }
65984
65985 return SQLITE_OK;
65986}
65987#endif /* !SQLITE_OMIT_SHARED_CACHE */
65988
65989#ifndef SQLITE_OMIT_SHARED_CACHE
65990/*
65991** Release all the table locks (locks obtained via calls to
65992** the setSharedCacheTableLock() procedure) held by Btree object p.
65993**
65994** This function assumes that Btree p has an open read or write
65995** transaction. If it does not, then the BTS_PENDING flag
65996** may be incorrectly cleared.
65997*/
65998static void clearAllSharedCacheTableLocks(Btree *p){
65999 BtShared *pBt = p->pBt;
66000 BtLock **ppIter = &pBt->pLock;
66001
66002 assert( sqlite3BtreeHoldsMutex(p) );
66003 assert( p->sharable || 0==*ppIter );
66004 assert( p->inTrans>0 );
66005
66006 while( *ppIter ){
66007 BtLock *pLock = *ppIter;
66008 assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
66009 assert( pLock->pBtree->inTrans>=pLock->eLock );
66010 if( pLock->pBtree==p ){
66011 *ppIter = pLock->pNext;
66012 assert( pLock->iTable!=1 || pLock==&p->lock );
66013 if( pLock->iTable!=1 ){
66014 sqlite3_free(p: pLock);
66015 }
66016 }else{
66017 ppIter = &pLock->pNext;
66018 }
66019 }
66020
66021 assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
66022 if( pBt->pWriter==p ){
66023 pBt->pWriter = 0;
66024 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
66025 }else if( pBt->nTransaction==2 ){
66026 /* This function is called when Btree p is concluding its
66027 ** transaction. If there currently exists a writer, and p is not
66028 ** that writer, then the number of locks held by connections other
66029 ** than the writer must be about to drop to zero. In this case
66030 ** set the BTS_PENDING flag to 0.
66031 **
66032 ** If there is not currently a writer, then BTS_PENDING must
66033 ** be zero already. So this next line is harmless in that case.
66034 */
66035 pBt->btsFlags &= ~BTS_PENDING;
66036 }
66037}
66038
66039/*
66040** This function changes all write-locks held by Btree p into read-locks.
66041*/
66042static void downgradeAllSharedCacheTableLocks(Btree *p){
66043 BtShared *pBt = p->pBt;
66044 if( pBt->pWriter==p ){
66045 BtLock *pLock;
66046 pBt->pWriter = 0;
66047 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
66048 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
66049 assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
66050 pLock->eLock = READ_LOCK;
66051 }
66052 }
66053}
66054
66055#endif /* SQLITE_OMIT_SHARED_CACHE */
66056
66057static void releasePage(MemPage *pPage); /* Forward reference */
66058static void releasePageOne(MemPage *pPage); /* Forward reference */
66059static void releasePageNotNull(MemPage *pPage); /* Forward reference */
66060
66061/*
66062***** This routine is used inside of assert() only ****
66063**
66064** Verify that the cursor holds the mutex on its BtShared
66065*/
66066#ifdef SQLITE_DEBUG
66067static int cursorHoldsMutex(BtCursor *p){
66068 return sqlite3_mutex_held(p->pBt->mutex);
66069}
66070
66071/* Verify that the cursor and the BtShared agree about what is the current
66072** database connetion. This is important in shared-cache mode. If the database
66073** connection pointers get out-of-sync, it is possible for routines like
66074** btreeInitPage() to reference an stale connection pointer that references a
66075** a connection that has already closed. This routine is used inside assert()
66076** statements only and for the purpose of double-checking that the btree code
66077** does keep the database connection pointers up-to-date.
66078*/
66079static int cursorOwnsBtShared(BtCursor *p){
66080 assert( cursorHoldsMutex(p) );
66081 return (p->pBtree->db==p->pBt->db);
66082}
66083#endif
66084
66085/*
66086** Invalidate the overflow cache of the cursor passed as the first argument.
66087** on the shared btree structure pBt.
66088*/
66089#define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl)
66090
66091/*
66092** Invalidate the overflow page-list cache for all cursors opened
66093** on the shared btree structure pBt.
66094*/
66095static void invalidateAllOverflowCache(BtShared *pBt){
66096 BtCursor *p;
66097 assert( sqlite3_mutex_held(pBt->mutex) );
66098 for(p=pBt->pCursor; p; p=p->pNext){
66099 invalidateOverflowCache(p);
66100 }
66101}
66102
66103#ifndef SQLITE_OMIT_INCRBLOB
66104/*
66105** This function is called before modifying the contents of a table
66106** to invalidate any incrblob cursors that are open on the
66107** row or one of the rows being modified.
66108**
66109** If argument isClearTable is true, then the entire contents of the
66110** table is about to be deleted. In this case invalidate all incrblob
66111** cursors open on any row within the table with root-page pgnoRoot.
66112**
66113** Otherwise, if argument isClearTable is false, then the row with
66114** rowid iRow is being replaced or deleted. In this case invalidate
66115** only those incrblob cursors open on that specific row.
66116*/
66117static void invalidateIncrblobCursors(
66118 Btree *pBtree, /* The database file to check */
66119 Pgno pgnoRoot, /* The table that might be changing */
66120 i64 iRow, /* The rowid that might be changing */
66121 int isClearTable /* True if all rows are being deleted */
66122){
66123 BtCursor *p;
66124 assert( pBtree->hasIncrblobCur );
66125 assert( sqlite3BtreeHoldsMutex(pBtree) );
66126 pBtree->hasIncrblobCur = 0;
66127 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
66128 if( (p->curFlags & BTCF_Incrblob)!=0 ){
66129 pBtree->hasIncrblobCur = 1;
66130 if( p->pgnoRoot==pgnoRoot && (isClearTable || p->info.nKey==iRow) ){
66131 p->eState = CURSOR_INVALID;
66132 }
66133 }
66134 }
66135}
66136
66137#else
66138 /* Stub function when INCRBLOB is omitted */
66139 #define invalidateIncrblobCursors(w,x,y,z)
66140#endif /* SQLITE_OMIT_INCRBLOB */
66141
66142/*
66143** Set bit pgno of the BtShared.pHasContent bitvec. This is called
66144** when a page that previously contained data becomes a free-list leaf
66145** page.
66146**
66147** The BtShared.pHasContent bitvec exists to work around an obscure
66148** bug caused by the interaction of two useful IO optimizations surrounding
66149** free-list leaf pages:
66150**
66151** 1) When all data is deleted from a page and the page becomes
66152** a free-list leaf page, the page is not written to the database
66153** (as free-list leaf pages contain no meaningful data). Sometimes
66154** such a page is not even journalled (as it will not be modified,
66155** why bother journalling it?).
66156**
66157** 2) When a free-list leaf page is reused, its content is not read
66158** from the database or written to the journal file (why should it
66159** be, if it is not at all meaningful?).
66160**
66161** By themselves, these optimizations work fine and provide a handy
66162** performance boost to bulk delete or insert operations. However, if
66163** a page is moved to the free-list and then reused within the same
66164** transaction, a problem comes up. If the page is not journalled when
66165** it is moved to the free-list and it is also not journalled when it
66166** is extracted from the free-list and reused, then the original data
66167** may be lost. In the event of a rollback, it may not be possible
66168** to restore the database to its original configuration.
66169**
66170** The solution is the BtShared.pHasContent bitvec. Whenever a page is
66171** moved to become a free-list leaf page, the corresponding bit is
66172** set in the bitvec. Whenever a leaf page is extracted from the free-list,
66173** optimization 2 above is omitted if the corresponding bit is already
66174** set in BtShared.pHasContent. The contents of the bitvec are cleared
66175** at the end of every transaction.
66176*/
66177static int btreeSetHasContent(BtShared *pBt, Pgno pgno){
66178 int rc = SQLITE_OK;
66179 if( !pBt->pHasContent ){
66180 assert( pgno<=pBt->nPage );
66181 pBt->pHasContent = sqlite3BitvecCreate(iSize: pBt->nPage);
66182 if( !pBt->pHasContent ){
66183 rc = SQLITE_NOMEM_BKPT;
66184 }
66185 }
66186 if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(p: pBt->pHasContent) ){
66187 rc = sqlite3BitvecSet(p: pBt->pHasContent, i: pgno);
66188 }
66189 return rc;
66190}
66191
66192/*
66193** Query the BtShared.pHasContent vector.
66194**
66195** This function is called when a free-list leaf page is removed from the
66196** free-list for reuse. It returns false if it is safe to retrieve the
66197** page from the pager layer with the 'no-content' flag set. True otherwise.
66198*/
66199static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
66200 Bitvec *p = pBt->pHasContent;
66201 return p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTestNotNull(p, i: pgno));
66202}
66203
66204/*
66205** Clear (destroy) the BtShared.pHasContent bitvec. This should be
66206** invoked at the conclusion of each write-transaction.
66207*/
66208static void btreeClearHasContent(BtShared *pBt){
66209 sqlite3BitvecDestroy(p: pBt->pHasContent);
66210 pBt->pHasContent = 0;
66211}
66212
66213/*
66214** Release all of the apPage[] pages for a cursor.
66215*/
66216static void btreeReleaseAllCursorPages(BtCursor *pCur){
66217 int i;
66218 if( pCur->iPage>=0 ){
66219 for(i=0; i<pCur->iPage; i++){
66220 releasePageNotNull(pPage: pCur->apPage[i]);
66221 }
66222 releasePageNotNull(pPage: pCur->pPage);
66223 pCur->iPage = -1;
66224 }
66225}
66226
66227/*
66228** The cursor passed as the only argument must point to a valid entry
66229** when this function is called (i.e. have eState==CURSOR_VALID). This
66230** function saves the current cursor key in variables pCur->nKey and
66231** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error
66232** code otherwise.
66233**
66234** If the cursor is open on an intkey table, then the integer key
66235** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to
66236** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is
66237** set to point to a malloced buffer pCur->nKey bytes in size containing
66238** the key.
66239*/
66240static int saveCursorKey(BtCursor *pCur){
66241 int rc = SQLITE_OK;
66242 assert( CURSOR_VALID==pCur->eState );
66243 assert( 0==pCur->pKey );
66244 assert( cursorHoldsMutex(pCur) );
66245
66246 if( pCur->curIntKey ){
66247 /* Only the rowid is required for a table btree */
66248 pCur->nKey = sqlite3BtreeIntegerKey(pCur);
66249 }else{
66250 /* For an index btree, save the complete key content. It is possible
66251 ** that the current key is corrupt. In that case, it is possible that
66252 ** the sqlite3VdbeRecordUnpack() function may overread the buffer by
66253 ** up to the size of 1 varint plus 1 8-byte value when the cursor
66254 ** position is restored. Hence the 17 bytes of padding allocated
66255 ** below. */
66256 void *pKey;
66257 pCur->nKey = sqlite3BtreePayloadSize(pCur);
66258 pKey = sqlite3Malloc( n: pCur->nKey + 9 + 8 );
66259 if( pKey ){
66260 rc = sqlite3BtreePayload(pCur, offset: 0, amt: (int)pCur->nKey, pKey);
66261 if( rc==SQLITE_OK ){
66262 memset(s: ((u8*)pKey)+pCur->nKey, c: 0, n: 9+8);
66263 pCur->pKey = pKey;
66264 }else{
66265 sqlite3_free(p: pKey);
66266 }
66267 }else{
66268 rc = SQLITE_NOMEM_BKPT;
66269 }
66270 }
66271 assert( !pCur->curIntKey || !pCur->pKey );
66272 return rc;
66273}
66274
66275/*
66276** Save the current cursor position in the variables BtCursor.nKey
66277** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
66278**
66279** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID)
66280** prior to calling this routine.
66281*/
66282static int saveCursorPosition(BtCursor *pCur){
66283 int rc;
66284
66285 assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState );
66286 assert( 0==pCur->pKey );
66287 assert( cursorHoldsMutex(pCur) );
66288
66289 if( pCur->curFlags & BTCF_Pinned ){
66290 return SQLITE_CONSTRAINT_PINNED;
66291 }
66292 if( pCur->eState==CURSOR_SKIPNEXT ){
66293 pCur->eState = CURSOR_VALID;
66294 }else{
66295 pCur->skipNext = 0;
66296 }
66297
66298 rc = saveCursorKey(pCur);
66299 if( rc==SQLITE_OK ){
66300 btreeReleaseAllCursorPages(pCur);
66301 pCur->eState = CURSOR_REQUIRESEEK;
66302 }
66303
66304 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl|BTCF_AtLast);
66305 return rc;
66306}
66307
66308/* Forward reference */
66309static int SQLITE_NOINLINE saveCursorsOnList(BtCursor*,Pgno,BtCursor*);
66310
66311/*
66312** Save the positions of all cursors (except pExcept) that are open on
66313** the table with root-page iRoot. "Saving the cursor position" means that
66314** the location in the btree is remembered in such a way that it can be
66315** moved back to the same spot after the btree has been modified. This
66316** routine is called just before cursor pExcept is used to modify the
66317** table, for example in BtreeDelete() or BtreeInsert().
66318**
66319** If there are two or more cursors on the same btree, then all such
66320** cursors should have their BTCF_Multiple flag set. The btreeCursor()
66321** routine enforces that rule. This routine only needs to be called in
66322** the uncommon case when pExpect has the BTCF_Multiple flag set.
66323**
66324** If pExpect!=NULL and if no other cursors are found on the same root-page,
66325** then the BTCF_Multiple flag on pExpect is cleared, to avoid another
66326** pointless call to this routine.
66327**
66328** Implementation note: This routine merely checks to see if any cursors
66329** need to be saved. It calls out to saveCursorsOnList() in the (unusual)
66330** event that cursors are in need to being saved.
66331*/
66332static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
66333 BtCursor *p;
66334 assert( sqlite3_mutex_held(pBt->mutex) );
66335 assert( pExcept==0 || pExcept->pBt==pBt );
66336 for(p=pBt->pCursor; p; p=p->pNext){
66337 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
66338 }
66339 if( p ) return saveCursorsOnList(p, iRoot, pExcept);
66340 if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple;
66341 return SQLITE_OK;
66342}
66343
66344/* This helper routine to saveAllCursors does the actual work of saving
66345** the cursors if and when a cursor is found that actually requires saving.
66346** The common case is that no cursors need to be saved, so this routine is
66347** broken out from its caller to avoid unnecessary stack pointer movement.
66348*/
66349static int SQLITE_NOINLINE saveCursorsOnList(
66350 BtCursor *p, /* The first cursor that needs saving */
66351 Pgno iRoot, /* Only save cursor with this iRoot. Save all if zero */
66352 BtCursor *pExcept /* Do not save this cursor */
66353){
66354 do{
66355 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
66356 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
66357 int rc = saveCursorPosition(pCur: p);
66358 if( SQLITE_OK!=rc ){
66359 return rc;
66360 }
66361 }else{
66362 testcase( p->iPage>=0 );
66363 btreeReleaseAllCursorPages(pCur: p);
66364 }
66365 }
66366 p = p->pNext;
66367 }while( p );
66368 return SQLITE_OK;
66369}
66370
66371/*
66372** Clear the current cursor position.
66373*/
66374SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *pCur){
66375 assert( cursorHoldsMutex(pCur) );
66376 sqlite3_free(p: pCur->pKey);
66377 pCur->pKey = 0;
66378 pCur->eState = CURSOR_INVALID;
66379}
66380
66381/*
66382** In this version of BtreeMoveto, pKey is a packed index record
66383** such as is generated by the OP_MakeRecord opcode. Unpack the
66384** record and then call BtreeMovetoUnpacked() to do the work.
66385*/
66386static int btreeMoveto(
66387 BtCursor *pCur, /* Cursor open on the btree to be searched */
66388 const void *pKey, /* Packed key if the btree is an index */
66389 i64 nKey, /* Integer key for tables. Size of pKey for indices */
66390 int bias, /* Bias search to the high end */
66391 int *pRes /* Write search results here */
66392){
66393 int rc; /* Status code */
66394 UnpackedRecord *pIdxKey; /* Unpacked index key */
66395
66396 if( pKey ){
66397 KeyInfo *pKeyInfo = pCur->pKeyInfo;
66398 assert( nKey==(i64)(int)nKey );
66399 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
66400 if( pIdxKey==0 ) return SQLITE_NOMEM_BKPT;
66401 sqlite3VdbeRecordUnpack(pKeyInfo, (int)nKey, pKey, pIdxKey);
66402 if( pIdxKey->nField==0 || pIdxKey->nField>pKeyInfo->nAllField ){
66403 rc = SQLITE_CORRUPT_BKPT;
66404 }else{
66405 rc = sqlite3BtreeIndexMoveto(pCur, pUnKey: pIdxKey, pRes);
66406 }
66407 sqlite3DbFree(db: pCur->pKeyInfo->db, p: pIdxKey);
66408 }else{
66409 pIdxKey = 0;
66410 rc = sqlite3BtreeTableMoveto(pCur, intKey: nKey, bias, pRes);
66411 }
66412 return rc;
66413}
66414
66415/*
66416** Restore the cursor to the position it was in (or as close to as possible)
66417** when saveCursorPosition() was called. Note that this call deletes the
66418** saved position info stored by saveCursorPosition(), so there can be
66419** at most one effective restoreCursorPosition() call after each
66420** saveCursorPosition().
66421*/
66422static int btreeRestoreCursorPosition(BtCursor *pCur){
66423 int rc;
66424 int skipNext = 0;
66425 assert( cursorOwnsBtShared(pCur) );
66426 assert( pCur->eState>=CURSOR_REQUIRESEEK );
66427 if( pCur->eState==CURSOR_FAULT ){
66428 return pCur->skipNext;
66429 }
66430 pCur->eState = CURSOR_INVALID;
66431 if( sqlite3FaultSim(iTest: 410) ){
66432 rc = SQLITE_IOERR;
66433 }else{
66434 rc = btreeMoveto(pCur, pKey: pCur->pKey, nKey: pCur->nKey, bias: 0, pRes: &skipNext);
66435 }
66436 if( rc==SQLITE_OK ){
66437 sqlite3_free(p: pCur->pKey);
66438 pCur->pKey = 0;
66439 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
66440 if( skipNext ) pCur->skipNext = skipNext;
66441 if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
66442 pCur->eState = CURSOR_SKIPNEXT;
66443 }
66444 }
66445 return rc;
66446}
66447
66448#define restoreCursorPosition(p) \
66449 (p->eState>=CURSOR_REQUIRESEEK ? \
66450 btreeRestoreCursorPosition(p) : \
66451 SQLITE_OK)
66452
66453/*
66454** Determine whether or not a cursor has moved from the position where
66455** it was last placed, or has been invalidated for any other reason.
66456** Cursors can move when the row they are pointing at is deleted out
66457** from under them, for example. Cursor might also move if a btree
66458** is rebalanced.
66459**
66460** Calling this routine with a NULL cursor pointer returns false.
66461**
66462** Use the separate sqlite3BtreeCursorRestore() routine to restore a cursor
66463** back to where it ought to be if this routine returns true.
66464*/
66465SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur){
66466 assert( EIGHT_BYTE_ALIGNMENT(pCur)
66467 || pCur==sqlite3BtreeFakeValidCursor() );
66468 assert( offsetof(BtCursor, eState)==0 );
66469 assert( sizeof(pCur->eState)==1 );
66470 return CURSOR_VALID != *(u8*)pCur;
66471}
66472
66473/*
66474** Return a pointer to a fake BtCursor object that will always answer
66475** false to the sqlite3BtreeCursorHasMoved() routine above. The fake
66476** cursor returned must not be used with any other Btree interface.
66477*/
66478SQLITE_PRIVATE BtCursor *sqlite3BtreeFakeValidCursor(void){
66479 static u8 fakeCursor = CURSOR_VALID;
66480 assert( offsetof(BtCursor, eState)==0 );
66481 return (BtCursor*)&fakeCursor;
66482}
66483
66484/*
66485** This routine restores a cursor back to its original position after it
66486** has been moved by some outside activity (such as a btree rebalance or
66487** a row having been deleted out from under the cursor).
66488**
66489** On success, the *pDifferentRow parameter is false if the cursor is left
66490** pointing at exactly the same row. *pDifferntRow is the row the cursor
66491** was pointing to has been deleted, forcing the cursor to point to some
66492** nearby row.
66493**
66494** This routine should only be called for a cursor that just returned
66495** TRUE from sqlite3BtreeCursorHasMoved().
66496*/
66497SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor *pCur, int *pDifferentRow){
66498 int rc;
66499
66500 assert( pCur!=0 );
66501 assert( pCur->eState!=CURSOR_VALID );
66502 rc = restoreCursorPosition(pCur);
66503 if( rc ){
66504 *pDifferentRow = 1;
66505 return rc;
66506 }
66507 if( pCur->eState!=CURSOR_VALID ){
66508 *pDifferentRow = 1;
66509 }else{
66510 *pDifferentRow = 0;
66511 }
66512 return SQLITE_OK;
66513}
66514
66515#ifdef SQLITE_ENABLE_CURSOR_HINTS
66516/*
66517** Provide hints to the cursor. The particular hint given (and the type
66518** and number of the varargs parameters) is determined by the eHintType
66519** parameter. See the definitions of the BTREE_HINT_* macros for details.
66520*/
66521SQLITE_PRIVATE void sqlite3BtreeCursorHint(BtCursor *pCur, int eHintType, ...){
66522 /* Used only by system that substitute their own storage engine */
66523}
66524#endif
66525
66526/*
66527** Provide flag hints to the cursor.
66528*/
66529SQLITE_PRIVATE void sqlite3BtreeCursorHintFlags(BtCursor *pCur, unsigned x){
66530 assert( x==BTREE_SEEK_EQ || x==BTREE_BULKLOAD || x==0 );
66531 pCur->hints = x;
66532}
66533
66534
66535#ifndef SQLITE_OMIT_AUTOVACUUM
66536/*
66537** Given a page number of a regular database page, return the page
66538** number for the pointer-map page that contains the entry for the
66539** input page number.
66540**
66541** Return 0 (not a valid page) for pgno==1 since there is
66542** no pointer map associated with page 1. The integrity_check logic
66543** requires that ptrmapPageno(*,1)!=1.
66544*/
66545static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
66546 int nPagesPerMapPage;
66547 Pgno iPtrMap, ret;
66548 assert( sqlite3_mutex_held(pBt->mutex) );
66549 if( pgno<2 ) return 0;
66550 nPagesPerMapPage = (pBt->usableSize/5)+1;
66551 iPtrMap = (pgno-2)/nPagesPerMapPage;
66552 ret = (iPtrMap*nPagesPerMapPage) + 2;
66553 if( ret==PENDING_BYTE_PAGE(pBt) ){
66554 ret++;
66555 }
66556 return ret;
66557}
66558
66559/*
66560** Write an entry into the pointer map.
66561**
66562** This routine updates the pointer map entry for page number 'key'
66563** so that it maps to type 'eType' and parent page number 'pgno'.
66564**
66565** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
66566** a no-op. If an error occurs, the appropriate error code is written
66567** into *pRC.
66568*/
66569static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){
66570 DbPage *pDbPage; /* The pointer map page */
66571 u8 *pPtrmap; /* The pointer map data */
66572 Pgno iPtrmap; /* The pointer map page number */
66573 int offset; /* Offset in pointer map page */
66574 int rc; /* Return code from subfunctions */
66575
66576 if( *pRC ) return;
66577
66578 assert( sqlite3_mutex_held(pBt->mutex) );
66579 /* The super-journal page number must never be used as a pointer map page */
66580 assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
66581
66582 assert( pBt->autoVacuum );
66583 if( key==0 ){
66584 *pRC = SQLITE_CORRUPT_BKPT;
66585 return;
66586 }
66587 iPtrmap = PTRMAP_PAGENO(pBt, key);
66588 rc = sqlite3PagerGet(pPager: pBt->pPager, pgno: iPtrmap, ppPage: &pDbPage, flags: 0);
66589 if( rc!=SQLITE_OK ){
66590 *pRC = rc;
66591 return;
66592 }
66593 if( ((char*)sqlite3PagerGetExtra(pPg: pDbPage))[0]!=0 ){
66594 /* The first byte of the extra data is the MemPage.isInit byte.
66595 ** If that byte is set, it means this page is also being used
66596 ** as a btree page. */
66597 *pRC = SQLITE_CORRUPT_BKPT;
66598 goto ptrmap_exit;
66599 }
66600 offset = PTRMAP_PTROFFSET(iPtrmap, key);
66601 if( offset<0 ){
66602 *pRC = SQLITE_CORRUPT_BKPT;
66603 goto ptrmap_exit;
66604 }
66605 assert( offset <= (int)pBt->usableSize-5 );
66606 pPtrmap = (u8 *)sqlite3PagerGetData(pPg: pDbPage);
66607
66608 if( eType!=pPtrmap[offset] || get4byte(p: &pPtrmap[offset+1])!=parent ){
66609 TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
66610 *pRC= rc = sqlite3PagerWrite(pPg: pDbPage);
66611 if( rc==SQLITE_OK ){
66612 pPtrmap[offset] = eType;
66613 put4byte(p: &pPtrmap[offset+1], v: parent);
66614 }
66615 }
66616
66617ptrmap_exit:
66618 sqlite3PagerUnref(pPg: pDbPage);
66619}
66620
66621/*
66622** Read an entry from the pointer map.
66623**
66624** This routine retrieves the pointer map entry for page 'key', writing
66625** the type and parent page number to *pEType and *pPgno respectively.
66626** An error code is returned if something goes wrong, otherwise SQLITE_OK.
66627*/
66628static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
66629 DbPage *pDbPage; /* The pointer map page */
66630 int iPtrmap; /* Pointer map page index */
66631 u8 *pPtrmap; /* Pointer map page data */
66632 int offset; /* Offset of entry in pointer map */
66633 int rc;
66634
66635 assert( sqlite3_mutex_held(pBt->mutex) );
66636
66637 iPtrmap = PTRMAP_PAGENO(pBt, key);
66638 rc = sqlite3PagerGet(pPager: pBt->pPager, pgno: iPtrmap, ppPage: &pDbPage, flags: 0);
66639 if( rc!=0 ){
66640 return rc;
66641 }
66642 pPtrmap = (u8 *)sqlite3PagerGetData(pPg: pDbPage);
66643
66644 offset = PTRMAP_PTROFFSET(iPtrmap, key);
66645 if( offset<0 ){
66646 sqlite3PagerUnref(pPg: pDbPage);
66647 return SQLITE_CORRUPT_BKPT;
66648 }
66649 assert( offset <= (int)pBt->usableSize-5 );
66650 assert( pEType!=0 );
66651 *pEType = pPtrmap[offset];
66652 if( pPgno ) *pPgno = get4byte(p: &pPtrmap[offset+1]);
66653
66654 sqlite3PagerUnref(pPg: pDbPage);
66655 if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_PGNO(iPtrmap);
66656 return SQLITE_OK;
66657}
66658
66659#else /* if defined SQLITE_OMIT_AUTOVACUUM */
66660 #define ptrmapPut(w,x,y,z,rc)
66661 #define ptrmapGet(w,x,y,z) SQLITE_OK
66662 #define ptrmapPutOvflPtr(x, y, z, rc)
66663#endif
66664
66665/*
66666** Given a btree page and a cell index (0 means the first cell on
66667** the page, 1 means the second cell, and so forth) return a pointer
66668** to the cell content.
66669**
66670** findCellPastPtr() does the same except it skips past the initial
66671** 4-byte child pointer found on interior pages, if there is one.
66672**
66673** This routine works only for pages that do not contain overflow cells.
66674*/
66675#define findCell(P,I) \
66676 ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
66677#define findCellPastPtr(P,I) \
66678 ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
66679
66680
66681/*
66682** This is common tail processing for btreeParseCellPtr() and
66683** btreeParseCellPtrIndex() for the case when the cell does not fit entirely
66684** on a single B-tree page. Make necessary adjustments to the CellInfo
66685** structure.
66686*/
66687static SQLITE_NOINLINE void btreeParseCellAdjustSizeForOverflow(
66688 MemPage *pPage, /* Page containing the cell */
66689 u8 *pCell, /* Pointer to the cell text. */
66690 CellInfo *pInfo /* Fill in this structure */
66691){
66692 /* If the payload will not fit completely on the local page, we have
66693 ** to decide how much to store locally and how much to spill onto
66694 ** overflow pages. The strategy is to minimize the amount of unused
66695 ** space on overflow pages while keeping the amount of local storage
66696 ** in between minLocal and maxLocal.
66697 **
66698 ** Warning: changing the way overflow payload is distributed in any
66699 ** way will result in an incompatible file format.
66700 */
66701 int minLocal; /* Minimum amount of payload held locally */
66702 int maxLocal; /* Maximum amount of payload held locally */
66703 int surplus; /* Overflow payload available for local storage */
66704
66705 minLocal = pPage->minLocal;
66706 maxLocal = pPage->maxLocal;
66707 surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
66708 testcase( surplus==maxLocal );
66709 testcase( surplus==maxLocal+1 );
66710 if( surplus <= maxLocal ){
66711 pInfo->nLocal = (u16)surplus;
66712 }else{
66713 pInfo->nLocal = (u16)minLocal;
66714 }
66715 pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4;
66716}
66717
66718/*
66719** Given a record with nPayload bytes of payload stored within btree
66720** page pPage, return the number of bytes of payload stored locally.
66721*/
66722static int btreePayloadToLocal(MemPage *pPage, i64 nPayload){
66723 int maxLocal; /* Maximum amount of payload held locally */
66724 maxLocal = pPage->maxLocal;
66725 if( nPayload<=maxLocal ){
66726 return nPayload;
66727 }else{
66728 int minLocal; /* Minimum amount of payload held locally */
66729 int surplus; /* Overflow payload available for local storage */
66730 minLocal = pPage->minLocal;
66731 surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize-4);
66732 return ( surplus <= maxLocal ) ? surplus : minLocal;
66733 }
66734}
66735
66736/*
66737** The following routines are implementations of the MemPage.xParseCell()
66738** method.
66739**
66740** Parse a cell content block and fill in the CellInfo structure.
66741**
66742** btreeParseCellPtr() => table btree leaf nodes
66743** btreeParseCellNoPayload() => table btree internal nodes
66744** btreeParseCellPtrIndex() => index btree nodes
66745**
66746** There is also a wrapper function btreeParseCell() that works for
66747** all MemPage types and that references the cell by index rather than
66748** by pointer.
66749*/
66750static void btreeParseCellPtrNoPayload(
66751 MemPage *pPage, /* Page containing the cell */
66752 u8 *pCell, /* Pointer to the cell text. */
66753 CellInfo *pInfo /* Fill in this structure */
66754){
66755 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66756 assert( pPage->leaf==0 );
66757 assert( pPage->childPtrSize==4 );
66758#ifndef SQLITE_DEBUG
66759 UNUSED_PARAMETER(pPage);
66760#endif
66761 pInfo->nSize = 4 + getVarint(p: &pCell[4], v: (u64*)&pInfo->nKey);
66762 pInfo->nPayload = 0;
66763 pInfo->nLocal = 0;
66764 pInfo->pPayload = 0;
66765 return;
66766}
66767static void btreeParseCellPtr(
66768 MemPage *pPage, /* Page containing the cell */
66769 u8 *pCell, /* Pointer to the cell text. */
66770 CellInfo *pInfo /* Fill in this structure */
66771){
66772 u8 *pIter; /* For scanning through pCell */
66773 u32 nPayload; /* Number of bytes of cell payload */
66774 u64 iKey; /* Extracted Key value */
66775
66776 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66777 assert( pPage->leaf==0 || pPage->leaf==1 );
66778 assert( pPage->intKeyLeaf );
66779 assert( pPage->childPtrSize==0 );
66780 pIter = pCell;
66781
66782 /* The next block of code is equivalent to:
66783 **
66784 ** pIter += getVarint32(pIter, nPayload);
66785 **
66786 ** The code is inlined to avoid a function call.
66787 */
66788 nPayload = *pIter;
66789 if( nPayload>=0x80 ){
66790 u8 *pEnd = &pIter[8];
66791 nPayload &= 0x7f;
66792 do{
66793 nPayload = (nPayload<<7) | (*++pIter & 0x7f);
66794 }while( (*pIter)>=0x80 && pIter<pEnd );
66795 }
66796 pIter++;
66797
66798 /* The next block of code is equivalent to:
66799 **
66800 ** pIter += getVarint(pIter, (u64*)&pInfo->nKey);
66801 **
66802 ** The code is inlined to avoid a function call.
66803 */
66804 iKey = *pIter;
66805 if( iKey>=0x80 ){
66806 u8 *pEnd = &pIter[7];
66807 iKey &= 0x7f;
66808 while(1){
66809 iKey = (iKey<<7) | (*++pIter & 0x7f);
66810 if( (*pIter)<0x80 ) break;
66811 if( pIter>=pEnd ){
66812 iKey = (iKey<<8) | *++pIter;
66813 break;
66814 }
66815 }
66816 }
66817 pIter++;
66818
66819 pInfo->nKey = *(i64*)&iKey;
66820 pInfo->nPayload = nPayload;
66821 pInfo->pPayload = pIter;
66822 testcase( nPayload==pPage->maxLocal );
66823 testcase( nPayload==(u32)pPage->maxLocal+1 );
66824 if( nPayload<=pPage->maxLocal ){
66825 /* This is the (easy) common case where the entire payload fits
66826 ** on the local page. No overflow is required.
66827 */
66828 pInfo->nSize = nPayload + (u16)(pIter - pCell);
66829 if( pInfo->nSize<4 ) pInfo->nSize = 4;
66830 pInfo->nLocal = (u16)nPayload;
66831 }else{
66832 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
66833 }
66834}
66835static void btreeParseCellPtrIndex(
66836 MemPage *pPage, /* Page containing the cell */
66837 u8 *pCell, /* Pointer to the cell text. */
66838 CellInfo *pInfo /* Fill in this structure */
66839){
66840 u8 *pIter; /* For scanning through pCell */
66841 u32 nPayload; /* Number of bytes of cell payload */
66842
66843 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66844 assert( pPage->leaf==0 || pPage->leaf==1 );
66845 assert( pPage->intKeyLeaf==0 );
66846 pIter = pCell + pPage->childPtrSize;
66847 nPayload = *pIter;
66848 if( nPayload>=0x80 ){
66849 u8 *pEnd = &pIter[8];
66850 nPayload &= 0x7f;
66851 do{
66852 nPayload = (nPayload<<7) | (*++pIter & 0x7f);
66853 }while( *(pIter)>=0x80 && pIter<pEnd );
66854 }
66855 pIter++;
66856 pInfo->nKey = nPayload;
66857 pInfo->nPayload = nPayload;
66858 pInfo->pPayload = pIter;
66859 testcase( nPayload==pPage->maxLocal );
66860 testcase( nPayload==(u32)pPage->maxLocal+1 );
66861 if( nPayload<=pPage->maxLocal ){
66862 /* This is the (easy) common case where the entire payload fits
66863 ** on the local page. No overflow is required.
66864 */
66865 pInfo->nSize = nPayload + (u16)(pIter - pCell);
66866 if( pInfo->nSize<4 ) pInfo->nSize = 4;
66867 pInfo->nLocal = (u16)nPayload;
66868 }else{
66869 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
66870 }
66871}
66872static void btreeParseCell(
66873 MemPage *pPage, /* Page containing the cell */
66874 int iCell, /* The cell index. First cell is 0 */
66875 CellInfo *pInfo /* Fill in this structure */
66876){
66877 pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo);
66878}
66879
66880/*
66881** The following routines are implementations of the MemPage.xCellSize
66882** method.
66883**
66884** Compute the total number of bytes that a Cell needs in the cell
66885** data area of the btree-page. The return number includes the cell
66886** data header and the local payload, but not any overflow page or
66887** the space used by the cell pointer.
66888**
66889** cellSizePtrNoPayload() => table internal nodes
66890** cellSizePtr() => all index nodes & table leaf nodes
66891*/
66892static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
66893 u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */
66894 u8 *pEnd; /* End mark for a varint */
66895 u32 nSize; /* Size value to return */
66896
66897#ifdef SQLITE_DEBUG
66898 /* The value returned by this function should always be the same as
66899 ** the (CellInfo.nSize) value found by doing a full parse of the
66900 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
66901 ** this function verifies that this invariant is not violated. */
66902 CellInfo debuginfo;
66903 pPage->xParseCell(pPage, pCell, &debuginfo);
66904#endif
66905
66906 nSize = *pIter;
66907 if( nSize>=0x80 ){
66908 pEnd = &pIter[8];
66909 nSize &= 0x7f;
66910 do{
66911 nSize = (nSize<<7) | (*++pIter & 0x7f);
66912 }while( *(pIter)>=0x80 && pIter<pEnd );
66913 }
66914 pIter++;
66915 if( pPage->intKey ){
66916 /* pIter now points at the 64-bit integer key value, a variable length
66917 ** integer. The following block moves pIter to point at the first byte
66918 ** past the end of the key value. */
66919 pEnd = &pIter[9];
66920 while( (*pIter++)&0x80 && pIter<pEnd );
66921 }
66922 testcase( nSize==pPage->maxLocal );
66923 testcase( nSize==(u32)pPage->maxLocal+1 );
66924 if( nSize<=pPage->maxLocal ){
66925 nSize += (u32)(pIter - pCell);
66926 if( nSize<4 ) nSize = 4;
66927 }else{
66928 int minLocal = pPage->minLocal;
66929 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
66930 testcase( nSize==pPage->maxLocal );
66931 testcase( nSize==(u32)pPage->maxLocal+1 );
66932 if( nSize>pPage->maxLocal ){
66933 nSize = minLocal;
66934 }
66935 nSize += 4 + (u16)(pIter - pCell);
66936 }
66937 assert( nSize==debuginfo.nSize || CORRUPT_DB );
66938 return (u16)nSize;
66939}
66940static u16 cellSizePtrNoPayload(MemPage *pPage, u8 *pCell){
66941 u8 *pIter = pCell + 4; /* For looping over bytes of pCell */
66942 u8 *pEnd; /* End mark for a varint */
66943
66944#ifdef SQLITE_DEBUG
66945 /* The value returned by this function should always be the same as
66946 ** the (CellInfo.nSize) value found by doing a full parse of the
66947 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
66948 ** this function verifies that this invariant is not violated. */
66949 CellInfo debuginfo;
66950 pPage->xParseCell(pPage, pCell, &debuginfo);
66951#else
66952 UNUSED_PARAMETER(pPage);
66953#endif
66954
66955 assert( pPage->childPtrSize==4 );
66956 pEnd = pIter + 9;
66957 while( (*pIter++)&0x80 && pIter<pEnd );
66958 assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB );
66959 return (u16)(pIter - pCell);
66960}
66961
66962
66963#ifdef SQLITE_DEBUG
66964/* This variation on cellSizePtr() is used inside of assert() statements
66965** only. */
66966static u16 cellSize(MemPage *pPage, int iCell){
66967 return pPage->xCellSize(pPage, findCell(pPage, iCell));
66968}
66969#endif
66970
66971#ifndef SQLITE_OMIT_AUTOVACUUM
66972/*
66973** The cell pCell is currently part of page pSrc but will ultimately be part
66974** of pPage. (pSrc and pPager are often the same.) If pCell contains a
66975** pointer to an overflow page, insert an entry into the pointer-map for
66976** the overflow page that will be valid after pCell has been moved to pPage.
66977*/
66978static void ptrmapPutOvflPtr(MemPage *pPage, MemPage *pSrc, u8 *pCell,int *pRC){
66979 CellInfo info;
66980 if( *pRC ) return;
66981 assert( pCell!=0 );
66982 pPage->xParseCell(pPage, pCell, &info);
66983 if( info.nLocal<info.nPayload ){
66984 Pgno ovfl;
66985 if( SQLITE_WITHIN(pSrc->aDataEnd, pCell, pCell+info.nLocal) ){
66986 testcase( pSrc!=pPage );
66987 *pRC = SQLITE_CORRUPT_BKPT;
66988 return;
66989 }
66990 ovfl = get4byte(p: &pCell[info.nSize-4]);
66991 ptrmapPut(pBt: pPage->pBt, key: ovfl, PTRMAP_OVERFLOW1, parent: pPage->pgno, pRC);
66992 }
66993}
66994#endif
66995
66996
66997/*
66998** Defragment the page given. This routine reorganizes cells within the
66999** page so that there are no free-blocks on the free-block list.
67000**
67001** Parameter nMaxFrag is the maximum amount of fragmented space that may be
67002** present in the page after this routine returns.
67003**
67004** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
67005** b-tree page so that there are no freeblocks or fragment bytes, all
67006** unused bytes are contained in the unallocated space region, and all
67007** cells are packed tightly at the end of the page.
67008*/
67009static int defragmentPage(MemPage *pPage, int nMaxFrag){
67010 int i; /* Loop counter */
67011 int pc; /* Address of the i-th cell */
67012 int hdr; /* Offset to the page header */
67013 int size; /* Size of a cell */
67014 int usableSize; /* Number of usable bytes on a page */
67015 int cellOffset; /* Offset to the cell pointer array */
67016 int cbrk; /* Offset to the cell content area */
67017 int nCell; /* Number of cells on the page */
67018 unsigned char *data; /* The page data */
67019 unsigned char *temp; /* Temp area for cell content */
67020 unsigned char *src; /* Source of content */
67021 int iCellFirst; /* First allowable cell index */
67022 int iCellLast; /* Last possible cell index */
67023 int iCellStart; /* First cell offset in input */
67024
67025 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
67026 assert( pPage->pBt!=0 );
67027 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
67028 assert( pPage->nOverflow==0 );
67029 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
67030 temp = 0;
67031 src = data = pPage->aData;
67032 hdr = pPage->hdrOffset;
67033 cellOffset = pPage->cellOffset;
67034 nCell = pPage->nCell;
67035 assert( nCell==get2byte(&data[hdr+3]) || CORRUPT_DB );
67036 iCellFirst = cellOffset + 2*nCell;
67037 usableSize = pPage->pBt->usableSize;
67038
67039 /* This block handles pages with two or fewer free blocks and nMaxFrag
67040 ** or fewer fragmented bytes. In this case it is faster to move the
67041 ** two (or one) blocks of cells using memmove() and add the required
67042 ** offsets to each pointer in the cell-pointer array than it is to
67043 ** reconstruct the entire page. */
67044 if( (int)data[hdr+7]<=nMaxFrag ){
67045 int iFree = get2byte(&data[hdr+1]);
67046 if( iFree>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
67047 if( iFree ){
67048 int iFree2 = get2byte(&data[iFree]);
67049 if( iFree2>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
67050 if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){
67051 u8 *pEnd = &data[cellOffset + nCell*2];
67052 u8 *pAddr;
67053 int sz2 = 0;
67054 int sz = get2byte(&data[iFree+2]);
67055 int top = get2byte(&data[hdr+5]);
67056 if( top>=iFree ){
67057 return SQLITE_CORRUPT_PAGE(pPage);
67058 }
67059 if( iFree2 ){
67060 if( iFree+sz>iFree2 ) return SQLITE_CORRUPT_PAGE(pPage);
67061 sz2 = get2byte(&data[iFree2+2]);
67062 if( iFree2+sz2 > usableSize ) return SQLITE_CORRUPT_PAGE(pPage);
67063 memmove(dest: &data[iFree+sz+sz2], src: &data[iFree+sz], n: iFree2-(iFree+sz));
67064 sz += sz2;
67065 }else if( NEVER(iFree+sz>usableSize) ){
67066 return SQLITE_CORRUPT_PAGE(pPage);
67067 }
67068
67069 cbrk = top+sz;
67070 assert( cbrk+(iFree-top) <= usableSize );
67071 memmove(dest: &data[cbrk], src: &data[top], n: iFree-top);
67072 for(pAddr=&data[cellOffset]; pAddr<pEnd; pAddr+=2){
67073 pc = get2byte(pAddr);
67074 if( pc<iFree ){ put2byte(pAddr, pc+sz); }
67075 else if( pc<iFree2 ){ put2byte(pAddr, pc+sz2); }
67076 }
67077 goto defragment_out;
67078 }
67079 }
67080 }
67081
67082 cbrk = usableSize;
67083 iCellLast = usableSize - 4;
67084 iCellStart = get2byte(&data[hdr+5]);
67085 for(i=0; i<nCell; i++){
67086 u8 *pAddr; /* The i-th cell pointer */
67087 pAddr = &data[cellOffset + i*2];
67088 pc = get2byte(pAddr);
67089 testcase( pc==iCellFirst );
67090 testcase( pc==iCellLast );
67091 /* These conditions have already been verified in btreeInitPage()
67092 ** if PRAGMA cell_size_check=ON.
67093 */
67094 if( pc<iCellStart || pc>iCellLast ){
67095 return SQLITE_CORRUPT_PAGE(pPage);
67096 }
67097 assert( pc>=iCellStart && pc<=iCellLast );
67098 size = pPage->xCellSize(pPage, &src[pc]);
67099 cbrk -= size;
67100 if( cbrk<iCellStart || pc+size>usableSize ){
67101 return SQLITE_CORRUPT_PAGE(pPage);
67102 }
67103 assert( cbrk+size<=usableSize && cbrk>=iCellStart );
67104 testcase( cbrk+size==usableSize );
67105 testcase( pc+size==usableSize );
67106 put2byte(pAddr, cbrk);
67107 if( temp==0 ){
67108 if( cbrk==pc ) continue;
67109 temp = sqlite3PagerTempSpace(pPager: pPage->pBt->pPager);
67110 memcpy(dest: &temp[iCellStart], src: &data[iCellStart], n: usableSize - iCellStart);
67111 src = temp;
67112 }
67113 memcpy(dest: &data[cbrk], src: &src[pc], n: size);
67114 }
67115 data[hdr+7] = 0;
67116
67117 defragment_out:
67118 assert( pPage->nFree>=0 );
67119 if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
67120 return SQLITE_CORRUPT_PAGE(pPage);
67121 }
67122 assert( cbrk>=iCellFirst );
67123 put2byte(&data[hdr+5], cbrk);
67124 data[hdr+1] = 0;
67125 data[hdr+2] = 0;
67126 memset(s: &data[iCellFirst], c: 0, n: cbrk-iCellFirst);
67127 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
67128 return SQLITE_OK;
67129}
67130
67131/*
67132** Search the free-list on page pPg for space to store a cell nByte bytes in
67133** size. If one can be found, return a pointer to the space and remove it
67134** from the free-list.
67135**
67136** If no suitable space can be found on the free-list, return NULL.
67137**
67138** This function may detect corruption within pPg. If corruption is
67139** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned.
67140**
67141** Slots on the free list that are between 1 and 3 bytes larger than nByte
67142** will be ignored if adding the extra space to the fragmentation count
67143** causes the fragmentation count to exceed 60.
67144*/
67145static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){
67146 const int hdr = pPg->hdrOffset; /* Offset to page header */
67147 u8 * const aData = pPg->aData; /* Page data */
67148 int iAddr = hdr + 1; /* Address of ptr to pc */
67149 int pc = get2byte(&aData[iAddr]); /* Address of a free slot */
67150 int x; /* Excess size of the slot */
67151 int maxPC = pPg->pBt->usableSize - nByte; /* Max address for a usable slot */
67152 int size; /* Size of the free slot */
67153
67154 assert( pc>0 );
67155 while( pc<=maxPC ){
67156 /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
67157 ** freeblock form a big-endian integer which is the size of the freeblock
67158 ** in bytes, including the 4-byte header. */
67159 size = get2byte(&aData[pc+2]);
67160 if( (x = size - nByte)>=0 ){
67161 testcase( x==4 );
67162 testcase( x==3 );
67163 if( x<4 ){
67164 /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
67165 ** number of bytes in fragments may not exceed 60. */
67166 if( aData[hdr+7]>57 ) return 0;
67167
67168 /* Remove the slot from the free-list. Update the number of
67169 ** fragmented bytes within the page. */
67170 memcpy(dest: &aData[iAddr], src: &aData[pc], n: 2);
67171 aData[hdr+7] += (u8)x;
67172 }else if( x+pc > maxPC ){
67173 /* This slot extends off the end of the usable part of the page */
67174 *pRc = SQLITE_CORRUPT_PAGE(pPg);
67175 return 0;
67176 }else{
67177 /* The slot remains on the free-list. Reduce its size to account
67178 ** for the portion used by the new allocation. */
67179 put2byte(&aData[pc+2], x);
67180 }
67181 return &aData[pc + x];
67182 }
67183 iAddr = pc;
67184 pc = get2byte(&aData[pc]);
67185 if( pc<=iAddr+size ){
67186 if( pc ){
67187 /* The next slot in the chain is not past the end of the current slot */
67188 *pRc = SQLITE_CORRUPT_PAGE(pPg);
67189 }
67190 return 0;
67191 }
67192 }
67193 if( pc>maxPC+nByte-4 ){
67194 /* The free slot chain extends off the end of the page */
67195 *pRc = SQLITE_CORRUPT_PAGE(pPg);
67196 }
67197 return 0;
67198}
67199
67200/*
67201** Allocate nByte bytes of space from within the B-Tree page passed
67202** as the first argument. Write into *pIdx the index into pPage->aData[]
67203** of the first byte of allocated space. Return either SQLITE_OK or
67204** an error code (usually SQLITE_CORRUPT).
67205**
67206** The caller guarantees that there is sufficient space to make the
67207** allocation. This routine might need to defragment in order to bring
67208** all the space together, however. This routine will avoid using
67209** the first two bytes past the cell pointer area since presumably this
67210** allocation is being made in order to insert a new cell, so we will
67211** also end up needing a new cell pointer.
67212*/
67213static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
67214 const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
67215 u8 * const data = pPage->aData; /* Local cache of pPage->aData */
67216 int top; /* First byte of cell content area */
67217 int rc = SQLITE_OK; /* Integer return code */
67218 int gap; /* First byte of gap between cell pointers and cell content */
67219
67220 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
67221 assert( pPage->pBt );
67222 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
67223 assert( nByte>=0 ); /* Minimum cell size is 4 */
67224 assert( pPage->nFree>=nByte );
67225 assert( pPage->nOverflow==0 );
67226 assert( nByte < (int)(pPage->pBt->usableSize-8) );
67227
67228 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
67229 gap = pPage->cellOffset + 2*pPage->nCell;
67230 assert( gap<=65536 );
67231 /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
67232 ** and the reserved space is zero (the usual value for reserved space)
67233 ** then the cell content offset of an empty page wants to be 65536.
67234 ** However, that integer is too large to be stored in a 2-byte unsigned
67235 ** integer, so a value of 0 is used in its place. */
67236 top = get2byte(&data[hdr+5]);
67237 assert( top<=(int)pPage->pBt->usableSize ); /* by btreeComputeFreeSpace() */
67238 if( gap>top ){
67239 if( top==0 && pPage->pBt->usableSize==65536 ){
67240 top = 65536;
67241 }else{
67242 return SQLITE_CORRUPT_PAGE(pPage);
67243 }
67244 }
67245
67246 /* If there is enough space between gap and top for one more cell pointer,
67247 ** and if the freelist is not empty, then search the
67248 ** freelist looking for a slot big enough to satisfy the request.
67249 */
67250 testcase( gap+2==top );
67251 testcase( gap+1==top );
67252 testcase( gap==top );
67253 if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){
67254 u8 *pSpace = pageFindSlot(pPg: pPage, nByte, pRc: &rc);
67255 if( pSpace ){
67256 int g2;
67257 assert( pSpace+nByte<=data+pPage->pBt->usableSize );
67258 *pIdx = g2 = (int)(pSpace-data);
67259 if( g2<=gap ){
67260 return SQLITE_CORRUPT_PAGE(pPage);
67261 }else{
67262 return SQLITE_OK;
67263 }
67264 }else if( rc ){
67265 return rc;
67266 }
67267 }
67268
67269 /* The request could not be fulfilled using a freelist slot. Check
67270 ** to see if defragmentation is necessary.
67271 */
67272 testcase( gap+2+nByte==top );
67273 if( gap+2+nByte>top ){
67274 assert( pPage->nCell>0 || CORRUPT_DB );
67275 assert( pPage->nFree>=0 );
67276 rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
67277 if( rc ) return rc;
67278 top = get2byteNotZero(&data[hdr+5]);
67279 assert( gap+2+nByte<=top );
67280 }
67281
67282
67283 /* Allocate memory from the gap in between the cell pointer array
67284 ** and the cell content area. The btreeComputeFreeSpace() call has already
67285 ** validated the freelist. Given that the freelist is valid, there
67286 ** is no way that the allocation can extend off the end of the page.
67287 ** The assert() below verifies the previous sentence.
67288 */
67289 top -= nByte;
67290 put2byte(&data[hdr+5], top);
67291 assert( top+nByte <= (int)pPage->pBt->usableSize );
67292 *pIdx = top;
67293 return SQLITE_OK;
67294}
67295
67296/*
67297** Return a section of the pPage->aData to the freelist.
67298** The first byte of the new free block is pPage->aData[iStart]
67299** and the size of the block is iSize bytes.
67300**
67301** Adjacent freeblocks are coalesced.
67302**
67303** Even though the freeblock list was checked by btreeComputeFreeSpace(),
67304** that routine will not detect overlap between cells or freeblocks. Nor
67305** does it detect cells or freeblocks that encrouch into the reserved bytes
67306** at the end of the page. So do additional corruption checks inside this
67307** routine and return SQLITE_CORRUPT if any problems are found.
67308*/
67309static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
67310 u16 iPtr; /* Address of ptr to next freeblock */
67311 u16 iFreeBlk; /* Address of the next freeblock */
67312 u8 hdr; /* Page header size. 0 or 100 */
67313 u8 nFrag = 0; /* Reduction in fragmentation */
67314 u16 iOrigSize = iSize; /* Original value of iSize */
67315 u16 x; /* Offset to cell content area */
67316 u32 iEnd = iStart + iSize; /* First byte past the iStart buffer */
67317 unsigned char *data = pPage->aData; /* Page content */
67318
67319 assert( pPage->pBt!=0 );
67320 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
67321 assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
67322 assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
67323 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
67324 assert( iSize>=4 ); /* Minimum cell size is 4 */
67325 assert( iStart<=pPage->pBt->usableSize-4 );
67326
67327 /* The list of freeblocks must be in ascending order. Find the
67328 ** spot on the list where iStart should be inserted.
67329 */
67330 hdr = pPage->hdrOffset;
67331 iPtr = hdr + 1;
67332 if( data[iPtr+1]==0 && data[iPtr]==0 ){
67333 iFreeBlk = 0; /* Shortcut for the case when the freelist is empty */
67334 }else{
67335 while( (iFreeBlk = get2byte(&data[iPtr]))<iStart ){
67336 if( iFreeBlk<iPtr+4 ){
67337 if( iFreeBlk==0 ) break; /* TH3: corrupt082.100 */
67338 return SQLITE_CORRUPT_PAGE(pPage);
67339 }
67340 iPtr = iFreeBlk;
67341 }
67342 if( iFreeBlk>pPage->pBt->usableSize-4 ){ /* TH3: corrupt081.100 */
67343 return SQLITE_CORRUPT_PAGE(pPage);
67344 }
67345 assert( iFreeBlk>iPtr || iFreeBlk==0 );
67346
67347 /* At this point:
67348 ** iFreeBlk: First freeblock after iStart, or zero if none
67349 ** iPtr: The address of a pointer to iFreeBlk
67350 **
67351 ** Check to see if iFreeBlk should be coalesced onto the end of iStart.
67352 */
67353 if( iFreeBlk && iEnd+3>=iFreeBlk ){
67354 nFrag = iFreeBlk - iEnd;
67355 if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_PAGE(pPage);
67356 iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]);
67357 if( iEnd > pPage->pBt->usableSize ){
67358 return SQLITE_CORRUPT_PAGE(pPage);
67359 }
67360 iSize = iEnd - iStart;
67361 iFreeBlk = get2byte(&data[iFreeBlk]);
67362 }
67363
67364 /* If iPtr is another freeblock (that is, if iPtr is not the freelist
67365 ** pointer in the page header) then check to see if iStart should be
67366 ** coalesced onto the end of iPtr.
67367 */
67368 if( iPtr>hdr+1 ){
67369 int iPtrEnd = iPtr + get2byte(&data[iPtr+2]);
67370 if( iPtrEnd+3>=iStart ){
67371 if( iPtrEnd>iStart ) return SQLITE_CORRUPT_PAGE(pPage);
67372 nFrag += iStart - iPtrEnd;
67373 iSize = iEnd - iPtr;
67374 iStart = iPtr;
67375 }
67376 }
67377 if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PAGE(pPage);
67378 data[hdr+7] -= nFrag;
67379 }
67380 x = get2byte(&data[hdr+5]);
67381 if( iStart<=x ){
67382 /* The new freeblock is at the beginning of the cell content area,
67383 ** so just extend the cell content area rather than create another
67384 ** freelist entry */
67385 if( iStart<x ) return SQLITE_CORRUPT_PAGE(pPage);
67386 if( iPtr!=hdr+1 ) return SQLITE_CORRUPT_PAGE(pPage);
67387 put2byte(&data[hdr+1], iFreeBlk);
67388 put2byte(&data[hdr+5], iEnd);
67389 }else{
67390 /* Insert the new freeblock into the freelist */
67391 put2byte(&data[iPtr], iStart);
67392 }
67393 if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
67394 /* Overwrite deleted information with zeros when the secure_delete
67395 ** option is enabled */
67396 memset(s: &data[iStart], c: 0, n: iSize);
67397 }
67398 put2byte(&data[iStart], iFreeBlk);
67399 put2byte(&data[iStart+2], iSize);
67400 pPage->nFree += iOrigSize;
67401 return SQLITE_OK;
67402}
67403
67404/*
67405** Decode the flags byte (the first byte of the header) for a page
67406** and initialize fields of the MemPage structure accordingly.
67407**
67408** Only the following combinations are supported. Anything different
67409** indicates a corrupt database files:
67410**
67411** PTF_ZERODATA
67412** PTF_ZERODATA | PTF_LEAF
67413** PTF_LEAFDATA | PTF_INTKEY
67414** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF
67415*/
67416static int decodeFlags(MemPage *pPage, int flagByte){
67417 BtShared *pBt; /* A copy of pPage->pBt */
67418
67419 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
67420 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
67421 pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 );
67422 flagByte &= ~PTF_LEAF;
67423 pPage->childPtrSize = 4-4*pPage->leaf;
67424 pPage->xCellSize = cellSizePtr;
67425 pBt = pPage->pBt;
67426 if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
67427 /* EVIDENCE-OF: R-07291-35328 A value of 5 (0x05) means the page is an
67428 ** interior table b-tree page. */
67429 assert( (PTF_LEAFDATA|PTF_INTKEY)==5 );
67430 /* EVIDENCE-OF: R-26900-09176 A value of 13 (0x0d) means the page is a
67431 ** leaf table b-tree page. */
67432 assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 );
67433 pPage->intKey = 1;
67434 if( pPage->leaf ){
67435 pPage->intKeyLeaf = 1;
67436 pPage->xParseCell = btreeParseCellPtr;
67437 }else{
67438 pPage->intKeyLeaf = 0;
67439 pPage->xCellSize = cellSizePtrNoPayload;
67440 pPage->xParseCell = btreeParseCellPtrNoPayload;
67441 }
67442 pPage->maxLocal = pBt->maxLeaf;
67443 pPage->minLocal = pBt->minLeaf;
67444 }else if( flagByte==PTF_ZERODATA ){
67445 /* EVIDENCE-OF: R-43316-37308 A value of 2 (0x02) means the page is an
67446 ** interior index b-tree page. */
67447 assert( (PTF_ZERODATA)==2 );
67448 /* EVIDENCE-OF: R-59615-42828 A value of 10 (0x0a) means the page is a
67449 ** leaf index b-tree page. */
67450 assert( (PTF_ZERODATA|PTF_LEAF)==10 );
67451 pPage->intKey = 0;
67452 pPage->intKeyLeaf = 0;
67453 pPage->xParseCell = btreeParseCellPtrIndex;
67454 pPage->maxLocal = pBt->maxLocal;
67455 pPage->minLocal = pBt->minLocal;
67456 }else{
67457 /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is
67458 ** an error. */
67459 return SQLITE_CORRUPT_PAGE(pPage);
67460 }
67461 pPage->max1bytePayload = pBt->max1bytePayload;
67462 return SQLITE_OK;
67463}
67464
67465/*
67466** Compute the amount of freespace on the page. In other words, fill
67467** in the pPage->nFree field.
67468*/
67469static int btreeComputeFreeSpace(MemPage *pPage){
67470 int pc; /* Address of a freeblock within pPage->aData[] */
67471 u8 hdr; /* Offset to beginning of page header */
67472 u8 *data; /* Equal to pPage->aData */
67473 int usableSize; /* Amount of usable space on each page */
67474 int nFree; /* Number of unused bytes on the page */
67475 int top; /* First byte of the cell content area */
67476 int iCellFirst; /* First allowable cell or freeblock offset */
67477 int iCellLast; /* Last possible cell or freeblock offset */
67478
67479 assert( pPage->pBt!=0 );
67480 assert( pPage->pBt->db!=0 );
67481 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
67482 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
67483 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
67484 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
67485 assert( pPage->isInit==1 );
67486 assert( pPage->nFree<0 );
67487
67488 usableSize = pPage->pBt->usableSize;
67489 hdr = pPage->hdrOffset;
67490 data = pPage->aData;
67491 /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
67492 ** the start of the cell content area. A zero value for this integer is
67493 ** interpreted as 65536. */
67494 top = get2byteNotZero(&data[hdr+5]);
67495 iCellFirst = hdr + 8 + pPage->childPtrSize + 2*pPage->nCell;
67496 iCellLast = usableSize - 4;
67497
67498 /* Compute the total free space on the page
67499 ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
67500 ** start of the first freeblock on the page, or is zero if there are no
67501 ** freeblocks. */
67502 pc = get2byte(&data[hdr+1]);
67503 nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */
67504 if( pc>0 ){
67505 u32 next, size;
67506 if( pc<top ){
67507 /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
67508 ** always be at least one cell before the first freeblock.
67509 */
67510 return SQLITE_CORRUPT_PAGE(pPage);
67511 }
67512 while( 1 ){
67513 if( pc>iCellLast ){
67514 /* Freeblock off the end of the page */
67515 return SQLITE_CORRUPT_PAGE(pPage);
67516 }
67517 next = get2byte(&data[pc]);
67518 size = get2byte(&data[pc+2]);
67519 nFree = nFree + size;
67520 if( next<=pc+size+3 ) break;
67521 pc = next;
67522 }
67523 if( next>0 ){
67524 /* Freeblock not in ascending order */
67525 return SQLITE_CORRUPT_PAGE(pPage);
67526 }
67527 if( pc+size>(unsigned int)usableSize ){
67528 /* Last freeblock extends past page end */
67529 return SQLITE_CORRUPT_PAGE(pPage);
67530 }
67531 }
67532
67533 /* At this point, nFree contains the sum of the offset to the start
67534 ** of the cell-content area plus the number of free bytes within
67535 ** the cell-content area. If this is greater than the usable-size
67536 ** of the page, then the page must be corrupted. This check also
67537 ** serves to verify that the offset to the start of the cell-content
67538 ** area, according to the page header, lies within the page.
67539 */
67540 if( nFree>usableSize || nFree<iCellFirst ){
67541 return SQLITE_CORRUPT_PAGE(pPage);
67542 }
67543 pPage->nFree = (u16)(nFree - iCellFirst);
67544 return SQLITE_OK;
67545}
67546
67547/*
67548** Do additional sanity check after btreeInitPage() if
67549** PRAGMA cell_size_check=ON
67550*/
67551static SQLITE_NOINLINE int btreeCellSizeCheck(MemPage *pPage){
67552 int iCellFirst; /* First allowable cell or freeblock offset */
67553 int iCellLast; /* Last possible cell or freeblock offset */
67554 int i; /* Index into the cell pointer array */
67555 int sz; /* Size of a cell */
67556 int pc; /* Address of a freeblock within pPage->aData[] */
67557 u8 *data; /* Equal to pPage->aData */
67558 int usableSize; /* Maximum usable space on the page */
67559 int cellOffset; /* Start of cell content area */
67560
67561 iCellFirst = pPage->cellOffset + 2*pPage->nCell;
67562 usableSize = pPage->pBt->usableSize;
67563 iCellLast = usableSize - 4;
67564 data = pPage->aData;
67565 cellOffset = pPage->cellOffset;
67566 if( !pPage->leaf ) iCellLast--;
67567 for(i=0; i<pPage->nCell; i++){
67568 pc = get2byteAligned(&data[cellOffset+i*2]);
67569 testcase( pc==iCellFirst );
67570 testcase( pc==iCellLast );
67571 if( pc<iCellFirst || pc>iCellLast ){
67572 return SQLITE_CORRUPT_PAGE(pPage);
67573 }
67574 sz = pPage->xCellSize(pPage, &data[pc]);
67575 testcase( pc+sz==usableSize );
67576 if( pc+sz>usableSize ){
67577 return SQLITE_CORRUPT_PAGE(pPage);
67578 }
67579 }
67580 return SQLITE_OK;
67581}
67582
67583/*
67584** Initialize the auxiliary information for a disk block.
67585**
67586** Return SQLITE_OK on success. If we see that the page does
67587** not contain a well-formed database page, then return
67588** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
67589** guarantee that the page is well-formed. It only shows that
67590** we failed to detect any corruption.
67591*/
67592static int btreeInitPage(MemPage *pPage){
67593 u8 *data; /* Equal to pPage->aData */
67594 BtShared *pBt; /* The main btree structure */
67595
67596 assert( pPage->pBt!=0 );
67597 assert( pPage->pBt->db!=0 );
67598 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
67599 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
67600 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
67601 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
67602 assert( pPage->isInit==0 );
67603
67604 pBt = pPage->pBt;
67605 data = pPage->aData + pPage->hdrOffset;
67606 /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
67607 ** the b-tree page type. */
67608 if( decodeFlags(pPage, flagByte: data[0]) ){
67609 return SQLITE_CORRUPT_PAGE(pPage);
67610 }
67611 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
67612 pPage->maskPage = (u16)(pBt->pageSize - 1);
67613 pPage->nOverflow = 0;
67614 pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize;
67615 pPage->aCellIdx = data + pPage->childPtrSize + 8;
67616 pPage->aDataEnd = pPage->aData + pBt->usableSize;
67617 pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
67618 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
67619 ** number of cells on the page. */
67620 pPage->nCell = get2byte(&data[3]);
67621 if( pPage->nCell>MX_CELL(pBt) ){
67622 /* To many cells for a single page. The page must be corrupt */
67623 return SQLITE_CORRUPT_PAGE(pPage);
67624 }
67625 testcase( pPage->nCell==MX_CELL(pBt) );
67626 /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
67627 ** possible for a root page of a table that contains no rows) then the
67628 ** offset to the cell content area will equal the page size minus the
67629 ** bytes of reserved space. */
67630 assert( pPage->nCell>0
67631 || get2byteNotZero(&data[5])==(int)pBt->usableSize
67632 || CORRUPT_DB );
67633 pPage->nFree = -1; /* Indicate that this value is yet uncomputed */
67634 pPage->isInit = 1;
67635 if( pBt->db->flags & SQLITE_CellSizeCk ){
67636 return btreeCellSizeCheck(pPage);
67637 }
67638 return SQLITE_OK;
67639}
67640
67641/*
67642** Set up a raw page so that it looks like a database page holding
67643** no entries.
67644*/
67645static void zeroPage(MemPage *pPage, int flags){
67646 unsigned char *data = pPage->aData;
67647 BtShared *pBt = pPage->pBt;
67648 u8 hdr = pPage->hdrOffset;
67649 u16 first;
67650
67651 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
67652 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
67653 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
67654 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
67655 assert( sqlite3_mutex_held(pBt->mutex) );
67656 if( pBt->btsFlags & BTS_FAST_SECURE ){
67657 memset(s: &data[hdr], c: 0, n: pBt->usableSize - hdr);
67658 }
67659 data[hdr] = (char)flags;
67660 first = hdr + ((flags&PTF_LEAF)==0 ? 12 : 8);
67661 memset(s: &data[hdr+1], c: 0, n: 4);
67662 data[hdr+7] = 0;
67663 put2byte(&data[hdr+5], pBt->usableSize);
67664 pPage->nFree = (u16)(pBt->usableSize - first);
67665 decodeFlags(pPage, flagByte: flags);
67666 pPage->cellOffset = first;
67667 pPage->aDataEnd = &data[pBt->usableSize];
67668 pPage->aCellIdx = &data[first];
67669 pPage->aDataOfst = &data[pPage->childPtrSize];
67670 pPage->nOverflow = 0;
67671 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
67672 pPage->maskPage = (u16)(pBt->pageSize - 1);
67673 pPage->nCell = 0;
67674 pPage->isInit = 1;
67675}
67676
67677
67678/*
67679** Convert a DbPage obtained from the pager into a MemPage used by
67680** the btree layer.
67681*/
67682static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
67683 MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pPg: pDbPage);
67684 if( pgno!=pPage->pgno ){
67685 pPage->aData = sqlite3PagerGetData(pPg: pDbPage);
67686 pPage->pDbPage = pDbPage;
67687 pPage->pBt = pBt;
67688 pPage->pgno = pgno;
67689 pPage->hdrOffset = pgno==1 ? 100 : 0;
67690 }
67691 assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
67692 return pPage;
67693}
67694
67695/*
67696** Get a page from the pager. Initialize the MemPage.pBt and
67697** MemPage.aData elements if needed. See also: btreeGetUnusedPage().
67698**
67699** If the PAGER_GET_NOCONTENT flag is set, it means that we do not care
67700** about the content of the page at this time. So do not go to the disk
67701** to fetch the content. Just fill in the content with zeros for now.
67702** If in the future we call sqlite3PagerWrite() on this page, that
67703** means we have started to be concerned about content and the disk
67704** read should occur at that point.
67705*/
67706static int btreeGetPage(
67707 BtShared *pBt, /* The btree */
67708 Pgno pgno, /* Number of the page to fetch */
67709 MemPage **ppPage, /* Return the page in this parameter */
67710 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
67711){
67712 int rc;
67713 DbPage *pDbPage;
67714
67715 assert( flags==0 || flags==PAGER_GET_NOCONTENT || flags==PAGER_GET_READONLY );
67716 assert( sqlite3_mutex_held(pBt->mutex) );
67717 rc = sqlite3PagerGet(pPager: pBt->pPager, pgno, ppPage: (DbPage**)&pDbPage, flags);
67718 if( rc ) return rc;
67719 *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
67720 return SQLITE_OK;
67721}
67722
67723/*
67724** Retrieve a page from the pager cache. If the requested page is not
67725** already in the pager cache return NULL. Initialize the MemPage.pBt and
67726** MemPage.aData elements if needed.
67727*/
67728static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){
67729 DbPage *pDbPage;
67730 assert( sqlite3_mutex_held(pBt->mutex) );
67731 pDbPage = sqlite3PagerLookup(pPager: pBt->pPager, pgno);
67732 if( pDbPage ){
67733 return btreePageFromDbPage(pDbPage, pgno, pBt);
67734 }
67735 return 0;
67736}
67737
67738/*
67739** Return the size of the database file in pages. If there is any kind of
67740** error, return ((unsigned int)-1).
67741*/
67742static Pgno btreePagecount(BtShared *pBt){
67743 return pBt->nPage;
67744}
67745SQLITE_PRIVATE Pgno sqlite3BtreeLastPage(Btree *p){
67746 assert( sqlite3BtreeHoldsMutex(p) );
67747 return btreePagecount(pBt: p->pBt);
67748}
67749
67750/*
67751** Get a page from the pager and initialize it.
67752**
67753** If pCur!=0 then the page is being fetched as part of a moveToChild()
67754** call. Do additional sanity checking on the page in this case.
67755** And if the fetch fails, this routine must decrement pCur->iPage.
67756**
67757** The page is fetched as read-write unless pCur is not NULL and is
67758** a read-only cursor.
67759**
67760** If an error occurs, then *ppPage is undefined. It
67761** may remain unchanged, or it may be set to an invalid value.
67762*/
67763static int getAndInitPage(
67764 BtShared *pBt, /* The database file */
67765 Pgno pgno, /* Number of the page to get */
67766 MemPage **ppPage, /* Write the page pointer here */
67767 BtCursor *pCur, /* Cursor to receive the page, or NULL */
67768 int bReadOnly /* True for a read-only page */
67769){
67770 int rc;
67771 DbPage *pDbPage;
67772 assert( sqlite3_mutex_held(pBt->mutex) );
67773 assert( pCur==0 || ppPage==&pCur->pPage );
67774 assert( pCur==0 || bReadOnly==pCur->curPagerFlags );
67775 assert( pCur==0 || pCur->iPage>0 );
67776
67777 if( pgno>btreePagecount(pBt) ){
67778 rc = SQLITE_CORRUPT_BKPT;
67779 goto getAndInitPage_error1;
67780 }
67781 rc = sqlite3PagerGet(pPager: pBt->pPager, pgno, ppPage: (DbPage**)&pDbPage, flags: bReadOnly);
67782 if( rc ){
67783 goto getAndInitPage_error1;
67784 }
67785 *ppPage = (MemPage*)sqlite3PagerGetExtra(pPg: pDbPage);
67786 if( (*ppPage)->isInit==0 ){
67787 btreePageFromDbPage(pDbPage, pgno, pBt);
67788 rc = btreeInitPage(pPage: *ppPage);
67789 if( rc!=SQLITE_OK ){
67790 goto getAndInitPage_error2;
67791 }
67792 }
67793 assert( (*ppPage)->pgno==pgno );
67794 assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) );
67795
67796 /* If obtaining a child page for a cursor, we must verify that the page is
67797 ** compatible with the root page. */
67798 if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){
67799 rc = SQLITE_CORRUPT_PGNO(pgno);
67800 goto getAndInitPage_error2;
67801 }
67802 return SQLITE_OK;
67803
67804getAndInitPage_error2:
67805 releasePage(pPage: *ppPage);
67806getAndInitPage_error1:
67807 if( pCur ){
67808 pCur->iPage--;
67809 pCur->pPage = pCur->apPage[pCur->iPage];
67810 }
67811 testcase( pgno==0 );
67812 assert( pgno!=0 || rc==SQLITE_CORRUPT );
67813 return rc;
67814}
67815
67816/*
67817** Release a MemPage. This should be called once for each prior
67818** call to btreeGetPage.
67819**
67820** Page1 is a special case and must be released using releasePageOne().
67821*/
67822static void releasePageNotNull(MemPage *pPage){
67823 assert( pPage->aData );
67824 assert( pPage->pBt );
67825 assert( pPage->pDbPage!=0 );
67826 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
67827 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
67828 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
67829 sqlite3PagerUnrefNotNull(pPg: pPage->pDbPage);
67830}
67831static void releasePage(MemPage *pPage){
67832 if( pPage ) releasePageNotNull(pPage);
67833}
67834static void releasePageOne(MemPage *pPage){
67835 assert( pPage!=0 );
67836 assert( pPage->aData );
67837 assert( pPage->pBt );
67838 assert( pPage->pDbPage!=0 );
67839 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
67840 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
67841 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
67842 sqlite3PagerUnrefPageOne(pPg: pPage->pDbPage);
67843}
67844
67845/*
67846** Get an unused page.
67847**
67848** This works just like btreeGetPage() with the addition:
67849**
67850** * If the page is already in use for some other purpose, immediately
67851** release it and return an SQLITE_CURRUPT error.
67852** * Make sure the isInit flag is clear
67853*/
67854static int btreeGetUnusedPage(
67855 BtShared *pBt, /* The btree */
67856 Pgno pgno, /* Number of the page to fetch */
67857 MemPage **ppPage, /* Return the page in this parameter */
67858 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
67859){
67860 int rc = btreeGetPage(pBt, pgno, ppPage, flags);
67861 if( rc==SQLITE_OK ){
67862 if( sqlite3PagerPageRefcount(pPage: (*ppPage)->pDbPage)>1 ){
67863 releasePage(pPage: *ppPage);
67864 *ppPage = 0;
67865 return SQLITE_CORRUPT_BKPT;
67866 }
67867 (*ppPage)->isInit = 0;
67868 }else{
67869 *ppPage = 0;
67870 }
67871 return rc;
67872}
67873
67874
67875/*
67876** During a rollback, when the pager reloads information into the cache
67877** so that the cache is restored to its original state at the start of
67878** the transaction, for each page restored this routine is called.
67879**
67880** This routine needs to reset the extra data section at the end of the
67881** page to agree with the restored data.
67882*/
67883static void pageReinit(DbPage *pData){
67884 MemPage *pPage;
67885 pPage = (MemPage *)sqlite3PagerGetExtra(pPg: pData);
67886 assert( sqlite3PagerPageRefcount(pData)>0 );
67887 if( pPage->isInit ){
67888 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
67889 pPage->isInit = 0;
67890 if( sqlite3PagerPageRefcount(pPage: pData)>1 ){
67891 /* pPage might not be a btree page; it might be an overflow page
67892 ** or ptrmap page or a free page. In those cases, the following
67893 ** call to btreeInitPage() will likely return SQLITE_CORRUPT.
67894 ** But no harm is done by this. And it is very important that
67895 ** btreeInitPage() be called on every btree page so we make
67896 ** the call for every page that comes in for re-initing. */
67897 btreeInitPage(pPage);
67898 }
67899 }
67900}
67901
67902/*
67903** Invoke the busy handler for a btree.
67904*/
67905static int btreeInvokeBusyHandler(void *pArg){
67906 BtShared *pBt = (BtShared*)pArg;
67907 assert( pBt->db );
67908 assert( sqlite3_mutex_held(pBt->db->mutex) );
67909 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
67910}
67911
67912/*
67913** Open a database file.
67914**
67915** zFilename is the name of the database file. If zFilename is NULL
67916** then an ephemeral database is created. The ephemeral database might
67917** be exclusively in memory, or it might use a disk-based memory cache.
67918** Either way, the ephemeral database will be automatically deleted
67919** when sqlite3BtreeClose() is called.
67920**
67921** If zFilename is ":memory:" then an in-memory database is created
67922** that is automatically destroyed when it is closed.
67923**
67924** The "flags" parameter is a bitmask that might contain bits like
67925** BTREE_OMIT_JOURNAL and/or BTREE_MEMORY.
67926**
67927** If the database is already opened in the same database connection
67928** and we are in shared cache mode, then the open will fail with an
67929** SQLITE_CONSTRAINT error. We cannot allow two or more BtShared
67930** objects in the same database connection since doing so will lead
67931** to problems with locking.
67932*/
67933SQLITE_PRIVATE int sqlite3BtreeOpen(
67934 sqlite3_vfs *pVfs, /* VFS to use for this b-tree */
67935 const char *zFilename, /* Name of the file containing the BTree database */
67936 sqlite3 *db, /* Associated database handle */
67937 Btree **ppBtree, /* Pointer to new Btree object written here */
67938 int flags, /* Options */
67939 int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */
67940){
67941 BtShared *pBt = 0; /* Shared part of btree structure */
67942 Btree *p; /* Handle to return */
67943 sqlite3_mutex *mutexOpen = 0; /* Prevents a race condition. Ticket #3537 */
67944 int rc = SQLITE_OK; /* Result code from this function */
67945 u8 nReserve; /* Byte of unused space on each page */
67946 unsigned char zDbHeader[100]; /* Database header content */
67947
67948 /* True if opening an ephemeral, temporary database */
67949 const int isTempDb = zFilename==0 || zFilename[0]==0;
67950
67951 /* Set the variable isMemdb to true for an in-memory database, or
67952 ** false for a file-based database.
67953 */
67954#ifdef SQLITE_OMIT_MEMORYDB
67955 const int isMemdb = 0;
67956#else
67957 const int isMemdb = (zFilename && strcmp(s1: zFilename, s2: ":memory:")==0)
67958 || (isTempDb && sqlite3TempInMemory(db))
67959 || (vfsFlags & SQLITE_OPEN_MEMORY)!=0;
67960#endif
67961
67962 assert( db!=0 );
67963 assert( pVfs!=0 );
67964 assert( sqlite3_mutex_held(db->mutex) );
67965 assert( (flags&0xff)==flags ); /* flags fit in 8 bits */
67966
67967 /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
67968 assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 );
67969
67970 /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
67971 assert( (flags & BTREE_SINGLE)==0 || isTempDb );
67972
67973 if( isMemdb ){
67974 flags |= BTREE_MEMORY;
67975 }
67976 if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){
67977 vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
67978 }
67979 p = sqlite3MallocZero(n: sizeof(Btree));
67980 if( !p ){
67981 return SQLITE_NOMEM_BKPT;
67982 }
67983 p->inTrans = TRANS_NONE;
67984 p->db = db;
67985#ifndef SQLITE_OMIT_SHARED_CACHE
67986 p->lock.pBtree = p;
67987 p->lock.iTable = 1;
67988#endif
67989
67990#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
67991 /*
67992 ** If this Btree is a candidate for shared cache, try to find an
67993 ** existing BtShared object that we can share with
67994 */
67995 if( isTempDb==0 && (isMemdb==0 || (vfsFlags&SQLITE_OPEN_URI)!=0) ){
67996 if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){
67997 int nFilename = sqlite3Strlen30(z: zFilename)+1;
67998 int nFullPathname = pVfs->mxPathname+1;
67999 char *zFullPathname = sqlite3Malloc(MAX(nFullPathname,nFilename));
68000 MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
68001
68002 p->sharable = 1;
68003 if( !zFullPathname ){
68004 sqlite3_free(p);
68005 return SQLITE_NOMEM_BKPT;
68006 }
68007 if( isMemdb ){
68008 memcpy(dest: zFullPathname, src: zFilename, n: nFilename);
68009 }else{
68010 rc = sqlite3OsFullPathname(pVfs, zPath: zFilename,
68011 nPathOut: nFullPathname, zPathOut: zFullPathname);
68012 if( rc ){
68013 if( rc==SQLITE_OK_SYMLINK ){
68014 rc = SQLITE_OK;
68015 }else{
68016 sqlite3_free(p: zFullPathname);
68017 sqlite3_free(p);
68018 return rc;
68019 }
68020 }
68021 }
68022#if SQLITE_THREADSAFE
68023 mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
68024 sqlite3_mutex_enter(p: mutexOpen);
68025 mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);
68026 sqlite3_mutex_enter(p: mutexShared);
68027#endif
68028 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
68029 assert( pBt->nRef>0 );
68030 if( 0==strcmp(s1: zFullPathname, s2: sqlite3PagerFilename(pPager: pBt->pPager, nullIfMemDb: 0))
68031 && sqlite3PagerVfs(pPager: pBt->pPager)==pVfs ){
68032 int iDb;
68033 for(iDb=db->nDb-1; iDb>=0; iDb--){
68034 Btree *pExisting = db->aDb[iDb].pBt;
68035 if( pExisting && pExisting->pBt==pBt ){
68036 sqlite3_mutex_leave(p: mutexShared);
68037 sqlite3_mutex_leave(p: mutexOpen);
68038 sqlite3_free(p: zFullPathname);
68039 sqlite3_free(p);
68040 return SQLITE_CONSTRAINT;
68041 }
68042 }
68043 p->pBt = pBt;
68044 pBt->nRef++;
68045 break;
68046 }
68047 }
68048 sqlite3_mutex_leave(p: mutexShared);
68049 sqlite3_free(p: zFullPathname);
68050 }
68051#ifdef SQLITE_DEBUG
68052 else{
68053 /* In debug mode, we mark all persistent databases as sharable
68054 ** even when they are not. This exercises the locking code and
68055 ** gives more opportunity for asserts(sqlite3_mutex_held())
68056 ** statements to find locking problems.
68057 */
68058 p->sharable = 1;
68059 }
68060#endif
68061 }
68062#endif
68063 if( pBt==0 ){
68064 /*
68065 ** The following asserts make sure that structures used by the btree are
68066 ** the right size. This is to guard against size changes that result
68067 ** when compiling on a different architecture.
68068 */
68069 assert( sizeof(i64)==8 );
68070 assert( sizeof(u64)==8 );
68071 assert( sizeof(u32)==4 );
68072 assert( sizeof(u16)==2 );
68073 assert( sizeof(Pgno)==4 );
68074
68075 pBt = sqlite3MallocZero( n: sizeof(*pBt) );
68076 if( pBt==0 ){
68077 rc = SQLITE_NOMEM_BKPT;
68078 goto btree_open_out;
68079 }
68080 rc = sqlite3PagerOpen(pVfs, ppPager: &pBt->pPager, zFilename,
68081 nExtra: sizeof(MemPage), flags, vfsFlags, xReinit: pageReinit);
68082 if( rc==SQLITE_OK ){
68083 sqlite3PagerSetMmapLimit(pPager: pBt->pPager, szMmap: db->szMmap);
68084 rc = sqlite3PagerReadFileheader(pPager: pBt->pPager,N: sizeof(zDbHeader),pDest: zDbHeader);
68085 }
68086 if( rc!=SQLITE_OK ){
68087 goto btree_open_out;
68088 }
68089 pBt->openFlags = (u8)flags;
68090 pBt->db = db;
68091 sqlite3PagerSetBusyHandler(pPager: pBt->pPager, xBusyHandler: btreeInvokeBusyHandler, pBusyHandlerArg: pBt);
68092 p->pBt = pBt;
68093
68094 pBt->pCursor = 0;
68095 pBt->pPage1 = 0;
68096 if( sqlite3PagerIsreadonly(pPager: pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
68097#if defined(SQLITE_SECURE_DELETE)
68098 pBt->btsFlags |= BTS_SECURE_DELETE;
68099#elif defined(SQLITE_FAST_SECURE_DELETE)
68100 pBt->btsFlags |= BTS_OVERWRITE;
68101#endif
68102 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
68103 ** determined by the 2-byte integer located at an offset of 16 bytes from
68104 ** the beginning of the database file. */
68105 pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
68106 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
68107 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
68108 pBt->pageSize = 0;
68109#ifndef SQLITE_OMIT_AUTOVACUUM
68110 /* If the magic name ":memory:" will create an in-memory database, then
68111 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
68112 ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
68113 ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
68114 ** regular file-name. In this case the auto-vacuum applies as per normal.
68115 */
68116 if( zFilename && !isMemdb ){
68117 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
68118 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
68119 }
68120#endif
68121 nReserve = 0;
68122 }else{
68123 /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is
68124 ** determined by the one-byte unsigned integer found at an offset of 20
68125 ** into the database file header. */
68126 nReserve = zDbHeader[20];
68127 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
68128#ifndef SQLITE_OMIT_AUTOVACUUM
68129 pBt->autoVacuum = (get4byte(p: &zDbHeader[36 + 4*4])?1:0);
68130 pBt->incrVacuum = (get4byte(p: &zDbHeader[36 + 7*4])?1:0);
68131#endif
68132 }
68133 rc = sqlite3PagerSetPagesize(pPager: pBt->pPager, pPageSize: &pBt->pageSize, nReserve);
68134 if( rc ) goto btree_open_out;
68135 pBt->usableSize = pBt->pageSize - nReserve;
68136 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
68137
68138#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
68139 /* Add the new BtShared object to the linked list sharable BtShareds.
68140 */
68141 pBt->nRef = 1;
68142 if( p->sharable ){
68143 MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
68144 MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);)
68145 if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
68146 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
68147 if( pBt->mutex==0 ){
68148 rc = SQLITE_NOMEM_BKPT;
68149 goto btree_open_out;
68150 }
68151 }
68152 sqlite3_mutex_enter(p: mutexShared);
68153 pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
68154 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt;
68155 sqlite3_mutex_leave(p: mutexShared);
68156 }
68157#endif
68158 }
68159
68160#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
68161 /* If the new Btree uses a sharable pBtShared, then link the new
68162 ** Btree into the list of all sharable Btrees for the same connection.
68163 ** The list is kept in ascending order by pBt address.
68164 */
68165 if( p->sharable ){
68166 int i;
68167 Btree *pSib;
68168 for(i=0; i<db->nDb; i++){
68169 if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
68170 while( pSib->pPrev ){ pSib = pSib->pPrev; }
68171 if( (uptr)p->pBt<(uptr)pSib->pBt ){
68172 p->pNext = pSib;
68173 p->pPrev = 0;
68174 pSib->pPrev = p;
68175 }else{
68176 while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){
68177 pSib = pSib->pNext;
68178 }
68179 p->pNext = pSib->pNext;
68180 p->pPrev = pSib;
68181 if( p->pNext ){
68182 p->pNext->pPrev = p;
68183 }
68184 pSib->pNext = p;
68185 }
68186 break;
68187 }
68188 }
68189 }
68190#endif
68191 *ppBtree = p;
68192
68193btree_open_out:
68194 if( rc!=SQLITE_OK ){
68195 if( pBt && pBt->pPager ){
68196 sqlite3PagerClose(pPager: pBt->pPager, db: 0);
68197 }
68198 sqlite3_free(p: pBt);
68199 sqlite3_free(p);
68200 *ppBtree = 0;
68201 }else{
68202 sqlite3_file *pFile;
68203
68204 /* If the B-Tree was successfully opened, set the pager-cache size to the
68205 ** default value. Except, when opening on an existing shared pager-cache,
68206 ** do not change the pager-cache size.
68207 */
68208 if( sqlite3BtreeSchema(p, 0, 0)==0 ){
68209 sqlite3BtreeSetCacheSize(p, SQLITE_DEFAULT_CACHE_SIZE);
68210 }
68211
68212 pFile = sqlite3PagerFile(pPager: pBt->pPager);
68213 if( pFile->pMethods ){
68214 sqlite3OsFileControlHint(id: pFile, SQLITE_FCNTL_PDB, pArg: (void*)&pBt->db);
68215 }
68216 }
68217 if( mutexOpen ){
68218 assert( sqlite3_mutex_held(mutexOpen) );
68219 sqlite3_mutex_leave(p: mutexOpen);
68220 }
68221 assert( rc!=SQLITE_OK || sqlite3BtreeConnectionCount(*ppBtree)>0 );
68222 return rc;
68223}
68224
68225/*
68226** Decrement the BtShared.nRef counter. When it reaches zero,
68227** remove the BtShared structure from the sharing list. Return
68228** true if the BtShared.nRef counter reaches zero and return
68229** false if it is still positive.
68230*/
68231static int removeFromSharingList(BtShared *pBt){
68232#ifndef SQLITE_OMIT_SHARED_CACHE
68233 MUTEX_LOGIC( sqlite3_mutex *pMainMtx; )
68234 BtShared *pList;
68235 int removed = 0;
68236
68237 assert( sqlite3_mutex_notheld(pBt->mutex) );
68238 MUTEX_LOGIC( pMainMtx = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )
68239 sqlite3_mutex_enter(p: pMainMtx);
68240 pBt->nRef--;
68241 if( pBt->nRef<=0 ){
68242 if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){
68243 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
68244 }else{
68245 pList = GLOBAL(BtShared*,sqlite3SharedCacheList);
68246 while( ALWAYS(pList) && pList->pNext!=pBt ){
68247 pList=pList->pNext;
68248 }
68249 if( ALWAYS(pList) ){
68250 pList->pNext = pBt->pNext;
68251 }
68252 }
68253 if( SQLITE_THREADSAFE ){
68254 sqlite3_mutex_free(p: pBt->mutex);
68255 }
68256 removed = 1;
68257 }
68258 sqlite3_mutex_leave(p: pMainMtx);
68259 return removed;
68260#else
68261 return 1;
68262#endif
68263}
68264
68265/*
68266** Make sure pBt->pTmpSpace points to an allocation of
68267** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child
68268** pointer.
68269*/
68270static void allocateTempSpace(BtShared *pBt){
68271 if( !pBt->pTmpSpace ){
68272 pBt->pTmpSpace = sqlite3PageMalloc( sz: pBt->pageSize );
68273
68274 /* One of the uses of pBt->pTmpSpace is to format cells before
68275 ** inserting them into a leaf page (function fillInCell()). If
68276 ** a cell is less than 4 bytes in size, it is rounded up to 4 bytes
68277 ** by the various routines that manipulate binary cells. Which
68278 ** can mean that fillInCell() only initializes the first 2 or 3
68279 ** bytes of pTmpSpace, but that the first 4 bytes are copied from
68280 ** it into a database page. This is not actually a problem, but it
68281 ** does cause a valgrind error when the 1 or 2 bytes of unitialized
68282 ** data is passed to system call write(). So to avoid this error,
68283 ** zero the first 4 bytes of temp space here.
68284 **
68285 ** Also: Provide four bytes of initialized space before the
68286 ** beginning of pTmpSpace as an area available to prepend the
68287 ** left-child pointer to the beginning of a cell.
68288 */
68289 if( pBt->pTmpSpace ){
68290 memset(s: pBt->pTmpSpace, c: 0, n: 8);
68291 pBt->pTmpSpace += 4;
68292 }
68293 }
68294}
68295
68296/*
68297** Free the pBt->pTmpSpace allocation
68298*/
68299static void freeTempSpace(BtShared *pBt){
68300 if( pBt->pTmpSpace ){
68301 pBt->pTmpSpace -= 4;
68302 sqlite3PageFree(p: pBt->pTmpSpace);
68303 pBt->pTmpSpace = 0;
68304 }
68305}
68306
68307/*
68308** Close an open database and invalidate all cursors.
68309*/
68310SQLITE_PRIVATE int sqlite3BtreeClose(Btree *p){
68311 BtShared *pBt = p->pBt;
68312
68313 /* Close all cursors opened via this handle. */
68314 assert( sqlite3_mutex_held(p->db->mutex) );
68315 sqlite3BtreeEnter(p);
68316
68317 /* Verify that no other cursors have this Btree open */
68318#ifdef SQLITE_DEBUG
68319 {
68320 BtCursor *pCur = pBt->pCursor;
68321 while( pCur ){
68322 BtCursor *pTmp = pCur;
68323 pCur = pCur->pNext;
68324 assert( pTmp->pBtree!=p );
68325
68326 }
68327 }
68328#endif
68329
68330 /* Rollback any active transaction and free the handle structure.
68331 ** The call to sqlite3BtreeRollback() drops any table-locks held by
68332 ** this handle.
68333 */
68334 sqlite3BtreeRollback(p, SQLITE_OK, 0);
68335 sqlite3BtreeLeave(p);
68336
68337 /* If there are still other outstanding references to the shared-btree
68338 ** structure, return now. The remainder of this procedure cleans
68339 ** up the shared-btree.
68340 */
68341 assert( p->wantToLock==0 && p->locked==0 );
68342 if( !p->sharable || removeFromSharingList(pBt) ){
68343 /* The pBt is no longer on the sharing list, so we can access
68344 ** it without having to hold the mutex.
68345 **
68346 ** Clean out and delete the BtShared object.
68347 */
68348 assert( !pBt->pCursor );
68349 sqlite3PagerClose(pPager: pBt->pPager, db: p->db);
68350 if( pBt->xFreeSchema && pBt->pSchema ){
68351 pBt->xFreeSchema(pBt->pSchema);
68352 }
68353 sqlite3DbFree(db: 0, p: pBt->pSchema);
68354 freeTempSpace(pBt);
68355 sqlite3_free(p: pBt);
68356 }
68357
68358#ifndef SQLITE_OMIT_SHARED_CACHE
68359 assert( p->wantToLock==0 );
68360 assert( p->locked==0 );
68361 if( p->pPrev ) p->pPrev->pNext = p->pNext;
68362 if( p->pNext ) p->pNext->pPrev = p->pPrev;
68363#endif
68364
68365 sqlite3_free(p);
68366 return SQLITE_OK;
68367}
68368
68369/*
68370** Change the "soft" limit on the number of pages in the cache.
68371** Unused and unmodified pages will be recycled when the number of
68372** pages in the cache exceeds this soft limit. But the size of the
68373** cache is allowed to grow larger than this limit if it contains
68374** dirty pages or pages still in active use.
68375*/
68376SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
68377 BtShared *pBt = p->pBt;
68378 assert( sqlite3_mutex_held(p->db->mutex) );
68379 sqlite3BtreeEnter(p);
68380 sqlite3PagerSetCachesize(pPager: pBt->pPager, mxPage);
68381 sqlite3BtreeLeave(p);
68382 return SQLITE_OK;
68383}
68384
68385/*
68386** Change the "spill" limit on the number of pages in the cache.
68387** If the number of pages exceeds this limit during a write transaction,
68388** the pager might attempt to "spill" pages to the journal early in
68389** order to free up memory.
68390**
68391** The value returned is the current spill size. If zero is passed
68392** as an argument, no changes are made to the spill size setting, so
68393** using mxPage of 0 is a way to query the current spill size.
68394*/
68395SQLITE_PRIVATE int sqlite3BtreeSetSpillSize(Btree *p, int mxPage){
68396 BtShared *pBt = p->pBt;
68397 int res;
68398 assert( sqlite3_mutex_held(p->db->mutex) );
68399 sqlite3BtreeEnter(p);
68400 res = sqlite3PagerSetSpillsize(pPager: pBt->pPager, mxPage);
68401 sqlite3BtreeLeave(p);
68402 return res;
68403}
68404
68405#if SQLITE_MAX_MMAP_SIZE>0
68406/*
68407** Change the limit on the amount of the database file that may be
68408** memory mapped.
68409*/
68410SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree *p, sqlite3_int64 szMmap){
68411 BtShared *pBt = p->pBt;
68412 assert( sqlite3_mutex_held(p->db->mutex) );
68413 sqlite3BtreeEnter(p);
68414 sqlite3PagerSetMmapLimit(pPager: pBt->pPager, szMmap);
68415 sqlite3BtreeLeave(p);
68416 return SQLITE_OK;
68417}
68418#endif /* SQLITE_MAX_MMAP_SIZE>0 */
68419
68420/*
68421** Change the way data is synced to disk in order to increase or decrease
68422** how well the database resists damage due to OS crashes and power
68423** failures. Level 1 is the same as asynchronous (no syncs() occur and
68424** there is a high probability of damage) Level 2 is the default. There
68425** is a very low but non-zero probability of damage. Level 3 reduces the
68426** probability of damage to near zero but with a write performance reduction.
68427*/
68428#ifndef SQLITE_OMIT_PAGER_PRAGMAS
68429SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(
68430 Btree *p, /* The btree to set the safety level on */
68431 unsigned pgFlags /* Various PAGER_* flags */
68432){
68433 BtShared *pBt = p->pBt;
68434 assert( sqlite3_mutex_held(p->db->mutex) );
68435 sqlite3BtreeEnter(p);
68436 sqlite3PagerSetFlags(pPager: pBt->pPager, pgFlags);
68437 sqlite3BtreeLeave(p);
68438 return SQLITE_OK;
68439}
68440#endif
68441
68442/*
68443** Change the default pages size and the number of reserved bytes per page.
68444** Or, if the page size has already been fixed, return SQLITE_READONLY
68445** without changing anything.
68446**
68447** The page size must be a power of 2 between 512 and 65536. If the page
68448** size supplied does not meet this constraint then the page size is not
68449** changed.
68450**
68451** Page sizes are constrained to be a power of two so that the region
68452** of the database file used for locking (beginning at PENDING_BYTE,
68453** the first byte past the 1GB boundary, 0x40000000) needs to occur
68454** at the beginning of a page.
68455**
68456** If parameter nReserve is less than zero, then the number of reserved
68457** bytes per page is left unchanged.
68458**
68459** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size
68460** and autovacuum mode can no longer be changed.
68461*/
68462SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
68463 int rc = SQLITE_OK;
68464 int x;
68465 BtShared *pBt = p->pBt;
68466 assert( nReserve>=0 && nReserve<=255 );
68467 sqlite3BtreeEnter(p);
68468 pBt->nReserveWanted = nReserve;
68469 x = pBt->pageSize - pBt->usableSize;
68470 if( nReserve<x ) nReserve = x;
68471 if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
68472 sqlite3BtreeLeave(p);
68473 return SQLITE_READONLY;
68474 }
68475 assert( nReserve>=0 && nReserve<=255 );
68476 if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
68477 ((pageSize-1)&pageSize)==0 ){
68478 assert( (pageSize & 7)==0 );
68479 assert( !pBt->pCursor );
68480 if( nReserve>32 && pageSize==512 ) pageSize = 1024;
68481 pBt->pageSize = (u32)pageSize;
68482 freeTempSpace(pBt);
68483 }
68484 rc = sqlite3PagerSetPagesize(pPager: pBt->pPager, pPageSize: &pBt->pageSize, nReserve);
68485 pBt->usableSize = pBt->pageSize - (u16)nReserve;
68486 if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
68487 sqlite3BtreeLeave(p);
68488 return rc;
68489}
68490
68491/*
68492** Return the currently defined page size
68493*/
68494SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree *p){
68495 return p->pBt->pageSize;
68496}
68497
68498/*
68499** This function is similar to sqlite3BtreeGetReserve(), except that it
68500** may only be called if it is guaranteed that the b-tree mutex is already
68501** held.
68502**
68503** This is useful in one special case in the backup API code where it is
68504** known that the shared b-tree mutex is held, but the mutex on the
68505** database handle that owns *p is not. In this case if sqlite3BtreeEnter()
68506** were to be called, it might collide with some other operation on the
68507** database handle that owns *p, causing undefined behavior.
68508*/
68509SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p){
68510 int n;
68511 assert( sqlite3_mutex_held(p->pBt->mutex) );
68512 n = p->pBt->pageSize - p->pBt->usableSize;
68513 return n;
68514}
68515
68516/*
68517** Return the number of bytes of space at the end of every page that
68518** are intentually left unused. This is the "reserved" space that is
68519** sometimes used by extensions.
68520**
68521** The value returned is the larger of the current reserve size and
68522** the latest reserve size requested by SQLITE_FILECTRL_RESERVE_BYTES.
68523** The amount of reserve can only grow - never shrink.
68524*/
68525SQLITE_PRIVATE int sqlite3BtreeGetRequestedReserve(Btree *p){
68526 int n1, n2;
68527 sqlite3BtreeEnter(p);
68528 n1 = (int)p->pBt->nReserveWanted;
68529 n2 = sqlite3BtreeGetReserveNoMutex(p);
68530 sqlite3BtreeLeave(p);
68531 return n1>n2 ? n1 : n2;
68532}
68533
68534
68535/*
68536** Set the maximum page count for a database if mxPage is positive.
68537** No changes are made if mxPage is 0 or negative.
68538** Regardless of the value of mxPage, return the maximum page count.
68539*/
68540SQLITE_PRIVATE Pgno sqlite3BtreeMaxPageCount(Btree *p, Pgno mxPage){
68541 Pgno n;
68542 sqlite3BtreeEnter(p);
68543 n = sqlite3PagerMaxPageCount(pPager: p->pBt->pPager, mxPage);
68544 sqlite3BtreeLeave(p);
68545 return n;
68546}
68547
68548/*
68549** Change the values for the BTS_SECURE_DELETE and BTS_OVERWRITE flags:
68550**
68551** newFlag==0 Both BTS_SECURE_DELETE and BTS_OVERWRITE are cleared
68552** newFlag==1 BTS_SECURE_DELETE set and BTS_OVERWRITE is cleared
68553** newFlag==2 BTS_SECURE_DELETE cleared and BTS_OVERWRITE is set
68554** newFlag==(-1) No changes
68555**
68556** This routine acts as a query if newFlag is less than zero
68557**
68558** With BTS_OVERWRITE set, deleted content is overwritten by zeros, but
68559** freelist leaf pages are not written back to the database. Thus in-page
68560** deleted content is cleared, but freelist deleted content is not.
68561**
68562** With BTS_SECURE_DELETE, operation is like BTS_OVERWRITE with the addition
68563** that freelist leaf pages are written back into the database, increasing
68564** the amount of disk I/O.
68565*/
68566SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
68567 int b;
68568 if( p==0 ) return 0;
68569 sqlite3BtreeEnter(p);
68570 assert( BTS_OVERWRITE==BTS_SECURE_DELETE*2 );
68571 assert( BTS_FAST_SECURE==(BTS_OVERWRITE|BTS_SECURE_DELETE) );
68572 if( newFlag>=0 ){
68573 p->pBt->btsFlags &= ~BTS_FAST_SECURE;
68574 p->pBt->btsFlags |= BTS_SECURE_DELETE*newFlag;
68575 }
68576 b = (p->pBt->btsFlags & BTS_FAST_SECURE)/BTS_SECURE_DELETE;
68577 sqlite3BtreeLeave(p);
68578 return b;
68579}
68580
68581/*
68582** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
68583** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
68584** is disabled. The default value for the auto-vacuum property is
68585** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
68586*/
68587SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
68588#ifdef SQLITE_OMIT_AUTOVACUUM
68589 return SQLITE_READONLY;
68590#else
68591 BtShared *pBt = p->pBt;
68592 int rc = SQLITE_OK;
68593 u8 av = (u8)autoVacuum;
68594
68595 sqlite3BtreeEnter(p);
68596 if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
68597 rc = SQLITE_READONLY;
68598 }else{
68599 pBt->autoVacuum = av ?1:0;
68600 pBt->incrVacuum = av==2 ?1:0;
68601 }
68602 sqlite3BtreeLeave(p);
68603 return rc;
68604#endif
68605}
68606
68607/*
68608** Return the value of the 'auto-vacuum' property. If auto-vacuum is
68609** enabled 1 is returned. Otherwise 0.
68610*/
68611SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *p){
68612#ifdef SQLITE_OMIT_AUTOVACUUM
68613 return BTREE_AUTOVACUUM_NONE;
68614#else
68615 int rc;
68616 sqlite3BtreeEnter(p);
68617 rc = (
68618 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
68619 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
68620 BTREE_AUTOVACUUM_INCR
68621 );
68622 sqlite3BtreeLeave(p);
68623 return rc;
68624#endif
68625}
68626
68627/*
68628** If the user has not set the safety-level for this database connection
68629** using "PRAGMA synchronous", and if the safety-level is not already
68630** set to the value passed to this function as the second parameter,
68631** set it so.
68632*/
68633#if SQLITE_DEFAULT_SYNCHRONOUS!=SQLITE_DEFAULT_WAL_SYNCHRONOUS \
68634 && !defined(SQLITE_OMIT_WAL)
68635static void setDefaultSyncFlag(BtShared *pBt, u8 safety_level){
68636 sqlite3 *db;
68637 Db *pDb;
68638 if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
68639 while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
68640 if( pDb->bSyncSet==0
68641 && pDb->safety_level!=safety_level
68642 && pDb!=&db->aDb[1]
68643 ){
68644 pDb->safety_level = safety_level;
68645 sqlite3PagerSetFlags(pBt->pPager,
68646 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
68647 }
68648 }
68649}
68650#else
68651# define setDefaultSyncFlag(pBt,safety_level)
68652#endif
68653
68654/* Forward declaration */
68655static int newDatabase(BtShared*);
68656
68657
68658/*
68659** Get a reference to pPage1 of the database file. This will
68660** also acquire a readlock on that file.
68661**
68662** SQLITE_OK is returned on success. If the file is not a
68663** well-formed database file, then SQLITE_CORRUPT is returned.
68664** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM
68665** is returned if we run out of memory.
68666*/
68667static int lockBtree(BtShared *pBt){
68668 int rc; /* Result code from subfunctions */
68669 MemPage *pPage1; /* Page 1 of the database file */
68670 u32 nPage; /* Number of pages in the database */
68671 u32 nPageFile = 0; /* Number of pages in the database file */
68672
68673 assert( sqlite3_mutex_held(pBt->mutex) );
68674 assert( pBt->pPage1==0 );
68675 rc = sqlite3PagerSharedLock(pPager: pBt->pPager);
68676 if( rc!=SQLITE_OK ) return rc;
68677 rc = btreeGetPage(pBt, pgno: 1, ppPage: &pPage1, flags: 0);
68678 if( rc!=SQLITE_OK ) return rc;
68679
68680 /* Do some checking to help insure the file we opened really is
68681 ** a valid database file.
68682 */
68683 nPage = get4byte(p: 28+(u8*)pPage1->aData);
68684 sqlite3PagerPagecount(pPager: pBt->pPager, pnPage: (int*)&nPageFile);
68685 if( nPage==0 || memcmp(s1: 24+(u8*)pPage1->aData, s2: 92+(u8*)pPage1->aData,n: 4)!=0 ){
68686 nPage = nPageFile;
68687 }
68688 if( (pBt->db->flags & SQLITE_ResetDatabase)!=0 ){
68689 nPage = 0;
68690 }
68691 if( nPage>0 ){
68692 u32 pageSize;
68693 u32 usableSize;
68694 u8 *page1 = pPage1->aData;
68695 rc = SQLITE_NOTADB;
68696 /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins
68697 ** with the following 16 bytes (in hex): 53 51 4c 69 74 65 20 66 6f 72 6d
68698 ** 61 74 20 33 00. */
68699 if( memcmp(s1: page1, s2: zMagicHeader, n: 16)!=0 ){
68700 goto page1_init_failed;
68701 }
68702
68703#ifdef SQLITE_OMIT_WAL
68704 if( page1[18]>1 ){
68705 pBt->btsFlags |= BTS_READ_ONLY;
68706 }
68707 if( page1[19]>1 ){
68708 goto page1_init_failed;
68709 }
68710#else
68711 if( page1[18]>2 ){
68712 pBt->btsFlags |= BTS_READ_ONLY;
68713 }
68714 if( page1[19]>2 ){
68715 goto page1_init_failed;
68716 }
68717
68718 /* If the read version is set to 2, this database should be accessed
68719 ** in WAL mode. If the log is not already open, open it now. Then
68720 ** return SQLITE_OK and return without populating BtShared.pPage1.
68721 ** The caller detects this and calls this function again. This is
68722 ** required as the version of page 1 currently in the page1 buffer
68723 ** may not be the latest version - there may be a newer one in the log
68724 ** file.
68725 */
68726 if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
68727 int isOpen = 0;
68728 rc = sqlite3PagerOpenWal(pPager: pBt->pPager, pbOpen: &isOpen);
68729 if( rc!=SQLITE_OK ){
68730 goto page1_init_failed;
68731 }else{
68732 setDefaultSyncFlag(pBt, SQLITE_DEFAULT_WAL_SYNCHRONOUS+1);
68733 if( isOpen==0 ){
68734 releasePageOne(pPage: pPage1);
68735 return SQLITE_OK;
68736 }
68737 }
68738 rc = SQLITE_NOTADB;
68739 }else{
68740 setDefaultSyncFlag(pBt, SQLITE_DEFAULT_SYNCHRONOUS+1);
68741 }
68742#endif
68743
68744 /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
68745 ** fractions and the leaf payload fraction values must be 64, 32, and 32.
68746 **
68747 ** The original design allowed these amounts to vary, but as of
68748 ** version 3.6.0, we require them to be fixed.
68749 */
68750 if( memcmp(s1: &page1[21], s2: "\100\040\040",n: 3)!=0 ){
68751 goto page1_init_failed;
68752 }
68753 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
68754 ** determined by the 2-byte integer located at an offset of 16 bytes from
68755 ** the beginning of the database file. */
68756 pageSize = (page1[16]<<8) | (page1[17]<<16);
68757 /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two
68758 ** between 512 and 65536 inclusive. */
68759 if( ((pageSize-1)&pageSize)!=0
68760 || pageSize>SQLITE_MAX_PAGE_SIZE
68761 || pageSize<=256
68762 ){
68763 goto page1_init_failed;
68764 }
68765 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
68766 assert( (pageSize & 7)==0 );
68767 /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte
68768 ** integer at offset 20 is the number of bytes of space at the end of
68769 ** each page to reserve for extensions.
68770 **
68771 ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is
68772 ** determined by the one-byte unsigned integer found at an offset of 20
68773 ** into the database file header. */
68774 usableSize = pageSize - page1[20];
68775 if( (u32)pageSize!=pBt->pageSize ){
68776 /* After reading the first page of the database assuming a page size
68777 ** of BtShared.pageSize, we have discovered that the page-size is
68778 ** actually pageSize. Unlock the database, leave pBt->pPage1 at
68779 ** zero and return SQLITE_OK. The caller will call this function
68780 ** again with the correct page-size.
68781 */
68782 releasePageOne(pPage: pPage1);
68783 pBt->usableSize = usableSize;
68784 pBt->pageSize = pageSize;
68785 freeTempSpace(pBt);
68786 rc = sqlite3PagerSetPagesize(pPager: pBt->pPager, pPageSize: &pBt->pageSize,
68787 nReserve: pageSize-usableSize);
68788 return rc;
68789 }
68790 if( sqlite3WritableSchema(pBt->db)==0 && nPage>nPageFile ){
68791 rc = SQLITE_CORRUPT_BKPT;
68792 goto page1_init_failed;
68793 }
68794 /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
68795 ** be less than 480. In other words, if the page size is 512, then the
68796 ** reserved space size cannot exceed 32. */
68797 if( usableSize<480 ){
68798 goto page1_init_failed;
68799 }
68800 pBt->pageSize = pageSize;
68801 pBt->usableSize = usableSize;
68802#ifndef SQLITE_OMIT_AUTOVACUUM
68803 pBt->autoVacuum = (get4byte(p: &page1[36 + 4*4])?1:0);
68804 pBt->incrVacuum = (get4byte(p: &page1[36 + 7*4])?1:0);
68805#endif
68806 }
68807
68808 /* maxLocal is the maximum amount of payload to store locally for
68809 ** a cell. Make sure it is small enough so that at least minFanout
68810 ** cells can will fit on one page. We assume a 10-byte page header.
68811 ** Besides the payload, the cell must store:
68812 ** 2-byte pointer to the cell
68813 ** 4-byte child pointer
68814 ** 9-byte nKey value
68815 ** 4-byte nData value
68816 ** 4-byte overflow page pointer
68817 ** So a cell consists of a 2-byte pointer, a header which is as much as
68818 ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
68819 ** page pointer.
68820 */
68821 pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
68822 pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
68823 pBt->maxLeaf = (u16)(pBt->usableSize - 35);
68824 pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
68825 if( pBt->maxLocal>127 ){
68826 pBt->max1bytePayload = 127;
68827 }else{
68828 pBt->max1bytePayload = (u8)pBt->maxLocal;
68829 }
68830 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
68831 pBt->pPage1 = pPage1;
68832 pBt->nPage = nPage;
68833 return SQLITE_OK;
68834
68835page1_init_failed:
68836 releasePageOne(pPage: pPage1);
68837 pBt->pPage1 = 0;
68838 return rc;
68839}
68840
68841#ifndef NDEBUG
68842/*
68843** Return the number of cursors open on pBt. This is for use
68844** in assert() expressions, so it is only compiled if NDEBUG is not
68845** defined.
68846**
68847** Only write cursors are counted if wrOnly is true. If wrOnly is
68848** false then all cursors are counted.
68849**
68850** For the purposes of this routine, a cursor is any cursor that
68851** is capable of reading or writing to the database. Cursors that
68852** have been tripped into the CURSOR_FAULT state are not counted.
68853*/
68854static int countValidCursors(BtShared *pBt, int wrOnly){
68855 BtCursor *pCur;
68856 int r = 0;
68857 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
68858 if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0)
68859 && pCur->eState!=CURSOR_FAULT ) r++;
68860 }
68861 return r;
68862}
68863#endif
68864
68865/*
68866** If there are no outstanding cursors and we are not in the middle
68867** of a transaction but there is a read lock on the database, then
68868** this routine unrefs the first page of the database file which
68869** has the effect of releasing the read lock.
68870**
68871** If there is a transaction in progress, this routine is a no-op.
68872*/
68873static void unlockBtreeIfUnused(BtShared *pBt){
68874 assert( sqlite3_mutex_held(pBt->mutex) );
68875 assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
68876 if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
68877 MemPage *pPage1 = pBt->pPage1;
68878 assert( pPage1->aData );
68879 assert( sqlite3PagerRefcount(pBt->pPager)==1 );
68880 pBt->pPage1 = 0;
68881 releasePageOne(pPage: pPage1);
68882 }
68883}
68884
68885/*
68886** If pBt points to an empty file then convert that empty file
68887** into a new empty database by initializing the first page of
68888** the database.
68889*/
68890static int newDatabase(BtShared *pBt){
68891 MemPage *pP1;
68892 unsigned char *data;
68893 int rc;
68894
68895 assert( sqlite3_mutex_held(pBt->mutex) );
68896 if( pBt->nPage>0 ){
68897 return SQLITE_OK;
68898 }
68899 pP1 = pBt->pPage1;
68900 assert( pP1!=0 );
68901 data = pP1->aData;
68902 rc = sqlite3PagerWrite(pPg: pP1->pDbPage);
68903 if( rc ) return rc;
68904 memcpy(dest: data, src: zMagicHeader, n: sizeof(zMagicHeader));
68905 assert( sizeof(zMagicHeader)==16 );
68906 data[16] = (u8)((pBt->pageSize>>8)&0xff);
68907 data[17] = (u8)((pBt->pageSize>>16)&0xff);
68908 data[18] = 1;
68909 data[19] = 1;
68910 assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
68911 data[20] = (u8)(pBt->pageSize - pBt->usableSize);
68912 data[21] = 64;
68913 data[22] = 32;
68914 data[23] = 32;
68915 memset(s: &data[24], c: 0, n: 100-24);
68916 zeroPage(pPage: pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
68917 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
68918#ifndef SQLITE_OMIT_AUTOVACUUM
68919 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
68920 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
68921 put4byte(p: &data[36 + 4*4], v: pBt->autoVacuum);
68922 put4byte(p: &data[36 + 7*4], v: pBt->incrVacuum);
68923#endif
68924 pBt->nPage = 1;
68925 data[31] = 1;
68926 return SQLITE_OK;
68927}
68928
68929/*
68930** Initialize the first page of the database file (creating a database
68931** consisting of a single page and no schema objects). Return SQLITE_OK
68932** if successful, or an SQLite error code otherwise.
68933*/
68934SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p){
68935 int rc;
68936 sqlite3BtreeEnter(p);
68937 p->pBt->nPage = 0;
68938 rc = newDatabase(pBt: p->pBt);
68939 sqlite3BtreeLeave(p);
68940 return rc;
68941}
68942
68943/*
68944** Attempt to start a new transaction. A write-transaction
68945** is started if the second argument is nonzero, otherwise a read-
68946** transaction. If the second argument is 2 or more and exclusive
68947** transaction is started, meaning that no other process is allowed
68948** to access the database. A preexisting transaction may not be
68949** upgraded to exclusive by calling this routine a second time - the
68950** exclusivity flag only works for a new transaction.
68951**
68952** A write-transaction must be started before attempting any
68953** changes to the database. None of the following routines
68954** will work unless a transaction is started first:
68955**
68956** sqlite3BtreeCreateTable()
68957** sqlite3BtreeCreateIndex()
68958** sqlite3BtreeClearTable()
68959** sqlite3BtreeDropTable()
68960** sqlite3BtreeInsert()
68961** sqlite3BtreeDelete()
68962** sqlite3BtreeUpdateMeta()
68963**
68964** If an initial attempt to acquire the lock fails because of lock contention
68965** and the database was previously unlocked, then invoke the busy handler
68966** if there is one. But if there was previously a read-lock, do not
68967** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
68968** returned when there is already a read-lock in order to avoid a deadlock.
68969**
68970** Suppose there are two processes A and B. A has a read lock and B has
68971** a reserved lock. B tries to promote to exclusive but is blocked because
68972** of A's read lock. A tries to promote to reserved but is blocked by B.
68973** One or the other of the two processes must give way or there can be
68974** no progress. By returning SQLITE_BUSY and not invoking the busy callback
68975** when A already has a read lock, we encourage A to give up and let B
68976** proceed.
68977*/
68978SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag, int *pSchemaVersion){
68979 BtShared *pBt = p->pBt;
68980 Pager *pPager = pBt->pPager;
68981 int rc = SQLITE_OK;
68982
68983 sqlite3BtreeEnter(p);
68984 btreeIntegrity(p);
68985
68986 /* If the btree is already in a write-transaction, or it
68987 ** is already in a read-transaction and a read-transaction
68988 ** is requested, this is a no-op.
68989 */
68990 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
68991 goto trans_begun;
68992 }
68993 assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 );
68994
68995 if( (p->db->flags & SQLITE_ResetDatabase)
68996 && sqlite3PagerIsreadonly(pPager)==0
68997 ){
68998 pBt->btsFlags &= ~BTS_READ_ONLY;
68999 }
69000
69001 /* Write transactions are not possible on a read-only database */
69002 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
69003 rc = SQLITE_READONLY;
69004 goto trans_begun;
69005 }
69006
69007#ifndef SQLITE_OMIT_SHARED_CACHE
69008 {
69009 sqlite3 *pBlock = 0;
69010 /* If another database handle has already opened a write transaction
69011 ** on this shared-btree structure and a second write transaction is
69012 ** requested, return SQLITE_LOCKED.
69013 */
69014 if( (wrflag && pBt->inTransaction==TRANS_WRITE)
69015 || (pBt->btsFlags & BTS_PENDING)!=0
69016 ){
69017 pBlock = pBt->pWriter->db;
69018 }else if( wrflag>1 ){
69019 BtLock *pIter;
69020 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
69021 if( pIter->pBtree!=p ){
69022 pBlock = pIter->pBtree->db;
69023 break;
69024 }
69025 }
69026 }
69027 if( pBlock ){
69028 sqlite3ConnectionBlocked(p->db, pBlock);
69029 rc = SQLITE_LOCKED_SHAREDCACHE;
69030 goto trans_begun;
69031 }
69032 }
69033#endif
69034
69035 /* Any read-only or read-write transaction implies a read-lock on
69036 ** page 1. So if some other shared-cache client already has a write-lock
69037 ** on page 1, the transaction cannot be opened. */
69038 rc = querySharedCacheTableLock(p, SCHEMA_ROOT, READ_LOCK);
69039 if( SQLITE_OK!=rc ) goto trans_begun;
69040
69041 pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
69042 if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
69043 do {
69044 sqlite3PagerWalDb(pPager, p->db);
69045
69046#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
69047 /* If transitioning from no transaction directly to a write transaction,
69048 ** block for the WRITER lock first if possible. */
69049 if( pBt->pPage1==0 && wrflag ){
69050 assert( pBt->inTransaction==TRANS_NONE );
69051 rc = sqlite3PagerWalWriteLock(pPager, 1);
69052 if( rc!=SQLITE_BUSY && rc!=SQLITE_OK ) break;
69053 }
69054#endif
69055
69056 /* Call lockBtree() until either pBt->pPage1 is populated or
69057 ** lockBtree() returns something other than SQLITE_OK. lockBtree()
69058 ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
69059 ** reading page 1 it discovers that the page-size of the database
69060 ** file is not pBt->pageSize. In this case lockBtree() will update
69061 ** pBt->pageSize to the page-size of the file on disk.
69062 */
69063 while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
69064
69065 if( rc==SQLITE_OK && wrflag ){
69066 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
69067 rc = SQLITE_READONLY;
69068 }else{
69069 rc = sqlite3PagerBegin(pPager, exFlag: wrflag>1, subjInMemory: sqlite3TempInMemory(p->db));
69070 if( rc==SQLITE_OK ){
69071 rc = newDatabase(pBt);
69072 }else if( rc==SQLITE_BUSY_SNAPSHOT && pBt->inTransaction==TRANS_NONE ){
69073 /* if there was no transaction opened when this function was
69074 ** called and SQLITE_BUSY_SNAPSHOT is returned, change the error
69075 ** code to SQLITE_BUSY. */
69076 rc = SQLITE_BUSY;
69077 }
69078 }
69079 }
69080
69081 if( rc!=SQLITE_OK ){
69082 (void)sqlite3PagerWalWriteLock(pPager, 0);
69083 unlockBtreeIfUnused(pBt);
69084 }
69085 }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
69086 btreeInvokeBusyHandler(pArg: pBt) );
69087 sqlite3PagerWalDb(pPager, 0);
69088#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
69089 if( rc==SQLITE_BUSY_TIMEOUT ) rc = SQLITE_BUSY;
69090#endif
69091
69092 if( rc==SQLITE_OK ){
69093 if( p->inTrans==TRANS_NONE ){
69094 pBt->nTransaction++;
69095#ifndef SQLITE_OMIT_SHARED_CACHE
69096 if( p->sharable ){
69097 assert( p->lock.pBtree==p && p->lock.iTable==1 );
69098 p->lock.eLock = READ_LOCK;
69099 p->lock.pNext = pBt->pLock;
69100 pBt->pLock = &p->lock;
69101 }
69102#endif
69103 }
69104 p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
69105 if( p->inTrans>pBt->inTransaction ){
69106 pBt->inTransaction = p->inTrans;
69107 }
69108 if( wrflag ){
69109 MemPage *pPage1 = pBt->pPage1;
69110#ifndef SQLITE_OMIT_SHARED_CACHE
69111 assert( !pBt->pWriter );
69112 pBt->pWriter = p;
69113 pBt->btsFlags &= ~BTS_EXCLUSIVE;
69114 if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
69115#endif
69116
69117 /* If the db-size header field is incorrect (as it may be if an old
69118 ** client has been writing the database file), update it now. Doing
69119 ** this sooner rather than later means the database size can safely
69120 ** re-read the database size from page 1 if a savepoint or transaction
69121 ** rollback occurs within the transaction.
69122 */
69123 if( pBt->nPage!=get4byte(p: &pPage1->aData[28]) ){
69124 rc = sqlite3PagerWrite(pPg: pPage1->pDbPage);
69125 if( rc==SQLITE_OK ){
69126 put4byte(p: &pPage1->aData[28], v: pBt->nPage);
69127 }
69128 }
69129 }
69130 }
69131
69132trans_begun:
69133 if( rc==SQLITE_OK ){
69134 if( pSchemaVersion ){
69135 *pSchemaVersion = get4byte(p: &pBt->pPage1->aData[40]);
69136 }
69137 if( wrflag ){
69138 /* This call makes sure that the pager has the correct number of
69139 ** open savepoints. If the second parameter is greater than 0 and
69140 ** the sub-journal is not already open, then it will be opened here.
69141 */
69142 rc = sqlite3PagerOpenSavepoint(pPager, nSavepoint: p->db->nSavepoint);
69143 }
69144 }
69145
69146 btreeIntegrity(p);
69147 sqlite3BtreeLeave(p);
69148 return rc;
69149}
69150
69151#ifndef SQLITE_OMIT_AUTOVACUUM
69152
69153/*
69154** Set the pointer-map entries for all children of page pPage. Also, if
69155** pPage contains cells that point to overflow pages, set the pointer
69156** map entries for the overflow pages as well.
69157*/
69158static int setChildPtrmaps(MemPage *pPage){
69159 int i; /* Counter variable */
69160 int nCell; /* Number of cells in page pPage */
69161 int rc; /* Return code */
69162 BtShared *pBt = pPage->pBt;
69163 Pgno pgno = pPage->pgno;
69164
69165 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
69166 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
69167 if( rc!=SQLITE_OK ) return rc;
69168 nCell = pPage->nCell;
69169
69170 for(i=0; i<nCell; i++){
69171 u8 *pCell = findCell(pPage, i);
69172
69173 ptrmapPutOvflPtr(pPage, pSrc: pPage, pCell, pRC: &rc);
69174
69175 if( !pPage->leaf ){
69176 Pgno childPgno = get4byte(p: pCell);
69177 ptrmapPut(pBt, key: childPgno, PTRMAP_BTREE, parent: pgno, pRC: &rc);
69178 }
69179 }
69180
69181 if( !pPage->leaf ){
69182 Pgno childPgno = get4byte(p: &pPage->aData[pPage->hdrOffset+8]);
69183 ptrmapPut(pBt, key: childPgno, PTRMAP_BTREE, parent: pgno, pRC: &rc);
69184 }
69185
69186 return rc;
69187}
69188
69189/*
69190** Somewhere on pPage is a pointer to page iFrom. Modify this pointer so
69191** that it points to iTo. Parameter eType describes the type of pointer to
69192** be modified, as follows:
69193**
69194** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
69195** page of pPage.
69196**
69197** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
69198** page pointed to by one of the cells on pPage.
69199**
69200** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
69201** overflow page in the list.
69202*/
69203static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
69204 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
69205 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
69206 if( eType==PTRMAP_OVERFLOW2 ){
69207 /* The pointer is always the first 4 bytes of the page in this case. */
69208 if( get4byte(p: pPage->aData)!=iFrom ){
69209 return SQLITE_CORRUPT_PAGE(pPage);
69210 }
69211 put4byte(p: pPage->aData, v: iTo);
69212 }else{
69213 int i;
69214 int nCell;
69215 int rc;
69216
69217 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
69218 if( rc ) return rc;
69219 nCell = pPage->nCell;
69220
69221 for(i=0; i<nCell; i++){
69222 u8 *pCell = findCell(pPage, i);
69223 if( eType==PTRMAP_OVERFLOW1 ){
69224 CellInfo info;
69225 pPage->xParseCell(pPage, pCell, &info);
69226 if( info.nLocal<info.nPayload ){
69227 if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
69228 return SQLITE_CORRUPT_PAGE(pPage);
69229 }
69230 if( iFrom==get4byte(p: pCell+info.nSize-4) ){
69231 put4byte(p: pCell+info.nSize-4, v: iTo);
69232 break;
69233 }
69234 }
69235 }else{
69236 if( get4byte(p: pCell)==iFrom ){
69237 put4byte(p: pCell, v: iTo);
69238 break;
69239 }
69240 }
69241 }
69242
69243 if( i==nCell ){
69244 if( eType!=PTRMAP_BTREE ||
69245 get4byte(p: &pPage->aData[pPage->hdrOffset+8])!=iFrom ){
69246 return SQLITE_CORRUPT_PAGE(pPage);
69247 }
69248 put4byte(p: &pPage->aData[pPage->hdrOffset+8], v: iTo);
69249 }
69250 }
69251 return SQLITE_OK;
69252}
69253
69254
69255/*
69256** Move the open database page pDbPage to location iFreePage in the
69257** database. The pDbPage reference remains valid.
69258**
69259** The isCommit flag indicates that there is no need to remember that
69260** the journal needs to be sync()ed before database page pDbPage->pgno
69261** can be written to. The caller has already promised not to write to that
69262** page.
69263*/
69264static int relocatePage(
69265 BtShared *pBt, /* Btree */
69266 MemPage *pDbPage, /* Open page to move */
69267 u8 eType, /* Pointer map 'type' entry for pDbPage */
69268 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
69269 Pgno iFreePage, /* The location to move pDbPage to */
69270 int isCommit /* isCommit flag passed to sqlite3PagerMovepage */
69271){
69272 MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */
69273 Pgno iDbPage = pDbPage->pgno;
69274 Pager *pPager = pBt->pPager;
69275 int rc;
69276
69277 assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 ||
69278 eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
69279 assert( sqlite3_mutex_held(pBt->mutex) );
69280 assert( pDbPage->pBt==pBt );
69281 if( iDbPage<3 ) return SQLITE_CORRUPT_BKPT;
69282
69283 /* Move page iDbPage from its current location to page number iFreePage */
69284 TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n",
69285 iDbPage, iFreePage, iPtrPage, eType));
69286 rc = sqlite3PagerMovepage(pPager, pPg: pDbPage->pDbPage, pgno: iFreePage, isCommit);
69287 if( rc!=SQLITE_OK ){
69288 return rc;
69289 }
69290 pDbPage->pgno = iFreePage;
69291
69292 /* If pDbPage was a btree-page, then it may have child pages and/or cells
69293 ** that point to overflow pages. The pointer map entries for all these
69294 ** pages need to be changed.
69295 **
69296 ** If pDbPage is an overflow page, then the first 4 bytes may store a
69297 ** pointer to a subsequent overflow page. If this is the case, then
69298 ** the pointer map needs to be updated for the subsequent overflow page.
69299 */
69300 if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
69301 rc = setChildPtrmaps(pDbPage);
69302 if( rc!=SQLITE_OK ){
69303 return rc;
69304 }
69305 }else{
69306 Pgno nextOvfl = get4byte(p: pDbPage->aData);
69307 if( nextOvfl!=0 ){
69308 ptrmapPut(pBt, key: nextOvfl, PTRMAP_OVERFLOW2, parent: iFreePage, pRC: &rc);
69309 if( rc!=SQLITE_OK ){
69310 return rc;
69311 }
69312 }
69313 }
69314
69315 /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
69316 ** that it points at iFreePage. Also fix the pointer map entry for
69317 ** iPtrPage.
69318 */
69319 if( eType!=PTRMAP_ROOTPAGE ){
69320 rc = btreeGetPage(pBt, pgno: iPtrPage, ppPage: &pPtrPage, flags: 0);
69321 if( rc!=SQLITE_OK ){
69322 return rc;
69323 }
69324 rc = sqlite3PagerWrite(pPg: pPtrPage->pDbPage);
69325 if( rc!=SQLITE_OK ){
69326 releasePage(pPage: pPtrPage);
69327 return rc;
69328 }
69329 rc = modifyPagePointer(pPage: pPtrPage, iFrom: iDbPage, iTo: iFreePage, eType);
69330 releasePage(pPage: pPtrPage);
69331 if( rc==SQLITE_OK ){
69332 ptrmapPut(pBt, key: iFreePage, eType, parent: iPtrPage, pRC: &rc);
69333 }
69334 }
69335 return rc;
69336}
69337
69338/* Forward declaration required by incrVacuumStep(). */
69339static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
69340
69341/*
69342** Perform a single step of an incremental-vacuum. If successful, return
69343** SQLITE_OK. If there is no work to do (and therefore no point in
69344** calling this function again), return SQLITE_DONE. Or, if an error
69345** occurs, return some other error code.
69346**
69347** More specifically, this function attempts to re-organize the database so
69348** that the last page of the file currently in use is no longer in use.
69349**
69350** Parameter nFin is the number of pages that this database would contain
69351** were this function called until it returns SQLITE_DONE.
69352**
69353** If the bCommit parameter is non-zero, this function assumes that the
69354** caller will keep calling incrVacuumStep() until it returns SQLITE_DONE
69355** or an error. bCommit is passed true for an auto-vacuum-on-commit
69356** operation, or false for an incremental vacuum.
69357*/
69358static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg, int bCommit){
69359 Pgno nFreeList; /* Number of pages still on the free-list */
69360 int rc;
69361
69362 assert( sqlite3_mutex_held(pBt->mutex) );
69363 assert( iLastPg>nFin );
69364
69365 if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
69366 u8 eType;
69367 Pgno iPtrPage;
69368
69369 nFreeList = get4byte(p: &pBt->pPage1->aData[36]);
69370 if( nFreeList==0 ){
69371 return SQLITE_DONE;
69372 }
69373
69374 rc = ptrmapGet(pBt, key: iLastPg, pEType: &eType, pPgno: &iPtrPage);
69375 if( rc!=SQLITE_OK ){
69376 return rc;
69377 }
69378 if( eType==PTRMAP_ROOTPAGE ){
69379 return SQLITE_CORRUPT_BKPT;
69380 }
69381
69382 if( eType==PTRMAP_FREEPAGE ){
69383 if( bCommit==0 ){
69384 /* Remove the page from the files free-list. This is not required
69385 ** if bCommit is non-zero. In that case, the free-list will be
69386 ** truncated to zero after this function returns, so it doesn't
69387 ** matter if it still contains some garbage entries.
69388 */
69389 Pgno iFreePg;
69390 MemPage *pFreePg;
69391 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, BTALLOC_EXACT);
69392 if( rc!=SQLITE_OK ){
69393 return rc;
69394 }
69395 assert( iFreePg==iLastPg );
69396 releasePage(pPage: pFreePg);
69397 }
69398 } else {
69399 Pgno iFreePg; /* Index of free page to move pLastPg to */
69400 MemPage *pLastPg;
69401 u8 eMode = BTALLOC_ANY; /* Mode parameter for allocateBtreePage() */
69402 Pgno iNear = 0; /* nearby parameter for allocateBtreePage() */
69403
69404 rc = btreeGetPage(pBt, pgno: iLastPg, ppPage: &pLastPg, flags: 0);
69405 if( rc!=SQLITE_OK ){
69406 return rc;
69407 }
69408
69409 /* If bCommit is zero, this loop runs exactly once and page pLastPg
69410 ** is swapped with the first free page pulled off the free list.
69411 **
69412 ** On the other hand, if bCommit is greater than zero, then keep
69413 ** looping until a free-page located within the first nFin pages
69414 ** of the file is found.
69415 */
69416 if( bCommit==0 ){
69417 eMode = BTALLOC_LE;
69418 iNear = nFin;
69419 }
69420 do {
69421 MemPage *pFreePg;
69422 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iNear, eMode);
69423 if( rc!=SQLITE_OK ){
69424 releasePage(pPage: pLastPg);
69425 return rc;
69426 }
69427 releasePage(pPage: pFreePg);
69428 }while( bCommit && iFreePg>nFin );
69429 assert( iFreePg<iLastPg );
69430
69431 rc = relocatePage(pBt, pDbPage: pLastPg, eType, iPtrPage, iFreePage: iFreePg, isCommit: bCommit);
69432 releasePage(pPage: pLastPg);
69433 if( rc!=SQLITE_OK ){
69434 return rc;
69435 }
69436 }
69437 }
69438
69439 if( bCommit==0 ){
69440 do {
69441 iLastPg--;
69442 }while( iLastPg==PENDING_BYTE_PAGE(pBt) || PTRMAP_ISPAGE(pBt, iLastPg) );
69443 pBt->bDoTruncate = 1;
69444 pBt->nPage = iLastPg;
69445 }
69446 return SQLITE_OK;
69447}
69448
69449/*
69450** The database opened by the first argument is an auto-vacuum database
69451** nOrig pages in size containing nFree free pages. Return the expected
69452** size of the database in pages following an auto-vacuum operation.
69453*/
69454static Pgno finalDbSize(BtShared *pBt, Pgno nOrig, Pgno nFree){
69455 int nEntry; /* Number of entries on one ptrmap page */
69456 Pgno nPtrmap; /* Number of PtrMap pages to be freed */
69457 Pgno nFin; /* Return value */
69458
69459 nEntry = pBt->usableSize/5;
69460 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
69461 nFin = nOrig - nFree - nPtrmap;
69462 if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){
69463 nFin--;
69464 }
69465 while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
69466 nFin--;
69467 }
69468
69469 return nFin;
69470}
69471
69472/*
69473** A write-transaction must be opened before calling this function.
69474** It performs a single unit of work towards an incremental vacuum.
69475**
69476** If the incremental vacuum is finished after this function has run,
69477** SQLITE_DONE is returned. If it is not finished, but no error occurred,
69478** SQLITE_OK is returned. Otherwise an SQLite error code.
69479*/
69480SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *p){
69481 int rc;
69482 BtShared *pBt = p->pBt;
69483
69484 sqlite3BtreeEnter(p);
69485 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
69486 if( !pBt->autoVacuum ){
69487 rc = SQLITE_DONE;
69488 }else{
69489 Pgno nOrig = btreePagecount(pBt);
69490 Pgno nFree = get4byte(p: &pBt->pPage1->aData[36]);
69491 Pgno nFin = finalDbSize(pBt, nOrig, nFree);
69492
69493 if( nOrig<nFin || nFree>=nOrig ){
69494 rc = SQLITE_CORRUPT_BKPT;
69495 }else if( nFree>0 ){
69496 rc = saveAllCursors(pBt, iRoot: 0, pExcept: 0);
69497 if( rc==SQLITE_OK ){
69498 invalidateAllOverflowCache(pBt);
69499 rc = incrVacuumStep(pBt, nFin, iLastPg: nOrig, bCommit: 0);
69500 }
69501 if( rc==SQLITE_OK ){
69502 rc = sqlite3PagerWrite(pPg: pBt->pPage1->pDbPage);
69503 put4byte(p: &pBt->pPage1->aData[28], v: pBt->nPage);
69504 }
69505 }else{
69506 rc = SQLITE_DONE;
69507 }
69508 }
69509 sqlite3BtreeLeave(p);
69510 return rc;
69511}
69512
69513/*
69514** This routine is called prior to sqlite3PagerCommit when a transaction
69515** is committed for an auto-vacuum database.
69516*/
69517static int autoVacuumCommit(Btree *p){
69518 int rc = SQLITE_OK;
69519 Pager *pPager;
69520 BtShared *pBt;
69521 sqlite3 *db;
69522 VVA_ONLY( int nRef );
69523
69524 assert( p!=0 );
69525 pBt = p->pBt;
69526 pPager = pBt->pPager;
69527 VVA_ONLY( nRef = sqlite3PagerRefcount(pPager); )
69528
69529 assert( sqlite3_mutex_held(pBt->mutex) );
69530 invalidateAllOverflowCache(pBt);
69531 assert(pBt->autoVacuum);
69532 if( !pBt->incrVacuum ){
69533 Pgno nFin; /* Number of pages in database after autovacuuming */
69534 Pgno nFree; /* Number of pages on the freelist initially */
69535 Pgno nVac; /* Number of pages to vacuum */
69536 Pgno iFree; /* The next page to be freed */
69537 Pgno nOrig; /* Database size before freeing */
69538
69539 nOrig = btreePagecount(pBt);
69540 if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){
69541 /* It is not possible to create a database for which the final page
69542 ** is either a pointer-map page or the pending-byte page. If one
69543 ** is encountered, this indicates corruption.
69544 */
69545 return SQLITE_CORRUPT_BKPT;
69546 }
69547
69548 nFree = get4byte(p: &pBt->pPage1->aData[36]);
69549 db = p->db;
69550 if( db->xAutovacPages ){
69551 int iDb;
69552 for(iDb=0; ALWAYS(iDb<db->nDb); iDb++){
69553 if( db->aDb[iDb].pBt==p ) break;
69554 }
69555 nVac = db->xAutovacPages(
69556 db->pAutovacPagesArg,
69557 db->aDb[iDb].zDbSName,
69558 nOrig,
69559 nFree,
69560 pBt->pageSize
69561 );
69562 if( nVac>nFree ){
69563 nVac = nFree;
69564 }
69565 if( nVac==0 ){
69566 return SQLITE_OK;
69567 }
69568 }else{
69569 nVac = nFree;
69570 }
69571 nFin = finalDbSize(pBt, nOrig, nFree: nVac);
69572 if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT;
69573 if( nFin<nOrig ){
69574 rc = saveAllCursors(pBt, iRoot: 0, pExcept: 0);
69575 }
69576 for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
69577 rc = incrVacuumStep(pBt, nFin, iLastPg: iFree, bCommit: nVac==nFree);
69578 }
69579 if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){
69580 rc = sqlite3PagerWrite(pPg: pBt->pPage1->pDbPage);
69581 if( nVac==nFree ){
69582 put4byte(p: &pBt->pPage1->aData[32], v: 0);
69583 put4byte(p: &pBt->pPage1->aData[36], v: 0);
69584 }
69585 put4byte(p: &pBt->pPage1->aData[28], v: nFin);
69586 pBt->bDoTruncate = 1;
69587 pBt->nPage = nFin;
69588 }
69589 if( rc!=SQLITE_OK ){
69590 sqlite3PagerRollback(pPager);
69591 }
69592 }
69593
69594 assert( nRef>=sqlite3PagerRefcount(pPager) );
69595 return rc;
69596}
69597
69598#else /* ifndef SQLITE_OMIT_AUTOVACUUM */
69599# define setChildPtrmaps(x) SQLITE_OK
69600#endif
69601
69602/*
69603** This routine does the first phase of a two-phase commit. This routine
69604** causes a rollback journal to be created (if it does not already exist)
69605** and populated with enough information so that if a power loss occurs
69606** the database can be restored to its original state by playing back
69607** the journal. Then the contents of the journal are flushed out to
69608** the disk. After the journal is safely on oxide, the changes to the
69609** database are written into the database file and flushed to oxide.
69610** At the end of this call, the rollback journal still exists on the
69611** disk and we are still holding all locks, so the transaction has not
69612** committed. See sqlite3BtreeCommitPhaseTwo() for the second phase of the
69613** commit process.
69614**
69615** This call is a no-op if no write-transaction is currently active on pBt.
69616**
69617** Otherwise, sync the database file for the btree pBt. zSuperJrnl points to
69618** the name of a super-journal file that should be written into the
69619** individual journal file, or is NULL, indicating no super-journal file
69620** (single database transaction).
69621**
69622** When this is called, the super-journal should already have been
69623** created, populated with this journal pointer and synced to disk.
69624**
69625** Once this is routine has returned, the only thing required to commit
69626** the write-transaction for this database file is to delete the journal.
69627*/
69628SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zSuperJrnl){
69629 int rc = SQLITE_OK;
69630 if( p->inTrans==TRANS_WRITE ){
69631 BtShared *pBt = p->pBt;
69632 sqlite3BtreeEnter(p);
69633#ifndef SQLITE_OMIT_AUTOVACUUM
69634 if( pBt->autoVacuum ){
69635 rc = autoVacuumCommit(p);
69636 if( rc!=SQLITE_OK ){
69637 sqlite3BtreeLeave(p);
69638 return rc;
69639 }
69640 }
69641 if( pBt->bDoTruncate ){
69642 sqlite3PagerTruncateImage(pPager: pBt->pPager, nPage: pBt->nPage);
69643 }
69644#endif
69645 rc = sqlite3PagerCommitPhaseOne(pPager: pBt->pPager, zSuper: zSuperJrnl, noSync: 0);
69646 sqlite3BtreeLeave(p);
69647 }
69648 return rc;
69649}
69650
69651/*
69652** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback()
69653** at the conclusion of a transaction.
69654*/
69655static void btreeEndTransaction(Btree *p){
69656 BtShared *pBt = p->pBt;
69657 sqlite3 *db = p->db;
69658 assert( sqlite3BtreeHoldsMutex(p) );
69659
69660#ifndef SQLITE_OMIT_AUTOVACUUM
69661 pBt->bDoTruncate = 0;
69662#endif
69663 if( p->inTrans>TRANS_NONE && db->nVdbeRead>1 ){
69664 /* If there are other active statements that belong to this database
69665 ** handle, downgrade to a read-only transaction. The other statements
69666 ** may still be reading from the database. */
69667 downgradeAllSharedCacheTableLocks(p);
69668 p->inTrans = TRANS_READ;
69669 }else{
69670 /* If the handle had any kind of transaction open, decrement the
69671 ** transaction count of the shared btree. If the transaction count
69672 ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused()
69673 ** call below will unlock the pager. */
69674 if( p->inTrans!=TRANS_NONE ){
69675 clearAllSharedCacheTableLocks(p);
69676 pBt->nTransaction--;
69677 if( 0==pBt->nTransaction ){
69678 pBt->inTransaction = TRANS_NONE;
69679 }
69680 }
69681
69682 /* Set the current transaction state to TRANS_NONE and unlock the
69683 ** pager if this call closed the only read or write transaction. */
69684 p->inTrans = TRANS_NONE;
69685 unlockBtreeIfUnused(pBt);
69686 }
69687
69688 btreeIntegrity(p);
69689}
69690
69691/*
69692** Commit the transaction currently in progress.
69693**
69694** This routine implements the second phase of a 2-phase commit. The
69695** sqlite3BtreeCommitPhaseOne() routine does the first phase and should
69696** be invoked prior to calling this routine. The sqlite3BtreeCommitPhaseOne()
69697** routine did all the work of writing information out to disk and flushing the
69698** contents so that they are written onto the disk platter. All this
69699** routine has to do is delete or truncate or zero the header in the
69700** the rollback journal (which causes the transaction to commit) and
69701** drop locks.
69702**
69703** Normally, if an error occurs while the pager layer is attempting to
69704** finalize the underlying journal file, this function returns an error and
69705** the upper layer will attempt a rollback. However, if the second argument
69706** is non-zero then this b-tree transaction is part of a multi-file
69707** transaction. In this case, the transaction has already been committed
69708** (by deleting a super-journal file) and the caller will ignore this
69709** functions return code. So, even if an error occurs in the pager layer,
69710** reset the b-tree objects internal state to indicate that the write
69711** transaction has been closed. This is quite safe, as the pager will have
69712** transitioned to the error state.
69713**
69714** This will release the write lock on the database file. If there
69715** are no active cursors, it also releases the read lock.
69716*/
69717SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){
69718
69719 if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
69720 sqlite3BtreeEnter(p);
69721 btreeIntegrity(p);
69722
69723 /* If the handle has a write-transaction open, commit the shared-btrees
69724 ** transaction and set the shared state to TRANS_READ.
69725 */
69726 if( p->inTrans==TRANS_WRITE ){
69727 int rc;
69728 BtShared *pBt = p->pBt;
69729 assert( pBt->inTransaction==TRANS_WRITE );
69730 assert( pBt->nTransaction>0 );
69731 rc = sqlite3PagerCommitPhaseTwo(pPager: pBt->pPager);
69732 if( rc!=SQLITE_OK && bCleanup==0 ){
69733 sqlite3BtreeLeave(p);
69734 return rc;
69735 }
69736 p->iBDataVersion--; /* Compensate for pPager->iDataVersion++; */
69737 pBt->inTransaction = TRANS_READ;
69738 btreeClearHasContent(pBt);
69739 }
69740
69741 btreeEndTransaction(p);
69742 sqlite3BtreeLeave(p);
69743 return SQLITE_OK;
69744}
69745
69746/*
69747** Do both phases of a commit.
69748*/
69749SQLITE_PRIVATE int sqlite3BtreeCommit(Btree *p){
69750 int rc;
69751 sqlite3BtreeEnter(p);
69752 rc = sqlite3BtreeCommitPhaseOne(p, zSuperJrnl: 0);
69753 if( rc==SQLITE_OK ){
69754 rc = sqlite3BtreeCommitPhaseTwo(p, bCleanup: 0);
69755 }
69756 sqlite3BtreeLeave(p);
69757 return rc;
69758}
69759
69760/*
69761** This routine sets the state to CURSOR_FAULT and the error
69762** code to errCode for every cursor on any BtShared that pBtree
69763** references. Or if the writeOnly flag is set to 1, then only
69764** trip write cursors and leave read cursors unchanged.
69765**
69766** Every cursor is a candidate to be tripped, including cursors
69767** that belong to other database connections that happen to be
69768** sharing the cache with pBtree.
69769**
69770** This routine gets called when a rollback occurs. If the writeOnly
69771** flag is true, then only write-cursors need be tripped - read-only
69772** cursors save their current positions so that they may continue
69773** following the rollback. Or, if writeOnly is false, all cursors are
69774** tripped. In general, writeOnly is false if the transaction being
69775** rolled back modified the database schema. In this case b-tree root
69776** pages may be moved or deleted from the database altogether, making
69777** it unsafe for read cursors to continue.
69778**
69779** If the writeOnly flag is true and an error is encountered while
69780** saving the current position of a read-only cursor, all cursors,
69781** including all read-cursors are tripped.
69782**
69783** SQLITE_OK is returned if successful, or if an error occurs while
69784** saving a cursor position, an SQLite error code.
69785*/
69786SQLITE_PRIVATE int sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int writeOnly){
69787 BtCursor *p;
69788 int rc = SQLITE_OK;
69789
69790 assert( (writeOnly==0 || writeOnly==1) && BTCF_WriteFlag==1 );
69791 if( pBtree ){
69792 sqlite3BtreeEnter(p: pBtree);
69793 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
69794 if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){
69795 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
69796 rc = saveCursorPosition(pCur: p);
69797 if( rc!=SQLITE_OK ){
69798 (void)sqlite3BtreeTripAllCursors(pBtree, errCode: rc, writeOnly: 0);
69799 break;
69800 }
69801 }
69802 }else{
69803 sqlite3BtreeClearCursor(pCur: p);
69804 p->eState = CURSOR_FAULT;
69805 p->skipNext = errCode;
69806 }
69807 btreeReleaseAllCursorPages(pCur: p);
69808 }
69809 sqlite3BtreeLeave(p: pBtree);
69810 }
69811 return rc;
69812}
69813
69814/*
69815** Set the pBt->nPage field correctly, according to the current
69816** state of the database. Assume pBt->pPage1 is valid.
69817*/
69818static void btreeSetNPage(BtShared *pBt, MemPage *pPage1){
69819 int nPage = get4byte(p: &pPage1->aData[28]);
69820 testcase( nPage==0 );
69821 if( nPage==0 ) sqlite3PagerPagecount(pPager: pBt->pPager, pnPage: &nPage);
69822 testcase( pBt->nPage!=(u32)nPage );
69823 pBt->nPage = nPage;
69824}
69825
69826/*
69827** Rollback the transaction in progress.
69828**
69829** If tripCode is not SQLITE_OK then cursors will be invalidated (tripped).
69830** Only write cursors are tripped if writeOnly is true but all cursors are
69831** tripped if writeOnly is false. Any attempt to use
69832** a tripped cursor will result in an error.
69833**
69834** This will release the write lock on the database file. If there
69835** are no active cursors, it also releases the read lock.
69836*/
69837SQLITE_PRIVATE int sqlite3BtreeRollback(Btree *p, int tripCode, int writeOnly){
69838 int rc;
69839 BtShared *pBt = p->pBt;
69840 MemPage *pPage1;
69841
69842 assert( writeOnly==1 || writeOnly==0 );
69843 assert( tripCode==SQLITE_ABORT_ROLLBACK || tripCode==SQLITE_OK );
69844 sqlite3BtreeEnter(p);
69845 if( tripCode==SQLITE_OK ){
69846 rc = tripCode = saveAllCursors(pBt, iRoot: 0, pExcept: 0);
69847 if( rc ) writeOnly = 0;
69848 }else{
69849 rc = SQLITE_OK;
69850 }
69851 if( tripCode ){
69852 int rc2 = sqlite3BtreeTripAllCursors(pBtree: p, errCode: tripCode, writeOnly);
69853 assert( rc==SQLITE_OK || (writeOnly==0 && rc2==SQLITE_OK) );
69854 if( rc2!=SQLITE_OK ) rc = rc2;
69855 }
69856 btreeIntegrity(p);
69857
69858 if( p->inTrans==TRANS_WRITE ){
69859 int rc2;
69860
69861 assert( TRANS_WRITE==pBt->inTransaction );
69862 rc2 = sqlite3PagerRollback(pPager: pBt->pPager);
69863 if( rc2!=SQLITE_OK ){
69864 rc = rc2;
69865 }
69866
69867 /* The rollback may have destroyed the pPage1->aData value. So
69868 ** call btreeGetPage() on page 1 again to make
69869 ** sure pPage1->aData is set correctly. */
69870 if( btreeGetPage(pBt, pgno: 1, ppPage: &pPage1, flags: 0)==SQLITE_OK ){
69871 btreeSetNPage(pBt, pPage1);
69872 releasePageOne(pPage: pPage1);
69873 }
69874 assert( countValidCursors(pBt, 1)==0 );
69875 pBt->inTransaction = TRANS_READ;
69876 btreeClearHasContent(pBt);
69877 }
69878
69879 btreeEndTransaction(p);
69880 sqlite3BtreeLeave(p);
69881 return rc;
69882}
69883
69884/*
69885** Start a statement subtransaction. The subtransaction can be rolled
69886** back independently of the main transaction. You must start a transaction
69887** before starting a subtransaction. The subtransaction is ended automatically
69888** if the main transaction commits or rolls back.
69889**
69890** Statement subtransactions are used around individual SQL statements
69891** that are contained within a BEGIN...COMMIT block. If a constraint
69892** error occurs within the statement, the effect of that one statement
69893** can be rolled back without having to rollback the entire transaction.
69894**
69895** A statement sub-transaction is implemented as an anonymous savepoint. The
69896** value passed as the second parameter is the total number of savepoints,
69897** including the new anonymous savepoint, open on the B-Tree. i.e. if there
69898** are no active savepoints and no other statement-transactions open,
69899** iStatement is 1. This anonymous savepoint can be released or rolled back
69900** using the sqlite3BtreeSavepoint() function.
69901*/
69902SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
69903 int rc;
69904 BtShared *pBt = p->pBt;
69905 sqlite3BtreeEnter(p);
69906 assert( p->inTrans==TRANS_WRITE );
69907 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
69908 assert( iStatement>0 );
69909 assert( iStatement>p->db->nSavepoint );
69910 assert( pBt->inTransaction==TRANS_WRITE );
69911 /* At the pager level, a statement transaction is a savepoint with
69912 ** an index greater than all savepoints created explicitly using
69913 ** SQL statements. It is illegal to open, release or rollback any
69914 ** such savepoints while the statement transaction savepoint is active.
69915 */
69916 rc = sqlite3PagerOpenSavepoint(pPager: pBt->pPager, nSavepoint: iStatement);
69917 sqlite3BtreeLeave(p);
69918 return rc;
69919}
69920
69921/*
69922** The second argument to this function, op, is always SAVEPOINT_ROLLBACK
69923** or SAVEPOINT_RELEASE. This function either releases or rolls back the
69924** savepoint identified by parameter iSavepoint, depending on the value
69925** of op.
69926**
69927** Normally, iSavepoint is greater than or equal to zero. However, if op is
69928** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
69929** contents of the entire transaction are rolled back. This is different
69930** from a normal transaction rollback, as no locks are released and the
69931** transaction remains open.
69932*/
69933SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
69934 int rc = SQLITE_OK;
69935 if( p && p->inTrans==TRANS_WRITE ){
69936 BtShared *pBt = p->pBt;
69937 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
69938 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
69939 sqlite3BtreeEnter(p);
69940 if( op==SAVEPOINT_ROLLBACK ){
69941 rc = saveAllCursors(pBt, iRoot: 0, pExcept: 0);
69942 }
69943 if( rc==SQLITE_OK ){
69944 rc = sqlite3PagerSavepoint(pPager: pBt->pPager, op, iSavepoint);
69945 }
69946 if( rc==SQLITE_OK ){
69947 if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
69948 pBt->nPage = 0;
69949 }
69950 rc = newDatabase(pBt);
69951 btreeSetNPage(pBt, pPage1: pBt->pPage1);
69952
69953 /* pBt->nPage might be zero if the database was corrupt when
69954 ** the transaction was started. Otherwise, it must be at least 1. */
69955 assert( CORRUPT_DB || pBt->nPage>0 );
69956 }
69957 sqlite3BtreeLeave(p);
69958 }
69959 return rc;
69960}
69961
69962/*
69963** Create a new cursor for the BTree whose root is on the page
69964** iTable. If a read-only cursor is requested, it is assumed that
69965** the caller already has at least a read-only transaction open
69966** on the database already. If a write-cursor is requested, then
69967** the caller is assumed to have an open write transaction.
69968**
69969** If the BTREE_WRCSR bit of wrFlag is clear, then the cursor can only
69970** be used for reading. If the BTREE_WRCSR bit is set, then the cursor
69971** can be used for reading or for writing if other conditions for writing
69972** are also met. These are the conditions that must be met in order
69973** for writing to be allowed:
69974**
69975** 1: The cursor must have been opened with wrFlag containing BTREE_WRCSR
69976**
69977** 2: Other database connections that share the same pager cache
69978** but which are not in the READ_UNCOMMITTED state may not have
69979** cursors open with wrFlag==0 on the same table. Otherwise
69980** the changes made by this write cursor would be visible to
69981** the read cursors in the other database connection.
69982**
69983** 3: The database must be writable (not on read-only media)
69984**
69985** 4: There must be an active transaction.
69986**
69987** The BTREE_FORDELETE bit of wrFlag may optionally be set if BTREE_WRCSR
69988** is set. If FORDELETE is set, that is a hint to the implementation that
69989** this cursor will only be used to seek to and delete entries of an index
69990** as part of a larger DELETE statement. The FORDELETE hint is not used by
69991** this implementation. But in a hypothetical alternative storage engine
69992** in which index entries are automatically deleted when corresponding table
69993** rows are deleted, the FORDELETE flag is a hint that all SEEK and DELETE
69994** operations on this cursor can be no-ops and all READ operations can
69995** return a null row (2-bytes: 0x01 0x00).
69996**
69997** No checking is done to make sure that page iTable really is the
69998** root page of a b-tree. If it is not, then the cursor acquired
69999** will not work correctly.
70000**
70001** It is assumed that the sqlite3BtreeCursorZero() has been called
70002** on pCur to initialize the memory space prior to invoking this routine.
70003*/
70004static int btreeCursor(
70005 Btree *p, /* The btree */
70006 Pgno iTable, /* Root page of table to open */
70007 int wrFlag, /* 1 to write. 0 read-only */
70008 struct KeyInfo *pKeyInfo, /* First arg to comparison function */
70009 BtCursor *pCur /* Space for new cursor */
70010){
70011 BtShared *pBt = p->pBt; /* Shared b-tree handle */
70012 BtCursor *pX; /* Looping over other all cursors */
70013
70014 assert( sqlite3BtreeHoldsMutex(p) );
70015 assert( wrFlag==0
70016 || wrFlag==BTREE_WRCSR
70017 || wrFlag==(BTREE_WRCSR|BTREE_FORDELETE)
70018 );
70019
70020 /* The following assert statements verify that if this is a sharable
70021 ** b-tree database, the connection is holding the required table locks,
70022 ** and that no other connection has any open cursor that conflicts with
70023 ** this lock. The iTable<1 term disables the check for corrupt schemas. */
70024 assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, (wrFlag?2:1))
70025 || iTable<1 );
70026 assert( wrFlag==0 || !hasReadConflicts(p, iTable) );
70027
70028 /* Assert that the caller has opened the required transaction. */
70029 assert( p->inTrans>TRANS_NONE );
70030 assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
70031 assert( pBt->pPage1 && pBt->pPage1->aData );
70032 assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 );
70033
70034 if( wrFlag ){
70035 allocateTempSpace(pBt);
70036 if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM_BKPT;
70037 }
70038 if( iTable<=1 ){
70039 if( iTable<1 ){
70040 return SQLITE_CORRUPT_BKPT;
70041 }else if( btreePagecount(pBt)==0 ){
70042 assert( wrFlag==0 );
70043 iTable = 0;
70044 }
70045 }
70046
70047 /* Now that no other errors can occur, finish filling in the BtCursor
70048 ** variables and link the cursor into the BtShared list. */
70049 pCur->pgnoRoot = iTable;
70050 pCur->iPage = -1;
70051 pCur->pKeyInfo = pKeyInfo;
70052 pCur->pBtree = p;
70053 pCur->pBt = pBt;
70054 pCur->curFlags = wrFlag ? BTCF_WriteFlag : 0;
70055 pCur->curPagerFlags = wrFlag ? 0 : PAGER_GET_READONLY;
70056 /* If there are two or more cursors on the same btree, then all such
70057 ** cursors *must* have the BTCF_Multiple flag set. */
70058 for(pX=pBt->pCursor; pX; pX=pX->pNext){
70059 if( pX->pgnoRoot==iTable ){
70060 pX->curFlags |= BTCF_Multiple;
70061 pCur->curFlags |= BTCF_Multiple;
70062 }
70063 }
70064 pCur->pNext = pBt->pCursor;
70065 pBt->pCursor = pCur;
70066 pCur->eState = CURSOR_INVALID;
70067 return SQLITE_OK;
70068}
70069static int btreeCursorWithLock(
70070 Btree *p, /* The btree */
70071 Pgno iTable, /* Root page of table to open */
70072 int wrFlag, /* 1 to write. 0 read-only */
70073 struct KeyInfo *pKeyInfo, /* First arg to comparison function */
70074 BtCursor *pCur /* Space for new cursor */
70075){
70076 int rc;
70077 sqlite3BtreeEnter(p);
70078 rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
70079 sqlite3BtreeLeave(p);
70080 return rc;
70081}
70082SQLITE_PRIVATE int sqlite3BtreeCursor(
70083 Btree *p, /* The btree */
70084 Pgno iTable, /* Root page of table to open */
70085 int wrFlag, /* 1 to write. 0 read-only */
70086 struct KeyInfo *pKeyInfo, /* First arg to xCompare() */
70087 BtCursor *pCur /* Write new cursor here */
70088){
70089 if( p->sharable ){
70090 return btreeCursorWithLock(p, iTable, wrFlag, pKeyInfo, pCur);
70091 }else{
70092 return btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
70093 }
70094}
70095
70096/*
70097** Return the size of a BtCursor object in bytes.
70098**
70099** This interfaces is needed so that users of cursors can preallocate
70100** sufficient storage to hold a cursor. The BtCursor object is opaque
70101** to users so they cannot do the sizeof() themselves - they must call
70102** this routine.
70103*/
70104SQLITE_PRIVATE int sqlite3BtreeCursorSize(void){
70105 return ROUND8(sizeof(BtCursor));
70106}
70107
70108/*
70109** Initialize memory that will be converted into a BtCursor object.
70110**
70111** The simple approach here would be to memset() the entire object
70112** to zero. But it turns out that the apPage[] and aiIdx[] arrays
70113** do not need to be zeroed and they are large, so we can save a lot
70114** of run-time by skipping the initialization of those elements.
70115*/
70116SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor *p){
70117 memset(s: p, c: 0, offsetof(BtCursor, BTCURSOR_FIRST_UNINIT));
70118}
70119
70120/*
70121** Close a cursor. The read lock on the database file is released
70122** when the last cursor is closed.
70123*/
70124SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){
70125 Btree *pBtree = pCur->pBtree;
70126 if( pBtree ){
70127 BtShared *pBt = pCur->pBt;
70128 sqlite3BtreeEnter(p: pBtree);
70129 assert( pBt->pCursor!=0 );
70130 if( pBt->pCursor==pCur ){
70131 pBt->pCursor = pCur->pNext;
70132 }else{
70133 BtCursor *pPrev = pBt->pCursor;
70134 do{
70135 if( pPrev->pNext==pCur ){
70136 pPrev->pNext = pCur->pNext;
70137 break;
70138 }
70139 pPrev = pPrev->pNext;
70140 }while( ALWAYS(pPrev) );
70141 }
70142 btreeReleaseAllCursorPages(pCur);
70143 unlockBtreeIfUnused(pBt);
70144 sqlite3_free(p: pCur->aOverflow);
70145 sqlite3_free(p: pCur->pKey);
70146 if( (pBt->openFlags & BTREE_SINGLE) && pBt->pCursor==0 ){
70147 /* Since the BtShared is not sharable, there is no need to
70148 ** worry about the missing sqlite3BtreeLeave() call here. */
70149 assert( pBtree->sharable==0 );
70150 sqlite3BtreeClose(p: pBtree);
70151 }else{
70152 sqlite3BtreeLeave(p: pBtree);
70153 }
70154 pCur->pBtree = 0;
70155 }
70156 return SQLITE_OK;
70157}
70158
70159/*
70160** Make sure the BtCursor* given in the argument has a valid
70161** BtCursor.info structure. If it is not already valid, call
70162** btreeParseCell() to fill it in.
70163**
70164** BtCursor.info is a cache of the information in the current cell.
70165** Using this cache reduces the number of calls to btreeParseCell().
70166*/
70167#ifndef NDEBUG
70168 static int cellInfoEqual(CellInfo *a, CellInfo *b){
70169 if( a->nKey!=b->nKey ) return 0;
70170 if( a->pPayload!=b->pPayload ) return 0;
70171 if( a->nPayload!=b->nPayload ) return 0;
70172 if( a->nLocal!=b->nLocal ) return 0;
70173 if( a->nSize!=b->nSize ) return 0;
70174 return 1;
70175 }
70176 static void assertCellInfo(BtCursor *pCur){
70177 CellInfo info;
70178 memset(&info, 0, sizeof(info));
70179 btreeParseCell(pCur->pPage, pCur->ix, &info);
70180 assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) );
70181 }
70182#else
70183 #define assertCellInfo(x)
70184#endif
70185static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){
70186 if( pCur->info.nSize==0 ){
70187 pCur->curFlags |= BTCF_ValidNKey;
70188 btreeParseCell(pPage: pCur->pPage,iCell: pCur->ix,pInfo: &pCur->info);
70189 }else{
70190 assertCellInfo(pCur);
70191 }
70192}
70193
70194#ifndef NDEBUG /* The next routine used only within assert() statements */
70195/*
70196** Return true if the given BtCursor is valid. A valid cursor is one
70197** that is currently pointing to a row in a (non-empty) table.
70198** This is a verification routine is used only within assert() statements.
70199*/
70200SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor *pCur){
70201 return pCur && pCur->eState==CURSOR_VALID;
70202}
70203#endif /* NDEBUG */
70204SQLITE_PRIVATE int sqlite3BtreeCursorIsValidNN(BtCursor *pCur){
70205 assert( pCur!=0 );
70206 return pCur->eState==CURSOR_VALID;
70207}
70208
70209/*
70210** Return the value of the integer key or "rowid" for a table btree.
70211** This routine is only valid for a cursor that is pointing into a
70212** ordinary table btree. If the cursor points to an index btree or
70213** is invalid, the result of this routine is undefined.
70214*/
70215SQLITE_PRIVATE i64 sqlite3BtreeIntegerKey(BtCursor *pCur){
70216 assert( cursorHoldsMutex(pCur) );
70217 assert( pCur->eState==CURSOR_VALID );
70218 assert( pCur->curIntKey );
70219 getCellInfo(pCur);
70220 return pCur->info.nKey;
70221}
70222
70223/*
70224** Pin or unpin a cursor.
70225*/
70226SQLITE_PRIVATE void sqlite3BtreeCursorPin(BtCursor *pCur){
70227 assert( (pCur->curFlags & BTCF_Pinned)==0 );
70228 pCur->curFlags |= BTCF_Pinned;
70229}
70230SQLITE_PRIVATE void sqlite3BtreeCursorUnpin(BtCursor *pCur){
70231 assert( (pCur->curFlags & BTCF_Pinned)!=0 );
70232 pCur->curFlags &= ~BTCF_Pinned;
70233}
70234
70235#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
70236/*
70237** Return the offset into the database file for the start of the
70238** payload to which the cursor is pointing.
70239*/
70240SQLITE_PRIVATE i64 sqlite3BtreeOffset(BtCursor *pCur){
70241 assert( cursorHoldsMutex(pCur) );
70242 assert( pCur->eState==CURSOR_VALID );
70243 getCellInfo(pCur);
70244 return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) +
70245 (i64)(pCur->info.pPayload - pCur->pPage->aData);
70246}
70247#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
70248
70249/*
70250** Return the number of bytes of payload for the entry that pCur is
70251** currently pointing to. For table btrees, this will be the amount
70252** of data. For index btrees, this will be the size of the key.
70253**
70254** The caller must guarantee that the cursor is pointing to a non-NULL
70255** valid entry. In other words, the calling procedure must guarantee
70256** that the cursor has Cursor.eState==CURSOR_VALID.
70257*/
70258SQLITE_PRIVATE u32 sqlite3BtreePayloadSize(BtCursor *pCur){
70259 assert( cursorHoldsMutex(pCur) );
70260 assert( pCur->eState==CURSOR_VALID );
70261 getCellInfo(pCur);
70262 return pCur->info.nPayload;
70263}
70264
70265/*
70266** Return an upper bound on the size of any record for the table
70267** that the cursor is pointing into.
70268**
70269** This is an optimization. Everything will still work if this
70270** routine always returns 2147483647 (which is the largest record
70271** that SQLite can handle) or more. But returning a smaller value might
70272** prevent large memory allocations when trying to interpret a
70273** corrupt datrabase.
70274**
70275** The current implementation merely returns the size of the underlying
70276** database file.
70277*/
70278SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeMaxRecordSize(BtCursor *pCur){
70279 assert( cursorHoldsMutex(pCur) );
70280 assert( pCur->eState==CURSOR_VALID );
70281 return pCur->pBt->pageSize * (sqlite3_int64)pCur->pBt->nPage;
70282}
70283
70284/*
70285** Given the page number of an overflow page in the database (parameter
70286** ovfl), this function finds the page number of the next page in the
70287** linked list of overflow pages. If possible, it uses the auto-vacuum
70288** pointer-map data instead of reading the content of page ovfl to do so.
70289**
70290** If an error occurs an SQLite error code is returned. Otherwise:
70291**
70292** The page number of the next overflow page in the linked list is
70293** written to *pPgnoNext. If page ovfl is the last page in its linked
70294** list, *pPgnoNext is set to zero.
70295**
70296** If ppPage is not NULL, and a reference to the MemPage object corresponding
70297** to page number pOvfl was obtained, then *ppPage is set to point to that
70298** reference. It is the responsibility of the caller to call releasePage()
70299** on *ppPage to free the reference. In no reference was obtained (because
70300** the pointer-map was used to obtain the value for *pPgnoNext), then
70301** *ppPage is set to zero.
70302*/
70303static int getOverflowPage(
70304 BtShared *pBt, /* The database file */
70305 Pgno ovfl, /* Current overflow page number */
70306 MemPage **ppPage, /* OUT: MemPage handle (may be NULL) */
70307 Pgno *pPgnoNext /* OUT: Next overflow page number */
70308){
70309 Pgno next = 0;
70310 MemPage *pPage = 0;
70311 int rc = SQLITE_OK;
70312
70313 assert( sqlite3_mutex_held(pBt->mutex) );
70314 assert(pPgnoNext);
70315
70316#ifndef SQLITE_OMIT_AUTOVACUUM
70317 /* Try to find the next page in the overflow list using the
70318 ** autovacuum pointer-map pages. Guess that the next page in
70319 ** the overflow list is page number (ovfl+1). If that guess turns
70320 ** out to be wrong, fall back to loading the data of page
70321 ** number ovfl to determine the next page number.
70322 */
70323 if( pBt->autoVacuum ){
70324 Pgno pgno;
70325 Pgno iGuess = ovfl+1;
70326 u8 eType;
70327
70328 while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
70329 iGuess++;
70330 }
70331
70332 if( iGuess<=btreePagecount(pBt) ){
70333 rc = ptrmapGet(pBt, key: iGuess, pEType: &eType, pPgno: &pgno);
70334 if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
70335 next = iGuess;
70336 rc = SQLITE_DONE;
70337 }
70338 }
70339 }
70340#endif
70341
70342 assert( next==0 || rc==SQLITE_DONE );
70343 if( rc==SQLITE_OK ){
70344 rc = btreeGetPage(pBt, pgno: ovfl, ppPage: &pPage, flags: (ppPage==0) ? PAGER_GET_READONLY : 0);
70345 assert( rc==SQLITE_OK || pPage==0 );
70346 if( rc==SQLITE_OK ){
70347 next = get4byte(p: pPage->aData);
70348 }
70349 }
70350
70351 *pPgnoNext = next;
70352 if( ppPage ){
70353 *ppPage = pPage;
70354 }else{
70355 releasePage(pPage);
70356 }
70357 return (rc==SQLITE_DONE ? SQLITE_OK : rc);
70358}
70359
70360/*
70361** Copy data from a buffer to a page, or from a page to a buffer.
70362**
70363** pPayload is a pointer to data stored on database page pDbPage.
70364** If argument eOp is false, then nByte bytes of data are copied
70365** from pPayload to the buffer pointed at by pBuf. If eOp is true,
70366** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
70367** of data are copied from the buffer pBuf to pPayload.
70368**
70369** SQLITE_OK is returned on success, otherwise an error code.
70370*/
70371static int copyPayload(
70372 void *pPayload, /* Pointer to page data */
70373 void *pBuf, /* Pointer to buffer */
70374 int nByte, /* Number of bytes to copy */
70375 int eOp, /* 0 -> copy from page, 1 -> copy to page */
70376 DbPage *pDbPage /* Page containing pPayload */
70377){
70378 if( eOp ){
70379 /* Copy data from buffer to page (a write operation) */
70380 int rc = sqlite3PagerWrite(pPg: pDbPage);
70381 if( rc!=SQLITE_OK ){
70382 return rc;
70383 }
70384 memcpy(dest: pPayload, src: pBuf, n: nByte);
70385 }else{
70386 /* Copy data from page to buffer (a read operation) */
70387 memcpy(dest: pBuf, src: pPayload, n: nByte);
70388 }
70389 return SQLITE_OK;
70390}
70391
70392/*
70393** This function is used to read or overwrite payload information
70394** for the entry that the pCur cursor is pointing to. The eOp
70395** argument is interpreted as follows:
70396**
70397** 0: The operation is a read. Populate the overflow cache.
70398** 1: The operation is a write. Populate the overflow cache.
70399**
70400** A total of "amt" bytes are read or written beginning at "offset".
70401** Data is read to or from the buffer pBuf.
70402**
70403** The content being read or written might appear on the main page
70404** or be scattered out on multiple overflow pages.
70405**
70406** If the current cursor entry uses one or more overflow pages
70407** this function may allocate space for and lazily populate
70408** the overflow page-list cache array (BtCursor.aOverflow).
70409** Subsequent calls use this cache to make seeking to the supplied offset
70410** more efficient.
70411**
70412** Once an overflow page-list cache has been allocated, it must be
70413** invalidated if some other cursor writes to the same table, or if
70414** the cursor is moved to a different row. Additionally, in auto-vacuum
70415** mode, the following events may invalidate an overflow page-list cache.
70416**
70417** * An incremental vacuum,
70418** * A commit in auto_vacuum="full" mode,
70419** * Creating a table (may require moving an overflow page).
70420*/
70421static int accessPayload(
70422 BtCursor *pCur, /* Cursor pointing to entry to read from */
70423 u32 offset, /* Begin reading this far into payload */
70424 u32 amt, /* Read this many bytes */
70425 unsigned char *pBuf, /* Write the bytes into this buffer */
70426 int eOp /* zero to read. non-zero to write. */
70427){
70428 unsigned char *aPayload;
70429 int rc = SQLITE_OK;
70430 int iIdx = 0;
70431 MemPage *pPage = pCur->pPage; /* Btree page of current entry */
70432 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
70433#ifdef SQLITE_DIRECT_OVERFLOW_READ
70434 unsigned char * const pBufStart = pBuf; /* Start of original out buffer */
70435#endif
70436
70437 assert( pPage );
70438 assert( eOp==0 || eOp==1 );
70439 assert( pCur->eState==CURSOR_VALID );
70440 if( pCur->ix>=pPage->nCell ){
70441 return SQLITE_CORRUPT_PAGE(pPage);
70442 }
70443 assert( cursorHoldsMutex(pCur) );
70444
70445 getCellInfo(pCur);
70446 aPayload = pCur->info.pPayload;
70447 assert( offset+amt <= pCur->info.nPayload );
70448
70449 assert( aPayload > pPage->aData );
70450 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
70451 /* Trying to read or write past the end of the data is an error. The
70452 ** conditional above is really:
70453 ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
70454 ** but is recast into its current form to avoid integer overflow problems
70455 */
70456 return SQLITE_CORRUPT_PAGE(pPage);
70457 }
70458
70459 /* Check if data must be read/written to/from the btree page itself. */
70460 if( offset<pCur->info.nLocal ){
70461 int a = amt;
70462 if( a+offset>pCur->info.nLocal ){
70463 a = pCur->info.nLocal - offset;
70464 }
70465 rc = copyPayload(pPayload: &aPayload[offset], pBuf, nByte: a, eOp, pDbPage: pPage->pDbPage);
70466 offset = 0;
70467 pBuf += a;
70468 amt -= a;
70469 }else{
70470 offset -= pCur->info.nLocal;
70471 }
70472
70473
70474 if( rc==SQLITE_OK && amt>0 ){
70475 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
70476 Pgno nextPage;
70477
70478 nextPage = get4byte(p: &aPayload[pCur->info.nLocal]);
70479
70480 /* If the BtCursor.aOverflow[] has not been allocated, allocate it now.
70481 **
70482 ** The aOverflow[] array is sized at one entry for each overflow page
70483 ** in the overflow chain. The page number of the first overflow page is
70484 ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
70485 ** means "not yet known" (the cache is lazily populated).
70486 */
70487 if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
70488 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
70489 if( pCur->aOverflow==0
70490 || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(p: pCur->aOverflow)
70491 ){
70492 Pgno *aNew = (Pgno*)sqlite3Realloc(
70493 pOld: pCur->aOverflow, nBytes: nOvfl*2*sizeof(Pgno)
70494 );
70495 if( aNew==0 ){
70496 return SQLITE_NOMEM_BKPT;
70497 }else{
70498 pCur->aOverflow = aNew;
70499 }
70500 }
70501 memset(s: pCur->aOverflow, c: 0, n: nOvfl*sizeof(Pgno));
70502 pCur->curFlags |= BTCF_ValidOvfl;
70503 }else{
70504 /* If the overflow page-list cache has been allocated and the
70505 ** entry for the first required overflow page is valid, skip
70506 ** directly to it.
70507 */
70508 if( pCur->aOverflow[offset/ovflSize] ){
70509 iIdx = (offset/ovflSize);
70510 nextPage = pCur->aOverflow[iIdx];
70511 offset = (offset%ovflSize);
70512 }
70513 }
70514
70515 assert( rc==SQLITE_OK && amt>0 );
70516 while( nextPage ){
70517 /* If required, populate the overflow page-list cache. */
70518 if( nextPage > pBt->nPage ) return SQLITE_CORRUPT_BKPT;
70519 assert( pCur->aOverflow[iIdx]==0
70520 || pCur->aOverflow[iIdx]==nextPage
70521 || CORRUPT_DB );
70522 pCur->aOverflow[iIdx] = nextPage;
70523
70524 if( offset>=ovflSize ){
70525 /* The only reason to read this page is to obtain the page
70526 ** number for the next page in the overflow chain. The page
70527 ** data is not required. So first try to lookup the overflow
70528 ** page-list cache, if any, then fall back to the getOverflowPage()
70529 ** function.
70530 */
70531 assert( pCur->curFlags & BTCF_ValidOvfl );
70532 assert( pCur->pBtree->db==pBt->db );
70533 if( pCur->aOverflow[iIdx+1] ){
70534 nextPage = pCur->aOverflow[iIdx+1];
70535 }else{
70536 rc = getOverflowPage(pBt, ovfl: nextPage, ppPage: 0, pPgnoNext: &nextPage);
70537 }
70538 offset -= ovflSize;
70539 }else{
70540 /* Need to read this page properly. It contains some of the
70541 ** range of data that is being read (eOp==0) or written (eOp!=0).
70542 */
70543 int a = amt;
70544 if( a + offset > ovflSize ){
70545 a = ovflSize - offset;
70546 }
70547
70548#ifdef SQLITE_DIRECT_OVERFLOW_READ
70549 /* If all the following are true:
70550 **
70551 ** 1) this is a read operation, and
70552 ** 2) data is required from the start of this overflow page, and
70553 ** 3) there are no dirty pages in the page-cache
70554 ** 4) the database is file-backed, and
70555 ** 5) the page is not in the WAL file
70556 ** 6) at least 4 bytes have already been read into the output buffer
70557 **
70558 ** then data can be read directly from the database file into the
70559 ** output buffer, bypassing the page-cache altogether. This speeds
70560 ** up loading large records that span many overflow pages.
70561 */
70562 if( eOp==0 /* (1) */
70563 && offset==0 /* (2) */
70564 && sqlite3PagerDirectReadOk(pBt->pPager, nextPage) /* (3,4,5) */
70565 && &pBuf[-4]>=pBufStart /* (6) */
70566 ){
70567 sqlite3_file *fd = sqlite3PagerFile(pBt->pPager);
70568 u8 aSave[4];
70569 u8 *aWrite = &pBuf[-4];
70570 assert( aWrite>=pBufStart ); /* due to (6) */
70571 memcpy(aSave, aWrite, 4);
70572 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
70573 if( rc && nextPage>pBt->nPage ) rc = SQLITE_CORRUPT_BKPT;
70574 nextPage = get4byte(aWrite);
70575 memcpy(aWrite, aSave, 4);
70576 }else
70577#endif
70578
70579 {
70580 DbPage *pDbPage;
70581 rc = sqlite3PagerGet(pPager: pBt->pPager, pgno: nextPage, ppPage: &pDbPage,
70582 flags: (eOp==0 ? PAGER_GET_READONLY : 0)
70583 );
70584 if( rc==SQLITE_OK ){
70585 aPayload = sqlite3PagerGetData(pPg: pDbPage);
70586 nextPage = get4byte(p: aPayload);
70587 rc = copyPayload(pPayload: &aPayload[offset+4], pBuf, nByte: a, eOp, pDbPage);
70588 sqlite3PagerUnref(pPg: pDbPage);
70589 offset = 0;
70590 }
70591 }
70592 amt -= a;
70593 if( amt==0 ) return rc;
70594 pBuf += a;
70595 }
70596 if( rc ) break;
70597 iIdx++;
70598 }
70599 }
70600
70601 if( rc==SQLITE_OK && amt>0 ){
70602 /* Overflow chain ends prematurely */
70603 return SQLITE_CORRUPT_PAGE(pPage);
70604 }
70605 return rc;
70606}
70607
70608/*
70609** Read part of the payload for the row at which that cursor pCur is currently
70610** pointing. "amt" bytes will be transferred into pBuf[]. The transfer
70611** begins at "offset".
70612**
70613** pCur can be pointing to either a table or an index b-tree.
70614** If pointing to a table btree, then the content section is read. If
70615** pCur is pointing to an index b-tree then the key section is read.
70616**
70617** For sqlite3BtreePayload(), the caller must ensure that pCur is pointing
70618** to a valid row in the table. For sqlite3BtreePayloadChecked(), the
70619** cursor might be invalid or might need to be restored before being read.
70620**
70621** Return SQLITE_OK on success or an error code if anything goes
70622** wrong. An error is returned if "offset+amt" is larger than
70623** the available payload.
70624*/
70625SQLITE_PRIVATE int sqlite3BtreePayload(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
70626 assert( cursorHoldsMutex(pCur) );
70627 assert( pCur->eState==CURSOR_VALID );
70628 assert( pCur->iPage>=0 && pCur->pPage );
70629 return accessPayload(pCur, offset, amt, pBuf: (unsigned char*)pBuf, eOp: 0);
70630}
70631
70632/*
70633** This variant of sqlite3BtreePayload() works even if the cursor has not
70634** in the CURSOR_VALID state. It is only used by the sqlite3_blob_read()
70635** interface.
70636*/
70637#ifndef SQLITE_OMIT_INCRBLOB
70638static SQLITE_NOINLINE int accessPayloadChecked(
70639 BtCursor *pCur,
70640 u32 offset,
70641 u32 amt,
70642 void *pBuf
70643){
70644 int rc;
70645 if ( pCur->eState==CURSOR_INVALID ){
70646 return SQLITE_ABORT;
70647 }
70648 assert( cursorOwnsBtShared(pCur) );
70649 rc = btreeRestoreCursorPosition(pCur);
70650 return rc ? rc : accessPayload(pCur, offset, amt, pBuf, eOp: 0);
70651}
70652SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
70653 if( pCur->eState==CURSOR_VALID ){
70654 assert( cursorOwnsBtShared(pCur) );
70655 return accessPayload(pCur, offset, amt, pBuf, eOp: 0);
70656 }else{
70657 return accessPayloadChecked(pCur, offset, amt, pBuf);
70658 }
70659}
70660#endif /* SQLITE_OMIT_INCRBLOB */
70661
70662/*
70663** Return a pointer to payload information from the entry that the
70664** pCur cursor is pointing to. The pointer is to the beginning of
70665** the key if index btrees (pPage->intKey==0) and is the data for
70666** table btrees (pPage->intKey==1). The number of bytes of available
70667** key/data is written into *pAmt. If *pAmt==0, then the value
70668** returned will not be a valid pointer.
70669**
70670** This routine is an optimization. It is common for the entire key
70671** and data to fit on the local page and for there to be no overflow
70672** pages. When that is so, this routine can be used to access the
70673** key and data without making a copy. If the key and/or data spills
70674** onto overflow pages, then accessPayload() must be used to reassemble
70675** the key/data and copy it into a preallocated buffer.
70676**
70677** The pointer returned by this routine looks directly into the cached
70678** page of the database. The data might change or move the next time
70679** any btree routine is called.
70680*/
70681static const void *fetchPayload(
70682 BtCursor *pCur, /* Cursor pointing to entry to read from */
70683 u32 *pAmt /* Write the number of available bytes here */
70684){
70685 int amt;
70686 assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage);
70687 assert( pCur->eState==CURSOR_VALID );
70688 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
70689 assert( cursorOwnsBtShared(pCur) );
70690 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB );
70691 assert( pCur->info.nSize>0 );
70692 assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB );
70693 assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB);
70694 amt = pCur->info.nLocal;
70695 if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){
70696 /* There is too little space on the page for the expected amount
70697 ** of local content. Database must be corrupt. */
70698 assert( CORRUPT_DB );
70699 amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload));
70700 }
70701 *pAmt = (u32)amt;
70702 return (void*)pCur->info.pPayload;
70703}
70704
70705
70706/*
70707** For the entry that cursor pCur is point to, return as
70708** many bytes of the key or data as are available on the local
70709** b-tree page. Write the number of available bytes into *pAmt.
70710**
70711** The pointer returned is ephemeral. The key/data may move
70712** or be destroyed on the next call to any Btree routine,
70713** including calls from other threads against the same cache.
70714** Hence, a mutex on the BtShared should be held prior to calling
70715** this routine.
70716**
70717** These routines is used to get quick access to key and data
70718** in the common case where no overflow pages are used.
70719*/
70720SQLITE_PRIVATE const void *sqlite3BtreePayloadFetch(BtCursor *pCur, u32 *pAmt){
70721 return fetchPayload(pCur, pAmt);
70722}
70723
70724
70725/*
70726** Move the cursor down to a new child page. The newPgno argument is the
70727** page number of the child page to move to.
70728**
70729** This function returns SQLITE_CORRUPT if the page-header flags field of
70730** the new child page does not match the flags field of the parent (i.e.
70731** if an intkey page appears to be the parent of a non-intkey page, or
70732** vice-versa).
70733*/
70734static int moveToChild(BtCursor *pCur, u32 newPgno){
70735 BtShared *pBt = pCur->pBt;
70736
70737 assert( cursorOwnsBtShared(pCur) );
70738 assert( pCur->eState==CURSOR_VALID );
70739 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
70740 assert( pCur->iPage>=0 );
70741 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
70742 return SQLITE_CORRUPT_BKPT;
70743 }
70744 pCur->info.nSize = 0;
70745 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
70746 pCur->aiIdx[pCur->iPage] = pCur->ix;
70747 pCur->apPage[pCur->iPage] = pCur->pPage;
70748 pCur->ix = 0;
70749 pCur->iPage++;
70750 return getAndInitPage(pBt, pgno: newPgno, ppPage: &pCur->pPage, pCur, bReadOnly: pCur->curPagerFlags);
70751}
70752
70753#ifdef SQLITE_DEBUG
70754/*
70755** Page pParent is an internal (non-leaf) tree page. This function
70756** asserts that page number iChild is the left-child if the iIdx'th
70757** cell in page pParent. Or, if iIdx is equal to the total number of
70758** cells in pParent, that page number iChild is the right-child of
70759** the page.
70760*/
70761static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){
70762 if( CORRUPT_DB ) return; /* The conditions tested below might not be true
70763 ** in a corrupt database */
70764 assert( iIdx<=pParent->nCell );
70765 if( iIdx==pParent->nCell ){
70766 assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
70767 }else{
70768 assert( get4byte(findCell(pParent, iIdx))==iChild );
70769 }
70770}
70771#else
70772# define assertParentIndex(x,y,z)
70773#endif
70774
70775/*
70776** Move the cursor up to the parent page.
70777**
70778** pCur->idx is set to the cell index that contains the pointer
70779** to the page we are coming from. If we are coming from the
70780** right-most child page then pCur->idx is set to one more than
70781** the largest cell index.
70782*/
70783static void moveToParent(BtCursor *pCur){
70784 MemPage *pLeaf;
70785 assert( cursorOwnsBtShared(pCur) );
70786 assert( pCur->eState==CURSOR_VALID );
70787 assert( pCur->iPage>0 );
70788 assert( pCur->pPage );
70789 assertParentIndex(
70790 pCur->apPage[pCur->iPage-1],
70791 pCur->aiIdx[pCur->iPage-1],
70792 pCur->pPage->pgno
70793 );
70794 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
70795 pCur->info.nSize = 0;
70796 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
70797 pCur->ix = pCur->aiIdx[pCur->iPage-1];
70798 pLeaf = pCur->pPage;
70799 pCur->pPage = pCur->apPage[--pCur->iPage];
70800 releasePageNotNull(pPage: pLeaf);
70801}
70802
70803/*
70804** Move the cursor to point to the root page of its b-tree structure.
70805**
70806** If the table has a virtual root page, then the cursor is moved to point
70807** to the virtual root page instead of the actual root page. A table has a
70808** virtual root page when the actual root page contains no cells and a
70809** single child page. This can only happen with the table rooted at page 1.
70810**
70811** If the b-tree structure is empty, the cursor state is set to
70812** CURSOR_INVALID and this routine returns SQLITE_EMPTY. Otherwise,
70813** the cursor is set to point to the first cell located on the root
70814** (or virtual root) page and the cursor state is set to CURSOR_VALID.
70815**
70816** If this function returns successfully, it may be assumed that the
70817** page-header flags indicate that the [virtual] root-page is the expected
70818** kind of b-tree page (i.e. if when opening the cursor the caller did not
70819** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D,
70820** indicating a table b-tree, or if the caller did specify a KeyInfo
70821** structure the flags byte is set to 0x02 or 0x0A, indicating an index
70822** b-tree).
70823*/
70824static int moveToRoot(BtCursor *pCur){
70825 MemPage *pRoot;
70826 int rc = SQLITE_OK;
70827
70828 assert( cursorOwnsBtShared(pCur) );
70829 assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
70830 assert( CURSOR_VALID < CURSOR_REQUIRESEEK );
70831 assert( CURSOR_FAULT > CURSOR_REQUIRESEEK );
70832 assert( pCur->eState < CURSOR_REQUIRESEEK || pCur->iPage<0 );
70833 assert( pCur->pgnoRoot>0 || pCur->iPage<0 );
70834
70835 if( pCur->iPage>=0 ){
70836 if( pCur->iPage ){
70837 releasePageNotNull(pPage: pCur->pPage);
70838 while( --pCur->iPage ){
70839 releasePageNotNull(pPage: pCur->apPage[pCur->iPage]);
70840 }
70841 pCur->pPage = pCur->apPage[0];
70842 goto skip_init;
70843 }
70844 }else if( pCur->pgnoRoot==0 ){
70845 pCur->eState = CURSOR_INVALID;
70846 return SQLITE_EMPTY;
70847 }else{
70848 assert( pCur->iPage==(-1) );
70849 if( pCur->eState>=CURSOR_REQUIRESEEK ){
70850 if( pCur->eState==CURSOR_FAULT ){
70851 assert( pCur->skipNext!=SQLITE_OK );
70852 return pCur->skipNext;
70853 }
70854 sqlite3BtreeClearCursor(pCur);
70855 }
70856 rc = getAndInitPage(pBt: pCur->pBtree->pBt, pgno: pCur->pgnoRoot, ppPage: &pCur->pPage,
70857 pCur: 0, bReadOnly: pCur->curPagerFlags);
70858 if( rc!=SQLITE_OK ){
70859 pCur->eState = CURSOR_INVALID;
70860 return rc;
70861 }
70862 pCur->iPage = 0;
70863 pCur->curIntKey = pCur->pPage->intKey;
70864 }
70865 pRoot = pCur->pPage;
70866 assert( pRoot->pgno==pCur->pgnoRoot );
70867
70868 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
70869 ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
70870 ** NULL, the caller expects a table b-tree. If this is not the case,
70871 ** return an SQLITE_CORRUPT error.
70872 **
70873 ** Earlier versions of SQLite assumed that this test could not fail
70874 ** if the root page was already loaded when this function was called (i.e.
70875 ** if pCur->iPage>=0). But this is not so if the database is corrupted
70876 ** in such a way that page pRoot is linked into a second b-tree table
70877 ** (or the freelist). */
70878 assert( pRoot->intKey==1 || pRoot->intKey==0 );
70879 if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
70880 return SQLITE_CORRUPT_PAGE(pCur->pPage);
70881 }
70882
70883skip_init:
70884 pCur->ix = 0;
70885 pCur->info.nSize = 0;
70886 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
70887
70888 pRoot = pCur->pPage;
70889 if( pRoot->nCell>0 ){
70890 pCur->eState = CURSOR_VALID;
70891 }else if( !pRoot->leaf ){
70892 Pgno subpage;
70893 if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
70894 subpage = get4byte(p: &pRoot->aData[pRoot->hdrOffset+8]);
70895 pCur->eState = CURSOR_VALID;
70896 rc = moveToChild(pCur, newPgno: subpage);
70897 }else{
70898 pCur->eState = CURSOR_INVALID;
70899 rc = SQLITE_EMPTY;
70900 }
70901 return rc;
70902}
70903
70904/*
70905** Move the cursor down to the left-most leaf entry beneath the
70906** entry to which it is currently pointing.
70907**
70908** The left-most leaf is the one with the smallest key - the first
70909** in ascending order.
70910*/
70911static int moveToLeftmost(BtCursor *pCur){
70912 Pgno pgno;
70913 int rc = SQLITE_OK;
70914 MemPage *pPage;
70915
70916 assert( cursorOwnsBtShared(pCur) );
70917 assert( pCur->eState==CURSOR_VALID );
70918 while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
70919 assert( pCur->ix<pPage->nCell );
70920 pgno = get4byte(findCell(pPage, pCur->ix));
70921 rc = moveToChild(pCur, newPgno: pgno);
70922 }
70923 return rc;
70924}
70925
70926/*
70927** Move the cursor down to the right-most leaf entry beneath the
70928** page to which it is currently pointing. Notice the difference
70929** between moveToLeftmost() and moveToRightmost(). moveToLeftmost()
70930** finds the left-most entry beneath the *entry* whereas moveToRightmost()
70931** finds the right-most entry beneath the *page*.
70932**
70933** The right-most entry is the one with the largest key - the last
70934** key in ascending order.
70935*/
70936static int moveToRightmost(BtCursor *pCur){
70937 Pgno pgno;
70938 int rc = SQLITE_OK;
70939 MemPage *pPage = 0;
70940
70941 assert( cursorOwnsBtShared(pCur) );
70942 assert( pCur->eState==CURSOR_VALID );
70943 while( !(pPage = pCur->pPage)->leaf ){
70944 pgno = get4byte(p: &pPage->aData[pPage->hdrOffset+8]);
70945 pCur->ix = pPage->nCell;
70946 rc = moveToChild(pCur, newPgno: pgno);
70947 if( rc ) return rc;
70948 }
70949 pCur->ix = pPage->nCell-1;
70950 assert( pCur->info.nSize==0 );
70951 assert( (pCur->curFlags & BTCF_ValidNKey)==0 );
70952 return SQLITE_OK;
70953}
70954
70955/* Move the cursor to the first entry in the table. Return SQLITE_OK
70956** on success. Set *pRes to 0 if the cursor actually points to something
70957** or set *pRes to 1 if the table is empty.
70958*/
70959SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
70960 int rc;
70961
70962 assert( cursorOwnsBtShared(pCur) );
70963 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
70964 rc = moveToRoot(pCur);
70965 if( rc==SQLITE_OK ){
70966 assert( pCur->pPage->nCell>0 );
70967 *pRes = 0;
70968 rc = moveToLeftmost(pCur);
70969 }else if( rc==SQLITE_EMPTY ){
70970 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
70971 *pRes = 1;
70972 rc = SQLITE_OK;
70973 }
70974 return rc;
70975}
70976
70977/* Move the cursor to the last entry in the table. Return SQLITE_OK
70978** on success. Set *pRes to 0 if the cursor actually points to something
70979** or set *pRes to 1 if the table is empty.
70980*/
70981SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
70982 int rc;
70983
70984 assert( cursorOwnsBtShared(pCur) );
70985 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
70986
70987 /* If the cursor already points to the last entry, this is a no-op. */
70988 if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
70989#ifdef SQLITE_DEBUG
70990 /* This block serves to assert() that the cursor really does point
70991 ** to the last entry in the b-tree. */
70992 int ii;
70993 for(ii=0; ii<pCur->iPage; ii++){
70994 assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
70995 }
70996 assert( pCur->ix==pCur->pPage->nCell-1 || CORRUPT_DB );
70997 testcase( pCur->ix!=pCur->pPage->nCell-1 );
70998 /* ^-- dbsqlfuzz b92b72e4de80b5140c30ab71372ca719b8feb618 */
70999 assert( pCur->pPage->leaf );
71000#endif
71001 *pRes = 0;
71002 return SQLITE_OK;
71003 }
71004
71005 rc = moveToRoot(pCur);
71006 if( rc==SQLITE_OK ){
71007 assert( pCur->eState==CURSOR_VALID );
71008 *pRes = 0;
71009 rc = moveToRightmost(pCur);
71010 if( rc==SQLITE_OK ){
71011 pCur->curFlags |= BTCF_AtLast;
71012 }else{
71013 pCur->curFlags &= ~BTCF_AtLast;
71014 }
71015 }else if( rc==SQLITE_EMPTY ){
71016 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
71017 *pRes = 1;
71018 rc = SQLITE_OK;
71019 }
71020 return rc;
71021}
71022
71023/* Move the cursor so that it points to an entry in a table (a.k.a INTKEY)
71024** table near the key intKey. Return a success code.
71025**
71026** If an exact match is not found, then the cursor is always
71027** left pointing at a leaf page which would hold the entry if it
71028** were present. The cursor might point to an entry that comes
71029** before or after the key.
71030**
71031** An integer is written into *pRes which is the result of
71032** comparing the key with the entry to which the cursor is
71033** pointing. The meaning of the integer written into
71034** *pRes is as follows:
71035**
71036** *pRes<0 The cursor is left pointing at an entry that
71037** is smaller than intKey or if the table is empty
71038** and the cursor is therefore left point to nothing.
71039**
71040** *pRes==0 The cursor is left pointing at an entry that
71041** exactly matches intKey.
71042**
71043** *pRes>0 The cursor is left pointing at an entry that
71044** is larger than intKey.
71045*/
71046SQLITE_PRIVATE int sqlite3BtreeTableMoveto(
71047 BtCursor *pCur, /* The cursor to be moved */
71048 i64 intKey, /* The table key */
71049 int biasRight, /* If true, bias the search to the high end */
71050 int *pRes /* Write search results here */
71051){
71052 int rc;
71053
71054 assert( cursorOwnsBtShared(pCur) );
71055 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
71056 assert( pRes );
71057 assert( pCur->pKeyInfo==0 );
71058 assert( pCur->eState!=CURSOR_VALID || pCur->curIntKey!=0 );
71059
71060 /* If the cursor is already positioned at the point we are trying
71061 ** to move to, then just return without doing any work */
71062 if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0 ){
71063 if( pCur->info.nKey==intKey ){
71064 *pRes = 0;
71065 return SQLITE_OK;
71066 }
71067 if( pCur->info.nKey<intKey ){
71068 if( (pCur->curFlags & BTCF_AtLast)!=0 ){
71069 *pRes = -1;
71070 return SQLITE_OK;
71071 }
71072 /* If the requested key is one more than the previous key, then
71073 ** try to get there using sqlite3BtreeNext() rather than a full
71074 ** binary search. This is an optimization only. The correct answer
71075 ** is still obtained without this case, only a little more slowely */
71076 if( pCur->info.nKey+1==intKey ){
71077 *pRes = 0;
71078 rc = sqlite3BtreeNext(pCur, flags: 0);
71079 if( rc==SQLITE_OK ){
71080 getCellInfo(pCur);
71081 if( pCur->info.nKey==intKey ){
71082 return SQLITE_OK;
71083 }
71084 }else if( rc!=SQLITE_DONE ){
71085 return rc;
71086 }
71087 }
71088 }
71089 }
71090
71091#ifdef SQLITE_DEBUG
71092 pCur->pBtree->nSeek++; /* Performance measurement during testing */
71093#endif
71094
71095 rc = moveToRoot(pCur);
71096 if( rc ){
71097 if( rc==SQLITE_EMPTY ){
71098 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
71099 *pRes = -1;
71100 return SQLITE_OK;
71101 }
71102 return rc;
71103 }
71104 assert( pCur->pPage );
71105 assert( pCur->pPage->isInit );
71106 assert( pCur->eState==CURSOR_VALID );
71107 assert( pCur->pPage->nCell > 0 );
71108 assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey );
71109 assert( pCur->curIntKey );
71110
71111 for(;;){
71112 int lwr, upr, idx, c;
71113 Pgno chldPg;
71114 MemPage *pPage = pCur->pPage;
71115 u8 *pCell; /* Pointer to current cell in pPage */
71116
71117 /* pPage->nCell must be greater than zero. If this is the root-page
71118 ** the cursor would have been INVALID above and this for(;;) loop
71119 ** not run. If this is not the root-page, then the moveToChild() routine
71120 ** would have already detected db corruption. Similarly, pPage must
71121 ** be the right kind (index or table) of b-tree page. Otherwise
71122 ** a moveToChild() or moveToRoot() call would have detected corruption. */
71123 assert( pPage->nCell>0 );
71124 assert( pPage->intKey );
71125 lwr = 0;
71126 upr = pPage->nCell-1;
71127 assert( biasRight==0 || biasRight==1 );
71128 idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
71129 pCur->ix = (u16)idx;
71130 for(;;){
71131 i64 nCellKey;
71132 pCell = findCellPastPtr(pPage, idx);
71133 if( pPage->intKeyLeaf ){
71134 while( 0x80 <= *(pCell++) ){
71135 if( pCell>=pPage->aDataEnd ){
71136 return SQLITE_CORRUPT_PAGE(pPage);
71137 }
71138 }
71139 }
71140 getVarint(p: pCell, v: (u64*)&nCellKey);
71141 if( nCellKey<intKey ){
71142 lwr = idx+1;
71143 if( lwr>upr ){ c = -1; break; }
71144 }else if( nCellKey>intKey ){
71145 upr = idx-1;
71146 if( lwr>upr ){ c = +1; break; }
71147 }else{
71148 assert( nCellKey==intKey );
71149 pCur->ix = (u16)idx;
71150 if( !pPage->leaf ){
71151 lwr = idx;
71152 goto moveto_table_next_layer;
71153 }else{
71154 pCur->curFlags |= BTCF_ValidNKey;
71155 pCur->info.nKey = nCellKey;
71156 pCur->info.nSize = 0;
71157 *pRes = 0;
71158 return SQLITE_OK;
71159 }
71160 }
71161 assert( lwr+upr>=0 );
71162 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */
71163 }
71164 assert( lwr==upr+1 || !pPage->leaf );
71165 assert( pPage->isInit );
71166 if( pPage->leaf ){
71167 assert( pCur->ix<pCur->pPage->nCell );
71168 pCur->ix = (u16)idx;
71169 *pRes = c;
71170 rc = SQLITE_OK;
71171 goto moveto_table_finish;
71172 }
71173moveto_table_next_layer:
71174 if( lwr>=pPage->nCell ){
71175 chldPg = get4byte(p: &pPage->aData[pPage->hdrOffset+8]);
71176 }else{
71177 chldPg = get4byte(findCell(pPage, lwr));
71178 }
71179 pCur->ix = (u16)lwr;
71180 rc = moveToChild(pCur, newPgno: chldPg);
71181 if( rc ) break;
71182 }
71183moveto_table_finish:
71184 pCur->info.nSize = 0;
71185 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
71186 return rc;
71187}
71188
71189/* Move the cursor so that it points to an entry in an index table
71190** near the key pIdxKey. Return a success code.
71191**
71192** If an exact match is not found, then the cursor is always
71193** left pointing at a leaf page which would hold the entry if it
71194** were present. The cursor might point to an entry that comes
71195** before or after the key.
71196**
71197** An integer is written into *pRes which is the result of
71198** comparing the key with the entry to which the cursor is
71199** pointing. The meaning of the integer written into
71200** *pRes is as follows:
71201**
71202** *pRes<0 The cursor is left pointing at an entry that
71203** is smaller than pIdxKey or if the table is empty
71204** and the cursor is therefore left point to nothing.
71205**
71206** *pRes==0 The cursor is left pointing at an entry that
71207** exactly matches pIdxKey.
71208**
71209** *pRes>0 The cursor is left pointing at an entry that
71210** is larger than pIdxKey.
71211**
71212** The pIdxKey->eqSeen field is set to 1 if there
71213** exists an entry in the table that exactly matches pIdxKey.
71214*/
71215SQLITE_PRIVATE int sqlite3BtreeIndexMoveto(
71216 BtCursor *pCur, /* The cursor to be moved */
71217 UnpackedRecord *pIdxKey, /* Unpacked index key */
71218 int *pRes /* Write search results here */
71219){
71220 int rc;
71221 RecordCompare xRecordCompare;
71222
71223 assert( cursorOwnsBtShared(pCur) );
71224 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
71225 assert( pRes );
71226 assert( pCur->pKeyInfo!=0 );
71227
71228#ifdef SQLITE_DEBUG
71229 pCur->pBtree->nSeek++; /* Performance measurement during testing */
71230#endif
71231
71232 xRecordCompare = sqlite3VdbeFindCompare(pIdxKey);
71233 pIdxKey->errCode = 0;
71234 assert( pIdxKey->default_rc==1
71235 || pIdxKey->default_rc==0
71236 || pIdxKey->default_rc==-1
71237 );
71238
71239 rc = moveToRoot(pCur);
71240 if( rc ){
71241 if( rc==SQLITE_EMPTY ){
71242 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
71243 *pRes = -1;
71244 return SQLITE_OK;
71245 }
71246 return rc;
71247 }
71248 assert( pCur->pPage );
71249 assert( pCur->pPage->isInit );
71250 assert( pCur->eState==CURSOR_VALID );
71251 assert( pCur->pPage->nCell > 0 );
71252 assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey );
71253 assert( pCur->curIntKey || pIdxKey );
71254 for(;;){
71255 int lwr, upr, idx, c;
71256 Pgno chldPg;
71257 MemPage *pPage = pCur->pPage;
71258 u8 *pCell; /* Pointer to current cell in pPage */
71259
71260 /* pPage->nCell must be greater than zero. If this is the root-page
71261 ** the cursor would have been INVALID above and this for(;;) loop
71262 ** not run. If this is not the root-page, then the moveToChild() routine
71263 ** would have already detected db corruption. Similarly, pPage must
71264 ** be the right kind (index or table) of b-tree page. Otherwise
71265 ** a moveToChild() or moveToRoot() call would have detected corruption. */
71266 assert( pPage->nCell>0 );
71267 assert( pPage->intKey==(pIdxKey==0) );
71268 lwr = 0;
71269 upr = pPage->nCell-1;
71270 idx = upr>>1; /* idx = (lwr+upr)/2; */
71271 pCur->ix = (u16)idx;
71272 for(;;){
71273 int nCell; /* Size of the pCell cell in bytes */
71274 pCell = findCellPastPtr(pPage, idx);
71275
71276 /* The maximum supported page-size is 65536 bytes. This means that
71277 ** the maximum number of record bytes stored on an index B-Tree
71278 ** page is less than 16384 bytes and may be stored as a 2-byte
71279 ** varint. This information is used to attempt to avoid parsing
71280 ** the entire cell by checking for the cases where the record is
71281 ** stored entirely within the b-tree page by inspecting the first
71282 ** 2 bytes of the cell.
71283 */
71284 nCell = pCell[0];
71285 if( nCell<=pPage->max1bytePayload ){
71286 /* This branch runs if the record-size field of the cell is a
71287 ** single byte varint and the record fits entirely on the main
71288 ** b-tree page. */
71289 testcase( pCell+nCell+1==pPage->aDataEnd );
71290 c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
71291 }else if( !(pCell[1] & 0x80)
71292 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
71293 ){
71294 /* The record-size field is a 2 byte varint and the record
71295 ** fits entirely on the main b-tree page. */
71296 testcase( pCell+nCell+2==pPage->aDataEnd );
71297 c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
71298 }else{
71299 /* The record flows over onto one or more overflow pages. In
71300 ** this case the whole cell needs to be parsed, a buffer allocated
71301 ** and accessPayload() used to retrieve the record into the
71302 ** buffer before VdbeRecordCompare() can be called.
71303 **
71304 ** If the record is corrupt, the xRecordCompare routine may read
71305 ** up to two varints past the end of the buffer. An extra 18
71306 ** bytes of padding is allocated at the end of the buffer in
71307 ** case this happens. */
71308 void *pCellKey;
71309 u8 * const pCellBody = pCell - pPage->childPtrSize;
71310 const int nOverrun = 18; /* Size of the overrun padding */
71311 pPage->xParseCell(pPage, pCellBody, &pCur->info);
71312 nCell = (int)pCur->info.nKey;
71313 testcase( nCell<0 ); /* True if key size is 2^32 or more */
71314 testcase( nCell==0 ); /* Invalid key size: 0x80 0x80 0x00 */
71315 testcase( nCell==1 ); /* Invalid key size: 0x80 0x80 0x01 */
71316 testcase( nCell==2 ); /* Minimum legal index key size */
71317 if( nCell<2 || nCell/pCur->pBt->usableSize>pCur->pBt->nPage ){
71318 rc = SQLITE_CORRUPT_PAGE(pPage);
71319 goto moveto_index_finish;
71320 }
71321 pCellKey = sqlite3Malloc( n: nCell+nOverrun );
71322 if( pCellKey==0 ){
71323 rc = SQLITE_NOMEM_BKPT;
71324 goto moveto_index_finish;
71325 }
71326 pCur->ix = (u16)idx;
71327 rc = accessPayload(pCur, offset: 0, amt: nCell, pBuf: (unsigned char*)pCellKey, eOp: 0);
71328 memset(s: ((u8*)pCellKey)+nCell,c: 0,n: nOverrun); /* Fix uninit warnings */
71329 pCur->curFlags &= ~BTCF_ValidOvfl;
71330 if( rc ){
71331 sqlite3_free(p: pCellKey);
71332 goto moveto_index_finish;
71333 }
71334 c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
71335 sqlite3_free(p: pCellKey);
71336 }
71337 assert(
71338 (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)
71339 && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
71340 );
71341 if( c<0 ){
71342 lwr = idx+1;
71343 }else if( c>0 ){
71344 upr = idx-1;
71345 }else{
71346 assert( c==0 );
71347 *pRes = 0;
71348 rc = SQLITE_OK;
71349 pCur->ix = (u16)idx;
71350 if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPT;
71351 goto moveto_index_finish;
71352 }
71353 if( lwr>upr ) break;
71354 assert( lwr+upr>=0 );
71355 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2 */
71356 }
71357 assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
71358 assert( pPage->isInit );
71359 if( pPage->leaf ){
71360 assert( pCur->ix<pCur->pPage->nCell );
71361 pCur->ix = (u16)idx;
71362 *pRes = c;
71363 rc = SQLITE_OK;
71364 goto moveto_index_finish;
71365 }
71366 if( lwr>=pPage->nCell ){
71367 chldPg = get4byte(p: &pPage->aData[pPage->hdrOffset+8]);
71368 }else{
71369 chldPg = get4byte(findCell(pPage, lwr));
71370 }
71371 pCur->ix = (u16)lwr;
71372 rc = moveToChild(pCur, newPgno: chldPg);
71373 if( rc ) break;
71374 }
71375moveto_index_finish:
71376 pCur->info.nSize = 0;
71377 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
71378 return rc;
71379}
71380
71381
71382/*
71383** Return TRUE if the cursor is not pointing at an entry of the table.
71384**
71385** TRUE will be returned after a call to sqlite3BtreeNext() moves
71386** past the last entry in the table or sqlite3BtreePrev() moves past
71387** the first entry. TRUE is also returned if the table is empty.
71388*/
71389SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor *pCur){
71390 /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
71391 ** have been deleted? This API will need to change to return an error code
71392 ** as well as the boolean result value.
71393 */
71394 return (CURSOR_VALID!=pCur->eState);
71395}
71396
71397/*
71398** Return an estimate for the number of rows in the table that pCur is
71399** pointing to. Return a negative number if no estimate is currently
71400** available.
71401*/
71402SQLITE_PRIVATE i64 sqlite3BtreeRowCountEst(BtCursor *pCur){
71403 i64 n;
71404 u8 i;
71405
71406 assert( cursorOwnsBtShared(pCur) );
71407 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
71408
71409 /* Currently this interface is only called by the OP_IfSmaller
71410 ** opcode, and it that case the cursor will always be valid and
71411 ** will always point to a leaf node. */
71412 if( NEVER(pCur->eState!=CURSOR_VALID) ) return -1;
71413 if( NEVER(pCur->pPage->leaf==0) ) return -1;
71414
71415 n = pCur->pPage->nCell;
71416 for(i=0; i<pCur->iPage; i++){
71417 n *= pCur->apPage[i]->nCell;
71418 }
71419 return n;
71420}
71421
71422/*
71423** Advance the cursor to the next entry in the database.
71424** Return value:
71425**
71426** SQLITE_OK success
71427** SQLITE_DONE cursor is already pointing at the last element
71428** otherwise some kind of error occurred
71429**
71430** The main entry point is sqlite3BtreeNext(). That routine is optimized
71431** for the common case of merely incrementing the cell counter BtCursor.aiIdx
71432** to the next cell on the current page. The (slower) btreeNext() helper
71433** routine is called when it is necessary to move to a different page or
71434** to restore the cursor.
71435**
71436** If bit 0x01 of the F argument in sqlite3BtreeNext(C,F) is 1, then the
71437** cursor corresponds to an SQL index and this routine could have been
71438** skipped if the SQL index had been a unique index. The F argument
71439** is a hint to the implement. SQLite btree implementation does not use
71440** this hint, but COMDB2 does.
71441*/
71442static SQLITE_NOINLINE int btreeNext(BtCursor *pCur){
71443 int rc;
71444 int idx;
71445 MemPage *pPage;
71446
71447 assert( cursorOwnsBtShared(pCur) );
71448 if( pCur->eState!=CURSOR_VALID ){
71449 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
71450 rc = restoreCursorPosition(pCur);
71451 if( rc!=SQLITE_OK ){
71452 return rc;
71453 }
71454 if( CURSOR_INVALID==pCur->eState ){
71455 return SQLITE_DONE;
71456 }
71457 if( pCur->eState==CURSOR_SKIPNEXT ){
71458 pCur->eState = CURSOR_VALID;
71459 if( pCur->skipNext>0 ) return SQLITE_OK;
71460 }
71461 }
71462
71463 pPage = pCur->pPage;
71464 idx = ++pCur->ix;
71465 if( !pPage->isInit || sqlite3FaultSim(iTest: 412) ){
71466 /* The only known way for this to happen is for there to be a
71467 ** recursive SQL function that does a DELETE operation as part of a
71468 ** SELECT which deletes content out from under an active cursor
71469 ** in a corrupt database file where the table being DELETE-ed from
71470 ** has pages in common with the table being queried. See TH3
71471 ** module cov1/btree78.test testcase 220 (2018-06-08) for an
71472 ** example. */
71473 return SQLITE_CORRUPT_BKPT;
71474 }
71475
71476 if( idx>=pPage->nCell ){
71477 if( !pPage->leaf ){
71478 rc = moveToChild(pCur, get4byte(p: &pPage->aData[pPage->hdrOffset+8]));
71479 if( rc ) return rc;
71480 return moveToLeftmost(pCur);
71481 }
71482 do{
71483 if( pCur->iPage==0 ){
71484 pCur->eState = CURSOR_INVALID;
71485 return SQLITE_DONE;
71486 }
71487 moveToParent(pCur);
71488 pPage = pCur->pPage;
71489 }while( pCur->ix>=pPage->nCell );
71490 if( pPage->intKey ){
71491 return sqlite3BtreeNext(pCur, flags: 0);
71492 }else{
71493 return SQLITE_OK;
71494 }
71495 }
71496 if( pPage->leaf ){
71497 return SQLITE_OK;
71498 }else{
71499 return moveToLeftmost(pCur);
71500 }
71501}
71502SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int flags){
71503 MemPage *pPage;
71504 UNUSED_PARAMETER( flags ); /* Used in COMDB2 but not native SQLite */
71505 assert( cursorOwnsBtShared(pCur) );
71506 assert( flags==0 || flags==1 );
71507 pCur->info.nSize = 0;
71508 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
71509 if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur);
71510 pPage = pCur->pPage;
71511 if( (++pCur->ix)>=pPage->nCell ){
71512 pCur->ix--;
71513 return btreeNext(pCur);
71514 }
71515 if( pPage->leaf ){
71516 return SQLITE_OK;
71517 }else{
71518 return moveToLeftmost(pCur);
71519 }
71520}
71521
71522/*
71523** Step the cursor to the back to the previous entry in the database.
71524** Return values:
71525**
71526** SQLITE_OK success
71527** SQLITE_DONE the cursor is already on the first element of the table
71528** otherwise some kind of error occurred
71529**
71530** The main entry point is sqlite3BtreePrevious(). That routine is optimized
71531** for the common case of merely decrementing the cell counter BtCursor.aiIdx
71532** to the previous cell on the current page. The (slower) btreePrevious()
71533** helper routine is called when it is necessary to move to a different page
71534** or to restore the cursor.
71535**
71536** If bit 0x01 of the F argument to sqlite3BtreePrevious(C,F) is 1, then
71537** the cursor corresponds to an SQL index and this routine could have been
71538** skipped if the SQL index had been a unique index. The F argument is a
71539** hint to the implement. The native SQLite btree implementation does not
71540** use this hint, but COMDB2 does.
71541*/
71542static SQLITE_NOINLINE int btreePrevious(BtCursor *pCur){
71543 int rc;
71544 MemPage *pPage;
71545
71546 assert( cursorOwnsBtShared(pCur) );
71547 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 );
71548 assert( pCur->info.nSize==0 );
71549 if( pCur->eState!=CURSOR_VALID ){
71550 rc = restoreCursorPosition(pCur);
71551 if( rc!=SQLITE_OK ){
71552 return rc;
71553 }
71554 if( CURSOR_INVALID==pCur->eState ){
71555 return SQLITE_DONE;
71556 }
71557 if( CURSOR_SKIPNEXT==pCur->eState ){
71558 pCur->eState = CURSOR_VALID;
71559 if( pCur->skipNext<0 ) return SQLITE_OK;
71560 }
71561 }
71562
71563 pPage = pCur->pPage;
71564 assert( pPage->isInit );
71565 if( !pPage->leaf ){
71566 int idx = pCur->ix;
71567 rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
71568 if( rc ) return rc;
71569 rc = moveToRightmost(pCur);
71570 }else{
71571 while( pCur->ix==0 ){
71572 if( pCur->iPage==0 ){
71573 pCur->eState = CURSOR_INVALID;
71574 return SQLITE_DONE;
71575 }
71576 moveToParent(pCur);
71577 }
71578 assert( pCur->info.nSize==0 );
71579 assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 );
71580
71581 pCur->ix--;
71582 pPage = pCur->pPage;
71583 if( pPage->intKey && !pPage->leaf ){
71584 rc = sqlite3BtreePrevious(pCur, flags: 0);
71585 }else{
71586 rc = SQLITE_OK;
71587 }
71588 }
71589 return rc;
71590}
71591SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int flags){
71592 assert( cursorOwnsBtShared(pCur) );
71593 assert( flags==0 || flags==1 );
71594 UNUSED_PARAMETER( flags ); /* Used in COMDB2 but not native SQLite */
71595 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey);
71596 pCur->info.nSize = 0;
71597 if( pCur->eState!=CURSOR_VALID
71598 || pCur->ix==0
71599 || pCur->pPage->leaf==0
71600 ){
71601 return btreePrevious(pCur);
71602 }
71603 pCur->ix--;
71604 return SQLITE_OK;
71605}
71606
71607/*
71608** Allocate a new page from the database file.
71609**
71610** The new page is marked as dirty. (In other words, sqlite3PagerWrite()
71611** has already been called on the new page.) The new page has also
71612** been referenced and the calling routine is responsible for calling
71613** sqlite3PagerUnref() on the new page when it is done.
71614**
71615** SQLITE_OK is returned on success. Any other return value indicates
71616** an error. *ppPage is set to NULL in the event of an error.
71617**
71618** If the "nearby" parameter is not 0, then an effort is made to
71619** locate a page close to the page number "nearby". This can be used in an
71620** attempt to keep related pages close to each other in the database file,
71621** which in turn can make database access faster.
71622**
71623** If the eMode parameter is BTALLOC_EXACT and the nearby page exists
71624** anywhere on the free-list, then it is guaranteed to be returned. If
71625** eMode is BTALLOC_LT then the page returned will be less than or equal
71626** to nearby if any such page exists. If eMode is BTALLOC_ANY then there
71627** are no restrictions on which page is returned.
71628*/
71629static int allocateBtreePage(
71630 BtShared *pBt, /* The btree */
71631 MemPage **ppPage, /* Store pointer to the allocated page here */
71632 Pgno *pPgno, /* Store the page number here */
71633 Pgno nearby, /* Search for a page near this one */
71634 u8 eMode /* BTALLOC_EXACT, BTALLOC_LT, or BTALLOC_ANY */
71635){
71636 MemPage *pPage1;
71637 int rc;
71638 u32 n; /* Number of pages on the freelist */
71639 u32 k; /* Number of leaves on the trunk of the freelist */
71640 MemPage *pTrunk = 0;
71641 MemPage *pPrevTrunk = 0;
71642 Pgno mxPage; /* Total size of the database file */
71643
71644 assert( sqlite3_mutex_held(pBt->mutex) );
71645 assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) );
71646 pPage1 = pBt->pPage1;
71647 mxPage = btreePagecount(pBt);
71648 /* EVIDENCE-OF: R-05119-02637 The 4-byte big-endian integer at offset 36
71649 ** stores stores the total number of pages on the freelist. */
71650 n = get4byte(p: &pPage1->aData[36]);
71651 testcase( n==mxPage-1 );
71652 if( n>=mxPage ){
71653 return SQLITE_CORRUPT_BKPT;
71654 }
71655 if( n>0 ){
71656 /* There are pages on the freelist. Reuse one of those pages. */
71657 Pgno iTrunk;
71658 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
71659 u32 nSearch = 0; /* Count of the number of search attempts */
71660
71661 /* If eMode==BTALLOC_EXACT and a query of the pointer-map
71662 ** shows that the page 'nearby' is somewhere on the free-list, then
71663 ** the entire-list will be searched for that page.
71664 */
71665#ifndef SQLITE_OMIT_AUTOVACUUM
71666 if( eMode==BTALLOC_EXACT ){
71667 if( nearby<=mxPage ){
71668 u8 eType;
71669 assert( nearby>0 );
71670 assert( pBt->autoVacuum );
71671 rc = ptrmapGet(pBt, key: nearby, pEType: &eType, pPgno: 0);
71672 if( rc ) return rc;
71673 if( eType==PTRMAP_FREEPAGE ){
71674 searchList = 1;
71675 }
71676 }
71677 }else if( eMode==BTALLOC_LE ){
71678 searchList = 1;
71679 }
71680#endif
71681
71682 /* Decrement the free-list count by 1. Set iTrunk to the index of the
71683 ** first free-list trunk page. iPrevTrunk is initially 1.
71684 */
71685 rc = sqlite3PagerWrite(pPg: pPage1->pDbPage);
71686 if( rc ) return rc;
71687 put4byte(p: &pPage1->aData[36], v: n-1);
71688
71689 /* The code within this loop is run only once if the 'searchList' variable
71690 ** is not true. Otherwise, it runs once for each trunk-page on the
71691 ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
71692 ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT)
71693 */
71694 do {
71695 pPrevTrunk = pTrunk;
71696 if( pPrevTrunk ){
71697 /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page
71698 ** is the page number of the next freelist trunk page in the list or
71699 ** zero if this is the last freelist trunk page. */
71700 iTrunk = get4byte(p: &pPrevTrunk->aData[0]);
71701 }else{
71702 /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32
71703 ** stores the page number of the first page of the freelist, or zero if
71704 ** the freelist is empty. */
71705 iTrunk = get4byte(p: &pPage1->aData[32]);
71706 }
71707 testcase( iTrunk==mxPage );
71708 if( iTrunk>mxPage || nSearch++ > n ){
71709 rc = SQLITE_CORRUPT_PGNO(pPrevTrunk ? pPrevTrunk->pgno : 1);
71710 }else{
71711 rc = btreeGetUnusedPage(pBt, pgno: iTrunk, ppPage: &pTrunk, flags: 0);
71712 }
71713 if( rc ){
71714 pTrunk = 0;
71715 goto end_allocate_page;
71716 }
71717 assert( pTrunk!=0 );
71718 assert( pTrunk->aData!=0 );
71719 /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page
71720 ** is the number of leaf page pointers to follow. */
71721 k = get4byte(p: &pTrunk->aData[4]);
71722 if( k==0 && !searchList ){
71723 /* The trunk has no leaves and the list is not being searched.
71724 ** So extract the trunk page itself and use it as the newly
71725 ** allocated page */
71726 assert( pPrevTrunk==0 );
71727 rc = sqlite3PagerWrite(pPg: pTrunk->pDbPage);
71728 if( rc ){
71729 goto end_allocate_page;
71730 }
71731 *pPgno = iTrunk;
71732 memcpy(dest: &pPage1->aData[32], src: &pTrunk->aData[0], n: 4);
71733 *ppPage = pTrunk;
71734 pTrunk = 0;
71735 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
71736 }else if( k>(u32)(pBt->usableSize/4 - 2) ){
71737 /* Value of k is out of range. Database corruption */
71738 rc = SQLITE_CORRUPT_PGNO(iTrunk);
71739 goto end_allocate_page;
71740#ifndef SQLITE_OMIT_AUTOVACUUM
71741 }else if( searchList
71742 && (nearby==iTrunk || (iTrunk<nearby && eMode==BTALLOC_LE))
71743 ){
71744 /* The list is being searched and this trunk page is the page
71745 ** to allocate, regardless of whether it has leaves.
71746 */
71747 *pPgno = iTrunk;
71748 *ppPage = pTrunk;
71749 searchList = 0;
71750 rc = sqlite3PagerWrite(pPg: pTrunk->pDbPage);
71751 if( rc ){
71752 goto end_allocate_page;
71753 }
71754 if( k==0 ){
71755 if( !pPrevTrunk ){
71756 memcpy(dest: &pPage1->aData[32], src: &pTrunk->aData[0], n: 4);
71757 }else{
71758 rc = sqlite3PagerWrite(pPg: pPrevTrunk->pDbPage);
71759 if( rc!=SQLITE_OK ){
71760 goto end_allocate_page;
71761 }
71762 memcpy(dest: &pPrevTrunk->aData[0], src: &pTrunk->aData[0], n: 4);
71763 }
71764 }else{
71765 /* The trunk page is required by the caller but it contains
71766 ** pointers to free-list leaves. The first leaf becomes a trunk
71767 ** page in this case.
71768 */
71769 MemPage *pNewTrunk;
71770 Pgno iNewTrunk = get4byte(p: &pTrunk->aData[8]);
71771 if( iNewTrunk>mxPage ){
71772 rc = SQLITE_CORRUPT_PGNO(iTrunk);
71773 goto end_allocate_page;
71774 }
71775 testcase( iNewTrunk==mxPage );
71776 rc = btreeGetUnusedPage(pBt, pgno: iNewTrunk, ppPage: &pNewTrunk, flags: 0);
71777 if( rc!=SQLITE_OK ){
71778 goto end_allocate_page;
71779 }
71780 rc = sqlite3PagerWrite(pPg: pNewTrunk->pDbPage);
71781 if( rc!=SQLITE_OK ){
71782 releasePage(pPage: pNewTrunk);
71783 goto end_allocate_page;
71784 }
71785 memcpy(dest: &pNewTrunk->aData[0], src: &pTrunk->aData[0], n: 4);
71786 put4byte(p: &pNewTrunk->aData[4], v: k-1);
71787 memcpy(dest: &pNewTrunk->aData[8], src: &pTrunk->aData[12], n: (k-1)*4);
71788 releasePage(pPage: pNewTrunk);
71789 if( !pPrevTrunk ){
71790 assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
71791 put4byte(p: &pPage1->aData[32], v: iNewTrunk);
71792 }else{
71793 rc = sqlite3PagerWrite(pPg: pPrevTrunk->pDbPage);
71794 if( rc ){
71795 goto end_allocate_page;
71796 }
71797 put4byte(p: &pPrevTrunk->aData[0], v: iNewTrunk);
71798 }
71799 }
71800 pTrunk = 0;
71801 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
71802#endif
71803 }else if( k>0 ){
71804 /* Extract a leaf from the trunk */
71805 u32 closest;
71806 Pgno iPage;
71807 unsigned char *aData = pTrunk->aData;
71808 if( nearby>0 ){
71809 u32 i;
71810 closest = 0;
71811 if( eMode==BTALLOC_LE ){
71812 for(i=0; i<k; i++){
71813 iPage = get4byte(p: &aData[8+i*4]);
71814 if( iPage<=nearby ){
71815 closest = i;
71816 break;
71817 }
71818 }
71819 }else{
71820 int dist;
71821 dist = sqlite3AbsInt32(get4byte(p: &aData[8]) - nearby);
71822 for(i=1; i<k; i++){
71823 int d2 = sqlite3AbsInt32(get4byte(p: &aData[8+i*4]) - nearby);
71824 if( d2<dist ){
71825 closest = i;
71826 dist = d2;
71827 }
71828 }
71829 }
71830 }else{
71831 closest = 0;
71832 }
71833
71834 iPage = get4byte(p: &aData[8+closest*4]);
71835 testcase( iPage==mxPage );
71836 if( iPage>mxPage || iPage<2 ){
71837 rc = SQLITE_CORRUPT_PGNO(iTrunk);
71838 goto end_allocate_page;
71839 }
71840 testcase( iPage==mxPage );
71841 if( !searchList
71842 || (iPage==nearby || (iPage<nearby && eMode==BTALLOC_LE))
71843 ){
71844 int noContent;
71845 *pPgno = iPage;
71846 TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
71847 ": %d more free pages\n",
71848 *pPgno, closest+1, k, pTrunk->pgno, n-1));
71849 rc = sqlite3PagerWrite(pPg: pTrunk->pDbPage);
71850 if( rc ) goto end_allocate_page;
71851 if( closest<k-1 ){
71852 memcpy(dest: &aData[8+closest*4], src: &aData[4+k*4], n: 4);
71853 }
71854 put4byte(p: &aData[4], v: k-1);
71855 noContent = !btreeGetHasContent(pBt, pgno: *pPgno)? PAGER_GET_NOCONTENT : 0;
71856 rc = btreeGetUnusedPage(pBt, pgno: *pPgno, ppPage, flags: noContent);
71857 if( rc==SQLITE_OK ){
71858 rc = sqlite3PagerWrite(pPg: (*ppPage)->pDbPage);
71859 if( rc!=SQLITE_OK ){
71860 releasePage(pPage: *ppPage);
71861 *ppPage = 0;
71862 }
71863 }
71864 searchList = 0;
71865 }
71866 }
71867 releasePage(pPage: pPrevTrunk);
71868 pPrevTrunk = 0;
71869 }while( searchList );
71870 }else{
71871 /* There are no pages on the freelist, so append a new page to the
71872 ** database image.
71873 **
71874 ** Normally, new pages allocated by this block can be requested from the
71875 ** pager layer with the 'no-content' flag set. This prevents the pager
71876 ** from trying to read the pages content from disk. However, if the
71877 ** current transaction has already run one or more incremental-vacuum
71878 ** steps, then the page we are about to allocate may contain content
71879 ** that is required in the event of a rollback. In this case, do
71880 ** not set the no-content flag. This causes the pager to load and journal
71881 ** the current page content before overwriting it.
71882 **
71883 ** Note that the pager will not actually attempt to load or journal
71884 ** content for any page that really does lie past the end of the database
71885 ** file on disk. So the effects of disabling the no-content optimization
71886 ** here are confined to those pages that lie between the end of the
71887 ** database image and the end of the database file.
71888 */
71889 int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0;
71890
71891 rc = sqlite3PagerWrite(pPg: pBt->pPage1->pDbPage);
71892 if( rc ) return rc;
71893 pBt->nPage++;
71894 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
71895
71896#ifndef SQLITE_OMIT_AUTOVACUUM
71897 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
71898 /* If *pPgno refers to a pointer-map page, allocate two new pages
71899 ** at the end of the file instead of one. The first allocated page
71900 ** becomes a new pointer-map page, the second is used by the caller.
71901 */
71902 MemPage *pPg = 0;
71903 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
71904 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
71905 rc = btreeGetUnusedPage(pBt, pgno: pBt->nPage, ppPage: &pPg, flags: bNoContent);
71906 if( rc==SQLITE_OK ){
71907 rc = sqlite3PagerWrite(pPg: pPg->pDbPage);
71908 releasePage(pPage: pPg);
71909 }
71910 if( rc ) return rc;
71911 pBt->nPage++;
71912 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
71913 }
71914#endif
71915 put4byte(p: 28 + (u8*)pBt->pPage1->aData, v: pBt->nPage);
71916 *pPgno = pBt->nPage;
71917
71918 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
71919 rc = btreeGetUnusedPage(pBt, pgno: *pPgno, ppPage, flags: bNoContent);
71920 if( rc ) return rc;
71921 rc = sqlite3PagerWrite(pPg: (*ppPage)->pDbPage);
71922 if( rc!=SQLITE_OK ){
71923 releasePage(pPage: *ppPage);
71924 *ppPage = 0;
71925 }
71926 TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
71927 }
71928
71929 assert( CORRUPT_DB || *pPgno!=PENDING_BYTE_PAGE(pBt) );
71930
71931end_allocate_page:
71932 releasePage(pPage: pTrunk);
71933 releasePage(pPage: pPrevTrunk);
71934 assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 );
71935 assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 );
71936 return rc;
71937}
71938
71939/*
71940** This function is used to add page iPage to the database file free-list.
71941** It is assumed that the page is not already a part of the free-list.
71942**
71943** The value passed as the second argument to this function is optional.
71944** If the caller happens to have a pointer to the MemPage object
71945** corresponding to page iPage handy, it may pass it as the second value.
71946** Otherwise, it may pass NULL.
71947**
71948** If a pointer to a MemPage object is passed as the second argument,
71949** its reference count is not altered by this function.
71950*/
71951static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
71952 MemPage *pTrunk = 0; /* Free-list trunk page */
71953 Pgno iTrunk = 0; /* Page number of free-list trunk page */
71954 MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */
71955 MemPage *pPage; /* Page being freed. May be NULL. */
71956 int rc; /* Return Code */
71957 u32 nFree; /* Initial number of pages on free-list */
71958
71959 assert( sqlite3_mutex_held(pBt->mutex) );
71960 assert( CORRUPT_DB || iPage>1 );
71961 assert( !pMemPage || pMemPage->pgno==iPage );
71962
71963 if( NEVER(iPage<2) || iPage>pBt->nPage ){
71964 return SQLITE_CORRUPT_BKPT;
71965 }
71966 if( pMemPage ){
71967 pPage = pMemPage;
71968 sqlite3PagerRef(pPg: pPage->pDbPage);
71969 }else{
71970 pPage = btreePageLookup(pBt, pgno: iPage);
71971 }
71972
71973 /* Increment the free page count on pPage1 */
71974 rc = sqlite3PagerWrite(pPg: pPage1->pDbPage);
71975 if( rc ) goto freepage_out;
71976 nFree = get4byte(p: &pPage1->aData[36]);
71977 put4byte(p: &pPage1->aData[36], v: nFree+1);
71978
71979 if( pBt->btsFlags & BTS_SECURE_DELETE ){
71980 /* If the secure_delete option is enabled, then
71981 ** always fully overwrite deleted information with zeros.
71982 */
71983 if( (!pPage && ((rc = btreeGetPage(pBt, pgno: iPage, ppPage: &pPage, flags: 0))!=0) )
71984 || ((rc = sqlite3PagerWrite(pPg: pPage->pDbPage))!=0)
71985 ){
71986 goto freepage_out;
71987 }
71988 memset(s: pPage->aData, c: 0, n: pPage->pBt->pageSize);
71989 }
71990
71991 /* If the database supports auto-vacuum, write an entry in the pointer-map
71992 ** to indicate that the page is free.
71993 */
71994 if( ISAUTOVACUUM ){
71995 ptrmapPut(pBt, key: iPage, PTRMAP_FREEPAGE, parent: 0, pRC: &rc);
71996 if( rc ) goto freepage_out;
71997 }
71998
71999 /* Now manipulate the actual database free-list structure. There are two
72000 ** possibilities. If the free-list is currently empty, or if the first
72001 ** trunk page in the free-list is full, then this page will become a
72002 ** new free-list trunk page. Otherwise, it will become a leaf of the
72003 ** first trunk page in the current free-list. This block tests if it
72004 ** is possible to add the page as a new free-list leaf.
72005 */
72006 if( nFree!=0 ){
72007 u32 nLeaf; /* Initial number of leaf cells on trunk page */
72008
72009 iTrunk = get4byte(p: &pPage1->aData[32]);
72010 if( iTrunk>btreePagecount(pBt) ){
72011 rc = SQLITE_CORRUPT_BKPT;
72012 goto freepage_out;
72013 }
72014 rc = btreeGetPage(pBt, pgno: iTrunk, ppPage: &pTrunk, flags: 0);
72015 if( rc!=SQLITE_OK ){
72016 goto freepage_out;
72017 }
72018
72019 nLeaf = get4byte(p: &pTrunk->aData[4]);
72020 assert( pBt->usableSize>32 );
72021 if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
72022 rc = SQLITE_CORRUPT_BKPT;
72023 goto freepage_out;
72024 }
72025 if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
72026 /* In this case there is room on the trunk page to insert the page
72027 ** being freed as a new leaf.
72028 **
72029 ** Note that the trunk page is not really full until it contains
72030 ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
72031 ** coded. But due to a coding error in versions of SQLite prior to
72032 ** 3.6.0, databases with freelist trunk pages holding more than
72033 ** usableSize/4 - 8 entries will be reported as corrupt. In order
72034 ** to maintain backwards compatibility with older versions of SQLite,
72035 ** we will continue to restrict the number of entries to usableSize/4 - 8
72036 ** for now. At some point in the future (once everyone has upgraded
72037 ** to 3.6.0 or later) we should consider fixing the conditional above
72038 ** to read "usableSize/4-2" instead of "usableSize/4-8".
72039 **
72040 ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still
72041 ** avoid using the last six entries in the freelist trunk page array in
72042 ** order that database files created by newer versions of SQLite can be
72043 ** read by older versions of SQLite.
72044 */
72045 rc = sqlite3PagerWrite(pPg: pTrunk->pDbPage);
72046 if( rc==SQLITE_OK ){
72047 put4byte(p: &pTrunk->aData[4], v: nLeaf+1);
72048 put4byte(p: &pTrunk->aData[8+nLeaf*4], v: iPage);
72049 if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
72050 sqlite3PagerDontWrite(pPg: pPage->pDbPage);
72051 }
72052 rc = btreeSetHasContent(pBt, pgno: iPage);
72053 }
72054 TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
72055 goto freepage_out;
72056 }
72057 }
72058
72059 /* If control flows to this point, then it was not possible to add the
72060 ** the page being freed as a leaf page of the first trunk in the free-list.
72061 ** Possibly because the free-list is empty, or possibly because the
72062 ** first trunk in the free-list is full. Either way, the page being freed
72063 ** will become the new first trunk page in the free-list.
72064 */
72065 if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, pgno: iPage, ppPage: &pPage, flags: 0)) ){
72066 goto freepage_out;
72067 }
72068 rc = sqlite3PagerWrite(pPg: pPage->pDbPage);
72069 if( rc!=SQLITE_OK ){
72070 goto freepage_out;
72071 }
72072 put4byte(p: pPage->aData, v: iTrunk);
72073 put4byte(p: &pPage->aData[4], v: 0);
72074 put4byte(p: &pPage1->aData[32], v: iPage);
72075 TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
72076
72077freepage_out:
72078 if( pPage ){
72079 pPage->isInit = 0;
72080 }
72081 releasePage(pPage);
72082 releasePage(pPage: pTrunk);
72083 return rc;
72084}
72085static void freePage(MemPage *pPage, int *pRC){
72086 if( (*pRC)==SQLITE_OK ){
72087 *pRC = freePage2(pBt: pPage->pBt, pMemPage: pPage, iPage: pPage->pgno);
72088 }
72089}
72090
72091/*
72092** Free the overflow pages associated with the given Cell.
72093*/
72094static SQLITE_NOINLINE int clearCellOverflow(
72095 MemPage *pPage, /* The page that contains the Cell */
72096 unsigned char *pCell, /* First byte of the Cell */
72097 CellInfo *pInfo /* Size information about the cell */
72098){
72099 BtShared *pBt;
72100 Pgno ovflPgno;
72101 int rc;
72102 int nOvfl;
72103 u32 ovflPageSize;
72104
72105 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72106 assert( pInfo->nLocal!=pInfo->nPayload );
72107 testcase( pCell + pInfo->nSize == pPage->aDataEnd );
72108 testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd );
72109 if( pCell + pInfo->nSize > pPage->aDataEnd ){
72110 /* Cell extends past end of page */
72111 return SQLITE_CORRUPT_PAGE(pPage);
72112 }
72113 ovflPgno = get4byte(p: pCell + pInfo->nSize - 4);
72114 pBt = pPage->pBt;
72115 assert( pBt->usableSize > 4 );
72116 ovflPageSize = pBt->usableSize - 4;
72117 nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
72118 assert( nOvfl>0 ||
72119 (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize)
72120 );
72121 while( nOvfl-- ){
72122 Pgno iNext = 0;
72123 MemPage *pOvfl = 0;
72124 if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){
72125 /* 0 is not a legal page number and page 1 cannot be an
72126 ** overflow page. Therefore if ovflPgno<2 or past the end of the
72127 ** file the database must be corrupt. */
72128 return SQLITE_CORRUPT_BKPT;
72129 }
72130 if( nOvfl ){
72131 rc = getOverflowPage(pBt, ovfl: ovflPgno, ppPage: &pOvfl, pPgnoNext: &iNext);
72132 if( rc ) return rc;
72133 }
72134
72135 if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, pgno: ovflPgno))!=0) )
72136 && sqlite3PagerPageRefcount(pPage: pOvfl->pDbPage)!=1
72137 ){
72138 /* There is no reason any cursor should have an outstanding reference
72139 ** to an overflow page belonging to a cell that is being deleted/updated.
72140 ** So if there exists more than one reference to this page, then it
72141 ** must not really be an overflow page and the database must be corrupt.
72142 ** It is helpful to detect this before calling freePage2(), as
72143 ** freePage2() may zero the page contents if secure-delete mode is
72144 ** enabled. If this 'overflow' page happens to be a page that the
72145 ** caller is iterating through or using in some other way, this
72146 ** can be problematic.
72147 */
72148 rc = SQLITE_CORRUPT_BKPT;
72149 }else{
72150 rc = freePage2(pBt, pMemPage: pOvfl, iPage: ovflPgno);
72151 }
72152
72153 if( pOvfl ){
72154 sqlite3PagerUnref(pPg: pOvfl->pDbPage);
72155 }
72156 if( rc ) return rc;
72157 ovflPgno = iNext;
72158 }
72159 return SQLITE_OK;
72160}
72161
72162/* Call xParseCell to compute the size of a cell. If the cell contains
72163** overflow, then invoke cellClearOverflow to clear out that overflow.
72164** STore the result code (SQLITE_OK or some error code) in rc.
72165**
72166** Implemented as macro to force inlining for performance.
72167*/
72168#define BTREE_CLEAR_CELL(rc, pPage, pCell, sInfo) \
72169 pPage->xParseCell(pPage, pCell, &sInfo); \
72170 if( sInfo.nLocal!=sInfo.nPayload ){ \
72171 rc = clearCellOverflow(pPage, pCell, &sInfo); \
72172 }else{ \
72173 rc = SQLITE_OK; \
72174 }
72175
72176
72177/*
72178** Create the byte sequence used to represent a cell on page pPage
72179** and write that byte sequence into pCell[]. Overflow pages are
72180** allocated and filled in as necessary. The calling procedure
72181** is responsible for making sure sufficient space has been allocated
72182** for pCell[].
72183**
72184** Note that pCell does not necessary need to point to the pPage->aData
72185** area. pCell might point to some temporary storage. The cell will
72186** be constructed in this temporary area then copied into pPage->aData
72187** later.
72188*/
72189static int fillInCell(
72190 MemPage *pPage, /* The page that contains the cell */
72191 unsigned char *pCell, /* Complete text of the cell */
72192 const BtreePayload *pX, /* Payload with which to construct the cell */
72193 int *pnSize /* Write cell size here */
72194){
72195 int nPayload;
72196 const u8 *pSrc;
72197 int nSrc, n, rc, mn;
72198 int spaceLeft;
72199 MemPage *pToRelease;
72200 unsigned char *pPrior;
72201 unsigned char *pPayload;
72202 BtShared *pBt;
72203 Pgno pgnoOvfl;
72204 int nHeader;
72205
72206 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72207
72208 /* pPage is not necessarily writeable since pCell might be auxiliary
72209 ** buffer space that is separate from the pPage buffer area */
72210 assert( pCell<pPage->aData || pCell>=&pPage->aData[pPage->pBt->pageSize]
72211 || sqlite3PagerIswriteable(pPage->pDbPage) );
72212
72213 /* Fill in the header. */
72214 nHeader = pPage->childPtrSize;
72215 if( pPage->intKey ){
72216 nPayload = pX->nData + pX->nZero;
72217 pSrc = pX->pData;
72218 nSrc = pX->nData;
72219 assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */
72220 nHeader += putVarint32(&pCell[nHeader], nPayload);
72221 nHeader += putVarint(p: &pCell[nHeader], v: *(u64*)&pX->nKey);
72222 }else{
72223 assert( pX->nKey<=0x7fffffff && pX->pKey!=0 );
72224 nSrc = nPayload = (int)pX->nKey;
72225 pSrc = pX->pKey;
72226 nHeader += putVarint32(&pCell[nHeader], nPayload);
72227 }
72228
72229 /* Fill in the payload */
72230 pPayload = &pCell[nHeader];
72231 if( nPayload<=pPage->maxLocal ){
72232 /* This is the common case where everything fits on the btree page
72233 ** and no overflow pages are required. */
72234 n = nHeader + nPayload;
72235 testcase( n==3 );
72236 testcase( n==4 );
72237 if( n<4 ) n = 4;
72238 *pnSize = n;
72239 assert( nSrc<=nPayload );
72240 testcase( nSrc<nPayload );
72241 memcpy(dest: pPayload, src: pSrc, n: nSrc);
72242 memset(s: pPayload+nSrc, c: 0, n: nPayload-nSrc);
72243 return SQLITE_OK;
72244 }
72245
72246 /* If we reach this point, it means that some of the content will need
72247 ** to spill onto overflow pages.
72248 */
72249 mn = pPage->minLocal;
72250 n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
72251 testcase( n==pPage->maxLocal );
72252 testcase( n==pPage->maxLocal+1 );
72253 if( n > pPage->maxLocal ) n = mn;
72254 spaceLeft = n;
72255 *pnSize = n + nHeader + 4;
72256 pPrior = &pCell[nHeader+n];
72257 pToRelease = 0;
72258 pgnoOvfl = 0;
72259 pBt = pPage->pBt;
72260
72261 /* At this point variables should be set as follows:
72262 **
72263 ** nPayload Total payload size in bytes
72264 ** pPayload Begin writing payload here
72265 ** spaceLeft Space available at pPayload. If nPayload>spaceLeft,
72266 ** that means content must spill into overflow pages.
72267 ** *pnSize Size of the local cell (not counting overflow pages)
72268 ** pPrior Where to write the pgno of the first overflow page
72269 **
72270 ** Use a call to btreeParseCellPtr() to verify that the values above
72271 ** were computed correctly.
72272 */
72273#ifdef SQLITE_DEBUG
72274 {
72275 CellInfo info;
72276 pPage->xParseCell(pPage, pCell, &info);
72277 assert( nHeader==(int)(info.pPayload - pCell) );
72278 assert( info.nKey==pX->nKey );
72279 assert( *pnSize == info.nSize );
72280 assert( spaceLeft == info.nLocal );
72281 }
72282#endif
72283
72284 /* Write the payload into the local Cell and any extra into overflow pages */
72285 while( 1 ){
72286 n = nPayload;
72287 if( n>spaceLeft ) n = spaceLeft;
72288
72289 /* If pToRelease is not zero than pPayload points into the data area
72290 ** of pToRelease. Make sure pToRelease is still writeable. */
72291 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
72292
72293 /* If pPayload is part of the data area of pPage, then make sure pPage
72294 ** is still writeable */
72295 assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
72296 || sqlite3PagerIswriteable(pPage->pDbPage) );
72297
72298 if( nSrc>=n ){
72299 memcpy(dest: pPayload, src: pSrc, n: n);
72300 }else if( nSrc>0 ){
72301 n = nSrc;
72302 memcpy(dest: pPayload, src: pSrc, n: n);
72303 }else{
72304 memset(s: pPayload, c: 0, n: n);
72305 }
72306 nPayload -= n;
72307 if( nPayload<=0 ) break;
72308 pPayload += n;
72309 pSrc += n;
72310 nSrc -= n;
72311 spaceLeft -= n;
72312 if( spaceLeft==0 ){
72313 MemPage *pOvfl = 0;
72314#ifndef SQLITE_OMIT_AUTOVACUUM
72315 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
72316 if( pBt->autoVacuum ){
72317 do{
72318 pgnoOvfl++;
72319 } while(
72320 PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt)
72321 );
72322 }
72323#endif
72324 rc = allocateBtreePage(pBt, ppPage: &pOvfl, pPgno: &pgnoOvfl, nearby: pgnoOvfl, eMode: 0);
72325#ifndef SQLITE_OMIT_AUTOVACUUM
72326 /* If the database supports auto-vacuum, and the second or subsequent
72327 ** overflow page is being allocated, add an entry to the pointer-map
72328 ** for that page now.
72329 **
72330 ** If this is the first overflow page, then write a partial entry
72331 ** to the pointer-map. If we write nothing to this pointer-map slot,
72332 ** then the optimistic overflow chain processing in clearCell()
72333 ** may misinterpret the uninitialized values and delete the
72334 ** wrong pages from the database.
72335 */
72336 if( pBt->autoVacuum && rc==SQLITE_OK ){
72337 u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1);
72338 ptrmapPut(pBt, key: pgnoOvfl, eType, parent: pgnoPtrmap, pRC: &rc);
72339 if( rc ){
72340 releasePage(pPage: pOvfl);
72341 }
72342 }
72343#endif
72344 if( rc ){
72345 releasePage(pPage: pToRelease);
72346 return rc;
72347 }
72348
72349 /* If pToRelease is not zero than pPrior points into the data area
72350 ** of pToRelease. Make sure pToRelease is still writeable. */
72351 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
72352
72353 /* If pPrior is part of the data area of pPage, then make sure pPage
72354 ** is still writeable */
72355 assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
72356 || sqlite3PagerIswriteable(pPage->pDbPage) );
72357
72358 put4byte(p: pPrior, v: pgnoOvfl);
72359 releasePage(pPage: pToRelease);
72360 pToRelease = pOvfl;
72361 pPrior = pOvfl->aData;
72362 put4byte(p: pPrior, v: 0);
72363 pPayload = &pOvfl->aData[4];
72364 spaceLeft = pBt->usableSize - 4;
72365 }
72366 }
72367 releasePage(pPage: pToRelease);
72368 return SQLITE_OK;
72369}
72370
72371/*
72372** Remove the i-th cell from pPage. This routine effects pPage only.
72373** The cell content is not freed or deallocated. It is assumed that
72374** the cell content has been copied someplace else. This routine just
72375** removes the reference to the cell from pPage.
72376**
72377** "sz" must be the number of bytes in the cell.
72378*/
72379static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
72380 u32 pc; /* Offset to cell content of cell being deleted */
72381 u8 *data; /* pPage->aData */
72382 u8 *ptr; /* Used to move bytes around within data[] */
72383 int rc; /* The return code */
72384 int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */
72385
72386 if( *pRC ) return;
72387 assert( idx>=0 );
72388 assert( idx<pPage->nCell );
72389 assert( CORRUPT_DB || sz==cellSize(pPage, idx) );
72390 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
72391 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72392 assert( pPage->nFree>=0 );
72393 data = pPage->aData;
72394 ptr = &pPage->aCellIdx[2*idx];
72395 assert( pPage->pBt->usableSize > (int)(ptr-data) );
72396 pc = get2byte(ptr);
72397 hdr = pPage->hdrOffset;
72398 testcase( pc==(u32)get2byte(&data[hdr+5]) );
72399 testcase( pc+sz==pPage->pBt->usableSize );
72400 if( pc+sz > pPage->pBt->usableSize ){
72401 *pRC = SQLITE_CORRUPT_BKPT;
72402 return;
72403 }
72404 rc = freeSpace(pPage, iStart: pc, iSize: sz);
72405 if( rc ){
72406 *pRC = rc;
72407 return;
72408 }
72409 pPage->nCell--;
72410 if( pPage->nCell==0 ){
72411 memset(s: &data[hdr+1], c: 0, n: 4);
72412 data[hdr+7] = 0;
72413 put2byte(&data[hdr+5], pPage->pBt->usableSize);
72414 pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset
72415 - pPage->childPtrSize - 8;
72416 }else{
72417 memmove(dest: ptr, src: ptr+2, n: 2*(pPage->nCell - idx));
72418 put2byte(&data[hdr+3], pPage->nCell);
72419 pPage->nFree += 2;
72420 }
72421}
72422
72423/*
72424** Insert a new cell on pPage at cell index "i". pCell points to the
72425** content of the cell.
72426**
72427** If the cell content will fit on the page, then put it there. If it
72428** will not fit, then make a copy of the cell content into pTemp if
72429** pTemp is not null. Regardless of pTemp, allocate a new entry
72430** in pPage->apOvfl[] and make it point to the cell content (either
72431** in pTemp or the original pCell) and also record its index.
72432** Allocating a new entry in pPage->aCell[] implies that
72433** pPage->nOverflow is incremented.
72434**
72435** *pRC must be SQLITE_OK when this routine is called.
72436*/
72437static void insertCell(
72438 MemPage *pPage, /* Page into which we are copying */
72439 int i, /* New cell becomes the i-th cell of the page */
72440 u8 *pCell, /* Content of the new cell */
72441 int sz, /* Bytes of content in pCell */
72442 u8 *pTemp, /* Temp storage space for pCell, if needed */
72443 Pgno iChild, /* If non-zero, replace first 4 bytes with this value */
72444 int *pRC /* Read and write return code from here */
72445){
72446 int idx = 0; /* Where to write new cell content in data[] */
72447 int j; /* Loop counter */
72448 u8 *data; /* The content of the whole page */
72449 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
72450
72451 assert( *pRC==SQLITE_OK );
72452 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
72453 assert( MX_CELL(pPage->pBt)<=10921 );
72454 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
72455 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
72456 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
72457 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72458 assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB );
72459 assert( pPage->nFree>=0 );
72460 if( pPage->nOverflow || sz+2>pPage->nFree ){
72461 if( pTemp ){
72462 memcpy(dest: pTemp, src: pCell, n: sz);
72463 pCell = pTemp;
72464 }
72465 if( iChild ){
72466 put4byte(p: pCell, v: iChild);
72467 }
72468 j = pPage->nOverflow++;
72469 /* Comparison against ArraySize-1 since we hold back one extra slot
72470 ** as a contingency. In other words, never need more than 3 overflow
72471 ** slots but 4 are allocated, just to be safe. */
72472 assert( j < ArraySize(pPage->apOvfl)-1 );
72473 pPage->apOvfl[j] = pCell;
72474 pPage->aiOvfl[j] = (u16)i;
72475
72476 /* When multiple overflows occur, they are always sequential and in
72477 ** sorted order. This invariants arise because multiple overflows can
72478 ** only occur when inserting divider cells into the parent page during
72479 ** balancing, and the dividers are adjacent and sorted.
72480 */
72481 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
72482 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
72483 }else{
72484 int rc = sqlite3PagerWrite(pPg: pPage->pDbPage);
72485 if( rc!=SQLITE_OK ){
72486 *pRC = rc;
72487 return;
72488 }
72489 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
72490 data = pPage->aData;
72491 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
72492 rc = allocateSpace(pPage, nByte: sz, pIdx: &idx);
72493 if( rc ){ *pRC = rc; return; }
72494 /* The allocateSpace() routine guarantees the following properties
72495 ** if it returns successfully */
72496 assert( idx >= 0 );
72497 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
72498 assert( idx+sz <= (int)pPage->pBt->usableSize );
72499 pPage->nFree -= (u16)(2 + sz);
72500 if( iChild ){
72501 /* In a corrupt database where an entry in the cell index section of
72502 ** a btree page has a value of 3 or less, the pCell value might point
72503 ** as many as 4 bytes in front of the start of the aData buffer for
72504 ** the source page. Make sure this does not cause problems by not
72505 ** reading the first 4 bytes */
72506 memcpy(dest: &data[idx+4], src: pCell+4, n: sz-4);
72507 put4byte(p: &data[idx], v: iChild);
72508 }else{
72509 memcpy(dest: &data[idx], src: pCell, n: sz);
72510 }
72511 pIns = pPage->aCellIdx + i*2;
72512 memmove(dest: pIns+2, src: pIns, n: 2*(pPage->nCell - i));
72513 put2byte(pIns, idx);
72514 pPage->nCell++;
72515 /* increment the cell count */
72516 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
72517 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
72518#ifndef SQLITE_OMIT_AUTOVACUUM
72519 if( pPage->pBt->autoVacuum ){
72520 /* The cell may contain a pointer to an overflow page. If so, write
72521 ** the entry for the overflow page into the pointer map.
72522 */
72523 ptrmapPutOvflPtr(pPage, pSrc: pPage, pCell, pRC);
72524 }
72525#endif
72526 }
72527}
72528
72529/*
72530** The following parameters determine how many adjacent pages get involved
72531** in a balancing operation. NN is the number of neighbors on either side
72532** of the page that participate in the balancing operation. NB is the
72533** total number of pages that participate, including the target page and
72534** NN neighbors on either side.
72535**
72536** The minimum value of NN is 1 (of course). Increasing NN above 1
72537** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
72538** in exchange for a larger degradation in INSERT and UPDATE performance.
72539** The value of NN appears to give the best results overall.
72540**
72541** (Later:) The description above makes it seem as if these values are
72542** tunable - as if you could change them and recompile and it would all work.
72543** But that is unlikely. NB has been 3 since the inception of SQLite and
72544** we have never tested any other value.
72545*/
72546#define NN 1 /* Number of neighbors on either side of pPage */
72547#define NB 3 /* (NN*2+1): Total pages involved in the balance */
72548
72549/*
72550** A CellArray object contains a cache of pointers and sizes for a
72551** consecutive sequence of cells that might be held on multiple pages.
72552**
72553** The cells in this array are the divider cell or cells from the pParent
72554** page plus up to three child pages. There are a total of nCell cells.
72555**
72556** pRef is a pointer to one of the pages that contributes cells. This is
72557** used to access information such as MemPage.intKey and MemPage.pBt->pageSize
72558** which should be common to all pages that contribute cells to this array.
72559**
72560** apCell[] and szCell[] hold, respectively, pointers to the start of each
72561** cell and the size of each cell. Some of the apCell[] pointers might refer
72562** to overflow cells. In other words, some apCel[] pointers might not point
72563** to content area of the pages.
72564**
72565** A szCell[] of zero means the size of that cell has not yet been computed.
72566**
72567** The cells come from as many as four different pages:
72568**
72569** -----------
72570** | Parent |
72571** -----------
72572** / | \
72573** / | \
72574** --------- --------- ---------
72575** |Child-1| |Child-2| |Child-3|
72576** --------- --------- ---------
72577**
72578** The order of cells is in the array is for an index btree is:
72579**
72580** 1. All cells from Child-1 in order
72581** 2. The first divider cell from Parent
72582** 3. All cells from Child-2 in order
72583** 4. The second divider cell from Parent
72584** 5. All cells from Child-3 in order
72585**
72586** For a table-btree (with rowids) the items 2 and 4 are empty because
72587** content exists only in leaves and there are no divider cells.
72588**
72589** For an index btree, the apEnd[] array holds pointer to the end of page
72590** for Child-1, the Parent, Child-2, the Parent (again), and Child-3,
72591** respectively. The ixNx[] array holds the number of cells contained in
72592** each of these 5 stages, and all stages to the left. Hence:
72593**
72594** ixNx[0] = Number of cells in Child-1.
72595** ixNx[1] = Number of cells in Child-1 plus 1 for first divider.
72596** ixNx[2] = Number of cells in Child-1 and Child-2 + 1 for 1st divider.
72597** ixNx[3] = Number of cells in Child-1 and Child-2 + both divider cells
72598** ixNx[4] = Total number of cells.
72599**
72600** For a table-btree, the concept is similar, except only apEnd[0]..apEnd[2]
72601** are used and they point to the leaf pages only, and the ixNx value are:
72602**
72603** ixNx[0] = Number of cells in Child-1.
72604** ixNx[1] = Number of cells in Child-1 and Child-2.
72605** ixNx[2] = Total number of cells.
72606**
72607** Sometimes when deleting, a child page can have zero cells. In those
72608** cases, ixNx[] entries with higher indexes, and the corresponding apEnd[]
72609** entries, shift down. The end result is that each ixNx[] entry should
72610** be larger than the previous
72611*/
72612typedef struct CellArray CellArray;
72613struct CellArray {
72614 int nCell; /* Number of cells in apCell[] */
72615 MemPage *pRef; /* Reference page */
72616 u8 **apCell; /* All cells begin balanced */
72617 u16 *szCell; /* Local size of all cells in apCell[] */
72618 u8 *apEnd[NB*2]; /* MemPage.aDataEnd values */
72619 int ixNx[NB*2]; /* Index of at which we move to the next apEnd[] */
72620};
72621
72622/*
72623** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
72624** computed.
72625*/
72626static void populateCellCache(CellArray *p, int idx, int N){
72627 assert( idx>=0 && idx+N<=p->nCell );
72628 while( N>0 ){
72629 assert( p->apCell[idx]!=0 );
72630 if( p->szCell[idx]==0 ){
72631 p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]);
72632 }else{
72633 assert( CORRUPT_DB ||
72634 p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) );
72635 }
72636 idx++;
72637 N--;
72638 }
72639}
72640
72641/*
72642** Return the size of the Nth element of the cell array
72643*/
72644static SQLITE_NOINLINE u16 computeCellSize(CellArray *p, int N){
72645 assert( N>=0 && N<p->nCell );
72646 assert( p->szCell[N]==0 );
72647 p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
72648 return p->szCell[N];
72649}
72650static u16 cachedCellSize(CellArray *p, int N){
72651 assert( N>=0 && N<p->nCell );
72652 if( p->szCell[N] ) return p->szCell[N];
72653 return computeCellSize(p, N);
72654}
72655
72656/*
72657** Array apCell[] contains pointers to nCell b-tree page cells. The
72658** szCell[] array contains the size in bytes of each cell. This function
72659** replaces the current contents of page pPg with the contents of the cell
72660** array.
72661**
72662** Some of the cells in apCell[] may currently be stored in pPg. This
72663** function works around problems caused by this by making a copy of any
72664** such cells before overwriting the page data.
72665**
72666** The MemPage.nFree field is invalidated by this function. It is the
72667** responsibility of the caller to set it correctly.
72668*/
72669static int rebuildPage(
72670 CellArray *pCArray, /* Content to be added to page pPg */
72671 int iFirst, /* First cell in pCArray to use */
72672 int nCell, /* Final number of cells on page */
72673 MemPage *pPg /* The page to be reconstructed */
72674){
72675 const int hdr = pPg->hdrOffset; /* Offset of header on pPg */
72676 u8 * const aData = pPg->aData; /* Pointer to data for pPg */
72677 const int usableSize = pPg->pBt->usableSize;
72678 u8 * const pEnd = &aData[usableSize];
72679 int i = iFirst; /* Which cell to copy from pCArray*/
72680 u32 j; /* Start of cell content area */
72681 int iEnd = i+nCell; /* Loop terminator */
72682 u8 *pCellptr = pPg->aCellIdx;
72683 u8 *pTmp = sqlite3PagerTempSpace(pPager: pPg->pBt->pPager);
72684 u8 *pData;
72685 int k; /* Current slot in pCArray->apEnd[] */
72686 u8 *pSrcEnd; /* Current pCArray->apEnd[k] value */
72687
72688 assert( i<iEnd );
72689 j = get2byte(&aData[hdr+5]);
72690 if( j>(u32)usableSize ){ j = 0; }
72691 memcpy(dest: &pTmp[j], src: &aData[j], n: usableSize - j);
72692
72693 for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2); k++){}
72694 pSrcEnd = pCArray->apEnd[k];
72695
72696 pData = pEnd;
72697 while( 1/*exit by break*/ ){
72698 u8 *pCell = pCArray->apCell[i];
72699 u16 sz = pCArray->szCell[i];
72700 assert( sz>0 );
72701 if( SQLITE_WITHIN(pCell,aData+j,pEnd) ){
72702 if( ((uptr)(pCell+sz))>(uptr)pEnd ) return SQLITE_CORRUPT_BKPT;
72703 pCell = &pTmp[pCell - aData];
72704 }else if( (uptr)(pCell+sz)>(uptr)pSrcEnd
72705 && (uptr)(pCell)<(uptr)pSrcEnd
72706 ){
72707 return SQLITE_CORRUPT_BKPT;
72708 }
72709
72710 pData -= sz;
72711 put2byte(pCellptr, (pData - aData));
72712 pCellptr += 2;
72713 if( pData < pCellptr ) return SQLITE_CORRUPT_BKPT;
72714 memmove(dest: pData, src: pCell, n: sz);
72715 assert( sz==pPg->xCellSize(pPg, pCell) || CORRUPT_DB );
72716 i++;
72717 if( i>=iEnd ) break;
72718 if( pCArray->ixNx[k]<=i ){
72719 k++;
72720 pSrcEnd = pCArray->apEnd[k];
72721 }
72722 }
72723
72724 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
72725 pPg->nCell = nCell;
72726 pPg->nOverflow = 0;
72727
72728 put2byte(&aData[hdr+1], 0);
72729 put2byte(&aData[hdr+3], pPg->nCell);
72730 put2byte(&aData[hdr+5], pData - aData);
72731 aData[hdr+7] = 0x00;
72732 return SQLITE_OK;
72733}
72734
72735/*
72736** The pCArray objects contains pointers to b-tree cells and the cell sizes.
72737** This function attempts to add the cells stored in the array to page pPg.
72738** If it cannot (because the page needs to be defragmented before the cells
72739** will fit), non-zero is returned. Otherwise, if the cells are added
72740** successfully, zero is returned.
72741**
72742** Argument pCellptr points to the first entry in the cell-pointer array
72743** (part of page pPg) to populate. After cell apCell[0] is written to the
72744** page body, a 16-bit offset is written to pCellptr. And so on, for each
72745** cell in the array. It is the responsibility of the caller to ensure
72746** that it is safe to overwrite this part of the cell-pointer array.
72747**
72748** When this function is called, *ppData points to the start of the
72749** content area on page pPg. If the size of the content area is extended,
72750** *ppData is updated to point to the new start of the content area
72751** before returning.
72752**
72753** Finally, argument pBegin points to the byte immediately following the
72754** end of the space required by this page for the cell-pointer area (for
72755** all cells - not just those inserted by the current call). If the content
72756** area must be extended to before this point in order to accomodate all
72757** cells in apCell[], then the cells do not fit and non-zero is returned.
72758*/
72759static int pageInsertArray(
72760 MemPage *pPg, /* Page to add cells to */
72761 u8 *pBegin, /* End of cell-pointer array */
72762 u8 **ppData, /* IN/OUT: Page content-area pointer */
72763 u8 *pCellptr, /* Pointer to cell-pointer area */
72764 int iFirst, /* Index of first cell to add */
72765 int nCell, /* Number of cells to add to pPg */
72766 CellArray *pCArray /* Array of cells */
72767){
72768 int i = iFirst; /* Loop counter - cell index to insert */
72769 u8 *aData = pPg->aData; /* Complete page */
72770 u8 *pData = *ppData; /* Content area. A subset of aData[] */
72771 int iEnd = iFirst + nCell; /* End of loop. One past last cell to ins */
72772 int k; /* Current slot in pCArray->apEnd[] */
72773 u8 *pEnd; /* Maximum extent of cell data */
72774 assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */
72775 if( iEnd<=iFirst ) return 0;
72776 for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2); k++){}
72777 pEnd = pCArray->apEnd[k];
72778 while( 1 /*Exit by break*/ ){
72779 int sz, rc;
72780 u8 *pSlot;
72781 assert( pCArray->szCell[i]!=0 );
72782 sz = pCArray->szCell[i];
72783 if( (aData[1]==0 && aData[2]==0) || (pSlot = pageFindSlot(pPg,nByte: sz,pRc: &rc))==0 ){
72784 if( (pData - pBegin)<sz ) return 1;
72785 pData -= sz;
72786 pSlot = pData;
72787 }
72788 /* pSlot and pCArray->apCell[i] will never overlap on a well-formed
72789 ** database. But they might for a corrupt database. Hence use memmove()
72790 ** since memcpy() sends SIGABORT with overlapping buffers on OpenBSD */
72791 assert( (pSlot+sz)<=pCArray->apCell[i]
72792 || pSlot>=(pCArray->apCell[i]+sz)
72793 || CORRUPT_DB );
72794 if( (uptr)(pCArray->apCell[i]+sz)>(uptr)pEnd
72795 && (uptr)(pCArray->apCell[i])<(uptr)pEnd
72796 ){
72797 assert( CORRUPT_DB );
72798 (void)SQLITE_CORRUPT_BKPT;
72799 return 1;
72800 }
72801 memmove(dest: pSlot, src: pCArray->apCell[i], n: sz);
72802 put2byte(pCellptr, (pSlot - aData));
72803 pCellptr += 2;
72804 i++;
72805 if( i>=iEnd ) break;
72806 if( pCArray->ixNx[k]<=i ){
72807 k++;
72808 pEnd = pCArray->apEnd[k];
72809 }
72810 }
72811 *ppData = pData;
72812 return 0;
72813}
72814
72815/*
72816** The pCArray object contains pointers to b-tree cells and their sizes.
72817**
72818** This function adds the space associated with each cell in the array
72819** that is currently stored within the body of pPg to the pPg free-list.
72820** The cell-pointers and other fields of the page are not updated.
72821**
72822** This function returns the total number of cells added to the free-list.
72823*/
72824static int pageFreeArray(
72825 MemPage *pPg, /* Page to edit */
72826 int iFirst, /* First cell to delete */
72827 int nCell, /* Cells to delete */
72828 CellArray *pCArray /* Array of cells */
72829){
72830 u8 * const aData = pPg->aData;
72831 u8 * const pEnd = &aData[pPg->pBt->usableSize];
72832 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
72833 int nRet = 0;
72834 int i;
72835 int iEnd = iFirst + nCell;
72836 u8 *pFree = 0;
72837 int szFree = 0;
72838
72839 for(i=iFirst; i<iEnd; i++){
72840 u8 *pCell = pCArray->apCell[i];
72841 if( SQLITE_WITHIN(pCell, pStart, pEnd) ){
72842 int sz;
72843 /* No need to use cachedCellSize() here. The sizes of all cells that
72844 ** are to be freed have already been computing while deciding which
72845 ** cells need freeing */
72846 sz = pCArray->szCell[i]; assert( sz>0 );
72847 if( pFree!=(pCell + sz) ){
72848 if( pFree ){
72849 assert( pFree>aData && (pFree - aData)<65536 );
72850 freeSpace(pPage: pPg, iStart: (u16)(pFree - aData), iSize: szFree);
72851 }
72852 pFree = pCell;
72853 szFree = sz;
72854 if( pFree+sz>pEnd ){
72855 return 0;
72856 }
72857 }else{
72858 pFree = pCell;
72859 szFree += sz;
72860 }
72861 nRet++;
72862 }
72863 }
72864 if( pFree ){
72865 assert( pFree>aData && (pFree - aData)<65536 );
72866 freeSpace(pPage: pPg, iStart: (u16)(pFree - aData), iSize: szFree);
72867 }
72868 return nRet;
72869}
72870
72871/*
72872** pCArray contains pointers to and sizes of all cells in the page being
72873** balanced. The current page, pPg, has pPg->nCell cells starting with
72874** pCArray->apCell[iOld]. After balancing, this page should hold nNew cells
72875** starting at apCell[iNew].
72876**
72877** This routine makes the necessary adjustments to pPg so that it contains
72878** the correct cells after being balanced.
72879**
72880** The pPg->nFree field is invalid when this function returns. It is the
72881** responsibility of the caller to set it correctly.
72882*/
72883static int editPage(
72884 MemPage *pPg, /* Edit this page */
72885 int iOld, /* Index of first cell currently on page */
72886 int iNew, /* Index of new first cell on page */
72887 int nNew, /* Final number of cells on page */
72888 CellArray *pCArray /* Array of cells and sizes */
72889){
72890 u8 * const aData = pPg->aData;
72891 const int hdr = pPg->hdrOffset;
72892 u8 *pBegin = &pPg->aCellIdx[nNew * 2];
72893 int nCell = pPg->nCell; /* Cells stored on pPg */
72894 u8 *pData;
72895 u8 *pCellptr;
72896 int i;
72897 int iOldEnd = iOld + pPg->nCell + pPg->nOverflow;
72898 int iNewEnd = iNew + nNew;
72899
72900#ifdef SQLITE_DEBUG
72901 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
72902 memcpy(pTmp, aData, pPg->pBt->usableSize);
72903#endif
72904
72905 /* Remove cells from the start and end of the page */
72906 assert( nCell>=0 );
72907 if( iOld<iNew ){
72908 int nShift = pageFreeArray(pPg, iFirst: iOld, nCell: iNew-iOld, pCArray);
72909 if( NEVER(nShift>nCell) ) return SQLITE_CORRUPT_BKPT;
72910 memmove(dest: pPg->aCellIdx, src: &pPg->aCellIdx[nShift*2], n: nCell*2);
72911 nCell -= nShift;
72912 }
72913 if( iNewEnd < iOldEnd ){
72914 int nTail = pageFreeArray(pPg, iFirst: iNewEnd, nCell: iOldEnd - iNewEnd, pCArray);
72915 assert( nCell>=nTail );
72916 nCell -= nTail;
72917 }
72918
72919 pData = &aData[get2byteNotZero(&aData[hdr+5])];
72920 if( pData<pBegin ) goto editpage_fail;
72921 if( pData>pPg->aDataEnd ) goto editpage_fail;
72922
72923 /* Add cells to the start of the page */
72924 if( iNew<iOld ){
72925 int nAdd = MIN(nNew,iOld-iNew);
72926 assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB );
72927 assert( nAdd>=0 );
72928 pCellptr = pPg->aCellIdx;
72929 memmove(dest: &pCellptr[nAdd*2], src: pCellptr, n: nCell*2);
72930 if( pageInsertArray(
72931 pPg, pBegin, ppData: &pData, pCellptr,
72932 iFirst: iNew, nCell: nAdd, pCArray
72933 ) ) goto editpage_fail;
72934 nCell += nAdd;
72935 }
72936
72937 /* Add any overflow cells */
72938 for(i=0; i<pPg->nOverflow; i++){
72939 int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
72940 if( iCell>=0 && iCell<nNew ){
72941 pCellptr = &pPg->aCellIdx[iCell * 2];
72942 if( nCell>iCell ){
72943 memmove(dest: &pCellptr[2], src: pCellptr, n: (nCell - iCell) * 2);
72944 }
72945 nCell++;
72946 cachedCellSize(p: pCArray, N: iCell+iNew);
72947 if( pageInsertArray(
72948 pPg, pBegin, ppData: &pData, pCellptr,
72949 iFirst: iCell+iNew, nCell: 1, pCArray
72950 ) ) goto editpage_fail;
72951 }
72952 }
72953
72954 /* Append cells to the end of the page */
72955 assert( nCell>=0 );
72956 pCellptr = &pPg->aCellIdx[nCell*2];
72957 if( pageInsertArray(
72958 pPg, pBegin, ppData: &pData, pCellptr,
72959 iFirst: iNew+nCell, nCell: nNew-nCell, pCArray
72960 ) ) goto editpage_fail;
72961
72962 pPg->nCell = nNew;
72963 pPg->nOverflow = 0;
72964
72965 put2byte(&aData[hdr+3], pPg->nCell);
72966 put2byte(&aData[hdr+5], pData - aData);
72967
72968#ifdef SQLITE_DEBUG
72969 for(i=0; i<nNew && !CORRUPT_DB; i++){
72970 u8 *pCell = pCArray->apCell[i+iNew];
72971 int iOff = get2byteAligned(&pPg->aCellIdx[i*2]);
72972 if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize]) ){
72973 pCell = &pTmp[pCell - aData];
72974 }
72975 assert( 0==memcmp(pCell, &aData[iOff],
72976 pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) );
72977 }
72978#endif
72979
72980 return SQLITE_OK;
72981 editpage_fail:
72982 /* Unable to edit this page. Rebuild it from scratch instead. */
72983 populateCellCache(p: pCArray, idx: iNew, N: nNew);
72984 return rebuildPage(pCArray, iFirst: iNew, nCell: nNew, pPg);
72985}
72986
72987
72988#ifndef SQLITE_OMIT_QUICKBALANCE
72989/*
72990** This version of balance() handles the common special case where
72991** a new entry is being inserted on the extreme right-end of the
72992** tree, in other words, when the new entry will become the largest
72993** entry in the tree.
72994**
72995** Instead of trying to balance the 3 right-most leaf pages, just add
72996** a new page to the right-hand side and put the one new entry in
72997** that page. This leaves the right side of the tree somewhat
72998** unbalanced. But odds are that we will be inserting new entries
72999** at the end soon afterwards so the nearly empty page will quickly
73000** fill up. On average.
73001**
73002** pPage is the leaf page which is the right-most page in the tree.
73003** pParent is its parent. pPage must have a single overflow entry
73004** which is also the right-most entry on the page.
73005**
73006** The pSpace buffer is used to store a temporary copy of the divider
73007** cell that will be inserted into pParent. Such a cell consists of a 4
73008** byte page number followed by a variable length integer. In other
73009** words, at most 13 bytes. Hence the pSpace buffer must be at
73010** least 13 bytes in size.
73011*/
73012static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
73013 BtShared *const pBt = pPage->pBt; /* B-Tree Database */
73014 MemPage *pNew; /* Newly allocated page */
73015 int rc; /* Return Code */
73016 Pgno pgnoNew; /* Page number of pNew */
73017
73018 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
73019 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
73020 assert( pPage->nOverflow==1 );
73021
73022 if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT; /* dbfuzz001.test */
73023 assert( pPage->nFree>=0 );
73024 assert( pParent->nFree>=0 );
73025
73026 /* Allocate a new page. This page will become the right-sibling of
73027 ** pPage. Make the parent page writable, so that the new divider cell
73028 ** may be inserted. If both these operations are successful, proceed.
73029 */
73030 rc = allocateBtreePage(pBt, ppPage: &pNew, pPgno: &pgnoNew, nearby: 0, eMode: 0);
73031
73032 if( rc==SQLITE_OK ){
73033
73034 u8 *pOut = &pSpace[4];
73035 u8 *pCell = pPage->apOvfl[0];
73036 u16 szCell = pPage->xCellSize(pPage, pCell);
73037 u8 *pStop;
73038 CellArray b;
73039
73040 assert( sqlite3PagerIswriteable(pNew->pDbPage) );
73041 assert( CORRUPT_DB || pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
73042 zeroPage(pPage: pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
73043 b.nCell = 1;
73044 b.pRef = pPage;
73045 b.apCell = &pCell;
73046 b.szCell = &szCell;
73047 b.apEnd[0] = pPage->aDataEnd;
73048 b.ixNx[0] = 2;
73049 rc = rebuildPage(pCArray: &b, iFirst: 0, nCell: 1, pPg: pNew);
73050 if( NEVER(rc) ){
73051 releasePage(pPage: pNew);
73052 return rc;
73053 }
73054 pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
73055
73056 /* If this is an auto-vacuum database, update the pointer map
73057 ** with entries for the new page, and any pointer from the
73058 ** cell on the page to an overflow page. If either of these
73059 ** operations fails, the return code is set, but the contents
73060 ** of the parent page are still manipulated by thh code below.
73061 ** That is Ok, at this point the parent page is guaranteed to
73062 ** be marked as dirty. Returning an error code will cause a
73063 ** rollback, undoing any changes made to the parent page.
73064 */
73065 if( ISAUTOVACUUM ){
73066 ptrmapPut(pBt, key: pgnoNew, PTRMAP_BTREE, parent: pParent->pgno, pRC: &rc);
73067 if( szCell>pNew->minLocal ){
73068 ptrmapPutOvflPtr(pPage: pNew, pSrc: pNew, pCell, pRC: &rc);
73069 }
73070 }
73071
73072 /* Create a divider cell to insert into pParent. The divider cell
73073 ** consists of a 4-byte page number (the page number of pPage) and
73074 ** a variable length key value (which must be the same value as the
73075 ** largest key on pPage).
73076 **
73077 ** To find the largest key value on pPage, first find the right-most
73078 ** cell on pPage. The first two fields of this cell are the
73079 ** record-length (a variable length integer at most 32-bits in size)
73080 ** and the key value (a variable length integer, may have any value).
73081 ** The first of the while(...) loops below skips over the record-length
73082 ** field. The second while(...) loop copies the key value from the
73083 ** cell on pPage into the pSpace buffer.
73084 */
73085 pCell = findCell(pPage, pPage->nCell-1);
73086 pStop = &pCell[9];
73087 while( (*(pCell++)&0x80) && pCell<pStop );
73088 pStop = &pCell[9];
73089 while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
73090
73091 /* Insert the new divider cell into pParent. */
73092 if( rc==SQLITE_OK ){
73093 insertCell(pPage: pParent, i: pParent->nCell, pCell: pSpace, sz: (int)(pOut-pSpace),
73094 pTemp: 0, iChild: pPage->pgno, pRC: &rc);
73095 }
73096
73097 /* Set the right-child pointer of pParent to point to the new page. */
73098 put4byte(p: &pParent->aData[pParent->hdrOffset+8], v: pgnoNew);
73099
73100 /* Release the reference to the new page. */
73101 releasePage(pPage: pNew);
73102 }
73103
73104 return rc;
73105}
73106#endif /* SQLITE_OMIT_QUICKBALANCE */
73107
73108#if 0
73109/*
73110** This function does not contribute anything to the operation of SQLite.
73111** it is sometimes activated temporarily while debugging code responsible
73112** for setting pointer-map entries.
73113*/
73114static int ptrmapCheckPages(MemPage **apPage, int nPage){
73115 int i, j;
73116 for(i=0; i<nPage; i++){
73117 Pgno n;
73118 u8 e;
73119 MemPage *pPage = apPage[i];
73120 BtShared *pBt = pPage->pBt;
73121 assert( pPage->isInit );
73122
73123 for(j=0; j<pPage->nCell; j++){
73124 CellInfo info;
73125 u8 *z;
73126
73127 z = findCell(pPage, j);
73128 pPage->xParseCell(pPage, z, &info);
73129 if( info.nLocal<info.nPayload ){
73130 Pgno ovfl = get4byte(&z[info.nSize-4]);
73131 ptrmapGet(pBt, ovfl, &e, &n);
73132 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
73133 }
73134 if( !pPage->leaf ){
73135 Pgno child = get4byte(z);
73136 ptrmapGet(pBt, child, &e, &n);
73137 assert( n==pPage->pgno && e==PTRMAP_BTREE );
73138 }
73139 }
73140 if( !pPage->leaf ){
73141 Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
73142 ptrmapGet(pBt, child, &e, &n);
73143 assert( n==pPage->pgno && e==PTRMAP_BTREE );
73144 }
73145 }
73146 return 1;
73147}
73148#endif
73149
73150/*
73151** This function is used to copy the contents of the b-tree node stored
73152** on page pFrom to page pTo. If page pFrom was not a leaf page, then
73153** the pointer-map entries for each child page are updated so that the
73154** parent page stored in the pointer map is page pTo. If pFrom contained
73155** any cells with overflow page pointers, then the corresponding pointer
73156** map entries are also updated so that the parent page is page pTo.
73157**
73158** If pFrom is currently carrying any overflow cells (entries in the
73159** MemPage.apOvfl[] array), they are not copied to pTo.
73160**
73161** Before returning, page pTo is reinitialized using btreeInitPage().
73162**
73163** The performance of this function is not critical. It is only used by
73164** the balance_shallower() and balance_deeper() procedures, neither of
73165** which are called often under normal circumstances.
73166*/
73167static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){
73168 if( (*pRC)==SQLITE_OK ){
73169 BtShared * const pBt = pFrom->pBt;
73170 u8 * const aFrom = pFrom->aData;
73171 u8 * const aTo = pTo->aData;
73172 int const iFromHdr = pFrom->hdrOffset;
73173 int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
73174 int rc;
73175 int iData;
73176
73177
73178 assert( pFrom->isInit );
73179 assert( pFrom->nFree>=iToHdr );
73180 assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
73181
73182 /* Copy the b-tree node content from page pFrom to page pTo. */
73183 iData = get2byte(&aFrom[iFromHdr+5]);
73184 memcpy(dest: &aTo[iData], src: &aFrom[iData], n: pBt->usableSize-iData);
73185 memcpy(dest: &aTo[iToHdr], src: &aFrom[iFromHdr], n: pFrom->cellOffset + 2*pFrom->nCell);
73186
73187 /* Reinitialize page pTo so that the contents of the MemPage structure
73188 ** match the new data. The initialization of pTo can actually fail under
73189 ** fairly obscure circumstances, even though it is a copy of initialized
73190 ** page pFrom.
73191 */
73192 pTo->isInit = 0;
73193 rc = btreeInitPage(pPage: pTo);
73194 if( rc==SQLITE_OK ) rc = btreeComputeFreeSpace(pPage: pTo);
73195 if( rc!=SQLITE_OK ){
73196 *pRC = rc;
73197 return;
73198 }
73199
73200 /* If this is an auto-vacuum database, update the pointer-map entries
73201 ** for any b-tree or overflow pages that pTo now contains the pointers to.
73202 */
73203 if( ISAUTOVACUUM ){
73204 *pRC = setChildPtrmaps(pTo);
73205 }
73206 }
73207}
73208
73209/*
73210** This routine redistributes cells on the iParentIdx'th child of pParent
73211** (hereafter "the page") and up to 2 siblings so that all pages have about the
73212** same amount of free space. Usually a single sibling on either side of the
73213** page are used in the balancing, though both siblings might come from one
73214** side if the page is the first or last child of its parent. If the page
73215** has fewer than 2 siblings (something which can only happen if the page
73216** is a root page or a child of a root page) then all available siblings
73217** participate in the balancing.
73218**
73219** The number of siblings of the page might be increased or decreased by
73220** one or two in an effort to keep pages nearly full but not over full.
73221**
73222** Note that when this routine is called, some of the cells on the page
73223** might not actually be stored in MemPage.aData[]. This can happen
73224** if the page is overfull. This routine ensures that all cells allocated
73225** to the page and its siblings fit into MemPage.aData[] before returning.
73226**
73227** In the course of balancing the page and its siblings, cells may be
73228** inserted into or removed from the parent page (pParent). Doing so
73229** may cause the parent page to become overfull or underfull. If this
73230** happens, it is the responsibility of the caller to invoke the correct
73231** balancing routine to fix this problem (see the balance() routine).
73232**
73233** If this routine fails for any reason, it might leave the database
73234** in a corrupted state. So if this routine fails, the database should
73235** be rolled back.
73236**
73237** The third argument to this function, aOvflSpace, is a pointer to a
73238** buffer big enough to hold one page. If while inserting cells into the parent
73239** page (pParent) the parent page becomes overfull, this buffer is
73240** used to store the parent's overflow cells. Because this function inserts
73241** a maximum of four divider cells into the parent page, and the maximum
73242** size of a cell stored within an internal node is always less than 1/4
73243** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
73244** enough for all overflow cells.
73245**
73246** If aOvflSpace is set to a null pointer, this function returns
73247** SQLITE_NOMEM.
73248*/
73249static int balance_nonroot(
73250 MemPage *pParent, /* Parent page of siblings being balanced */
73251 int iParentIdx, /* Index of "the page" in pParent */
73252 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
73253 int isRoot, /* True if pParent is a root-page */
73254 int bBulk /* True if this call is part of a bulk load */
73255){
73256 BtShared *pBt; /* The whole database */
73257 int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
73258 int nNew = 0; /* Number of pages in apNew[] */
73259 int nOld; /* Number of pages in apOld[] */
73260 int i, j, k; /* Loop counters */
73261 int nxDiv; /* Next divider slot in pParent->aCell[] */
73262 int rc = SQLITE_OK; /* The return code */
73263 u16 leafCorrection; /* 4 if pPage is a leaf. 0 if not */
73264 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
73265 int usableSpace; /* Bytes in pPage beyond the header */
73266 int pageFlags; /* Value of pPage->aData[0] */
73267 int iSpace1 = 0; /* First unused byte of aSpace1[] */
73268 int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */
73269 int szScratch; /* Size of scratch memory requested */
73270 MemPage *apOld[NB]; /* pPage and up to two siblings */
73271 MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
73272 u8 *pRight; /* Location in parent of right-sibling pointer */
73273 u8 *apDiv[NB-1]; /* Divider cells in pParent */
73274 int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */
73275 int cntOld[NB+2]; /* Old index in b.apCell[] */
73276 int szNew[NB+2]; /* Combined size of cells placed on i-th page */
73277 u8 *aSpace1; /* Space for copies of dividers cells */
73278 Pgno pgno; /* Temp var to store a page number in */
73279 u8 abDone[NB+2]; /* True after i'th new page is populated */
73280 Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */
73281 Pgno aPgOrder[NB+2]; /* Copy of aPgno[] used for sorting pages */
73282 u16 aPgFlags[NB+2]; /* flags field of new pages before shuffling */
73283 CellArray b; /* Parsed information on cells being balanced */
73284
73285 memset(s: abDone, c: 0, n: sizeof(abDone));
73286 memset(s: &b, c: 0, n: sizeof(b));
73287 pBt = pParent->pBt;
73288 assert( sqlite3_mutex_held(pBt->mutex) );
73289 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
73290
73291 /* At this point pParent may have at most one overflow cell. And if
73292 ** this overflow cell is present, it must be the cell with
73293 ** index iParentIdx. This scenario comes about when this function
73294 ** is called (indirectly) from sqlite3BtreeDelete().
73295 */
73296 assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
73297 assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
73298
73299 if( !aOvflSpace ){
73300 return SQLITE_NOMEM_BKPT;
73301 }
73302 assert( pParent->nFree>=0 );
73303
73304 /* Find the sibling pages to balance. Also locate the cells in pParent
73305 ** that divide the siblings. An attempt is made to find NN siblings on
73306 ** either side of pPage. More siblings are taken from one side, however,
73307 ** if there are fewer than NN siblings on the other side. If pParent
73308 ** has NB or fewer children then all children of pParent are taken.
73309 **
73310 ** This loop also drops the divider cells from the parent page. This
73311 ** way, the remainder of the function does not have to deal with any
73312 ** overflow cells in the parent page, since if any existed they will
73313 ** have already been removed.
73314 */
73315 i = pParent->nOverflow + pParent->nCell;
73316 if( i<2 ){
73317 nxDiv = 0;
73318 }else{
73319 assert( bBulk==0 || bBulk==1 );
73320 if( iParentIdx==0 ){
73321 nxDiv = 0;
73322 }else if( iParentIdx==i ){
73323 nxDiv = i-2+bBulk;
73324 }else{
73325 nxDiv = iParentIdx-1;
73326 }
73327 i = 2-bBulk;
73328 }
73329 nOld = i+1;
73330 if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
73331 pRight = &pParent->aData[pParent->hdrOffset+8];
73332 }else{
73333 pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
73334 }
73335 pgno = get4byte(p: pRight);
73336 while( 1 ){
73337 if( rc==SQLITE_OK ){
73338 rc = getAndInitPage(pBt, pgno, ppPage: &apOld[i], pCur: 0, bReadOnly: 0);
73339 }
73340 if( rc ){
73341 memset(s: apOld, c: 0, n: (i+1)*sizeof(MemPage*));
73342 goto balance_cleanup;
73343 }
73344 if( apOld[i]->nFree<0 ){
73345 rc = btreeComputeFreeSpace(pPage: apOld[i]);
73346 if( rc ){
73347 memset(s: apOld, c: 0, n: (i)*sizeof(MemPage*));
73348 goto balance_cleanup;
73349 }
73350 }
73351 nMaxCells += apOld[i]->nCell + ArraySize(pParent->apOvfl);
73352 if( (i--)==0 ) break;
73353
73354 if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
73355 apDiv[i] = pParent->apOvfl[0];
73356 pgno = get4byte(p: apDiv[i]);
73357 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
73358 pParent->nOverflow = 0;
73359 }else{
73360 apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
73361 pgno = get4byte(p: apDiv[i]);
73362 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
73363
73364 /* Drop the cell from the parent page. apDiv[i] still points to
73365 ** the cell within the parent, even though it has been dropped.
73366 ** This is safe because dropping a cell only overwrites the first
73367 ** four bytes of it, and this function does not need the first
73368 ** four bytes of the divider cell. So the pointer is safe to use
73369 ** later on.
73370 **
73371 ** But not if we are in secure-delete mode. In secure-delete mode,
73372 ** the dropCell() routine will overwrite the entire cell with zeroes.
73373 ** In this case, temporarily copy the cell into the aOvflSpace[]
73374 ** buffer. It will be copied out again as soon as the aSpace[] buffer
73375 ** is allocated. */
73376 if( pBt->btsFlags & BTS_FAST_SECURE ){
73377 int iOff;
73378
73379 /* If the following if() condition is not true, the db is corrupted.
73380 ** The call to dropCell() below will detect this. */
73381 iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
73382 if( (iOff+szNew[i])<=(int)pBt->usableSize ){
73383 memcpy(dest: &aOvflSpace[iOff], src: apDiv[i], n: szNew[i]);
73384 apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
73385 }
73386 }
73387 dropCell(pPage: pParent, idx: i+nxDiv-pParent->nOverflow, sz: szNew[i], pRC: &rc);
73388 }
73389 }
73390
73391 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
73392 ** alignment */
73393 nMaxCells = (nMaxCells + 3)&~3;
73394
73395 /*
73396 ** Allocate space for memory structures
73397 */
73398 szScratch =
73399 nMaxCells*sizeof(u8*) /* b.apCell */
73400 + nMaxCells*sizeof(u16) /* b.szCell */
73401 + pBt->pageSize; /* aSpace1 */
73402
73403 assert( szScratch<=7*(int)pBt->pageSize );
73404 b.apCell = sqlite3StackAllocRaw(0, szScratch );
73405 if( b.apCell==0 ){
73406 rc = SQLITE_NOMEM_BKPT;
73407 goto balance_cleanup;
73408 }
73409 b.szCell = (u16*)&b.apCell[nMaxCells];
73410 aSpace1 = (u8*)&b.szCell[nMaxCells];
73411 assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
73412
73413 /*
73414 ** Load pointers to all cells on sibling pages and the divider cells
73415 ** into the local b.apCell[] array. Make copies of the divider cells
73416 ** into space obtained from aSpace1[]. The divider cells have already
73417 ** been removed from pParent.
73418 **
73419 ** If the siblings are on leaf pages, then the child pointers of the
73420 ** divider cells are stripped from the cells before they are copied
73421 ** into aSpace1[]. In this way, all cells in b.apCell[] are without
73422 ** child pointers. If siblings are not leaves, then all cell in
73423 ** b.apCell[] include child pointers. Either way, all cells in b.apCell[]
73424 ** are alike.
73425 **
73426 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
73427 ** leafData: 1 if pPage holds key+data and pParent holds only keys.
73428 */
73429 b.pRef = apOld[0];
73430 leafCorrection = b.pRef->leaf*4;
73431 leafData = b.pRef->intKeyLeaf;
73432 for(i=0; i<nOld; i++){
73433 MemPage *pOld = apOld[i];
73434 int limit = pOld->nCell;
73435 u8 *aData = pOld->aData;
73436 u16 maskPage = pOld->maskPage;
73437 u8 *piCell = aData + pOld->cellOffset;
73438 u8 *piEnd;
73439 VVA_ONLY( int nCellAtStart = b.nCell; )
73440
73441 /* Verify that all sibling pages are of the same "type" (table-leaf,
73442 ** table-interior, index-leaf, or index-interior).
73443 */
73444 if( pOld->aData[0]!=apOld[0]->aData[0] ){
73445 rc = SQLITE_CORRUPT_BKPT;
73446 goto balance_cleanup;
73447 }
73448
73449 /* Load b.apCell[] with pointers to all cells in pOld. If pOld
73450 ** contains overflow cells, include them in the b.apCell[] array
73451 ** in the correct spot.
73452 **
73453 ** Note that when there are multiple overflow cells, it is always the
73454 ** case that they are sequential and adjacent. This invariant arises
73455 ** because multiple overflows can only occurs when inserting divider
73456 ** cells into a parent on a prior balance, and divider cells are always
73457 ** adjacent and are inserted in order. There is an assert() tagged
73458 ** with "NOTE 1" in the overflow cell insertion loop to prove this
73459 ** invariant.
73460 **
73461 ** This must be done in advance. Once the balance starts, the cell
73462 ** offset section of the btree page will be overwritten and we will no
73463 ** long be able to find the cells if a pointer to each cell is not saved
73464 ** first.
73465 */
73466 memset(s: &b.szCell[b.nCell], c: 0, n: sizeof(b.szCell[0])*(limit+pOld->nOverflow));
73467 if( pOld->nOverflow>0 ){
73468 if( NEVER(limit<pOld->aiOvfl[0]) ){
73469 rc = SQLITE_CORRUPT_BKPT;
73470 goto balance_cleanup;
73471 }
73472 limit = pOld->aiOvfl[0];
73473 for(j=0; j<limit; j++){
73474 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
73475 piCell += 2;
73476 b.nCell++;
73477 }
73478 for(k=0; k<pOld->nOverflow; k++){
73479 assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */
73480 b.apCell[b.nCell] = pOld->apOvfl[k];
73481 b.nCell++;
73482 }
73483 }
73484 piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
73485 while( piCell<piEnd ){
73486 assert( b.nCell<nMaxCells );
73487 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
73488 piCell += 2;
73489 b.nCell++;
73490 }
73491 assert( (b.nCell-nCellAtStart)==(pOld->nCell+pOld->nOverflow) );
73492
73493 cntOld[i] = b.nCell;
73494 if( i<nOld-1 && !leafData){
73495 u16 sz = (u16)szNew[i];
73496 u8 *pTemp;
73497 assert( b.nCell<nMaxCells );
73498 b.szCell[b.nCell] = sz;
73499 pTemp = &aSpace1[iSpace1];
73500 iSpace1 += sz;
73501 assert( sz<=pBt->maxLocal+23 );
73502 assert( iSpace1 <= (int)pBt->pageSize );
73503 memcpy(dest: pTemp, src: apDiv[i], n: sz);
73504 b.apCell[b.nCell] = pTemp+leafCorrection;
73505 assert( leafCorrection==0 || leafCorrection==4 );
73506 b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
73507 if( !pOld->leaf ){
73508 assert( leafCorrection==0 );
73509 assert( pOld->hdrOffset==0 || CORRUPT_DB );
73510 /* The right pointer of the child page pOld becomes the left
73511 ** pointer of the divider cell */
73512 memcpy(dest: b.apCell[b.nCell], src: &pOld->aData[8], n: 4);
73513 }else{
73514 assert( leafCorrection==4 );
73515 while( b.szCell[b.nCell]<4 ){
73516 /* Do not allow any cells smaller than 4 bytes. If a smaller cell
73517 ** does exist, pad it with 0x00 bytes. */
73518 assert( b.szCell[b.nCell]==3 || CORRUPT_DB );
73519 assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
73520 aSpace1[iSpace1++] = 0x00;
73521 b.szCell[b.nCell]++;
73522 }
73523 }
73524 b.nCell++;
73525 }
73526 }
73527
73528 /*
73529 ** Figure out the number of pages needed to hold all b.nCell cells.
73530 ** Store this number in "k". Also compute szNew[] which is the total
73531 ** size of all cells on the i-th page and cntNew[] which is the index
73532 ** in b.apCell[] of the cell that divides page i from page i+1.
73533 ** cntNew[k] should equal b.nCell.
73534 **
73535 ** Values computed by this block:
73536 **
73537 ** k: The total number of sibling pages
73538 ** szNew[i]: Spaced used on the i-th sibling page.
73539 ** cntNew[i]: Index in b.apCell[] and b.szCell[] for the first cell to
73540 ** the right of the i-th sibling page.
73541 ** usableSpace: Number of bytes of space available on each sibling.
73542 **
73543 */
73544 usableSpace = pBt->usableSize - 12 + leafCorrection;
73545 for(i=k=0; i<nOld; i++, k++){
73546 MemPage *p = apOld[i];
73547 b.apEnd[k] = p->aDataEnd;
73548 b.ixNx[k] = cntOld[i];
73549 if( k && b.ixNx[k]==b.ixNx[k-1] ){
73550 k--; /* Omit b.ixNx[] entry for child pages with no cells */
73551 }
73552 if( !leafData ){
73553 k++;
73554 b.apEnd[k] = pParent->aDataEnd;
73555 b.ixNx[k] = cntOld[i]+1;
73556 }
73557 assert( p->nFree>=0 );
73558 szNew[i] = usableSpace - p->nFree;
73559 for(j=0; j<p->nOverflow; j++){
73560 szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
73561 }
73562 cntNew[i] = cntOld[i];
73563 }
73564 k = nOld;
73565 for(i=0; i<k; i++){
73566 int sz;
73567 while( szNew[i]>usableSpace ){
73568 if( i+1>=k ){
73569 k = i+2;
73570 if( k>NB+2 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
73571 szNew[k-1] = 0;
73572 cntNew[k-1] = b.nCell;
73573 }
73574 sz = 2 + cachedCellSize(p: &b, N: cntNew[i]-1);
73575 szNew[i] -= sz;
73576 if( !leafData ){
73577 if( cntNew[i]<b.nCell ){
73578 sz = 2 + cachedCellSize(p: &b, N: cntNew[i]);
73579 }else{
73580 sz = 0;
73581 }
73582 }
73583 szNew[i+1] += sz;
73584 cntNew[i]--;
73585 }
73586 while( cntNew[i]<b.nCell ){
73587 sz = 2 + cachedCellSize(p: &b, N: cntNew[i]);
73588 if( szNew[i]+sz>usableSpace ) break;
73589 szNew[i] += sz;
73590 cntNew[i]++;
73591 if( !leafData ){
73592 if( cntNew[i]<b.nCell ){
73593 sz = 2 + cachedCellSize(p: &b, N: cntNew[i]);
73594 }else{
73595 sz = 0;
73596 }
73597 }
73598 szNew[i+1] -= sz;
73599 }
73600 if( cntNew[i]>=b.nCell ){
73601 k = i+1;
73602 }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
73603 rc = SQLITE_CORRUPT_BKPT;
73604 goto balance_cleanup;
73605 }
73606 }
73607
73608 /*
73609 ** The packing computed by the previous block is biased toward the siblings
73610 ** on the left side (siblings with smaller keys). The left siblings are
73611 ** always nearly full, while the right-most sibling might be nearly empty.
73612 ** The next block of code attempts to adjust the packing of siblings to
73613 ** get a better balance.
73614 **
73615 ** This adjustment is more than an optimization. The packing above might
73616 ** be so out of balance as to be illegal. For example, the right-most
73617 ** sibling might be completely empty. This adjustment is not optional.
73618 */
73619 for(i=k-1; i>0; i--){
73620 int szRight = szNew[i]; /* Size of sibling on the right */
73621 int szLeft = szNew[i-1]; /* Size of sibling on the left */
73622 int r; /* Index of right-most cell in left sibling */
73623 int d; /* Index of first cell to the left of right sibling */
73624
73625 r = cntNew[i-1] - 1;
73626 d = r + 1 - leafData;
73627 (void)cachedCellSize(p: &b, N: d);
73628 do{
73629 assert( d<nMaxCells );
73630 assert( r<nMaxCells );
73631 (void)cachedCellSize(p: &b, N: r);
73632 if( szRight!=0
73633 && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+(i==k-1?0:2)))){
73634 break;
73635 }
73636 szRight += b.szCell[d] + 2;
73637 szLeft -= b.szCell[r] + 2;
73638 cntNew[i-1] = r;
73639 r--;
73640 d--;
73641 }while( r>=0 );
73642 szNew[i] = szRight;
73643 szNew[i-1] = szLeft;
73644 if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
73645 rc = SQLITE_CORRUPT_BKPT;
73646 goto balance_cleanup;
73647 }
73648 }
73649
73650 /* Sanity check: For a non-corrupt database file one of the follwing
73651 ** must be true:
73652 ** (1) We found one or more cells (cntNew[0])>0), or
73653 ** (2) pPage is a virtual root page. A virtual root page is when
73654 ** the real root page is page 1 and we are the only child of
73655 ** that page.
73656 */
73657 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB);
73658 TRACE(("BALANCE: old: %d(nc=%d) %d(nc=%d) %d(nc=%d)\n",
73659 apOld[0]->pgno, apOld[0]->nCell,
73660 nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0,
73661 nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0
73662 ));
73663
73664 /*
73665 ** Allocate k new pages. Reuse old pages where possible.
73666 */
73667 pageFlags = apOld[0]->aData[0];
73668 for(i=0; i<k; i++){
73669 MemPage *pNew;
73670 if( i<nOld ){
73671 pNew = apNew[i] = apOld[i];
73672 apOld[i] = 0;
73673 rc = sqlite3PagerWrite(pPg: pNew->pDbPage);
73674 nNew++;
73675 if( sqlite3PagerPageRefcount(pPage: pNew->pDbPage)!=1+(i==(iParentIdx-nxDiv))
73676 && rc==SQLITE_OK
73677 ){
73678 rc = SQLITE_CORRUPT_BKPT;
73679 }
73680 if( rc ) goto balance_cleanup;
73681 }else{
73682 assert( i>0 );
73683 rc = allocateBtreePage(pBt, ppPage: &pNew, pPgno: &pgno, nearby: (bBulk ? 1 : pgno), eMode: 0);
73684 if( rc ) goto balance_cleanup;
73685 zeroPage(pPage: pNew, flags: pageFlags);
73686 apNew[i] = pNew;
73687 nNew++;
73688 cntOld[i] = b.nCell;
73689
73690 /* Set the pointer-map entry for the new sibling page. */
73691 if( ISAUTOVACUUM ){
73692 ptrmapPut(pBt, key: pNew->pgno, PTRMAP_BTREE, parent: pParent->pgno, pRC: &rc);
73693 if( rc!=SQLITE_OK ){
73694 goto balance_cleanup;
73695 }
73696 }
73697 }
73698 }
73699
73700 /*
73701 ** Reassign page numbers so that the new pages are in ascending order.
73702 ** This helps to keep entries in the disk file in order so that a scan
73703 ** of the table is closer to a linear scan through the file. That in turn
73704 ** helps the operating system to deliver pages from the disk more rapidly.
73705 **
73706 ** An O(n^2) insertion sort algorithm is used, but since n is never more
73707 ** than (NB+2) (a small constant), that should not be a problem.
73708 **
73709 ** When NB==3, this one optimization makes the database about 25% faster
73710 ** for large insertions and deletions.
73711 */
73712 for(i=0; i<nNew; i++){
73713 aPgOrder[i] = aPgno[i] = apNew[i]->pgno;
73714 aPgFlags[i] = apNew[i]->pDbPage->flags;
73715 for(j=0; j<i; j++){
73716 if( NEVER(aPgno[j]==aPgno[i]) ){
73717 /* This branch is taken if the set of sibling pages somehow contains
73718 ** duplicate entries. This can happen if the database is corrupt.
73719 ** It would be simpler to detect this as part of the loop below, but
73720 ** we do the detection here in order to avoid populating the pager
73721 ** cache with two separate objects associated with the same
73722 ** page number. */
73723 assert( CORRUPT_DB );
73724 rc = SQLITE_CORRUPT_BKPT;
73725 goto balance_cleanup;
73726 }
73727 }
73728 }
73729 for(i=0; i<nNew; i++){
73730 int iBest = 0; /* aPgno[] index of page number to use */
73731 for(j=1; j<nNew; j++){
73732 if( aPgOrder[j]<aPgOrder[iBest] ) iBest = j;
73733 }
73734 pgno = aPgOrder[iBest];
73735 aPgOrder[iBest] = 0xffffffff;
73736 if( iBest!=i ){
73737 if( iBest>i ){
73738 sqlite3PagerRekey(pPg: apNew[iBest]->pDbPage, iNew: pBt->nPage+iBest+1, flags: 0);
73739 }
73740 sqlite3PagerRekey(pPg: apNew[i]->pDbPage, iNew: pgno, flags: aPgFlags[iBest]);
73741 apNew[i]->pgno = pgno;
73742 }
73743 }
73744
73745 TRACE(("BALANCE: new: %d(%d nc=%d) %d(%d nc=%d) %d(%d nc=%d) "
73746 "%d(%d nc=%d) %d(%d nc=%d)\n",
73747 apNew[0]->pgno, szNew[0], cntNew[0],
73748 nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
73749 nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0,
73750 nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
73751 nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0,
73752 nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
73753 nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0,
73754 nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0,
73755 nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0
73756 ));
73757
73758 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
73759 assert( nNew>=1 && nNew<=ArraySize(apNew) );
73760 assert( apNew[nNew-1]!=0 );
73761 put4byte(p: pRight, v: apNew[nNew-1]->pgno);
73762
73763 /* If the sibling pages are not leaves, ensure that the right-child pointer
73764 ** of the right-most new sibling page is set to the value that was
73765 ** originally in the same field of the right-most old sibling page. */
73766 if( (pageFlags & PTF_LEAF)==0 && nOld!=nNew ){
73767 MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1];
73768 memcpy(dest: &apNew[nNew-1]->aData[8], src: &pOld->aData[8], n: 4);
73769 }
73770
73771 /* Make any required updates to pointer map entries associated with
73772 ** cells stored on sibling pages following the balance operation. Pointer
73773 ** map entries associated with divider cells are set by the insertCell()
73774 ** routine. The associated pointer map entries are:
73775 **
73776 ** a) if the cell contains a reference to an overflow chain, the
73777 ** entry associated with the first page in the overflow chain, and
73778 **
73779 ** b) if the sibling pages are not leaves, the child page associated
73780 ** with the cell.
73781 **
73782 ** If the sibling pages are not leaves, then the pointer map entry
73783 ** associated with the right-child of each sibling may also need to be
73784 ** updated. This happens below, after the sibling pages have been
73785 ** populated, not here.
73786 */
73787 if( ISAUTOVACUUM ){
73788 MemPage *pOld;
73789 MemPage *pNew = pOld = apNew[0];
73790 int cntOldNext = pNew->nCell + pNew->nOverflow;
73791 int iNew = 0;
73792 int iOld = 0;
73793
73794 for(i=0; i<b.nCell; i++){
73795 u8 *pCell = b.apCell[i];
73796 while( i==cntOldNext ){
73797 iOld++;
73798 assert( iOld<nNew || iOld<nOld );
73799 assert( iOld>=0 && iOld<NB );
73800 pOld = iOld<nNew ? apNew[iOld] : apOld[iOld];
73801 cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
73802 }
73803 if( i==cntNew[iNew] ){
73804 pNew = apNew[++iNew];
73805 if( !leafData ) continue;
73806 }
73807
73808 /* Cell pCell is destined for new sibling page pNew. Originally, it
73809 ** was either part of sibling page iOld (possibly an overflow cell),
73810 ** or else the divider cell to the left of sibling page iOld. So,
73811 ** if sibling page iOld had the same page number as pNew, and if
73812 ** pCell really was a part of sibling page iOld (not a divider or
73813 ** overflow cell), we can skip updating the pointer map entries. */
73814 if( iOld>=nNew
73815 || pNew->pgno!=aPgno[iOld]
73816 || !SQLITE_WITHIN(pCell,pOld->aData,pOld->aDataEnd)
73817 ){
73818 if( !leafCorrection ){
73819 ptrmapPut(pBt, get4byte(p: pCell), PTRMAP_BTREE, parent: pNew->pgno, pRC: &rc);
73820 }
73821 if( cachedCellSize(p: &b,N: i)>pNew->minLocal ){
73822 ptrmapPutOvflPtr(pPage: pNew, pSrc: pOld, pCell, pRC: &rc);
73823 }
73824 if( rc ) goto balance_cleanup;
73825 }
73826 }
73827 }
73828
73829 /* Insert new divider cells into pParent. */
73830 for(i=0; i<nNew-1; i++){
73831 u8 *pCell;
73832 u8 *pTemp;
73833 int sz;
73834 u8 *pSrcEnd;
73835 MemPage *pNew = apNew[i];
73836 j = cntNew[i];
73837
73838 assert( j<nMaxCells );
73839 assert( b.apCell[j]!=0 );
73840 pCell = b.apCell[j];
73841 sz = b.szCell[j] + leafCorrection;
73842 pTemp = &aOvflSpace[iOvflSpace];
73843 if( !pNew->leaf ){
73844 memcpy(dest: &pNew->aData[8], src: pCell, n: 4);
73845 }else if( leafData ){
73846 /* If the tree is a leaf-data tree, and the siblings are leaves,
73847 ** then there is no divider cell in b.apCell[]. Instead, the divider
73848 ** cell consists of the integer key for the right-most cell of
73849 ** the sibling-page assembled above only.
73850 */
73851 CellInfo info;
73852 j--;
73853 pNew->xParseCell(pNew, b.apCell[j], &info);
73854 pCell = pTemp;
73855 sz = 4 + putVarint(p: &pCell[4], v: info.nKey);
73856 pTemp = 0;
73857 }else{
73858 pCell -= 4;
73859 /* Obscure case for non-leaf-data trees: If the cell at pCell was
73860 ** previously stored on a leaf node, and its reported size was 4
73861 ** bytes, then it may actually be smaller than this
73862 ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
73863 ** any cell). But it is important to pass the correct size to
73864 ** insertCell(), so reparse the cell now.
73865 **
73866 ** This can only happen for b-trees used to evaluate "IN (SELECT ...)"
73867 ** and WITHOUT ROWID tables with exactly one column which is the
73868 ** primary key.
73869 */
73870 if( b.szCell[j]==4 ){
73871 assert(leafCorrection==4);
73872 sz = pParent->xCellSize(pParent, pCell);
73873 }
73874 }
73875 iOvflSpace += sz;
73876 assert( sz<=pBt->maxLocal+23 );
73877 assert( iOvflSpace <= (int)pBt->pageSize );
73878 for(k=0; b.ixNx[k]<=i && ALWAYS(k<NB*2); k++){}
73879 pSrcEnd = b.apEnd[k];
73880 if( SQLITE_WITHIN(pSrcEnd, pCell, pCell+sz) ){
73881 rc = SQLITE_CORRUPT_BKPT;
73882 goto balance_cleanup;
73883 }
73884 insertCell(pPage: pParent, i: nxDiv+i, pCell, sz, pTemp, iChild: pNew->pgno, pRC: &rc);
73885 if( rc!=SQLITE_OK ) goto balance_cleanup;
73886 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
73887 }
73888
73889 /* Now update the actual sibling pages. The order in which they are updated
73890 ** is important, as this code needs to avoid disrupting any page from which
73891 ** cells may still to be read. In practice, this means:
73892 **
73893 ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1])
73894 ** then it is not safe to update page apNew[iPg] until after
73895 ** the left-hand sibling apNew[iPg-1] has been updated.
73896 **
73897 ** (2) If cells are moving right (from apNew[iPg] to apNew[iPg+1])
73898 ** then it is not safe to update page apNew[iPg] until after
73899 ** the right-hand sibling apNew[iPg+1] has been updated.
73900 **
73901 ** If neither of the above apply, the page is safe to update.
73902 **
73903 ** The iPg value in the following loop starts at nNew-1 goes down
73904 ** to 0, then back up to nNew-1 again, thus making two passes over
73905 ** the pages. On the initial downward pass, only condition (1) above
73906 ** needs to be tested because (2) will always be true from the previous
73907 ** step. On the upward pass, both conditions are always true, so the
73908 ** upwards pass simply processes pages that were missed on the downward
73909 ** pass.
73910 */
73911 for(i=1-nNew; i<nNew; i++){
73912 int iPg = i<0 ? -i : i;
73913 assert( iPg>=0 && iPg<nNew );
73914 if( abDone[iPg] ) continue; /* Skip pages already processed */
73915 if( i>=0 /* On the upwards pass, or... */
73916 || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */
73917 ){
73918 int iNew;
73919 int iOld;
73920 int nNewCell;
73921
73922 /* Verify condition (1): If cells are moving left, update iPg
73923 ** only after iPg-1 has already been updated. */
73924 assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] );
73925
73926 /* Verify condition (2): If cells are moving right, update iPg
73927 ** only after iPg+1 has already been updated. */
73928 assert( cntNew[iPg]>=cntOld[iPg] || abDone[iPg+1] );
73929
73930 if( iPg==0 ){
73931 iNew = iOld = 0;
73932 nNewCell = cntNew[0];
73933 }else{
73934 iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
73935 iNew = cntNew[iPg-1] + !leafData;
73936 nNewCell = cntNew[iPg] - iNew;
73937 }
73938
73939 rc = editPage(pPg: apNew[iPg], iOld, iNew, nNew: nNewCell, pCArray: &b);
73940 if( rc ) goto balance_cleanup;
73941 abDone[iPg]++;
73942 apNew[iPg]->nFree = usableSpace-szNew[iPg];
73943 assert( apNew[iPg]->nOverflow==0 );
73944 assert( apNew[iPg]->nCell==nNewCell );
73945 }
73946 }
73947
73948 /* All pages have been processed exactly once */
73949 assert( memcmp(abDone, "\01\01\01\01\01", nNew)==0 );
73950
73951 assert( nOld>0 );
73952 assert( nNew>0 );
73953
73954 if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
73955 /* The root page of the b-tree now contains no cells. The only sibling
73956 ** page is the right-child of the parent. Copy the contents of the
73957 ** child page into the parent, decreasing the overall height of the
73958 ** b-tree structure by one. This is described as the "balance-shallower"
73959 ** sub-algorithm in some documentation.
73960 **
73961 ** If this is an auto-vacuum database, the call to copyNodeContent()
73962 ** sets all pointer-map entries corresponding to database image pages
73963 ** for which the pointer is stored within the content being copied.
73964 **
73965 ** It is critical that the child page be defragmented before being
73966 ** copied into the parent, because if the parent is page 1 then it will
73967 ** by smaller than the child due to the database header, and so all the
73968 ** free space needs to be up front.
73969 */
73970 assert( nNew==1 || CORRUPT_DB );
73971 rc = defragmentPage(pPage: apNew[0], nMaxFrag: -1);
73972 testcase( rc!=SQLITE_OK );
73973 assert( apNew[0]->nFree ==
73974 (get2byteNotZero(&apNew[0]->aData[5]) - apNew[0]->cellOffset
73975 - apNew[0]->nCell*2)
73976 || rc!=SQLITE_OK
73977 );
73978 copyNodeContent(pFrom: apNew[0], pTo: pParent, pRC: &rc);
73979 freePage(pPage: apNew[0], pRC: &rc);
73980 }else if( ISAUTOVACUUM && !leafCorrection ){
73981 /* Fix the pointer map entries associated with the right-child of each
73982 ** sibling page. All other pointer map entries have already been taken
73983 ** care of. */
73984 for(i=0; i<nNew; i++){
73985 u32 key = get4byte(p: &apNew[i]->aData[8]);
73986 ptrmapPut(pBt, key, PTRMAP_BTREE, parent: apNew[i]->pgno, pRC: &rc);
73987 }
73988 }
73989
73990 assert( pParent->isInit );
73991 TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
73992 nOld, nNew, b.nCell));
73993
73994 /* Free any old pages that were not reused as new pages.
73995 */
73996 for(i=nNew; i<nOld; i++){
73997 freePage(pPage: apOld[i], pRC: &rc);
73998 }
73999
74000#if 0
74001 if( ISAUTOVACUUM && rc==SQLITE_OK && apNew[0]->isInit ){
74002 /* The ptrmapCheckPages() contains assert() statements that verify that
74003 ** all pointer map pages are set correctly. This is helpful while
74004 ** debugging. This is usually disabled because a corrupt database may
74005 ** cause an assert() statement to fail. */
74006 ptrmapCheckPages(apNew, nNew);
74007 ptrmapCheckPages(&pParent, 1);
74008 }
74009#endif
74010
74011 /*
74012 ** Cleanup before returning.
74013 */
74014balance_cleanup:
74015 sqlite3StackFree(0, b.apCell);
74016 for(i=0; i<nOld; i++){
74017 releasePage(pPage: apOld[i]);
74018 }
74019 for(i=0; i<nNew; i++){
74020 releasePage(pPage: apNew[i]);
74021 }
74022
74023 return rc;
74024}
74025
74026
74027/*
74028** This function is called when the root page of a b-tree structure is
74029** overfull (has one or more overflow pages).
74030**
74031** A new child page is allocated and the contents of the current root
74032** page, including overflow cells, are copied into the child. The root
74033** page is then overwritten to make it an empty page with the right-child
74034** pointer pointing to the new page.
74035**
74036** Before returning, all pointer-map entries corresponding to pages
74037** that the new child-page now contains pointers to are updated. The
74038** entry corresponding to the new right-child pointer of the root
74039** page is also updated.
74040**
74041** If successful, *ppChild is set to contain a reference to the child
74042** page and SQLITE_OK is returned. In this case the caller is required
74043** to call releasePage() on *ppChild exactly once. If an error occurs,
74044** an error code is returned and *ppChild is set to 0.
74045*/
74046static int balance_deeper(MemPage *pRoot, MemPage **ppChild){
74047 int rc; /* Return value from subprocedures */
74048 MemPage *pChild = 0; /* Pointer to a new child page */
74049 Pgno pgnoChild = 0; /* Page number of the new child page */
74050 BtShared *pBt = pRoot->pBt; /* The BTree */
74051
74052 assert( pRoot->nOverflow>0 );
74053 assert( sqlite3_mutex_held(pBt->mutex) );
74054
74055 /* Make pRoot, the root page of the b-tree, writable. Allocate a new
74056 ** page that will become the new right-child of pPage. Copy the contents
74057 ** of the node stored on pRoot into the new child page.
74058 */
74059 rc = sqlite3PagerWrite(pPg: pRoot->pDbPage);
74060 if( rc==SQLITE_OK ){
74061 rc = allocateBtreePage(pBt,ppPage: &pChild,pPgno: &pgnoChild,nearby: pRoot->pgno,eMode: 0);
74062 copyNodeContent(pFrom: pRoot, pTo: pChild, pRC: &rc);
74063 if( ISAUTOVACUUM ){
74064 ptrmapPut(pBt, key: pgnoChild, PTRMAP_BTREE, parent: pRoot->pgno, pRC: &rc);
74065 }
74066 }
74067 if( rc ){
74068 *ppChild = 0;
74069 releasePage(pPage: pChild);
74070 return rc;
74071 }
74072 assert( sqlite3PagerIswriteable(pChild->pDbPage) );
74073 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
74074 assert( pChild->nCell==pRoot->nCell || CORRUPT_DB );
74075
74076 TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
74077
74078 /* Copy the overflow cells from pRoot to pChild */
74079 memcpy(dest: pChild->aiOvfl, src: pRoot->aiOvfl,
74080 n: pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
74081 memcpy(dest: pChild->apOvfl, src: pRoot->apOvfl,
74082 n: pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
74083 pChild->nOverflow = pRoot->nOverflow;
74084
74085 /* Zero the contents of pRoot. Then install pChild as the right-child. */
74086 zeroPage(pPage: pRoot, flags: pChild->aData[0] & ~PTF_LEAF);
74087 put4byte(p: &pRoot->aData[pRoot->hdrOffset+8], v: pgnoChild);
74088
74089 *ppChild = pChild;
74090 return SQLITE_OK;
74091}
74092
74093/*
74094** Return SQLITE_CORRUPT if any cursor other than pCur is currently valid
74095** on the same B-tree as pCur.
74096**
74097** This can occur if a database is corrupt with two or more SQL tables
74098** pointing to the same b-tree. If an insert occurs on one SQL table
74099** and causes a BEFORE TRIGGER to do a secondary insert on the other SQL
74100** table linked to the same b-tree. If the secondary insert causes a
74101** rebalance, that can change content out from under the cursor on the
74102** first SQL table, violating invariants on the first insert.
74103*/
74104static int anotherValidCursor(BtCursor *pCur){
74105 BtCursor *pOther;
74106 for(pOther=pCur->pBt->pCursor; pOther; pOther=pOther->pNext){
74107 if( pOther!=pCur
74108 && pOther->eState==CURSOR_VALID
74109 && pOther->pPage==pCur->pPage
74110 ){
74111 return SQLITE_CORRUPT_BKPT;
74112 }
74113 }
74114 return SQLITE_OK;
74115}
74116
74117/*
74118** The page that pCur currently points to has just been modified in
74119** some way. This function figures out if this modification means the
74120** tree needs to be balanced, and if so calls the appropriate balancing
74121** routine. Balancing routines are:
74122**
74123** balance_quick()
74124** balance_deeper()
74125** balance_nonroot()
74126*/
74127static int balance(BtCursor *pCur){
74128 int rc = SQLITE_OK;
74129 const int nMin = pCur->pBt->usableSize * 2 / 3;
74130 u8 aBalanceQuickSpace[13];
74131 u8 *pFree = 0;
74132
74133 VVA_ONLY( int balance_quick_called = 0 );
74134 VVA_ONLY( int balance_deeper_called = 0 );
74135
74136 do {
74137 int iPage;
74138 MemPage *pPage = pCur->pPage;
74139
74140 if( NEVER(pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break;
74141 if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
74142 break;
74143 }else if( (iPage = pCur->iPage)==0 ){
74144 if( pPage->nOverflow && (rc = anotherValidCursor(pCur))==SQLITE_OK ){
74145 /* The root page of the b-tree is overfull. In this case call the
74146 ** balance_deeper() function to create a new child for the root-page
74147 ** and copy the current contents of the root-page to it. The
74148 ** next iteration of the do-loop will balance the child page.
74149 */
74150 assert( balance_deeper_called==0 );
74151 VVA_ONLY( balance_deeper_called++ );
74152 rc = balance_deeper(pRoot: pPage, ppChild: &pCur->apPage[1]);
74153 if( rc==SQLITE_OK ){
74154 pCur->iPage = 1;
74155 pCur->ix = 0;
74156 pCur->aiIdx[0] = 0;
74157 pCur->apPage[0] = pPage;
74158 pCur->pPage = pCur->apPage[1];
74159 assert( pCur->pPage->nOverflow );
74160 }
74161 }else{
74162 break;
74163 }
74164 }else{
74165 MemPage * const pParent = pCur->apPage[iPage-1];
74166 int const iIdx = pCur->aiIdx[iPage-1];
74167
74168 rc = sqlite3PagerWrite(pPg: pParent->pDbPage);
74169 if( rc==SQLITE_OK && pParent->nFree<0 ){
74170 rc = btreeComputeFreeSpace(pPage: pParent);
74171 }
74172 if( rc==SQLITE_OK ){
74173#ifndef SQLITE_OMIT_QUICKBALANCE
74174 if( pPage->intKeyLeaf
74175 && pPage->nOverflow==1
74176 && pPage->aiOvfl[0]==pPage->nCell
74177 && pParent->pgno!=1
74178 && pParent->nCell==iIdx
74179 ){
74180 /* Call balance_quick() to create a new sibling of pPage on which
74181 ** to store the overflow cell. balance_quick() inserts a new cell
74182 ** into pParent, which may cause pParent overflow. If this
74183 ** happens, the next iteration of the do-loop will balance pParent
74184 ** use either balance_nonroot() or balance_deeper(). Until this
74185 ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
74186 ** buffer.
74187 **
74188 ** The purpose of the following assert() is to check that only a
74189 ** single call to balance_quick() is made for each call to this
74190 ** function. If this were not verified, a subtle bug involving reuse
74191 ** of the aBalanceQuickSpace[] might sneak in.
74192 */
74193 assert( balance_quick_called==0 );
74194 VVA_ONLY( balance_quick_called++ );
74195 rc = balance_quick(pParent, pPage, pSpace: aBalanceQuickSpace);
74196 }else
74197#endif
74198 {
74199 /* In this case, call balance_nonroot() to redistribute cells
74200 ** between pPage and up to 2 of its sibling pages. This involves
74201 ** modifying the contents of pParent, which may cause pParent to
74202 ** become overfull or underfull. The next iteration of the do-loop
74203 ** will balance the parent page to correct this.
74204 **
74205 ** If the parent page becomes overfull, the overflow cell or cells
74206 ** are stored in the pSpace buffer allocated immediately below.
74207 ** A subsequent iteration of the do-loop will deal with this by
74208 ** calling balance_nonroot() (balance_deeper() may be called first,
74209 ** but it doesn't deal with overflow cells - just moves them to a
74210 ** different page). Once this subsequent call to balance_nonroot()
74211 ** has completed, it is safe to release the pSpace buffer used by
74212 ** the previous call, as the overflow cell data will have been
74213 ** copied either into the body of a database page or into the new
74214 ** pSpace buffer passed to the latter call to balance_nonroot().
74215 */
74216 u8 *pSpace = sqlite3PageMalloc(sz: pCur->pBt->pageSize);
74217 rc = balance_nonroot(pParent, iParentIdx: iIdx, aOvflSpace: pSpace, isRoot: iPage==1,
74218 bBulk: pCur->hints&BTREE_BULKLOAD);
74219 if( pFree ){
74220 /* If pFree is not NULL, it points to the pSpace buffer used
74221 ** by a previous call to balance_nonroot(). Its contents are
74222 ** now stored either on real database pages or within the
74223 ** new pSpace buffer, so it may be safely freed here. */
74224 sqlite3PageFree(p: pFree);
74225 }
74226
74227 /* The pSpace buffer will be freed after the next call to
74228 ** balance_nonroot(), or just before this function returns, whichever
74229 ** comes first. */
74230 pFree = pSpace;
74231 }
74232 }
74233
74234 pPage->nOverflow = 0;
74235
74236 /* The next iteration of the do-loop balances the parent page. */
74237 releasePage(pPage);
74238 pCur->iPage--;
74239 assert( pCur->iPage>=0 );
74240 pCur->pPage = pCur->apPage[pCur->iPage];
74241 }
74242 }while( rc==SQLITE_OK );
74243
74244 if( pFree ){
74245 sqlite3PageFree(p: pFree);
74246 }
74247 return rc;
74248}
74249
74250/* Overwrite content from pX into pDest. Only do the write if the
74251** content is different from what is already there.
74252*/
74253static int btreeOverwriteContent(
74254 MemPage *pPage, /* MemPage on which writing will occur */
74255 u8 *pDest, /* Pointer to the place to start writing */
74256 const BtreePayload *pX, /* Source of data to write */
74257 int iOffset, /* Offset of first byte to write */
74258 int iAmt /* Number of bytes to be written */
74259){
74260 int nData = pX->nData - iOffset;
74261 if( nData<=0 ){
74262 /* Overwritting with zeros */
74263 int i;
74264 for(i=0; i<iAmt && pDest[i]==0; i++){}
74265 if( i<iAmt ){
74266 int rc = sqlite3PagerWrite(pPg: pPage->pDbPage);
74267 if( rc ) return rc;
74268 memset(s: pDest + i, c: 0, n: iAmt - i);
74269 }
74270 }else{
74271 if( nData<iAmt ){
74272 /* Mixed read data and zeros at the end. Make a recursive call
74273 ** to write the zeros then fall through to write the real data */
74274 int rc = btreeOverwriteContent(pPage, pDest: pDest+nData, pX, iOffset: iOffset+nData,
74275 iAmt: iAmt-nData);
74276 if( rc ) return rc;
74277 iAmt = nData;
74278 }
74279 if( memcmp(s1: pDest, s2: ((u8*)pX->pData) + iOffset, n: iAmt)!=0 ){
74280 int rc = sqlite3PagerWrite(pPg: pPage->pDbPage);
74281 if( rc ) return rc;
74282 /* In a corrupt database, it is possible for the source and destination
74283 ** buffers to overlap. This is harmless since the database is already
74284 ** corrupt but it does cause valgrind and ASAN warnings. So use
74285 ** memmove(). */
74286 memmove(dest: pDest, src: ((u8*)pX->pData) + iOffset, n: iAmt);
74287 }
74288 }
74289 return SQLITE_OK;
74290}
74291
74292/*
74293** Overwrite the cell that cursor pCur is pointing to with fresh content
74294** contained in pX.
74295*/
74296static int btreeOverwriteCell(BtCursor *pCur, const BtreePayload *pX){
74297 int iOffset; /* Next byte of pX->pData to write */
74298 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
74299 int rc; /* Return code */
74300 MemPage *pPage = pCur->pPage; /* Page being written */
74301 BtShared *pBt; /* Btree */
74302 Pgno ovflPgno; /* Next overflow page to write */
74303 u32 ovflPageSize; /* Size to write on overflow page */
74304
74305 if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd
74306 || pCur->info.pPayload < pPage->aData + pPage->cellOffset
74307 ){
74308 return SQLITE_CORRUPT_BKPT;
74309 }
74310 /* Overwrite the local portion first */
74311 rc = btreeOverwriteContent(pPage, pDest: pCur->info.pPayload, pX,
74312 iOffset: 0, iAmt: pCur->info.nLocal);
74313 if( rc ) return rc;
74314 if( pCur->info.nLocal==nTotal ) return SQLITE_OK;
74315
74316 /* Now overwrite the overflow pages */
74317 iOffset = pCur->info.nLocal;
74318 assert( nTotal>=0 );
74319 assert( iOffset>=0 );
74320 ovflPgno = get4byte(p: pCur->info.pPayload + iOffset);
74321 pBt = pPage->pBt;
74322 ovflPageSize = pBt->usableSize - 4;
74323 do{
74324 rc = btreeGetPage(pBt, pgno: ovflPgno, ppPage: &pPage, flags: 0);
74325 if( rc ) return rc;
74326 if( sqlite3PagerPageRefcount(pPage: pPage->pDbPage)!=1 || pPage->isInit ){
74327 rc = SQLITE_CORRUPT_BKPT;
74328 }else{
74329 if( iOffset+ovflPageSize<(u32)nTotal ){
74330 ovflPgno = get4byte(p: pPage->aData);
74331 }else{
74332 ovflPageSize = nTotal - iOffset;
74333 }
74334 rc = btreeOverwriteContent(pPage, pDest: pPage->aData+4, pX,
74335 iOffset, iAmt: ovflPageSize);
74336 }
74337 sqlite3PagerUnref(pPg: pPage->pDbPage);
74338 if( rc ) return rc;
74339 iOffset += ovflPageSize;
74340 }while( iOffset<nTotal );
74341 return SQLITE_OK;
74342}
74343
74344
74345/*
74346** Insert a new record into the BTree. The content of the new record
74347** is described by the pX object. The pCur cursor is used only to
74348** define what table the record should be inserted into, and is left
74349** pointing at a random location.
74350**
74351** For a table btree (used for rowid tables), only the pX.nKey value of
74352** the key is used. The pX.pKey value must be NULL. The pX.nKey is the
74353** rowid or INTEGER PRIMARY KEY of the row. The pX.nData,pData,nZero fields
74354** hold the content of the row.
74355**
74356** For an index btree (used for indexes and WITHOUT ROWID tables), the
74357** key is an arbitrary byte sequence stored in pX.pKey,nKey. The
74358** pX.pData,nData,nZero fields must be zero.
74359**
74360** If the seekResult parameter is non-zero, then a successful call to
74361** MovetoUnpacked() to seek cursor pCur to (pKey,nKey) has already
74362** been performed. In other words, if seekResult!=0 then the cursor
74363** is currently pointing to a cell that will be adjacent to the cell
74364** to be inserted. If seekResult<0 then pCur points to a cell that is
74365** smaller then (pKey,nKey). If seekResult>0 then pCur points to a cell
74366** that is larger than (pKey,nKey).
74367**
74368** If seekResult==0, that means pCur is pointing at some unknown location.
74369** In that case, this routine must seek the cursor to the correct insertion
74370** point for (pKey,nKey) before doing the insertion. For index btrees,
74371** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked
74372** key values and pX->aMem can be used instead of pX->pKey to avoid having
74373** to decode the key.
74374*/
74375SQLITE_PRIVATE int sqlite3BtreeInsert(
74376 BtCursor *pCur, /* Insert data into the table of this cursor */
74377 const BtreePayload *pX, /* Content of the row to be inserted */
74378 int flags, /* True if this is likely an append */
74379 int seekResult /* Result of prior MovetoUnpacked() call */
74380){
74381 int rc;
74382 int loc = seekResult; /* -1: before desired location +1: after */
74383 int szNew = 0;
74384 int idx;
74385 MemPage *pPage;
74386 Btree *p = pCur->pBtree;
74387 BtShared *pBt = p->pBt;
74388 unsigned char *oldCell;
74389 unsigned char *newCell = 0;
74390
74391 assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND|BTREE_PREFORMAT))==flags );
74392 assert( (flags & BTREE_PREFORMAT)==0 || seekResult || pCur->pKeyInfo==0 );
74393
74394 if( pCur->eState==CURSOR_FAULT ){
74395 assert( pCur->skipNext!=SQLITE_OK );
74396 return pCur->skipNext;
74397 }
74398
74399 assert( cursorOwnsBtShared(pCur) );
74400 assert( (pCur->curFlags & BTCF_WriteFlag)!=0
74401 && pBt->inTransaction==TRANS_WRITE
74402 && (pBt->btsFlags & BTS_READ_ONLY)==0 );
74403 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
74404
74405 /* Assert that the caller has been consistent. If this cursor was opened
74406 ** expecting an index b-tree, then the caller should be inserting blob
74407 ** keys with no associated data. If the cursor was opened expecting an
74408 ** intkey table, the caller should be inserting integer keys with a
74409 ** blob of associated data. */
74410 assert( (flags & BTREE_PREFORMAT) || (pX->pKey==0)==(pCur->pKeyInfo==0) );
74411
74412 /* Save the positions of any other cursors open on this table.
74413 **
74414 ** In some cases, the call to btreeMoveto() below is a no-op. For
74415 ** example, when inserting data into a table with auto-generated integer
74416 ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the
74417 ** integer key to use. It then calls this function to actually insert the
74418 ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
74419 ** that the cursor is already where it needs to be and returns without
74420 ** doing any work. To avoid thwarting these optimizations, it is important
74421 ** not to clear the cursor here.
74422 */
74423 if( pCur->curFlags & BTCF_Multiple ){
74424 rc = saveAllCursors(pBt, iRoot: pCur->pgnoRoot, pExcept: pCur);
74425 if( rc ) return rc;
74426 if( loc && pCur->iPage<0 ){
74427 /* This can only happen if the schema is corrupt such that there is more
74428 ** than one table or index with the same root page as used by the cursor.
74429 ** Which can only happen if the SQLITE_NoSchemaError flag was set when
74430 ** the schema was loaded. This cannot be asserted though, as a user might
74431 ** set the flag, load the schema, and then unset the flag. */
74432 return SQLITE_CORRUPT_BKPT;
74433 }
74434 }
74435
74436 if( pCur->pKeyInfo==0 ){
74437 assert( pX->pKey==0 );
74438 /* If this is an insert into a table b-tree, invalidate any incrblob
74439 ** cursors open on the row being replaced */
74440 if( p->hasIncrblobCur ){
74441 invalidateIncrblobCursors(pBtree: p, pgnoRoot: pCur->pgnoRoot, iRow: pX->nKey, isClearTable: 0);
74442 }
74443
74444 /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
74445 ** to a row with the same key as the new entry being inserted.
74446 */
74447#ifdef SQLITE_DEBUG
74448 if( flags & BTREE_SAVEPOSITION ){
74449 assert( pCur->curFlags & BTCF_ValidNKey );
74450 assert( pX->nKey==pCur->info.nKey );
74451 assert( loc==0 );
74452 }
74453#endif
74454
74455 /* On the other hand, BTREE_SAVEPOSITION==0 does not imply
74456 ** that the cursor is not pointing to a row to be overwritten.
74457 ** So do a complete check.
74458 */
74459 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
74460 /* The cursor is pointing to the entry that is to be
74461 ** overwritten */
74462 assert( pX->nData>=0 && pX->nZero>=0 );
74463 if( pCur->info.nSize!=0
74464 && pCur->info.nPayload==(u32)pX->nData+pX->nZero
74465 ){
74466 /* New entry is the same size as the old. Do an overwrite */
74467 return btreeOverwriteCell(pCur, pX);
74468 }
74469 assert( loc==0 );
74470 }else if( loc==0 ){
74471 /* The cursor is *not* pointing to the cell to be overwritten, nor
74472 ** to an adjacent cell. Move the cursor so that it is pointing either
74473 ** to the cell to be overwritten or an adjacent cell.
74474 */
74475 rc = sqlite3BtreeTableMoveto(pCur, intKey: pX->nKey,
74476 biasRight: (flags & BTREE_APPEND)!=0, pRes: &loc);
74477 if( rc ) return rc;
74478 }
74479 }else{
74480 /* This is an index or a WITHOUT ROWID table */
74481
74482 /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
74483 ** to a row with the same key as the new entry being inserted.
74484 */
74485 assert( (flags & BTREE_SAVEPOSITION)==0 || loc==0 );
74486
74487 /* If the cursor is not already pointing either to the cell to be
74488 ** overwritten, or if a new cell is being inserted, if the cursor is
74489 ** not pointing to an immediately adjacent cell, then move the cursor
74490 ** so that it does.
74491 */
74492 if( loc==0 && (flags & BTREE_SAVEPOSITION)==0 ){
74493 if( pX->nMem ){
74494 UnpackedRecord r;
74495 r.pKeyInfo = pCur->pKeyInfo;
74496 r.aMem = pX->aMem;
74497 r.nField = pX->nMem;
74498 r.default_rc = 0;
74499 r.eqSeen = 0;
74500 rc = sqlite3BtreeIndexMoveto(pCur, pIdxKey: &r, pRes: &loc);
74501 }else{
74502 rc = btreeMoveto(pCur, pKey: pX->pKey, nKey: pX->nKey,
74503 bias: (flags & BTREE_APPEND)!=0, pRes: &loc);
74504 }
74505 if( rc ) return rc;
74506 }
74507
74508 /* If the cursor is currently pointing to an entry to be overwritten
74509 ** and the new content is the same as as the old, then use the
74510 ** overwrite optimization.
74511 */
74512 if( loc==0 ){
74513 getCellInfo(pCur);
74514 if( pCur->info.nKey==pX->nKey ){
74515 BtreePayload x2;
74516 x2.pData = pX->pKey;
74517 x2.nData = pX->nKey;
74518 x2.nZero = 0;
74519 return btreeOverwriteCell(pCur, pX: &x2);
74520 }
74521 }
74522 }
74523 assert( pCur->eState==CURSOR_VALID
74524 || (pCur->eState==CURSOR_INVALID && loc)
74525 || CORRUPT_DB );
74526
74527 pPage = pCur->pPage;
74528 assert( pPage->intKey || pX->nKey>=0 || (flags & BTREE_PREFORMAT) );
74529 assert( pPage->leaf || !pPage->intKey );
74530 if( pPage->nFree<0 ){
74531 if( NEVER(pCur->eState>CURSOR_INVALID) ){
74532 rc = SQLITE_CORRUPT_BKPT;
74533 }else{
74534 rc = btreeComputeFreeSpace(pPage);
74535 }
74536 if( rc ) return rc;
74537 }
74538
74539 TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
74540 pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
74541 loc==0 ? "overwrite" : "new entry"));
74542 assert( pPage->isInit );
74543 newCell = pBt->pTmpSpace;
74544 assert( newCell!=0 );
74545 if( flags & BTREE_PREFORMAT ){
74546 rc = SQLITE_OK;
74547 szNew = pBt->nPreformatSize;
74548 if( szNew<4 ) szNew = 4;
74549 if( ISAUTOVACUUM && szNew>pPage->maxLocal ){
74550 CellInfo info;
74551 pPage->xParseCell(pPage, newCell, &info);
74552 if( info.nPayload!=info.nLocal ){
74553 Pgno ovfl = get4byte(p: &newCell[szNew-4]);
74554 ptrmapPut(pBt, key: ovfl, PTRMAP_OVERFLOW1, parent: pPage->pgno, pRC: &rc);
74555 }
74556 }
74557 }else{
74558 rc = fillInCell(pPage, pCell: newCell, pX, pnSize: &szNew);
74559 }
74560 if( rc ) goto end_insert;
74561 assert( szNew==pPage->xCellSize(pPage, newCell) );
74562 assert( szNew <= MX_CELL_SIZE(pBt) );
74563 idx = pCur->ix;
74564 if( loc==0 ){
74565 CellInfo info;
74566 assert( idx>=0 );
74567 if( idx>=pPage->nCell ){
74568 return SQLITE_CORRUPT_BKPT;
74569 }
74570 rc = sqlite3PagerWrite(pPg: pPage->pDbPage);
74571 if( rc ){
74572 goto end_insert;
74573 }
74574 oldCell = findCell(pPage, idx);
74575 if( !pPage->leaf ){
74576 memcpy(dest: newCell, src: oldCell, n: 4);
74577 }
74578 BTREE_CLEAR_CELL(rc, pPage, oldCell, info);
74579 testcase( pCur->curFlags & BTCF_ValidOvfl );
74580 invalidateOverflowCache(pCur);
74581 if( info.nSize==szNew && info.nLocal==info.nPayload
74582 && (!ISAUTOVACUUM || szNew<pPage->minLocal)
74583 ){
74584 /* Overwrite the old cell with the new if they are the same size.
74585 ** We could also try to do this if the old cell is smaller, then add
74586 ** the leftover space to the free list. But experiments show that
74587 ** doing that is no faster then skipping this optimization and just
74588 ** calling dropCell() and insertCell().
74589 **
74590 ** This optimization cannot be used on an autovacuum database if the
74591 ** new entry uses overflow pages, as the insertCell() call below is
74592 ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry. */
74593 assert( rc==SQLITE_OK ); /* clearCell never fails when nLocal==nPayload */
74594 if( oldCell < pPage->aData+pPage->hdrOffset+10 ){
74595 return SQLITE_CORRUPT_BKPT;
74596 }
74597 if( oldCell+szNew > pPage->aDataEnd ){
74598 return SQLITE_CORRUPT_BKPT;
74599 }
74600 memcpy(dest: oldCell, src: newCell, n: szNew);
74601 return SQLITE_OK;
74602 }
74603 dropCell(pPage, idx, sz: info.nSize, pRC: &rc);
74604 if( rc ) goto end_insert;
74605 }else if( loc<0 && pPage->nCell>0 ){
74606 assert( pPage->leaf );
74607 idx = ++pCur->ix;
74608 pCur->curFlags &= ~BTCF_ValidNKey;
74609 }else{
74610 assert( pPage->leaf );
74611 }
74612 insertCell(pPage, i: idx, pCell: newCell, sz: szNew, pTemp: 0, iChild: 0, pRC: &rc);
74613 assert( pPage->nOverflow==0 || rc==SQLITE_OK );
74614 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
74615
74616 /* If no error has occurred and pPage has an overflow cell, call balance()
74617 ** to redistribute the cells within the tree. Since balance() may move
74618 ** the cursor, zero the BtCursor.info.nSize and BTCF_ValidNKey
74619 ** variables.
74620 **
74621 ** Previous versions of SQLite called moveToRoot() to move the cursor
74622 ** back to the root page as balance() used to invalidate the contents
74623 ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
74624 ** set the cursor state to "invalid". This makes common insert operations
74625 ** slightly faster.
74626 **
74627 ** There is a subtle but important optimization here too. When inserting
74628 ** multiple records into an intkey b-tree using a single cursor (as can
74629 ** happen while processing an "INSERT INTO ... SELECT" statement), it
74630 ** is advantageous to leave the cursor pointing to the last entry in
74631 ** the b-tree if possible. If the cursor is left pointing to the last
74632 ** entry in the table, and the next row inserted has an integer key
74633 ** larger than the largest existing key, it is possible to insert the
74634 ** row without seeking the cursor. This can be a big performance boost.
74635 */
74636 pCur->info.nSize = 0;
74637 if( pPage->nOverflow ){
74638 assert( rc==SQLITE_OK );
74639 pCur->curFlags &= ~(BTCF_ValidNKey);
74640 rc = balance(pCur);
74641
74642 /* Must make sure nOverflow is reset to zero even if the balance()
74643 ** fails. Internal data structure corruption will result otherwise.
74644 ** Also, set the cursor state to invalid. This stops saveCursorPosition()
74645 ** from trying to save the current position of the cursor. */
74646 pCur->pPage->nOverflow = 0;
74647 pCur->eState = CURSOR_INVALID;
74648 if( (flags & BTREE_SAVEPOSITION) && rc==SQLITE_OK ){
74649 btreeReleaseAllCursorPages(pCur);
74650 if( pCur->pKeyInfo ){
74651 assert( pCur->pKey==0 );
74652 pCur->pKey = sqlite3Malloc( n: pX->nKey );
74653 if( pCur->pKey==0 ){
74654 rc = SQLITE_NOMEM;
74655 }else{
74656 memcpy(dest: pCur->pKey, src: pX->pKey, n: pX->nKey);
74657 }
74658 }
74659 pCur->eState = CURSOR_REQUIRESEEK;
74660 pCur->nKey = pX->nKey;
74661 }
74662 }
74663 assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 );
74664
74665end_insert:
74666 return rc;
74667}
74668
74669/*
74670** This function is used as part of copying the current row from cursor
74671** pSrc into cursor pDest. If the cursors are open on intkey tables, then
74672** parameter iKey is used as the rowid value when the record is copied
74673** into pDest. Otherwise, the record is copied verbatim.
74674**
74675** This function does not actually write the new value to cursor pDest.
74676** Instead, it creates and populates any required overflow pages and
74677** writes the data for the new cell into the BtShared.pTmpSpace buffer
74678** for the destination database. The size of the cell, in bytes, is left
74679** in BtShared.nPreformatSize. The caller completes the insertion by
74680** calling sqlite3BtreeInsert() with the BTREE_PREFORMAT flag specified.
74681**
74682** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
74683*/
74684SQLITE_PRIVATE int sqlite3BtreeTransferRow(BtCursor *pDest, BtCursor *pSrc, i64 iKey){
74685 int rc = SQLITE_OK;
74686 BtShared *pBt = pDest->pBt;
74687 u8 *aOut = pBt->pTmpSpace; /* Pointer to next output buffer */
74688 const u8 *aIn; /* Pointer to next input buffer */
74689 u32 nIn; /* Size of input buffer aIn[] */
74690 u32 nRem; /* Bytes of data still to copy */
74691
74692 getCellInfo(pCur: pSrc);
74693 aOut += putVarint32(aOut, pSrc->info.nPayload);
74694 if( pDest->pKeyInfo==0 ) aOut += putVarint(p: aOut, v: iKey);
74695 nIn = pSrc->info.nLocal;
74696 aIn = pSrc->info.pPayload;
74697 if( aIn+nIn>pSrc->pPage->aDataEnd ){
74698 return SQLITE_CORRUPT_BKPT;
74699 }
74700 nRem = pSrc->info.nPayload;
74701 if( nIn==nRem && nIn<pDest->pPage->maxLocal ){
74702 memcpy(dest: aOut, src: aIn, n: nIn);
74703 pBt->nPreformatSize = nIn + (aOut - pBt->pTmpSpace);
74704 }else{
74705 Pager *pSrcPager = pSrc->pBt->pPager;
74706 u8 *pPgnoOut = 0;
74707 Pgno ovflIn = 0;
74708 DbPage *pPageIn = 0;
74709 MemPage *pPageOut = 0;
74710 u32 nOut; /* Size of output buffer aOut[] */
74711
74712 nOut = btreePayloadToLocal(pPage: pDest->pPage, nPayload: pSrc->info.nPayload);
74713 pBt->nPreformatSize = nOut + (aOut - pBt->pTmpSpace);
74714 if( nOut<pSrc->info.nPayload ){
74715 pPgnoOut = &aOut[nOut];
74716 pBt->nPreformatSize += 4;
74717 }
74718
74719 if( nRem>nIn ){
74720 if( aIn+nIn+4>pSrc->pPage->aDataEnd ){
74721 return SQLITE_CORRUPT_BKPT;
74722 }
74723 ovflIn = get4byte(p: &pSrc->info.pPayload[nIn]);
74724 }
74725
74726 do {
74727 nRem -= nOut;
74728 do{
74729 assert( nOut>0 );
74730 if( nIn>0 ){
74731 int nCopy = MIN(nOut, nIn);
74732 memcpy(dest: aOut, src: aIn, n: nCopy);
74733 nOut -= nCopy;
74734 nIn -= nCopy;
74735 aOut += nCopy;
74736 aIn += nCopy;
74737 }
74738 if( nOut>0 ){
74739 sqlite3PagerUnref(pPg: pPageIn);
74740 pPageIn = 0;
74741 rc = sqlite3PagerGet(pPager: pSrcPager, pgno: ovflIn, ppPage: &pPageIn, PAGER_GET_READONLY);
74742 if( rc==SQLITE_OK ){
74743 aIn = (const u8*)sqlite3PagerGetData(pPg: pPageIn);
74744 ovflIn = get4byte(p: aIn);
74745 aIn += 4;
74746 nIn = pSrc->pBt->usableSize - 4;
74747 }
74748 }
74749 }while( rc==SQLITE_OK && nOut>0 );
74750
74751 if( rc==SQLITE_OK && nRem>0 && ALWAYS(pPgnoOut) ){
74752 Pgno pgnoNew;
74753 MemPage *pNew = 0;
74754 rc = allocateBtreePage(pBt, ppPage: &pNew, pPgno: &pgnoNew, nearby: 0, eMode: 0);
74755 put4byte(p: pPgnoOut, v: pgnoNew);
74756 if( ISAUTOVACUUM && pPageOut ){
74757 ptrmapPut(pBt, key: pgnoNew, PTRMAP_OVERFLOW2, parent: pPageOut->pgno, pRC: &rc);
74758 }
74759 releasePage(pPage: pPageOut);
74760 pPageOut = pNew;
74761 if( pPageOut ){
74762 pPgnoOut = pPageOut->aData;
74763 put4byte(p: pPgnoOut, v: 0);
74764 aOut = &pPgnoOut[4];
74765 nOut = MIN(pBt->usableSize - 4, nRem);
74766 }
74767 }
74768 }while( nRem>0 && rc==SQLITE_OK );
74769
74770 releasePage(pPage: pPageOut);
74771 sqlite3PagerUnref(pPg: pPageIn);
74772 }
74773
74774 return rc;
74775}
74776
74777/*
74778** Delete the entry that the cursor is pointing to.
74779**
74780** If the BTREE_SAVEPOSITION bit of the flags parameter is zero, then
74781** the cursor is left pointing at an arbitrary location after the delete.
74782** But if that bit is set, then the cursor is left in a state such that
74783** the next call to BtreeNext() or BtreePrev() moves it to the same row
74784** as it would have been on if the call to BtreeDelete() had been omitted.
74785**
74786** The BTREE_AUXDELETE bit of flags indicates that is one of several deletes
74787** associated with a single table entry and its indexes. Only one of those
74788** deletes is considered the "primary" delete. The primary delete occurs
74789** on a cursor that is not a BTREE_FORDELETE cursor. All but one delete
74790** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag.
74791** The BTREE_AUXDELETE bit is a hint that is not used by this implementation,
74792** but which might be used by alternative storage engines.
74793*/
74794SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){
74795 Btree *p = pCur->pBtree;
74796 BtShared *pBt = p->pBt;
74797 int rc; /* Return code */
74798 MemPage *pPage; /* Page to delete cell from */
74799 unsigned char *pCell; /* Pointer to cell to delete */
74800 int iCellIdx; /* Index of cell to delete */
74801 int iCellDepth; /* Depth of node containing pCell */
74802 CellInfo info; /* Size of the cell being deleted */
74803 int bSkipnext = 0; /* Leaf cursor in SKIPNEXT state */
74804 u8 bPreserve = flags & BTREE_SAVEPOSITION; /* Keep cursor valid */
74805
74806 assert( cursorOwnsBtShared(pCur) );
74807 assert( pBt->inTransaction==TRANS_WRITE );
74808 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
74809 assert( pCur->curFlags & BTCF_WriteFlag );
74810 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
74811 assert( !hasReadConflicts(p, pCur->pgnoRoot) );
74812 assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 );
74813 if( pCur->eState==CURSOR_REQUIRESEEK ){
74814 rc = btreeRestoreCursorPosition(pCur);
74815 assert( rc!=SQLITE_OK || CORRUPT_DB || pCur->eState==CURSOR_VALID );
74816 if( rc || pCur->eState!=CURSOR_VALID ) return rc;
74817 }
74818 assert( CORRUPT_DB || pCur->eState==CURSOR_VALID );
74819
74820 iCellDepth = pCur->iPage;
74821 iCellIdx = pCur->ix;
74822 pPage = pCur->pPage;
74823 pCell = findCell(pPage, iCellIdx);
74824 if( pPage->nFree<0 && btreeComputeFreeSpace(pPage) ){
74825 return SQLITE_CORRUPT_BKPT;
74826 }
74827 if( pPage->nCell<=iCellIdx ){
74828 return SQLITE_CORRUPT_BKPT;
74829 }
74830
74831 /* If the bPreserve flag is set to true, then the cursor position must
74832 ** be preserved following this delete operation. If the current delete
74833 ** will cause a b-tree rebalance, then this is done by saving the cursor
74834 ** key and leaving the cursor in CURSOR_REQUIRESEEK state before
74835 ** returning.
74836 **
74837 ** Or, if the current delete will not cause a rebalance, then the cursor
74838 ** will be left in CURSOR_SKIPNEXT state pointing to the entry immediately
74839 ** before or after the deleted entry. In this case set bSkipnext to true. */
74840 if( bPreserve ){
74841 if( !pPage->leaf
74842 || (pPage->nFree+cellSizePtr(pPage,pCell)+2)>(int)(pBt->usableSize*2/3)
74843 || pPage->nCell==1 /* See dbfuzz001.test for a test case */
74844 ){
74845 /* A b-tree rebalance will be required after deleting this entry.
74846 ** Save the cursor key. */
74847 rc = saveCursorKey(pCur);
74848 if( rc ) return rc;
74849 }else{
74850 bSkipnext = 1;
74851 }
74852 }
74853
74854 /* If the page containing the entry to delete is not a leaf page, move
74855 ** the cursor to the largest entry in the tree that is smaller than
74856 ** the entry being deleted. This cell will replace the cell being deleted
74857 ** from the internal node. The 'previous' entry is used for this instead
74858 ** of the 'next' entry, as the previous entry is always a part of the
74859 ** sub-tree headed by the child page of the cell being deleted. This makes
74860 ** balancing the tree following the delete operation easier. */
74861 if( !pPage->leaf ){
74862 rc = sqlite3BtreePrevious(pCur, flags: 0);
74863 assert( rc!=SQLITE_DONE );
74864 if( rc ) return rc;
74865 }
74866
74867 /* Save the positions of any other cursors open on this table before
74868 ** making any modifications. */
74869 if( pCur->curFlags & BTCF_Multiple ){
74870 rc = saveAllCursors(pBt, iRoot: pCur->pgnoRoot, pExcept: pCur);
74871 if( rc ) return rc;
74872 }
74873
74874 /* If this is a delete operation to remove a row from a table b-tree,
74875 ** invalidate any incrblob cursors open on the row being deleted. */
74876 if( pCur->pKeyInfo==0 && p->hasIncrblobCur ){
74877 invalidateIncrblobCursors(pBtree: p, pgnoRoot: pCur->pgnoRoot, iRow: pCur->info.nKey, isClearTable: 0);
74878 }
74879
74880 /* Make the page containing the entry to be deleted writable. Then free any
74881 ** overflow pages associated with the entry and finally remove the cell
74882 ** itself from within the page. */
74883 rc = sqlite3PagerWrite(pPg: pPage->pDbPage);
74884 if( rc ) return rc;
74885 BTREE_CLEAR_CELL(rc, pPage, pCell, info);
74886 dropCell(pPage, idx: iCellIdx, sz: info.nSize, pRC: &rc);
74887 if( rc ) return rc;
74888
74889 /* If the cell deleted was not located on a leaf page, then the cursor
74890 ** is currently pointing to the largest entry in the sub-tree headed
74891 ** by the child-page of the cell that was just deleted from an internal
74892 ** node. The cell from the leaf node needs to be moved to the internal
74893 ** node to replace the deleted cell. */
74894 if( !pPage->leaf ){
74895 MemPage *pLeaf = pCur->pPage;
74896 int nCell;
74897 Pgno n;
74898 unsigned char *pTmp;
74899
74900 if( pLeaf->nFree<0 ){
74901 rc = btreeComputeFreeSpace(pPage: pLeaf);
74902 if( rc ) return rc;
74903 }
74904 if( iCellDepth<pCur->iPage-1 ){
74905 n = pCur->apPage[iCellDepth+1]->pgno;
74906 }else{
74907 n = pCur->pPage->pgno;
74908 }
74909 pCell = findCell(pLeaf, pLeaf->nCell-1);
74910 if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
74911 nCell = pLeaf->xCellSize(pLeaf, pCell);
74912 assert( MX_CELL_SIZE(pBt) >= nCell );
74913 pTmp = pBt->pTmpSpace;
74914 assert( pTmp!=0 );
74915 rc = sqlite3PagerWrite(pPg: pLeaf->pDbPage);
74916 if( rc==SQLITE_OK ){
74917 insertCell(pPage, i: iCellIdx, pCell: pCell-4, sz: nCell+4, pTemp: pTmp, iChild: n, pRC: &rc);
74918 }
74919 dropCell(pPage: pLeaf, idx: pLeaf->nCell-1, sz: nCell, pRC: &rc);
74920 if( rc ) return rc;
74921 }
74922
74923 /* Balance the tree. If the entry deleted was located on a leaf page,
74924 ** then the cursor still points to that page. In this case the first
74925 ** call to balance() repairs the tree, and the if(...) condition is
74926 ** never true.
74927 **
74928 ** Otherwise, if the entry deleted was on an internal node page, then
74929 ** pCur is pointing to the leaf page from which a cell was removed to
74930 ** replace the cell deleted from the internal node. This is slightly
74931 ** tricky as the leaf node may be underfull, and the internal node may
74932 ** be either under or overfull. In this case run the balancing algorithm
74933 ** on the leaf node first. If the balance proceeds far enough up the
74934 ** tree that we can be sure that any problem in the internal node has
74935 ** been corrected, so be it. Otherwise, after balancing the leaf node,
74936 ** walk the cursor up the tree to the internal node and balance it as
74937 ** well. */
74938 rc = balance(pCur);
74939 if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
74940 releasePageNotNull(pPage: pCur->pPage);
74941 pCur->iPage--;
74942 while( pCur->iPage>iCellDepth ){
74943 releasePage(pPage: pCur->apPage[pCur->iPage--]);
74944 }
74945 pCur->pPage = pCur->apPage[pCur->iPage];
74946 rc = balance(pCur);
74947 }
74948
74949 if( rc==SQLITE_OK ){
74950 if( bSkipnext ){
74951 assert( bPreserve && (pCur->iPage==iCellDepth || CORRUPT_DB) );
74952 assert( pPage==pCur->pPage || CORRUPT_DB );
74953 assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell );
74954 pCur->eState = CURSOR_SKIPNEXT;
74955 if( iCellIdx>=pPage->nCell ){
74956 pCur->skipNext = -1;
74957 pCur->ix = pPage->nCell-1;
74958 }else{
74959 pCur->skipNext = 1;
74960 }
74961 }else{
74962 rc = moveToRoot(pCur);
74963 if( bPreserve ){
74964 btreeReleaseAllCursorPages(pCur);
74965 pCur->eState = CURSOR_REQUIRESEEK;
74966 }
74967 if( rc==SQLITE_EMPTY ) rc = SQLITE_OK;
74968 }
74969 }
74970 return rc;
74971}
74972
74973/*
74974** Create a new BTree table. Write into *piTable the page
74975** number for the root page of the new table.
74976**
74977** The type of type is determined by the flags parameter. Only the
74978** following values of flags are currently in use. Other values for
74979** flags might not work:
74980**
74981** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys
74982** BTREE_ZERODATA Used for SQL indices
74983*/
74984static int btreeCreateTable(Btree *p, Pgno *piTable, int createTabFlags){
74985 BtShared *pBt = p->pBt;
74986 MemPage *pRoot;
74987 Pgno pgnoRoot;
74988 int rc;
74989 int ptfFlags; /* Page-type flage for the root page of new table */
74990
74991 assert( sqlite3BtreeHoldsMutex(p) );
74992 assert( pBt->inTransaction==TRANS_WRITE );
74993 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
74994
74995#ifdef SQLITE_OMIT_AUTOVACUUM
74996 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
74997 if( rc ){
74998 return rc;
74999 }
75000#else
75001 if( pBt->autoVacuum ){
75002 Pgno pgnoMove; /* Move a page here to make room for the root-page */
75003 MemPage *pPageMove; /* The page to move to. */
75004
75005 /* Creating a new table may probably require moving an existing database
75006 ** to make room for the new tables root page. In case this page turns
75007 ** out to be an overflow page, delete all overflow page-map caches
75008 ** held by open cursors.
75009 */
75010 invalidateAllOverflowCache(pBt);
75011
75012 /* Read the value of meta[3] from the database to determine where the
75013 ** root page of the new table should go. meta[3] is the largest root-page
75014 ** created so far, so the new root-page is (meta[3]+1).
75015 */
75016 sqlite3BtreeGetMeta(pBtree: p, BTREE_LARGEST_ROOT_PAGE, pValue: &pgnoRoot);
75017 if( pgnoRoot>btreePagecount(pBt) ){
75018 return SQLITE_CORRUPT_BKPT;
75019 }
75020 pgnoRoot++;
75021
75022 /* The new root-page may not be allocated on a pointer-map page, or the
75023 ** PENDING_BYTE page.
75024 */
75025 while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
75026 pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
75027 pgnoRoot++;
75028 }
75029 assert( pgnoRoot>=3 );
75030
75031 /* Allocate a page. The page that currently resides at pgnoRoot will
75032 ** be moved to the allocated page (unless the allocated page happens
75033 ** to reside at pgnoRoot).
75034 */
75035 rc = allocateBtreePage(pBt, ppPage: &pPageMove, pPgno: &pgnoMove, nearby: pgnoRoot, BTALLOC_EXACT);
75036 if( rc!=SQLITE_OK ){
75037 return rc;
75038 }
75039
75040 if( pgnoMove!=pgnoRoot ){
75041 /* pgnoRoot is the page that will be used for the root-page of
75042 ** the new table (assuming an error did not occur). But we were
75043 ** allocated pgnoMove. If required (i.e. if it was not allocated
75044 ** by extending the file), the current page at position pgnoMove
75045 ** is already journaled.
75046 */
75047 u8 eType = 0;
75048 Pgno iPtrPage = 0;
75049
75050 /* Save the positions of any open cursors. This is required in
75051 ** case they are holding a reference to an xFetch reference
75052 ** corresponding to page pgnoRoot. */
75053 rc = saveAllCursors(pBt, iRoot: 0, pExcept: 0);
75054 releasePage(pPage: pPageMove);
75055 if( rc!=SQLITE_OK ){
75056 return rc;
75057 }
75058
75059 /* Move the page currently at pgnoRoot to pgnoMove. */
75060 rc = btreeGetPage(pBt, pgno: pgnoRoot, ppPage: &pRoot, flags: 0);
75061 if( rc!=SQLITE_OK ){
75062 return rc;
75063 }
75064 rc = ptrmapGet(pBt, key: pgnoRoot, pEType: &eType, pPgno: &iPtrPage);
75065 if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
75066 rc = SQLITE_CORRUPT_BKPT;
75067 }
75068 if( rc!=SQLITE_OK ){
75069 releasePage(pPage: pRoot);
75070 return rc;
75071 }
75072 assert( eType!=PTRMAP_ROOTPAGE );
75073 assert( eType!=PTRMAP_FREEPAGE );
75074 rc = relocatePage(pBt, pDbPage: pRoot, eType, iPtrPage, iFreePage: pgnoMove, isCommit: 0);
75075 releasePage(pPage: pRoot);
75076
75077 /* Obtain the page at pgnoRoot */
75078 if( rc!=SQLITE_OK ){
75079 return rc;
75080 }
75081 rc = btreeGetPage(pBt, pgno: pgnoRoot, ppPage: &pRoot, flags: 0);
75082 if( rc!=SQLITE_OK ){
75083 return rc;
75084 }
75085 rc = sqlite3PagerWrite(pPg: pRoot->pDbPage);
75086 if( rc!=SQLITE_OK ){
75087 releasePage(pPage: pRoot);
75088 return rc;
75089 }
75090 }else{
75091 pRoot = pPageMove;
75092 }
75093
75094 /* Update the pointer-map and meta-data with the new root-page number. */
75095 ptrmapPut(pBt, key: pgnoRoot, PTRMAP_ROOTPAGE, parent: 0, pRC: &rc);
75096 if( rc ){
75097 releasePage(pPage: pRoot);
75098 return rc;
75099 }
75100
75101 /* When the new root page was allocated, page 1 was made writable in
75102 ** order either to increase the database filesize, or to decrement the
75103 ** freelist count. Hence, the sqlite3BtreeUpdateMeta() call cannot fail.
75104 */
75105 assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
75106 rc = sqlite3BtreeUpdateMeta(p, idx: 4, value: pgnoRoot);
75107 if( NEVER(rc) ){
75108 releasePage(pPage: pRoot);
75109 return rc;
75110 }
75111
75112 }else{
75113 rc = allocateBtreePage(pBt, ppPage: &pRoot, pPgno: &pgnoRoot, nearby: 1, eMode: 0);
75114 if( rc ) return rc;
75115 }
75116#endif
75117 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
75118 if( createTabFlags & BTREE_INTKEY ){
75119 ptfFlags = PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF;
75120 }else{
75121 ptfFlags = PTF_ZERODATA | PTF_LEAF;
75122 }
75123 zeroPage(pPage: pRoot, flags: ptfFlags);
75124 sqlite3PagerUnref(pPg: pRoot->pDbPage);
75125 assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
75126 *piTable = pgnoRoot;
75127 return SQLITE_OK;
75128}
75129SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree *p, Pgno *piTable, int flags){
75130 int rc;
75131 sqlite3BtreeEnter(p);
75132 rc = btreeCreateTable(p, piTable, createTabFlags: flags);
75133 sqlite3BtreeLeave(p);
75134 return rc;
75135}
75136
75137/*
75138** Erase the given database page and all its children. Return
75139** the page to the freelist.
75140*/
75141static int clearDatabasePage(
75142 BtShared *pBt, /* The BTree that contains the table */
75143 Pgno pgno, /* Page number to clear */
75144 int freePageFlag, /* Deallocate page if true */
75145 i64 *pnChange /* Add number of Cells freed to this counter */
75146){
75147 MemPage *pPage;
75148 int rc;
75149 unsigned char *pCell;
75150 int i;
75151 int hdr;
75152 CellInfo info;
75153
75154 assert( sqlite3_mutex_held(pBt->mutex) );
75155 if( pgno>btreePagecount(pBt) ){
75156 return SQLITE_CORRUPT_BKPT;
75157 }
75158 rc = getAndInitPage(pBt, pgno, ppPage: &pPage, pCur: 0, bReadOnly: 0);
75159 if( rc ) return rc;
75160 if( (pBt->openFlags & BTREE_SINGLE)==0
75161 && sqlite3PagerPageRefcount(pPage: pPage->pDbPage)!=1
75162 ){
75163 rc = SQLITE_CORRUPT_BKPT;
75164 goto cleardatabasepage_out;
75165 }
75166 hdr = pPage->hdrOffset;
75167 for(i=0; i<pPage->nCell; i++){
75168 pCell = findCell(pPage, i);
75169 if( !pPage->leaf ){
75170 rc = clearDatabasePage(pBt, get4byte(p: pCell), freePageFlag: 1, pnChange);
75171 if( rc ) goto cleardatabasepage_out;
75172 }
75173 BTREE_CLEAR_CELL(rc, pPage, pCell, info);
75174 if( rc ) goto cleardatabasepage_out;
75175 }
75176 if( !pPage->leaf ){
75177 rc = clearDatabasePage(pBt, get4byte(p: &pPage->aData[hdr+8]), freePageFlag: 1, pnChange);
75178 if( rc ) goto cleardatabasepage_out;
75179 if( pPage->intKey ) pnChange = 0;
75180 }
75181 if( pnChange ){
75182 testcase( !pPage->intKey );
75183 *pnChange += pPage->nCell;
75184 }
75185 if( freePageFlag ){
75186 freePage(pPage, pRC: &rc);
75187 }else if( (rc = sqlite3PagerWrite(pPg: pPage->pDbPage))==0 ){
75188 zeroPage(pPage, flags: pPage->aData[hdr] | PTF_LEAF);
75189 }
75190
75191cleardatabasepage_out:
75192 releasePage(pPage);
75193 return rc;
75194}
75195
75196/*
75197** Delete all information from a single table in the database. iTable is
75198** the page number of the root of the table. After this routine returns,
75199** the root page is empty, but still exists.
75200**
75201** This routine will fail with SQLITE_LOCKED if there are any open
75202** read cursors on the table. Open write cursors are moved to the
75203** root of the table.
75204**
75205** If pnChange is not NULL, then the integer value pointed to by pnChange
75206** is incremented by the number of entries in the table.
75207*/
75208SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree *p, int iTable, i64 *pnChange){
75209 int rc;
75210 BtShared *pBt = p->pBt;
75211 sqlite3BtreeEnter(p);
75212 assert( p->inTrans==TRANS_WRITE );
75213
75214 rc = saveAllCursors(pBt, iRoot: (Pgno)iTable, pExcept: 0);
75215
75216 if( SQLITE_OK==rc ){
75217 /* Invalidate all incrblob cursors open on table iTable (assuming iTable
75218 ** is the root of a table b-tree - if it is not, the following call is
75219 ** a no-op). */
75220 if( p->hasIncrblobCur ){
75221 invalidateIncrblobCursors(pBtree: p, pgnoRoot: (Pgno)iTable, iRow: 0, isClearTable: 1);
75222 }
75223 rc = clearDatabasePage(pBt, pgno: (Pgno)iTable, freePageFlag: 0, pnChange);
75224 }
75225 sqlite3BtreeLeave(p);
75226 return rc;
75227}
75228
75229/*
75230** Delete all information from the single table that pCur is open on.
75231**
75232** This routine only work for pCur on an ephemeral table.
75233*/
75234SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor *pCur){
75235 return sqlite3BtreeClearTable(p: pCur->pBtree, iTable: pCur->pgnoRoot, pnChange: 0);
75236}
75237
75238/*
75239** Erase all information in a table and add the root of the table to
75240** the freelist. Except, the root of the principle table (the one on
75241** page 1) is never added to the freelist.
75242**
75243** This routine will fail with SQLITE_LOCKED if there are any open
75244** cursors on the table.
75245**
75246** If AUTOVACUUM is enabled and the page at iTable is not the last
75247** root page in the database file, then the last root page
75248** in the database file is moved into the slot formerly occupied by
75249** iTable and that last slot formerly occupied by the last root page
75250** is added to the freelist instead of iTable. In this say, all
75251** root pages are kept at the beginning of the database file, which
75252** is necessary for AUTOVACUUM to work right. *piMoved is set to the
75253** page number that used to be the last root page in the file before
75254** the move. If no page gets moved, *piMoved is set to 0.
75255** The last root page is recorded in meta[3] and the value of
75256** meta[3] is updated by this procedure.
75257*/
75258static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
75259 int rc;
75260 MemPage *pPage = 0;
75261 BtShared *pBt = p->pBt;
75262
75263 assert( sqlite3BtreeHoldsMutex(p) );
75264 assert( p->inTrans==TRANS_WRITE );
75265 assert( iTable>=2 );
75266 if( iTable>btreePagecount(pBt) ){
75267 return SQLITE_CORRUPT_BKPT;
75268 }
75269
75270 rc = sqlite3BtreeClearTable(p, iTable, pnChange: 0);
75271 if( rc ) return rc;
75272 rc = btreeGetPage(pBt, pgno: (Pgno)iTable, ppPage: &pPage, flags: 0);
75273 if( NEVER(rc) ){
75274 releasePage(pPage);
75275 return rc;
75276 }
75277
75278 *piMoved = 0;
75279
75280#ifdef SQLITE_OMIT_AUTOVACUUM
75281 freePage(pPage, &rc);
75282 releasePage(pPage);
75283#else
75284 if( pBt->autoVacuum ){
75285 Pgno maxRootPgno;
75286 sqlite3BtreeGetMeta(pBtree: p, BTREE_LARGEST_ROOT_PAGE, pValue: &maxRootPgno);
75287
75288 if( iTable==maxRootPgno ){
75289 /* If the table being dropped is the table with the largest root-page
75290 ** number in the database, put the root page on the free list.
75291 */
75292 freePage(pPage, pRC: &rc);
75293 releasePage(pPage);
75294 if( rc!=SQLITE_OK ){
75295 return rc;
75296 }
75297 }else{
75298 /* The table being dropped does not have the largest root-page
75299 ** number in the database. So move the page that does into the
75300 ** gap left by the deleted root-page.
75301 */
75302 MemPage *pMove;
75303 releasePage(pPage);
75304 rc = btreeGetPage(pBt, pgno: maxRootPgno, ppPage: &pMove, flags: 0);
75305 if( rc!=SQLITE_OK ){
75306 return rc;
75307 }
75308 rc = relocatePage(pBt, pDbPage: pMove, PTRMAP_ROOTPAGE, iPtrPage: 0, iFreePage: iTable, isCommit: 0);
75309 releasePage(pPage: pMove);
75310 if( rc!=SQLITE_OK ){
75311 return rc;
75312 }
75313 pMove = 0;
75314 rc = btreeGetPage(pBt, pgno: maxRootPgno, ppPage: &pMove, flags: 0);
75315 freePage(pPage: pMove, pRC: &rc);
75316 releasePage(pPage: pMove);
75317 if( rc!=SQLITE_OK ){
75318 return rc;
75319 }
75320 *piMoved = maxRootPgno;
75321 }
75322
75323 /* Set the new 'max-root-page' value in the database header. This
75324 ** is the old value less one, less one more if that happens to
75325 ** be a root-page number, less one again if that is the
75326 ** PENDING_BYTE_PAGE.
75327 */
75328 maxRootPgno--;
75329 while( maxRootPgno==PENDING_BYTE_PAGE(pBt)
75330 || PTRMAP_ISPAGE(pBt, maxRootPgno) ){
75331 maxRootPgno--;
75332 }
75333 assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
75334
75335 rc = sqlite3BtreeUpdateMeta(p, idx: 4, value: maxRootPgno);
75336 }else{
75337 freePage(pPage, pRC: &rc);
75338 releasePage(pPage);
75339 }
75340#endif
75341 return rc;
75342}
75343SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
75344 int rc;
75345 sqlite3BtreeEnter(p);
75346 rc = btreeDropTable(p, iTable, piMoved);
75347 sqlite3BtreeLeave(p);
75348 return rc;
75349}
75350
75351
75352/*
75353** This function may only be called if the b-tree connection already
75354** has a read or write transaction open on the database.
75355**
75356** Read the meta-information out of a database file. Meta[0]
75357** is the number of free pages currently in the database. Meta[1]
75358** through meta[15] are available for use by higher layers. Meta[0]
75359** is read-only, the others are read/write.
75360**
75361** The schema layer numbers meta values differently. At the schema
75362** layer (and the SetCookie and ReadCookie opcodes) the number of
75363** free pages is not visible. So Cookie[0] is the same as Meta[1].
75364**
75365** This routine treats Meta[BTREE_DATA_VERSION] as a special case. Instead
75366** of reading the value out of the header, it instead loads the "DataVersion"
75367** from the pager. The BTREE_DATA_VERSION value is not actually stored in the
75368** database file. It is a number computed by the pager. But its access
75369** pattern is the same as header meta values, and so it is convenient to
75370** read it from this routine.
75371*/
75372SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
75373 BtShared *pBt = p->pBt;
75374
75375 sqlite3BtreeEnter(p);
75376 assert( p->inTrans>TRANS_NONE );
75377 assert( SQLITE_OK==querySharedCacheTableLock(p, SCHEMA_ROOT, READ_LOCK) );
75378 assert( pBt->pPage1 );
75379 assert( idx>=0 && idx<=15 );
75380
75381 if( idx==BTREE_DATA_VERSION ){
75382 *pMeta = sqlite3PagerDataVersion(pPager: pBt->pPager) + p->iBDataVersion;
75383 }else{
75384 *pMeta = get4byte(p: &pBt->pPage1->aData[36 + idx*4]);
75385 }
75386
75387 /* If auto-vacuum is disabled in this build and this is an auto-vacuum
75388 ** database, mark the database as read-only. */
75389#ifdef SQLITE_OMIT_AUTOVACUUM
75390 if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ){
75391 pBt->btsFlags |= BTS_READ_ONLY;
75392 }
75393#endif
75394
75395 sqlite3BtreeLeave(p);
75396}
75397
75398/*
75399** Write meta-information back into the database. Meta[0] is
75400** read-only and may not be written.
75401*/
75402SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
75403 BtShared *pBt = p->pBt;
75404 unsigned char *pP1;
75405 int rc;
75406 assert( idx>=1 && idx<=15 );
75407 sqlite3BtreeEnter(p);
75408 assert( p->inTrans==TRANS_WRITE );
75409 assert( pBt->pPage1!=0 );
75410 pP1 = pBt->pPage1->aData;
75411 rc = sqlite3PagerWrite(pPg: pBt->pPage1->pDbPage);
75412 if( rc==SQLITE_OK ){
75413 put4byte(p: &pP1[36 + idx*4], v: iMeta);
75414#ifndef SQLITE_OMIT_AUTOVACUUM
75415 if( idx==BTREE_INCR_VACUUM ){
75416 assert( pBt->autoVacuum || iMeta==0 );
75417 assert( iMeta==0 || iMeta==1 );
75418 pBt->incrVacuum = (u8)iMeta;
75419 }
75420#endif
75421 }
75422 sqlite3BtreeLeave(p);
75423 return rc;
75424}
75425
75426/*
75427** The first argument, pCur, is a cursor opened on some b-tree. Count the
75428** number of entries in the b-tree and write the result to *pnEntry.
75429**
75430** SQLITE_OK is returned if the operation is successfully executed.
75431** Otherwise, if an error is encountered (i.e. an IO error or database
75432** corruption) an SQLite error code is returned.
75433*/
75434SQLITE_PRIVATE int sqlite3BtreeCount(sqlite3 *db, BtCursor *pCur, i64 *pnEntry){
75435 i64 nEntry = 0; /* Value to return in *pnEntry */
75436 int rc; /* Return code */
75437
75438 rc = moveToRoot(pCur);
75439 if( rc==SQLITE_EMPTY ){
75440 *pnEntry = 0;
75441 return SQLITE_OK;
75442 }
75443
75444 /* Unless an error occurs, the following loop runs one iteration for each
75445 ** page in the B-Tree structure (not including overflow pages).
75446 */
75447 while( rc==SQLITE_OK && !AtomicLoad(&db->u1.isInterrupted) ){
75448 int iIdx; /* Index of child node in parent */
75449 MemPage *pPage; /* Current page of the b-tree */
75450
75451 /* If this is a leaf page or the tree is not an int-key tree, then
75452 ** this page contains countable entries. Increment the entry counter
75453 ** accordingly.
75454 */
75455 pPage = pCur->pPage;
75456 if( pPage->leaf || !pPage->intKey ){
75457 nEntry += pPage->nCell;
75458 }
75459
75460 /* pPage is a leaf node. This loop navigates the cursor so that it
75461 ** points to the first interior cell that it points to the parent of
75462 ** the next page in the tree that has not yet been visited. The
75463 ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
75464 ** of the page, or to the number of cells in the page if the next page
75465 ** to visit is the right-child of its parent.
75466 **
75467 ** If all pages in the tree have been visited, return SQLITE_OK to the
75468 ** caller.
75469 */
75470 if( pPage->leaf ){
75471 do {
75472 if( pCur->iPage==0 ){
75473 /* All pages of the b-tree have been visited. Return successfully. */
75474 *pnEntry = nEntry;
75475 return moveToRoot(pCur);
75476 }
75477 moveToParent(pCur);
75478 }while ( pCur->ix>=pCur->pPage->nCell );
75479
75480 pCur->ix++;
75481 pPage = pCur->pPage;
75482 }
75483
75484 /* Descend to the child node of the cell that the cursor currently
75485 ** points at. This is the right-child if (iIdx==pPage->nCell).
75486 */
75487 iIdx = pCur->ix;
75488 if( iIdx==pPage->nCell ){
75489 rc = moveToChild(pCur, get4byte(p: &pPage->aData[pPage->hdrOffset+8]));
75490 }else{
75491 rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx)));
75492 }
75493 }
75494
75495 /* An error has occurred. Return an error code. */
75496 return rc;
75497}
75498
75499/*
75500** Return the pager associated with a BTree. This routine is used for
75501** testing and debugging only.
75502*/
75503SQLITE_PRIVATE Pager *sqlite3BtreePager(Btree *p){
75504 return p->pBt->pPager;
75505}
75506
75507#ifndef SQLITE_OMIT_INTEGRITY_CHECK
75508/*
75509** Append a message to the error message string.
75510*/
75511static void checkAppendMsg(
75512 IntegrityCk *pCheck,
75513 const char *zFormat,
75514 ...
75515){
75516 va_list ap;
75517 if( !pCheck->mxErr ) return;
75518 pCheck->mxErr--;
75519 pCheck->nErr++;
75520 va_start(ap, zFormat);
75521 if( pCheck->errMsg.nChar ){
75522 sqlite3_str_append(p: &pCheck->errMsg, z: "\n", N: 1);
75523 }
75524 if( pCheck->zPfx ){
75525 sqlite3_str_appendf(p: &pCheck->errMsg, zFormat: pCheck->zPfx, pCheck->v1, pCheck->v2);
75526 }
75527 sqlite3_str_vappendf(pAccum: &pCheck->errMsg, fmt: zFormat, ap);
75528 va_end(ap);
75529 if( pCheck->errMsg.accError==SQLITE_NOMEM ){
75530 pCheck->bOomFault = 1;
75531 }
75532}
75533#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
75534
75535#ifndef SQLITE_OMIT_INTEGRITY_CHECK
75536
75537/*
75538** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
75539** corresponds to page iPg is already set.
75540*/
75541static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){
75542 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
75543 return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
75544}
75545
75546/*
75547** Set the bit in the IntegrityCk.aPgRef[] array that corresponds to page iPg.
75548*/
75549static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){
75550 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
75551 pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
75552}
75553
75554
75555/*
75556** Add 1 to the reference count for page iPage. If this is the second
75557** reference to the page, add an error message to pCheck->zErrMsg.
75558** Return 1 if there are 2 or more references to the page and 0 if
75559** if this is the first reference to the page.
75560**
75561** Also check that the page number is in bounds.
75562*/
75563static int checkRef(IntegrityCk *pCheck, Pgno iPage){
75564 if( iPage>pCheck->nPage || iPage==0 ){
75565 checkAppendMsg(pCheck, zFormat: "invalid page number %d", iPage);
75566 return 1;
75567 }
75568 if( getPageReferenced(pCheck, iPg: iPage) ){
75569 checkAppendMsg(pCheck, zFormat: "2nd reference to page %d", iPage);
75570 return 1;
75571 }
75572 if( AtomicLoad(&pCheck->db->u1.isInterrupted) ) return 1;
75573 setPageReferenced(pCheck, iPg: iPage);
75574 return 0;
75575}
75576
75577#ifndef SQLITE_OMIT_AUTOVACUUM
75578/*
75579** Check that the entry in the pointer-map for page iChild maps to
75580** page iParent, pointer type ptrType. If not, append an error message
75581** to pCheck.
75582*/
75583static void checkPtrmap(
75584 IntegrityCk *pCheck, /* Integrity check context */
75585 Pgno iChild, /* Child page number */
75586 u8 eType, /* Expected pointer map type */
75587 Pgno iParent /* Expected pointer map parent page number */
75588){
75589 int rc;
75590 u8 ePtrmapType;
75591 Pgno iPtrmapParent;
75592
75593 rc = ptrmapGet(pBt: pCheck->pBt, key: iChild, pEType: &ePtrmapType, pPgno: &iPtrmapParent);
75594 if( rc!=SQLITE_OK ){
75595 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->bOomFault = 1;
75596 checkAppendMsg(pCheck, zFormat: "Failed to read ptrmap key=%d", iChild);
75597 return;
75598 }
75599
75600 if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
75601 checkAppendMsg(pCheck,
75602 zFormat: "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)",
75603 iChild, eType, iParent, ePtrmapType, iPtrmapParent);
75604 }
75605}
75606#endif
75607
75608/*
75609** Check the integrity of the freelist or of an overflow page list.
75610** Verify that the number of pages on the list is N.
75611*/
75612static void checkList(
75613 IntegrityCk *pCheck, /* Integrity checking context */
75614 int isFreeList, /* True for a freelist. False for overflow page list */
75615 Pgno iPage, /* Page number for first page in the list */
75616 u32 N /* Expected number of pages in the list */
75617){
75618 int i;
75619 u32 expected = N;
75620 int nErrAtStart = pCheck->nErr;
75621 while( iPage!=0 && pCheck->mxErr ){
75622 DbPage *pOvflPage;
75623 unsigned char *pOvflData;
75624 if( checkRef(pCheck, iPage) ) break;
75625 N--;
75626 if( sqlite3PagerGet(pPager: pCheck->pPager, pgno: (Pgno)iPage, ppPage: &pOvflPage, flags: 0) ){
75627 checkAppendMsg(pCheck, zFormat: "failed to get page %d", iPage);
75628 break;
75629 }
75630 pOvflData = (unsigned char *)sqlite3PagerGetData(pPg: pOvflPage);
75631 if( isFreeList ){
75632 u32 n = (u32)get4byte(p: &pOvflData[4]);
75633#ifndef SQLITE_OMIT_AUTOVACUUM
75634 if( pCheck->pBt->autoVacuum ){
75635 checkPtrmap(pCheck, iChild: iPage, PTRMAP_FREEPAGE, iParent: 0);
75636 }
75637#endif
75638 if( n>pCheck->pBt->usableSize/4-2 ){
75639 checkAppendMsg(pCheck,
75640 zFormat: "freelist leaf count too big on page %d", iPage);
75641 N--;
75642 }else{
75643 for(i=0; i<(int)n; i++){
75644 Pgno iFreePage = get4byte(p: &pOvflData[8+i*4]);
75645#ifndef SQLITE_OMIT_AUTOVACUUM
75646 if( pCheck->pBt->autoVacuum ){
75647 checkPtrmap(pCheck, iChild: iFreePage, PTRMAP_FREEPAGE, iParent: 0);
75648 }
75649#endif
75650 checkRef(pCheck, iPage: iFreePage);
75651 }
75652 N -= n;
75653 }
75654 }
75655#ifndef SQLITE_OMIT_AUTOVACUUM
75656 else{
75657 /* If this database supports auto-vacuum and iPage is not the last
75658 ** page in this overflow list, check that the pointer-map entry for
75659 ** the following page matches iPage.
75660 */
75661 if( pCheck->pBt->autoVacuum && N>0 ){
75662 i = get4byte(p: pOvflData);
75663 checkPtrmap(pCheck, iChild: i, PTRMAP_OVERFLOW2, iParent: iPage);
75664 }
75665 }
75666#endif
75667 iPage = get4byte(p: pOvflData);
75668 sqlite3PagerUnref(pPg: pOvflPage);
75669 }
75670 if( N && nErrAtStart==pCheck->nErr ){
75671 checkAppendMsg(pCheck,
75672 zFormat: "%s is %d but should be %d",
75673 isFreeList ? "size" : "overflow list length",
75674 expected-N, expected);
75675 }
75676}
75677#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
75678
75679/*
75680** An implementation of a min-heap.
75681**
75682** aHeap[0] is the number of elements on the heap. aHeap[1] is the
75683** root element. The daughter nodes of aHeap[N] are aHeap[N*2]
75684** and aHeap[N*2+1].
75685**
75686** The heap property is this: Every node is less than or equal to both
75687** of its daughter nodes. A consequence of the heap property is that the
75688** root node aHeap[1] is always the minimum value currently in the heap.
75689**
75690** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto
75691** the heap, preserving the heap property. The btreeHeapPull() routine
75692** removes the root element from the heap (the minimum value in the heap)
75693** and then moves other nodes around as necessary to preserve the heap
75694** property.
75695**
75696** This heap is used for cell overlap and coverage testing. Each u32
75697** entry represents the span of a cell or freeblock on a btree page.
75698** The upper 16 bits are the index of the first byte of a range and the
75699** lower 16 bits are the index of the last byte of that range.
75700*/
75701static void btreeHeapInsert(u32 *aHeap, u32 x){
75702 u32 j, i = ++aHeap[0];
75703 aHeap[i] = x;
75704 while( (j = i/2)>0 && aHeap[j]>aHeap[i] ){
75705 x = aHeap[j];
75706 aHeap[j] = aHeap[i];
75707 aHeap[i] = x;
75708 i = j;
75709 }
75710}
75711static int btreeHeapPull(u32 *aHeap, u32 *pOut){
75712 u32 j, i, x;
75713 if( (x = aHeap[0])==0 ) return 0;
75714 *pOut = aHeap[1];
75715 aHeap[1] = aHeap[x];
75716 aHeap[x] = 0xffffffff;
75717 aHeap[0]--;
75718 i = 1;
75719 while( (j = i*2)<=aHeap[0] ){
75720 if( aHeap[j]>aHeap[j+1] ) j++;
75721 if( aHeap[i]<aHeap[j] ) break;
75722 x = aHeap[i];
75723 aHeap[i] = aHeap[j];
75724 aHeap[j] = x;
75725 i = j;
75726 }
75727 return 1;
75728}
75729
75730#ifndef SQLITE_OMIT_INTEGRITY_CHECK
75731/*
75732** Do various sanity checks on a single page of a tree. Return
75733** the tree depth. Root pages return 0. Parents of root pages
75734** return 1, and so forth.
75735**
75736** These checks are done:
75737**
75738** 1. Make sure that cells and freeblocks do not overlap
75739** but combine to completely cover the page.
75740** 2. Make sure integer cell keys are in order.
75741** 3. Check the integrity of overflow pages.
75742** 4. Recursively call checkTreePage on all children.
75743** 5. Verify that the depth of all children is the same.
75744*/
75745static int checkTreePage(
75746 IntegrityCk *pCheck, /* Context for the sanity check */
75747 Pgno iPage, /* Page number of the page to check */
75748 i64 *piMinKey, /* Write minimum integer primary key here */
75749 i64 maxKey /* Error if integer primary key greater than this */
75750){
75751 MemPage *pPage = 0; /* The page being analyzed */
75752 int i; /* Loop counter */
75753 int rc; /* Result code from subroutine call */
75754 int depth = -1, d2; /* Depth of a subtree */
75755 int pgno; /* Page number */
75756 int nFrag; /* Number of fragmented bytes on the page */
75757 int hdr; /* Offset to the page header */
75758 int cellStart; /* Offset to the start of the cell pointer array */
75759 int nCell; /* Number of cells */
75760 int doCoverageCheck = 1; /* True if cell coverage checking should be done */
75761 int keyCanBeEqual = 1; /* True if IPK can be equal to maxKey
75762 ** False if IPK must be strictly less than maxKey */
75763 u8 *data; /* Page content */
75764 u8 *pCell; /* Cell content */
75765 u8 *pCellIdx; /* Next element of the cell pointer array */
75766 BtShared *pBt; /* The BtShared object that owns pPage */
75767 u32 pc; /* Address of a cell */
75768 u32 usableSize; /* Usable size of the page */
75769 u32 contentOffset; /* Offset to the start of the cell content area */
75770 u32 *heap = 0; /* Min-heap used for checking cell coverage */
75771 u32 x, prev = 0; /* Next and previous entry on the min-heap */
75772 const char *saved_zPfx = pCheck->zPfx;
75773 int saved_v1 = pCheck->v1;
75774 int saved_v2 = pCheck->v2;
75775 u8 savedIsInit = 0;
75776
75777 /* Check that the page exists
75778 */
75779 pBt = pCheck->pBt;
75780 usableSize = pBt->usableSize;
75781 if( iPage==0 ) return 0;
75782 if( checkRef(pCheck, iPage) ) return 0;
75783 pCheck->zPfx = "Page %u: ";
75784 pCheck->v1 = iPage;
75785 if( (rc = btreeGetPage(pBt, pgno: iPage, ppPage: &pPage, flags: 0))!=0 ){
75786 checkAppendMsg(pCheck,
75787 zFormat: "unable to get the page. error code=%d", rc);
75788 goto end_of_check;
75789 }
75790
75791 /* Clear MemPage.isInit to make sure the corruption detection code in
75792 ** btreeInitPage() is executed. */
75793 savedIsInit = pPage->isInit;
75794 pPage->isInit = 0;
75795 if( (rc = btreeInitPage(pPage))!=0 ){
75796 assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */
75797 checkAppendMsg(pCheck,
75798 zFormat: "btreeInitPage() returns error code %d", rc);
75799 goto end_of_check;
75800 }
75801 if( (rc = btreeComputeFreeSpace(pPage))!=0 ){
75802 assert( rc==SQLITE_CORRUPT );
75803 checkAppendMsg(pCheck, zFormat: "free space corruption", rc);
75804 goto end_of_check;
75805 }
75806 data = pPage->aData;
75807 hdr = pPage->hdrOffset;
75808
75809 /* Set up for cell analysis */
75810 pCheck->zPfx = "On tree page %u cell %d: ";
75811 contentOffset = get2byteNotZero(&data[hdr+5]);
75812 assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */
75813
75814 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
75815 ** number of cells on the page. */
75816 nCell = get2byte(&data[hdr+3]);
75817 assert( pPage->nCell==nCell );
75818
75819 /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
75820 ** immediately follows the b-tree page header. */
75821 cellStart = hdr + 12 - 4*pPage->leaf;
75822 assert( pPage->aCellIdx==&data[cellStart] );
75823 pCellIdx = &data[cellStart + 2*(nCell-1)];
75824
75825 if( !pPage->leaf ){
75826 /* Analyze the right-child page of internal pages */
75827 pgno = get4byte(p: &data[hdr+8]);
75828#ifndef SQLITE_OMIT_AUTOVACUUM
75829 if( pBt->autoVacuum ){
75830 pCheck->zPfx = "On page %u at right child: ";
75831 checkPtrmap(pCheck, iChild: pgno, PTRMAP_BTREE, iParent: iPage);
75832 }
75833#endif
75834 depth = checkTreePage(pCheck, iPage: pgno, piMinKey: &maxKey, maxKey);
75835 keyCanBeEqual = 0;
75836 }else{
75837 /* For leaf pages, the coverage check will occur in the same loop
75838 ** as the other cell checks, so initialize the heap. */
75839 heap = pCheck->heap;
75840 heap[0] = 0;
75841 }
75842
75843 /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
75844 ** integer offsets to the cell contents. */
75845 for(i=nCell-1; i>=0 && pCheck->mxErr; i--){
75846 CellInfo info;
75847
75848 /* Check cell size */
75849 pCheck->v2 = i;
75850 assert( pCellIdx==&data[cellStart + i*2] );
75851 pc = get2byteAligned(pCellIdx);
75852 pCellIdx -= 2;
75853 if( pc<contentOffset || pc>usableSize-4 ){
75854 checkAppendMsg(pCheck, zFormat: "Offset %d out of range %d..%d",
75855 pc, contentOffset, usableSize-4);
75856 doCoverageCheck = 0;
75857 continue;
75858 }
75859 pCell = &data[pc];
75860 pPage->xParseCell(pPage, pCell, &info);
75861 if( pc+info.nSize>usableSize ){
75862 checkAppendMsg(pCheck, zFormat: "Extends off end of page");
75863 doCoverageCheck = 0;
75864 continue;
75865 }
75866
75867 /* Check for integer primary key out of range */
75868 if( pPage->intKey ){
75869 if( keyCanBeEqual ? (info.nKey > maxKey) : (info.nKey >= maxKey) ){
75870 checkAppendMsg(pCheck, zFormat: "Rowid %lld out of order", info.nKey);
75871 }
75872 maxKey = info.nKey;
75873 keyCanBeEqual = 0; /* Only the first key on the page may ==maxKey */
75874 }
75875
75876 /* Check the content overflow list */
75877 if( info.nPayload>info.nLocal ){
75878 u32 nPage; /* Number of pages on the overflow chain */
75879 Pgno pgnoOvfl; /* First page of the overflow chain */
75880 assert( pc + info.nSize - 4 <= usableSize );
75881 nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
75882 pgnoOvfl = get4byte(p: &pCell[info.nSize - 4]);
75883#ifndef SQLITE_OMIT_AUTOVACUUM
75884 if( pBt->autoVacuum ){
75885 checkPtrmap(pCheck, iChild: pgnoOvfl, PTRMAP_OVERFLOW1, iParent: iPage);
75886 }
75887#endif
75888 checkList(pCheck, isFreeList: 0, iPage: pgnoOvfl, N: nPage);
75889 }
75890
75891 if( !pPage->leaf ){
75892 /* Check sanity of left child page for internal pages */
75893 pgno = get4byte(p: pCell);
75894#ifndef SQLITE_OMIT_AUTOVACUUM
75895 if( pBt->autoVacuum ){
75896 checkPtrmap(pCheck, iChild: pgno, PTRMAP_BTREE, iParent: iPage);
75897 }
75898#endif
75899 d2 = checkTreePage(pCheck, iPage: pgno, piMinKey: &maxKey, maxKey);
75900 keyCanBeEqual = 0;
75901 if( d2!=depth ){
75902 checkAppendMsg(pCheck, zFormat: "Child page depth differs");
75903 depth = d2;
75904 }
75905 }else{
75906 /* Populate the coverage-checking heap for leaf pages */
75907 btreeHeapInsert(aHeap: heap, x: (pc<<16)|(pc+info.nSize-1));
75908 }
75909 }
75910 *piMinKey = maxKey;
75911
75912 /* Check for complete coverage of the page
75913 */
75914 pCheck->zPfx = 0;
75915 if( doCoverageCheck && pCheck->mxErr>0 ){
75916 /* For leaf pages, the min-heap has already been initialized and the
75917 ** cells have already been inserted. But for internal pages, that has
75918 ** not yet been done, so do it now */
75919 if( !pPage->leaf ){
75920 heap = pCheck->heap;
75921 heap[0] = 0;
75922 for(i=nCell-1; i>=0; i--){
75923 u32 size;
75924 pc = get2byteAligned(&data[cellStart+i*2]);
75925 size = pPage->xCellSize(pPage, &data[pc]);
75926 btreeHeapInsert(aHeap: heap, x: (pc<<16)|(pc+size-1));
75927 }
75928 }
75929 /* Add the freeblocks to the min-heap
75930 **
75931 ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
75932 ** is the offset of the first freeblock, or zero if there are no
75933 ** freeblocks on the page.
75934 */
75935 i = get2byte(&data[hdr+1]);
75936 while( i>0 ){
75937 int size, j;
75938 assert( (u32)i<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
75939 size = get2byte(&data[i+2]);
75940 assert( (u32)(i+size)<=usableSize ); /* due to btreeComputeFreeSpace() */
75941 btreeHeapInsert(aHeap: heap, x: (((u32)i)<<16)|(i+size-1));
75942 /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
75943 ** big-endian integer which is the offset in the b-tree page of the next
75944 ** freeblock in the chain, or zero if the freeblock is the last on the
75945 ** chain. */
75946 j = get2byte(&data[i]);
75947 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
75948 ** increasing offset. */
75949 assert( j==0 || j>i+size ); /* Enforced by btreeComputeFreeSpace() */
75950 assert( (u32)j<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
75951 i = j;
75952 }
75953 /* Analyze the min-heap looking for overlap between cells and/or
75954 ** freeblocks, and counting the number of untracked bytes in nFrag.
75955 **
75956 ** Each min-heap entry is of the form: (start_address<<16)|end_address.
75957 ** There is an implied first entry the covers the page header, the cell
75958 ** pointer index, and the gap between the cell pointer index and the start
75959 ** of cell content.
75960 **
75961 ** The loop below pulls entries from the min-heap in order and compares
75962 ** the start_address against the previous end_address. If there is an
75963 ** overlap, that means bytes are used multiple times. If there is a gap,
75964 ** that gap is added to the fragmentation count.
75965 */
75966 nFrag = 0;
75967 prev = contentOffset - 1; /* Implied first min-heap entry */
75968 while( btreeHeapPull(aHeap: heap,pOut: &x) ){
75969 if( (prev&0xffff)>=(x>>16) ){
75970 checkAppendMsg(pCheck,
75971 zFormat: "Multiple uses for byte %u of page %u", x>>16, iPage);
75972 break;
75973 }else{
75974 nFrag += (x>>16) - (prev&0xffff) - 1;
75975 prev = x;
75976 }
75977 }
75978 nFrag += usableSize - (prev&0xffff) - 1;
75979 /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
75980 ** is stored in the fifth field of the b-tree page header.
75981 ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
75982 ** number of fragmented free bytes within the cell content area.
75983 */
75984 if( heap[0]==0 && nFrag!=data[hdr+7] ){
75985 checkAppendMsg(pCheck,
75986 zFormat: "Fragmentation of %d bytes reported as %d on page %u",
75987 nFrag, data[hdr+7], iPage);
75988 }
75989 }
75990
75991end_of_check:
75992 if( !doCoverageCheck ) pPage->isInit = savedIsInit;
75993 releasePage(pPage);
75994 pCheck->zPfx = saved_zPfx;
75995 pCheck->v1 = saved_v1;
75996 pCheck->v2 = saved_v2;
75997 return depth+1;
75998}
75999#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
76000
76001#ifndef SQLITE_OMIT_INTEGRITY_CHECK
76002/*
76003** This routine does a complete check of the given BTree file. aRoot[] is
76004** an array of pages numbers were each page number is the root page of
76005** a table. nRoot is the number of entries in aRoot.
76006**
76007** A read-only or read-write transaction must be opened before calling
76008** this function.
76009**
76010** Write the number of error seen in *pnErr. Except for some memory
76011** allocation errors, an error message held in memory obtained from
76012** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is
76013** returned. If a memory allocation error occurs, NULL is returned.
76014**
76015** If the first entry in aRoot[] is 0, that indicates that the list of
76016** root pages is incomplete. This is a "partial integrity-check". This
76017** happens when performing an integrity check on a single table. The
76018** zero is skipped, of course. But in addition, the freelist checks
76019** and the checks to make sure every page is referenced are also skipped,
76020** since obviously it is not possible to know which pages are covered by
76021** the unverified btrees. Except, if aRoot[1] is 1, then the freelist
76022** checks are still performed.
76023*/
76024SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(
76025 sqlite3 *db, /* Database connection that is running the check */
76026 Btree *p, /* The btree to be checked */
76027 Pgno *aRoot, /* An array of root pages numbers for individual trees */
76028 int nRoot, /* Number of entries in aRoot[] */
76029 int mxErr, /* Stop reporting errors after this many */
76030 int *pnErr /* Write number of errors seen to this variable */
76031){
76032 Pgno i;
76033 IntegrityCk sCheck;
76034 BtShared *pBt = p->pBt;
76035 u64 savedDbFlags = pBt->db->flags;
76036 char zErr[100];
76037 int bPartial = 0; /* True if not checking all btrees */
76038 int bCkFreelist = 1; /* True to scan the freelist */
76039 VVA_ONLY( int nRef );
76040 assert( nRoot>0 );
76041
76042 /* aRoot[0]==0 means this is a partial check */
76043 if( aRoot[0]==0 ){
76044 assert( nRoot>1 );
76045 bPartial = 1;
76046 if( aRoot[1]!=1 ) bCkFreelist = 0;
76047 }
76048
76049 sqlite3BtreeEnter(p);
76050 assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
76051 VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) );
76052 assert( nRef>=0 );
76053 sCheck.db = db;
76054 sCheck.pBt = pBt;
76055 sCheck.pPager = pBt->pPager;
76056 sCheck.nPage = btreePagecount(pBt: sCheck.pBt);
76057 sCheck.mxErr = mxErr;
76058 sCheck.nErr = 0;
76059 sCheck.bOomFault = 0;
76060 sCheck.zPfx = 0;
76061 sCheck.v1 = 0;
76062 sCheck.v2 = 0;
76063 sCheck.aPgRef = 0;
76064 sCheck.heap = 0;
76065 sqlite3StrAccumInit(p: &sCheck.errMsg, db: 0, zBase: zErr, n: sizeof(zErr), SQLITE_MAX_LENGTH);
76066 sCheck.errMsg.printfFlags = SQLITE_PRINTF_INTERNAL;
76067 if( sCheck.nPage==0 ){
76068 goto integrity_ck_cleanup;
76069 }
76070
76071 sCheck.aPgRef = sqlite3MallocZero(n: (sCheck.nPage / 8)+ 1);
76072 if( !sCheck.aPgRef ){
76073 sCheck.bOomFault = 1;
76074 goto integrity_ck_cleanup;
76075 }
76076 sCheck.heap = (u32*)sqlite3PageMalloc( sz: pBt->pageSize );
76077 if( sCheck.heap==0 ){
76078 sCheck.bOomFault = 1;
76079 goto integrity_ck_cleanup;
76080 }
76081
76082 i = PENDING_BYTE_PAGE(pBt);
76083 if( i<=sCheck.nPage ) setPageReferenced(pCheck: &sCheck, iPg: i);
76084
76085 /* Check the integrity of the freelist
76086 */
76087 if( bCkFreelist ){
76088 sCheck.zPfx = "Main freelist: ";
76089 checkList(pCheck: &sCheck, isFreeList: 1, get4byte(p: &pBt->pPage1->aData[32]),
76090 get4byte(p: &pBt->pPage1->aData[36]));
76091 sCheck.zPfx = 0;
76092 }
76093
76094 /* Check all the tables.
76095 */
76096#ifndef SQLITE_OMIT_AUTOVACUUM
76097 if( !bPartial ){
76098 if( pBt->autoVacuum ){
76099 Pgno mx = 0;
76100 Pgno mxInHdr;
76101 for(i=0; (int)i<nRoot; i++) if( mx<aRoot[i] ) mx = aRoot[i];
76102 mxInHdr = get4byte(p: &pBt->pPage1->aData[52]);
76103 if( mx!=mxInHdr ){
76104 checkAppendMsg(pCheck: &sCheck,
76105 zFormat: "max rootpage (%d) disagrees with header (%d)",
76106 mx, mxInHdr
76107 );
76108 }
76109 }else if( get4byte(p: &pBt->pPage1->aData[64])!=0 ){
76110 checkAppendMsg(pCheck: &sCheck,
76111 zFormat: "incremental_vacuum enabled with a max rootpage of zero"
76112 );
76113 }
76114 }
76115#endif
76116 testcase( pBt->db->flags & SQLITE_CellSizeCk );
76117 pBt->db->flags &= ~(u64)SQLITE_CellSizeCk;
76118 for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
76119 i64 notUsed;
76120 if( aRoot[i]==0 ) continue;
76121#ifndef SQLITE_OMIT_AUTOVACUUM
76122 if( pBt->autoVacuum && aRoot[i]>1 && !bPartial ){
76123 checkPtrmap(pCheck: &sCheck, iChild: aRoot[i], PTRMAP_ROOTPAGE, iParent: 0);
76124 }
76125#endif
76126 checkTreePage(pCheck: &sCheck, iPage: aRoot[i], piMinKey: &notUsed, LARGEST_INT64);
76127 }
76128 pBt->db->flags = savedDbFlags;
76129
76130 /* Make sure every page in the file is referenced
76131 */
76132 if( !bPartial ){
76133 for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
76134#ifdef SQLITE_OMIT_AUTOVACUUM
76135 if( getPageReferenced(&sCheck, i)==0 ){
76136 checkAppendMsg(&sCheck, "Page %d is never used", i);
76137 }
76138#else
76139 /* If the database supports auto-vacuum, make sure no tables contain
76140 ** references to pointer-map pages.
76141 */
76142 if( getPageReferenced(pCheck: &sCheck, iPg: i)==0 &&
76143 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
76144 checkAppendMsg(pCheck: &sCheck, zFormat: "Page %d is never used", i);
76145 }
76146 if( getPageReferenced(pCheck: &sCheck, iPg: i)!=0 &&
76147 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
76148 checkAppendMsg(pCheck: &sCheck, zFormat: "Pointer map page %d is referenced", i);
76149 }
76150#endif
76151 }
76152 }
76153
76154 /* Clean up and report errors.
76155 */
76156integrity_ck_cleanup:
76157 sqlite3PageFree(p: sCheck.heap);
76158 sqlite3_free(p: sCheck.aPgRef);
76159 if( sCheck.bOomFault ){
76160 sqlite3_str_reset(p: &sCheck.errMsg);
76161 sCheck.nErr++;
76162 }
76163 *pnErr = sCheck.nErr;
76164 if( sCheck.nErr==0 ) sqlite3_str_reset(p: &sCheck.errMsg);
76165 /* Make sure this analysis did not leave any unref() pages. */
76166 assert( nRef==sqlite3PagerRefcount(pBt->pPager) );
76167 sqlite3BtreeLeave(p);
76168 return sqlite3StrAccumFinish(p: &sCheck.errMsg);
76169}
76170#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
76171
76172/*
76173** Return the full pathname of the underlying database file. Return
76174** an empty string if the database is in-memory or a TEMP database.
76175**
76176** The pager filename is invariant as long as the pager is
76177** open so it is safe to access without the BtShared mutex.
76178*/
76179SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *p){
76180 assert( p->pBt->pPager!=0 );
76181 return sqlite3PagerFilename(pPager: p->pBt->pPager, nullIfMemDb: 1);
76182}
76183
76184/*
76185** Return the pathname of the journal file for this database. The return
76186** value of this routine is the same regardless of whether the journal file
76187** has been created or not.
76188**
76189** The pager journal filename is invariant as long as the pager is
76190** open so it is safe to access without the BtShared mutex.
76191*/
76192SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *p){
76193 assert( p->pBt->pPager!=0 );
76194 return sqlite3PagerJournalname(pPager: p->pBt->pPager);
76195}
76196
76197/*
76198** Return one of SQLITE_TXN_NONE, SQLITE_TXN_READ, or SQLITE_TXN_WRITE
76199** to describe the current transaction state of Btree p.
76200*/
76201SQLITE_PRIVATE int sqlite3BtreeTxnState(Btree *p){
76202 assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
76203 return p ? p->inTrans : 0;
76204}
76205
76206#ifndef SQLITE_OMIT_WAL
76207/*
76208** Run a checkpoint on the Btree passed as the first argument.
76209**
76210** Return SQLITE_LOCKED if this or any other connection has an open
76211** transaction on the shared-cache the argument Btree is connected to.
76212**
76213** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
76214*/
76215SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){
76216 int rc = SQLITE_OK;
76217 if( p ){
76218 BtShared *pBt = p->pBt;
76219 sqlite3BtreeEnter(p);
76220 if( pBt->inTransaction!=TRANS_NONE ){
76221 rc = SQLITE_LOCKED;
76222 }else{
76223 rc = sqlite3PagerCheckpoint(pPager: pBt->pPager, db: p->db, eMode, pnLog, pnCkpt);
76224 }
76225 sqlite3BtreeLeave(p);
76226 }
76227 return rc;
76228}
76229#endif
76230
76231/*
76232** Return true if there is currently a backup running on Btree p.
76233*/
76234SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree *p){
76235 assert( p );
76236 assert( sqlite3_mutex_held(p->db->mutex) );
76237 return p->nBackup!=0;
76238}
76239
76240/*
76241** This function returns a pointer to a blob of memory associated with
76242** a single shared-btree. The memory is used by client code for its own
76243** purposes (for example, to store a high-level schema associated with
76244** the shared-btree). The btree layer manages reference counting issues.
76245**
76246** The first time this is called on a shared-btree, nBytes bytes of memory
76247** are allocated, zeroed, and returned to the caller. For each subsequent
76248** call the nBytes parameter is ignored and a pointer to the same blob
76249** of memory returned.
76250**
76251** If the nBytes parameter is 0 and the blob of memory has not yet been
76252** allocated, a null pointer is returned. If the blob has already been
76253** allocated, it is returned as normal.
76254**
76255** Just before the shared-btree is closed, the function passed as the
76256** xFree argument when the memory allocation was made is invoked on the
76257** blob of allocated memory. The xFree function should not call sqlite3_free()
76258** on the memory, the btree layer does that.
76259*/
76260SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
76261 BtShared *pBt = p->pBt;
76262 sqlite3BtreeEnter(p);
76263 if( !pBt->pSchema && nBytes ){
76264 pBt->pSchema = sqlite3DbMallocZero(db: 0, n: nBytes);
76265 pBt->xFreeSchema = xFree;
76266 }
76267 sqlite3BtreeLeave(p);
76268 return pBt->pSchema;
76269}
76270
76271/*
76272** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared
76273** btree as the argument handle holds an exclusive lock on the
76274** sqlite_schema table. Otherwise SQLITE_OK.
76275*/
76276SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *p){
76277 int rc;
76278 assert( sqlite3_mutex_held(p->db->mutex) );
76279 sqlite3BtreeEnter(p);
76280 rc = querySharedCacheTableLock(p, SCHEMA_ROOT, READ_LOCK);
76281 assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE );
76282 sqlite3BtreeLeave(p);
76283 return rc;
76284}
76285
76286
76287#ifndef SQLITE_OMIT_SHARED_CACHE
76288/*
76289** Obtain a lock on the table whose root page is iTab. The
76290** lock is a write lock if isWritelock is true or a read lock
76291** if it is false.
76292*/
76293SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
76294 int rc = SQLITE_OK;
76295 assert( p->inTrans!=TRANS_NONE );
76296 if( p->sharable ){
76297 u8 lockType = READ_LOCK + isWriteLock;
76298 assert( READ_LOCK+1==WRITE_LOCK );
76299 assert( isWriteLock==0 || isWriteLock==1 );
76300
76301 sqlite3BtreeEnter(p);
76302 rc = querySharedCacheTableLock(p, iTab, eLock: lockType);
76303 if( rc==SQLITE_OK ){
76304 rc = setSharedCacheTableLock(p, iTable: iTab, eLock: lockType);
76305 }
76306 sqlite3BtreeLeave(p);
76307 }
76308 return rc;
76309}
76310#endif
76311
76312#ifndef SQLITE_OMIT_INCRBLOB
76313/*
76314** Argument pCsr must be a cursor opened for writing on an
76315** INTKEY table currently pointing at a valid table entry.
76316** This function modifies the data stored as part of that entry.
76317**
76318** Only the data content may only be modified, it is not possible to
76319** change the length of the data stored. If this function is called with
76320** parameters that attempt to write past the end of the existing data,
76321** no modifications are made and SQLITE_CORRUPT is returned.
76322*/
76323SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
76324 int rc;
76325 assert( cursorOwnsBtShared(pCsr) );
76326 assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
76327 assert( pCsr->curFlags & BTCF_Incrblob );
76328
76329 rc = restoreCursorPosition(pCsr);
76330 if( rc!=SQLITE_OK ){
76331 return rc;
76332 }
76333 assert( pCsr->eState!=CURSOR_REQUIRESEEK );
76334 if( pCsr->eState!=CURSOR_VALID ){
76335 return SQLITE_ABORT;
76336 }
76337
76338 /* Save the positions of all other cursors open on this table. This is
76339 ** required in case any of them are holding references to an xFetch
76340 ** version of the b-tree page modified by the accessPayload call below.
76341 **
76342 ** Note that pCsr must be open on a INTKEY table and saveCursorPosition()
76343 ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence
76344 ** saveAllCursors can only return SQLITE_OK.
76345 */
76346 VVA_ONLY(rc =) saveAllCursors(pBt: pCsr->pBt, iRoot: pCsr->pgnoRoot, pExcept: pCsr);
76347 assert( rc==SQLITE_OK );
76348
76349 /* Check some assumptions:
76350 ** (a) the cursor is open for writing,
76351 ** (b) there is a read/write transaction open,
76352 ** (c) the connection holds a write-lock on the table (if required),
76353 ** (d) there are no conflicting read-locks, and
76354 ** (e) the cursor points at a valid row of an intKey table.
76355 */
76356 if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){
76357 return SQLITE_READONLY;
76358 }
76359 assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
76360 && pCsr->pBt->inTransaction==TRANS_WRITE );
76361 assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
76362 assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
76363 assert( pCsr->pPage->intKey );
76364
76365 return accessPayload(pCur: pCsr, offset, amt, pBuf: (unsigned char *)z, eOp: 1);
76366}
76367
76368/*
76369** Mark this cursor as an incremental blob cursor.
76370*/
76371SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *pCur){
76372 pCur->curFlags |= BTCF_Incrblob;
76373 pCur->pBtree->hasIncrblobCur = 1;
76374}
76375#endif
76376
76377/*
76378** Set both the "read version" (single byte at byte offset 18) and
76379** "write version" (single byte at byte offset 19) fields in the database
76380** header to iVersion.
76381*/
76382SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
76383 BtShared *pBt = pBtree->pBt;
76384 int rc; /* Return code */
76385
76386 assert( iVersion==1 || iVersion==2 );
76387
76388 /* If setting the version fields to 1, do not automatically open the
76389 ** WAL connection, even if the version fields are currently set to 2.
76390 */
76391 pBt->btsFlags &= ~BTS_NO_WAL;
76392 if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
76393
76394 rc = sqlite3BtreeBeginTrans(p: pBtree, wrflag: 0, pSchemaVersion: 0);
76395 if( rc==SQLITE_OK ){
76396 u8 *aData = pBt->pPage1->aData;
76397 if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
76398 rc = sqlite3BtreeBeginTrans(p: pBtree, wrflag: 2, pSchemaVersion: 0);
76399 if( rc==SQLITE_OK ){
76400 rc = sqlite3PagerWrite(pPg: pBt->pPage1->pDbPage);
76401 if( rc==SQLITE_OK ){
76402 aData[18] = (u8)iVersion;
76403 aData[19] = (u8)iVersion;
76404 }
76405 }
76406 }
76407 }
76408
76409 pBt->btsFlags &= ~BTS_NO_WAL;
76410 return rc;
76411}
76412
76413/*
76414** Return true if the cursor has a hint specified. This routine is
76415** only used from within assert() statements
76416*/
76417SQLITE_PRIVATE int sqlite3BtreeCursorHasHint(BtCursor *pCsr, unsigned int mask){
76418 return (pCsr->hints & mask)!=0;
76419}
76420
76421/*
76422** Return true if the given Btree is read-only.
76423*/
76424SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *p){
76425 return (p->pBt->btsFlags & BTS_READ_ONLY)!=0;
76426}
76427
76428/*
76429** Return the size of the header added to each page by this module.
76430*/
76431SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void){ return ROUND8(sizeof(MemPage)); }
76432
76433#if !defined(SQLITE_OMIT_SHARED_CACHE)
76434/*
76435** Return true if the Btree passed as the only argument is sharable.
76436*/
76437SQLITE_PRIVATE int sqlite3BtreeSharable(Btree *p){
76438 return p->sharable;
76439}
76440
76441/*
76442** Return the number of connections to the BtShared object accessed by
76443** the Btree handle passed as the only argument. For private caches
76444** this is always 1. For shared caches it may be 1 or greater.
76445*/
76446SQLITE_PRIVATE int sqlite3BtreeConnectionCount(Btree *p){
76447 testcase( p->sharable );
76448 return p->pBt->nRef;
76449}
76450#endif
76451
76452/************** End of btree.c ***********************************************/
76453/************** Begin file backup.c ******************************************/
76454/*
76455** 2009 January 28
76456**
76457** The author disclaims copyright to this source code. In place of
76458** a legal notice, here is a blessing:
76459**
76460** May you do good and not evil.
76461** May you find forgiveness for yourself and forgive others.
76462** May you share freely, never taking more than you give.
76463**
76464*************************************************************************
76465** This file contains the implementation of the sqlite3_backup_XXX()
76466** API functions and the related features.
76467*/
76468/* #include "sqliteInt.h" */
76469/* #include "btreeInt.h" */
76470
76471/*
76472** Structure allocated for each backup operation.
76473*/
76474struct sqlite3_backup {
76475 sqlite3* pDestDb; /* Destination database handle */
76476 Btree *pDest; /* Destination b-tree file */
76477 u32 iDestSchema; /* Original schema cookie in destination */
76478 int bDestLocked; /* True once a write-transaction is open on pDest */
76479
76480 Pgno iNext; /* Page number of the next source page to copy */
76481 sqlite3* pSrcDb; /* Source database handle */
76482 Btree *pSrc; /* Source b-tree file */
76483
76484 int rc; /* Backup process error code */
76485
76486 /* These two variables are set by every call to backup_step(). They are
76487 ** read by calls to backup_remaining() and backup_pagecount().
76488 */
76489 Pgno nRemaining; /* Number of pages left to copy */
76490 Pgno nPagecount; /* Total number of pages to copy */
76491
76492 int isAttached; /* True once backup has been registered with pager */
76493 sqlite3_backup *pNext; /* Next backup associated with source pager */
76494};
76495
76496/*
76497** THREAD SAFETY NOTES:
76498**
76499** Once it has been created using backup_init(), a single sqlite3_backup
76500** structure may be accessed via two groups of thread-safe entry points:
76501**
76502** * Via the sqlite3_backup_XXX() API function backup_step() and
76503** backup_finish(). Both these functions obtain the source database
76504** handle mutex and the mutex associated with the source BtShared
76505** structure, in that order.
76506**
76507** * Via the BackupUpdate() and BackupRestart() functions, which are
76508** invoked by the pager layer to report various state changes in
76509** the page cache associated with the source database. The mutex
76510** associated with the source database BtShared structure will always
76511** be held when either of these functions are invoked.
76512**
76513** The other sqlite3_backup_XXX() API functions, backup_remaining() and
76514** backup_pagecount() are not thread-safe functions. If they are called
76515** while some other thread is calling backup_step() or backup_finish(),
76516** the values returned may be invalid. There is no way for a call to
76517** BackupUpdate() or BackupRestart() to interfere with backup_remaining()
76518** or backup_pagecount().
76519**
76520** Depending on the SQLite configuration, the database handles and/or
76521** the Btree objects may have their own mutexes that require locking.
76522** Non-sharable Btrees (in-memory databases for example), do not have
76523** associated mutexes.
76524*/
76525
76526/*
76527** Return a pointer corresponding to database zDb (i.e. "main", "temp")
76528** in connection handle pDb. If such a database cannot be found, return
76529** a NULL pointer and write an error message to pErrorDb.
76530**
76531** If the "temp" database is requested, it may need to be opened by this
76532** function. If an error occurs while doing so, return 0 and write an
76533** error message to pErrorDb.
76534*/
76535static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){
76536 int i = sqlite3FindDbName(pDb, zDb);
76537
76538 if( i==1 ){
76539 Parse sParse;
76540 int rc = 0;
76541 memset(s: &sParse, c: 0, n: sizeof(sParse));
76542 sParse.db = pDb;
76543 if( sqlite3OpenTempDatabase(&sParse) ){
76544 sqlite3ErrorWithMsg(db: pErrorDb, err_code: sParse.rc, zFormat: "%s", sParse.zErrMsg);
76545 rc = SQLITE_ERROR;
76546 }
76547 sqlite3DbFree(db: pErrorDb, p: sParse.zErrMsg);
76548 sqlite3ParserReset(&sParse);
76549 if( rc ){
76550 return 0;
76551 }
76552 }
76553
76554 if( i<0 ){
76555 sqlite3ErrorWithMsg(db: pErrorDb, SQLITE_ERROR, zFormat: "unknown database %s", zDb);
76556 return 0;
76557 }
76558
76559 return pDb->aDb[i].pBt;
76560}
76561
76562/*
76563** Attempt to set the page size of the destination to match the page size
76564** of the source.
76565*/
76566static int setDestPgsz(sqlite3_backup *p){
76567 int rc;
76568 rc = sqlite3BtreeSetPageSize(p: p->pDest,pageSize: sqlite3BtreeGetPageSize(p: p->pSrc),nReserve: 0,iFix: 0);
76569 return rc;
76570}
76571
76572/*
76573** Check that there is no open read-transaction on the b-tree passed as the
76574** second argument. If there is not, return SQLITE_OK. Otherwise, if there
76575** is an open read-transaction, return SQLITE_ERROR and leave an error
76576** message in database handle db.
76577*/
76578static int checkReadTransaction(sqlite3 *db, Btree *p){
76579 if( sqlite3BtreeTxnState(p)!=SQLITE_TXN_NONE ){
76580 sqlite3ErrorWithMsg(db, SQLITE_ERROR, zFormat: "destination database is in use");
76581 return SQLITE_ERROR;
76582 }
76583 return SQLITE_OK;
76584}
76585
76586/*
76587** Create an sqlite3_backup process to copy the contents of zSrcDb from
76588** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
76589** a pointer to the new sqlite3_backup object.
76590**
76591** If an error occurs, NULL is returned and an error code and error message
76592** stored in database handle pDestDb.
76593*/
76594SQLITE_API sqlite3_backup *sqlite3_backup_init(
76595 sqlite3* pDestDb, /* Database to write to */
76596 const char *zDestDb, /* Name of database within pDestDb */
76597 sqlite3* pSrcDb, /* Database connection to read from */
76598 const char *zSrcDb /* Name of database within pSrcDb */
76599){
76600 sqlite3_backup *p; /* Value to return */
76601
76602#ifdef SQLITE_ENABLE_API_ARMOR
76603 if( !sqlite3SafetyCheckOk(pSrcDb)||!sqlite3SafetyCheckOk(pDestDb) ){
76604 (void)SQLITE_MISUSE_BKPT;
76605 return 0;
76606 }
76607#endif
76608
76609 /* Lock the source database handle. The destination database
76610 ** handle is not locked in this routine, but it is locked in
76611 ** sqlite3_backup_step(). The user is required to ensure that no
76612 ** other thread accesses the destination handle for the duration
76613 ** of the backup operation. Any attempt to use the destination
76614 ** database connection while a backup is in progress may cause
76615 ** a malfunction or a deadlock.
76616 */
76617 sqlite3_mutex_enter(p: pSrcDb->mutex);
76618 sqlite3_mutex_enter(p: pDestDb->mutex);
76619
76620 if( pSrcDb==pDestDb ){
76621 sqlite3ErrorWithMsg(
76622 db: pDestDb, SQLITE_ERROR, zFormat: "source and destination must be distinct"
76623 );
76624 p = 0;
76625 }else {
76626 /* Allocate space for a new sqlite3_backup object...
76627 ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
76628 ** call to sqlite3_backup_init() and is destroyed by a call to
76629 ** sqlite3_backup_finish(). */
76630 p = (sqlite3_backup *)sqlite3MallocZero(n: sizeof(sqlite3_backup));
76631 if( !p ){
76632 sqlite3Error(db: pDestDb, SQLITE_NOMEM_BKPT);
76633 }
76634 }
76635
76636 /* If the allocation succeeded, populate the new object. */
76637 if( p ){
76638 p->pSrc = findBtree(pErrorDb: pDestDb, pDb: pSrcDb, zDb: zSrcDb);
76639 p->pDest = findBtree(pErrorDb: pDestDb, pDb: pDestDb, zDb: zDestDb);
76640 p->pDestDb = pDestDb;
76641 p->pSrcDb = pSrcDb;
76642 p->iNext = 1;
76643 p->isAttached = 0;
76644
76645 if( 0==p->pSrc || 0==p->pDest
76646 || checkReadTransaction(db: pDestDb, p: p->pDest)!=SQLITE_OK
76647 ){
76648 /* One (or both) of the named databases did not exist or an OOM
76649 ** error was hit. Or there is a transaction open on the destination
76650 ** database. The error has already been written into the pDestDb
76651 ** handle. All that is left to do here is free the sqlite3_backup
76652 ** structure. */
76653 sqlite3_free(p);
76654 p = 0;
76655 }
76656 }
76657 if( p ){
76658 p->pSrc->nBackup++;
76659 }
76660
76661 sqlite3_mutex_leave(p: pDestDb->mutex);
76662 sqlite3_mutex_leave(p: pSrcDb->mutex);
76663 return p;
76664}
76665
76666/*
76667** Argument rc is an SQLite error code. Return true if this error is
76668** considered fatal if encountered during a backup operation. All errors
76669** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED.
76670*/
76671static int isFatalError(int rc){
76672 return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED));
76673}
76674
76675/*
76676** Parameter zSrcData points to a buffer containing the data for
76677** page iSrcPg from the source database. Copy this data into the
76678** destination database.
76679*/
76680static int backupOnePage(
76681 sqlite3_backup *p, /* Backup handle */
76682 Pgno iSrcPg, /* Source database page to backup */
76683 const u8 *zSrcData, /* Source database page data */
76684 int bUpdate /* True for an update, false otherwise */
76685){
76686 Pager * const pDestPager = sqlite3BtreePager(p: p->pDest);
76687 const int nSrcPgsz = sqlite3BtreeGetPageSize(p: p->pSrc);
76688 int nDestPgsz = sqlite3BtreeGetPageSize(p: p->pDest);
76689 const int nCopy = MIN(nSrcPgsz, nDestPgsz);
76690 const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
76691 int rc = SQLITE_OK;
76692 i64 iOff;
76693
76694 assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
76695 assert( p->bDestLocked );
76696 assert( !isFatalError(p->rc) );
76697 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
76698 assert( zSrcData );
76699
76700 /* Catch the case where the destination is an in-memory database and the
76701 ** page sizes of the source and destination differ.
76702 */
76703 if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pPager: pDestPager) ){
76704 rc = SQLITE_READONLY;
76705 }
76706
76707 /* This loop runs once for each destination page spanned by the source
76708 ** page. For each iteration, variable iOff is set to the byte offset
76709 ** of the destination page.
76710 */
76711 for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
76712 DbPage *pDestPg = 0;
76713 Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
76714 if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
76715 if( SQLITE_OK==(rc = sqlite3PagerGet(pPager: pDestPager, pgno: iDest, ppPage: &pDestPg, flags: 0))
76716 && SQLITE_OK==(rc = sqlite3PagerWrite(pPg: pDestPg))
76717 ){
76718 const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
76719 u8 *zDestData = sqlite3PagerGetData(pPg: pDestPg);
76720 u8 *zOut = &zDestData[iOff%nDestPgsz];
76721
76722 /* Copy the data from the source page into the destination page.
76723 ** Then clear the Btree layer MemPage.isInit flag. Both this module
76724 ** and the pager code use this trick (clearing the first byte
76725 ** of the page 'extra' space to invalidate the Btree layers
76726 ** cached parse of the page). MemPage.isInit is marked
76727 ** "MUST BE FIRST" for this purpose.
76728 */
76729 memcpy(dest: zOut, src: zIn, n: nCopy);
76730 ((u8 *)sqlite3PagerGetExtra(pPg: pDestPg))[0] = 0;
76731 if( iOff==0 && bUpdate==0 ){
76732 sqlite3Put4byte(p: &zOut[28], v: sqlite3BtreeLastPage(p: p->pSrc));
76733 }
76734 }
76735 sqlite3PagerUnref(pPg: pDestPg);
76736 }
76737
76738 return rc;
76739}
76740
76741/*
76742** If pFile is currently larger than iSize bytes, then truncate it to
76743** exactly iSize bytes. If pFile is not larger than iSize bytes, then
76744** this function is a no-op.
76745**
76746** Return SQLITE_OK if everything is successful, or an SQLite error
76747** code if an error occurs.
76748*/
76749static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
76750 i64 iCurrent;
76751 int rc = sqlite3OsFileSize(id: pFile, pSize: &iCurrent);
76752 if( rc==SQLITE_OK && iCurrent>iSize ){
76753 rc = sqlite3OsTruncate(id: pFile, size: iSize);
76754 }
76755 return rc;
76756}
76757
76758/*
76759** Register this backup object with the associated source pager for
76760** callbacks when pages are changed or the cache invalidated.
76761*/
76762static void attachBackupObject(sqlite3_backup *p){
76763 sqlite3_backup **pp;
76764 assert( sqlite3BtreeHoldsMutex(p->pSrc) );
76765 pp = sqlite3PagerBackupPtr(pPager: sqlite3BtreePager(p: p->pSrc));
76766 p->pNext = *pp;
76767 *pp = p;
76768 p->isAttached = 1;
76769}
76770
76771/*
76772** Copy nPage pages from the source b-tree to the destination.
76773*/
76774SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage){
76775 int rc;
76776 int destMode; /* Destination journal mode */
76777 int pgszSrc = 0; /* Source page size */
76778 int pgszDest = 0; /* Destination page size */
76779
76780#ifdef SQLITE_ENABLE_API_ARMOR
76781 if( p==0 ) return SQLITE_MISUSE_BKPT;
76782#endif
76783 sqlite3_mutex_enter(p: p->pSrcDb->mutex);
76784 sqlite3BtreeEnter(p: p->pSrc);
76785 if( p->pDestDb ){
76786 sqlite3_mutex_enter(p: p->pDestDb->mutex);
76787 }
76788
76789 rc = p->rc;
76790 if( !isFatalError(rc) ){
76791 Pager * const pSrcPager = sqlite3BtreePager(p: p->pSrc); /* Source pager */
76792 Pager * const pDestPager = sqlite3BtreePager(p: p->pDest); /* Dest pager */
76793 int ii; /* Iterator variable */
76794 int nSrcPage = -1; /* Size of source db in pages */
76795 int bCloseTrans = 0; /* True if src db requires unlocking */
76796
76797 /* If the source pager is currently in a write-transaction, return
76798 ** SQLITE_BUSY immediately.
76799 */
76800 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
76801 rc = SQLITE_BUSY;
76802 }else{
76803 rc = SQLITE_OK;
76804 }
76805
76806 /* If there is no open read-transaction on the source database, open
76807 ** one now. If a transaction is opened here, then it will be closed
76808 ** before this function exits.
76809 */
76810 if( rc==SQLITE_OK && SQLITE_TXN_NONE==sqlite3BtreeTxnState(p: p->pSrc) ){
76811 rc = sqlite3BtreeBeginTrans(p: p->pSrc, wrflag: 0, pSchemaVersion: 0);
76812 bCloseTrans = 1;
76813 }
76814
76815 /* If the destination database has not yet been locked (i.e. if this
76816 ** is the first call to backup_step() for the current backup operation),
76817 ** try to set its page size to the same as the source database. This
76818 ** is especially important on ZipVFS systems, as in that case it is
76819 ** not possible to create a database file that uses one page size by
76820 ** writing to it with another. */
76821 if( p->bDestLocked==0 && rc==SQLITE_OK && setDestPgsz(p)==SQLITE_NOMEM ){
76822 rc = SQLITE_NOMEM;
76823 }
76824
76825 /* Lock the destination database, if it is not locked already. */
76826 if( SQLITE_OK==rc && p->bDestLocked==0
76827 && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p: p->pDest, wrflag: 2,
76828 pSchemaVersion: (int*)&p->iDestSchema))
76829 ){
76830 p->bDestLocked = 1;
76831 }
76832
76833 /* Do not allow backup if the destination database is in WAL mode
76834 ** and the page sizes are different between source and destination */
76835 pgszSrc = sqlite3BtreeGetPageSize(p: p->pSrc);
76836 pgszDest = sqlite3BtreeGetPageSize(p: p->pDest);
76837 destMode = sqlite3PagerGetJournalMode(pPager: sqlite3BtreePager(p: p->pDest));
76838 if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){
76839 rc = SQLITE_READONLY;
76840 }
76841
76842 /* Now that there is a read-lock on the source database, query the
76843 ** source pager for the number of pages in the database.
76844 */
76845 nSrcPage = (int)sqlite3BtreeLastPage(p: p->pSrc);
76846 assert( nSrcPage>=0 );
76847 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
76848 const Pgno iSrcPg = p->iNext; /* Source page number */
76849 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
76850 DbPage *pSrcPg; /* Source page object */
76851 rc = sqlite3PagerGet(pPager: pSrcPager, pgno: iSrcPg, ppPage: &pSrcPg,PAGER_GET_READONLY);
76852 if( rc==SQLITE_OK ){
76853 rc = backupOnePage(p, iSrcPg, zSrcData: sqlite3PagerGetData(pPg: pSrcPg), bUpdate: 0);
76854 sqlite3PagerUnref(pPg: pSrcPg);
76855 }
76856 }
76857 p->iNext++;
76858 }
76859 if( rc==SQLITE_OK ){
76860 p->nPagecount = nSrcPage;
76861 p->nRemaining = nSrcPage+1-p->iNext;
76862 if( p->iNext>(Pgno)nSrcPage ){
76863 rc = SQLITE_DONE;
76864 }else if( !p->isAttached ){
76865 attachBackupObject(p);
76866 }
76867 }
76868
76869 /* Update the schema version field in the destination database. This
76870 ** is to make sure that the schema-version really does change in
76871 ** the case where the source and destination databases have the
76872 ** same schema version.
76873 */
76874 if( rc==SQLITE_DONE ){
76875 if( nSrcPage==0 ){
76876 rc = sqlite3BtreeNewDb(p: p->pDest);
76877 nSrcPage = 1;
76878 }
76879 if( rc==SQLITE_OK || rc==SQLITE_DONE ){
76880 rc = sqlite3BtreeUpdateMeta(p: p->pDest,idx: 1,iMeta: p->iDestSchema+1);
76881 }
76882 if( rc==SQLITE_OK ){
76883 if( p->pDestDb ){
76884 sqlite3ResetAllSchemasOfConnection(p->pDestDb);
76885 }
76886 if( destMode==PAGER_JOURNALMODE_WAL ){
76887 rc = sqlite3BtreeSetVersion(pBtree: p->pDest, iVersion: 2);
76888 }
76889 }
76890 if( rc==SQLITE_OK ){
76891 int nDestTruncate;
76892 /* Set nDestTruncate to the final number of pages in the destination
76893 ** database. The complication here is that the destination page
76894 ** size may be different to the source page size.
76895 **
76896 ** If the source page size is smaller than the destination page size,
76897 ** round up. In this case the call to sqlite3OsTruncate() below will
76898 ** fix the size of the file. However it is important to call
76899 ** sqlite3PagerTruncateImage() here so that any pages in the
76900 ** destination file that lie beyond the nDestTruncate page mark are
76901 ** journalled by PagerCommitPhaseOne() before they are destroyed
76902 ** by the file truncation.
76903 */
76904 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
76905 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
76906 if( pgszSrc<pgszDest ){
76907 int ratio = pgszDest/pgszSrc;
76908 nDestTruncate = (nSrcPage+ratio-1)/ratio;
76909 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
76910 nDestTruncate--;
76911 }
76912 }else{
76913 nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
76914 }
76915 assert( nDestTruncate>0 );
76916
76917 if( pgszSrc<pgszDest ){
76918 /* If the source page-size is smaller than the destination page-size,
76919 ** two extra things may need to happen:
76920 **
76921 ** * The destination may need to be truncated, and
76922 **
76923 ** * Data stored on the pages immediately following the
76924 ** pending-byte page in the source database may need to be
76925 ** copied into the destination database.
76926 */
76927 const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
76928 sqlite3_file * const pFile = sqlite3PagerFile(pPager: pDestPager);
76929 Pgno iPg;
76930 int nDstPage;
76931 i64 iOff;
76932 i64 iEnd;
76933
76934 assert( pFile );
76935 assert( nDestTruncate==0
76936 || (i64)nDestTruncate*(i64)pgszDest >= iSize || (
76937 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
76938 && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
76939 ));
76940
76941 /* This block ensures that all data required to recreate the original
76942 ** database has been stored in the journal for pDestPager and the
76943 ** journal synced to disk. So at this point we may safely modify
76944 ** the database file in any way, knowing that if a power failure
76945 ** occurs, the original database will be reconstructed from the
76946 ** journal file. */
76947 sqlite3PagerPagecount(pPager: pDestPager, pnPage: &nDstPage);
76948 for(iPg=nDestTruncate; rc==SQLITE_OK && iPg<=(Pgno)nDstPage; iPg++){
76949 if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
76950 DbPage *pPg;
76951 rc = sqlite3PagerGet(pPager: pDestPager, pgno: iPg, ppPage: &pPg, flags: 0);
76952 if( rc==SQLITE_OK ){
76953 rc = sqlite3PagerWrite(pPg);
76954 sqlite3PagerUnref(pPg);
76955 }
76956 }
76957 }
76958 if( rc==SQLITE_OK ){
76959 rc = sqlite3PagerCommitPhaseOne(pPager: pDestPager, zSuper: 0, noSync: 1);
76960 }
76961
76962 /* Write the extra pages and truncate the database file as required */
76963 iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
76964 for(
76965 iOff=PENDING_BYTE+pgszSrc;
76966 rc==SQLITE_OK && iOff<iEnd;
76967 iOff+=pgszSrc
76968 ){
76969 PgHdr *pSrcPg = 0;
76970 const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
76971 rc = sqlite3PagerGet(pPager: pSrcPager, pgno: iSrcPg, ppPage: &pSrcPg, flags: 0);
76972 if( rc==SQLITE_OK ){
76973 u8 *zData = sqlite3PagerGetData(pPg: pSrcPg);
76974 rc = sqlite3OsWrite(id: pFile, pBuf: zData, amt: pgszSrc, offset: iOff);
76975 }
76976 sqlite3PagerUnref(pPg: pSrcPg);
76977 }
76978 if( rc==SQLITE_OK ){
76979 rc = backupTruncateFile(pFile, iSize);
76980 }
76981
76982 /* Sync the database file to disk. */
76983 if( rc==SQLITE_OK ){
76984 rc = sqlite3PagerSync(pPager: pDestPager, zSuper: 0);
76985 }
76986 }else{
76987 sqlite3PagerTruncateImage(pPager: pDestPager, nPage: nDestTruncate);
76988 rc = sqlite3PagerCommitPhaseOne(pPager: pDestPager, zSuper: 0, noSync: 0);
76989 }
76990
76991 /* Finish committing the transaction to the destination database. */
76992 if( SQLITE_OK==rc
76993 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p: p->pDest, bCleanup: 0))
76994 ){
76995 rc = SQLITE_DONE;
76996 }
76997 }
76998 }
76999
77000 /* If bCloseTrans is true, then this function opened a read transaction
77001 ** on the source database. Close the read transaction here. There is
77002 ** no need to check the return values of the btree methods here, as
77003 ** "committing" a read-only transaction cannot fail.
77004 */
77005 if( bCloseTrans ){
77006 TESTONLY( int rc2 );
77007 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p: p->pSrc, zSuperJrnl: 0);
77008 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p: p->pSrc, bCleanup: 0);
77009 assert( rc2==SQLITE_OK );
77010 }
77011
77012 if( rc==SQLITE_IOERR_NOMEM ){
77013 rc = SQLITE_NOMEM_BKPT;
77014 }
77015 p->rc = rc;
77016 }
77017 if( p->pDestDb ){
77018 sqlite3_mutex_leave(p: p->pDestDb->mutex);
77019 }
77020 sqlite3BtreeLeave(p: p->pSrc);
77021 sqlite3_mutex_leave(p: p->pSrcDb->mutex);
77022 return rc;
77023}
77024
77025/*
77026** Release all resources associated with an sqlite3_backup* handle.
77027*/
77028SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
77029 sqlite3_backup **pp; /* Ptr to head of pagers backup list */
77030 sqlite3 *pSrcDb; /* Source database connection */
77031 int rc; /* Value to return */
77032
77033 /* Enter the mutexes */
77034 if( p==0 ) return SQLITE_OK;
77035 pSrcDb = p->pSrcDb;
77036 sqlite3_mutex_enter(p: pSrcDb->mutex);
77037 sqlite3BtreeEnter(p: p->pSrc);
77038 if( p->pDestDb ){
77039 sqlite3_mutex_enter(p: p->pDestDb->mutex);
77040 }
77041
77042 /* Detach this backup from the source pager. */
77043 if( p->pDestDb ){
77044 p->pSrc->nBackup--;
77045 }
77046 if( p->isAttached ){
77047 pp = sqlite3PagerBackupPtr(pPager: sqlite3BtreePager(p: p->pSrc));
77048 assert( pp!=0 );
77049 while( *pp!=p ){
77050 pp = &(*pp)->pNext;
77051 assert( pp!=0 );
77052 }
77053 *pp = p->pNext;
77054 }
77055
77056 /* If a transaction is still open on the Btree, roll it back. */
77057 sqlite3BtreeRollback(p: p->pDest, SQLITE_OK, writeOnly: 0);
77058
77059 /* Set the error code of the destination database handle. */
77060 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
77061 if( p->pDestDb ){
77062 sqlite3Error(db: p->pDestDb, err_code: rc);
77063
77064 /* Exit the mutexes and free the backup context structure. */
77065 sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
77066 }
77067 sqlite3BtreeLeave(p: p->pSrc);
77068 if( p->pDestDb ){
77069 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
77070 ** call to sqlite3_backup_init() and is destroyed by a call to
77071 ** sqlite3_backup_finish(). */
77072 sqlite3_free(p);
77073 }
77074 sqlite3LeaveMutexAndCloseZombie(pSrcDb);
77075 return rc;
77076}
77077
77078/*
77079** Return the number of pages still to be backed up as of the most recent
77080** call to sqlite3_backup_step().
77081*/
77082SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p){
77083#ifdef SQLITE_ENABLE_API_ARMOR
77084 if( p==0 ){
77085 (void)SQLITE_MISUSE_BKPT;
77086 return 0;
77087 }
77088#endif
77089 return p->nRemaining;
77090}
77091
77092/*
77093** Return the total number of pages in the source database as of the most
77094** recent call to sqlite3_backup_step().
77095*/
77096SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p){
77097#ifdef SQLITE_ENABLE_API_ARMOR
77098 if( p==0 ){
77099 (void)SQLITE_MISUSE_BKPT;
77100 return 0;
77101 }
77102#endif
77103 return p->nPagecount;
77104}
77105
77106/*
77107** This function is called after the contents of page iPage of the
77108** source database have been modified. If page iPage has already been
77109** copied into the destination database, then the data written to the
77110** destination is now invalidated. The destination copy of iPage needs
77111** to be updated with the new data before the backup operation is
77112** complete.
77113**
77114** It is assumed that the mutex associated with the BtShared object
77115** corresponding to the source database is held when this function is
77116** called.
77117*/
77118static SQLITE_NOINLINE void backupUpdate(
77119 sqlite3_backup *p,
77120 Pgno iPage,
77121 const u8 *aData
77122){
77123 assert( p!=0 );
77124 do{
77125 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
77126 if( !isFatalError(rc: p->rc) && iPage<p->iNext ){
77127 /* The backup process p has already copied page iPage. But now it
77128 ** has been modified by a transaction on the source pager. Copy
77129 ** the new data into the backup.
77130 */
77131 int rc;
77132 assert( p->pDestDb );
77133 sqlite3_mutex_enter(p: p->pDestDb->mutex);
77134 rc = backupOnePage(p, iSrcPg: iPage, zSrcData: aData, bUpdate: 1);
77135 sqlite3_mutex_leave(p: p->pDestDb->mutex);
77136 assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
77137 if( rc!=SQLITE_OK ){
77138 p->rc = rc;
77139 }
77140 }
77141 }while( (p = p->pNext)!=0 );
77142}
77143SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
77144 if( pBackup ) backupUpdate(p: pBackup, iPage, aData);
77145}
77146
77147/*
77148** Restart the backup process. This is called when the pager layer
77149** detects that the database has been modified by an external database
77150** connection. In this case there is no way of knowing which of the
77151** pages that have been copied into the destination database are still
77152** valid and which are not, so the entire process needs to be restarted.
77153**
77154** It is assumed that the mutex associated with the BtShared object
77155** corresponding to the source database is held when this function is
77156** called.
77157*/
77158SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *pBackup){
77159 sqlite3_backup *p; /* Iterator variable */
77160 for(p=pBackup; p; p=p->pNext){
77161 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
77162 p->iNext = 1;
77163 }
77164}
77165
77166#ifndef SQLITE_OMIT_VACUUM
77167/*
77168** Copy the complete content of pBtFrom into pBtTo. A transaction
77169** must be active for both files.
77170**
77171** The size of file pTo may be reduced by this operation. If anything
77172** goes wrong, the transaction on pTo is rolled back. If successful, the
77173** transaction is committed before returning.
77174*/
77175SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
77176 int rc;
77177 sqlite3_file *pFd; /* File descriptor for database pTo */
77178 sqlite3_backup b;
77179 sqlite3BtreeEnter(p: pTo);
77180 sqlite3BtreeEnter(p: pFrom);
77181
77182 assert( sqlite3BtreeTxnState(pTo)==SQLITE_TXN_WRITE );
77183 pFd = sqlite3PagerFile(pPager: sqlite3BtreePager(p: pTo));
77184 if( pFd->pMethods ){
77185 i64 nByte = sqlite3BtreeGetPageSize(p: pFrom)*(i64)sqlite3BtreeLastPage(p: pFrom);
77186 rc = sqlite3OsFileControl(id: pFd, SQLITE_FCNTL_OVERWRITE, pArg: &nByte);
77187 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
77188 if( rc ) goto copy_finished;
77189 }
77190
77191 /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
77192 ** to 0. This is used by the implementations of sqlite3_backup_step()
77193 ** and sqlite3_backup_finish() to detect that they are being called
77194 ** from this function, not directly by the user.
77195 */
77196 memset(s: &b, c: 0, n: sizeof(b));
77197 b.pSrcDb = pFrom->db;
77198 b.pSrc = pFrom;
77199 b.pDest = pTo;
77200 b.iNext = 1;
77201
77202 /* 0x7FFFFFFF is the hard limit for the number of pages in a database
77203 ** file. By passing this as the number of pages to copy to
77204 ** sqlite3_backup_step(), we can guarantee that the copy finishes
77205 ** within a single call (unless an error occurs). The assert() statement
77206 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
77207 ** or an error code. */
77208 sqlite3_backup_step(p: &b, nPage: 0x7FFFFFFF);
77209 assert( b.rc!=SQLITE_OK );
77210
77211 rc = sqlite3_backup_finish(p: &b);
77212 if( rc==SQLITE_OK ){
77213 pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
77214 }else{
77215 sqlite3PagerClearCache(pPager: sqlite3BtreePager(p: b.pDest));
77216 }
77217
77218 assert( sqlite3BtreeTxnState(pTo)!=SQLITE_TXN_WRITE );
77219copy_finished:
77220 sqlite3BtreeLeave(p: pFrom);
77221 sqlite3BtreeLeave(p: pTo);
77222 return rc;
77223}
77224#endif /* SQLITE_OMIT_VACUUM */
77225
77226/************** End of backup.c **********************************************/
77227/************** Begin file vdbemem.c *****************************************/
77228/*
77229** 2004 May 26
77230**
77231** The author disclaims copyright to this source code. In place of
77232** a legal notice, here is a blessing:
77233**
77234** May you do good and not evil.
77235** May you find forgiveness for yourself and forgive others.
77236** May you share freely, never taking more than you give.
77237**
77238*************************************************************************
77239**
77240** This file contains code use to manipulate "Mem" structure. A "Mem"
77241** stores a single value in the VDBE. Mem is an opaque structure visible
77242** only within the VDBE. Interface routines refer to a Mem using the
77243** name sqlite_value
77244*/
77245/* #include "sqliteInt.h" */
77246/* #include "vdbeInt.h" */
77247
77248/* True if X is a power of two. 0 is considered a power of two here.
77249** In other words, return true if X has at most one bit set.
77250*/
77251#define ISPOWEROF2(X) (((X)&((X)-1))==0)
77252
77253#ifdef SQLITE_DEBUG
77254/*
77255** Check invariants on a Mem object.
77256**
77257** This routine is intended for use inside of assert() statements, like
77258** this: assert( sqlite3VdbeCheckMemInvariants(pMem) );
77259*/
77260SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem *p){
77261 /* If MEM_Dyn is set then Mem.xDel!=0.
77262 ** Mem.xDel might not be initialized if MEM_Dyn is clear.
77263 */
77264 assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
77265
77266 /* MEM_Dyn may only be set if Mem.szMalloc==0. In this way we
77267 ** ensure that if Mem.szMalloc>0 then it is safe to do
77268 ** Mem.z = Mem.zMalloc without having to check Mem.flags&MEM_Dyn.
77269 ** That saves a few cycles in inner loops. */
77270 assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 );
77271
77272 /* Cannot have more than one of MEM_Int, MEM_Real, or MEM_IntReal */
77273 assert( ISPOWEROF2(p->flags & (MEM_Int|MEM_Real|MEM_IntReal)) );
77274
77275 if( p->flags & MEM_Null ){
77276 /* Cannot be both MEM_Null and some other type */
77277 assert( (p->flags & (MEM_Int|MEM_Real|MEM_Str|MEM_Blob|MEM_Agg))==0 );
77278
77279 /* If MEM_Null is set, then either the value is a pure NULL (the usual
77280 ** case) or it is a pointer set using sqlite3_bind_pointer() or
77281 ** sqlite3_result_pointer(). If a pointer, then MEM_Term must also be
77282 ** set.
77283 */
77284 if( (p->flags & (MEM_Term|MEM_Subtype))==(MEM_Term|MEM_Subtype) ){
77285 /* This is a pointer type. There may be a flag to indicate what to
77286 ** do with the pointer. */
77287 assert( ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
77288 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
77289 ((p->flags&MEM_Static)!=0 ? 1 : 0) <= 1 );
77290
77291 /* No other bits set */
77292 assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype|MEM_FromBind
77293 |MEM_Dyn|MEM_Ephem|MEM_Static))==0 );
77294 }else{
77295 /* A pure NULL might have other flags, such as MEM_Static, MEM_Dyn,
77296 ** MEM_Ephem, MEM_Cleared, or MEM_Subtype */
77297 }
77298 }else{
77299 /* The MEM_Cleared bit is only allowed on NULLs */
77300 assert( (p->flags & MEM_Cleared)==0 );
77301 }
77302
77303 /* The szMalloc field holds the correct memory allocation size */
77304 assert( p->szMalloc==0
77305 || (p->flags==MEM_Undefined
77306 && p->szMalloc<=sqlite3DbMallocSize(p->db,p->zMalloc))
77307 || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc));
77308
77309 /* If p holds a string or blob, the Mem.z must point to exactly
77310 ** one of the following:
77311 **
77312 ** (1) Memory in Mem.zMalloc and managed by the Mem object
77313 ** (2) Memory to be freed using Mem.xDel
77314 ** (3) An ephemeral string or blob
77315 ** (4) A static string or blob
77316 */
77317 if( (p->flags & (MEM_Str|MEM_Blob)) && p->n>0 ){
77318 assert(
77319 ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) +
77320 ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
77321 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
77322 ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
77323 );
77324 }
77325 return 1;
77326}
77327#endif
77328
77329/*
77330** Render a Mem object which is one of MEM_Int, MEM_Real, or MEM_IntReal
77331** into a buffer.
77332*/
77333static void vdbeMemRenderNum(int sz, char *zBuf, Mem *p){
77334 StrAccum acc;
77335 assert( p->flags & (MEM_Int|MEM_Real|MEM_IntReal) );
77336 assert( sz>22 );
77337 if( p->flags & MEM_Int ){
77338#if GCC_VERSION>=7000000
77339 /* Work-around for GCC bug
77340 ** https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96270 */
77341 i64 x;
77342 assert( (p->flags&MEM_Int)*2==sizeof(x) );
77343 memcpy(&x, (char*)&p->u, (p->flags&MEM_Int)*2);
77344 sqlite3Int64ToText(x, zBuf);
77345#else
77346 sqlite3Int64ToText(v: p->u.i, zOut: zBuf);
77347#endif
77348 }else{
77349 sqlite3StrAccumInit(p: &acc, db: 0, zBase: zBuf, n: sz, mx: 0);
77350 sqlite3_str_appendf(p: &acc, zFormat: "%!.15g",
77351 (p->flags & MEM_IntReal)!=0 ? (double)p->u.i : p->u.r);
77352 assert( acc.zText==zBuf && acc.mxAlloc<=0 );
77353 zBuf[acc.nChar] = 0; /* Fast version of sqlite3StrAccumFinish(&acc) */
77354 }
77355}
77356
77357#ifdef SQLITE_DEBUG
77358/*
77359** Validity checks on pMem. pMem holds a string.
77360**
77361** (1) Check that string value of pMem agrees with its integer or real value.
77362** (2) Check that the string is correctly zero terminated
77363**
77364** A single int or real value always converts to the same strings. But
77365** many different strings can be converted into the same int or real.
77366** If a table contains a numeric value and an index is based on the
77367** corresponding string value, then it is important that the string be
77368** derived from the numeric value, not the other way around, to ensure
77369** that the index and table are consistent. See ticket
77370** https://www.sqlite.org/src/info/343634942dd54ab (2018-01-31) for
77371** an example.
77372**
77373** This routine looks at pMem to verify that if it has both a numeric
77374** representation and a string representation then the string rep has
77375** been derived from the numeric and not the other way around. It returns
77376** true if everything is ok and false if there is a problem.
77377**
77378** This routine is for use inside of assert() statements only.
77379*/
77380SQLITE_PRIVATE int sqlite3VdbeMemValidStrRep(Mem *p){
77381 char zBuf[100];
77382 char *z;
77383 int i, j, incr;
77384 if( (p->flags & MEM_Str)==0 ) return 1;
77385 if( p->flags & MEM_Term ){
77386 /* Insure that the string is properly zero-terminated. Pay particular
77387 ** attention to the case where p->n is odd */
77388 if( p->szMalloc>0 && p->z==p->zMalloc ){
77389 assert( p->enc==SQLITE_UTF8 || p->szMalloc >= ((p->n+1)&~1)+2 );
77390 assert( p->enc!=SQLITE_UTF8 || p->szMalloc >= p->n+1 );
77391 }
77392 assert( p->z[p->n]==0 );
77393 assert( p->enc==SQLITE_UTF8 || p->z[(p->n+1)&~1]==0 );
77394 assert( p->enc==SQLITE_UTF8 || p->z[((p->n+1)&~1)+1]==0 );
77395 }
77396 if( (p->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 ) return 1;
77397 vdbeMemRenderNum(sizeof(zBuf), zBuf, p);
77398 z = p->z;
77399 i = j = 0;
77400 incr = 1;
77401 if( p->enc!=SQLITE_UTF8 ){
77402 incr = 2;
77403 if( p->enc==SQLITE_UTF16BE ) z++;
77404 }
77405 while( zBuf[j] ){
77406 if( zBuf[j++]!=z[i] ) return 0;
77407 i += incr;
77408 }
77409 return 1;
77410}
77411#endif /* SQLITE_DEBUG */
77412
77413/*
77414** If pMem is an object with a valid string representation, this routine
77415** ensures the internal encoding for the string representation is
77416** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
77417**
77418** If pMem is not a string object, or the encoding of the string
77419** representation is already stored using the requested encoding, then this
77420** routine is a no-op.
77421**
77422** SQLITE_OK is returned if the conversion is successful (or not required).
77423** SQLITE_NOMEM may be returned if a malloc() fails during conversion
77424** between formats.
77425*/
77426SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
77427#ifndef SQLITE_OMIT_UTF16
77428 int rc;
77429#endif
77430 assert( pMem!=0 );
77431 assert( !sqlite3VdbeMemIsRowSet(pMem) );
77432 assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE
77433 || desiredEnc==SQLITE_UTF16BE );
77434 if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
77435 return SQLITE_OK;
77436 }
77437 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
77438#ifdef SQLITE_OMIT_UTF16
77439 return SQLITE_ERROR;
77440#else
77441
77442 /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
77443 ** then the encoding of the value may not have changed.
77444 */
77445 rc = sqlite3VdbeMemTranslate(pMem, desiredEnc: (u8)desiredEnc);
77446 assert(rc==SQLITE_OK || rc==SQLITE_NOMEM);
77447 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc);
77448 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
77449 return rc;
77450#endif
77451}
77452
77453/*
77454** Make sure pMem->z points to a writable allocation of at least n bytes.
77455**
77456** If the bPreserve argument is true, then copy of the content of
77457** pMem->z into the new allocation. pMem must be either a string or
77458** blob if bPreserve is true. If bPreserve is false, any prior content
77459** in pMem->z is discarded.
77460*/
77461SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
77462 assert( sqlite3VdbeCheckMemInvariants(pMem) );
77463 assert( !sqlite3VdbeMemIsRowSet(pMem) );
77464 testcase( pMem->db==0 );
77465
77466 /* If the bPreserve flag is set to true, then the memory cell must already
77467 ** contain a valid string or blob value. */
77468 assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
77469 testcase( bPreserve && pMem->z==0 );
77470
77471 assert( pMem->szMalloc==0
77472 || (pMem->flags==MEM_Undefined
77473 && pMem->szMalloc<=sqlite3DbMallocSize(pMem->db,pMem->zMalloc))
77474 || pMem->szMalloc==sqlite3DbMallocSize(pMem->db,pMem->zMalloc));
77475 if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){
77476 if( pMem->db ){
77477 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(db: pMem->db, p: pMem->z, n);
77478 }else{
77479 pMem->zMalloc = sqlite3Realloc(pOld: pMem->z, nBytes: n);
77480 if( pMem->zMalloc==0 ) sqlite3_free(p: pMem->z);
77481 pMem->z = pMem->zMalloc;
77482 }
77483 bPreserve = 0;
77484 }else{
77485 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(db: pMem->db, p: pMem->zMalloc);
77486 pMem->zMalloc = sqlite3DbMallocRaw(db: pMem->db, n);
77487 }
77488 if( pMem->zMalloc==0 ){
77489 sqlite3VdbeMemSetNull(pMem);
77490 pMem->z = 0;
77491 pMem->szMalloc = 0;
77492 return SQLITE_NOMEM_BKPT;
77493 }else{
77494 pMem->szMalloc = sqlite3DbMallocSize(db: pMem->db, p: pMem->zMalloc);
77495 }
77496
77497 if( bPreserve && pMem->z ){
77498 assert( pMem->z!=pMem->zMalloc );
77499 memcpy(dest: pMem->zMalloc, src: pMem->z, n: pMem->n);
77500 }
77501 if( (pMem->flags&MEM_Dyn)!=0 ){
77502 assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );
77503 pMem->xDel((void *)(pMem->z));
77504 }
77505
77506 pMem->z = pMem->zMalloc;
77507 pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
77508 return SQLITE_OK;
77509}
77510
77511/*
77512** Change the pMem->zMalloc allocation to be at least szNew bytes.
77513** If pMem->zMalloc already meets or exceeds the requested size, this
77514** routine is a no-op.
77515**
77516** Any prior string or blob content in the pMem object may be discarded.
77517** The pMem->xDel destructor is called, if it exists. Though MEM_Str
77518** and MEM_Blob values may be discarded, MEM_Int, MEM_Real, MEM_IntReal,
77519** and MEM_Null values are preserved.
77520**
77521** Return SQLITE_OK on success or an error code (probably SQLITE_NOMEM)
77522** if unable to complete the resizing.
77523*/
77524SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){
77525 assert( CORRUPT_DB || szNew>0 );
77526 assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 );
77527 if( pMem->szMalloc<szNew ){
77528 return sqlite3VdbeMemGrow(pMem, n: szNew, bPreserve: 0);
77529 }
77530 assert( (pMem->flags & MEM_Dyn)==0 );
77531 pMem->z = pMem->zMalloc;
77532 pMem->flags &= (MEM_Null|MEM_Int|MEM_Real|MEM_IntReal);
77533 return SQLITE_OK;
77534}
77535
77536/*
77537** It is already known that pMem contains an unterminated string.
77538** Add the zero terminator.
77539**
77540** Three bytes of zero are added. In this way, there is guaranteed
77541** to be a double-zero byte at an even byte boundary in order to
77542** terminate a UTF16 string, even if the initial size of the buffer
77543** is an odd number of bytes.
77544*/
77545static SQLITE_NOINLINE int vdbeMemAddTerminator(Mem *pMem){
77546 if( sqlite3VdbeMemGrow(pMem, n: pMem->n+3, bPreserve: 1) ){
77547 return SQLITE_NOMEM_BKPT;
77548 }
77549 pMem->z[pMem->n] = 0;
77550 pMem->z[pMem->n+1] = 0;
77551 pMem->z[pMem->n+2] = 0;
77552 pMem->flags |= MEM_Term;
77553 return SQLITE_OK;
77554}
77555
77556/*
77557** Change pMem so that its MEM_Str or MEM_Blob value is stored in
77558** MEM.zMalloc, where it can be safely written.
77559**
77560** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
77561*/
77562SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem *pMem){
77563 assert( pMem!=0 );
77564 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
77565 assert( !sqlite3VdbeMemIsRowSet(pMem) );
77566 if( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ){
77567 if( ExpandBlob(pMem) ) return SQLITE_NOMEM;
77568 if( pMem->szMalloc==0 || pMem->z!=pMem->zMalloc ){
77569 int rc = vdbeMemAddTerminator(pMem);
77570 if( rc ) return rc;
77571 }
77572 }
77573 pMem->flags &= ~MEM_Ephem;
77574#ifdef SQLITE_DEBUG
77575 pMem->pScopyFrom = 0;
77576#endif
77577
77578 return SQLITE_OK;
77579}
77580
77581/*
77582** If the given Mem* has a zero-filled tail, turn it into an ordinary
77583** blob stored in dynamically allocated space.
77584*/
77585#ifndef SQLITE_OMIT_INCRBLOB
77586SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *pMem){
77587 int nByte;
77588 assert( pMem!=0 );
77589 assert( pMem->flags & MEM_Zero );
77590 assert( (pMem->flags&MEM_Blob)!=0 || MemNullNochng(pMem) );
77591 testcase( sqlite3_value_nochange(pMem) );
77592 assert( !sqlite3VdbeMemIsRowSet(pMem) );
77593 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
77594
77595 /* Set nByte to the number of bytes required to store the expanded blob. */
77596 nByte = pMem->n + pMem->u.nZero;
77597 if( nByte<=0 ){
77598 if( (pMem->flags & MEM_Blob)==0 ) return SQLITE_OK;
77599 nByte = 1;
77600 }
77601 if( sqlite3VdbeMemGrow(pMem, n: nByte, bPreserve: 1) ){
77602 return SQLITE_NOMEM_BKPT;
77603 }
77604 assert( pMem->z!=0 );
77605 assert( sqlite3DbMallocSize(pMem->db,pMem->z) >= nByte );
77606
77607 memset(s: &pMem->z[pMem->n], c: 0, n: pMem->u.nZero);
77608 pMem->n += pMem->u.nZero;
77609 pMem->flags &= ~(MEM_Zero|MEM_Term);
77610 return SQLITE_OK;
77611}
77612#endif
77613
77614/*
77615** Make sure the given Mem is \u0000 terminated.
77616*/
77617SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem *pMem){
77618 assert( pMem!=0 );
77619 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
77620 testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) );
77621 testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 );
77622 if( (pMem->flags & (MEM_Term|MEM_Str))!=MEM_Str ){
77623 return SQLITE_OK; /* Nothing to do */
77624 }else{
77625 return vdbeMemAddTerminator(pMem);
77626 }
77627}
77628
77629/*
77630** Add MEM_Str to the set of representations for the given Mem. This
77631** routine is only called if pMem is a number of some kind, not a NULL
77632** or a BLOB.
77633**
77634** Existing representations MEM_Int, MEM_Real, or MEM_IntReal are invalidated
77635** if bForce is true but are retained if bForce is false.
77636**
77637** A MEM_Null value will never be passed to this function. This function is
77638** used for converting values to text for returning to the user (i.e. via
77639** sqlite3_value_text()), or for ensuring that values to be used as btree
77640** keys are strings. In the former case a NULL pointer is returned the
77641** user and the latter is an internal programming error.
77642*/
77643SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem *pMem, u8 enc, u8 bForce){
77644 const int nByte = 32;
77645
77646 assert( pMem!=0 );
77647 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
77648 assert( !(pMem->flags&MEM_Zero) );
77649 assert( !(pMem->flags&(MEM_Str|MEM_Blob)) );
77650 assert( pMem->flags&(MEM_Int|MEM_Real|MEM_IntReal) );
77651 assert( !sqlite3VdbeMemIsRowSet(pMem) );
77652 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
77653
77654
77655 if( sqlite3VdbeMemClearAndResize(pMem, szNew: nByte) ){
77656 pMem->enc = 0;
77657 return SQLITE_NOMEM_BKPT;
77658 }
77659
77660 vdbeMemRenderNum(sz: nByte, zBuf: pMem->z, p: pMem);
77661 assert( pMem->z!=0 );
77662 pMem->n = sqlite3Strlen30NN(pMem->z);
77663 pMem->enc = SQLITE_UTF8;
77664 pMem->flags |= MEM_Str|MEM_Term;
77665 if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal);
77666 sqlite3VdbeChangeEncoding(pMem, desiredEnc: enc);
77667 return SQLITE_OK;
77668}
77669
77670/*
77671** Memory cell pMem contains the context of an aggregate function.
77672** This routine calls the finalize method for that function. The
77673** result of the aggregate is stored back into pMem.
77674**
77675** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK
77676** otherwise.
77677*/
77678SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
77679 sqlite3_context ctx;
77680 Mem t;
77681 assert( pFunc!=0 );
77682 assert( pMem!=0 );
77683 assert( pFunc->xFinalize!=0 );
77684 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
77685 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
77686 memset(s: &ctx, c: 0, n: sizeof(ctx));
77687 memset(s: &t, c: 0, n: sizeof(t));
77688 t.flags = MEM_Null;
77689 t.db = pMem->db;
77690 ctx.pOut = &t;
77691 ctx.pMem = pMem;
77692 ctx.pFunc = pFunc;
77693 pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
77694 assert( (pMem->flags & MEM_Dyn)==0 );
77695 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(db: pMem->db, p: pMem->zMalloc);
77696 memcpy(dest: pMem, src: &t, n: sizeof(t));
77697 return ctx.isError;
77698}
77699
77700/*
77701** Memory cell pAccum contains the context of an aggregate function.
77702** This routine calls the xValue method for that function and stores
77703** the results in memory cell pMem.
77704**
77705** SQLITE_ERROR is returned if xValue() reports an error. SQLITE_OK
77706** otherwise.
77707*/
77708#ifndef SQLITE_OMIT_WINDOWFUNC
77709SQLITE_PRIVATE int sqlite3VdbeMemAggValue(Mem *pAccum, Mem *pOut, FuncDef *pFunc){
77710 sqlite3_context ctx;
77711 assert( pFunc!=0 );
77712 assert( pFunc->xValue!=0 );
77713 assert( (pAccum->flags & MEM_Null)!=0 || pFunc==pAccum->u.pDef );
77714 assert( pAccum->db==0 || sqlite3_mutex_held(pAccum->db->mutex) );
77715 memset(s: &ctx, c: 0, n: sizeof(ctx));
77716 sqlite3VdbeMemSetNull(pOut);
77717 ctx.pOut = pOut;
77718 ctx.pMem = pAccum;
77719 ctx.pFunc = pFunc;
77720 pFunc->xValue(&ctx);
77721 return ctx.isError;
77722}
77723#endif /* SQLITE_OMIT_WINDOWFUNC */
77724
77725/*
77726** If the memory cell contains a value that must be freed by
77727** invoking the external callback in Mem.xDel, then this routine
77728** will free that value. It also sets Mem.flags to MEM_Null.
77729**
77730** This is a helper routine for sqlite3VdbeMemSetNull() and
77731** for sqlite3VdbeMemRelease(). Use those other routines as the
77732** entry point for releasing Mem resources.
77733*/
77734static SQLITE_NOINLINE void vdbeMemClearExternAndSetNull(Mem *p){
77735 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
77736 assert( VdbeMemDynamic(p) );
77737 if( p->flags&MEM_Agg ){
77738 sqlite3VdbeMemFinalize(pMem: p, pFunc: p->u.pDef);
77739 assert( (p->flags & MEM_Agg)==0 );
77740 testcase( p->flags & MEM_Dyn );
77741 }
77742 if( p->flags&MEM_Dyn ){
77743 assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
77744 p->xDel((void *)p->z);
77745 }
77746 p->flags = MEM_Null;
77747}
77748
77749/*
77750** Release memory held by the Mem p, both external memory cleared
77751** by p->xDel and memory in p->zMalloc.
77752**
77753** This is a helper routine invoked by sqlite3VdbeMemRelease() in
77754** the unusual case where there really is memory in p that needs
77755** to be freed.
77756*/
77757static SQLITE_NOINLINE void vdbeMemClear(Mem *p){
77758 if( VdbeMemDynamic(p) ){
77759 vdbeMemClearExternAndSetNull(p);
77760 }
77761 if( p->szMalloc ){
77762 sqlite3DbFreeNN(db: p->db, p: p->zMalloc);
77763 p->szMalloc = 0;
77764 }
77765 p->z = 0;
77766}
77767
77768/*
77769** Release any memory resources held by the Mem. Both the memory that is
77770** free by Mem.xDel and the Mem.zMalloc allocation are freed.
77771**
77772** Use this routine prior to clean up prior to abandoning a Mem, or to
77773** reset a Mem back to its minimum memory utilization.
77774**
77775** Use sqlite3VdbeMemSetNull() to release just the Mem.xDel space
77776** prior to inserting new content into the Mem.
77777*/
77778SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
77779 assert( sqlite3VdbeCheckMemInvariants(p) );
77780 if( VdbeMemDynamic(p) || p->szMalloc ){
77781 vdbeMemClear(p);
77782 }
77783}
77784
77785/*
77786** Convert a 64-bit IEEE double into a 64-bit signed integer.
77787** If the double is out of range of a 64-bit signed integer then
77788** return the closest available 64-bit signed integer.
77789*/
77790static SQLITE_NOINLINE i64 doubleToInt64(double r){
77791#ifdef SQLITE_OMIT_FLOATING_POINT
77792 /* When floating-point is omitted, double and int64 are the same thing */
77793 return r;
77794#else
77795 /*
77796 ** Many compilers we encounter do not define constants for the
77797 ** minimum and maximum 64-bit integers, or they define them
77798 ** inconsistently. And many do not understand the "LL" notation.
77799 ** So we define our own static constants here using nothing
77800 ** larger than a 32-bit integer constant.
77801 */
77802 static const i64 maxInt = LARGEST_INT64;
77803 static const i64 minInt = SMALLEST_INT64;
77804
77805 if( r<=(double)minInt ){
77806 return minInt;
77807 }else if( r>=(double)maxInt ){
77808 return maxInt;
77809 }else{
77810 return (i64)r;
77811 }
77812#endif
77813}
77814
77815/*
77816** Return some kind of integer value which is the best we can do
77817** at representing the value that *pMem describes as an integer.
77818** If pMem is an integer, then the value is exact. If pMem is
77819** a floating-point then the value returned is the integer part.
77820** If pMem is a string or blob, then we make an attempt to convert
77821** it into an integer and return that. If pMem represents an
77822** an SQL-NULL value, return 0.
77823**
77824** If pMem represents a string value, its encoding might be changed.
77825*/
77826static SQLITE_NOINLINE i64 memIntValue(Mem *pMem){
77827 i64 value = 0;
77828 sqlite3Atoi64(zNum: pMem->z, pNum: &value, length: pMem->n, enc: pMem->enc);
77829 return value;
77830}
77831SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem *pMem){
77832 int flags;
77833 assert( pMem!=0 );
77834 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
77835 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
77836 flags = pMem->flags;
77837 if( flags & (MEM_Int|MEM_IntReal) ){
77838 testcase( flags & MEM_IntReal );
77839 return pMem->u.i;
77840 }else if( flags & MEM_Real ){
77841 return doubleToInt64(r: pMem->u.r);
77842 }else if( (flags & (MEM_Str|MEM_Blob))!=0 && pMem->z!=0 ){
77843 return memIntValue(pMem);
77844 }else{
77845 return 0;
77846 }
77847}
77848
77849/*
77850** Return the best representation of pMem that we can get into a
77851** double. If pMem is already a double or an integer, return its
77852** value. If it is a string or blob, try to convert it to a double.
77853** If it is a NULL, return 0.0.
77854*/
77855static SQLITE_NOINLINE double memRealValue(Mem *pMem){
77856 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
77857 double val = (double)0;
77858 sqlite3AtoF(z: pMem->z, pResult: &val, length: pMem->n, enc: pMem->enc);
77859 return val;
77860}
77861SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem *pMem){
77862 assert( pMem!=0 );
77863 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
77864 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
77865 if( pMem->flags & MEM_Real ){
77866 return pMem->u.r;
77867 }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){
77868 testcase( pMem->flags & MEM_IntReal );
77869 return (double)pMem->u.i;
77870 }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
77871 return memRealValue(pMem);
77872 }else{
77873 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
77874 return (double)0;
77875 }
77876}
77877
77878/*
77879** Return 1 if pMem represents true, and return 0 if pMem represents false.
77880** Return the value ifNull if pMem is NULL.
77881*/
77882SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem *pMem, int ifNull){
77883 testcase( pMem->flags & MEM_IntReal );
77884 if( pMem->flags & (MEM_Int|MEM_IntReal) ) return pMem->u.i!=0;
77885 if( pMem->flags & MEM_Null ) return ifNull;
77886 return sqlite3VdbeRealValue(pMem)!=0.0;
77887}
77888
77889/*
77890** The MEM structure is already a MEM_Real. Try to also make it a
77891** MEM_Int if we can.
77892*/
77893SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem){
77894 i64 ix;
77895 assert( pMem!=0 );
77896 assert( pMem->flags & MEM_Real );
77897 assert( !sqlite3VdbeMemIsRowSet(pMem) );
77898 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
77899 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
77900
77901 ix = doubleToInt64(r: pMem->u.r);
77902
77903 /* Only mark the value as an integer if
77904 **
77905 ** (1) the round-trip conversion real->int->real is a no-op, and
77906 ** (2) The integer is neither the largest nor the smallest
77907 ** possible integer (ticket #3922)
77908 **
77909 ** The second and third terms in the following conditional enforces
77910 ** the second condition under the assumption that addition overflow causes
77911 ** values to wrap around.
77912 */
77913 if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){
77914 pMem->u.i = ix;
77915 MemSetTypeFlag(pMem, MEM_Int);
77916 }
77917}
77918
77919/*
77920** Convert pMem to type integer. Invalidate any prior representations.
77921*/
77922SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem *pMem){
77923 assert( pMem!=0 );
77924 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
77925 assert( !sqlite3VdbeMemIsRowSet(pMem) );
77926 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
77927
77928 pMem->u.i = sqlite3VdbeIntValue(pMem);
77929 MemSetTypeFlag(pMem, MEM_Int);
77930 return SQLITE_OK;
77931}
77932
77933/*
77934** Convert pMem so that it is of type MEM_Real.
77935** Invalidate any prior representations.
77936*/
77937SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem *pMem){
77938 assert( pMem!=0 );
77939 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
77940 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
77941
77942 pMem->u.r = sqlite3VdbeRealValue(pMem);
77943 MemSetTypeFlag(pMem, MEM_Real);
77944 return SQLITE_OK;
77945}
77946
77947/* Compare a floating point value to an integer. Return true if the two
77948** values are the same within the precision of the floating point value.
77949**
77950** This function assumes that i was obtained by assignment from r1.
77951**
77952** For some versions of GCC on 32-bit machines, if you do the more obvious
77953** comparison of "r1==(double)i" you sometimes get an answer of false even
77954** though the r1 and (double)i values are bit-for-bit the same.
77955*/
77956SQLITE_PRIVATE int sqlite3RealSameAsInt(double r1, sqlite3_int64 i){
77957 double r2 = (double)i;
77958 return r1==0.0
77959 || (memcmp(s1: &r1, s2: &r2, n: sizeof(r1))==0
77960 && i >= -2251799813685248LL && i < 2251799813685248LL);
77961}
77962
77963/*
77964** Convert pMem so that it has type MEM_Real or MEM_Int.
77965** Invalidate any prior representations.
77966**
77967** Every effort is made to force the conversion, even if the input
77968** is a string that does not look completely like a number. Convert
77969** as much of the string as we can and ignore the rest.
77970*/
77971SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem *pMem){
77972 assert( pMem!=0 );
77973 testcase( pMem->flags & MEM_Int );
77974 testcase( pMem->flags & MEM_Real );
77975 testcase( pMem->flags & MEM_IntReal );
77976 testcase( pMem->flags & MEM_Null );
77977 if( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))==0 ){
77978 int rc;
77979 sqlite3_int64 ix;
77980 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
77981 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
77982 rc = sqlite3AtoF(z: pMem->z, pResult: &pMem->u.r, length: pMem->n, enc: pMem->enc);
77983 if( ((rc==0 || rc==1) && sqlite3Atoi64(zNum: pMem->z, pNum: &ix, length: pMem->n, enc: pMem->enc)<=1)
77984 || sqlite3RealSameAsInt(r1: pMem->u.r, i: (ix = (i64)pMem->u.r))
77985 ){
77986 pMem->u.i = ix;
77987 MemSetTypeFlag(pMem, MEM_Int);
77988 }else{
77989 MemSetTypeFlag(pMem, MEM_Real);
77990 }
77991 }
77992 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))!=0 );
77993 pMem->flags &= ~(MEM_Str|MEM_Blob|MEM_Zero);
77994 return SQLITE_OK;
77995}
77996
77997/*
77998** Cast the datatype of the value in pMem according to the affinity
77999** "aff". Casting is different from applying affinity in that a cast
78000** is forced. In other words, the value is converted into the desired
78001** affinity even if that results in loss of data. This routine is
78002** used (for example) to implement the SQL "cast()" operator.
78003*/
78004SQLITE_PRIVATE int sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){
78005 if( pMem->flags & MEM_Null ) return SQLITE_OK;
78006 switch( aff ){
78007 case SQLITE_AFF_BLOB: { /* Really a cast to BLOB */
78008 if( (pMem->flags & MEM_Blob)==0 ){
78009 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
78010 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
78011 if( pMem->flags & MEM_Str ) MemSetTypeFlag(pMem, MEM_Blob);
78012 }else{
78013 pMem->flags &= ~(MEM_TypeMask&~MEM_Blob);
78014 }
78015 break;
78016 }
78017 case SQLITE_AFF_NUMERIC: {
78018 sqlite3VdbeMemNumerify(pMem);
78019 break;
78020 }
78021 case SQLITE_AFF_INTEGER: {
78022 sqlite3VdbeMemIntegerify(pMem);
78023 break;
78024 }
78025 case SQLITE_AFF_REAL: {
78026 sqlite3VdbeMemRealify(pMem);
78027 break;
78028 }
78029 default: {
78030 assert( aff==SQLITE_AFF_TEXT );
78031 assert( MEM_Str==(MEM_Blob>>3) );
78032 pMem->flags |= (pMem->flags&MEM_Blob)>>3;
78033 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
78034 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
78035 pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal|MEM_Blob|MEM_Zero);
78036 return sqlite3VdbeChangeEncoding(pMem, desiredEnc: encoding);
78037 }
78038 }
78039 return SQLITE_OK;
78040}
78041
78042/*
78043** Initialize bulk memory to be a consistent Mem object.
78044**
78045** The minimum amount of initialization feasible is performed.
78046*/
78047SQLITE_PRIVATE void sqlite3VdbeMemInit(Mem *pMem, sqlite3 *db, u16 flags){
78048 assert( (flags & ~MEM_TypeMask)==0 );
78049 pMem->flags = flags;
78050 pMem->db = db;
78051 pMem->szMalloc = 0;
78052}
78053
78054
78055/*
78056** Delete any previous value and set the value stored in *pMem to NULL.
78057**
78058** This routine calls the Mem.xDel destructor to dispose of values that
78059** require the destructor. But it preserves the Mem.zMalloc memory allocation.
78060** To free all resources, use sqlite3VdbeMemRelease(), which both calls this
78061** routine to invoke the destructor and deallocates Mem.zMalloc.
78062**
78063** Use this routine to reset the Mem prior to insert a new value.
78064**
78065** Use sqlite3VdbeMemRelease() to complete erase the Mem prior to abandoning it.
78066*/
78067SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem *pMem){
78068 if( VdbeMemDynamic(pMem) ){
78069 vdbeMemClearExternAndSetNull(p: pMem);
78070 }else{
78071 pMem->flags = MEM_Null;
78072 }
78073}
78074SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value *p){
78075 sqlite3VdbeMemSetNull(pMem: (Mem*)p);
78076}
78077
78078/*
78079** Delete any previous value and set the value to be a BLOB of length
78080** n containing all zeros.
78081*/
78082#ifndef SQLITE_OMIT_INCRBLOB
78083SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
78084 sqlite3VdbeMemRelease(p: pMem);
78085 pMem->flags = MEM_Blob|MEM_Zero;
78086 pMem->n = 0;
78087 if( n<0 ) n = 0;
78088 pMem->u.nZero = n;
78089 pMem->enc = SQLITE_UTF8;
78090 pMem->z = 0;
78091}
78092#else
78093SQLITE_PRIVATE int sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
78094 int nByte = n>0?n:1;
78095 if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){
78096 return SQLITE_NOMEM_BKPT;
78097 }
78098 assert( pMem->z!=0 );
78099 assert( sqlite3DbMallocSize(pMem->db, pMem->z)>=nByte );
78100 memset(pMem->z, 0, nByte);
78101 pMem->n = n>0?n:0;
78102 pMem->flags = MEM_Blob;
78103 pMem->enc = SQLITE_UTF8;
78104 return SQLITE_OK;
78105}
78106#endif
78107
78108/*
78109** The pMem is known to contain content that needs to be destroyed prior
78110** to a value change. So invoke the destructor, then set the value to
78111** a 64-bit integer.
78112*/
78113static SQLITE_NOINLINE void vdbeReleaseAndSetInt64(Mem *pMem, i64 val){
78114 sqlite3VdbeMemSetNull(pMem);
78115 pMem->u.i = val;
78116 pMem->flags = MEM_Int;
78117}
78118
78119/*
78120** Delete any previous value and set the value stored in *pMem to val,
78121** manifest type INTEGER.
78122*/
78123SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
78124 if( VdbeMemDynamic(pMem) ){
78125 vdbeReleaseAndSetInt64(pMem, val);
78126 }else{
78127 pMem->u.i = val;
78128 pMem->flags = MEM_Int;
78129 }
78130}
78131
78132/* A no-op destructor */
78133SQLITE_PRIVATE void sqlite3NoopDestructor(void *p){ UNUSED_PARAMETER(p); }
78134
78135/*
78136** Set the value stored in *pMem should already be a NULL.
78137** Also store a pointer to go with it.
78138*/
78139SQLITE_PRIVATE void sqlite3VdbeMemSetPointer(
78140 Mem *pMem,
78141 void *pPtr,
78142 const char *zPType,
78143 void (*xDestructor)(void*)
78144){
78145 assert( pMem->flags==MEM_Null );
78146 pMem->u.zPType = zPType ? zPType : "";
78147 pMem->z = pPtr;
78148 pMem->flags = MEM_Null|MEM_Dyn|MEM_Subtype|MEM_Term;
78149 pMem->eSubtype = 'p';
78150 pMem->xDel = xDestructor ? xDestructor : sqlite3NoopDestructor;
78151}
78152
78153#ifndef SQLITE_OMIT_FLOATING_POINT
78154/*
78155** Delete any previous value and set the value stored in *pMem to val,
78156** manifest type REAL.
78157*/
78158SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
78159 sqlite3VdbeMemSetNull(pMem);
78160 if( !sqlite3IsNaN(x: val) ){
78161 pMem->u.r = val;
78162 pMem->flags = MEM_Real;
78163 }
78164}
78165#endif
78166
78167#ifdef SQLITE_DEBUG
78168/*
78169** Return true if the Mem holds a RowSet object. This routine is intended
78170** for use inside of assert() statements.
78171*/
78172SQLITE_PRIVATE int sqlite3VdbeMemIsRowSet(const Mem *pMem){
78173 return (pMem->flags&(MEM_Blob|MEM_Dyn))==(MEM_Blob|MEM_Dyn)
78174 && pMem->xDel==sqlite3RowSetDelete;
78175}
78176#endif
78177
78178/*
78179** Delete any previous value and set the value of pMem to be an
78180** empty boolean index.
78181**
78182** Return SQLITE_OK on success and SQLITE_NOMEM if a memory allocation
78183** error occurs.
78184*/
78185SQLITE_PRIVATE int sqlite3VdbeMemSetRowSet(Mem *pMem){
78186 sqlite3 *db = pMem->db;
78187 RowSet *p;
78188 assert( db!=0 );
78189 assert( !sqlite3VdbeMemIsRowSet(pMem) );
78190 sqlite3VdbeMemRelease(p: pMem);
78191 p = sqlite3RowSetInit(db);
78192 if( p==0 ) return SQLITE_NOMEM;
78193 pMem->z = (char*)p;
78194 pMem->flags = MEM_Blob|MEM_Dyn;
78195 pMem->xDel = sqlite3RowSetDelete;
78196 return SQLITE_OK;
78197}
78198
78199/*
78200** Return true if the Mem object contains a TEXT or BLOB that is
78201** too large - whose size exceeds SQLITE_MAX_LENGTH.
78202*/
78203SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem *p){
78204 assert( p->db!=0 );
78205 if( p->flags & (MEM_Str|MEM_Blob) ){
78206 int n = p->n;
78207 if( p->flags & MEM_Zero ){
78208 n += p->u.nZero;
78209 }
78210 return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
78211 }
78212 return 0;
78213}
78214
78215#ifdef SQLITE_DEBUG
78216/*
78217** This routine prepares a memory cell for modification by breaking
78218** its link to a shallow copy and by marking any current shallow
78219** copies of this cell as invalid.
78220**
78221** This is used for testing and debugging only - to help ensure that shallow
78222** copies (created by OP_SCopy) are not misused.
78223*/
78224SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
78225 int i;
78226 Mem *pX;
78227 for(i=1, pX=pVdbe->aMem+1; i<pVdbe->nMem; i++, pX++){
78228 if( pX->pScopyFrom==pMem ){
78229 u16 mFlags;
78230 if( pVdbe->db->flags & SQLITE_VdbeTrace ){
78231 sqlite3DebugPrintf("Invalidate R[%d] due to change in R[%d]\n",
78232 (int)(pX - pVdbe->aMem), (int)(pMem - pVdbe->aMem));
78233 }
78234 /* If pX is marked as a shallow copy of pMem, then try to verify that
78235 ** no significant changes have been made to pX since the OP_SCopy.
78236 ** A significant change would indicated a missed call to this
78237 ** function for pX. Minor changes, such as adding or removing a
78238 ** dual type, are allowed, as long as the underlying value is the
78239 ** same. */
78240 mFlags = pMem->flags & pX->flags & pX->mScopyFlags;
78241 assert( (mFlags&(MEM_Int|MEM_IntReal))==0 || pMem->u.i==pX->u.i );
78242
78243 /* pMem is the register that is changing. But also mark pX as
78244 ** undefined so that we can quickly detect the shallow-copy error */
78245 pX->flags = MEM_Undefined;
78246 pX->pScopyFrom = 0;
78247 }
78248 }
78249 pMem->pScopyFrom = 0;
78250}
78251#endif /* SQLITE_DEBUG */
78252
78253/*
78254** Make an shallow copy of pFrom into pTo. Prior contents of
78255** pTo are freed. The pFrom->z field is not duplicated. If
78256** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
78257** and flags gets srcType (either MEM_Ephem or MEM_Static).
78258*/
78259static SQLITE_NOINLINE void vdbeClrCopy(Mem *pTo, const Mem *pFrom, int eType){
78260 vdbeMemClearExternAndSetNull(p: pTo);
78261 assert( !VdbeMemDynamic(pTo) );
78262 sqlite3VdbeMemShallowCopy(pTo, pFrom, eType);
78263}
78264SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
78265 assert( !sqlite3VdbeMemIsRowSet(pFrom) );
78266 assert( pTo->db==pFrom->db );
78267 if( VdbeMemDynamic(pTo) ){ vdbeClrCopy(pTo,pFrom,eType: srcType); return; }
78268 memcpy(dest: pTo, src: pFrom, MEMCELLSIZE);
78269 if( (pFrom->flags&MEM_Static)==0 ){
78270 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
78271 assert( srcType==MEM_Ephem || srcType==MEM_Static );
78272 pTo->flags |= srcType;
78273 }
78274}
78275
78276/*
78277** Make a full copy of pFrom into pTo. Prior contents of pTo are
78278** freed before the copy is made.
78279*/
78280SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
78281 int rc = SQLITE_OK;
78282
78283 assert( !sqlite3VdbeMemIsRowSet(pFrom) );
78284 if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(p: pTo);
78285 memcpy(dest: pTo, src: pFrom, MEMCELLSIZE);
78286 pTo->flags &= ~MEM_Dyn;
78287 if( pTo->flags&(MEM_Str|MEM_Blob) ){
78288 if( 0==(pFrom->flags&MEM_Static) ){
78289 pTo->flags |= MEM_Ephem;
78290 rc = sqlite3VdbeMemMakeWriteable(pMem: pTo);
78291 }
78292 }
78293
78294 return rc;
78295}
78296
78297/*
78298** Transfer the contents of pFrom to pTo. Any existing value in pTo is
78299** freed. If pFrom contains ephemeral data, a copy is made.
78300**
78301** pFrom contains an SQL NULL when this routine returns.
78302*/
78303SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
78304 assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
78305 assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
78306 assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
78307
78308 sqlite3VdbeMemRelease(p: pTo);
78309 memcpy(dest: pTo, src: pFrom, n: sizeof(Mem));
78310 pFrom->flags = MEM_Null;
78311 pFrom->szMalloc = 0;
78312}
78313
78314/*
78315** Change the value of a Mem to be a string or a BLOB.
78316**
78317** The memory management strategy depends on the value of the xDel
78318** parameter. If the value passed is SQLITE_TRANSIENT, then the
78319** string is copied into a (possibly existing) buffer managed by the
78320** Mem structure. Otherwise, any existing buffer is freed and the
78321** pointer copied.
78322**
78323** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
78324** size limit) then no memory allocation occurs. If the string can be
78325** stored without allocating memory, then it is. If a memory allocation
78326** is required to store the string, then value of pMem is unchanged. In
78327** either case, SQLITE_TOOBIG is returned.
78328*/
78329SQLITE_PRIVATE int sqlite3VdbeMemSetStr(
78330 Mem *pMem, /* Memory cell to set to string value */
78331 const char *z, /* String pointer */
78332 i64 n, /* Bytes in string, or negative */
78333 u8 enc, /* Encoding of z. 0 for BLOBs */
78334 void (*xDel)(void*) /* Destructor function */
78335){
78336 i64 nByte = n; /* New value for pMem->n */
78337 int iLimit; /* Maximum allowed string or blob size */
78338 u16 flags = 0; /* New value for pMem->flags */
78339
78340 assert( pMem!=0 );
78341 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
78342 assert( !sqlite3VdbeMemIsRowSet(pMem) );
78343
78344 /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
78345 if( !z ){
78346 sqlite3VdbeMemSetNull(pMem);
78347 return SQLITE_OK;
78348 }
78349
78350 if( pMem->db ){
78351 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
78352 }else{
78353 iLimit = SQLITE_MAX_LENGTH;
78354 }
78355 flags = (enc==0?MEM_Blob:MEM_Str);
78356 if( nByte<0 ){
78357 assert( enc!=0 );
78358 if( enc==SQLITE_UTF8 ){
78359 nByte = strlen(s: z);
78360 }else{
78361 for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
78362 }
78363 flags |= MEM_Term;
78364 }
78365
78366 /* The following block sets the new values of Mem.z and Mem.xDel. It
78367 ** also sets a flag in local variable "flags" to indicate the memory
78368 ** management (one of MEM_Dyn or MEM_Static).
78369 */
78370 if( xDel==SQLITE_TRANSIENT ){
78371 i64 nAlloc = nByte;
78372 if( flags&MEM_Term ){
78373 nAlloc += (enc==SQLITE_UTF8?1:2);
78374 }
78375 if( nByte>iLimit ){
78376 return sqlite3ErrorToParser(db: pMem->db, SQLITE_TOOBIG);
78377 }
78378 testcase( nAlloc==0 );
78379 testcase( nAlloc==31 );
78380 testcase( nAlloc==32 );
78381 if( sqlite3VdbeMemClearAndResize(pMem, szNew: (int)MAX(nAlloc,32)) ){
78382 return SQLITE_NOMEM_BKPT;
78383 }
78384 memcpy(dest: pMem->z, src: z, n: nAlloc);
78385 }else{
78386 sqlite3VdbeMemRelease(p: pMem);
78387 pMem->z = (char *)z;
78388 if( xDel==SQLITE_DYNAMIC ){
78389 pMem->zMalloc = pMem->z;
78390 pMem->szMalloc = sqlite3DbMallocSize(db: pMem->db, p: pMem->zMalloc);
78391 }else{
78392 pMem->xDel = xDel;
78393 flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
78394 }
78395 }
78396
78397 pMem->n = (int)(nByte & 0x7fffffff);
78398 pMem->flags = flags;
78399 if( enc ){
78400 pMem->enc = enc;
78401#ifdef SQLITE_ENABLE_SESSION
78402 }else if( pMem->db==0 ){
78403 pMem->enc = SQLITE_UTF8;
78404#endif
78405 }else{
78406 assert( pMem->db!=0 );
78407 pMem->enc = ENC(pMem->db);
78408 }
78409
78410#ifndef SQLITE_OMIT_UTF16
78411 if( enc>SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
78412 return SQLITE_NOMEM_BKPT;
78413 }
78414#endif
78415
78416 if( nByte>iLimit ){
78417 return sqlite3ErrorToParser(db: pMem->db, SQLITE_TOOBIG);
78418 }
78419
78420 return SQLITE_OK;
78421}
78422
78423/*
78424** Move data out of a btree key or data field and into a Mem structure.
78425** The data is payload from the entry that pCur is currently pointing
78426** to. offset and amt determine what portion of the data or key to retrieve.
78427** The result is written into the pMem element.
78428**
78429** The pMem object must have been initialized. This routine will use
78430** pMem->zMalloc to hold the content from the btree, if possible. New
78431** pMem->zMalloc space will be allocated if necessary. The calling routine
78432** is responsible for making sure that the pMem object is eventually
78433** destroyed.
78434**
78435** If this routine fails for any reason (malloc returns NULL or unable
78436** to read from the disk) then the pMem is left in an inconsistent state.
78437*/
78438SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(
78439 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
78440 u32 offset, /* Offset from the start of data to return bytes from. */
78441 u32 amt, /* Number of bytes to return. */
78442 Mem *pMem /* OUT: Return data in this Mem structure. */
78443){
78444 int rc;
78445 pMem->flags = MEM_Null;
78446 if( sqlite3BtreeMaxRecordSize(pCur)<offset+amt ){
78447 return SQLITE_CORRUPT_BKPT;
78448 }
78449 if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, szNew: amt+1)) ){
78450 rc = sqlite3BtreePayload(pCur, offset, amt, pBuf: pMem->z);
78451 if( rc==SQLITE_OK ){
78452 pMem->z[amt] = 0; /* Overrun area used when reading malformed records */
78453 pMem->flags = MEM_Blob;
78454 pMem->n = (int)amt;
78455 }else{
78456 sqlite3VdbeMemRelease(p: pMem);
78457 }
78458 }
78459 return rc;
78460}
78461SQLITE_PRIVATE int sqlite3VdbeMemFromBtreeZeroOffset(
78462 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
78463 u32 amt, /* Number of bytes to return. */
78464 Mem *pMem /* OUT: Return data in this Mem structure. */
78465){
78466 u32 available = 0; /* Number of bytes available on the local btree page */
78467 int rc = SQLITE_OK; /* Return code */
78468
78469 assert( sqlite3BtreeCursorIsValid(pCur) );
78470 assert( !VdbeMemDynamic(pMem) );
78471
78472 /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert()
78473 ** that both the BtShared and database handle mutexes are held. */
78474 assert( !sqlite3VdbeMemIsRowSet(pMem) );
78475 pMem->z = (char *)sqlite3BtreePayloadFetch(pCur, pAmt: &available);
78476 assert( pMem->z!=0 );
78477
78478 if( amt<=available ){
78479 pMem->flags = MEM_Blob|MEM_Ephem;
78480 pMem->n = (int)amt;
78481 }else{
78482 rc = sqlite3VdbeMemFromBtree(pCur, offset: 0, amt, pMem);
78483 }
78484
78485 return rc;
78486}
78487
78488/*
78489** The pVal argument is known to be a value other than NULL.
78490** Convert it into a string with encoding enc and return a pointer
78491** to a zero-terminated version of that string.
78492*/
78493static SQLITE_NOINLINE const void *valueToText(sqlite3_value* pVal, u8 enc){
78494 assert( pVal!=0 );
78495 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
78496 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
78497 assert( !sqlite3VdbeMemIsRowSet(pVal) );
78498 assert( (pVal->flags & (MEM_Null))==0 );
78499 if( pVal->flags & (MEM_Blob|MEM_Str) ){
78500 if( ExpandBlob(pVal) ) return 0;
78501 pVal->flags |= MEM_Str;
78502 if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED) ){
78503 sqlite3VdbeChangeEncoding(pMem: pVal, desiredEnc: enc & ~SQLITE_UTF16_ALIGNED);
78504 }
78505 if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
78506 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
78507 if( sqlite3VdbeMemMakeWriteable(pMem: pVal)!=SQLITE_OK ){
78508 return 0;
78509 }
78510 }
78511 sqlite3VdbeMemNulTerminate(pMem: pVal); /* IMP: R-31275-44060 */
78512 }else{
78513 sqlite3VdbeMemStringify(pMem: pVal, enc, bForce: 0);
78514 assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
78515 }
78516 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
78517 || pVal->db->mallocFailed );
78518 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
78519 assert( sqlite3VdbeMemValidStrRep(pVal) );
78520 return pVal->z;
78521 }else{
78522 return 0;
78523 }
78524}
78525
78526/* This function is only available internally, it is not part of the
78527** external API. It works in a similar way to sqlite3_value_text(),
78528** except the data returned is in the encoding specified by the second
78529** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
78530** SQLITE_UTF8.
78531**
78532** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
78533** If that is the case, then the result must be aligned on an even byte
78534** boundary.
78535*/
78536SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
78537 if( !pVal ) return 0;
78538 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
78539 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
78540 assert( !sqlite3VdbeMemIsRowSet(pVal) );
78541 if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){
78542 assert( sqlite3VdbeMemValidStrRep(pVal) );
78543 return pVal->z;
78544 }
78545 if( pVal->flags&MEM_Null ){
78546 return 0;
78547 }
78548 return valueToText(pVal, enc);
78549}
78550
78551/*
78552** Create a new sqlite3_value object.
78553*/
78554SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *db){
78555 Mem *p = sqlite3DbMallocZero(db, n: sizeof(*p));
78556 if( p ){
78557 p->flags = MEM_Null;
78558 p->db = db;
78559 }
78560 return p;
78561}
78562
78563/*
78564** Context object passed by sqlite3Stat4ProbeSetValue() through to
78565** valueNew(). See comments above valueNew() for details.
78566*/
78567struct ValueNewStat4Ctx {
78568 Parse *pParse;
78569 Index *pIdx;
78570 UnpackedRecord **ppRec;
78571 int iVal;
78572};
78573
78574/*
78575** Allocate and return a pointer to a new sqlite3_value object. If
78576** the second argument to this function is NULL, the object is allocated
78577** by calling sqlite3ValueNew().
78578**
78579** Otherwise, if the second argument is non-zero, then this function is
78580** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not
78581** already been allocated, allocate the UnpackedRecord structure that
78582** that function will return to its caller here. Then return a pointer to
78583** an sqlite3_value within the UnpackedRecord.a[] array.
78584*/
78585static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
78586#ifdef SQLITE_ENABLE_STAT4
78587 if( p ){
78588 UnpackedRecord *pRec = p->ppRec[0];
78589
78590 if( pRec==0 ){
78591 Index *pIdx = p->pIdx; /* Index being probed */
78592 int nByte; /* Bytes of space to allocate */
78593 int i; /* Counter variable */
78594 int nCol = pIdx->nColumn; /* Number of index columns including rowid */
78595
78596 nByte = sizeof(Mem) * nCol + ROUND8(sizeof(UnpackedRecord));
78597 pRec = (UnpackedRecord*)sqlite3DbMallocZero(db, nByte);
78598 if( pRec ){
78599 pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
78600 if( pRec->pKeyInfo ){
78601 assert( pRec->pKeyInfo->nAllField==nCol );
78602 assert( pRec->pKeyInfo->enc==ENC(db) );
78603 pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
78604 for(i=0; i<nCol; i++){
78605 pRec->aMem[i].flags = MEM_Null;
78606 pRec->aMem[i].db = db;
78607 }
78608 }else{
78609 sqlite3DbFreeNN(db, pRec);
78610 pRec = 0;
78611 }
78612 }
78613 if( pRec==0 ) return 0;
78614 p->ppRec[0] = pRec;
78615 }
78616
78617 pRec->nField = p->iVal+1;
78618 return &pRec->aMem[p->iVal];
78619 }
78620#else
78621 UNUSED_PARAMETER(p);
78622#endif /* defined(SQLITE_ENABLE_STAT4) */
78623 return sqlite3ValueNew(db);
78624}
78625
78626/*
78627** The expression object indicated by the second argument is guaranteed
78628** to be a scalar SQL function. If
78629**
78630** * all function arguments are SQL literals,
78631** * one of the SQLITE_FUNC_CONSTANT or _SLOCHNG function flags is set, and
78632** * the SQLITE_FUNC_NEEDCOLL function flag is not set,
78633**
78634** then this routine attempts to invoke the SQL function. Assuming no
78635** error occurs, output parameter (*ppVal) is set to point to a value
78636** object containing the result before returning SQLITE_OK.
78637**
78638** Affinity aff is applied to the result of the function before returning.
78639** If the result is a text value, the sqlite3_value object uses encoding
78640** enc.
78641**
78642** If the conditions above are not met, this function returns SQLITE_OK
78643** and sets (*ppVal) to NULL. Or, if an error occurs, (*ppVal) is set to
78644** NULL and an SQLite error code returned.
78645*/
78646#ifdef SQLITE_ENABLE_STAT4
78647static int valueFromFunction(
78648 sqlite3 *db, /* The database connection */
78649 const Expr *p, /* The expression to evaluate */
78650 u8 enc, /* Encoding to use */
78651 u8 aff, /* Affinity to use */
78652 sqlite3_value **ppVal, /* Write the new value here */
78653 struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */
78654){
78655 sqlite3_context ctx; /* Context object for function invocation */
78656 sqlite3_value **apVal = 0; /* Function arguments */
78657 int nVal = 0; /* Size of apVal[] array */
78658 FuncDef *pFunc = 0; /* Function definition */
78659 sqlite3_value *pVal = 0; /* New value */
78660 int rc = SQLITE_OK; /* Return code */
78661 ExprList *pList = 0; /* Function arguments */
78662 int i; /* Iterator variable */
78663
78664 assert( pCtx!=0 );
78665 assert( (p->flags & EP_TokenOnly)==0 );
78666 assert( ExprUseXList(p) );
78667 pList = p->x.pList;
78668 if( pList ) nVal = pList->nExpr;
78669 assert( !ExprHasProperty(p, EP_IntValue) );
78670 pFunc = sqlite3FindFunction(db, p->u.zToken, nVal, enc, 0);
78671 assert( pFunc );
78672 if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0
78673 || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
78674 ){
78675 return SQLITE_OK;
78676 }
78677
78678 if( pList ){
78679 apVal = (sqlite3_value**)sqlite3DbMallocZero(db, sizeof(apVal[0]) * nVal);
78680 if( apVal==0 ){
78681 rc = SQLITE_NOMEM_BKPT;
78682 goto value_from_function_out;
78683 }
78684 for(i=0; i<nVal; i++){
78685 rc = sqlite3ValueFromExpr(db, pList->a[i].pExpr, enc, aff, &apVal[i]);
78686 if( apVal[i]==0 || rc!=SQLITE_OK ) goto value_from_function_out;
78687 }
78688 }
78689
78690 pVal = valueNew(db, pCtx);
78691 if( pVal==0 ){
78692 rc = SQLITE_NOMEM_BKPT;
78693 goto value_from_function_out;
78694 }
78695
78696 assert( pCtx->pParse->rc==SQLITE_OK );
78697 memset(&ctx, 0, sizeof(ctx));
78698 ctx.pOut = pVal;
78699 ctx.pFunc = pFunc;
78700 pFunc->xSFunc(&ctx, nVal, apVal);
78701 if( ctx.isError ){
78702 rc = ctx.isError;
78703 sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal));
78704 }else{
78705 sqlite3ValueApplyAffinity(pVal, aff, SQLITE_UTF8);
78706 assert( rc==SQLITE_OK );
78707 rc = sqlite3VdbeChangeEncoding(pVal, enc);
78708 if( rc==SQLITE_OK && sqlite3VdbeMemTooBig(pVal) ){
78709 rc = SQLITE_TOOBIG;
78710 pCtx->pParse->nErr++;
78711 }
78712 }
78713 pCtx->pParse->rc = rc;
78714
78715 value_from_function_out:
78716 if( rc!=SQLITE_OK ){
78717 pVal = 0;
78718 }
78719 if( apVal ){
78720 for(i=0; i<nVal; i++){
78721 sqlite3ValueFree(apVal[i]);
78722 }
78723 sqlite3DbFreeNN(db, apVal);
78724 }
78725
78726 *ppVal = pVal;
78727 return rc;
78728}
78729#else
78730# define valueFromFunction(a,b,c,d,e,f) SQLITE_OK
78731#endif /* defined(SQLITE_ENABLE_STAT4) */
78732
78733/*
78734** Extract a value from the supplied expression in the manner described
78735** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object
78736** using valueNew().
78737**
78738** If pCtx is NULL and an error occurs after the sqlite3_value object
78739** has been allocated, it is freed before returning. Or, if pCtx is not
78740** NULL, it is assumed that the caller will free any allocated object
78741** in all cases.
78742*/
78743static int valueFromExpr(
78744 sqlite3 *db, /* The database connection */
78745 const Expr *pExpr, /* The expression to evaluate */
78746 u8 enc, /* Encoding to use */
78747 u8 affinity, /* Affinity to use */
78748 sqlite3_value **ppVal, /* Write the new value here */
78749 struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */
78750){
78751 int op;
78752 char *zVal = 0;
78753 sqlite3_value *pVal = 0;
78754 int negInt = 1;
78755 const char *zNeg = "";
78756 int rc = SQLITE_OK;
78757
78758 assert( pExpr!=0 );
78759 while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft;
78760#if defined(SQLITE_ENABLE_STAT4)
78761 if( op==TK_REGISTER ) op = pExpr->op2;
78762#else
78763 if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
78764#endif
78765
78766 /* Compressed expressions only appear when parsing the DEFAULT clause
78767 ** on a table column definition, and hence only when pCtx==0. This
78768 ** check ensures that an EP_TokenOnly expression is never passed down
78769 ** into valueFromFunction(). */
78770 assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 );
78771
78772 if( op==TK_CAST ){
78773 u8 aff;
78774 assert( !ExprHasProperty(pExpr, EP_IntValue) );
78775 aff = sqlite3AffinityType(pExpr->u.zToken,0);
78776 rc = valueFromExpr(db, pExpr: pExpr->pLeft, enc, affinity: aff, ppVal, pCtx);
78777 testcase( rc!=SQLITE_OK );
78778 if( *ppVal ){
78779 sqlite3VdbeMemCast(pMem: *ppVal, aff, SQLITE_UTF8);
78780 sqlite3ValueApplyAffinity(*ppVal, affinity, SQLITE_UTF8);
78781 }
78782 return rc;
78783 }
78784
78785 /* Handle negative integers in a single step. This is needed in the
78786 ** case when the value is -9223372036854775808.
78787 */
78788 if( op==TK_UMINUS
78789 && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
78790 pExpr = pExpr->pLeft;
78791 op = pExpr->op;
78792 negInt = -1;
78793 zNeg = "-";
78794 }
78795
78796 if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
78797 pVal = valueNew(db, p: pCtx);
78798 if( pVal==0 ) goto no_mem;
78799 if( ExprHasProperty(pExpr, EP_IntValue) ){
78800 sqlite3VdbeMemSetInt64(pMem: pVal, val: (i64)pExpr->u.iValue*negInt);
78801 }else{
78802 zVal = sqlite3MPrintf(db, zFormat: "%s%s", zNeg, pExpr->u.zToken);
78803 if( zVal==0 ) goto no_mem;
78804 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
78805 }
78806 if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_BLOB ){
78807 sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
78808 }else{
78809 sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
78810 }
78811 assert( (pVal->flags & MEM_IntReal)==0 );
78812 if( pVal->flags & (MEM_Int|MEM_IntReal|MEM_Real) ){
78813 testcase( pVal->flags & MEM_Int );
78814 testcase( pVal->flags & MEM_Real );
78815 pVal->flags &= ~MEM_Str;
78816 }
78817 if( enc!=SQLITE_UTF8 ){
78818 rc = sqlite3VdbeChangeEncoding(pMem: pVal, desiredEnc: enc);
78819 }
78820 }else if( op==TK_UMINUS ) {
78821 /* This branch happens for multiple negative signs. Ex: -(-5) */
78822 if( SQLITE_OK==valueFromExpr(db,pExpr: pExpr->pLeft,enc,affinity,ppVal: &pVal,pCtx)
78823 && pVal!=0
78824 ){
78825 sqlite3VdbeMemNumerify(pMem: pVal);
78826 if( pVal->flags & MEM_Real ){
78827 pVal->u.r = -pVal->u.r;
78828 }else if( pVal->u.i==SMALLEST_INT64 ){
78829#ifndef SQLITE_OMIT_FLOATING_POINT
78830 pVal->u.r = -(double)SMALLEST_INT64;
78831#else
78832 pVal->u.r = LARGEST_INT64;
78833#endif
78834 MemSetTypeFlag(pVal, MEM_Real);
78835 }else{
78836 pVal->u.i = -pVal->u.i;
78837 }
78838 sqlite3ValueApplyAffinity(pVal, affinity, enc);
78839 }
78840 }else if( op==TK_NULL ){
78841 pVal = valueNew(db, p: pCtx);
78842 if( pVal==0 ) goto no_mem;
78843 sqlite3VdbeMemSetNull(pMem: pVal);
78844 }
78845#ifndef SQLITE_OMIT_BLOB_LITERAL
78846 else if( op==TK_BLOB ){
78847 int nVal;
78848 assert( !ExprHasProperty(pExpr, EP_IntValue) );
78849 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
78850 assert( pExpr->u.zToken[1]=='\'' );
78851 pVal = valueNew(db, p: pCtx);
78852 if( !pVal ) goto no_mem;
78853 zVal = &pExpr->u.zToken[2];
78854 nVal = sqlite3Strlen30(z: zVal)-1;
78855 assert( zVal[nVal]=='\'' );
78856 sqlite3VdbeMemSetStr(pMem: pVal, z: sqlite3HexToBlob(db, z: zVal, n: nVal), n: nVal/2,
78857 enc: 0, SQLITE_DYNAMIC);
78858 }
78859#endif
78860#ifdef SQLITE_ENABLE_STAT4
78861 else if( op==TK_FUNCTION && pCtx!=0 ){
78862 rc = valueFromFunction(db, pExpr, enc, affinity, &pVal, pCtx);
78863 }
78864#endif
78865 else if( op==TK_TRUEFALSE ){
78866 assert( !ExprHasProperty(pExpr, EP_IntValue) );
78867 pVal = valueNew(db, p: pCtx);
78868 if( pVal ){
78869 pVal->flags = MEM_Int;
78870 pVal->u.i = pExpr->u.zToken[4]==0;
78871 }
78872 }
78873
78874 *ppVal = pVal;
78875 return rc;
78876
78877no_mem:
78878#ifdef SQLITE_ENABLE_STAT4
78879 if( pCtx==0 || pCtx->pParse->nErr==0 )
78880#endif
78881 sqlite3OomFault(db);
78882 sqlite3DbFree(db, p: zVal);
78883 assert( *ppVal==0 );
78884#ifdef SQLITE_ENABLE_STAT4
78885 if( pCtx==0 ) sqlite3ValueFree(pVal);
78886#else
78887 assert( pCtx==0 ); sqlite3ValueFree(pVal);
78888#endif
78889 return SQLITE_NOMEM_BKPT;
78890}
78891
78892/*
78893** Create a new sqlite3_value object, containing the value of pExpr.
78894**
78895** This only works for very simple expressions that consist of one constant
78896** token (i.e. "5", "5.1", "'a string'"). If the expression can
78897** be converted directly into a value, then the value is allocated and
78898** a pointer written to *ppVal. The caller is responsible for deallocating
78899** the value by passing it to sqlite3ValueFree() later on. If the expression
78900** cannot be converted to a value, then *ppVal is set to NULL.
78901*/
78902SQLITE_PRIVATE int sqlite3ValueFromExpr(
78903 sqlite3 *db, /* The database connection */
78904 const Expr *pExpr, /* The expression to evaluate */
78905 u8 enc, /* Encoding to use */
78906 u8 affinity, /* Affinity to use */
78907 sqlite3_value **ppVal /* Write the new value here */
78908){
78909 return pExpr ? valueFromExpr(db, pExpr, enc, affinity, ppVal, pCtx: 0) : 0;
78910}
78911
78912#ifdef SQLITE_ENABLE_STAT4
78913/*
78914** Attempt to extract a value from pExpr and use it to construct *ppVal.
78915**
78916** If pAlloc is not NULL, then an UnpackedRecord object is created for
78917** pAlloc if one does not exist and the new value is added to the
78918** UnpackedRecord object.
78919**
78920** A value is extracted in the following cases:
78921**
78922** * (pExpr==0). In this case the value is assumed to be an SQL NULL,
78923**
78924** * The expression is a bound variable, and this is a reprepare, or
78925**
78926** * The expression is a literal value.
78927**
78928** On success, *ppVal is made to point to the extracted value. The caller
78929** is responsible for ensuring that the value is eventually freed.
78930*/
78931static int stat4ValueFromExpr(
78932 Parse *pParse, /* Parse context */
78933 Expr *pExpr, /* The expression to extract a value from */
78934 u8 affinity, /* Affinity to use */
78935 struct ValueNewStat4Ctx *pAlloc,/* How to allocate space. Or NULL */
78936 sqlite3_value **ppVal /* OUT: New value object (or NULL) */
78937){
78938 int rc = SQLITE_OK;
78939 sqlite3_value *pVal = 0;
78940 sqlite3 *db = pParse->db;
78941
78942 /* Skip over any TK_COLLATE nodes */
78943 pExpr = sqlite3ExprSkipCollate(pExpr);
78944
78945 assert( pExpr==0 || pExpr->op!=TK_REGISTER || pExpr->op2!=TK_VARIABLE );
78946 if( !pExpr ){
78947 pVal = valueNew(db, pAlloc);
78948 if( pVal ){
78949 sqlite3VdbeMemSetNull((Mem*)pVal);
78950 }
78951 }else if( pExpr->op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
78952 Vdbe *v;
78953 int iBindVar = pExpr->iColumn;
78954 sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
78955 if( (v = pParse->pReprepare)!=0 ){
78956 pVal = valueNew(db, pAlloc);
78957 if( pVal ){
78958 rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
78959 sqlite3ValueApplyAffinity(pVal, affinity, ENC(db));
78960 pVal->db = pParse->db;
78961 }
78962 }
78963 }else{
78964 rc = valueFromExpr(db, pExpr, ENC(db), affinity, &pVal, pAlloc);
78965 }
78966
78967 assert( pVal==0 || pVal->db==db );
78968 *ppVal = pVal;
78969 return rc;
78970}
78971
78972/*
78973** This function is used to allocate and populate UnpackedRecord
78974** structures intended to be compared against sample index keys stored
78975** in the sqlite_stat4 table.
78976**
78977** A single call to this function populates zero or more fields of the
78978** record starting with field iVal (fields are numbered from left to
78979** right starting with 0). A single field is populated if:
78980**
78981** * (pExpr==0). In this case the value is assumed to be an SQL NULL,
78982**
78983** * The expression is a bound variable, and this is a reprepare, or
78984**
78985** * The sqlite3ValueFromExpr() function is able to extract a value
78986** from the expression (i.e. the expression is a literal value).
78987**
78988** Or, if pExpr is a TK_VECTOR, one field is populated for each of the
78989** vector components that match either of the two latter criteria listed
78990** above.
78991**
78992** Before any value is appended to the record, the affinity of the
78993** corresponding column within index pIdx is applied to it. Before
78994** this function returns, output parameter *pnExtract is set to the
78995** number of values appended to the record.
78996**
78997** When this function is called, *ppRec must either point to an object
78998** allocated by an earlier call to this function, or must be NULL. If it
78999** is NULL and a value can be successfully extracted, a new UnpackedRecord
79000** is allocated (and *ppRec set to point to it) before returning.
79001**
79002** Unless an error is encountered, SQLITE_OK is returned. It is not an
79003** error if a value cannot be extracted from pExpr. If an error does
79004** occur, an SQLite error code is returned.
79005*/
79006SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(
79007 Parse *pParse, /* Parse context */
79008 Index *pIdx, /* Index being probed */
79009 UnpackedRecord **ppRec, /* IN/OUT: Probe record */
79010 Expr *pExpr, /* The expression to extract a value from */
79011 int nElem, /* Maximum number of values to append */
79012 int iVal, /* Array element to populate */
79013 int *pnExtract /* OUT: Values appended to the record */
79014){
79015 int rc = SQLITE_OK;
79016 int nExtract = 0;
79017
79018 if( pExpr==0 || pExpr->op!=TK_SELECT ){
79019 int i;
79020 struct ValueNewStat4Ctx alloc;
79021
79022 alloc.pParse = pParse;
79023 alloc.pIdx = pIdx;
79024 alloc.ppRec = ppRec;
79025
79026 for(i=0; i<nElem; i++){
79027 sqlite3_value *pVal = 0;
79028 Expr *pElem = (pExpr ? sqlite3VectorFieldSubexpr(pExpr, i) : 0);
79029 u8 aff = sqlite3IndexColumnAffinity(pParse->db, pIdx, iVal+i);
79030 alloc.iVal = iVal+i;
79031 rc = stat4ValueFromExpr(pParse, pElem, aff, &alloc, &pVal);
79032 if( !pVal ) break;
79033 nExtract++;
79034 }
79035 }
79036
79037 *pnExtract = nExtract;
79038 return rc;
79039}
79040
79041/*
79042** Attempt to extract a value from expression pExpr using the methods
79043** as described for sqlite3Stat4ProbeSetValue() above.
79044**
79045** If successful, set *ppVal to point to a new value object and return
79046** SQLITE_OK. If no value can be extracted, but no other error occurs
79047** (e.g. OOM), return SQLITE_OK and set *ppVal to NULL. Or, if an error
79048** does occur, return an SQLite error code. The final value of *ppVal
79049** is undefined in this case.
79050*/
79051SQLITE_PRIVATE int sqlite3Stat4ValueFromExpr(
79052 Parse *pParse, /* Parse context */
79053 Expr *pExpr, /* The expression to extract a value from */
79054 u8 affinity, /* Affinity to use */
79055 sqlite3_value **ppVal /* OUT: New value object (or NULL) */
79056){
79057 return stat4ValueFromExpr(pParse, pExpr, affinity, 0, ppVal);
79058}
79059
79060/*
79061** Extract the iCol-th column from the nRec-byte record in pRec. Write
79062** the column value into *ppVal. If *ppVal is initially NULL then a new
79063** sqlite3_value object is allocated.
79064**
79065** If *ppVal is initially NULL then the caller is responsible for
79066** ensuring that the value written into *ppVal is eventually freed.
79067*/
79068SQLITE_PRIVATE int sqlite3Stat4Column(
79069 sqlite3 *db, /* Database handle */
79070 const void *pRec, /* Pointer to buffer containing record */
79071 int nRec, /* Size of buffer pRec in bytes */
79072 int iCol, /* Column to extract */
79073 sqlite3_value **ppVal /* OUT: Extracted value */
79074){
79075 u32 t = 0; /* a column type code */
79076 int nHdr; /* Size of the header in the record */
79077 int iHdr; /* Next unread header byte */
79078 int iField; /* Next unread data byte */
79079 int szField = 0; /* Size of the current data field */
79080 int i; /* Column index */
79081 u8 *a = (u8*)pRec; /* Typecast byte array */
79082 Mem *pMem = *ppVal; /* Write result into this Mem object */
79083
79084 assert( iCol>0 );
79085 iHdr = getVarint32(a, nHdr);
79086 if( nHdr>nRec || iHdr>=nHdr ) return SQLITE_CORRUPT_BKPT;
79087 iField = nHdr;
79088 for(i=0; i<=iCol; i++){
79089 iHdr += getVarint32(&a[iHdr], t);
79090 testcase( iHdr==nHdr );
79091 testcase( iHdr==nHdr+1 );
79092 if( iHdr>nHdr ) return SQLITE_CORRUPT_BKPT;
79093 szField = sqlite3VdbeSerialTypeLen(t);
79094 iField += szField;
79095 }
79096 testcase( iField==nRec );
79097 testcase( iField==nRec+1 );
79098 if( iField>nRec ) return SQLITE_CORRUPT_BKPT;
79099 if( pMem==0 ){
79100 pMem = *ppVal = sqlite3ValueNew(db);
79101 if( pMem==0 ) return SQLITE_NOMEM_BKPT;
79102 }
79103 sqlite3VdbeSerialGet(&a[iField-szField], t, pMem);
79104 pMem->enc = ENC(db);
79105 return SQLITE_OK;
79106}
79107
79108/*
79109** Unless it is NULL, the argument must be an UnpackedRecord object returned
79110** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes
79111** the object.
79112*/
79113SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){
79114 if( pRec ){
79115 int i;
79116 int nCol = pRec->pKeyInfo->nAllField;
79117 Mem *aMem = pRec->aMem;
79118 sqlite3 *db = aMem[0].db;
79119 for(i=0; i<nCol; i++){
79120 sqlite3VdbeMemRelease(&aMem[i]);
79121 }
79122 sqlite3KeyInfoUnref(pRec->pKeyInfo);
79123 sqlite3DbFreeNN(db, pRec);
79124 }
79125}
79126#endif /* ifdef SQLITE_ENABLE_STAT4 */
79127
79128/*
79129** Change the string value of an sqlite3_value object
79130*/
79131SQLITE_PRIVATE void sqlite3ValueSetStr(
79132 sqlite3_value *v, /* Value to be set */
79133 int n, /* Length of string z */
79134 const void *z, /* Text of the new string */
79135 u8 enc, /* Encoding to use */
79136 void (*xDel)(void*) /* Destructor for the string */
79137){
79138 if( v ) sqlite3VdbeMemSetStr(pMem: (Mem *)v, z, n, enc, xDel);
79139}
79140
79141/*
79142** Free an sqlite3_value object
79143*/
79144SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value *v){
79145 if( !v ) return;
79146 sqlite3VdbeMemRelease(p: (Mem *)v);
79147 sqlite3DbFreeNN(db: ((Mem*)v)->db, p: v);
79148}
79149
79150/*
79151** The sqlite3ValueBytes() routine returns the number of bytes in the
79152** sqlite3_value object assuming that it uses the encoding "enc".
79153** The valueBytes() routine is a helper function.
79154*/
79155static SQLITE_NOINLINE int valueBytes(sqlite3_value *pVal, u8 enc){
79156 return valueToText(pVal, enc)!=0 ? pVal->n : 0;
79157}
79158SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
79159 Mem *p = (Mem*)pVal;
79160 assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
79161 if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
79162 return p->n;
79163 }
79164 if( (p->flags & MEM_Blob)!=0 ){
79165 if( p->flags & MEM_Zero ){
79166 return p->n + p->u.nZero;
79167 }else{
79168 return p->n;
79169 }
79170 }
79171 if( p->flags & MEM_Null ) return 0;
79172 return valueBytes(pVal, enc);
79173}
79174
79175/************** End of vdbemem.c *********************************************/
79176/************** Begin file vdbeaux.c *****************************************/
79177/*
79178** 2003 September 6
79179**
79180** The author disclaims copyright to this source code. In place of
79181** a legal notice, here is a blessing:
79182**
79183** May you do good and not evil.
79184** May you find forgiveness for yourself and forgive others.
79185** May you share freely, never taking more than you give.
79186**
79187*************************************************************************
79188** This file contains code used for creating, destroying, and populating
79189** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.)
79190*/
79191/* #include "sqliteInt.h" */
79192/* #include "vdbeInt.h" */
79193
79194/* Forward references */
79195static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef);
79196static void vdbeFreeOpArray(sqlite3 *, Op *, int);
79197
79198/*
79199** Create a new virtual database engine.
79200*/
79201SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse *pParse){
79202 sqlite3 *db = pParse->db;
79203 Vdbe *p;
79204 p = sqlite3DbMallocRawNN(db, n: sizeof(Vdbe) );
79205 if( p==0 ) return 0;
79206 memset(s: &p->aOp, c: 0, n: sizeof(Vdbe)-offsetof(Vdbe,aOp));
79207 p->db = db;
79208 if( db->pVdbe ){
79209 db->pVdbe->pPrev = p;
79210 }
79211 p->pNext = db->pVdbe;
79212 p->pPrev = 0;
79213 db->pVdbe = p;
79214 p->iVdbeMagic = VDBE_MAGIC_INIT;
79215 p->pParse = pParse;
79216 pParse->pVdbe = p;
79217 assert( pParse->aLabel==0 );
79218 assert( pParse->nLabel==0 );
79219 assert( p->nOpAlloc==0 );
79220 assert( pParse->szOpAlloc==0 );
79221 sqlite3VdbeAddOp2(p, OP_Init, 0, 1);
79222 return p;
79223}
79224
79225/*
79226** Return the Parse object that owns a Vdbe object.
79227*/
79228SQLITE_PRIVATE Parse *sqlite3VdbeParser(Vdbe *p){
79229 return p->pParse;
79230}
79231
79232/*
79233** Change the error string stored in Vdbe.zErrMsg
79234*/
79235SQLITE_PRIVATE void sqlite3VdbeError(Vdbe *p, const char *zFormat, ...){
79236 va_list ap;
79237 sqlite3DbFree(db: p->db, p: p->zErrMsg);
79238 va_start(ap, zFormat);
79239 p->zErrMsg = sqlite3VMPrintf(db: p->db, zFormat, ap);
79240 va_end(ap);
79241}
79242
79243/*
79244** Remember the SQL string for a prepared statement.
79245*/
79246SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, u8 prepFlags){
79247 if( p==0 ) return;
79248 p->prepFlags = prepFlags;
79249 if( (prepFlags & SQLITE_PREPARE_SAVESQL)==0 ){
79250 p->expmask = 0;
79251 }
79252 assert( p->zSql==0 );
79253 p->zSql = sqlite3DbStrNDup(db: p->db, z, n);
79254}
79255
79256#ifdef SQLITE_ENABLE_NORMALIZE
79257/*
79258** Add a new element to the Vdbe->pDblStr list.
79259*/
79260SQLITE_PRIVATE void sqlite3VdbeAddDblquoteStr(sqlite3 *db, Vdbe *p, const char *z){
79261 if( p ){
79262 int n = sqlite3Strlen30(z);
79263 DblquoteStr *pStr = sqlite3DbMallocRawNN(db,
79264 sizeof(*pStr)+n+1-sizeof(pStr->z));
79265 if( pStr ){
79266 pStr->pNextStr = p->pDblStr;
79267 p->pDblStr = pStr;
79268 memcpy(pStr->z, z, n+1);
79269 }
79270 }
79271}
79272#endif
79273
79274#ifdef SQLITE_ENABLE_NORMALIZE
79275/*
79276** zId of length nId is a double-quoted identifier. Check to see if
79277** that identifier is really used as a string literal.
79278*/
79279SQLITE_PRIVATE int sqlite3VdbeUsesDoubleQuotedString(
79280 Vdbe *pVdbe, /* The prepared statement */
79281 const char *zId /* The double-quoted identifier, already dequoted */
79282){
79283 DblquoteStr *pStr;
79284 assert( zId!=0 );
79285 if( pVdbe->pDblStr==0 ) return 0;
79286 for(pStr=pVdbe->pDblStr; pStr; pStr=pStr->pNextStr){
79287 if( strcmp(zId, pStr->z)==0 ) return 1;
79288 }
79289 return 0;
79290}
79291#endif
79292
79293/*
79294** Swap all content between two VDBE structures.
79295*/
79296SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
79297 Vdbe tmp, *pTmp;
79298 char *zTmp;
79299 assert( pA->db==pB->db );
79300 tmp = *pA;
79301 *pA = *pB;
79302 *pB = tmp;
79303 pTmp = pA->pNext;
79304 pA->pNext = pB->pNext;
79305 pB->pNext = pTmp;
79306 pTmp = pA->pPrev;
79307 pA->pPrev = pB->pPrev;
79308 pB->pPrev = pTmp;
79309 zTmp = pA->zSql;
79310 pA->zSql = pB->zSql;
79311 pB->zSql = zTmp;
79312#ifdef SQLITE_ENABLE_NORMALIZE
79313 zTmp = pA->zNormSql;
79314 pA->zNormSql = pB->zNormSql;
79315 pB->zNormSql = zTmp;
79316#endif
79317 pB->expmask = pA->expmask;
79318 pB->prepFlags = pA->prepFlags;
79319 memcpy(dest: pB->aCounter, src: pA->aCounter, n: sizeof(pB->aCounter));
79320 pB->aCounter[SQLITE_STMTSTATUS_REPREPARE]++;
79321}
79322
79323/*
79324** Resize the Vdbe.aOp array so that it is at least nOp elements larger
79325** than its current size. nOp is guaranteed to be less than or equal
79326** to 1024/sizeof(Op).
79327**
79328** If an out-of-memory error occurs while resizing the array, return
79329** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain
79330** unchanged (this is so that any opcodes already allocated can be
79331** correctly deallocated along with the rest of the Vdbe).
79332*/
79333static int growOpArray(Vdbe *v, int nOp){
79334 VdbeOp *pNew;
79335 Parse *p = v->pParse;
79336
79337 /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
79338 ** more frequent reallocs and hence provide more opportunities for
79339 ** simulated OOM faults. SQLITE_TEST_REALLOC_STRESS is generally used
79340 ** during testing only. With SQLITE_TEST_REALLOC_STRESS grow the op array
79341 ** by the minimum* amount required until the size reaches 512. Normal
79342 ** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current
79343 ** size of the op array or add 1KB of space, whichever is smaller. */
79344#ifdef SQLITE_TEST_REALLOC_STRESS
79345 sqlite3_int64 nNew = (v->nOpAlloc>=512 ? 2*(sqlite3_int64)v->nOpAlloc
79346 : (sqlite3_int64)v->nOpAlloc+nOp);
79347#else
79348 sqlite3_int64 nNew = (v->nOpAlloc ? 2*(sqlite3_int64)v->nOpAlloc
79349 : (sqlite3_int64)(1024/sizeof(Op)));
79350 UNUSED_PARAMETER(nOp);
79351#endif
79352
79353 /* Ensure that the size of a VDBE does not grow too large */
79354 if( nNew > p->db->aLimit[SQLITE_LIMIT_VDBE_OP] ){
79355 sqlite3OomFault(db: p->db);
79356 return SQLITE_NOMEM;
79357 }
79358
79359 assert( nOp<=(1024/sizeof(Op)) );
79360 assert( nNew>=(v->nOpAlloc+nOp) );
79361 pNew = sqlite3DbRealloc(db: p->db, p: v->aOp, n: nNew*sizeof(Op));
79362 if( pNew ){
79363 p->szOpAlloc = sqlite3DbMallocSize(db: p->db, p: pNew);
79364 v->nOpAlloc = p->szOpAlloc/sizeof(Op);
79365 v->aOp = pNew;
79366 }
79367 return (pNew ? SQLITE_OK : SQLITE_NOMEM_BKPT);
79368}
79369
79370#ifdef SQLITE_DEBUG
79371/* This routine is just a convenient place to set a breakpoint that will
79372** fire after each opcode is inserted and displayed using
79373** "PRAGMA vdbe_addoptrace=on". Parameters "pc" (program counter) and
79374** pOp are available to make the breakpoint conditional.
79375**
79376** Other useful labels for breakpoints include:
79377** test_trace_breakpoint(pc,pOp)
79378** sqlite3CorruptError(lineno)
79379** sqlite3MisuseError(lineno)
79380** sqlite3CantopenError(lineno)
79381*/
79382static void test_addop_breakpoint(int pc, Op *pOp){
79383 static int n = 0;
79384 n++;
79385}
79386#endif
79387
79388/*
79389** Add a new instruction to the list of instructions current in the
79390** VDBE. Return the address of the new instruction.
79391**
79392** Parameters:
79393**
79394** p Pointer to the VDBE
79395**
79396** op The opcode for this instruction
79397**
79398** p1, p2, p3 Operands
79399**
79400** Use the sqlite3VdbeResolveLabel() function to fix an address and
79401** the sqlite3VdbeChangeP4() function to change the value of the P4
79402** operand.
79403*/
79404static SQLITE_NOINLINE int growOp3(Vdbe *p, int op, int p1, int p2, int p3){
79405 assert( p->nOpAlloc<=p->nOp );
79406 if( growOpArray(v: p, nOp: 1) ) return 1;
79407 assert( p->nOpAlloc>p->nOp );
79408 return sqlite3VdbeAddOp3(p, op, p1, p2, p3);
79409}
79410SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
79411 int i;
79412 VdbeOp *pOp;
79413
79414 i = p->nOp;
79415 assert( p->iVdbeMagic==VDBE_MAGIC_INIT );
79416 assert( op>=0 && op<0xff );
79417 if( p->nOpAlloc<=i ){
79418 return growOp3(p, op, p1, p2, p3);
79419 }
79420 assert( p->aOp!=0 );
79421 p->nOp++;
79422 pOp = &p->aOp[i];
79423 assert( pOp!=0 );
79424 pOp->opcode = (u8)op;
79425 pOp->p5 = 0;
79426 pOp->p1 = p1;
79427 pOp->p2 = p2;
79428 pOp->p3 = p3;
79429 pOp->p4.p = 0;
79430 pOp->p4type = P4_NOTUSED;
79431#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
79432 pOp->zComment = 0;
79433#endif
79434#ifdef SQLITE_DEBUG
79435 if( p->db->flags & SQLITE_VdbeAddopTrace ){
79436 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
79437 test_addop_breakpoint(i, &p->aOp[i]);
79438 }
79439#endif
79440#ifdef VDBE_PROFILE
79441 pOp->cycles = 0;
79442 pOp->cnt = 0;
79443#endif
79444#ifdef SQLITE_VDBE_COVERAGE
79445 pOp->iSrcLine = 0;
79446#endif
79447 return i;
79448}
79449SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe *p, int op){
79450 return sqlite3VdbeAddOp3(p, op, p1: 0, p2: 0, p3: 0);
79451}
79452SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
79453 return sqlite3VdbeAddOp3(p, op, p1, p2: 0, p3: 0);
79454}
79455SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
79456 return sqlite3VdbeAddOp3(p, op, p1, p2, p3: 0);
79457}
79458
79459/* Generate code for an unconditional jump to instruction iDest
79460*/
79461SQLITE_PRIVATE int sqlite3VdbeGoto(Vdbe *p, int iDest){
79462 return sqlite3VdbeAddOp3(p, OP_Goto, p1: 0, p2: iDest, p3: 0);
79463}
79464
79465/* Generate code to cause the string zStr to be loaded into
79466** register iDest
79467*/
79468SQLITE_PRIVATE int sqlite3VdbeLoadString(Vdbe *p, int iDest, const char *zStr){
79469 return sqlite3VdbeAddOp4(p, OP_String8, 0, iDest, 0, zP4: zStr, 0);
79470}
79471
79472/*
79473** Generate code that initializes multiple registers to string or integer
79474** constants. The registers begin with iDest and increase consecutively.
79475** One register is initialized for each characgter in zTypes[]. For each
79476** "s" character in zTypes[], the register is a string if the argument is
79477** not NULL, or OP_Null if the value is a null pointer. For each "i" character
79478** in zTypes[], the register is initialized to an integer.
79479**
79480** If the input string does not end with "X" then an OP_ResultRow instruction
79481** is generated for the values inserted.
79482*/
79483SQLITE_PRIVATE void sqlite3VdbeMultiLoad(Vdbe *p, int iDest, const char *zTypes, ...){
79484 va_list ap;
79485 int i;
79486 char c;
79487 va_start(ap, zTypes);
79488 for(i=0; (c = zTypes[i])!=0; i++){
79489 if( c=='s' ){
79490 const char *z = va_arg(ap, const char*);
79491 sqlite3VdbeAddOp4(p, z==0 ? OP_Null : OP_String8, 0, iDest+i, 0, zP4: z, 0);
79492 }else if( c=='i' ){
79493 sqlite3VdbeAddOp2(p, OP_Integer, va_arg(ap, int), p2: iDest+i);
79494 }else{
79495 goto skip_op_resultrow;
79496 }
79497 }
79498 sqlite3VdbeAddOp2(p, OP_ResultRow, p1: iDest, p2: i);
79499skip_op_resultrow:
79500 va_end(ap);
79501}
79502
79503/*
79504** Add an opcode that includes the p4 value as a pointer.
79505*/
79506SQLITE_PRIVATE int sqlite3VdbeAddOp4(
79507 Vdbe *p, /* Add the opcode to this VM */
79508 int op, /* The new opcode */
79509 int p1, /* The P1 operand */
79510 int p2, /* The P2 operand */
79511 int p3, /* The P3 operand */
79512 const char *zP4, /* The P4 operand */
79513 int p4type /* P4 operand type */
79514){
79515 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
79516 sqlite3VdbeChangeP4(p, addr, zP4, N: p4type);
79517 return addr;
79518}
79519
79520/*
79521** Add an OP_Function or OP_PureFunc opcode.
79522**
79523** The eCallCtx argument is information (typically taken from Expr.op2)
79524** that describes the calling context of the function. 0 means a general
79525** function call. NC_IsCheck means called by a check constraint,
79526** NC_IdxExpr means called as part of an index expression. NC_PartIdx
79527** means in the WHERE clause of a partial index. NC_GenCol means called
79528** while computing a generated column value. 0 is the usual case.
79529*/
79530SQLITE_PRIVATE int sqlite3VdbeAddFunctionCall(
79531 Parse *pParse, /* Parsing context */
79532 int p1, /* Constant argument mask */
79533 int p2, /* First argument register */
79534 int p3, /* Register into which results are written */
79535 int nArg, /* Number of argument */
79536 const FuncDef *pFunc, /* The function to be invoked */
79537 int eCallCtx /* Calling context */
79538){
79539 Vdbe *v = pParse->pVdbe;
79540 int nByte;
79541 int addr;
79542 sqlite3_context *pCtx;
79543 assert( v );
79544 nByte = sizeof(*pCtx) + (nArg-1)*sizeof(sqlite3_value*);
79545 pCtx = sqlite3DbMallocRawNN(db: pParse->db, n: nByte);
79546 if( pCtx==0 ){
79547 assert( pParse->db->mallocFailed );
79548 freeEphemeralFunction(db: pParse->db, pDef: (FuncDef*)pFunc);
79549 return 0;
79550 }
79551 pCtx->pOut = 0;
79552 pCtx->pFunc = (FuncDef*)pFunc;
79553 pCtx->pVdbe = 0;
79554 pCtx->isError = 0;
79555 pCtx->argc = nArg;
79556 pCtx->iOp = sqlite3VdbeCurrentAddr(v);
79557 addr = sqlite3VdbeAddOp4(p: v, op: eCallCtx ? OP_PureFunc : OP_Function,
79558 p1, p2, p3, zP4: (char*)pCtx, P4_FUNCCTX);
79559 sqlite3VdbeChangeP5(v, P5: eCallCtx & NC_SelfRef);
79560 return addr;
79561}
79562
79563/*
79564** Add an opcode that includes the p4 value with a P4_INT64 or
79565** P4_REAL type.
79566*/
79567SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(
79568 Vdbe *p, /* Add the opcode to this VM */
79569 int op, /* The new opcode */
79570 int p1, /* The P1 operand */
79571 int p2, /* The P2 operand */
79572 int p3, /* The P3 operand */
79573 const u8 *zP4, /* The P4 operand */
79574 int p4type /* P4 operand type */
79575){
79576 char *p4copy = sqlite3DbMallocRawNN(db: sqlite3VdbeDb(p), n: 8);
79577 if( p4copy ) memcpy(dest: p4copy, src: zP4, n: 8);
79578 return sqlite3VdbeAddOp4(p, op, p1, p2, p3, zP4: p4copy, p4type);
79579}
79580
79581#ifndef SQLITE_OMIT_EXPLAIN
79582/*
79583** Return the address of the current EXPLAIN QUERY PLAN baseline.
79584** 0 means "none".
79585*/
79586SQLITE_PRIVATE int sqlite3VdbeExplainParent(Parse *pParse){
79587 VdbeOp *pOp;
79588 if( pParse->addrExplain==0 ) return 0;
79589 pOp = sqlite3VdbeGetOp(pParse->pVdbe, pParse->addrExplain);
79590 return pOp->p2;
79591}
79592
79593/*
79594** Set a debugger breakpoint on the following routine in order to
79595** monitor the EXPLAIN QUERY PLAN code generation.
79596*/
79597#if defined(SQLITE_DEBUG)
79598SQLITE_PRIVATE void sqlite3ExplainBreakpoint(const char *z1, const char *z2){
79599 (void)z1;
79600 (void)z2;
79601}
79602#endif
79603
79604/*
79605** Add a new OP_Explain opcode.
79606**
79607** If the bPush flag is true, then make this opcode the parent for
79608** subsequent Explains until sqlite3VdbeExplainPop() is called.
79609*/
79610SQLITE_PRIVATE void sqlite3VdbeExplain(Parse *pParse, u8 bPush, const char *zFmt, ...){
79611#ifndef SQLITE_DEBUG
79612 /* Always include the OP_Explain opcodes if SQLITE_DEBUG is defined.
79613 ** But omit them (for performance) during production builds */
79614 if( pParse->explain==2 )
79615#endif
79616 {
79617 char *zMsg;
79618 Vdbe *v;
79619 va_list ap;
79620 int iThis;
79621 va_start(ap, zFmt);
79622 zMsg = sqlite3VMPrintf(db: pParse->db, zFormat: zFmt, ap);
79623 va_end(ap);
79624 v = pParse->pVdbe;
79625 iThis = v->nOp;
79626 sqlite3VdbeAddOp4(p: v, OP_Explain, p1: iThis, p2: pParse->addrExplain, p3: 0,
79627 zP4: zMsg, P4_DYNAMIC);
79628 sqlite3ExplainBreakpoint(bPush?"PUSH":"", sqlite3VdbeGetOp(v,-1)->p4.z);
79629 if( bPush){
79630 pParse->addrExplain = iThis;
79631 }
79632 }
79633}
79634
79635/*
79636** Pop the EXPLAIN QUERY PLAN stack one level.
79637*/
79638SQLITE_PRIVATE void sqlite3VdbeExplainPop(Parse *pParse){
79639 sqlite3ExplainBreakpoint("POP", 0);
79640 pParse->addrExplain = sqlite3VdbeExplainParent(pParse);
79641}
79642#endif /* SQLITE_OMIT_EXPLAIN */
79643
79644/*
79645** Add an OP_ParseSchema opcode. This routine is broken out from
79646** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
79647** as having been used.
79648**
79649** The zWhere string must have been obtained from sqlite3_malloc().
79650** This routine will take ownership of the allocated memory.
79651*/
79652SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere, u16 p5){
79653 int j;
79654 sqlite3VdbeAddOp4(p, OP_ParseSchema, p1: iDb, p2: 0, p3: 0, zP4: zWhere, P4_DYNAMIC);
79655 sqlite3VdbeChangeP5(p, P5: p5);
79656 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
79657 sqlite3MayAbort(p->pParse);
79658}
79659
79660/*
79661** Add an opcode that includes the p4 value as an integer.
79662*/
79663SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(
79664 Vdbe *p, /* Add the opcode to this VM */
79665 int op, /* The new opcode */
79666 int p1, /* The P1 operand */
79667 int p2, /* The P2 operand */
79668 int p3, /* The P3 operand */
79669 int p4 /* The P4 operand as an integer */
79670){
79671 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
79672 if( p->db->mallocFailed==0 ){
79673 VdbeOp *pOp = &p->aOp[addr];
79674 pOp->p4type = P4_INT32;
79675 pOp->p4.i = p4;
79676 }
79677 return addr;
79678}
79679
79680/* Insert the end of a co-routine
79681*/
79682SQLITE_PRIVATE void sqlite3VdbeEndCoroutine(Vdbe *v, int regYield){
79683 sqlite3VdbeAddOp1(p: v, OP_EndCoroutine, p1: regYield);
79684
79685 /* Clear the temporary register cache, thereby ensuring that each
79686 ** co-routine has its own independent set of registers, because co-routines
79687 ** might expect their registers to be preserved across an OP_Yield, and
79688 ** that could cause problems if two or more co-routines are using the same
79689 ** temporary register.
79690 */
79691 v->pParse->nTempReg = 0;
79692 v->pParse->nRangeReg = 0;
79693}
79694
79695/*
79696** Create a new symbolic label for an instruction that has yet to be
79697** coded. The symbolic label is really just a negative number. The
79698** label can be used as the P2 value of an operation. Later, when
79699** the label is resolved to a specific address, the VDBE will scan
79700** through its operation list and change all values of P2 which match
79701** the label into the resolved address.
79702**
79703** The VDBE knows that a P2 value is a label because labels are
79704** always negative and P2 values are suppose to be non-negative.
79705** Hence, a negative P2 value is a label that has yet to be resolved.
79706** (Later:) This is only true for opcodes that have the OPFLG_JUMP
79707** property.
79708**
79709** Variable usage notes:
79710**
79711** Parse.aLabel[x] Stores the address that the x-th label resolves
79712** into. For testing (SQLITE_DEBUG), unresolved
79713** labels stores -1, but that is not required.
79714** Parse.nLabelAlloc Number of slots allocated to Parse.aLabel[]
79715** Parse.nLabel The *negative* of the number of labels that have
79716** been issued. The negative is stored because
79717** that gives a performance improvement over storing
79718** the equivalent positive value.
79719*/
79720SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Parse *pParse){
79721 return --pParse->nLabel;
79722}
79723
79724/*
79725** Resolve label "x" to be the address of the next instruction to
79726** be inserted. The parameter "x" must have been obtained from
79727** a prior call to sqlite3VdbeMakeLabel().
79728*/
79729static SQLITE_NOINLINE void resizeResolveLabel(Parse *p, Vdbe *v, int j){
79730 int nNewSize = 10 - p->nLabel;
79731 p->aLabel = sqlite3DbReallocOrFree(db: p->db, p: p->aLabel,
79732 n: nNewSize*sizeof(p->aLabel[0]));
79733 if( p->aLabel==0 ){
79734 p->nLabelAlloc = 0;
79735 }else{
79736#ifdef SQLITE_DEBUG
79737 int i;
79738 for(i=p->nLabelAlloc; i<nNewSize; i++) p->aLabel[i] = -1;
79739#endif
79740 p->nLabelAlloc = nNewSize;
79741 p->aLabel[j] = v->nOp;
79742 }
79743}
79744SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe *v, int x){
79745 Parse *p = v->pParse;
79746 int j = ADDR(x);
79747 assert( v->iVdbeMagic==VDBE_MAGIC_INIT );
79748 assert( j<-p->nLabel );
79749 assert( j>=0 );
79750#ifdef SQLITE_DEBUG
79751 if( p->db->flags & SQLITE_VdbeAddopTrace ){
79752 printf("RESOLVE LABEL %d to %d\n", x, v->nOp);
79753 }
79754#endif
79755 if( p->nLabelAlloc + p->nLabel < 0 ){
79756 resizeResolveLabel(p,v,j);
79757 }else{
79758 assert( p->aLabel[j]==(-1) ); /* Labels may only be resolved once */
79759 p->aLabel[j] = v->nOp;
79760 }
79761}
79762
79763/*
79764** Mark the VDBE as one that can only be run one time.
79765*/
79766SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe *p){
79767 p->runOnlyOnce = 1;
79768}
79769
79770/*
79771** Mark the VDBE as one that can only be run multiple times.
79772*/
79773SQLITE_PRIVATE void sqlite3VdbeReusable(Vdbe *p){
79774 p->runOnlyOnce = 0;
79775}
79776
79777#ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
79778
79779/*
79780** The following type and function are used to iterate through all opcodes
79781** in a Vdbe main program and each of the sub-programs (triggers) it may
79782** invoke directly or indirectly. It should be used as follows:
79783**
79784** Op *pOp;
79785** VdbeOpIter sIter;
79786**
79787** memset(&sIter, 0, sizeof(sIter));
79788** sIter.v = v; // v is of type Vdbe*
79789** while( (pOp = opIterNext(&sIter)) ){
79790** // Do something with pOp
79791** }
79792** sqlite3DbFree(v->db, sIter.apSub);
79793**
79794*/
79795typedef struct VdbeOpIter VdbeOpIter;
79796struct VdbeOpIter {
79797 Vdbe *v; /* Vdbe to iterate through the opcodes of */
79798 SubProgram **apSub; /* Array of subprograms */
79799 int nSub; /* Number of entries in apSub */
79800 int iAddr; /* Address of next instruction to return */
79801 int iSub; /* 0 = main program, 1 = first sub-program etc. */
79802};
79803static Op *opIterNext(VdbeOpIter *p){
79804 Vdbe *v = p->v;
79805 Op *pRet = 0;
79806 Op *aOp;
79807 int nOp;
79808
79809 if( p->iSub<=p->nSub ){
79810
79811 if( p->iSub==0 ){
79812 aOp = v->aOp;
79813 nOp = v->nOp;
79814 }else{
79815 aOp = p->apSub[p->iSub-1]->aOp;
79816 nOp = p->apSub[p->iSub-1]->nOp;
79817 }
79818 assert( p->iAddr<nOp );
79819
79820 pRet = &aOp[p->iAddr];
79821 p->iAddr++;
79822 if( p->iAddr==nOp ){
79823 p->iSub++;
79824 p->iAddr = 0;
79825 }
79826
79827 if( pRet->p4type==P4_SUBPROGRAM ){
79828 int nByte = (p->nSub+1)*sizeof(SubProgram*);
79829 int j;
79830 for(j=0; j<p->nSub; j++){
79831 if( p->apSub[j]==pRet->p4.pProgram ) break;
79832 }
79833 if( j==p->nSub ){
79834 p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
79835 if( !p->apSub ){
79836 pRet = 0;
79837 }else{
79838 p->apSub[p->nSub++] = pRet->p4.pProgram;
79839 }
79840 }
79841 }
79842 }
79843
79844 return pRet;
79845}
79846
79847/*
79848** Check if the program stored in the VM associated with pParse may
79849** throw an ABORT exception (causing the statement, but not entire transaction
79850** to be rolled back). This condition is true if the main program or any
79851** sub-programs contains any of the following:
79852**
79853** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
79854** * OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
79855** * OP_Destroy
79856** * OP_VUpdate
79857** * OP_VCreate
79858** * OP_VRename
79859** * OP_FkCounter with P2==0 (immediate foreign key constraint)
79860** * OP_CreateBtree/BTREE_INTKEY and OP_InitCoroutine
79861** (for CREATE TABLE AS SELECT ...)
79862**
79863** Then check that the value of Parse.mayAbort is true if an
79864** ABORT may be thrown, or false otherwise. Return true if it does
79865** match, or false otherwise. This function is intended to be used as
79866** part of an assert statement in the compiler. Similar to:
79867**
79868** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
79869*/
79870SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
79871 int hasAbort = 0;
79872 int hasFkCounter = 0;
79873 int hasCreateTable = 0;
79874 int hasCreateIndex = 0;
79875 int hasInitCoroutine = 0;
79876 Op *pOp;
79877 VdbeOpIter sIter;
79878 memset(&sIter, 0, sizeof(sIter));
79879 sIter.v = v;
79880
79881 while( (pOp = opIterNext(&sIter))!=0 ){
79882 int opcode = pOp->opcode;
79883 if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename
79884 || opcode==OP_VDestroy
79885 || opcode==OP_VCreate
79886 || opcode==OP_ParseSchema
79887 || ((opcode==OP_Halt || opcode==OP_HaltIfNull)
79888 && ((pOp->p1)!=SQLITE_OK && pOp->p2==OE_Abort))
79889 ){
79890 hasAbort = 1;
79891 break;
79892 }
79893 if( opcode==OP_CreateBtree && pOp->p3==BTREE_INTKEY ) hasCreateTable = 1;
79894 if( mayAbort ){
79895 /* hasCreateIndex may also be set for some DELETE statements that use
79896 ** OP_Clear. So this routine may end up returning true in the case
79897 ** where a "DELETE FROM tbl" has a statement-journal but does not
79898 ** require one. This is not so bad - it is an inefficiency, not a bug. */
79899 if( opcode==OP_CreateBtree && pOp->p3==BTREE_BLOBKEY ) hasCreateIndex = 1;
79900 if( opcode==OP_Clear ) hasCreateIndex = 1;
79901 }
79902 if( opcode==OP_InitCoroutine ) hasInitCoroutine = 1;
79903#ifndef SQLITE_OMIT_FOREIGN_KEY
79904 if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){
79905 hasFkCounter = 1;
79906 }
79907#endif
79908 }
79909 sqlite3DbFree(v->db, sIter.apSub);
79910
79911 /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred.
79912 ** If malloc failed, then the while() loop above may not have iterated
79913 ** through all opcodes and hasAbort may be set incorrectly. Return
79914 ** true for this case to prevent the assert() in the callers frame
79915 ** from failing. */
79916 return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
79917 || (hasCreateTable && hasInitCoroutine) || hasCreateIndex
79918 );
79919}
79920#endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
79921
79922#ifdef SQLITE_DEBUG
79923/*
79924** Increment the nWrite counter in the VDBE if the cursor is not an
79925** ephemeral cursor, or if the cursor argument is NULL.
79926*/
79927SQLITE_PRIVATE void sqlite3VdbeIncrWriteCounter(Vdbe *p, VdbeCursor *pC){
79928 if( pC==0
79929 || (pC->eCurType!=CURTYPE_SORTER
79930 && pC->eCurType!=CURTYPE_PSEUDO
79931 && !pC->isEphemeral)
79932 ){
79933 p->nWrite++;
79934 }
79935}
79936#endif
79937
79938#ifdef SQLITE_DEBUG
79939/*
79940** Assert if an Abort at this point in time might result in a corrupt
79941** database.
79942*/
79943SQLITE_PRIVATE void sqlite3VdbeAssertAbortable(Vdbe *p){
79944 assert( p->nWrite==0 || p->usesStmtJournal );
79945}
79946#endif
79947
79948/*
79949** This routine is called after all opcodes have been inserted. It loops
79950** through all the opcodes and fixes up some details.
79951**
79952** (1) For each jump instruction with a negative P2 value (a label)
79953** resolve the P2 value to an actual address.
79954**
79955** (2) Compute the maximum number of arguments used by any SQL function
79956** and store that value in *pMaxFuncArgs.
79957**
79958** (3) Update the Vdbe.readOnly and Vdbe.bIsReader flags to accurately
79959** indicate what the prepared statement actually does.
79960**
79961** (4) Initialize the p4.xAdvance pointer on opcodes that use it.
79962**
79963** (5) Reclaim the memory allocated for storing labels.
79964**
79965** This routine will only function correctly if the mkopcodeh.tcl generator
79966** script numbers the opcodes correctly. Changes to this routine must be
79967** coordinated with changes to mkopcodeh.tcl.
79968*/
79969static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
79970 int nMaxArgs = *pMaxFuncArgs;
79971 Op *pOp;
79972 Parse *pParse = p->pParse;
79973 int *aLabel = pParse->aLabel;
79974 p->readOnly = 1;
79975 p->bIsReader = 0;
79976 pOp = &p->aOp[p->nOp-1];
79977 while(1){
79978
79979 /* Only JUMP opcodes and the short list of special opcodes in the switch
79980 ** below need to be considered. The mkopcodeh.tcl generator script groups
79981 ** all these opcodes together near the front of the opcode list. Skip
79982 ** any opcode that does not need processing by virtual of the fact that
79983 ** it is larger than SQLITE_MX_JUMP_OPCODE, as a performance optimization.
79984 */
79985 if( pOp->opcode<=SQLITE_MX_JUMP_OPCODE ){
79986 /* NOTE: Be sure to update mkopcodeh.tcl when adding or removing
79987 ** cases from this switch! */
79988 switch( pOp->opcode ){
79989 case OP_Transaction: {
79990 if( pOp->p2!=0 ) p->readOnly = 0;
79991 /* no break */ deliberate_fall_through
79992 }
79993 case OP_AutoCommit:
79994 case OP_Savepoint: {
79995 p->bIsReader = 1;
79996 break;
79997 }
79998#ifndef SQLITE_OMIT_WAL
79999 case OP_Checkpoint:
80000#endif
80001 case OP_Vacuum:
80002 case OP_JournalMode: {
80003 p->readOnly = 0;
80004 p->bIsReader = 1;
80005 break;
80006 }
80007 case OP_Next:
80008 case OP_SorterNext: {
80009 pOp->p4.xAdvance = sqlite3BtreeNext;
80010 pOp->p4type = P4_ADVANCE;
80011 /* The code generator never codes any of these opcodes as a jump
80012 ** to a label. They are always coded as a jump backwards to a
80013 ** known address */
80014 assert( pOp->p2>=0 );
80015 break;
80016 }
80017 case OP_Prev: {
80018 pOp->p4.xAdvance = sqlite3BtreePrevious;
80019 pOp->p4type = P4_ADVANCE;
80020 /* The code generator never codes any of these opcodes as a jump
80021 ** to a label. They are always coded as a jump backwards to a
80022 ** known address */
80023 assert( pOp->p2>=0 );
80024 break;
80025 }
80026#ifndef SQLITE_OMIT_VIRTUALTABLE
80027 case OP_VUpdate: {
80028 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
80029 break;
80030 }
80031 case OP_VFilter: {
80032 int n;
80033 assert( (pOp - p->aOp) >= 3 );
80034 assert( pOp[-1].opcode==OP_Integer );
80035 n = pOp[-1].p1;
80036 if( n>nMaxArgs ) nMaxArgs = n;
80037 /* Fall through into the default case */
80038 /* no break */ deliberate_fall_through
80039 }
80040#endif
80041 default: {
80042 if( pOp->p2<0 ){
80043 /* The mkopcodeh.tcl script has so arranged things that the only
80044 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
80045 ** have non-negative values for P2. */
80046 assert( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 );
80047 assert( ADDR(pOp->p2)<-pParse->nLabel );
80048 pOp->p2 = aLabel[ADDR(pOp->p2)];
80049 }
80050 break;
80051 }
80052 }
80053 /* The mkopcodeh.tcl script has so arranged things that the only
80054 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
80055 ** have non-negative values for P2. */
80056 assert( (sqlite3OpcodeProperty[pOp->opcode]&OPFLG_JUMP)==0 || pOp->p2>=0);
80057 }
80058 if( pOp==p->aOp ) break;
80059 pOp--;
80060 }
80061 sqlite3DbFree(db: p->db, p: pParse->aLabel);
80062 pParse->aLabel = 0;
80063 pParse->nLabel = 0;
80064 *pMaxFuncArgs = nMaxArgs;
80065 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
80066}
80067
80068/*
80069** Return the address of the next instruction to be inserted.
80070*/
80071SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe *p){
80072 assert( p->iVdbeMagic==VDBE_MAGIC_INIT );
80073 return p->nOp;
80074}
80075
80076/*
80077** Verify that at least N opcode slots are available in p without
80078** having to malloc for more space (except when compiled using
80079** SQLITE_TEST_REALLOC_STRESS). This interface is used during testing
80080** to verify that certain calls to sqlite3VdbeAddOpList() can never
80081** fail due to a OOM fault and hence that the return value from
80082** sqlite3VdbeAddOpList() will always be non-NULL.
80083*/
80084#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
80085SQLITE_PRIVATE void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N){
80086 assert( p->nOp + N <= p->nOpAlloc );
80087}
80088#endif
80089
80090/*
80091** Verify that the VM passed as the only argument does not contain
80092** an OP_ResultRow opcode. Fail an assert() if it does. This is used
80093** by code in pragma.c to ensure that the implementation of certain
80094** pragmas comports with the flags specified in the mkpragmatab.tcl
80095** script.
80096*/
80097#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
80098SQLITE_PRIVATE void sqlite3VdbeVerifyNoResultRow(Vdbe *p){
80099 int i;
80100 for(i=0; i<p->nOp; i++){
80101 assert( p->aOp[i].opcode!=OP_ResultRow );
80102 }
80103}
80104#endif
80105
80106/*
80107** Generate code (a single OP_Abortable opcode) that will
80108** verify that the VDBE program can safely call Abort in the current
80109** context.
80110*/
80111#if defined(SQLITE_DEBUG)
80112SQLITE_PRIVATE void sqlite3VdbeVerifyAbortable(Vdbe *p, int onError){
80113 if( onError==OE_Abort ) sqlite3VdbeAddOp0(p, OP_Abortable);
80114}
80115#endif
80116
80117/*
80118** This function returns a pointer to the array of opcodes associated with
80119** the Vdbe passed as the first argument. It is the callers responsibility
80120** to arrange for the returned array to be eventually freed using the
80121** vdbeFreeOpArray() function.
80122**
80123** Before returning, *pnOp is set to the number of entries in the returned
80124** array. Also, *pnMaxArg is set to the larger of its current value and
80125** the number of entries in the Vdbe.apArg[] array required to execute the
80126** returned program.
80127*/
80128SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
80129 VdbeOp *aOp = p->aOp;
80130 assert( aOp && !p->db->mallocFailed );
80131
80132 /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
80133 assert( DbMaskAllZero(p->btreeMask) );
80134
80135 resolveP2Values(p, pMaxFuncArgs: pnMaxArg);
80136 *pnOp = p->nOp;
80137 p->aOp = 0;
80138 return aOp;
80139}
80140
80141/*
80142** Add a whole list of operations to the operation stack. Return a
80143** pointer to the first operation inserted.
80144**
80145** Non-zero P2 arguments to jump instructions are automatically adjusted
80146** so that the jump target is relative to the first operation inserted.
80147*/
80148SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(
80149 Vdbe *p, /* Add opcodes to the prepared statement */
80150 int nOp, /* Number of opcodes to add */
80151 VdbeOpList const *aOp, /* The opcodes to be added */
80152 int iLineno /* Source-file line number of first opcode */
80153){
80154 int i;
80155 VdbeOp *pOut, *pFirst;
80156 assert( nOp>0 );
80157 assert( p->iVdbeMagic==VDBE_MAGIC_INIT );
80158 if( p->nOp + nOp > p->nOpAlloc && growOpArray(v: p, nOp) ){
80159 return 0;
80160 }
80161 pFirst = pOut = &p->aOp[p->nOp];
80162 for(i=0; i<nOp; i++, aOp++, pOut++){
80163 pOut->opcode = aOp->opcode;
80164 pOut->p1 = aOp->p1;
80165 pOut->p2 = aOp->p2;
80166 assert( aOp->p2>=0 );
80167 if( (sqlite3OpcodeProperty[aOp->opcode] & OPFLG_JUMP)!=0 && aOp->p2>0 ){
80168 pOut->p2 += p->nOp;
80169 }
80170 pOut->p3 = aOp->p3;
80171 pOut->p4type = P4_NOTUSED;
80172 pOut->p4.p = 0;
80173 pOut->p5 = 0;
80174#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
80175 pOut->zComment = 0;
80176#endif
80177#ifdef SQLITE_VDBE_COVERAGE
80178 pOut->iSrcLine = iLineno+i;
80179#else
80180 (void)iLineno;
80181#endif
80182#ifdef SQLITE_DEBUG
80183 if( p->db->flags & SQLITE_VdbeAddopTrace ){
80184 sqlite3VdbePrintOp(0, i+p->nOp, &p->aOp[i+p->nOp]);
80185 }
80186#endif
80187 }
80188 p->nOp += nOp;
80189 return pFirst;
80190}
80191
80192#if defined(SQLITE_ENABLE_STMT_SCANSTATUS)
80193/*
80194** Add an entry to the array of counters managed by sqlite3_stmt_scanstatus().
80195*/
80196SQLITE_PRIVATE void sqlite3VdbeScanStatus(
80197 Vdbe *p, /* VM to add scanstatus() to */
80198 int addrExplain, /* Address of OP_Explain (or 0) */
80199 int addrLoop, /* Address of loop counter */
80200 int addrVisit, /* Address of rows visited counter */
80201 LogEst nEst, /* Estimated number of output rows */
80202 const char *zName /* Name of table or index being scanned */
80203){
80204 sqlite3_int64 nByte = (p->nScan+1) * sizeof(ScanStatus);
80205 ScanStatus *aNew;
80206 aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
80207 if( aNew ){
80208 ScanStatus *pNew = &aNew[p->nScan++];
80209 pNew->addrExplain = addrExplain;
80210 pNew->addrLoop = addrLoop;
80211 pNew->addrVisit = addrVisit;
80212 pNew->nEst = nEst;
80213 pNew->zName = sqlite3DbStrDup(p->db, zName);
80214 p->aScan = aNew;
80215 }
80216}
80217#endif
80218
80219
80220/*
80221** Change the value of the opcode, or P1, P2, P3, or P5 operands
80222** for a specific instruction.
80223*/
80224SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe *p, int addr, u8 iNewOpcode){
80225 sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode;
80226}
80227SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
80228 sqlite3VdbeGetOp(p,addr)->p1 = val;
80229}
80230SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
80231 sqlite3VdbeGetOp(p,addr)->p2 = val;
80232}
80233SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){
80234 sqlite3VdbeGetOp(p,addr)->p3 = val;
80235}
80236SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){
80237 assert( p->nOp>0 || p->db->mallocFailed );
80238 if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
80239}
80240
80241/*
80242** Change the P2 operand of instruction addr so that it points to
80243** the address of the next instruction to be coded.
80244*/
80245SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
80246 sqlite3VdbeChangeP2(p, addr, val: p->nOp);
80247}
80248
80249/*
80250** Change the P2 operand of the jump instruction at addr so that
80251** the jump lands on the next opcode. Or if the jump instruction was
80252** the previous opcode (and is thus a no-op) then simply back up
80253** the next instruction counter by one slot so that the jump is
80254** overwritten by the next inserted opcode.
80255**
80256** This routine is an optimization of sqlite3VdbeJumpHere() that
80257** strives to omit useless byte-code like this:
80258**
80259** 7 Once 0 8 0
80260** 8 ...
80261*/
80262SQLITE_PRIVATE void sqlite3VdbeJumpHereOrPopInst(Vdbe *p, int addr){
80263 if( addr==p->nOp-1 ){
80264 assert( p->aOp[addr].opcode==OP_Once
80265 || p->aOp[addr].opcode==OP_If
80266 || p->aOp[addr].opcode==OP_FkIfZero );
80267 assert( p->aOp[addr].p4type==0 );
80268#ifdef SQLITE_VDBE_COVERAGE
80269 sqlite3VdbeGetOp(p,-1)->iSrcLine = 0; /* Erase VdbeCoverage() macros */
80270#endif
80271 p->nOp--;
80272 }else{
80273 sqlite3VdbeChangeP2(p, addr, val: p->nOp);
80274 }
80275}
80276
80277
80278/*
80279** If the input FuncDef structure is ephemeral, then free it. If
80280** the FuncDef is not ephermal, then do nothing.
80281*/
80282static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
80283 if( (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
80284 sqlite3DbFreeNN(db, p: pDef);
80285 }
80286}
80287
80288/*
80289** Delete a P4 value if necessary.
80290*/
80291static SQLITE_NOINLINE void freeP4Mem(sqlite3 *db, Mem *p){
80292 if( p->szMalloc ) sqlite3DbFree(db, p: p->zMalloc);
80293 sqlite3DbFreeNN(db, p);
80294}
80295static SQLITE_NOINLINE void freeP4FuncCtx(sqlite3 *db, sqlite3_context *p){
80296 freeEphemeralFunction(db, pDef: p->pFunc);
80297 sqlite3DbFreeNN(db, p);
80298}
80299static void freeP4(sqlite3 *db, int p4type, void *p4){
80300 assert( db );
80301 switch( p4type ){
80302 case P4_FUNCCTX: {
80303 freeP4FuncCtx(db, p: (sqlite3_context*)p4);
80304 break;
80305 }
80306 case P4_REAL:
80307 case P4_INT64:
80308 case P4_DYNAMIC:
80309 case P4_DYNBLOB:
80310 case P4_INTARRAY: {
80311 sqlite3DbFree(db, p: p4);
80312 break;
80313 }
80314 case P4_KEYINFO: {
80315 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
80316 break;
80317 }
80318#ifdef SQLITE_ENABLE_CURSOR_HINTS
80319 case P4_EXPR: {
80320 sqlite3ExprDelete(db, (Expr*)p4);
80321 break;
80322 }
80323#endif
80324 case P4_FUNCDEF: {
80325 freeEphemeralFunction(db, pDef: (FuncDef*)p4);
80326 break;
80327 }
80328 case P4_MEM: {
80329 if( db->pnBytesFreed==0 ){
80330 sqlite3ValueFree(v: (sqlite3_value*)p4);
80331 }else{
80332 freeP4Mem(db, p: (Mem*)p4);
80333 }
80334 break;
80335 }
80336 case P4_VTAB : {
80337 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
80338 break;
80339 }
80340 }
80341}
80342
80343/*
80344** Free the space allocated for aOp and any p4 values allocated for the
80345** opcodes contained within. If aOp is not NULL it is assumed to contain
80346** nOp entries.
80347*/
80348static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
80349 if( aOp ){
80350 Op *pOp;
80351 for(pOp=&aOp[nOp-1]; pOp>=aOp; pOp--){
80352 if( pOp->p4type <= P4_FREE_IF_LE ) freeP4(db, p4type: pOp->p4type, p4: pOp->p4.p);
80353#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
80354 sqlite3DbFree(db, pOp->zComment);
80355#endif
80356 }
80357 sqlite3DbFreeNN(db, p: aOp);
80358 }
80359}
80360
80361/*
80362** Link the SubProgram object passed as the second argument into the linked
80363** list at Vdbe.pSubProgram. This list is used to delete all sub-program
80364** objects when the VM is no longer required.
80365*/
80366SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
80367 p->pNext = pVdbe->pProgram;
80368 pVdbe->pProgram = p;
80369}
80370
80371/*
80372** Return true if the given Vdbe has any SubPrograms.
80373*/
80374SQLITE_PRIVATE int sqlite3VdbeHasSubProgram(Vdbe *pVdbe){
80375 return pVdbe->pProgram!=0;
80376}
80377
80378/*
80379** Change the opcode at addr into OP_Noop
80380*/
80381SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
80382 VdbeOp *pOp;
80383 if( p->db->mallocFailed ) return 0;
80384 assert( addr>=0 && addr<p->nOp );
80385 pOp = &p->aOp[addr];
80386 freeP4(db: p->db, p4type: pOp->p4type, p4: pOp->p4.p);
80387 pOp->p4type = P4_NOTUSED;
80388 pOp->p4.z = 0;
80389 pOp->opcode = OP_Noop;
80390 return 1;
80391}
80392
80393/*
80394** If the last opcode is "op" and it is not a jump destination,
80395** then remove it. Return true if and only if an opcode was removed.
80396*/
80397SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
80398 if( p->nOp>0 && p->aOp[p->nOp-1].opcode==op ){
80399 return sqlite3VdbeChangeToNoop(p, addr: p->nOp-1);
80400 }else{
80401 return 0;
80402 }
80403}
80404
80405#ifdef SQLITE_DEBUG
80406/*
80407** Generate an OP_ReleaseReg opcode to indicate that a range of
80408** registers, except any identified by mask, are no longer in use.
80409*/
80410SQLITE_PRIVATE void sqlite3VdbeReleaseRegisters(
80411 Parse *pParse, /* Parsing context */
80412 int iFirst, /* Index of first register to be released */
80413 int N, /* Number of registers to release */
80414 u32 mask, /* Mask of registers to NOT release */
80415 int bUndefine /* If true, mark registers as undefined */
80416){
80417 if( N==0 ) return;
80418 assert( pParse->pVdbe );
80419 assert( iFirst>=1 );
80420 assert( iFirst+N-1<=pParse->nMem );
80421 if( N<=31 && mask!=0 ){
80422 while( N>0 && (mask&1)!=0 ){
80423 mask >>= 1;
80424 iFirst++;
80425 N--;
80426 }
80427 while( N>0 && N<=32 && (mask & MASKBIT32(N-1))!=0 ){
80428 mask &= ~MASKBIT32(N-1);
80429 N--;
80430 }
80431 }
80432 if( N>0 ){
80433 sqlite3VdbeAddOp3(pParse->pVdbe, OP_ReleaseReg, iFirst, N, *(int*)&mask);
80434 if( bUndefine ) sqlite3VdbeChangeP5(pParse->pVdbe, 1);
80435 }
80436}
80437#endif /* SQLITE_DEBUG */
80438
80439
80440/*
80441** Change the value of the P4 operand for a specific instruction.
80442** This routine is useful when a large program is loaded from a
80443** static array using sqlite3VdbeAddOpList but we want to make a
80444** few minor changes to the program.
80445**
80446** If n>=0 then the P4 operand is dynamic, meaning that a copy of
80447** the string is made into memory obtained from sqlite3_malloc().
80448** A value of n==0 means copy bytes of zP4 up to and including the
80449** first null byte. If n>0 then copy n+1 bytes of zP4.
80450**
80451** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
80452** to a string or structure that is guaranteed to exist for the lifetime of
80453** the Vdbe. In these cases we can just copy the pointer.
80454**
80455** If addr<0 then change P4 on the most recently inserted instruction.
80456*/
80457static void SQLITE_NOINLINE vdbeChangeP4Full(
80458 Vdbe *p,
80459 Op *pOp,
80460 const char *zP4,
80461 int n
80462){
80463 if( pOp->p4type ){
80464 freeP4(db: p->db, p4type: pOp->p4type, p4: pOp->p4.p);
80465 pOp->p4type = 0;
80466 pOp->p4.p = 0;
80467 }
80468 if( n<0 ){
80469 sqlite3VdbeChangeP4(p, addr: (int)(pOp - p->aOp), zP4, N: n);
80470 }else{
80471 if( n==0 ) n = sqlite3Strlen30(z: zP4);
80472 pOp->p4.z = sqlite3DbStrNDup(db: p->db, z: zP4, n);
80473 pOp->p4type = P4_DYNAMIC;
80474 }
80475}
80476SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
80477 Op *pOp;
80478 sqlite3 *db;
80479 assert( p!=0 );
80480 db = p->db;
80481 assert( p->iVdbeMagic==VDBE_MAGIC_INIT );
80482 assert( p->aOp!=0 || db->mallocFailed );
80483 if( db->mallocFailed ){
80484 if( n!=P4_VTAB ) freeP4(db, p4type: n, p4: (void*)*(char**)&zP4);
80485 return;
80486 }
80487 assert( p->nOp>0 );
80488 assert( addr<p->nOp );
80489 if( addr<0 ){
80490 addr = p->nOp - 1;
80491 }
80492 pOp = &p->aOp[addr];
80493 if( n>=0 || pOp->p4type ){
80494 vdbeChangeP4Full(p, pOp, zP4, n);
80495 return;
80496 }
80497 if( n==P4_INT32 ){
80498 /* Note: this cast is safe, because the origin data point was an int
80499 ** that was cast to a (const char *). */
80500 pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
80501 pOp->p4type = P4_INT32;
80502 }else if( zP4!=0 ){
80503 assert( n<0 );
80504 pOp->p4.p = (void*)zP4;
80505 pOp->p4type = (signed char)n;
80506 if( n==P4_VTAB ) sqlite3VtabLock((VTable*)zP4);
80507 }
80508}
80509
80510/*
80511** Change the P4 operand of the most recently coded instruction
80512** to the value defined by the arguments. This is a high-speed
80513** version of sqlite3VdbeChangeP4().
80514**
80515** The P4 operand must not have been previously defined. And the new
80516** P4 must not be P4_INT32. Use sqlite3VdbeChangeP4() in either of
80517** those cases.
80518*/
80519SQLITE_PRIVATE void sqlite3VdbeAppendP4(Vdbe *p, void *pP4, int n){
80520 VdbeOp *pOp;
80521 assert( n!=P4_INT32 && n!=P4_VTAB );
80522 assert( n<=0 );
80523 if( p->db->mallocFailed ){
80524 freeP4(db: p->db, p4type: n, p4: pP4);
80525 }else{
80526 assert( pP4!=0 );
80527 assert( p->nOp>0 );
80528 pOp = &p->aOp[p->nOp-1];
80529 assert( pOp->p4type==P4_NOTUSED );
80530 pOp->p4type = n;
80531 pOp->p4.p = pP4;
80532 }
80533}
80534
80535/*
80536** Set the P4 on the most recently added opcode to the KeyInfo for the
80537** index given.
80538*/
80539SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){
80540 Vdbe *v = pParse->pVdbe;
80541 KeyInfo *pKeyInfo;
80542 assert( v!=0 );
80543 assert( pIdx!=0 );
80544 pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pIdx);
80545 if( pKeyInfo ) sqlite3VdbeAppendP4(p: v, pP4: pKeyInfo, P4_KEYINFO);
80546}
80547
80548#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
80549/*
80550** Change the comment on the most recently coded instruction. Or
80551** insert a No-op and add the comment to that new instruction. This
80552** makes the code easier to read during debugging. None of this happens
80553** in a production build.
80554*/
80555static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){
80556 assert( p->nOp>0 || p->aOp==0 );
80557 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed
80558 || p->pParse->nErr>0 );
80559 if( p->nOp ){
80560 assert( p->aOp );
80561 sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
80562 p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
80563 }
80564}
80565SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
80566 va_list ap;
80567 if( p ){
80568 va_start(ap, zFormat);
80569 vdbeVComment(p, zFormat, ap);
80570 va_end(ap);
80571 }
80572}
80573SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
80574 va_list ap;
80575 if( p ){
80576 sqlite3VdbeAddOp0(p, OP_Noop);
80577 va_start(ap, zFormat);
80578 vdbeVComment(p, zFormat, ap);
80579 va_end(ap);
80580 }
80581}
80582#endif /* NDEBUG */
80583
80584#ifdef SQLITE_VDBE_COVERAGE
80585/*
80586** Set the value if the iSrcLine field for the previously coded instruction.
80587*/
80588SQLITE_PRIVATE void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){
80589 sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine;
80590}
80591#endif /* SQLITE_VDBE_COVERAGE */
80592
80593/*
80594** Return the opcode for a given address. If the address is -1, then
80595** return the most recently inserted opcode.
80596**
80597** If a memory allocation error has occurred prior to the calling of this
80598** routine, then a pointer to a dummy VdbeOp will be returned. That opcode
80599** is readable but not writable, though it is cast to a writable value.
80600** The return of a dummy opcode allows the call to continue functioning
80601** after an OOM fault without having to check to see if the return from
80602** this routine is a valid pointer. But because the dummy.opcode is 0,
80603** dummy will never be written to. This is verified by code inspection and
80604** by running with Valgrind.
80605*/
80606SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
80607 /* C89 specifies that the constant "dummy" will be initialized to all
80608 ** zeros, which is correct. MSVC generates a warning, nevertheless. */
80609 static VdbeOp dummy; /* Ignore the MSVC warning about no initializer */
80610 assert( p->iVdbeMagic==VDBE_MAGIC_INIT );
80611 if( addr<0 ){
80612 addr = p->nOp - 1;
80613 }
80614 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
80615 if( p->db->mallocFailed ){
80616 return (VdbeOp*)&dummy;
80617 }else{
80618 return &p->aOp[addr];
80619 }
80620}
80621
80622#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
80623/*
80624** Return an integer value for one of the parameters to the opcode pOp
80625** determined by character c.
80626*/
80627static int translateP(char c, const Op *pOp){
80628 if( c=='1' ) return pOp->p1;
80629 if( c=='2' ) return pOp->p2;
80630 if( c=='3' ) return pOp->p3;
80631 if( c=='4' ) return pOp->p4.i;
80632 return pOp->p5;
80633}
80634
80635/*
80636** Compute a string for the "comment" field of a VDBE opcode listing.
80637**
80638** The Synopsis: field in comments in the vdbe.c source file gets converted
80639** to an extra string that is appended to the sqlite3OpcodeName(). In the
80640** absence of other comments, this synopsis becomes the comment on the opcode.
80641** Some translation occurs:
80642**
80643** "PX" -> "r[X]"
80644** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1
80645** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0
80646** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x
80647*/
80648SQLITE_PRIVATE char *sqlite3VdbeDisplayComment(
80649 sqlite3 *db, /* Optional - Oom error reporting only */
80650 const Op *pOp, /* The opcode to be commented */
80651 const char *zP4 /* Previously obtained value for P4 */
80652){
80653 const char *zOpName;
80654 const char *zSynopsis;
80655 int nOpName;
80656 int ii;
80657 char zAlt[50];
80658 StrAccum x;
80659
80660 sqlite3StrAccumInit(&x, 0, 0, 0, SQLITE_MAX_LENGTH);
80661 zOpName = sqlite3OpcodeName(pOp->opcode);
80662 nOpName = sqlite3Strlen30(zOpName);
80663 if( zOpName[nOpName+1] ){
80664 int seenCom = 0;
80665 char c;
80666 zSynopsis = zOpName + nOpName + 1;
80667 if( strncmp(zSynopsis,"IF ",3)==0 ){
80668 sqlite3_snprintf(sizeof(zAlt), zAlt, "if %s goto P2", zSynopsis+3);
80669 zSynopsis = zAlt;
80670 }
80671 for(ii=0; (c = zSynopsis[ii])!=0; ii++){
80672 if( c=='P' ){
80673 c = zSynopsis[++ii];
80674 if( c=='4' ){
80675 sqlite3_str_appendall(&x, zP4);
80676 }else if( c=='X' ){
80677 sqlite3_str_appendall(&x, pOp->zComment);
80678 seenCom = 1;
80679 }else{
80680 int v1 = translateP(c, pOp);
80681 int v2;
80682 if( strncmp(zSynopsis+ii+1, "@P", 2)==0 ){
80683 ii += 3;
80684 v2 = translateP(zSynopsis[ii], pOp);
80685 if( strncmp(zSynopsis+ii+1,"+1",2)==0 ){
80686 ii += 2;
80687 v2++;
80688 }
80689 if( v2<2 ){
80690 sqlite3_str_appendf(&x, "%d", v1);
80691 }else{
80692 sqlite3_str_appendf(&x, "%d..%d", v1, v1+v2-1);
80693 }
80694 }else if( strncmp(zSynopsis+ii+1, "@NP", 3)==0 ){
80695 sqlite3_context *pCtx = pOp->p4.pCtx;
80696 if( pOp->p4type!=P4_FUNCCTX || pCtx->argc==1 ){
80697 sqlite3_str_appendf(&x, "%d", v1);
80698 }else if( pCtx->argc>1 ){
80699 sqlite3_str_appendf(&x, "%d..%d", v1, v1+pCtx->argc-1);
80700 }else if( x.accError==0 ){
80701 assert( x.nChar>2 );
80702 x.nChar -= 2;
80703 ii++;
80704 }
80705 ii += 3;
80706 }else{
80707 sqlite3_str_appendf(&x, "%d", v1);
80708 if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
80709 ii += 4;
80710 }
80711 }
80712 }
80713 }else{
80714 sqlite3_str_appendchar(&x, 1, c);
80715 }
80716 }
80717 if( !seenCom && pOp->zComment ){
80718 sqlite3_str_appendf(&x, "; %s", pOp->zComment);
80719 }
80720 }else if( pOp->zComment ){
80721 sqlite3_str_appendall(&x, pOp->zComment);
80722 }
80723 if( (x.accError & SQLITE_NOMEM)!=0 && db!=0 ){
80724 sqlite3OomFault(db);
80725 }
80726 return sqlite3StrAccumFinish(&x);
80727}
80728#endif /* SQLITE_ENABLE_EXPLAIN_COMMENTS */
80729
80730#if VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS)
80731/*
80732** Translate the P4.pExpr value for an OP_CursorHint opcode into text
80733** that can be displayed in the P4 column of EXPLAIN output.
80734*/
80735static void displayP4Expr(StrAccum *p, Expr *pExpr){
80736 const char *zOp = 0;
80737 switch( pExpr->op ){
80738 case TK_STRING:
80739 assert( !ExprHasProperty(pExpr, EP_IntValue) );
80740 sqlite3_str_appendf(p, "%Q", pExpr->u.zToken);
80741 break;
80742 case TK_INTEGER:
80743 sqlite3_str_appendf(p, "%d", pExpr->u.iValue);
80744 break;
80745 case TK_NULL:
80746 sqlite3_str_appendf(p, "NULL");
80747 break;
80748 case TK_REGISTER: {
80749 sqlite3_str_appendf(p, "r[%d]", pExpr->iTable);
80750 break;
80751 }
80752 case TK_COLUMN: {
80753 if( pExpr->iColumn<0 ){
80754 sqlite3_str_appendf(p, "rowid");
80755 }else{
80756 sqlite3_str_appendf(p, "c%d", (int)pExpr->iColumn);
80757 }
80758 break;
80759 }
80760 case TK_LT: zOp = "LT"; break;
80761 case TK_LE: zOp = "LE"; break;
80762 case TK_GT: zOp = "GT"; break;
80763 case TK_GE: zOp = "GE"; break;
80764 case TK_NE: zOp = "NE"; break;
80765 case TK_EQ: zOp = "EQ"; break;
80766 case TK_IS: zOp = "IS"; break;
80767 case TK_ISNOT: zOp = "ISNOT"; break;
80768 case TK_AND: zOp = "AND"; break;
80769 case TK_OR: zOp = "OR"; break;
80770 case TK_PLUS: zOp = "ADD"; break;
80771 case TK_STAR: zOp = "MUL"; break;
80772 case TK_MINUS: zOp = "SUB"; break;
80773 case TK_REM: zOp = "REM"; break;
80774 case TK_BITAND: zOp = "BITAND"; break;
80775 case TK_BITOR: zOp = "BITOR"; break;
80776 case TK_SLASH: zOp = "DIV"; break;
80777 case TK_LSHIFT: zOp = "LSHIFT"; break;
80778 case TK_RSHIFT: zOp = "RSHIFT"; break;
80779 case TK_CONCAT: zOp = "CONCAT"; break;
80780 case TK_UMINUS: zOp = "MINUS"; break;
80781 case TK_UPLUS: zOp = "PLUS"; break;
80782 case TK_BITNOT: zOp = "BITNOT"; break;
80783 case TK_NOT: zOp = "NOT"; break;
80784 case TK_ISNULL: zOp = "ISNULL"; break;
80785 case TK_NOTNULL: zOp = "NOTNULL"; break;
80786
80787 default:
80788 sqlite3_str_appendf(p, "%s", "expr");
80789 break;
80790 }
80791
80792 if( zOp ){
80793 sqlite3_str_appendf(p, "%s(", zOp);
80794 displayP4Expr(p, pExpr->pLeft);
80795 if( pExpr->pRight ){
80796 sqlite3_str_append(p, ",", 1);
80797 displayP4Expr(p, pExpr->pRight);
80798 }
80799 sqlite3_str_append(p, ")", 1);
80800 }
80801}
80802#endif /* VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) */
80803
80804
80805#if VDBE_DISPLAY_P4
80806/*
80807** Compute a string that describes the P4 parameter for an opcode.
80808** Use zTemp for any required temporary buffer space.
80809*/
80810SQLITE_PRIVATE char *sqlite3VdbeDisplayP4(sqlite3 *db, Op *pOp){
80811 char *zP4 = 0;
80812 StrAccum x;
80813
80814 sqlite3StrAccumInit(p: &x, db: 0, zBase: 0, n: 0, SQLITE_MAX_LENGTH);
80815 switch( pOp->p4type ){
80816 case P4_KEYINFO: {
80817 int j;
80818 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
80819 assert( pKeyInfo->aSortFlags!=0 );
80820 sqlite3_str_appendf(p: &x, zFormat: "k(%d", pKeyInfo->nKeyField);
80821 for(j=0; j<pKeyInfo->nKeyField; j++){
80822 CollSeq *pColl = pKeyInfo->aColl[j];
80823 const char *zColl = pColl ? pColl->zName : "";
80824 if( strcmp(s1: zColl, s2: "BINARY")==0 ) zColl = "B";
80825 sqlite3_str_appendf(p: &x, zFormat: ",%s%s%s",
80826 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_DESC) ? "-" : "",
80827 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_BIGNULL)? "N." : "",
80828 zColl);
80829 }
80830 sqlite3_str_append(p: &x, z: ")", N: 1);
80831 break;
80832 }
80833#ifdef SQLITE_ENABLE_CURSOR_HINTS
80834 case P4_EXPR: {
80835 displayP4Expr(&x, pOp->p4.pExpr);
80836 break;
80837 }
80838#endif
80839 case P4_COLLSEQ: {
80840 static const char *const encnames[] = {"?", "8", "16LE", "16BE"};
80841 CollSeq *pColl = pOp->p4.pColl;
80842 assert( pColl->enc<4 );
80843 sqlite3_str_appendf(p: &x, zFormat: "%.18s-%s", pColl->zName,
80844 encnames[pColl->enc]);
80845 break;
80846 }
80847 case P4_FUNCDEF: {
80848 FuncDef *pDef = pOp->p4.pFunc;
80849 sqlite3_str_appendf(p: &x, zFormat: "%s(%d)", pDef->zName, pDef->nArg);
80850 break;
80851 }
80852 case P4_FUNCCTX: {
80853 FuncDef *pDef = pOp->p4.pCtx->pFunc;
80854 sqlite3_str_appendf(p: &x, zFormat: "%s(%d)", pDef->zName, pDef->nArg);
80855 break;
80856 }
80857 case P4_INT64: {
80858 sqlite3_str_appendf(p: &x, zFormat: "%lld", *pOp->p4.pI64);
80859 break;
80860 }
80861 case P4_INT32: {
80862 sqlite3_str_appendf(p: &x, zFormat: "%d", pOp->p4.i);
80863 break;
80864 }
80865 case P4_REAL: {
80866 sqlite3_str_appendf(p: &x, zFormat: "%.16g", *pOp->p4.pReal);
80867 break;
80868 }
80869 case P4_MEM: {
80870 Mem *pMem = pOp->p4.pMem;
80871 if( pMem->flags & MEM_Str ){
80872 zP4 = pMem->z;
80873 }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){
80874 sqlite3_str_appendf(p: &x, zFormat: "%lld", pMem->u.i);
80875 }else if( pMem->flags & MEM_Real ){
80876 sqlite3_str_appendf(p: &x, zFormat: "%.16g", pMem->u.r);
80877 }else if( pMem->flags & MEM_Null ){
80878 zP4 = "NULL";
80879 }else{
80880 assert( pMem->flags & MEM_Blob );
80881 zP4 = "(blob)";
80882 }
80883 break;
80884 }
80885#ifndef SQLITE_OMIT_VIRTUALTABLE
80886 case P4_VTAB: {
80887 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
80888 sqlite3_str_appendf(p: &x, zFormat: "vtab:%p", pVtab);
80889 break;
80890 }
80891#endif
80892 case P4_INTARRAY: {
80893 u32 i;
80894 u32 *ai = pOp->p4.ai;
80895 u32 n = ai[0]; /* The first element of an INTARRAY is always the
80896 ** count of the number of elements to follow */
80897 for(i=1; i<=n; i++){
80898 sqlite3_str_appendf(p: &x, zFormat: "%c%u", (i==1 ? '[' : ','), ai[i]);
80899 }
80900 sqlite3_str_append(p: &x, z: "]", N: 1);
80901 break;
80902 }
80903 case P4_SUBPROGRAM: {
80904 zP4 = "program";
80905 break;
80906 }
80907 case P4_DYNBLOB:
80908 case P4_ADVANCE: {
80909 break;
80910 }
80911 case P4_TABLE: {
80912 zP4 = pOp->p4.pTab->zName;
80913 break;
80914 }
80915 default: {
80916 zP4 = pOp->p4.z;
80917 }
80918 }
80919 if( zP4 ) sqlite3_str_appendall(p: &x, z: zP4);
80920 if( (x.accError & SQLITE_NOMEM)!=0 ){
80921 sqlite3OomFault(db);
80922 }
80923 return sqlite3StrAccumFinish(p: &x);
80924}
80925#endif /* VDBE_DISPLAY_P4 */
80926
80927/*
80928** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
80929**
80930** The prepared statements need to know in advance the complete set of
80931** attached databases that will be use. A mask of these databases
80932** is maintained in p->btreeMask. The p->lockMask value is the subset of
80933** p->btreeMask of databases that will require a lock.
80934*/
80935SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
80936 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
80937 assert( i<(int)sizeof(p->btreeMask)*8 );
80938 DbMaskSet(p->btreeMask, i);
80939 if( i!=1 && sqlite3BtreeSharable(p: p->db->aDb[i].pBt) ){
80940 DbMaskSet(p->lockMask, i);
80941 }
80942}
80943
80944#if !defined(SQLITE_OMIT_SHARED_CACHE)
80945/*
80946** If SQLite is compiled to support shared-cache mode and to be threadsafe,
80947** this routine obtains the mutex associated with each BtShared structure
80948** that may be accessed by the VM passed as an argument. In doing so it also
80949** sets the BtShared.db member of each of the BtShared structures, ensuring
80950** that the correct busy-handler callback is invoked if required.
80951**
80952** If SQLite is not threadsafe but does support shared-cache mode, then
80953** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
80954** of all of BtShared structures accessible via the database handle
80955** associated with the VM.
80956**
80957** If SQLite is not threadsafe and does not support shared-cache mode, this
80958** function is a no-op.
80959**
80960** The p->btreeMask field is a bitmask of all btrees that the prepared
80961** statement p will ever use. Let N be the number of bits in p->btreeMask
80962** corresponding to btrees that use shared cache. Then the runtime of
80963** this routine is N*N. But as N is rarely more than 1, this should not
80964** be a problem.
80965*/
80966SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe *p){
80967 int i;
80968 sqlite3 *db;
80969 Db *aDb;
80970 int nDb;
80971 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
80972 db = p->db;
80973 aDb = db->aDb;
80974 nDb = db->nDb;
80975 for(i=0; i<nDb; i++){
80976 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
80977 sqlite3BtreeEnter(p: aDb[i].pBt);
80978 }
80979 }
80980}
80981#endif
80982
80983#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
80984/*
80985** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
80986*/
80987static SQLITE_NOINLINE void vdbeLeave(Vdbe *p){
80988 int i;
80989 sqlite3 *db;
80990 Db *aDb;
80991 int nDb;
80992 db = p->db;
80993 aDb = db->aDb;
80994 nDb = db->nDb;
80995 for(i=0; i<nDb; i++){
80996 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
80997 sqlite3BtreeLeave(p: aDb[i].pBt);
80998 }
80999 }
81000}
81001SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
81002 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
81003 vdbeLeave(p);
81004}
81005#endif
81006
81007#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
81008/*
81009** Print a single opcode. This routine is used for debugging only.
81010*/
81011SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE *pOut, int pc, VdbeOp *pOp){
81012 char *zP4;
81013 char *zCom;
81014 sqlite3 dummyDb;
81015 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
81016 if( pOut==0 ) pOut = stdout;
81017 sqlite3BeginBenignMalloc();
81018 dummyDb.mallocFailed = 1;
81019 zP4 = sqlite3VdbeDisplayP4(&dummyDb, pOp);
81020#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
81021 zCom = sqlite3VdbeDisplayComment(0, pOp, zP4);
81022#else
81023 zCom = 0;
81024#endif
81025 /* NB: The sqlite3OpcodeName() function is implemented by code created
81026 ** by the mkopcodeh.awk and mkopcodec.awk scripts which extract the
81027 ** information from the vdbe.c source text */
81028 fprintf(pOut, zFormat1, pc,
81029 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3,
81030 zP4 ? zP4 : "", pOp->p5,
81031 zCom ? zCom : ""
81032 );
81033 fflush(pOut);
81034 sqlite3_free(zP4);
81035 sqlite3_free(zCom);
81036 sqlite3EndBenignMalloc();
81037}
81038#endif
81039
81040/*
81041** Initialize an array of N Mem element.
81042*/
81043static void initMemArray(Mem *p, int N, sqlite3 *db, u16 flags){
81044 while( (N--)>0 ){
81045 p->db = db;
81046 p->flags = flags;
81047 p->szMalloc = 0;
81048#ifdef SQLITE_DEBUG
81049 p->pScopyFrom = 0;
81050#endif
81051 p++;
81052 }
81053}
81054
81055/*
81056** Release an array of N Mem elements
81057*/
81058static void releaseMemArray(Mem *p, int N){
81059 if( p && N ){
81060 Mem *pEnd = &p[N];
81061 sqlite3 *db = p->db;
81062 if( db->pnBytesFreed ){
81063 do{
81064 if( p->szMalloc ) sqlite3DbFree(db, p: p->zMalloc);
81065 }while( (++p)<pEnd );
81066 return;
81067 }
81068 do{
81069 assert( (&p[1])==pEnd || p[0].db==p[1].db );
81070 assert( sqlite3VdbeCheckMemInvariants(p) );
81071
81072 /* This block is really an inlined version of sqlite3VdbeMemRelease()
81073 ** that takes advantage of the fact that the memory cell value is
81074 ** being set to NULL after releasing any dynamic resources.
81075 **
81076 ** The justification for duplicating code is that according to
81077 ** callgrind, this causes a certain test case to hit the CPU 4.7
81078 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
81079 ** sqlite3MemRelease() were called from here. With -O2, this jumps
81080 ** to 6.6 percent. The test case is inserting 1000 rows into a table
81081 ** with no indexes using a single prepared INSERT statement, bind()
81082 ** and reset(). Inserts are grouped into a transaction.
81083 */
81084 testcase( p->flags & MEM_Agg );
81085 testcase( p->flags & MEM_Dyn );
81086 if( p->flags&(MEM_Agg|MEM_Dyn) ){
81087 testcase( (p->flags & MEM_Dyn)!=0 && p->xDel==sqlite3VdbeFrameMemDel );
81088 sqlite3VdbeMemRelease(p);
81089 }else if( p->szMalloc ){
81090 sqlite3DbFreeNN(db, p: p->zMalloc);
81091 p->szMalloc = 0;
81092 }
81093
81094 p->flags = MEM_Undefined;
81095 }while( (++p)<pEnd );
81096 }
81097}
81098
81099#ifdef SQLITE_DEBUG
81100/*
81101** Verify that pFrame is a valid VdbeFrame pointer. Return true if it is
81102** and false if something is wrong.
81103**
81104** This routine is intended for use inside of assert() statements only.
81105*/
81106SQLITE_PRIVATE int sqlite3VdbeFrameIsValid(VdbeFrame *pFrame){
81107 if( pFrame->iFrameMagic!=SQLITE_FRAME_MAGIC ) return 0;
81108 return 1;
81109}
81110#endif
81111
81112
81113/*
81114** This is a destructor on a Mem object (which is really an sqlite3_value)
81115** that deletes the Frame object that is attached to it as a blob.
81116**
81117** This routine does not delete the Frame right away. It merely adds the
81118** frame to a list of frames to be deleted when the Vdbe halts.
81119*/
81120SQLITE_PRIVATE void sqlite3VdbeFrameMemDel(void *pArg){
81121 VdbeFrame *pFrame = (VdbeFrame*)pArg;
81122 assert( sqlite3VdbeFrameIsValid(pFrame) );
81123 pFrame->pParent = pFrame->v->pDelFrame;
81124 pFrame->v->pDelFrame = pFrame;
81125}
81126
81127#if defined(SQLITE_ENABLE_BYTECODE_VTAB) || !defined(SQLITE_OMIT_EXPLAIN)
81128/*
81129** Locate the next opcode to be displayed in EXPLAIN or EXPLAIN
81130** QUERY PLAN output.
81131**
81132** Return SQLITE_ROW on success. Return SQLITE_DONE if there are no
81133** more opcodes to be displayed.
81134*/
81135SQLITE_PRIVATE int sqlite3VdbeNextOpcode(
81136 Vdbe *p, /* The statement being explained */
81137 Mem *pSub, /* Storage for keeping track of subprogram nesting */
81138 int eMode, /* 0: normal. 1: EQP. 2: TablesUsed */
81139 int *piPc, /* IN/OUT: Current rowid. Overwritten with next rowid */
81140 int *piAddr, /* OUT: Write index into (*paOp)[] here */
81141 Op **paOp /* OUT: Write the opcode array here */
81142){
81143 int nRow; /* Stop when row count reaches this */
81144 int nSub = 0; /* Number of sub-vdbes seen so far */
81145 SubProgram **apSub = 0; /* Array of sub-vdbes */
81146 int i; /* Next instruction address */
81147 int rc = SQLITE_OK; /* Result code */
81148 Op *aOp = 0; /* Opcode array */
81149 int iPc; /* Rowid. Copy of value in *piPc */
81150
81151 /* When the number of output rows reaches nRow, that means the
81152 ** listing has finished and sqlite3_step() should return SQLITE_DONE.
81153 ** nRow is the sum of the number of rows in the main program, plus
81154 ** the sum of the number of rows in all trigger subprograms encountered
81155 ** so far. The nRow value will increase as new trigger subprograms are
81156 ** encountered, but p->pc will eventually catch up to nRow.
81157 */
81158 nRow = p->nOp;
81159 if( pSub!=0 ){
81160 if( pSub->flags&MEM_Blob ){
81161 /* pSub is initiallly NULL. It is initialized to a BLOB by
81162 ** the P4_SUBPROGRAM processing logic below */
81163 nSub = pSub->n/sizeof(Vdbe*);
81164 apSub = (SubProgram **)pSub->z;
81165 }
81166 for(i=0; i<nSub; i++){
81167 nRow += apSub[i]->nOp;
81168 }
81169 }
81170 iPc = *piPc;
81171 while(1){ /* Loop exits via break */
81172 i = iPc++;
81173 if( i>=nRow ){
81174 p->rc = SQLITE_OK;
81175 rc = SQLITE_DONE;
81176 break;
81177 }
81178 if( i<p->nOp ){
81179 /* The rowid is small enough that we are still in the
81180 ** main program. */
81181 aOp = p->aOp;
81182 }else{
81183 /* We are currently listing subprograms. Figure out which one and
81184 ** pick up the appropriate opcode. */
81185 int j;
81186 i -= p->nOp;
81187 assert( apSub!=0 );
81188 assert( nSub>0 );
81189 for(j=0; i>=apSub[j]->nOp; j++){
81190 i -= apSub[j]->nOp;
81191 assert( i<apSub[j]->nOp || j+1<nSub );
81192 }
81193 aOp = apSub[j]->aOp;
81194 }
81195
81196 /* When an OP_Program opcode is encounter (the only opcode that has
81197 ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
81198 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
81199 ** has not already been seen.
81200 */
81201 if( pSub!=0 && aOp[i].p4type==P4_SUBPROGRAM ){
81202 int nByte = (nSub+1)*sizeof(SubProgram*);
81203 int j;
81204 for(j=0; j<nSub; j++){
81205 if( apSub[j]==aOp[i].p4.pProgram ) break;
81206 }
81207 if( j==nSub ){
81208 p->rc = sqlite3VdbeMemGrow(pMem: pSub, n: nByte, bPreserve: nSub!=0);
81209 if( p->rc!=SQLITE_OK ){
81210 rc = SQLITE_ERROR;
81211 break;
81212 }
81213 apSub = (SubProgram **)pSub->z;
81214 apSub[nSub++] = aOp[i].p4.pProgram;
81215 MemSetTypeFlag(pSub, MEM_Blob);
81216 pSub->n = nSub*sizeof(SubProgram*);
81217 nRow += aOp[i].p4.pProgram->nOp;
81218 }
81219 }
81220 if( eMode==0 ) break;
81221#ifdef SQLITE_ENABLE_BYTECODE_VTAB
81222 if( eMode==2 ){
81223 Op *pOp = aOp + i;
81224 if( pOp->opcode==OP_OpenRead ) break;
81225 if( pOp->opcode==OP_OpenWrite && (pOp->p5 & OPFLAG_P2ISREG)==0 ) break;
81226 if( pOp->opcode==OP_ReopenIdx ) break;
81227 }else
81228#endif
81229 {
81230 assert( eMode==1 );
81231 if( aOp[i].opcode==OP_Explain ) break;
81232 if( aOp[i].opcode==OP_Init && iPc>1 ) break;
81233 }
81234 }
81235 *piPc = iPc;
81236 *piAddr = i;
81237 *paOp = aOp;
81238 return rc;
81239}
81240#endif /* SQLITE_ENABLE_BYTECODE_VTAB || !SQLITE_OMIT_EXPLAIN */
81241
81242
81243/*
81244** Delete a VdbeFrame object and its contents. VdbeFrame objects are
81245** allocated by the OP_Program opcode in sqlite3VdbeExec().
81246*/
81247SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame *p){
81248 int i;
81249 Mem *aMem = VdbeFrameMem(p);
81250 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
81251 assert( sqlite3VdbeFrameIsValid(p) );
81252 for(i=0; i<p->nChildCsr; i++){
81253 sqlite3VdbeFreeCursor(p->v, apCsr[i]);
81254 }
81255 releaseMemArray(p: aMem, N: p->nChildMem);
81256 sqlite3VdbeDeleteAuxData(p->v->db, &p->pAuxData, -1, 0);
81257 sqlite3DbFree(db: p->v->db, p);
81258}
81259
81260#ifndef SQLITE_OMIT_EXPLAIN
81261/*
81262** Give a listing of the program in the virtual machine.
81263**
81264** The interface is the same as sqlite3VdbeExec(). But instead of
81265** running the code, it invokes the callback once for each instruction.
81266** This feature is used to implement "EXPLAIN".
81267**
81268** When p->explain==1, each instruction is listed. When
81269** p->explain==2, only OP_Explain instructions are listed and these
81270** are shown in a different format. p->explain==2 is used to implement
81271** EXPLAIN QUERY PLAN.
81272** 2018-04-24: In p->explain==2 mode, the OP_Init opcodes of triggers
81273** are also shown, so that the boundaries between the main program and
81274** each trigger are clear.
81275**
81276** When p->explain==1, first the main program is listed, then each of
81277** the trigger subprograms are listed one by one.
81278*/
81279SQLITE_PRIVATE int sqlite3VdbeList(
81280 Vdbe *p /* The VDBE */
81281){
81282 Mem *pSub = 0; /* Memory cell hold array of subprogs */
81283 sqlite3 *db = p->db; /* The database connection */
81284 int i; /* Loop counter */
81285 int rc = SQLITE_OK; /* Return code */
81286 Mem *pMem = &p->aMem[1]; /* First Mem of result set */
81287 int bListSubprogs = (p->explain==1 || (db->flags & SQLITE_TriggerEQP)!=0);
81288 Op *aOp; /* Array of opcodes */
81289 Op *pOp; /* Current opcode */
81290
81291 assert( p->explain );
81292 assert( p->iVdbeMagic==VDBE_MAGIC_RUN );
81293 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
81294
81295 /* Even though this opcode does not use dynamic strings for
81296 ** the result, result columns may become dynamic if the user calls
81297 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
81298 */
81299 releaseMemArray(p: pMem, N: 8);
81300 p->pResultSet = 0;
81301
81302 if( p->rc==SQLITE_NOMEM ){
81303 /* This happens if a malloc() inside a call to sqlite3_column_text() or
81304 ** sqlite3_column_text16() failed. */
81305 sqlite3OomFault(db);
81306 return SQLITE_ERROR;
81307 }
81308
81309 if( bListSubprogs ){
81310 /* The first 8 memory cells are used for the result set. So we will
81311 ** commandeer the 9th cell to use as storage for an array of pointers
81312 ** to trigger subprograms. The VDBE is guaranteed to have at least 9
81313 ** cells. */
81314 assert( p->nMem>9 );
81315 pSub = &p->aMem[9];
81316 }else{
81317 pSub = 0;
81318 }
81319
81320 /* Figure out which opcode is next to display */
81321 rc = sqlite3VdbeNextOpcode(p, pSub, eMode: p->explain==2, piPc: &p->pc, piAddr: &i, paOp: &aOp);
81322
81323 if( rc==SQLITE_OK ){
81324 pOp = aOp + i;
81325 if( AtomicLoad(&db->u1.isInterrupted) ){
81326 p->rc = SQLITE_INTERRUPT;
81327 rc = SQLITE_ERROR;
81328 sqlite3VdbeError(p, zFormat: sqlite3ErrStr(p->rc));
81329 }else{
81330 char *zP4 = sqlite3VdbeDisplayP4(db, pOp);
81331 if( p->explain==2 ){
81332 sqlite3VdbeMemSetInt64(pMem, val: pOp->p1);
81333 sqlite3VdbeMemSetInt64(pMem: pMem+1, val: pOp->p2);
81334 sqlite3VdbeMemSetInt64(pMem: pMem+2, val: pOp->p3);
81335 sqlite3VdbeMemSetStr(pMem: pMem+3, z: zP4, n: -1, SQLITE_UTF8, xDel: sqlite3_free);
81336 p->nResColumn = 4;
81337 }else{
81338 sqlite3VdbeMemSetInt64(pMem: pMem+0, val: i);
81339 sqlite3VdbeMemSetStr(pMem: pMem+1, z: (char*)sqlite3OpcodeName(i: pOp->opcode),
81340 n: -1, SQLITE_UTF8, SQLITE_STATIC);
81341 sqlite3VdbeMemSetInt64(pMem: pMem+2, val: pOp->p1);
81342 sqlite3VdbeMemSetInt64(pMem: pMem+3, val: pOp->p2);
81343 sqlite3VdbeMemSetInt64(pMem: pMem+4, val: pOp->p3);
81344 /* pMem+5 for p4 is done last */
81345 sqlite3VdbeMemSetInt64(pMem: pMem+6, val: pOp->p5);
81346#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
81347 {
81348 char *zCom = sqlite3VdbeDisplayComment(db, pOp, zP4);
81349 sqlite3VdbeMemSetStr(pMem+7, zCom, -1, SQLITE_UTF8, sqlite3_free);
81350 }
81351#else
81352 sqlite3VdbeMemSetNull(pMem: pMem+7);
81353#endif
81354 sqlite3VdbeMemSetStr(pMem: pMem+5, z: zP4, n: -1, SQLITE_UTF8, xDel: sqlite3_free);
81355 p->nResColumn = 8;
81356 }
81357 p->pResultSet = pMem;
81358 if( db->mallocFailed ){
81359 p->rc = SQLITE_NOMEM;
81360 rc = SQLITE_ERROR;
81361 }else{
81362 p->rc = SQLITE_OK;
81363 rc = SQLITE_ROW;
81364 }
81365 }
81366 }
81367 return rc;
81368}
81369#endif /* SQLITE_OMIT_EXPLAIN */
81370
81371#ifdef SQLITE_DEBUG
81372/*
81373** Print the SQL that was used to generate a VDBE program.
81374*/
81375SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe *p){
81376 const char *z = 0;
81377 if( p->zSql ){
81378 z = p->zSql;
81379 }else if( p->nOp>=1 ){
81380 const VdbeOp *pOp = &p->aOp[0];
81381 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
81382 z = pOp->p4.z;
81383 while( sqlite3Isspace(*z) ) z++;
81384 }
81385 }
81386 if( z ) printf("SQL: [%s]\n", z);
81387}
81388#endif
81389
81390#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
81391/*
81392** Print an IOTRACE message showing SQL content.
81393*/
81394SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe *p){
81395 int nOp = p->nOp;
81396 VdbeOp *pOp;
81397 if( sqlite3IoTrace==0 ) return;
81398 if( nOp<1 ) return;
81399 pOp = &p->aOp[0];
81400 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
81401 int i, j;
81402 char z[1000];
81403 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
81404 for(i=0; sqlite3Isspace(z[i]); i++){}
81405 for(j=0; z[i]; i++){
81406 if( sqlite3Isspace(z[i]) ){
81407 if( z[i-1]!=' ' ){
81408 z[j++] = ' ';
81409 }
81410 }else{
81411 z[j++] = z[i];
81412 }
81413 }
81414 z[j] = 0;
81415 sqlite3IoTrace("SQL %s\n", z);
81416 }
81417}
81418#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
81419
81420/* An instance of this object describes bulk memory available for use
81421** by subcomponents of a prepared statement. Space is allocated out
81422** of a ReusableSpace object by the allocSpace() routine below.
81423*/
81424struct ReusableSpace {
81425 u8 *pSpace; /* Available memory */
81426 sqlite3_int64 nFree; /* Bytes of available memory */
81427 sqlite3_int64 nNeeded; /* Total bytes that could not be allocated */
81428};
81429
81430/* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf
81431** from the ReusableSpace object. Return a pointer to the allocated
81432** memory on success. If insufficient memory is available in the
81433** ReusableSpace object, increase the ReusableSpace.nNeeded
81434** value by the amount needed and return NULL.
81435**
81436** If pBuf is not initially NULL, that means that the memory has already
81437** been allocated by a prior call to this routine, so just return a copy
81438** of pBuf and leave ReusableSpace unchanged.
81439**
81440** This allocator is employed to repurpose unused slots at the end of the
81441** opcode array of prepared state for other memory needs of the prepared
81442** statement.
81443*/
81444static void *allocSpace(
81445 struct ReusableSpace *p, /* Bulk memory available for allocation */
81446 void *pBuf, /* Pointer to a prior allocation */
81447 sqlite3_int64 nByte /* Bytes of memory needed */
81448){
81449 assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) );
81450 if( pBuf==0 ){
81451 nByte = ROUND8(nByte);
81452 if( nByte <= p->nFree ){
81453 p->nFree -= nByte;
81454 pBuf = &p->pSpace[p->nFree];
81455 }else{
81456 p->nNeeded += nByte;
81457 }
81458 }
81459 assert( EIGHT_BYTE_ALIGNMENT(pBuf) );
81460 return pBuf;
81461}
81462
81463/*
81464** Rewind the VDBE back to the beginning in preparation for
81465** running it.
81466*/
81467SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe *p){
81468#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
81469 int i;
81470#endif
81471 assert( p!=0 );
81472 assert( p->iVdbeMagic==VDBE_MAGIC_INIT || p->iVdbeMagic==VDBE_MAGIC_RESET );
81473
81474 /* There should be at least one opcode.
81475 */
81476 assert( p->nOp>0 );
81477
81478 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
81479 p->iVdbeMagic = VDBE_MAGIC_RUN;
81480
81481#ifdef SQLITE_DEBUG
81482 for(i=0; i<p->nMem; i++){
81483 assert( p->aMem[i].db==p->db );
81484 }
81485#endif
81486 p->pc = -1;
81487 p->rc = SQLITE_OK;
81488 p->errorAction = OE_Abort;
81489 p->nChange = 0;
81490 p->cacheCtr = 1;
81491 p->minWriteFileFormat = 255;
81492 p->iStatement = 0;
81493 p->nFkConstraint = 0;
81494#ifdef VDBE_PROFILE
81495 for(i=0; i<p->nOp; i++){
81496 p->aOp[i].cnt = 0;
81497 p->aOp[i].cycles = 0;
81498 }
81499#endif
81500}
81501
81502/*
81503** Prepare a virtual machine for execution for the first time after
81504** creating the virtual machine. This involves things such
81505** as allocating registers and initializing the program counter.
81506** After the VDBE has be prepped, it can be executed by one or more
81507** calls to sqlite3VdbeExec().
81508**
81509** This function may be called exactly once on each virtual machine.
81510** After this routine is called the VM has been "packaged" and is ready
81511** to run. After this routine is called, further calls to
81512** sqlite3VdbeAddOp() functions are prohibited. This routine disconnects
81513** the Vdbe from the Parse object that helped generate it so that the
81514** the Vdbe becomes an independent entity and the Parse object can be
81515** destroyed.
81516**
81517** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
81518** to its initial state after it has been run.
81519*/
81520SQLITE_PRIVATE void sqlite3VdbeMakeReady(
81521 Vdbe *p, /* The VDBE */
81522 Parse *pParse /* Parsing context */
81523){
81524 sqlite3 *db; /* The database connection */
81525 int nVar; /* Number of parameters */
81526 int nMem; /* Number of VM memory registers */
81527 int nCursor; /* Number of cursors required */
81528 int nArg; /* Number of arguments in subprograms */
81529 int n; /* Loop counter */
81530 struct ReusableSpace x; /* Reusable bulk memory */
81531
81532 assert( p!=0 );
81533 assert( p->nOp>0 );
81534 assert( pParse!=0 );
81535 assert( p->iVdbeMagic==VDBE_MAGIC_INIT );
81536 assert( pParse==p->pParse );
81537 p->pVList = pParse->pVList;
81538 pParse->pVList = 0;
81539 db = p->db;
81540 assert( db->mallocFailed==0 );
81541 nVar = pParse->nVar;
81542 nMem = pParse->nMem;
81543 nCursor = pParse->nTab;
81544 nArg = pParse->nMaxArg;
81545
81546 /* Each cursor uses a memory cell. The first cursor (cursor 0) can
81547 ** use aMem[0] which is not otherwise used by the VDBE program. Allocate
81548 ** space at the end of aMem[] for cursors 1 and greater.
81549 ** See also: allocateCursor().
81550 */
81551 nMem += nCursor;
81552 if( nCursor==0 && nMem>0 ) nMem++; /* Space for aMem[0] even if not used */
81553
81554 /* Figure out how much reusable memory is available at the end of the
81555 ** opcode array. This extra memory will be reallocated for other elements
81556 ** of the prepared statement.
81557 */
81558 n = ROUND8(sizeof(Op)*p->nOp); /* Bytes of opcode memory used */
81559 x.pSpace = &((u8*)p->aOp)[n]; /* Unused opcode memory */
81560 assert( EIGHT_BYTE_ALIGNMENT(x.pSpace) );
81561 x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n); /* Bytes of unused memory */
81562 assert( x.nFree>=0 );
81563 assert( EIGHT_BYTE_ALIGNMENT(&x.pSpace[x.nFree]) );
81564
81565 resolveP2Values(p, pMaxFuncArgs: &nArg);
81566 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
81567 if( pParse->explain ){
81568 static const char * const azColName[] = {
81569 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
81570 "id", "parent", "notused", "detail"
81571 };
81572 int iFirst, mx, i;
81573 if( nMem<10 ) nMem = 10;
81574 p->explain = pParse->explain;
81575 if( pParse->explain==2 ){
81576 sqlite3VdbeSetNumCols(p, 4);
81577 iFirst = 8;
81578 mx = 12;
81579 }else{
81580 sqlite3VdbeSetNumCols(p, 8);
81581 iFirst = 0;
81582 mx = 8;
81583 }
81584 for(i=iFirst; i<mx; i++){
81585 sqlite3VdbeSetColName(p, i-iFirst, COLNAME_NAME,
81586 azColName[i], SQLITE_STATIC);
81587 }
81588 }
81589 p->expired = 0;
81590
81591 /* Memory for registers, parameters, cursor, etc, is allocated in one or two
81592 ** passes. On the first pass, we try to reuse unused memory at the
81593 ** end of the opcode array. If we are unable to satisfy all memory
81594 ** requirements by reusing the opcode array tail, then the second
81595 ** pass will fill in the remainder using a fresh memory allocation.
81596 **
81597 ** This two-pass approach that reuses as much memory as possible from
81598 ** the leftover memory at the end of the opcode array. This can significantly
81599 ** reduce the amount of memory held by a prepared statement.
81600 */
81601 x.nNeeded = 0;
81602 p->aMem = allocSpace(p: &x, pBuf: 0, nByte: nMem*sizeof(Mem));
81603 p->aVar = allocSpace(p: &x, pBuf: 0, nByte: nVar*sizeof(Mem));
81604 p->apArg = allocSpace(p: &x, pBuf: 0, nByte: nArg*sizeof(Mem*));
81605 p->apCsr = allocSpace(p: &x, pBuf: 0, nByte: nCursor*sizeof(VdbeCursor*));
81606#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
81607 p->anExec = allocSpace(&x, 0, p->nOp*sizeof(i64));
81608#endif
81609 if( x.nNeeded ){
81610 x.pSpace = p->pFree = sqlite3DbMallocRawNN(db, n: x.nNeeded);
81611 x.nFree = x.nNeeded;
81612 if( !db->mallocFailed ){
81613 p->aMem = allocSpace(p: &x, pBuf: p->aMem, nByte: nMem*sizeof(Mem));
81614 p->aVar = allocSpace(p: &x, pBuf: p->aVar, nByte: nVar*sizeof(Mem));
81615 p->apArg = allocSpace(p: &x, pBuf: p->apArg, nByte: nArg*sizeof(Mem*));
81616 p->apCsr = allocSpace(p: &x, pBuf: p->apCsr, nByte: nCursor*sizeof(VdbeCursor*));
81617#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
81618 p->anExec = allocSpace(&x, p->anExec, p->nOp*sizeof(i64));
81619#endif
81620 }
81621 }
81622
81623 if( db->mallocFailed ){
81624 p->nVar = 0;
81625 p->nCursor = 0;
81626 p->nMem = 0;
81627 }else{
81628 p->nCursor = nCursor;
81629 p->nVar = (ynVar)nVar;
81630 initMemArray(p: p->aVar, N: nVar, db, MEM_Null);
81631 p->nMem = nMem;
81632 initMemArray(p: p->aMem, N: nMem, db, MEM_Undefined);
81633 memset(s: p->apCsr, c: 0, n: nCursor*sizeof(VdbeCursor*));
81634#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
81635 memset(p->anExec, 0, p->nOp*sizeof(i64));
81636#endif
81637 }
81638 sqlite3VdbeRewind(p);
81639}
81640
81641/*
81642** Close a VDBE cursor and release all the resources that cursor
81643** happens to hold.
81644*/
81645SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
81646 if( pCx==0 ){
81647 return;
81648 }
81649 assert( pCx->pBtx==0 || pCx->eCurType==CURTYPE_BTREE );
81650 assert( pCx->pBtx==0 || pCx->isEphemeral );
81651 switch( pCx->eCurType ){
81652 case CURTYPE_SORTER: {
81653 sqlite3VdbeSorterClose(p->db, pCx);
81654 break;
81655 }
81656 case CURTYPE_BTREE: {
81657 assert( pCx->uc.pCursor!=0 );
81658 sqlite3BtreeCloseCursor(pCur: pCx->uc.pCursor);
81659 break;
81660 }
81661#ifndef SQLITE_OMIT_VIRTUALTABLE
81662 case CURTYPE_VTAB: {
81663 sqlite3_vtab_cursor *pVCur = pCx->uc.pVCur;
81664 const sqlite3_module *pModule = pVCur->pVtab->pModule;
81665 assert( pVCur->pVtab->nRef>0 );
81666 pVCur->pVtab->nRef--;
81667 pModule->xClose(pVCur);
81668 break;
81669 }
81670#endif
81671 }
81672}
81673
81674/*
81675** Close all cursors in the current frame.
81676*/
81677static void closeCursorsInFrame(Vdbe *p){
81678 if( p->apCsr ){
81679 int i;
81680 for(i=0; i<p->nCursor; i++){
81681 VdbeCursor *pC = p->apCsr[i];
81682 if( pC ){
81683 sqlite3VdbeFreeCursor(p, pCx: pC);
81684 p->apCsr[i] = 0;
81685 }
81686 }
81687 }
81688}
81689
81690/*
81691** Copy the values stored in the VdbeFrame structure to its Vdbe. This
81692** is used, for example, when a trigger sub-program is halted to restore
81693** control to the main program.
81694*/
81695SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
81696 Vdbe *v = pFrame->v;
81697 closeCursorsInFrame(p: v);
81698#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
81699 v->anExec = pFrame->anExec;
81700#endif
81701 v->aOp = pFrame->aOp;
81702 v->nOp = pFrame->nOp;
81703 v->aMem = pFrame->aMem;
81704 v->nMem = pFrame->nMem;
81705 v->apCsr = pFrame->apCsr;
81706 v->nCursor = pFrame->nCursor;
81707 v->db->lastRowid = pFrame->lastRowid;
81708 v->nChange = pFrame->nChange;
81709 v->db->nChange = pFrame->nDbChange;
81710 sqlite3VdbeDeleteAuxData(v->db, &v->pAuxData, -1, 0);
81711 v->pAuxData = pFrame->pAuxData;
81712 pFrame->pAuxData = 0;
81713 return pFrame->pc;
81714}
81715
81716/*
81717** Close all cursors.
81718**
81719** Also release any dynamic memory held by the VM in the Vdbe.aMem memory
81720** cell array. This is necessary as the memory cell array may contain
81721** pointers to VdbeFrame objects, which may in turn contain pointers to
81722** open cursors.
81723*/
81724static void closeAllCursors(Vdbe *p){
81725 if( p->pFrame ){
81726 VdbeFrame *pFrame;
81727 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
81728 sqlite3VdbeFrameRestore(pFrame);
81729 p->pFrame = 0;
81730 p->nFrame = 0;
81731 }
81732 assert( p->nFrame==0 );
81733 closeCursorsInFrame(p);
81734 if( p->aMem ){
81735 releaseMemArray(p: p->aMem, N: p->nMem);
81736 }
81737 while( p->pDelFrame ){
81738 VdbeFrame *pDel = p->pDelFrame;
81739 p->pDelFrame = pDel->pParent;
81740 sqlite3VdbeFrameDelete(p: pDel);
81741 }
81742
81743 /* Delete any auxdata allocations made by the VM */
81744 if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p->db, &p->pAuxData, -1, 0);
81745 assert( p->pAuxData==0 );
81746}
81747
81748/*
81749** Set the number of result columns that will be returned by this SQL
81750** statement. This is now set at compile time, rather than during
81751** execution of the vdbe program so that sqlite3_column_count() can
81752** be called on an SQL statement before sqlite3_step().
81753*/
81754SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
81755 int n;
81756 sqlite3 *db = p->db;
81757
81758 if( p->nResColumn ){
81759 releaseMemArray(p: p->aColName, N: p->nResColumn*COLNAME_N);
81760 sqlite3DbFree(db, p: p->aColName);
81761 }
81762 n = nResColumn*COLNAME_N;
81763 p->nResColumn = (u16)nResColumn;
81764 p->aColName = (Mem*)sqlite3DbMallocRawNN(db, n: sizeof(Mem)*n );
81765 if( p->aColName==0 ) return;
81766 initMemArray(p: p->aColName, N: n, db, MEM_Null);
81767}
81768
81769/*
81770** Set the name of the idx'th column to be returned by the SQL statement.
81771** zName must be a pointer to a nul terminated string.
81772**
81773** This call must be made after a call to sqlite3VdbeSetNumCols().
81774**
81775** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
81776** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
81777** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
81778*/
81779SQLITE_PRIVATE int sqlite3VdbeSetColName(
81780 Vdbe *p, /* Vdbe being configured */
81781 int idx, /* Index of column zName applies to */
81782 int var, /* One of the COLNAME_* constants */
81783 const char *zName, /* Pointer to buffer containing name */
81784 void (*xDel)(void*) /* Memory management strategy for zName */
81785){
81786 int rc;
81787 Mem *pColName;
81788 assert( idx<p->nResColumn );
81789 assert( var<COLNAME_N );
81790 if( p->db->mallocFailed ){
81791 assert( !zName || xDel!=SQLITE_DYNAMIC );
81792 return SQLITE_NOMEM_BKPT;
81793 }
81794 assert( p->aColName!=0 );
81795 pColName = &(p->aColName[idx+var*p->nResColumn]);
81796 rc = sqlite3VdbeMemSetStr(pMem: pColName, z: zName, n: -1, SQLITE_UTF8, xDel);
81797 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
81798 return rc;
81799}
81800
81801/*
81802** A read or write transaction may or may not be active on database handle
81803** db. If a transaction is active, commit it. If there is a
81804** write-transaction spanning more than one database file, this routine
81805** takes care of the super-journal trickery.
81806*/
81807static int vdbeCommit(sqlite3 *db, Vdbe *p){
81808 int i;
81809 int nTrans = 0; /* Number of databases with an active write-transaction
81810 ** that are candidates for a two-phase commit using a
81811 ** super-journal */
81812 int rc = SQLITE_OK;
81813 int needXcommit = 0;
81814
81815#ifdef SQLITE_OMIT_VIRTUALTABLE
81816 /* With this option, sqlite3VtabSync() is defined to be simply
81817 ** SQLITE_OK so p is not used.
81818 */
81819 UNUSED_PARAMETER(p);
81820#endif
81821
81822 /* Before doing anything else, call the xSync() callback for any
81823 ** virtual module tables written in this transaction. This has to
81824 ** be done before determining whether a super-journal file is
81825 ** required, as an xSync() callback may add an attached database
81826 ** to the transaction.
81827 */
81828 rc = sqlite3VtabSync(db, p);
81829
81830 /* This loop determines (a) if the commit hook should be invoked and
81831 ** (b) how many database files have open write transactions, not
81832 ** including the temp database. (b) is important because if more than
81833 ** one database file has an open write transaction, a super-journal
81834 ** file is required for an atomic commit.
81835 */
81836 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
81837 Btree *pBt = db->aDb[i].pBt;
81838 if( sqlite3BtreeTxnState(p: pBt)==SQLITE_TXN_WRITE ){
81839 /* Whether or not a database might need a super-journal depends upon
81840 ** its journal mode (among other things). This matrix determines which
81841 ** journal modes use a super-journal and which do not */
81842 static const u8 aMJNeeded[] = {
81843 /* DELETE */ 1,
81844 /* PERSIST */ 1,
81845 /* OFF */ 0,
81846 /* TRUNCATE */ 1,
81847 /* MEMORY */ 0,
81848 /* WAL */ 0
81849 };
81850 Pager *pPager; /* Pager associated with pBt */
81851 needXcommit = 1;
81852 sqlite3BtreeEnter(p: pBt);
81853 pPager = sqlite3BtreePager(p: pBt);
81854 if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF
81855 && aMJNeeded[sqlite3PagerGetJournalMode(pPager)]
81856 && sqlite3PagerIsMemdb(pPager)==0
81857 ){
81858 assert( i!=1 );
81859 nTrans++;
81860 }
81861 rc = sqlite3PagerExclusiveLock(pPager);
81862 sqlite3BtreeLeave(p: pBt);
81863 }
81864 }
81865 if( rc!=SQLITE_OK ){
81866 return rc;
81867 }
81868
81869 /* If there are any write-transactions at all, invoke the commit hook */
81870 if( needXcommit && db->xCommitCallback ){
81871 rc = db->xCommitCallback(db->pCommitArg);
81872 if( rc ){
81873 return SQLITE_CONSTRAINT_COMMITHOOK;
81874 }
81875 }
81876
81877 /* The simple case - no more than one database file (not counting the
81878 ** TEMP database) has a transaction active. There is no need for the
81879 ** super-journal.
81880 **
81881 ** If the return value of sqlite3BtreeGetFilename() is a zero length
81882 ** string, it means the main database is :memory: or a temp file. In
81883 ** that case we do not support atomic multi-file commits, so use the
81884 ** simple case then too.
81885 */
81886 if( 0==sqlite3Strlen30(z: sqlite3BtreeGetFilename(p: db->aDb[0].pBt))
81887 || nTrans<=1
81888 ){
81889 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
81890 Btree *pBt = db->aDb[i].pBt;
81891 if( pBt ){
81892 rc = sqlite3BtreeCommitPhaseOne(p: pBt, zSuperJrnl: 0);
81893 }
81894 }
81895
81896 /* Do the commit only if all databases successfully complete phase 1.
81897 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
81898 ** IO error while deleting or truncating a journal file. It is unlikely,
81899 ** but could happen. In this case abandon processing and return the error.
81900 */
81901 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
81902 Btree *pBt = db->aDb[i].pBt;
81903 if( pBt ){
81904 rc = sqlite3BtreeCommitPhaseTwo(p: pBt, bCleanup: 0);
81905 }
81906 }
81907 if( rc==SQLITE_OK ){
81908 sqlite3VtabCommit(db);
81909 }
81910 }
81911
81912 /* The complex case - There is a multi-file write-transaction active.
81913 ** This requires a super-journal file to ensure the transaction is
81914 ** committed atomically.
81915 */
81916#ifndef SQLITE_OMIT_DISKIO
81917 else{
81918 sqlite3_vfs *pVfs = db->pVfs;
81919 char *zSuper = 0; /* File-name for the super-journal */
81920 char const *zMainFile = sqlite3BtreeGetFilename(p: db->aDb[0].pBt);
81921 sqlite3_file *pSuperJrnl = 0;
81922 i64 offset = 0;
81923 int res;
81924 int retryCount = 0;
81925 int nMainFile;
81926
81927 /* Select a super-journal file name */
81928 nMainFile = sqlite3Strlen30(z: zMainFile);
81929 zSuper = sqlite3MPrintf(db, zFormat: "%.4c%s%.16c", 0,zMainFile,0);
81930 if( zSuper==0 ) return SQLITE_NOMEM_BKPT;
81931 zSuper += 4;
81932 do {
81933 u32 iRandom;
81934 if( retryCount ){
81935 if( retryCount>100 ){
81936 sqlite3_log(SQLITE_FULL, zFormat: "MJ delete: %s", zSuper);
81937 sqlite3OsDelete(pVfs, zPath: zSuper, dirSync: 0);
81938 break;
81939 }else if( retryCount==1 ){
81940 sqlite3_log(SQLITE_FULL, zFormat: "MJ collide: %s", zSuper);
81941 }
81942 }
81943 retryCount++;
81944 sqlite3_randomness(N: sizeof(iRandom), pBuf: &iRandom);
81945 sqlite3_snprintf(n: 13, zBuf: &zSuper[nMainFile], zFormat: "-mj%06X9%02X",
81946 (iRandom>>8)&0xffffff, iRandom&0xff);
81947 /* The antipenultimate character of the super-journal name must
81948 ** be "9" to avoid name collisions when using 8+3 filenames. */
81949 assert( zSuper[sqlite3Strlen30(zSuper)-3]=='9' );
81950 sqlite3FileSuffix3(zMainFile, zSuper);
81951 rc = sqlite3OsAccess(pVfs, zPath: zSuper, SQLITE_ACCESS_EXISTS, pResOut: &res);
81952 }while( rc==SQLITE_OK && res );
81953 if( rc==SQLITE_OK ){
81954 /* Open the super-journal. */
81955 rc = sqlite3OsOpenMalloc(pVfs, zFile: zSuper, ppFile: &pSuperJrnl,
81956 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
81957 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_SUPER_JOURNAL, pOutFlags: 0
81958 );
81959 }
81960 if( rc!=SQLITE_OK ){
81961 sqlite3DbFree(db, p: zSuper-4);
81962 return rc;
81963 }
81964
81965 /* Write the name of each database file in the transaction into the new
81966 ** super-journal file. If an error occurs at this point close
81967 ** and delete the super-journal file. All the individual journal files
81968 ** still have 'null' as the super-journal pointer, so they will roll
81969 ** back independently if a failure occurs.
81970 */
81971 for(i=0; i<db->nDb; i++){
81972 Btree *pBt = db->aDb[i].pBt;
81973 if( sqlite3BtreeTxnState(p: pBt)==SQLITE_TXN_WRITE ){
81974 char const *zFile = sqlite3BtreeGetJournalname(p: pBt);
81975 if( zFile==0 ){
81976 continue; /* Ignore TEMP and :memory: databases */
81977 }
81978 assert( zFile[0]!=0 );
81979 rc = sqlite3OsWrite(id: pSuperJrnl, pBuf: zFile, amt: sqlite3Strlen30(z: zFile)+1,offset);
81980 offset += sqlite3Strlen30(z: zFile)+1;
81981 if( rc!=SQLITE_OK ){
81982 sqlite3OsCloseFree(pFile: pSuperJrnl);
81983 sqlite3OsDelete(pVfs, zPath: zSuper, dirSync: 0);
81984 sqlite3DbFree(db, p: zSuper-4);
81985 return rc;
81986 }
81987 }
81988 }
81989
81990 /* Sync the super-journal file. If the IOCAP_SEQUENTIAL device
81991 ** flag is set this is not required.
81992 */
81993 if( 0==(sqlite3OsDeviceCharacteristics(id: pSuperJrnl)&SQLITE_IOCAP_SEQUENTIAL)
81994 && SQLITE_OK!=(rc = sqlite3OsSync(id: pSuperJrnl, SQLITE_SYNC_NORMAL))
81995 ){
81996 sqlite3OsCloseFree(pFile: pSuperJrnl);
81997 sqlite3OsDelete(pVfs, zPath: zSuper, dirSync: 0);
81998 sqlite3DbFree(db, p: zSuper-4);
81999 return rc;
82000 }
82001
82002 /* Sync all the db files involved in the transaction. The same call
82003 ** sets the super-journal pointer in each individual journal. If
82004 ** an error occurs here, do not delete the super-journal file.
82005 **
82006 ** If the error occurs during the first call to
82007 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
82008 ** super-journal file will be orphaned. But we cannot delete it,
82009 ** in case the super-journal file name was written into the journal
82010 ** file before the failure occurred.
82011 */
82012 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
82013 Btree *pBt = db->aDb[i].pBt;
82014 if( pBt ){
82015 rc = sqlite3BtreeCommitPhaseOne(p: pBt, zSuperJrnl: zSuper);
82016 }
82017 }
82018 sqlite3OsCloseFree(pFile: pSuperJrnl);
82019 assert( rc!=SQLITE_BUSY );
82020 if( rc!=SQLITE_OK ){
82021 sqlite3DbFree(db, p: zSuper-4);
82022 return rc;
82023 }
82024
82025 /* Delete the super-journal file. This commits the transaction. After
82026 ** doing this the directory is synced again before any individual
82027 ** transaction files are deleted.
82028 */
82029 rc = sqlite3OsDelete(pVfs, zPath: zSuper, dirSync: 1);
82030 sqlite3DbFree(db, p: zSuper-4);
82031 zSuper = 0;
82032 if( rc ){
82033 return rc;
82034 }
82035
82036 /* All files and directories have already been synced, so the following
82037 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
82038 ** deleting or truncating journals. If something goes wrong while
82039 ** this is happening we don't really care. The integrity of the
82040 ** transaction is already guaranteed, but some stray 'cold' journals
82041 ** may be lying around. Returning an error code won't help matters.
82042 */
82043 disable_simulated_io_errors();
82044 sqlite3BeginBenignMalloc();
82045 for(i=0; i<db->nDb; i++){
82046 Btree *pBt = db->aDb[i].pBt;
82047 if( pBt ){
82048 sqlite3BtreeCommitPhaseTwo(p: pBt, bCleanup: 1);
82049 }
82050 }
82051 sqlite3EndBenignMalloc();
82052 enable_simulated_io_errors();
82053
82054 sqlite3VtabCommit(db);
82055 }
82056#endif
82057
82058 return rc;
82059}
82060
82061/*
82062** This routine checks that the sqlite3.nVdbeActive count variable
82063** matches the number of vdbe's in the list sqlite3.pVdbe that are
82064** currently active. An assertion fails if the two counts do not match.
82065** This is an internal self-check only - it is not an essential processing
82066** step.
82067**
82068** This is a no-op if NDEBUG is defined.
82069*/
82070#ifndef NDEBUG
82071static void checkActiveVdbeCnt(sqlite3 *db){
82072 Vdbe *p;
82073 int cnt = 0;
82074 int nWrite = 0;
82075 int nRead = 0;
82076 p = db->pVdbe;
82077 while( p ){
82078 if( sqlite3_stmt_busy((sqlite3_stmt*)p) ){
82079 cnt++;
82080 if( p->readOnly==0 ) nWrite++;
82081 if( p->bIsReader ) nRead++;
82082 }
82083 p = p->pNext;
82084 }
82085 assert( cnt==db->nVdbeActive );
82086 assert( nWrite==db->nVdbeWrite );
82087 assert( nRead==db->nVdbeRead );
82088}
82089#else
82090#define checkActiveVdbeCnt(x)
82091#endif
82092
82093/*
82094** If the Vdbe passed as the first argument opened a statement-transaction,
82095** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
82096** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
82097** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the
82098** statement transaction is committed.
82099**
82100** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
82101** Otherwise SQLITE_OK.
82102*/
82103static SQLITE_NOINLINE int vdbeCloseStatement(Vdbe *p, int eOp){
82104 sqlite3 *const db = p->db;
82105 int rc = SQLITE_OK;
82106 int i;
82107 const int iSavepoint = p->iStatement-1;
82108
82109 assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
82110 assert( db->nStatement>0 );
82111 assert( p->iStatement==(db->nStatement+db->nSavepoint) );
82112
82113 for(i=0; i<db->nDb; i++){
82114 int rc2 = SQLITE_OK;
82115 Btree *pBt = db->aDb[i].pBt;
82116 if( pBt ){
82117 if( eOp==SAVEPOINT_ROLLBACK ){
82118 rc2 = sqlite3BtreeSavepoint(p: pBt, SAVEPOINT_ROLLBACK, iSavepoint);
82119 }
82120 if( rc2==SQLITE_OK ){
82121 rc2 = sqlite3BtreeSavepoint(p: pBt, SAVEPOINT_RELEASE, iSavepoint);
82122 }
82123 if( rc==SQLITE_OK ){
82124 rc = rc2;
82125 }
82126 }
82127 }
82128 db->nStatement--;
82129 p->iStatement = 0;
82130
82131 if( rc==SQLITE_OK ){
82132 if( eOp==SAVEPOINT_ROLLBACK ){
82133 rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
82134 }
82135 if( rc==SQLITE_OK ){
82136 rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
82137 }
82138 }
82139
82140 /* If the statement transaction is being rolled back, also restore the
82141 ** database handles deferred constraint counter to the value it had when
82142 ** the statement transaction was opened. */
82143 if( eOp==SAVEPOINT_ROLLBACK ){
82144 db->nDeferredCons = p->nStmtDefCons;
82145 db->nDeferredImmCons = p->nStmtDefImmCons;
82146 }
82147 return rc;
82148}
82149SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
82150 if( p->db->nStatement && p->iStatement ){
82151 return vdbeCloseStatement(p, eOp);
82152 }
82153 return SQLITE_OK;
82154}
82155
82156
82157/*
82158** This function is called when a transaction opened by the database
82159** handle associated with the VM passed as an argument is about to be
82160** committed. If there are outstanding deferred foreign key constraint
82161** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
82162**
82163** If there are outstanding FK violations and this function returns
82164** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY
82165** and write an error message to it. Then return SQLITE_ERROR.
82166*/
82167#ifndef SQLITE_OMIT_FOREIGN_KEY
82168SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
82169 sqlite3 *db = p->db;
82170 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
82171 || (!deferred && p->nFkConstraint>0)
82172 ){
82173 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
82174 p->errorAction = OE_Abort;
82175 sqlite3VdbeError(p, zFormat: "FOREIGN KEY constraint failed");
82176 return SQLITE_ERROR;
82177 }
82178 return SQLITE_OK;
82179}
82180#endif
82181
82182/*
82183** This routine is called the when a VDBE tries to halt. If the VDBE
82184** has made changes and is in autocommit mode, then commit those
82185** changes. If a rollback is needed, then do the rollback.
82186**
82187** This routine is the only way to move the sqlite3eOpenState of a VM from
82188** SQLITE_STATE_RUN to SQLITE_STATE_HALT. It is harmless to
82189** call this on a VM that is in the SQLITE_STATE_HALT state.
82190**
82191** Return an error code. If the commit could not complete because of
82192** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
82193** means the close did not happen and needs to be repeated.
82194*/
82195SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe *p){
82196 int rc; /* Used to store transient return codes */
82197 sqlite3 *db = p->db;
82198
82199 /* This function contains the logic that determines if a statement or
82200 ** transaction will be committed or rolled back as a result of the
82201 ** execution of this virtual machine.
82202 **
82203 ** If any of the following errors occur:
82204 **
82205 ** SQLITE_NOMEM
82206 ** SQLITE_IOERR
82207 ** SQLITE_FULL
82208 ** SQLITE_INTERRUPT
82209 **
82210 ** Then the internal cache might have been left in an inconsistent
82211 ** state. We need to rollback the statement transaction, if there is
82212 ** one, or the complete transaction if there is no statement transaction.
82213 */
82214
82215 if( p->iVdbeMagic!=VDBE_MAGIC_RUN ){
82216 return SQLITE_OK;
82217 }
82218 if( db->mallocFailed ){
82219 p->rc = SQLITE_NOMEM_BKPT;
82220 }
82221 closeAllCursors(p);
82222 checkActiveVdbeCnt(db);
82223
82224 /* No commit or rollback needed if the program never started or if the
82225 ** SQL statement does not read or write a database file. */
82226 if( p->pc>=0 && p->bIsReader ){
82227 int mrc; /* Primary error code from p->rc */
82228 int eStatementOp = 0;
82229 int isSpecialError; /* Set to true if a 'special' error */
82230
82231 /* Lock all btrees used by the statement */
82232 sqlite3VdbeEnter(p);
82233
82234 /* Check for one of the special errors */
82235 if( p->rc ){
82236 mrc = p->rc & 0xff;
82237 isSpecialError = mrc==SQLITE_NOMEM
82238 || mrc==SQLITE_IOERR
82239 || mrc==SQLITE_INTERRUPT
82240 || mrc==SQLITE_FULL;
82241 }else{
82242 mrc = isSpecialError = 0;
82243 }
82244 if( isSpecialError ){
82245 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
82246 ** no rollback is necessary. Otherwise, at least a savepoint
82247 ** transaction must be rolled back to restore the database to a
82248 ** consistent state.
82249 **
82250 ** Even if the statement is read-only, it is important to perform
82251 ** a statement or transaction rollback operation. If the error
82252 ** occurred while writing to the journal, sub-journal or database
82253 ** file as part of an effort to free up cache space (see function
82254 ** pagerStress() in pager.c), the rollback is required to restore
82255 ** the pager to a consistent state.
82256 */
82257 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
82258 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
82259 eStatementOp = SAVEPOINT_ROLLBACK;
82260 }else{
82261 /* We are forced to roll back the active transaction. Before doing
82262 ** so, abort any other statements this handle currently has active.
82263 */
82264 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
82265 sqlite3CloseSavepoints(db);
82266 db->autoCommit = 1;
82267 p->nChange = 0;
82268 }
82269 }
82270 }
82271
82272 /* Check for immediate foreign key violations. */
82273 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
82274 sqlite3VdbeCheckFk(p, deferred: 0);
82275 }
82276
82277 /* If the auto-commit flag is set and this is the only active writer
82278 ** VM, then we do either a commit or rollback of the current transaction.
82279 **
82280 ** Note: This block also runs if one of the special errors handled
82281 ** above has occurred.
82282 */
82283 if( !sqlite3VtabInSync(db)
82284 && db->autoCommit
82285 && db->nVdbeWrite==(p->readOnly==0)
82286 ){
82287 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
82288 rc = sqlite3VdbeCheckFk(p, deferred: 1);
82289 if( rc!=SQLITE_OK ){
82290 if( NEVER(p->readOnly) ){
82291 sqlite3VdbeLeave(p);
82292 return SQLITE_ERROR;
82293 }
82294 rc = SQLITE_CONSTRAINT_FOREIGNKEY;
82295 }else if( db->flags & SQLITE_CorruptRdOnly ){
82296 rc = SQLITE_CORRUPT;
82297 db->flags &= ~SQLITE_CorruptRdOnly;
82298 }else{
82299 /* The auto-commit flag is true, the vdbe program was successful
82300 ** or hit an 'OR FAIL' constraint and there are no deferred foreign
82301 ** key constraints to hold up the transaction. This means a commit
82302 ** is required. */
82303 rc = vdbeCommit(db, p);
82304 }
82305 if( rc==SQLITE_BUSY && p->readOnly ){
82306 sqlite3VdbeLeave(p);
82307 return SQLITE_BUSY;
82308 }else if( rc!=SQLITE_OK ){
82309 p->rc = rc;
82310 sqlite3RollbackAll(db, SQLITE_OK);
82311 p->nChange = 0;
82312 }else{
82313 db->nDeferredCons = 0;
82314 db->nDeferredImmCons = 0;
82315 db->flags &= ~(u64)SQLITE_DeferFKs;
82316 sqlite3CommitInternalChanges(db);
82317 }
82318 }else{
82319 sqlite3RollbackAll(db, SQLITE_OK);
82320 p->nChange = 0;
82321 }
82322 db->nStatement = 0;
82323 }else if( eStatementOp==0 ){
82324 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
82325 eStatementOp = SAVEPOINT_RELEASE;
82326 }else if( p->errorAction==OE_Abort ){
82327 eStatementOp = SAVEPOINT_ROLLBACK;
82328 }else{
82329 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
82330 sqlite3CloseSavepoints(db);
82331 db->autoCommit = 1;
82332 p->nChange = 0;
82333 }
82334 }
82335
82336 /* If eStatementOp is non-zero, then a statement transaction needs to
82337 ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
82338 ** do so. If this operation returns an error, and the current statement
82339 ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
82340 ** current statement error code.
82341 */
82342 if( eStatementOp ){
82343 rc = sqlite3VdbeCloseStatement(p, eOp: eStatementOp);
82344 if( rc ){
82345 if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
82346 p->rc = rc;
82347 sqlite3DbFree(db, p: p->zErrMsg);
82348 p->zErrMsg = 0;
82349 }
82350 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
82351 sqlite3CloseSavepoints(db);
82352 db->autoCommit = 1;
82353 p->nChange = 0;
82354 }
82355 }
82356
82357 /* If this was an INSERT, UPDATE or DELETE and no statement transaction
82358 ** has been rolled back, update the database connection change-counter.
82359 */
82360 if( p->changeCntOn ){
82361 if( eStatementOp!=SAVEPOINT_ROLLBACK ){
82362 sqlite3VdbeSetChanges(db, p->nChange);
82363 }else{
82364 sqlite3VdbeSetChanges(db, 0);
82365 }
82366 p->nChange = 0;
82367 }
82368
82369 /* Release the locks */
82370 sqlite3VdbeLeave(p);
82371 }
82372
82373 /* We have successfully halted and closed the VM. Record this fact. */
82374 if( p->pc>=0 ){
82375 db->nVdbeActive--;
82376 if( !p->readOnly ) db->nVdbeWrite--;
82377 if( p->bIsReader ) db->nVdbeRead--;
82378 assert( db->nVdbeActive>=db->nVdbeRead );
82379 assert( db->nVdbeRead>=db->nVdbeWrite );
82380 assert( db->nVdbeWrite>=0 );
82381 }
82382 p->iVdbeMagic = VDBE_MAGIC_HALT;
82383 checkActiveVdbeCnt(db);
82384 if( db->mallocFailed ){
82385 p->rc = SQLITE_NOMEM_BKPT;
82386 }
82387
82388 /* If the auto-commit flag is set to true, then any locks that were held
82389 ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
82390 ** to invoke any required unlock-notify callbacks.
82391 */
82392 if( db->autoCommit ){
82393 sqlite3ConnectionUnlocked(db);
82394 }
82395
82396 assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
82397 return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
82398}
82399
82400
82401/*
82402** Each VDBE holds the result of the most recent sqlite3_step() call
82403** in p->rc. This routine sets that result back to SQLITE_OK.
82404*/
82405SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe *p){
82406 p->rc = SQLITE_OK;
82407}
82408
82409/*
82410** Copy the error code and error message belonging to the VDBE passed
82411** as the first argument to its database handle (so that they will be
82412** returned by calls to sqlite3_errcode() and sqlite3_errmsg()).
82413**
82414** This function does not clear the VDBE error code or message, just
82415** copies them to the database handle.
82416*/
82417SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p){
82418 sqlite3 *db = p->db;
82419 int rc = p->rc;
82420 if( p->zErrMsg ){
82421 db->bBenignMalloc++;
82422 sqlite3BeginBenignMalloc();
82423 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
82424 sqlite3ValueSetStr(v: db->pErr, n: -1, z: p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
82425 sqlite3EndBenignMalloc();
82426 db->bBenignMalloc--;
82427 }else if( db->pErr ){
82428 sqlite3ValueSetNull(p: db->pErr);
82429 }
82430 db->errCode = rc;
82431 return rc;
82432}
82433
82434#ifdef SQLITE_ENABLE_SQLLOG
82435/*
82436** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run,
82437** invoke it.
82438*/
82439static void vdbeInvokeSqllog(Vdbe *v){
82440 if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
82441 char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
82442 assert( v->db->init.busy==0 );
82443 if( zExpanded ){
82444 sqlite3GlobalConfig.xSqllog(
82445 sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
82446 );
82447 sqlite3DbFree(v->db, zExpanded);
82448 }
82449 }
82450}
82451#else
82452# define vdbeInvokeSqllog(x)
82453#endif
82454
82455/*
82456** Clean up a VDBE after execution but do not delete the VDBE just yet.
82457** Write any error messages into *pzErrMsg. Return the result code.
82458**
82459** After this routine is run, the VDBE should be ready to be executed
82460** again.
82461**
82462** To look at it another way, this routine resets the state of the
82463** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
82464** VDBE_MAGIC_INIT.
82465*/
82466SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){
82467#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
82468 int i;
82469#endif
82470
82471 sqlite3 *db;
82472 db = p->db;
82473
82474 /* If the VM did not run to completion or if it encountered an
82475 ** error, then it might not have been halted properly. So halt
82476 ** it now.
82477 */
82478 sqlite3VdbeHalt(p);
82479
82480 /* If the VDBE has been run even partially, then transfer the error code
82481 ** and error message from the VDBE into the main database structure. But
82482 ** if the VDBE has just been set to run but has not actually executed any
82483 ** instructions yet, leave the main database error information unchanged.
82484 */
82485 if( p->pc>=0 ){
82486 vdbeInvokeSqllog(p);
82487 if( db->pErr || p->zErrMsg ){
82488 sqlite3VdbeTransferError(p);
82489 }else{
82490 db->errCode = p->rc;
82491 }
82492 if( p->runOnlyOnce ) p->expired = 1;
82493 }else if( p->rc && p->expired ){
82494 /* The expired flag was set on the VDBE before the first call
82495 ** to sqlite3_step(). For consistency (since sqlite3_step() was
82496 ** called), set the database error in this case as well.
82497 */
82498 sqlite3ErrorWithMsg(db, err_code: p->rc, zFormat: p->zErrMsg ? "%s" : 0, p->zErrMsg);
82499 }
82500
82501 /* Reset register contents and reclaim error message memory.
82502 */
82503#ifdef SQLITE_DEBUG
82504 /* Execute assert() statements to ensure that the Vdbe.apCsr[] and
82505 ** Vdbe.aMem[] arrays have already been cleaned up. */
82506 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
82507 if( p->aMem ){
82508 for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
82509 }
82510#endif
82511 if( p->zErrMsg ){
82512 sqlite3DbFree(db, p: p->zErrMsg);
82513 p->zErrMsg = 0;
82514 }
82515 p->pResultSet = 0;
82516#ifdef SQLITE_DEBUG
82517 p->nWrite = 0;
82518#endif
82519
82520 /* Save profiling information from this VDBE run.
82521 */
82522#ifdef VDBE_PROFILE
82523 {
82524 FILE *out = fopen("vdbe_profile.out", "a");
82525 if( out ){
82526 fprintf(out, "---- ");
82527 for(i=0; i<p->nOp; i++){
82528 fprintf(out, "%02x", p->aOp[i].opcode);
82529 }
82530 fprintf(out, "\n");
82531 if( p->zSql ){
82532 char c, pc = 0;
82533 fprintf(out, "-- ");
82534 for(i=0; (c = p->zSql[i])!=0; i++){
82535 if( pc=='\n' ) fprintf(out, "-- ");
82536 putc(c, out);
82537 pc = c;
82538 }
82539 if( pc!='\n' ) fprintf(out, "\n");
82540 }
82541 for(i=0; i<p->nOp; i++){
82542 char zHdr[100];
82543 sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ",
82544 p->aOp[i].cnt,
82545 p->aOp[i].cycles,
82546 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
82547 );
82548 fprintf(out, "%s", zHdr);
82549 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
82550 }
82551 fclose(out);
82552 }
82553 }
82554#endif
82555 p->iVdbeMagic = VDBE_MAGIC_RESET;
82556 return p->rc & db->errMask;
82557}
82558
82559/*
82560** Clean up and delete a VDBE after execution. Return an integer which is
82561** the result code. Write any error message text into *pzErrMsg.
82562*/
82563SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe *p){
82564 int rc = SQLITE_OK;
82565 if( p->iVdbeMagic==VDBE_MAGIC_RUN || p->iVdbeMagic==VDBE_MAGIC_HALT ){
82566 rc = sqlite3VdbeReset(p);
82567 assert( (rc & p->db->errMask)==rc );
82568 }
82569 sqlite3VdbeDelete(p);
82570 return rc;
82571}
82572
82573/*
82574** If parameter iOp is less than zero, then invoke the destructor for
82575** all auxiliary data pointers currently cached by the VM passed as
82576** the first argument.
82577**
82578** Or, if iOp is greater than or equal to zero, then the destructor is
82579** only invoked for those auxiliary data pointers created by the user
82580** function invoked by the OP_Function opcode at instruction iOp of
82581** VM pVdbe, and only then if:
82582**
82583** * the associated function parameter is the 32nd or later (counting
82584** from left to right), or
82585**
82586** * the corresponding bit in argument mask is clear (where the first
82587** function parameter corresponds to bit 0 etc.).
82588*/
82589SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(sqlite3 *db, AuxData **pp, int iOp, int mask){
82590 while( *pp ){
82591 AuxData *pAux = *pp;
82592 if( (iOp<0)
82593 || (pAux->iAuxOp==iOp
82594 && pAux->iAuxArg>=0
82595 && (pAux->iAuxArg>31 || !(mask & MASKBIT32(pAux->iAuxArg))))
82596 ){
82597 testcase( pAux->iAuxArg==31 );
82598 if( pAux->xDeleteAux ){
82599 pAux->xDeleteAux(pAux->pAux);
82600 }
82601 *pp = pAux->pNextAux;
82602 sqlite3DbFree(db, p: pAux);
82603 }else{
82604 pp= &pAux->pNextAux;
82605 }
82606 }
82607}
82608
82609/*
82610** Free all memory associated with the Vdbe passed as the second argument,
82611** except for object itself, which is preserved.
82612**
82613** The difference between this function and sqlite3VdbeDelete() is that
82614** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
82615** the database connection and frees the object itself.
82616*/
82617SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
82618 SubProgram *pSub, *pNext;
82619 assert( p->db==0 || p->db==db );
82620 releaseMemArray(p: p->aColName, N: p->nResColumn*COLNAME_N);
82621 for(pSub=p->pProgram; pSub; pSub=pNext){
82622 pNext = pSub->pNext;
82623 vdbeFreeOpArray(db, aOp: pSub->aOp, nOp: pSub->nOp);
82624 sqlite3DbFree(db, p: pSub);
82625 }
82626 if( p->iVdbeMagic!=VDBE_MAGIC_INIT ){
82627 releaseMemArray(p: p->aVar, N: p->nVar);
82628 sqlite3DbFree(db, p: p->pVList);
82629 sqlite3DbFree(db, p: p->pFree);
82630 }
82631 vdbeFreeOpArray(db, aOp: p->aOp, nOp: p->nOp);
82632 sqlite3DbFree(db, p: p->aColName);
82633 sqlite3DbFree(db, p: p->zSql);
82634#ifdef SQLITE_ENABLE_NORMALIZE
82635 sqlite3DbFree(db, p->zNormSql);
82636 {
82637 DblquoteStr *pThis, *pNext;
82638 for(pThis=p->pDblStr; pThis; pThis=pNext){
82639 pNext = pThis->pNextStr;
82640 sqlite3DbFree(db, pThis);
82641 }
82642 }
82643#endif
82644#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
82645 {
82646 int i;
82647 for(i=0; i<p->nScan; i++){
82648 sqlite3DbFree(db, p->aScan[i].zName);
82649 }
82650 sqlite3DbFree(db, p->aScan);
82651 }
82652#endif
82653}
82654
82655/*
82656** Delete an entire VDBE.
82657*/
82658SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){
82659 sqlite3 *db;
82660
82661 assert( p!=0 );
82662 db = p->db;
82663 assert( sqlite3_mutex_held(db->mutex) );
82664 sqlite3VdbeClearObject(db, p);
82665 if( p->pPrev ){
82666 p->pPrev->pNext = p->pNext;
82667 }else{
82668 assert( db->pVdbe==p );
82669 db->pVdbe = p->pNext;
82670 }
82671 if( p->pNext ){
82672 p->pNext->pPrev = p->pPrev;
82673 }
82674 p->iVdbeMagic = VDBE_MAGIC_DEAD;
82675 p->db = 0;
82676 sqlite3DbFreeNN(db, p);
82677}
82678
82679/*
82680** The cursor "p" has a pending seek operation that has not yet been
82681** carried out. Seek the cursor now. If an error occurs, return
82682** the appropriate error code.
82683*/
82684SQLITE_PRIVATE int SQLITE_NOINLINE sqlite3VdbeFinishMoveto(VdbeCursor *p){
82685 int res, rc;
82686#ifdef SQLITE_TEST
82687 extern int sqlite3_search_count;
82688#endif
82689 assert( p->deferredMoveto );
82690 assert( p->isTable );
82691 assert( p->eCurType==CURTYPE_BTREE );
82692 rc = sqlite3BtreeTableMoveto(pCur: p->uc.pCursor, intKey: p->movetoTarget, biasRight: 0, pRes: &res);
82693 if( rc ) return rc;
82694 if( res!=0 ) return SQLITE_CORRUPT_BKPT;
82695#ifdef SQLITE_TEST
82696 sqlite3_search_count++;
82697#endif
82698 p->deferredMoveto = 0;
82699 p->cacheStatus = CACHE_STALE;
82700 return SQLITE_OK;
82701}
82702
82703/*
82704** Something has moved cursor "p" out of place. Maybe the row it was
82705** pointed to was deleted out from under it. Or maybe the btree was
82706** rebalanced. Whatever the cause, try to restore "p" to the place it
82707** is supposed to be pointing. If the row was deleted out from under the
82708** cursor, set the cursor to point to a NULL row.
82709*/
82710static int SQLITE_NOINLINE handleMovedCursor(VdbeCursor *p){
82711 int isDifferentRow, rc;
82712 assert( p->eCurType==CURTYPE_BTREE );
82713 assert( p->uc.pCursor!=0 );
82714 assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) );
82715 rc = sqlite3BtreeCursorRestore(pCur: p->uc.pCursor, pDifferentRow: &isDifferentRow);
82716 p->cacheStatus = CACHE_STALE;
82717 if( isDifferentRow ) p->nullRow = 1;
82718 return rc;
82719}
82720
82721/*
82722** Check to ensure that the cursor is valid. Restore the cursor
82723** if need be. Return any I/O error from the restore operation.
82724*/
82725SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor *p){
82726 assert( p->eCurType==CURTYPE_BTREE );
82727 if( sqlite3BtreeCursorHasMoved(pCur: p->uc.pCursor) ){
82728 return handleMovedCursor(p);
82729 }
82730 return SQLITE_OK;
82731}
82732
82733/*
82734** Make sure the cursor p is ready to read or write the row to which it
82735** was last positioned. Return an error code if an OOM fault or I/O error
82736** prevents us from positioning the cursor to its correct position.
82737**
82738** If a MoveTo operation is pending on the given cursor, then do that
82739** MoveTo now. If no move is pending, check to see if the row has been
82740** deleted out from under the cursor and if it has, mark the row as
82741** a NULL row.
82742**
82743** If the cursor is already pointing to the correct row and that row has
82744** not been deleted out from under the cursor, then this routine is a no-op.
82745*/
82746SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor **pp, u32 *piCol){
82747 VdbeCursor *p = *pp;
82748 assert( p->eCurType==CURTYPE_BTREE || p->eCurType==CURTYPE_PSEUDO );
82749 if( p->deferredMoveto ){
82750 u32 iMap;
82751 assert( !p->isEphemeral );
82752 if( p->aAltMap && (iMap = p->aAltMap[1+*piCol])>0 && !p->nullRow ){
82753 *pp = p->pAltCursor;
82754 *piCol = iMap - 1;
82755 return SQLITE_OK;
82756 }
82757 return sqlite3VdbeFinishMoveto(p);
82758 }
82759 if( sqlite3BtreeCursorHasMoved(pCur: p->uc.pCursor) ){
82760 return handleMovedCursor(p);
82761 }
82762 return SQLITE_OK;
82763}
82764
82765/*
82766** The following functions:
82767**
82768** sqlite3VdbeSerialType()
82769** sqlite3VdbeSerialTypeLen()
82770** sqlite3VdbeSerialLen()
82771** sqlite3VdbeSerialPut()
82772** sqlite3VdbeSerialGet()
82773**
82774** encapsulate the code that serializes values for storage in SQLite
82775** data and index records. Each serialized value consists of a
82776** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
82777** integer, stored as a varint.
82778**
82779** In an SQLite index record, the serial type is stored directly before
82780** the blob of data that it corresponds to. In a table record, all serial
82781** types are stored at the start of the record, and the blobs of data at
82782** the end. Hence these functions allow the caller to handle the
82783** serial-type and data blob separately.
82784**
82785** The following table describes the various storage classes for data:
82786**
82787** serial type bytes of data type
82788** -------------- --------------- ---------------
82789** 0 0 NULL
82790** 1 1 signed integer
82791** 2 2 signed integer
82792** 3 3 signed integer
82793** 4 4 signed integer
82794** 5 6 signed integer
82795** 6 8 signed integer
82796** 7 8 IEEE float
82797** 8 0 Integer constant 0
82798** 9 0 Integer constant 1
82799** 10,11 reserved for expansion
82800** N>=12 and even (N-12)/2 BLOB
82801** N>=13 and odd (N-13)/2 text
82802**
82803** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
82804** of SQLite will not understand those serial types.
82805*/
82806
82807#if 0 /* Inlined into the OP_MakeRecord opcode */
82808/*
82809** Return the serial-type for the value stored in pMem.
82810**
82811** This routine might convert a large MEM_IntReal value into MEM_Real.
82812**
82813** 2019-07-11: The primary user of this subroutine was the OP_MakeRecord
82814** opcode in the byte-code engine. But by moving this routine in-line, we
82815** can omit some redundant tests and make that opcode a lot faster. So
82816** this routine is now only used by the STAT3 logic and STAT3 support has
82817** ended. The code is kept here for historical reference only.
82818*/
82819SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem *pMem, int file_format, u32 *pLen){
82820 int flags = pMem->flags;
82821 u32 n;
82822
82823 assert( pLen!=0 );
82824 if( flags&MEM_Null ){
82825 *pLen = 0;
82826 return 0;
82827 }
82828 if( flags&(MEM_Int|MEM_IntReal) ){
82829 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
82830# define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
82831 i64 i = pMem->u.i;
82832 u64 u;
82833 testcase( flags & MEM_Int );
82834 testcase( flags & MEM_IntReal );
82835 if( i<0 ){
82836 u = ~i;
82837 }else{
82838 u = i;
82839 }
82840 if( u<=127 ){
82841 if( (i&1)==i && file_format>=4 ){
82842 *pLen = 0;
82843 return 8+(u32)u;
82844 }else{
82845 *pLen = 1;
82846 return 1;
82847 }
82848 }
82849 if( u<=32767 ){ *pLen = 2; return 2; }
82850 if( u<=8388607 ){ *pLen = 3; return 3; }
82851 if( u<=2147483647 ){ *pLen = 4; return 4; }
82852 if( u<=MAX_6BYTE ){ *pLen = 6; return 5; }
82853 *pLen = 8;
82854 if( flags&MEM_IntReal ){
82855 /* If the value is IntReal and is going to take up 8 bytes to store
82856 ** as an integer, then we might as well make it an 8-byte floating
82857 ** point value */
82858 pMem->u.r = (double)pMem->u.i;
82859 pMem->flags &= ~MEM_IntReal;
82860 pMem->flags |= MEM_Real;
82861 return 7;
82862 }
82863 return 6;
82864 }
82865 if( flags&MEM_Real ){
82866 *pLen = 8;
82867 return 7;
82868 }
82869 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
82870 assert( pMem->n>=0 );
82871 n = (u32)pMem->n;
82872 if( flags & MEM_Zero ){
82873 n += pMem->u.nZero;
82874 }
82875 *pLen = n;
82876 return ((n*2) + 12 + ((flags&MEM_Str)!=0));
82877}
82878#endif /* inlined into OP_MakeRecord */
82879
82880/*
82881** The sizes for serial types less than 128
82882*/
82883static const u8 sqlite3SmallTypeSizes[] = {
82884 /* 0 1 2 3 4 5 6 7 8 9 */
82885/* 0 */ 0, 1, 2, 3, 4, 6, 8, 8, 0, 0,
82886/* 10 */ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3,
82887/* 20 */ 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
82888/* 30 */ 9, 9, 10, 10, 11, 11, 12, 12, 13, 13,
82889/* 40 */ 14, 14, 15, 15, 16, 16, 17, 17, 18, 18,
82890/* 50 */ 19, 19, 20, 20, 21, 21, 22, 22, 23, 23,
82891/* 60 */ 24, 24, 25, 25, 26, 26, 27, 27, 28, 28,
82892/* 70 */ 29, 29, 30, 30, 31, 31, 32, 32, 33, 33,
82893/* 80 */ 34, 34, 35, 35, 36, 36, 37, 37, 38, 38,
82894/* 90 */ 39, 39, 40, 40, 41, 41, 42, 42, 43, 43,
82895/* 100 */ 44, 44, 45, 45, 46, 46, 47, 47, 48, 48,
82896/* 110 */ 49, 49, 50, 50, 51, 51, 52, 52, 53, 53,
82897/* 120 */ 54, 54, 55, 55, 56, 56, 57, 57
82898};
82899
82900/*
82901** Return the length of the data corresponding to the supplied serial-type.
82902*/
82903SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
82904 if( serial_type>=128 ){
82905 return (serial_type-12)/2;
82906 }else{
82907 assert( serial_type<12
82908 || sqlite3SmallTypeSizes[serial_type]==(serial_type - 12)/2 );
82909 return sqlite3SmallTypeSizes[serial_type];
82910 }
82911}
82912SQLITE_PRIVATE u8 sqlite3VdbeOneByteSerialTypeLen(u8 serial_type){
82913 assert( serial_type<128 );
82914 return sqlite3SmallTypeSizes[serial_type];
82915}
82916
82917/*
82918** If we are on an architecture with mixed-endian floating
82919** points (ex: ARM7) then swap the lower 4 bytes with the
82920** upper 4 bytes. Return the result.
82921**
82922** For most architectures, this is a no-op.
82923**
82924** (later): It is reported to me that the mixed-endian problem
82925** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
82926** that early versions of GCC stored the two words of a 64-bit
82927** float in the wrong order. And that error has been propagated
82928** ever since. The blame is not necessarily with GCC, though.
82929** GCC might have just copying the problem from a prior compiler.
82930** I am also told that newer versions of GCC that follow a different
82931** ABI get the byte order right.
82932**
82933** Developers using SQLite on an ARM7 should compile and run their
82934** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
82935** enabled, some asserts below will ensure that the byte order of
82936** floating point values is correct.
82937**
82938** (2007-08-30) Frank van Vugt has studied this problem closely
82939** and has send his findings to the SQLite developers. Frank
82940** writes that some Linux kernels offer floating point hardware
82941** emulation that uses only 32-bit mantissas instead of a full
82942** 48-bits as required by the IEEE standard. (This is the
82943** CONFIG_FPE_FASTFPE option.) On such systems, floating point
82944** byte swapping becomes very complicated. To avoid problems,
82945** the necessary byte swapping is carried out using a 64-bit integer
82946** rather than a 64-bit float. Frank assures us that the code here
82947** works for him. We, the developers, have no way to independently
82948** verify this, but Frank seems to know what he is talking about
82949** so we trust him.
82950*/
82951#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
82952static u64 floatSwap(u64 in){
82953 union {
82954 u64 r;
82955 u32 i[2];
82956 } u;
82957 u32 t;
82958
82959 u.r = in;
82960 t = u.i[0];
82961 u.i[0] = u.i[1];
82962 u.i[1] = t;
82963 return u.r;
82964}
82965# define swapMixedEndianFloat(X) X = floatSwap(X)
82966#else
82967# define swapMixedEndianFloat(X)
82968#endif
82969
82970/*
82971** Write the serialized data blob for the value stored in pMem into
82972** buf. It is assumed that the caller has allocated sufficient space.
82973** Return the number of bytes written.
82974**
82975** nBuf is the amount of space left in buf[]. The caller is responsible
82976** for allocating enough space to buf[] to hold the entire field, exclusive
82977** of the pMem->u.nZero bytes for a MEM_Zero value.
82978**
82979** Return the number of bytes actually written into buf[]. The number
82980** of bytes in the zero-filled tail is included in the return value only
82981** if those bytes were zeroed in buf[].
82982*/
82983SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(u8 *buf, Mem *pMem, u32 serial_type){
82984 u32 len;
82985
82986 /* Integer and Real */
82987 if( serial_type<=7 && serial_type>0 ){
82988 u64 v;
82989 u32 i;
82990 if( serial_type==7 ){
82991 assert( sizeof(v)==sizeof(pMem->u.r) );
82992 memcpy(dest: &v, src: &pMem->u.r, n: sizeof(v));
82993 swapMixedEndianFloat(v);
82994 }else{
82995 v = pMem->u.i;
82996 }
82997 len = i = sqlite3SmallTypeSizes[serial_type];
82998 assert( i>0 );
82999 do{
83000 buf[--i] = (u8)(v&0xFF);
83001 v >>= 8;
83002 }while( i );
83003 return len;
83004 }
83005
83006 /* String or blob */
83007 if( serial_type>=12 ){
83008 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
83009 == (int)sqlite3VdbeSerialTypeLen(serial_type) );
83010 len = pMem->n;
83011 if( len>0 ) memcpy(dest: buf, src: pMem->z, n: len);
83012 return len;
83013 }
83014
83015 /* NULL or constants 0 or 1 */
83016 return 0;
83017}
83018
83019/* Input "x" is a sequence of unsigned characters that represent a
83020** big-endian integer. Return the equivalent native integer
83021*/
83022#define ONE_BYTE_INT(x) ((i8)(x)[0])
83023#define TWO_BYTE_INT(x) (256*(i8)((x)[0])|(x)[1])
83024#define THREE_BYTE_INT(x) (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2])
83025#define FOUR_BYTE_UINT(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
83026#define FOUR_BYTE_INT(x) (16777216*(i8)((x)[0])|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
83027
83028/*
83029** Deserialize the data blob pointed to by buf as serial type serial_type
83030** and store the result in pMem. Return the number of bytes read.
83031**
83032** This function is implemented as two separate routines for performance.
83033** The few cases that require local variables are broken out into a separate
83034** routine so that in most cases the overhead of moving the stack pointer
83035** is avoided.
83036*/
83037static u32 serialGet(
83038 const unsigned char *buf, /* Buffer to deserialize from */
83039 u32 serial_type, /* Serial type to deserialize */
83040 Mem *pMem /* Memory cell to write value into */
83041){
83042 u64 x = FOUR_BYTE_UINT(buf);
83043 u32 y = FOUR_BYTE_UINT(buf+4);
83044 x = (x<<32) + y;
83045 if( serial_type==6 ){
83046 /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit
83047 ** twos-complement integer. */
83048 pMem->u.i = *(i64*)&x;
83049 pMem->flags = MEM_Int;
83050 testcase( pMem->u.i<0 );
83051 }else{
83052 /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit
83053 ** floating point number. */
83054#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
83055 /* Verify that integers and floating point values use the same
83056 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
83057 ** defined that 64-bit floating point values really are mixed
83058 ** endian.
83059 */
83060 static const u64 t1 = ((u64)0x3ff00000)<<32;
83061 static const double r1 = 1.0;
83062 u64 t2 = t1;
83063 swapMixedEndianFloat(t2);
83064 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
83065#endif
83066 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
83067 swapMixedEndianFloat(x);
83068 memcpy(dest: &pMem->u.r, src: &x, n: sizeof(x));
83069 pMem->flags = IsNaN(x) ? MEM_Null : MEM_Real;
83070 }
83071 return 8;
83072}
83073SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(
83074 const unsigned char *buf, /* Buffer to deserialize from */
83075 u32 serial_type, /* Serial type to deserialize */
83076 Mem *pMem /* Memory cell to write value into */
83077){
83078 switch( serial_type ){
83079 case 10: { /* Internal use only: NULL with virtual table
83080 ** UPDATE no-change flag set */
83081 pMem->flags = MEM_Null|MEM_Zero;
83082 pMem->n = 0;
83083 pMem->u.nZero = 0;
83084 break;
83085 }
83086 case 11: /* Reserved for future use */
83087 case 0: { /* Null */
83088 /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
83089 pMem->flags = MEM_Null;
83090 break;
83091 }
83092 case 1: {
83093 /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement
83094 ** integer. */
83095 pMem->u.i = ONE_BYTE_INT(buf);
83096 pMem->flags = MEM_Int;
83097 testcase( pMem->u.i<0 );
83098 return 1;
83099 }
83100 case 2: { /* 2-byte signed integer */
83101 /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit
83102 ** twos-complement integer. */
83103 pMem->u.i = TWO_BYTE_INT(buf);
83104 pMem->flags = MEM_Int;
83105 testcase( pMem->u.i<0 );
83106 return 2;
83107 }
83108 case 3: { /* 3-byte signed integer */
83109 /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit
83110 ** twos-complement integer. */
83111 pMem->u.i = THREE_BYTE_INT(buf);
83112 pMem->flags = MEM_Int;
83113 testcase( pMem->u.i<0 );
83114 return 3;
83115 }
83116 case 4: { /* 4-byte signed integer */
83117 /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
83118 ** twos-complement integer. */
83119 pMem->u.i = FOUR_BYTE_INT(buf);
83120#ifdef __HP_cc
83121 /* Work around a sign-extension bug in the HP compiler for HP/UX */
83122 if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL;
83123#endif
83124 pMem->flags = MEM_Int;
83125 testcase( pMem->u.i<0 );
83126 return 4;
83127 }
83128 case 5: { /* 6-byte signed integer */
83129 /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit
83130 ** twos-complement integer. */
83131 pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
83132 pMem->flags = MEM_Int;
83133 testcase( pMem->u.i<0 );
83134 return 6;
83135 }
83136 case 6: /* 8-byte signed integer */
83137 case 7: { /* IEEE floating point */
83138 /* These use local variables, so do them in a separate routine
83139 ** to avoid having to move the frame pointer in the common case */
83140 return serialGet(buf,serial_type,pMem);
83141 }
83142 case 8: /* Integer 0 */
83143 case 9: { /* Integer 1 */
83144 /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */
83145 /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */
83146 pMem->u.i = serial_type-8;
83147 pMem->flags = MEM_Int;
83148 return 0;
83149 }
83150 default: {
83151 /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in
83152 ** length.
83153 ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and
83154 ** (N-13)/2 bytes in length. */
83155 static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem };
83156 pMem->z = (char *)buf;
83157 pMem->n = (serial_type-12)/2;
83158 pMem->flags = aFlag[serial_type&1];
83159 return pMem->n;
83160 }
83161 }
83162 return 0;
83163}
83164/*
83165** This routine is used to allocate sufficient space for an UnpackedRecord
83166** structure large enough to be used with sqlite3VdbeRecordUnpack() if
83167** the first argument is a pointer to KeyInfo structure pKeyInfo.
83168**
83169** The space is either allocated using sqlite3DbMallocRaw() or from within
83170** the unaligned buffer passed via the second and third arguments (presumably
83171** stack space). If the former, then *ppFree is set to a pointer that should
83172** be eventually freed by the caller using sqlite3DbFree(). Or, if the
83173** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
83174** before returning.
83175**
83176** If an OOM error occurs, NULL is returned.
83177*/
83178SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
83179 KeyInfo *pKeyInfo /* Description of the record */
83180){
83181 UnpackedRecord *p; /* Unpacked record to return */
83182 int nByte; /* Number of bytes required for *p */
83183 nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nKeyField+1);
83184 p = (UnpackedRecord *)sqlite3DbMallocRaw(db: pKeyInfo->db, n: nByte);
83185 if( !p ) return 0;
83186 p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
83187 assert( pKeyInfo->aSortFlags!=0 );
83188 p->pKeyInfo = pKeyInfo;
83189 p->nField = pKeyInfo->nKeyField + 1;
83190 return p;
83191}
83192
83193/*
83194** Given the nKey-byte encoding of a record in pKey[], populate the
83195** UnpackedRecord structure indicated by the fourth argument with the
83196** contents of the decoded record.
83197*/
83198SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(
83199 KeyInfo *pKeyInfo, /* Information about the record format */
83200 int nKey, /* Size of the binary record */
83201 const void *pKey, /* The binary record */
83202 UnpackedRecord *p /* Populate this structure before returning. */
83203){
83204 const unsigned char *aKey = (const unsigned char *)pKey;
83205 u32 d;
83206 u32 idx; /* Offset in aKey[] to read from */
83207 u16 u; /* Unsigned loop counter */
83208 u32 szHdr;
83209 Mem *pMem = p->aMem;
83210
83211 p->default_rc = 0;
83212 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
83213 idx = getVarint32(aKey, szHdr);
83214 d = szHdr;
83215 u = 0;
83216 while( idx<szHdr && d<=(u32)nKey ){
83217 u32 serial_type;
83218
83219 idx += getVarint32(&aKey[idx], serial_type);
83220 pMem->enc = pKeyInfo->enc;
83221 pMem->db = pKeyInfo->db;
83222 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
83223 pMem->szMalloc = 0;
83224 pMem->z = 0;
83225 d += sqlite3VdbeSerialGet(buf: &aKey[d], serial_type, pMem);
83226 pMem++;
83227 if( (++u)>=p->nField ) break;
83228 }
83229 if( d>(u32)nKey && u ){
83230 assert( CORRUPT_DB );
83231 /* In a corrupt record entry, the last pMem might have been set up using
83232 ** uninitialized memory. Overwrite its value with NULL, to prevent
83233 ** warnings from MSAN. */
83234 sqlite3VdbeMemSetNull(pMem: pMem-1);
83235 }
83236 assert( u<=pKeyInfo->nKeyField + 1 );
83237 p->nField = u;
83238}
83239
83240#ifdef SQLITE_DEBUG
83241/*
83242** This function compares two index or table record keys in the same way
83243** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(),
83244** this function deserializes and compares values using the
83245** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used
83246** in assert() statements to ensure that the optimized code in
83247** sqlite3VdbeRecordCompare() returns results with these two primitives.
83248**
83249** Return true if the result of comparison is equivalent to desiredResult.
83250** Return false if there is a disagreement.
83251*/
83252static int vdbeRecordCompareDebug(
83253 int nKey1, const void *pKey1, /* Left key */
83254 const UnpackedRecord *pPKey2, /* Right key */
83255 int desiredResult /* Correct answer */
83256){
83257 u32 d1; /* Offset into aKey[] of next data element */
83258 u32 idx1; /* Offset into aKey[] of next header element */
83259 u32 szHdr1; /* Number of bytes in header */
83260 int i = 0;
83261 int rc = 0;
83262 const unsigned char *aKey1 = (const unsigned char *)pKey1;
83263 KeyInfo *pKeyInfo;
83264 Mem mem1;
83265
83266 pKeyInfo = pPKey2->pKeyInfo;
83267 if( pKeyInfo->db==0 ) return 1;
83268 mem1.enc = pKeyInfo->enc;
83269 mem1.db = pKeyInfo->db;
83270 /* mem1.flags = 0; // Will be initialized by sqlite3VdbeSerialGet() */
83271 VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
83272
83273 /* Compilers may complain that mem1.u.i is potentially uninitialized.
83274 ** We could initialize it, as shown here, to silence those complaints.
83275 ** But in fact, mem1.u.i will never actually be used uninitialized, and doing
83276 ** the unnecessary initialization has a measurable negative performance
83277 ** impact, since this routine is a very high runner. And so, we choose
83278 ** to ignore the compiler warnings and leave this variable uninitialized.
83279 */
83280 /* mem1.u.i = 0; // not needed, here to silence compiler warning */
83281
83282 idx1 = getVarint32(aKey1, szHdr1);
83283 if( szHdr1>98307 ) return SQLITE_CORRUPT;
83284 d1 = szHdr1;
83285 assert( pKeyInfo->nAllField>=pPKey2->nField || CORRUPT_DB );
83286 assert( pKeyInfo->aSortFlags!=0 );
83287 assert( pKeyInfo->nKeyField>0 );
83288 assert( idx1<=szHdr1 || CORRUPT_DB );
83289 do{
83290 u32 serial_type1;
83291
83292 /* Read the serial types for the next element in each key. */
83293 idx1 += getVarint32( aKey1+idx1, serial_type1 );
83294
83295 /* Verify that there is enough key space remaining to avoid
83296 ** a buffer overread. The "d1+serial_type1+2" subexpression will
83297 ** always be greater than or equal to the amount of required key space.
83298 ** Use that approximation to avoid the more expensive call to
83299 ** sqlite3VdbeSerialTypeLen() in the common case.
83300 */
83301 if( d1+(u64)serial_type1+2>(u64)nKey1
83302 && d1+(u64)sqlite3VdbeSerialTypeLen(serial_type1)>(u64)nKey1
83303 ){
83304 break;
83305 }
83306
83307 /* Extract the values to be compared.
83308 */
83309 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
83310
83311 /* Do the comparison
83312 */
83313 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
83314 pKeyInfo->nAllField>i ? pKeyInfo->aColl[i] : 0);
83315 if( rc!=0 ){
83316 assert( mem1.szMalloc==0 ); /* See comment below */
83317 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
83318 && ((mem1.flags & MEM_Null) || (pPKey2->aMem[i].flags & MEM_Null))
83319 ){
83320 rc = -rc;
83321 }
83322 if( pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC ){
83323 rc = -rc; /* Invert the result for DESC sort order. */
83324 }
83325 goto debugCompareEnd;
83326 }
83327 i++;
83328 }while( idx1<szHdr1 && i<pPKey2->nField );
83329
83330 /* No memory allocation is ever used on mem1. Prove this using
83331 ** the following assert(). If the assert() fails, it indicates a
83332 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
83333 */
83334 assert( mem1.szMalloc==0 );
83335
83336 /* rc==0 here means that one of the keys ran out of fields and
83337 ** all the fields up to that point were equal. Return the default_rc
83338 ** value. */
83339 rc = pPKey2->default_rc;
83340
83341debugCompareEnd:
83342 if( desiredResult==0 && rc==0 ) return 1;
83343 if( desiredResult<0 && rc<0 ) return 1;
83344 if( desiredResult>0 && rc>0 ) return 1;
83345 if( CORRUPT_DB ) return 1;
83346 if( pKeyInfo->db->mallocFailed ) return 1;
83347 return 0;
83348}
83349#endif
83350
83351#ifdef SQLITE_DEBUG
83352/*
83353** Count the number of fields (a.k.a. columns) in the record given by
83354** pKey,nKey. The verify that this count is less than or equal to the
83355** limit given by pKeyInfo->nAllField.
83356**
83357** If this constraint is not satisfied, it means that the high-speed
83358** vdbeRecordCompareInt() and vdbeRecordCompareString() routines will
83359** not work correctly. If this assert() ever fires, it probably means
83360** that the KeyInfo.nKeyField or KeyInfo.nAllField values were computed
83361** incorrectly.
83362*/
83363static void vdbeAssertFieldCountWithinLimits(
83364 int nKey, const void *pKey, /* The record to verify */
83365 const KeyInfo *pKeyInfo /* Compare size with this KeyInfo */
83366){
83367 int nField = 0;
83368 u32 szHdr;
83369 u32 idx;
83370 u32 notUsed;
83371 const unsigned char *aKey = (const unsigned char*)pKey;
83372
83373 if( CORRUPT_DB ) return;
83374 idx = getVarint32(aKey, szHdr);
83375 assert( nKey>=0 );
83376 assert( szHdr<=(u32)nKey );
83377 while( idx<szHdr ){
83378 idx += getVarint32(aKey+idx, notUsed);
83379 nField++;
83380 }
83381 assert( nField <= pKeyInfo->nAllField );
83382}
83383#else
83384# define vdbeAssertFieldCountWithinLimits(A,B,C)
83385#endif
83386
83387/*
83388** Both *pMem1 and *pMem2 contain string values. Compare the two values
83389** using the collation sequence pColl. As usual, return a negative , zero
83390** or positive value if *pMem1 is less than, equal to or greater than
83391** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
83392*/
83393static int vdbeCompareMemString(
83394 const Mem *pMem1,
83395 const Mem *pMem2,
83396 const CollSeq *pColl,
83397 u8 *prcErr /* If an OOM occurs, set to SQLITE_NOMEM */
83398){
83399 if( pMem1->enc==pColl->enc ){
83400 /* The strings are already in the correct encoding. Call the
83401 ** comparison function directly */
83402 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
83403 }else{
83404 int rc;
83405 const void *v1, *v2;
83406 Mem c1;
83407 Mem c2;
83408 sqlite3VdbeMemInit(pMem: &c1, db: pMem1->db, MEM_Null);
83409 sqlite3VdbeMemInit(pMem: &c2, db: pMem1->db, MEM_Null);
83410 sqlite3VdbeMemShallowCopy(pTo: &c1, pFrom: pMem1, MEM_Ephem);
83411 sqlite3VdbeMemShallowCopy(pTo: &c2, pFrom: pMem2, MEM_Ephem);
83412 v1 = sqlite3ValueText(pVal: (sqlite3_value*)&c1, enc: pColl->enc);
83413 v2 = sqlite3ValueText(pVal: (sqlite3_value*)&c2, enc: pColl->enc);
83414 if( (v1==0 || v2==0) ){
83415 if( prcErr ) *prcErr = SQLITE_NOMEM_BKPT;
83416 rc = 0;
83417 }else{
83418 rc = pColl->xCmp(pColl->pUser, c1.n, v1, c2.n, v2);
83419 }
83420 sqlite3VdbeMemRelease(p: &c1);
83421 sqlite3VdbeMemRelease(p: &c2);
83422 return rc;
83423 }
83424}
83425
83426/*
83427** The input pBlob is guaranteed to be a Blob that is not marked
83428** with MEM_Zero. Return true if it could be a zero-blob.
83429*/
83430static int isAllZero(const char *z, int n){
83431 int i;
83432 for(i=0; i<n; i++){
83433 if( z[i] ) return 0;
83434 }
83435 return 1;
83436}
83437
83438/*
83439** Compare two blobs. Return negative, zero, or positive if the first
83440** is less than, equal to, or greater than the second, respectively.
83441** If one blob is a prefix of the other, then the shorter is the lessor.
83442*/
83443SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){
83444 int c;
83445 int n1 = pB1->n;
83446 int n2 = pB2->n;
83447
83448 /* It is possible to have a Blob value that has some non-zero content
83449 ** followed by zero content. But that only comes up for Blobs formed
83450 ** by the OP_MakeRecord opcode, and such Blobs never get passed into
83451 ** sqlite3MemCompare(). */
83452 assert( (pB1->flags & MEM_Zero)==0 || n1==0 );
83453 assert( (pB2->flags & MEM_Zero)==0 || n2==0 );
83454
83455 if( (pB1->flags|pB2->flags) & MEM_Zero ){
83456 if( pB1->flags & pB2->flags & MEM_Zero ){
83457 return pB1->u.nZero - pB2->u.nZero;
83458 }else if( pB1->flags & MEM_Zero ){
83459 if( !isAllZero(z: pB2->z, n: pB2->n) ) return -1;
83460 return pB1->u.nZero - n2;
83461 }else{
83462 if( !isAllZero(z: pB1->z, n: pB1->n) ) return +1;
83463 return n1 - pB2->u.nZero;
83464 }
83465 }
83466 c = memcmp(s1: pB1->z, s2: pB2->z, n: n1>n2 ? n2 : n1);
83467 if( c ) return c;
83468 return n1 - n2;
83469}
83470
83471/*
83472** Do a comparison between a 64-bit signed integer and a 64-bit floating-point
83473** number. Return negative, zero, or positive if the first (i64) is less than,
83474** equal to, or greater than the second (double).
83475*/
83476SQLITE_PRIVATE int sqlite3IntFloatCompare(i64 i, double r){
83477 if( sizeof(LONGDOUBLE_TYPE)>8 ){
83478 LONGDOUBLE_TYPE x = (LONGDOUBLE_TYPE)i;
83479 testcase( x<r );
83480 testcase( x>r );
83481 testcase( x==r );
83482 if( x<r ) return -1;
83483 if( x>r ) return +1; /*NO_TEST*/ /* work around bugs in gcov */
83484 return 0; /*NO_TEST*/ /* work around bugs in gcov */
83485 }else{
83486 i64 y;
83487 double s;
83488 if( r<-9223372036854775808.0 ) return +1;
83489 if( r>=9223372036854775808.0 ) return -1;
83490 y = (i64)r;
83491 if( i<y ) return -1;
83492 if( i>y ) return +1;
83493 s = (double)i;
83494 if( s<r ) return -1;
83495 if( s>r ) return +1;
83496 return 0;
83497 }
83498}
83499
83500/*
83501** Compare the values contained by the two memory cells, returning
83502** negative, zero or positive if pMem1 is less than, equal to, or greater
83503** than pMem2. Sorting order is NULL's first, followed by numbers (integers
83504** and reals) sorted numerically, followed by text ordered by the collating
83505** sequence pColl and finally blob's ordered by memcmp().
83506**
83507** Two NULL values are considered equal by this function.
83508*/
83509SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
83510 int f1, f2;
83511 int combined_flags;
83512
83513 f1 = pMem1->flags;
83514 f2 = pMem2->flags;
83515 combined_flags = f1|f2;
83516 assert( !sqlite3VdbeMemIsRowSet(pMem1) && !sqlite3VdbeMemIsRowSet(pMem2) );
83517
83518 /* If one value is NULL, it is less than the other. If both values
83519 ** are NULL, return 0.
83520 */
83521 if( combined_flags&MEM_Null ){
83522 return (f2&MEM_Null) - (f1&MEM_Null);
83523 }
83524
83525 /* At least one of the two values is a number
83526 */
83527 if( combined_flags&(MEM_Int|MEM_Real|MEM_IntReal) ){
83528 testcase( combined_flags & MEM_Int );
83529 testcase( combined_flags & MEM_Real );
83530 testcase( combined_flags & MEM_IntReal );
83531 if( (f1 & f2 & (MEM_Int|MEM_IntReal))!=0 ){
83532 testcase( f1 & f2 & MEM_Int );
83533 testcase( f1 & f2 & MEM_IntReal );
83534 if( pMem1->u.i < pMem2->u.i ) return -1;
83535 if( pMem1->u.i > pMem2->u.i ) return +1;
83536 return 0;
83537 }
83538 if( (f1 & f2 & MEM_Real)!=0 ){
83539 if( pMem1->u.r < pMem2->u.r ) return -1;
83540 if( pMem1->u.r > pMem2->u.r ) return +1;
83541 return 0;
83542 }
83543 if( (f1&(MEM_Int|MEM_IntReal))!=0 ){
83544 testcase( f1 & MEM_Int );
83545 testcase( f1 & MEM_IntReal );
83546 if( (f2&MEM_Real)!=0 ){
83547 return sqlite3IntFloatCompare(i: pMem1->u.i, r: pMem2->u.r);
83548 }else if( (f2&(MEM_Int|MEM_IntReal))!=0 ){
83549 if( pMem1->u.i < pMem2->u.i ) return -1;
83550 if( pMem1->u.i > pMem2->u.i ) return +1;
83551 return 0;
83552 }else{
83553 return -1;
83554 }
83555 }
83556 if( (f1&MEM_Real)!=0 ){
83557 if( (f2&(MEM_Int|MEM_IntReal))!=0 ){
83558 testcase( f2 & MEM_Int );
83559 testcase( f2 & MEM_IntReal );
83560 return -sqlite3IntFloatCompare(i: pMem2->u.i, r: pMem1->u.r);
83561 }else{
83562 return -1;
83563 }
83564 }
83565 return +1;
83566 }
83567
83568 /* If one value is a string and the other is a blob, the string is less.
83569 ** If both are strings, compare using the collating functions.
83570 */
83571 if( combined_flags&MEM_Str ){
83572 if( (f1 & MEM_Str)==0 ){
83573 return 1;
83574 }
83575 if( (f2 & MEM_Str)==0 ){
83576 return -1;
83577 }
83578
83579 assert( pMem1->enc==pMem2->enc || pMem1->db->mallocFailed );
83580 assert( pMem1->enc==SQLITE_UTF8 ||
83581 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
83582
83583 /* The collation sequence must be defined at this point, even if
83584 ** the user deletes the collation sequence after the vdbe program is
83585 ** compiled (this was not always the case).
83586 */
83587 assert( !pColl || pColl->xCmp );
83588
83589 if( pColl ){
83590 return vdbeCompareMemString(pMem1, pMem2, pColl, prcErr: 0);
83591 }
83592 /* If a NULL pointer was passed as the collate function, fall through
83593 ** to the blob case and use memcmp(). */
83594 }
83595
83596 /* Both values must be blobs. Compare using memcmp(). */
83597 return sqlite3BlobCompare(pB1: pMem1, pB2: pMem2);
83598}
83599
83600
83601/*
83602** The first argument passed to this function is a serial-type that
83603** corresponds to an integer - all values between 1 and 9 inclusive
83604** except 7. The second points to a buffer containing an integer value
83605** serialized according to serial_type. This function deserializes
83606** and returns the value.
83607*/
83608static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){
83609 u32 y;
83610 assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) );
83611 switch( serial_type ){
83612 case 0:
83613 case 1:
83614 testcase( aKey[0]&0x80 );
83615 return ONE_BYTE_INT(aKey);
83616 case 2:
83617 testcase( aKey[0]&0x80 );
83618 return TWO_BYTE_INT(aKey);
83619 case 3:
83620 testcase( aKey[0]&0x80 );
83621 return THREE_BYTE_INT(aKey);
83622 case 4: {
83623 testcase( aKey[0]&0x80 );
83624 y = FOUR_BYTE_UINT(aKey);
83625 return (i64)*(int*)&y;
83626 }
83627 case 5: {
83628 testcase( aKey[0]&0x80 );
83629 return FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
83630 }
83631 case 6: {
83632 u64 x = FOUR_BYTE_UINT(aKey);
83633 testcase( aKey[0]&0x80 );
83634 x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
83635 return (i64)*(i64*)&x;
83636 }
83637 }
83638
83639 return (serial_type - 8);
83640}
83641
83642/*
83643** This function compares the two table rows or index records
83644** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero
83645** or positive integer if key1 is less than, equal to or
83646** greater than key2. The {nKey1, pKey1} key must be a blob
83647** created by the OP_MakeRecord opcode of the VDBE. The pPKey2
83648** key must be a parsed key such as obtained from
83649** sqlite3VdbeParseRecord.
83650**
83651** If argument bSkip is non-zero, it is assumed that the caller has already
83652** determined that the first fields of the keys are equal.
83653**
83654** Key1 and Key2 do not have to contain the same number of fields. If all
83655** fields that appear in both keys are equal, then pPKey2->default_rc is
83656** returned.
83657**
83658** If database corruption is discovered, set pPKey2->errCode to
83659** SQLITE_CORRUPT and return 0. If an OOM error is encountered,
83660** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
83661** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
83662*/
83663SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(
83664 int nKey1, const void *pKey1, /* Left key */
83665 UnpackedRecord *pPKey2, /* Right key */
83666 int bSkip /* If true, skip the first field */
83667){
83668 u32 d1; /* Offset into aKey[] of next data element */
83669 int i; /* Index of next field to compare */
83670 u32 szHdr1; /* Size of record header in bytes */
83671 u32 idx1; /* Offset of first type in header */
83672 int rc = 0; /* Return value */
83673 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */
83674 KeyInfo *pKeyInfo;
83675 const unsigned char *aKey1 = (const unsigned char *)pKey1;
83676 Mem mem1;
83677
83678 /* If bSkip is true, then the caller has already determined that the first
83679 ** two elements in the keys are equal. Fix the various stack variables so
83680 ** that this routine begins comparing at the second field. */
83681 if( bSkip ){
83682 u32 s1;
83683 idx1 = 1 + getVarint32(&aKey1[1], s1);
83684 szHdr1 = aKey1[0];
83685 d1 = szHdr1 + sqlite3VdbeSerialTypeLen(serial_type: s1);
83686 i = 1;
83687 pRhs++;
83688 }else{
83689 idx1 = getVarint32(aKey1, szHdr1);
83690 d1 = szHdr1;
83691 i = 0;
83692 }
83693 if( d1>(unsigned)nKey1 ){
83694 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
83695 return 0; /* Corruption */
83696 }
83697
83698 VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
83699 assert( pPKey2->pKeyInfo->nAllField>=pPKey2->nField
83700 || CORRUPT_DB );
83701 assert( pPKey2->pKeyInfo->aSortFlags!=0 );
83702 assert( pPKey2->pKeyInfo->nKeyField>0 );
83703 assert( idx1<=szHdr1 || CORRUPT_DB );
83704 do{
83705 u32 serial_type;
83706
83707 /* RHS is an integer */
83708 if( pRhs->flags & (MEM_Int|MEM_IntReal) ){
83709 testcase( pRhs->flags & MEM_Int );
83710 testcase( pRhs->flags & MEM_IntReal );
83711 serial_type = aKey1[idx1];
83712 testcase( serial_type==12 );
83713 if( serial_type>=10 ){
83714 rc = +1;
83715 }else if( serial_type==0 ){
83716 rc = -1;
83717 }else if( serial_type==7 ){
83718 sqlite3VdbeSerialGet(buf: &aKey1[d1], serial_type, pMem: &mem1);
83719 rc = -sqlite3IntFloatCompare(i: pRhs->u.i, r: mem1.u.r);
83720 }else{
83721 i64 lhs = vdbeRecordDecodeInt(serial_type, aKey: &aKey1[d1]);
83722 i64 rhs = pRhs->u.i;
83723 if( lhs<rhs ){
83724 rc = -1;
83725 }else if( lhs>rhs ){
83726 rc = +1;
83727 }
83728 }
83729 }
83730
83731 /* RHS is real */
83732 else if( pRhs->flags & MEM_Real ){
83733 serial_type = aKey1[idx1];
83734 if( serial_type>=10 ){
83735 /* Serial types 12 or greater are strings and blobs (greater than
83736 ** numbers). Types 10 and 11 are currently "reserved for future
83737 ** use", so it doesn't really matter what the results of comparing
83738 ** them to numberic values are. */
83739 rc = +1;
83740 }else if( serial_type==0 ){
83741 rc = -1;
83742 }else{
83743 sqlite3VdbeSerialGet(buf: &aKey1[d1], serial_type, pMem: &mem1);
83744 if( serial_type==7 ){
83745 if( mem1.u.r<pRhs->u.r ){
83746 rc = -1;
83747 }else if( mem1.u.r>pRhs->u.r ){
83748 rc = +1;
83749 }
83750 }else{
83751 rc = sqlite3IntFloatCompare(i: mem1.u.i, r: pRhs->u.r);
83752 }
83753 }
83754 }
83755
83756 /* RHS is a string */
83757 else if( pRhs->flags & MEM_Str ){
83758 getVarint32NR(&aKey1[idx1], serial_type);
83759 testcase( serial_type==12 );
83760 if( serial_type<12 ){
83761 rc = -1;
83762 }else if( !(serial_type & 0x01) ){
83763 rc = +1;
83764 }else{
83765 mem1.n = (serial_type - 12) / 2;
83766 testcase( (d1+mem1.n)==(unsigned)nKey1 );
83767 testcase( (d1+mem1.n+1)==(unsigned)nKey1 );
83768 if( (d1+mem1.n) > (unsigned)nKey1
83769 || (pKeyInfo = pPKey2->pKeyInfo)->nAllField<=i
83770 ){
83771 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
83772 return 0; /* Corruption */
83773 }else if( pKeyInfo->aColl[i] ){
83774 mem1.enc = pKeyInfo->enc;
83775 mem1.db = pKeyInfo->db;
83776 mem1.flags = MEM_Str;
83777 mem1.z = (char*)&aKey1[d1];
83778 rc = vdbeCompareMemString(
83779 pMem1: &mem1, pMem2: pRhs, pColl: pKeyInfo->aColl[i], prcErr: &pPKey2->errCode
83780 );
83781 }else{
83782 int nCmp = MIN(mem1.n, pRhs->n);
83783 rc = memcmp(s1: &aKey1[d1], s2: pRhs->z, n: nCmp);
83784 if( rc==0 ) rc = mem1.n - pRhs->n;
83785 }
83786 }
83787 }
83788
83789 /* RHS is a blob */
83790 else if( pRhs->flags & MEM_Blob ){
83791 assert( (pRhs->flags & MEM_Zero)==0 || pRhs->n==0 );
83792 getVarint32NR(&aKey1[idx1], serial_type);
83793 testcase( serial_type==12 );
83794 if( serial_type<12 || (serial_type & 0x01) ){
83795 rc = -1;
83796 }else{
83797 int nStr = (serial_type - 12) / 2;
83798 testcase( (d1+nStr)==(unsigned)nKey1 );
83799 testcase( (d1+nStr+1)==(unsigned)nKey1 );
83800 if( (d1+nStr) > (unsigned)nKey1 ){
83801 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
83802 return 0; /* Corruption */
83803 }else if( pRhs->flags & MEM_Zero ){
83804 if( !isAllZero(z: (const char*)&aKey1[d1],n: nStr) ){
83805 rc = 1;
83806 }else{
83807 rc = nStr - pRhs->u.nZero;
83808 }
83809 }else{
83810 int nCmp = MIN(nStr, pRhs->n);
83811 rc = memcmp(s1: &aKey1[d1], s2: pRhs->z, n: nCmp);
83812 if( rc==0 ) rc = nStr - pRhs->n;
83813 }
83814 }
83815 }
83816
83817 /* RHS is null */
83818 else{
83819 serial_type = aKey1[idx1];
83820 rc = (serial_type!=0);
83821 }
83822
83823 if( rc!=0 ){
83824 int sortFlags = pPKey2->pKeyInfo->aSortFlags[i];
83825 if( sortFlags ){
83826 if( (sortFlags & KEYINFO_ORDER_BIGNULL)==0
83827 || ((sortFlags & KEYINFO_ORDER_DESC)
83828 !=(serial_type==0 || (pRhs->flags&MEM_Null)))
83829 ){
83830 rc = -rc;
83831 }
83832 }
83833 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, rc) );
83834 assert( mem1.szMalloc==0 ); /* See comment below */
83835 return rc;
83836 }
83837
83838 i++;
83839 if( i==pPKey2->nField ) break;
83840 pRhs++;
83841 d1 += sqlite3VdbeSerialTypeLen(serial_type);
83842 idx1 += sqlite3VarintLen(v: serial_type);
83843 }while( idx1<(unsigned)szHdr1 && d1<=(unsigned)nKey1 );
83844
83845 /* No memory allocation is ever used on mem1. Prove this using
83846 ** the following assert(). If the assert() fails, it indicates a
83847 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */
83848 assert( mem1.szMalloc==0 );
83849
83850 /* rc==0 here means that one or both of the keys ran out of fields and
83851 ** all the fields up to that point were equal. Return the default_rc
83852 ** value. */
83853 assert( CORRUPT_DB
83854 || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc)
83855 || pPKey2->pKeyInfo->db->mallocFailed
83856 );
83857 pPKey2->eqSeen = 1;
83858 return pPKey2->default_rc;
83859}
83860SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
83861 int nKey1, const void *pKey1, /* Left key */
83862 UnpackedRecord *pPKey2 /* Right key */
83863){
83864 return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, bSkip: 0);
83865}
83866
83867
83868/*
83869** This function is an optimized version of sqlite3VdbeRecordCompare()
83870** that (a) the first field of pPKey2 is an integer, and (b) the
83871** size-of-header varint at the start of (pKey1/nKey1) fits in a single
83872** byte (i.e. is less than 128).
83873**
83874** To avoid concerns about buffer overreads, this routine is only used
83875** on schemas where the maximum valid header size is 63 bytes or less.
83876*/
83877static int vdbeRecordCompareInt(
83878 int nKey1, const void *pKey1, /* Left key */
83879 UnpackedRecord *pPKey2 /* Right key */
83880){
83881 const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F];
83882 int serial_type = ((const u8*)pKey1)[1];
83883 int res;
83884 u32 y;
83885 u64 x;
83886 i64 v;
83887 i64 lhs;
83888
83889 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
83890 assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB );
83891 switch( serial_type ){
83892 case 1: { /* 1-byte signed integer */
83893 lhs = ONE_BYTE_INT(aKey);
83894 testcase( lhs<0 );
83895 break;
83896 }
83897 case 2: { /* 2-byte signed integer */
83898 lhs = TWO_BYTE_INT(aKey);
83899 testcase( lhs<0 );
83900 break;
83901 }
83902 case 3: { /* 3-byte signed integer */
83903 lhs = THREE_BYTE_INT(aKey);
83904 testcase( lhs<0 );
83905 break;
83906 }
83907 case 4: { /* 4-byte signed integer */
83908 y = FOUR_BYTE_UINT(aKey);
83909 lhs = (i64)*(int*)&y;
83910 testcase( lhs<0 );
83911 break;
83912 }
83913 case 5: { /* 6-byte signed integer */
83914 lhs = FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
83915 testcase( lhs<0 );
83916 break;
83917 }
83918 case 6: { /* 8-byte signed integer */
83919 x = FOUR_BYTE_UINT(aKey);
83920 x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
83921 lhs = *(i64*)&x;
83922 testcase( lhs<0 );
83923 break;
83924 }
83925 case 8:
83926 lhs = 0;
83927 break;
83928 case 9:
83929 lhs = 1;
83930 break;
83931
83932 /* This case could be removed without changing the results of running
83933 ** this code. Including it causes gcc to generate a faster switch
83934 ** statement (since the range of switch targets now starts at zero and
83935 ** is contiguous) but does not cause any duplicate code to be generated
83936 ** (as gcc is clever enough to combine the two like cases). Other
83937 ** compilers might be similar. */
83938 case 0: case 7:
83939 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
83940
83941 default:
83942 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
83943 }
83944
83945 v = pPKey2->aMem[0].u.i;
83946 if( v>lhs ){
83947 res = pPKey2->r1;
83948 }else if( v<lhs ){
83949 res = pPKey2->r2;
83950 }else if( pPKey2->nField>1 ){
83951 /* The first fields of the two keys are equal. Compare the trailing
83952 ** fields. */
83953 res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, bSkip: 1);
83954 }else{
83955 /* The first fields of the two keys are equal and there are no trailing
83956 ** fields. Return pPKey2->default_rc in this case. */
83957 res = pPKey2->default_rc;
83958 pPKey2->eqSeen = 1;
83959 }
83960
83961 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) );
83962 return res;
83963}
83964
83965/*
83966** This function is an optimized version of sqlite3VdbeRecordCompare()
83967** that (a) the first field of pPKey2 is a string, that (b) the first field
83968** uses the collation sequence BINARY and (c) that the size-of-header varint
83969** at the start of (pKey1/nKey1) fits in a single byte.
83970*/
83971static int vdbeRecordCompareString(
83972 int nKey1, const void *pKey1, /* Left key */
83973 UnpackedRecord *pPKey2 /* Right key */
83974){
83975 const u8 *aKey1 = (const u8*)pKey1;
83976 int serial_type;
83977 int res;
83978
83979 assert( pPKey2->aMem[0].flags & MEM_Str );
83980 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
83981 serial_type = (u8)(aKey1[1]);
83982 if( serial_type >= 0x80 ){
83983 sqlite3GetVarint32(p: &aKey1[1], v: (u32*)&serial_type);
83984 }
83985 if( serial_type<12 ){
83986 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */
83987 }else if( !(serial_type & 0x01) ){
83988 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */
83989 }else{
83990 int nCmp;
83991 int nStr;
83992 int szHdr = aKey1[0];
83993
83994 nStr = (serial_type-12) / 2;
83995 if( (szHdr + nStr) > nKey1 ){
83996 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
83997 return 0; /* Corruption */
83998 }
83999 nCmp = MIN( pPKey2->aMem[0].n, nStr );
84000 res = memcmp(s1: &aKey1[szHdr], s2: pPKey2->aMem[0].z, n: nCmp);
84001
84002 if( res>0 ){
84003 res = pPKey2->r2;
84004 }else if( res<0 ){
84005 res = pPKey2->r1;
84006 }else{
84007 res = nStr - pPKey2->aMem[0].n;
84008 if( res==0 ){
84009 if( pPKey2->nField>1 ){
84010 res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, bSkip: 1);
84011 }else{
84012 res = pPKey2->default_rc;
84013 pPKey2->eqSeen = 1;
84014 }
84015 }else if( res>0 ){
84016 res = pPKey2->r2;
84017 }else{
84018 res = pPKey2->r1;
84019 }
84020 }
84021 }
84022
84023 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res)
84024 || CORRUPT_DB
84025 || pPKey2->pKeyInfo->db->mallocFailed
84026 );
84027 return res;
84028}
84029
84030/*
84031** Return a pointer to an sqlite3VdbeRecordCompare() compatible function
84032** suitable for comparing serialized records to the unpacked record passed
84033** as the only argument.
84034*/
84035SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){
84036 /* varintRecordCompareInt() and varintRecordCompareString() both assume
84037 ** that the size-of-header varint that occurs at the start of each record
84038 ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt()
84039 ** also assumes that it is safe to overread a buffer by at least the
84040 ** maximum possible legal header size plus 8 bytes. Because there is
84041 ** guaranteed to be at least 74 (but not 136) bytes of padding following each
84042 ** buffer passed to varintRecordCompareInt() this makes it convenient to
84043 ** limit the size of the header to 64 bytes in cases where the first field
84044 ** is an integer.
84045 **
84046 ** The easiest way to enforce this limit is to consider only records with
84047 ** 13 fields or less. If the first field is an integer, the maximum legal
84048 ** header size is (12*5 + 1 + 1) bytes. */
84049 if( p->pKeyInfo->nAllField<=13 ){
84050 int flags = p->aMem[0].flags;
84051 if( p->pKeyInfo->aSortFlags[0] ){
84052 if( p->pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL ){
84053 return sqlite3VdbeRecordCompare;
84054 }
84055 p->r1 = 1;
84056 p->r2 = -1;
84057 }else{
84058 p->r1 = -1;
84059 p->r2 = 1;
84060 }
84061 if( (flags & MEM_Int) ){
84062 return vdbeRecordCompareInt;
84063 }
84064 testcase( flags & MEM_Real );
84065 testcase( flags & MEM_Null );
84066 testcase( flags & MEM_Blob );
84067 if( (flags & (MEM_Real|MEM_IntReal|MEM_Null|MEM_Blob))==0
84068 && p->pKeyInfo->aColl[0]==0
84069 ){
84070 assert( flags & MEM_Str );
84071 return vdbeRecordCompareString;
84072 }
84073 }
84074
84075 return sqlite3VdbeRecordCompare;
84076}
84077
84078/*
84079** pCur points at an index entry created using the OP_MakeRecord opcode.
84080** Read the rowid (the last field in the record) and store it in *rowid.
84081** Return SQLITE_OK if everything works, or an error code otherwise.
84082**
84083** pCur might be pointing to text obtained from a corrupt database file.
84084** So the content cannot be trusted. Do appropriate checks on the content.
84085*/
84086SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
84087 i64 nCellKey = 0;
84088 int rc;
84089 u32 szHdr; /* Size of the header */
84090 u32 typeRowid; /* Serial type of the rowid */
84091 u32 lenRowid; /* Size of the rowid */
84092 Mem m, v;
84093
84094 /* Get the size of the index entry. Only indices entries of less
84095 ** than 2GiB are support - anything large must be database corruption.
84096 ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
84097 ** this code can safely assume that nCellKey is 32-bits
84098 */
84099 assert( sqlite3BtreeCursorIsValid(pCur) );
84100 nCellKey = sqlite3BtreePayloadSize(pCur);
84101 assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
84102
84103 /* Read in the complete content of the index entry */
84104 sqlite3VdbeMemInit(pMem: &m, db, flags: 0);
84105 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, amt: (u32)nCellKey, pMem: &m);
84106 if( rc ){
84107 return rc;
84108 }
84109
84110 /* The index entry must begin with a header size */
84111 getVarint32NR((u8*)m.z, szHdr);
84112 testcase( szHdr==3 );
84113 testcase( szHdr==(u32)m.n );
84114 testcase( szHdr>0x7fffffff );
84115 assert( m.n>=0 );
84116 if( unlikely(szHdr<3 || szHdr>(unsigned)m.n) ){
84117 goto idx_rowid_corruption;
84118 }
84119
84120 /* The last field of the index should be an integer - the ROWID.
84121 ** Verify that the last entry really is an integer. */
84122 getVarint32NR((u8*)&m.z[szHdr-1], typeRowid);
84123 testcase( typeRowid==1 );
84124 testcase( typeRowid==2 );
84125 testcase( typeRowid==3 );
84126 testcase( typeRowid==4 );
84127 testcase( typeRowid==5 );
84128 testcase( typeRowid==6 );
84129 testcase( typeRowid==8 );
84130 testcase( typeRowid==9 );
84131 if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
84132 goto idx_rowid_corruption;
84133 }
84134 lenRowid = sqlite3SmallTypeSizes[typeRowid];
84135 testcase( (u32)m.n==szHdr+lenRowid );
84136 if( unlikely((u32)m.n<szHdr+lenRowid) ){
84137 goto idx_rowid_corruption;
84138 }
84139
84140 /* Fetch the integer off the end of the index record */
84141 sqlite3VdbeSerialGet(buf: (u8*)&m.z[m.n-lenRowid], serial_type: typeRowid, pMem: &v);
84142 *rowid = v.u.i;
84143 sqlite3VdbeMemRelease(p: &m);
84144 return SQLITE_OK;
84145
84146 /* Jump here if database corruption is detected after m has been
84147 ** allocated. Free the m object and return SQLITE_CORRUPT. */
84148idx_rowid_corruption:
84149 testcase( m.szMalloc!=0 );
84150 sqlite3VdbeMemRelease(p: &m);
84151 return SQLITE_CORRUPT_BKPT;
84152}
84153
84154/*
84155** Compare the key of the index entry that cursor pC is pointing to against
84156** the key string in pUnpacked. Write into *pRes a number
84157** that is negative, zero, or positive if pC is less than, equal to,
84158** or greater than pUnpacked. Return SQLITE_OK on success.
84159**
84160** pUnpacked is either created without a rowid or is truncated so that it
84161** omits the rowid at the end. The rowid at the end of the index entry
84162** is ignored as well. Hence, this routine only compares the prefixes
84163** of the keys prior to the final rowid, not the entire key.
84164*/
84165SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(
84166 sqlite3 *db, /* Database connection */
84167 VdbeCursor *pC, /* The cursor to compare against */
84168 UnpackedRecord *pUnpacked, /* Unpacked version of key */
84169 int *res /* Write the comparison result here */
84170){
84171 i64 nCellKey = 0;
84172 int rc;
84173 BtCursor *pCur;
84174 Mem m;
84175
84176 assert( pC->eCurType==CURTYPE_BTREE );
84177 pCur = pC->uc.pCursor;
84178 assert( sqlite3BtreeCursorIsValid(pCur) );
84179 nCellKey = sqlite3BtreePayloadSize(pCur);
84180 /* nCellKey will always be between 0 and 0xffffffff because of the way
84181 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
84182 if( nCellKey<=0 || nCellKey>0x7fffffff ){
84183 *res = 0;
84184 return SQLITE_CORRUPT_BKPT;
84185 }
84186 sqlite3VdbeMemInit(pMem: &m, db, flags: 0);
84187 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, amt: (u32)nCellKey, pMem: &m);
84188 if( rc ){
84189 return rc;
84190 }
84191 *res = sqlite3VdbeRecordCompareWithSkip(nKey1: m.n, pKey1: m.z, pPKey2: pUnpacked, bSkip: 0);
84192 sqlite3VdbeMemRelease(p: &m);
84193 return SQLITE_OK;
84194}
84195
84196/*
84197** This routine sets the value to be returned by subsequent calls to
84198** sqlite3_changes() on the database handle 'db'.
84199*/
84200SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *db, i64 nChange){
84201 assert( sqlite3_mutex_held(db->mutex) );
84202 db->nChange = nChange;
84203 db->nTotalChange += nChange;
84204}
84205
84206/*
84207** Set a flag in the vdbe to update the change counter when it is finalised
84208** or reset.
84209*/
84210SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe *v){
84211 v->changeCntOn = 1;
84212}
84213
84214/*
84215** Mark every prepared statement associated with a database connection
84216** as expired.
84217**
84218** An expired statement means that recompilation of the statement is
84219** recommend. Statements expire when things happen that make their
84220** programs obsolete. Removing user-defined functions or collating
84221** sequences, or changing an authorization function are the types of
84222** things that make prepared statements obsolete.
84223**
84224** If iCode is 1, then expiration is advisory. The statement should
84225** be reprepared before being restarted, but if it is already running
84226** it is allowed to run to completion.
84227**
84228** Internally, this function just sets the Vdbe.expired flag on all
84229** prepared statements. The flag is set to 1 for an immediate expiration
84230** and set to 2 for an advisory expiration.
84231*/
84232SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3 *db, int iCode){
84233 Vdbe *p;
84234 for(p = db->pVdbe; p; p=p->pNext){
84235 p->expired = iCode+1;
84236 }
84237}
84238
84239/*
84240** Return the database associated with the Vdbe.
84241*/
84242SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe *v){
84243 return v->db;
84244}
84245
84246/*
84247** Return the SQLITE_PREPARE flags for a Vdbe.
84248*/
84249SQLITE_PRIVATE u8 sqlite3VdbePrepareFlags(Vdbe *v){
84250 return v->prepFlags;
84251}
84252
84253/*
84254** Return a pointer to an sqlite3_value structure containing the value bound
84255** parameter iVar of VM v. Except, if the value is an SQL NULL, return
84256** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
84257** constants) to the value before returning it.
84258**
84259** The returned value must be freed by the caller using sqlite3ValueFree().
84260*/
84261SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){
84262 assert( iVar>0 );
84263 if( v ){
84264 Mem *pMem = &v->aVar[iVar-1];
84265 assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
84266 if( 0==(pMem->flags & MEM_Null) ){
84267 sqlite3_value *pRet = sqlite3ValueNew(db: v->db);
84268 if( pRet ){
84269 sqlite3VdbeMemCopy(pTo: (Mem *)pRet, pFrom: pMem);
84270 sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
84271 }
84272 return pRet;
84273 }
84274 }
84275 return 0;
84276}
84277
84278/*
84279** Configure SQL variable iVar so that binding a new value to it signals
84280** to sqlite3_reoptimize() that re-preparing the statement may result
84281** in a better query plan.
84282*/
84283SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
84284 assert( iVar>0 );
84285 assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
84286 if( iVar>=32 ){
84287 v->expmask |= 0x80000000;
84288 }else{
84289 v->expmask |= ((u32)1 << (iVar-1));
84290 }
84291}
84292
84293/*
84294** Cause a function to throw an error if it was call from OP_PureFunc
84295** rather than OP_Function.
84296**
84297** OP_PureFunc means that the function must be deterministic, and should
84298** throw an error if it is given inputs that would make it non-deterministic.
84299** This routine is invoked by date/time functions that use non-deterministic
84300** features such as 'now'.
84301*/
84302SQLITE_PRIVATE int sqlite3NotPureFunc(sqlite3_context *pCtx){
84303 const VdbeOp *pOp;
84304#ifdef SQLITE_ENABLE_STAT4
84305 if( pCtx->pVdbe==0 ) return 1;
84306#endif
84307 pOp = pCtx->pVdbe->aOp + pCtx->iOp;
84308 if( pOp->opcode==OP_PureFunc ){
84309 const char *zContext;
84310 char *zMsg;
84311 if( pOp->p5 & NC_IsCheck ){
84312 zContext = "a CHECK constraint";
84313 }else if( pOp->p5 & NC_GenCol ){
84314 zContext = "a generated column";
84315 }else{
84316 zContext = "an index";
84317 }
84318 zMsg = sqlite3_mprintf(zFormat: "non-deterministic use of %s() in %s",
84319 pCtx->pFunc->zName, zContext);
84320 sqlite3_result_error(pCtx, zMsg, -1);
84321 sqlite3_free(p: zMsg);
84322 return 0;
84323 }
84324 return 1;
84325}
84326
84327#ifndef SQLITE_OMIT_VIRTUALTABLE
84328/*
84329** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
84330** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
84331** in memory obtained from sqlite3DbMalloc).
84332*/
84333SQLITE_PRIVATE void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){
84334 if( pVtab->zErrMsg ){
84335 sqlite3 *db = p->db;
84336 sqlite3DbFree(db, p: p->zErrMsg);
84337 p->zErrMsg = sqlite3DbStrDup(db, z: pVtab->zErrMsg);
84338 sqlite3_free(p: pVtab->zErrMsg);
84339 pVtab->zErrMsg = 0;
84340 }
84341}
84342#endif /* SQLITE_OMIT_VIRTUALTABLE */
84343
84344#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
84345
84346/*
84347** If the second argument is not NULL, release any allocations associated
84348** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord
84349** structure itself, using sqlite3DbFree().
84350**
84351** This function is used to free UnpackedRecord structures allocated by
84352** the vdbeUnpackRecord() function found in vdbeapi.c.
84353*/
84354static void vdbeFreeUnpacked(sqlite3 *db, int nField, UnpackedRecord *p){
84355 if( p ){
84356 int i;
84357 for(i=0; i<nField; i++){
84358 Mem *pMem = &p->aMem[i];
84359 if( pMem->zMalloc ) sqlite3VdbeMemRelease(pMem);
84360 }
84361 sqlite3DbFreeNN(db, p);
84362 }
84363}
84364#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
84365
84366#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
84367/*
84368** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call,
84369** then cursor passed as the second argument should point to the row about
84370** to be update or deleted. If the application calls sqlite3_preupdate_old(),
84371** the required value will be read from the row the cursor points to.
84372*/
84373SQLITE_PRIVATE void sqlite3VdbePreUpdateHook(
84374 Vdbe *v, /* Vdbe pre-update hook is invoked by */
84375 VdbeCursor *pCsr, /* Cursor to grab old.* values from */
84376 int op, /* SQLITE_INSERT, UPDATE or DELETE */
84377 const char *zDb, /* Database name */
84378 Table *pTab, /* Modified table */
84379 i64 iKey1, /* Initial key value */
84380 int iReg, /* Register for new.* record */
84381 int iBlobWrite
84382){
84383 sqlite3 *db = v->db;
84384 i64 iKey2;
84385 PreUpdate preupdate;
84386 const char *zTbl = pTab->zName;
84387 static const u8 fakeSortOrder = 0;
84388
84389 assert( db->pPreUpdate==0 );
84390 memset(&preupdate, 0, sizeof(PreUpdate));
84391 if( HasRowid(pTab)==0 ){
84392 iKey1 = iKey2 = 0;
84393 preupdate.pPk = sqlite3PrimaryKeyIndex(pTab);
84394 }else{
84395 if( op==SQLITE_UPDATE ){
84396 iKey2 = v->aMem[iReg].u.i;
84397 }else{
84398 iKey2 = iKey1;
84399 }
84400 }
84401
84402 assert( pCsr!=0 );
84403 assert( pCsr->eCurType==CURTYPE_BTREE );
84404 assert( pCsr->nField==pTab->nCol
84405 || (pCsr->nField==pTab->nCol+1 && op==SQLITE_DELETE && iReg==-1)
84406 );
84407
84408 preupdate.v = v;
84409 preupdate.pCsr = pCsr;
84410 preupdate.op = op;
84411 preupdate.iNewReg = iReg;
84412 preupdate.keyinfo.db = db;
84413 preupdate.keyinfo.enc = ENC(db);
84414 preupdate.keyinfo.nKeyField = pTab->nCol;
84415 preupdate.keyinfo.aSortFlags = (u8*)&fakeSortOrder;
84416 preupdate.iKey1 = iKey1;
84417 preupdate.iKey2 = iKey2;
84418 preupdate.pTab = pTab;
84419 preupdate.iBlobWrite = iBlobWrite;
84420
84421 db->pPreUpdate = &preupdate;
84422 db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
84423 db->pPreUpdate = 0;
84424 sqlite3DbFree(db, preupdate.aRecord);
84425 vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pUnpacked);
84426 vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pNewUnpacked);
84427 if( preupdate.aNew ){
84428 int i;
84429 for(i=0; i<pCsr->nField; i++){
84430 sqlite3VdbeMemRelease(&preupdate.aNew[i]);
84431 }
84432 sqlite3DbFreeNN(db, preupdate.aNew);
84433 }
84434}
84435#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
84436
84437/************** End of vdbeaux.c *********************************************/
84438/************** Begin file vdbeapi.c *****************************************/
84439/*
84440** 2004 May 26
84441**
84442** The author disclaims copyright to this source code. In place of
84443** a legal notice, here is a blessing:
84444**
84445** May you do good and not evil.
84446** May you find forgiveness for yourself and forgive others.
84447** May you share freely, never taking more than you give.
84448**
84449*************************************************************************
84450**
84451** This file contains code use to implement APIs that are part of the
84452** VDBE.
84453*/
84454/* #include "sqliteInt.h" */
84455/* #include "vdbeInt.h" */
84456
84457#ifndef SQLITE_OMIT_DEPRECATED
84458/*
84459** Return TRUE (non-zero) of the statement supplied as an argument needs
84460** to be recompiled. A statement needs to be recompiled whenever the
84461** execution environment changes in a way that would alter the program
84462** that sqlite3_prepare() generates. For example, if new functions or
84463** collating sequences are registered or if an authorizer function is
84464** added or changed.
84465*/
84466SQLITE_API int sqlite3_expired(sqlite3_stmt *pStmt){
84467 Vdbe *p = (Vdbe*)pStmt;
84468 return p==0 || p->expired;
84469}
84470#endif
84471
84472/*
84473** Check on a Vdbe to make sure it has not been finalized. Log
84474** an error and return true if it has been finalized (or is otherwise
84475** invalid). Return false if it is ok.
84476*/
84477static int vdbeSafety(Vdbe *p){
84478 if( p->db==0 ){
84479 sqlite3_log(SQLITE_MISUSE, zFormat: "API called with finalized prepared statement");
84480 return 1;
84481 }else{
84482 return 0;
84483 }
84484}
84485static int vdbeSafetyNotNull(Vdbe *p){
84486 if( p==0 ){
84487 sqlite3_log(SQLITE_MISUSE, zFormat: "API called with NULL prepared statement");
84488 return 1;
84489 }else{
84490 return vdbeSafety(p);
84491 }
84492}
84493
84494#ifndef SQLITE_OMIT_TRACE
84495/*
84496** Invoke the profile callback. This routine is only called if we already
84497** know that the profile callback is defined and needs to be invoked.
84498*/
84499static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){
84500 sqlite3_int64 iNow;
84501 sqlite3_int64 iElapse;
84502 assert( p->startTime>0 );
84503 assert( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0 );
84504 assert( db->init.busy==0 );
84505 assert( p->zSql!=0 );
84506 sqlite3OsCurrentTimeInt64(pVfs: db->pVfs, pTimeOut: &iNow);
84507 iElapse = (iNow - p->startTime)*1000000;
84508#ifndef SQLITE_OMIT_DEPRECATED
84509 if( db->xProfile ){
84510 db->xProfile(db->pProfileArg, p->zSql, iElapse);
84511 }
84512#endif
84513 if( db->mTrace & SQLITE_TRACE_PROFILE ){
84514 db->trace.xV2(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse);
84515 }
84516 p->startTime = 0;
84517}
84518/*
84519** The checkProfileCallback(DB,P) macro checks to see if a profile callback
84520** is needed, and it invokes the callback if it is needed.
84521*/
84522# define checkProfileCallback(DB,P) \
84523 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
84524#else
84525# define checkProfileCallback(DB,P) /*no-op*/
84526#endif
84527
84528/*
84529** The following routine destroys a virtual machine that is created by
84530** the sqlite3_compile() routine. The integer returned is an SQLITE_
84531** success/failure code that describes the result of executing the virtual
84532** machine.
84533**
84534** This routine sets the error code and string returned by
84535** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
84536*/
84537SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt){
84538 int rc;
84539 if( pStmt==0 ){
84540 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
84541 ** pointer is a harmless no-op. */
84542 rc = SQLITE_OK;
84543 }else{
84544 Vdbe *v = (Vdbe*)pStmt;
84545 sqlite3 *db = v->db;
84546 if( vdbeSafety(p: v) ) return SQLITE_MISUSE_BKPT;
84547 sqlite3_mutex_enter(p: db->mutex);
84548 checkProfileCallback(db, v);
84549 rc = sqlite3VdbeFinalize(p: v);
84550 rc = sqlite3ApiExit(db, rc);
84551 sqlite3LeaveMutexAndCloseZombie(db);
84552 }
84553 return rc;
84554}
84555
84556/*
84557** Terminate the current execution of an SQL statement and reset it
84558** back to its starting state so that it can be reused. A success code from
84559** the prior execution is returned.
84560**
84561** This routine sets the error code and string returned by
84562** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
84563*/
84564SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt){
84565 int rc;
84566 if( pStmt==0 ){
84567 rc = SQLITE_OK;
84568 }else{
84569 Vdbe *v = (Vdbe*)pStmt;
84570 sqlite3 *db = v->db;
84571 sqlite3_mutex_enter(p: db->mutex);
84572 checkProfileCallback(db, v);
84573 rc = sqlite3VdbeReset(p: v);
84574 sqlite3VdbeRewind(p: v);
84575 assert( (rc & (db->errMask))==rc );
84576 rc = sqlite3ApiExit(db, rc);
84577 sqlite3_mutex_leave(p: db->mutex);
84578 }
84579 return rc;
84580}
84581
84582/*
84583** Set all the parameters in the compiled SQL statement to NULL.
84584*/
84585SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
84586 int i;
84587 int rc = SQLITE_OK;
84588 Vdbe *p = (Vdbe*)pStmt;
84589#if SQLITE_THREADSAFE
84590 sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
84591#endif
84592 sqlite3_mutex_enter(p: mutex);
84593 for(i=0; i<p->nVar; i++){
84594 sqlite3VdbeMemRelease(p: &p->aVar[i]);
84595 p->aVar[i].flags = MEM_Null;
84596 }
84597 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
84598 if( p->expmask ){
84599 p->expired = 1;
84600 }
84601 sqlite3_mutex_leave(p: mutex);
84602 return rc;
84603}
84604
84605
84606/**************************** sqlite3_value_ *******************************
84607** The following routines extract information from a Mem or sqlite3_value
84608** structure.
84609*/
84610SQLITE_API const void *sqlite3_value_blob(sqlite3_value *pVal){
84611 Mem *p = (Mem*)pVal;
84612 if( p->flags & (MEM_Blob|MEM_Str) ){
84613 if( ExpandBlob(p)!=SQLITE_OK ){
84614 assert( p->flags==MEM_Null && p->z==0 );
84615 return 0;
84616 }
84617 p->flags |= MEM_Blob;
84618 return p->n ? p->z : 0;
84619 }else{
84620 return sqlite3_value_text(pVal);
84621 }
84622}
84623SQLITE_API int sqlite3_value_bytes(sqlite3_value *pVal){
84624 return sqlite3ValueBytes(pVal, SQLITE_UTF8);
84625}
84626SQLITE_API int sqlite3_value_bytes16(sqlite3_value *pVal){
84627 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
84628}
84629SQLITE_API double sqlite3_value_double(sqlite3_value *pVal){
84630 return sqlite3VdbeRealValue(pMem: (Mem*)pVal);
84631}
84632SQLITE_API int sqlite3_value_int(sqlite3_value *pVal){
84633 return (int)sqlite3VdbeIntValue(pMem: (Mem*)pVal);
84634}
84635SQLITE_API sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
84636 return sqlite3VdbeIntValue(pMem: (Mem*)pVal);
84637}
84638SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value *pVal){
84639 Mem *pMem = (Mem*)pVal;
84640 return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0);
84641}
84642SQLITE_API void *sqlite3_value_pointer(sqlite3_value *pVal, const char *zPType){
84643 Mem *p = (Mem*)pVal;
84644 if( (p->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==
84645 (MEM_Null|MEM_Term|MEM_Subtype)
84646 && zPType!=0
84647 && p->eSubtype=='p'
84648 && strcmp(s1: p->u.zPType, s2: zPType)==0
84649 ){
84650 return (void*)p->z;
84651 }else{
84652 return 0;
84653 }
84654}
84655SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
84656 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
84657}
84658#ifndef SQLITE_OMIT_UTF16
84659SQLITE_API const void *sqlite3_value_text16(sqlite3_value* pVal){
84660 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
84661}
84662SQLITE_API const void *sqlite3_value_text16be(sqlite3_value *pVal){
84663 return sqlite3ValueText(pVal, SQLITE_UTF16BE);
84664}
84665SQLITE_API const void *sqlite3_value_text16le(sqlite3_value *pVal){
84666 return sqlite3ValueText(pVal, SQLITE_UTF16LE);
84667}
84668#endif /* SQLITE_OMIT_UTF16 */
84669/* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
84670** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
84671** point number string BLOB NULL
84672*/
84673SQLITE_API int sqlite3_value_type(sqlite3_value* pVal){
84674 static const u8 aType[] = {
84675 SQLITE_BLOB, /* 0x00 (not possible) */
84676 SQLITE_NULL, /* 0x01 NULL */
84677 SQLITE_TEXT, /* 0x02 TEXT */
84678 SQLITE_NULL, /* 0x03 (not possible) */
84679 SQLITE_INTEGER, /* 0x04 INTEGER */
84680 SQLITE_NULL, /* 0x05 (not possible) */
84681 SQLITE_INTEGER, /* 0x06 INTEGER + TEXT */
84682 SQLITE_NULL, /* 0x07 (not possible) */
84683 SQLITE_FLOAT, /* 0x08 FLOAT */
84684 SQLITE_NULL, /* 0x09 (not possible) */
84685 SQLITE_FLOAT, /* 0x0a FLOAT + TEXT */
84686 SQLITE_NULL, /* 0x0b (not possible) */
84687 SQLITE_INTEGER, /* 0x0c (not possible) */
84688 SQLITE_NULL, /* 0x0d (not possible) */
84689 SQLITE_INTEGER, /* 0x0e (not possible) */
84690 SQLITE_NULL, /* 0x0f (not possible) */
84691 SQLITE_BLOB, /* 0x10 BLOB */
84692 SQLITE_NULL, /* 0x11 (not possible) */
84693 SQLITE_TEXT, /* 0x12 (not possible) */
84694 SQLITE_NULL, /* 0x13 (not possible) */
84695 SQLITE_INTEGER, /* 0x14 INTEGER + BLOB */
84696 SQLITE_NULL, /* 0x15 (not possible) */
84697 SQLITE_INTEGER, /* 0x16 (not possible) */
84698 SQLITE_NULL, /* 0x17 (not possible) */
84699 SQLITE_FLOAT, /* 0x18 FLOAT + BLOB */
84700 SQLITE_NULL, /* 0x19 (not possible) */
84701 SQLITE_FLOAT, /* 0x1a (not possible) */
84702 SQLITE_NULL, /* 0x1b (not possible) */
84703 SQLITE_INTEGER, /* 0x1c (not possible) */
84704 SQLITE_NULL, /* 0x1d (not possible) */
84705 SQLITE_INTEGER, /* 0x1e (not possible) */
84706 SQLITE_NULL, /* 0x1f (not possible) */
84707 SQLITE_FLOAT, /* 0x20 INTREAL */
84708 SQLITE_NULL, /* 0x21 (not possible) */
84709 SQLITE_TEXT, /* 0x22 INTREAL + TEXT */
84710 SQLITE_NULL, /* 0x23 (not possible) */
84711 SQLITE_FLOAT, /* 0x24 (not possible) */
84712 SQLITE_NULL, /* 0x25 (not possible) */
84713 SQLITE_FLOAT, /* 0x26 (not possible) */
84714 SQLITE_NULL, /* 0x27 (not possible) */
84715 SQLITE_FLOAT, /* 0x28 (not possible) */
84716 SQLITE_NULL, /* 0x29 (not possible) */
84717 SQLITE_FLOAT, /* 0x2a (not possible) */
84718 SQLITE_NULL, /* 0x2b (not possible) */
84719 SQLITE_FLOAT, /* 0x2c (not possible) */
84720 SQLITE_NULL, /* 0x2d (not possible) */
84721 SQLITE_FLOAT, /* 0x2e (not possible) */
84722 SQLITE_NULL, /* 0x2f (not possible) */
84723 SQLITE_BLOB, /* 0x30 (not possible) */
84724 SQLITE_NULL, /* 0x31 (not possible) */
84725 SQLITE_TEXT, /* 0x32 (not possible) */
84726 SQLITE_NULL, /* 0x33 (not possible) */
84727 SQLITE_FLOAT, /* 0x34 (not possible) */
84728 SQLITE_NULL, /* 0x35 (not possible) */
84729 SQLITE_FLOAT, /* 0x36 (not possible) */
84730 SQLITE_NULL, /* 0x37 (not possible) */
84731 SQLITE_FLOAT, /* 0x38 (not possible) */
84732 SQLITE_NULL, /* 0x39 (not possible) */
84733 SQLITE_FLOAT, /* 0x3a (not possible) */
84734 SQLITE_NULL, /* 0x3b (not possible) */
84735 SQLITE_FLOAT, /* 0x3c (not possible) */
84736 SQLITE_NULL, /* 0x3d (not possible) */
84737 SQLITE_FLOAT, /* 0x3e (not possible) */
84738 SQLITE_NULL, /* 0x3f (not possible) */
84739 };
84740#ifdef SQLITE_DEBUG
84741 {
84742 int eType = SQLITE_BLOB;
84743 if( pVal->flags & MEM_Null ){
84744 eType = SQLITE_NULL;
84745 }else if( pVal->flags & (MEM_Real|MEM_IntReal) ){
84746 eType = SQLITE_FLOAT;
84747 }else if( pVal->flags & MEM_Int ){
84748 eType = SQLITE_INTEGER;
84749 }else if( pVal->flags & MEM_Str ){
84750 eType = SQLITE_TEXT;
84751 }
84752 assert( eType == aType[pVal->flags&MEM_AffMask] );
84753 }
84754#endif
84755 return aType[pVal->flags&MEM_AffMask];
84756}
84757
84758/* Return true if a parameter to xUpdate represents an unchanged column */
84759SQLITE_API int sqlite3_value_nochange(sqlite3_value *pVal){
84760 return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);
84761}
84762
84763/* Return true if a parameter value originated from an sqlite3_bind() */
84764SQLITE_API int sqlite3_value_frombind(sqlite3_value *pVal){
84765 return (pVal->flags&MEM_FromBind)!=0;
84766}
84767
84768/* Make a copy of an sqlite3_value object
84769*/
84770SQLITE_API sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){
84771 sqlite3_value *pNew;
84772 if( pOrig==0 ) return 0;
84773 pNew = sqlite3_malloc( n: sizeof(*pNew) );
84774 if( pNew==0 ) return 0;
84775 memset(s: pNew, c: 0, n: sizeof(*pNew));
84776 memcpy(dest: pNew, src: pOrig, MEMCELLSIZE);
84777 pNew->flags &= ~MEM_Dyn;
84778 pNew->db = 0;
84779 if( pNew->flags&(MEM_Str|MEM_Blob) ){
84780 pNew->flags &= ~(MEM_Static|MEM_Dyn);
84781 pNew->flags |= MEM_Ephem;
84782 if( sqlite3VdbeMemMakeWriteable(pMem: pNew)!=SQLITE_OK ){
84783 sqlite3ValueFree(v: pNew);
84784 pNew = 0;
84785 }
84786 }
84787 return pNew;
84788}
84789
84790/* Destroy an sqlite3_value object previously obtained from
84791** sqlite3_value_dup().
84792*/
84793SQLITE_API void sqlite3_value_free(sqlite3_value *pOld){
84794 sqlite3ValueFree(v: pOld);
84795}
84796
84797
84798/**************************** sqlite3_result_ *******************************
84799** The following routines are used by user-defined functions to specify
84800** the function result.
84801**
84802** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the
84803** result as a string or blob. Appropriate errors are set if the string/blob
84804** is too big or if an OOM occurs.
84805**
84806** The invokeValueDestructor(P,X) routine invokes destructor function X()
84807** on value P is not going to be used and need to be destroyed.
84808*/
84809static void setResultStrOrError(
84810 sqlite3_context *pCtx, /* Function context */
84811 const char *z, /* String pointer */
84812 int n, /* Bytes in string, or negative */
84813 u8 enc, /* Encoding of z. 0 for BLOBs */
84814 void (*xDel)(void*) /* Destructor function */
84815){
84816 int rc = sqlite3VdbeMemSetStr(pMem: pCtx->pOut, z, n, enc, xDel);
84817 if( rc ){
84818 if( rc==SQLITE_TOOBIG ){
84819 sqlite3_result_error_toobig(pCtx);
84820 }else{
84821 /* The only errors possible from sqlite3VdbeMemSetStr are
84822 ** SQLITE_TOOBIG and SQLITE_NOMEM */
84823 assert( rc==SQLITE_NOMEM );
84824 sqlite3_result_error_nomem(pCtx);
84825 }
84826 }
84827}
84828static int invokeValueDestructor(
84829 const void *p, /* Value to destroy */
84830 void (*xDel)(void*), /* The destructor */
84831 sqlite3_context *pCtx /* Set a SQLITE_TOOBIG error if no NULL */
84832){
84833 assert( xDel!=SQLITE_DYNAMIC );
84834 if( xDel==0 ){
84835 /* noop */
84836 }else if( xDel==SQLITE_TRANSIENT ){
84837 /* noop */
84838 }else{
84839 xDel((void*)p);
84840 }
84841 sqlite3_result_error_toobig(pCtx);
84842 return SQLITE_TOOBIG;
84843}
84844SQLITE_API void sqlite3_result_blob(
84845 sqlite3_context *pCtx,
84846 const void *z,
84847 int n,
84848 void (*xDel)(void *)
84849){
84850 assert( n>=0 );
84851 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84852 setResultStrOrError(pCtx, z, n, enc: 0, xDel);
84853}
84854SQLITE_API void sqlite3_result_blob64(
84855 sqlite3_context *pCtx,
84856 const void *z,
84857 sqlite3_uint64 n,
84858 void (*xDel)(void *)
84859){
84860 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84861 assert( xDel!=SQLITE_DYNAMIC );
84862 if( n>0x7fffffff ){
84863 (void)invokeValueDestructor(p: z, xDel, pCtx);
84864 }else{
84865 setResultStrOrError(pCtx, z, n: (int)n, enc: 0, xDel);
84866 }
84867}
84868SQLITE_API void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
84869 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84870 sqlite3VdbeMemSetDouble(pMem: pCtx->pOut, val: rVal);
84871}
84872SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
84873 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84874 pCtx->isError = SQLITE_ERROR;
84875 sqlite3VdbeMemSetStr(pMem: pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
84876}
84877#ifndef SQLITE_OMIT_UTF16
84878SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
84879 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84880 pCtx->isError = SQLITE_ERROR;
84881 sqlite3VdbeMemSetStr(pMem: pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
84882}
84883#endif
84884SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
84885 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84886 sqlite3VdbeMemSetInt64(pMem: pCtx->pOut, val: (i64)iVal);
84887}
84888SQLITE_API void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
84889 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84890 sqlite3VdbeMemSetInt64(pMem: pCtx->pOut, val: iVal);
84891}
84892SQLITE_API void sqlite3_result_null(sqlite3_context *pCtx){
84893 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84894 sqlite3VdbeMemSetNull(pMem: pCtx->pOut);
84895}
84896SQLITE_API void sqlite3_result_pointer(
84897 sqlite3_context *pCtx,
84898 void *pPtr,
84899 const char *zPType,
84900 void (*xDestructor)(void*)
84901){
84902 Mem *pOut = pCtx->pOut;
84903 assert( sqlite3_mutex_held(pOut->db->mutex) );
84904 sqlite3VdbeMemRelease(p: pOut);
84905 pOut->flags = MEM_Null;
84906 sqlite3VdbeMemSetPointer(pMem: pOut, pPtr, zPType, xDestructor);
84907}
84908SQLITE_API void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){
84909 Mem *pOut = pCtx->pOut;
84910 assert( sqlite3_mutex_held(pOut->db->mutex) );
84911 pOut->eSubtype = eSubtype & 0xff;
84912 pOut->flags |= MEM_Subtype;
84913}
84914SQLITE_API void sqlite3_result_text(
84915 sqlite3_context *pCtx,
84916 const char *z,
84917 int n,
84918 void (*xDel)(void *)
84919){
84920 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84921 setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
84922}
84923SQLITE_API void sqlite3_result_text64(
84924 sqlite3_context *pCtx,
84925 const char *z,
84926 sqlite3_uint64 n,
84927 void (*xDel)(void *),
84928 unsigned char enc
84929){
84930 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84931 assert( xDel!=SQLITE_DYNAMIC );
84932 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
84933 if( n>0x7fffffff ){
84934 (void)invokeValueDestructor(p: z, xDel, pCtx);
84935 }else{
84936 setResultStrOrError(pCtx, z, n: (int)n, enc, xDel);
84937 }
84938}
84939#ifndef SQLITE_OMIT_UTF16
84940SQLITE_API void sqlite3_result_text16(
84941 sqlite3_context *pCtx,
84942 const void *z,
84943 int n,
84944 void (*xDel)(void *)
84945){
84946 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84947 setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
84948}
84949SQLITE_API void sqlite3_result_text16be(
84950 sqlite3_context *pCtx,
84951 const void *z,
84952 int n,
84953 void (*xDel)(void *)
84954){
84955 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84956 setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
84957}
84958SQLITE_API void sqlite3_result_text16le(
84959 sqlite3_context *pCtx,
84960 const void *z,
84961 int n,
84962 void (*xDel)(void *)
84963){
84964 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84965 setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
84966}
84967#endif /* SQLITE_OMIT_UTF16 */
84968SQLITE_API void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
84969 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84970 sqlite3VdbeMemCopy(pTo: pCtx->pOut, pFrom: pValue);
84971}
84972SQLITE_API void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
84973 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84974 sqlite3VdbeMemSetZeroBlob(pMem: pCtx->pOut, n);
84975}
84976SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){
84977 Mem *pOut = pCtx->pOut;
84978 assert( sqlite3_mutex_held(pOut->db->mutex) );
84979 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){
84980 return SQLITE_TOOBIG;
84981 }
84982#ifndef SQLITE_OMIT_INCRBLOB
84983 sqlite3VdbeMemSetZeroBlob(pMem: pCtx->pOut, n: (int)n);
84984 return SQLITE_OK;
84985#else
84986 return sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
84987#endif
84988}
84989SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
84990 pCtx->isError = errCode ? errCode : -1;
84991#ifdef SQLITE_DEBUG
84992 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
84993#endif
84994 if( pCtx->pOut->flags & MEM_Null ){
84995 sqlite3VdbeMemSetStr(pMem: pCtx->pOut, z: sqlite3ErrStr(errCode), n: -1,
84996 SQLITE_UTF8, SQLITE_STATIC);
84997 }
84998}
84999
85000/* Force an SQLITE_TOOBIG error. */
85001SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
85002 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
85003 pCtx->isError = SQLITE_TOOBIG;
85004 sqlite3VdbeMemSetStr(pMem: pCtx->pOut, z: "string or blob too big", n: -1,
85005 SQLITE_UTF8, SQLITE_STATIC);
85006}
85007
85008/* An SQLITE_NOMEM error. */
85009SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
85010 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
85011 sqlite3VdbeMemSetNull(pMem: pCtx->pOut);
85012 pCtx->isError = SQLITE_NOMEM_BKPT;
85013 sqlite3OomFault(db: pCtx->pOut->db);
85014}
85015
85016#ifndef SQLITE_UNTESTABLE
85017/* Force the INT64 value currently stored as the result to be
85018** a MEM_IntReal value. See the SQLITE_TESTCTRL_RESULT_INTREAL
85019** test-control.
85020*/
85021SQLITE_PRIVATE void sqlite3ResultIntReal(sqlite3_context *pCtx){
85022 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
85023 if( pCtx->pOut->flags & MEM_Int ){
85024 pCtx->pOut->flags &= ~MEM_Int;
85025 pCtx->pOut->flags |= MEM_IntReal;
85026 }
85027}
85028#endif
85029
85030
85031/*
85032** This function is called after a transaction has been committed. It
85033** invokes callbacks registered with sqlite3_wal_hook() as required.
85034*/
85035static int doWalCallbacks(sqlite3 *db){
85036 int rc = SQLITE_OK;
85037#ifndef SQLITE_OMIT_WAL
85038 int i;
85039 for(i=0; i<db->nDb; i++){
85040 Btree *pBt = db->aDb[i].pBt;
85041 if( pBt ){
85042 int nEntry;
85043 sqlite3BtreeEnter(p: pBt);
85044 nEntry = sqlite3PagerWalCallback(pPager: sqlite3BtreePager(p: pBt));
85045 sqlite3BtreeLeave(p: pBt);
85046 if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK ){
85047 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
85048 }
85049 }
85050 }
85051#endif
85052 return rc;
85053}
85054
85055
85056/*
85057** Execute the statement pStmt, either until a row of data is ready, the
85058** statement is completely executed or an error occurs.
85059**
85060** This routine implements the bulk of the logic behind the sqlite_step()
85061** API. The only thing omitted is the automatic recompile if a
85062** schema change has occurred. That detail is handled by the
85063** outer sqlite3_step() wrapper procedure.
85064*/
85065static int sqlite3Step(Vdbe *p){
85066 sqlite3 *db;
85067 int rc;
85068
85069 assert(p);
85070 if( p->iVdbeMagic!=VDBE_MAGIC_RUN ){
85071 /* We used to require that sqlite3_reset() be called before retrying
85072 ** sqlite3_step() after any error or after SQLITE_DONE. But beginning
85073 ** with version 3.7.0, we changed this so that sqlite3_reset() would
85074 ** be called automatically instead of throwing the SQLITE_MISUSE error.
85075 ** This "automatic-reset" change is not technically an incompatibility,
85076 ** since any application that receives an SQLITE_MISUSE is broken by
85077 ** definition.
85078 **
85079 ** Nevertheless, some published applications that were originally written
85080 ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
85081 ** returns, and those were broken by the automatic-reset change. As a
85082 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
85083 ** legacy behavior of returning SQLITE_MISUSE for cases where the
85084 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
85085 ** or SQLITE_BUSY error.
85086 */
85087#ifdef SQLITE_OMIT_AUTORESET
85088 if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){
85089 sqlite3_reset((sqlite3_stmt*)p);
85090 }else{
85091 return SQLITE_MISUSE_BKPT;
85092 }
85093#else
85094 sqlite3_reset(pStmt: (sqlite3_stmt*)p);
85095#endif
85096 }
85097
85098 /* Check that malloc() has not failed. If it has, return early. */
85099 db = p->db;
85100 if( db->mallocFailed ){
85101 p->rc = SQLITE_NOMEM;
85102 return SQLITE_NOMEM_BKPT;
85103 }
85104
85105 if( p->pc<0 && p->expired ){
85106 p->rc = SQLITE_SCHEMA;
85107 rc = SQLITE_ERROR;
85108 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){
85109 /* If this statement was prepared using saved SQL and an
85110 ** error has occurred, then return the error code in p->rc to the
85111 ** caller. Set the error code in the database handle to the same value.
85112 */
85113 rc = sqlite3VdbeTransferError(p);
85114 }
85115 goto end_of_step;
85116 }
85117 if( p->pc<0 ){
85118 /* If there are no other statements currently running, then
85119 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt
85120 ** from interrupting a statement that has not yet started.
85121 */
85122 if( db->nVdbeActive==0 ){
85123 AtomicStore(&db->u1.isInterrupted, 0);
85124 }
85125
85126 assert( db->nVdbeWrite>0 || db->autoCommit==0
85127 || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
85128 );
85129
85130#ifndef SQLITE_OMIT_TRACE
85131 if( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0
85132 && !db->init.busy && p->zSql ){
85133 sqlite3OsCurrentTimeInt64(pVfs: db->pVfs, pTimeOut: &p->startTime);
85134 }else{
85135 assert( p->startTime==0 );
85136 }
85137#endif
85138
85139 db->nVdbeActive++;
85140 if( p->readOnly==0 ) db->nVdbeWrite++;
85141 if( p->bIsReader ) db->nVdbeRead++;
85142 p->pc = 0;
85143 }
85144#ifdef SQLITE_DEBUG
85145 p->rcApp = SQLITE_OK;
85146#endif
85147#ifndef SQLITE_OMIT_EXPLAIN
85148 if( p->explain ){
85149 rc = sqlite3VdbeList(p);
85150 }else
85151#endif /* SQLITE_OMIT_EXPLAIN */
85152 {
85153 db->nVdbeExec++;
85154 rc = sqlite3VdbeExec(p);
85155 db->nVdbeExec--;
85156 }
85157
85158 if( rc!=SQLITE_ROW ){
85159#ifndef SQLITE_OMIT_TRACE
85160 /* If the statement completed successfully, invoke the profile callback */
85161 checkProfileCallback(db, p);
85162#endif
85163
85164 if( rc==SQLITE_DONE && db->autoCommit ){
85165 assert( p->rc==SQLITE_OK );
85166 p->rc = doWalCallbacks(db);
85167 if( p->rc!=SQLITE_OK ){
85168 rc = SQLITE_ERROR;
85169 }
85170 }else if( rc!=SQLITE_DONE && (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){
85171 /* If this statement was prepared using saved SQL and an
85172 ** error has occurred, then return the error code in p->rc to the
85173 ** caller. Set the error code in the database handle to the same value.
85174 */
85175 rc = sqlite3VdbeTransferError(p);
85176 }
85177 }
85178
85179 db->errCode = rc;
85180 if( SQLITE_NOMEM==sqlite3ApiExit(db: p->db, rc: p->rc) ){
85181 p->rc = SQLITE_NOMEM_BKPT;
85182 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ) rc = p->rc;
85183 }
85184end_of_step:
85185 /* There are only a limited number of result codes allowed from the
85186 ** statements prepared using the legacy sqlite3_prepare() interface */
85187 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0
85188 || rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR
85189 || (rc&0xff)==SQLITE_BUSY || rc==SQLITE_MISUSE
85190 );
85191 return (rc&db->errMask);
85192}
85193
85194/*
85195** This is the top-level implementation of sqlite3_step(). Call
85196** sqlite3Step() to do most of the work. If a schema error occurs,
85197** call sqlite3Reprepare() and try again.
85198*/
85199SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
85200 int rc = SQLITE_OK; /* Result from sqlite3Step() */
85201 Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */
85202 int cnt = 0; /* Counter to prevent infinite loop of reprepares */
85203 sqlite3 *db; /* The database connection */
85204
85205 if( vdbeSafetyNotNull(p: v) ){
85206 return SQLITE_MISUSE_BKPT;
85207 }
85208 db = v->db;
85209 sqlite3_mutex_enter(p: db->mutex);
85210 v->doingRerun = 0;
85211 while( (rc = sqlite3Step(p: v))==SQLITE_SCHEMA
85212 && cnt++ < SQLITE_MAX_SCHEMA_RETRY ){
85213 int savedPc = v->pc;
85214 rc = sqlite3Reprepare(v);
85215 if( rc!=SQLITE_OK ){
85216 /* This case occurs after failing to recompile an sql statement.
85217 ** The error message from the SQL compiler has already been loaded
85218 ** into the database handle. This block copies the error message
85219 ** from the database handle into the statement and sets the statement
85220 ** program counter to 0 to ensure that when the statement is
85221 ** finalized or reset the parser error message is available via
85222 ** sqlite3_errmsg() and sqlite3_errcode().
85223 */
85224 const char *zErr = (const char *)sqlite3_value_text(pVal: db->pErr);
85225 sqlite3DbFree(db, p: v->zErrMsg);
85226 if( !db->mallocFailed ){
85227 v->zErrMsg = sqlite3DbStrDup(db, z: zErr);
85228 v->rc = rc = sqlite3ApiExit(db, rc);
85229 } else {
85230 v->zErrMsg = 0;
85231 v->rc = rc = SQLITE_NOMEM_BKPT;
85232 }
85233 break;
85234 }
85235 sqlite3_reset(pStmt);
85236 if( savedPc>=0 ) v->doingRerun = 1;
85237 assert( v->expired==0 );
85238 }
85239 sqlite3_mutex_leave(p: db->mutex);
85240 return rc;
85241}
85242
85243
85244/*
85245** Extract the user data from a sqlite3_context structure and return a
85246** pointer to it.
85247*/
85248SQLITE_API void *sqlite3_user_data(sqlite3_context *p){
85249 assert( p && p->pFunc );
85250 return p->pFunc->pUserData;
85251}
85252
85253/*
85254** Extract the user data from a sqlite3_context structure and return a
85255** pointer to it.
85256**
85257** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
85258** returns a copy of the pointer to the database connection (the 1st
85259** parameter) of the sqlite3_create_function() and
85260** sqlite3_create_function16() routines that originally registered the
85261** application defined function.
85262*/
85263SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
85264 assert( p && p->pOut );
85265 return p->pOut->db;
85266}
85267
85268/*
85269** If this routine is invoked from within an xColumn method of a virtual
85270** table, then it returns true if and only if the the call is during an
85271** UPDATE operation and the value of the column will not be modified
85272** by the UPDATE.
85273**
85274** If this routine is called from any context other than within the
85275** xColumn method of a virtual table, then the return value is meaningless
85276** and arbitrary.
85277**
85278** Virtual table implements might use this routine to optimize their
85279** performance by substituting a NULL result, or some other light-weight
85280** value, as a signal to the xUpdate routine that the column is unchanged.
85281*/
85282SQLITE_API int sqlite3_vtab_nochange(sqlite3_context *p){
85283 assert( p );
85284 return sqlite3_value_nochange(pVal: p->pOut);
85285}
85286
85287/*
85288** Return the current time for a statement. If the current time
85289** is requested more than once within the same run of a single prepared
85290** statement, the exact same time is returned for each invocation regardless
85291** of the amount of time that elapses between invocations. In other words,
85292** the time returned is always the time of the first call.
85293*/
85294SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
85295 int rc;
85296#ifndef SQLITE_ENABLE_STAT4
85297 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
85298 assert( p->pVdbe!=0 );
85299#else
85300 sqlite3_int64 iTime = 0;
85301 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
85302#endif
85303 if( *piTime==0 ){
85304 rc = sqlite3OsCurrentTimeInt64(pVfs: p->pOut->db->pVfs, pTimeOut: piTime);
85305 if( rc ) *piTime = 0;
85306 }
85307 return *piTime;
85308}
85309
85310/*
85311** Create a new aggregate context for p and return a pointer to
85312** its pMem->z element.
85313*/
85314static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){
85315 Mem *pMem = p->pMem;
85316 assert( (pMem->flags & MEM_Agg)==0 );
85317 if( nByte<=0 ){
85318 sqlite3VdbeMemSetNull(pMem);
85319 pMem->z = 0;
85320 }else{
85321 sqlite3VdbeMemClearAndResize(pMem, szNew: nByte);
85322 pMem->flags = MEM_Agg;
85323 pMem->u.pDef = p->pFunc;
85324 if( pMem->z ){
85325 memset(s: pMem->z, c: 0, n: nByte);
85326 }
85327 }
85328 return (void*)pMem->z;
85329}
85330
85331/*
85332** Allocate or return the aggregate context for a user function. A new
85333** context is allocated on the first call. Subsequent calls return the
85334** same context that was returned on prior calls.
85335*/
85336SQLITE_API void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
85337 assert( p && p->pFunc && p->pFunc->xFinalize );
85338 assert( sqlite3_mutex_held(p->pOut->db->mutex) );
85339 testcase( nByte<0 );
85340 if( (p->pMem->flags & MEM_Agg)==0 ){
85341 return createAggContext(p, nByte);
85342 }else{
85343 return (void*)p->pMem->z;
85344 }
85345}
85346
85347/*
85348** Return the auxiliary data pointer, if any, for the iArg'th argument to
85349** the user-function defined by pCtx.
85350**
85351** The left-most argument is 0.
85352**
85353** Undocumented behavior: If iArg is negative then access a cache of
85354** auxiliary data pointers that is available to all functions within a
85355** single prepared statement. The iArg values must match.
85356*/
85357SQLITE_API void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
85358 AuxData *pAuxData;
85359
85360 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
85361#if SQLITE_ENABLE_STAT4
85362 if( pCtx->pVdbe==0 ) return 0;
85363#else
85364 assert( pCtx->pVdbe!=0 );
85365#endif
85366 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
85367 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
85368 return pAuxData->pAux;
85369 }
85370 }
85371 return 0;
85372}
85373
85374/*
85375** Set the auxiliary data pointer and delete function, for the iArg'th
85376** argument to the user-function defined by pCtx. Any previous value is
85377** deleted by calling the delete function specified when it was set.
85378**
85379** The left-most argument is 0.
85380**
85381** Undocumented behavior: If iArg is negative then make the data available
85382** to all functions within the current prepared statement using iArg as an
85383** access code.
85384*/
85385SQLITE_API void sqlite3_set_auxdata(
85386 sqlite3_context *pCtx,
85387 int iArg,
85388 void *pAux,
85389 void (*xDelete)(void*)
85390){
85391 AuxData *pAuxData;
85392 Vdbe *pVdbe = pCtx->pVdbe;
85393
85394 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
85395#ifdef SQLITE_ENABLE_STAT4
85396 if( pVdbe==0 ) goto failed;
85397#else
85398 assert( pVdbe!=0 );
85399#endif
85400
85401 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
85402 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
85403 break;
85404 }
85405 }
85406 if( pAuxData==0 ){
85407 pAuxData = sqlite3DbMallocZero(db: pVdbe->db, n: sizeof(AuxData));
85408 if( !pAuxData ) goto failed;
85409 pAuxData->iAuxOp = pCtx->iOp;
85410 pAuxData->iAuxArg = iArg;
85411 pAuxData->pNextAux = pVdbe->pAuxData;
85412 pVdbe->pAuxData = pAuxData;
85413 if( pCtx->isError==0 ) pCtx->isError = -1;
85414 }else if( pAuxData->xDeleteAux ){
85415 pAuxData->xDeleteAux(pAuxData->pAux);
85416 }
85417
85418 pAuxData->pAux = pAux;
85419 pAuxData->xDeleteAux = xDelete;
85420 return;
85421
85422failed:
85423 if( xDelete ){
85424 xDelete(pAux);
85425 }
85426}
85427
85428#ifndef SQLITE_OMIT_DEPRECATED
85429/*
85430** Return the number of times the Step function of an aggregate has been
85431** called.
85432**
85433** This function is deprecated. Do not use it for new code. It is
85434** provide only to avoid breaking legacy code. New aggregate function
85435** implementations should keep their own counts within their aggregate
85436** context.
85437*/
85438SQLITE_API int sqlite3_aggregate_count(sqlite3_context *p){
85439 assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize );
85440 return p->pMem->n;
85441}
85442#endif
85443
85444/*
85445** Return the number of columns in the result set for the statement pStmt.
85446*/
85447SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt){
85448 Vdbe *pVm = (Vdbe *)pStmt;
85449 return pVm ? pVm->nResColumn : 0;
85450}
85451
85452/*
85453** Return the number of values available from the current row of the
85454** currently executing statement pStmt.
85455*/
85456SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt){
85457 Vdbe *pVm = (Vdbe *)pStmt;
85458 if( pVm==0 || pVm->pResultSet==0 ) return 0;
85459 return pVm->nResColumn;
85460}
85461
85462/*
85463** Return a pointer to static memory containing an SQL NULL value.
85464*/
85465static const Mem *columnNullValue(void){
85466 /* Even though the Mem structure contains an element
85467 ** of type i64, on certain architectures (x86) with certain compiler
85468 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
85469 ** instead of an 8-byte one. This all works fine, except that when
85470 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
85471 ** that a Mem structure is located on an 8-byte boundary. To prevent
85472 ** these assert()s from failing, when building with SQLITE_DEBUG defined
85473 ** using gcc, we force nullMem to be 8-byte aligned using the magical
85474 ** __attribute__((aligned(8))) macro. */
85475 static const Mem nullMem
85476#if defined(SQLITE_DEBUG) && defined(__GNUC__)
85477 __attribute__((aligned(8)))
85478#endif
85479 = {
85480 /* .u = */ {0},
85481 /* .flags = */ (u16)MEM_Null,
85482 /* .enc = */ (u8)0,
85483 /* .eSubtype = */ (u8)0,
85484 /* .n = */ (int)0,
85485 /* .z = */ (char*)0,
85486 /* .zMalloc = */ (char*)0,
85487 /* .szMalloc = */ (int)0,
85488 /* .uTemp = */ (u32)0,
85489 /* .db = */ (sqlite3*)0,
85490 /* .xDel = */ (void(*)(void*))0,
85491#ifdef SQLITE_DEBUG
85492 /* .pScopyFrom = */ (Mem*)0,
85493 /* .mScopyFlags= */ 0,
85494#endif
85495 };
85496 return &nullMem;
85497}
85498
85499/*
85500** Check to see if column iCol of the given statement is valid. If
85501** it is, return a pointer to the Mem for the value of that column.
85502** If iCol is not valid, return a pointer to a Mem which has a value
85503** of NULL.
85504*/
85505static Mem *columnMem(sqlite3_stmt *pStmt, int i){
85506 Vdbe *pVm;
85507 Mem *pOut;
85508
85509 pVm = (Vdbe *)pStmt;
85510 if( pVm==0 ) return (Mem*)columnNullValue();
85511 assert( pVm->db );
85512 sqlite3_mutex_enter(p: pVm->db->mutex);
85513 if( pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
85514 pOut = &pVm->pResultSet[i];
85515 }else{
85516 sqlite3Error(db: pVm->db, SQLITE_RANGE);
85517 pOut = (Mem*)columnNullValue();
85518 }
85519 return pOut;
85520}
85521
85522/*
85523** This function is called after invoking an sqlite3_value_XXX function on a
85524** column value (i.e. a value returned by evaluating an SQL expression in the
85525** select list of a SELECT statement) that may cause a malloc() failure. If
85526** malloc() has failed, the threads mallocFailed flag is cleared and the result
85527** code of statement pStmt set to SQLITE_NOMEM.
85528**
85529** Specifically, this is called from within:
85530**
85531** sqlite3_column_int()
85532** sqlite3_column_int64()
85533** sqlite3_column_text()
85534** sqlite3_column_text16()
85535** sqlite3_column_real()
85536** sqlite3_column_bytes()
85537** sqlite3_column_bytes16()
85538** sqiite3_column_blob()
85539*/
85540static void columnMallocFailure(sqlite3_stmt *pStmt)
85541{
85542 /* If malloc() failed during an encoding conversion within an
85543 ** sqlite3_column_XXX API, then set the return code of the statement to
85544 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
85545 ** and _finalize() will return NOMEM.
85546 */
85547 Vdbe *p = (Vdbe *)pStmt;
85548 if( p ){
85549 assert( p->db!=0 );
85550 assert( sqlite3_mutex_held(p->db->mutex) );
85551 p->rc = sqlite3ApiExit(db: p->db, rc: p->rc);
85552 sqlite3_mutex_leave(p: p->db->mutex);
85553 }
85554}
85555
85556/**************************** sqlite3_column_ *******************************
85557** The following routines are used to access elements of the current row
85558** in the result set.
85559*/
85560SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
85561 const void *val;
85562 val = sqlite3_value_blob( pVal: columnMem(pStmt,i) );
85563 /* Even though there is no encoding conversion, value_blob() might
85564 ** need to call malloc() to expand the result of a zeroblob()
85565 ** expression.
85566 */
85567 columnMallocFailure(pStmt);
85568 return val;
85569}
85570SQLITE_API int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
85571 int val = sqlite3_value_bytes( pVal: columnMem(pStmt,i) );
85572 columnMallocFailure(pStmt);
85573 return val;
85574}
85575SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
85576 int val = sqlite3_value_bytes16( pVal: columnMem(pStmt,i) );
85577 columnMallocFailure(pStmt);
85578 return val;
85579}
85580SQLITE_API double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
85581 double val = sqlite3_value_double( pVal: columnMem(pStmt,i) );
85582 columnMallocFailure(pStmt);
85583 return val;
85584}
85585SQLITE_API int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
85586 int val = sqlite3_value_int( pVal: columnMem(pStmt,i) );
85587 columnMallocFailure(pStmt);
85588 return val;
85589}
85590SQLITE_API sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
85591 sqlite_int64 val = sqlite3_value_int64( pVal: columnMem(pStmt,i) );
85592 columnMallocFailure(pStmt);
85593 return val;
85594}
85595SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
85596 const unsigned char *val = sqlite3_value_text( pVal: columnMem(pStmt,i) );
85597 columnMallocFailure(pStmt);
85598 return val;
85599}
85600SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
85601 Mem *pOut = columnMem(pStmt, i);
85602 if( pOut->flags&MEM_Static ){
85603 pOut->flags &= ~MEM_Static;
85604 pOut->flags |= MEM_Ephem;
85605 }
85606 columnMallocFailure(pStmt);
85607 return (sqlite3_value *)pOut;
85608}
85609#ifndef SQLITE_OMIT_UTF16
85610SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
85611 const void *val = sqlite3_value_text16( pVal: columnMem(pStmt,i) );
85612 columnMallocFailure(pStmt);
85613 return val;
85614}
85615#endif /* SQLITE_OMIT_UTF16 */
85616SQLITE_API int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
85617 int iType = sqlite3_value_type( pVal: columnMem(pStmt,i) );
85618 columnMallocFailure(pStmt);
85619 return iType;
85620}
85621
85622/*
85623** Convert the N-th element of pStmt->pColName[] into a string using
85624** xFunc() then return that string. If N is out of range, return 0.
85625**
85626** There are up to 5 names for each column. useType determines which
85627** name is returned. Here are the names:
85628**
85629** 0 The column name as it should be displayed for output
85630** 1 The datatype name for the column
85631** 2 The name of the database that the column derives from
85632** 3 The name of the table that the column derives from
85633** 4 The name of the table column that the result column derives from
85634**
85635** If the result is not a simple column reference (if it is an expression
85636** or a constant) then useTypes 2, 3, and 4 return NULL.
85637*/
85638static const void *columnName(
85639 sqlite3_stmt *pStmt, /* The statement */
85640 int N, /* Which column to get the name for */
85641 int useUtf16, /* True to return the name as UTF16 */
85642 int useType /* What type of name */
85643){
85644 const void *ret;
85645 Vdbe *p;
85646 int n;
85647 sqlite3 *db;
85648#ifdef SQLITE_ENABLE_API_ARMOR
85649 if( pStmt==0 ){
85650 (void)SQLITE_MISUSE_BKPT;
85651 return 0;
85652 }
85653#endif
85654 ret = 0;
85655 p = (Vdbe *)pStmt;
85656 db = p->db;
85657 assert( db!=0 );
85658 n = sqlite3_column_count(pStmt);
85659 if( N<n && N>=0 ){
85660 N += useType*n;
85661 sqlite3_mutex_enter(p: db->mutex);
85662 assert( db->mallocFailed==0 );
85663#ifndef SQLITE_OMIT_UTF16
85664 if( useUtf16 ){
85665 ret = sqlite3_value_text16(pVal: (sqlite3_value*)&p->aColName[N]);
85666 }else
85667#endif
85668 {
85669 ret = sqlite3_value_text(pVal: (sqlite3_value*)&p->aColName[N]);
85670 }
85671 /* A malloc may have failed inside of the _text() call. If this
85672 ** is the case, clear the mallocFailed flag and return NULL.
85673 */
85674 if( db->mallocFailed ){
85675 sqlite3OomClear(db);
85676 ret = 0;
85677 }
85678 sqlite3_mutex_leave(p: db->mutex);
85679 }
85680 return ret;
85681}
85682
85683/*
85684** Return the name of the Nth column of the result set returned by SQL
85685** statement pStmt.
85686*/
85687SQLITE_API const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
85688 return columnName(pStmt, N, useUtf16: 0, COLNAME_NAME);
85689}
85690#ifndef SQLITE_OMIT_UTF16
85691SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
85692 return columnName(pStmt, N, useUtf16: 1, COLNAME_NAME);
85693}
85694#endif
85695
85696/*
85697** Constraint: If you have ENABLE_COLUMN_METADATA then you must
85698** not define OMIT_DECLTYPE.
85699*/
85700#if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
85701# error "Must not define both SQLITE_OMIT_DECLTYPE \
85702 and SQLITE_ENABLE_COLUMN_METADATA"
85703#endif
85704
85705#ifndef SQLITE_OMIT_DECLTYPE
85706/*
85707** Return the column declaration type (if applicable) of the 'i'th column
85708** of the result set of SQL statement pStmt.
85709*/
85710SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
85711 return columnName(pStmt, N, useUtf16: 0, COLNAME_DECLTYPE);
85712}
85713#ifndef SQLITE_OMIT_UTF16
85714SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
85715 return columnName(pStmt, N, useUtf16: 1, COLNAME_DECLTYPE);
85716}
85717#endif /* SQLITE_OMIT_UTF16 */
85718#endif /* SQLITE_OMIT_DECLTYPE */
85719
85720#ifdef SQLITE_ENABLE_COLUMN_METADATA
85721/*
85722** Return the name of the database from which a result column derives.
85723** NULL is returned if the result column is an expression or constant or
85724** anything else which is not an unambiguous reference to a database column.
85725*/
85726SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
85727 return columnName(pStmt, N, useUtf16: 0, COLNAME_DATABASE);
85728}
85729#ifndef SQLITE_OMIT_UTF16
85730SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
85731 return columnName(pStmt, N, useUtf16: 1, COLNAME_DATABASE);
85732}
85733#endif /* SQLITE_OMIT_UTF16 */
85734
85735/*
85736** Return the name of the table from which a result column derives.
85737** NULL is returned if the result column is an expression or constant or
85738** anything else which is not an unambiguous reference to a database column.
85739*/
85740SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
85741 return columnName(pStmt, N, useUtf16: 0, COLNAME_TABLE);
85742}
85743#ifndef SQLITE_OMIT_UTF16
85744SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
85745 return columnName(pStmt, N, useUtf16: 1, COLNAME_TABLE);
85746}
85747#endif /* SQLITE_OMIT_UTF16 */
85748
85749/*
85750** Return the name of the table column from which a result column derives.
85751** NULL is returned if the result column is an expression or constant or
85752** anything else which is not an unambiguous reference to a database column.
85753*/
85754SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
85755 return columnName(pStmt, N, useUtf16: 0, COLNAME_COLUMN);
85756}
85757#ifndef SQLITE_OMIT_UTF16
85758SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
85759 return columnName(pStmt, N, useUtf16: 1, COLNAME_COLUMN);
85760}
85761#endif /* SQLITE_OMIT_UTF16 */
85762#endif /* SQLITE_ENABLE_COLUMN_METADATA */
85763
85764
85765/******************************* sqlite3_bind_ ***************************
85766**
85767** Routines used to attach values to wildcards in a compiled SQL statement.
85768*/
85769/*
85770** Unbind the value bound to variable i in virtual machine p. This is the
85771** the same as binding a NULL value to the column. If the "i" parameter is
85772** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
85773**
85774** A successful evaluation of this routine acquires the mutex on p.
85775** the mutex is released if any kind of error occurs.
85776**
85777** The error code stored in database p->db is overwritten with the return
85778** value in any case.
85779*/
85780static int vdbeUnbind(Vdbe *p, int i){
85781 Mem *pVar;
85782 if( vdbeSafetyNotNull(p) ){
85783 return SQLITE_MISUSE_BKPT;
85784 }
85785 sqlite3_mutex_enter(p: p->db->mutex);
85786 if( p->iVdbeMagic!=VDBE_MAGIC_RUN || p->pc>=0 ){
85787 sqlite3Error(db: p->db, SQLITE_MISUSE);
85788 sqlite3_mutex_leave(p: p->db->mutex);
85789 sqlite3_log(SQLITE_MISUSE,
85790 zFormat: "bind on a busy prepared statement: [%s]", p->zSql);
85791 return SQLITE_MISUSE_BKPT;
85792 }
85793 if( i<1 || i>p->nVar ){
85794 sqlite3Error(db: p->db, SQLITE_RANGE);
85795 sqlite3_mutex_leave(p: p->db->mutex);
85796 return SQLITE_RANGE;
85797 }
85798 i--;
85799 pVar = &p->aVar[i];
85800 sqlite3VdbeMemRelease(p: pVar);
85801 pVar->flags = MEM_Null;
85802 p->db->errCode = SQLITE_OK;
85803
85804 /* If the bit corresponding to this variable in Vdbe.expmask is set, then
85805 ** binding a new value to this variable invalidates the current query plan.
85806 **
85807 ** IMPLEMENTATION-OF: R-57496-20354 If the specific value bound to a host
85808 ** parameter in the WHERE clause might influence the choice of query plan
85809 ** for a statement, then the statement will be automatically recompiled,
85810 ** as if there had been a schema change, on the first sqlite3_step() call
85811 ** following any change to the bindings of that parameter.
85812 */
85813 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
85814 if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){
85815 p->expired = 1;
85816 }
85817 return SQLITE_OK;
85818}
85819
85820/*
85821** Bind a text or BLOB value.
85822*/
85823static int bindText(
85824 sqlite3_stmt *pStmt, /* The statement to bind against */
85825 int i, /* Index of the parameter to bind */
85826 const void *zData, /* Pointer to the data to be bound */
85827 i64 nData, /* Number of bytes of data to be bound */
85828 void (*xDel)(void*), /* Destructor for the data */
85829 u8 encoding /* Encoding for the data */
85830){
85831 Vdbe *p = (Vdbe *)pStmt;
85832 Mem *pVar;
85833 int rc;
85834
85835 rc = vdbeUnbind(p, i);
85836 if( rc==SQLITE_OK ){
85837 if( zData!=0 ){
85838 pVar = &p->aVar[i-1];
85839 rc = sqlite3VdbeMemSetStr(pMem: pVar, z: zData, n: nData, enc: encoding, xDel);
85840 if( rc==SQLITE_OK && encoding!=0 ){
85841 rc = sqlite3VdbeChangeEncoding(pMem: pVar, ENC(p->db));
85842 }
85843 if( rc ){
85844 sqlite3Error(db: p->db, err_code: rc);
85845 rc = sqlite3ApiExit(db: p->db, rc);
85846 }
85847 }
85848 sqlite3_mutex_leave(p: p->db->mutex);
85849 }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){
85850 xDel((void*)zData);
85851 }
85852 return rc;
85853}
85854
85855
85856/*
85857** Bind a blob value to an SQL statement variable.
85858*/
85859SQLITE_API int sqlite3_bind_blob(
85860 sqlite3_stmt *pStmt,
85861 int i,
85862 const void *zData,
85863 int nData,
85864 void (*xDel)(void*)
85865){
85866#ifdef SQLITE_ENABLE_API_ARMOR
85867 if( nData<0 ) return SQLITE_MISUSE_BKPT;
85868#endif
85869 return bindText(pStmt, i, zData, nData, xDel, encoding: 0);
85870}
85871SQLITE_API int sqlite3_bind_blob64(
85872 sqlite3_stmt *pStmt,
85873 int i,
85874 const void *zData,
85875 sqlite3_uint64 nData,
85876 void (*xDel)(void*)
85877){
85878 assert( xDel!=SQLITE_DYNAMIC );
85879 return bindText(pStmt, i, zData, nData, xDel, encoding: 0);
85880}
85881SQLITE_API int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
85882 int rc;
85883 Vdbe *p = (Vdbe *)pStmt;
85884 rc = vdbeUnbind(p, i);
85885 if( rc==SQLITE_OK ){
85886 sqlite3VdbeMemSetDouble(pMem: &p->aVar[i-1], val: rValue);
85887 sqlite3_mutex_leave(p: p->db->mutex);
85888 }
85889 return rc;
85890}
85891SQLITE_API int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
85892 return sqlite3_bind_int64(p, i, (i64)iValue);
85893}
85894SQLITE_API int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
85895 int rc;
85896 Vdbe *p = (Vdbe *)pStmt;
85897 rc = vdbeUnbind(p, i);
85898 if( rc==SQLITE_OK ){
85899 sqlite3VdbeMemSetInt64(pMem: &p->aVar[i-1], val: iValue);
85900 sqlite3_mutex_leave(p: p->db->mutex);
85901 }
85902 return rc;
85903}
85904SQLITE_API int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
85905 int rc;
85906 Vdbe *p = (Vdbe*)pStmt;
85907 rc = vdbeUnbind(p, i);
85908 if( rc==SQLITE_OK ){
85909 sqlite3_mutex_leave(p: p->db->mutex);
85910 }
85911 return rc;
85912}
85913SQLITE_API int sqlite3_bind_pointer(
85914 sqlite3_stmt *pStmt,
85915 int i,
85916 void *pPtr,
85917 const char *zPTtype,
85918 void (*xDestructor)(void*)
85919){
85920 int rc;
85921 Vdbe *p = (Vdbe*)pStmt;
85922 rc = vdbeUnbind(p, i);
85923 if( rc==SQLITE_OK ){
85924 sqlite3VdbeMemSetPointer(pMem: &p->aVar[i-1], pPtr, zPType: zPTtype, xDestructor);
85925 sqlite3_mutex_leave(p: p->db->mutex);
85926 }else if( xDestructor ){
85927 xDestructor(pPtr);
85928 }
85929 return rc;
85930}
85931SQLITE_API int sqlite3_bind_text(
85932 sqlite3_stmt *pStmt,
85933 int i,
85934 const char *zData,
85935 int nData,
85936 void (*xDel)(void*)
85937){
85938 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
85939}
85940SQLITE_API int sqlite3_bind_text64(
85941 sqlite3_stmt *pStmt,
85942 int i,
85943 const char *zData,
85944 sqlite3_uint64 nData,
85945 void (*xDel)(void*),
85946 unsigned char enc
85947){
85948 assert( xDel!=SQLITE_DYNAMIC );
85949 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
85950 return bindText(pStmt, i, zData, nData, xDel, encoding: enc);
85951}
85952#ifndef SQLITE_OMIT_UTF16
85953SQLITE_API int sqlite3_bind_text16(
85954 sqlite3_stmt *pStmt,
85955 int i,
85956 const void *zData,
85957 int nData,
85958 void (*xDel)(void*)
85959){
85960 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
85961}
85962#endif /* SQLITE_OMIT_UTF16 */
85963SQLITE_API int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
85964 int rc;
85965 switch( sqlite3_value_type(pVal: (sqlite3_value*)pValue) ){
85966 case SQLITE_INTEGER: {
85967 rc = sqlite3_bind_int64(pStmt, i, iValue: pValue->u.i);
85968 break;
85969 }
85970 case SQLITE_FLOAT: {
85971 rc = sqlite3_bind_double(pStmt, i, rValue: pValue->u.r);
85972 break;
85973 }
85974 case SQLITE_BLOB: {
85975 if( pValue->flags & MEM_Zero ){
85976 rc = sqlite3_bind_zeroblob(pStmt, i, n: pValue->u.nZero);
85977 }else{
85978 rc = sqlite3_bind_blob(pStmt, i, zData: pValue->z, nData: pValue->n,SQLITE_TRANSIENT);
85979 }
85980 break;
85981 }
85982 case SQLITE_TEXT: {
85983 rc = bindText(pStmt,i, zData: pValue->z, nData: pValue->n, SQLITE_TRANSIENT,
85984 encoding: pValue->enc);
85985 break;
85986 }
85987 default: {
85988 rc = sqlite3_bind_null(pStmt, i);
85989 break;
85990 }
85991 }
85992 return rc;
85993}
85994SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
85995 int rc;
85996 Vdbe *p = (Vdbe *)pStmt;
85997 rc = vdbeUnbind(p, i);
85998 if( rc==SQLITE_OK ){
85999#ifndef SQLITE_OMIT_INCRBLOB
86000 sqlite3VdbeMemSetZeroBlob(pMem: &p->aVar[i-1], n);
86001#else
86002 rc = sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
86003#endif
86004 sqlite3_mutex_leave(p: p->db->mutex);
86005 }
86006 return rc;
86007}
86008SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){
86009 int rc;
86010 Vdbe *p = (Vdbe *)pStmt;
86011 sqlite3_mutex_enter(p: p->db->mutex);
86012 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){
86013 rc = SQLITE_TOOBIG;
86014 }else{
86015 assert( (n & 0x7FFFFFFF)==n );
86016 rc = sqlite3_bind_zeroblob(pStmt, i, n);
86017 }
86018 rc = sqlite3ApiExit(db: p->db, rc);
86019 sqlite3_mutex_leave(p: p->db->mutex);
86020 return rc;
86021}
86022
86023/*
86024** Return the number of wildcards that can be potentially bound to.
86025** This routine is added to support DBD::SQLite.
86026*/
86027SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
86028 Vdbe *p = (Vdbe*)pStmt;
86029 return p ? p->nVar : 0;
86030}
86031
86032/*
86033** Return the name of a wildcard parameter. Return NULL if the index
86034** is out of range or if the wildcard is unnamed.
86035**
86036** The result is always UTF-8.
86037*/
86038SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
86039 Vdbe *p = (Vdbe*)pStmt;
86040 if( p==0 ) return 0;
86041 return sqlite3VListNumToName(pIn: p->pVList, iVal: i);
86042}
86043
86044/*
86045** Given a wildcard parameter name, return the index of the variable
86046** with that name. If there is no variable with the given name,
86047** return 0.
86048*/
86049SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
86050 if( p==0 || zName==0 ) return 0;
86051 return sqlite3VListNameToNum(pIn: p->pVList, zName, nName);
86052}
86053SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
86054 return sqlite3VdbeParameterIndex(p: (Vdbe*)pStmt, zName, nName: sqlite3Strlen30(z: zName));
86055}
86056
86057/*
86058** Transfer all bindings from the first statement over to the second.
86059*/
86060SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
86061 Vdbe *pFrom = (Vdbe*)pFromStmt;
86062 Vdbe *pTo = (Vdbe*)pToStmt;
86063 int i;
86064 assert( pTo->db==pFrom->db );
86065 assert( pTo->nVar==pFrom->nVar );
86066 sqlite3_mutex_enter(p: pTo->db->mutex);
86067 for(i=0; i<pFrom->nVar; i++){
86068 sqlite3VdbeMemMove(pTo: &pTo->aVar[i], pFrom: &pFrom->aVar[i]);
86069 }
86070 sqlite3_mutex_leave(p: pTo->db->mutex);
86071 return SQLITE_OK;
86072}
86073
86074#ifndef SQLITE_OMIT_DEPRECATED
86075/*
86076** Deprecated external interface. Internal/core SQLite code
86077** should call sqlite3TransferBindings.
86078**
86079** It is misuse to call this routine with statements from different
86080** database connections. But as this is a deprecated interface, we
86081** will not bother to check for that condition.
86082**
86083** If the two statements contain a different number of bindings, then
86084** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise
86085** SQLITE_OK is returned.
86086*/
86087SQLITE_API int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
86088 Vdbe *pFrom = (Vdbe*)pFromStmt;
86089 Vdbe *pTo = (Vdbe*)pToStmt;
86090 if( pFrom->nVar!=pTo->nVar ){
86091 return SQLITE_ERROR;
86092 }
86093 assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 );
86094 if( pTo->expmask ){
86095 pTo->expired = 1;
86096 }
86097 assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 );
86098 if( pFrom->expmask ){
86099 pFrom->expired = 1;
86100 }
86101 return sqlite3TransferBindings(pFromStmt, pToStmt);
86102}
86103#endif
86104
86105/*
86106** Return the sqlite3* database handle to which the prepared statement given
86107** in the argument belongs. This is the same database handle that was
86108** the first argument to the sqlite3_prepare() that was used to create
86109** the statement in the first place.
86110*/
86111SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
86112 return pStmt ? ((Vdbe*)pStmt)->db : 0;
86113}
86114
86115/*
86116** Return true if the prepared statement is guaranteed to not modify the
86117** database.
86118*/
86119SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
86120 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
86121}
86122
86123/*
86124** Return 1 if the statement is an EXPLAIN and return 2 if the
86125** statement is an EXPLAIN QUERY PLAN
86126*/
86127SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt){
86128 return pStmt ? ((Vdbe*)pStmt)->explain : 0;
86129}
86130
86131/*
86132** Return true if the prepared statement is in need of being reset.
86133*/
86134SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
86135 Vdbe *v = (Vdbe*)pStmt;
86136 return v!=0 && v->iVdbeMagic==VDBE_MAGIC_RUN && v->pc>=0;
86137}
86138
86139/*
86140** Return a pointer to the next prepared statement after pStmt associated
86141** with database connection pDb. If pStmt is NULL, return the first
86142** prepared statement for the database connection. Return NULL if there
86143** are no more.
86144*/
86145SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
86146 sqlite3_stmt *pNext;
86147#ifdef SQLITE_ENABLE_API_ARMOR
86148 if( !sqlite3SafetyCheckOk(pDb) ){
86149 (void)SQLITE_MISUSE_BKPT;
86150 return 0;
86151 }
86152#endif
86153 sqlite3_mutex_enter(p: pDb->mutex);
86154 if( pStmt==0 ){
86155 pNext = (sqlite3_stmt*)pDb->pVdbe;
86156 }else{
86157 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
86158 }
86159 sqlite3_mutex_leave(p: pDb->mutex);
86160 return pNext;
86161}
86162
86163/*
86164** Return the value of a status counter for a prepared statement
86165*/
86166SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
86167 Vdbe *pVdbe = (Vdbe*)pStmt;
86168 u32 v;
86169#ifdef SQLITE_ENABLE_API_ARMOR
86170 if( !pStmt
86171 || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter)))
86172 ){
86173 (void)SQLITE_MISUSE_BKPT;
86174 return 0;
86175 }
86176#endif
86177 if( op==SQLITE_STMTSTATUS_MEMUSED ){
86178 sqlite3 *db = pVdbe->db;
86179 sqlite3_mutex_enter(p: db->mutex);
86180 v = 0;
86181 db->pnBytesFreed = (int*)&v;
86182 sqlite3VdbeClearObject(db, p: pVdbe);
86183 sqlite3DbFree(db, p: pVdbe);
86184 db->pnBytesFreed = 0;
86185 sqlite3_mutex_leave(p: db->mutex);
86186 }else{
86187 v = pVdbe->aCounter[op];
86188 if( resetFlag ) pVdbe->aCounter[op] = 0;
86189 }
86190 return (int)v;
86191}
86192
86193/*
86194** Return the SQL associated with a prepared statement
86195*/
86196SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt){
86197 Vdbe *p = (Vdbe *)pStmt;
86198 return p ? p->zSql : 0;
86199}
86200
86201/*
86202** Return the SQL associated with a prepared statement with
86203** bound parameters expanded. Space to hold the returned string is
86204** obtained from sqlite3_malloc(). The caller is responsible for
86205** freeing the returned string by passing it to sqlite3_free().
86206**
86207** The SQLITE_TRACE_SIZE_LIMIT puts an upper bound on the size of
86208** expanded bound parameters.
86209*/
86210SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt){
86211#ifdef SQLITE_OMIT_TRACE
86212 return 0;
86213#else
86214 char *z = 0;
86215 const char *zSql = sqlite3_sql(pStmt);
86216 if( zSql ){
86217 Vdbe *p = (Vdbe *)pStmt;
86218 sqlite3_mutex_enter(p: p->db->mutex);
86219 z = sqlite3VdbeExpandSql(p, zSql);
86220 sqlite3_mutex_leave(p: p->db->mutex);
86221 }
86222 return z;
86223#endif
86224}
86225
86226#ifdef SQLITE_ENABLE_NORMALIZE
86227/*
86228** Return the normalized SQL associated with a prepared statement.
86229*/
86230SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt){
86231 Vdbe *p = (Vdbe *)pStmt;
86232 if( p==0 ) return 0;
86233 if( p->zNormSql==0 && ALWAYS(p->zSql!=0) ){
86234 sqlite3_mutex_enter(p->db->mutex);
86235 p->zNormSql = sqlite3Normalize(p, p->zSql);
86236 sqlite3_mutex_leave(p->db->mutex);
86237 }
86238 return p->zNormSql;
86239}
86240#endif /* SQLITE_ENABLE_NORMALIZE */
86241
86242#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
86243/*
86244** Allocate and populate an UnpackedRecord structure based on the serialized
86245** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure
86246** if successful, or a NULL pointer if an OOM error is encountered.
86247*/
86248static UnpackedRecord *vdbeUnpackRecord(
86249 KeyInfo *pKeyInfo,
86250 int nKey,
86251 const void *pKey
86252){
86253 UnpackedRecord *pRet; /* Return value */
86254
86255 pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
86256 if( pRet ){
86257 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1));
86258 sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, pRet);
86259 }
86260 return pRet;
86261}
86262
86263/*
86264** This function is called from within a pre-update callback to retrieve
86265** a field of the row currently being updated or deleted.
86266*/
86267SQLITE_API int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
86268 PreUpdate *p = db->pPreUpdate;
86269 Mem *pMem;
86270 int rc = SQLITE_OK;
86271
86272 /* Test that this call is being made from within an SQLITE_DELETE or
86273 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
86274 if( !p || p->op==SQLITE_INSERT ){
86275 rc = SQLITE_MISUSE_BKPT;
86276 goto preupdate_old_out;
86277 }
86278 if( p->pPk ){
86279 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
86280 }
86281 if( iIdx>=p->pCsr->nField || iIdx<0 ){
86282 rc = SQLITE_RANGE;
86283 goto preupdate_old_out;
86284 }
86285
86286 /* If the old.* record has not yet been loaded into memory, do so now. */
86287 if( p->pUnpacked==0 ){
86288 u32 nRec;
86289 u8 *aRec;
86290
86291 assert( p->pCsr->eCurType==CURTYPE_BTREE );
86292 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
86293 aRec = sqlite3DbMallocRaw(db, nRec);
86294 if( !aRec ) goto preupdate_old_out;
86295 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
86296 if( rc==SQLITE_OK ){
86297 p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
86298 if( !p->pUnpacked ) rc = SQLITE_NOMEM;
86299 }
86300 if( rc!=SQLITE_OK ){
86301 sqlite3DbFree(db, aRec);
86302 goto preupdate_old_out;
86303 }
86304 p->aRecord = aRec;
86305 }
86306
86307 pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
86308 if( iIdx==p->pTab->iPKey ){
86309 sqlite3VdbeMemSetInt64(pMem, p->iKey1);
86310 }else if( iIdx>=p->pUnpacked->nField ){
86311 *ppValue = (sqlite3_value *)columnNullValue();
86312 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
86313 if( pMem->flags & (MEM_Int|MEM_IntReal) ){
86314 testcase( pMem->flags & MEM_Int );
86315 testcase( pMem->flags & MEM_IntReal );
86316 sqlite3VdbeMemRealify(pMem);
86317 }
86318 }
86319
86320 preupdate_old_out:
86321 sqlite3Error(db, rc);
86322 return sqlite3ApiExit(db, rc);
86323}
86324#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
86325
86326#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
86327/*
86328** This function is called from within a pre-update callback to retrieve
86329** the number of columns in the row being updated, deleted or inserted.
86330*/
86331SQLITE_API int sqlite3_preupdate_count(sqlite3 *db){
86332 PreUpdate *p = db->pPreUpdate;
86333 return (p ? p->keyinfo.nKeyField : 0);
86334}
86335#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
86336
86337#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
86338/*
86339** This function is designed to be called from within a pre-update callback
86340** only. It returns zero if the change that caused the callback was made
86341** immediately by a user SQL statement. Or, if the change was made by a
86342** trigger program, it returns the number of trigger programs currently
86343** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
86344** top-level trigger etc.).
86345**
86346** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL
86347** or SET DEFAULT action is considered a trigger.
86348*/
86349SQLITE_API int sqlite3_preupdate_depth(sqlite3 *db){
86350 PreUpdate *p = db->pPreUpdate;
86351 return (p ? p->v->nFrame : 0);
86352}
86353#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
86354
86355#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
86356/*
86357** This function is designed to be called from within a pre-update callback
86358** only.
86359*/
86360SQLITE_API int sqlite3_preupdate_blobwrite(sqlite3 *db){
86361 PreUpdate *p = db->pPreUpdate;
86362 return (p ? p->iBlobWrite : -1);
86363}
86364#endif
86365
86366#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
86367/*
86368** This function is called from within a pre-update callback to retrieve
86369** a field of the row currently being updated or inserted.
86370*/
86371SQLITE_API int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
86372 PreUpdate *p = db->pPreUpdate;
86373 int rc = SQLITE_OK;
86374 Mem *pMem;
86375
86376 if( !p || p->op==SQLITE_DELETE ){
86377 rc = SQLITE_MISUSE_BKPT;
86378 goto preupdate_new_out;
86379 }
86380 if( p->pPk && p->op!=SQLITE_UPDATE ){
86381 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
86382 }
86383 if( iIdx>=p->pCsr->nField || iIdx<0 ){
86384 rc = SQLITE_RANGE;
86385 goto preupdate_new_out;
86386 }
86387
86388 if( p->op==SQLITE_INSERT ){
86389 /* For an INSERT, memory cell p->iNewReg contains the serialized record
86390 ** that is being inserted. Deserialize it. */
86391 UnpackedRecord *pUnpack = p->pNewUnpacked;
86392 if( !pUnpack ){
86393 Mem *pData = &p->v->aMem[p->iNewReg];
86394 rc = ExpandBlob(pData);
86395 if( rc!=SQLITE_OK ) goto preupdate_new_out;
86396 pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
86397 if( !pUnpack ){
86398 rc = SQLITE_NOMEM;
86399 goto preupdate_new_out;
86400 }
86401 p->pNewUnpacked = pUnpack;
86402 }
86403 pMem = &pUnpack->aMem[iIdx];
86404 if( iIdx==p->pTab->iPKey ){
86405 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
86406 }else if( iIdx>=pUnpack->nField ){
86407 pMem = (sqlite3_value *)columnNullValue();
86408 }
86409 }else{
86410 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
86411 ** value. Make a copy of the cell contents and return a pointer to it.
86412 ** It is not safe to return a pointer to the memory cell itself as the
86413 ** caller may modify the value text encoding.
86414 */
86415 assert( p->op==SQLITE_UPDATE );
86416 if( !p->aNew ){
86417 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField);
86418 if( !p->aNew ){
86419 rc = SQLITE_NOMEM;
86420 goto preupdate_new_out;
86421 }
86422 }
86423 assert( iIdx>=0 && iIdx<p->pCsr->nField );
86424 pMem = &p->aNew[iIdx];
86425 if( pMem->flags==0 ){
86426 if( iIdx==p->pTab->iPKey ){
86427 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
86428 }else{
86429 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]);
86430 if( rc!=SQLITE_OK ) goto preupdate_new_out;
86431 }
86432 }
86433 }
86434 *ppValue = pMem;
86435
86436 preupdate_new_out:
86437 sqlite3Error(db, rc);
86438 return sqlite3ApiExit(db, rc);
86439}
86440#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
86441
86442#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
86443/*
86444** Return status data for a single loop within query pStmt.
86445*/
86446SQLITE_API int sqlite3_stmt_scanstatus(
86447 sqlite3_stmt *pStmt, /* Prepared statement being queried */
86448 int idx, /* Index of loop to report on */
86449 int iScanStatusOp, /* Which metric to return */
86450 void *pOut /* OUT: Write the answer here */
86451){
86452 Vdbe *p = (Vdbe*)pStmt;
86453 ScanStatus *pScan;
86454 if( idx<0 || idx>=p->nScan ) return 1;
86455 pScan = &p->aScan[idx];
86456 switch( iScanStatusOp ){
86457 case SQLITE_SCANSTAT_NLOOP: {
86458 *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop];
86459 break;
86460 }
86461 case SQLITE_SCANSTAT_NVISIT: {
86462 *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit];
86463 break;
86464 }
86465 case SQLITE_SCANSTAT_EST: {
86466 double r = 1.0;
86467 LogEst x = pScan->nEst;
86468 while( x<100 ){
86469 x += 10;
86470 r *= 0.5;
86471 }
86472 *(double*)pOut = r*sqlite3LogEstToInt(x);
86473 break;
86474 }
86475 case SQLITE_SCANSTAT_NAME: {
86476 *(const char**)pOut = pScan->zName;
86477 break;
86478 }
86479 case SQLITE_SCANSTAT_EXPLAIN: {
86480 if( pScan->addrExplain ){
86481 *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z;
86482 }else{
86483 *(const char**)pOut = 0;
86484 }
86485 break;
86486 }
86487 case SQLITE_SCANSTAT_SELECTID: {
86488 if( pScan->addrExplain ){
86489 *(int*)pOut = p->aOp[ pScan->addrExplain ].p1;
86490 }else{
86491 *(int*)pOut = -1;
86492 }
86493 break;
86494 }
86495 default: {
86496 return 1;
86497 }
86498 }
86499 return 0;
86500}
86501
86502/*
86503** Zero all counters associated with the sqlite3_stmt_scanstatus() data.
86504*/
86505SQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){
86506 Vdbe *p = (Vdbe*)pStmt;
86507 memset(p->anExec, 0, p->nOp * sizeof(i64));
86508}
86509#endif /* SQLITE_ENABLE_STMT_SCANSTATUS */
86510
86511/************** End of vdbeapi.c *********************************************/
86512/************** Begin file vdbetrace.c ***************************************/
86513/*
86514** 2009 November 25
86515**
86516** The author disclaims copyright to this source code. In place of
86517** a legal notice, here is a blessing:
86518**
86519** May you do good and not evil.
86520** May you find forgiveness for yourself and forgive others.
86521** May you share freely, never taking more than you give.
86522**
86523*************************************************************************
86524**
86525** This file contains code used to insert the values of host parameters
86526** (aka "wildcards") into the SQL text output by sqlite3_trace().
86527**
86528** The Vdbe parse-tree explainer is also found here.
86529*/
86530/* #include "sqliteInt.h" */
86531/* #include "vdbeInt.h" */
86532
86533#ifndef SQLITE_OMIT_TRACE
86534
86535/*
86536** zSql is a zero-terminated string of UTF-8 SQL text. Return the number of
86537** bytes in this text up to but excluding the first character in
86538** a host parameter. If the text contains no host parameters, return
86539** the total number of bytes in the text.
86540*/
86541static int findNextHostParameter(const char *zSql, int *pnToken){
86542 int tokenType;
86543 int nTotal = 0;
86544 int n;
86545
86546 *pnToken = 0;
86547 while( zSql[0] ){
86548 n = sqlite3GetToken((u8*)zSql, &tokenType);
86549 assert( n>0 && tokenType!=TK_ILLEGAL );
86550 if( tokenType==TK_VARIABLE ){
86551 *pnToken = n;
86552 break;
86553 }
86554 nTotal += n;
86555 zSql += n;
86556 }
86557 return nTotal;
86558}
86559
86560/*
86561** This function returns a pointer to a nul-terminated string in memory
86562** obtained from sqlite3DbMalloc(). If sqlite3.nVdbeExec is 1, then the
86563** string contains a copy of zRawSql but with host parameters expanded to
86564** their current bindings. Or, if sqlite3.nVdbeExec is greater than 1,
86565** then the returned string holds a copy of zRawSql with "-- " prepended
86566** to each line of text.
86567**
86568** If the SQLITE_TRACE_SIZE_LIMIT macro is defined to an integer, then
86569** then long strings and blobs are truncated to that many bytes. This
86570** can be used to prevent unreasonably large trace strings when dealing
86571** with large (multi-megabyte) strings and blobs.
86572**
86573** The calling function is responsible for making sure the memory returned
86574** is eventually freed.
86575**
86576** ALGORITHM: Scan the input string looking for host parameters in any of
86577** these forms: ?, ?N, $A, @A, :A. Take care to avoid text within
86578** string literals, quoted identifier names, and comments. For text forms,
86579** the host parameter index is found by scanning the prepared
86580** statement for the corresponding OP_Variable opcode. Once the host
86581** parameter index is known, locate the value in p->aVar[]. Then render
86582** the value as a literal in place of the host parameter name.
86583*/
86584SQLITE_PRIVATE char *sqlite3VdbeExpandSql(
86585 Vdbe *p, /* The prepared statement being evaluated */
86586 const char *zRawSql /* Raw text of the SQL statement */
86587){
86588 sqlite3 *db; /* The database connection */
86589 int idx = 0; /* Index of a host parameter */
86590 int nextIndex = 1; /* Index of next ? host parameter */
86591 int n; /* Length of a token prefix */
86592 int nToken; /* Length of the parameter token */
86593 int i; /* Loop counter */
86594 Mem *pVar; /* Value of a host parameter */
86595 StrAccum out; /* Accumulate the output here */
86596#ifndef SQLITE_OMIT_UTF16
86597 Mem utf8; /* Used to convert UTF16 into UTF8 for display */
86598#endif
86599
86600 db = p->db;
86601 sqlite3StrAccumInit(p: &out, db: 0, zBase: 0, n: 0, mx: db->aLimit[SQLITE_LIMIT_LENGTH]);
86602 if( db->nVdbeExec>1 ){
86603 while( *zRawSql ){
86604 const char *zStart = zRawSql;
86605 while( *(zRawSql++)!='\n' && *zRawSql );
86606 sqlite3_str_append(p: &out, z: "-- ", N: 3);
86607 assert( (zRawSql - zStart) > 0 );
86608 sqlite3_str_append(p: &out, z: zStart, N: (int)(zRawSql-zStart));
86609 }
86610 }else if( p->nVar==0 ){
86611 sqlite3_str_append(p: &out, z: zRawSql, N: sqlite3Strlen30(z: zRawSql));
86612 }else{
86613 while( zRawSql[0] ){
86614 n = findNextHostParameter(zSql: zRawSql, pnToken: &nToken);
86615 assert( n>0 );
86616 sqlite3_str_append(p: &out, z: zRawSql, N: n);
86617 zRawSql += n;
86618 assert( zRawSql[0] || nToken==0 );
86619 if( nToken==0 ) break;
86620 if( zRawSql[0]=='?' ){
86621 if( nToken>1 ){
86622 assert( sqlite3Isdigit(zRawSql[1]) );
86623 sqlite3GetInt32(zNum: &zRawSql[1], pValue: &idx);
86624 }else{
86625 idx = nextIndex;
86626 }
86627 }else{
86628 assert( zRawSql[0]==':' || zRawSql[0]=='$' ||
86629 zRawSql[0]=='@' || zRawSql[0]=='#' );
86630 testcase( zRawSql[0]==':' );
86631 testcase( zRawSql[0]=='$' );
86632 testcase( zRawSql[0]=='@' );
86633 testcase( zRawSql[0]=='#' );
86634 idx = sqlite3VdbeParameterIndex(p, zName: zRawSql, nName: nToken);
86635 assert( idx>0 );
86636 }
86637 zRawSql += nToken;
86638 nextIndex = MAX(idx + 1, nextIndex);
86639 assert( idx>0 && idx<=p->nVar );
86640 pVar = &p->aVar[idx-1];
86641 if( pVar->flags & MEM_Null ){
86642 sqlite3_str_append(p: &out, z: "NULL", N: 4);
86643 }else if( pVar->flags & (MEM_Int|MEM_IntReal) ){
86644 sqlite3_str_appendf(p: &out, zFormat: "%lld", pVar->u.i);
86645 }else if( pVar->flags & MEM_Real ){
86646 sqlite3_str_appendf(p: &out, zFormat: "%!.15g", pVar->u.r);
86647 }else if( pVar->flags & MEM_Str ){
86648 int nOut; /* Number of bytes of the string text to include in output */
86649#ifndef SQLITE_OMIT_UTF16
86650 u8 enc = ENC(db);
86651 if( enc!=SQLITE_UTF8 ){
86652 memset(s: &utf8, c: 0, n: sizeof(utf8));
86653 utf8.db = db;
86654 sqlite3VdbeMemSetStr(pMem: &utf8, z: pVar->z, n: pVar->n, enc, SQLITE_STATIC);
86655 if( SQLITE_NOMEM==sqlite3VdbeChangeEncoding(pMem: &utf8, SQLITE_UTF8) ){
86656 out.accError = SQLITE_NOMEM;
86657 out.nAlloc = 0;
86658 }
86659 pVar = &utf8;
86660 }
86661#endif
86662 nOut = pVar->n;
86663#ifdef SQLITE_TRACE_SIZE_LIMIT
86664 if( nOut>SQLITE_TRACE_SIZE_LIMIT ){
86665 nOut = SQLITE_TRACE_SIZE_LIMIT;
86666 while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
86667 }
86668#endif
86669 sqlite3_str_appendf(p: &out, zFormat: "'%.*q'", nOut, pVar->z);
86670#ifdef SQLITE_TRACE_SIZE_LIMIT
86671 if( nOut<pVar->n ){
86672 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
86673 }
86674#endif
86675#ifndef SQLITE_OMIT_UTF16
86676 if( enc!=SQLITE_UTF8 ) sqlite3VdbeMemRelease(p: &utf8);
86677#endif
86678 }else if( pVar->flags & MEM_Zero ){
86679 sqlite3_str_appendf(p: &out, zFormat: "zeroblob(%d)", pVar->u.nZero);
86680 }else{
86681 int nOut; /* Number of bytes of the blob to include in output */
86682 assert( pVar->flags & MEM_Blob );
86683 sqlite3_str_append(p: &out, z: "x'", N: 2);
86684 nOut = pVar->n;
86685#ifdef SQLITE_TRACE_SIZE_LIMIT
86686 if( nOut>SQLITE_TRACE_SIZE_LIMIT ) nOut = SQLITE_TRACE_SIZE_LIMIT;
86687#endif
86688 for(i=0; i<nOut; i++){
86689 sqlite3_str_appendf(p: &out, zFormat: "%02x", pVar->z[i]&0xff);
86690 }
86691 sqlite3_str_append(p: &out, z: "'", N: 1);
86692#ifdef SQLITE_TRACE_SIZE_LIMIT
86693 if( nOut<pVar->n ){
86694 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
86695 }
86696#endif
86697 }
86698 }
86699 }
86700 if( out.accError ) sqlite3_str_reset(p: &out);
86701 return sqlite3StrAccumFinish(p: &out);
86702}
86703
86704#endif /* #ifndef SQLITE_OMIT_TRACE */
86705
86706/************** End of vdbetrace.c *******************************************/
86707/************** Begin file vdbe.c ********************************************/
86708/*
86709** 2001 September 15
86710**
86711** The author disclaims copyright to this source code. In place of
86712** a legal notice, here is a blessing:
86713**
86714** May you do good and not evil.
86715** May you find forgiveness for yourself and forgive others.
86716** May you share freely, never taking more than you give.
86717**
86718*************************************************************************
86719** The code in this file implements the function that runs the
86720** bytecode of a prepared statement.
86721**
86722** Various scripts scan this source file in order to generate HTML
86723** documentation, headers files, or other derived files. The formatting
86724** of the code in this file is, therefore, important. See other comments
86725** in this file for details. If in doubt, do not deviate from existing
86726** commenting and indentation practices when changing or adding code.
86727*/
86728/* #include "sqliteInt.h" */
86729/* #include "vdbeInt.h" */
86730
86731/*
86732** Invoke this macro on memory cells just prior to changing the
86733** value of the cell. This macro verifies that shallow copies are
86734** not misused. A shallow copy of a string or blob just copies a
86735** pointer to the string or blob, not the content. If the original
86736** is changed while the copy is still in use, the string or blob might
86737** be changed out from under the copy. This macro verifies that nothing
86738** like that ever happens.
86739*/
86740#ifdef SQLITE_DEBUG
86741# define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
86742#else
86743# define memAboutToChange(P,M)
86744#endif
86745
86746/*
86747** The following global variable is incremented every time a cursor
86748** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test
86749** procedures use this information to make sure that indices are
86750** working correctly. This variable has no function other than to
86751** help verify the correct operation of the library.
86752*/
86753#ifdef SQLITE_TEST
86754SQLITE_API int sqlite3_search_count = 0;
86755#endif
86756
86757/*
86758** When this global variable is positive, it gets decremented once before
86759** each instruction in the VDBE. When it reaches zero, the u1.isInterrupted
86760** field of the sqlite3 structure is set in order to simulate an interrupt.
86761**
86762** This facility is used for testing purposes only. It does not function
86763** in an ordinary build.
86764*/
86765#ifdef SQLITE_TEST
86766SQLITE_API int sqlite3_interrupt_count = 0;
86767#endif
86768
86769/*
86770** The next global variable is incremented each type the OP_Sort opcode
86771** is executed. The test procedures use this information to make sure that
86772** sorting is occurring or not occurring at appropriate times. This variable
86773** has no function other than to help verify the correct operation of the
86774** library.
86775*/
86776#ifdef SQLITE_TEST
86777SQLITE_API int sqlite3_sort_count = 0;
86778#endif
86779
86780/*
86781** The next global variable records the size of the largest MEM_Blob
86782** or MEM_Str that has been used by a VDBE opcode. The test procedures
86783** use this information to make sure that the zero-blob functionality
86784** is working correctly. This variable has no function other than to
86785** help verify the correct operation of the library.
86786*/
86787#ifdef SQLITE_TEST
86788SQLITE_API int sqlite3_max_blobsize = 0;
86789static void updateMaxBlobsize(Mem *p){
86790 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
86791 sqlite3_max_blobsize = p->n;
86792 }
86793}
86794#endif
86795
86796/*
86797** This macro evaluates to true if either the update hook or the preupdate
86798** hook are enabled for database connect DB.
86799*/
86800#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
86801# define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback)
86802#else
86803# define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback)
86804#endif
86805
86806/*
86807** The next global variable is incremented each time the OP_Found opcode
86808** is executed. This is used to test whether or not the foreign key
86809** operation implemented using OP_FkIsZero is working. This variable
86810** has no function other than to help verify the correct operation of the
86811** library.
86812*/
86813#ifdef SQLITE_TEST
86814SQLITE_API int sqlite3_found_count = 0;
86815#endif
86816
86817/*
86818** Test a register to see if it exceeds the current maximum blob size.
86819** If it does, record the new maximum blob size.
86820*/
86821#if defined(SQLITE_TEST) && !defined(SQLITE_UNTESTABLE)
86822# define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P)
86823#else
86824# define UPDATE_MAX_BLOBSIZE(P)
86825#endif
86826
86827#ifdef SQLITE_DEBUG
86828/* This routine provides a convenient place to set a breakpoint during
86829** tracing with PRAGMA vdbe_trace=on. The breakpoint fires right after
86830** each opcode is printed. Variables "pc" (program counter) and pOp are
86831** available to add conditionals to the breakpoint. GDB example:
86832**
86833** break test_trace_breakpoint if pc=22
86834**
86835** Other useful labels for breakpoints include:
86836** test_addop_breakpoint(pc,pOp)
86837** sqlite3CorruptError(lineno)
86838** sqlite3MisuseError(lineno)
86839** sqlite3CantopenError(lineno)
86840*/
86841static void test_trace_breakpoint(int pc, Op *pOp, Vdbe *v){
86842 static int n = 0;
86843 n++;
86844}
86845#endif
86846
86847/*
86848** Invoke the VDBE coverage callback, if that callback is defined. This
86849** feature is used for test suite validation only and does not appear an
86850** production builds.
86851**
86852** M is the type of branch. I is the direction taken for this instance of
86853** the branch.
86854**
86855** M: 2 - two-way branch (I=0: fall-thru 1: jump )
86856** 3 - two-way + NULL (I=0: fall-thru 1: jump 2: NULL )
86857** 4 - OP_Jump (I=0: jump p1 1: jump p2 2: jump p3)
86858**
86859** In other words, if M is 2, then I is either 0 (for fall-through) or
86860** 1 (for when the branch is taken). If M is 3, the I is 0 for an
86861** ordinary fall-through, I is 1 if the branch was taken, and I is 2
86862** if the result of comparison is NULL. For M=3, I=2 the jump may or
86863** may not be taken, depending on the SQLITE_JUMPIFNULL flags in p5.
86864** When M is 4, that means that an OP_Jump is being run. I is 0, 1, or 2
86865** depending on if the operands are less than, equal, or greater than.
86866**
86867** iSrcLine is the source code line (from the __LINE__ macro) that
86868** generated the VDBE instruction combined with flag bits. The source
86869** code line number is in the lower 24 bits of iSrcLine and the upper
86870** 8 bytes are flags. The lower three bits of the flags indicate
86871** values for I that should never occur. For example, if the branch is
86872** always taken, the flags should be 0x05 since the fall-through and
86873** alternate branch are never taken. If a branch is never taken then
86874** flags should be 0x06 since only the fall-through approach is allowed.
86875**
86876** Bit 0x08 of the flags indicates an OP_Jump opcode that is only
86877** interested in equal or not-equal. In other words, I==0 and I==2
86878** should be treated as equivalent
86879**
86880** Since only a line number is retained, not the filename, this macro
86881** only works for amalgamation builds. But that is ok, since these macros
86882** should be no-ops except for special builds used to measure test coverage.
86883*/
86884#if !defined(SQLITE_VDBE_COVERAGE)
86885# define VdbeBranchTaken(I,M)
86886#else
86887# define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
86888 static void vdbeTakeBranch(u32 iSrcLine, u8 I, u8 M){
86889 u8 mNever;
86890 assert( I<=2 ); /* 0: fall through, 1: taken, 2: alternate taken */
86891 assert( M<=4 ); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */
86892 assert( I<M ); /* I can only be 2 if M is 3 or 4 */
86893 /* Transform I from a integer [0,1,2] into a bitmask of [1,2,4] */
86894 I = 1<<I;
86895 /* The upper 8 bits of iSrcLine are flags. The lower three bits of
86896 ** the flags indicate directions that the branch can never go. If
86897 ** a branch really does go in one of those directions, assert right
86898 ** away. */
86899 mNever = iSrcLine >> 24;
86900 assert( (I & mNever)==0 );
86901 if( sqlite3GlobalConfig.xVdbeBranch==0 ) return; /*NO_TEST*/
86902 /* Invoke the branch coverage callback with three arguments:
86903 ** iSrcLine - the line number of the VdbeCoverage() macro, with
86904 ** flags removed.
86905 ** I - Mask of bits 0x07 indicating which cases are are
86906 ** fulfilled by this instance of the jump. 0x01 means
86907 ** fall-thru, 0x02 means taken, 0x04 means NULL. Any
86908 ** impossible cases (ex: if the comparison is never NULL)
86909 ** are filled in automatically so that the coverage
86910 ** measurement logic does not flag those impossible cases
86911 ** as missed coverage.
86912 ** M - Type of jump. Same as M argument above
86913 */
86914 I |= mNever;
86915 if( M==2 ) I |= 0x04;
86916 if( M==4 ){
86917 I |= 0x08;
86918 if( (mNever&0x08)!=0 && (I&0x05)!=0) I |= 0x05; /*NO_TEST*/
86919 }
86920 sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg,
86921 iSrcLine&0xffffff, I, M);
86922 }
86923#endif
86924
86925/*
86926** An ephemeral string value (signified by the MEM_Ephem flag) contains
86927** a pointer to a dynamically allocated string where some other entity
86928** is responsible for deallocating that string. Because the register
86929** does not control the string, it might be deleted without the register
86930** knowing it.
86931**
86932** This routine converts an ephemeral string into a dynamically allocated
86933** string that the register itself controls. In other words, it
86934** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
86935*/
86936#define Deephemeralize(P) \
86937 if( ((P)->flags&MEM_Ephem)!=0 \
86938 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
86939
86940/* Return true if the cursor was opened using the OP_OpenSorter opcode. */
86941#define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
86942
86943/*
86944** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
86945** if we run out of memory.
86946*/
86947static VdbeCursor *allocateCursor(
86948 Vdbe *p, /* The virtual machine */
86949 int iCur, /* Index of the new VdbeCursor */
86950 int nField, /* Number of fields in the table or index */
86951 int iDb, /* Database the cursor belongs to, or -1 */
86952 u8 eCurType /* Type of the new cursor */
86953){
86954 /* Find the memory cell that will be used to store the blob of memory
86955 ** required for this VdbeCursor structure. It is convenient to use a
86956 ** vdbe memory cell to manage the memory allocation required for a
86957 ** VdbeCursor structure for the following reasons:
86958 **
86959 ** * Sometimes cursor numbers are used for a couple of different
86960 ** purposes in a vdbe program. The different uses might require
86961 ** different sized allocations. Memory cells provide growable
86962 ** allocations.
86963 **
86964 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
86965 ** be freed lazily via the sqlite3_release_memory() API. This
86966 ** minimizes the number of malloc calls made by the system.
86967 **
86968 ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from
86969 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
86970 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
86971 */
86972 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
86973
86974 int nByte;
86975 VdbeCursor *pCx = 0;
86976 nByte =
86977 ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField +
86978 (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0);
86979
86980 assert( iCur>=0 && iCur<p->nCursor );
86981 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
86982 sqlite3VdbeFreeCursor(p, pCx: p->apCsr[iCur]);
86983 p->apCsr[iCur] = 0;
86984 }
86985
86986 /* There used to be a call to sqlite3VdbeMemClearAndResize() to make sure
86987 ** the pMem used to hold space for the cursor has enough storage available
86988 ** in pMem->zMalloc. But for the special case of the aMem[] entries used
86989 ** to hold cursors, it is faster to in-line the logic. */
86990 assert( pMem->flags==MEM_Undefined );
86991 assert( (pMem->flags & MEM_Dyn)==0 );
86992 assert( pMem->szMalloc==0 || pMem->z==pMem->zMalloc );
86993 if( pMem->szMalloc<nByte ){
86994 if( pMem->szMalloc>0 ){
86995 sqlite3DbFreeNN(db: pMem->db, p: pMem->zMalloc);
86996 }
86997 pMem->z = pMem->zMalloc = sqlite3DbMallocRaw(db: pMem->db, n: nByte);
86998 if( pMem->zMalloc==0 ){
86999 pMem->szMalloc = 0;
87000 return 0;
87001 }
87002 pMem->szMalloc = nByte;
87003 }
87004
87005 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->zMalloc;
87006 memset(s: pCx, c: 0, offsetof(VdbeCursor,pAltCursor));
87007 pCx->eCurType = eCurType;
87008 pCx->iDb = iDb;
87009 pCx->nField = nField;
87010 pCx->aOffset = &pCx->aType[nField];
87011 if( eCurType==CURTYPE_BTREE ){
87012 pCx->uc.pCursor = (BtCursor*)
87013 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
87014 sqlite3BtreeCursorZero(p: pCx->uc.pCursor);
87015 }
87016 return pCx;
87017}
87018
87019/*
87020** The string in pRec is known to look like an integer and to have a
87021** floating point value of rValue. Return true and set *piValue to the
87022** integer value if the string is in range to be an integer. Otherwise,
87023** return false.
87024*/
87025static int alsoAnInt(Mem *pRec, double rValue, i64 *piValue){
87026 i64 iValue = (double)rValue;
87027 if( sqlite3RealSameAsInt(r1: rValue,i: iValue) ){
87028 *piValue = iValue;
87029 return 1;
87030 }
87031 return 0==sqlite3Atoi64(zNum: pRec->z, pNum: piValue, length: pRec->n, enc: pRec->enc);
87032}
87033
87034/*
87035** Try to convert a value into a numeric representation if we can
87036** do so without loss of information. In other words, if the string
87037** looks like a number, convert it into a number. If it does not
87038** look like a number, leave it alone.
87039**
87040** If the bTryForInt flag is true, then extra effort is made to give
87041** an integer representation. Strings that look like floating point
87042** values but which have no fractional component (example: '48.00')
87043** will have a MEM_Int representation when bTryForInt is true.
87044**
87045** If bTryForInt is false, then if the input string contains a decimal
87046** point or exponential notation, the result is only MEM_Real, even
87047** if there is an exact integer representation of the quantity.
87048*/
87049static void applyNumericAffinity(Mem *pRec, int bTryForInt){
87050 double rValue;
87051 u8 enc = pRec->enc;
87052 int rc;
87053 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str );
87054 rc = sqlite3AtoF(z: pRec->z, pResult: &rValue, length: pRec->n, enc);
87055 if( rc<=0 ) return;
87056 if( rc==1 && alsoAnInt(pRec, rValue, piValue: &pRec->u.i) ){
87057 pRec->flags |= MEM_Int;
87058 }else{
87059 pRec->u.r = rValue;
87060 pRec->flags |= MEM_Real;
87061 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pMem: pRec);
87062 }
87063 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
87064 ** string representation after computing a numeric equivalent, because the
87065 ** string representation might not be the canonical representation for the
87066 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
87067 pRec->flags &= ~MEM_Str;
87068}
87069
87070/*
87071** Processing is determine by the affinity parameter:
87072**
87073** SQLITE_AFF_INTEGER:
87074** SQLITE_AFF_REAL:
87075** SQLITE_AFF_NUMERIC:
87076** Try to convert pRec to an integer representation or a
87077** floating-point representation if an integer representation
87078** is not possible. Note that the integer representation is
87079** always preferred, even if the affinity is REAL, because
87080** an integer representation is more space efficient on disk.
87081**
87082** SQLITE_AFF_TEXT:
87083** Convert pRec to a text representation.
87084**
87085** SQLITE_AFF_BLOB:
87086** SQLITE_AFF_NONE:
87087** No-op. pRec is unchanged.
87088*/
87089static void applyAffinity(
87090 Mem *pRec, /* The value to apply affinity to */
87091 char affinity, /* The affinity to be applied */
87092 u8 enc /* Use this text encoding */
87093){
87094 if( affinity>=SQLITE_AFF_NUMERIC ){
87095 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
87096 || affinity==SQLITE_AFF_NUMERIC );
87097 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
87098 if( (pRec->flags & MEM_Real)==0 ){
87099 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,bTryForInt: 1);
87100 }else{
87101 sqlite3VdbeIntegerAffinity(pMem: pRec);
87102 }
87103 }
87104 }else if( affinity==SQLITE_AFF_TEXT ){
87105 /* Only attempt the conversion to TEXT if there is an integer or real
87106 ** representation (blob and NULL do not get converted) but no string
87107 ** representation. It would be harmless to repeat the conversion if
87108 ** there is already a string rep, but it is pointless to waste those
87109 ** CPU cycles. */
87110 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
87111 if( (pRec->flags&(MEM_Real|MEM_Int|MEM_IntReal)) ){
87112 testcase( pRec->flags & MEM_Int );
87113 testcase( pRec->flags & MEM_Real );
87114 testcase( pRec->flags & MEM_IntReal );
87115 sqlite3VdbeMemStringify(pMem: pRec, enc, bForce: 1);
87116 }
87117 }
87118 pRec->flags &= ~(MEM_Real|MEM_Int|MEM_IntReal);
87119 }
87120}
87121
87122/*
87123** Try to convert the type of a function argument or a result column
87124** into a numeric representation. Use either INTEGER or REAL whichever
87125** is appropriate. But only do the conversion if it is possible without
87126** loss of information and return the revised type of the argument.
87127*/
87128SQLITE_API int sqlite3_value_numeric_type(sqlite3_value *pVal){
87129 int eType = sqlite3_value_type(pVal);
87130 if( eType==SQLITE_TEXT ){
87131 Mem *pMem = (Mem*)pVal;
87132 applyNumericAffinity(pRec: pMem, bTryForInt: 0);
87133 eType = sqlite3_value_type(pVal);
87134 }
87135 return eType;
87136}
87137
87138/*
87139** Exported version of applyAffinity(). This one works on sqlite3_value*,
87140** not the internal Mem* type.
87141*/
87142SQLITE_PRIVATE void sqlite3ValueApplyAffinity(
87143 sqlite3_value *pVal,
87144 u8 affinity,
87145 u8 enc
87146){
87147 applyAffinity(pRec: (Mem *)pVal, affinity, enc);
87148}
87149
87150/*
87151** pMem currently only holds a string type (or maybe a BLOB that we can
87152** interpret as a string if we want to). Compute its corresponding
87153** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
87154** accordingly.
87155*/
87156static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
87157 int rc;
87158 sqlite3_int64 ix;
87159 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 );
87160 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
87161 if( ExpandBlob(pMem) ){
87162 pMem->u.i = 0;
87163 return MEM_Int;
87164 }
87165 rc = sqlite3AtoF(z: pMem->z, pResult: &pMem->u.r, length: pMem->n, enc: pMem->enc);
87166 if( rc<=0 ){
87167 if( rc==0 && sqlite3Atoi64(zNum: pMem->z, pNum: &ix, length: pMem->n, enc: pMem->enc)<=1 ){
87168 pMem->u.i = ix;
87169 return MEM_Int;
87170 }else{
87171 return MEM_Real;
87172 }
87173 }else if( rc==1 && sqlite3Atoi64(zNum: pMem->z, pNum: &ix, length: pMem->n, enc: pMem->enc)==0 ){
87174 pMem->u.i = ix;
87175 return MEM_Int;
87176 }
87177 return MEM_Real;
87178}
87179
87180/*
87181** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
87182** none.
87183**
87184** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
87185** But it does set pMem->u.r and pMem->u.i appropriately.
87186*/
87187static u16 numericType(Mem *pMem){
87188 if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal) ){
87189 testcase( pMem->flags & MEM_Int );
87190 testcase( pMem->flags & MEM_Real );
87191 testcase( pMem->flags & MEM_IntReal );
87192 return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal);
87193 }
87194 if( pMem->flags & (MEM_Str|MEM_Blob) ){
87195 testcase( pMem->flags & MEM_Str );
87196 testcase( pMem->flags & MEM_Blob );
87197 return computeNumericType(pMem);
87198 }
87199 return 0;
87200}
87201
87202#ifdef SQLITE_DEBUG
87203/*
87204** Write a nice string representation of the contents of cell pMem
87205** into buffer zBuf, length nBuf.
87206*/
87207SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, StrAccum *pStr){
87208 int f = pMem->flags;
87209 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
87210 if( f&MEM_Blob ){
87211 int i;
87212 char c;
87213 if( f & MEM_Dyn ){
87214 c = 'z';
87215 assert( (f & (MEM_Static|MEM_Ephem))==0 );
87216 }else if( f & MEM_Static ){
87217 c = 't';
87218 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
87219 }else if( f & MEM_Ephem ){
87220 c = 'e';
87221 assert( (f & (MEM_Static|MEM_Dyn))==0 );
87222 }else{
87223 c = 's';
87224 }
87225 sqlite3_str_appendf(pStr, "%cx[", c);
87226 for(i=0; i<25 && i<pMem->n; i++){
87227 sqlite3_str_appendf(pStr, "%02X", ((int)pMem->z[i] & 0xFF));
87228 }
87229 sqlite3_str_appendf(pStr, "|");
87230 for(i=0; i<25 && i<pMem->n; i++){
87231 char z = pMem->z[i];
87232 sqlite3_str_appendchar(pStr, 1, (z<32||z>126)?'.':z);
87233 }
87234 sqlite3_str_appendf(pStr,"]");
87235 if( f & MEM_Zero ){
87236 sqlite3_str_appendf(pStr, "+%dz",pMem->u.nZero);
87237 }
87238 }else if( f & MEM_Str ){
87239 int j;
87240 u8 c;
87241 if( f & MEM_Dyn ){
87242 c = 'z';
87243 assert( (f & (MEM_Static|MEM_Ephem))==0 );
87244 }else if( f & MEM_Static ){
87245 c = 't';
87246 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
87247 }else if( f & MEM_Ephem ){
87248 c = 'e';
87249 assert( (f & (MEM_Static|MEM_Dyn))==0 );
87250 }else{
87251 c = 's';
87252 }
87253 sqlite3_str_appendf(pStr, " %c%d[", c, pMem->n);
87254 for(j=0; j<25 && j<pMem->n; j++){
87255 c = pMem->z[j];
87256 sqlite3_str_appendchar(pStr, 1, (c>=0x20&&c<=0x7f) ? c : '.');
87257 }
87258 sqlite3_str_appendf(pStr, "]%s", encnames[pMem->enc]);
87259 }
87260}
87261#endif
87262
87263#ifdef SQLITE_DEBUG
87264/*
87265** Print the value of a register for tracing purposes:
87266*/
87267static void memTracePrint(Mem *p){
87268 if( p->flags & MEM_Undefined ){
87269 printf(" undefined");
87270 }else if( p->flags & MEM_Null ){
87271 printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL");
87272 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
87273 printf(" si:%lld", p->u.i);
87274 }else if( (p->flags & (MEM_IntReal))!=0 ){
87275 printf(" ir:%lld", p->u.i);
87276 }else if( p->flags & MEM_Int ){
87277 printf(" i:%lld", p->u.i);
87278#ifndef SQLITE_OMIT_FLOATING_POINT
87279 }else if( p->flags & MEM_Real ){
87280 printf(" r:%.17g", p->u.r);
87281#endif
87282 }else if( sqlite3VdbeMemIsRowSet(p) ){
87283 printf(" (rowset)");
87284 }else{
87285 StrAccum acc;
87286 char zBuf[1000];
87287 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
87288 sqlite3VdbeMemPrettyPrint(p, &acc);
87289 printf(" %s", sqlite3StrAccumFinish(&acc));
87290 }
87291 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
87292}
87293static void registerTrace(int iReg, Mem *p){
87294 printf("R[%d] = ", iReg);
87295 memTracePrint(p);
87296 if( p->pScopyFrom ){
87297 printf(" <== R[%d]", (int)(p->pScopyFrom - &p[-iReg]));
87298 }
87299 printf("\n");
87300 sqlite3VdbeCheckMemInvariants(p);
87301}
87302/**/ void sqlite3PrintMem(Mem *pMem){
87303 memTracePrint(pMem);
87304 printf("\n");
87305 fflush(stdout);
87306}
87307#endif
87308
87309#ifdef SQLITE_DEBUG
87310/*
87311** Show the values of all registers in the virtual machine. Used for
87312** interactive debugging.
87313*/
87314SQLITE_PRIVATE void sqlite3VdbeRegisterDump(Vdbe *v){
87315 int i;
87316 for(i=1; i<v->nMem; i++) registerTrace(i, v->aMem+i);
87317}
87318#endif /* SQLITE_DEBUG */
87319
87320
87321#ifdef SQLITE_DEBUG
87322# define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
87323#else
87324# define REGISTER_TRACE(R,M)
87325#endif
87326
87327
87328#ifdef VDBE_PROFILE
87329
87330/*
87331** hwtime.h contains inline assembler code for implementing
87332** high-performance timing routines.
87333*/
87334/* #include "hwtime.h" */
87335
87336#endif
87337
87338#ifndef NDEBUG
87339/*
87340** This function is only called from within an assert() expression. It
87341** checks that the sqlite3.nTransaction variable is correctly set to
87342** the number of non-transaction savepoints currently in the
87343** linked list starting at sqlite3.pSavepoint.
87344**
87345** Usage:
87346**
87347** assert( checkSavepointCount(db) );
87348*/
87349static int checkSavepointCount(sqlite3 *db){
87350 int n = 0;
87351 Savepoint *p;
87352 for(p=db->pSavepoint; p; p=p->pNext) n++;
87353 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
87354 return 1;
87355}
87356#endif
87357
87358/*
87359** Return the register of pOp->p2 after first preparing it to be
87360** overwritten with an integer value.
87361*/
87362static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){
87363 sqlite3VdbeMemSetNull(pMem: pOut);
87364 pOut->flags = MEM_Int;
87365 return pOut;
87366}
87367static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){
87368 Mem *pOut;
87369 assert( pOp->p2>0 );
87370 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
87371 pOut = &p->aMem[pOp->p2];
87372 memAboutToChange(p, pOut);
87373 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
87374 return out2PrereleaseWithClear(pOut);
87375 }else{
87376 pOut->flags = MEM_Int;
87377 return pOut;
87378 }
87379}
87380
87381/*
87382** Return the symbolic name for the data type of a pMem
87383*/
87384static const char *vdbeMemTypeName(Mem *pMem){
87385 static const char *azTypes[] = {
87386 /* SQLITE_INTEGER */ "INT",
87387 /* SQLITE_FLOAT */ "REAL",
87388 /* SQLITE_TEXT */ "TEXT",
87389 /* SQLITE_BLOB */ "BLOB",
87390 /* SQLITE_NULL */ "NULL"
87391 };
87392 return azTypes[sqlite3_value_type(pVal: pMem)-1];
87393}
87394
87395/*
87396** Execute as much of a VDBE program as we can.
87397** This is the core of sqlite3_step().
87398*/
87399SQLITE_PRIVATE int sqlite3VdbeExec(
87400 Vdbe *p /* The VDBE */
87401){
87402 Op *aOp = p->aOp; /* Copy of p->aOp */
87403 Op *pOp = aOp; /* Current operation */
87404#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
87405 Op *pOrigOp; /* Value of pOp at the top of the loop */
87406#endif
87407#ifdef SQLITE_DEBUG
87408 int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */
87409#endif
87410 int rc = SQLITE_OK; /* Value to return */
87411 sqlite3 *db = p->db; /* The database */
87412 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
87413 u8 encoding = ENC(db); /* The database encoding */
87414 int iCompare = 0; /* Result of last comparison */
87415 u64 nVmStep = 0; /* Number of virtual machine steps */
87416#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
87417 u64 nProgressLimit; /* Invoke xProgress() when nVmStep reaches this */
87418#endif
87419 Mem *aMem = p->aMem; /* Copy of p->aMem */
87420 Mem *pIn1 = 0; /* 1st input operand */
87421 Mem *pIn2 = 0; /* 2nd input operand */
87422 Mem *pIn3 = 0; /* 3rd input operand */
87423 Mem *pOut = 0; /* Output operand */
87424#ifdef VDBE_PROFILE
87425 u64 start; /* CPU clock count at start of opcode */
87426#endif
87427 /*** INSERT STACK UNION HERE ***/
87428
87429 assert( p->iVdbeMagic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
87430 sqlite3VdbeEnter(p);
87431#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
87432 if( db->xProgress ){
87433 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
87434 assert( 0 < db->nProgressOps );
87435 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
87436 }else{
87437 nProgressLimit = LARGEST_UINT64;
87438 }
87439#endif
87440 if( p->rc==SQLITE_NOMEM ){
87441 /* This happens if a malloc() inside a call to sqlite3_column_text() or
87442 ** sqlite3_column_text16() failed. */
87443 goto no_mem;
87444 }
87445 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
87446 testcase( p->rc!=SQLITE_OK );
87447 p->rc = SQLITE_OK;
87448 assert( p->bIsReader || p->readOnly!=0 );
87449 p->iCurrentTime = 0;
87450 assert( p->explain==0 );
87451 p->pResultSet = 0;
87452 db->busyHandler.nBusy = 0;
87453 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
87454 sqlite3VdbeIOTraceSql(p);
87455#ifdef SQLITE_DEBUG
87456 sqlite3BeginBenignMalloc();
87457 if( p->pc==0
87458 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
87459 ){
87460 int i;
87461 int once = 1;
87462 sqlite3VdbePrintSql(p);
87463 if( p->db->flags & SQLITE_VdbeListing ){
87464 printf("VDBE Program Listing:\n");
87465 for(i=0; i<p->nOp; i++){
87466 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
87467 }
87468 }
87469 if( p->db->flags & SQLITE_VdbeEQP ){
87470 for(i=0; i<p->nOp; i++){
87471 if( aOp[i].opcode==OP_Explain ){
87472 if( once ) printf("VDBE Query Plan:\n");
87473 printf("%s\n", aOp[i].p4.z);
87474 once = 0;
87475 }
87476 }
87477 }
87478 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
87479 }
87480 sqlite3EndBenignMalloc();
87481#endif
87482 for(pOp=&aOp[p->pc]; 1; pOp++){
87483 /* Errors are detected by individual opcodes, with an immediate
87484 ** jumps to abort_due_to_error. */
87485 assert( rc==SQLITE_OK );
87486
87487 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
87488#ifdef VDBE_PROFILE
87489 start = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
87490#endif
87491 nVmStep++;
87492#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
87493 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
87494#endif
87495
87496 /* Only allow tracing if SQLITE_DEBUG is defined.
87497 */
87498#ifdef SQLITE_DEBUG
87499 if( db->flags & SQLITE_VdbeTrace ){
87500 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
87501 test_trace_breakpoint((int)(pOp - aOp),pOp,p);
87502 }
87503#endif
87504
87505
87506 /* Check to see if we need to simulate an interrupt. This only happens
87507 ** if we have a special test build.
87508 */
87509#ifdef SQLITE_TEST
87510 if( sqlite3_interrupt_count>0 ){
87511 sqlite3_interrupt_count--;
87512 if( sqlite3_interrupt_count==0 ){
87513 sqlite3_interrupt(db);
87514 }
87515 }
87516#endif
87517
87518 /* Sanity checking on other operands */
87519#ifdef SQLITE_DEBUG
87520 {
87521 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
87522 if( (opProperty & OPFLG_IN1)!=0 ){
87523 assert( pOp->p1>0 );
87524 assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
87525 assert( memIsValid(&aMem[pOp->p1]) );
87526 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
87527 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
87528 }
87529 if( (opProperty & OPFLG_IN2)!=0 ){
87530 assert( pOp->p2>0 );
87531 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
87532 assert( memIsValid(&aMem[pOp->p2]) );
87533 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
87534 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
87535 }
87536 if( (opProperty & OPFLG_IN3)!=0 ){
87537 assert( pOp->p3>0 );
87538 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
87539 assert( memIsValid(&aMem[pOp->p3]) );
87540 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
87541 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
87542 }
87543 if( (opProperty & OPFLG_OUT2)!=0 ){
87544 assert( pOp->p2>0 );
87545 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
87546 memAboutToChange(p, &aMem[pOp->p2]);
87547 }
87548 if( (opProperty & OPFLG_OUT3)!=0 ){
87549 assert( pOp->p3>0 );
87550 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
87551 memAboutToChange(p, &aMem[pOp->p3]);
87552 }
87553 }
87554#endif
87555#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
87556 pOrigOp = pOp;
87557#endif
87558
87559 switch( pOp->opcode ){
87560
87561/*****************************************************************************
87562** What follows is a massive switch statement where each case implements a
87563** separate instruction in the virtual machine. If we follow the usual
87564** indentation conventions, each case should be indented by 6 spaces. But
87565** that is a lot of wasted space on the left margin. So the code within
87566** the switch statement will break with convention and be flush-left. Another
87567** big comment (similar to this one) will mark the point in the code where
87568** we transition back to normal indentation.
87569**
87570** The formatting of each case is important. The makefile for SQLite
87571** generates two C files "opcodes.h" and "opcodes.c" by scanning this
87572** file looking for lines that begin with "case OP_". The opcodes.h files
87573** will be filled with #defines that give unique integer values to each
87574** opcode and the opcodes.c file is filled with an array of strings where
87575** each string is the symbolic name for the corresponding opcode. If the
87576** case statement is followed by a comment of the form "/# same as ... #/"
87577** that comment is used to determine the particular value of the opcode.
87578**
87579** Other keywords in the comment that follows each case are used to
87580** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
87581** Keywords include: in1, in2, in3, out2, out3. See
87582** the mkopcodeh.awk script for additional information.
87583**
87584** Documentation about VDBE opcodes is generated by scanning this file
87585** for lines of that contain "Opcode:". That line and all subsequent
87586** comment lines are used in the generation of the opcode.html documentation
87587** file.
87588**
87589** SUMMARY:
87590**
87591** Formatting is important to scripts that scan this file.
87592** Do not deviate from the formatting style currently in use.
87593**
87594*****************************************************************************/
87595
87596/* Opcode: Goto * P2 * * *
87597**
87598** An unconditional jump to address P2.
87599** The next instruction executed will be
87600** the one at index P2 from the beginning of
87601** the program.
87602**
87603** The P1 parameter is not actually used by this opcode. However, it
87604** is sometimes set to 1 instead of 0 as a hint to the command-line shell
87605** that this Goto is the bottom of a loop and that the lines from P2 down
87606** to the current line should be indented for EXPLAIN output.
87607*/
87608case OP_Goto: { /* jump */
87609
87610#ifdef SQLITE_DEBUG
87611 /* In debuggging mode, when the p5 flags is set on an OP_Goto, that
87612 ** means we should really jump back to the preceeding OP_ReleaseReg
87613 ** instruction. */
87614 if( pOp->p5 ){
87615 assert( pOp->p2 < (int)(pOp - aOp) );
87616 assert( pOp->p2 > 1 );
87617 pOp = &aOp[pOp->p2 - 2];
87618 assert( pOp[1].opcode==OP_ReleaseReg );
87619 goto check_for_interrupt;
87620 }
87621#endif
87622
87623jump_to_p2_and_check_for_interrupt:
87624 pOp = &aOp[pOp->p2 - 1];
87625
87626 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
87627 ** OP_VNext, or OP_SorterNext) all jump here upon
87628 ** completion. Check to see if sqlite3_interrupt() has been called
87629 ** or if the progress callback needs to be invoked.
87630 **
87631 ** This code uses unstructured "goto" statements and does not look clean.
87632 ** But that is not due to sloppy coding habits. The code is written this
87633 ** way for performance, to avoid having to run the interrupt and progress
87634 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
87635 ** faster according to "valgrind --tool=cachegrind" */
87636check_for_interrupt:
87637 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
87638#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
87639 /* Call the progress callback if it is configured and the required number
87640 ** of VDBE ops have been executed (either since this invocation of
87641 ** sqlite3VdbeExec() or since last time the progress callback was called).
87642 ** If the progress callback returns non-zero, exit the virtual machine with
87643 ** a return code SQLITE_ABORT.
87644 */
87645 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
87646 assert( db->nProgressOps!=0 );
87647 nProgressLimit += db->nProgressOps;
87648 if( db->xProgress(db->pProgressArg) ){
87649 nProgressLimit = LARGEST_UINT64;
87650 rc = SQLITE_INTERRUPT;
87651 goto abort_due_to_error;
87652 }
87653 }
87654#endif
87655
87656 break;
87657}
87658
87659/* Opcode: Gosub P1 P2 * * *
87660**
87661** Write the current address onto register P1
87662** and then jump to address P2.
87663*/
87664case OP_Gosub: { /* jump */
87665 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
87666 pIn1 = &aMem[pOp->p1];
87667 assert( VdbeMemDynamic(pIn1)==0 );
87668 memAboutToChange(p, pIn1);
87669 pIn1->flags = MEM_Int;
87670 pIn1->u.i = (int)(pOp-aOp);
87671 REGISTER_TRACE(pOp->p1, pIn1);
87672
87673 /* Most jump operations do a goto to this spot in order to update
87674 ** the pOp pointer. */
87675jump_to_p2:
87676 pOp = &aOp[pOp->p2 - 1];
87677 break;
87678}
87679
87680/* Opcode: Return P1 * * * *
87681**
87682** Jump to the next instruction after the address in register P1. After
87683** the jump, register P1 becomes undefined.
87684*/
87685case OP_Return: { /* in1 */
87686 pIn1 = &aMem[pOp->p1];
87687 assert( pIn1->flags==MEM_Int );
87688 pOp = &aOp[pIn1->u.i];
87689 pIn1->flags = MEM_Undefined;
87690 break;
87691}
87692
87693/* Opcode: InitCoroutine P1 P2 P3 * *
87694**
87695** Set up register P1 so that it will Yield to the coroutine
87696** located at address P3.
87697**
87698** If P2!=0 then the coroutine implementation immediately follows
87699** this opcode. So jump over the coroutine implementation to
87700** address P2.
87701**
87702** See also: EndCoroutine
87703*/
87704case OP_InitCoroutine: { /* jump */
87705 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
87706 assert( pOp->p2>=0 && pOp->p2<p->nOp );
87707 assert( pOp->p3>=0 && pOp->p3<p->nOp );
87708 pOut = &aMem[pOp->p1];
87709 assert( !VdbeMemDynamic(pOut) );
87710 pOut->u.i = pOp->p3 - 1;
87711 pOut->flags = MEM_Int;
87712 if( pOp->p2 ) goto jump_to_p2;
87713 break;
87714}
87715
87716/* Opcode: EndCoroutine P1 * * * *
87717**
87718** The instruction at the address in register P1 is a Yield.
87719** Jump to the P2 parameter of that Yield.
87720** After the jump, register P1 becomes undefined.
87721**
87722** See also: InitCoroutine
87723*/
87724case OP_EndCoroutine: { /* in1 */
87725 VdbeOp *pCaller;
87726 pIn1 = &aMem[pOp->p1];
87727 assert( pIn1->flags==MEM_Int );
87728 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
87729 pCaller = &aOp[pIn1->u.i];
87730 assert( pCaller->opcode==OP_Yield );
87731 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
87732 pOp = &aOp[pCaller->p2 - 1];
87733 pIn1->flags = MEM_Undefined;
87734 break;
87735}
87736
87737/* Opcode: Yield P1 P2 * * *
87738**
87739** Swap the program counter with the value in register P1. This
87740** has the effect of yielding to a coroutine.
87741**
87742** If the coroutine that is launched by this instruction ends with
87743** Yield or Return then continue to the next instruction. But if
87744** the coroutine launched by this instruction ends with
87745** EndCoroutine, then jump to P2 rather than continuing with the
87746** next instruction.
87747**
87748** See also: InitCoroutine
87749*/
87750case OP_Yield: { /* in1, jump */
87751 int pcDest;
87752 pIn1 = &aMem[pOp->p1];
87753 assert( VdbeMemDynamic(pIn1)==0 );
87754 pIn1->flags = MEM_Int;
87755 pcDest = (int)pIn1->u.i;
87756 pIn1->u.i = (int)(pOp - aOp);
87757 REGISTER_TRACE(pOp->p1, pIn1);
87758 pOp = &aOp[pcDest];
87759 break;
87760}
87761
87762/* Opcode: HaltIfNull P1 P2 P3 P4 P5
87763** Synopsis: if r[P3]=null halt
87764**
87765** Check the value in register P3. If it is NULL then Halt using
87766** parameter P1, P2, and P4 as if this were a Halt instruction. If the
87767** value in register P3 is not NULL, then this routine is a no-op.
87768** The P5 parameter should be 1.
87769*/
87770case OP_HaltIfNull: { /* in3 */
87771 pIn3 = &aMem[pOp->p3];
87772#ifdef SQLITE_DEBUG
87773 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
87774#endif
87775 if( (pIn3->flags & MEM_Null)==0 ) break;
87776 /* Fall through into OP_Halt */
87777 /* no break */ deliberate_fall_through
87778}
87779
87780/* Opcode: Halt P1 P2 * P4 P5
87781**
87782** Exit immediately. All open cursors, etc are closed
87783** automatically.
87784**
87785** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
87786** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
87787** For errors, it can be some other value. If P1!=0 then P2 will determine
87788** whether or not to rollback the current transaction. Do not rollback
87789** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
87790** then back out all changes that have occurred during this execution of the
87791** VDBE, but do not rollback the transaction.
87792**
87793** If P4 is not null then it is an error message string.
87794**
87795** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
87796**
87797** 0: (no change)
87798** 1: NOT NULL contraint failed: P4
87799** 2: UNIQUE constraint failed: P4
87800** 3: CHECK constraint failed: P4
87801** 4: FOREIGN KEY constraint failed: P4
87802**
87803** If P5 is not zero and P4 is NULL, then everything after the ":" is
87804** omitted.
87805**
87806** There is an implied "Halt 0 0 0" instruction inserted at the very end of
87807** every program. So a jump past the last instruction of the program
87808** is the same as executing Halt.
87809*/
87810case OP_Halt: {
87811 VdbeFrame *pFrame;
87812 int pcx;
87813
87814 pcx = (int)(pOp - aOp);
87815#ifdef SQLITE_DEBUG
87816 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
87817#endif
87818 if( pOp->p1==SQLITE_OK && p->pFrame ){
87819 /* Halt the sub-program. Return control to the parent frame. */
87820 pFrame = p->pFrame;
87821 p->pFrame = pFrame->pParent;
87822 p->nFrame--;
87823 sqlite3VdbeSetChanges(db, nChange: p->nChange);
87824 pcx = sqlite3VdbeFrameRestore(pFrame);
87825 if( pOp->p2==OE_Ignore ){
87826 /* Instruction pcx is the OP_Program that invoked the sub-program
87827 ** currently being halted. If the p2 instruction of this OP_Halt
87828 ** instruction is set to OE_Ignore, then the sub-program is throwing
87829 ** an IGNORE exception. In this case jump to the address specified
87830 ** as the p2 of the calling OP_Program. */
87831 pcx = p->aOp[pcx].p2-1;
87832 }
87833 aOp = p->aOp;
87834 aMem = p->aMem;
87835 pOp = &aOp[pcx];
87836 break;
87837 }
87838 p->rc = pOp->p1;
87839 p->errorAction = (u8)pOp->p2;
87840 p->pc = pcx;
87841 assert( pOp->p5<=4 );
87842 if( p->rc ){
87843 if( pOp->p5 ){
87844 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
87845 "FOREIGN KEY" };
87846 testcase( pOp->p5==1 );
87847 testcase( pOp->p5==2 );
87848 testcase( pOp->p5==3 );
87849 testcase( pOp->p5==4 );
87850 sqlite3VdbeError(p, zFormat: "%s constraint failed", azType[pOp->p5-1]);
87851 if( pOp->p4.z ){
87852 p->zErrMsg = sqlite3MPrintf(db, zFormat: "%z: %s", p->zErrMsg, pOp->p4.z);
87853 }
87854 }else{
87855 sqlite3VdbeError(p, zFormat: "%s", pOp->p4.z);
87856 }
87857 sqlite3_log(iErrCode: pOp->p1, zFormat: "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
87858 }
87859 rc = sqlite3VdbeHalt(p);
87860 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
87861 if( rc==SQLITE_BUSY ){
87862 p->rc = SQLITE_BUSY;
87863 }else{
87864 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
87865 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
87866 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
87867 }
87868 goto vdbe_return;
87869}
87870
87871/* Opcode: Integer P1 P2 * * *
87872** Synopsis: r[P2]=P1
87873**
87874** The 32-bit integer value P1 is written into register P2.
87875*/
87876case OP_Integer: { /* out2 */
87877 pOut = out2Prerelease(p, pOp);
87878 pOut->u.i = pOp->p1;
87879 break;
87880}
87881
87882/* Opcode: Int64 * P2 * P4 *
87883** Synopsis: r[P2]=P4
87884**
87885** P4 is a pointer to a 64-bit integer value.
87886** Write that value into register P2.
87887*/
87888case OP_Int64: { /* out2 */
87889 pOut = out2Prerelease(p, pOp);
87890 assert( pOp->p4.pI64!=0 );
87891 pOut->u.i = *pOp->p4.pI64;
87892 break;
87893}
87894
87895#ifndef SQLITE_OMIT_FLOATING_POINT
87896/* Opcode: Real * P2 * P4 *
87897** Synopsis: r[P2]=P4
87898**
87899** P4 is a pointer to a 64-bit floating point value.
87900** Write that value into register P2.
87901*/
87902case OP_Real: { /* same as TK_FLOAT, out2 */
87903 pOut = out2Prerelease(p, pOp);
87904 pOut->flags = MEM_Real;
87905 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
87906 pOut->u.r = *pOp->p4.pReal;
87907 break;
87908}
87909#endif
87910
87911/* Opcode: String8 * P2 * P4 *
87912** Synopsis: r[P2]='P4'
87913**
87914** P4 points to a nul terminated UTF-8 string. This opcode is transformed
87915** into a String opcode before it is executed for the first time. During
87916** this transformation, the length of string P4 is computed and stored
87917** as the P1 parameter.
87918*/
87919case OP_String8: { /* same as TK_STRING, out2 */
87920 assert( pOp->p4.z!=0 );
87921 pOut = out2Prerelease(p, pOp);
87922 pOp->p1 = sqlite3Strlen30(z: pOp->p4.z);
87923
87924#ifndef SQLITE_OMIT_UTF16
87925 if( encoding!=SQLITE_UTF8 ){
87926 rc = sqlite3VdbeMemSetStr(pMem: pOut, z: pOp->p4.z, n: -1, SQLITE_UTF8, SQLITE_STATIC);
87927 assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG );
87928 if( rc ) goto too_big;
87929 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pMem: pOut, desiredEnc: encoding) ) goto no_mem;
87930 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
87931 assert( VdbeMemDynamic(pOut)==0 );
87932 pOut->szMalloc = 0;
87933 pOut->flags |= MEM_Static;
87934 if( pOp->p4type==P4_DYNAMIC ){
87935 sqlite3DbFree(db, p: pOp->p4.z);
87936 }
87937 pOp->p4type = P4_DYNAMIC;
87938 pOp->p4.z = pOut->z;
87939 pOp->p1 = pOut->n;
87940 }
87941#endif
87942 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
87943 goto too_big;
87944 }
87945 pOp->opcode = OP_String;
87946 assert( rc==SQLITE_OK );
87947 /* Fall through to the next case, OP_String */
87948 /* no break */ deliberate_fall_through
87949}
87950
87951/* Opcode: String P1 P2 P3 P4 P5
87952** Synopsis: r[P2]='P4' (len=P1)
87953**
87954** The string value P4 of length P1 (bytes) is stored in register P2.
87955**
87956** If P3 is not zero and the content of register P3 is equal to P5, then
87957** the datatype of the register P2 is converted to BLOB. The content is
87958** the same sequence of bytes, it is merely interpreted as a BLOB instead
87959** of a string, as if it had been CAST. In other words:
87960**
87961** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB)
87962*/
87963case OP_String: { /* out2 */
87964 assert( pOp->p4.z!=0 );
87965 pOut = out2Prerelease(p, pOp);
87966 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
87967 pOut->z = pOp->p4.z;
87968 pOut->n = pOp->p1;
87969 pOut->enc = encoding;
87970 UPDATE_MAX_BLOBSIZE(pOut);
87971#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
87972 if( pOp->p3>0 ){
87973 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
87974 pIn3 = &aMem[pOp->p3];
87975 assert( pIn3->flags & MEM_Int );
87976 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
87977 }
87978#endif
87979 break;
87980}
87981
87982/* Opcode: Null P1 P2 P3 * *
87983** Synopsis: r[P2..P3]=NULL
87984**
87985** Write a NULL into registers P2. If P3 greater than P2, then also write
87986** NULL into register P3 and every register in between P2 and P3. If P3
87987** is less than P2 (typically P3 is zero) then only register P2 is
87988** set to NULL.
87989**
87990** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
87991** NULL values will not compare equal even if SQLITE_NULLEQ is set on
87992** OP_Ne or OP_Eq.
87993*/
87994case OP_Null: { /* out2 */
87995 int cnt;
87996 u16 nullFlag;
87997 pOut = out2Prerelease(p, pOp);
87998 cnt = pOp->p3-pOp->p2;
87999 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
88000 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
88001 pOut->n = 0;
88002#ifdef SQLITE_DEBUG
88003 pOut->uTemp = 0;
88004#endif
88005 while( cnt>0 ){
88006 pOut++;
88007 memAboutToChange(p, pOut);
88008 sqlite3VdbeMemSetNull(pMem: pOut);
88009 pOut->flags = nullFlag;
88010 pOut->n = 0;
88011 cnt--;
88012 }
88013 break;
88014}
88015
88016/* Opcode: SoftNull P1 * * * *
88017** Synopsis: r[P1]=NULL
88018**
88019** Set register P1 to have the value NULL as seen by the OP_MakeRecord
88020** instruction, but do not free any string or blob memory associated with
88021** the register, so that if the value was a string or blob that was
88022** previously copied using OP_SCopy, the copies will continue to be valid.
88023*/
88024case OP_SoftNull: {
88025 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
88026 pOut = &aMem[pOp->p1];
88027 pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null;
88028 break;
88029}
88030
88031/* Opcode: Blob P1 P2 * P4 *
88032** Synopsis: r[P2]=P4 (len=P1)
88033**
88034** P4 points to a blob of data P1 bytes long. Store this
88035** blob in register P2.
88036*/
88037case OP_Blob: { /* out2 */
88038 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
88039 pOut = out2Prerelease(p, pOp);
88040 sqlite3VdbeMemSetStr(pMem: pOut, z: pOp->p4.z, n: pOp->p1, enc: 0, xDel: 0);
88041 pOut->enc = encoding;
88042 UPDATE_MAX_BLOBSIZE(pOut);
88043 break;
88044}
88045
88046/* Opcode: Variable P1 P2 * P4 *
88047** Synopsis: r[P2]=parameter(P1,P4)
88048**
88049** Transfer the values of bound parameter P1 into register P2
88050**
88051** If the parameter is named, then its name appears in P4.
88052** The P4 value is used by sqlite3_bind_parameter_name().
88053*/
88054case OP_Variable: { /* out2 */
88055 Mem *pVar; /* Value being transferred */
88056
88057 assert( pOp->p1>0 && pOp->p1<=p->nVar );
88058 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
88059 pVar = &p->aVar[pOp->p1 - 1];
88060 if( sqlite3VdbeMemTooBig(p: pVar) ){
88061 goto too_big;
88062 }
88063 pOut = &aMem[pOp->p2];
88064 if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pMem: pOut);
88065 memcpy(dest: pOut, src: pVar, MEMCELLSIZE);
88066 pOut->flags &= ~(MEM_Dyn|MEM_Ephem);
88067 pOut->flags |= MEM_Static|MEM_FromBind;
88068 UPDATE_MAX_BLOBSIZE(pOut);
88069 break;
88070}
88071
88072/* Opcode: Move P1 P2 P3 * *
88073** Synopsis: r[P2@P3]=r[P1@P3]
88074**
88075** Move the P3 values in register P1..P1+P3-1 over into
88076** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
88077** left holding a NULL. It is an error for register ranges
88078** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
88079** for P3 to be less than 1.
88080*/
88081case OP_Move: {
88082 int n; /* Number of registers left to copy */
88083 int p1; /* Register to copy from */
88084 int p2; /* Register to copy to */
88085
88086 n = pOp->p3;
88087 p1 = pOp->p1;
88088 p2 = pOp->p2;
88089 assert( n>0 && p1>0 && p2>0 );
88090 assert( p1+n<=p2 || p2+n<=p1 );
88091
88092 pIn1 = &aMem[p1];
88093 pOut = &aMem[p2];
88094 do{
88095 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
88096 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
88097 assert( memIsValid(pIn1) );
88098 memAboutToChange(p, pOut);
88099 sqlite3VdbeMemMove(pTo: pOut, pFrom: pIn1);
88100#ifdef SQLITE_DEBUG
88101 pIn1->pScopyFrom = 0;
88102 { int i;
88103 for(i=1; i<p->nMem; i++){
88104 if( aMem[i].pScopyFrom==pIn1 ){
88105 aMem[i].pScopyFrom = pOut;
88106 }
88107 }
88108 }
88109#endif
88110 Deephemeralize(pOut);
88111 REGISTER_TRACE(p2++, pOut);
88112 pIn1++;
88113 pOut++;
88114 }while( --n );
88115 break;
88116}
88117
88118/* Opcode: Copy P1 P2 P3 * *
88119** Synopsis: r[P2@P3+1]=r[P1@P3+1]
88120**
88121** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
88122**
88123** This instruction makes a deep copy of the value. A duplicate
88124** is made of any string or blob constant. See also OP_SCopy.
88125*/
88126case OP_Copy: {
88127 int n;
88128
88129 n = pOp->p3;
88130 pIn1 = &aMem[pOp->p1];
88131 pOut = &aMem[pOp->p2];
88132 assert( pOut!=pIn1 );
88133 while( 1 ){
88134 memAboutToChange(p, pOut);
88135 sqlite3VdbeMemShallowCopy(pTo: pOut, pFrom: pIn1, MEM_Ephem);
88136 Deephemeralize(pOut);
88137#ifdef SQLITE_DEBUG
88138 pOut->pScopyFrom = 0;
88139#endif
88140 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
88141 if( (n--)==0 ) break;
88142 pOut++;
88143 pIn1++;
88144 }
88145 break;
88146}
88147
88148/* Opcode: SCopy P1 P2 * * *
88149** Synopsis: r[P2]=r[P1]
88150**
88151** Make a shallow copy of register P1 into register P2.
88152**
88153** This instruction makes a shallow copy of the value. If the value
88154** is a string or blob, then the copy is only a pointer to the
88155** original and hence if the original changes so will the copy.
88156** Worse, if the original is deallocated, the copy becomes invalid.
88157** Thus the program must guarantee that the original will not change
88158** during the lifetime of the copy. Use OP_Copy to make a complete
88159** copy.
88160*/
88161case OP_SCopy: { /* out2 */
88162 pIn1 = &aMem[pOp->p1];
88163 pOut = &aMem[pOp->p2];
88164 assert( pOut!=pIn1 );
88165 sqlite3VdbeMemShallowCopy(pTo: pOut, pFrom: pIn1, MEM_Ephem);
88166#ifdef SQLITE_DEBUG
88167 pOut->pScopyFrom = pIn1;
88168 pOut->mScopyFlags = pIn1->flags;
88169#endif
88170 break;
88171}
88172
88173/* Opcode: IntCopy P1 P2 * * *
88174** Synopsis: r[P2]=r[P1]
88175**
88176** Transfer the integer value held in register P1 into register P2.
88177**
88178** This is an optimized version of SCopy that works only for integer
88179** values.
88180*/
88181case OP_IntCopy: { /* out2 */
88182 pIn1 = &aMem[pOp->p1];
88183 assert( (pIn1->flags & MEM_Int)!=0 );
88184 pOut = &aMem[pOp->p2];
88185 sqlite3VdbeMemSetInt64(pMem: pOut, val: pIn1->u.i);
88186 break;
88187}
88188
88189/* Opcode: ChngCntRow P1 P2 * * *
88190** Synopsis: output=r[P1]
88191**
88192** Output value in register P1 as the chance count for a DML statement,
88193** due to the "PRAGMA count_changes=ON" setting. Or, if there was a
88194** foreign key error in the statement, trigger the error now.
88195**
88196** This opcode is a variant of OP_ResultRow that checks the foreign key
88197** immediate constraint count and throws an error if the count is
88198** non-zero. The P2 opcode must be 1.
88199*/
88200case OP_ChngCntRow: {
88201 assert( pOp->p2==1 );
88202 if( (rc = sqlite3VdbeCheckFk(p,deferred: 0))!=SQLITE_OK ){
88203 goto abort_due_to_error;
88204 }
88205 /* Fall through to the next case, OP_ResultRow */
88206 /* no break */ deliberate_fall_through
88207}
88208
88209/* Opcode: ResultRow P1 P2 * * *
88210** Synopsis: output=r[P1@P2]
88211**
88212** The registers P1 through P1+P2-1 contain a single row of
88213** results. This opcode causes the sqlite3_step() call to terminate
88214** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
88215** structure to provide access to the r(P1)..r(P1+P2-1) values as
88216** the result row.
88217*/
88218case OP_ResultRow: {
88219 Mem *pMem;
88220 int i;
88221 assert( p->nResColumn==pOp->p2 );
88222 assert( pOp->p1>0 || CORRUPT_DB );
88223 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
88224
88225 /* Invalidate all ephemeral cursor row caches */
88226 p->cacheCtr = (p->cacheCtr + 2)|1;
88227
88228 /* Make sure the results of the current row are \000 terminated
88229 ** and have an assigned type. The results are de-ephemeralized as
88230 ** a side effect.
88231 */
88232 pMem = p->pResultSet = &aMem[pOp->p1];
88233 for(i=0; i<pOp->p2; i++){
88234 assert( memIsValid(&pMem[i]) );
88235 Deephemeralize(&pMem[i]);
88236 assert( (pMem[i].flags & MEM_Ephem)==0
88237 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
88238 sqlite3VdbeMemNulTerminate(pMem: &pMem[i]);
88239 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
88240#ifdef SQLITE_DEBUG
88241 /* The registers in the result will not be used again when the
88242 ** prepared statement restarts. This is because sqlite3_column()
88243 ** APIs might have caused type conversions of made other changes to
88244 ** the register values. Therefore, we can go ahead and break any
88245 ** OP_SCopy dependencies. */
88246 pMem[i].pScopyFrom = 0;
88247#endif
88248 }
88249 if( db->mallocFailed ) goto no_mem;
88250
88251 if( db->mTrace & SQLITE_TRACE_ROW ){
88252 db->trace.xV2(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
88253 }
88254
88255
88256 /* Return SQLITE_ROW
88257 */
88258 p->pc = (int)(pOp - aOp) + 1;
88259 rc = SQLITE_ROW;
88260 goto vdbe_return;
88261}
88262
88263/* Opcode: Concat P1 P2 P3 * *
88264** Synopsis: r[P3]=r[P2]+r[P1]
88265**
88266** Add the text in register P1 onto the end of the text in
88267** register P2 and store the result in register P3.
88268** If either the P1 or P2 text are NULL then store NULL in P3.
88269**
88270** P3 = P2 || P1
88271**
88272** It is illegal for P1 and P3 to be the same register. Sometimes,
88273** if P3 is the same register as P2, the implementation is able
88274** to avoid a memcpy().
88275*/
88276case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
88277 i64 nByte; /* Total size of the output string or blob */
88278 u16 flags1; /* Initial flags for P1 */
88279 u16 flags2; /* Initial flags for P2 */
88280
88281 pIn1 = &aMem[pOp->p1];
88282 pIn2 = &aMem[pOp->p2];
88283 pOut = &aMem[pOp->p3];
88284 testcase( pOut==pIn2 );
88285 assert( pIn1!=pOut );
88286 flags1 = pIn1->flags;
88287 testcase( flags1 & MEM_Null );
88288 testcase( pIn2->flags & MEM_Null );
88289 if( (flags1 | pIn2->flags) & MEM_Null ){
88290 sqlite3VdbeMemSetNull(pMem: pOut);
88291 break;
88292 }
88293 if( (flags1 & (MEM_Str|MEM_Blob))==0 ){
88294 if( sqlite3VdbeMemStringify(pMem: pIn1,enc: encoding,bForce: 0) ) goto no_mem;
88295 flags1 = pIn1->flags & ~MEM_Str;
88296 }else if( (flags1 & MEM_Zero)!=0 ){
88297 if( sqlite3VdbeMemExpandBlob(pMem: pIn1) ) goto no_mem;
88298 flags1 = pIn1->flags & ~MEM_Str;
88299 }
88300 flags2 = pIn2->flags;
88301 if( (flags2 & (MEM_Str|MEM_Blob))==0 ){
88302 if( sqlite3VdbeMemStringify(pMem: pIn2,enc: encoding,bForce: 0) ) goto no_mem;
88303 flags2 = pIn2->flags & ~MEM_Str;
88304 }else if( (flags2 & MEM_Zero)!=0 ){
88305 if( sqlite3VdbeMemExpandBlob(pMem: pIn2) ) goto no_mem;
88306 flags2 = pIn2->flags & ~MEM_Str;
88307 }
88308 nByte = pIn1->n + pIn2->n;
88309 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
88310 goto too_big;
88311 }
88312 if( sqlite3VdbeMemGrow(pMem: pOut, n: (int)nByte+3, bPreserve: pOut==pIn2) ){
88313 goto no_mem;
88314 }
88315 MemSetTypeFlag(pOut, MEM_Str);
88316 if( pOut!=pIn2 ){
88317 memcpy(dest: pOut->z, src: pIn2->z, n: pIn2->n);
88318 assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) );
88319 pIn2->flags = flags2;
88320 }
88321 memcpy(dest: &pOut->z[pIn2->n], src: pIn1->z, n: pIn1->n);
88322 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
88323 pIn1->flags = flags1;
88324 pOut->z[nByte]=0;
88325 pOut->z[nByte+1] = 0;
88326 pOut->z[nByte+2] = 0;
88327 pOut->flags |= MEM_Term;
88328 pOut->n = (int)nByte;
88329 pOut->enc = encoding;
88330 UPDATE_MAX_BLOBSIZE(pOut);
88331 break;
88332}
88333
88334/* Opcode: Add P1 P2 P3 * *
88335** Synopsis: r[P3]=r[P1]+r[P2]
88336**
88337** Add the value in register P1 to the value in register P2
88338** and store the result in register P3.
88339** If either input is NULL, the result is NULL.
88340*/
88341/* Opcode: Multiply P1 P2 P3 * *
88342** Synopsis: r[P3]=r[P1]*r[P2]
88343**
88344**
88345** Multiply the value in register P1 by the value in register P2
88346** and store the result in register P3.
88347** If either input is NULL, the result is NULL.
88348*/
88349/* Opcode: Subtract P1 P2 P3 * *
88350** Synopsis: r[P3]=r[P2]-r[P1]
88351**
88352** Subtract the value in register P1 from the value in register P2
88353** and store the result in register P3.
88354** If either input is NULL, the result is NULL.
88355*/
88356/* Opcode: Divide P1 P2 P3 * *
88357** Synopsis: r[P3]=r[P2]/r[P1]
88358**
88359** Divide the value in register P1 by the value in register P2
88360** and store the result in register P3 (P3=P2/P1). If the value in
88361** register P1 is zero, then the result is NULL. If either input is
88362** NULL, the result is NULL.
88363*/
88364/* Opcode: Remainder P1 P2 P3 * *
88365** Synopsis: r[P3]=r[P2]%r[P1]
88366**
88367** Compute the remainder after integer register P2 is divided by
88368** register P1 and store the result in register P3.
88369** If the value in register P1 is zero the result is NULL.
88370** If either operand is NULL, the result is NULL.
88371*/
88372case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
88373case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
88374case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
88375case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
88376case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
88377 u16 flags; /* Combined MEM_* flags from both inputs */
88378 u16 type1; /* Numeric type of left operand */
88379 u16 type2; /* Numeric type of right operand */
88380 i64 iA; /* Integer value of left operand */
88381 i64 iB; /* Integer value of right operand */
88382 double rA; /* Real value of left operand */
88383 double rB; /* Real value of right operand */
88384
88385 pIn1 = &aMem[pOp->p1];
88386 type1 = numericType(pMem: pIn1);
88387 pIn2 = &aMem[pOp->p2];
88388 type2 = numericType(pMem: pIn2);
88389 pOut = &aMem[pOp->p3];
88390 flags = pIn1->flags | pIn2->flags;
88391 if( (type1 & type2 & MEM_Int)!=0 ){
88392 iA = pIn1->u.i;
88393 iB = pIn2->u.i;
88394 switch( pOp->opcode ){
88395 case OP_Add: if( sqlite3AddInt64(pA: &iB,iB: iA) ) goto fp_math; break;
88396 case OP_Subtract: if( sqlite3SubInt64(pA: &iB,iB: iA) ) goto fp_math; break;
88397 case OP_Multiply: if( sqlite3MulInt64(pA: &iB,iB: iA) ) goto fp_math; break;
88398 case OP_Divide: {
88399 if( iA==0 ) goto arithmetic_result_is_null;
88400 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
88401 iB /= iA;
88402 break;
88403 }
88404 default: {
88405 if( iA==0 ) goto arithmetic_result_is_null;
88406 if( iA==-1 ) iA = 1;
88407 iB %= iA;
88408 break;
88409 }
88410 }
88411 pOut->u.i = iB;
88412 MemSetTypeFlag(pOut, MEM_Int);
88413 }else if( (flags & MEM_Null)!=0 ){
88414 goto arithmetic_result_is_null;
88415 }else{
88416fp_math:
88417 rA = sqlite3VdbeRealValue(pMem: pIn1);
88418 rB = sqlite3VdbeRealValue(pMem: pIn2);
88419 switch( pOp->opcode ){
88420 case OP_Add: rB += rA; break;
88421 case OP_Subtract: rB -= rA; break;
88422 case OP_Multiply: rB *= rA; break;
88423 case OP_Divide: {
88424 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
88425 if( rA==(double)0 ) goto arithmetic_result_is_null;
88426 rB /= rA;
88427 break;
88428 }
88429 default: {
88430 iA = sqlite3VdbeIntValue(pMem: pIn1);
88431 iB = sqlite3VdbeIntValue(pMem: pIn2);
88432 if( iA==0 ) goto arithmetic_result_is_null;
88433 if( iA==-1 ) iA = 1;
88434 rB = (double)(iB % iA);
88435 break;
88436 }
88437 }
88438#ifdef SQLITE_OMIT_FLOATING_POINT
88439 pOut->u.i = rB;
88440 MemSetTypeFlag(pOut, MEM_Int);
88441#else
88442 if( sqlite3IsNaN(x: rB) ){
88443 goto arithmetic_result_is_null;
88444 }
88445 pOut->u.r = rB;
88446 MemSetTypeFlag(pOut, MEM_Real);
88447#endif
88448 }
88449 break;
88450
88451arithmetic_result_is_null:
88452 sqlite3VdbeMemSetNull(pMem: pOut);
88453 break;
88454}
88455
88456/* Opcode: CollSeq P1 * * P4
88457**
88458** P4 is a pointer to a CollSeq object. If the next call to a user function
88459** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
88460** be returned. This is used by the built-in min(), max() and nullif()
88461** functions.
88462**
88463** If P1 is not zero, then it is a register that a subsequent min() or
88464** max() aggregate will set to 1 if the current row is not the minimum or
88465** maximum. The P1 register is initialized to 0 by this instruction.
88466**
88467** The interface used by the implementation of the aforementioned functions
88468** to retrieve the collation sequence set by this opcode is not available
88469** publicly. Only built-in functions have access to this feature.
88470*/
88471case OP_CollSeq: {
88472 assert( pOp->p4type==P4_COLLSEQ );
88473 if( pOp->p1 ){
88474 sqlite3VdbeMemSetInt64(pMem: &aMem[pOp->p1], val: 0);
88475 }
88476 break;
88477}
88478
88479/* Opcode: BitAnd P1 P2 P3 * *
88480** Synopsis: r[P3]=r[P1]&r[P2]
88481**
88482** Take the bit-wise AND of the values in register P1 and P2 and
88483** store the result in register P3.
88484** If either input is NULL, the result is NULL.
88485*/
88486/* Opcode: BitOr P1 P2 P3 * *
88487** Synopsis: r[P3]=r[P1]|r[P2]
88488**
88489** Take the bit-wise OR of the values in register P1 and P2 and
88490** store the result in register P3.
88491** If either input is NULL, the result is NULL.
88492*/
88493/* Opcode: ShiftLeft P1 P2 P3 * *
88494** Synopsis: r[P3]=r[P2]<<r[P1]
88495**
88496** Shift the integer value in register P2 to the left by the
88497** number of bits specified by the integer in register P1.
88498** Store the result in register P3.
88499** If either input is NULL, the result is NULL.
88500*/
88501/* Opcode: ShiftRight P1 P2 P3 * *
88502** Synopsis: r[P3]=r[P2]>>r[P1]
88503**
88504** Shift the integer value in register P2 to the right by the
88505** number of bits specified by the integer in register P1.
88506** Store the result in register P3.
88507** If either input is NULL, the result is NULL.
88508*/
88509case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
88510case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
88511case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
88512case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
88513 i64 iA;
88514 u64 uA;
88515 i64 iB;
88516 u8 op;
88517
88518 pIn1 = &aMem[pOp->p1];
88519 pIn2 = &aMem[pOp->p2];
88520 pOut = &aMem[pOp->p3];
88521 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
88522 sqlite3VdbeMemSetNull(pMem: pOut);
88523 break;
88524 }
88525 iA = sqlite3VdbeIntValue(pMem: pIn2);
88526 iB = sqlite3VdbeIntValue(pMem: pIn1);
88527 op = pOp->opcode;
88528 if( op==OP_BitAnd ){
88529 iA &= iB;
88530 }else if( op==OP_BitOr ){
88531 iA |= iB;
88532 }else if( iB!=0 ){
88533 assert( op==OP_ShiftRight || op==OP_ShiftLeft );
88534
88535 /* If shifting by a negative amount, shift in the other direction */
88536 if( iB<0 ){
88537 assert( OP_ShiftRight==OP_ShiftLeft+1 );
88538 op = 2*OP_ShiftLeft + 1 - op;
88539 iB = iB>(-64) ? -iB : 64;
88540 }
88541
88542 if( iB>=64 ){
88543 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
88544 }else{
88545 memcpy(dest: &uA, src: &iA, n: sizeof(uA));
88546 if( op==OP_ShiftLeft ){
88547 uA <<= iB;
88548 }else{
88549 uA >>= iB;
88550 /* Sign-extend on a right shift of a negative number */
88551 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
88552 }
88553 memcpy(dest: &iA, src: &uA, n: sizeof(iA));
88554 }
88555 }
88556 pOut->u.i = iA;
88557 MemSetTypeFlag(pOut, MEM_Int);
88558 break;
88559}
88560
88561/* Opcode: AddImm P1 P2 * * *
88562** Synopsis: r[P1]=r[P1]+P2
88563**
88564** Add the constant P2 to the value in register P1.
88565** The result is always an integer.
88566**
88567** To force any register to be an integer, just add 0.
88568*/
88569case OP_AddImm: { /* in1 */
88570 pIn1 = &aMem[pOp->p1];
88571 memAboutToChange(p, pIn1);
88572 sqlite3VdbeMemIntegerify(pMem: pIn1);
88573 pIn1->u.i += pOp->p2;
88574 break;
88575}
88576
88577/* Opcode: MustBeInt P1 P2 * * *
88578**
88579** Force the value in register P1 to be an integer. If the value
88580** in P1 is not an integer and cannot be converted into an integer
88581** without data loss, then jump immediately to P2, or if P2==0
88582** raise an SQLITE_MISMATCH exception.
88583*/
88584case OP_MustBeInt: { /* jump, in1 */
88585 pIn1 = &aMem[pOp->p1];
88586 if( (pIn1->flags & MEM_Int)==0 ){
88587 applyAffinity(pRec: pIn1, SQLITE_AFF_NUMERIC, enc: encoding);
88588 if( (pIn1->flags & MEM_Int)==0 ){
88589 VdbeBranchTaken(1, 2);
88590 if( pOp->p2==0 ){
88591 rc = SQLITE_MISMATCH;
88592 goto abort_due_to_error;
88593 }else{
88594 goto jump_to_p2;
88595 }
88596 }
88597 }
88598 VdbeBranchTaken(0, 2);
88599 MemSetTypeFlag(pIn1, MEM_Int);
88600 break;
88601}
88602
88603#ifndef SQLITE_OMIT_FLOATING_POINT
88604/* Opcode: RealAffinity P1 * * * *
88605**
88606** If register P1 holds an integer convert it to a real value.
88607**
88608** This opcode is used when extracting information from a column that
88609** has REAL affinity. Such column values may still be stored as
88610** integers, for space efficiency, but after extraction we want them
88611** to have only a real value.
88612*/
88613case OP_RealAffinity: { /* in1 */
88614 pIn1 = &aMem[pOp->p1];
88615 if( pIn1->flags & (MEM_Int|MEM_IntReal) ){
88616 testcase( pIn1->flags & MEM_Int );
88617 testcase( pIn1->flags & MEM_IntReal );
88618 sqlite3VdbeMemRealify(pMem: pIn1);
88619 REGISTER_TRACE(pOp->p1, pIn1);
88620 }
88621 break;
88622}
88623#endif
88624
88625#ifndef SQLITE_OMIT_CAST
88626/* Opcode: Cast P1 P2 * * *
88627** Synopsis: affinity(r[P1])
88628**
88629** Force the value in register P1 to be the type defined by P2.
88630**
88631** <ul>
88632** <li> P2=='A' &rarr; BLOB
88633** <li> P2=='B' &rarr; TEXT
88634** <li> P2=='C' &rarr; NUMERIC
88635** <li> P2=='D' &rarr; INTEGER
88636** <li> P2=='E' &rarr; REAL
88637** </ul>
88638**
88639** A NULL value is not changed by this routine. It remains NULL.
88640*/
88641case OP_Cast: { /* in1 */
88642 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
88643 testcase( pOp->p2==SQLITE_AFF_TEXT );
88644 testcase( pOp->p2==SQLITE_AFF_BLOB );
88645 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
88646 testcase( pOp->p2==SQLITE_AFF_INTEGER );
88647 testcase( pOp->p2==SQLITE_AFF_REAL );
88648 pIn1 = &aMem[pOp->p1];
88649 memAboutToChange(p, pIn1);
88650 rc = ExpandBlob(pIn1);
88651 if( rc ) goto abort_due_to_error;
88652 rc = sqlite3VdbeMemCast(pMem: pIn1, aff: pOp->p2, encoding);
88653 if( rc ) goto abort_due_to_error;
88654 UPDATE_MAX_BLOBSIZE(pIn1);
88655 REGISTER_TRACE(pOp->p1, pIn1);
88656 break;
88657}
88658#endif /* SQLITE_OMIT_CAST */
88659
88660/* Opcode: Eq P1 P2 P3 P4 P5
88661** Synopsis: IF r[P3]==r[P1]
88662**
88663** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then
88664** jump to address P2.
88665**
88666** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
88667** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
88668** to coerce both inputs according to this affinity before the
88669** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
88670** affinity is used. Note that the affinity conversions are stored
88671** back into the input registers P1 and P3. So this opcode can cause
88672** persistent changes to registers P1 and P3.
88673**
88674** Once any conversions have taken place, and neither value is NULL,
88675** the values are compared. If both values are blobs then memcmp() is
88676** used to determine the results of the comparison. If both values
88677** are text, then the appropriate collating function specified in
88678** P4 is used to do the comparison. If P4 is not specified then
88679** memcmp() is used to compare text string. If both values are
88680** numeric, then a numeric comparison is used. If the two values
88681** are of different types, then numbers are considered less than
88682** strings and strings are considered less than blobs.
88683**
88684** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
88685** true or false and is never NULL. If both operands are NULL then the result
88686** of comparison is true. If either operand is NULL then the result is false.
88687** If neither operand is NULL the result is the same as it would be if
88688** the SQLITE_NULLEQ flag were omitted from P5.
88689**
88690** This opcode saves the result of comparison for use by the new
88691** OP_Jump opcode.
88692*/
88693/* Opcode: Ne P1 P2 P3 P4 P5
88694** Synopsis: IF r[P3]!=r[P1]
88695**
88696** This works just like the Eq opcode except that the jump is taken if
88697** the operands in registers P1 and P3 are not equal. See the Eq opcode for
88698** additional information.
88699*/
88700/* Opcode: Lt P1 P2 P3 P4 P5
88701** Synopsis: IF r[P3]<r[P1]
88702**
88703** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
88704** jump to address P2.
88705**
88706** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
88707** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL
88708** bit is clear then fall through if either operand is NULL.
88709**
88710** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
88711** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
88712** to coerce both inputs according to this affinity before the
88713** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
88714** affinity is used. Note that the affinity conversions are stored
88715** back into the input registers P1 and P3. So this opcode can cause
88716** persistent changes to registers P1 and P3.
88717**
88718** Once any conversions have taken place, and neither value is NULL,
88719** the values are compared. If both values are blobs then memcmp() is
88720** used to determine the results of the comparison. If both values
88721** are text, then the appropriate collating function specified in
88722** P4 is used to do the comparison. If P4 is not specified then
88723** memcmp() is used to compare text string. If both values are
88724** numeric, then a numeric comparison is used. If the two values
88725** are of different types, then numbers are considered less than
88726** strings and strings are considered less than blobs.
88727**
88728** This opcode saves the result of comparison for use by the new
88729** OP_Jump opcode.
88730*/
88731/* Opcode: Le P1 P2 P3 P4 P5
88732** Synopsis: IF r[P3]<=r[P1]
88733**
88734** This works just like the Lt opcode except that the jump is taken if
88735** the content of register P3 is less than or equal to the content of
88736** register P1. See the Lt opcode for additional information.
88737*/
88738/* Opcode: Gt P1 P2 P3 P4 P5
88739** Synopsis: IF r[P3]>r[P1]
88740**
88741** This works just like the Lt opcode except that the jump is taken if
88742** the content of register P3 is greater than the content of
88743** register P1. See the Lt opcode for additional information.
88744*/
88745/* Opcode: Ge P1 P2 P3 P4 P5
88746** Synopsis: IF r[P3]>=r[P1]
88747**
88748** This works just like the Lt opcode except that the jump is taken if
88749** the content of register P3 is greater than or equal to the content of
88750** register P1. See the Lt opcode for additional information.
88751*/
88752case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
88753case OP_Ne: /* same as TK_NE, jump, in1, in3 */
88754case OP_Lt: /* same as TK_LT, jump, in1, in3 */
88755case OP_Le: /* same as TK_LE, jump, in1, in3 */
88756case OP_Gt: /* same as TK_GT, jump, in1, in3 */
88757case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
88758 int res, res2; /* Result of the comparison of pIn1 against pIn3 */
88759 char affinity; /* Affinity to use for comparison */
88760 u16 flags1; /* Copy of initial value of pIn1->flags */
88761 u16 flags3; /* Copy of initial value of pIn3->flags */
88762
88763 pIn1 = &aMem[pOp->p1];
88764 pIn3 = &aMem[pOp->p3];
88765 flags1 = pIn1->flags;
88766 flags3 = pIn3->flags;
88767 if( (flags1 & flags3 & MEM_Int)!=0 ){
88768 assert( (pOp->p5 & SQLITE_AFF_MASK)!=SQLITE_AFF_TEXT || CORRUPT_DB );
88769 /* Common case of comparison of two integers */
88770 if( pIn3->u.i > pIn1->u.i ){
88771 iCompare = +1;
88772 if( sqlite3aGTb[pOp->opcode] ){
88773 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
88774 goto jump_to_p2;
88775 }
88776 }else if( pIn3->u.i < pIn1->u.i ){
88777 iCompare = -1;
88778 if( sqlite3aLTb[pOp->opcode] ){
88779 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
88780 goto jump_to_p2;
88781 }
88782 }else{
88783 iCompare = 0;
88784 if( sqlite3aEQb[pOp->opcode] ){
88785 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
88786 goto jump_to_p2;
88787 }
88788 }
88789 VdbeBranchTaken(0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
88790 break;
88791 }
88792 if( (flags1 | flags3)&MEM_Null ){
88793 /* One or both operands are NULL */
88794 if( pOp->p5 & SQLITE_NULLEQ ){
88795 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
88796 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
88797 ** or not both operands are null.
88798 */
88799 assert( (flags1 & MEM_Cleared)==0 );
88800 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB );
88801 testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 );
88802 if( (flags1&flags3&MEM_Null)!=0
88803 && (flags3&MEM_Cleared)==0
88804 ){
88805 res = 0; /* Operands are equal */
88806 }else{
88807 res = ((flags3 & MEM_Null) ? -1 : +1); /* Operands are not equal */
88808 }
88809 }else{
88810 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
88811 ** then the result is always NULL.
88812 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
88813 */
88814 iCompare = 1; /* Operands are not equal */
88815 VdbeBranchTaken(2,3);
88816 if( pOp->p5 & SQLITE_JUMPIFNULL ){
88817 goto jump_to_p2;
88818 }
88819 break;
88820 }
88821 }else{
88822 /* Neither operand is NULL and we couldn't do the special high-speed
88823 ** integer comparison case. So do a general-case comparison. */
88824 affinity = pOp->p5 & SQLITE_AFF_MASK;
88825 if( affinity>=SQLITE_AFF_NUMERIC ){
88826 if( (flags1 | flags3)&MEM_Str ){
88827 if( (flags1 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){
88828 applyNumericAffinity(pRec: pIn1,bTryForInt: 0);
88829 testcase( flags3==pIn3->flags );
88830 flags3 = pIn3->flags;
88831 }
88832 if( (flags3 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){
88833 applyNumericAffinity(pRec: pIn3,bTryForInt: 0);
88834 }
88835 }
88836 }else if( affinity==SQLITE_AFF_TEXT ){
88837 if( (flags1 & MEM_Str)==0 && (flags1&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){
88838 testcase( pIn1->flags & MEM_Int );
88839 testcase( pIn1->flags & MEM_Real );
88840 testcase( pIn1->flags & MEM_IntReal );
88841 sqlite3VdbeMemStringify(pMem: pIn1, enc: encoding, bForce: 1);
88842 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
88843 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
88844 if( NEVER(pIn1==pIn3) ) flags3 = flags1 | MEM_Str;
88845 }
88846 if( (flags3 & MEM_Str)==0 && (flags3&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){
88847 testcase( pIn3->flags & MEM_Int );
88848 testcase( pIn3->flags & MEM_Real );
88849 testcase( pIn3->flags & MEM_IntReal );
88850 sqlite3VdbeMemStringify(pMem: pIn3, enc: encoding, bForce: 1);
88851 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
88852 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
88853 }
88854 }
88855 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
88856 res = sqlite3MemCompare(pMem1: pIn3, pMem2: pIn1, pColl: pOp->p4.pColl);
88857 }
88858
88859 /* At this point, res is negative, zero, or positive if reg[P1] is
88860 ** less than, equal to, or greater than reg[P3], respectively. Compute
88861 ** the answer to this operator in res2, depending on what the comparison
88862 ** operator actually is. The next block of code depends on the fact
88863 ** that the 6 comparison operators are consecutive integers in this
88864 ** order: NE, EQ, GT, LE, LT, GE */
88865 assert( OP_Eq==OP_Ne+1 ); assert( OP_Gt==OP_Ne+2 ); assert( OP_Le==OP_Ne+3 );
88866 assert( OP_Lt==OP_Ne+4 ); assert( OP_Ge==OP_Ne+5 );
88867 if( res<0 ){
88868 res2 = sqlite3aLTb[pOp->opcode];
88869 }else if( res==0 ){
88870 res2 = sqlite3aEQb[pOp->opcode];
88871 }else{
88872 res2 = sqlite3aGTb[pOp->opcode];
88873 }
88874 iCompare = res;
88875
88876 /* Undo any changes made by applyAffinity() to the input registers. */
88877 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
88878 pIn3->flags = flags3;
88879 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
88880 pIn1->flags = flags1;
88881
88882 VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
88883 if( res2 ){
88884 goto jump_to_p2;
88885 }
88886 break;
88887}
88888
88889/* Opcode: ElseEq * P2 * * *
88890**
88891** This opcode must follow an OP_Lt or OP_Gt comparison operator. There
88892** can be zero or more OP_ReleaseReg opcodes intervening, but no other
88893** opcodes are allowed to occur between this instruction and the previous
88894** OP_Lt or OP_Gt.
88895**
88896** If result of an OP_Eq comparison on the same two operands as the
88897** prior OP_Lt or OP_Gt would have been true, then jump to P2.
88898** If the result of an OP_Eq comparison on the two previous
88899** operands would have been false or NULL, then fall through.
88900*/
88901case OP_ElseEq: { /* same as TK_ESCAPE, jump */
88902
88903#ifdef SQLITE_DEBUG
88904 /* Verify the preconditions of this opcode - that it follows an OP_Lt or
88905 ** OP_Gt with zero or more intervening OP_ReleaseReg opcodes */
88906 int iAddr;
88907 for(iAddr = (int)(pOp - aOp) - 1; ALWAYS(iAddr>=0); iAddr--){
88908 if( aOp[iAddr].opcode==OP_ReleaseReg ) continue;
88909 assert( aOp[iAddr].opcode==OP_Lt || aOp[iAddr].opcode==OP_Gt );
88910 break;
88911 }
88912#endif /* SQLITE_DEBUG */
88913 VdbeBranchTaken(iCompare==0, 2);
88914 if( iCompare==0 ) goto jump_to_p2;
88915 break;
88916}
88917
88918
88919/* Opcode: Permutation * * * P4 *
88920**
88921** Set the permutation used by the OP_Compare operator in the next
88922** instruction. The permutation is stored in the P4 operand.
88923**
88924** The permutation is only valid until the next OP_Compare that has
88925** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
88926** occur immediately prior to the OP_Compare.
88927**
88928** The first integer in the P4 integer array is the length of the array
88929** and does not become part of the permutation.
88930*/
88931case OP_Permutation: {
88932 assert( pOp->p4type==P4_INTARRAY );
88933 assert( pOp->p4.ai );
88934 assert( pOp[1].opcode==OP_Compare );
88935 assert( pOp[1].p5 & OPFLAG_PERMUTE );
88936 break;
88937}
88938
88939/* Opcode: Compare P1 P2 P3 P4 P5
88940** Synopsis: r[P1@P3] <-> r[P2@P3]
88941**
88942** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
88943** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
88944** the comparison for use by the next OP_Jump instruct.
88945**
88946** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
88947** determined by the most recent OP_Permutation operator. If the
88948** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
88949** order.
88950**
88951** P4 is a KeyInfo structure that defines collating sequences and sort
88952** orders for the comparison. The permutation applies to registers
88953** only. The KeyInfo elements are used sequentially.
88954**
88955** The comparison is a sort comparison, so NULLs compare equal,
88956** NULLs are less than numbers, numbers are less than strings,
88957** and strings are less than blobs.
88958*/
88959case OP_Compare: {
88960 int n;
88961 int i;
88962 int p1;
88963 int p2;
88964 const KeyInfo *pKeyInfo;
88965 u32 idx;
88966 CollSeq *pColl; /* Collating sequence to use on this term */
88967 int bRev; /* True for DESCENDING sort order */
88968 u32 *aPermute; /* The permutation */
88969
88970 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
88971 aPermute = 0;
88972 }else{
88973 assert( pOp>aOp );
88974 assert( pOp[-1].opcode==OP_Permutation );
88975 assert( pOp[-1].p4type==P4_INTARRAY );
88976 aPermute = pOp[-1].p4.ai + 1;
88977 assert( aPermute!=0 );
88978 }
88979 n = pOp->p3;
88980 pKeyInfo = pOp->p4.pKeyInfo;
88981 assert( n>0 );
88982 assert( pKeyInfo!=0 );
88983 p1 = pOp->p1;
88984 p2 = pOp->p2;
88985#ifdef SQLITE_DEBUG
88986 if( aPermute ){
88987 int k, mx = 0;
88988 for(k=0; k<n; k++) if( aPermute[k]>(u32)mx ) mx = aPermute[k];
88989 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
88990 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
88991 }else{
88992 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
88993 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
88994 }
88995#endif /* SQLITE_DEBUG */
88996 for(i=0; i<n; i++){
88997 idx = aPermute ? aPermute[i] : (u32)i;
88998 assert( memIsValid(&aMem[p1+idx]) );
88999 assert( memIsValid(&aMem[p2+idx]) );
89000 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
89001 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
89002 assert( i<pKeyInfo->nKeyField );
89003 pColl = pKeyInfo->aColl[i];
89004 bRev = (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC);
89005 iCompare = sqlite3MemCompare(pMem1: &aMem[p1+idx], pMem2: &aMem[p2+idx], pColl);
89006 if( iCompare ){
89007 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
89008 && ((aMem[p1+idx].flags & MEM_Null) || (aMem[p2+idx].flags & MEM_Null))
89009 ){
89010 iCompare = -iCompare;
89011 }
89012 if( bRev ) iCompare = -iCompare;
89013 break;
89014 }
89015 }
89016 break;
89017}
89018
89019/* Opcode: Jump P1 P2 P3 * *
89020**
89021** Jump to the instruction at address P1, P2, or P3 depending on whether
89022** in the most recent OP_Compare instruction the P1 vector was less than
89023** equal to, or greater than the P2 vector, respectively.
89024*/
89025case OP_Jump: { /* jump */
89026 if( iCompare<0 ){
89027 VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1];
89028 }else if( iCompare==0 ){
89029 VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1];
89030 }else{
89031 VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1];
89032 }
89033 break;
89034}
89035
89036/* Opcode: And P1 P2 P3 * *
89037** Synopsis: r[P3]=(r[P1] && r[P2])
89038**
89039** Take the logical AND of the values in registers P1 and P2 and
89040** write the result into register P3.
89041**
89042** If either P1 or P2 is 0 (false) then the result is 0 even if
89043** the other input is NULL. A NULL and true or two NULLs give
89044** a NULL output.
89045*/
89046/* Opcode: Or P1 P2 P3 * *
89047** Synopsis: r[P3]=(r[P1] || r[P2])
89048**
89049** Take the logical OR of the values in register P1 and P2 and
89050** store the answer in register P3.
89051**
89052** If either P1 or P2 is nonzero (true) then the result is 1 (true)
89053** even if the other input is NULL. A NULL and false or two NULLs
89054** give a NULL output.
89055*/
89056case OP_And: /* same as TK_AND, in1, in2, out3 */
89057case OP_Or: { /* same as TK_OR, in1, in2, out3 */
89058 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
89059 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
89060
89061 v1 = sqlite3VdbeBooleanValue(pMem: &aMem[pOp->p1], ifNull: 2);
89062 v2 = sqlite3VdbeBooleanValue(pMem: &aMem[pOp->p2], ifNull: 2);
89063 if( pOp->opcode==OP_And ){
89064 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
89065 v1 = and_logic[v1*3+v2];
89066 }else{
89067 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
89068 v1 = or_logic[v1*3+v2];
89069 }
89070 pOut = &aMem[pOp->p3];
89071 if( v1==2 ){
89072 MemSetTypeFlag(pOut, MEM_Null);
89073 }else{
89074 pOut->u.i = v1;
89075 MemSetTypeFlag(pOut, MEM_Int);
89076 }
89077 break;
89078}
89079
89080/* Opcode: IsTrue P1 P2 P3 P4 *
89081** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4
89082**
89083** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and
89084** IS NOT FALSE operators.
89085**
89086** Interpret the value in register P1 as a boolean value. Store that
89087** boolean (a 0 or 1) in register P2. Or if the value in register P1 is
89088** NULL, then the P3 is stored in register P2. Invert the answer if P4
89089** is 1.
89090**
89091** The logic is summarized like this:
89092**
89093** <ul>
89094** <li> If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE
89095** <li> If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE
89096** <li> If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE
89097** <li> If P3==1 and P4==0 then r[P2] := r[P1] IS NOT FALSE
89098** </ul>
89099*/
89100case OP_IsTrue: { /* in1, out2 */
89101 assert( pOp->p4type==P4_INT32 );
89102 assert( pOp->p4.i==0 || pOp->p4.i==1 );
89103 assert( pOp->p3==0 || pOp->p3==1 );
89104 sqlite3VdbeMemSetInt64(pMem: &aMem[pOp->p2],
89105 val: sqlite3VdbeBooleanValue(pMem: &aMem[pOp->p1], ifNull: pOp->p3) ^ pOp->p4.i);
89106 break;
89107}
89108
89109/* Opcode: Not P1 P2 * * *
89110** Synopsis: r[P2]= !r[P1]
89111**
89112** Interpret the value in register P1 as a boolean value. Store the
89113** boolean complement in register P2. If the value in register P1 is
89114** NULL, then a NULL is stored in P2.
89115*/
89116case OP_Not: { /* same as TK_NOT, in1, out2 */
89117 pIn1 = &aMem[pOp->p1];
89118 pOut = &aMem[pOp->p2];
89119 if( (pIn1->flags & MEM_Null)==0 ){
89120 sqlite3VdbeMemSetInt64(pMem: pOut, val: !sqlite3VdbeBooleanValue(pMem: pIn1,ifNull: 0));
89121 }else{
89122 sqlite3VdbeMemSetNull(pMem: pOut);
89123 }
89124 break;
89125}
89126
89127/* Opcode: BitNot P1 P2 * * *
89128** Synopsis: r[P2]= ~r[P1]
89129**
89130** Interpret the content of register P1 as an integer. Store the
89131** ones-complement of the P1 value into register P2. If P1 holds
89132** a NULL then store a NULL in P2.
89133*/
89134case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
89135 pIn1 = &aMem[pOp->p1];
89136 pOut = &aMem[pOp->p2];
89137 sqlite3VdbeMemSetNull(pMem: pOut);
89138 if( (pIn1->flags & MEM_Null)==0 ){
89139 pOut->flags = MEM_Int;
89140 pOut->u.i = ~sqlite3VdbeIntValue(pMem: pIn1);
89141 }
89142 break;
89143}
89144
89145/* Opcode: Once P1 P2 * * *
89146**
89147** Fall through to the next instruction the first time this opcode is
89148** encountered on each invocation of the byte-code program. Jump to P2
89149** on the second and all subsequent encounters during the same invocation.
89150**
89151** Top-level programs determine first invocation by comparing the P1
89152** operand against the P1 operand on the OP_Init opcode at the beginning
89153** of the program. If the P1 values differ, then fall through and make
89154** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are
89155** the same then take the jump.
89156**
89157** For subprograms, there is a bitmask in the VdbeFrame that determines
89158** whether or not the jump should be taken. The bitmask is necessary
89159** because the self-altering code trick does not work for recursive
89160** triggers.
89161*/
89162case OP_Once: { /* jump */
89163 u32 iAddr; /* Address of this instruction */
89164 assert( p->aOp[0].opcode==OP_Init );
89165 if( p->pFrame ){
89166 iAddr = (int)(pOp - p->aOp);
89167 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
89168 VdbeBranchTaken(1, 2);
89169 goto jump_to_p2;
89170 }
89171 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
89172 }else{
89173 if( p->aOp[0].p1==pOp->p1 ){
89174 VdbeBranchTaken(1, 2);
89175 goto jump_to_p2;
89176 }
89177 }
89178 VdbeBranchTaken(0, 2);
89179 pOp->p1 = p->aOp[0].p1;
89180 break;
89181}
89182
89183/* Opcode: If P1 P2 P3 * *
89184**
89185** Jump to P2 if the value in register P1 is true. The value
89186** is considered true if it is numeric and non-zero. If the value
89187** in P1 is NULL then take the jump if and only if P3 is non-zero.
89188*/
89189case OP_If: { /* jump, in1 */
89190 int c;
89191 c = sqlite3VdbeBooleanValue(pMem: &aMem[pOp->p1], ifNull: pOp->p3);
89192 VdbeBranchTaken(c!=0, 2);
89193 if( c ) goto jump_to_p2;
89194 break;
89195}
89196
89197/* Opcode: IfNot P1 P2 P3 * *
89198**
89199** Jump to P2 if the value in register P1 is False. The value
89200** is considered false if it has a numeric value of zero. If the value
89201** in P1 is NULL then take the jump if and only if P3 is non-zero.
89202*/
89203case OP_IfNot: { /* jump, in1 */
89204 int c;
89205 c = !sqlite3VdbeBooleanValue(pMem: &aMem[pOp->p1], ifNull: !pOp->p3);
89206 VdbeBranchTaken(c!=0, 2);
89207 if( c ) goto jump_to_p2;
89208 break;
89209}
89210
89211/* Opcode: IsNull P1 P2 * * *
89212** Synopsis: if r[P1]==NULL goto P2
89213**
89214** Jump to P2 if the value in register P1 is NULL.
89215*/
89216case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
89217 pIn1 = &aMem[pOp->p1];
89218 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
89219 if( (pIn1->flags & MEM_Null)!=0 ){
89220 goto jump_to_p2;
89221 }
89222 break;
89223}
89224
89225/* Opcode: IsNullOrType P1 P2 P3 * *
89226** Synopsis: if typeof(r[P1]) IN (P3,5) goto P2
89227**
89228** Jump to P2 if the value in register P1 is NULL or has a datatype P3.
89229** P3 is an integer which should be one of SQLITE_INTEGER, SQLITE_FLOAT,
89230** SQLITE_BLOB, SQLITE_NULL, or SQLITE_TEXT.
89231*/
89232case OP_IsNullOrType: { /* jump, in1 */
89233 int doTheJump;
89234 pIn1 = &aMem[pOp->p1];
89235 doTheJump = (pIn1->flags & MEM_Null)!=0 || sqlite3_value_type(pVal: pIn1)==pOp->p3;
89236 VdbeBranchTaken( doTheJump, 2);
89237 if( doTheJump ) goto jump_to_p2;
89238 break;
89239}
89240
89241/* Opcode: ZeroOrNull P1 P2 P3 * *
89242** Synopsis: r[P2] = 0 OR NULL
89243**
89244** If all both registers P1 and P3 are NOT NULL, then store a zero in
89245** register P2. If either registers P1 or P3 are NULL then put
89246** a NULL in register P2.
89247*/
89248case OP_ZeroOrNull: { /* in1, in2, out2, in3 */
89249 if( (aMem[pOp->p1].flags & MEM_Null)!=0
89250 || (aMem[pOp->p3].flags & MEM_Null)!=0
89251 ){
89252 sqlite3VdbeMemSetNull(pMem: aMem + pOp->p2);
89253 }else{
89254 sqlite3VdbeMemSetInt64(pMem: aMem + pOp->p2, val: 0);
89255 }
89256 break;
89257}
89258
89259/* Opcode: NotNull P1 P2 * * *
89260** Synopsis: if r[P1]!=NULL goto P2
89261**
89262** Jump to P2 if the value in register P1 is not NULL.
89263*/
89264case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
89265 pIn1 = &aMem[pOp->p1];
89266 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
89267 if( (pIn1->flags & MEM_Null)==0 ){
89268 goto jump_to_p2;
89269 }
89270 break;
89271}
89272
89273/* Opcode: IfNullRow P1 P2 P3 * *
89274** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2
89275**
89276** Check the cursor P1 to see if it is currently pointing at a NULL row.
89277** If it is, then set register P3 to NULL and jump immediately to P2.
89278** If P1 is not on a NULL row, then fall through without making any
89279** changes.
89280*/
89281case OP_IfNullRow: { /* jump */
89282 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
89283 assert( p->apCsr[pOp->p1]!=0 );
89284 if( p->apCsr[pOp->p1]->nullRow ){
89285 sqlite3VdbeMemSetNull(pMem: aMem + pOp->p3);
89286 goto jump_to_p2;
89287 }
89288 break;
89289}
89290
89291#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
89292/* Opcode: Offset P1 P2 P3 * *
89293** Synopsis: r[P3] = sqlite_offset(P1)
89294**
89295** Store in register r[P3] the byte offset into the database file that is the
89296** start of the payload for the record at which that cursor P1 is currently
89297** pointing.
89298**
89299** P2 is the column number for the argument to the sqlite_offset() function.
89300** This opcode does not use P2 itself, but the P2 value is used by the
89301** code generator. The P1, P2, and P3 operands to this opcode are the
89302** same as for OP_Column.
89303**
89304** This opcode is only available if SQLite is compiled with the
89305** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
89306*/
89307case OP_Offset: { /* out3 */
89308 VdbeCursor *pC; /* The VDBE cursor */
89309 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
89310 pC = p->apCsr[pOp->p1];
89311 pOut = &p->aMem[pOp->p3];
89312 if( NEVER(pC==0) || pC->eCurType!=CURTYPE_BTREE ){
89313 sqlite3VdbeMemSetNull(pOut);
89314 }else{
89315 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
89316 }
89317 break;
89318}
89319#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
89320
89321/* Opcode: Column P1 P2 P3 P4 P5
89322** Synopsis: r[P3]=PX
89323**
89324** Interpret the data that cursor P1 points to as a structure built using
89325** the MakeRecord instruction. (See the MakeRecord opcode for additional
89326** information about the format of the data.) Extract the P2-th column
89327** from this record. If there are less that (P2+1)
89328** values in the record, extract a NULL.
89329**
89330** The value extracted is stored in register P3.
89331**
89332** If the record contains fewer than P2 fields, then extract a NULL. Or,
89333** if the P4 argument is a P4_MEM use the value of the P4 argument as
89334** the result.
89335**
89336** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 then
89337** the result is guaranteed to only be used as the argument of a length()
89338** or typeof() function, respectively. The loading of large blobs can be
89339** skipped for length() and all content loading can be skipped for typeof().
89340*/
89341case OP_Column: {
89342 u32 p2; /* column number to retrieve */
89343 VdbeCursor *pC; /* The VDBE cursor */
89344 BtCursor *pCrsr; /* The BTree cursor */
89345 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
89346 int len; /* The length of the serialized data for the column */
89347 int i; /* Loop counter */
89348 Mem *pDest; /* Where to write the extracted value */
89349 Mem sMem; /* For storing the record being decoded */
89350 const u8 *zData; /* Part of the record being decoded */
89351 const u8 *zHdr; /* Next unparsed byte of the header */
89352 const u8 *zEndHdr; /* Pointer to first byte after the header */
89353 u64 offset64; /* 64-bit offset */
89354 u32 t; /* A type code from the record header */
89355 Mem *pReg; /* PseudoTable input register */
89356
89357 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
89358 pC = p->apCsr[pOp->p1];
89359 assert( pC!=0 );
89360 p2 = (u32)pOp->p2;
89361
89362 /* If the cursor cache is stale (meaning it is not currently point at
89363 ** the correct row) then bring it up-to-date by doing the necessary
89364 ** B-Tree seek. */
89365 rc = sqlite3VdbeCursorMoveto(pp: &pC, piCol: &p2);
89366 if( rc ) goto abort_due_to_error;
89367
89368 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
89369 pDest = &aMem[pOp->p3];
89370 memAboutToChange(p, pDest);
89371 assert( pC!=0 );
89372 assert( p2<(u32)pC->nField );
89373 aOffset = pC->aOffset;
89374 assert( pC->eCurType!=CURTYPE_VTAB );
89375 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
89376 assert( pC->eCurType!=CURTYPE_SORTER );
89377
89378 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
89379 if( pC->nullRow ){
89380 if( pC->eCurType==CURTYPE_PSEUDO ){
89381 /* For the special case of as pseudo-cursor, the seekResult field
89382 ** identifies the register that holds the record */
89383 assert( pC->seekResult>0 );
89384 pReg = &aMem[pC->seekResult];
89385 assert( pReg->flags & MEM_Blob );
89386 assert( memIsValid(pReg) );
89387 pC->payloadSize = pC->szRow = pReg->n;
89388 pC->aRow = (u8*)pReg->z;
89389 }else{
89390 sqlite3VdbeMemSetNull(pMem: pDest);
89391 goto op_column_out;
89392 }
89393 }else{
89394 pCrsr = pC->uc.pCursor;
89395 assert( pC->eCurType==CURTYPE_BTREE );
89396 assert( pCrsr );
89397 assert( sqlite3BtreeCursorIsValid(pCrsr) );
89398 pC->payloadSize = sqlite3BtreePayloadSize(pCur: pCrsr);
89399 pC->aRow = sqlite3BtreePayloadFetch(pCur: pCrsr, pAmt: &pC->szRow);
89400 assert( pC->szRow<=pC->payloadSize );
89401 assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */
89402 if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
89403 goto too_big;
89404 }
89405 }
89406 pC->cacheStatus = p->cacheCtr;
89407 pC->iHdrOffset = getVarint32(pC->aRow, aOffset[0]);
89408 pC->nHdrParsed = 0;
89409
89410
89411 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/
89412 /* pC->aRow does not have to hold the entire row, but it does at least
89413 ** need to cover the header of the record. If pC->aRow does not contain
89414 ** the complete header, then set it to zero, forcing the header to be
89415 ** dynamically allocated. */
89416 pC->aRow = 0;
89417 pC->szRow = 0;
89418
89419 /* Make sure a corrupt database has not given us an oversize header.
89420 ** Do this now to avoid an oversize memory allocation.
89421 **
89422 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
89423 ** types use so much data space that there can only be 4096 and 32 of
89424 ** them, respectively. So the maximum header length results from a
89425 ** 3-byte type for each of the maximum of 32768 columns plus three
89426 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
89427 */
89428 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
89429 goto op_column_corrupt;
89430 }
89431 }else{
89432 /* This is an optimization. By skipping over the first few tests
89433 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
89434 ** measurable performance gain.
89435 **
89436 ** This branch is taken even if aOffset[0]==0. Such a record is never
89437 ** generated by SQLite, and could be considered corruption, but we
89438 ** accept it for historical reasons. When aOffset[0]==0, the code this
89439 ** branch jumps to reads past the end of the record, but never more
89440 ** than a few bytes. Even if the record occurs at the end of the page
89441 ** content area, the "page header" comes after the page content and so
89442 ** this overread is harmless. Similar overreads can occur for a corrupt
89443 ** database file.
89444 */
89445 zData = pC->aRow;
89446 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
89447 testcase( aOffset[0]==0 );
89448 goto op_column_read_header;
89449 }
89450 }
89451
89452 /* Make sure at least the first p2+1 entries of the header have been
89453 ** parsed and valid information is in aOffset[] and pC->aType[].
89454 */
89455 if( pC->nHdrParsed<=p2 ){
89456 /* If there is more header available for parsing in the record, try
89457 ** to extract additional fields up through the p2+1-th field
89458 */
89459 if( pC->iHdrOffset<aOffset[0] ){
89460 /* Make sure zData points to enough of the record to cover the header. */
89461 if( pC->aRow==0 ){
89462 memset(s: &sMem, c: 0, n: sizeof(sMem));
89463 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur: pC->uc.pCursor,amt: aOffset[0],pMem: &sMem);
89464 if( rc!=SQLITE_OK ) goto abort_due_to_error;
89465 zData = (u8*)sMem.z;
89466 }else{
89467 zData = pC->aRow;
89468 }
89469
89470 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
89471 op_column_read_header:
89472 i = pC->nHdrParsed;
89473 offset64 = aOffset[i];
89474 zHdr = zData + pC->iHdrOffset;
89475 zEndHdr = zData + aOffset[0];
89476 testcase( zHdr>=zEndHdr );
89477 do{
89478 if( (pC->aType[i] = t = zHdr[0])<0x80 ){
89479 zHdr++;
89480 offset64 += sqlite3VdbeOneByteSerialTypeLen(serial_type: t);
89481 }else{
89482 zHdr += sqlite3GetVarint32(p: zHdr, v: &t);
89483 pC->aType[i] = t;
89484 offset64 += sqlite3VdbeSerialTypeLen(serial_type: t);
89485 }
89486 aOffset[++i] = (u32)(offset64 & 0xffffffff);
89487 }while( (u32)i<=p2 && zHdr<zEndHdr );
89488
89489 /* The record is corrupt if any of the following are true:
89490 ** (1) the bytes of the header extend past the declared header size
89491 ** (2) the entire header was used but not all data was used
89492 ** (3) the end of the data extends beyond the end of the record.
89493 */
89494 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
89495 || (offset64 > pC->payloadSize)
89496 ){
89497 if( aOffset[0]==0 ){
89498 i = 0;
89499 zHdr = zEndHdr;
89500 }else{
89501 if( pC->aRow==0 ) sqlite3VdbeMemRelease(p: &sMem);
89502 goto op_column_corrupt;
89503 }
89504 }
89505
89506 pC->nHdrParsed = i;
89507 pC->iHdrOffset = (u32)(zHdr - zData);
89508 if( pC->aRow==0 ) sqlite3VdbeMemRelease(p: &sMem);
89509 }else{
89510 t = 0;
89511 }
89512
89513 /* If after trying to extract new entries from the header, nHdrParsed is
89514 ** still not up to p2, that means that the record has fewer than p2
89515 ** columns. So the result will be either the default value or a NULL.
89516 */
89517 if( pC->nHdrParsed<=p2 ){
89518 if( pOp->p4type==P4_MEM ){
89519 sqlite3VdbeMemShallowCopy(pTo: pDest, pFrom: pOp->p4.pMem, MEM_Static);
89520 }else{
89521 sqlite3VdbeMemSetNull(pMem: pDest);
89522 }
89523 goto op_column_out;
89524 }
89525 }else{
89526 t = pC->aType[p2];
89527 }
89528
89529 /* Extract the content for the p2+1-th column. Control can only
89530 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
89531 ** all valid.
89532 */
89533 assert( p2<pC->nHdrParsed );
89534 assert( rc==SQLITE_OK );
89535 assert( sqlite3VdbeCheckMemInvariants(pDest) );
89536 if( VdbeMemDynamic(pDest) ){
89537 sqlite3VdbeMemSetNull(pMem: pDest);
89538 }
89539 assert( t==pC->aType[p2] );
89540 if( pC->szRow>=aOffset[p2+1] ){
89541 /* This is the common case where the desired content fits on the original
89542 ** page - where the content is not on an overflow page */
89543 zData = pC->aRow + aOffset[p2];
89544 if( t<12 ){
89545 sqlite3VdbeSerialGet(buf: zData, serial_type: t, pMem: pDest);
89546 }else{
89547 /* If the column value is a string, we need a persistent value, not
89548 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
89549 ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize().
89550 */
89551 static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term };
89552 pDest->n = len = (t-12)/2;
89553 pDest->enc = encoding;
89554 if( pDest->szMalloc < len+2 ){
89555 pDest->flags = MEM_Null;
89556 if( sqlite3VdbeMemGrow(pMem: pDest, n: len+2, bPreserve: 0) ) goto no_mem;
89557 }else{
89558 pDest->z = pDest->zMalloc;
89559 }
89560 memcpy(dest: pDest->z, src: zData, n: len);
89561 pDest->z[len] = 0;
89562 pDest->z[len+1] = 0;
89563 pDest->flags = aFlag[t&1];
89564 }
89565 }else{
89566 pDest->enc = encoding;
89567 /* This branch happens only when content is on overflow pages */
89568 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
89569 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
89570 || (len = sqlite3VdbeSerialTypeLen(serial_type: t))==0
89571 ){
89572 /* Content is irrelevant for
89573 ** 1. the typeof() function,
89574 ** 2. the length(X) function if X is a blob, and
89575 ** 3. if the content length is zero.
89576 ** So we might as well use bogus content rather than reading
89577 ** content from disk.
89578 **
89579 ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the
89580 ** buffer passed to it, debugging function VdbeMemPrettyPrint() may
89581 ** read more. Use the global constant sqlite3CtypeMap[] as the array,
89582 ** as that array is 256 bytes long (plenty for VdbeMemPrettyPrint())
89583 ** and it begins with a bunch of zeros.
89584 */
89585 sqlite3VdbeSerialGet(buf: (u8*)sqlite3CtypeMap, serial_type: t, pMem: pDest);
89586 }else{
89587 rc = sqlite3VdbeMemFromBtree(pCur: pC->uc.pCursor, offset: aOffset[p2], amt: len, pMem: pDest);
89588 if( rc!=SQLITE_OK ) goto abort_due_to_error;
89589 sqlite3VdbeSerialGet(buf: (const u8*)pDest->z, serial_type: t, pMem: pDest);
89590 pDest->flags &= ~MEM_Ephem;
89591 }
89592 }
89593
89594op_column_out:
89595 UPDATE_MAX_BLOBSIZE(pDest);
89596 REGISTER_TRACE(pOp->p3, pDest);
89597 break;
89598
89599op_column_corrupt:
89600 if( aOp[0].p3>0 ){
89601 pOp = &aOp[aOp[0].p3-1];
89602 break;
89603 }else{
89604 rc = SQLITE_CORRUPT_BKPT;
89605 goto abort_due_to_error;
89606 }
89607}
89608
89609/* Opcode: TypeCheck P1 P2 P3 P4 *
89610** Synopsis: typecheck(r[P1@P2])
89611**
89612** Apply affinities to the range of P2 registers beginning with P1.
89613** Take the affinities from the Table object in P4. If any value
89614** cannot be coerced into the correct type, then raise an error.
89615**
89616** This opcode is similar to OP_Affinity except that this opcode
89617** forces the register type to the Table column type. This is used
89618** to implement "strict affinity".
89619**
89620** GENERATED ALWAYS AS ... STATIC columns are only checked if P3
89621** is zero. When P3 is non-zero, no type checking occurs for
89622** static generated columns. Virtual columns are computed at query time
89623** and so they are never checked.
89624**
89625** Preconditions:
89626**
89627** <ul>
89628** <li> P2 should be the number of non-virtual columns in the
89629** table of P4.
89630** <li> Table P4 should be a STRICT table.
89631** </ul>
89632**
89633** If any precondition is false, an assertion fault occurs.
89634*/
89635case OP_TypeCheck: {
89636 Table *pTab;
89637 Column *aCol;
89638 int i;
89639
89640 assert( pOp->p4type==P4_TABLE );
89641 pTab = pOp->p4.pTab;
89642 assert( pTab->tabFlags & TF_Strict );
89643 assert( pTab->nNVCol==pOp->p2 );
89644 aCol = pTab->aCol;
89645 pIn1 = &aMem[pOp->p1];
89646 for(i=0; i<pTab->nCol; i++){
89647 if( aCol[i].colFlags & COLFLAG_GENERATED ){
89648 if( aCol[i].colFlags & COLFLAG_VIRTUAL ) continue;
89649 if( pOp->p3 ){ pIn1++; continue; }
89650 }
89651 assert( pIn1 < &aMem[pOp->p1+pOp->p2] );
89652 applyAffinity(pRec: pIn1, affinity: aCol[i].affinity, enc: encoding);
89653 if( (pIn1->flags & MEM_Null)==0 ){
89654 switch( aCol[i].eCType ){
89655 case COLTYPE_BLOB: {
89656 if( (pIn1->flags & MEM_Blob)==0 ) goto vdbe_type_error;
89657 break;
89658 }
89659 case COLTYPE_INTEGER:
89660 case COLTYPE_INT: {
89661 if( (pIn1->flags & MEM_Int)==0 ) goto vdbe_type_error;
89662 break;
89663 }
89664 case COLTYPE_TEXT: {
89665 if( (pIn1->flags & MEM_Str)==0 ) goto vdbe_type_error;
89666 break;
89667 }
89668 case COLTYPE_REAL: {
89669 if( pIn1->flags & MEM_Int ){
89670 /* When applying REAL affinity, if the result is still an MEM_Int
89671 ** that will fit in 6 bytes, then change the type to MEM_IntReal
89672 ** so that we keep the high-resolution integer value but know that
89673 ** the type really wants to be REAL. */
89674 testcase( pIn1->u.i==140737488355328LL );
89675 testcase( pIn1->u.i==140737488355327LL );
89676 testcase( pIn1->u.i==-140737488355328LL );
89677 testcase( pIn1->u.i==-140737488355329LL );
89678 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL){
89679 pIn1->flags |= MEM_IntReal;
89680 pIn1->flags &= ~MEM_Int;
89681 }else{
89682 pIn1->u.r = (double)pIn1->u.i;
89683 pIn1->flags |= MEM_Real;
89684 pIn1->flags &= ~MEM_Int;
89685 }
89686 }else if( (pIn1->flags & MEM_Real)==0 ){
89687 goto vdbe_type_error;
89688 }
89689 break;
89690 }
89691 default: {
89692 /* COLTYPE_ANY. Accept anything. */
89693 break;
89694 }
89695 }
89696 }
89697 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
89698 pIn1++;
89699 }
89700 assert( pIn1 == &aMem[pOp->p1+pOp->p2] );
89701 break;
89702
89703vdbe_type_error:
89704 sqlite3VdbeError(p, zFormat: "cannot store %s value in %s column %s.%s",
89705 vdbeMemTypeName(pMem: pIn1), sqlite3StdType[aCol[i].eCType-1],
89706 pTab->zName, aCol[i].zCnName);
89707 rc = SQLITE_CONSTRAINT_DATATYPE;
89708 goto abort_due_to_error;
89709}
89710
89711/* Opcode: Affinity P1 P2 * P4 *
89712** Synopsis: affinity(r[P1@P2])
89713**
89714** Apply affinities to a range of P2 registers starting with P1.
89715**
89716** P4 is a string that is P2 characters long. The N-th character of the
89717** string indicates the column affinity that should be used for the N-th
89718** memory cell in the range.
89719*/
89720case OP_Affinity: {
89721 const char *zAffinity; /* The affinity to be applied */
89722
89723 zAffinity = pOp->p4.z;
89724 assert( zAffinity!=0 );
89725 assert( pOp->p2>0 );
89726 assert( zAffinity[pOp->p2]==0 );
89727 pIn1 = &aMem[pOp->p1];
89728 while( 1 /*exit-by-break*/ ){
89729 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
89730 assert( zAffinity[0]==SQLITE_AFF_NONE || memIsValid(pIn1) );
89731 applyAffinity(pRec: pIn1, affinity: zAffinity[0], enc: encoding);
89732 if( zAffinity[0]==SQLITE_AFF_REAL && (pIn1->flags & MEM_Int)!=0 ){
89733 /* When applying REAL affinity, if the result is still an MEM_Int
89734 ** that will fit in 6 bytes, then change the type to MEM_IntReal
89735 ** so that we keep the high-resolution integer value but know that
89736 ** the type really wants to be REAL. */
89737 testcase( pIn1->u.i==140737488355328LL );
89738 testcase( pIn1->u.i==140737488355327LL );
89739 testcase( pIn1->u.i==-140737488355328LL );
89740 testcase( pIn1->u.i==-140737488355329LL );
89741 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL ){
89742 pIn1->flags |= MEM_IntReal;
89743 pIn1->flags &= ~MEM_Int;
89744 }else{
89745 pIn1->u.r = (double)pIn1->u.i;
89746 pIn1->flags |= MEM_Real;
89747 pIn1->flags &= ~MEM_Int;
89748 }
89749 }
89750 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
89751 zAffinity++;
89752 if( zAffinity[0]==0 ) break;
89753 pIn1++;
89754 }
89755 break;
89756}
89757
89758/* Opcode: MakeRecord P1 P2 P3 P4 *
89759** Synopsis: r[P3]=mkrec(r[P1@P2])
89760**
89761** Convert P2 registers beginning with P1 into the [record format]
89762** use as a data record in a database table or as a key
89763** in an index. The OP_Column opcode can decode the record later.
89764**
89765** P4 may be a string that is P2 characters long. The N-th character of the
89766** string indicates the column affinity that should be used for the N-th
89767** field of the index key.
89768**
89769** The mapping from character to affinity is given by the SQLITE_AFF_
89770** macros defined in sqliteInt.h.
89771**
89772** If P4 is NULL then all index fields have the affinity BLOB.
89773**
89774** The meaning of P5 depends on whether or not the SQLITE_ENABLE_NULL_TRIM
89775** compile-time option is enabled:
89776**
89777** * If SQLITE_ENABLE_NULL_TRIM is enabled, then the P5 is the index
89778** of the right-most table that can be null-trimmed.
89779**
89780** * If SQLITE_ENABLE_NULL_TRIM is omitted, then P5 has the value
89781** OPFLAG_NOCHNG_MAGIC if the OP_MakeRecord opcode is allowed to
89782** accept no-change records with serial_type 10. This value is
89783** only used inside an assert() and does not affect the end result.
89784*/
89785case OP_MakeRecord: {
89786 Mem *pRec; /* The new record */
89787 u64 nData; /* Number of bytes of data space */
89788 int nHdr; /* Number of bytes of header space */
89789 i64 nByte; /* Data space required for this record */
89790 i64 nZero; /* Number of zero bytes at the end of the record */
89791 int nVarint; /* Number of bytes in a varint */
89792 u32 serial_type; /* Type field */
89793 Mem *pData0; /* First field to be combined into the record */
89794 Mem *pLast; /* Last field of the record */
89795 int nField; /* Number of fields in the record */
89796 char *zAffinity; /* The affinity string for the record */
89797 int file_format; /* File format to use for encoding */
89798 u32 len; /* Length of a field */
89799 u8 *zHdr; /* Where to write next byte of the header */
89800 u8 *zPayload; /* Where to write next byte of the payload */
89801
89802 /* Assuming the record contains N fields, the record format looks
89803 ** like this:
89804 **
89805 ** ------------------------------------------------------------------------
89806 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
89807 ** ------------------------------------------------------------------------
89808 **
89809 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
89810 ** and so forth.
89811 **
89812 ** Each type field is a varint representing the serial type of the
89813 ** corresponding data element (see sqlite3VdbeSerialType()). The
89814 ** hdr-size field is also a varint which is the offset from the beginning
89815 ** of the record to data0.
89816 */
89817 nData = 0; /* Number of bytes of data space */
89818 nHdr = 0; /* Number of bytes of header space */
89819 nZero = 0; /* Number of zero bytes at the end of the record */
89820 nField = pOp->p1;
89821 zAffinity = pOp->p4.z;
89822 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
89823 pData0 = &aMem[nField];
89824 nField = pOp->p2;
89825 pLast = &pData0[nField-1];
89826 file_format = p->minWriteFileFormat;
89827
89828 /* Identify the output register */
89829 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
89830 pOut = &aMem[pOp->p3];
89831 memAboutToChange(p, pOut);
89832
89833 /* Apply the requested affinity to all inputs
89834 */
89835 assert( pData0<=pLast );
89836 if( zAffinity ){
89837 pRec = pData0;
89838 do{
89839 applyAffinity(pRec, affinity: zAffinity[0], enc: encoding);
89840 if( zAffinity[0]==SQLITE_AFF_REAL && (pRec->flags & MEM_Int) ){
89841 pRec->flags |= MEM_IntReal;
89842 pRec->flags &= ~(MEM_Int);
89843 }
89844 REGISTER_TRACE((int)(pRec-aMem), pRec);
89845 zAffinity++;
89846 pRec++;
89847 assert( zAffinity[0]==0 || pRec<=pLast );
89848 }while( zAffinity[0] );
89849 }
89850
89851#ifdef SQLITE_ENABLE_NULL_TRIM
89852 /* NULLs can be safely trimmed from the end of the record, as long as
89853 ** as the schema format is 2 or more and none of the omitted columns
89854 ** have a non-NULL default value. Also, the record must be left with
89855 ** at least one field. If P5>0 then it will be one more than the
89856 ** index of the right-most column with a non-NULL default value */
89857 if( pOp->p5 ){
89858 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
89859 pLast--;
89860 nField--;
89861 }
89862 }
89863#endif
89864
89865 /* Loop through the elements that will make up the record to figure
89866 ** out how much space is required for the new record. After this loop,
89867 ** the Mem.uTemp field of each term should hold the serial-type that will
89868 ** be used for that term in the generated record:
89869 **
89870 ** Mem.uTemp value type
89871 ** --------------- ---------------
89872 ** 0 NULL
89873 ** 1 1-byte signed integer
89874 ** 2 2-byte signed integer
89875 ** 3 3-byte signed integer
89876 ** 4 4-byte signed integer
89877 ** 5 6-byte signed integer
89878 ** 6 8-byte signed integer
89879 ** 7 IEEE float
89880 ** 8 Integer constant 0
89881 ** 9 Integer constant 1
89882 ** 10,11 reserved for expansion
89883 ** N>=12 and even BLOB
89884 ** N>=13 and odd text
89885 **
89886 ** The following additional values are computed:
89887 ** nHdr Number of bytes needed for the record header
89888 ** nData Number of bytes of data space needed for the record
89889 ** nZero Zero bytes at the end of the record
89890 */
89891 pRec = pLast;
89892 do{
89893 assert( memIsValid(pRec) );
89894 if( pRec->flags & MEM_Null ){
89895 if( pRec->flags & MEM_Zero ){
89896 /* Values with MEM_Null and MEM_Zero are created by xColumn virtual
89897 ** table methods that never invoke sqlite3_result_xxxxx() while
89898 ** computing an unchanging column value in an UPDATE statement.
89899 ** Give such values a special internal-use-only serial-type of 10
89900 ** so that they can be passed through to xUpdate and have
89901 ** a true sqlite3_value_nochange(). */
89902#ifndef SQLITE_ENABLE_NULL_TRIM
89903 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB );
89904#endif
89905 pRec->uTemp = 10;
89906 }else{
89907 pRec->uTemp = 0;
89908 }
89909 nHdr++;
89910 }else if( pRec->flags & (MEM_Int|MEM_IntReal) ){
89911 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
89912 i64 i = pRec->u.i;
89913 u64 uu;
89914 testcase( pRec->flags & MEM_Int );
89915 testcase( pRec->flags & MEM_IntReal );
89916 if( i<0 ){
89917 uu = ~i;
89918 }else{
89919 uu = i;
89920 }
89921 nHdr++;
89922 testcase( uu==127 ); testcase( uu==128 );
89923 testcase( uu==32767 ); testcase( uu==32768 );
89924 testcase( uu==8388607 ); testcase( uu==8388608 );
89925 testcase( uu==2147483647 ); testcase( uu==2147483648 );
89926 testcase( uu==140737488355327LL ); testcase( uu==140737488355328LL );
89927 if( uu<=127 ){
89928 if( (i&1)==i && file_format>=4 ){
89929 pRec->uTemp = 8+(u32)uu;
89930 }else{
89931 nData++;
89932 pRec->uTemp = 1;
89933 }
89934 }else if( uu<=32767 ){
89935 nData += 2;
89936 pRec->uTemp = 2;
89937 }else if( uu<=8388607 ){
89938 nData += 3;
89939 pRec->uTemp = 3;
89940 }else if( uu<=2147483647 ){
89941 nData += 4;
89942 pRec->uTemp = 4;
89943 }else if( uu<=140737488355327LL ){
89944 nData += 6;
89945 pRec->uTemp = 5;
89946 }else{
89947 nData += 8;
89948 if( pRec->flags & MEM_IntReal ){
89949 /* If the value is IntReal and is going to take up 8 bytes to store
89950 ** as an integer, then we might as well make it an 8-byte floating
89951 ** point value */
89952 pRec->u.r = (double)pRec->u.i;
89953 pRec->flags &= ~MEM_IntReal;
89954 pRec->flags |= MEM_Real;
89955 pRec->uTemp = 7;
89956 }else{
89957 pRec->uTemp = 6;
89958 }
89959 }
89960 }else if( pRec->flags & MEM_Real ){
89961 nHdr++;
89962 nData += 8;
89963 pRec->uTemp = 7;
89964 }else{
89965 assert( db->mallocFailed || pRec->flags&(MEM_Str|MEM_Blob) );
89966 assert( pRec->n>=0 );
89967 len = (u32)pRec->n;
89968 serial_type = (len*2) + 12 + ((pRec->flags & MEM_Str)!=0);
89969 if( pRec->flags & MEM_Zero ){
89970 serial_type += pRec->u.nZero*2;
89971 if( nData ){
89972 if( sqlite3VdbeMemExpandBlob(pMem: pRec) ) goto no_mem;
89973 len += pRec->u.nZero;
89974 }else{
89975 nZero += pRec->u.nZero;
89976 }
89977 }
89978 nData += len;
89979 nHdr += sqlite3VarintLen(v: serial_type);
89980 pRec->uTemp = serial_type;
89981 }
89982 if( pRec==pData0 ) break;
89983 pRec--;
89984 }while(1);
89985
89986 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
89987 ** which determines the total number of bytes in the header. The varint
89988 ** value is the size of the header in bytes including the size varint
89989 ** itself. */
89990 testcase( nHdr==126 );
89991 testcase( nHdr==127 );
89992 if( nHdr<=126 ){
89993 /* The common case */
89994 nHdr += 1;
89995 }else{
89996 /* Rare case of a really large header */
89997 nVarint = sqlite3VarintLen(v: nHdr);
89998 nHdr += nVarint;
89999 if( nVarint<sqlite3VarintLen(v: nHdr) ) nHdr++;
90000 }
90001 nByte = nHdr+nData;
90002
90003 /* Make sure the output register has a buffer large enough to store
90004 ** the new record. The output register (pOp->p3) is not allowed to
90005 ** be one of the input registers (because the following call to
90006 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
90007 */
90008 if( nByte+nZero<=pOut->szMalloc ){
90009 /* The output register is already large enough to hold the record.
90010 ** No error checks or buffer enlargement is required */
90011 pOut->z = pOut->zMalloc;
90012 }else{
90013 /* Need to make sure that the output is not too big and then enlarge
90014 ** the output register to hold the full result */
90015 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
90016 goto too_big;
90017 }
90018 if( sqlite3VdbeMemClearAndResize(pMem: pOut, szNew: (int)nByte) ){
90019 goto no_mem;
90020 }
90021 }
90022 pOut->n = (int)nByte;
90023 pOut->flags = MEM_Blob;
90024 if( nZero ){
90025 pOut->u.nZero = nZero;
90026 pOut->flags |= MEM_Zero;
90027 }
90028 UPDATE_MAX_BLOBSIZE(pOut);
90029 zHdr = (u8 *)pOut->z;
90030 zPayload = zHdr + nHdr;
90031
90032 /* Write the record */
90033 zHdr += putVarint32(zHdr, nHdr);
90034 assert( pData0<=pLast );
90035 pRec = pData0;
90036 do{
90037 serial_type = pRec->uTemp;
90038 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
90039 ** additional varints, one per column. */
90040 zHdr += putVarint32(zHdr, serial_type); /* serial type */
90041 /* EVIDENCE-OF: R-64536-51728 The values for each column in the record
90042 ** immediately follow the header. */
90043 zPayload += sqlite3VdbeSerialPut(buf: zPayload, pMem: pRec, serial_type); /* content */
90044 }while( (++pRec)<=pLast );
90045 assert( nHdr==(int)(zHdr - (u8*)pOut->z) );
90046 assert( nByte==(int)(zPayload - (u8*)pOut->z) );
90047
90048 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
90049 REGISTER_TRACE(pOp->p3, pOut);
90050 break;
90051}
90052
90053/* Opcode: Count P1 P2 p3 * *
90054** Synopsis: r[P2]=count()
90055**
90056** Store the number of entries (an integer value) in the table or index
90057** opened by cursor P1 in register P2.
90058**
90059** If P3==0, then an exact count is obtained, which involves visiting
90060** every btree page of the table. But if P3 is non-zero, an estimate
90061** is returned based on the current cursor position.
90062*/
90063case OP_Count: { /* out2 */
90064 i64 nEntry;
90065 BtCursor *pCrsr;
90066
90067 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
90068 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
90069 assert( pCrsr );
90070 if( pOp->p3 ){
90071 nEntry = sqlite3BtreeRowCountEst(pCur: pCrsr);
90072 }else{
90073 nEntry = 0; /* Not needed. Only used to silence a warning. */
90074 rc = sqlite3BtreeCount(db, pCur: pCrsr, pnEntry: &nEntry);
90075 if( rc ) goto abort_due_to_error;
90076 }
90077 pOut = out2Prerelease(p, pOp);
90078 pOut->u.i = nEntry;
90079 goto check_for_interrupt;
90080}
90081
90082/* Opcode: Savepoint P1 * * P4 *
90083**
90084** Open, release or rollback the savepoint named by parameter P4, depending
90085** on the value of P1. To open a new savepoint set P1==0 (SAVEPOINT_BEGIN).
90086** To release (commit) an existing savepoint set P1==1 (SAVEPOINT_RELEASE).
90087** To rollback an existing savepoint set P1==2 (SAVEPOINT_ROLLBACK).
90088*/
90089case OP_Savepoint: {
90090 int p1; /* Value of P1 operand */
90091 char *zName; /* Name of savepoint */
90092 int nName;
90093 Savepoint *pNew;
90094 Savepoint *pSavepoint;
90095 Savepoint *pTmp;
90096 int iSavepoint;
90097 int ii;
90098
90099 p1 = pOp->p1;
90100 zName = pOp->p4.z;
90101
90102 /* Assert that the p1 parameter is valid. Also that if there is no open
90103 ** transaction, then there cannot be any savepoints.
90104 */
90105 assert( db->pSavepoint==0 || db->autoCommit==0 );
90106 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
90107 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
90108 assert( checkSavepointCount(db) );
90109 assert( p->bIsReader );
90110
90111 if( p1==SAVEPOINT_BEGIN ){
90112 if( db->nVdbeWrite>0 ){
90113 /* A new savepoint cannot be created if there are active write
90114 ** statements (i.e. open read/write incremental blob handles).
90115 */
90116 sqlite3VdbeError(p, zFormat: "cannot open savepoint - SQL statements in progress");
90117 rc = SQLITE_BUSY;
90118 }else{
90119 nName = sqlite3Strlen30(z: zName);
90120
90121#ifndef SQLITE_OMIT_VIRTUALTABLE
90122 /* This call is Ok even if this savepoint is actually a transaction
90123 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
90124 ** If this is a transaction savepoint being opened, it is guaranteed
90125 ** that the db->aVTrans[] array is empty. */
90126 assert( db->autoCommit==0 || db->nVTrans==0 );
90127 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
90128 db->nStatement+db->nSavepoint);
90129 if( rc!=SQLITE_OK ) goto abort_due_to_error;
90130#endif
90131
90132 /* Create a new savepoint structure. */
90133 pNew = sqlite3DbMallocRawNN(db, n: sizeof(Savepoint)+nName+1);
90134 if( pNew ){
90135 pNew->zName = (char *)&pNew[1];
90136 memcpy(dest: pNew->zName, src: zName, n: nName+1);
90137
90138 /* If there is no open transaction, then mark this as a special
90139 ** "transaction savepoint". */
90140 if( db->autoCommit ){
90141 db->autoCommit = 0;
90142 db->isTransactionSavepoint = 1;
90143 }else{
90144 db->nSavepoint++;
90145 }
90146
90147 /* Link the new savepoint into the database handle's list. */
90148 pNew->pNext = db->pSavepoint;
90149 db->pSavepoint = pNew;
90150 pNew->nDeferredCons = db->nDeferredCons;
90151 pNew->nDeferredImmCons = db->nDeferredImmCons;
90152 }
90153 }
90154 }else{
90155 assert( p1==SAVEPOINT_RELEASE || p1==SAVEPOINT_ROLLBACK );
90156 iSavepoint = 0;
90157
90158 /* Find the named savepoint. If there is no such savepoint, then an
90159 ** an error is returned to the user. */
90160 for(
90161 pSavepoint = db->pSavepoint;
90162 pSavepoint && sqlite3StrICmp(zLeft: pSavepoint->zName, zRight: zName);
90163 pSavepoint = pSavepoint->pNext
90164 ){
90165 iSavepoint++;
90166 }
90167 if( !pSavepoint ){
90168 sqlite3VdbeError(p, zFormat: "no such savepoint: %s", zName);
90169 rc = SQLITE_ERROR;
90170 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
90171 /* It is not possible to release (commit) a savepoint if there are
90172 ** active write statements.
90173 */
90174 sqlite3VdbeError(p, zFormat: "cannot release savepoint - "
90175 "SQL statements in progress");
90176 rc = SQLITE_BUSY;
90177 }else{
90178
90179 /* Determine whether or not this is a transaction savepoint. If so,
90180 ** and this is a RELEASE command, then the current transaction
90181 ** is committed.
90182 */
90183 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
90184 if( isTransaction && p1==SAVEPOINT_RELEASE ){
90185 if( (rc = sqlite3VdbeCheckFk(p, deferred: 1))!=SQLITE_OK ){
90186 goto vdbe_return;
90187 }
90188 db->autoCommit = 1;
90189 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
90190 p->pc = (int)(pOp - aOp);
90191 db->autoCommit = 0;
90192 p->rc = rc = SQLITE_BUSY;
90193 goto vdbe_return;
90194 }
90195 rc = p->rc;
90196 if( rc ){
90197 db->autoCommit = 0;
90198 }else{
90199 db->isTransactionSavepoint = 0;
90200 }
90201 }else{
90202 int isSchemaChange;
90203 iSavepoint = db->nSavepoint - iSavepoint - 1;
90204 if( p1==SAVEPOINT_ROLLBACK ){
90205 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0;
90206 for(ii=0; ii<db->nDb; ii++){
90207 rc = sqlite3BtreeTripAllCursors(pBtree: db->aDb[ii].pBt,
90208 SQLITE_ABORT_ROLLBACK,
90209 writeOnly: isSchemaChange==0);
90210 if( rc!=SQLITE_OK ) goto abort_due_to_error;
90211 }
90212 }else{
90213 assert( p1==SAVEPOINT_RELEASE );
90214 isSchemaChange = 0;
90215 }
90216 for(ii=0; ii<db->nDb; ii++){
90217 rc = sqlite3BtreeSavepoint(p: db->aDb[ii].pBt, op: p1, iSavepoint);
90218 if( rc!=SQLITE_OK ){
90219 goto abort_due_to_error;
90220 }
90221 }
90222 if( isSchemaChange ){
90223 sqlite3ExpirePreparedStatements(db, iCode: 0);
90224 sqlite3ResetAllSchemasOfConnection(db);
90225 db->mDbFlags |= DBFLAG_SchemaChange;
90226 }
90227 }
90228 if( rc ) goto abort_due_to_error;
90229
90230 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
90231 ** savepoints nested inside of the savepoint being operated on. */
90232 while( db->pSavepoint!=pSavepoint ){
90233 pTmp = db->pSavepoint;
90234 db->pSavepoint = pTmp->pNext;
90235 sqlite3DbFree(db, p: pTmp);
90236 db->nSavepoint--;
90237 }
90238
90239 /* If it is a RELEASE, then destroy the savepoint being operated on
90240 ** too. If it is a ROLLBACK TO, then set the number of deferred
90241 ** constraint violations present in the database to the value stored
90242 ** when the savepoint was created. */
90243 if( p1==SAVEPOINT_RELEASE ){
90244 assert( pSavepoint==db->pSavepoint );
90245 db->pSavepoint = pSavepoint->pNext;
90246 sqlite3DbFree(db, p: pSavepoint);
90247 if( !isTransaction ){
90248 db->nSavepoint--;
90249 }
90250 }else{
90251 assert( p1==SAVEPOINT_ROLLBACK );
90252 db->nDeferredCons = pSavepoint->nDeferredCons;
90253 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
90254 }
90255
90256 if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){
90257 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
90258 if( rc!=SQLITE_OK ) goto abort_due_to_error;
90259 }
90260 }
90261 }
90262 if( rc ) goto abort_due_to_error;
90263
90264 break;
90265}
90266
90267/* Opcode: AutoCommit P1 P2 * * *
90268**
90269** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
90270** back any currently active btree transactions. If there are any active
90271** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
90272** there are active writing VMs or active VMs that use shared cache.
90273**
90274** This instruction causes the VM to halt.
90275*/
90276case OP_AutoCommit: {
90277 int desiredAutoCommit;
90278 int iRollback;
90279
90280 desiredAutoCommit = pOp->p1;
90281 iRollback = pOp->p2;
90282 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
90283 assert( desiredAutoCommit==1 || iRollback==0 );
90284 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
90285 assert( p->bIsReader );
90286
90287 if( desiredAutoCommit!=db->autoCommit ){
90288 if( iRollback ){
90289 assert( desiredAutoCommit==1 );
90290 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
90291 db->autoCommit = 1;
90292 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
90293 /* If this instruction implements a COMMIT and other VMs are writing
90294 ** return an error indicating that the other VMs must complete first.
90295 */
90296 sqlite3VdbeError(p, zFormat: "cannot commit transaction - "
90297 "SQL statements in progress");
90298 rc = SQLITE_BUSY;
90299 goto abort_due_to_error;
90300 }else if( (rc = sqlite3VdbeCheckFk(p, deferred: 1))!=SQLITE_OK ){
90301 goto vdbe_return;
90302 }else{
90303 db->autoCommit = (u8)desiredAutoCommit;
90304 }
90305 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
90306 p->pc = (int)(pOp - aOp);
90307 db->autoCommit = (u8)(1-desiredAutoCommit);
90308 p->rc = rc = SQLITE_BUSY;
90309 goto vdbe_return;
90310 }
90311 sqlite3CloseSavepoints(db);
90312 if( p->rc==SQLITE_OK ){
90313 rc = SQLITE_DONE;
90314 }else{
90315 rc = SQLITE_ERROR;
90316 }
90317 goto vdbe_return;
90318 }else{
90319 sqlite3VdbeError(p,
90320 zFormat: (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
90321 (iRollback)?"cannot rollback - no transaction is active":
90322 "cannot commit - no transaction is active"));
90323
90324 rc = SQLITE_ERROR;
90325 goto abort_due_to_error;
90326 }
90327 /*NOTREACHED*/ assert(0);
90328}
90329
90330/* Opcode: Transaction P1 P2 P3 P4 P5
90331**
90332** Begin a transaction on database P1 if a transaction is not already
90333** active.
90334** If P2 is non-zero, then a write-transaction is started, or if a
90335** read-transaction is already active, it is upgraded to a write-transaction.
90336** If P2 is zero, then a read-transaction is started. If P2 is 2 or more
90337** then an exclusive transaction is started.
90338**
90339** P1 is the index of the database file on which the transaction is
90340** started. Index 0 is the main database file and index 1 is the
90341** file used for temporary tables. Indices of 2 or more are used for
90342** attached databases.
90343**
90344** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
90345** true (this flag is set if the Vdbe may modify more than one row and may
90346** throw an ABORT exception), a statement transaction may also be opened.
90347** More specifically, a statement transaction is opened iff the database
90348** connection is currently not in autocommit mode, or if there are other
90349** active statements. A statement transaction allows the changes made by this
90350** VDBE to be rolled back after an error without having to roll back the
90351** entire transaction. If no error is encountered, the statement transaction
90352** will automatically commit when the VDBE halts.
90353**
90354** If P5!=0 then this opcode also checks the schema cookie against P3
90355** and the schema generation counter against P4.
90356** The cookie changes its value whenever the database schema changes.
90357** This operation is used to detect when that the cookie has changed
90358** and that the current process needs to reread the schema. If the schema
90359** cookie in P3 differs from the schema cookie in the database header or
90360** if the schema generation counter in P4 differs from the current
90361** generation counter, then an SQLITE_SCHEMA error is raised and execution
90362** halts. The sqlite3_step() wrapper function might then reprepare the
90363** statement and rerun it from the beginning.
90364*/
90365case OP_Transaction: {
90366 Btree *pBt;
90367 int iMeta = 0;
90368
90369 assert( p->bIsReader );
90370 assert( p->readOnly==0 || pOp->p2==0 );
90371 assert( pOp->p2>=0 && pOp->p2<=2 );
90372 assert( pOp->p1>=0 && pOp->p1<db->nDb );
90373 assert( DbMaskTest(p->btreeMask, pOp->p1) );
90374 assert( rc==SQLITE_OK );
90375 if( pOp->p2 && (db->flags & (SQLITE_QueryOnly|SQLITE_CorruptRdOnly))!=0 ){
90376 if( db->flags & SQLITE_QueryOnly ){
90377 /* Writes prohibited by the "PRAGMA query_only=TRUE" statement */
90378 rc = SQLITE_READONLY;
90379 }else{
90380 /* Writes prohibited due to a prior SQLITE_CORRUPT in the current
90381 ** transaction */
90382 rc = SQLITE_CORRUPT;
90383 }
90384 goto abort_due_to_error;
90385 }
90386 pBt = db->aDb[pOp->p1].pBt;
90387
90388 if( pBt ){
90389 rc = sqlite3BtreeBeginTrans(p: pBt, wrflag: pOp->p2, pSchemaVersion: &iMeta);
90390 testcase( rc==SQLITE_BUSY_SNAPSHOT );
90391 testcase( rc==SQLITE_BUSY_RECOVERY );
90392 if( rc!=SQLITE_OK ){
90393 if( (rc&0xff)==SQLITE_BUSY ){
90394 p->pc = (int)(pOp - aOp);
90395 p->rc = rc;
90396 goto vdbe_return;
90397 }
90398 goto abort_due_to_error;
90399 }
90400
90401 if( p->usesStmtJournal
90402 && pOp->p2
90403 && (db->autoCommit==0 || db->nVdbeRead>1)
90404 ){
90405 assert( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_WRITE );
90406 if( p->iStatement==0 ){
90407 assert( db->nStatement>=0 && db->nSavepoint>=0 );
90408 db->nStatement++;
90409 p->iStatement = db->nSavepoint + db->nStatement;
90410 }
90411
90412 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
90413 if( rc==SQLITE_OK ){
90414 rc = sqlite3BtreeBeginStmt(p: pBt, iStatement: p->iStatement);
90415 }
90416
90417 /* Store the current value of the database handles deferred constraint
90418 ** counter. If the statement transaction needs to be rolled back,
90419 ** the value of this counter needs to be restored too. */
90420 p->nStmtDefCons = db->nDeferredCons;
90421 p->nStmtDefImmCons = db->nDeferredImmCons;
90422 }
90423 }
90424 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
90425 if( rc==SQLITE_OK
90426 && pOp->p5
90427 && (iMeta!=pOp->p3
90428 || db->aDb[pOp->p1].pSchema->iGeneration!=pOp->p4.i)
90429 ){
90430 /*
90431 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
90432 ** version is checked to ensure that the schema has not changed since the
90433 ** SQL statement was prepared.
90434 */
90435 sqlite3DbFree(db, p: p->zErrMsg);
90436 p->zErrMsg = sqlite3DbStrDup(db, z: "database schema has changed");
90437 /* If the schema-cookie from the database file matches the cookie
90438 ** stored with the in-memory representation of the schema, do
90439 ** not reload the schema from the database file.
90440 **
90441 ** If virtual-tables are in use, this is not just an optimization.
90442 ** Often, v-tables store their data in other SQLite tables, which
90443 ** are queried from within xNext() and other v-table methods using
90444 ** prepared queries. If such a query is out-of-date, we do not want to
90445 ** discard the database schema, as the user code implementing the
90446 ** v-table would have to be ready for the sqlite3_vtab structure itself
90447 ** to be invalidated whenever sqlite3_step() is called from within
90448 ** a v-table method.
90449 */
90450 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
90451 sqlite3ResetOneSchema(db, pOp->p1);
90452 }
90453 p->expired = 1;
90454 rc = SQLITE_SCHEMA;
90455 }
90456 if( rc ) goto abort_due_to_error;
90457 break;
90458}
90459
90460/* Opcode: ReadCookie P1 P2 P3 * *
90461**
90462** Read cookie number P3 from database P1 and write it into register P2.
90463** P3==1 is the schema version. P3==2 is the database format.
90464** P3==3 is the recommended pager cache size, and so forth. P1==0 is
90465** the main database file and P1==1 is the database file used to store
90466** temporary tables.
90467**
90468** There must be a read-lock on the database (either a transaction
90469** must be started or there must be an open cursor) before
90470** executing this instruction.
90471*/
90472case OP_ReadCookie: { /* out2 */
90473 int iMeta;
90474 int iDb;
90475 int iCookie;
90476
90477 assert( p->bIsReader );
90478 iDb = pOp->p1;
90479 iCookie = pOp->p3;
90480 assert( pOp->p3<SQLITE_N_BTREE_META );
90481 assert( iDb>=0 && iDb<db->nDb );
90482 assert( db->aDb[iDb].pBt!=0 );
90483 assert( DbMaskTest(p->btreeMask, iDb) );
90484
90485 sqlite3BtreeGetMeta(p: db->aDb[iDb].pBt, idx: iCookie, pMeta: (u32 *)&iMeta);
90486 pOut = out2Prerelease(p, pOp);
90487 pOut->u.i = iMeta;
90488 break;
90489}
90490
90491/* Opcode: SetCookie P1 P2 P3 * P5
90492**
90493** Write the integer value P3 into cookie number P2 of database P1.
90494** P2==1 is the schema version. P2==2 is the database format.
90495** P2==3 is the recommended pager cache
90496** size, and so forth. P1==0 is the main database file and P1==1 is the
90497** database file used to store temporary tables.
90498**
90499** A transaction must be started before executing this opcode.
90500**
90501** If P2 is the SCHEMA_VERSION cookie (cookie number 1) then the internal
90502** schema version is set to P3-P5. The "PRAGMA schema_version=N" statement
90503** has P5 set to 1, so that the internal schema version will be different
90504** from the database schema version, resulting in a schema reset.
90505*/
90506case OP_SetCookie: {
90507 Db *pDb;
90508
90509 sqlite3VdbeIncrWriteCounter(p, 0);
90510 assert( pOp->p2<SQLITE_N_BTREE_META );
90511 assert( pOp->p1>=0 && pOp->p1<db->nDb );
90512 assert( DbMaskTest(p->btreeMask, pOp->p1) );
90513 assert( p->readOnly==0 );
90514 pDb = &db->aDb[pOp->p1];
90515 assert( pDb->pBt!=0 );
90516 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
90517 /* See note about index shifting on OP_ReadCookie */
90518 rc = sqlite3BtreeUpdateMeta(p: pDb->pBt, idx: pOp->p2, iMeta: pOp->p3);
90519 if( pOp->p2==BTREE_SCHEMA_VERSION ){
90520 /* When the schema cookie changes, record the new cookie internally */
90521 pDb->pSchema->schema_cookie = pOp->p3 - pOp->p5;
90522 db->mDbFlags |= DBFLAG_SchemaChange;
90523 sqlite3FkClearTriggerCache(db, pOp->p1);
90524 }else if( pOp->p2==BTREE_FILE_FORMAT ){
90525 /* Record changes in the file format */
90526 pDb->pSchema->file_format = pOp->p3;
90527 }
90528 if( pOp->p1==1 ){
90529 /* Invalidate all prepared statements whenever the TEMP database
90530 ** schema is changed. Ticket #1644 */
90531 sqlite3ExpirePreparedStatements(db, iCode: 0);
90532 p->expired = 0;
90533 }
90534 if( rc ) goto abort_due_to_error;
90535 break;
90536}
90537
90538/* Opcode: OpenRead P1 P2 P3 P4 P5
90539** Synopsis: root=P2 iDb=P3
90540**
90541** Open a read-only cursor for the database table whose root page is
90542** P2 in a database file. The database file is determined by P3.
90543** P3==0 means the main database, P3==1 means the database used for
90544** temporary tables, and P3>1 means used the corresponding attached
90545** database. Give the new cursor an identifier of P1. The P1
90546** values need not be contiguous but all P1 values should be small integers.
90547** It is an error for P1 to be negative.
90548**
90549** Allowed P5 bits:
90550** <ul>
90551** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
90552** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
90553** of OP_SeekLE/OP_IdxLT)
90554** </ul>
90555**
90556** The P4 value may be either an integer (P4_INT32) or a pointer to
90557** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
90558** object, then table being opened must be an [index b-tree] where the
90559** KeyInfo object defines the content and collating
90560** sequence of that index b-tree. Otherwise, if P4 is an integer
90561** value, then the table being opened must be a [table b-tree] with a
90562** number of columns no less than the value of P4.
90563**
90564** See also: OpenWrite, ReopenIdx
90565*/
90566/* Opcode: ReopenIdx P1 P2 P3 P4 P5
90567** Synopsis: root=P2 iDb=P3
90568**
90569** The ReopenIdx opcode works like OP_OpenRead except that it first
90570** checks to see if the cursor on P1 is already open on the same
90571** b-tree and if it is this opcode becomes a no-op. In other words,
90572** if the cursor is already open, do not reopen it.
90573**
90574** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ
90575** and with P4 being a P4_KEYINFO object. Furthermore, the P3 value must
90576** be the same as every other ReopenIdx or OpenRead for the same cursor
90577** number.
90578**
90579** Allowed P5 bits:
90580** <ul>
90581** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
90582** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
90583** of OP_SeekLE/OP_IdxLT)
90584** </ul>
90585**
90586** See also: OP_OpenRead, OP_OpenWrite
90587*/
90588/* Opcode: OpenWrite P1 P2 P3 P4 P5
90589** Synopsis: root=P2 iDb=P3
90590**
90591** Open a read/write cursor named P1 on the table or index whose root
90592** page is P2 (or whose root page is held in register P2 if the
90593** OPFLAG_P2ISREG bit is set in P5 - see below).
90594**
90595** The P4 value may be either an integer (P4_INT32) or a pointer to
90596** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
90597** object, then table being opened must be an [index b-tree] where the
90598** KeyInfo object defines the content and collating
90599** sequence of that index b-tree. Otherwise, if P4 is an integer
90600** value, then the table being opened must be a [table b-tree] with a
90601** number of columns no less than the value of P4.
90602**
90603** Allowed P5 bits:
90604** <ul>
90605** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
90606** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
90607** of OP_SeekLE/OP_IdxLT)
90608** <li> <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek
90609** and subsequently delete entries in an index btree. This is a
90610** hint to the storage engine that the storage engine is allowed to
90611** ignore. The hint is not used by the official SQLite b*tree storage
90612** engine, but is used by COMDB2.
90613** <li> <b>0x10 OPFLAG_P2ISREG</b>: Use the content of register P2
90614** as the root page, not the value of P2 itself.
90615** </ul>
90616**
90617** This instruction works like OpenRead except that it opens the cursor
90618** in read/write mode.
90619**
90620** See also: OP_OpenRead, OP_ReopenIdx
90621*/
90622case OP_ReopenIdx: {
90623 int nField;
90624 KeyInfo *pKeyInfo;
90625 u32 p2;
90626 int iDb;
90627 int wrFlag;
90628 Btree *pX;
90629 VdbeCursor *pCur;
90630 Db *pDb;
90631
90632 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
90633 assert( pOp->p4type==P4_KEYINFO );
90634 pCur = p->apCsr[pOp->p1];
90635 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
90636 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
90637 assert( pCur->eCurType==CURTYPE_BTREE );
90638 sqlite3BtreeClearCursor(pCur: pCur->uc.pCursor);
90639 goto open_cursor_set_hints;
90640 }
90641 /* If the cursor is not currently open or is open on a different
90642 ** index, then fall through into OP_OpenRead to force a reopen */
90643case OP_OpenRead:
90644case OP_OpenWrite:
90645
90646 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
90647 assert( p->bIsReader );
90648 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
90649 || p->readOnly==0 );
90650
90651 if( p->expired==1 ){
90652 rc = SQLITE_ABORT_ROLLBACK;
90653 goto abort_due_to_error;
90654 }
90655
90656 nField = 0;
90657 pKeyInfo = 0;
90658 p2 = (u32)pOp->p2;
90659 iDb = pOp->p3;
90660 assert( iDb>=0 && iDb<db->nDb );
90661 assert( DbMaskTest(p->btreeMask, iDb) );
90662 pDb = &db->aDb[iDb];
90663 pX = pDb->pBt;
90664 assert( pX!=0 );
90665 if( pOp->opcode==OP_OpenWrite ){
90666 assert( OPFLAG_FORDELETE==BTREE_FORDELETE );
90667 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
90668 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
90669 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
90670 p->minWriteFileFormat = pDb->pSchema->file_format;
90671 }
90672 }else{
90673 wrFlag = 0;
90674 }
90675 if( pOp->p5 & OPFLAG_P2ISREG ){
90676 assert( p2>0 );
90677 assert( p2<=(u32)(p->nMem+1 - p->nCursor) );
90678 assert( pOp->opcode==OP_OpenWrite );
90679 pIn2 = &aMem[p2];
90680 assert( memIsValid(pIn2) );
90681 assert( (pIn2->flags & MEM_Int)!=0 );
90682 sqlite3VdbeMemIntegerify(pMem: pIn2);
90683 p2 = (int)pIn2->u.i;
90684 /* The p2 value always comes from a prior OP_CreateBtree opcode and
90685 ** that opcode will always set the p2 value to 2 or more or else fail.
90686 ** If there were a failure, the prepared statement would have halted
90687 ** before reaching this instruction. */
90688 assert( p2>=2 );
90689 }
90690 if( pOp->p4type==P4_KEYINFO ){
90691 pKeyInfo = pOp->p4.pKeyInfo;
90692 assert( pKeyInfo->enc==ENC(db) );
90693 assert( pKeyInfo->db==db );
90694 nField = pKeyInfo->nAllField;
90695 }else if( pOp->p4type==P4_INT32 ){
90696 nField = pOp->p4.i;
90697 }
90698 assert( pOp->p1>=0 );
90699 assert( nField>=0 );
90700 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
90701 pCur = allocateCursor(p, iCur: pOp->p1, nField, iDb, CURTYPE_BTREE);
90702 if( pCur==0 ) goto no_mem;
90703 pCur->nullRow = 1;
90704 pCur->isOrdered = 1;
90705 pCur->pgnoRoot = p2;
90706#ifdef SQLITE_DEBUG
90707 pCur->wrFlag = wrFlag;
90708#endif
90709 rc = sqlite3BtreeCursor(p: pX, iTable: p2, wrFlag, pKeyInfo, pCur: pCur->uc.pCursor);
90710 pCur->pKeyInfo = pKeyInfo;
90711 /* Set the VdbeCursor.isTable variable. Previous versions of
90712 ** SQLite used to check if the root-page flags were sane at this point
90713 ** and report database corruption if they were not, but this check has
90714 ** since moved into the btree layer. */
90715 pCur->isTable = pOp->p4type!=P4_KEYINFO;
90716
90717open_cursor_set_hints:
90718 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
90719 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
90720 testcase( pOp->p5 & OPFLAG_BULKCSR );
90721 testcase( pOp->p2 & OPFLAG_SEEKEQ );
90722 sqlite3BtreeCursorHintFlags(pCur: pCur->uc.pCursor,
90723 x: (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
90724 if( rc ) goto abort_due_to_error;
90725 break;
90726}
90727
90728/* Opcode: OpenDup P1 P2 * * *
90729**
90730** Open a new cursor P1 that points to the same ephemeral table as
90731** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral
90732** opcode. Only ephemeral cursors may be duplicated.
90733**
90734** Duplicate ephemeral cursors are used for self-joins of materialized views.
90735*/
90736case OP_OpenDup: {
90737 VdbeCursor *pOrig; /* The original cursor to be duplicated */
90738 VdbeCursor *pCx; /* The new cursor */
90739
90740 pOrig = p->apCsr[pOp->p2];
90741 assert( pOrig );
90742 assert( pOrig->isEphemeral ); /* Only ephemeral cursors can be duplicated */
90743
90744 pCx = allocateCursor(p, iCur: pOp->p1, nField: pOrig->nField, iDb: -1, CURTYPE_BTREE);
90745 if( pCx==0 ) goto no_mem;
90746 pCx->nullRow = 1;
90747 pCx->isEphemeral = 1;
90748 pCx->pKeyInfo = pOrig->pKeyInfo;
90749 pCx->isTable = pOrig->isTable;
90750 pCx->pgnoRoot = pOrig->pgnoRoot;
90751 pCx->isOrdered = pOrig->isOrdered;
90752 pCx->pBtx = pOrig->pBtx;
90753 pCx->hasBeenDuped = 1;
90754 pOrig->hasBeenDuped = 1;
90755 rc = sqlite3BtreeCursor(p: pCx->pBtx, iTable: pCx->pgnoRoot, BTREE_WRCSR,
90756 pKeyInfo: pCx->pKeyInfo, pCur: pCx->uc.pCursor);
90757 /* The sqlite3BtreeCursor() routine can only fail for the first cursor
90758 ** opened for a database. Since there is already an open cursor when this
90759 ** opcode is run, the sqlite3BtreeCursor() cannot fail */
90760 assert( rc==SQLITE_OK );
90761 break;
90762}
90763
90764
90765/* Opcode: OpenEphemeral P1 P2 P3 P4 P5
90766** Synopsis: nColumn=P2
90767**
90768** Open a new cursor P1 to a transient table.
90769** The cursor is always opened read/write even if
90770** the main database is read-only. The ephemeral
90771** table is deleted automatically when the cursor is closed.
90772**
90773** If the cursor P1 is already opened on an ephemeral table, the table
90774** is cleared (all content is erased).
90775**
90776** P2 is the number of columns in the ephemeral table.
90777** The cursor points to a BTree table if P4==0 and to a BTree index
90778** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
90779** that defines the format of keys in the index.
90780**
90781** The P5 parameter can be a mask of the BTREE_* flags defined
90782** in btree.h. These flags control aspects of the operation of
90783** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
90784** added automatically.
90785**
90786** If P3 is positive, then reg[P3] is modified slightly so that it
90787** can be used as zero-length data for OP_Insert. This is an optimization
90788** that avoids an extra OP_Blob opcode to initialize that register.
90789*/
90790/* Opcode: OpenAutoindex P1 P2 * P4 *
90791** Synopsis: nColumn=P2
90792**
90793** This opcode works the same as OP_OpenEphemeral. It has a
90794** different name to distinguish its use. Tables created using
90795** by this opcode will be used for automatically created transient
90796** indices in joins.
90797*/
90798case OP_OpenAutoindex:
90799case OP_OpenEphemeral: {
90800 VdbeCursor *pCx;
90801 KeyInfo *pKeyInfo;
90802
90803 static const int vfsFlags =
90804 SQLITE_OPEN_READWRITE |
90805 SQLITE_OPEN_CREATE |
90806 SQLITE_OPEN_EXCLUSIVE |
90807 SQLITE_OPEN_DELETEONCLOSE |
90808 SQLITE_OPEN_TRANSIENT_DB;
90809 assert( pOp->p1>=0 );
90810 assert( pOp->p2>=0 );
90811 if( pOp->p3>0 ){
90812 /* Make register reg[P3] into a value that can be used as the data
90813 ** form sqlite3BtreeInsert() where the length of the data is zero. */
90814 assert( pOp->p2==0 ); /* Only used when number of columns is zero */
90815 assert( pOp->opcode==OP_OpenEphemeral );
90816 assert( aMem[pOp->p3].flags & MEM_Null );
90817 aMem[pOp->p3].n = 0;
90818 aMem[pOp->p3].z = "";
90819 }
90820 pCx = p->apCsr[pOp->p1];
90821 if( pCx && !pCx->hasBeenDuped && ALWAYS(pOp->p2<=pCx->nField) ){
90822 /* If the ephermeral table is already open and has no duplicates from
90823 ** OP_OpenDup, then erase all existing content so that the table is
90824 ** empty again, rather than creating a new table. */
90825 assert( pCx->isEphemeral );
90826 pCx->seqCount = 0;
90827 pCx->cacheStatus = CACHE_STALE;
90828 rc = sqlite3BtreeClearTable(p: pCx->pBtx, iTable: pCx->pgnoRoot, pnChange: 0);
90829 }else{
90830 pCx = allocateCursor(p, iCur: pOp->p1, nField: pOp->p2, iDb: -1, CURTYPE_BTREE);
90831 if( pCx==0 ) goto no_mem;
90832 pCx->isEphemeral = 1;
90833 rc = sqlite3BtreeOpen(pVfs: db->pVfs, zFilename: 0, db, ppBtree: &pCx->pBtx,
90834 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5,
90835 vfsFlags);
90836 if( rc==SQLITE_OK ){
90837 rc = sqlite3BtreeBeginTrans(p: pCx->pBtx, wrflag: 1, pSchemaVersion: 0);
90838 if( rc==SQLITE_OK ){
90839 /* If a transient index is required, create it by calling
90840 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
90841 ** opening it. If a transient table is required, just use the
90842 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
90843 */
90844 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
90845 assert( pOp->p4type==P4_KEYINFO );
90846 rc = sqlite3BtreeCreateTable(p: pCx->pBtx, piTable: &pCx->pgnoRoot,
90847 BTREE_BLOBKEY | pOp->p5);
90848 if( rc==SQLITE_OK ){
90849 assert( pCx->pgnoRoot==SCHEMA_ROOT+1 );
90850 assert( pKeyInfo->db==db );
90851 assert( pKeyInfo->enc==ENC(db) );
90852 rc = sqlite3BtreeCursor(p: pCx->pBtx, iTable: pCx->pgnoRoot, BTREE_WRCSR,
90853 pKeyInfo, pCur: pCx->uc.pCursor);
90854 }
90855 pCx->isTable = 0;
90856 }else{
90857 pCx->pgnoRoot = SCHEMA_ROOT;
90858 rc = sqlite3BtreeCursor(p: pCx->pBtx, SCHEMA_ROOT, BTREE_WRCSR,
90859 pKeyInfo: 0, pCur: pCx->uc.pCursor);
90860 pCx->isTable = 1;
90861 }
90862 }
90863 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
90864 if( rc ){
90865 sqlite3BtreeClose(p: pCx->pBtx);
90866 }
90867 }
90868 }
90869 if( rc ) goto abort_due_to_error;
90870 pCx->nullRow = 1;
90871 break;
90872}
90873
90874/* Opcode: SorterOpen P1 P2 P3 P4 *
90875**
90876** This opcode works like OP_OpenEphemeral except that it opens
90877** a transient index that is specifically designed to sort large
90878** tables using an external merge-sort algorithm.
90879**
90880** If argument P3 is non-zero, then it indicates that the sorter may
90881** assume that a stable sort considering the first P3 fields of each
90882** key is sufficient to produce the required results.
90883*/
90884case OP_SorterOpen: {
90885 VdbeCursor *pCx;
90886
90887 assert( pOp->p1>=0 );
90888 assert( pOp->p2>=0 );
90889 pCx = allocateCursor(p, iCur: pOp->p1, nField: pOp->p2, iDb: -1, CURTYPE_SORTER);
90890 if( pCx==0 ) goto no_mem;
90891 pCx->pKeyInfo = pOp->p4.pKeyInfo;
90892 assert( pCx->pKeyInfo->db==db );
90893 assert( pCx->pKeyInfo->enc==ENC(db) );
90894 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
90895 if( rc ) goto abort_due_to_error;
90896 break;
90897}
90898
90899/* Opcode: SequenceTest P1 P2 * * *
90900** Synopsis: if( cursor[P1].ctr++ ) pc = P2
90901**
90902** P1 is a sorter cursor. If the sequence counter is currently zero, jump
90903** to P2. Regardless of whether or not the jump is taken, increment the
90904** the sequence value.
90905*/
90906case OP_SequenceTest: {
90907 VdbeCursor *pC;
90908 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90909 pC = p->apCsr[pOp->p1];
90910 assert( isSorter(pC) );
90911 if( (pC->seqCount++)==0 ){
90912 goto jump_to_p2;
90913 }
90914 break;
90915}
90916
90917/* Opcode: OpenPseudo P1 P2 P3 * *
90918** Synopsis: P3 columns in r[P2]
90919**
90920** Open a new cursor that points to a fake table that contains a single
90921** row of data. The content of that one row is the content of memory
90922** register P2. In other words, cursor P1 becomes an alias for the
90923** MEM_Blob content contained in register P2.
90924**
90925** A pseudo-table created by this opcode is used to hold a single
90926** row output from the sorter so that the row can be decomposed into
90927** individual columns using the OP_Column opcode. The OP_Column opcode
90928** is the only cursor opcode that works with a pseudo-table.
90929**
90930** P3 is the number of fields in the records that will be stored by
90931** the pseudo-table.
90932*/
90933case OP_OpenPseudo: {
90934 VdbeCursor *pCx;
90935
90936 assert( pOp->p1>=0 );
90937 assert( pOp->p3>=0 );
90938 pCx = allocateCursor(p, iCur: pOp->p1, nField: pOp->p3, iDb: -1, CURTYPE_PSEUDO);
90939 if( pCx==0 ) goto no_mem;
90940 pCx->nullRow = 1;
90941 pCx->seekResult = pOp->p2;
90942 pCx->isTable = 1;
90943 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
90944 ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test
90945 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
90946 ** which is a performance optimization */
90947 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
90948 assert( pOp->p5==0 );
90949 break;
90950}
90951
90952/* Opcode: Close P1 * * * *
90953**
90954** Close a cursor previously opened as P1. If P1 is not
90955** currently open, this instruction is a no-op.
90956*/
90957case OP_Close: {
90958 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90959 sqlite3VdbeFreeCursor(p, pCx: p->apCsr[pOp->p1]);
90960 p->apCsr[pOp->p1] = 0;
90961 break;
90962}
90963
90964#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
90965/* Opcode: ColumnsUsed P1 * * P4 *
90966**
90967** This opcode (which only exists if SQLite was compiled with
90968** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
90969** table or index for cursor P1 are used. P4 is a 64-bit integer
90970** (P4_INT64) in which the first 63 bits are one for each of the
90971** first 63 columns of the table or index that are actually used
90972** by the cursor. The high-order bit is set if any column after
90973** the 64th is used.
90974*/
90975case OP_ColumnsUsed: {
90976 VdbeCursor *pC;
90977 pC = p->apCsr[pOp->p1];
90978 assert( pC->eCurType==CURTYPE_BTREE );
90979 pC->maskUsed = *(u64*)pOp->p4.pI64;
90980 break;
90981}
90982#endif
90983
90984/* Opcode: SeekGE P1 P2 P3 P4 *
90985** Synopsis: key=r[P3@P4]
90986**
90987** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
90988** use the value in register P3 as the key. If cursor P1 refers
90989** to an SQL index, then P3 is the first in an array of P4 registers
90990** that are used as an unpacked index key.
90991**
90992** Reposition cursor P1 so that it points to the smallest entry that
90993** is greater than or equal to the key value. If there are no records
90994** greater than or equal to the key and P2 is not zero, then jump to P2.
90995**
90996** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
90997** opcode will either land on a record that exactly matches the key, or
90998** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ,
90999** this opcode must be followed by an IdxLE opcode with the same arguments.
91000** The IdxGT opcode will be skipped if this opcode succeeds, but the
91001** IdxGT opcode will be used on subsequent loop iterations. The
91002** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
91003** is an equality search.
91004**
91005** This opcode leaves the cursor configured to move in forward order,
91006** from the beginning toward the end. In other words, the cursor is
91007** configured to use Next, not Prev.
91008**
91009** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
91010*/
91011/* Opcode: SeekGT P1 P2 P3 P4 *
91012** Synopsis: key=r[P3@P4]
91013**
91014** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
91015** use the value in register P3 as a key. If cursor P1 refers
91016** to an SQL index, then P3 is the first in an array of P4 registers
91017** that are used as an unpacked index key.
91018**
91019** Reposition cursor P1 so that it points to the smallest entry that
91020** is greater than the key value. If there are no records greater than
91021** the key and P2 is not zero, then jump to P2.
91022**
91023** This opcode leaves the cursor configured to move in forward order,
91024** from the beginning toward the end. In other words, the cursor is
91025** configured to use Next, not Prev.
91026**
91027** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
91028*/
91029/* Opcode: SeekLT P1 P2 P3 P4 *
91030** Synopsis: key=r[P3@P4]
91031**
91032** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
91033** use the value in register P3 as a key. If cursor P1 refers
91034** to an SQL index, then P3 is the first in an array of P4 registers
91035** that are used as an unpacked index key.
91036**
91037** Reposition cursor P1 so that it points to the largest entry that
91038** is less than the key value. If there are no records less than
91039** the key and P2 is not zero, then jump to P2.
91040**
91041** This opcode leaves the cursor configured to move in reverse order,
91042** from the end toward the beginning. In other words, the cursor is
91043** configured to use Prev, not Next.
91044**
91045** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
91046*/
91047/* Opcode: SeekLE P1 P2 P3 P4 *
91048** Synopsis: key=r[P3@P4]
91049**
91050** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
91051** use the value in register P3 as a key. If cursor P1 refers
91052** to an SQL index, then P3 is the first in an array of P4 registers
91053** that are used as an unpacked index key.
91054**
91055** Reposition cursor P1 so that it points to the largest entry that
91056** is less than or equal to the key value. If there are no records
91057** less than or equal to the key and P2 is not zero, then jump to P2.
91058**
91059** This opcode leaves the cursor configured to move in reverse order,
91060** from the end toward the beginning. In other words, the cursor is
91061** configured to use Prev, not Next.
91062**
91063** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
91064** opcode will either land on a record that exactly matches the key, or
91065** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ,
91066** this opcode must be followed by an IdxLE opcode with the same arguments.
91067** The IdxGE opcode will be skipped if this opcode succeeds, but the
91068** IdxGE opcode will be used on subsequent loop iterations. The
91069** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
91070** is an equality search.
91071**
91072** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
91073*/
91074case OP_SeekLT: /* jump, in3, group */
91075case OP_SeekLE: /* jump, in3, group */
91076case OP_SeekGE: /* jump, in3, group */
91077case OP_SeekGT: { /* jump, in3, group */
91078 int res; /* Comparison result */
91079 int oc; /* Opcode */
91080 VdbeCursor *pC; /* The cursor to seek */
91081 UnpackedRecord r; /* The key to seek for */
91082 int nField; /* Number of columns or fields in the key */
91083 i64 iKey; /* The rowid we are to seek to */
91084 int eqOnly; /* Only interested in == results */
91085
91086 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91087 assert( pOp->p2!=0 );
91088 pC = p->apCsr[pOp->p1];
91089 assert( pC!=0 );
91090 assert( pC->eCurType==CURTYPE_BTREE );
91091 assert( OP_SeekLE == OP_SeekLT+1 );
91092 assert( OP_SeekGE == OP_SeekLT+2 );
91093 assert( OP_SeekGT == OP_SeekLT+3 );
91094 assert( pC->isOrdered );
91095 assert( pC->uc.pCursor!=0 );
91096 oc = pOp->opcode;
91097 eqOnly = 0;
91098 pC->nullRow = 0;
91099#ifdef SQLITE_DEBUG
91100 pC->seekOp = pOp->opcode;
91101#endif
91102
91103 pC->deferredMoveto = 0;
91104 pC->cacheStatus = CACHE_STALE;
91105 if( pC->isTable ){
91106 u16 flags3, newType;
91107 /* The OPFLAG_SEEKEQ/BTREE_SEEK_EQ flag is only set on index cursors */
91108 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
91109 || CORRUPT_DB );
91110
91111 /* The input value in P3 might be of any type: integer, real, string,
91112 ** blob, or NULL. But it needs to be an integer before we can do
91113 ** the seek, so convert it. */
91114 pIn3 = &aMem[pOp->p3];
91115 flags3 = pIn3->flags;
91116 if( (flags3 & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Str))==MEM_Str ){
91117 applyNumericAffinity(pRec: pIn3, bTryForInt: 0);
91118 }
91119 iKey = sqlite3VdbeIntValue(pMem: pIn3); /* Get the integer key value */
91120 newType = pIn3->flags; /* Record the type after applying numeric affinity */
91121 pIn3->flags = flags3; /* But convert the type back to its original */
91122
91123 /* If the P3 value could not be converted into an integer without
91124 ** loss of information, then special processing is required... */
91125 if( (newType & (MEM_Int|MEM_IntReal))==0 ){
91126 int c;
91127 if( (newType & MEM_Real)==0 ){
91128 if( (newType & MEM_Null) || oc>=OP_SeekGE ){
91129 VdbeBranchTaken(1,2);
91130 goto jump_to_p2;
91131 }else{
91132 rc = sqlite3BtreeLast(pCur: pC->uc.pCursor, pRes: &res);
91133 if( rc!=SQLITE_OK ) goto abort_due_to_error;
91134 goto seek_not_found;
91135 }
91136 }
91137 c = sqlite3IntFloatCompare(i: iKey, r: pIn3->u.r);
91138
91139 /* If the approximation iKey is larger than the actual real search
91140 ** term, substitute >= for > and < for <=. e.g. if the search term
91141 ** is 4.9 and the integer approximation 5:
91142 **
91143 ** (x > 4.9) -> (x >= 5)
91144 ** (x <= 4.9) -> (x < 5)
91145 */
91146 if( c>0 ){
91147 assert( OP_SeekGE==(OP_SeekGT-1) );
91148 assert( OP_SeekLT==(OP_SeekLE-1) );
91149 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) );
91150 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
91151 }
91152
91153 /* If the approximation iKey is smaller than the actual real search
91154 ** term, substitute <= for < and > for >=. */
91155 else if( c<0 ){
91156 assert( OP_SeekLE==(OP_SeekLT+1) );
91157 assert( OP_SeekGT==(OP_SeekGE+1) );
91158 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
91159 if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
91160 }
91161 }
91162 rc = sqlite3BtreeTableMoveto(pCur: pC->uc.pCursor, intKey: (u64)iKey, biasRight: 0, pRes: &res);
91163 pC->movetoTarget = iKey; /* Used by OP_Delete */
91164 if( rc!=SQLITE_OK ){
91165 goto abort_due_to_error;
91166 }
91167 }else{
91168 /* For a cursor with the OPFLAG_SEEKEQ/BTREE_SEEK_EQ hint, only the
91169 ** OP_SeekGE and OP_SeekLE opcodes are allowed, and these must be
91170 ** immediately followed by an OP_IdxGT or OP_IdxLT opcode, respectively,
91171 ** with the same key.
91172 */
91173 if( sqlite3BtreeCursorHasHint(pCsr: pC->uc.pCursor, BTREE_SEEK_EQ) ){
91174 eqOnly = 1;
91175 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
91176 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
91177 assert( pOp->opcode==OP_SeekGE || pOp[1].opcode==OP_IdxLT );
91178 assert( pOp->opcode==OP_SeekLE || pOp[1].opcode==OP_IdxGT );
91179 assert( pOp[1].p1==pOp[0].p1 );
91180 assert( pOp[1].p2==pOp[0].p2 );
91181 assert( pOp[1].p3==pOp[0].p3 );
91182 assert( pOp[1].p4.i==pOp[0].p4.i );
91183 }
91184
91185 nField = pOp->p4.i;
91186 assert( pOp->p4type==P4_INT32 );
91187 assert( nField>0 );
91188 r.pKeyInfo = pC->pKeyInfo;
91189 r.nField = (u16)nField;
91190
91191 /* The next line of code computes as follows, only faster:
91192 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
91193 ** r.default_rc = -1;
91194 ** }else{
91195 ** r.default_rc = +1;
91196 ** }
91197 */
91198 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
91199 assert( oc!=OP_SeekGT || r.default_rc==-1 );
91200 assert( oc!=OP_SeekLE || r.default_rc==-1 );
91201 assert( oc!=OP_SeekGE || r.default_rc==+1 );
91202 assert( oc!=OP_SeekLT || r.default_rc==+1 );
91203
91204 r.aMem = &aMem[pOp->p3];
91205#ifdef SQLITE_DEBUG
91206 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
91207#endif
91208 r.eqSeen = 0;
91209 rc = sqlite3BtreeIndexMoveto(pCur: pC->uc.pCursor, pIdxKey: &r, pRes: &res);
91210 if( rc!=SQLITE_OK ){
91211 goto abort_due_to_error;
91212 }
91213 if( eqOnly && r.eqSeen==0 ){
91214 assert( res!=0 );
91215 goto seek_not_found;
91216 }
91217 }
91218#ifdef SQLITE_TEST
91219 sqlite3_search_count++;
91220#endif
91221 if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT );
91222 if( res<0 || (res==0 && oc==OP_SeekGT) ){
91223 res = 0;
91224 rc = sqlite3BtreeNext(pCur: pC->uc.pCursor, flags: 0);
91225 if( rc!=SQLITE_OK ){
91226 if( rc==SQLITE_DONE ){
91227 rc = SQLITE_OK;
91228 res = 1;
91229 }else{
91230 goto abort_due_to_error;
91231 }
91232 }
91233 }else{
91234 res = 0;
91235 }
91236 }else{
91237 assert( oc==OP_SeekLT || oc==OP_SeekLE );
91238 if( res>0 || (res==0 && oc==OP_SeekLT) ){
91239 res = 0;
91240 rc = sqlite3BtreePrevious(pCur: pC->uc.pCursor, flags: 0);
91241 if( rc!=SQLITE_OK ){
91242 if( rc==SQLITE_DONE ){
91243 rc = SQLITE_OK;
91244 res = 1;
91245 }else{
91246 goto abort_due_to_error;
91247 }
91248 }
91249 }else{
91250 /* res might be negative because the table is empty. Check to
91251 ** see if this is the case.
91252 */
91253 res = sqlite3BtreeEof(pCur: pC->uc.pCursor);
91254 }
91255 }
91256seek_not_found:
91257 assert( pOp->p2>0 );
91258 VdbeBranchTaken(res!=0,2);
91259 if( res ){
91260 goto jump_to_p2;
91261 }else if( eqOnly ){
91262 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
91263 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
91264 }
91265 break;
91266}
91267
91268
91269/* Opcode: SeekScan P1 P2 * * *
91270** Synopsis: Scan-ahead up to P1 rows
91271**
91272** This opcode is a prefix opcode to OP_SeekGE. In other words, this
91273** opcode must be immediately followed by OP_SeekGE. This constraint is
91274** checked by assert() statements.
91275**
91276** This opcode uses the P1 through P4 operands of the subsequent
91277** OP_SeekGE. In the text that follows, the operands of the subsequent
91278** OP_SeekGE opcode are denoted as SeekOP.P1 through SeekOP.P4. Only
91279** the P1 and P2 operands of this opcode are also used, and are called
91280** This.P1 and This.P2.
91281**
91282** This opcode helps to optimize IN operators on a multi-column index
91283** where the IN operator is on the later terms of the index by avoiding
91284** unnecessary seeks on the btree, substituting steps to the next row
91285** of the b-tree instead. A correct answer is obtained if this opcode
91286** is omitted or is a no-op.
91287**
91288** The SeekGE.P3 and SeekGE.P4 operands identify an unpacked key which
91289** is the desired entry that we want the cursor SeekGE.P1 to be pointing
91290** to. Call this SeekGE.P4/P5 row the "target".
91291**
91292** If the SeekGE.P1 cursor is not currently pointing to a valid row,
91293** then this opcode is a no-op and control passes through into the OP_SeekGE.
91294**
91295** If the SeekGE.P1 cursor is pointing to a valid row, then that row
91296** might be the target row, or it might be near and slightly before the
91297** target row. This opcode attempts to position the cursor on the target
91298** row by, perhaps by invoking sqlite3BtreeStep() on the cursor
91299** between 0 and This.P1 times.
91300**
91301** There are three possible outcomes from this opcode:<ol>
91302**
91303** <li> If after This.P1 steps, the cursor is still pointing to a place that
91304** is earlier in the btree than the target row, then fall through
91305** into the subsquence OP_SeekGE opcode.
91306**
91307** <li> If the cursor is successfully moved to the target row by 0 or more
91308** sqlite3BtreeNext() calls, then jump to This.P2, which will land just
91309** past the OP_IdxGT or OP_IdxGE opcode that follows the OP_SeekGE.
91310**
91311** <li> If the cursor ends up past the target row (indicating the the target
91312** row does not exist in the btree) then jump to SeekOP.P2.
91313** </ol>
91314*/
91315case OP_SeekScan: {
91316 VdbeCursor *pC;
91317 int res;
91318 int nStep;
91319 UnpackedRecord r;
91320
91321 assert( pOp[1].opcode==OP_SeekGE );
91322
91323 /* pOp->p2 points to the first instruction past the OP_IdxGT that
91324 ** follows the OP_SeekGE. */
91325 assert( pOp->p2>=(int)(pOp-aOp)+2 );
91326 assert( aOp[pOp->p2-1].opcode==OP_IdxGT || aOp[pOp->p2-1].opcode==OP_IdxGE );
91327 testcase( aOp[pOp->p2-1].opcode==OP_IdxGE );
91328 assert( pOp[1].p1==aOp[pOp->p2-1].p1 );
91329 assert( pOp[1].p2==aOp[pOp->p2-1].p2 );
91330 assert( pOp[1].p3==aOp[pOp->p2-1].p3 );
91331
91332 assert( pOp->p1>0 );
91333 pC = p->apCsr[pOp[1].p1];
91334 assert( pC!=0 );
91335 assert( pC->eCurType==CURTYPE_BTREE );
91336 assert( !pC->isTable );
91337 if( !sqlite3BtreeCursorIsValidNN(pCur: pC->uc.pCursor) ){
91338#ifdef SQLITE_DEBUG
91339 if( db->flags&SQLITE_VdbeTrace ){
91340 printf("... cursor not valid - fall through\n");
91341 }
91342#endif
91343 break;
91344 }
91345 nStep = pOp->p1;
91346 assert( nStep>=1 );
91347 r.pKeyInfo = pC->pKeyInfo;
91348 r.nField = (u16)pOp[1].p4.i;
91349 r.default_rc = 0;
91350 r.aMem = &aMem[pOp[1].p3];
91351#ifdef SQLITE_DEBUG
91352 {
91353 int i;
91354 for(i=0; i<r.nField; i++){
91355 assert( memIsValid(&r.aMem[i]) );
91356 REGISTER_TRACE(pOp[1].p3+i, &aMem[pOp[1].p3+i]);
91357 }
91358 }
91359#endif
91360 res = 0; /* Not needed. Only used to silence a warning. */
91361 while(1){
91362 rc = sqlite3VdbeIdxKeyCompare(db, pC, pUnpacked: &r, res: &res);
91363 if( rc ) goto abort_due_to_error;
91364 if( res>0 ){
91365 seekscan_search_fail:
91366#ifdef SQLITE_DEBUG
91367 if( db->flags&SQLITE_VdbeTrace ){
91368 printf("... %d steps and then skip\n", pOp->p1 - nStep);
91369 }
91370#endif
91371 VdbeBranchTaken(1,3);
91372 pOp++;
91373 goto jump_to_p2;
91374 }
91375 if( res==0 ){
91376#ifdef SQLITE_DEBUG
91377 if( db->flags&SQLITE_VdbeTrace ){
91378 printf("... %d steps and then success\n", pOp->p1 - nStep);
91379 }
91380#endif
91381 VdbeBranchTaken(2,3);
91382 goto jump_to_p2;
91383 break;
91384 }
91385 if( nStep<=0 ){
91386#ifdef SQLITE_DEBUG
91387 if( db->flags&SQLITE_VdbeTrace ){
91388 printf("... fall through after %d steps\n", pOp->p1);
91389 }
91390#endif
91391 VdbeBranchTaken(0,3);
91392 break;
91393 }
91394 nStep--;
91395 rc = sqlite3BtreeNext(pCur: pC->uc.pCursor, flags: 0);
91396 if( rc ){
91397 if( rc==SQLITE_DONE ){
91398 rc = SQLITE_OK;
91399 goto seekscan_search_fail;
91400 }else{
91401 goto abort_due_to_error;
91402 }
91403 }
91404 }
91405
91406 break;
91407}
91408
91409
91410/* Opcode: SeekHit P1 P2 P3 * *
91411** Synopsis: set P2<=seekHit<=P3
91412**
91413** Increase or decrease the seekHit value for cursor P1, if necessary,
91414** so that it is no less than P2 and no greater than P3.
91415**
91416** The seekHit integer represents the maximum of terms in an index for which
91417** there is known to be at least one match. If the seekHit value is smaller
91418** than the total number of equality terms in an index lookup, then the
91419** OP_IfNoHope opcode might run to see if the IN loop can be abandoned
91420** early, thus saving work. This is part of the IN-early-out optimization.
91421**
91422** P1 must be a valid b-tree cursor.
91423*/
91424case OP_SeekHit: {
91425 VdbeCursor *pC;
91426 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91427 pC = p->apCsr[pOp->p1];
91428 assert( pC!=0 );
91429 assert( pOp->p3>=pOp->p2 );
91430 if( pC->seekHit<pOp->p2 ){
91431#ifdef SQLITE_DEBUG
91432 if( db->flags&SQLITE_VdbeTrace ){
91433 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p2);
91434 }
91435#endif
91436 pC->seekHit = pOp->p2;
91437 }else if( pC->seekHit>pOp->p3 ){
91438#ifdef SQLITE_DEBUG
91439 if( db->flags&SQLITE_VdbeTrace ){
91440 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p3);
91441 }
91442#endif
91443 pC->seekHit = pOp->p3;
91444 }
91445 break;
91446}
91447
91448/* Opcode: IfNotOpen P1 P2 * * *
91449** Synopsis: if( !csr[P1] ) goto P2
91450**
91451** If cursor P1 is not open, jump to instruction P2. Otherwise, fall through.
91452*/
91453case OP_IfNotOpen: { /* jump */
91454 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91455 VdbeBranchTaken(p->apCsr[pOp->p1]==0, 2);
91456 if( !p->apCsr[pOp->p1] ){
91457 goto jump_to_p2_and_check_for_interrupt;
91458 }
91459 break;
91460}
91461
91462/* Opcode: Found P1 P2 P3 P4 *
91463** Synopsis: key=r[P3@P4]
91464**
91465** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
91466** P4>0 then register P3 is the first of P4 registers that form an unpacked
91467** record.
91468**
91469** Cursor P1 is on an index btree. If the record identified by P3 and P4
91470** is a prefix of any entry in P1 then a jump is made to P2 and
91471** P1 is left pointing at the matching entry.
91472**
91473** This operation leaves the cursor in a state where it can be
91474** advanced in the forward direction. The Next instruction will work,
91475** but not the Prev instruction.
91476**
91477** See also: NotFound, NoConflict, NotExists. SeekGe
91478*/
91479/* Opcode: NotFound P1 P2 P3 P4 *
91480** Synopsis: key=r[P3@P4]
91481**
91482** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
91483** P4>0 then register P3 is the first of P4 registers that form an unpacked
91484** record.
91485**
91486** Cursor P1 is on an index btree. If the record identified by P3 and P4
91487** is not the prefix of any entry in P1 then a jump is made to P2. If P1
91488** does contain an entry whose prefix matches the P3/P4 record then control
91489** falls through to the next instruction and P1 is left pointing at the
91490** matching entry.
91491**
91492** This operation leaves the cursor in a state where it cannot be
91493** advanced in either direction. In other words, the Next and Prev
91494** opcodes do not work after this operation.
91495**
91496** See also: Found, NotExists, NoConflict, IfNoHope
91497*/
91498/* Opcode: IfNoHope P1 P2 P3 P4 *
91499** Synopsis: key=r[P3@P4]
91500**
91501** Register P3 is the first of P4 registers that form an unpacked
91502** record. Cursor P1 is an index btree. P2 is a jump destination.
91503** In other words, the operands to this opcode are the same as the
91504** operands to OP_NotFound and OP_IdxGT.
91505**
91506** This opcode is an optimization attempt only. If this opcode always
91507** falls through, the correct answer is still obtained, but extra works
91508** is performed.
91509**
91510** A value of N in the seekHit flag of cursor P1 means that there exists
91511** a key P3:N that will match some record in the index. We want to know
91512** if it is possible for a record P3:P4 to match some record in the
91513** index. If it is not possible, we can skips some work. So if seekHit
91514** is less than P4, attempt to find out if a match is possible by running
91515** OP_NotFound.
91516**
91517** This opcode is used in IN clause processing for a multi-column key.
91518** If an IN clause is attached to an element of the key other than the
91519** left-most element, and if there are no matches on the most recent
91520** seek over the whole key, then it might be that one of the key element
91521** to the left is prohibiting a match, and hence there is "no hope" of
91522** any match regardless of how many IN clause elements are checked.
91523** In such a case, we abandon the IN clause search early, using this
91524** opcode. The opcode name comes from the fact that the
91525** jump is taken if there is "no hope" of achieving a match.
91526**
91527** See also: NotFound, SeekHit
91528*/
91529/* Opcode: NoConflict P1 P2 P3 P4 *
91530** Synopsis: key=r[P3@P4]
91531**
91532** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
91533** P4>0 then register P3 is the first of P4 registers that form an unpacked
91534** record.
91535**
91536** Cursor P1 is on an index btree. If the record identified by P3 and P4
91537** contains any NULL value, jump immediately to P2. If all terms of the
91538** record are not-NULL then a check is done to determine if any row in the
91539** P1 index btree has a matching key prefix. If there are no matches, jump
91540** immediately to P2. If there is a match, fall through and leave the P1
91541** cursor pointing to the matching row.
91542**
91543** This opcode is similar to OP_NotFound with the exceptions that the
91544** branch is always taken if any part of the search key input is NULL.
91545**
91546** This operation leaves the cursor in a state where it cannot be
91547** advanced in either direction. In other words, the Next and Prev
91548** opcodes do not work after this operation.
91549**
91550** See also: NotFound, Found, NotExists
91551*/
91552case OP_IfNoHope: { /* jump, in3 */
91553 VdbeCursor *pC;
91554 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91555 pC = p->apCsr[pOp->p1];
91556 assert( pC!=0 );
91557#ifdef SQLITE_DEBUG
91558 if( db->flags&SQLITE_VdbeTrace ){
91559 printf("seekHit is %d\n", pC->seekHit);
91560 }
91561#endif
91562 if( pC->seekHit>=pOp->p4.i ) break;
91563 /* Fall through into OP_NotFound */
91564 /* no break */ deliberate_fall_through
91565}
91566case OP_NoConflict: /* jump, in3 */
91567case OP_NotFound: /* jump, in3 */
91568case OP_Found: { /* jump, in3 */
91569 int alreadyExists;
91570 int takeJump;
91571 int ii;
91572 VdbeCursor *pC;
91573 int res;
91574 UnpackedRecord *pFree;
91575 UnpackedRecord *pIdxKey;
91576 UnpackedRecord r;
91577
91578#ifdef SQLITE_TEST
91579 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
91580#endif
91581
91582 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91583 assert( pOp->p4type==P4_INT32 );
91584 pC = p->apCsr[pOp->p1];
91585 assert( pC!=0 );
91586#ifdef SQLITE_DEBUG
91587 pC->seekOp = pOp->opcode;
91588#endif
91589 pIn3 = &aMem[pOp->p3];
91590 assert( pC->eCurType==CURTYPE_BTREE );
91591 assert( pC->uc.pCursor!=0 );
91592 assert( pC->isTable==0 );
91593 if( pOp->p4.i>0 ){
91594 r.pKeyInfo = pC->pKeyInfo;
91595 r.nField = (u16)pOp->p4.i;
91596 r.aMem = pIn3;
91597#ifdef SQLITE_DEBUG
91598 for(ii=0; ii<r.nField; ii++){
91599 assert( memIsValid(&r.aMem[ii]) );
91600 assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 );
91601 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
91602 }
91603#endif
91604 pIdxKey = &r;
91605 pFree = 0;
91606 }else{
91607 assert( pIn3->flags & MEM_Blob );
91608 rc = ExpandBlob(pIn3);
91609 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
91610 if( rc ) goto no_mem;
91611 pFree = pIdxKey = sqlite3VdbeAllocUnpackedRecord(pKeyInfo: pC->pKeyInfo);
91612 if( pIdxKey==0 ) goto no_mem;
91613 sqlite3VdbeRecordUnpack(pKeyInfo: pC->pKeyInfo, nKey: pIn3->n, pKey: pIn3->z, p: pIdxKey);
91614 }
91615 pIdxKey->default_rc = 0;
91616 takeJump = 0;
91617 if( pOp->opcode==OP_NoConflict ){
91618 /* For the OP_NoConflict opcode, take the jump if any of the
91619 ** input fields are NULL, since any key with a NULL will not
91620 ** conflict */
91621 for(ii=0; ii<pIdxKey->nField; ii++){
91622 if( pIdxKey->aMem[ii].flags & MEM_Null ){
91623 takeJump = 1;
91624 break;
91625 }
91626 }
91627 }
91628 rc = sqlite3BtreeIndexMoveto(pCur: pC->uc.pCursor, pIdxKey, pRes: &res);
91629 if( pFree ) sqlite3DbFreeNN(db, p: pFree);
91630 if( rc!=SQLITE_OK ){
91631 goto abort_due_to_error;
91632 }
91633 pC->seekResult = res;
91634 alreadyExists = (res==0);
91635 pC->nullRow = 1-alreadyExists;
91636 pC->deferredMoveto = 0;
91637 pC->cacheStatus = CACHE_STALE;
91638 if( pOp->opcode==OP_Found ){
91639 VdbeBranchTaken(alreadyExists!=0,2);
91640 if( alreadyExists ) goto jump_to_p2;
91641 }else{
91642 VdbeBranchTaken(takeJump||alreadyExists==0,2);
91643 if( takeJump || !alreadyExists ) goto jump_to_p2;
91644 if( pOp->opcode==OP_IfNoHope ) pC->seekHit = pOp->p4.i;
91645 }
91646 break;
91647}
91648
91649/* Opcode: SeekRowid P1 P2 P3 * *
91650** Synopsis: intkey=r[P3]
91651**
91652** P1 is the index of a cursor open on an SQL table btree (with integer
91653** keys). If register P3 does not contain an integer or if P1 does not
91654** contain a record with rowid P3 then jump immediately to P2.
91655** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain
91656** a record with rowid P3 then
91657** leave the cursor pointing at that record and fall through to the next
91658** instruction.
91659**
91660** The OP_NotExists opcode performs the same operation, but with OP_NotExists
91661** the P3 register must be guaranteed to contain an integer value. With this
91662** opcode, register P3 might not contain an integer.
91663**
91664** The OP_NotFound opcode performs the same operation on index btrees
91665** (with arbitrary multi-value keys).
91666**
91667** This opcode leaves the cursor in a state where it cannot be advanced
91668** in either direction. In other words, the Next and Prev opcodes will
91669** not work following this opcode.
91670**
91671** See also: Found, NotFound, NoConflict, SeekRowid
91672*/
91673/* Opcode: NotExists P1 P2 P3 * *
91674** Synopsis: intkey=r[P3]
91675**
91676** P1 is the index of a cursor open on an SQL table btree (with integer
91677** keys). P3 is an integer rowid. If P1 does not contain a record with
91678** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an
91679** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
91680** leave the cursor pointing at that record and fall through to the next
91681** instruction.
91682**
91683** The OP_SeekRowid opcode performs the same operation but also allows the
91684** P3 register to contain a non-integer value, in which case the jump is
91685** always taken. This opcode requires that P3 always contain an integer.
91686**
91687** The OP_NotFound opcode performs the same operation on index btrees
91688** (with arbitrary multi-value keys).
91689**
91690** This opcode leaves the cursor in a state where it cannot be advanced
91691** in either direction. In other words, the Next and Prev opcodes will
91692** not work following this opcode.
91693**
91694** See also: Found, NotFound, NoConflict, SeekRowid
91695*/
91696case OP_SeekRowid: { /* jump, in3 */
91697 VdbeCursor *pC;
91698 BtCursor *pCrsr;
91699 int res;
91700 u64 iKey;
91701
91702 pIn3 = &aMem[pOp->p3];
91703 testcase( pIn3->flags & MEM_Int );
91704 testcase( pIn3->flags & MEM_IntReal );
91705 testcase( pIn3->flags & MEM_Real );
91706 testcase( (pIn3->flags & (MEM_Str|MEM_Int))==MEM_Str );
91707 if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){
91708 /* If pIn3->u.i does not contain an integer, compute iKey as the
91709 ** integer value of pIn3. Jump to P2 if pIn3 cannot be converted
91710 ** into an integer without loss of information. Take care to avoid
91711 ** changing the datatype of pIn3, however, as it is used by other
91712 ** parts of the prepared statement. */
91713 Mem x = pIn3[0];
91714 applyAffinity(pRec: &x, SQLITE_AFF_NUMERIC, enc: encoding);
91715 if( (x.flags & MEM_Int)==0 ) goto jump_to_p2;
91716 iKey = x.u.i;
91717 goto notExistsWithKey;
91718 }
91719 /* Fall through into OP_NotExists */
91720 /* no break */ deliberate_fall_through
91721case OP_NotExists: /* jump, in3 */
91722 pIn3 = &aMem[pOp->p3];
91723 assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid );
91724 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91725 iKey = pIn3->u.i;
91726notExistsWithKey:
91727 pC = p->apCsr[pOp->p1];
91728 assert( pC!=0 );
91729#ifdef SQLITE_DEBUG
91730 if( pOp->opcode==OP_SeekRowid ) pC->seekOp = OP_SeekRowid;
91731#endif
91732 assert( pC->isTable );
91733 assert( pC->eCurType==CURTYPE_BTREE );
91734 pCrsr = pC->uc.pCursor;
91735 assert( pCrsr!=0 );
91736 res = 0;
91737 rc = sqlite3BtreeTableMoveto(pCur: pCrsr, intKey: iKey, biasRight: 0, pRes: &res);
91738 assert( rc==SQLITE_OK || res==0 );
91739 pC->movetoTarget = iKey; /* Used by OP_Delete */
91740 pC->nullRow = 0;
91741 pC->cacheStatus = CACHE_STALE;
91742 pC->deferredMoveto = 0;
91743 VdbeBranchTaken(res!=0,2);
91744 pC->seekResult = res;
91745 if( res!=0 ){
91746 assert( rc==SQLITE_OK );
91747 if( pOp->p2==0 ){
91748 rc = SQLITE_CORRUPT_BKPT;
91749 }else{
91750 goto jump_to_p2;
91751 }
91752 }
91753 if( rc ) goto abort_due_to_error;
91754 break;
91755}
91756
91757/* Opcode: Sequence P1 P2 * * *
91758** Synopsis: r[P2]=cursor[P1].ctr++
91759**
91760** Find the next available sequence number for cursor P1.
91761** Write the sequence number into register P2.
91762** The sequence number on the cursor is incremented after this
91763** instruction.
91764*/
91765case OP_Sequence: { /* out2 */
91766 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91767 assert( p->apCsr[pOp->p1]!=0 );
91768 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
91769 pOut = out2Prerelease(p, pOp);
91770 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
91771 break;
91772}
91773
91774
91775/* Opcode: NewRowid P1 P2 P3 * *
91776** Synopsis: r[P2]=rowid
91777**
91778** Get a new integer record number (a.k.a "rowid") used as the key to a table.
91779** The record number is not previously used as a key in the database
91780** table that cursor P1 points to. The new record number is written
91781** written to register P2.
91782**
91783** If P3>0 then P3 is a register in the root frame of this VDBE that holds
91784** the largest previously generated record number. No new record numbers are
91785** allowed to be less than this value. When this value reaches its maximum,
91786** an SQLITE_FULL error is generated. The P3 register is updated with the '
91787** generated record number. This P3 mechanism is used to help implement the
91788** AUTOINCREMENT feature.
91789*/
91790case OP_NewRowid: { /* out2 */
91791 i64 v; /* The new rowid */
91792 VdbeCursor *pC; /* Cursor of table to get the new rowid */
91793 int res; /* Result of an sqlite3BtreeLast() */
91794 int cnt; /* Counter to limit the number of searches */
91795#ifndef SQLITE_OMIT_AUTOINCREMENT
91796 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
91797 VdbeFrame *pFrame; /* Root frame of VDBE */
91798#endif
91799
91800 v = 0;
91801 res = 0;
91802 pOut = out2Prerelease(p, pOp);
91803 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91804 pC = p->apCsr[pOp->p1];
91805 assert( pC!=0 );
91806 assert( pC->isTable );
91807 assert( pC->eCurType==CURTYPE_BTREE );
91808 assert( pC->uc.pCursor!=0 );
91809 {
91810 /* The next rowid or record number (different terms for the same
91811 ** thing) is obtained in a two-step algorithm.
91812 **
91813 ** First we attempt to find the largest existing rowid and add one
91814 ** to that. But if the largest existing rowid is already the maximum
91815 ** positive integer, we have to fall through to the second
91816 ** probabilistic algorithm
91817 **
91818 ** The second algorithm is to select a rowid at random and see if
91819 ** it already exists in the table. If it does not exist, we have
91820 ** succeeded. If the random rowid does exist, we select a new one
91821 ** and try again, up to 100 times.
91822 */
91823 assert( pC->isTable );
91824
91825#ifdef SQLITE_32BIT_ROWID
91826# define MAX_ROWID 0x7fffffff
91827#else
91828 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
91829 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
91830 ** to provide the constant while making all compilers happy.
91831 */
91832# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
91833#endif
91834
91835 if( !pC->useRandomRowid ){
91836 rc = sqlite3BtreeLast(pCur: pC->uc.pCursor, pRes: &res);
91837 if( rc!=SQLITE_OK ){
91838 goto abort_due_to_error;
91839 }
91840 if( res ){
91841 v = 1; /* IMP: R-61914-48074 */
91842 }else{
91843 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
91844 v = sqlite3BtreeIntegerKey(pCur: pC->uc.pCursor);
91845 if( v>=MAX_ROWID ){
91846 pC->useRandomRowid = 1;
91847 }else{
91848 v++; /* IMP: R-29538-34987 */
91849 }
91850 }
91851 }
91852
91853#ifndef SQLITE_OMIT_AUTOINCREMENT
91854 if( pOp->p3 ){
91855 /* Assert that P3 is a valid memory cell. */
91856 assert( pOp->p3>0 );
91857 if( p->pFrame ){
91858 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
91859 /* Assert that P3 is a valid memory cell. */
91860 assert( pOp->p3<=pFrame->nMem );
91861 pMem = &pFrame->aMem[pOp->p3];
91862 }else{
91863 /* Assert that P3 is a valid memory cell. */
91864 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
91865 pMem = &aMem[pOp->p3];
91866 memAboutToChange(p, pMem);
91867 }
91868 assert( memIsValid(pMem) );
91869
91870 REGISTER_TRACE(pOp->p3, pMem);
91871 sqlite3VdbeMemIntegerify(pMem);
91872 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
91873 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
91874 rc = SQLITE_FULL; /* IMP: R-17817-00630 */
91875 goto abort_due_to_error;
91876 }
91877 if( v<pMem->u.i+1 ){
91878 v = pMem->u.i + 1;
91879 }
91880 pMem->u.i = v;
91881 }
91882#endif
91883 if( pC->useRandomRowid ){
91884 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
91885 ** largest possible integer (9223372036854775807) then the database
91886 ** engine starts picking positive candidate ROWIDs at random until
91887 ** it finds one that is not previously used. */
91888 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
91889 ** an AUTOINCREMENT table. */
91890 cnt = 0;
91891 do{
91892 sqlite3_randomness(N: sizeof(v), pBuf: &v);
91893 v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */
91894 }while( ((rc = sqlite3BtreeTableMoveto(pCur: pC->uc.pCursor, intKey: (u64)v,
91895 biasRight: 0, pRes: &res))==SQLITE_OK)
91896 && (res==0)
91897 && (++cnt<100));
91898 if( rc ) goto abort_due_to_error;
91899 if( res==0 ){
91900 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
91901 goto abort_due_to_error;
91902 }
91903 assert( v>0 ); /* EV: R-40812-03570 */
91904 }
91905 pC->deferredMoveto = 0;
91906 pC->cacheStatus = CACHE_STALE;
91907 }
91908 pOut->u.i = v;
91909 break;
91910}
91911
91912/* Opcode: Insert P1 P2 P3 P4 P5
91913** Synopsis: intkey=r[P3] data=r[P2]
91914**
91915** Write an entry into the table of cursor P1. A new entry is
91916** created if it doesn't already exist or the data for an existing
91917** entry is overwritten. The data is the value MEM_Blob stored in register
91918** number P2. The key is stored in register P3. The key must
91919** be a MEM_Int.
91920**
91921** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
91922** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
91923** then rowid is stored for subsequent return by the
91924** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
91925**
91926** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
91927** run faster by avoiding an unnecessary seek on cursor P1. However,
91928** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
91929** seeks on the cursor or if the most recent seek used a key equal to P3.
91930**
91931** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
91932** UPDATE operation. Otherwise (if the flag is clear) then this opcode
91933** is part of an INSERT operation. The difference is only important to
91934** the update hook.
91935**
91936** Parameter P4 may point to a Table structure, or may be NULL. If it is
91937** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
91938** following a successful insert.
91939**
91940** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
91941** allocated, then ownership of P2 is transferred to the pseudo-cursor
91942** and register P2 becomes ephemeral. If the cursor is changed, the
91943** value of register P2 will then change. Make sure this does not
91944** cause any problems.)
91945**
91946** This instruction only works on tables. The equivalent instruction
91947** for indices is OP_IdxInsert.
91948*/
91949case OP_Insert: {
91950 Mem *pData; /* MEM cell holding data for the record to be inserted */
91951 Mem *pKey; /* MEM cell holding key for the record */
91952 VdbeCursor *pC; /* Cursor to table into which insert is written */
91953 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
91954 const char *zDb; /* database name - used by the update hook */
91955 Table *pTab; /* Table structure - used by update and pre-update hooks */
91956 BtreePayload x; /* Payload to be inserted */
91957
91958 pData = &aMem[pOp->p2];
91959 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91960 assert( memIsValid(pData) );
91961 pC = p->apCsr[pOp->p1];
91962 assert( pC!=0 );
91963 assert( pC->eCurType==CURTYPE_BTREE );
91964 assert( pC->deferredMoveto==0 );
91965 assert( pC->uc.pCursor!=0 );
91966 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
91967 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
91968 REGISTER_TRACE(pOp->p2, pData);
91969 sqlite3VdbeIncrWriteCounter(p, pC);
91970
91971 pKey = &aMem[pOp->p3];
91972 assert( pKey->flags & MEM_Int );
91973 assert( memIsValid(pKey) );
91974 REGISTER_TRACE(pOp->p3, pKey);
91975 x.nKey = pKey->u.i;
91976
91977 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
91978 assert( pC->iDb>=0 );
91979 zDb = db->aDb[pC->iDb].zDbSName;
91980 pTab = pOp->p4.pTab;
91981 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
91982 }else{
91983 pTab = 0;
91984 zDb = 0;
91985 }
91986
91987#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
91988 /* Invoke the pre-update hook, if any */
91989 if( pTab ){
91990 if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){
91991 sqlite3VdbePreUpdateHook(p,pC,SQLITE_INSERT,zDb,pTab,x.nKey,pOp->p2,-1);
91992 }
91993 if( db->xUpdateCallback==0 || pTab->aCol==0 ){
91994 /* Prevent post-update hook from running in cases when it should not */
91995 pTab = 0;
91996 }
91997 }
91998 if( pOp->p5 & OPFLAG_ISNOOP ) break;
91999#endif
92000
92001 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
92002 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
92003 assert( (pData->flags & (MEM_Blob|MEM_Str))!=0 || pData->n==0 );
92004 x.pData = pData->z;
92005 x.nData = pData->n;
92006 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
92007 if( pData->flags & MEM_Zero ){
92008 x.nZero = pData->u.nZero;
92009 }else{
92010 x.nZero = 0;
92011 }
92012 x.pKey = 0;
92013 rc = sqlite3BtreeInsert(pCur: pC->uc.pCursor, pX: &x,
92014 flags: (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
92015 seekResult
92016 );
92017 pC->deferredMoveto = 0;
92018 pC->cacheStatus = CACHE_STALE;
92019
92020 /* Invoke the update-hook if required. */
92021 if( rc ) goto abort_due_to_error;
92022 if( pTab ){
92023 assert( db->xUpdateCallback!=0 );
92024 assert( pTab->aCol!=0 );
92025 db->xUpdateCallback(db->pUpdateArg,
92026 (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT,
92027 zDb, pTab->zName, x.nKey);
92028 }
92029 break;
92030}
92031
92032/* Opcode: RowCell P1 P2 P3 * *
92033**
92034** P1 and P2 are both open cursors. Both must be opened on the same type
92035** of table - intkey or index. This opcode is used as part of copying
92036** the current row from P2 into P1. If the cursors are opened on intkey
92037** tables, register P3 contains the rowid to use with the new record in
92038** P1. If they are opened on index tables, P3 is not used.
92039**
92040** This opcode must be followed by either an Insert or InsertIdx opcode
92041** with the OPFLAG_PREFORMAT flag set to complete the insert operation.
92042*/
92043case OP_RowCell: {
92044 VdbeCursor *pDest; /* Cursor to write to */
92045 VdbeCursor *pSrc; /* Cursor to read from */
92046 i64 iKey; /* Rowid value to insert with */
92047 assert( pOp[1].opcode==OP_Insert || pOp[1].opcode==OP_IdxInsert );
92048 assert( pOp[1].opcode==OP_Insert || pOp->p3==0 );
92049 assert( pOp[1].opcode==OP_IdxInsert || pOp->p3>0 );
92050 assert( pOp[1].p5 & OPFLAG_PREFORMAT );
92051 pDest = p->apCsr[pOp->p1];
92052 pSrc = p->apCsr[pOp->p2];
92053 iKey = pOp->p3 ? aMem[pOp->p3].u.i : 0;
92054 rc = sqlite3BtreeTransferRow(pDest: pDest->uc.pCursor, pSrc: pSrc->uc.pCursor, iKey);
92055 if( rc!=SQLITE_OK ) goto abort_due_to_error;
92056 break;
92057};
92058
92059/* Opcode: Delete P1 P2 P3 P4 P5
92060**
92061** Delete the record at which the P1 cursor is currently pointing.
92062**
92063** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then
92064** the cursor will be left pointing at either the next or the previous
92065** record in the table. If it is left pointing at the next record, then
92066** the next Next instruction will be a no-op. As a result, in this case
92067** it is ok to delete a record from within a Next loop. If
92068** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be
92069** left in an undefined state.
92070**
92071** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this
92072** delete one of several associated with deleting a table row and all its
92073** associated index entries. Exactly one of those deletes is the "primary"
92074** delete. The others are all on OPFLAG_FORDELETE cursors or else are
92075** marked with the AUXDELETE flag.
92076**
92077** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row
92078** change count is incremented (otherwise not).
92079**
92080** P1 must not be pseudo-table. It has to be a real table with
92081** multiple rows.
92082**
92083** If P4 is not NULL then it points to a Table object. In this case either
92084** the update or pre-update hook, or both, may be invoked. The P1 cursor must
92085** have been positioned using OP_NotFound prior to invoking this opcode in
92086** this case. Specifically, if one is configured, the pre-update hook is
92087** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
92088** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2.
92089**
92090** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address
92091** of the memory cell that contains the value that the rowid of the row will
92092** be set to by the update.
92093*/
92094case OP_Delete: {
92095 VdbeCursor *pC;
92096 const char *zDb;
92097 Table *pTab;
92098 int opflags;
92099
92100 opflags = pOp->p2;
92101 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
92102 pC = p->apCsr[pOp->p1];
92103 assert( pC!=0 );
92104 assert( pC->eCurType==CURTYPE_BTREE );
92105 assert( pC->uc.pCursor!=0 );
92106 assert( pC->deferredMoveto==0 );
92107 sqlite3VdbeIncrWriteCounter(p, pC);
92108
92109#ifdef SQLITE_DEBUG
92110 if( pOp->p4type==P4_TABLE
92111 && HasRowid(pOp->p4.pTab)
92112 && pOp->p5==0
92113 && sqlite3BtreeCursorIsValidNN(pC->uc.pCursor)
92114 ){
92115 /* If p5 is zero, the seek operation that positioned the cursor prior to
92116 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
92117 ** the row that is being deleted */
92118 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
92119 assert( CORRUPT_DB || pC->movetoTarget==iKey );
92120 }
92121#endif
92122
92123 /* If the update-hook or pre-update-hook will be invoked, set zDb to
92124 ** the name of the db to pass as to it. Also set local pTab to a copy
92125 ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was
92126 ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set
92127 ** VdbeCursor.movetoTarget to the current rowid. */
92128 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
92129 assert( pC->iDb>=0 );
92130 assert( pOp->p4.pTab!=0 );
92131 zDb = db->aDb[pC->iDb].zDbSName;
92132 pTab = pOp->p4.pTab;
92133 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
92134 pC->movetoTarget = sqlite3BtreeIntegerKey(pCur: pC->uc.pCursor);
92135 }
92136 }else{
92137 zDb = 0;
92138 pTab = 0;
92139 }
92140
92141#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
92142 /* Invoke the pre-update-hook if required. */
92143 assert( db->xPreUpdateCallback==0 || pTab==pOp->p4.pTab );
92144 if( db->xPreUpdateCallback && pTab ){
92145 assert( !(opflags & OPFLAG_ISUPDATE)
92146 || HasRowid(pTab)==0
92147 || (aMem[pOp->p3].flags & MEM_Int)
92148 );
92149 sqlite3VdbePreUpdateHook(p, pC,
92150 (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE,
92151 zDb, pTab, pC->movetoTarget,
92152 pOp->p3, -1
92153 );
92154 }
92155 if( opflags & OPFLAG_ISNOOP ) break;
92156#endif
92157
92158 /* Only flags that can be set are SAVEPOISTION and AUXDELETE */
92159 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
92160 assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION );
92161 assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE );
92162
92163#ifdef SQLITE_DEBUG
92164 if( p->pFrame==0 ){
92165 if( pC->isEphemeral==0
92166 && (pOp->p5 & OPFLAG_AUXDELETE)==0
92167 && (pC->wrFlag & OPFLAG_FORDELETE)==0
92168 ){
92169 nExtraDelete++;
92170 }
92171 if( pOp->p2 & OPFLAG_NCHANGE ){
92172 nExtraDelete--;
92173 }
92174 }
92175#endif
92176
92177 rc = sqlite3BtreeDelete(pCur: pC->uc.pCursor, flags: pOp->p5);
92178 pC->cacheStatus = CACHE_STALE;
92179 pC->seekResult = 0;
92180 if( rc ) goto abort_due_to_error;
92181
92182 /* Invoke the update-hook if required. */
92183 if( opflags & OPFLAG_NCHANGE ){
92184 p->nChange++;
92185 if( db->xUpdateCallback && ALWAYS(pTab!=0) && HasRowid(pTab) ){
92186 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
92187 pC->movetoTarget);
92188 assert( pC->iDb>=0 );
92189 }
92190 }
92191
92192 break;
92193}
92194/* Opcode: ResetCount * * * * *
92195**
92196** The value of the change counter is copied to the database handle
92197** change counter (returned by subsequent calls to sqlite3_changes()).
92198** Then the VMs internal change counter resets to 0.
92199** This is used by trigger programs.
92200*/
92201case OP_ResetCount: {
92202 sqlite3VdbeSetChanges(db, nChange: p->nChange);
92203 p->nChange = 0;
92204 break;
92205}
92206
92207/* Opcode: SorterCompare P1 P2 P3 P4
92208** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
92209**
92210** P1 is a sorter cursor. This instruction compares a prefix of the
92211** record blob in register P3 against a prefix of the entry that
92212** the sorter cursor currently points to. Only the first P4 fields
92213** of r[P3] and the sorter record are compared.
92214**
92215** If either P3 or the sorter contains a NULL in one of their significant
92216** fields (not counting the P4 fields at the end which are ignored) then
92217** the comparison is assumed to be equal.
92218**
92219** Fall through to next instruction if the two records compare equal to
92220** each other. Jump to P2 if they are different.
92221*/
92222case OP_SorterCompare: {
92223 VdbeCursor *pC;
92224 int res;
92225 int nKeyCol;
92226
92227 pC = p->apCsr[pOp->p1];
92228 assert( isSorter(pC) );
92229 assert( pOp->p4type==P4_INT32 );
92230 pIn3 = &aMem[pOp->p3];
92231 nKeyCol = pOp->p4.i;
92232 res = 0;
92233 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
92234 VdbeBranchTaken(res!=0,2);
92235 if( rc ) goto abort_due_to_error;
92236 if( res ) goto jump_to_p2;
92237 break;
92238};
92239
92240/* Opcode: SorterData P1 P2 P3 * *
92241** Synopsis: r[P2]=data
92242**
92243** Write into register P2 the current sorter data for sorter cursor P1.
92244** Then clear the column header cache on cursor P3.
92245**
92246** This opcode is normally use to move a record out of the sorter and into
92247** a register that is the source for a pseudo-table cursor created using
92248** OpenPseudo. That pseudo-table cursor is the one that is identified by
92249** parameter P3. Clearing the P3 column cache as part of this opcode saves
92250** us from having to issue a separate NullRow instruction to clear that cache.
92251*/
92252case OP_SorterData: {
92253 VdbeCursor *pC;
92254
92255 pOut = &aMem[pOp->p2];
92256 pC = p->apCsr[pOp->p1];
92257 assert( isSorter(pC) );
92258 rc = sqlite3VdbeSorterRowkey(pC, pOut);
92259 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
92260 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
92261 if( rc ) goto abort_due_to_error;
92262 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
92263 break;
92264}
92265
92266/* Opcode: RowData P1 P2 P3 * *
92267** Synopsis: r[P2]=data
92268**
92269** Write into register P2 the complete row content for the row at
92270** which cursor P1 is currently pointing.
92271** There is no interpretation of the data.
92272** It is just copied onto the P2 register exactly as
92273** it is found in the database file.
92274**
92275** If cursor P1 is an index, then the content is the key of the row.
92276** If cursor P2 is a table, then the content extracted is the data.
92277**
92278** If the P1 cursor must be pointing to a valid row (not a NULL row)
92279** of a real table, not a pseudo-table.
92280**
92281** If P3!=0 then this opcode is allowed to make an ephemeral pointer
92282** into the database page. That means that the content of the output
92283** register will be invalidated as soon as the cursor moves - including
92284** moves caused by other cursors that "save" the current cursors
92285** position in order that they can write to the same table. If P3==0
92286** then a copy of the data is made into memory. P3!=0 is faster, but
92287** P3==0 is safer.
92288**
92289** If P3!=0 then the content of the P2 register is unsuitable for use
92290** in OP_Result and any OP_Result will invalidate the P2 register content.
92291** The P2 register content is invalidated by opcodes like OP_Function or
92292** by any use of another cursor pointing to the same table.
92293*/
92294case OP_RowData: {
92295 VdbeCursor *pC;
92296 BtCursor *pCrsr;
92297 u32 n;
92298
92299 pOut = out2Prerelease(p, pOp);
92300
92301 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
92302 pC = p->apCsr[pOp->p1];
92303 assert( pC!=0 );
92304 assert( pC->eCurType==CURTYPE_BTREE );
92305 assert( isSorter(pC)==0 );
92306 assert( pC->nullRow==0 );
92307 assert( pC->uc.pCursor!=0 );
92308 pCrsr = pC->uc.pCursor;
92309
92310 /* The OP_RowData opcodes always follow OP_NotExists or
92311 ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions
92312 ** that might invalidate the cursor.
92313 ** If this where not the case, on of the following assert()s
92314 ** would fail. Should this ever change (because of changes in the code
92315 ** generator) then the fix would be to insert a call to
92316 ** sqlite3VdbeCursorMoveto().
92317 */
92318 assert( pC->deferredMoveto==0 );
92319 assert( sqlite3BtreeCursorIsValid(pCrsr) );
92320
92321 n = sqlite3BtreePayloadSize(pCur: pCrsr);
92322 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
92323 goto too_big;
92324 }
92325 testcase( n==0 );
92326 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur: pCrsr, amt: n, pMem: pOut);
92327 if( rc ) goto abort_due_to_error;
92328 if( !pOp->p3 ) Deephemeralize(pOut);
92329 UPDATE_MAX_BLOBSIZE(pOut);
92330 REGISTER_TRACE(pOp->p2, pOut);
92331 break;
92332}
92333
92334/* Opcode: Rowid P1 P2 * * *
92335** Synopsis: r[P2]=rowid
92336**
92337** Store in register P2 an integer which is the key of the table entry that
92338** P1 is currently point to.
92339**
92340** P1 can be either an ordinary table or a virtual table. There used to
92341** be a separate OP_VRowid opcode for use with virtual tables, but this
92342** one opcode now works for both table types.
92343*/
92344case OP_Rowid: { /* out2 */
92345 VdbeCursor *pC;
92346 i64 v;
92347 sqlite3_vtab *pVtab;
92348 const sqlite3_module *pModule;
92349
92350 pOut = out2Prerelease(p, pOp);
92351 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
92352 pC = p->apCsr[pOp->p1];
92353 assert( pC!=0 );
92354 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
92355 if( pC->nullRow ){
92356 pOut->flags = MEM_Null;
92357 break;
92358 }else if( pC->deferredMoveto ){
92359 v = pC->movetoTarget;
92360#ifndef SQLITE_OMIT_VIRTUALTABLE
92361 }else if( pC->eCurType==CURTYPE_VTAB ){
92362 assert( pC->uc.pVCur!=0 );
92363 pVtab = pC->uc.pVCur->pVtab;
92364 pModule = pVtab->pModule;
92365 assert( pModule->xRowid );
92366 rc = pModule->xRowid(pC->uc.pVCur, &v);
92367 sqlite3VtabImportErrmsg(p, pVtab);
92368 if( rc ) goto abort_due_to_error;
92369#endif /* SQLITE_OMIT_VIRTUALTABLE */
92370 }else{
92371 assert( pC->eCurType==CURTYPE_BTREE );
92372 assert( pC->uc.pCursor!=0 );
92373 rc = sqlite3VdbeCursorRestore(p: pC);
92374 if( rc ) goto abort_due_to_error;
92375 if( pC->nullRow ){
92376 pOut->flags = MEM_Null;
92377 break;
92378 }
92379 v = sqlite3BtreeIntegerKey(pCur: pC->uc.pCursor);
92380 }
92381 pOut->u.i = v;
92382 break;
92383}
92384
92385/* Opcode: NullRow P1 * * * *
92386**
92387** Move the cursor P1 to a null row. Any OP_Column operations
92388** that occur while the cursor is on the null row will always
92389** write a NULL.
92390*/
92391case OP_NullRow: {
92392 VdbeCursor *pC;
92393
92394 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
92395 pC = p->apCsr[pOp->p1];
92396 assert( pC!=0 );
92397 pC->nullRow = 1;
92398 pC->cacheStatus = CACHE_STALE;
92399 if( pC->eCurType==CURTYPE_BTREE ){
92400 assert( pC->uc.pCursor!=0 );
92401 sqlite3BtreeClearCursor(pCur: pC->uc.pCursor);
92402 }
92403#ifdef SQLITE_DEBUG
92404 if( pC->seekOp==0 ) pC->seekOp = OP_NullRow;
92405#endif
92406 break;
92407}
92408
92409/* Opcode: SeekEnd P1 * * * *
92410**
92411** Position cursor P1 at the end of the btree for the purpose of
92412** appending a new entry onto the btree.
92413**
92414** It is assumed that the cursor is used only for appending and so
92415** if the cursor is valid, then the cursor must already be pointing
92416** at the end of the btree and so no changes are made to
92417** the cursor.
92418*/
92419/* Opcode: Last P1 P2 * * *
92420**
92421** The next use of the Rowid or Column or Prev instruction for P1
92422** will refer to the last entry in the database table or index.
92423** If the table or index is empty and P2>0, then jump immediately to P2.
92424** If P2 is 0 or if the table or index is not empty, fall through
92425** to the following instruction.
92426**
92427** This opcode leaves the cursor configured to move in reverse order,
92428** from the end toward the beginning. In other words, the cursor is
92429** configured to use Prev, not Next.
92430*/
92431case OP_SeekEnd:
92432case OP_Last: { /* jump */
92433 VdbeCursor *pC;
92434 BtCursor *pCrsr;
92435 int res;
92436
92437 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
92438 pC = p->apCsr[pOp->p1];
92439 assert( pC!=0 );
92440 assert( pC->eCurType==CURTYPE_BTREE );
92441 pCrsr = pC->uc.pCursor;
92442 res = 0;
92443 assert( pCrsr!=0 );
92444#ifdef SQLITE_DEBUG
92445 pC->seekOp = pOp->opcode;
92446#endif
92447 if( pOp->opcode==OP_SeekEnd ){
92448 assert( pOp->p2==0 );
92449 pC->seekResult = -1;
92450 if( sqlite3BtreeCursorIsValidNN(pCur: pCrsr) ){
92451 break;
92452 }
92453 }
92454 rc = sqlite3BtreeLast(pCur: pCrsr, pRes: &res);
92455 pC->nullRow = (u8)res;
92456 pC->deferredMoveto = 0;
92457 pC->cacheStatus = CACHE_STALE;
92458 if( rc ) goto abort_due_to_error;
92459 if( pOp->p2>0 ){
92460 VdbeBranchTaken(res!=0,2);
92461 if( res ) goto jump_to_p2;
92462 }
92463 break;
92464}
92465
92466/* Opcode: IfSmaller P1 P2 P3 * *
92467**
92468** Estimate the number of rows in the table P1. Jump to P2 if that
92469** estimate is less than approximately 2**(0.1*P3).
92470*/
92471case OP_IfSmaller: { /* jump */
92472 VdbeCursor *pC;
92473 BtCursor *pCrsr;
92474 int res;
92475 i64 sz;
92476
92477 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
92478 pC = p->apCsr[pOp->p1];
92479 assert( pC!=0 );
92480 pCrsr = pC->uc.pCursor;
92481 assert( pCrsr );
92482 rc = sqlite3BtreeFirst(pCur: pCrsr, pRes: &res);
92483 if( rc ) goto abort_due_to_error;
92484 if( res==0 ){
92485 sz = sqlite3BtreeRowCountEst(pCur: pCrsr);
92486 if( ALWAYS(sz>=0) && sqlite3LogEst(x: (u64)sz)<pOp->p3 ) res = 1;
92487 }
92488 VdbeBranchTaken(res!=0,2);
92489 if( res ) goto jump_to_p2;
92490 break;
92491}
92492
92493
92494/* Opcode: SorterSort P1 P2 * * *
92495**
92496** After all records have been inserted into the Sorter object
92497** identified by P1, invoke this opcode to actually do the sorting.
92498** Jump to P2 if there are no records to be sorted.
92499**
92500** This opcode is an alias for OP_Sort and OP_Rewind that is used
92501** for Sorter objects.
92502*/
92503/* Opcode: Sort P1 P2 * * *
92504**
92505** This opcode does exactly the same thing as OP_Rewind except that
92506** it increments an undocumented global variable used for testing.
92507**
92508** Sorting is accomplished by writing records into a sorting index,
92509** then rewinding that index and playing it back from beginning to
92510** end. We use the OP_Sort opcode instead of OP_Rewind to do the
92511** rewinding so that the global variable will be incremented and
92512** regression tests can determine whether or not the optimizer is
92513** correctly optimizing out sorts.
92514*/
92515case OP_SorterSort: /* jump */
92516case OP_Sort: { /* jump */
92517#ifdef SQLITE_TEST
92518 sqlite3_sort_count++;
92519 sqlite3_search_count--;
92520#endif
92521 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
92522 /* Fall through into OP_Rewind */
92523 /* no break */ deliberate_fall_through
92524}
92525/* Opcode: Rewind P1 P2 * * *
92526**
92527** The next use of the Rowid or Column or Next instruction for P1
92528** will refer to the first entry in the database table or index.
92529** If the table or index is empty, jump immediately to P2.
92530** If the table or index is not empty, fall through to the following
92531** instruction.
92532**
92533** This opcode leaves the cursor configured to move in forward order,
92534** from the beginning toward the end. In other words, the cursor is
92535** configured to use Next, not Prev.
92536*/
92537case OP_Rewind: { /* jump */
92538 VdbeCursor *pC;
92539 BtCursor *pCrsr;
92540 int res;
92541
92542 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
92543 assert( pOp->p5==0 );
92544 pC = p->apCsr[pOp->p1];
92545 assert( pC!=0 );
92546 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
92547 res = 1;
92548#ifdef SQLITE_DEBUG
92549 pC->seekOp = OP_Rewind;
92550#endif
92551 if( isSorter(pC) ){
92552 rc = sqlite3VdbeSorterRewind(pC, &res);
92553 }else{
92554 assert( pC->eCurType==CURTYPE_BTREE );
92555 pCrsr = pC->uc.pCursor;
92556 assert( pCrsr );
92557 rc = sqlite3BtreeFirst(pCur: pCrsr, pRes: &res);
92558 pC->deferredMoveto = 0;
92559 pC->cacheStatus = CACHE_STALE;
92560 }
92561 if( rc ) goto abort_due_to_error;
92562 pC->nullRow = (u8)res;
92563 assert( pOp->p2>0 && pOp->p2<p->nOp );
92564 VdbeBranchTaken(res!=0,2);
92565 if( res ) goto jump_to_p2;
92566 break;
92567}
92568
92569/* Opcode: Next P1 P2 P3 P4 P5
92570**
92571** Advance cursor P1 so that it points to the next key/data pair in its
92572** table or index. If there are no more key/value pairs then fall through
92573** to the following instruction. But if the cursor advance was successful,
92574** jump immediately to P2.
92575**
92576** The Next opcode is only valid following an SeekGT, SeekGE, or
92577** OP_Rewind opcode used to position the cursor. Next is not allowed
92578** to follow SeekLT, SeekLE, or OP_Last.
92579**
92580** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
92581** been opened prior to this opcode or the program will segfault.
92582**
92583** The P3 value is a hint to the btree implementation. If P3==1, that
92584** means P1 is an SQL index and that this instruction could have been
92585** omitted if that index had been unique. P3 is usually 0. P3 is
92586** always either 0 or 1.
92587**
92588** P4 is always of type P4_ADVANCE. The function pointer points to
92589** sqlite3BtreeNext().
92590**
92591** If P5 is positive and the jump is taken, then event counter
92592** number P5-1 in the prepared statement is incremented.
92593**
92594** See also: Prev
92595*/
92596/* Opcode: Prev P1 P2 P3 P4 P5
92597**
92598** Back up cursor P1 so that it points to the previous key/data pair in its
92599** table or index. If there is no previous key/value pairs then fall through
92600** to the following instruction. But if the cursor backup was successful,
92601** jump immediately to P2.
92602**
92603**
92604** The Prev opcode is only valid following an SeekLT, SeekLE, or
92605** OP_Last opcode used to position the cursor. Prev is not allowed
92606** to follow SeekGT, SeekGE, or OP_Rewind.
92607**
92608** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
92609** not open then the behavior is undefined.
92610**
92611** The P3 value is a hint to the btree implementation. If P3==1, that
92612** means P1 is an SQL index and that this instruction could have been
92613** omitted if that index had been unique. P3 is usually 0. P3 is
92614** always either 0 or 1.
92615**
92616** P4 is always of type P4_ADVANCE. The function pointer points to
92617** sqlite3BtreePrevious().
92618**
92619** If P5 is positive and the jump is taken, then event counter
92620** number P5-1 in the prepared statement is incremented.
92621*/
92622/* Opcode: SorterNext P1 P2 * * P5
92623**
92624** This opcode works just like OP_Next except that P1 must be a
92625** sorter object for which the OP_SorterSort opcode has been
92626** invoked. This opcode advances the cursor to the next sorted
92627** record, or jumps to P2 if there are no more sorted records.
92628*/
92629case OP_SorterNext: { /* jump */
92630 VdbeCursor *pC;
92631
92632 pC = p->apCsr[pOp->p1];
92633 assert( isSorter(pC) );
92634 rc = sqlite3VdbeSorterNext(db, pC);
92635 goto next_tail;
92636case OP_Prev: /* jump */
92637case OP_Next: /* jump */
92638 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
92639 assert( pOp->p5<ArraySize(p->aCounter) );
92640 pC = p->apCsr[pOp->p1];
92641 assert( pC!=0 );
92642 assert( pC->deferredMoveto==0 );
92643 assert( pC->eCurType==CURTYPE_BTREE );
92644 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
92645 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
92646
92647 /* The Next opcode is only used after SeekGT, SeekGE, Rewind, and Found.
92648 ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */
92649 assert( pOp->opcode!=OP_Next
92650 || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
92651 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found
92652 || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid
92653 || pC->seekOp==OP_IfNoHope);
92654 assert( pOp->opcode!=OP_Prev
92655 || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
92656 || pC->seekOp==OP_Last || pC->seekOp==OP_IfNoHope
92657 || pC->seekOp==OP_NullRow);
92658
92659 rc = pOp->p4.xAdvance(pC->uc.pCursor, pOp->p3);
92660next_tail:
92661 pC->cacheStatus = CACHE_STALE;
92662 VdbeBranchTaken(rc==SQLITE_OK,2);
92663 if( rc==SQLITE_OK ){
92664 pC->nullRow = 0;
92665 p->aCounter[pOp->p5]++;
92666#ifdef SQLITE_TEST
92667 sqlite3_search_count++;
92668#endif
92669 goto jump_to_p2_and_check_for_interrupt;
92670 }
92671 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
92672 rc = SQLITE_OK;
92673 pC->nullRow = 1;
92674 goto check_for_interrupt;
92675}
92676
92677/* Opcode: IdxInsert P1 P2 P3 P4 P5
92678** Synopsis: key=r[P2]
92679**
92680** Register P2 holds an SQL index key made using the
92681** MakeRecord instructions. This opcode writes that key
92682** into the index P1. Data for the entry is nil.
92683**
92684** If P4 is not zero, then it is the number of values in the unpacked
92685** key of reg(P2). In that case, P3 is the index of the first register
92686** for the unpacked key. The availability of the unpacked key can sometimes
92687** be an optimization.
92688**
92689** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
92690** that this insert is likely to be an append.
92691**
92692** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
92693** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear,
92694** then the change counter is unchanged.
92695**
92696** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
92697** run faster by avoiding an unnecessary seek on cursor P1. However,
92698** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
92699** seeks on the cursor or if the most recent seek used a key equivalent
92700** to P2.
92701**
92702** This instruction only works for indices. The equivalent instruction
92703** for tables is OP_Insert.
92704*/
92705case OP_IdxInsert: { /* in2 */
92706 VdbeCursor *pC;
92707 BtreePayload x;
92708
92709 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
92710 pC = p->apCsr[pOp->p1];
92711 sqlite3VdbeIncrWriteCounter(p, pC);
92712 assert( pC!=0 );
92713 assert( !isSorter(pC) );
92714 pIn2 = &aMem[pOp->p2];
92715 assert( (pIn2->flags & MEM_Blob) || (pOp->p5 & OPFLAG_PREFORMAT) );
92716 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
92717 assert( pC->eCurType==CURTYPE_BTREE );
92718 assert( pC->isTable==0 );
92719 rc = ExpandBlob(pIn2);
92720 if( rc ) goto abort_due_to_error;
92721 x.nKey = pIn2->n;
92722 x.pKey = pIn2->z;
92723 x.aMem = aMem + pOp->p3;
92724 x.nMem = (u16)pOp->p4.i;
92725 rc = sqlite3BtreeInsert(pCur: pC->uc.pCursor, pX: &x,
92726 flags: (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
92727 seekResult: ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
92728 );
92729 assert( pC->deferredMoveto==0 );
92730 pC->cacheStatus = CACHE_STALE;
92731 if( rc) goto abort_due_to_error;
92732 break;
92733}
92734
92735/* Opcode: SorterInsert P1 P2 * * *
92736** Synopsis: key=r[P2]
92737**
92738** Register P2 holds an SQL index key made using the
92739** MakeRecord instructions. This opcode writes that key
92740** into the sorter P1. Data for the entry is nil.
92741*/
92742case OP_SorterInsert: { /* in2 */
92743 VdbeCursor *pC;
92744
92745 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
92746 pC = p->apCsr[pOp->p1];
92747 sqlite3VdbeIncrWriteCounter(p, pC);
92748 assert( pC!=0 );
92749 assert( isSorter(pC) );
92750 pIn2 = &aMem[pOp->p2];
92751 assert( pIn2->flags & MEM_Blob );
92752 assert( pC->isTable==0 );
92753 rc = ExpandBlob(pIn2);
92754 if( rc ) goto abort_due_to_error;
92755 rc = sqlite3VdbeSorterWrite(pC, pIn2);
92756 if( rc) goto abort_due_to_error;
92757 break;
92758}
92759
92760/* Opcode: IdxDelete P1 P2 P3 * P5
92761** Synopsis: key=r[P2@P3]
92762**
92763** The content of P3 registers starting at register P2 form
92764** an unpacked index key. This opcode removes that entry from the
92765** index opened by cursor P1.
92766**
92767** If P5 is not zero, then raise an SQLITE_CORRUPT_INDEX error
92768** if no matching index entry is found. This happens when running
92769** an UPDATE or DELETE statement and the index entry to be updated
92770** or deleted is not found. For some uses of IdxDelete
92771** (example: the EXCEPT operator) it does not matter that no matching
92772** entry is found. For those cases, P5 is zero. Also, do not raise
92773** this (self-correcting and non-critical) error if in writable_schema mode.
92774*/
92775case OP_IdxDelete: {
92776 VdbeCursor *pC;
92777 BtCursor *pCrsr;
92778 int res;
92779 UnpackedRecord r;
92780
92781 assert( pOp->p3>0 );
92782 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
92783 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
92784 pC = p->apCsr[pOp->p1];
92785 assert( pC!=0 );
92786 assert( pC->eCurType==CURTYPE_BTREE );
92787 sqlite3VdbeIncrWriteCounter(p, pC);
92788 pCrsr = pC->uc.pCursor;
92789 assert( pCrsr!=0 );
92790 r.pKeyInfo = pC->pKeyInfo;
92791 r.nField = (u16)pOp->p3;
92792 r.default_rc = 0;
92793 r.aMem = &aMem[pOp->p2];
92794 rc = sqlite3BtreeIndexMoveto(pCur: pCrsr, pIdxKey: &r, pRes: &res);
92795 if( rc ) goto abort_due_to_error;
92796 if( res==0 ){
92797 rc = sqlite3BtreeDelete(pCur: pCrsr, BTREE_AUXDELETE);
92798 if( rc ) goto abort_due_to_error;
92799 }else if( pOp->p5 && !sqlite3WritableSchema(db) ){
92800 rc = sqlite3ReportError(SQLITE_CORRUPT_INDEX, __LINE__, zType: "index corruption");
92801 goto abort_due_to_error;
92802 }
92803 assert( pC->deferredMoveto==0 );
92804 pC->cacheStatus = CACHE_STALE;
92805 pC->seekResult = 0;
92806 break;
92807}
92808
92809/* Opcode: DeferredSeek P1 * P3 P4 *
92810** Synopsis: Move P3 to P1.rowid if needed
92811**
92812** P1 is an open index cursor and P3 is a cursor on the corresponding
92813** table. This opcode does a deferred seek of the P3 table cursor
92814** to the row that corresponds to the current row of P1.
92815**
92816** This is a deferred seek. Nothing actually happens until
92817** the cursor is used to read a record. That way, if no reads
92818** occur, no unnecessary I/O happens.
92819**
92820** P4 may be an array of integers (type P4_INTARRAY) containing
92821** one entry for each column in the P3 table. If array entry a(i)
92822** is non-zero, then reading column a(i)-1 from cursor P3 is
92823** equivalent to performing the deferred seek and then reading column i
92824** from P1. This information is stored in P3 and used to redirect
92825** reads against P3 over to P1, thus possibly avoiding the need to
92826** seek and read cursor P3.
92827*/
92828/* Opcode: IdxRowid P1 P2 * * *
92829** Synopsis: r[P2]=rowid
92830**
92831** Write into register P2 an integer which is the last entry in the record at
92832** the end of the index key pointed to by cursor P1. This integer should be
92833** the rowid of the table entry to which this index entry points.
92834**
92835** See also: Rowid, MakeRecord.
92836*/
92837case OP_DeferredSeek:
92838case OP_IdxRowid: { /* out2 */
92839 VdbeCursor *pC; /* The P1 index cursor */
92840 VdbeCursor *pTabCur; /* The P2 table cursor (OP_DeferredSeek only) */
92841 i64 rowid; /* Rowid that P1 current points to */
92842
92843 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
92844 pC = p->apCsr[pOp->p1];
92845 assert( pC!=0 );
92846 assert( pC->eCurType==CURTYPE_BTREE );
92847 assert( pC->uc.pCursor!=0 );
92848 assert( pC->isTable==0 );
92849 assert( pC->deferredMoveto==0 );
92850 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
92851
92852 /* The IdxRowid and Seek opcodes are combined because of the commonality
92853 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
92854 rc = sqlite3VdbeCursorRestore(p: pC);
92855
92856 /* sqlite3VbeCursorRestore() can only fail if the record has been deleted
92857 ** out from under the cursor. That will never happens for an IdxRowid
92858 ** or Seek opcode */
92859 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
92860
92861 if( !pC->nullRow ){
92862 rowid = 0; /* Not needed. Only used to silence a warning. */
92863 rc = sqlite3VdbeIdxRowid(db, pCur: pC->uc.pCursor, rowid: &rowid);
92864 if( rc!=SQLITE_OK ){
92865 goto abort_due_to_error;
92866 }
92867 if( pOp->opcode==OP_DeferredSeek ){
92868 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
92869 pTabCur = p->apCsr[pOp->p3];
92870 assert( pTabCur!=0 );
92871 assert( pTabCur->eCurType==CURTYPE_BTREE );
92872 assert( pTabCur->uc.pCursor!=0 );
92873 assert( pTabCur->isTable );
92874 pTabCur->nullRow = 0;
92875 pTabCur->movetoTarget = rowid;
92876 pTabCur->deferredMoveto = 1;
92877 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
92878 pTabCur->aAltMap = pOp->p4.ai;
92879 assert( !pC->isEphemeral );
92880 assert( !pTabCur->isEphemeral );
92881 pTabCur->pAltCursor = pC;
92882 }else{
92883 pOut = out2Prerelease(p, pOp);
92884 pOut->u.i = rowid;
92885 }
92886 }else{
92887 assert( pOp->opcode==OP_IdxRowid );
92888 sqlite3VdbeMemSetNull(pMem: &aMem[pOp->p2]);
92889 }
92890 break;
92891}
92892
92893/* Opcode: FinishSeek P1 * * * *
92894**
92895** If cursor P1 was previously moved via OP_DeferredSeek, complete that
92896** seek operation now, without further delay. If the cursor seek has
92897** already occurred, this instruction is a no-op.
92898*/
92899case OP_FinishSeek: {
92900 VdbeCursor *pC; /* The P1 index cursor */
92901
92902 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
92903 pC = p->apCsr[pOp->p1];
92904 if( pC->deferredMoveto ){
92905 rc = sqlite3VdbeFinishMoveto(p: pC);
92906 if( rc ) goto abort_due_to_error;
92907 }
92908 break;
92909}
92910
92911/* Opcode: IdxGE P1 P2 P3 P4 *
92912** Synopsis: key=r[P3@P4]
92913**
92914** The P4 register values beginning with P3 form an unpacked index
92915** key that omits the PRIMARY KEY. Compare this key value against the index
92916** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
92917** fields at the end.
92918**
92919** If the P1 index entry is greater than or equal to the key value
92920** then jump to P2. Otherwise fall through to the next instruction.
92921*/
92922/* Opcode: IdxGT P1 P2 P3 P4 *
92923** Synopsis: key=r[P3@P4]
92924**
92925** The P4 register values beginning with P3 form an unpacked index
92926** key that omits the PRIMARY KEY. Compare this key value against the index
92927** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
92928** fields at the end.
92929**
92930** If the P1 index entry is greater than the key value
92931** then jump to P2. Otherwise fall through to the next instruction.
92932*/
92933/* Opcode: IdxLT P1 P2 P3 P4 *
92934** Synopsis: key=r[P3@P4]
92935**
92936** The P4 register values beginning with P3 form an unpacked index
92937** key that omits the PRIMARY KEY or ROWID. Compare this key value against
92938** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
92939** ROWID on the P1 index.
92940**
92941** If the P1 index entry is less than the key value then jump to P2.
92942** Otherwise fall through to the next instruction.
92943*/
92944/* Opcode: IdxLE P1 P2 P3 P4 *
92945** Synopsis: key=r[P3@P4]
92946**
92947** The P4 register values beginning with P3 form an unpacked index
92948** key that omits the PRIMARY KEY or ROWID. Compare this key value against
92949** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
92950** ROWID on the P1 index.
92951**
92952** If the P1 index entry is less than or equal to the key value then jump
92953** to P2. Otherwise fall through to the next instruction.
92954*/
92955case OP_IdxLE: /* jump */
92956case OP_IdxGT: /* jump */
92957case OP_IdxLT: /* jump */
92958case OP_IdxGE: { /* jump */
92959 VdbeCursor *pC;
92960 int res;
92961 UnpackedRecord r;
92962
92963 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
92964 pC = p->apCsr[pOp->p1];
92965 assert( pC!=0 );
92966 assert( pC->isOrdered );
92967 assert( pC->eCurType==CURTYPE_BTREE );
92968 assert( pC->uc.pCursor!=0);
92969 assert( pC->deferredMoveto==0 );
92970 assert( pOp->p4type==P4_INT32 );
92971 r.pKeyInfo = pC->pKeyInfo;
92972 r.nField = (u16)pOp->p4.i;
92973 if( pOp->opcode<OP_IdxLT ){
92974 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
92975 r.default_rc = -1;
92976 }else{
92977 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
92978 r.default_rc = 0;
92979 }
92980 r.aMem = &aMem[pOp->p3];
92981#ifdef SQLITE_DEBUG
92982 {
92983 int i;
92984 for(i=0; i<r.nField; i++){
92985 assert( memIsValid(&r.aMem[i]) );
92986 REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]);
92987 }
92988 }
92989#endif
92990
92991 /* Inlined version of sqlite3VdbeIdxKeyCompare() */
92992 {
92993 i64 nCellKey = 0;
92994 BtCursor *pCur;
92995 Mem m;
92996
92997 assert( pC->eCurType==CURTYPE_BTREE );
92998 pCur = pC->uc.pCursor;
92999 assert( sqlite3BtreeCursorIsValid(pCur) );
93000 nCellKey = sqlite3BtreePayloadSize(pCur);
93001 /* nCellKey will always be between 0 and 0xffffffff because of the way
93002 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
93003 if( nCellKey<=0 || nCellKey>0x7fffffff ){
93004 rc = SQLITE_CORRUPT_BKPT;
93005 goto abort_due_to_error;
93006 }
93007 sqlite3VdbeMemInit(pMem: &m, db, flags: 0);
93008 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, amt: (u32)nCellKey, pMem: &m);
93009 if( rc ) goto abort_due_to_error;
93010 res = sqlite3VdbeRecordCompareWithSkip(nKey1: m.n, pKey1: m.z, pPKey2: &r, bSkip: 0);
93011 sqlite3VdbeMemRelease(p: &m);
93012 }
93013 /* End of inlined sqlite3VdbeIdxKeyCompare() */
93014
93015 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
93016 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
93017 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
93018 res = -res;
93019 }else{
93020 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
93021 res++;
93022 }
93023 VdbeBranchTaken(res>0,2);
93024 assert( rc==SQLITE_OK );
93025 if( res>0 ) goto jump_to_p2;
93026 break;
93027}
93028
93029/* Opcode: Destroy P1 P2 P3 * *
93030**
93031** Delete an entire database table or index whose root page in the database
93032** file is given by P1.
93033**
93034** The table being destroyed is in the main database file if P3==0. If
93035** P3==1 then the table to be clear is in the auxiliary database file
93036** that is used to store tables create using CREATE TEMPORARY TABLE.
93037**
93038** If AUTOVACUUM is enabled then it is possible that another root page
93039** might be moved into the newly deleted root page in order to keep all
93040** root pages contiguous at the beginning of the database. The former
93041** value of the root page that moved - its value before the move occurred -
93042** is stored in register P2. If no page movement was required (because the
93043** table being dropped was already the last one in the database) then a
93044** zero is stored in register P2. If AUTOVACUUM is disabled then a zero
93045** is stored in register P2.
93046**
93047** This opcode throws an error if there are any active reader VMs when
93048** it is invoked. This is done to avoid the difficulty associated with
93049** updating existing cursors when a root page is moved in an AUTOVACUUM
93050** database. This error is thrown even if the database is not an AUTOVACUUM
93051** db in order to avoid introducing an incompatibility between autovacuum
93052** and non-autovacuum modes.
93053**
93054** See also: Clear
93055*/
93056case OP_Destroy: { /* out2 */
93057 int iMoved;
93058 int iDb;
93059
93060 sqlite3VdbeIncrWriteCounter(p, 0);
93061 assert( p->readOnly==0 );
93062 assert( pOp->p1>1 );
93063 pOut = out2Prerelease(p, pOp);
93064 pOut->flags = MEM_Null;
93065 if( db->nVdbeRead > db->nVDestroy+1 ){
93066 rc = SQLITE_LOCKED;
93067 p->errorAction = OE_Abort;
93068 goto abort_due_to_error;
93069 }else{
93070 iDb = pOp->p3;
93071 assert( DbMaskTest(p->btreeMask, iDb) );
93072 iMoved = 0; /* Not needed. Only to silence a warning. */
93073 rc = sqlite3BtreeDropTable(p: db->aDb[iDb].pBt, iTable: pOp->p1, piMoved: &iMoved);
93074 pOut->flags = MEM_Int;
93075 pOut->u.i = iMoved;
93076 if( rc ) goto abort_due_to_error;
93077#ifndef SQLITE_OMIT_AUTOVACUUM
93078 if( iMoved!=0 ){
93079 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
93080 /* All OP_Destroy operations occur on the same btree */
93081 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
93082 resetSchemaOnFault = iDb+1;
93083 }
93084#endif
93085 }
93086 break;
93087}
93088
93089/* Opcode: Clear P1 P2 P3
93090**
93091** Delete all contents of the database table or index whose root page
93092** in the database file is given by P1. But, unlike Destroy, do not
93093** remove the table or index from the database file.
93094**
93095** The table being clear is in the main database file if P2==0. If
93096** P2==1 then the table to be clear is in the auxiliary database file
93097** that is used to store tables create using CREATE TEMPORARY TABLE.
93098**
93099** If the P3 value is non-zero, then the row change count is incremented
93100** by the number of rows in the table being cleared. If P3 is greater
93101** than zero, then the value stored in register P3 is also incremented
93102** by the number of rows in the table being cleared.
93103**
93104** See also: Destroy
93105*/
93106case OP_Clear: {
93107 i64 nChange;
93108
93109 sqlite3VdbeIncrWriteCounter(p, 0);
93110 nChange = 0;
93111 assert( p->readOnly==0 );
93112 assert( DbMaskTest(p->btreeMask, pOp->p2) );
93113 rc = sqlite3BtreeClearTable(p: db->aDb[pOp->p2].pBt, iTable: (u32)pOp->p1, pnChange: &nChange);
93114 if( pOp->p3 ){
93115 p->nChange += nChange;
93116 if( pOp->p3>0 ){
93117 assert( memIsValid(&aMem[pOp->p3]) );
93118 memAboutToChange(p, &aMem[pOp->p3]);
93119 aMem[pOp->p3].u.i += nChange;
93120 }
93121 }
93122 if( rc ) goto abort_due_to_error;
93123 break;
93124}
93125
93126/* Opcode: ResetSorter P1 * * * *
93127**
93128** Delete all contents from the ephemeral table or sorter
93129** that is open on cursor P1.
93130**
93131** This opcode only works for cursors used for sorting and
93132** opened with OP_OpenEphemeral or OP_SorterOpen.
93133*/
93134case OP_ResetSorter: {
93135 VdbeCursor *pC;
93136
93137 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
93138 pC = p->apCsr[pOp->p1];
93139 assert( pC!=0 );
93140 if( isSorter(pC) ){
93141 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
93142 }else{
93143 assert( pC->eCurType==CURTYPE_BTREE );
93144 assert( pC->isEphemeral );
93145 rc = sqlite3BtreeClearTableOfCursor(pCur: pC->uc.pCursor);
93146 if( rc ) goto abort_due_to_error;
93147 }
93148 break;
93149}
93150
93151/* Opcode: CreateBtree P1 P2 P3 * *
93152** Synopsis: r[P2]=root iDb=P1 flags=P3
93153**
93154** Allocate a new b-tree in the main database file if P1==0 or in the
93155** TEMP database file if P1==1 or in an attached database if
93156** P1>1. The P3 argument must be 1 (BTREE_INTKEY) for a rowid table
93157** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table.
93158** The root page number of the new b-tree is stored in register P2.
93159*/
93160case OP_CreateBtree: { /* out2 */
93161 Pgno pgno;
93162 Db *pDb;
93163
93164 sqlite3VdbeIncrWriteCounter(p, 0);
93165 pOut = out2Prerelease(p, pOp);
93166 pgno = 0;
93167 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
93168 assert( pOp->p1>=0 && pOp->p1<db->nDb );
93169 assert( DbMaskTest(p->btreeMask, pOp->p1) );
93170 assert( p->readOnly==0 );
93171 pDb = &db->aDb[pOp->p1];
93172 assert( pDb->pBt!=0 );
93173 rc = sqlite3BtreeCreateTable(p: pDb->pBt, piTable: &pgno, flags: pOp->p3);
93174 if( rc ) goto abort_due_to_error;
93175 pOut->u.i = pgno;
93176 break;
93177}
93178
93179/* Opcode: SqlExec * * * P4 *
93180**
93181** Run the SQL statement or statements specified in the P4 string.
93182*/
93183case OP_SqlExec: {
93184 sqlite3VdbeIncrWriteCounter(p, 0);
93185 db->nSqlExec++;
93186 rc = sqlite3_exec(db, sql: pOp->p4.z, callback: 0, 0, errmsg: 0);
93187 db->nSqlExec--;
93188 if( rc ) goto abort_due_to_error;
93189 break;
93190}
93191
93192/* Opcode: ParseSchema P1 * * P4 *
93193**
93194** Read and parse all entries from the schema table of database P1
93195** that match the WHERE clause P4. If P4 is a NULL pointer, then the
93196** entire schema for P1 is reparsed.
93197**
93198** This opcode invokes the parser to create a new virtual machine,
93199** then runs the new virtual machine. It is thus a re-entrant opcode.
93200*/
93201case OP_ParseSchema: {
93202 int iDb;
93203 const char *zSchema;
93204 char *zSql;
93205 InitData initData;
93206
93207 /* Any prepared statement that invokes this opcode will hold mutexes
93208 ** on every btree. This is a prerequisite for invoking
93209 ** sqlite3InitCallback().
93210 */
93211#ifdef SQLITE_DEBUG
93212 for(iDb=0; iDb<db->nDb; iDb++){
93213 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
93214 }
93215#endif
93216
93217 iDb = pOp->p1;
93218 assert( iDb>=0 && iDb<db->nDb );
93219 assert( DbHasProperty(db, iDb, DB_SchemaLoaded)
93220 || db->mallocFailed
93221 || (CORRUPT_DB && (db->flags & SQLITE_NoSchemaError)!=0) );
93222
93223#ifndef SQLITE_OMIT_ALTERTABLE
93224 if( pOp->p4.z==0 ){
93225 sqlite3SchemaClear(db->aDb[iDb].pSchema);
93226 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
93227 rc = sqlite3InitOne(db, iDb, &p->zErrMsg, pOp->p5);
93228 db->mDbFlags |= DBFLAG_SchemaChange;
93229 p->expired = 0;
93230 }else
93231#endif
93232 {
93233 zSchema = LEGACY_SCHEMA_TABLE;
93234 initData.db = db;
93235 initData.iDb = iDb;
93236 initData.pzErrMsg = &p->zErrMsg;
93237 initData.mInitFlags = 0;
93238 initData.mxPage = sqlite3BtreeLastPage(p: db->aDb[iDb].pBt);
93239 zSql = sqlite3MPrintf(db,
93240 zFormat: "SELECT*FROM\"%w\".%s WHERE %s ORDER BY rowid",
93241 db->aDb[iDb].zDbSName, zSchema, pOp->p4.z);
93242 if( zSql==0 ){
93243 rc = SQLITE_NOMEM_BKPT;
93244 }else{
93245 assert( db->init.busy==0 );
93246 db->init.busy = 1;
93247 initData.rc = SQLITE_OK;
93248 initData.nInitRow = 0;
93249 assert( !db->mallocFailed );
93250 rc = sqlite3_exec(db, sql: zSql, callback: sqlite3InitCallback, &initData, errmsg: 0);
93251 if( rc==SQLITE_OK ) rc = initData.rc;
93252 if( rc==SQLITE_OK && initData.nInitRow==0 ){
93253 /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse
93254 ** at least one SQL statement. Any less than that indicates that
93255 ** the sqlite_schema table is corrupt. */
93256 rc = SQLITE_CORRUPT_BKPT;
93257 }
93258 sqlite3DbFreeNN(db, p: zSql);
93259 db->init.busy = 0;
93260 }
93261 }
93262 if( rc ){
93263 sqlite3ResetAllSchemasOfConnection(db);
93264 if( rc==SQLITE_NOMEM ){
93265 goto no_mem;
93266 }
93267 goto abort_due_to_error;
93268 }
93269 break;
93270}
93271
93272#if !defined(SQLITE_OMIT_ANALYZE)
93273/* Opcode: LoadAnalysis P1 * * * *
93274**
93275** Read the sqlite_stat1 table for database P1 and load the content
93276** of that table into the internal index hash table. This will cause
93277** the analysis to be used when preparing all subsequent queries.
93278*/
93279case OP_LoadAnalysis: {
93280 assert( pOp->p1>=0 && pOp->p1<db->nDb );
93281 rc = sqlite3AnalysisLoad(db, iDB: pOp->p1);
93282 if( rc ) goto abort_due_to_error;
93283 break;
93284}
93285#endif /* !defined(SQLITE_OMIT_ANALYZE) */
93286
93287/* Opcode: DropTable P1 * * P4 *
93288**
93289** Remove the internal (in-memory) data structures that describe
93290** the table named P4 in database P1. This is called after a table
93291** is dropped from disk (using the Destroy opcode) in order to keep
93292** the internal representation of the
93293** schema consistent with what is on disk.
93294*/
93295case OP_DropTable: {
93296 sqlite3VdbeIncrWriteCounter(p, 0);
93297 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
93298 break;
93299}
93300
93301/* Opcode: DropIndex P1 * * P4 *
93302**
93303** Remove the internal (in-memory) data structures that describe
93304** the index named P4 in database P1. This is called after an index
93305** is dropped from disk (using the Destroy opcode)
93306** in order to keep the internal representation of the
93307** schema consistent with what is on disk.
93308*/
93309case OP_DropIndex: {
93310 sqlite3VdbeIncrWriteCounter(p, 0);
93311 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
93312 break;
93313}
93314
93315/* Opcode: DropTrigger P1 * * P4 *
93316**
93317** Remove the internal (in-memory) data structures that describe
93318** the trigger named P4 in database P1. This is called after a trigger
93319** is dropped from disk (using the Destroy opcode) in order to keep
93320** the internal representation of the
93321** schema consistent with what is on disk.
93322*/
93323case OP_DropTrigger: {
93324 sqlite3VdbeIncrWriteCounter(p, 0);
93325 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
93326 break;
93327}
93328
93329
93330#ifndef SQLITE_OMIT_INTEGRITY_CHECK
93331/* Opcode: IntegrityCk P1 P2 P3 P4 P5
93332**
93333** Do an analysis of the currently open database. Store in
93334** register P1 the text of an error message describing any problems.
93335** If no problems are found, store a NULL in register P1.
93336**
93337** The register P3 contains one less than the maximum number of allowed errors.
93338** At most reg(P3) errors will be reported.
93339** In other words, the analysis stops as soon as reg(P1) errors are
93340** seen. Reg(P1) is updated with the number of errors remaining.
93341**
93342** The root page numbers of all tables in the database are integers
93343** stored in P4_INTARRAY argument.
93344**
93345** If P5 is not zero, the check is done on the auxiliary database
93346** file, not the main database file.
93347**
93348** This opcode is used to implement the integrity_check pragma.
93349*/
93350case OP_IntegrityCk: {
93351 int nRoot; /* Number of tables to check. (Number of root pages.) */
93352 Pgno *aRoot; /* Array of rootpage numbers for tables to be checked */
93353 int nErr; /* Number of errors reported */
93354 char *z; /* Text of the error report */
93355 Mem *pnErr; /* Register keeping track of errors remaining */
93356
93357 assert( p->bIsReader );
93358 nRoot = pOp->p2;
93359 aRoot = pOp->p4.ai;
93360 assert( nRoot>0 );
93361 assert( aRoot[0]==(Pgno)nRoot );
93362 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
93363 pnErr = &aMem[pOp->p3];
93364 assert( (pnErr->flags & MEM_Int)!=0 );
93365 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
93366 pIn1 = &aMem[pOp->p1];
93367 assert( pOp->p5<db->nDb );
93368 assert( DbMaskTest(p->btreeMask, pOp->p5) );
93369 z = sqlite3BtreeIntegrityCheck(db, p: db->aDb[pOp->p5].pBt, aRoot: &aRoot[1], nRoot,
93370 mxErr: (int)pnErr->u.i+1, pnErr: &nErr);
93371 sqlite3VdbeMemSetNull(pMem: pIn1);
93372 if( nErr==0 ){
93373 assert( z==0 );
93374 }else if( z==0 ){
93375 goto no_mem;
93376 }else{
93377 pnErr->u.i -= nErr-1;
93378 sqlite3VdbeMemSetStr(pMem: pIn1, z, n: -1, SQLITE_UTF8, xDel: sqlite3_free);
93379 }
93380 UPDATE_MAX_BLOBSIZE(pIn1);
93381 sqlite3VdbeChangeEncoding(pMem: pIn1, desiredEnc: encoding);
93382 goto check_for_interrupt;
93383}
93384#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
93385
93386/* Opcode: RowSetAdd P1 P2 * * *
93387** Synopsis: rowset(P1)=r[P2]
93388**
93389** Insert the integer value held by register P2 into a RowSet object
93390** held in register P1.
93391**
93392** An assertion fails if P2 is not an integer.
93393*/
93394case OP_RowSetAdd: { /* in1, in2 */
93395 pIn1 = &aMem[pOp->p1];
93396 pIn2 = &aMem[pOp->p2];
93397 assert( (pIn2->flags & MEM_Int)!=0 );
93398 if( (pIn1->flags & MEM_Blob)==0 ){
93399 if( sqlite3VdbeMemSetRowSet(pMem: pIn1) ) goto no_mem;
93400 }
93401 assert( sqlite3VdbeMemIsRowSet(pIn1) );
93402 sqlite3RowSetInsert(p: (RowSet*)pIn1->z, rowid: pIn2->u.i);
93403 break;
93404}
93405
93406/* Opcode: RowSetRead P1 P2 P3 * *
93407** Synopsis: r[P3]=rowset(P1)
93408**
93409** Extract the smallest value from the RowSet object in P1
93410** and put that value into register P3.
93411** Or, if RowSet object P1 is initially empty, leave P3
93412** unchanged and jump to instruction P2.
93413*/
93414case OP_RowSetRead: { /* jump, in1, out3 */
93415 i64 val;
93416
93417 pIn1 = &aMem[pOp->p1];
93418 assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) );
93419 if( (pIn1->flags & MEM_Blob)==0
93420 || sqlite3RowSetNext(p: (RowSet*)pIn1->z, pRowid: &val)==0
93421 ){
93422 /* The boolean index is empty */
93423 sqlite3VdbeMemSetNull(pMem: pIn1);
93424 VdbeBranchTaken(1,2);
93425 goto jump_to_p2_and_check_for_interrupt;
93426 }else{
93427 /* A value was pulled from the index */
93428 VdbeBranchTaken(0,2);
93429 sqlite3VdbeMemSetInt64(pMem: &aMem[pOp->p3], val);
93430 }
93431 goto check_for_interrupt;
93432}
93433
93434/* Opcode: RowSetTest P1 P2 P3 P4
93435** Synopsis: if r[P3] in rowset(P1) goto P2
93436**
93437** Register P3 is assumed to hold a 64-bit integer value. If register P1
93438** contains a RowSet object and that RowSet object contains
93439** the value held in P3, jump to register P2. Otherwise, insert the
93440** integer in P3 into the RowSet and continue on to the
93441** next opcode.
93442**
93443** The RowSet object is optimized for the case where sets of integers
93444** are inserted in distinct phases, which each set contains no duplicates.
93445** Each set is identified by a unique P4 value. The first set
93446** must have P4==0, the final set must have P4==-1, and for all other sets
93447** must have P4>0.
93448**
93449** This allows optimizations: (a) when P4==0 there is no need to test
93450** the RowSet object for P3, as it is guaranteed not to contain it,
93451** (b) when P4==-1 there is no need to insert the value, as it will
93452** never be tested for, and (c) when a value that is part of set X is
93453** inserted, there is no need to search to see if the same value was
93454** previously inserted as part of set X (only if it was previously
93455** inserted as part of some other set).
93456*/
93457case OP_RowSetTest: { /* jump, in1, in3 */
93458 int iSet;
93459 int exists;
93460
93461 pIn1 = &aMem[pOp->p1];
93462 pIn3 = &aMem[pOp->p3];
93463 iSet = pOp->p4.i;
93464 assert( pIn3->flags&MEM_Int );
93465
93466 /* If there is anything other than a rowset object in memory cell P1,
93467 ** delete it now and initialize P1 with an empty rowset
93468 */
93469 if( (pIn1->flags & MEM_Blob)==0 ){
93470 if( sqlite3VdbeMemSetRowSet(pMem: pIn1) ) goto no_mem;
93471 }
93472 assert( sqlite3VdbeMemIsRowSet(pIn1) );
93473 assert( pOp->p4type==P4_INT32 );
93474 assert( iSet==-1 || iSet>=0 );
93475 if( iSet ){
93476 exists = sqlite3RowSetTest(pRowSet: (RowSet*)pIn1->z, iBatch: iSet, iRowid: pIn3->u.i);
93477 VdbeBranchTaken(exists!=0,2);
93478 if( exists ) goto jump_to_p2;
93479 }
93480 if( iSet>=0 ){
93481 sqlite3RowSetInsert(p: (RowSet*)pIn1->z, rowid: pIn3->u.i);
93482 }
93483 break;
93484}
93485
93486
93487#ifndef SQLITE_OMIT_TRIGGER
93488
93489/* Opcode: Program P1 P2 P3 P4 P5
93490**
93491** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
93492**
93493** P1 contains the address of the memory cell that contains the first memory
93494** cell in an array of values used as arguments to the sub-program. P2
93495** contains the address to jump to if the sub-program throws an IGNORE
93496** exception using the RAISE() function. Register P3 contains the address
93497** of a memory cell in this (the parent) VM that is used to allocate the
93498** memory required by the sub-vdbe at runtime.
93499**
93500** P4 is a pointer to the VM containing the trigger program.
93501**
93502** If P5 is non-zero, then recursive program invocation is enabled.
93503*/
93504case OP_Program: { /* jump */
93505 int nMem; /* Number of memory registers for sub-program */
93506 int nByte; /* Bytes of runtime space required for sub-program */
93507 Mem *pRt; /* Register to allocate runtime space */
93508 Mem *pMem; /* Used to iterate through memory cells */
93509 Mem *pEnd; /* Last memory cell in new array */
93510 VdbeFrame *pFrame; /* New vdbe frame to execute in */
93511 SubProgram *pProgram; /* Sub-program to execute */
93512 void *t; /* Token identifying trigger */
93513
93514 pProgram = pOp->p4.pProgram;
93515 pRt = &aMem[pOp->p3];
93516 assert( pProgram->nOp>0 );
93517
93518 /* If the p5 flag is clear, then recursive invocation of triggers is
93519 ** disabled for backwards compatibility (p5 is set if this sub-program
93520 ** is really a trigger, not a foreign key action, and the flag set
93521 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
93522 **
93523 ** It is recursive invocation of triggers, at the SQL level, that is
93524 ** disabled. In some cases a single trigger may generate more than one
93525 ** SubProgram (if the trigger may be executed with more than one different
93526 ** ON CONFLICT algorithm). SubProgram structures associated with a
93527 ** single trigger all have the same value for the SubProgram.token
93528 ** variable. */
93529 if( pOp->p5 ){
93530 t = pProgram->token;
93531 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
93532 if( pFrame ) break;
93533 }
93534
93535 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
93536 rc = SQLITE_ERROR;
93537 sqlite3VdbeError(p, zFormat: "too many levels of trigger recursion");
93538 goto abort_due_to_error;
93539 }
93540
93541 /* Register pRt is used to store the memory required to save the state
93542 ** of the current program, and the memory required at runtime to execute
93543 ** the trigger program. If this trigger has been fired before, then pRt
93544 ** is already allocated. Otherwise, it must be initialized. */
93545 if( (pRt->flags&MEM_Blob)==0 ){
93546 /* SubProgram.nMem is set to the number of memory cells used by the
93547 ** program stored in SubProgram.aOp. As well as these, one memory
93548 ** cell is required for each cursor used by the program. Set local
93549 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
93550 */
93551 nMem = pProgram->nMem + pProgram->nCsr;
93552 assert( nMem>0 );
93553 if( pProgram->nCsr==0 ) nMem++;
93554 nByte = ROUND8(sizeof(VdbeFrame))
93555 + nMem * sizeof(Mem)
93556 + pProgram->nCsr * sizeof(VdbeCursor*)
93557 + (pProgram->nOp + 7)/8;
93558 pFrame = sqlite3DbMallocZero(db, n: nByte);
93559 if( !pFrame ){
93560 goto no_mem;
93561 }
93562 sqlite3VdbeMemRelease(p: pRt);
93563 pRt->flags = MEM_Blob|MEM_Dyn;
93564 pRt->z = (char*)pFrame;
93565 pRt->n = nByte;
93566 pRt->xDel = sqlite3VdbeFrameMemDel;
93567
93568 pFrame->v = p;
93569 pFrame->nChildMem = nMem;
93570 pFrame->nChildCsr = pProgram->nCsr;
93571 pFrame->pc = (int)(pOp - aOp);
93572 pFrame->aMem = p->aMem;
93573 pFrame->nMem = p->nMem;
93574 pFrame->apCsr = p->apCsr;
93575 pFrame->nCursor = p->nCursor;
93576 pFrame->aOp = p->aOp;
93577 pFrame->nOp = p->nOp;
93578 pFrame->token = pProgram->token;
93579#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
93580 pFrame->anExec = p->anExec;
93581#endif
93582#ifdef SQLITE_DEBUG
93583 pFrame->iFrameMagic = SQLITE_FRAME_MAGIC;
93584#endif
93585
93586 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
93587 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
93588 pMem->flags = MEM_Undefined;
93589 pMem->db = db;
93590 }
93591 }else{
93592 pFrame = (VdbeFrame*)pRt->z;
93593 assert( pRt->xDel==sqlite3VdbeFrameMemDel );
93594 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
93595 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
93596 assert( pProgram->nCsr==pFrame->nChildCsr );
93597 assert( (int)(pOp - aOp)==pFrame->pc );
93598 }
93599
93600 p->nFrame++;
93601 pFrame->pParent = p->pFrame;
93602 pFrame->lastRowid = db->lastRowid;
93603 pFrame->nChange = p->nChange;
93604 pFrame->nDbChange = p->db->nChange;
93605 assert( pFrame->pAuxData==0 );
93606 pFrame->pAuxData = p->pAuxData;
93607 p->pAuxData = 0;
93608 p->nChange = 0;
93609 p->pFrame = pFrame;
93610 p->aMem = aMem = VdbeFrameMem(pFrame);
93611 p->nMem = pFrame->nChildMem;
93612 p->nCursor = (u16)pFrame->nChildCsr;
93613 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
93614 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
93615 memset(s: pFrame->aOnce, c: 0, n: (pProgram->nOp + 7)/8);
93616 p->aOp = aOp = pProgram->aOp;
93617 p->nOp = pProgram->nOp;
93618#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
93619 p->anExec = 0;
93620#endif
93621#ifdef SQLITE_DEBUG
93622 /* Verify that second and subsequent executions of the same trigger do not
93623 ** try to reuse register values from the first use. */
93624 {
93625 int i;
93626 for(i=0; i<p->nMem; i++){
93627 aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */
93628 MemSetTypeFlag(&aMem[i], MEM_Undefined); /* Fault if this reg is reused */
93629 }
93630 }
93631#endif
93632 pOp = &aOp[-1];
93633 goto check_for_interrupt;
93634}
93635
93636/* Opcode: Param P1 P2 * * *
93637**
93638** This opcode is only ever present in sub-programs called via the
93639** OP_Program instruction. Copy a value currently stored in a memory
93640** cell of the calling (parent) frame to cell P2 in the current frames
93641** address space. This is used by trigger programs to access the new.*
93642** and old.* values.
93643**
93644** The address of the cell in the parent frame is determined by adding
93645** the value of the P1 argument to the value of the P1 argument to the
93646** calling OP_Program instruction.
93647*/
93648case OP_Param: { /* out2 */
93649 VdbeFrame *pFrame;
93650 Mem *pIn;
93651 pOut = out2Prerelease(p, pOp);
93652 pFrame = p->pFrame;
93653 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
93654 sqlite3VdbeMemShallowCopy(pTo: pOut, pFrom: pIn, MEM_Ephem);
93655 break;
93656}
93657
93658#endif /* #ifndef SQLITE_OMIT_TRIGGER */
93659
93660#ifndef SQLITE_OMIT_FOREIGN_KEY
93661/* Opcode: FkCounter P1 P2 * * *
93662** Synopsis: fkctr[P1]+=P2
93663**
93664** Increment a "constraint counter" by P2 (P2 may be negative or positive).
93665** If P1 is non-zero, the database constraint counter is incremented
93666** (deferred foreign key constraints). Otherwise, if P1 is zero, the
93667** statement counter is incremented (immediate foreign key constraints).
93668*/
93669case OP_FkCounter: {
93670 if( db->flags & SQLITE_DeferFKs ){
93671 db->nDeferredImmCons += pOp->p2;
93672 }else if( pOp->p1 ){
93673 db->nDeferredCons += pOp->p2;
93674 }else{
93675 p->nFkConstraint += pOp->p2;
93676 }
93677 break;
93678}
93679
93680/* Opcode: FkIfZero P1 P2 * * *
93681** Synopsis: if fkctr[P1]==0 goto P2
93682**
93683** This opcode tests if a foreign key constraint-counter is currently zero.
93684** If so, jump to instruction P2. Otherwise, fall through to the next
93685** instruction.
93686**
93687** If P1 is non-zero, then the jump is taken if the database constraint-counter
93688** is zero (the one that counts deferred constraint violations). If P1 is
93689** zero, the jump is taken if the statement constraint-counter is zero
93690** (immediate foreign key constraint violations).
93691*/
93692case OP_FkIfZero: { /* jump */
93693 if( pOp->p1 ){
93694 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
93695 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
93696 }else{
93697 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
93698 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
93699 }
93700 break;
93701}
93702#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
93703
93704#ifndef SQLITE_OMIT_AUTOINCREMENT
93705/* Opcode: MemMax P1 P2 * * *
93706** Synopsis: r[P1]=max(r[P1],r[P2])
93707**
93708** P1 is a register in the root frame of this VM (the root frame is
93709** different from the current frame if this instruction is being executed
93710** within a sub-program). Set the value of register P1 to the maximum of
93711** its current value and the value in register P2.
93712**
93713** This instruction throws an error if the memory cell is not initially
93714** an integer.
93715*/
93716case OP_MemMax: { /* in2 */
93717 VdbeFrame *pFrame;
93718 if( p->pFrame ){
93719 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
93720 pIn1 = &pFrame->aMem[pOp->p1];
93721 }else{
93722 pIn1 = &aMem[pOp->p1];
93723 }
93724 assert( memIsValid(pIn1) );
93725 sqlite3VdbeMemIntegerify(pMem: pIn1);
93726 pIn2 = &aMem[pOp->p2];
93727 sqlite3VdbeMemIntegerify(pMem: pIn2);
93728 if( pIn1->u.i<pIn2->u.i){
93729 pIn1->u.i = pIn2->u.i;
93730 }
93731 break;
93732}
93733#endif /* SQLITE_OMIT_AUTOINCREMENT */
93734
93735/* Opcode: IfPos P1 P2 P3 * *
93736** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
93737**
93738** Register P1 must contain an integer.
93739** If the value of register P1 is 1 or greater, subtract P3 from the
93740** value in P1 and jump to P2.
93741**
93742** If the initial value of register P1 is less than 1, then the
93743** value is unchanged and control passes through to the next instruction.
93744*/
93745case OP_IfPos: { /* jump, in1 */
93746 pIn1 = &aMem[pOp->p1];
93747 assert( pIn1->flags&MEM_Int );
93748 VdbeBranchTaken( pIn1->u.i>0, 2);
93749 if( pIn1->u.i>0 ){
93750 pIn1->u.i -= pOp->p3;
93751 goto jump_to_p2;
93752 }
93753 break;
93754}
93755
93756/* Opcode: OffsetLimit P1 P2 P3 * *
93757** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
93758**
93759** This opcode performs a commonly used computation associated with
93760** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3]
93761** holds the offset counter. The opcode computes the combined value
93762** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
93763** value computed is the total number of rows that will need to be
93764** visited in order to complete the query.
93765**
93766** If r[P3] is zero or negative, that means there is no OFFSET
93767** and r[P2] is set to be the value of the LIMIT, r[P1].
93768**
93769** if r[P1] is zero or negative, that means there is no LIMIT
93770** and r[P2] is set to -1.
93771**
93772** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
93773*/
93774case OP_OffsetLimit: { /* in1, out2, in3 */
93775 i64 x;
93776 pIn1 = &aMem[pOp->p1];
93777 pIn3 = &aMem[pOp->p3];
93778 pOut = out2Prerelease(p, pOp);
93779 assert( pIn1->flags & MEM_Int );
93780 assert( pIn3->flags & MEM_Int );
93781 x = pIn1->u.i;
93782 if( x<=0 || sqlite3AddInt64(pA: &x, iB: pIn3->u.i>0?pIn3->u.i:0) ){
93783 /* If the LIMIT is less than or equal to zero, loop forever. This
93784 ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then
93785 ** also loop forever. This is undocumented. In fact, one could argue
93786 ** that the loop should terminate. But assuming 1 billion iterations
93787 ** per second (far exceeding the capabilities of any current hardware)
93788 ** it would take nearly 300 years to actually reach the limit. So
93789 ** looping forever is a reasonable approximation. */
93790 pOut->u.i = -1;
93791 }else{
93792 pOut->u.i = x;
93793 }
93794 break;
93795}
93796
93797/* Opcode: IfNotZero P1 P2 * * *
93798** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
93799**
93800** Register P1 must contain an integer. If the content of register P1 is
93801** initially greater than zero, then decrement the value in register P1.
93802** If it is non-zero (negative or positive) and then also jump to P2.
93803** If register P1 is initially zero, leave it unchanged and fall through.
93804*/
93805case OP_IfNotZero: { /* jump, in1 */
93806 pIn1 = &aMem[pOp->p1];
93807 assert( pIn1->flags&MEM_Int );
93808 VdbeBranchTaken(pIn1->u.i<0, 2);
93809 if( pIn1->u.i ){
93810 if( pIn1->u.i>0 ) pIn1->u.i--;
93811 goto jump_to_p2;
93812 }
93813 break;
93814}
93815
93816/* Opcode: DecrJumpZero P1 P2 * * *
93817** Synopsis: if (--r[P1])==0 goto P2
93818**
93819** Register P1 must hold an integer. Decrement the value in P1
93820** and jump to P2 if the new value is exactly zero.
93821*/
93822case OP_DecrJumpZero: { /* jump, in1 */
93823 pIn1 = &aMem[pOp->p1];
93824 assert( pIn1->flags&MEM_Int );
93825 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
93826 VdbeBranchTaken(pIn1->u.i==0, 2);
93827 if( pIn1->u.i==0 ) goto jump_to_p2;
93828 break;
93829}
93830
93831
93832/* Opcode: AggStep * P2 P3 P4 P5
93833** Synopsis: accum=r[P3] step(r[P2@P5])
93834**
93835** Execute the xStep function for an aggregate.
93836** The function has P5 arguments. P4 is a pointer to the
93837** FuncDef structure that specifies the function. Register P3 is the
93838** accumulator.
93839**
93840** The P5 arguments are taken from register P2 and its
93841** successors.
93842*/
93843/* Opcode: AggInverse * P2 P3 P4 P5
93844** Synopsis: accum=r[P3] inverse(r[P2@P5])
93845**
93846** Execute the xInverse function for an aggregate.
93847** The function has P5 arguments. P4 is a pointer to the
93848** FuncDef structure that specifies the function. Register P3 is the
93849** accumulator.
93850**
93851** The P5 arguments are taken from register P2 and its
93852** successors.
93853*/
93854/* Opcode: AggStep1 P1 P2 P3 P4 P5
93855** Synopsis: accum=r[P3] step(r[P2@P5])
93856**
93857** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an
93858** aggregate. The function has P5 arguments. P4 is a pointer to the
93859** FuncDef structure that specifies the function. Register P3 is the
93860** accumulator.
93861**
93862** The P5 arguments are taken from register P2 and its
93863** successors.
93864**
93865** This opcode is initially coded as OP_AggStep0. On first evaluation,
93866** the FuncDef stored in P4 is converted into an sqlite3_context and
93867** the opcode is changed. In this way, the initialization of the
93868** sqlite3_context only happens once, instead of on each call to the
93869** step function.
93870*/
93871case OP_AggInverse:
93872case OP_AggStep: {
93873 int n;
93874 sqlite3_context *pCtx;
93875
93876 assert( pOp->p4type==P4_FUNCDEF );
93877 n = pOp->p5;
93878 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
93879 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
93880 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
93881 pCtx = sqlite3DbMallocRawNN(db, n: n*sizeof(sqlite3_value*) +
93882 (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
93883 if( pCtx==0 ) goto no_mem;
93884 pCtx->pMem = 0;
93885 pCtx->pOut = (Mem*)&(pCtx->argv[n]);
93886 sqlite3VdbeMemInit(pMem: pCtx->pOut, db, MEM_Null);
93887 pCtx->pFunc = pOp->p4.pFunc;
93888 pCtx->iOp = (int)(pOp - aOp);
93889 pCtx->pVdbe = p;
93890 pCtx->skipFlag = 0;
93891 pCtx->isError = 0;
93892 pCtx->argc = n;
93893 pOp->p4type = P4_FUNCCTX;
93894 pOp->p4.pCtx = pCtx;
93895
93896 /* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */
93897 assert( pOp->p1==(pOp->opcode==OP_AggInverse) );
93898
93899 pOp->opcode = OP_AggStep1;
93900 /* Fall through into OP_AggStep */
93901 /* no break */ deliberate_fall_through
93902}
93903case OP_AggStep1: {
93904 int i;
93905 sqlite3_context *pCtx;
93906 Mem *pMem;
93907
93908 assert( pOp->p4type==P4_FUNCCTX );
93909 pCtx = pOp->p4.pCtx;
93910 pMem = &aMem[pOp->p3];
93911
93912#ifdef SQLITE_DEBUG
93913 if( pOp->p1 ){
93914 /* This is an OP_AggInverse call. Verify that xStep has always
93915 ** been called at least once prior to any xInverse call. */
93916 assert( pMem->uTemp==0x1122e0e3 );
93917 }else{
93918 /* This is an OP_AggStep call. Mark it as such. */
93919 pMem->uTemp = 0x1122e0e3;
93920 }
93921#endif
93922
93923 /* If this function is inside of a trigger, the register array in aMem[]
93924 ** might change from one evaluation to the next. The next block of code
93925 ** checks to see if the register array has changed, and if so it
93926 ** reinitializes the relavant parts of the sqlite3_context object */
93927 if( pCtx->pMem != pMem ){
93928 pCtx->pMem = pMem;
93929 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
93930 }
93931
93932#ifdef SQLITE_DEBUG
93933 for(i=0; i<pCtx->argc; i++){
93934 assert( memIsValid(pCtx->argv[i]) );
93935 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
93936 }
93937#endif
93938
93939 pMem->n++;
93940 assert( pCtx->pOut->flags==MEM_Null );
93941 assert( pCtx->isError==0 );
93942 assert( pCtx->skipFlag==0 );
93943#ifndef SQLITE_OMIT_WINDOWFUNC
93944 if( pOp->p1 ){
93945 (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
93946 }else
93947#endif
93948 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
93949
93950 if( pCtx->isError ){
93951 if( pCtx->isError>0 ){
93952 sqlite3VdbeError(p, zFormat: "%s", sqlite3_value_text(pVal: pCtx->pOut));
93953 rc = pCtx->isError;
93954 }
93955 if( pCtx->skipFlag ){
93956 assert( pOp[-1].opcode==OP_CollSeq );
93957 i = pOp[-1].p1;
93958 if( i ) sqlite3VdbeMemSetInt64(pMem: &aMem[i], val: 1);
93959 pCtx->skipFlag = 0;
93960 }
93961 sqlite3VdbeMemRelease(p: pCtx->pOut);
93962 pCtx->pOut->flags = MEM_Null;
93963 pCtx->isError = 0;
93964 if( rc ) goto abort_due_to_error;
93965 }
93966 assert( pCtx->pOut->flags==MEM_Null );
93967 assert( pCtx->skipFlag==0 );
93968 break;
93969}
93970
93971/* Opcode: AggFinal P1 P2 * P4 *
93972** Synopsis: accum=r[P1] N=P2
93973**
93974** P1 is the memory location that is the accumulator for an aggregate
93975** or window function. Execute the finalizer function
93976** for an aggregate and store the result in P1.
93977**
93978** P2 is the number of arguments that the step function takes and
93979** P4 is a pointer to the FuncDef for this function. The P2
93980** argument is not used by this opcode. It is only there to disambiguate
93981** functions that can take varying numbers of arguments. The
93982** P4 argument is only needed for the case where
93983** the step function was not previously called.
93984*/
93985/* Opcode: AggValue * P2 P3 P4 *
93986** Synopsis: r[P3]=value N=P2
93987**
93988** Invoke the xValue() function and store the result in register P3.
93989**
93990** P2 is the number of arguments that the step function takes and
93991** P4 is a pointer to the FuncDef for this function. The P2
93992** argument is not used by this opcode. It is only there to disambiguate
93993** functions that can take varying numbers of arguments. The
93994** P4 argument is only needed for the case where
93995** the step function was not previously called.
93996*/
93997case OP_AggValue:
93998case OP_AggFinal: {
93999 Mem *pMem;
94000 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
94001 assert( pOp->p3==0 || pOp->opcode==OP_AggValue );
94002 pMem = &aMem[pOp->p1];
94003 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
94004#ifndef SQLITE_OMIT_WINDOWFUNC
94005 if( pOp->p3 ){
94006 memAboutToChange(p, &aMem[pOp->p3]);
94007 rc = sqlite3VdbeMemAggValue(pAccum: pMem, pOut: &aMem[pOp->p3], pFunc: pOp->p4.pFunc);
94008 pMem = &aMem[pOp->p3];
94009 }else
94010#endif
94011 {
94012 rc = sqlite3VdbeMemFinalize(pMem, pFunc: pOp->p4.pFunc);
94013 }
94014
94015 if( rc ){
94016 sqlite3VdbeError(p, zFormat: "%s", sqlite3_value_text(pVal: pMem));
94017 goto abort_due_to_error;
94018 }
94019 sqlite3VdbeChangeEncoding(pMem, desiredEnc: encoding);
94020 UPDATE_MAX_BLOBSIZE(pMem);
94021 if( sqlite3VdbeMemTooBig(p: pMem) ){
94022 goto too_big;
94023 }
94024 break;
94025}
94026
94027#ifndef SQLITE_OMIT_WAL
94028/* Opcode: Checkpoint P1 P2 P3 * *
94029**
94030** Checkpoint database P1. This is a no-op if P1 is not currently in
94031** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
94032** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns
94033** SQLITE_BUSY or not, respectively. Write the number of pages in the
94034** WAL after the checkpoint into mem[P3+1] and the number of pages
94035** in the WAL that have been checkpointed after the checkpoint
94036** completes into mem[P3+2]. However on an error, mem[P3+1] and
94037** mem[P3+2] are initialized to -1.
94038*/
94039case OP_Checkpoint: {
94040 int i; /* Loop counter */
94041 int aRes[3]; /* Results */
94042 Mem *pMem; /* Write results here */
94043
94044 assert( p->readOnly==0 );
94045 aRes[0] = 0;
94046 aRes[1] = aRes[2] = -1;
94047 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
94048 || pOp->p2==SQLITE_CHECKPOINT_FULL
94049 || pOp->p2==SQLITE_CHECKPOINT_RESTART
94050 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
94051 );
94052 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
94053 if( rc ){
94054 if( rc!=SQLITE_BUSY ) goto abort_due_to_error;
94055 rc = SQLITE_OK;
94056 aRes[0] = 1;
94057 }
94058 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
94059 sqlite3VdbeMemSetInt64(pMem, val: (i64)aRes[i]);
94060 }
94061 break;
94062};
94063#endif
94064
94065#ifndef SQLITE_OMIT_PRAGMA
94066/* Opcode: JournalMode P1 P2 P3 * *
94067**
94068** Change the journal mode of database P1 to P3. P3 must be one of the
94069** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
94070** modes (delete, truncate, persist, off and memory), this is a simple
94071** operation. No IO is required.
94072**
94073** If changing into or out of WAL mode the procedure is more complicated.
94074**
94075** Write a string containing the final journal-mode to register P2.
94076*/
94077case OP_JournalMode: { /* out2 */
94078 Btree *pBt; /* Btree to change journal mode of */
94079 Pager *pPager; /* Pager associated with pBt */
94080 int eNew; /* New journal mode */
94081 int eOld; /* The old journal mode */
94082#ifndef SQLITE_OMIT_WAL
94083 const char *zFilename; /* Name of database file for pPager */
94084#endif
94085
94086 pOut = out2Prerelease(p, pOp);
94087 eNew = pOp->p3;
94088 assert( eNew==PAGER_JOURNALMODE_DELETE
94089 || eNew==PAGER_JOURNALMODE_TRUNCATE
94090 || eNew==PAGER_JOURNALMODE_PERSIST
94091 || eNew==PAGER_JOURNALMODE_OFF
94092 || eNew==PAGER_JOURNALMODE_MEMORY
94093 || eNew==PAGER_JOURNALMODE_WAL
94094 || eNew==PAGER_JOURNALMODE_QUERY
94095 );
94096 assert( pOp->p1>=0 && pOp->p1<db->nDb );
94097 assert( p->readOnly==0 );
94098
94099 pBt = db->aDb[pOp->p1].pBt;
94100 pPager = sqlite3BtreePager(p: pBt);
94101 eOld = sqlite3PagerGetJournalMode(pPager);
94102 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
94103 assert( sqlite3BtreeHoldsMutex(pBt) );
94104 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
94105
94106#ifndef SQLITE_OMIT_WAL
94107 zFilename = sqlite3PagerFilename(pPager, nullIfMemDb: 1);
94108
94109 /* Do not allow a transition to journal_mode=WAL for a database
94110 ** in temporary storage or if the VFS does not support shared memory
94111 */
94112 if( eNew==PAGER_JOURNALMODE_WAL
94113 && (sqlite3Strlen30(z: zFilename)==0 /* Temp file */
94114 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
94115 ){
94116 eNew = eOld;
94117 }
94118
94119 if( (eNew!=eOld)
94120 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
94121 ){
94122 if( !db->autoCommit || db->nVdbeRead>1 ){
94123 rc = SQLITE_ERROR;
94124 sqlite3VdbeError(p,
94125 zFormat: "cannot change %s wal mode from within a transaction",
94126 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
94127 );
94128 goto abort_due_to_error;
94129 }else{
94130
94131 if( eOld==PAGER_JOURNALMODE_WAL ){
94132 /* If leaving WAL mode, close the log file. If successful, the call
94133 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
94134 ** file. An EXCLUSIVE lock may still be held on the database file
94135 ** after a successful return.
94136 */
94137 rc = sqlite3PagerCloseWal(pPager, db);
94138 if( rc==SQLITE_OK ){
94139 sqlite3PagerSetJournalMode(pPager, eMode: eNew);
94140 }
94141 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
94142 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
94143 ** as an intermediate */
94144 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
94145 }
94146
94147 /* Open a transaction on the database file. Regardless of the journal
94148 ** mode, this transaction always uses a rollback journal.
94149 */
94150 assert( sqlite3BtreeTxnState(pBt)!=SQLITE_TXN_WRITE );
94151 if( rc==SQLITE_OK ){
94152 rc = sqlite3BtreeSetVersion(pBtree: pBt, iVersion: (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
94153 }
94154 }
94155 }
94156#endif /* ifndef SQLITE_OMIT_WAL */
94157
94158 if( rc ) eNew = eOld;
94159 eNew = sqlite3PagerSetJournalMode(pPager, eMode: eNew);
94160
94161 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
94162 pOut->z = (char *)sqlite3JournalModename(eNew);
94163 pOut->n = sqlite3Strlen30(z: pOut->z);
94164 pOut->enc = SQLITE_UTF8;
94165 sqlite3VdbeChangeEncoding(pMem: pOut, desiredEnc: encoding);
94166 if( rc ) goto abort_due_to_error;
94167 break;
94168};
94169#endif /* SQLITE_OMIT_PRAGMA */
94170
94171#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
94172/* Opcode: Vacuum P1 P2 * * *
94173**
94174** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more
94175** for an attached database. The "temp" database may not be vacuumed.
94176**
94177** If P2 is not zero, then it is a register holding a string which is
94178** the file into which the result of vacuum should be written. When
94179** P2 is zero, the vacuum overwrites the original database.
94180*/
94181case OP_Vacuum: {
94182 assert( p->readOnly==0 );
94183 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1,
94184 pOp->p2 ? &aMem[pOp->p2] : 0);
94185 if( rc ) goto abort_due_to_error;
94186 break;
94187}
94188#endif
94189
94190#if !defined(SQLITE_OMIT_AUTOVACUUM)
94191/* Opcode: IncrVacuum P1 P2 * * *
94192**
94193** Perform a single step of the incremental vacuum procedure on
94194** the P1 database. If the vacuum has finished, jump to instruction
94195** P2. Otherwise, fall through to the next instruction.
94196*/
94197case OP_IncrVacuum: { /* jump */
94198 Btree *pBt;
94199
94200 assert( pOp->p1>=0 && pOp->p1<db->nDb );
94201 assert( DbMaskTest(p->btreeMask, pOp->p1) );
94202 assert( p->readOnly==0 );
94203 pBt = db->aDb[pOp->p1].pBt;
94204 rc = sqlite3BtreeIncrVacuum(p: pBt);
94205 VdbeBranchTaken(rc==SQLITE_DONE,2);
94206 if( rc ){
94207 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
94208 rc = SQLITE_OK;
94209 goto jump_to_p2;
94210 }
94211 break;
94212}
94213#endif
94214
94215/* Opcode: Expire P1 P2 * * *
94216**
94217** Cause precompiled statements to expire. When an expired statement
94218** is executed using sqlite3_step() it will either automatically
94219** reprepare itself (if it was originally created using sqlite3_prepare_v2())
94220** or it will fail with SQLITE_SCHEMA.
94221**
94222** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
94223** then only the currently executing statement is expired.
94224**
94225** If P2 is 0, then SQL statements are expired immediately. If P2 is 1,
94226** then running SQL statements are allowed to continue to run to completion.
94227** The P2==1 case occurs when a CREATE INDEX or similar schema change happens
94228** that might help the statement run faster but which does not affect the
94229** correctness of operation.
94230*/
94231case OP_Expire: {
94232 assert( pOp->p2==0 || pOp->p2==1 );
94233 if( !pOp->p1 ){
94234 sqlite3ExpirePreparedStatements(db, iCode: pOp->p2);
94235 }else{
94236 p->expired = pOp->p2+1;
94237 }
94238 break;
94239}
94240
94241/* Opcode: CursorLock P1 * * * *
94242**
94243** Lock the btree to which cursor P1 is pointing so that the btree cannot be
94244** written by an other cursor.
94245*/
94246case OP_CursorLock: {
94247 VdbeCursor *pC;
94248 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94249 pC = p->apCsr[pOp->p1];
94250 assert( pC!=0 );
94251 assert( pC->eCurType==CURTYPE_BTREE );
94252 sqlite3BtreeCursorPin(pCur: pC->uc.pCursor);
94253 break;
94254}
94255
94256/* Opcode: CursorUnlock P1 * * * *
94257**
94258** Unlock the btree to which cursor P1 is pointing so that it can be
94259** written by other cursors.
94260*/
94261case OP_CursorUnlock: {
94262 VdbeCursor *pC;
94263 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94264 pC = p->apCsr[pOp->p1];
94265 assert( pC!=0 );
94266 assert( pC->eCurType==CURTYPE_BTREE );
94267 sqlite3BtreeCursorUnpin(pCur: pC->uc.pCursor);
94268 break;
94269}
94270
94271#ifndef SQLITE_OMIT_SHARED_CACHE
94272/* Opcode: TableLock P1 P2 P3 P4 *
94273** Synopsis: iDb=P1 root=P2 write=P3
94274**
94275** Obtain a lock on a particular table. This instruction is only used when
94276** the shared-cache feature is enabled.
94277**
94278** P1 is the index of the database in sqlite3.aDb[] of the database
94279** on which the lock is acquired. A readlock is obtained if P3==0 or
94280** a write lock if P3==1.
94281**
94282** P2 contains the root-page of the table to lock.
94283**
94284** P4 contains a pointer to the name of the table being locked. This is only
94285** used to generate an error message if the lock cannot be obtained.
94286*/
94287case OP_TableLock: {
94288 u8 isWriteLock = (u8)pOp->p3;
94289 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){
94290 int p1 = pOp->p1;
94291 assert( p1>=0 && p1<db->nDb );
94292 assert( DbMaskTest(p->btreeMask, p1) );
94293 assert( isWriteLock==0 || isWriteLock==1 );
94294 rc = sqlite3BtreeLockTable(p: db->aDb[p1].pBt, iTab: pOp->p2, isWriteLock);
94295 if( rc ){
94296 if( (rc&0xFF)==SQLITE_LOCKED ){
94297 const char *z = pOp->p4.z;
94298 sqlite3VdbeError(p, zFormat: "database table is locked: %s", z);
94299 }
94300 goto abort_due_to_error;
94301 }
94302 }
94303 break;
94304}
94305#endif /* SQLITE_OMIT_SHARED_CACHE */
94306
94307#ifndef SQLITE_OMIT_VIRTUALTABLE
94308/* Opcode: VBegin * * * P4 *
94309**
94310** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
94311** xBegin method for that table.
94312**
94313** Also, whether or not P4 is set, check that this is not being called from
94314** within a callback to a virtual table xSync() method. If it is, the error
94315** code will be set to SQLITE_LOCKED.
94316*/
94317case OP_VBegin: {
94318 VTable *pVTab;
94319 pVTab = pOp->p4.pVtab;
94320 rc = sqlite3VtabBegin(db, pVTab);
94321 if( pVTab ) sqlite3VtabImportErrmsg(p, pVtab: pVTab->pVtab);
94322 if( rc ) goto abort_due_to_error;
94323 break;
94324}
94325#endif /* SQLITE_OMIT_VIRTUALTABLE */
94326
94327#ifndef SQLITE_OMIT_VIRTUALTABLE
94328/* Opcode: VCreate P1 P2 * * *
94329**
94330** P2 is a register that holds the name of a virtual table in database
94331** P1. Call the xCreate method for that table.
94332*/
94333case OP_VCreate: {
94334 Mem sMem; /* For storing the record being decoded */
94335 const char *zTab; /* Name of the virtual table */
94336
94337 memset(s: &sMem, c: 0, n: sizeof(sMem));
94338 sMem.db = db;
94339 /* Because P2 is always a static string, it is impossible for the
94340 ** sqlite3VdbeMemCopy() to fail */
94341 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
94342 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
94343 rc = sqlite3VdbeMemCopy(pTo: &sMem, pFrom: &aMem[pOp->p2]);
94344 assert( rc==SQLITE_OK );
94345 zTab = (const char*)sqlite3_value_text(pVal: &sMem);
94346 assert( zTab || db->mallocFailed );
94347 if( zTab ){
94348 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
94349 }
94350 sqlite3VdbeMemRelease(p: &sMem);
94351 if( rc ) goto abort_due_to_error;
94352 break;
94353}
94354#endif /* SQLITE_OMIT_VIRTUALTABLE */
94355
94356#ifndef SQLITE_OMIT_VIRTUALTABLE
94357/* Opcode: VDestroy P1 * * P4 *
94358**
94359** P4 is the name of a virtual table in database P1. Call the xDestroy method
94360** of that table.
94361*/
94362case OP_VDestroy: {
94363 db->nVDestroy++;
94364 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
94365 db->nVDestroy--;
94366 assert( p->errorAction==OE_Abort && p->usesStmtJournal );
94367 if( rc ) goto abort_due_to_error;
94368 break;
94369}
94370#endif /* SQLITE_OMIT_VIRTUALTABLE */
94371
94372#ifndef SQLITE_OMIT_VIRTUALTABLE
94373/* Opcode: VOpen P1 * * P4 *
94374**
94375** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
94376** P1 is a cursor number. This opcode opens a cursor to the virtual
94377** table and stores that cursor in P1.
94378*/
94379case OP_VOpen: {
94380 VdbeCursor *pCur;
94381 sqlite3_vtab_cursor *pVCur;
94382 sqlite3_vtab *pVtab;
94383 const sqlite3_module *pModule;
94384
94385 assert( p->bIsReader );
94386 pCur = 0;
94387 pVCur = 0;
94388 pVtab = pOp->p4.pVtab->pVtab;
94389 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
94390 rc = SQLITE_LOCKED;
94391 goto abort_due_to_error;
94392 }
94393 pModule = pVtab->pModule;
94394 rc = pModule->xOpen(pVtab, &pVCur);
94395 sqlite3VtabImportErrmsg(p, pVtab);
94396 if( rc ) goto abort_due_to_error;
94397
94398 /* Initialize sqlite3_vtab_cursor base class */
94399 pVCur->pVtab = pVtab;
94400
94401 /* Initialize vdbe cursor object */
94402 pCur = allocateCursor(p, iCur: pOp->p1, nField: 0, iDb: -1, CURTYPE_VTAB);
94403 if( pCur ){
94404 pCur->uc.pVCur = pVCur;
94405 pVtab->nRef++;
94406 }else{
94407 assert( db->mallocFailed );
94408 pModule->xClose(pVCur);
94409 goto no_mem;
94410 }
94411 break;
94412}
94413#endif /* SQLITE_OMIT_VIRTUALTABLE */
94414
94415#ifndef SQLITE_OMIT_VIRTUALTABLE
94416/* Opcode: VFilter P1 P2 P3 P4 *
94417** Synopsis: iplan=r[P3] zplan='P4'
94418**
94419** P1 is a cursor opened using VOpen. P2 is an address to jump to if
94420** the filtered result set is empty.
94421**
94422** P4 is either NULL or a string that was generated by the xBestIndex
94423** method of the module. The interpretation of the P4 string is left
94424** to the module implementation.
94425**
94426** This opcode invokes the xFilter method on the virtual table specified
94427** by P1. The integer query plan parameter to xFilter is stored in register
94428** P3. Register P3+1 stores the argc parameter to be passed to the
94429** xFilter method. Registers P3+2..P3+1+argc are the argc
94430** additional parameters which are passed to
94431** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
94432**
94433** A jump is made to P2 if the result set after filtering would be empty.
94434*/
94435case OP_VFilter: { /* jump */
94436 int nArg;
94437 int iQuery;
94438 const sqlite3_module *pModule;
94439 Mem *pQuery;
94440 Mem *pArgc;
94441 sqlite3_vtab_cursor *pVCur;
94442 sqlite3_vtab *pVtab;
94443 VdbeCursor *pCur;
94444 int res;
94445 int i;
94446 Mem **apArg;
94447
94448 pQuery = &aMem[pOp->p3];
94449 pArgc = &pQuery[1];
94450 pCur = p->apCsr[pOp->p1];
94451 assert( memIsValid(pQuery) );
94452 REGISTER_TRACE(pOp->p3, pQuery);
94453 assert( pCur!=0 );
94454 assert( pCur->eCurType==CURTYPE_VTAB );
94455 pVCur = pCur->uc.pVCur;
94456 pVtab = pVCur->pVtab;
94457 pModule = pVtab->pModule;
94458
94459 /* Grab the index number and argc parameters */
94460 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
94461 nArg = (int)pArgc->u.i;
94462 iQuery = (int)pQuery->u.i;
94463
94464 /* Invoke the xFilter method */
94465 apArg = p->apArg;
94466 for(i = 0; i<nArg; i++){
94467 apArg[i] = &pArgc[i+1];
94468 }
94469 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
94470 sqlite3VtabImportErrmsg(p, pVtab);
94471 if( rc ) goto abort_due_to_error;
94472 res = pModule->xEof(pVCur);
94473 pCur->nullRow = 0;
94474 VdbeBranchTaken(res!=0,2);
94475 if( res ) goto jump_to_p2;
94476 break;
94477}
94478#endif /* SQLITE_OMIT_VIRTUALTABLE */
94479
94480#ifndef SQLITE_OMIT_VIRTUALTABLE
94481/* Opcode: VColumn P1 P2 P3 * P5
94482** Synopsis: r[P3]=vcolumn(P2)
94483**
94484** Store in register P3 the value of the P2-th column of
94485** the current row of the virtual-table of cursor P1.
94486**
94487** If the VColumn opcode is being used to fetch the value of
94488** an unchanging column during an UPDATE operation, then the P5
94489** value is OPFLAG_NOCHNG. This will cause the sqlite3_vtab_nochange()
94490** function to return true inside the xColumn method of the virtual
94491** table implementation. The P5 column might also contain other
94492** bits (OPFLAG_LENGTHARG or OPFLAG_TYPEOFARG) but those bits are
94493** unused by OP_VColumn.
94494*/
94495case OP_VColumn: {
94496 sqlite3_vtab *pVtab;
94497 const sqlite3_module *pModule;
94498 Mem *pDest;
94499 sqlite3_context sContext;
94500
94501 VdbeCursor *pCur = p->apCsr[pOp->p1];
94502 assert( pCur!=0 );
94503 assert( pCur->eCurType==CURTYPE_VTAB );
94504 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
94505 pDest = &aMem[pOp->p3];
94506 memAboutToChange(p, pDest);
94507 if( pCur->nullRow ){
94508 sqlite3VdbeMemSetNull(pMem: pDest);
94509 break;
94510 }
94511 pVtab = pCur->uc.pVCur->pVtab;
94512 pModule = pVtab->pModule;
94513 assert( pModule->xColumn );
94514 memset(s: &sContext, c: 0, n: sizeof(sContext));
94515 sContext.pOut = pDest;
94516 assert( pOp->p5==OPFLAG_NOCHNG || pOp->p5==0 );
94517 if( pOp->p5 & OPFLAG_NOCHNG ){
94518 sqlite3VdbeMemSetNull(pMem: pDest);
94519 pDest->flags = MEM_Null|MEM_Zero;
94520 pDest->u.nZero = 0;
94521 }else{
94522 MemSetTypeFlag(pDest, MEM_Null);
94523 }
94524 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
94525 sqlite3VtabImportErrmsg(p, pVtab);
94526 if( sContext.isError>0 ){
94527 sqlite3VdbeError(p, zFormat: "%s", sqlite3_value_text(pVal: pDest));
94528 rc = sContext.isError;
94529 }
94530 sqlite3VdbeChangeEncoding(pMem: pDest, desiredEnc: encoding);
94531 REGISTER_TRACE(pOp->p3, pDest);
94532 UPDATE_MAX_BLOBSIZE(pDest);
94533
94534 if( sqlite3VdbeMemTooBig(p: pDest) ){
94535 goto too_big;
94536 }
94537 if( rc ) goto abort_due_to_error;
94538 break;
94539}
94540#endif /* SQLITE_OMIT_VIRTUALTABLE */
94541
94542#ifndef SQLITE_OMIT_VIRTUALTABLE
94543/* Opcode: VNext P1 P2 * * *
94544**
94545** Advance virtual table P1 to the next row in its result set and
94546** jump to instruction P2. Or, if the virtual table has reached
94547** the end of its result set, then fall through to the next instruction.
94548*/
94549case OP_VNext: { /* jump */
94550 sqlite3_vtab *pVtab;
94551 const sqlite3_module *pModule;
94552 int res;
94553 VdbeCursor *pCur;
94554
94555 pCur = p->apCsr[pOp->p1];
94556 assert( pCur!=0 );
94557 assert( pCur->eCurType==CURTYPE_VTAB );
94558 if( pCur->nullRow ){
94559 break;
94560 }
94561 pVtab = pCur->uc.pVCur->pVtab;
94562 pModule = pVtab->pModule;
94563 assert( pModule->xNext );
94564
94565 /* Invoke the xNext() method of the module. There is no way for the
94566 ** underlying implementation to return an error if one occurs during
94567 ** xNext(). Instead, if an error occurs, true is returned (indicating that
94568 ** data is available) and the error code returned when xColumn or
94569 ** some other method is next invoked on the save virtual table cursor.
94570 */
94571 rc = pModule->xNext(pCur->uc.pVCur);
94572 sqlite3VtabImportErrmsg(p, pVtab);
94573 if( rc ) goto abort_due_to_error;
94574 res = pModule->xEof(pCur->uc.pVCur);
94575 VdbeBranchTaken(!res,2);
94576 if( !res ){
94577 /* If there is data, jump to P2 */
94578 goto jump_to_p2_and_check_for_interrupt;
94579 }
94580 goto check_for_interrupt;
94581}
94582#endif /* SQLITE_OMIT_VIRTUALTABLE */
94583
94584#ifndef SQLITE_OMIT_VIRTUALTABLE
94585/* Opcode: VRename P1 * * P4 *
94586**
94587** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
94588** This opcode invokes the corresponding xRename method. The value
94589** in register P1 is passed as the zName argument to the xRename method.
94590*/
94591case OP_VRename: {
94592 sqlite3_vtab *pVtab;
94593 Mem *pName;
94594 int isLegacy;
94595
94596 isLegacy = (db->flags & SQLITE_LegacyAlter);
94597 db->flags |= SQLITE_LegacyAlter;
94598 pVtab = pOp->p4.pVtab->pVtab;
94599 pName = &aMem[pOp->p1];
94600 assert( pVtab->pModule->xRename );
94601 assert( memIsValid(pName) );
94602 assert( p->readOnly==0 );
94603 REGISTER_TRACE(pOp->p1, pName);
94604 assert( pName->flags & MEM_Str );
94605 testcase( pName->enc==SQLITE_UTF8 );
94606 testcase( pName->enc==SQLITE_UTF16BE );
94607 testcase( pName->enc==SQLITE_UTF16LE );
94608 rc = sqlite3VdbeChangeEncoding(pMem: pName, SQLITE_UTF8);
94609 if( rc ) goto abort_due_to_error;
94610 rc = pVtab->pModule->xRename(pVtab, pName->z);
94611 if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter;
94612 sqlite3VtabImportErrmsg(p, pVtab);
94613 p->expired = 0;
94614 if( rc ) goto abort_due_to_error;
94615 break;
94616}
94617#endif
94618
94619#ifndef SQLITE_OMIT_VIRTUALTABLE
94620/* Opcode: VUpdate P1 P2 P3 P4 P5
94621** Synopsis: data=r[P3@P2]
94622**
94623** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
94624** This opcode invokes the corresponding xUpdate method. P2 values
94625** are contiguous memory cells starting at P3 to pass to the xUpdate
94626** invocation. The value in register (P3+P2-1) corresponds to the
94627** p2th element of the argv array passed to xUpdate.
94628**
94629** The xUpdate method will do a DELETE or an INSERT or both.
94630** The argv[0] element (which corresponds to memory cell P3)
94631** is the rowid of a row to delete. If argv[0] is NULL then no
94632** deletion occurs. The argv[1] element is the rowid of the new
94633** row. This can be NULL to have the virtual table select the new
94634** rowid for itself. The subsequent elements in the array are
94635** the values of columns in the new row.
94636**
94637** If P2==1 then no insert is performed. argv[0] is the rowid of
94638** a row to delete.
94639**
94640** P1 is a boolean flag. If it is set to true and the xUpdate call
94641** is successful, then the value returned by sqlite3_last_insert_rowid()
94642** is set to the value of the rowid for the row just inserted.
94643**
94644** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
94645** apply in the case of a constraint failure on an insert or update.
94646*/
94647case OP_VUpdate: {
94648 sqlite3_vtab *pVtab;
94649 const sqlite3_module *pModule;
94650 int nArg;
94651 int i;
94652 sqlite_int64 rowid = 0;
94653 Mem **apArg;
94654 Mem *pX;
94655
94656 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
94657 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
94658 );
94659 assert( p->readOnly==0 );
94660 if( db->mallocFailed ) goto no_mem;
94661 sqlite3VdbeIncrWriteCounter(p, 0);
94662 pVtab = pOp->p4.pVtab->pVtab;
94663 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
94664 rc = SQLITE_LOCKED;
94665 goto abort_due_to_error;
94666 }
94667 pModule = pVtab->pModule;
94668 nArg = pOp->p2;
94669 assert( pOp->p4type==P4_VTAB );
94670 if( ALWAYS(pModule->xUpdate) ){
94671 u8 vtabOnConflict = db->vtabOnConflict;
94672 apArg = p->apArg;
94673 pX = &aMem[pOp->p3];
94674 for(i=0; i<nArg; i++){
94675 assert( memIsValid(pX) );
94676 memAboutToChange(p, pX);
94677 apArg[i] = pX;
94678 pX++;
94679 }
94680 db->vtabOnConflict = pOp->p5;
94681 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
94682 db->vtabOnConflict = vtabOnConflict;
94683 sqlite3VtabImportErrmsg(p, pVtab);
94684 if( rc==SQLITE_OK && pOp->p1 ){
94685 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
94686 db->lastRowid = rowid;
94687 }
94688 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
94689 if( pOp->p5==OE_Ignore ){
94690 rc = SQLITE_OK;
94691 }else{
94692 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
94693 }
94694 }else{
94695 p->nChange++;
94696 }
94697 if( rc ) goto abort_due_to_error;
94698 }
94699 break;
94700}
94701#endif /* SQLITE_OMIT_VIRTUALTABLE */
94702
94703#ifndef SQLITE_OMIT_PAGER_PRAGMAS
94704/* Opcode: Pagecount P1 P2 * * *
94705**
94706** Write the current number of pages in database P1 to memory cell P2.
94707*/
94708case OP_Pagecount: { /* out2 */
94709 pOut = out2Prerelease(p, pOp);
94710 pOut->u.i = sqlite3BtreeLastPage(p: db->aDb[pOp->p1].pBt);
94711 break;
94712}
94713#endif
94714
94715
94716#ifndef SQLITE_OMIT_PAGER_PRAGMAS
94717/* Opcode: MaxPgcnt P1 P2 P3 * *
94718**
94719** Try to set the maximum page count for database P1 to the value in P3.
94720** Do not let the maximum page count fall below the current page count and
94721** do not change the maximum page count value if P3==0.
94722**
94723** Store the maximum page count after the change in register P2.
94724*/
94725case OP_MaxPgcnt: { /* out2 */
94726 unsigned int newMax;
94727 Btree *pBt;
94728
94729 pOut = out2Prerelease(p, pOp);
94730 pBt = db->aDb[pOp->p1].pBt;
94731 newMax = 0;
94732 if( pOp->p3 ){
94733 newMax = sqlite3BtreeLastPage(p: pBt);
94734 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
94735 }
94736 pOut->u.i = sqlite3BtreeMaxPageCount(p: pBt, mxPage: newMax);
94737 break;
94738}
94739#endif
94740
94741/* Opcode: Function P1 P2 P3 P4 *
94742** Synopsis: r[P3]=func(r[P2@NP])
94743**
94744** Invoke a user function (P4 is a pointer to an sqlite3_context object that
94745** contains a pointer to the function to be run) with arguments taken
94746** from register P2 and successors. The number of arguments is in
94747** the sqlite3_context object that P4 points to.
94748** The result of the function is stored
94749** in register P3. Register P3 must not be one of the function inputs.
94750**
94751** P1 is a 32-bit bitmask indicating whether or not each argument to the
94752** function was determined to be constant at compile time. If the first
94753** argument was constant then bit 0 of P1 is set. This is used to determine
94754** whether meta data associated with a user function argument using the
94755** sqlite3_set_auxdata() API may be safely retained until the next
94756** invocation of this opcode.
94757**
94758** See also: AggStep, AggFinal, PureFunc
94759*/
94760/* Opcode: PureFunc P1 P2 P3 P4 *
94761** Synopsis: r[P3]=func(r[P2@NP])
94762**
94763** Invoke a user function (P4 is a pointer to an sqlite3_context object that
94764** contains a pointer to the function to be run) with arguments taken
94765** from register P2 and successors. The number of arguments is in
94766** the sqlite3_context object that P4 points to.
94767** The result of the function is stored
94768** in register P3. Register P3 must not be one of the function inputs.
94769**
94770** P1 is a 32-bit bitmask indicating whether or not each argument to the
94771** function was determined to be constant at compile time. If the first
94772** argument was constant then bit 0 of P1 is set. This is used to determine
94773** whether meta data associated with a user function argument using the
94774** sqlite3_set_auxdata() API may be safely retained until the next
94775** invocation of this opcode.
94776**
94777** This opcode works exactly like OP_Function. The only difference is in
94778** its name. This opcode is used in places where the function must be
94779** purely non-deterministic. Some built-in date/time functions can be
94780** either determinitic of non-deterministic, depending on their arguments.
94781** When those function are used in a non-deterministic way, they will check
94782** to see if they were called using OP_PureFunc instead of OP_Function, and
94783** if they were, they throw an error.
94784**
94785** See also: AggStep, AggFinal, Function
94786*/
94787case OP_PureFunc: /* group */
94788case OP_Function: { /* group */
94789 int i;
94790 sqlite3_context *pCtx;
94791
94792 assert( pOp->p4type==P4_FUNCCTX );
94793 pCtx = pOp->p4.pCtx;
94794
94795 /* If this function is inside of a trigger, the register array in aMem[]
94796 ** might change from one evaluation to the next. The next block of code
94797 ** checks to see if the register array has changed, and if so it
94798 ** reinitializes the relavant parts of the sqlite3_context object */
94799 pOut = &aMem[pOp->p3];
94800 if( pCtx->pOut != pOut ){
94801 pCtx->pVdbe = p;
94802 pCtx->pOut = pOut;
94803 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
94804 }
94805 assert( pCtx->pVdbe==p );
94806
94807 memAboutToChange(p, pOut);
94808#ifdef SQLITE_DEBUG
94809 for(i=0; i<pCtx->argc; i++){
94810 assert( memIsValid(pCtx->argv[i]) );
94811 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
94812 }
94813#endif
94814 MemSetTypeFlag(pOut, MEM_Null);
94815 assert( pCtx->isError==0 );
94816 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
94817
94818 /* If the function returned an error, throw an exception */
94819 if( pCtx->isError ){
94820 if( pCtx->isError>0 ){
94821 sqlite3VdbeError(p, zFormat: "%s", sqlite3_value_text(pVal: pOut));
94822 rc = pCtx->isError;
94823 }
94824 sqlite3VdbeDeleteAuxData(db, pp: &p->pAuxData, iOp: pCtx->iOp, mask: pOp->p1);
94825 pCtx->isError = 0;
94826 if( rc ) goto abort_due_to_error;
94827 }
94828
94829 /* Copy the result of the function into register P3 */
94830 if( pOut->flags & (MEM_Str|MEM_Blob) ){
94831 sqlite3VdbeChangeEncoding(pMem: pOut, desiredEnc: encoding);
94832 if( sqlite3VdbeMemTooBig(p: pOut) ) goto too_big;
94833 }
94834
94835 REGISTER_TRACE(pOp->p3, pOut);
94836 UPDATE_MAX_BLOBSIZE(pOut);
94837 break;
94838}
94839
94840/* Opcode: Trace P1 P2 * P4 *
94841**
94842** Write P4 on the statement trace output if statement tracing is
94843** enabled.
94844**
94845** Operand P1 must be 0x7fffffff and P2 must positive.
94846*/
94847/* Opcode: Init P1 P2 P3 P4 *
94848** Synopsis: Start at P2
94849**
94850** Programs contain a single instance of this opcode as the very first
94851** opcode.
94852**
94853** If tracing is enabled (by the sqlite3_trace()) interface, then
94854** the UTF-8 string contained in P4 is emitted on the trace callback.
94855** Or if P4 is blank, use the string returned by sqlite3_sql().
94856**
94857** If P2 is not zero, jump to instruction P2.
94858**
94859** Increment the value of P1 so that OP_Once opcodes will jump the
94860** first time they are evaluated for this run.
94861**
94862** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
94863** error is encountered.
94864*/
94865case OP_Trace:
94866case OP_Init: { /* jump */
94867 int i;
94868#ifndef SQLITE_OMIT_TRACE
94869 char *zTrace;
94870#endif
94871
94872 /* If the P4 argument is not NULL, then it must be an SQL comment string.
94873 ** The "--" string is broken up to prevent false-positives with srcck1.c.
94874 **
94875 ** This assert() provides evidence for:
94876 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
94877 ** would have been returned by the legacy sqlite3_trace() interface by
94878 ** using the X argument when X begins with "--" and invoking
94879 ** sqlite3_expanded_sql(P) otherwise.
94880 */
94881 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
94882
94883 /* OP_Init is always instruction 0 */
94884 assert( pOp==p->aOp || pOp->opcode==OP_Trace );
94885
94886#ifndef SQLITE_OMIT_TRACE
94887 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
94888 && !p->doingRerun
94889 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
94890 ){
94891#ifndef SQLITE_OMIT_DEPRECATED
94892 if( db->mTrace & SQLITE_TRACE_LEGACY ){
94893 char *z = sqlite3VdbeExpandSql(p, zRawSql: zTrace);
94894 db->trace.xLegacy(db->pTraceArg, z);
94895 sqlite3_free(p: z);
94896 }else
94897#endif
94898 if( db->nVdbeExec>1 ){
94899 char *z = sqlite3MPrintf(db, zFormat: "-- %s", zTrace);
94900 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
94901 sqlite3DbFree(db, p: z);
94902 }else{
94903 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
94904 }
94905 }
94906#ifdef SQLITE_USE_FCNTL_TRACE
94907 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
94908 if( zTrace ){
94909 int j;
94910 for(j=0; j<db->nDb; j++){
94911 if( DbMaskTest(p->btreeMask, j)==0 ) continue;
94912 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
94913 }
94914 }
94915#endif /* SQLITE_USE_FCNTL_TRACE */
94916#ifdef SQLITE_DEBUG
94917 if( (db->flags & SQLITE_SqlTrace)!=0
94918 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
94919 ){
94920 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
94921 }
94922#endif /* SQLITE_DEBUG */
94923#endif /* SQLITE_OMIT_TRACE */
94924 assert( pOp->p2>0 );
94925 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
94926 if( pOp->opcode==OP_Trace ) break;
94927 for(i=1; i<p->nOp; i++){
94928 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
94929 }
94930 pOp->p1 = 0;
94931 }
94932 pOp->p1++;
94933 p->aCounter[SQLITE_STMTSTATUS_RUN]++;
94934 goto jump_to_p2;
94935}
94936
94937#ifdef SQLITE_ENABLE_CURSOR_HINTS
94938/* Opcode: CursorHint P1 * * P4 *
94939**
94940** Provide a hint to cursor P1 that it only needs to return rows that
94941** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer
94942** to values currently held in registers. TK_COLUMN terms in the P4
94943** expression refer to columns in the b-tree to which cursor P1 is pointing.
94944*/
94945case OP_CursorHint: {
94946 VdbeCursor *pC;
94947
94948 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94949 assert( pOp->p4type==P4_EXPR );
94950 pC = p->apCsr[pOp->p1];
94951 if( pC ){
94952 assert( pC->eCurType==CURTYPE_BTREE );
94953 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
94954 pOp->p4.pExpr, aMem);
94955 }
94956 break;
94957}
94958#endif /* SQLITE_ENABLE_CURSOR_HINTS */
94959
94960#ifdef SQLITE_DEBUG
94961/* Opcode: Abortable * * * * *
94962**
94963** Verify that an Abort can happen. Assert if an Abort at this point
94964** might cause database corruption. This opcode only appears in debugging
94965** builds.
94966**
94967** An Abort is safe if either there have been no writes, or if there is
94968** an active statement journal.
94969*/
94970case OP_Abortable: {
94971 sqlite3VdbeAssertAbortable(p);
94972 break;
94973}
94974#endif
94975
94976#ifdef SQLITE_DEBUG
94977/* Opcode: ReleaseReg P1 P2 P3 * P5
94978** Synopsis: release r[P1@P2] mask P3
94979**
94980** Release registers from service. Any content that was in the
94981** the registers is unreliable after this opcode completes.
94982**
94983** The registers released will be the P2 registers starting at P1,
94984** except if bit ii of P3 set, then do not release register P1+ii.
94985** In other words, P3 is a mask of registers to preserve.
94986**
94987** Releasing a register clears the Mem.pScopyFrom pointer. That means
94988** that if the content of the released register was set using OP_SCopy,
94989** a change to the value of the source register for the OP_SCopy will no longer
94990** generate an assertion fault in sqlite3VdbeMemAboutToChange().
94991**
94992** If P5 is set, then all released registers have their type set
94993** to MEM_Undefined so that any subsequent attempt to read the released
94994** register (before it is reinitialized) will generate an assertion fault.
94995**
94996** P5 ought to be set on every call to this opcode.
94997** However, there are places in the code generator will release registers
94998** before their are used, under the (valid) assumption that the registers
94999** will not be reallocated for some other purpose before they are used and
95000** hence are safe to release.
95001**
95002** This opcode is only available in testing and debugging builds. It is
95003** not generated for release builds. The purpose of this opcode is to help
95004** validate the generated bytecode. This opcode does not actually contribute
95005** to computing an answer.
95006*/
95007case OP_ReleaseReg: {
95008 Mem *pMem;
95009 int i;
95010 u32 constMask;
95011 assert( pOp->p1>0 );
95012 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
95013 pMem = &aMem[pOp->p1];
95014 constMask = pOp->p3;
95015 for(i=0; i<pOp->p2; i++, pMem++){
95016 if( i>=32 || (constMask & MASKBIT32(i))==0 ){
95017 pMem->pScopyFrom = 0;
95018 if( i<32 && pOp->p5 ) MemSetTypeFlag(pMem, MEM_Undefined);
95019 }
95020 }
95021 break;
95022}
95023#endif
95024
95025/* Opcode: Noop * * * * *
95026**
95027** Do nothing. This instruction is often useful as a jump
95028** destination.
95029*/
95030/*
95031** The magic Explain opcode are only inserted when explain==2 (which
95032** is to say when the EXPLAIN QUERY PLAN syntax is used.)
95033** This opcode records information from the optimizer. It is the
95034** the same as a no-op. This opcodesnever appears in a real VM program.
95035*/
95036default: { /* This is really OP_Noop, OP_Explain */
95037 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
95038
95039 break;
95040}
95041
95042/*****************************************************************************
95043** The cases of the switch statement above this line should all be indented
95044** by 6 spaces. But the left-most 6 spaces have been removed to improve the
95045** readability. From this point on down, the normal indentation rules are
95046** restored.
95047*****************************************************************************/
95048 }
95049
95050#ifdef VDBE_PROFILE
95051 {
95052 u64 endTime = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
95053 if( endTime>start ) pOrigOp->cycles += endTime - start;
95054 pOrigOp->cnt++;
95055 }
95056#endif
95057
95058 /* The following code adds nothing to the actual functionality
95059 ** of the program. It is only here for testing and debugging.
95060 ** On the other hand, it does burn CPU cycles every time through
95061 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
95062 */
95063#ifndef NDEBUG
95064 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
95065
95066#ifdef SQLITE_DEBUG
95067 if( db->flags & SQLITE_VdbeTrace ){
95068 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
95069 if( rc!=0 ) printf("rc=%d\n",rc);
95070 if( opProperty & (OPFLG_OUT2) ){
95071 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
95072 }
95073 if( opProperty & OPFLG_OUT3 ){
95074 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
95075 }
95076 if( opProperty==0xff ){
95077 /* Never happens. This code exists to avoid a harmless linkage
95078 ** warning aboud sqlite3VdbeRegisterDump() being defined but not
95079 ** used. */
95080 sqlite3VdbeRegisterDump(p);
95081 }
95082 }
95083#endif /* SQLITE_DEBUG */
95084#endif /* NDEBUG */
95085 } /* The end of the for(;;) loop the loops through opcodes */
95086
95087 /* If we reach this point, it means that execution is finished with
95088 ** an error of some kind.
95089 */
95090abort_due_to_error:
95091 if( db->mallocFailed ){
95092 rc = SQLITE_NOMEM_BKPT;
95093 }else if( rc==SQLITE_IOERR_CORRUPTFS ){
95094 rc = SQLITE_CORRUPT_BKPT;
95095 }
95096 assert( rc );
95097#ifdef SQLITE_DEBUG
95098 if( db->flags & SQLITE_VdbeTrace ){
95099 const char *zTrace = p->zSql;
95100 if( zTrace==0 ){
95101 if( aOp[0].opcode==OP_Trace ){
95102 zTrace = aOp[0].p4.z;
95103 }
95104 if( zTrace==0 ) zTrace = "???";
95105 }
95106 printf("ABORT-due-to-error (rc=%d): %s\n", rc, zTrace);
95107 }
95108#endif
95109 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
95110 sqlite3VdbeError(p, zFormat: "%s", sqlite3ErrStr(rc));
95111 }
95112 p->rc = rc;
95113 sqlite3SystemError(db, rc);
95114 testcase( sqlite3GlobalConfig.xLog!=0 );
95115 sqlite3_log(iErrCode: rc, zFormat: "statement aborts at %d: [%s] %s",
95116 (int)(pOp - aOp), p->zSql, p->zErrMsg);
95117 sqlite3VdbeHalt(p);
95118 if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db);
95119 if( rc==SQLITE_CORRUPT && db->autoCommit==0 ){
95120 db->flags |= SQLITE_CorruptRdOnly;
95121 }
95122 rc = SQLITE_ERROR;
95123 if( resetSchemaOnFault>0 ){
95124 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
95125 }
95126
95127 /* This is the only way out of this procedure. We have to
95128 ** release the mutexes on btrees that were acquired at the
95129 ** top. */
95130vdbe_return:
95131#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
95132 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
95133 nProgressLimit += db->nProgressOps;
95134 if( db->xProgress(db->pProgressArg) ){
95135 nProgressLimit = LARGEST_UINT64;
95136 rc = SQLITE_INTERRUPT;
95137 goto abort_due_to_error;
95138 }
95139 }
95140#endif
95141 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
95142 sqlite3VdbeLeave(p);
95143 assert( rc!=SQLITE_OK || nExtraDelete==0
95144 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
95145 );
95146 return rc;
95147
95148 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
95149 ** is encountered.
95150 */
95151too_big:
95152 sqlite3VdbeError(p, zFormat: "string or blob too big");
95153 rc = SQLITE_TOOBIG;
95154 goto abort_due_to_error;
95155
95156 /* Jump to here if a malloc() fails.
95157 */
95158no_mem:
95159 sqlite3OomFault(db);
95160 sqlite3VdbeError(p, zFormat: "out of memory");
95161 rc = SQLITE_NOMEM_BKPT;
95162 goto abort_due_to_error;
95163
95164 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
95165 ** flag.
95166 */
95167abort_due_to_interrupt:
95168 assert( AtomicLoad(&db->u1.isInterrupted) );
95169 rc = SQLITE_INTERRUPT;
95170 goto abort_due_to_error;
95171}
95172
95173
95174/************** End of vdbe.c ************************************************/
95175/************** Begin file vdbeblob.c ****************************************/
95176/*
95177** 2007 May 1
95178**
95179** The author disclaims copyright to this source code. In place of
95180** a legal notice, here is a blessing:
95181**
95182** May you do good and not evil.
95183** May you find forgiveness for yourself and forgive others.
95184** May you share freely, never taking more than you give.
95185**
95186*************************************************************************
95187**
95188** This file contains code used to implement incremental BLOB I/O.
95189*/
95190
95191/* #include "sqliteInt.h" */
95192/* #include "vdbeInt.h" */
95193
95194#ifndef SQLITE_OMIT_INCRBLOB
95195
95196/*
95197** Valid sqlite3_blob* handles point to Incrblob structures.
95198*/
95199typedef struct Incrblob Incrblob;
95200struct Incrblob {
95201 int nByte; /* Size of open blob, in bytes */
95202 int iOffset; /* Byte offset of blob in cursor data */
95203 u16 iCol; /* Table column this handle is open on */
95204 BtCursor *pCsr; /* Cursor pointing at blob row */
95205 sqlite3_stmt *pStmt; /* Statement holding cursor open */
95206 sqlite3 *db; /* The associated database */
95207 char *zDb; /* Database name */
95208 Table *pTab; /* Table object */
95209};
95210
95211
95212/*
95213** This function is used by both blob_open() and blob_reopen(). It seeks
95214** the b-tree cursor associated with blob handle p to point to row iRow.
95215** If successful, SQLITE_OK is returned and subsequent calls to
95216** sqlite3_blob_read() or sqlite3_blob_write() access the specified row.
95217**
95218** If an error occurs, or if the specified row does not exist or does not
95219** contain a value of type TEXT or BLOB in the column nominated when the
95220** blob handle was opened, then an error code is returned and *pzErr may
95221** be set to point to a buffer containing an error message. It is the
95222** responsibility of the caller to free the error message buffer using
95223** sqlite3DbFree().
95224**
95225** If an error does occur, then the b-tree cursor is closed. All subsequent
95226** calls to sqlite3_blob_read(), blob_write() or blob_reopen() will
95227** immediately return SQLITE_ABORT.
95228*/
95229static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
95230 int rc; /* Error code */
95231 char *zErr = 0; /* Error message */
95232 Vdbe *v = (Vdbe *)p->pStmt;
95233
95234 /* Set the value of register r[1] in the SQL statement to integer iRow.
95235 ** This is done directly as a performance optimization
95236 */
95237 v->aMem[1].flags = MEM_Int;
95238 v->aMem[1].u.i = iRow;
95239
95240 /* If the statement has been run before (and is paused at the OP_ResultRow)
95241 ** then back it up to the point where it does the OP_NotExists. This could
95242 ** have been down with an extra OP_Goto, but simply setting the program
95243 ** counter is faster. */
95244 if( v->pc>4 ){
95245 v->pc = 4;
95246 assert( v->aOp[v->pc].opcode==OP_NotExists );
95247 rc = sqlite3VdbeExec(p: v);
95248 }else{
95249 rc = sqlite3_step(pStmt: p->pStmt);
95250 }
95251 if( rc==SQLITE_ROW ){
95252 VdbeCursor *pC = v->apCsr[0];
95253 u32 type;
95254 assert( pC!=0 );
95255 assert( pC->eCurType==CURTYPE_BTREE );
95256 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0;
95257 testcase( pC->nHdrParsed==p->iCol );
95258 testcase( pC->nHdrParsed==p->iCol+1 );
95259 if( type<12 ){
95260 zErr = sqlite3MPrintf(db: p->db, zFormat: "cannot open value of type %s",
95261 type==0?"null": type==7?"real": "integer"
95262 );
95263 rc = SQLITE_ERROR;
95264 sqlite3_finalize(pStmt: p->pStmt);
95265 p->pStmt = 0;
95266 }else{
95267 p->iOffset = pC->aType[p->iCol + pC->nField];
95268 p->nByte = sqlite3VdbeSerialTypeLen(serial_type: type);
95269 p->pCsr = pC->uc.pCursor;
95270 sqlite3BtreeIncrblobCursor(pCur: p->pCsr);
95271 }
95272 }
95273
95274 if( rc==SQLITE_ROW ){
95275 rc = SQLITE_OK;
95276 }else if( p->pStmt ){
95277 rc = sqlite3_finalize(pStmt: p->pStmt);
95278 p->pStmt = 0;
95279 if( rc==SQLITE_OK ){
95280 zErr = sqlite3MPrintf(db: p->db, zFormat: "no such rowid: %lld", iRow);
95281 rc = SQLITE_ERROR;
95282 }else{
95283 zErr = sqlite3MPrintf(db: p->db, zFormat: "%s", sqlite3_errmsg(p->db));
95284 }
95285 }
95286
95287 assert( rc!=SQLITE_OK || zErr==0 );
95288 assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE );
95289
95290 *pzErr = zErr;
95291 return rc;
95292}
95293
95294/*
95295** Open a blob handle.
95296*/
95297SQLITE_API int sqlite3_blob_open(
95298 sqlite3* db, /* The database connection */
95299 const char *zDb, /* The attached database containing the blob */
95300 const char *zTable, /* The table containing the blob */
95301 const char *zColumn, /* The column containing the blob */
95302 sqlite_int64 iRow, /* The row containing the glob */
95303 int wrFlag, /* True -> read/write access, false -> read-only */
95304 sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */
95305){
95306 int nAttempt = 0;
95307 int iCol; /* Index of zColumn in row-record */
95308 int rc = SQLITE_OK;
95309 char *zErr = 0;
95310 Table *pTab;
95311 Incrblob *pBlob = 0;
95312 Parse sParse;
95313
95314#ifdef SQLITE_ENABLE_API_ARMOR
95315 if( ppBlob==0 ){
95316 return SQLITE_MISUSE_BKPT;
95317 }
95318#endif
95319 *ppBlob = 0;
95320#ifdef SQLITE_ENABLE_API_ARMOR
95321 if( !sqlite3SafetyCheckOk(db) || zTable==0 ){
95322 return SQLITE_MISUSE_BKPT;
95323 }
95324#endif
95325 wrFlag = !!wrFlag; /* wrFlag = (wrFlag ? 1 : 0); */
95326
95327 sqlite3_mutex_enter(p: db->mutex);
95328
95329 pBlob = (Incrblob *)sqlite3DbMallocZero(db, n: sizeof(Incrblob));
95330 do {
95331 memset(s: &sParse, c: 0, n: sizeof(Parse));
95332 if( !pBlob ) goto blob_open_out;
95333 sParse.db = db;
95334 sqlite3DbFree(db, p: zErr);
95335 zErr = 0;
95336
95337 sqlite3BtreeEnterAll(db);
95338 pTab = sqlite3LocateTable(&sParse, flags: 0, zTable, zDb);
95339 if( pTab && IsVirtual(pTab) ){
95340 pTab = 0;
95341 sqlite3ErrorMsg(pParse: &sParse, zFormat: "cannot open virtual table: %s", zTable);
95342 }
95343 if( pTab && !HasRowid(pTab) ){
95344 pTab = 0;
95345 sqlite3ErrorMsg(pParse: &sParse, zFormat: "cannot open table without rowid: %s", zTable);
95346 }
95347#ifndef SQLITE_OMIT_VIEW
95348 if( pTab && IsView(pTab) ){
95349 pTab = 0;
95350 sqlite3ErrorMsg(pParse: &sParse, zFormat: "cannot open view: %s", zTable);
95351 }
95352#endif
95353 if( !pTab ){
95354 if( sParse.zErrMsg ){
95355 sqlite3DbFree(db, p: zErr);
95356 zErr = sParse.zErrMsg;
95357 sParse.zErrMsg = 0;
95358 }
95359 rc = SQLITE_ERROR;
95360 sqlite3BtreeLeaveAll(db);
95361 goto blob_open_out;
95362 }
95363 pBlob->pTab = pTab;
95364 pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zDbSName;
95365
95366 /* Now search pTab for the exact column. */
95367 for(iCol=0; iCol<pTab->nCol; iCol++) {
95368 if( sqlite3StrICmp(zLeft: pTab->aCol[iCol].zCnName, zRight: zColumn)==0 ){
95369 break;
95370 }
95371 }
95372 if( iCol==pTab->nCol ){
95373 sqlite3DbFree(db, p: zErr);
95374 zErr = sqlite3MPrintf(db, zFormat: "no such column: \"%s\"", zColumn);
95375 rc = SQLITE_ERROR;
95376 sqlite3BtreeLeaveAll(db);
95377 goto blob_open_out;
95378 }
95379
95380 /* If the value is being opened for writing, check that the
95381 ** column is not indexed, and that it is not part of a foreign key.
95382 */
95383 if( wrFlag ){
95384 const char *zFault = 0;
95385 Index *pIdx;
95386#ifndef SQLITE_OMIT_FOREIGN_KEY
95387 if( db->flags&SQLITE_ForeignKeys ){
95388 /* Check that the column is not part of an FK child key definition. It
95389 ** is not necessary to check if it is part of a parent key, as parent
95390 ** key columns must be indexed. The check below will pick up this
95391 ** case. */
95392 FKey *pFKey;
95393 assert( IsOrdinaryTable(pTab) );
95394 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
95395 int j;
95396 for(j=0; j<pFKey->nCol; j++){
95397 if( pFKey->aCol[j].iFrom==iCol ){
95398 zFault = "foreign key";
95399 }
95400 }
95401 }
95402 }
95403#endif
95404 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
95405 int j;
95406 for(j=0; j<pIdx->nKeyCol; j++){
95407 /* FIXME: Be smarter about indexes that use expressions */
95408 if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR ){
95409 zFault = "indexed";
95410 }
95411 }
95412 }
95413 if( zFault ){
95414 sqlite3DbFree(db, p: zErr);
95415 zErr = sqlite3MPrintf(db, zFormat: "cannot open %s column for writing", zFault);
95416 rc = SQLITE_ERROR;
95417 sqlite3BtreeLeaveAll(db);
95418 goto blob_open_out;
95419 }
95420 }
95421
95422 pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(pParse: &sParse);
95423 assert( pBlob->pStmt || db->mallocFailed );
95424 if( pBlob->pStmt ){
95425
95426 /* This VDBE program seeks a btree cursor to the identified
95427 ** db/table/row entry. The reason for using a vdbe program instead
95428 ** of writing code to use the b-tree layer directly is that the
95429 ** vdbe program will take advantage of the various transaction,
95430 ** locking and error handling infrastructure built into the vdbe.
95431 **
95432 ** After seeking the cursor, the vdbe executes an OP_ResultRow.
95433 ** Code external to the Vdbe then "borrows" the b-tree cursor and
95434 ** uses it to implement the blob_read(), blob_write() and
95435 ** blob_bytes() functions.
95436 **
95437 ** The sqlite3_blob_close() function finalizes the vdbe program,
95438 ** which closes the b-tree cursor and (possibly) commits the
95439 ** transaction.
95440 */
95441 static const int iLn = VDBE_OFFSET_LINENO(2);
95442 static const VdbeOpList openBlob[] = {
95443 {OP_TableLock, 0, 0, 0}, /* 0: Acquire a read or write lock */
95444 {OP_OpenRead, 0, 0, 0}, /* 1: Open a cursor */
95445 /* blobSeekToRow() will initialize r[1] to the desired rowid */
95446 {OP_NotExists, 0, 5, 1}, /* 2: Seek the cursor to rowid=r[1] */
95447 {OP_Column, 0, 0, 1}, /* 3 */
95448 {OP_ResultRow, 1, 0, 0}, /* 4 */
95449 {OP_Halt, 0, 0, 0}, /* 5 */
95450 };
95451 Vdbe *v = (Vdbe *)pBlob->pStmt;
95452 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
95453 VdbeOp *aOp;
95454
95455 sqlite3VdbeAddOp4Int(p: v, OP_Transaction, p1: iDb, p2: wrFlag,
95456 p3: pTab->pSchema->schema_cookie,
95457 p4: pTab->pSchema->iGeneration);
95458 sqlite3VdbeChangeP5(p: v, p5: 1);
95459 assert( sqlite3VdbeCurrentAddr(v)==2 || db->mallocFailed );
95460 aOp = sqlite3VdbeAddOpList(p: v, ArraySize(openBlob), aOp: openBlob, iLineno: iLn);
95461
95462 /* Make sure a mutex is held on the table to be accessed */
95463 sqlite3VdbeUsesBtree(p: v, i: iDb);
95464
95465 if( db->mallocFailed==0 ){
95466 assert( aOp!=0 );
95467 /* Configure the OP_TableLock instruction */
95468#ifdef SQLITE_OMIT_SHARED_CACHE
95469 aOp[0].opcode = OP_Noop;
95470#else
95471 aOp[0].p1 = iDb;
95472 aOp[0].p2 = pTab->tnum;
95473 aOp[0].p3 = wrFlag;
95474 sqlite3VdbeChangeP4(p: v, addr: 2, zP4: pTab->zName, P4_TRANSIENT);
95475 }
95476 if( db->mallocFailed==0 ){
95477#endif
95478
95479 /* Remove either the OP_OpenWrite or OpenRead. Set the P2
95480 ** parameter of the other to pTab->tnum. */
95481 if( wrFlag ) aOp[1].opcode = OP_OpenWrite;
95482 aOp[1].p2 = pTab->tnum;
95483 aOp[1].p3 = iDb;
95484
95485 /* Configure the number of columns. Configure the cursor to
95486 ** think that the table has one more column than it really
95487 ** does. An OP_Column to retrieve this imaginary column will
95488 ** always return an SQL NULL. This is useful because it means
95489 ** we can invoke OP_Column to fill in the vdbe cursors type
95490 ** and offset cache without causing any IO.
95491 */
95492 aOp[1].p4type = P4_INT32;
95493 aOp[1].p4.i = pTab->nCol+1;
95494 aOp[3].p2 = pTab->nCol;
95495
95496 sParse.nVar = 0;
95497 sParse.nMem = 1;
95498 sParse.nTab = 1;
95499 sqlite3VdbeMakeReady(p: v, pParse: &sParse);
95500 }
95501 }
95502
95503 pBlob->iCol = iCol;
95504 pBlob->db = db;
95505 sqlite3BtreeLeaveAll(db);
95506 if( db->mallocFailed ){
95507 goto blob_open_out;
95508 }
95509 rc = blobSeekToRow(p: pBlob, iRow, pzErr: &zErr);
95510 } while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA );
95511
95512blob_open_out:
95513 if( rc==SQLITE_OK && db->mallocFailed==0 ){
95514 *ppBlob = (sqlite3_blob *)pBlob;
95515 }else{
95516 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize(p: (Vdbe *)pBlob->pStmt);
95517 sqlite3DbFree(db, p: pBlob);
95518 }
95519 sqlite3ErrorWithMsg(db, err_code: rc, zFormat: (zErr ? "%s" : 0), zErr);
95520 sqlite3DbFree(db, p: zErr);
95521 sqlite3ParserReset(&sParse);
95522 rc = sqlite3ApiExit(db, rc);
95523 sqlite3_mutex_leave(p: db->mutex);
95524 return rc;
95525}
95526
95527/*
95528** Close a blob handle that was previously created using
95529** sqlite3_blob_open().
95530*/
95531SQLITE_API int sqlite3_blob_close(sqlite3_blob *pBlob){
95532 Incrblob *p = (Incrblob *)pBlob;
95533 int rc;
95534 sqlite3 *db;
95535
95536 if( p ){
95537 sqlite3_stmt *pStmt = p->pStmt;
95538 db = p->db;
95539 sqlite3_mutex_enter(p: db->mutex);
95540 sqlite3DbFree(db, p);
95541 sqlite3_mutex_leave(p: db->mutex);
95542 rc = sqlite3_finalize(pStmt);
95543 }else{
95544 rc = SQLITE_OK;
95545 }
95546 return rc;
95547}
95548
95549/*
95550** Perform a read or write operation on a blob
95551*/
95552static int blobReadWrite(
95553 sqlite3_blob *pBlob,
95554 void *z,
95555 int n,
95556 int iOffset,
95557 int (*xCall)(BtCursor*, u32, u32, void*)
95558){
95559 int rc;
95560 Incrblob *p = (Incrblob *)pBlob;
95561 Vdbe *v;
95562 sqlite3 *db;
95563
95564 if( p==0 ) return SQLITE_MISUSE_BKPT;
95565 db = p->db;
95566 sqlite3_mutex_enter(p: db->mutex);
95567 v = (Vdbe*)p->pStmt;
95568
95569 if( n<0 || iOffset<0 || ((sqlite3_int64)iOffset+n)>p->nByte ){
95570 /* Request is out of range. Return a transient error. */
95571 rc = SQLITE_ERROR;
95572 }else if( v==0 ){
95573 /* If there is no statement handle, then the blob-handle has
95574 ** already been invalidated. Return SQLITE_ABORT in this case.
95575 */
95576 rc = SQLITE_ABORT;
95577 }else{
95578 /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
95579 ** returned, clean-up the statement handle.
95580 */
95581 assert( db == v->db );
95582 sqlite3BtreeEnterCursor(pCur: p->pCsr);
95583
95584#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
95585 if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){
95586 /* If a pre-update hook is registered and this is a write cursor,
95587 ** invoke it here.
95588 **
95589 ** TODO: The preupdate-hook is passed SQLITE_DELETE, even though this
95590 ** operation should really be an SQLITE_UPDATE. This is probably
95591 ** incorrect, but is convenient because at this point the new.* values
95592 ** are not easily obtainable. And for the sessions module, an
95593 ** SQLITE_UPDATE where the PK columns do not change is handled in the
95594 ** same way as an SQLITE_DELETE (the SQLITE_DELETE code is actually
95595 ** slightly more efficient). Since you cannot write to a PK column
95596 ** using the incremental-blob API, this works. For the sessions module
95597 ** anyhow.
95598 */
95599 sqlite3_int64 iKey;
95600 iKey = sqlite3BtreeIntegerKey(p->pCsr);
95601 assert( v->apCsr[0]!=0 );
95602 assert( v->apCsr[0]->eCurType==CURTYPE_BTREE );
95603 sqlite3VdbePreUpdateHook(
95604 v, v->apCsr[0], SQLITE_DELETE, p->zDb, p->pTab, iKey, -1, p->iCol
95605 );
95606 }
95607#endif
95608
95609 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
95610 sqlite3BtreeLeaveCursor(pCur: p->pCsr);
95611 if( rc==SQLITE_ABORT ){
95612 sqlite3VdbeFinalize(p: v);
95613 p->pStmt = 0;
95614 }else{
95615 v->rc = rc;
95616 }
95617 }
95618 sqlite3Error(db, err_code: rc);
95619 rc = sqlite3ApiExit(db, rc);
95620 sqlite3_mutex_leave(p: db->mutex);
95621 return rc;
95622}
95623
95624/*
95625** Read data from a blob handle.
95626*/
95627SQLITE_API int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
95628 return blobReadWrite(pBlob, z, n, iOffset, xCall: sqlite3BtreePayloadChecked);
95629}
95630
95631/*
95632** Write data to a blob handle.
95633*/
95634SQLITE_API int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
95635 return blobReadWrite(pBlob, z: (void *)z, n, iOffset, xCall: sqlite3BtreePutData);
95636}
95637
95638/*
95639** Query a blob handle for the size of the data.
95640**
95641** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
95642** so no mutex is required for access.
95643*/
95644SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *pBlob){
95645 Incrblob *p = (Incrblob *)pBlob;
95646 return (p && p->pStmt) ? p->nByte : 0;
95647}
95648
95649/*
95650** Move an existing blob handle to point to a different row of the same
95651** database table.
95652**
95653** If an error occurs, or if the specified row does not exist or does not
95654** contain a blob or text value, then an error code is returned and the
95655** database handle error code and message set. If this happens, then all
95656** subsequent calls to sqlite3_blob_xxx() functions (except blob_close())
95657** immediately return SQLITE_ABORT.
95658*/
95659SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){
95660 int rc;
95661 Incrblob *p = (Incrblob *)pBlob;
95662 sqlite3 *db;
95663
95664 if( p==0 ) return SQLITE_MISUSE_BKPT;
95665 db = p->db;
95666 sqlite3_mutex_enter(p: db->mutex);
95667
95668 if( p->pStmt==0 ){
95669 /* If there is no statement handle, then the blob-handle has
95670 ** already been invalidated. Return SQLITE_ABORT in this case.
95671 */
95672 rc = SQLITE_ABORT;
95673 }else{
95674 char *zErr;
95675 ((Vdbe*)p->pStmt)->rc = SQLITE_OK;
95676 rc = blobSeekToRow(p, iRow, pzErr: &zErr);
95677 if( rc!=SQLITE_OK ){
95678 sqlite3ErrorWithMsg(db, err_code: rc, zFormat: (zErr ? "%s" : 0), zErr);
95679 sqlite3DbFree(db, p: zErr);
95680 }
95681 assert( rc!=SQLITE_SCHEMA );
95682 }
95683
95684 rc = sqlite3ApiExit(db, rc);
95685 assert( rc==SQLITE_OK || p->pStmt==0 );
95686 sqlite3_mutex_leave(p: db->mutex);
95687 return rc;
95688}
95689
95690#endif /* #ifndef SQLITE_OMIT_INCRBLOB */
95691
95692/************** End of vdbeblob.c ********************************************/
95693/************** Begin file vdbesort.c ****************************************/
95694/*
95695** 2011-07-09
95696**
95697** The author disclaims copyright to this source code. In place of
95698** a legal notice, here is a blessing:
95699**
95700** May you do good and not evil.
95701** May you find forgiveness for yourself and forgive others.
95702** May you share freely, never taking more than you give.
95703**
95704*************************************************************************
95705** This file contains code for the VdbeSorter object, used in concert with
95706** a VdbeCursor to sort large numbers of keys for CREATE INDEX statements
95707** or by SELECT statements with ORDER BY clauses that cannot be satisfied
95708** using indexes and without LIMIT clauses.
95709**
95710** The VdbeSorter object implements a multi-threaded external merge sort
95711** algorithm that is efficient even if the number of elements being sorted
95712** exceeds the available memory.
95713**
95714** Here is the (internal, non-API) interface between this module and the
95715** rest of the SQLite system:
95716**
95717** sqlite3VdbeSorterInit() Create a new VdbeSorter object.
95718**
95719** sqlite3VdbeSorterWrite() Add a single new row to the VdbeSorter
95720** object. The row is a binary blob in the
95721** OP_MakeRecord format that contains both
95722** the ORDER BY key columns and result columns
95723** in the case of a SELECT w/ ORDER BY, or
95724** the complete record for an index entry
95725** in the case of a CREATE INDEX.
95726**
95727** sqlite3VdbeSorterRewind() Sort all content previously added.
95728** Position the read cursor on the
95729** first sorted element.
95730**
95731** sqlite3VdbeSorterNext() Advance the read cursor to the next sorted
95732** element.
95733**
95734** sqlite3VdbeSorterRowkey() Return the complete binary blob for the
95735** row currently under the read cursor.
95736**
95737** sqlite3VdbeSorterCompare() Compare the binary blob for the row
95738** currently under the read cursor against
95739** another binary blob X and report if
95740** X is strictly less than the read cursor.
95741** Used to enforce uniqueness in a
95742** CREATE UNIQUE INDEX statement.
95743**
95744** sqlite3VdbeSorterClose() Close the VdbeSorter object and reclaim
95745** all resources.
95746**
95747** sqlite3VdbeSorterReset() Refurbish the VdbeSorter for reuse. This
95748** is like Close() followed by Init() only
95749** much faster.
95750**
95751** The interfaces above must be called in a particular order. Write() can
95752** only occur in between Init()/Reset() and Rewind(). Next(), Rowkey(), and
95753** Compare() can only occur in between Rewind() and Close()/Reset(). i.e.
95754**
95755** Init()
95756** for each record: Write()
95757** Rewind()
95758** Rowkey()/Compare()
95759** Next()
95760** Close()
95761**
95762** Algorithm:
95763**
95764** Records passed to the sorter via calls to Write() are initially held
95765** unsorted in main memory. Assuming the amount of memory used never exceeds
95766** a threshold, when Rewind() is called the set of records is sorted using
95767** an in-memory merge sort. In this case, no temporary files are required
95768** and subsequent calls to Rowkey(), Next() and Compare() read records
95769** directly from main memory.
95770**
95771** If the amount of space used to store records in main memory exceeds the
95772** threshold, then the set of records currently in memory are sorted and
95773** written to a temporary file in "Packed Memory Array" (PMA) format.
95774** A PMA created at this point is known as a "level-0 PMA". Higher levels
95775** of PMAs may be created by merging existing PMAs together - for example
95776** merging two or more level-0 PMAs together creates a level-1 PMA.
95777**
95778** The threshold for the amount of main memory to use before flushing
95779** records to a PMA is roughly the same as the limit configured for the
95780** page-cache of the main database. Specifically, the threshold is set to
95781** the value returned by "PRAGMA main.page_size" multipled by
95782** that returned by "PRAGMA main.cache_size", in bytes.
95783**
95784** If the sorter is running in single-threaded mode, then all PMAs generated
95785** are appended to a single temporary file. Or, if the sorter is running in
95786** multi-threaded mode then up to (N+1) temporary files may be opened, where
95787** N is the configured number of worker threads. In this case, instead of
95788** sorting the records and writing the PMA to a temporary file itself, the
95789** calling thread usually launches a worker thread to do so. Except, if
95790** there are already N worker threads running, the main thread does the work
95791** itself.
95792**
95793** The sorter is running in multi-threaded mode if (a) the library was built
95794** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater
95795** than zero, and (b) worker threads have been enabled at runtime by calling
95796** "PRAGMA threads=N" with some value of N greater than 0.
95797**
95798** When Rewind() is called, any data remaining in memory is flushed to a
95799** final PMA. So at this point the data is stored in some number of sorted
95800** PMAs within temporary files on disk.
95801**
95802** If there are fewer than SORTER_MAX_MERGE_COUNT PMAs in total and the
95803** sorter is running in single-threaded mode, then these PMAs are merged
95804** incrementally as keys are retreived from the sorter by the VDBE. The
95805** MergeEngine object, described in further detail below, performs this
95806** merge.
95807**
95808** Or, if running in multi-threaded mode, then a background thread is
95809** launched to merge the existing PMAs. Once the background thread has
95810** merged T bytes of data into a single sorted PMA, the main thread
95811** begins reading keys from that PMA while the background thread proceeds
95812** with merging the next T bytes of data. And so on.
95813**
95814** Parameter T is set to half the value of the memory threshold used
95815** by Write() above to determine when to create a new PMA.
95816**
95817** If there are more than SORTER_MAX_MERGE_COUNT PMAs in total when
95818** Rewind() is called, then a hierarchy of incremental-merges is used.
95819** First, T bytes of data from the first SORTER_MAX_MERGE_COUNT PMAs on
95820** disk are merged together. Then T bytes of data from the second set, and
95821** so on, such that no operation ever merges more than SORTER_MAX_MERGE_COUNT
95822** PMAs at a time. This done is to improve locality.
95823**
95824** If running in multi-threaded mode and there are more than
95825** SORTER_MAX_MERGE_COUNT PMAs on disk when Rewind() is called, then more
95826** than one background thread may be created. Specifically, there may be
95827** one background thread for each temporary file on disk, and one background
95828** thread to merge the output of each of the others to a single PMA for
95829** the main thread to read from.
95830*/
95831/* #include "sqliteInt.h" */
95832/* #include "vdbeInt.h" */
95833
95834/*
95835** If SQLITE_DEBUG_SORTER_THREADS is defined, this module outputs various
95836** messages to stderr that may be helpful in understanding the performance
95837** characteristics of the sorter in multi-threaded mode.
95838*/
95839#if 0
95840# define SQLITE_DEBUG_SORTER_THREADS 1
95841#endif
95842
95843/*
95844** Hard-coded maximum amount of data to accumulate in memory before flushing
95845** to a level 0 PMA. The purpose of this limit is to prevent various integer
95846** overflows. 512MiB.
95847*/
95848#define SQLITE_MAX_PMASZ (1<<29)
95849
95850/*
95851** Private objects used by the sorter
95852*/
95853typedef struct MergeEngine MergeEngine; /* Merge PMAs together */
95854typedef struct PmaReader PmaReader; /* Incrementally read one PMA */
95855typedef struct PmaWriter PmaWriter; /* Incrementally write one PMA */
95856typedef struct SorterRecord SorterRecord; /* A record being sorted */
95857typedef struct SortSubtask SortSubtask; /* A sub-task in the sort process */
95858typedef struct SorterFile SorterFile; /* Temporary file object wrapper */
95859typedef struct SorterList SorterList; /* In-memory list of records */
95860typedef struct IncrMerger IncrMerger; /* Read & merge multiple PMAs */
95861
95862/*
95863** A container for a temp file handle and the current amount of data
95864** stored in the file.
95865*/
95866struct SorterFile {
95867 sqlite3_file *pFd; /* File handle */
95868 i64 iEof; /* Bytes of data stored in pFd */
95869};
95870
95871/*
95872** An in-memory list of objects to be sorted.
95873**
95874** If aMemory==0 then each object is allocated separately and the objects
95875** are connected using SorterRecord.u.pNext. If aMemory!=0 then all objects
95876** are stored in the aMemory[] bulk memory, one right after the other, and
95877** are connected using SorterRecord.u.iNext.
95878*/
95879struct SorterList {
95880 SorterRecord *pList; /* Linked list of records */
95881 u8 *aMemory; /* If non-NULL, bulk memory to hold pList */
95882 int szPMA; /* Size of pList as PMA in bytes */
95883};
95884
95885/*
95886** The MergeEngine object is used to combine two or more smaller PMAs into
95887** one big PMA using a merge operation. Separate PMAs all need to be
95888** combined into one big PMA in order to be able to step through the sorted
95889** records in order.
95890**
95891** The aReadr[] array contains a PmaReader object for each of the PMAs being
95892** merged. An aReadr[] object either points to a valid key or else is at EOF.
95893** ("EOF" means "End Of File". When aReadr[] is at EOF there is no more data.)
95894** For the purposes of the paragraphs below, we assume that the array is
95895** actually N elements in size, where N is the smallest power of 2 greater
95896** to or equal to the number of PMAs being merged. The extra aReadr[] elements
95897** are treated as if they are empty (always at EOF).
95898**
95899** The aTree[] array is also N elements in size. The value of N is stored in
95900** the MergeEngine.nTree variable.
95901**
95902** The final (N/2) elements of aTree[] contain the results of comparing
95903** pairs of PMA keys together. Element i contains the result of
95904** comparing aReadr[2*i-N] and aReadr[2*i-N+1]. Whichever key is smaller, the
95905** aTree element is set to the index of it.
95906**
95907** For the purposes of this comparison, EOF is considered greater than any
95908** other key value. If the keys are equal (only possible with two EOF
95909** values), it doesn't matter which index is stored.
95910**
95911** The (N/4) elements of aTree[] that precede the final (N/2) described
95912** above contains the index of the smallest of each block of 4 PmaReaders
95913** And so on. So that aTree[1] contains the index of the PmaReader that
95914** currently points to the smallest key value. aTree[0] is unused.
95915**
95916** Example:
95917**
95918** aReadr[0] -> Banana
95919** aReadr[1] -> Feijoa
95920** aReadr[2] -> Elderberry
95921** aReadr[3] -> Currant
95922** aReadr[4] -> Grapefruit
95923** aReadr[5] -> Apple
95924** aReadr[6] -> Durian
95925** aReadr[7] -> EOF
95926**
95927** aTree[] = { X, 5 0, 5 0, 3, 5, 6 }
95928**
95929** The current element is "Apple" (the value of the key indicated by
95930** PmaReader 5). When the Next() operation is invoked, PmaReader 5 will
95931** be advanced to the next key in its segment. Say the next key is
95932** "Eggplant":
95933**
95934** aReadr[5] -> Eggplant
95935**
95936** The contents of aTree[] are updated first by comparing the new PmaReader
95937** 5 key to the current key of PmaReader 4 (still "Grapefruit"). The PmaReader
95938** 5 value is still smaller, so aTree[6] is set to 5. And so on up the tree.
95939** The value of PmaReader 6 - "Durian" - is now smaller than that of PmaReader
95940** 5, so aTree[3] is set to 6. Key 0 is smaller than key 6 (Banana<Durian),
95941** so the value written into element 1 of the array is 0. As follows:
95942**
95943** aTree[] = { X, 0 0, 6 0, 3, 5, 6 }
95944**
95945** In other words, each time we advance to the next sorter element, log2(N)
95946** key comparison operations are required, where N is the number of segments
95947** being merged (rounded up to the next power of 2).
95948*/
95949struct MergeEngine {
95950 int nTree; /* Used size of aTree/aReadr (power of 2) */
95951 SortSubtask *pTask; /* Used by this thread only */
95952 int *aTree; /* Current state of incremental merge */
95953 PmaReader *aReadr; /* Array of PmaReaders to merge data from */
95954};
95955
95956/*
95957** This object represents a single thread of control in a sort operation.
95958** Exactly VdbeSorter.nTask instances of this object are allocated
95959** as part of each VdbeSorter object. Instances are never allocated any
95960** other way. VdbeSorter.nTask is set to the number of worker threads allowed
95961** (see SQLITE_CONFIG_WORKER_THREADS) plus one (the main thread). Thus for
95962** single-threaded operation, there is exactly one instance of this object
95963** and for multi-threaded operation there are two or more instances.
95964**
95965** Essentially, this structure contains all those fields of the VdbeSorter
95966** structure for which each thread requires a separate instance. For example,
95967** each thread requries its own UnpackedRecord object to unpack records in
95968** as part of comparison operations.
95969**
95970** Before a background thread is launched, variable bDone is set to 0. Then,
95971** right before it exits, the thread itself sets bDone to 1. This is used for
95972** two purposes:
95973**
95974** 1. When flushing the contents of memory to a level-0 PMA on disk, to
95975** attempt to select a SortSubtask for which there is not already an
95976** active background thread (since doing so causes the main thread
95977** to block until it finishes).
95978**
95979** 2. If SQLITE_DEBUG_SORTER_THREADS is defined, to determine if a call
95980** to sqlite3ThreadJoin() is likely to block. Cases that are likely to
95981** block provoke debugging output.
95982**
95983** In both cases, the effects of the main thread seeing (bDone==0) even
95984** after the thread has finished are not dire. So we don't worry about
95985** memory barriers and such here.
95986*/
95987typedef int (*SorterCompare)(SortSubtask*,int*,const void*,int,const void*,int);
95988struct SortSubtask {
95989 SQLiteThread *pThread; /* Background thread, if any */
95990 int bDone; /* Set if thread is finished but not joined */
95991 VdbeSorter *pSorter; /* Sorter that owns this sub-task */
95992 UnpackedRecord *pUnpacked; /* Space to unpack a record */
95993 SorterList list; /* List for thread to write to a PMA */
95994 int nPMA; /* Number of PMAs currently in file */
95995 SorterCompare xCompare; /* Compare function to use */
95996 SorterFile file; /* Temp file for level-0 PMAs */
95997 SorterFile file2; /* Space for other PMAs */
95998};
95999
96000
96001/*
96002** Main sorter structure. A single instance of this is allocated for each
96003** sorter cursor created by the VDBE.
96004**
96005** mxKeysize:
96006** As records are added to the sorter by calls to sqlite3VdbeSorterWrite(),
96007** this variable is updated so as to be set to the size on disk of the
96008** largest record in the sorter.
96009*/
96010struct VdbeSorter {
96011 int mnPmaSize; /* Minimum PMA size, in bytes */
96012 int mxPmaSize; /* Maximum PMA size, in bytes. 0==no limit */
96013 int mxKeysize; /* Largest serialized key seen so far */
96014 int pgsz; /* Main database page size */
96015 PmaReader *pReader; /* Readr data from here after Rewind() */
96016 MergeEngine *pMerger; /* Or here, if bUseThreads==0 */
96017 sqlite3 *db; /* Database connection */
96018 KeyInfo *pKeyInfo; /* How to compare records */
96019 UnpackedRecord *pUnpacked; /* Used by VdbeSorterCompare() */
96020 SorterList list; /* List of in-memory records */
96021 int iMemory; /* Offset of free space in list.aMemory */
96022 int nMemory; /* Size of list.aMemory allocation in bytes */
96023 u8 bUsePMA; /* True if one or more PMAs created */
96024 u8 bUseThreads; /* True to use background threads */
96025 u8 iPrev; /* Previous thread used to flush PMA */
96026 u8 nTask; /* Size of aTask[] array */
96027 u8 typeMask;
96028 SortSubtask aTask[1]; /* One or more subtasks */
96029};
96030
96031#define SORTER_TYPE_INTEGER 0x01
96032#define SORTER_TYPE_TEXT 0x02
96033
96034/*
96035** An instance of the following object is used to read records out of a
96036** PMA, in sorted order. The next key to be read is cached in nKey/aKey.
96037** aKey might point into aMap or into aBuffer. If neither of those locations
96038** contain a contiguous representation of the key, then aAlloc is allocated
96039** and the key is copied into aAlloc and aKey is made to poitn to aAlloc.
96040**
96041** pFd==0 at EOF.
96042*/
96043struct PmaReader {
96044 i64 iReadOff; /* Current read offset */
96045 i64 iEof; /* 1 byte past EOF for this PmaReader */
96046 int nAlloc; /* Bytes of space at aAlloc */
96047 int nKey; /* Number of bytes in key */
96048 sqlite3_file *pFd; /* File handle we are reading from */
96049 u8 *aAlloc; /* Space for aKey if aBuffer and pMap wont work */
96050 u8 *aKey; /* Pointer to current key */
96051 u8 *aBuffer; /* Current read buffer */
96052 int nBuffer; /* Size of read buffer in bytes */
96053 u8 *aMap; /* Pointer to mapping of entire file */
96054 IncrMerger *pIncr; /* Incremental merger */
96055};
96056
96057/*
96058** Normally, a PmaReader object iterates through an existing PMA stored
96059** within a temp file. However, if the PmaReader.pIncr variable points to
96060** an object of the following type, it may be used to iterate/merge through
96061** multiple PMAs simultaneously.
96062**
96063** There are two types of IncrMerger object - single (bUseThread==0) and
96064** multi-threaded (bUseThread==1).
96065**
96066** A multi-threaded IncrMerger object uses two temporary files - aFile[0]
96067** and aFile[1]. Neither file is allowed to grow to more than mxSz bytes in
96068** size. When the IncrMerger is initialized, it reads enough data from
96069** pMerger to populate aFile[0]. It then sets variables within the
96070** corresponding PmaReader object to read from that file and kicks off
96071** a background thread to populate aFile[1] with the next mxSz bytes of
96072** sorted record data from pMerger.
96073**
96074** When the PmaReader reaches the end of aFile[0], it blocks until the
96075** background thread has finished populating aFile[1]. It then exchanges
96076** the contents of the aFile[0] and aFile[1] variables within this structure,
96077** sets the PmaReader fields to read from the new aFile[0] and kicks off
96078** another background thread to populate the new aFile[1]. And so on, until
96079** the contents of pMerger are exhausted.
96080**
96081** A single-threaded IncrMerger does not open any temporary files of its
96082** own. Instead, it has exclusive access to mxSz bytes of space beginning
96083** at offset iStartOff of file pTask->file2. And instead of using a
96084** background thread to prepare data for the PmaReader, with a single
96085** threaded IncrMerger the allocate part of pTask->file2 is "refilled" with
96086** keys from pMerger by the calling thread whenever the PmaReader runs out
96087** of data.
96088*/
96089struct IncrMerger {
96090 SortSubtask *pTask; /* Task that owns this merger */
96091 MergeEngine *pMerger; /* Merge engine thread reads data from */
96092 i64 iStartOff; /* Offset to start writing file at */
96093 int mxSz; /* Maximum bytes of data to store */
96094 int bEof; /* Set to true when merge is finished */
96095 int bUseThread; /* True to use a bg thread for this object */
96096 SorterFile aFile[2]; /* aFile[0] for reading, [1] for writing */
96097};
96098
96099/*
96100** An instance of this object is used for writing a PMA.
96101**
96102** The PMA is written one record at a time. Each record is of an arbitrary
96103** size. But I/O is more efficient if it occurs in page-sized blocks where
96104** each block is aligned on a page boundary. This object caches writes to
96105** the PMA so that aligned, page-size blocks are written.
96106*/
96107struct PmaWriter {
96108 int eFWErr; /* Non-zero if in an error state */
96109 u8 *aBuffer; /* Pointer to write buffer */
96110 int nBuffer; /* Size of write buffer in bytes */
96111 int iBufStart; /* First byte of buffer to write */
96112 int iBufEnd; /* Last byte of buffer to write */
96113 i64 iWriteOff; /* Offset of start of buffer in file */
96114 sqlite3_file *pFd; /* File handle to write to */
96115};
96116
96117/*
96118** This object is the header on a single record while that record is being
96119** held in memory and prior to being written out as part of a PMA.
96120**
96121** How the linked list is connected depends on how memory is being managed
96122** by this module. If using a separate allocation for each in-memory record
96123** (VdbeSorter.list.aMemory==0), then the list is always connected using the
96124** SorterRecord.u.pNext pointers.
96125**
96126** Or, if using the single large allocation method (VdbeSorter.list.aMemory!=0),
96127** then while records are being accumulated the list is linked using the
96128** SorterRecord.u.iNext offset. This is because the aMemory[] array may
96129** be sqlite3Realloc()ed while records are being accumulated. Once the VM
96130** has finished passing records to the sorter, or when the in-memory buffer
96131** is full, the list is sorted. As part of the sorting process, it is
96132** converted to use the SorterRecord.u.pNext pointers. See function
96133** vdbeSorterSort() for details.
96134*/
96135struct SorterRecord {
96136 int nVal; /* Size of the record in bytes */
96137 union {
96138 SorterRecord *pNext; /* Pointer to next record in list */
96139 int iNext; /* Offset within aMemory of next record */
96140 } u;
96141 /* The data for the record immediately follows this header */
96142};
96143
96144/* Return a pointer to the buffer containing the record data for SorterRecord
96145** object p. Should be used as if:
96146**
96147** void *SRVAL(SorterRecord *p) { return (void*)&p[1]; }
96148*/
96149#define SRVAL(p) ((void*)((SorterRecord*)(p) + 1))
96150
96151
96152/* Maximum number of PMAs that a single MergeEngine can merge */
96153#define SORTER_MAX_MERGE_COUNT 16
96154
96155static int vdbeIncrSwap(IncrMerger*);
96156static void vdbeIncrFree(IncrMerger *);
96157
96158/*
96159** Free all memory belonging to the PmaReader object passed as the
96160** argument. All structure fields are set to zero before returning.
96161*/
96162static void vdbePmaReaderClear(PmaReader *pReadr){
96163 sqlite3_free(p: pReadr->aAlloc);
96164 sqlite3_free(p: pReadr->aBuffer);
96165 if( pReadr->aMap ) sqlite3OsUnfetch(id: pReadr->pFd, iOff: 0, p: pReadr->aMap);
96166 vdbeIncrFree(pReadr->pIncr);
96167 memset(s: pReadr, c: 0, n: sizeof(PmaReader));
96168}
96169
96170/*
96171** Read the next nByte bytes of data from the PMA p.
96172** If successful, set *ppOut to point to a buffer containing the data
96173** and return SQLITE_OK. Otherwise, if an error occurs, return an SQLite
96174** error code.
96175**
96176** The buffer returned in *ppOut is only valid until the
96177** next call to this function.
96178*/
96179static int vdbePmaReadBlob(
96180 PmaReader *p, /* PmaReader from which to take the blob */
96181 int nByte, /* Bytes of data to read */
96182 u8 **ppOut /* OUT: Pointer to buffer containing data */
96183){
96184 int iBuf; /* Offset within buffer to read from */
96185 int nAvail; /* Bytes of data available in buffer */
96186
96187 if( p->aMap ){
96188 *ppOut = &p->aMap[p->iReadOff];
96189 p->iReadOff += nByte;
96190 return SQLITE_OK;
96191 }
96192
96193 assert( p->aBuffer );
96194
96195 /* If there is no more data to be read from the buffer, read the next
96196 ** p->nBuffer bytes of data from the file into it. Or, if there are less
96197 ** than p->nBuffer bytes remaining in the PMA, read all remaining data. */
96198 iBuf = p->iReadOff % p->nBuffer;
96199 if( iBuf==0 ){
96200 int nRead; /* Bytes to read from disk */
96201 int rc; /* sqlite3OsRead() return code */
96202
96203 /* Determine how many bytes of data to read. */
96204 if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
96205 nRead = p->nBuffer;
96206 }else{
96207 nRead = (int)(p->iEof - p->iReadOff);
96208 }
96209 assert( nRead>0 );
96210
96211 /* Readr data from the file. Return early if an error occurs. */
96212 rc = sqlite3OsRead(id: p->pFd, pBuf: p->aBuffer, amt: nRead, offset: p->iReadOff);
96213 assert( rc!=SQLITE_IOERR_SHORT_READ );
96214 if( rc!=SQLITE_OK ) return rc;
96215 }
96216 nAvail = p->nBuffer - iBuf;
96217
96218 if( nByte<=nAvail ){
96219 /* The requested data is available in the in-memory buffer. In this
96220 ** case there is no need to make a copy of the data, just return a
96221 ** pointer into the buffer to the caller. */
96222 *ppOut = &p->aBuffer[iBuf];
96223 p->iReadOff += nByte;
96224 }else{
96225 /* The requested data is not all available in the in-memory buffer.
96226 ** In this case, allocate space at p->aAlloc[] to copy the requested
96227 ** range into. Then return a copy of pointer p->aAlloc to the caller. */
96228 int nRem; /* Bytes remaining to copy */
96229
96230 /* Extend the p->aAlloc[] allocation if required. */
96231 if( p->nAlloc<nByte ){
96232 u8 *aNew;
96233 sqlite3_int64 nNew = MAX(128, 2*(sqlite3_int64)p->nAlloc);
96234 while( nByte>nNew ) nNew = nNew*2;
96235 aNew = sqlite3Realloc(pOld: p->aAlloc, nBytes: nNew);
96236 if( !aNew ) return SQLITE_NOMEM_BKPT;
96237 p->nAlloc = nNew;
96238 p->aAlloc = aNew;
96239 }
96240
96241 /* Copy as much data as is available in the buffer into the start of
96242 ** p->aAlloc[]. */
96243 memcpy(dest: p->aAlloc, src: &p->aBuffer[iBuf], n: nAvail);
96244 p->iReadOff += nAvail;
96245 nRem = nByte - nAvail;
96246
96247 /* The following loop copies up to p->nBuffer bytes per iteration into
96248 ** the p->aAlloc[] buffer. */
96249 while( nRem>0 ){
96250 int rc; /* vdbePmaReadBlob() return code */
96251 int nCopy; /* Number of bytes to copy */
96252 u8 *aNext; /* Pointer to buffer to copy data from */
96253
96254 nCopy = nRem;
96255 if( nRem>p->nBuffer ) nCopy = p->nBuffer;
96256 rc = vdbePmaReadBlob(p, nByte: nCopy, ppOut: &aNext);
96257 if( rc!=SQLITE_OK ) return rc;
96258 assert( aNext!=p->aAlloc );
96259 memcpy(dest: &p->aAlloc[nByte - nRem], src: aNext, n: nCopy);
96260 nRem -= nCopy;
96261 }
96262
96263 *ppOut = p->aAlloc;
96264 }
96265
96266 return SQLITE_OK;
96267}
96268
96269/*
96270** Read a varint from the stream of data accessed by p. Set *pnOut to
96271** the value read.
96272*/
96273static int vdbePmaReadVarint(PmaReader *p, u64 *pnOut){
96274 int iBuf;
96275
96276 if( p->aMap ){
96277 p->iReadOff += sqlite3GetVarint(p: &p->aMap[p->iReadOff], v: pnOut);
96278 }else{
96279 iBuf = p->iReadOff % p->nBuffer;
96280 if( iBuf && (p->nBuffer-iBuf)>=9 ){
96281 p->iReadOff += sqlite3GetVarint(p: &p->aBuffer[iBuf], v: pnOut);
96282 }else{
96283 u8 aVarint[16], *a;
96284 int i = 0, rc;
96285 do{
96286 rc = vdbePmaReadBlob(p, nByte: 1, ppOut: &a);
96287 if( rc ) return rc;
96288 aVarint[(i++)&0xf] = a[0];
96289 }while( (a[0]&0x80)!=0 );
96290 sqlite3GetVarint(p: aVarint, v: pnOut);
96291 }
96292 }
96293
96294 return SQLITE_OK;
96295}
96296
96297/*
96298** Attempt to memory map file pFile. If successful, set *pp to point to the
96299** new mapping and return SQLITE_OK. If the mapping is not attempted
96300** (because the file is too large or the VFS layer is configured not to use
96301** mmap), return SQLITE_OK and set *pp to NULL.
96302**
96303** Or, if an error occurs, return an SQLite error code. The final value of
96304** *pp is undefined in this case.
96305*/
96306static int vdbeSorterMapFile(SortSubtask *pTask, SorterFile *pFile, u8 **pp){
96307 int rc = SQLITE_OK;
96308 if( pFile->iEof<=(i64)(pTask->pSorter->db->nMaxSorterMmap) ){
96309 sqlite3_file *pFd = pFile->pFd;
96310 if( pFd->pMethods->iVersion>=3 ){
96311 rc = sqlite3OsFetch(id: pFd, iOff: 0, iAmt: (int)pFile->iEof, pp: (void**)pp);
96312 testcase( rc!=SQLITE_OK );
96313 }
96314 }
96315 return rc;
96316}
96317
96318/*
96319** Attach PmaReader pReadr to file pFile (if it is not already attached to
96320** that file) and seek it to offset iOff within the file. Return SQLITE_OK
96321** if successful, or an SQLite error code if an error occurs.
96322*/
96323static int vdbePmaReaderSeek(
96324 SortSubtask *pTask, /* Task context */
96325 PmaReader *pReadr, /* Reader whose cursor is to be moved */
96326 SorterFile *pFile, /* Sorter file to read from */
96327 i64 iOff /* Offset in pFile */
96328){
96329 int rc = SQLITE_OK;
96330
96331 assert( pReadr->pIncr==0 || pReadr->pIncr->bEof==0 );
96332
96333 if( sqlite3FaultSim(iTest: 201) ) return SQLITE_IOERR_READ;
96334 if( pReadr->aMap ){
96335 sqlite3OsUnfetch(id: pReadr->pFd, iOff: 0, p: pReadr->aMap);
96336 pReadr->aMap = 0;
96337 }
96338 pReadr->iReadOff = iOff;
96339 pReadr->iEof = pFile->iEof;
96340 pReadr->pFd = pFile->pFd;
96341
96342 rc = vdbeSorterMapFile(pTask, pFile, pp: &pReadr->aMap);
96343 if( rc==SQLITE_OK && pReadr->aMap==0 ){
96344 int pgsz = pTask->pSorter->pgsz;
96345 int iBuf = pReadr->iReadOff % pgsz;
96346 if( pReadr->aBuffer==0 ){
96347 pReadr->aBuffer = (u8*)sqlite3Malloc(n: pgsz);
96348 if( pReadr->aBuffer==0 ) rc = SQLITE_NOMEM_BKPT;
96349 pReadr->nBuffer = pgsz;
96350 }
96351 if( rc==SQLITE_OK && iBuf ){
96352 int nRead = pgsz - iBuf;
96353 if( (pReadr->iReadOff + nRead) > pReadr->iEof ){
96354 nRead = (int)(pReadr->iEof - pReadr->iReadOff);
96355 }
96356 rc = sqlite3OsRead(
96357 id: pReadr->pFd, pBuf: &pReadr->aBuffer[iBuf], amt: nRead, offset: pReadr->iReadOff
96358 );
96359 testcase( rc!=SQLITE_OK );
96360 }
96361 }
96362
96363 return rc;
96364}
96365
96366/*
96367** Advance PmaReader pReadr to the next key in its PMA. Return SQLITE_OK if
96368** no error occurs, or an SQLite error code if one does.
96369*/
96370static int vdbePmaReaderNext(PmaReader *pReadr){
96371 int rc = SQLITE_OK; /* Return Code */
96372 u64 nRec = 0; /* Size of record in bytes */
96373
96374
96375 if( pReadr->iReadOff>=pReadr->iEof ){
96376 IncrMerger *pIncr = pReadr->pIncr;
96377 int bEof = 1;
96378 if( pIncr ){
96379 rc = vdbeIncrSwap(pIncr);
96380 if( rc==SQLITE_OK && pIncr->bEof==0 ){
96381 rc = vdbePmaReaderSeek(
96382 pTask: pIncr->pTask, pReadr, pFile: &pIncr->aFile[0], iOff: pIncr->iStartOff
96383 );
96384 bEof = 0;
96385 }
96386 }
96387
96388 if( bEof ){
96389 /* This is an EOF condition */
96390 vdbePmaReaderClear(pReadr);
96391 testcase( rc!=SQLITE_OK );
96392 return rc;
96393 }
96394 }
96395
96396 if( rc==SQLITE_OK ){
96397 rc = vdbePmaReadVarint(p: pReadr, pnOut: &nRec);
96398 }
96399 if( rc==SQLITE_OK ){
96400 pReadr->nKey = (int)nRec;
96401 rc = vdbePmaReadBlob(p: pReadr, nByte: (int)nRec, ppOut: &pReadr->aKey);
96402 testcase( rc!=SQLITE_OK );
96403 }
96404
96405 return rc;
96406}
96407
96408/*
96409** Initialize PmaReader pReadr to scan through the PMA stored in file pFile
96410** starting at offset iStart and ending at offset iEof-1. This function
96411** leaves the PmaReader pointing to the first key in the PMA (or EOF if the
96412** PMA is empty).
96413**
96414** If the pnByte parameter is NULL, then it is assumed that the file
96415** contains a single PMA, and that that PMA omits the initial length varint.
96416*/
96417static int vdbePmaReaderInit(
96418 SortSubtask *pTask, /* Task context */
96419 SorterFile *pFile, /* Sorter file to read from */
96420 i64 iStart, /* Start offset in pFile */
96421 PmaReader *pReadr, /* PmaReader to populate */
96422 i64 *pnByte /* IN/OUT: Increment this value by PMA size */
96423){
96424 int rc;
96425
96426 assert( pFile->iEof>iStart );
96427 assert( pReadr->aAlloc==0 && pReadr->nAlloc==0 );
96428 assert( pReadr->aBuffer==0 );
96429 assert( pReadr->aMap==0 );
96430
96431 rc = vdbePmaReaderSeek(pTask, pReadr, pFile, iOff: iStart);
96432 if( rc==SQLITE_OK ){
96433 u64 nByte = 0; /* Size of PMA in bytes */
96434 rc = vdbePmaReadVarint(p: pReadr, pnOut: &nByte);
96435 pReadr->iEof = pReadr->iReadOff + nByte;
96436 *pnByte += nByte;
96437 }
96438
96439 if( rc==SQLITE_OK ){
96440 rc = vdbePmaReaderNext(pReadr);
96441 }
96442 return rc;
96443}
96444
96445/*
96446** A version of vdbeSorterCompare() that assumes that it has already been
96447** determined that the first field of key1 is equal to the first field of
96448** key2.
96449*/
96450static int vdbeSorterCompareTail(
96451 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
96452 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
96453 const void *pKey1, int nKey1, /* Left side of comparison */
96454 const void *pKey2, int nKey2 /* Right side of comparison */
96455){
96456 UnpackedRecord *r2 = pTask->pUnpacked;
96457 if( *pbKey2Cached==0 ){
96458 sqlite3VdbeRecordUnpack(pKeyInfo: pTask->pSorter->pKeyInfo, nKey: nKey2, pKey: pKey2, p: r2);
96459 *pbKey2Cached = 1;
96460 }
96461 return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2: r2, bSkip: 1);
96462}
96463
96464/*
96465** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2,
96466** size nKey2 bytes). Use (pTask->pKeyInfo) for the collation sequences
96467** used by the comparison. Return the result of the comparison.
96468**
96469** If IN/OUT parameter *pbKey2Cached is true when this function is called,
96470** it is assumed that (pTask->pUnpacked) contains the unpacked version
96471** of key2. If it is false, (pTask->pUnpacked) is populated with the unpacked
96472** version of key2 and *pbKey2Cached set to true before returning.
96473**
96474** If an OOM error is encountered, (pTask->pUnpacked->error_rc) is set
96475** to SQLITE_NOMEM.
96476*/
96477static int vdbeSorterCompare(
96478 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
96479 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
96480 const void *pKey1, int nKey1, /* Left side of comparison */
96481 const void *pKey2, int nKey2 /* Right side of comparison */
96482){
96483 UnpackedRecord *r2 = pTask->pUnpacked;
96484 if( !*pbKey2Cached ){
96485 sqlite3VdbeRecordUnpack(pKeyInfo: pTask->pSorter->pKeyInfo, nKey: nKey2, pKey: pKey2, p: r2);
96486 *pbKey2Cached = 1;
96487 }
96488 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2: r2);
96489}
96490
96491/*
96492** A specially optimized version of vdbeSorterCompare() that assumes that
96493** the first field of each key is a TEXT value and that the collation
96494** sequence to compare them with is BINARY.
96495*/
96496static int vdbeSorterCompareText(
96497 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
96498 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
96499 const void *pKey1, int nKey1, /* Left side of comparison */
96500 const void *pKey2, int nKey2 /* Right side of comparison */
96501){
96502 const u8 * const p1 = (const u8 * const)pKey1;
96503 const u8 * const p2 = (const u8 * const)pKey2;
96504 const u8 * const v1 = &p1[ p1[0] ]; /* Pointer to value 1 */
96505 const u8 * const v2 = &p2[ p2[0] ]; /* Pointer to value 2 */
96506
96507 int n1;
96508 int n2;
96509 int res;
96510
96511 getVarint32NR(&p1[1], n1);
96512 getVarint32NR(&p2[1], n2);
96513 res = memcmp(s1: v1, s2: v2, n: (MIN(n1, n2) - 13)/2);
96514 if( res==0 ){
96515 res = n1 - n2;
96516 }
96517
96518 if( res==0 ){
96519 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
96520 res = vdbeSorterCompareTail(
96521 pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
96522 );
96523 }
96524 }else{
96525 assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) );
96526 if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
96527 res = res * -1;
96528 }
96529 }
96530
96531 return res;
96532}
96533
96534/*
96535** A specially optimized version of vdbeSorterCompare() that assumes that
96536** the first field of each key is an INTEGER value.
96537*/
96538static int vdbeSorterCompareInt(
96539 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
96540 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
96541 const void *pKey1, int nKey1, /* Left side of comparison */
96542 const void *pKey2, int nKey2 /* Right side of comparison */
96543){
96544 const u8 * const p1 = (const u8 * const)pKey1;
96545 const u8 * const p2 = (const u8 * const)pKey2;
96546 const int s1 = p1[1]; /* Left hand serial type */
96547 const int s2 = p2[1]; /* Right hand serial type */
96548 const u8 * const v1 = &p1[ p1[0] ]; /* Pointer to value 1 */
96549 const u8 * const v2 = &p2[ p2[0] ]; /* Pointer to value 2 */
96550 int res; /* Return value */
96551
96552 assert( (s1>0 && s1<7) || s1==8 || s1==9 );
96553 assert( (s2>0 && s2<7) || s2==8 || s2==9 );
96554
96555 if( s1==s2 ){
96556 /* The two values have the same sign. Compare using memcmp(). */
96557 static const u8 aLen[] = {0, 1, 2, 3, 4, 6, 8, 0, 0, 0 };
96558 const u8 n = aLen[s1];
96559 int i;
96560 res = 0;
96561 for(i=0; i<n; i++){
96562 if( (res = v1[i] - v2[i])!=0 ){
96563 if( ((v1[0] ^ v2[0]) & 0x80)!=0 ){
96564 res = v1[0] & 0x80 ? -1 : +1;
96565 }
96566 break;
96567 }
96568 }
96569 }else if( s1>7 && s2>7 ){
96570 res = s1 - s2;
96571 }else{
96572 if( s2>7 ){
96573 res = +1;
96574 }else if( s1>7 ){
96575 res = -1;
96576 }else{
96577 res = s1 - s2;
96578 }
96579 assert( res!=0 );
96580
96581 if( res>0 ){
96582 if( *v1 & 0x80 ) res = -1;
96583 }else{
96584 if( *v2 & 0x80 ) res = +1;
96585 }
96586 }
96587
96588 if( res==0 ){
96589 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
96590 res = vdbeSorterCompareTail(
96591 pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
96592 );
96593 }
96594 }else if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
96595 assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) );
96596 res = res * -1;
96597 }
96598
96599 return res;
96600}
96601
96602/*
96603** Initialize the temporary index cursor just opened as a sorter cursor.
96604**
96605** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nKeyField)
96606** to determine the number of fields that should be compared from the
96607** records being sorted. However, if the value passed as argument nField
96608** is non-zero and the sorter is able to guarantee a stable sort, nField
96609** is used instead. This is used when sorting records for a CREATE INDEX
96610** statement. In this case, keys are always delivered to the sorter in
96611** order of the primary key, which happens to be make up the final part
96612** of the records being sorted. So if the sort is stable, there is never
96613** any reason to compare PK fields and they can be ignored for a small
96614** performance boost.
96615**
96616** The sorter can guarantee a stable sort when running in single-threaded
96617** mode, but not in multi-threaded mode.
96618**
96619** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
96620*/
96621SQLITE_PRIVATE int sqlite3VdbeSorterInit(
96622 sqlite3 *db, /* Database connection (for malloc()) */
96623 int nField, /* Number of key fields in each record */
96624 VdbeCursor *pCsr /* Cursor that holds the new sorter */
96625){
96626 int pgsz; /* Page size of main database */
96627 int i; /* Used to iterate through aTask[] */
96628 VdbeSorter *pSorter; /* The new sorter */
96629 KeyInfo *pKeyInfo; /* Copy of pCsr->pKeyInfo with db==0 */
96630 int szKeyInfo; /* Size of pCsr->pKeyInfo in bytes */
96631 int sz; /* Size of pSorter in bytes */
96632 int rc = SQLITE_OK;
96633#if SQLITE_MAX_WORKER_THREADS==0
96634# define nWorker 0
96635#else
96636 int nWorker;
96637#endif
96638
96639 /* Initialize the upper limit on the number of worker threads */
96640#if SQLITE_MAX_WORKER_THREADS>0
96641 if( sqlite3TempInMemory(db) || sqlite3GlobalConfig.bCoreMutex==0 ){
96642 nWorker = 0;
96643 }else{
96644 nWorker = db->aLimit[SQLITE_LIMIT_WORKER_THREADS];
96645 }
96646#endif
96647
96648 /* Do not allow the total number of threads (main thread + all workers)
96649 ** to exceed the maximum merge count */
96650#if SQLITE_MAX_WORKER_THREADS>=SORTER_MAX_MERGE_COUNT
96651 if( nWorker>=SORTER_MAX_MERGE_COUNT ){
96652 nWorker = SORTER_MAX_MERGE_COUNT-1;
96653 }
96654#endif
96655
96656 assert( pCsr->pKeyInfo && pCsr->pBtx==0 );
96657 assert( pCsr->eCurType==CURTYPE_SORTER );
96658 szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nKeyField-1)*sizeof(CollSeq*);
96659 sz = sizeof(VdbeSorter) + nWorker * sizeof(SortSubtask);
96660
96661 pSorter = (VdbeSorter*)sqlite3DbMallocZero(db, n: sz + szKeyInfo);
96662 pCsr->uc.pSorter = pSorter;
96663 if( pSorter==0 ){
96664 rc = SQLITE_NOMEM_BKPT;
96665 }else{
96666 Btree *pBt = db->aDb[0].pBt;
96667 pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz);
96668 memcpy(dest: pKeyInfo, src: pCsr->pKeyInfo, n: szKeyInfo);
96669 pKeyInfo->db = 0;
96670 if( nField && nWorker==0 ){
96671 pKeyInfo->nKeyField = nField;
96672 }
96673 sqlite3BtreeEnter(p: pBt);
96674 pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(p: pBt);
96675 sqlite3BtreeLeave(p: pBt);
96676 pSorter->nTask = nWorker + 1;
96677 pSorter->iPrev = (u8)(nWorker - 1);
96678 pSorter->bUseThreads = (pSorter->nTask>1);
96679 pSorter->db = db;
96680 for(i=0; i<pSorter->nTask; i++){
96681 SortSubtask *pTask = &pSorter->aTask[i];
96682 pTask->pSorter = pSorter;
96683 }
96684
96685 if( !sqlite3TempInMemory(db) ){
96686 i64 mxCache; /* Cache size in bytes*/
96687 u32 szPma = sqlite3GlobalConfig.szPma;
96688 pSorter->mnPmaSize = szPma * pgsz;
96689
96690 mxCache = db->aDb[0].pSchema->cache_size;
96691 if( mxCache<0 ){
96692 /* A negative cache-size value C indicates that the cache is abs(C)
96693 ** KiB in size. */
96694 mxCache = mxCache * -1024;
96695 }else{
96696 mxCache = mxCache * pgsz;
96697 }
96698 mxCache = MIN(mxCache, SQLITE_MAX_PMASZ);
96699 pSorter->mxPmaSize = MAX(pSorter->mnPmaSize, (int)mxCache);
96700
96701 /* Avoid large memory allocations if the application has requested
96702 ** SQLITE_CONFIG_SMALL_MALLOC. */
96703 if( sqlite3GlobalConfig.bSmallMalloc==0 ){
96704 assert( pSorter->iMemory==0 );
96705 pSorter->nMemory = pgsz;
96706 pSorter->list.aMemory = (u8*)sqlite3Malloc(n: pgsz);
96707 if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM_BKPT;
96708 }
96709 }
96710
96711 if( pKeyInfo->nAllField<13
96712 && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl)
96713 && (pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL)==0
96714 ){
96715 pSorter->typeMask = SORTER_TYPE_INTEGER | SORTER_TYPE_TEXT;
96716 }
96717 }
96718
96719 return rc;
96720}
96721#undef nWorker /* Defined at the top of this function */
96722
96723/*
96724** Free the list of sorted records starting at pRecord.
96725*/
96726static void vdbeSorterRecordFree(sqlite3 *db, SorterRecord *pRecord){
96727 SorterRecord *p;
96728 SorterRecord *pNext;
96729 for(p=pRecord; p; p=pNext){
96730 pNext = p->u.pNext;
96731 sqlite3DbFree(db, p);
96732 }
96733}
96734
96735/*
96736** Free all resources owned by the object indicated by argument pTask. All
96737** fields of *pTask are zeroed before returning.
96738*/
96739static void vdbeSortSubtaskCleanup(sqlite3 *db, SortSubtask *pTask){
96740 sqlite3DbFree(db, p: pTask->pUnpacked);
96741#if SQLITE_MAX_WORKER_THREADS>0
96742 /* pTask->list.aMemory can only be non-zero if it was handed memory
96743 ** from the main thread. That only occurs SQLITE_MAX_WORKER_THREADS>0 */
96744 if( pTask->list.aMemory ){
96745 sqlite3_free(p: pTask->list.aMemory);
96746 }else
96747#endif
96748 {
96749 assert( pTask->list.aMemory==0 );
96750 vdbeSorterRecordFree(db: 0, pRecord: pTask->list.pList);
96751 }
96752 if( pTask->file.pFd ){
96753 sqlite3OsCloseFree(pFile: pTask->file.pFd);
96754 }
96755 if( pTask->file2.pFd ){
96756 sqlite3OsCloseFree(pFile: pTask->file2.pFd);
96757 }
96758 memset(s: pTask, c: 0, n: sizeof(SortSubtask));
96759}
96760
96761#ifdef SQLITE_DEBUG_SORTER_THREADS
96762static void vdbeSorterWorkDebug(SortSubtask *pTask, const char *zEvent){
96763 i64 t;
96764 int iTask = (pTask - pTask->pSorter->aTask);
96765 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
96766 fprintf(stderr, "%lld:%d %s\n", t, iTask, zEvent);
96767}
96768static void vdbeSorterRewindDebug(const char *zEvent){
96769 i64 t = 0;
96770 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
96771 if( ALWAYS(pVfs) ) sqlite3OsCurrentTimeInt64(pVfs, &t);
96772 fprintf(stderr, "%lld:X %s\n", t, zEvent);
96773}
96774static void vdbeSorterPopulateDebug(
96775 SortSubtask *pTask,
96776 const char *zEvent
96777){
96778 i64 t;
96779 int iTask = (pTask - pTask->pSorter->aTask);
96780 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
96781 fprintf(stderr, "%lld:bg%d %s\n", t, iTask, zEvent);
96782}
96783static void vdbeSorterBlockDebug(
96784 SortSubtask *pTask,
96785 int bBlocked,
96786 const char *zEvent
96787){
96788 if( bBlocked ){
96789 i64 t;
96790 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
96791 fprintf(stderr, "%lld:main %s\n", t, zEvent);
96792 }
96793}
96794#else
96795# define vdbeSorterWorkDebug(x,y)
96796# define vdbeSorterRewindDebug(y)
96797# define vdbeSorterPopulateDebug(x,y)
96798# define vdbeSorterBlockDebug(x,y,z)
96799#endif
96800
96801#if SQLITE_MAX_WORKER_THREADS>0
96802/*
96803** Join thread pTask->thread.
96804*/
96805static int vdbeSorterJoinThread(SortSubtask *pTask){
96806 int rc = SQLITE_OK;
96807 if( pTask->pThread ){
96808#ifdef SQLITE_DEBUG_SORTER_THREADS
96809 int bDone = pTask->bDone;
96810#endif
96811 void *pRet = SQLITE_INT_TO_PTR(SQLITE_ERROR);
96812 vdbeSorterBlockDebug(pTask, !bDone, "enter");
96813 (void)sqlite3ThreadJoin(p: pTask->pThread, ppOut: &pRet);
96814 vdbeSorterBlockDebug(pTask, !bDone, "exit");
96815 rc = SQLITE_PTR_TO_INT(pRet);
96816 assert( pTask->bDone==1 );
96817 pTask->bDone = 0;
96818 pTask->pThread = 0;
96819 }
96820 return rc;
96821}
96822
96823/*
96824** Launch a background thread to run xTask(pIn).
96825*/
96826static int vdbeSorterCreateThread(
96827 SortSubtask *pTask, /* Thread will use this task object */
96828 void *(*xTask)(void*), /* Routine to run in a separate thread */
96829 void *pIn /* Argument passed into xTask() */
96830){
96831 assert( pTask->pThread==0 && pTask->bDone==0 );
96832 return sqlite3ThreadCreate(ppThread: &pTask->pThread, xTask, pIn);
96833}
96834
96835/*
96836** Join all outstanding threads launched by SorterWrite() to create
96837** level-0 PMAs.
96838*/
96839static int vdbeSorterJoinAll(VdbeSorter *pSorter, int rcin){
96840 int rc = rcin;
96841 int i;
96842
96843 /* This function is always called by the main user thread.
96844 **
96845 ** If this function is being called after SorterRewind() has been called,
96846 ** it is possible that thread pSorter->aTask[pSorter->nTask-1].pThread
96847 ** is currently attempt to join one of the other threads. To avoid a race
96848 ** condition where this thread also attempts to join the same object, join
96849 ** thread pSorter->aTask[pSorter->nTask-1].pThread first. */
96850 for(i=pSorter->nTask-1; i>=0; i--){
96851 SortSubtask *pTask = &pSorter->aTask[i];
96852 int rc2 = vdbeSorterJoinThread(pTask);
96853 if( rc==SQLITE_OK ) rc = rc2;
96854 }
96855 return rc;
96856}
96857#else
96858# define vdbeSorterJoinAll(x,rcin) (rcin)
96859# define vdbeSorterJoinThread(pTask) SQLITE_OK
96860#endif
96861
96862/*
96863** Allocate a new MergeEngine object capable of handling up to
96864** nReader PmaReader inputs.
96865**
96866** nReader is automatically rounded up to the next power of two.
96867** nReader may not exceed SORTER_MAX_MERGE_COUNT even after rounding up.
96868*/
96869static MergeEngine *vdbeMergeEngineNew(int nReader){
96870 int N = 2; /* Smallest power of two >= nReader */
96871 int nByte; /* Total bytes of space to allocate */
96872 MergeEngine *pNew; /* Pointer to allocated object to return */
96873
96874 assert( nReader<=SORTER_MAX_MERGE_COUNT );
96875
96876 while( N<nReader ) N += N;
96877 nByte = sizeof(MergeEngine) + N * (sizeof(int) + sizeof(PmaReader));
96878
96879 pNew = sqlite3FaultSim(iTest: 100) ? 0 : (MergeEngine*)sqlite3MallocZero(n: nByte);
96880 if( pNew ){
96881 pNew->nTree = N;
96882 pNew->pTask = 0;
96883 pNew->aReadr = (PmaReader*)&pNew[1];
96884 pNew->aTree = (int*)&pNew->aReadr[N];
96885 }
96886 return pNew;
96887}
96888
96889/*
96890** Free the MergeEngine object passed as the only argument.
96891*/
96892static void vdbeMergeEngineFree(MergeEngine *pMerger){
96893 int i;
96894 if( pMerger ){
96895 for(i=0; i<pMerger->nTree; i++){
96896 vdbePmaReaderClear(pReadr: &pMerger->aReadr[i]);
96897 }
96898 }
96899 sqlite3_free(p: pMerger);
96900}
96901
96902/*
96903** Free all resources associated with the IncrMerger object indicated by
96904** the first argument.
96905*/
96906static void vdbeIncrFree(IncrMerger *pIncr){
96907 if( pIncr ){
96908#if SQLITE_MAX_WORKER_THREADS>0
96909 if( pIncr->bUseThread ){
96910 vdbeSorterJoinThread(pTask: pIncr->pTask);
96911 if( pIncr->aFile[0].pFd ) sqlite3OsCloseFree(pFile: pIncr->aFile[0].pFd);
96912 if( pIncr->aFile[1].pFd ) sqlite3OsCloseFree(pFile: pIncr->aFile[1].pFd);
96913 }
96914#endif
96915 vdbeMergeEngineFree(pMerger: pIncr->pMerger);
96916 sqlite3_free(p: pIncr);
96917 }
96918}
96919
96920/*
96921** Reset a sorting cursor back to its original empty state.
96922*/
96923SQLITE_PRIVATE void sqlite3VdbeSorterReset(sqlite3 *db, VdbeSorter *pSorter){
96924 int i;
96925 (void)vdbeSorterJoinAll(pSorter, SQLITE_OK);
96926 assert( pSorter->bUseThreads || pSorter->pReader==0 );
96927#if SQLITE_MAX_WORKER_THREADS>0
96928 if( pSorter->pReader ){
96929 vdbePmaReaderClear(pReadr: pSorter->pReader);
96930 sqlite3DbFree(db, p: pSorter->pReader);
96931 pSorter->pReader = 0;
96932 }
96933#endif
96934 vdbeMergeEngineFree(pMerger: pSorter->pMerger);
96935 pSorter->pMerger = 0;
96936 for(i=0; i<pSorter->nTask; i++){
96937 SortSubtask *pTask = &pSorter->aTask[i];
96938 vdbeSortSubtaskCleanup(db, pTask);
96939 pTask->pSorter = pSorter;
96940 }
96941 if( pSorter->list.aMemory==0 ){
96942 vdbeSorterRecordFree(db: 0, pRecord: pSorter->list.pList);
96943 }
96944 pSorter->list.pList = 0;
96945 pSorter->list.szPMA = 0;
96946 pSorter->bUsePMA = 0;
96947 pSorter->iMemory = 0;
96948 pSorter->mxKeysize = 0;
96949 sqlite3DbFree(db, p: pSorter->pUnpacked);
96950 pSorter->pUnpacked = 0;
96951}
96952
96953/*
96954** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
96955*/
96956SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){
96957 VdbeSorter *pSorter;
96958 assert( pCsr->eCurType==CURTYPE_SORTER );
96959 pSorter = pCsr->uc.pSorter;
96960 if( pSorter ){
96961 sqlite3VdbeSorterReset(db, pSorter);
96962 sqlite3_free(p: pSorter->list.aMemory);
96963 sqlite3DbFree(db, p: pSorter);
96964 pCsr->uc.pSorter = 0;
96965 }
96966}
96967
96968#if SQLITE_MAX_MMAP_SIZE>0
96969/*
96970** The first argument is a file-handle open on a temporary file. The file
96971** is guaranteed to be nByte bytes or smaller in size. This function
96972** attempts to extend the file to nByte bytes in size and to ensure that
96973** the VFS has memory mapped it.
96974**
96975** Whether or not the file does end up memory mapped of course depends on
96976** the specific VFS implementation.
96977*/
96978static void vdbeSorterExtendFile(sqlite3 *db, sqlite3_file *pFd, i64 nByte){
96979 if( nByte<=(i64)(db->nMaxSorterMmap) && pFd->pMethods->iVersion>=3 ){
96980 void *p = 0;
96981 int chunksize = 4*1024;
96982 sqlite3OsFileControlHint(id: pFd, SQLITE_FCNTL_CHUNK_SIZE, pArg: &chunksize);
96983 sqlite3OsFileControlHint(id: pFd, SQLITE_FCNTL_SIZE_HINT, pArg: &nByte);
96984 sqlite3OsFetch(id: pFd, iOff: 0, iAmt: (int)nByte, pp: &p);
96985 if( p ) sqlite3OsUnfetch(id: pFd, iOff: 0, p);
96986 }
96987}
96988#else
96989# define vdbeSorterExtendFile(x,y,z)
96990#endif
96991
96992/*
96993** Allocate space for a file-handle and open a temporary file. If successful,
96994** set *ppFd to point to the malloc'd file-handle and return SQLITE_OK.
96995** Otherwise, set *ppFd to 0 and return an SQLite error code.
96996*/
96997static int vdbeSorterOpenTempFile(
96998 sqlite3 *db, /* Database handle doing sort */
96999 i64 nExtend, /* Attempt to extend file to this size */
97000 sqlite3_file **ppFd
97001){
97002 int rc;
97003 if( sqlite3FaultSim(iTest: 202) ) return SQLITE_IOERR_ACCESS;
97004 rc = sqlite3OsOpenMalloc(pVfs: db->pVfs, zFile: 0, ppFile: ppFd,
97005 SQLITE_OPEN_TEMP_JOURNAL |
97006 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
97007 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE, pOutFlags: &rc
97008 );
97009 if( rc==SQLITE_OK ){
97010 i64 max = SQLITE_MAX_MMAP_SIZE;
97011 sqlite3OsFileControlHint(id: *ppFd, SQLITE_FCNTL_MMAP_SIZE, pArg: (void*)&max);
97012 if( nExtend>0 ){
97013 vdbeSorterExtendFile(db, pFd: *ppFd, nByte: nExtend);
97014 }
97015 }
97016 return rc;
97017}
97018
97019/*
97020** If it has not already been allocated, allocate the UnpackedRecord
97021** structure at pTask->pUnpacked. Return SQLITE_OK if successful (or
97022** if no allocation was required), or SQLITE_NOMEM otherwise.
97023*/
97024static int vdbeSortAllocUnpacked(SortSubtask *pTask){
97025 if( pTask->pUnpacked==0 ){
97026 pTask->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo: pTask->pSorter->pKeyInfo);
97027 if( pTask->pUnpacked==0 ) return SQLITE_NOMEM_BKPT;
97028 pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nKeyField;
97029 pTask->pUnpacked->errCode = 0;
97030 }
97031 return SQLITE_OK;
97032}
97033
97034
97035/*
97036** Merge the two sorted lists p1 and p2 into a single list.
97037*/
97038static SorterRecord *vdbeSorterMerge(
97039 SortSubtask *pTask, /* Calling thread context */
97040 SorterRecord *p1, /* First list to merge */
97041 SorterRecord *p2 /* Second list to merge */
97042){
97043 SorterRecord *pFinal = 0;
97044 SorterRecord **pp = &pFinal;
97045 int bCached = 0;
97046
97047 assert( p1!=0 && p2!=0 );
97048 for(;;){
97049 int res;
97050 res = pTask->xCompare(
97051 pTask, &bCached, SRVAL(p1), p1->nVal, SRVAL(p2), p2->nVal
97052 );
97053
97054 if( res<=0 ){
97055 *pp = p1;
97056 pp = &p1->u.pNext;
97057 p1 = p1->u.pNext;
97058 if( p1==0 ){
97059 *pp = p2;
97060 break;
97061 }
97062 }else{
97063 *pp = p2;
97064 pp = &p2->u.pNext;
97065 p2 = p2->u.pNext;
97066 bCached = 0;
97067 if( p2==0 ){
97068 *pp = p1;
97069 break;
97070 }
97071 }
97072 }
97073 return pFinal;
97074}
97075
97076/*
97077** Return the SorterCompare function to compare values collected by the
97078** sorter object passed as the only argument.
97079*/
97080static SorterCompare vdbeSorterGetCompare(VdbeSorter *p){
97081 if( p->typeMask==SORTER_TYPE_INTEGER ){
97082 return vdbeSorterCompareInt;
97083 }else if( p->typeMask==SORTER_TYPE_TEXT ){
97084 return vdbeSorterCompareText;
97085 }
97086 return vdbeSorterCompare;
97087}
97088
97089/*
97090** Sort the linked list of records headed at pTask->pList. Return
97091** SQLITE_OK if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if
97092** an error occurs.
97093*/
97094static int vdbeSorterSort(SortSubtask *pTask, SorterList *pList){
97095 int i;
97096 SorterRecord *p;
97097 int rc;
97098 SorterRecord *aSlot[64];
97099
97100 rc = vdbeSortAllocUnpacked(pTask);
97101 if( rc!=SQLITE_OK ) return rc;
97102
97103 p = pList->pList;
97104 pTask->xCompare = vdbeSorterGetCompare(p: pTask->pSorter);
97105 memset(s: aSlot, c: 0, n: sizeof(aSlot));
97106
97107 while( p ){
97108 SorterRecord *pNext;
97109 if( pList->aMemory ){
97110 if( (u8*)p==pList->aMemory ){
97111 pNext = 0;
97112 }else{
97113 assert( p->u.iNext<sqlite3MallocSize(pList->aMemory) );
97114 pNext = (SorterRecord*)&pList->aMemory[p->u.iNext];
97115 }
97116 }else{
97117 pNext = p->u.pNext;
97118 }
97119
97120 p->u.pNext = 0;
97121 for(i=0; aSlot[i]; i++){
97122 p = vdbeSorterMerge(pTask, p1: p, p2: aSlot[i]);
97123 aSlot[i] = 0;
97124 }
97125 aSlot[i] = p;
97126 p = pNext;
97127 }
97128
97129 p = 0;
97130 for(i=0; i<ArraySize(aSlot); i++){
97131 if( aSlot[i]==0 ) continue;
97132 p = p ? vdbeSorterMerge(pTask, p1: p, p2: aSlot[i]) : aSlot[i];
97133 }
97134 pList->pList = p;
97135
97136 assert( pTask->pUnpacked->errCode==SQLITE_OK
97137 || pTask->pUnpacked->errCode==SQLITE_NOMEM
97138 );
97139 return pTask->pUnpacked->errCode;
97140}
97141
97142/*
97143** Initialize a PMA-writer object.
97144*/
97145static void vdbePmaWriterInit(
97146 sqlite3_file *pFd, /* File handle to write to */
97147 PmaWriter *p, /* Object to populate */
97148 int nBuf, /* Buffer size */
97149 i64 iStart /* Offset of pFd to begin writing at */
97150){
97151 memset(s: p, c: 0, n: sizeof(PmaWriter));
97152 p->aBuffer = (u8*)sqlite3Malloc(n: nBuf);
97153 if( !p->aBuffer ){
97154 p->eFWErr = SQLITE_NOMEM_BKPT;
97155 }else{
97156 p->iBufEnd = p->iBufStart = (iStart % nBuf);
97157 p->iWriteOff = iStart - p->iBufStart;
97158 p->nBuffer = nBuf;
97159 p->pFd = pFd;
97160 }
97161}
97162
97163/*
97164** Write nData bytes of data to the PMA. Return SQLITE_OK
97165** if successful, or an SQLite error code if an error occurs.
97166*/
97167static void vdbePmaWriteBlob(PmaWriter *p, u8 *pData, int nData){
97168 int nRem = nData;
97169 while( nRem>0 && p->eFWErr==0 ){
97170 int nCopy = nRem;
97171 if( nCopy>(p->nBuffer - p->iBufEnd) ){
97172 nCopy = p->nBuffer - p->iBufEnd;
97173 }
97174
97175 memcpy(dest: &p->aBuffer[p->iBufEnd], src: &pData[nData-nRem], n: nCopy);
97176 p->iBufEnd += nCopy;
97177 if( p->iBufEnd==p->nBuffer ){
97178 p->eFWErr = sqlite3OsWrite(id: p->pFd,
97179 pBuf: &p->aBuffer[p->iBufStart], amt: p->iBufEnd - p->iBufStart,
97180 offset: p->iWriteOff + p->iBufStart
97181 );
97182 p->iBufStart = p->iBufEnd = 0;
97183 p->iWriteOff += p->nBuffer;
97184 }
97185 assert( p->iBufEnd<p->nBuffer );
97186
97187 nRem -= nCopy;
97188 }
97189}
97190
97191/*
97192** Flush any buffered data to disk and clean up the PMA-writer object.
97193** The results of using the PMA-writer after this call are undefined.
97194** Return SQLITE_OK if flushing the buffered data succeeds or is not
97195** required. Otherwise, return an SQLite error code.
97196**
97197** Before returning, set *piEof to the offset immediately following the
97198** last byte written to the file.
97199*/
97200static int vdbePmaWriterFinish(PmaWriter *p, i64 *piEof){
97201 int rc;
97202 if( p->eFWErr==0 && ALWAYS(p->aBuffer) && p->iBufEnd>p->iBufStart ){
97203 p->eFWErr = sqlite3OsWrite(id: p->pFd,
97204 pBuf: &p->aBuffer[p->iBufStart], amt: p->iBufEnd - p->iBufStart,
97205 offset: p->iWriteOff + p->iBufStart
97206 );
97207 }
97208 *piEof = (p->iWriteOff + p->iBufEnd);
97209 sqlite3_free(p: p->aBuffer);
97210 rc = p->eFWErr;
97211 memset(s: p, c: 0, n: sizeof(PmaWriter));
97212 return rc;
97213}
97214
97215/*
97216** Write value iVal encoded as a varint to the PMA. Return
97217** SQLITE_OK if successful, or an SQLite error code if an error occurs.
97218*/
97219static void vdbePmaWriteVarint(PmaWriter *p, u64 iVal){
97220 int nByte;
97221 u8 aByte[10];
97222 nByte = sqlite3PutVarint(p: aByte, v: iVal);
97223 vdbePmaWriteBlob(p, pData: aByte, nData: nByte);
97224}
97225
97226/*
97227** Write the current contents of in-memory linked-list pList to a level-0
97228** PMA in the temp file belonging to sub-task pTask. Return SQLITE_OK if
97229** successful, or an SQLite error code otherwise.
97230**
97231** The format of a PMA is:
97232**
97233** * A varint. This varint contains the total number of bytes of content
97234** in the PMA (not including the varint itself).
97235**
97236** * One or more records packed end-to-end in order of ascending keys.
97237** Each record consists of a varint followed by a blob of data (the
97238** key). The varint is the number of bytes in the blob of data.
97239*/
97240static int vdbeSorterListToPMA(SortSubtask *pTask, SorterList *pList){
97241 sqlite3 *db = pTask->pSorter->db;
97242 int rc = SQLITE_OK; /* Return code */
97243 PmaWriter writer; /* Object used to write to the file */
97244
97245#ifdef SQLITE_DEBUG
97246 /* Set iSz to the expected size of file pTask->file after writing the PMA.
97247 ** This is used by an assert() statement at the end of this function. */
97248 i64 iSz = pList->szPMA + sqlite3VarintLen(pList->szPMA) + pTask->file.iEof;
97249#endif
97250
97251 vdbeSorterWorkDebug(pTask, "enter");
97252 memset(s: &writer, c: 0, n: sizeof(PmaWriter));
97253 assert( pList->szPMA>0 );
97254
97255 /* If the first temporary PMA file has not been opened, open it now. */
97256 if( pTask->file.pFd==0 ){
97257 rc = vdbeSorterOpenTempFile(db, nExtend: 0, ppFd: &pTask->file.pFd);
97258 assert( rc!=SQLITE_OK || pTask->file.pFd );
97259 assert( pTask->file.iEof==0 );
97260 assert( pTask->nPMA==0 );
97261 }
97262
97263 /* Try to get the file to memory map */
97264 if( rc==SQLITE_OK ){
97265 vdbeSorterExtendFile(db, pFd: pTask->file.pFd, nByte: pTask->file.iEof+pList->szPMA+9);
97266 }
97267
97268 /* Sort the list */
97269 if( rc==SQLITE_OK ){
97270 rc = vdbeSorterSort(pTask, pList);
97271 }
97272
97273 if( rc==SQLITE_OK ){
97274 SorterRecord *p;
97275 SorterRecord *pNext = 0;
97276
97277 vdbePmaWriterInit(pFd: pTask->file.pFd, p: &writer, nBuf: pTask->pSorter->pgsz,
97278 iStart: pTask->file.iEof);
97279 pTask->nPMA++;
97280 vdbePmaWriteVarint(p: &writer, iVal: pList->szPMA);
97281 for(p=pList->pList; p; p=pNext){
97282 pNext = p->u.pNext;
97283 vdbePmaWriteVarint(p: &writer, iVal: p->nVal);
97284 vdbePmaWriteBlob(p: &writer, SRVAL(p), nData: p->nVal);
97285 if( pList->aMemory==0 ) sqlite3_free(p);
97286 }
97287 pList->pList = p;
97288 rc = vdbePmaWriterFinish(p: &writer, piEof: &pTask->file.iEof);
97289 }
97290
97291 vdbeSorterWorkDebug(pTask, "exit");
97292 assert( rc!=SQLITE_OK || pList->pList==0 );
97293 assert( rc!=SQLITE_OK || pTask->file.iEof==iSz );
97294 return rc;
97295}
97296
97297/*
97298** Advance the MergeEngine to its next entry.
97299** Set *pbEof to true there is no next entry because
97300** the MergeEngine has reached the end of all its inputs.
97301**
97302** Return SQLITE_OK if successful or an error code if an error occurs.
97303*/
97304static int vdbeMergeEngineStep(
97305 MergeEngine *pMerger, /* The merge engine to advance to the next row */
97306 int *pbEof /* Set TRUE at EOF. Set false for more content */
97307){
97308 int rc;
97309 int iPrev = pMerger->aTree[1];/* Index of PmaReader to advance */
97310 SortSubtask *pTask = pMerger->pTask;
97311
97312 /* Advance the current PmaReader */
97313 rc = vdbePmaReaderNext(pReadr: &pMerger->aReadr[iPrev]);
97314
97315 /* Update contents of aTree[] */
97316 if( rc==SQLITE_OK ){
97317 int i; /* Index of aTree[] to recalculate */
97318 PmaReader *pReadr1; /* First PmaReader to compare */
97319 PmaReader *pReadr2; /* Second PmaReader to compare */
97320 int bCached = 0;
97321
97322 /* Find the first two PmaReaders to compare. The one that was just
97323 ** advanced (iPrev) and the one next to it in the array. */
97324 pReadr1 = &pMerger->aReadr[(iPrev & 0xFFFE)];
97325 pReadr2 = &pMerger->aReadr[(iPrev | 0x0001)];
97326
97327 for(i=(pMerger->nTree+iPrev)/2; i>0; i=i/2){
97328 /* Compare pReadr1 and pReadr2. Store the result in variable iRes. */
97329 int iRes;
97330 if( pReadr1->pFd==0 ){
97331 iRes = +1;
97332 }else if( pReadr2->pFd==0 ){
97333 iRes = -1;
97334 }else{
97335 iRes = pTask->xCompare(pTask, &bCached,
97336 pReadr1->aKey, pReadr1->nKey, pReadr2->aKey, pReadr2->nKey
97337 );
97338 }
97339
97340 /* If pReadr1 contained the smaller value, set aTree[i] to its index.
97341 ** Then set pReadr2 to the next PmaReader to compare to pReadr1. In this
97342 ** case there is no cache of pReadr2 in pTask->pUnpacked, so set
97343 ** pKey2 to point to the record belonging to pReadr2.
97344 **
97345 ** Alternatively, if pReadr2 contains the smaller of the two values,
97346 ** set aTree[i] to its index and update pReadr1. If vdbeSorterCompare()
97347 ** was actually called above, then pTask->pUnpacked now contains
97348 ** a value equivalent to pReadr2. So set pKey2 to NULL to prevent
97349 ** vdbeSorterCompare() from decoding pReadr2 again.
97350 **
97351 ** If the two values were equal, then the value from the oldest
97352 ** PMA should be considered smaller. The VdbeSorter.aReadr[] array
97353 ** is sorted from oldest to newest, so pReadr1 contains older values
97354 ** than pReadr2 iff (pReadr1<pReadr2). */
97355 if( iRes<0 || (iRes==0 && pReadr1<pReadr2) ){
97356 pMerger->aTree[i] = (int)(pReadr1 - pMerger->aReadr);
97357 pReadr2 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
97358 bCached = 0;
97359 }else{
97360 if( pReadr1->pFd ) bCached = 0;
97361 pMerger->aTree[i] = (int)(pReadr2 - pMerger->aReadr);
97362 pReadr1 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
97363 }
97364 }
97365 *pbEof = (pMerger->aReadr[pMerger->aTree[1]].pFd==0);
97366 }
97367
97368 return (rc==SQLITE_OK ? pTask->pUnpacked->errCode : rc);
97369}
97370
97371#if SQLITE_MAX_WORKER_THREADS>0
97372/*
97373** The main routine for background threads that write level-0 PMAs.
97374*/
97375static void *vdbeSorterFlushThread(void *pCtx){
97376 SortSubtask *pTask = (SortSubtask*)pCtx;
97377 int rc; /* Return code */
97378 assert( pTask->bDone==0 );
97379 rc = vdbeSorterListToPMA(pTask, pList: &pTask->list);
97380 pTask->bDone = 1;
97381 return SQLITE_INT_TO_PTR(rc);
97382}
97383#endif /* SQLITE_MAX_WORKER_THREADS>0 */
97384
97385/*
97386** Flush the current contents of VdbeSorter.list to a new PMA, possibly
97387** using a background thread.
97388*/
97389static int vdbeSorterFlushPMA(VdbeSorter *pSorter){
97390#if SQLITE_MAX_WORKER_THREADS==0
97391 pSorter->bUsePMA = 1;
97392 return vdbeSorterListToPMA(&pSorter->aTask[0], &pSorter->list);
97393#else
97394 int rc = SQLITE_OK;
97395 int i;
97396 SortSubtask *pTask = 0; /* Thread context used to create new PMA */
97397 int nWorker = (pSorter->nTask-1);
97398
97399 /* Set the flag to indicate that at least one PMA has been written.
97400 ** Or will be, anyhow. */
97401 pSorter->bUsePMA = 1;
97402
97403 /* Select a sub-task to sort and flush the current list of in-memory
97404 ** records to disk. If the sorter is running in multi-threaded mode,
97405 ** round-robin between the first (pSorter->nTask-1) tasks. Except, if
97406 ** the background thread from a sub-tasks previous turn is still running,
97407 ** skip it. If the first (pSorter->nTask-1) sub-tasks are all still busy,
97408 ** fall back to using the final sub-task. The first (pSorter->nTask-1)
97409 ** sub-tasks are prefered as they use background threads - the final
97410 ** sub-task uses the main thread. */
97411 for(i=0; i<nWorker; i++){
97412 int iTest = (pSorter->iPrev + i + 1) % nWorker;
97413 pTask = &pSorter->aTask[iTest];
97414 if( pTask->bDone ){
97415 rc = vdbeSorterJoinThread(pTask);
97416 }
97417 if( rc!=SQLITE_OK || pTask->pThread==0 ) break;
97418 }
97419
97420 if( rc==SQLITE_OK ){
97421 if( i==nWorker ){
97422 /* Use the foreground thread for this operation */
97423 rc = vdbeSorterListToPMA(pTask: &pSorter->aTask[nWorker], pList: &pSorter->list);
97424 }else{
97425 /* Launch a background thread for this operation */
97426 u8 *aMem;
97427 void *pCtx;
97428
97429 assert( pTask!=0 );
97430 assert( pTask->pThread==0 && pTask->bDone==0 );
97431 assert( pTask->list.pList==0 );
97432 assert( pTask->list.aMemory==0 || pSorter->list.aMemory!=0 );
97433
97434 aMem = pTask->list.aMemory;
97435 pCtx = (void*)pTask;
97436 pSorter->iPrev = (u8)(pTask - pSorter->aTask);
97437 pTask->list = pSorter->list;
97438 pSorter->list.pList = 0;
97439 pSorter->list.szPMA = 0;
97440 if( aMem ){
97441 pSorter->list.aMemory = aMem;
97442 pSorter->nMemory = sqlite3MallocSize(p: aMem);
97443 }else if( pSorter->list.aMemory ){
97444 pSorter->list.aMemory = sqlite3Malloc(n: pSorter->nMemory);
97445 if( !pSorter->list.aMemory ) return SQLITE_NOMEM_BKPT;
97446 }
97447
97448 rc = vdbeSorterCreateThread(pTask, xTask: vdbeSorterFlushThread, pIn: pCtx);
97449 }
97450 }
97451
97452 return rc;
97453#endif /* SQLITE_MAX_WORKER_THREADS!=0 */
97454}
97455
97456/*
97457** Add a record to the sorter.
97458*/
97459SQLITE_PRIVATE int sqlite3VdbeSorterWrite(
97460 const VdbeCursor *pCsr, /* Sorter cursor */
97461 Mem *pVal /* Memory cell containing record */
97462){
97463 VdbeSorter *pSorter;
97464 int rc = SQLITE_OK; /* Return Code */
97465 SorterRecord *pNew; /* New list element */
97466 int bFlush; /* True to flush contents of memory to PMA */
97467 int nReq; /* Bytes of memory required */
97468 int nPMA; /* Bytes of PMA space required */
97469 int t; /* serial type of first record field */
97470
97471 assert( pCsr->eCurType==CURTYPE_SORTER );
97472 pSorter = pCsr->uc.pSorter;
97473 getVarint32NR((const u8*)&pVal->z[1], t);
97474 if( t>0 && t<10 && t!=7 ){
97475 pSorter->typeMask &= SORTER_TYPE_INTEGER;
97476 }else if( t>10 && (t & 0x01) ){
97477 pSorter->typeMask &= SORTER_TYPE_TEXT;
97478 }else{
97479 pSorter->typeMask = 0;
97480 }
97481
97482 assert( pSorter );
97483
97484 /* Figure out whether or not the current contents of memory should be
97485 ** flushed to a PMA before continuing. If so, do so.
97486 **
97487 ** If using the single large allocation mode (pSorter->aMemory!=0), then
97488 ** flush the contents of memory to a new PMA if (a) at least one value is
97489 ** already in memory and (b) the new value will not fit in memory.
97490 **
97491 ** Or, if using separate allocations for each record, flush the contents
97492 ** of memory to a PMA if either of the following are true:
97493 **
97494 ** * The total memory allocated for the in-memory list is greater
97495 ** than (page-size * cache-size), or
97496 **
97497 ** * The total memory allocated for the in-memory list is greater
97498 ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
97499 */
97500 nReq = pVal->n + sizeof(SorterRecord);
97501 nPMA = pVal->n + sqlite3VarintLen(v: pVal->n);
97502 if( pSorter->mxPmaSize ){
97503 if( pSorter->list.aMemory ){
97504 bFlush = pSorter->iMemory && (pSorter->iMemory+nReq) > pSorter->mxPmaSize;
97505 }else{
97506 bFlush = (
97507 (pSorter->list.szPMA > pSorter->mxPmaSize)
97508 || (pSorter->list.szPMA > pSorter->mnPmaSize && sqlite3HeapNearlyFull())
97509 );
97510 }
97511 if( bFlush ){
97512 rc = vdbeSorterFlushPMA(pSorter);
97513 pSorter->list.szPMA = 0;
97514 pSorter->iMemory = 0;
97515 assert( rc!=SQLITE_OK || pSorter->list.pList==0 );
97516 }
97517 }
97518
97519 pSorter->list.szPMA += nPMA;
97520 if( nPMA>pSorter->mxKeysize ){
97521 pSorter->mxKeysize = nPMA;
97522 }
97523
97524 if( pSorter->list.aMemory ){
97525 int nMin = pSorter->iMemory + nReq;
97526
97527 if( nMin>pSorter->nMemory ){
97528 u8 *aNew;
97529 sqlite3_int64 nNew = 2 * (sqlite3_int64)pSorter->nMemory;
97530 int iListOff = -1;
97531 if( pSorter->list.pList ){
97532 iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
97533 }
97534 while( nNew < nMin ) nNew = nNew*2;
97535 if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
97536 if( nNew < nMin ) nNew = nMin;
97537 aNew = sqlite3Realloc(pOld: pSorter->list.aMemory, nBytes: nNew);
97538 if( !aNew ) return SQLITE_NOMEM_BKPT;
97539 if( iListOff>=0 ){
97540 pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
97541 }
97542 pSorter->list.aMemory = aNew;
97543 pSorter->nMemory = nNew;
97544 }
97545
97546 pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
97547 pSorter->iMemory += ROUND8(nReq);
97548 if( pSorter->list.pList ){
97549 pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
97550 }
97551 }else{
97552 pNew = (SorterRecord *)sqlite3Malloc(n: nReq);
97553 if( pNew==0 ){
97554 return SQLITE_NOMEM_BKPT;
97555 }
97556 pNew->u.pNext = pSorter->list.pList;
97557 }
97558
97559 memcpy(SRVAL(pNew), src: pVal->z, n: pVal->n);
97560 pNew->nVal = pVal->n;
97561 pSorter->list.pList = pNew;
97562
97563 return rc;
97564}
97565
97566/*
97567** Read keys from pIncr->pMerger and populate pIncr->aFile[1]. The format
97568** of the data stored in aFile[1] is the same as that used by regular PMAs,
97569** except that the number-of-bytes varint is omitted from the start.
97570*/
97571static int vdbeIncrPopulate(IncrMerger *pIncr){
97572 int rc = SQLITE_OK;
97573 int rc2;
97574 i64 iStart = pIncr->iStartOff;
97575 SorterFile *pOut = &pIncr->aFile[1];
97576 SortSubtask *pTask = pIncr->pTask;
97577 MergeEngine *pMerger = pIncr->pMerger;
97578 PmaWriter writer;
97579 assert( pIncr->bEof==0 );
97580
97581 vdbeSorterPopulateDebug(pTask, "enter");
97582
97583 vdbePmaWriterInit(pFd: pOut->pFd, p: &writer, nBuf: pTask->pSorter->pgsz, iStart);
97584 while( rc==SQLITE_OK ){
97585 int dummy;
97586 PmaReader *pReader = &pMerger->aReadr[ pMerger->aTree[1] ];
97587 int nKey = pReader->nKey;
97588 i64 iEof = writer.iWriteOff + writer.iBufEnd;
97589
97590 /* Check if the output file is full or if the input has been exhausted.
97591 ** In either case exit the loop. */
97592 if( pReader->pFd==0 ) break;
97593 if( (iEof + nKey + sqlite3VarintLen(v: nKey))>(iStart + pIncr->mxSz) ) break;
97594
97595 /* Write the next key to the output. */
97596 vdbePmaWriteVarint(p: &writer, iVal: nKey);
97597 vdbePmaWriteBlob(p: &writer, pData: pReader->aKey, nData: nKey);
97598 assert( pIncr->pMerger->pTask==pTask );
97599 rc = vdbeMergeEngineStep(pMerger: pIncr->pMerger, pbEof: &dummy);
97600 }
97601
97602 rc2 = vdbePmaWriterFinish(p: &writer, piEof: &pOut->iEof);
97603 if( rc==SQLITE_OK ) rc = rc2;
97604 vdbeSorterPopulateDebug(pTask, "exit");
97605 return rc;
97606}
97607
97608#if SQLITE_MAX_WORKER_THREADS>0
97609/*
97610** The main routine for background threads that populate aFile[1] of
97611** multi-threaded IncrMerger objects.
97612*/
97613static void *vdbeIncrPopulateThread(void *pCtx){
97614 IncrMerger *pIncr = (IncrMerger*)pCtx;
97615 void *pRet = SQLITE_INT_TO_PTR( vdbeIncrPopulate(pIncr) );
97616 pIncr->pTask->bDone = 1;
97617 return pRet;
97618}
97619
97620/*
97621** Launch a background thread to populate aFile[1] of pIncr.
97622*/
97623static int vdbeIncrBgPopulate(IncrMerger *pIncr){
97624 void *p = (void*)pIncr;
97625 assert( pIncr->bUseThread );
97626 return vdbeSorterCreateThread(pTask: pIncr->pTask, xTask: vdbeIncrPopulateThread, pIn: p);
97627}
97628#endif
97629
97630/*
97631** This function is called when the PmaReader corresponding to pIncr has
97632** finished reading the contents of aFile[0]. Its purpose is to "refill"
97633** aFile[0] such that the PmaReader should start rereading it from the
97634** beginning.
97635**
97636** For single-threaded objects, this is accomplished by literally reading
97637** keys from pIncr->pMerger and repopulating aFile[0].
97638**
97639** For multi-threaded objects, all that is required is to wait until the
97640** background thread is finished (if it is not already) and then swap
97641** aFile[0] and aFile[1] in place. If the contents of pMerger have not
97642** been exhausted, this function also launches a new background thread
97643** to populate the new aFile[1].
97644**
97645** SQLITE_OK is returned on success, or an SQLite error code otherwise.
97646*/
97647static int vdbeIncrSwap(IncrMerger *pIncr){
97648 int rc = SQLITE_OK;
97649
97650#if SQLITE_MAX_WORKER_THREADS>0
97651 if( pIncr->bUseThread ){
97652 rc = vdbeSorterJoinThread(pTask: pIncr->pTask);
97653
97654 if( rc==SQLITE_OK ){
97655 SorterFile f0 = pIncr->aFile[0];
97656 pIncr->aFile[0] = pIncr->aFile[1];
97657 pIncr->aFile[1] = f0;
97658 }
97659
97660 if( rc==SQLITE_OK ){
97661 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
97662 pIncr->bEof = 1;
97663 }else{
97664 rc = vdbeIncrBgPopulate(pIncr);
97665 }
97666 }
97667 }else
97668#endif
97669 {
97670 rc = vdbeIncrPopulate(pIncr);
97671 pIncr->aFile[0] = pIncr->aFile[1];
97672 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
97673 pIncr->bEof = 1;
97674 }
97675 }
97676
97677 return rc;
97678}
97679
97680/*
97681** Allocate and return a new IncrMerger object to read data from pMerger.
97682**
97683** If an OOM condition is encountered, return NULL. In this case free the
97684** pMerger argument before returning.
97685*/
97686static int vdbeIncrMergerNew(
97687 SortSubtask *pTask, /* The thread that will be using the new IncrMerger */
97688 MergeEngine *pMerger, /* The MergeEngine that the IncrMerger will control */
97689 IncrMerger **ppOut /* Write the new IncrMerger here */
97690){
97691 int rc = SQLITE_OK;
97692 IncrMerger *pIncr = *ppOut = (IncrMerger*)
97693 (sqlite3FaultSim(iTest: 100) ? 0 : sqlite3MallocZero(n: sizeof(*pIncr)));
97694 if( pIncr ){
97695 pIncr->pMerger = pMerger;
97696 pIncr->pTask = pTask;
97697 pIncr->mxSz = MAX(pTask->pSorter->mxKeysize+9,pTask->pSorter->mxPmaSize/2);
97698 pTask->file2.iEof += pIncr->mxSz;
97699 }else{
97700 vdbeMergeEngineFree(pMerger);
97701 rc = SQLITE_NOMEM_BKPT;
97702 }
97703 assert( *ppOut!=0 || rc!=SQLITE_OK );
97704 return rc;
97705}
97706
97707#if SQLITE_MAX_WORKER_THREADS>0
97708/*
97709** Set the "use-threads" flag on object pIncr.
97710*/
97711static void vdbeIncrMergerSetThreads(IncrMerger *pIncr){
97712 pIncr->bUseThread = 1;
97713 pIncr->pTask->file2.iEof -= pIncr->mxSz;
97714}
97715#endif /* SQLITE_MAX_WORKER_THREADS>0 */
97716
97717
97718
97719/*
97720** Recompute pMerger->aTree[iOut] by comparing the next keys on the
97721** two PmaReaders that feed that entry. Neither of the PmaReaders
97722** are advanced. This routine merely does the comparison.
97723*/
97724static void vdbeMergeEngineCompare(
97725 MergeEngine *pMerger, /* Merge engine containing PmaReaders to compare */
97726 int iOut /* Store the result in pMerger->aTree[iOut] */
97727){
97728 int i1;
97729 int i2;
97730 int iRes;
97731 PmaReader *p1;
97732 PmaReader *p2;
97733
97734 assert( iOut<pMerger->nTree && iOut>0 );
97735
97736 if( iOut>=(pMerger->nTree/2) ){
97737 i1 = (iOut - pMerger->nTree/2) * 2;
97738 i2 = i1 + 1;
97739 }else{
97740 i1 = pMerger->aTree[iOut*2];
97741 i2 = pMerger->aTree[iOut*2+1];
97742 }
97743
97744 p1 = &pMerger->aReadr[i1];
97745 p2 = &pMerger->aReadr[i2];
97746
97747 if( p1->pFd==0 ){
97748 iRes = i2;
97749 }else if( p2->pFd==0 ){
97750 iRes = i1;
97751 }else{
97752 SortSubtask *pTask = pMerger->pTask;
97753 int bCached = 0;
97754 int res;
97755 assert( pTask->pUnpacked!=0 ); /* from vdbeSortSubtaskMain() */
97756 res = pTask->xCompare(
97757 pTask, &bCached, p1->aKey, p1->nKey, p2->aKey, p2->nKey
97758 );
97759 if( res<=0 ){
97760 iRes = i1;
97761 }else{
97762 iRes = i2;
97763 }
97764 }
97765
97766 pMerger->aTree[iOut] = iRes;
97767}
97768
97769/*
97770** Allowed values for the eMode parameter to vdbeMergeEngineInit()
97771** and vdbePmaReaderIncrMergeInit().
97772**
97773** Only INCRINIT_NORMAL is valid in single-threaded builds (when
97774** SQLITE_MAX_WORKER_THREADS==0). The other values are only used
97775** when there exists one or more separate worker threads.
97776*/
97777#define INCRINIT_NORMAL 0
97778#define INCRINIT_TASK 1
97779#define INCRINIT_ROOT 2
97780
97781/*
97782** Forward reference required as the vdbeIncrMergeInit() and
97783** vdbePmaReaderIncrInit() routines are called mutually recursively when
97784** building a merge tree.
97785*/
97786static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode);
97787
97788/*
97789** Initialize the MergeEngine object passed as the second argument. Once this
97790** function returns, the first key of merged data may be read from the
97791** MergeEngine object in the usual fashion.
97792**
97793** If argument eMode is INCRINIT_ROOT, then it is assumed that any IncrMerge
97794** objects attached to the PmaReader objects that the merger reads from have
97795** already been populated, but that they have not yet populated aFile[0] and
97796** set the PmaReader objects up to read from it. In this case all that is
97797** required is to call vdbePmaReaderNext() on each PmaReader to point it at
97798** its first key.
97799**
97800** Otherwise, if eMode is any value other than INCRINIT_ROOT, then use
97801** vdbePmaReaderIncrMergeInit() to initialize each PmaReader that feeds data
97802** to pMerger.
97803**
97804** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
97805*/
97806static int vdbeMergeEngineInit(
97807 SortSubtask *pTask, /* Thread that will run pMerger */
97808 MergeEngine *pMerger, /* MergeEngine to initialize */
97809 int eMode /* One of the INCRINIT_XXX constants */
97810){
97811 int rc = SQLITE_OK; /* Return code */
97812 int i; /* For looping over PmaReader objects */
97813 int nTree; /* Number of subtrees to merge */
97814
97815 /* Failure to allocate the merge would have been detected prior to
97816 ** invoking this routine */
97817 assert( pMerger!=0 );
97818
97819 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
97820 assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL );
97821
97822 /* Verify that the MergeEngine is assigned to a single thread */
97823 assert( pMerger->pTask==0 );
97824 pMerger->pTask = pTask;
97825
97826 nTree = pMerger->nTree;
97827 for(i=0; i<nTree; i++){
97828 if( SQLITE_MAX_WORKER_THREADS>0 && eMode==INCRINIT_ROOT ){
97829 /* PmaReaders should be normally initialized in order, as if they are
97830 ** reading from the same temp file this makes for more linear file IO.
97831 ** However, in the INCRINIT_ROOT case, if PmaReader aReadr[nTask-1] is
97832 ** in use it will block the vdbePmaReaderNext() call while it uses
97833 ** the main thread to fill its buffer. So calling PmaReaderNext()
97834 ** on this PmaReader before any of the multi-threaded PmaReaders takes
97835 ** better advantage of multi-processor hardware. */
97836 rc = vdbePmaReaderNext(pReadr: &pMerger->aReadr[nTree-i-1]);
97837 }else{
97838 rc = vdbePmaReaderIncrInit(pReadr: &pMerger->aReadr[i], INCRINIT_NORMAL);
97839 }
97840 if( rc!=SQLITE_OK ) return rc;
97841 }
97842
97843 for(i=pMerger->nTree-1; i>0; i--){
97844 vdbeMergeEngineCompare(pMerger, iOut: i);
97845 }
97846 return pTask->pUnpacked->errCode;
97847}
97848
97849/*
97850** The PmaReader passed as the first argument is guaranteed to be an
97851** incremental-reader (pReadr->pIncr!=0). This function serves to open
97852** and/or initialize the temp file related fields of the IncrMerge
97853** object at (pReadr->pIncr).
97854**
97855** If argument eMode is set to INCRINIT_NORMAL, then all PmaReaders
97856** in the sub-tree headed by pReadr are also initialized. Data is then
97857** loaded into the buffers belonging to pReadr and it is set to point to
97858** the first key in its range.
97859**
97860** If argument eMode is set to INCRINIT_TASK, then pReadr is guaranteed
97861** to be a multi-threaded PmaReader and this function is being called in a
97862** background thread. In this case all PmaReaders in the sub-tree are
97863** initialized as for INCRINIT_NORMAL and the aFile[1] buffer belonging to
97864** pReadr is populated. However, pReadr itself is not set up to point
97865** to its first key. A call to vdbePmaReaderNext() is still required to do
97866** that.
97867**
97868** The reason this function does not call vdbePmaReaderNext() immediately
97869** in the INCRINIT_TASK case is that vdbePmaReaderNext() assumes that it has
97870** to block on thread (pTask->thread) before accessing aFile[1]. But, since
97871** this entire function is being run by thread (pTask->thread), that will
97872** lead to the current background thread attempting to join itself.
97873**
97874** Finally, if argument eMode is set to INCRINIT_ROOT, it may be assumed
97875** that pReadr->pIncr is a multi-threaded IncrMerge objects, and that all
97876** child-trees have already been initialized using IncrInit(INCRINIT_TASK).
97877** In this case vdbePmaReaderNext() is called on all child PmaReaders and
97878** the current PmaReader set to point to the first key in its range.
97879**
97880** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
97881*/
97882static int vdbePmaReaderIncrMergeInit(PmaReader *pReadr, int eMode){
97883 int rc = SQLITE_OK;
97884 IncrMerger *pIncr = pReadr->pIncr;
97885 SortSubtask *pTask = pIncr->pTask;
97886 sqlite3 *db = pTask->pSorter->db;
97887
97888 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
97889 assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL );
97890
97891 rc = vdbeMergeEngineInit(pTask, pMerger: pIncr->pMerger, eMode);
97892
97893 /* Set up the required files for pIncr. A multi-theaded IncrMerge object
97894 ** requires two temp files to itself, whereas a single-threaded object
97895 ** only requires a region of pTask->file2. */
97896 if( rc==SQLITE_OK ){
97897 int mxSz = pIncr->mxSz;
97898#if SQLITE_MAX_WORKER_THREADS>0
97899 if( pIncr->bUseThread ){
97900 rc = vdbeSorterOpenTempFile(db, nExtend: mxSz, ppFd: &pIncr->aFile[0].pFd);
97901 if( rc==SQLITE_OK ){
97902 rc = vdbeSorterOpenTempFile(db, nExtend: mxSz, ppFd: &pIncr->aFile[1].pFd);
97903 }
97904 }else
97905#endif
97906 /*if( !pIncr->bUseThread )*/{
97907 if( pTask->file2.pFd==0 ){
97908 assert( pTask->file2.iEof>0 );
97909 rc = vdbeSorterOpenTempFile(db, nExtend: pTask->file2.iEof, ppFd: &pTask->file2.pFd);
97910 pTask->file2.iEof = 0;
97911 }
97912 if( rc==SQLITE_OK ){
97913 pIncr->aFile[1].pFd = pTask->file2.pFd;
97914 pIncr->iStartOff = pTask->file2.iEof;
97915 pTask->file2.iEof += mxSz;
97916 }
97917 }
97918 }
97919
97920#if SQLITE_MAX_WORKER_THREADS>0
97921 if( rc==SQLITE_OK && pIncr->bUseThread ){
97922 /* Use the current thread to populate aFile[1], even though this
97923 ** PmaReader is multi-threaded. If this is an INCRINIT_TASK object,
97924 ** then this function is already running in background thread
97925 ** pIncr->pTask->thread.
97926 **
97927 ** If this is the INCRINIT_ROOT object, then it is running in the
97928 ** main VDBE thread. But that is Ok, as that thread cannot return
97929 ** control to the VDBE or proceed with anything useful until the
97930 ** first results are ready from this merger object anyway.
97931 */
97932 assert( eMode==INCRINIT_ROOT || eMode==INCRINIT_TASK );
97933 rc = vdbeIncrPopulate(pIncr);
97934 }
97935#endif
97936
97937 if( rc==SQLITE_OK && (SQLITE_MAX_WORKER_THREADS==0 || eMode!=INCRINIT_TASK) ){
97938 rc = vdbePmaReaderNext(pReadr);
97939 }
97940
97941 return rc;
97942}
97943
97944#if SQLITE_MAX_WORKER_THREADS>0
97945/*
97946** The main routine for vdbePmaReaderIncrMergeInit() operations run in
97947** background threads.
97948*/
97949static void *vdbePmaReaderBgIncrInit(void *pCtx){
97950 PmaReader *pReader = (PmaReader*)pCtx;
97951 void *pRet = SQLITE_INT_TO_PTR(
97952 vdbePmaReaderIncrMergeInit(pReader,INCRINIT_TASK)
97953 );
97954 pReader->pIncr->pTask->bDone = 1;
97955 return pRet;
97956}
97957#endif
97958
97959/*
97960** If the PmaReader passed as the first argument is not an incremental-reader
97961** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it invokes
97962** the vdbePmaReaderIncrMergeInit() function with the parameters passed to
97963** this routine to initialize the incremental merge.
97964**
97965** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1),
97966** then a background thread is launched to call vdbePmaReaderIncrMergeInit().
97967** Or, if the IncrMerger is single threaded, the same function is called
97968** using the current thread.
97969*/
97970static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode){
97971 IncrMerger *pIncr = pReadr->pIncr; /* Incremental merger */
97972 int rc = SQLITE_OK; /* Return code */
97973 if( pIncr ){
97974#if SQLITE_MAX_WORKER_THREADS>0
97975 assert( pIncr->bUseThread==0 || eMode==INCRINIT_TASK );
97976 if( pIncr->bUseThread ){
97977 void *pCtx = (void*)pReadr;
97978 rc = vdbeSorterCreateThread(pTask: pIncr->pTask, xTask: vdbePmaReaderBgIncrInit, pIn: pCtx);
97979 }else
97980#endif
97981 {
97982 rc = vdbePmaReaderIncrMergeInit(pReadr, eMode);
97983 }
97984 }
97985 return rc;
97986}
97987
97988/*
97989** Allocate a new MergeEngine object to merge the contents of nPMA level-0
97990** PMAs from pTask->file. If no error occurs, set *ppOut to point to
97991** the new object and return SQLITE_OK. Or, if an error does occur, set *ppOut
97992** to NULL and return an SQLite error code.
97993**
97994** When this function is called, *piOffset is set to the offset of the
97995** first PMA to read from pTask->file. Assuming no error occurs, it is
97996** set to the offset immediately following the last byte of the last
97997** PMA before returning. If an error does occur, then the final value of
97998** *piOffset is undefined.
97999*/
98000static int vdbeMergeEngineLevel0(
98001 SortSubtask *pTask, /* Sorter task to read from */
98002 int nPMA, /* Number of PMAs to read */
98003 i64 *piOffset, /* IN/OUT: Readr offset in pTask->file */
98004 MergeEngine **ppOut /* OUT: New merge-engine */
98005){
98006 MergeEngine *pNew; /* Merge engine to return */
98007 i64 iOff = *piOffset;
98008 int i;
98009 int rc = SQLITE_OK;
98010
98011 *ppOut = pNew = vdbeMergeEngineNew(nReader: nPMA);
98012 if( pNew==0 ) rc = SQLITE_NOMEM_BKPT;
98013
98014 for(i=0; i<nPMA && rc==SQLITE_OK; i++){
98015 i64 nDummy = 0;
98016 PmaReader *pReadr = &pNew->aReadr[i];
98017 rc = vdbePmaReaderInit(pTask, pFile: &pTask->file, iStart: iOff, pReadr, pnByte: &nDummy);
98018 iOff = pReadr->iEof;
98019 }
98020
98021 if( rc!=SQLITE_OK ){
98022 vdbeMergeEngineFree(pMerger: pNew);
98023 *ppOut = 0;
98024 }
98025 *piOffset = iOff;
98026 return rc;
98027}
98028
98029/*
98030** Return the depth of a tree comprising nPMA PMAs, assuming a fanout of
98031** SORTER_MAX_MERGE_COUNT. The returned value does not include leaf nodes.
98032**
98033** i.e.
98034**
98035** nPMA<=16 -> TreeDepth() == 0
98036** nPMA<=256 -> TreeDepth() == 1
98037** nPMA<=65536 -> TreeDepth() == 2
98038*/
98039static int vdbeSorterTreeDepth(int nPMA){
98040 int nDepth = 0;
98041 i64 nDiv = SORTER_MAX_MERGE_COUNT;
98042 while( nDiv < (i64)nPMA ){
98043 nDiv = nDiv * SORTER_MAX_MERGE_COUNT;
98044 nDepth++;
98045 }
98046 return nDepth;
98047}
98048
98049/*
98050** pRoot is the root of an incremental merge-tree with depth nDepth (according
98051** to vdbeSorterTreeDepth()). pLeaf is the iSeq'th leaf to be added to the
98052** tree, counting from zero. This function adds pLeaf to the tree.
98053**
98054** If successful, SQLITE_OK is returned. If an error occurs, an SQLite error
98055** code is returned and pLeaf is freed.
98056*/
98057static int vdbeSorterAddToTree(
98058 SortSubtask *pTask, /* Task context */
98059 int nDepth, /* Depth of tree according to TreeDepth() */
98060 int iSeq, /* Sequence number of leaf within tree */
98061 MergeEngine *pRoot, /* Root of tree */
98062 MergeEngine *pLeaf /* Leaf to add to tree */
98063){
98064 int rc = SQLITE_OK;
98065 int nDiv = 1;
98066 int i;
98067 MergeEngine *p = pRoot;
98068 IncrMerger *pIncr;
98069
98070 rc = vdbeIncrMergerNew(pTask, pMerger: pLeaf, ppOut: &pIncr);
98071
98072 for(i=1; i<nDepth; i++){
98073 nDiv = nDiv * SORTER_MAX_MERGE_COUNT;
98074 }
98075
98076 for(i=1; i<nDepth && rc==SQLITE_OK; i++){
98077 int iIter = (iSeq / nDiv) % SORTER_MAX_MERGE_COUNT;
98078 PmaReader *pReadr = &p->aReadr[iIter];
98079
98080 if( pReadr->pIncr==0 ){
98081 MergeEngine *pNew = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT);
98082 if( pNew==0 ){
98083 rc = SQLITE_NOMEM_BKPT;
98084 }else{
98085 rc = vdbeIncrMergerNew(pTask, pMerger: pNew, ppOut: &pReadr->pIncr);
98086 }
98087 }
98088 if( rc==SQLITE_OK ){
98089 p = pReadr->pIncr->pMerger;
98090 nDiv = nDiv / SORTER_MAX_MERGE_COUNT;
98091 }
98092 }
98093
98094 if( rc==SQLITE_OK ){
98095 p->aReadr[iSeq % SORTER_MAX_MERGE_COUNT].pIncr = pIncr;
98096 }else{
98097 vdbeIncrFree(pIncr);
98098 }
98099 return rc;
98100}
98101
98102/*
98103** This function is called as part of a SorterRewind() operation on a sorter
98104** that has already written two or more level-0 PMAs to one or more temp
98105** files. It builds a tree of MergeEngine/IncrMerger/PmaReader objects that
98106** can be used to incrementally merge all PMAs on disk.
98107**
98108** If successful, SQLITE_OK is returned and *ppOut set to point to the
98109** MergeEngine object at the root of the tree before returning. Or, if an
98110** error occurs, an SQLite error code is returned and the final value
98111** of *ppOut is undefined.
98112*/
98113static int vdbeSorterMergeTreeBuild(
98114 VdbeSorter *pSorter, /* The VDBE cursor that implements the sort */
98115 MergeEngine **ppOut /* Write the MergeEngine here */
98116){
98117 MergeEngine *pMain = 0;
98118 int rc = SQLITE_OK;
98119 int iTask;
98120
98121#if SQLITE_MAX_WORKER_THREADS>0
98122 /* If the sorter uses more than one task, then create the top-level
98123 ** MergeEngine here. This MergeEngine will read data from exactly
98124 ** one PmaReader per sub-task. */
98125 assert( pSorter->bUseThreads || pSorter->nTask==1 );
98126 if( pSorter->nTask>1 ){
98127 pMain = vdbeMergeEngineNew(nReader: pSorter->nTask);
98128 if( pMain==0 ) rc = SQLITE_NOMEM_BKPT;
98129 }
98130#endif
98131
98132 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
98133 SortSubtask *pTask = &pSorter->aTask[iTask];
98134 assert( pTask->nPMA>0 || SQLITE_MAX_WORKER_THREADS>0 );
98135 if( SQLITE_MAX_WORKER_THREADS==0 || pTask->nPMA ){
98136 MergeEngine *pRoot = 0; /* Root node of tree for this task */
98137 int nDepth = vdbeSorterTreeDepth(nPMA: pTask->nPMA);
98138 i64 iReadOff = 0;
98139
98140 if( pTask->nPMA<=SORTER_MAX_MERGE_COUNT ){
98141 rc = vdbeMergeEngineLevel0(pTask, nPMA: pTask->nPMA, piOffset: &iReadOff, ppOut: &pRoot);
98142 }else{
98143 int i;
98144 int iSeq = 0;
98145 pRoot = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT);
98146 if( pRoot==0 ) rc = SQLITE_NOMEM_BKPT;
98147 for(i=0; i<pTask->nPMA && rc==SQLITE_OK; i += SORTER_MAX_MERGE_COUNT){
98148 MergeEngine *pMerger = 0; /* New level-0 PMA merger */
98149 int nReader; /* Number of level-0 PMAs to merge */
98150
98151 nReader = MIN(pTask->nPMA - i, SORTER_MAX_MERGE_COUNT);
98152 rc = vdbeMergeEngineLevel0(pTask, nPMA: nReader, piOffset: &iReadOff, ppOut: &pMerger);
98153 if( rc==SQLITE_OK ){
98154 rc = vdbeSorterAddToTree(pTask, nDepth, iSeq: iSeq++, pRoot, pLeaf: pMerger);
98155 }
98156 }
98157 }
98158
98159 if( rc==SQLITE_OK ){
98160#if SQLITE_MAX_WORKER_THREADS>0
98161 if( pMain!=0 ){
98162 rc = vdbeIncrMergerNew(pTask, pMerger: pRoot, ppOut: &pMain->aReadr[iTask].pIncr);
98163 }else
98164#endif
98165 {
98166 assert( pMain==0 );
98167 pMain = pRoot;
98168 }
98169 }else{
98170 vdbeMergeEngineFree(pMerger: pRoot);
98171 }
98172 }
98173 }
98174
98175 if( rc!=SQLITE_OK ){
98176 vdbeMergeEngineFree(pMerger: pMain);
98177 pMain = 0;
98178 }
98179 *ppOut = pMain;
98180 return rc;
98181}
98182
98183/*
98184** This function is called as part of an sqlite3VdbeSorterRewind() operation
98185** on a sorter that has written two or more PMAs to temporary files. It sets
98186** up either VdbeSorter.pMerger (for single threaded sorters) or pReader
98187** (for multi-threaded sorters) so that it can be used to iterate through
98188** all records stored in the sorter.
98189**
98190** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
98191*/
98192static int vdbeSorterSetupMerge(VdbeSorter *pSorter){
98193 int rc; /* Return code */
98194 SortSubtask *pTask0 = &pSorter->aTask[0];
98195 MergeEngine *pMain = 0;
98196#if SQLITE_MAX_WORKER_THREADS
98197 sqlite3 *db = pTask0->pSorter->db;
98198 int i;
98199 SorterCompare xCompare = vdbeSorterGetCompare(p: pSorter);
98200 for(i=0; i<pSorter->nTask; i++){
98201 pSorter->aTask[i].xCompare = xCompare;
98202 }
98203#endif
98204
98205 rc = vdbeSorterMergeTreeBuild(pSorter, ppOut: &pMain);
98206 if( rc==SQLITE_OK ){
98207#if SQLITE_MAX_WORKER_THREADS
98208 assert( pSorter->bUseThreads==0 || pSorter->nTask>1 );
98209 if( pSorter->bUseThreads ){
98210 int iTask;
98211 PmaReader *pReadr = 0;
98212 SortSubtask *pLast = &pSorter->aTask[pSorter->nTask-1];
98213 rc = vdbeSortAllocUnpacked(pTask: pLast);
98214 if( rc==SQLITE_OK ){
98215 pReadr = (PmaReader*)sqlite3DbMallocZero(db, n: sizeof(PmaReader));
98216 pSorter->pReader = pReadr;
98217 if( pReadr==0 ) rc = SQLITE_NOMEM_BKPT;
98218 }
98219 if( rc==SQLITE_OK ){
98220 rc = vdbeIncrMergerNew(pTask: pLast, pMerger: pMain, ppOut: &pReadr->pIncr);
98221 if( rc==SQLITE_OK ){
98222 vdbeIncrMergerSetThreads(pIncr: pReadr->pIncr);
98223 for(iTask=0; iTask<(pSorter->nTask-1); iTask++){
98224 IncrMerger *pIncr;
98225 if( (pIncr = pMain->aReadr[iTask].pIncr) ){
98226 vdbeIncrMergerSetThreads(pIncr);
98227 assert( pIncr->pTask!=pLast );
98228 }
98229 }
98230 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
98231 /* Check that:
98232 **
98233 ** a) The incremental merge object is configured to use the
98234 ** right task, and
98235 ** b) If it is using task (nTask-1), it is configured to run
98236 ** in single-threaded mode. This is important, as the
98237 ** root merge (INCRINIT_ROOT) will be using the same task
98238 ** object.
98239 */
98240 PmaReader *p = &pMain->aReadr[iTask];
98241 assert( p->pIncr==0 || (
98242 (p->pIncr->pTask==&pSorter->aTask[iTask]) /* a */
98243 && (iTask!=pSorter->nTask-1 || p->pIncr->bUseThread==0) /* b */
98244 ));
98245 rc = vdbePmaReaderIncrInit(pReadr: p, INCRINIT_TASK);
98246 }
98247 }
98248 pMain = 0;
98249 }
98250 if( rc==SQLITE_OK ){
98251 rc = vdbePmaReaderIncrMergeInit(pReadr, INCRINIT_ROOT);
98252 }
98253 }else
98254#endif
98255 {
98256 rc = vdbeMergeEngineInit(pTask: pTask0, pMerger: pMain, INCRINIT_NORMAL);
98257 pSorter->pMerger = pMain;
98258 pMain = 0;
98259 }
98260 }
98261
98262 if( rc!=SQLITE_OK ){
98263 vdbeMergeEngineFree(pMerger: pMain);
98264 }
98265 return rc;
98266}
98267
98268
98269/*
98270** Once the sorter has been populated by calls to sqlite3VdbeSorterWrite,
98271** this function is called to prepare for iterating through the records
98272** in sorted order.
98273*/
98274SQLITE_PRIVATE int sqlite3VdbeSorterRewind(const VdbeCursor *pCsr, int *pbEof){
98275 VdbeSorter *pSorter;
98276 int rc = SQLITE_OK; /* Return code */
98277
98278 assert( pCsr->eCurType==CURTYPE_SORTER );
98279 pSorter = pCsr->uc.pSorter;
98280 assert( pSorter );
98281
98282 /* If no data has been written to disk, then do not do so now. Instead,
98283 ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly
98284 ** from the in-memory list. */
98285 if( pSorter->bUsePMA==0 ){
98286 if( pSorter->list.pList ){
98287 *pbEof = 0;
98288 rc = vdbeSorterSort(pTask: &pSorter->aTask[0], pList: &pSorter->list);
98289 }else{
98290 *pbEof = 1;
98291 }
98292 return rc;
98293 }
98294
98295 /* Write the current in-memory list to a PMA. When the VdbeSorterWrite()
98296 ** function flushes the contents of memory to disk, it immediately always
98297 ** creates a new list consisting of a single key immediately afterwards.
98298 ** So the list is never empty at this point. */
98299 assert( pSorter->list.pList );
98300 rc = vdbeSorterFlushPMA(pSorter);
98301
98302 /* Join all threads */
98303 rc = vdbeSorterJoinAll(pSorter, rcin: rc);
98304
98305 vdbeSorterRewindDebug("rewind");
98306
98307 /* Assuming no errors have occurred, set up a merger structure to
98308 ** incrementally read and merge all remaining PMAs. */
98309 assert( pSorter->pReader==0 );
98310 if( rc==SQLITE_OK ){
98311 rc = vdbeSorterSetupMerge(pSorter);
98312 *pbEof = 0;
98313 }
98314
98315 vdbeSorterRewindDebug("rewinddone");
98316 return rc;
98317}
98318
98319/*
98320** Advance to the next element in the sorter. Return value:
98321**
98322** SQLITE_OK success
98323** SQLITE_DONE end of data
98324** otherwise some kind of error.
98325*/
98326SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr){
98327 VdbeSorter *pSorter;
98328 int rc; /* Return code */
98329
98330 assert( pCsr->eCurType==CURTYPE_SORTER );
98331 pSorter = pCsr->uc.pSorter;
98332 assert( pSorter->bUsePMA || (pSorter->pReader==0 && pSorter->pMerger==0) );
98333 if( pSorter->bUsePMA ){
98334 assert( pSorter->pReader==0 || pSorter->pMerger==0 );
98335 assert( pSorter->bUseThreads==0 || pSorter->pReader );
98336 assert( pSorter->bUseThreads==1 || pSorter->pMerger );
98337#if SQLITE_MAX_WORKER_THREADS>0
98338 if( pSorter->bUseThreads ){
98339 rc = vdbePmaReaderNext(pReadr: pSorter->pReader);
98340 if( rc==SQLITE_OK && pSorter->pReader->pFd==0 ) rc = SQLITE_DONE;
98341 }else
98342#endif
98343 /*if( !pSorter->bUseThreads )*/ {
98344 int res = 0;
98345 assert( pSorter->pMerger!=0 );
98346 assert( pSorter->pMerger->pTask==(&pSorter->aTask[0]) );
98347 rc = vdbeMergeEngineStep(pMerger: pSorter->pMerger, pbEof: &res);
98348 if( rc==SQLITE_OK && res ) rc = SQLITE_DONE;
98349 }
98350 }else{
98351 SorterRecord *pFree = pSorter->list.pList;
98352 pSorter->list.pList = pFree->u.pNext;
98353 pFree->u.pNext = 0;
98354 if( pSorter->list.aMemory==0 ) vdbeSorterRecordFree(db, pRecord: pFree);
98355 rc = pSorter->list.pList ? SQLITE_OK : SQLITE_DONE;
98356 }
98357 return rc;
98358}
98359
98360/*
98361** Return a pointer to a buffer owned by the sorter that contains the
98362** current key.
98363*/
98364static void *vdbeSorterRowkey(
98365 const VdbeSorter *pSorter, /* Sorter object */
98366 int *pnKey /* OUT: Size of current key in bytes */
98367){
98368 void *pKey;
98369 if( pSorter->bUsePMA ){
98370 PmaReader *pReader;
98371#if SQLITE_MAX_WORKER_THREADS>0
98372 if( pSorter->bUseThreads ){
98373 pReader = pSorter->pReader;
98374 }else
98375#endif
98376 /*if( !pSorter->bUseThreads )*/{
98377 pReader = &pSorter->pMerger->aReadr[pSorter->pMerger->aTree[1]];
98378 }
98379 *pnKey = pReader->nKey;
98380 pKey = pReader->aKey;
98381 }else{
98382 *pnKey = pSorter->list.pList->nVal;
98383 pKey = SRVAL(pSorter->list.pList);
98384 }
98385 return pKey;
98386}
98387
98388/*
98389** Copy the current sorter key into the memory cell pOut.
98390*/
98391SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *pCsr, Mem *pOut){
98392 VdbeSorter *pSorter;
98393 void *pKey; int nKey; /* Sorter key to copy into pOut */
98394
98395 assert( pCsr->eCurType==CURTYPE_SORTER );
98396 pSorter = pCsr->uc.pSorter;
98397 pKey = vdbeSorterRowkey(pSorter, pnKey: &nKey);
98398 if( sqlite3VdbeMemClearAndResize(pMem: pOut, szNew: nKey) ){
98399 return SQLITE_NOMEM_BKPT;
98400 }
98401 pOut->n = nKey;
98402 MemSetTypeFlag(pOut, MEM_Blob);
98403 memcpy(dest: pOut->z, src: pKey, n: nKey);
98404
98405 return SQLITE_OK;
98406}
98407
98408/*
98409** Compare the key in memory cell pVal with the key that the sorter cursor
98410** passed as the first argument currently points to. For the purposes of
98411** the comparison, ignore the rowid field at the end of each record.
98412**
98413** If the sorter cursor key contains any NULL values, consider it to be
98414** less than pVal. Even if pVal also contains NULL values.
98415**
98416** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM).
98417** Otherwise, set *pRes to a negative, zero or positive value if the
98418** key in pVal is smaller than, equal to or larger than the current sorter
98419** key.
98420**
98421** This routine forms the core of the OP_SorterCompare opcode, which in
98422** turn is used to verify uniqueness when constructing a UNIQUE INDEX.
98423*/
98424SQLITE_PRIVATE int sqlite3VdbeSorterCompare(
98425 const VdbeCursor *pCsr, /* Sorter cursor */
98426 Mem *pVal, /* Value to compare to current sorter key */
98427 int nKeyCol, /* Compare this many columns */
98428 int *pRes /* OUT: Result of comparison */
98429){
98430 VdbeSorter *pSorter;
98431 UnpackedRecord *r2;
98432 KeyInfo *pKeyInfo;
98433 int i;
98434 void *pKey; int nKey; /* Sorter key to compare pVal with */
98435
98436 assert( pCsr->eCurType==CURTYPE_SORTER );
98437 pSorter = pCsr->uc.pSorter;
98438 r2 = pSorter->pUnpacked;
98439 pKeyInfo = pCsr->pKeyInfo;
98440 if( r2==0 ){
98441 r2 = pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
98442 if( r2==0 ) return SQLITE_NOMEM_BKPT;
98443 r2->nField = nKeyCol;
98444 }
98445 assert( r2->nField==nKeyCol );
98446
98447 pKey = vdbeSorterRowkey(pSorter, pnKey: &nKey);
98448 sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, p: r2);
98449 for(i=0; i<nKeyCol; i++){
98450 if( r2->aMem[i].flags & MEM_Null ){
98451 *pRes = -1;
98452 return SQLITE_OK;
98453 }
98454 }
98455
98456 *pRes = sqlite3VdbeRecordCompare(nKey1: pVal->n, pKey1: pVal->z, pPKey2: r2);
98457 return SQLITE_OK;
98458}
98459
98460/************** End of vdbesort.c ********************************************/
98461/************** Begin file vdbevtab.c ****************************************/
98462/*
98463** 2020-03-23
98464**
98465** The author disclaims copyright to this source code. In place of
98466** a legal notice, here is a blessing:
98467**
98468** May you do good and not evil.
98469** May you find forgiveness for yourself and forgive others.
98470** May you share freely, never taking more than you give.
98471**
98472*************************************************************************
98473**
98474** This file implements virtual-tables for examining the bytecode content
98475** of a prepared statement.
98476*/
98477/* #include "sqliteInt.h" */
98478#if defined(SQLITE_ENABLE_BYTECODE_VTAB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
98479/* #include "vdbeInt.h" */
98480
98481/* An instance of the bytecode() table-valued function.
98482*/
98483typedef struct bytecodevtab bytecodevtab;
98484struct bytecodevtab {
98485 sqlite3_vtab base; /* Base class - must be first */
98486 sqlite3 *db; /* Database connection */
98487 int bTablesUsed; /* 2 for tables_used(). 0 for bytecode(). */
98488};
98489
98490/* A cursor for scanning through the bytecode
98491*/
98492typedef struct bytecodevtab_cursor bytecodevtab_cursor;
98493struct bytecodevtab_cursor {
98494 sqlite3_vtab_cursor base; /* Base class - must be first */
98495 sqlite3_stmt *pStmt; /* The statement whose bytecode is displayed */
98496 int iRowid; /* The rowid of the output table */
98497 int iAddr; /* Address */
98498 int needFinalize; /* Cursors owns pStmt and must finalize it */
98499 int showSubprograms; /* Provide a listing of subprograms */
98500 Op *aOp; /* Operand array */
98501 char *zP4; /* Rendered P4 value */
98502 const char *zType; /* tables_used.type */
98503 const char *zSchema; /* tables_used.schema */
98504 const char *zName; /* tables_used.name */
98505 Mem sub; /* Subprograms */
98506};
98507
98508/*
98509** Create a new bytecode() table-valued function.
98510*/
98511static int bytecodevtabConnect(
98512 sqlite3 *db,
98513 void *pAux,
98514 int argc, const char *const*argv,
98515 sqlite3_vtab **ppVtab,
98516 char **pzErr
98517){
98518 bytecodevtab *pNew;
98519 int rc;
98520 int isTabUsed = pAux!=0;
98521 const char *azSchema[2] = {
98522 /* bytecode() schema */
98523 "CREATE TABLE x("
98524 "addr INT,"
98525 "opcode TEXT,"
98526 "p1 INT,"
98527 "p2 INT,"
98528 "p3 INT,"
98529 "p4 TEXT,"
98530 "p5 INT,"
98531 "comment TEXT,"
98532 "subprog TEXT,"
98533 "stmt HIDDEN"
98534 ");",
98535
98536 /* Tables_used() schema */
98537 "CREATE TABLE x("
98538 "type TEXT,"
98539 "schema TEXT,"
98540 "name TEXT,"
98541 "wr INT,"
98542 "subprog TEXT,"
98543 "stmt HIDDEN"
98544 ");"
98545 };
98546
98547 rc = sqlite3_declare_vtab(db, azSchema[isTabUsed]);
98548 if( rc==SQLITE_OK ){
98549 pNew = sqlite3_malloc( sizeof(*pNew) );
98550 *ppVtab = (sqlite3_vtab*)pNew;
98551 if( pNew==0 ) return SQLITE_NOMEM;
98552 memset(pNew, 0, sizeof(*pNew));
98553 pNew->db = db;
98554 pNew->bTablesUsed = isTabUsed*2;
98555 }
98556 return rc;
98557}
98558
98559/*
98560** This method is the destructor for bytecodevtab objects.
98561*/
98562static int bytecodevtabDisconnect(sqlite3_vtab *pVtab){
98563 bytecodevtab *p = (bytecodevtab*)pVtab;
98564 sqlite3_free(p);
98565 return SQLITE_OK;
98566}
98567
98568/*
98569** Constructor for a new bytecodevtab_cursor object.
98570*/
98571static int bytecodevtabOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
98572 bytecodevtab *pVTab = (bytecodevtab*)p;
98573 bytecodevtab_cursor *pCur;
98574 pCur = sqlite3_malloc( sizeof(*pCur) );
98575 if( pCur==0 ) return SQLITE_NOMEM;
98576 memset(pCur, 0, sizeof(*pCur));
98577 sqlite3VdbeMemInit(&pCur->sub, pVTab->db, 1);
98578 *ppCursor = &pCur->base;
98579 return SQLITE_OK;
98580}
98581
98582/*
98583** Clear all internal content from a bytecodevtab cursor.
98584*/
98585static void bytecodevtabCursorClear(bytecodevtab_cursor *pCur){
98586 sqlite3_free(pCur->zP4);
98587 pCur->zP4 = 0;
98588 sqlite3VdbeMemRelease(&pCur->sub);
98589 sqlite3VdbeMemSetNull(&pCur->sub);
98590 if( pCur->needFinalize ){
98591 sqlite3_finalize(pCur->pStmt);
98592 }
98593 pCur->pStmt = 0;
98594 pCur->needFinalize = 0;
98595 pCur->zType = 0;
98596 pCur->zSchema = 0;
98597 pCur->zName = 0;
98598}
98599
98600/*
98601** Destructor for a bytecodevtab_cursor.
98602*/
98603static int bytecodevtabClose(sqlite3_vtab_cursor *cur){
98604 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
98605 bytecodevtabCursorClear(pCur);
98606 sqlite3_free(pCur);
98607 return SQLITE_OK;
98608}
98609
98610
98611/*
98612** Advance a bytecodevtab_cursor to its next row of output.
98613*/
98614static int bytecodevtabNext(sqlite3_vtab_cursor *cur){
98615 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
98616 bytecodevtab *pTab = (bytecodevtab*)cur->pVtab;
98617 int rc;
98618 if( pCur->zP4 ){
98619 sqlite3_free(pCur->zP4);
98620 pCur->zP4 = 0;
98621 }
98622 if( pCur->zName ){
98623 pCur->zName = 0;
98624 pCur->zType = 0;
98625 pCur->zSchema = 0;
98626 }
98627 rc = sqlite3VdbeNextOpcode(
98628 (Vdbe*)pCur->pStmt,
98629 pCur->showSubprograms ? &pCur->sub : 0,
98630 pTab->bTablesUsed,
98631 &pCur->iRowid,
98632 &pCur->iAddr,
98633 &pCur->aOp);
98634 if( rc!=SQLITE_OK ){
98635 sqlite3VdbeMemSetNull(&pCur->sub);
98636 pCur->aOp = 0;
98637 }
98638 return SQLITE_OK;
98639}
98640
98641/*
98642** Return TRUE if the cursor has been moved off of the last
98643** row of output.
98644*/
98645static int bytecodevtabEof(sqlite3_vtab_cursor *cur){
98646 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
98647 return pCur->aOp==0;
98648}
98649
98650/*
98651** Return values of columns for the row at which the bytecodevtab_cursor
98652** is currently pointing.
98653*/
98654static int bytecodevtabColumn(
98655 sqlite3_vtab_cursor *cur, /* The cursor */
98656 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
98657 int i /* Which column to return */
98658){
98659 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
98660 bytecodevtab *pVTab = (bytecodevtab*)cur->pVtab;
98661 Op *pOp = pCur->aOp + pCur->iAddr;
98662 if( pVTab->bTablesUsed ){
98663 if( i==4 ){
98664 i = 8;
98665 }else{
98666 if( i<=2 && pCur->zType==0 ){
98667 Schema *pSchema;
98668 HashElem *k;
98669 int iDb = pOp->p3;
98670 Pgno iRoot = (Pgno)pOp->p2;
98671 sqlite3 *db = pVTab->db;
98672 pSchema = db->aDb[iDb].pSchema;
98673 pCur->zSchema = db->aDb[iDb].zDbSName;
98674 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
98675 Table *pTab = (Table*)sqliteHashData(k);
98676 if( !IsVirtual(pTab) && pTab->tnum==iRoot ){
98677 pCur->zName = pTab->zName;
98678 pCur->zType = "table";
98679 break;
98680 }
98681 }
98682 if( pCur->zName==0 ){
98683 for(k=sqliteHashFirst(&pSchema->idxHash); k; k=sqliteHashNext(k)){
98684 Index *pIdx = (Index*)sqliteHashData(k);
98685 if( pIdx->tnum==iRoot ){
98686 pCur->zName = pIdx->zName;
98687 pCur->zType = "index";
98688 }
98689 }
98690 }
98691 }
98692 i += 10;
98693 }
98694 }
98695 switch( i ){
98696 case 0: /* addr */
98697 sqlite3_result_int(ctx, pCur->iAddr);
98698 break;
98699 case 1: /* opcode */
98700 sqlite3_result_text(ctx, (char*)sqlite3OpcodeName(pOp->opcode),
98701 -1, SQLITE_STATIC);
98702 break;
98703 case 2: /* p1 */
98704 sqlite3_result_int(ctx, pOp->p1);
98705 break;
98706 case 3: /* p2 */
98707 sqlite3_result_int(ctx, pOp->p2);
98708 break;
98709 case 4: /* p3 */
98710 sqlite3_result_int(ctx, pOp->p3);
98711 break;
98712 case 5: /* p4 */
98713 case 7: /* comment */
98714 if( pCur->zP4==0 ){
98715 pCur->zP4 = sqlite3VdbeDisplayP4(pVTab->db, pOp);
98716 }
98717 if( i==5 ){
98718 sqlite3_result_text(ctx, pCur->zP4, -1, SQLITE_STATIC);
98719 }else{
98720#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
98721 char *zCom = sqlite3VdbeDisplayComment(pVTab->db, pOp, pCur->zP4);
98722 sqlite3_result_text(ctx, zCom, -1, sqlite3_free);
98723#endif
98724 }
98725 break;
98726 case 6: /* p5 */
98727 sqlite3_result_int(ctx, pOp->p5);
98728 break;
98729 case 8: { /* subprog */
98730 Op *aOp = pCur->aOp;
98731 assert( aOp[0].opcode==OP_Init );
98732 assert( aOp[0].p4.z==0 || strncmp(aOp[0].p4.z,"-" "- ",3)==0 );
98733 if( pCur->iRowid==pCur->iAddr+1 ){
98734 break; /* Result is NULL for the main program */
98735 }else if( aOp[0].p4.z!=0 ){
98736 sqlite3_result_text(ctx, aOp[0].p4.z+3, -1, SQLITE_STATIC);
98737 }else{
98738 sqlite3_result_text(ctx, "(FK)", 4, SQLITE_STATIC);
98739 }
98740 break;
98741 }
98742 case 10: /* tables_used.type */
98743 sqlite3_result_text(ctx, pCur->zType, -1, SQLITE_STATIC);
98744 break;
98745 case 11: /* tables_used.schema */
98746 sqlite3_result_text(ctx, pCur->zSchema, -1, SQLITE_STATIC);
98747 break;
98748 case 12: /* tables_used.name */
98749 sqlite3_result_text(ctx, pCur->zName, -1, SQLITE_STATIC);
98750 break;
98751 case 13: /* tables_used.wr */
98752 sqlite3_result_int(ctx, pOp->opcode==OP_OpenWrite);
98753 break;
98754 }
98755 return SQLITE_OK;
98756}
98757
98758/*
98759** Return the rowid for the current row. In this implementation, the
98760** rowid is the same as the output value.
98761*/
98762static int bytecodevtabRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
98763 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
98764 *pRowid = pCur->iRowid;
98765 return SQLITE_OK;
98766}
98767
98768/*
98769** Initialize a cursor.
98770**
98771** idxNum==0 means show all subprograms
98772** idxNum==1 means show only the main bytecode and omit subprograms.
98773*/
98774static int bytecodevtabFilter(
98775 sqlite3_vtab_cursor *pVtabCursor,
98776 int idxNum, const char *idxStr,
98777 int argc, sqlite3_value **argv
98778){
98779 bytecodevtab_cursor *pCur = (bytecodevtab_cursor *)pVtabCursor;
98780 bytecodevtab *pVTab = (bytecodevtab *)pVtabCursor->pVtab;
98781 int rc = SQLITE_OK;
98782
98783 bytecodevtabCursorClear(pCur);
98784 pCur->iRowid = 0;
98785 pCur->iAddr = 0;
98786 pCur->showSubprograms = idxNum==0;
98787 assert( argc==1 );
98788 if( sqlite3_value_type(argv[0])==SQLITE_TEXT ){
98789 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
98790 if( zSql==0 ){
98791 rc = SQLITE_NOMEM;
98792 }else{
98793 rc = sqlite3_prepare_v2(pVTab->db, zSql, -1, &pCur->pStmt, 0);
98794 pCur->needFinalize = 1;
98795 }
98796 }else{
98797 pCur->pStmt = (sqlite3_stmt*)sqlite3_value_pointer(argv[0],"stmt-pointer");
98798 }
98799 if( pCur->pStmt==0 ){
98800 pVTab->base.zErrMsg = sqlite3_mprintf(
98801 "argument to %s() is not a valid SQL statement",
98802 pVTab->bTablesUsed ? "tables_used" : "bytecode"
98803 );
98804 rc = SQLITE_ERROR;
98805 }else{
98806 bytecodevtabNext(pVtabCursor);
98807 }
98808 return rc;
98809}
98810
98811/*
98812** We must have a single stmt=? constraint that will be passed through
98813** into the xFilter method. If there is no valid stmt=? constraint,
98814** then return an SQLITE_CONSTRAINT error.
98815*/
98816static int bytecodevtabBestIndex(
98817 sqlite3_vtab *tab,
98818 sqlite3_index_info *pIdxInfo
98819){
98820 int i;
98821 int rc = SQLITE_CONSTRAINT;
98822 struct sqlite3_index_constraint *p;
98823 bytecodevtab *pVTab = (bytecodevtab*)tab;
98824 int iBaseCol = pVTab->bTablesUsed ? 4 : 8;
98825 pIdxInfo->estimatedCost = (double)100;
98826 pIdxInfo->estimatedRows = 100;
98827 pIdxInfo->idxNum = 0;
98828 for(i=0, p=pIdxInfo->aConstraint; i<pIdxInfo->nConstraint; i++, p++){
98829 if( p->usable==0 ) continue;
98830 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ && p->iColumn==iBaseCol+1 ){
98831 rc = SQLITE_OK;
98832 pIdxInfo->aConstraintUsage[i].omit = 1;
98833 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
98834 }
98835 if( p->op==SQLITE_INDEX_CONSTRAINT_ISNULL && p->iColumn==iBaseCol ){
98836 pIdxInfo->aConstraintUsage[i].omit = 1;
98837 pIdxInfo->idxNum = 1;
98838 }
98839 }
98840 return rc;
98841}
98842
98843/*
98844** This following structure defines all the methods for the
98845** virtual table.
98846*/
98847static sqlite3_module bytecodevtabModule = {
98848 /* iVersion */ 0,
98849 /* xCreate */ 0,
98850 /* xConnect */ bytecodevtabConnect,
98851 /* xBestIndex */ bytecodevtabBestIndex,
98852 /* xDisconnect */ bytecodevtabDisconnect,
98853 /* xDestroy */ 0,
98854 /* xOpen */ bytecodevtabOpen,
98855 /* xClose */ bytecodevtabClose,
98856 /* xFilter */ bytecodevtabFilter,
98857 /* xNext */ bytecodevtabNext,
98858 /* xEof */ bytecodevtabEof,
98859 /* xColumn */ bytecodevtabColumn,
98860 /* xRowid */ bytecodevtabRowid,
98861 /* xUpdate */ 0,
98862 /* xBegin */ 0,
98863 /* xSync */ 0,
98864 /* xCommit */ 0,
98865 /* xRollback */ 0,
98866 /* xFindMethod */ 0,
98867 /* xRename */ 0,
98868 /* xSavepoint */ 0,
98869 /* xRelease */ 0,
98870 /* xRollbackTo */ 0,
98871 /* xShadowName */ 0
98872};
98873
98874
98875SQLITE_PRIVATE int sqlite3VdbeBytecodeVtabInit(sqlite3 *db){
98876 int rc;
98877 rc = sqlite3_create_module(db, "bytecode", &bytecodevtabModule, 0);
98878 if( rc==SQLITE_OK ){
98879 rc = sqlite3_create_module(db, "tables_used", &bytecodevtabModule, &db);
98880 }
98881 return rc;
98882}
98883#elif defined(SQLITE_ENABLE_BYTECODE_VTAB)
98884SQLITE_PRIVATE int sqlite3VdbeBytecodeVtabInit(sqlite3 *db){ return SQLITE_OK; }
98885#endif /* SQLITE_ENABLE_BYTECODE_VTAB */
98886
98887/************** End of vdbevtab.c ********************************************/
98888/************** Begin file memjournal.c **************************************/
98889/*
98890** 2008 October 7
98891**
98892** The author disclaims copyright to this source code. In place of
98893** a legal notice, here is a blessing:
98894**
98895** May you do good and not evil.
98896** May you find forgiveness for yourself and forgive others.
98897** May you share freely, never taking more than you give.
98898**
98899*************************************************************************
98900**
98901** This file contains code use to implement an in-memory rollback journal.
98902** The in-memory rollback journal is used to journal transactions for
98903** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
98904**
98905** Update: The in-memory journal is also used to temporarily cache
98906** smaller journals that are not critical for power-loss recovery.
98907** For example, statement journals that are not too big will be held
98908** entirely in memory, thus reducing the number of file I/O calls, and
98909** more importantly, reducing temporary file creation events. If these
98910** journals become too large for memory, they are spilled to disk. But
98911** in the common case, they are usually small and no file I/O needs to
98912** occur.
98913*/
98914/* #include "sqliteInt.h" */
98915
98916/* Forward references to internal structures */
98917typedef struct MemJournal MemJournal;
98918typedef struct FilePoint FilePoint;
98919typedef struct FileChunk FileChunk;
98920
98921/*
98922** The rollback journal is composed of a linked list of these structures.
98923**
98924** The zChunk array is always at least 8 bytes in size - usually much more.
98925** Its actual size is stored in the MemJournal.nChunkSize variable.
98926*/
98927struct FileChunk {
98928 FileChunk *pNext; /* Next chunk in the journal */
98929 u8 zChunk[8]; /* Content of this chunk */
98930};
98931
98932/*
98933** By default, allocate this many bytes of memory for each FileChunk object.
98934*/
98935#define MEMJOURNAL_DFLT_FILECHUNKSIZE 1024
98936
98937/*
98938** For chunk size nChunkSize, return the number of bytes that should
98939** be allocated for each FileChunk structure.
98940*/
98941#define fileChunkSize(nChunkSize) (sizeof(FileChunk) + ((nChunkSize)-8))
98942
98943/*
98944** An instance of this object serves as a cursor into the rollback journal.
98945** The cursor can be either for reading or writing.
98946*/
98947struct FilePoint {
98948 sqlite3_int64 iOffset; /* Offset from the beginning of the file */
98949 FileChunk *pChunk; /* Specific chunk into which cursor points */
98950};
98951
98952/*
98953** This structure is a subclass of sqlite3_file. Each open memory-journal
98954** is an instance of this class.
98955*/
98956struct MemJournal {
98957 const sqlite3_io_methods *pMethod; /* Parent class. MUST BE FIRST */
98958 int nChunkSize; /* In-memory chunk-size */
98959
98960 int nSpill; /* Bytes of data before flushing */
98961 FileChunk *pFirst; /* Head of in-memory chunk-list */
98962 FilePoint endpoint; /* Pointer to the end of the file */
98963 FilePoint readpoint; /* Pointer to the end of the last xRead() */
98964
98965 int flags; /* xOpen flags */
98966 sqlite3_vfs *pVfs; /* The "real" underlying VFS */
98967 const char *zJournal; /* Name of the journal file */
98968};
98969
98970/*
98971** Read data from the in-memory journal file. This is the implementation
98972** of the sqlite3_vfs.xRead method.
98973*/
98974static int memjrnlRead(
98975 sqlite3_file *pJfd, /* The journal file from which to read */
98976 void *zBuf, /* Put the results here */
98977 int iAmt, /* Number of bytes to read */
98978 sqlite_int64 iOfst /* Begin reading at this offset */
98979){
98980 MemJournal *p = (MemJournal *)pJfd;
98981 u8 *zOut = zBuf;
98982 int nRead = iAmt;
98983 int iChunkOffset;
98984 FileChunk *pChunk;
98985
98986 if( (iAmt+iOfst)>p->endpoint.iOffset ){
98987 return SQLITE_IOERR_SHORT_READ;
98988 }
98989 assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 );
98990 if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
98991 sqlite3_int64 iOff = 0;
98992 for(pChunk=p->pFirst;
98993 ALWAYS(pChunk) && (iOff+p->nChunkSize)<=iOfst;
98994 pChunk=pChunk->pNext
98995 ){
98996 iOff += p->nChunkSize;
98997 }
98998 }else{
98999 pChunk = p->readpoint.pChunk;
99000 assert( pChunk!=0 );
99001 }
99002
99003 iChunkOffset = (int)(iOfst%p->nChunkSize);
99004 do {
99005 int iSpace = p->nChunkSize - iChunkOffset;
99006 int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset));
99007 memcpy(dest: zOut, src: (u8*)pChunk->zChunk + iChunkOffset, n: nCopy);
99008 zOut += nCopy;
99009 nRead -= iSpace;
99010 iChunkOffset = 0;
99011 } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
99012 p->readpoint.iOffset = pChunk ? iOfst+iAmt : 0;
99013 p->readpoint.pChunk = pChunk;
99014
99015 return SQLITE_OK;
99016}
99017
99018/*
99019** Free the list of FileChunk structures headed at MemJournal.pFirst.
99020*/
99021static void memjrnlFreeChunks(FileChunk *pFirst){
99022 FileChunk *pIter;
99023 FileChunk *pNext;
99024 for(pIter=pFirst; pIter; pIter=pNext){
99025 pNext = pIter->pNext;
99026 sqlite3_free(p: pIter);
99027 }
99028}
99029
99030/*
99031** Flush the contents of memory to a real file on disk.
99032*/
99033static int memjrnlCreateFile(MemJournal *p){
99034 int rc;
99035 sqlite3_file *pReal = (sqlite3_file*)p;
99036 MemJournal copy = *p;
99037
99038 memset(s: p, c: 0, n: sizeof(MemJournal));
99039 rc = sqlite3OsOpen(pVfs: copy.pVfs, zPath: copy.zJournal, pFile: pReal, flags: copy.flags, pFlagsOut: 0);
99040 if( rc==SQLITE_OK ){
99041 int nChunk = copy.nChunkSize;
99042 i64 iOff = 0;
99043 FileChunk *pIter;
99044 for(pIter=copy.pFirst; pIter; pIter=pIter->pNext){
99045 if( iOff + nChunk > copy.endpoint.iOffset ){
99046 nChunk = copy.endpoint.iOffset - iOff;
99047 }
99048 rc = sqlite3OsWrite(id: pReal, pBuf: (u8*)pIter->zChunk, amt: nChunk, offset: iOff);
99049 if( rc ) break;
99050 iOff += nChunk;
99051 }
99052 if( rc==SQLITE_OK ){
99053 /* No error has occurred. Free the in-memory buffers. */
99054 memjrnlFreeChunks(pFirst: copy.pFirst);
99055 }
99056 }
99057 if( rc!=SQLITE_OK ){
99058 /* If an error occurred while creating or writing to the file, restore
99059 ** the original before returning. This way, SQLite uses the in-memory
99060 ** journal data to roll back changes made to the internal page-cache
99061 ** before this function was called. */
99062 sqlite3OsClose(pId: pReal);
99063 *p = copy;
99064 }
99065 return rc;
99066}
99067
99068
99069/*
99070** Write data to the file.
99071*/
99072static int memjrnlWrite(
99073 sqlite3_file *pJfd, /* The journal file into which to write */
99074 const void *zBuf, /* Take data to be written from here */
99075 int iAmt, /* Number of bytes to write */
99076 sqlite_int64 iOfst /* Begin writing at this offset into the file */
99077){
99078 MemJournal *p = (MemJournal *)pJfd;
99079 int nWrite = iAmt;
99080 u8 *zWrite = (u8 *)zBuf;
99081
99082 /* If the file should be created now, create it and write the new data
99083 ** into the file on disk. */
99084 if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){
99085 int rc = memjrnlCreateFile(p);
99086 if( rc==SQLITE_OK ){
99087 rc = sqlite3OsWrite(id: pJfd, pBuf: zBuf, amt: iAmt, offset: iOfst);
99088 }
99089 return rc;
99090 }
99091
99092 /* If the contents of this write should be stored in memory */
99093 else{
99094 /* An in-memory journal file should only ever be appended to. Random
99095 ** access writes are not required. The only exception to this is when
99096 ** the in-memory journal is being used by a connection using the
99097 ** atomic-write optimization. In this case the first 28 bytes of the
99098 ** journal file may be written as part of committing the transaction. */
99099 assert( iOfst==p->endpoint.iOffset || iOfst==0 );
99100#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
99101 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
99102 if( iOfst==0 && p->pFirst ){
99103 assert( p->nChunkSize>iAmt );
99104 memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt);
99105 }else
99106#else
99107 assert( iOfst>0 || p->pFirst==0 );
99108#endif
99109 {
99110 while( nWrite>0 ){
99111 FileChunk *pChunk = p->endpoint.pChunk;
99112 int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize);
99113 int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset);
99114
99115 if( iChunkOffset==0 ){
99116 /* New chunk is required to extend the file. */
99117 FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize));
99118 if( !pNew ){
99119 return SQLITE_IOERR_NOMEM_BKPT;
99120 }
99121 pNew->pNext = 0;
99122 if( pChunk ){
99123 assert( p->pFirst );
99124 pChunk->pNext = pNew;
99125 }else{
99126 assert( !p->pFirst );
99127 p->pFirst = pNew;
99128 }
99129 p->endpoint.pChunk = pNew;
99130 }
99131
99132 memcpy(dest: (u8*)p->endpoint.pChunk->zChunk + iChunkOffset, src: zWrite, n: iSpace);
99133 zWrite += iSpace;
99134 nWrite -= iSpace;
99135 p->endpoint.iOffset += iSpace;
99136 }
99137 }
99138 }
99139
99140 return SQLITE_OK;
99141}
99142
99143/*
99144** Truncate the in-memory file.
99145*/
99146static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
99147 MemJournal *p = (MemJournal *)pJfd;
99148 assert( p->endpoint.pChunk==0 || p->endpoint.pChunk->pNext==0 );
99149 if( size<p->endpoint.iOffset ){
99150 FileChunk *pIter = 0;
99151 if( size==0 ){
99152 memjrnlFreeChunks(pFirst: p->pFirst);
99153 p->pFirst = 0;
99154 }else{
99155 i64 iOff = p->nChunkSize;
99156 for(pIter=p->pFirst; ALWAYS(pIter) && iOff<size; pIter=pIter->pNext){
99157 iOff += p->nChunkSize;
99158 }
99159 if( ALWAYS(pIter) ){
99160 memjrnlFreeChunks(pFirst: pIter->pNext);
99161 pIter->pNext = 0;
99162 }
99163 }
99164
99165 p->endpoint.pChunk = pIter;
99166 p->endpoint.iOffset = size;
99167 p->readpoint.pChunk = 0;
99168 p->readpoint.iOffset = 0;
99169 }
99170 return SQLITE_OK;
99171}
99172
99173/*
99174** Close the file.
99175*/
99176static int memjrnlClose(sqlite3_file *pJfd){
99177 MemJournal *p = (MemJournal *)pJfd;
99178 memjrnlFreeChunks(pFirst: p->pFirst);
99179 return SQLITE_OK;
99180}
99181
99182/*
99183** Sync the file.
99184**
99185** If the real file has been created, call its xSync method. Otherwise,
99186** syncing an in-memory journal is a no-op.
99187*/
99188static int memjrnlSync(sqlite3_file *pJfd, int flags){
99189 UNUSED_PARAMETER2(pJfd, flags);
99190 return SQLITE_OK;
99191}
99192
99193/*
99194** Query the size of the file in bytes.
99195*/
99196static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
99197 MemJournal *p = (MemJournal *)pJfd;
99198 *pSize = (sqlite_int64) p->endpoint.iOffset;
99199 return SQLITE_OK;
99200}
99201
99202/*
99203** Table of methods for MemJournal sqlite3_file object.
99204*/
99205static const struct sqlite3_io_methods MemJournalMethods = {
99206 1, /* iVersion */
99207 memjrnlClose, /* xClose */
99208 memjrnlRead, /* xRead */
99209 memjrnlWrite, /* xWrite */
99210 memjrnlTruncate, /* xTruncate */
99211 memjrnlSync, /* xSync */
99212 memjrnlFileSize, /* xFileSize */
99213 0, /* xLock */
99214 0, /* xUnlock */
99215 0, /* xCheckReservedLock */
99216 0, /* xFileControl */
99217 0, /* xSectorSize */
99218 0, /* xDeviceCharacteristics */
99219 0, /* xShmMap */
99220 0, /* xShmLock */
99221 0, /* xShmBarrier */
99222 0, /* xShmUnmap */
99223 0, /* xFetch */
99224 0 /* xUnfetch */
99225};
99226
99227/*
99228** Open a journal file.
99229**
99230** The behaviour of the journal file depends on the value of parameter
99231** nSpill. If nSpill is 0, then the journal file is always create and
99232** accessed using the underlying VFS. If nSpill is less than zero, then
99233** all content is always stored in main-memory. Finally, if nSpill is a
99234** positive value, then the journal file is initially created in-memory
99235** but may be flushed to disk later on. In this case the journal file is
99236** flushed to disk either when it grows larger than nSpill bytes in size,
99237** or when sqlite3JournalCreate() is called.
99238*/
99239SQLITE_PRIVATE int sqlite3JournalOpen(
99240 sqlite3_vfs *pVfs, /* The VFS to use for actual file I/O */
99241 const char *zName, /* Name of the journal file */
99242 sqlite3_file *pJfd, /* Preallocated, blank file handle */
99243 int flags, /* Opening flags */
99244 int nSpill /* Bytes buffered before opening the file */
99245){
99246 MemJournal *p = (MemJournal*)pJfd;
99247
99248 /* Zero the file-handle object. If nSpill was passed zero, initialize
99249 ** it using the sqlite3OsOpen() function of the underlying VFS. In this
99250 ** case none of the code in this module is executed as a result of calls
99251 ** made on the journal file-handle. */
99252 memset(s: p, c: 0, n: sizeof(MemJournal));
99253 if( nSpill==0 ){
99254 return sqlite3OsOpen(pVfs, zPath: zName, pFile: pJfd, flags, pFlagsOut: 0);
99255 }
99256
99257 if( nSpill>0 ){
99258 p->nChunkSize = nSpill;
99259 }else{
99260 p->nChunkSize = 8 + MEMJOURNAL_DFLT_FILECHUNKSIZE - sizeof(FileChunk);
99261 assert( MEMJOURNAL_DFLT_FILECHUNKSIZE==fileChunkSize(p->nChunkSize) );
99262 }
99263
99264 pJfd->pMethods = (const sqlite3_io_methods*)&MemJournalMethods;
99265 p->nSpill = nSpill;
99266 p->flags = flags;
99267 p->zJournal = zName;
99268 p->pVfs = pVfs;
99269 return SQLITE_OK;
99270}
99271
99272/*
99273** Open an in-memory journal file.
99274*/
99275SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *pJfd){
99276 sqlite3JournalOpen(pVfs: 0, zName: 0, pJfd, flags: 0, nSpill: -1);
99277}
99278
99279#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
99280 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
99281/*
99282** If the argument p points to a MemJournal structure that is not an
99283** in-memory-only journal file (i.e. is one that was opened with a +ve
99284** nSpill parameter or as SQLITE_OPEN_MAIN_JOURNAL), and the underlying
99285** file has not yet been created, create it now.
99286*/
99287SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *pJfd){
99288 int rc = SQLITE_OK;
99289 MemJournal *p = (MemJournal*)pJfd;
99290 if( pJfd->pMethods==&MemJournalMethods && (
99291#ifdef SQLITE_ENABLE_ATOMIC_WRITE
99292 p->nSpill>0
99293#else
99294 /* While this appears to not be possible without ATOMIC_WRITE, the
99295 ** paths are complex, so it seems prudent to leave the test in as
99296 ** a NEVER(), in case our analysis is subtly flawed. */
99297 NEVER(p->nSpill>0)
99298#endif
99299#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
99300 || (p->flags & SQLITE_OPEN_MAIN_JOURNAL)
99301#endif
99302 )){
99303 rc = memjrnlCreateFile(p);
99304 }
99305 return rc;
99306}
99307#endif
99308
99309/*
99310** The file-handle passed as the only argument is open on a journal file.
99311** Return true if this "journal file" is currently stored in heap memory,
99312** or false otherwise.
99313*/
99314SQLITE_PRIVATE int sqlite3JournalIsInMemory(sqlite3_file *p){
99315 return p->pMethods==&MemJournalMethods;
99316}
99317
99318/*
99319** Return the number of bytes required to store a JournalFile that uses vfs
99320** pVfs to create the underlying on-disk files.
99321*/
99322SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *pVfs){
99323 return MAX(pVfs->szOsFile, (int)sizeof(MemJournal));
99324}
99325
99326/************** End of memjournal.c ******************************************/
99327/************** Begin file walker.c ******************************************/
99328/*
99329** 2008 August 16
99330**
99331** The author disclaims copyright to this source code. In place of
99332** a legal notice, here is a blessing:
99333**
99334** May you do good and not evil.
99335** May you find forgiveness for yourself and forgive others.
99336** May you share freely, never taking more than you give.
99337**
99338*************************************************************************
99339** This file contains routines used for walking the parser tree for
99340** an SQL statement.
99341*/
99342/* #include "sqliteInt.h" */
99343/* #include <stdlib.h> */
99344/* #include <string.h> */
99345
99346
99347#if !defined(SQLITE_OMIT_WINDOWFUNC)
99348/*
99349** Walk all expressions linked into the list of Window objects passed
99350** as the second argument.
99351*/
99352static int walkWindowList(Walker *pWalker, Window *pList, int bOneOnly){
99353 Window *pWin;
99354 for(pWin=pList; pWin; pWin=pWin->pNextWin){
99355 int rc;
99356 rc = sqlite3WalkExprList(pWalker, pWin->pOrderBy);
99357 if( rc ) return WRC_Abort;
99358 rc = sqlite3WalkExprList(pWalker, pWin->pPartition);
99359 if( rc ) return WRC_Abort;
99360 rc = sqlite3WalkExpr(pWalker, pWin->pFilter);
99361 if( rc ) return WRC_Abort;
99362 rc = sqlite3WalkExpr(pWalker, pWin->pStart);
99363 if( rc ) return WRC_Abort;
99364 rc = sqlite3WalkExpr(pWalker, pWin->pEnd);
99365 if( rc ) return WRC_Abort;
99366 if( bOneOnly ) break;
99367 }
99368 return WRC_Continue;
99369}
99370#endif
99371
99372/*
99373** Walk an expression tree. Invoke the callback once for each node
99374** of the expression, while descending. (In other words, the callback
99375** is invoked before visiting children.)
99376**
99377** The return value from the callback should be one of the WRC_*
99378** constants to specify how to proceed with the walk.
99379**
99380** WRC_Continue Continue descending down the tree.
99381**
99382** WRC_Prune Do not descend into child nodes, but allow
99383** the walk to continue with sibling nodes.
99384**
99385** WRC_Abort Do no more callbacks. Unwind the stack and
99386** return from the top-level walk call.
99387**
99388** The return value from this routine is WRC_Abort to abandon the tree walk
99389** and WRC_Continue to continue.
99390*/
99391static SQLITE_NOINLINE int walkExpr(Walker *pWalker, Expr *pExpr){
99392 int rc;
99393 testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
99394 testcase( ExprHasProperty(pExpr, EP_Reduced) );
99395 while(1){
99396 rc = pWalker->xExprCallback(pWalker, pExpr);
99397 if( rc ) return rc & WRC_Abort;
99398 if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){
99399 assert( pExpr->x.pList==0 || pExpr->pRight==0 );
99400 if( pExpr->pLeft && walkExpr(pWalker, pExpr: pExpr->pLeft) ) return WRC_Abort;
99401 if( pExpr->pRight ){
99402 assert( !ExprHasProperty(pExpr, EP_WinFunc) );
99403 pExpr = pExpr->pRight;
99404 continue;
99405 }else if( ExprUseXSelect(pExpr) ){
99406 assert( !ExprHasProperty(pExpr, EP_WinFunc) );
99407 if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
99408 }else{
99409 if( pExpr->x.pList ){
99410 if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
99411 }
99412#ifndef SQLITE_OMIT_WINDOWFUNC
99413 if( ExprHasProperty(pExpr, EP_WinFunc) ){
99414 if( walkWindowList(pWalker, pList: pExpr->y.pWin, bOneOnly: 1) ) return WRC_Abort;
99415 }
99416#endif
99417 }
99418 }
99419 break;
99420 }
99421 return WRC_Continue;
99422}
99423SQLITE_PRIVATE int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
99424 return pExpr ? walkExpr(pWalker,pExpr) : WRC_Continue;
99425}
99426
99427/*
99428** Call sqlite3WalkExpr() for every expression in list p or until
99429** an abort request is seen.
99430*/
99431SQLITE_PRIVATE int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
99432 int i;
99433 struct ExprList_item *pItem;
99434 if( p ){
99435 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
99436 if( sqlite3WalkExpr(pWalker, pExpr: pItem->pExpr) ) return WRC_Abort;
99437 }
99438 }
99439 return WRC_Continue;
99440}
99441
99442/*
99443** This is a no-op callback for Walker->xSelectCallback2. If this
99444** callback is set, then the Select->pWinDefn list is traversed.
99445*/
99446SQLITE_PRIVATE void sqlite3WalkWinDefnDummyCallback(Walker *pWalker, Select *p){
99447 UNUSED_PARAMETER(pWalker);
99448 UNUSED_PARAMETER(p);
99449 /* No-op */
99450}
99451
99452/*
99453** Walk all expressions associated with SELECT statement p. Do
99454** not invoke the SELECT callback on p, but do (of course) invoke
99455** any expr callbacks and SELECT callbacks that come from subqueries.
99456** Return WRC_Abort or WRC_Continue.
99457*/
99458SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
99459 if( sqlite3WalkExprList(pWalker, p: p->pEList) ) return WRC_Abort;
99460 if( sqlite3WalkExpr(pWalker, pExpr: p->pWhere) ) return WRC_Abort;
99461 if( sqlite3WalkExprList(pWalker, p: p->pGroupBy) ) return WRC_Abort;
99462 if( sqlite3WalkExpr(pWalker, pExpr: p->pHaving) ) return WRC_Abort;
99463 if( sqlite3WalkExprList(pWalker, p: p->pOrderBy) ) return WRC_Abort;
99464 if( sqlite3WalkExpr(pWalker, pExpr: p->pLimit) ) return WRC_Abort;
99465#if !defined(SQLITE_OMIT_WINDOWFUNC)
99466 if( p->pWinDefn ){
99467 Parse *pParse;
99468 if( pWalker->xSelectCallback2==sqlite3WalkWinDefnDummyCallback
99469 || ((pParse = pWalker->pParse)!=0 && IN_RENAME_OBJECT)
99470#ifndef SQLITE_OMIT_CTE
99471 || pWalker->xSelectCallback2==sqlite3SelectPopWith
99472#endif
99473 ){
99474 /* The following may return WRC_Abort if there are unresolvable
99475 ** symbols (e.g. a table that does not exist) in a window definition. */
99476 int rc = walkWindowList(pWalker, pList: p->pWinDefn, bOneOnly: 0);
99477 return rc;
99478 }
99479 }
99480#endif
99481 return WRC_Continue;
99482}
99483
99484/*
99485** Walk the parse trees associated with all subqueries in the
99486** FROM clause of SELECT statement p. Do not invoke the select
99487** callback on p, but do invoke it on each FROM clause subquery
99488** and on any subqueries further down in the tree. Return
99489** WRC_Abort or WRC_Continue;
99490*/
99491SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
99492 SrcList *pSrc;
99493 int i;
99494 SrcItem *pItem;
99495
99496 pSrc = p->pSrc;
99497 if( ALWAYS(pSrc) ){
99498 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
99499 if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){
99500 return WRC_Abort;
99501 }
99502 if( pItem->fg.isTabFunc
99503 && sqlite3WalkExprList(pWalker, p: pItem->u1.pFuncArg)
99504 ){
99505 return WRC_Abort;
99506 }
99507 }
99508 }
99509 return WRC_Continue;
99510}
99511
99512/*
99513** Call sqlite3WalkExpr() for every expression in Select statement p.
99514** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
99515** on the compound select chain, p->pPrior.
99516**
99517** If it is not NULL, the xSelectCallback() callback is invoked before
99518** the walk of the expressions and FROM clause. The xSelectCallback2()
99519** method is invoked following the walk of the expressions and FROM clause,
99520** but only if both xSelectCallback and xSelectCallback2 are both non-NULL
99521** and if the expressions and FROM clause both return WRC_Continue;
99522**
99523** Return WRC_Continue under normal conditions. Return WRC_Abort if
99524** there is an abort request.
99525**
99526** If the Walker does not have an xSelectCallback() then this routine
99527** is a no-op returning WRC_Continue.
99528*/
99529SQLITE_PRIVATE int sqlite3WalkSelect(Walker *pWalker, Select *p){
99530 int rc;
99531 if( p==0 ) return WRC_Continue;
99532 if( pWalker->xSelectCallback==0 ) return WRC_Continue;
99533 do{
99534 rc = pWalker->xSelectCallback(pWalker, p);
99535 if( rc ) return rc & WRC_Abort;
99536 if( sqlite3WalkSelectExpr(pWalker, p)
99537 || sqlite3WalkSelectFrom(pWalker, p)
99538 ){
99539 return WRC_Abort;
99540 }
99541 if( pWalker->xSelectCallback2 ){
99542 pWalker->xSelectCallback2(pWalker, p);
99543 }
99544 p = p->pPrior;
99545 }while( p!=0 );
99546 return WRC_Continue;
99547}
99548
99549/* Increase the walkerDepth when entering a subquery, and
99550** descrease when leaving the subquery.
99551*/
99552SQLITE_PRIVATE int sqlite3WalkerDepthIncrease(Walker *pWalker, Select *pSelect){
99553 UNUSED_PARAMETER(pSelect);
99554 pWalker->walkerDepth++;
99555 return WRC_Continue;
99556}
99557SQLITE_PRIVATE void sqlite3WalkerDepthDecrease(Walker *pWalker, Select *pSelect){
99558 UNUSED_PARAMETER(pSelect);
99559 pWalker->walkerDepth--;
99560}
99561
99562
99563/*
99564** No-op routine for the parse-tree walker.
99565**
99566** When this routine is the Walker.xExprCallback then expression trees
99567** are walked without any actions being taken at each node. Presumably,
99568** when this routine is used for Walker.xExprCallback then
99569** Walker.xSelectCallback is set to do something useful for every
99570** subquery in the parser tree.
99571*/
99572SQLITE_PRIVATE int sqlite3ExprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
99573 UNUSED_PARAMETER2(NotUsed, NotUsed2);
99574 return WRC_Continue;
99575}
99576
99577/*
99578** No-op routine for the parse-tree walker for SELECT statements.
99579** subquery in the parser tree.
99580*/
99581SQLITE_PRIVATE int sqlite3SelectWalkNoop(Walker *NotUsed, Select *NotUsed2){
99582 UNUSED_PARAMETER2(NotUsed, NotUsed2);
99583 return WRC_Continue;
99584}
99585
99586/************** End of walker.c **********************************************/
99587/************** Begin file resolve.c *****************************************/
99588/*
99589** 2008 August 18
99590**
99591** The author disclaims copyright to this source code. In place of
99592** a legal notice, here is a blessing:
99593**
99594** May you do good and not evil.
99595** May you find forgiveness for yourself and forgive others.
99596** May you share freely, never taking more than you give.
99597**
99598*************************************************************************
99599**
99600** This file contains routines used for walking the parser tree and
99601** resolve all identifiers by associating them with a particular
99602** table and column.
99603*/
99604/* #include "sqliteInt.h" */
99605
99606/*
99607** Magic table number to mean the EXCLUDED table in an UPSERT statement.
99608*/
99609#define EXCLUDED_TABLE_NUMBER 2
99610
99611/*
99612** Walk the expression tree pExpr and increase the aggregate function
99613** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
99614** This needs to occur when copying a TK_AGG_FUNCTION node from an
99615** outer query into an inner subquery.
99616**
99617** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..)
99618** is a helper function - a callback for the tree walker.
99619**
99620** See also the sqlite3WindowExtraAggFuncDepth() routine in window.c
99621*/
99622static int incrAggDepth(Walker *pWalker, Expr *pExpr){
99623 if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n;
99624 return WRC_Continue;
99625}
99626static void incrAggFunctionDepth(Expr *pExpr, int N){
99627 if( N>0 ){
99628 Walker w;
99629 memset(s: &w, c: 0, n: sizeof(w));
99630 w.xExprCallback = incrAggDepth;
99631 w.u.n = N;
99632 sqlite3WalkExpr(pWalker: &w, pExpr);
99633 }
99634}
99635
99636/*
99637** Turn the pExpr expression into an alias for the iCol-th column of the
99638** result set in pEList.
99639**
99640** If the reference is followed by a COLLATE operator, then make sure
99641** the COLLATE operator is preserved. For example:
99642**
99643** SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase;
99644**
99645** Should be transformed into:
99646**
99647** SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase;
99648**
99649** The nSubquery parameter specifies how many levels of subquery the
99650** alias is removed from the original expression. The usual value is
99651** zero but it might be more if the alias is contained within a subquery
99652** of the original expression. The Expr.op2 field of TK_AGG_FUNCTION
99653** structures must be increased by the nSubquery amount.
99654*/
99655static void resolveAlias(
99656 Parse *pParse, /* Parsing context */
99657 ExprList *pEList, /* A result set */
99658 int iCol, /* A column in the result set. 0..pEList->nExpr-1 */
99659 Expr *pExpr, /* Transform this into an alias to the result set */
99660 int nSubquery /* Number of subqueries that the label is moving */
99661){
99662 Expr *pOrig; /* The iCol-th column of the result set */
99663 Expr *pDup; /* Copy of pOrig */
99664 sqlite3 *db; /* The database connection */
99665
99666 assert( iCol>=0 && iCol<pEList->nExpr );
99667 pOrig = pEList->a[iCol].pExpr;
99668 assert( pOrig!=0 );
99669 db = pParse->db;
99670 pDup = sqlite3ExprDup(db, pOrig, 0);
99671 if( db->mallocFailed ){
99672 sqlite3ExprDelete(db, pDup);
99673 pDup = 0;
99674 }else{
99675 incrAggFunctionDepth(pExpr: pDup, N: nSubquery);
99676 if( pExpr->op==TK_COLLATE ){
99677 assert( !ExprHasProperty(pExpr, EP_IntValue) );
99678 pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
99679 }
99680
99681 /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This
99682 ** prevents ExprDelete() from deleting the Expr structure itself,
99683 ** allowing it to be repopulated by the memcpy() on the following line.
99684 ** The pExpr->u.zToken might point into memory that will be freed by the
99685 ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to
99686 ** make a copy of the token before doing the sqlite3DbFree().
99687 */
99688 ExprSetProperty(pExpr, EP_Static);
99689 sqlite3ExprDelete(db, pExpr);
99690 memcpy(dest: pExpr, src: pDup, n: sizeof(*pExpr));
99691 if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){
99692 assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 );
99693 pExpr->u.zToken = sqlite3DbStrDup(db, z: pExpr->u.zToken);
99694 pExpr->flags |= EP_MemToken;
99695 }
99696 if( ExprHasProperty(pExpr, EP_WinFunc) ){
99697 if( ALWAYS(pExpr->y.pWin!=0) ){
99698 pExpr->y.pWin->pOwner = pExpr;
99699 }
99700 }
99701 sqlite3DbFree(db, p: pDup);
99702 }
99703}
99704
99705
99706/*
99707** Return TRUE if the name zCol occurs anywhere in the USING clause.
99708**
99709** Return FALSE if the USING clause is NULL or if it does not contain
99710** zCol.
99711*/
99712static int nameInUsingClause(IdList *pUsing, const char *zCol){
99713 if( pUsing ){
99714 int k;
99715 for(k=0; k<pUsing->nId; k++){
99716 if( sqlite3StrICmp(zLeft: pUsing->a[k].zName, zRight: zCol)==0 ) return 1;
99717 }
99718 }
99719 return 0;
99720}
99721
99722/*
99723** Subqueries stores the original database, table and column names for their
99724** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN".
99725** Check to see if the zSpan given to this routine matches the zDb, zTab,
99726** and zCol. If any of zDb, zTab, and zCol are NULL then those fields will
99727** match anything.
99728*/
99729SQLITE_PRIVATE int sqlite3MatchEName(
99730 const struct ExprList_item *pItem,
99731 const char *zCol,
99732 const char *zTab,
99733 const char *zDb
99734){
99735 int n;
99736 const char *zSpan;
99737 if( pItem->eEName!=ENAME_TAB ) return 0;
99738 zSpan = pItem->zEName;
99739 for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
99740 if( zDb && (sqlite3StrNICmp(zLeft: zSpan, zRight: zDb, N: n)!=0 || zDb[n]!=0) ){
99741 return 0;
99742 }
99743 zSpan += n+1;
99744 for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
99745 if( zTab && (sqlite3StrNICmp(zLeft: zSpan, zRight: zTab, N: n)!=0 || zTab[n]!=0) ){
99746 return 0;
99747 }
99748 zSpan += n+1;
99749 if( zCol && sqlite3StrICmp(zLeft: zSpan, zRight: zCol)!=0 ){
99750 return 0;
99751 }
99752 return 1;
99753}
99754
99755/*
99756** Return TRUE if the double-quoted string mis-feature should be supported.
99757*/
99758static int areDoubleQuotedStringsEnabled(sqlite3 *db, NameContext *pTopNC){
99759 if( db->init.busy ) return 1; /* Always support for legacy schemas */
99760 if( pTopNC->ncFlags & NC_IsDDL ){
99761 /* Currently parsing a DDL statement */
99762 if( sqlite3WritableSchema(db) && (db->flags & SQLITE_DqsDML)!=0 ){
99763 return 1;
99764 }
99765 return (db->flags & SQLITE_DqsDDL)!=0;
99766 }else{
99767 /* Currently parsing a DML statement */
99768 return (db->flags & SQLITE_DqsDML)!=0;
99769 }
99770}
99771
99772/*
99773** The argument is guaranteed to be a non-NULL Expr node of type TK_COLUMN.
99774** return the appropriate colUsed mask.
99775*/
99776SQLITE_PRIVATE Bitmask sqlite3ExprColUsed(Expr *pExpr){
99777 int n;
99778 Table *pExTab;
99779
99780 n = pExpr->iColumn;
99781 assert( ExprUseYTab(pExpr) );
99782 pExTab = pExpr->y.pTab;
99783 assert( pExTab!=0 );
99784 if( (pExTab->tabFlags & TF_HasGenerated)!=0
99785 && (pExTab->aCol[n].colFlags & COLFLAG_GENERATED)!=0
99786 ){
99787 testcase( pExTab->nCol==BMS-1 );
99788 testcase( pExTab->nCol==BMS );
99789 return pExTab->nCol>=BMS ? ALLBITS : MASKBIT(pExTab->nCol)-1;
99790 }else{
99791 testcase( n==BMS-1 );
99792 testcase( n==BMS );
99793 if( n>=BMS ) n = BMS-1;
99794 return ((Bitmask)1)<<n;
99795 }
99796}
99797
99798/*
99799** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
99800** that name in the set of source tables in pSrcList and make the pExpr
99801** expression node refer back to that source column. The following changes
99802** are made to pExpr:
99803**
99804** pExpr->iDb Set the index in db->aDb[] of the database X
99805** (even if X is implied).
99806** pExpr->iTable Set to the cursor number for the table obtained
99807** from pSrcList.
99808** pExpr->y.pTab Points to the Table structure of X.Y (even if
99809** X and/or Y are implied.)
99810** pExpr->iColumn Set to the column number within the table.
99811** pExpr->op Set to TK_COLUMN.
99812** pExpr->pLeft Any expression this points to is deleted
99813** pExpr->pRight Any expression this points to is deleted.
99814**
99815** The zDb variable is the name of the database (the "X"). This value may be
99816** NULL meaning that name is of the form Y.Z or Z. Any available database
99817** can be used. The zTable variable is the name of the table (the "Y"). This
99818** value can be NULL if zDb is also NULL. If zTable is NULL it
99819** means that the form of the name is Z and that columns from any table
99820** can be used.
99821**
99822** If the name cannot be resolved unambiguously, leave an error message
99823** in pParse and return WRC_Abort. Return WRC_Prune on success.
99824*/
99825static int lookupName(
99826 Parse *pParse, /* The parsing context */
99827 const char *zDb, /* Name of the database containing table, or NULL */
99828 const char *zTab, /* Name of table containing column, or NULL */
99829 const char *zCol, /* Name of the column. */
99830 NameContext *pNC, /* The name context used to resolve the name */
99831 Expr *pExpr /* Make this EXPR node point to the selected column */
99832){
99833 int i, j; /* Loop counters */
99834 int cnt = 0; /* Number of matching column names */
99835 int cntTab = 0; /* Number of matching table names */
99836 int nSubquery = 0; /* How many levels of subquery */
99837 sqlite3 *db = pParse->db; /* The database connection */
99838 SrcItem *pItem; /* Use for looping over pSrcList items */
99839 SrcItem *pMatch = 0; /* The matching pSrcList item */
99840 NameContext *pTopNC = pNC; /* First namecontext in the list */
99841 Schema *pSchema = 0; /* Schema of the expression */
99842 int eNewExprOp = TK_COLUMN; /* New value for pExpr->op on success */
99843 Table *pTab = 0; /* Table hold the row */
99844 Column *pCol; /* A column of pTab */
99845
99846 assert( pNC ); /* the name context cannot be NULL. */
99847 assert( zCol ); /* The Z in X.Y.Z cannot be NULL */
99848 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
99849
99850 /* Initialize the node to no-match */
99851 pExpr->iTable = -1;
99852 ExprSetVVAProperty(pExpr, EP_NoReduce);
99853
99854 /* Translate the schema name in zDb into a pointer to the corresponding
99855 ** schema. If not found, pSchema will remain NULL and nothing will match
99856 ** resulting in an appropriate error message toward the end of this routine
99857 */
99858 if( zDb ){
99859 testcase( pNC->ncFlags & NC_PartIdx );
99860 testcase( pNC->ncFlags & NC_IsCheck );
99861 if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){
99862 /* Silently ignore database qualifiers inside CHECK constraints and
99863 ** partial indices. Do not raise errors because that might break
99864 ** legacy and because it does not hurt anything to just ignore the
99865 ** database name. */
99866 zDb = 0;
99867 }else{
99868 for(i=0; i<db->nDb; i++){
99869 assert( db->aDb[i].zDbSName );
99870 if( sqlite3StrICmp(zLeft: db->aDb[i].zDbSName,zRight: zDb)==0 ){
99871 pSchema = db->aDb[i].pSchema;
99872 break;
99873 }
99874 }
99875 if( i==db->nDb && sqlite3StrICmp(zLeft: "main", zRight: zDb)==0 ){
99876 /* This branch is taken when the main database has been renamed
99877 ** using SQLITE_DBCONFIG_MAINDBNAME. */
99878 pSchema = db->aDb[0].pSchema;
99879 zDb = db->aDb[0].zDbSName;
99880 }
99881 }
99882 }
99883
99884 /* Start at the inner-most context and move outward until a match is found */
99885 assert( pNC && cnt==0 );
99886 do{
99887 ExprList *pEList;
99888 SrcList *pSrcList = pNC->pSrcList;
99889
99890 if( pSrcList ){
99891 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
99892 u8 hCol;
99893 pTab = pItem->pTab;
99894 assert( pTab!=0 && pTab->zName!=0 );
99895 assert( pTab->nCol>0 || pParse->nErr );
99896 if( pItem->pSelect && (pItem->pSelect->selFlags & SF_NestedFrom)!=0 ){
99897 int hit = 0;
99898 pEList = pItem->pSelect->pEList;
99899 for(j=0; j<pEList->nExpr; j++){
99900 if( sqlite3MatchEName(pItem: &pEList->a[j], zCol, zTab, zDb) ){
99901 cnt++;
99902 cntTab = 2;
99903 pMatch = pItem;
99904 pExpr->iColumn = j;
99905 hit = 1;
99906 }
99907 }
99908 if( hit || zTab==0 ) continue;
99909 }
99910 if( zDb && pTab->pSchema!=pSchema ){
99911 continue;
99912 }
99913 if( zTab ){
99914 const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
99915 assert( zTabName!=0 );
99916 if( sqlite3StrICmp(zLeft: zTabName, zRight: zTab)!=0 ){
99917 continue;
99918 }
99919 assert( ExprUseYTab(pExpr) );
99920 if( IN_RENAME_OBJECT && pItem->zAlias ){
99921 sqlite3RenameTokenRemap(pParse, pTo: 0, pFrom: (void*)&pExpr->y.pTab);
99922 }
99923 }
99924 hCol = sqlite3StrIHash(z: zCol);
99925 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
99926 if( pCol->hName==hCol
99927 && sqlite3StrICmp(zLeft: pCol->zCnName, zRight: zCol)==0
99928 ){
99929 /* If there has been exactly one prior match and this match
99930 ** is for the right-hand table of a NATURAL JOIN or is in a
99931 ** USING clause, then skip this match.
99932 */
99933 if( cnt==1 ){
99934 if( pItem->fg.jointype & JT_NATURAL ) continue;
99935 if( nameInUsingClause(pUsing: pItem->pUsing, zCol) ) continue;
99936 }
99937 cnt++;
99938 pMatch = pItem;
99939 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
99940 pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
99941 break;
99942 }
99943 }
99944 if( 0==cnt && VisibleRowid(pTab) ){
99945 cntTab++;
99946 pMatch = pItem;
99947 }
99948 }
99949 if( pMatch ){
99950 pExpr->iTable = pMatch->iCursor;
99951 assert( ExprUseYTab(pExpr) );
99952 pExpr->y.pTab = pMatch->pTab;
99953 /* RIGHT JOIN not (yet) supported */
99954 assert( (pMatch->fg.jointype & JT_RIGHT)==0 );
99955 if( (pMatch->fg.jointype & JT_LEFT)!=0 ){
99956 ExprSetProperty(pExpr, EP_CanBeNull);
99957 }
99958 pSchema = pExpr->y.pTab->pSchema;
99959 }
99960 } /* if( pSrcList ) */
99961
99962#if !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT)
99963 /* If we have not already resolved the name, then maybe
99964 ** it is a new.* or old.* trigger argument reference. Or
99965 ** maybe it is an excluded.* from an upsert. Or maybe it is
99966 ** a reference in the RETURNING clause to a table being modified.
99967 */
99968 if( cnt==0 && zDb==0 ){
99969 pTab = 0;
99970#ifndef SQLITE_OMIT_TRIGGER
99971 if( pParse->pTriggerTab!=0 ){
99972 int op = pParse->eTriggerOp;
99973 assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
99974 if( pParse->bReturning ){
99975 if( (pNC->ncFlags & NC_UBaseReg)!=0
99976 && (zTab==0 || sqlite3StrICmp(zLeft: zTab,zRight: pParse->pTriggerTab->zName)==0)
99977 ){
99978 pExpr->iTable = op!=TK_DELETE;
99979 pTab = pParse->pTriggerTab;
99980 }
99981 }else if( op!=TK_DELETE && zTab && sqlite3StrICmp(zLeft: "new",zRight: zTab) == 0 ){
99982 pExpr->iTable = 1;
99983 pTab = pParse->pTriggerTab;
99984 }else if( op!=TK_INSERT && zTab && sqlite3StrICmp(zLeft: "old",zRight: zTab)==0 ){
99985 pExpr->iTable = 0;
99986 pTab = pParse->pTriggerTab;
99987 }
99988 }
99989#endif /* SQLITE_OMIT_TRIGGER */
99990#ifndef SQLITE_OMIT_UPSERT
99991 if( (pNC->ncFlags & NC_UUpsert)!=0 && zTab!=0 ){
99992 Upsert *pUpsert = pNC->uNC.pUpsert;
99993 if( pUpsert && sqlite3StrICmp(zLeft: "excluded",zRight: zTab)==0 ){
99994 pTab = pUpsert->pUpsertSrc->a[0].pTab;
99995 pExpr->iTable = EXCLUDED_TABLE_NUMBER;
99996 }
99997 }
99998#endif /* SQLITE_OMIT_UPSERT */
99999
100000 if( pTab ){
100001 int iCol;
100002 u8 hCol = sqlite3StrIHash(z: zCol);
100003 pSchema = pTab->pSchema;
100004 cntTab++;
100005 for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
100006 if( pCol->hName==hCol
100007 && sqlite3StrICmp(zLeft: pCol->zCnName, zRight: zCol)==0
100008 ){
100009 if( iCol==pTab->iPKey ){
100010 iCol = -1;
100011 }
100012 break;
100013 }
100014 }
100015 if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){
100016 /* IMP: R-51414-32910 */
100017 iCol = -1;
100018 }
100019 if( iCol<pTab->nCol ){
100020 cnt++;
100021 pMatch = 0;
100022#ifndef SQLITE_OMIT_UPSERT
100023 if( pExpr->iTable==EXCLUDED_TABLE_NUMBER ){
100024 testcase( iCol==(-1) );
100025 assert( ExprUseYTab(pExpr) );
100026 if( IN_RENAME_OBJECT ){
100027 pExpr->iColumn = iCol;
100028 pExpr->y.pTab = pTab;
100029 eNewExprOp = TK_COLUMN;
100030 }else{
100031 pExpr->iTable = pNC->uNC.pUpsert->regData +
100032 sqlite3TableColumnToStorage(pTab, iCol);
100033 eNewExprOp = TK_REGISTER;
100034 }
100035 }else
100036#endif /* SQLITE_OMIT_UPSERT */
100037 {
100038 assert( ExprUseYTab(pExpr) );
100039 pExpr->y.pTab = pTab;
100040 if( pParse->bReturning ){
100041 eNewExprOp = TK_REGISTER;
100042 pExpr->iTable = pNC->uNC.iBaseReg + (pTab->nCol+1)*pExpr->iTable +
100043 sqlite3TableColumnToStorage(pTab, iCol) + 1;
100044 }else{
100045 pExpr->iColumn = (i16)iCol;
100046 eNewExprOp = TK_TRIGGER;
100047#ifndef SQLITE_OMIT_TRIGGER
100048 if( iCol<0 ){
100049 pExpr->affExpr = SQLITE_AFF_INTEGER;
100050 }else if( pExpr->iTable==0 ){
100051 testcase( iCol==31 );
100052 testcase( iCol==32 );
100053 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
100054 }else{
100055 testcase( iCol==31 );
100056 testcase( iCol==32 );
100057 pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
100058 }
100059#endif /* SQLITE_OMIT_TRIGGER */
100060 }
100061 }
100062 }
100063 }
100064 }
100065#endif /* !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT) */
100066
100067 /*
100068 ** Perhaps the name is a reference to the ROWID
100069 */
100070 if( cnt==0
100071 && cntTab==1
100072 && pMatch
100073 && (pNC->ncFlags & (NC_IdxExpr|NC_GenCol))==0
100074 && sqlite3IsRowid(zCol)
100075 && ALWAYS(VisibleRowid(pMatch->pTab))
100076 ){
100077 cnt = 1;
100078 pExpr->iColumn = -1;
100079 pExpr->affExpr = SQLITE_AFF_INTEGER;
100080 }
100081
100082 /*
100083 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
100084 ** might refer to an result-set alias. This happens, for example, when
100085 ** we are resolving names in the WHERE clause of the following command:
100086 **
100087 ** SELECT a+b AS x FROM table WHERE x<10;
100088 **
100089 ** In cases like this, replace pExpr with a copy of the expression that
100090 ** forms the result set entry ("a+b" in the example) and return immediately.
100091 ** Note that the expression in the result set should have already been
100092 ** resolved by the time the WHERE clause is resolved.
100093 **
100094 ** The ability to use an output result-set column in the WHERE, GROUP BY,
100095 ** or HAVING clauses, or as part of a larger expression in the ORDER BY
100096 ** clause is not standard SQL. This is a (goofy) SQLite extension, that
100097 ** is supported for backwards compatibility only. Hence, we issue a warning
100098 ** on sqlite3_log() whenever the capability is used.
100099 */
100100 if( cnt==0
100101 && (pNC->ncFlags & NC_UEList)!=0
100102 && zTab==0
100103 ){
100104 pEList = pNC->uNC.pEList;
100105 assert( pEList!=0 );
100106 for(j=0; j<pEList->nExpr; j++){
100107 char *zAs = pEList->a[j].zEName;
100108 if( pEList->a[j].eEName==ENAME_NAME
100109 && sqlite3_stricmp(zLeft: zAs, zRight: zCol)==0
100110 ){
100111 Expr *pOrig;
100112 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
100113 assert( ExprUseXList(pExpr)==0 || pExpr->x.pList==0 );
100114 assert( ExprUseXSelect(pExpr)==0 || pExpr->x.pSelect==0 );
100115 pOrig = pEList->a[j].pExpr;
100116 if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
100117 sqlite3ErrorMsg(pParse, zFormat: "misuse of aliased aggregate %s", zAs);
100118 return WRC_Abort;
100119 }
100120 if( ExprHasProperty(pOrig, EP_Win)
100121 && ((pNC->ncFlags&NC_AllowWin)==0 || pNC!=pTopNC )
100122 ){
100123 sqlite3ErrorMsg(pParse, zFormat: "misuse of aliased window function %s",zAs);
100124 return WRC_Abort;
100125 }
100126 if( sqlite3ExprVectorSize(pExpr: pOrig)!=1 ){
100127 sqlite3ErrorMsg(pParse, zFormat: "row value misused");
100128 return WRC_Abort;
100129 }
100130 resolveAlias(pParse, pEList, iCol: j, pExpr, nSubquery);
100131 cnt = 1;
100132 pMatch = 0;
100133 assert( zTab==0 && zDb==0 );
100134 if( IN_RENAME_OBJECT ){
100135 sqlite3RenameTokenRemap(pParse, pTo: 0, pFrom: (void*)pExpr);
100136 }
100137 goto lookupname_end;
100138 }
100139 }
100140 }
100141
100142 /* Advance to the next name context. The loop will exit when either
100143 ** we have a match (cnt>0) or when we run out of name contexts.
100144 */
100145 if( cnt ) break;
100146 pNC = pNC->pNext;
100147 nSubquery++;
100148 }while( pNC );
100149
100150
100151 /*
100152 ** If X and Y are NULL (in other words if only the column name Z is
100153 ** supplied) and the value of Z is enclosed in double-quotes, then
100154 ** Z is a string literal if it doesn't match any column names. In that
100155 ** case, we need to return right away and not make any changes to
100156 ** pExpr.
100157 **
100158 ** Because no reference was made to outer contexts, the pNC->nRef
100159 ** fields are not changed in any context.
100160 */
100161 if( cnt==0 && zTab==0 ){
100162 assert( pExpr->op==TK_ID );
100163 if( ExprHasProperty(pExpr,EP_DblQuoted)
100164 && areDoubleQuotedStringsEnabled(db, pTopNC)
100165 ){
100166 /* If a double-quoted identifier does not match any known column name,
100167 ** then treat it as a string.
100168 **
100169 ** This hack was added in the early days of SQLite in a misguided attempt
100170 ** to be compatible with MySQL 3.x, which used double-quotes for strings.
100171 ** I now sorely regret putting in this hack. The effect of this hack is
100172 ** that misspelled identifier names are silently converted into strings
100173 ** rather than causing an error, to the frustration of countless
100174 ** programmers. To all those frustrated programmers, my apologies.
100175 **
100176 ** Someday, I hope to get rid of this hack. Unfortunately there is
100177 ** a huge amount of legacy SQL that uses it. So for now, we just
100178 ** issue a warning.
100179 */
100180 sqlite3_log(SQLITE_WARNING,
100181 zFormat: "double-quoted string literal: \"%w\"", zCol);
100182#ifdef SQLITE_ENABLE_NORMALIZE
100183 sqlite3VdbeAddDblquoteStr(db, pParse->pVdbe, zCol);
100184#endif
100185 pExpr->op = TK_STRING;
100186 memset(s: &pExpr->y, c: 0, n: sizeof(pExpr->y));
100187 return WRC_Prune;
100188 }
100189 if( sqlite3ExprIdToTrueFalse(pExpr) ){
100190 return WRC_Prune;
100191 }
100192 }
100193
100194 /*
100195 ** cnt==0 means there was not match. cnt>1 means there were two or
100196 ** more matches. Either way, we have an error.
100197 */
100198 if( cnt!=1 ){
100199 const char *zErr;
100200 zErr = cnt==0 ? "no such column" : "ambiguous column name";
100201 if( zDb ){
100202 sqlite3ErrorMsg(pParse, zFormat: "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
100203 }else if( zTab ){
100204 sqlite3ErrorMsg(pParse, zFormat: "%s: %s.%s", zErr, zTab, zCol);
100205 }else{
100206 sqlite3ErrorMsg(pParse, zFormat: "%s: %s", zErr, zCol);
100207 }
100208 pParse->checkSchema = 1;
100209 pTopNC->nNcErr++;
100210 }
100211
100212 /* If a column from a table in pSrcList is referenced, then record
100213 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
100214 ** bit 0 to be set. Column 1 sets bit 1. And so forth. Bit 63 is
100215 ** set if the 63rd or any subsequent column is used.
100216 **
100217 ** The colUsed mask is an optimization used to help determine if an
100218 ** index is a covering index. The correct answer is still obtained
100219 ** if the mask contains extra set bits. However, it is important to
100220 ** avoid setting bits beyond the maximum column number of the table.
100221 ** (See ticket [b92e5e8ec2cdbaa1]).
100222 **
100223 ** If a generated column is referenced, set bits for every column
100224 ** of the table.
100225 */
100226 if( pExpr->iColumn>=0 && pMatch!=0 ){
100227 pMatch->colUsed |= sqlite3ExprColUsed(pExpr);
100228 }
100229
100230 /* Clean up and return
100231 */
100232 if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){
100233 sqlite3ExprDelete(db, pExpr->pLeft);
100234 pExpr->pLeft = 0;
100235 sqlite3ExprDelete(db, pExpr->pRight);
100236 pExpr->pRight = 0;
100237 }
100238 pExpr->op = eNewExprOp;
100239 ExprSetProperty(pExpr, EP_Leaf);
100240lookupname_end:
100241 if( cnt==1 ){
100242 assert( pNC!=0 );
100243#ifndef SQLITE_OMIT_AUTHORIZATION
100244 if( pParse->db->xAuth
100245 && (pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER)
100246 ){
100247 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
100248 }
100249#endif
100250 /* Increment the nRef value on all name contexts from TopNC up to
100251 ** the point where the name matched. */
100252 for(;;){
100253 assert( pTopNC!=0 );
100254 pTopNC->nRef++;
100255 if( pTopNC==pNC ) break;
100256 pTopNC = pTopNC->pNext;
100257 }
100258 return WRC_Prune;
100259 } else {
100260 return WRC_Abort;
100261 }
100262}
100263
100264/*
100265** Allocate and return a pointer to an expression to load the column iCol
100266** from datasource iSrc in SrcList pSrc.
100267*/
100268SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
100269 Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
100270 if( p ){
100271 SrcItem *pItem = &pSrc->a[iSrc];
100272 Table *pTab;
100273 assert( ExprUseYTab(p) );
100274 pTab = p->y.pTab = pItem->pTab;
100275 p->iTable = pItem->iCursor;
100276 if( p->y.pTab->iPKey==iCol ){
100277 p->iColumn = -1;
100278 }else{
100279 p->iColumn = (ynVar)iCol;
100280 if( (pTab->tabFlags & TF_HasGenerated)!=0
100281 && (pTab->aCol[iCol].colFlags & COLFLAG_GENERATED)!=0
100282 ){
100283 testcase( pTab->nCol==63 );
100284 testcase( pTab->nCol==64 );
100285 pItem->colUsed = pTab->nCol>=64 ? ALLBITS : MASKBIT(pTab->nCol)-1;
100286 }else{
100287 testcase( iCol==BMS );
100288 testcase( iCol==BMS-1 );
100289 pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
100290 }
100291 }
100292 }
100293 return p;
100294}
100295
100296/*
100297** Report an error that an expression is not valid for some set of
100298** pNC->ncFlags values determined by validMask.
100299**
100300** static void notValid(
100301** Parse *pParse, // Leave error message here
100302** NameContext *pNC, // The name context
100303** const char *zMsg, // Type of error
100304** int validMask, // Set of contexts for which prohibited
100305** Expr *pExpr // Invalidate this expression on error
100306** ){...}
100307**
100308** As an optimization, since the conditional is almost always false
100309** (because errors are rare), the conditional is moved outside of the
100310** function call using a macro.
100311*/
100312static void notValidImpl(
100313 Parse *pParse, /* Leave error message here */
100314 NameContext *pNC, /* The name context */
100315 const char *zMsg, /* Type of error */
100316 Expr *pExpr /* Invalidate this expression on error */
100317){
100318 const char *zIn = "partial index WHERE clauses";
100319 if( pNC->ncFlags & NC_IdxExpr ) zIn = "index expressions";
100320#ifndef SQLITE_OMIT_CHECK
100321 else if( pNC->ncFlags & NC_IsCheck ) zIn = "CHECK constraints";
100322#endif
100323#ifndef SQLITE_OMIT_GENERATED_COLUMNS
100324 else if( pNC->ncFlags & NC_GenCol ) zIn = "generated columns";
100325#endif
100326 sqlite3ErrorMsg(pParse, zFormat: "%s prohibited in %s", zMsg, zIn);
100327 if( pExpr ) pExpr->op = TK_NULL;
100328}
100329#define sqlite3ResolveNotValid(P,N,M,X,E) \
100330 assert( ((X)&~(NC_IsCheck|NC_PartIdx|NC_IdxExpr|NC_GenCol))==0 ); \
100331 if( ((N)->ncFlags & (X))!=0 ) notValidImpl(P,N,M,E);
100332
100333/*
100334** Expression p should encode a floating point value between 1.0 and 0.0.
100335** Return 1024 times this value. Or return -1 if p is not a floating point
100336** value between 1.0 and 0.0.
100337*/
100338static int exprProbability(Expr *p){
100339 double r = -1.0;
100340 if( p->op!=TK_FLOAT ) return -1;
100341 assert( !ExprHasProperty(p, EP_IntValue) );
100342 sqlite3AtoF(z: p->u.zToken, pResult: &r, length: sqlite3Strlen30(z: p->u.zToken), SQLITE_UTF8);
100343 assert( r>=0.0 );
100344 if( r>1.0 ) return -1;
100345 return (int)(r*134217728.0);
100346}
100347
100348/*
100349** This routine is callback for sqlite3WalkExpr().
100350**
100351** Resolve symbolic names into TK_COLUMN operators for the current
100352** node in the expression tree. Return 0 to continue the search down
100353** the tree or 2 to abort the tree walk.
100354**
100355** This routine also does error checking and name resolution for
100356** function names. The operator for aggregate functions is changed
100357** to TK_AGG_FUNCTION.
100358*/
100359static int resolveExprStep(Walker *pWalker, Expr *pExpr){
100360 NameContext *pNC;
100361 Parse *pParse;
100362
100363 pNC = pWalker->u.pNC;
100364 assert( pNC!=0 );
100365 pParse = pNC->pParse;
100366 assert( pParse==pWalker->pParse );
100367
100368#ifndef NDEBUG
100369 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
100370 SrcList *pSrcList = pNC->pSrcList;
100371 int i;
100372 for(i=0; i<pNC->pSrcList->nSrc; i++){
100373 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
100374 }
100375 }
100376#endif
100377 switch( pExpr->op ){
100378
100379 /* The special operator TK_ROW means use the rowid for the first
100380 ** column in the FROM clause. This is used by the LIMIT and ORDER BY
100381 ** clause processing on UPDATE and DELETE statements, and by
100382 ** UPDATE ... FROM statement processing.
100383 */
100384 case TK_ROW: {
100385 SrcList *pSrcList = pNC->pSrcList;
100386 SrcItem *pItem;
100387 assert( pSrcList && pSrcList->nSrc>=1 );
100388 pItem = pSrcList->a;
100389 pExpr->op = TK_COLUMN;
100390 assert( ExprUseYTab(pExpr) );
100391 pExpr->y.pTab = pItem->pTab;
100392 pExpr->iTable = pItem->iCursor;
100393 pExpr->iColumn--;
100394 pExpr->affExpr = SQLITE_AFF_INTEGER;
100395 break;
100396 }
100397
100398 /* An optimization: Attempt to convert
100399 **
100400 ** "expr IS NOT NULL" --> "TRUE"
100401 ** "expr IS NULL" --> "FALSE"
100402 **
100403 ** if we can prove that "expr" is never NULL. Call this the
100404 ** "NOT NULL strength reduction optimization".
100405 **
100406 ** If this optimization occurs, also restore the NameContext ref-counts
100407 ** to the state they where in before the "column" LHS expression was
100408 ** resolved. This prevents "column" from being counted as having been
100409 ** referenced, which might prevent a SELECT from being erroneously
100410 ** marked as correlated.
100411 */
100412 case TK_NOTNULL:
100413 case TK_ISNULL: {
100414 int anRef[8];
100415 NameContext *p;
100416 int i;
100417 for(i=0, p=pNC; p && i<ArraySize(anRef); p=p->pNext, i++){
100418 anRef[i] = p->nRef;
100419 }
100420 sqlite3WalkExpr(pWalker, pExpr: pExpr->pLeft);
100421 if( 0==sqlite3ExprCanBeNull(pExpr->pLeft) && !IN_RENAME_OBJECT ){
100422 testcase( ExprHasProperty(pExpr, EP_FromJoin) );
100423 assert( !ExprHasProperty(pExpr, EP_IntValue) );
100424 if( pExpr->op==TK_NOTNULL ){
100425 pExpr->u.zToken = "true";
100426 ExprSetProperty(pExpr, EP_IsTrue);
100427 }else{
100428 pExpr->u.zToken = "false";
100429 ExprSetProperty(pExpr, EP_IsFalse);
100430 }
100431 pExpr->op = TK_TRUEFALSE;
100432 for(i=0, p=pNC; p && i<ArraySize(anRef); p=p->pNext, i++){
100433 p->nRef = anRef[i];
100434 }
100435 sqlite3ExprDelete(pParse->db, pExpr->pLeft);
100436 pExpr->pLeft = 0;
100437 }
100438 return WRC_Prune;
100439 }
100440
100441 /* A column name: ID
100442 ** Or table name and column name: ID.ID
100443 ** Or a database, table and column: ID.ID.ID
100444 **
100445 ** The TK_ID and TK_OUT cases are combined so that there will only
100446 ** be one call to lookupName(). Then the compiler will in-line
100447 ** lookupName() for a size reduction and performance increase.
100448 */
100449 case TK_ID:
100450 case TK_DOT: {
100451 const char *zColumn;
100452 const char *zTable;
100453 const char *zDb;
100454 Expr *pRight;
100455
100456 if( pExpr->op==TK_ID ){
100457 zDb = 0;
100458 zTable = 0;
100459 assert( !ExprHasProperty(pExpr, EP_IntValue) );
100460 zColumn = pExpr->u.zToken;
100461 }else{
100462 Expr *pLeft = pExpr->pLeft;
100463 testcase( pNC->ncFlags & NC_IdxExpr );
100464 testcase( pNC->ncFlags & NC_GenCol );
100465 sqlite3ResolveNotValid(pParse, pNC, "the \".\" operator",
100466 NC_IdxExpr|NC_GenCol, 0);
100467 pRight = pExpr->pRight;
100468 if( pRight->op==TK_ID ){
100469 zDb = 0;
100470 }else{
100471 assert( pRight->op==TK_DOT );
100472 assert( !ExprHasProperty(pRight, EP_IntValue) );
100473 zDb = pLeft->u.zToken;
100474 pLeft = pRight->pLeft;
100475 pRight = pRight->pRight;
100476 }
100477 assert( ExprUseUToken(pLeft) && ExprUseUToken(pRight) );
100478 zTable = pLeft->u.zToken;
100479 zColumn = pRight->u.zToken;
100480 assert( ExprUseYTab(pExpr) );
100481 if( IN_RENAME_OBJECT ){
100482 sqlite3RenameTokenRemap(pParse, pTo: (void*)pExpr, pFrom: (void*)pRight);
100483 sqlite3RenameTokenRemap(pParse, pTo: (void*)&pExpr->y.pTab, pFrom: (void*)pLeft);
100484 }
100485 }
100486 return lookupName(pParse, zDb, zTab: zTable, zCol: zColumn, pNC, pExpr);
100487 }
100488
100489 /* Resolve function names
100490 */
100491 case TK_FUNCTION: {
100492 ExprList *pList = pExpr->x.pList; /* The argument list */
100493 int n = pList ? pList->nExpr : 0; /* Number of arguments */
100494 int no_such_func = 0; /* True if no such function exists */
100495 int wrong_num_args = 0; /* True if wrong number of arguments */
100496 int is_agg = 0; /* True if is an aggregate function */
100497 int nId; /* Number of characters in function name */
100498 const char *zId; /* The function name. */
100499 FuncDef *pDef; /* Information about the function */
100500 u8 enc = ENC(pParse->db); /* The database encoding */
100501 int savedAllowFlags = (pNC->ncFlags & (NC_AllowAgg | NC_AllowWin));
100502#ifndef SQLITE_OMIT_WINDOWFUNC
100503 Window *pWin = (IsWindowFunc(pExpr) ? pExpr->y.pWin : 0);
100504#endif
100505 assert( !ExprHasProperty(pExpr, EP_xIsSelect|EP_IntValue) );
100506 zId = pExpr->u.zToken;
100507 nId = sqlite3Strlen30(z: zId);
100508 pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
100509 if( pDef==0 ){
100510 pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);
100511 if( pDef==0 ){
100512 no_such_func = 1;
100513 }else{
100514 wrong_num_args = 1;
100515 }
100516 }else{
100517 is_agg = pDef->xFinalize!=0;
100518 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
100519 ExprSetProperty(pExpr, EP_Unlikely);
100520 if( n==2 ){
100521 pExpr->iTable = exprProbability(p: pList->a[1].pExpr);
100522 if( pExpr->iTable<0 ){
100523 sqlite3ErrorMsg(pParse,
100524 zFormat: "second argument to likelihood() must be a "
100525 "constant between 0.0 and 1.0");
100526 pNC->nNcErr++;
100527 }
100528 }else{
100529 /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is
100530 ** equivalent to likelihood(X, 0.0625).
100531 ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is
100532 ** short-hand for likelihood(X,0.0625).
100533 ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand
100534 ** for likelihood(X,0.9375).
100535 ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent
100536 ** to likelihood(X,0.9375). */
100537 /* TUNING: unlikely() probability is 0.0625. likely() is 0.9375 */
100538 pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
100539 }
100540 }
100541#ifndef SQLITE_OMIT_AUTHORIZATION
100542 {
100543 int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0,pDef->zName,0);
100544 if( auth!=SQLITE_OK ){
100545 if( auth==SQLITE_DENY ){
100546 sqlite3ErrorMsg(pParse, zFormat: "not authorized to use function: %s",
100547 pDef->zName);
100548 pNC->nNcErr++;
100549 }
100550 pExpr->op = TK_NULL;
100551 return WRC_Prune;
100552 }
100553 }
100554#endif
100555 if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG) ){
100556 /* For the purposes of the EP_ConstFunc flag, date and time
100557 ** functions and other functions that change slowly are considered
100558 ** constant because they are constant for the duration of one query.
100559 ** This allows them to be factored out of inner loops. */
100560 ExprSetProperty(pExpr,EP_ConstFunc);
100561 }
100562 if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT)==0 ){
100563 /* Clearly non-deterministic functions like random(), but also
100564 ** date/time functions that use 'now', and other functions like
100565 ** sqlite_version() that might change over time cannot be used
100566 ** in an index or generated column. Curiously, they can be used
100567 ** in a CHECK constraint. SQLServer, MySQL, and PostgreSQL all
100568 ** all this. */
100569 sqlite3ResolveNotValid(pParse, pNC, "non-deterministic functions",
100570 NC_IdxExpr|NC_PartIdx|NC_GenCol, 0);
100571 }else{
100572 assert( (NC_SelfRef & 0xff)==NC_SelfRef ); /* Must fit in 8 bits */
100573 pExpr->op2 = pNC->ncFlags & NC_SelfRef;
100574 if( pNC->ncFlags & NC_FromDDL ) ExprSetProperty(pExpr, EP_FromDDL);
100575 }
100576 if( (pDef->funcFlags & SQLITE_FUNC_INTERNAL)!=0
100577 && pParse->nested==0
100578 && (pParse->db->mDbFlags & DBFLAG_InternalFunc)==0
100579 ){
100580 /* Internal-use-only functions are disallowed unless the
100581 ** SQL is being compiled using sqlite3NestedParse() or
100582 ** the SQLITE_TESTCTRL_INTERNAL_FUNCTIONS test-control has be
100583 ** used to activate internal functionsn for testing purposes */
100584 no_such_func = 1;
100585 pDef = 0;
100586 }else
100587 if( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0
100588 && !IN_RENAME_OBJECT
100589 ){
100590 sqlite3ExprFunctionUsable(pParse, pExpr, pDef);
100591 }
100592 }
100593
100594 if( 0==IN_RENAME_OBJECT ){
100595#ifndef SQLITE_OMIT_WINDOWFUNC
100596 assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX)
100597 || (pDef->xValue==0 && pDef->xInverse==0)
100598 || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize)
100599 );
100600 if( pDef && pDef->xValue==0 && pWin ){
100601 sqlite3ErrorMsg(pParse,
100602 zFormat: "%.*s() may not be used as a window function", nId, zId
100603 );
100604 pNC->nNcErr++;
100605 }else if(
100606 (is_agg && (pNC->ncFlags & NC_AllowAgg)==0)
100607 || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW) && !pWin)
100608 || (is_agg && pWin && (pNC->ncFlags & NC_AllowWin)==0)
100609 ){
100610 const char *zType;
100611 if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pWin ){
100612 zType = "window";
100613 }else{
100614 zType = "aggregate";
100615 }
100616 sqlite3ErrorMsg(pParse, zFormat: "misuse of %s function %.*s()",zType,nId,zId);
100617 pNC->nNcErr++;
100618 is_agg = 0;
100619 }
100620#else
100621 if( (is_agg && (pNC->ncFlags & NC_AllowAgg)==0) ){
100622 sqlite3ErrorMsg(pParse,"misuse of aggregate function %.*s()",nId,zId);
100623 pNC->nNcErr++;
100624 is_agg = 0;
100625 }
100626#endif
100627 else if( no_such_func && pParse->db->init.busy==0
100628#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
100629 && pParse->explain==0
100630#endif
100631 ){
100632 sqlite3ErrorMsg(pParse, zFormat: "no such function: %.*s", nId, zId);
100633 pNC->nNcErr++;
100634 }else if( wrong_num_args ){
100635 sqlite3ErrorMsg(pParse,zFormat: "wrong number of arguments to function %.*s()",
100636 nId, zId);
100637 pNC->nNcErr++;
100638 }
100639#ifndef SQLITE_OMIT_WINDOWFUNC
100640 else if( is_agg==0 && ExprHasProperty(pExpr, EP_WinFunc) ){
100641 sqlite3ErrorMsg(pParse,
100642 zFormat: "FILTER may not be used with non-aggregate %.*s()",
100643 nId, zId
100644 );
100645 pNC->nNcErr++;
100646 }
100647#endif
100648 if( is_agg ){
100649 /* Window functions may not be arguments of aggregate functions.
100650 ** Or arguments of other window functions. But aggregate functions
100651 ** may be arguments for window functions. */
100652#ifndef SQLITE_OMIT_WINDOWFUNC
100653 pNC->ncFlags &= ~(NC_AllowWin | (!pWin ? NC_AllowAgg : 0));
100654#else
100655 pNC->ncFlags &= ~NC_AllowAgg;
100656#endif
100657 }
100658 }
100659#ifndef SQLITE_OMIT_WINDOWFUNC
100660 else if( ExprHasProperty(pExpr, EP_WinFunc) ){
100661 is_agg = 1;
100662 }
100663#endif
100664 sqlite3WalkExprList(pWalker, p: pList);
100665 if( is_agg ){
100666#ifndef SQLITE_OMIT_WINDOWFUNC
100667 if( pWin ){
100668 Select *pSel = pNC->pWinSelect;
100669 assert( pWin==0 || (ExprUseYWin(pExpr) && pWin==pExpr->y.pWin) );
100670 if( IN_RENAME_OBJECT==0 ){
100671 sqlite3WindowUpdate(pParse, pSel ? pSel->pWinDefn : 0, pWin, pDef);
100672 if( pParse->db->mallocFailed ) break;
100673 }
100674 sqlite3WalkExprList(pWalker, p: pWin->pPartition);
100675 sqlite3WalkExprList(pWalker, p: pWin->pOrderBy);
100676 sqlite3WalkExpr(pWalker, pExpr: pWin->pFilter);
100677 sqlite3WindowLink(pSel, pWin);
100678 pNC->ncFlags |= NC_HasWin;
100679 }else
100680#endif /* SQLITE_OMIT_WINDOWFUNC */
100681 {
100682 NameContext *pNC2; /* For looping up thru outer contexts */
100683 pExpr->op = TK_AGG_FUNCTION;
100684 pExpr->op2 = 0;
100685#ifndef SQLITE_OMIT_WINDOWFUNC
100686 if( ExprHasProperty(pExpr, EP_WinFunc) ){
100687 sqlite3WalkExpr(pWalker, pExpr: pExpr->y.pWin->pFilter);
100688 }
100689#endif
100690 pNC2 = pNC;
100691 while( pNC2
100692 && sqlite3ReferencesSrcList(pParse, pExpr, pNC2->pSrcList)==0
100693 ){
100694 pExpr->op2++;
100695 pNC2 = pNC2->pNext;
100696 }
100697 assert( pDef!=0 || IN_RENAME_OBJECT );
100698 if( pNC2 && pDef ){
100699 assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg );
100700 assert( SQLITE_FUNC_ANYORDER==NC_OrderAgg );
100701 testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
100702 testcase( (pDef->funcFlags & SQLITE_FUNC_ANYORDER)!=0 );
100703 pNC2->ncFlags |= NC_HasAgg
100704 | ((pDef->funcFlags^SQLITE_FUNC_ANYORDER)
100705 & (SQLITE_FUNC_MINMAX|SQLITE_FUNC_ANYORDER));
100706 }
100707 }
100708 pNC->ncFlags |= savedAllowFlags;
100709 }
100710 /* FIX ME: Compute pExpr->affinity based on the expected return
100711 ** type of the function
100712 */
100713 return WRC_Prune;
100714 }
100715#ifndef SQLITE_OMIT_SUBQUERY
100716 case TK_SELECT:
100717 case TK_EXISTS: testcase( pExpr->op==TK_EXISTS );
100718#endif
100719 case TK_IN: {
100720 testcase( pExpr->op==TK_IN );
100721 if( ExprUseXSelect(pExpr) ){
100722 int nRef = pNC->nRef;
100723 testcase( pNC->ncFlags & NC_IsCheck );
100724 testcase( pNC->ncFlags & NC_PartIdx );
100725 testcase( pNC->ncFlags & NC_IdxExpr );
100726 testcase( pNC->ncFlags & NC_GenCol );
100727 if( pNC->ncFlags & NC_SelfRef ){
100728 notValidImpl(pParse, pNC, zMsg: "subqueries", pExpr);
100729 }else{
100730 sqlite3WalkSelect(pWalker, p: pExpr->x.pSelect);
100731 }
100732 assert( pNC->nRef>=nRef );
100733 if( nRef!=pNC->nRef ){
100734 ExprSetProperty(pExpr, EP_VarSelect);
100735 pNC->ncFlags |= NC_VarSelect;
100736 }
100737 }
100738 break;
100739 }
100740 case TK_VARIABLE: {
100741 testcase( pNC->ncFlags & NC_IsCheck );
100742 testcase( pNC->ncFlags & NC_PartIdx );
100743 testcase( pNC->ncFlags & NC_IdxExpr );
100744 testcase( pNC->ncFlags & NC_GenCol );
100745 sqlite3ResolveNotValid(pParse, pNC, "parameters",
100746 NC_IsCheck|NC_PartIdx|NC_IdxExpr|NC_GenCol, pExpr);
100747 break;
100748 }
100749 case TK_IS:
100750 case TK_ISNOT: {
100751 Expr *pRight = sqlite3ExprSkipCollateAndLikely(pExpr->pRight);
100752 assert( !ExprHasProperty(pExpr, EP_Reduced) );
100753 /* Handle special cases of "x IS TRUE", "x IS FALSE", "x IS NOT TRUE",
100754 ** and "x IS NOT FALSE". */
100755 if( ALWAYS(pRight) && (pRight->op==TK_ID || pRight->op==TK_TRUEFALSE) ){
100756 int rc = resolveExprStep(pWalker, pExpr: pRight);
100757 if( rc==WRC_Abort ) return WRC_Abort;
100758 if( pRight->op==TK_TRUEFALSE ){
100759 pExpr->op2 = pExpr->op;
100760 pExpr->op = TK_TRUTH;
100761 return WRC_Continue;
100762 }
100763 }
100764 /* no break */ deliberate_fall_through
100765 }
100766 case TK_BETWEEN:
100767 case TK_EQ:
100768 case TK_NE:
100769 case TK_LT:
100770 case TK_LE:
100771 case TK_GT:
100772 case TK_GE: {
100773 int nLeft, nRight;
100774 if( pParse->db->mallocFailed ) break;
100775 assert( pExpr->pLeft!=0 );
100776 nLeft = sqlite3ExprVectorSize(pExpr: pExpr->pLeft);
100777 if( pExpr->op==TK_BETWEEN ){
100778 assert( ExprUseXList(pExpr) );
100779 nRight = sqlite3ExprVectorSize(pExpr: pExpr->x.pList->a[0].pExpr);
100780 if( nRight==nLeft ){
100781 nRight = sqlite3ExprVectorSize(pExpr: pExpr->x.pList->a[1].pExpr);
100782 }
100783 }else{
100784 assert( pExpr->pRight!=0 );
100785 nRight = sqlite3ExprVectorSize(pExpr: pExpr->pRight);
100786 }
100787 if( nLeft!=nRight ){
100788 testcase( pExpr->op==TK_EQ );
100789 testcase( pExpr->op==TK_NE );
100790 testcase( pExpr->op==TK_LT );
100791 testcase( pExpr->op==TK_LE );
100792 testcase( pExpr->op==TK_GT );
100793 testcase( pExpr->op==TK_GE );
100794 testcase( pExpr->op==TK_IS );
100795 testcase( pExpr->op==TK_ISNOT );
100796 testcase( pExpr->op==TK_BETWEEN );
100797 sqlite3ErrorMsg(pParse, zFormat: "row value misused");
100798 }
100799 break;
100800 }
100801 }
100802 return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
100803}
100804
100805/*
100806** pEList is a list of expressions which are really the result set of the
100807** a SELECT statement. pE is a term in an ORDER BY or GROUP BY clause.
100808** This routine checks to see if pE is a simple identifier which corresponds
100809** to the AS-name of one of the terms of the expression list. If it is,
100810** this routine return an integer between 1 and N where N is the number of
100811** elements in pEList, corresponding to the matching entry. If there is
100812** no match, or if pE is not a simple identifier, then this routine
100813** return 0.
100814**
100815** pEList has been resolved. pE has not.
100816*/
100817static int resolveAsName(
100818 Parse *pParse, /* Parsing context for error messages */
100819 ExprList *pEList, /* List of expressions to scan */
100820 Expr *pE /* Expression we are trying to match */
100821){
100822 int i; /* Loop counter */
100823
100824 UNUSED_PARAMETER(pParse);
100825
100826 if( pE->op==TK_ID ){
100827 const char *zCol;
100828 assert( !ExprHasProperty(pE, EP_IntValue) );
100829 zCol = pE->u.zToken;
100830 for(i=0; i<pEList->nExpr; i++){
100831 if( pEList->a[i].eEName==ENAME_NAME
100832 && sqlite3_stricmp(zLeft: pEList->a[i].zEName, zRight: zCol)==0
100833 ){
100834 return i+1;
100835 }
100836 }
100837 }
100838 return 0;
100839}
100840
100841/*
100842** pE is a pointer to an expression which is a single term in the
100843** ORDER BY of a compound SELECT. The expression has not been
100844** name resolved.
100845**
100846** At the point this routine is called, we already know that the
100847** ORDER BY term is not an integer index into the result set. That
100848** case is handled by the calling routine.
100849**
100850** Attempt to match pE against result set columns in the left-most
100851** SELECT statement. Return the index i of the matching column,
100852** as an indication to the caller that it should sort by the i-th column.
100853** The left-most column is 1. In other words, the value returned is the
100854** same integer value that would be used in the SQL statement to indicate
100855** the column.
100856**
100857** If there is no match, return 0. Return -1 if an error occurs.
100858*/
100859static int resolveOrderByTermToExprList(
100860 Parse *pParse, /* Parsing context for error messages */
100861 Select *pSelect, /* The SELECT statement with the ORDER BY clause */
100862 Expr *pE /* The specific ORDER BY term */
100863){
100864 int i; /* Loop counter */
100865 ExprList *pEList; /* The columns of the result set */
100866 NameContext nc; /* Name context for resolving pE */
100867 sqlite3 *db; /* Database connection */
100868 int rc; /* Return code from subprocedures */
100869 u8 savedSuppErr; /* Saved value of db->suppressErr */
100870
100871 assert( sqlite3ExprIsInteger(pE, &i)==0 );
100872 pEList = pSelect->pEList;
100873
100874 /* Resolve all names in the ORDER BY term expression
100875 */
100876 memset(s: &nc, c: 0, n: sizeof(nc));
100877 nc.pParse = pParse;
100878 nc.pSrcList = pSelect->pSrc;
100879 nc.uNC.pEList = pEList;
100880 nc.ncFlags = NC_AllowAgg|NC_UEList|NC_NoSelect;
100881 nc.nNcErr = 0;
100882 db = pParse->db;
100883 savedSuppErr = db->suppressErr;
100884 db->suppressErr = 1;
100885 rc = sqlite3ResolveExprNames(&nc, pE);
100886 db->suppressErr = savedSuppErr;
100887 if( rc ) return 0;
100888
100889 /* Try to match the ORDER BY expression against an expression
100890 ** in the result set. Return an 1-based index of the matching
100891 ** result-set entry.
100892 */
100893 for(i=0; i<pEList->nExpr; i++){
100894 if( sqlite3ExprCompare(0, pEList->a[i].pExpr, pE, -1)<2 ){
100895 return i+1;
100896 }
100897 }
100898
100899 /* If no match, return 0. */
100900 return 0;
100901}
100902
100903/*
100904** Generate an ORDER BY or GROUP BY term out-of-range error.
100905*/
100906static void resolveOutOfRangeError(
100907 Parse *pParse, /* The error context into which to write the error */
100908 const char *zType, /* "ORDER" or "GROUP" */
100909 int i, /* The index (1-based) of the term out of range */
100910 int mx /* Largest permissible value of i */
100911){
100912 sqlite3ErrorMsg(pParse,
100913 zFormat: "%r %s BY term out of range - should be "
100914 "between 1 and %d", i, zType, mx);
100915}
100916
100917/*
100918** Analyze the ORDER BY clause in a compound SELECT statement. Modify
100919** each term of the ORDER BY clause is a constant integer between 1
100920** and N where N is the number of columns in the compound SELECT.
100921**
100922** ORDER BY terms that are already an integer between 1 and N are
100923** unmodified. ORDER BY terms that are integers outside the range of
100924** 1 through N generate an error. ORDER BY terms that are expressions
100925** are matched against result set expressions of compound SELECT
100926** beginning with the left-most SELECT and working toward the right.
100927** At the first match, the ORDER BY expression is transformed into
100928** the integer column number.
100929**
100930** Return the number of errors seen.
100931*/
100932static int resolveCompoundOrderBy(
100933 Parse *pParse, /* Parsing context. Leave error messages here */
100934 Select *pSelect /* The SELECT statement containing the ORDER BY */
100935){
100936 int i;
100937 ExprList *pOrderBy;
100938 ExprList *pEList;
100939 sqlite3 *db;
100940 int moreToDo = 1;
100941
100942 pOrderBy = pSelect->pOrderBy;
100943 if( pOrderBy==0 ) return 0;
100944 db = pParse->db;
100945 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
100946 sqlite3ErrorMsg(pParse, zFormat: "too many terms in ORDER BY clause");
100947 return 1;
100948 }
100949 for(i=0; i<pOrderBy->nExpr; i++){
100950 pOrderBy->a[i].done = 0;
100951 }
100952 pSelect->pNext = 0;
100953 while( pSelect->pPrior ){
100954 pSelect->pPrior->pNext = pSelect;
100955 pSelect = pSelect->pPrior;
100956 }
100957 while( pSelect && moreToDo ){
100958 struct ExprList_item *pItem;
100959 moreToDo = 0;
100960 pEList = pSelect->pEList;
100961 assert( pEList!=0 );
100962 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
100963 int iCol = -1;
100964 Expr *pE, *pDup;
100965 if( pItem->done ) continue;
100966 pE = sqlite3ExprSkipCollateAndLikely(pItem->pExpr);
100967 if( NEVER(pE==0) ) continue;
100968 if( sqlite3ExprIsInteger(pE, &iCol) ){
100969 if( iCol<=0 || iCol>pEList->nExpr ){
100970 resolveOutOfRangeError(pParse, zType: "ORDER", i: i+1, mx: pEList->nExpr);
100971 return 1;
100972 }
100973 }else{
100974 iCol = resolveAsName(pParse, pEList, pE);
100975 if( iCol==0 ){
100976 /* Now test if expression pE matches one of the values returned
100977 ** by pSelect. In the usual case this is done by duplicating the
100978 ** expression, resolving any symbols in it, and then comparing
100979 ** it against each expression returned by the SELECT statement.
100980 ** Once the comparisons are finished, the duplicate expression
100981 ** is deleted.
100982 **
100983 ** If this is running as part of an ALTER TABLE operation and
100984 ** the symbols resolve successfully, also resolve the symbols in the
100985 ** actual expression. This allows the code in alter.c to modify
100986 ** column references within the ORDER BY expression as required. */
100987 pDup = sqlite3ExprDup(db, pE, 0);
100988 if( !db->mallocFailed ){
100989 assert(pDup);
100990 iCol = resolveOrderByTermToExprList(pParse, pSelect, pE: pDup);
100991 if( IN_RENAME_OBJECT && iCol>0 ){
100992 resolveOrderByTermToExprList(pParse, pSelect, pE);
100993 }
100994 }
100995 sqlite3ExprDelete(db, pDup);
100996 }
100997 }
100998 if( iCol>0 ){
100999 /* Convert the ORDER BY term into an integer column number iCol,
101000 ** taking care to preserve the COLLATE clause if it exists. */
101001 if( !IN_RENAME_OBJECT ){
101002 Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
101003 if( pNew==0 ) return 1;
101004 pNew->flags |= EP_IntValue;
101005 pNew->u.iValue = iCol;
101006 if( pItem->pExpr==pE ){
101007 pItem->pExpr = pNew;
101008 }else{
101009 Expr *pParent = pItem->pExpr;
101010 assert( pParent->op==TK_COLLATE );
101011 while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft;
101012 assert( pParent->pLeft==pE );
101013 pParent->pLeft = pNew;
101014 }
101015 sqlite3ExprDelete(db, pE);
101016 pItem->u.x.iOrderByCol = (u16)iCol;
101017 }
101018 pItem->done = 1;
101019 }else{
101020 moreToDo = 1;
101021 }
101022 }
101023 pSelect = pSelect->pNext;
101024 }
101025 for(i=0; i<pOrderBy->nExpr; i++){
101026 if( pOrderBy->a[i].done==0 ){
101027 sqlite3ErrorMsg(pParse, zFormat: "%r ORDER BY term does not match any "
101028 "column in the result set", i+1);
101029 return 1;
101030 }
101031 }
101032 return 0;
101033}
101034
101035/*
101036** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
101037** the SELECT statement pSelect. If any term is reference to a
101038** result set expression (as determined by the ExprList.a.u.x.iOrderByCol
101039** field) then convert that term into a copy of the corresponding result set
101040** column.
101041**
101042** If any errors are detected, add an error message to pParse and
101043** return non-zero. Return zero if no errors are seen.
101044*/
101045SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(
101046 Parse *pParse, /* Parsing context. Leave error messages here */
101047 Select *pSelect, /* The SELECT statement containing the clause */
101048 ExprList *pOrderBy, /* The ORDER BY or GROUP BY clause to be processed */
101049 const char *zType /* "ORDER" or "GROUP" */
101050){
101051 int i;
101052 sqlite3 *db = pParse->db;
101053 ExprList *pEList;
101054 struct ExprList_item *pItem;
101055
101056 if( pOrderBy==0 || pParse->db->mallocFailed || IN_RENAME_OBJECT ) return 0;
101057 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
101058 sqlite3ErrorMsg(pParse, zFormat: "too many terms in %s BY clause", zType);
101059 return 1;
101060 }
101061 pEList = pSelect->pEList;
101062 assert( pEList!=0 ); /* sqlite3SelectNew() guarantees this */
101063 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
101064 if( pItem->u.x.iOrderByCol ){
101065 if( pItem->u.x.iOrderByCol>pEList->nExpr ){
101066 resolveOutOfRangeError(pParse, zType, i: i+1, mx: pEList->nExpr);
101067 return 1;
101068 }
101069 resolveAlias(pParse, pEList, iCol: pItem->u.x.iOrderByCol-1, pExpr: pItem->pExpr,nSubquery: 0);
101070 }
101071 }
101072 return 0;
101073}
101074
101075#ifndef SQLITE_OMIT_WINDOWFUNC
101076/*
101077** Walker callback for windowRemoveExprFromSelect().
101078*/
101079static int resolveRemoveWindowsCb(Walker *pWalker, Expr *pExpr){
101080 UNUSED_PARAMETER(pWalker);
101081 if( ExprHasProperty(pExpr, EP_WinFunc) ){
101082 Window *pWin = pExpr->y.pWin;
101083 sqlite3WindowUnlinkFromSelect(pWin);
101084 }
101085 return WRC_Continue;
101086}
101087
101088/*
101089** Remove any Window objects owned by the expression pExpr from the
101090** Select.pWin list of Select object pSelect.
101091*/
101092static void windowRemoveExprFromSelect(Select *pSelect, Expr *pExpr){
101093 if( pSelect->pWin ){
101094 Walker sWalker;
101095 memset(s: &sWalker, c: 0, n: sizeof(Walker));
101096 sWalker.xExprCallback = resolveRemoveWindowsCb;
101097 sWalker.u.pSelect = pSelect;
101098 sqlite3WalkExpr(pWalker: &sWalker, pExpr);
101099 }
101100}
101101#else
101102# define windowRemoveExprFromSelect(a, b)
101103#endif /* SQLITE_OMIT_WINDOWFUNC */
101104
101105/*
101106** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
101107** The Name context of the SELECT statement is pNC. zType is either
101108** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
101109**
101110** This routine resolves each term of the clause into an expression.
101111** If the order-by term is an integer I between 1 and N (where N is the
101112** number of columns in the result set of the SELECT) then the expression
101113** in the resolution is a copy of the I-th result-set expression. If
101114** the order-by term is an identifier that corresponds to the AS-name of
101115** a result-set expression, then the term resolves to a copy of the
101116** result-set expression. Otherwise, the expression is resolved in
101117** the usual way - using sqlite3ResolveExprNames().
101118**
101119** This routine returns the number of errors. If errors occur, then
101120** an appropriate error message might be left in pParse. (OOM errors
101121** excepted.)
101122*/
101123static int resolveOrderGroupBy(
101124 NameContext *pNC, /* The name context of the SELECT statement */
101125 Select *pSelect, /* The SELECT statement holding pOrderBy */
101126 ExprList *pOrderBy, /* An ORDER BY or GROUP BY clause to resolve */
101127 const char *zType /* Either "ORDER" or "GROUP", as appropriate */
101128){
101129 int i, j; /* Loop counters */
101130 int iCol; /* Column number */
101131 struct ExprList_item *pItem; /* A term of the ORDER BY clause */
101132 Parse *pParse; /* Parsing context */
101133 int nResult; /* Number of terms in the result set */
101134
101135 assert( pOrderBy!=0 );
101136 nResult = pSelect->pEList->nExpr;
101137 pParse = pNC->pParse;
101138 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
101139 Expr *pE = pItem->pExpr;
101140 Expr *pE2 = sqlite3ExprSkipCollateAndLikely(pE);
101141 if( NEVER(pE2==0) ) continue;
101142 if( zType[0]!='G' ){
101143 iCol = resolveAsName(pParse, pEList: pSelect->pEList, pE: pE2);
101144 if( iCol>0 ){
101145 /* If an AS-name match is found, mark this ORDER BY column as being
101146 ** a copy of the iCol-th result-set column. The subsequent call to
101147 ** sqlite3ResolveOrderGroupBy() will convert the expression to a
101148 ** copy of the iCol-th result-set expression. */
101149 pItem->u.x.iOrderByCol = (u16)iCol;
101150 continue;
101151 }
101152 }
101153 if( sqlite3ExprIsInteger(pE2, &iCol) ){
101154 /* The ORDER BY term is an integer constant. Again, set the column
101155 ** number so that sqlite3ResolveOrderGroupBy() will convert the
101156 ** order-by term to a copy of the result-set expression */
101157 if( iCol<1 || iCol>0xffff ){
101158 resolveOutOfRangeError(pParse, zType, i: i+1, mx: nResult);
101159 return 1;
101160 }
101161 pItem->u.x.iOrderByCol = (u16)iCol;
101162 continue;
101163 }
101164
101165 /* Otherwise, treat the ORDER BY term as an ordinary expression */
101166 pItem->u.x.iOrderByCol = 0;
101167 if( sqlite3ResolveExprNames(pNC, pE) ){
101168 return 1;
101169 }
101170 for(j=0; j<pSelect->pEList->nExpr; j++){
101171 if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
101172 /* Since this expresion is being changed into a reference
101173 ** to an identical expression in the result set, remove all Window
101174 ** objects belonging to the expression from the Select.pWin list. */
101175 windowRemoveExprFromSelect(pSelect, pExpr: pE);
101176 pItem->u.x.iOrderByCol = j+1;
101177 }
101178 }
101179 }
101180 return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
101181}
101182
101183/*
101184** Resolve names in the SELECT statement p and all of its descendants.
101185*/
101186static int resolveSelectStep(Walker *pWalker, Select *p){
101187 NameContext *pOuterNC; /* Context that contains this SELECT */
101188 NameContext sNC; /* Name context of this SELECT */
101189 int isCompound; /* True if p is a compound select */
101190 int nCompound; /* Number of compound terms processed so far */
101191 Parse *pParse; /* Parsing context */
101192 int i; /* Loop counter */
101193 ExprList *pGroupBy; /* The GROUP BY clause */
101194 Select *pLeftmost; /* Left-most of SELECT of a compound */
101195 sqlite3 *db; /* Database connection */
101196
101197
101198 assert( p!=0 );
101199 if( p->selFlags & SF_Resolved ){
101200 return WRC_Prune;
101201 }
101202 pOuterNC = pWalker->u.pNC;
101203 pParse = pWalker->pParse;
101204 db = pParse->db;
101205
101206 /* Normally sqlite3SelectExpand() will be called first and will have
101207 ** already expanded this SELECT. However, if this is a subquery within
101208 ** an expression, sqlite3ResolveExprNames() will be called without a
101209 ** prior call to sqlite3SelectExpand(). When that happens, let
101210 ** sqlite3SelectPrep() do all of the processing for this SELECT.
101211 ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
101212 ** this routine in the correct order.
101213 */
101214 if( (p->selFlags & SF_Expanded)==0 ){
101215 sqlite3SelectPrep(pParse, p, pOuterNC);
101216 return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune;
101217 }
101218
101219 isCompound = p->pPrior!=0;
101220 nCompound = 0;
101221 pLeftmost = p;
101222 while( p ){
101223 assert( (p->selFlags & SF_Expanded)!=0 );
101224 assert( (p->selFlags & SF_Resolved)==0 );
101225 assert( db->suppressErr==0 ); /* SF_Resolved not set if errors suppressed */
101226 p->selFlags |= SF_Resolved;
101227
101228
101229 /* Resolve the expressions in the LIMIT and OFFSET clauses. These
101230 ** are not allowed to refer to any names, so pass an empty NameContext.
101231 */
101232 memset(s: &sNC, c: 0, n: sizeof(sNC));
101233 sNC.pParse = pParse;
101234 sNC.pWinSelect = p;
101235 if( sqlite3ResolveExprNames(&sNC, p->pLimit) ){
101236 return WRC_Abort;
101237 }
101238
101239 /* If the SF_Converted flags is set, then this Select object was
101240 ** was created by the convertCompoundSelectToSubquery() function.
101241 ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
101242 ** as if it were part of the sub-query, not the parent. This block
101243 ** moves the pOrderBy down to the sub-query. It will be moved back
101244 ** after the names have been resolved. */
101245 if( p->selFlags & SF_Converted ){
101246 Select *pSub = p->pSrc->a[0].pSelect;
101247 assert( p->pSrc->nSrc==1 && p->pOrderBy );
101248 assert( pSub->pPrior && pSub->pOrderBy==0 );
101249 pSub->pOrderBy = p->pOrderBy;
101250 p->pOrderBy = 0;
101251 }
101252
101253 /* Recursively resolve names in all subqueries in the FROM clause
101254 */
101255 for(i=0; i<p->pSrc->nSrc; i++){
101256 SrcItem *pItem = &p->pSrc->a[i];
101257 if( pItem->pSelect && (pItem->pSelect->selFlags & SF_Resolved)==0 ){
101258 int nRef = pOuterNC ? pOuterNC->nRef : 0;
101259 const char *zSavedContext = pParse->zAuthContext;
101260
101261 if( pItem->zName ) pParse->zAuthContext = pItem->zName;
101262 sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
101263 pParse->zAuthContext = zSavedContext;
101264 if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
101265
101266 /* If the number of references to the outer context changed when
101267 ** expressions in the sub-select were resolved, the sub-select
101268 ** is correlated. It is not required to check the refcount on any
101269 ** but the innermost outer context object, as lookupName() increments
101270 ** the refcount on all contexts between the current one and the
101271 ** context containing the column when it resolves a name. */
101272 if( pOuterNC ){
101273 assert( pItem->fg.isCorrelated==0 && pOuterNC->nRef>=nRef );
101274 pItem->fg.isCorrelated = (pOuterNC->nRef>nRef);
101275 }
101276 }
101277 }
101278
101279 /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
101280 ** resolve the result-set expression list.
101281 */
101282 sNC.ncFlags = NC_AllowAgg|NC_AllowWin;
101283 sNC.pSrcList = p->pSrc;
101284 sNC.pNext = pOuterNC;
101285
101286 /* Resolve names in the result set. */
101287 if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort;
101288 sNC.ncFlags &= ~NC_AllowWin;
101289
101290 /* If there are no aggregate functions in the result-set, and no GROUP BY
101291 ** expression, do not allow aggregates in any of the other expressions.
101292 */
101293 assert( (p->selFlags & SF_Aggregate)==0 );
101294 pGroupBy = p->pGroupBy;
101295 if( pGroupBy || (sNC.ncFlags & NC_HasAgg)!=0 ){
101296 assert( NC_MinMaxAgg==SF_MinMaxAgg );
101297 assert( NC_OrderAgg==SF_OrderByReqd );
101298 p->selFlags |= SF_Aggregate | (sNC.ncFlags&(NC_MinMaxAgg|NC_OrderAgg));
101299 }else{
101300 sNC.ncFlags &= ~NC_AllowAgg;
101301 }
101302
101303 /* Add the output column list to the name-context before parsing the
101304 ** other expressions in the SELECT statement. This is so that
101305 ** expressions in the WHERE clause (etc.) can refer to expressions by
101306 ** aliases in the result set.
101307 **
101308 ** Minor point: If this is the case, then the expression will be
101309 ** re-evaluated for each reference to it.
101310 */
101311 assert( (sNC.ncFlags & (NC_UAggInfo|NC_UUpsert|NC_UBaseReg))==0 );
101312 sNC.uNC.pEList = p->pEList;
101313 sNC.ncFlags |= NC_UEList;
101314 if( p->pHaving ){
101315 if( !pGroupBy ){
101316 sqlite3ErrorMsg(pParse, zFormat: "a GROUP BY clause is required before HAVING");
101317 return WRC_Abort;
101318 }
101319 if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
101320 }
101321 if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
101322
101323 /* Resolve names in table-valued-function arguments */
101324 for(i=0; i<p->pSrc->nSrc; i++){
101325 SrcItem *pItem = &p->pSrc->a[i];
101326 if( pItem->fg.isTabFunc
101327 && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg)
101328 ){
101329 return WRC_Abort;
101330 }
101331 }
101332
101333#ifndef SQLITE_OMIT_WINDOWFUNC
101334 if( IN_RENAME_OBJECT ){
101335 Window *pWin;
101336 for(pWin=p->pWinDefn; pWin; pWin=pWin->pNextWin){
101337 if( sqlite3ResolveExprListNames(&sNC, pWin->pOrderBy)
101338 || sqlite3ResolveExprListNames(&sNC, pWin->pPartition)
101339 ){
101340 return WRC_Abort;
101341 }
101342 }
101343 }
101344#endif
101345
101346 /* The ORDER BY and GROUP BY clauses may not refer to terms in
101347 ** outer queries
101348 */
101349 sNC.pNext = 0;
101350 sNC.ncFlags |= NC_AllowAgg|NC_AllowWin;
101351
101352 /* If this is a converted compound query, move the ORDER BY clause from
101353 ** the sub-query back to the parent query. At this point each term
101354 ** within the ORDER BY clause has been transformed to an integer value.
101355 ** These integers will be replaced by copies of the corresponding result
101356 ** set expressions by the call to resolveOrderGroupBy() below. */
101357 if( p->selFlags & SF_Converted ){
101358 Select *pSub = p->pSrc->a[0].pSelect;
101359 p->pOrderBy = pSub->pOrderBy;
101360 pSub->pOrderBy = 0;
101361 }
101362
101363 /* Process the ORDER BY clause for singleton SELECT statements.
101364 ** The ORDER BY clause for compounds SELECT statements is handled
101365 ** below, after all of the result-sets for all of the elements of
101366 ** the compound have been resolved.
101367 **
101368 ** If there is an ORDER BY clause on a term of a compound-select other
101369 ** than the right-most term, then that is a syntax error. But the error
101370 ** is not detected until much later, and so we need to go ahead and
101371 ** resolve those symbols on the incorrect ORDER BY for consistency.
101372 */
101373 if( p->pOrderBy!=0
101374 && isCompound<=nCompound /* Defer right-most ORDER BY of a compound */
101375 && resolveOrderGroupBy(pNC: &sNC, pSelect: p, pOrderBy: p->pOrderBy, zType: "ORDER")
101376 ){
101377 return WRC_Abort;
101378 }
101379 if( db->mallocFailed ){
101380 return WRC_Abort;
101381 }
101382 sNC.ncFlags &= ~NC_AllowWin;
101383
101384 /* Resolve the GROUP BY clause. At the same time, make sure
101385 ** the GROUP BY clause does not contain aggregate functions.
101386 */
101387 if( pGroupBy ){
101388 struct ExprList_item *pItem;
101389
101390 if( resolveOrderGroupBy(pNC: &sNC, pSelect: p, pOrderBy: pGroupBy, zType: "GROUP") || db->mallocFailed ){
101391 return WRC_Abort;
101392 }
101393 for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
101394 if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
101395 sqlite3ErrorMsg(pParse, zFormat: "aggregate functions are not allowed in "
101396 "the GROUP BY clause");
101397 return WRC_Abort;
101398 }
101399 }
101400 }
101401
101402 /* If this is part of a compound SELECT, check that it has the right
101403 ** number of expressions in the select list. */
101404 if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
101405 sqlite3SelectWrongNumTermsError(pParse, p: p->pNext);
101406 return WRC_Abort;
101407 }
101408
101409 /* Advance to the next term of the compound
101410 */
101411 p = p->pPrior;
101412 nCompound++;
101413 }
101414
101415 /* Resolve the ORDER BY on a compound SELECT after all terms of
101416 ** the compound have been resolved.
101417 */
101418 if( isCompound && resolveCompoundOrderBy(pParse, pSelect: pLeftmost) ){
101419 return WRC_Abort;
101420 }
101421
101422 return WRC_Prune;
101423}
101424
101425/*
101426** This routine walks an expression tree and resolves references to
101427** table columns and result-set columns. At the same time, do error
101428** checking on function usage and set a flag if any aggregate functions
101429** are seen.
101430**
101431** To resolve table columns references we look for nodes (or subtrees) of the
101432** form X.Y.Z or Y.Z or just Z where
101433**
101434** X: The name of a database. Ex: "main" or "temp" or
101435** the symbolic name assigned to an ATTACH-ed database.
101436**
101437** Y: The name of a table in a FROM clause. Or in a trigger
101438** one of the special names "old" or "new".
101439**
101440** Z: The name of a column in table Y.
101441**
101442** The node at the root of the subtree is modified as follows:
101443**
101444** Expr.op Changed to TK_COLUMN
101445** Expr.pTab Points to the Table object for X.Y
101446** Expr.iColumn The column index in X.Y. -1 for the rowid.
101447** Expr.iTable The VDBE cursor number for X.Y
101448**
101449**
101450** To resolve result-set references, look for expression nodes of the
101451** form Z (with no X and Y prefix) where the Z matches the right-hand
101452** size of an AS clause in the result-set of a SELECT. The Z expression
101453** is replaced by a copy of the left-hand side of the result-set expression.
101454** Table-name and function resolution occurs on the substituted expression
101455** tree. For example, in:
101456**
101457** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
101458**
101459** The "x" term of the order by is replaced by "a+b" to render:
101460**
101461** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
101462**
101463** Function calls are checked to make sure that the function is
101464** defined and that the correct number of arguments are specified.
101465** If the function is an aggregate function, then the NC_HasAgg flag is
101466** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
101467** If an expression contains aggregate functions then the EP_Agg
101468** property on the expression is set.
101469**
101470** An error message is left in pParse if anything is amiss. The number
101471** if errors is returned.
101472*/
101473SQLITE_PRIVATE int sqlite3ResolveExprNames(
101474 NameContext *pNC, /* Namespace to resolve expressions in. */
101475 Expr *pExpr /* The expression to be analyzed. */
101476){
101477 int savedHasAgg;
101478 Walker w;
101479
101480 if( pExpr==0 ) return SQLITE_OK;
101481 savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
101482 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
101483 w.pParse = pNC->pParse;
101484 w.xExprCallback = resolveExprStep;
101485 w.xSelectCallback = (pNC->ncFlags & NC_NoSelect) ? 0 : resolveSelectStep;
101486 w.xSelectCallback2 = 0;
101487 w.u.pNC = pNC;
101488#if SQLITE_MAX_EXPR_DEPTH>0
101489 w.pParse->nHeight += pExpr->nHeight;
101490 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
101491 return SQLITE_ERROR;
101492 }
101493#endif
101494 sqlite3WalkExpr(pWalker: &w, pExpr);
101495#if SQLITE_MAX_EXPR_DEPTH>0
101496 w.pParse->nHeight -= pExpr->nHeight;
101497#endif
101498 assert( EP_Agg==NC_HasAgg );
101499 assert( EP_Win==NC_HasWin );
101500 testcase( pNC->ncFlags & NC_HasAgg );
101501 testcase( pNC->ncFlags & NC_HasWin );
101502 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
101503 pNC->ncFlags |= savedHasAgg;
101504 return pNC->nNcErr>0 || w.pParse->nErr>0;
101505}
101506
101507/*
101508** Resolve all names for all expression in an expression list. This is
101509** just like sqlite3ResolveExprNames() except that it works for an expression
101510** list rather than a single expression.
101511*/
101512SQLITE_PRIVATE int sqlite3ResolveExprListNames(
101513 NameContext *pNC, /* Namespace to resolve expressions in. */
101514 ExprList *pList /* The expression list to be analyzed. */
101515){
101516 int i;
101517 int savedHasAgg = 0;
101518 Walker w;
101519 if( pList==0 ) return WRC_Continue;
101520 w.pParse = pNC->pParse;
101521 w.xExprCallback = resolveExprStep;
101522 w.xSelectCallback = resolveSelectStep;
101523 w.xSelectCallback2 = 0;
101524 w.u.pNC = pNC;
101525 savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
101526 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
101527 for(i=0; i<pList->nExpr; i++){
101528 Expr *pExpr = pList->a[i].pExpr;
101529 if( pExpr==0 ) continue;
101530#if SQLITE_MAX_EXPR_DEPTH>0
101531 w.pParse->nHeight += pExpr->nHeight;
101532 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
101533 return WRC_Abort;
101534 }
101535#endif
101536 sqlite3WalkExpr(pWalker: &w, pExpr);
101537#if SQLITE_MAX_EXPR_DEPTH>0
101538 w.pParse->nHeight -= pExpr->nHeight;
101539#endif
101540 assert( EP_Agg==NC_HasAgg );
101541 assert( EP_Win==NC_HasWin );
101542 testcase( pNC->ncFlags & NC_HasAgg );
101543 testcase( pNC->ncFlags & NC_HasWin );
101544 if( pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg) ){
101545 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
101546 savedHasAgg |= pNC->ncFlags &
101547 (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
101548 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
101549 }
101550 if( w.pParse->nErr>0 ) return WRC_Abort;
101551 }
101552 pNC->ncFlags |= savedHasAgg;
101553 return WRC_Continue;
101554}
101555
101556/*
101557** Resolve all names in all expressions of a SELECT and in all
101558** decendents of the SELECT, including compounds off of p->pPrior,
101559** subqueries in expressions, and subqueries used as FROM clause
101560** terms.
101561**
101562** See sqlite3ResolveExprNames() for a description of the kinds of
101563** transformations that occur.
101564**
101565** All SELECT statements should have been expanded using
101566** sqlite3SelectExpand() prior to invoking this routine.
101567*/
101568SQLITE_PRIVATE void sqlite3ResolveSelectNames(
101569 Parse *pParse, /* The parser context */
101570 Select *p, /* The SELECT statement being coded. */
101571 NameContext *pOuterNC /* Name context for parent SELECT statement */
101572){
101573 Walker w;
101574
101575 assert( p!=0 );
101576 w.xExprCallback = resolveExprStep;
101577 w.xSelectCallback = resolveSelectStep;
101578 w.xSelectCallback2 = 0;
101579 w.pParse = pParse;
101580 w.u.pNC = pOuterNC;
101581 sqlite3WalkSelect(pWalker: &w, p);
101582}
101583
101584/*
101585** Resolve names in expressions that can only reference a single table
101586** or which cannot reference any tables at all. Examples:
101587**
101588** "type" flag
101589** ------------
101590** (1) CHECK constraints NC_IsCheck
101591** (2) WHERE clauses on partial indices NC_PartIdx
101592** (3) Expressions in indexes on expressions NC_IdxExpr
101593** (4) Expression arguments to VACUUM INTO. 0
101594** (5) GENERATED ALWAYS as expressions NC_GenCol
101595**
101596** In all cases except (4), the Expr.iTable value for Expr.op==TK_COLUMN
101597** nodes of the expression is set to -1 and the Expr.iColumn value is
101598** set to the column number. In case (4), TK_COLUMN nodes cause an error.
101599**
101600** Any errors cause an error message to be set in pParse.
101601*/
101602SQLITE_PRIVATE int sqlite3ResolveSelfReference(
101603 Parse *pParse, /* Parsing context */
101604 Table *pTab, /* The table being referenced, or NULL */
101605 int type, /* NC_IsCheck, NC_PartIdx, NC_IdxExpr, NC_GenCol, or 0 */
101606 Expr *pExpr, /* Expression to resolve. May be NULL. */
101607 ExprList *pList /* Expression list to resolve. May be NULL. */
101608){
101609 SrcList sSrc; /* Fake SrcList for pParse->pNewTable */
101610 NameContext sNC; /* Name context for pParse->pNewTable */
101611 int rc;
101612
101613 assert( type==0 || pTab!=0 );
101614 assert( type==NC_IsCheck || type==NC_PartIdx || type==NC_IdxExpr
101615 || type==NC_GenCol || pTab==0 );
101616 memset(s: &sNC, c: 0, n: sizeof(sNC));
101617 memset(s: &sSrc, c: 0, n: sizeof(sSrc));
101618 if( pTab ){
101619 sSrc.nSrc = 1;
101620 sSrc.a[0].zName = pTab->zName;
101621 sSrc.a[0].pTab = pTab;
101622 sSrc.a[0].iCursor = -1;
101623 if( pTab->pSchema!=pParse->db->aDb[1].pSchema ){
101624 /* Cause EP_FromDDL to be set on TK_FUNCTION nodes of non-TEMP
101625 ** schema elements */
101626 type |= NC_FromDDL;
101627 }
101628 }
101629 sNC.pParse = pParse;
101630 sNC.pSrcList = &sSrc;
101631 sNC.ncFlags = type | NC_IsDDL;
101632 if( (rc = sqlite3ResolveExprNames(pNC: &sNC, pExpr))!=SQLITE_OK ) return rc;
101633 if( pList ) rc = sqlite3ResolveExprListNames(pNC: &sNC, pList);
101634 return rc;
101635}
101636
101637/************** End of resolve.c *********************************************/
101638/************** Begin file expr.c ********************************************/
101639/*
101640** 2001 September 15
101641**
101642** The author disclaims copyright to this source code. In place of
101643** a legal notice, here is a blessing:
101644**
101645** May you do good and not evil.
101646** May you find forgiveness for yourself and forgive others.
101647** May you share freely, never taking more than you give.
101648**
101649*************************************************************************
101650** This file contains routines used for analyzing expressions and
101651** for generating VDBE code that evaluates expressions in SQLite.
101652*/
101653/* #include "sqliteInt.h" */
101654
101655/* Forward declarations */
101656static void exprCodeBetween(Parse*,Expr*,int,void(*)(Parse*,Expr*,int,int),int);
101657static int exprCodeVector(Parse *pParse, Expr *p, int *piToFree);
101658
101659/*
101660** Return the affinity character for a single column of a table.
101661*/
101662SQLITE_PRIVATE char sqlite3TableColumnAffinity(const Table *pTab, int iCol){
101663 if( iCol<0 || NEVER(iCol>=pTab->nCol) ) return SQLITE_AFF_INTEGER;
101664 return pTab->aCol[iCol].affinity;
101665}
101666
101667/*
101668** Return the 'affinity' of the expression pExpr if any.
101669**
101670** If pExpr is a column, a reference to a column via an 'AS' alias,
101671** or a sub-select with a column as the return value, then the
101672** affinity of that column is returned. Otherwise, 0x00 is returned,
101673** indicating no affinity for the expression.
101674**
101675** i.e. the WHERE clause expressions in the following statements all
101676** have an affinity:
101677**
101678** CREATE TABLE t1(a);
101679** SELECT * FROM t1 WHERE a;
101680** SELECT a AS b FROM t1 WHERE b;
101681** SELECT * FROM t1 WHERE (select a from t1);
101682*/
101683SQLITE_PRIVATE char sqlite3ExprAffinity(const Expr *pExpr){
101684 int op;
101685 while( ExprHasProperty(pExpr, EP_Skip|EP_IfNullRow) ){
101686 assert( pExpr->op==TK_COLLATE
101687 || pExpr->op==TK_IF_NULL_ROW
101688 || (pExpr->op==TK_REGISTER && pExpr->op2==TK_IF_NULL_ROW) );
101689 pExpr = pExpr->pLeft;
101690 assert( pExpr!=0 );
101691 }
101692 op = pExpr->op;
101693 if( op==TK_REGISTER ) op = pExpr->op2;
101694 if( op==TK_COLUMN || op==TK_AGG_COLUMN ){
101695 assert( ExprUseYTab(pExpr) );
101696 if( pExpr->y.pTab ){
101697 return sqlite3TableColumnAffinity(pTab: pExpr->y.pTab, iCol: pExpr->iColumn);
101698 }
101699 }
101700 if( op==TK_SELECT ){
101701 assert( ExprUseXSelect(pExpr) );
101702 assert( pExpr->x.pSelect!=0 );
101703 assert( pExpr->x.pSelect->pEList!=0 );
101704 assert( pExpr->x.pSelect->pEList->a[0].pExpr!=0 );
101705 return sqlite3ExprAffinity(pExpr: pExpr->x.pSelect->pEList->a[0].pExpr);
101706 }
101707#ifndef SQLITE_OMIT_CAST
101708 if( op==TK_CAST ){
101709 assert( !ExprHasProperty(pExpr, EP_IntValue) );
101710 return sqlite3AffinityType(pExpr->u.zToken, 0);
101711 }
101712#endif
101713 if( op==TK_SELECT_COLUMN ){
101714 assert( pExpr->pLeft!=0 && ExprUseXSelect(pExpr->pLeft) );
101715 assert( pExpr->iColumn < pExpr->iTable );
101716 assert( pExpr->iTable==pExpr->pLeft->x.pSelect->pEList->nExpr );
101717 return sqlite3ExprAffinity(
101718 pExpr: pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
101719 );
101720 }
101721 if( op==TK_VECTOR ){
101722 assert( ExprUseXList(pExpr) );
101723 return sqlite3ExprAffinity(pExpr: pExpr->x.pList->a[0].pExpr);
101724 }
101725 return pExpr->affExpr;
101726}
101727
101728/*
101729** Set the collating sequence for expression pExpr to be the collating
101730** sequence named by pToken. Return a pointer to a new Expr node that
101731** implements the COLLATE operator.
101732**
101733** If a memory allocation error occurs, that fact is recorded in pParse->db
101734** and the pExpr parameter is returned unchanged.
101735*/
101736SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(
101737 const Parse *pParse, /* Parsing context */
101738 Expr *pExpr, /* Add the "COLLATE" clause to this expression */
101739 const Token *pCollName, /* Name of collating sequence */
101740 int dequote /* True to dequote pCollName */
101741){
101742 if( pCollName->n>0 ){
101743 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote);
101744 if( pNew ){
101745 pNew->pLeft = pExpr;
101746 pNew->flags |= EP_Collate|EP_Skip;
101747 pExpr = pNew;
101748 }
101749 }
101750 return pExpr;
101751}
101752SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(
101753 const Parse *pParse, /* Parsing context */
101754 Expr *pExpr, /* Add the "COLLATE" clause to this expression */
101755 const char *zC /* The collating sequence name */
101756){
101757 Token s;
101758 assert( zC!=0 );
101759 sqlite3TokenInit(p: &s, z: (char*)zC);
101760 return sqlite3ExprAddCollateToken(pParse, pExpr, pCollName: &s, dequote: 0);
101761}
101762
101763/*
101764** Skip over any TK_COLLATE operators.
101765*/
101766SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr *pExpr){
101767 while( pExpr && ExprHasProperty(pExpr, EP_Skip) ){
101768 assert( pExpr->op==TK_COLLATE );
101769 pExpr = pExpr->pLeft;
101770 }
101771 return pExpr;
101772}
101773
101774/*
101775** Skip over any TK_COLLATE operators and/or any unlikely()
101776** or likelihood() or likely() functions at the root of an
101777** expression.
101778*/
101779SQLITE_PRIVATE Expr *sqlite3ExprSkipCollateAndLikely(Expr *pExpr){
101780 while( pExpr && ExprHasProperty(pExpr, EP_Skip|EP_Unlikely) ){
101781 if( ExprHasProperty(pExpr, EP_Unlikely) ){
101782 assert( ExprUseXList(pExpr) );
101783 assert( pExpr->x.pList->nExpr>0 );
101784 assert( pExpr->op==TK_FUNCTION );
101785 pExpr = pExpr->x.pList->a[0].pExpr;
101786 }else{
101787 assert( pExpr->op==TK_COLLATE );
101788 pExpr = pExpr->pLeft;
101789 }
101790 }
101791 return pExpr;
101792}
101793
101794/*
101795** Return the collation sequence for the expression pExpr. If
101796** there is no defined collating sequence, return NULL.
101797**
101798** See also: sqlite3ExprNNCollSeq()
101799**
101800** The sqlite3ExprNNCollSeq() works the same exact that it returns the
101801** default collation if pExpr has no defined collation.
101802**
101803** The collating sequence might be determined by a COLLATE operator
101804** or by the presence of a column with a defined collating sequence.
101805** COLLATE operators take first precedence. Left operands take
101806** precedence over right operands.
101807*/
101808SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, const Expr *pExpr){
101809 sqlite3 *db = pParse->db;
101810 CollSeq *pColl = 0;
101811 const Expr *p = pExpr;
101812 while( p ){
101813 int op = p->op;
101814 if( op==TK_REGISTER ) op = p->op2;
101815 if( op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_TRIGGER ){
101816 assert( ExprUseYTab(p) );
101817 if( p->y.pTab!=0 ){
101818 /* op==TK_REGISTER && p->y.pTab!=0 happens when pExpr was originally
101819 ** a TK_COLUMN but was previously evaluated and cached in a register */
101820 int j = p->iColumn;
101821 if( j>=0 ){
101822 const char *zColl = sqlite3ColumnColl(&p->y.pTab->aCol[j]);
101823 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
101824 }
101825 break;
101826 }
101827 }
101828 if( op==TK_CAST || op==TK_UPLUS ){
101829 p = p->pLeft;
101830 continue;
101831 }
101832 if( op==TK_VECTOR ){
101833 assert( ExprUseXList(p) );
101834 p = p->x.pList->a[0].pExpr;
101835 continue;
101836 }
101837 if( op==TK_COLLATE ){
101838 assert( !ExprHasProperty(p, EP_IntValue) );
101839 pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
101840 break;
101841 }
101842 if( p->flags & EP_Collate ){
101843 if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){
101844 p = p->pLeft;
101845 }else{
101846 Expr *pNext = p->pRight;
101847 /* The Expr.x union is never used at the same time as Expr.pRight */
101848 assert( ExprUseXList(p) );
101849 assert( p->x.pList==0 || p->pRight==0 );
101850 if( p->x.pList!=0 && !db->mallocFailed ){
101851 int i;
101852 for(i=0; ALWAYS(i<p->x.pList->nExpr); i++){
101853 if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){
101854 pNext = p->x.pList->a[i].pExpr;
101855 break;
101856 }
101857 }
101858 }
101859 p = pNext;
101860 }
101861 }else{
101862 break;
101863 }
101864 }
101865 if( sqlite3CheckCollSeq(pParse, pColl) ){
101866 pColl = 0;
101867 }
101868 return pColl;
101869}
101870
101871/*
101872** Return the collation sequence for the expression pExpr. If
101873** there is no defined collating sequence, return a pointer to the
101874** defautl collation sequence.
101875**
101876** See also: sqlite3ExprCollSeq()
101877**
101878** The sqlite3ExprCollSeq() routine works the same except that it
101879** returns NULL if there is no defined collation.
101880*/
101881SQLITE_PRIVATE CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, const Expr *pExpr){
101882 CollSeq *p = sqlite3ExprCollSeq(pParse, pExpr);
101883 if( p==0 ) p = pParse->db->pDfltColl;
101884 assert( p!=0 );
101885 return p;
101886}
101887
101888/*
101889** Return TRUE if the two expressions have equivalent collating sequences.
101890*/
101891SQLITE_PRIVATE int sqlite3ExprCollSeqMatch(Parse *pParse, const Expr *pE1, const Expr *pE2){
101892 CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pExpr: pE1);
101893 CollSeq *pColl2 = sqlite3ExprNNCollSeq(pParse, pExpr: pE2);
101894 return sqlite3StrICmp(zLeft: pColl1->zName, zRight: pColl2->zName)==0;
101895}
101896
101897/*
101898** pExpr is an operand of a comparison operator. aff2 is the
101899** type affinity of the other operand. This routine returns the
101900** type affinity that should be used for the comparison operator.
101901*/
101902SQLITE_PRIVATE char sqlite3CompareAffinity(const Expr *pExpr, char aff2){
101903 char aff1 = sqlite3ExprAffinity(pExpr);
101904 if( aff1>SQLITE_AFF_NONE && aff2>SQLITE_AFF_NONE ){
101905 /* Both sides of the comparison are columns. If one has numeric
101906 ** affinity, use that. Otherwise use no affinity.
101907 */
101908 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
101909 return SQLITE_AFF_NUMERIC;
101910 }else{
101911 return SQLITE_AFF_BLOB;
101912 }
101913 }else{
101914 /* One side is a column, the other is not. Use the columns affinity. */
101915 assert( aff1<=SQLITE_AFF_NONE || aff2<=SQLITE_AFF_NONE );
101916 return (aff1<=SQLITE_AFF_NONE ? aff2 : aff1) | SQLITE_AFF_NONE;
101917 }
101918}
101919
101920/*
101921** pExpr is a comparison operator. Return the type affinity that should
101922** be applied to both operands prior to doing the comparison.
101923*/
101924static char comparisonAffinity(const Expr *pExpr){
101925 char aff;
101926 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
101927 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
101928 pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
101929 assert( pExpr->pLeft );
101930 aff = sqlite3ExprAffinity(pExpr: pExpr->pLeft);
101931 if( pExpr->pRight ){
101932 aff = sqlite3CompareAffinity(pExpr: pExpr->pRight, aff2: aff);
101933 }else if( ExprUseXSelect(pExpr) ){
101934 aff = sqlite3CompareAffinity(pExpr: pExpr->x.pSelect->pEList->a[0].pExpr, aff2: aff);
101935 }else if( aff==0 ){
101936 aff = SQLITE_AFF_BLOB;
101937 }
101938 return aff;
101939}
101940
101941/*
101942** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
101943** idx_affinity is the affinity of an indexed column. Return true
101944** if the index with affinity idx_affinity may be used to implement
101945** the comparison in pExpr.
101946*/
101947SQLITE_PRIVATE int sqlite3IndexAffinityOk(const Expr *pExpr, char idx_affinity){
101948 char aff = comparisonAffinity(pExpr);
101949 if( aff<SQLITE_AFF_TEXT ){
101950 return 1;
101951 }
101952 if( aff==SQLITE_AFF_TEXT ){
101953 return idx_affinity==SQLITE_AFF_TEXT;
101954 }
101955 return sqlite3IsNumericAffinity(idx_affinity);
101956}
101957
101958/*
101959** Return the P5 value that should be used for a binary comparison
101960** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
101961*/
101962static u8 binaryCompareP5(
101963 const Expr *pExpr1, /* Left operand */
101964 const Expr *pExpr2, /* Right operand */
101965 int jumpIfNull /* Extra flags added to P5 */
101966){
101967 u8 aff = (char)sqlite3ExprAffinity(pExpr: pExpr2);
101968 aff = (u8)sqlite3CompareAffinity(pExpr: pExpr1, aff2: aff) | (u8)jumpIfNull;
101969 return aff;
101970}
101971
101972/*
101973** Return a pointer to the collation sequence that should be used by
101974** a binary comparison operator comparing pLeft and pRight.
101975**
101976** If the left hand expression has a collating sequence type, then it is
101977** used. Otherwise the collation sequence for the right hand expression
101978** is used, or the default (BINARY) if neither expression has a collating
101979** type.
101980**
101981** Argument pRight (but not pLeft) may be a null pointer. In this case,
101982** it is not considered.
101983*/
101984SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(
101985 Parse *pParse,
101986 const Expr *pLeft,
101987 const Expr *pRight
101988){
101989 CollSeq *pColl;
101990 assert( pLeft );
101991 if( pLeft->flags & EP_Collate ){
101992 pColl = sqlite3ExprCollSeq(pParse, pExpr: pLeft);
101993 }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
101994 pColl = sqlite3ExprCollSeq(pParse, pExpr: pRight);
101995 }else{
101996 pColl = sqlite3ExprCollSeq(pParse, pExpr: pLeft);
101997 if( !pColl ){
101998 pColl = sqlite3ExprCollSeq(pParse, pExpr: pRight);
101999 }
102000 }
102001 return pColl;
102002}
102003
102004/* Expresssion p is a comparison operator. Return a collation sequence
102005** appropriate for the comparison operator.
102006**
102007** This is normally just a wrapper around sqlite3BinaryCompareCollSeq().
102008** However, if the OP_Commuted flag is set, then the order of the operands
102009** is reversed in the sqlite3BinaryCompareCollSeq() call so that the
102010** correct collating sequence is found.
102011*/
102012SQLITE_PRIVATE CollSeq *sqlite3ExprCompareCollSeq(Parse *pParse, const Expr *p){
102013 if( ExprHasProperty(p, EP_Commuted) ){
102014 return sqlite3BinaryCompareCollSeq(pParse, pLeft: p->pRight, pRight: p->pLeft);
102015 }else{
102016 return sqlite3BinaryCompareCollSeq(pParse, pLeft: p->pLeft, pRight: p->pRight);
102017 }
102018}
102019
102020/*
102021** Generate code for a comparison operator.
102022*/
102023static int codeCompare(
102024 Parse *pParse, /* The parsing (and code generating) context */
102025 Expr *pLeft, /* The left operand */
102026 Expr *pRight, /* The right operand */
102027 int opcode, /* The comparison opcode */
102028 int in1, int in2, /* Register holding operands */
102029 int dest, /* Jump here if true. */
102030 int jumpIfNull, /* If true, jump if either operand is NULL */
102031 int isCommuted /* The comparison has been commuted */
102032){
102033 int p5;
102034 int addr;
102035 CollSeq *p4;
102036
102037 if( pParse->nErr ) return 0;
102038 if( isCommuted ){
102039 p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft: pRight, pRight: pLeft);
102040 }else{
102041 p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
102042 }
102043 p5 = binaryCompareP5(pExpr1: pLeft, pExpr2: pRight, jumpIfNull);
102044 addr = sqlite3VdbeAddOp4(p: pParse->pVdbe, op: opcode, p1: in2, p2: dest, p3: in1,
102045 zP4: (void*)p4, P4_COLLSEQ);
102046 sqlite3VdbeChangeP5(p: pParse->pVdbe, p5: (u8)p5);
102047 return addr;
102048}
102049
102050/*
102051** Return true if expression pExpr is a vector, or false otherwise.
102052**
102053** A vector is defined as any expression that results in two or more
102054** columns of result. Every TK_VECTOR node is an vector because the
102055** parser will not generate a TK_VECTOR with fewer than two entries.
102056** But a TK_SELECT might be either a vector or a scalar. It is only
102057** considered a vector if it has two or more result columns.
102058*/
102059SQLITE_PRIVATE int sqlite3ExprIsVector(const Expr *pExpr){
102060 return sqlite3ExprVectorSize(pExpr)>1;
102061}
102062
102063/*
102064** If the expression passed as the only argument is of type TK_VECTOR
102065** return the number of expressions in the vector. Or, if the expression
102066** is a sub-select, return the number of columns in the sub-select. For
102067** any other type of expression, return 1.
102068*/
102069SQLITE_PRIVATE int sqlite3ExprVectorSize(const Expr *pExpr){
102070 u8 op = pExpr->op;
102071 if( op==TK_REGISTER ) op = pExpr->op2;
102072 if( op==TK_VECTOR ){
102073 assert( ExprUseXList(pExpr) );
102074 return pExpr->x.pList->nExpr;
102075 }else if( op==TK_SELECT ){
102076 assert( ExprUseXSelect(pExpr) );
102077 return pExpr->x.pSelect->pEList->nExpr;
102078 }else{
102079 return 1;
102080 }
102081}
102082
102083/*
102084** Return a pointer to a subexpression of pVector that is the i-th
102085** column of the vector (numbered starting with 0). The caller must
102086** ensure that i is within range.
102087**
102088** If pVector is really a scalar (and "scalar" here includes subqueries
102089** that return a single column!) then return pVector unmodified.
102090**
102091** pVector retains ownership of the returned subexpression.
102092**
102093** If the vector is a (SELECT ...) then the expression returned is
102094** just the expression for the i-th term of the result set, and may
102095** not be ready for evaluation because the table cursor has not yet
102096** been positioned.
102097*/
102098SQLITE_PRIVATE Expr *sqlite3VectorFieldSubexpr(Expr *pVector, int i){
102099 assert( i<sqlite3ExprVectorSize(pVector) || pVector->op==TK_ERROR );
102100 if( sqlite3ExprIsVector(pExpr: pVector) ){
102101 assert( pVector->op2==0 || pVector->op==TK_REGISTER );
102102 if( pVector->op==TK_SELECT || pVector->op2==TK_SELECT ){
102103 assert( ExprUseXSelect(pVector) );
102104 return pVector->x.pSelect->pEList->a[i].pExpr;
102105 }else{
102106 assert( ExprUseXList(pVector) );
102107 return pVector->x.pList->a[i].pExpr;
102108 }
102109 }
102110 return pVector;
102111}
102112
102113/*
102114** Compute and return a new Expr object which when passed to
102115** sqlite3ExprCode() will generate all necessary code to compute
102116** the iField-th column of the vector expression pVector.
102117**
102118** It is ok for pVector to be a scalar (as long as iField==0).
102119** In that case, this routine works like sqlite3ExprDup().
102120**
102121** The caller owns the returned Expr object and is responsible for
102122** ensuring that the returned value eventually gets freed.
102123**
102124** The caller retains ownership of pVector. If pVector is a TK_SELECT,
102125** then the returned object will reference pVector and so pVector must remain
102126** valid for the life of the returned object. If pVector is a TK_VECTOR
102127** or a scalar expression, then it can be deleted as soon as this routine
102128** returns.
102129**
102130** A trick to cause a TK_SELECT pVector to be deleted together with
102131** the returned Expr object is to attach the pVector to the pRight field
102132** of the returned TK_SELECT_COLUMN Expr object.
102133*/
102134SQLITE_PRIVATE Expr *sqlite3ExprForVectorField(
102135 Parse *pParse, /* Parsing context */
102136 Expr *pVector, /* The vector. List of expressions or a sub-SELECT */
102137 int iField, /* Which column of the vector to return */
102138 int nField /* Total number of columns in the vector */
102139){
102140 Expr *pRet;
102141 if( pVector->op==TK_SELECT ){
102142 assert( ExprUseXSelect(pVector) );
102143 /* The TK_SELECT_COLUMN Expr node:
102144 **
102145 ** pLeft: pVector containing TK_SELECT. Not deleted.
102146 ** pRight: not used. But recursively deleted.
102147 ** iColumn: Index of a column in pVector
102148 ** iTable: 0 or the number of columns on the LHS of an assignment
102149 ** pLeft->iTable: First in an array of register holding result, or 0
102150 ** if the result is not yet computed.
102151 **
102152 ** sqlite3ExprDelete() specifically skips the recursive delete of
102153 ** pLeft on TK_SELECT_COLUMN nodes. But pRight is followed, so pVector
102154 ** can be attached to pRight to cause this node to take ownership of
102155 ** pVector. Typically there will be multiple TK_SELECT_COLUMN nodes
102156 ** with the same pLeft pointer to the pVector, but only one of them
102157 ** will own the pVector.
102158 */
102159 pRet = sqlite3PExpr(pParse, TK_SELECT_COLUMN, 0, 0);
102160 if( pRet ){
102161 pRet->iTable = nField;
102162 pRet->iColumn = iField;
102163 pRet->pLeft = pVector;
102164 }
102165 }else{
102166 if( pVector->op==TK_VECTOR ){
102167 Expr **ppVector;
102168 assert( ExprUseXList(pVector) );
102169 ppVector = &pVector->x.pList->a[iField].pExpr;
102170 pVector = *ppVector;
102171 if( IN_RENAME_OBJECT ){
102172 /* This must be a vector UPDATE inside a trigger */
102173 *ppVector = 0;
102174 return pVector;
102175 }
102176 }
102177 pRet = sqlite3ExprDup(pParse->db, pVector, 0);
102178 }
102179 return pRet;
102180}
102181
102182/*
102183** If expression pExpr is of type TK_SELECT, generate code to evaluate
102184** it. Return the register in which the result is stored (or, if the
102185** sub-select returns more than one column, the first in an array
102186** of registers in which the result is stored).
102187**
102188** If pExpr is not a TK_SELECT expression, return 0.
102189*/
102190static int exprCodeSubselect(Parse *pParse, Expr *pExpr){
102191 int reg = 0;
102192#ifndef SQLITE_OMIT_SUBQUERY
102193 if( pExpr->op==TK_SELECT ){
102194 reg = sqlite3CodeSubselect(pParse, pExpr);
102195 }
102196#endif
102197 return reg;
102198}
102199
102200/*
102201** Argument pVector points to a vector expression - either a TK_VECTOR
102202** or TK_SELECT that returns more than one column. This function returns
102203** the register number of a register that contains the value of
102204** element iField of the vector.
102205**
102206** If pVector is a TK_SELECT expression, then code for it must have
102207** already been generated using the exprCodeSubselect() routine. In this
102208** case parameter regSelect should be the first in an array of registers
102209** containing the results of the sub-select.
102210**
102211** If pVector is of type TK_VECTOR, then code for the requested field
102212** is generated. In this case (*pRegFree) may be set to the number of
102213** a temporary register to be freed by the caller before returning.
102214**
102215** Before returning, output parameter (*ppExpr) is set to point to the
102216** Expr object corresponding to element iElem of the vector.
102217*/
102218static int exprVectorRegister(
102219 Parse *pParse, /* Parse context */
102220 Expr *pVector, /* Vector to extract element from */
102221 int iField, /* Field to extract from pVector */
102222 int regSelect, /* First in array of registers */
102223 Expr **ppExpr, /* OUT: Expression element */
102224 int *pRegFree /* OUT: Temp register to free */
102225){
102226 u8 op = pVector->op;
102227 assert( op==TK_VECTOR || op==TK_REGISTER || op==TK_SELECT || op==TK_ERROR );
102228 if( op==TK_REGISTER ){
102229 *ppExpr = sqlite3VectorFieldSubexpr(pVector, i: iField);
102230 return pVector->iTable+iField;
102231 }
102232 if( op==TK_SELECT ){
102233 assert( ExprUseXSelect(pVector) );
102234 *ppExpr = pVector->x.pSelect->pEList->a[iField].pExpr;
102235 return regSelect+iField;
102236 }
102237 if( op==TK_VECTOR ){
102238 assert( ExprUseXList(pVector) );
102239 *ppExpr = pVector->x.pList->a[iField].pExpr;
102240 return sqlite3ExprCodeTemp(pParse, *ppExpr, pRegFree);
102241 }
102242 return 0;
102243}
102244
102245/*
102246** Expression pExpr is a comparison between two vector values. Compute
102247** the result of the comparison (1, 0, or NULL) and write that
102248** result into register dest.
102249**
102250** The caller must satisfy the following preconditions:
102251**
102252** if pExpr->op==TK_IS: op==TK_EQ and p5==SQLITE_NULLEQ
102253** if pExpr->op==TK_ISNOT: op==TK_NE and p5==SQLITE_NULLEQ
102254** otherwise: op==pExpr->op and p5==0
102255*/
102256static void codeVectorCompare(
102257 Parse *pParse, /* Code generator context */
102258 Expr *pExpr, /* The comparison operation */
102259 int dest, /* Write results into this register */
102260 u8 op, /* Comparison operator */
102261 u8 p5 /* SQLITE_NULLEQ or zero */
102262){
102263 Vdbe *v = pParse->pVdbe;
102264 Expr *pLeft = pExpr->pLeft;
102265 Expr *pRight = pExpr->pRight;
102266 int nLeft = sqlite3ExprVectorSize(pExpr: pLeft);
102267 int i;
102268 int regLeft = 0;
102269 int regRight = 0;
102270 u8 opx = op;
102271 int addrCmp = 0;
102272 int addrDone = sqlite3VdbeMakeLabel(pParse);
102273 int isCommuted = ExprHasProperty(pExpr,EP_Commuted);
102274
102275 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) );
102276 if( pParse->nErr ) return;
102277 if( nLeft!=sqlite3ExprVectorSize(pExpr: pRight) ){
102278 sqlite3ErrorMsg(pParse, zFormat: "row value misused");
102279 return;
102280 }
102281 assert( pExpr->op==TK_EQ || pExpr->op==TK_NE
102282 || pExpr->op==TK_IS || pExpr->op==TK_ISNOT
102283 || pExpr->op==TK_LT || pExpr->op==TK_GT
102284 || pExpr->op==TK_LE || pExpr->op==TK_GE
102285 );
102286 assert( pExpr->op==op || (pExpr->op==TK_IS && op==TK_EQ)
102287 || (pExpr->op==TK_ISNOT && op==TK_NE) );
102288 assert( p5==0 || pExpr->op!=op );
102289 assert( p5==SQLITE_NULLEQ || pExpr->op==op );
102290
102291 if( op==TK_LE ) opx = TK_LT;
102292 if( op==TK_GE ) opx = TK_GT;
102293 if( op==TK_NE ) opx = TK_EQ;
102294
102295 regLeft = exprCodeSubselect(pParse, pExpr: pLeft);
102296 regRight = exprCodeSubselect(pParse, pExpr: pRight);
102297
102298 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 1, p2: dest);
102299 for(i=0; 1 /*Loop exits by "break"*/; i++){
102300 int regFree1 = 0, regFree2 = 0;
102301 Expr *pL = 0, *pR = 0;
102302 int r1, r2;
102303 assert( i>=0 && i<nLeft );
102304 if( addrCmp ) sqlite3VdbeJumpHere(p: v, addr: addrCmp);
102305 r1 = exprVectorRegister(pParse, pVector: pLeft, iField: i, regSelect: regLeft, ppExpr: &pL, pRegFree: &regFree1);
102306 r2 = exprVectorRegister(pParse, pVector: pRight, iField: i, regSelect: regRight, ppExpr: &pR, pRegFree: &regFree2);
102307 addrCmp = sqlite3VdbeCurrentAddr(p: v);
102308 codeCompare(pParse, pLeft: pL, pRight: pR, opcode: opx, in1: r1, in2: r2, dest: addrDone, jumpIfNull: p5, isCommuted);
102309 testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
102310 testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
102311 testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
102312 testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
102313 testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
102314 testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
102315 sqlite3ReleaseTempReg(pParse, regFree1);
102316 sqlite3ReleaseTempReg(pParse, regFree2);
102317 if( (opx==TK_LT || opx==TK_GT) && i<nLeft-1 ){
102318 addrCmp = sqlite3VdbeAddOp0(p: v, OP_ElseEq);
102319 testcase(opx==TK_LT); VdbeCoverageIf(v,opx==TK_LT);
102320 testcase(opx==TK_GT); VdbeCoverageIf(v,opx==TK_GT);
102321 }
102322 if( p5==SQLITE_NULLEQ ){
102323 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: dest);
102324 }else{
102325 sqlite3VdbeAddOp3(p: v, OP_ZeroOrNull, p1: r1, p2: dest, p3: r2);
102326 }
102327 if( i==nLeft-1 ){
102328 break;
102329 }
102330 if( opx==TK_EQ ){
102331 sqlite3VdbeAddOp2(p: v, OP_NotNull, p1: dest, p2: addrDone); VdbeCoverage(v);
102332 }else{
102333 assert( op==TK_LT || op==TK_GT || op==TK_LE || op==TK_GE );
102334 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: addrDone);
102335 if( i==nLeft-2 ) opx = op;
102336 }
102337 }
102338 sqlite3VdbeJumpHere(p: v, addr: addrCmp);
102339 sqlite3VdbeResolveLabel(v, x: addrDone);
102340 if( op==TK_NE ){
102341 sqlite3VdbeAddOp2(p: v, OP_Not, p1: dest, p2: dest);
102342 }
102343}
102344
102345#if SQLITE_MAX_EXPR_DEPTH>0
102346/*
102347** Check that argument nHeight is less than or equal to the maximum
102348** expression depth allowed. If it is not, leave an error message in
102349** pParse.
102350*/
102351SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){
102352 int rc = SQLITE_OK;
102353 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
102354 if( nHeight>mxHeight ){
102355 sqlite3ErrorMsg(pParse,
102356 zFormat: "Expression tree is too large (maximum depth %d)", mxHeight
102357 );
102358 rc = SQLITE_ERROR;
102359 }
102360 return rc;
102361}
102362
102363/* The following three functions, heightOfExpr(), heightOfExprList()
102364** and heightOfSelect(), are used to determine the maximum height
102365** of any expression tree referenced by the structure passed as the
102366** first argument.
102367**
102368** If this maximum height is greater than the current value pointed
102369** to by pnHeight, the second parameter, then set *pnHeight to that
102370** value.
102371*/
102372static void heightOfExpr(const Expr *p, int *pnHeight){
102373 if( p ){
102374 if( p->nHeight>*pnHeight ){
102375 *pnHeight = p->nHeight;
102376 }
102377 }
102378}
102379static void heightOfExprList(const ExprList *p, int *pnHeight){
102380 if( p ){
102381 int i;
102382 for(i=0; i<p->nExpr; i++){
102383 heightOfExpr(p: p->a[i].pExpr, pnHeight);
102384 }
102385 }
102386}
102387static void heightOfSelect(const Select *pSelect, int *pnHeight){
102388 const Select *p;
102389 for(p=pSelect; p; p=p->pPrior){
102390 heightOfExpr(p: p->pWhere, pnHeight);
102391 heightOfExpr(p: p->pHaving, pnHeight);
102392 heightOfExpr(p: p->pLimit, pnHeight);
102393 heightOfExprList(p: p->pEList, pnHeight);
102394 heightOfExprList(p: p->pGroupBy, pnHeight);
102395 heightOfExprList(p: p->pOrderBy, pnHeight);
102396 }
102397}
102398
102399/*
102400** Set the Expr.nHeight variable in the structure passed as an
102401** argument. An expression with no children, Expr.pList or
102402** Expr.pSelect member has a height of 1. Any other expression
102403** has a height equal to the maximum height of any other
102404** referenced Expr plus one.
102405**
102406** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags,
102407** if appropriate.
102408*/
102409static void exprSetHeight(Expr *p){
102410 int nHeight = 0;
102411 heightOfExpr(p: p->pLeft, pnHeight: &nHeight);
102412 heightOfExpr(p: p->pRight, pnHeight: &nHeight);
102413 if( ExprUseXSelect(p) ){
102414 heightOfSelect(pSelect: p->x.pSelect, pnHeight: &nHeight);
102415 }else if( p->x.pList ){
102416 heightOfExprList(p: p->x.pList, pnHeight: &nHeight);
102417 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
102418 }
102419 p->nHeight = nHeight + 1;
102420}
102421
102422/*
102423** Set the Expr.nHeight variable using the exprSetHeight() function. If
102424** the height is greater than the maximum allowed expression depth,
102425** leave an error in pParse.
102426**
102427** Also propagate all EP_Propagate flags from the Expr.x.pList into
102428** Expr.flags.
102429*/
102430SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
102431 if( pParse->nErr ) return;
102432 exprSetHeight(p);
102433 sqlite3ExprCheckHeight(pParse, nHeight: p->nHeight);
102434}
102435
102436/*
102437** Return the maximum height of any expression tree referenced
102438** by the select statement passed as an argument.
102439*/
102440SQLITE_PRIVATE int sqlite3SelectExprHeight(const Select *p){
102441 int nHeight = 0;
102442 heightOfSelect(pSelect: p, pnHeight: &nHeight);
102443 return nHeight;
102444}
102445#else /* ABOVE: Height enforcement enabled. BELOW: Height enforcement off */
102446/*
102447** Propagate all EP_Propagate flags from the Expr.x.pList into
102448** Expr.flags.
102449*/
102450SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
102451 if( pParse->nErr ) return;
102452 if( p && ExprUseXList(p) && p->x.pList ){
102453 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
102454 }
102455}
102456#define exprSetHeight(y)
102457#endif /* SQLITE_MAX_EXPR_DEPTH>0 */
102458
102459/*
102460** This routine is the core allocator for Expr nodes.
102461**
102462** Construct a new expression node and return a pointer to it. Memory
102463** for this node and for the pToken argument is a single allocation
102464** obtained from sqlite3DbMalloc(). The calling function
102465** is responsible for making sure the node eventually gets freed.
102466**
102467** If dequote is true, then the token (if it exists) is dequoted.
102468** If dequote is false, no dequoting is performed. The deQuote
102469** parameter is ignored if pToken is NULL or if the token does not
102470** appear to be quoted. If the quotes were of the form "..." (double-quotes)
102471** then the EP_DblQuoted flag is set on the expression node.
102472**
102473** Special case: If op==TK_INTEGER and pToken points to a string that
102474** can be translated into a 32-bit integer, then the token is not
102475** stored in u.zToken. Instead, the integer values is written
102476** into u.iValue and the EP_IntValue flag is set. No extra storage
102477** is allocated to hold the integer text and the dequote flag is ignored.
102478*/
102479SQLITE_PRIVATE Expr *sqlite3ExprAlloc(
102480 sqlite3 *db, /* Handle for sqlite3DbMallocRawNN() */
102481 int op, /* Expression opcode */
102482 const Token *pToken, /* Token argument. Might be NULL */
102483 int dequote /* True to dequote */
102484){
102485 Expr *pNew;
102486 int nExtra = 0;
102487 int iValue = 0;
102488
102489 assert( db!=0 );
102490 if( pToken ){
102491 if( op!=TK_INTEGER || pToken->z==0
102492 || sqlite3GetInt32(zNum: pToken->z, pValue: &iValue)==0 ){
102493 nExtra = pToken->n+1;
102494 assert( iValue>=0 );
102495 }
102496 }
102497 pNew = sqlite3DbMallocRawNN(db, n: sizeof(Expr)+nExtra);
102498 if( pNew ){
102499 memset(s: pNew, c: 0, n: sizeof(Expr));
102500 pNew->op = (u8)op;
102501 pNew->iAgg = -1;
102502 if( pToken ){
102503 if( nExtra==0 ){
102504 pNew->flags |= EP_IntValue|EP_Leaf|(iValue?EP_IsTrue:EP_IsFalse);
102505 pNew->u.iValue = iValue;
102506 }else{
102507 pNew->u.zToken = (char*)&pNew[1];
102508 assert( pToken->z!=0 || pToken->n==0 );
102509 if( pToken->n ) memcpy(dest: pNew->u.zToken, src: pToken->z, n: pToken->n);
102510 pNew->u.zToken[pToken->n] = 0;
102511 if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){
102512 sqlite3DequoteExpr(p: pNew);
102513 }
102514 }
102515 }
102516#if SQLITE_MAX_EXPR_DEPTH>0
102517 pNew->nHeight = 1;
102518#endif
102519 }
102520 return pNew;
102521}
102522
102523/*
102524** Allocate a new expression node from a zero-terminated token that has
102525** already been dequoted.
102526*/
102527SQLITE_PRIVATE Expr *sqlite3Expr(
102528 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
102529 int op, /* Expression opcode */
102530 const char *zToken /* Token argument. Might be NULL */
102531){
102532 Token x;
102533 x.z = zToken;
102534 x.n = sqlite3Strlen30(z: zToken);
102535 return sqlite3ExprAlloc(db, op, pToken: &x, dequote: 0);
102536}
102537
102538/*
102539** Attach subtrees pLeft and pRight to the Expr node pRoot.
102540**
102541** If pRoot==NULL that means that a memory allocation error has occurred.
102542** In that case, delete the subtrees pLeft and pRight.
102543*/
102544SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(
102545 sqlite3 *db,
102546 Expr *pRoot,
102547 Expr *pLeft,
102548 Expr *pRight
102549){
102550 if( pRoot==0 ){
102551 assert( db->mallocFailed );
102552 sqlite3ExprDelete(db, pLeft);
102553 sqlite3ExprDelete(db, pRight);
102554 }else{
102555 if( pRight ){
102556 pRoot->pRight = pRight;
102557 pRoot->flags |= EP_Propagate & pRight->flags;
102558 }
102559 if( pLeft ){
102560 pRoot->pLeft = pLeft;
102561 pRoot->flags |= EP_Propagate & pLeft->flags;
102562 }
102563 exprSetHeight(p: pRoot);
102564 }
102565}
102566
102567/*
102568** Allocate an Expr node which joins as many as two subtrees.
102569**
102570** One or both of the subtrees can be NULL. Return a pointer to the new
102571** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed,
102572** free the subtrees and return NULL.
102573*/
102574SQLITE_PRIVATE Expr *sqlite3PExpr(
102575 Parse *pParse, /* Parsing context */
102576 int op, /* Expression opcode */
102577 Expr *pLeft, /* Left operand */
102578 Expr *pRight /* Right operand */
102579){
102580 Expr *p;
102581 p = sqlite3DbMallocRawNN(db: pParse->db, n: sizeof(Expr));
102582 if( p ){
102583 memset(s: p, c: 0, n: sizeof(Expr));
102584 p->op = op & 0xff;
102585 p->iAgg = -1;
102586 sqlite3ExprAttachSubtrees(db: pParse->db, pRoot: p, pLeft, pRight);
102587 sqlite3ExprCheckHeight(pParse, nHeight: p->nHeight);
102588 }else{
102589 sqlite3ExprDelete(pParse->db, pLeft);
102590 sqlite3ExprDelete(pParse->db, pRight);
102591 }
102592 return p;
102593}
102594
102595/*
102596** Add pSelect to the Expr.x.pSelect field. Or, if pExpr is NULL (due
102597** do a memory allocation failure) then delete the pSelect object.
102598*/
102599SQLITE_PRIVATE void sqlite3PExprAddSelect(Parse *pParse, Expr *pExpr, Select *pSelect){
102600 if( pExpr ){
102601 pExpr->x.pSelect = pSelect;
102602 ExprSetProperty(pExpr, EP_xIsSelect|EP_Subquery);
102603 sqlite3ExprSetHeightAndFlags(pParse, p: pExpr);
102604 }else{
102605 assert( pParse->db->mallocFailed );
102606 sqlite3SelectDelete(pParse->db, pSelect);
102607 }
102608}
102609
102610/*
102611** Expression list pEList is a list of vector values. This function
102612** converts the contents of pEList to a VALUES(...) Select statement
102613** returning 1 row for each element of the list. For example, the
102614** expression list:
102615**
102616** ( (1,2), (3,4) (5,6) )
102617**
102618** is translated to the equivalent of:
102619**
102620** VALUES(1,2), (3,4), (5,6)
102621**
102622** Each of the vector values in pEList must contain exactly nElem terms.
102623** If a list element that is not a vector or does not contain nElem terms,
102624** an error message is left in pParse.
102625**
102626** This is used as part of processing IN(...) expressions with a list
102627** of vectors on the RHS. e.g. "... IN ((1,2), (3,4), (5,6))".
102628*/
102629SQLITE_PRIVATE Select *sqlite3ExprListToValues(Parse *pParse, int nElem, ExprList *pEList){
102630 int ii;
102631 Select *pRet = 0;
102632 assert( nElem>1 );
102633 for(ii=0; ii<pEList->nExpr; ii++){
102634 Select *pSel;
102635 Expr *pExpr = pEList->a[ii].pExpr;
102636 int nExprElem;
102637 if( pExpr->op==TK_VECTOR ){
102638 assert( ExprUseXList(pExpr) );
102639 nExprElem = pExpr->x.pList->nExpr;
102640 }else{
102641 nExprElem = 1;
102642 }
102643 if( nExprElem!=nElem ){
102644 sqlite3ErrorMsg(pParse, zFormat: "IN(...) element has %d term%s - expected %d",
102645 nExprElem, nExprElem>1?"s":"", nElem
102646 );
102647 break;
102648 }
102649 assert( ExprUseXList(pExpr) );
102650 pSel = sqlite3SelectNew(pParse, pExpr->x.pList, 0, 0, 0, 0, 0, SF_Values,0);
102651 pExpr->x.pList = 0;
102652 if( pSel ){
102653 if( pRet ){
102654 pSel->op = TK_ALL;
102655 pSel->pPrior = pRet;
102656 }
102657 pRet = pSel;
102658 }
102659 }
102660
102661 if( pRet && pRet->pPrior ){
102662 pRet->selFlags |= SF_MultiValue;
102663 }
102664 sqlite3ExprListDelete(pParse->db, pEList);
102665 return pRet;
102666}
102667
102668/*
102669** Join two expressions using an AND operator. If either expression is
102670** NULL, then just return the other expression.
102671**
102672** If one side or the other of the AND is known to be false, then instead
102673** of returning an AND expression, just return a constant expression with
102674** a value of false.
102675*/
102676SQLITE_PRIVATE Expr *sqlite3ExprAnd(Parse *pParse, Expr *pLeft, Expr *pRight){
102677 sqlite3 *db = pParse->db;
102678 if( pLeft==0 ){
102679 return pRight;
102680 }else if( pRight==0 ){
102681 return pLeft;
102682 }else if( (ExprAlwaysFalse(pLeft) || ExprAlwaysFalse(pRight))
102683 && !IN_RENAME_OBJECT
102684 ){
102685 sqlite3ExprDeferredDelete(pParse, pLeft);
102686 sqlite3ExprDeferredDelete(pParse, pRight);
102687 return sqlite3Expr(db, TK_INTEGER, zToken: "0");
102688 }else{
102689 return sqlite3PExpr(pParse, TK_AND, pLeft, pRight);
102690 }
102691}
102692
102693/*
102694** Construct a new expression node for a function with multiple
102695** arguments.
102696*/
102697SQLITE_PRIVATE Expr *sqlite3ExprFunction(
102698 Parse *pParse, /* Parsing context */
102699 ExprList *pList, /* Argument list */
102700 const Token *pToken, /* Name of the function */
102701 int eDistinct /* SF_Distinct or SF_ALL or 0 */
102702){
102703 Expr *pNew;
102704 sqlite3 *db = pParse->db;
102705 assert( pToken );
102706 pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, dequote: 1);
102707 if( pNew==0 ){
102708 sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
102709 return 0;
102710 }
102711 if( pList
102712 && pList->nExpr > pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG]
102713 && !pParse->nested
102714 ){
102715 sqlite3ErrorMsg(pParse, zFormat: "too many arguments on function %T", pToken);
102716 }
102717 pNew->x.pList = pList;
102718 ExprSetProperty(pNew, EP_HasFunc);
102719 assert( ExprUseXList(pNew) );
102720 sqlite3ExprSetHeightAndFlags(pParse, p: pNew);
102721 if( eDistinct==SF_Distinct ) ExprSetProperty(pNew, EP_Distinct);
102722 return pNew;
102723}
102724
102725/*
102726** Check to see if a function is usable according to current access
102727** rules:
102728**
102729** SQLITE_FUNC_DIRECT - Only usable from top-level SQL
102730**
102731** SQLITE_FUNC_UNSAFE - Usable if TRUSTED_SCHEMA or from
102732** top-level SQL
102733**
102734** If the function is not usable, create an error.
102735*/
102736SQLITE_PRIVATE void sqlite3ExprFunctionUsable(
102737 Parse *pParse, /* Parsing and code generating context */
102738 const Expr *pExpr, /* The function invocation */
102739 const FuncDef *pDef /* The function being invoked */
102740){
102741 assert( !IN_RENAME_OBJECT );
102742 assert( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0 );
102743 if( ExprHasProperty(pExpr, EP_FromDDL) ){
102744 if( (pDef->funcFlags & SQLITE_FUNC_DIRECT)!=0
102745 || (pParse->db->flags & SQLITE_TrustedSchema)==0
102746 ){
102747 /* Functions prohibited in triggers and views if:
102748 ** (1) tagged with SQLITE_DIRECTONLY
102749 ** (2) not tagged with SQLITE_INNOCUOUS (which means it
102750 ** is tagged with SQLITE_FUNC_UNSAFE) and
102751 ** SQLITE_DBCONFIG_TRUSTED_SCHEMA is off (meaning
102752 ** that the schema is possibly tainted).
102753 */
102754 sqlite3ErrorMsg(pParse, zFormat: "unsafe use of %s()", pDef->zName);
102755 }
102756 }
102757}
102758
102759/*
102760** Assign a variable number to an expression that encodes a wildcard
102761** in the original SQL statement.
102762**
102763** Wildcards consisting of a single "?" are assigned the next sequential
102764** variable number.
102765**
102766** Wildcards of the form "?nnn" are assigned the number "nnn". We make
102767** sure "nnn" is not too big to avoid a denial of service attack when
102768** the SQL statement comes from an external source.
102769**
102770** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
102771** as the previous instance of the same wildcard. Or if this is the first
102772** instance of the wildcard, the next sequential variable number is
102773** assigned.
102774*/
102775SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr, u32 n){
102776 sqlite3 *db = pParse->db;
102777 const char *z;
102778 ynVar x;
102779
102780 if( pExpr==0 ) return;
102781 assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) );
102782 z = pExpr->u.zToken;
102783 assert( z!=0 );
102784 assert( z[0]!=0 );
102785 assert( n==(u32)sqlite3Strlen30(z) );
102786 if( z[1]==0 ){
102787 /* Wildcard of the form "?". Assign the next variable number */
102788 assert( z[0]=='?' );
102789 x = (ynVar)(++pParse->nVar);
102790 }else{
102791 int doAdd = 0;
102792 if( z[0]=='?' ){
102793 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
102794 ** use it as the variable number */
102795 i64 i;
102796 int bOk;
102797 if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/
102798 i = z[1]-'0'; /* The common case of ?N for a single digit N */
102799 bOk = 1;
102800 }else{
102801 bOk = 0==sqlite3Atoi64(zNum: &z[1], pNum: &i, length: n-1, SQLITE_UTF8);
102802 }
102803 testcase( i==0 );
102804 testcase( i==1 );
102805 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
102806 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
102807 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
102808 sqlite3ErrorMsg(pParse, zFormat: "variable number must be between ?1 and ?%d",
102809 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
102810 return;
102811 }
102812 x = (ynVar)i;
102813 if( x>pParse->nVar ){
102814 pParse->nVar = (int)x;
102815 doAdd = 1;
102816 }else if( sqlite3VListNumToName(pIn: pParse->pVList, iVal: x)==0 ){
102817 doAdd = 1;
102818 }
102819 }else{
102820 /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable
102821 ** number as the prior appearance of the same name, or if the name
102822 ** has never appeared before, reuse the same variable number
102823 */
102824 x = (ynVar)sqlite3VListNameToNum(pIn: pParse->pVList, zName: z, nName: n);
102825 if( x==0 ){
102826 x = (ynVar)(++pParse->nVar);
102827 doAdd = 1;
102828 }
102829 }
102830 if( doAdd ){
102831 pParse->pVList = sqlite3VListAdd(db, pIn: pParse->pVList, zName: z, nName: n, iVal: x);
102832 }
102833 }
102834 pExpr->iColumn = x;
102835 if( x>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
102836 sqlite3ErrorMsg(pParse, zFormat: "too many SQL variables");
102837 }
102838}
102839
102840/*
102841** Recursively delete an expression tree.
102842*/
102843static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){
102844 assert( p!=0 );
102845 assert( !ExprUseUValue(p) || p->u.iValue>=0 );
102846 assert( !ExprUseYWin(p) || !ExprUseYSub(p) );
102847 assert( !ExprUseYWin(p) || p->y.pWin!=0 || db->mallocFailed );
102848 assert( p->op!=TK_FUNCTION || !ExprUseYSub(p) );
102849#ifdef SQLITE_DEBUG
102850 if( ExprHasProperty(p, EP_Leaf) && !ExprHasProperty(p, EP_TokenOnly) ){
102851 assert( p->pLeft==0 );
102852 assert( p->pRight==0 );
102853 assert( !ExprUseXSelect(p) || p->x.pSelect==0 );
102854 assert( !ExprUseXList(p) || p->x.pList==0 );
102855 }
102856#endif
102857 if( !ExprHasProperty(p, (EP_TokenOnly|EP_Leaf)) ){
102858 /* The Expr.x union is never used at the same time as Expr.pRight */
102859 assert( (ExprUseXList(p) && p->x.pList==0) || p->pRight==0 );
102860 if( p->pLeft && p->op!=TK_SELECT_COLUMN ) sqlite3ExprDeleteNN(db, p: p->pLeft);
102861 if( p->pRight ){
102862 assert( !ExprHasProperty(p, EP_WinFunc) );
102863 sqlite3ExprDeleteNN(db, p: p->pRight);
102864 }else if( ExprUseXSelect(p) ){
102865 assert( !ExprHasProperty(p, EP_WinFunc) );
102866 sqlite3SelectDelete(db, p->x.pSelect);
102867 }else{
102868 sqlite3ExprListDelete(db, p->x.pList);
102869#ifndef SQLITE_OMIT_WINDOWFUNC
102870 if( ExprHasProperty(p, EP_WinFunc) ){
102871 sqlite3WindowDelete(db, p->y.pWin);
102872 }
102873#endif
102874 }
102875 }
102876 if( ExprHasProperty(p, EP_MemToken) ){
102877 assert( !ExprHasProperty(p, EP_IntValue) );
102878 sqlite3DbFree(db, p: p->u.zToken);
102879 }
102880 if( !ExprHasProperty(p, EP_Static) ){
102881 sqlite3DbFreeNN(db, p);
102882 }
102883}
102884SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3 *db, Expr *p){
102885 if( p ) sqlite3ExprDeleteNN(db, p);
102886}
102887
102888
102889/*
102890** Arrange to cause pExpr to be deleted when the pParse is deleted.
102891** This is similar to sqlite3ExprDelete() except that the delete is
102892** deferred untilthe pParse is deleted.
102893**
102894** The pExpr might be deleted immediately on an OOM error.
102895**
102896** The deferred delete is (currently) implemented by adding the
102897** pExpr to the pParse->pConstExpr list with a register number of 0.
102898*/
102899SQLITE_PRIVATE void sqlite3ExprDeferredDelete(Parse *pParse, Expr *pExpr){
102900 pParse->pConstExpr =
102901 sqlite3ExprListAppend(pParse, pParse->pConstExpr, pExpr);
102902}
102903
102904/* Invoke sqlite3RenameExprUnmap() and sqlite3ExprDelete() on the
102905** expression.
102906*/
102907SQLITE_PRIVATE void sqlite3ExprUnmapAndDelete(Parse *pParse, Expr *p){
102908 if( p ){
102909 if( IN_RENAME_OBJECT ){
102910 sqlite3RenameExprUnmap(pParse, p);
102911 }
102912 sqlite3ExprDeleteNN(db: pParse->db, p);
102913 }
102914}
102915
102916/*
102917** Return the number of bytes allocated for the expression structure
102918** passed as the first argument. This is always one of EXPR_FULLSIZE,
102919** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
102920*/
102921static int exprStructSize(const Expr *p){
102922 if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE;
102923 if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE;
102924 return EXPR_FULLSIZE;
102925}
102926
102927/*
102928** The dupedExpr*Size() routines each return the number of bytes required
102929** to store a copy of an expression or expression tree. They differ in
102930** how much of the tree is measured.
102931**
102932** dupedExprStructSize() Size of only the Expr structure
102933** dupedExprNodeSize() Size of Expr + space for token
102934** dupedExprSize() Expr + token + subtree components
102935**
102936***************************************************************************
102937**
102938** The dupedExprStructSize() function returns two values OR-ed together:
102939** (1) the space required for a copy of the Expr structure only and
102940** (2) the EP_xxx flags that indicate what the structure size should be.
102941** The return values is always one of:
102942**
102943** EXPR_FULLSIZE
102944** EXPR_REDUCEDSIZE | EP_Reduced
102945** EXPR_TOKENONLYSIZE | EP_TokenOnly
102946**
102947** The size of the structure can be found by masking the return value
102948** of this routine with 0xfff. The flags can be found by masking the
102949** return value with EP_Reduced|EP_TokenOnly.
102950**
102951** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
102952** (unreduced) Expr objects as they or originally constructed by the parser.
102953** During expression analysis, extra information is computed and moved into
102954** later parts of the Expr object and that extra information might get chopped
102955** off if the expression is reduced. Note also that it does not work to
102956** make an EXPRDUP_REDUCE copy of a reduced expression. It is only legal
102957** to reduce a pristine expression tree from the parser. The implementation
102958** of dupedExprStructSize() contain multiple assert() statements that attempt
102959** to enforce this constraint.
102960*/
102961static int dupedExprStructSize(const Expr *p, int flags){
102962 int nSize;
102963 assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */
102964 assert( EXPR_FULLSIZE<=0xfff );
102965 assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 );
102966 if( 0==flags || p->op==TK_SELECT_COLUMN
102967#ifndef SQLITE_OMIT_WINDOWFUNC
102968 || ExprHasProperty(p, EP_WinFunc)
102969#endif
102970 ){
102971 nSize = EXPR_FULLSIZE;
102972 }else{
102973 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
102974 assert( !ExprHasProperty(p, EP_FromJoin) );
102975 assert( !ExprHasProperty(p, EP_MemToken) );
102976 assert( !ExprHasVVAProperty(p, EP_NoReduce) );
102977 if( p->pLeft || p->x.pList ){
102978 nSize = EXPR_REDUCEDSIZE | EP_Reduced;
102979 }else{
102980 assert( p->pRight==0 );
102981 nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
102982 }
102983 }
102984 return nSize;
102985}
102986
102987/*
102988** This function returns the space in bytes required to store the copy
102989** of the Expr structure and a copy of the Expr.u.zToken string (if that
102990** string is defined.)
102991*/
102992static int dupedExprNodeSize(const Expr *p, int flags){
102993 int nByte = dupedExprStructSize(p, flags) & 0xfff;
102994 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
102995 nByte += sqlite3Strlen30NN(p->u.zToken)+1;
102996 }
102997 return ROUND8(nByte);
102998}
102999
103000/*
103001** Return the number of bytes required to create a duplicate of the
103002** expression passed as the first argument. The second argument is a
103003** mask containing EXPRDUP_XXX flags.
103004**
103005** The value returned includes space to create a copy of the Expr struct
103006** itself and the buffer referred to by Expr.u.zToken, if any.
103007**
103008** If the EXPRDUP_REDUCE flag is set, then the return value includes
103009** space to duplicate all Expr nodes in the tree formed by Expr.pLeft
103010** and Expr.pRight variables (but not for any structures pointed to or
103011** descended from the Expr.x.pList or Expr.x.pSelect variables).
103012*/
103013static int dupedExprSize(const Expr *p, int flags){
103014 int nByte = 0;
103015 if( p ){
103016 nByte = dupedExprNodeSize(p, flags);
103017 if( flags&EXPRDUP_REDUCE ){
103018 nByte += dupedExprSize(p: p->pLeft, flags) + dupedExprSize(p: p->pRight, flags);
103019 }
103020 }
103021 return nByte;
103022}
103023
103024/*
103025** This function is similar to sqlite3ExprDup(), except that if pzBuffer
103026** is not NULL then *pzBuffer is assumed to point to a buffer large enough
103027** to store the copy of expression p, the copies of p->u.zToken
103028** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
103029** if any. Before returning, *pzBuffer is set to the first byte past the
103030** portion of the buffer copied into by this function.
103031*/
103032static Expr *exprDup(sqlite3 *db, const Expr *p, int dupFlags, u8 **pzBuffer){
103033 Expr *pNew; /* Value to return */
103034 u8 *zAlloc; /* Memory space from which to build Expr object */
103035 u32 staticFlag; /* EP_Static if space not obtained from malloc */
103036
103037 assert( db!=0 );
103038 assert( p );
103039 assert( dupFlags==0 || dupFlags==EXPRDUP_REDUCE );
103040 assert( pzBuffer==0 || dupFlags==EXPRDUP_REDUCE );
103041
103042 /* Figure out where to write the new Expr structure. */
103043 if( pzBuffer ){
103044 zAlloc = *pzBuffer;
103045 staticFlag = EP_Static;
103046 assert( zAlloc!=0 );
103047 }else{
103048 zAlloc = sqlite3DbMallocRawNN(db, n: dupedExprSize(p, flags: dupFlags));
103049 staticFlag = 0;
103050 }
103051 pNew = (Expr *)zAlloc;
103052
103053 if( pNew ){
103054 /* Set nNewSize to the size allocated for the structure pointed to
103055 ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
103056 ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
103057 ** by the copy of the p->u.zToken string (if any).
103058 */
103059 const unsigned nStructSize = dupedExprStructSize(p, flags: dupFlags);
103060 const int nNewSize = nStructSize & 0xfff;
103061 int nToken;
103062 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
103063 nToken = sqlite3Strlen30(z: p->u.zToken) + 1;
103064 }else{
103065 nToken = 0;
103066 }
103067 if( dupFlags ){
103068 assert( ExprHasProperty(p, EP_Reduced)==0 );
103069 memcpy(dest: zAlloc, src: p, n: nNewSize);
103070 }else{
103071 u32 nSize = (u32)exprStructSize(p);
103072 memcpy(dest: zAlloc, src: p, n: nSize);
103073 if( nSize<EXPR_FULLSIZE ){
103074 memset(s: &zAlloc[nSize], c: 0, EXPR_FULLSIZE-nSize);
103075 }
103076 }
103077
103078 /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
103079 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken);
103080 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
103081 pNew->flags |= staticFlag;
103082 ExprClearVVAProperties(pNew);
103083 if( dupFlags ){
103084 ExprSetVVAProperty(pNew, EP_Immutable);
103085 }
103086
103087 /* Copy the p->u.zToken string, if any. */
103088 if( nToken ){
103089 char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
103090 memcpy(dest: zToken, src: p->u.zToken, n: nToken);
103091 }
103092
103093 if( 0==((p->flags|pNew->flags) & (EP_TokenOnly|EP_Leaf)) ){
103094 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
103095 if( ExprUseXSelect(p) ){
103096 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
103097 }else{
103098 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags);
103099 }
103100 }
103101
103102 /* Fill in pNew->pLeft and pNew->pRight. */
103103 if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly|EP_WinFunc) ){
103104 zAlloc += dupedExprNodeSize(p, flags: dupFlags);
103105 if( !ExprHasProperty(pNew, EP_TokenOnly|EP_Leaf) ){
103106 pNew->pLeft = p->pLeft ?
103107 exprDup(db, p: p->pLeft, EXPRDUP_REDUCE, pzBuffer: &zAlloc) : 0;
103108 pNew->pRight = p->pRight ?
103109 exprDup(db, p: p->pRight, EXPRDUP_REDUCE, pzBuffer: &zAlloc) : 0;
103110 }
103111#ifndef SQLITE_OMIT_WINDOWFUNC
103112 if( ExprHasProperty(p, EP_WinFunc) ){
103113 pNew->y.pWin = sqlite3WindowDup(db, pOwner: pNew, p: p->y.pWin);
103114 assert( ExprHasProperty(pNew, EP_WinFunc) );
103115 }
103116#endif /* SQLITE_OMIT_WINDOWFUNC */
103117 if( pzBuffer ){
103118 *pzBuffer = zAlloc;
103119 }
103120 }else{
103121 if( !ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){
103122 if( pNew->op==TK_SELECT_COLUMN ){
103123 pNew->pLeft = p->pLeft;
103124 assert( p->pRight==0 || p->pRight==p->pLeft
103125 || ExprHasProperty(p->pLeft, EP_Subquery) );
103126 }else{
103127 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
103128 }
103129 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
103130 }
103131 }
103132 }
103133 return pNew;
103134}
103135
103136/*
103137** Create and return a deep copy of the object passed as the second
103138** argument. If an OOM condition is encountered, NULL is returned
103139** and the db->mallocFailed flag set.
103140*/
103141#ifndef SQLITE_OMIT_CTE
103142SQLITE_PRIVATE With *sqlite3WithDup(sqlite3 *db, With *p){
103143 With *pRet = 0;
103144 if( p ){
103145 sqlite3_int64 nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
103146 pRet = sqlite3DbMallocZero(db, n: nByte);
103147 if( pRet ){
103148 int i;
103149 pRet->nCte = p->nCte;
103150 for(i=0; i<p->nCte; i++){
103151 pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
103152 pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
103153 pRet->a[i].zName = sqlite3DbStrDup(db, z: p->a[i].zName);
103154 }
103155 }
103156 }
103157 return pRet;
103158}
103159#else
103160# define sqlite3WithDup(x,y) 0
103161#endif
103162
103163#ifndef SQLITE_OMIT_WINDOWFUNC
103164/*
103165** The gatherSelectWindows() procedure and its helper routine
103166** gatherSelectWindowsCallback() are used to scan all the expressions
103167** an a newly duplicated SELECT statement and gather all of the Window
103168** objects found there, assembling them onto the linked list at Select->pWin.
103169*/
103170static int gatherSelectWindowsCallback(Walker *pWalker, Expr *pExpr){
103171 if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_WinFunc) ){
103172 Select *pSelect = pWalker->u.pSelect;
103173 Window *pWin = pExpr->y.pWin;
103174 assert( pWin );
103175 assert( IsWindowFunc(pExpr) );
103176 assert( pWin->ppThis==0 );
103177 sqlite3WindowLink(pSel: pSelect, pWin);
103178 }
103179 return WRC_Continue;
103180}
103181static int gatherSelectWindowsSelectCallback(Walker *pWalker, Select *p){
103182 return p==pWalker->u.pSelect ? WRC_Continue : WRC_Prune;
103183}
103184static void gatherSelectWindows(Select *p){
103185 Walker w;
103186 w.xExprCallback = gatherSelectWindowsCallback;
103187 w.xSelectCallback = gatherSelectWindowsSelectCallback;
103188 w.xSelectCallback2 = 0;
103189 w.pParse = 0;
103190 w.u.pSelect = p;
103191 sqlite3WalkSelect(pWalker: &w, p);
103192}
103193#endif
103194
103195
103196/*
103197** The following group of routines make deep copies of expressions,
103198** expression lists, ID lists, and select statements. The copies can
103199** be deleted (by being passed to their respective ...Delete() routines)
103200** without effecting the originals.
103201**
103202** The expression list, ID, and source lists return by sqlite3ExprListDup(),
103203** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
103204** by subsequent calls to sqlite*ListAppend() routines.
103205**
103206** Any tables that the SrcList might point to are not duplicated.
103207**
103208** The flags parameter contains a combination of the EXPRDUP_XXX flags.
103209** If the EXPRDUP_REDUCE flag is set, then the structure returned is a
103210** truncated version of the usual Expr structure that will be stored as
103211** part of the in-memory representation of the database schema.
103212*/
103213SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3 *db, const Expr *p, int flags){
103214 assert( flags==0 || flags==EXPRDUP_REDUCE );
103215 return p ? exprDup(db, p, dupFlags: flags, pzBuffer: 0) : 0;
103216}
103217SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, const ExprList *p, int flags){
103218 ExprList *pNew;
103219 struct ExprList_item *pItem;
103220 const struct ExprList_item *pOldItem;
103221 int i;
103222 Expr *pPriorSelectColOld = 0;
103223 Expr *pPriorSelectColNew = 0;
103224 assert( db!=0 );
103225 if( p==0 ) return 0;
103226 pNew = sqlite3DbMallocRawNN(db, n: sqlite3DbMallocSize(db, p));
103227 if( pNew==0 ) return 0;
103228 pNew->nExpr = p->nExpr;
103229 pNew->nAlloc = p->nAlloc;
103230 pItem = pNew->a;
103231 pOldItem = p->a;
103232 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
103233 Expr *pOldExpr = pOldItem->pExpr;
103234 Expr *pNewExpr;
103235 pItem->pExpr = sqlite3ExprDup(db, p: pOldExpr, flags);
103236 if( pOldExpr
103237 && pOldExpr->op==TK_SELECT_COLUMN
103238 && (pNewExpr = pItem->pExpr)!=0
103239 ){
103240 if( pNewExpr->pRight ){
103241 pPriorSelectColOld = pOldExpr->pRight;
103242 pPriorSelectColNew = pNewExpr->pRight;
103243 pNewExpr->pLeft = pNewExpr->pRight;
103244 }else{
103245 if( pOldExpr->pLeft!=pPriorSelectColOld ){
103246 pPriorSelectColOld = pOldExpr->pLeft;
103247 pPriorSelectColNew = sqlite3ExprDup(db, p: pPriorSelectColOld, flags);
103248 pNewExpr->pRight = pPriorSelectColNew;
103249 }
103250 pNewExpr->pLeft = pPriorSelectColNew;
103251 }
103252 }
103253 pItem->zEName = sqlite3DbStrDup(db, z: pOldItem->zEName);
103254 pItem->sortFlags = pOldItem->sortFlags;
103255 pItem->eEName = pOldItem->eEName;
103256 pItem->done = 0;
103257 pItem->bNulls = pOldItem->bNulls;
103258 pItem->bSorterRef = pOldItem->bSorterRef;
103259 pItem->u = pOldItem->u;
103260 }
103261 return pNew;
103262}
103263
103264/*
103265** If cursors, triggers, views and subqueries are all omitted from
103266** the build, then none of the following routines, except for
103267** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
103268** called with a NULL argument.
103269*/
103270#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
103271 || !defined(SQLITE_OMIT_SUBQUERY)
103272SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3 *db, const SrcList *p, int flags){
103273 SrcList *pNew;
103274 int i;
103275 int nByte;
103276 assert( db!=0 );
103277 if( p==0 ) return 0;
103278 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
103279 pNew = sqlite3DbMallocRawNN(db, n: nByte );
103280 if( pNew==0 ) return 0;
103281 pNew->nSrc = pNew->nAlloc = p->nSrc;
103282 for(i=0; i<p->nSrc; i++){
103283 SrcItem *pNewItem = &pNew->a[i];
103284 const SrcItem *pOldItem = &p->a[i];
103285 Table *pTab;
103286 pNewItem->pSchema = pOldItem->pSchema;
103287 pNewItem->zDatabase = sqlite3DbStrDup(db, z: pOldItem->zDatabase);
103288 pNewItem->zName = sqlite3DbStrDup(db, z: pOldItem->zName);
103289 pNewItem->zAlias = sqlite3DbStrDup(db, z: pOldItem->zAlias);
103290 pNewItem->fg = pOldItem->fg;
103291 pNewItem->iCursor = pOldItem->iCursor;
103292 pNewItem->addrFillSub = pOldItem->addrFillSub;
103293 pNewItem->regReturn = pOldItem->regReturn;
103294 if( pNewItem->fg.isIndexedBy ){
103295 pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, z: pOldItem->u1.zIndexedBy);
103296 }
103297 pNewItem->u2 = pOldItem->u2;
103298 if( pNewItem->fg.isCte ){
103299 pNewItem->u2.pCteUse->nUse++;
103300 }
103301 if( pNewItem->fg.isTabFunc ){
103302 pNewItem->u1.pFuncArg =
103303 sqlite3ExprListDup(db, p: pOldItem->u1.pFuncArg, flags);
103304 }
103305 pTab = pNewItem->pTab = pOldItem->pTab;
103306 if( pTab ){
103307 pTab->nTabRef++;
103308 }
103309 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
103310 pNewItem->pOn = sqlite3ExprDup(db, p: pOldItem->pOn, flags);
103311 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
103312 pNewItem->colUsed = pOldItem->colUsed;
103313 }
103314 return pNew;
103315}
103316SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3 *db, const IdList *p){
103317 IdList *pNew;
103318 int i;
103319 assert( db!=0 );
103320 if( p==0 ) return 0;
103321 pNew = sqlite3DbMallocRawNN(db, n: sizeof(*pNew) );
103322 if( pNew==0 ) return 0;
103323 pNew->nId = p->nId;
103324 pNew->a = sqlite3DbMallocRawNN(db, n: p->nId*sizeof(p->a[0]) );
103325 if( pNew->a==0 ){
103326 sqlite3DbFreeNN(db, p: pNew);
103327 return 0;
103328 }
103329 /* Note that because the size of the allocation for p->a[] is not
103330 ** necessarily a power of two, sqlite3IdListAppend() may not be called
103331 ** on the duplicate created by this function. */
103332 for(i=0; i<p->nId; i++){
103333 struct IdList_item *pNewItem = &pNew->a[i];
103334 struct IdList_item *pOldItem = &p->a[i];
103335 pNewItem->zName = sqlite3DbStrDup(db, z: pOldItem->zName);
103336 pNewItem->idx = pOldItem->idx;
103337 }
103338 return pNew;
103339}
103340SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, const Select *pDup, int flags){
103341 Select *pRet = 0;
103342 Select *pNext = 0;
103343 Select **pp = &pRet;
103344 const Select *p;
103345
103346 assert( db!=0 );
103347 for(p=pDup; p; p=p->pPrior){
103348 Select *pNew = sqlite3DbMallocRawNN(db, n: sizeof(*p) );
103349 if( pNew==0 ) break;
103350 pNew->pEList = sqlite3ExprListDup(db, p: p->pEList, flags);
103351 pNew->pSrc = sqlite3SrcListDup(db, p: p->pSrc, flags);
103352 pNew->pWhere = sqlite3ExprDup(db, p: p->pWhere, flags);
103353 pNew->pGroupBy = sqlite3ExprListDup(db, p: p->pGroupBy, flags);
103354 pNew->pHaving = sqlite3ExprDup(db, p: p->pHaving, flags);
103355 pNew->pOrderBy = sqlite3ExprListDup(db, p: p->pOrderBy, flags);
103356 pNew->op = p->op;
103357 pNew->pNext = pNext;
103358 pNew->pPrior = 0;
103359 pNew->pLimit = sqlite3ExprDup(db, p: p->pLimit, flags);
103360 pNew->iLimit = 0;
103361 pNew->iOffset = 0;
103362 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
103363 pNew->addrOpenEphm[0] = -1;
103364 pNew->addrOpenEphm[1] = -1;
103365 pNew->nSelectRow = p->nSelectRow;
103366 pNew->pWith = sqlite3WithDup(db, p: p->pWith);
103367#ifndef SQLITE_OMIT_WINDOWFUNC
103368 pNew->pWin = 0;
103369 pNew->pWinDefn = sqlite3WindowListDup(db, p: p->pWinDefn);
103370 if( p->pWin && db->mallocFailed==0 ) gatherSelectWindows(p: pNew);
103371#endif
103372 pNew->selId = p->selId;
103373 if( db->mallocFailed ){
103374 /* Any prior OOM might have left the Select object incomplete.
103375 ** Delete the whole thing rather than allow an incomplete Select
103376 ** to be used by the code generator. */
103377 pNew->pNext = 0;
103378 sqlite3SelectDelete(db, pNew);
103379 break;
103380 }
103381 *pp = pNew;
103382 pp = &pNew->pPrior;
103383 pNext = pNew;
103384 }
103385
103386 return pRet;
103387}
103388#else
103389SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, const Select *p, int flags){
103390 assert( p==0 );
103391 return 0;
103392}
103393#endif
103394
103395
103396/*
103397** Add a new element to the end of an expression list. If pList is
103398** initially NULL, then create a new expression list.
103399**
103400** The pList argument must be either NULL or a pointer to an ExprList
103401** obtained from a prior call to sqlite3ExprListAppend(). This routine
103402** may not be used with an ExprList obtained from sqlite3ExprListDup().
103403** Reason: This routine assumes that the number of slots in pList->a[]
103404** is a power of two. That is true for sqlite3ExprListAppend() returns
103405** but is not necessarily true from the return value of sqlite3ExprListDup().
103406**
103407** If a memory allocation error occurs, the entire list is freed and
103408** NULL is returned. If non-NULL is returned, then it is guaranteed
103409** that the new entry was successfully appended.
103410*/
103411static const struct ExprList_item zeroItem = {0};
103412SQLITE_PRIVATE SQLITE_NOINLINE ExprList *sqlite3ExprListAppendNew(
103413 sqlite3 *db, /* Database handle. Used for memory allocation */
103414 Expr *pExpr /* Expression to be appended. Might be NULL */
103415){
103416 struct ExprList_item *pItem;
103417 ExprList *pList;
103418
103419 pList = sqlite3DbMallocRawNN(db, n: sizeof(ExprList)+sizeof(pList->a[0])*4 );
103420 if( pList==0 ){
103421 sqlite3ExprDelete(db, p: pExpr);
103422 return 0;
103423 }
103424 pList->nAlloc = 4;
103425 pList->nExpr = 1;
103426 pItem = &pList->a[0];
103427 *pItem = zeroItem;
103428 pItem->pExpr = pExpr;
103429 return pList;
103430}
103431SQLITE_PRIVATE SQLITE_NOINLINE ExprList *sqlite3ExprListAppendGrow(
103432 sqlite3 *db, /* Database handle. Used for memory allocation */
103433 ExprList *pList, /* List to which to append. Might be NULL */
103434 Expr *pExpr /* Expression to be appended. Might be NULL */
103435){
103436 struct ExprList_item *pItem;
103437 ExprList *pNew;
103438 pList->nAlloc *= 2;
103439 pNew = sqlite3DbRealloc(db, p: pList,
103440 n: sizeof(*pList)+(pList->nAlloc-1)*sizeof(pList->a[0]));
103441 if( pNew==0 ){
103442 sqlite3ExprListDelete(db, pList);
103443 sqlite3ExprDelete(db, p: pExpr);
103444 return 0;
103445 }else{
103446 pList = pNew;
103447 }
103448 pItem = &pList->a[pList->nExpr++];
103449 *pItem = zeroItem;
103450 pItem->pExpr = pExpr;
103451 return pList;
103452}
103453SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(
103454 Parse *pParse, /* Parsing context */
103455 ExprList *pList, /* List to which to append. Might be NULL */
103456 Expr *pExpr /* Expression to be appended. Might be NULL */
103457){
103458 struct ExprList_item *pItem;
103459 if( pList==0 ){
103460 return sqlite3ExprListAppendNew(db: pParse->db,pExpr);
103461 }
103462 if( pList->nAlloc<pList->nExpr+1 ){
103463 return sqlite3ExprListAppendGrow(db: pParse->db,pList,pExpr);
103464 }
103465 pItem = &pList->a[pList->nExpr++];
103466 *pItem = zeroItem;
103467 pItem->pExpr = pExpr;
103468 return pList;
103469}
103470
103471/*
103472** pColumns and pExpr form a vector assignment which is part of the SET
103473** clause of an UPDATE statement. Like this:
103474**
103475** (a,b,c) = (expr1,expr2,expr3)
103476** Or: (a,b,c) = (SELECT x,y,z FROM ....)
103477**
103478** For each term of the vector assignment, append new entries to the
103479** expression list pList. In the case of a subquery on the RHS, append
103480** TK_SELECT_COLUMN expressions.
103481*/
103482SQLITE_PRIVATE ExprList *sqlite3ExprListAppendVector(
103483 Parse *pParse, /* Parsing context */
103484 ExprList *pList, /* List to which to append. Might be NULL */
103485 IdList *pColumns, /* List of names of LHS of the assignment */
103486 Expr *pExpr /* Vector expression to be appended. Might be NULL */
103487){
103488 sqlite3 *db = pParse->db;
103489 int n;
103490 int i;
103491 int iFirst = pList ? pList->nExpr : 0;
103492 /* pColumns can only be NULL due to an OOM but an OOM will cause an
103493 ** exit prior to this routine being invoked */
103494 if( NEVER(pColumns==0) ) goto vector_append_error;
103495 if( pExpr==0 ) goto vector_append_error;
103496
103497 /* If the RHS is a vector, then we can immediately check to see that
103498 ** the size of the RHS and LHS match. But if the RHS is a SELECT,
103499 ** wildcards ("*") in the result set of the SELECT must be expanded before
103500 ** we can do the size check, so defer the size check until code generation.
103501 */
103502 if( pExpr->op!=TK_SELECT && pColumns->nId!=(n=sqlite3ExprVectorSize(pExpr)) ){
103503 sqlite3ErrorMsg(pParse, zFormat: "%d columns assigned %d values",
103504 pColumns->nId, n);
103505 goto vector_append_error;
103506 }
103507
103508 for(i=0; i<pColumns->nId; i++){
103509 Expr *pSubExpr = sqlite3ExprForVectorField(pParse, pVector: pExpr, iField: i, nField: pColumns->nId);
103510 assert( pSubExpr!=0 || db->mallocFailed );
103511 if( pSubExpr==0 ) continue;
103512 pList = sqlite3ExprListAppend(pParse, pList, pExpr: pSubExpr);
103513 if( pList ){
103514 assert( pList->nExpr==iFirst+i+1 );
103515 pList->a[pList->nExpr-1].zEName = pColumns->a[i].zName;
103516 pColumns->a[i].zName = 0;
103517 }
103518 }
103519
103520 if( !db->mallocFailed && pExpr->op==TK_SELECT && ALWAYS(pList!=0) ){
103521 Expr *pFirst = pList->a[iFirst].pExpr;
103522 assert( pFirst!=0 );
103523 assert( pFirst->op==TK_SELECT_COLUMN );
103524
103525 /* Store the SELECT statement in pRight so it will be deleted when
103526 ** sqlite3ExprListDelete() is called */
103527 pFirst->pRight = pExpr;
103528 pExpr = 0;
103529
103530 /* Remember the size of the LHS in iTable so that we can check that
103531 ** the RHS and LHS sizes match during code generation. */
103532 pFirst->iTable = pColumns->nId;
103533 }
103534
103535vector_append_error:
103536 sqlite3ExprUnmapAndDelete(pParse, p: pExpr);
103537 sqlite3IdListDelete(db, pColumns);
103538 return pList;
103539}
103540
103541/*
103542** Set the sort order for the last element on the given ExprList.
103543*/
103544SQLITE_PRIVATE void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder, int eNulls){
103545 struct ExprList_item *pItem;
103546 if( p==0 ) return;
103547 assert( p->nExpr>0 );
103548
103549 assert( SQLITE_SO_UNDEFINED<0 && SQLITE_SO_ASC==0 && SQLITE_SO_DESC>0 );
103550 assert( iSortOrder==SQLITE_SO_UNDEFINED
103551 || iSortOrder==SQLITE_SO_ASC
103552 || iSortOrder==SQLITE_SO_DESC
103553 );
103554 assert( eNulls==SQLITE_SO_UNDEFINED
103555 || eNulls==SQLITE_SO_ASC
103556 || eNulls==SQLITE_SO_DESC
103557 );
103558
103559 pItem = &p->a[p->nExpr-1];
103560 assert( pItem->bNulls==0 );
103561 if( iSortOrder==SQLITE_SO_UNDEFINED ){
103562 iSortOrder = SQLITE_SO_ASC;
103563 }
103564 pItem->sortFlags = (u8)iSortOrder;
103565
103566 if( eNulls!=SQLITE_SO_UNDEFINED ){
103567 pItem->bNulls = 1;
103568 if( iSortOrder!=eNulls ){
103569 pItem->sortFlags |= KEYINFO_ORDER_BIGNULL;
103570 }
103571 }
103572}
103573
103574/*
103575** Set the ExprList.a[].zEName element of the most recently added item
103576** on the expression list.
103577**
103578** pList might be NULL following an OOM error. But pName should never be
103579** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
103580** is set.
103581*/
103582SQLITE_PRIVATE void sqlite3ExprListSetName(
103583 Parse *pParse, /* Parsing context */
103584 ExprList *pList, /* List to which to add the span. */
103585 const Token *pName, /* Name to be added */
103586 int dequote /* True to cause the name to be dequoted */
103587){
103588 assert( pList!=0 || pParse->db->mallocFailed!=0 );
103589 assert( pParse->eParseMode!=PARSE_MODE_UNMAP || dequote==0 );
103590 if( pList ){
103591 struct ExprList_item *pItem;
103592 assert( pList->nExpr>0 );
103593 pItem = &pList->a[pList->nExpr-1];
103594 assert( pItem->zEName==0 );
103595 assert( pItem->eEName==ENAME_NAME );
103596 pItem->zEName = sqlite3DbStrNDup(db: pParse->db, z: pName->z, n: pName->n);
103597 if( dequote ){
103598 /* If dequote==0, then pName->z does not point to part of a DDL
103599 ** statement handled by the parser. And so no token need be added
103600 ** to the token-map. */
103601 sqlite3Dequote(z: pItem->zEName);
103602 if( IN_RENAME_OBJECT ){
103603 sqlite3RenameTokenMap(pParse, (const void*)pItem->zEName, pName);
103604 }
103605 }
103606 }
103607}
103608
103609/*
103610** Set the ExprList.a[].zSpan element of the most recently added item
103611** on the expression list.
103612**
103613** pList might be NULL following an OOM error. But pSpan should never be
103614** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
103615** is set.
103616*/
103617SQLITE_PRIVATE void sqlite3ExprListSetSpan(
103618 Parse *pParse, /* Parsing context */
103619 ExprList *pList, /* List to which to add the span. */
103620 const char *zStart, /* Start of the span */
103621 const char *zEnd /* End of the span */
103622){
103623 sqlite3 *db = pParse->db;
103624 assert( pList!=0 || db->mallocFailed!=0 );
103625 if( pList ){
103626 struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
103627 assert( pList->nExpr>0 );
103628 if( pItem->zEName==0 ){
103629 pItem->zEName = sqlite3DbSpanDup(db, zStart, zEnd);
103630 pItem->eEName = ENAME_SPAN;
103631 }
103632 }
103633}
103634
103635/*
103636** If the expression list pEList contains more than iLimit elements,
103637** leave an error message in pParse.
103638*/
103639SQLITE_PRIVATE void sqlite3ExprListCheckLength(
103640 Parse *pParse,
103641 ExprList *pEList,
103642 const char *zObject
103643){
103644 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
103645 testcase( pEList && pEList->nExpr==mx );
103646 testcase( pEList && pEList->nExpr==mx+1 );
103647 if( pEList && pEList->nExpr>mx ){
103648 sqlite3ErrorMsg(pParse, zFormat: "too many columns in %s", zObject);
103649 }
103650}
103651
103652/*
103653** Delete an entire expression list.
103654*/
103655static SQLITE_NOINLINE void exprListDeleteNN(sqlite3 *db, ExprList *pList){
103656 int i = pList->nExpr;
103657 struct ExprList_item *pItem = pList->a;
103658 assert( pList->nExpr>0 );
103659 do{
103660 sqlite3ExprDelete(db, p: pItem->pExpr);
103661 sqlite3DbFree(db, p: pItem->zEName);
103662 pItem++;
103663 }while( --i>0 );
103664 sqlite3DbFreeNN(db, p: pList);
103665}
103666SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
103667 if( pList ) exprListDeleteNN(db, pList);
103668}
103669
103670/*
103671** Return the bitwise-OR of all Expr.flags fields in the given
103672** ExprList.
103673*/
103674SQLITE_PRIVATE u32 sqlite3ExprListFlags(const ExprList *pList){
103675 int i;
103676 u32 m = 0;
103677 assert( pList!=0 );
103678 for(i=0; i<pList->nExpr; i++){
103679 Expr *pExpr = pList->a[i].pExpr;
103680 assert( pExpr!=0 );
103681 m |= pExpr->flags;
103682 }
103683 return m;
103684}
103685
103686/*
103687** This is a SELECT-node callback for the expression walker that
103688** always "fails". By "fail" in this case, we mean set
103689** pWalker->eCode to zero and abort.
103690**
103691** This callback is used by multiple expression walkers.
103692*/
103693SQLITE_PRIVATE int sqlite3SelectWalkFail(Walker *pWalker, Select *NotUsed){
103694 UNUSED_PARAMETER(NotUsed);
103695 pWalker->eCode = 0;
103696 return WRC_Abort;
103697}
103698
103699/*
103700** Check the input string to see if it is "true" or "false" (in any case).
103701**
103702** If the string is.... Return
103703** "true" EP_IsTrue
103704** "false" EP_IsFalse
103705** anything else 0
103706*/
103707SQLITE_PRIVATE u32 sqlite3IsTrueOrFalse(const char *zIn){
103708 if( sqlite3StrICmp(zLeft: zIn, zRight: "true")==0 ) return EP_IsTrue;
103709 if( sqlite3StrICmp(zLeft: zIn, zRight: "false")==0 ) return EP_IsFalse;
103710 return 0;
103711}
103712
103713
103714/*
103715** If the input expression is an ID with the name "true" or "false"
103716** then convert it into an TK_TRUEFALSE term. Return non-zero if
103717** the conversion happened, and zero if the expression is unaltered.
103718*/
103719SQLITE_PRIVATE int sqlite3ExprIdToTrueFalse(Expr *pExpr){
103720 u32 v;
103721 assert( pExpr->op==TK_ID || pExpr->op==TK_STRING );
103722 if( !ExprHasProperty(pExpr, EP_Quoted|EP_IntValue)
103723 && (v = sqlite3IsTrueOrFalse(zIn: pExpr->u.zToken))!=0
103724 ){
103725 pExpr->op = TK_TRUEFALSE;
103726 ExprSetProperty(pExpr, v);
103727 return 1;
103728 }
103729 return 0;
103730}
103731
103732/*
103733** The argument must be a TK_TRUEFALSE Expr node. Return 1 if it is TRUE
103734** and 0 if it is FALSE.
103735*/
103736SQLITE_PRIVATE int sqlite3ExprTruthValue(const Expr *pExpr){
103737 pExpr = sqlite3ExprSkipCollate(pExpr: (Expr*)pExpr);
103738 assert( pExpr->op==TK_TRUEFALSE );
103739 assert( !ExprHasProperty(pExpr, EP_IntValue) );
103740 assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0
103741 || sqlite3StrICmp(pExpr->u.zToken,"false")==0 );
103742 return pExpr->u.zToken[4]==0;
103743}
103744
103745/*
103746** If pExpr is an AND or OR expression, try to simplify it by eliminating
103747** terms that are always true or false. Return the simplified expression.
103748** Or return the original expression if no simplification is possible.
103749**
103750** Examples:
103751**
103752** (x<10) AND true => (x<10)
103753** (x<10) AND false => false
103754** (x<10) AND (y=22 OR false) => (x<10) AND (y=22)
103755** (x<10) AND (y=22 OR true) => (x<10)
103756** (y=22) OR true => true
103757*/
103758SQLITE_PRIVATE Expr *sqlite3ExprSimplifiedAndOr(Expr *pExpr){
103759 assert( pExpr!=0 );
103760 if( pExpr->op==TK_AND || pExpr->op==TK_OR ){
103761 Expr *pRight = sqlite3ExprSimplifiedAndOr(pExpr: pExpr->pRight);
103762 Expr *pLeft = sqlite3ExprSimplifiedAndOr(pExpr: pExpr->pLeft);
103763 if( ExprAlwaysTrue(pLeft) || ExprAlwaysFalse(pRight) ){
103764 pExpr = pExpr->op==TK_AND ? pRight : pLeft;
103765 }else if( ExprAlwaysTrue(pRight) || ExprAlwaysFalse(pLeft) ){
103766 pExpr = pExpr->op==TK_AND ? pLeft : pRight;
103767 }
103768 }
103769 return pExpr;
103770}
103771
103772
103773/*
103774** These routines are Walker callbacks used to check expressions to
103775** see if they are "constant" for some definition of constant. The
103776** Walker.eCode value determines the type of "constant" we are looking
103777** for.
103778**
103779** These callback routines are used to implement the following:
103780**
103781** sqlite3ExprIsConstant() pWalker->eCode==1
103782** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
103783** sqlite3ExprIsTableConstant() pWalker->eCode==3
103784** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
103785**
103786** In all cases, the callbacks set Walker.eCode=0 and abort if the expression
103787** is found to not be a constant.
103788**
103789** The sqlite3ExprIsConstantOrFunction() is used for evaluating DEFAULT
103790** expressions in a CREATE TABLE statement. The Walker.eCode value is 5
103791** when parsing an existing schema out of the sqlite_schema table and 4
103792** when processing a new CREATE TABLE statement. A bound parameter raises
103793** an error for new statements, but is silently converted
103794** to NULL for existing schemas. This allows sqlite_schema tables that
103795** contain a bound parameter because they were generated by older versions
103796** of SQLite to be parsed by newer versions of SQLite without raising a
103797** malformed schema error.
103798*/
103799static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
103800
103801 /* If pWalker->eCode is 2 then any term of the expression that comes from
103802 ** the ON or USING clauses of a left join disqualifies the expression
103803 ** from being considered constant. */
103804 if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin) ){
103805 pWalker->eCode = 0;
103806 return WRC_Abort;
103807 }
103808
103809 switch( pExpr->op ){
103810 /* Consider functions to be constant if all their arguments are constant
103811 ** and either pWalker->eCode==4 or 5 or the function has the
103812 ** SQLITE_FUNC_CONST flag. */
103813 case TK_FUNCTION:
103814 if( (pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc))
103815 && !ExprHasProperty(pExpr, EP_WinFunc)
103816 ){
103817 if( pWalker->eCode==5 ) ExprSetProperty(pExpr, EP_FromDDL);
103818 return WRC_Continue;
103819 }else{
103820 pWalker->eCode = 0;
103821 return WRC_Abort;
103822 }
103823 case TK_ID:
103824 /* Convert "true" or "false" in a DEFAULT clause into the
103825 ** appropriate TK_TRUEFALSE operator */
103826 if( sqlite3ExprIdToTrueFalse(pExpr) ){
103827 return WRC_Prune;
103828 }
103829 /* no break */ deliberate_fall_through
103830 case TK_COLUMN:
103831 case TK_AGG_FUNCTION:
103832 case TK_AGG_COLUMN:
103833 testcase( pExpr->op==TK_ID );
103834 testcase( pExpr->op==TK_COLUMN );
103835 testcase( pExpr->op==TK_AGG_FUNCTION );
103836 testcase( pExpr->op==TK_AGG_COLUMN );
103837 if( ExprHasProperty(pExpr, EP_FixedCol) && pWalker->eCode!=2 ){
103838 return WRC_Continue;
103839 }
103840 if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
103841 return WRC_Continue;
103842 }
103843 /* no break */ deliberate_fall_through
103844 case TK_IF_NULL_ROW:
103845 case TK_REGISTER:
103846 case TK_DOT:
103847 testcase( pExpr->op==TK_REGISTER );
103848 testcase( pExpr->op==TK_IF_NULL_ROW );
103849 testcase( pExpr->op==TK_DOT );
103850 pWalker->eCode = 0;
103851 return WRC_Abort;
103852 case TK_VARIABLE:
103853 if( pWalker->eCode==5 ){
103854 /* Silently convert bound parameters that appear inside of CREATE
103855 ** statements into a NULL when parsing the CREATE statement text out
103856 ** of the sqlite_schema table */
103857 pExpr->op = TK_NULL;
103858 }else if( pWalker->eCode==4 ){
103859 /* A bound parameter in a CREATE statement that originates from
103860 ** sqlite3_prepare() causes an error */
103861 pWalker->eCode = 0;
103862 return WRC_Abort;
103863 }
103864 /* no break */ deliberate_fall_through
103865 default:
103866 testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail() disallows */
103867 testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail() disallows */
103868 return WRC_Continue;
103869 }
103870}
103871static int exprIsConst(Expr *p, int initFlag, int iCur){
103872 Walker w;
103873 w.eCode = initFlag;
103874 w.xExprCallback = exprNodeIsConstant;
103875 w.xSelectCallback = sqlite3SelectWalkFail;
103876#ifdef SQLITE_DEBUG
103877 w.xSelectCallback2 = sqlite3SelectWalkAssert2;
103878#endif
103879 w.u.iCur = iCur;
103880 sqlite3WalkExpr(pWalker: &w, pExpr: p);
103881 return w.eCode;
103882}
103883
103884/*
103885** Walk an expression tree. Return non-zero if the expression is constant
103886** and 0 if it involves variables or function calls.
103887**
103888** For the purposes of this function, a double-quoted string (ex: "abc")
103889** is considered a variable but a single-quoted string (ex: 'abc') is
103890** a constant.
103891*/
103892SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){
103893 return exprIsConst(p, initFlag: 1, iCur: 0);
103894}
103895
103896/*
103897** Walk an expression tree. Return non-zero if
103898**
103899** (1) the expression is constant, and
103900** (2) the expression does originate in the ON or USING clause
103901** of a LEFT JOIN, and
103902** (3) the expression does not contain any EP_FixedCol TK_COLUMN
103903** operands created by the constant propagation optimization.
103904**
103905** When this routine returns true, it indicates that the expression
103906** can be added to the pParse->pConstExpr list and evaluated once when
103907** the prepared statement starts up. See sqlite3ExprCodeRunJustOnce().
103908*/
103909SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
103910 return exprIsConst(p, initFlag: 2, iCur: 0);
103911}
103912
103913/*
103914** Walk an expression tree. Return non-zero if the expression is constant
103915** for any single row of the table with cursor iCur. In other words, the
103916** expression must not refer to any non-deterministic function nor any
103917** table other than iCur.
103918*/
103919SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr *p, int iCur){
103920 return exprIsConst(p, initFlag: 3, iCur);
103921}
103922
103923
103924/*
103925** sqlite3WalkExpr() callback used by sqlite3ExprIsConstantOrGroupBy().
103926*/
103927static int exprNodeIsConstantOrGroupBy(Walker *pWalker, Expr *pExpr){
103928 ExprList *pGroupBy = pWalker->u.pGroupBy;
103929 int i;
103930
103931 /* Check if pExpr is identical to any GROUP BY term. If so, consider
103932 ** it constant. */
103933 for(i=0; i<pGroupBy->nExpr; i++){
103934 Expr *p = pGroupBy->a[i].pExpr;
103935 if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){
103936 CollSeq *pColl = sqlite3ExprNNCollSeq(pParse: pWalker->pParse, pExpr: p);
103937 if( sqlite3IsBinary(pColl) ){
103938 return WRC_Prune;
103939 }
103940 }
103941 }
103942
103943 /* Check if pExpr is a sub-select. If so, consider it variable. */
103944 if( ExprUseXSelect(pExpr) ){
103945 pWalker->eCode = 0;
103946 return WRC_Abort;
103947 }
103948
103949 return exprNodeIsConstant(pWalker, pExpr);
103950}
103951
103952/*
103953** Walk the expression tree passed as the first argument. Return non-zero
103954** if the expression consists entirely of constants or copies of terms
103955** in pGroupBy that sort with the BINARY collation sequence.
103956**
103957** This routine is used to determine if a term of the HAVING clause can
103958** be promoted into the WHERE clause. In order for such a promotion to work,
103959** the value of the HAVING clause term must be the same for all members of
103960** a "group". The requirement that the GROUP BY term must be BINARY
103961** assumes that no other collating sequence will have a finer-grained
103962** grouping than binary. In other words (A=B COLLATE binary) implies
103963** A=B in every other collating sequence. The requirement that the
103964** GROUP BY be BINARY is stricter than necessary. It would also work
103965** to promote HAVING clauses that use the same alternative collating
103966** sequence as the GROUP BY term, but that is much harder to check,
103967** alternative collating sequences are uncommon, and this is only an
103968** optimization, so we take the easy way out and simply require the
103969** GROUP BY to use the BINARY collating sequence.
103970*/
103971SQLITE_PRIVATE int sqlite3ExprIsConstantOrGroupBy(Parse *pParse, Expr *p, ExprList *pGroupBy){
103972 Walker w;
103973 w.eCode = 1;
103974 w.xExprCallback = exprNodeIsConstantOrGroupBy;
103975 w.xSelectCallback = 0;
103976 w.u.pGroupBy = pGroupBy;
103977 w.pParse = pParse;
103978 sqlite3WalkExpr(pWalker: &w, pExpr: p);
103979 return w.eCode;
103980}
103981
103982/*
103983** Walk an expression tree for the DEFAULT field of a column definition
103984** in a CREATE TABLE statement. Return non-zero if the expression is
103985** acceptable for use as a DEFAULT. That is to say, return non-zero if
103986** the expression is constant or a function call with constant arguments.
103987** Return and 0 if there are any variables.
103988**
103989** isInit is true when parsing from sqlite_schema. isInit is false when
103990** processing a new CREATE TABLE statement. When isInit is true, parameters
103991** (such as ? or $abc) in the expression are converted into NULL. When
103992** isInit is false, parameters raise an error. Parameters should not be
103993** allowed in a CREATE TABLE statement, but some legacy versions of SQLite
103994** allowed it, so we need to support it when reading sqlite_schema for
103995** backwards compatibility.
103996**
103997** If isInit is true, set EP_FromDDL on every TK_FUNCTION node.
103998**
103999** For the purposes of this function, a double-quoted string (ex: "abc")
104000** is considered a variable but a single-quoted string (ex: 'abc') is
104001** a constant.
104002*/
104003SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){
104004 assert( isInit==0 || isInit==1 );
104005 return exprIsConst(p, initFlag: 4+isInit, iCur: 0);
104006}
104007
104008#ifdef SQLITE_ENABLE_CURSOR_HINTS
104009/*
104010** Walk an expression tree. Return 1 if the expression contains a
104011** subquery of some kind. Return 0 if there are no subqueries.
104012*/
104013SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr *p){
104014 Walker w;
104015 w.eCode = 1;
104016 w.xExprCallback = sqlite3ExprWalkNoop;
104017 w.xSelectCallback = sqlite3SelectWalkFail;
104018#ifdef SQLITE_DEBUG
104019 w.xSelectCallback2 = sqlite3SelectWalkAssert2;
104020#endif
104021 sqlite3WalkExpr(&w, p);
104022 return w.eCode==0;
104023}
104024#endif
104025
104026/*
104027** If the expression p codes a constant integer that is small enough
104028** to fit in a 32-bit integer, return 1 and put the value of the integer
104029** in *pValue. If the expression is not an integer or if it is too big
104030** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
104031*/
104032SQLITE_PRIVATE int sqlite3ExprIsInteger(const Expr *p, int *pValue){
104033 int rc = 0;
104034 if( NEVER(p==0) ) return 0; /* Used to only happen following on OOM */
104035
104036 /* If an expression is an integer literal that fits in a signed 32-bit
104037 ** integer, then the EP_IntValue flag will have already been set */
104038 assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
104039 || sqlite3GetInt32(p->u.zToken, &rc)==0 );
104040
104041 if( p->flags & EP_IntValue ){
104042 *pValue = p->u.iValue;
104043 return 1;
104044 }
104045 switch( p->op ){
104046 case TK_UPLUS: {
104047 rc = sqlite3ExprIsInteger(p: p->pLeft, pValue);
104048 break;
104049 }
104050 case TK_UMINUS: {
104051 int v = 0;
104052 if( sqlite3ExprIsInteger(p: p->pLeft, pValue: &v) ){
104053 assert( ((unsigned int)v)!=0x80000000 );
104054 *pValue = -v;
104055 rc = 1;
104056 }
104057 break;
104058 }
104059 default: break;
104060 }
104061 return rc;
104062}
104063
104064/*
104065** Return FALSE if there is no chance that the expression can be NULL.
104066**
104067** If the expression might be NULL or if the expression is too complex
104068** to tell return TRUE.
104069**
104070** This routine is used as an optimization, to skip OP_IsNull opcodes
104071** when we know that a value cannot be NULL. Hence, a false positive
104072** (returning TRUE when in fact the expression can never be NULL) might
104073** be a small performance hit but is otherwise harmless. On the other
104074** hand, a false negative (returning FALSE when the result could be NULL)
104075** will likely result in an incorrect answer. So when in doubt, return
104076** TRUE.
104077*/
104078SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr *p){
104079 u8 op;
104080 assert( p!=0 );
104081 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){
104082 p = p->pLeft;
104083 assert( p!=0 );
104084 }
104085 op = p->op;
104086 if( op==TK_REGISTER ) op = p->op2;
104087 switch( op ){
104088 case TK_INTEGER:
104089 case TK_STRING:
104090 case TK_FLOAT:
104091 case TK_BLOB:
104092 return 0;
104093 case TK_COLUMN:
104094 assert( ExprUseYTab(p) );
104095 return ExprHasProperty(p, EP_CanBeNull) ||
104096 p->y.pTab==0 || /* Reference to column of index on expression */
104097 (p->iColumn>=0
104098 && p->y.pTab->aCol!=0 /* Possible due to prior error */
104099 && p->y.pTab->aCol[p->iColumn].notNull==0);
104100 default:
104101 return 1;
104102 }
104103}
104104
104105/*
104106** Return TRUE if the given expression is a constant which would be
104107** unchanged by OP_Affinity with the affinity given in the second
104108** argument.
104109**
104110** This routine is used to determine if the OP_Affinity operation
104111** can be omitted. When in doubt return FALSE. A false negative
104112** is harmless. A false positive, however, can result in the wrong
104113** answer.
104114*/
104115SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
104116 u8 op;
104117 int unaryMinus = 0;
104118 if( aff==SQLITE_AFF_BLOB ) return 1;
104119 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){
104120 if( p->op==TK_UMINUS ) unaryMinus = 1;
104121 p = p->pLeft;
104122 }
104123 op = p->op;
104124 if( op==TK_REGISTER ) op = p->op2;
104125 switch( op ){
104126 case TK_INTEGER: {
104127 return aff>=SQLITE_AFF_NUMERIC;
104128 }
104129 case TK_FLOAT: {
104130 return aff>=SQLITE_AFF_NUMERIC;
104131 }
104132 case TK_STRING: {
104133 return !unaryMinus && aff==SQLITE_AFF_TEXT;
104134 }
104135 case TK_BLOB: {
104136 return !unaryMinus;
104137 }
104138 case TK_COLUMN: {
104139 assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */
104140 return aff>=SQLITE_AFF_NUMERIC && p->iColumn<0;
104141 }
104142 default: {
104143 return 0;
104144 }
104145 }
104146}
104147
104148/*
104149** Return TRUE if the given string is a row-id column name.
104150*/
104151SQLITE_PRIVATE int sqlite3IsRowid(const char *z){
104152 if( sqlite3StrICmp(zLeft: z, zRight: "_ROWID_")==0 ) return 1;
104153 if( sqlite3StrICmp(zLeft: z, zRight: "ROWID")==0 ) return 1;
104154 if( sqlite3StrICmp(zLeft: z, zRight: "OID")==0 ) return 1;
104155 return 0;
104156}
104157
104158/*
104159** pX is the RHS of an IN operator. If pX is a SELECT statement
104160** that can be simplified to a direct table access, then return
104161** a pointer to the SELECT statement. If pX is not a SELECT statement,
104162** or if the SELECT statement needs to be manifested into a transient
104163** table, then return NULL.
104164*/
104165#ifndef SQLITE_OMIT_SUBQUERY
104166static Select *isCandidateForInOpt(const Expr *pX){
104167 Select *p;
104168 SrcList *pSrc;
104169 ExprList *pEList;
104170 Table *pTab;
104171 int i;
104172 if( !ExprUseXSelect(pX) ) return 0; /* Not a subquery */
104173 if( ExprHasProperty(pX, EP_VarSelect) ) return 0; /* Correlated subq */
104174 p = pX->x.pSelect;
104175 if( p->pPrior ) return 0; /* Not a compound SELECT */
104176 if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
104177 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
104178 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
104179 return 0; /* No DISTINCT keyword and no aggregate functions */
104180 }
104181 assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */
104182 if( p->pLimit ) return 0; /* Has no LIMIT clause */
104183 if( p->pWhere ) return 0; /* Has no WHERE clause */
104184 pSrc = p->pSrc;
104185 assert( pSrc!=0 );
104186 if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */
104187 if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */
104188 pTab = pSrc->a[0].pTab;
104189 assert( pTab!=0 );
104190 assert( !IsView(pTab) ); /* FROM clause is not a view */
104191 if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */
104192 pEList = p->pEList;
104193 assert( pEList!=0 );
104194 /* All SELECT results must be columns. */
104195 for(i=0; i<pEList->nExpr; i++){
104196 Expr *pRes = pEList->a[i].pExpr;
104197 if( pRes->op!=TK_COLUMN ) return 0;
104198 assert( pRes->iTable==pSrc->a[0].iCursor ); /* Not a correlated subquery */
104199 }
104200 return p;
104201}
104202#endif /* SQLITE_OMIT_SUBQUERY */
104203
104204#ifndef SQLITE_OMIT_SUBQUERY
104205/*
104206** Generate code that checks the left-most column of index table iCur to see if
104207** it contains any NULL entries. Cause the register at regHasNull to be set
104208** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull
104209** to be set to NULL if iCur contains one or more NULL values.
104210*/
104211static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){
104212 int addr1;
104213 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: regHasNull);
104214 addr1 = sqlite3VdbeAddOp1(p: v, OP_Rewind, p1: iCur); VdbeCoverage(v);
104215 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iCur, p2: 0, p3: regHasNull);
104216 sqlite3VdbeChangeP5(p: v, OPFLAG_TYPEOFARG);
104217 VdbeComment((v, "first_entry_in(%d)", iCur));
104218 sqlite3VdbeJumpHere(p: v, addr: addr1);
104219}
104220#endif
104221
104222
104223#ifndef SQLITE_OMIT_SUBQUERY
104224/*
104225** The argument is an IN operator with a list (not a subquery) on the
104226** right-hand side. Return TRUE if that list is constant.
104227*/
104228static int sqlite3InRhsIsConstant(Expr *pIn){
104229 Expr *pLHS;
104230 int res;
104231 assert( !ExprHasProperty(pIn, EP_xIsSelect) );
104232 pLHS = pIn->pLeft;
104233 pIn->pLeft = 0;
104234 res = sqlite3ExprIsConstant(p: pIn);
104235 pIn->pLeft = pLHS;
104236 return res;
104237}
104238#endif
104239
104240/*
104241** This function is used by the implementation of the IN (...) operator.
104242** The pX parameter is the expression on the RHS of the IN operator, which
104243** might be either a list of expressions or a subquery.
104244**
104245** The job of this routine is to find or create a b-tree object that can
104246** be used either to test for membership in the RHS set or to iterate through
104247** all members of the RHS set, skipping duplicates.
104248**
104249** A cursor is opened on the b-tree object that is the RHS of the IN operator
104250** and pX->iTable is set to the index of that cursor.
104251**
104252** The returned value of this function indicates the b-tree type, as follows:
104253**
104254** IN_INDEX_ROWID - The cursor was opened on a database table.
104255** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index.
104256** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
104257** IN_INDEX_EPH - The cursor was opened on a specially created and
104258** populated epheremal table.
104259** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be
104260** implemented as a sequence of comparisons.
104261**
104262** An existing b-tree might be used if the RHS expression pX is a simple
104263** subquery such as:
104264**
104265** SELECT <column1>, <column2>... FROM <table>
104266**
104267** If the RHS of the IN operator is a list or a more complex subquery, then
104268** an ephemeral table might need to be generated from the RHS and then
104269** pX->iTable made to point to the ephemeral table instead of an
104270** existing table.
104271**
104272** The inFlags parameter must contain, at a minimum, one of the bits
104273** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP but not both. If inFlags contains
104274** IN_INDEX_MEMBERSHIP, then the generated table will be used for a fast
104275** membership test. When the IN_INDEX_LOOP bit is set, the IN index will
104276** be used to loop over all values of the RHS of the IN operator.
104277**
104278** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
104279** through the set members) then the b-tree must not contain duplicates.
104280** An epheremal table will be created unless the selected columns are guaranteed
104281** to be unique - either because it is an INTEGER PRIMARY KEY or due to
104282** a UNIQUE constraint or index.
104283**
104284** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
104285** for fast set membership tests) then an epheremal table must
104286** be used unless <columns> is a single INTEGER PRIMARY KEY column or an
104287** index can be found with the specified <columns> as its left-most.
104288**
104289** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and
104290** if the RHS of the IN operator is a list (not a subquery) then this
104291** routine might decide that creating an ephemeral b-tree for membership
104292** testing is too expensive and return IN_INDEX_NOOP. In that case, the
104293** calling routine should implement the IN operator using a sequence
104294** of Eq or Ne comparison operations.
104295**
104296** When the b-tree is being used for membership tests, the calling function
104297** might need to know whether or not the RHS side of the IN operator
104298** contains a NULL. If prRhsHasNull is not a NULL pointer and
104299** if there is any chance that the (...) might contain a NULL value at
104300** runtime, then a register is allocated and the register number written
104301** to *prRhsHasNull. If there is no chance that the (...) contains a
104302** NULL value, then *prRhsHasNull is left unchanged.
104303**
104304** If a register is allocated and its location stored in *prRhsHasNull, then
104305** the value in that register will be NULL if the b-tree contains one or more
104306** NULL values, and it will be some non-NULL value if the b-tree contains no
104307** NULL values.
104308**
104309** If the aiMap parameter is not NULL, it must point to an array containing
104310** one element for each column returned by the SELECT statement on the RHS
104311** of the IN(...) operator. The i'th entry of the array is populated with the
104312** offset of the index column that matches the i'th column returned by the
104313** SELECT. For example, if the expression and selected index are:
104314**
104315** (?,?,?) IN (SELECT a, b, c FROM t1)
104316** CREATE INDEX i1 ON t1(b, c, a);
104317**
104318** then aiMap[] is populated with {2, 0, 1}.
104319*/
104320#ifndef SQLITE_OMIT_SUBQUERY
104321SQLITE_PRIVATE int sqlite3FindInIndex(
104322 Parse *pParse, /* Parsing context */
104323 Expr *pX, /* The IN expression */
104324 u32 inFlags, /* IN_INDEX_LOOP, _MEMBERSHIP, and/or _NOOP_OK */
104325 int *prRhsHasNull, /* Register holding NULL status. See notes */
104326 int *aiMap, /* Mapping from Index fields to RHS fields */
104327 int *piTab /* OUT: index to use */
104328){
104329 Select *p; /* SELECT to the right of IN operator */
104330 int eType = 0; /* Type of RHS table. IN_INDEX_* */
104331 int iTab = pParse->nTab++; /* Cursor of the RHS table */
104332 int mustBeUnique; /* True if RHS must be unique */
104333 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
104334
104335 assert( pX->op==TK_IN );
104336 mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0;
104337
104338 /* If the RHS of this IN(...) operator is a SELECT, and if it matters
104339 ** whether or not the SELECT result contains NULL values, check whether
104340 ** or not NULL is actually possible (it may not be, for example, due
104341 ** to NOT NULL constraints in the schema). If no NULL values are possible,
104342 ** set prRhsHasNull to 0 before continuing. */
104343 if( prRhsHasNull && ExprUseXSelect(pX) ){
104344 int i;
104345 ExprList *pEList = pX->x.pSelect->pEList;
104346 for(i=0; i<pEList->nExpr; i++){
104347 if( sqlite3ExprCanBeNull(p: pEList->a[i].pExpr) ) break;
104348 }
104349 if( i==pEList->nExpr ){
104350 prRhsHasNull = 0;
104351 }
104352 }
104353
104354 /* Check to see if an existing table or index can be used to
104355 ** satisfy the query. This is preferable to generating a new
104356 ** ephemeral table. */
104357 if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){
104358 sqlite3 *db = pParse->db; /* Database connection */
104359 Table *pTab; /* Table <table>. */
104360 int iDb; /* Database idx for pTab */
104361 ExprList *pEList = p->pEList;
104362 int nExpr = pEList->nExpr;
104363
104364 assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */
104365 assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
104366 assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */
104367 pTab = p->pSrc->a[0].pTab;
104368
104369 /* Code an OP_Transaction and OP_TableLock for <table>. */
104370 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
104371 assert( iDb>=0 && iDb<SQLITE_MAX_DB );
104372 sqlite3CodeVerifySchema(pParse, iDb);
104373 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
104374
104375 assert(v); /* sqlite3GetVdbe() has always been previously called */
104376 if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){
104377 /* The "x IN (SELECT rowid FROM table)" case */
104378 int iAddr = sqlite3VdbeAddOp0(p: v, OP_Once);
104379 VdbeCoverage(v);
104380
104381 sqlite3OpenTable(pParse, iCur: iTab, iDb, pTab, OP_OpenRead);
104382 eType = IN_INDEX_ROWID;
104383 ExplainQueryPlan((pParse, 0,
104384 "USING ROWID SEARCH ON TABLE %s FOR IN-OPERATOR",pTab->zName));
104385 sqlite3VdbeJumpHere(p: v, addr: iAddr);
104386 }else{
104387 Index *pIdx; /* Iterator variable */
104388 int affinity_ok = 1;
104389 int i;
104390
104391 /* Check that the affinity that will be used to perform each
104392 ** comparison is the same as the affinity of each column in table
104393 ** on the RHS of the IN operator. If it not, it is not possible to
104394 ** use any index of the RHS table. */
104395 for(i=0; i<nExpr && affinity_ok; i++){
104396 Expr *pLhs = sqlite3VectorFieldSubexpr(pVector: pX->pLeft, i);
104397 int iCol = pEList->a[i].pExpr->iColumn;
104398 char idxaff = sqlite3TableColumnAffinity(pTab,iCol); /* RHS table */
104399 char cmpaff = sqlite3CompareAffinity(pExpr: pLhs, aff2: idxaff);
104400 testcase( cmpaff==SQLITE_AFF_BLOB );
104401 testcase( cmpaff==SQLITE_AFF_TEXT );
104402 switch( cmpaff ){
104403 case SQLITE_AFF_BLOB:
104404 break;
104405 case SQLITE_AFF_TEXT:
104406 /* sqlite3CompareAffinity() only returns TEXT if one side or the
104407 ** other has no affinity and the other side is TEXT. Hence,
104408 ** the only way for cmpaff to be TEXT is for idxaff to be TEXT
104409 ** and for the term on the LHS of the IN to have no affinity. */
104410 assert( idxaff==SQLITE_AFF_TEXT );
104411 break;
104412 default:
104413 affinity_ok = sqlite3IsNumericAffinity(idxaff);
104414 }
104415 }
104416
104417 if( affinity_ok ){
104418 /* Search for an existing index that will work for this IN operator */
104419 for(pIdx=pTab->pIndex; pIdx && eType==0; pIdx=pIdx->pNext){
104420 Bitmask colUsed; /* Columns of the index used */
104421 Bitmask mCol; /* Mask for the current column */
104422 if( pIdx->nColumn<nExpr ) continue;
104423 if( pIdx->pPartIdxWhere!=0 ) continue;
104424 /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute
104425 ** BITMASK(nExpr) without overflowing */
104426 testcase( pIdx->nColumn==BMS-2 );
104427 testcase( pIdx->nColumn==BMS-1 );
104428 if( pIdx->nColumn>=BMS-1 ) continue;
104429 if( mustBeUnique ){
104430 if( pIdx->nKeyCol>nExpr
104431 ||(pIdx->nColumn>nExpr && !IsUniqueIndex(pIdx))
104432 ){
104433 continue; /* This index is not unique over the IN RHS columns */
104434 }
104435 }
104436
104437 colUsed = 0; /* Columns of index used so far */
104438 for(i=0; i<nExpr; i++){
104439 Expr *pLhs = sqlite3VectorFieldSubexpr(pVector: pX->pLeft, i);
104440 Expr *pRhs = pEList->a[i].pExpr;
104441 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pLeft: pLhs, pRight: pRhs);
104442 int j;
104443
104444 assert( pReq!=0 || pRhs->iColumn==XN_ROWID
104445 || pParse->nErr || db->mallocFailed );
104446 for(j=0; j<nExpr; j++){
104447 if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue;
104448 assert( pIdx->azColl[j] );
104449 if( pReq!=0 && sqlite3StrICmp(zLeft: pReq->zName, zRight: pIdx->azColl[j])!=0 ){
104450 continue;
104451 }
104452 break;
104453 }
104454 if( j==nExpr ) break;
104455 mCol = MASKBIT(j);
104456 if( mCol & colUsed ) break; /* Each column used only once */
104457 colUsed |= mCol;
104458 if( aiMap ) aiMap[i] = j;
104459 }
104460
104461 assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) );
104462 if( colUsed==(MASKBIT(nExpr)-1) ){
104463 /* If we reach this point, that means the index pIdx is usable */
104464 int iAddr = sqlite3VdbeAddOp0(p: v, OP_Once); VdbeCoverage(v);
104465 ExplainQueryPlan((pParse, 0,
104466 "USING INDEX %s FOR IN-OPERATOR",pIdx->zName));
104467 sqlite3VdbeAddOp3(p: v, OP_OpenRead, p1: iTab, p2: pIdx->tnum, p3: iDb);
104468 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
104469 VdbeComment((v, "%s", pIdx->zName));
104470 assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
104471 eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
104472
104473 if( prRhsHasNull ){
104474#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
104475 i64 mask = (1<<nExpr)-1;
104476 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed,
104477 iTab, 0, 0, (u8*)&mask, P4_INT64);
104478#endif
104479 *prRhsHasNull = ++pParse->nMem;
104480 if( nExpr==1 ){
104481 sqlite3SetHasNullFlag(v, iCur: iTab, regHasNull: *prRhsHasNull);
104482 }
104483 }
104484 sqlite3VdbeJumpHere(p: v, addr: iAddr);
104485 }
104486 } /* End loop over indexes */
104487 } /* End if( affinity_ok ) */
104488 } /* End if not an rowid index */
104489 } /* End attempt to optimize using an index */
104490
104491 /* If no preexisting index is available for the IN clause
104492 ** and IN_INDEX_NOOP is an allowed reply
104493 ** and the RHS of the IN operator is a list, not a subquery
104494 ** and the RHS is not constant or has two or fewer terms,
104495 ** then it is not worth creating an ephemeral table to evaluate
104496 ** the IN operator so return IN_INDEX_NOOP.
104497 */
104498 if( eType==0
104499 && (inFlags & IN_INDEX_NOOP_OK)
104500 && ExprUseXList(pX)
104501 && (!sqlite3InRhsIsConstant(pIn: pX) || pX->x.pList->nExpr<=2)
104502 ){
104503 eType = IN_INDEX_NOOP;
104504 }
104505
104506 if( eType==0 ){
104507 /* Could not find an existing table or index to use as the RHS b-tree.
104508 ** We will have to generate an ephemeral table to do the job.
104509 */
104510 u32 savedNQueryLoop = pParse->nQueryLoop;
104511 int rMayHaveNull = 0;
104512 eType = IN_INDEX_EPH;
104513 if( inFlags & IN_INDEX_LOOP ){
104514 pParse->nQueryLoop = 0;
104515 }else if( prRhsHasNull ){
104516 *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
104517 }
104518 assert( pX->op==TK_IN );
104519 sqlite3CodeRhsOfIN(pParse, pX, iTab);
104520 if( rMayHaveNull ){
104521 sqlite3SetHasNullFlag(v, iCur: iTab, regHasNull: rMayHaveNull);
104522 }
104523 pParse->nQueryLoop = savedNQueryLoop;
104524 }
104525
104526 if( aiMap && eType!=IN_INDEX_INDEX_ASC && eType!=IN_INDEX_INDEX_DESC ){
104527 int i, n;
104528 n = sqlite3ExprVectorSize(pExpr: pX->pLeft);
104529 for(i=0; i<n; i++) aiMap[i] = i;
104530 }
104531 *piTab = iTab;
104532 return eType;
104533}
104534#endif
104535
104536#ifndef SQLITE_OMIT_SUBQUERY
104537/*
104538** Argument pExpr is an (?, ?...) IN(...) expression. This
104539** function allocates and returns a nul-terminated string containing
104540** the affinities to be used for each column of the comparison.
104541**
104542** It is the responsibility of the caller to ensure that the returned
104543** string is eventually freed using sqlite3DbFree().
104544*/
104545static char *exprINAffinity(Parse *pParse, const Expr *pExpr){
104546 Expr *pLeft = pExpr->pLeft;
104547 int nVal = sqlite3ExprVectorSize(pExpr: pLeft);
104548 Select *pSelect = ExprUseXSelect(pExpr) ? pExpr->x.pSelect : 0;
104549 char *zRet;
104550
104551 assert( pExpr->op==TK_IN );
104552 zRet = sqlite3DbMallocRaw(db: pParse->db, n: nVal+1);
104553 if( zRet ){
104554 int i;
104555 for(i=0; i<nVal; i++){
104556 Expr *pA = sqlite3VectorFieldSubexpr(pVector: pLeft, i);
104557 char a = sqlite3ExprAffinity(pExpr: pA);
104558 if( pSelect ){
104559 zRet[i] = sqlite3CompareAffinity(pExpr: pSelect->pEList->a[i].pExpr, aff2: a);
104560 }else{
104561 zRet[i] = a;
104562 }
104563 }
104564 zRet[nVal] = '\0';
104565 }
104566 return zRet;
104567}
104568#endif
104569
104570#ifndef SQLITE_OMIT_SUBQUERY
104571/*
104572** Load the Parse object passed as the first argument with an error
104573** message of the form:
104574**
104575** "sub-select returns N columns - expected M"
104576*/
104577SQLITE_PRIVATE void sqlite3SubselectError(Parse *pParse, int nActual, int nExpect){
104578 if( pParse->nErr==0 ){
104579 const char *zFmt = "sub-select returns %d columns - expected %d";
104580 sqlite3ErrorMsg(pParse, zFormat: zFmt, nActual, nExpect);
104581 }
104582}
104583#endif
104584
104585/*
104586** Expression pExpr is a vector that has been used in a context where
104587** it is not permitted. If pExpr is a sub-select vector, this routine
104588** loads the Parse object with a message of the form:
104589**
104590** "sub-select returns N columns - expected 1"
104591**
104592** Or, if it is a regular scalar vector:
104593**
104594** "row value misused"
104595*/
104596SQLITE_PRIVATE void sqlite3VectorErrorMsg(Parse *pParse, Expr *pExpr){
104597#ifndef SQLITE_OMIT_SUBQUERY
104598 if( ExprUseXSelect(pExpr) ){
104599 sqlite3SubselectError(pParse, nActual: pExpr->x.pSelect->pEList->nExpr, nExpect: 1);
104600 }else
104601#endif
104602 {
104603 sqlite3ErrorMsg(pParse, zFormat: "row value misused");
104604 }
104605}
104606
104607#ifndef SQLITE_OMIT_SUBQUERY
104608/*
104609** Generate code that will construct an ephemeral table containing all terms
104610** in the RHS of an IN operator. The IN operator can be in either of two
104611** forms:
104612**
104613** x IN (4,5,11) -- IN operator with list on right-hand side
104614** x IN (SELECT a FROM b) -- IN operator with subquery on the right
104615**
104616** The pExpr parameter is the IN operator. The cursor number for the
104617** constructed ephermeral table is returned. The first time the ephemeral
104618** table is computed, the cursor number is also stored in pExpr->iTable,
104619** however the cursor number returned might not be the same, as it might
104620** have been duplicated using OP_OpenDup.
104621**
104622** If the LHS expression ("x" in the examples) is a column value, or
104623** the SELECT statement returns a column value, then the affinity of that
104624** column is used to build the index keys. If both 'x' and the
104625** SELECT... statement are columns, then numeric affinity is used
104626** if either column has NUMERIC or INTEGER affinity. If neither
104627** 'x' nor the SELECT... statement are columns, then numeric affinity
104628** is used.
104629*/
104630SQLITE_PRIVATE void sqlite3CodeRhsOfIN(
104631 Parse *pParse, /* Parsing context */
104632 Expr *pExpr, /* The IN operator */
104633 int iTab /* Use this cursor number */
104634){
104635 int addrOnce = 0; /* Address of the OP_Once instruction at top */
104636 int addr; /* Address of OP_OpenEphemeral instruction */
104637 Expr *pLeft; /* the LHS of the IN operator */
104638 KeyInfo *pKeyInfo = 0; /* Key information */
104639 int nVal; /* Size of vector pLeft */
104640 Vdbe *v; /* The prepared statement under construction */
104641
104642 v = pParse->pVdbe;
104643 assert( v!=0 );
104644
104645 /* The evaluation of the IN must be repeated every time it
104646 ** is encountered if any of the following is true:
104647 **
104648 ** * The right-hand side is a correlated subquery
104649 ** * The right-hand side is an expression list containing variables
104650 ** * We are inside a trigger
104651 **
104652 ** If all of the above are false, then we can compute the RHS just once
104653 ** and reuse it many names.
104654 */
104655 if( !ExprHasProperty(pExpr, EP_VarSelect) && pParse->iSelfTab==0 ){
104656 /* Reuse of the RHS is allowed */
104657 /* If this routine has already been coded, but the previous code
104658 ** might not have been invoked yet, so invoke it now as a subroutine.
104659 */
104660 if( ExprHasProperty(pExpr, EP_Subrtn) ){
104661 addrOnce = sqlite3VdbeAddOp0(p: v, OP_Once); VdbeCoverage(v);
104662 if( ExprUseXSelect(pExpr) ){
104663 ExplainQueryPlan((pParse, 0, "REUSE LIST SUBQUERY %d",
104664 pExpr->x.pSelect->selId));
104665 }
104666 assert( ExprUseYSub(pExpr) );
104667 sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: pExpr->y.sub.regReturn,
104668 p2: pExpr->y.sub.iAddr);
104669 sqlite3VdbeAddOp2(p: v, OP_OpenDup, p1: iTab, p2: pExpr->iTable);
104670 sqlite3VdbeJumpHere(p: v, addr: addrOnce);
104671 return;
104672 }
104673
104674 /* Begin coding the subroutine */
104675 assert( !ExprUseYWin(pExpr) );
104676 ExprSetProperty(pExpr, EP_Subrtn);
104677 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
104678 pExpr->y.sub.regReturn = ++pParse->nMem;
104679 pExpr->y.sub.iAddr =
104680 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: pExpr->y.sub.regReturn) + 1;
104681 VdbeComment((v, "return address"));
104682
104683 addrOnce = sqlite3VdbeAddOp0(p: v, OP_Once); VdbeCoverage(v);
104684 }
104685
104686 /* Check to see if this is a vector IN operator */
104687 pLeft = pExpr->pLeft;
104688 nVal = sqlite3ExprVectorSize(pExpr: pLeft);
104689
104690 /* Construct the ephemeral table that will contain the content of
104691 ** RHS of the IN operator.
104692 */
104693 pExpr->iTable = iTab;
104694 addr = sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: pExpr->iTable, p2: nVal);
104695#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
104696 if( ExprUseXSelect(pExpr) ){
104697 VdbeComment((v, "Result of SELECT %u", pExpr->x.pSelect->selId));
104698 }else{
104699 VdbeComment((v, "RHS of IN operator"));
104700 }
104701#endif
104702 pKeyInfo = sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
104703
104704 if( ExprUseXSelect(pExpr) ){
104705 /* Case 1: expr IN (SELECT ...)
104706 **
104707 ** Generate code to write the results of the select into the temporary
104708 ** table allocated and opened above.
104709 */
104710 Select *pSelect = pExpr->x.pSelect;
104711 ExprList *pEList = pSelect->pEList;
104712
104713 ExplainQueryPlan((pParse, 1, "%sLIST SUBQUERY %d",
104714 addrOnce?"":"CORRELATED ", pSelect->selId
104715 ));
104716 /* If the LHS and RHS of the IN operator do not match, that
104717 ** error will have been caught long before we reach this point. */
104718 if( ALWAYS(pEList->nExpr==nVal) ){
104719 Select *pCopy;
104720 SelectDest dest;
104721 int i;
104722 int rc;
104723 sqlite3SelectDestInit(&dest, SRT_Set, iTab);
104724 dest.zAffSdst = exprINAffinity(pParse, pExpr);
104725 pSelect->iLimit = 0;
104726 testcase( pSelect->selFlags & SF_Distinct );
104727 testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
104728 pCopy = sqlite3SelectDup(db: pParse->db, pDup: pSelect, flags: 0);
104729 rc = pParse->db->mallocFailed ? 1 :sqlite3Select(pParse, pCopy, &dest);
104730 sqlite3SelectDelete(pParse->db, pCopy);
104731 sqlite3DbFree(db: pParse->db, p: dest.zAffSdst);
104732 if( rc ){
104733 sqlite3KeyInfoUnref(pKeyInfo);
104734 return;
104735 }
104736 assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */
104737 assert( pEList!=0 );
104738 assert( pEList->nExpr>0 );
104739 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
104740 for(i=0; i<nVal; i++){
104741 Expr *p = sqlite3VectorFieldSubexpr(pVector: pLeft, i);
104742 pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq(
104743 pParse, pLeft: p, pRight: pEList->a[i].pExpr
104744 );
104745 }
104746 }
104747 }else if( ALWAYS(pExpr->x.pList!=0) ){
104748 /* Case 2: expr IN (exprlist)
104749 **
104750 ** For each expression, build an index key from the evaluation and
104751 ** store it in the temporary table. If <expr> is a column, then use
104752 ** that columns affinity when building index keys. If <expr> is not
104753 ** a column, use numeric affinity.
104754 */
104755 char affinity; /* Affinity of the LHS of the IN */
104756 int i;
104757 ExprList *pList = pExpr->x.pList;
104758 struct ExprList_item *pItem;
104759 int r1, r2;
104760 affinity = sqlite3ExprAffinity(pExpr: pLeft);
104761 if( affinity<=SQLITE_AFF_NONE ){
104762 affinity = SQLITE_AFF_BLOB;
104763 }else if( affinity==SQLITE_AFF_REAL ){
104764 affinity = SQLITE_AFF_NUMERIC;
104765 }
104766 if( pKeyInfo ){
104767 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
104768 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr: pExpr->pLeft);
104769 }
104770
104771 /* Loop through each expression in <exprlist>. */
104772 r1 = sqlite3GetTempReg(pParse);
104773 r2 = sqlite3GetTempReg(pParse);
104774 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
104775 Expr *pE2 = pItem->pExpr;
104776
104777 /* If the expression is not constant then we will need to
104778 ** disable the test that was generated above that makes sure
104779 ** this code only executes once. Because for a non-constant
104780 ** expression we need to rerun this code each time.
104781 */
104782 if( addrOnce && !sqlite3ExprIsConstant(p: pE2) ){
104783 sqlite3VdbeChangeToNoop(p: v, addr: addrOnce);
104784 ExprClearProperty(pExpr, EP_Subrtn);
104785 addrOnce = 0;
104786 }
104787
104788 /* Evaluate the expression and insert it into the temp table */
104789 sqlite3ExprCode(pParse, pE2, r1);
104790 sqlite3VdbeAddOp4(p: v, OP_MakeRecord, p1: r1, p2: 1, p3: r2, zP4: &affinity, p4type: 1);
104791 sqlite3VdbeAddOp4Int(p: v, OP_IdxInsert, p1: iTab, p2: r2, p3: r1, p4: 1);
104792 }
104793 sqlite3ReleaseTempReg(pParse, r1);
104794 sqlite3ReleaseTempReg(pParse, r2);
104795 }
104796 if( pKeyInfo ){
104797 sqlite3VdbeChangeP4(p: v, addr, zP4: (void *)pKeyInfo, P4_KEYINFO);
104798 }
104799 if( addrOnce ){
104800 sqlite3VdbeJumpHere(p: v, addr: addrOnce);
104801 /* Subroutine return */
104802 assert( ExprUseYSub(pExpr) );
104803 sqlite3VdbeAddOp1(p: v, OP_Return, p1: pExpr->y.sub.regReturn);
104804 sqlite3VdbeChangeP1(p: v, addr: pExpr->y.sub.iAddr-1, val: sqlite3VdbeCurrentAddr(p: v)-1);
104805 sqlite3ClearTempRegCache(pParse);
104806 }
104807}
104808#endif /* SQLITE_OMIT_SUBQUERY */
104809
104810/*
104811** Generate code for scalar subqueries used as a subquery expression
104812** or EXISTS operator:
104813**
104814** (SELECT a FROM b) -- subquery
104815** EXISTS (SELECT a FROM b) -- EXISTS subquery
104816**
104817** The pExpr parameter is the SELECT or EXISTS operator to be coded.
104818**
104819** Return the register that holds the result. For a multi-column SELECT,
104820** the result is stored in a contiguous array of registers and the
104821** return value is the register of the left-most result column.
104822** Return 0 if an error occurs.
104823*/
104824#ifndef SQLITE_OMIT_SUBQUERY
104825SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
104826 int addrOnce = 0; /* Address of OP_Once at top of subroutine */
104827 int rReg = 0; /* Register storing resulting */
104828 Select *pSel; /* SELECT statement to encode */
104829 SelectDest dest; /* How to deal with SELECT result */
104830 int nReg; /* Registers to allocate */
104831 Expr *pLimit; /* New limit expression */
104832
104833 Vdbe *v = pParse->pVdbe;
104834 assert( v!=0 );
104835 if( pParse->nErr ) return 0;
104836 testcase( pExpr->op==TK_EXISTS );
104837 testcase( pExpr->op==TK_SELECT );
104838 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
104839 assert( ExprUseXSelect(pExpr) );
104840 pSel = pExpr->x.pSelect;
104841
104842 /* If this routine has already been coded, then invoke it as a
104843 ** subroutine. */
104844 if( ExprHasProperty(pExpr, EP_Subrtn) ){
104845 ExplainQueryPlan((pParse, 0, "REUSE SUBQUERY %d", pSel->selId));
104846 assert( ExprUseYSub(pExpr) );
104847 sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: pExpr->y.sub.regReturn,
104848 p2: pExpr->y.sub.iAddr);
104849 return pExpr->iTable;
104850 }
104851
104852 /* Begin coding the subroutine */
104853 assert( !ExprUseYWin(pExpr) );
104854 assert( !ExprHasProperty(pExpr, EP_Reduced|EP_TokenOnly) );
104855 ExprSetProperty(pExpr, EP_Subrtn);
104856 pExpr->y.sub.regReturn = ++pParse->nMem;
104857 pExpr->y.sub.iAddr =
104858 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: pExpr->y.sub.regReturn) + 1;
104859 VdbeComment((v, "return address"));
104860
104861
104862 /* The evaluation of the EXISTS/SELECT must be repeated every time it
104863 ** is encountered if any of the following is true:
104864 **
104865 ** * The right-hand side is a correlated subquery
104866 ** * The right-hand side is an expression list containing variables
104867 ** * We are inside a trigger
104868 **
104869 ** If all of the above are false, then we can run this code just once
104870 ** save the results, and reuse the same result on subsequent invocations.
104871 */
104872 if( !ExprHasProperty(pExpr, EP_VarSelect) ){
104873 addrOnce = sqlite3VdbeAddOp0(p: v, OP_Once); VdbeCoverage(v);
104874 }
104875
104876 /* For a SELECT, generate code to put the values for all columns of
104877 ** the first row into an array of registers and return the index of
104878 ** the first register.
104879 **
104880 ** If this is an EXISTS, write an integer 0 (not exists) or 1 (exists)
104881 ** into a register and return that register number.
104882 **
104883 ** In both cases, the query is augmented with "LIMIT 1". Any
104884 ** preexisting limit is discarded in place of the new LIMIT 1.
104885 */
104886 ExplainQueryPlan((pParse, 1, "%sSCALAR SUBQUERY %d",
104887 addrOnce?"":"CORRELATED ", pSel->selId));
104888 nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1;
104889 sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
104890 pParse->nMem += nReg;
104891 if( pExpr->op==TK_SELECT ){
104892 dest.eDest = SRT_Mem;
104893 dest.iSdst = dest.iSDParm;
104894 dest.nSdst = nReg;
104895 sqlite3VdbeAddOp3(p: v, OP_Null, p1: 0, p2: dest.iSDParm, p3: dest.iSDParm+nReg-1);
104896 VdbeComment((v, "Init subquery result"));
104897 }else{
104898 dest.eDest = SRT_Exists;
104899 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: dest.iSDParm);
104900 VdbeComment((v, "Init EXISTS result"));
104901 }
104902 if( pSel->pLimit ){
104903 /* The subquery already has a limit. If the pre-existing limit is X
104904 ** then make the new limit X<>0 so that the new limit is either 1 or 0 */
104905 sqlite3 *db = pParse->db;
104906 pLimit = sqlite3Expr(db, TK_INTEGER, zToken: "0");
104907 if( pLimit ){
104908 pLimit->affExpr = SQLITE_AFF_NUMERIC;
104909 pLimit = sqlite3PExpr(pParse, TK_NE,
104910 pLeft: sqlite3ExprDup(db, p: pSel->pLimit->pLeft, flags: 0), pRight: pLimit);
104911 }
104912 sqlite3ExprDelete(db, p: pSel->pLimit->pLeft);
104913 pSel->pLimit->pLeft = pLimit;
104914 }else{
104915 /* If there is no pre-existing limit add a limit of 1 */
104916 pLimit = sqlite3Expr(db: pParse->db, TK_INTEGER, zToken: "1");
104917 pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLeft: pLimit, pRight: 0);
104918 }
104919 pSel->iLimit = 0;
104920 if( sqlite3Select(pParse, pSel, &dest) ){
104921 if( pParse->nErr ){
104922 pExpr->op2 = pExpr->op;
104923 pExpr->op = TK_ERROR;
104924 }
104925 return 0;
104926 }
104927 pExpr->iTable = rReg = dest.iSDParm;
104928 ExprSetVVAProperty(pExpr, EP_NoReduce);
104929 if( addrOnce ){
104930 sqlite3VdbeJumpHere(p: v, addr: addrOnce);
104931 }
104932
104933 /* Subroutine return */
104934 assert( ExprUseYSub(pExpr) );
104935 sqlite3VdbeAddOp1(p: v, OP_Return, p1: pExpr->y.sub.regReturn);
104936 sqlite3VdbeChangeP1(p: v, addr: pExpr->y.sub.iAddr-1, val: sqlite3VdbeCurrentAddr(p: v)-1);
104937 sqlite3ClearTempRegCache(pParse);
104938 return rReg;
104939}
104940#endif /* SQLITE_OMIT_SUBQUERY */
104941
104942#ifndef SQLITE_OMIT_SUBQUERY
104943/*
104944** Expr pIn is an IN(...) expression. This function checks that the
104945** sub-select on the RHS of the IN() operator has the same number of
104946** columns as the vector on the LHS. Or, if the RHS of the IN() is not
104947** a sub-query, that the LHS is a vector of size 1.
104948*/
104949SQLITE_PRIVATE int sqlite3ExprCheckIN(Parse *pParse, Expr *pIn){
104950 int nVector = sqlite3ExprVectorSize(pExpr: pIn->pLeft);
104951 if( ExprUseXSelect(pIn) && !pParse->db->mallocFailed ){
104952 if( nVector!=pIn->x.pSelect->pEList->nExpr ){
104953 sqlite3SubselectError(pParse, nActual: pIn->x.pSelect->pEList->nExpr, nExpect: nVector);
104954 return 1;
104955 }
104956 }else if( nVector!=1 ){
104957 sqlite3VectorErrorMsg(pParse, pExpr: pIn->pLeft);
104958 return 1;
104959 }
104960 return 0;
104961}
104962#endif
104963
104964#ifndef SQLITE_OMIT_SUBQUERY
104965/*
104966** Generate code for an IN expression.
104967**
104968** x IN (SELECT ...)
104969** x IN (value, value, ...)
104970**
104971** The left-hand side (LHS) is a scalar or vector expression. The
104972** right-hand side (RHS) is an array of zero or more scalar values, or a
104973** subquery. If the RHS is a subquery, the number of result columns must
104974** match the number of columns in the vector on the LHS. If the RHS is
104975** a list of values, the LHS must be a scalar.
104976**
104977** The IN operator is true if the LHS value is contained within the RHS.
104978** The result is false if the LHS is definitely not in the RHS. The
104979** result is NULL if the presence of the LHS in the RHS cannot be
104980** determined due to NULLs.
104981**
104982** This routine generates code that jumps to destIfFalse if the LHS is not
104983** contained within the RHS. If due to NULLs we cannot determine if the LHS
104984** is contained in the RHS then jump to destIfNull. If the LHS is contained
104985** within the RHS then fall through.
104986**
104987** See the separate in-operator.md documentation file in the canonical
104988** SQLite source tree for additional information.
104989*/
104990static void sqlite3ExprCodeIN(
104991 Parse *pParse, /* Parsing and code generating context */
104992 Expr *pExpr, /* The IN expression */
104993 int destIfFalse, /* Jump here if LHS is not contained in the RHS */
104994 int destIfNull /* Jump here if the results are unknown due to NULLs */
104995){
104996 int rRhsHasNull = 0; /* Register that is true if RHS contains NULL values */
104997 int eType; /* Type of the RHS */
104998 int rLhs; /* Register(s) holding the LHS values */
104999 int rLhsOrig; /* LHS values prior to reordering by aiMap[] */
105000 Vdbe *v; /* Statement under construction */
105001 int *aiMap = 0; /* Map from vector field to index column */
105002 char *zAff = 0; /* Affinity string for comparisons */
105003 int nVector; /* Size of vectors for this IN operator */
105004 int iDummy; /* Dummy parameter to exprCodeVector() */
105005 Expr *pLeft; /* The LHS of the IN operator */
105006 int i; /* loop counter */
105007 int destStep2; /* Where to jump when NULLs seen in step 2 */
105008 int destStep6 = 0; /* Start of code for Step 6 */
105009 int addrTruthOp; /* Address of opcode that determines the IN is true */
105010 int destNotNull; /* Jump here if a comparison is not true in step 6 */
105011 int addrTop; /* Top of the step-6 loop */
105012 int iTab = 0; /* Index to use */
105013 u8 okConstFactor = pParse->okConstFactor;
105014
105015 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) );
105016 pLeft = pExpr->pLeft;
105017 if( sqlite3ExprCheckIN(pParse, pIn: pExpr) ) return;
105018 zAff = exprINAffinity(pParse, pExpr);
105019 nVector = sqlite3ExprVectorSize(pExpr: pExpr->pLeft);
105020 aiMap = (int*)sqlite3DbMallocZero(
105021 db: pParse->db, n: nVector*(sizeof(int) + sizeof(char)) + 1
105022 );
105023 if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error;
105024
105025 /* Attempt to compute the RHS. After this step, if anything other than
105026 ** IN_INDEX_NOOP is returned, the table opened with cursor iTab
105027 ** contains the values that make up the RHS. If IN_INDEX_NOOP is returned,
105028 ** the RHS has not yet been coded. */
105029 v = pParse->pVdbe;
105030 assert( v!=0 ); /* OOM detected prior to this routine */
105031 VdbeNoopComment((v, "begin IN expr"));
105032 eType = sqlite3FindInIndex(pParse, pX: pExpr,
105033 IN_INDEX_MEMBERSHIP | IN_INDEX_NOOP_OK,
105034 prRhsHasNull: destIfFalse==destIfNull ? 0 : &rRhsHasNull,
105035 aiMap, piTab: &iTab);
105036
105037 assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH
105038 || eType==IN_INDEX_INDEX_ASC || eType==IN_INDEX_INDEX_DESC
105039 );
105040#ifdef SQLITE_DEBUG
105041 /* Confirm that aiMap[] contains nVector integer values between 0 and
105042 ** nVector-1. */
105043 for(i=0; i<nVector; i++){
105044 int j, cnt;
105045 for(cnt=j=0; j<nVector; j++) if( aiMap[j]==i ) cnt++;
105046 assert( cnt==1 );
105047 }
105048#endif
105049
105050 /* Code the LHS, the <expr> from "<expr> IN (...)". If the LHS is a
105051 ** vector, then it is stored in an array of nVector registers starting
105052 ** at r1.
105053 **
105054 ** sqlite3FindInIndex() might have reordered the fields of the LHS vector
105055 ** so that the fields are in the same order as an existing index. The
105056 ** aiMap[] array contains a mapping from the original LHS field order to
105057 ** the field order that matches the RHS index.
105058 **
105059 ** Avoid factoring the LHS of the IN(...) expression out of the loop,
105060 ** even if it is constant, as OP_Affinity may be used on the register
105061 ** by code generated below. */
105062 assert( pParse->okConstFactor==okConstFactor );
105063 pParse->okConstFactor = 0;
105064 rLhsOrig = exprCodeVector(pParse, p: pLeft, piToFree: &iDummy);
105065 pParse->okConstFactor = okConstFactor;
105066 for(i=0; i<nVector && aiMap[i]==i; i++){} /* Are LHS fields reordered? */
105067 if( i==nVector ){
105068 /* LHS fields are not reordered */
105069 rLhs = rLhsOrig;
105070 }else{
105071 /* Need to reorder the LHS fields according to aiMap */
105072 rLhs = sqlite3GetTempRange(pParse, nVector);
105073 for(i=0; i<nVector; i++){
105074 sqlite3VdbeAddOp3(p: v, OP_Copy, p1: rLhsOrig+i, p2: rLhs+aiMap[i], p3: 0);
105075 }
105076 }
105077
105078 /* If sqlite3FindInIndex() did not find or create an index that is
105079 ** suitable for evaluating the IN operator, then evaluate using a
105080 ** sequence of comparisons.
105081 **
105082 ** This is step (1) in the in-operator.md optimized algorithm.
105083 */
105084 if( eType==IN_INDEX_NOOP ){
105085 ExprList *pList;
105086 CollSeq *pColl;
105087 int labelOk = sqlite3VdbeMakeLabel(pParse);
105088 int r2, regToFree;
105089 int regCkNull = 0;
105090 int ii;
105091 assert( ExprUseXList(pExpr) );
105092 pList = pExpr->x.pList;
105093 pColl = sqlite3ExprCollSeq(pParse, pExpr: pExpr->pLeft);
105094 if( destIfNull!=destIfFalse ){
105095 regCkNull = sqlite3GetTempReg(pParse);
105096 sqlite3VdbeAddOp3(p: v, OP_BitAnd, p1: rLhs, p2: rLhs, p3: regCkNull);
105097 }
105098 for(ii=0; ii<pList->nExpr; ii++){
105099 r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
105100 if( regCkNull && sqlite3ExprCanBeNull(p: pList->a[ii].pExpr) ){
105101 sqlite3VdbeAddOp3(p: v, OP_BitAnd, p1: regCkNull, p2: r2, p3: regCkNull);
105102 }
105103 sqlite3ReleaseTempReg(pParse, regToFree);
105104 if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
105105 int op = rLhs!=r2 ? OP_Eq : OP_NotNull;
105106 sqlite3VdbeAddOp4(p: v, op, p1: rLhs, p2: labelOk, p3: r2,
105107 zP4: (void*)pColl, P4_COLLSEQ);
105108 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_Eq);
105109 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_Eq);
105110 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_NotNull);
105111 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_NotNull);
105112 sqlite3VdbeChangeP5(p: v, p5: zAff[0]);
105113 }else{
105114 int op = rLhs!=r2 ? OP_Ne : OP_IsNull;
105115 assert( destIfNull==destIfFalse );
105116 sqlite3VdbeAddOp4(p: v, op, p1: rLhs, p2: destIfFalse, p3: r2,
105117 zP4: (void*)pColl, P4_COLLSEQ);
105118 VdbeCoverageIf(v, op==OP_Ne);
105119 VdbeCoverageIf(v, op==OP_IsNull);
105120 sqlite3VdbeChangeP5(p: v, p5: zAff[0] | SQLITE_JUMPIFNULL);
105121 }
105122 }
105123 if( regCkNull ){
105124 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: regCkNull, p2: destIfNull); VdbeCoverage(v);
105125 sqlite3VdbeGoto(p: v, iDest: destIfFalse);
105126 }
105127 sqlite3VdbeResolveLabel(v, x: labelOk);
105128 sqlite3ReleaseTempReg(pParse, regCkNull);
105129 goto sqlite3ExprCodeIN_finished;
105130 }
105131
105132 /* Step 2: Check to see if the LHS contains any NULL columns. If the
105133 ** LHS does contain NULLs then the result must be either FALSE or NULL.
105134 ** We will then skip the binary search of the RHS.
105135 */
105136 if( destIfNull==destIfFalse ){
105137 destStep2 = destIfFalse;
105138 }else{
105139 destStep2 = destStep6 = sqlite3VdbeMakeLabel(pParse);
105140 }
105141 if( pParse->nErr ) goto sqlite3ExprCodeIN_finished;
105142 for(i=0; i<nVector; i++){
105143 Expr *p = sqlite3VectorFieldSubexpr(pVector: pExpr->pLeft, i);
105144 if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error;
105145 if( sqlite3ExprCanBeNull(p) ){
105146 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: rLhs+i, p2: destStep2);
105147 VdbeCoverage(v);
105148 }
105149 }
105150
105151 /* Step 3. The LHS is now known to be non-NULL. Do the binary search
105152 ** of the RHS using the LHS as a probe. If found, the result is
105153 ** true.
105154 */
105155 if( eType==IN_INDEX_ROWID ){
105156 /* In this case, the RHS is the ROWID of table b-tree and so we also
105157 ** know that the RHS is non-NULL. Hence, we combine steps 3 and 4
105158 ** into a single opcode. */
105159 sqlite3VdbeAddOp3(p: v, OP_SeekRowid, p1: iTab, p2: destIfFalse, p3: rLhs);
105160 VdbeCoverage(v);
105161 addrTruthOp = sqlite3VdbeAddOp0(p: v, OP_Goto); /* Return True */
105162 }else{
105163 sqlite3VdbeAddOp4(p: v, OP_Affinity, p1: rLhs, p2: nVector, p3: 0, zP4: zAff, p4type: nVector);
105164 if( destIfFalse==destIfNull ){
105165 /* Combine Step 3 and Step 5 into a single opcode */
105166 sqlite3VdbeAddOp4Int(p: v, OP_NotFound, p1: iTab, p2: destIfFalse,
105167 p3: rLhs, p4: nVector); VdbeCoverage(v);
105168 goto sqlite3ExprCodeIN_finished;
105169 }
105170 /* Ordinary Step 3, for the case where FALSE and NULL are distinct */
105171 addrTruthOp = sqlite3VdbeAddOp4Int(p: v, OP_Found, p1: iTab, p2: 0,
105172 p3: rLhs, p4: nVector); VdbeCoverage(v);
105173 }
105174
105175 /* Step 4. If the RHS is known to be non-NULL and we did not find
105176 ** an match on the search above, then the result must be FALSE.
105177 */
105178 if( rRhsHasNull && nVector==1 ){
105179 sqlite3VdbeAddOp2(p: v, OP_NotNull, p1: rRhsHasNull, p2: destIfFalse);
105180 VdbeCoverage(v);
105181 }
105182
105183 /* Step 5. If we do not care about the difference between NULL and
105184 ** FALSE, then just return false.
105185 */
105186 if( destIfFalse==destIfNull ) sqlite3VdbeGoto(p: v, iDest: destIfFalse);
105187
105188 /* Step 6: Loop through rows of the RHS. Compare each row to the LHS.
105189 ** If any comparison is NULL, then the result is NULL. If all
105190 ** comparisons are FALSE then the final result is FALSE.
105191 **
105192 ** For a scalar LHS, it is sufficient to check just the first row
105193 ** of the RHS.
105194 */
105195 if( destStep6 ) sqlite3VdbeResolveLabel(v, x: destStep6);
105196 addrTop = sqlite3VdbeAddOp2(p: v, OP_Rewind, p1: iTab, p2: destIfFalse);
105197 VdbeCoverage(v);
105198 if( nVector>1 ){
105199 destNotNull = sqlite3VdbeMakeLabel(pParse);
105200 }else{
105201 /* For nVector==1, combine steps 6 and 7 by immediately returning
105202 ** FALSE if the first comparison is not NULL */
105203 destNotNull = destIfFalse;
105204 }
105205 for(i=0; i<nVector; i++){
105206 Expr *p;
105207 CollSeq *pColl;
105208 int r3 = sqlite3GetTempReg(pParse);
105209 p = sqlite3VectorFieldSubexpr(pVector: pLeft, i);
105210 pColl = sqlite3ExprCollSeq(pParse, pExpr: p);
105211 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iTab, p2: i, p3: r3);
105212 sqlite3VdbeAddOp4(p: v, OP_Ne, p1: rLhs+i, p2: destNotNull, p3: r3,
105213 zP4: (void*)pColl, P4_COLLSEQ);
105214 VdbeCoverage(v);
105215 sqlite3ReleaseTempReg(pParse, r3);
105216 }
105217 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: destIfNull);
105218 if( nVector>1 ){
105219 sqlite3VdbeResolveLabel(v, x: destNotNull);
105220 sqlite3VdbeAddOp2(p: v, OP_Next, p1: iTab, p2: addrTop+1);
105221 VdbeCoverage(v);
105222
105223 /* Step 7: If we reach this point, we know that the result must
105224 ** be false. */
105225 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: destIfFalse);
105226 }
105227
105228 /* Jumps here in order to return true. */
105229 sqlite3VdbeJumpHere(p: v, addr: addrTruthOp);
105230
105231sqlite3ExprCodeIN_finished:
105232 if( rLhs!=rLhsOrig ) sqlite3ReleaseTempReg(pParse, rLhs);
105233 VdbeComment((v, "end IN expr"));
105234sqlite3ExprCodeIN_oom_error:
105235 sqlite3DbFree(db: pParse->db, p: aiMap);
105236 sqlite3DbFree(db: pParse->db, p: zAff);
105237}
105238#endif /* SQLITE_OMIT_SUBQUERY */
105239
105240#ifndef SQLITE_OMIT_FLOATING_POINT
105241/*
105242** Generate an instruction that will put the floating point
105243** value described by z[0..n-1] into register iMem.
105244**
105245** The z[] string will probably not be zero-terminated. But the
105246** z[n] character is guaranteed to be something that does not look
105247** like the continuation of the number.
105248*/
105249static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
105250 if( ALWAYS(z!=0) ){
105251 double value;
105252 sqlite3AtoF(z, pResult: &value, length: sqlite3Strlen30(z), SQLITE_UTF8);
105253 assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */
105254 if( negateFlag ) value = -value;
105255 sqlite3VdbeAddOp4Dup8(p: v, OP_Real, p1: 0, p2: iMem, p3: 0, zP4: (u8*)&value, P4_REAL);
105256 }
105257}
105258#endif
105259
105260
105261/*
105262** Generate an instruction that will put the integer describe by
105263** text z[0..n-1] into register iMem.
105264**
105265** Expr.u.zToken is always UTF8 and zero-terminated.
105266*/
105267static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){
105268 Vdbe *v = pParse->pVdbe;
105269 if( pExpr->flags & EP_IntValue ){
105270 int i = pExpr->u.iValue;
105271 assert( i>=0 );
105272 if( negFlag ) i = -i;
105273 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: i, p2: iMem);
105274 }else{
105275 int c;
105276 i64 value;
105277 const char *z = pExpr->u.zToken;
105278 assert( z!=0 );
105279 c = sqlite3DecOrHexToI64(z, pOut: &value);
105280 if( (c==3 && !negFlag) || (c==2) || (negFlag && value==SMALLEST_INT64)){
105281#ifdef SQLITE_OMIT_FLOATING_POINT
105282 sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
105283#else
105284#ifndef SQLITE_OMIT_HEX_INTEGER
105285 if( sqlite3_strnicmp(zLeft: z,zRight: "0x",N: 2)==0 ){
105286 sqlite3ErrorMsg(pParse, zFormat: "hex literal too big: %s%s", negFlag?"-":"",z);
105287 }else
105288#endif
105289 {
105290 codeReal(v, z, negateFlag: negFlag, iMem);
105291 }
105292#endif
105293 }else{
105294 if( negFlag ){ value = c==3 ? SMALLEST_INT64 : -value; }
105295 sqlite3VdbeAddOp4Dup8(p: v, OP_Int64, p1: 0, p2: iMem, p3: 0, zP4: (u8*)&value, P4_INT64);
105296 }
105297 }
105298}
105299
105300
105301/* Generate code that will load into register regOut a value that is
105302** appropriate for the iIdxCol-th column of index pIdx.
105303*/
105304SQLITE_PRIVATE void sqlite3ExprCodeLoadIndexColumn(
105305 Parse *pParse, /* The parsing context */
105306 Index *pIdx, /* The index whose column is to be loaded */
105307 int iTabCur, /* Cursor pointing to a table row */
105308 int iIdxCol, /* The column of the index to be loaded */
105309 int regOut /* Store the index column value in this register */
105310){
105311 i16 iTabCol = pIdx->aiColumn[iIdxCol];
105312 if( iTabCol==XN_EXPR ){
105313 assert( pIdx->aColExpr );
105314 assert( pIdx->aColExpr->nExpr>iIdxCol );
105315 pParse->iSelfTab = iTabCur + 1;
105316 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut);
105317 pParse->iSelfTab = 0;
105318 }else{
105319 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur,
105320 iTabCol, regOut);
105321 }
105322}
105323
105324#ifndef SQLITE_OMIT_GENERATED_COLUMNS
105325/*
105326** Generate code that will compute the value of generated column pCol
105327** and store the result in register regOut
105328*/
105329SQLITE_PRIVATE void sqlite3ExprCodeGeneratedColumn(
105330 Parse *pParse, /* Parsing context */
105331 Table *pTab, /* Table containing the generated column */
105332 Column *pCol, /* The generated column */
105333 int regOut /* Put the result in this register */
105334){
105335 int iAddr;
105336 Vdbe *v = pParse->pVdbe;
105337 assert( v!=0 );
105338 assert( pParse->iSelfTab!=0 );
105339 if( pParse->iSelfTab>0 ){
105340 iAddr = sqlite3VdbeAddOp3(p: v, OP_IfNullRow, p1: pParse->iSelfTab-1, p2: 0, p3: regOut);
105341 }else{
105342 iAddr = 0;
105343 }
105344 sqlite3ExprCodeCopy(pParse, sqlite3ColumnExpr(pTab,pCol), regOut);
105345 if( pCol->affinity>=SQLITE_AFF_TEXT ){
105346 sqlite3VdbeAddOp4(p: v, OP_Affinity, p1: regOut, p2: 1, p3: 0, zP4: &pCol->affinity, p4type: 1);
105347 }
105348 if( iAddr ) sqlite3VdbeJumpHere(p: v, addr: iAddr);
105349}
105350#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
105351
105352/*
105353** Generate code to extract the value of the iCol-th column of a table.
105354*/
105355SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(
105356 Vdbe *v, /* Parsing context */
105357 Table *pTab, /* The table containing the value */
105358 int iTabCur, /* The table cursor. Or the PK cursor for WITHOUT ROWID */
105359 int iCol, /* Index of the column to extract */
105360 int regOut /* Extract the value into this register */
105361){
105362 Column *pCol;
105363 assert( v!=0 );
105364 if( pTab==0 ){
105365 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iTabCur, p2: iCol, p3: regOut);
105366 return;
105367 }
105368 if( iCol<0 || iCol==pTab->iPKey ){
105369 sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: iTabCur, p2: regOut);
105370 }else{
105371 int op;
105372 int x;
105373 if( IsVirtual(pTab) ){
105374 op = OP_VColumn;
105375 x = iCol;
105376#ifndef SQLITE_OMIT_GENERATED_COLUMNS
105377 }else if( (pCol = &pTab->aCol[iCol])->colFlags & COLFLAG_VIRTUAL ){
105378 Parse *pParse = sqlite3VdbeParser(p: v);
105379 if( pCol->colFlags & COLFLAG_BUSY ){
105380 sqlite3ErrorMsg(pParse, zFormat: "generated column loop on \"%s\"",
105381 pCol->zCnName);
105382 }else{
105383 int savedSelfTab = pParse->iSelfTab;
105384 pCol->colFlags |= COLFLAG_BUSY;
105385 pParse->iSelfTab = iTabCur+1;
105386 sqlite3ExprCodeGeneratedColumn(pParse, pTab, pCol, regOut);
105387 pParse->iSelfTab = savedSelfTab;
105388 pCol->colFlags &= ~COLFLAG_BUSY;
105389 }
105390 return;
105391#endif
105392 }else if( !HasRowid(pTab) ){
105393 testcase( iCol!=sqlite3TableColumnToStorage(pTab, iCol) );
105394 x = sqlite3TableColumnToIndex(sqlite3PrimaryKeyIndex(pTab), iCol);
105395 op = OP_Column;
105396 }else{
105397 x = sqlite3TableColumnToStorage(pTab,iCol);
105398 testcase( x!=iCol );
105399 op = OP_Column;
105400 }
105401 sqlite3VdbeAddOp3(p: v, op, p1: iTabCur, p2: x, p3: regOut);
105402 sqlite3ColumnDefault(v, pTab, iCol, regOut);
105403 }
105404}
105405
105406/*
105407** Generate code that will extract the iColumn-th column from
105408** table pTab and store the column value in register iReg.
105409**
105410** There must be an open cursor to pTab in iTable when this routine
105411** is called. If iColumn<0 then code is generated that extracts the rowid.
105412*/
105413SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(
105414 Parse *pParse, /* Parsing and code generating context */
105415 Table *pTab, /* Description of the table we are reading from */
105416 int iColumn, /* Index of the table column */
105417 int iTable, /* The cursor pointing to the table */
105418 int iReg, /* Store results here */
105419 u8 p5 /* P5 value for OP_Column + FLAGS */
105420){
105421 assert( pParse->pVdbe!=0 );
105422 sqlite3ExprCodeGetColumnOfTable(v: pParse->pVdbe, pTab, iTabCur: iTable, iCol: iColumn, regOut: iReg);
105423 if( p5 ){
105424 VdbeOp *pOp = sqlite3VdbeGetOp(p: pParse->pVdbe,addr: -1);
105425 if( pOp->opcode==OP_Column ) pOp->p5 = p5;
105426 }
105427 return iReg;
105428}
105429
105430/*
105431** Generate code to move content from registers iFrom...iFrom+nReg-1
105432** over to iTo..iTo+nReg-1.
105433*/
105434SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
105435 sqlite3VdbeAddOp3(p: pParse->pVdbe, OP_Move, p1: iFrom, p2: iTo, p3: nReg);
105436}
105437
105438/*
105439** Convert a scalar expression node to a TK_REGISTER referencing
105440** register iReg. The caller must ensure that iReg already contains
105441** the correct value for the expression.
105442*/
105443static void exprToRegister(Expr *pExpr, int iReg){
105444 Expr *p = sqlite3ExprSkipCollateAndLikely(pExpr);
105445 if( NEVER(p==0) ) return;
105446 p->op2 = p->op;
105447 p->op = TK_REGISTER;
105448 p->iTable = iReg;
105449 ExprClearProperty(p, EP_Skip);
105450}
105451
105452/*
105453** Evaluate an expression (either a vector or a scalar expression) and store
105454** the result in continguous temporary registers. Return the index of
105455** the first register used to store the result.
105456**
105457** If the returned result register is a temporary scalar, then also write
105458** that register number into *piFreeable. If the returned result register
105459** is not a temporary or if the expression is a vector set *piFreeable
105460** to 0.
105461*/
105462static int exprCodeVector(Parse *pParse, Expr *p, int *piFreeable){
105463 int iResult;
105464 int nResult = sqlite3ExprVectorSize(pExpr: p);
105465 if( nResult==1 ){
105466 iResult = sqlite3ExprCodeTemp(pParse, p, piFreeable);
105467 }else{
105468 *piFreeable = 0;
105469 if( p->op==TK_SELECT ){
105470#if SQLITE_OMIT_SUBQUERY
105471 iResult = 0;
105472#else
105473 iResult = sqlite3CodeSubselect(pParse, pExpr: p);
105474#endif
105475 }else{
105476 int i;
105477 iResult = pParse->nMem+1;
105478 pParse->nMem += nResult;
105479 assert( ExprUseXList(p) );
105480 for(i=0; i<nResult; i++){
105481 sqlite3ExprCodeFactorable(pParse, p->x.pList->a[i].pExpr, i+iResult);
105482 }
105483 }
105484 }
105485 return iResult;
105486}
105487
105488/*
105489** If the last opcode is a OP_Copy, then set the do-not-merge flag (p5)
105490** so that a subsequent copy will not be merged into this one.
105491*/
105492static void setDoNotMergeFlagOnCopy(Vdbe *v){
105493 if( sqlite3VdbeGetOp(p: v, addr: -1)->opcode==OP_Copy ){
105494 sqlite3VdbeChangeP5(p: v, p5: 1); /* Tag trailing OP_Copy as not mergable */
105495 }
105496}
105497
105498/*
105499** Generate code to implement special SQL functions that are implemented
105500** in-line rather than by using the usual callbacks.
105501*/
105502static int exprCodeInlineFunction(
105503 Parse *pParse, /* Parsing context */
105504 ExprList *pFarg, /* List of function arguments */
105505 int iFuncId, /* Function ID. One of the INTFUNC_... values */
105506 int target /* Store function result in this register */
105507){
105508 int nFarg;
105509 Vdbe *v = pParse->pVdbe;
105510 assert( v!=0 );
105511 assert( pFarg!=0 );
105512 nFarg = pFarg->nExpr;
105513 assert( nFarg>0 ); /* All in-line functions have at least one argument */
105514 switch( iFuncId ){
105515 case INLINEFUNC_coalesce: {
105516 /* Attempt a direct implementation of the built-in COALESCE() and
105517 ** IFNULL() functions. This avoids unnecessary evaluation of
105518 ** arguments past the first non-NULL argument.
105519 */
105520 int endCoalesce = sqlite3VdbeMakeLabel(pParse);
105521 int i;
105522 assert( nFarg>=2 );
105523 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
105524 for(i=1; i<nFarg; i++){
105525 sqlite3VdbeAddOp2(p: v, OP_NotNull, p1: target, p2: endCoalesce);
105526 VdbeCoverage(v);
105527 sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
105528 }
105529 setDoNotMergeFlagOnCopy(v);
105530 sqlite3VdbeResolveLabel(v, x: endCoalesce);
105531 break;
105532 }
105533 case INLINEFUNC_iif: {
105534 Expr caseExpr;
105535 memset(s: &caseExpr, c: 0, n: sizeof(caseExpr));
105536 caseExpr.op = TK_CASE;
105537 caseExpr.x.pList = pFarg;
105538 return sqlite3ExprCodeTarget(pParse, &caseExpr, target);
105539 }
105540
105541 default: {
105542 /* The UNLIKELY() function is a no-op. The result is the value
105543 ** of the first argument.
105544 */
105545 assert( nFarg==1 || nFarg==2 );
105546 target = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
105547 break;
105548 }
105549
105550 /***********************************************************************
105551 ** Test-only SQL functions that are only usable if enabled
105552 ** via SQLITE_TESTCTRL_INTERNAL_FUNCTIONS
105553 */
105554#if !defined(SQLITE_UNTESTABLE)
105555 case INLINEFUNC_expr_compare: {
105556 /* Compare two expressions using sqlite3ExprCompare() */
105557 assert( nFarg==2 );
105558 sqlite3VdbeAddOp2(p: v, OP_Integer,
105559 p1: sqlite3ExprCompare(0,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
105560 p2: target);
105561 break;
105562 }
105563
105564 case INLINEFUNC_expr_implies_expr: {
105565 /* Compare two expressions using sqlite3ExprImpliesExpr() */
105566 assert( nFarg==2 );
105567 sqlite3VdbeAddOp2(p: v, OP_Integer,
105568 p1: sqlite3ExprImpliesExpr(pParse,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
105569 p2: target);
105570 break;
105571 }
105572
105573 case INLINEFUNC_implies_nonnull_row: {
105574 /* REsult of sqlite3ExprImpliesNonNullRow() */
105575 Expr *pA1;
105576 assert( nFarg==2 );
105577 pA1 = pFarg->a[1].pExpr;
105578 if( pA1->op==TK_COLUMN ){
105579 sqlite3VdbeAddOp2(p: v, OP_Integer,
105580 p1: sqlite3ExprImpliesNonNullRow(pFarg->a[0].pExpr,pA1->iTable),
105581 p2: target);
105582 }else{
105583 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: target);
105584 }
105585 break;
105586 }
105587
105588 case INLINEFUNC_affinity: {
105589 /* The AFFINITY() function evaluates to a string that describes
105590 ** the type affinity of the argument. This is used for testing of
105591 ** the SQLite type logic.
105592 */
105593 const char *azAff[] = { "blob", "text", "numeric", "integer", "real" };
105594 char aff;
105595 assert( nFarg==1 );
105596 aff = sqlite3ExprAffinity(pExpr: pFarg->a[0].pExpr);
105597 sqlite3VdbeLoadString(p: v, iDest: target,
105598 zStr: (aff<=SQLITE_AFF_NONE) ? "none" : azAff[aff-SQLITE_AFF_BLOB]);
105599 break;
105600 }
105601#endif /* !defined(SQLITE_UNTESTABLE) */
105602 }
105603 return target;
105604}
105605
105606
105607/*
105608** Generate code into the current Vdbe to evaluate the given
105609** expression. Attempt to store the results in register "target".
105610** Return the register where results are stored.
105611**
105612** With this routine, there is no guarantee that results will
105613** be stored in target. The result might be stored in some other
105614** register if it is convenient to do so. The calling function
105615** must check the return code and move the results to the desired
105616** register.
105617*/
105618SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
105619 Vdbe *v = pParse->pVdbe; /* The VM under construction */
105620 int op; /* The opcode being coded */
105621 int inReg = target; /* Results stored in register inReg */
105622 int regFree1 = 0; /* If non-zero free this temporary register */
105623 int regFree2 = 0; /* If non-zero free this temporary register */
105624 int r1, r2; /* Various register numbers */
105625 Expr tempX; /* Temporary expression node */
105626 int p5 = 0;
105627
105628 assert( target>0 && target<=pParse->nMem );
105629 assert( v!=0 );
105630
105631expr_code_doover:
105632 if( pExpr==0 ){
105633 op = TK_NULL;
105634 }else{
105635 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) );
105636 op = pExpr->op;
105637 }
105638 switch( op ){
105639 case TK_AGG_COLUMN: {
105640 AggInfo *pAggInfo = pExpr->pAggInfo;
105641 struct AggInfo_col *pCol;
105642 assert( pAggInfo!=0 );
105643 assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn );
105644 pCol = &pAggInfo->aCol[pExpr->iAgg];
105645 if( !pAggInfo->directMode ){
105646 assert( pCol->iMem>0 );
105647 return pCol->iMem;
105648 }else if( pAggInfo->useSortingIdx ){
105649 Table *pTab = pCol->pTab;
105650 sqlite3VdbeAddOp3(p: v, OP_Column, p1: pAggInfo->sortingIdxPTab,
105651 p2: pCol->iSorterColumn, p3: target);
105652 if( pCol->iColumn<0 ){
105653 VdbeComment((v,"%s.rowid",pTab->zName));
105654 }else{
105655 VdbeComment((v,"%s.%s",
105656 pTab->zName, pTab->aCol[pCol->iColumn].zCnName));
105657 if( pTab->aCol[pCol->iColumn].affinity==SQLITE_AFF_REAL ){
105658 sqlite3VdbeAddOp1(p: v, OP_RealAffinity, p1: target);
105659 }
105660 }
105661 return target;
105662 }
105663 /* Otherwise, fall thru into the TK_COLUMN case */
105664 /* no break */ deliberate_fall_through
105665 }
105666 case TK_COLUMN: {
105667 int iTab = pExpr->iTable;
105668 int iReg;
105669 if( ExprHasProperty(pExpr, EP_FixedCol) ){
105670 /* This COLUMN expression is really a constant due to WHERE clause
105671 ** constraints, and that constant is coded by the pExpr->pLeft
105672 ** expresssion. However, make sure the constant has the correct
105673 ** datatype by applying the Affinity of the table column to the
105674 ** constant.
105675 */
105676 int aff;
105677 iReg = sqlite3ExprCodeTarget(pParse, pExpr: pExpr->pLeft,target);
105678 assert( ExprUseYTab(pExpr) );
105679 if( pExpr->y.pTab ){
105680 aff = sqlite3TableColumnAffinity(pTab: pExpr->y.pTab, iCol: pExpr->iColumn);
105681 }else{
105682 aff = pExpr->affExpr;
105683 }
105684 if( aff>SQLITE_AFF_BLOB ){
105685 static const char zAff[] = "B\000C\000D\000E";
105686 assert( SQLITE_AFF_BLOB=='A' );
105687 assert( SQLITE_AFF_TEXT=='B' );
105688 sqlite3VdbeAddOp4(p: v, OP_Affinity, p1: iReg, p2: 1, p3: 0,
105689 zP4: &zAff[(aff-'B')*2], P4_STATIC);
105690 }
105691 return iReg;
105692 }
105693 if( iTab<0 ){
105694 if( pParse->iSelfTab<0 ){
105695 /* Other columns in the same row for CHECK constraints or
105696 ** generated columns or for inserting into partial index.
105697 ** The row is unpacked into registers beginning at
105698 ** 0-(pParse->iSelfTab). The rowid (if any) is in a register
105699 ** immediately prior to the first column.
105700 */
105701 Column *pCol;
105702 Table *pTab;
105703 int iSrc;
105704 int iCol = pExpr->iColumn;
105705 assert( ExprUseYTab(pExpr) );
105706 pTab = pExpr->y.pTab;
105707 assert( pTab!=0 );
105708 assert( iCol>=XN_ROWID );
105709 assert( iCol<pTab->nCol );
105710 if( iCol<0 ){
105711 return -1-pParse->iSelfTab;
105712 }
105713 pCol = pTab->aCol + iCol;
105714 testcase( iCol!=sqlite3TableColumnToStorage(pTab,iCol) );
105715 iSrc = sqlite3TableColumnToStorage(pTab, iCol) - pParse->iSelfTab;
105716#ifndef SQLITE_OMIT_GENERATED_COLUMNS
105717 if( pCol->colFlags & COLFLAG_GENERATED ){
105718 if( pCol->colFlags & COLFLAG_BUSY ){
105719 sqlite3ErrorMsg(pParse, zFormat: "generated column loop on \"%s\"",
105720 pCol->zCnName);
105721 return 0;
105722 }
105723 pCol->colFlags |= COLFLAG_BUSY;
105724 if( pCol->colFlags & COLFLAG_NOTAVAIL ){
105725 sqlite3ExprCodeGeneratedColumn(pParse, pTab, pCol, regOut: iSrc);
105726 }
105727 pCol->colFlags &= ~(COLFLAG_BUSY|COLFLAG_NOTAVAIL);
105728 return iSrc;
105729 }else
105730#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
105731 if( pCol->affinity==SQLITE_AFF_REAL ){
105732 sqlite3VdbeAddOp2(p: v, OP_SCopy, p1: iSrc, p2: target);
105733 sqlite3VdbeAddOp1(p: v, OP_RealAffinity, p1: target);
105734 return target;
105735 }else{
105736 return iSrc;
105737 }
105738 }else{
105739 /* Coding an expression that is part of an index where column names
105740 ** in the index refer to the table to which the index belongs */
105741 iTab = pParse->iSelfTab - 1;
105742 }
105743 }
105744 assert( ExprUseYTab(pExpr) );
105745 iReg = sqlite3ExprCodeGetColumn(pParse, pTab: pExpr->y.pTab,
105746 iColumn: pExpr->iColumn, iTable: iTab, iReg: target,
105747 p5: pExpr->op2);
105748 if( pExpr->y.pTab==0 && pExpr->affExpr==SQLITE_AFF_REAL ){
105749 sqlite3VdbeAddOp1(p: v, OP_RealAffinity, p1: iReg);
105750 }
105751 return iReg;
105752 }
105753 case TK_INTEGER: {
105754 codeInteger(pParse, pExpr, negFlag: 0, iMem: target);
105755 return target;
105756 }
105757 case TK_TRUEFALSE: {
105758 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: sqlite3ExprTruthValue(pExpr), p2: target);
105759 return target;
105760 }
105761#ifndef SQLITE_OMIT_FLOATING_POINT
105762 case TK_FLOAT: {
105763 assert( !ExprHasProperty(pExpr, EP_IntValue) );
105764 codeReal(v, z: pExpr->u.zToken, negateFlag: 0, iMem: target);
105765 return target;
105766 }
105767#endif
105768 case TK_STRING: {
105769 assert( !ExprHasProperty(pExpr, EP_IntValue) );
105770 sqlite3VdbeLoadString(p: v, iDest: target, zStr: pExpr->u.zToken);
105771 return target;
105772 }
105773 default: {
105774 /* Make NULL the default case so that if a bug causes an illegal
105775 ** Expr node to be passed into this function, it will be handled
105776 ** sanely and not crash. But keep the assert() to bring the problem
105777 ** to the attention of the developers. */
105778 assert( op==TK_NULL || op==TK_ERROR || pParse->db->mallocFailed );
105779 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: target);
105780 return target;
105781 }
105782#ifndef SQLITE_OMIT_BLOB_LITERAL
105783 case TK_BLOB: {
105784 int n;
105785 const char *z;
105786 char *zBlob;
105787 assert( !ExprHasProperty(pExpr, EP_IntValue) );
105788 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
105789 assert( pExpr->u.zToken[1]=='\'' );
105790 z = &pExpr->u.zToken[2];
105791 n = sqlite3Strlen30(z) - 1;
105792 assert( z[n]=='\'' );
105793 zBlob = sqlite3HexToBlob(db: sqlite3VdbeDb(v), z, n);
105794 sqlite3VdbeAddOp4(p: v, OP_Blob, p1: n/2, p2: target, p3: 0, zP4: zBlob, P4_DYNAMIC);
105795 return target;
105796 }
105797#endif
105798 case TK_VARIABLE: {
105799 assert( !ExprHasProperty(pExpr, EP_IntValue) );
105800 assert( pExpr->u.zToken!=0 );
105801 assert( pExpr->u.zToken[0]!=0 );
105802 sqlite3VdbeAddOp2(p: v, OP_Variable, p1: pExpr->iColumn, p2: target);
105803 if( pExpr->u.zToken[1]!=0 ){
105804 const char *z = sqlite3VListNumToName(pIn: pParse->pVList, iVal: pExpr->iColumn);
105805 assert( pExpr->u.zToken[0]=='?' || (z && !strcmp(pExpr->u.zToken, z)) );
105806 pParse->pVList[0] = 0; /* Indicate VList may no longer be enlarged */
105807 sqlite3VdbeAppendP4(p: v, pP4: (char*)z, P4_STATIC);
105808 }
105809 return target;
105810 }
105811 case TK_REGISTER: {
105812 return pExpr->iTable;
105813 }
105814#ifndef SQLITE_OMIT_CAST
105815 case TK_CAST: {
105816 /* Expressions of the form: CAST(pLeft AS token) */
105817 inReg = sqlite3ExprCodeTarget(pParse, pExpr: pExpr->pLeft, target);
105818 if( inReg!=target ){
105819 sqlite3VdbeAddOp2(p: v, OP_SCopy, p1: inReg, p2: target);
105820 inReg = target;
105821 }
105822 assert( !ExprHasProperty(pExpr, EP_IntValue) );
105823 sqlite3VdbeAddOp2(p: v, OP_Cast, p1: target,
105824 p2: sqlite3AffinityType(pExpr->u.zToken, 0));
105825 return inReg;
105826 }
105827#endif /* SQLITE_OMIT_CAST */
105828 case TK_IS:
105829 case TK_ISNOT:
105830 op = (op==TK_IS) ? TK_EQ : TK_NE;
105831 p5 = SQLITE_NULLEQ;
105832 /* fall-through */
105833 case TK_LT:
105834 case TK_LE:
105835 case TK_GT:
105836 case TK_GE:
105837 case TK_NE:
105838 case TK_EQ: {
105839 Expr *pLeft = pExpr->pLeft;
105840 if( sqlite3ExprIsVector(pExpr: pLeft) ){
105841 codeVectorCompare(pParse, pExpr, dest: target, op, p5);
105842 }else{
105843 r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
105844 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
105845 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 1, p2: inReg);
105846 codeCompare(pParse, pLeft, pRight: pExpr->pRight, opcode: op, in1: r1, in2: r2,
105847 dest: sqlite3VdbeCurrentAddr(p: v)+2, jumpIfNull: p5,
105848 ExprHasProperty(pExpr,EP_Commuted));
105849 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
105850 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
105851 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
105852 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
105853 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
105854 assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
105855 if( p5==SQLITE_NULLEQ ){
105856 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: inReg);
105857 }else{
105858 sqlite3VdbeAddOp3(p: v, OP_ZeroOrNull, p1: r1, p2: inReg, p3: r2);
105859 }
105860 testcase( regFree1==0 );
105861 testcase( regFree2==0 );
105862 }
105863 break;
105864 }
105865 case TK_AND:
105866 case TK_OR:
105867 case TK_PLUS:
105868 case TK_STAR:
105869 case TK_MINUS:
105870 case TK_REM:
105871 case TK_BITAND:
105872 case TK_BITOR:
105873 case TK_SLASH:
105874 case TK_LSHIFT:
105875 case TK_RSHIFT:
105876 case TK_CONCAT: {
105877 assert( TK_AND==OP_And ); testcase( op==TK_AND );
105878 assert( TK_OR==OP_Or ); testcase( op==TK_OR );
105879 assert( TK_PLUS==OP_Add ); testcase( op==TK_PLUS );
105880 assert( TK_MINUS==OP_Subtract ); testcase( op==TK_MINUS );
105881 assert( TK_REM==OP_Remainder ); testcase( op==TK_REM );
105882 assert( TK_BITAND==OP_BitAnd ); testcase( op==TK_BITAND );
105883 assert( TK_BITOR==OP_BitOr ); testcase( op==TK_BITOR );
105884 assert( TK_SLASH==OP_Divide ); testcase( op==TK_SLASH );
105885 assert( TK_LSHIFT==OP_ShiftLeft ); testcase( op==TK_LSHIFT );
105886 assert( TK_RSHIFT==OP_ShiftRight ); testcase( op==TK_RSHIFT );
105887 assert( TK_CONCAT==OP_Concat ); testcase( op==TK_CONCAT );
105888 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
105889 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
105890 sqlite3VdbeAddOp3(p: v, op, p1: r2, p2: r1, p3: target);
105891 testcase( regFree1==0 );
105892 testcase( regFree2==0 );
105893 break;
105894 }
105895 case TK_UMINUS: {
105896 Expr *pLeft = pExpr->pLeft;
105897 assert( pLeft );
105898 if( pLeft->op==TK_INTEGER ){
105899 codeInteger(pParse, pExpr: pLeft, negFlag: 1, iMem: target);
105900 return target;
105901#ifndef SQLITE_OMIT_FLOATING_POINT
105902 }else if( pLeft->op==TK_FLOAT ){
105903 assert( !ExprHasProperty(pExpr, EP_IntValue) );
105904 codeReal(v, z: pLeft->u.zToken, negateFlag: 1, iMem: target);
105905 return target;
105906#endif
105907 }else{
105908 tempX.op = TK_INTEGER;
105909 tempX.flags = EP_IntValue|EP_TokenOnly;
105910 tempX.u.iValue = 0;
105911 ExprClearVVAProperties(&tempX);
105912 r1 = sqlite3ExprCodeTemp(pParse, &tempX, &regFree1);
105913 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
105914 sqlite3VdbeAddOp3(p: v, OP_Subtract, p1: r2, p2: r1, p3: target);
105915 testcase( regFree2==0 );
105916 }
105917 break;
105918 }
105919 case TK_BITNOT:
105920 case TK_NOT: {
105921 assert( TK_BITNOT==OP_BitNot ); testcase( op==TK_BITNOT );
105922 assert( TK_NOT==OP_Not ); testcase( op==TK_NOT );
105923 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
105924 testcase( regFree1==0 );
105925 sqlite3VdbeAddOp2(p: v, op, p1: r1, p2: inReg);
105926 break;
105927 }
105928 case TK_TRUTH: {
105929 int isTrue; /* IS TRUE or IS NOT TRUE */
105930 int bNormal; /* IS TRUE or IS FALSE */
105931 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
105932 testcase( regFree1==0 );
105933 isTrue = sqlite3ExprTruthValue(pExpr: pExpr->pRight);
105934 bNormal = pExpr->op2==TK_IS;
105935 testcase( isTrue && bNormal);
105936 testcase( !isTrue && bNormal);
105937 sqlite3VdbeAddOp4Int(p: v, OP_IsTrue, p1: r1, p2: inReg, p3: !isTrue, p4: isTrue ^ bNormal);
105938 break;
105939 }
105940 case TK_ISNULL:
105941 case TK_NOTNULL: {
105942 int addr;
105943 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL );
105944 assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
105945 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 1, p2: target);
105946 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
105947 testcase( regFree1==0 );
105948 addr = sqlite3VdbeAddOp1(p: v, op, p1: r1);
105949 VdbeCoverageIf(v, op==TK_ISNULL);
105950 VdbeCoverageIf(v, op==TK_NOTNULL);
105951 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: target);
105952 sqlite3VdbeJumpHere(p: v, addr);
105953 break;
105954 }
105955 case TK_AGG_FUNCTION: {
105956 AggInfo *pInfo = pExpr->pAggInfo;
105957 if( pInfo==0
105958 || NEVER(pExpr->iAgg<0)
105959 || NEVER(pExpr->iAgg>=pInfo->nFunc)
105960 ){
105961 assert( !ExprHasProperty(pExpr, EP_IntValue) );
105962 sqlite3ErrorMsg(pParse, zFormat: "misuse of aggregate: %s()", pExpr->u.zToken);
105963 }else{
105964 return pInfo->aFunc[pExpr->iAgg].iMem;
105965 }
105966 break;
105967 }
105968 case TK_FUNCTION: {
105969 ExprList *pFarg; /* List of function arguments */
105970 int nFarg; /* Number of function arguments */
105971 FuncDef *pDef; /* The function definition object */
105972 const char *zId; /* The function name */
105973 u32 constMask = 0; /* Mask of function arguments that are constant */
105974 int i; /* Loop counter */
105975 sqlite3 *db = pParse->db; /* The database connection */
105976 u8 enc = ENC(db); /* The text encoding used by this database */
105977 CollSeq *pColl = 0; /* A collating sequence */
105978
105979#ifndef SQLITE_OMIT_WINDOWFUNC
105980 if( ExprHasProperty(pExpr, EP_WinFunc) ){
105981 return pExpr->y.pWin->regResult;
105982 }
105983#endif
105984
105985 if( ConstFactorOk(pParse) && sqlite3ExprIsConstantNotJoin(p: pExpr) ){
105986 /* SQL functions can be expensive. So try to avoid running them
105987 ** multiple times if we know they always give the same result */
105988 return sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
105989 }
105990 assert( !ExprHasProperty(pExpr, EP_TokenOnly) );
105991 assert( ExprUseXList(pExpr) );
105992 pFarg = pExpr->x.pList;
105993 nFarg = pFarg ? pFarg->nExpr : 0;
105994 assert( !ExprHasProperty(pExpr, EP_IntValue) );
105995 zId = pExpr->u.zToken;
105996 pDef = sqlite3FindFunction(db, zId, nFarg, enc, 0);
105997#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
105998 if( pDef==0 && pParse->explain ){
105999 pDef = sqlite3FindFunction(db, "unknown", nFarg, enc, 0);
106000 }
106001#endif
106002 if( pDef==0 || pDef->xFinalize!=0 ){
106003 sqlite3ErrorMsg(pParse, zFormat: "unknown function: %s()", zId);
106004 break;
106005 }
106006 if( pDef->funcFlags & SQLITE_FUNC_INLINE ){
106007 assert( (pDef->funcFlags & SQLITE_FUNC_UNSAFE)==0 );
106008 assert( (pDef->funcFlags & SQLITE_FUNC_DIRECT)==0 );
106009 return exprCodeInlineFunction(pParse, pFarg,
106010 SQLITE_PTR_TO_INT(pDef->pUserData), target);
106011 }else if( pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE) ){
106012 sqlite3ExprFunctionUsable(pParse, pExpr, pDef);
106013 }
106014
106015 for(i=0; i<nFarg; i++){
106016 if( i<32 && sqlite3ExprIsConstant(p: pFarg->a[i].pExpr) ){
106017 testcase( i==31 );
106018 constMask |= MASKBIT32(i);
106019 }
106020 if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
106021 pColl = sqlite3ExprCollSeq(pParse, pExpr: pFarg->a[i].pExpr);
106022 }
106023 }
106024 if( pFarg ){
106025 if( constMask ){
106026 r1 = pParse->nMem+1;
106027 pParse->nMem += nFarg;
106028 }else{
106029 r1 = sqlite3GetTempRange(pParse, nFarg);
106030 }
106031
106032 /* For length() and typeof() functions with a column argument,
106033 ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG
106034 ** or OPFLAG_TYPEOFARG respectively, to avoid unnecessary data
106035 ** loading.
106036 */
106037 if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){
106038 u8 exprOp;
106039 assert( nFarg==1 );
106040 assert( pFarg->a[0].pExpr!=0 );
106041 exprOp = pFarg->a[0].pExpr->op;
106042 if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){
106043 assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG );
106044 assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG );
106045 testcase( pDef->funcFlags & OPFLAG_LENGTHARG );
106046 pFarg->a[0].pExpr->op2 =
106047 pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG);
106048 }
106049 }
106050
106051 sqlite3ExprCodeExprList(pParse, pFarg, r1, 0,
106052 SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR);
106053 }else{
106054 r1 = 0;
106055 }
106056#ifndef SQLITE_OMIT_VIRTUALTABLE
106057 /* Possibly overload the function if the first argument is
106058 ** a virtual table column.
106059 **
106060 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
106061 ** second argument, not the first, as the argument to test to
106062 ** see if it is a column in a virtual table. This is done because
106063 ** the left operand of infix functions (the operand we want to
106064 ** control overloading) ends up as the second argument to the
106065 ** function. The expression "A glob B" is equivalent to
106066 ** "glob(B,A). We want to use the A in "A glob B" to test
106067 ** for function overloading. But we use the B term in "glob(B,A)".
106068 */
106069 if( nFarg>=2 && ExprHasProperty(pExpr, EP_InfixFunc) ){
106070 pDef = sqlite3VtabOverloadFunction(db, pDef, nArg: nFarg, pFarg->a[1].pExpr);
106071 }else if( nFarg>0 ){
106072 pDef = sqlite3VtabOverloadFunction(db, pDef, nArg: nFarg, pFarg->a[0].pExpr);
106073 }
106074#endif
106075 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
106076 if( !pColl ) pColl = db->pDfltColl;
106077 sqlite3VdbeAddOp4(p: v, OP_CollSeq, p1: 0, p2: 0, p3: 0, zP4: (char *)pColl, P4_COLLSEQ);
106078 }
106079#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
106080 if( (pDef->funcFlags & SQLITE_FUNC_OFFSET)!=0 && ALWAYS(pFarg!=0) ){
106081 Expr *pArg = pFarg->a[0].pExpr;
106082 if( pArg->op==TK_COLUMN ){
106083 sqlite3VdbeAddOp3(v, OP_Offset, pArg->iTable, pArg->iColumn, target);
106084 }else{
106085 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
106086 }
106087 }else
106088#endif
106089 {
106090 sqlite3VdbeAddFunctionCall(pParse, p1: constMask, p2: r1, p3: target, nArg: nFarg,
106091 pFunc: pDef, eCallCtx: pExpr->op2);
106092 }
106093 if( nFarg ){
106094 if( constMask==0 ){
106095 sqlite3ReleaseTempRange(pParse, r1, nFarg);
106096 }else{
106097 sqlite3VdbeReleaseRegisters(pParse, r1, nFarg, constMask, 1);
106098 }
106099 }
106100 return target;
106101 }
106102#ifndef SQLITE_OMIT_SUBQUERY
106103 case TK_EXISTS:
106104 case TK_SELECT: {
106105 int nCol;
106106 testcase( op==TK_EXISTS );
106107 testcase( op==TK_SELECT );
106108 if( pParse->db->mallocFailed ){
106109 return 0;
106110 }else if( op==TK_SELECT
106111 && ALWAYS( ExprUseXSelect(pExpr) )
106112 && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1
106113 ){
106114 sqlite3SubselectError(pParse, nActual: nCol, nExpect: 1);
106115 }else{
106116 return sqlite3CodeSubselect(pParse, pExpr);
106117 }
106118 break;
106119 }
106120 case TK_SELECT_COLUMN: {
106121 int n;
106122 if( pExpr->pLeft->iTable==0 ){
106123 pExpr->pLeft->iTable = sqlite3CodeSubselect(pParse, pExpr: pExpr->pLeft);
106124 }
106125 assert( pExpr->pLeft->op==TK_SELECT || pExpr->pLeft->op==TK_ERROR );
106126 n = sqlite3ExprVectorSize(pExpr: pExpr->pLeft);
106127 if( pExpr->iTable!=n ){
106128 sqlite3ErrorMsg(pParse, zFormat: "%d columns assigned %d values",
106129 pExpr->iTable, n);
106130 }
106131 return pExpr->pLeft->iTable + pExpr->iColumn;
106132 }
106133 case TK_IN: {
106134 int destIfFalse = sqlite3VdbeMakeLabel(pParse);
106135 int destIfNull = sqlite3VdbeMakeLabel(pParse);
106136 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: target);
106137 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
106138 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 1, p2: target);
106139 sqlite3VdbeResolveLabel(v, x: destIfFalse);
106140 sqlite3VdbeAddOp2(p: v, OP_AddImm, p1: target, p2: 0);
106141 sqlite3VdbeResolveLabel(v, x: destIfNull);
106142 return target;
106143 }
106144#endif /* SQLITE_OMIT_SUBQUERY */
106145
106146
106147 /*
106148 ** x BETWEEN y AND z
106149 **
106150 ** This is equivalent to
106151 **
106152 ** x>=y AND x<=z
106153 **
106154 ** X is stored in pExpr->pLeft.
106155 ** Y is stored in pExpr->pList->a[0].pExpr.
106156 ** Z is stored in pExpr->pList->a[1].pExpr.
106157 */
106158 case TK_BETWEEN: {
106159 exprCodeBetween(pParse, pExpr, target, 0, 0);
106160 return target;
106161 }
106162 case TK_SPAN:
106163 case TK_COLLATE:
106164 case TK_UPLUS: {
106165 pExpr = pExpr->pLeft;
106166 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. OSSFuzz. */
106167 }
106168
106169 case TK_TRIGGER: {
106170 /* If the opcode is TK_TRIGGER, then the expression is a reference
106171 ** to a column in the new.* or old.* pseudo-tables available to
106172 ** trigger programs. In this case Expr.iTable is set to 1 for the
106173 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
106174 ** is set to the column of the pseudo-table to read, or to -1 to
106175 ** read the rowid field.
106176 **
106177 ** The expression is implemented using an OP_Param opcode. The p1
106178 ** parameter is set to 0 for an old.rowid reference, or to (i+1)
106179 ** to reference another column of the old.* pseudo-table, where
106180 ** i is the index of the column. For a new.rowid reference, p1 is
106181 ** set to (n+1), where n is the number of columns in each pseudo-table.
106182 ** For a reference to any other column in the new.* pseudo-table, p1
106183 ** is set to (n+2+i), where n and i are as defined previously. For
106184 ** example, if the table on which triggers are being fired is
106185 ** declared as:
106186 **
106187 ** CREATE TABLE t1(a, b);
106188 **
106189 ** Then p1 is interpreted as follows:
106190 **
106191 ** p1==0 -> old.rowid p1==3 -> new.rowid
106192 ** p1==1 -> old.a p1==4 -> new.a
106193 ** p1==2 -> old.b p1==5 -> new.b
106194 */
106195 Table *pTab;
106196 int iCol;
106197 int p1;
106198
106199 assert( ExprUseYTab(pExpr) );
106200 pTab = pExpr->y.pTab;
106201 iCol = pExpr->iColumn;
106202 p1 = pExpr->iTable * (pTab->nCol+1) + 1
106203 + sqlite3TableColumnToStorage(pTab, iCol);
106204
106205 assert( pExpr->iTable==0 || pExpr->iTable==1 );
106206 assert( iCol>=-1 && iCol<pTab->nCol );
106207 assert( pTab->iPKey<0 || iCol!=pTab->iPKey );
106208 assert( p1>=0 && p1<(pTab->nCol*2+2) );
106209
106210 sqlite3VdbeAddOp2(p: v, OP_Param, p1, p2: target);
106211 VdbeComment((v, "r[%d]=%s.%s", target,
106212 (pExpr->iTable ? "new" : "old"),
106213 (pExpr->iColumn<0 ? "rowid" : pExpr->y.pTab->aCol[iCol].zCnName)
106214 ));
106215
106216#ifndef SQLITE_OMIT_FLOATING_POINT
106217 /* If the column has REAL affinity, it may currently be stored as an
106218 ** integer. Use OP_RealAffinity to make sure it is really real.
106219 **
106220 ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
106221 ** floating point when extracting it from the record. */
106222 if( iCol>=0 && pTab->aCol[iCol].affinity==SQLITE_AFF_REAL ){
106223 sqlite3VdbeAddOp1(p: v, OP_RealAffinity, p1: target);
106224 }
106225#endif
106226 break;
106227 }
106228
106229 case TK_VECTOR: {
106230 sqlite3ErrorMsg(pParse, zFormat: "row value misused");
106231 break;
106232 }
106233
106234 /* TK_IF_NULL_ROW Expr nodes are inserted ahead of expressions
106235 ** that derive from the right-hand table of a LEFT JOIN. The
106236 ** Expr.iTable value is the table number for the right-hand table.
106237 ** The expression is only evaluated if that table is not currently
106238 ** on a LEFT JOIN NULL row.
106239 */
106240 case TK_IF_NULL_ROW: {
106241 int addrINR;
106242 u8 okConstFactor = pParse->okConstFactor;
106243 addrINR = sqlite3VdbeAddOp1(p: v, OP_IfNullRow, p1: pExpr->iTable);
106244 /* Temporarily disable factoring of constant expressions, since
106245 ** even though expressions may appear to be constant, they are not
106246 ** really constant because they originate from the right-hand side
106247 ** of a LEFT JOIN. */
106248 pParse->okConstFactor = 0;
106249 inReg = sqlite3ExprCodeTarget(pParse, pExpr: pExpr->pLeft, target);
106250 pParse->okConstFactor = okConstFactor;
106251 sqlite3VdbeJumpHere(p: v, addr: addrINR);
106252 sqlite3VdbeChangeP3(p: v, addr: addrINR, val: inReg);
106253 break;
106254 }
106255
106256 /*
106257 ** Form A:
106258 ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
106259 **
106260 ** Form B:
106261 ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
106262 **
106263 ** Form A is can be transformed into the equivalent form B as follows:
106264 ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
106265 ** WHEN x=eN THEN rN ELSE y END
106266 **
106267 ** X (if it exists) is in pExpr->pLeft.
106268 ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
106269 ** odd. The Y is also optional. If the number of elements in x.pList
106270 ** is even, then Y is omitted and the "otherwise" result is NULL.
106271 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
106272 **
106273 ** The result of the expression is the Ri for the first matching Ei,
106274 ** or if there is no matching Ei, the ELSE term Y, or if there is
106275 ** no ELSE term, NULL.
106276 */
106277 case TK_CASE: {
106278 int endLabel; /* GOTO label for end of CASE stmt */
106279 int nextCase; /* GOTO label for next WHEN clause */
106280 int nExpr; /* 2x number of WHEN terms */
106281 int i; /* Loop counter */
106282 ExprList *pEList; /* List of WHEN terms */
106283 struct ExprList_item *aListelem; /* Array of WHEN terms */
106284 Expr opCompare; /* The X==Ei expression */
106285 Expr *pX; /* The X expression */
106286 Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */
106287 Expr *pDel = 0;
106288 sqlite3 *db = pParse->db;
106289
106290 assert( ExprUseXList(pExpr) && pExpr->x.pList!=0 );
106291 assert(pExpr->x.pList->nExpr > 0);
106292 pEList = pExpr->x.pList;
106293 aListelem = pEList->a;
106294 nExpr = pEList->nExpr;
106295 endLabel = sqlite3VdbeMakeLabel(pParse);
106296 if( (pX = pExpr->pLeft)!=0 ){
106297 pDel = sqlite3ExprDup(db, p: pX, flags: 0);
106298 if( db->mallocFailed ){
106299 sqlite3ExprDelete(db, p: pDel);
106300 break;
106301 }
106302 testcase( pX->op==TK_COLUMN );
106303 exprToRegister(pExpr: pDel, iReg: exprCodeVector(pParse, p: pDel, piFreeable: &regFree1));
106304 testcase( regFree1==0 );
106305 memset(s: &opCompare, c: 0, n: sizeof(opCompare));
106306 opCompare.op = TK_EQ;
106307 opCompare.pLeft = pDel;
106308 pTest = &opCompare;
106309 /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
106310 ** The value in regFree1 might get SCopy-ed into the file result.
106311 ** So make sure that the regFree1 register is not reused for other
106312 ** purposes and possibly overwritten. */
106313 regFree1 = 0;
106314 }
106315 for(i=0; i<nExpr-1; i=i+2){
106316 if( pX ){
106317 assert( pTest!=0 );
106318 opCompare.pRight = aListelem[i].pExpr;
106319 }else{
106320 pTest = aListelem[i].pExpr;
106321 }
106322 nextCase = sqlite3VdbeMakeLabel(pParse);
106323 testcase( pTest->op==TK_COLUMN );
106324 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
106325 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
106326 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
106327 sqlite3VdbeGoto(p: v, iDest: endLabel);
106328 sqlite3VdbeResolveLabel(v, x: nextCase);
106329 }
106330 if( (nExpr&1)!=0 ){
106331 sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
106332 }else{
106333 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: target);
106334 }
106335 sqlite3ExprDelete(db, p: pDel);
106336 setDoNotMergeFlagOnCopy(v);
106337 sqlite3VdbeResolveLabel(v, x: endLabel);
106338 break;
106339 }
106340#ifndef SQLITE_OMIT_TRIGGER
106341 case TK_RAISE: {
106342 assert( pExpr->affExpr==OE_Rollback
106343 || pExpr->affExpr==OE_Abort
106344 || pExpr->affExpr==OE_Fail
106345 || pExpr->affExpr==OE_Ignore
106346 );
106347 if( !pParse->pTriggerTab && !pParse->nested ){
106348 sqlite3ErrorMsg(pParse,
106349 zFormat: "RAISE() may only be used within a trigger-program");
106350 return 0;
106351 }
106352 if( pExpr->affExpr==OE_Abort ){
106353 sqlite3MayAbort(pParse);
106354 }
106355 assert( !ExprHasProperty(pExpr, EP_IntValue) );
106356 if( pExpr->affExpr==OE_Ignore ){
106357 sqlite3VdbeAddOp4(
106358 p: v, OP_Halt, SQLITE_OK, OE_Ignore, p3: 0, zP4: pExpr->u.zToken,p4type: 0);
106359 VdbeCoverage(v);
106360 }else{
106361 sqlite3HaltConstraint(pParse,
106362 pParse->pTriggerTab ? SQLITE_CONSTRAINT_TRIGGER : SQLITE_ERROR,
106363 pExpr->affExpr, pExpr->u.zToken, 0, 0);
106364 }
106365
106366 break;
106367 }
106368#endif
106369 }
106370 sqlite3ReleaseTempReg(pParse, regFree1);
106371 sqlite3ReleaseTempReg(pParse, regFree2);
106372 return inReg;
106373}
106374
106375/*
106376** Generate code that will evaluate expression pExpr just one time
106377** per prepared statement execution.
106378**
106379** If the expression uses functions (that might throw an exception) then
106380** guard them with an OP_Once opcode to ensure that the code is only executed
106381** once. If no functions are involved, then factor the code out and put it at
106382** the end of the prepared statement in the initialization section.
106383**
106384** If regDest>=0 then the result is always stored in that register and the
106385** result is not reusable. If regDest<0 then this routine is free to
106386** store the value whereever it wants. The register where the expression
106387** is stored is returned. When regDest<0, two identical expressions might
106388** code to the same register, if they do not contain function calls and hence
106389** are factored out into the initialization section at the end of the
106390** prepared statement.
106391*/
106392SQLITE_PRIVATE int sqlite3ExprCodeRunJustOnce(
106393 Parse *pParse, /* Parsing context */
106394 Expr *pExpr, /* The expression to code when the VDBE initializes */
106395 int regDest /* Store the value in this register */
106396){
106397 ExprList *p;
106398 assert( ConstFactorOk(pParse) );
106399 p = pParse->pConstExpr;
106400 if( regDest<0 && p ){
106401 struct ExprList_item *pItem;
106402 int i;
106403 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
106404 if( pItem->reusable && sqlite3ExprCompare(0,pItem->pExpr,pExpr,-1)==0 ){
106405 return pItem->u.iConstExprReg;
106406 }
106407 }
106408 }
106409 pExpr = sqlite3ExprDup(db: pParse->db, p: pExpr, flags: 0);
106410 if( pExpr!=0 && ExprHasProperty(pExpr, EP_HasFunc) ){
106411 Vdbe *v = pParse->pVdbe;
106412 int addr;
106413 assert( v );
106414 addr = sqlite3VdbeAddOp0(p: v, OP_Once); VdbeCoverage(v);
106415 pParse->okConstFactor = 0;
106416 if( !pParse->db->mallocFailed ){
106417 if( regDest<0 ) regDest = ++pParse->nMem;
106418 sqlite3ExprCode(pParse, pExpr, regDest);
106419 }
106420 pParse->okConstFactor = 1;
106421 sqlite3ExprDelete(db: pParse->db, p: pExpr);
106422 sqlite3VdbeJumpHere(p: v, addr);
106423 }else{
106424 p = sqlite3ExprListAppend(pParse, pList: p, pExpr);
106425 if( p ){
106426 struct ExprList_item *pItem = &p->a[p->nExpr-1];
106427 pItem->reusable = regDest<0;
106428 if( regDest<0 ) regDest = ++pParse->nMem;
106429 pItem->u.iConstExprReg = regDest;
106430 }
106431 pParse->pConstExpr = p;
106432 }
106433 return regDest;
106434}
106435
106436/*
106437** Generate code to evaluate an expression and store the results
106438** into a register. Return the register number where the results
106439** are stored.
106440**
106441** If the register is a temporary register that can be deallocated,
106442** then write its number into *pReg. If the result register is not
106443** a temporary, then set *pReg to zero.
106444**
106445** If pExpr is a constant, then this routine might generate this
106446** code to fill the register in the initialization section of the
106447** VDBE program, in order to factor it out of the evaluation loop.
106448*/
106449SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
106450 int r2;
106451 pExpr = sqlite3ExprSkipCollateAndLikely(pExpr);
106452 if( ConstFactorOk(pParse)
106453 && ALWAYS(pExpr!=0)
106454 && pExpr->op!=TK_REGISTER
106455 && sqlite3ExprIsConstantNotJoin(p: pExpr)
106456 ){
106457 *pReg = 0;
106458 r2 = sqlite3ExprCodeRunJustOnce(pParse, pExpr, regDest: -1);
106459 }else{
106460 int r1 = sqlite3GetTempReg(pParse);
106461 r2 = sqlite3ExprCodeTarget(pParse, pExpr, target: r1);
106462 if( r2==r1 ){
106463 *pReg = r1;
106464 }else{
106465 sqlite3ReleaseTempReg(pParse, r1);
106466 *pReg = 0;
106467 }
106468 }
106469 return r2;
106470}
106471
106472/*
106473** Generate code that will evaluate expression pExpr and store the
106474** results in register target. The results are guaranteed to appear
106475** in register target.
106476*/
106477SQLITE_PRIVATE void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
106478 int inReg;
106479
106480 assert( pExpr==0 || !ExprHasVVAProperty(pExpr,EP_Immutable) );
106481 assert( target>0 && target<=pParse->nMem );
106482 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed );
106483 if( pParse->pVdbe==0 ) return;
106484 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
106485 if( inReg!=target ){
106486 u8 op;
106487 if( ALWAYS(pExpr) && ExprHasProperty(pExpr,EP_Subquery) ){
106488 op = OP_Copy;
106489 }else{
106490 op = OP_SCopy;
106491 }
106492 sqlite3VdbeAddOp2(p: pParse->pVdbe, op, p1: inReg, p2: target);
106493 }
106494}
106495
106496/*
106497** Make a transient copy of expression pExpr and then code it using
106498** sqlite3ExprCode(). This routine works just like sqlite3ExprCode()
106499** except that the input expression is guaranteed to be unchanged.
106500*/
106501SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse *pParse, Expr *pExpr, int target){
106502 sqlite3 *db = pParse->db;
106503 pExpr = sqlite3ExprDup(db, p: pExpr, flags: 0);
106504 if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target);
106505 sqlite3ExprDelete(db, p: pExpr);
106506}
106507
106508/*
106509** Generate code that will evaluate expression pExpr and store the
106510** results in register target. The results are guaranteed to appear
106511** in register target. If the expression is constant, then this routine
106512** might choose to code the expression at initialization time.
106513*/
106514SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
106515 if( pParse->okConstFactor && sqlite3ExprIsConstantNotJoin(p: pExpr) ){
106516 sqlite3ExprCodeRunJustOnce(pParse, pExpr, regDest: target);
106517 }else{
106518 sqlite3ExprCodeCopy(pParse, pExpr, target);
106519 }
106520}
106521
106522/*
106523** Generate code that pushes the value of every element of the given
106524** expression list into a sequence of registers beginning at target.
106525**
106526** Return the number of elements evaluated. The number returned will
106527** usually be pList->nExpr but might be reduced if SQLITE_ECEL_OMITREF
106528** is defined.
106529**
106530** The SQLITE_ECEL_DUP flag prevents the arguments from being
106531** filled using OP_SCopy. OP_Copy must be used instead.
106532**
106533** The SQLITE_ECEL_FACTOR argument allows constant arguments to be
106534** factored out into initialization code.
106535**
106536** The SQLITE_ECEL_REF flag means that expressions in the list with
106537** ExprList.a[].u.x.iOrderByCol>0 have already been evaluated and stored
106538** in registers at srcReg, and so the value can be copied from there.
106539** If SQLITE_ECEL_OMITREF is also set, then the values with u.x.iOrderByCol>0
106540** are simply omitted rather than being copied from srcReg.
106541*/
106542SQLITE_PRIVATE int sqlite3ExprCodeExprList(
106543 Parse *pParse, /* Parsing context */
106544 ExprList *pList, /* The expression list to be coded */
106545 int target, /* Where to write results */
106546 int srcReg, /* Source registers if SQLITE_ECEL_REF */
106547 u8 flags /* SQLITE_ECEL_* flags */
106548){
106549 struct ExprList_item *pItem;
106550 int i, j, n;
106551 u8 copyOp = (flags & SQLITE_ECEL_DUP) ? OP_Copy : OP_SCopy;
106552 Vdbe *v = pParse->pVdbe;
106553 assert( pList!=0 );
106554 assert( target>0 );
106555 assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */
106556 n = pList->nExpr;
106557 if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR;
106558 for(pItem=pList->a, i=0; i<n; i++, pItem++){
106559 Expr *pExpr = pItem->pExpr;
106560#ifdef SQLITE_ENABLE_SORTER_REFERENCES
106561 if( pItem->bSorterRef ){
106562 i--;
106563 n--;
106564 }else
106565#endif
106566 if( (flags & SQLITE_ECEL_REF)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){
106567 if( flags & SQLITE_ECEL_OMITREF ){
106568 i--;
106569 n--;
106570 }else{
106571 sqlite3VdbeAddOp2(p: v, op: copyOp, p1: j+srcReg-1, p2: target+i);
106572 }
106573 }else if( (flags & SQLITE_ECEL_FACTOR)!=0
106574 && sqlite3ExprIsConstantNotJoin(p: pExpr)
106575 ){
106576 sqlite3ExprCodeRunJustOnce(pParse, pExpr, regDest: target+i);
106577 }else{
106578 int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target: target+i);
106579 if( inReg!=target+i ){
106580 VdbeOp *pOp;
106581 if( copyOp==OP_Copy
106582 && (pOp=sqlite3VdbeGetOp(p: v, addr: -1))->opcode==OP_Copy
106583 && pOp->p1+pOp->p3+1==inReg
106584 && pOp->p2+pOp->p3+1==target+i
106585 && pOp->p5==0 /* The do-not-merge flag must be clear */
106586 ){
106587 pOp->p3++;
106588 }else{
106589 sqlite3VdbeAddOp2(p: v, op: copyOp, p1: inReg, p2: target+i);
106590 }
106591 }
106592 }
106593 }
106594 return n;
106595}
106596
106597/*
106598** Generate code for a BETWEEN operator.
106599**
106600** x BETWEEN y AND z
106601**
106602** The above is equivalent to
106603**
106604** x>=y AND x<=z
106605**
106606** Code it as such, taking care to do the common subexpression
106607** elimination of x.
106608**
106609** The xJumpIf parameter determines details:
106610**
106611** NULL: Store the boolean result in reg[dest]
106612** sqlite3ExprIfTrue: Jump to dest if true
106613** sqlite3ExprIfFalse: Jump to dest if false
106614**
106615** The jumpIfNull parameter is ignored if xJumpIf is NULL.
106616*/
106617static void exprCodeBetween(
106618 Parse *pParse, /* Parsing and code generating context */
106619 Expr *pExpr, /* The BETWEEN expression */
106620 int dest, /* Jump destination or storage location */
106621 void (*xJump)(Parse*,Expr*,int,int), /* Action to take */
106622 int jumpIfNull /* Take the jump if the BETWEEN is NULL */
106623){
106624 Expr exprAnd; /* The AND operator in x>=y AND x<=z */
106625 Expr compLeft; /* The x>=y term */
106626 Expr compRight; /* The x<=z term */
106627 int regFree1 = 0; /* Temporary use register */
106628 Expr *pDel = 0;
106629 sqlite3 *db = pParse->db;
106630
106631 memset(s: &compLeft, c: 0, n: sizeof(Expr));
106632 memset(s: &compRight, c: 0, n: sizeof(Expr));
106633 memset(s: &exprAnd, c: 0, n: sizeof(Expr));
106634
106635 assert( ExprUseXList(pExpr) );
106636 pDel = sqlite3ExprDup(db, p: pExpr->pLeft, flags: 0);
106637 if( db->mallocFailed==0 ){
106638 exprAnd.op = TK_AND;
106639 exprAnd.pLeft = &compLeft;
106640 exprAnd.pRight = &compRight;
106641 compLeft.op = TK_GE;
106642 compLeft.pLeft = pDel;
106643 compLeft.pRight = pExpr->x.pList->a[0].pExpr;
106644 compRight.op = TK_LE;
106645 compRight.pLeft = pDel;
106646 compRight.pRight = pExpr->x.pList->a[1].pExpr;
106647 exprToRegister(pExpr: pDel, iReg: exprCodeVector(pParse, p: pDel, piFreeable: &regFree1));
106648 if( xJump ){
106649 xJump(pParse, &exprAnd, dest, jumpIfNull);
106650 }else{
106651 /* Mark the expression is being from the ON or USING clause of a join
106652 ** so that the sqlite3ExprCodeTarget() routine will not attempt to move
106653 ** it into the Parse.pConstExpr list. We should use a new bit for this,
106654 ** for clarity, but we are out of bits in the Expr.flags field so we
106655 ** have to reuse the EP_FromJoin bit. Bummer. */
106656 pDel->flags |= EP_FromJoin;
106657 sqlite3ExprCodeTarget(pParse, pExpr: &exprAnd, target: dest);
106658 }
106659 sqlite3ReleaseTempReg(pParse, regFree1);
106660 }
106661 sqlite3ExprDelete(db, p: pDel);
106662
106663 /* Ensure adequate test coverage */
106664 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull==0 && regFree1==0 );
106665 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull==0 && regFree1!=0 );
106666 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull!=0 && regFree1==0 );
106667 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull!=0 && regFree1!=0 );
106668 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1==0 );
106669 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1!=0 );
106670 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1==0 );
106671 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1!=0 );
106672 testcase( xJump==0 );
106673}
106674
106675/*
106676** Generate code for a boolean expression such that a jump is made
106677** to the label "dest" if the expression is true but execution
106678** continues straight thru if the expression is false.
106679**
106680** If the expression evaluates to NULL (neither true nor false), then
106681** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
106682**
106683** This code depends on the fact that certain token values (ex: TK_EQ)
106684** are the same as opcode values (ex: OP_Eq) that implement the corresponding
106685** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
106686** the make process cause these values to align. Assert()s in the code
106687** below verify that the numbers are aligned correctly.
106688*/
106689SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
106690 Vdbe *v = pParse->pVdbe;
106691 int op = 0;
106692 int regFree1 = 0;
106693 int regFree2 = 0;
106694 int r1, r2;
106695
106696 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
106697 if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
106698 if( NEVER(pExpr==0) ) return; /* No way this can happen */
106699 assert( !ExprHasVVAProperty(pExpr, EP_Immutable) );
106700 op = pExpr->op;
106701 switch( op ){
106702 case TK_AND:
106703 case TK_OR: {
106704 Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr);
106705 if( pAlt!=pExpr ){
106706 sqlite3ExprIfTrue(pParse, pExpr: pAlt, dest, jumpIfNull);
106707 }else if( op==TK_AND ){
106708 int d2 = sqlite3VdbeMakeLabel(pParse);
106709 testcase( jumpIfNull==0 );
106710 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,
106711 jumpIfNull^SQLITE_JUMPIFNULL);
106712 sqlite3ExprIfTrue(pParse, pExpr: pExpr->pRight, dest, jumpIfNull);
106713 sqlite3VdbeResolveLabel(v, x: d2);
106714 }else{
106715 testcase( jumpIfNull==0 );
106716 sqlite3ExprIfTrue(pParse, pExpr: pExpr->pLeft, dest, jumpIfNull);
106717 sqlite3ExprIfTrue(pParse, pExpr: pExpr->pRight, dest, jumpIfNull);
106718 }
106719 break;
106720 }
106721 case TK_NOT: {
106722 testcase( jumpIfNull==0 );
106723 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
106724 break;
106725 }
106726 case TK_TRUTH: {
106727 int isNot; /* IS NOT TRUE or IS NOT FALSE */
106728 int isTrue; /* IS TRUE or IS NOT TRUE */
106729 testcase( jumpIfNull==0 );
106730 isNot = pExpr->op2==TK_ISNOT;
106731 isTrue = sqlite3ExprTruthValue(pExpr: pExpr->pRight);
106732 testcase( isTrue && isNot );
106733 testcase( !isTrue && isNot );
106734 if( isTrue ^ isNot ){
106735 sqlite3ExprIfTrue(pParse, pExpr: pExpr->pLeft, dest,
106736 jumpIfNull: isNot ? SQLITE_JUMPIFNULL : 0);
106737 }else{
106738 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
106739 isNot ? SQLITE_JUMPIFNULL : 0);
106740 }
106741 break;
106742 }
106743 case TK_IS:
106744 case TK_ISNOT:
106745 testcase( op==TK_IS );
106746 testcase( op==TK_ISNOT );
106747 op = (op==TK_IS) ? TK_EQ : TK_NE;
106748 jumpIfNull = SQLITE_NULLEQ;
106749 /* no break */ deliberate_fall_through
106750 case TK_LT:
106751 case TK_LE:
106752 case TK_GT:
106753 case TK_GE:
106754 case TK_NE:
106755 case TK_EQ: {
106756 if( sqlite3ExprIsVector(pExpr: pExpr->pLeft) ) goto default_expr;
106757 testcase( jumpIfNull==0 );
106758 r1 = sqlite3ExprCodeTemp(pParse, pExpr: pExpr->pLeft, pReg: &regFree1);
106759 r2 = sqlite3ExprCodeTemp(pParse, pExpr: pExpr->pRight, pReg: &regFree2);
106760 codeCompare(pParse, pLeft: pExpr->pLeft, pRight: pExpr->pRight, opcode: op,
106761 in1: r1, in2: r2, dest, jumpIfNull, ExprHasProperty(pExpr,EP_Commuted));
106762 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
106763 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
106764 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
106765 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
106766 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq);
106767 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ);
106768 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ);
106769 assert(TK_NE==OP_Ne); testcase(op==OP_Ne);
106770 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ);
106771 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ);
106772 testcase( regFree1==0 );
106773 testcase( regFree2==0 );
106774 break;
106775 }
106776 case TK_ISNULL:
106777 case TK_NOTNULL: {
106778 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL );
106779 assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
106780 r1 = sqlite3ExprCodeTemp(pParse, pExpr: pExpr->pLeft, pReg: &regFree1);
106781 sqlite3VdbeAddOp2(p: v, op, p1: r1, p2: dest);
106782 VdbeCoverageIf(v, op==TK_ISNULL);
106783 VdbeCoverageIf(v, op==TK_NOTNULL);
106784 testcase( regFree1==0 );
106785 break;
106786 }
106787 case TK_BETWEEN: {
106788 testcase( jumpIfNull==0 );
106789 exprCodeBetween(pParse, pExpr, dest, xJump: sqlite3ExprIfTrue, jumpIfNull);
106790 break;
106791 }
106792#ifndef SQLITE_OMIT_SUBQUERY
106793 case TK_IN: {
106794 int destIfFalse = sqlite3VdbeMakeLabel(pParse);
106795 int destIfNull = jumpIfNull ? dest : destIfFalse;
106796 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
106797 sqlite3VdbeGoto(p: v, iDest: dest);
106798 sqlite3VdbeResolveLabel(v, x: destIfFalse);
106799 break;
106800 }
106801#endif
106802 default: {
106803 default_expr:
106804 if( ExprAlwaysTrue(pExpr) ){
106805 sqlite3VdbeGoto(p: v, iDest: dest);
106806 }else if( ExprAlwaysFalse(pExpr) ){
106807 /* No-op */
106808 }else{
106809 r1 = sqlite3ExprCodeTemp(pParse, pExpr, pReg: &regFree1);
106810 sqlite3VdbeAddOp3(p: v, OP_If, p1: r1, p2: dest, p3: jumpIfNull!=0);
106811 VdbeCoverage(v);
106812 testcase( regFree1==0 );
106813 testcase( jumpIfNull==0 );
106814 }
106815 break;
106816 }
106817 }
106818 sqlite3ReleaseTempReg(pParse, regFree1);
106819 sqlite3ReleaseTempReg(pParse, regFree2);
106820}
106821
106822/*
106823** Generate code for a boolean expression such that a jump is made
106824** to the label "dest" if the expression is false but execution
106825** continues straight thru if the expression is true.
106826**
106827** If the expression evaluates to NULL (neither true nor false) then
106828** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
106829** is 0.
106830*/
106831SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
106832 Vdbe *v = pParse->pVdbe;
106833 int op = 0;
106834 int regFree1 = 0;
106835 int regFree2 = 0;
106836 int r1, r2;
106837
106838 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
106839 if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
106840 if( pExpr==0 ) return;
106841 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) );
106842
106843 /* The value of pExpr->op and op are related as follows:
106844 **
106845 ** pExpr->op op
106846 ** --------- ----------
106847 ** TK_ISNULL OP_NotNull
106848 ** TK_NOTNULL OP_IsNull
106849 ** TK_NE OP_Eq
106850 ** TK_EQ OP_Ne
106851 ** TK_GT OP_Le
106852 ** TK_LE OP_Gt
106853 ** TK_GE OP_Lt
106854 ** TK_LT OP_Ge
106855 **
106856 ** For other values of pExpr->op, op is undefined and unused.
106857 ** The value of TK_ and OP_ constants are arranged such that we
106858 ** can compute the mapping above using the following expression.
106859 ** Assert()s verify that the computation is correct.
106860 */
106861 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
106862
106863 /* Verify correct alignment of TK_ and OP_ constants
106864 */
106865 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
106866 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
106867 assert( pExpr->op!=TK_NE || op==OP_Eq );
106868 assert( pExpr->op!=TK_EQ || op==OP_Ne );
106869 assert( pExpr->op!=TK_LT || op==OP_Ge );
106870 assert( pExpr->op!=TK_LE || op==OP_Gt );
106871 assert( pExpr->op!=TK_GT || op==OP_Le );
106872 assert( pExpr->op!=TK_GE || op==OP_Lt );
106873
106874 switch( pExpr->op ){
106875 case TK_AND:
106876 case TK_OR: {
106877 Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr);
106878 if( pAlt!=pExpr ){
106879 sqlite3ExprIfFalse(pParse, pExpr: pAlt, dest, jumpIfNull);
106880 }else if( pExpr->op==TK_AND ){
106881 testcase( jumpIfNull==0 );
106882 sqlite3ExprIfFalse(pParse, pExpr: pExpr->pLeft, dest, jumpIfNull);
106883 sqlite3ExprIfFalse(pParse, pExpr: pExpr->pRight, dest, jumpIfNull);
106884 }else{
106885 int d2 = sqlite3VdbeMakeLabel(pParse);
106886 testcase( jumpIfNull==0 );
106887 sqlite3ExprIfTrue(pParse, pExpr: pExpr->pLeft, dest: d2,
106888 jumpIfNull: jumpIfNull^SQLITE_JUMPIFNULL);
106889 sqlite3ExprIfFalse(pParse, pExpr: pExpr->pRight, dest, jumpIfNull);
106890 sqlite3VdbeResolveLabel(v, x: d2);
106891 }
106892 break;
106893 }
106894 case TK_NOT: {
106895 testcase( jumpIfNull==0 );
106896 sqlite3ExprIfTrue(pParse, pExpr: pExpr->pLeft, dest, jumpIfNull);
106897 break;
106898 }
106899 case TK_TRUTH: {
106900 int isNot; /* IS NOT TRUE or IS NOT FALSE */
106901 int isTrue; /* IS TRUE or IS NOT TRUE */
106902 testcase( jumpIfNull==0 );
106903 isNot = pExpr->op2==TK_ISNOT;
106904 isTrue = sqlite3ExprTruthValue(pExpr: pExpr->pRight);
106905 testcase( isTrue && isNot );
106906 testcase( !isTrue && isNot );
106907 if( isTrue ^ isNot ){
106908 /* IS TRUE and IS NOT FALSE */
106909 sqlite3ExprIfFalse(pParse, pExpr: pExpr->pLeft, dest,
106910 jumpIfNull: isNot ? 0 : SQLITE_JUMPIFNULL);
106911
106912 }else{
106913 /* IS FALSE and IS NOT TRUE */
106914 sqlite3ExprIfTrue(pParse, pExpr: pExpr->pLeft, dest,
106915 jumpIfNull: isNot ? 0 : SQLITE_JUMPIFNULL);
106916 }
106917 break;
106918 }
106919 case TK_IS:
106920 case TK_ISNOT:
106921 testcase( pExpr->op==TK_IS );
106922 testcase( pExpr->op==TK_ISNOT );
106923 op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
106924 jumpIfNull = SQLITE_NULLEQ;
106925 /* no break */ deliberate_fall_through
106926 case TK_LT:
106927 case TK_LE:
106928 case TK_GT:
106929 case TK_GE:
106930 case TK_NE:
106931 case TK_EQ: {
106932 if( sqlite3ExprIsVector(pExpr: pExpr->pLeft) ) goto default_expr;
106933 testcase( jumpIfNull==0 );
106934 r1 = sqlite3ExprCodeTemp(pParse, pExpr: pExpr->pLeft, pReg: &regFree1);
106935 r2 = sqlite3ExprCodeTemp(pParse, pExpr: pExpr->pRight, pReg: &regFree2);
106936 codeCompare(pParse, pLeft: pExpr->pLeft, pRight: pExpr->pRight, opcode: op,
106937 in1: r1, in2: r2, dest, jumpIfNull,ExprHasProperty(pExpr,EP_Commuted));
106938 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
106939 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
106940 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
106941 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
106942 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq);
106943 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ);
106944 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ);
106945 assert(TK_NE==OP_Ne); testcase(op==OP_Ne);
106946 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ);
106947 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ);
106948 testcase( regFree1==0 );
106949 testcase( regFree2==0 );
106950 break;
106951 }
106952 case TK_ISNULL:
106953 case TK_NOTNULL: {
106954 r1 = sqlite3ExprCodeTemp(pParse, pExpr: pExpr->pLeft, pReg: &regFree1);
106955 sqlite3VdbeAddOp2(p: v, op, p1: r1, p2: dest);
106956 testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL);
106957 testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL);
106958 testcase( regFree1==0 );
106959 break;
106960 }
106961 case TK_BETWEEN: {
106962 testcase( jumpIfNull==0 );
106963 exprCodeBetween(pParse, pExpr, dest, xJump: sqlite3ExprIfFalse, jumpIfNull);
106964 break;
106965 }
106966#ifndef SQLITE_OMIT_SUBQUERY
106967 case TK_IN: {
106968 if( jumpIfNull ){
106969 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse: dest, destIfNull: dest);
106970 }else{
106971 int destIfNull = sqlite3VdbeMakeLabel(pParse);
106972 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse: dest, destIfNull);
106973 sqlite3VdbeResolveLabel(v, x: destIfNull);
106974 }
106975 break;
106976 }
106977#endif
106978 default: {
106979 default_expr:
106980 if( ExprAlwaysFalse(pExpr) ){
106981 sqlite3VdbeGoto(p: v, iDest: dest);
106982 }else if( ExprAlwaysTrue(pExpr) ){
106983 /* no-op */
106984 }else{
106985 r1 = sqlite3ExprCodeTemp(pParse, pExpr, pReg: &regFree1);
106986 sqlite3VdbeAddOp3(p: v, OP_IfNot, p1: r1, p2: dest, p3: jumpIfNull!=0);
106987 VdbeCoverage(v);
106988 testcase( regFree1==0 );
106989 testcase( jumpIfNull==0 );
106990 }
106991 break;
106992 }
106993 }
106994 sqlite3ReleaseTempReg(pParse, regFree1);
106995 sqlite3ReleaseTempReg(pParse, regFree2);
106996}
106997
106998/*
106999** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before
107000** code generation, and that copy is deleted after code generation. This
107001** ensures that the original pExpr is unchanged.
107002*/
107003SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){
107004 sqlite3 *db = pParse->db;
107005 Expr *pCopy = sqlite3ExprDup(db, p: pExpr, flags: 0);
107006 if( db->mallocFailed==0 ){
107007 sqlite3ExprIfFalse(pParse, pExpr: pCopy, dest, jumpIfNull);
107008 }
107009 sqlite3ExprDelete(db, p: pCopy);
107010}
107011
107012/*
107013** Expression pVar is guaranteed to be an SQL variable. pExpr may be any
107014** type of expression.
107015**
107016** If pExpr is a simple SQL value - an integer, real, string, blob
107017** or NULL value - then the VDBE currently being prepared is configured
107018** to re-prepare each time a new value is bound to variable pVar.
107019**
107020** Additionally, if pExpr is a simple SQL value and the value is the
107021** same as that currently bound to variable pVar, non-zero is returned.
107022** Otherwise, if the values are not the same or if pExpr is not a simple
107023** SQL value, zero is returned.
107024*/
107025static int exprCompareVariable(
107026 const Parse *pParse,
107027 const Expr *pVar,
107028 const Expr *pExpr
107029){
107030 int res = 0;
107031 int iVar;
107032 sqlite3_value *pL, *pR = 0;
107033
107034 sqlite3ValueFromExpr(db: pParse->db, pExpr, SQLITE_UTF8, SQLITE_AFF_BLOB, ppVal: &pR);
107035 if( pR ){
107036 iVar = pVar->iColumn;
107037 sqlite3VdbeSetVarmask(v: pParse->pVdbe, iVar);
107038 pL = sqlite3VdbeGetBoundValue(v: pParse->pReprepare, iVar, SQLITE_AFF_BLOB);
107039 if( pL ){
107040 if( sqlite3_value_type(pVal: pL)==SQLITE_TEXT ){
107041 sqlite3_value_text(pVal: pL); /* Make sure the encoding is UTF-8 */
107042 }
107043 res = 0==sqlite3MemCompare(pMem1: pL, pMem2: pR, pColl: 0);
107044 }
107045 sqlite3ValueFree(v: pR);
107046 sqlite3ValueFree(v: pL);
107047 }
107048
107049 return res;
107050}
107051
107052/*
107053** Do a deep comparison of two expression trees. Return 0 if the two
107054** expressions are completely identical. Return 1 if they differ only
107055** by a COLLATE operator at the top level. Return 2 if there are differences
107056** other than the top-level COLLATE operator.
107057**
107058** If any subelement of pB has Expr.iTable==(-1) then it is allowed
107059** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
107060**
107061** The pA side might be using TK_REGISTER. If that is the case and pB is
107062** not using TK_REGISTER but is otherwise equivalent, then still return 0.
107063**
107064** Sometimes this routine will return 2 even if the two expressions
107065** really are equivalent. If we cannot prove that the expressions are
107066** identical, we return 2 just to be safe. So if this routine
107067** returns 2, then you do not really know for certain if the two
107068** expressions are the same. But if you get a 0 or 1 return, then you
107069** can be sure the expressions are the same. In the places where
107070** this routine is used, it does not hurt to get an extra 2 - that
107071** just might result in some slightly slower code. But returning
107072** an incorrect 0 or 1 could lead to a malfunction.
107073**
107074** If pParse is not NULL then TK_VARIABLE terms in pA with bindings in
107075** pParse->pReprepare can be matched against literals in pB. The
107076** pParse->pVdbe->expmask bitmask is updated for each variable referenced.
107077** If pParse is NULL (the normal case) then any TK_VARIABLE term in
107078** Argument pParse should normally be NULL. If it is not NULL and pA or
107079** pB causes a return value of 2.
107080*/
107081SQLITE_PRIVATE int sqlite3ExprCompare(
107082 const Parse *pParse,
107083 const Expr *pA,
107084 const Expr *pB,
107085 int iTab
107086){
107087 u32 combinedFlags;
107088 if( pA==0 || pB==0 ){
107089 return pB==pA ? 0 : 2;
107090 }
107091 if( pParse && pA->op==TK_VARIABLE && exprCompareVariable(pParse, pVar: pA, pExpr: pB) ){
107092 return 0;
107093 }
107094 combinedFlags = pA->flags | pB->flags;
107095 if( combinedFlags & EP_IntValue ){
107096 if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
107097 return 0;
107098 }
107099 return 2;
107100 }
107101 if( pA->op!=pB->op || pA->op==TK_RAISE ){
107102 if( pA->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA: pA->pLeft,pB,iTab)<2 ){
107103 return 1;
107104 }
107105 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA,pB: pB->pLeft,iTab)<2 ){
107106 return 1;
107107 }
107108 return 2;
107109 }
107110 assert( !ExprHasProperty(pA, EP_IntValue) );
107111 assert( !ExprHasProperty(pB, EP_IntValue) );
107112 if( pA->u.zToken ){
107113 if( pA->op==TK_FUNCTION || pA->op==TK_AGG_FUNCTION ){
107114 if( sqlite3StrICmp(zLeft: pA->u.zToken,zRight: pB->u.zToken)!=0 ) return 2;
107115#ifndef SQLITE_OMIT_WINDOWFUNC
107116 assert( pA->op==pB->op );
107117 if( ExprHasProperty(pA,EP_WinFunc)!=ExprHasProperty(pB,EP_WinFunc) ){
107118 return 2;
107119 }
107120 if( ExprHasProperty(pA,EP_WinFunc) ){
107121 if( sqlite3WindowCompare(pParse, pA->y.pWin, pB->y.pWin, 1)!=0 ){
107122 return 2;
107123 }
107124 }
107125#endif
107126 }else if( pA->op==TK_NULL ){
107127 return 0;
107128 }else if( pA->op==TK_COLLATE ){
107129 if( sqlite3_stricmp(zLeft: pA->u.zToken,zRight: pB->u.zToken)!=0 ) return 2;
107130 }else
107131 if( pB->u.zToken!=0
107132 && pA->op!=TK_COLUMN
107133 && pA->op!=TK_AGG_COLUMN
107134 && strcmp(s1: pA->u.zToken,s2: pB->u.zToken)!=0
107135 ){
107136 return 2;
107137 }
107138 }
107139 if( (pA->flags & (EP_Distinct|EP_Commuted))
107140 != (pB->flags & (EP_Distinct|EP_Commuted)) ) return 2;
107141 if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){
107142 if( combinedFlags & EP_xIsSelect ) return 2;
107143 if( (combinedFlags & EP_FixedCol)==0
107144 && sqlite3ExprCompare(pParse, pA: pA->pLeft, pB: pB->pLeft, iTab) ) return 2;
107145 if( sqlite3ExprCompare(pParse, pA: pA->pRight, pB: pB->pRight, iTab) ) return 2;
107146 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
107147 if( pA->op!=TK_STRING
107148 && pA->op!=TK_TRUEFALSE
107149 && ALWAYS((combinedFlags & EP_Reduced)==0)
107150 ){
107151 if( pA->iColumn!=pB->iColumn ) return 2;
107152 if( pA->op2!=pB->op2 && pA->op==TK_TRUTH ) return 2;
107153 if( pA->op!=TK_IN && pA->iTable!=pB->iTable && pA->iTable!=iTab ){
107154 return 2;
107155 }
107156 }
107157 }
107158 return 0;
107159}
107160
107161/*
107162** Compare two ExprList objects. Return 0 if they are identical, 1
107163** if they are certainly different, or 2 if it is not possible to
107164** determine if they are identical or not.
107165**
107166** If any subelement of pB has Expr.iTable==(-1) then it is allowed
107167** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
107168**
107169** This routine might return non-zero for equivalent ExprLists. The
107170** only consequence will be disabled optimizations. But this routine
107171** must never return 0 if the two ExprList objects are different, or
107172** a malfunction will result.
107173**
107174** Two NULL pointers are considered to be the same. But a NULL pointer
107175** always differs from a non-NULL pointer.
107176*/
107177SQLITE_PRIVATE int sqlite3ExprListCompare(const ExprList *pA, const ExprList *pB, int iTab){
107178 int i;
107179 if( pA==0 && pB==0 ) return 0;
107180 if( pA==0 || pB==0 ) return 1;
107181 if( pA->nExpr!=pB->nExpr ) return 1;
107182 for(i=0; i<pA->nExpr; i++){
107183 int res;
107184 Expr *pExprA = pA->a[i].pExpr;
107185 Expr *pExprB = pB->a[i].pExpr;
107186 if( pA->a[i].sortFlags!=pB->a[i].sortFlags ) return 1;
107187 if( (res = sqlite3ExprCompare(pParse: 0, pA: pExprA, pB: pExprB, iTab)) ) return res;
107188 }
107189 return 0;
107190}
107191
107192/*
107193** Like sqlite3ExprCompare() except COLLATE operators at the top-level
107194** are ignored.
107195*/
107196SQLITE_PRIVATE int sqlite3ExprCompareSkip(Expr *pA,Expr *pB, int iTab){
107197 return sqlite3ExprCompare(pParse: 0,
107198 pA: sqlite3ExprSkipCollateAndLikely(pExpr: pA),
107199 pB: sqlite3ExprSkipCollateAndLikely(pExpr: pB),
107200 iTab);
107201}
107202
107203/*
107204** Return non-zero if Expr p can only be true if pNN is not NULL.
107205**
107206** Or if seenNot is true, return non-zero if Expr p can only be
107207** non-NULL if pNN is not NULL
107208*/
107209static int exprImpliesNotNull(
107210 const Parse *pParse,/* Parsing context */
107211 const Expr *p, /* The expression to be checked */
107212 const Expr *pNN, /* The expression that is NOT NULL */
107213 int iTab, /* Table being evaluated */
107214 int seenNot /* Return true only if p can be any non-NULL value */
107215){
107216 assert( p );
107217 assert( pNN );
107218 if( sqlite3ExprCompare(pParse, pA: p, pB: pNN, iTab)==0 ){
107219 return pNN->op!=TK_NULL;
107220 }
107221 switch( p->op ){
107222 case TK_IN: {
107223 if( seenNot && ExprHasProperty(p, EP_xIsSelect) ) return 0;
107224 assert( ExprUseXSelect(p) || (p->x.pList!=0 && p->x.pList->nExpr>0) );
107225 return exprImpliesNotNull(pParse, p: p->pLeft, pNN, iTab, seenNot: 1);
107226 }
107227 case TK_BETWEEN: {
107228 ExprList *pList;
107229 assert( ExprUseXList(p) );
107230 pList = p->x.pList;
107231 assert( pList!=0 );
107232 assert( pList->nExpr==2 );
107233 if( seenNot ) return 0;
107234 if( exprImpliesNotNull(pParse, p: pList->a[0].pExpr, pNN, iTab, seenNot: 1)
107235 || exprImpliesNotNull(pParse, p: pList->a[1].pExpr, pNN, iTab, seenNot: 1)
107236 ){
107237 return 1;
107238 }
107239 return exprImpliesNotNull(pParse, p: p->pLeft, pNN, iTab, seenNot: 1);
107240 }
107241 case TK_EQ:
107242 case TK_NE:
107243 case TK_LT:
107244 case TK_LE:
107245 case TK_GT:
107246 case TK_GE:
107247 case TK_PLUS:
107248 case TK_MINUS:
107249 case TK_BITOR:
107250 case TK_LSHIFT:
107251 case TK_RSHIFT:
107252 case TK_CONCAT:
107253 seenNot = 1;
107254 /* no break */ deliberate_fall_through
107255 case TK_STAR:
107256 case TK_REM:
107257 case TK_BITAND:
107258 case TK_SLASH: {
107259 if( exprImpliesNotNull(pParse, p: p->pRight, pNN, iTab, seenNot) ) return 1;
107260 /* no break */ deliberate_fall_through
107261 }
107262 case TK_SPAN:
107263 case TK_COLLATE:
107264 case TK_UPLUS:
107265 case TK_UMINUS: {
107266 return exprImpliesNotNull(pParse, p: p->pLeft, pNN, iTab, seenNot);
107267 }
107268 case TK_TRUTH: {
107269 if( seenNot ) return 0;
107270 if( p->op2!=TK_IS ) return 0;
107271 return exprImpliesNotNull(pParse, p: p->pLeft, pNN, iTab, seenNot: 1);
107272 }
107273 case TK_BITNOT:
107274 case TK_NOT: {
107275 return exprImpliesNotNull(pParse, p: p->pLeft, pNN, iTab, seenNot: 1);
107276 }
107277 }
107278 return 0;
107279}
107280
107281/*
107282** Return true if we can prove the pE2 will always be true if pE1 is
107283** true. Return false if we cannot complete the proof or if pE2 might
107284** be false. Examples:
107285**
107286** pE1: x==5 pE2: x==5 Result: true
107287** pE1: x>0 pE2: x==5 Result: false
107288** pE1: x=21 pE2: x=21 OR y=43 Result: true
107289** pE1: x!=123 pE2: x IS NOT NULL Result: true
107290** pE1: x!=?1 pE2: x IS NOT NULL Result: true
107291** pE1: x IS NULL pE2: x IS NOT NULL Result: false
107292** pE1: x IS ?2 pE2: x IS NOT NULL Reuslt: false
107293**
107294** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has
107295** Expr.iTable<0 then assume a table number given by iTab.
107296**
107297** If pParse is not NULL, then the values of bound variables in pE1 are
107298** compared against literal values in pE2 and pParse->pVdbe->expmask is
107299** modified to record which bound variables are referenced. If pParse
107300** is NULL, then false will be returned if pE1 contains any bound variables.
107301**
107302** When in doubt, return false. Returning true might give a performance
107303** improvement. Returning false might cause a performance reduction, but
107304** it will always give the correct answer and is hence always safe.
107305*/
107306SQLITE_PRIVATE int sqlite3ExprImpliesExpr(
107307 const Parse *pParse,
107308 const Expr *pE1,
107309 const Expr *pE2,
107310 int iTab
107311){
107312 if( sqlite3ExprCompare(pParse, pA: pE1, pB: pE2, iTab)==0 ){
107313 return 1;
107314 }
107315 if( pE2->op==TK_OR
107316 && (sqlite3ExprImpliesExpr(pParse, pE1, pE2: pE2->pLeft, iTab)
107317 || sqlite3ExprImpliesExpr(pParse, pE1, pE2: pE2->pRight, iTab) )
107318 ){
107319 return 1;
107320 }
107321 if( pE2->op==TK_NOTNULL
107322 && exprImpliesNotNull(pParse, p: pE1, pNN: pE2->pLeft, iTab, seenNot: 0)
107323 ){
107324 return 1;
107325 }
107326 return 0;
107327}
107328
107329/*
107330** This is the Expr node callback for sqlite3ExprImpliesNonNullRow().
107331** If the expression node requires that the table at pWalker->iCur
107332** have one or more non-NULL column, then set pWalker->eCode to 1 and abort.
107333**
107334** This routine controls an optimization. False positives (setting
107335** pWalker->eCode to 1 when it should not be) are deadly, but false-negatives
107336** (never setting pWalker->eCode) is a harmless missed optimization.
107337*/
107338static int impliesNotNullRow(Walker *pWalker, Expr *pExpr){
107339 testcase( pExpr->op==TK_AGG_COLUMN );
107340 testcase( pExpr->op==TK_AGG_FUNCTION );
107341 if( ExprHasProperty(pExpr, EP_FromJoin) ) return WRC_Prune;
107342 switch( pExpr->op ){
107343 case TK_ISNOT:
107344 case TK_ISNULL:
107345 case TK_NOTNULL:
107346 case TK_IS:
107347 case TK_OR:
107348 case TK_VECTOR:
107349 case TK_CASE:
107350 case TK_IN:
107351 case TK_FUNCTION:
107352 case TK_TRUTH:
107353 testcase( pExpr->op==TK_ISNOT );
107354 testcase( pExpr->op==TK_ISNULL );
107355 testcase( pExpr->op==TK_NOTNULL );
107356 testcase( pExpr->op==TK_IS );
107357 testcase( pExpr->op==TK_OR );
107358 testcase( pExpr->op==TK_VECTOR );
107359 testcase( pExpr->op==TK_CASE );
107360 testcase( pExpr->op==TK_IN );
107361 testcase( pExpr->op==TK_FUNCTION );
107362 testcase( pExpr->op==TK_TRUTH );
107363 return WRC_Prune;
107364 case TK_COLUMN:
107365 if( pWalker->u.iCur==pExpr->iTable ){
107366 pWalker->eCode = 1;
107367 return WRC_Abort;
107368 }
107369 return WRC_Prune;
107370
107371 case TK_AND:
107372 if( pWalker->eCode==0 ){
107373 sqlite3WalkExpr(pWalker, pExpr: pExpr->pLeft);
107374 if( pWalker->eCode ){
107375 pWalker->eCode = 0;
107376 sqlite3WalkExpr(pWalker, pExpr: pExpr->pRight);
107377 }
107378 }
107379 return WRC_Prune;
107380
107381 case TK_BETWEEN:
107382 if( sqlite3WalkExpr(pWalker, pExpr: pExpr->pLeft)==WRC_Abort ){
107383 assert( pWalker->eCode );
107384 return WRC_Abort;
107385 }
107386 return WRC_Prune;
107387
107388 /* Virtual tables are allowed to use constraints like x=NULL. So
107389 ** a term of the form x=y does not prove that y is not null if x
107390 ** is the column of a virtual table */
107391 case TK_EQ:
107392 case TK_NE:
107393 case TK_LT:
107394 case TK_LE:
107395 case TK_GT:
107396 case TK_GE: {
107397 Expr *pLeft = pExpr->pLeft;
107398 Expr *pRight = pExpr->pRight;
107399 testcase( pExpr->op==TK_EQ );
107400 testcase( pExpr->op==TK_NE );
107401 testcase( pExpr->op==TK_LT );
107402 testcase( pExpr->op==TK_LE );
107403 testcase( pExpr->op==TK_GT );
107404 testcase( pExpr->op==TK_GE );
107405 /* The y.pTab=0 assignment in wherecode.c always happens after the
107406 ** impliesNotNullRow() test */
107407 assert( pLeft->op!=TK_COLUMN || ExprUseYTab(pLeft) );
107408 assert( pRight->op!=TK_COLUMN || ExprUseYTab(pRight) );
107409 if( (pLeft->op==TK_COLUMN
107410 && pLeft->y.pTab!=0
107411 && IsVirtual(pLeft->y.pTab))
107412 || (pRight->op==TK_COLUMN
107413 && pRight->y.pTab!=0
107414 && IsVirtual(pRight->y.pTab))
107415 ){
107416 return WRC_Prune;
107417 }
107418 /* no break */ deliberate_fall_through
107419 }
107420 default:
107421 return WRC_Continue;
107422 }
107423}
107424
107425/*
107426** Return true (non-zero) if expression p can only be true if at least
107427** one column of table iTab is non-null. In other words, return true
107428** if expression p will always be NULL or false if every column of iTab
107429** is NULL.
107430**
107431** False negatives are acceptable. In other words, it is ok to return
107432** zero even if expression p will never be true of every column of iTab
107433** is NULL. A false negative is merely a missed optimization opportunity.
107434**
107435** False positives are not allowed, however. A false positive may result
107436** in an incorrect answer.
107437**
107438** Terms of p that are marked with EP_FromJoin (and hence that come from
107439** the ON or USING clauses of LEFT JOINS) are excluded from the analysis.
107440**
107441** This routine is used to check if a LEFT JOIN can be converted into
107442** an ordinary JOIN. The p argument is the WHERE clause. If the WHERE
107443** clause requires that some column of the right table of the LEFT JOIN
107444** be non-NULL, then the LEFT JOIN can be safely converted into an
107445** ordinary join.
107446*/
107447SQLITE_PRIVATE int sqlite3ExprImpliesNonNullRow(Expr *p, int iTab){
107448 Walker w;
107449 p = sqlite3ExprSkipCollateAndLikely(pExpr: p);
107450 if( p==0 ) return 0;
107451 if( p->op==TK_NOTNULL ){
107452 p = p->pLeft;
107453 }else{
107454 while( p->op==TK_AND ){
107455 if( sqlite3ExprImpliesNonNullRow(p: p->pLeft, iTab) ) return 1;
107456 p = p->pRight;
107457 }
107458 }
107459 w.xExprCallback = impliesNotNullRow;
107460 w.xSelectCallback = 0;
107461 w.xSelectCallback2 = 0;
107462 w.eCode = 0;
107463 w.u.iCur = iTab;
107464 sqlite3WalkExpr(pWalker: &w, pExpr: p);
107465 return w.eCode;
107466}
107467
107468/*
107469** An instance of the following structure is used by the tree walker
107470** to determine if an expression can be evaluated by reference to the
107471** index only, without having to do a search for the corresponding
107472** table entry. The IdxCover.pIdx field is the index. IdxCover.iCur
107473** is the cursor for the table.
107474*/
107475struct IdxCover {
107476 Index *pIdx; /* The index to be tested for coverage */
107477 int iCur; /* Cursor number for the table corresponding to the index */
107478};
107479
107480/*
107481** Check to see if there are references to columns in table
107482** pWalker->u.pIdxCover->iCur can be satisfied using the index
107483** pWalker->u.pIdxCover->pIdx.
107484*/
107485static int exprIdxCover(Walker *pWalker, Expr *pExpr){
107486 if( pExpr->op==TK_COLUMN
107487 && pExpr->iTable==pWalker->u.pIdxCover->iCur
107488 && sqlite3TableColumnToIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0
107489 ){
107490 pWalker->eCode = 1;
107491 return WRC_Abort;
107492 }
107493 return WRC_Continue;
107494}
107495
107496/*
107497** Determine if an index pIdx on table with cursor iCur contains will
107498** the expression pExpr. Return true if the index does cover the
107499** expression and false if the pExpr expression references table columns
107500** that are not found in the index pIdx.
107501**
107502** An index covering an expression means that the expression can be
107503** evaluated using only the index and without having to lookup the
107504** corresponding table entry.
107505*/
107506SQLITE_PRIVATE int sqlite3ExprCoveredByIndex(
107507 Expr *pExpr, /* The index to be tested */
107508 int iCur, /* The cursor number for the corresponding table */
107509 Index *pIdx /* The index that might be used for coverage */
107510){
107511 Walker w;
107512 struct IdxCover xcov;
107513 memset(s: &w, c: 0, n: sizeof(w));
107514 xcov.iCur = iCur;
107515 xcov.pIdx = pIdx;
107516 w.xExprCallback = exprIdxCover;
107517 w.u.pIdxCover = &xcov;
107518 sqlite3WalkExpr(pWalker: &w, pExpr);
107519 return !w.eCode;
107520}
107521
107522
107523/* Structure used to pass information throught the Walker in order to
107524** implement sqlite3ReferencesSrcList().
107525*/
107526struct RefSrcList {
107527 sqlite3 *db; /* Database connection used for sqlite3DbRealloc() */
107528 SrcList *pRef; /* Looking for references to these tables */
107529 i64 nExclude; /* Number of tables to exclude from the search */
107530 int *aiExclude; /* Cursor IDs for tables to exclude from the search */
107531};
107532
107533/*
107534** Walker SELECT callbacks for sqlite3ReferencesSrcList().
107535**
107536** When entering a new subquery on the pExpr argument, add all FROM clause
107537** entries for that subquery to the exclude list.
107538**
107539** When leaving the subquery, remove those entries from the exclude list.
107540*/
107541static int selectRefEnter(Walker *pWalker, Select *pSelect){
107542 struct RefSrcList *p = pWalker->u.pRefSrcList;
107543 SrcList *pSrc = pSelect->pSrc;
107544 i64 i, j;
107545 int *piNew;
107546 if( pSrc->nSrc==0 ) return WRC_Continue;
107547 j = p->nExclude;
107548 p->nExclude += pSrc->nSrc;
107549 piNew = sqlite3DbRealloc(db: p->db, p: p->aiExclude, n: p->nExclude*sizeof(int));
107550 if( piNew==0 ){
107551 p->nExclude = 0;
107552 return WRC_Abort;
107553 }else{
107554 p->aiExclude = piNew;
107555 }
107556 for(i=0; i<pSrc->nSrc; i++, j++){
107557 p->aiExclude[j] = pSrc->a[i].iCursor;
107558 }
107559 return WRC_Continue;
107560}
107561static void selectRefLeave(Walker *pWalker, Select *pSelect){
107562 struct RefSrcList *p = pWalker->u.pRefSrcList;
107563 SrcList *pSrc = pSelect->pSrc;
107564 if( p->nExclude ){
107565 assert( p->nExclude>=pSrc->nSrc );
107566 p->nExclude -= pSrc->nSrc;
107567 }
107568}
107569
107570/* This is the Walker EXPR callback for sqlite3ReferencesSrcList().
107571**
107572** Set the 0x01 bit of pWalker->eCode if there is a reference to any
107573** of the tables shown in RefSrcList.pRef.
107574**
107575** Set the 0x02 bit of pWalker->eCode if there is a reference to a
107576** table is in neither RefSrcList.pRef nor RefSrcList.aiExclude.
107577*/
107578static int exprRefToSrcList(Walker *pWalker, Expr *pExpr){
107579 if( pExpr->op==TK_COLUMN
107580 || pExpr->op==TK_AGG_COLUMN
107581 ){
107582 int i;
107583 struct RefSrcList *p = pWalker->u.pRefSrcList;
107584 SrcList *pSrc = p->pRef;
107585 int nSrc = pSrc ? pSrc->nSrc : 0;
107586 for(i=0; i<nSrc; i++){
107587 if( pExpr->iTable==pSrc->a[i].iCursor ){
107588 pWalker->eCode |= 1;
107589 return WRC_Continue;
107590 }
107591 }
107592 for(i=0; i<p->nExclude && p->aiExclude[i]!=pExpr->iTable; i++){}
107593 if( i>=p->nExclude ){
107594 pWalker->eCode |= 2;
107595 }
107596 }
107597 return WRC_Continue;
107598}
107599
107600/*
107601** Check to see if pExpr references any tables in pSrcList.
107602** Possible return values:
107603**
107604** 1 pExpr does references a table in pSrcList.
107605**
107606** 0 pExpr references some table that is not defined in either
107607** pSrcList or in subqueries of pExpr itself.
107608**
107609** -1 pExpr only references no tables at all, or it only
107610** references tables defined in subqueries of pExpr itself.
107611**
107612** As currently used, pExpr is always an aggregate function call. That
107613** fact is exploited for efficiency.
107614*/
107615SQLITE_PRIVATE int sqlite3ReferencesSrcList(Parse *pParse, Expr *pExpr, SrcList *pSrcList){
107616 Walker w;
107617 struct RefSrcList x;
107618 memset(s: &w, c: 0, n: sizeof(w));
107619 memset(s: &x, c: 0, n: sizeof(x));
107620 w.xExprCallback = exprRefToSrcList;
107621 w.xSelectCallback = selectRefEnter;
107622 w.xSelectCallback2 = selectRefLeave;
107623 w.u.pRefSrcList = &x;
107624 x.db = pParse->db;
107625 x.pRef = pSrcList;
107626 assert( pExpr->op==TK_AGG_FUNCTION );
107627 assert( ExprUseXList(pExpr) );
107628 sqlite3WalkExprList(pWalker: &w, p: pExpr->x.pList);
107629#ifndef SQLITE_OMIT_WINDOWFUNC
107630 if( ExprHasProperty(pExpr, EP_WinFunc) ){
107631 sqlite3WalkExpr(pWalker: &w, pExpr: pExpr->y.pWin->pFilter);
107632 }
107633#endif
107634 sqlite3DbFree(db: pParse->db, p: x.aiExclude);
107635 if( w.eCode & 0x01 ){
107636 return 1;
107637 }else if( w.eCode ){
107638 return 0;
107639 }else{
107640 return -1;
107641 }
107642}
107643
107644/*
107645** This is a Walker expression node callback.
107646**
107647** For Expr nodes that contain pAggInfo pointers, make sure the AggInfo
107648** object that is referenced does not refer directly to the Expr. If
107649** it does, make a copy. This is done because the pExpr argument is
107650** subject to change.
107651**
107652** The copy is stored on pParse->pConstExpr with a register number of 0.
107653** This will cause the expression to be deleted automatically when the
107654** Parse object is destroyed, but the zero register number means that it
107655** will not generate any code in the preamble.
107656*/
107657static int agginfoPersistExprCb(Walker *pWalker, Expr *pExpr){
107658 if( ALWAYS(!ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced))
107659 && pExpr->pAggInfo!=0
107660 ){
107661 AggInfo *pAggInfo = pExpr->pAggInfo;
107662 int iAgg = pExpr->iAgg;
107663 Parse *pParse = pWalker->pParse;
107664 sqlite3 *db = pParse->db;
107665 assert( pExpr->op==TK_AGG_COLUMN || pExpr->op==TK_AGG_FUNCTION );
107666 if( pExpr->op==TK_AGG_COLUMN ){
107667 assert( iAgg>=0 && iAgg<pAggInfo->nColumn );
107668 if( pAggInfo->aCol[iAgg].pCExpr==pExpr ){
107669 pExpr = sqlite3ExprDup(db, p: pExpr, flags: 0);
107670 if( pExpr ){
107671 pAggInfo->aCol[iAgg].pCExpr = pExpr;
107672 sqlite3ExprDeferredDelete(pParse, pExpr);
107673 }
107674 }
107675 }else{
107676 assert( iAgg>=0 && iAgg<pAggInfo->nFunc );
107677 if( pAggInfo->aFunc[iAgg].pFExpr==pExpr ){
107678 pExpr = sqlite3ExprDup(db, p: pExpr, flags: 0);
107679 if( pExpr ){
107680 pAggInfo->aFunc[iAgg].pFExpr = pExpr;
107681 sqlite3ExprDeferredDelete(pParse, pExpr);
107682 }
107683 }
107684 }
107685 }
107686 return WRC_Continue;
107687}
107688
107689/*
107690** Initialize a Walker object so that will persist AggInfo entries referenced
107691** by the tree that is walked.
107692*/
107693SQLITE_PRIVATE void sqlite3AggInfoPersistWalkerInit(Walker *pWalker, Parse *pParse){
107694 memset(s: pWalker, c: 0, n: sizeof(*pWalker));
107695 pWalker->pParse = pParse;
107696 pWalker->xExprCallback = agginfoPersistExprCb;
107697 pWalker->xSelectCallback = sqlite3SelectWalkNoop;
107698}
107699
107700/*
107701** Add a new element to the pAggInfo->aCol[] array. Return the index of
107702** the new element. Return a negative number if malloc fails.
107703*/
107704static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
107705 int i;
107706 pInfo->aCol = sqlite3ArrayAllocate(
107707 db,
107708 pInfo->aCol,
107709 sizeof(pInfo->aCol[0]),
107710 &pInfo->nColumn,
107711 &i
107712 );
107713 return i;
107714}
107715
107716/*
107717** Add a new element to the pAggInfo->aFunc[] array. Return the index of
107718** the new element. Return a negative number if malloc fails.
107719*/
107720static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
107721 int i;
107722 pInfo->aFunc = sqlite3ArrayAllocate(
107723 db,
107724 pInfo->aFunc,
107725 sizeof(pInfo->aFunc[0]),
107726 &pInfo->nFunc,
107727 &i
107728 );
107729 return i;
107730}
107731
107732/*
107733** This is the xExprCallback for a tree walker. It is used to
107734** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
107735** for additional information.
107736*/
107737static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
107738 int i;
107739 NameContext *pNC = pWalker->u.pNC;
107740 Parse *pParse = pNC->pParse;
107741 SrcList *pSrcList = pNC->pSrcList;
107742 AggInfo *pAggInfo = pNC->uNC.pAggInfo;
107743
107744 assert( pNC->ncFlags & NC_UAggInfo );
107745 switch( pExpr->op ){
107746 case TK_AGG_COLUMN:
107747 case TK_COLUMN: {
107748 testcase( pExpr->op==TK_AGG_COLUMN );
107749 testcase( pExpr->op==TK_COLUMN );
107750 /* Check to see if the column is in one of the tables in the FROM
107751 ** clause of the aggregate query */
107752 if( ALWAYS(pSrcList!=0) ){
107753 SrcItem *pItem = pSrcList->a;
107754 for(i=0; i<pSrcList->nSrc; i++, pItem++){
107755 struct AggInfo_col *pCol;
107756 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
107757 if( pExpr->iTable==pItem->iCursor ){
107758 /* If we reach this point, it means that pExpr refers to a table
107759 ** that is in the FROM clause of the aggregate query.
107760 **
107761 ** Make an entry for the column in pAggInfo->aCol[] if there
107762 ** is not an entry there already.
107763 */
107764 int k;
107765 pCol = pAggInfo->aCol;
107766 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
107767 if( pCol->iTable==pExpr->iTable &&
107768 pCol->iColumn==pExpr->iColumn ){
107769 break;
107770 }
107771 }
107772 if( (k>=pAggInfo->nColumn)
107773 && (k = addAggInfoColumn(db: pParse->db, pInfo: pAggInfo))>=0
107774 ){
107775 pCol = &pAggInfo->aCol[k];
107776 assert( ExprUseYTab(pExpr) );
107777 pCol->pTab = pExpr->y.pTab;
107778 pCol->iTable = pExpr->iTable;
107779 pCol->iColumn = pExpr->iColumn;
107780 pCol->iMem = ++pParse->nMem;
107781 pCol->iSorterColumn = -1;
107782 pCol->pCExpr = pExpr;
107783 if( pAggInfo->pGroupBy ){
107784 int j, n;
107785 ExprList *pGB = pAggInfo->pGroupBy;
107786 struct ExprList_item *pTerm = pGB->a;
107787 n = pGB->nExpr;
107788 for(j=0; j<n; j++, pTerm++){
107789 Expr *pE = pTerm->pExpr;
107790 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
107791 pE->iColumn==pExpr->iColumn ){
107792 pCol->iSorterColumn = j;
107793 break;
107794 }
107795 }
107796 }
107797 if( pCol->iSorterColumn<0 ){
107798 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
107799 }
107800 }
107801 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
107802 ** because it was there before or because we just created it).
107803 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
107804 ** pAggInfo->aCol[] entry.
107805 */
107806 ExprSetVVAProperty(pExpr, EP_NoReduce);
107807 pExpr->pAggInfo = pAggInfo;
107808 pExpr->op = TK_AGG_COLUMN;
107809 pExpr->iAgg = (i16)k;
107810 break;
107811 } /* endif pExpr->iTable==pItem->iCursor */
107812 } /* end loop over pSrcList */
107813 }
107814 return WRC_Prune;
107815 }
107816 case TK_AGG_FUNCTION: {
107817 if( (pNC->ncFlags & NC_InAggFunc)==0
107818 && pWalker->walkerDepth==pExpr->op2
107819 ){
107820 /* Check to see if pExpr is a duplicate of another aggregate
107821 ** function that is already in the pAggInfo structure
107822 */
107823 struct AggInfo_func *pItem = pAggInfo->aFunc;
107824 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
107825 if( pItem->pFExpr==pExpr ) break;
107826 if( sqlite3ExprCompare(pParse: 0, pA: pItem->pFExpr, pB: pExpr, iTab: -1)==0 ){
107827 break;
107828 }
107829 }
107830 if( i>=pAggInfo->nFunc ){
107831 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
107832 */
107833 u8 enc = ENC(pParse->db);
107834 i = addAggInfoFunc(db: pParse->db, pInfo: pAggInfo);
107835 if( i>=0 ){
107836 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
107837 pItem = &pAggInfo->aFunc[i];
107838 pItem->pFExpr = pExpr;
107839 pItem->iMem = ++pParse->nMem;
107840 assert( ExprUseUToken(pExpr) );
107841 pItem->pFunc = sqlite3FindFunction(pParse->db,
107842 pExpr->u.zToken,
107843 pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
107844 if( pExpr->flags & EP_Distinct ){
107845 pItem->iDistinct = pParse->nTab++;
107846 }else{
107847 pItem->iDistinct = -1;
107848 }
107849 }
107850 }
107851 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
107852 */
107853 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
107854 ExprSetVVAProperty(pExpr, EP_NoReduce);
107855 pExpr->iAgg = (i16)i;
107856 pExpr->pAggInfo = pAggInfo;
107857 return WRC_Prune;
107858 }else{
107859 return WRC_Continue;
107860 }
107861 }
107862 }
107863 return WRC_Continue;
107864}
107865
107866/*
107867** Analyze the pExpr expression looking for aggregate functions and
107868** for variables that need to be added to AggInfo object that pNC->pAggInfo
107869** points to. Additional entries are made on the AggInfo object as
107870** necessary.
107871**
107872** This routine should only be called after the expression has been
107873** analyzed by sqlite3ResolveExprNames().
107874*/
107875SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
107876 Walker w;
107877 w.xExprCallback = analyzeAggregate;
107878 w.xSelectCallback = sqlite3WalkerDepthIncrease;
107879 w.xSelectCallback2 = sqlite3WalkerDepthDecrease;
107880 w.walkerDepth = 0;
107881 w.u.pNC = pNC;
107882 w.pParse = 0;
107883 assert( pNC->pSrcList!=0 );
107884 sqlite3WalkExpr(pWalker: &w, pExpr);
107885}
107886
107887/*
107888** Call sqlite3ExprAnalyzeAggregates() for every expression in an
107889** expression list. Return the number of errors.
107890**
107891** If an error is found, the analysis is cut short.
107892*/
107893SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
107894 struct ExprList_item *pItem;
107895 int i;
107896 if( pList ){
107897 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
107898 sqlite3ExprAnalyzeAggregates(pNC, pExpr: pItem->pExpr);
107899 }
107900 }
107901}
107902
107903/*
107904** Allocate a single new register for use to hold some intermediate result.
107905*/
107906SQLITE_PRIVATE int sqlite3GetTempReg(Parse *pParse){
107907 if( pParse->nTempReg==0 ){
107908 return ++pParse->nMem;
107909 }
107910 return pParse->aTempReg[--pParse->nTempReg];
107911}
107912
107913/*
107914** Deallocate a register, making available for reuse for some other
107915** purpose.
107916*/
107917SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
107918 if( iReg ){
107919 sqlite3VdbeReleaseRegisters(pParse, iReg, 1, 0, 0);
107920 if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
107921 pParse->aTempReg[pParse->nTempReg++] = iReg;
107922 }
107923 }
107924}
107925
107926/*
107927** Allocate or deallocate a block of nReg consecutive registers.
107928*/
107929SQLITE_PRIVATE int sqlite3GetTempRange(Parse *pParse, int nReg){
107930 int i, n;
107931 if( nReg==1 ) return sqlite3GetTempReg(pParse);
107932 i = pParse->iRangeReg;
107933 n = pParse->nRangeReg;
107934 if( nReg<=n ){
107935 pParse->iRangeReg += nReg;
107936 pParse->nRangeReg -= nReg;
107937 }else{
107938 i = pParse->nMem+1;
107939 pParse->nMem += nReg;
107940 }
107941 return i;
107942}
107943SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
107944 if( nReg==1 ){
107945 sqlite3ReleaseTempReg(pParse, iReg);
107946 return;
107947 }
107948 sqlite3VdbeReleaseRegisters(pParse, iReg, nReg, 0, 0);
107949 if( nReg>pParse->nRangeReg ){
107950 pParse->nRangeReg = nReg;
107951 pParse->iRangeReg = iReg;
107952 }
107953}
107954
107955/*
107956** Mark all temporary registers as being unavailable for reuse.
107957**
107958** Always invoke this procedure after coding a subroutine or co-routine
107959** that might be invoked from other parts of the code, to ensure that
107960** the sub/co-routine does not use registers in common with the code that
107961** invokes the sub/co-routine.
107962*/
107963SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse *pParse){
107964 pParse->nTempReg = 0;
107965 pParse->nRangeReg = 0;
107966}
107967
107968/*
107969** Validate that no temporary register falls within the range of
107970** iFirst..iLast, inclusive. This routine is only call from within assert()
107971** statements.
107972*/
107973#ifdef SQLITE_DEBUG
107974SQLITE_PRIVATE int sqlite3NoTempsInRange(Parse *pParse, int iFirst, int iLast){
107975 int i;
107976 if( pParse->nRangeReg>0
107977 && pParse->iRangeReg+pParse->nRangeReg > iFirst
107978 && pParse->iRangeReg <= iLast
107979 ){
107980 return 0;
107981 }
107982 for(i=0; i<pParse->nTempReg; i++){
107983 if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){
107984 return 0;
107985 }
107986 }
107987 return 1;
107988}
107989#endif /* SQLITE_DEBUG */
107990
107991/************** End of expr.c ************************************************/
107992/************** Begin file alter.c *******************************************/
107993/*
107994** 2005 February 15
107995**
107996** The author disclaims copyright to this source code. In place of
107997** a legal notice, here is a blessing:
107998**
107999** May you do good and not evil.
108000** May you find forgiveness for yourself and forgive others.
108001** May you share freely, never taking more than you give.
108002**
108003*************************************************************************
108004** This file contains C code routines that used to generate VDBE code
108005** that implements the ALTER TABLE command.
108006*/
108007/* #include "sqliteInt.h" */
108008
108009/*
108010** The code in this file only exists if we are not omitting the
108011** ALTER TABLE logic from the build.
108012*/
108013#ifndef SQLITE_OMIT_ALTERTABLE
108014
108015/*
108016** Parameter zName is the name of a table that is about to be altered
108017** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN).
108018** If the table is a system table, this function leaves an error message
108019** in pParse->zErr (system tables may not be altered) and returns non-zero.
108020**
108021** Or, if zName is not a system table, zero is returned.
108022*/
108023static int isAlterableTable(Parse *pParse, Table *pTab){
108024 if( 0==sqlite3StrNICmp(zLeft: pTab->zName, zRight: "sqlite_", N: 7)
108025#ifndef SQLITE_OMIT_VIRTUALTABLE
108026 || (pTab->tabFlags & TF_Eponymous)!=0
108027 || ( (pTab->tabFlags & TF_Shadow)!=0
108028 && sqlite3ReadOnlyShadowTables(db: pParse->db)
108029 )
108030#endif
108031 ){
108032 sqlite3ErrorMsg(pParse, zFormat: "table %s may not be altered", pTab->zName);
108033 return 1;
108034 }
108035 return 0;
108036}
108037
108038/*
108039** Generate code to verify that the schemas of database zDb and, if
108040** bTemp is not true, database "temp", can still be parsed. This is
108041** called at the end of the generation of an ALTER TABLE ... RENAME ...
108042** statement to ensure that the operation has not rendered any schema
108043** objects unusable.
108044*/
108045static void renameTestSchema(
108046 Parse *pParse, /* Parse context */
108047 const char *zDb, /* Name of db to verify schema of */
108048 int bTemp, /* True if this is the temp db */
108049 const char *zWhen, /* "when" part of error message */
108050 int bNoDQS /* Do not allow DQS in the schema */
108051){
108052 pParse->colNamesSet = 1;
108053 sqlite3NestedParse(pParse,
108054 "SELECT 1 "
108055 "FROM \"%w\"." LEGACY_SCHEMA_TABLE " "
108056 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
108057 " AND sql NOT LIKE 'create virtual%%'"
108058 " AND sqlite_rename_test(%Q, sql, type, name, %d, %Q, %d)=NULL ",
108059 zDb,
108060 zDb, bTemp, zWhen, bNoDQS
108061 );
108062
108063 if( bTemp==0 ){
108064 sqlite3NestedParse(pParse,
108065 "SELECT 1 "
108066 "FROM temp." LEGACY_SCHEMA_TABLE " "
108067 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
108068 " AND sql NOT LIKE 'create virtual%%'"
108069 " AND sqlite_rename_test(%Q, sql, type, name, 1, %Q, %d)=NULL ",
108070 zDb, zWhen, bNoDQS
108071 );
108072 }
108073}
108074
108075/*
108076** Generate VM code to replace any double-quoted strings (but not double-quoted
108077** identifiers) within the "sql" column of the sqlite_schema table in
108078** database zDb with their single-quoted equivalents. If argument bTemp is
108079** not true, similarly update all SQL statements in the sqlite_schema table
108080** of the temp db.
108081*/
108082static void renameFixQuotes(Parse *pParse, const char *zDb, int bTemp){
108083 sqlite3NestedParse(pParse,
108084 "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE
108085 " SET sql = sqlite_rename_quotefix(%Q, sql)"
108086 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
108087 " AND sql NOT LIKE 'create virtual%%'" , zDb, zDb
108088 );
108089 if( bTemp==0 ){
108090 sqlite3NestedParse(pParse,
108091 "UPDATE temp." LEGACY_SCHEMA_TABLE
108092 " SET sql = sqlite_rename_quotefix('temp', sql)"
108093 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
108094 " AND sql NOT LIKE 'create virtual%%'"
108095 );
108096 }
108097}
108098
108099/*
108100** Generate code to reload the schema for database iDb. And, if iDb!=1, for
108101** the temp database as well.
108102*/
108103static void renameReloadSchema(Parse *pParse, int iDb, u16 p5){
108104 Vdbe *v = pParse->pVdbe;
108105 if( v ){
108106 sqlite3ChangeCookie(pParse, iDb);
108107 sqlite3VdbeAddParseSchemaOp(p: pParse->pVdbe, iDb, zWhere: 0, p5);
108108 if( iDb!=1 ) sqlite3VdbeAddParseSchemaOp(p: pParse->pVdbe, iDb: 1, zWhere: 0, p5);
108109 }
108110}
108111
108112/*
108113** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy"
108114** command.
108115*/
108116SQLITE_PRIVATE void sqlite3AlterRenameTable(
108117 Parse *pParse, /* Parser context. */
108118 SrcList *pSrc, /* The table to rename. */
108119 Token *pName /* The new table name. */
108120){
108121 int iDb; /* Database that contains the table */
108122 char *zDb; /* Name of database iDb */
108123 Table *pTab; /* Table being renamed */
108124 char *zName = 0; /* NULL-terminated version of pName */
108125 sqlite3 *db = pParse->db; /* Database connection */
108126 int nTabName; /* Number of UTF-8 characters in zTabName */
108127 const char *zTabName; /* Original name of the table */
108128 Vdbe *v;
108129 VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */
108130
108131 if( NEVER(db->mallocFailed) ) goto exit_rename_table;
108132 assert( pSrc->nSrc==1 );
108133 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
108134
108135 pTab = sqlite3LocateTableItem(pParse, flags: 0, &pSrc->a[0]);
108136 if( !pTab ) goto exit_rename_table;
108137 iDb = sqlite3SchemaToIndex(db: pParse->db, pTab->pSchema);
108138 zDb = db->aDb[iDb].zDbSName;
108139
108140 /* Get a NULL terminated version of the new table name. */
108141 zName = sqlite3NameFromToken(db, pName);
108142 if( !zName ) goto exit_rename_table;
108143
108144 /* Check that a table or index named 'zName' does not already exist
108145 ** in database iDb. If so, this is an error.
108146 */
108147 if( sqlite3FindTable(db, zName, zDb)
108148 || sqlite3FindIndex(db, zName, zDb)
108149 || sqlite3IsShadowTableOf(db, pTab, zName)
108150 ){
108151 sqlite3ErrorMsg(pParse,
108152 zFormat: "there is already another table or index with this name: %s", zName);
108153 goto exit_rename_table;
108154 }
108155
108156 /* Make sure it is not a system table being altered, or a reserved name
108157 ** that the table is being renamed to.
108158 */
108159 if( SQLITE_OK!=isAlterableTable(pParse, pTab) ){
108160 goto exit_rename_table;
108161 }
108162 if( SQLITE_OK!=sqlite3CheckObjectName(pParse,zName,"table",zName) ){
108163 goto exit_rename_table;
108164 }
108165
108166#ifndef SQLITE_OMIT_VIEW
108167 if( IsView(pTab) ){
108168 sqlite3ErrorMsg(pParse, zFormat: "view %s may not be altered", pTab->zName);
108169 goto exit_rename_table;
108170 }
108171#endif
108172
108173#ifndef SQLITE_OMIT_AUTHORIZATION
108174 /* Invoke the authorization callback. */
108175 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
108176 goto exit_rename_table;
108177 }
108178#endif
108179
108180#ifndef SQLITE_OMIT_VIRTUALTABLE
108181 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
108182 goto exit_rename_table;
108183 }
108184 if( IsVirtual(pTab) ){
108185 pVTab = sqlite3GetVTable(db, pTab);
108186 if( pVTab->pVtab->pModule->xRename==0 ){
108187 pVTab = 0;
108188 }
108189 }
108190#endif
108191
108192 /* Begin a transaction for database iDb. Then modify the schema cookie
108193 ** (since the ALTER TABLE modifies the schema). Call sqlite3MayAbort(),
108194 ** as the scalar functions (e.g. sqlite_rename_table()) invoked by the
108195 ** nested SQL may raise an exception. */
108196 v = sqlite3GetVdbe(pParse);
108197 if( v==0 ){
108198 goto exit_rename_table;
108199 }
108200 sqlite3MayAbort(pParse);
108201
108202 /* figure out how many UTF-8 characters are in zName */
108203 zTabName = pTab->zName;
108204 nTabName = sqlite3Utf8CharLen(zIn: zTabName, nByte: -1);
108205
108206 /* Rewrite all CREATE TABLE, INDEX, TRIGGER or VIEW statements in
108207 ** the schema to use the new table name. */
108208 sqlite3NestedParse(pParse,
108209 "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE " SET "
108210 "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, %d) "
108211 "WHERE (type!='index' OR tbl_name=%Q COLLATE nocase)"
108212 "AND name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
108213 , zDb, zDb, zTabName, zName, (iDb==1), zTabName
108214 );
108215
108216 /* Update the tbl_name and name columns of the sqlite_schema table
108217 ** as required. */
108218 sqlite3NestedParse(pParse,
108219 "UPDATE %Q." LEGACY_SCHEMA_TABLE " SET "
108220 "tbl_name = %Q, "
108221 "name = CASE "
108222 "WHEN type='table' THEN %Q "
108223 "WHEN name LIKE 'sqliteX_autoindex%%' ESCAPE 'X' "
108224 " AND type='index' THEN "
108225 "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
108226 "ELSE name END "
108227 "WHERE tbl_name=%Q COLLATE nocase AND "
108228 "(type='table' OR type='index' OR type='trigger');",
108229 zDb,
108230 zName, zName, zName,
108231 nTabName, zTabName
108232 );
108233
108234#ifndef SQLITE_OMIT_AUTOINCREMENT
108235 /* If the sqlite_sequence table exists in this database, then update
108236 ** it with the new table name.
108237 */
108238 if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
108239 sqlite3NestedParse(pParse,
108240 "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
108241 zDb, zName, pTab->zName);
108242 }
108243#endif
108244
108245 /* If the table being renamed is not itself part of the temp database,
108246 ** edit view and trigger definitions within the temp database
108247 ** as required. */
108248 if( iDb!=1 ){
108249 sqlite3NestedParse(pParse,
108250 "UPDATE sqlite_temp_schema SET "
108251 "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, 1), "
108252 "tbl_name = "
108253 "CASE WHEN tbl_name=%Q COLLATE nocase AND "
108254 " sqlite_rename_test(%Q, sql, type, name, 1, 'after rename', 0) "
108255 "THEN %Q ELSE tbl_name END "
108256 "WHERE type IN ('view', 'trigger')"
108257 , zDb, zTabName, zName, zTabName, zDb, zName);
108258 }
108259
108260 /* If this is a virtual table, invoke the xRename() function if
108261 ** one is defined. The xRename() callback will modify the names
108262 ** of any resources used by the v-table implementation (including other
108263 ** SQLite tables) that are identified by the name of the virtual table.
108264 */
108265#ifndef SQLITE_OMIT_VIRTUALTABLE
108266 if( pVTab ){
108267 int i = ++pParse->nMem;
108268 sqlite3VdbeLoadString(p: v, iDest: i, zStr: zName);
108269 sqlite3VdbeAddOp4(p: v, OP_VRename, p1: i, p2: 0, p3: 0,zP4: (const char*)pVTab, P4_VTAB);
108270 }
108271#endif
108272
108273 renameReloadSchema(pParse, iDb, INITFLAG_AlterRename);
108274 renameTestSchema(pParse, zDb, bTemp: iDb==1, zWhen: "after rename", bNoDQS: 0);
108275
108276exit_rename_table:
108277 sqlite3SrcListDelete(db, pSrc);
108278 sqlite3DbFree(db, p: zName);
108279}
108280
108281/*
108282** Write code that will raise an error if the table described by
108283** zDb and zTab is not empty.
108284*/
108285static void sqlite3ErrorIfNotEmpty(
108286 Parse *pParse, /* Parsing context */
108287 const char *zDb, /* Schema holding the table */
108288 const char *zTab, /* Table to check for empty */
108289 const char *zErr /* Error message text */
108290){
108291 sqlite3NestedParse(pParse,
108292 "SELECT raise(ABORT,%Q) FROM \"%w\".\"%w\"",
108293 zErr, zDb, zTab
108294 );
108295}
108296
108297/*
108298** This function is called after an "ALTER TABLE ... ADD" statement
108299** has been parsed. Argument pColDef contains the text of the new
108300** column definition.
108301**
108302** The Table structure pParse->pNewTable was extended to include
108303** the new column during parsing.
108304*/
108305SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
108306 Table *pNew; /* Copy of pParse->pNewTable */
108307 Table *pTab; /* Table being altered */
108308 int iDb; /* Database number */
108309 const char *zDb; /* Database name */
108310 const char *zTab; /* Table name */
108311 char *zCol; /* Null-terminated column definition */
108312 Column *pCol; /* The new column */
108313 Expr *pDflt; /* Default value for the new column */
108314 sqlite3 *db; /* The database connection; */
108315 Vdbe *v; /* The prepared statement under construction */
108316 int r1; /* Temporary registers */
108317
108318 db = pParse->db;
108319 if( pParse->nErr || db->mallocFailed ) return;
108320 pNew = pParse->pNewTable;
108321 assert( pNew );
108322
108323 assert( sqlite3BtreeHoldsAllMutexes(db) );
108324 iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
108325 zDb = db->aDb[iDb].zDbSName;
108326 zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */
108327 pCol = &pNew->aCol[pNew->nCol-1];
108328 pDflt = sqlite3ColumnExpr(pNew, pCol);
108329 pTab = sqlite3FindTable(db, zTab, zDb);
108330 assert( pTab );
108331
108332#ifndef SQLITE_OMIT_AUTHORIZATION
108333 /* Invoke the authorization callback. */
108334 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
108335 return;
108336 }
108337#endif
108338
108339
108340 /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
108341 ** If there is a NOT NULL constraint, then the default value for the
108342 ** column must not be NULL.
108343 */
108344 if( pCol->colFlags & COLFLAG_PRIMKEY ){
108345 sqlite3ErrorMsg(pParse, zFormat: "Cannot add a PRIMARY KEY column");
108346 return;
108347 }
108348 if( pNew->pIndex ){
108349 sqlite3ErrorMsg(pParse,
108350 zFormat: "Cannot add a UNIQUE column");
108351 return;
108352 }
108353 if( (pCol->colFlags & COLFLAG_GENERATED)==0 ){
108354 /* If the default value for the new column was specified with a
108355 ** literal NULL, then set pDflt to 0. This simplifies checking
108356 ** for an SQL NULL default below.
108357 */
108358 assert( pDflt==0 || pDflt->op==TK_SPAN );
108359 if( pDflt && pDflt->pLeft->op==TK_NULL ){
108360 pDflt = 0;
108361 }
108362 assert( IsOrdinaryTable(pNew) );
108363 if( (db->flags&SQLITE_ForeignKeys) && pNew->u.tab.pFKey && pDflt ){
108364 sqlite3ErrorIfNotEmpty(pParse, zDb, zTab,
108365 zErr: "Cannot add a REFERENCES column with non-NULL default value");
108366 }
108367 if( pCol->notNull && !pDflt ){
108368 sqlite3ErrorIfNotEmpty(pParse, zDb, zTab,
108369 zErr: "Cannot add a NOT NULL column with default value NULL");
108370 }
108371
108372
108373 /* Ensure the default expression is something that sqlite3ValueFromExpr()
108374 ** can handle (i.e. not CURRENT_TIME etc.)
108375 */
108376 if( pDflt ){
108377 sqlite3_value *pVal = 0;
108378 int rc;
108379 rc = sqlite3ValueFromExpr(db, pExpr: pDflt, SQLITE_UTF8, SQLITE_AFF_BLOB, ppVal: &pVal);
108380 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
108381 if( rc!=SQLITE_OK ){
108382 assert( db->mallocFailed == 1 );
108383 return;
108384 }
108385 if( !pVal ){
108386 sqlite3ErrorIfNotEmpty(pParse, zDb, zTab,
108387 zErr: "Cannot add a column with non-constant default");
108388 }
108389 sqlite3ValueFree(v: pVal);
108390 }
108391 }else if( pCol->colFlags & COLFLAG_STORED ){
108392 sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, zErr: "cannot add a STORED column");
108393 }
108394
108395
108396 /* Modify the CREATE TABLE statement. */
108397 zCol = sqlite3DbStrNDup(db, z: (char*)pColDef->z, n: pColDef->n);
108398 if( zCol ){
108399 char *zEnd = &zCol[pColDef->n-1];
108400 while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){
108401 *zEnd-- = '\0';
108402 }
108403 /* substr() operations on characters, but addColOffset is in bytes. So we
108404 ** have to use printf() to translate between these units: */
108405 assert( IsOrdinaryTable(pTab) );
108406 assert( IsOrdinaryTable(pNew) );
108407 sqlite3NestedParse(pParse,
108408 "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE " SET "
108409 "sql = printf('%%.%ds, ',sql) || %Q"
108410 " || substr(sql,1+length(printf('%%.%ds',sql))) "
108411 "WHERE type = 'table' AND name = %Q",
108412 zDb, pNew->u.tab.addColOffset, zCol, pNew->u.tab.addColOffset,
108413 zTab
108414 );
108415 sqlite3DbFree(db, p: zCol);
108416 }
108417
108418 v = sqlite3GetVdbe(pParse);
108419 if( v ){
108420 /* Make sure the schema version is at least 3. But do not upgrade
108421 ** from less than 3 to 4, as that will corrupt any preexisting DESC
108422 ** index.
108423 */
108424 r1 = sqlite3GetTempReg(pParse);
108425 sqlite3VdbeAddOp3(p: v, OP_ReadCookie, p1: iDb, p2: r1, BTREE_FILE_FORMAT);
108426 sqlite3VdbeUsesBtree(p: v, i: iDb);
108427 sqlite3VdbeAddOp2(p: v, OP_AddImm, p1: r1, p2: -2);
108428 sqlite3VdbeAddOp2(p: v, OP_IfPos, p1: r1, p2: sqlite3VdbeCurrentAddr(p: v)+2);
108429 VdbeCoverage(v);
108430 sqlite3VdbeAddOp3(p: v, OP_SetCookie, p1: iDb, BTREE_FILE_FORMAT, p3: 3);
108431 sqlite3ReleaseTempReg(pParse, iReg: r1);
108432
108433 /* Reload the table definition */
108434 renameReloadSchema(pParse, iDb, INITFLAG_AlterAdd);
108435
108436 /* Verify that constraints are still satisfied */
108437 if( pNew->pCheck!=0
108438 || (pCol->notNull && (pCol->colFlags & COLFLAG_GENERATED)!=0)
108439 ){
108440 sqlite3NestedParse(pParse,
108441 "SELECT CASE WHEN quick_check GLOB 'CHECK*'"
108442 " THEN raise(ABORT,'CHECK constraint failed')"
108443 " ELSE raise(ABORT,'NOT NULL constraint failed')"
108444 " END"
108445 " FROM pragma_quick_check(\"%w\",\"%w\")"
108446 " WHERE quick_check GLOB 'CHECK*' OR quick_check GLOB 'NULL*'",
108447 zTab, zDb
108448 );
108449 }
108450 }
108451}
108452
108453/*
108454** This function is called by the parser after the table-name in
108455** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
108456** pSrc is the full-name of the table being altered.
108457**
108458** This routine makes a (partial) copy of the Table structure
108459** for the table being altered and sets Parse.pNewTable to point
108460** to it. Routines called by the parser as the column definition
108461** is parsed (i.e. sqlite3AddColumn()) add the new Column data to
108462** the copy. The copy of the Table structure is deleted by tokenize.c
108463** after parsing is finished.
108464**
108465** Routine sqlite3AlterFinishAddColumn() will be called to complete
108466** coding the "ALTER TABLE ... ADD" statement.
108467*/
108468SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
108469 Table *pNew;
108470 Table *pTab;
108471 int iDb;
108472 int i;
108473 int nAlloc;
108474 sqlite3 *db = pParse->db;
108475
108476 /* Look up the table being altered. */
108477 assert( pParse->pNewTable==0 );
108478 assert( sqlite3BtreeHoldsAllMutexes(db) );
108479 if( db->mallocFailed ) goto exit_begin_add_column;
108480 pTab = sqlite3LocateTableItem(pParse, flags: 0, &pSrc->a[0]);
108481 if( !pTab ) goto exit_begin_add_column;
108482
108483#ifndef SQLITE_OMIT_VIRTUALTABLE
108484 if( IsVirtual(pTab) ){
108485 sqlite3ErrorMsg(pParse, zFormat: "virtual tables may not be altered");
108486 goto exit_begin_add_column;
108487 }
108488#endif
108489
108490 /* Make sure this is not an attempt to ALTER a view. */
108491 if( IsView(pTab) ){
108492 sqlite3ErrorMsg(pParse, zFormat: "Cannot add a column to a view");
108493 goto exit_begin_add_column;
108494 }
108495 if( SQLITE_OK!=isAlterableTable(pParse, pTab) ){
108496 goto exit_begin_add_column;
108497 }
108498
108499 sqlite3MayAbort(pParse);
108500 assert( IsOrdinaryTable(pTab) );
108501 assert( pTab->u.tab.addColOffset>0 );
108502 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
108503
108504 /* Put a copy of the Table struct in Parse.pNewTable for the
108505 ** sqlite3AddColumn() function and friends to modify. But modify
108506 ** the name by adding an "sqlite_altertab_" prefix. By adding this
108507 ** prefix, we insure that the name will not collide with an existing
108508 ** table because user table are not allowed to have the "sqlite_"
108509 ** prefix on their name.
108510 */
108511 pNew = (Table*)sqlite3DbMallocZero(db, n: sizeof(Table));
108512 if( !pNew ) goto exit_begin_add_column;
108513 pParse->pNewTable = pNew;
108514 pNew->nTabRef = 1;
108515 pNew->nCol = pTab->nCol;
108516 assert( pNew->nCol>0 );
108517 nAlloc = (((pNew->nCol-1)/8)*8)+8;
108518 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
108519 pNew->aCol = (Column*)sqlite3DbMallocZero(db, n: sizeof(Column)*nAlloc);
108520 pNew->zName = sqlite3MPrintf(db, zFormat: "sqlite_altertab_%s", pTab->zName);
108521 if( !pNew->aCol || !pNew->zName ){
108522 assert( db->mallocFailed );
108523 goto exit_begin_add_column;
108524 }
108525 memcpy(dest: pNew->aCol, src: pTab->aCol, n: sizeof(Column)*pNew->nCol);
108526 for(i=0; i<pNew->nCol; i++){
108527 Column *pCol = &pNew->aCol[i];
108528 pCol->zCnName = sqlite3DbStrDup(db, z: pCol->zCnName);
108529 pCol->hName = sqlite3StrIHash(z: pCol->zCnName);
108530 }
108531 assert( IsOrdinaryTable(pNew) );
108532 pNew->u.tab.pDfltList = sqlite3ExprListDup(db, p: pTab->u.tab.pDfltList, flags: 0);
108533 pNew->pSchema = db->aDb[iDb].pSchema;
108534 pNew->u.tab.addColOffset = pTab->u.tab.addColOffset;
108535 pNew->nTabRef = 1;
108536
108537exit_begin_add_column:
108538 sqlite3SrcListDelete(db, pSrc);
108539 return;
108540}
108541
108542/*
108543** Parameter pTab is the subject of an ALTER TABLE ... RENAME COLUMN
108544** command. This function checks if the table is a view or virtual
108545** table (columns of views or virtual tables may not be renamed). If so,
108546** it loads an error message into pParse and returns non-zero.
108547**
108548** Or, if pTab is not a view or virtual table, zero is returned.
108549*/
108550#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
108551static int isRealTable(Parse *pParse, Table *pTab, int bDrop){
108552 const char *zType = 0;
108553#ifndef SQLITE_OMIT_VIEW
108554 if( IsView(pTab) ){
108555 zType = "view";
108556 }
108557#endif
108558#ifndef SQLITE_OMIT_VIRTUALTABLE
108559 if( IsVirtual(pTab) ){
108560 zType = "virtual table";
108561 }
108562#endif
108563 if( zType ){
108564 sqlite3ErrorMsg(pParse, zFormat: "cannot %s %s \"%s\"",
108565 (bDrop ? "drop column from" : "rename columns of"),
108566 zType, pTab->zName
108567 );
108568 return 1;
108569 }
108570 return 0;
108571}
108572#else /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
108573# define isRealTable(x,y,z) (0)
108574#endif
108575
108576/*
108577** Handles the following parser reduction:
108578**
108579** cmd ::= ALTER TABLE pSrc RENAME COLUMN pOld TO pNew
108580*/
108581SQLITE_PRIVATE void sqlite3AlterRenameColumn(
108582 Parse *pParse, /* Parsing context */
108583 SrcList *pSrc, /* Table being altered. pSrc->nSrc==1 */
108584 Token *pOld, /* Name of column being changed */
108585 Token *pNew /* New column name */
108586){
108587 sqlite3 *db = pParse->db; /* Database connection */
108588 Table *pTab; /* Table being updated */
108589 int iCol; /* Index of column being renamed */
108590 char *zOld = 0; /* Old column name */
108591 char *zNew = 0; /* New column name */
108592 const char *zDb; /* Name of schema containing the table */
108593 int iSchema; /* Index of the schema */
108594 int bQuote; /* True to quote the new name */
108595
108596 /* Locate the table to be altered */
108597 pTab = sqlite3LocateTableItem(pParse, flags: 0, &pSrc->a[0]);
108598 if( !pTab ) goto exit_rename_column;
108599
108600 /* Cannot alter a system table */
108601 if( SQLITE_OK!=isAlterableTable(pParse, pTab) ) goto exit_rename_column;
108602 if( SQLITE_OK!=isRealTable(pParse, pTab, bDrop: 0) ) goto exit_rename_column;
108603
108604 /* Which schema holds the table to be altered */
108605 iSchema = sqlite3SchemaToIndex(db, pTab->pSchema);
108606 assert( iSchema>=0 );
108607 zDb = db->aDb[iSchema].zDbSName;
108608
108609#ifndef SQLITE_OMIT_AUTHORIZATION
108610 /* Invoke the authorization callback. */
108611 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
108612 goto exit_rename_column;
108613 }
108614#endif
108615
108616 /* Make sure the old name really is a column name in the table to be
108617 ** altered. Set iCol to be the index of the column being renamed */
108618 zOld = sqlite3NameFromToken(db, pOld);
108619 if( !zOld ) goto exit_rename_column;
108620 for(iCol=0; iCol<pTab->nCol; iCol++){
108621 if( 0==sqlite3StrICmp(zLeft: pTab->aCol[iCol].zCnName, zRight: zOld) ) break;
108622 }
108623 if( iCol==pTab->nCol ){
108624 sqlite3ErrorMsg(pParse, zFormat: "no such column: \"%s\"", zOld);
108625 goto exit_rename_column;
108626 }
108627
108628 /* Ensure the schema contains no double-quoted strings */
108629 renameTestSchema(pParse, zDb, bTemp: iSchema==1, zWhen: "", bNoDQS: 0);
108630 renameFixQuotes(pParse, zDb, bTemp: iSchema==1);
108631
108632 /* Do the rename operation using a recursive UPDATE statement that
108633 ** uses the sqlite_rename_column() SQL function to compute the new
108634 ** CREATE statement text for the sqlite_schema table.
108635 */
108636 sqlite3MayAbort(pParse);
108637 zNew = sqlite3NameFromToken(db, pNew);
108638 if( !zNew ) goto exit_rename_column;
108639 assert( pNew->n>0 );
108640 bQuote = sqlite3Isquote(pNew->z[0]);
108641 sqlite3NestedParse(pParse,
108642 "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE " SET "
108643 "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, %d) "
108644 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X' "
108645 " AND (type != 'index' OR tbl_name = %Q)",
108646 zDb,
108647 zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1,
108648 pTab->zName
108649 );
108650
108651 sqlite3NestedParse(pParse,
108652 "UPDATE temp." LEGACY_SCHEMA_TABLE " SET "
108653 "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, 1) "
108654 "WHERE type IN ('trigger', 'view')",
108655 zDb, pTab->zName, iCol, zNew, bQuote
108656 );
108657
108658 /* Drop and reload the database schema. */
108659 renameReloadSchema(pParse, iDb: iSchema, INITFLAG_AlterRename);
108660 renameTestSchema(pParse, zDb, bTemp: iSchema==1, zWhen: "after rename", bNoDQS: 1);
108661
108662 exit_rename_column:
108663 sqlite3SrcListDelete(db, pSrc);
108664 sqlite3DbFree(db, p: zOld);
108665 sqlite3DbFree(db, p: zNew);
108666 return;
108667}
108668
108669/*
108670** Each RenameToken object maps an element of the parse tree into
108671** the token that generated that element. The parse tree element
108672** might be one of:
108673**
108674** * A pointer to an Expr that represents an ID
108675** * The name of a table column in Column.zName
108676**
108677** A list of RenameToken objects can be constructed during parsing.
108678** Each new object is created by sqlite3RenameTokenMap().
108679** As the parse tree is transformed, the sqlite3RenameTokenRemap()
108680** routine is used to keep the mapping current.
108681**
108682** After the parse finishes, renameTokenFind() routine can be used
108683** to look up the actual token value that created some element in
108684** the parse tree.
108685*/
108686struct RenameToken {
108687 const void *p; /* Parse tree element created by token t */
108688 Token t; /* The token that created parse tree element p */
108689 RenameToken *pNext; /* Next is a list of all RenameToken objects */
108690};
108691
108692/*
108693** The context of an ALTER TABLE RENAME COLUMN operation that gets passed
108694** down into the Walker.
108695*/
108696typedef struct RenameCtx RenameCtx;
108697struct RenameCtx {
108698 RenameToken *pList; /* List of tokens to overwrite */
108699 int nList; /* Number of tokens in pList */
108700 int iCol; /* Index of column being renamed */
108701 Table *pTab; /* Table being ALTERed */
108702 const char *zOld; /* Old column name */
108703};
108704
108705#ifdef SQLITE_DEBUG
108706/*
108707** This function is only for debugging. It performs two tasks:
108708**
108709** 1. Checks that pointer pPtr does not already appear in the
108710** rename-token list.
108711**
108712** 2. Dereferences each pointer in the rename-token list.
108713**
108714** The second is most effective when debugging under valgrind or
108715** address-sanitizer or similar. If any of these pointers no longer
108716** point to valid objects, an exception is raised by the memory-checking
108717** tool.
108718**
108719** The point of this is to prevent comparisons of invalid pointer values.
108720** Even though this always seems to work, it is undefined according to the
108721** C standard. Example of undefined comparison:
108722**
108723** sqlite3_free(x);
108724** if( x==y ) ...
108725**
108726** Technically, as x no longer points into a valid object or to the byte
108727** following a valid object, it may not be used in comparison operations.
108728*/
108729static void renameTokenCheckAll(Parse *pParse, const void *pPtr){
108730 if( pParse->nErr==0 && pParse->db->mallocFailed==0 ){
108731 const RenameToken *p;
108732 u8 i = 0;
108733 for(p=pParse->pRename; p; p=p->pNext){
108734 if( p->p ){
108735 assert( p->p!=pPtr );
108736 i += *(u8*)(p->p);
108737 }
108738 }
108739 }
108740}
108741#else
108742# define renameTokenCheckAll(x,y)
108743#endif
108744
108745/*
108746** Remember that the parser tree element pPtr was created using
108747** the token pToken.
108748**
108749** In other words, construct a new RenameToken object and add it
108750** to the list of RenameToken objects currently being built up
108751** in pParse->pRename.
108752**
108753** The pPtr argument is returned so that this routine can be used
108754** with tail recursion in tokenExpr() routine, for a small performance
108755** improvement.
108756*/
108757SQLITE_PRIVATE const void *sqlite3RenameTokenMap(
108758 Parse *pParse,
108759 const void *pPtr,
108760 const Token *pToken
108761){
108762 RenameToken *pNew;
108763 assert( pPtr || pParse->db->mallocFailed );
108764 renameTokenCheckAll(pParse, pPtr);
108765 if( ALWAYS(pParse->eParseMode!=PARSE_MODE_UNMAP) ){
108766 pNew = sqlite3DbMallocZero(db: pParse->db, n: sizeof(RenameToken));
108767 if( pNew ){
108768 pNew->p = pPtr;
108769 pNew->t = *pToken;
108770 pNew->pNext = pParse->pRename;
108771 pParse->pRename = pNew;
108772 }
108773 }
108774
108775 return pPtr;
108776}
108777
108778/*
108779** It is assumed that there is already a RenameToken object associated
108780** with parse tree element pFrom. This function remaps the associated token
108781** to parse tree element pTo.
108782*/
108783SQLITE_PRIVATE void sqlite3RenameTokenRemap(Parse *pParse, const void *pTo, const void *pFrom){
108784 RenameToken *p;
108785 renameTokenCheckAll(pParse, pTo);
108786 for(p=pParse->pRename; p; p=p->pNext){
108787 if( p->p==pFrom ){
108788 p->p = pTo;
108789 break;
108790 }
108791 }
108792}
108793
108794/*
108795** Walker callback used by sqlite3RenameExprUnmap().
108796*/
108797static int renameUnmapExprCb(Walker *pWalker, Expr *pExpr){
108798 Parse *pParse = pWalker->pParse;
108799 sqlite3RenameTokenRemap(pParse, pTo: 0, pFrom: (const void*)pExpr);
108800 if( ExprUseYTab(pExpr) ){
108801 sqlite3RenameTokenRemap(pParse, pTo: 0, pFrom: (const void*)&pExpr->y.pTab);
108802 }
108803 return WRC_Continue;
108804}
108805
108806/*
108807** Iterate through the Select objects that are part of WITH clauses attached
108808** to select statement pSelect.
108809*/
108810static void renameWalkWith(Walker *pWalker, Select *pSelect){
108811 With *pWith = pSelect->pWith;
108812 if( pWith ){
108813 Parse *pParse = pWalker->pParse;
108814 int i;
108815 With *pCopy = 0;
108816 assert( pWith->nCte>0 );
108817 if( (pWith->a[0].pSelect->selFlags & SF_Expanded)==0 ){
108818 /* Push a copy of the With object onto the with-stack. We use a copy
108819 ** here as the original will be expanded and resolved (flags SF_Expanded
108820 ** and SF_Resolved) below. And the parser code that uses the with-stack
108821 ** fails if the Select objects on it have already been expanded and
108822 ** resolved. */
108823 pCopy = sqlite3WithDup(db: pParse->db, p: pWith);
108824 pCopy = sqlite3WithPush(pParse, pCopy, 1);
108825 }
108826 for(i=0; i<pWith->nCte; i++){
108827 Select *p = pWith->a[i].pSelect;
108828 NameContext sNC;
108829 memset(s: &sNC, c: 0, n: sizeof(sNC));
108830 sNC.pParse = pParse;
108831 if( pCopy ) sqlite3SelectPrep(sNC.pParse, p, &sNC);
108832 if( sNC.pParse->db->mallocFailed ) return;
108833 sqlite3WalkSelect(pWalker, p);
108834 sqlite3RenameExprlistUnmap(pParse, pWith->a[i].pCols);
108835 }
108836 if( pCopy && pParse->pWith==pCopy ){
108837 pParse->pWith = pCopy->pOuter;
108838 }
108839 }
108840}
108841
108842/*
108843** Unmap all tokens in the IdList object passed as the second argument.
108844*/
108845static void unmapColumnIdlistNames(
108846 Parse *pParse,
108847 const IdList *pIdList
108848){
108849 if( pIdList ){
108850 int ii;
108851 for(ii=0; ii<pIdList->nId; ii++){
108852 sqlite3RenameTokenRemap(pParse, pTo: 0, pFrom: (const void*)pIdList->a[ii].zName);
108853 }
108854 }
108855}
108856
108857/*
108858** Walker callback used by sqlite3RenameExprUnmap().
108859*/
108860static int renameUnmapSelectCb(Walker *pWalker, Select *p){
108861 Parse *pParse = pWalker->pParse;
108862 int i;
108863 if( pParse->nErr ) return WRC_Abort;
108864 testcase( p->selFlags & SF_View );
108865 testcase( p->selFlags & SF_CopyCte );
108866 if( p->selFlags & (SF_View|SF_CopyCte) ){
108867 return WRC_Prune;
108868 }
108869 if( ALWAYS(p->pEList) ){
108870 ExprList *pList = p->pEList;
108871 for(i=0; i<pList->nExpr; i++){
108872 if( pList->a[i].zEName && pList->a[i].eEName==ENAME_NAME ){
108873 sqlite3RenameTokenRemap(pParse, pTo: 0, pFrom: (void*)pList->a[i].zEName);
108874 }
108875 }
108876 }
108877 if( ALWAYS(p->pSrc) ){ /* Every Select as a SrcList, even if it is empty */
108878 SrcList *pSrc = p->pSrc;
108879 for(i=0; i<pSrc->nSrc; i++){
108880 sqlite3RenameTokenRemap(pParse, pTo: 0, pFrom: (void*)pSrc->a[i].zName);
108881 sqlite3WalkExpr(pWalker, pExpr: pSrc->a[i].pOn);
108882 unmapColumnIdlistNames(pParse, pIdList: pSrc->a[i].pUsing);
108883 }
108884 }
108885
108886 renameWalkWith(pWalker, pSelect: p);
108887 return WRC_Continue;
108888}
108889
108890/*
108891** Remove all nodes that are part of expression pExpr from the rename list.
108892*/
108893SQLITE_PRIVATE void sqlite3RenameExprUnmap(Parse *pParse, Expr *pExpr){
108894 u8 eMode = pParse->eParseMode;
108895 Walker sWalker;
108896 memset(s: &sWalker, c: 0, n: sizeof(Walker));
108897 sWalker.pParse = pParse;
108898 sWalker.xExprCallback = renameUnmapExprCb;
108899 sWalker.xSelectCallback = renameUnmapSelectCb;
108900 pParse->eParseMode = PARSE_MODE_UNMAP;
108901 sqlite3WalkExpr(pWalker: &sWalker, pExpr);
108902 pParse->eParseMode = eMode;
108903}
108904
108905/*
108906** Remove all nodes that are part of expression-list pEList from the
108907** rename list.
108908*/
108909SQLITE_PRIVATE void sqlite3RenameExprlistUnmap(Parse *pParse, ExprList *pEList){
108910 if( pEList ){
108911 int i;
108912 Walker sWalker;
108913 memset(s: &sWalker, c: 0, n: sizeof(Walker));
108914 sWalker.pParse = pParse;
108915 sWalker.xExprCallback = renameUnmapExprCb;
108916 sqlite3WalkExprList(pWalker: &sWalker, p: pEList);
108917 for(i=0; i<pEList->nExpr; i++){
108918 if( ALWAYS(pEList->a[i].eEName==ENAME_NAME) ){
108919 sqlite3RenameTokenRemap(pParse, pTo: 0, pFrom: (void*)pEList->a[i].zEName);
108920 }
108921 }
108922 }
108923}
108924
108925/*
108926** Free the list of RenameToken objects given in the second argument
108927*/
108928static void renameTokenFree(sqlite3 *db, RenameToken *pToken){
108929 RenameToken *pNext;
108930 RenameToken *p;
108931 for(p=pToken; p; p=pNext){
108932 pNext = p->pNext;
108933 sqlite3DbFree(db, p);
108934 }
108935}
108936
108937/*
108938** Search the Parse object passed as the first argument for a RenameToken
108939** object associated with parse tree element pPtr. If found, return a pointer
108940** to it. Otherwise, return NULL.
108941**
108942** If the second argument passed to this function is not NULL and a matching
108943** RenameToken object is found, remove it from the Parse object and add it to
108944** the list maintained by the RenameCtx object.
108945*/
108946static RenameToken *renameTokenFind(
108947 Parse *pParse,
108948 struct RenameCtx *pCtx,
108949 const void *pPtr
108950){
108951 RenameToken **pp;
108952 if( NEVER(pPtr==0) ){
108953 return 0;
108954 }
108955 for(pp=&pParse->pRename; (*pp); pp=&(*pp)->pNext){
108956 if( (*pp)->p==pPtr ){
108957 RenameToken *pToken = *pp;
108958 if( pCtx ){
108959 *pp = pToken->pNext;
108960 pToken->pNext = pCtx->pList;
108961 pCtx->pList = pToken;
108962 pCtx->nList++;
108963 }
108964 return pToken;
108965 }
108966 }
108967 return 0;
108968}
108969
108970/*
108971** This is a Walker select callback. It does nothing. It is only required
108972** because without a dummy callback, sqlite3WalkExpr() and similar do not
108973** descend into sub-select statements.
108974*/
108975static int renameColumnSelectCb(Walker *pWalker, Select *p){
108976 if( p->selFlags & (SF_View|SF_CopyCte) ){
108977 testcase( p->selFlags & SF_View );
108978 testcase( p->selFlags & SF_CopyCte );
108979 return WRC_Prune;
108980 }
108981 renameWalkWith(pWalker, pSelect: p);
108982 return WRC_Continue;
108983}
108984
108985/*
108986** This is a Walker expression callback.
108987**
108988** For every TK_COLUMN node in the expression tree, search to see
108989** if the column being references is the column being renamed by an
108990** ALTER TABLE statement. If it is, then attach its associated
108991** RenameToken object to the list of RenameToken objects being
108992** constructed in RenameCtx object at pWalker->u.pRename.
108993*/
108994static int renameColumnExprCb(Walker *pWalker, Expr *pExpr){
108995 RenameCtx *p = pWalker->u.pRename;
108996 if( pExpr->op==TK_TRIGGER
108997 && pExpr->iColumn==p->iCol
108998 && pWalker->pParse->pTriggerTab==p->pTab
108999 ){
109000 renameTokenFind(pParse: pWalker->pParse, pCtx: p, pPtr: (void*)pExpr);
109001 }else if( pExpr->op==TK_COLUMN
109002 && pExpr->iColumn==p->iCol
109003 && ALWAYS(ExprUseYTab(pExpr))
109004 && p->pTab==pExpr->y.pTab
109005 ){
109006 renameTokenFind(pParse: pWalker->pParse, pCtx: p, pPtr: (void*)pExpr);
109007 }
109008 return WRC_Continue;
109009}
109010
109011/*
109012** The RenameCtx contains a list of tokens that reference a column that
109013** is being renamed by an ALTER TABLE statement. Return the "last"
109014** RenameToken in the RenameCtx and remove that RenameToken from the
109015** RenameContext. "Last" means the last RenameToken encountered when
109016** the input SQL is parsed from left to right. Repeated calls to this routine
109017** return all column name tokens in the order that they are encountered
109018** in the SQL statement.
109019*/
109020static RenameToken *renameColumnTokenNext(RenameCtx *pCtx){
109021 RenameToken *pBest = pCtx->pList;
109022 RenameToken *pToken;
109023 RenameToken **pp;
109024
109025 for(pToken=pBest->pNext; pToken; pToken=pToken->pNext){
109026 if( pToken->t.z>pBest->t.z ) pBest = pToken;
109027 }
109028 for(pp=&pCtx->pList; *pp!=pBest; pp=&(*pp)->pNext);
109029 *pp = pBest->pNext;
109030
109031 return pBest;
109032}
109033
109034/*
109035** An error occured while parsing or otherwise processing a database
109036** object (either pParse->pNewTable, pNewIndex or pNewTrigger) as part of an
109037** ALTER TABLE RENAME COLUMN program. The error message emitted by the
109038** sub-routine is currently stored in pParse->zErrMsg. This function
109039** adds context to the error message and then stores it in pCtx.
109040*/
109041static void renameColumnParseError(
109042 sqlite3_context *pCtx,
109043 const char *zWhen,
109044 sqlite3_value *pType,
109045 sqlite3_value *pObject,
109046 Parse *pParse
109047){
109048 const char *zT = (const char*)sqlite3_value_text(pVal: pType);
109049 const char *zN = (const char*)sqlite3_value_text(pVal: pObject);
109050 char *zErr;
109051
109052 zErr = sqlite3_mprintf(zFormat: "error in %s %s%s%s: %s",
109053 zT, zN, (zWhen[0] ? " " : ""), zWhen,
109054 pParse->zErrMsg
109055 );
109056 sqlite3_result_error(pCtx, z: zErr, n: -1);
109057 sqlite3_free(p: zErr);
109058}
109059
109060/*
109061** For each name in the the expression-list pEList (i.e. each
109062** pEList->a[i].zName) that matches the string in zOld, extract the
109063** corresponding rename-token from Parse object pParse and add it
109064** to the RenameCtx pCtx.
109065*/
109066static void renameColumnElistNames(
109067 Parse *pParse,
109068 RenameCtx *pCtx,
109069 const ExprList *pEList,
109070 const char *zOld
109071){
109072 if( pEList ){
109073 int i;
109074 for(i=0; i<pEList->nExpr; i++){
109075 const char *zName = pEList->a[i].zEName;
109076 if( ALWAYS(pEList->a[i].eEName==ENAME_NAME)
109077 && ALWAYS(zName!=0)
109078 && 0==sqlite3_stricmp(zLeft: zName, zRight: zOld)
109079 ){
109080 renameTokenFind(pParse, pCtx, pPtr: (const void*)zName);
109081 }
109082 }
109083 }
109084}
109085
109086/*
109087** For each name in the the id-list pIdList (i.e. each pIdList->a[i].zName)
109088** that matches the string in zOld, extract the corresponding rename-token
109089** from Parse object pParse and add it to the RenameCtx pCtx.
109090*/
109091static void renameColumnIdlistNames(
109092 Parse *pParse,
109093 RenameCtx *pCtx,
109094 const IdList *pIdList,
109095 const char *zOld
109096){
109097 if( pIdList ){
109098 int i;
109099 for(i=0; i<pIdList->nId; i++){
109100 const char *zName = pIdList->a[i].zName;
109101 if( 0==sqlite3_stricmp(zLeft: zName, zRight: zOld) ){
109102 renameTokenFind(pParse, pCtx, pPtr: (const void*)zName);
109103 }
109104 }
109105 }
109106}
109107
109108
109109/*
109110** Parse the SQL statement zSql using Parse object (*p). The Parse object
109111** is initialized by this function before it is used.
109112*/
109113static int renameParseSql(
109114 Parse *p, /* Memory to use for Parse object */
109115 const char *zDb, /* Name of schema SQL belongs to */
109116 sqlite3 *db, /* Database handle */
109117 const char *zSql, /* SQL to parse */
109118 int bTemp /* True if SQL is from temp schema */
109119){
109120 int rc;
109121 char *zErr = 0;
109122
109123 db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
109124
109125 /* Parse the SQL statement passed as the first argument. If no error
109126 ** occurs and the parse does not result in a new table, index or
109127 ** trigger object, the database must be corrupt. */
109128 memset(s: p, c: 0, n: sizeof(Parse));
109129 p->eParseMode = PARSE_MODE_RENAME;
109130 p->db = db;
109131 p->nQueryLoop = 1;
109132 rc = zSql ? sqlite3RunParser(p, zSql, &zErr) : SQLITE_NOMEM;
109133 assert( p->zErrMsg==0 );
109134 assert( rc!=SQLITE_OK || zErr==0 );
109135 p->zErrMsg = zErr;
109136 if( db->mallocFailed ) rc = SQLITE_NOMEM;
109137 if( rc==SQLITE_OK
109138 && p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0
109139 ){
109140 rc = SQLITE_CORRUPT_BKPT;
109141 }
109142
109143#ifdef SQLITE_DEBUG
109144 /* Ensure that all mappings in the Parse.pRename list really do map to
109145 ** a part of the input string. */
109146 if( rc==SQLITE_OK ){
109147 int nSql = sqlite3Strlen30(zSql);
109148 RenameToken *pToken;
109149 for(pToken=p->pRename; pToken; pToken=pToken->pNext){
109150 assert( pToken->t.z>=zSql && &pToken->t.z[pToken->t.n]<=&zSql[nSql] );
109151 }
109152 }
109153#endif
109154
109155 db->init.iDb = 0;
109156 return rc;
109157}
109158
109159/*
109160** This function edits SQL statement zSql, replacing each token identified
109161** by the linked list pRename with the text of zNew. If argument bQuote is
109162** true, then zNew is always quoted first. If no error occurs, the result
109163** is loaded into context object pCtx as the result.
109164**
109165** Or, if an error occurs (i.e. an OOM condition), an error is left in
109166** pCtx and an SQLite error code returned.
109167*/
109168static int renameEditSql(
109169 sqlite3_context *pCtx, /* Return result here */
109170 RenameCtx *pRename, /* Rename context */
109171 const char *zSql, /* SQL statement to edit */
109172 const char *zNew, /* New token text */
109173 int bQuote /* True to always quote token */
109174){
109175 i64 nNew = sqlite3Strlen30(z: zNew);
109176 i64 nSql = sqlite3Strlen30(z: zSql);
109177 sqlite3 *db = sqlite3_context_db_handle(p: pCtx);
109178 int rc = SQLITE_OK;
109179 char *zQuot = 0;
109180 char *zOut;
109181 i64 nQuot = 0;
109182 char *zBuf1 = 0;
109183 char *zBuf2 = 0;
109184
109185 if( zNew ){
109186 /* Set zQuot to point to a buffer containing a quoted copy of the
109187 ** identifier zNew. If the corresponding identifier in the original
109188 ** ALTER TABLE statement was quoted (bQuote==1), then set zNew to
109189 ** point to zQuot so that all substitutions are made using the
109190 ** quoted version of the new column name. */
109191 zQuot = sqlite3MPrintf(db, zFormat: "\"%w\" ", zNew);
109192 if( zQuot==0 ){
109193 return SQLITE_NOMEM;
109194 }else{
109195 nQuot = sqlite3Strlen30(z: zQuot)-1;
109196 }
109197
109198 assert( nQuot>=nNew );
109199 zOut = sqlite3DbMallocZero(db, n: nSql + pRename->nList*nQuot + 1);
109200 }else{
109201 zOut = (char*)sqlite3DbMallocZero(db, n: (nSql*2+1) * 3);
109202 if( zOut ){
109203 zBuf1 = &zOut[nSql*2+1];
109204 zBuf2 = &zOut[nSql*4+2];
109205 }
109206 }
109207
109208 /* At this point pRename->pList contains a list of RenameToken objects
109209 ** corresponding to all tokens in the input SQL that must be replaced
109210 ** with the new column name, or with single-quoted versions of themselves.
109211 ** All that remains is to construct and return the edited SQL string. */
109212 if( zOut ){
109213 int nOut = nSql;
109214 memcpy(dest: zOut, src: zSql, n: nSql);
109215 while( pRename->pList ){
109216 int iOff; /* Offset of token to replace in zOut */
109217 u32 nReplace;
109218 const char *zReplace;
109219 RenameToken *pBest = renameColumnTokenNext(pCtx: pRename);
109220
109221 if( zNew ){
109222 if( bQuote==0 && sqlite3IsIdChar(*pBest->t.z) ){
109223 nReplace = nNew;
109224 zReplace = zNew;
109225 }else{
109226 nReplace = nQuot;
109227 zReplace = zQuot;
109228 if( pBest->t.z[pBest->t.n]=='"' ) nReplace++;
109229 }
109230 }else{
109231 /* Dequote the double-quoted token. Then requote it again, this time
109232 ** using single quotes. If the character immediately following the
109233 ** original token within the input SQL was a single quote ('), then
109234 ** add another space after the new, single-quoted version of the
109235 ** token. This is so that (SELECT "string"'alias') maps to
109236 ** (SELECT 'string' 'alias'), and not (SELECT 'string''alias'). */
109237 memcpy(dest: zBuf1, src: pBest->t.z, n: pBest->t.n);
109238 zBuf1[pBest->t.n] = 0;
109239 sqlite3Dequote(z: zBuf1);
109240 sqlite3_snprintf(n: nSql*2, zBuf: zBuf2, zFormat: "%Q%s", zBuf1,
109241 pBest->t.z[pBest->t.n]=='\'' ? " " : ""
109242 );
109243 zReplace = zBuf2;
109244 nReplace = sqlite3Strlen30(z: zReplace);
109245 }
109246
109247 iOff = pBest->t.z - zSql;
109248 if( pBest->t.n!=nReplace ){
109249 memmove(dest: &zOut[iOff + nReplace], src: &zOut[iOff + pBest->t.n],
109250 n: nOut - (iOff + pBest->t.n)
109251 );
109252 nOut += nReplace - pBest->t.n;
109253 zOut[nOut] = '\0';
109254 }
109255 memcpy(dest: &zOut[iOff], src: zReplace, n: nReplace);
109256 sqlite3DbFree(db, p: pBest);
109257 }
109258
109259 sqlite3_result_text(pCtx, z: zOut, n: -1, SQLITE_TRANSIENT);
109260 sqlite3DbFree(db, p: zOut);
109261 }else{
109262 rc = SQLITE_NOMEM;
109263 }
109264
109265 sqlite3_free(p: zQuot);
109266 return rc;
109267}
109268
109269/*
109270** Resolve all symbols in the trigger at pParse->pNewTrigger, assuming
109271** it was read from the schema of database zDb. Return SQLITE_OK if
109272** successful. Otherwise, return an SQLite error code and leave an error
109273** message in the Parse object.
109274*/
109275static int renameResolveTrigger(Parse *pParse){
109276 sqlite3 *db = pParse->db;
109277 Trigger *pNew = pParse->pNewTrigger;
109278 TriggerStep *pStep;
109279 NameContext sNC;
109280 int rc = SQLITE_OK;
109281
109282 memset(s: &sNC, c: 0, n: sizeof(sNC));
109283 sNC.pParse = pParse;
109284 assert( pNew->pTabSchema );
109285 pParse->pTriggerTab = sqlite3FindTable(db, pNew->table,
109286 db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName
109287 );
109288 pParse->eTriggerOp = pNew->op;
109289 /* ALWAYS() because if the table of the trigger does not exist, the
109290 ** error would have been hit before this point */
109291 if( ALWAYS(pParse->pTriggerTab) ){
109292 rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab);
109293 }
109294
109295 /* Resolve symbols in WHEN clause */
109296 if( rc==SQLITE_OK && pNew->pWhen ){
109297 rc = sqlite3ResolveExprNames(pNC: &sNC, pExpr: pNew->pWhen);
109298 }
109299
109300 for(pStep=pNew->step_list; rc==SQLITE_OK && pStep; pStep=pStep->pNext){
109301 if( pStep->pSelect ){
109302 sqlite3SelectPrep(pParse, pStep->pSelect, &sNC);
109303 if( pParse->nErr ) rc = pParse->rc;
109304 }
109305 if( rc==SQLITE_OK && pStep->zTarget ){
109306 SrcList *pSrc = sqlite3TriggerStepSrc(pParse, pStep);
109307 if( pSrc ){
109308 int i;
109309 for(i=0; i<pSrc->nSrc && rc==SQLITE_OK; i++){
109310 SrcItem *p = &pSrc->a[i];
109311 p->iCursor = pParse->nTab++;
109312 if( p->pSelect ){
109313 sqlite3SelectPrep(pParse, p->pSelect, 0);
109314 sqlite3ExpandSubquery(pParse, p);
109315 assert( i>0 );
109316 assert( pStep->pFrom->a[i-1].pSelect );
109317 sqlite3SelectPrep(pParse, pStep->pFrom->a[i-1].pSelect, 0);
109318 }else{
109319 p->pTab = sqlite3LocateTableItem(pParse, flags: 0, p);
109320 if( p->pTab==0 ){
109321 rc = SQLITE_ERROR;
109322 }else{
109323 p->pTab->nTabRef++;
109324 rc = sqlite3ViewGetColumnNames(pParse, p->pTab);
109325 }
109326 }
109327 }
109328 if( rc==SQLITE_OK && db->mallocFailed ){
109329 rc = SQLITE_NOMEM;
109330 }
109331 sNC.pSrcList = pSrc;
109332 if( rc==SQLITE_OK && pStep->pWhere ){
109333 rc = sqlite3ResolveExprNames(pNC: &sNC, pExpr: pStep->pWhere);
109334 }
109335 if( rc==SQLITE_OK ){
109336 rc = sqlite3ResolveExprListNames(pNC: &sNC, pList: pStep->pExprList);
109337 }
109338 assert( !pStep->pUpsert || (!pStep->pWhere && !pStep->pExprList) );
109339 if( pStep->pUpsert && rc==SQLITE_OK ){
109340 Upsert *pUpsert = pStep->pUpsert;
109341 pUpsert->pUpsertSrc = pSrc;
109342 sNC.uNC.pUpsert = pUpsert;
109343 sNC.ncFlags = NC_UUpsert;
109344 rc = sqlite3ResolveExprListNames(pNC: &sNC, pList: pUpsert->pUpsertTarget);
109345 if( rc==SQLITE_OK ){
109346 ExprList *pUpsertSet = pUpsert->pUpsertSet;
109347 rc = sqlite3ResolveExprListNames(pNC: &sNC, pList: pUpsertSet);
109348 }
109349 if( rc==SQLITE_OK ){
109350 rc = sqlite3ResolveExprNames(pNC: &sNC, pExpr: pUpsert->pUpsertWhere);
109351 }
109352 if( rc==SQLITE_OK ){
109353 rc = sqlite3ResolveExprNames(pNC: &sNC, pExpr: pUpsert->pUpsertTargetWhere);
109354 }
109355 sNC.ncFlags = 0;
109356 }
109357 sNC.pSrcList = 0;
109358 sqlite3SrcListDelete(db, pSrc);
109359 }else{
109360 rc = SQLITE_NOMEM;
109361 }
109362 }
109363 }
109364 return rc;
109365}
109366
109367/*
109368** Invoke sqlite3WalkExpr() or sqlite3WalkSelect() on all Select or Expr
109369** objects that are part of the trigger passed as the second argument.
109370*/
109371static void renameWalkTrigger(Walker *pWalker, Trigger *pTrigger){
109372 TriggerStep *pStep;
109373
109374 /* Find tokens to edit in WHEN clause */
109375 sqlite3WalkExpr(pWalker, pExpr: pTrigger->pWhen);
109376
109377 /* Find tokens to edit in trigger steps */
109378 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
109379 sqlite3WalkSelect(pWalker, p: pStep->pSelect);
109380 sqlite3WalkExpr(pWalker, pExpr: pStep->pWhere);
109381 sqlite3WalkExprList(pWalker, p: pStep->pExprList);
109382 if( pStep->pUpsert ){
109383 Upsert *pUpsert = pStep->pUpsert;
109384 sqlite3WalkExprList(pWalker, p: pUpsert->pUpsertTarget);
109385 sqlite3WalkExprList(pWalker, p: pUpsert->pUpsertSet);
109386 sqlite3WalkExpr(pWalker, pExpr: pUpsert->pUpsertWhere);
109387 sqlite3WalkExpr(pWalker, pExpr: pUpsert->pUpsertTargetWhere);
109388 }
109389 if( pStep->pFrom ){
109390 int i;
109391 for(i=0; i<pStep->pFrom->nSrc; i++){
109392 sqlite3WalkSelect(pWalker, p: pStep->pFrom->a[i].pSelect);
109393 }
109394 }
109395 }
109396}
109397
109398/*
109399** Free the contents of Parse object (*pParse). Do not free the memory
109400** occupied by the Parse object itself.
109401*/
109402static void renameParseCleanup(Parse *pParse){
109403 sqlite3 *db = pParse->db;
109404 Index *pIdx;
109405 if( pParse->pVdbe ){
109406 sqlite3VdbeFinalize(p: pParse->pVdbe);
109407 }
109408 sqlite3DeleteTable(db, pParse->pNewTable);
109409 while( (pIdx = pParse->pNewIndex)!=0 ){
109410 pParse->pNewIndex = pIdx->pNext;
109411 sqlite3FreeIndex(db, pIdx);
109412 }
109413 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
109414 sqlite3DbFree(db, p: pParse->zErrMsg);
109415 renameTokenFree(db, pToken: pParse->pRename);
109416 sqlite3ParserReset(pParse);
109417}
109418
109419/*
109420** SQL function:
109421**
109422** sqlite_rename_column(zSql, iCol, bQuote, zNew, zTable, zOld)
109423**
109424** 0. zSql: SQL statement to rewrite
109425** 1. type: Type of object ("table", "view" etc.)
109426** 2. object: Name of object
109427** 3. Database: Database name (e.g. "main")
109428** 4. Table: Table name
109429** 5. iCol: Index of column to rename
109430** 6. zNew: New column name
109431** 7. bQuote: Non-zero if the new column name should be quoted.
109432** 8. bTemp: True if zSql comes from temp schema
109433**
109434** Do a column rename operation on the CREATE statement given in zSql.
109435** The iCol-th column (left-most is 0) of table zTable is renamed from zCol
109436** into zNew. The name should be quoted if bQuote is true.
109437**
109438** This function is used internally by the ALTER TABLE RENAME COLUMN command.
109439** It is only accessible to SQL created using sqlite3NestedParse(). It is
109440** not reachable from ordinary SQL passed into sqlite3_prepare().
109441*/
109442static void renameColumnFunc(
109443 sqlite3_context *context,
109444 int NotUsed,
109445 sqlite3_value **argv
109446){
109447 sqlite3 *db = sqlite3_context_db_handle(p: context);
109448 RenameCtx sCtx;
109449 const char *zSql = (const char*)sqlite3_value_text(pVal: argv[0]);
109450 const char *zDb = (const char*)sqlite3_value_text(pVal: argv[3]);
109451 const char *zTable = (const char*)sqlite3_value_text(pVal: argv[4]);
109452 int iCol = sqlite3_value_int(pVal: argv[5]);
109453 const char *zNew = (const char*)sqlite3_value_text(pVal: argv[6]);
109454 int bQuote = sqlite3_value_int(pVal: argv[7]);
109455 int bTemp = sqlite3_value_int(pVal: argv[8]);
109456 const char *zOld;
109457 int rc;
109458 Parse sParse;
109459 Walker sWalker;
109460 Index *pIdx;
109461 int i;
109462 Table *pTab;
109463#ifndef SQLITE_OMIT_AUTHORIZATION
109464 sqlite3_xauth xAuth = db->xAuth;
109465#endif
109466
109467 UNUSED_PARAMETER(NotUsed);
109468 if( zSql==0 ) return;
109469 if( zTable==0 ) return;
109470 if( zNew==0 ) return;
109471 if( iCol<0 ) return;
109472 sqlite3BtreeEnterAll(db);
109473 pTab = sqlite3FindTable(db, zTable, zDb);
109474 if( pTab==0 || iCol>=pTab->nCol ){
109475 sqlite3BtreeLeaveAll(db);
109476 return;
109477 }
109478 zOld = pTab->aCol[iCol].zCnName;
109479 memset(s: &sCtx, c: 0, n: sizeof(sCtx));
109480 sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol);
109481
109482#ifndef SQLITE_OMIT_AUTHORIZATION
109483 db->xAuth = 0;
109484#endif
109485 rc = renameParseSql(p: &sParse, zDb, db, zSql, bTemp);
109486
109487 /* Find tokens that need to be replaced. */
109488 memset(s: &sWalker, c: 0, n: sizeof(Walker));
109489 sWalker.pParse = &sParse;
109490 sWalker.xExprCallback = renameColumnExprCb;
109491 sWalker.xSelectCallback = renameColumnSelectCb;
109492 sWalker.u.pRename = &sCtx;
109493
109494 sCtx.pTab = pTab;
109495 if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
109496 if( sParse.pNewTable ){
109497 if( IsView(sParse.pNewTable) ){
109498 Select *pSelect = sParse.pNewTable->u.view.pSelect;
109499 pSelect->selFlags &= ~SF_View;
109500 sParse.rc = SQLITE_OK;
109501 sqlite3SelectPrep(&sParse, pSelect, 0);
109502 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
109503 if( rc==SQLITE_OK ){
109504 sqlite3WalkSelect(pWalker: &sWalker, p: pSelect);
109505 }
109506 if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
109507 }else if( IsOrdinaryTable(sParse.pNewTable) ){
109508 /* A regular table */
109509 int bFKOnly = sqlite3_stricmp(zLeft: zTable, zRight: sParse.pNewTable->zName);
109510 FKey *pFKey;
109511 sCtx.pTab = sParse.pNewTable;
109512 if( bFKOnly==0 ){
109513 if( iCol<sParse.pNewTable->nCol ){
109514 renameTokenFind(
109515 pParse: &sParse, pCtx: &sCtx, pPtr: (void*)sParse.pNewTable->aCol[iCol].zCnName
109516 );
109517 }
109518 if( sCtx.iCol<0 ){
109519 renameTokenFind(pParse: &sParse, pCtx: &sCtx, pPtr: (void*)&sParse.pNewTable->iPKey);
109520 }
109521 sqlite3WalkExprList(pWalker: &sWalker, p: sParse.pNewTable->pCheck);
109522 for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){
109523 sqlite3WalkExprList(pWalker: &sWalker, p: pIdx->aColExpr);
109524 }
109525 for(pIdx=sParse.pNewIndex; pIdx; pIdx=pIdx->pNext){
109526 sqlite3WalkExprList(pWalker: &sWalker, p: pIdx->aColExpr);
109527 }
109528#ifndef SQLITE_OMIT_GENERATED_COLUMNS
109529 for(i=0; i<sParse.pNewTable->nCol; i++){
109530 Expr *pExpr = sqlite3ColumnExpr(sParse.pNewTable,
109531 &sParse.pNewTable->aCol[i]);
109532 sqlite3WalkExpr(pWalker: &sWalker, pExpr);
109533 }
109534#endif
109535 }
109536
109537 assert( IsOrdinaryTable(sParse.pNewTable) );
109538 for(pFKey=sParse.pNewTable->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
109539 for(i=0; i<pFKey->nCol; i++){
109540 if( bFKOnly==0 && pFKey->aCol[i].iFrom==iCol ){
109541 renameTokenFind(pParse: &sParse, pCtx: &sCtx, pPtr: (void*)&pFKey->aCol[i]);
109542 }
109543 if( 0==sqlite3_stricmp(zLeft: pFKey->zTo, zRight: zTable)
109544 && 0==sqlite3_stricmp(zLeft: pFKey->aCol[i].zCol, zRight: zOld)
109545 ){
109546 renameTokenFind(pParse: &sParse, pCtx: &sCtx, pPtr: (void*)pFKey->aCol[i].zCol);
109547 }
109548 }
109549 }
109550 }
109551 }else if( sParse.pNewIndex ){
109552 sqlite3WalkExprList(pWalker: &sWalker, p: sParse.pNewIndex->aColExpr);
109553 sqlite3WalkExpr(pWalker: &sWalker, pExpr: sParse.pNewIndex->pPartIdxWhere);
109554 }else{
109555 /* A trigger */
109556 TriggerStep *pStep;
109557 rc = renameResolveTrigger(pParse: &sParse);
109558 if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
109559
109560 for(pStep=sParse.pNewTrigger->step_list; pStep; pStep=pStep->pNext){
109561 if( pStep->zTarget ){
109562 Table *pTarget = sqlite3LocateTable(&sParse, flags: 0, pStep->zTarget, zDb);
109563 if( pTarget==pTab ){
109564 if( pStep->pUpsert ){
109565 ExprList *pUpsertSet = pStep->pUpsert->pUpsertSet;
109566 renameColumnElistNames(pParse: &sParse, pCtx: &sCtx, pEList: pUpsertSet, zOld);
109567 }
109568 renameColumnIdlistNames(pParse: &sParse, pCtx: &sCtx, pIdList: pStep->pIdList, zOld);
109569 renameColumnElistNames(pParse: &sParse, pCtx: &sCtx, pEList: pStep->pExprList, zOld);
109570 }
109571 }
109572 }
109573
109574
109575 /* Find tokens to edit in UPDATE OF clause */
109576 if( sParse.pTriggerTab==pTab ){
109577 renameColumnIdlistNames(pParse: &sParse, pCtx: &sCtx,pIdList: sParse.pNewTrigger->pColumns,zOld);
109578 }
109579
109580 /* Find tokens to edit in various expressions and selects */
109581 renameWalkTrigger(pWalker: &sWalker, pTrigger: sParse.pNewTrigger);
109582 }
109583
109584 assert( rc==SQLITE_OK );
109585 rc = renameEditSql(pCtx: context, pRename: &sCtx, zSql, zNew, bQuote);
109586
109587renameColumnFunc_done:
109588 if( rc!=SQLITE_OK ){
109589 if( sParse.zErrMsg ){
109590 renameColumnParseError(pCtx: context, zWhen: "", pType: argv[1], pObject: argv[2], pParse: &sParse);
109591 }else{
109592 sqlite3_result_error_code(pCtx: context, errCode: rc);
109593 }
109594 }
109595
109596 renameParseCleanup(pParse: &sParse);
109597 renameTokenFree(db, pToken: sCtx.pList);
109598#ifndef SQLITE_OMIT_AUTHORIZATION
109599 db->xAuth = xAuth;
109600#endif
109601 sqlite3BtreeLeaveAll(db);
109602}
109603
109604/*
109605** Walker expression callback used by "RENAME TABLE".
109606*/
109607static int renameTableExprCb(Walker *pWalker, Expr *pExpr){
109608 RenameCtx *p = pWalker->u.pRename;
109609 if( pExpr->op==TK_COLUMN
109610 && ALWAYS(ExprUseYTab(pExpr))
109611 && p->pTab==pExpr->y.pTab
109612 ){
109613 renameTokenFind(pParse: pWalker->pParse, pCtx: p, pPtr: (void*)&pExpr->y.pTab);
109614 }
109615 return WRC_Continue;
109616}
109617
109618/*
109619** Walker select callback used by "RENAME TABLE".
109620*/
109621static int renameTableSelectCb(Walker *pWalker, Select *pSelect){
109622 int i;
109623 RenameCtx *p = pWalker->u.pRename;
109624 SrcList *pSrc = pSelect->pSrc;
109625 if( pSelect->selFlags & (SF_View|SF_CopyCte) ){
109626 testcase( pSelect->selFlags & SF_View );
109627 testcase( pSelect->selFlags & SF_CopyCte );
109628 return WRC_Prune;
109629 }
109630 if( NEVER(pSrc==0) ){
109631 assert( pWalker->pParse->db->mallocFailed );
109632 return WRC_Abort;
109633 }
109634 for(i=0; i<pSrc->nSrc; i++){
109635 SrcItem *pItem = &pSrc->a[i];
109636 if( pItem->pTab==p->pTab ){
109637 renameTokenFind(pParse: pWalker->pParse, pCtx: p, pPtr: pItem->zName);
109638 }
109639 }
109640 renameWalkWith(pWalker, pSelect);
109641
109642 return WRC_Continue;
109643}
109644
109645
109646/*
109647** This C function implements an SQL user function that is used by SQL code
109648** generated by the ALTER TABLE ... RENAME command to modify the definition
109649** of any foreign key constraints that use the table being renamed as the
109650** parent table. It is passed three arguments:
109651**
109652** 0: The database containing the table being renamed.
109653** 1. type: Type of object ("table", "view" etc.)
109654** 2. object: Name of object
109655** 3: The complete text of the schema statement being modified,
109656** 4: The old name of the table being renamed, and
109657** 5: The new name of the table being renamed.
109658** 6: True if the schema statement comes from the temp db.
109659**
109660** It returns the new schema statement. For example:
109661**
109662** sqlite_rename_table('main', 'CREATE TABLE t1(a REFERENCES t2)','t2','t3',0)
109663** -> 'CREATE TABLE t1(a REFERENCES t3)'
109664*/
109665static void renameTableFunc(
109666 sqlite3_context *context,
109667 int NotUsed,
109668 sqlite3_value **argv
109669){
109670 sqlite3 *db = sqlite3_context_db_handle(p: context);
109671 const char *zDb = (const char*)sqlite3_value_text(pVal: argv[0]);
109672 const char *zInput = (const char*)sqlite3_value_text(pVal: argv[3]);
109673 const char *zOld = (const char*)sqlite3_value_text(pVal: argv[4]);
109674 const char *zNew = (const char*)sqlite3_value_text(pVal: argv[5]);
109675 int bTemp = sqlite3_value_int(pVal: argv[6]);
109676 UNUSED_PARAMETER(NotUsed);
109677
109678 if( zInput && zOld && zNew ){
109679 Parse sParse;
109680 int rc;
109681 int bQuote = 1;
109682 RenameCtx sCtx;
109683 Walker sWalker;
109684
109685#ifndef SQLITE_OMIT_AUTHORIZATION
109686 sqlite3_xauth xAuth = db->xAuth;
109687 db->xAuth = 0;
109688#endif
109689
109690 sqlite3BtreeEnterAll(db);
109691
109692 memset(s: &sCtx, c: 0, n: sizeof(RenameCtx));
109693 sCtx.pTab = sqlite3FindTable(db, zOld, zDb);
109694 memset(s: &sWalker, c: 0, n: sizeof(Walker));
109695 sWalker.pParse = &sParse;
109696 sWalker.xExprCallback = renameTableExprCb;
109697 sWalker.xSelectCallback = renameTableSelectCb;
109698 sWalker.u.pRename = &sCtx;
109699
109700 rc = renameParseSql(p: &sParse, zDb, db, zSql: zInput, bTemp);
109701
109702 if( rc==SQLITE_OK ){
109703 int isLegacy = (db->flags & SQLITE_LegacyAlter);
109704 if( sParse.pNewTable ){
109705 Table *pTab = sParse.pNewTable;
109706
109707 if( IsView(pTab) ){
109708 if( isLegacy==0 ){
109709 Select *pSelect = pTab->u.view.pSelect;
109710 NameContext sNC;
109711 memset(s: &sNC, c: 0, n: sizeof(sNC));
109712 sNC.pParse = &sParse;
109713
109714 assert( pSelect->selFlags & SF_View );
109715 pSelect->selFlags &= ~SF_View;
109716 sqlite3SelectPrep(&sParse, pTab->u.view.pSelect, &sNC);
109717 if( sParse.nErr ){
109718 rc = sParse.rc;
109719 }else{
109720 sqlite3WalkSelect(pWalker: &sWalker, p: pTab->u.view.pSelect);
109721 }
109722 }
109723 }else{
109724 /* Modify any FK definitions to point to the new table. */
109725#ifndef SQLITE_OMIT_FOREIGN_KEY
109726 if( (isLegacy==0 || (db->flags & SQLITE_ForeignKeys))
109727 && !IsVirtual(pTab)
109728 ){
109729 FKey *pFKey;
109730 assert( IsOrdinaryTable(pTab) );
109731 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
109732 if( sqlite3_stricmp(zLeft: pFKey->zTo, zRight: zOld)==0 ){
109733 renameTokenFind(pParse: &sParse, pCtx: &sCtx, pPtr: (void*)pFKey->zTo);
109734 }
109735 }
109736 }
109737#endif
109738
109739 /* If this is the table being altered, fix any table refs in CHECK
109740 ** expressions. Also update the name that appears right after the
109741 ** "CREATE [VIRTUAL] TABLE" bit. */
109742 if( sqlite3_stricmp(zLeft: zOld, zRight: pTab->zName)==0 ){
109743 sCtx.pTab = pTab;
109744 if( isLegacy==0 ){
109745 sqlite3WalkExprList(pWalker: &sWalker, p: pTab->pCheck);
109746 }
109747 renameTokenFind(pParse: &sParse, pCtx: &sCtx, pPtr: pTab->zName);
109748 }
109749 }
109750 }
109751
109752 else if( sParse.pNewIndex ){
109753 renameTokenFind(pParse: &sParse, pCtx: &sCtx, pPtr: sParse.pNewIndex->zName);
109754 if( isLegacy==0 ){
109755 sqlite3WalkExpr(pWalker: &sWalker, pExpr: sParse.pNewIndex->pPartIdxWhere);
109756 }
109757 }
109758
109759#ifndef SQLITE_OMIT_TRIGGER
109760 else{
109761 Trigger *pTrigger = sParse.pNewTrigger;
109762 TriggerStep *pStep;
109763 if( 0==sqlite3_stricmp(zLeft: sParse.pNewTrigger->table, zRight: zOld)
109764 && sCtx.pTab->pSchema==pTrigger->pTabSchema
109765 ){
109766 renameTokenFind(pParse: &sParse, pCtx: &sCtx, pPtr: sParse.pNewTrigger->table);
109767 }
109768
109769 if( isLegacy==0 ){
109770 rc = renameResolveTrigger(pParse: &sParse);
109771 if( rc==SQLITE_OK ){
109772 renameWalkTrigger(pWalker: &sWalker, pTrigger);
109773 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
109774 if( pStep->zTarget && 0==sqlite3_stricmp(zLeft: pStep->zTarget, zRight: zOld) ){
109775 renameTokenFind(pParse: &sParse, pCtx: &sCtx, pPtr: pStep->zTarget);
109776 }
109777 }
109778 }
109779 }
109780 }
109781#endif
109782 }
109783
109784 if( rc==SQLITE_OK ){
109785 rc = renameEditSql(pCtx: context, pRename: &sCtx, zSql: zInput, zNew, bQuote);
109786 }
109787 if( rc!=SQLITE_OK ){
109788 if( sParse.zErrMsg ){
109789 renameColumnParseError(pCtx: context, zWhen: "", pType: argv[1], pObject: argv[2], pParse: &sParse);
109790 }else{
109791 sqlite3_result_error_code(pCtx: context, errCode: rc);
109792 }
109793 }
109794
109795 renameParseCleanup(pParse: &sParse);
109796 renameTokenFree(db, pToken: sCtx.pList);
109797 sqlite3BtreeLeaveAll(db);
109798#ifndef SQLITE_OMIT_AUTHORIZATION
109799 db->xAuth = xAuth;
109800#endif
109801 }
109802
109803 return;
109804}
109805
109806static int renameQuotefixExprCb(Walker *pWalker, Expr *pExpr){
109807 if( pExpr->op==TK_STRING && (pExpr->flags & EP_DblQuoted) ){
109808 renameTokenFind(pParse: pWalker->pParse, pCtx: pWalker->u.pRename, pPtr: (const void*)pExpr);
109809 }
109810 return WRC_Continue;
109811}
109812
109813/*
109814** The implementation of an SQL scalar function that rewrites DDL statements
109815** so that any string literals that use double-quotes are modified so that
109816** they use single quotes.
109817**
109818** Two arguments must be passed:
109819**
109820** 0: Database name ("main", "temp" etc.).
109821** 1: SQL statement to edit.
109822**
109823** The returned value is the modified SQL statement. For example, given
109824** the database schema:
109825**
109826** CREATE TABLE t1(a, b, c);
109827**
109828** SELECT sqlite_rename_quotefix('main',
109829** 'CREATE VIEW v1 AS SELECT "a", "string" FROM t1'
109830** );
109831**
109832** returns the string:
109833**
109834** CREATE VIEW v1 AS SELECT "a", 'string' FROM t1
109835*/
109836static void renameQuotefixFunc(
109837 sqlite3_context *context,
109838 int NotUsed,
109839 sqlite3_value **argv
109840){
109841 sqlite3 *db = sqlite3_context_db_handle(p: context);
109842 char const *zDb = (const char*)sqlite3_value_text(pVal: argv[0]);
109843 char const *zInput = (const char*)sqlite3_value_text(pVal: argv[1]);
109844
109845#ifndef SQLITE_OMIT_AUTHORIZATION
109846 sqlite3_xauth xAuth = db->xAuth;
109847 db->xAuth = 0;
109848#endif
109849
109850 sqlite3BtreeEnterAll(db);
109851
109852 UNUSED_PARAMETER(NotUsed);
109853 if( zDb && zInput ){
109854 int rc;
109855 Parse sParse;
109856 rc = renameParseSql(p: &sParse, zDb, db, zSql: zInput, bTemp: 0);
109857
109858 if( rc==SQLITE_OK ){
109859 RenameCtx sCtx;
109860 Walker sWalker;
109861
109862 /* Walker to find tokens that need to be replaced. */
109863 memset(s: &sCtx, c: 0, n: sizeof(RenameCtx));
109864 memset(s: &sWalker, c: 0, n: sizeof(Walker));
109865 sWalker.pParse = &sParse;
109866 sWalker.xExprCallback = renameQuotefixExprCb;
109867 sWalker.xSelectCallback = renameColumnSelectCb;
109868 sWalker.u.pRename = &sCtx;
109869
109870 if( sParse.pNewTable ){
109871 if( IsView(sParse.pNewTable) ){
109872 Select *pSelect = sParse.pNewTable->u.view.pSelect;
109873 pSelect->selFlags &= ~SF_View;
109874 sParse.rc = SQLITE_OK;
109875 sqlite3SelectPrep(&sParse, pSelect, 0);
109876 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
109877 if( rc==SQLITE_OK ){
109878 sqlite3WalkSelect(pWalker: &sWalker, p: pSelect);
109879 }
109880 }else{
109881 int i;
109882 sqlite3WalkExprList(pWalker: &sWalker, p: sParse.pNewTable->pCheck);
109883#ifndef SQLITE_OMIT_GENERATED_COLUMNS
109884 for(i=0; i<sParse.pNewTable->nCol; i++){
109885 sqlite3WalkExpr(pWalker: &sWalker,
109886 pExpr: sqlite3ColumnExpr(sParse.pNewTable,
109887 &sParse.pNewTable->aCol[i]));
109888 }
109889#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
109890 }
109891 }else if( sParse.pNewIndex ){
109892 sqlite3WalkExprList(pWalker: &sWalker, p: sParse.pNewIndex->aColExpr);
109893 sqlite3WalkExpr(pWalker: &sWalker, pExpr: sParse.pNewIndex->pPartIdxWhere);
109894 }else{
109895#ifndef SQLITE_OMIT_TRIGGER
109896 rc = renameResolveTrigger(pParse: &sParse);
109897 if( rc==SQLITE_OK ){
109898 renameWalkTrigger(pWalker: &sWalker, pTrigger: sParse.pNewTrigger);
109899 }
109900#endif /* SQLITE_OMIT_TRIGGER */
109901 }
109902
109903 if( rc==SQLITE_OK ){
109904 rc = renameEditSql(pCtx: context, pRename: &sCtx, zSql: zInput, zNew: 0, bQuote: 0);
109905 }
109906 renameTokenFree(db, pToken: sCtx.pList);
109907 }
109908 if( rc!=SQLITE_OK ){
109909 sqlite3_result_error_code(pCtx: context, errCode: rc);
109910 }
109911 renameParseCleanup(pParse: &sParse);
109912 }
109913
109914#ifndef SQLITE_OMIT_AUTHORIZATION
109915 db->xAuth = xAuth;
109916#endif
109917
109918 sqlite3BtreeLeaveAll(db);
109919}
109920
109921/*
109922** An SQL user function that checks that there are no parse or symbol
109923** resolution problems in a CREATE TRIGGER|TABLE|VIEW|INDEX statement.
109924** After an ALTER TABLE .. RENAME operation is performed and the schema
109925** reloaded, this function is called on each SQL statement in the schema
109926** to ensure that it is still usable.
109927**
109928** 0: Database name ("main", "temp" etc.).
109929** 1: SQL statement.
109930** 2: Object type ("view", "table", "trigger" or "index").
109931** 3: Object name.
109932** 4: True if object is from temp schema.
109933** 5: "when" part of error message.
109934** 6: True to disable the DQS quirk when parsing SQL.
109935**
109936** Unless it finds an error, this function normally returns NULL. However, it
109937** returns integer value 1 if:
109938**
109939** * the SQL argument creates a trigger, and
109940** * the table that the trigger is attached to is in database zDb.
109941*/
109942static void renameTableTest(
109943 sqlite3_context *context,
109944 int NotUsed,
109945 sqlite3_value **argv
109946){
109947 sqlite3 *db = sqlite3_context_db_handle(p: context);
109948 char const *zDb = (const char*)sqlite3_value_text(pVal: argv[0]);
109949 char const *zInput = (const char*)sqlite3_value_text(pVal: argv[1]);
109950 int bTemp = sqlite3_value_int(pVal: argv[4]);
109951 int isLegacy = (db->flags & SQLITE_LegacyAlter);
109952 char const *zWhen = (const char*)sqlite3_value_text(pVal: argv[5]);
109953 int bNoDQS = sqlite3_value_int(pVal: argv[6]);
109954
109955#ifndef SQLITE_OMIT_AUTHORIZATION
109956 sqlite3_xauth xAuth = db->xAuth;
109957 db->xAuth = 0;
109958#endif
109959
109960 UNUSED_PARAMETER(NotUsed);
109961
109962 if( zDb && zInput ){
109963 int rc;
109964 Parse sParse;
109965 int flags = db->flags;
109966 if( bNoDQS ) db->flags &= ~(SQLITE_DqsDML|SQLITE_DqsDDL);
109967 rc = renameParseSql(p: &sParse, zDb, db, zSql: zInput, bTemp);
109968 db->flags |= (flags & (SQLITE_DqsDML|SQLITE_DqsDDL));
109969 if( rc==SQLITE_OK ){
109970 if( isLegacy==0 && sParse.pNewTable && IsView(sParse.pNewTable) ){
109971 NameContext sNC;
109972 memset(s: &sNC, c: 0, n: sizeof(sNC));
109973 sNC.pParse = &sParse;
109974 sqlite3SelectPrep(&sParse, sParse.pNewTable->u.view.pSelect, &sNC);
109975 if( sParse.nErr ) rc = sParse.rc;
109976 }
109977
109978 else if( sParse.pNewTrigger ){
109979 if( isLegacy==0 ){
109980 rc = renameResolveTrigger(pParse: &sParse);
109981 }
109982 if( rc==SQLITE_OK ){
109983 int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema);
109984 int i2 = sqlite3FindDbName(db, zDb);
109985 if( i1==i2 ) sqlite3_result_int(pCtx: context, iVal: 1);
109986 }
109987 }
109988 }
109989
109990 if( rc!=SQLITE_OK && zWhen ){
109991 renameColumnParseError(pCtx: context, zWhen, pType: argv[2], pObject: argv[3],pParse: &sParse);
109992 }
109993 renameParseCleanup(pParse: &sParse);
109994 }
109995
109996#ifndef SQLITE_OMIT_AUTHORIZATION
109997 db->xAuth = xAuth;
109998#endif
109999}
110000
110001/*
110002** The implementation of internal UDF sqlite_drop_column().
110003**
110004** Arguments:
110005**
110006** argv[0]: An integer - the index of the schema containing the table
110007** argv[1]: CREATE TABLE statement to modify.
110008** argv[2]: An integer - the index of the column to remove.
110009**
110010** The value returned is a string containing the CREATE TABLE statement
110011** with column argv[2] removed.
110012*/
110013static void dropColumnFunc(
110014 sqlite3_context *context,
110015 int NotUsed,
110016 sqlite3_value **argv
110017){
110018 sqlite3 *db = sqlite3_context_db_handle(p: context);
110019 int iSchema = sqlite3_value_int(pVal: argv[0]);
110020 const char *zSql = (const char*)sqlite3_value_text(pVal: argv[1]);
110021 int iCol = sqlite3_value_int(pVal: argv[2]);
110022 const char *zDb = db->aDb[iSchema].zDbSName;
110023 int rc;
110024 Parse sParse;
110025 RenameToken *pCol;
110026 Table *pTab;
110027 const char *zEnd;
110028 char *zNew = 0;
110029
110030#ifndef SQLITE_OMIT_AUTHORIZATION
110031 sqlite3_xauth xAuth = db->xAuth;
110032 db->xAuth = 0;
110033#endif
110034
110035 UNUSED_PARAMETER(NotUsed);
110036 rc = renameParseSql(p: &sParse, zDb, db, zSql, bTemp: iSchema==1);
110037 if( rc!=SQLITE_OK ) goto drop_column_done;
110038 pTab = sParse.pNewTable;
110039 if( pTab==0 || pTab->nCol==1 || iCol>=pTab->nCol ){
110040 /* This can happen if the sqlite_schema table is corrupt */
110041 rc = SQLITE_CORRUPT_BKPT;
110042 goto drop_column_done;
110043 }
110044
110045 pCol = renameTokenFind(pParse: &sParse, pCtx: 0, pPtr: (void*)pTab->aCol[iCol].zCnName);
110046 if( iCol<pTab->nCol-1 ){
110047 RenameToken *pEnd;
110048 pEnd = renameTokenFind(pParse: &sParse, pCtx: 0, pPtr: (void*)pTab->aCol[iCol+1].zCnName);
110049 zEnd = (const char*)pEnd->t.z;
110050 }else{
110051 assert( IsOrdinaryTable(pTab) );
110052 zEnd = (const char*)&zSql[pTab->u.tab.addColOffset];
110053 while( ALWAYS(pCol->t.z[0]!=0) && pCol->t.z[0]!=',' ) pCol->t.z--;
110054 }
110055
110056 zNew = sqlite3MPrintf(db, zFormat: "%.*s%s", pCol->t.z-zSql, zSql, zEnd);
110057 sqlite3_result_text(pCtx: context, z: zNew, n: -1, SQLITE_TRANSIENT);
110058 sqlite3_free(p: zNew);
110059
110060drop_column_done:
110061 renameParseCleanup(pParse: &sParse);
110062#ifndef SQLITE_OMIT_AUTHORIZATION
110063 db->xAuth = xAuth;
110064#endif
110065 if( rc!=SQLITE_OK ){
110066 sqlite3_result_error_code(pCtx: context, errCode: rc);
110067 }
110068}
110069
110070/*
110071** This function is called by the parser upon parsing an
110072**
110073** ALTER TABLE pSrc DROP COLUMN pName
110074**
110075** statement. Argument pSrc contains the possibly qualified name of the
110076** table being edited, and token pName the name of the column to drop.
110077*/
110078SQLITE_PRIVATE void sqlite3AlterDropColumn(Parse *pParse, SrcList *pSrc, const Token *pName){
110079 sqlite3 *db = pParse->db; /* Database handle */
110080 Table *pTab; /* Table to modify */
110081 int iDb; /* Index of db containing pTab in aDb[] */
110082 const char *zDb; /* Database containing pTab ("main" etc.) */
110083 char *zCol = 0; /* Name of column to drop */
110084 int iCol; /* Index of column zCol in pTab->aCol[] */
110085
110086 /* Look up the table being altered. */
110087 assert( pParse->pNewTable==0 );
110088 assert( sqlite3BtreeHoldsAllMutexes(db) );
110089 if( NEVER(db->mallocFailed) ) goto exit_drop_column;
110090 pTab = sqlite3LocateTableItem(pParse, flags: 0, &pSrc->a[0]);
110091 if( !pTab ) goto exit_drop_column;
110092
110093 /* Make sure this is not an attempt to ALTER a view, virtual table or
110094 ** system table. */
110095 if( SQLITE_OK!=isAlterableTable(pParse, pTab) ) goto exit_drop_column;
110096 if( SQLITE_OK!=isRealTable(pParse, pTab, bDrop: 1) ) goto exit_drop_column;
110097
110098 /* Find the index of the column being dropped. */
110099 zCol = sqlite3NameFromToken(db, pName);
110100 if( zCol==0 ){
110101 assert( db->mallocFailed );
110102 goto exit_drop_column;
110103 }
110104 iCol = sqlite3ColumnIndex(pTab, zCol);
110105 if( iCol<0 ){
110106 sqlite3ErrorMsg(pParse, zFormat: "no such column: \"%s\"", zCol);
110107 goto exit_drop_column;
110108 }
110109
110110 /* Do not allow the user to drop a PRIMARY KEY column or a column
110111 ** constrained by a UNIQUE constraint. */
110112 if( pTab->aCol[iCol].colFlags & (COLFLAG_PRIMKEY|COLFLAG_UNIQUE) ){
110113 sqlite3ErrorMsg(pParse, zFormat: "cannot drop %s column: \"%s\"",
110114 (pTab->aCol[iCol].colFlags&COLFLAG_PRIMKEY) ? "PRIMARY KEY" : "UNIQUE",
110115 zCol
110116 );
110117 goto exit_drop_column;
110118 }
110119
110120 /* Do not allow the number of columns to go to zero */
110121 if( pTab->nCol<=1 ){
110122 sqlite3ErrorMsg(pParse, zFormat: "cannot drop column \"%s\": no other columns exist",zCol);
110123 goto exit_drop_column;
110124 }
110125
110126 /* Edit the sqlite_schema table */
110127 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
110128 assert( iDb>=0 );
110129 zDb = db->aDb[iDb].zDbSName;
110130 renameTestSchema(pParse, zDb, bTemp: iDb==1, zWhen: "", bNoDQS: 0);
110131 renameFixQuotes(pParse, zDb, bTemp: iDb==1);
110132 sqlite3NestedParse(pParse,
110133 "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE " SET "
110134 "sql = sqlite_drop_column(%d, sql, %d) "
110135 "WHERE (type=='table' AND tbl_name=%Q COLLATE nocase)"
110136 , zDb, iDb, iCol, pTab->zName
110137 );
110138
110139 /* Drop and reload the database schema. */
110140 renameReloadSchema(pParse, iDb, INITFLAG_AlterDrop);
110141 renameTestSchema(pParse, zDb, bTemp: iDb==1, zWhen: "after drop column", bNoDQS: 1);
110142
110143 /* Edit rows of table on disk */
110144 if( pParse->nErr==0 && (pTab->aCol[iCol].colFlags & COLFLAG_VIRTUAL)==0 ){
110145 int i;
110146 int addr;
110147 int reg;
110148 int regRec;
110149 Index *pPk = 0;
110150 int nField = 0; /* Number of non-virtual columns after drop */
110151 int iCur;
110152 Vdbe *v = sqlite3GetVdbe(pParse);
110153 iCur = pParse->nTab++;
110154 sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite);
110155 addr = sqlite3VdbeAddOp1(p: v, OP_Rewind, p1: iCur); VdbeCoverage(v);
110156 reg = ++pParse->nMem;
110157 if( HasRowid(pTab) ){
110158 sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: iCur, p2: reg);
110159 pParse->nMem += pTab->nCol;
110160 }else{
110161 pPk = sqlite3PrimaryKeyIndex(pTab);
110162 pParse->nMem += pPk->nColumn;
110163 for(i=0; i<pPk->nKeyCol; i++){
110164 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iCur, p2: i, p3: reg+i+1);
110165 }
110166 nField = pPk->nKeyCol;
110167 }
110168 regRec = ++pParse->nMem;
110169 for(i=0; i<pTab->nCol; i++){
110170 if( i!=iCol && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
110171 int regOut;
110172 if( pPk ){
110173 int iPos = sqlite3TableColumnToIndex(pPk, i);
110174 int iColPos = sqlite3TableColumnToIndex(pPk, iCol);
110175 if( iPos<pPk->nKeyCol ) continue;
110176 regOut = reg+1+iPos-(iPos>iColPos);
110177 }else{
110178 regOut = reg+1+nField;
110179 }
110180 if( i==pTab->iPKey ){
110181 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: regOut);
110182 }else{
110183 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur: iCur, iCol: i, regOut);
110184 }
110185 nField++;
110186 }
110187 }
110188 if( nField==0 ){
110189 /* dbsqlfuzz 5f09e7bcc78b4954d06bf9f2400d7715f48d1fef */
110190 pParse->nMem++;
110191 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: reg+1);
110192 nField = 1;
110193 }
110194 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: reg+1, p2: nField, p3: regRec);
110195 if( pPk ){
110196 sqlite3VdbeAddOp4Int(p: v, OP_IdxInsert, p1: iCur, p2: regRec, p3: reg+1, p4: pPk->nKeyCol);
110197 }else{
110198 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: iCur, p2: regRec, p3: reg);
110199 }
110200 sqlite3VdbeChangeP5(p: v, OPFLAG_SAVEPOSITION);
110201
110202 sqlite3VdbeAddOp2(p: v, OP_Next, p1: iCur, p2: addr+1); VdbeCoverage(v);
110203 sqlite3VdbeJumpHere(p: v, addr);
110204 }
110205
110206exit_drop_column:
110207 sqlite3DbFree(db, p: zCol);
110208 sqlite3SrcListDelete(db, pSrc);
110209}
110210
110211/*
110212** Register built-in functions used to help implement ALTER TABLE
110213*/
110214SQLITE_PRIVATE void sqlite3AlterFunctions(void){
110215 static FuncDef aAlterTableFuncs[] = {
110216 INTERNAL_FUNCTION(sqlite_rename_column, 9, renameColumnFunc),
110217 INTERNAL_FUNCTION(sqlite_rename_table, 7, renameTableFunc),
110218 INTERNAL_FUNCTION(sqlite_rename_test, 7, renameTableTest),
110219 INTERNAL_FUNCTION(sqlite_drop_column, 3, dropColumnFunc),
110220 INTERNAL_FUNCTION(sqlite_rename_quotefix,2, renameQuotefixFunc),
110221 };
110222 sqlite3InsertBuiltinFuncs(aAlterTableFuncs, ArraySize(aAlterTableFuncs));
110223}
110224#endif /* SQLITE_ALTER_TABLE */
110225
110226/************** End of alter.c ***********************************************/
110227/************** Begin file analyze.c *****************************************/
110228/*
110229** 2005-07-08
110230**
110231** The author disclaims copyright to this source code. In place of
110232** a legal notice, here is a blessing:
110233**
110234** May you do good and not evil.
110235** May you find forgiveness for yourself and forgive others.
110236** May you share freely, never taking more than you give.
110237**
110238*************************************************************************
110239** This file contains code associated with the ANALYZE command.
110240**
110241** The ANALYZE command gather statistics about the content of tables
110242** and indices. These statistics are made available to the query planner
110243** to help it make better decisions about how to perform queries.
110244**
110245** The following system tables are or have been supported:
110246**
110247** CREATE TABLE sqlite_stat1(tbl, idx, stat);
110248** CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
110249** CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
110250** CREATE TABLE sqlite_stat4(tbl, idx, nEq, nLt, nDLt, sample);
110251**
110252** Additional tables might be added in future releases of SQLite.
110253** The sqlite_stat2 table is not created or used unless the SQLite version
110254** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
110255** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated.
110256** The sqlite_stat2 table is superseded by sqlite_stat3, which is only
110257** created and used by SQLite versions 3.7.9 through 3.29.0 when
110258** SQLITE_ENABLE_STAT3 defined. The functionality of sqlite_stat3
110259** is a superset of sqlite_stat2 and is also now deprecated. The
110260** sqlite_stat4 is an enhanced version of sqlite_stat3 and is only
110261** available when compiled with SQLITE_ENABLE_STAT4 and in SQLite
110262** versions 3.8.1 and later. STAT4 is the only variant that is still
110263** supported.
110264**
110265** For most applications, sqlite_stat1 provides all the statistics required
110266** for the query planner to make good choices.
110267**
110268** Format of sqlite_stat1:
110269**
110270** There is normally one row per index, with the index identified by the
110271** name in the idx column. The tbl column is the name of the table to
110272** which the index belongs. In each such row, the stat column will be
110273** a string consisting of a list of integers. The first integer in this
110274** list is the number of rows in the index. (This is the same as the
110275** number of rows in the table, except for partial indices.) The second
110276** integer is the average number of rows in the index that have the same
110277** value in the first column of the index. The third integer is the average
110278** number of rows in the index that have the same value for the first two
110279** columns. The N-th integer (for N>1) is the average number of rows in
110280** the index which have the same value for the first N-1 columns. For
110281** a K-column index, there will be K+1 integers in the stat column. If
110282** the index is unique, then the last integer will be 1.
110283**
110284** The list of integers in the stat column can optionally be followed
110285** by the keyword "unordered". The "unordered" keyword, if it is present,
110286** must be separated from the last integer by a single space. If the
110287** "unordered" keyword is present, then the query planner assumes that
110288** the index is unordered and will not use the index for a range query.
110289**
110290** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat
110291** column contains a single integer which is the (estimated) number of
110292** rows in the table identified by sqlite_stat1.tbl.
110293**
110294** Format of sqlite_stat2:
110295**
110296** The sqlite_stat2 is only created and is only used if SQLite is compiled
110297** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between
110298** 3.6.18 and 3.7.8. The "stat2" table contains additional information
110299** about the distribution of keys within an index. The index is identified by
110300** the "idx" column and the "tbl" column is the name of the table to which
110301** the index belongs. There are usually 10 rows in the sqlite_stat2
110302** table for each index.
110303**
110304** The sqlite_stat2 entries for an index that have sampleno between 0 and 9
110305** inclusive are samples of the left-most key value in the index taken at
110306** evenly spaced points along the index. Let the number of samples be S
110307** (10 in the standard build) and let C be the number of rows in the index.
110308** Then the sampled rows are given by:
110309**
110310** rownumber = (i*C*2 + C)/(S*2)
110311**
110312** For i between 0 and S-1. Conceptually, the index space is divided into
110313** S uniform buckets and the samples are the middle row from each bucket.
110314**
110315** The format for sqlite_stat2 is recorded here for legacy reference. This
110316** version of SQLite does not support sqlite_stat2. It neither reads nor
110317** writes the sqlite_stat2 table. This version of SQLite only supports
110318** sqlite_stat3.
110319**
110320** Format for sqlite_stat3:
110321**
110322** The sqlite_stat3 format is a subset of sqlite_stat4. Hence, the
110323** sqlite_stat4 format will be described first. Further information
110324** about sqlite_stat3 follows the sqlite_stat4 description.
110325**
110326** Format for sqlite_stat4:
110327**
110328** As with sqlite_stat2, the sqlite_stat4 table contains histogram data
110329** to aid the query planner in choosing good indices based on the values
110330** that indexed columns are compared against in the WHERE clauses of
110331** queries.
110332**
110333** The sqlite_stat4 table contains multiple entries for each index.
110334** The idx column names the index and the tbl column is the table of the
110335** index. If the idx and tbl columns are the same, then the sample is
110336** of the INTEGER PRIMARY KEY. The sample column is a blob which is the
110337** binary encoding of a key from the index. The nEq column is a
110338** list of integers. The first integer is the approximate number
110339** of entries in the index whose left-most column exactly matches
110340** the left-most column of the sample. The second integer in nEq
110341** is the approximate number of entries in the index where the
110342** first two columns match the first two columns of the sample.
110343** And so forth. nLt is another list of integers that show the approximate
110344** number of entries that are strictly less than the sample. The first
110345** integer in nLt contains the number of entries in the index where the
110346** left-most column is less than the left-most column of the sample.
110347** The K-th integer in the nLt entry is the number of index entries
110348** where the first K columns are less than the first K columns of the
110349** sample. The nDLt column is like nLt except that it contains the
110350** number of distinct entries in the index that are less than the
110351** sample.
110352**
110353** There can be an arbitrary number of sqlite_stat4 entries per index.
110354** The ANALYZE command will typically generate sqlite_stat4 tables
110355** that contain between 10 and 40 samples which are distributed across
110356** the key space, though not uniformly, and which include samples with
110357** large nEq values.
110358**
110359** Format for sqlite_stat3 redux:
110360**
110361** The sqlite_stat3 table is like sqlite_stat4 except that it only
110362** looks at the left-most column of the index. The sqlite_stat3.sample
110363** column contains the actual value of the left-most column instead
110364** of a blob encoding of the complete index key as is found in
110365** sqlite_stat4.sample. The nEq, nLt, and nDLt entries of sqlite_stat3
110366** all contain just a single integer which is the same as the first
110367** integer in the equivalent columns in sqlite_stat4.
110368*/
110369#ifndef SQLITE_OMIT_ANALYZE
110370/* #include "sqliteInt.h" */
110371
110372#if defined(SQLITE_ENABLE_STAT4)
110373# define IsStat4 1
110374#else
110375# define IsStat4 0
110376# undef SQLITE_STAT4_SAMPLES
110377# define SQLITE_STAT4_SAMPLES 1
110378#endif
110379
110380/*
110381** This routine generates code that opens the sqlite_statN tables.
110382** The sqlite_stat1 table is always relevant. sqlite_stat2 is now
110383** obsolete. sqlite_stat3 and sqlite_stat4 are only opened when
110384** appropriate compile-time options are provided.
110385**
110386** If the sqlite_statN tables do not previously exist, it is created.
110387**
110388** Argument zWhere may be a pointer to a buffer containing a table name,
110389** or it may be a NULL pointer. If it is not NULL, then all entries in
110390** the sqlite_statN tables associated with the named table are deleted.
110391** If zWhere==0, then code is generated to delete all stat table entries.
110392*/
110393static void openStatTable(
110394 Parse *pParse, /* Parsing context */
110395 int iDb, /* The database we are looking in */
110396 int iStatCur, /* Open the sqlite_stat1 table on this cursor */
110397 const char *zWhere, /* Delete entries for this table or index */
110398 const char *zWhereType /* Either "tbl" or "idx" */
110399){
110400 static const struct {
110401 const char *zName;
110402 const char *zCols;
110403 } aTable[] = {
110404 { "sqlite_stat1", "tbl,idx,stat" },
110405#if defined(SQLITE_ENABLE_STAT4)
110406 { "sqlite_stat4", "tbl,idx,neq,nlt,ndlt,sample" },
110407#else
110408 { "sqlite_stat4", 0 },
110409#endif
110410 { "sqlite_stat3", 0 },
110411 };
110412 int i;
110413 sqlite3 *db = pParse->db;
110414 Db *pDb;
110415 Vdbe *v = sqlite3GetVdbe(pParse);
110416 u32 aRoot[ArraySize(aTable)];
110417 u8 aCreateTbl[ArraySize(aTable)];
110418#ifdef SQLITE_ENABLE_STAT4
110419 const int nToOpen = OptimizationEnabled(db,SQLITE_Stat4) ? 2 : 1;
110420#else
110421 const int nToOpen = 1;
110422#endif
110423
110424 if( v==0 ) return;
110425 assert( sqlite3BtreeHoldsAllMutexes(db) );
110426 assert( sqlite3VdbeDb(v)==db );
110427 pDb = &db->aDb[iDb];
110428
110429 /* Create new statistic tables if they do not exist, or clear them
110430 ** if they do already exist.
110431 */
110432 for(i=0; i<ArraySize(aTable); i++){
110433 const char *zTab = aTable[i].zName;
110434 Table *pStat;
110435 aCreateTbl[i] = 0;
110436 if( (pStat = sqlite3FindTable(db, zTab, pDb->zDbSName))==0 ){
110437 if( i<nToOpen ){
110438 /* The sqlite_statN table does not exist. Create it. Note that a
110439 ** side-effect of the CREATE TABLE statement is to leave the rootpage
110440 ** of the new table in register pParse->regRoot. This is important
110441 ** because the OpenWrite opcode below will be needing it. */
110442 sqlite3NestedParse(pParse,
110443 "CREATE TABLE %Q.%s(%s)", pDb->zDbSName, zTab, aTable[i].zCols
110444 );
110445 aRoot[i] = (u32)pParse->regRoot;
110446 aCreateTbl[i] = OPFLAG_P2ISREG;
110447 }
110448 }else{
110449 /* The table already exists. If zWhere is not NULL, delete all entries
110450 ** associated with the table zWhere. If zWhere is NULL, delete the
110451 ** entire contents of the table. */
110452 aRoot[i] = pStat->tnum;
110453 sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
110454 if( zWhere ){
110455 sqlite3NestedParse(pParse,
110456 "DELETE FROM %Q.%s WHERE %s=%Q",
110457 pDb->zDbSName, zTab, zWhereType, zWhere
110458 );
110459#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
110460 }else if( db->xPreUpdateCallback ){
110461 sqlite3NestedParse(pParse, "DELETE FROM %Q.%s", pDb->zDbSName, zTab);
110462#endif
110463 }else{
110464 /* The sqlite_stat[134] table already exists. Delete all rows. */
110465 sqlite3VdbeAddOp2(p: v, OP_Clear, p1: (int)aRoot[i], p2: iDb);
110466 }
110467 }
110468 }
110469
110470 /* Open the sqlite_stat[134] tables for writing. */
110471 for(i=0; i<nToOpen; i++){
110472 assert( i<ArraySize(aTable) );
110473 sqlite3VdbeAddOp4Int(p: v, OP_OpenWrite, p1: iStatCur+i, p2: (int)aRoot[i], p3: iDb, p4: 3);
110474 sqlite3VdbeChangeP5(p: v, p5: aCreateTbl[i]);
110475 VdbeComment((v, aTable[i].zName));
110476 }
110477}
110478
110479/*
110480** Recommended number of samples for sqlite_stat4
110481*/
110482#ifndef SQLITE_STAT4_SAMPLES
110483# define SQLITE_STAT4_SAMPLES 24
110484#endif
110485
110486/*
110487** Three SQL functions - stat_init(), stat_push(), and stat_get() -
110488** share an instance of the following structure to hold their state
110489** information.
110490*/
110491typedef struct StatAccum StatAccum;
110492typedef struct StatSample StatSample;
110493struct StatSample {
110494 tRowcnt *anEq; /* sqlite_stat4.nEq */
110495 tRowcnt *anDLt; /* sqlite_stat4.nDLt */
110496#ifdef SQLITE_ENABLE_STAT4
110497 tRowcnt *anLt; /* sqlite_stat4.nLt */
110498 union {
110499 i64 iRowid; /* Rowid in main table of the key */
110500 u8 *aRowid; /* Key for WITHOUT ROWID tables */
110501 } u;
110502 u32 nRowid; /* Sizeof aRowid[] */
110503 u8 isPSample; /* True if a periodic sample */
110504 int iCol; /* If !isPSample, the reason for inclusion */
110505 u32 iHash; /* Tiebreaker hash */
110506#endif
110507};
110508struct StatAccum {
110509 sqlite3 *db; /* Database connection, for malloc() */
110510 tRowcnt nEst; /* Estimated number of rows */
110511 tRowcnt nRow; /* Number of rows visited so far */
110512 int nLimit; /* Analysis row-scan limit */
110513 int nCol; /* Number of columns in index + pk/rowid */
110514 int nKeyCol; /* Number of index columns w/o the pk/rowid */
110515 u8 nSkipAhead; /* Number of times of skip-ahead */
110516 StatSample current; /* Current row as a StatSample */
110517#ifdef SQLITE_ENABLE_STAT4
110518 tRowcnt nPSample; /* How often to do a periodic sample */
110519 int mxSample; /* Maximum number of samples to accumulate */
110520 u32 iPrn; /* Pseudo-random number used for sampling */
110521 StatSample *aBest; /* Array of nCol best samples */
110522 int iMin; /* Index in a[] of entry with minimum score */
110523 int nSample; /* Current number of samples */
110524 int nMaxEqZero; /* Max leading 0 in anEq[] for any a[] entry */
110525 int iGet; /* Index of current sample accessed by stat_get() */
110526 StatSample *a; /* Array of mxSample StatSample objects */
110527#endif
110528};
110529
110530/* Reclaim memory used by a StatSample
110531*/
110532#ifdef SQLITE_ENABLE_STAT4
110533static void sampleClear(sqlite3 *db, StatSample *p){
110534 assert( db!=0 );
110535 if( p->nRowid ){
110536 sqlite3DbFree(db, p->u.aRowid);
110537 p->nRowid = 0;
110538 }
110539}
110540#endif
110541
110542/* Initialize the BLOB value of a ROWID
110543*/
110544#ifdef SQLITE_ENABLE_STAT4
110545static void sampleSetRowid(sqlite3 *db, StatSample *p, int n, const u8 *pData){
110546 assert( db!=0 );
110547 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
110548 p->u.aRowid = sqlite3DbMallocRawNN(db, n);
110549 if( p->u.aRowid ){
110550 p->nRowid = n;
110551 memcpy(p->u.aRowid, pData, n);
110552 }else{
110553 p->nRowid = 0;
110554 }
110555}
110556#endif
110557
110558/* Initialize the INTEGER value of a ROWID.
110559*/
110560#ifdef SQLITE_ENABLE_STAT4
110561static void sampleSetRowidInt64(sqlite3 *db, StatSample *p, i64 iRowid){
110562 assert( db!=0 );
110563 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
110564 p->nRowid = 0;
110565 p->u.iRowid = iRowid;
110566}
110567#endif
110568
110569
110570/*
110571** Copy the contents of object (*pFrom) into (*pTo).
110572*/
110573#ifdef SQLITE_ENABLE_STAT4
110574static void sampleCopy(StatAccum *p, StatSample *pTo, StatSample *pFrom){
110575 pTo->isPSample = pFrom->isPSample;
110576 pTo->iCol = pFrom->iCol;
110577 pTo->iHash = pFrom->iHash;
110578 memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
110579 memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
110580 memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
110581 if( pFrom->nRowid ){
110582 sampleSetRowid(p->db, pTo, pFrom->nRowid, pFrom->u.aRowid);
110583 }else{
110584 sampleSetRowidInt64(p->db, pTo, pFrom->u.iRowid);
110585 }
110586}
110587#endif
110588
110589/*
110590** Reclaim all memory of a StatAccum structure.
110591*/
110592static void statAccumDestructor(void *pOld){
110593 StatAccum *p = (StatAccum*)pOld;
110594#ifdef SQLITE_ENABLE_STAT4
110595 if( p->mxSample ){
110596 int i;
110597 for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
110598 for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
110599 sampleClear(p->db, &p->current);
110600 }
110601#endif
110602 sqlite3DbFree(db: p->db, p);
110603}
110604
110605/*
110606** Implementation of the stat_init(N,K,C,L) SQL function. The four parameters
110607** are:
110608** N: The number of columns in the index including the rowid/pk (note 1)
110609** K: The number of columns in the index excluding the rowid/pk.
110610** C: Estimated number of rows in the index
110611** L: A limit on the number of rows to scan, or 0 for no-limit
110612**
110613** Note 1: In the special case of the covering index that implements a
110614** WITHOUT ROWID table, N is the number of PRIMARY KEY columns, not the
110615** total number of columns in the table.
110616**
110617** For indexes on ordinary rowid tables, N==K+1. But for indexes on
110618** WITHOUT ROWID tables, N=K+P where P is the number of columns in the
110619** PRIMARY KEY of the table. The covering index that implements the
110620** original WITHOUT ROWID table as N==K as a special case.
110621**
110622** This routine allocates the StatAccum object in heap memory. The return
110623** value is a pointer to the StatAccum object. The datatype of the
110624** return value is BLOB, but it is really just a pointer to the StatAccum
110625** object.
110626*/
110627static void statInit(
110628 sqlite3_context *context,
110629 int argc,
110630 sqlite3_value **argv
110631){
110632 StatAccum *p;
110633 int nCol; /* Number of columns in index being sampled */
110634 int nKeyCol; /* Number of key columns */
110635 int nColUp; /* nCol rounded up for alignment */
110636 int n; /* Bytes of space to allocate */
110637 sqlite3 *db = sqlite3_context_db_handle(p: context); /* Database connection */
110638#ifdef SQLITE_ENABLE_STAT4
110639 /* Maximum number of samples. 0 if STAT4 data is not collected */
110640 int mxSample = OptimizationEnabled(db,SQLITE_Stat4) ?SQLITE_STAT4_SAMPLES :0;
110641#endif
110642
110643 /* Decode the three function arguments */
110644 UNUSED_PARAMETER(argc);
110645 nCol = sqlite3_value_int(pVal: argv[0]);
110646 assert( nCol>0 );
110647 nColUp = sizeof(tRowcnt)<8 ? (nCol+1)&~1 : nCol;
110648 nKeyCol = sqlite3_value_int(pVal: argv[1]);
110649 assert( nKeyCol<=nCol );
110650 assert( nKeyCol>0 );
110651
110652 /* Allocate the space required for the StatAccum object */
110653 n = sizeof(*p)
110654 + sizeof(tRowcnt)*nColUp /* StatAccum.anEq */
110655 + sizeof(tRowcnt)*nColUp; /* StatAccum.anDLt */
110656#ifdef SQLITE_ENABLE_STAT4
110657 if( mxSample ){
110658 n += sizeof(tRowcnt)*nColUp /* StatAccum.anLt */
110659 + sizeof(StatSample)*(nCol+mxSample) /* StatAccum.aBest[], a[] */
110660 + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample);
110661 }
110662#endif
110663 p = sqlite3DbMallocZero(db, n);
110664 if( p==0 ){
110665 sqlite3_result_error_nomem(pCtx: context);
110666 return;
110667 }
110668
110669 p->db = db;
110670 p->nEst = sqlite3_value_int64(pVal: argv[2]);
110671 p->nRow = 0;
110672 p->nLimit = sqlite3_value_int64(pVal: argv[3]);
110673 p->nCol = nCol;
110674 p->nKeyCol = nKeyCol;
110675 p->nSkipAhead = 0;
110676 p->current.anDLt = (tRowcnt*)&p[1];
110677 p->current.anEq = &p->current.anDLt[nColUp];
110678
110679#ifdef SQLITE_ENABLE_STAT4
110680 p->mxSample = p->nLimit==0 ? mxSample : 0;
110681 if( mxSample ){
110682 u8 *pSpace; /* Allocated space not yet assigned */
110683 int i; /* Used to iterate through p->aSample[] */
110684
110685 p->iGet = -1;
110686 p->nPSample = (tRowcnt)(p->nEst/(mxSample/3+1) + 1);
110687 p->current.anLt = &p->current.anEq[nColUp];
110688 p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]);
110689
110690 /* Set up the StatAccum.a[] and aBest[] arrays */
110691 p->a = (struct StatSample*)&p->current.anLt[nColUp];
110692 p->aBest = &p->a[mxSample];
110693 pSpace = (u8*)(&p->a[mxSample+nCol]);
110694 for(i=0; i<(mxSample+nCol); i++){
110695 p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
110696 p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
110697 p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
110698 }
110699 assert( (pSpace - (u8*)p)==n );
110700
110701 for(i=0; i<nCol; i++){
110702 p->aBest[i].iCol = i;
110703 }
110704 }
110705#endif
110706
110707 /* Return a pointer to the allocated object to the caller. Note that
110708 ** only the pointer (the 2nd parameter) matters. The size of the object
110709 ** (given by the 3rd parameter) is never used and can be any positive
110710 ** value. */
110711 sqlite3_result_blob(pCtx: context, z: p, n: sizeof(*p), xDel: statAccumDestructor);
110712}
110713static const FuncDef statInitFuncdef = {
110714 4, /* nArg */
110715 SQLITE_UTF8, /* funcFlags */
110716 0, /* pUserData */
110717 0, /* pNext */
110718 statInit, /* xSFunc */
110719 0, /* xFinalize */
110720 0, 0, /* xValue, xInverse */
110721 "stat_init", /* zName */
110722 {0}
110723};
110724
110725#ifdef SQLITE_ENABLE_STAT4
110726/*
110727** pNew and pOld are both candidate non-periodic samples selected for
110728** the same column (pNew->iCol==pOld->iCol). Ignoring this column and
110729** considering only any trailing columns and the sample hash value, this
110730** function returns true if sample pNew is to be preferred over pOld.
110731** In other words, if we assume that the cardinalities of the selected
110732** column for pNew and pOld are equal, is pNew to be preferred over pOld.
110733**
110734** This function assumes that for each argument sample, the contents of
110735** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
110736*/
110737static int sampleIsBetterPost(
110738 StatAccum *pAccum,
110739 StatSample *pNew,
110740 StatSample *pOld
110741){
110742 int nCol = pAccum->nCol;
110743 int i;
110744 assert( pNew->iCol==pOld->iCol );
110745 for(i=pNew->iCol+1; i<nCol; i++){
110746 if( pNew->anEq[i]>pOld->anEq[i] ) return 1;
110747 if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
110748 }
110749 if( pNew->iHash>pOld->iHash ) return 1;
110750 return 0;
110751}
110752#endif
110753
110754#ifdef SQLITE_ENABLE_STAT4
110755/*
110756** Return true if pNew is to be preferred over pOld.
110757**
110758** This function assumes that for each argument sample, the contents of
110759** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
110760*/
110761static int sampleIsBetter(
110762 StatAccum *pAccum,
110763 StatSample *pNew,
110764 StatSample *pOld
110765){
110766 tRowcnt nEqNew = pNew->anEq[pNew->iCol];
110767 tRowcnt nEqOld = pOld->anEq[pOld->iCol];
110768
110769 assert( pOld->isPSample==0 && pNew->isPSample==0 );
110770 assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) );
110771
110772 if( (nEqNew>nEqOld) ) return 1;
110773 if( nEqNew==nEqOld ){
110774 if( pNew->iCol<pOld->iCol ) return 1;
110775 return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
110776 }
110777 return 0;
110778}
110779
110780/*
110781** Copy the contents of sample *pNew into the p->a[] array. If necessary,
110782** remove the least desirable sample from p->a[] to make room.
110783*/
110784static void sampleInsert(StatAccum *p, StatSample *pNew, int nEqZero){
110785 StatSample *pSample = 0;
110786 int i;
110787
110788 assert( IsStat4 || nEqZero==0 );
110789
110790 /* StatAccum.nMaxEqZero is set to the maximum number of leading 0
110791 ** values in the anEq[] array of any sample in StatAccum.a[]. In
110792 ** other words, if nMaxEqZero is n, then it is guaranteed that there
110793 ** are no samples with StatSample.anEq[m]==0 for (m>=n). */
110794 if( nEqZero>p->nMaxEqZero ){
110795 p->nMaxEqZero = nEqZero;
110796 }
110797 if( pNew->isPSample==0 ){
110798 StatSample *pUpgrade = 0;
110799 assert( pNew->anEq[pNew->iCol]>0 );
110800
110801 /* This sample is being added because the prefix that ends in column
110802 ** iCol occurs many times in the table. However, if we have already
110803 ** added a sample that shares this prefix, there is no need to add
110804 ** this one. Instead, upgrade the priority of the highest priority
110805 ** existing sample that shares this prefix. */
110806 for(i=p->nSample-1; i>=0; i--){
110807 StatSample *pOld = &p->a[i];
110808 if( pOld->anEq[pNew->iCol]==0 ){
110809 if( pOld->isPSample ) return;
110810 assert( pOld->iCol>pNew->iCol );
110811 assert( sampleIsBetter(p, pNew, pOld) );
110812 if( pUpgrade==0 || sampleIsBetter(p, pOld, pUpgrade) ){
110813 pUpgrade = pOld;
110814 }
110815 }
110816 }
110817 if( pUpgrade ){
110818 pUpgrade->iCol = pNew->iCol;
110819 pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
110820 goto find_new_min;
110821 }
110822 }
110823
110824 /* If necessary, remove sample iMin to make room for the new sample. */
110825 if( p->nSample>=p->mxSample ){
110826 StatSample *pMin = &p->a[p->iMin];
110827 tRowcnt *anEq = pMin->anEq;
110828 tRowcnt *anLt = pMin->anLt;
110829 tRowcnt *anDLt = pMin->anDLt;
110830 sampleClear(p->db, pMin);
110831 memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
110832 pSample = &p->a[p->nSample-1];
110833 pSample->nRowid = 0;
110834 pSample->anEq = anEq;
110835 pSample->anDLt = anDLt;
110836 pSample->anLt = anLt;
110837 p->nSample = p->mxSample-1;
110838 }
110839
110840 /* The "rows less-than" for the rowid column must be greater than that
110841 ** for the last sample in the p->a[] array. Otherwise, the samples would
110842 ** be out of order. */
110843 assert( p->nSample==0
110844 || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] );
110845
110846 /* Insert the new sample */
110847 pSample = &p->a[p->nSample];
110848 sampleCopy(p, pSample, pNew);
110849 p->nSample++;
110850
110851 /* Zero the first nEqZero entries in the anEq[] array. */
110852 memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
110853
110854find_new_min:
110855 if( p->nSample>=p->mxSample ){
110856 int iMin = -1;
110857 for(i=0; i<p->mxSample; i++){
110858 if( p->a[i].isPSample ) continue;
110859 if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
110860 iMin = i;
110861 }
110862 }
110863 assert( iMin>=0 );
110864 p->iMin = iMin;
110865 }
110866}
110867#endif /* SQLITE_ENABLE_STAT4 */
110868
110869#ifdef SQLITE_ENABLE_STAT4
110870/*
110871** Field iChng of the index being scanned has changed. So at this point
110872** p->current contains a sample that reflects the previous row of the
110873** index. The value of anEq[iChng] and subsequent anEq[] elements are
110874** correct at this point.
110875*/
110876static void samplePushPrevious(StatAccum *p, int iChng){
110877 int i;
110878
110879 /* Check if any samples from the aBest[] array should be pushed
110880 ** into IndexSample.a[] at this point. */
110881 for(i=(p->nCol-2); i>=iChng; i--){
110882 StatSample *pBest = &p->aBest[i];
110883 pBest->anEq[i] = p->current.anEq[i];
110884 if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
110885 sampleInsert(p, pBest, i);
110886 }
110887 }
110888
110889 /* Check that no sample contains an anEq[] entry with an index of
110890 ** p->nMaxEqZero or greater set to zero. */
110891 for(i=p->nSample-1; i>=0; i--){
110892 int j;
110893 for(j=p->nMaxEqZero; j<p->nCol; j++) assert( p->a[i].anEq[j]>0 );
110894 }
110895
110896 /* Update the anEq[] fields of any samples already collected. */
110897 if( iChng<p->nMaxEqZero ){
110898 for(i=p->nSample-1; i>=0; i--){
110899 int j;
110900 for(j=iChng; j<p->nCol; j++){
110901 if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
110902 }
110903 }
110904 p->nMaxEqZero = iChng;
110905 }
110906}
110907#endif /* SQLITE_ENABLE_STAT4 */
110908
110909/*
110910** Implementation of the stat_push SQL function: stat_push(P,C,R)
110911** Arguments:
110912**
110913** P Pointer to the StatAccum object created by stat_init()
110914** C Index of left-most column to differ from previous row
110915** R Rowid for the current row. Might be a key record for
110916** WITHOUT ROWID tables.
110917**
110918** The purpose of this routine is to collect statistical data and/or
110919** samples from the index being analyzed into the StatAccum object.
110920** The stat_get() SQL function will be used afterwards to
110921** retrieve the information gathered.
110922**
110923** This SQL function usually returns NULL, but might return an integer
110924** if it wants the byte-code to do special processing.
110925**
110926** The R parameter is only used for STAT4
110927*/
110928static void statPush(
110929 sqlite3_context *context,
110930 int argc,
110931 sqlite3_value **argv
110932){
110933 int i;
110934
110935 /* The three function arguments */
110936 StatAccum *p = (StatAccum*)sqlite3_value_blob(pVal: argv[0]);
110937 int iChng = sqlite3_value_int(pVal: argv[1]);
110938
110939 UNUSED_PARAMETER( argc );
110940 UNUSED_PARAMETER( context );
110941 assert( p->nCol>0 );
110942 assert( iChng<p->nCol );
110943
110944 if( p->nRow==0 ){
110945 /* This is the first call to this function. Do initialization. */
110946 for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
110947 }else{
110948 /* Second and subsequent calls get processed here */
110949#ifdef SQLITE_ENABLE_STAT4
110950 if( p->mxSample ) samplePushPrevious(p, iChng);
110951#endif
110952
110953 /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply
110954 ** to the current row of the index. */
110955 for(i=0; i<iChng; i++){
110956 p->current.anEq[i]++;
110957 }
110958 for(i=iChng; i<p->nCol; i++){
110959 p->current.anDLt[i]++;
110960#ifdef SQLITE_ENABLE_STAT4
110961 if( p->mxSample ) p->current.anLt[i] += p->current.anEq[i];
110962#endif
110963 p->current.anEq[i] = 1;
110964 }
110965 }
110966
110967 p->nRow++;
110968#ifdef SQLITE_ENABLE_STAT4
110969 if( p->mxSample ){
110970 tRowcnt nLt;
110971 if( sqlite3_value_type(argv[2])==SQLITE_INTEGER ){
110972 sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2]));
110973 }else{
110974 sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]),
110975 sqlite3_value_blob(argv[2]));
110976 }
110977 p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
110978
110979 nLt = p->current.anLt[p->nCol-1];
110980 /* Check if this is to be a periodic sample. If so, add it. */
110981 if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
110982 p->current.isPSample = 1;
110983 p->current.iCol = 0;
110984 sampleInsert(p, &p->current, p->nCol-1);
110985 p->current.isPSample = 0;
110986 }
110987
110988 /* Update the aBest[] array. */
110989 for(i=0; i<(p->nCol-1); i++){
110990 p->current.iCol = i;
110991 if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){
110992 sampleCopy(p, &p->aBest[i], &p->current);
110993 }
110994 }
110995 }else
110996#endif
110997 if( p->nLimit && p->nRow>(tRowcnt)p->nLimit*(p->nSkipAhead+1) ){
110998 p->nSkipAhead++;
110999 sqlite3_result_int(pCtx: context, iVal: p->current.anDLt[0]>0);
111000 }
111001}
111002
111003static const FuncDef statPushFuncdef = {
111004 2+IsStat4, /* nArg */
111005 SQLITE_UTF8, /* funcFlags */
111006 0, /* pUserData */
111007 0, /* pNext */
111008 statPush, /* xSFunc */
111009 0, /* xFinalize */
111010 0, 0, /* xValue, xInverse */
111011 "stat_push", /* zName */
111012 {0}
111013};
111014
111015#define STAT_GET_STAT1 0 /* "stat" column of stat1 table */
111016#define STAT_GET_ROWID 1 /* "rowid" column of stat[34] entry */
111017#define STAT_GET_NEQ 2 /* "neq" column of stat[34] entry */
111018#define STAT_GET_NLT 3 /* "nlt" column of stat[34] entry */
111019#define STAT_GET_NDLT 4 /* "ndlt" column of stat[34] entry */
111020
111021/*
111022** Implementation of the stat_get(P,J) SQL function. This routine is
111023** used to query statistical information that has been gathered into
111024** the StatAccum object by prior calls to stat_push(). The P parameter
111025** has type BLOB but it is really just a pointer to the StatAccum object.
111026** The content to returned is determined by the parameter J
111027** which is one of the STAT_GET_xxxx values defined above.
111028**
111029** The stat_get(P,J) function is not available to generic SQL. It is
111030** inserted as part of a manually constructed bytecode program. (See
111031** the callStatGet() routine below.) It is guaranteed that the P
111032** parameter will always be a pointer to a StatAccum object, never a
111033** NULL.
111034**
111035** If STAT4 is not enabled, then J is always
111036** STAT_GET_STAT1 and is hence omitted and this routine becomes
111037** a one-parameter function, stat_get(P), that always returns the
111038** stat1 table entry information.
111039*/
111040static void statGet(
111041 sqlite3_context *context,
111042 int argc,
111043 sqlite3_value **argv
111044){
111045 StatAccum *p = (StatAccum*)sqlite3_value_blob(pVal: argv[0]);
111046#ifdef SQLITE_ENABLE_STAT4
111047 /* STAT4 has a parameter on this routine. */
111048 int eCall = sqlite3_value_int(argv[1]);
111049 assert( argc==2 );
111050 assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ
111051 || eCall==STAT_GET_ROWID || eCall==STAT_GET_NLT
111052 || eCall==STAT_GET_NDLT
111053 );
111054 assert( eCall==STAT_GET_STAT1 || p->mxSample );
111055 if( eCall==STAT_GET_STAT1 )
111056#else
111057 assert( argc==1 );
111058#endif
111059 {
111060 /* Return the value to store in the "stat" column of the sqlite_stat1
111061 ** table for this index.
111062 **
111063 ** The value is a string composed of a list of integers describing
111064 ** the index. The first integer in the list is the total number of
111065 ** entries in the index. There is one additional integer in the list
111066 ** for each indexed column. This additional integer is an estimate of
111067 ** the number of rows matched by a equality query on the index using
111068 ** a key with the corresponding number of fields. In other words,
111069 ** if the index is on columns (a,b) and the sqlite_stat1 value is
111070 ** "100 10 2", then SQLite estimates that:
111071 **
111072 ** * the index contains 100 rows,
111073 ** * "WHERE a=?" matches 10 rows, and
111074 ** * "WHERE a=? AND b=?" matches 2 rows.
111075 **
111076 ** If D is the count of distinct values and K is the total number of
111077 ** rows, then each estimate is computed as:
111078 **
111079 ** I = (K+D-1)/D
111080 */
111081 sqlite3_str sStat; /* Text of the constructed "stat" line */
111082 int i; /* Loop counter */
111083
111084 sqlite3StrAccumInit(p: &sStat, db: 0, zBase: 0, n: 0, mx: (p->nKeyCol+1)*100);
111085 sqlite3_str_appendf(p: &sStat, zFormat: "%llu",
111086 p->nSkipAhead ? (u64)p->nEst : (u64)p->nRow);
111087 for(i=0; i<p->nKeyCol; i++){
111088 u64 nDistinct = p->current.anDLt[i] + 1;
111089 u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
111090 sqlite3_str_appendf(p: &sStat, zFormat: " %llu", iVal);
111091 assert( p->current.anEq[i] );
111092 }
111093 sqlite3ResultStrAccum(pCtx: context, p: &sStat);
111094 }
111095#ifdef SQLITE_ENABLE_STAT4
111096 else if( eCall==STAT_GET_ROWID ){
111097 if( p->iGet<0 ){
111098 samplePushPrevious(p, 0);
111099 p->iGet = 0;
111100 }
111101 if( p->iGet<p->nSample ){
111102 StatSample *pS = p->a + p->iGet;
111103 if( pS->nRowid==0 ){
111104 sqlite3_result_int64(context, pS->u.iRowid);
111105 }else{
111106 sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid,
111107 SQLITE_TRANSIENT);
111108 }
111109 }
111110 }else{
111111 tRowcnt *aCnt = 0;
111112 sqlite3_str sStat;
111113 int i;
111114
111115 assert( p->iGet<p->nSample );
111116 switch( eCall ){
111117 case STAT_GET_NEQ: aCnt = p->a[p->iGet].anEq; break;
111118 case STAT_GET_NLT: aCnt = p->a[p->iGet].anLt; break;
111119 default: {
111120 aCnt = p->a[p->iGet].anDLt;
111121 p->iGet++;
111122 break;
111123 }
111124 }
111125 sqlite3StrAccumInit(&sStat, 0, 0, 0, p->nCol*100);
111126 for(i=0; i<p->nCol; i++){
111127 sqlite3_str_appendf(&sStat, "%llu ", (u64)aCnt[i]);
111128 }
111129 if( sStat.nChar ) sStat.nChar--;
111130 sqlite3ResultStrAccum(context, &sStat);
111131 }
111132#endif /* SQLITE_ENABLE_STAT4 */
111133#ifndef SQLITE_DEBUG
111134 UNUSED_PARAMETER( argc );
111135#endif
111136}
111137static const FuncDef statGetFuncdef = {
111138 1+IsStat4, /* nArg */
111139 SQLITE_UTF8, /* funcFlags */
111140 0, /* pUserData */
111141 0, /* pNext */
111142 statGet, /* xSFunc */
111143 0, /* xFinalize */
111144 0, 0, /* xValue, xInverse */
111145 "stat_get", /* zName */
111146 {0}
111147};
111148
111149static void callStatGet(Parse *pParse, int regStat, int iParam, int regOut){
111150#ifdef SQLITE_ENABLE_STAT4
111151 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Integer, iParam, regStat+1);
111152#elif SQLITE_DEBUG
111153 assert( iParam==STAT_GET_STAT1 );
111154#else
111155 UNUSED_PARAMETER( iParam );
111156#endif
111157 assert( regOut!=regStat && regOut!=regStat+1 );
111158 sqlite3VdbeAddFunctionCall(pParse, p1: 0, p2: regStat, p3: regOut, nArg: 1+IsStat4,
111159 pFunc: &statGetFuncdef, eCallCtx: 0);
111160}
111161
111162#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
111163/* Add a comment to the most recent VDBE opcode that is the name
111164** of the k-th column of the pIdx index.
111165*/
111166static void analyzeVdbeCommentIndexWithColumnName(
111167 Vdbe *v, /* Prepared statement under construction */
111168 Index *pIdx, /* Index whose column is being loaded */
111169 int k /* Which column index */
111170){
111171 int i; /* Index of column in the table */
111172 assert( k>=0 && k<pIdx->nColumn );
111173 i = pIdx->aiColumn[k];
111174 if( NEVER(i==XN_ROWID) ){
111175 VdbeComment((v,"%s.rowid",pIdx->zName));
111176 }else if( i==XN_EXPR ){
111177 VdbeComment((v,"%s.expr(%d)",pIdx->zName, k));
111178 }else{
111179 VdbeComment((v,"%s.%s", pIdx->zName, pIdx->pTable->aCol[i].zCnName));
111180 }
111181}
111182#else
111183# define analyzeVdbeCommentIndexWithColumnName(a,b,c)
111184#endif /* SQLITE_DEBUG */
111185
111186/*
111187** Generate code to do an analysis of all indices associated with
111188** a single table.
111189*/
111190static void analyzeOneTable(
111191 Parse *pParse, /* Parser context */
111192 Table *pTab, /* Table whose indices are to be analyzed */
111193 Index *pOnlyIdx, /* If not NULL, only analyze this one index */
111194 int iStatCur, /* Index of VdbeCursor that writes the sqlite_stat1 table */
111195 int iMem, /* Available memory locations begin here */
111196 int iTab /* Next available cursor */
111197){
111198 sqlite3 *db = pParse->db; /* Database handle */
111199 Index *pIdx; /* An index to being analyzed */
111200 int iIdxCur; /* Cursor open on index being analyzed */
111201 int iTabCur; /* Table cursor */
111202 Vdbe *v; /* The virtual machine being built up */
111203 int i; /* Loop counter */
111204 int jZeroRows = -1; /* Jump from here if number of rows is zero */
111205 int iDb; /* Index of database containing pTab */
111206 u8 needTableCnt = 1; /* True to count the table */
111207 int regNewRowid = iMem++; /* Rowid for the inserted record */
111208 int regStat = iMem++; /* Register to hold StatAccum object */
111209 int regChng = iMem++; /* Index of changed index field */
111210 int regRowid = iMem++; /* Rowid argument passed to stat_push() */
111211 int regTemp = iMem++; /* Temporary use register */
111212 int regTemp2 = iMem++; /* Second temporary use register */
111213 int regTabname = iMem++; /* Register containing table name */
111214 int regIdxname = iMem++; /* Register containing index name */
111215 int regStat1 = iMem++; /* Value for the stat column of sqlite_stat1 */
111216 int regPrev = iMem; /* MUST BE LAST (see below) */
111217#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
111218 Table *pStat1 = 0;
111219#endif
111220
111221 pParse->nMem = MAX(pParse->nMem, iMem);
111222 v = sqlite3GetVdbe(pParse);
111223 if( v==0 || NEVER(pTab==0) ){
111224 return;
111225 }
111226 if( !IsOrdinaryTable(pTab) ){
111227 /* Do not gather statistics on views or virtual tables */
111228 return;
111229 }
111230 if( sqlite3_strlike(zGlob: "sqlite\\_%", zStr: pTab->zName, cEsc: '\\')==0 ){
111231 /* Do not gather statistics on system tables */
111232 return;
111233 }
111234 assert( sqlite3BtreeHoldsAllMutexes(db) );
111235 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
111236 assert( iDb>=0 );
111237 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
111238#ifndef SQLITE_OMIT_AUTHORIZATION
111239 if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
111240 db->aDb[iDb].zDbSName ) ){
111241 return;
111242 }
111243#endif
111244
111245#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
111246 if( db->xPreUpdateCallback ){
111247 pStat1 = (Table*)sqlite3DbMallocZero(db, sizeof(Table) + 13);
111248 if( pStat1==0 ) return;
111249 pStat1->zName = (char*)&pStat1[1];
111250 memcpy(pStat1->zName, "sqlite_stat1", 13);
111251 pStat1->nCol = 3;
111252 pStat1->iPKey = -1;
111253 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Noop, 0, 0, 0,(char*)pStat1,P4_DYNBLOB);
111254 }
111255#endif
111256
111257 /* Establish a read-lock on the table at the shared-cache level.
111258 ** Open a read-only cursor on the table. Also allocate a cursor number
111259 ** to use for scanning indexes (iIdxCur). No index cursor is opened at
111260 ** this time though. */
111261 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
111262 iTabCur = iTab++;
111263 iIdxCur = iTab++;
111264 pParse->nTab = MAX(pParse->nTab, iTab);
111265 sqlite3OpenTable(pParse, iCur: iTabCur, iDb, pTab, OP_OpenRead);
111266 sqlite3VdbeLoadString(p: v, iDest: regTabname, zStr: pTab->zName);
111267
111268 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
111269 int nCol; /* Number of columns in pIdx. "N" */
111270 int addrRewind; /* Address of "OP_Rewind iIdxCur" */
111271 int addrNextRow; /* Address of "next_row:" */
111272 const char *zIdxName; /* Name of the index */
111273 int nColTest; /* Number of columns to test for changes */
111274
111275 if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
111276 if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
111277 if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIdx) ){
111278 nCol = pIdx->nKeyCol;
111279 zIdxName = pTab->zName;
111280 nColTest = nCol - 1;
111281 }else{
111282 nCol = pIdx->nColumn;
111283 zIdxName = pIdx->zName;
111284 nColTest = pIdx->uniqNotNull ? pIdx->nKeyCol-1 : nCol-1;
111285 }
111286
111287 /* Populate the register containing the index name. */
111288 sqlite3VdbeLoadString(p: v, iDest: regIdxname, zStr: zIdxName);
111289 VdbeComment((v, "Analysis for %s.%s", pTab->zName, zIdxName));
111290
111291 /*
111292 ** Pseudo-code for loop that calls stat_push():
111293 **
111294 ** Rewind csr
111295 ** if eof(csr) goto end_of_scan;
111296 ** regChng = 0
111297 ** goto chng_addr_0;
111298 **
111299 ** next_row:
111300 ** regChng = 0
111301 ** if( idx(0) != regPrev(0) ) goto chng_addr_0
111302 ** regChng = 1
111303 ** if( idx(1) != regPrev(1) ) goto chng_addr_1
111304 ** ...
111305 ** regChng = N
111306 ** goto chng_addr_N
111307 **
111308 ** chng_addr_0:
111309 ** regPrev(0) = idx(0)
111310 ** chng_addr_1:
111311 ** regPrev(1) = idx(1)
111312 ** ...
111313 **
111314 ** endDistinctTest:
111315 ** regRowid = idx(rowid)
111316 ** stat_push(P, regChng, regRowid)
111317 ** Next csr
111318 ** if !eof(csr) goto next_row;
111319 **
111320 ** end_of_scan:
111321 */
111322
111323 /* Make sure there are enough memory cells allocated to accommodate
111324 ** the regPrev array and a trailing rowid (the rowid slot is required
111325 ** when building a record to insert into the sample column of
111326 ** the sqlite_stat4 table. */
111327 pParse->nMem = MAX(pParse->nMem, regPrev+nColTest);
111328
111329 /* Open a read-only cursor on the index being analyzed. */
111330 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
111331 sqlite3VdbeAddOp3(p: v, OP_OpenRead, p1: iIdxCur, p2: pIdx->tnum, p3: iDb);
111332 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
111333 VdbeComment((v, "%s", pIdx->zName));
111334
111335 /* Invoke the stat_init() function. The arguments are:
111336 **
111337 ** (1) the number of columns in the index including the rowid
111338 ** (or for a WITHOUT ROWID table, the number of PK columns),
111339 ** (2) the number of columns in the key without the rowid/pk
111340 ** (3) estimated number of rows in the index,
111341 */
111342 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: nCol, p2: regStat+1);
111343 assert( regRowid==regStat+2 );
111344 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: pIdx->nKeyCol, p2: regRowid);
111345#ifdef SQLITE_ENABLE_STAT4
111346 if( OptimizationEnabled(db, SQLITE_Stat4) ){
111347 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regTemp);
111348 addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur);
111349 VdbeCoverage(v);
111350 }else
111351#endif
111352 {
111353 addrRewind = sqlite3VdbeAddOp1(p: v, OP_Rewind, p1: iIdxCur);
111354 VdbeCoverage(v);
111355 sqlite3VdbeAddOp3(p: v, OP_Count, p1: iIdxCur, p2: regTemp, p3: 1);
111356 }
111357 assert( regTemp2==regStat+4 );
111358 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: db->nAnalysisLimit, p2: regTemp2);
111359 sqlite3VdbeAddFunctionCall(pParse, p1: 0, p2: regStat+1, p3: regStat, nArg: 4,
111360 pFunc: &statInitFuncdef, eCallCtx: 0);
111361
111362 /* Implementation of the following:
111363 **
111364 ** Rewind csr
111365 ** if eof(csr) goto end_of_scan;
111366 ** regChng = 0
111367 ** goto next_push_0;
111368 **
111369 */
111370 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: regChng);
111371 addrNextRow = sqlite3VdbeCurrentAddr(p: v);
111372
111373 if( nColTest>0 ){
111374 int endDistinctTest = sqlite3VdbeMakeLabel(pParse);
111375 int *aGotoChng; /* Array of jump instruction addresses */
111376 aGotoChng = sqlite3DbMallocRawNN(db, n: sizeof(int)*nColTest);
111377 if( aGotoChng==0 ) continue;
111378
111379 /*
111380 ** next_row:
111381 ** regChng = 0
111382 ** if( idx(0) != regPrev(0) ) goto chng_addr_0
111383 ** regChng = 1
111384 ** if( idx(1) != regPrev(1) ) goto chng_addr_1
111385 ** ...
111386 ** regChng = N
111387 ** goto endDistinctTest
111388 */
111389 sqlite3VdbeAddOp0(p: v, OP_Goto);
111390 addrNextRow = sqlite3VdbeCurrentAddr(p: v);
111391 if( nColTest==1 && pIdx->nKeyCol==1 && IsUniqueIndex(pIdx) ){
111392 /* For a single-column UNIQUE index, once we have found a non-NULL
111393 ** row, we know that all the rest will be distinct, so skip
111394 ** subsequent distinctness tests. */
111395 sqlite3VdbeAddOp2(p: v, OP_NotNull, p1: regPrev, p2: endDistinctTest);
111396 VdbeCoverage(v);
111397 }
111398 for(i=0; i<nColTest; i++){
111399 char *pColl = (char*)sqlite3LocateCollSeq(pParse, zName: pIdx->azColl[i]);
111400 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: i, p2: regChng);
111401 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iIdxCur, p2: i, p3: regTemp);
111402 analyzeVdbeCommentIndexWithColumnName(v,pIdx,i);
111403 aGotoChng[i] =
111404 sqlite3VdbeAddOp4(p: v, OP_Ne, p1: regTemp, p2: 0, p3: regPrev+i, zP4: pColl, P4_COLLSEQ);
111405 sqlite3VdbeChangeP5(p: v, SQLITE_NULLEQ);
111406 VdbeCoverage(v);
111407 }
111408 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: nColTest, p2: regChng);
111409 sqlite3VdbeGoto(p: v, iDest: endDistinctTest);
111410
111411
111412 /*
111413 ** chng_addr_0:
111414 ** regPrev(0) = idx(0)
111415 ** chng_addr_1:
111416 ** regPrev(1) = idx(1)
111417 ** ...
111418 */
111419 sqlite3VdbeJumpHere(p: v, addr: addrNextRow-1);
111420 for(i=0; i<nColTest; i++){
111421 sqlite3VdbeJumpHere(p: v, addr: aGotoChng[i]);
111422 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iIdxCur, p2: i, p3: regPrev+i);
111423 analyzeVdbeCommentIndexWithColumnName(v,pIdx,i);
111424 }
111425 sqlite3VdbeResolveLabel(v, x: endDistinctTest);
111426 sqlite3DbFree(db, p: aGotoChng);
111427 }
111428
111429 /*
111430 ** chng_addr_N:
111431 ** regRowid = idx(rowid) // STAT4 only
111432 ** stat_push(P, regChng, regRowid) // 3rd parameter STAT4 only
111433 ** Next csr
111434 ** if !eof(csr) goto next_row;
111435 */
111436#ifdef SQLITE_ENABLE_STAT4
111437 if( OptimizationEnabled(db, SQLITE_Stat4) ){
111438 assert( regRowid==(regStat+2) );
111439 if( HasRowid(pTab) ){
111440 sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid);
111441 }else{
111442 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
111443 int j, k, regKey;
111444 regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
111445 for(j=0; j<pPk->nKeyCol; j++){
111446 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
111447 assert( k>=0 && k<pIdx->nColumn );
111448 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, regKey+j);
111449 analyzeVdbeCommentIndexWithColumnName(v,pIdx,k);
111450 }
111451 sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
111452 sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
111453 }
111454 }
111455#endif
111456 assert( regChng==(regStat+1) );
111457 {
111458 sqlite3VdbeAddFunctionCall(pParse, p1: 1, p2: regStat, p3: regTemp, nArg: 2+IsStat4,
111459 pFunc: &statPushFuncdef, eCallCtx: 0);
111460 if( db->nAnalysisLimit ){
111461 int j1, j2, j3;
111462 j1 = sqlite3VdbeAddOp1(p: v, OP_IsNull, p1: regTemp); VdbeCoverage(v);
111463 j2 = sqlite3VdbeAddOp1(p: v, OP_If, p1: regTemp); VdbeCoverage(v);
111464 j3 = sqlite3VdbeAddOp4Int(p: v, OP_SeekGT, p1: iIdxCur, p2: 0, p3: regPrev, p4: 1);
111465 VdbeCoverage(v);
111466 sqlite3VdbeJumpHere(p: v, addr: j1);
111467 sqlite3VdbeAddOp2(p: v, OP_Next, p1: iIdxCur, p2: addrNextRow); VdbeCoverage(v);
111468 sqlite3VdbeJumpHere(p: v, addr: j2);
111469 sqlite3VdbeJumpHere(p: v, addr: j3);
111470 }else{
111471 sqlite3VdbeAddOp2(p: v, OP_Next, p1: iIdxCur, p2: addrNextRow); VdbeCoverage(v);
111472 }
111473 }
111474
111475 /* Add the entry to the stat1 table. */
111476 callStatGet(pParse, regStat, STAT_GET_STAT1, regOut: regStat1);
111477 assert( "BBB"[0]==SQLITE_AFF_TEXT );
111478 sqlite3VdbeAddOp4(p: v, OP_MakeRecord, p1: regTabname, p2: 3, p3: regTemp, zP4: "BBB", p4type: 0);
111479 sqlite3VdbeAddOp2(p: v, OP_NewRowid, p1: iStatCur, p2: regNewRowid);
111480 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: iStatCur, p2: regTemp, p3: regNewRowid);
111481#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
111482 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
111483#endif
111484 sqlite3VdbeChangeP5(p: v, OPFLAG_APPEND);
111485
111486 /* Add the entries to the stat4 table. */
111487#ifdef SQLITE_ENABLE_STAT4
111488 if( OptimizationEnabled(db, SQLITE_Stat4) && db->nAnalysisLimit==0 ){
111489 int regEq = regStat1;
111490 int regLt = regStat1+1;
111491 int regDLt = regStat1+2;
111492 int regSample = regStat1+3;
111493 int regCol = regStat1+4;
111494 int regSampleRowid = regCol + nCol;
111495 int addrNext;
111496 int addrIsNull;
111497 u8 seekOp = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
111498
111499 pParse->nMem = MAX(pParse->nMem, regCol+nCol);
111500
111501 addrNext = sqlite3VdbeCurrentAddr(v);
111502 callStatGet(pParse, regStat, STAT_GET_ROWID, regSampleRowid);
111503 addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid);
111504 VdbeCoverage(v);
111505 callStatGet(pParse, regStat, STAT_GET_NEQ, regEq);
111506 callStatGet(pParse, regStat, STAT_GET_NLT, regLt);
111507 callStatGet(pParse, regStat, STAT_GET_NDLT, regDLt);
111508 sqlite3VdbeAddOp4Int(v, seekOp, iTabCur, addrNext, regSampleRowid, 0);
111509 VdbeCoverage(v);
111510 for(i=0; i<nCol; i++){
111511 sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, i, regCol+i);
111512 }
111513 sqlite3VdbeAddOp3(v, OP_MakeRecord, regCol, nCol, regSample);
111514 sqlite3VdbeAddOp3(v, OP_MakeRecord, regTabname, 6, regTemp);
111515 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
111516 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regTemp, regNewRowid);
111517 sqlite3VdbeAddOp2(v, OP_Goto, 1, addrNext); /* P1==1 for end-of-loop */
111518 sqlite3VdbeJumpHere(v, addrIsNull);
111519 }
111520#endif /* SQLITE_ENABLE_STAT4 */
111521
111522 /* End of analysis */
111523 sqlite3VdbeJumpHere(p: v, addr: addrRewind);
111524 }
111525
111526
111527 /* Create a single sqlite_stat1 entry containing NULL as the index
111528 ** name and the row count as the content.
111529 */
111530 if( pOnlyIdx==0 && needTableCnt ){
111531 VdbeComment((v, "%s", pTab->zName));
111532 sqlite3VdbeAddOp2(p: v, OP_Count, p1: iTabCur, p2: regStat1);
111533 jZeroRows = sqlite3VdbeAddOp1(p: v, OP_IfNot, p1: regStat1); VdbeCoverage(v);
111534 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: regIdxname);
111535 assert( "BBB"[0]==SQLITE_AFF_TEXT );
111536 sqlite3VdbeAddOp4(p: v, OP_MakeRecord, p1: regTabname, p2: 3, p3: regTemp, zP4: "BBB", p4type: 0);
111537 sqlite3VdbeAddOp2(p: v, OP_NewRowid, p1: iStatCur, p2: regNewRowid);
111538 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: iStatCur, p2: regTemp, p3: regNewRowid);
111539 sqlite3VdbeChangeP5(p: v, OPFLAG_APPEND);
111540#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
111541 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
111542#endif
111543 sqlite3VdbeJumpHere(p: v, addr: jZeroRows);
111544 }
111545}
111546
111547
111548/*
111549** Generate code that will cause the most recent index analysis to
111550** be loaded into internal hash tables where is can be used.
111551*/
111552static void loadAnalysis(Parse *pParse, int iDb){
111553 Vdbe *v = sqlite3GetVdbe(pParse);
111554 if( v ){
111555 sqlite3VdbeAddOp1(p: v, OP_LoadAnalysis, p1: iDb);
111556 }
111557}
111558
111559/*
111560** Generate code that will do an analysis of an entire database
111561*/
111562static void analyzeDatabase(Parse *pParse, int iDb){
111563 sqlite3 *db = pParse->db;
111564 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
111565 HashElem *k;
111566 int iStatCur;
111567 int iMem;
111568 int iTab;
111569
111570 sqlite3BeginWriteOperation(pParse, 0, iDb);
111571 iStatCur = pParse->nTab;
111572 pParse->nTab += 3;
111573 openStatTable(pParse, iDb, iStatCur, zWhere: 0, zWhereType: 0);
111574 iMem = pParse->nMem+1;
111575 iTab = pParse->nTab;
111576 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
111577 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
111578 Table *pTab = (Table*)sqliteHashData(k);
111579 analyzeOneTable(pParse, pTab, pOnlyIdx: 0, iStatCur, iMem, iTab);
111580 }
111581 loadAnalysis(pParse, iDb);
111582}
111583
111584/*
111585** Generate code that will do an analysis of a single table in
111586** a database. If pOnlyIdx is not NULL then it is a single index
111587** in pTab that should be analyzed.
111588*/
111589static void analyzeTable(Parse *pParse, Table *pTab, Index *pOnlyIdx){
111590 int iDb;
111591 int iStatCur;
111592
111593 assert( pTab!=0 );
111594 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
111595 iDb = sqlite3SchemaToIndex(db: pParse->db, pTab->pSchema);
111596 sqlite3BeginWriteOperation(pParse, 0, iDb);
111597 iStatCur = pParse->nTab;
111598 pParse->nTab += 3;
111599 if( pOnlyIdx ){
111600 openStatTable(pParse, iDb, iStatCur, zWhere: pOnlyIdx->zName, zWhereType: "idx");
111601 }else{
111602 openStatTable(pParse, iDb, iStatCur, zWhere: pTab->zName, zWhereType: "tbl");
111603 }
111604 analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,iMem: pParse->nMem+1,iTab: pParse->nTab);
111605 loadAnalysis(pParse, iDb);
111606}
111607
111608/*
111609** Generate code for the ANALYZE command. The parser calls this routine
111610** when it recognizes an ANALYZE command.
111611**
111612** ANALYZE -- 1
111613** ANALYZE <database> -- 2
111614** ANALYZE ?<database>.?<tablename> -- 3
111615**
111616** Form 1 causes all indices in all attached databases to be analyzed.
111617** Form 2 analyzes all indices the single database named.
111618** Form 3 analyzes all indices associated with the named table.
111619*/
111620SQLITE_PRIVATE void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
111621 sqlite3 *db = pParse->db;
111622 int iDb;
111623 int i;
111624 char *z, *zDb;
111625 Table *pTab;
111626 Index *pIdx;
111627 Token *pTableName;
111628 Vdbe *v;
111629
111630 /* Read the database schema. If an error occurs, leave an error message
111631 ** and code in pParse and return NULL. */
111632 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
111633 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
111634 return;
111635 }
111636
111637 assert( pName2!=0 || pName1==0 );
111638 if( pName1==0 ){
111639 /* Form 1: Analyze everything */
111640 for(i=0; i<db->nDb; i++){
111641 if( i==1 ) continue; /* Do not analyze the TEMP database */
111642 analyzeDatabase(pParse, iDb: i);
111643 }
111644 }else if( pName2->n==0 && (iDb = sqlite3FindDb(db, pName1))>=0 ){
111645 /* Analyze the schema named as the argument */
111646 analyzeDatabase(pParse, iDb);
111647 }else{
111648 /* Form 3: Analyze the table or index named as an argument */
111649 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
111650 if( iDb>=0 ){
111651 zDb = pName2->n ? db->aDb[iDb].zDbSName : 0;
111652 z = sqlite3NameFromToken(db, pTableName);
111653 if( z ){
111654 if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){
111655 analyzeTable(pParse, pTab: pIdx->pTable, pOnlyIdx: pIdx);
111656 }else if( (pTab = sqlite3LocateTable(pParse, flags: 0, z, zDb))!=0 ){
111657 analyzeTable(pParse, pTab, pOnlyIdx: 0);
111658 }
111659 sqlite3DbFree(db, p: z);
111660 }
111661 }
111662 }
111663 if( db->nSqlExec==0 && (v = sqlite3GetVdbe(pParse))!=0 ){
111664 sqlite3VdbeAddOp0(p: v, OP_Expire);
111665 }
111666}
111667
111668/*
111669** Used to pass information from the analyzer reader through to the
111670** callback routine.
111671*/
111672typedef struct analysisInfo analysisInfo;
111673struct analysisInfo {
111674 sqlite3 *db;
111675 const char *zDatabase;
111676};
111677
111678/*
111679** The first argument points to a nul-terminated string containing a
111680** list of space separated integers. Read the first nOut of these into
111681** the array aOut[].
111682*/
111683static void decodeIntArray(
111684 char *zIntArray, /* String containing int array to decode */
111685 int nOut, /* Number of slots in aOut[] */
111686 tRowcnt *aOut, /* Store integers here */
111687 LogEst *aLog, /* Or, if aOut==0, here */
111688 Index *pIndex /* Handle extra flags for this index, if not NULL */
111689){
111690 char *z = zIntArray;
111691 int c;
111692 int i;
111693 tRowcnt v;
111694
111695#ifdef SQLITE_ENABLE_STAT4
111696 if( z==0 ) z = "";
111697#else
111698 assert( z!=0 );
111699#endif
111700 for(i=0; *z && i<nOut; i++){
111701 v = 0;
111702 while( (c=z[0])>='0' && c<='9' ){
111703 v = v*10 + c - '0';
111704 z++;
111705 }
111706#ifdef SQLITE_ENABLE_STAT4
111707 if( aOut ) aOut[i] = v;
111708 if( aLog ) aLog[i] = sqlite3LogEst(v);
111709#else
111710 assert( aOut==0 );
111711 UNUSED_PARAMETER(aOut);
111712 assert( aLog!=0 );
111713 aLog[i] = sqlite3LogEst(x: v);
111714#endif
111715 if( *z==' ' ) z++;
111716 }
111717#ifndef SQLITE_ENABLE_STAT4
111718 assert( pIndex!=0 ); {
111719#else
111720 if( pIndex ){
111721#endif
111722 pIndex->bUnordered = 0;
111723 pIndex->noSkipScan = 0;
111724 while( z[0] ){
111725 if( sqlite3_strglob(zGlob: "unordered*", zStr: z)==0 ){
111726 pIndex->bUnordered = 1;
111727 }else if( sqlite3_strglob(zGlob: "sz=[0-9]*", zStr: z)==0 ){
111728 int sz = sqlite3Atoi(z: z+3);
111729 if( sz<2 ) sz = 2;
111730 pIndex->szIdxRow = sqlite3LogEst(x: sz);
111731 }else if( sqlite3_strglob(zGlob: "noskipscan*", zStr: z)==0 ){
111732 pIndex->noSkipScan = 1;
111733 }
111734#ifdef SQLITE_ENABLE_COSTMULT
111735 else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){
111736 pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9));
111737 }
111738#endif
111739 while( z[0]!=0 && z[0]!=' ' ) z++;
111740 while( z[0]==' ' ) z++;
111741 }
111742 }
111743}
111744
111745/*
111746** This callback is invoked once for each index when reading the
111747** sqlite_stat1 table.
111748**
111749** argv[0] = name of the table
111750** argv[1] = name of the index (might be NULL)
111751** argv[2] = results of analysis - on integer for each column
111752**
111753** Entries for which argv[1]==NULL simply record the number of rows in
111754** the table.
111755*/
111756static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
111757 analysisInfo *pInfo = (analysisInfo*)pData;
111758 Index *pIndex;
111759 Table *pTable;
111760 const char *z;
111761
111762 assert( argc==3 );
111763 UNUSED_PARAMETER2(NotUsed, argc);
111764
111765 if( argv==0 || argv[0]==0 || argv[2]==0 ){
111766 return 0;
111767 }
111768 pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
111769 if( pTable==0 ){
111770 return 0;
111771 }
111772 if( argv[1]==0 ){
111773 pIndex = 0;
111774 }else if( sqlite3_stricmp(zLeft: argv[0],zRight: argv[1])==0 ){
111775 pIndex = sqlite3PrimaryKeyIndex(pTable);
111776 }else{
111777 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
111778 }
111779 z = argv[2];
111780
111781 if( pIndex ){
111782 tRowcnt *aiRowEst = 0;
111783 int nCol = pIndex->nKeyCol+1;
111784#ifdef SQLITE_ENABLE_STAT4
111785 /* Index.aiRowEst may already be set here if there are duplicate
111786 ** sqlite_stat1 entries for this index. In that case just clobber
111787 ** the old data with the new instead of allocating a new array. */
111788 if( pIndex->aiRowEst==0 ){
111789 pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
111790 if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db);
111791 }
111792 aiRowEst = pIndex->aiRowEst;
111793#endif
111794 pIndex->bUnordered = 0;
111795 decodeIntArray(zIntArray: (char*)z, nOut: nCol, aOut: aiRowEst, aLog: pIndex->aiRowLogEst, pIndex);
111796 pIndex->hasStat1 = 1;
111797 if( pIndex->pPartIdxWhere==0 ){
111798 pTable->nRowLogEst = pIndex->aiRowLogEst[0];
111799 pTable->tabFlags |= TF_HasStat1;
111800 }
111801 }else{
111802 Index fakeIdx;
111803 fakeIdx.szIdxRow = pTable->szTabRow;
111804#ifdef SQLITE_ENABLE_COSTMULT
111805 fakeIdx.pTable = pTable;
111806#endif
111807 decodeIntArray(zIntArray: (char*)z, nOut: 1, aOut: 0, aLog: &pTable->nRowLogEst, pIndex: &fakeIdx);
111808 pTable->szTabRow = fakeIdx.szIdxRow;
111809 pTable->tabFlags |= TF_HasStat1;
111810 }
111811
111812 return 0;
111813}
111814
111815/*
111816** If the Index.aSample variable is not NULL, delete the aSample[] array
111817** and its contents.
111818*/
111819SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
111820#ifdef SQLITE_ENABLE_STAT4
111821 if( pIdx->aSample ){
111822 int j;
111823 for(j=0; j<pIdx->nSample; j++){
111824 IndexSample *p = &pIdx->aSample[j];
111825 sqlite3DbFree(db, p->p);
111826 }
111827 sqlite3DbFree(db, pIdx->aSample);
111828 }
111829 if( db && db->pnBytesFreed==0 ){
111830 pIdx->nSample = 0;
111831 pIdx->aSample = 0;
111832 }
111833#else
111834 UNUSED_PARAMETER(db);
111835 UNUSED_PARAMETER(pIdx);
111836#endif /* SQLITE_ENABLE_STAT4 */
111837}
111838
111839#ifdef SQLITE_ENABLE_STAT4
111840/*
111841** Populate the pIdx->aAvgEq[] array based on the samples currently
111842** stored in pIdx->aSample[].
111843*/
111844static void initAvgEq(Index *pIdx){
111845 if( pIdx ){
111846 IndexSample *aSample = pIdx->aSample;
111847 IndexSample *pFinal = &aSample[pIdx->nSample-1];
111848 int iCol;
111849 int nCol = 1;
111850 if( pIdx->nSampleCol>1 ){
111851 /* If this is stat4 data, then calculate aAvgEq[] values for all
111852 ** sample columns except the last. The last is always set to 1, as
111853 ** once the trailing PK fields are considered all index keys are
111854 ** unique. */
111855 nCol = pIdx->nSampleCol-1;
111856 pIdx->aAvgEq[nCol] = 1;
111857 }
111858 for(iCol=0; iCol<nCol; iCol++){
111859 int nSample = pIdx->nSample;
111860 int i; /* Used to iterate through samples */
111861 tRowcnt sumEq = 0; /* Sum of the nEq values */
111862 tRowcnt avgEq = 0;
111863 tRowcnt nRow; /* Number of rows in index */
111864 i64 nSum100 = 0; /* Number of terms contributing to sumEq */
111865 i64 nDist100; /* Number of distinct values in index */
111866
111867 if( !pIdx->aiRowEst || iCol>=pIdx->nKeyCol || pIdx->aiRowEst[iCol+1]==0 ){
111868 nRow = pFinal->anLt[iCol];
111869 nDist100 = (i64)100 * pFinal->anDLt[iCol];
111870 nSample--;
111871 }else{
111872 nRow = pIdx->aiRowEst[0];
111873 nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1];
111874 }
111875 pIdx->nRowEst0 = nRow;
111876
111877 /* Set nSum to the number of distinct (iCol+1) field prefixes that
111878 ** occur in the stat4 table for this index. Set sumEq to the sum of
111879 ** the nEq values for column iCol for the same set (adding the value
111880 ** only once where there exist duplicate prefixes). */
111881 for(i=0; i<nSample; i++){
111882 if( i==(pIdx->nSample-1)
111883 || aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol]
111884 ){
111885 sumEq += aSample[i].anEq[iCol];
111886 nSum100 += 100;
111887 }
111888 }
111889
111890 if( nDist100>nSum100 && sumEq<nRow ){
111891 avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
111892 }
111893 if( avgEq==0 ) avgEq = 1;
111894 pIdx->aAvgEq[iCol] = avgEq;
111895 }
111896 }
111897}
111898
111899/*
111900** Look up an index by name. Or, if the name of a WITHOUT ROWID table
111901** is supplied instead, find the PRIMARY KEY index for that table.
111902*/
111903static Index *findIndexOrPrimaryKey(
111904 sqlite3 *db,
111905 const char *zName,
111906 const char *zDb
111907){
111908 Index *pIdx = sqlite3FindIndex(db, zName, zDb);
111909 if( pIdx==0 ){
111910 Table *pTab = sqlite3FindTable(db, zName, zDb);
111911 if( pTab && !HasRowid(pTab) ) pIdx = sqlite3PrimaryKeyIndex(pTab);
111912 }
111913 return pIdx;
111914}
111915
111916/*
111917** Load the content from either the sqlite_stat4
111918** into the relevant Index.aSample[] arrays.
111919**
111920** Arguments zSql1 and zSql2 must point to SQL statements that return
111921** data equivalent to the following:
111922**
111923** zSql1: SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx
111924** zSql2: SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4
111925**
111926** where %Q is replaced with the database name before the SQL is executed.
111927*/
111928static int loadStatTbl(
111929 sqlite3 *db, /* Database handle */
111930 const char *zSql1, /* SQL statement 1 (see above) */
111931 const char *zSql2, /* SQL statement 2 (see above) */
111932 const char *zDb /* Database name (e.g. "main") */
111933){
111934 int rc; /* Result codes from subroutines */
111935 sqlite3_stmt *pStmt = 0; /* An SQL statement being run */
111936 char *zSql; /* Text of the SQL statement */
111937 Index *pPrevIdx = 0; /* Previous index in the loop */
111938 IndexSample *pSample; /* A slot in pIdx->aSample[] */
111939
111940 assert( db->lookaside.bDisable );
111941 zSql = sqlite3MPrintf(db, zSql1, zDb);
111942 if( !zSql ){
111943 return SQLITE_NOMEM_BKPT;
111944 }
111945 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
111946 sqlite3DbFree(db, zSql);
111947 if( rc ) return rc;
111948
111949 while( sqlite3_step(pStmt)==SQLITE_ROW ){
111950 int nIdxCol = 1; /* Number of columns in stat4 records */
111951
111952 char *zIndex; /* Index name */
111953 Index *pIdx; /* Pointer to the index object */
111954 int nSample; /* Number of samples */
111955 int nByte; /* Bytes of space required */
111956 int i; /* Bytes of space required */
111957 tRowcnt *pSpace;
111958
111959 zIndex = (char *)sqlite3_column_text(pStmt, 0);
111960 if( zIndex==0 ) continue;
111961 nSample = sqlite3_column_int(pStmt, 1);
111962 pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
111963 assert( pIdx==0 || pIdx->nSample==0 );
111964 if( pIdx==0 ) continue;
111965 assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 );
111966 if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
111967 nIdxCol = pIdx->nKeyCol;
111968 }else{
111969 nIdxCol = pIdx->nColumn;
111970 }
111971 pIdx->nSampleCol = nIdxCol;
111972 nByte = sizeof(IndexSample) * nSample;
111973 nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample;
111974 nByte += nIdxCol * sizeof(tRowcnt); /* Space for Index.aAvgEq[] */
111975
111976 pIdx->aSample = sqlite3DbMallocZero(db, nByte);
111977 if( pIdx->aSample==0 ){
111978 sqlite3_finalize(pStmt);
111979 return SQLITE_NOMEM_BKPT;
111980 }
111981 pSpace = (tRowcnt*)&pIdx->aSample[nSample];
111982 pIdx->aAvgEq = pSpace; pSpace += nIdxCol;
111983 pIdx->pTable->tabFlags |= TF_HasStat4;
111984 for(i=0; i<nSample; i++){
111985 pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
111986 pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
111987 pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
111988 }
111989 assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) );
111990 }
111991 rc = sqlite3_finalize(pStmt);
111992 if( rc ) return rc;
111993
111994 zSql = sqlite3MPrintf(db, zSql2, zDb);
111995 if( !zSql ){
111996 return SQLITE_NOMEM_BKPT;
111997 }
111998 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
111999 sqlite3DbFree(db, zSql);
112000 if( rc ) return rc;
112001
112002 while( sqlite3_step(pStmt)==SQLITE_ROW ){
112003 char *zIndex; /* Index name */
112004 Index *pIdx; /* Pointer to the index object */
112005 int nCol = 1; /* Number of columns in index */
112006
112007 zIndex = (char *)sqlite3_column_text(pStmt, 0);
112008 if( zIndex==0 ) continue;
112009 pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
112010 if( pIdx==0 ) continue;
112011 /* This next condition is true if data has already been loaded from
112012 ** the sqlite_stat4 table. */
112013 nCol = pIdx->nSampleCol;
112014 if( pIdx!=pPrevIdx ){
112015 initAvgEq(pPrevIdx);
112016 pPrevIdx = pIdx;
112017 }
112018 pSample = &pIdx->aSample[pIdx->nSample];
112019 decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0);
112020 decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0);
112021 decodeIntArray((char*)sqlite3_column_text(pStmt,3),nCol,pSample->anDLt,0,0);
112022
112023 /* Take a copy of the sample. Add two 0x00 bytes the end of the buffer.
112024 ** This is in case the sample record is corrupted. In that case, the
112025 ** sqlite3VdbeRecordCompare() may read up to two varints past the
112026 ** end of the allocated buffer before it realizes it is dealing with
112027 ** a corrupt record. Adding the two 0x00 bytes prevents this from causing
112028 ** a buffer overread. */
112029 pSample->n = sqlite3_column_bytes(pStmt, 4);
112030 pSample->p = sqlite3DbMallocZero(db, pSample->n + 2);
112031 if( pSample->p==0 ){
112032 sqlite3_finalize(pStmt);
112033 return SQLITE_NOMEM_BKPT;
112034 }
112035 if( pSample->n ){
112036 memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
112037 }
112038 pIdx->nSample++;
112039 }
112040 rc = sqlite3_finalize(pStmt);
112041 if( rc==SQLITE_OK ) initAvgEq(pPrevIdx);
112042 return rc;
112043}
112044
112045/*
112046** Load content from the sqlite_stat4 table into
112047** the Index.aSample[] arrays of all indices.
112048*/
112049static int loadStat4(sqlite3 *db, const char *zDb){
112050 int rc = SQLITE_OK; /* Result codes from subroutines */
112051 const Table *pStat4;
112052
112053 assert( db->lookaside.bDisable );
112054 if( (pStat4 = sqlite3FindTable(db, "sqlite_stat4", zDb))!=0
112055 && IsOrdinaryTable(pStat4)
112056 ){
112057 rc = loadStatTbl(db,
112058 "SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx",
112059 "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4",
112060 zDb
112061 );
112062 }
112063 return rc;
112064}
112065#endif /* SQLITE_ENABLE_STAT4 */
112066
112067/*
112068** Load the content of the sqlite_stat1 and sqlite_stat4 tables. The
112069** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
112070** arrays. The contents of sqlite_stat4 are used to populate the
112071** Index.aSample[] arrays.
112072**
112073** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
112074** is returned. In this case, even if SQLITE_ENABLE_STAT4 was defined
112075** during compilation and the sqlite_stat4 table is present, no data is
112076** read from it.
112077**
112078** If SQLITE_ENABLE_STAT4 was defined during compilation and the
112079** sqlite_stat4 table is not present in the database, SQLITE_ERROR is
112080** returned. However, in this case, data is read from the sqlite_stat1
112081** table (if it is present) before returning.
112082**
112083** If an OOM error occurs, this function always sets db->mallocFailed.
112084** This means if the caller does not care about other errors, the return
112085** code may be ignored.
112086*/
112087SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
112088 analysisInfo sInfo;
112089 HashElem *i;
112090 char *zSql;
112091 int rc = SQLITE_OK;
112092 Schema *pSchema = db->aDb[iDb].pSchema;
112093 const Table *pStat1;
112094
112095 assert( iDb>=0 && iDb<db->nDb );
112096 assert( db->aDb[iDb].pBt!=0 );
112097
112098 /* Clear any prior statistics */
112099 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
112100 for(i=sqliteHashFirst(&pSchema->tblHash); i; i=sqliteHashNext(i)){
112101 Table *pTab = sqliteHashData(i);
112102 pTab->tabFlags &= ~TF_HasStat1;
112103 }
112104 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
112105 Index *pIdx = sqliteHashData(i);
112106 pIdx->hasStat1 = 0;
112107#ifdef SQLITE_ENABLE_STAT4
112108 sqlite3DeleteIndexSamples(db, pIdx);
112109 pIdx->aSample = 0;
112110#endif
112111 }
112112
112113 /* Load new statistics out of the sqlite_stat1 table */
112114 sInfo.db = db;
112115 sInfo.zDatabase = db->aDb[iDb].zDbSName;
112116 if( (pStat1 = sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase))
112117 && IsOrdinaryTable(pStat1)
112118 ){
112119 zSql = sqlite3MPrintf(db,
112120 zFormat: "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
112121 if( zSql==0 ){
112122 rc = SQLITE_NOMEM_BKPT;
112123 }else{
112124 rc = sqlite3_exec(db, sql: zSql, callback: analysisLoader, &sInfo, errmsg: 0);
112125 sqlite3DbFree(db, p: zSql);
112126 }
112127 }
112128
112129 /* Set appropriate defaults on all indexes not in the sqlite_stat1 table */
112130 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
112131 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
112132 Index *pIdx = sqliteHashData(i);
112133 if( !pIdx->hasStat1 ) sqlite3DefaultRowEst(pIdx);
112134 }
112135
112136 /* Load the statistics from the sqlite_stat4 table. */
112137#ifdef SQLITE_ENABLE_STAT4
112138 if( rc==SQLITE_OK ){
112139 DisableLookaside;
112140 rc = loadStat4(db, sInfo.zDatabase);
112141 EnableLookaside;
112142 }
112143 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
112144 Index *pIdx = sqliteHashData(i);
112145 sqlite3_free(pIdx->aiRowEst);
112146 pIdx->aiRowEst = 0;
112147 }
112148#endif
112149
112150 if( rc==SQLITE_NOMEM ){
112151 sqlite3OomFault(db);
112152 }
112153 return rc;
112154}
112155
112156
112157#endif /* SQLITE_OMIT_ANALYZE */
112158
112159/************** End of analyze.c *********************************************/
112160/************** Begin file attach.c ******************************************/
112161/*
112162** 2003 April 6
112163**
112164** The author disclaims copyright to this source code. In place of
112165** a legal notice, here is a blessing:
112166**
112167** May you do good and not evil.
112168** May you find forgiveness for yourself and forgive others.
112169** May you share freely, never taking more than you give.
112170**
112171*************************************************************************
112172** This file contains code used to implement the ATTACH and DETACH commands.
112173*/
112174/* #include "sqliteInt.h" */
112175
112176#ifndef SQLITE_OMIT_ATTACH
112177/*
112178** Resolve an expression that was part of an ATTACH or DETACH statement. This
112179** is slightly different from resolving a normal SQL expression, because simple
112180** identifiers are treated as strings, not possible column names or aliases.
112181**
112182** i.e. if the parser sees:
112183**
112184** ATTACH DATABASE abc AS def
112185**
112186** it treats the two expressions as literal strings 'abc' and 'def' instead of
112187** looking for columns of the same name.
112188**
112189** This only applies to the root node of pExpr, so the statement:
112190**
112191** ATTACH DATABASE abc||def AS 'db2'
112192**
112193** will fail because neither abc or def can be resolved.
112194*/
112195static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
112196{
112197 int rc = SQLITE_OK;
112198 if( pExpr ){
112199 if( pExpr->op!=TK_ID ){
112200 rc = sqlite3ResolveExprNames(pNC: pName, pExpr);
112201 }else{
112202 pExpr->op = TK_STRING;
112203 }
112204 }
112205 return rc;
112206}
112207
112208/*
112209** Return true if zName points to a name that may be used to refer to
112210** database iDb attached to handle db.
112211*/
112212SQLITE_PRIVATE int sqlite3DbIsNamed(sqlite3 *db, int iDb, const char *zName){
112213 return (
112214 sqlite3StrICmp(zLeft: db->aDb[iDb].zDbSName, zRight: zName)==0
112215 || (iDb==0 && sqlite3StrICmp(zLeft: "main", zRight: zName)==0)
112216 );
112217}
112218
112219/*
112220** An SQL user-function registered to do the work of an ATTACH statement. The
112221** three arguments to the function come directly from an attach statement:
112222**
112223** ATTACH DATABASE x AS y KEY z
112224**
112225** SELECT sqlite_attach(x, y, z)
112226**
112227** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
112228** third argument.
112229**
112230** If the db->init.reopenMemdb flags is set, then instead of attaching a
112231** new database, close the database on db->init.iDb and reopen it as an
112232** empty MemDB.
112233*/
112234static void attachFunc(
112235 sqlite3_context *context,
112236 int NotUsed,
112237 sqlite3_value **argv
112238){
112239 int i;
112240 int rc = 0;
112241 sqlite3 *db = sqlite3_context_db_handle(p: context);
112242 const char *zName;
112243 const char *zFile;
112244 char *zPath = 0;
112245 char *zErr = 0;
112246 unsigned int flags;
112247 Db *aNew; /* New array of Db pointers */
112248 Db *pNew; /* Db object for the newly attached database */
112249 char *zErrDyn = 0;
112250 sqlite3_vfs *pVfs;
112251
112252 UNUSED_PARAMETER(NotUsed);
112253 zFile = (const char *)sqlite3_value_text(pVal: argv[0]);
112254 zName = (const char *)sqlite3_value_text(pVal: argv[1]);
112255 if( zFile==0 ) zFile = "";
112256 if( zName==0 ) zName = "";
112257
112258#ifndef SQLITE_OMIT_DESERIALIZE
112259# define REOPEN_AS_MEMDB(db) (db->init.reopenMemdb)
112260#else
112261# define REOPEN_AS_MEMDB(db) (0)
112262#endif
112263
112264 if( REOPEN_AS_MEMDB(db) ){
112265 /* This is not a real ATTACH. Instead, this routine is being called
112266 ** from sqlite3_deserialize() to close database db->init.iDb and
112267 ** reopen it as a MemDB */
112268 pVfs = sqlite3_vfs_find(zVfs: "memdb");
112269 if( pVfs==0 ) return;
112270 pNew = &db->aDb[db->init.iDb];
112271 if( pNew->pBt ) sqlite3BtreeClose(p: pNew->pBt);
112272 pNew->pBt = 0;
112273 pNew->pSchema = 0;
112274 rc = sqlite3BtreeOpen(pVfs, zFilename: "x\0", db, ppBtree: &pNew->pBt, flags: 0, SQLITE_OPEN_MAIN_DB);
112275 }else{
112276 /* This is a real ATTACH
112277 **
112278 ** Check for the following errors:
112279 **
112280 ** * Too many attached databases,
112281 ** * Transaction currently open
112282 ** * Specified database name already being used.
112283 */
112284 if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
112285 zErrDyn = sqlite3MPrintf(db, zFormat: "too many attached databases - max %d",
112286 db->aLimit[SQLITE_LIMIT_ATTACHED]
112287 );
112288 goto attach_error;
112289 }
112290 for(i=0; i<db->nDb; i++){
112291 assert( zName );
112292 if( sqlite3DbIsNamed(db, iDb: i, zName) ){
112293 zErrDyn = sqlite3MPrintf(db, zFormat: "database %s is already in use", zName);
112294 goto attach_error;
112295 }
112296 }
112297
112298 /* Allocate the new entry in the db->aDb[] array and initialize the schema
112299 ** hash tables.
112300 */
112301 if( db->aDb==db->aDbStatic ){
112302 aNew = sqlite3DbMallocRawNN(db, n: sizeof(db->aDb[0])*3 );
112303 if( aNew==0 ) return;
112304 memcpy(dest: aNew, src: db->aDb, n: sizeof(db->aDb[0])*2);
112305 }else{
112306 aNew = sqlite3DbRealloc(db, p: db->aDb, n: sizeof(db->aDb[0])*(db->nDb+1) );
112307 if( aNew==0 ) return;
112308 }
112309 db->aDb = aNew;
112310 pNew = &db->aDb[db->nDb];
112311 memset(s: pNew, c: 0, n: sizeof(*pNew));
112312
112313 /* Open the database file. If the btree is successfully opened, use
112314 ** it to obtain the database schema. At this point the schema may
112315 ** or may not be initialized.
112316 */
112317 flags = db->openFlags;
112318 rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
112319 if( rc!=SQLITE_OK ){
112320 if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
112321 sqlite3_result_error(pCtx: context, z: zErr, n: -1);
112322 sqlite3_free(p: zErr);
112323 return;
112324 }
112325 assert( pVfs );
112326 flags |= SQLITE_OPEN_MAIN_DB;
112327 rc = sqlite3BtreeOpen(pVfs, zFilename: zPath, db, ppBtree: &pNew->pBt, flags: 0, vfsFlags: flags);
112328 db->nDb++;
112329 pNew->zDbSName = sqlite3DbStrDup(db, z: zName);
112330 }
112331 db->noSharedCache = 0;
112332 if( rc==SQLITE_CONSTRAINT ){
112333 rc = SQLITE_ERROR;
112334 zErrDyn = sqlite3MPrintf(db, zFormat: "database is already attached");
112335 }else if( rc==SQLITE_OK ){
112336 Pager *pPager;
112337 pNew->pSchema = sqlite3SchemaGet(db, pNew->pBt);
112338 if( !pNew->pSchema ){
112339 rc = SQLITE_NOMEM_BKPT;
112340 }else if( pNew->pSchema->file_format && pNew->pSchema->enc!=ENC(db) ){
112341 zErrDyn = sqlite3MPrintf(db,
112342 zFormat: "attached databases must use the same text encoding as main database");
112343 rc = SQLITE_ERROR;
112344 }
112345 sqlite3BtreeEnter(p: pNew->pBt);
112346 pPager = sqlite3BtreePager(p: pNew->pBt);
112347 sqlite3PagerLockingMode(pPager, eMode: db->dfltLockMode);
112348 sqlite3BtreeSecureDelete(p: pNew->pBt,
112349 newFlag: sqlite3BtreeSecureDelete(p: db->aDb[0].pBt,newFlag: -1) );
112350#ifndef SQLITE_OMIT_PAGER_PRAGMAS
112351 sqlite3BtreeSetPagerFlags(p: pNew->pBt,
112352 PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
112353#endif
112354 sqlite3BtreeLeave(p: pNew->pBt);
112355 }
112356 pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
112357 if( rc==SQLITE_OK && pNew->zDbSName==0 ){
112358 rc = SQLITE_NOMEM_BKPT;
112359 }
112360 sqlite3_free_filename( zPath );
112361
112362 /* If the file was opened successfully, read the schema for the new database.
112363 ** If this fails, or if opening the file failed, then close the file and
112364 ** remove the entry from the db->aDb[] array. i.e. put everything back the
112365 ** way we found it.
112366 */
112367 if( rc==SQLITE_OK ){
112368 sqlite3BtreeEnterAll(db);
112369 db->init.iDb = 0;
112370 db->mDbFlags &= ~(DBFLAG_SchemaKnownOk);
112371 if( !REOPEN_AS_MEMDB(db) ){
112372 rc = sqlite3Init(db, &zErrDyn);
112373 }
112374 sqlite3BtreeLeaveAll(db);
112375 assert( zErrDyn==0 || rc!=SQLITE_OK );
112376 }
112377#ifdef SQLITE_USER_AUTHENTICATION
112378 if( rc==SQLITE_OK && !REOPEN_AS_MEMDB(db) ){
112379 u8 newAuth = 0;
112380 rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth);
112381 if( newAuth<db->auth.authLevel ){
112382 rc = SQLITE_AUTH_USER;
112383 }
112384 }
112385#endif
112386 if( rc ){
112387 if( !REOPEN_AS_MEMDB(db) ){
112388 int iDb = db->nDb - 1;
112389 assert( iDb>=2 );
112390 if( db->aDb[iDb].pBt ){
112391 sqlite3BtreeClose(p: db->aDb[iDb].pBt);
112392 db->aDb[iDb].pBt = 0;
112393 db->aDb[iDb].pSchema = 0;
112394 }
112395 sqlite3ResetAllSchemasOfConnection(db);
112396 db->nDb = iDb;
112397 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
112398 sqlite3OomFault(db);
112399 sqlite3DbFree(db, p: zErrDyn);
112400 zErrDyn = sqlite3MPrintf(db, zFormat: "out of memory");
112401 }else if( zErrDyn==0 ){
112402 zErrDyn = sqlite3MPrintf(db, zFormat: "unable to open database: %s", zFile);
112403 }
112404 }
112405 goto attach_error;
112406 }
112407
112408 return;
112409
112410attach_error:
112411 /* Return an error if we get here */
112412 if( zErrDyn ){
112413 sqlite3_result_error(pCtx: context, z: zErrDyn, n: -1);
112414 sqlite3DbFree(db, p: zErrDyn);
112415 }
112416 if( rc ) sqlite3_result_error_code(pCtx: context, errCode: rc);
112417}
112418
112419/*
112420** An SQL user-function registered to do the work of an DETACH statement. The
112421** three arguments to the function come directly from a detach statement:
112422**
112423** DETACH DATABASE x
112424**
112425** SELECT sqlite_detach(x)
112426*/
112427static void detachFunc(
112428 sqlite3_context *context,
112429 int NotUsed,
112430 sqlite3_value **argv
112431){
112432 const char *zName = (const char *)sqlite3_value_text(pVal: argv[0]);
112433 sqlite3 *db = sqlite3_context_db_handle(p: context);
112434 int i;
112435 Db *pDb = 0;
112436 HashElem *pEntry;
112437 char zErr[128];
112438
112439 UNUSED_PARAMETER(NotUsed);
112440
112441 if( zName==0 ) zName = "";
112442 for(i=0; i<db->nDb; i++){
112443 pDb = &db->aDb[i];
112444 if( pDb->pBt==0 ) continue;
112445 if( sqlite3DbIsNamed(db, iDb: i, zName) ) break;
112446 }
112447
112448 if( i>=db->nDb ){
112449 sqlite3_snprintf(n: sizeof(zErr),zBuf: zErr, zFormat: "no such database: %s", zName);
112450 goto detach_error;
112451 }
112452 if( i<2 ){
112453 sqlite3_snprintf(n: sizeof(zErr),zBuf: zErr, zFormat: "cannot detach database %s", zName);
112454 goto detach_error;
112455 }
112456 if( sqlite3BtreeTxnState(p: pDb->pBt)!=SQLITE_TXN_NONE
112457 || sqlite3BtreeIsInBackup(p: pDb->pBt)
112458 ){
112459 sqlite3_snprintf(n: sizeof(zErr),zBuf: zErr, zFormat: "database %s is locked", zName);
112460 goto detach_error;
112461 }
112462
112463 /* If any TEMP triggers reference the schema being detached, move those
112464 ** triggers to reference the TEMP schema itself. */
112465 assert( db->aDb[1].pSchema );
112466 pEntry = sqliteHashFirst(&db->aDb[1].pSchema->trigHash);
112467 while( pEntry ){
112468 Trigger *pTrig = (Trigger*)sqliteHashData(pEntry);
112469 if( pTrig->pTabSchema==pDb->pSchema ){
112470 pTrig->pTabSchema = pTrig->pSchema;
112471 }
112472 pEntry = sqliteHashNext(pEntry);
112473 }
112474
112475 sqlite3BtreeClose(p: pDb->pBt);
112476 pDb->pBt = 0;
112477 pDb->pSchema = 0;
112478 sqlite3CollapseDatabaseArray(db);
112479 return;
112480
112481detach_error:
112482 sqlite3_result_error(pCtx: context, z: zErr, n: -1);
112483}
112484
112485/*
112486** This procedure generates VDBE code for a single invocation of either the
112487** sqlite_detach() or sqlite_attach() SQL user functions.
112488*/
112489static void codeAttach(
112490 Parse *pParse, /* The parser context */
112491 int type, /* Either SQLITE_ATTACH or SQLITE_DETACH */
112492 FuncDef const *pFunc,/* FuncDef wrapper for detachFunc() or attachFunc() */
112493 Expr *pAuthArg, /* Expression to pass to authorization callback */
112494 Expr *pFilename, /* Name of database file */
112495 Expr *pDbname, /* Name of the database to use internally */
112496 Expr *pKey /* Database key for encryption extension */
112497){
112498 int rc;
112499 NameContext sName;
112500 Vdbe *v;
112501 sqlite3* db = pParse->db;
112502 int regArgs;
112503
112504 if( pParse->nErr ) goto attach_end;
112505 memset(s: &sName, c: 0, n: sizeof(NameContext));
112506 sName.pParse = pParse;
112507
112508 if(
112509 SQLITE_OK!=resolveAttachExpr(pName: &sName, pExpr: pFilename) ||
112510 SQLITE_OK!=resolveAttachExpr(pName: &sName, pExpr: pDbname) ||
112511 SQLITE_OK!=resolveAttachExpr(pName: &sName, pExpr: pKey)
112512 ){
112513 goto attach_end;
112514 }
112515
112516#ifndef SQLITE_OMIT_AUTHORIZATION
112517 if( pAuthArg ){
112518 char *zAuthArg;
112519 if( pAuthArg->op==TK_STRING ){
112520 assert( !ExprHasProperty(pAuthArg, EP_IntValue) );
112521 zAuthArg = pAuthArg->u.zToken;
112522 }else{
112523 zAuthArg = 0;
112524 }
112525 rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
112526 if(rc!=SQLITE_OK ){
112527 goto attach_end;
112528 }
112529 }
112530#endif /* SQLITE_OMIT_AUTHORIZATION */
112531
112532
112533 v = sqlite3GetVdbe(pParse);
112534 regArgs = sqlite3GetTempRange(pParse, nReg: 4);
112535 sqlite3ExprCode(pParse, pExpr: pFilename, target: regArgs);
112536 sqlite3ExprCode(pParse, pExpr: pDbname, target: regArgs+1);
112537 sqlite3ExprCode(pParse, pExpr: pKey, target: regArgs+2);
112538
112539 assert( v || db->mallocFailed );
112540 if( v ){
112541 sqlite3VdbeAddFunctionCall(pParse, p1: 0, p2: regArgs+3-pFunc->nArg, p3: regArgs+3,
112542 nArg: pFunc->nArg, pFunc, eCallCtx: 0);
112543 /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
112544 ** statement only). For DETACH, set it to false (expire all existing
112545 ** statements).
112546 */
112547 sqlite3VdbeAddOp1(p: v, OP_Expire, p1: (type==SQLITE_ATTACH));
112548 }
112549
112550attach_end:
112551 sqlite3ExprDelete(db, p: pFilename);
112552 sqlite3ExprDelete(db, p: pDbname);
112553 sqlite3ExprDelete(db, p: pKey);
112554}
112555
112556/*
112557** Called by the parser to compile a DETACH statement.
112558**
112559** DETACH pDbname
112560*/
112561SQLITE_PRIVATE void sqlite3Detach(Parse *pParse, Expr *pDbname){
112562 static const FuncDef detach_func = {
112563 1, /* nArg */
112564 SQLITE_UTF8, /* funcFlags */
112565 0, /* pUserData */
112566 0, /* pNext */
112567 detachFunc, /* xSFunc */
112568 0, /* xFinalize */
112569 0, 0, /* xValue, xInverse */
112570 "sqlite_detach", /* zName */
112571 {0}
112572 };
112573 codeAttach(pParse, SQLITE_DETACH, pFunc: &detach_func, pAuthArg: pDbname, pFilename: 0, pDbname: 0, pKey: pDbname);
112574}
112575
112576/*
112577** Called by the parser to compile an ATTACH statement.
112578**
112579** ATTACH p AS pDbname KEY pKey
112580*/
112581SQLITE_PRIVATE void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){
112582 static const FuncDef attach_func = {
112583 3, /* nArg */
112584 SQLITE_UTF8, /* funcFlags */
112585 0, /* pUserData */
112586 0, /* pNext */
112587 attachFunc, /* xSFunc */
112588 0, /* xFinalize */
112589 0, 0, /* xValue, xInverse */
112590 "sqlite_attach", /* zName */
112591 {0}
112592 };
112593 codeAttach(pParse, SQLITE_ATTACH, pFunc: &attach_func, pAuthArg: p, pFilename: p, pDbname, pKey);
112594}
112595#endif /* SQLITE_OMIT_ATTACH */
112596
112597/*
112598** Expression callback used by sqlite3FixAAAA() routines.
112599*/
112600static int fixExprCb(Walker *p, Expr *pExpr){
112601 DbFixer *pFix = p->u.pFix;
112602 if( !pFix->bTemp ) ExprSetProperty(pExpr, EP_FromDDL);
112603 if( pExpr->op==TK_VARIABLE ){
112604 if( pFix->pParse->db->init.busy ){
112605 pExpr->op = TK_NULL;
112606 }else{
112607 sqlite3ErrorMsg(pParse: pFix->pParse, zFormat: "%s cannot use variables", pFix->zType);
112608 return WRC_Abort;
112609 }
112610 }
112611 return WRC_Continue;
112612}
112613
112614/*
112615** Select callback used by sqlite3FixAAAA() routines.
112616*/
112617static int fixSelectCb(Walker *p, Select *pSelect){
112618 DbFixer *pFix = p->u.pFix;
112619 int i;
112620 SrcItem *pItem;
112621 sqlite3 *db = pFix->pParse->db;
112622 int iDb = sqlite3FindDbName(db, pFix->zDb);
112623 SrcList *pList = pSelect->pSrc;
112624
112625 if( NEVER(pList==0) ) return WRC_Continue;
112626 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
112627 if( pFix->bTemp==0 ){
112628 if( pItem->zDatabase ){
112629 if( iDb!=sqlite3FindDbName(db, pItem->zDatabase) ){
112630 sqlite3ErrorMsg(pParse: pFix->pParse,
112631 zFormat: "%s %T cannot reference objects in database %s",
112632 pFix->zType, pFix->pName, pItem->zDatabase);
112633 return WRC_Abort;
112634 }
112635 sqlite3DbFree(db, p: pItem->zDatabase);
112636 pItem->zDatabase = 0;
112637 pItem->fg.notCte = 1;
112638 }
112639 pItem->pSchema = pFix->pSchema;
112640 pItem->fg.fromDDL = 1;
112641 }
112642#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
112643 if( sqlite3WalkExpr(pWalker: &pFix->w, pExpr: pList->a[i].pOn) ) return WRC_Abort;
112644#endif
112645 }
112646 if( pSelect->pWith ){
112647 for(i=0; i<pSelect->pWith->nCte; i++){
112648 if( sqlite3WalkSelect(pWalker: p, p: pSelect->pWith->a[i].pSelect) ){
112649 return WRC_Abort;
112650 }
112651 }
112652 }
112653 return WRC_Continue;
112654}
112655
112656/*
112657** Initialize a DbFixer structure. This routine must be called prior
112658** to passing the structure to one of the sqliteFixAAAA() routines below.
112659*/
112660SQLITE_PRIVATE void sqlite3FixInit(
112661 DbFixer *pFix, /* The fixer to be initialized */
112662 Parse *pParse, /* Error messages will be written here */
112663 int iDb, /* This is the database that must be used */
112664 const char *zType, /* "view", "trigger", or "index" */
112665 const Token *pName /* Name of the view, trigger, or index */
112666){
112667 sqlite3 *db = pParse->db;
112668 assert( db->nDb>iDb );
112669 pFix->pParse = pParse;
112670 pFix->zDb = db->aDb[iDb].zDbSName;
112671 pFix->pSchema = db->aDb[iDb].pSchema;
112672 pFix->zType = zType;
112673 pFix->pName = pName;
112674 pFix->bTemp = (iDb==1);
112675 pFix->w.pParse = pParse;
112676 pFix->w.xExprCallback = fixExprCb;
112677 pFix->w.xSelectCallback = fixSelectCb;
112678 pFix->w.xSelectCallback2 = sqlite3WalkWinDefnDummyCallback;
112679 pFix->w.walkerDepth = 0;
112680 pFix->w.eCode = 0;
112681 pFix->w.u.pFix = pFix;
112682}
112683
112684/*
112685** The following set of routines walk through the parse tree and assign
112686** a specific database to all table references where the database name
112687** was left unspecified in the original SQL statement. The pFix structure
112688** must have been initialized by a prior call to sqlite3FixInit().
112689**
112690** These routines are used to make sure that an index, trigger, or
112691** view in one database does not refer to objects in a different database.
112692** (Exception: indices, triggers, and views in the TEMP database are
112693** allowed to refer to anything.) If a reference is explicitly made
112694** to an object in a different database, an error message is added to
112695** pParse->zErrMsg and these routines return non-zero. If everything
112696** checks out, these routines return 0.
112697*/
112698SQLITE_PRIVATE int sqlite3FixSrcList(
112699 DbFixer *pFix, /* Context of the fixation */
112700 SrcList *pList /* The Source list to check and modify */
112701){
112702 int res = 0;
112703 if( pList ){
112704 Select s;
112705 memset(s: &s, c: 0, n: sizeof(s));
112706 s.pSrc = pList;
112707 res = sqlite3WalkSelect(pWalker: &pFix->w, p: &s);
112708 }
112709 return res;
112710}
112711#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
112712SQLITE_PRIVATE int sqlite3FixSelect(
112713 DbFixer *pFix, /* Context of the fixation */
112714 Select *pSelect /* The SELECT statement to be fixed to one database */
112715){
112716 return sqlite3WalkSelect(pWalker: &pFix->w, p: pSelect);
112717}
112718SQLITE_PRIVATE int sqlite3FixExpr(
112719 DbFixer *pFix, /* Context of the fixation */
112720 Expr *pExpr /* The expression to be fixed to one database */
112721){
112722 return sqlite3WalkExpr(pWalker: &pFix->w, pExpr);
112723}
112724#endif
112725
112726#ifndef SQLITE_OMIT_TRIGGER
112727SQLITE_PRIVATE int sqlite3FixTriggerStep(
112728 DbFixer *pFix, /* Context of the fixation */
112729 TriggerStep *pStep /* The trigger step be fixed to one database */
112730){
112731 while( pStep ){
112732 if( sqlite3WalkSelect(pWalker: &pFix->w, p: pStep->pSelect)
112733 || sqlite3WalkExpr(pWalker: &pFix->w, pExpr: pStep->pWhere)
112734 || sqlite3WalkExprList(pWalker: &pFix->w, p: pStep->pExprList)
112735 || sqlite3FixSrcList(pFix, pList: pStep->pFrom)
112736 ){
112737 return 1;
112738 }
112739#ifndef SQLITE_OMIT_UPSERT
112740 {
112741 Upsert *pUp;
112742 for(pUp=pStep->pUpsert; pUp; pUp=pUp->pNextUpsert){
112743 if( sqlite3WalkExprList(pWalker: &pFix->w, p: pUp->pUpsertTarget)
112744 || sqlite3WalkExpr(pWalker: &pFix->w, pExpr: pUp->pUpsertTargetWhere)
112745 || sqlite3WalkExprList(pWalker: &pFix->w, p: pUp->pUpsertSet)
112746 || sqlite3WalkExpr(pWalker: &pFix->w, pExpr: pUp->pUpsertWhere)
112747 ){
112748 return 1;
112749 }
112750 }
112751 }
112752#endif
112753 pStep = pStep->pNext;
112754 }
112755
112756 return 0;
112757}
112758#endif
112759
112760/************** End of attach.c **********************************************/
112761/************** Begin file auth.c ********************************************/
112762/*
112763** 2003 January 11
112764**
112765** The author disclaims copyright to this source code. In place of
112766** a legal notice, here is a blessing:
112767**
112768** May you do good and not evil.
112769** May you find forgiveness for yourself and forgive others.
112770** May you share freely, never taking more than you give.
112771**
112772*************************************************************************
112773** This file contains code used to implement the sqlite3_set_authorizer()
112774** API. This facility is an optional feature of the library. Embedded
112775** systems that do not need this facility may omit it by recompiling
112776** the library with -DSQLITE_OMIT_AUTHORIZATION=1
112777*/
112778/* #include "sqliteInt.h" */
112779
112780/*
112781** All of the code in this file may be omitted by defining a single
112782** macro.
112783*/
112784#ifndef SQLITE_OMIT_AUTHORIZATION
112785
112786/*
112787** Set or clear the access authorization function.
112788**
112789** The access authorization function is be called during the compilation
112790** phase to verify that the user has read and/or write access permission on
112791** various fields of the database. The first argument to the auth function
112792** is a copy of the 3rd argument to this routine. The second argument
112793** to the auth function is one of these constants:
112794**
112795** SQLITE_CREATE_INDEX
112796** SQLITE_CREATE_TABLE
112797** SQLITE_CREATE_TEMP_INDEX
112798** SQLITE_CREATE_TEMP_TABLE
112799** SQLITE_CREATE_TEMP_TRIGGER
112800** SQLITE_CREATE_TEMP_VIEW
112801** SQLITE_CREATE_TRIGGER
112802** SQLITE_CREATE_VIEW
112803** SQLITE_DELETE
112804** SQLITE_DROP_INDEX
112805** SQLITE_DROP_TABLE
112806** SQLITE_DROP_TEMP_INDEX
112807** SQLITE_DROP_TEMP_TABLE
112808** SQLITE_DROP_TEMP_TRIGGER
112809** SQLITE_DROP_TEMP_VIEW
112810** SQLITE_DROP_TRIGGER
112811** SQLITE_DROP_VIEW
112812** SQLITE_INSERT
112813** SQLITE_PRAGMA
112814** SQLITE_READ
112815** SQLITE_SELECT
112816** SQLITE_TRANSACTION
112817** SQLITE_UPDATE
112818**
112819** The third and fourth arguments to the auth function are the name of
112820** the table and the column that are being accessed. The auth function
112821** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE. If
112822** SQLITE_OK is returned, it means that access is allowed. SQLITE_DENY
112823** means that the SQL statement will never-run - the sqlite3_exec() call
112824** will return with an error. SQLITE_IGNORE means that the SQL statement
112825** should run but attempts to read the specified column will return NULL
112826** and attempts to write the column will be ignored.
112827**
112828** Setting the auth function to NULL disables this hook. The default
112829** setting of the auth function is NULL.
112830*/
112831SQLITE_API int sqlite3_set_authorizer(
112832 sqlite3 *db,
112833 int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
112834 void *pArg
112835){
112836#ifdef SQLITE_ENABLE_API_ARMOR
112837 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
112838#endif
112839 sqlite3_mutex_enter(p: db->mutex);
112840 db->xAuth = (sqlite3_xauth)xAuth;
112841 db->pAuthArg = pArg;
112842 if( db->xAuth ) sqlite3ExpirePreparedStatements(db, iCode: 1);
112843 sqlite3_mutex_leave(p: db->mutex);
112844 return SQLITE_OK;
112845}
112846
112847/*
112848** Write an error message into pParse->zErrMsg that explains that the
112849** user-supplied authorization function returned an illegal value.
112850*/
112851static void sqliteAuthBadReturnCode(Parse *pParse){
112852 sqlite3ErrorMsg(pParse, zFormat: "authorizer malfunction");
112853 pParse->rc = SQLITE_ERROR;
112854}
112855
112856/*
112857** Invoke the authorization callback for permission to read column zCol from
112858** table zTab in database zDb. This function assumes that an authorization
112859** callback has been registered (i.e. that sqlite3.xAuth is not NULL).
112860**
112861** If SQLITE_IGNORE is returned and pExpr is not NULL, then pExpr is changed
112862** to an SQL NULL expression. Otherwise, if pExpr is NULL, then SQLITE_IGNORE
112863** is treated as SQLITE_DENY. In this case an error is left in pParse.
112864*/
112865SQLITE_PRIVATE int sqlite3AuthReadCol(
112866 Parse *pParse, /* The parser context */
112867 const char *zTab, /* Table name */
112868 const char *zCol, /* Column name */
112869 int iDb /* Index of containing database. */
112870){
112871 sqlite3 *db = pParse->db; /* Database handle */
112872 char *zDb = db->aDb[iDb].zDbSName; /* Schema name of attached database */
112873 int rc; /* Auth callback return code */
112874
112875 if( db->init.busy ) return SQLITE_OK;
112876 rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext
112877#ifdef SQLITE_USER_AUTHENTICATION
112878 ,db->auth.zAuthUser
112879#endif
112880 );
112881 if( rc==SQLITE_DENY ){
112882 char *z = sqlite3_mprintf(zFormat: "%s.%s", zTab, zCol);
112883 if( db->nDb>2 || iDb!=0 ) z = sqlite3_mprintf(zFormat: "%s.%z", zDb, z);
112884 sqlite3ErrorMsg(pParse, zFormat: "access to %z is prohibited", z);
112885 pParse->rc = SQLITE_AUTH;
112886 }else if( rc!=SQLITE_IGNORE && rc!=SQLITE_OK ){
112887 sqliteAuthBadReturnCode(pParse);
112888 }
112889 return rc;
112890}
112891
112892/*
112893** The pExpr should be a TK_COLUMN expression. The table referred to
112894** is in pTabList or else it is the NEW or OLD table of a trigger.
112895** Check to see if it is OK to read this particular column.
112896**
112897** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN
112898** instruction into a TK_NULL. If the auth function returns SQLITE_DENY,
112899** then generate an error.
112900*/
112901SQLITE_PRIVATE void sqlite3AuthRead(
112902 Parse *pParse, /* The parser context */
112903 Expr *pExpr, /* The expression to check authorization on */
112904 Schema *pSchema, /* The schema of the expression */
112905 SrcList *pTabList /* All table that pExpr might refer to */
112906){
112907 Table *pTab = 0; /* The table being read */
112908 const char *zCol; /* Name of the column of the table */
112909 int iSrc; /* Index in pTabList->a[] of table being read */
112910 int iDb; /* The index of the database the expression refers to */
112911 int iCol; /* Index of column in table */
112912
112913 assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
112914 assert( !IN_RENAME_OBJECT );
112915 assert( pParse->db->xAuth!=0 );
112916 iDb = sqlite3SchemaToIndex(db: pParse->db, pSchema);
112917 if( iDb<0 ){
112918 /* An attempt to read a column out of a subquery or other
112919 ** temporary table. */
112920 return;
112921 }
112922
112923 if( pExpr->op==TK_TRIGGER ){
112924 pTab = pParse->pTriggerTab;
112925 }else{
112926 assert( pTabList );
112927 for(iSrc=0; iSrc<pTabList->nSrc; iSrc++){
112928 if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
112929 pTab = pTabList->a[iSrc].pTab;
112930 break;
112931 }
112932 }
112933 }
112934 iCol = pExpr->iColumn;
112935 if( pTab==0 ) return;
112936
112937 if( iCol>=0 ){
112938 assert( iCol<pTab->nCol );
112939 zCol = pTab->aCol[iCol].zCnName;
112940 }else if( pTab->iPKey>=0 ){
112941 assert( pTab->iPKey<pTab->nCol );
112942 zCol = pTab->aCol[pTab->iPKey].zCnName;
112943 }else{
112944 zCol = "ROWID";
112945 }
112946 assert( iDb>=0 && iDb<pParse->db->nDb );
112947 if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, zTab: pTab->zName, zCol, iDb) ){
112948 pExpr->op = TK_NULL;
112949 }
112950}
112951
112952/*
112953** Do an authorization check using the code and arguments given. Return
112954** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY. If SQLITE_DENY
112955** is returned, then the error count and error message in pParse are
112956** modified appropriately.
112957*/
112958SQLITE_PRIVATE int sqlite3AuthCheck(
112959 Parse *pParse,
112960 int code,
112961 const char *zArg1,
112962 const char *zArg2,
112963 const char *zArg3
112964){
112965 sqlite3 *db = pParse->db;
112966 int rc;
112967
112968 /* Don't do any authorization checks if the database is initialising
112969 ** or if the parser is being invoked from within sqlite3_declare_vtab.
112970 */
112971 assert( !IN_RENAME_OBJECT || db->xAuth==0 );
112972 if( db->xAuth==0 || db->init.busy || IN_SPECIAL_PARSE ){
112973 return SQLITE_OK;
112974 }
112975
112976 /* EVIDENCE-OF: R-43249-19882 The third through sixth parameters to the
112977 ** callback are either NULL pointers or zero-terminated strings that
112978 ** contain additional details about the action to be authorized.
112979 **
112980 ** The following testcase() macros show that any of the 3rd through 6th
112981 ** parameters can be either NULL or a string. */
112982 testcase( zArg1==0 );
112983 testcase( zArg2==0 );
112984 testcase( zArg3==0 );
112985 testcase( pParse->zAuthContext==0 );
112986
112987 rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext
112988#ifdef SQLITE_USER_AUTHENTICATION
112989 ,db->auth.zAuthUser
112990#endif
112991 );
112992 if( rc==SQLITE_DENY ){
112993 sqlite3ErrorMsg(pParse, zFormat: "not authorized");
112994 pParse->rc = SQLITE_AUTH;
112995 }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){
112996 rc = SQLITE_DENY;
112997 sqliteAuthBadReturnCode(pParse);
112998 }
112999 return rc;
113000}
113001
113002/*
113003** Push an authorization context. After this routine is called, the
113004** zArg3 argument to authorization callbacks will be zContext until
113005** popped. Or if pParse==0, this routine is a no-op.
113006*/
113007SQLITE_PRIVATE void sqlite3AuthContextPush(
113008 Parse *pParse,
113009 AuthContext *pContext,
113010 const char *zContext
113011){
113012 assert( pParse );
113013 pContext->pParse = pParse;
113014 pContext->zAuthContext = pParse->zAuthContext;
113015 pParse->zAuthContext = zContext;
113016}
113017
113018/*
113019** Pop an authorization context that was previously pushed
113020** by sqlite3AuthContextPush
113021*/
113022SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext *pContext){
113023 if( pContext->pParse ){
113024 pContext->pParse->zAuthContext = pContext->zAuthContext;
113025 pContext->pParse = 0;
113026 }
113027}
113028
113029#endif /* SQLITE_OMIT_AUTHORIZATION */
113030
113031/************** End of auth.c ************************************************/
113032/************** Begin file build.c *******************************************/
113033/*
113034** 2001 September 15
113035**
113036** The author disclaims copyright to this source code. In place of
113037** a legal notice, here is a blessing:
113038**
113039** May you do good and not evil.
113040** May you find forgiveness for yourself and forgive others.
113041** May you share freely, never taking more than you give.
113042**
113043*************************************************************************
113044** This file contains C code routines that are called by the SQLite parser
113045** when syntax rules are reduced. The routines in this file handle the
113046** following kinds of SQL syntax:
113047**
113048** CREATE TABLE
113049** DROP TABLE
113050** CREATE INDEX
113051** DROP INDEX
113052** creating ID lists
113053** BEGIN TRANSACTION
113054** COMMIT
113055** ROLLBACK
113056*/
113057/* #include "sqliteInt.h" */
113058
113059#ifndef SQLITE_OMIT_SHARED_CACHE
113060/*
113061** The TableLock structure is only used by the sqlite3TableLock() and
113062** codeTableLocks() functions.
113063*/
113064struct TableLock {
113065 int iDb; /* The database containing the table to be locked */
113066 Pgno iTab; /* The root page of the table to be locked */
113067 u8 isWriteLock; /* True for write lock. False for a read lock */
113068 const char *zLockName; /* Name of the table */
113069};
113070
113071/*
113072** Record the fact that we want to lock a table at run-time.
113073**
113074** The table to be locked has root page iTab and is found in database iDb.
113075** A read or a write lock can be taken depending on isWritelock.
113076**
113077** This routine just records the fact that the lock is desired. The
113078** code to make the lock occur is generated by a later call to
113079** codeTableLocks() which occurs during sqlite3FinishCoding().
113080*/
113081static SQLITE_NOINLINE void lockTable(
113082 Parse *pParse, /* Parsing context */
113083 int iDb, /* Index of the database containing the table to lock */
113084 Pgno iTab, /* Root page number of the table to be locked */
113085 u8 isWriteLock, /* True for a write lock */
113086 const char *zName /* Name of the table to be locked */
113087){
113088 Parse *pToplevel;
113089 int i;
113090 int nBytes;
113091 TableLock *p;
113092 assert( iDb>=0 );
113093
113094 pToplevel = sqlite3ParseToplevel(pParse);
113095 for(i=0; i<pToplevel->nTableLock; i++){
113096 p = &pToplevel->aTableLock[i];
113097 if( p->iDb==iDb && p->iTab==iTab ){
113098 p->isWriteLock = (p->isWriteLock || isWriteLock);
113099 return;
113100 }
113101 }
113102
113103 nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
113104 pToplevel->aTableLock =
113105 sqlite3DbReallocOrFree(db: pToplevel->db, p: pToplevel->aTableLock, n: nBytes);
113106 if( pToplevel->aTableLock ){
113107 p = &pToplevel->aTableLock[pToplevel->nTableLock++];
113108 p->iDb = iDb;
113109 p->iTab = iTab;
113110 p->isWriteLock = isWriteLock;
113111 p->zLockName = zName;
113112 }else{
113113 pToplevel->nTableLock = 0;
113114 sqlite3OomFault(db: pToplevel->db);
113115 }
113116}
113117SQLITE_PRIVATE void sqlite3TableLock(
113118 Parse *pParse, /* Parsing context */
113119 int iDb, /* Index of the database containing the table to lock */
113120 Pgno iTab, /* Root page number of the table to be locked */
113121 u8 isWriteLock, /* True for a write lock */
113122 const char *zName /* Name of the table to be locked */
113123){
113124 if( iDb==1 ) return;
113125 if( !sqlite3BtreeSharable(p: pParse->db->aDb[iDb].pBt) ) return;
113126 lockTable(pParse, iDb, iTab, isWriteLock, zName);
113127}
113128
113129/*
113130** Code an OP_TableLock instruction for each table locked by the
113131** statement (configured by calls to sqlite3TableLock()).
113132*/
113133static void codeTableLocks(Parse *pParse){
113134 int i;
113135 Vdbe *pVdbe = pParse->pVdbe;
113136 assert( pVdbe!=0 );
113137
113138 for(i=0; i<pParse->nTableLock; i++){
113139 TableLock *p = &pParse->aTableLock[i];
113140 int p1 = p->iDb;
113141 sqlite3VdbeAddOp4(p: pVdbe, OP_TableLock, p1, p2: p->iTab, p3: p->isWriteLock,
113142 zP4: p->zLockName, P4_STATIC);
113143 }
113144}
113145#else
113146 #define codeTableLocks(x)
113147#endif
113148
113149/*
113150** Return TRUE if the given yDbMask object is empty - if it contains no
113151** 1 bits. This routine is used by the DbMaskAllZero() and DbMaskNotZero()
113152** macros when SQLITE_MAX_ATTACHED is greater than 30.
113153*/
113154#if SQLITE_MAX_ATTACHED>30
113155SQLITE_PRIVATE int sqlite3DbMaskAllZero(yDbMask m){
113156 int i;
113157 for(i=0; i<sizeof(yDbMask); i++) if( m[i] ) return 0;
113158 return 1;
113159}
113160#endif
113161
113162/*
113163** This routine is called after a single SQL statement has been
113164** parsed and a VDBE program to execute that statement has been
113165** prepared. This routine puts the finishing touches on the
113166** VDBE program and resets the pParse structure for the next
113167** parse.
113168**
113169** Note that if an error occurred, it might be the case that
113170** no VDBE code was generated.
113171*/
113172SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){
113173 sqlite3 *db;
113174 Vdbe *v;
113175
113176 assert( pParse->pToplevel==0 );
113177 db = pParse->db;
113178 if( pParse->nested ) return;
113179 if( db->mallocFailed || pParse->nErr ){
113180 if( pParse->rc==SQLITE_OK ) pParse->rc = SQLITE_ERROR;
113181 return;
113182 }
113183
113184 /* Begin by generating some termination code at the end of the
113185 ** vdbe program
113186 */
113187 v = pParse->pVdbe;
113188 if( v==0 ){
113189 if( db->init.busy ){
113190 pParse->rc = SQLITE_DONE;
113191 return;
113192 }
113193 v = sqlite3GetVdbe(pParse);
113194 if( v==0 ) pParse->rc = SQLITE_ERROR;
113195 }
113196 assert( !pParse->isMultiWrite
113197 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
113198 if( v ){
113199 if( pParse->bReturning ){
113200 Returning *pReturning = pParse->u1.pReturning;
113201 int addrRewind;
113202 int i;
113203 int reg;
113204
113205 if( pReturning->nRetCol==0 ){
113206 assert( CORRUPT_DB );
113207 }else{
113208 addrRewind =
113209 sqlite3VdbeAddOp1(p: v, OP_Rewind, p1: pReturning->iRetCur);
113210 VdbeCoverage(v);
113211 reg = pReturning->iRetReg;
113212 for(i=0; i<pReturning->nRetCol; i++){
113213 sqlite3VdbeAddOp3(p: v, OP_Column, p1: pReturning->iRetCur, p2: i, p3: reg+i);
113214 }
113215 sqlite3VdbeAddOp2(p: v, OP_ResultRow, p1: reg, p2: i);
113216 sqlite3VdbeAddOp2(p: v, OP_Next, p1: pReturning->iRetCur, p2: addrRewind+1);
113217 VdbeCoverage(v);
113218 sqlite3VdbeJumpHere(p: v, addr: addrRewind);
113219 }
113220 }
113221 sqlite3VdbeAddOp0(p: v, OP_Halt);
113222
113223#if SQLITE_USER_AUTHENTICATION
113224 if( pParse->nTableLock>0 && db->init.busy==0 ){
113225 sqlite3UserAuthInit(db);
113226 if( db->auth.authLevel<UAUTH_User ){
113227 sqlite3ErrorMsg(pParse, "user not authenticated");
113228 pParse->rc = SQLITE_AUTH_USER;
113229 return;
113230 }
113231 }
113232#endif
113233
113234 /* The cookie mask contains one bit for each database file open.
113235 ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are
113236 ** set for each database that is used. Generate code to start a
113237 ** transaction on each used database and to verify the schema cookie
113238 ** on each used database.
113239 */
113240 if( db->mallocFailed==0
113241 && (DbMaskNonZero(pParse->cookieMask) || pParse->pConstExpr)
113242 ){
113243 int iDb, i;
113244 assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
113245 sqlite3VdbeJumpHere(p: v, addr: 0);
113246 for(iDb=0; iDb<db->nDb; iDb++){
113247 Schema *pSchema;
113248 if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue;
113249 sqlite3VdbeUsesBtree(p: v, i: iDb);
113250 pSchema = db->aDb[iDb].pSchema;
113251 sqlite3VdbeAddOp4Int(p: v,
113252 OP_Transaction, /* Opcode */
113253 p1: iDb, /* P1 */
113254 DbMaskTest(pParse->writeMask,iDb), /* P2 */
113255 p3: pSchema->schema_cookie, /* P3 */
113256 p4: pSchema->iGeneration /* P4 */
113257 );
113258 if( db->init.busy==0 ) sqlite3VdbeChangeP5(p: v, p5: 1);
113259 VdbeComment((v,
113260 "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
113261 }
113262#ifndef SQLITE_OMIT_VIRTUALTABLE
113263 for(i=0; i<pParse->nVtabLock; i++){
113264 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
113265 sqlite3VdbeAddOp4(p: v, OP_VBegin, p1: 0, p2: 0, p3: 0, zP4: vtab, P4_VTAB);
113266 }
113267 pParse->nVtabLock = 0;
113268#endif
113269
113270 /* Once all the cookies have been verified and transactions opened,
113271 ** obtain the required table-locks. This is a no-op unless the
113272 ** shared-cache feature is enabled.
113273 */
113274 codeTableLocks(pParse);
113275
113276 /* Initialize any AUTOINCREMENT data structures required.
113277 */
113278 sqlite3AutoincrementBegin(pParse);
113279
113280 /* Code constant expressions that where factored out of inner loops.
113281 **
113282 ** The pConstExpr list might also contain expressions that we simply
113283 ** want to keep around until the Parse object is deleted. Such
113284 ** expressions have iConstExprReg==0. Do not generate code for
113285 ** those expressions, of course.
113286 */
113287 if( pParse->pConstExpr ){
113288 ExprList *pEL = pParse->pConstExpr;
113289 pParse->okConstFactor = 0;
113290 for(i=0; i<pEL->nExpr; i++){
113291 int iReg = pEL->a[i].u.iConstExprReg;
113292 if( iReg>0 ){
113293 sqlite3ExprCode(pParse, pExpr: pEL->a[i].pExpr, target: iReg);
113294 }
113295 }
113296 }
113297
113298 if( pParse->bReturning ){
113299 Returning *pRet = pParse->u1.pReturning;
113300 if( pRet->nRetCol==0 ){
113301 assert( CORRUPT_DB );
113302 }else{
113303 sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: pRet->iRetCur, p2: pRet->nRetCol);
113304 }
113305 }
113306
113307 /* Finally, jump back to the beginning of the executable code. */
113308 sqlite3VdbeGoto(p: v, iDest: 1);
113309 }
113310 }
113311
113312 /* Get the VDBE program ready for execution
113313 */
113314 if( v && pParse->nErr==0 && !db->mallocFailed ){
113315 /* A minimum of one cursor is required if autoincrement is used
113316 * See ticket [a696379c1f08866] */
113317 assert( pParse->pAinc==0 || pParse->nTab>0 );
113318 sqlite3VdbeMakeReady(p: v, pParse);
113319 pParse->rc = SQLITE_DONE;
113320 }else{
113321 pParse->rc = SQLITE_ERROR;
113322 }
113323}
113324
113325/*
113326** Run the parser and code generator recursively in order to generate
113327** code for the SQL statement given onto the end of the pParse context
113328** currently under construction. Notes:
113329**
113330** * The final OP_Halt is not appended and other initialization
113331** and finalization steps are omitted because those are handling by the
113332** outermost parser.
113333**
113334** * Built-in SQL functions always take precedence over application-defined
113335** SQL functions. In other words, it is not possible to override a
113336** built-in function.
113337*/
113338SQLITE_PRIVATE void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
113339 va_list ap;
113340 char *zSql;
113341 char *zErrMsg = 0;
113342 sqlite3 *db = pParse->db;
113343 u32 savedDbFlags = db->mDbFlags;
113344 char saveBuf[PARSE_TAIL_SZ];
113345
113346 if( pParse->nErr ) return;
113347 assert( pParse->nested<10 ); /* Nesting should only be of limited depth */
113348 va_start(ap, zFormat);
113349 zSql = sqlite3VMPrintf(db, zFormat, ap);
113350 va_end(ap);
113351 if( zSql==0 ){
113352 /* This can result either from an OOM or because the formatted string
113353 ** exceeds SQLITE_LIMIT_LENGTH. In the latter case, we need to set
113354 ** an error */
113355 if( !db->mallocFailed ) pParse->rc = SQLITE_TOOBIG;
113356 pParse->nErr++;
113357 return;
113358 }
113359 pParse->nested++;
113360 memcpy(dest: saveBuf, PARSE_TAIL(pParse), PARSE_TAIL_SZ);
113361 memset(PARSE_TAIL(pParse), c: 0, PARSE_TAIL_SZ);
113362 db->mDbFlags |= DBFLAG_PreferBuiltin;
113363 sqlite3RunParser(pParse, zSql, &zErrMsg);
113364 db->mDbFlags = savedDbFlags;
113365 sqlite3DbFree(db, p: zErrMsg);
113366 sqlite3DbFree(db, p: zSql);
113367 memcpy(PARSE_TAIL(pParse), src: saveBuf, PARSE_TAIL_SZ);
113368 pParse->nested--;
113369}
113370
113371#if SQLITE_USER_AUTHENTICATION
113372/*
113373** Return TRUE if zTable is the name of the system table that stores the
113374** list of users and their access credentials.
113375*/
113376SQLITE_PRIVATE int sqlite3UserAuthTable(const char *zTable){
113377 return sqlite3_stricmp(zTable, "sqlite_user")==0;
113378}
113379#endif
113380
113381/*
113382** Locate the in-memory structure that describes a particular database
113383** table given the name of that table and (optionally) the name of the
113384** database containing the table. Return NULL if not found.
113385**
113386** If zDatabase is 0, all databases are searched for the table and the
113387** first matching table is returned. (No checking for duplicate table
113388** names is done.) The search order is TEMP first, then MAIN, then any
113389** auxiliary databases added using the ATTACH command.
113390**
113391** See also sqlite3LocateTable().
113392*/
113393SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
113394 Table *p = 0;
113395 int i;
113396
113397 /* All mutexes are required for schema access. Make sure we hold them. */
113398 assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
113399#if SQLITE_USER_AUTHENTICATION
113400 /* Only the admin user is allowed to know that the sqlite_user table
113401 ** exists */
113402 if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){
113403 return 0;
113404 }
113405#endif
113406 if( zDatabase ){
113407 for(i=0; i<db->nDb; i++){
113408 if( sqlite3StrICmp(zLeft: zDatabase, zRight: db->aDb[i].zDbSName)==0 ) break;
113409 }
113410 if( i>=db->nDb ){
113411 /* No match against the official names. But always match "main"
113412 ** to schema 0 as a legacy fallback. */
113413 if( sqlite3StrICmp(zLeft: zDatabase,zRight: "main")==0 ){
113414 i = 0;
113415 }else{
113416 return 0;
113417 }
113418 }
113419 p = sqlite3HashFind(pH: &db->aDb[i].pSchema->tblHash, pKey: zName);
113420 if( p==0 && sqlite3StrNICmp(zLeft: zName, zRight: "sqlite_", N: 7)==0 ){
113421 if( i==1 ){
113422 if( sqlite3StrICmp(zLeft: zName+7, zRight: &PREFERRED_TEMP_SCHEMA_TABLE[7])==0
113423 || sqlite3StrICmp(zLeft: zName+7, zRight: &PREFERRED_SCHEMA_TABLE[7])==0
113424 || sqlite3StrICmp(zLeft: zName+7, zRight: &LEGACY_SCHEMA_TABLE[7])==0
113425 ){
113426 p = sqlite3HashFind(pH: &db->aDb[1].pSchema->tblHash,
113427 LEGACY_TEMP_SCHEMA_TABLE);
113428 }
113429 }else{
113430 if( sqlite3StrICmp(zLeft: zName+7, zRight: &PREFERRED_SCHEMA_TABLE[7])==0 ){
113431 p = sqlite3HashFind(pH: &db->aDb[i].pSchema->tblHash,
113432 LEGACY_SCHEMA_TABLE);
113433 }
113434 }
113435 }
113436 }else{
113437 /* Match against TEMP first */
113438 p = sqlite3HashFind(pH: &db->aDb[1].pSchema->tblHash, pKey: zName);
113439 if( p ) return p;
113440 /* The main database is second */
113441 p = sqlite3HashFind(pH: &db->aDb[0].pSchema->tblHash, pKey: zName);
113442 if( p ) return p;
113443 /* Attached databases are in order of attachment */
113444 for(i=2; i<db->nDb; i++){
113445 assert( sqlite3SchemaMutexHeld(db, i, 0) );
113446 p = sqlite3HashFind(pH: &db->aDb[i].pSchema->tblHash, pKey: zName);
113447 if( p ) break;
113448 }
113449 if( p==0 && sqlite3StrNICmp(zLeft: zName, zRight: "sqlite_", N: 7)==0 ){
113450 if( sqlite3StrICmp(zLeft: zName+7, zRight: &PREFERRED_SCHEMA_TABLE[7])==0 ){
113451 p = sqlite3HashFind(pH: &db->aDb[0].pSchema->tblHash, LEGACY_SCHEMA_TABLE);
113452 }else if( sqlite3StrICmp(zLeft: zName+7, zRight: &PREFERRED_TEMP_SCHEMA_TABLE[7])==0 ){
113453 p = sqlite3HashFind(pH: &db->aDb[1].pSchema->tblHash,
113454 LEGACY_TEMP_SCHEMA_TABLE);
113455 }
113456 }
113457 }
113458 return p;
113459}
113460
113461/*
113462** Locate the in-memory structure that describes a particular database
113463** table given the name of that table and (optionally) the name of the
113464** database containing the table. Return NULL if not found. Also leave an
113465** error message in pParse->zErrMsg.
113466**
113467** The difference between this routine and sqlite3FindTable() is that this
113468** routine leaves an error message in pParse->zErrMsg where
113469** sqlite3FindTable() does not.
113470*/
113471SQLITE_PRIVATE Table *sqlite3LocateTable(
113472 Parse *pParse, /* context in which to report errors */
113473 u32 flags, /* LOCATE_VIEW or LOCATE_NOERR */
113474 const char *zName, /* Name of the table we are looking for */
113475 const char *zDbase /* Name of the database. Might be NULL */
113476){
113477 Table *p;
113478 sqlite3 *db = pParse->db;
113479
113480 /* Read the database schema. If an error occurs, leave an error message
113481 ** and code in pParse and return NULL. */
113482 if( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0
113483 && SQLITE_OK!=sqlite3ReadSchema(pParse)
113484 ){
113485 return 0;
113486 }
113487
113488 p = sqlite3FindTable(db, zName, zDatabase: zDbase);
113489 if( p==0 ){
113490#ifndef SQLITE_OMIT_VIRTUALTABLE
113491 /* If zName is the not the name of a table in the schema created using
113492 ** CREATE, then check to see if it is the name of an virtual table that
113493 ** can be an eponymous virtual table. */
113494 if( pParse->disableVtab==0 && db->init.busy==0 ){
113495 Module *pMod = (Module*)sqlite3HashFind(pH: &db->aModule, pKey: zName);
113496 if( pMod==0 && sqlite3_strnicmp(zLeft: zName, zRight: "pragma_", N: 7)==0 ){
113497 pMod = sqlite3PragmaVtabRegister(db, zName);
113498 }
113499 if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){
113500 testcase( pMod->pEpoTab==0 );
113501 return pMod->pEpoTab;
113502 }
113503 }
113504#endif
113505 if( flags & LOCATE_NOERR ) return 0;
113506 pParse->checkSchema = 1;
113507 }else if( IsVirtual(p) && pParse->disableVtab ){
113508 p = 0;
113509 }
113510
113511 if( p==0 ){
113512 const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table";
113513 if( zDbase ){
113514 sqlite3ErrorMsg(pParse, zFormat: "%s: %s.%s", zMsg, zDbase, zName);
113515 }else{
113516 sqlite3ErrorMsg(pParse, zFormat: "%s: %s", zMsg, zName);
113517 }
113518 }else{
113519 assert( HasRowid(p) || p->iPKey<0 );
113520 }
113521
113522 return p;
113523}
113524
113525/*
113526** Locate the table identified by *p.
113527**
113528** This is a wrapper around sqlite3LocateTable(). The difference between
113529** sqlite3LocateTable() and this function is that this function restricts
113530** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
113531** non-NULL if it is part of a view or trigger program definition. See
113532** sqlite3FixSrcList() for details.
113533*/
113534SQLITE_PRIVATE Table *sqlite3LocateTableItem(
113535 Parse *pParse,
113536 u32 flags,
113537 SrcItem *p
113538){
113539 const char *zDb;
113540 assert( p->pSchema==0 || p->zDatabase==0 );
113541 if( p->pSchema ){
113542 int iDb = sqlite3SchemaToIndex(db: pParse->db, p->pSchema);
113543 zDb = pParse->db->aDb[iDb].zDbSName;
113544 }else{
113545 zDb = p->zDatabase;
113546 }
113547 return sqlite3LocateTable(pParse, flags, zName: p->zName, zDbase: zDb);
113548}
113549
113550/*
113551** Return the preferred table name for system tables. Translate legacy
113552** names into the new preferred names, as appropriate.
113553*/
113554SQLITE_PRIVATE const char *sqlite3PreferredTableName(const char *zName){
113555 if( sqlite3StrNICmp(zLeft: zName, zRight: "sqlite_", N: 7)==0 ){
113556 if( sqlite3StrICmp(zLeft: zName+7, zRight: &LEGACY_SCHEMA_TABLE[7])==0 ){
113557 return PREFERRED_SCHEMA_TABLE;
113558 }
113559 if( sqlite3StrICmp(zLeft: zName+7, zRight: &LEGACY_TEMP_SCHEMA_TABLE[7])==0 ){
113560 return PREFERRED_TEMP_SCHEMA_TABLE;
113561 }
113562 }
113563 return zName;
113564}
113565
113566/*
113567** Locate the in-memory structure that describes
113568** a particular index given the name of that index
113569** and the name of the database that contains the index.
113570** Return NULL if not found.
113571**
113572** If zDatabase is 0, all databases are searched for the
113573** table and the first matching index is returned. (No checking
113574** for duplicate index names is done.) The search order is
113575** TEMP first, then MAIN, then any auxiliary databases added
113576** using the ATTACH command.
113577*/
113578SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
113579 Index *p = 0;
113580 int i;
113581 /* All mutexes are required for schema access. Make sure we hold them. */
113582 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
113583 for(i=OMIT_TEMPDB; i<db->nDb; i++){
113584 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
113585 Schema *pSchema = db->aDb[j].pSchema;
113586 assert( pSchema );
113587 if( zDb && sqlite3DbIsNamed(db, iDb: j, zName: zDb)==0 ) continue;
113588 assert( sqlite3SchemaMutexHeld(db, j, 0) );
113589 p = sqlite3HashFind(pH: &pSchema->idxHash, pKey: zName);
113590 if( p ) break;
113591 }
113592 return p;
113593}
113594
113595/*
113596** Reclaim the memory used by an index
113597*/
113598SQLITE_PRIVATE void sqlite3FreeIndex(sqlite3 *db, Index *p){
113599#ifndef SQLITE_OMIT_ANALYZE
113600 sqlite3DeleteIndexSamples(db, pIdx: p);
113601#endif
113602 sqlite3ExprDelete(db, p: p->pPartIdxWhere);
113603 sqlite3ExprListDelete(db, pList: p->aColExpr);
113604 sqlite3DbFree(db, p: p->zColAff);
113605 if( p->isResized ) sqlite3DbFree(db, p: (void *)p->azColl);
113606#ifdef SQLITE_ENABLE_STAT4
113607 sqlite3_free(p->aiRowEst);
113608#endif
113609 sqlite3DbFree(db, p);
113610}
113611
113612/*
113613** For the index called zIdxName which is found in the database iDb,
113614** unlike that index from its Table then remove the index from
113615** the index hash table and free all memory structures associated
113616** with the index.
113617*/
113618SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
113619 Index *pIndex;
113620 Hash *pHash;
113621
113622 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
113623 pHash = &db->aDb[iDb].pSchema->idxHash;
113624 pIndex = sqlite3HashInsert(pH: pHash, pKey: zIdxName, data: 0);
113625 if( ALWAYS(pIndex) ){
113626 if( pIndex->pTable->pIndex==pIndex ){
113627 pIndex->pTable->pIndex = pIndex->pNext;
113628 }else{
113629 Index *p;
113630 /* Justification of ALWAYS(); The index must be on the list of
113631 ** indices. */
113632 p = pIndex->pTable->pIndex;
113633 while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
113634 if( ALWAYS(p && p->pNext==pIndex) ){
113635 p->pNext = pIndex->pNext;
113636 }
113637 }
113638 sqlite3FreeIndex(db, p: pIndex);
113639 }
113640 db->mDbFlags |= DBFLAG_SchemaChange;
113641}
113642
113643/*
113644** Look through the list of open database files in db->aDb[] and if
113645** any have been closed, remove them from the list. Reallocate the
113646** db->aDb[] structure to a smaller size, if possible.
113647**
113648** Entry 0 (the "main" database) and entry 1 (the "temp" database)
113649** are never candidates for being collapsed.
113650*/
113651SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3 *db){
113652 int i, j;
113653 for(i=j=2; i<db->nDb; i++){
113654 struct Db *pDb = &db->aDb[i];
113655 if( pDb->pBt==0 ){
113656 sqlite3DbFree(db, p: pDb->zDbSName);
113657 pDb->zDbSName = 0;
113658 continue;
113659 }
113660 if( j<i ){
113661 db->aDb[j] = db->aDb[i];
113662 }
113663 j++;
113664 }
113665 db->nDb = j;
113666 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
113667 memcpy(dest: db->aDbStatic, src: db->aDb, n: 2*sizeof(db->aDb[0]));
113668 sqlite3DbFree(db, p: db->aDb);
113669 db->aDb = db->aDbStatic;
113670 }
113671}
113672
113673/*
113674** Reset the schema for the database at index iDb. Also reset the
113675** TEMP schema. The reset is deferred if db->nSchemaLock is not zero.
113676** Deferred resets may be run by calling with iDb<0.
113677*/
113678SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3 *db, int iDb){
113679 int i;
113680 assert( iDb<db->nDb );
113681
113682 if( iDb>=0 ){
113683 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
113684 DbSetProperty(db, iDb, DB_ResetWanted);
113685 DbSetProperty(db, 1, DB_ResetWanted);
113686 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
113687 }
113688
113689 if( db->nSchemaLock==0 ){
113690 for(i=0; i<db->nDb; i++){
113691 if( DbHasProperty(db, i, DB_ResetWanted) ){
113692 sqlite3SchemaClear(db->aDb[i].pSchema);
113693 }
113694 }
113695 }
113696}
113697
113698/*
113699** Erase all schema information from all attached databases (including
113700** "main" and "temp") for a single database connection.
113701*/
113702SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){
113703 int i;
113704 sqlite3BtreeEnterAll(db);
113705 for(i=0; i<db->nDb; i++){
113706 Db *pDb = &db->aDb[i];
113707 if( pDb->pSchema ){
113708 if( db->nSchemaLock==0 ){
113709 sqlite3SchemaClear(pDb->pSchema);
113710 }else{
113711 DbSetProperty(db, i, DB_ResetWanted);
113712 }
113713 }
113714 }
113715 db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk);
113716 sqlite3VtabUnlockList(db);
113717 sqlite3BtreeLeaveAll(db);
113718 if( db->nSchemaLock==0 ){
113719 sqlite3CollapseDatabaseArray(db);
113720 }
113721}
113722
113723/*
113724** This routine is called when a commit occurs.
113725*/
113726SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3 *db){
113727 db->mDbFlags &= ~DBFLAG_SchemaChange;
113728}
113729
113730/*
113731** Set the expression associated with a column. This is usually
113732** the DEFAULT value, but might also be the expression that computes
113733** the value for a generated column.
113734*/
113735SQLITE_PRIVATE void sqlite3ColumnSetExpr(
113736 Parse *pParse, /* Parsing context */
113737 Table *pTab, /* The table containing the column */
113738 Column *pCol, /* The column to receive the new DEFAULT expression */
113739 Expr *pExpr /* The new default expression */
113740){
113741 ExprList *pList;
113742 assert( IsOrdinaryTable(pTab) );
113743 pList = pTab->u.tab.pDfltList;
113744 if( pCol->iDflt==0
113745 || NEVER(pList==0)
113746 || NEVER(pList->nExpr<pCol->iDflt)
113747 ){
113748 pCol->iDflt = pList==0 ? 1 : pList->nExpr+1;
113749 pTab->u.tab.pDfltList = sqlite3ExprListAppend(pParse, pList, pExpr);
113750 }else{
113751 sqlite3ExprDelete(db: pParse->db, p: pList->a[pCol->iDflt-1].pExpr);
113752 pList->a[pCol->iDflt-1].pExpr = pExpr;
113753 }
113754}
113755
113756/*
113757** Return the expression associated with a column. The expression might be
113758** the DEFAULT clause or the AS clause of a generated column.
113759** Return NULL if the column has no associated expression.
113760*/
113761SQLITE_PRIVATE Expr *sqlite3ColumnExpr(Table *pTab, Column *pCol){
113762 if( pCol->iDflt==0 ) return 0;
113763 if( NEVER(!IsOrdinaryTable(pTab)) ) return 0;
113764 if( NEVER(pTab->u.tab.pDfltList==0) ) return 0;
113765 if( NEVER(pTab->u.tab.pDfltList->nExpr<pCol->iDflt) ) return 0;
113766 return pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr;
113767}
113768
113769/*
113770** Set the collating sequence name for a column.
113771*/
113772SQLITE_PRIVATE void sqlite3ColumnSetColl(
113773 sqlite3 *db,
113774 Column *pCol,
113775 const char *zColl
113776){
113777 i64 nColl;
113778 i64 n;
113779 char *zNew;
113780 assert( zColl!=0 );
113781 n = sqlite3Strlen30(z: pCol->zCnName) + 1;
113782 if( pCol->colFlags & COLFLAG_HASTYPE ){
113783 n += sqlite3Strlen30(z: pCol->zCnName+n) + 1;
113784 }
113785 nColl = sqlite3Strlen30(z: zColl) + 1;
113786 zNew = sqlite3DbRealloc(db, p: pCol->zCnName, n: nColl+n);
113787 if( zNew ){
113788 pCol->zCnName = zNew;
113789 memcpy(dest: pCol->zCnName + n, src: zColl, n: nColl);
113790 pCol->colFlags |= COLFLAG_HASCOLL;
113791 }
113792}
113793
113794/*
113795** Return the collating squence name for a column
113796*/
113797SQLITE_PRIVATE const char *sqlite3ColumnColl(Column *pCol){
113798 const char *z;
113799 if( (pCol->colFlags & COLFLAG_HASCOLL)==0 ) return 0;
113800 z = pCol->zCnName;
113801 while( *z ){ z++; }
113802 if( pCol->colFlags & COLFLAG_HASTYPE ){
113803 do{ z++; }while( *z );
113804 }
113805 return z+1;
113806}
113807
113808/*
113809** Delete memory allocated for the column names of a table or view (the
113810** Table.aCol[] array).
113811*/
113812SQLITE_PRIVATE void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){
113813 int i;
113814 Column *pCol;
113815 assert( pTable!=0 );
113816 if( (pCol = pTable->aCol)!=0 ){
113817 for(i=0; i<pTable->nCol; i++, pCol++){
113818 assert( pCol->zCnName==0 || pCol->hName==sqlite3StrIHash(pCol->zCnName) );
113819 sqlite3DbFree(db, p: pCol->zCnName);
113820 }
113821 sqlite3DbFree(db, p: pTable->aCol);
113822 if( IsOrdinaryTable(pTable) ){
113823 sqlite3ExprListDelete(db, pList: pTable->u.tab.pDfltList);
113824 }
113825 if( db==0 || db->pnBytesFreed==0 ){
113826 pTable->aCol = 0;
113827 pTable->nCol = 0;
113828 if( IsOrdinaryTable(pTable) ){
113829 pTable->u.tab.pDfltList = 0;
113830 }
113831 }
113832 }
113833}
113834
113835/*
113836** Remove the memory data structures associated with the given
113837** Table. No changes are made to disk by this routine.
113838**
113839** This routine just deletes the data structure. It does not unlink
113840** the table data structure from the hash table. But it does destroy
113841** memory structures of the indices and foreign keys associated with
113842** the table.
113843**
113844** The db parameter is optional. It is needed if the Table object
113845** contains lookaside memory. (Table objects in the schema do not use
113846** lookaside memory, but some ephemeral Table objects do.) Or the
113847** db parameter can be used with db->pnBytesFreed to measure the memory
113848** used by the Table object.
113849*/
113850static void SQLITE_NOINLINE deleteTable(sqlite3 *db, Table *pTable){
113851 Index *pIndex, *pNext;
113852
113853#ifdef SQLITE_DEBUG
113854 /* Record the number of outstanding lookaside allocations in schema Tables
113855 ** prior to doing any free() operations. Since schema Tables do not use
113856 ** lookaside, this number should not change.
113857 **
113858 ** If malloc has already failed, it may be that it failed while allocating
113859 ** a Table object that was going to be marked ephemeral. So do not check
113860 ** that no lookaside memory is used in this case either. */
113861 int nLookaside = 0;
113862 if( db && !db->mallocFailed && (pTable->tabFlags & TF_Ephemeral)==0 ){
113863 nLookaside = sqlite3LookasideUsed(db, 0);
113864 }
113865#endif
113866
113867 /* Delete all indices associated with this table. */
113868 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
113869 pNext = pIndex->pNext;
113870 assert( pIndex->pSchema==pTable->pSchema
113871 || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) );
113872 if( (db==0 || db->pnBytesFreed==0) && !IsVirtual(pTable) ){
113873 char *zName = pIndex->zName;
113874 TESTONLY ( Index *pOld = ) sqlite3HashInsert(
113875 pH: &pIndex->pSchema->idxHash, pKey: zName, data: 0
113876 );
113877 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
113878 assert( pOld==pIndex || pOld==0 );
113879 }
113880 sqlite3FreeIndex(db, p: pIndex);
113881 }
113882
113883 if( IsOrdinaryTable(pTable) ){
113884 sqlite3FkDelete(db, pTable);
113885 }
113886#ifndef SQLITE_OMIT_VIRTUAL_TABLE
113887 else if( IsVirtual(pTable) ){
113888 sqlite3VtabClear(db, pTable);
113889 }
113890#endif
113891 else{
113892 assert( IsView(pTable) );
113893 sqlite3SelectDelete(db, pTable->u.view.pSelect);
113894 }
113895
113896 /* Delete the Table structure itself.
113897 */
113898 sqlite3DeleteColumnNames(db, pTable);
113899 sqlite3DbFree(db, p: pTable->zName);
113900 sqlite3DbFree(db, p: pTable->zColAff);
113901 sqlite3ExprListDelete(db, pList: pTable->pCheck);
113902 sqlite3DbFree(db, p: pTable);
113903
113904 /* Verify that no lookaside memory was used by schema tables */
113905 assert( nLookaside==0 || nLookaside==sqlite3LookasideUsed(db,0) );
113906}
113907SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
113908 /* Do not delete the table until the reference count reaches zero. */
113909 if( !pTable ) return;
113910 if( ((!db || db->pnBytesFreed==0) && (--pTable->nTabRef)>0) ) return;
113911 deleteTable(db, pTable);
113912}
113913
113914
113915/*
113916** Unlink the given table from the hash tables and the delete the
113917** table structure with all its indices and foreign keys.
113918*/
113919SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
113920 Table *p;
113921 Db *pDb;
113922
113923 assert( db!=0 );
113924 assert( iDb>=0 && iDb<db->nDb );
113925 assert( zTabName );
113926 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
113927 testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */
113928 pDb = &db->aDb[iDb];
113929 p = sqlite3HashInsert(pH: &pDb->pSchema->tblHash, pKey: zTabName, data: 0);
113930 sqlite3DeleteTable(db, pTable: p);
113931 db->mDbFlags |= DBFLAG_SchemaChange;
113932}
113933
113934/*
113935** Given a token, return a string that consists of the text of that
113936** token. Space to hold the returned string
113937** is obtained from sqliteMalloc() and must be freed by the calling
113938** function.
113939**
113940** Any quotation marks (ex: "name", 'name', [name], or `name`) that
113941** surround the body of the token are removed.
113942**
113943** Tokens are often just pointers into the original SQL text and so
113944** are not \000 terminated and are not persistent. The returned string
113945** is \000 terminated and is persistent.
113946*/
113947SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3 *db, const Token *pName){
113948 char *zName;
113949 if( pName ){
113950 zName = sqlite3DbStrNDup(db, z: (const char*)pName->z, n: pName->n);
113951 sqlite3Dequote(z: zName);
113952 }else{
113953 zName = 0;
113954 }
113955 return zName;
113956}
113957
113958/*
113959** Open the sqlite_schema table stored in database number iDb for
113960** writing. The table is opened using cursor 0.
113961*/
113962SQLITE_PRIVATE void sqlite3OpenSchemaTable(Parse *p, int iDb){
113963 Vdbe *v = sqlite3GetVdbe(p);
113964 sqlite3TableLock(pParse: p, iDb, SCHEMA_ROOT, isWriteLock: 1, LEGACY_SCHEMA_TABLE);
113965 sqlite3VdbeAddOp4Int(p: v, OP_OpenWrite, p1: 0, SCHEMA_ROOT, p3: iDb, p4: 5);
113966 if( p->nTab==0 ){
113967 p->nTab = 1;
113968 }
113969}
113970
113971/*
113972** Parameter zName points to a nul-terminated buffer containing the name
113973** of a database ("main", "temp" or the name of an attached db). This
113974** function returns the index of the named database in db->aDb[], or
113975** -1 if the named db cannot be found.
113976*/
113977SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *db, const char *zName){
113978 int i = -1; /* Database number */
113979 if( zName ){
113980 Db *pDb;
113981 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
113982 if( 0==sqlite3_stricmp(zLeft: pDb->zDbSName, zRight: zName) ) break;
113983 /* "main" is always an acceptable alias for the primary database
113984 ** even if it has been renamed using SQLITE_DBCONFIG_MAINDBNAME. */
113985 if( i==0 && 0==sqlite3_stricmp(zLeft: "main", zRight: zName) ) break;
113986 }
113987 }
113988 return i;
113989}
113990
113991/*
113992** The token *pName contains the name of a database (either "main" or
113993** "temp" or the name of an attached db). This routine returns the
113994** index of the named database in db->aDb[], or -1 if the named db
113995** does not exist.
113996*/
113997SQLITE_PRIVATE int sqlite3FindDb(sqlite3 *db, Token *pName){
113998 int i; /* Database number */
113999 char *zName; /* Name we are searching for */
114000 zName = sqlite3NameFromToken(db, pName);
114001 i = sqlite3FindDbName(db, zName);
114002 sqlite3DbFree(db, p: zName);
114003 return i;
114004}
114005
114006/* The table or view or trigger name is passed to this routine via tokens
114007** pName1 and pName2. If the table name was fully qualified, for example:
114008**
114009** CREATE TABLE xxx.yyy (...);
114010**
114011** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
114012** the table name is not fully qualified, i.e.:
114013**
114014** CREATE TABLE yyy(...);
114015**
114016** Then pName1 is set to "yyy" and pName2 is "".
114017**
114018** This routine sets the *ppUnqual pointer to point at the token (pName1 or
114019** pName2) that stores the unqualified table name. The index of the
114020** database "xxx" is returned.
114021*/
114022SQLITE_PRIVATE int sqlite3TwoPartName(
114023 Parse *pParse, /* Parsing and code generating context */
114024 Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */
114025 Token *pName2, /* The "yyy" in the name "xxx.yyy" */
114026 Token **pUnqual /* Write the unqualified object name here */
114027){
114028 int iDb; /* Database holding the object */
114029 sqlite3 *db = pParse->db;
114030
114031 assert( pName2!=0 );
114032 if( pName2->n>0 ){
114033 if( db->init.busy ) {
114034 sqlite3ErrorMsg(pParse, zFormat: "corrupt database");
114035 return -1;
114036 }
114037 *pUnqual = pName2;
114038 iDb = sqlite3FindDb(db, pName: pName1);
114039 if( iDb<0 ){
114040 sqlite3ErrorMsg(pParse, zFormat: "unknown database %T", pName1);
114041 return -1;
114042 }
114043 }else{
114044 assert( db->init.iDb==0 || db->init.busy || IN_SPECIAL_PARSE
114045 || (db->mDbFlags & DBFLAG_Vacuum)!=0);
114046 iDb = db->init.iDb;
114047 *pUnqual = pName1;
114048 }
114049 return iDb;
114050}
114051
114052/*
114053** True if PRAGMA writable_schema is ON
114054*/
114055SQLITE_PRIVATE int sqlite3WritableSchema(sqlite3 *db){
114056 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 );
114057 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
114058 SQLITE_WriteSchema );
114059 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
114060 SQLITE_Defensive );
114061 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
114062 (SQLITE_WriteSchema|SQLITE_Defensive) );
114063 return (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==SQLITE_WriteSchema;
114064}
114065
114066/*
114067** This routine is used to check if the UTF-8 string zName is a legal
114068** unqualified name for a new schema object (table, index, view or
114069** trigger). All names are legal except those that begin with the string
114070** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
114071** is reserved for internal use.
114072**
114073** When parsing the sqlite_schema table, this routine also checks to
114074** make sure the "type", "name", and "tbl_name" columns are consistent
114075** with the SQL.
114076*/
114077SQLITE_PRIVATE int sqlite3CheckObjectName(
114078 Parse *pParse, /* Parsing context */
114079 const char *zName, /* Name of the object to check */
114080 const char *zType, /* Type of this object */
114081 const char *zTblName /* Parent table name for triggers and indexes */
114082){
114083 sqlite3 *db = pParse->db;
114084 if( sqlite3WritableSchema(db)
114085 || db->init.imposterTable
114086 || !sqlite3Config.bExtraSchemaChecks
114087 ){
114088 /* Skip these error checks for writable_schema=ON */
114089 return SQLITE_OK;
114090 }
114091 if( db->init.busy ){
114092 if( sqlite3_stricmp(zLeft: zType, zRight: db->init.azInit[0])
114093 || sqlite3_stricmp(zLeft: zName, zRight: db->init.azInit[1])
114094 || sqlite3_stricmp(zLeft: zTblName, zRight: db->init.azInit[2])
114095 ){
114096 sqlite3ErrorMsg(pParse, zFormat: ""); /* corruptSchema() will supply the error */
114097 return SQLITE_ERROR;
114098 }
114099 }else{
114100 if( (pParse->nested==0 && 0==sqlite3StrNICmp(zLeft: zName, zRight: "sqlite_", N: 7))
114101 || (sqlite3ReadOnlyShadowTables(db) && sqlite3ShadowTableName(db, zName))
114102 ){
114103 sqlite3ErrorMsg(pParse, zFormat: "object name reserved for internal use: %s",
114104 zName);
114105 return SQLITE_ERROR;
114106 }
114107
114108 }
114109 return SQLITE_OK;
114110}
114111
114112/*
114113** Return the PRIMARY KEY index of a table
114114*/
114115SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table *pTab){
114116 Index *p;
114117 for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){}
114118 return p;
114119}
114120
114121/*
114122** Convert an table column number into a index column number. That is,
114123** for the column iCol in the table (as defined by the CREATE TABLE statement)
114124** find the (first) offset of that column in index pIdx. Or return -1
114125** if column iCol is not used in index pIdx.
114126*/
114127SQLITE_PRIVATE i16 sqlite3TableColumnToIndex(Index *pIdx, i16 iCol){
114128 int i;
114129 for(i=0; i<pIdx->nColumn; i++){
114130 if( iCol==pIdx->aiColumn[i] ) return i;
114131 }
114132 return -1;
114133}
114134
114135#ifndef SQLITE_OMIT_GENERATED_COLUMNS
114136/* Convert a storage column number into a table column number.
114137**
114138** The storage column number (0,1,2,....) is the index of the value
114139** as it appears in the record on disk. The true column number
114140** is the index (0,1,2,...) of the column in the CREATE TABLE statement.
114141**
114142** The storage column number is less than the table column number if
114143** and only there are VIRTUAL columns to the left.
114144**
114145** If SQLITE_OMIT_GENERATED_COLUMNS, this routine is a no-op macro.
114146*/
114147SQLITE_PRIVATE i16 sqlite3StorageColumnToTable(Table *pTab, i16 iCol){
114148 if( pTab->tabFlags & TF_HasVirtual ){
114149 int i;
114150 for(i=0; i<=iCol; i++){
114151 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) iCol++;
114152 }
114153 }
114154 return iCol;
114155}
114156#endif
114157
114158#ifndef SQLITE_OMIT_GENERATED_COLUMNS
114159/* Convert a table column number into a storage column number.
114160**
114161** The storage column number (0,1,2,....) is the index of the value
114162** as it appears in the record on disk. Or, if the input column is
114163** the N-th virtual column (zero-based) then the storage number is
114164** the number of non-virtual columns in the table plus N.
114165**
114166** The true column number is the index (0,1,2,...) of the column in
114167** the CREATE TABLE statement.
114168**
114169** If the input column is a VIRTUAL column, then it should not appear
114170** in storage. But the value sometimes is cached in registers that
114171** follow the range of registers used to construct storage. This
114172** avoids computing the same VIRTUAL column multiple times, and provides
114173** values for use by OP_Param opcodes in triggers. Hence, if the
114174** input column is a VIRTUAL table, put it after all the other columns.
114175**
114176** In the following, N means "normal column", S means STORED, and
114177** V means VIRTUAL. Suppose the CREATE TABLE has columns like this:
114178**
114179** CREATE TABLE ex(N,S,V,N,S,V,N,S,V);
114180** -- 0 1 2 3 4 5 6 7 8
114181**
114182** Then the mapping from this function is as follows:
114183**
114184** INPUTS: 0 1 2 3 4 5 6 7 8
114185** OUTPUTS: 0 1 6 2 3 7 4 5 8
114186**
114187** So, in other words, this routine shifts all the virtual columns to
114188** the end.
114189**
114190** If SQLITE_OMIT_GENERATED_COLUMNS then there are no virtual columns and
114191** this routine is a no-op macro. If the pTab does not have any virtual
114192** columns, then this routine is no-op that always return iCol. If iCol
114193** is negative (indicating the ROWID column) then this routine return iCol.
114194*/
114195SQLITE_PRIVATE i16 sqlite3TableColumnToStorage(Table *pTab, i16 iCol){
114196 int i;
114197 i16 n;
114198 assert( iCol<pTab->nCol );
114199 if( (pTab->tabFlags & TF_HasVirtual)==0 || iCol<0 ) return iCol;
114200 for(i=0, n=0; i<iCol; i++){
114201 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) n++;
114202 }
114203 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ){
114204 /* iCol is a virtual column itself */
114205 return pTab->nNVCol + i - n;
114206 }else{
114207 /* iCol is a normal or stored column */
114208 return n;
114209 }
114210}
114211#endif
114212
114213/*
114214** Insert a single OP_JournalMode query opcode in order to force the
114215** prepared statement to return false for sqlite3_stmt_readonly(). This
114216** is used by CREATE TABLE IF NOT EXISTS and similar if the table already
114217** exists, so that the prepared statement for CREATE TABLE IF NOT EXISTS
114218** will return false for sqlite3_stmt_readonly() even if that statement
114219** is a read-only no-op.
114220*/
114221static void sqlite3ForceNotReadOnly(Parse *pParse){
114222 int iReg = ++pParse->nMem;
114223 Vdbe *v = sqlite3GetVdbe(pParse);
114224 if( v ){
114225 sqlite3VdbeAddOp3(p: v, OP_JournalMode, p1: 0, p2: iReg, PAGER_JOURNALMODE_QUERY);
114226 sqlite3VdbeUsesBtree(p: v, i: 0);
114227 }
114228}
114229
114230/*
114231** Begin constructing a new table representation in memory. This is
114232** the first of several action routines that get called in response
114233** to a CREATE TABLE statement. In particular, this routine is called
114234** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
114235** flag is true if the table should be stored in the auxiliary database
114236** file instead of in the main database file. This is normally the case
114237** when the "TEMP" or "TEMPORARY" keyword occurs in between
114238** CREATE and TABLE.
114239**
114240** The new table record is initialized and put in pParse->pNewTable.
114241** As more of the CREATE TABLE statement is parsed, additional action
114242** routines will be called to add more information to this record.
114243** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
114244** is called to complete the construction of the new table record.
114245*/
114246SQLITE_PRIVATE void sqlite3StartTable(
114247 Parse *pParse, /* Parser context */
114248 Token *pName1, /* First part of the name of the table or view */
114249 Token *pName2, /* Second part of the name of the table or view */
114250 int isTemp, /* True if this is a TEMP table */
114251 int isView, /* True if this is a VIEW */
114252 int isVirtual, /* True if this is a VIRTUAL table */
114253 int noErr /* Do nothing if table already exists */
114254){
114255 Table *pTable;
114256 char *zName = 0; /* The name of the new table */
114257 sqlite3 *db = pParse->db;
114258 Vdbe *v;
114259 int iDb; /* Database number to create the table in */
114260 Token *pName; /* Unqualified name of the table to create */
114261
114262 if( db->init.busy && db->init.newTnum==1 ){
114263 /* Special case: Parsing the sqlite_schema or sqlite_temp_schema schema */
114264 iDb = db->init.iDb;
114265 zName = sqlite3DbStrDup(db, SCHEMA_TABLE(iDb));
114266 pName = pName1;
114267 }else{
114268 /* The common case */
114269 iDb = sqlite3TwoPartName(pParse, pName1, pName2, pUnqual: &pName);
114270 if( iDb<0 ) return;
114271 if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
114272 /* If creating a temp table, the name may not be qualified. Unless
114273 ** the database name is "temp" anyway. */
114274 sqlite3ErrorMsg(pParse, zFormat: "temporary table name must be unqualified");
114275 return;
114276 }
114277 if( !OMIT_TEMPDB && isTemp ) iDb = 1;
114278 zName = sqlite3NameFromToken(db, pName);
114279 if( IN_RENAME_OBJECT ){
114280 sqlite3RenameTokenMap(pParse, pPtr: (void*)zName, pToken: pName);
114281 }
114282 }
114283 pParse->sNameToken = *pName;
114284 if( zName==0 ) return;
114285 if( sqlite3CheckObjectName(pParse, zName, zType: isView?"view":"table", zTblName: zName) ){
114286 goto begin_table_error;
114287 }
114288 if( db->init.iDb==1 ) isTemp = 1;
114289#ifndef SQLITE_OMIT_AUTHORIZATION
114290 assert( isTemp==0 || isTemp==1 );
114291 assert( isView==0 || isView==1 );
114292 {
114293 static const u8 aCode[] = {
114294 SQLITE_CREATE_TABLE,
114295 SQLITE_CREATE_TEMP_TABLE,
114296 SQLITE_CREATE_VIEW,
114297 SQLITE_CREATE_TEMP_VIEW
114298 };
114299 char *zDb = db->aDb[iDb].zDbSName;
114300 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), zArg2: 0, zArg3: zDb) ){
114301 goto begin_table_error;
114302 }
114303 if( !isVirtual && sqlite3AuthCheck(pParse, code: (int)aCode[isTemp+2*isView],
114304 zArg1: zName, zArg2: 0, zArg3: zDb) ){
114305 goto begin_table_error;
114306 }
114307 }
114308#endif
114309
114310 /* Make sure the new table name does not collide with an existing
114311 ** index or table name in the same database. Issue an error message if
114312 ** it does. The exception is if the statement being parsed was passed
114313 ** to an sqlite3_declare_vtab() call. In that case only the column names
114314 ** and types will be used, so there is no need to test for namespace
114315 ** collisions.
114316 */
114317 if( !IN_SPECIAL_PARSE ){
114318 char *zDb = db->aDb[iDb].zDbSName;
114319 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
114320 goto begin_table_error;
114321 }
114322 pTable = sqlite3FindTable(db, zName, zDatabase: zDb);
114323 if( pTable ){
114324 if( !noErr ){
114325 sqlite3ErrorMsg(pParse, zFormat: "table %T already exists", pName);
114326 }else{
114327 assert( !db->init.busy || CORRUPT_DB );
114328 sqlite3CodeVerifySchema(pParse, iDb);
114329 sqlite3ForceNotReadOnly(pParse);
114330 }
114331 goto begin_table_error;
114332 }
114333 if( sqlite3FindIndex(db, zName, zDb)!=0 ){
114334 sqlite3ErrorMsg(pParse, zFormat: "there is already an index named %s", zName);
114335 goto begin_table_error;
114336 }
114337 }
114338
114339 pTable = sqlite3DbMallocZero(db, n: sizeof(Table));
114340 if( pTable==0 ){
114341 assert( db->mallocFailed );
114342 pParse->rc = SQLITE_NOMEM_BKPT;
114343 pParse->nErr++;
114344 goto begin_table_error;
114345 }
114346 pTable->zName = zName;
114347 pTable->iPKey = -1;
114348 pTable->pSchema = db->aDb[iDb].pSchema;
114349 pTable->nTabRef = 1;
114350#ifdef SQLITE_DEFAULT_ROWEST
114351 pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST);
114352#else
114353 pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
114354#endif
114355 assert( pParse->pNewTable==0 );
114356 pParse->pNewTable = pTable;
114357
114358 /* Begin generating the code that will insert the table record into
114359 ** the schema table. Note in particular that we must go ahead
114360 ** and allocate the record number for the table entry now. Before any
114361 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
114362 ** indices to be created and the table record must come before the
114363 ** indices. Hence, the record number for the table must be allocated
114364 ** now.
114365 */
114366 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
114367 int addr1;
114368 int fileFormat;
114369 int reg1, reg2, reg3;
114370 /* nullRow[] is an OP_Record encoding of a row containing 5 NULLs */
114371 static const char nullRow[] = { 6, 0, 0, 0, 0, 0 };
114372 sqlite3BeginWriteOperation(pParse, 1, iDb);
114373
114374#ifndef SQLITE_OMIT_VIRTUALTABLE
114375 if( isVirtual ){
114376 sqlite3VdbeAddOp0(p: v, OP_VBegin);
114377 }
114378#endif
114379
114380 /* If the file format and encoding in the database have not been set,
114381 ** set them now.
114382 */
114383 reg1 = pParse->regRowid = ++pParse->nMem;
114384 reg2 = pParse->regRoot = ++pParse->nMem;
114385 reg3 = ++pParse->nMem;
114386 sqlite3VdbeAddOp3(p: v, OP_ReadCookie, p1: iDb, p2: reg3, BTREE_FILE_FORMAT);
114387 sqlite3VdbeUsesBtree(p: v, i: iDb);
114388 addr1 = sqlite3VdbeAddOp1(p: v, OP_If, p1: reg3); VdbeCoverage(v);
114389 fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
114390 1 : SQLITE_MAX_FILE_FORMAT;
114391 sqlite3VdbeAddOp3(p: v, OP_SetCookie, p1: iDb, BTREE_FILE_FORMAT, p3: fileFormat);
114392 sqlite3VdbeAddOp3(p: v, OP_SetCookie, p1: iDb, BTREE_TEXT_ENCODING, ENC(db));
114393 sqlite3VdbeJumpHere(p: v, addr: addr1);
114394
114395 /* This just creates a place-holder record in the sqlite_schema table.
114396 ** The record created does not contain anything yet. It will be replaced
114397 ** by the real entry in code generated at sqlite3EndTable().
114398 **
114399 ** The rowid for the new entry is left in register pParse->regRowid.
114400 ** The root page number of the new table is left in reg pParse->regRoot.
114401 ** The rowid and root page number values are needed by the code that
114402 ** sqlite3EndTable will generate.
114403 */
114404#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
114405 if( isView || isVirtual ){
114406 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: reg2);
114407 }else
114408#endif
114409 {
114410 assert( !pParse->bReturning );
114411 pParse->u1.addrCrTab =
114412 sqlite3VdbeAddOp3(p: v, OP_CreateBtree, p1: iDb, p2: reg2, BTREE_INTKEY);
114413 }
114414 sqlite3OpenSchemaTable(p: pParse, iDb);
114415 sqlite3VdbeAddOp2(p: v, OP_NewRowid, p1: 0, p2: reg1);
114416 sqlite3VdbeAddOp4(p: v, OP_Blob, p1: 6, p2: reg3, p3: 0, zP4: nullRow, P4_STATIC);
114417 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: 0, p2: reg3, p3: reg1);
114418 sqlite3VdbeChangeP5(p: v, OPFLAG_APPEND);
114419 sqlite3VdbeAddOp0(p: v, OP_Close);
114420 }
114421
114422 /* Normal (non-error) return. */
114423 return;
114424
114425 /* If an error occurs, we jump here */
114426begin_table_error:
114427 pParse->checkSchema = 1;
114428 sqlite3DbFree(db, p: zName);
114429 return;
114430}
114431
114432/* Set properties of a table column based on the (magical)
114433** name of the column.
114434*/
114435#if SQLITE_ENABLE_HIDDEN_COLUMNS
114436SQLITE_PRIVATE void sqlite3ColumnPropertiesFromName(Table *pTab, Column *pCol){
114437 if( sqlite3_strnicmp(pCol->zCnName, "__hidden__", 10)==0 ){
114438 pCol->colFlags |= COLFLAG_HIDDEN;
114439 if( pTab ) pTab->tabFlags |= TF_HasHidden;
114440 }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){
114441 pTab->tabFlags |= TF_OOOHidden;
114442 }
114443}
114444#endif
114445
114446/*
114447** Name of the special TEMP trigger used to implement RETURNING. The
114448** name begins with "sqlite_" so that it is guaranteed not to collide
114449** with any application-generated triggers.
114450*/
114451#define RETURNING_TRIGGER_NAME "sqlite_returning"
114452
114453/*
114454** Clean up the data structures associated with the RETURNING clause.
114455*/
114456static void sqlite3DeleteReturning(sqlite3 *db, Returning *pRet){
114457 Hash *pHash;
114458 pHash = &(db->aDb[1].pSchema->trigHash);
114459 sqlite3HashInsert(pH: pHash, RETURNING_TRIGGER_NAME, data: 0);
114460 sqlite3ExprListDelete(db, pList: pRet->pReturnEL);
114461 sqlite3DbFree(db, p: pRet);
114462}
114463
114464/*
114465** Add the RETURNING clause to the parse currently underway.
114466**
114467** This routine creates a special TEMP trigger that will fire for each row
114468** of the DML statement. That TEMP trigger contains a single SELECT
114469** statement with a result set that is the argument of the RETURNING clause.
114470** The trigger has the Trigger.bReturning flag and an opcode of
114471** TK_RETURNING instead of TK_SELECT, so that the trigger code generator
114472** knows to handle it specially. The TEMP trigger is automatically
114473** removed at the end of the parse.
114474**
114475** When this routine is called, we do not yet know if the RETURNING clause
114476** is attached to a DELETE, INSERT, or UPDATE, so construct it as a
114477** RETURNING trigger instead. It will then be converted into the appropriate
114478** type on the first call to sqlite3TriggersExist().
114479*/
114480SQLITE_PRIVATE void sqlite3AddReturning(Parse *pParse, ExprList *pList){
114481 Returning *pRet;
114482 Hash *pHash;
114483 sqlite3 *db = pParse->db;
114484 if( pParse->pNewTrigger ){
114485 sqlite3ErrorMsg(pParse, zFormat: "cannot use RETURNING in a trigger");
114486 }else{
114487 assert( pParse->bReturning==0 );
114488 }
114489 pParse->bReturning = 1;
114490 pRet = sqlite3DbMallocZero(db, n: sizeof(*pRet));
114491 if( pRet==0 ){
114492 sqlite3ExprListDelete(db, pList);
114493 return;
114494 }
114495 pParse->u1.pReturning = pRet;
114496 pRet->pParse = pParse;
114497 pRet->pReturnEL = pList;
114498 sqlite3ParserAddCleanup(pParse,
114499 (void(*)(sqlite3*,void*))sqlite3DeleteReturning, pRet);
114500 testcase( pParse->earlyCleanup );
114501 if( db->mallocFailed ) return;
114502 pRet->retTrig.zName = RETURNING_TRIGGER_NAME;
114503 pRet->retTrig.op = TK_RETURNING;
114504 pRet->retTrig.tr_tm = TRIGGER_AFTER;
114505 pRet->retTrig.bReturning = 1;
114506 pRet->retTrig.pSchema = db->aDb[1].pSchema;
114507 pRet->retTrig.pTabSchema = db->aDb[1].pSchema;
114508 pRet->retTrig.step_list = &pRet->retTStep;
114509 pRet->retTStep.op = TK_RETURNING;
114510 pRet->retTStep.pTrig = &pRet->retTrig;
114511 pRet->retTStep.pExprList = pList;
114512 pHash = &(db->aDb[1].pSchema->trigHash);
114513 assert( sqlite3HashFind(pHash, RETURNING_TRIGGER_NAME)==0 || pParse->nErr );
114514 if( sqlite3HashInsert(pH: pHash, RETURNING_TRIGGER_NAME, data: &pRet->retTrig)
114515 ==&pRet->retTrig ){
114516 sqlite3OomFault(db);
114517 }
114518}
114519
114520/*
114521** Add a new column to the table currently being constructed.
114522**
114523** The parser calls this routine once for each column declaration
114524** in a CREATE TABLE statement. sqlite3StartTable() gets called
114525** first to get things going. Then this routine is called for each
114526** column.
114527*/
114528SQLITE_PRIVATE void sqlite3AddColumn(Parse *pParse, Token sName, Token sType){
114529 Table *p;
114530 int i;
114531 char *z;
114532 char *zType;
114533 Column *pCol;
114534 sqlite3 *db = pParse->db;
114535 u8 hName;
114536 Column *aNew;
114537 u8 eType = COLTYPE_CUSTOM;
114538 u8 szEst = 1;
114539 char affinity = SQLITE_AFF_BLOB;
114540
114541 if( (p = pParse->pNewTable)==0 ) return;
114542 if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
114543 sqlite3ErrorMsg(pParse, zFormat: "too many columns on %s", p->zName);
114544 return;
114545 }
114546 if( !IN_RENAME_OBJECT ) sqlite3DequoteToken(p: &sName);
114547
114548 /* Because keywords GENERATE ALWAYS can be converted into indentifiers
114549 ** by the parser, we can sometimes end up with a typename that ends
114550 ** with "generated always". Check for this case and omit the surplus
114551 ** text. */
114552 if( sType.n>=16
114553 && sqlite3_strnicmp(zLeft: sType.z+(sType.n-6),zRight: "always",N: 6)==0
114554 ){
114555 sType.n -= 6;
114556 while( ALWAYS(sType.n>0) && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--;
114557 if( sType.n>=9
114558 && sqlite3_strnicmp(zLeft: sType.z+(sType.n-9),zRight: "generated",N: 9)==0
114559 ){
114560 sType.n -= 9;
114561 while( sType.n>0 && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--;
114562 }
114563 }
114564
114565 /* Check for standard typenames. For standard typenames we will
114566 ** set the Column.eType field rather than storing the typename after
114567 ** the column name, in order to save space. */
114568 if( sType.n>=3 ){
114569 sqlite3DequoteToken(p: &sType);
114570 for(i=0; i<SQLITE_N_STDTYPE; i++){
114571 if( sType.n==sqlite3StdTypeLen[i]
114572 && sqlite3_strnicmp(zLeft: sType.z, zRight: sqlite3StdType[i], N: sType.n)==0
114573 ){
114574 sType.n = 0;
114575 eType = i+1;
114576 affinity = sqlite3StdTypeAffinity[i];
114577 if( affinity<=SQLITE_AFF_TEXT ) szEst = 5;
114578 break;
114579 }
114580 }
114581 }
114582
114583 z = sqlite3DbMallocRaw(db, n: (i64)sName.n + 1 + (i64)sType.n + (sType.n>0) );
114584 if( z==0 ) return;
114585 if( IN_RENAME_OBJECT ) sqlite3RenameTokenMap(pParse, pPtr: (void*)z, pToken: &sName);
114586 memcpy(dest: z, src: sName.z, n: sName.n);
114587 z[sName.n] = 0;
114588 sqlite3Dequote(z);
114589 hName = sqlite3StrIHash(z);
114590 for(i=0; i<p->nCol; i++){
114591 if( p->aCol[i].hName==hName && sqlite3StrICmp(zLeft: z, zRight: p->aCol[i].zCnName)==0 ){
114592 sqlite3ErrorMsg(pParse, zFormat: "duplicate column name: %s", z);
114593 sqlite3DbFree(db, p: z);
114594 return;
114595 }
114596 }
114597 aNew = sqlite3DbRealloc(db,p: p->aCol,n: ((i64)p->nCol+1)*sizeof(p->aCol[0]));
114598 if( aNew==0 ){
114599 sqlite3DbFree(db, p: z);
114600 return;
114601 }
114602 p->aCol = aNew;
114603 pCol = &p->aCol[p->nCol];
114604 memset(s: pCol, c: 0, n: sizeof(p->aCol[0]));
114605 pCol->zCnName = z;
114606 pCol->hName = hName;
114607 sqlite3ColumnPropertiesFromName(p, pCol);
114608
114609 if( sType.n==0 ){
114610 /* If there is no type specified, columns have the default affinity
114611 ** 'BLOB' with a default size of 4 bytes. */
114612 pCol->affinity = affinity;
114613 pCol->eCType = eType;
114614 pCol->szEst = szEst;
114615#ifdef SQLITE_ENABLE_SORTER_REFERENCES
114616 if( affinity==SQLITE_AFF_BLOB ){
114617 if( 4>=sqlite3GlobalConfig.szSorterRef ){
114618 pCol->colFlags |= COLFLAG_SORTERREF;
114619 }
114620 }
114621#endif
114622 }else{
114623 zType = z + sqlite3Strlen30(z) + 1;
114624 memcpy(dest: zType, src: sType.z, n: sType.n);
114625 zType[sType.n] = 0;
114626 sqlite3Dequote(z: zType);
114627 pCol->affinity = sqlite3AffinityType(zType, pCol);
114628 pCol->colFlags |= COLFLAG_HASTYPE;
114629 }
114630 p->nCol++;
114631 p->nNVCol++;
114632 pParse->constraintName.n = 0;
114633}
114634
114635/*
114636** This routine is called by the parser while in the middle of
114637** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
114638** been seen on a column. This routine sets the notNull flag on
114639** the column currently under construction.
114640*/
114641SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){
114642 Table *p;
114643 Column *pCol;
114644 p = pParse->pNewTable;
114645 if( p==0 || NEVER(p->nCol<1) ) return;
114646 pCol = &p->aCol[p->nCol-1];
114647 pCol->notNull = (u8)onError;
114648 p->tabFlags |= TF_HasNotNull;
114649
114650 /* Set the uniqNotNull flag on any UNIQUE or PK indexes already created
114651 ** on this column. */
114652 if( pCol->colFlags & COLFLAG_UNIQUE ){
114653 Index *pIdx;
114654 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
114655 assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None );
114656 if( pIdx->aiColumn[0]==p->nCol-1 ){
114657 pIdx->uniqNotNull = 1;
114658 }
114659 }
114660 }
114661}
114662
114663/*
114664** Scan the column type name zType (length nType) and return the
114665** associated affinity type.
114666**
114667** This routine does a case-independent search of zType for the
114668** substrings in the following table. If one of the substrings is
114669** found, the corresponding affinity is returned. If zType contains
114670** more than one of the substrings, entries toward the top of
114671** the table take priority. For example, if zType is 'BLOBINT',
114672** SQLITE_AFF_INTEGER is returned.
114673**
114674** Substring | Affinity
114675** --------------------------------
114676** 'INT' | SQLITE_AFF_INTEGER
114677** 'CHAR' | SQLITE_AFF_TEXT
114678** 'CLOB' | SQLITE_AFF_TEXT
114679** 'TEXT' | SQLITE_AFF_TEXT
114680** 'BLOB' | SQLITE_AFF_BLOB
114681** 'REAL' | SQLITE_AFF_REAL
114682** 'FLOA' | SQLITE_AFF_REAL
114683** 'DOUB' | SQLITE_AFF_REAL
114684**
114685** If none of the substrings in the above table are found,
114686** SQLITE_AFF_NUMERIC is returned.
114687*/
114688SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn, Column *pCol){
114689 u32 h = 0;
114690 char aff = SQLITE_AFF_NUMERIC;
114691 const char *zChar = 0;
114692
114693 assert( zIn!=0 );
114694 while( zIn[0] ){
114695 h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
114696 zIn++;
114697 if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */
114698 aff = SQLITE_AFF_TEXT;
114699 zChar = zIn;
114700 }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */
114701 aff = SQLITE_AFF_TEXT;
114702 }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */
114703 aff = SQLITE_AFF_TEXT;
114704 }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */
114705 && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
114706 aff = SQLITE_AFF_BLOB;
114707 if( zIn[0]=='(' ) zChar = zIn;
114708#ifndef SQLITE_OMIT_FLOATING_POINT
114709 }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */
114710 && aff==SQLITE_AFF_NUMERIC ){
114711 aff = SQLITE_AFF_REAL;
114712 }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */
114713 && aff==SQLITE_AFF_NUMERIC ){
114714 aff = SQLITE_AFF_REAL;
114715 }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b') /* DOUB */
114716 && aff==SQLITE_AFF_NUMERIC ){
114717 aff = SQLITE_AFF_REAL;
114718#endif
114719 }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){ /* INT */
114720 aff = SQLITE_AFF_INTEGER;
114721 break;
114722 }
114723 }
114724
114725 /* If pCol is not NULL, store an estimate of the field size. The
114726 ** estimate is scaled so that the size of an integer is 1. */
114727 if( pCol ){
114728 int v = 0; /* default size is approx 4 bytes */
114729 if( aff<SQLITE_AFF_NUMERIC ){
114730 if( zChar ){
114731 while( zChar[0] ){
114732 if( sqlite3Isdigit(zChar[0]) ){
114733 /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
114734 sqlite3GetInt32(zNum: zChar, pValue: &v);
114735 break;
114736 }
114737 zChar++;
114738 }
114739 }else{
114740 v = 16; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/
114741 }
114742 }
114743#ifdef SQLITE_ENABLE_SORTER_REFERENCES
114744 if( v>=sqlite3GlobalConfig.szSorterRef ){
114745 pCol->colFlags |= COLFLAG_SORTERREF;
114746 }
114747#endif
114748 v = v/4 + 1;
114749 if( v>255 ) v = 255;
114750 pCol->szEst = v;
114751 }
114752 return aff;
114753}
114754
114755/*
114756** The expression is the default value for the most recently added column
114757** of the table currently under construction.
114758**
114759** Default value expressions must be constant. Raise an exception if this
114760** is not the case.
114761**
114762** This routine is called by the parser while in the middle of
114763** parsing a CREATE TABLE statement.
114764*/
114765SQLITE_PRIVATE void sqlite3AddDefaultValue(
114766 Parse *pParse, /* Parsing context */
114767 Expr *pExpr, /* The parsed expression of the default value */
114768 const char *zStart, /* Start of the default value text */
114769 const char *zEnd /* First character past end of defaut value text */
114770){
114771 Table *p;
114772 Column *pCol;
114773 sqlite3 *db = pParse->db;
114774 p = pParse->pNewTable;
114775 if( p!=0 ){
114776 int isInit = db->init.busy && db->init.iDb!=1;
114777 pCol = &(p->aCol[p->nCol-1]);
114778 if( !sqlite3ExprIsConstantOrFunction(p: pExpr, isInit) ){
114779 sqlite3ErrorMsg(pParse, zFormat: "default value of column [%s] is not constant",
114780 pCol->zCnName);
114781#ifndef SQLITE_OMIT_GENERATED_COLUMNS
114782 }else if( pCol->colFlags & COLFLAG_GENERATED ){
114783 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
114784 testcase( pCol->colFlags & COLFLAG_STORED );
114785 sqlite3ErrorMsg(pParse, zFormat: "cannot use DEFAULT on a generated column");
114786#endif
114787 }else{
114788 /* A copy of pExpr is used instead of the original, as pExpr contains
114789 ** tokens that point to volatile memory.
114790 */
114791 Expr x, *pDfltExpr;
114792 memset(s: &x, c: 0, n: sizeof(x));
114793 x.op = TK_SPAN;
114794 x.u.zToken = sqlite3DbSpanDup(db, zStart, zEnd);
114795 x.pLeft = pExpr;
114796 x.flags = EP_Skip;
114797 pDfltExpr = sqlite3ExprDup(db, p: &x, EXPRDUP_REDUCE);
114798 sqlite3DbFree(db, p: x.u.zToken);
114799 sqlite3ColumnSetExpr(pParse, pTab: p, pCol, pExpr: pDfltExpr);
114800 }
114801 }
114802 if( IN_RENAME_OBJECT ){
114803 sqlite3RenameExprUnmap(pParse, pExpr);
114804 }
114805 sqlite3ExprDelete(db, p: pExpr);
114806}
114807
114808/*
114809** Backwards Compatibility Hack:
114810**
114811** Historical versions of SQLite accepted strings as column names in
114812** indexes and PRIMARY KEY constraints and in UNIQUE constraints. Example:
114813**
114814** CREATE TABLE xyz(a,b,c,d,e,PRIMARY KEY('a'),UNIQUE('b','c' COLLATE trim)
114815** CREATE INDEX abc ON xyz('c','d' DESC,'e' COLLATE nocase DESC);
114816**
114817** This is goofy. But to preserve backwards compatibility we continue to
114818** accept it. This routine does the necessary conversion. It converts
114819** the expression given in its argument from a TK_STRING into a TK_ID
114820** if the expression is just a TK_STRING with an optional COLLATE clause.
114821** If the expression is anything other than TK_STRING, the expression is
114822** unchanged.
114823*/
114824static void sqlite3StringToId(Expr *p){
114825 if( p->op==TK_STRING ){
114826 p->op = TK_ID;
114827 }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){
114828 p->pLeft->op = TK_ID;
114829 }
114830}
114831
114832/*
114833** Tag the given column as being part of the PRIMARY KEY
114834*/
114835static void makeColumnPartOfPrimaryKey(Parse *pParse, Column *pCol){
114836 pCol->colFlags |= COLFLAG_PRIMKEY;
114837#ifndef SQLITE_OMIT_GENERATED_COLUMNS
114838 if( pCol->colFlags & COLFLAG_GENERATED ){
114839 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
114840 testcase( pCol->colFlags & COLFLAG_STORED );
114841 sqlite3ErrorMsg(pParse,
114842 zFormat: "generated columns cannot be part of the PRIMARY KEY");
114843 }
114844#endif
114845}
114846
114847/*
114848** Designate the PRIMARY KEY for the table. pList is a list of names
114849** of columns that form the primary key. If pList is NULL, then the
114850** most recently added column of the table is the primary key.
114851**
114852** A table can have at most one primary key. If the table already has
114853** a primary key (and this is the second primary key) then create an
114854** error.
114855**
114856** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
114857** then we will try to use that column as the rowid. Set the Table.iPKey
114858** field of the table under construction to be the index of the
114859** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
114860** no INTEGER PRIMARY KEY.
114861**
114862** If the key is not an INTEGER PRIMARY KEY, then create a unique
114863** index for the key. No index is created for INTEGER PRIMARY KEYs.
114864*/
114865SQLITE_PRIVATE void sqlite3AddPrimaryKey(
114866 Parse *pParse, /* Parsing context */
114867 ExprList *pList, /* List of field names to be indexed */
114868 int onError, /* What to do with a uniqueness conflict */
114869 int autoInc, /* True if the AUTOINCREMENT keyword is present */
114870 int sortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */
114871){
114872 Table *pTab = pParse->pNewTable;
114873 Column *pCol = 0;
114874 int iCol = -1, i;
114875 int nTerm;
114876 if( pTab==0 ) goto primary_key_exit;
114877 if( pTab->tabFlags & TF_HasPrimaryKey ){
114878 sqlite3ErrorMsg(pParse,
114879 zFormat: "table \"%s\" has more than one primary key", pTab->zName);
114880 goto primary_key_exit;
114881 }
114882 pTab->tabFlags |= TF_HasPrimaryKey;
114883 if( pList==0 ){
114884 iCol = pTab->nCol - 1;
114885 pCol = &pTab->aCol[iCol];
114886 makeColumnPartOfPrimaryKey(pParse, pCol);
114887 nTerm = 1;
114888 }else{
114889 nTerm = pList->nExpr;
114890 for(i=0; i<nTerm; i++){
114891 Expr *pCExpr = sqlite3ExprSkipCollate(pExpr: pList->a[i].pExpr);
114892 assert( pCExpr!=0 );
114893 sqlite3StringToId(p: pCExpr);
114894 if( pCExpr->op==TK_ID ){
114895 const char *zCName;
114896 assert( !ExprHasProperty(pCExpr, EP_IntValue) );
114897 zCName = pCExpr->u.zToken;
114898 for(iCol=0; iCol<pTab->nCol; iCol++){
114899 if( sqlite3StrICmp(zLeft: zCName, zRight: pTab->aCol[iCol].zCnName)==0 ){
114900 pCol = &pTab->aCol[iCol];
114901 makeColumnPartOfPrimaryKey(pParse, pCol);
114902 break;
114903 }
114904 }
114905 }
114906 }
114907 }
114908 if( nTerm==1
114909 && pCol
114910 && pCol->eCType==COLTYPE_INTEGER
114911 && sortOrder!=SQLITE_SO_DESC
114912 ){
114913 if( IN_RENAME_OBJECT && pList ){
114914 Expr *pCExpr = sqlite3ExprSkipCollate(pExpr: pList->a[0].pExpr);
114915 sqlite3RenameTokenRemap(pParse, pTo: &pTab->iPKey, pFrom: pCExpr);
114916 }
114917 pTab->iPKey = iCol;
114918 pTab->keyConf = (u8)onError;
114919 assert( autoInc==0 || autoInc==1 );
114920 pTab->tabFlags |= autoInc*TF_Autoincrement;
114921 if( pList ) pParse->iPkSortOrder = pList->a[0].sortFlags;
114922 (void)sqlite3HasExplicitNulls(pParse, pList);
114923 }else if( autoInc ){
114924#ifndef SQLITE_OMIT_AUTOINCREMENT
114925 sqlite3ErrorMsg(pParse, zFormat: "AUTOINCREMENT is only allowed on an "
114926 "INTEGER PRIMARY KEY");
114927#endif
114928 }else{
114929 sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
114930 0, sortOrder, 0, SQLITE_IDXTYPE_PRIMARYKEY);
114931 pList = 0;
114932 }
114933
114934primary_key_exit:
114935 sqlite3ExprListDelete(db: pParse->db, pList);
114936 return;
114937}
114938
114939/*
114940** Add a new CHECK constraint to the table currently under construction.
114941*/
114942SQLITE_PRIVATE void sqlite3AddCheckConstraint(
114943 Parse *pParse, /* Parsing context */
114944 Expr *pCheckExpr, /* The check expression */
114945 const char *zStart, /* Opening "(" */
114946 const char *zEnd /* Closing ")" */
114947){
114948#ifndef SQLITE_OMIT_CHECK
114949 Table *pTab = pParse->pNewTable;
114950 sqlite3 *db = pParse->db;
114951 if( pTab && !IN_DECLARE_VTAB
114952 && !sqlite3BtreeIsReadonly(p: db->aDb[db->init.iDb].pBt)
114953 ){
114954 pTab->pCheck = sqlite3ExprListAppend(pParse, pList: pTab->pCheck, pExpr: pCheckExpr);
114955 if( pParse->constraintName.n ){
114956 sqlite3ExprListSetName(pParse, pList: pTab->pCheck, pName: &pParse->constraintName, dequote: 1);
114957 }else{
114958 Token t;
114959 for(zStart++; sqlite3Isspace(zStart[0]); zStart++){}
114960 while( sqlite3Isspace(zEnd[-1]) ){ zEnd--; }
114961 t.z = zStart;
114962 t.n = (int)(zEnd - t.z);
114963 sqlite3ExprListSetName(pParse, pList: pTab->pCheck, pName: &t, dequote: 1);
114964 }
114965 }else
114966#endif
114967 {
114968 sqlite3ExprDelete(db: pParse->db, p: pCheckExpr);
114969 }
114970}
114971
114972/*
114973** Set the collation function of the most recently parsed table column
114974** to the CollSeq given.
114975*/
114976SQLITE_PRIVATE void sqlite3AddCollateType(Parse *pParse, Token *pToken){
114977 Table *p;
114978 int i;
114979 char *zColl; /* Dequoted name of collation sequence */
114980 sqlite3 *db;
114981
114982 if( (p = pParse->pNewTable)==0 || IN_RENAME_OBJECT ) return;
114983 i = p->nCol-1;
114984 db = pParse->db;
114985 zColl = sqlite3NameFromToken(db, pName: pToken);
114986 if( !zColl ) return;
114987
114988 if( sqlite3LocateCollSeq(pParse, zName: zColl) ){
114989 Index *pIdx;
114990 sqlite3ColumnSetColl(db, pCol: &p->aCol[i], zColl);
114991
114992 /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
114993 ** then an index may have been created on this column before the
114994 ** collation type was added. Correct this if it is the case.
114995 */
114996 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
114997 assert( pIdx->nKeyCol==1 );
114998 if( pIdx->aiColumn[0]==i ){
114999 pIdx->azColl[0] = sqlite3ColumnColl(pCol: &p->aCol[i]);
115000 }
115001 }
115002 }
115003 sqlite3DbFree(db, p: zColl);
115004}
115005
115006/* Change the most recently parsed column to be a GENERATED ALWAYS AS
115007** column.
115008*/
115009SQLITE_PRIVATE void sqlite3AddGenerated(Parse *pParse, Expr *pExpr, Token *pType){
115010#ifndef SQLITE_OMIT_GENERATED_COLUMNS
115011 u8 eType = COLFLAG_VIRTUAL;
115012 Table *pTab = pParse->pNewTable;
115013 Column *pCol;
115014 if( pTab==0 ){
115015 /* generated column in an CREATE TABLE IF NOT EXISTS that already exists */
115016 goto generated_done;
115017 }
115018 pCol = &(pTab->aCol[pTab->nCol-1]);
115019 if( IN_DECLARE_VTAB ){
115020 sqlite3ErrorMsg(pParse, zFormat: "virtual tables cannot use computed columns");
115021 goto generated_done;
115022 }
115023 if( pCol->iDflt>0 ) goto generated_error;
115024 if( pType ){
115025 if( pType->n==7 && sqlite3StrNICmp(zLeft: "virtual",zRight: pType->z,N: 7)==0 ){
115026 /* no-op */
115027 }else if( pType->n==6 && sqlite3StrNICmp(zLeft: "stored",zRight: pType->z,N: 6)==0 ){
115028 eType = COLFLAG_STORED;
115029 }else{
115030 goto generated_error;
115031 }
115032 }
115033 if( eType==COLFLAG_VIRTUAL ) pTab->nNVCol--;
115034 pCol->colFlags |= eType;
115035 assert( TF_HasVirtual==COLFLAG_VIRTUAL );
115036 assert( TF_HasStored==COLFLAG_STORED );
115037 pTab->tabFlags |= eType;
115038 if( pCol->colFlags & COLFLAG_PRIMKEY ){
115039 makeColumnPartOfPrimaryKey(pParse, pCol); /* For the error message */
115040 }
115041 sqlite3ColumnSetExpr(pParse, pTab, pCol, pExpr);
115042 pExpr = 0;
115043 goto generated_done;
115044
115045generated_error:
115046 sqlite3ErrorMsg(pParse, zFormat: "error in generated column \"%s\"",
115047 pCol->zCnName);
115048generated_done:
115049 sqlite3ExprDelete(db: pParse->db, p: pExpr);
115050#else
115051 /* Throw and error for the GENERATED ALWAYS AS clause if the
115052 ** SQLITE_OMIT_GENERATED_COLUMNS compile-time option is used. */
115053 sqlite3ErrorMsg(pParse, "generated columns not supported");
115054 sqlite3ExprDelete(pParse->db, pExpr);
115055#endif
115056}
115057
115058/*
115059** Generate code that will increment the schema cookie.
115060**
115061** The schema cookie is used to determine when the schema for the
115062** database changes. After each schema change, the cookie value
115063** changes. When a process first reads the schema it records the
115064** cookie. Thereafter, whenever it goes to access the database,
115065** it checks the cookie to make sure the schema has not changed
115066** since it was last read.
115067**
115068** This plan is not completely bullet-proof. It is possible for
115069** the schema to change multiple times and for the cookie to be
115070** set back to prior value. But schema changes are infrequent
115071** and the probability of hitting the same cookie value is only
115072** 1 chance in 2^32. So we're safe enough.
115073**
115074** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments
115075** the schema-version whenever the schema changes.
115076*/
115077SQLITE_PRIVATE void sqlite3ChangeCookie(Parse *pParse, int iDb){
115078 sqlite3 *db = pParse->db;
115079 Vdbe *v = pParse->pVdbe;
115080 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
115081 sqlite3VdbeAddOp3(p: v, OP_SetCookie, p1: iDb, BTREE_SCHEMA_VERSION,
115082 p3: (int)(1+(unsigned)db->aDb[iDb].pSchema->schema_cookie));
115083}
115084
115085/*
115086** Measure the number of characters needed to output the given
115087** identifier. The number returned includes any quotes used
115088** but does not include the null terminator.
115089**
115090** The estimate is conservative. It might be larger that what is
115091** really needed.
115092*/
115093static int identLength(const char *z){
115094 int n;
115095 for(n=0; *z; n++, z++){
115096 if( *z=='"' ){ n++; }
115097 }
115098 return n + 2;
115099}
115100
115101/*
115102** The first parameter is a pointer to an output buffer. The second
115103** parameter is a pointer to an integer that contains the offset at
115104** which to write into the output buffer. This function copies the
115105** nul-terminated string pointed to by the third parameter, zSignedIdent,
115106** to the specified offset in the buffer and updates *pIdx to refer
115107** to the first byte after the last byte written before returning.
115108**
115109** If the string zSignedIdent consists entirely of alpha-numeric
115110** characters, does not begin with a digit and is not an SQL keyword,
115111** then it is copied to the output buffer exactly as it is. Otherwise,
115112** it is quoted using double-quotes.
115113*/
115114static void identPut(char *z, int *pIdx, char *zSignedIdent){
115115 unsigned char *zIdent = (unsigned char*)zSignedIdent;
115116 int i, j, needQuote;
115117 i = *pIdx;
115118
115119 for(j=0; zIdent[j]; j++){
115120 if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
115121 }
115122 needQuote = sqlite3Isdigit(zIdent[0])
115123 || sqlite3KeywordCode(zIdent, j)!=TK_ID
115124 || zIdent[j]!=0
115125 || j==0;
115126
115127 if( needQuote ) z[i++] = '"';
115128 for(j=0; zIdent[j]; j++){
115129 z[i++] = zIdent[j];
115130 if( zIdent[j]=='"' ) z[i++] = '"';
115131 }
115132 if( needQuote ) z[i++] = '"';
115133 z[i] = 0;
115134 *pIdx = i;
115135}
115136
115137/*
115138** Generate a CREATE TABLE statement appropriate for the given
115139** table. Memory to hold the text of the statement is obtained
115140** from sqliteMalloc() and must be freed by the calling function.
115141*/
115142static char *createTableStmt(sqlite3 *db, Table *p){
115143 int i, k, n;
115144 char *zStmt;
115145 char *zSep, *zSep2, *zEnd;
115146 Column *pCol;
115147 n = 0;
115148 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
115149 n += identLength(z: pCol->zCnName) + 5;
115150 }
115151 n += identLength(z: p->zName);
115152 if( n<50 ){
115153 zSep = "";
115154 zSep2 = ",";
115155 zEnd = ")";
115156 }else{
115157 zSep = "\n ";
115158 zSep2 = ",\n ";
115159 zEnd = "\n)";
115160 }
115161 n += 35 + 6*p->nCol;
115162 zStmt = sqlite3DbMallocRaw(db: 0, n);
115163 if( zStmt==0 ){
115164 sqlite3OomFault(db);
115165 return 0;
115166 }
115167 sqlite3_snprintf(n, zBuf: zStmt, zFormat: "CREATE TABLE ");
115168 k = sqlite3Strlen30(z: zStmt);
115169 identPut(z: zStmt, pIdx: &k, zSignedIdent: p->zName);
115170 zStmt[k++] = '(';
115171 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
115172 static const char * const azType[] = {
115173 /* SQLITE_AFF_BLOB */ "",
115174 /* SQLITE_AFF_TEXT */ " TEXT",
115175 /* SQLITE_AFF_NUMERIC */ " NUM",
115176 /* SQLITE_AFF_INTEGER */ " INT",
115177 /* SQLITE_AFF_REAL */ " REAL"
115178 };
115179 int len;
115180 const char *zType;
115181
115182 sqlite3_snprintf(n: n-k, zBuf: &zStmt[k], zFormat: zSep);
115183 k += sqlite3Strlen30(z: &zStmt[k]);
115184 zSep = zSep2;
115185 identPut(z: zStmt, pIdx: &k, zSignedIdent: pCol->zCnName);
115186 assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 );
115187 assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) );
115188 testcase( pCol->affinity==SQLITE_AFF_BLOB );
115189 testcase( pCol->affinity==SQLITE_AFF_TEXT );
115190 testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
115191 testcase( pCol->affinity==SQLITE_AFF_INTEGER );
115192 testcase( pCol->affinity==SQLITE_AFF_REAL );
115193
115194 zType = azType[pCol->affinity - SQLITE_AFF_BLOB];
115195 len = sqlite3Strlen30(z: zType);
115196 assert( pCol->affinity==SQLITE_AFF_BLOB
115197 || pCol->affinity==sqlite3AffinityType(zType, 0) );
115198 memcpy(dest: &zStmt[k], src: zType, n: len);
115199 k += len;
115200 assert( k<=n );
115201 }
115202 sqlite3_snprintf(n: n-k, zBuf: &zStmt[k], zFormat: "%s", zEnd);
115203 return zStmt;
115204}
115205
115206/*
115207** Resize an Index object to hold N columns total. Return SQLITE_OK
115208** on success and SQLITE_NOMEM on an OOM error.
115209*/
115210static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){
115211 char *zExtra;
115212 int nByte;
115213 if( pIdx->nColumn>=N ) return SQLITE_OK;
115214 assert( pIdx->isResized==0 );
115215 nByte = (sizeof(char*) + sizeof(LogEst) + sizeof(i16) + 1)*N;
115216 zExtra = sqlite3DbMallocZero(db, n: nByte);
115217 if( zExtra==0 ) return SQLITE_NOMEM_BKPT;
115218 memcpy(dest: zExtra, src: pIdx->azColl, n: sizeof(char*)*pIdx->nColumn);
115219 pIdx->azColl = (const char**)zExtra;
115220 zExtra += sizeof(char*)*N;
115221 memcpy(dest: zExtra, src: pIdx->aiRowLogEst, n: sizeof(LogEst)*(pIdx->nKeyCol+1));
115222 pIdx->aiRowLogEst = (LogEst*)zExtra;
115223 zExtra += sizeof(LogEst)*N;
115224 memcpy(dest: zExtra, src: pIdx->aiColumn, n: sizeof(i16)*pIdx->nColumn);
115225 pIdx->aiColumn = (i16*)zExtra;
115226 zExtra += sizeof(i16)*N;
115227 memcpy(dest: zExtra, src: pIdx->aSortOrder, n: pIdx->nColumn);
115228 pIdx->aSortOrder = (u8*)zExtra;
115229 pIdx->nColumn = N;
115230 pIdx->isResized = 1;
115231 return SQLITE_OK;
115232}
115233
115234/*
115235** Estimate the total row width for a table.
115236*/
115237static void estimateTableWidth(Table *pTab){
115238 unsigned wTable = 0;
115239 const Column *pTabCol;
115240 int i;
115241 for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){
115242 wTable += pTabCol->szEst;
115243 }
115244 if( pTab->iPKey<0 ) wTable++;
115245 pTab->szTabRow = sqlite3LogEst(x: wTable*4);
115246}
115247
115248/*
115249** Estimate the average size of a row for an index.
115250*/
115251static void estimateIndexWidth(Index *pIdx){
115252 unsigned wIndex = 0;
115253 int i;
115254 const Column *aCol = pIdx->pTable->aCol;
115255 for(i=0; i<pIdx->nColumn; i++){
115256 i16 x = pIdx->aiColumn[i];
115257 assert( x<pIdx->pTable->nCol );
115258 wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst;
115259 }
115260 pIdx->szIdxRow = sqlite3LogEst(x: wIndex*4);
115261}
115262
115263/* Return true if column number x is any of the first nCol entries of aiCol[].
115264** This is used to determine if the column number x appears in any of the
115265** first nCol entries of an index.
115266*/
115267static int hasColumn(const i16 *aiCol, int nCol, int x){
115268 while( nCol-- > 0 ){
115269 if( x==*(aiCol++) ){
115270 return 1;
115271 }
115272 }
115273 return 0;
115274}
115275
115276/*
115277** Return true if any of the first nKey entries of index pIdx exactly
115278** match the iCol-th entry of pPk. pPk is always a WITHOUT ROWID
115279** PRIMARY KEY index. pIdx is an index on the same table. pIdx may
115280** or may not be the same index as pPk.
115281**
115282** The first nKey entries of pIdx are guaranteed to be ordinary columns,
115283** not a rowid or expression.
115284**
115285** This routine differs from hasColumn() in that both the column and the
115286** collating sequence must match for this routine, but for hasColumn() only
115287** the column name must match.
115288*/
115289static int isDupColumn(Index *pIdx, int nKey, Index *pPk, int iCol){
115290 int i, j;
115291 assert( nKey<=pIdx->nColumn );
115292 assert( iCol<MAX(pPk->nColumn,pPk->nKeyCol) );
115293 assert( pPk->idxType==SQLITE_IDXTYPE_PRIMARYKEY );
115294 assert( pPk->pTable->tabFlags & TF_WithoutRowid );
115295 assert( pPk->pTable==pIdx->pTable );
115296 testcase( pPk==pIdx );
115297 j = pPk->aiColumn[iCol];
115298 assert( j!=XN_ROWID && j!=XN_EXPR );
115299 for(i=0; i<nKey; i++){
115300 assert( pIdx->aiColumn[i]>=0 || j>=0 );
115301 if( pIdx->aiColumn[i]==j
115302 && sqlite3StrICmp(zLeft: pIdx->azColl[i], zRight: pPk->azColl[iCol])==0
115303 ){
115304 return 1;
115305 }
115306 }
115307 return 0;
115308}
115309
115310/* Recompute the colNotIdxed field of the Index.
115311**
115312** colNotIdxed is a bitmask that has a 0 bit representing each indexed
115313** columns that are within the first 63 columns of the table. The
115314** high-order bit of colNotIdxed is always 1. All unindexed columns
115315** of the table have a 1.
115316**
115317** 2019-10-24: For the purpose of this computation, virtual columns are
115318** not considered to be covered by the index, even if they are in the
115319** index, because we do not trust the logic in whereIndexExprTrans() to be
115320** able to find all instances of a reference to the indexed table column
115321** and convert them into references to the index. Hence we always want
115322** the actual table at hand in order to recompute the virtual column, if
115323** necessary.
115324**
115325** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask
115326** to determine if the index is covering index.
115327*/
115328static void recomputeColumnsNotIndexed(Index *pIdx){
115329 Bitmask m = 0;
115330 int j;
115331 Table *pTab = pIdx->pTable;
115332 for(j=pIdx->nColumn-1; j>=0; j--){
115333 int x = pIdx->aiColumn[j];
115334 if( x>=0 && (pTab->aCol[x].colFlags & COLFLAG_VIRTUAL)==0 ){
115335 testcase( x==BMS-1 );
115336 testcase( x==BMS-2 );
115337 if( x<BMS-1 ) m |= MASKBIT(x);
115338 }
115339 }
115340 pIdx->colNotIdxed = ~m;
115341 assert( (pIdx->colNotIdxed>>63)==1 );
115342}
115343
115344/*
115345** This routine runs at the end of parsing a CREATE TABLE statement that
115346** has a WITHOUT ROWID clause. The job of this routine is to convert both
115347** internal schema data structures and the generated VDBE code so that they
115348** are appropriate for a WITHOUT ROWID table instead of a rowid table.
115349** Changes include:
115350**
115351** (1) Set all columns of the PRIMARY KEY schema object to be NOT NULL.
115352** (2) Convert P3 parameter of the OP_CreateBtree from BTREE_INTKEY
115353** into BTREE_BLOBKEY.
115354** (3) Bypass the creation of the sqlite_schema table entry
115355** for the PRIMARY KEY as the primary key index is now
115356** identified by the sqlite_schema table entry of the table itself.
115357** (4) Set the Index.tnum of the PRIMARY KEY Index object in the
115358** schema to the rootpage from the main table.
115359** (5) Add all table columns to the PRIMARY KEY Index object
115360** so that the PRIMARY KEY is a covering index. The surplus
115361** columns are part of KeyInfo.nAllField and are not used for
115362** sorting or lookup or uniqueness checks.
115363** (6) Replace the rowid tail on all automatically generated UNIQUE
115364** indices with the PRIMARY KEY columns.
115365**
115366** For virtual tables, only (1) is performed.
115367*/
115368static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
115369 Index *pIdx;
115370 Index *pPk;
115371 int nPk;
115372 int nExtra;
115373 int i, j;
115374 sqlite3 *db = pParse->db;
115375 Vdbe *v = pParse->pVdbe;
115376
115377 /* Mark every PRIMARY KEY column as NOT NULL (except for imposter tables)
115378 */
115379 if( !db->init.imposterTable ){
115380 for(i=0; i<pTab->nCol; i++){
115381 if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0
115382 && (pTab->aCol[i].notNull==OE_None)
115383 ){
115384 pTab->aCol[i].notNull = OE_Abort;
115385 }
115386 }
115387 pTab->tabFlags |= TF_HasNotNull;
115388 }
115389
115390 /* Convert the P3 operand of the OP_CreateBtree opcode from BTREE_INTKEY
115391 ** into BTREE_BLOBKEY.
115392 */
115393 assert( !pParse->bReturning );
115394 if( pParse->u1.addrCrTab ){
115395 assert( v );
115396 sqlite3VdbeChangeP3(p: v, addr: pParse->u1.addrCrTab, BTREE_BLOBKEY);
115397 }
115398
115399 /* Locate the PRIMARY KEY index. Or, if this table was originally
115400 ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index.
115401 */
115402 if( pTab->iPKey>=0 ){
115403 ExprList *pList;
115404 Token ipkToken;
115405 sqlite3TokenInit(p: &ipkToken, z: pTab->aCol[pTab->iPKey].zCnName);
115406 pList = sqlite3ExprListAppend(pParse, pList: 0,
115407 pExpr: sqlite3ExprAlloc(db, TK_ID, pToken: &ipkToken, dequote: 0));
115408 if( pList==0 ){
115409 pTab->tabFlags &= ~TF_WithoutRowid;
115410 return;
115411 }
115412 if( IN_RENAME_OBJECT ){
115413 sqlite3RenameTokenRemap(pParse, pTo: pList->a[0].pExpr, pFrom: &pTab->iPKey);
115414 }
115415 pList->a[0].sortFlags = pParse->iPkSortOrder;
115416 assert( pParse->pNewTable==pTab );
115417 pTab->iPKey = -1;
115418 sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
115419 SQLITE_IDXTYPE_PRIMARYKEY);
115420 if( db->mallocFailed || pParse->nErr ){
115421 pTab->tabFlags &= ~TF_WithoutRowid;
115422 return;
115423 }
115424 pPk = sqlite3PrimaryKeyIndex(pTab);
115425 assert( pPk->nKeyCol==1 );
115426 }else{
115427 pPk = sqlite3PrimaryKeyIndex(pTab);
115428 assert( pPk!=0 );
115429
115430 /*
115431 ** Remove all redundant columns from the PRIMARY KEY. For example, change
115432 ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)". Later
115433 ** code assumes the PRIMARY KEY contains no repeated columns.
115434 */
115435 for(i=j=1; i<pPk->nKeyCol; i++){
115436 if( isDupColumn(pIdx: pPk, nKey: j, pPk, iCol: i) ){
115437 pPk->nColumn--;
115438 }else{
115439 testcase( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) );
115440 pPk->azColl[j] = pPk->azColl[i];
115441 pPk->aSortOrder[j] = pPk->aSortOrder[i];
115442 pPk->aiColumn[j++] = pPk->aiColumn[i];
115443 }
115444 }
115445 pPk->nKeyCol = j;
115446 }
115447 assert( pPk!=0 );
115448 pPk->isCovering = 1;
115449 if( !db->init.imposterTable ) pPk->uniqNotNull = 1;
115450 nPk = pPk->nColumn = pPk->nKeyCol;
115451
115452 /* Bypass the creation of the PRIMARY KEY btree and the sqlite_schema
115453 ** table entry. This is only required if currently generating VDBE
115454 ** code for a CREATE TABLE (not when parsing one as part of reading
115455 ** a database schema). */
115456 if( v && pPk->tnum>0 ){
115457 assert( db->init.busy==0 );
115458 sqlite3VdbeChangeOpcode(p: v, addr: (int)pPk->tnum, OP_Goto);
115459 }
115460
115461 /* The root page of the PRIMARY KEY is the table root page */
115462 pPk->tnum = pTab->tnum;
115463
115464 /* Update the in-memory representation of all UNIQUE indices by converting
115465 ** the final rowid column into one or more columns of the PRIMARY KEY.
115466 */
115467 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
115468 int n;
115469 if( IsPrimaryKeyIndex(pIdx) ) continue;
115470 for(i=n=0; i<nPk; i++){
115471 if( !isDupColumn(pIdx, nKey: pIdx->nKeyCol, pPk, iCol: i) ){
115472 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
115473 n++;
115474 }
115475 }
115476 if( n==0 ){
115477 /* This index is a superset of the primary key */
115478 pIdx->nColumn = pIdx->nKeyCol;
115479 continue;
115480 }
115481 if( resizeIndexObject(db, pIdx, N: pIdx->nKeyCol+n) ) return;
115482 for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
115483 if( !isDupColumn(pIdx, nKey: pIdx->nKeyCol, pPk, iCol: i) ){
115484 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
115485 pIdx->aiColumn[j] = pPk->aiColumn[i];
115486 pIdx->azColl[j] = pPk->azColl[i];
115487 if( pPk->aSortOrder[i] ){
115488 /* See ticket https://www.sqlite.org/src/info/bba7b69f9849b5bf */
115489 pIdx->bAscKeyBug = 1;
115490 }
115491 j++;
115492 }
115493 }
115494 assert( pIdx->nColumn>=pIdx->nKeyCol+n );
115495 assert( pIdx->nColumn>=j );
115496 }
115497
115498 /* Add all table columns to the PRIMARY KEY index
115499 */
115500 nExtra = 0;
115501 for(i=0; i<pTab->nCol; i++){
115502 if( !hasColumn(aiCol: pPk->aiColumn, nCol: nPk, x: i)
115503 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) nExtra++;
115504 }
115505 if( resizeIndexObject(db, pIdx: pPk, N: nPk+nExtra) ) return;
115506 for(i=0, j=nPk; i<pTab->nCol; i++){
115507 if( !hasColumn(aiCol: pPk->aiColumn, nCol: j, x: i)
115508 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0
115509 ){
115510 assert( j<pPk->nColumn );
115511 pPk->aiColumn[j] = i;
115512 pPk->azColl[j] = sqlite3StrBINARY;
115513 j++;
115514 }
115515 }
115516 assert( pPk->nColumn==j );
115517 assert( pTab->nNVCol<=j );
115518 recomputeColumnsNotIndexed(pIdx: pPk);
115519}
115520
115521
115522#ifndef SQLITE_OMIT_VIRTUALTABLE
115523/*
115524** Return true if pTab is a virtual table and zName is a shadow table name
115525** for that virtual table.
115526*/
115527SQLITE_PRIVATE int sqlite3IsShadowTableOf(sqlite3 *db, Table *pTab, const char *zName){
115528 int nName; /* Length of zName */
115529 Module *pMod; /* Module for the virtual table */
115530
115531 if( !IsVirtual(pTab) ) return 0;
115532 nName = sqlite3Strlen30(z: pTab->zName);
115533 if( sqlite3_strnicmp(zLeft: zName, zRight: pTab->zName, N: nName)!=0 ) return 0;
115534 if( zName[nName]!='_' ) return 0;
115535 pMod = (Module*)sqlite3HashFind(pH: &db->aModule, pKey: pTab->u.vtab.azArg[0]);
115536 if( pMod==0 ) return 0;
115537 if( pMod->pModule->iVersion<3 ) return 0;
115538 if( pMod->pModule->xShadowName==0 ) return 0;
115539 return pMod->pModule->xShadowName(zName+nName+1);
115540}
115541#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
115542
115543#ifndef SQLITE_OMIT_VIRTUALTABLE
115544/*
115545** Table pTab is a virtual table. If it the virtual table implementation
115546** exists and has an xShadowName method, then loop over all other ordinary
115547** tables within the same schema looking for shadow tables of pTab, and mark
115548** any shadow tables seen using the TF_Shadow flag.
115549*/
115550SQLITE_PRIVATE void sqlite3MarkAllShadowTablesOf(sqlite3 *db, Table *pTab){
115551 int nName; /* Length of pTab->zName */
115552 Module *pMod; /* Module for the virtual table */
115553 HashElem *k; /* For looping through the symbol table */
115554
115555 assert( IsVirtual(pTab) );
115556 pMod = (Module*)sqlite3HashFind(pH: &db->aModule, pKey: pTab->u.vtab.azArg[0]);
115557 if( pMod==0 ) return;
115558 if( NEVER(pMod->pModule==0) ) return;
115559 if( pMod->pModule->iVersion<3 ) return;
115560 if( pMod->pModule->xShadowName==0 ) return;
115561 assert( pTab->zName!=0 );
115562 nName = sqlite3Strlen30(z: pTab->zName);
115563 for(k=sqliteHashFirst(&pTab->pSchema->tblHash); k; k=sqliteHashNext(k)){
115564 Table *pOther = sqliteHashData(k);
115565 assert( pOther->zName!=0 );
115566 if( !IsOrdinaryTable(pOther) ) continue;
115567 if( pOther->tabFlags & TF_Shadow ) continue;
115568 if( sqlite3StrNICmp(zLeft: pOther->zName, zRight: pTab->zName, N: nName)==0
115569 && pOther->zName[nName]=='_'
115570 && pMod->pModule->xShadowName(pOther->zName+nName+1)
115571 ){
115572 pOther->tabFlags |= TF_Shadow;
115573 }
115574 }
115575}
115576#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
115577
115578#ifndef SQLITE_OMIT_VIRTUALTABLE
115579/*
115580** Return true if zName is a shadow table name in the current database
115581** connection.
115582**
115583** zName is temporarily modified while this routine is running, but is
115584** restored to its original value prior to this routine returning.
115585*/
115586SQLITE_PRIVATE int sqlite3ShadowTableName(sqlite3 *db, const char *zName){
115587 char *zTail; /* Pointer to the last "_" in zName */
115588 Table *pTab; /* Table that zName is a shadow of */
115589 zTail = strrchr(s: zName, c: '_');
115590 if( zTail==0 ) return 0;
115591 *zTail = 0;
115592 pTab = sqlite3FindTable(db, zName, zDatabase: 0);
115593 *zTail = '_';
115594 if( pTab==0 ) return 0;
115595 if( !IsVirtual(pTab) ) return 0;
115596 return sqlite3IsShadowTableOf(db, pTab, zName);
115597}
115598#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
115599
115600
115601#ifdef SQLITE_DEBUG
115602/*
115603** Mark all nodes of an expression as EP_Immutable, indicating that
115604** they should not be changed. Expressions attached to a table or
115605** index definition are tagged this way to help ensure that we do
115606** not pass them into code generator routines by mistake.
115607*/
115608static int markImmutableExprStep(Walker *pWalker, Expr *pExpr){
115609 ExprSetVVAProperty(pExpr, EP_Immutable);
115610 return WRC_Continue;
115611}
115612static void markExprListImmutable(ExprList *pList){
115613 if( pList ){
115614 Walker w;
115615 memset(&w, 0, sizeof(w));
115616 w.xExprCallback = markImmutableExprStep;
115617 w.xSelectCallback = sqlite3SelectWalkNoop;
115618 w.xSelectCallback2 = 0;
115619 sqlite3WalkExprList(&w, pList);
115620 }
115621}
115622#else
115623#define markExprListImmutable(X) /* no-op */
115624#endif /* SQLITE_DEBUG */
115625
115626
115627/*
115628** This routine is called to report the final ")" that terminates
115629** a CREATE TABLE statement.
115630**
115631** The table structure that other action routines have been building
115632** is added to the internal hash tables, assuming no errors have
115633** occurred.
115634**
115635** An entry for the table is made in the schema table on disk, unless
115636** this is a temporary table or db->init.busy==1. When db->init.busy==1
115637** it means we are reading the sqlite_schema table because we just
115638** connected to the database or because the sqlite_schema table has
115639** recently changed, so the entry for this table already exists in
115640** the sqlite_schema table. We do not want to create it again.
115641**
115642** If the pSelect argument is not NULL, it means that this routine
115643** was called to create a table generated from a
115644** "CREATE TABLE ... AS SELECT ..." statement. The column names of
115645** the new table will match the result set of the SELECT.
115646*/
115647SQLITE_PRIVATE void sqlite3EndTable(
115648 Parse *pParse, /* Parse context */
115649 Token *pCons, /* The ',' token after the last column defn. */
115650 Token *pEnd, /* The ')' before options in the CREATE TABLE */
115651 u32 tabOpts, /* Extra table options. Usually 0. */
115652 Select *pSelect /* Select from a "CREATE ... AS SELECT" */
115653){
115654 Table *p; /* The new table */
115655 sqlite3 *db = pParse->db; /* The database connection */
115656 int iDb; /* Database in which the table lives */
115657 Index *pIdx; /* An implied index of the table */
115658
115659 if( pEnd==0 && pSelect==0 ){
115660 return;
115661 }
115662 p = pParse->pNewTable;
115663 if( p==0 ) return;
115664
115665 if( pSelect==0 && sqlite3ShadowTableName(db, zName: p->zName) ){
115666 p->tabFlags |= TF_Shadow;
115667 }
115668
115669 /* If the db->init.busy is 1 it means we are reading the SQL off the
115670 ** "sqlite_schema" or "sqlite_temp_schema" table on the disk.
115671 ** So do not write to the disk again. Extract the root page number
115672 ** for the table from the db->init.newTnum field. (The page number
115673 ** should have been put there by the sqliteOpenCb routine.)
115674 **
115675 ** If the root page number is 1, that means this is the sqlite_schema
115676 ** table itself. So mark it read-only.
115677 */
115678 if( db->init.busy ){
115679 if( pSelect || (!IsOrdinaryTable(p) && db->init.newTnum) ){
115680 sqlite3ErrorMsg(pParse, zFormat: "");
115681 return;
115682 }
115683 p->tnum = db->init.newTnum;
115684 if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
115685 }
115686
115687 /* Special processing for tables that include the STRICT keyword:
115688 **
115689 ** * Do not allow custom column datatypes. Every column must have
115690 ** a datatype that is one of INT, INTEGER, REAL, TEXT, or BLOB.
115691 **
115692 ** * If a PRIMARY KEY is defined, other than the INTEGER PRIMARY KEY,
115693 ** then all columns of the PRIMARY KEY must have a NOT NULL
115694 ** constraint.
115695 */
115696 if( tabOpts & TF_Strict ){
115697 int ii;
115698 p->tabFlags |= TF_Strict;
115699 for(ii=0; ii<p->nCol; ii++){
115700 Column *pCol = &p->aCol[ii];
115701 if( pCol->eCType==COLTYPE_CUSTOM ){
115702 if( pCol->colFlags & COLFLAG_HASTYPE ){
115703 sqlite3ErrorMsg(pParse,
115704 zFormat: "unknown datatype for %s.%s: \"%s\"",
115705 p->zName, pCol->zCnName, sqlite3ColumnType(pCol, zDflt: "")
115706 );
115707 }else{
115708 sqlite3ErrorMsg(pParse, zFormat: "missing datatype for %s.%s",
115709 p->zName, pCol->zCnName);
115710 }
115711 return;
115712 }else if( pCol->eCType==COLTYPE_ANY ){
115713 pCol->affinity = SQLITE_AFF_BLOB;
115714 }
115715 if( (pCol->colFlags & COLFLAG_PRIMKEY)!=0
115716 && p->iPKey!=ii
115717 && pCol->notNull == OE_None
115718 ){
115719 pCol->notNull = OE_Abort;
115720 p->tabFlags |= TF_HasNotNull;
115721 }
115722 }
115723 }
115724
115725 assert( (p->tabFlags & TF_HasPrimaryKey)==0
115726 || p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 );
115727 assert( (p->tabFlags & TF_HasPrimaryKey)!=0
115728 || (p->iPKey<0 && sqlite3PrimaryKeyIndex(p)==0) );
115729
115730 /* Special processing for WITHOUT ROWID Tables */
115731 if( tabOpts & TF_WithoutRowid ){
115732 if( (p->tabFlags & TF_Autoincrement) ){
115733 sqlite3ErrorMsg(pParse,
115734 zFormat: "AUTOINCREMENT not allowed on WITHOUT ROWID tables");
115735 return;
115736 }
115737 if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
115738 sqlite3ErrorMsg(pParse, zFormat: "PRIMARY KEY missing on table %s", p->zName);
115739 return;
115740 }
115741 p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid;
115742 convertToWithoutRowidTable(pParse, pTab: p);
115743 }
115744 iDb = sqlite3SchemaToIndex(db, p->pSchema);
115745
115746#ifndef SQLITE_OMIT_CHECK
115747 /* Resolve names in all CHECK constraint expressions.
115748 */
115749 if( p->pCheck ){
115750 sqlite3ResolveSelfReference(pParse, pTab: p, NC_IsCheck, pExpr: 0, pList: p->pCheck);
115751 if( pParse->nErr ){
115752 /* If errors are seen, delete the CHECK constraints now, else they might
115753 ** actually be used if PRAGMA writable_schema=ON is set. */
115754 sqlite3ExprListDelete(db, pList: p->pCheck);
115755 p->pCheck = 0;
115756 }else{
115757 markExprListImmutable(p->pCheck);
115758 }
115759 }
115760#endif /* !defined(SQLITE_OMIT_CHECK) */
115761#ifndef SQLITE_OMIT_GENERATED_COLUMNS
115762 if( p->tabFlags & TF_HasGenerated ){
115763 int ii, nNG = 0;
115764 testcase( p->tabFlags & TF_HasVirtual );
115765 testcase( p->tabFlags & TF_HasStored );
115766 for(ii=0; ii<p->nCol; ii++){
115767 u32 colFlags = p->aCol[ii].colFlags;
115768 if( (colFlags & COLFLAG_GENERATED)!=0 ){
115769 Expr *pX = sqlite3ColumnExpr(pTab: p, pCol: &p->aCol[ii]);
115770 testcase( colFlags & COLFLAG_VIRTUAL );
115771 testcase( colFlags & COLFLAG_STORED );
115772 if( sqlite3ResolveSelfReference(pParse, pTab: p, NC_GenCol, pExpr: pX, pList: 0) ){
115773 /* If there are errors in resolving the expression, change the
115774 ** expression to a NULL. This prevents code generators that operate
115775 ** on the expression from inserting extra parts into the expression
115776 ** tree that have been allocated from lookaside memory, which is
115777 ** illegal in a schema and will lead to errors or heap corruption
115778 ** when the database connection closes. */
115779 sqlite3ColumnSetExpr(pParse, pTab: p, pCol: &p->aCol[ii],
115780 pExpr: sqlite3ExprAlloc(db, TK_NULL, pToken: 0, dequote: 0));
115781 }
115782 }else{
115783 nNG++;
115784 }
115785 }
115786 if( nNG==0 ){
115787 sqlite3ErrorMsg(pParse, zFormat: "must have at least one non-generated column");
115788 return;
115789 }
115790 }
115791#endif
115792
115793 /* Estimate the average row size for the table and for all implied indices */
115794 estimateTableWidth(pTab: p);
115795 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
115796 estimateIndexWidth(pIdx);
115797 }
115798
115799 /* If not initializing, then create a record for the new table
115800 ** in the schema table of the database.
115801 **
115802 ** If this is a TEMPORARY table, write the entry into the auxiliary
115803 ** file instead of into the main database file.
115804 */
115805 if( !db->init.busy ){
115806 int n;
115807 Vdbe *v;
115808 char *zType; /* "view" or "table" */
115809 char *zType2; /* "VIEW" or "TABLE" */
115810 char *zStmt; /* Text of the CREATE TABLE or CREATE VIEW statement */
115811
115812 v = sqlite3GetVdbe(pParse);
115813 if( NEVER(v==0) ) return;
115814
115815 sqlite3VdbeAddOp1(p: v, OP_Close, p1: 0);
115816
115817 /*
115818 ** Initialize zType for the new view or table.
115819 */
115820 if( IsOrdinaryTable(p) ){
115821 /* A regular table */
115822 zType = "table";
115823 zType2 = "TABLE";
115824#ifndef SQLITE_OMIT_VIEW
115825 }else{
115826 /* A view */
115827 zType = "view";
115828 zType2 = "VIEW";
115829#endif
115830 }
115831
115832 /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
115833 ** statement to populate the new table. The root-page number for the
115834 ** new table is in register pParse->regRoot.
115835 **
115836 ** Once the SELECT has been coded by sqlite3Select(), it is in a
115837 ** suitable state to query for the column names and types to be used
115838 ** by the new table.
115839 **
115840 ** A shared-cache write-lock is not required to write to the new table,
115841 ** as a schema-lock must have already been obtained to create it. Since
115842 ** a schema-lock excludes all other database users, the write-lock would
115843 ** be redundant.
115844 */
115845 if( pSelect ){
115846 SelectDest dest; /* Where the SELECT should store results */
115847 int regYield; /* Register holding co-routine entry-point */
115848 int addrTop; /* Top of the co-routine */
115849 int regRec; /* A record to be insert into the new table */
115850 int regRowid; /* Rowid of the next row to insert */
115851 int addrInsLoop; /* Top of the loop for inserting rows */
115852 Table *pSelTab; /* A table that describes the SELECT results */
115853
115854 regYield = ++pParse->nMem;
115855 regRec = ++pParse->nMem;
115856 regRowid = ++pParse->nMem;
115857 assert(pParse->nTab==1);
115858 sqlite3MayAbort(pParse);
115859 sqlite3VdbeAddOp3(p: v, OP_OpenWrite, p1: 1, p2: pParse->regRoot, p3: iDb);
115860 sqlite3VdbeChangeP5(p: v, OPFLAG_P2ISREG);
115861 pParse->nTab = 2;
115862 addrTop = sqlite3VdbeCurrentAddr(p: v) + 1;
115863 sqlite3VdbeAddOp3(p: v, OP_InitCoroutine, p1: regYield, p2: 0, p3: addrTop);
115864 if( pParse->nErr ) return;
115865 pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect, SQLITE_AFF_BLOB);
115866 if( pSelTab==0 ) return;
115867 assert( p->aCol==0 );
115868 p->nCol = p->nNVCol = pSelTab->nCol;
115869 p->aCol = pSelTab->aCol;
115870 pSelTab->nCol = 0;
115871 pSelTab->aCol = 0;
115872 sqlite3DeleteTable(db, pTable: pSelTab);
115873 sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
115874 sqlite3Select(pParse, pSelect, &dest);
115875 if( pParse->nErr ) return;
115876 sqlite3VdbeEndCoroutine(v, regYield);
115877 sqlite3VdbeJumpHere(p: v, addr: addrTop - 1);
115878 addrInsLoop = sqlite3VdbeAddOp1(p: v, OP_Yield, p1: dest.iSDParm);
115879 VdbeCoverage(v);
115880 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: dest.iSdst, p2: dest.nSdst, p3: regRec);
115881 sqlite3TableAffinity(v, p, 0);
115882 sqlite3VdbeAddOp2(p: v, OP_NewRowid, p1: 1, p2: regRowid);
115883 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: 1, p2: regRec, p3: regRowid);
115884 sqlite3VdbeGoto(p: v, iDest: addrInsLoop);
115885 sqlite3VdbeJumpHere(p: v, addr: addrInsLoop);
115886 sqlite3VdbeAddOp1(p: v, OP_Close, p1: 1);
115887 }
115888
115889 /* Compute the complete text of the CREATE statement */
115890 if( pSelect ){
115891 zStmt = createTableStmt(db, p);
115892 }else{
115893 Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
115894 n = (int)(pEnd2->z - pParse->sNameToken.z);
115895 if( pEnd2->z[0]!=';' ) n += pEnd2->n;
115896 zStmt = sqlite3MPrintf(db,
115897 zFormat: "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
115898 );
115899 }
115900
115901 /* A slot for the record has already been allocated in the
115902 ** schema table. We just need to update that slot with all
115903 ** the information we've collected.
115904 */
115905 sqlite3NestedParse(pParse,
115906 zFormat: "UPDATE %Q." LEGACY_SCHEMA_TABLE
115907 " SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q"
115908 " WHERE rowid=#%d",
115909 db->aDb[iDb].zDbSName,
115910 zType,
115911 p->zName,
115912 p->zName,
115913 pParse->regRoot,
115914 zStmt,
115915 pParse->regRowid
115916 );
115917 sqlite3DbFree(db, p: zStmt);
115918 sqlite3ChangeCookie(pParse, iDb);
115919
115920#ifndef SQLITE_OMIT_AUTOINCREMENT
115921 /* Check to see if we need to create an sqlite_sequence table for
115922 ** keeping track of autoincrement keys.
115923 */
115924 if( (p->tabFlags & TF_Autoincrement)!=0 && !IN_SPECIAL_PARSE ){
115925 Db *pDb = &db->aDb[iDb];
115926 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
115927 if( pDb->pSchema->pSeqTab==0 ){
115928 sqlite3NestedParse(pParse,
115929 zFormat: "CREATE TABLE %Q.sqlite_sequence(name,seq)",
115930 pDb->zDbSName
115931 );
115932 }
115933 }
115934#endif
115935
115936 /* Reparse everything to update our internal data structures */
115937 sqlite3VdbeAddParseSchemaOp(p: v, iDb,
115938 zWhere: sqlite3MPrintf(db, zFormat: "tbl_name='%q' AND type!='trigger'", p->zName),p5: 0);
115939 }
115940
115941 /* Add the table to the in-memory representation of the database.
115942 */
115943 if( db->init.busy ){
115944 Table *pOld;
115945 Schema *pSchema = p->pSchema;
115946 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
115947 assert( HasRowid(p) || p->iPKey<0 );
115948 pOld = sqlite3HashInsert(pH: &pSchema->tblHash, pKey: p->zName, data: p);
115949 if( pOld ){
115950 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
115951 sqlite3OomFault(db);
115952 return;
115953 }
115954 pParse->pNewTable = 0;
115955 db->mDbFlags |= DBFLAG_SchemaChange;
115956
115957 /* If this is the magic sqlite_sequence table used by autoincrement,
115958 ** then record a pointer to this table in the main database structure
115959 ** so that INSERT can find the table easily. */
115960 assert( !pParse->nested );
115961#ifndef SQLITE_OMIT_AUTOINCREMENT
115962 if( strcmp(s1: p->zName, s2: "sqlite_sequence")==0 ){
115963 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
115964 p->pSchema->pSeqTab = p;
115965 }
115966#endif
115967 }
115968
115969#ifndef SQLITE_OMIT_ALTERTABLE
115970 if( !pSelect && IsOrdinaryTable(p) ){
115971 assert( pCons && pEnd );
115972 if( pCons->z==0 ){
115973 pCons = pEnd;
115974 }
115975 p->u.tab.addColOffset = 13 + (int)(pCons->z - pParse->sNameToken.z);
115976 }
115977#endif
115978}
115979
115980#ifndef SQLITE_OMIT_VIEW
115981/*
115982** The parser calls this routine in order to create a new VIEW
115983*/
115984SQLITE_PRIVATE void sqlite3CreateView(
115985 Parse *pParse, /* The parsing context */
115986 Token *pBegin, /* The CREATE token that begins the statement */
115987 Token *pName1, /* The token that holds the name of the view */
115988 Token *pName2, /* The token that holds the name of the view */
115989 ExprList *pCNames, /* Optional list of view column names */
115990 Select *pSelect, /* A SELECT statement that will become the new view */
115991 int isTemp, /* TRUE for a TEMPORARY view */
115992 int noErr /* Suppress error messages if VIEW already exists */
115993){
115994 Table *p;
115995 int n;
115996 const char *z;
115997 Token sEnd;
115998 DbFixer sFix;
115999 Token *pName = 0;
116000 int iDb;
116001 sqlite3 *db = pParse->db;
116002
116003 if( pParse->nVar>0 ){
116004 sqlite3ErrorMsg(pParse, zFormat: "parameters are not allowed in views");
116005 goto create_view_fail;
116006 }
116007 sqlite3StartTable(pParse, pName1, pName2, isTemp, isView: 1, isVirtual: 0, noErr);
116008 p = pParse->pNewTable;
116009 if( p==0 || pParse->nErr ) goto create_view_fail;
116010
116011 /* Legacy versions of SQLite allowed the use of the magic "rowid" column
116012 ** on a view, even though views do not have rowids. The following flag
116013 ** setting fixes this problem. But the fix can be disabled by compiling
116014 ** with -DSQLITE_ALLOW_ROWID_IN_VIEW in case there are legacy apps that
116015 ** depend upon the old buggy behavior. */
116016#ifndef SQLITE_ALLOW_ROWID_IN_VIEW
116017 p->tabFlags |= TF_NoVisibleRowid;
116018#endif
116019
116020 sqlite3TwoPartName(pParse, pName1, pName2, pUnqual: &pName);
116021 iDb = sqlite3SchemaToIndex(db, p->pSchema);
116022 sqlite3FixInit(pFix: &sFix, pParse, iDb, zType: "view", pName);
116023 if( sqlite3FixSelect(pFix: &sFix, pSelect) ) goto create_view_fail;
116024
116025 /* Make a copy of the entire SELECT statement that defines the view.
116026 ** This will force all the Expr.token.z values to be dynamically
116027 ** allocated rather than point to the input string - which means that
116028 ** they will persist after the current sqlite3_exec() call returns.
116029 */
116030 pSelect->selFlags |= SF_View;
116031 if( IN_RENAME_OBJECT ){
116032 p->u.view.pSelect = pSelect;
116033 pSelect = 0;
116034 }else{
116035 p->u.view.pSelect = sqlite3SelectDup(db, pDup: pSelect, EXPRDUP_REDUCE);
116036 }
116037 p->pCheck = sqlite3ExprListDup(db, p: pCNames, EXPRDUP_REDUCE);
116038 p->eTabType = TABTYP_VIEW;
116039 if( db->mallocFailed ) goto create_view_fail;
116040
116041 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
116042 ** the end.
116043 */
116044 sEnd = pParse->sLastToken;
116045 assert( sEnd.z[0]!=0 || sEnd.n==0 );
116046 if( sEnd.z[0]!=';' ){
116047 sEnd.z += sEnd.n;
116048 }
116049 sEnd.n = 0;
116050 n = (int)(sEnd.z - pBegin->z);
116051 assert( n>0 );
116052 z = pBegin->z;
116053 while( sqlite3Isspace(z[n-1]) ){ n--; }
116054 sEnd.z = &z[n-1];
116055 sEnd.n = 1;
116056
116057 /* Use sqlite3EndTable() to add the view to the schema table */
116058 sqlite3EndTable(pParse, pCons: 0, pEnd: &sEnd, tabOpts: 0, pSelect: 0);
116059
116060create_view_fail:
116061 sqlite3SelectDelete(db, pSelect);
116062 if( IN_RENAME_OBJECT ){
116063 sqlite3RenameExprlistUnmap(pParse, pEList: pCNames);
116064 }
116065 sqlite3ExprListDelete(db, pList: pCNames);
116066 return;
116067}
116068#endif /* SQLITE_OMIT_VIEW */
116069
116070#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
116071/*
116072** The Table structure pTable is really a VIEW. Fill in the names of
116073** the columns of the view in the pTable structure. Return the number
116074** of errors. If an error is seen leave an error message in pParse->zErrMsg.
116075*/
116076SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
116077 Table *pSelTab; /* A fake table from which we get the result set */
116078 Select *pSel; /* Copy of the SELECT that implements the view */
116079 int nErr = 0; /* Number of errors encountered */
116080 int n; /* Temporarily holds the number of cursors assigned */
116081 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
116082#ifndef SQLITE_OMIT_VIRTUALTABLE
116083 int rc;
116084#endif
116085#ifndef SQLITE_OMIT_AUTHORIZATION
116086 sqlite3_xauth xAuth; /* Saved xAuth pointer */
116087#endif
116088
116089 assert( pTable );
116090
116091#ifndef SQLITE_OMIT_VIRTUALTABLE
116092 if( IsVirtual(pTable) ){
116093 db->nSchemaLock++;
116094 rc = sqlite3VtabCallConnect(pParse, pTable);
116095 db->nSchemaLock--;
116096 return rc;
116097 }
116098#endif
116099
116100#ifndef SQLITE_OMIT_VIEW
116101 /* A positive nCol means the columns names for this view are
116102 ** already known.
116103 */
116104 if( pTable->nCol>0 ) return 0;
116105
116106 /* A negative nCol is a special marker meaning that we are currently
116107 ** trying to compute the column names. If we enter this routine with
116108 ** a negative nCol, it means two or more views form a loop, like this:
116109 **
116110 ** CREATE VIEW one AS SELECT * FROM two;
116111 ** CREATE VIEW two AS SELECT * FROM one;
116112 **
116113 ** Actually, the error above is now caught prior to reaching this point.
116114 ** But the following test is still important as it does come up
116115 ** in the following:
116116 **
116117 ** CREATE TABLE main.ex1(a);
116118 ** CREATE TEMP VIEW ex1 AS SELECT a FROM ex1;
116119 ** SELECT * FROM temp.ex1;
116120 */
116121 if( pTable->nCol<0 ){
116122 sqlite3ErrorMsg(pParse, zFormat: "view %s is circularly defined", pTable->zName);
116123 return 1;
116124 }
116125 assert( pTable->nCol>=0 );
116126
116127 /* If we get this far, it means we need to compute the table names.
116128 ** Note that the call to sqlite3ResultSetOfSelect() will expand any
116129 ** "*" elements in the results set of the view and will assign cursors
116130 ** to the elements of the FROM clause. But we do not want these changes
116131 ** to be permanent. So the computation is done on a copy of the SELECT
116132 ** statement that defines the view.
116133 */
116134 assert( IsView(pTable) );
116135 pSel = sqlite3SelectDup(db, pDup: pTable->u.view.pSelect, flags: 0);
116136 if( pSel ){
116137 u8 eParseMode = pParse->eParseMode;
116138 pParse->eParseMode = PARSE_MODE_NORMAL;
116139 n = pParse->nTab;
116140 sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
116141 pTable->nCol = -1;
116142 DisableLookaside;
116143#ifndef SQLITE_OMIT_AUTHORIZATION
116144 xAuth = db->xAuth;
116145 db->xAuth = 0;
116146 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE);
116147 db->xAuth = xAuth;
116148#else
116149 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE);
116150#endif
116151 pParse->nTab = n;
116152 if( pSelTab==0 ){
116153 pTable->nCol = 0;
116154 nErr++;
116155 }else if( pTable->pCheck ){
116156 /* CREATE VIEW name(arglist) AS ...
116157 ** The names of the columns in the table are taken from
116158 ** arglist which is stored in pTable->pCheck. The pCheck field
116159 ** normally holds CHECK constraints on an ordinary table, but for
116160 ** a VIEW it holds the list of column names.
116161 */
116162 sqlite3ColumnsFromExprList(pParse, pTable->pCheck,
116163 &pTable->nCol, &pTable->aCol);
116164 if( db->mallocFailed==0
116165 && pParse->nErr==0
116166 && pTable->nCol==pSel->pEList->nExpr
116167 ){
116168 sqlite3SelectAddColumnTypeAndCollation(pParse, pTable, pSel,
116169 SQLITE_AFF_NONE);
116170 }
116171 }else{
116172 /* CREATE VIEW name AS... without an argument list. Construct
116173 ** the column names from the SELECT statement that defines the view.
116174 */
116175 assert( pTable->aCol==0 );
116176 pTable->nCol = pSelTab->nCol;
116177 pTable->aCol = pSelTab->aCol;
116178 pTable->tabFlags |= (pSelTab->tabFlags & COLFLAG_NOINSERT);
116179 pSelTab->nCol = 0;
116180 pSelTab->aCol = 0;
116181 assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
116182 }
116183 pTable->nNVCol = pTable->nCol;
116184 sqlite3DeleteTable(db, pTable: pSelTab);
116185 sqlite3SelectDelete(db, pSel);
116186 EnableLookaside;
116187 pParse->eParseMode = eParseMode;
116188 } else {
116189 nErr++;
116190 }
116191 pTable->pSchema->schemaFlags |= DB_UnresetViews;
116192 if( db->mallocFailed ){
116193 sqlite3DeleteColumnNames(db, pTable);
116194 }
116195#endif /* SQLITE_OMIT_VIEW */
116196 return nErr;
116197}
116198#endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
116199
116200#ifndef SQLITE_OMIT_VIEW
116201/*
116202** Clear the column names from every VIEW in database idx.
116203*/
116204static void sqliteViewResetAll(sqlite3 *db, int idx){
116205 HashElem *i;
116206 assert( sqlite3SchemaMutexHeld(db, idx, 0) );
116207 if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
116208 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
116209 Table *pTab = sqliteHashData(i);
116210 if( IsView(pTab) ){
116211 sqlite3DeleteColumnNames(db, pTable: pTab);
116212 }
116213 }
116214 DbClearProperty(db, idx, DB_UnresetViews);
116215}
116216#else
116217# define sqliteViewResetAll(A,B)
116218#endif /* SQLITE_OMIT_VIEW */
116219
116220/*
116221** This function is called by the VDBE to adjust the internal schema
116222** used by SQLite when the btree layer moves a table root page. The
116223** root-page of a table or index in database iDb has changed from iFrom
116224** to iTo.
116225**
116226** Ticket #1728: The symbol table might still contain information
116227** on tables and/or indices that are the process of being deleted.
116228** If you are unlucky, one of those deleted indices or tables might
116229** have the same rootpage number as the real table or index that is
116230** being moved. So we cannot stop searching after the first match
116231** because the first match might be for one of the deleted indices
116232** or tables and not the table/index that is actually being moved.
116233** We must continue looping until all tables and indices with
116234** rootpage==iFrom have been converted to have a rootpage of iTo
116235** in order to be certain that we got the right one.
116236*/
116237#ifndef SQLITE_OMIT_AUTOVACUUM
116238SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3 *db, int iDb, Pgno iFrom, Pgno iTo){
116239 HashElem *pElem;
116240 Hash *pHash;
116241 Db *pDb;
116242
116243 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
116244 pDb = &db->aDb[iDb];
116245 pHash = &pDb->pSchema->tblHash;
116246 for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
116247 Table *pTab = sqliteHashData(pElem);
116248 if( pTab->tnum==iFrom ){
116249 pTab->tnum = iTo;
116250 }
116251 }
116252 pHash = &pDb->pSchema->idxHash;
116253 for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
116254 Index *pIdx = sqliteHashData(pElem);
116255 if( pIdx->tnum==iFrom ){
116256 pIdx->tnum = iTo;
116257 }
116258 }
116259}
116260#endif
116261
116262/*
116263** Write code to erase the table with root-page iTable from database iDb.
116264** Also write code to modify the sqlite_schema table and internal schema
116265** if a root-page of another table is moved by the btree-layer whilst
116266** erasing iTable (this can happen with an auto-vacuum database).
116267*/
116268static void destroyRootPage(Parse *pParse, int iTable, int iDb){
116269 Vdbe *v = sqlite3GetVdbe(pParse);
116270 int r1 = sqlite3GetTempReg(pParse);
116271 if( iTable<2 ) sqlite3ErrorMsg(pParse, zFormat: "corrupt schema");
116272 sqlite3VdbeAddOp3(p: v, OP_Destroy, p1: iTable, p2: r1, p3: iDb);
116273 sqlite3MayAbort(pParse);
116274#ifndef SQLITE_OMIT_AUTOVACUUM
116275 /* OP_Destroy stores an in integer r1. If this integer
116276 ** is non-zero, then it is the root page number of a table moved to
116277 ** location iTable. The following code modifies the sqlite_schema table to
116278 ** reflect this.
116279 **
116280 ** The "#NNN" in the SQL is a special constant that means whatever value
116281 ** is in register NNN. See grammar rules associated with the TK_REGISTER
116282 ** token for additional information.
116283 */
116284 sqlite3NestedParse(pParse,
116285 zFormat: "UPDATE %Q." LEGACY_SCHEMA_TABLE
116286 " SET rootpage=%d WHERE #%d AND rootpage=#%d",
116287 pParse->db->aDb[iDb].zDbSName, iTable, r1, r1);
116288#endif
116289 sqlite3ReleaseTempReg(pParse, iReg: r1);
116290}
116291
116292/*
116293** Write VDBE code to erase table pTab and all associated indices on disk.
116294** Code to update the sqlite_schema tables and internal schema definitions
116295** in case a root-page belonging to another table is moved by the btree layer
116296** is also added (this can happen with an auto-vacuum database).
116297*/
116298static void destroyTable(Parse *pParse, Table *pTab){
116299 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
116300 ** is not defined), then it is important to call OP_Destroy on the
116301 ** table and index root-pages in order, starting with the numerically
116302 ** largest root-page number. This guarantees that none of the root-pages
116303 ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
116304 ** following were coded:
116305 **
116306 ** OP_Destroy 4 0
116307 ** ...
116308 ** OP_Destroy 5 0
116309 **
116310 ** and root page 5 happened to be the largest root-page number in the
116311 ** database, then root page 5 would be moved to page 4 by the
116312 ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
116313 ** a free-list page.
116314 */
116315 Pgno iTab = pTab->tnum;
116316 Pgno iDestroyed = 0;
116317
116318 while( 1 ){
116319 Index *pIdx;
116320 Pgno iLargest = 0;
116321
116322 if( iDestroyed==0 || iTab<iDestroyed ){
116323 iLargest = iTab;
116324 }
116325 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
116326 Pgno iIdx = pIdx->tnum;
116327 assert( pIdx->pSchema==pTab->pSchema );
116328 if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
116329 iLargest = iIdx;
116330 }
116331 }
116332 if( iLargest==0 ){
116333 return;
116334 }else{
116335 int iDb = sqlite3SchemaToIndex(db: pParse->db, pTab->pSchema);
116336 assert( iDb>=0 && iDb<pParse->db->nDb );
116337 destroyRootPage(pParse, iTable: iLargest, iDb);
116338 iDestroyed = iLargest;
116339 }
116340 }
116341}
116342
116343/*
116344** Remove entries from the sqlite_statN tables (for N in (1,2,3))
116345** after a DROP INDEX or DROP TABLE command.
116346*/
116347static void sqlite3ClearStatTables(
116348 Parse *pParse, /* The parsing context */
116349 int iDb, /* The database number */
116350 const char *zType, /* "idx" or "tbl" */
116351 const char *zName /* Name of index or table */
116352){
116353 int i;
116354 const char *zDbName = pParse->db->aDb[iDb].zDbSName;
116355 for(i=1; i<=4; i++){
116356 char zTab[24];
116357 sqlite3_snprintf(n: sizeof(zTab),zBuf: zTab,zFormat: "sqlite_stat%d",i);
116358 if( sqlite3FindTable(db: pParse->db, zName: zTab, zDatabase: zDbName) ){
116359 sqlite3NestedParse(pParse,
116360 zFormat: "DELETE FROM %Q.%s WHERE %s=%Q",
116361 zDbName, zTab, zType, zName
116362 );
116363 }
116364 }
116365}
116366
116367/*
116368** Generate code to drop a table.
116369*/
116370SQLITE_PRIVATE void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){
116371 Vdbe *v;
116372 sqlite3 *db = pParse->db;
116373 Trigger *pTrigger;
116374 Db *pDb = &db->aDb[iDb];
116375
116376 v = sqlite3GetVdbe(pParse);
116377 assert( v!=0 );
116378 sqlite3BeginWriteOperation(pParse, 1, iDb);
116379
116380#ifndef SQLITE_OMIT_VIRTUALTABLE
116381 if( IsVirtual(pTab) ){
116382 sqlite3VdbeAddOp0(p: v, OP_VBegin);
116383 }
116384#endif
116385
116386 /* Drop all triggers associated with the table being dropped. Code
116387 ** is generated to remove entries from sqlite_schema and/or
116388 ** sqlite_temp_schema if required.
116389 */
116390 pTrigger = sqlite3TriggerList(pParse, pTab);
116391 while( pTrigger ){
116392 assert( pTrigger->pSchema==pTab->pSchema ||
116393 pTrigger->pSchema==db->aDb[1].pSchema );
116394 sqlite3DropTriggerPtr(pParse, pTrigger);
116395 pTrigger = pTrigger->pNext;
116396 }
116397
116398#ifndef SQLITE_OMIT_AUTOINCREMENT
116399 /* Remove any entries of the sqlite_sequence table associated with
116400 ** the table being dropped. This is done before the table is dropped
116401 ** at the btree level, in case the sqlite_sequence table needs to
116402 ** move as a result of the drop (can happen in auto-vacuum mode).
116403 */
116404 if( pTab->tabFlags & TF_Autoincrement ){
116405 sqlite3NestedParse(pParse,
116406 zFormat: "DELETE FROM %Q.sqlite_sequence WHERE name=%Q",
116407 pDb->zDbSName, pTab->zName
116408 );
116409 }
116410#endif
116411
116412 /* Drop all entries in the schema table that refer to the
116413 ** table. The program name loops through the schema table and deletes
116414 ** every row that refers to a table of the same name as the one being
116415 ** dropped. Triggers are handled separately because a trigger can be
116416 ** created in the temp database that refers to a table in another
116417 ** database.
116418 */
116419 sqlite3NestedParse(pParse,
116420 zFormat: "DELETE FROM %Q." LEGACY_SCHEMA_TABLE
116421 " WHERE tbl_name=%Q and type!='trigger'",
116422 pDb->zDbSName, pTab->zName);
116423 if( !isView && !IsVirtual(pTab) ){
116424 destroyTable(pParse, pTab);
116425 }
116426
116427 /* Remove the table entry from SQLite's internal schema and modify
116428 ** the schema cookie.
116429 */
116430 if( IsVirtual(pTab) ){
116431 sqlite3VdbeAddOp4(p: v, OP_VDestroy, p1: iDb, p2: 0, p3: 0, zP4: pTab->zName, p4type: 0);
116432 sqlite3MayAbort(pParse);
116433 }
116434 sqlite3VdbeAddOp4(p: v, OP_DropTable, p1: iDb, p2: 0, p3: 0, zP4: pTab->zName, p4type: 0);
116435 sqlite3ChangeCookie(pParse, iDb);
116436 sqliteViewResetAll(db, idx: iDb);
116437}
116438
116439/*
116440** Return TRUE if shadow tables should be read-only in the current
116441** context.
116442*/
116443SQLITE_PRIVATE int sqlite3ReadOnlyShadowTables(sqlite3 *db){
116444#ifndef SQLITE_OMIT_VIRTUALTABLE
116445 if( (db->flags & SQLITE_Defensive)!=0
116446 && db->pVtabCtx==0
116447 && db->nVdbeExec==0
116448 && !sqlite3VtabInSync(db)
116449 ){
116450 return 1;
116451 }
116452#endif
116453 return 0;
116454}
116455
116456/*
116457** Return true if it is not allowed to drop the given table
116458*/
116459static int tableMayNotBeDropped(sqlite3 *db, Table *pTab){
116460 if( sqlite3StrNICmp(zLeft: pTab->zName, zRight: "sqlite_", N: 7)==0 ){
116461 if( sqlite3StrNICmp(zLeft: pTab->zName+7, zRight: "stat", N: 4)==0 ) return 0;
116462 if( sqlite3StrNICmp(zLeft: pTab->zName+7, zRight: "parameters", N: 10)==0 ) return 0;
116463 return 1;
116464 }
116465 if( (pTab->tabFlags & TF_Shadow)!=0 && sqlite3ReadOnlyShadowTables(db) ){
116466 return 1;
116467 }
116468 if( pTab->tabFlags & TF_Eponymous ){
116469 return 1;
116470 }
116471 return 0;
116472}
116473
116474/*
116475** This routine is called to do the work of a DROP TABLE statement.
116476** pName is the name of the table to be dropped.
116477*/
116478SQLITE_PRIVATE void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
116479 Table *pTab;
116480 Vdbe *v;
116481 sqlite3 *db = pParse->db;
116482 int iDb;
116483
116484 if( db->mallocFailed ){
116485 goto exit_drop_table;
116486 }
116487 assert( pParse->nErr==0 );
116488 assert( pName->nSrc==1 );
116489 if( sqlite3ReadSchema(pParse) ) goto exit_drop_table;
116490 if( noErr ) db->suppressErr++;
116491 assert( isView==0 || isView==LOCATE_VIEW );
116492 pTab = sqlite3LocateTableItem(pParse, flags: isView, p: &pName->a[0]);
116493 if( noErr ) db->suppressErr--;
116494
116495 if( pTab==0 ){
116496 if( noErr ){
116497 sqlite3CodeVerifyNamedSchema(pParse, zDb: pName->a[0].zDatabase);
116498 sqlite3ForceNotReadOnly(pParse);
116499 }
116500 goto exit_drop_table;
116501 }
116502 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
116503 assert( iDb>=0 && iDb<db->nDb );
116504
116505 /* If pTab is a virtual table, call ViewGetColumnNames() to ensure
116506 ** it is initialized.
116507 */
116508 if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTable: pTab) ){
116509 goto exit_drop_table;
116510 }
116511#ifndef SQLITE_OMIT_AUTHORIZATION
116512 {
116513 int code;
116514 const char *zTab = SCHEMA_TABLE(iDb);
116515 const char *zDb = db->aDb[iDb].zDbSName;
116516 const char *zArg2 = 0;
116517 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zArg1: zTab, zArg2: 0, zArg3: zDb)){
116518 goto exit_drop_table;
116519 }
116520 if( isView ){
116521 if( !OMIT_TEMPDB && iDb==1 ){
116522 code = SQLITE_DROP_TEMP_VIEW;
116523 }else{
116524 code = SQLITE_DROP_VIEW;
116525 }
116526#ifndef SQLITE_OMIT_VIRTUALTABLE
116527 }else if( IsVirtual(pTab) ){
116528 code = SQLITE_DROP_VTABLE;
116529 zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
116530#endif
116531 }else{
116532 if( !OMIT_TEMPDB && iDb==1 ){
116533 code = SQLITE_DROP_TEMP_TABLE;
116534 }else{
116535 code = SQLITE_DROP_TABLE;
116536 }
116537 }
116538 if( sqlite3AuthCheck(pParse, code, zArg1: pTab->zName, zArg2, zArg3: zDb) ){
116539 goto exit_drop_table;
116540 }
116541 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zArg1: pTab->zName, zArg2: 0, zArg3: zDb) ){
116542 goto exit_drop_table;
116543 }
116544 }
116545#endif
116546 if( tableMayNotBeDropped(db, pTab) ){
116547 sqlite3ErrorMsg(pParse, zFormat: "table %s may not be dropped", pTab->zName);
116548 goto exit_drop_table;
116549 }
116550
116551#ifndef SQLITE_OMIT_VIEW
116552 /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
116553 ** on a table.
116554 */
116555 if( isView && !IsView(pTab) ){
116556 sqlite3ErrorMsg(pParse, zFormat: "use DROP TABLE to delete table %s", pTab->zName);
116557 goto exit_drop_table;
116558 }
116559 if( !isView && IsView(pTab) ){
116560 sqlite3ErrorMsg(pParse, zFormat: "use DROP VIEW to delete view %s", pTab->zName);
116561 goto exit_drop_table;
116562 }
116563#endif
116564
116565 /* Generate code to remove the table from the schema table
116566 ** on disk.
116567 */
116568 v = sqlite3GetVdbe(pParse);
116569 if( v ){
116570 sqlite3BeginWriteOperation(pParse, 1, iDb);
116571 if( !isView ){
116572 sqlite3ClearStatTables(pParse, iDb, zType: "tbl", zName: pTab->zName);
116573 sqlite3FkDropTable(pParse, pName, pTab);
116574 }
116575 sqlite3CodeDropTable(pParse, pTab, iDb, isView);
116576 }
116577
116578exit_drop_table:
116579 sqlite3SrcListDelete(db, pName);
116580}
116581
116582/*
116583** This routine is called to create a new foreign key on the table
116584** currently under construction. pFromCol determines which columns
116585** in the current table point to the foreign key. If pFromCol==0 then
116586** connect the key to the last column inserted. pTo is the name of
116587** the table referred to (a.k.a the "parent" table). pToCol is a list
116588** of tables in the parent pTo table. flags contains all
116589** information about the conflict resolution algorithms specified
116590** in the ON DELETE, ON UPDATE and ON INSERT clauses.
116591**
116592** An FKey structure is created and added to the table currently
116593** under construction in the pParse->pNewTable field.
116594**
116595** The foreign key is set for IMMEDIATE processing. A subsequent call
116596** to sqlite3DeferForeignKey() might change this to DEFERRED.
116597*/
116598SQLITE_PRIVATE void sqlite3CreateForeignKey(
116599 Parse *pParse, /* Parsing context */
116600 ExprList *pFromCol, /* Columns in this table that point to other table */
116601 Token *pTo, /* Name of the other table */
116602 ExprList *pToCol, /* Columns in the other table */
116603 int flags /* Conflict resolution algorithms. */
116604){
116605 sqlite3 *db = pParse->db;
116606#ifndef SQLITE_OMIT_FOREIGN_KEY
116607 FKey *pFKey = 0;
116608 FKey *pNextTo;
116609 Table *p = pParse->pNewTable;
116610 i64 nByte;
116611 int i;
116612 int nCol;
116613 char *z;
116614
116615 assert( pTo!=0 );
116616 if( p==0 || IN_DECLARE_VTAB ) goto fk_end;
116617 if( pFromCol==0 ){
116618 int iCol = p->nCol-1;
116619 if( NEVER(iCol<0) ) goto fk_end;
116620 if( pToCol && pToCol->nExpr!=1 ){
116621 sqlite3ErrorMsg(pParse, zFormat: "foreign key on %s"
116622 " should reference only one column of table %T",
116623 p->aCol[iCol].zCnName, pTo);
116624 goto fk_end;
116625 }
116626 nCol = 1;
116627 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
116628 sqlite3ErrorMsg(pParse,
116629 zFormat: "number of columns in foreign key does not match the number of "
116630 "columns in the referenced table");
116631 goto fk_end;
116632 }else{
116633 nCol = pFromCol->nExpr;
116634 }
116635 nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
116636 if( pToCol ){
116637 for(i=0; i<pToCol->nExpr; i++){
116638 nByte += sqlite3Strlen30(z: pToCol->a[i].zEName) + 1;
116639 }
116640 }
116641 pFKey = sqlite3DbMallocZero(db, n: nByte );
116642 if( pFKey==0 ){
116643 goto fk_end;
116644 }
116645 pFKey->pFrom = p;
116646 assert( IsOrdinaryTable(p) );
116647 pFKey->pNextFrom = p->u.tab.pFKey;
116648 z = (char*)&pFKey->aCol[nCol];
116649 pFKey->zTo = z;
116650 if( IN_RENAME_OBJECT ){
116651 sqlite3RenameTokenMap(pParse, pPtr: (void*)z, pToken: pTo);
116652 }
116653 memcpy(dest: z, src: pTo->z, n: pTo->n);
116654 z[pTo->n] = 0;
116655 sqlite3Dequote(z);
116656 z += pTo->n+1;
116657 pFKey->nCol = nCol;
116658 if( pFromCol==0 ){
116659 pFKey->aCol[0].iFrom = p->nCol-1;
116660 }else{
116661 for(i=0; i<nCol; i++){
116662 int j;
116663 for(j=0; j<p->nCol; j++){
116664 if( sqlite3StrICmp(zLeft: p->aCol[j].zCnName, zRight: pFromCol->a[i].zEName)==0 ){
116665 pFKey->aCol[i].iFrom = j;
116666 break;
116667 }
116668 }
116669 if( j>=p->nCol ){
116670 sqlite3ErrorMsg(pParse,
116671 zFormat: "unknown column \"%s\" in foreign key definition",
116672 pFromCol->a[i].zEName);
116673 goto fk_end;
116674 }
116675 if( IN_RENAME_OBJECT ){
116676 sqlite3RenameTokenRemap(pParse, pTo: &pFKey->aCol[i], pFrom: pFromCol->a[i].zEName);
116677 }
116678 }
116679 }
116680 if( pToCol ){
116681 for(i=0; i<nCol; i++){
116682 int n = sqlite3Strlen30(z: pToCol->a[i].zEName);
116683 pFKey->aCol[i].zCol = z;
116684 if( IN_RENAME_OBJECT ){
116685 sqlite3RenameTokenRemap(pParse, pTo: z, pFrom: pToCol->a[i].zEName);
116686 }
116687 memcpy(dest: z, src: pToCol->a[i].zEName, n: n);
116688 z[n] = 0;
116689 z += n+1;
116690 }
116691 }
116692 pFKey->isDeferred = 0;
116693 pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */
116694 pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */
116695
116696 assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
116697 pNextTo = (FKey *)sqlite3HashInsert(pH: &p->pSchema->fkeyHash,
116698 pKey: pFKey->zTo, data: (void *)pFKey
116699 );
116700 if( pNextTo==pFKey ){
116701 sqlite3OomFault(db);
116702 goto fk_end;
116703 }
116704 if( pNextTo ){
116705 assert( pNextTo->pPrevTo==0 );
116706 pFKey->pNextTo = pNextTo;
116707 pNextTo->pPrevTo = pFKey;
116708 }
116709
116710 /* Link the foreign key to the table as the last step.
116711 */
116712 assert( IsOrdinaryTable(p) );
116713 p->u.tab.pFKey = pFKey;
116714 pFKey = 0;
116715
116716fk_end:
116717 sqlite3DbFree(db, p: pFKey);
116718#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
116719 sqlite3ExprListDelete(db, pList: pFromCol);
116720 sqlite3ExprListDelete(db, pList: pToCol);
116721}
116722
116723/*
116724** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
116725** clause is seen as part of a foreign key definition. The isDeferred
116726** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
116727** The behavior of the most recently created foreign key is adjusted
116728** accordingly.
116729*/
116730SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
116731#ifndef SQLITE_OMIT_FOREIGN_KEY
116732 Table *pTab;
116733 FKey *pFKey;
116734 if( (pTab = pParse->pNewTable)==0 ) return;
116735 if( NEVER(!IsOrdinaryTable(pTab)) ) return;
116736 if( (pFKey = pTab->u.tab.pFKey)==0 ) return;
116737 assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
116738 pFKey->isDeferred = (u8)isDeferred;
116739#endif
116740}
116741
116742/*
116743** Generate code that will erase and refill index *pIdx. This is
116744** used to initialize a newly created index or to recompute the
116745** content of an index in response to a REINDEX command.
116746**
116747** if memRootPage is not negative, it means that the index is newly
116748** created. The register specified by memRootPage contains the
116749** root page number of the index. If memRootPage is negative, then
116750** the index already exists and must be cleared before being refilled and
116751** the root page number of the index is taken from pIndex->tnum.
116752*/
116753static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
116754 Table *pTab = pIndex->pTable; /* The table that is indexed */
116755 int iTab = pParse->nTab++; /* Btree cursor used for pTab */
116756 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
116757 int iSorter; /* Cursor opened by OpenSorter (if in use) */
116758 int addr1; /* Address of top of loop */
116759 int addr2; /* Address to jump to for next iteration */
116760 Pgno tnum; /* Root page of index */
116761 int iPartIdxLabel; /* Jump to this label to skip a row */
116762 Vdbe *v; /* Generate code into this virtual machine */
116763 KeyInfo *pKey; /* KeyInfo for index */
116764 int regRecord; /* Register holding assembled index record */
116765 sqlite3 *db = pParse->db; /* The database connection */
116766 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
116767
116768#ifndef SQLITE_OMIT_AUTHORIZATION
116769 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, zArg1: pIndex->zName, zArg2: 0,
116770 zArg3: db->aDb[iDb].zDbSName ) ){
116771 return;
116772 }
116773#endif
116774
116775 /* Require a write-lock on the table to perform this operation */
116776 sqlite3TableLock(pParse, iDb, iTab: pTab->tnum, isWriteLock: 1, zName: pTab->zName);
116777
116778 v = sqlite3GetVdbe(pParse);
116779 if( v==0 ) return;
116780 if( memRootPage>=0 ){
116781 tnum = (Pgno)memRootPage;
116782 }else{
116783 tnum = pIndex->tnum;
116784 }
116785 pKey = sqlite3KeyInfoOfIndex(pParse, pIndex);
116786 assert( pKey!=0 || db->mallocFailed || pParse->nErr );
116787
116788 /* Open the sorter cursor if we are to use one. */
116789 iSorter = pParse->nTab++;
116790 sqlite3VdbeAddOp4(p: v, OP_SorterOpen, p1: iSorter, p2: 0, p3: pIndex->nKeyCol, zP4: (char*)
116791 sqlite3KeyInfoRef(pKey), P4_KEYINFO);
116792
116793 /* Open the table. Loop through all rows of the table, inserting index
116794 ** records into the sorter. */
116795 sqlite3OpenTable(pParse, iCur: iTab, iDb, pTab, OP_OpenRead);
116796 addr1 = sqlite3VdbeAddOp2(p: v, OP_Rewind, p1: iTab, p2: 0); VdbeCoverage(v);
116797 regRecord = sqlite3GetTempReg(pParse);
116798 sqlite3MultiWrite(pParse);
116799
116800 sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0);
116801 sqlite3VdbeAddOp2(p: v, OP_SorterInsert, p1: iSorter, p2: regRecord);
116802 sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
116803 sqlite3VdbeAddOp2(p: v, OP_Next, p1: iTab, p2: addr1+1); VdbeCoverage(v);
116804 sqlite3VdbeJumpHere(p: v, addr: addr1);
116805 if( memRootPage<0 ) sqlite3VdbeAddOp2(p: v, OP_Clear, p1: tnum, p2: iDb);
116806 sqlite3VdbeAddOp4(p: v, OP_OpenWrite, p1: iIdx, p2: (int)tnum, p3: iDb,
116807 zP4: (char *)pKey, P4_KEYINFO);
116808 sqlite3VdbeChangeP5(p: v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
116809
116810 addr1 = sqlite3VdbeAddOp2(p: v, OP_SorterSort, p1: iSorter, p2: 0); VdbeCoverage(v);
116811 if( IsUniqueIndex(pIndex) ){
116812 int j2 = sqlite3VdbeGoto(p: v, iDest: 1);
116813 addr2 = sqlite3VdbeCurrentAddr(p: v);
116814 sqlite3VdbeVerifyAbortable(v, OE_Abort);
116815 sqlite3VdbeAddOp4Int(p: v, OP_SorterCompare, p1: iSorter, p2: j2, p3: regRecord,
116816 p4: pIndex->nKeyCol); VdbeCoverage(v);
116817 sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
116818 sqlite3VdbeJumpHere(p: v, addr: j2);
116819 }else{
116820 /* Most CREATE INDEX and REINDEX statements that are not UNIQUE can not
116821 ** abort. The exception is if one of the indexed expressions contains a
116822 ** user function that throws an exception when it is evaluated. But the
116823 ** overhead of adding a statement journal to a CREATE INDEX statement is
116824 ** very small (since most of the pages written do not contain content that
116825 ** needs to be restored if the statement aborts), so we call
116826 ** sqlite3MayAbort() for all CREATE INDEX statements. */
116827 sqlite3MayAbort(pParse);
116828 addr2 = sqlite3VdbeCurrentAddr(p: v);
116829 }
116830 sqlite3VdbeAddOp3(p: v, OP_SorterData, p1: iSorter, p2: regRecord, p3: iIdx);
116831 if( !pIndex->bAscKeyBug ){
116832 /* This OP_SeekEnd opcode makes index insert for a REINDEX go much
116833 ** faster by avoiding unnecessary seeks. But the optimization does
116834 ** not work for UNIQUE constraint indexes on WITHOUT ROWID tables
116835 ** with DESC primary keys, since those indexes have there keys in
116836 ** a different order from the main table.
116837 ** See ticket: https://www.sqlite.org/src/info/bba7b69f9849b5bf
116838 */
116839 sqlite3VdbeAddOp1(p: v, OP_SeekEnd, p1: iIdx);
116840 }
116841 sqlite3VdbeAddOp2(p: v, OP_IdxInsert, p1: iIdx, p2: regRecord);
116842 sqlite3VdbeChangeP5(p: v, OPFLAG_USESEEKRESULT);
116843 sqlite3ReleaseTempReg(pParse, iReg: regRecord);
116844 sqlite3VdbeAddOp2(p: v, OP_SorterNext, p1: iSorter, p2: addr2); VdbeCoverage(v);
116845 sqlite3VdbeJumpHere(p: v, addr: addr1);
116846
116847 sqlite3VdbeAddOp1(p: v, OP_Close, p1: iTab);
116848 sqlite3VdbeAddOp1(p: v, OP_Close, p1: iIdx);
116849 sqlite3VdbeAddOp1(p: v, OP_Close, p1: iSorter);
116850}
116851
116852/*
116853** Allocate heap space to hold an Index object with nCol columns.
116854**
116855** Increase the allocation size to provide an extra nExtra bytes
116856** of 8-byte aligned space after the Index object and return a
116857** pointer to this extra space in *ppExtra.
116858*/
116859SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(
116860 sqlite3 *db, /* Database connection */
116861 i16 nCol, /* Total number of columns in the index */
116862 int nExtra, /* Number of bytes of extra space to alloc */
116863 char **ppExtra /* Pointer to the "extra" space */
116864){
116865 Index *p; /* Allocated index object */
116866 int nByte; /* Bytes of space for Index object + arrays */
116867
116868 nByte = ROUND8(sizeof(Index)) + /* Index structure */
116869 ROUND8(sizeof(char*)*nCol) + /* Index.azColl */
116870 ROUND8(sizeof(LogEst)*(nCol+1) + /* Index.aiRowLogEst */
116871 sizeof(i16)*nCol + /* Index.aiColumn */
116872 sizeof(u8)*nCol); /* Index.aSortOrder */
116873 p = sqlite3DbMallocZero(db, n: nByte + nExtra);
116874 if( p ){
116875 char *pExtra = ((char*)p)+ROUND8(sizeof(Index));
116876 p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol);
116877 p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
116878 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol;
116879 p->aSortOrder = (u8*)pExtra;
116880 p->nColumn = nCol;
116881 p->nKeyCol = nCol - 1;
116882 *ppExtra = ((char*)p) + nByte;
116883 }
116884 return p;
116885}
116886
116887/*
116888** If expression list pList contains an expression that was parsed with
116889** an explicit "NULLS FIRST" or "NULLS LAST" clause, leave an error in
116890** pParse and return non-zero. Otherwise, return zero.
116891*/
116892SQLITE_PRIVATE int sqlite3HasExplicitNulls(Parse *pParse, ExprList *pList){
116893 if( pList ){
116894 int i;
116895 for(i=0; i<pList->nExpr; i++){
116896 if( pList->a[i].bNulls ){
116897 u8 sf = pList->a[i].sortFlags;
116898 sqlite3ErrorMsg(pParse, zFormat: "unsupported use of NULLS %s",
116899 (sf==0 || sf==3) ? "FIRST" : "LAST"
116900 );
116901 return 1;
116902 }
116903 }
116904 }
116905 return 0;
116906}
116907
116908/*
116909** Create a new index for an SQL table. pName1.pName2 is the name of the index
116910** and pTblList is the name of the table that is to be indexed. Both will
116911** be NULL for a primary key or an index that is created to satisfy a
116912** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
116913** as the table to be indexed. pParse->pNewTable is a table that is
116914** currently being constructed by a CREATE TABLE statement.
116915**
116916** pList is a list of columns to be indexed. pList will be NULL if this
116917** is a primary key or unique-constraint on the most recent column added
116918** to the table currently under construction.
116919*/
116920SQLITE_PRIVATE void sqlite3CreateIndex(
116921 Parse *pParse, /* All information about this parse */
116922 Token *pName1, /* First part of index name. May be NULL */
116923 Token *pName2, /* Second part of index name. May be NULL */
116924 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
116925 ExprList *pList, /* A list of columns to be indexed */
116926 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
116927 Token *pStart, /* The CREATE token that begins this statement */
116928 Expr *pPIWhere, /* WHERE clause for partial indices */
116929 int sortOrder, /* Sort order of primary key when pList==NULL */
116930 int ifNotExist, /* Omit error if index already exists */
116931 u8 idxType /* The index type */
116932){
116933 Table *pTab = 0; /* Table to be indexed */
116934 Index *pIndex = 0; /* The index to be created */
116935 char *zName = 0; /* Name of the index */
116936 int nName; /* Number of characters in zName */
116937 int i, j;
116938 DbFixer sFix; /* For assigning database names to pTable */
116939 int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */
116940 sqlite3 *db = pParse->db;
116941 Db *pDb; /* The specific table containing the indexed database */
116942 int iDb; /* Index of the database that is being written */
116943 Token *pName = 0; /* Unqualified name of the index to create */
116944 struct ExprList_item *pListItem; /* For looping over pList */
116945 int nExtra = 0; /* Space allocated for zExtra[] */
116946 int nExtraCol; /* Number of extra columns needed */
116947 char *zExtra = 0; /* Extra space after the Index object */
116948 Index *pPk = 0; /* PRIMARY KEY index for WITHOUT ROWID tables */
116949
116950 if( db->mallocFailed || pParse->nErr>0 ){
116951 goto exit_create_index;
116952 }
116953 if( IN_DECLARE_VTAB && idxType!=SQLITE_IDXTYPE_PRIMARYKEY ){
116954 goto exit_create_index;
116955 }
116956 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
116957 goto exit_create_index;
116958 }
116959 if( sqlite3HasExplicitNulls(pParse, pList) ){
116960 goto exit_create_index;
116961 }
116962
116963 /*
116964 ** Find the table that is to be indexed. Return early if not found.
116965 */
116966 if( pTblName!=0 ){
116967
116968 /* Use the two-part index name to determine the database
116969 ** to search for the table. 'Fix' the table name to this db
116970 ** before looking up the table.
116971 */
116972 assert( pName1 && pName2 );
116973 iDb = sqlite3TwoPartName(pParse, pName1, pName2, pUnqual: &pName);
116974 if( iDb<0 ) goto exit_create_index;
116975 assert( pName && pName->z );
116976
116977#ifndef SQLITE_OMIT_TEMPDB
116978 /* If the index name was unqualified, check if the table
116979 ** is a temp table. If so, set the database to 1. Do not do this
116980 ** if initialising a database schema.
116981 */
116982 if( !db->init.busy ){
116983 pTab = sqlite3SrcListLookup(pParse, pTblName);
116984 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
116985 iDb = 1;
116986 }
116987 }
116988#endif
116989
116990 sqlite3FixInit(pFix: &sFix, pParse, iDb, zType: "index", pName);
116991 if( sqlite3FixSrcList(pFix: &sFix, pList: pTblName) ){
116992 /* Because the parser constructs pTblName from a single identifier,
116993 ** sqlite3FixSrcList can never fail. */
116994 assert(0);
116995 }
116996 pTab = sqlite3LocateTableItem(pParse, flags: 0, p: &pTblName->a[0]);
116997 assert( db->mallocFailed==0 || pTab==0 );
116998 if( pTab==0 ) goto exit_create_index;
116999 if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
117000 sqlite3ErrorMsg(pParse,
117001 zFormat: "cannot create a TEMP index on non-TEMP table \"%s\"",
117002 pTab->zName);
117003 goto exit_create_index;
117004 }
117005 if( !HasRowid(pTab) ) pPk = sqlite3PrimaryKeyIndex(pTab);
117006 }else{
117007 assert( pName==0 );
117008 assert( pStart==0 );
117009 pTab = pParse->pNewTable;
117010 if( !pTab ) goto exit_create_index;
117011 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
117012 }
117013 pDb = &db->aDb[iDb];
117014
117015 assert( pTab!=0 );
117016 assert( pParse->nErr==0 );
117017 if( sqlite3StrNICmp(zLeft: pTab->zName, zRight: "sqlite_", N: 7)==0
117018 && db->init.busy==0
117019 && pTblName!=0
117020#if SQLITE_USER_AUTHENTICATION
117021 && sqlite3UserAuthTable(pTab->zName)==0
117022#endif
117023 ){
117024 sqlite3ErrorMsg(pParse, zFormat: "table %s may not be indexed", pTab->zName);
117025 goto exit_create_index;
117026 }
117027#ifndef SQLITE_OMIT_VIEW
117028 if( IsView(pTab) ){
117029 sqlite3ErrorMsg(pParse, zFormat: "views may not be indexed");
117030 goto exit_create_index;
117031 }
117032#endif
117033#ifndef SQLITE_OMIT_VIRTUALTABLE
117034 if( IsVirtual(pTab) ){
117035 sqlite3ErrorMsg(pParse, zFormat: "virtual tables may not be indexed");
117036 goto exit_create_index;
117037 }
117038#endif
117039
117040 /*
117041 ** Find the name of the index. Make sure there is not already another
117042 ** index or table with the same name.
117043 **
117044 ** Exception: If we are reading the names of permanent indices from the
117045 ** sqlite_schema table (because some other process changed the schema) and
117046 ** one of the index names collides with the name of a temporary table or
117047 ** index, then we will continue to process this index.
117048 **
117049 ** If pName==0 it means that we are
117050 ** dealing with a primary key or UNIQUE constraint. We have to invent our
117051 ** own name.
117052 */
117053 if( pName ){
117054 zName = sqlite3NameFromToken(db, pName);
117055 if( zName==0 ) goto exit_create_index;
117056 assert( pName->z!=0 );
117057 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName,zType: "index",zTblName: pTab->zName) ){
117058 goto exit_create_index;
117059 }
117060 if( !IN_RENAME_OBJECT ){
117061 if( !db->init.busy ){
117062 if( sqlite3FindTable(db, zName, zDatabase: 0)!=0 ){
117063 sqlite3ErrorMsg(pParse, zFormat: "there is already a table named %s", zName);
117064 goto exit_create_index;
117065 }
117066 }
117067 if( sqlite3FindIndex(db, zName, zDb: pDb->zDbSName)!=0 ){
117068 if( !ifNotExist ){
117069 sqlite3ErrorMsg(pParse, zFormat: "index %s already exists", zName);
117070 }else{
117071 assert( !db->init.busy );
117072 sqlite3CodeVerifySchema(pParse, iDb);
117073 sqlite3ForceNotReadOnly(pParse);
117074 }
117075 goto exit_create_index;
117076 }
117077 }
117078 }else{
117079 int n;
117080 Index *pLoop;
117081 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
117082 zName = sqlite3MPrintf(db, zFormat: "sqlite_autoindex_%s_%d", pTab->zName, n);
117083 if( zName==0 ){
117084 goto exit_create_index;
117085 }
117086
117087 /* Automatic index names generated from within sqlite3_declare_vtab()
117088 ** must have names that are distinct from normal automatic index names.
117089 ** The following statement converts "sqlite3_autoindex..." into
117090 ** "sqlite3_butoindex..." in order to make the names distinct.
117091 ** The "vtab_err.test" test demonstrates the need of this statement. */
117092 if( IN_SPECIAL_PARSE ) zName[7]++;
117093 }
117094
117095 /* Check for authorization to create an index.
117096 */
117097#ifndef SQLITE_OMIT_AUTHORIZATION
117098 if( !IN_RENAME_OBJECT ){
117099 const char *zDb = pDb->zDbSName;
117100 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), zArg2: 0, zArg3: zDb) ){
117101 goto exit_create_index;
117102 }
117103 i = SQLITE_CREATE_INDEX;
117104 if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX;
117105 if( sqlite3AuthCheck(pParse, code: i, zArg1: zName, zArg2: pTab->zName, zArg3: zDb) ){
117106 goto exit_create_index;
117107 }
117108 }
117109#endif
117110
117111 /* If pList==0, it means this routine was called to make a primary
117112 ** key out of the last column added to the table under construction.
117113 ** So create a fake list to simulate this.
117114 */
117115 if( pList==0 ){
117116 Token prevCol;
117117 Column *pCol = &pTab->aCol[pTab->nCol-1];
117118 pCol->colFlags |= COLFLAG_UNIQUE;
117119 sqlite3TokenInit(p: &prevCol, z: pCol->zCnName);
117120 pList = sqlite3ExprListAppend(pParse, pList: 0,
117121 pExpr: sqlite3ExprAlloc(db, TK_ID, pToken: &prevCol, dequote: 0));
117122 if( pList==0 ) goto exit_create_index;
117123 assert( pList->nExpr==1 );
117124 sqlite3ExprListSetSortOrder(p: pList, iSortOrder: sortOrder, SQLITE_SO_UNDEFINED);
117125 }else{
117126 sqlite3ExprListCheckLength(pParse, pEList: pList, zObject: "index");
117127 if( pParse->nErr ) goto exit_create_index;
117128 }
117129
117130 /* Figure out how many bytes of space are required to store explicitly
117131 ** specified collation sequence names.
117132 */
117133 for(i=0; i<pList->nExpr; i++){
117134 Expr *pExpr = pList->a[i].pExpr;
117135 assert( pExpr!=0 );
117136 if( pExpr->op==TK_COLLATE ){
117137 assert( !ExprHasProperty(pExpr, EP_IntValue) );
117138 nExtra += (1 + sqlite3Strlen30(z: pExpr->u.zToken));
117139 }
117140 }
117141
117142 /*
117143 ** Allocate the index structure.
117144 */
117145 nName = sqlite3Strlen30(z: zName);
117146 nExtraCol = pPk ? pPk->nKeyCol : 1;
117147 assert( pList->nExpr + nExtraCol <= 32767 /* Fits in i16 */ );
117148 pIndex = sqlite3AllocateIndexObject(db, nCol: pList->nExpr + nExtraCol,
117149 nExtra: nName + nExtra + 1, ppExtra: &zExtra);
117150 if( db->mallocFailed ){
117151 goto exit_create_index;
117152 }
117153 assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) );
117154 assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
117155 pIndex->zName = zExtra;
117156 zExtra += nName + 1;
117157 memcpy(dest: pIndex->zName, src: zName, n: nName+1);
117158 pIndex->pTable = pTab;
117159 pIndex->onError = (u8)onError;
117160 pIndex->uniqNotNull = onError!=OE_None;
117161 pIndex->idxType = idxType;
117162 pIndex->pSchema = db->aDb[iDb].pSchema;
117163 pIndex->nKeyCol = pList->nExpr;
117164 if( pPIWhere ){
117165 sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pExpr: pPIWhere, pList: 0);
117166 pIndex->pPartIdxWhere = pPIWhere;
117167 pPIWhere = 0;
117168 }
117169 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
117170
117171 /* Check to see if we should honor DESC requests on index columns
117172 */
117173 if( pDb->pSchema->file_format>=4 ){
117174 sortOrderMask = -1; /* Honor DESC */
117175 }else{
117176 sortOrderMask = 0; /* Ignore DESC */
117177 }
117178
117179 /* Analyze the list of expressions that form the terms of the index and
117180 ** report any errors. In the common case where the expression is exactly
117181 ** a table column, store that column in aiColumn[]. For general expressions,
117182 ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
117183 **
117184 ** TODO: Issue a warning if two or more columns of the index are identical.
117185 ** TODO: Issue a warning if the table primary key is used as part of the
117186 ** index key.
117187 */
117188 pListItem = pList->a;
117189 if( IN_RENAME_OBJECT ){
117190 pIndex->aColExpr = pList;
117191 pList = 0;
117192 }
117193 for(i=0; i<pIndex->nKeyCol; i++, pListItem++){
117194 Expr *pCExpr; /* The i-th index expression */
117195 int requestedSortOrder; /* ASC or DESC on the i-th expression */
117196 const char *zColl; /* Collation sequence name */
117197
117198 sqlite3StringToId(p: pListItem->pExpr);
117199 sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pExpr: pListItem->pExpr, pList: 0);
117200 if( pParse->nErr ) goto exit_create_index;
117201 pCExpr = sqlite3ExprSkipCollate(pExpr: pListItem->pExpr);
117202 if( pCExpr->op!=TK_COLUMN ){
117203 if( pTab==pParse->pNewTable ){
117204 sqlite3ErrorMsg(pParse, zFormat: "expressions prohibited in PRIMARY KEY and "
117205 "UNIQUE constraints");
117206 goto exit_create_index;
117207 }
117208 if( pIndex->aColExpr==0 ){
117209 pIndex->aColExpr = pList;
117210 pList = 0;
117211 }
117212 j = XN_EXPR;
117213 pIndex->aiColumn[i] = XN_EXPR;
117214 pIndex->uniqNotNull = 0;
117215 }else{
117216 j = pCExpr->iColumn;
117217 assert( j<=0x7fff );
117218 if( j<0 ){
117219 j = pTab->iPKey;
117220 }else{
117221 if( pTab->aCol[j].notNull==0 ){
117222 pIndex->uniqNotNull = 0;
117223 }
117224 if( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL ){
117225 pIndex->bHasVCol = 1;
117226 }
117227 }
117228 pIndex->aiColumn[i] = (i16)j;
117229 }
117230 zColl = 0;
117231 if( pListItem->pExpr->op==TK_COLLATE ){
117232 int nColl;
117233 assert( !ExprHasProperty(pListItem->pExpr, EP_IntValue) );
117234 zColl = pListItem->pExpr->u.zToken;
117235 nColl = sqlite3Strlen30(z: zColl) + 1;
117236 assert( nExtra>=nColl );
117237 memcpy(dest: zExtra, src: zColl, n: nColl);
117238 zColl = zExtra;
117239 zExtra += nColl;
117240 nExtra -= nColl;
117241 }else if( j>=0 ){
117242 zColl = sqlite3ColumnColl(pCol: &pTab->aCol[j]);
117243 }
117244 if( !zColl ) zColl = sqlite3StrBINARY;
117245 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zName: zColl) ){
117246 goto exit_create_index;
117247 }
117248 pIndex->azColl[i] = zColl;
117249 requestedSortOrder = pListItem->sortFlags & sortOrderMask;
117250 pIndex->aSortOrder[i] = (u8)requestedSortOrder;
117251 }
117252
117253 /* Append the table key to the end of the index. For WITHOUT ROWID
117254 ** tables (when pPk!=0) this will be the declared PRIMARY KEY. For
117255 ** normal tables (when pPk==0) this will be the rowid.
117256 */
117257 if( pPk ){
117258 for(j=0; j<pPk->nKeyCol; j++){
117259 int x = pPk->aiColumn[j];
117260 assert( x>=0 );
117261 if( isDupColumn(pIdx: pIndex, nKey: pIndex->nKeyCol, pPk, iCol: j) ){
117262 pIndex->nColumn--;
117263 }else{
117264 testcase( hasColumn(pIndex->aiColumn,pIndex->nKeyCol,x) );
117265 pIndex->aiColumn[i] = x;
117266 pIndex->azColl[i] = pPk->azColl[j];
117267 pIndex->aSortOrder[i] = pPk->aSortOrder[j];
117268 i++;
117269 }
117270 }
117271 assert( i==pIndex->nColumn );
117272 }else{
117273 pIndex->aiColumn[i] = XN_ROWID;
117274 pIndex->azColl[i] = sqlite3StrBINARY;
117275 }
117276 sqlite3DefaultRowEst(pIndex);
117277 if( pParse->pNewTable==0 ) estimateIndexWidth(pIdx: pIndex);
117278
117279 /* If this index contains every column of its table, then mark
117280 ** it as a covering index */
117281 assert( HasRowid(pTab)
117282 || pTab->iPKey<0 || sqlite3TableColumnToIndex(pIndex, pTab->iPKey)>=0 );
117283 recomputeColumnsNotIndexed(pIdx: pIndex);
117284 if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){
117285 pIndex->isCovering = 1;
117286 for(j=0; j<pTab->nCol; j++){
117287 if( j==pTab->iPKey ) continue;
117288 if( sqlite3TableColumnToIndex(pIdx: pIndex,iCol: j)>=0 ) continue;
117289 pIndex->isCovering = 0;
117290 break;
117291 }
117292 }
117293
117294 if( pTab==pParse->pNewTable ){
117295 /* This routine has been called to create an automatic index as a
117296 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
117297 ** a PRIMARY KEY or UNIQUE clause following the column definitions.
117298 ** i.e. one of:
117299 **
117300 ** CREATE TABLE t(x PRIMARY KEY, y);
117301 ** CREATE TABLE t(x, y, UNIQUE(x, y));
117302 **
117303 ** Either way, check to see if the table already has such an index. If
117304 ** so, don't bother creating this one. This only applies to
117305 ** automatically created indices. Users can do as they wish with
117306 ** explicit indices.
117307 **
117308 ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent
117309 ** (and thus suppressing the second one) even if they have different
117310 ** sort orders.
117311 **
117312 ** If there are different collating sequences or if the columns of
117313 ** the constraint occur in different orders, then the constraints are
117314 ** considered distinct and both result in separate indices.
117315 */
117316 Index *pIdx;
117317 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
117318 int k;
117319 assert( IsUniqueIndex(pIdx) );
117320 assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF );
117321 assert( IsUniqueIndex(pIndex) );
117322
117323 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
117324 for(k=0; k<pIdx->nKeyCol; k++){
117325 const char *z1;
117326 const char *z2;
117327 assert( pIdx->aiColumn[k]>=0 );
117328 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
117329 z1 = pIdx->azColl[k];
117330 z2 = pIndex->azColl[k];
117331 if( sqlite3StrICmp(zLeft: z1, zRight: z2) ) break;
117332 }
117333 if( k==pIdx->nKeyCol ){
117334 if( pIdx->onError!=pIndex->onError ){
117335 /* This constraint creates the same index as a previous
117336 ** constraint specified somewhere in the CREATE TABLE statement.
117337 ** However the ON CONFLICT clauses are different. If both this
117338 ** constraint and the previous equivalent constraint have explicit
117339 ** ON CONFLICT clauses this is an error. Otherwise, use the
117340 ** explicitly specified behavior for the index.
117341 */
117342 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
117343 sqlite3ErrorMsg(pParse,
117344 zFormat: "conflicting ON CONFLICT clauses specified", 0);
117345 }
117346 if( pIdx->onError==OE_Default ){
117347 pIdx->onError = pIndex->onError;
117348 }
117349 }
117350 if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType;
117351 if( IN_RENAME_OBJECT ){
117352 pIndex->pNext = pParse->pNewIndex;
117353 pParse->pNewIndex = pIndex;
117354 pIndex = 0;
117355 }
117356 goto exit_create_index;
117357 }
117358 }
117359 }
117360
117361 if( !IN_RENAME_OBJECT ){
117362
117363 /* Link the new Index structure to its table and to the other
117364 ** in-memory database structures.
117365 */
117366 assert( pParse->nErr==0 );
117367 if( db->init.busy ){
117368 Index *p;
117369 assert( !IN_SPECIAL_PARSE );
117370 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
117371 if( pTblName!=0 ){
117372 pIndex->tnum = db->init.newTnum;
117373 if( sqlite3IndexHasDuplicateRootPage(pIndex) ){
117374 sqlite3ErrorMsg(pParse, zFormat: "invalid rootpage");
117375 pParse->rc = SQLITE_CORRUPT_BKPT;
117376 goto exit_create_index;
117377 }
117378 }
117379 p = sqlite3HashInsert(pH: &pIndex->pSchema->idxHash,
117380 pKey: pIndex->zName, data: pIndex);
117381 if( p ){
117382 assert( p==pIndex ); /* Malloc must have failed */
117383 sqlite3OomFault(db);
117384 goto exit_create_index;
117385 }
117386 db->mDbFlags |= DBFLAG_SchemaChange;
117387 }
117388
117389 /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the
117390 ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then
117391 ** emit code to allocate the index rootpage on disk and make an entry for
117392 ** the index in the sqlite_schema table and populate the index with
117393 ** content. But, do not do this if we are simply reading the sqlite_schema
117394 ** table to parse the schema, or if this index is the PRIMARY KEY index
117395 ** of a WITHOUT ROWID table.
117396 **
117397 ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY
117398 ** or UNIQUE index in a CREATE TABLE statement. Since the table
117399 ** has just been created, it contains no data and the index initialization
117400 ** step can be skipped.
117401 */
117402 else if( HasRowid(pTab) || pTblName!=0 ){
117403 Vdbe *v;
117404 char *zStmt;
117405 int iMem = ++pParse->nMem;
117406
117407 v = sqlite3GetVdbe(pParse);
117408 if( v==0 ) goto exit_create_index;
117409
117410 sqlite3BeginWriteOperation(pParse, 1, iDb);
117411
117412 /* Create the rootpage for the index using CreateIndex. But before
117413 ** doing so, code a Noop instruction and store its address in
117414 ** Index.tnum. This is required in case this index is actually a
117415 ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In
117416 ** that case the convertToWithoutRowidTable() routine will replace
117417 ** the Noop with a Goto to jump over the VDBE code generated below. */
117418 pIndex->tnum = (Pgno)sqlite3VdbeAddOp0(p: v, OP_Noop);
117419 sqlite3VdbeAddOp3(p: v, OP_CreateBtree, p1: iDb, p2: iMem, BTREE_BLOBKEY);
117420
117421 /* Gather the complete text of the CREATE INDEX statement into
117422 ** the zStmt variable
117423 */
117424 assert( pName!=0 || pStart==0 );
117425 if( pStart ){
117426 int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
117427 if( pName->z[n-1]==';' ) n--;
117428 /* A named index with an explicit CREATE INDEX statement */
117429 zStmt = sqlite3MPrintf(db, zFormat: "CREATE%s INDEX %.*s",
117430 onError==OE_None ? "" : " UNIQUE", n, pName->z);
117431 }else{
117432 /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
117433 /* zStmt = sqlite3MPrintf(""); */
117434 zStmt = 0;
117435 }
117436
117437 /* Add an entry in sqlite_schema for this index
117438 */
117439 sqlite3NestedParse(pParse,
117440 zFormat: "INSERT INTO %Q." LEGACY_SCHEMA_TABLE " VALUES('index',%Q,%Q,#%d,%Q);",
117441 db->aDb[iDb].zDbSName,
117442 pIndex->zName,
117443 pTab->zName,
117444 iMem,
117445 zStmt
117446 );
117447 sqlite3DbFree(db, p: zStmt);
117448
117449 /* Fill the index with data and reparse the schema. Code an OP_Expire
117450 ** to invalidate all pre-compiled statements.
117451 */
117452 if( pTblName ){
117453 sqlite3RefillIndex(pParse, pIndex, memRootPage: iMem);
117454 sqlite3ChangeCookie(pParse, iDb);
117455 sqlite3VdbeAddParseSchemaOp(p: v, iDb,
117456 zWhere: sqlite3MPrintf(db, zFormat: "name='%q' AND type='index'", pIndex->zName), p5: 0);
117457 sqlite3VdbeAddOp2(p: v, OP_Expire, p1: 0, p2: 1);
117458 }
117459
117460 sqlite3VdbeJumpHere(p: v, addr: (int)pIndex->tnum);
117461 }
117462 }
117463 if( db->init.busy || pTblName==0 ){
117464 pIndex->pNext = pTab->pIndex;
117465 pTab->pIndex = pIndex;
117466 pIndex = 0;
117467 }
117468 else if( IN_RENAME_OBJECT ){
117469 assert( pParse->pNewIndex==0 );
117470 pParse->pNewIndex = pIndex;
117471 pIndex = 0;
117472 }
117473
117474 /* Clean up before exiting */
117475exit_create_index:
117476 if( pIndex ) sqlite3FreeIndex(db, p: pIndex);
117477 if( pTab ){
117478 /* Ensure all REPLACE indexes on pTab are at the end of the pIndex list.
117479 ** The list was already ordered when this routine was entered, so at this
117480 ** point at most a single index (the newly added index) will be out of
117481 ** order. So we have to reorder at most one index. */
117482 Index **ppFrom;
117483 Index *pThis;
117484 for(ppFrom=&pTab->pIndex; (pThis = *ppFrom)!=0; ppFrom=&pThis->pNext){
117485 Index *pNext;
117486 if( pThis->onError!=OE_Replace ) continue;
117487 while( (pNext = pThis->pNext)!=0 && pNext->onError!=OE_Replace ){
117488 *ppFrom = pNext;
117489 pThis->pNext = pNext->pNext;
117490 pNext->pNext = pThis;
117491 ppFrom = &pNext->pNext;
117492 }
117493 break;
117494 }
117495#ifdef SQLITE_DEBUG
117496 /* Verify that all REPLACE indexes really are now at the end
117497 ** of the index list. In other words, no other index type ever
117498 ** comes after a REPLACE index on the list. */
117499 for(pThis = pTab->pIndex; pThis; pThis=pThis->pNext){
117500 assert( pThis->onError!=OE_Replace
117501 || pThis->pNext==0
117502 || pThis->pNext->onError==OE_Replace );
117503 }
117504#endif
117505 }
117506 sqlite3ExprDelete(db, p: pPIWhere);
117507 sqlite3ExprListDelete(db, pList);
117508 sqlite3SrcListDelete(db, pTblName);
117509 sqlite3DbFree(db, p: zName);
117510}
117511
117512/*
117513** Fill the Index.aiRowEst[] array with default information - information
117514** to be used when we have not run the ANALYZE command.
117515**
117516** aiRowEst[0] is supposed to contain the number of elements in the index.
117517** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the
117518** number of rows in the table that match any particular value of the
117519** first column of the index. aiRowEst[2] is an estimate of the number
117520** of rows that match any particular combination of the first 2 columns
117521** of the index. And so forth. It must always be the case that
117522*
117523** aiRowEst[N]<=aiRowEst[N-1]
117524** aiRowEst[N]>=1
117525**
117526** Apart from that, we have little to go on besides intuition as to
117527** how aiRowEst[] should be initialized. The numbers generated here
117528** are based on typical values found in actual indices.
117529*/
117530SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
117531 /* 10, 9, 8, 7, 6 */
117532 static const LogEst aVal[] = { 33, 32, 30, 28, 26 };
117533 LogEst *a = pIdx->aiRowLogEst;
117534 LogEst x;
117535 int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol);
117536 int i;
117537
117538 /* Indexes with default row estimates should not have stat1 data */
117539 assert( !pIdx->hasStat1 );
117540
117541 /* Set the first entry (number of rows in the index) to the estimated
117542 ** number of rows in the table, or half the number of rows in the table
117543 ** for a partial index.
117544 **
117545 ** 2020-05-27: If some of the stat data is coming from the sqlite_stat1
117546 ** table but other parts we are having to guess at, then do not let the
117547 ** estimated number of rows in the table be less than 1000 (LogEst 99).
117548 ** Failure to do this can cause the indexes for which we do not have
117549 ** stat1 data to be ignored by the query planner.
117550 */
117551 x = pIdx->pTable->nRowLogEst;
117552 assert( 99==sqlite3LogEst(1000) );
117553 if( x<99 ){
117554 pIdx->pTable->nRowLogEst = x = 99;
117555 }
117556 if( pIdx->pPartIdxWhere!=0 ){ x -= 10; assert( 10==sqlite3LogEst(2) ); }
117557 a[0] = x;
117558
117559 /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is
117560 ** 6 and each subsequent value (if any) is 5. */
117561 memcpy(dest: &a[1], src: aVal, n: nCopy*sizeof(LogEst));
117562 for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
117563 a[i] = 23; assert( 23==sqlite3LogEst(5) );
117564 }
117565
117566 assert( 0==sqlite3LogEst(1) );
117567 if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0;
117568}
117569
117570/*
117571** This routine will drop an existing named index. This routine
117572** implements the DROP INDEX statement.
117573*/
117574SQLITE_PRIVATE void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
117575 Index *pIndex;
117576 Vdbe *v;
117577 sqlite3 *db = pParse->db;
117578 int iDb;
117579
117580 assert( pParse->nErr==0 ); /* Never called with prior errors */
117581 if( db->mallocFailed ){
117582 goto exit_drop_index;
117583 }
117584 assert( pName->nSrc==1 );
117585 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
117586 goto exit_drop_index;
117587 }
117588 pIndex = sqlite3FindIndex(db, zName: pName->a[0].zName, zDb: pName->a[0].zDatabase);
117589 if( pIndex==0 ){
117590 if( !ifExists ){
117591 sqlite3ErrorMsg(pParse, zFormat: "no such index: %S", pName->a);
117592 }else{
117593 sqlite3CodeVerifyNamedSchema(pParse, zDb: pName->a[0].zDatabase);
117594 sqlite3ForceNotReadOnly(pParse);
117595 }
117596 pParse->checkSchema = 1;
117597 goto exit_drop_index;
117598 }
117599 if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){
117600 sqlite3ErrorMsg(pParse, zFormat: "index associated with UNIQUE "
117601 "or PRIMARY KEY constraint cannot be dropped", 0);
117602 goto exit_drop_index;
117603 }
117604 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
117605#ifndef SQLITE_OMIT_AUTHORIZATION
117606 {
117607 int code = SQLITE_DROP_INDEX;
117608 Table *pTab = pIndex->pTable;
117609 const char *zDb = db->aDb[iDb].zDbSName;
117610 const char *zTab = SCHEMA_TABLE(iDb);
117611 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zArg1: zTab, zArg2: 0, zArg3: zDb) ){
117612 goto exit_drop_index;
117613 }
117614 if( !OMIT_TEMPDB && iDb==1 ) code = SQLITE_DROP_TEMP_INDEX;
117615 if( sqlite3AuthCheck(pParse, code, zArg1: pIndex->zName, zArg2: pTab->zName, zArg3: zDb) ){
117616 goto exit_drop_index;
117617 }
117618 }
117619#endif
117620
117621 /* Generate code to remove the index and from the schema table */
117622 v = sqlite3GetVdbe(pParse);
117623 if( v ){
117624 sqlite3BeginWriteOperation(pParse, 1, iDb);
117625 sqlite3NestedParse(pParse,
117626 zFormat: "DELETE FROM %Q." LEGACY_SCHEMA_TABLE " WHERE name=%Q AND type='index'",
117627 db->aDb[iDb].zDbSName, pIndex->zName
117628 );
117629 sqlite3ClearStatTables(pParse, iDb, zType: "idx", zName: pIndex->zName);
117630 sqlite3ChangeCookie(pParse, iDb);
117631 destroyRootPage(pParse, iTable: pIndex->tnum, iDb);
117632 sqlite3VdbeAddOp4(p: v, OP_DropIndex, p1: iDb, p2: 0, p3: 0, zP4: pIndex->zName, p4type: 0);
117633 }
117634
117635exit_drop_index:
117636 sqlite3SrcListDelete(db, pName);
117637}
117638
117639/*
117640** pArray is a pointer to an array of objects. Each object in the
117641** array is szEntry bytes in size. This routine uses sqlite3DbRealloc()
117642** to extend the array so that there is space for a new object at the end.
117643**
117644** When this function is called, *pnEntry contains the current size of
117645** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
117646** in total).
117647**
117648** If the realloc() is successful (i.e. if no OOM condition occurs), the
117649** space allocated for the new object is zeroed, *pnEntry updated to
117650** reflect the new size of the array and a pointer to the new allocation
117651** returned. *pIdx is set to the index of the new array entry in this case.
117652**
117653** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
117654** unchanged and a copy of pArray returned.
117655*/
117656SQLITE_PRIVATE void *sqlite3ArrayAllocate(
117657 sqlite3 *db, /* Connection to notify of malloc failures */
117658 void *pArray, /* Array of objects. Might be reallocated */
117659 int szEntry, /* Size of each object in the array */
117660 int *pnEntry, /* Number of objects currently in use */
117661 int *pIdx /* Write the index of a new slot here */
117662){
117663 char *z;
117664 sqlite3_int64 n = *pIdx = *pnEntry;
117665 if( (n & (n-1))==0 ){
117666 sqlite3_int64 sz = (n==0) ? 1 : 2*n;
117667 void *pNew = sqlite3DbRealloc(db, p: pArray, n: sz*szEntry);
117668 if( pNew==0 ){
117669 *pIdx = -1;
117670 return pArray;
117671 }
117672 pArray = pNew;
117673 }
117674 z = (char*)pArray;
117675 memset(s: &z[n * szEntry], c: 0, n: szEntry);
117676 ++*pnEntry;
117677 return pArray;
117678}
117679
117680/*
117681** Append a new element to the given IdList. Create a new IdList if
117682** need be.
117683**
117684** A new IdList is returned, or NULL if malloc() fails.
117685*/
117686SQLITE_PRIVATE IdList *sqlite3IdListAppend(Parse *pParse, IdList *pList, Token *pToken){
117687 sqlite3 *db = pParse->db;
117688 int i;
117689 if( pList==0 ){
117690 pList = sqlite3DbMallocZero(db, n: sizeof(IdList) );
117691 if( pList==0 ) return 0;
117692 }
117693 pList->a = sqlite3ArrayAllocate(
117694 db,
117695 pArray: pList->a,
117696 szEntry: sizeof(pList->a[0]),
117697 pnEntry: &pList->nId,
117698 pIdx: &i
117699 );
117700 if( i<0 ){
117701 sqlite3IdListDelete(db, pList);
117702 return 0;
117703 }
117704 pList->a[i].zName = sqlite3NameFromToken(db, pName: pToken);
117705 if( IN_RENAME_OBJECT && pList->a[i].zName ){
117706 sqlite3RenameTokenMap(pParse, pPtr: (void*)pList->a[i].zName, pToken);
117707 }
117708 return pList;
117709}
117710
117711/*
117712** Delete an IdList.
117713*/
117714SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
117715 int i;
117716 if( pList==0 ) return;
117717 for(i=0; i<pList->nId; i++){
117718 sqlite3DbFree(db, p: pList->a[i].zName);
117719 }
117720 sqlite3DbFree(db, p: pList->a);
117721 sqlite3DbFreeNN(db, p: pList);
117722}
117723
117724/*
117725** Return the index in pList of the identifier named zId. Return -1
117726** if not found.
117727*/
117728SQLITE_PRIVATE int sqlite3IdListIndex(IdList *pList, const char *zName){
117729 int i;
117730 if( pList==0 ) return -1;
117731 for(i=0; i<pList->nId; i++){
117732 if( sqlite3StrICmp(zLeft: pList->a[i].zName, zRight: zName)==0 ) return i;
117733 }
117734 return -1;
117735}
117736
117737/*
117738** Maximum size of a SrcList object.
117739** The SrcList object is used to represent the FROM clause of a
117740** SELECT statement, and the query planner cannot deal with more
117741** than 64 tables in a join. So any value larger than 64 here
117742** is sufficient for most uses. Smaller values, like say 10, are
117743** appropriate for small and memory-limited applications.
117744*/
117745#ifndef SQLITE_MAX_SRCLIST
117746# define SQLITE_MAX_SRCLIST 200
117747#endif
117748
117749/*
117750** Expand the space allocated for the given SrcList object by
117751** creating nExtra new slots beginning at iStart. iStart is zero based.
117752** New slots are zeroed.
117753**
117754** For example, suppose a SrcList initially contains two entries: A,B.
117755** To append 3 new entries onto the end, do this:
117756**
117757** sqlite3SrcListEnlarge(db, pSrclist, 3, 2);
117758**
117759** After the call above it would contain: A, B, nil, nil, nil.
117760** If the iStart argument had been 1 instead of 2, then the result
117761** would have been: A, nil, nil, nil, B. To prepend the new slots,
117762** the iStart value would be 0. The result then would
117763** be: nil, nil, nil, A, B.
117764**
117765** If a memory allocation fails or the SrcList becomes too large, leave
117766** the original SrcList unchanged, return NULL, and leave an error message
117767** in pParse.
117768*/
117769SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(
117770 Parse *pParse, /* Parsing context into which errors are reported */
117771 SrcList *pSrc, /* The SrcList to be enlarged */
117772 int nExtra, /* Number of new slots to add to pSrc->a[] */
117773 int iStart /* Index in pSrc->a[] of first new slot */
117774){
117775 int i;
117776
117777 /* Sanity checking on calling parameters */
117778 assert( iStart>=0 );
117779 assert( nExtra>=1 );
117780 assert( pSrc!=0 );
117781 assert( iStart<=pSrc->nSrc );
117782
117783 /* Allocate additional space if needed */
117784 if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
117785 SrcList *pNew;
117786 sqlite3_int64 nAlloc = 2*(sqlite3_int64)pSrc->nSrc+nExtra;
117787 sqlite3 *db = pParse->db;
117788
117789 if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST ){
117790 sqlite3ErrorMsg(pParse, zFormat: "too many FROM clause terms, max: %d",
117791 SQLITE_MAX_SRCLIST);
117792 return 0;
117793 }
117794 if( nAlloc>SQLITE_MAX_SRCLIST ) nAlloc = SQLITE_MAX_SRCLIST;
117795 pNew = sqlite3DbRealloc(db, p: pSrc,
117796 n: sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
117797 if( pNew==0 ){
117798 assert( db->mallocFailed );
117799 return 0;
117800 }
117801 pSrc = pNew;
117802 pSrc->nAlloc = nAlloc;
117803 }
117804
117805 /* Move existing slots that come after the newly inserted slots
117806 ** out of the way */
117807 for(i=pSrc->nSrc-1; i>=iStart; i--){
117808 pSrc->a[i+nExtra] = pSrc->a[i];
117809 }
117810 pSrc->nSrc += nExtra;
117811
117812 /* Zero the newly allocated slots */
117813 memset(s: &pSrc->a[iStart], c: 0, n: sizeof(pSrc->a[0])*nExtra);
117814 for(i=iStart; i<iStart+nExtra; i++){
117815 pSrc->a[i].iCursor = -1;
117816 }
117817
117818 /* Return a pointer to the enlarged SrcList */
117819 return pSrc;
117820}
117821
117822
117823/*
117824** Append a new table name to the given SrcList. Create a new SrcList if
117825** need be. A new entry is created in the SrcList even if pTable is NULL.
117826**
117827** A SrcList is returned, or NULL if there is an OOM error or if the
117828** SrcList grows to large. The returned
117829** SrcList might be the same as the SrcList that was input or it might be
117830** a new one. If an OOM error does occurs, then the prior value of pList
117831** that is input to this routine is automatically freed.
117832**
117833** If pDatabase is not null, it means that the table has an optional
117834** database name prefix. Like this: "database.table". The pDatabase
117835** points to the table name and the pTable points to the database name.
117836** The SrcList.a[].zName field is filled with the table name which might
117837** come from pTable (if pDatabase is NULL) or from pDatabase.
117838** SrcList.a[].zDatabase is filled with the database name from pTable,
117839** or with NULL if no database is specified.
117840**
117841** In other words, if call like this:
117842**
117843** sqlite3SrcListAppend(D,A,B,0);
117844**
117845** Then B is a table name and the database name is unspecified. If called
117846** like this:
117847**
117848** sqlite3SrcListAppend(D,A,B,C);
117849**
117850** Then C is the table name and B is the database name. If C is defined
117851** then so is B. In other words, we never have a case where:
117852**
117853** sqlite3SrcListAppend(D,A,0,C);
117854**
117855** Both pTable and pDatabase are assumed to be quoted. They are dequoted
117856** before being added to the SrcList.
117857*/
117858SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(
117859 Parse *pParse, /* Parsing context, in which errors are reported */
117860 SrcList *pList, /* Append to this SrcList. NULL creates a new SrcList */
117861 Token *pTable, /* Table to append */
117862 Token *pDatabase /* Database of the table */
117863){
117864 SrcItem *pItem;
117865 sqlite3 *db;
117866 assert( pDatabase==0 || pTable!=0 ); /* Cannot have C without B */
117867 assert( pParse!=0 );
117868 assert( pParse->db!=0 );
117869 db = pParse->db;
117870 if( pList==0 ){
117871 pList = sqlite3DbMallocRawNN(db: pParse->db, n: sizeof(SrcList) );
117872 if( pList==0 ) return 0;
117873 pList->nAlloc = 1;
117874 pList->nSrc = 1;
117875 memset(s: &pList->a[0], c: 0, n: sizeof(pList->a[0]));
117876 pList->a[0].iCursor = -1;
117877 }else{
117878 SrcList *pNew = sqlite3SrcListEnlarge(pParse, pSrc: pList, nExtra: 1, iStart: pList->nSrc);
117879 if( pNew==0 ){
117880 sqlite3SrcListDelete(db, pList);
117881 return 0;
117882 }else{
117883 pList = pNew;
117884 }
117885 }
117886 pItem = &pList->a[pList->nSrc-1];
117887 if( pDatabase && pDatabase->z==0 ){
117888 pDatabase = 0;
117889 }
117890 if( pDatabase ){
117891 pItem->zName = sqlite3NameFromToken(db, pName: pDatabase);
117892 pItem->zDatabase = sqlite3NameFromToken(db, pName: pTable);
117893 }else{
117894 pItem->zName = sqlite3NameFromToken(db, pName: pTable);
117895 pItem->zDatabase = 0;
117896 }
117897 return pList;
117898}
117899
117900/*
117901** Assign VdbeCursor index numbers to all tables in a SrcList
117902*/
117903SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
117904 int i;
117905 SrcItem *pItem;
117906 assert( pList || pParse->db->mallocFailed );
117907 if( ALWAYS(pList) ){
117908 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
117909 if( pItem->iCursor>=0 ) continue;
117910 pItem->iCursor = pParse->nTab++;
117911 if( pItem->pSelect ){
117912 sqlite3SrcListAssignCursors(pParse, pList: pItem->pSelect->pSrc);
117913 }
117914 }
117915 }
117916}
117917
117918/*
117919** Delete an entire SrcList including all its substructure.
117920*/
117921SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
117922 int i;
117923 SrcItem *pItem;
117924 if( pList==0 ) return;
117925 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
117926 if( pItem->zDatabase ) sqlite3DbFreeNN(db, p: pItem->zDatabase);
117927 sqlite3DbFree(db, p: pItem->zName);
117928 if( pItem->zAlias ) sqlite3DbFreeNN(db, p: pItem->zAlias);
117929 if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, p: pItem->u1.zIndexedBy);
117930 if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pList: pItem->u1.pFuncArg);
117931 sqlite3DeleteTable(db, pTable: pItem->pTab);
117932 if( pItem->pSelect ) sqlite3SelectDelete(db, pItem->pSelect);
117933 if( pItem->pOn ) sqlite3ExprDelete(db, p: pItem->pOn);
117934 if( pItem->pUsing ) sqlite3IdListDelete(db, pList: pItem->pUsing);
117935 }
117936 sqlite3DbFreeNN(db, p: pList);
117937}
117938
117939/*
117940** This routine is called by the parser to add a new term to the
117941** end of a growing FROM clause. The "p" parameter is the part of
117942** the FROM clause that has already been constructed. "p" is NULL
117943** if this is the first term of the FROM clause. pTable and pDatabase
117944** are the name of the table and database named in the FROM clause term.
117945** pDatabase is NULL if the database name qualifier is missing - the
117946** usual case. If the term has an alias, then pAlias points to the
117947** alias token. If the term is a subquery, then pSubquery is the
117948** SELECT statement that the subquery encodes. The pTable and
117949** pDatabase parameters are NULL for subqueries. The pOn and pUsing
117950** parameters are the content of the ON and USING clauses.
117951**
117952** Return a new SrcList which encodes is the FROM with the new
117953** term added.
117954*/
117955SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(
117956 Parse *pParse, /* Parsing context */
117957 SrcList *p, /* The left part of the FROM clause already seen */
117958 Token *pTable, /* Name of the table to add to the FROM clause */
117959 Token *pDatabase, /* Name of the database containing pTable */
117960 Token *pAlias, /* The right-hand side of the AS subexpression */
117961 Select *pSubquery, /* A subquery used in place of a table name */
117962 Expr *pOn, /* The ON clause of a join */
117963 IdList *pUsing /* The USING clause of a join */
117964){
117965 SrcItem *pItem;
117966 sqlite3 *db = pParse->db;
117967 if( !p && (pOn || pUsing) ){
117968 sqlite3ErrorMsg(pParse, zFormat: "a JOIN clause is required before %s",
117969 (pOn ? "ON" : "USING")
117970 );
117971 goto append_from_error;
117972 }
117973 p = sqlite3SrcListAppend(pParse, pList: p, pTable, pDatabase);
117974 if( p==0 ){
117975 goto append_from_error;
117976 }
117977 assert( p->nSrc>0 );
117978 pItem = &p->a[p->nSrc-1];
117979 assert( (pTable==0)==(pDatabase==0) );
117980 assert( pItem->zName==0 || pDatabase!=0 );
117981 if( IN_RENAME_OBJECT && pItem->zName ){
117982 Token *pToken = (ALWAYS(pDatabase) && pDatabase->z) ? pDatabase : pTable;
117983 sqlite3RenameTokenMap(pParse, pPtr: pItem->zName, pToken);
117984 }
117985 assert( pAlias!=0 );
117986 if( pAlias->n ){
117987 pItem->zAlias = sqlite3NameFromToken(db, pName: pAlias);
117988 }
117989 pItem->pSelect = pSubquery;
117990 pItem->pOn = pOn;
117991 pItem->pUsing = pUsing;
117992 return p;
117993
117994 append_from_error:
117995 assert( p==0 );
117996 sqlite3ExprDelete(db, p: pOn);
117997 sqlite3IdListDelete(db, pList: pUsing);
117998 sqlite3SelectDelete(db, pSubquery);
117999 return 0;
118000}
118001
118002/*
118003** Add an INDEXED BY or NOT INDEXED clause to the most recently added
118004** element of the source-list passed as the second argument.
118005*/
118006SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
118007 assert( pIndexedBy!=0 );
118008 if( p && pIndexedBy->n>0 ){
118009 SrcItem *pItem;
118010 assert( p->nSrc>0 );
118011 pItem = &p->a[p->nSrc-1];
118012 assert( pItem->fg.notIndexed==0 );
118013 assert( pItem->fg.isIndexedBy==0 );
118014 assert( pItem->fg.isTabFunc==0 );
118015 if( pIndexedBy->n==1 && !pIndexedBy->z ){
118016 /* A "NOT INDEXED" clause was supplied. See parse.y
118017 ** construct "indexed_opt" for details. */
118018 pItem->fg.notIndexed = 1;
118019 }else{
118020 pItem->u1.zIndexedBy = sqlite3NameFromToken(db: pParse->db, pName: pIndexedBy);
118021 pItem->fg.isIndexedBy = 1;
118022 assert( pItem->fg.isCte==0 ); /* No collision on union u2 */
118023 }
118024 }
118025}
118026
118027/*
118028** Append the contents of SrcList p2 to SrcList p1 and return the resulting
118029** SrcList. Or, if an error occurs, return NULL. In all cases, p1 and p2
118030** are deleted by this function.
118031*/
118032SQLITE_PRIVATE SrcList *sqlite3SrcListAppendList(Parse *pParse, SrcList *p1, SrcList *p2){
118033 assert( p1 && p1->nSrc==1 );
118034 if( p2 ){
118035 SrcList *pNew = sqlite3SrcListEnlarge(pParse, pSrc: p1, nExtra: p2->nSrc, iStart: 1);
118036 if( pNew==0 ){
118037 sqlite3SrcListDelete(db: pParse->db, pList: p2);
118038 }else{
118039 p1 = pNew;
118040 memcpy(dest: &p1->a[1], src: p2->a, n: p2->nSrc*sizeof(SrcItem));
118041 sqlite3DbFree(db: pParse->db, p: p2);
118042 }
118043 }
118044 return p1;
118045}
118046
118047/*
118048** Add the list of function arguments to the SrcList entry for a
118049** table-valued-function.
118050*/
118051SQLITE_PRIVATE void sqlite3SrcListFuncArgs(Parse *pParse, SrcList *p, ExprList *pList){
118052 if( p ){
118053 SrcItem *pItem = &p->a[p->nSrc-1];
118054 assert( pItem->fg.notIndexed==0 );
118055 assert( pItem->fg.isIndexedBy==0 );
118056 assert( pItem->fg.isTabFunc==0 );
118057 pItem->u1.pFuncArg = pList;
118058 pItem->fg.isTabFunc = 1;
118059 }else{
118060 sqlite3ExprListDelete(db: pParse->db, pList);
118061 }
118062}
118063
118064/*
118065** When building up a FROM clause in the parser, the join operator
118066** is initially attached to the left operand. But the code generator
118067** expects the join operator to be on the right operand. This routine
118068** Shifts all join operators from left to right for an entire FROM
118069** clause.
118070**
118071** Example: Suppose the join is like this:
118072**
118073** A natural cross join B
118074**
118075** The operator is "natural cross join". The A and B operands are stored
118076** in p->a[0] and p->a[1], respectively. The parser initially stores the
118077** operator with A. This routine shifts that operator over to B.
118078*/
118079SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){
118080 if( p ){
118081 int i;
118082 for(i=p->nSrc-1; i>0; i--){
118083 p->a[i].fg.jointype = p->a[i-1].fg.jointype;
118084 }
118085 p->a[0].fg.jointype = 0;
118086 }
118087}
118088
118089/*
118090** Generate VDBE code for a BEGIN statement.
118091*/
118092SQLITE_PRIVATE void sqlite3BeginTransaction(Parse *pParse, int type){
118093 sqlite3 *db;
118094 Vdbe *v;
118095 int i;
118096
118097 assert( pParse!=0 );
118098 db = pParse->db;
118099 assert( db!=0 );
118100 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, zArg1: "BEGIN", zArg2: 0, zArg3: 0) ){
118101 return;
118102 }
118103 v = sqlite3GetVdbe(pParse);
118104 if( !v ) return;
118105 if( type!=TK_DEFERRED ){
118106 for(i=0; i<db->nDb; i++){
118107 int eTxnType;
118108 Btree *pBt = db->aDb[i].pBt;
118109 if( pBt && sqlite3BtreeIsReadonly(p: pBt) ){
118110 eTxnType = 0; /* Read txn */
118111 }else if( type==TK_EXCLUSIVE ){
118112 eTxnType = 2; /* Exclusive txn */
118113 }else{
118114 eTxnType = 1; /* Write txn */
118115 }
118116 sqlite3VdbeAddOp2(p: v, OP_Transaction, p1: i, p2: eTxnType);
118117 sqlite3VdbeUsesBtree(p: v, i);
118118 }
118119 }
118120 sqlite3VdbeAddOp0(p: v, OP_AutoCommit);
118121}
118122
118123/*
118124** Generate VDBE code for a COMMIT or ROLLBACK statement.
118125** Code for ROLLBACK is generated if eType==TK_ROLLBACK. Otherwise
118126** code is generated for a COMMIT.
118127*/
118128SQLITE_PRIVATE void sqlite3EndTransaction(Parse *pParse, int eType){
118129 Vdbe *v;
118130 int isRollback;
118131
118132 assert( pParse!=0 );
118133 assert( pParse->db!=0 );
118134 assert( eType==TK_COMMIT || eType==TK_END || eType==TK_ROLLBACK );
118135 isRollback = eType==TK_ROLLBACK;
118136 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION,
118137 zArg1: isRollback ? "ROLLBACK" : "COMMIT", zArg2: 0, zArg3: 0) ){
118138 return;
118139 }
118140 v = sqlite3GetVdbe(pParse);
118141 if( v ){
118142 sqlite3VdbeAddOp2(p: v, OP_AutoCommit, p1: 1, p2: isRollback);
118143 }
118144}
118145
118146/*
118147** This function is called by the parser when it parses a command to create,
118148** release or rollback an SQL savepoint.
118149*/
118150SQLITE_PRIVATE void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
118151 char *zName = sqlite3NameFromToken(db: pParse->db, pName);
118152 if( zName ){
118153 Vdbe *v = sqlite3GetVdbe(pParse);
118154#ifndef SQLITE_OMIT_AUTHORIZATION
118155 static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
118156 assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 );
118157#endif
118158 if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, zArg1: az[op], zArg2: zName, zArg3: 0) ){
118159 sqlite3DbFree(db: pParse->db, p: zName);
118160 return;
118161 }
118162 sqlite3VdbeAddOp4(p: v, OP_Savepoint, p1: op, p2: 0, p3: 0, zP4: zName, P4_DYNAMIC);
118163 }
118164}
118165
118166/*
118167** Make sure the TEMP database is open and available for use. Return
118168** the number of errors. Leave any error messages in the pParse structure.
118169*/
118170SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *pParse){
118171 sqlite3 *db = pParse->db;
118172 if( db->aDb[1].pBt==0 && !pParse->explain ){
118173 int rc;
118174 Btree *pBt;
118175 static const int flags =
118176 SQLITE_OPEN_READWRITE |
118177 SQLITE_OPEN_CREATE |
118178 SQLITE_OPEN_EXCLUSIVE |
118179 SQLITE_OPEN_DELETEONCLOSE |
118180 SQLITE_OPEN_TEMP_DB;
118181
118182 rc = sqlite3BtreeOpen(pVfs: db->pVfs, zFilename: 0, db, ppBtree: &pBt, flags: 0, vfsFlags: flags);
118183 if( rc!=SQLITE_OK ){
118184 sqlite3ErrorMsg(pParse, zFormat: "unable to open a temporary database "
118185 "file for storing temporary tables");
118186 pParse->rc = rc;
118187 return 1;
118188 }
118189 db->aDb[1].pBt = pBt;
118190 assert( db->aDb[1].pSchema );
118191 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(p: pBt, pageSize: db->nextPagesize, nReserve: 0, iFix: 0) ){
118192 sqlite3OomFault(db);
118193 return 1;
118194 }
118195 }
118196 return 0;
118197}
118198
118199/*
118200** Record the fact that the schema cookie will need to be verified
118201** for database iDb. The code to actually verify the schema cookie
118202** will occur at the end of the top-level VDBE and will be generated
118203** later, by sqlite3FinishCoding().
118204*/
118205static void sqlite3CodeVerifySchemaAtToplevel(Parse *pToplevel, int iDb){
118206 assert( iDb>=0 && iDb<pToplevel->db->nDb );
118207 assert( pToplevel->db->aDb[iDb].pBt!=0 || iDb==1 );
118208 assert( iDb<SQLITE_MAX_DB );
118209 assert( sqlite3SchemaMutexHeld(pToplevel->db, iDb, 0) );
118210 if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){
118211 DbMaskSet(pToplevel->cookieMask, iDb);
118212 if( !OMIT_TEMPDB && iDb==1 ){
118213 sqlite3OpenTempDatabase(pParse: pToplevel);
118214 }
118215 }
118216}
118217SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
118218 sqlite3CodeVerifySchemaAtToplevel(sqlite3ParseToplevel(pParse), iDb);
118219}
118220
118221
118222/*
118223** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each
118224** attached database. Otherwise, invoke it for the database named zDb only.
118225*/
118226SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){
118227 sqlite3 *db = pParse->db;
118228 int i;
118229 for(i=0; i<db->nDb; i++){
118230 Db *pDb = &db->aDb[i];
118231 if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zLeft: zDb, zRight: pDb->zDbSName)) ){
118232 sqlite3CodeVerifySchema(pParse, iDb: i);
118233 }
118234 }
118235}
118236
118237/*
118238** Generate VDBE code that prepares for doing an operation that
118239** might change the database.
118240**
118241** This routine starts a new transaction if we are not already within
118242** a transaction. If we are already within a transaction, then a checkpoint
118243** is set if the setStatement parameter is true. A checkpoint should
118244** be set for operations that might fail (due to a constraint) part of
118245** the way through and which will need to undo some writes without having to
118246** rollback the whole transaction. For operations where all constraints
118247** can be checked before any changes are made to the database, it is never
118248** necessary to undo a write and the checkpoint should not be set.
118249*/
118250SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
118251 Parse *pToplevel = sqlite3ParseToplevel(pParse);
118252 sqlite3CodeVerifySchemaAtToplevel(pToplevel, iDb);
118253 DbMaskSet(pToplevel->writeMask, iDb);
118254 pToplevel->isMultiWrite |= setStatement;
118255}
118256
118257/*
118258** Indicate that the statement currently under construction might write
118259** more than one entry (example: deleting one row then inserting another,
118260** inserting multiple rows in a table, or inserting a row and index entries.)
118261** If an abort occurs after some of these writes have completed, then it will
118262** be necessary to undo the completed writes.
118263*/
118264SQLITE_PRIVATE void sqlite3MultiWrite(Parse *pParse){
118265 Parse *pToplevel = sqlite3ParseToplevel(pParse);
118266 pToplevel->isMultiWrite = 1;
118267}
118268
118269/*
118270** The code generator calls this routine if is discovers that it is
118271** possible to abort a statement prior to completion. In order to
118272** perform this abort without corrupting the database, we need to make
118273** sure that the statement is protected by a statement transaction.
118274**
118275** Technically, we only need to set the mayAbort flag if the
118276** isMultiWrite flag was previously set. There is a time dependency
118277** such that the abort must occur after the multiwrite. This makes
118278** some statements involving the REPLACE conflict resolution algorithm
118279** go a little faster. But taking advantage of this time dependency
118280** makes it more difficult to prove that the code is correct (in
118281** particular, it prevents us from writing an effective
118282** implementation of sqlite3AssertMayAbort()) and so we have chosen
118283** to take the safe route and skip the optimization.
118284*/
118285SQLITE_PRIVATE void sqlite3MayAbort(Parse *pParse){
118286 Parse *pToplevel = sqlite3ParseToplevel(pParse);
118287 pToplevel->mayAbort = 1;
118288}
118289
118290/*
118291** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT
118292** error. The onError parameter determines which (if any) of the statement
118293** and/or current transaction is rolled back.
118294*/
118295SQLITE_PRIVATE void sqlite3HaltConstraint(
118296 Parse *pParse, /* Parsing context */
118297 int errCode, /* extended error code */
118298 int onError, /* Constraint type */
118299 char *p4, /* Error message */
118300 i8 p4type, /* P4_STATIC or P4_TRANSIENT */
118301 u8 p5Errmsg /* P5_ErrMsg type */
118302){
118303 Vdbe *v;
118304 assert( pParse->pVdbe!=0 );
118305 v = sqlite3GetVdbe(pParse);
118306 assert( (errCode&0xff)==SQLITE_CONSTRAINT || pParse->nested );
118307 if( onError==OE_Abort ){
118308 sqlite3MayAbort(pParse);
118309 }
118310 sqlite3VdbeAddOp4(p: v, OP_Halt, p1: errCode, p2: onError, p3: 0, zP4: p4, p4type);
118311 sqlite3VdbeChangeP5(p: v, p5: p5Errmsg);
118312}
118313
118314/*
118315** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation.
118316*/
118317SQLITE_PRIVATE void sqlite3UniqueConstraint(
118318 Parse *pParse, /* Parsing context */
118319 int onError, /* Constraint type */
118320 Index *pIdx /* The index that triggers the constraint */
118321){
118322 char *zErr;
118323 int j;
118324 StrAccum errMsg;
118325 Table *pTab = pIdx->pTable;
118326
118327 sqlite3StrAccumInit(p: &errMsg, db: pParse->db, zBase: 0, n: 0,
118328 mx: pParse->db->aLimit[SQLITE_LIMIT_LENGTH]);
118329 if( pIdx->aColExpr ){
118330 sqlite3_str_appendf(p: &errMsg, zFormat: "index '%q'", pIdx->zName);
118331 }else{
118332 for(j=0; j<pIdx->nKeyCol; j++){
118333 char *zCol;
118334 assert( pIdx->aiColumn[j]>=0 );
118335 zCol = pTab->aCol[pIdx->aiColumn[j]].zCnName;
118336 if( j ) sqlite3_str_append(p: &errMsg, z: ", ", N: 2);
118337 sqlite3_str_appendall(p: &errMsg, z: pTab->zName);
118338 sqlite3_str_append(p: &errMsg, z: ".", N: 1);
118339 sqlite3_str_appendall(p: &errMsg, z: zCol);
118340 }
118341 }
118342 zErr = sqlite3StrAccumFinish(p: &errMsg);
118343 sqlite3HaltConstraint(pParse,
118344 IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY
118345 : SQLITE_CONSTRAINT_UNIQUE,
118346 onError, p4: zErr, P4_DYNAMIC, P5_ConstraintUnique);
118347}
118348
118349
118350/*
118351** Code an OP_Halt due to non-unique rowid.
118352*/
118353SQLITE_PRIVATE void sqlite3RowidConstraint(
118354 Parse *pParse, /* Parsing context */
118355 int onError, /* Conflict resolution algorithm */
118356 Table *pTab /* The table with the non-unique rowid */
118357){
118358 char *zMsg;
118359 int rc;
118360 if( pTab->iPKey>=0 ){
118361 zMsg = sqlite3MPrintf(db: pParse->db, zFormat: "%s.%s", pTab->zName,
118362 pTab->aCol[pTab->iPKey].zCnName);
118363 rc = SQLITE_CONSTRAINT_PRIMARYKEY;
118364 }else{
118365 zMsg = sqlite3MPrintf(db: pParse->db, zFormat: "%s.rowid", pTab->zName);
118366 rc = SQLITE_CONSTRAINT_ROWID;
118367 }
118368 sqlite3HaltConstraint(pParse, errCode: rc, onError, p4: zMsg, P4_DYNAMIC,
118369 P5_ConstraintUnique);
118370}
118371
118372/*
118373** Check to see if pIndex uses the collating sequence pColl. Return
118374** true if it does and false if it does not.
118375*/
118376#ifndef SQLITE_OMIT_REINDEX
118377static int collationMatch(const char *zColl, Index *pIndex){
118378 int i;
118379 assert( zColl!=0 );
118380 for(i=0; i<pIndex->nColumn; i++){
118381 const char *z = pIndex->azColl[i];
118382 assert( z!=0 || pIndex->aiColumn[i]<0 );
118383 if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(zLeft: z, zRight: zColl) ){
118384 return 1;
118385 }
118386 }
118387 return 0;
118388}
118389#endif
118390
118391/*
118392** Recompute all indices of pTab that use the collating sequence pColl.
118393** If pColl==0 then recompute all indices of pTab.
118394*/
118395#ifndef SQLITE_OMIT_REINDEX
118396static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){
118397 if( !IsVirtual(pTab) ){
118398 Index *pIndex; /* An index associated with pTab */
118399
118400 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
118401 if( zColl==0 || collationMatch(zColl, pIndex) ){
118402 int iDb = sqlite3SchemaToIndex(db: pParse->db, pTab->pSchema);
118403 sqlite3BeginWriteOperation(pParse, setStatement: 0, iDb);
118404 sqlite3RefillIndex(pParse, pIndex, memRootPage: -1);
118405 }
118406 }
118407 }
118408}
118409#endif
118410
118411/*
118412** Recompute all indices of all tables in all databases where the
118413** indices use the collating sequence pColl. If pColl==0 then recompute
118414** all indices everywhere.
118415*/
118416#ifndef SQLITE_OMIT_REINDEX
118417static void reindexDatabases(Parse *pParse, char const *zColl){
118418 Db *pDb; /* A single database */
118419 int iDb; /* The database index number */
118420 sqlite3 *db = pParse->db; /* The database connection */
118421 HashElem *k; /* For looping over tables in pDb */
118422 Table *pTab; /* A table in the database */
118423
118424 assert( sqlite3BtreeHoldsAllMutexes(db) ); /* Needed for schema access */
118425 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
118426 assert( pDb!=0 );
118427 for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){
118428 pTab = (Table*)sqliteHashData(k);
118429 reindexTable(pParse, pTab, zColl);
118430 }
118431 }
118432}
118433#endif
118434
118435/*
118436** Generate code for the REINDEX command.
118437**
118438** REINDEX -- 1
118439** REINDEX <collation> -- 2
118440** REINDEX ?<database>.?<tablename> -- 3
118441** REINDEX ?<database>.?<indexname> -- 4
118442**
118443** Form 1 causes all indices in all attached databases to be rebuilt.
118444** Form 2 rebuilds all indices in all databases that use the named
118445** collating function. Forms 3 and 4 rebuild the named index or all
118446** indices associated with the named table.
118447*/
118448#ifndef SQLITE_OMIT_REINDEX
118449SQLITE_PRIVATE void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
118450 CollSeq *pColl; /* Collating sequence to be reindexed, or NULL */
118451 char *z; /* Name of a table or index */
118452 const char *zDb; /* Name of the database */
118453 Table *pTab; /* A table in the database */
118454 Index *pIndex; /* An index associated with pTab */
118455 int iDb; /* The database index number */
118456 sqlite3 *db = pParse->db; /* The database connection */
118457 Token *pObjName; /* Name of the table or index to be reindexed */
118458
118459 /* Read the database schema. If an error occurs, leave an error message
118460 ** and code in pParse and return NULL. */
118461 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
118462 return;
118463 }
118464
118465 if( pName1==0 ){
118466 reindexDatabases(pParse, zColl: 0);
118467 return;
118468 }else if( NEVER(pName2==0) || pName2->z==0 ){
118469 char *zColl;
118470 assert( pName1->z );
118471 zColl = sqlite3NameFromToken(db: pParse->db, pName: pName1);
118472 if( !zColl ) return;
118473 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
118474 if( pColl ){
118475 reindexDatabases(pParse, zColl);
118476 sqlite3DbFree(db, p: zColl);
118477 return;
118478 }
118479 sqlite3DbFree(db, p: zColl);
118480 }
118481 iDb = sqlite3TwoPartName(pParse, pName1, pName2, pUnqual: &pObjName);
118482 if( iDb<0 ) return;
118483 z = sqlite3NameFromToken(db, pName: pObjName);
118484 if( z==0 ) return;
118485 zDb = db->aDb[iDb].zDbSName;
118486 pTab = sqlite3FindTable(db, zName: z, zDatabase: zDb);
118487 if( pTab ){
118488 reindexTable(pParse, pTab, zColl: 0);
118489 sqlite3DbFree(db, p: z);
118490 return;
118491 }
118492 pIndex = sqlite3FindIndex(db, zName: z, zDb);
118493 sqlite3DbFree(db, p: z);
118494 if( pIndex ){
118495 sqlite3BeginWriteOperation(pParse, setStatement: 0, iDb);
118496 sqlite3RefillIndex(pParse, pIndex, memRootPage: -1);
118497 return;
118498 }
118499 sqlite3ErrorMsg(pParse, zFormat: "unable to identify the object to be reindexed");
118500}
118501#endif
118502
118503/*
118504** Return a KeyInfo structure that is appropriate for the given Index.
118505**
118506** The caller should invoke sqlite3KeyInfoUnref() on the returned object
118507** when it has finished using it.
118508*/
118509SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){
118510 int i;
118511 int nCol = pIdx->nColumn;
118512 int nKey = pIdx->nKeyCol;
118513 KeyInfo *pKey;
118514 if( pParse->nErr ) return 0;
118515 if( pIdx->uniqNotNull ){
118516 pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey);
118517 }else{
118518 pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
118519 }
118520 if( pKey ){
118521 assert( sqlite3KeyInfoIsWriteable(pKey) );
118522 for(i=0; i<nCol; i++){
118523 const char *zColl = pIdx->azColl[i];
118524 pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 :
118525 sqlite3LocateCollSeq(pParse, zName: zColl);
118526 pKey->aSortFlags[i] = pIdx->aSortOrder[i];
118527 assert( 0==(pKey->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) );
118528 }
118529 if( pParse->nErr ){
118530 assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ );
118531 if( pIdx->bNoQuery==0 ){
118532 /* Deactivate the index because it contains an unknown collating
118533 ** sequence. The only way to reactive the index is to reload the
118534 ** schema. Adding the missing collating sequence later does not
118535 ** reactive the index. The application had the chance to register
118536 ** the missing index using the collation-needed callback. For
118537 ** simplicity, SQLite will not give the application a second chance.
118538 */
118539 pIdx->bNoQuery = 1;
118540 pParse->rc = SQLITE_ERROR_RETRY;
118541 }
118542 sqlite3KeyInfoUnref(pKey);
118543 pKey = 0;
118544 }
118545 }
118546 return pKey;
118547}
118548
118549#ifndef SQLITE_OMIT_CTE
118550/*
118551** Create a new CTE object
118552*/
118553SQLITE_PRIVATE Cte *sqlite3CteNew(
118554 Parse *pParse, /* Parsing context */
118555 Token *pName, /* Name of the common-table */
118556 ExprList *pArglist, /* Optional column name list for the table */
118557 Select *pQuery, /* Query used to initialize the table */
118558 u8 eM10d /* The MATERIALIZED flag */
118559){
118560 Cte *pNew;
118561 sqlite3 *db = pParse->db;
118562
118563 pNew = sqlite3DbMallocZero(db, n: sizeof(*pNew));
118564 assert( pNew!=0 || db->mallocFailed );
118565
118566 if( db->mallocFailed ){
118567 sqlite3ExprListDelete(db, pList: pArglist);
118568 sqlite3SelectDelete(db, pQuery);
118569 }else{
118570 pNew->pSelect = pQuery;
118571 pNew->pCols = pArglist;
118572 pNew->zName = sqlite3NameFromToken(db: pParse->db, pName);
118573 pNew->eM10d = eM10d;
118574 }
118575 return pNew;
118576}
118577
118578/*
118579** Clear information from a Cte object, but do not deallocate storage
118580** for the object itself.
118581*/
118582static void cteClear(sqlite3 *db, Cte *pCte){
118583 assert( pCte!=0 );
118584 sqlite3ExprListDelete(db, pList: pCte->pCols);
118585 sqlite3SelectDelete(db, pCte->pSelect);
118586 sqlite3DbFree(db, p: pCte->zName);
118587}
118588
118589/*
118590** Free the contents of the CTE object passed as the second argument.
118591*/
118592SQLITE_PRIVATE void sqlite3CteDelete(sqlite3 *db, Cte *pCte){
118593 assert( pCte!=0 );
118594 cteClear(db, pCte);
118595 sqlite3DbFree(db, p: pCte);
118596}
118597
118598/*
118599** This routine is invoked once per CTE by the parser while parsing a
118600** WITH clause. The CTE described by teh third argument is added to
118601** the WITH clause of the second argument. If the second argument is
118602** NULL, then a new WITH argument is created.
118603*/
118604SQLITE_PRIVATE With *sqlite3WithAdd(
118605 Parse *pParse, /* Parsing context */
118606 With *pWith, /* Existing WITH clause, or NULL */
118607 Cte *pCte /* CTE to add to the WITH clause */
118608){
118609 sqlite3 *db = pParse->db;
118610 With *pNew;
118611 char *zName;
118612
118613 if( pCte==0 ){
118614 return pWith;
118615 }
118616
118617 /* Check that the CTE name is unique within this WITH clause. If
118618 ** not, store an error in the Parse structure. */
118619 zName = pCte->zName;
118620 if( zName && pWith ){
118621 int i;
118622 for(i=0; i<pWith->nCte; i++){
118623 if( sqlite3StrICmp(zLeft: zName, zRight: pWith->a[i].zName)==0 ){
118624 sqlite3ErrorMsg(pParse, zFormat: "duplicate WITH table name: %s", zName);
118625 }
118626 }
118627 }
118628
118629 if( pWith ){
118630 sqlite3_int64 nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
118631 pNew = sqlite3DbRealloc(db, p: pWith, n: nByte);
118632 }else{
118633 pNew = sqlite3DbMallocZero(db, n: sizeof(*pWith));
118634 }
118635 assert( (pNew!=0 && zName!=0) || db->mallocFailed );
118636
118637 if( db->mallocFailed ){
118638 sqlite3CteDelete(db, pCte);
118639 pNew = pWith;
118640 }else{
118641 pNew->a[pNew->nCte++] = *pCte;
118642 sqlite3DbFree(db, p: pCte);
118643 }
118644
118645 return pNew;
118646}
118647
118648/*
118649** Free the contents of the With object passed as the second argument.
118650*/
118651SQLITE_PRIVATE void sqlite3WithDelete(sqlite3 *db, With *pWith){
118652 if( pWith ){
118653 int i;
118654 for(i=0; i<pWith->nCte; i++){
118655 cteClear(db, pCte: &pWith->a[i]);
118656 }
118657 sqlite3DbFree(db, p: pWith);
118658 }
118659}
118660#endif /* !defined(SQLITE_OMIT_CTE) */
118661
118662/************** End of build.c ***********************************************/
118663/************** Begin file callback.c ****************************************/
118664/*
118665** 2005 May 23
118666**
118667** The author disclaims copyright to this source code. In place of
118668** a legal notice, here is a blessing:
118669**
118670** May you do good and not evil.
118671** May you find forgiveness for yourself and forgive others.
118672** May you share freely, never taking more than you give.
118673**
118674*************************************************************************
118675**
118676** This file contains functions used to access the internal hash tables
118677** of user defined functions and collation sequences.
118678*/
118679
118680/* #include "sqliteInt.h" */
118681
118682/*
118683** Invoke the 'collation needed' callback to request a collation sequence
118684** in the encoding enc of name zName, length nName.
118685*/
118686static void callCollNeeded(sqlite3 *db, int enc, const char *zName){
118687 assert( !db->xCollNeeded || !db->xCollNeeded16 );
118688 if( db->xCollNeeded ){
118689 char *zExternal = sqlite3DbStrDup(db, z: zName);
118690 if( !zExternal ) return;
118691 db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
118692 sqlite3DbFree(db, p: zExternal);
118693 }
118694#ifndef SQLITE_OMIT_UTF16
118695 if( db->xCollNeeded16 ){
118696 char const *zExternal;
118697 sqlite3_value *pTmp = sqlite3ValueNew(db);
118698 sqlite3ValueSetStr(v: pTmp, n: -1, z: zName, SQLITE_UTF8, SQLITE_STATIC);
118699 zExternal = sqlite3ValueText(pVal: pTmp, SQLITE_UTF16NATIVE);
118700 if( zExternal ){
118701 db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
118702 }
118703 sqlite3ValueFree(v: pTmp);
118704 }
118705#endif
118706}
118707
118708/*
118709** This routine is called if the collation factory fails to deliver a
118710** collation function in the best encoding but there may be other versions
118711** of this collation function (for other text encodings) available. Use one
118712** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
118713** possible.
118714*/
118715static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
118716 CollSeq *pColl2;
118717 char *z = pColl->zName;
118718 int i;
118719 static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
118720 for(i=0; i<3; i++){
118721 pColl2 = sqlite3FindCollSeq(db, enc: aEnc[i], z, 0);
118722 if( pColl2->xCmp!=0 ){
118723 memcpy(dest: pColl, src: pColl2, n: sizeof(CollSeq));
118724 pColl->xDel = 0; /* Do not copy the destructor */
118725 return SQLITE_OK;
118726 }
118727 }
118728 return SQLITE_ERROR;
118729}
118730
118731/*
118732** This routine is called on a collation sequence before it is used to
118733** check that it is defined. An undefined collation sequence exists when
118734** a database is loaded that contains references to collation sequences
118735** that have not been defined by sqlite3_create_collation() etc.
118736**
118737** If required, this routine calls the 'collation needed' callback to
118738** request a definition of the collating sequence. If this doesn't work,
118739** an equivalent collating sequence that uses a text encoding different
118740** from the main database is substituted, if one is available.
118741*/
118742SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
118743 if( pColl && pColl->xCmp==0 ){
118744 const char *zName = pColl->zName;
118745 sqlite3 *db = pParse->db;
118746 CollSeq *p = sqlite3GetCollSeq(pParse, ENC(db), pColl, zName);
118747 if( !p ){
118748 return SQLITE_ERROR;
118749 }
118750 assert( p==pColl );
118751 }
118752 return SQLITE_OK;
118753}
118754
118755
118756
118757/*
118758** Locate and return an entry from the db.aCollSeq hash table. If the entry
118759** specified by zName and nName is not found and parameter 'create' is
118760** true, then create a new entry. Otherwise return NULL.
118761**
118762** Each pointer stored in the sqlite3.aCollSeq hash table contains an
118763** array of three CollSeq structures. The first is the collation sequence
118764** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
118765**
118766** Stored immediately after the three collation sequences is a copy of
118767** the collation sequence name. A pointer to this string is stored in
118768** each collation sequence structure.
118769*/
118770static CollSeq *findCollSeqEntry(
118771 sqlite3 *db, /* Database connection */
118772 const char *zName, /* Name of the collating sequence */
118773 int create /* Create a new entry if true */
118774){
118775 CollSeq *pColl;
118776 pColl = sqlite3HashFind(pH: &db->aCollSeq, pKey: zName);
118777
118778 if( 0==pColl && create ){
118779 int nName = sqlite3Strlen30(z: zName) + 1;
118780 pColl = sqlite3DbMallocZero(db, n: 3*sizeof(*pColl) + nName);
118781 if( pColl ){
118782 CollSeq *pDel = 0;
118783 pColl[0].zName = (char*)&pColl[3];
118784 pColl[0].enc = SQLITE_UTF8;
118785 pColl[1].zName = (char*)&pColl[3];
118786 pColl[1].enc = SQLITE_UTF16LE;
118787 pColl[2].zName = (char*)&pColl[3];
118788 pColl[2].enc = SQLITE_UTF16BE;
118789 memcpy(dest: pColl[0].zName, src: zName, n: nName);
118790 pDel = sqlite3HashInsert(pH: &db->aCollSeq, pKey: pColl[0].zName, data: pColl);
118791
118792 /* If a malloc() failure occurred in sqlite3HashInsert(), it will
118793 ** return the pColl pointer to be deleted (because it wasn't added
118794 ** to the hash table).
118795 */
118796 assert( pDel==0 || pDel==pColl );
118797 if( pDel!=0 ){
118798 sqlite3OomFault(db);
118799 sqlite3DbFree(db, p: pDel);
118800 pColl = 0;
118801 }
118802 }
118803 }
118804 return pColl;
118805}
118806
118807/*
118808** Parameter zName points to a UTF-8 encoded string nName bytes long.
118809** Return the CollSeq* pointer for the collation sequence named zName
118810** for the encoding 'enc' from the database 'db'.
118811**
118812** If the entry specified is not found and 'create' is true, then create a
118813** new entry. Otherwise return NULL.
118814**
118815** A separate function sqlite3LocateCollSeq() is a wrapper around
118816** this routine. sqlite3LocateCollSeq() invokes the collation factory
118817** if necessary and generates an error message if the collating sequence
118818** cannot be found.
118819**
118820** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq()
118821*/
118822SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(
118823 sqlite3 *db, /* Database connection to search */
118824 u8 enc, /* Desired text encoding */
118825 const char *zName, /* Name of the collating sequence. Might be NULL */
118826 int create /* True to create CollSeq if doesn't already exist */
118827){
118828 CollSeq *pColl;
118829 assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
118830 assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
118831 if( zName ){
118832 pColl = findCollSeqEntry(db, zName, create);
118833 if( pColl ) pColl += enc-1;
118834 }else{
118835 pColl = db->pDfltColl;
118836 }
118837 return pColl;
118838}
118839
118840/*
118841** Change the text encoding for a database connection. This means that
118842** the pDfltColl must change as well.
118843*/
118844SQLITE_PRIVATE void sqlite3SetTextEncoding(sqlite3 *db, u8 enc){
118845 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
118846 db->enc = enc;
118847 /* EVIDENCE-OF: R-08308-17224 The default collating function for all
118848 ** strings is BINARY.
118849 */
118850 db->pDfltColl = sqlite3FindCollSeq(db, enc, zName: sqlite3StrBINARY, create: 0);
118851}
118852
118853/*
118854** This function is responsible for invoking the collation factory callback
118855** or substituting a collation sequence of a different encoding when the
118856** requested collation sequence is not available in the desired encoding.
118857**
118858** If it is not NULL, then pColl must point to the database native encoding
118859** collation sequence with name zName, length nName.
118860**
118861** The return value is either the collation sequence to be used in database
118862** db for collation type name zName, length nName, or NULL, if no collation
118863** sequence can be found. If no collation is found, leave an error message.
118864**
118865** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
118866*/
118867SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(
118868 Parse *pParse, /* Parsing context */
118869 u8 enc, /* The desired encoding for the collating sequence */
118870 CollSeq *pColl, /* Collating sequence with native encoding, or NULL */
118871 const char *zName /* Collating sequence name */
118872){
118873 CollSeq *p;
118874 sqlite3 *db = pParse->db;
118875
118876 p = pColl;
118877 if( !p ){
118878 p = sqlite3FindCollSeq(db, enc, zName, create: 0);
118879 }
118880 if( !p || !p->xCmp ){
118881 /* No collation sequence of this type for this encoding is registered.
118882 ** Call the collation factory to see if it can supply us with one.
118883 */
118884 callCollNeeded(db, enc, zName);
118885 p = sqlite3FindCollSeq(db, enc, zName, create: 0);
118886 }
118887 if( p && !p->xCmp && synthCollSeq(db, pColl: p) ){
118888 p = 0;
118889 }
118890 assert( !p || p->xCmp );
118891 if( p==0 ){
118892 sqlite3ErrorMsg(pParse, zFormat: "no such collation sequence: %s", zName);
118893 pParse->rc = SQLITE_ERROR_MISSING_COLLSEQ;
118894 }
118895 return p;
118896}
118897
118898/*
118899** This function returns the collation sequence for database native text
118900** encoding identified by the string zName.
118901**
118902** If the requested collation sequence is not available, or not available
118903** in the database native encoding, the collation factory is invoked to
118904** request it. If the collation factory does not supply such a sequence,
118905** and the sequence is available in another text encoding, then that is
118906** returned instead.
118907**
118908** If no versions of the requested collations sequence are available, or
118909** another error occurs, NULL is returned and an error message written into
118910** pParse.
118911**
118912** This routine is a wrapper around sqlite3FindCollSeq(). This routine
118913** invokes the collation factory if the named collation cannot be found
118914** and generates an error message.
118915**
118916** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq()
118917*/
118918SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){
118919 sqlite3 *db = pParse->db;
118920 u8 enc = ENC(db);
118921 u8 initbusy = db->init.busy;
118922 CollSeq *pColl;
118923
118924 pColl = sqlite3FindCollSeq(db, enc, zName, create: initbusy);
118925 if( !initbusy && (!pColl || !pColl->xCmp) ){
118926 pColl = sqlite3GetCollSeq(pParse, enc, pColl, zName);
118927 }
118928
118929 return pColl;
118930}
118931
118932/* During the search for the best function definition, this procedure
118933** is called to test how well the function passed as the first argument
118934** matches the request for a function with nArg arguments in a system
118935** that uses encoding enc. The value returned indicates how well the
118936** request is matched. A higher value indicates a better match.
118937**
118938** If nArg is -1 that means to only return a match (non-zero) if p->nArg
118939** is also -1. In other words, we are searching for a function that
118940** takes a variable number of arguments.
118941**
118942** If nArg is -2 that means that we are searching for any function
118943** regardless of the number of arguments it uses, so return a positive
118944** match score for any
118945**
118946** The returned value is always between 0 and 6, as follows:
118947**
118948** 0: Not a match.
118949** 1: UTF8/16 conversion required and function takes any number of arguments.
118950** 2: UTF16 byte order change required and function takes any number of args.
118951** 3: encoding matches and function takes any number of arguments
118952** 4: UTF8/16 conversion required - argument count matches exactly
118953** 5: UTF16 byte order conversion required - argument count matches exactly
118954** 6: Perfect match: encoding and argument count match exactly.
118955**
118956** If nArg==(-2) then any function with a non-null xSFunc is
118957** a perfect match and any function with xSFunc NULL is
118958** a non-match.
118959*/
118960#define FUNC_PERFECT_MATCH 6 /* The score for a perfect match */
118961static int matchQuality(
118962 FuncDef *p, /* The function we are evaluating for match quality */
118963 int nArg, /* Desired number of arguments. (-1)==any */
118964 u8 enc /* Desired text encoding */
118965){
118966 int match;
118967 assert( p->nArg>=-1 );
118968
118969 /* Wrong number of arguments means "no match" */
118970 if( p->nArg!=nArg ){
118971 if( nArg==(-2) ) return (p->xSFunc==0) ? 0 : FUNC_PERFECT_MATCH;
118972 if( p->nArg>=0 ) return 0;
118973 }
118974
118975 /* Give a better score to a function with a specific number of arguments
118976 ** than to function that accepts any number of arguments. */
118977 if( p->nArg==nArg ){
118978 match = 4;
118979 }else{
118980 match = 1;
118981 }
118982
118983 /* Bonus points if the text encoding matches */
118984 if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){
118985 match += 2; /* Exact encoding match */
118986 }else if( (enc & p->funcFlags & 2)!=0 ){
118987 match += 1; /* Both are UTF16, but with different byte orders */
118988 }
118989
118990 return match;
118991}
118992
118993/*
118994** Search a FuncDefHash for a function with the given name. Return
118995** a pointer to the matching FuncDef if found, or 0 if there is no match.
118996*/
118997SQLITE_PRIVATE FuncDef *sqlite3FunctionSearch(
118998 int h, /* Hash of the name */
118999 const char *zFunc /* Name of function */
119000){
119001 FuncDef *p;
119002 for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
119003 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
119004 if( sqlite3StrICmp(zLeft: p->zName, zRight: zFunc)==0 ){
119005 return p;
119006 }
119007 }
119008 return 0;
119009}
119010
119011/*
119012** Insert a new FuncDef into a FuncDefHash hash table.
119013*/
119014SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(
119015 FuncDef *aDef, /* List of global functions to be inserted */
119016 int nDef /* Length of the apDef[] list */
119017){
119018 int i;
119019 for(i=0; i<nDef; i++){
119020 FuncDef *pOther;
119021 const char *zName = aDef[i].zName;
119022 int nName = sqlite3Strlen30(z: zName);
119023 int h = SQLITE_FUNC_HASH(zName[0], nName);
119024 assert( zName[0]>='a' && zName[0]<='z' );
119025 assert( aDef[i].funcFlags & SQLITE_FUNC_BUILTIN );
119026 pOther = sqlite3FunctionSearch(h, zFunc: zName);
119027 if( pOther ){
119028 assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] );
119029 aDef[i].pNext = pOther->pNext;
119030 pOther->pNext = &aDef[i];
119031 }else{
119032 aDef[i].pNext = 0;
119033 aDef[i].u.pHash = sqlite3BuiltinFunctions.a[h];
119034 sqlite3BuiltinFunctions.a[h] = &aDef[i];
119035 }
119036 }
119037}
119038
119039
119040
119041/*
119042** Locate a user function given a name, a number of arguments and a flag
119043** indicating whether the function prefers UTF-16 over UTF-8. Return a
119044** pointer to the FuncDef structure that defines that function, or return
119045** NULL if the function does not exist.
119046**
119047** If the createFlag argument is true, then a new (blank) FuncDef
119048** structure is created and liked into the "db" structure if a
119049** no matching function previously existed.
119050**
119051** If nArg is -2, then the first valid function found is returned. A
119052** function is valid if xSFunc is non-zero. The nArg==(-2)
119053** case is used to see if zName is a valid function name for some number
119054** of arguments. If nArg is -2, then createFlag must be 0.
119055**
119056** If createFlag is false, then a function with the required name and
119057** number of arguments may be returned even if the eTextRep flag does not
119058** match that requested.
119059*/
119060SQLITE_PRIVATE FuncDef *sqlite3FindFunction(
119061 sqlite3 *db, /* An open database */
119062 const char *zName, /* Name of the function. zero-terminated */
119063 int nArg, /* Number of arguments. -1 means any number */
119064 u8 enc, /* Preferred text encoding */
119065 u8 createFlag /* Create new entry if true and does not otherwise exist */
119066){
119067 FuncDef *p; /* Iterator variable */
119068 FuncDef *pBest = 0; /* Best match found so far */
119069 int bestScore = 0; /* Score of best match */
119070 int h; /* Hash value */
119071 int nName; /* Length of the name */
119072
119073 assert( nArg>=(-2) );
119074 assert( nArg>=(-1) || createFlag==0 );
119075 nName = sqlite3Strlen30(z: zName);
119076
119077 /* First search for a match amongst the application-defined functions.
119078 */
119079 p = (FuncDef*)sqlite3HashFind(pH: &db->aFunc, pKey: zName);
119080 while( p ){
119081 int score = matchQuality(p, nArg, enc);
119082 if( score>bestScore ){
119083 pBest = p;
119084 bestScore = score;
119085 }
119086 p = p->pNext;
119087 }
119088
119089 /* If no match is found, search the built-in functions.
119090 **
119091 ** If the DBFLAG_PreferBuiltin flag is set, then search the built-in
119092 ** functions even if a prior app-defined function was found. And give
119093 ** priority to built-in functions.
119094 **
119095 ** Except, if createFlag is true, that means that we are trying to
119096 ** install a new function. Whatever FuncDef structure is returned it will
119097 ** have fields overwritten with new information appropriate for the
119098 ** new function. But the FuncDefs for built-in functions are read-only.
119099 ** So we must not search for built-ins when creating a new function.
119100 */
119101 if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin)!=0) ){
119102 bestScore = 0;
119103 h = SQLITE_FUNC_HASH(sqlite3UpperToLower[(u8)zName[0]], nName);
119104 p = sqlite3FunctionSearch(h, zFunc: zName);
119105 while( p ){
119106 int score = matchQuality(p, nArg, enc);
119107 if( score>bestScore ){
119108 pBest = p;
119109 bestScore = score;
119110 }
119111 p = p->pNext;
119112 }
119113 }
119114
119115 /* If the createFlag parameter is true and the search did not reveal an
119116 ** exact match for the name, number of arguments and encoding, then add a
119117 ** new entry to the hash table and return it.
119118 */
119119 if( createFlag && bestScore<FUNC_PERFECT_MATCH &&
119120 (pBest = sqlite3DbMallocZero(db, n: sizeof(*pBest)+nName+1))!=0 ){
119121 FuncDef *pOther;
119122 u8 *z;
119123 pBest->zName = (const char*)&pBest[1];
119124 pBest->nArg = (u16)nArg;
119125 pBest->funcFlags = enc;
119126 memcpy(dest: (char*)&pBest[1], src: zName, n: nName+1);
119127 for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z];
119128 pOther = (FuncDef*)sqlite3HashInsert(pH: &db->aFunc, pKey: pBest->zName, data: pBest);
119129 if( pOther==pBest ){
119130 sqlite3DbFree(db, p: pBest);
119131 sqlite3OomFault(db);
119132 return 0;
119133 }else{
119134 pBest->pNext = pOther;
119135 }
119136 }
119137
119138 if( pBest && (pBest->xSFunc || createFlag) ){
119139 return pBest;
119140 }
119141 return 0;
119142}
119143
119144/*
119145** Free all resources held by the schema structure. The void* argument points
119146** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the
119147** pointer itself, it just cleans up subsidiary resources (i.e. the contents
119148** of the schema hash tables).
119149**
119150** The Schema.cache_size variable is not cleared.
119151*/
119152SQLITE_PRIVATE void sqlite3SchemaClear(void *p){
119153 Hash temp1;
119154 Hash temp2;
119155 HashElem *pElem;
119156 Schema *pSchema = (Schema *)p;
119157
119158 temp1 = pSchema->tblHash;
119159 temp2 = pSchema->trigHash;
119160 sqlite3HashInit(pNew: &pSchema->trigHash);
119161 sqlite3HashClear(pH: &pSchema->idxHash);
119162 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
119163 sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem));
119164 }
119165 sqlite3HashClear(pH: &temp2);
119166 sqlite3HashInit(pNew: &pSchema->tblHash);
119167 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
119168 Table *pTab = sqliteHashData(pElem);
119169 sqlite3DeleteTable(db: 0, pTable: pTab);
119170 }
119171 sqlite3HashClear(pH: &temp1);
119172 sqlite3HashClear(pH: &pSchema->fkeyHash);
119173 pSchema->pSeqTab = 0;
119174 if( pSchema->schemaFlags & DB_SchemaLoaded ){
119175 pSchema->iGeneration++;
119176 }
119177 pSchema->schemaFlags &= ~(DB_SchemaLoaded|DB_ResetWanted);
119178}
119179
119180/*
119181** Find and return the schema associated with a BTree. Create
119182** a new one if necessary.
119183*/
119184SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
119185 Schema * p;
119186 if( pBt ){
119187 p = (Schema *)sqlite3BtreeSchema(p: pBt, nBytes: sizeof(Schema), xFree: sqlite3SchemaClear);
119188 }else{
119189 p = (Schema *)sqlite3DbMallocZero(db: 0, n: sizeof(Schema));
119190 }
119191 if( !p ){
119192 sqlite3OomFault(db);
119193 }else if ( 0==p->file_format ){
119194 sqlite3HashInit(pNew: &p->tblHash);
119195 sqlite3HashInit(pNew: &p->idxHash);
119196 sqlite3HashInit(pNew: &p->trigHash);
119197 sqlite3HashInit(pNew: &p->fkeyHash);
119198 p->enc = SQLITE_UTF8;
119199 }
119200 return p;
119201}
119202
119203/************** End of callback.c ********************************************/
119204/************** Begin file delete.c ******************************************/
119205/*
119206** 2001 September 15
119207**
119208** The author disclaims copyright to this source code. In place of
119209** a legal notice, here is a blessing:
119210**
119211** May you do good and not evil.
119212** May you find forgiveness for yourself and forgive others.
119213** May you share freely, never taking more than you give.
119214**
119215*************************************************************************
119216** This file contains C code routines that are called by the parser
119217** in order to generate code for DELETE FROM statements.
119218*/
119219/* #include "sqliteInt.h" */
119220
119221/*
119222** While a SrcList can in general represent multiple tables and subqueries
119223** (as in the FROM clause of a SELECT statement) in this case it contains
119224** the name of a single table, as one might find in an INSERT, DELETE,
119225** or UPDATE statement. Look up that table in the symbol table and
119226** return a pointer. Set an error message and return NULL if the table
119227** name is not found or if any other error occurs.
119228**
119229** The following fields are initialized appropriate in pSrc:
119230**
119231** pSrc->a[0].pTab Pointer to the Table object
119232** pSrc->a[0].pIndex Pointer to the INDEXED BY index, if there is one
119233**
119234*/
119235SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
119236 SrcItem *pItem = pSrc->a;
119237 Table *pTab;
119238 assert( pItem && pSrc->nSrc>=1 );
119239 pTab = sqlite3LocateTableItem(pParse, flags: 0, p: pItem);
119240 sqlite3DeleteTable(db: pParse->db, pTable: pItem->pTab);
119241 pItem->pTab = pTab;
119242 if( pTab ){
119243 pTab->nTabRef++;
119244 if( pItem->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pItem) ){
119245 pTab = 0;
119246 }
119247 }
119248 return pTab;
119249}
119250
119251/* Return true if table pTab is read-only.
119252**
119253** A table is read-only if any of the following are true:
119254**
119255** 1) It is a virtual table and no implementation of the xUpdate method
119256** has been provided
119257**
119258** 2) It is a system table (i.e. sqlite_schema), this call is not
119259** part of a nested parse and writable_schema pragma has not
119260** been specified
119261**
119262** 3) The table is a shadow table, the database connection is in
119263** defensive mode, and the current sqlite3_prepare()
119264** is for a top-level SQL statement.
119265*/
119266static int tabIsReadOnly(Parse *pParse, Table *pTab){
119267 sqlite3 *db;
119268 if( IsVirtual(pTab) ){
119269 return sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0;
119270 }
119271 if( (pTab->tabFlags & (TF_Readonly|TF_Shadow))==0 ) return 0;
119272 db = pParse->db;
119273 if( (pTab->tabFlags & TF_Readonly)!=0 ){
119274 return sqlite3WritableSchema(db)==0 && pParse->nested==0;
119275 }
119276 assert( pTab->tabFlags & TF_Shadow );
119277 return sqlite3ReadOnlyShadowTables(db);
119278}
119279
119280/*
119281** Check to make sure the given table is writable. If it is not
119282** writable, generate an error message and return 1. If it is
119283** writable return 0;
119284*/
119285SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
119286 if( tabIsReadOnly(pParse, pTab) ){
119287 sqlite3ErrorMsg(pParse, zFormat: "table %s may not be modified", pTab->zName);
119288 return 1;
119289 }
119290#ifndef SQLITE_OMIT_VIEW
119291 if( !viewOk && IsView(pTab) ){
119292 sqlite3ErrorMsg(pParse,zFormat: "cannot modify %s because it is a view",pTab->zName);
119293 return 1;
119294 }
119295#endif
119296 return 0;
119297}
119298
119299
119300#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
119301/*
119302** Evaluate a view and store its result in an ephemeral table. The
119303** pWhere argument is an optional WHERE clause that restricts the
119304** set of rows in the view that are to be added to the ephemeral table.
119305*/
119306SQLITE_PRIVATE void sqlite3MaterializeView(
119307 Parse *pParse, /* Parsing context */
119308 Table *pView, /* View definition */
119309 Expr *pWhere, /* Optional WHERE clause to be added */
119310 ExprList *pOrderBy, /* Optional ORDER BY clause */
119311 Expr *pLimit, /* Optional LIMIT clause */
119312 int iCur /* Cursor number for ephemeral table */
119313){
119314 SelectDest dest;
119315 Select *pSel;
119316 SrcList *pFrom;
119317 sqlite3 *db = pParse->db;
119318 int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
119319 pWhere = sqlite3ExprDup(db, p: pWhere, flags: 0);
119320 pFrom = sqlite3SrcListAppend(pParse, pList: 0, pTable: 0, pDatabase: 0);
119321 if( pFrom ){
119322 assert( pFrom->nSrc==1 );
119323 pFrom->a[0].zName = sqlite3DbStrDup(db, z: pView->zName);
119324 pFrom->a[0].zDatabase = sqlite3DbStrDup(db, z: db->aDb[iDb].zDbSName);
119325 assert( pFrom->a[0].pOn==0 );
119326 assert( pFrom->a[0].pUsing==0 );
119327 }
119328 pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, pOrderBy,
119329 SF_IncludeHidden, pLimit);
119330 sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
119331 sqlite3Select(pParse, pSel, &dest);
119332 sqlite3SelectDelete(db, pSel);
119333}
119334#endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
119335
119336#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
119337/*
119338** Generate an expression tree to implement the WHERE, ORDER BY,
119339** and LIMIT/OFFSET portion of DELETE and UPDATE statements.
119340**
119341** DELETE FROM table_wxyz WHERE a<5 ORDER BY a LIMIT 1;
119342** \__________________________/
119343** pLimitWhere (pInClause)
119344*/
119345SQLITE_PRIVATE Expr *sqlite3LimitWhere(
119346 Parse *pParse, /* The parser context */
119347 SrcList *pSrc, /* the FROM clause -- which tables to scan */
119348 Expr *pWhere, /* The WHERE clause. May be null */
119349 ExprList *pOrderBy, /* The ORDER BY clause. May be null */
119350 Expr *pLimit, /* The LIMIT clause. May be null */
119351 char *zStmtType /* Either DELETE or UPDATE. For err msgs. */
119352){
119353 sqlite3 *db = pParse->db;
119354 Expr *pLhs = NULL; /* LHS of IN(SELECT...) operator */
119355 Expr *pInClause = NULL; /* WHERE rowid IN ( select ) */
119356 ExprList *pEList = NULL; /* Expression list contaning only pSelectRowid */
119357 SrcList *pSelectSrc = NULL; /* SELECT rowid FROM x ... (dup of pSrc) */
119358 Select *pSelect = NULL; /* Complete SELECT tree */
119359 Table *pTab;
119360
119361 /* Check that there isn't an ORDER BY without a LIMIT clause.
119362 */
119363 if( pOrderBy && pLimit==0 ) {
119364 sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
119365 sqlite3ExprDelete(pParse->db, pWhere);
119366 sqlite3ExprListDelete(pParse->db, pOrderBy);
119367 return 0;
119368 }
119369
119370 /* We only need to generate a select expression if there
119371 ** is a limit/offset term to enforce.
119372 */
119373 if( pLimit == 0 ) {
119374 return pWhere;
119375 }
119376
119377 /* Generate a select expression tree to enforce the limit/offset
119378 ** term for the DELETE or UPDATE statement. For example:
119379 ** DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
119380 ** becomes:
119381 ** DELETE FROM table_a WHERE rowid IN (
119382 ** SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
119383 ** );
119384 */
119385
119386 pTab = pSrc->a[0].pTab;
119387 if( HasRowid(pTab) ){
119388 pLhs = sqlite3PExpr(pParse, TK_ROW, 0, 0);
119389 pEList = sqlite3ExprListAppend(
119390 pParse, 0, sqlite3PExpr(pParse, TK_ROW, 0, 0)
119391 );
119392 }else{
119393 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
119394 if( pPk->nKeyCol==1 ){
119395 const char *zName = pTab->aCol[pPk->aiColumn[0]].zCnName;
119396 pLhs = sqlite3Expr(db, TK_ID, zName);
119397 pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ID, zName));
119398 }else{
119399 int i;
119400 for(i=0; i<pPk->nKeyCol; i++){
119401 Expr *p = sqlite3Expr(db, TK_ID, pTab->aCol[pPk->aiColumn[i]].zCnName);
119402 pEList = sqlite3ExprListAppend(pParse, pEList, p);
119403 }
119404 pLhs = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
119405 if( pLhs ){
119406 pLhs->x.pList = sqlite3ExprListDup(db, pEList, 0);
119407 }
119408 }
119409 }
119410
119411 /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree
119412 ** and the SELECT subtree. */
119413 pSrc->a[0].pTab = 0;
119414 pSelectSrc = sqlite3SrcListDup(db, pSrc, 0);
119415 pSrc->a[0].pTab = pTab;
119416 if( pSrc->a[0].fg.isIndexedBy ){
119417 assert( pSrc->a[0].fg.isCte==0 );
119418 pSrc->a[0].u2.pIBIndex = 0;
119419 pSrc->a[0].fg.isIndexedBy = 0;
119420 sqlite3DbFree(db, pSrc->a[0].u1.zIndexedBy);
119421 }else if( pSrc->a[0].fg.isCte ){
119422 pSrc->a[0].u2.pCteUse->nUse++;
119423 }
119424
119425 /* generate the SELECT expression tree. */
119426 pSelect = sqlite3SelectNew(pParse, pEList, pSelectSrc, pWhere, 0 ,0,
119427 pOrderBy,0,pLimit
119428 );
119429
119430 /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */
119431 pInClause = sqlite3PExpr(pParse, TK_IN, pLhs, 0);
119432 sqlite3PExprAddSelect(pParse, pInClause, pSelect);
119433 return pInClause;
119434}
119435#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) */
119436 /* && !defined(SQLITE_OMIT_SUBQUERY) */
119437
119438/*
119439** Generate code for a DELETE FROM statement.
119440**
119441** DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
119442** \________/ \________________/
119443** pTabList pWhere
119444*/
119445SQLITE_PRIVATE void sqlite3DeleteFrom(
119446 Parse *pParse, /* The parser context */
119447 SrcList *pTabList, /* The table from which we should delete things */
119448 Expr *pWhere, /* The WHERE clause. May be null */
119449 ExprList *pOrderBy, /* ORDER BY clause. May be null */
119450 Expr *pLimit /* LIMIT clause. May be null */
119451){
119452 Vdbe *v; /* The virtual database engine */
119453 Table *pTab; /* The table from which records will be deleted */
119454 int i; /* Loop counter */
119455 WhereInfo *pWInfo; /* Information about the WHERE clause */
119456 Index *pIdx; /* For looping over indices of the table */
119457 int iTabCur; /* Cursor number for the table */
119458 int iDataCur = 0; /* VDBE cursor for the canonical data source */
119459 int iIdxCur = 0; /* Cursor number of the first index */
119460 int nIdx; /* Number of indices */
119461 sqlite3 *db; /* Main database structure */
119462 AuthContext sContext; /* Authorization context */
119463 NameContext sNC; /* Name context to resolve expressions in */
119464 int iDb; /* Database number */
119465 int memCnt = 0; /* Memory cell used for change counting */
119466 int rcauth; /* Value returned by authorization callback */
119467 int eOnePass; /* ONEPASS_OFF or _SINGLE or _MULTI */
119468 int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */
119469 u8 *aToOpen = 0; /* Open cursor iTabCur+j if aToOpen[j] is true */
119470 Index *pPk; /* The PRIMARY KEY index on the table */
119471 int iPk = 0; /* First of nPk registers holding PRIMARY KEY value */
119472 i16 nPk = 1; /* Number of columns in the PRIMARY KEY */
119473 int iKey; /* Memory cell holding key of row to be deleted */
119474 i16 nKey; /* Number of memory cells in the row key */
119475 int iEphCur = 0; /* Ephemeral table holding all primary key values */
119476 int iRowSet = 0; /* Register for rowset of rows to delete */
119477 int addrBypass = 0; /* Address of jump over the delete logic */
119478 int addrLoop = 0; /* Top of the delete loop */
119479 int addrEphOpen = 0; /* Instruction to open the Ephemeral table */
119480 int bComplex; /* True if there are triggers or FKs or
119481 ** subqueries in the WHERE clause */
119482
119483#ifndef SQLITE_OMIT_TRIGGER
119484 int isView; /* True if attempting to delete from a view */
119485 Trigger *pTrigger; /* List of table triggers, if required */
119486#endif
119487
119488 memset(s: &sContext, c: 0, n: sizeof(sContext));
119489 db = pParse->db;
119490 if( pParse->nErr || db->mallocFailed ){
119491 goto delete_from_cleanup;
119492 }
119493 assert( pTabList->nSrc==1 );
119494
119495
119496 /* Locate the table which we want to delete. This table has to be
119497 ** put in an SrcList structure because some of the subroutines we
119498 ** will be calling are designed to work with multiple tables and expect
119499 ** an SrcList* parameter instead of just a Table* parameter.
119500 */
119501 pTab = sqlite3SrcListLookup(pParse, pSrc: pTabList);
119502 if( pTab==0 ) goto delete_from_cleanup;
119503
119504 /* Figure out if we have any triggers and if the table being
119505 ** deleted from is a view
119506 */
119507#ifndef SQLITE_OMIT_TRIGGER
119508 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, pMask: 0);
119509 isView = IsView(pTab);
119510#else
119511# define pTrigger 0
119512# define isView 0
119513#endif
119514 bComplex = pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0);
119515#ifdef SQLITE_OMIT_VIEW
119516# undef isView
119517# define isView 0
119518#endif
119519
119520#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
119521 if( !isView ){
119522 pWhere = sqlite3LimitWhere(
119523 pParse, pTabList, pWhere, pOrderBy, pLimit, "DELETE"
119524 );
119525 pOrderBy = 0;
119526 pLimit = 0;
119527 }
119528#endif
119529
119530 /* If pTab is really a view, make sure it has been initialized.
119531 */
119532 if( sqlite3ViewGetColumnNames(pParse, pTable: pTab) ){
119533 goto delete_from_cleanup;
119534 }
119535
119536 if( sqlite3IsReadOnly(pParse, pTab, viewOk: (pTrigger?1:0)) ){
119537 goto delete_from_cleanup;
119538 }
119539 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
119540 assert( iDb<db->nDb );
119541 rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, zArg1: pTab->zName, zArg2: 0,
119542 zArg3: db->aDb[iDb].zDbSName);
119543 assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE );
119544 if( rcauth==SQLITE_DENY ){
119545 goto delete_from_cleanup;
119546 }
119547 assert(!isView || pTrigger);
119548
119549 /* Assign cursor numbers to the table and all its indices.
119550 */
119551 assert( pTabList->nSrc==1 );
119552 iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
119553 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
119554 pParse->nTab++;
119555 }
119556
119557 /* Start the view context
119558 */
119559 if( isView ){
119560 sqlite3AuthContextPush(pParse, pContext: &sContext, zContext: pTab->zName);
119561 }
119562
119563 /* Begin generating code.
119564 */
119565 v = sqlite3GetVdbe(pParse);
119566 if( v==0 ){
119567 goto delete_from_cleanup;
119568 }
119569 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
119570 sqlite3BeginWriteOperation(pParse, setStatement: bComplex, iDb);
119571
119572 /* If we are trying to delete from a view, realize that view into
119573 ** an ephemeral table.
119574 */
119575#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
119576 if( isView ){
119577 sqlite3MaterializeView(pParse, pView: pTab,
119578 pWhere, pOrderBy, pLimit, iCur: iTabCur
119579 );
119580 iDataCur = iIdxCur = iTabCur;
119581 pOrderBy = 0;
119582 pLimit = 0;
119583 }
119584#endif
119585
119586 /* Resolve the column names in the WHERE clause.
119587 */
119588 memset(s: &sNC, c: 0, n: sizeof(sNC));
119589 sNC.pParse = pParse;
119590 sNC.pSrcList = pTabList;
119591 if( sqlite3ResolveExprNames(pNC: &sNC, pExpr: pWhere) ){
119592 goto delete_from_cleanup;
119593 }
119594
119595 /* Initialize the counter of the number of rows deleted, if
119596 ** we are counting rows.
119597 */
119598 if( (db->flags & SQLITE_CountRows)!=0
119599 && !pParse->nested
119600 && !pParse->pTriggerTab
119601 && !pParse->bReturning
119602 ){
119603 memCnt = ++pParse->nMem;
119604 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: memCnt);
119605 }
119606
119607#ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
119608 /* Special case: A DELETE without a WHERE clause deletes everything.
119609 ** It is easier just to erase the whole table. Prior to version 3.6.5,
119610 ** this optimization caused the row change count (the value returned by
119611 ** API function sqlite3_count_changes) to be set incorrectly.
119612 **
119613 ** The "rcauth==SQLITE_OK" terms is the
119614 ** IMPLEMENTATION-OF: R-17228-37124 If the action code is SQLITE_DELETE and
119615 ** the callback returns SQLITE_IGNORE then the DELETE operation proceeds but
119616 ** the truncate optimization is disabled and all rows are deleted
119617 ** individually.
119618 */
119619 if( rcauth==SQLITE_OK
119620 && pWhere==0
119621 && !bComplex
119622 && !IsVirtual(pTab)
119623#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
119624 && db->xPreUpdateCallback==0
119625#endif
119626 ){
119627 assert( !isView );
119628 sqlite3TableLock(pParse, iDb, iTab: pTab->tnum, isWriteLock: 1, zName: pTab->zName);
119629 if( HasRowid(pTab) ){
119630 sqlite3VdbeAddOp4(p: v, OP_Clear, p1: pTab->tnum, p2: iDb, p3: memCnt ? memCnt : -1,
119631 zP4: pTab->zName, P4_STATIC);
119632 }
119633 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
119634 assert( pIdx->pSchema==pTab->pSchema );
119635 sqlite3VdbeAddOp2(p: v, OP_Clear, p1: pIdx->tnum, p2: iDb);
119636 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
119637 sqlite3VdbeChangeP3(p: v, addr: -1, val: memCnt ? memCnt : -1);
119638 }
119639 }
119640 }else
119641#endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
119642 {
119643 u16 wcf = WHERE_ONEPASS_DESIRED|WHERE_DUPLICATES_OK;
119644 if( sNC.ncFlags & NC_VarSelect ) bComplex = 1;
119645 wcf |= (bComplex ? 0 : WHERE_ONEPASS_MULTIROW);
119646 if( HasRowid(pTab) ){
119647 /* For a rowid table, initialize the RowSet to an empty set */
119648 pPk = 0;
119649 nPk = 1;
119650 iRowSet = ++pParse->nMem;
119651 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: iRowSet);
119652 }else{
119653 /* For a WITHOUT ROWID table, create an ephemeral table used to
119654 ** hold all primary keys for rows to be deleted. */
119655 pPk = sqlite3PrimaryKeyIndex(pTab);
119656 assert( pPk!=0 );
119657 nPk = pPk->nKeyCol;
119658 iPk = pParse->nMem+1;
119659 pParse->nMem += nPk;
119660 iEphCur = pParse->nTab++;
119661 addrEphOpen = sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: iEphCur, p2: nPk);
119662 sqlite3VdbeSetP4KeyInfo(pParse, pIdx: pPk);
119663 }
119664
119665 /* Construct a query to find the rowid or primary key for every row
119666 ** to be deleted, based on the WHERE clause. Set variable eOnePass
119667 ** to indicate the strategy used to implement this delete:
119668 **
119669 ** ONEPASS_OFF: Two-pass approach - use a FIFO for rowids/PK values.
119670 ** ONEPASS_SINGLE: One-pass approach - at most one row deleted.
119671 ** ONEPASS_MULTI: One-pass approach - any number of rows may be deleted.
119672 */
119673 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, wcf, iTabCur+1);
119674 if( pWInfo==0 ) goto delete_from_cleanup;
119675 eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
119676 assert( IsVirtual(pTab)==0 || eOnePass!=ONEPASS_MULTI );
119677 assert( IsVirtual(pTab) || bComplex || eOnePass!=ONEPASS_OFF );
119678 if( eOnePass!=ONEPASS_SINGLE ) sqlite3MultiWrite(pParse);
119679 if( sqlite3WhereUsesDeferredSeek(pWInfo) ){
119680 sqlite3VdbeAddOp1(p: v, OP_FinishSeek, p1: iTabCur);
119681 }
119682
119683 /* Keep track of the number of rows to be deleted */
119684 if( memCnt ){
119685 sqlite3VdbeAddOp2(p: v, OP_AddImm, p1: memCnt, p2: 1);
119686 }
119687
119688 /* Extract the rowid or primary key for the current row */
119689 if( pPk ){
119690 for(i=0; i<nPk; i++){
119691 assert( pPk->aiColumn[i]>=0 );
119692 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
119693 iCol: pPk->aiColumn[i], regOut: iPk+i);
119694 }
119695 iKey = iPk;
119696 }else{
119697 iKey = ++pParse->nMem;
119698 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, iCol: -1, regOut: iKey);
119699 }
119700
119701 if( eOnePass!=ONEPASS_OFF ){
119702 /* For ONEPASS, no need to store the rowid/primary-key. There is only
119703 ** one, so just keep it in its register(s) and fall through to the
119704 ** delete code. */
119705 nKey = nPk; /* OP_Found will use an unpacked key */
119706 aToOpen = sqlite3DbMallocRawNN(db, n: nIdx+2);
119707 if( aToOpen==0 ){
119708 sqlite3WhereEnd(pWInfo);
119709 goto delete_from_cleanup;
119710 }
119711 memset(s: aToOpen, c: 1, n: nIdx+1);
119712 aToOpen[nIdx+1] = 0;
119713 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
119714 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
119715 if( addrEphOpen ) sqlite3VdbeChangeToNoop(p: v, addr: addrEphOpen);
119716 addrBypass = sqlite3VdbeMakeLabel(pParse);
119717 }else{
119718 if( pPk ){
119719 /* Add the PK key for this row to the temporary table */
119720 iKey = ++pParse->nMem;
119721 nKey = 0; /* Zero tells OP_Found to use a composite key */
119722 sqlite3VdbeAddOp4(p: v, OP_MakeRecord, p1: iPk, p2: nPk, p3: iKey,
119723 zP4: sqlite3IndexAffinityStr(pParse->db, pPk), p4type: nPk);
119724 sqlite3VdbeAddOp4Int(p: v, OP_IdxInsert, p1: iEphCur, p2: iKey, p3: iPk, p4: nPk);
119725 }else{
119726 /* Add the rowid of the row to be deleted to the RowSet */
119727 nKey = 1; /* OP_DeferredSeek always uses a single rowid */
119728 sqlite3VdbeAddOp2(p: v, OP_RowSetAdd, p1: iRowSet, p2: iKey);
119729 }
119730 sqlite3WhereEnd(pWInfo);
119731 }
119732
119733 /* Unless this is a view, open cursors for the table we are
119734 ** deleting from and all its indices. If this is a view, then the
119735 ** only effect this statement has is to fire the INSTEAD OF
119736 ** triggers.
119737 */
119738 if( !isView ){
119739 int iAddrOnce = 0;
119740 if( eOnePass==ONEPASS_MULTI ){
119741 iAddrOnce = sqlite3VdbeAddOp0(p: v, OP_Once); VdbeCoverage(v);
119742 }
119743 testcase( IsVirtual(pTab) );
119744 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, OPFLAG_FORDELETE,
119745 iTabCur, aToOpen, &iDataCur, &iIdxCur);
119746 assert( pPk || IsVirtual(pTab) || iDataCur==iTabCur );
119747 assert( pPk || IsVirtual(pTab) || iIdxCur==iDataCur+1 );
119748 if( eOnePass==ONEPASS_MULTI ){
119749 sqlite3VdbeJumpHereOrPopInst(p: v, addr: iAddrOnce);
119750 }
119751 }
119752
119753 /* Set up a loop over the rowids/primary-keys that were found in the
119754 ** where-clause loop above.
119755 */
119756 if( eOnePass!=ONEPASS_OFF ){
119757 assert( nKey==nPk ); /* OP_Found will use an unpacked key */
119758 if( !IsVirtual(pTab) && aToOpen[iDataCur-iTabCur] ){
119759 assert( pPk!=0 || IsView(pTab) );
119760 sqlite3VdbeAddOp4Int(p: v, OP_NotFound, p1: iDataCur, p2: addrBypass, p3: iKey, p4: nKey);
119761 VdbeCoverage(v);
119762 }
119763 }else if( pPk ){
119764 addrLoop = sqlite3VdbeAddOp1(p: v, OP_Rewind, p1: iEphCur); VdbeCoverage(v);
119765 if( IsVirtual(pTab) ){
119766 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iEphCur, p2: 0, p3: iKey);
119767 }else{
119768 sqlite3VdbeAddOp2(p: v, OP_RowData, p1: iEphCur, p2: iKey);
119769 }
119770 assert( nKey==0 ); /* OP_Found will use a composite key */
119771 }else{
119772 addrLoop = sqlite3VdbeAddOp3(p: v, OP_RowSetRead, p1: iRowSet, p2: 0, p3: iKey);
119773 VdbeCoverage(v);
119774 assert( nKey==1 );
119775 }
119776
119777 /* Delete the row */
119778#ifndef SQLITE_OMIT_VIRTUALTABLE
119779 if( IsVirtual(pTab) ){
119780 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
119781 sqlite3VtabMakeWritable(pParse, pTab);
119782 assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE );
119783 sqlite3MayAbort(pParse);
119784 if( eOnePass==ONEPASS_SINGLE ){
119785 sqlite3VdbeAddOp1(p: v, OP_Close, p1: iTabCur);
119786 if( sqlite3IsToplevel(pParse) ){
119787 pParse->isMultiWrite = 0;
119788 }
119789 }
119790 sqlite3VdbeAddOp4(p: v, OP_VUpdate, p1: 0, p2: 1, p3: iKey, zP4: pVTab, P4_VTAB);
119791 sqlite3VdbeChangeP5(p: v, OE_Abort);
119792 }else
119793#endif
119794 {
119795 int count = (pParse->nested==0); /* True to count changes */
119796 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
119797 iKey, nKey, count, OE_Default, eOnePass, aiCurOnePass[1]);
119798 }
119799
119800 /* End of the loop over all rowids/primary-keys. */
119801 if( eOnePass!=ONEPASS_OFF ){
119802 sqlite3VdbeResolveLabel(v, x: addrBypass);
119803 sqlite3WhereEnd(pWInfo);
119804 }else if( pPk ){
119805 sqlite3VdbeAddOp2(p: v, OP_Next, p1: iEphCur, p2: addrLoop+1); VdbeCoverage(v);
119806 sqlite3VdbeJumpHere(p: v, addr: addrLoop);
119807 }else{
119808 sqlite3VdbeGoto(p: v, iDest: addrLoop);
119809 sqlite3VdbeJumpHere(p: v, addr: addrLoop);
119810 }
119811 } /* End non-truncate path */
119812
119813 /* Update the sqlite_sequence table by storing the content of the
119814 ** maximum rowid counter values recorded while inserting into
119815 ** autoincrement tables.
119816 */
119817 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
119818 sqlite3AutoincrementEnd(pParse);
119819 }
119820
119821 /* Return the number of rows that were deleted. If this routine is
119822 ** generating code because of a call to sqlite3NestedParse(), do not
119823 ** invoke the callback function.
119824 */
119825 if( memCnt ){
119826 sqlite3VdbeAddOp2(p: v, OP_ChngCntRow, p1: memCnt, p2: 1);
119827 sqlite3VdbeSetNumCols(p: v, nResColumn: 1);
119828 sqlite3VdbeSetColName(p: v, idx: 0, COLNAME_NAME, zName: "rows deleted", SQLITE_STATIC);
119829 }
119830
119831delete_from_cleanup:
119832 sqlite3AuthContextPop(pContext: &sContext);
119833 sqlite3SrcListDelete(db, pList: pTabList);
119834 sqlite3ExprDelete(db, p: pWhere);
119835#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
119836 sqlite3ExprListDelete(db, pOrderBy);
119837 sqlite3ExprDelete(db, pLimit);
119838#endif
119839 sqlite3DbFree(db, p: aToOpen);
119840 return;
119841}
119842/* Make sure "isView" and other macros defined above are undefined. Otherwise
119843** they may interfere with compilation of other functions in this file
119844** (or in another file, if this file becomes part of the amalgamation). */
119845#ifdef isView
119846 #undef isView
119847#endif
119848#ifdef pTrigger
119849 #undef pTrigger
119850#endif
119851
119852/*
119853** This routine generates VDBE code that causes a single row of a
119854** single table to be deleted. Both the original table entry and
119855** all indices are removed.
119856**
119857** Preconditions:
119858**
119859** 1. iDataCur is an open cursor on the btree that is the canonical data
119860** store for the table. (This will be either the table itself,
119861** in the case of a rowid table, or the PRIMARY KEY index in the case
119862** of a WITHOUT ROWID table.)
119863**
119864** 2. Read/write cursors for all indices of pTab must be open as
119865** cursor number iIdxCur+i for the i-th index.
119866**
119867** 3. The primary key for the row to be deleted must be stored in a
119868** sequence of nPk memory cells starting at iPk. If nPk==0 that means
119869** that a search record formed from OP_MakeRecord is contained in the
119870** single memory location iPk.
119871**
119872** eMode:
119873** Parameter eMode may be passed either ONEPASS_OFF (0), ONEPASS_SINGLE, or
119874** ONEPASS_MULTI. If eMode is not ONEPASS_OFF, then the cursor
119875** iDataCur already points to the row to delete. If eMode is ONEPASS_OFF
119876** then this function must seek iDataCur to the entry identified by iPk
119877** and nPk before reading from it.
119878**
119879** If eMode is ONEPASS_MULTI, then this call is being made as part
119880** of a ONEPASS delete that affects multiple rows. In this case, if
119881** iIdxNoSeek is a valid cursor number (>=0) and is not the same as
119882** iDataCur, then its position should be preserved following the delete
119883** operation. Or, if iIdxNoSeek is not a valid cursor number, the
119884** position of iDataCur should be preserved instead.
119885**
119886** iIdxNoSeek:
119887** If iIdxNoSeek is a valid cursor number (>=0) not equal to iDataCur,
119888** then it identifies an index cursor (from within array of cursors
119889** starting at iIdxCur) that already points to the index entry to be deleted.
119890** Except, this optimization is disabled if there are BEFORE triggers since
119891** the trigger body might have moved the cursor.
119892*/
119893SQLITE_PRIVATE void sqlite3GenerateRowDelete(
119894 Parse *pParse, /* Parsing context */
119895 Table *pTab, /* Table containing the row to be deleted */
119896 Trigger *pTrigger, /* List of triggers to (potentially) fire */
119897 int iDataCur, /* Cursor from which column data is extracted */
119898 int iIdxCur, /* First index cursor */
119899 int iPk, /* First memory cell containing the PRIMARY KEY */
119900 i16 nPk, /* Number of PRIMARY KEY memory cells */
119901 u8 count, /* If non-zero, increment the row change counter */
119902 u8 onconf, /* Default ON CONFLICT policy for triggers */
119903 u8 eMode, /* ONEPASS_OFF, _SINGLE, or _MULTI. See above */
119904 int iIdxNoSeek /* Cursor number of cursor that does not need seeking */
119905){
119906 Vdbe *v = pParse->pVdbe; /* Vdbe */
119907 int iOld = 0; /* First register in OLD.* array */
119908 int iLabel; /* Label resolved to end of generated code */
119909 u8 opSeek; /* Seek opcode */
119910
119911 /* Vdbe is guaranteed to have been allocated by this stage. */
119912 assert( v );
119913 VdbeModuleComment((v, "BEGIN: GenRowDel(%d,%d,%d,%d)",
119914 iDataCur, iIdxCur, iPk, (int)nPk));
119915
119916 /* Seek cursor iCur to the row to delete. If this row no longer exists
119917 ** (this can happen if a trigger program has already deleted it), do
119918 ** not attempt to delete it or fire any DELETE triggers. */
119919 iLabel = sqlite3VdbeMakeLabel(pParse);
119920 opSeek = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
119921 if( eMode==ONEPASS_OFF ){
119922 sqlite3VdbeAddOp4Int(p: v, op: opSeek, p1: iDataCur, p2: iLabel, p3: iPk, p4: nPk);
119923 VdbeCoverageIf(v, opSeek==OP_NotExists);
119924 VdbeCoverageIf(v, opSeek==OP_NotFound);
119925 }
119926
119927 /* If there are any triggers to fire, allocate a range of registers to
119928 ** use for the old.* references in the triggers. */
119929 if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
119930 u32 mask; /* Mask of OLD.* columns in use */
119931 int iCol; /* Iterator used while populating OLD.* */
119932 int addrStart; /* Start of BEFORE trigger programs */
119933
119934 /* TODO: Could use temporary registers here. Also could attempt to
119935 ** avoid copying the contents of the rowid register. */
119936 mask = sqlite3TriggerColmask(
119937 pParse, pTrigger, 0, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onconf
119938 );
119939 mask |= sqlite3FkOldmask(pParse, pTab);
119940 iOld = pParse->nMem+1;
119941 pParse->nMem += (1 + pTab->nCol);
119942
119943 /* Populate the OLD.* pseudo-table register array. These values will be
119944 ** used by any BEFORE and AFTER triggers that exist. */
119945 sqlite3VdbeAddOp2(p: v, OP_Copy, p1: iPk, p2: iOld);
119946 for(iCol=0; iCol<pTab->nCol; iCol++){
119947 testcase( mask!=0xffffffff && iCol==31 );
119948 testcase( mask!=0xffffffff && iCol==32 );
119949 if( mask==0xffffffff || (iCol<=31 && (mask & MASKBIT32(iCol))!=0) ){
119950 int kk = sqlite3TableColumnToStorage(pTab, iCol);
119951 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur: iDataCur, iCol, regOut: iOld+kk+1);
119952 }
119953 }
119954
119955 /* Invoke BEFORE DELETE trigger programs. */
119956 addrStart = sqlite3VdbeCurrentAddr(p: v);
119957 sqlite3CodeRowTrigger(pParse, pTrigger,
119958 TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
119959 );
119960
119961 /* If any BEFORE triggers were coded, then seek the cursor to the
119962 ** row to be deleted again. It may be that the BEFORE triggers moved
119963 ** the cursor or already deleted the row that the cursor was
119964 ** pointing to.
119965 **
119966 ** Also disable the iIdxNoSeek optimization since the BEFORE trigger
119967 ** may have moved that cursor.
119968 */
119969 if( addrStart<sqlite3VdbeCurrentAddr(p: v) ){
119970 sqlite3VdbeAddOp4Int(p: v, op: opSeek, p1: iDataCur, p2: iLabel, p3: iPk, p4: nPk);
119971 VdbeCoverageIf(v, opSeek==OP_NotExists);
119972 VdbeCoverageIf(v, opSeek==OP_NotFound);
119973 testcase( iIdxNoSeek>=0 );
119974 iIdxNoSeek = -1;
119975 }
119976
119977 /* Do FK processing. This call checks that any FK constraints that
119978 ** refer to this table (i.e. constraints attached to other tables)
119979 ** are not violated by deleting this row. */
119980 sqlite3FkCheck(pParse, pTab, iOld, 0, 0, 0);
119981 }
119982
119983 /* Delete the index and table entries. Skip this step if pTab is really
119984 ** a view (in which case the only effect of the DELETE statement is to
119985 ** fire the INSTEAD OF triggers).
119986 **
119987 ** If variable 'count' is non-zero, then this OP_Delete instruction should
119988 ** invoke the update-hook. The pre-update-hook, on the other hand should
119989 ** be invoked unless table pTab is a system table. The difference is that
119990 ** the update-hook is not invoked for rows removed by REPLACE, but the
119991 ** pre-update-hook is.
119992 */
119993 if( !IsView(pTab) ){
119994 u8 p5 = 0;
119995 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,iIdxNoSeek);
119996 sqlite3VdbeAddOp2(p: v, OP_Delete, p1: iDataCur, p2: (count?OPFLAG_NCHANGE:0));
119997 if( pParse->nested==0 || 0==sqlite3_stricmp(zLeft: pTab->zName, zRight: "sqlite_stat1") ){
119998 sqlite3VdbeAppendP4(p: v, pP4: (char*)pTab, P4_TABLE);
119999 }
120000 if( eMode!=ONEPASS_OFF ){
120001 sqlite3VdbeChangeP5(p: v, OPFLAG_AUXDELETE);
120002 }
120003 if( iIdxNoSeek>=0 && iIdxNoSeek!=iDataCur ){
120004 sqlite3VdbeAddOp1(p: v, OP_Delete, p1: iIdxNoSeek);
120005 }
120006 if( eMode==ONEPASS_MULTI ) p5 |= OPFLAG_SAVEPOSITION;
120007 sqlite3VdbeChangeP5(p: v, p5);
120008 }
120009
120010 /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
120011 ** handle rows (possibly in other tables) that refer via a foreign key
120012 ** to the row just deleted. */
120013 sqlite3FkActions(pParse, pTab, 0, iOld, 0, 0);
120014
120015 /* Invoke AFTER DELETE trigger programs. */
120016 sqlite3CodeRowTrigger(pParse, pTrigger,
120017 TK_DELETE, 0, TRIGGER_AFTER, pTab, iOld, onconf, iLabel
120018 );
120019
120020 /* Jump here if the row had already been deleted before any BEFORE
120021 ** trigger programs were invoked. Or if a trigger program throws a
120022 ** RAISE(IGNORE) exception. */
120023 sqlite3VdbeResolveLabel(v, x: iLabel);
120024 VdbeModuleComment((v, "END: GenRowDel()"));
120025}
120026
120027/*
120028** This routine generates VDBE code that causes the deletion of all
120029** index entries associated with a single row of a single table, pTab
120030**
120031** Preconditions:
120032**
120033** 1. A read/write cursor "iDataCur" must be open on the canonical storage
120034** btree for the table pTab. (This will be either the table itself
120035** for rowid tables or to the primary key index for WITHOUT ROWID
120036** tables.)
120037**
120038** 2. Read/write cursors for all indices of pTab must be open as
120039** cursor number iIdxCur+i for the i-th index. (The pTab->pIndex
120040** index is the 0-th index.)
120041**
120042** 3. The "iDataCur" cursor must be already be positioned on the row
120043** that is to be deleted.
120044*/
120045SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(
120046 Parse *pParse, /* Parsing and code generating context */
120047 Table *pTab, /* Table containing the row to be deleted */
120048 int iDataCur, /* Cursor of table holding data. */
120049 int iIdxCur, /* First index cursor */
120050 int *aRegIdx, /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
120051 int iIdxNoSeek /* Do not delete from this cursor */
120052){
120053 int i; /* Index loop counter */
120054 int r1 = -1; /* Register holding an index key */
120055 int iPartIdxLabel; /* Jump destination for skipping partial index entries */
120056 Index *pIdx; /* Current index */
120057 Index *pPrior = 0; /* Prior index */
120058 Vdbe *v; /* The prepared statement under construction */
120059 Index *pPk; /* PRIMARY KEY index, or NULL for rowid tables */
120060
120061 v = pParse->pVdbe;
120062 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
120063 for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
120064 assert( iIdxCur+i!=iDataCur || pPk==pIdx );
120065 if( aRegIdx!=0 && aRegIdx[i]==0 ) continue;
120066 if( pIdx==pPk ) continue;
120067 if( iIdxCur+i==iIdxNoSeek ) continue;
120068 VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
120069 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 1,
120070 &iPartIdxLabel, pPrior, r1);
120071 sqlite3VdbeAddOp3(p: v, OP_IdxDelete, p1: iIdxCur+i, p2: r1,
120072 p3: pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
120073 sqlite3VdbeChangeP5(p: v, p5: 1); /* Cause IdxDelete to error if no entry found */
120074 sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
120075 pPrior = pIdx;
120076 }
120077}
120078
120079/*
120080** Generate code that will assemble an index key and stores it in register
120081** regOut. The key with be for index pIdx which is an index on pTab.
120082** iCur is the index of a cursor open on the pTab table and pointing to
120083** the entry that needs indexing. If pTab is a WITHOUT ROWID table, then
120084** iCur must be the cursor of the PRIMARY KEY index.
120085**
120086** Return a register number which is the first in a block of
120087** registers that holds the elements of the index key. The
120088** block of registers has already been deallocated by the time
120089** this routine returns.
120090**
120091** If *piPartIdxLabel is not NULL, fill it in with a label and jump
120092** to that label if pIdx is a partial index that should be skipped.
120093** The label should be resolved using sqlite3ResolvePartIdxLabel().
120094** A partial index should be skipped if its WHERE clause evaluates
120095** to false or null. If pIdx is not a partial index, *piPartIdxLabel
120096** will be set to zero which is an empty label that is ignored by
120097** sqlite3ResolvePartIdxLabel().
120098**
120099** The pPrior and regPrior parameters are used to implement a cache to
120100** avoid unnecessary register loads. If pPrior is not NULL, then it is
120101** a pointer to a different index for which an index key has just been
120102** computed into register regPrior. If the current pIdx index is generating
120103** its key into the same sequence of registers and if pPrior and pIdx share
120104** a column in common, then the register corresponding to that column already
120105** holds the correct value and the loading of that register is skipped.
120106** This optimization is helpful when doing a DELETE or an INTEGRITY_CHECK
120107** on a table with multiple indices, and especially with the ROWID or
120108** PRIMARY KEY columns of the index.
120109*/
120110SQLITE_PRIVATE int sqlite3GenerateIndexKey(
120111 Parse *pParse, /* Parsing context */
120112 Index *pIdx, /* The index for which to generate a key */
120113 int iDataCur, /* Cursor number from which to take column data */
120114 int regOut, /* Put the new key into this register if not 0 */
120115 int prefixOnly, /* Compute only a unique prefix of the key */
120116 int *piPartIdxLabel, /* OUT: Jump to this label to skip partial index */
120117 Index *pPrior, /* Previously generated index key */
120118 int regPrior /* Register holding previous generated key */
120119){
120120 Vdbe *v = pParse->pVdbe;
120121 int j;
120122 int regBase;
120123 int nCol;
120124
120125 if( piPartIdxLabel ){
120126 if( pIdx->pPartIdxWhere ){
120127 *piPartIdxLabel = sqlite3VdbeMakeLabel(pParse);
120128 pParse->iSelfTab = iDataCur + 1;
120129 sqlite3ExprIfFalseDup(pParse, pExpr: pIdx->pPartIdxWhere, dest: *piPartIdxLabel,
120130 SQLITE_JUMPIFNULL);
120131 pParse->iSelfTab = 0;
120132 pPrior = 0; /* Ticket a9efb42811fa41ee 2019-11-02;
120133 ** pPartIdxWhere may have corrupted regPrior registers */
120134 }else{
120135 *piPartIdxLabel = 0;
120136 }
120137 }
120138 nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
120139 regBase = sqlite3GetTempRange(pParse, nReg: nCol);
120140 if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
120141 for(j=0; j<nCol; j++){
120142 if( pPrior
120143 && pPrior->aiColumn[j]==pIdx->aiColumn[j]
120144 && pPrior->aiColumn[j]!=XN_EXPR
120145 ){
120146 /* This column was already computed by the previous index */
120147 continue;
120148 }
120149 sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur: iDataCur, iIdxCol: j, regOut: regBase+j);
120150 if( pIdx->aiColumn[j]>=0 ){
120151 /* If the column affinity is REAL but the number is an integer, then it
120152 ** might be stored in the table as an integer (using a compact
120153 ** representation) then converted to REAL by an OP_RealAffinity opcode.
120154 ** But we are getting ready to store this value back into an index, where
120155 ** it should be converted by to INTEGER again. So omit the
120156 ** OP_RealAffinity opcode if it is present */
120157 sqlite3VdbeDeletePriorOpcode(p: v, OP_RealAffinity);
120158 }
120159 }
120160 if( regOut ){
120161 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: regBase, p2: nCol, p3: regOut);
120162 }
120163 sqlite3ReleaseTempRange(pParse, iReg: regBase, nReg: nCol);
120164 return regBase;
120165}
120166
120167/*
120168** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label
120169** because it was a partial index, then this routine should be called to
120170** resolve that label.
120171*/
120172SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse *pParse, int iLabel){
120173 if( iLabel ){
120174 sqlite3VdbeResolveLabel(v: pParse->pVdbe, x: iLabel);
120175 }
120176}
120177
120178/************** End of delete.c **********************************************/
120179/************** Begin file func.c ********************************************/
120180/*
120181** 2002 February 23
120182**
120183** The author disclaims copyright to this source code. In place of
120184** a legal notice, here is a blessing:
120185**
120186** May you do good and not evil.
120187** May you find forgiveness for yourself and forgive others.
120188** May you share freely, never taking more than you give.
120189**
120190*************************************************************************
120191** This file contains the C-language implementations for many of the SQL
120192** functions of SQLite. (Some function, and in particular the date and
120193** time functions, are implemented separately.)
120194*/
120195/* #include "sqliteInt.h" */
120196/* #include <stdlib.h> */
120197/* #include <assert.h> */
120198#ifndef SQLITE_OMIT_FLOATING_POINT
120199/* #include <math.h> */
120200#endif
120201/* #include "vdbeInt.h" */
120202
120203/*
120204** Return the collating function associated with a function.
120205*/
120206static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
120207 VdbeOp *pOp;
120208 assert( context->pVdbe!=0 );
120209 pOp = &context->pVdbe->aOp[context->iOp-1];
120210 assert( pOp->opcode==OP_CollSeq );
120211 assert( pOp->p4type==P4_COLLSEQ );
120212 return pOp->p4.pColl;
120213}
120214
120215/*
120216** Indicate that the accumulator load should be skipped on this
120217** iteration of the aggregate loop.
120218*/
120219static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){
120220 assert( context->isError<=0 );
120221 context->isError = -1;
120222 context->skipFlag = 1;
120223}
120224
120225/*
120226** Implementation of the non-aggregate min() and max() functions
120227*/
120228static void minmaxFunc(
120229 sqlite3_context *context,
120230 int argc,
120231 sqlite3_value **argv
120232){
120233 int i;
120234 int mask; /* 0 for min() or 0xffffffff for max() */
120235 int iBest;
120236 CollSeq *pColl;
120237
120238 assert( argc>1 );
120239 mask = sqlite3_user_data(p: context)==0 ? 0 : -1;
120240 pColl = sqlite3GetFuncCollSeq(context);
120241 assert( pColl );
120242 assert( mask==-1 || mask==0 );
120243 iBest = 0;
120244 if( sqlite3_value_type(pVal: argv[0])==SQLITE_NULL ) return;
120245 for(i=1; i<argc; i++){
120246 if( sqlite3_value_type(pVal: argv[i])==SQLITE_NULL ) return;
120247 if( (sqlite3MemCompare(pMem1: argv[iBest], pMem2: argv[i], pColl)^mask)>=0 ){
120248 testcase( mask==0 );
120249 iBest = i;
120250 }
120251 }
120252 sqlite3_result_value(pCtx: context, pValue: argv[iBest]);
120253}
120254
120255/*
120256** Return the type of the argument.
120257*/
120258static void typeofFunc(
120259 sqlite3_context *context,
120260 int NotUsed,
120261 sqlite3_value **argv
120262){
120263 static const char *azType[] = { "integer", "real", "text", "blob", "null" };
120264 int i = sqlite3_value_type(pVal: argv[0]) - 1;
120265 UNUSED_PARAMETER(NotUsed);
120266 assert( i>=0 && i<ArraySize(azType) );
120267 assert( SQLITE_INTEGER==1 );
120268 assert( SQLITE_FLOAT==2 );
120269 assert( SQLITE_TEXT==3 );
120270 assert( SQLITE_BLOB==4 );
120271 assert( SQLITE_NULL==5 );
120272 /* EVIDENCE-OF: R-01470-60482 The sqlite3_value_type(V) interface returns
120273 ** the datatype code for the initial datatype of the sqlite3_value object
120274 ** V. The returned value is one of SQLITE_INTEGER, SQLITE_FLOAT,
120275 ** SQLITE_TEXT, SQLITE_BLOB, or SQLITE_NULL. */
120276 sqlite3_result_text(pCtx: context, z: azType[i], n: -1, SQLITE_STATIC);
120277}
120278
120279
120280/*
120281** Implementation of the length() function
120282*/
120283static void lengthFunc(
120284 sqlite3_context *context,
120285 int argc,
120286 sqlite3_value **argv
120287){
120288 assert( argc==1 );
120289 UNUSED_PARAMETER(argc);
120290 switch( sqlite3_value_type(pVal: argv[0]) ){
120291 case SQLITE_BLOB:
120292 case SQLITE_INTEGER:
120293 case SQLITE_FLOAT: {
120294 sqlite3_result_int(pCtx: context, iVal: sqlite3_value_bytes(pVal: argv[0]));
120295 break;
120296 }
120297 case SQLITE_TEXT: {
120298 const unsigned char *z = sqlite3_value_text(pVal: argv[0]);
120299 const unsigned char *z0;
120300 unsigned char c;
120301 if( z==0 ) return;
120302 z0 = z;
120303 while( (c = *z)!=0 ){
120304 z++;
120305 if( c>=0xc0 ){
120306 while( (*z & 0xc0)==0x80 ){ z++; z0++; }
120307 }
120308 }
120309 sqlite3_result_int(pCtx: context, iVal: (int)(z-z0));
120310 break;
120311 }
120312 default: {
120313 sqlite3_result_null(pCtx: context);
120314 break;
120315 }
120316 }
120317}
120318
120319/*
120320** Implementation of the abs() function.
120321**
120322** IMP: R-23979-26855 The abs(X) function returns the absolute value of
120323** the numeric argument X.
120324*/
120325static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
120326 assert( argc==1 );
120327 UNUSED_PARAMETER(argc);
120328 switch( sqlite3_value_type(pVal: argv[0]) ){
120329 case SQLITE_INTEGER: {
120330 i64 iVal = sqlite3_value_int64(pVal: argv[0]);
120331 if( iVal<0 ){
120332 if( iVal==SMALLEST_INT64 ){
120333 /* IMP: R-31676-45509 If X is the integer -9223372036854775808
120334 ** then abs(X) throws an integer overflow error since there is no
120335 ** equivalent positive 64-bit two complement value. */
120336 sqlite3_result_error(pCtx: context, z: "integer overflow", n: -1);
120337 return;
120338 }
120339 iVal = -iVal;
120340 }
120341 sqlite3_result_int64(pCtx: context, iVal);
120342 break;
120343 }
120344 case SQLITE_NULL: {
120345 /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
120346 sqlite3_result_null(pCtx: context);
120347 break;
120348 }
120349 default: {
120350 /* Because sqlite3_value_double() returns 0.0 if the argument is not
120351 ** something that can be converted into a number, we have:
120352 ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob
120353 ** that cannot be converted to a numeric value.
120354 */
120355 double rVal = sqlite3_value_double(pVal: argv[0]);
120356 if( rVal<0 ) rVal = -rVal;
120357 sqlite3_result_double(pCtx: context, rVal);
120358 break;
120359 }
120360 }
120361}
120362
120363/*
120364** Implementation of the instr() function.
120365**
120366** instr(haystack,needle) finds the first occurrence of needle
120367** in haystack and returns the number of previous characters plus 1,
120368** or 0 if needle does not occur within haystack.
120369**
120370** If both haystack and needle are BLOBs, then the result is one more than
120371** the number of bytes in haystack prior to the first occurrence of needle,
120372** or 0 if needle never occurs in haystack.
120373*/
120374static void instrFunc(
120375 sqlite3_context *context,
120376 int argc,
120377 sqlite3_value **argv
120378){
120379 const unsigned char *zHaystack;
120380 const unsigned char *zNeedle;
120381 int nHaystack;
120382 int nNeedle;
120383 int typeHaystack, typeNeedle;
120384 int N = 1;
120385 int isText;
120386 unsigned char firstChar;
120387 sqlite3_value *pC1 = 0;
120388 sqlite3_value *pC2 = 0;
120389
120390 UNUSED_PARAMETER(argc);
120391 typeHaystack = sqlite3_value_type(pVal: argv[0]);
120392 typeNeedle = sqlite3_value_type(pVal: argv[1]);
120393 if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return;
120394 nHaystack = sqlite3_value_bytes(pVal: argv[0]);
120395 nNeedle = sqlite3_value_bytes(pVal: argv[1]);
120396 if( nNeedle>0 ){
120397 if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){
120398 zHaystack = sqlite3_value_blob(pVal: argv[0]);
120399 zNeedle = sqlite3_value_blob(pVal: argv[1]);
120400 isText = 0;
120401 }else if( typeHaystack!=SQLITE_BLOB && typeNeedle!=SQLITE_BLOB ){
120402 zHaystack = sqlite3_value_text(pVal: argv[0]);
120403 zNeedle = sqlite3_value_text(pVal: argv[1]);
120404 isText = 1;
120405 }else{
120406 pC1 = sqlite3_value_dup(pOrig: argv[0]);
120407 zHaystack = sqlite3_value_text(pVal: pC1);
120408 if( zHaystack==0 ) goto endInstrOOM;
120409 nHaystack = sqlite3_value_bytes(pVal: pC1);
120410 pC2 = sqlite3_value_dup(pOrig: argv[1]);
120411 zNeedle = sqlite3_value_text(pVal: pC2);
120412 if( zNeedle==0 ) goto endInstrOOM;
120413 nNeedle = sqlite3_value_bytes(pVal: pC2);
120414 isText = 1;
120415 }
120416 if( zNeedle==0 || (nHaystack && zHaystack==0) ) goto endInstrOOM;
120417 firstChar = zNeedle[0];
120418 while( nNeedle<=nHaystack
120419 && (zHaystack[0]!=firstChar || memcmp(s1: zHaystack, s2: zNeedle, n: nNeedle)!=0)
120420 ){
120421 N++;
120422 do{
120423 nHaystack--;
120424 zHaystack++;
120425 }while( isText && (zHaystack[0]&0xc0)==0x80 );
120426 }
120427 if( nNeedle>nHaystack ) N = 0;
120428 }
120429 sqlite3_result_int(pCtx: context, iVal: N);
120430endInstr:
120431 sqlite3_value_free(pOld: pC1);
120432 sqlite3_value_free(pOld: pC2);
120433 return;
120434endInstrOOM:
120435 sqlite3_result_error_nomem(pCtx: context);
120436 goto endInstr;
120437}
120438
120439/*
120440** Implementation of the printf() function.
120441*/
120442static void printfFunc(
120443 sqlite3_context *context,
120444 int argc,
120445 sqlite3_value **argv
120446){
120447 PrintfArguments x;
120448 StrAccum str;
120449 const char *zFormat;
120450 int n;
120451 sqlite3 *db = sqlite3_context_db_handle(p: context);
120452
120453 if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(pVal: argv[0]))!=0 ){
120454 x.nArg = argc-1;
120455 x.nUsed = 0;
120456 x.apArg = argv+1;
120457 sqlite3StrAccumInit(p: &str, db, zBase: 0, n: 0, mx: db->aLimit[SQLITE_LIMIT_LENGTH]);
120458 str.printfFlags = SQLITE_PRINTF_SQLFUNC;
120459 sqlite3_str_appendf(p: &str, zFormat, &x);
120460 n = str.nChar;
120461 sqlite3_result_text(pCtx: context, z: sqlite3StrAccumFinish(p: &str), n,
120462 SQLITE_DYNAMIC);
120463 }
120464}
120465
120466/*
120467** Implementation of the substr() function.
120468**
120469** substr(x,p1,p2) returns p2 characters of x[] beginning with p1.
120470** p1 is 1-indexed. So substr(x,1,1) returns the first character
120471** of x. If x is text, then we actually count UTF-8 characters.
120472** If x is a blob, then we count bytes.
120473**
120474** If p1 is negative, then we begin abs(p1) from the end of x[].
120475**
120476** If p2 is negative, return the p2 characters preceding p1.
120477*/
120478static void substrFunc(
120479 sqlite3_context *context,
120480 int argc,
120481 sqlite3_value **argv
120482){
120483 const unsigned char *z;
120484 const unsigned char *z2;
120485 int len;
120486 int p0type;
120487 i64 p1, p2;
120488 int negP2 = 0;
120489
120490 assert( argc==3 || argc==2 );
120491 if( sqlite3_value_type(pVal: argv[1])==SQLITE_NULL
120492 || (argc==3 && sqlite3_value_type(pVal: argv[2])==SQLITE_NULL)
120493 ){
120494 return;
120495 }
120496 p0type = sqlite3_value_type(pVal: argv[0]);
120497 p1 = sqlite3_value_int(pVal: argv[1]);
120498 if( p0type==SQLITE_BLOB ){
120499 len = sqlite3_value_bytes(pVal: argv[0]);
120500 z = sqlite3_value_blob(pVal: argv[0]);
120501 if( z==0 ) return;
120502 assert( len==sqlite3_value_bytes(argv[0]) );
120503 }else{
120504 z = sqlite3_value_text(pVal: argv[0]);
120505 if( z==0 ) return;
120506 len = 0;
120507 if( p1<0 ){
120508 for(z2=z; *z2; len++){
120509 SQLITE_SKIP_UTF8(z2);
120510 }
120511 }
120512 }
120513#ifdef SQLITE_SUBSTR_COMPATIBILITY
120514 /* If SUBSTR_COMPATIBILITY is defined then substr(X,0,N) work the same as
120515 ** as substr(X,1,N) - it returns the first N characters of X. This
120516 ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8]
120517 ** from 2009-02-02 for compatibility of applications that exploited the
120518 ** old buggy behavior. */
120519 if( p1==0 ) p1 = 1; /* <rdar://problem/6778339> */
120520#endif
120521 if( argc==3 ){
120522 p2 = sqlite3_value_int(pVal: argv[2]);
120523 if( p2<0 ){
120524 p2 = -p2;
120525 negP2 = 1;
120526 }
120527 }else{
120528 p2 = sqlite3_context_db_handle(p: context)->aLimit[SQLITE_LIMIT_LENGTH];
120529 }
120530 if( p1<0 ){
120531 p1 += len;
120532 if( p1<0 ){
120533 p2 += p1;
120534 if( p2<0 ) p2 = 0;
120535 p1 = 0;
120536 }
120537 }else if( p1>0 ){
120538 p1--;
120539 }else if( p2>0 ){
120540 p2--;
120541 }
120542 if( negP2 ){
120543 p1 -= p2;
120544 if( p1<0 ){
120545 p2 += p1;
120546 p1 = 0;
120547 }
120548 }
120549 assert( p1>=0 && p2>=0 );
120550 if( p0type!=SQLITE_BLOB ){
120551 while( *z && p1 ){
120552 SQLITE_SKIP_UTF8(z);
120553 p1--;
120554 }
120555 for(z2=z; *z2 && p2; p2--){
120556 SQLITE_SKIP_UTF8(z2);
120557 }
120558 sqlite3_result_text64(pCtx: context, z: (char*)z, n: z2-z, SQLITE_TRANSIENT,
120559 SQLITE_UTF8);
120560 }else{
120561 if( p1+p2>len ){
120562 p2 = len-p1;
120563 if( p2<0 ) p2 = 0;
120564 }
120565 sqlite3_result_blob64(pCtx: context, z: (char*)&z[p1], n: (u64)p2, SQLITE_TRANSIENT);
120566 }
120567}
120568
120569/*
120570** Implementation of the round() function
120571*/
120572#ifndef SQLITE_OMIT_FLOATING_POINT
120573static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
120574 int n = 0;
120575 double r;
120576 char *zBuf;
120577 assert( argc==1 || argc==2 );
120578 if( argc==2 ){
120579 if( SQLITE_NULL==sqlite3_value_type(pVal: argv[1]) ) return;
120580 n = sqlite3_value_int(pVal: argv[1]);
120581 if( n>30 ) n = 30;
120582 if( n<0 ) n = 0;
120583 }
120584 if( sqlite3_value_type(pVal: argv[0])==SQLITE_NULL ) return;
120585 r = sqlite3_value_double(pVal: argv[0]);
120586 /* If Y==0 and X will fit in a 64-bit int,
120587 ** handle the rounding directly,
120588 ** otherwise use printf.
120589 */
120590 if( r<-4503599627370496.0 || r>+4503599627370496.0 ){
120591 /* The value has no fractional part so there is nothing to round */
120592 }else if( n==0 ){
120593 r = (double)((sqlite_int64)(r+(r<0?-0.5:+0.5)));
120594 }else{
120595 zBuf = sqlite3_mprintf(zFormat: "%.*f",n,r);
120596 if( zBuf==0 ){
120597 sqlite3_result_error_nomem(pCtx: context);
120598 return;
120599 }
120600 sqlite3AtoF(z: zBuf, pResult: &r, length: sqlite3Strlen30(z: zBuf), SQLITE_UTF8);
120601 sqlite3_free(p: zBuf);
120602 }
120603 sqlite3_result_double(pCtx: context, rVal: r);
120604}
120605#endif
120606
120607/*
120608** Allocate nByte bytes of space using sqlite3Malloc(). If the
120609** allocation fails, call sqlite3_result_error_nomem() to notify
120610** the database handle that malloc() has failed and return NULL.
120611** If nByte is larger than the maximum string or blob length, then
120612** raise an SQLITE_TOOBIG exception and return NULL.
120613*/
120614static void *contextMalloc(sqlite3_context *context, i64 nByte){
120615 char *z;
120616 sqlite3 *db = sqlite3_context_db_handle(p: context);
120617 assert( nByte>0 );
120618 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
120619 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
120620 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
120621 sqlite3_result_error_toobig(pCtx: context);
120622 z = 0;
120623 }else{
120624 z = sqlite3Malloc(n: nByte);
120625 if( !z ){
120626 sqlite3_result_error_nomem(pCtx: context);
120627 }
120628 }
120629 return z;
120630}
120631
120632/*
120633** Implementation of the upper() and lower() SQL functions.
120634*/
120635static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
120636 char *z1;
120637 const char *z2;
120638 int i, n;
120639 UNUSED_PARAMETER(argc);
120640 z2 = (char*)sqlite3_value_text(pVal: argv[0]);
120641 n = sqlite3_value_bytes(pVal: argv[0]);
120642 /* Verify that the call to _bytes() does not invalidate the _text() pointer */
120643 assert( z2==(char*)sqlite3_value_text(argv[0]) );
120644 if( z2 ){
120645 z1 = contextMalloc(context, nByte: ((i64)n)+1);
120646 if( z1 ){
120647 for(i=0; i<n; i++){
120648 z1[i] = (char)sqlite3Toupper(z2[i]);
120649 }
120650 sqlite3_result_text(pCtx: context, z: z1, n, xDel: sqlite3_free);
120651 }
120652 }
120653}
120654static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
120655 char *z1;
120656 const char *z2;
120657 int i, n;
120658 UNUSED_PARAMETER(argc);
120659 z2 = (char*)sqlite3_value_text(pVal: argv[0]);
120660 n = sqlite3_value_bytes(pVal: argv[0]);
120661 /* Verify that the call to _bytes() does not invalidate the _text() pointer */
120662 assert( z2==(char*)sqlite3_value_text(argv[0]) );
120663 if( z2 ){
120664 z1 = contextMalloc(context, nByte: ((i64)n)+1);
120665 if( z1 ){
120666 for(i=0; i<n; i++){
120667 z1[i] = sqlite3Tolower(z2[i]);
120668 }
120669 sqlite3_result_text(pCtx: context, z: z1, n, xDel: sqlite3_free);
120670 }
120671 }
120672}
120673
120674/*
120675** Some functions like COALESCE() and IFNULL() and UNLIKELY() are implemented
120676** as VDBE code so that unused argument values do not have to be computed.
120677** However, we still need some kind of function implementation for this
120678** routines in the function table. The noopFunc macro provides this.
120679** noopFunc will never be called so it doesn't matter what the implementation
120680** is. We might as well use the "version()" function as a substitute.
120681*/
120682#define noopFunc versionFunc /* Substitute function - never called */
120683
120684/*
120685** Implementation of random(). Return a random integer.
120686*/
120687static void randomFunc(
120688 sqlite3_context *context,
120689 int NotUsed,
120690 sqlite3_value **NotUsed2
120691){
120692 sqlite_int64 r;
120693 UNUSED_PARAMETER2(NotUsed, NotUsed2);
120694 sqlite3_randomness(N: sizeof(r), pBuf: &r);
120695 if( r<0 ){
120696 /* We need to prevent a random number of 0x8000000000000000
120697 ** (or -9223372036854775808) since when you do abs() of that
120698 ** number of you get the same value back again. To do this
120699 ** in a way that is testable, mask the sign bit off of negative
120700 ** values, resulting in a positive value. Then take the
120701 ** 2s complement of that positive value. The end result can
120702 ** therefore be no less than -9223372036854775807.
120703 */
120704 r = -(r & LARGEST_INT64);
120705 }
120706 sqlite3_result_int64(pCtx: context, iVal: r);
120707}
120708
120709/*
120710** Implementation of randomblob(N). Return a random blob
120711** that is N bytes long.
120712*/
120713static void randomBlob(
120714 sqlite3_context *context,
120715 int argc,
120716 sqlite3_value **argv
120717){
120718 sqlite3_int64 n;
120719 unsigned char *p;
120720 assert( argc==1 );
120721 UNUSED_PARAMETER(argc);
120722 n = sqlite3_value_int64(pVal: argv[0]);
120723 if( n<1 ){
120724 n = 1;
120725 }
120726 p = contextMalloc(context, nByte: n);
120727 if( p ){
120728 sqlite3_randomness(N: n, pBuf: p);
120729 sqlite3_result_blob(pCtx: context, z: (char*)p, n, xDel: sqlite3_free);
120730 }
120731}
120732
120733/*
120734** Implementation of the last_insert_rowid() SQL function. The return
120735** value is the same as the sqlite3_last_insert_rowid() API function.
120736*/
120737static void last_insert_rowid(
120738 sqlite3_context *context,
120739 int NotUsed,
120740 sqlite3_value **NotUsed2
120741){
120742 sqlite3 *db = sqlite3_context_db_handle(p: context);
120743 UNUSED_PARAMETER2(NotUsed, NotUsed2);
120744 /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
120745 ** wrapper around the sqlite3_last_insert_rowid() C/C++ interface
120746 ** function. */
120747 sqlite3_result_int64(pCtx: context, iVal: sqlite3_last_insert_rowid(db));
120748}
120749
120750/*
120751** Implementation of the changes() SQL function.
120752**
120753** IMP: R-32760-32347 The changes() SQL function is a wrapper
120754** around the sqlite3_changes64() C/C++ function and hence follows the
120755** same rules for counting changes.
120756*/
120757static void changes(
120758 sqlite3_context *context,
120759 int NotUsed,
120760 sqlite3_value **NotUsed2
120761){
120762 sqlite3 *db = sqlite3_context_db_handle(p: context);
120763 UNUSED_PARAMETER2(NotUsed, NotUsed2);
120764 sqlite3_result_int64(pCtx: context, iVal: sqlite3_changes64(db));
120765}
120766
120767/*
120768** Implementation of the total_changes() SQL function. The return value is
120769** the same as the sqlite3_total_changes64() API function.
120770*/
120771static void total_changes(
120772 sqlite3_context *context,
120773 int NotUsed,
120774 sqlite3_value **NotUsed2
120775){
120776 sqlite3 *db = sqlite3_context_db_handle(p: context);
120777 UNUSED_PARAMETER2(NotUsed, NotUsed2);
120778 /* IMP: R-11217-42568 This function is a wrapper around the
120779 ** sqlite3_total_changes64() C/C++ interface. */
120780 sqlite3_result_int64(pCtx: context, iVal: sqlite3_total_changes64(db));
120781}
120782
120783/*
120784** A structure defining how to do GLOB-style comparisons.
120785*/
120786struct compareInfo {
120787 u8 matchAll; /* "*" or "%" */
120788 u8 matchOne; /* "?" or "_" */
120789 u8 matchSet; /* "[" or 0 */
120790 u8 noCase; /* true to ignore case differences */
120791};
120792
120793/*
120794** For LIKE and GLOB matching on EBCDIC machines, assume that every
120795** character is exactly one byte in size. Also, provde the Utf8Read()
120796** macro for fast reading of the next character in the common case where
120797** the next character is ASCII.
120798*/
120799#if defined(SQLITE_EBCDIC)
120800# define sqlite3Utf8Read(A) (*((*A)++))
120801# define Utf8Read(A) (*(A++))
120802#else
120803# define Utf8Read(A) (A[0]<0x80?*(A++):sqlite3Utf8Read(&A))
120804#endif
120805
120806static const struct compareInfo globInfo = { '*', '?', '[', 0 };
120807/* The correct SQL-92 behavior is for the LIKE operator to ignore
120808** case. Thus 'a' LIKE 'A' would be true. */
120809static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 };
120810/* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
120811** is case sensitive causing 'a' LIKE 'A' to be false */
120812static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 };
120813
120814/*
120815** Possible error returns from patternMatch()
120816*/
120817#define SQLITE_MATCH 0
120818#define SQLITE_NOMATCH 1
120819#define SQLITE_NOWILDCARDMATCH 2
120820
120821/*
120822** Compare two UTF-8 strings for equality where the first string is
120823** a GLOB or LIKE expression. Return values:
120824**
120825** SQLITE_MATCH: Match
120826** SQLITE_NOMATCH: No match
120827** SQLITE_NOWILDCARDMATCH: No match in spite of having * or % wildcards.
120828**
120829** Globbing rules:
120830**
120831** '*' Matches any sequence of zero or more characters.
120832**
120833** '?' Matches exactly one character.
120834**
120835** [...] Matches one character from the enclosed list of
120836** characters.
120837**
120838** [^...] Matches one character not in the enclosed list.
120839**
120840** With the [...] and [^...] matching, a ']' character can be included
120841** in the list by making it the first character after '[' or '^'. A
120842** range of characters can be specified using '-'. Example:
120843** "[a-z]" matches any single lower-case letter. To match a '-', make
120844** it the last character in the list.
120845**
120846** Like matching rules:
120847**
120848** '%' Matches any sequence of zero or more characters
120849**
120850*** '_' Matches any one character
120851**
120852** Ec Where E is the "esc" character and c is any other
120853** character, including '%', '_', and esc, match exactly c.
120854**
120855** The comments within this routine usually assume glob matching.
120856**
120857** This routine is usually quick, but can be N**2 in the worst case.
120858*/
120859static int patternCompare(
120860 const u8 *zPattern, /* The glob pattern */
120861 const u8 *zString, /* The string to compare against the glob */
120862 const struct compareInfo *pInfo, /* Information about how to do the compare */
120863 u32 matchOther /* The escape char (LIKE) or '[' (GLOB) */
120864){
120865 u32 c, c2; /* Next pattern and input string chars */
120866 u32 matchOne = pInfo->matchOne; /* "?" or "_" */
120867 u32 matchAll = pInfo->matchAll; /* "*" or "%" */
120868 u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */
120869 const u8 *zEscaped = 0; /* One past the last escaped input char */
120870
120871 while( (c = Utf8Read(zPattern))!=0 ){
120872 if( c==matchAll ){ /* Match "*" */
120873 /* Skip over multiple "*" characters in the pattern. If there
120874 ** are also "?" characters, skip those as well, but consume a
120875 ** single character of the input string for each "?" skipped */
120876 while( (c=Utf8Read(zPattern)) == matchAll
120877 || (c == matchOne && matchOne!=0) ){
120878 if( c==matchOne && sqlite3Utf8Read(pz: &zString)==0 ){
120879 return SQLITE_NOWILDCARDMATCH;
120880 }
120881 }
120882 if( c==0 ){
120883 return SQLITE_MATCH; /* "*" at the end of the pattern matches */
120884 }else if( c==matchOther ){
120885 if( pInfo->matchSet==0 ){
120886 c = sqlite3Utf8Read(pz: &zPattern);
120887 if( c==0 ) return SQLITE_NOWILDCARDMATCH;
120888 }else{
120889 /* "[...]" immediately follows the "*". We have to do a slow
120890 ** recursive search in this case, but it is an unusual case. */
120891 assert( matchOther<0x80 ); /* '[' is a single-byte character */
120892 while( *zString ){
120893 int bMatch = patternCompare(zPattern: &zPattern[-1],zString,pInfo,matchOther);
120894 if( bMatch!=SQLITE_NOMATCH ) return bMatch;
120895 SQLITE_SKIP_UTF8(zString);
120896 }
120897 return SQLITE_NOWILDCARDMATCH;
120898 }
120899 }
120900
120901 /* At this point variable c contains the first character of the
120902 ** pattern string past the "*". Search in the input string for the
120903 ** first matching character and recursively continue the match from
120904 ** that point.
120905 **
120906 ** For a case-insensitive search, set variable cx to be the same as
120907 ** c but in the other case and search the input string for either
120908 ** c or cx.
120909 */
120910 if( c<=0x80 ){
120911 char zStop[3];
120912 int bMatch;
120913 if( noCase ){
120914 zStop[0] = sqlite3Toupper(c);
120915 zStop[1] = sqlite3Tolower(c);
120916 zStop[2] = 0;
120917 }else{
120918 zStop[0] = c;
120919 zStop[1] = 0;
120920 }
120921 while(1){
120922 zString += strcspn(s: (const char*)zString, reject: zStop);
120923 if( zString[0]==0 ) break;
120924 zString++;
120925 bMatch = patternCompare(zPattern,zString,pInfo,matchOther);
120926 if( bMatch!=SQLITE_NOMATCH ) return bMatch;
120927 }
120928 }else{
120929 int bMatch;
120930 while( (c2 = Utf8Read(zString))!=0 ){
120931 if( c2!=c ) continue;
120932 bMatch = patternCompare(zPattern,zString,pInfo,matchOther);
120933 if( bMatch!=SQLITE_NOMATCH ) return bMatch;
120934 }
120935 }
120936 return SQLITE_NOWILDCARDMATCH;
120937 }
120938 if( c==matchOther ){
120939 if( pInfo->matchSet==0 ){
120940 c = sqlite3Utf8Read(pz: &zPattern);
120941 if( c==0 ) return SQLITE_NOMATCH;
120942 zEscaped = zPattern;
120943 }else{
120944 u32 prior_c = 0;
120945 int seen = 0;
120946 int invert = 0;
120947 c = sqlite3Utf8Read(pz: &zString);
120948 if( c==0 ) return SQLITE_NOMATCH;
120949 c2 = sqlite3Utf8Read(pz: &zPattern);
120950 if( c2=='^' ){
120951 invert = 1;
120952 c2 = sqlite3Utf8Read(pz: &zPattern);
120953 }
120954 if( c2==']' ){
120955 if( c==']' ) seen = 1;
120956 c2 = sqlite3Utf8Read(pz: &zPattern);
120957 }
120958 while( c2 && c2!=']' ){
120959 if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
120960 c2 = sqlite3Utf8Read(pz: &zPattern);
120961 if( c>=prior_c && c<=c2 ) seen = 1;
120962 prior_c = 0;
120963 }else{
120964 if( c==c2 ){
120965 seen = 1;
120966 }
120967 prior_c = c2;
120968 }
120969 c2 = sqlite3Utf8Read(pz: &zPattern);
120970 }
120971 if( c2==0 || (seen ^ invert)==0 ){
120972 return SQLITE_NOMATCH;
120973 }
120974 continue;
120975 }
120976 }
120977 c2 = Utf8Read(zString);
120978 if( c==c2 ) continue;
120979 if( noCase && sqlite3Tolower(c)==sqlite3Tolower(c2) && c<0x80 && c2<0x80 ){
120980 continue;
120981 }
120982 if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue;
120983 return SQLITE_NOMATCH;
120984 }
120985 return *zString==0 ? SQLITE_MATCH : SQLITE_NOMATCH;
120986}
120987
120988/*
120989** The sqlite3_strglob() interface. Return 0 on a match (like strcmp()) and
120990** non-zero if there is no match.
120991*/
120992SQLITE_API int sqlite3_strglob(const char *zGlobPattern, const char *zString){
120993 return patternCompare(zPattern: (u8*)zGlobPattern, zString: (u8*)zString, pInfo: &globInfo, matchOther: '[');
120994}
120995
120996/*
120997** The sqlite3_strlike() interface. Return 0 on a match and non-zero for
120998** a miss - like strcmp().
120999*/
121000SQLITE_API int sqlite3_strlike(const char *zPattern, const char *zStr, unsigned int esc){
121001 return patternCompare(zPattern: (u8*)zPattern, zString: (u8*)zStr, pInfo: &likeInfoNorm, matchOther: esc);
121002}
121003
121004/*
121005** Count the number of times that the LIKE operator (or GLOB which is
121006** just a variation of LIKE) gets called. This is used for testing
121007** only.
121008*/
121009#ifdef SQLITE_TEST
121010SQLITE_API int sqlite3_like_count = 0;
121011#endif
121012
121013
121014/*
121015** Implementation of the like() SQL function. This function implements
121016** the build-in LIKE operator. The first argument to the function is the
121017** pattern and the second argument is the string. So, the SQL statements:
121018**
121019** A LIKE B
121020**
121021** is implemented as like(B,A).
121022**
121023** This same function (with a different compareInfo structure) computes
121024** the GLOB operator.
121025*/
121026static void likeFunc(
121027 sqlite3_context *context,
121028 int argc,
121029 sqlite3_value **argv
121030){
121031 const unsigned char *zA, *zB;
121032 u32 escape;
121033 int nPat;
121034 sqlite3 *db = sqlite3_context_db_handle(p: context);
121035 struct compareInfo *pInfo = sqlite3_user_data(p: context);
121036 struct compareInfo backupInfo;
121037
121038#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
121039 if( sqlite3_value_type(argv[0])==SQLITE_BLOB
121040 || sqlite3_value_type(argv[1])==SQLITE_BLOB
121041 ){
121042#ifdef SQLITE_TEST
121043 sqlite3_like_count++;
121044#endif
121045 sqlite3_result_int(context, 0);
121046 return;
121047 }
121048#endif
121049
121050 /* Limit the length of the LIKE or GLOB pattern to avoid problems
121051 ** of deep recursion and N*N behavior in patternCompare().
121052 */
121053 nPat = sqlite3_value_bytes(pVal: argv[0]);
121054 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
121055 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
121056 if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
121057 sqlite3_result_error(pCtx: context, z: "LIKE or GLOB pattern too complex", n: -1);
121058 return;
121059 }
121060 if( argc==3 ){
121061 /* The escape character string must consist of a single UTF-8 character.
121062 ** Otherwise, return an error.
121063 */
121064 const unsigned char *zEsc = sqlite3_value_text(pVal: argv[2]);
121065 if( zEsc==0 ) return;
121066 if( sqlite3Utf8CharLen(zIn: (char*)zEsc, nByte: -1)!=1 ){
121067 sqlite3_result_error(pCtx: context,
121068 z: "ESCAPE expression must be a single character", n: -1);
121069 return;
121070 }
121071 escape = sqlite3Utf8Read(pz: &zEsc);
121072 if( escape==pInfo->matchAll || escape==pInfo->matchOne ){
121073 memcpy(dest: &backupInfo, src: pInfo, n: sizeof(backupInfo));
121074 pInfo = &backupInfo;
121075 if( escape==pInfo->matchAll ) pInfo->matchAll = 0;
121076 if( escape==pInfo->matchOne ) pInfo->matchOne = 0;
121077 }
121078 }else{
121079 escape = pInfo->matchSet;
121080 }
121081 zB = sqlite3_value_text(pVal: argv[0]);
121082 zA = sqlite3_value_text(pVal: argv[1]);
121083 if( zA && zB ){
121084#ifdef SQLITE_TEST
121085 sqlite3_like_count++;
121086#endif
121087 sqlite3_result_int(pCtx: context,
121088 iVal: patternCompare(zPattern: zB, zString: zA, pInfo, matchOther: escape)==SQLITE_MATCH);
121089 }
121090}
121091
121092/*
121093** Implementation of the NULLIF(x,y) function. The result is the first
121094** argument if the arguments are different. The result is NULL if the
121095** arguments are equal to each other.
121096*/
121097static void nullifFunc(
121098 sqlite3_context *context,
121099 int NotUsed,
121100 sqlite3_value **argv
121101){
121102 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
121103 UNUSED_PARAMETER(NotUsed);
121104 if( sqlite3MemCompare(pMem1: argv[0], pMem2: argv[1], pColl)!=0 ){
121105 sqlite3_result_value(pCtx: context, pValue: argv[0]);
121106 }
121107}
121108
121109/*
121110** Implementation of the sqlite_version() function. The result is the version
121111** of the SQLite library that is running.
121112*/
121113static void versionFunc(
121114 sqlite3_context *context,
121115 int NotUsed,
121116 sqlite3_value **NotUsed2
121117){
121118 UNUSED_PARAMETER2(NotUsed, NotUsed2);
121119 /* IMP: R-48699-48617 This function is an SQL wrapper around the
121120 ** sqlite3_libversion() C-interface. */
121121 sqlite3_result_text(pCtx: context, z: sqlite3_libversion(), n: -1, SQLITE_STATIC);
121122}
121123
121124/*
121125** Implementation of the sqlite_source_id() function. The result is a string
121126** that identifies the particular version of the source code used to build
121127** SQLite.
121128*/
121129static void sourceidFunc(
121130 sqlite3_context *context,
121131 int NotUsed,
121132 sqlite3_value **NotUsed2
121133){
121134 UNUSED_PARAMETER2(NotUsed, NotUsed2);
121135 /* IMP: R-24470-31136 This function is an SQL wrapper around the
121136 ** sqlite3_sourceid() C interface. */
121137 sqlite3_result_text(pCtx: context, z: sqlite3_sourceid(), n: -1, SQLITE_STATIC);
121138}
121139
121140/*
121141** Implementation of the sqlite_log() function. This is a wrapper around
121142** sqlite3_log(). The return value is NULL. The function exists purely for
121143** its side-effects.
121144*/
121145static void errlogFunc(
121146 sqlite3_context *context,
121147 int argc,
121148 sqlite3_value **argv
121149){
121150 UNUSED_PARAMETER(argc);
121151 UNUSED_PARAMETER(context);
121152 sqlite3_log(iErrCode: sqlite3_value_int(pVal: argv[0]), zFormat: "%s", sqlite3_value_text(pVal: argv[1]));
121153}
121154
121155/*
121156** Implementation of the sqlite_compileoption_used() function.
121157** The result is an integer that identifies if the compiler option
121158** was used to build SQLite.
121159*/
121160#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
121161static void compileoptionusedFunc(
121162 sqlite3_context *context,
121163 int argc,
121164 sqlite3_value **argv
121165){
121166 const char *zOptName;
121167 assert( argc==1 );
121168 UNUSED_PARAMETER(argc);
121169 /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
121170 ** function is a wrapper around the sqlite3_compileoption_used() C/C++
121171 ** function.
121172 */
121173 if( (zOptName = (const char*)sqlite3_value_text(pVal: argv[0]))!=0 ){
121174 sqlite3_result_int(pCtx: context, iVal: sqlite3_compileoption_used(zOptName));
121175 }
121176}
121177#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
121178
121179/*
121180** Implementation of the sqlite_compileoption_get() function.
121181** The result is a string that identifies the compiler options
121182** used to build SQLite.
121183*/
121184#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
121185static void compileoptiongetFunc(
121186 sqlite3_context *context,
121187 int argc,
121188 sqlite3_value **argv
121189){
121190 int n;
121191 assert( argc==1 );
121192 UNUSED_PARAMETER(argc);
121193 /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
121194 ** is a wrapper around the sqlite3_compileoption_get() C/C++ function.
121195 */
121196 n = sqlite3_value_int(pVal: argv[0]);
121197 sqlite3_result_text(pCtx: context, z: sqlite3_compileoption_get(N: n), n: -1, SQLITE_STATIC);
121198}
121199#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
121200
121201/* Array for converting from half-bytes (nybbles) into ASCII hex
121202** digits. */
121203static const char hexdigits[] = {
121204 '0', '1', '2', '3', '4', '5', '6', '7',
121205 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
121206};
121207
121208/*
121209** Implementation of the QUOTE() function. This function takes a single
121210** argument. If the argument is numeric, the return value is the same as
121211** the argument. If the argument is NULL, the return value is the string
121212** "NULL". Otherwise, the argument is enclosed in single quotes with
121213** single-quote escapes.
121214*/
121215static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
121216 assert( argc==1 );
121217 UNUSED_PARAMETER(argc);
121218 switch( sqlite3_value_type(pVal: argv[0]) ){
121219 case SQLITE_FLOAT: {
121220 double r1, r2;
121221 char zBuf[50];
121222 r1 = sqlite3_value_double(pVal: argv[0]);
121223 sqlite3_snprintf(n: sizeof(zBuf), zBuf, zFormat: "%!.15g", r1);
121224 sqlite3AtoF(z: zBuf, pResult: &r2, length: 20, SQLITE_UTF8);
121225 if( r1!=r2 ){
121226 sqlite3_snprintf(n: sizeof(zBuf), zBuf, zFormat: "%!.20e", r1);
121227 }
121228 sqlite3_result_text(pCtx: context, z: zBuf, n: -1, SQLITE_TRANSIENT);
121229 break;
121230 }
121231 case SQLITE_INTEGER: {
121232 sqlite3_result_value(pCtx: context, pValue: argv[0]);
121233 break;
121234 }
121235 case SQLITE_BLOB: {
121236 char *zText = 0;
121237 char const *zBlob = sqlite3_value_blob(pVal: argv[0]);
121238 int nBlob = sqlite3_value_bytes(pVal: argv[0]);
121239 assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
121240 zText = (char *)contextMalloc(context, nByte: (2*(i64)nBlob)+4);
121241 if( zText ){
121242 int i;
121243 for(i=0; i<nBlob; i++){
121244 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
121245 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
121246 }
121247 zText[(nBlob*2)+2] = '\'';
121248 zText[(nBlob*2)+3] = '\0';
121249 zText[0] = 'X';
121250 zText[1] = '\'';
121251 sqlite3_result_text(pCtx: context, z: zText, n: -1, SQLITE_TRANSIENT);
121252 sqlite3_free(p: zText);
121253 }
121254 break;
121255 }
121256 case SQLITE_TEXT: {
121257 int i,j;
121258 u64 n;
121259 const unsigned char *zArg = sqlite3_value_text(pVal: argv[0]);
121260 char *z;
121261
121262 if( zArg==0 ) return;
121263 for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
121264 z = contextMalloc(context, nByte: ((i64)i)+((i64)n)+3);
121265 if( z ){
121266 z[0] = '\'';
121267 for(i=0, j=1; zArg[i]; i++){
121268 z[j++] = zArg[i];
121269 if( zArg[i]=='\'' ){
121270 z[j++] = '\'';
121271 }
121272 }
121273 z[j++] = '\'';
121274 z[j] = 0;
121275 sqlite3_result_text(pCtx: context, z, n: j, xDel: sqlite3_free);
121276 }
121277 break;
121278 }
121279 default: {
121280 assert( sqlite3_value_type(argv[0])==SQLITE_NULL );
121281 sqlite3_result_text(pCtx: context, z: "NULL", n: 4, SQLITE_STATIC);
121282 break;
121283 }
121284 }
121285}
121286
121287/*
121288** The unicode() function. Return the integer unicode code-point value
121289** for the first character of the input string.
121290*/
121291static void unicodeFunc(
121292 sqlite3_context *context,
121293 int argc,
121294 sqlite3_value **argv
121295){
121296 const unsigned char *z = sqlite3_value_text(pVal: argv[0]);
121297 (void)argc;
121298 if( z && z[0] ) sqlite3_result_int(pCtx: context, iVal: sqlite3Utf8Read(pz: &z));
121299}
121300
121301/*
121302** The char() function takes zero or more arguments, each of which is
121303** an integer. It constructs a string where each character of the string
121304** is the unicode character for the corresponding integer argument.
121305*/
121306static void charFunc(
121307 sqlite3_context *context,
121308 int argc,
121309 sqlite3_value **argv
121310){
121311 unsigned char *z, *zOut;
121312 int i;
121313 zOut = z = sqlite3_malloc64( n: argc*4+1 );
121314 if( z==0 ){
121315 sqlite3_result_error_nomem(pCtx: context);
121316 return;
121317 }
121318 for(i=0; i<argc; i++){
121319 sqlite3_int64 x;
121320 unsigned c;
121321 x = sqlite3_value_int64(pVal: argv[i]);
121322 if( x<0 || x>0x10ffff ) x = 0xfffd;
121323 c = (unsigned)(x & 0x1fffff);
121324 if( c<0x00080 ){
121325 *zOut++ = (u8)(c&0xFF);
121326 }else if( c<0x00800 ){
121327 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);
121328 *zOut++ = 0x80 + (u8)(c & 0x3F);
121329 }else if( c<0x10000 ){
121330 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);
121331 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
121332 *zOut++ = 0x80 + (u8)(c & 0x3F);
121333 }else{
121334 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);
121335 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);
121336 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
121337 *zOut++ = 0x80 + (u8)(c & 0x3F);
121338 } \
121339 }
121340 sqlite3_result_text64(pCtx: context, z: (char*)z, n: zOut-z, xDel: sqlite3_free, SQLITE_UTF8);
121341}
121342
121343/*
121344** The hex() function. Interpret the argument as a blob. Return
121345** a hexadecimal rendering as text.
121346*/
121347static void hexFunc(
121348 sqlite3_context *context,
121349 int argc,
121350 sqlite3_value **argv
121351){
121352 int i, n;
121353 const unsigned char *pBlob;
121354 char *zHex, *z;
121355 assert( argc==1 );
121356 UNUSED_PARAMETER(argc);
121357 pBlob = sqlite3_value_blob(pVal: argv[0]);
121358 n = sqlite3_value_bytes(pVal: argv[0]);
121359 assert( pBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
121360 z = zHex = contextMalloc(context, nByte: ((i64)n)*2 + 1);
121361 if( zHex ){
121362 for(i=0; i<n; i++, pBlob++){
121363 unsigned char c = *pBlob;
121364 *(z++) = hexdigits[(c>>4)&0xf];
121365 *(z++) = hexdigits[c&0xf];
121366 }
121367 *z = 0;
121368 sqlite3_result_text(pCtx: context, z: zHex, n: n*2, xDel: sqlite3_free);
121369 }
121370}
121371
121372/*
121373** The zeroblob(N) function returns a zero-filled blob of size N bytes.
121374*/
121375static void zeroblobFunc(
121376 sqlite3_context *context,
121377 int argc,
121378 sqlite3_value **argv
121379){
121380 i64 n;
121381 int rc;
121382 assert( argc==1 );
121383 UNUSED_PARAMETER(argc);
121384 n = sqlite3_value_int64(pVal: argv[0]);
121385 if( n<0 ) n = 0;
121386 rc = sqlite3_result_zeroblob64(pCtx: context, n); /* IMP: R-00293-64994 */
121387 if( rc ){
121388 sqlite3_result_error_code(pCtx: context, errCode: rc);
121389 }
121390}
121391
121392/*
121393** The replace() function. Three arguments are all strings: call
121394** them A, B, and C. The result is also a string which is derived
121395** from A by replacing every occurrence of B with C. The match
121396** must be exact. Collating sequences are not used.
121397*/
121398static void replaceFunc(
121399 sqlite3_context *context,
121400 int argc,
121401 sqlite3_value **argv
121402){
121403 const unsigned char *zStr; /* The input string A */
121404 const unsigned char *zPattern; /* The pattern string B */
121405 const unsigned char *zRep; /* The replacement string C */
121406 unsigned char *zOut; /* The output */
121407 int nStr; /* Size of zStr */
121408 int nPattern; /* Size of zPattern */
121409 int nRep; /* Size of zRep */
121410 i64 nOut; /* Maximum size of zOut */
121411 int loopLimit; /* Last zStr[] that might match zPattern[] */
121412 int i, j; /* Loop counters */
121413 unsigned cntExpand; /* Number zOut expansions */
121414 sqlite3 *db = sqlite3_context_db_handle(p: context);
121415
121416 assert( argc==3 );
121417 UNUSED_PARAMETER(argc);
121418 zStr = sqlite3_value_text(pVal: argv[0]);
121419 if( zStr==0 ) return;
121420 nStr = sqlite3_value_bytes(pVal: argv[0]);
121421 assert( zStr==sqlite3_value_text(argv[0]) ); /* No encoding change */
121422 zPattern = sqlite3_value_text(pVal: argv[1]);
121423 if( zPattern==0 ){
121424 assert( sqlite3_value_type(argv[1])==SQLITE_NULL
121425 || sqlite3_context_db_handle(context)->mallocFailed );
121426 return;
121427 }
121428 if( zPattern[0]==0 ){
121429 assert( sqlite3_value_type(argv[1])!=SQLITE_NULL );
121430 sqlite3_result_value(pCtx: context, pValue: argv[0]);
121431 return;
121432 }
121433 nPattern = sqlite3_value_bytes(pVal: argv[1]);
121434 assert( zPattern==sqlite3_value_text(argv[1]) ); /* No encoding change */
121435 zRep = sqlite3_value_text(pVal: argv[2]);
121436 if( zRep==0 ) return;
121437 nRep = sqlite3_value_bytes(pVal: argv[2]);
121438 assert( zRep==sqlite3_value_text(argv[2]) );
121439 nOut = nStr + 1;
121440 assert( nOut<SQLITE_MAX_LENGTH );
121441 zOut = contextMalloc(context, nByte: (i64)nOut);
121442 if( zOut==0 ){
121443 return;
121444 }
121445 loopLimit = nStr - nPattern;
121446 cntExpand = 0;
121447 for(i=j=0; i<=loopLimit; i++){
121448 if( zStr[i]!=zPattern[0] || memcmp(s1: &zStr[i], s2: zPattern, n: nPattern) ){
121449 zOut[j++] = zStr[i];
121450 }else{
121451 if( nRep>nPattern ){
121452 nOut += nRep - nPattern;
121453 testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
121454 testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
121455 if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
121456 sqlite3_result_error_toobig(pCtx: context);
121457 sqlite3_free(p: zOut);
121458 return;
121459 }
121460 cntExpand++;
121461 if( (cntExpand&(cntExpand-1))==0 ){
121462 /* Grow the size of the output buffer only on substitutions
121463 ** whose index is a power of two: 1, 2, 4, 8, 16, 32, ... */
121464 u8 *zOld;
121465 zOld = zOut;
121466 zOut = sqlite3Realloc(pOld: zOut, nBytes: (int)nOut + (nOut - nStr - 1));
121467 if( zOut==0 ){
121468 sqlite3_result_error_nomem(pCtx: context);
121469 sqlite3_free(p: zOld);
121470 return;
121471 }
121472 }
121473 }
121474 memcpy(dest: &zOut[j], src: zRep, n: nRep);
121475 j += nRep;
121476 i += nPattern-1;
121477 }
121478 }
121479 assert( j+nStr-i+1<=nOut );
121480 memcpy(dest: &zOut[j], src: &zStr[i], n: nStr-i);
121481 j += nStr - i;
121482 assert( j<=nOut );
121483 zOut[j] = 0;
121484 sqlite3_result_text(pCtx: context, z: (char*)zOut, n: j, xDel: sqlite3_free);
121485}
121486
121487/*
121488** Implementation of the TRIM(), LTRIM(), and RTRIM() functions.
121489** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both.
121490*/
121491static void trimFunc(
121492 sqlite3_context *context,
121493 int argc,
121494 sqlite3_value **argv
121495){
121496 const unsigned char *zIn; /* Input string */
121497 const unsigned char *zCharSet; /* Set of characters to trim */
121498 unsigned int nIn; /* Number of bytes in input */
121499 int flags; /* 1: trimleft 2: trimright 3: trim */
121500 int i; /* Loop counter */
121501 unsigned int *aLen = 0; /* Length of each character in zCharSet */
121502 unsigned char **azChar = 0; /* Individual characters in zCharSet */
121503 int nChar; /* Number of characters in zCharSet */
121504
121505 if( sqlite3_value_type(pVal: argv[0])==SQLITE_NULL ){
121506 return;
121507 }
121508 zIn = sqlite3_value_text(pVal: argv[0]);
121509 if( zIn==0 ) return;
121510 nIn = (unsigned)sqlite3_value_bytes(pVal: argv[0]);
121511 assert( zIn==sqlite3_value_text(argv[0]) );
121512 if( argc==1 ){
121513 static const unsigned lenOne[] = { 1 };
121514 static unsigned char * const azOne[] = { (u8*)" " };
121515 nChar = 1;
121516 aLen = (unsigned*)lenOne;
121517 azChar = (unsigned char **)azOne;
121518 zCharSet = 0;
121519 }else if( (zCharSet = sqlite3_value_text(pVal: argv[1]))==0 ){
121520 return;
121521 }else{
121522 const unsigned char *z;
121523 for(z=zCharSet, nChar=0; *z; nChar++){
121524 SQLITE_SKIP_UTF8(z);
121525 }
121526 if( nChar>0 ){
121527 azChar = contextMalloc(context,
121528 nByte: ((i64)nChar)*(sizeof(char*)+sizeof(unsigned)));
121529 if( azChar==0 ){
121530 return;
121531 }
121532 aLen = (unsigned*)&azChar[nChar];
121533 for(z=zCharSet, nChar=0; *z; nChar++){
121534 azChar[nChar] = (unsigned char *)z;
121535 SQLITE_SKIP_UTF8(z);
121536 aLen[nChar] = (unsigned)(z - azChar[nChar]);
121537 }
121538 }
121539 }
121540 if( nChar>0 ){
121541 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context));
121542 if( flags & 1 ){
121543 while( nIn>0 ){
121544 unsigned int len = 0;
121545 for(i=0; i<nChar; i++){
121546 len = aLen[i];
121547 if( len<=nIn && memcmp(s1: zIn, s2: azChar[i], n: len)==0 ) break;
121548 }
121549 if( i>=nChar ) break;
121550 zIn += len;
121551 nIn -= len;
121552 }
121553 }
121554 if( flags & 2 ){
121555 while( nIn>0 ){
121556 unsigned int len = 0;
121557 for(i=0; i<nChar; i++){
121558 len = aLen[i];
121559 if( len<=nIn && memcmp(s1: &zIn[nIn-len],s2: azChar[i],n: len)==0 ) break;
121560 }
121561 if( i>=nChar ) break;
121562 nIn -= len;
121563 }
121564 }
121565 if( zCharSet ){
121566 sqlite3_free(p: azChar);
121567 }
121568 }
121569 sqlite3_result_text(pCtx: context, z: (char*)zIn, n: nIn, SQLITE_TRANSIENT);
121570}
121571
121572
121573#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
121574/*
121575** The "unknown" function is automatically substituted in place of
121576** any unrecognized function name when doing an EXPLAIN or EXPLAIN QUERY PLAN
121577** when the SQLITE_ENABLE_UNKNOWN_FUNCTION compile-time option is used.
121578** When the "sqlite3" command-line shell is built using this functionality,
121579** that allows an EXPLAIN or EXPLAIN QUERY PLAN for complex queries
121580** involving application-defined functions to be examined in a generic
121581** sqlite3 shell.
121582*/
121583static void unknownFunc(
121584 sqlite3_context *context,
121585 int argc,
121586 sqlite3_value **argv
121587){
121588 /* no-op */
121589}
121590#endif /*SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION*/
121591
121592
121593/* IMP: R-25361-16150 This function is omitted from SQLite by default. It
121594** is only available if the SQLITE_SOUNDEX compile-time option is used
121595** when SQLite is built.
121596*/
121597#ifdef SQLITE_SOUNDEX
121598/*
121599** Compute the soundex encoding of a word.
121600**
121601** IMP: R-59782-00072 The soundex(X) function returns a string that is the
121602** soundex encoding of the string X.
121603*/
121604static void soundexFunc(
121605 sqlite3_context *context,
121606 int argc,
121607 sqlite3_value **argv
121608){
121609 char zResult[8];
121610 const u8 *zIn;
121611 int i, j;
121612 static const unsigned char iCode[] = {
121613 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
121614 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
121615 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
121616 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
121617 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
121618 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
121619 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
121620 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
121621 };
121622 assert( argc==1 );
121623 zIn = (u8*)sqlite3_value_text(argv[0]);
121624 if( zIn==0 ) zIn = (u8*)"";
121625 for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){}
121626 if( zIn[i] ){
121627 u8 prevcode = iCode[zIn[i]&0x7f];
121628 zResult[0] = sqlite3Toupper(zIn[i]);
121629 for(j=1; j<4 && zIn[i]; i++){
121630 int code = iCode[zIn[i]&0x7f];
121631 if( code>0 ){
121632 if( code!=prevcode ){
121633 prevcode = code;
121634 zResult[j++] = code + '0';
121635 }
121636 }else{
121637 prevcode = 0;
121638 }
121639 }
121640 while( j<4 ){
121641 zResult[j++] = '0';
121642 }
121643 zResult[j] = 0;
121644 sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
121645 }else{
121646 /* IMP: R-64894-50321 The string "?000" is returned if the argument
121647 ** is NULL or contains no ASCII alphabetic characters. */
121648 sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
121649 }
121650}
121651#endif /* SQLITE_SOUNDEX */
121652
121653#ifndef SQLITE_OMIT_LOAD_EXTENSION
121654/*
121655** A function that loads a shared-library extension then returns NULL.
121656*/
121657static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
121658 const char *zFile = (const char *)sqlite3_value_text(pVal: argv[0]);
121659 const char *zProc;
121660 sqlite3 *db = sqlite3_context_db_handle(p: context);
121661 char *zErrMsg = 0;
121662
121663 /* Disallow the load_extension() SQL function unless the SQLITE_LoadExtFunc
121664 ** flag is set. See the sqlite3_enable_load_extension() API.
121665 */
121666 if( (db->flags & SQLITE_LoadExtFunc)==0 ){
121667 sqlite3_result_error(pCtx: context, z: "not authorized", n: -1);
121668 return;
121669 }
121670
121671 if( argc==2 ){
121672 zProc = (const char *)sqlite3_value_text(pVal: argv[1]);
121673 }else{
121674 zProc = 0;
121675 }
121676 if( zFile && sqlite3_load_extension(db, zFile, zProc, pzErrMsg: &zErrMsg) ){
121677 sqlite3_result_error(pCtx: context, z: zErrMsg, n: -1);
121678 sqlite3_free(p: zErrMsg);
121679 }
121680}
121681#endif
121682
121683
121684/*
121685** An instance of the following structure holds the context of a
121686** sum() or avg() aggregate computation.
121687*/
121688typedef struct SumCtx SumCtx;
121689struct SumCtx {
121690 double rSum; /* Floating point sum */
121691 i64 iSum; /* Integer sum */
121692 i64 cnt; /* Number of elements summed */
121693 u8 overflow; /* True if integer overflow seen */
121694 u8 approx; /* True if non-integer value was input to the sum */
121695};
121696
121697/*
121698** Routines used to compute the sum, average, and total.
121699**
121700** The SUM() function follows the (broken) SQL standard which means
121701** that it returns NULL if it sums over no inputs. TOTAL returns
121702** 0.0 in that case. In addition, TOTAL always returns a float where
121703** SUM might return an integer if it never encounters a floating point
121704** value. TOTAL never fails, but SUM might through an exception if
121705** it overflows an integer.
121706*/
121707static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
121708 SumCtx *p;
121709 int type;
121710 assert( argc==1 );
121711 UNUSED_PARAMETER(argc);
121712 p = sqlite3_aggregate_context(p: context, nByte: sizeof(*p));
121713 type = sqlite3_value_numeric_type(pVal: argv[0]);
121714 if( p && type!=SQLITE_NULL ){
121715 p->cnt++;
121716 if( type==SQLITE_INTEGER ){
121717 i64 v = sqlite3_value_int64(pVal: argv[0]);
121718 p->rSum += v;
121719 if( (p->approx|p->overflow)==0 && sqlite3AddInt64(pA: &p->iSum, iB: v) ){
121720 p->approx = p->overflow = 1;
121721 }
121722 }else{
121723 p->rSum += sqlite3_value_double(pVal: argv[0]);
121724 p->approx = 1;
121725 }
121726 }
121727}
121728#ifndef SQLITE_OMIT_WINDOWFUNC
121729static void sumInverse(sqlite3_context *context, int argc, sqlite3_value**argv){
121730 SumCtx *p;
121731 int type;
121732 assert( argc==1 );
121733 UNUSED_PARAMETER(argc);
121734 p = sqlite3_aggregate_context(p: context, nByte: sizeof(*p));
121735 type = sqlite3_value_numeric_type(pVal: argv[0]);
121736 /* p is always non-NULL because sumStep() will have been called first
121737 ** to initialize it */
121738 if( ALWAYS(p) && type!=SQLITE_NULL ){
121739 assert( p->cnt>0 );
121740 p->cnt--;
121741 assert( type==SQLITE_INTEGER || p->approx );
121742 if( type==SQLITE_INTEGER && p->approx==0 ){
121743 i64 v = sqlite3_value_int64(pVal: argv[0]);
121744 p->rSum -= v;
121745 p->iSum -= v;
121746 }else{
121747 p->rSum -= sqlite3_value_double(pVal: argv[0]);
121748 }
121749 }
121750}
121751#else
121752# define sumInverse 0
121753#endif /* SQLITE_OMIT_WINDOWFUNC */
121754static void sumFinalize(sqlite3_context *context){
121755 SumCtx *p;
121756 p = sqlite3_aggregate_context(p: context, nByte: 0);
121757 if( p && p->cnt>0 ){
121758 if( p->overflow ){
121759 sqlite3_result_error(pCtx: context,z: "integer overflow",n: -1);
121760 }else if( p->approx ){
121761 sqlite3_result_double(pCtx: context, rVal: p->rSum);
121762 }else{
121763 sqlite3_result_int64(pCtx: context, iVal: p->iSum);
121764 }
121765 }
121766}
121767static void avgFinalize(sqlite3_context *context){
121768 SumCtx *p;
121769 p = sqlite3_aggregate_context(p: context, nByte: 0);
121770 if( p && p->cnt>0 ){
121771 sqlite3_result_double(pCtx: context, rVal: p->rSum/(double)p->cnt);
121772 }
121773}
121774static void totalFinalize(sqlite3_context *context){
121775 SumCtx *p;
121776 p = sqlite3_aggregate_context(p: context, nByte: 0);
121777 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
121778 sqlite3_result_double(pCtx: context, rVal: p ? p->rSum : (double)0);
121779}
121780
121781/*
121782** The following structure keeps track of state information for the
121783** count() aggregate function.
121784*/
121785typedef struct CountCtx CountCtx;
121786struct CountCtx {
121787 i64 n;
121788#ifdef SQLITE_DEBUG
121789 int bInverse; /* True if xInverse() ever called */
121790#endif
121791};
121792
121793/*
121794** Routines to implement the count() aggregate function.
121795*/
121796static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
121797 CountCtx *p;
121798 p = sqlite3_aggregate_context(p: context, nByte: sizeof(*p));
121799 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(pVal: argv[0])) && p ){
121800 p->n++;
121801 }
121802
121803#ifndef SQLITE_OMIT_DEPRECATED
121804 /* The sqlite3_aggregate_count() function is deprecated. But just to make
121805 ** sure it still operates correctly, verify that its count agrees with our
121806 ** internal count when using count(*) and when the total count can be
121807 ** expressed as a 32-bit integer. */
121808 assert( argc==1 || p==0 || p->n>0x7fffffff || p->bInverse
121809 || p->n==sqlite3_aggregate_count(context) );
121810#endif
121811}
121812static void countFinalize(sqlite3_context *context){
121813 CountCtx *p;
121814 p = sqlite3_aggregate_context(p: context, nByte: 0);
121815 sqlite3_result_int64(pCtx: context, iVal: p ? p->n : 0);
121816}
121817#ifndef SQLITE_OMIT_WINDOWFUNC
121818static void countInverse(sqlite3_context *ctx, int argc, sqlite3_value **argv){
121819 CountCtx *p;
121820 p = sqlite3_aggregate_context(p: ctx, nByte: sizeof(*p));
121821 /* p is always non-NULL since countStep() will have been called first */
121822 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(pVal: argv[0])) && ALWAYS(p) ){
121823 p->n--;
121824#ifdef SQLITE_DEBUG
121825 p->bInverse = 1;
121826#endif
121827 }
121828}
121829#else
121830# define countInverse 0
121831#endif /* SQLITE_OMIT_WINDOWFUNC */
121832
121833/*
121834** Routines to implement min() and max() aggregate functions.
121835*/
121836static void minmaxStep(
121837 sqlite3_context *context,
121838 int NotUsed,
121839 sqlite3_value **argv
121840){
121841 Mem *pArg = (Mem *)argv[0];
121842 Mem *pBest;
121843 UNUSED_PARAMETER(NotUsed);
121844
121845 pBest = (Mem *)sqlite3_aggregate_context(p: context, nByte: sizeof(*pBest));
121846 if( !pBest ) return;
121847
121848 if( sqlite3_value_type(pVal: pArg)==SQLITE_NULL ){
121849 if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
121850 }else if( pBest->flags ){
121851 int max;
121852 int cmp;
121853 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
121854 /* This step function is used for both the min() and max() aggregates,
121855 ** the only difference between the two being that the sense of the
121856 ** comparison is inverted. For the max() aggregate, the
121857 ** sqlite3_user_data() function returns (void *)-1. For min() it
121858 ** returns (void *)db, where db is the sqlite3* database pointer.
121859 ** Therefore the next statement sets variable 'max' to 1 for the max()
121860 ** aggregate, or 0 for min().
121861 */
121862 max = sqlite3_user_data(p: context)!=0;
121863 cmp = sqlite3MemCompare(pMem1: pBest, pMem2: pArg, pColl);
121864 if( (max && cmp<0) || (!max && cmp>0) ){
121865 sqlite3VdbeMemCopy(pTo: pBest, pFrom: pArg);
121866 }else{
121867 sqlite3SkipAccumulatorLoad(context);
121868 }
121869 }else{
121870 pBest->db = sqlite3_context_db_handle(p: context);
121871 sqlite3VdbeMemCopy(pTo: pBest, pFrom: pArg);
121872 }
121873}
121874static void minMaxValueFinalize(sqlite3_context *context, int bValue){
121875 sqlite3_value *pRes;
121876 pRes = (sqlite3_value *)sqlite3_aggregate_context(p: context, nByte: 0);
121877 if( pRes ){
121878 if( pRes->flags ){
121879 sqlite3_result_value(pCtx: context, pValue: pRes);
121880 }
121881 if( bValue==0 ) sqlite3VdbeMemRelease(p: pRes);
121882 }
121883}
121884#ifndef SQLITE_OMIT_WINDOWFUNC
121885static void minMaxValue(sqlite3_context *context){
121886 minMaxValueFinalize(context, bValue: 1);
121887}
121888#else
121889# define minMaxValue 0
121890#endif /* SQLITE_OMIT_WINDOWFUNC */
121891static void minMaxFinalize(sqlite3_context *context){
121892 minMaxValueFinalize(context, bValue: 0);
121893}
121894
121895/*
121896** group_concat(EXPR, ?SEPARATOR?)
121897**
121898** The SEPARATOR goes before the EXPR string. This is tragic. The
121899** groupConcatInverse() implementation would have been easier if the
121900** SEPARATOR were appended after EXPR. And the order is undocumented,
121901** so we could change it, in theory. But the old behavior has been
121902** around for so long that we dare not, for fear of breaking something.
121903*/
121904typedef struct {
121905 StrAccum str; /* The accumulated concatenation */
121906#ifndef SQLITE_OMIT_WINDOWFUNC
121907 int nAccum; /* Number of strings presently concatenated */
121908 int nFirstSepLength; /* Used to detect separator length change */
121909 /* If pnSepLengths!=0, refs an array of inter-string separator lengths,
121910 ** stored as actually incorporated into presently accumulated result.
121911 ** (Hence, its slots in use number nAccum-1 between method calls.)
121912 ** If pnSepLengths==0, nFirstSepLength is the length used throughout.
121913 */
121914 int *pnSepLengths;
121915#endif
121916} GroupConcatCtx;
121917
121918static void groupConcatStep(
121919 sqlite3_context *context,
121920 int argc,
121921 sqlite3_value **argv
121922){
121923 const char *zVal;
121924 GroupConcatCtx *pGCC;
121925 const char *zSep;
121926 int nVal, nSep;
121927 assert( argc==1 || argc==2 );
121928 if( sqlite3_value_type(pVal: argv[0])==SQLITE_NULL ) return;
121929 pGCC = (GroupConcatCtx*)sqlite3_aggregate_context(p: context, nByte: sizeof(*pGCC));
121930 if( pGCC ){
121931 sqlite3 *db = sqlite3_context_db_handle(p: context);
121932 int firstTerm = pGCC->str.mxAlloc==0;
121933 pGCC->str.mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
121934 if( argc==1 ){
121935 if( !firstTerm ){
121936 sqlite3_str_appendchar(p: &pGCC->str, N: 1, c: ',');
121937 }
121938#ifndef SQLITE_OMIT_WINDOWFUNC
121939 else{
121940 pGCC->nFirstSepLength = 1;
121941 }
121942#endif
121943 }else if( !firstTerm ){
121944 zSep = (char*)sqlite3_value_text(pVal: argv[1]);
121945 nSep = sqlite3_value_bytes(pVal: argv[1]);
121946 if( zSep ){
121947 sqlite3_str_append(p: &pGCC->str, z: zSep, N: nSep);
121948 }
121949#ifndef SQLITE_OMIT_WINDOWFUNC
121950 else{
121951 nSep = 0;
121952 }
121953 if( nSep != pGCC->nFirstSepLength || pGCC->pnSepLengths != 0 ){
121954 int *pnsl = pGCC->pnSepLengths;
121955 if( pnsl == 0 ){
121956 /* First separator length variation seen, start tracking them. */
121957 pnsl = (int*)sqlite3_malloc64(n: (pGCC->nAccum+1) * sizeof(int));
121958 if( pnsl!=0 ){
121959 int i = 0, nA = pGCC->nAccum-1;
121960 while( i<nA ) pnsl[i++] = pGCC->nFirstSepLength;
121961 }
121962 }else{
121963 pnsl = (int*)sqlite3_realloc64(pOld: pnsl, n: pGCC->nAccum * sizeof(int));
121964 }
121965 if( pnsl!=0 ){
121966 if( ALWAYS(pGCC->nAccum>0) ){
121967 pnsl[pGCC->nAccum-1] = nSep;
121968 }
121969 pGCC->pnSepLengths = pnsl;
121970 }else{
121971 sqlite3StrAccumSetError(p: &pGCC->str, SQLITE_NOMEM);
121972 }
121973 }
121974#endif
121975 }
121976#ifndef SQLITE_OMIT_WINDOWFUNC
121977 else{
121978 pGCC->nFirstSepLength = sqlite3_value_bytes(pVal: argv[1]);
121979 }
121980 pGCC->nAccum += 1;
121981#endif
121982 zVal = (char*)sqlite3_value_text(pVal: argv[0]);
121983 nVal = sqlite3_value_bytes(pVal: argv[0]);
121984 if( zVal ) sqlite3_str_append(p: &pGCC->str, z: zVal, N: nVal);
121985 }
121986}
121987
121988#ifndef SQLITE_OMIT_WINDOWFUNC
121989static void groupConcatInverse(
121990 sqlite3_context *context,
121991 int argc,
121992 sqlite3_value **argv
121993){
121994 GroupConcatCtx *pGCC;
121995 assert( argc==1 || argc==2 );
121996 (void)argc; /* Suppress unused parameter warning */
121997 if( sqlite3_value_type(pVal: argv[0])==SQLITE_NULL ) return;
121998 pGCC = (GroupConcatCtx*)sqlite3_aggregate_context(p: context, nByte: sizeof(*pGCC));
121999 /* pGCC is always non-NULL since groupConcatStep() will have always
122000 ** run frist to initialize it */
122001 if( ALWAYS(pGCC) ){
122002 int nVS;
122003 /* Must call sqlite3_value_text() to convert the argument into text prior
122004 ** to invoking sqlite3_value_bytes(), in case the text encoding is UTF16 */
122005 (void)sqlite3_value_text(pVal: argv[0]);
122006 nVS = sqlite3_value_bytes(pVal: argv[0]);
122007 pGCC->nAccum -= 1;
122008 if( pGCC->pnSepLengths!=0 ){
122009 assert(pGCC->nAccum >= 0);
122010 if( pGCC->nAccum>0 ){
122011 nVS += *pGCC->pnSepLengths;
122012 memmove(dest: pGCC->pnSepLengths, src: pGCC->pnSepLengths+1,
122013 n: (pGCC->nAccum-1)*sizeof(int));
122014 }
122015 }else{
122016 /* If removing single accumulated string, harmlessly over-do. */
122017 nVS += pGCC->nFirstSepLength;
122018 }
122019 if( nVS>=(int)pGCC->str.nChar ){
122020 pGCC->str.nChar = 0;
122021 }else{
122022 pGCC->str.nChar -= nVS;
122023 memmove(dest: pGCC->str.zText, src: &pGCC->str.zText[nVS], n: pGCC->str.nChar);
122024 }
122025 if( pGCC->str.nChar==0 ){
122026 pGCC->str.mxAlloc = 0;
122027 sqlite3_free(p: pGCC->pnSepLengths);
122028 pGCC->pnSepLengths = 0;
122029 }
122030 }
122031}
122032#else
122033# define groupConcatInverse 0
122034#endif /* SQLITE_OMIT_WINDOWFUNC */
122035static void groupConcatFinalize(sqlite3_context *context){
122036 GroupConcatCtx *pGCC
122037 = (GroupConcatCtx*)sqlite3_aggregate_context(p: context, nByte: 0);
122038 if( pGCC ){
122039 sqlite3ResultStrAccum(pCtx: context, p: &pGCC->str);
122040#ifndef SQLITE_OMIT_WINDOWFUNC
122041 sqlite3_free(p: pGCC->pnSepLengths);
122042#endif
122043 }
122044}
122045#ifndef SQLITE_OMIT_WINDOWFUNC
122046static void groupConcatValue(sqlite3_context *context){
122047 GroupConcatCtx *pGCC
122048 = (GroupConcatCtx*)sqlite3_aggregate_context(p: context, nByte: 0);
122049 if( pGCC ){
122050 StrAccum *pAccum = &pGCC->str;
122051 if( pAccum->accError==SQLITE_TOOBIG ){
122052 sqlite3_result_error_toobig(pCtx: context);
122053 }else if( pAccum->accError==SQLITE_NOMEM ){
122054 sqlite3_result_error_nomem(pCtx: context);
122055 }else{
122056 const char *zText = sqlite3_str_value(p: pAccum);
122057 sqlite3_result_text(pCtx: context, z: zText, n: pAccum->nChar, SQLITE_TRANSIENT);
122058 }
122059 }
122060}
122061#else
122062# define groupConcatValue 0
122063#endif /* SQLITE_OMIT_WINDOWFUNC */
122064
122065/*
122066** This routine does per-connection function registration. Most
122067** of the built-in functions above are part of the global function set.
122068** This routine only deals with those that are not global.
122069*/
122070SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3 *db){
122071 int rc = sqlite3_overload_function(db, zFuncName: "MATCH", nArg: 2);
122072 assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
122073 if( rc==SQLITE_NOMEM ){
122074 sqlite3OomFault(db);
122075 }
122076}
122077
122078/*
122079** Re-register the built-in LIKE functions. The caseSensitive
122080** parameter determines whether or not the LIKE operator is case
122081** sensitive.
122082*/
122083SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
122084 struct compareInfo *pInfo;
122085 int flags;
122086 if( caseSensitive ){
122087 pInfo = (struct compareInfo*)&likeInfoAlt;
122088 flags = SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE;
122089 }else{
122090 pInfo = (struct compareInfo*)&likeInfoNorm;
122091 flags = SQLITE_FUNC_LIKE;
122092 }
122093 sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0, 0, pDestructor: 0);
122094 sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0, 0, pDestructor: 0);
122095 sqlite3FindFunction(db, zName: "like", nArg: 2, SQLITE_UTF8, createFlag: 0)->funcFlags |= flags;
122096 sqlite3FindFunction(db, zName: "like", nArg: 3, SQLITE_UTF8, createFlag: 0)->funcFlags |= flags;
122097}
122098
122099/*
122100** pExpr points to an expression which implements a function. If
122101** it is appropriate to apply the LIKE optimization to that function
122102** then set aWc[0] through aWc[2] to the wildcard characters and the
122103** escape character and then return TRUE. If the function is not a
122104** LIKE-style function then return FALSE.
122105**
122106** The expression "a LIKE b ESCAPE c" is only considered a valid LIKE
122107** operator if c is a string literal that is exactly one byte in length.
122108** That one byte is stored in aWc[3]. aWc[3] is set to zero if there is
122109** no ESCAPE clause.
122110**
122111** *pIsNocase is set to true if uppercase and lowercase are equivalent for
122112** the function (default for LIKE). If the function makes the distinction
122113** between uppercase and lowercase (as does GLOB) then *pIsNocase is set to
122114** false.
122115*/
122116SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
122117 FuncDef *pDef;
122118 int nExpr;
122119 assert( pExpr!=0 );
122120 assert( pExpr->op==TK_FUNCTION );
122121 assert( ExprUseXList(pExpr) );
122122 if( !pExpr->x.pList ){
122123 return 0;
122124 }
122125 nExpr = pExpr->x.pList->nExpr;
122126 assert( !ExprHasProperty(pExpr, EP_IntValue) );
122127 pDef = sqlite3FindFunction(db, zName: pExpr->u.zToken, nArg: nExpr, SQLITE_UTF8, createFlag: 0);
122128#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
122129 if( pDef==0 ) return 0;
122130#endif
122131 if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){
122132 return 0;
122133 }
122134
122135 /* The memcpy() statement assumes that the wildcard characters are
122136 ** the first three statements in the compareInfo structure. The
122137 ** asserts() that follow verify that assumption
122138 */
122139 memcpy(dest: aWc, src: pDef->pUserData, n: 3);
122140 assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
122141 assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
122142 assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
122143
122144 if( nExpr<3 ){
122145 aWc[3] = 0;
122146 }else{
122147 Expr *pEscape = pExpr->x.pList->a[2].pExpr;
122148 char *zEscape;
122149 if( pEscape->op!=TK_STRING ) return 0;
122150 assert( !ExprHasProperty(pEscape, EP_IntValue) );
122151 zEscape = pEscape->u.zToken;
122152 if( zEscape[0]==0 || zEscape[1]!=0 ) return 0;
122153 if( zEscape[0]==aWc[0] ) return 0;
122154 if( zEscape[0]==aWc[1] ) return 0;
122155 aWc[3] = zEscape[0];
122156 }
122157
122158 *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0;
122159 return 1;
122160}
122161
122162/* Mathematical Constants */
122163#ifndef M_PI
122164# define M_PI 3.141592653589793238462643383279502884
122165#endif
122166#ifndef M_LN10
122167# define M_LN10 2.302585092994045684017991454684364208
122168#endif
122169#ifndef M_LN2
122170# define M_LN2 0.693147180559945309417232121458176568
122171#endif
122172
122173
122174/* Extra math functions that require linking with -lm
122175*/
122176#ifdef SQLITE_ENABLE_MATH_FUNCTIONS
122177/*
122178** Implementation SQL functions:
122179**
122180** ceil(X)
122181** ceiling(X)
122182** floor(X)
122183**
122184** The sqlite3_user_data() pointer is a pointer to the libm implementation
122185** of the underlying C function.
122186*/
122187static void ceilingFunc(
122188 sqlite3_context *context,
122189 int argc,
122190 sqlite3_value **argv
122191){
122192 assert( argc==1 );
122193 switch( sqlite3_value_numeric_type(argv[0]) ){
122194 case SQLITE_INTEGER: {
122195 sqlite3_result_int64(context, sqlite3_value_int64(argv[0]));
122196 break;
122197 }
122198 case SQLITE_FLOAT: {
122199 double (*x)(double) = (double(*)(double))sqlite3_user_data(context);
122200 sqlite3_result_double(context, x(sqlite3_value_double(argv[0])));
122201 break;
122202 }
122203 default: {
122204 break;
122205 }
122206 }
122207}
122208
122209/*
122210** On some systems, ceil() and floor() are intrinsic function. You are
122211** unable to take a pointer to these functions. Hence, we here wrap them
122212** in our own actual functions.
122213*/
122214static double xCeil(double x){ return ceil(x); }
122215static double xFloor(double x){ return floor(x); }
122216
122217/*
122218** Implementation of SQL functions:
122219**
122220** ln(X) - natural logarithm
122221** log(X) - log X base 10
122222** log10(X) - log X base 10
122223** log(B,X) - log X base B
122224*/
122225static void logFunc(
122226 sqlite3_context *context,
122227 int argc,
122228 sqlite3_value **argv
122229){
122230 double x, b, ans;
122231 assert( argc==1 || argc==2 );
122232 switch( sqlite3_value_numeric_type(argv[0]) ){
122233 case SQLITE_INTEGER:
122234 case SQLITE_FLOAT:
122235 x = sqlite3_value_double(argv[0]);
122236 if( x<=0.0 ) return;
122237 break;
122238 default:
122239 return;
122240 }
122241 if( argc==2 ){
122242 switch( sqlite3_value_numeric_type(argv[0]) ){
122243 case SQLITE_INTEGER:
122244 case SQLITE_FLOAT:
122245 b = log(x);
122246 if( b<=0.0 ) return;
122247 x = sqlite3_value_double(argv[1]);
122248 if( x<=0.0 ) return;
122249 break;
122250 default:
122251 return;
122252 }
122253 ans = log(x)/b;
122254 }else{
122255 ans = log(x);
122256 switch( SQLITE_PTR_TO_INT(sqlite3_user_data(context)) ){
122257 case 1:
122258 /* Convert from natural logarithm to log base 10 */
122259 ans *= 1.0/M_LN10;
122260 break;
122261 case 2:
122262 /* Convert from natural logarithm to log base 2 */
122263 ans *= 1.0/M_LN2;
122264 break;
122265 default:
122266 break;
122267 }
122268 }
122269 sqlite3_result_double(context, ans);
122270}
122271
122272/*
122273** Functions to converts degrees to radians and radians to degrees.
122274*/
122275static double degToRad(double x){ return x*(M_PI/180.0); }
122276static double radToDeg(double x){ return x*(180.0/M_PI); }
122277
122278/*
122279** Implementation of 1-argument SQL math functions:
122280**
122281** exp(X) - Compute e to the X-th power
122282*/
122283static void math1Func(
122284 sqlite3_context *context,
122285 int argc,
122286 sqlite3_value **argv
122287){
122288 int type0;
122289 double v0, ans;
122290 double (*x)(double);
122291 assert( argc==1 );
122292 type0 = sqlite3_value_numeric_type(argv[0]);
122293 if( type0!=SQLITE_INTEGER && type0!=SQLITE_FLOAT ) return;
122294 v0 = sqlite3_value_double(argv[0]);
122295 x = (double(*)(double))sqlite3_user_data(context);
122296 ans = x(v0);
122297 sqlite3_result_double(context, ans);
122298}
122299
122300/*
122301** Implementation of 2-argument SQL math functions:
122302**
122303** power(X,Y) - Compute X to the Y-th power
122304*/
122305static void math2Func(
122306 sqlite3_context *context,
122307 int argc,
122308 sqlite3_value **argv
122309){
122310 int type0, type1;
122311 double v0, v1, ans;
122312 double (*x)(double,double);
122313 assert( argc==2 );
122314 type0 = sqlite3_value_numeric_type(argv[0]);
122315 if( type0!=SQLITE_INTEGER && type0!=SQLITE_FLOAT ) return;
122316 type1 = sqlite3_value_numeric_type(argv[1]);
122317 if( type1!=SQLITE_INTEGER && type1!=SQLITE_FLOAT ) return;
122318 v0 = sqlite3_value_double(argv[0]);
122319 v1 = sqlite3_value_double(argv[1]);
122320 x = (double(*)(double,double))sqlite3_user_data(context);
122321 ans = x(v0, v1);
122322 sqlite3_result_double(context, ans);
122323}
122324
122325/*
122326** Implementation of 0-argument pi() function.
122327*/
122328static void piFunc(
122329 sqlite3_context *context,
122330 int argc,
122331 sqlite3_value **argv
122332){
122333 assert( argc==0 );
122334 sqlite3_result_double(context, M_PI);
122335}
122336
122337#endif /* SQLITE_ENABLE_MATH_FUNCTIONS */
122338
122339/*
122340** Implementation of sign(X) function.
122341*/
122342static void signFunc(
122343 sqlite3_context *context,
122344 int argc,
122345 sqlite3_value **argv
122346){
122347 int type0;
122348 double x;
122349 UNUSED_PARAMETER(argc);
122350 assert( argc==1 );
122351 type0 = sqlite3_value_numeric_type(pVal: argv[0]);
122352 if( type0!=SQLITE_INTEGER && type0!=SQLITE_FLOAT ) return;
122353 x = sqlite3_value_double(pVal: argv[0]);
122354 sqlite3_result_int(pCtx: context, iVal: x<0.0 ? -1 : x>0.0 ? +1 : 0);
122355}
122356
122357/*
122358** All of the FuncDef structures in the aBuiltinFunc[] array above
122359** to the global function hash table. This occurs at start-time (as
122360** a consequence of calling sqlite3_initialize()).
122361**
122362** After this routine runs
122363*/
122364SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void){
122365 /*
122366 ** The following array holds FuncDef structures for all of the functions
122367 ** defined in this file.
122368 **
122369 ** The array cannot be constant since changes are made to the
122370 ** FuncDef.pHash elements at start-time. The elements of this array
122371 ** are read-only after initialization is complete.
122372 **
122373 ** For peak efficiency, put the most frequently used function last.
122374 */
122375 static FuncDef aBuiltinFunc[] = {
122376/***** Functions only available with SQLITE_TESTCTRL_INTERNAL_FUNCTIONS *****/
122377#if !defined(SQLITE_UNTESTABLE)
122378 TEST_FUNC(implies_nonnull_row, 2, INLINEFUNC_implies_nonnull_row, 0),
122379 TEST_FUNC(expr_compare, 2, INLINEFUNC_expr_compare, 0),
122380 TEST_FUNC(expr_implies_expr, 2, INLINEFUNC_expr_implies_expr, 0),
122381 TEST_FUNC(affinity, 1, INLINEFUNC_affinity, 0),
122382#endif /* !defined(SQLITE_UNTESTABLE) */
122383/***** Regular functions *****/
122384#ifdef SQLITE_SOUNDEX
122385 FUNCTION(soundex, 1, 0, 0, soundexFunc ),
122386#endif
122387#ifndef SQLITE_OMIT_LOAD_EXTENSION
122388 SFUNCTION(load_extension, 1, 0, 0, loadExt ),
122389 SFUNCTION(load_extension, 2, 0, 0, loadExt ),
122390#endif
122391#if SQLITE_USER_AUTHENTICATION
122392 FUNCTION(sqlite_crypt, 2, 0, 0, sqlite3CryptFunc ),
122393#endif
122394#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
122395 DFUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc ),
122396 DFUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ),
122397#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
122398 INLINE_FUNC(unlikely, 1, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY),
122399 INLINE_FUNC(likelihood, 2, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY),
122400 INLINE_FUNC(likely, 1, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY),
122401#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
122402 FUNCTION2(sqlite_offset, 1, 0, 0, noopFunc, SQLITE_FUNC_OFFSET|
122403 SQLITE_FUNC_TYPEOF),
122404#endif
122405 FUNCTION(ltrim, 1, 1, 0, trimFunc ),
122406 FUNCTION(ltrim, 2, 1, 0, trimFunc ),
122407 FUNCTION(rtrim, 1, 2, 0, trimFunc ),
122408 FUNCTION(rtrim, 2, 2, 0, trimFunc ),
122409 FUNCTION(trim, 1, 3, 0, trimFunc ),
122410 FUNCTION(trim, 2, 3, 0, trimFunc ),
122411 FUNCTION(min, -1, 0, 1, minmaxFunc ),
122412 FUNCTION(min, 0, 0, 1, 0 ),
122413 WAGGREGATE(min, 1, 0, 1, minmaxStep, minMaxFinalize, minMaxValue, 0,
122414 SQLITE_FUNC_MINMAX|SQLITE_FUNC_ANYORDER ),
122415 FUNCTION(max, -1, 1, 1, minmaxFunc ),
122416 FUNCTION(max, 0, 1, 1, 0 ),
122417 WAGGREGATE(max, 1, 1, 1, minmaxStep, minMaxFinalize, minMaxValue, 0,
122418 SQLITE_FUNC_MINMAX|SQLITE_FUNC_ANYORDER ),
122419 FUNCTION2(typeof, 1, 0, 0, typeofFunc, SQLITE_FUNC_TYPEOF),
122420 FUNCTION2(length, 1, 0, 0, lengthFunc, SQLITE_FUNC_LENGTH),
122421 FUNCTION(instr, 2, 0, 0, instrFunc ),
122422 FUNCTION(printf, -1, 0, 0, printfFunc ),
122423 FUNCTION(unicode, 1, 0, 0, unicodeFunc ),
122424 FUNCTION(char, -1, 0, 0, charFunc ),
122425 FUNCTION(abs, 1, 0, 0, absFunc ),
122426#ifndef SQLITE_OMIT_FLOATING_POINT
122427 FUNCTION(round, 1, 0, 0, roundFunc ),
122428 FUNCTION(round, 2, 0, 0, roundFunc ),
122429#endif
122430 FUNCTION(upper, 1, 0, 0, upperFunc ),
122431 FUNCTION(lower, 1, 0, 0, lowerFunc ),
122432 FUNCTION(hex, 1, 0, 0, hexFunc ),
122433 INLINE_FUNC(ifnull, 2, INLINEFUNC_coalesce, 0 ),
122434 VFUNCTION(random, 0, 0, 0, randomFunc ),
122435 VFUNCTION(randomblob, 1, 0, 0, randomBlob ),
122436 FUNCTION(nullif, 2, 0, 1, nullifFunc ),
122437 DFUNCTION(sqlite_version, 0, 0, 0, versionFunc ),
122438 DFUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ),
122439 FUNCTION(sqlite_log, 2, 0, 0, errlogFunc ),
122440 FUNCTION(quote, 1, 0, 0, quoteFunc ),
122441 VFUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid),
122442 VFUNCTION(changes, 0, 0, 0, changes ),
122443 VFUNCTION(total_changes, 0, 0, 0, total_changes ),
122444 FUNCTION(replace, 3, 0, 0, replaceFunc ),
122445 FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc ),
122446 FUNCTION(substr, 2, 0, 0, substrFunc ),
122447 FUNCTION(substr, 3, 0, 0, substrFunc ),
122448 FUNCTION(substring, 2, 0, 0, substrFunc ),
122449 FUNCTION(substring, 3, 0, 0, substrFunc ),
122450 WAGGREGATE(sum, 1,0,0, sumStep, sumFinalize, sumFinalize, sumInverse, 0),
122451 WAGGREGATE(total, 1,0,0, sumStep,totalFinalize,totalFinalize,sumInverse, 0),
122452 WAGGREGATE(avg, 1,0,0, sumStep, avgFinalize, avgFinalize, sumInverse, 0),
122453 WAGGREGATE(count, 0,0,0, countStep,
122454 countFinalize, countFinalize, countInverse,
122455 SQLITE_FUNC_COUNT|SQLITE_FUNC_ANYORDER ),
122456 WAGGREGATE(count, 1,0,0, countStep,
122457 countFinalize, countFinalize, countInverse, SQLITE_FUNC_ANYORDER ),
122458 WAGGREGATE(group_concat, 1, 0, 0, groupConcatStep,
122459 groupConcatFinalize, groupConcatValue, groupConcatInverse, 0),
122460 WAGGREGATE(group_concat, 2, 0, 0, groupConcatStep,
122461 groupConcatFinalize, groupConcatValue, groupConcatInverse, 0),
122462
122463 LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
122464#ifdef SQLITE_CASE_SENSITIVE_LIKE
122465 LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
122466 LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
122467#else
122468 LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE),
122469 LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE),
122470#endif
122471#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
122472 FUNCTION(unknown, -1, 0, 0, unknownFunc ),
122473#endif
122474 FUNCTION(coalesce, 1, 0, 0, 0 ),
122475 FUNCTION(coalesce, 0, 0, 0, 0 ),
122476#ifdef SQLITE_ENABLE_MATH_FUNCTIONS
122477 MFUNCTION(ceil, 1, xCeil, ceilingFunc ),
122478 MFUNCTION(ceiling, 1, xCeil, ceilingFunc ),
122479 MFUNCTION(floor, 1, xFloor, ceilingFunc ),
122480#if SQLITE_HAVE_C99_MATH_FUNCS
122481 MFUNCTION(trunc, 1, trunc, ceilingFunc ),
122482#endif
122483 FUNCTION(ln, 1, 0, 0, logFunc ),
122484 FUNCTION(log, 1, 1, 0, logFunc ),
122485 FUNCTION(log10, 1, 1, 0, logFunc ),
122486 FUNCTION(log2, 1, 2, 0, logFunc ),
122487 FUNCTION(log, 2, 0, 0, logFunc ),
122488 MFUNCTION(exp, 1, exp, math1Func ),
122489 MFUNCTION(pow, 2, pow, math2Func ),
122490 MFUNCTION(power, 2, pow, math2Func ),
122491 MFUNCTION(mod, 2, fmod, math2Func ),
122492 MFUNCTION(acos, 1, acos, math1Func ),
122493 MFUNCTION(asin, 1, asin, math1Func ),
122494 MFUNCTION(atan, 1, atan, math1Func ),
122495 MFUNCTION(atan2, 2, atan2, math2Func ),
122496 MFUNCTION(cos, 1, cos, math1Func ),
122497 MFUNCTION(sin, 1, sin, math1Func ),
122498 MFUNCTION(tan, 1, tan, math1Func ),
122499 MFUNCTION(cosh, 1, cosh, math1Func ),
122500 MFUNCTION(sinh, 1, sinh, math1Func ),
122501 MFUNCTION(tanh, 1, tanh, math1Func ),
122502#if SQLITE_HAVE_C99_MATH_FUNCS
122503 MFUNCTION(acosh, 1, acosh, math1Func ),
122504 MFUNCTION(asinh, 1, asinh, math1Func ),
122505 MFUNCTION(atanh, 1, atanh, math1Func ),
122506#endif
122507 MFUNCTION(sqrt, 1, sqrt, math1Func ),
122508 MFUNCTION(radians, 1, degToRad, math1Func ),
122509 MFUNCTION(degrees, 1, radToDeg, math1Func ),
122510 FUNCTION(pi, 0, 0, 0, piFunc ),
122511#endif /* SQLITE_ENABLE_MATH_FUNCTIONS */
122512 FUNCTION(sign, 1, 0, 0, signFunc ),
122513 INLINE_FUNC(coalesce, -1, INLINEFUNC_coalesce, 0 ),
122514 INLINE_FUNC(iif, 3, INLINEFUNC_iif, 0 ),
122515 };
122516#ifndef SQLITE_OMIT_ALTERTABLE
122517 sqlite3AlterFunctions();
122518#endif
122519 sqlite3WindowFunctions();
122520 sqlite3RegisterDateTimeFunctions();
122521 sqlite3InsertBuiltinFuncs(aDef: aBuiltinFunc, ArraySize(aBuiltinFunc));
122522
122523#if 0 /* Enable to print out how the built-in functions are hashed */
122524 {
122525 int i;
122526 FuncDef *p;
122527 for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){
122528 printf("FUNC-HASH %02d:", i);
122529 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash){
122530 int n = sqlite3Strlen30(p->zName);
122531 int h = p->zName[0] + n;
122532 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
122533 printf(" %s(%d)", p->zName, h);
122534 }
122535 printf("\n");
122536 }
122537 }
122538#endif
122539}
122540
122541/************** End of func.c ************************************************/
122542/************** Begin file fkey.c ********************************************/
122543/*
122544**
122545** The author disclaims copyright to this source code. In place of
122546** a legal notice, here is a blessing:
122547**
122548** May you do good and not evil.
122549** May you find forgiveness for yourself and forgive others.
122550** May you share freely, never taking more than you give.
122551**
122552*************************************************************************
122553** This file contains code used by the compiler to add foreign key
122554** support to compiled SQL statements.
122555*/
122556/* #include "sqliteInt.h" */
122557
122558#ifndef SQLITE_OMIT_FOREIGN_KEY
122559#ifndef SQLITE_OMIT_TRIGGER
122560
122561/*
122562** Deferred and Immediate FKs
122563** --------------------------
122564**
122565** Foreign keys in SQLite come in two flavours: deferred and immediate.
122566** If an immediate foreign key constraint is violated,
122567** SQLITE_CONSTRAINT_FOREIGNKEY is returned and the current
122568** statement transaction rolled back. If a
122569** deferred foreign key constraint is violated, no action is taken
122570** immediately. However if the application attempts to commit the
122571** transaction before fixing the constraint violation, the attempt fails.
122572**
122573** Deferred constraints are implemented using a simple counter associated
122574** with the database handle. The counter is set to zero each time a
122575** database transaction is opened. Each time a statement is executed
122576** that causes a foreign key violation, the counter is incremented. Each
122577** time a statement is executed that removes an existing violation from
122578** the database, the counter is decremented. When the transaction is
122579** committed, the commit fails if the current value of the counter is
122580** greater than zero. This scheme has two big drawbacks:
122581**
122582** * When a commit fails due to a deferred foreign key constraint,
122583** there is no way to tell which foreign constraint is not satisfied,
122584** or which row it is not satisfied for.
122585**
122586** * If the database contains foreign key violations when the
122587** transaction is opened, this may cause the mechanism to malfunction.
122588**
122589** Despite these problems, this approach is adopted as it seems simpler
122590** than the alternatives.
122591**
122592** INSERT operations:
122593**
122594** I.1) For each FK for which the table is the child table, search
122595** the parent table for a match. If none is found increment the
122596** constraint counter.
122597**
122598** I.2) For each FK for which the table is the parent table,
122599** search the child table for rows that correspond to the new
122600** row in the parent table. Decrement the counter for each row
122601** found (as the constraint is now satisfied).
122602**
122603** DELETE operations:
122604**
122605** D.1) For each FK for which the table is the child table,
122606** search the parent table for a row that corresponds to the
122607** deleted row in the child table. If such a row is not found,
122608** decrement the counter.
122609**
122610** D.2) For each FK for which the table is the parent table, search
122611** the child table for rows that correspond to the deleted row
122612** in the parent table. For each found increment the counter.
122613**
122614** UPDATE operations:
122615**
122616** An UPDATE command requires that all 4 steps above are taken, but only
122617** for FK constraints for which the affected columns are actually
122618** modified (values must be compared at runtime).
122619**
122620** Note that I.1 and D.1 are very similar operations, as are I.2 and D.2.
122621** This simplifies the implementation a bit.
122622**
122623** For the purposes of immediate FK constraints, the OR REPLACE conflict
122624** resolution is considered to delete rows before the new row is inserted.
122625** If a delete caused by OR REPLACE violates an FK constraint, an exception
122626** is thrown, even if the FK constraint would be satisfied after the new
122627** row is inserted.
122628**
122629** Immediate constraints are usually handled similarly. The only difference
122630** is that the counter used is stored as part of each individual statement
122631** object (struct Vdbe). If, after the statement has run, its immediate
122632** constraint counter is greater than zero,
122633** it returns SQLITE_CONSTRAINT_FOREIGNKEY
122634** and the statement transaction is rolled back. An exception is an INSERT
122635** statement that inserts a single row only (no triggers). In this case,
122636** instead of using a counter, an exception is thrown immediately if the
122637** INSERT violates a foreign key constraint. This is necessary as such
122638** an INSERT does not open a statement transaction.
122639**
122640** TODO: How should dropping a table be handled? How should renaming a
122641** table be handled?
122642**
122643**
122644** Query API Notes
122645** ---------------
122646**
122647** Before coding an UPDATE or DELETE row operation, the code-generator
122648** for those two operations needs to know whether or not the operation
122649** requires any FK processing and, if so, which columns of the original
122650** row are required by the FK processing VDBE code (i.e. if FKs were
122651** implemented using triggers, which of the old.* columns would be
122652** accessed). No information is required by the code-generator before
122653** coding an INSERT operation. The functions used by the UPDATE/DELETE
122654** generation code to query for this information are:
122655**
122656** sqlite3FkRequired() - Test to see if FK processing is required.
122657** sqlite3FkOldmask() - Query for the set of required old.* columns.
122658**
122659**
122660** Externally accessible module functions
122661** --------------------------------------
122662**
122663** sqlite3FkCheck() - Check for foreign key violations.
122664** sqlite3FkActions() - Code triggers for ON UPDATE/ON DELETE actions.
122665** sqlite3FkDelete() - Delete an FKey structure.
122666*/
122667
122668/*
122669** VDBE Calling Convention
122670** -----------------------
122671**
122672** Example:
122673**
122674** For the following INSERT statement:
122675**
122676** CREATE TABLE t1(a, b INTEGER PRIMARY KEY, c);
122677** INSERT INTO t1 VALUES(1, 2, 3.1);
122678**
122679** Register (x): 2 (type integer)
122680** Register (x+1): 1 (type integer)
122681** Register (x+2): NULL (type NULL)
122682** Register (x+3): 3.1 (type real)
122683*/
122684
122685/*
122686** A foreign key constraint requires that the key columns in the parent
122687** table are collectively subject to a UNIQUE or PRIMARY KEY constraint.
122688** Given that pParent is the parent table for foreign key constraint pFKey,
122689** search the schema for a unique index on the parent key columns.
122690**
122691** If successful, zero is returned. If the parent key is an INTEGER PRIMARY
122692** KEY column, then output variable *ppIdx is set to NULL. Otherwise, *ppIdx
122693** is set to point to the unique index.
122694**
122695** If the parent key consists of a single column (the foreign key constraint
122696** is not a composite foreign key), output variable *paiCol is set to NULL.
122697** Otherwise, it is set to point to an allocated array of size N, where
122698** N is the number of columns in the parent key. The first element of the
122699** array is the index of the child table column that is mapped by the FK
122700** constraint to the parent table column stored in the left-most column
122701** of index *ppIdx. The second element of the array is the index of the
122702** child table column that corresponds to the second left-most column of
122703** *ppIdx, and so on.
122704**
122705** If the required index cannot be found, either because:
122706**
122707** 1) The named parent key columns do not exist, or
122708**
122709** 2) The named parent key columns do exist, but are not subject to a
122710** UNIQUE or PRIMARY KEY constraint, or
122711**
122712** 3) No parent key columns were provided explicitly as part of the
122713** foreign key definition, and the parent table does not have a
122714** PRIMARY KEY, or
122715**
122716** 4) No parent key columns were provided explicitly as part of the
122717** foreign key definition, and the PRIMARY KEY of the parent table
122718** consists of a different number of columns to the child key in
122719** the child table.
122720**
122721** then non-zero is returned, and a "foreign key mismatch" error loaded
122722** into pParse. If an OOM error occurs, non-zero is returned and the
122723** pParse->db->mallocFailed flag is set.
122724*/
122725SQLITE_PRIVATE int sqlite3FkLocateIndex(
122726 Parse *pParse, /* Parse context to store any error in */
122727 Table *pParent, /* Parent table of FK constraint pFKey */
122728 FKey *pFKey, /* Foreign key to find index for */
122729 Index **ppIdx, /* OUT: Unique index on parent table */
122730 int **paiCol /* OUT: Map of index columns in pFKey */
122731){
122732 Index *pIdx = 0; /* Value to return via *ppIdx */
122733 int *aiCol = 0; /* Value to return via *paiCol */
122734 int nCol = pFKey->nCol; /* Number of columns in parent key */
122735 char *zKey = pFKey->aCol[0].zCol; /* Name of left-most parent key column */
122736
122737 /* The caller is responsible for zeroing output parameters. */
122738 assert( ppIdx && *ppIdx==0 );
122739 assert( !paiCol || *paiCol==0 );
122740 assert( pParse );
122741
122742 /* If this is a non-composite (single column) foreign key, check if it
122743 ** maps to the INTEGER PRIMARY KEY of table pParent. If so, leave *ppIdx
122744 ** and *paiCol set to zero and return early.
122745 **
122746 ** Otherwise, for a composite foreign key (more than one column), allocate
122747 ** space for the aiCol array (returned via output parameter *paiCol).
122748 ** Non-composite foreign keys do not require the aiCol array.
122749 */
122750 if( nCol==1 ){
122751 /* The FK maps to the IPK if any of the following are true:
122752 **
122753 ** 1) There is an INTEGER PRIMARY KEY column and the FK is implicitly
122754 ** mapped to the primary key of table pParent, or
122755 ** 2) The FK is explicitly mapped to a column declared as INTEGER
122756 ** PRIMARY KEY.
122757 */
122758 if( pParent->iPKey>=0 ){
122759 if( !zKey ) return 0;
122760 if( !sqlite3StrICmp(zLeft: pParent->aCol[pParent->iPKey].zCnName, zRight: zKey) ){
122761 return 0;
122762 }
122763 }
122764 }else if( paiCol ){
122765 assert( nCol>1 );
122766 aiCol = (int *)sqlite3DbMallocRawNN(db: pParse->db, n: nCol*sizeof(int));
122767 if( !aiCol ) return 1;
122768 *paiCol = aiCol;
122769 }
122770
122771 for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
122772 if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx) && pIdx->pPartIdxWhere==0 ){
122773 /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
122774 ** of columns. If each indexed column corresponds to a foreign key
122775 ** column of pFKey, then this index is a winner. */
122776
122777 if( zKey==0 ){
122778 /* If zKey is NULL, then this foreign key is implicitly mapped to
122779 ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be
122780 ** identified by the test. */
122781 if( IsPrimaryKeyIndex(pIdx) ){
122782 if( aiCol ){
122783 int i;
122784 for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
122785 }
122786 break;
122787 }
122788 }else{
122789 /* If zKey is non-NULL, then this foreign key was declared to
122790 ** map to an explicit list of columns in table pParent. Check if this
122791 ** index matches those columns. Also, check that the index uses
122792 ** the default collation sequences for each column. */
122793 int i, j;
122794 for(i=0; i<nCol; i++){
122795 i16 iCol = pIdx->aiColumn[i]; /* Index of column in parent tbl */
122796 const char *zDfltColl; /* Def. collation for column */
122797 char *zIdxCol; /* Name of indexed column */
122798
122799 if( iCol<0 ) break; /* No foreign keys against expression indexes */
122800
122801 /* If the index uses a collation sequence that is different from
122802 ** the default collation sequence for the column, this index is
122803 ** unusable. Bail out early in this case. */
122804 zDfltColl = sqlite3ColumnColl(pCol: &pParent->aCol[iCol]);
122805 if( !zDfltColl ) zDfltColl = sqlite3StrBINARY;
122806 if( sqlite3StrICmp(zLeft: pIdx->azColl[i], zRight: zDfltColl) ) break;
122807
122808 zIdxCol = pParent->aCol[iCol].zCnName;
122809 for(j=0; j<nCol; j++){
122810 if( sqlite3StrICmp(zLeft: pFKey->aCol[j].zCol, zRight: zIdxCol)==0 ){
122811 if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
122812 break;
122813 }
122814 }
122815 if( j==nCol ) break;
122816 }
122817 if( i==nCol ) break; /* pIdx is usable */
122818 }
122819 }
122820 }
122821
122822 if( !pIdx ){
122823 if( !pParse->disableTriggers ){
122824 sqlite3ErrorMsg(pParse,
122825 zFormat: "foreign key mismatch - \"%w\" referencing \"%w\"",
122826 pFKey->pFrom->zName, pFKey->zTo);
122827 }
122828 sqlite3DbFree(db: pParse->db, p: aiCol);
122829 return 1;
122830 }
122831
122832 *ppIdx = pIdx;
122833 return 0;
122834}
122835
122836/*
122837** This function is called when a row is inserted into or deleted from the
122838** child table of foreign key constraint pFKey. If an SQL UPDATE is executed
122839** on the child table of pFKey, this function is invoked twice for each row
122840** affected - once to "delete" the old row, and then again to "insert" the
122841** new row.
122842**
122843** Each time it is called, this function generates VDBE code to locate the
122844** row in the parent table that corresponds to the row being inserted into
122845** or deleted from the child table. If the parent row can be found, no
122846** special action is taken. Otherwise, if the parent row can *not* be
122847** found in the parent table:
122848**
122849** Operation | FK type | Action taken
122850** --------------------------------------------------------------------------
122851** INSERT immediate Increment the "immediate constraint counter".
122852**
122853** DELETE immediate Decrement the "immediate constraint counter".
122854**
122855** INSERT deferred Increment the "deferred constraint counter".
122856**
122857** DELETE deferred Decrement the "deferred constraint counter".
122858**
122859** These operations are identified in the comment at the top of this file
122860** (fkey.c) as "I.1" and "D.1".
122861*/
122862static void fkLookupParent(
122863 Parse *pParse, /* Parse context */
122864 int iDb, /* Index of database housing pTab */
122865 Table *pTab, /* Parent table of FK pFKey */
122866 Index *pIdx, /* Unique index on parent key columns in pTab */
122867 FKey *pFKey, /* Foreign key constraint */
122868 int *aiCol, /* Map from parent key columns to child table columns */
122869 int regData, /* Address of array containing child table row */
122870 int nIncr, /* Increment constraint counter by this */
122871 int isIgnore /* If true, pretend pTab contains all NULL values */
122872){
122873 int i; /* Iterator variable */
122874 Vdbe *v = sqlite3GetVdbe(pParse); /* Vdbe to add code to */
122875 int iCur = pParse->nTab - 1; /* Cursor number to use */
122876 int iOk = sqlite3VdbeMakeLabel(pParse); /* jump here if parent key found */
122877
122878 sqlite3VdbeVerifyAbortable(v,
122879 (!pFKey->isDeferred
122880 && !(pParse->db->flags & SQLITE_DeferFKs)
122881 && !pParse->pToplevel
122882 && !pParse->isMultiWrite) ? OE_Abort : OE_Ignore);
122883
122884 /* If nIncr is less than zero, then check at runtime if there are any
122885 ** outstanding constraints to resolve. If there are not, there is no need
122886 ** to check if deleting this row resolves any outstanding violations.
122887 **
122888 ** Check if any of the key columns in the child table row are NULL. If
122889 ** any are, then the constraint is considered satisfied. No need to
122890 ** search for a matching row in the parent table. */
122891 if( nIncr<0 ){
122892 sqlite3VdbeAddOp2(p: v, OP_FkIfZero, p1: pFKey->isDeferred, p2: iOk);
122893 VdbeCoverage(v);
122894 }
122895 for(i=0; i<pFKey->nCol; i++){
122896 int iReg = sqlite3TableColumnToStorage(pTab: pFKey->pFrom,iCol: aiCol[i]) + regData + 1;
122897 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: iReg, p2: iOk); VdbeCoverage(v);
122898 }
122899
122900 if( isIgnore==0 ){
122901 if( pIdx==0 ){
122902 /* If pIdx is NULL, then the parent key is the INTEGER PRIMARY KEY
122903 ** column of the parent table (table pTab). */
122904 int iMustBeInt; /* Address of MustBeInt instruction */
122905 int regTemp = sqlite3GetTempReg(pParse);
122906
122907 /* Invoke MustBeInt to coerce the child key value to an integer (i.e.
122908 ** apply the affinity of the parent key). If this fails, then there
122909 ** is no matching parent key. Before using MustBeInt, make a copy of
122910 ** the value. Otherwise, the value inserted into the child key column
122911 ** will have INTEGER affinity applied to it, which may not be correct. */
122912 sqlite3VdbeAddOp2(p: v, OP_SCopy,
122913 p1: sqlite3TableColumnToStorage(pTab: pFKey->pFrom,iCol: aiCol[0])+1+regData, p2: regTemp);
122914 iMustBeInt = sqlite3VdbeAddOp2(p: v, OP_MustBeInt, p1: regTemp, p2: 0);
122915 VdbeCoverage(v);
122916
122917 /* If the parent table is the same as the child table, and we are about
122918 ** to increment the constraint-counter (i.e. this is an INSERT operation),
122919 ** then check if the row being inserted matches itself. If so, do not
122920 ** increment the constraint-counter. */
122921 if( pTab==pFKey->pFrom && nIncr==1 ){
122922 sqlite3VdbeAddOp3(p: v, OP_Eq, p1: regData, p2: iOk, p3: regTemp); VdbeCoverage(v);
122923 sqlite3VdbeChangeP5(p: v, SQLITE_NOTNULL);
122924 }
122925
122926 sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
122927 sqlite3VdbeAddOp3(p: v, OP_NotExists, p1: iCur, p2: 0, p3: regTemp); VdbeCoverage(v);
122928 sqlite3VdbeGoto(p: v, iDest: iOk);
122929 sqlite3VdbeJumpHere(p: v, addr: sqlite3VdbeCurrentAddr(p: v)-2);
122930 sqlite3VdbeJumpHere(p: v, addr: iMustBeInt);
122931 sqlite3ReleaseTempReg(pParse, iReg: regTemp);
122932 }else{
122933 int nCol = pFKey->nCol;
122934 int regTemp = sqlite3GetTempRange(pParse, nReg: nCol);
122935 int regRec = sqlite3GetTempReg(pParse);
122936
122937 sqlite3VdbeAddOp3(p: v, OP_OpenRead, p1: iCur, p2: pIdx->tnum, p3: iDb);
122938 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
122939 for(i=0; i<nCol; i++){
122940 sqlite3VdbeAddOp2(p: v, OP_Copy,
122941 p1: sqlite3TableColumnToStorage(pTab: pFKey->pFrom, iCol: aiCol[i])+1+regData,
122942 p2: regTemp+i);
122943 }
122944
122945 /* If the parent table is the same as the child table, and we are about
122946 ** to increment the constraint-counter (i.e. this is an INSERT operation),
122947 ** then check if the row being inserted matches itself. If so, do not
122948 ** increment the constraint-counter.
122949 **
122950 ** If any of the parent-key values are NULL, then the row cannot match
122951 ** itself. So set JUMPIFNULL to make sure we do the OP_Found if any
122952 ** of the parent-key values are NULL (at this point it is known that
122953 ** none of the child key values are).
122954 */
122955 if( pTab==pFKey->pFrom && nIncr==1 ){
122956 int iJump = sqlite3VdbeCurrentAddr(p: v) + nCol + 1;
122957 for(i=0; i<nCol; i++){
122958 int iChild = sqlite3TableColumnToStorage(pTab: pFKey->pFrom,iCol: aiCol[i])
122959 +1+regData;
122960 int iParent = 1+regData;
122961 iParent += sqlite3TableColumnToStorage(pTab: pIdx->pTable,
122962 iCol: pIdx->aiColumn[i]);
122963 assert( pIdx->aiColumn[i]>=0 );
122964 assert( aiCol[i]!=pTab->iPKey );
122965 if( pIdx->aiColumn[i]==pTab->iPKey ){
122966 /* The parent key is a composite key that includes the IPK column */
122967 iParent = regData;
122968 }
122969 sqlite3VdbeAddOp3(p: v, OP_Ne, p1: iChild, p2: iJump, p3: iParent); VdbeCoverage(v);
122970 sqlite3VdbeChangeP5(p: v, SQLITE_JUMPIFNULL);
122971 }
122972 sqlite3VdbeGoto(p: v, iDest: iOk);
122973 }
122974
122975 sqlite3VdbeAddOp4(p: v, OP_MakeRecord, p1: regTemp, p2: nCol, p3: regRec,
122976 zP4: sqlite3IndexAffinityStr(pParse->db,pIdx), p4type: nCol);
122977 sqlite3VdbeAddOp4Int(p: v, OP_Found, p1: iCur, p2: iOk, p3: regRec, p4: 0); VdbeCoverage(v);
122978
122979 sqlite3ReleaseTempReg(pParse, iReg: regRec);
122980 sqlite3ReleaseTempRange(pParse, iReg: regTemp, nReg: nCol);
122981 }
122982 }
122983
122984 if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs)
122985 && !pParse->pToplevel
122986 && !pParse->isMultiWrite
122987 ){
122988 /* Special case: If this is an INSERT statement that will insert exactly
122989 ** one row into the table, raise a constraint immediately instead of
122990 ** incrementing a counter. This is necessary as the VM code is being
122991 ** generated for will not open a statement transaction. */
122992 assert( nIncr==1 );
122993 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
122994 OE_Abort, p4: 0, P4_STATIC, P5_ConstraintFK);
122995 }else{
122996 if( nIncr>0 && pFKey->isDeferred==0 ){
122997 sqlite3MayAbort(pParse);
122998 }
122999 sqlite3VdbeAddOp2(p: v, OP_FkCounter, p1: pFKey->isDeferred, p2: nIncr);
123000 }
123001
123002 sqlite3VdbeResolveLabel(v, x: iOk);
123003 sqlite3VdbeAddOp1(p: v, OP_Close, p1: iCur);
123004}
123005
123006
123007/*
123008** Return an Expr object that refers to a memory register corresponding
123009** to column iCol of table pTab.
123010**
123011** regBase is the first of an array of register that contains the data
123012** for pTab. regBase itself holds the rowid. regBase+1 holds the first
123013** column. regBase+2 holds the second column, and so forth.
123014*/
123015static Expr *exprTableRegister(
123016 Parse *pParse, /* Parsing and code generating context */
123017 Table *pTab, /* The table whose content is at r[regBase]... */
123018 int regBase, /* Contents of table pTab */
123019 i16 iCol /* Which column of pTab is desired */
123020){
123021 Expr *pExpr;
123022 Column *pCol;
123023 const char *zColl;
123024 sqlite3 *db = pParse->db;
123025
123026 pExpr = sqlite3Expr(db, TK_REGISTER, zToken: 0);
123027 if( pExpr ){
123028 if( iCol>=0 && iCol!=pTab->iPKey ){
123029 pCol = &pTab->aCol[iCol];
123030 pExpr->iTable = regBase + sqlite3TableColumnToStorage(pTab,iCol) + 1;
123031 pExpr->affExpr = pCol->affinity;
123032 zColl = sqlite3ColumnColl(pCol);
123033 if( zColl==0 ) zColl = db->pDfltColl->zName;
123034 pExpr = sqlite3ExprAddCollateString(pParse, pExpr, zC: zColl);
123035 }else{
123036 pExpr->iTable = regBase;
123037 pExpr->affExpr = SQLITE_AFF_INTEGER;
123038 }
123039 }
123040 return pExpr;
123041}
123042
123043/*
123044** Return an Expr object that refers to column iCol of table pTab which
123045** has cursor iCur.
123046*/
123047static Expr *exprTableColumn(
123048 sqlite3 *db, /* The database connection */
123049 Table *pTab, /* The table whose column is desired */
123050 int iCursor, /* The open cursor on the table */
123051 i16 iCol /* The column that is wanted */
123052){
123053 Expr *pExpr = sqlite3Expr(db, TK_COLUMN, zToken: 0);
123054 if( pExpr ){
123055 assert( ExprUseYTab(pExpr) );
123056 pExpr->y.pTab = pTab;
123057 pExpr->iTable = iCursor;
123058 pExpr->iColumn = iCol;
123059 }
123060 return pExpr;
123061}
123062
123063/*
123064** This function is called to generate code executed when a row is deleted
123065** from the parent table of foreign key constraint pFKey and, if pFKey is
123066** deferred, when a row is inserted into the same table. When generating
123067** code for an SQL UPDATE operation, this function may be called twice -
123068** once to "delete" the old row and once to "insert" the new row.
123069**
123070** Parameter nIncr is passed -1 when inserting a row (as this may decrease
123071** the number of FK violations in the db) or +1 when deleting one (as this
123072** may increase the number of FK constraint problems).
123073**
123074** The code generated by this function scans through the rows in the child
123075** table that correspond to the parent table row being deleted or inserted.
123076** For each child row found, one of the following actions is taken:
123077**
123078** Operation | FK type | Action taken
123079** --------------------------------------------------------------------------
123080** DELETE immediate Increment the "immediate constraint counter".
123081** Or, if the ON (UPDATE|DELETE) action is RESTRICT,
123082** throw a "FOREIGN KEY constraint failed" exception.
123083**
123084** INSERT immediate Decrement the "immediate constraint counter".
123085**
123086** DELETE deferred Increment the "deferred constraint counter".
123087** Or, if the ON (UPDATE|DELETE) action is RESTRICT,
123088** throw a "FOREIGN KEY constraint failed" exception.
123089**
123090** INSERT deferred Decrement the "deferred constraint counter".
123091**
123092** These operations are identified in the comment at the top of this file
123093** (fkey.c) as "I.2" and "D.2".
123094*/
123095static void fkScanChildren(
123096 Parse *pParse, /* Parse context */
123097 SrcList *pSrc, /* The child table to be scanned */
123098 Table *pTab, /* The parent table */
123099 Index *pIdx, /* Index on parent covering the foreign key */
123100 FKey *pFKey, /* The foreign key linking pSrc to pTab */
123101 int *aiCol, /* Map from pIdx cols to child table cols */
123102 int regData, /* Parent row data starts here */
123103 int nIncr /* Amount to increment deferred counter by */
123104){
123105 sqlite3 *db = pParse->db; /* Database handle */
123106 int i; /* Iterator variable */
123107 Expr *pWhere = 0; /* WHERE clause to scan with */
123108 NameContext sNameContext; /* Context used to resolve WHERE clause */
123109 WhereInfo *pWInfo; /* Context used by sqlite3WhereXXX() */
123110 int iFkIfZero = 0; /* Address of OP_FkIfZero */
123111 Vdbe *v = sqlite3GetVdbe(pParse);
123112
123113 assert( pIdx==0 || pIdx->pTable==pTab );
123114 assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol );
123115 assert( pIdx!=0 || pFKey->nCol==1 );
123116 assert( pIdx!=0 || HasRowid(pTab) );
123117
123118 if( nIncr<0 ){
123119 iFkIfZero = sqlite3VdbeAddOp2(p: v, OP_FkIfZero, p1: pFKey->isDeferred, p2: 0);
123120 VdbeCoverage(v);
123121 }
123122
123123 /* Create an Expr object representing an SQL expression like:
123124 **
123125 ** <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
123126 **
123127 ** The collation sequence used for the comparison should be that of
123128 ** the parent key columns. The affinity of the parent key column should
123129 ** be applied to each child key value before the comparison takes place.
123130 */
123131 for(i=0; i<pFKey->nCol; i++){
123132 Expr *pLeft; /* Value from parent table row */
123133 Expr *pRight; /* Column ref to child table */
123134 Expr *pEq; /* Expression (pLeft = pRight) */
123135 i16 iCol; /* Index of column in child table */
123136 const char *zCol; /* Name of column in child table */
123137
123138 iCol = pIdx ? pIdx->aiColumn[i] : -1;
123139 pLeft = exprTableRegister(pParse, pTab, regBase: regData, iCol);
123140 iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
123141 assert( iCol>=0 );
123142 zCol = pFKey->pFrom->aCol[iCol].zCnName;
123143 pRight = sqlite3Expr(db, TK_ID, zToken: zCol);
123144 pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight);
123145 pWhere = sqlite3ExprAnd(pParse, pLeft: pWhere, pRight: pEq);
123146 }
123147
123148 /* If the child table is the same as the parent table, then add terms
123149 ** to the WHERE clause that prevent this entry from being scanned.
123150 ** The added WHERE clause terms are like this:
123151 **
123152 ** $current_rowid!=rowid
123153 ** NOT( $current_a==a AND $current_b==b AND ... )
123154 **
123155 ** The first form is used for rowid tables. The second form is used
123156 ** for WITHOUT ROWID tables. In the second form, the *parent* key is
123157 ** (a,b,...). Either the parent or primary key could be used to
123158 ** uniquely identify the current row, but the parent key is more convenient
123159 ** as the required values have already been loaded into registers
123160 ** by the caller.
123161 */
123162 if( pTab==pFKey->pFrom && nIncr>0 ){
123163 Expr *pNe; /* Expression (pLeft != pRight) */
123164 Expr *pLeft; /* Value from parent table row */
123165 Expr *pRight; /* Column ref to child table */
123166 if( HasRowid(pTab) ){
123167 pLeft = exprTableRegister(pParse, pTab, regBase: regData, iCol: -1);
123168 pRight = exprTableColumn(db, pTab, iCursor: pSrc->a[0].iCursor, iCol: -1);
123169 pNe = sqlite3PExpr(pParse, TK_NE, pLeft, pRight);
123170 }else{
123171 Expr *pEq, *pAll = 0;
123172 assert( pIdx!=0 );
123173 for(i=0; i<pIdx->nKeyCol; i++){
123174 i16 iCol = pIdx->aiColumn[i];
123175 assert( iCol>=0 );
123176 pLeft = exprTableRegister(pParse, pTab, regBase: regData, iCol);
123177 pRight = sqlite3Expr(db, TK_ID, zToken: pTab->aCol[iCol].zCnName);
123178 pEq = sqlite3PExpr(pParse, TK_IS, pLeft, pRight);
123179 pAll = sqlite3ExprAnd(pParse, pLeft: pAll, pRight: pEq);
123180 }
123181 pNe = sqlite3PExpr(pParse, TK_NOT, pLeft: pAll, pRight: 0);
123182 }
123183 pWhere = sqlite3ExprAnd(pParse, pLeft: pWhere, pRight: pNe);
123184 }
123185
123186 /* Resolve the references in the WHERE clause. */
123187 memset(s: &sNameContext, c: 0, n: sizeof(NameContext));
123188 sNameContext.pSrcList = pSrc;
123189 sNameContext.pParse = pParse;
123190 sqlite3ResolveExprNames(pNC: &sNameContext, pExpr: pWhere);
123191
123192 /* Create VDBE to loop through the entries in pSrc that match the WHERE
123193 ** clause. For each row found, increment either the deferred or immediate
123194 ** foreign key constraint counter. */
123195 if( pParse->nErr==0 ){
123196 pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0);
123197 sqlite3VdbeAddOp2(p: v, OP_FkCounter, p1: pFKey->isDeferred, p2: nIncr);
123198 if( pWInfo ){
123199 sqlite3WhereEnd(pWInfo);
123200 }
123201 }
123202
123203 /* Clean up the WHERE clause constructed above. */
123204 sqlite3ExprDelete(db, p: pWhere);
123205 if( iFkIfZero ){
123206 sqlite3VdbeJumpHereOrPopInst(p: v, addr: iFkIfZero);
123207 }
123208}
123209
123210/*
123211** This function returns a linked list of FKey objects (connected by
123212** FKey.pNextTo) holding all children of table pTab. For example,
123213** given the following schema:
123214**
123215** CREATE TABLE t1(a PRIMARY KEY);
123216** CREATE TABLE t2(b REFERENCES t1(a);
123217**
123218** Calling this function with table "t1" as an argument returns a pointer
123219** to the FKey structure representing the foreign key constraint on table
123220** "t2". Calling this function with "t2" as the argument would return a
123221** NULL pointer (as there are no FK constraints for which t2 is the parent
123222** table).
123223*/
123224SQLITE_PRIVATE FKey *sqlite3FkReferences(Table *pTab){
123225 return (FKey *)sqlite3HashFind(pH: &pTab->pSchema->fkeyHash, pKey: pTab->zName);
123226}
123227
123228/*
123229** The second argument is a Trigger structure allocated by the
123230** fkActionTrigger() routine. This function deletes the Trigger structure
123231** and all of its sub-components.
123232**
123233** The Trigger structure or any of its sub-components may be allocated from
123234** the lookaside buffer belonging to database handle dbMem.
123235*/
123236static void fkTriggerDelete(sqlite3 *dbMem, Trigger *p){
123237 if( p ){
123238 TriggerStep *pStep = p->step_list;
123239 sqlite3ExprDelete(db: dbMem, p: pStep->pWhere);
123240 sqlite3ExprListDelete(db: dbMem, pList: pStep->pExprList);
123241 sqlite3SelectDelete(dbMem, pStep->pSelect);
123242 sqlite3ExprDelete(db: dbMem, p: p->pWhen);
123243 sqlite3DbFree(db: dbMem, p);
123244 }
123245}
123246
123247/*
123248** Clear the apTrigger[] cache of CASCADE triggers for all foreign keys
123249** in a particular database. This needs to happen when the schema
123250** changes.
123251*/
123252SQLITE_PRIVATE void sqlite3FkClearTriggerCache(sqlite3 *db, int iDb){
123253 HashElem *k;
123254 Hash *pHash = &db->aDb[iDb].pSchema->tblHash;
123255 for(k=sqliteHashFirst(pHash); k; k=sqliteHashNext(k)){
123256 Table *pTab = sqliteHashData(k);
123257 FKey *pFKey;
123258 if( !IsOrdinaryTable(pTab) ) continue;
123259 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
123260 fkTriggerDelete(dbMem: db, p: pFKey->apTrigger[0]); pFKey->apTrigger[0] = 0;
123261 fkTriggerDelete(dbMem: db, p: pFKey->apTrigger[1]); pFKey->apTrigger[1] = 0;
123262 }
123263 }
123264}
123265
123266/*
123267** This function is called to generate code that runs when table pTab is
123268** being dropped from the database. The SrcList passed as the second argument
123269** to this function contains a single entry guaranteed to resolve to
123270** table pTab.
123271**
123272** Normally, no code is required. However, if either
123273**
123274** (a) The table is the parent table of a FK constraint, or
123275** (b) The table is the child table of a deferred FK constraint and it is
123276** determined at runtime that there are outstanding deferred FK
123277** constraint violations in the database,
123278**
123279** then the equivalent of "DELETE FROM <tbl>" is executed before dropping
123280** the table from the database. Triggers are disabled while running this
123281** DELETE, but foreign key actions are not.
123282*/
123283SQLITE_PRIVATE void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTab){
123284 sqlite3 *db = pParse->db;
123285 if( (db->flags&SQLITE_ForeignKeys) && IsOrdinaryTable(pTab) ){
123286 int iSkip = 0;
123287 Vdbe *v = sqlite3GetVdbe(pParse);
123288
123289 assert( v ); /* VDBE has already been allocated */
123290 assert( IsOrdinaryTable(pTab) );
123291 if( sqlite3FkReferences(pTab)==0 ){
123292 /* Search for a deferred foreign key constraint for which this table
123293 ** is the child table. If one cannot be found, return without
123294 ** generating any VDBE code. If one can be found, then jump over
123295 ** the entire DELETE if there are no outstanding deferred constraints
123296 ** when this statement is run. */
123297 FKey *p;
123298 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
123299 if( p->isDeferred || (db->flags & SQLITE_DeferFKs) ) break;
123300 }
123301 if( !p ) return;
123302 iSkip = sqlite3VdbeMakeLabel(pParse);
123303 sqlite3VdbeAddOp2(p: v, OP_FkIfZero, p1: 1, p2: iSkip); VdbeCoverage(v);
123304 }
123305
123306 pParse->disableTriggers = 1;
123307 sqlite3DeleteFrom(pParse, pTabList: sqlite3SrcListDup(db, p: pName, flags: 0), pWhere: 0, pOrderBy: 0, pLimit: 0);
123308 pParse->disableTriggers = 0;
123309
123310 /* If the DELETE has generated immediate foreign key constraint
123311 ** violations, halt the VDBE and return an error at this point, before
123312 ** any modifications to the schema are made. This is because statement
123313 ** transactions are not able to rollback schema changes.
123314 **
123315 ** If the SQLITE_DeferFKs flag is set, then this is not required, as
123316 ** the statement transaction will not be rolled back even if FK
123317 ** constraints are violated.
123318 */
123319 if( (db->flags & SQLITE_DeferFKs)==0 ){
123320 sqlite3VdbeVerifyAbortable(v, OE_Abort);
123321 sqlite3VdbeAddOp2(p: v, OP_FkIfZero, p1: 0, p2: sqlite3VdbeCurrentAddr(p: v)+2);
123322 VdbeCoverage(v);
123323 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
123324 OE_Abort, p4: 0, P4_STATIC, P5_ConstraintFK);
123325 }
123326
123327 if( iSkip ){
123328 sqlite3VdbeResolveLabel(v, x: iSkip);
123329 }
123330 }
123331}
123332
123333
123334/*
123335** The second argument points to an FKey object representing a foreign key
123336** for which pTab is the child table. An UPDATE statement against pTab
123337** is currently being processed. For each column of the table that is
123338** actually updated, the corresponding element in the aChange[] array
123339** is zero or greater (if a column is unmodified the corresponding element
123340** is set to -1). If the rowid column is modified by the UPDATE statement
123341** the bChngRowid argument is non-zero.
123342**
123343** This function returns true if any of the columns that are part of the
123344** child key for FK constraint *p are modified.
123345*/
123346static int fkChildIsModified(
123347 Table *pTab, /* Table being updated */
123348 FKey *p, /* Foreign key for which pTab is the child */
123349 int *aChange, /* Array indicating modified columns */
123350 int bChngRowid /* True if rowid is modified by this update */
123351){
123352 int i;
123353 for(i=0; i<p->nCol; i++){
123354 int iChildKey = p->aCol[i].iFrom;
123355 if( aChange[iChildKey]>=0 ) return 1;
123356 if( iChildKey==pTab->iPKey && bChngRowid ) return 1;
123357 }
123358 return 0;
123359}
123360
123361/*
123362** The second argument points to an FKey object representing a foreign key
123363** for which pTab is the parent table. An UPDATE statement against pTab
123364** is currently being processed. For each column of the table that is
123365** actually updated, the corresponding element in the aChange[] array
123366** is zero or greater (if a column is unmodified the corresponding element
123367** is set to -1). If the rowid column is modified by the UPDATE statement
123368** the bChngRowid argument is non-zero.
123369**
123370** This function returns true if any of the columns that are part of the
123371** parent key for FK constraint *p are modified.
123372*/
123373static int fkParentIsModified(
123374 Table *pTab,
123375 FKey *p,
123376 int *aChange,
123377 int bChngRowid
123378){
123379 int i;
123380 for(i=0; i<p->nCol; i++){
123381 char *zKey = p->aCol[i].zCol;
123382 int iKey;
123383 for(iKey=0; iKey<pTab->nCol; iKey++){
123384 if( aChange[iKey]>=0 || (iKey==pTab->iPKey && bChngRowid) ){
123385 Column *pCol = &pTab->aCol[iKey];
123386 if( zKey ){
123387 if( 0==sqlite3StrICmp(zLeft: pCol->zCnName, zRight: zKey) ) return 1;
123388 }else if( pCol->colFlags & COLFLAG_PRIMKEY ){
123389 return 1;
123390 }
123391 }
123392 }
123393 }
123394 return 0;
123395}
123396
123397/*
123398** Return true if the parser passed as the first argument is being
123399** used to code a trigger that is really a "SET NULL" action belonging
123400** to trigger pFKey.
123401*/
123402static int isSetNullAction(Parse *pParse, FKey *pFKey){
123403 Parse *pTop = sqlite3ParseToplevel(pParse);
123404 if( pTop->pTriggerPrg ){
123405 Trigger *p = pTop->pTriggerPrg->pTrigger;
123406 if( (p==pFKey->apTrigger[0] && pFKey->aAction[0]==OE_SetNull)
123407 || (p==pFKey->apTrigger[1] && pFKey->aAction[1]==OE_SetNull)
123408 ){
123409 return 1;
123410 }
123411 }
123412 return 0;
123413}
123414
123415/*
123416** This function is called when inserting, deleting or updating a row of
123417** table pTab to generate VDBE code to perform foreign key constraint
123418** processing for the operation.
123419**
123420** For a DELETE operation, parameter regOld is passed the index of the
123421** first register in an array of (pTab->nCol+1) registers containing the
123422** rowid of the row being deleted, followed by each of the column values
123423** of the row being deleted, from left to right. Parameter regNew is passed
123424** zero in this case.
123425**
123426** For an INSERT operation, regOld is passed zero and regNew is passed the
123427** first register of an array of (pTab->nCol+1) registers containing the new
123428** row data.
123429**
123430** For an UPDATE operation, this function is called twice. Once before
123431** the original record is deleted from the table using the calling convention
123432** described for DELETE. Then again after the original record is deleted
123433** but before the new record is inserted using the INSERT convention.
123434*/
123435SQLITE_PRIVATE void sqlite3FkCheck(
123436 Parse *pParse, /* Parse context */
123437 Table *pTab, /* Row is being deleted from this table */
123438 int regOld, /* Previous row data is stored here */
123439 int regNew, /* New row data is stored here */
123440 int *aChange, /* Array indicating UPDATEd columns (or 0) */
123441 int bChngRowid /* True if rowid is UPDATEd */
123442){
123443 sqlite3 *db = pParse->db; /* Database handle */
123444 FKey *pFKey; /* Used to iterate through FKs */
123445 int iDb; /* Index of database containing pTab */
123446 const char *zDb; /* Name of database containing pTab */
123447 int isIgnoreErrors = pParse->disableTriggers;
123448
123449 /* Exactly one of regOld and regNew should be non-zero. */
123450 assert( (regOld==0)!=(regNew==0) );
123451
123452 /* If foreign-keys are disabled, this function is a no-op. */
123453 if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
123454 if( !IsOrdinaryTable(pTab) ) return;
123455
123456 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
123457 zDb = db->aDb[iDb].zDbSName;
123458
123459 /* Loop through all the foreign key constraints for which pTab is the
123460 ** child table (the table that the foreign key definition is part of). */
123461 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
123462 Table *pTo; /* Parent table of foreign key pFKey */
123463 Index *pIdx = 0; /* Index on key columns in pTo */
123464 int *aiFree = 0;
123465 int *aiCol;
123466 int iCol;
123467 int i;
123468 int bIgnore = 0;
123469
123470 if( aChange
123471 && sqlite3_stricmp(zLeft: pTab->zName, zRight: pFKey->zTo)!=0
123472 && fkChildIsModified(pTab, p: pFKey, aChange, bChngRowid)==0
123473 ){
123474 continue;
123475 }
123476
123477 /* Find the parent table of this foreign key. Also find a unique index
123478 ** on the parent key columns in the parent table. If either of these
123479 ** schema items cannot be located, set an error in pParse and return
123480 ** early. */
123481 if( pParse->disableTriggers ){
123482 pTo = sqlite3FindTable(db, zName: pFKey->zTo, zDatabase: zDb);
123483 }else{
123484 pTo = sqlite3LocateTable(pParse, flags: 0, zName: pFKey->zTo, zDbase: zDb);
123485 }
123486 if( !pTo || sqlite3FkLocateIndex(pParse, pParent: pTo, pFKey, ppIdx: &pIdx, paiCol: &aiFree) ){
123487 assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) );
123488 if( !isIgnoreErrors || db->mallocFailed ) return;
123489 if( pTo==0 ){
123490 /* If isIgnoreErrors is true, then a table is being dropped. In this
123491 ** case SQLite runs a "DELETE FROM xxx" on the table being dropped
123492 ** before actually dropping it in order to check FK constraints.
123493 ** If the parent table of an FK constraint on the current table is
123494 ** missing, behave as if it is empty. i.e. decrement the relevant
123495 ** FK counter for each row of the current table with non-NULL keys.
123496 */
123497 Vdbe *v = sqlite3GetVdbe(pParse);
123498 int iJump = sqlite3VdbeCurrentAddr(p: v) + pFKey->nCol + 1;
123499 for(i=0; i<pFKey->nCol; i++){
123500 int iFromCol, iReg;
123501 iFromCol = pFKey->aCol[i].iFrom;
123502 iReg = sqlite3TableColumnToStorage(pTab: pFKey->pFrom,iCol: iFromCol) + regOld+1;
123503 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: iReg, p2: iJump); VdbeCoverage(v);
123504 }
123505 sqlite3VdbeAddOp2(p: v, OP_FkCounter, p1: pFKey->isDeferred, p2: -1);
123506 }
123507 continue;
123508 }
123509 assert( pFKey->nCol==1 || (aiFree && pIdx) );
123510
123511 if( aiFree ){
123512 aiCol = aiFree;
123513 }else{
123514 iCol = pFKey->aCol[0].iFrom;
123515 aiCol = &iCol;
123516 }
123517 for(i=0; i<pFKey->nCol; i++){
123518 if( aiCol[i]==pTab->iPKey ){
123519 aiCol[i] = -1;
123520 }
123521 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
123522#ifndef SQLITE_OMIT_AUTHORIZATION
123523 /* Request permission to read the parent key columns. If the
123524 ** authorization callback returns SQLITE_IGNORE, behave as if any
123525 ** values read from the parent table are NULL. */
123526 if( db->xAuth ){
123527 int rcauth;
123528 char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zCnName;
123529 rcauth = sqlite3AuthReadCol(pParse, zTab: pTo->zName, zCol, iDb);
123530 bIgnore = (rcauth==SQLITE_IGNORE);
123531 }
123532#endif
123533 }
123534
123535 /* Take a shared-cache advisory read-lock on the parent table. Allocate
123536 ** a cursor to use to search the unique index on the parent key columns
123537 ** in the parent table. */
123538 sqlite3TableLock(pParse, iDb, iTab: pTo->tnum, isWriteLock: 0, zName: pTo->zName);
123539 pParse->nTab++;
123540
123541 if( regOld!=0 ){
123542 /* A row is being removed from the child table. Search for the parent.
123543 ** If the parent does not exist, removing the child row resolves an
123544 ** outstanding foreign key constraint violation. */
123545 fkLookupParent(pParse, iDb, pTab: pTo, pIdx, pFKey, aiCol, regData: regOld, nIncr: -1, isIgnore: bIgnore);
123546 }
123547 if( regNew!=0 && !isSetNullAction(pParse, pFKey) ){
123548 /* A row is being added to the child table. If a parent row cannot
123549 ** be found, adding the child row has violated the FK constraint.
123550 **
123551 ** If this operation is being performed as part of a trigger program
123552 ** that is actually a "SET NULL" action belonging to this very
123553 ** foreign key, then omit this scan altogether. As all child key
123554 ** values are guaranteed to be NULL, it is not possible for adding
123555 ** this row to cause an FK violation. */
123556 fkLookupParent(pParse, iDb, pTab: pTo, pIdx, pFKey, aiCol, regData: regNew, nIncr: +1, isIgnore: bIgnore);
123557 }
123558
123559 sqlite3DbFree(db, p: aiFree);
123560 }
123561
123562 /* Loop through all the foreign key constraints that refer to this table.
123563 ** (the "child" constraints) */
123564 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
123565 Index *pIdx = 0; /* Foreign key index for pFKey */
123566 SrcList *pSrc;
123567 int *aiCol = 0;
123568
123569 if( aChange && fkParentIsModified(pTab, p: pFKey, aChange, bChngRowid)==0 ){
123570 continue;
123571 }
123572
123573 if( !pFKey->isDeferred && !(db->flags & SQLITE_DeferFKs)
123574 && !pParse->pToplevel && !pParse->isMultiWrite
123575 ){
123576 assert( regOld==0 && regNew!=0 );
123577 /* Inserting a single row into a parent table cannot cause (or fix)
123578 ** an immediate foreign key violation. So do nothing in this case. */
123579 continue;
123580 }
123581
123582 if( sqlite3FkLocateIndex(pParse, pParent: pTab, pFKey, ppIdx: &pIdx, paiCol: &aiCol) ){
123583 if( !isIgnoreErrors || db->mallocFailed ) return;
123584 continue;
123585 }
123586 assert( aiCol || pFKey->nCol==1 );
123587
123588 /* Create a SrcList structure containing the child table. We need the
123589 ** child table as a SrcList for sqlite3WhereBegin() */
123590 pSrc = sqlite3SrcListAppend(pParse, pList: 0, pTable: 0, pDatabase: 0);
123591 if( pSrc ){
123592 SrcItem *pItem = pSrc->a;
123593 pItem->pTab = pFKey->pFrom;
123594 pItem->zName = pFKey->pFrom->zName;
123595 pItem->pTab->nTabRef++;
123596 pItem->iCursor = pParse->nTab++;
123597
123598 if( regNew!=0 ){
123599 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regData: regNew, nIncr: -1);
123600 }
123601 if( regOld!=0 ){
123602 int eAction = pFKey->aAction[aChange!=0];
123603 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regData: regOld, nIncr: 1);
123604 /* If this is a deferred FK constraint, or a CASCADE or SET NULL
123605 ** action applies, then any foreign key violations caused by
123606 ** removing the parent key will be rectified by the action trigger.
123607 ** So do not set the "may-abort" flag in this case.
123608 **
123609 ** Note 1: If the FK is declared "ON UPDATE CASCADE", then the
123610 ** may-abort flag will eventually be set on this statement anyway
123611 ** (when this function is called as part of processing the UPDATE
123612 ** within the action trigger).
123613 **
123614 ** Note 2: At first glance it may seem like SQLite could simply omit
123615 ** all OP_FkCounter related scans when either CASCADE or SET NULL
123616 ** applies. The trouble starts if the CASCADE or SET NULL action
123617 ** trigger causes other triggers or action rules attached to the
123618 ** child table to fire. In these cases the fk constraint counters
123619 ** might be set incorrectly if any OP_FkCounter related scans are
123620 ** omitted. */
123621 if( !pFKey->isDeferred && eAction!=OE_Cascade && eAction!=OE_SetNull ){
123622 sqlite3MayAbort(pParse);
123623 }
123624 }
123625 pItem->zName = 0;
123626 sqlite3SrcListDelete(db, pList: pSrc);
123627 }
123628 sqlite3DbFree(db, p: aiCol);
123629 }
123630}
123631
123632#define COLUMN_MASK(x) (((x)>31) ? 0xffffffff : ((u32)1<<(x)))
123633
123634/*
123635** This function is called before generating code to update or delete a
123636** row contained in table pTab.
123637*/
123638SQLITE_PRIVATE u32 sqlite3FkOldmask(
123639 Parse *pParse, /* Parse context */
123640 Table *pTab /* Table being modified */
123641){
123642 u32 mask = 0;
123643 if( pParse->db->flags&SQLITE_ForeignKeys && IsOrdinaryTable(pTab) ){
123644 FKey *p;
123645 int i;
123646 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
123647 for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
123648 }
123649 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
123650 Index *pIdx = 0;
123651 sqlite3FkLocateIndex(pParse, pParent: pTab, pFKey: p, ppIdx: &pIdx, paiCol: 0);
123652 if( pIdx ){
123653 for(i=0; i<pIdx->nKeyCol; i++){
123654 assert( pIdx->aiColumn[i]>=0 );
123655 mask |= COLUMN_MASK(pIdx->aiColumn[i]);
123656 }
123657 }
123658 }
123659 }
123660 return mask;
123661}
123662
123663
123664/*
123665** This function is called before generating code to update or delete a
123666** row contained in table pTab. If the operation is a DELETE, then
123667** parameter aChange is passed a NULL value. For an UPDATE, aChange points
123668** to an array of size N, where N is the number of columns in table pTab.
123669** If the i'th column is not modified by the UPDATE, then the corresponding
123670** entry in the aChange[] array is set to -1. If the column is modified,
123671** the value is 0 or greater. Parameter chngRowid is set to true if the
123672** UPDATE statement modifies the rowid fields of the table.
123673**
123674** If any foreign key processing will be required, this function returns
123675** non-zero. If there is no foreign key related processing, this function
123676** returns zero.
123677**
123678** For an UPDATE, this function returns 2 if:
123679**
123680** * There are any FKs for which pTab is the child and the parent table
123681** and any FK processing at all is required (even of a different FK), or
123682**
123683** * the UPDATE modifies one or more parent keys for which the action is
123684** not "NO ACTION" (i.e. is CASCADE, SET DEFAULT or SET NULL).
123685**
123686** Or, assuming some other foreign key processing is required, 1.
123687*/
123688SQLITE_PRIVATE int sqlite3FkRequired(
123689 Parse *pParse, /* Parse context */
123690 Table *pTab, /* Table being modified */
123691 int *aChange, /* Non-NULL for UPDATE operations */
123692 int chngRowid /* True for UPDATE that affects rowid */
123693){
123694 int eRet = 1; /* Value to return if bHaveFK is true */
123695 int bHaveFK = 0; /* If FK processing is required */
123696 if( pParse->db->flags&SQLITE_ForeignKeys && IsOrdinaryTable(pTab) ){
123697 if( !aChange ){
123698 /* A DELETE operation. Foreign key processing is required if the
123699 ** table in question is either the child or parent table for any
123700 ** foreign key constraint. */
123701 bHaveFK = (sqlite3FkReferences(pTab) || pTab->u.tab.pFKey);
123702 }else{
123703 /* This is an UPDATE. Foreign key processing is only required if the
123704 ** operation modifies one or more child or parent key columns. */
123705 FKey *p;
123706
123707 /* Check if any child key columns are being modified. */
123708 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
123709 if( fkChildIsModified(pTab, p, aChange, bChngRowid: chngRowid) ){
123710 if( 0==sqlite3_stricmp(zLeft: pTab->zName, zRight: p->zTo) ) eRet = 2;
123711 bHaveFK = 1;
123712 }
123713 }
123714
123715 /* Check if any parent key columns are being modified. */
123716 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
123717 if( fkParentIsModified(pTab, p, aChange, bChngRowid: chngRowid) ){
123718 if( p->aAction[1]!=OE_None ) return 2;
123719 bHaveFK = 1;
123720 }
123721 }
123722 }
123723 }
123724 return bHaveFK ? eRet : 0;
123725}
123726
123727/*
123728** This function is called when an UPDATE or DELETE operation is being
123729** compiled on table pTab, which is the parent table of foreign-key pFKey.
123730** If the current operation is an UPDATE, then the pChanges parameter is
123731** passed a pointer to the list of columns being modified. If it is a
123732** DELETE, pChanges is passed a NULL pointer.
123733**
123734** It returns a pointer to a Trigger structure containing a trigger
123735** equivalent to the ON UPDATE or ON DELETE action specified by pFKey.
123736** If the action is "NO ACTION" or "RESTRICT", then a NULL pointer is
123737** returned (these actions require no special handling by the triggers
123738** sub-system, code for them is created by fkScanChildren()).
123739**
123740** For example, if pFKey is the foreign key and pTab is table "p" in
123741** the following schema:
123742**
123743** CREATE TABLE p(pk PRIMARY KEY);
123744** CREATE TABLE c(ck REFERENCES p ON DELETE CASCADE);
123745**
123746** then the returned trigger structure is equivalent to:
123747**
123748** CREATE TRIGGER ... DELETE ON p BEGIN
123749** DELETE FROM c WHERE ck = old.pk;
123750** END;
123751**
123752** The returned pointer is cached as part of the foreign key object. It
123753** is eventually freed along with the rest of the foreign key object by
123754** sqlite3FkDelete().
123755*/
123756static Trigger *fkActionTrigger(
123757 Parse *pParse, /* Parse context */
123758 Table *pTab, /* Table being updated or deleted from */
123759 FKey *pFKey, /* Foreign key to get action for */
123760 ExprList *pChanges /* Change-list for UPDATE, NULL for DELETE */
123761){
123762 sqlite3 *db = pParse->db; /* Database handle */
123763 int action; /* One of OE_None, OE_Cascade etc. */
123764 Trigger *pTrigger; /* Trigger definition to return */
123765 int iAction = (pChanges!=0); /* 1 for UPDATE, 0 for DELETE */
123766
123767 action = pFKey->aAction[iAction];
123768 if( action==OE_Restrict && (db->flags & SQLITE_DeferFKs) ){
123769 return 0;
123770 }
123771 pTrigger = pFKey->apTrigger[iAction];
123772
123773 if( action!=OE_None && !pTrigger ){
123774 char const *zFrom; /* Name of child table */
123775 int nFrom; /* Length in bytes of zFrom */
123776 Index *pIdx = 0; /* Parent key index for this FK */
123777 int *aiCol = 0; /* child table cols -> parent key cols */
123778 TriggerStep *pStep = 0; /* First (only) step of trigger program */
123779 Expr *pWhere = 0; /* WHERE clause of trigger step */
123780 ExprList *pList = 0; /* Changes list if ON UPDATE CASCADE */
123781 Select *pSelect = 0; /* If RESTRICT, "SELECT RAISE(...)" */
123782 int i; /* Iterator variable */
123783 Expr *pWhen = 0; /* WHEN clause for the trigger */
123784
123785 if( sqlite3FkLocateIndex(pParse, pParent: pTab, pFKey, ppIdx: &pIdx, paiCol: &aiCol) ) return 0;
123786 assert( aiCol || pFKey->nCol==1 );
123787
123788 for(i=0; i<pFKey->nCol; i++){
123789 Token tOld = { "old", 3 }; /* Literal "old" token */
123790 Token tNew = { "new", 3 }; /* Literal "new" token */
123791 Token tFromCol; /* Name of column in child table */
123792 Token tToCol; /* Name of column in parent table */
123793 int iFromCol; /* Idx of column in child table */
123794 Expr *pEq; /* tFromCol = OLD.tToCol */
123795
123796 iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
123797 assert( iFromCol>=0 );
123798 assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) );
123799 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
123800 sqlite3TokenInit(p: &tToCol,
123801 z: pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zCnName);
123802 sqlite3TokenInit(p: &tFromCol, z: pFKey->pFrom->aCol[iFromCol].zCnName);
123803
123804 /* Create the expression "OLD.zToCol = zFromCol". It is important
123805 ** that the "OLD.zToCol" term is on the LHS of the = operator, so
123806 ** that the affinity and collation sequence associated with the
123807 ** parent table are used for the comparison. */
123808 pEq = sqlite3PExpr(pParse, TK_EQ,
123809 pLeft: sqlite3PExpr(pParse, TK_DOT,
123810 pLeft: sqlite3ExprAlloc(db, TK_ID, pToken: &tOld, dequote: 0),
123811 pRight: sqlite3ExprAlloc(db, TK_ID, pToken: &tToCol, dequote: 0)),
123812 pRight: sqlite3ExprAlloc(db, TK_ID, pToken: &tFromCol, dequote: 0)
123813 );
123814 pWhere = sqlite3ExprAnd(pParse, pLeft: pWhere, pRight: pEq);
123815
123816 /* For ON UPDATE, construct the next term of the WHEN clause.
123817 ** The final WHEN clause will be like this:
123818 **
123819 ** WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN)
123820 */
123821 if( pChanges ){
123822 pEq = sqlite3PExpr(pParse, TK_IS,
123823 pLeft: sqlite3PExpr(pParse, TK_DOT,
123824 pLeft: sqlite3ExprAlloc(db, TK_ID, pToken: &tOld, dequote: 0),
123825 pRight: sqlite3ExprAlloc(db, TK_ID, pToken: &tToCol, dequote: 0)),
123826 pRight: sqlite3PExpr(pParse, TK_DOT,
123827 pLeft: sqlite3ExprAlloc(db, TK_ID, pToken: &tNew, dequote: 0),
123828 pRight: sqlite3ExprAlloc(db, TK_ID, pToken: &tToCol, dequote: 0))
123829 );
123830 pWhen = sqlite3ExprAnd(pParse, pLeft: pWhen, pRight: pEq);
123831 }
123832
123833 if( action!=OE_Restrict && (action!=OE_Cascade || pChanges) ){
123834 Expr *pNew;
123835 if( action==OE_Cascade ){
123836 pNew = sqlite3PExpr(pParse, TK_DOT,
123837 pLeft: sqlite3ExprAlloc(db, TK_ID, pToken: &tNew, dequote: 0),
123838 pRight: sqlite3ExprAlloc(db, TK_ID, pToken: &tToCol, dequote: 0));
123839 }else if( action==OE_SetDflt ){
123840 Column *pCol = pFKey->pFrom->aCol + iFromCol;
123841 Expr *pDflt;
123842 if( pCol->colFlags & COLFLAG_GENERATED ){
123843 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
123844 testcase( pCol->colFlags & COLFLAG_STORED );
123845 pDflt = 0;
123846 }else{
123847 pDflt = sqlite3ColumnExpr(pTab: pFKey->pFrom, pCol);
123848 }
123849 if( pDflt ){
123850 pNew = sqlite3ExprDup(db, p: pDflt, flags: 0);
123851 }else{
123852 pNew = sqlite3ExprAlloc(db, TK_NULL, pToken: 0, dequote: 0);
123853 }
123854 }else{
123855 pNew = sqlite3ExprAlloc(db, TK_NULL, pToken: 0, dequote: 0);
123856 }
123857 pList = sqlite3ExprListAppend(pParse, pList, pExpr: pNew);
123858 sqlite3ExprListSetName(pParse, pList, pName: &tFromCol, dequote: 0);
123859 }
123860 }
123861 sqlite3DbFree(db, p: aiCol);
123862
123863 zFrom = pFKey->pFrom->zName;
123864 nFrom = sqlite3Strlen30(z: zFrom);
123865
123866 if( action==OE_Restrict ){
123867 Token tFrom;
123868 Expr *pRaise;
123869
123870 tFrom.z = zFrom;
123871 tFrom.n = nFrom;
123872 pRaise = sqlite3Expr(db, TK_RAISE, zToken: "FOREIGN KEY constraint failed");
123873 if( pRaise ){
123874 pRaise->affExpr = OE_Abort;
123875 }
123876 pSelect = sqlite3SelectNew(pParse,
123877 sqlite3ExprListAppend(pParse, pList: 0, pExpr: pRaise),
123878 sqlite3SrcListAppend(pParse, pList: 0, pTable: &tFrom, pDatabase: 0),
123879 pWhere,
123880 0, 0, 0, 0, 0
123881 );
123882 pWhere = 0;
123883 }
123884
123885 /* Disable lookaside memory allocation */
123886 DisableLookaside;
123887
123888 pTrigger = (Trigger *)sqlite3DbMallocZero(db,
123889 n: sizeof(Trigger) + /* struct Trigger */
123890 sizeof(TriggerStep) + /* Single step in trigger program */
123891 nFrom + 1 /* Space for pStep->zTarget */
123892 );
123893 if( pTrigger ){
123894 pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
123895 pStep->zTarget = (char *)&pStep[1];
123896 memcpy(dest: (char *)pStep->zTarget, src: zFrom, n: nFrom);
123897
123898 pStep->pWhere = sqlite3ExprDup(db, p: pWhere, EXPRDUP_REDUCE);
123899 pStep->pExprList = sqlite3ExprListDup(db, p: pList, EXPRDUP_REDUCE);
123900 pStep->pSelect = sqlite3SelectDup(db, pDup: pSelect, EXPRDUP_REDUCE);
123901 if( pWhen ){
123902 pWhen = sqlite3PExpr(pParse, TK_NOT, pLeft: pWhen, pRight: 0);
123903 pTrigger->pWhen = sqlite3ExprDup(db, p: pWhen, EXPRDUP_REDUCE);
123904 }
123905 }
123906
123907 /* Re-enable the lookaside buffer, if it was disabled earlier. */
123908 EnableLookaside;
123909
123910 sqlite3ExprDelete(db, p: pWhere);
123911 sqlite3ExprDelete(db, p: pWhen);
123912 sqlite3ExprListDelete(db, pList);
123913 sqlite3SelectDelete(db, pSelect);
123914 if( db->mallocFailed==1 ){
123915 fkTriggerDelete(dbMem: db, p: pTrigger);
123916 return 0;
123917 }
123918 assert( pStep!=0 );
123919 assert( pTrigger!=0 );
123920
123921 switch( action ){
123922 case OE_Restrict:
123923 pStep->op = TK_SELECT;
123924 break;
123925 case OE_Cascade:
123926 if( !pChanges ){
123927 pStep->op = TK_DELETE;
123928 break;
123929 }
123930 /* no break */ deliberate_fall_through
123931 default:
123932 pStep->op = TK_UPDATE;
123933 }
123934 pStep->pTrig = pTrigger;
123935 pTrigger->pSchema = pTab->pSchema;
123936 pTrigger->pTabSchema = pTab->pSchema;
123937 pFKey->apTrigger[iAction] = pTrigger;
123938 pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
123939 }
123940
123941 return pTrigger;
123942}
123943
123944/*
123945** This function is called when deleting or updating a row to implement
123946** any required CASCADE, SET NULL or SET DEFAULT actions.
123947*/
123948SQLITE_PRIVATE void sqlite3FkActions(
123949 Parse *pParse, /* Parse context */
123950 Table *pTab, /* Table being updated or deleted from */
123951 ExprList *pChanges, /* Change-list for UPDATE, NULL for DELETE */
123952 int regOld, /* Address of array containing old row */
123953 int *aChange, /* Array indicating UPDATEd columns (or 0) */
123954 int bChngRowid /* True if rowid is UPDATEd */
123955){
123956 /* If foreign-key support is enabled, iterate through all FKs that
123957 ** refer to table pTab. If there is an action associated with the FK
123958 ** for this operation (either update or delete), invoke the associated
123959 ** trigger sub-program. */
123960 if( pParse->db->flags&SQLITE_ForeignKeys ){
123961 FKey *pFKey; /* Iterator variable */
123962 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
123963 if( aChange==0 || fkParentIsModified(pTab, p: pFKey, aChange, bChngRowid) ){
123964 Trigger *pAct = fkActionTrigger(pParse, pTab, pFKey, pChanges);
123965 if( pAct ){
123966 sqlite3CodeRowTriggerDirect(pParse, pAct, pTab, regOld, OE_Abort, 0);
123967 }
123968 }
123969 }
123970 }
123971}
123972
123973#endif /* ifndef SQLITE_OMIT_TRIGGER */
123974
123975/*
123976** Free all memory associated with foreign key definitions attached to
123977** table pTab. Remove the deleted foreign keys from the Schema.fkeyHash
123978** hash table.
123979*/
123980SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *db, Table *pTab){
123981 FKey *pFKey; /* Iterator variable */
123982 FKey *pNext; /* Copy of pFKey->pNextFrom */
123983
123984 assert( IsOrdinaryTable(pTab) );
123985 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pNext){
123986 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
123987
123988 /* Remove the FK from the fkeyHash hash table. */
123989 if( !db || db->pnBytesFreed==0 ){
123990 if( pFKey->pPrevTo ){
123991 pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
123992 }else{
123993 void *p = (void *)pFKey->pNextTo;
123994 const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo);
123995 sqlite3HashInsert(pH: &pTab->pSchema->fkeyHash, pKey: z, data: p);
123996 }
123997 if( pFKey->pNextTo ){
123998 pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
123999 }
124000 }
124001
124002 /* EV: R-30323-21917 Each foreign key constraint in SQLite is
124003 ** classified as either immediate or deferred.
124004 */
124005 assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
124006
124007 /* Delete any triggers created to implement actions for this FK. */
124008#ifndef SQLITE_OMIT_TRIGGER
124009 fkTriggerDelete(dbMem: db, p: pFKey->apTrigger[0]);
124010 fkTriggerDelete(dbMem: db, p: pFKey->apTrigger[1]);
124011#endif
124012
124013 pNext = pFKey->pNextFrom;
124014 sqlite3DbFree(db, p: pFKey);
124015 }
124016}
124017#endif /* ifndef SQLITE_OMIT_FOREIGN_KEY */
124018
124019/************** End of fkey.c ************************************************/
124020/************** Begin file insert.c ******************************************/
124021/*
124022** 2001 September 15
124023**
124024** The author disclaims copyright to this source code. In place of
124025** a legal notice, here is a blessing:
124026**
124027** May you do good and not evil.
124028** May you find forgiveness for yourself and forgive others.
124029** May you share freely, never taking more than you give.
124030**
124031*************************************************************************
124032** This file contains C code routines that are called by the parser
124033** to handle INSERT statements in SQLite.
124034*/
124035/* #include "sqliteInt.h" */
124036
124037/*
124038** Generate code that will
124039**
124040** (1) acquire a lock for table pTab then
124041** (2) open pTab as cursor iCur.
124042**
124043** If pTab is a WITHOUT ROWID table, then it is the PRIMARY KEY index
124044** for that table that is actually opened.
124045*/
124046SQLITE_PRIVATE void sqlite3OpenTable(
124047 Parse *pParse, /* Generate code into this VDBE */
124048 int iCur, /* The cursor number of the table */
124049 int iDb, /* The database index in sqlite3.aDb[] */
124050 Table *pTab, /* The table to be opened */
124051 int opcode /* OP_OpenRead or OP_OpenWrite */
124052){
124053 Vdbe *v;
124054 assert( !IsVirtual(pTab) );
124055 assert( pParse->pVdbe!=0 );
124056 v = pParse->pVdbe;
124057 assert( opcode==OP_OpenWrite || opcode==OP_OpenRead );
124058 sqlite3TableLock(pParse, iDb, iTab: pTab->tnum,
124059 isWriteLock: (opcode==OP_OpenWrite)?1:0, zName: pTab->zName);
124060 if( HasRowid(pTab) ){
124061 sqlite3VdbeAddOp4Int(p: v, op: opcode, p1: iCur, p2: pTab->tnum, p3: iDb, p4: pTab->nNVCol);
124062 VdbeComment((v, "%s", pTab->zName));
124063 }else{
124064 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
124065 assert( pPk!=0 );
124066 assert( pPk->tnum==pTab->tnum || CORRUPT_DB );
124067 sqlite3VdbeAddOp3(p: v, op: opcode, p1: iCur, p2: pPk->tnum, p3: iDb);
124068 sqlite3VdbeSetP4KeyInfo(pParse, pIdx: pPk);
124069 VdbeComment((v, "%s", pTab->zName));
124070 }
124071}
124072
124073/*
124074** Return a pointer to the column affinity string associated with index
124075** pIdx. A column affinity string has one character for each column in
124076** the table, according to the affinity of the column:
124077**
124078** Character Column affinity
124079** ------------------------------
124080** 'A' BLOB
124081** 'B' TEXT
124082** 'C' NUMERIC
124083** 'D' INTEGER
124084** 'F' REAL
124085**
124086** An extra 'D' is appended to the end of the string to cover the
124087** rowid that appears as the last column in every index.
124088**
124089** Memory for the buffer containing the column index affinity string
124090** is managed along with the rest of the Index structure. It will be
124091** released when sqlite3DeleteIndex() is called.
124092*/
124093SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(sqlite3 *db, Index *pIdx){
124094 if( !pIdx->zColAff ){
124095 /* The first time a column affinity string for a particular index is
124096 ** required, it is allocated and populated here. It is then stored as
124097 ** a member of the Index structure for subsequent use.
124098 **
124099 ** The column affinity string will eventually be deleted by
124100 ** sqliteDeleteIndex() when the Index structure itself is cleaned
124101 ** up.
124102 */
124103 int n;
124104 Table *pTab = pIdx->pTable;
124105 pIdx->zColAff = (char *)sqlite3DbMallocRaw(db: 0, n: pIdx->nColumn+1);
124106 if( !pIdx->zColAff ){
124107 sqlite3OomFault(db);
124108 return 0;
124109 }
124110 for(n=0; n<pIdx->nColumn; n++){
124111 i16 x = pIdx->aiColumn[n];
124112 char aff;
124113 if( x>=0 ){
124114 aff = pTab->aCol[x].affinity;
124115 }else if( x==XN_ROWID ){
124116 aff = SQLITE_AFF_INTEGER;
124117 }else{
124118 assert( x==XN_EXPR );
124119 assert( pIdx->aColExpr!=0 );
124120 aff = sqlite3ExprAffinity(pExpr: pIdx->aColExpr->a[n].pExpr);
124121 }
124122 if( aff<SQLITE_AFF_BLOB ) aff = SQLITE_AFF_BLOB;
124123 if( aff>SQLITE_AFF_NUMERIC) aff = SQLITE_AFF_NUMERIC;
124124 pIdx->zColAff[n] = aff;
124125 }
124126 pIdx->zColAff[n] = 0;
124127 }
124128
124129 return pIdx->zColAff;
124130}
124131
124132/*
124133** Make changes to the evolving bytecode to do affinity transformations
124134** of values that are about to be gathered into a row for table pTab.
124135**
124136** For ordinary (legacy, non-strict) tables:
124137** -----------------------------------------
124138**
124139** Compute the affinity string for table pTab, if it has not already been
124140** computed. As an optimization, omit trailing SQLITE_AFF_BLOB affinities.
124141**
124142** If the affinity string is empty (because it was all SQLITE_AFF_BLOB entries
124143** which were then optimized out) then this routine becomes a no-op.
124144**
124145** Otherwise if iReg>0 then code an OP_Affinity opcode that will set the
124146** affinities for register iReg and following. Or if iReg==0,
124147** then just set the P4 operand of the previous opcode (which should be
124148** an OP_MakeRecord) to the affinity string.
124149**
124150** A column affinity string has one character per column:
124151**
124152** Character Column affinity
124153** --------- ---------------
124154** 'A' BLOB
124155** 'B' TEXT
124156** 'C' NUMERIC
124157** 'D' INTEGER
124158** 'E' REAL
124159**
124160** For STRICT tables:
124161** ------------------
124162**
124163** Generate an appropropriate OP_TypeCheck opcode that will verify the
124164** datatypes against the column definitions in pTab. If iReg==0, that
124165** means an OP_MakeRecord opcode has already been generated and should be
124166** the last opcode generated. The new OP_TypeCheck needs to be inserted
124167** before the OP_MakeRecord. The new OP_TypeCheck should use the same
124168** register set as the OP_MakeRecord. If iReg>0 then register iReg is
124169** the first of a series of registers that will form the new record.
124170** Apply the type checking to that array of registers.
124171*/
124172SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
124173 int i, j;
124174 char *zColAff;
124175 if( pTab->tabFlags & TF_Strict ){
124176 if( iReg==0 ){
124177 /* Move the previous opcode (which should be OP_MakeRecord) forward
124178 ** by one slot and insert a new OP_TypeCheck where the current
124179 ** OP_MakeRecord is found */
124180 VdbeOp *pPrev;
124181 sqlite3VdbeAppendP4(p: v, pP4: pTab, P4_TABLE);
124182 pPrev = sqlite3VdbeGetOp(p: v, addr: -1);
124183 assert( pPrev!=0 );
124184 assert( pPrev->opcode==OP_MakeRecord || sqlite3VdbeDb(v)->mallocFailed );
124185 pPrev->opcode = OP_TypeCheck;
124186 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: pPrev->p1, p2: pPrev->p2, p3: pPrev->p3);
124187 }else{
124188 /* Insert an isolated OP_Typecheck */
124189 sqlite3VdbeAddOp2(p: v, OP_TypeCheck, p1: iReg, p2: pTab->nNVCol);
124190 sqlite3VdbeAppendP4(p: v, pP4: pTab, P4_TABLE);
124191 }
124192 return;
124193 }
124194 zColAff = pTab->zColAff;
124195 if( zColAff==0 ){
124196 sqlite3 *db = sqlite3VdbeDb(v);
124197 zColAff = (char *)sqlite3DbMallocRaw(db: 0, n: pTab->nCol+1);
124198 if( !zColAff ){
124199 sqlite3OomFault(db);
124200 return;
124201 }
124202
124203 for(i=j=0; i<pTab->nCol; i++){
124204 assert( pTab->aCol[i].affinity!=0 );
124205 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
124206 zColAff[j++] = pTab->aCol[i].affinity;
124207 }
124208 }
124209 do{
124210 zColAff[j--] = 0;
124211 }while( j>=0 && zColAff[j]<=SQLITE_AFF_BLOB );
124212 pTab->zColAff = zColAff;
124213 }
124214 assert( zColAff!=0 );
124215 i = sqlite3Strlen30NN(zColAff);
124216 if( i ){
124217 if( iReg ){
124218 sqlite3VdbeAddOp4(p: v, OP_Affinity, p1: iReg, p2: i, p3: 0, zP4: zColAff, p4type: i);
124219 }else{
124220 assert( sqlite3VdbeGetOp(v, -1)->opcode==OP_MakeRecord
124221 || sqlite3VdbeDb(v)->mallocFailed );
124222 sqlite3VdbeChangeP4(p: v, addr: -1, zP4: zColAff, n: i);
124223 }
124224 }
124225}
124226
124227/*
124228** Return non-zero if the table pTab in database iDb or any of its indices
124229** have been opened at any point in the VDBE program. This is used to see if
124230** a statement of the form "INSERT INTO <iDb, pTab> SELECT ..." can
124231** run without using a temporary table for the results of the SELECT.
124232*/
124233static int readsTable(Parse *p, int iDb, Table *pTab){
124234 Vdbe *v = sqlite3GetVdbe(p);
124235 int i;
124236 int iEnd = sqlite3VdbeCurrentAddr(p: v);
124237#ifndef SQLITE_OMIT_VIRTUALTABLE
124238 VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
124239#endif
124240
124241 for(i=1; i<iEnd; i++){
124242 VdbeOp *pOp = sqlite3VdbeGetOp(p: v, addr: i);
124243 assert( pOp!=0 );
124244 if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
124245 Index *pIndex;
124246 Pgno tnum = pOp->p2;
124247 if( tnum==pTab->tnum ){
124248 return 1;
124249 }
124250 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
124251 if( tnum==pIndex->tnum ){
124252 return 1;
124253 }
124254 }
124255 }
124256#ifndef SQLITE_OMIT_VIRTUALTABLE
124257 if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
124258 assert( pOp->p4.pVtab!=0 );
124259 assert( pOp->p4type==P4_VTAB );
124260 return 1;
124261 }
124262#endif
124263 }
124264 return 0;
124265}
124266
124267/* This walker callback will compute the union of colFlags flags for all
124268** referenced columns in a CHECK constraint or generated column expression.
124269*/
124270static int exprColumnFlagUnion(Walker *pWalker, Expr *pExpr){
124271 if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 ){
124272 assert( pExpr->iColumn < pWalker->u.pTab->nCol );
124273 pWalker->eCode |= pWalker->u.pTab->aCol[pExpr->iColumn].colFlags;
124274 }
124275 return WRC_Continue;
124276}
124277
124278#ifndef SQLITE_OMIT_GENERATED_COLUMNS
124279/*
124280** All regular columns for table pTab have been puts into registers
124281** starting with iRegStore. The registers that correspond to STORED
124282** or VIRTUAL columns have not yet been initialized. This routine goes
124283** back and computes the values for those columns based on the previously
124284** computed normal columns.
124285*/
124286SQLITE_PRIVATE void sqlite3ComputeGeneratedColumns(
124287 Parse *pParse, /* Parsing context */
124288 int iRegStore, /* Register holding the first column */
124289 Table *pTab /* The table */
124290){
124291 int i;
124292 Walker w;
124293 Column *pRedo;
124294 int eProgress;
124295 VdbeOp *pOp;
124296
124297 assert( pTab->tabFlags & TF_HasGenerated );
124298 testcase( pTab->tabFlags & TF_HasVirtual );
124299 testcase( pTab->tabFlags & TF_HasStored );
124300
124301 /* Before computing generated columns, first go through and make sure
124302 ** that appropriate affinity has been applied to the regular columns
124303 */
124304 sqlite3TableAffinity(v: pParse->pVdbe, pTab, iReg: iRegStore);
124305 if( (pTab->tabFlags & TF_HasStored)!=0 ){
124306 pOp = sqlite3VdbeGetOp(p: pParse->pVdbe,addr: -1);
124307 if( pOp->opcode==OP_Affinity ){
124308 /* Change the OP_Affinity argument to '@' (NONE) for all stored
124309 ** columns. '@' is the no-op affinity and those columns have not
124310 ** yet been computed. */
124311 int ii, jj;
124312 char *zP4 = pOp->p4.z;
124313 assert( zP4!=0 );
124314 assert( pOp->p4type==P4_DYNAMIC );
124315 for(ii=jj=0; zP4[jj]; ii++){
124316 if( pTab->aCol[ii].colFlags & COLFLAG_VIRTUAL ){
124317 continue;
124318 }
124319 if( pTab->aCol[ii].colFlags & COLFLAG_STORED ){
124320 zP4[jj] = SQLITE_AFF_NONE;
124321 }
124322 jj++;
124323 }
124324 }else if( pOp->opcode==OP_TypeCheck ){
124325 /* If an OP_TypeCheck was generated because the table is STRICT,
124326 ** then set the P3 operand to indicate that generated columns should
124327 ** not be checked */
124328 pOp->p3 = 1;
124329 }
124330 }
124331
124332 /* Because there can be multiple generated columns that refer to one another,
124333 ** this is a two-pass algorithm. On the first pass, mark all generated
124334 ** columns as "not available".
124335 */
124336 for(i=0; i<pTab->nCol; i++){
124337 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
124338 testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
124339 testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
124340 pTab->aCol[i].colFlags |= COLFLAG_NOTAVAIL;
124341 }
124342 }
124343
124344 w.u.pTab = pTab;
124345 w.xExprCallback = exprColumnFlagUnion;
124346 w.xSelectCallback = 0;
124347 w.xSelectCallback2 = 0;
124348
124349 /* On the second pass, compute the value of each NOT-AVAILABLE column.
124350 ** Companion code in the TK_COLUMN case of sqlite3ExprCodeTarget() will
124351 ** compute dependencies and mark remove the COLSPAN_NOTAVAIL mark, as
124352 ** they are needed.
124353 */
124354 pParse->iSelfTab = -iRegStore;
124355 do{
124356 eProgress = 0;
124357 pRedo = 0;
124358 for(i=0; i<pTab->nCol; i++){
124359 Column *pCol = pTab->aCol + i;
124360 if( (pCol->colFlags & COLFLAG_NOTAVAIL)!=0 ){
124361 int x;
124362 pCol->colFlags |= COLFLAG_BUSY;
124363 w.eCode = 0;
124364 sqlite3WalkExpr(pWalker: &w, pExpr: sqlite3ColumnExpr(pTab, pCol));
124365 pCol->colFlags &= ~COLFLAG_BUSY;
124366 if( w.eCode & COLFLAG_NOTAVAIL ){
124367 pRedo = pCol;
124368 continue;
124369 }
124370 eProgress = 1;
124371 assert( pCol->colFlags & COLFLAG_GENERATED );
124372 x = sqlite3TableColumnToStorage(pTab, iCol: i) + iRegStore;
124373 sqlite3ExprCodeGeneratedColumn(pParse, pTab, pCol, regOut: x);
124374 pCol->colFlags &= ~COLFLAG_NOTAVAIL;
124375 }
124376 }
124377 }while( pRedo && eProgress );
124378 if( pRedo ){
124379 sqlite3ErrorMsg(pParse, zFormat: "generated column loop on \"%s\"", pRedo->zCnName);
124380 }
124381 pParse->iSelfTab = 0;
124382}
124383#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
124384
124385
124386#ifndef SQLITE_OMIT_AUTOINCREMENT
124387/*
124388** Locate or create an AutoincInfo structure associated with table pTab
124389** which is in database iDb. Return the register number for the register
124390** that holds the maximum rowid. Return zero if pTab is not an AUTOINCREMENT
124391** table. (Also return zero when doing a VACUUM since we do not want to
124392** update the AUTOINCREMENT counters during a VACUUM.)
124393**
124394** There is at most one AutoincInfo structure per table even if the
124395** same table is autoincremented multiple times due to inserts within
124396** triggers. A new AutoincInfo structure is created if this is the
124397** first use of table pTab. On 2nd and subsequent uses, the original
124398** AutoincInfo structure is used.
124399**
124400** Four consecutive registers are allocated:
124401**
124402** (1) The name of the pTab table.
124403** (2) The maximum ROWID of pTab.
124404** (3) The rowid in sqlite_sequence of pTab
124405** (4) The original value of the max ROWID in pTab, or NULL if none
124406**
124407** The 2nd register is the one that is returned. That is all the
124408** insert routine needs to know about.
124409*/
124410static int autoIncBegin(
124411 Parse *pParse, /* Parsing context */
124412 int iDb, /* Index of the database holding pTab */
124413 Table *pTab /* The table we are writing to */
124414){
124415 int memId = 0; /* Register holding maximum rowid */
124416 assert( pParse->db->aDb[iDb].pSchema!=0 );
124417 if( (pTab->tabFlags & TF_Autoincrement)!=0
124418 && (pParse->db->mDbFlags & DBFLAG_Vacuum)==0
124419 ){
124420 Parse *pToplevel = sqlite3ParseToplevel(pParse);
124421 AutoincInfo *pInfo;
124422 Table *pSeqTab = pParse->db->aDb[iDb].pSchema->pSeqTab;
124423
124424 /* Verify that the sqlite_sequence table exists and is an ordinary
124425 ** rowid table with exactly two columns.
124426 ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */
124427 if( pSeqTab==0
124428 || !HasRowid(pSeqTab)
124429 || NEVER(IsVirtual(pSeqTab))
124430 || pSeqTab->nCol!=2
124431 ){
124432 pParse->nErr++;
124433 pParse->rc = SQLITE_CORRUPT_SEQUENCE;
124434 return 0;
124435 }
124436
124437 pInfo = pToplevel->pAinc;
124438 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
124439 if( pInfo==0 ){
124440 pInfo = sqlite3DbMallocRawNN(db: pParse->db, n: sizeof(*pInfo));
124441 sqlite3ParserAddCleanup(pToplevel, sqlite3DbFree, pInfo);
124442 testcase( pParse->earlyCleanup );
124443 if( pParse->db->mallocFailed ) return 0;
124444 pInfo->pNext = pToplevel->pAinc;
124445 pToplevel->pAinc = pInfo;
124446 pInfo->pTab = pTab;
124447 pInfo->iDb = iDb;
124448 pToplevel->nMem++; /* Register to hold name of table */
124449 pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */
124450 pToplevel->nMem +=2; /* Rowid in sqlite_sequence + orig max val */
124451 }
124452 memId = pInfo->regCtr;
124453 }
124454 return memId;
124455}
124456
124457/*
124458** This routine generates code that will initialize all of the
124459** register used by the autoincrement tracker.
124460*/
124461SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse){
124462 AutoincInfo *p; /* Information about an AUTOINCREMENT */
124463 sqlite3 *db = pParse->db; /* The database connection */
124464 Db *pDb; /* Database only autoinc table */
124465 int memId; /* Register holding max rowid */
124466 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
124467
124468 /* This routine is never called during trigger-generation. It is
124469 ** only called from the top-level */
124470 assert( pParse->pTriggerTab==0 );
124471 assert( sqlite3IsToplevel(pParse) );
124472
124473 assert( v ); /* We failed long ago if this is not so */
124474 for(p = pParse->pAinc; p; p = p->pNext){
124475 static const int iLn = VDBE_OFFSET_LINENO(2);
124476 static const VdbeOpList autoInc[] = {
124477 /* 0 */ {OP_Null, 0, 0, 0},
124478 /* 1 */ {OP_Rewind, 0, 10, 0},
124479 /* 2 */ {OP_Column, 0, 0, 0},
124480 /* 3 */ {OP_Ne, 0, 9, 0},
124481 /* 4 */ {OP_Rowid, 0, 0, 0},
124482 /* 5 */ {OP_Column, 0, 1, 0},
124483 /* 6 */ {OP_AddImm, 0, 0, 0},
124484 /* 7 */ {OP_Copy, 0, 0, 0},
124485 /* 8 */ {OP_Goto, 0, 11, 0},
124486 /* 9 */ {OP_Next, 0, 2, 0},
124487 /* 10 */ {OP_Integer, 0, 0, 0},
124488 /* 11 */ {OP_Close, 0, 0, 0}
124489 };
124490 VdbeOp *aOp;
124491 pDb = &db->aDb[p->iDb];
124492 memId = p->regCtr;
124493 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
124494 sqlite3OpenTable(pParse, iCur: 0, iDb: p->iDb, pTab: pDb->pSchema->pSeqTab, OP_OpenRead);
124495 sqlite3VdbeLoadString(p: v, iDest: memId-1, zStr: p->pTab->zName);
124496 aOp = sqlite3VdbeAddOpList(p: v, ArraySize(autoInc), aOp: autoInc, iLineno: iLn);
124497 if( aOp==0 ) break;
124498 aOp[0].p2 = memId;
124499 aOp[0].p3 = memId+2;
124500 aOp[2].p3 = memId;
124501 aOp[3].p1 = memId-1;
124502 aOp[3].p3 = memId;
124503 aOp[3].p5 = SQLITE_JUMPIFNULL;
124504 aOp[4].p2 = memId+1;
124505 aOp[5].p3 = memId;
124506 aOp[6].p1 = memId;
124507 aOp[7].p2 = memId+2;
124508 aOp[7].p1 = memId;
124509 aOp[10].p2 = memId;
124510 if( pParse->nTab==0 ) pParse->nTab = 1;
124511 }
124512}
124513
124514/*
124515** Update the maximum rowid for an autoincrement calculation.
124516**
124517** This routine should be called when the regRowid register holds a
124518** new rowid that is about to be inserted. If that new rowid is
124519** larger than the maximum rowid in the memId memory cell, then the
124520** memory cell is updated.
124521*/
124522static void autoIncStep(Parse *pParse, int memId, int regRowid){
124523 if( memId>0 ){
124524 sqlite3VdbeAddOp2(p: pParse->pVdbe, OP_MemMax, p1: memId, p2: regRowid);
124525 }
124526}
124527
124528/*
124529** This routine generates the code needed to write autoincrement
124530** maximum rowid values back into the sqlite_sequence register.
124531** Every statement that might do an INSERT into an autoincrement
124532** table (either directly or through triggers) needs to call this
124533** routine just before the "exit" code.
124534*/
124535static SQLITE_NOINLINE void autoIncrementEnd(Parse *pParse){
124536 AutoincInfo *p;
124537 Vdbe *v = pParse->pVdbe;
124538 sqlite3 *db = pParse->db;
124539
124540 assert( v );
124541 for(p = pParse->pAinc; p; p = p->pNext){
124542 static const int iLn = VDBE_OFFSET_LINENO(2);
124543 static const VdbeOpList autoIncEnd[] = {
124544 /* 0 */ {OP_NotNull, 0, 2, 0},
124545 /* 1 */ {OP_NewRowid, 0, 0, 0},
124546 /* 2 */ {OP_MakeRecord, 0, 2, 0},
124547 /* 3 */ {OP_Insert, 0, 0, 0},
124548 /* 4 */ {OP_Close, 0, 0, 0}
124549 };
124550 VdbeOp *aOp;
124551 Db *pDb = &db->aDb[p->iDb];
124552 int iRec;
124553 int memId = p->regCtr;
124554
124555 iRec = sqlite3GetTempReg(pParse);
124556 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
124557 sqlite3VdbeAddOp3(p: v, OP_Le, p1: memId+2, p2: sqlite3VdbeCurrentAddr(p: v)+7, p3: memId);
124558 VdbeCoverage(v);
124559 sqlite3OpenTable(pParse, iCur: 0, iDb: p->iDb, pTab: pDb->pSchema->pSeqTab, OP_OpenWrite);
124560 aOp = sqlite3VdbeAddOpList(p: v, ArraySize(autoIncEnd), aOp: autoIncEnd, iLineno: iLn);
124561 if( aOp==0 ) break;
124562 aOp[0].p1 = memId+1;
124563 aOp[1].p2 = memId+1;
124564 aOp[2].p1 = memId-1;
124565 aOp[2].p3 = iRec;
124566 aOp[3].p2 = iRec;
124567 aOp[3].p3 = memId+1;
124568 aOp[3].p5 = OPFLAG_APPEND;
124569 sqlite3ReleaseTempReg(pParse, iReg: iRec);
124570 }
124571}
124572SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse){
124573 if( pParse->pAinc ) autoIncrementEnd(pParse);
124574}
124575#else
124576/*
124577** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
124578** above are all no-ops
124579*/
124580# define autoIncBegin(A,B,C) (0)
124581# define autoIncStep(A,B,C)
124582#endif /* SQLITE_OMIT_AUTOINCREMENT */
124583
124584
124585/* Forward declaration */
124586static int xferOptimization(
124587 Parse *pParse, /* Parser context */
124588 Table *pDest, /* The table we are inserting into */
124589 Select *pSelect, /* A SELECT statement to use as the data source */
124590 int onError, /* How to handle constraint errors */
124591 int iDbDest /* The database of pDest */
124592);
124593
124594/*
124595** This routine is called to handle SQL of the following forms:
124596**
124597** insert into TABLE (IDLIST) values(EXPRLIST),(EXPRLIST),...
124598** insert into TABLE (IDLIST) select
124599** insert into TABLE (IDLIST) default values
124600**
124601** The IDLIST following the table name is always optional. If omitted,
124602** then a list of all (non-hidden) columns for the table is substituted.
124603** The IDLIST appears in the pColumn parameter. pColumn is NULL if IDLIST
124604** is omitted.
124605**
124606** For the pSelect parameter holds the values to be inserted for the
124607** first two forms shown above. A VALUES clause is really just short-hand
124608** for a SELECT statement that omits the FROM clause and everything else
124609** that follows. If the pSelect parameter is NULL, that means that the
124610** DEFAULT VALUES form of the INSERT statement is intended.
124611**
124612** The code generated follows one of four templates. For a simple
124613** insert with data coming from a single-row VALUES clause, the code executes
124614** once straight down through. Pseudo-code follows (we call this
124615** the "1st template"):
124616**
124617** open write cursor to <table> and its indices
124618** put VALUES clause expressions into registers
124619** write the resulting record into <table>
124620** cleanup
124621**
124622** The three remaining templates assume the statement is of the form
124623**
124624** INSERT INTO <table> SELECT ...
124625**
124626** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
124627** in other words if the SELECT pulls all columns from a single table
124628** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
124629** if <table2> and <table1> are distinct tables but have identical
124630** schemas, including all the same indices, then a special optimization
124631** is invoked that copies raw records from <table2> over to <table1>.
124632** See the xferOptimization() function for the implementation of this
124633** template. This is the 2nd template.
124634**
124635** open a write cursor to <table>
124636** open read cursor on <table2>
124637** transfer all records in <table2> over to <table>
124638** close cursors
124639** foreach index on <table>
124640** open a write cursor on the <table> index
124641** open a read cursor on the corresponding <table2> index
124642** transfer all records from the read to the write cursors
124643** close cursors
124644** end foreach
124645**
124646** The 3rd template is for when the second template does not apply
124647** and the SELECT clause does not read from <table> at any time.
124648** The generated code follows this template:
124649**
124650** X <- A
124651** goto B
124652** A: setup for the SELECT
124653** loop over the rows in the SELECT
124654** load values into registers R..R+n
124655** yield X
124656** end loop
124657** cleanup after the SELECT
124658** end-coroutine X
124659** B: open write cursor to <table> and its indices
124660** C: yield X, at EOF goto D
124661** insert the select result into <table> from R..R+n
124662** goto C
124663** D: cleanup
124664**
124665** The 4th template is used if the insert statement takes its
124666** values from a SELECT but the data is being inserted into a table
124667** that is also read as part of the SELECT. In the third form,
124668** we have to use an intermediate table to store the results of
124669** the select. The template is like this:
124670**
124671** X <- A
124672** goto B
124673** A: setup for the SELECT
124674** loop over the tables in the SELECT
124675** load value into register R..R+n
124676** yield X
124677** end loop
124678** cleanup after the SELECT
124679** end co-routine R
124680** B: open temp table
124681** L: yield X, at EOF goto M
124682** insert row from R..R+n into temp table
124683** goto L
124684** M: open write cursor to <table> and its indices
124685** rewind temp table
124686** C: loop over rows of intermediate table
124687** transfer values form intermediate table into <table>
124688** end loop
124689** D: cleanup
124690*/
124691SQLITE_PRIVATE void sqlite3Insert(
124692 Parse *pParse, /* Parser context */
124693 SrcList *pTabList, /* Name of table into which we are inserting */
124694 Select *pSelect, /* A SELECT statement to use as the data source */
124695 IdList *pColumn, /* Column names corresponding to IDLIST, or NULL. */
124696 int onError, /* How to handle constraint errors */
124697 Upsert *pUpsert /* ON CONFLICT clauses for upsert, or NULL */
124698){
124699 sqlite3 *db; /* The main database structure */
124700 Table *pTab; /* The table to insert into. aka TABLE */
124701 int i, j; /* Loop counters */
124702 Vdbe *v; /* Generate code into this virtual machine */
124703 Index *pIdx; /* For looping over indices of the table */
124704 int nColumn; /* Number of columns in the data */
124705 int nHidden = 0; /* Number of hidden columns if TABLE is virtual */
124706 int iDataCur = 0; /* VDBE cursor that is the main data repository */
124707 int iIdxCur = 0; /* First index cursor */
124708 int ipkColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
124709 int endOfLoop; /* Label for the end of the insertion loop */
124710 int srcTab = 0; /* Data comes from this temporary cursor if >=0 */
124711 int addrInsTop = 0; /* Jump to label "D" */
124712 int addrCont = 0; /* Top of insert loop. Label "C" in templates 3 and 4 */
124713 SelectDest dest; /* Destination for SELECT on rhs of INSERT */
124714 int iDb; /* Index of database holding TABLE */
124715 u8 useTempTable = 0; /* Store SELECT results in intermediate table */
124716 u8 appendFlag = 0; /* True if the insert is likely to be an append */
124717 u8 withoutRowid; /* 0 for normal table. 1 for WITHOUT ROWID table */
124718 u8 bIdListInOrder; /* True if IDLIST is in table order */
124719 ExprList *pList = 0; /* List of VALUES() to be inserted */
124720 int iRegStore; /* Register in which to store next column */
124721
124722 /* Register allocations */
124723 int regFromSelect = 0;/* Base register for data coming from SELECT */
124724 int regAutoinc = 0; /* Register holding the AUTOINCREMENT counter */
124725 int regRowCount = 0; /* Memory cell used for the row counter */
124726 int regIns; /* Block of regs holding rowid+data being inserted */
124727 int regRowid; /* registers holding insert rowid */
124728 int regData; /* register holding first column to insert */
124729 int *aRegIdx = 0; /* One register allocated to each index */
124730
124731#ifndef SQLITE_OMIT_TRIGGER
124732 int isView; /* True if attempting to insert into a view */
124733 Trigger *pTrigger; /* List of triggers on pTab, if required */
124734 int tmask; /* Mask of trigger times */
124735#endif
124736
124737 db = pParse->db;
124738 if( pParse->nErr || db->mallocFailed ){
124739 goto insert_cleanup;
124740 }
124741 dest.iSDParm = 0; /* Suppress a harmless compiler warning */
124742
124743 /* If the Select object is really just a simple VALUES() list with a
124744 ** single row (the common case) then keep that one row of values
124745 ** and discard the other (unused) parts of the pSelect object
124746 */
124747 if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){
124748 pList = pSelect->pEList;
124749 pSelect->pEList = 0;
124750 sqlite3SelectDelete(db, pSelect);
124751 pSelect = 0;
124752 }
124753
124754 /* Locate the table into which we will be inserting new information.
124755 */
124756 assert( pTabList->nSrc==1 );
124757 pTab = sqlite3SrcListLookup(pParse, pSrc: pTabList);
124758 if( pTab==0 ){
124759 goto insert_cleanup;
124760 }
124761 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
124762 assert( iDb<db->nDb );
124763 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, zArg1: pTab->zName, zArg2: 0,
124764 zArg3: db->aDb[iDb].zDbSName) ){
124765 goto insert_cleanup;
124766 }
124767 withoutRowid = !HasRowid(pTab);
124768
124769 /* Figure out if we have any triggers and if the table being
124770 ** inserted into is a view
124771 */
124772#ifndef SQLITE_OMIT_TRIGGER
124773 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, pMask: &tmask);
124774 isView = IsView(pTab);
124775#else
124776# define pTrigger 0
124777# define tmask 0
124778# define isView 0
124779#endif
124780#ifdef SQLITE_OMIT_VIEW
124781# undef isView
124782# define isView 0
124783#endif
124784 assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) );
124785
124786 /* If pTab is really a view, make sure it has been initialized.
124787 ** ViewGetColumnNames() is a no-op if pTab is not a view.
124788 */
124789 if( sqlite3ViewGetColumnNames(pParse, pTable: pTab) ){
124790 goto insert_cleanup;
124791 }
124792
124793 /* Cannot insert into a read-only table.
124794 */
124795 if( sqlite3IsReadOnly(pParse, pTab, viewOk: tmask) ){
124796 goto insert_cleanup;
124797 }
124798
124799 /* Allocate a VDBE
124800 */
124801 v = sqlite3GetVdbe(pParse);
124802 if( v==0 ) goto insert_cleanup;
124803 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
124804 sqlite3BeginWriteOperation(pParse, setStatement: pSelect || pTrigger, iDb);
124805
124806#ifndef SQLITE_OMIT_XFER_OPT
124807 /* If the statement is of the form
124808 **
124809 ** INSERT INTO <table1> SELECT * FROM <table2>;
124810 **
124811 ** Then special optimizations can be applied that make the transfer
124812 ** very fast and which reduce fragmentation of indices.
124813 **
124814 ** This is the 2nd template.
124815 */
124816 if( pColumn==0 && xferOptimization(pParse, pDest: pTab, pSelect, onError, iDbDest: iDb) ){
124817 assert( !pTrigger );
124818 assert( pList==0 );
124819 goto insert_end;
124820 }
124821#endif /* SQLITE_OMIT_XFER_OPT */
124822
124823 /* If this is an AUTOINCREMENT table, look up the sequence number in the
124824 ** sqlite_sequence table and store it in memory cell regAutoinc.
124825 */
124826 regAutoinc = autoIncBegin(pParse, iDb, pTab);
124827
124828 /* Allocate a block registers to hold the rowid and the values
124829 ** for all columns of the new row.
124830 */
124831 regRowid = regIns = pParse->nMem+1;
124832 pParse->nMem += pTab->nCol + 1;
124833 if( IsVirtual(pTab) ){
124834 regRowid++;
124835 pParse->nMem++;
124836 }
124837 regData = regRowid+1;
124838
124839 /* If the INSERT statement included an IDLIST term, then make sure
124840 ** all elements of the IDLIST really are columns of the table and
124841 ** remember the column indices.
124842 **
124843 ** If the table has an INTEGER PRIMARY KEY column and that column
124844 ** is named in the IDLIST, then record in the ipkColumn variable
124845 ** the index into IDLIST of the primary key column. ipkColumn is
124846 ** the index of the primary key as it appears in IDLIST, not as
124847 ** is appears in the original table. (The index of the INTEGER
124848 ** PRIMARY KEY in the original table is pTab->iPKey.) After this
124849 ** loop, if ipkColumn==(-1), that means that integer primary key
124850 ** is unspecified, and hence the table is either WITHOUT ROWID or
124851 ** it will automatically generated an integer primary key.
124852 **
124853 ** bIdListInOrder is true if the columns in IDLIST are in storage
124854 ** order. This enables an optimization that avoids shuffling the
124855 ** columns into storage order. False negatives are harmless,
124856 ** but false positives will cause database corruption.
124857 */
124858 bIdListInOrder = (pTab->tabFlags & (TF_OOOHidden|TF_HasStored))==0;
124859 if( pColumn ){
124860 for(i=0; i<pColumn->nId; i++){
124861 pColumn->a[i].idx = -1;
124862 }
124863 for(i=0; i<pColumn->nId; i++){
124864 for(j=0; j<pTab->nCol; j++){
124865 if( sqlite3StrICmp(zLeft: pColumn->a[i].zName, zRight: pTab->aCol[j].zCnName)==0 ){
124866 pColumn->a[i].idx = j;
124867 if( i!=j ) bIdListInOrder = 0;
124868 if( j==pTab->iPKey ){
124869 ipkColumn = i; assert( !withoutRowid );
124870 }
124871#ifndef SQLITE_OMIT_GENERATED_COLUMNS
124872 if( pTab->aCol[j].colFlags & (COLFLAG_STORED|COLFLAG_VIRTUAL) ){
124873 sqlite3ErrorMsg(pParse,
124874 zFormat: "cannot INSERT into generated column \"%s\"",
124875 pTab->aCol[j].zCnName);
124876 goto insert_cleanup;
124877 }
124878#endif
124879 break;
124880 }
124881 }
124882 if( j>=pTab->nCol ){
124883 if( sqlite3IsRowid(z: pColumn->a[i].zName) && !withoutRowid ){
124884 ipkColumn = i;
124885 bIdListInOrder = 0;
124886 }else{
124887 sqlite3ErrorMsg(pParse, zFormat: "table %S has no column named %s",
124888 pTabList->a, pColumn->a[i].zName);
124889 pParse->checkSchema = 1;
124890 goto insert_cleanup;
124891 }
124892 }
124893 }
124894 }
124895
124896 /* Figure out how many columns of data are supplied. If the data
124897 ** is coming from a SELECT statement, then generate a co-routine that
124898 ** produces a single row of the SELECT on each invocation. The
124899 ** co-routine is the common header to the 3rd and 4th templates.
124900 */
124901 if( pSelect ){
124902 /* Data is coming from a SELECT or from a multi-row VALUES clause.
124903 ** Generate a co-routine to run the SELECT. */
124904 int regYield; /* Register holding co-routine entry-point */
124905 int addrTop; /* Top of the co-routine */
124906 int rc; /* Result code */
124907
124908 regYield = ++pParse->nMem;
124909 addrTop = sqlite3VdbeCurrentAddr(p: v) + 1;
124910 sqlite3VdbeAddOp3(p: v, OP_InitCoroutine, p1: regYield, p2: 0, p3: addrTop);
124911 sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
124912 dest.iSdst = bIdListInOrder ? regData : 0;
124913 dest.nSdst = pTab->nCol;
124914 rc = sqlite3Select(pParse, pSelect, &dest);
124915 regFromSelect = dest.iSdst;
124916 if( rc || db->mallocFailed || pParse->nErr ) goto insert_cleanup;
124917 sqlite3VdbeEndCoroutine(v, regYield);
124918 sqlite3VdbeJumpHere(p: v, addr: addrTop - 1); /* label B: */
124919 assert( pSelect->pEList );
124920 nColumn = pSelect->pEList->nExpr;
124921
124922 /* Set useTempTable to TRUE if the result of the SELECT statement
124923 ** should be written into a temporary table (template 4). Set to
124924 ** FALSE if each output row of the SELECT can be written directly into
124925 ** the destination table (template 3).
124926 **
124927 ** A temp table must be used if the table being updated is also one
124928 ** of the tables being read by the SELECT statement. Also use a
124929 ** temp table in the case of row triggers.
124930 */
124931 if( pTrigger || readsTable(p: pParse, iDb, pTab) ){
124932 useTempTable = 1;
124933 }
124934
124935 if( useTempTable ){
124936 /* Invoke the coroutine to extract information from the SELECT
124937 ** and add it to a transient table srcTab. The code generated
124938 ** here is from the 4th template:
124939 **
124940 ** B: open temp table
124941 ** L: yield X, goto M at EOF
124942 ** insert row from R..R+n into temp table
124943 ** goto L
124944 ** M: ...
124945 */
124946 int regRec; /* Register to hold packed record */
124947 int regTempRowid; /* Register to hold temp table ROWID */
124948 int addrL; /* Label "L" */
124949
124950 srcTab = pParse->nTab++;
124951 regRec = sqlite3GetTempReg(pParse);
124952 regTempRowid = sqlite3GetTempReg(pParse);
124953 sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: srcTab, p2: nColumn);
124954 addrL = sqlite3VdbeAddOp1(p: v, OP_Yield, p1: dest.iSDParm); VdbeCoverage(v);
124955 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: regFromSelect, p2: nColumn, p3: regRec);
124956 sqlite3VdbeAddOp2(p: v, OP_NewRowid, p1: srcTab, p2: regTempRowid);
124957 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: srcTab, p2: regRec, p3: regTempRowid);
124958 sqlite3VdbeGoto(p: v, iDest: addrL);
124959 sqlite3VdbeJumpHere(p: v, addr: addrL);
124960 sqlite3ReleaseTempReg(pParse, iReg: regRec);
124961 sqlite3ReleaseTempReg(pParse, iReg: regTempRowid);
124962 }
124963 }else{
124964 /* This is the case if the data for the INSERT is coming from a
124965 ** single-row VALUES clause
124966 */
124967 NameContext sNC;
124968 memset(s: &sNC, c: 0, n: sizeof(sNC));
124969 sNC.pParse = pParse;
124970 srcTab = -1;
124971 assert( useTempTable==0 );
124972 if( pList ){
124973 nColumn = pList->nExpr;
124974 if( sqlite3ResolveExprListNames(pNC: &sNC, pList) ){
124975 goto insert_cleanup;
124976 }
124977 }else{
124978 nColumn = 0;
124979 }
124980 }
124981
124982 /* If there is no IDLIST term but the table has an integer primary
124983 ** key, the set the ipkColumn variable to the integer primary key
124984 ** column index in the original table definition.
124985 */
124986 if( pColumn==0 && nColumn>0 ){
124987 ipkColumn = pTab->iPKey;
124988#ifndef SQLITE_OMIT_GENERATED_COLUMNS
124989 if( ipkColumn>=0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){
124990 testcase( pTab->tabFlags & TF_HasVirtual );
124991 testcase( pTab->tabFlags & TF_HasStored );
124992 for(i=ipkColumn-1; i>=0; i--){
124993 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
124994 testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
124995 testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
124996 ipkColumn--;
124997 }
124998 }
124999 }
125000#endif
125001
125002 /* Make sure the number of columns in the source data matches the number
125003 ** of columns to be inserted into the table.
125004 */
125005 assert( TF_HasHidden==COLFLAG_HIDDEN );
125006 assert( TF_HasGenerated==COLFLAG_GENERATED );
125007 assert( COLFLAG_NOINSERT==(COLFLAG_GENERATED|COLFLAG_HIDDEN) );
125008 if( (pTab->tabFlags & (TF_HasGenerated|TF_HasHidden))!=0 ){
125009 for(i=0; i<pTab->nCol; i++){
125010 if( pTab->aCol[i].colFlags & COLFLAG_NOINSERT ) nHidden++;
125011 }
125012 }
125013 if( nColumn!=(pTab->nCol-nHidden) ){
125014 sqlite3ErrorMsg(pParse,
125015 zFormat: "table %S has %d columns but %d values were supplied",
125016 pTabList->a, pTab->nCol-nHidden, nColumn);
125017 goto insert_cleanup;
125018 }
125019 }
125020 if( pColumn!=0 && nColumn!=pColumn->nId ){
125021 sqlite3ErrorMsg(pParse, zFormat: "%d values for %d columns", nColumn, pColumn->nId);
125022 goto insert_cleanup;
125023 }
125024
125025 /* Initialize the count of rows to be inserted
125026 */
125027 if( (db->flags & SQLITE_CountRows)!=0
125028 && !pParse->nested
125029 && !pParse->pTriggerTab
125030 && !pParse->bReturning
125031 ){
125032 regRowCount = ++pParse->nMem;
125033 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: regRowCount);
125034 }
125035
125036 /* If this is not a view, open the table and and all indices */
125037 if( !isView ){
125038 int nIdx;
125039 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0,
125040 &iDataCur, &iIdxCur);
125041 aRegIdx = sqlite3DbMallocRawNN(db, n: sizeof(int)*(nIdx+2));
125042 if( aRegIdx==0 ){
125043 goto insert_cleanup;
125044 }
125045 for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){
125046 assert( pIdx );
125047 aRegIdx[i] = ++pParse->nMem;
125048 pParse->nMem += pIdx->nColumn;
125049 }
125050 aRegIdx[i] = ++pParse->nMem; /* Register to store the table record */
125051 }
125052#ifndef SQLITE_OMIT_UPSERT
125053 if( pUpsert ){
125054 Upsert *pNx;
125055 if( IsVirtual(pTab) ){
125056 sqlite3ErrorMsg(pParse, zFormat: "UPSERT not implemented for virtual table \"%s\"",
125057 pTab->zName);
125058 goto insert_cleanup;
125059 }
125060 if( IsView(pTab) ){
125061 sqlite3ErrorMsg(pParse, zFormat: "cannot UPSERT a view");
125062 goto insert_cleanup;
125063 }
125064 if( sqlite3HasExplicitNulls(pParse, pList: pUpsert->pUpsertTarget) ){
125065 goto insert_cleanup;
125066 }
125067 pTabList->a[0].iCursor = iDataCur;
125068 pNx = pUpsert;
125069 do{
125070 pNx->pUpsertSrc = pTabList;
125071 pNx->regData = regData;
125072 pNx->iDataCur = iDataCur;
125073 pNx->iIdxCur = iIdxCur;
125074 if( pNx->pUpsertTarget ){
125075 if( sqlite3UpsertAnalyzeTarget(pParse, pTabList, pNx) ){
125076 goto insert_cleanup;
125077 }
125078 }
125079 pNx = pNx->pNextUpsert;
125080 }while( pNx!=0 );
125081 }
125082#endif
125083
125084
125085 /* This is the top of the main insertion loop */
125086 if( useTempTable ){
125087 /* This block codes the top of loop only. The complete loop is the
125088 ** following pseudocode (template 4):
125089 **
125090 ** rewind temp table, if empty goto D
125091 ** C: loop over rows of intermediate table
125092 ** transfer values form intermediate table into <table>
125093 ** end loop
125094 ** D: ...
125095 */
125096 addrInsTop = sqlite3VdbeAddOp1(p: v, OP_Rewind, p1: srcTab); VdbeCoverage(v);
125097 addrCont = sqlite3VdbeCurrentAddr(p: v);
125098 }else if( pSelect ){
125099 /* This block codes the top of loop only. The complete loop is the
125100 ** following pseudocode (template 3):
125101 **
125102 ** C: yield X, at EOF goto D
125103 ** insert the select result into <table> from R..R+n
125104 ** goto C
125105 ** D: ...
125106 */
125107 sqlite3VdbeReleaseRegisters(pParse, regData, pTab->nCol, 0, 0);
125108 addrInsTop = addrCont = sqlite3VdbeAddOp1(p: v, OP_Yield, p1: dest.iSDParm);
125109 VdbeCoverage(v);
125110 if( ipkColumn>=0 ){
125111 /* tag-20191021-001: If the INTEGER PRIMARY KEY is being generated by the
125112 ** SELECT, go ahead and copy the value into the rowid slot now, so that
125113 ** the value does not get overwritten by a NULL at tag-20191021-002. */
125114 sqlite3VdbeAddOp2(p: v, OP_Copy, p1: regFromSelect+ipkColumn, p2: regRowid);
125115 }
125116 }
125117
125118 /* Compute data for ordinary columns of the new entry. Values
125119 ** are written in storage order into registers starting with regData.
125120 ** Only ordinary columns are computed in this loop. The rowid
125121 ** (if there is one) is computed later and generated columns are
125122 ** computed after the rowid since they might depend on the value
125123 ** of the rowid.
125124 */
125125 nHidden = 0;
125126 iRegStore = regData; assert( regData==regRowid+1 );
125127 for(i=0; i<pTab->nCol; i++, iRegStore++){
125128 int k;
125129 u32 colFlags;
125130 assert( i>=nHidden );
125131 if( i==pTab->iPKey ){
125132 /* tag-20191021-002: References to the INTEGER PRIMARY KEY are filled
125133 ** using the rowid. So put a NULL in the IPK slot of the record to avoid
125134 ** using excess space. The file format definition requires this extra
125135 ** NULL - we cannot optimize further by skipping the column completely */
125136 sqlite3VdbeAddOp1(p: v, OP_SoftNull, p1: iRegStore);
125137 continue;
125138 }
125139 if( ((colFlags = pTab->aCol[i].colFlags) & COLFLAG_NOINSERT)!=0 ){
125140 nHidden++;
125141 if( (colFlags & COLFLAG_VIRTUAL)!=0 ){
125142 /* Virtual columns do not participate in OP_MakeRecord. So back up
125143 ** iRegStore by one slot to compensate for the iRegStore++ in the
125144 ** outer for() loop */
125145 iRegStore--;
125146 continue;
125147 }else if( (colFlags & COLFLAG_STORED)!=0 ){
125148 /* Stored columns are computed later. But if there are BEFORE
125149 ** triggers, the slots used for stored columns will be OP_Copy-ed
125150 ** to a second block of registers, so the register needs to be
125151 ** initialized to NULL to avoid an uninitialized register read */
125152 if( tmask & TRIGGER_BEFORE ){
125153 sqlite3VdbeAddOp1(p: v, OP_SoftNull, p1: iRegStore);
125154 }
125155 continue;
125156 }else if( pColumn==0 ){
125157 /* Hidden columns that are not explicitly named in the INSERT
125158 ** get there default value */
125159 sqlite3ExprCodeFactorable(pParse,
125160 pExpr: sqlite3ColumnExpr(pTab, pCol: &pTab->aCol[i]),
125161 target: iRegStore);
125162 continue;
125163 }
125164 }
125165 if( pColumn ){
125166 for(j=0; j<pColumn->nId && pColumn->a[j].idx!=i; j++){}
125167 if( j>=pColumn->nId ){
125168 /* A column not named in the insert column list gets its
125169 ** default value */
125170 sqlite3ExprCodeFactorable(pParse,
125171 pExpr: sqlite3ColumnExpr(pTab, pCol: &pTab->aCol[i]),
125172 target: iRegStore);
125173 continue;
125174 }
125175 k = j;
125176 }else if( nColumn==0 ){
125177 /* This is INSERT INTO ... DEFAULT VALUES. Load the default value. */
125178 sqlite3ExprCodeFactorable(pParse,
125179 pExpr: sqlite3ColumnExpr(pTab, pCol: &pTab->aCol[i]),
125180 target: iRegStore);
125181 continue;
125182 }else{
125183 k = i - nHidden;
125184 }
125185
125186 if( useTempTable ){
125187 sqlite3VdbeAddOp3(p: v, OP_Column, p1: srcTab, p2: k, p3: iRegStore);
125188 }else if( pSelect ){
125189 if( regFromSelect!=regData ){
125190 sqlite3VdbeAddOp2(p: v, OP_SCopy, p1: regFromSelect+k, p2: iRegStore);
125191 }
125192 }else{
125193 sqlite3ExprCode(pParse, pExpr: pList->a[k].pExpr, target: iRegStore);
125194 }
125195 }
125196
125197
125198 /* Run the BEFORE and INSTEAD OF triggers, if there are any
125199 */
125200 endOfLoop = sqlite3VdbeMakeLabel(pParse);
125201 if( tmask & TRIGGER_BEFORE ){
125202 int regCols = sqlite3GetTempRange(pParse, nReg: pTab->nCol+1);
125203
125204 /* build the NEW.* reference row. Note that if there is an INTEGER
125205 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
125206 ** translated into a unique ID for the row. But on a BEFORE trigger,
125207 ** we do not know what the unique ID will be (because the insert has
125208 ** not happened yet) so we substitute a rowid of -1
125209 */
125210 if( ipkColumn<0 ){
125211 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: -1, p2: regCols);
125212 }else{
125213 int addr1;
125214 assert( !withoutRowid );
125215 if( useTempTable ){
125216 sqlite3VdbeAddOp3(p: v, OP_Column, p1: srcTab, p2: ipkColumn, p3: regCols);
125217 }else{
125218 assert( pSelect==0 ); /* Otherwise useTempTable is true */
125219 sqlite3ExprCode(pParse, pExpr: pList->a[ipkColumn].pExpr, target: regCols);
125220 }
125221 addr1 = sqlite3VdbeAddOp1(p: v, OP_NotNull, p1: regCols); VdbeCoverage(v);
125222 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: -1, p2: regCols);
125223 sqlite3VdbeJumpHere(p: v, addr: addr1);
125224 sqlite3VdbeAddOp1(p: v, OP_MustBeInt, p1: regCols); VdbeCoverage(v);
125225 }
125226
125227 /* Copy the new data already generated. */
125228 assert( pTab->nNVCol>0 );
125229 sqlite3VdbeAddOp3(p: v, OP_Copy, p1: regRowid+1, p2: regCols+1, p3: pTab->nNVCol-1);
125230
125231#ifndef SQLITE_OMIT_GENERATED_COLUMNS
125232 /* Compute the new value for generated columns after all other
125233 ** columns have already been computed. This must be done after
125234 ** computing the ROWID in case one of the generated columns
125235 ** refers to the ROWID. */
125236 if( pTab->tabFlags & TF_HasGenerated ){
125237 testcase( pTab->tabFlags & TF_HasVirtual );
125238 testcase( pTab->tabFlags & TF_HasStored );
125239 sqlite3ComputeGeneratedColumns(pParse, iRegStore: regCols+1, pTab);
125240 }
125241#endif
125242
125243 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
125244 ** do not attempt any conversions before assembling the record.
125245 ** If this is a real table, attempt conversions as required by the
125246 ** table column affinities.
125247 */
125248 if( !isView ){
125249 sqlite3TableAffinity(v, pTab, iReg: regCols+1);
125250 }
125251
125252 /* Fire BEFORE or INSTEAD OF triggers */
125253 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE,
125254 pTab, regCols-pTab->nCol-1, onError, endOfLoop);
125255
125256 sqlite3ReleaseTempRange(pParse, iReg: regCols, nReg: pTab->nCol+1);
125257 }
125258
125259 if( !isView ){
125260 if( IsVirtual(pTab) ){
125261 /* The row that the VUpdate opcode will delete: none */
125262 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: regIns);
125263 }
125264 if( ipkColumn>=0 ){
125265 /* Compute the new rowid */
125266 if( useTempTable ){
125267 sqlite3VdbeAddOp3(p: v, OP_Column, p1: srcTab, p2: ipkColumn, p3: regRowid);
125268 }else if( pSelect ){
125269 /* Rowid already initialized at tag-20191021-001 */
125270 }else{
125271 Expr *pIpk = pList->a[ipkColumn].pExpr;
125272 if( pIpk->op==TK_NULL && !IsVirtual(pTab) ){
125273 sqlite3VdbeAddOp3(p: v, OP_NewRowid, p1: iDataCur, p2: regRowid, p3: regAutoinc);
125274 appendFlag = 1;
125275 }else{
125276 sqlite3ExprCode(pParse, pExpr: pList->a[ipkColumn].pExpr, target: regRowid);
125277 }
125278 }
125279 /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
125280 ** to generate a unique primary key value.
125281 */
125282 if( !appendFlag ){
125283 int addr1;
125284 if( !IsVirtual(pTab) ){
125285 addr1 = sqlite3VdbeAddOp1(p: v, OP_NotNull, p1: regRowid); VdbeCoverage(v);
125286 sqlite3VdbeAddOp3(p: v, OP_NewRowid, p1: iDataCur, p2: regRowid, p3: regAutoinc);
125287 sqlite3VdbeJumpHere(p: v, addr: addr1);
125288 }else{
125289 addr1 = sqlite3VdbeCurrentAddr(p: v);
125290 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: regRowid, p2: addr1+2); VdbeCoverage(v);
125291 }
125292 sqlite3VdbeAddOp1(p: v, OP_MustBeInt, p1: regRowid); VdbeCoverage(v);
125293 }
125294 }else if( IsVirtual(pTab) || withoutRowid ){
125295 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: regRowid);
125296 }else{
125297 sqlite3VdbeAddOp3(p: v, OP_NewRowid, p1: iDataCur, p2: regRowid, p3: regAutoinc);
125298 appendFlag = 1;
125299 }
125300 autoIncStep(pParse, memId: regAutoinc, regRowid);
125301
125302#ifndef SQLITE_OMIT_GENERATED_COLUMNS
125303 /* Compute the new value for generated columns after all other
125304 ** columns have already been computed. This must be done after
125305 ** computing the ROWID in case one of the generated columns
125306 ** is derived from the INTEGER PRIMARY KEY. */
125307 if( pTab->tabFlags & TF_HasGenerated ){
125308 sqlite3ComputeGeneratedColumns(pParse, iRegStore: regRowid+1, pTab);
125309 }
125310#endif
125311
125312 /* Generate code to check constraints and generate index keys and
125313 ** do the insertion.
125314 */
125315#ifndef SQLITE_OMIT_VIRTUALTABLE
125316 if( IsVirtual(pTab) ){
125317 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
125318 sqlite3VtabMakeWritable(pParse, pTab);
125319 sqlite3VdbeAddOp4(p: v, OP_VUpdate, p1: 1, p2: pTab->nCol+2, p3: regIns, zP4: pVTab, P4_VTAB);
125320 sqlite3VdbeChangeP5(p: v, p5: onError==OE_Default ? OE_Abort : onError);
125321 sqlite3MayAbort(pParse);
125322 }else
125323#endif
125324 {
125325 int isReplace = 0;/* Set to true if constraints may cause a replace */
125326 int bUseSeek; /* True to use OPFLAG_SEEKRESULT */
125327 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
125328 regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0, pUpsert
125329 );
125330 sqlite3FkCheck(pParse, pTab, regOld: 0, regNew: regIns, aChange: 0, bChngRowid: 0);
125331
125332 /* Set the OPFLAG_USESEEKRESULT flag if either (a) there are no REPLACE
125333 ** constraints or (b) there are no triggers and this table is not a
125334 ** parent table in a foreign key constraint. It is safe to set the
125335 ** flag in the second case as if any REPLACE constraint is hit, an
125336 ** OP_Delete or OP_IdxDelete instruction will be executed on each
125337 ** cursor that is disturbed. And these instructions both clear the
125338 ** VdbeCursor.seekResult variable, disabling the OPFLAG_USESEEKRESULT
125339 ** functionality. */
125340 bUseSeek = (isReplace==0 || !sqlite3VdbeHasSubProgram(pVdbe: v));
125341 sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
125342 regIns, aRegIdx, 0, appendFlag, bUseSeek
125343 );
125344 }
125345#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
125346 }else if( pParse->bReturning ){
125347 /* If there is a RETURNING clause, populate the rowid register with
125348 ** constant value -1, in case one or more of the returned expressions
125349 ** refer to the "rowid" of the view. */
125350 sqlite3VdbeAddOp2(v, OP_Integer, -1, regRowid);
125351#endif
125352 }
125353
125354 /* Update the count of rows that are inserted
125355 */
125356 if( regRowCount ){
125357 sqlite3VdbeAddOp2(p: v, OP_AddImm, p1: regRowCount, p2: 1);
125358 }
125359
125360 if( pTrigger ){
125361 /* Code AFTER triggers */
125362 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER,
125363 pTab, regData-2-pTab->nCol, onError, endOfLoop);
125364 }
125365
125366 /* The bottom of the main insertion loop, if the data source
125367 ** is a SELECT statement.
125368 */
125369 sqlite3VdbeResolveLabel(v, x: endOfLoop);
125370 if( useTempTable ){
125371 sqlite3VdbeAddOp2(p: v, OP_Next, p1: srcTab, p2: addrCont); VdbeCoverage(v);
125372 sqlite3VdbeJumpHere(p: v, addr: addrInsTop);
125373 sqlite3VdbeAddOp1(p: v, OP_Close, p1: srcTab);
125374 }else if( pSelect ){
125375 sqlite3VdbeGoto(p: v, iDest: addrCont);
125376#ifdef SQLITE_DEBUG
125377 /* If we are jumping back to an OP_Yield that is preceded by an
125378 ** OP_ReleaseReg, set the p5 flag on the OP_Goto so that the
125379 ** OP_ReleaseReg will be included in the loop. */
125380 if( sqlite3VdbeGetOp(v, addrCont-1)->opcode==OP_ReleaseReg ){
125381 assert( sqlite3VdbeGetOp(v, addrCont)->opcode==OP_Yield );
125382 sqlite3VdbeChangeP5(v, 1);
125383 }
125384#endif
125385 sqlite3VdbeJumpHere(p: v, addr: addrInsTop);
125386 }
125387
125388#ifndef SQLITE_OMIT_XFER_OPT
125389insert_end:
125390#endif /* SQLITE_OMIT_XFER_OPT */
125391 /* Update the sqlite_sequence table by storing the content of the
125392 ** maximum rowid counter values recorded while inserting into
125393 ** autoincrement tables.
125394 */
125395 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
125396 sqlite3AutoincrementEnd(pParse);
125397 }
125398
125399 /*
125400 ** Return the number of rows inserted. If this routine is
125401 ** generating code because of a call to sqlite3NestedParse(), do not
125402 ** invoke the callback function.
125403 */
125404 if( regRowCount ){
125405 sqlite3VdbeAddOp2(p: v, OP_ChngCntRow, p1: regRowCount, p2: 1);
125406 sqlite3VdbeSetNumCols(p: v, nResColumn: 1);
125407 sqlite3VdbeSetColName(p: v, idx: 0, COLNAME_NAME, zName: "rows inserted", SQLITE_STATIC);
125408 }
125409
125410insert_cleanup:
125411 sqlite3SrcListDelete(db, pList: pTabList);
125412 sqlite3ExprListDelete(db, pList);
125413 sqlite3UpsertDelete(db, pUpsert);
125414 sqlite3SelectDelete(db, pSelect);
125415 sqlite3IdListDelete(db, pList: pColumn);
125416 sqlite3DbFree(db, p: aRegIdx);
125417}
125418
125419/* Make sure "isView" and other macros defined above are undefined. Otherwise
125420** they may interfere with compilation of other functions in this file
125421** (or in another file, if this file becomes part of the amalgamation). */
125422#ifdef isView
125423 #undef isView
125424#endif
125425#ifdef pTrigger
125426 #undef pTrigger
125427#endif
125428#ifdef tmask
125429 #undef tmask
125430#endif
125431
125432/*
125433** Meanings of bits in of pWalker->eCode for
125434** sqlite3ExprReferencesUpdatedColumn()
125435*/
125436#define CKCNSTRNT_COLUMN 0x01 /* CHECK constraint uses a changing column */
125437#define CKCNSTRNT_ROWID 0x02 /* CHECK constraint references the ROWID */
125438
125439/* This is the Walker callback from sqlite3ExprReferencesUpdatedColumn().
125440* Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this
125441** expression node references any of the
125442** columns that are being modifed by an UPDATE statement.
125443*/
125444static int checkConstraintExprNode(Walker *pWalker, Expr *pExpr){
125445 if( pExpr->op==TK_COLUMN ){
125446 assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 );
125447 if( pExpr->iColumn>=0 ){
125448 if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
125449 pWalker->eCode |= CKCNSTRNT_COLUMN;
125450 }
125451 }else{
125452 pWalker->eCode |= CKCNSTRNT_ROWID;
125453 }
125454 }
125455 return WRC_Continue;
125456}
125457
125458/*
125459** pExpr is a CHECK constraint on a row that is being UPDATE-ed. The
125460** only columns that are modified by the UPDATE are those for which
125461** aiChng[i]>=0, and also the ROWID is modified if chngRowid is true.
125462**
125463** Return true if CHECK constraint pExpr uses any of the
125464** changing columns (or the rowid if it is changing). In other words,
125465** return true if this CHECK constraint must be validated for
125466** the new row in the UPDATE statement.
125467**
125468** 2018-09-15: pExpr might also be an expression for an index-on-expressions.
125469** The operation of this routine is the same - return true if an only if
125470** the expression uses one or more of columns identified by the second and
125471** third arguments.
125472*/
125473SQLITE_PRIVATE int sqlite3ExprReferencesUpdatedColumn(
125474 Expr *pExpr, /* The expression to be checked */
125475 int *aiChng, /* aiChng[x]>=0 if column x changed by the UPDATE */
125476 int chngRowid /* True if UPDATE changes the rowid */
125477){
125478 Walker w;
125479 memset(s: &w, c: 0, n: sizeof(w));
125480 w.eCode = 0;
125481 w.xExprCallback = checkConstraintExprNode;
125482 w.u.aiCol = aiChng;
125483 sqlite3WalkExpr(pWalker: &w, pExpr);
125484 if( !chngRowid ){
125485 testcase( (w.eCode & CKCNSTRNT_ROWID)!=0 );
125486 w.eCode &= ~CKCNSTRNT_ROWID;
125487 }
125488 testcase( w.eCode==0 );
125489 testcase( w.eCode==CKCNSTRNT_COLUMN );
125490 testcase( w.eCode==CKCNSTRNT_ROWID );
125491 testcase( w.eCode==(CKCNSTRNT_ROWID|CKCNSTRNT_COLUMN) );
125492 return w.eCode!=0;
125493}
125494
125495/*
125496** The sqlite3GenerateConstraintChecks() routine usually wants to visit
125497** the indexes of a table in the order provided in the Table->pIndex list.
125498** However, sometimes (rarely - when there is an upsert) it wants to visit
125499** the indexes in a different order. The following data structures accomplish
125500** this.
125501**
125502** The IndexIterator object is used to walk through all of the indexes
125503** of a table in either Index.pNext order, or in some other order established
125504** by an array of IndexListTerm objects.
125505*/
125506typedef struct IndexListTerm IndexListTerm;
125507typedef struct IndexIterator IndexIterator;
125508struct IndexIterator {
125509 int eType; /* 0 for Index.pNext list. 1 for an array of IndexListTerm */
125510 int i; /* Index of the current item from the list */
125511 union {
125512 struct { /* Use this object for eType==0: A Index.pNext list */
125513 Index *pIdx; /* The current Index */
125514 } lx;
125515 struct { /* Use this object for eType==1; Array of IndexListTerm */
125516 int nIdx; /* Size of the array */
125517 IndexListTerm *aIdx; /* Array of IndexListTerms */
125518 } ax;
125519 } u;
125520};
125521
125522/* When IndexIterator.eType==1, then each index is an array of instances
125523** of the following object
125524*/
125525struct IndexListTerm {
125526 Index *p; /* The index */
125527 int ix; /* Which entry in the original Table.pIndex list is this index*/
125528};
125529
125530/* Return the first index on the list */
125531static Index *indexIteratorFirst(IndexIterator *pIter, int *pIx){
125532 assert( pIter->i==0 );
125533 if( pIter->eType ){
125534 *pIx = pIter->u.ax.aIdx[0].ix;
125535 return pIter->u.ax.aIdx[0].p;
125536 }else{
125537 *pIx = 0;
125538 return pIter->u.lx.pIdx;
125539 }
125540}
125541
125542/* Return the next index from the list. Return NULL when out of indexes */
125543static Index *indexIteratorNext(IndexIterator *pIter, int *pIx){
125544 if( pIter->eType ){
125545 int i = ++pIter->i;
125546 if( i>=pIter->u.ax.nIdx ){
125547 *pIx = i;
125548 return 0;
125549 }
125550 *pIx = pIter->u.ax.aIdx[i].ix;
125551 return pIter->u.ax.aIdx[i].p;
125552 }else{
125553 ++(*pIx);
125554 pIter->u.lx.pIdx = pIter->u.lx.pIdx->pNext;
125555 return pIter->u.lx.pIdx;
125556 }
125557}
125558
125559/*
125560** Generate code to do constraint checks prior to an INSERT or an UPDATE
125561** on table pTab.
125562**
125563** The regNewData parameter is the first register in a range that contains
125564** the data to be inserted or the data after the update. There will be
125565** pTab->nCol+1 registers in this range. The first register (the one
125566** that regNewData points to) will contain the new rowid, or NULL in the
125567** case of a WITHOUT ROWID table. The second register in the range will
125568** contain the content of the first table column. The third register will
125569** contain the content of the second table column. And so forth.
125570**
125571** The regOldData parameter is similar to regNewData except that it contains
125572** the data prior to an UPDATE rather than afterwards. regOldData is zero
125573** for an INSERT. This routine can distinguish between UPDATE and INSERT by
125574** checking regOldData for zero.
125575**
125576** For an UPDATE, the pkChng boolean is true if the true primary key (the
125577** rowid for a normal table or the PRIMARY KEY for a WITHOUT ROWID table)
125578** might be modified by the UPDATE. If pkChng is false, then the key of
125579** the iDataCur content table is guaranteed to be unchanged by the UPDATE.
125580**
125581** For an INSERT, the pkChng boolean indicates whether or not the rowid
125582** was explicitly specified as part of the INSERT statement. If pkChng
125583** is zero, it means that the either rowid is computed automatically or
125584** that the table is a WITHOUT ROWID table and has no rowid. On an INSERT,
125585** pkChng will only be true if the INSERT statement provides an integer
125586** value for either the rowid column or its INTEGER PRIMARY KEY alias.
125587**
125588** The code generated by this routine will store new index entries into
125589** registers identified by aRegIdx[]. No index entry is created for
125590** indices where aRegIdx[i]==0. The order of indices in aRegIdx[] is
125591** the same as the order of indices on the linked list of indices
125592** at pTab->pIndex.
125593**
125594** (2019-05-07) The generated code also creates a new record for the
125595** main table, if pTab is a rowid table, and stores that record in the
125596** register identified by aRegIdx[nIdx] - in other words in the first
125597** entry of aRegIdx[] past the last index. It is important that the
125598** record be generated during constraint checks to avoid affinity changes
125599** to the register content that occur after constraint checks but before
125600** the new record is inserted.
125601**
125602** The caller must have already opened writeable cursors on the main
125603** table and all applicable indices (that is to say, all indices for which
125604** aRegIdx[] is not zero). iDataCur is the cursor for the main table when
125605** inserting or updating a rowid table, or the cursor for the PRIMARY KEY
125606** index when operating on a WITHOUT ROWID table. iIdxCur is the cursor
125607** for the first index in the pTab->pIndex list. Cursors for other indices
125608** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
125609**
125610** This routine also generates code to check constraints. NOT NULL,
125611** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
125612** then the appropriate action is performed. There are five possible
125613** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
125614**
125615** Constraint type Action What Happens
125616** --------------- ---------- ----------------------------------------
125617** any ROLLBACK The current transaction is rolled back and
125618** sqlite3_step() returns immediately with a
125619** return code of SQLITE_CONSTRAINT.
125620**
125621** any ABORT Back out changes from the current command
125622** only (do not do a complete rollback) then
125623** cause sqlite3_step() to return immediately
125624** with SQLITE_CONSTRAINT.
125625**
125626** any FAIL Sqlite3_step() returns immediately with a
125627** return code of SQLITE_CONSTRAINT. The
125628** transaction is not rolled back and any
125629** changes to prior rows are retained.
125630**
125631** any IGNORE The attempt in insert or update the current
125632** row is skipped, without throwing an error.
125633** Processing continues with the next row.
125634** (There is an immediate jump to ignoreDest.)
125635**
125636** NOT NULL REPLACE The NULL value is replace by the default
125637** value for that column. If the default value
125638** is NULL, the action is the same as ABORT.
125639**
125640** UNIQUE REPLACE The other row that conflicts with the row
125641** being inserted is removed.
125642**
125643** CHECK REPLACE Illegal. The results in an exception.
125644**
125645** Which action to take is determined by the overrideError parameter.
125646** Or if overrideError==OE_Default, then the pParse->onError parameter
125647** is used. Or if pParse->onError==OE_Default then the onError value
125648** for the constraint is used.
125649*/
125650SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
125651 Parse *pParse, /* The parser context */
125652 Table *pTab, /* The table being inserted or updated */
125653 int *aRegIdx, /* Use register aRegIdx[i] for index i. 0 for unused */
125654 int iDataCur, /* Canonical data cursor (main table or PK index) */
125655 int iIdxCur, /* First index cursor */
125656 int regNewData, /* First register in a range holding values to insert */
125657 int regOldData, /* Previous content. 0 for INSERTs */
125658 u8 pkChng, /* Non-zero if the rowid or PRIMARY KEY changed */
125659 u8 overrideError, /* Override onError to this if not OE_Default */
125660 int ignoreDest, /* Jump to this label on an OE_Ignore resolution */
125661 int *pbMayReplace, /* OUT: Set to true if constraint may cause a replace */
125662 int *aiChng, /* column i is unchanged if aiChng[i]<0 */
125663 Upsert *pUpsert /* ON CONFLICT clauses, if any. NULL otherwise */
125664){
125665 Vdbe *v; /* VDBE under constrution */
125666 Index *pIdx; /* Pointer to one of the indices */
125667 Index *pPk = 0; /* The PRIMARY KEY index for WITHOUT ROWID tables */
125668 sqlite3 *db; /* Database connection */
125669 int i; /* loop counter */
125670 int ix; /* Index loop counter */
125671 int nCol; /* Number of columns */
125672 int onError; /* Conflict resolution strategy */
125673 int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
125674 int nPkField; /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
125675 Upsert *pUpsertClause = 0; /* The specific ON CONFLICT clause for pIdx */
125676 u8 isUpdate; /* True if this is an UPDATE operation */
125677 u8 bAffinityDone = 0; /* True if the OP_Affinity operation has been run */
125678 int upsertIpkReturn = 0; /* Address of Goto at end of IPK uniqueness check */
125679 int upsertIpkDelay = 0; /* Address of Goto to bypass initial IPK check */
125680 int ipkTop = 0; /* Top of the IPK uniqueness check */
125681 int ipkBottom = 0; /* OP_Goto at the end of the IPK uniqueness check */
125682 /* Variables associated with retesting uniqueness constraints after
125683 ** replace triggers fire have run */
125684 int regTrigCnt; /* Register used to count replace trigger invocations */
125685 int addrRecheck = 0; /* Jump here to recheck all uniqueness constraints */
125686 int lblRecheckOk = 0; /* Each recheck jumps to this label if it passes */
125687 Trigger *pTrigger; /* List of DELETE triggers on the table pTab */
125688 int nReplaceTrig = 0; /* Number of replace triggers coded */
125689 IndexIterator sIdxIter; /* Index iterator */
125690
125691 isUpdate = regOldData!=0;
125692 db = pParse->db;
125693 v = pParse->pVdbe;
125694 assert( v!=0 );
125695 assert( !IsView(pTab) ); /* This table is not a VIEW */
125696 nCol = pTab->nCol;
125697
125698 /* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for
125699 ** normal rowid tables. nPkField is the number of key fields in the
125700 ** pPk index or 1 for a rowid table. In other words, nPkField is the
125701 ** number of fields in the true primary key of the table. */
125702 if( HasRowid(pTab) ){
125703 pPk = 0;
125704 nPkField = 1;
125705 }else{
125706 pPk = sqlite3PrimaryKeyIndex(pTab);
125707 nPkField = pPk->nKeyCol;
125708 }
125709
125710 /* Record that this module has started */
125711 VdbeModuleComment((v, "BEGIN: GenCnstCks(%d,%d,%d,%d,%d)",
125712 iDataCur, iIdxCur, regNewData, regOldData, pkChng));
125713
125714 /* Test all NOT NULL constraints.
125715 */
125716 if( pTab->tabFlags & TF_HasNotNull ){
125717 int b2ndPass = 0; /* True if currently running 2nd pass */
125718 int nSeenReplace = 0; /* Number of ON CONFLICT REPLACE operations */
125719 int nGenerated = 0; /* Number of generated columns with NOT NULL */
125720 while(1){ /* Make 2 passes over columns. Exit loop via "break" */
125721 for(i=0; i<nCol; i++){
125722 int iReg; /* Register holding column value */
125723 Column *pCol = &pTab->aCol[i]; /* The column to check for NOT NULL */
125724 int isGenerated; /* non-zero if column is generated */
125725 onError = pCol->notNull;
125726 if( onError==OE_None ) continue; /* No NOT NULL on this column */
125727 if( i==pTab->iPKey ){
125728 continue; /* ROWID is never NULL */
125729 }
125730 isGenerated = pCol->colFlags & COLFLAG_GENERATED;
125731 if( isGenerated && !b2ndPass ){
125732 nGenerated++;
125733 continue; /* Generated columns processed on 2nd pass */
125734 }
125735 if( aiChng && aiChng[i]<0 && !isGenerated ){
125736 /* Do not check NOT NULL on columns that do not change */
125737 continue;
125738 }
125739 if( overrideError!=OE_Default ){
125740 onError = overrideError;
125741 }else if( onError==OE_Default ){
125742 onError = OE_Abort;
125743 }
125744 if( onError==OE_Replace ){
125745 if( b2ndPass /* REPLACE becomes ABORT on the 2nd pass */
125746 || pCol->iDflt==0 /* REPLACE is ABORT if no DEFAULT value */
125747 ){
125748 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
125749 testcase( pCol->colFlags & COLFLAG_STORED );
125750 testcase( pCol->colFlags & COLFLAG_GENERATED );
125751 onError = OE_Abort;
125752 }else{
125753 assert( !isGenerated );
125754 }
125755 }else if( b2ndPass && !isGenerated ){
125756 continue;
125757 }
125758 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
125759 || onError==OE_Ignore || onError==OE_Replace );
125760 testcase( i!=sqlite3TableColumnToStorage(pTab, i) );
125761 iReg = sqlite3TableColumnToStorage(pTab, iCol: i) + regNewData + 1;
125762 switch( onError ){
125763 case OE_Replace: {
125764 int addr1 = sqlite3VdbeAddOp1(p: v, OP_NotNull, p1: iReg);
125765 VdbeCoverage(v);
125766 assert( (pCol->colFlags & COLFLAG_GENERATED)==0 );
125767 nSeenReplace++;
125768 sqlite3ExprCodeCopy(pParse,
125769 pExpr: sqlite3ColumnExpr(pTab, pCol), target: iReg);
125770 sqlite3VdbeJumpHere(p: v, addr: addr1);
125771 break;
125772 }
125773 case OE_Abort:
125774 sqlite3MayAbort(pParse);
125775 /* no break */ deliberate_fall_through
125776 case OE_Rollback:
125777 case OE_Fail: {
125778 char *zMsg = sqlite3MPrintf(db, zFormat: "%s.%s", pTab->zName,
125779 pCol->zCnName);
125780 sqlite3VdbeAddOp3(p: v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL,
125781 p2: onError, p3: iReg);
125782 sqlite3VdbeAppendP4(p: v, pP4: zMsg, P4_DYNAMIC);
125783 sqlite3VdbeChangeP5(p: v, P5_ConstraintNotNull);
125784 VdbeCoverage(v);
125785 break;
125786 }
125787 default: {
125788 assert( onError==OE_Ignore );
125789 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: iReg, p2: ignoreDest);
125790 VdbeCoverage(v);
125791 break;
125792 }
125793 } /* end switch(onError) */
125794 } /* end loop i over columns */
125795 if( nGenerated==0 && nSeenReplace==0 ){
125796 /* If there are no generated columns with NOT NULL constraints
125797 ** and no NOT NULL ON CONFLICT REPLACE constraints, then a single
125798 ** pass is sufficient */
125799 break;
125800 }
125801 if( b2ndPass ) break; /* Never need more than 2 passes */
125802 b2ndPass = 1;
125803#ifndef SQLITE_OMIT_GENERATED_COLUMNS
125804 if( nSeenReplace>0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){
125805 /* If any NOT NULL ON CONFLICT REPLACE constraints fired on the
125806 ** first pass, recomputed values for all generated columns, as
125807 ** those values might depend on columns affected by the REPLACE.
125808 */
125809 sqlite3ComputeGeneratedColumns(pParse, iRegStore: regNewData+1, pTab);
125810 }
125811#endif
125812 } /* end of 2-pass loop */
125813 } /* end if( has-not-null-constraints ) */
125814
125815 /* Test all CHECK constraints
125816 */
125817#ifndef SQLITE_OMIT_CHECK
125818 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
125819 ExprList *pCheck = pTab->pCheck;
125820 pParse->iSelfTab = -(regNewData+1);
125821 onError = overrideError!=OE_Default ? overrideError : OE_Abort;
125822 for(i=0; i<pCheck->nExpr; i++){
125823 int allOk;
125824 Expr *pCopy;
125825 Expr *pExpr = pCheck->a[i].pExpr;
125826 if( aiChng
125827 && !sqlite3ExprReferencesUpdatedColumn(pExpr, aiChng, chngRowid: pkChng)
125828 ){
125829 /* The check constraints do not reference any of the columns being
125830 ** updated so there is no point it verifying the check constraint */
125831 continue;
125832 }
125833 if( bAffinityDone==0 ){
125834 sqlite3TableAffinity(v, pTab, iReg: regNewData+1);
125835 bAffinityDone = 1;
125836 }
125837 allOk = sqlite3VdbeMakeLabel(pParse);
125838 sqlite3VdbeVerifyAbortable(v, onError);
125839 pCopy = sqlite3ExprDup(db, p: pExpr, flags: 0);
125840 if( !db->mallocFailed ){
125841 sqlite3ExprIfTrue(pParse, pExpr: pCopy, dest: allOk, SQLITE_JUMPIFNULL);
125842 }
125843 sqlite3ExprDelete(db, p: pCopy);
125844 if( onError==OE_Ignore ){
125845 sqlite3VdbeGoto(p: v, iDest: ignoreDest);
125846 }else{
125847 char *zName = pCheck->a[i].zEName;
125848 assert( zName!=0 || pParse->db->mallocFailed );
125849 if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-26383-51744 */
125850 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_CHECK,
125851 onError, p4: zName, P4_TRANSIENT,
125852 P5_ConstraintCheck);
125853 }
125854 sqlite3VdbeResolveLabel(v, x: allOk);
125855 }
125856 pParse->iSelfTab = 0;
125857 }
125858#endif /* !defined(SQLITE_OMIT_CHECK) */
125859
125860 /* UNIQUE and PRIMARY KEY constraints should be handled in the following
125861 ** order:
125862 **
125863 ** (1) OE_Update
125864 ** (2) OE_Abort, OE_Fail, OE_Rollback, OE_Ignore
125865 ** (3) OE_Replace
125866 **
125867 ** OE_Fail and OE_Ignore must happen before any changes are made.
125868 ** OE_Update guarantees that only a single row will change, so it
125869 ** must happen before OE_Replace. Technically, OE_Abort and OE_Rollback
125870 ** could happen in any order, but they are grouped up front for
125871 ** convenience.
125872 **
125873 ** 2018-08-14: Ticket https://www.sqlite.org/src/info/908f001483982c43
125874 ** The order of constraints used to have OE_Update as (2) and OE_Abort
125875 ** and so forth as (1). But apparently PostgreSQL checks the OE_Update
125876 ** constraint before any others, so it had to be moved.
125877 **
125878 ** Constraint checking code is generated in this order:
125879 ** (A) The rowid constraint
125880 ** (B) Unique index constraints that do not have OE_Replace as their
125881 ** default conflict resolution strategy
125882 ** (C) Unique index that do use OE_Replace by default.
125883 **
125884 ** The ordering of (2) and (3) is accomplished by making sure the linked
125885 ** list of indexes attached to a table puts all OE_Replace indexes last
125886 ** in the list. See sqlite3CreateIndex() for where that happens.
125887 */
125888 sIdxIter.eType = 0;
125889 sIdxIter.i = 0;
125890 sIdxIter.u.ax.aIdx = 0; /* Silence harmless compiler warning */
125891 sIdxIter.u.lx.pIdx = pTab->pIndex;
125892 if( pUpsert ){
125893 if( pUpsert->pUpsertTarget==0 ){
125894 /* There is just on ON CONFLICT clause and it has no constraint-target */
125895 assert( pUpsert->pNextUpsert==0 );
125896 if( pUpsert->isDoUpdate==0 ){
125897 /* A single ON CONFLICT DO NOTHING clause, without a constraint-target.
125898 ** Make all unique constraint resolution be OE_Ignore */
125899 overrideError = OE_Ignore;
125900 pUpsert = 0;
125901 }else{
125902 /* A single ON CONFLICT DO UPDATE. Make all resolutions OE_Update */
125903 overrideError = OE_Update;
125904 }
125905 }else if( pTab->pIndex!=0 ){
125906 /* Otherwise, we'll need to run the IndexListTerm array version of the
125907 ** iterator to ensure that all of the ON CONFLICT conditions are
125908 ** checked first and in order. */
125909 int nIdx, jj;
125910 u64 nByte;
125911 Upsert *pTerm;
125912 u8 *bUsed;
125913 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
125914 assert( aRegIdx[nIdx]>0 );
125915 }
125916 sIdxIter.eType = 1;
125917 sIdxIter.u.ax.nIdx = nIdx;
125918 nByte = (sizeof(IndexListTerm)+1)*nIdx + nIdx;
125919 sIdxIter.u.ax.aIdx = sqlite3DbMallocZero(db, n: nByte);
125920 if( sIdxIter.u.ax.aIdx==0 ) return; /* OOM */
125921 bUsed = (u8*)&sIdxIter.u.ax.aIdx[nIdx];
125922 pUpsert->pToFree = sIdxIter.u.ax.aIdx;
125923 for(i=0, pTerm=pUpsert; pTerm; pTerm=pTerm->pNextUpsert){
125924 if( pTerm->pUpsertTarget==0 ) break;
125925 if( pTerm->pUpsertIdx==0 ) continue; /* Skip ON CONFLICT for the IPK */
125926 jj = 0;
125927 pIdx = pTab->pIndex;
125928 while( ALWAYS(pIdx!=0) && pIdx!=pTerm->pUpsertIdx ){
125929 pIdx = pIdx->pNext;
125930 jj++;
125931 }
125932 if( bUsed[jj] ) continue; /* Duplicate ON CONFLICT clause ignored */
125933 bUsed[jj] = 1;
125934 sIdxIter.u.ax.aIdx[i].p = pIdx;
125935 sIdxIter.u.ax.aIdx[i].ix = jj;
125936 i++;
125937 }
125938 for(jj=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, jj++){
125939 if( bUsed[jj] ) continue;
125940 sIdxIter.u.ax.aIdx[i].p = pIdx;
125941 sIdxIter.u.ax.aIdx[i].ix = jj;
125942 i++;
125943 }
125944 assert( i==nIdx );
125945 }
125946 }
125947
125948 /* Determine if it is possible that triggers (either explicitly coded
125949 ** triggers or FK resolution actions) might run as a result of deletes
125950 ** that happen when OE_Replace conflict resolution occurs. (Call these
125951 ** "replace triggers".) If any replace triggers run, we will need to
125952 ** recheck all of the uniqueness constraints after they have all run.
125953 ** But on the recheck, the resolution is OE_Abort instead of OE_Replace.
125954 **
125955 ** If replace triggers are a possibility, then
125956 **
125957 ** (1) Allocate register regTrigCnt and initialize it to zero.
125958 ** That register will count the number of replace triggers that
125959 ** fire. Constraint recheck only occurs if the number is positive.
125960 ** (2) Initialize pTrigger to the list of all DELETE triggers on pTab.
125961 ** (3) Initialize addrRecheck and lblRecheckOk
125962 **
125963 ** The uniqueness rechecking code will create a series of tests to run
125964 ** in a second pass. The addrRecheck and lblRecheckOk variables are
125965 ** used to link together these tests which are separated from each other
125966 ** in the generate bytecode.
125967 */
125968 if( (db->flags & (SQLITE_RecTriggers|SQLITE_ForeignKeys))==0 ){
125969 /* There are not DELETE triggers nor FK constraints. No constraint
125970 ** rechecks are needed. */
125971 pTrigger = 0;
125972 regTrigCnt = 0;
125973 }else{
125974 if( db->flags&SQLITE_RecTriggers ){
125975 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, pMask: 0);
125976 regTrigCnt = pTrigger!=0 || sqlite3FkRequired(pParse, pTab, aChange: 0, chngRowid: 0);
125977 }else{
125978 pTrigger = 0;
125979 regTrigCnt = sqlite3FkRequired(pParse, pTab, aChange: 0, chngRowid: 0);
125980 }
125981 if( regTrigCnt ){
125982 /* Replace triggers might exist. Allocate the counter and
125983 ** initialize it to zero. */
125984 regTrigCnt = ++pParse->nMem;
125985 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: regTrigCnt);
125986 VdbeComment((v, "trigger count"));
125987 lblRecheckOk = sqlite3VdbeMakeLabel(pParse);
125988 addrRecheck = lblRecheckOk;
125989 }
125990 }
125991
125992 /* If rowid is changing, make sure the new rowid does not previously
125993 ** exist in the table.
125994 */
125995 if( pkChng && pPk==0 ){
125996 int addrRowidOk = sqlite3VdbeMakeLabel(pParse);
125997
125998 /* Figure out what action to take in case of a rowid collision */
125999 onError = pTab->keyConf;
126000 if( overrideError!=OE_Default ){
126001 onError = overrideError;
126002 }else if( onError==OE_Default ){
126003 onError = OE_Abort;
126004 }
126005
126006 /* figure out whether or not upsert applies in this case */
126007 if( pUpsert ){
126008 pUpsertClause = sqlite3UpsertOfIndex(pUpsert,0);
126009 if( pUpsertClause!=0 ){
126010 if( pUpsertClause->isDoUpdate==0 ){
126011 onError = OE_Ignore; /* DO NOTHING is the same as INSERT OR IGNORE */
126012 }else{
126013 onError = OE_Update; /* DO UPDATE */
126014 }
126015 }
126016 if( pUpsertClause!=pUpsert ){
126017 /* The first ON CONFLICT clause has a conflict target other than
126018 ** the IPK. We have to jump ahead to that first ON CONFLICT clause
126019 ** and then come back here and deal with the IPK afterwards */
126020 upsertIpkDelay = sqlite3VdbeAddOp0(p: v, OP_Goto);
126021 }
126022 }
126023
126024 /* If the response to a rowid conflict is REPLACE but the response
126025 ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
126026 ** to defer the running of the rowid conflict checking until after
126027 ** the UNIQUE constraints have run.
126028 */
126029 if( onError==OE_Replace /* IPK rule is REPLACE */
126030 && onError!=overrideError /* Rules for other constraints are different */
126031 && pTab->pIndex /* There exist other constraints */
126032 && !upsertIpkDelay /* IPK check already deferred by UPSERT */
126033 ){
126034 ipkTop = sqlite3VdbeAddOp0(p: v, OP_Goto)+1;
126035 VdbeComment((v, "defer IPK REPLACE until last"));
126036 }
126037
126038 if( isUpdate ){
126039 /* pkChng!=0 does not mean that the rowid has changed, only that
126040 ** it might have changed. Skip the conflict logic below if the rowid
126041 ** is unchanged. */
126042 sqlite3VdbeAddOp3(p: v, OP_Eq, p1: regNewData, p2: addrRowidOk, p3: regOldData);
126043 sqlite3VdbeChangeP5(p: v, SQLITE_NOTNULL);
126044 VdbeCoverage(v);
126045 }
126046
126047 /* Check to see if the new rowid already exists in the table. Skip
126048 ** the following conflict logic if it does not. */
126049 VdbeNoopComment((v, "uniqueness check for ROWID"));
126050 sqlite3VdbeVerifyAbortable(v, onError);
126051 sqlite3VdbeAddOp3(p: v, OP_NotExists, p1: iDataCur, p2: addrRowidOk, p3: regNewData);
126052 VdbeCoverage(v);
126053
126054 switch( onError ){
126055 default: {
126056 onError = OE_Abort;
126057 /* no break */ deliberate_fall_through
126058 }
126059 case OE_Rollback:
126060 case OE_Abort:
126061 case OE_Fail: {
126062 testcase( onError==OE_Rollback );
126063 testcase( onError==OE_Abort );
126064 testcase( onError==OE_Fail );
126065 sqlite3RowidConstraint(pParse, onError, pTab);
126066 break;
126067 }
126068 case OE_Replace: {
126069 /* If there are DELETE triggers on this table and the
126070 ** recursive-triggers flag is set, call GenerateRowDelete() to
126071 ** remove the conflicting row from the table. This will fire
126072 ** the triggers and remove both the table and index b-tree entries.
126073 **
126074 ** Otherwise, if there are no triggers or the recursive-triggers
126075 ** flag is not set, but the table has one or more indexes, call
126076 ** GenerateRowIndexDelete(). This removes the index b-tree entries
126077 ** only. The table b-tree entry will be replaced by the new entry
126078 ** when it is inserted.
126079 **
126080 ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called,
126081 ** also invoke MultiWrite() to indicate that this VDBE may require
126082 ** statement rollback (if the statement is aborted after the delete
126083 ** takes place). Earlier versions called sqlite3MultiWrite() regardless,
126084 ** but being more selective here allows statements like:
126085 **
126086 ** REPLACE INTO t(rowid) VALUES($newrowid)
126087 **
126088 ** to run without a statement journal if there are no indexes on the
126089 ** table.
126090 */
126091 if( regTrigCnt ){
126092 sqlite3MultiWrite(pParse);
126093 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
126094 iPk: regNewData, nPk: 1, count: 0, OE_Replace, eMode: 1, iIdxNoSeek: -1);
126095 sqlite3VdbeAddOp2(p: v, OP_AddImm, p1: regTrigCnt, p2: 1); /* incr trigger cnt */
126096 nReplaceTrig++;
126097 }else{
126098#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
126099 assert( HasRowid(pTab) );
126100 /* This OP_Delete opcode fires the pre-update-hook only. It does
126101 ** not modify the b-tree. It is more efficient to let the coming
126102 ** OP_Insert replace the existing entry than it is to delete the
126103 ** existing entry and then insert a new one. */
126104 sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, OPFLAG_ISNOOP);
126105 sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
126106#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
126107 if( pTab->pIndex ){
126108 sqlite3MultiWrite(pParse);
126109 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,aRegIdx: 0,iIdxNoSeek: -1);
126110 }
126111 }
126112 seenReplace = 1;
126113 break;
126114 }
126115#ifndef SQLITE_OMIT_UPSERT
126116 case OE_Update: {
126117 sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, 0, iDataCur);
126118 /* no break */ deliberate_fall_through
126119 }
126120#endif
126121 case OE_Ignore: {
126122 testcase( onError==OE_Ignore );
126123 sqlite3VdbeGoto(p: v, iDest: ignoreDest);
126124 break;
126125 }
126126 }
126127 sqlite3VdbeResolveLabel(v, x: addrRowidOk);
126128 if( pUpsert && pUpsertClause!=pUpsert ){
126129 upsertIpkReturn = sqlite3VdbeAddOp0(p: v, OP_Goto);
126130 }else if( ipkTop ){
126131 ipkBottom = sqlite3VdbeAddOp0(p: v, OP_Goto);
126132 sqlite3VdbeJumpHere(p: v, addr: ipkTop-1);
126133 }
126134 }
126135
126136 /* Test all UNIQUE constraints by creating entries for each UNIQUE
126137 ** index and making sure that duplicate entries do not already exist.
126138 ** Compute the revised record entries for indices as we go.
126139 **
126140 ** This loop also handles the case of the PRIMARY KEY index for a
126141 ** WITHOUT ROWID table.
126142 */
126143 for(pIdx = indexIteratorFirst(pIter: &sIdxIter, pIx: &ix);
126144 pIdx;
126145 pIdx = indexIteratorNext(pIter: &sIdxIter, pIx: &ix)
126146 ){
126147 int regIdx; /* Range of registers hold conent for pIdx */
126148 int regR; /* Range of registers holding conflicting PK */
126149 int iThisCur; /* Cursor for this UNIQUE index */
126150 int addrUniqueOk; /* Jump here if the UNIQUE constraint is satisfied */
126151 int addrConflictCk; /* First opcode in the conflict check logic */
126152
126153 if( aRegIdx[ix]==0 ) continue; /* Skip indices that do not change */
126154 if( pUpsert ){
126155 pUpsertClause = sqlite3UpsertOfIndex(pUpsert, pIdx);
126156 if( upsertIpkDelay && pUpsertClause==pUpsert ){
126157 sqlite3VdbeJumpHere(p: v, addr: upsertIpkDelay);
126158 }
126159 }
126160 addrUniqueOk = sqlite3VdbeMakeLabel(pParse);
126161 if( bAffinityDone==0 ){
126162 sqlite3TableAffinity(v, pTab, iReg: regNewData+1);
126163 bAffinityDone = 1;
126164 }
126165 VdbeNoopComment((v, "prep index %s", pIdx->zName));
126166 iThisCur = iIdxCur+ix;
126167
126168
126169 /* Skip partial indices for which the WHERE clause is not true */
126170 if( pIdx->pPartIdxWhere ){
126171 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: aRegIdx[ix]);
126172 pParse->iSelfTab = -(regNewData+1);
126173 sqlite3ExprIfFalseDup(pParse, pExpr: pIdx->pPartIdxWhere, dest: addrUniqueOk,
126174 SQLITE_JUMPIFNULL);
126175 pParse->iSelfTab = 0;
126176 }
126177
126178 /* Create a record for this index entry as it should appear after
126179 ** the insert or update. Store that record in the aRegIdx[ix] register
126180 */
126181 regIdx = aRegIdx[ix]+1;
126182 for(i=0; i<pIdx->nColumn; i++){
126183 int iField = pIdx->aiColumn[i];
126184 int x;
126185 if( iField==XN_EXPR ){
126186 pParse->iSelfTab = -(regNewData+1);
126187 sqlite3ExprCodeCopy(pParse, pExpr: pIdx->aColExpr->a[i].pExpr, target: regIdx+i);
126188 pParse->iSelfTab = 0;
126189 VdbeComment((v, "%s column %d", pIdx->zName, i));
126190 }else if( iField==XN_ROWID || iField==pTab->iPKey ){
126191 x = regNewData;
126192 sqlite3VdbeAddOp2(p: v, OP_IntCopy, p1: x, p2: regIdx+i);
126193 VdbeComment((v, "rowid"));
126194 }else{
126195 testcase( sqlite3TableColumnToStorage(pTab, iField)!=iField );
126196 x = sqlite3TableColumnToStorage(pTab, iCol: iField) + regNewData + 1;
126197 sqlite3VdbeAddOp2(p: v, OP_SCopy, p1: x, p2: regIdx+i);
126198 VdbeComment((v, "%s", pTab->aCol[iField].zCnName));
126199 }
126200 }
126201 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: regIdx, p2: pIdx->nColumn, p3: aRegIdx[ix]);
126202 VdbeComment((v, "for %s", pIdx->zName));
126203#ifdef SQLITE_ENABLE_NULL_TRIM
126204 if( pIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
126205 sqlite3SetMakeRecordP5(v, pIdx->pTable);
126206 }
126207#endif
126208 sqlite3VdbeReleaseRegisters(pParse, regIdx, pIdx->nColumn, 0, 0);
126209
126210 /* In an UPDATE operation, if this index is the PRIMARY KEY index
126211 ** of a WITHOUT ROWID table and there has been no change the
126212 ** primary key, then no collision is possible. The collision detection
126213 ** logic below can all be skipped. */
126214 if( isUpdate && pPk==pIdx && pkChng==0 ){
126215 sqlite3VdbeResolveLabel(v, x: addrUniqueOk);
126216 continue;
126217 }
126218
126219 /* Find out what action to take in case there is a uniqueness conflict */
126220 onError = pIdx->onError;
126221 if( onError==OE_None ){
126222 sqlite3VdbeResolveLabel(v, x: addrUniqueOk);
126223 continue; /* pIdx is not a UNIQUE index */
126224 }
126225 if( overrideError!=OE_Default ){
126226 onError = overrideError;
126227 }else if( onError==OE_Default ){
126228 onError = OE_Abort;
126229 }
126230
126231 /* Figure out if the upsert clause applies to this index */
126232 if( pUpsertClause ){
126233 if( pUpsertClause->isDoUpdate==0 ){
126234 onError = OE_Ignore; /* DO NOTHING is the same as INSERT OR IGNORE */
126235 }else{
126236 onError = OE_Update; /* DO UPDATE */
126237 }
126238 }
126239
126240 /* Collision detection may be omitted if all of the following are true:
126241 ** (1) The conflict resolution algorithm is REPLACE
126242 ** (2) The table is a WITHOUT ROWID table
126243 ** (3) There are no secondary indexes on the table
126244 ** (4) No delete triggers need to be fired if there is a conflict
126245 ** (5) No FK constraint counters need to be updated if a conflict occurs.
126246 **
126247 ** This is not possible for ENABLE_PREUPDATE_HOOK builds, as the row
126248 ** must be explicitly deleted in order to ensure any pre-update hook
126249 ** is invoked. */
126250 assert( IsOrdinaryTable(pTab) );
126251#ifndef SQLITE_ENABLE_PREUPDATE_HOOK
126252 if( (ix==0 && pIdx->pNext==0) /* Condition 3 */
126253 && pPk==pIdx /* Condition 2 */
126254 && onError==OE_Replace /* Condition 1 */
126255 && ( 0==(db->flags&SQLITE_RecTriggers) || /* Condition 4 */
126256 0==sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, pMask: 0))
126257 && ( 0==(db->flags&SQLITE_ForeignKeys) || /* Condition 5 */
126258 (0==pTab->u.tab.pFKey && 0==sqlite3FkReferences(pTab)))
126259 ){
126260 sqlite3VdbeResolveLabel(v, x: addrUniqueOk);
126261 continue;
126262 }
126263#endif /* ifndef SQLITE_ENABLE_PREUPDATE_HOOK */
126264
126265 /* Check to see if the new index entry will be unique */
126266 sqlite3VdbeVerifyAbortable(v, onError);
126267 addrConflictCk =
126268 sqlite3VdbeAddOp4Int(p: v, OP_NoConflict, p1: iThisCur, p2: addrUniqueOk,
126269 p3: regIdx, p4: pIdx->nKeyCol); VdbeCoverage(v);
126270
126271 /* Generate code to handle collisions */
126272 regR = pIdx==pPk ? regIdx : sqlite3GetTempRange(pParse, nReg: nPkField);
126273 if( isUpdate || onError==OE_Replace ){
126274 if( HasRowid(pTab) ){
126275 sqlite3VdbeAddOp2(p: v, OP_IdxRowid, p1: iThisCur, p2: regR);
126276 /* Conflict only if the rowid of the existing index entry
126277 ** is different from old-rowid */
126278 if( isUpdate ){
126279 sqlite3VdbeAddOp3(p: v, OP_Eq, p1: regR, p2: addrUniqueOk, p3: regOldData);
126280 sqlite3VdbeChangeP5(p: v, SQLITE_NOTNULL);
126281 VdbeCoverage(v);
126282 }
126283 }else{
126284 int x;
126285 /* Extract the PRIMARY KEY from the end of the index entry and
126286 ** store it in registers regR..regR+nPk-1 */
126287 if( pIdx!=pPk ){
126288 for(i=0; i<pPk->nKeyCol; i++){
126289 assert( pPk->aiColumn[i]>=0 );
126290 x = sqlite3TableColumnToIndex(pIdx, iCol: pPk->aiColumn[i]);
126291 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iThisCur, p2: x, p3: regR+i);
126292 VdbeComment((v, "%s.%s", pTab->zName,
126293 pTab->aCol[pPk->aiColumn[i]].zCnName));
126294 }
126295 }
126296 if( isUpdate ){
126297 /* If currently processing the PRIMARY KEY of a WITHOUT ROWID
126298 ** table, only conflict if the new PRIMARY KEY values are actually
126299 ** different from the old.
126300 **
126301 ** For a UNIQUE index, only conflict if the PRIMARY KEY values
126302 ** of the matched index row are different from the original PRIMARY
126303 ** KEY values of this row before the update. */
126304 int addrJump = sqlite3VdbeCurrentAddr(p: v)+pPk->nKeyCol;
126305 int op = OP_Ne;
126306 int regCmp = (IsPrimaryKeyIndex(pIdx) ? regIdx : regR);
126307
126308 for(i=0; i<pPk->nKeyCol; i++){
126309 char *p4 = (char*)sqlite3LocateCollSeq(pParse, zName: pPk->azColl[i]);
126310 x = pPk->aiColumn[i];
126311 assert( x>=0 );
126312 if( i==(pPk->nKeyCol-1) ){
126313 addrJump = addrUniqueOk;
126314 op = OP_Eq;
126315 }
126316 x = sqlite3TableColumnToStorage(pTab, iCol: x);
126317 sqlite3VdbeAddOp4(p: v, op,
126318 p1: regOldData+1+x, p2: addrJump, p3: regCmp+i, zP4: p4, P4_COLLSEQ
126319 );
126320 sqlite3VdbeChangeP5(p: v, SQLITE_NOTNULL);
126321 VdbeCoverageIf(v, op==OP_Eq);
126322 VdbeCoverageIf(v, op==OP_Ne);
126323 }
126324 }
126325 }
126326 }
126327
126328 /* Generate code that executes if the new index entry is not unique */
126329 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
126330 || onError==OE_Ignore || onError==OE_Replace || onError==OE_Update );
126331 switch( onError ){
126332 case OE_Rollback:
126333 case OE_Abort:
126334 case OE_Fail: {
126335 testcase( onError==OE_Rollback );
126336 testcase( onError==OE_Abort );
126337 testcase( onError==OE_Fail );
126338 sqlite3UniqueConstraint(pParse, onError, pIdx);
126339 break;
126340 }
126341#ifndef SQLITE_OMIT_UPSERT
126342 case OE_Update: {
126343 sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, pIdx, iIdxCur+ix);
126344 /* no break */ deliberate_fall_through
126345 }
126346#endif
126347 case OE_Ignore: {
126348 testcase( onError==OE_Ignore );
126349 sqlite3VdbeGoto(p: v, iDest: ignoreDest);
126350 break;
126351 }
126352 default: {
126353 int nConflictCk; /* Number of opcodes in conflict check logic */
126354
126355 assert( onError==OE_Replace );
126356 nConflictCk = sqlite3VdbeCurrentAddr(p: v) - addrConflictCk;
126357 assert( nConflictCk>0 || db->mallocFailed );
126358 testcase( nConflictCk<=0 );
126359 testcase( nConflictCk>1 );
126360 if( regTrigCnt ){
126361 sqlite3MultiWrite(pParse);
126362 nReplaceTrig++;
126363 }
126364 if( pTrigger && isUpdate ){
126365 sqlite3VdbeAddOp1(p: v, OP_CursorLock, p1: iDataCur);
126366 }
126367 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
126368 iPk: regR, nPk: nPkField, count: 0, OE_Replace,
126369 eMode: (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), iIdxNoSeek: iThisCur);
126370 if( pTrigger && isUpdate ){
126371 sqlite3VdbeAddOp1(p: v, OP_CursorUnlock, p1: iDataCur);
126372 }
126373 if( regTrigCnt ){
126374 int addrBypass; /* Jump destination to bypass recheck logic */
126375
126376 sqlite3VdbeAddOp2(p: v, OP_AddImm, p1: regTrigCnt, p2: 1); /* incr trigger cnt */
126377 addrBypass = sqlite3VdbeAddOp0(p: v, OP_Goto); /* Bypass recheck */
126378 VdbeComment((v, "bypass recheck"));
126379
126380 /* Here we insert code that will be invoked after all constraint
126381 ** checks have run, if and only if one or more replace triggers
126382 ** fired. */
126383 sqlite3VdbeResolveLabel(v, x: lblRecheckOk);
126384 lblRecheckOk = sqlite3VdbeMakeLabel(pParse);
126385 if( pIdx->pPartIdxWhere ){
126386 /* Bypass the recheck if this partial index is not defined
126387 ** for the current row */
126388 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: regIdx-1, p2: lblRecheckOk);
126389 VdbeCoverage(v);
126390 }
126391 /* Copy the constraint check code from above, except change
126392 ** the constraint-ok jump destination to be the address of
126393 ** the next retest block */
126394 while( nConflictCk>0 ){
126395 VdbeOp x; /* Conflict check opcode to copy */
126396 /* The sqlite3VdbeAddOp4() call might reallocate the opcode array.
126397 ** Hence, make a complete copy of the opcode, rather than using
126398 ** a pointer to the opcode. */
126399 x = *sqlite3VdbeGetOp(p: v, addr: addrConflictCk);
126400 if( x.opcode!=OP_IdxRowid ){
126401 int p2; /* New P2 value for copied conflict check opcode */
126402 const char *zP4;
126403 if( sqlite3OpcodeProperty[x.opcode]&OPFLG_JUMP ){
126404 p2 = lblRecheckOk;
126405 }else{
126406 p2 = x.p2;
126407 }
126408 zP4 = x.p4type==P4_INT32 ? SQLITE_INT_TO_PTR(x.p4.i) : x.p4.z;
126409 sqlite3VdbeAddOp4(p: v, op: x.opcode, p1: x.p1, p2, p3: x.p3, zP4, p4type: x.p4type);
126410 sqlite3VdbeChangeP5(p: v, p5: x.p5);
126411 VdbeCoverageIf(v, p2!=x.p2);
126412 }
126413 nConflictCk--;
126414 addrConflictCk++;
126415 }
126416 /* If the retest fails, issue an abort */
126417 sqlite3UniqueConstraint(pParse, OE_Abort, pIdx);
126418
126419 sqlite3VdbeJumpHere(p: v, addr: addrBypass); /* Terminate the recheck bypass */
126420 }
126421 seenReplace = 1;
126422 break;
126423 }
126424 }
126425 sqlite3VdbeResolveLabel(v, x: addrUniqueOk);
126426 if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, iReg: regR, nReg: nPkField);
126427 if( pUpsertClause
126428 && upsertIpkReturn
126429 && sqlite3UpsertNextIsIPK(pUpsertClause)
126430 ){
126431 sqlite3VdbeGoto(p: v, iDest: upsertIpkDelay+1);
126432 sqlite3VdbeJumpHere(p: v, addr: upsertIpkReturn);
126433 upsertIpkReturn = 0;
126434 }
126435 }
126436
126437 /* If the IPK constraint is a REPLACE, run it last */
126438 if( ipkTop ){
126439 sqlite3VdbeGoto(p: v, iDest: ipkTop);
126440 VdbeComment((v, "Do IPK REPLACE"));
126441 assert( ipkBottom>0 );
126442 sqlite3VdbeJumpHere(p: v, addr: ipkBottom);
126443 }
126444
126445 /* Recheck all uniqueness constraints after replace triggers have run */
126446 testcase( regTrigCnt!=0 && nReplaceTrig==0 );
126447 assert( regTrigCnt!=0 || nReplaceTrig==0 );
126448 if( nReplaceTrig ){
126449 sqlite3VdbeAddOp2(p: v, OP_IfNot, p1: regTrigCnt, p2: lblRecheckOk);VdbeCoverage(v);
126450 if( !pPk ){
126451 if( isUpdate ){
126452 sqlite3VdbeAddOp3(p: v, OP_Eq, p1: regNewData, p2: addrRecheck, p3: regOldData);
126453 sqlite3VdbeChangeP5(p: v, SQLITE_NOTNULL);
126454 VdbeCoverage(v);
126455 }
126456 sqlite3VdbeAddOp3(p: v, OP_NotExists, p1: iDataCur, p2: addrRecheck, p3: regNewData);
126457 VdbeCoverage(v);
126458 sqlite3RowidConstraint(pParse, OE_Abort, pTab);
126459 }else{
126460 sqlite3VdbeGoto(p: v, iDest: addrRecheck);
126461 }
126462 sqlite3VdbeResolveLabel(v, x: lblRecheckOk);
126463 }
126464
126465 /* Generate the table record */
126466 if( HasRowid(pTab) ){
126467 int regRec = aRegIdx[ix];
126468 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: regNewData+1, p2: pTab->nNVCol, p3: regRec);
126469 sqlite3SetMakeRecordP5(v, pTab);
126470 if( !bAffinityDone ){
126471 sqlite3TableAffinity(v, pTab, iReg: 0);
126472 }
126473 }
126474
126475 *pbMayReplace = seenReplace;
126476 VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
126477}
126478
126479#ifdef SQLITE_ENABLE_NULL_TRIM
126480/*
126481** Change the P5 operand on the last opcode (which should be an OP_MakeRecord)
126482** to be the number of columns in table pTab that must not be NULL-trimmed.
126483**
126484** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
126485*/
126486SQLITE_PRIVATE void sqlite3SetMakeRecordP5(Vdbe *v, Table *pTab){
126487 u16 i;
126488
126489 /* Records with omitted columns are only allowed for schema format
126490 ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
126491 if( pTab->pSchema->file_format<2 ) return;
126492
126493 for(i=pTab->nCol-1; i>0; i--){
126494 if( pTab->aCol[i].iDflt!=0 ) break;
126495 if( pTab->aCol[i].colFlags & COLFLAG_PRIMKEY ) break;
126496 }
126497 sqlite3VdbeChangeP5(v, i+1);
126498}
126499#endif
126500
126501/*
126502** Table pTab is a WITHOUT ROWID table that is being written to. The cursor
126503** number is iCur, and register regData contains the new record for the
126504** PK index. This function adds code to invoke the pre-update hook,
126505** if one is registered.
126506*/
126507#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
126508static void codeWithoutRowidPreupdate(
126509 Parse *pParse, /* Parse context */
126510 Table *pTab, /* Table being updated */
126511 int iCur, /* Cursor number for table */
126512 int regData /* Data containing new record */
126513){
126514 Vdbe *v = pParse->pVdbe;
126515 int r = sqlite3GetTempReg(pParse);
126516 assert( !HasRowid(pTab) );
126517 assert( 0==(pParse->db->mDbFlags & DBFLAG_Vacuum) || CORRUPT_DB );
126518 sqlite3VdbeAddOp2(v, OP_Integer, 0, r);
126519 sqlite3VdbeAddOp4(v, OP_Insert, iCur, regData, r, (char*)pTab, P4_TABLE);
126520 sqlite3VdbeChangeP5(v, OPFLAG_ISNOOP);
126521 sqlite3ReleaseTempReg(pParse, r);
126522}
126523#else
126524# define codeWithoutRowidPreupdate(a,b,c,d)
126525#endif
126526
126527/*
126528** This routine generates code to finish the INSERT or UPDATE operation
126529** that was started by a prior call to sqlite3GenerateConstraintChecks.
126530** A consecutive range of registers starting at regNewData contains the
126531** rowid and the content to be inserted.
126532**
126533** The arguments to this routine should be the same as the first six
126534** arguments to sqlite3GenerateConstraintChecks.
126535*/
126536SQLITE_PRIVATE void sqlite3CompleteInsertion(
126537 Parse *pParse, /* The parser context */
126538 Table *pTab, /* the table into which we are inserting */
126539 int iDataCur, /* Cursor of the canonical data source */
126540 int iIdxCur, /* First index cursor */
126541 int regNewData, /* Range of content */
126542 int *aRegIdx, /* Register used by each index. 0 for unused indices */
126543 int update_flags, /* True for UPDATE, False for INSERT */
126544 int appendBias, /* True if this is likely to be an append */
126545 int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
126546){
126547 Vdbe *v; /* Prepared statements under construction */
126548 Index *pIdx; /* An index being inserted or updated */
126549 u8 pik_flags; /* flag values passed to the btree insert */
126550 int i; /* Loop counter */
126551
126552 assert( update_flags==0
126553 || update_flags==OPFLAG_ISUPDATE
126554 || update_flags==(OPFLAG_ISUPDATE|OPFLAG_SAVEPOSITION)
126555 );
126556
126557 v = pParse->pVdbe;
126558 assert( v!=0 );
126559 assert( !IsView(pTab) ); /* This table is not a VIEW */
126560 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
126561 /* All REPLACE indexes are at the end of the list */
126562 assert( pIdx->onError!=OE_Replace
126563 || pIdx->pNext==0
126564 || pIdx->pNext->onError==OE_Replace );
126565 if( aRegIdx[i]==0 ) continue;
126566 if( pIdx->pPartIdxWhere ){
126567 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: aRegIdx[i], p2: sqlite3VdbeCurrentAddr(p: v)+2);
126568 VdbeCoverage(v);
126569 }
126570 pik_flags = (useSeekResult ? OPFLAG_USESEEKRESULT : 0);
126571 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
126572 pik_flags |= OPFLAG_NCHANGE;
126573 pik_flags |= (update_flags & OPFLAG_SAVEPOSITION);
126574 if( update_flags==0 ){
126575 codeWithoutRowidPreupdate(pParse, pTab, iIdxCur+i, aRegIdx[i]);
126576 }
126577 }
126578 sqlite3VdbeAddOp4Int(p: v, OP_IdxInsert, p1: iIdxCur+i, p2: aRegIdx[i],
126579 p3: aRegIdx[i]+1,
126580 p4: pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
126581 sqlite3VdbeChangeP5(p: v, p5: pik_flags);
126582 }
126583 if( !HasRowid(pTab) ) return;
126584 if( pParse->nested ){
126585 pik_flags = 0;
126586 }else{
126587 pik_flags = OPFLAG_NCHANGE;
126588 pik_flags |= (update_flags?update_flags:OPFLAG_LASTROWID);
126589 }
126590 if( appendBias ){
126591 pik_flags |= OPFLAG_APPEND;
126592 }
126593 if( useSeekResult ){
126594 pik_flags |= OPFLAG_USESEEKRESULT;
126595 }
126596 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: iDataCur, p2: aRegIdx[i], p3: regNewData);
126597 if( !pParse->nested ){
126598 sqlite3VdbeAppendP4(p: v, pP4: pTab, P4_TABLE);
126599 }
126600 sqlite3VdbeChangeP5(p: v, p5: pik_flags);
126601}
126602
126603/*
126604** Allocate cursors for the pTab table and all its indices and generate
126605** code to open and initialized those cursors.
126606**
126607** The cursor for the object that contains the complete data (normally
126608** the table itself, but the PRIMARY KEY index in the case of a WITHOUT
126609** ROWID table) is returned in *piDataCur. The first index cursor is
126610** returned in *piIdxCur. The number of indices is returned.
126611**
126612** Use iBase as the first cursor (either the *piDataCur for rowid tables
126613** or the first index for WITHOUT ROWID tables) if it is non-negative.
126614** If iBase is negative, then allocate the next available cursor.
126615**
126616** For a rowid table, *piDataCur will be exactly one less than *piIdxCur.
126617** For a WITHOUT ROWID table, *piDataCur will be somewhere in the range
126618** of *piIdxCurs, depending on where the PRIMARY KEY index appears on the
126619** pTab->pIndex list.
126620**
126621** If pTab is a virtual table, then this routine is a no-op and the
126622** *piDataCur and *piIdxCur values are left uninitialized.
126623*/
126624SQLITE_PRIVATE int sqlite3OpenTableAndIndices(
126625 Parse *pParse, /* Parsing context */
126626 Table *pTab, /* Table to be opened */
126627 int op, /* OP_OpenRead or OP_OpenWrite */
126628 u8 p5, /* P5 value for OP_Open* opcodes (except on WITHOUT ROWID) */
126629 int iBase, /* Use this for the table cursor, if there is one */
126630 u8 *aToOpen, /* If not NULL: boolean for each table and index */
126631 int *piDataCur, /* Write the database source cursor number here */
126632 int *piIdxCur /* Write the first index cursor number here */
126633){
126634 int i;
126635 int iDb;
126636 int iDataCur;
126637 Index *pIdx;
126638 Vdbe *v;
126639
126640 assert( op==OP_OpenRead || op==OP_OpenWrite );
126641 assert( op==OP_OpenWrite || p5==0 );
126642 if( IsVirtual(pTab) ){
126643 /* This routine is a no-op for virtual tables. Leave the output
126644 ** variables *piDataCur and *piIdxCur set to illegal cursor numbers
126645 ** for improved error detection. */
126646 *piDataCur = *piIdxCur = -999;
126647 return 0;
126648 }
126649 iDb = sqlite3SchemaToIndex(db: pParse->db, pTab->pSchema);
126650 v = pParse->pVdbe;
126651 assert( v!=0 );
126652 if( iBase<0 ) iBase = pParse->nTab;
126653 iDataCur = iBase++;
126654 if( piDataCur ) *piDataCur = iDataCur;
126655 if( HasRowid(pTab) && (aToOpen==0 || aToOpen[0]) ){
126656 sqlite3OpenTable(pParse, iCur: iDataCur, iDb, pTab, opcode: op);
126657 }else{
126658 sqlite3TableLock(pParse, iDb, iTab: pTab->tnum, isWriteLock: op==OP_OpenWrite, zName: pTab->zName);
126659 }
126660 if( piIdxCur ) *piIdxCur = iBase;
126661 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
126662 int iIdxCur = iBase++;
126663 assert( pIdx->pSchema==pTab->pSchema );
126664 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
126665 if( piDataCur ) *piDataCur = iIdxCur;
126666 p5 = 0;
126667 }
126668 if( aToOpen==0 || aToOpen[i+1] ){
126669 sqlite3VdbeAddOp3(p: v, op, p1: iIdxCur, p2: pIdx->tnum, p3: iDb);
126670 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
126671 sqlite3VdbeChangeP5(p: v, p5);
126672 VdbeComment((v, "%s", pIdx->zName));
126673 }
126674 }
126675 if( iBase>pParse->nTab ) pParse->nTab = iBase;
126676 return i;
126677}
126678
126679
126680#ifdef SQLITE_TEST
126681/*
126682** The following global variable is incremented whenever the
126683** transfer optimization is used. This is used for testing
126684** purposes only - to make sure the transfer optimization really
126685** is happening when it is supposed to.
126686*/
126687SQLITE_API int sqlite3_xferopt_count;
126688#endif /* SQLITE_TEST */
126689
126690
126691#ifndef SQLITE_OMIT_XFER_OPT
126692/*
126693** Check to see if index pSrc is compatible as a source of data
126694** for index pDest in an insert transfer optimization. The rules
126695** for a compatible index:
126696**
126697** * The index is over the same set of columns
126698** * The same DESC and ASC markings occurs on all columns
126699** * The same onError processing (OE_Abort, OE_Ignore, etc)
126700** * The same collating sequence on each column
126701** * The index has the exact same WHERE clause
126702*/
126703static int xferCompatibleIndex(Index *pDest, Index *pSrc){
126704 int i;
126705 assert( pDest && pSrc );
126706 assert( pDest->pTable!=pSrc->pTable );
126707 if( pDest->nKeyCol!=pSrc->nKeyCol || pDest->nColumn!=pSrc->nColumn ){
126708 return 0; /* Different number of columns */
126709 }
126710 if( pDest->onError!=pSrc->onError ){
126711 return 0; /* Different conflict resolution strategies */
126712 }
126713 for(i=0; i<pSrc->nKeyCol; i++){
126714 if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
126715 return 0; /* Different columns indexed */
126716 }
126717 if( pSrc->aiColumn[i]==XN_EXPR ){
126718 assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 );
126719 if( sqlite3ExprCompare(pParse: 0, pA: pSrc->aColExpr->a[i].pExpr,
126720 pB: pDest->aColExpr->a[i].pExpr, iTab: -1)!=0 ){
126721 return 0; /* Different expressions in the index */
126722 }
126723 }
126724 if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
126725 return 0; /* Different sort orders */
126726 }
126727 if( sqlite3_stricmp(zLeft: pSrc->azColl[i],zRight: pDest->azColl[i])!=0 ){
126728 return 0; /* Different collating sequences */
126729 }
126730 }
126731 if( sqlite3ExprCompare(pParse: 0, pA: pSrc->pPartIdxWhere, pB: pDest->pPartIdxWhere, iTab: -1) ){
126732 return 0; /* Different WHERE clauses */
126733 }
126734
126735 /* If no test above fails then the indices must be compatible */
126736 return 1;
126737}
126738
126739/*
126740** Attempt the transfer optimization on INSERTs of the form
126741**
126742** INSERT INTO tab1 SELECT * FROM tab2;
126743**
126744** The xfer optimization transfers raw records from tab2 over to tab1.
126745** Columns are not decoded and reassembled, which greatly improves
126746** performance. Raw index records are transferred in the same way.
126747**
126748** The xfer optimization is only attempted if tab1 and tab2 are compatible.
126749** There are lots of rules for determining compatibility - see comments
126750** embedded in the code for details.
126751**
126752** This routine returns TRUE if the optimization is guaranteed to be used.
126753** Sometimes the xfer optimization will only work if the destination table
126754** is empty - a factor that can only be determined at run-time. In that
126755** case, this routine generates code for the xfer optimization but also
126756** does a test to see if the destination table is empty and jumps over the
126757** xfer optimization code if the test fails. In that case, this routine
126758** returns FALSE so that the caller will know to go ahead and generate
126759** an unoptimized transfer. This routine also returns FALSE if there
126760** is no chance that the xfer optimization can be applied.
126761**
126762** This optimization is particularly useful at making VACUUM run faster.
126763*/
126764static int xferOptimization(
126765 Parse *pParse, /* Parser context */
126766 Table *pDest, /* The table we are inserting into */
126767 Select *pSelect, /* A SELECT statement to use as the data source */
126768 int onError, /* How to handle constraint errors */
126769 int iDbDest /* The database of pDest */
126770){
126771 sqlite3 *db = pParse->db;
126772 ExprList *pEList; /* The result set of the SELECT */
126773 Table *pSrc; /* The table in the FROM clause of SELECT */
126774 Index *pSrcIdx, *pDestIdx; /* Source and destination indices */
126775 SrcItem *pItem; /* An element of pSelect->pSrc */
126776 int i; /* Loop counter */
126777 int iDbSrc; /* The database of pSrc */
126778 int iSrc, iDest; /* Cursors from source and destination */
126779 int addr1, addr2; /* Loop addresses */
126780 int emptyDestTest = 0; /* Address of test for empty pDest */
126781 int emptySrcTest = 0; /* Address of test for empty pSrc */
126782 Vdbe *v; /* The VDBE we are building */
126783 int regAutoinc; /* Memory register used by AUTOINC */
126784 int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */
126785 int regData, regRowid; /* Registers holding data and rowid */
126786
126787 if( pSelect==0 ){
126788 return 0; /* Must be of the form INSERT INTO ... SELECT ... */
126789 }
126790 if( pParse->pWith || pSelect->pWith ){
126791 /* Do not attempt to process this query if there are an WITH clauses
126792 ** attached to it. Proceeding may generate a false "no such table: xxx"
126793 ** error if pSelect reads from a CTE named "xxx". */
126794 return 0;
126795 }
126796 if( sqlite3TriggerList(pParse, pDest) ){
126797 return 0; /* tab1 must not have triggers */
126798 }
126799#ifndef SQLITE_OMIT_VIRTUALTABLE
126800 if( IsVirtual(pDest) ){
126801 return 0; /* tab1 must not be a virtual table */
126802 }
126803#endif
126804 if( onError==OE_Default ){
126805 if( pDest->iPKey>=0 ) onError = pDest->keyConf;
126806 if( onError==OE_Default ) onError = OE_Abort;
126807 }
126808 assert(pSelect->pSrc); /* allocated even if there is no FROM clause */
126809 if( pSelect->pSrc->nSrc!=1 ){
126810 return 0; /* FROM clause must have exactly one term */
126811 }
126812 if( pSelect->pSrc->a[0].pSelect ){
126813 return 0; /* FROM clause cannot contain a subquery */
126814 }
126815 if( pSelect->pWhere ){
126816 return 0; /* SELECT may not have a WHERE clause */
126817 }
126818 if( pSelect->pOrderBy ){
126819 return 0; /* SELECT may not have an ORDER BY clause */
126820 }
126821 /* Do not need to test for a HAVING clause. If HAVING is present but
126822 ** there is no ORDER BY, we will get an error. */
126823 if( pSelect->pGroupBy ){
126824 return 0; /* SELECT may not have a GROUP BY clause */
126825 }
126826 if( pSelect->pLimit ){
126827 return 0; /* SELECT may not have a LIMIT clause */
126828 }
126829 if( pSelect->pPrior ){
126830 return 0; /* SELECT may not be a compound query */
126831 }
126832 if( pSelect->selFlags & SF_Distinct ){
126833 return 0; /* SELECT may not be DISTINCT */
126834 }
126835 pEList = pSelect->pEList;
126836 assert( pEList!=0 );
126837 if( pEList->nExpr!=1 ){
126838 return 0; /* The result set must have exactly one column */
126839 }
126840 assert( pEList->a[0].pExpr );
126841 if( pEList->a[0].pExpr->op!=TK_ASTERISK ){
126842 return 0; /* The result set must be the special operator "*" */
126843 }
126844
126845 /* At this point we have established that the statement is of the
126846 ** correct syntactic form to participate in this optimization. Now
126847 ** we have to check the semantics.
126848 */
126849 pItem = pSelect->pSrc->a;
126850 pSrc = sqlite3LocateTableItem(pParse, flags: 0, p: pItem);
126851 if( pSrc==0 ){
126852 return 0; /* FROM clause does not contain a real table */
126853 }
126854 if( pSrc->tnum==pDest->tnum && pSrc->pSchema==pDest->pSchema ){
126855 testcase( pSrc!=pDest ); /* Possible due to bad sqlite_schema.rootpage */
126856 return 0; /* tab1 and tab2 may not be the same table */
126857 }
126858 if( HasRowid(pDest)!=HasRowid(pSrc) ){
126859 return 0; /* source and destination must both be WITHOUT ROWID or not */
126860 }
126861 if( !IsOrdinaryTable(pSrc) ){
126862 return 0; /* tab2 may not be a view or virtual table */
126863 }
126864 if( pDest->nCol!=pSrc->nCol ){
126865 return 0; /* Number of columns must be the same in tab1 and tab2 */
126866 }
126867 if( pDest->iPKey!=pSrc->iPKey ){
126868 return 0; /* Both tables must have the same INTEGER PRIMARY KEY */
126869 }
126870 if( (pDest->tabFlags & TF_Strict)!=0 && (pSrc->tabFlags & TF_Strict)==0 ){
126871 return 0; /* Cannot feed from a non-strict into a strict table */
126872 }
126873 for(i=0; i<pDest->nCol; i++){
126874 Column *pDestCol = &pDest->aCol[i];
126875 Column *pSrcCol = &pSrc->aCol[i];
126876#ifdef SQLITE_ENABLE_HIDDEN_COLUMNS
126877 if( (db->mDbFlags & DBFLAG_Vacuum)==0
126878 && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN
126879 ){
126880 return 0; /* Neither table may have __hidden__ columns */
126881 }
126882#endif
126883#ifndef SQLITE_OMIT_GENERATED_COLUMNS
126884 /* Even if tables t1 and t2 have identical schemas, if they contain
126885 ** generated columns, then this statement is semantically incorrect:
126886 **
126887 ** INSERT INTO t2 SELECT * FROM t1;
126888 **
126889 ** The reason is that generated column values are returned by the
126890 ** the SELECT statement on the right but the INSERT statement on the
126891 ** left wants them to be omitted.
126892 **
126893 ** Nevertheless, this is a useful notational shorthand to tell SQLite
126894 ** to do a bulk transfer all of the content from t1 over to t2.
126895 **
126896 ** We could, in theory, disable this (except for internal use by the
126897 ** VACUUM command where it is actually needed). But why do that? It
126898 ** seems harmless enough, and provides a useful service.
126899 */
126900 if( (pDestCol->colFlags & COLFLAG_GENERATED) !=
126901 (pSrcCol->colFlags & COLFLAG_GENERATED) ){
126902 return 0; /* Both columns have the same generated-column type */
126903 }
126904 /* But the transfer is only allowed if both the source and destination
126905 ** tables have the exact same expressions for generated columns.
126906 ** This requirement could be relaxed for VIRTUAL columns, I suppose.
126907 */
126908 if( (pDestCol->colFlags & COLFLAG_GENERATED)!=0 ){
126909 if( sqlite3ExprCompare(pParse: 0,
126910 pA: sqlite3ColumnExpr(pTab: pSrc, pCol: pSrcCol),
126911 pB: sqlite3ColumnExpr(pTab: pDest, pCol: pDestCol), iTab: -1)!=0 ){
126912 testcase( pDestCol->colFlags & COLFLAG_VIRTUAL );
126913 testcase( pDestCol->colFlags & COLFLAG_STORED );
126914 return 0; /* Different generator expressions */
126915 }
126916 }
126917#endif
126918 if( pDestCol->affinity!=pSrcCol->affinity ){
126919 return 0; /* Affinity must be the same on all columns */
126920 }
126921 if( sqlite3_stricmp(zLeft: sqlite3ColumnColl(pCol: pDestCol),
126922 zRight: sqlite3ColumnColl(pCol: pSrcCol))!=0 ){
126923 return 0; /* Collating sequence must be the same on all columns */
126924 }
126925 if( pDestCol->notNull && !pSrcCol->notNull ){
126926 return 0; /* tab2 must be NOT NULL if tab1 is */
126927 }
126928 /* Default values for second and subsequent columns need to match. */
126929 if( (pDestCol->colFlags & COLFLAG_GENERATED)==0 && i>0 ){
126930 Expr *pDestExpr = sqlite3ColumnExpr(pTab: pDest, pCol: pDestCol);
126931 Expr *pSrcExpr = sqlite3ColumnExpr(pTab: pSrc, pCol: pSrcCol);
126932 assert( pDestExpr==0 || pDestExpr->op==TK_SPAN );
126933 assert( pDestExpr==0 || !ExprHasProperty(pDestExpr, EP_IntValue) );
126934 assert( pSrcExpr==0 || pSrcExpr->op==TK_SPAN );
126935 assert( pSrcExpr==0 || !ExprHasProperty(pSrcExpr, EP_IntValue) );
126936 if( (pDestExpr==0)!=(pSrcExpr==0)
126937 || (pDestExpr!=0 && strcmp(s1: pDestExpr->u.zToken,
126938 s2: pSrcExpr->u.zToken)!=0)
126939 ){
126940 return 0; /* Default values must be the same for all columns */
126941 }
126942 }
126943 }
126944 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
126945 if( IsUniqueIndex(pDestIdx) ){
126946 destHasUniqueIdx = 1;
126947 }
126948 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
126949 if( xferCompatibleIndex(pDest: pDestIdx, pSrc: pSrcIdx) ) break;
126950 }
126951 if( pSrcIdx==0 ){
126952 return 0; /* pDestIdx has no corresponding index in pSrc */
126953 }
126954 if( pSrcIdx->tnum==pDestIdx->tnum && pSrc->pSchema==pDest->pSchema
126955 && sqlite3FaultSim(iTest: 411)==SQLITE_OK ){
126956 /* The sqlite3FaultSim() call allows this corruption test to be
126957 ** bypassed during testing, in order to exercise other corruption tests
126958 ** further downstream. */
126959 return 0; /* Corrupt schema - two indexes on the same btree */
126960 }
126961 }
126962#ifndef SQLITE_OMIT_CHECK
126963 if( pDest->pCheck && sqlite3ExprListCompare(pA: pSrc->pCheck,pB: pDest->pCheck,iTab: -1) ){
126964 return 0; /* Tables have different CHECK constraints. Ticket #2252 */
126965 }
126966#endif
126967#ifndef SQLITE_OMIT_FOREIGN_KEY
126968 /* Disallow the transfer optimization if the destination table constains
126969 ** any foreign key constraints. This is more restrictive than necessary.
126970 ** But the main beneficiary of the transfer optimization is the VACUUM
126971 ** command, and the VACUUM command disables foreign key constraints. So
126972 ** the extra complication to make this rule less restrictive is probably
126973 ** not worth the effort. Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
126974 */
126975 assert( IsOrdinaryTable(pDest) );
126976 if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->u.tab.pFKey!=0 ){
126977 return 0;
126978 }
126979#endif
126980 if( (db->flags & SQLITE_CountRows)!=0 ){
126981 return 0; /* xfer opt does not play well with PRAGMA count_changes */
126982 }
126983
126984 /* If we get this far, it means that the xfer optimization is at
126985 ** least a possibility, though it might only work if the destination
126986 ** table (tab1) is initially empty.
126987 */
126988#ifdef SQLITE_TEST
126989 sqlite3_xferopt_count++;
126990#endif
126991 iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema);
126992 v = sqlite3GetVdbe(pParse);
126993 sqlite3CodeVerifySchema(pParse, iDb: iDbSrc);
126994 iSrc = pParse->nTab++;
126995 iDest = pParse->nTab++;
126996 regAutoinc = autoIncBegin(pParse, iDb: iDbDest, pTab: pDest);
126997 regData = sqlite3GetTempReg(pParse);
126998 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: regData);
126999 regRowid = sqlite3GetTempReg(pParse);
127000 sqlite3OpenTable(pParse, iCur: iDest, iDb: iDbDest, pTab: pDest, OP_OpenWrite);
127001 assert( HasRowid(pDest) || destHasUniqueIdx );
127002 if( (db->mDbFlags & DBFLAG_Vacuum)==0 && (
127003 (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */
127004 || destHasUniqueIdx /* (2) */
127005 || (onError!=OE_Abort && onError!=OE_Rollback) /* (3) */
127006 )){
127007 /* In some circumstances, we are able to run the xfer optimization
127008 ** only if the destination table is initially empty. Unless the
127009 ** DBFLAG_Vacuum flag is set, this block generates code to make
127010 ** that determination. If DBFLAG_Vacuum is set, then the destination
127011 ** table is always empty.
127012 **
127013 ** Conditions under which the destination must be empty:
127014 **
127015 ** (1) There is no INTEGER PRIMARY KEY but there are indices.
127016 ** (If the destination is not initially empty, the rowid fields
127017 ** of index entries might need to change.)
127018 **
127019 ** (2) The destination has a unique index. (The xfer optimization
127020 ** is unable to test uniqueness.)
127021 **
127022 ** (3) onError is something other than OE_Abort and OE_Rollback.
127023 */
127024 addr1 = sqlite3VdbeAddOp2(p: v, OP_Rewind, p1: iDest, p2: 0); VdbeCoverage(v);
127025 emptyDestTest = sqlite3VdbeAddOp0(p: v, OP_Goto);
127026 sqlite3VdbeJumpHere(p: v, addr: addr1);
127027 }
127028 if( HasRowid(pSrc) ){
127029 u8 insFlags;
127030 sqlite3OpenTable(pParse, iCur: iSrc, iDb: iDbSrc, pTab: pSrc, OP_OpenRead);
127031 emptySrcTest = sqlite3VdbeAddOp2(p: v, OP_Rewind, p1: iSrc, p2: 0); VdbeCoverage(v);
127032 if( pDest->iPKey>=0 ){
127033 addr1 = sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: iSrc, p2: regRowid);
127034 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
127035 sqlite3VdbeVerifyAbortable(v, onError);
127036 addr2 = sqlite3VdbeAddOp3(p: v, OP_NotExists, p1: iDest, p2: 0, p3: regRowid);
127037 VdbeCoverage(v);
127038 sqlite3RowidConstraint(pParse, onError, pTab: pDest);
127039 sqlite3VdbeJumpHere(p: v, addr: addr2);
127040 }
127041 autoIncStep(pParse, memId: regAutoinc, regRowid);
127042 }else if( pDest->pIndex==0 && !(db->mDbFlags & DBFLAG_VacuumInto) ){
127043 addr1 = sqlite3VdbeAddOp2(p: v, OP_NewRowid, p1: iDest, p2: regRowid);
127044 }else{
127045 addr1 = sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: iSrc, p2: regRowid);
127046 assert( (pDest->tabFlags & TF_Autoincrement)==0 );
127047 }
127048
127049 if( db->mDbFlags & DBFLAG_Vacuum ){
127050 sqlite3VdbeAddOp1(p: v, OP_SeekEnd, p1: iDest);
127051 insFlags = OPFLAG_APPEND|OPFLAG_USESEEKRESULT|OPFLAG_PREFORMAT;
127052 }else{
127053 insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND|OPFLAG_PREFORMAT;
127054 }
127055#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
127056 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
127057 sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
127058 insFlags &= ~OPFLAG_PREFORMAT;
127059 }else
127060#endif
127061 {
127062 sqlite3VdbeAddOp3(p: v, OP_RowCell, p1: iDest, p2: iSrc, p3: regRowid);
127063 }
127064 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: iDest, p2: regData, p3: regRowid);
127065 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
127066 sqlite3VdbeChangeP4(p: v, addr: -1, zP4: (char*)pDest, P4_TABLE);
127067 }
127068 sqlite3VdbeChangeP5(p: v, p5: insFlags);
127069
127070 sqlite3VdbeAddOp2(p: v, OP_Next, p1: iSrc, p2: addr1); VdbeCoverage(v);
127071 sqlite3VdbeAddOp2(p: v, OP_Close, p1: iSrc, p2: 0);
127072 sqlite3VdbeAddOp2(p: v, OP_Close, p1: iDest, p2: 0);
127073 }else{
127074 sqlite3TableLock(pParse, iDb: iDbDest, iTab: pDest->tnum, isWriteLock: 1, zName: pDest->zName);
127075 sqlite3TableLock(pParse, iDb: iDbSrc, iTab: pSrc->tnum, isWriteLock: 0, zName: pSrc->zName);
127076 }
127077 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
127078 u8 idxInsFlags = 0;
127079 for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
127080 if( xferCompatibleIndex(pDest: pDestIdx, pSrc: pSrcIdx) ) break;
127081 }
127082 assert( pSrcIdx );
127083 sqlite3VdbeAddOp3(p: v, OP_OpenRead, p1: iSrc, p2: pSrcIdx->tnum, p3: iDbSrc);
127084 sqlite3VdbeSetP4KeyInfo(pParse, pIdx: pSrcIdx);
127085 VdbeComment((v, "%s", pSrcIdx->zName));
127086 sqlite3VdbeAddOp3(p: v, OP_OpenWrite, p1: iDest, p2: pDestIdx->tnum, p3: iDbDest);
127087 sqlite3VdbeSetP4KeyInfo(pParse, pIdx: pDestIdx);
127088 sqlite3VdbeChangeP5(p: v, OPFLAG_BULKCSR);
127089 VdbeComment((v, "%s", pDestIdx->zName));
127090 addr1 = sqlite3VdbeAddOp2(p: v, OP_Rewind, p1: iSrc, p2: 0); VdbeCoverage(v);
127091 if( db->mDbFlags & DBFLAG_Vacuum ){
127092 /* This INSERT command is part of a VACUUM operation, which guarantees
127093 ** that the destination table is empty. If all indexed columns use
127094 ** collation sequence BINARY, then it can also be assumed that the
127095 ** index will be populated by inserting keys in strictly sorted
127096 ** order. In this case, instead of seeking within the b-tree as part
127097 ** of every OP_IdxInsert opcode, an OP_SeekEnd is added before the
127098 ** OP_IdxInsert to seek to the point within the b-tree where each key
127099 ** should be inserted. This is faster.
127100 **
127101 ** If any of the indexed columns use a collation sequence other than
127102 ** BINARY, this optimization is disabled. This is because the user
127103 ** might change the definition of a collation sequence and then run
127104 ** a VACUUM command. In that case keys may not be written in strictly
127105 ** sorted order. */
127106 for(i=0; i<pSrcIdx->nColumn; i++){
127107 const char *zColl = pSrcIdx->azColl[i];
127108 if( sqlite3_stricmp(zLeft: sqlite3StrBINARY, zRight: zColl) ) break;
127109 }
127110 if( i==pSrcIdx->nColumn ){
127111 idxInsFlags = OPFLAG_USESEEKRESULT|OPFLAG_PREFORMAT;
127112 sqlite3VdbeAddOp1(p: v, OP_SeekEnd, p1: iDest);
127113 sqlite3VdbeAddOp2(p: v, OP_RowCell, p1: iDest, p2: iSrc);
127114 }
127115 }else if( !HasRowid(pSrc) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
127116 idxInsFlags |= OPFLAG_NCHANGE;
127117 }
127118 if( idxInsFlags!=(OPFLAG_USESEEKRESULT|OPFLAG_PREFORMAT) ){
127119 sqlite3VdbeAddOp3(p: v, OP_RowData, p1: iSrc, p2: regData, p3: 1);
127120 if( (db->mDbFlags & DBFLAG_Vacuum)==0
127121 && !HasRowid(pDest)
127122 && IsPrimaryKeyIndex(pDestIdx)
127123 ){
127124 codeWithoutRowidPreupdate(pParse, pDest, iDest, regData);
127125 }
127126 }
127127 sqlite3VdbeAddOp2(p: v, OP_IdxInsert, p1: iDest, p2: regData);
127128 sqlite3VdbeChangeP5(p: v, p5: idxInsFlags|OPFLAG_APPEND);
127129 sqlite3VdbeAddOp2(p: v, OP_Next, p1: iSrc, p2: addr1+1); VdbeCoverage(v);
127130 sqlite3VdbeJumpHere(p: v, addr: addr1);
127131 sqlite3VdbeAddOp2(p: v, OP_Close, p1: iSrc, p2: 0);
127132 sqlite3VdbeAddOp2(p: v, OP_Close, p1: iDest, p2: 0);
127133 }
127134 if( emptySrcTest ) sqlite3VdbeJumpHere(p: v, addr: emptySrcTest);
127135 sqlite3ReleaseTempReg(pParse, iReg: regRowid);
127136 sqlite3ReleaseTempReg(pParse, iReg: regData);
127137 if( emptyDestTest ){
127138 sqlite3AutoincrementEnd(pParse);
127139 sqlite3VdbeAddOp2(p: v, OP_Halt, SQLITE_OK, p2: 0);
127140 sqlite3VdbeJumpHere(p: v, addr: emptyDestTest);
127141 sqlite3VdbeAddOp2(p: v, OP_Close, p1: iDest, p2: 0);
127142 return 0;
127143 }else{
127144 return 1;
127145 }
127146}
127147#endif /* SQLITE_OMIT_XFER_OPT */
127148
127149/************** End of insert.c **********************************************/
127150/************** Begin file legacy.c ******************************************/
127151/*
127152** 2001 September 15
127153**
127154** The author disclaims copyright to this source code. In place of
127155** a legal notice, here is a blessing:
127156**
127157** May you do good and not evil.
127158** May you find forgiveness for yourself and forgive others.
127159** May you share freely, never taking more than you give.
127160**
127161*************************************************************************
127162** Main file for the SQLite library. The routines in this file
127163** implement the programmer interface to the library. Routines in
127164** other files are for internal use by SQLite and should not be
127165** accessed by users of the library.
127166*/
127167
127168/* #include "sqliteInt.h" */
127169
127170/*
127171** Execute SQL code. Return one of the SQLITE_ success/failure
127172** codes. Also write an error message into memory obtained from
127173** malloc() and make *pzErrMsg point to that message.
127174**
127175** If the SQL is a query, then for each row in the query result
127176** the xCallback() function is called. pArg becomes the first
127177** argument to xCallback(). If xCallback=NULL then no callback
127178** is invoked, even for queries.
127179*/
127180SQLITE_API int sqlite3_exec(
127181 sqlite3 *db, /* The database on which the SQL executes */
127182 const char *zSql, /* The SQL to be executed */
127183 sqlite3_callback xCallback, /* Invoke this callback routine */
127184 void *pArg, /* First argument to xCallback() */
127185 char **pzErrMsg /* Write error messages here */
127186){
127187 int rc = SQLITE_OK; /* Return code */
127188 const char *zLeftover; /* Tail of unprocessed SQL */
127189 sqlite3_stmt *pStmt = 0; /* The current SQL statement */
127190 char **azCols = 0; /* Names of result columns */
127191 int callbackIsInit; /* True if callback data is initialized */
127192
127193 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
127194 if( zSql==0 ) zSql = "";
127195
127196 sqlite3_mutex_enter(p: db->mutex);
127197 sqlite3Error(db, SQLITE_OK);
127198 while( rc==SQLITE_OK && zSql[0] ){
127199 int nCol = 0;
127200 char **azVals = 0;
127201
127202 pStmt = 0;
127203 rc = sqlite3_prepare_v2(db, zSql, nByte: -1, ppStmt: &pStmt, pzTail: &zLeftover);
127204 assert( rc==SQLITE_OK || pStmt==0 );
127205 if( rc!=SQLITE_OK ){
127206 continue;
127207 }
127208 if( !pStmt ){
127209 /* this happens for a comment or white-space */
127210 zSql = zLeftover;
127211 continue;
127212 }
127213 callbackIsInit = 0;
127214
127215 while( 1 ){
127216 int i;
127217 rc = sqlite3_step(pStmt);
127218
127219 /* Invoke the callback function if required */
127220 if( xCallback && (SQLITE_ROW==rc ||
127221 (SQLITE_DONE==rc && !callbackIsInit
127222 && db->flags&SQLITE_NullCallback)) ){
127223 if( !callbackIsInit ){
127224 nCol = sqlite3_column_count(pStmt);
127225 azCols = sqlite3DbMallocRaw(db, n: (2*nCol+1)*sizeof(const char*));
127226 if( azCols==0 ){
127227 goto exec_out;
127228 }
127229 for(i=0; i<nCol; i++){
127230 azCols[i] = (char *)sqlite3_column_name(pStmt, N: i);
127231 /* sqlite3VdbeSetColName() installs column names as UTF8
127232 ** strings so there is no way for sqlite3_column_name() to fail. */
127233 assert( azCols[i]!=0 );
127234 }
127235 callbackIsInit = 1;
127236 }
127237 if( rc==SQLITE_ROW ){
127238 azVals = &azCols[nCol];
127239 for(i=0; i<nCol; i++){
127240 azVals[i] = (char *)sqlite3_column_text(pStmt, i);
127241 if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
127242 sqlite3OomFault(db);
127243 goto exec_out;
127244 }
127245 }
127246 azVals[i] = 0;
127247 }
127248 if( xCallback(pArg, nCol, azVals, azCols) ){
127249 /* EVIDENCE-OF: R-38229-40159 If the callback function to
127250 ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
127251 ** return SQLITE_ABORT. */
127252 rc = SQLITE_ABORT;
127253 sqlite3VdbeFinalize(p: (Vdbe *)pStmt);
127254 pStmt = 0;
127255 sqlite3Error(db, SQLITE_ABORT);
127256 goto exec_out;
127257 }
127258 }
127259
127260 if( rc!=SQLITE_ROW ){
127261 rc = sqlite3VdbeFinalize(p: (Vdbe *)pStmt);
127262 pStmt = 0;
127263 zSql = zLeftover;
127264 while( sqlite3Isspace(zSql[0]) ) zSql++;
127265 break;
127266 }
127267 }
127268
127269 sqlite3DbFree(db, p: azCols);
127270 azCols = 0;
127271 }
127272
127273exec_out:
127274 if( pStmt ) sqlite3VdbeFinalize(p: (Vdbe *)pStmt);
127275 sqlite3DbFree(db, p: azCols);
127276
127277 rc = sqlite3ApiExit(db, rc);
127278 if( rc!=SQLITE_OK && pzErrMsg ){
127279 *pzErrMsg = sqlite3DbStrDup(db: 0, z: sqlite3_errmsg(db));
127280 if( *pzErrMsg==0 ){
127281 rc = SQLITE_NOMEM_BKPT;
127282 sqlite3Error(db, SQLITE_NOMEM);
127283 }
127284 }else if( pzErrMsg ){
127285 *pzErrMsg = 0;
127286 }
127287
127288 assert( (rc&db->errMask)==rc );
127289 sqlite3_mutex_leave(p: db->mutex);
127290 return rc;
127291}
127292
127293/************** End of legacy.c **********************************************/
127294/************** Begin file loadext.c *****************************************/
127295/*
127296** 2006 June 7
127297**
127298** The author disclaims copyright to this source code. In place of
127299** a legal notice, here is a blessing:
127300**
127301** May you do good and not evil.
127302** May you find forgiveness for yourself and forgive others.
127303** May you share freely, never taking more than you give.
127304**
127305*************************************************************************
127306** This file contains code used to dynamically load extensions into
127307** the SQLite library.
127308*/
127309
127310#ifndef SQLITE_CORE
127311 #define SQLITE_CORE 1 /* Disable the API redefinition in sqlite3ext.h */
127312#endif
127313/************** Include sqlite3ext.h in the middle of loadext.c **************/
127314/************** Begin file sqlite3ext.h **************************************/
127315/*
127316** 2006 June 7
127317**
127318** The author disclaims copyright to this source code. In place of
127319** a legal notice, here is a blessing:
127320**
127321** May you do good and not evil.
127322** May you find forgiveness for yourself and forgive others.
127323** May you share freely, never taking more than you give.
127324**
127325*************************************************************************
127326** This header file defines the SQLite interface for use by
127327** shared libraries that want to be imported as extensions into
127328** an SQLite instance. Shared libraries that intend to be loaded
127329** as extensions by SQLite should #include this file instead of
127330** sqlite3.h.
127331*/
127332#ifndef SQLITE3EXT_H
127333#define SQLITE3EXT_H
127334/* #include "sqlite3.h" */
127335
127336/*
127337** The following structure holds pointers to all of the SQLite API
127338** routines.
127339**
127340** WARNING: In order to maintain backwards compatibility, add new
127341** interfaces to the end of this structure only. If you insert new
127342** interfaces in the middle of this structure, then older different
127343** versions of SQLite will not be able to load each other's shared
127344** libraries!
127345*/
127346struct sqlite3_api_routines {
127347 void * (*aggregate_context)(sqlite3_context*,int nBytes);
127348 int (*aggregate_count)(sqlite3_context*);
127349 int (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
127350 int (*bind_double)(sqlite3_stmt*,int,double);
127351 int (*bind_int)(sqlite3_stmt*,int,int);
127352 int (*bind_int64)(sqlite3_stmt*,int,sqlite_int64);
127353 int (*bind_null)(sqlite3_stmt*,int);
127354 int (*bind_parameter_count)(sqlite3_stmt*);
127355 int (*bind_parameter_index)(sqlite3_stmt*,const char*zName);
127356 const char * (*bind_parameter_name)(sqlite3_stmt*,int);
127357 int (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
127358 int (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
127359 int (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
127360 int (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
127361 int (*busy_timeout)(sqlite3*,int ms);
127362 int (*changes)(sqlite3*);
127363 int (*close)(sqlite3*);
127364 int (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,
127365 int eTextRep,const char*));
127366 int (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,
127367 int eTextRep,const void*));
127368 const void * (*column_blob)(sqlite3_stmt*,int iCol);
127369 int (*column_bytes)(sqlite3_stmt*,int iCol);
127370 int (*column_bytes16)(sqlite3_stmt*,int iCol);
127371 int (*column_count)(sqlite3_stmt*pStmt);
127372 const char * (*column_database_name)(sqlite3_stmt*,int);
127373 const void * (*column_database_name16)(sqlite3_stmt*,int);
127374 const char * (*column_decltype)(sqlite3_stmt*,int i);
127375 const void * (*column_decltype16)(sqlite3_stmt*,int);
127376 double (*column_double)(sqlite3_stmt*,int iCol);
127377 int (*column_int)(sqlite3_stmt*,int iCol);
127378 sqlite_int64 (*column_int64)(sqlite3_stmt*,int iCol);
127379 const char * (*column_name)(sqlite3_stmt*,int);
127380 const void * (*column_name16)(sqlite3_stmt*,int);
127381 const char * (*column_origin_name)(sqlite3_stmt*,int);
127382 const void * (*column_origin_name16)(sqlite3_stmt*,int);
127383 const char * (*column_table_name)(sqlite3_stmt*,int);
127384 const void * (*column_table_name16)(sqlite3_stmt*,int);
127385 const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
127386 const void * (*column_text16)(sqlite3_stmt*,int iCol);
127387 int (*column_type)(sqlite3_stmt*,int iCol);
127388 sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
127389 void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
127390 int (*complete)(const char*sql);
127391 int (*complete16)(const void*sql);
127392 int (*create_collation)(sqlite3*,const char*,int,void*,
127393 int(*)(void*,int,const void*,int,const void*));
127394 int (*create_collation16)(sqlite3*,const void*,int,void*,
127395 int(*)(void*,int,const void*,int,const void*));
127396 int (*create_function)(sqlite3*,const char*,int,int,void*,
127397 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
127398 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
127399 void (*xFinal)(sqlite3_context*));
127400 int (*create_function16)(sqlite3*,const void*,int,int,void*,
127401 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
127402 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
127403 void (*xFinal)(sqlite3_context*));
127404 int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
127405 int (*data_count)(sqlite3_stmt*pStmt);
127406 sqlite3 * (*db_handle)(sqlite3_stmt*);
127407 int (*declare_vtab)(sqlite3*,const char*);
127408 int (*enable_shared_cache)(int);
127409 int (*errcode)(sqlite3*db);
127410 const char * (*errmsg)(sqlite3*);
127411 const void * (*errmsg16)(sqlite3*);
127412 int (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**);
127413 int (*expired)(sqlite3_stmt*);
127414 int (*finalize)(sqlite3_stmt*pStmt);
127415 void (*free)(void*);
127416 void (*free_table)(char**result);
127417 int (*get_autocommit)(sqlite3*);
127418 void * (*get_auxdata)(sqlite3_context*,int);
127419 int (*get_table)(sqlite3*,const char*,char***,int*,int*,char**);
127420 int (*global_recover)(void);
127421 void (*interruptx)(sqlite3*);
127422 sqlite_int64 (*last_insert_rowid)(sqlite3*);
127423 const char * (*libversion)(void);
127424 int (*libversion_number)(void);
127425 void *(*malloc)(int);
127426 char * (*mprintf)(const char*,...);
127427 int (*open)(const char*,sqlite3**);
127428 int (*open16)(const void*,sqlite3**);
127429 int (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
127430 int (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
127431 void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*);
127432 void (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
127433 void *(*realloc)(void*,int);
127434 int (*reset)(sqlite3_stmt*pStmt);
127435 void (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
127436 void (*result_double)(sqlite3_context*,double);
127437 void (*result_error)(sqlite3_context*,const char*,int);
127438 void (*result_error16)(sqlite3_context*,const void*,int);
127439 void (*result_int)(sqlite3_context*,int);
127440 void (*result_int64)(sqlite3_context*,sqlite_int64);
127441 void (*result_null)(sqlite3_context*);
127442 void (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
127443 void (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
127444 void (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
127445 void (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
127446 void (*result_value)(sqlite3_context*,sqlite3_value*);
127447 void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
127448 int (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,
127449 const char*,const char*),void*);
127450 void (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
127451 char * (*xsnprintf)(int,char*,const char*,...);
127452 int (*step)(sqlite3_stmt*);
127453 int (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,
127454 char const**,char const**,int*,int*,int*);
127455 void (*thread_cleanup)(void);
127456 int (*total_changes)(sqlite3*);
127457 void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
127458 int (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
127459 void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,
127460 sqlite_int64),void*);
127461 void * (*user_data)(sqlite3_context*);
127462 const void * (*value_blob)(sqlite3_value*);
127463 int (*value_bytes)(sqlite3_value*);
127464 int (*value_bytes16)(sqlite3_value*);
127465 double (*value_double)(sqlite3_value*);
127466 int (*value_int)(sqlite3_value*);
127467 sqlite_int64 (*value_int64)(sqlite3_value*);
127468 int (*value_numeric_type)(sqlite3_value*);
127469 const unsigned char * (*value_text)(sqlite3_value*);
127470 const void * (*value_text16)(sqlite3_value*);
127471 const void * (*value_text16be)(sqlite3_value*);
127472 const void * (*value_text16le)(sqlite3_value*);
127473 int (*value_type)(sqlite3_value*);
127474 char *(*vmprintf)(const char*,va_list);
127475 /* Added ??? */
127476 int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
127477 /* Added by 3.3.13 */
127478 int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
127479 int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
127480 int (*clear_bindings)(sqlite3_stmt*);
127481 /* Added by 3.4.1 */
127482 int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,
127483 void (*xDestroy)(void *));
127484 /* Added by 3.5.0 */
127485 int (*bind_zeroblob)(sqlite3_stmt*,int,int);
127486 int (*blob_bytes)(sqlite3_blob*);
127487 int (*blob_close)(sqlite3_blob*);
127488 int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,
127489 int,sqlite3_blob**);
127490 int (*blob_read)(sqlite3_blob*,void*,int,int);
127491 int (*blob_write)(sqlite3_blob*,const void*,int,int);
127492 int (*create_collation_v2)(sqlite3*,const char*,int,void*,
127493 int(*)(void*,int,const void*,int,const void*),
127494 void(*)(void*));
127495 int (*file_control)(sqlite3*,const char*,int,void*);
127496 sqlite3_int64 (*memory_highwater)(int);
127497 sqlite3_int64 (*memory_used)(void);
127498 sqlite3_mutex *(*mutex_alloc)(int);
127499 void (*mutex_enter)(sqlite3_mutex*);
127500 void (*mutex_free)(sqlite3_mutex*);
127501 void (*mutex_leave)(sqlite3_mutex*);
127502 int (*mutex_try)(sqlite3_mutex*);
127503 int (*open_v2)(const char*,sqlite3**,int,const char*);
127504 int (*release_memory)(int);
127505 void (*result_error_nomem)(sqlite3_context*);
127506 void (*result_error_toobig)(sqlite3_context*);
127507 int (*sleep)(int);
127508 void (*soft_heap_limit)(int);
127509 sqlite3_vfs *(*vfs_find)(const char*);
127510 int (*vfs_register)(sqlite3_vfs*,int);
127511 int (*vfs_unregister)(sqlite3_vfs*);
127512 int (*xthreadsafe)(void);
127513 void (*result_zeroblob)(sqlite3_context*,int);
127514 void (*result_error_code)(sqlite3_context*,int);
127515 int (*test_control)(int, ...);
127516 void (*randomness)(int,void*);
127517 sqlite3 *(*context_db_handle)(sqlite3_context*);
127518 int (*extended_result_codes)(sqlite3*,int);
127519 int (*limit)(sqlite3*,int,int);
127520 sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*);
127521 const char *(*sql)(sqlite3_stmt*);
127522 int (*status)(int,int*,int*,int);
127523 int (*backup_finish)(sqlite3_backup*);
127524 sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*);
127525 int (*backup_pagecount)(sqlite3_backup*);
127526 int (*backup_remaining)(sqlite3_backup*);
127527 int (*backup_step)(sqlite3_backup*,int);
127528 const char *(*compileoption_get)(int);
127529 int (*compileoption_used)(const char*);
127530 int (*create_function_v2)(sqlite3*,const char*,int,int,void*,
127531 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
127532 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
127533 void (*xFinal)(sqlite3_context*),
127534 void(*xDestroy)(void*));
127535 int (*db_config)(sqlite3*,int,...);
127536 sqlite3_mutex *(*db_mutex)(sqlite3*);
127537 int (*db_status)(sqlite3*,int,int*,int*,int);
127538 int (*extended_errcode)(sqlite3*);
127539 void (*log)(int,const char*,...);
127540 sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64);
127541 const char *(*sourceid)(void);
127542 int (*stmt_status)(sqlite3_stmt*,int,int);
127543 int (*strnicmp)(const char*,const char*,int);
127544 int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*);
127545 int (*wal_autocheckpoint)(sqlite3*,int);
127546 int (*wal_checkpoint)(sqlite3*,const char*);
127547 void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*);
127548 int (*blob_reopen)(sqlite3_blob*,sqlite3_int64);
127549 int (*vtab_config)(sqlite3*,int op,...);
127550 int (*vtab_on_conflict)(sqlite3*);
127551 /* Version 3.7.16 and later */
127552 int (*close_v2)(sqlite3*);
127553 const char *(*db_filename)(sqlite3*,const char*);
127554 int (*db_readonly)(sqlite3*,const char*);
127555 int (*db_release_memory)(sqlite3*);
127556 const char *(*errstr)(int);
127557 int (*stmt_busy)(sqlite3_stmt*);
127558 int (*stmt_readonly)(sqlite3_stmt*);
127559 int (*stricmp)(const char*,const char*);
127560 int (*uri_boolean)(const char*,const char*,int);
127561 sqlite3_int64 (*uri_int64)(const char*,const char*,sqlite3_int64);
127562 const char *(*uri_parameter)(const char*,const char*);
127563 char *(*xvsnprintf)(int,char*,const char*,va_list);
127564 int (*wal_checkpoint_v2)(sqlite3*,const char*,int,int*,int*);
127565 /* Version 3.8.7 and later */
127566 int (*auto_extension)(void(*)(void));
127567 int (*bind_blob64)(sqlite3_stmt*,int,const void*,sqlite3_uint64,
127568 void(*)(void*));
127569 int (*bind_text64)(sqlite3_stmt*,int,const char*,sqlite3_uint64,
127570 void(*)(void*),unsigned char);
127571 int (*cancel_auto_extension)(void(*)(void));
127572 int (*load_extension)(sqlite3*,const char*,const char*,char**);
127573 void *(*malloc64)(sqlite3_uint64);
127574 sqlite3_uint64 (*msize)(void*);
127575 void *(*realloc64)(void*,sqlite3_uint64);
127576 void (*reset_auto_extension)(void);
127577 void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64,
127578 void(*)(void*));
127579 void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64,
127580 void(*)(void*), unsigned char);
127581 int (*strglob)(const char*,const char*);
127582 /* Version 3.8.11 and later */
127583 sqlite3_value *(*value_dup)(const sqlite3_value*);
127584 void (*value_free)(sqlite3_value*);
127585 int (*result_zeroblob64)(sqlite3_context*,sqlite3_uint64);
127586 int (*bind_zeroblob64)(sqlite3_stmt*, int, sqlite3_uint64);
127587 /* Version 3.9.0 and later */
127588 unsigned int (*value_subtype)(sqlite3_value*);
127589 void (*result_subtype)(sqlite3_context*,unsigned int);
127590 /* Version 3.10.0 and later */
127591 int (*status64)(int,sqlite3_int64*,sqlite3_int64*,int);
127592 int (*strlike)(const char*,const char*,unsigned int);
127593 int (*db_cacheflush)(sqlite3*);
127594 /* Version 3.12.0 and later */
127595 int (*system_errno)(sqlite3*);
127596 /* Version 3.14.0 and later */
127597 int (*trace_v2)(sqlite3*,unsigned,int(*)(unsigned,void*,void*,void*),void*);
127598 char *(*expanded_sql)(sqlite3_stmt*);
127599 /* Version 3.18.0 and later */
127600 void (*set_last_insert_rowid)(sqlite3*,sqlite3_int64);
127601 /* Version 3.20.0 and later */
127602 int (*prepare_v3)(sqlite3*,const char*,int,unsigned int,
127603 sqlite3_stmt**,const char**);
127604 int (*prepare16_v3)(sqlite3*,const void*,int,unsigned int,
127605 sqlite3_stmt**,const void**);
127606 int (*bind_pointer)(sqlite3_stmt*,int,void*,const char*,void(*)(void*));
127607 void (*result_pointer)(sqlite3_context*,void*,const char*,void(*)(void*));
127608 void *(*value_pointer)(sqlite3_value*,const char*);
127609 int (*vtab_nochange)(sqlite3_context*);
127610 int (*value_nochange)(sqlite3_value*);
127611 const char *(*vtab_collation)(sqlite3_index_info*,int);
127612 /* Version 3.24.0 and later */
127613 int (*keyword_count)(void);
127614 int (*keyword_name)(int,const char**,int*);
127615 int (*keyword_check)(const char*,int);
127616 sqlite3_str *(*str_new)(sqlite3*);
127617 char *(*str_finish)(sqlite3_str*);
127618 void (*str_appendf)(sqlite3_str*, const char *zFormat, ...);
127619 void (*str_vappendf)(sqlite3_str*, const char *zFormat, va_list);
127620 void (*str_append)(sqlite3_str*, const char *zIn, int N);
127621 void (*str_appendall)(sqlite3_str*, const char *zIn);
127622 void (*str_appendchar)(sqlite3_str*, int N, char C);
127623 void (*str_reset)(sqlite3_str*);
127624 int (*str_errcode)(sqlite3_str*);
127625 int (*str_length)(sqlite3_str*);
127626 char *(*str_value)(sqlite3_str*);
127627 /* Version 3.25.0 and later */
127628 int (*create_window_function)(sqlite3*,const char*,int,int,void*,
127629 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
127630 void (*xFinal)(sqlite3_context*),
127631 void (*xValue)(sqlite3_context*),
127632 void (*xInv)(sqlite3_context*,int,sqlite3_value**),
127633 void(*xDestroy)(void*));
127634 /* Version 3.26.0 and later */
127635 const char *(*normalized_sql)(sqlite3_stmt*);
127636 /* Version 3.28.0 and later */
127637 int (*stmt_isexplain)(sqlite3_stmt*);
127638 int (*value_frombind)(sqlite3_value*);
127639 /* Version 3.30.0 and later */
127640 int (*drop_modules)(sqlite3*,const char**);
127641 /* Version 3.31.0 and later */
127642 sqlite3_int64 (*hard_heap_limit64)(sqlite3_int64);
127643 const char *(*uri_key)(const char*,int);
127644 const char *(*filename_database)(const char*);
127645 const char *(*filename_journal)(const char*);
127646 const char *(*filename_wal)(const char*);
127647 /* Version 3.32.0 and later */
127648 char *(*create_filename)(const char*,const char*,const char*,
127649 int,const char**);
127650 void (*free_filename)(char*);
127651 sqlite3_file *(*database_file_object)(const char*);
127652 /* Version 3.34.0 and later */
127653 int (*txn_state)(sqlite3*,const char*);
127654 /* Version 3.36.1 and later */
127655 sqlite3_int64 (*changes64)(sqlite3*);
127656 sqlite3_int64 (*total_changes64)(sqlite3*);
127657 /* Version 3.37.0 and later */
127658 int (*autovacuum_pages)(sqlite3*,
127659 unsigned int(*)(void*,const char*,unsigned int,unsigned int,unsigned int),
127660 void*, void(*)(void*));
127661};
127662
127663/*
127664** This is the function signature used for all extension entry points. It
127665** is also defined in the file "loadext.c".
127666*/
127667typedef int (*sqlite3_loadext_entry)(
127668 sqlite3 *db, /* Handle to the database. */
127669 char **pzErrMsg, /* Used to set error string on failure. */
127670 const sqlite3_api_routines *pThunk /* Extension API function pointers. */
127671);
127672
127673/*
127674** The following macros redefine the API routines so that they are
127675** redirected through the global sqlite3_api structure.
127676**
127677** This header file is also used by the loadext.c source file
127678** (part of the main SQLite library - not an extension) so that
127679** it can get access to the sqlite3_api_routines structure
127680** definition. But the main library does not want to redefine
127681** the API. So the redefinition macros are only valid if the
127682** SQLITE_CORE macros is undefined.
127683*/
127684#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
127685#define sqlite3_aggregate_context sqlite3_api->aggregate_context
127686#ifndef SQLITE_OMIT_DEPRECATED
127687#define sqlite3_aggregate_count sqlite3_api->aggregate_count
127688#endif
127689#define sqlite3_bind_blob sqlite3_api->bind_blob
127690#define sqlite3_bind_double sqlite3_api->bind_double
127691#define sqlite3_bind_int sqlite3_api->bind_int
127692#define sqlite3_bind_int64 sqlite3_api->bind_int64
127693#define sqlite3_bind_null sqlite3_api->bind_null
127694#define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count
127695#define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index
127696#define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name
127697#define sqlite3_bind_text sqlite3_api->bind_text
127698#define sqlite3_bind_text16 sqlite3_api->bind_text16
127699#define sqlite3_bind_value sqlite3_api->bind_value
127700#define sqlite3_busy_handler sqlite3_api->busy_handler
127701#define sqlite3_busy_timeout sqlite3_api->busy_timeout
127702#define sqlite3_changes sqlite3_api->changes
127703#define sqlite3_close sqlite3_api->close
127704#define sqlite3_collation_needed sqlite3_api->collation_needed
127705#define sqlite3_collation_needed16 sqlite3_api->collation_needed16
127706#define sqlite3_column_blob sqlite3_api->column_blob
127707#define sqlite3_column_bytes sqlite3_api->column_bytes
127708#define sqlite3_column_bytes16 sqlite3_api->column_bytes16
127709#define sqlite3_column_count sqlite3_api->column_count
127710#define sqlite3_column_database_name sqlite3_api->column_database_name
127711#define sqlite3_column_database_name16 sqlite3_api->column_database_name16
127712#define sqlite3_column_decltype sqlite3_api->column_decltype
127713#define sqlite3_column_decltype16 sqlite3_api->column_decltype16
127714#define sqlite3_column_double sqlite3_api->column_double
127715#define sqlite3_column_int sqlite3_api->column_int
127716#define sqlite3_column_int64 sqlite3_api->column_int64
127717#define sqlite3_column_name sqlite3_api->column_name
127718#define sqlite3_column_name16 sqlite3_api->column_name16
127719#define sqlite3_column_origin_name sqlite3_api->column_origin_name
127720#define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16
127721#define sqlite3_column_table_name sqlite3_api->column_table_name
127722#define sqlite3_column_table_name16 sqlite3_api->column_table_name16
127723#define sqlite3_column_text sqlite3_api->column_text
127724#define sqlite3_column_text16 sqlite3_api->column_text16
127725#define sqlite3_column_type sqlite3_api->column_type
127726#define sqlite3_column_value sqlite3_api->column_value
127727#define sqlite3_commit_hook sqlite3_api->commit_hook
127728#define sqlite3_complete sqlite3_api->complete
127729#define sqlite3_complete16 sqlite3_api->complete16
127730#define sqlite3_create_collation sqlite3_api->create_collation
127731#define sqlite3_create_collation16 sqlite3_api->create_collation16
127732#define sqlite3_create_function sqlite3_api->create_function
127733#define sqlite3_create_function16 sqlite3_api->create_function16
127734#define sqlite3_create_module sqlite3_api->create_module
127735#define sqlite3_create_module_v2 sqlite3_api->create_module_v2
127736#define sqlite3_data_count sqlite3_api->data_count
127737#define sqlite3_db_handle sqlite3_api->db_handle
127738#define sqlite3_declare_vtab sqlite3_api->declare_vtab
127739#define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache
127740#define sqlite3_errcode sqlite3_api->errcode
127741#define sqlite3_errmsg sqlite3_api->errmsg
127742#define sqlite3_errmsg16 sqlite3_api->errmsg16
127743#define sqlite3_exec sqlite3_api->exec
127744#ifndef SQLITE_OMIT_DEPRECATED
127745#define sqlite3_expired sqlite3_api->expired
127746#endif
127747#define sqlite3_finalize sqlite3_api->finalize
127748#define sqlite3_free sqlite3_api->free
127749#define sqlite3_free_table sqlite3_api->free_table
127750#define sqlite3_get_autocommit sqlite3_api->get_autocommit
127751#define sqlite3_get_auxdata sqlite3_api->get_auxdata
127752#define sqlite3_get_table sqlite3_api->get_table
127753#ifndef SQLITE_OMIT_DEPRECATED
127754#define sqlite3_global_recover sqlite3_api->global_recover
127755#endif
127756#define sqlite3_interrupt sqlite3_api->interruptx
127757#define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid
127758#define sqlite3_libversion sqlite3_api->libversion
127759#define sqlite3_libversion_number sqlite3_api->libversion_number
127760#define sqlite3_malloc sqlite3_api->malloc
127761#define sqlite3_mprintf sqlite3_api->mprintf
127762#define sqlite3_open sqlite3_api->open
127763#define sqlite3_open16 sqlite3_api->open16
127764#define sqlite3_prepare sqlite3_api->prepare
127765#define sqlite3_prepare16 sqlite3_api->prepare16
127766#define sqlite3_prepare_v2 sqlite3_api->prepare_v2
127767#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
127768#define sqlite3_profile sqlite3_api->profile
127769#define sqlite3_progress_handler sqlite3_api->progress_handler
127770#define sqlite3_realloc sqlite3_api->realloc
127771#define sqlite3_reset sqlite3_api->reset
127772#define sqlite3_result_blob sqlite3_api->result_blob
127773#define sqlite3_result_double sqlite3_api->result_double
127774#define sqlite3_result_error sqlite3_api->result_error
127775#define sqlite3_result_error16 sqlite3_api->result_error16
127776#define sqlite3_result_int sqlite3_api->result_int
127777#define sqlite3_result_int64 sqlite3_api->result_int64
127778#define sqlite3_result_null sqlite3_api->result_null
127779#define sqlite3_result_text sqlite3_api->result_text
127780#define sqlite3_result_text16 sqlite3_api->result_text16
127781#define sqlite3_result_text16be sqlite3_api->result_text16be
127782#define sqlite3_result_text16le sqlite3_api->result_text16le
127783#define sqlite3_result_value sqlite3_api->result_value
127784#define sqlite3_rollback_hook sqlite3_api->rollback_hook
127785#define sqlite3_set_authorizer sqlite3_api->set_authorizer
127786#define sqlite3_set_auxdata sqlite3_api->set_auxdata
127787#define sqlite3_snprintf sqlite3_api->xsnprintf
127788#define sqlite3_step sqlite3_api->step
127789#define sqlite3_table_column_metadata sqlite3_api->table_column_metadata
127790#define sqlite3_thread_cleanup sqlite3_api->thread_cleanup
127791#define sqlite3_total_changes sqlite3_api->total_changes
127792#define sqlite3_trace sqlite3_api->trace
127793#ifndef SQLITE_OMIT_DEPRECATED
127794#define sqlite3_transfer_bindings sqlite3_api->transfer_bindings
127795#endif
127796#define sqlite3_update_hook sqlite3_api->update_hook
127797#define sqlite3_user_data sqlite3_api->user_data
127798#define sqlite3_value_blob sqlite3_api->value_blob
127799#define sqlite3_value_bytes sqlite3_api->value_bytes
127800#define sqlite3_value_bytes16 sqlite3_api->value_bytes16
127801#define sqlite3_value_double sqlite3_api->value_double
127802#define sqlite3_value_int sqlite3_api->value_int
127803#define sqlite3_value_int64 sqlite3_api->value_int64
127804#define sqlite3_value_numeric_type sqlite3_api->value_numeric_type
127805#define sqlite3_value_text sqlite3_api->value_text
127806#define sqlite3_value_text16 sqlite3_api->value_text16
127807#define sqlite3_value_text16be sqlite3_api->value_text16be
127808#define sqlite3_value_text16le sqlite3_api->value_text16le
127809#define sqlite3_value_type sqlite3_api->value_type
127810#define sqlite3_vmprintf sqlite3_api->vmprintf
127811#define sqlite3_vsnprintf sqlite3_api->xvsnprintf
127812#define sqlite3_overload_function sqlite3_api->overload_function
127813#define sqlite3_prepare_v2 sqlite3_api->prepare_v2
127814#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
127815#define sqlite3_clear_bindings sqlite3_api->clear_bindings
127816#define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob
127817#define sqlite3_blob_bytes sqlite3_api->blob_bytes
127818#define sqlite3_blob_close sqlite3_api->blob_close
127819#define sqlite3_blob_open sqlite3_api->blob_open
127820#define sqlite3_blob_read sqlite3_api->blob_read
127821#define sqlite3_blob_write sqlite3_api->blob_write
127822#define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2
127823#define sqlite3_file_control sqlite3_api->file_control
127824#define sqlite3_memory_highwater sqlite3_api->memory_highwater
127825#define sqlite3_memory_used sqlite3_api->memory_used
127826#define sqlite3_mutex_alloc sqlite3_api->mutex_alloc
127827#define sqlite3_mutex_enter sqlite3_api->mutex_enter
127828#define sqlite3_mutex_free sqlite3_api->mutex_free
127829#define sqlite3_mutex_leave sqlite3_api->mutex_leave
127830#define sqlite3_mutex_try sqlite3_api->mutex_try
127831#define sqlite3_open_v2 sqlite3_api->open_v2
127832#define sqlite3_release_memory sqlite3_api->release_memory
127833#define sqlite3_result_error_nomem sqlite3_api->result_error_nomem
127834#define sqlite3_result_error_toobig sqlite3_api->result_error_toobig
127835#define sqlite3_sleep sqlite3_api->sleep
127836#define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit
127837#define sqlite3_vfs_find sqlite3_api->vfs_find
127838#define sqlite3_vfs_register sqlite3_api->vfs_register
127839#define sqlite3_vfs_unregister sqlite3_api->vfs_unregister
127840#define sqlite3_threadsafe sqlite3_api->xthreadsafe
127841#define sqlite3_result_zeroblob sqlite3_api->result_zeroblob
127842#define sqlite3_result_error_code sqlite3_api->result_error_code
127843#define sqlite3_test_control sqlite3_api->test_control
127844#define sqlite3_randomness sqlite3_api->randomness
127845#define sqlite3_context_db_handle sqlite3_api->context_db_handle
127846#define sqlite3_extended_result_codes sqlite3_api->extended_result_codes
127847#define sqlite3_limit sqlite3_api->limit
127848#define sqlite3_next_stmt sqlite3_api->next_stmt
127849#define sqlite3_sql sqlite3_api->sql
127850#define sqlite3_status sqlite3_api->status
127851#define sqlite3_backup_finish sqlite3_api->backup_finish
127852#define sqlite3_backup_init sqlite3_api->backup_init
127853#define sqlite3_backup_pagecount sqlite3_api->backup_pagecount
127854#define sqlite3_backup_remaining sqlite3_api->backup_remaining
127855#define sqlite3_backup_step sqlite3_api->backup_step
127856#define sqlite3_compileoption_get sqlite3_api->compileoption_get
127857#define sqlite3_compileoption_used sqlite3_api->compileoption_used
127858#define sqlite3_create_function_v2 sqlite3_api->create_function_v2
127859#define sqlite3_db_config sqlite3_api->db_config
127860#define sqlite3_db_mutex sqlite3_api->db_mutex
127861#define sqlite3_db_status sqlite3_api->db_status
127862#define sqlite3_extended_errcode sqlite3_api->extended_errcode
127863#define sqlite3_log sqlite3_api->log
127864#define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64
127865#define sqlite3_sourceid sqlite3_api->sourceid
127866#define sqlite3_stmt_status sqlite3_api->stmt_status
127867#define sqlite3_strnicmp sqlite3_api->strnicmp
127868#define sqlite3_unlock_notify sqlite3_api->unlock_notify
127869#define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint
127870#define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint
127871#define sqlite3_wal_hook sqlite3_api->wal_hook
127872#define sqlite3_blob_reopen sqlite3_api->blob_reopen
127873#define sqlite3_vtab_config sqlite3_api->vtab_config
127874#define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict
127875/* Version 3.7.16 and later */
127876#define sqlite3_close_v2 sqlite3_api->close_v2
127877#define sqlite3_db_filename sqlite3_api->db_filename
127878#define sqlite3_db_readonly sqlite3_api->db_readonly
127879#define sqlite3_db_release_memory sqlite3_api->db_release_memory
127880#define sqlite3_errstr sqlite3_api->errstr
127881#define sqlite3_stmt_busy sqlite3_api->stmt_busy
127882#define sqlite3_stmt_readonly sqlite3_api->stmt_readonly
127883#define sqlite3_stricmp sqlite3_api->stricmp
127884#define sqlite3_uri_boolean sqlite3_api->uri_boolean
127885#define sqlite3_uri_int64 sqlite3_api->uri_int64
127886#define sqlite3_uri_parameter sqlite3_api->uri_parameter
127887#define sqlite3_uri_vsnprintf sqlite3_api->xvsnprintf
127888#define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2
127889/* Version 3.8.7 and later */
127890#define sqlite3_auto_extension sqlite3_api->auto_extension
127891#define sqlite3_bind_blob64 sqlite3_api->bind_blob64
127892#define sqlite3_bind_text64 sqlite3_api->bind_text64
127893#define sqlite3_cancel_auto_extension sqlite3_api->cancel_auto_extension
127894#define sqlite3_load_extension sqlite3_api->load_extension
127895#define sqlite3_malloc64 sqlite3_api->malloc64
127896#define sqlite3_msize sqlite3_api->msize
127897#define sqlite3_realloc64 sqlite3_api->realloc64
127898#define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension
127899#define sqlite3_result_blob64 sqlite3_api->result_blob64
127900#define sqlite3_result_text64 sqlite3_api->result_text64
127901#define sqlite3_strglob sqlite3_api->strglob
127902/* Version 3.8.11 and later */
127903#define sqlite3_value_dup sqlite3_api->value_dup
127904#define sqlite3_value_free sqlite3_api->value_free
127905#define sqlite3_result_zeroblob64 sqlite3_api->result_zeroblob64
127906#define sqlite3_bind_zeroblob64 sqlite3_api->bind_zeroblob64
127907/* Version 3.9.0 and later */
127908#define sqlite3_value_subtype sqlite3_api->value_subtype
127909#define sqlite3_result_subtype sqlite3_api->result_subtype
127910/* Version 3.10.0 and later */
127911#define sqlite3_status64 sqlite3_api->status64
127912#define sqlite3_strlike sqlite3_api->strlike
127913#define sqlite3_db_cacheflush sqlite3_api->db_cacheflush
127914/* Version 3.12.0 and later */
127915#define sqlite3_system_errno sqlite3_api->system_errno
127916/* Version 3.14.0 and later */
127917#define sqlite3_trace_v2 sqlite3_api->trace_v2
127918#define sqlite3_expanded_sql sqlite3_api->expanded_sql
127919/* Version 3.18.0 and later */
127920#define sqlite3_set_last_insert_rowid sqlite3_api->set_last_insert_rowid
127921/* Version 3.20.0 and later */
127922#define sqlite3_prepare_v3 sqlite3_api->prepare_v3
127923#define sqlite3_prepare16_v3 sqlite3_api->prepare16_v3
127924#define sqlite3_bind_pointer sqlite3_api->bind_pointer
127925#define sqlite3_result_pointer sqlite3_api->result_pointer
127926#define sqlite3_value_pointer sqlite3_api->value_pointer
127927/* Version 3.22.0 and later */
127928#define sqlite3_vtab_nochange sqlite3_api->vtab_nochange
127929#define sqlite3_value_nochange sqlite3_api->value_nochange
127930#define sqlite3_vtab_collation sqlite3_api->vtab_collation
127931/* Version 3.24.0 and later */
127932#define sqlite3_keyword_count sqlite3_api->keyword_count
127933#define sqlite3_keyword_name sqlite3_api->keyword_name
127934#define sqlite3_keyword_check sqlite3_api->keyword_check
127935#define sqlite3_str_new sqlite3_api->str_new
127936#define sqlite3_str_finish sqlite3_api->str_finish
127937#define sqlite3_str_appendf sqlite3_api->str_appendf
127938#define sqlite3_str_vappendf sqlite3_api->str_vappendf
127939#define sqlite3_str_append sqlite3_api->str_append
127940#define sqlite3_str_appendall sqlite3_api->str_appendall
127941#define sqlite3_str_appendchar sqlite3_api->str_appendchar
127942#define sqlite3_str_reset sqlite3_api->str_reset
127943#define sqlite3_str_errcode sqlite3_api->str_errcode
127944#define sqlite3_str_length sqlite3_api->str_length
127945#define sqlite3_str_value sqlite3_api->str_value
127946/* Version 3.25.0 and later */
127947#define sqlite3_create_window_function sqlite3_api->create_window_function
127948/* Version 3.26.0 and later */
127949#define sqlite3_normalized_sql sqlite3_api->normalized_sql
127950/* Version 3.28.0 and later */
127951#define sqlite3_stmt_isexplain sqlite3_api->stmt_isexplain
127952#define sqlite3_value_frombind sqlite3_api->value_frombind
127953/* Version 3.30.0 and later */
127954#define sqlite3_drop_modules sqlite3_api->drop_modules
127955/* Version 3.31.0 and later */
127956#define sqlite3_hard_heap_limit64 sqlite3_api->hard_heap_limit64
127957#define sqlite3_uri_key sqlite3_api->uri_key
127958#define sqlite3_filename_database sqlite3_api->filename_database
127959#define sqlite3_filename_journal sqlite3_api->filename_journal
127960#define sqlite3_filename_wal sqlite3_api->filename_wal
127961/* Version 3.32.0 and later */
127962#define sqlite3_create_filename sqlite3_api->create_filename
127963#define sqlite3_free_filename sqlite3_api->free_filename
127964#define sqlite3_database_file_object sqlite3_api->database_file_object
127965/* Version 3.34.0 and later */
127966#define sqlite3_txn_state sqlite3_api->txn_state
127967/* Version 3.36.1 and later */
127968#define sqlite3_changes64 sqlite3_api->changes64
127969#define sqlite3_total_changes64 sqlite3_api->total_changes64
127970/* Version 3.37.0 and later */
127971#define sqlite3_autovacuum_pages sqlite3_api->autovacuum_pages
127972#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
127973
127974#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
127975 /* This case when the file really is being compiled as a loadable
127976 ** extension */
127977# define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api=0;
127978# define SQLITE_EXTENSION_INIT2(v) sqlite3_api=v;
127979# define SQLITE_EXTENSION_INIT3 \
127980 extern const sqlite3_api_routines *sqlite3_api;
127981#else
127982 /* This case when the file is being statically linked into the
127983 ** application */
127984# define SQLITE_EXTENSION_INIT1 /*no-op*/
127985# define SQLITE_EXTENSION_INIT2(v) (void)v; /* unused parameter */
127986# define SQLITE_EXTENSION_INIT3 /*no-op*/
127987#endif
127988
127989#endif /* SQLITE3EXT_H */
127990
127991/************** End of sqlite3ext.h ******************************************/
127992/************** Continuing where we left off in loadext.c ********************/
127993/* #include "sqliteInt.h" */
127994
127995#ifndef SQLITE_OMIT_LOAD_EXTENSION
127996/*
127997** Some API routines are omitted when various features are
127998** excluded from a build of SQLite. Substitute a NULL pointer
127999** for any missing APIs.
128000*/
128001#ifndef SQLITE_ENABLE_COLUMN_METADATA
128002# define sqlite3_column_database_name 0
128003# define sqlite3_column_database_name16 0
128004# define sqlite3_column_table_name 0
128005# define sqlite3_column_table_name16 0
128006# define sqlite3_column_origin_name 0
128007# define sqlite3_column_origin_name16 0
128008#endif
128009
128010#ifdef SQLITE_OMIT_AUTHORIZATION
128011# define sqlite3_set_authorizer 0
128012#endif
128013
128014#ifdef SQLITE_OMIT_UTF16
128015# define sqlite3_bind_text16 0
128016# define sqlite3_collation_needed16 0
128017# define sqlite3_column_decltype16 0
128018# define sqlite3_column_name16 0
128019# define sqlite3_column_text16 0
128020# define sqlite3_complete16 0
128021# define sqlite3_create_collation16 0
128022# define sqlite3_create_function16 0
128023# define sqlite3_errmsg16 0
128024# define sqlite3_open16 0
128025# define sqlite3_prepare16 0
128026# define sqlite3_prepare16_v2 0
128027# define sqlite3_prepare16_v3 0
128028# define sqlite3_result_error16 0
128029# define sqlite3_result_text16 0
128030# define sqlite3_result_text16be 0
128031# define sqlite3_result_text16le 0
128032# define sqlite3_value_text16 0
128033# define sqlite3_value_text16be 0
128034# define sqlite3_value_text16le 0
128035# define sqlite3_column_database_name16 0
128036# define sqlite3_column_table_name16 0
128037# define sqlite3_column_origin_name16 0
128038#endif
128039
128040#ifdef SQLITE_OMIT_COMPLETE
128041# define sqlite3_complete 0
128042# define sqlite3_complete16 0
128043#endif
128044
128045#ifdef SQLITE_OMIT_DECLTYPE
128046# define sqlite3_column_decltype16 0
128047# define sqlite3_column_decltype 0
128048#endif
128049
128050#ifdef SQLITE_OMIT_PROGRESS_CALLBACK
128051# define sqlite3_progress_handler 0
128052#endif
128053
128054#ifdef SQLITE_OMIT_VIRTUALTABLE
128055# define sqlite3_create_module 0
128056# define sqlite3_create_module_v2 0
128057# define sqlite3_declare_vtab 0
128058# define sqlite3_vtab_config 0
128059# define sqlite3_vtab_on_conflict 0
128060# define sqlite3_vtab_collation 0
128061#endif
128062
128063#ifdef SQLITE_OMIT_SHARED_CACHE
128064# define sqlite3_enable_shared_cache 0
128065#endif
128066
128067#if defined(SQLITE_OMIT_TRACE) || defined(SQLITE_OMIT_DEPRECATED)
128068# define sqlite3_profile 0
128069# define sqlite3_trace 0
128070#endif
128071
128072#ifdef SQLITE_OMIT_GET_TABLE
128073# define sqlite3_free_table 0
128074# define sqlite3_get_table 0
128075#endif
128076
128077#ifdef SQLITE_OMIT_INCRBLOB
128078#define sqlite3_bind_zeroblob 0
128079#define sqlite3_blob_bytes 0
128080#define sqlite3_blob_close 0
128081#define sqlite3_blob_open 0
128082#define sqlite3_blob_read 0
128083#define sqlite3_blob_write 0
128084#define sqlite3_blob_reopen 0
128085#endif
128086
128087#if defined(SQLITE_OMIT_TRACE)
128088# define sqlite3_trace_v2 0
128089#endif
128090
128091/*
128092** The following structure contains pointers to all SQLite API routines.
128093** A pointer to this structure is passed into extensions when they are
128094** loaded so that the extension can make calls back into the SQLite
128095** library.
128096**
128097** When adding new APIs, add them to the bottom of this structure
128098** in order to preserve backwards compatibility.
128099**
128100** Extensions that use newer APIs should first call the
128101** sqlite3_libversion_number() to make sure that the API they
128102** intend to use is supported by the library. Extensions should
128103** also check to make sure that the pointer to the function is
128104** not NULL before calling it.
128105*/
128106static const sqlite3_api_routines sqlite3Apis = {
128107 sqlite3_aggregate_context,
128108#ifndef SQLITE_OMIT_DEPRECATED
128109 sqlite3_aggregate_count,
128110#else
128111 0,
128112#endif
128113 sqlite3_bind_blob,
128114 sqlite3_bind_double,
128115 sqlite3_bind_int,
128116 sqlite3_bind_int64,
128117 sqlite3_bind_null,
128118 sqlite3_bind_parameter_count,
128119 sqlite3_bind_parameter_index,
128120 sqlite3_bind_parameter_name,
128121 sqlite3_bind_text,
128122 sqlite3_bind_text16,
128123 sqlite3_bind_value,
128124 sqlite3_busy_handler,
128125 sqlite3_busy_timeout,
128126 sqlite3_changes,
128127 sqlite3_close,
128128 sqlite3_collation_needed,
128129 sqlite3_collation_needed16,
128130 sqlite3_column_blob,
128131 sqlite3_column_bytes,
128132 sqlite3_column_bytes16,
128133 sqlite3_column_count,
128134 sqlite3_column_database_name,
128135 sqlite3_column_database_name16,
128136 sqlite3_column_decltype,
128137 sqlite3_column_decltype16,
128138 sqlite3_column_double,
128139 sqlite3_column_int,
128140 sqlite3_column_int64,
128141 sqlite3_column_name,
128142 sqlite3_column_name16,
128143 sqlite3_column_origin_name,
128144 sqlite3_column_origin_name16,
128145 sqlite3_column_table_name,
128146 sqlite3_column_table_name16,
128147 sqlite3_column_text,
128148 sqlite3_column_text16,
128149 sqlite3_column_type,
128150 sqlite3_column_value,
128151 sqlite3_commit_hook,
128152 sqlite3_complete,
128153 sqlite3_complete16,
128154 sqlite3_create_collation,
128155 sqlite3_create_collation16,
128156 sqlite3_create_function,
128157 sqlite3_create_function16,
128158 sqlite3_create_module,
128159 sqlite3_data_count,
128160 sqlite3_db_handle,
128161 sqlite3_declare_vtab,
128162 sqlite3_enable_shared_cache,
128163 sqlite3_errcode,
128164 sqlite3_errmsg,
128165 sqlite3_errmsg16,
128166 sqlite3_exec,
128167#ifndef SQLITE_OMIT_DEPRECATED
128168 sqlite3_expired,
128169#else
128170 0,
128171#endif
128172 sqlite3_finalize,
128173 sqlite3_free,
128174 sqlite3_free_table,
128175 sqlite3_get_autocommit,
128176 sqlite3_get_auxdata,
128177 sqlite3_get_table,
128178 0, /* Was sqlite3_global_recover(), but that function is deprecated */
128179 sqlite3_interrupt,
128180 sqlite3_last_insert_rowid,
128181 sqlite3_libversion,
128182 sqlite3_libversion_number,
128183 sqlite3_malloc,
128184 sqlite3_mprintf,
128185 sqlite3_open,
128186 sqlite3_open16,
128187 sqlite3_prepare,
128188 sqlite3_prepare16,
128189 sqlite3_profile,
128190 sqlite3_progress_handler,
128191 sqlite3_realloc,
128192 sqlite3_reset,
128193 sqlite3_result_blob,
128194 sqlite3_result_double,
128195 sqlite3_result_error,
128196 sqlite3_result_error16,
128197 sqlite3_result_int,
128198 sqlite3_result_int64,
128199 sqlite3_result_null,
128200 sqlite3_result_text,
128201 sqlite3_result_text16,
128202 sqlite3_result_text16be,
128203 sqlite3_result_text16le,
128204 sqlite3_result_value,
128205 sqlite3_rollback_hook,
128206 sqlite3_set_authorizer,
128207 sqlite3_set_auxdata,
128208 sqlite3_snprintf,
128209 sqlite3_step,
128210 sqlite3_table_column_metadata,
128211#ifndef SQLITE_OMIT_DEPRECATED
128212 sqlite3_thread_cleanup,
128213#else
128214 0,
128215#endif
128216 sqlite3_total_changes,
128217 sqlite3_trace,
128218#ifndef SQLITE_OMIT_DEPRECATED
128219 sqlite3_transfer_bindings,
128220#else
128221 0,
128222#endif
128223 sqlite3_update_hook,
128224 sqlite3_user_data,
128225 sqlite3_value_blob,
128226 sqlite3_value_bytes,
128227 sqlite3_value_bytes16,
128228 sqlite3_value_double,
128229 sqlite3_value_int,
128230 sqlite3_value_int64,
128231 sqlite3_value_numeric_type,
128232 sqlite3_value_text,
128233 sqlite3_value_text16,
128234 sqlite3_value_text16be,
128235 sqlite3_value_text16le,
128236 sqlite3_value_type,
128237 sqlite3_vmprintf,
128238 /*
128239 ** The original API set ends here. All extensions can call any
128240 ** of the APIs above provided that the pointer is not NULL. But
128241 ** before calling APIs that follow, extension should check the
128242 ** sqlite3_libversion_number() to make sure they are dealing with
128243 ** a library that is new enough to support that API.
128244 *************************************************************************
128245 */
128246 sqlite3_overload_function,
128247
128248 /*
128249 ** Added after 3.3.13
128250 */
128251 sqlite3_prepare_v2,
128252 sqlite3_prepare16_v2,
128253 sqlite3_clear_bindings,
128254
128255 /*
128256 ** Added for 3.4.1
128257 */
128258 sqlite3_create_module_v2,
128259
128260 /*
128261 ** Added for 3.5.0
128262 */
128263 sqlite3_bind_zeroblob,
128264 sqlite3_blob_bytes,
128265 sqlite3_blob_close,
128266 sqlite3_blob_open,
128267 sqlite3_blob_read,
128268 sqlite3_blob_write,
128269 sqlite3_create_collation_v2,
128270 sqlite3_file_control,
128271 sqlite3_memory_highwater,
128272 sqlite3_memory_used,
128273#ifdef SQLITE_MUTEX_OMIT
128274 0,
128275 0,
128276 0,
128277 0,
128278 0,
128279#else
128280 sqlite3_mutex_alloc,
128281 sqlite3_mutex_enter,
128282 sqlite3_mutex_free,
128283 sqlite3_mutex_leave,
128284 sqlite3_mutex_try,
128285#endif
128286 sqlite3_open_v2,
128287 sqlite3_release_memory,
128288 sqlite3_result_error_nomem,
128289 sqlite3_result_error_toobig,
128290 sqlite3_sleep,
128291 sqlite3_soft_heap_limit,
128292 sqlite3_vfs_find,
128293 sqlite3_vfs_register,
128294 sqlite3_vfs_unregister,
128295
128296 /*
128297 ** Added for 3.5.8
128298 */
128299 sqlite3_threadsafe,
128300 sqlite3_result_zeroblob,
128301 sqlite3_result_error_code,
128302 sqlite3_test_control,
128303 sqlite3_randomness,
128304 sqlite3_context_db_handle,
128305
128306 /*
128307 ** Added for 3.6.0
128308 */
128309 sqlite3_extended_result_codes,
128310 sqlite3_limit,
128311 sqlite3_next_stmt,
128312 sqlite3_sql,
128313 sqlite3_status,
128314
128315 /*
128316 ** Added for 3.7.4
128317 */
128318 sqlite3_backup_finish,
128319 sqlite3_backup_init,
128320 sqlite3_backup_pagecount,
128321 sqlite3_backup_remaining,
128322 sqlite3_backup_step,
128323#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
128324 sqlite3_compileoption_get,
128325 sqlite3_compileoption_used,
128326#else
128327 0,
128328 0,
128329#endif
128330 sqlite3_create_function_v2,
128331 sqlite3_db_config,
128332 sqlite3_db_mutex,
128333 sqlite3_db_status,
128334 sqlite3_extended_errcode,
128335 sqlite3_log,
128336 sqlite3_soft_heap_limit64,
128337 sqlite3_sourceid,
128338 sqlite3_stmt_status,
128339 sqlite3_strnicmp,
128340#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
128341 sqlite3_unlock_notify,
128342#else
128343 0,
128344#endif
128345#ifndef SQLITE_OMIT_WAL
128346 sqlite3_wal_autocheckpoint,
128347 sqlite3_wal_checkpoint,
128348 sqlite3_wal_hook,
128349#else
128350 0,
128351 0,
128352 0,
128353#endif
128354 sqlite3_blob_reopen,
128355 sqlite3_vtab_config,
128356 sqlite3_vtab_on_conflict,
128357 sqlite3_close_v2,
128358 sqlite3_db_filename,
128359 sqlite3_db_readonly,
128360 sqlite3_db_release_memory,
128361 sqlite3_errstr,
128362 sqlite3_stmt_busy,
128363 sqlite3_stmt_readonly,
128364 sqlite3_stricmp,
128365 sqlite3_uri_boolean,
128366 sqlite3_uri_int64,
128367 sqlite3_uri_parameter,
128368 sqlite3_vsnprintf,
128369 sqlite3_wal_checkpoint_v2,
128370 /* Version 3.8.7 and later */
128371 sqlite3_auto_extension,
128372 sqlite3_bind_blob64,
128373 sqlite3_bind_text64,
128374 sqlite3_cancel_auto_extension,
128375 sqlite3_load_extension,
128376 sqlite3_malloc64,
128377 sqlite3_msize,
128378 sqlite3_realloc64,
128379 sqlite3_reset_auto_extension,
128380 sqlite3_result_blob64,
128381 sqlite3_result_text64,
128382 sqlite3_strglob,
128383 /* Version 3.8.11 and later */
128384 (sqlite3_value*(*)(const sqlite3_value*))sqlite3_value_dup,
128385 sqlite3_value_free,
128386 sqlite3_result_zeroblob64,
128387 sqlite3_bind_zeroblob64,
128388 /* Version 3.9.0 and later */
128389 sqlite3_value_subtype,
128390 sqlite3_result_subtype,
128391 /* Version 3.10.0 and later */
128392 sqlite3_status64,
128393 sqlite3_strlike,
128394 sqlite3_db_cacheflush,
128395 /* Version 3.12.0 and later */
128396 sqlite3_system_errno,
128397 /* Version 3.14.0 and later */
128398 sqlite3_trace_v2,
128399 sqlite3_expanded_sql,
128400 /* Version 3.18.0 and later */
128401 sqlite3_set_last_insert_rowid,
128402 /* Version 3.20.0 and later */
128403 sqlite3_prepare_v3,
128404 sqlite3_prepare16_v3,
128405 sqlite3_bind_pointer,
128406 sqlite3_result_pointer,
128407 sqlite3_value_pointer,
128408 /* Version 3.22.0 and later */
128409 sqlite3_vtab_nochange,
128410 sqlite3_value_nochange,
128411 sqlite3_vtab_collation,
128412 /* Version 3.24.0 and later */
128413 sqlite3_keyword_count,
128414 sqlite3_keyword_name,
128415 sqlite3_keyword_check,
128416 sqlite3_str_new,
128417 sqlite3_str_finish,
128418 sqlite3_str_appendf,
128419 sqlite3_str_vappendf,
128420 sqlite3_str_append,
128421 sqlite3_str_appendall,
128422 sqlite3_str_appendchar,
128423 sqlite3_str_reset,
128424 sqlite3_str_errcode,
128425 sqlite3_str_length,
128426 sqlite3_str_value,
128427 /* Version 3.25.0 and later */
128428 sqlite3_create_window_function,
128429 /* Version 3.26.0 and later */
128430#ifdef SQLITE_ENABLE_NORMALIZE
128431 sqlite3_normalized_sql,
128432#else
128433 0,
128434#endif
128435 /* Version 3.28.0 and later */
128436 sqlite3_stmt_isexplain,
128437 sqlite3_value_frombind,
128438 /* Version 3.30.0 and later */
128439#ifndef SQLITE_OMIT_VIRTUALTABLE
128440 sqlite3_drop_modules,
128441#else
128442 0,
128443#endif
128444 /* Version 3.31.0 and later */
128445 sqlite3_hard_heap_limit64,
128446 sqlite3_uri_key,
128447 sqlite3_filename_database,
128448 sqlite3_filename_journal,
128449 sqlite3_filename_wal,
128450 /* Version 3.32.0 and later */
128451 sqlite3_create_filename,
128452 sqlite3_free_filename,
128453 sqlite3_database_file_object,
128454 /* Version 3.34.0 and later */
128455 sqlite3_txn_state,
128456 /* Version 3.36.1 and later */
128457 sqlite3_changes64,
128458 sqlite3_total_changes64,
128459 /* Version 3.37.0 and later */
128460 sqlite3_autovacuum_pages,
128461};
128462
128463/* True if x is the directory separator character
128464*/
128465#if SQLITE_OS_WIN
128466# define DirSep(X) ((X)=='/'||(X)=='\\')
128467#else
128468# define DirSep(X) ((X)=='/')
128469#endif
128470
128471/*
128472** Attempt to load an SQLite extension library contained in the file
128473** zFile. The entry point is zProc. zProc may be 0 in which case a
128474** default entry point name (sqlite3_extension_init) is used. Use
128475** of the default name is recommended.
128476**
128477** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
128478**
128479** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with
128480** error message text. The calling function should free this memory
128481** by calling sqlite3DbFree(db, ).
128482*/
128483static int sqlite3LoadExtension(
128484 sqlite3 *db, /* Load the extension into this database connection */
128485 const char *zFile, /* Name of the shared library containing extension */
128486 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
128487 char **pzErrMsg /* Put error message here if not 0 */
128488){
128489 sqlite3_vfs *pVfs = db->pVfs;
128490 void *handle;
128491 sqlite3_loadext_entry xInit;
128492 char *zErrmsg = 0;
128493 const char *zEntry;
128494 char *zAltEntry = 0;
128495 void **aHandle;
128496 u64 nMsg = strlen(s: zFile);
128497 int ii;
128498 int rc;
128499
128500 /* Shared library endings to try if zFile cannot be loaded as written */
128501 static const char *azEndings[] = {
128502#if SQLITE_OS_WIN
128503 "dll"
128504#elif defined(__APPLE__)
128505 "dylib"
128506#else
128507 "so"
128508#endif
128509 };
128510
128511
128512 if( pzErrMsg ) *pzErrMsg = 0;
128513
128514 /* Ticket #1863. To avoid a creating security problems for older
128515 ** applications that relink against newer versions of SQLite, the
128516 ** ability to run load_extension is turned off by default. One
128517 ** must call either sqlite3_enable_load_extension(db) or
128518 ** sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, 0)
128519 ** to turn on extension loading.
128520 */
128521 if( (db->flags & SQLITE_LoadExtension)==0 ){
128522 if( pzErrMsg ){
128523 *pzErrMsg = sqlite3_mprintf(zFormat: "not authorized");
128524 }
128525 return SQLITE_ERROR;
128526 }
128527
128528 zEntry = zProc ? zProc : "sqlite3_extension_init";
128529
128530 /* tag-20210611-1. Some dlopen() implementations will segfault if given
128531 ** an oversize filename. Most filesystems have a pathname limit of 4K,
128532 ** so limit the extension filename length to about twice that.
128533 ** https://sqlite.org/forum/forumpost/08a0d6d9bf */
128534 if( nMsg>SQLITE_MAX_PATHLEN ) goto extension_not_found;
128535
128536 handle = sqlite3OsDlOpen(pVfs, zPath: zFile);
128537#if SQLITE_OS_UNIX || SQLITE_OS_WIN
128538 for(ii=0; ii<ArraySize(azEndings) && handle==0; ii++){
128539 char *zAltFile = sqlite3_mprintf(zFormat: "%s.%s", zFile, azEndings[ii]);
128540 if( zAltFile==0 ) return SQLITE_NOMEM_BKPT;
128541 handle = sqlite3OsDlOpen(pVfs, zPath: zAltFile);
128542 sqlite3_free(p: zAltFile);
128543 }
128544#endif
128545 if( handle==0 ) goto extension_not_found;
128546 xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, pHdle: handle, zSym: zEntry);
128547
128548 /* If no entry point was specified and the default legacy
128549 ** entry point name "sqlite3_extension_init" was not found, then
128550 ** construct an entry point name "sqlite3_X_init" where the X is
128551 ** replaced by the lowercase value of every ASCII alphabetic
128552 ** character in the filename after the last "/" upto the first ".",
128553 ** and eliding the first three characters if they are "lib".
128554 ** Examples:
128555 **
128556 ** /usr/local/lib/libExample5.4.3.so ==> sqlite3_example_init
128557 ** C:/lib/mathfuncs.dll ==> sqlite3_mathfuncs_init
128558 */
128559 if( xInit==0 && zProc==0 ){
128560 int iFile, iEntry, c;
128561 int ncFile = sqlite3Strlen30(z: zFile);
128562 zAltEntry = sqlite3_malloc64(n: ncFile+30);
128563 if( zAltEntry==0 ){
128564 sqlite3OsDlClose(pVfs, pHandle: handle);
128565 return SQLITE_NOMEM_BKPT;
128566 }
128567 memcpy(dest: zAltEntry, src: "sqlite3_", n: 8);
128568 for(iFile=ncFile-1; iFile>=0 && !DirSep(zFile[iFile]); iFile--){}
128569 iFile++;
128570 if( sqlite3_strnicmp(zLeft: zFile+iFile, zRight: "lib", N: 3)==0 ) iFile += 3;
128571 for(iEntry=8; (c = zFile[iFile])!=0 && c!='.'; iFile++){
128572 if( sqlite3Isalpha(c) ){
128573 zAltEntry[iEntry++] = (char)sqlite3UpperToLower[(unsigned)c];
128574 }
128575 }
128576 memcpy(dest: zAltEntry+iEntry, src: "_init", n: 6);
128577 zEntry = zAltEntry;
128578 xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, pHdle: handle, zSym: zEntry);
128579 }
128580 if( xInit==0 ){
128581 if( pzErrMsg ){
128582 nMsg += strlen(s: zEntry) + 300;
128583 *pzErrMsg = zErrmsg = sqlite3_malloc64(n: nMsg);
128584 if( zErrmsg ){
128585 assert( nMsg<0x7fffffff ); /* zErrmsg would be NULL if not so */
128586 sqlite3_snprintf(n: (int)nMsg, zBuf: zErrmsg,
128587 zFormat: "no entry point [%s] in shared library [%s]", zEntry, zFile);
128588 sqlite3OsDlError(pVfs, nByte: nMsg-1, zBufOut: zErrmsg);
128589 }
128590 }
128591 sqlite3OsDlClose(pVfs, pHandle: handle);
128592 sqlite3_free(p: zAltEntry);
128593 return SQLITE_ERROR;
128594 }
128595 sqlite3_free(p: zAltEntry);
128596 rc = xInit(db, &zErrmsg, &sqlite3Apis);
128597 if( rc ){
128598 if( rc==SQLITE_OK_LOAD_PERMANENTLY ) return SQLITE_OK;
128599 if( pzErrMsg ){
128600 *pzErrMsg = sqlite3_mprintf(zFormat: "error during initialization: %s", zErrmsg);
128601 }
128602 sqlite3_free(p: zErrmsg);
128603 sqlite3OsDlClose(pVfs, pHandle: handle);
128604 return SQLITE_ERROR;
128605 }
128606
128607 /* Append the new shared library handle to the db->aExtension array. */
128608 aHandle = sqlite3DbMallocZero(db, n: sizeof(handle)*(db->nExtension+1));
128609 if( aHandle==0 ){
128610 return SQLITE_NOMEM_BKPT;
128611 }
128612 if( db->nExtension>0 ){
128613 memcpy(dest: aHandle, src: db->aExtension, n: sizeof(handle)*db->nExtension);
128614 }
128615 sqlite3DbFree(db, p: db->aExtension);
128616 db->aExtension = aHandle;
128617
128618 db->aExtension[db->nExtension++] = handle;
128619 return SQLITE_OK;
128620
128621extension_not_found:
128622 if( pzErrMsg ){
128623 nMsg += 300;
128624 *pzErrMsg = zErrmsg = sqlite3_malloc64(n: nMsg);
128625 if( zErrmsg ){
128626 assert( nMsg<0x7fffffff ); /* zErrmsg would be NULL if not so */
128627 sqlite3_snprintf(n: (int)nMsg, zBuf: zErrmsg,
128628 zFormat: "unable to open shared library [%.*s]", SQLITE_MAX_PATHLEN, zFile);
128629 sqlite3OsDlError(pVfs, nByte: nMsg-1, zBufOut: zErrmsg);
128630 }
128631 }
128632 return SQLITE_ERROR;
128633}
128634SQLITE_API int sqlite3_load_extension(
128635 sqlite3 *db, /* Load the extension into this database connection */
128636 const char *zFile, /* Name of the shared library containing extension */
128637 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
128638 char **pzErrMsg /* Put error message here if not 0 */
128639){
128640 int rc;
128641 sqlite3_mutex_enter(p: db->mutex);
128642 rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg);
128643 rc = sqlite3ApiExit(db, rc);
128644 sqlite3_mutex_leave(p: db->mutex);
128645 return rc;
128646}
128647
128648/*
128649** Call this routine when the database connection is closing in order
128650** to clean up loaded extensions
128651*/
128652SQLITE_PRIVATE void sqlite3CloseExtensions(sqlite3 *db){
128653 int i;
128654 assert( sqlite3_mutex_held(db->mutex) );
128655 for(i=0; i<db->nExtension; i++){
128656 sqlite3OsDlClose(pVfs: db->pVfs, pHandle: db->aExtension[i]);
128657 }
128658 sqlite3DbFree(db, p: db->aExtension);
128659}
128660
128661/*
128662** Enable or disable extension loading. Extension loading is disabled by
128663** default so as not to open security holes in older applications.
128664*/
128665SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
128666 sqlite3_mutex_enter(p: db->mutex);
128667 if( onoff ){
128668 db->flags |= SQLITE_LoadExtension|SQLITE_LoadExtFunc;
128669 }else{
128670 db->flags &= ~(u64)(SQLITE_LoadExtension|SQLITE_LoadExtFunc);
128671 }
128672 sqlite3_mutex_leave(p: db->mutex);
128673 return SQLITE_OK;
128674}
128675
128676#endif /* !defined(SQLITE_OMIT_LOAD_EXTENSION) */
128677
128678/*
128679** The following object holds the list of automatically loaded
128680** extensions.
128681**
128682** This list is shared across threads. The SQLITE_MUTEX_STATIC_MAIN
128683** mutex must be held while accessing this list.
128684*/
128685typedef struct sqlite3AutoExtList sqlite3AutoExtList;
128686static SQLITE_WSD struct sqlite3AutoExtList {
128687 u32 nExt; /* Number of entries in aExt[] */
128688 void (**aExt)(void); /* Pointers to the extension init functions */
128689} sqlite3Autoext = { 0, 0 };
128690
128691/* The "wsdAutoext" macro will resolve to the autoextension
128692** state vector. If writable static data is unsupported on the target,
128693** we have to locate the state vector at run-time. In the more common
128694** case where writable static data is supported, wsdStat can refer directly
128695** to the "sqlite3Autoext" state vector declared above.
128696*/
128697#ifdef SQLITE_OMIT_WSD
128698# define wsdAutoextInit \
128699 sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext)
128700# define wsdAutoext x[0]
128701#else
128702# define wsdAutoextInit
128703# define wsdAutoext sqlite3Autoext
128704#endif
128705
128706
128707/*
128708** Register a statically linked extension that is automatically
128709** loaded by every new database connection.
128710*/
128711SQLITE_API int sqlite3_auto_extension(
128712 void (*xInit)(void)
128713){
128714 int rc = SQLITE_OK;
128715#ifndef SQLITE_OMIT_AUTOINIT
128716 rc = sqlite3_initialize();
128717 if( rc ){
128718 return rc;
128719 }else
128720#endif
128721 {
128722 u32 i;
128723#if SQLITE_THREADSAFE
128724 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);
128725#endif
128726 wsdAutoextInit;
128727 sqlite3_mutex_enter(p: mutex);
128728 for(i=0; i<wsdAutoext.nExt; i++){
128729 if( wsdAutoext.aExt[i]==xInit ) break;
128730 }
128731 if( i==wsdAutoext.nExt ){
128732 u64 nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]);
128733 void (**aNew)(void);
128734 aNew = sqlite3_realloc64(wsdAutoext.aExt, n: nByte);
128735 if( aNew==0 ){
128736 rc = SQLITE_NOMEM_BKPT;
128737 }else{
128738 wsdAutoext.aExt = aNew;
128739 wsdAutoext.aExt[wsdAutoext.nExt] = xInit;
128740 wsdAutoext.nExt++;
128741 }
128742 }
128743 sqlite3_mutex_leave(p: mutex);
128744 assert( (rc&0xff)==rc );
128745 return rc;
128746 }
128747}
128748
128749/*
128750** Cancel a prior call to sqlite3_auto_extension. Remove xInit from the
128751** set of routines that is invoked for each new database connection, if it
128752** is currently on the list. If xInit is not on the list, then this
128753** routine is a no-op.
128754**
128755** Return 1 if xInit was found on the list and removed. Return 0 if xInit
128756** was not on the list.
128757*/
128758SQLITE_API int sqlite3_cancel_auto_extension(
128759 void (*xInit)(void)
128760){
128761#if SQLITE_THREADSAFE
128762 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);
128763#endif
128764 int i;
128765 int n = 0;
128766 wsdAutoextInit;
128767 sqlite3_mutex_enter(p: mutex);
128768 for(i=(int)wsdAutoext.nExt-1; i>=0; i--){
128769 if( wsdAutoext.aExt[i]==xInit ){
128770 wsdAutoext.nExt--;
128771 wsdAutoext.aExt[i] = wsdAutoext.aExt[wsdAutoext.nExt];
128772 n++;
128773 break;
128774 }
128775 }
128776 sqlite3_mutex_leave(p: mutex);
128777 return n;
128778}
128779
128780/*
128781** Reset the automatic extension loading mechanism.
128782*/
128783SQLITE_API void sqlite3_reset_auto_extension(void){
128784#ifndef SQLITE_OMIT_AUTOINIT
128785 if( sqlite3_initialize()==SQLITE_OK )
128786#endif
128787 {
128788#if SQLITE_THREADSAFE
128789 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);
128790#endif
128791 wsdAutoextInit;
128792 sqlite3_mutex_enter(p: mutex);
128793 sqlite3_free(wsdAutoext.aExt);
128794 wsdAutoext.aExt = 0;
128795 wsdAutoext.nExt = 0;
128796 sqlite3_mutex_leave(p: mutex);
128797 }
128798}
128799
128800/*
128801** Load all automatic extensions.
128802**
128803** If anything goes wrong, set an error in the database connection.
128804*/
128805SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3 *db){
128806 u32 i;
128807 int go = 1;
128808 int rc;
128809 sqlite3_loadext_entry xInit;
128810
128811 wsdAutoextInit;
128812 if( wsdAutoext.nExt==0 ){
128813 /* Common case: early out without every having to acquire a mutex */
128814 return;
128815 }
128816 for(i=0; go; i++){
128817 char *zErrmsg;
128818#if SQLITE_THREADSAFE
128819 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);
128820#endif
128821#ifdef SQLITE_OMIT_LOAD_EXTENSION
128822 const sqlite3_api_routines *pThunk = 0;
128823#else
128824 const sqlite3_api_routines *pThunk = &sqlite3Apis;
128825#endif
128826 sqlite3_mutex_enter(p: mutex);
128827 if( i>=wsdAutoext.nExt ){
128828 xInit = 0;
128829 go = 0;
128830 }else{
128831 xInit = (sqlite3_loadext_entry)wsdAutoext.aExt[i];
128832 }
128833 sqlite3_mutex_leave(p: mutex);
128834 zErrmsg = 0;
128835 if( xInit && (rc = xInit(db, &zErrmsg, pThunk))!=0 ){
128836 sqlite3ErrorWithMsg(db, err_code: rc,
128837 zFormat: "automatic extension loading failed: %s", zErrmsg);
128838 go = 0;
128839 }
128840 sqlite3_free(p: zErrmsg);
128841 }
128842}
128843
128844/************** End of loadext.c *********************************************/
128845/************** Begin file pragma.c ******************************************/
128846/*
128847** 2003 April 6
128848**
128849** The author disclaims copyright to this source code. In place of
128850** a legal notice, here is a blessing:
128851**
128852** May you do good and not evil.
128853** May you find forgiveness for yourself and forgive others.
128854** May you share freely, never taking more than you give.
128855**
128856*************************************************************************
128857** This file contains code used to implement the PRAGMA command.
128858*/
128859/* #include "sqliteInt.h" */
128860
128861#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
128862# if defined(__APPLE__)
128863# define SQLITE_ENABLE_LOCKING_STYLE 1
128864# else
128865# define SQLITE_ENABLE_LOCKING_STYLE 0
128866# endif
128867#endif
128868
128869/***************************************************************************
128870** The "pragma.h" include file is an automatically generated file that
128871** that includes the PragType_XXXX macro definitions and the aPragmaName[]
128872** object. This ensures that the aPragmaName[] table is arranged in
128873** lexicographical order to facility a binary search of the pragma name.
128874** Do not edit pragma.h directly. Edit and rerun the script in at
128875** ../tool/mkpragmatab.tcl. */
128876/************** Include pragma.h in the middle of pragma.c *******************/
128877/************** Begin file pragma.h ******************************************/
128878/* DO NOT EDIT!
128879** This file is automatically generated by the script at
128880** ../tool/mkpragmatab.tcl. To update the set of pragmas, edit
128881** that script and rerun it.
128882*/
128883
128884/* The various pragma types */
128885#define PragTyp_ACTIVATE_EXTENSIONS 0
128886#define PragTyp_ANALYSIS_LIMIT 1
128887#define PragTyp_HEADER_VALUE 2
128888#define PragTyp_AUTO_VACUUM 3
128889#define PragTyp_FLAG 4
128890#define PragTyp_BUSY_TIMEOUT 5
128891#define PragTyp_CACHE_SIZE 6
128892#define PragTyp_CACHE_SPILL 7
128893#define PragTyp_CASE_SENSITIVE_LIKE 8
128894#define PragTyp_COLLATION_LIST 9
128895#define PragTyp_COMPILE_OPTIONS 10
128896#define PragTyp_DATA_STORE_DIRECTORY 11
128897#define PragTyp_DATABASE_LIST 12
128898#define PragTyp_DEFAULT_CACHE_SIZE 13
128899#define PragTyp_ENCODING 14
128900#define PragTyp_FOREIGN_KEY_CHECK 15
128901#define PragTyp_FOREIGN_KEY_LIST 16
128902#define PragTyp_FUNCTION_LIST 17
128903#define PragTyp_HARD_HEAP_LIMIT 18
128904#define PragTyp_INCREMENTAL_VACUUM 19
128905#define PragTyp_INDEX_INFO 20
128906#define PragTyp_INDEX_LIST 21
128907#define PragTyp_INTEGRITY_CHECK 22
128908#define PragTyp_JOURNAL_MODE 23
128909#define PragTyp_JOURNAL_SIZE_LIMIT 24
128910#define PragTyp_LOCK_PROXY_FILE 25
128911#define PragTyp_LOCKING_MODE 26
128912#define PragTyp_PAGE_COUNT 27
128913#define PragTyp_MMAP_SIZE 28
128914#define PragTyp_MODULE_LIST 29
128915#define PragTyp_OPTIMIZE 30
128916#define PragTyp_PAGE_SIZE 31
128917#define PragTyp_PRAGMA_LIST 32
128918#define PragTyp_SECURE_DELETE 33
128919#define PragTyp_SHRINK_MEMORY 34
128920#define PragTyp_SOFT_HEAP_LIMIT 35
128921#define PragTyp_SYNCHRONOUS 36
128922#define PragTyp_TABLE_INFO 37
128923#define PragTyp_TABLE_LIST 38
128924#define PragTyp_TEMP_STORE 39
128925#define PragTyp_TEMP_STORE_DIRECTORY 40
128926#define PragTyp_THREADS 41
128927#define PragTyp_WAL_AUTOCHECKPOINT 42
128928#define PragTyp_WAL_CHECKPOINT 43
128929#define PragTyp_LOCK_STATUS 44
128930#define PragTyp_STATS 45
128931
128932/* Property flags associated with various pragma. */
128933#define PragFlg_NeedSchema 0x01 /* Force schema load before running */
128934#define PragFlg_NoColumns 0x02 /* OP_ResultRow called with zero columns */
128935#define PragFlg_NoColumns1 0x04 /* zero columns if RHS argument is present */
128936#define PragFlg_ReadOnly 0x08 /* Read-only HEADER_VALUE */
128937#define PragFlg_Result0 0x10 /* Acts as query when no argument */
128938#define PragFlg_Result1 0x20 /* Acts as query when has one argument */
128939#define PragFlg_SchemaOpt 0x40 /* Schema restricts name search if present */
128940#define PragFlg_SchemaReq 0x80 /* Schema required - "main" is default */
128941
128942/* Names of columns for pragmas that return multi-column result
128943** or that return single-column results where the name of the
128944** result column is different from the name of the pragma
128945*/
128946static const char *const pragCName[] = {
128947 /* 0 */ "id", /* Used by: foreign_key_list */
128948 /* 1 */ "seq",
128949 /* 2 */ "table",
128950 /* 3 */ "from",
128951 /* 4 */ "to",
128952 /* 5 */ "on_update",
128953 /* 6 */ "on_delete",
128954 /* 7 */ "match",
128955 /* 8 */ "cid", /* Used by: table_xinfo */
128956 /* 9 */ "name",
128957 /* 10 */ "type",
128958 /* 11 */ "notnull",
128959 /* 12 */ "dflt_value",
128960 /* 13 */ "pk",
128961 /* 14 */ "hidden",
128962 /* table_info reuses 8 */
128963 /* 15 */ "schema", /* Used by: table_list */
128964 /* 16 */ "name",
128965 /* 17 */ "type",
128966 /* 18 */ "ncol",
128967 /* 19 */ "wr",
128968 /* 20 */ "strict",
128969 /* 21 */ "seqno", /* Used by: index_xinfo */
128970 /* 22 */ "cid",
128971 /* 23 */ "name",
128972 /* 24 */ "desc",
128973 /* 25 */ "coll",
128974 /* 26 */ "key",
128975 /* 27 */ "name", /* Used by: function_list */
128976 /* 28 */ "builtin",
128977 /* 29 */ "type",
128978 /* 30 */ "enc",
128979 /* 31 */ "narg",
128980 /* 32 */ "flags",
128981 /* 33 */ "tbl", /* Used by: stats */
128982 /* 34 */ "idx",
128983 /* 35 */ "wdth",
128984 /* 36 */ "hght",
128985 /* 37 */ "flgs",
128986 /* 38 */ "seq", /* Used by: index_list */
128987 /* 39 */ "name",
128988 /* 40 */ "unique",
128989 /* 41 */ "origin",
128990 /* 42 */ "partial",
128991 /* 43 */ "table", /* Used by: foreign_key_check */
128992 /* 44 */ "rowid",
128993 /* 45 */ "parent",
128994 /* 46 */ "fkid",
128995 /* index_info reuses 21 */
128996 /* 47 */ "seq", /* Used by: database_list */
128997 /* 48 */ "name",
128998 /* 49 */ "file",
128999 /* 50 */ "busy", /* Used by: wal_checkpoint */
129000 /* 51 */ "log",
129001 /* 52 */ "checkpointed",
129002 /* collation_list reuses 38 */
129003 /* 53 */ "database", /* Used by: lock_status */
129004 /* 54 */ "status",
129005 /* 55 */ "cache_size", /* Used by: default_cache_size */
129006 /* module_list pragma_list reuses 9 */
129007 /* 56 */ "timeout", /* Used by: busy_timeout */
129008};
129009
129010/* Definitions of all built-in pragmas */
129011typedef struct PragmaName {
129012 const char *const zName; /* Name of pragma */
129013 u8 ePragTyp; /* PragTyp_XXX value */
129014 u8 mPragFlg; /* Zero or more PragFlg_XXX values */
129015 u8 iPragCName; /* Start of column names in pragCName[] */
129016 u8 nPragCName; /* Num of col names. 0 means use pragma name */
129017 u64 iArg; /* Extra argument */
129018} PragmaName;
129019static const PragmaName aPragmaName[] = {
129020#if defined(SQLITE_ENABLE_CEROD)
129021 {/* zName: */ "activate_extensions",
129022 /* ePragTyp: */ PragTyp_ACTIVATE_EXTENSIONS,
129023 /* ePragFlg: */ 0,
129024 /* ColNames: */ 0, 0,
129025 /* iArg: */ 0 },
129026#endif
129027 {/* zName: */ "analysis_limit",
129028 /* ePragTyp: */ PragTyp_ANALYSIS_LIMIT,
129029 /* ePragFlg: */ PragFlg_Result0,
129030 /* ColNames: */ 0, 0,
129031 /* iArg: */ 0 },
129032#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
129033 {/* zName: */ "application_id",
129034 /* ePragTyp: */ PragTyp_HEADER_VALUE,
129035 /* ePragFlg: */ PragFlg_NoColumns1|PragFlg_Result0,
129036 /* ColNames: */ 0, 0,
129037 /* iArg: */ BTREE_APPLICATION_ID },
129038#endif
129039#if !defined(SQLITE_OMIT_AUTOVACUUM)
129040 {/* zName: */ "auto_vacuum",
129041 /* ePragTyp: */ PragTyp_AUTO_VACUUM,
129042 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
129043 /* ColNames: */ 0, 0,
129044 /* iArg: */ 0 },
129045#endif
129046#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
129047#if !defined(SQLITE_OMIT_AUTOMATIC_INDEX)
129048 {/* zName: */ "automatic_index",
129049 /* ePragTyp: */ PragTyp_FLAG,
129050 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
129051 /* ColNames: */ 0, 0,
129052 /* iArg: */ SQLITE_AutoIndex },
129053#endif
129054#endif
129055 {/* zName: */ "busy_timeout",
129056 /* ePragTyp: */ PragTyp_BUSY_TIMEOUT,
129057 /* ePragFlg: */ PragFlg_Result0,
129058 /* ColNames: */ 56, 1,
129059 /* iArg: */ 0 },
129060#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
129061 {/* zName: */ "cache_size",
129062 /* ePragTyp: */ PragTyp_CACHE_SIZE,
129063 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
129064 /* ColNames: */ 0, 0,
129065 /* iArg: */ 0 },
129066#endif
129067#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
129068 {/* zName: */ "cache_spill",
129069 /* ePragTyp: */ PragTyp_CACHE_SPILL,
129070 /* ePragFlg: */ PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
129071 /* ColNames: */ 0, 0,
129072 /* iArg: */ 0 },
129073#endif
129074#if !defined(SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA)
129075 {/* zName: */ "case_sensitive_like",
129076 /* ePragTyp: */ PragTyp_CASE_SENSITIVE_LIKE,
129077 /* ePragFlg: */ PragFlg_NoColumns,
129078 /* ColNames: */ 0, 0,
129079 /* iArg: */ 0 },
129080#endif
129081 {/* zName: */ "cell_size_check",
129082 /* ePragTyp: */ PragTyp_FLAG,
129083 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
129084 /* ColNames: */ 0, 0,
129085 /* iArg: */ SQLITE_CellSizeCk },
129086#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
129087 {/* zName: */ "checkpoint_fullfsync",
129088 /* ePragTyp: */ PragTyp_FLAG,
129089 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
129090 /* ColNames: */ 0, 0,
129091 /* iArg: */ SQLITE_CkptFullFSync },
129092#endif
129093#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
129094 {/* zName: */ "collation_list",
129095 /* ePragTyp: */ PragTyp_COLLATION_LIST,
129096 /* ePragFlg: */ PragFlg_Result0,
129097 /* ColNames: */ 38, 2,
129098 /* iArg: */ 0 },
129099#endif
129100#if !defined(SQLITE_OMIT_COMPILEOPTION_DIAGS)
129101 {/* zName: */ "compile_options",
129102 /* ePragTyp: */ PragTyp_COMPILE_OPTIONS,
129103 /* ePragFlg: */ PragFlg_Result0,
129104 /* ColNames: */ 0, 0,
129105 /* iArg: */ 0 },
129106#endif
129107#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
129108 {/* zName: */ "count_changes",
129109 /* ePragTyp: */ PragTyp_FLAG,
129110 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
129111 /* ColNames: */ 0, 0,
129112 /* iArg: */ SQLITE_CountRows },
129113#endif
129114#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_OS_WIN
129115 {/* zName: */ "data_store_directory",
129116 /* ePragTyp: */ PragTyp_DATA_STORE_DIRECTORY,
129117 /* ePragFlg: */ PragFlg_NoColumns1,
129118 /* ColNames: */ 0, 0,
129119 /* iArg: */ 0 },
129120#endif
129121#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
129122 {/* zName: */ "data_version",
129123 /* ePragTyp: */ PragTyp_HEADER_VALUE,
129124 /* ePragFlg: */ PragFlg_ReadOnly|PragFlg_Result0,
129125 /* ColNames: */ 0, 0,
129126 /* iArg: */ BTREE_DATA_VERSION },
129127#endif
129128#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
129129 {/* zName: */ "database_list",
129130 /* ePragTyp: */ PragTyp_DATABASE_LIST,
129131 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0,
129132 /* ColNames: */ 47, 3,
129133 /* iArg: */ 0 },
129134#endif
129135#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
129136 {/* zName: */ "default_cache_size",
129137 /* ePragTyp: */ PragTyp_DEFAULT_CACHE_SIZE,
129138 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
129139 /* ColNames: */ 55, 1,
129140 /* iArg: */ 0 },
129141#endif
129142#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
129143#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
129144 {/* zName: */ "defer_foreign_keys",
129145 /* ePragTyp: */ PragTyp_FLAG,
129146 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
129147 /* ColNames: */ 0, 0,
129148 /* iArg: */ SQLITE_DeferFKs },
129149#endif
129150#endif
129151#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
129152 {/* zName: */ "empty_result_callbacks",
129153 /* ePragTyp: */ PragTyp_FLAG,
129154 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
129155 /* ColNames: */ 0, 0,
129156 /* iArg: */ SQLITE_NullCallback },
129157#endif
129158#if !defined(SQLITE_OMIT_UTF16)
129159 {/* zName: */ "encoding",
129160 /* ePragTyp: */ PragTyp_ENCODING,
129161 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
129162 /* ColNames: */ 0, 0,
129163 /* iArg: */ 0 },
129164#endif
129165#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
129166 {/* zName: */ "foreign_key_check",
129167 /* ePragTyp: */ PragTyp_FOREIGN_KEY_CHECK,
129168 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_Result1|PragFlg_SchemaOpt,
129169 /* ColNames: */ 43, 4,
129170 /* iArg: */ 0 },
129171#endif
129172#if !defined(SQLITE_OMIT_FOREIGN_KEY)
129173 {/* zName: */ "foreign_key_list",
129174 /* ePragTyp: */ PragTyp_FOREIGN_KEY_LIST,
129175 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
129176 /* ColNames: */ 0, 8,
129177 /* iArg: */ 0 },
129178#endif
129179#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
129180#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
129181 {/* zName: */ "foreign_keys",
129182 /* ePragTyp: */ PragTyp_FLAG,
129183 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
129184 /* ColNames: */ 0, 0,
129185 /* iArg: */ SQLITE_ForeignKeys },
129186#endif
129187#endif
129188#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
129189 {/* zName: */ "freelist_count",
129190 /* ePragTyp: */ PragTyp_HEADER_VALUE,
129191 /* ePragFlg: */ PragFlg_ReadOnly|PragFlg_Result0,
129192 /* ColNames: */ 0, 0,
129193 /* iArg: */ BTREE_FREE_PAGE_COUNT },
129194#endif
129195#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
129196 {/* zName: */ "full_column_names",
129197 /* ePragTyp: */ PragTyp_FLAG,
129198 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
129199 /* ColNames: */ 0, 0,
129200 /* iArg: */ SQLITE_FullColNames },
129201 {/* zName: */ "fullfsync",
129202 /* ePragTyp: */ PragTyp_FLAG,
129203 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
129204 /* ColNames: */ 0, 0,
129205 /* iArg: */ SQLITE_FullFSync },
129206#endif
129207#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
129208#if !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS)
129209 {/* zName: */ "function_list",
129210 /* ePragTyp: */ PragTyp_FUNCTION_LIST,
129211 /* ePragFlg: */ PragFlg_Result0,
129212 /* ColNames: */ 27, 6,
129213 /* iArg: */ 0 },
129214#endif
129215#endif
129216 {/* zName: */ "hard_heap_limit",
129217 /* ePragTyp: */ PragTyp_HARD_HEAP_LIMIT,
129218 /* ePragFlg: */ PragFlg_Result0,
129219 /* ColNames: */ 0, 0,
129220 /* iArg: */ 0 },
129221#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
129222#if !defined(SQLITE_OMIT_CHECK)
129223 {/* zName: */ "ignore_check_constraints",
129224 /* ePragTyp: */ PragTyp_FLAG,
129225 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
129226 /* ColNames: */ 0, 0,
129227 /* iArg: */ SQLITE_IgnoreChecks },
129228#endif
129229#endif
129230#if !defined(SQLITE_OMIT_AUTOVACUUM)
129231 {/* zName: */ "incremental_vacuum",
129232 /* ePragTyp: */ PragTyp_INCREMENTAL_VACUUM,
129233 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_NoColumns,
129234 /* ColNames: */ 0, 0,
129235 /* iArg: */ 0 },
129236#endif
129237#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
129238 {/* zName: */ "index_info",
129239 /* ePragTyp: */ PragTyp_INDEX_INFO,
129240 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
129241 /* ColNames: */ 21, 3,
129242 /* iArg: */ 0 },
129243 {/* zName: */ "index_list",
129244 /* ePragTyp: */ PragTyp_INDEX_LIST,
129245 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
129246 /* ColNames: */ 38, 5,
129247 /* iArg: */ 0 },
129248 {/* zName: */ "index_xinfo",
129249 /* ePragTyp: */ PragTyp_INDEX_INFO,
129250 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
129251 /* ColNames: */ 21, 6,
129252 /* iArg: */ 1 },
129253#endif
129254#if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
129255 {/* zName: */ "integrity_check",
129256 /* ePragTyp: */ PragTyp_INTEGRITY_CHECK,
129257 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_Result1|PragFlg_SchemaOpt,
129258 /* ColNames: */ 0, 0,
129259 /* iArg: */ 0 },
129260#endif
129261#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
129262 {/* zName: */ "journal_mode",
129263 /* ePragTyp: */ PragTyp_JOURNAL_MODE,
129264 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
129265 /* ColNames: */ 0, 0,
129266 /* iArg: */ 0 },
129267 {/* zName: */ "journal_size_limit",
129268 /* ePragTyp: */ PragTyp_JOURNAL_SIZE_LIMIT,
129269 /* ePragFlg: */ PragFlg_Result0|PragFlg_SchemaReq,
129270 /* ColNames: */ 0, 0,
129271 /* iArg: */ 0 },
129272#endif
129273#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
129274 {/* zName: */ "legacy_alter_table",
129275 /* ePragTyp: */ PragTyp_FLAG,
129276 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
129277 /* ColNames: */ 0, 0,
129278 /* iArg: */ SQLITE_LegacyAlter },
129279#endif
129280#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_ENABLE_LOCKING_STYLE
129281 {/* zName: */ "lock_proxy_file",
129282 /* ePragTyp: */ PragTyp_LOCK_PROXY_FILE,
129283 /* ePragFlg: */ PragFlg_NoColumns1,
129284 /* ColNames: */ 0, 0,
129285 /* iArg: */ 0 },
129286#endif
129287#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
129288 {/* zName: */ "lock_status",
129289 /* ePragTyp: */ PragTyp_LOCK_STATUS,
129290 /* ePragFlg: */ PragFlg_Result0,
129291 /* ColNames: */ 53, 2,
129292 /* iArg: */ 0 },
129293#endif
129294#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
129295 {/* zName: */ "locking_mode",
129296 /* ePragTyp: */ PragTyp_LOCKING_MODE,
129297 /* ePragFlg: */ PragFlg_Result0|PragFlg_SchemaReq,
129298 /* ColNames: */ 0, 0,
129299 /* iArg: */ 0 },
129300 {/* zName: */ "max_page_count",
129301 /* ePragTyp: */ PragTyp_PAGE_COUNT,
129302 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
129303 /* ColNames: */ 0, 0,
129304 /* iArg: */ 0 },
129305 {/* zName: */ "mmap_size",
129306 /* ePragTyp: */ PragTyp_MMAP_SIZE,
129307 /* ePragFlg: */ 0,
129308 /* ColNames: */ 0, 0,
129309 /* iArg: */ 0 },
129310#endif
129311#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
129312#if !defined(SQLITE_OMIT_VIRTUALTABLE)
129313#if !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS)
129314 {/* zName: */ "module_list",
129315 /* ePragTyp: */ PragTyp_MODULE_LIST,
129316 /* ePragFlg: */ PragFlg_Result0,
129317 /* ColNames: */ 9, 1,
129318 /* iArg: */ 0 },
129319#endif
129320#endif
129321#endif
129322 {/* zName: */ "optimize",
129323 /* ePragTyp: */ PragTyp_OPTIMIZE,
129324 /* ePragFlg: */ PragFlg_Result1|PragFlg_NeedSchema,
129325 /* ColNames: */ 0, 0,
129326 /* iArg: */ 0 },
129327#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
129328 {/* zName: */ "page_count",
129329 /* ePragTyp: */ PragTyp_PAGE_COUNT,
129330 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
129331 /* ColNames: */ 0, 0,
129332 /* iArg: */ 0 },
129333 {/* zName: */ "page_size",
129334 /* ePragTyp: */ PragTyp_PAGE_SIZE,
129335 /* ePragFlg: */ PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
129336 /* ColNames: */ 0, 0,
129337 /* iArg: */ 0 },
129338#endif
129339#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
129340#if defined(SQLITE_DEBUG)
129341 {/* zName: */ "parser_trace",
129342 /* ePragTyp: */ PragTyp_FLAG,
129343 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
129344 /* ColNames: */ 0, 0,
129345 /* iArg: */ SQLITE_ParserTrace },
129346#endif
129347#endif
129348#if !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS)
129349 {/* zName: */ "pragma_list",
129350 /* ePragTyp: */ PragTyp_PRAGMA_LIST,
129351 /* ePragFlg: */ PragFlg_Result0,
129352 /* ColNames: */ 9, 1,
129353 /* iArg: */ 0 },
129354#endif
129355#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
129356 {/* zName: */ "query_only",
129357 /* ePragTyp: */ PragTyp_FLAG,
129358 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
129359 /* ColNames: */ 0, 0,
129360 /* iArg: */ SQLITE_QueryOnly },
129361#endif
129362#if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
129363 {/* zName: */ "quick_check",
129364 /* ePragTyp: */ PragTyp_INTEGRITY_CHECK,
129365 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_Result1|PragFlg_SchemaOpt,
129366 /* ColNames: */ 0, 0,
129367 /* iArg: */ 0 },
129368#endif
129369#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
129370 {/* zName: */ "read_uncommitted",
129371 /* ePragTyp: */ PragTyp_FLAG,
129372 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
129373 /* ColNames: */ 0, 0,
129374 /* iArg: */ SQLITE_ReadUncommit },
129375 {/* zName: */ "recursive_triggers",
129376 /* ePragTyp: */ PragTyp_FLAG,
129377 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
129378 /* ColNames: */ 0, 0,
129379 /* iArg: */ SQLITE_RecTriggers },
129380 {/* zName: */ "reverse_unordered_selects",
129381 /* ePragTyp: */ PragTyp_FLAG,
129382 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
129383 /* ColNames: */ 0, 0,
129384 /* iArg: */ SQLITE_ReverseOrder },
129385#endif
129386#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
129387 {/* zName: */ "schema_version",
129388 /* ePragTyp: */ PragTyp_HEADER_VALUE,
129389 /* ePragFlg: */ PragFlg_NoColumns1|PragFlg_Result0,
129390 /* ColNames: */ 0, 0,
129391 /* iArg: */ BTREE_SCHEMA_VERSION },
129392#endif
129393#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
129394 {/* zName: */ "secure_delete",
129395 /* ePragTyp: */ PragTyp_SECURE_DELETE,
129396 /* ePragFlg: */ PragFlg_Result0,
129397 /* ColNames: */ 0, 0,
129398 /* iArg: */ 0 },
129399#endif
129400#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
129401 {/* zName: */ "short_column_names",
129402 /* ePragTyp: */ PragTyp_FLAG,
129403 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
129404 /* ColNames: */ 0, 0,
129405 /* iArg: */ SQLITE_ShortColNames },
129406#endif
129407 {/* zName: */ "shrink_memory",
129408 /* ePragTyp: */ PragTyp_SHRINK_MEMORY,
129409 /* ePragFlg: */ PragFlg_NoColumns,
129410 /* ColNames: */ 0, 0,
129411 /* iArg: */ 0 },
129412 {/* zName: */ "soft_heap_limit",
129413 /* ePragTyp: */ PragTyp_SOFT_HEAP_LIMIT,
129414 /* ePragFlg: */ PragFlg_Result0,
129415 /* ColNames: */ 0, 0,
129416 /* iArg: */ 0 },
129417#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
129418#if defined(SQLITE_DEBUG)
129419 {/* zName: */ "sql_trace",
129420 /* ePragTyp: */ PragTyp_FLAG,
129421 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
129422 /* ColNames: */ 0, 0,
129423 /* iArg: */ SQLITE_SqlTrace },
129424#endif
129425#endif
129426#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && defined(SQLITE_DEBUG)
129427 {/* zName: */ "stats",
129428 /* ePragTyp: */ PragTyp_STATS,
129429 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
129430 /* ColNames: */ 33, 5,
129431 /* iArg: */ 0 },
129432#endif
129433#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
129434 {/* zName: */ "synchronous",
129435 /* ePragTyp: */ PragTyp_SYNCHRONOUS,
129436 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
129437 /* ColNames: */ 0, 0,
129438 /* iArg: */ 0 },
129439#endif
129440#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
129441 {/* zName: */ "table_info",
129442 /* ePragTyp: */ PragTyp_TABLE_INFO,
129443 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
129444 /* ColNames: */ 8, 6,
129445 /* iArg: */ 0 },
129446 {/* zName: */ "table_list",
129447 /* ePragTyp: */ PragTyp_TABLE_LIST,
129448 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1,
129449 /* ColNames: */ 15, 6,
129450 /* iArg: */ 0 },
129451 {/* zName: */ "table_xinfo",
129452 /* ePragTyp: */ PragTyp_TABLE_INFO,
129453 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
129454 /* ColNames: */ 8, 7,
129455 /* iArg: */ 1 },
129456#endif
129457#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
129458 {/* zName: */ "temp_store",
129459 /* ePragTyp: */ PragTyp_TEMP_STORE,
129460 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
129461 /* ColNames: */ 0, 0,
129462 /* iArg: */ 0 },
129463 {/* zName: */ "temp_store_directory",
129464 /* ePragTyp: */ PragTyp_TEMP_STORE_DIRECTORY,
129465 /* ePragFlg: */ PragFlg_NoColumns1,
129466 /* ColNames: */ 0, 0,
129467 /* iArg: */ 0 },
129468#endif
129469 {/* zName: */ "threads",
129470 /* ePragTyp: */ PragTyp_THREADS,
129471 /* ePragFlg: */ PragFlg_Result0,
129472 /* ColNames: */ 0, 0,
129473 /* iArg: */ 0 },
129474#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
129475 {/* zName: */ "trusted_schema",
129476 /* ePragTyp: */ PragTyp_FLAG,
129477 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
129478 /* ColNames: */ 0, 0,
129479 /* iArg: */ SQLITE_TrustedSchema },
129480#endif
129481#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
129482 {/* zName: */ "user_version",
129483 /* ePragTyp: */ PragTyp_HEADER_VALUE,
129484 /* ePragFlg: */ PragFlg_NoColumns1|PragFlg_Result0,
129485 /* ColNames: */ 0, 0,
129486 /* iArg: */ BTREE_USER_VERSION },
129487#endif
129488#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
129489#if defined(SQLITE_DEBUG)
129490 {/* zName: */ "vdbe_addoptrace",
129491 /* ePragTyp: */ PragTyp_FLAG,
129492 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
129493 /* ColNames: */ 0, 0,
129494 /* iArg: */ SQLITE_VdbeAddopTrace },
129495 {/* zName: */ "vdbe_debug",
129496 /* ePragTyp: */ PragTyp_FLAG,
129497 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
129498 /* ColNames: */ 0, 0,
129499 /* iArg: */ SQLITE_SqlTrace|SQLITE_VdbeListing|SQLITE_VdbeTrace },
129500 {/* zName: */ "vdbe_eqp",
129501 /* ePragTyp: */ PragTyp_FLAG,
129502 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
129503 /* ColNames: */ 0, 0,
129504 /* iArg: */ SQLITE_VdbeEQP },
129505 {/* zName: */ "vdbe_listing",
129506 /* ePragTyp: */ PragTyp_FLAG,
129507 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
129508 /* ColNames: */ 0, 0,
129509 /* iArg: */ SQLITE_VdbeListing },
129510 {/* zName: */ "vdbe_trace",
129511 /* ePragTyp: */ PragTyp_FLAG,
129512 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
129513 /* ColNames: */ 0, 0,
129514 /* iArg: */ SQLITE_VdbeTrace },
129515#endif
129516#endif
129517#if !defined(SQLITE_OMIT_WAL)
129518 {/* zName: */ "wal_autocheckpoint",
129519 /* ePragTyp: */ PragTyp_WAL_AUTOCHECKPOINT,
129520 /* ePragFlg: */ 0,
129521 /* ColNames: */ 0, 0,
129522 /* iArg: */ 0 },
129523 {/* zName: */ "wal_checkpoint",
129524 /* ePragTyp: */ PragTyp_WAL_CHECKPOINT,
129525 /* ePragFlg: */ PragFlg_NeedSchema,
129526 /* ColNames: */ 50, 3,
129527 /* iArg: */ 0 },
129528#endif
129529#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
129530 {/* zName: */ "writable_schema",
129531 /* ePragTyp: */ PragTyp_FLAG,
129532 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
129533 /* ColNames: */ 0, 0,
129534 /* iArg: */ SQLITE_WriteSchema|SQLITE_NoSchemaError },
129535#endif
129536};
129537/* Number of pragmas: 68 on by default, 78 total. */
129538
129539/************** End of pragma.h **********************************************/
129540/************** Continuing where we left off in pragma.c *********************/
129541
129542/*
129543** Interpret the given string as a safety level. Return 0 for OFF,
129544** 1 for ON or NORMAL, 2 for FULL, and 3 for EXTRA. Return 1 for an empty or
129545** unrecognized string argument. The FULL and EXTRA option is disallowed
129546** if the omitFull parameter it 1.
129547**
129548** Note that the values returned are one less that the values that
129549** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
129550** to support legacy SQL code. The safety level used to be boolean
129551** and older scripts may have used numbers 0 for OFF and 1 for ON.
129552*/
129553static u8 getSafetyLevel(const char *z, int omitFull, u8 dflt){
129554 /* 123456789 123456789 123 */
129555 static const char zText[] = "onoffalseyestruextrafull";
129556 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 15, 20};
129557 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 5, 4};
129558 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 3, 2};
129559 /* on no off false yes true extra full */
129560 int i, n;
129561 if( sqlite3Isdigit(*z) ){
129562 return (u8)sqlite3Atoi(z);
129563 }
129564 n = sqlite3Strlen30(z);
129565 for(i=0; i<ArraySize(iLength); i++){
129566 if( iLength[i]==n && sqlite3StrNICmp(zLeft: &zText[iOffset[i]],zRight: z,N: n)==0
129567 && (!omitFull || iValue[i]<=1)
129568 ){
129569 return iValue[i];
129570 }
129571 }
129572 return dflt;
129573}
129574
129575/*
129576** Interpret the given string as a boolean value.
129577*/
129578SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z, u8 dflt){
129579 return getSafetyLevel(z,omitFull: 1,dflt)!=0;
129580}
129581
129582/* The sqlite3GetBoolean() function is used by other modules but the
129583** remainder of this file is specific to PRAGMA processing. So omit
129584** the rest of the file if PRAGMAs are omitted from the build.
129585*/
129586#if !defined(SQLITE_OMIT_PRAGMA)
129587
129588/*
129589** Interpret the given string as a locking mode value.
129590*/
129591static int getLockingMode(const char *z){
129592 if( z ){
129593 if( 0==sqlite3StrICmp(zLeft: z, zRight: "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
129594 if( 0==sqlite3StrICmp(zLeft: z, zRight: "normal") ) return PAGER_LOCKINGMODE_NORMAL;
129595 }
129596 return PAGER_LOCKINGMODE_QUERY;
129597}
129598
129599#ifndef SQLITE_OMIT_AUTOVACUUM
129600/*
129601** Interpret the given string as an auto-vacuum mode value.
129602**
129603** The following strings, "none", "full" and "incremental" are
129604** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
129605*/
129606static int getAutoVacuum(const char *z){
129607 int i;
129608 if( 0==sqlite3StrICmp(zLeft: z, zRight: "none") ) return BTREE_AUTOVACUUM_NONE;
129609 if( 0==sqlite3StrICmp(zLeft: z, zRight: "full") ) return BTREE_AUTOVACUUM_FULL;
129610 if( 0==sqlite3StrICmp(zLeft: z, zRight: "incremental") ) return BTREE_AUTOVACUUM_INCR;
129611 i = sqlite3Atoi(z);
129612 return (u8)((i>=0&&i<=2)?i:0);
129613}
129614#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
129615
129616#ifndef SQLITE_OMIT_PAGER_PRAGMAS
129617/*
129618** Interpret the given string as a temp db location. Return 1 for file
129619** backed temporary databases, 2 for the Red-Black tree in memory database
129620** and 0 to use the compile-time default.
129621*/
129622static int getTempStore(const char *z){
129623 if( z[0]>='0' && z[0]<='2' ){
129624 return z[0] - '0';
129625 }else if( sqlite3StrICmp(zLeft: z, zRight: "file")==0 ){
129626 return 1;
129627 }else if( sqlite3StrICmp(zLeft: z, zRight: "memory")==0 ){
129628 return 2;
129629 }else{
129630 return 0;
129631 }
129632}
129633#endif /* SQLITE_PAGER_PRAGMAS */
129634
129635#ifndef SQLITE_OMIT_PAGER_PRAGMAS
129636/*
129637** Invalidate temp storage, either when the temp storage is changed
129638** from default, or when 'file' and the temp_store_directory has changed
129639*/
129640static int invalidateTempStorage(Parse *pParse){
129641 sqlite3 *db = pParse->db;
129642 if( db->aDb[1].pBt!=0 ){
129643 if( !db->autoCommit
129644 || sqlite3BtreeTxnState(p: db->aDb[1].pBt)!=SQLITE_TXN_NONE
129645 ){
129646 sqlite3ErrorMsg(pParse, zFormat: "temporary storage cannot be changed "
129647 "from within a transaction");
129648 return SQLITE_ERROR;
129649 }
129650 sqlite3BtreeClose(p: db->aDb[1].pBt);
129651 db->aDb[1].pBt = 0;
129652 sqlite3ResetAllSchemasOfConnection(db);
129653 }
129654 return SQLITE_OK;
129655}
129656#endif /* SQLITE_PAGER_PRAGMAS */
129657
129658#ifndef SQLITE_OMIT_PAGER_PRAGMAS
129659/*
129660** If the TEMP database is open, close it and mark the database schema
129661** as needing reloading. This must be done when using the SQLITE_TEMP_STORE
129662** or DEFAULT_TEMP_STORE pragmas.
129663*/
129664static int changeTempStorage(Parse *pParse, const char *zStorageType){
129665 int ts = getTempStore(z: zStorageType);
129666 sqlite3 *db = pParse->db;
129667 if( db->temp_store==ts ) return SQLITE_OK;
129668 if( invalidateTempStorage( pParse ) != SQLITE_OK ){
129669 return SQLITE_ERROR;
129670 }
129671 db->temp_store = (u8)ts;
129672 return SQLITE_OK;
129673}
129674#endif /* SQLITE_PAGER_PRAGMAS */
129675
129676/*
129677** Set result column names for a pragma.
129678*/
129679static void setPragmaResultColumnNames(
129680 Vdbe *v, /* The query under construction */
129681 const PragmaName *pPragma /* The pragma */
129682){
129683 u8 n = pPragma->nPragCName;
129684 sqlite3VdbeSetNumCols(p: v, nResColumn: n==0 ? 1 : n);
129685 if( n==0 ){
129686 sqlite3VdbeSetColName(p: v, idx: 0, COLNAME_NAME, zName: pPragma->zName, SQLITE_STATIC);
129687 }else{
129688 int i, j;
129689 for(i=0, j=pPragma->iPragCName; i<n; i++, j++){
129690 sqlite3VdbeSetColName(p: v, idx: i, COLNAME_NAME, zName: pragCName[j], SQLITE_STATIC);
129691 }
129692 }
129693}
129694
129695/*
129696** Generate code to return a single integer value.
129697*/
129698static void returnSingleInt(Vdbe *v, i64 value){
129699 sqlite3VdbeAddOp4Dup8(p: v, OP_Int64, p1: 0, p2: 1, p3: 0, zP4: (const u8*)&value, P4_INT64);
129700 sqlite3VdbeAddOp2(p: v, OP_ResultRow, p1: 1, p2: 1);
129701}
129702
129703/*
129704** Generate code to return a single text value.
129705*/
129706static void returnSingleText(
129707 Vdbe *v, /* Prepared statement under construction */
129708 const char *zValue /* Value to be returned */
129709){
129710 if( zValue ){
129711 sqlite3VdbeLoadString(p: v, iDest: 1, zStr: (const char*)zValue);
129712 sqlite3VdbeAddOp2(p: v, OP_ResultRow, p1: 1, p2: 1);
129713 }
129714}
129715
129716
129717/*
129718** Set the safety_level and pager flags for pager iDb. Or if iDb<0
129719** set these values for all pagers.
129720*/
129721#ifndef SQLITE_OMIT_PAGER_PRAGMAS
129722static void setAllPagerFlags(sqlite3 *db){
129723 if( db->autoCommit ){
129724 Db *pDb = db->aDb;
129725 int n = db->nDb;
129726 assert( SQLITE_FullFSync==PAGER_FULLFSYNC );
129727 assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC );
129728 assert( SQLITE_CacheSpill==PAGER_CACHESPILL );
129729 assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL)
129730 == PAGER_FLAGS_MASK );
129731 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
129732 while( (n--) > 0 ){
129733 if( pDb->pBt ){
129734 sqlite3BtreeSetPagerFlags(p: pDb->pBt,
129735 pgFlags: pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
129736 }
129737 pDb++;
129738 }
129739 }
129740}
129741#else
129742# define setAllPagerFlags(X) /* no-op */
129743#endif
129744
129745
129746/*
129747** Return a human-readable name for a constraint resolution action.
129748*/
129749#ifndef SQLITE_OMIT_FOREIGN_KEY
129750static const char *actionName(u8 action){
129751 const char *zName;
129752 switch( action ){
129753 case OE_SetNull: zName = "SET NULL"; break;
129754 case OE_SetDflt: zName = "SET DEFAULT"; break;
129755 case OE_Cascade: zName = "CASCADE"; break;
129756 case OE_Restrict: zName = "RESTRICT"; break;
129757 default: zName = "NO ACTION";
129758 assert( action==OE_None ); break;
129759 }
129760 return zName;
129761}
129762#endif
129763
129764
129765/*
129766** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
129767** defined in pager.h. This function returns the associated lowercase
129768** journal-mode name.
129769*/
129770SQLITE_PRIVATE const char *sqlite3JournalModename(int eMode){
129771 static char * const azModeName[] = {
129772 "delete", "persist", "off", "truncate", "memory"
129773#ifndef SQLITE_OMIT_WAL
129774 , "wal"
129775#endif
129776 };
129777 assert( PAGER_JOURNALMODE_DELETE==0 );
129778 assert( PAGER_JOURNALMODE_PERSIST==1 );
129779 assert( PAGER_JOURNALMODE_OFF==2 );
129780 assert( PAGER_JOURNALMODE_TRUNCATE==3 );
129781 assert( PAGER_JOURNALMODE_MEMORY==4 );
129782 assert( PAGER_JOURNALMODE_WAL==5 );
129783 assert( eMode>=0 && eMode<=ArraySize(azModeName) );
129784
129785 if( eMode==ArraySize(azModeName) ) return 0;
129786 return azModeName[eMode];
129787}
129788
129789/*
129790** Locate a pragma in the aPragmaName[] array.
129791*/
129792static const PragmaName *pragmaLocate(const char *zName){
129793 int upr, lwr, mid = 0, rc;
129794 lwr = 0;
129795 upr = ArraySize(aPragmaName)-1;
129796 while( lwr<=upr ){
129797 mid = (lwr+upr)/2;
129798 rc = sqlite3_stricmp(zLeft: zName, zRight: aPragmaName[mid].zName);
129799 if( rc==0 ) break;
129800 if( rc<0 ){
129801 upr = mid - 1;
129802 }else{
129803 lwr = mid + 1;
129804 }
129805 }
129806 return lwr>upr ? 0 : &aPragmaName[mid];
129807}
129808
129809/*
129810** Create zero or more entries in the output for the SQL functions
129811** defined by FuncDef p.
129812*/
129813static void pragmaFunclistLine(
129814 Vdbe *v, /* The prepared statement being created */
129815 FuncDef *p, /* A particular function definition */
129816 int isBuiltin, /* True if this is a built-in function */
129817 int showInternFuncs /* True if showing internal functions */
129818){
129819 for(; p; p=p->pNext){
129820 const char *zType;
129821 static const u32 mask =
129822 SQLITE_DETERMINISTIC |
129823 SQLITE_DIRECTONLY |
129824 SQLITE_SUBTYPE |
129825 SQLITE_INNOCUOUS |
129826 SQLITE_FUNC_INTERNAL
129827 ;
129828 static const char *azEnc[] = { 0, "utf8", "utf16le", "utf16be" };
129829
129830 assert( SQLITE_FUNC_ENCMASK==0x3 );
129831 assert( strcmp(azEnc[SQLITE_UTF8],"utf8")==0 );
129832 assert( strcmp(azEnc[SQLITE_UTF16LE],"utf16le")==0 );
129833 assert( strcmp(azEnc[SQLITE_UTF16BE],"utf16be")==0 );
129834
129835 if( p->xSFunc==0 ) continue;
129836 if( (p->funcFlags & SQLITE_FUNC_INTERNAL)!=0
129837 && showInternFuncs==0
129838 ){
129839 continue;
129840 }
129841 if( p->xValue!=0 ){
129842 zType = "w";
129843 }else if( p->xFinalize!=0 ){
129844 zType = "a";
129845 }else{
129846 zType = "s";
129847 }
129848 sqlite3VdbeMultiLoad(p: v, iDest: 1, zTypes: "sissii",
129849 p->zName, isBuiltin,
129850 zType, azEnc[p->funcFlags&SQLITE_FUNC_ENCMASK],
129851 p->nArg,
129852 (p->funcFlags & mask) ^ SQLITE_INNOCUOUS
129853 );
129854 }
129855}
129856
129857
129858/*
129859** Helper subroutine for PRAGMA integrity_check:
129860**
129861** Generate code to output a single-column result row with a value of the
129862** string held in register 3. Decrement the result count in register 1
129863** and halt if the maximum number of result rows have been issued.
129864*/
129865static int integrityCheckResultRow(Vdbe *v){
129866 int addr;
129867 sqlite3VdbeAddOp2(p: v, OP_ResultRow, p1: 3, p2: 1);
129868 addr = sqlite3VdbeAddOp3(p: v, OP_IfPos, p1: 1, p2: sqlite3VdbeCurrentAddr(p: v)+2, p3: 1);
129869 VdbeCoverage(v);
129870 sqlite3VdbeAddOp0(p: v, OP_Halt);
129871 return addr;
129872}
129873
129874/*
129875** Process a pragma statement.
129876**
129877** Pragmas are of this form:
129878**
129879** PRAGMA [schema.]id [= value]
129880**
129881** The identifier might also be a string. The value is a string, and
129882** identifier, or a number. If minusFlag is true, then the value is
129883** a number that was preceded by a minus sign.
129884**
129885** If the left side is "database.id" then pId1 is the database name
129886** and pId2 is the id. If the left side is just "id" then pId1 is the
129887** id and pId2 is any empty string.
129888*/
129889SQLITE_PRIVATE void sqlite3Pragma(
129890 Parse *pParse,
129891 Token *pId1, /* First part of [schema.]id field */
129892 Token *pId2, /* Second part of [schema.]id field, or NULL */
129893 Token *pValue, /* Token for <value>, or NULL */
129894 int minusFlag /* True if a '-' sign preceded <value> */
129895){
129896 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
129897 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
129898 const char *zDb = 0; /* The database name */
129899 Token *pId; /* Pointer to <id> token */
129900 char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */
129901 int iDb; /* Database index for <database> */
129902 int rc; /* return value form SQLITE_FCNTL_PRAGMA */
129903 sqlite3 *db = pParse->db; /* The database connection */
129904 Db *pDb; /* The specific database being pragmaed */
129905 Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */
129906 const PragmaName *pPragma; /* The pragma */
129907
129908 if( v==0 ) return;
129909 sqlite3VdbeRunOnlyOnce(p: v);
129910 pParse->nMem = 2;
129911
129912 /* Interpret the [schema.] part of the pragma statement. iDb is the
129913 ** index of the database this pragma is being applied to in db.aDb[]. */
129914 iDb = sqlite3TwoPartName(pParse, pName1: pId1, pName2: pId2, pUnqual: &pId);
129915 if( iDb<0 ) return;
129916 pDb = &db->aDb[iDb];
129917
129918 /* If the temp database has been explicitly named as part of the
129919 ** pragma, make sure it is open.
129920 */
129921 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
129922 return;
129923 }
129924
129925 zLeft = sqlite3NameFromToken(db, pName: pId);
129926 if( !zLeft ) return;
129927 if( minusFlag ){
129928 zRight = sqlite3MPrintf(db, zFormat: "-%T", pValue);
129929 }else{
129930 zRight = sqlite3NameFromToken(db, pName: pValue);
129931 }
129932
129933 assert( pId2 );
129934 zDb = pId2->n>0 ? pDb->zDbSName : 0;
129935 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zArg1: zLeft, zArg2: zRight, zArg3: zDb) ){
129936 goto pragma_out;
129937 }
129938
129939 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
129940 ** connection. If it returns SQLITE_OK, then assume that the VFS
129941 ** handled the pragma and generate a no-op prepared statement.
129942 **
129943 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
129944 ** an SQLITE_FCNTL_PRAGMA file control is sent to the open sqlite3_file
129945 ** object corresponding to the database file to which the pragma
129946 ** statement refers.
129947 **
129948 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
129949 ** file control is an array of pointers to strings (char**) in which the
129950 ** second element of the array is the name of the pragma and the third
129951 ** element is the argument to the pragma or NULL if the pragma has no
129952 ** argument.
129953 */
129954 aFcntl[0] = 0;
129955 aFcntl[1] = zLeft;
129956 aFcntl[2] = zRight;
129957 aFcntl[3] = 0;
129958 db->busyHandler.nBusy = 0;
129959 rc = sqlite3_file_control(db, zDbName: zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
129960 if( rc==SQLITE_OK ){
129961 sqlite3VdbeSetNumCols(p: v, nResColumn: 1);
129962 sqlite3VdbeSetColName(p: v, idx: 0, COLNAME_NAME, zName: aFcntl[0], SQLITE_TRANSIENT);
129963 returnSingleText(v, zValue: aFcntl[0]);
129964 sqlite3_free(p: aFcntl[0]);
129965 goto pragma_out;
129966 }
129967 if( rc!=SQLITE_NOTFOUND ){
129968 if( aFcntl[0] ){
129969 sqlite3ErrorMsg(pParse, zFormat: "%s", aFcntl[0]);
129970 sqlite3_free(p: aFcntl[0]);
129971 }
129972 pParse->nErr++;
129973 pParse->rc = rc;
129974 goto pragma_out;
129975 }
129976
129977 /* Locate the pragma in the lookup table */
129978 pPragma = pragmaLocate(zName: zLeft);
129979 if( pPragma==0 ){
129980 /* IMP: R-43042-22504 No error messages are generated if an
129981 ** unknown pragma is issued. */
129982 goto pragma_out;
129983 }
129984
129985 /* Make sure the database schema is loaded if the pragma requires that */
129986 if( (pPragma->mPragFlg & PragFlg_NeedSchema)!=0 ){
129987 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
129988 }
129989
129990 /* Register the result column names for pragmas that return results */
129991 if( (pPragma->mPragFlg & PragFlg_NoColumns)==0
129992 && ((pPragma->mPragFlg & PragFlg_NoColumns1)==0 || zRight==0)
129993 ){
129994 setPragmaResultColumnNames(v, pPragma);
129995 }
129996
129997 /* Jump to the appropriate pragma handler */
129998 switch( pPragma->ePragTyp ){
129999
130000#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
130001 /*
130002 ** PRAGMA [schema.]default_cache_size
130003 ** PRAGMA [schema.]default_cache_size=N
130004 **
130005 ** The first form reports the current persistent setting for the
130006 ** page cache size. The value returned is the maximum number of
130007 ** pages in the page cache. The second form sets both the current
130008 ** page cache size value and the persistent page cache size value
130009 ** stored in the database file.
130010 **
130011 ** Older versions of SQLite would set the default cache size to a
130012 ** negative number to indicate synchronous=OFF. These days, synchronous
130013 ** is always on by default regardless of the sign of the default cache
130014 ** size. But continue to take the absolute value of the default cache
130015 ** size of historical compatibility.
130016 */
130017 case PragTyp_DEFAULT_CACHE_SIZE: {
130018 static const int iLn = VDBE_OFFSET_LINENO(2);
130019 static const VdbeOpList getCacheSize[] = {
130020 { OP_Transaction, 0, 0, 0}, /* 0 */
130021 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
130022 { OP_IfPos, 1, 8, 0},
130023 { OP_Integer, 0, 2, 0},
130024 { OP_Subtract, 1, 2, 1},
130025 { OP_IfPos, 1, 8, 0},
130026 { OP_Integer, 0, 1, 0}, /* 6 */
130027 { OP_Noop, 0, 0, 0},
130028 { OP_ResultRow, 1, 1, 0},
130029 };
130030 VdbeOp *aOp;
130031 sqlite3VdbeUsesBtree(p: v, i: iDb);
130032 if( !zRight ){
130033 pParse->nMem += 2;
130034 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(getCacheSize));
130035 aOp = sqlite3VdbeAddOpList(p: v, ArraySize(getCacheSize), aOp: getCacheSize, iLineno: iLn);
130036 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
130037 aOp[0].p1 = iDb;
130038 aOp[1].p1 = iDb;
130039 aOp[6].p1 = SQLITE_DEFAULT_CACHE_SIZE;
130040 }else{
130041 int size = sqlite3AbsInt32(x: sqlite3Atoi(z: zRight));
130042 sqlite3BeginWriteOperation(pParse, setStatement: 0, iDb);
130043 sqlite3VdbeAddOp3(p: v, OP_SetCookie, p1: iDb, BTREE_DEFAULT_CACHE_SIZE, p3: size);
130044 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
130045 pDb->pSchema->cache_size = size;
130046 sqlite3BtreeSetCacheSize(p: pDb->pBt, mxPage: pDb->pSchema->cache_size);
130047 }
130048 break;
130049 }
130050#endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
130051
130052#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
130053 /*
130054 ** PRAGMA [schema.]page_size
130055 ** PRAGMA [schema.]page_size=N
130056 **
130057 ** The first form reports the current setting for the
130058 ** database page size in bytes. The second form sets the
130059 ** database page size value. The value can only be set if
130060 ** the database has not yet been created.
130061 */
130062 case PragTyp_PAGE_SIZE: {
130063 Btree *pBt = pDb->pBt;
130064 assert( pBt!=0 );
130065 if( !zRight ){
130066 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(p: pBt) : 0;
130067 returnSingleInt(v, value: size);
130068 }else{
130069 /* Malloc may fail when setting the page-size, as there is an internal
130070 ** buffer that the pager module resizes using sqlite3_realloc().
130071 */
130072 db->nextPagesize = sqlite3Atoi(z: zRight);
130073 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(p: pBt, pageSize: db->nextPagesize,nReserve: 0,iFix: 0) ){
130074 sqlite3OomFault(db);
130075 }
130076 }
130077 break;
130078 }
130079
130080 /*
130081 ** PRAGMA [schema.]secure_delete
130082 ** PRAGMA [schema.]secure_delete=ON/OFF/FAST
130083 **
130084 ** The first form reports the current setting for the
130085 ** secure_delete flag. The second form changes the secure_delete
130086 ** flag setting and reports the new value.
130087 */
130088 case PragTyp_SECURE_DELETE: {
130089 Btree *pBt = pDb->pBt;
130090 int b = -1;
130091 assert( pBt!=0 );
130092 if( zRight ){
130093 if( sqlite3_stricmp(zLeft: zRight, zRight: "fast")==0 ){
130094 b = 2;
130095 }else{
130096 b = sqlite3GetBoolean(z: zRight, dflt: 0);
130097 }
130098 }
130099 if( pId2->n==0 && b>=0 ){
130100 int ii;
130101 for(ii=0; ii<db->nDb; ii++){
130102 sqlite3BtreeSecureDelete(p: db->aDb[ii].pBt, newFlag: b);
130103 }
130104 }
130105 b = sqlite3BtreeSecureDelete(p: pBt, newFlag: b);
130106 returnSingleInt(v, value: b);
130107 break;
130108 }
130109
130110 /*
130111 ** PRAGMA [schema.]max_page_count
130112 ** PRAGMA [schema.]max_page_count=N
130113 **
130114 ** The first form reports the current setting for the
130115 ** maximum number of pages in the database file. The
130116 ** second form attempts to change this setting. Both
130117 ** forms return the current setting.
130118 **
130119 ** The absolute value of N is used. This is undocumented and might
130120 ** change. The only purpose is to provide an easy way to test
130121 ** the sqlite3AbsInt32() function.
130122 **
130123 ** PRAGMA [schema.]page_count
130124 **
130125 ** Return the number of pages in the specified database.
130126 */
130127 case PragTyp_PAGE_COUNT: {
130128 int iReg;
130129 i64 x = 0;
130130 sqlite3CodeVerifySchema(pParse, iDb);
130131 iReg = ++pParse->nMem;
130132 if( sqlite3Tolower(zLeft[0])=='p' ){
130133 sqlite3VdbeAddOp2(p: v, OP_Pagecount, p1: iDb, p2: iReg);
130134 }else{
130135 if( zRight && sqlite3DecOrHexToI64(z: zRight,pOut: &x)==0 ){
130136 if( x<0 ) x = 0;
130137 else if( x>0xfffffffe ) x = 0xfffffffe;
130138 }else{
130139 x = 0;
130140 }
130141 sqlite3VdbeAddOp3(p: v, OP_MaxPgcnt, p1: iDb, p2: iReg, p3: (int)x);
130142 }
130143 sqlite3VdbeAddOp2(p: v, OP_ResultRow, p1: iReg, p2: 1);
130144 break;
130145 }
130146
130147 /*
130148 ** PRAGMA [schema.]locking_mode
130149 ** PRAGMA [schema.]locking_mode = (normal|exclusive)
130150 */
130151 case PragTyp_LOCKING_MODE: {
130152 const char *zRet = "normal";
130153 int eMode = getLockingMode(z: zRight);
130154
130155 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
130156 /* Simple "PRAGMA locking_mode;" statement. This is a query for
130157 ** the current default locking mode (which may be different to
130158 ** the locking-mode of the main database).
130159 */
130160 eMode = db->dfltLockMode;
130161 }else{
130162 Pager *pPager;
130163 if( pId2->n==0 ){
130164 /* This indicates that no database name was specified as part
130165 ** of the PRAGMA command. In this case the locking-mode must be
130166 ** set on all attached databases, as well as the main db file.
130167 **
130168 ** Also, the sqlite3.dfltLockMode variable is set so that
130169 ** any subsequently attached databases also use the specified
130170 ** locking mode.
130171 */
130172 int ii;
130173 assert(pDb==&db->aDb[0]);
130174 for(ii=2; ii<db->nDb; ii++){
130175 pPager = sqlite3BtreePager(p: db->aDb[ii].pBt);
130176 sqlite3PagerLockingMode(pPager, eMode);
130177 }
130178 db->dfltLockMode = (u8)eMode;
130179 }
130180 pPager = sqlite3BtreePager(p: pDb->pBt);
130181 eMode = sqlite3PagerLockingMode(pPager, eMode);
130182 }
130183
130184 assert( eMode==PAGER_LOCKINGMODE_NORMAL
130185 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
130186 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
130187 zRet = "exclusive";
130188 }
130189 returnSingleText(v, zValue: zRet);
130190 break;
130191 }
130192
130193 /*
130194 ** PRAGMA [schema.]journal_mode
130195 ** PRAGMA [schema.]journal_mode =
130196 ** (delete|persist|off|truncate|memory|wal|off)
130197 */
130198 case PragTyp_JOURNAL_MODE: {
130199 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
130200 int ii; /* Loop counter */
130201
130202 if( zRight==0 ){
130203 /* If there is no "=MODE" part of the pragma, do a query for the
130204 ** current mode */
130205 eMode = PAGER_JOURNALMODE_QUERY;
130206 }else{
130207 const char *zMode;
130208 int n = sqlite3Strlen30(z: zRight);
130209 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
130210 if( sqlite3StrNICmp(zLeft: zRight, zRight: zMode, N: n)==0 ) break;
130211 }
130212 if( !zMode ){
130213 /* If the "=MODE" part does not match any known journal mode,
130214 ** then do a query */
130215 eMode = PAGER_JOURNALMODE_QUERY;
130216 }
130217 if( eMode==PAGER_JOURNALMODE_OFF && (db->flags & SQLITE_Defensive)!=0 ){
130218 /* Do not allow journal-mode "OFF" in defensive since the database
130219 ** can become corrupted using ordinary SQL when the journal is off */
130220 eMode = PAGER_JOURNALMODE_QUERY;
130221 }
130222 }
130223 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
130224 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
130225 iDb = 0;
130226 pId2->n = 1;
130227 }
130228 for(ii=db->nDb-1; ii>=0; ii--){
130229 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
130230 sqlite3VdbeUsesBtree(p: v, i: ii);
130231 sqlite3VdbeAddOp3(p: v, OP_JournalMode, p1: ii, p2: 1, p3: eMode);
130232 }
130233 }
130234 sqlite3VdbeAddOp2(p: v, OP_ResultRow, p1: 1, p2: 1);
130235 break;
130236 }
130237
130238 /*
130239 ** PRAGMA [schema.]journal_size_limit
130240 ** PRAGMA [schema.]journal_size_limit=N
130241 **
130242 ** Get or set the size limit on rollback journal files.
130243 */
130244 case PragTyp_JOURNAL_SIZE_LIMIT: {
130245 Pager *pPager = sqlite3BtreePager(p: pDb->pBt);
130246 i64 iLimit = -2;
130247 if( zRight ){
130248 sqlite3DecOrHexToI64(z: zRight, pOut: &iLimit);
130249 if( iLimit<-1 ) iLimit = -1;
130250 }
130251 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
130252 returnSingleInt(v, value: iLimit);
130253 break;
130254 }
130255
130256#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
130257
130258 /*
130259 ** PRAGMA [schema.]auto_vacuum
130260 ** PRAGMA [schema.]auto_vacuum=N
130261 **
130262 ** Get or set the value of the database 'auto-vacuum' parameter.
130263 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
130264 */
130265#ifndef SQLITE_OMIT_AUTOVACUUM
130266 case PragTyp_AUTO_VACUUM: {
130267 Btree *pBt = pDb->pBt;
130268 assert( pBt!=0 );
130269 if( !zRight ){
130270 returnSingleInt(v, value: sqlite3BtreeGetAutoVacuum(p: pBt));
130271 }else{
130272 int eAuto = getAutoVacuum(z: zRight);
130273 assert( eAuto>=0 && eAuto<=2 );
130274 db->nextAutovac = (u8)eAuto;
130275 /* Call SetAutoVacuum() to set initialize the internal auto and
130276 ** incr-vacuum flags. This is required in case this connection
130277 ** creates the database file. It is important that it is created
130278 ** as an auto-vacuum capable db.
130279 */
130280 rc = sqlite3BtreeSetAutoVacuum(p: pBt, autoVacuum: eAuto);
130281 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
130282 /* When setting the auto_vacuum mode to either "full" or
130283 ** "incremental", write the value of meta[6] in the database
130284 ** file. Before writing to meta[6], check that meta[3] indicates
130285 ** that this really is an auto-vacuum capable database.
130286 */
130287 static const int iLn = VDBE_OFFSET_LINENO(2);
130288 static const VdbeOpList setMeta6[] = {
130289 { OP_Transaction, 0, 1, 0}, /* 0 */
130290 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
130291 { OP_If, 1, 0, 0}, /* 2 */
130292 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
130293 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 0}, /* 4 */
130294 };
130295 VdbeOp *aOp;
130296 int iAddr = sqlite3VdbeCurrentAddr(p: v);
130297 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setMeta6));
130298 aOp = sqlite3VdbeAddOpList(p: v, ArraySize(setMeta6), aOp: setMeta6, iLineno: iLn);
130299 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
130300 aOp[0].p1 = iDb;
130301 aOp[1].p1 = iDb;
130302 aOp[2].p2 = iAddr+4;
130303 aOp[4].p1 = iDb;
130304 aOp[4].p3 = eAuto - 1;
130305 sqlite3VdbeUsesBtree(p: v, i: iDb);
130306 }
130307 }
130308 break;
130309 }
130310#endif
130311
130312 /*
130313 ** PRAGMA [schema.]incremental_vacuum(N)
130314 **
130315 ** Do N steps of incremental vacuuming on a database.
130316 */
130317#ifndef SQLITE_OMIT_AUTOVACUUM
130318 case PragTyp_INCREMENTAL_VACUUM: {
130319 int iLimit, addr;
130320 if( zRight==0 || !sqlite3GetInt32(zNum: zRight, pValue: &iLimit) || iLimit<=0 ){
130321 iLimit = 0x7fffffff;
130322 }
130323 sqlite3BeginWriteOperation(pParse, setStatement: 0, iDb);
130324 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: iLimit, p2: 1);
130325 addr = sqlite3VdbeAddOp1(p: v, OP_IncrVacuum, p1: iDb); VdbeCoverage(v);
130326 sqlite3VdbeAddOp1(p: v, OP_ResultRow, p1: 1);
130327 sqlite3VdbeAddOp2(p: v, OP_AddImm, p1: 1, p2: -1);
130328 sqlite3VdbeAddOp2(p: v, OP_IfPos, p1: 1, p2: addr); VdbeCoverage(v);
130329 sqlite3VdbeJumpHere(p: v, addr);
130330 break;
130331 }
130332#endif
130333
130334#ifndef SQLITE_OMIT_PAGER_PRAGMAS
130335 /*
130336 ** PRAGMA [schema.]cache_size
130337 ** PRAGMA [schema.]cache_size=N
130338 **
130339 ** The first form reports the current local setting for the
130340 ** page cache size. The second form sets the local
130341 ** page cache size value. If N is positive then that is the
130342 ** number of pages in the cache. If N is negative, then the
130343 ** number of pages is adjusted so that the cache uses -N kibibytes
130344 ** of memory.
130345 */
130346 case PragTyp_CACHE_SIZE: {
130347 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
130348 if( !zRight ){
130349 returnSingleInt(v, value: pDb->pSchema->cache_size);
130350 }else{
130351 int size = sqlite3Atoi(z: zRight);
130352 pDb->pSchema->cache_size = size;
130353 sqlite3BtreeSetCacheSize(p: pDb->pBt, mxPage: pDb->pSchema->cache_size);
130354 }
130355 break;
130356 }
130357
130358 /*
130359 ** PRAGMA [schema.]cache_spill
130360 ** PRAGMA cache_spill=BOOLEAN
130361 ** PRAGMA [schema.]cache_spill=N
130362 **
130363 ** The first form reports the current local setting for the
130364 ** page cache spill size. The second form turns cache spill on
130365 ** or off. When turnning cache spill on, the size is set to the
130366 ** current cache_size. The third form sets a spill size that
130367 ** may be different form the cache size.
130368 ** If N is positive then that is the
130369 ** number of pages in the cache. If N is negative, then the
130370 ** number of pages is adjusted so that the cache uses -N kibibytes
130371 ** of memory.
130372 **
130373 ** If the number of cache_spill pages is less then the number of
130374 ** cache_size pages, no spilling occurs until the page count exceeds
130375 ** the number of cache_size pages.
130376 **
130377 ** The cache_spill=BOOLEAN setting applies to all attached schemas,
130378 ** not just the schema specified.
130379 */
130380 case PragTyp_CACHE_SPILL: {
130381 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
130382 if( !zRight ){
130383 returnSingleInt(v,
130384 value: (db->flags & SQLITE_CacheSpill)==0 ? 0 :
130385 sqlite3BtreeSetSpillSize(p: pDb->pBt,mxPage: 0));
130386 }else{
130387 int size = 1;
130388 if( sqlite3GetInt32(zNum: zRight, pValue: &size) ){
130389 sqlite3BtreeSetSpillSize(p: pDb->pBt, mxPage: size);
130390 }
130391 if( sqlite3GetBoolean(z: zRight, dflt: size!=0) ){
130392 db->flags |= SQLITE_CacheSpill;
130393 }else{
130394 db->flags &= ~(u64)SQLITE_CacheSpill;
130395 }
130396 setAllPagerFlags(db);
130397 }
130398 break;
130399 }
130400
130401 /*
130402 ** PRAGMA [schema.]mmap_size(N)
130403 **
130404 ** Used to set mapping size limit. The mapping size limit is
130405 ** used to limit the aggregate size of all memory mapped regions of the
130406 ** database file. If this parameter is set to zero, then memory mapping
130407 ** is not used at all. If N is negative, then the default memory map
130408 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
130409 ** The parameter N is measured in bytes.
130410 **
130411 ** This value is advisory. The underlying VFS is free to memory map
130412 ** as little or as much as it wants. Except, if N is set to 0 then the
130413 ** upper layers will never invoke the xFetch interfaces to the VFS.
130414 */
130415 case PragTyp_MMAP_SIZE: {
130416 sqlite3_int64 sz;
130417#if SQLITE_MAX_MMAP_SIZE>0
130418 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
130419 if( zRight ){
130420 int ii;
130421 sqlite3DecOrHexToI64(z: zRight, pOut: &sz);
130422 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
130423 if( pId2->n==0 ) db->szMmap = sz;
130424 for(ii=db->nDb-1; ii>=0; ii--){
130425 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
130426 sqlite3BtreeSetMmapLimit(p: db->aDb[ii].pBt, szMmap: sz);
130427 }
130428 }
130429 }
130430 sz = -1;
130431 rc = sqlite3_file_control(db, zDbName: zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
130432#else
130433 sz = 0;
130434 rc = SQLITE_OK;
130435#endif
130436 if( rc==SQLITE_OK ){
130437 returnSingleInt(v, value: sz);
130438 }else if( rc!=SQLITE_NOTFOUND ){
130439 pParse->nErr++;
130440 pParse->rc = rc;
130441 }
130442 break;
130443 }
130444
130445 /*
130446 ** PRAGMA temp_store
130447 ** PRAGMA temp_store = "default"|"memory"|"file"
130448 **
130449 ** Return or set the local value of the temp_store flag. Changing
130450 ** the local value does not make changes to the disk file and the default
130451 ** value will be restored the next time the database is opened.
130452 **
130453 ** Note that it is possible for the library compile-time options to
130454 ** override this setting
130455 */
130456 case PragTyp_TEMP_STORE: {
130457 if( !zRight ){
130458 returnSingleInt(v, value: db->temp_store);
130459 }else{
130460 changeTempStorage(pParse, zStorageType: zRight);
130461 }
130462 break;
130463 }
130464
130465 /*
130466 ** PRAGMA temp_store_directory
130467 ** PRAGMA temp_store_directory = ""|"directory_name"
130468 **
130469 ** Return or set the local value of the temp_store_directory flag. Changing
130470 ** the value sets a specific directory to be used for temporary files.
130471 ** Setting to a null string reverts to the default temporary directory search.
130472 ** If temporary directory is changed, then invalidateTempStorage.
130473 **
130474 */
130475 case PragTyp_TEMP_STORE_DIRECTORY: {
130476 if( !zRight ){
130477 returnSingleText(v, zValue: sqlite3_temp_directory);
130478 }else{
130479#ifndef SQLITE_OMIT_WSD
130480 if( zRight[0] ){
130481 int res;
130482 rc = sqlite3OsAccess(pVfs: db->pVfs, zPath: zRight, SQLITE_ACCESS_READWRITE, pResOut: &res);
130483 if( rc!=SQLITE_OK || res==0 ){
130484 sqlite3ErrorMsg(pParse, zFormat: "not a writable directory");
130485 goto pragma_out;
130486 }
130487 }
130488 if( SQLITE_TEMP_STORE==0
130489 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
130490 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
130491 ){
130492 invalidateTempStorage(pParse);
130493 }
130494 sqlite3_free(p: sqlite3_temp_directory);
130495 if( zRight[0] ){
130496 sqlite3_temp_directory = sqlite3_mprintf(zFormat: "%s", zRight);
130497 }else{
130498 sqlite3_temp_directory = 0;
130499 }
130500#endif /* SQLITE_OMIT_WSD */
130501 }
130502 break;
130503 }
130504
130505#if SQLITE_OS_WIN
130506 /*
130507 ** PRAGMA data_store_directory
130508 ** PRAGMA data_store_directory = ""|"directory_name"
130509 **
130510 ** Return or set the local value of the data_store_directory flag. Changing
130511 ** the value sets a specific directory to be used for database files that
130512 ** were specified with a relative pathname. Setting to a null string reverts
130513 ** to the default database directory, which for database files specified with
130514 ** a relative path will probably be based on the current directory for the
130515 ** process. Database file specified with an absolute path are not impacted
130516 ** by this setting, regardless of its value.
130517 **
130518 */
130519 case PragTyp_DATA_STORE_DIRECTORY: {
130520 if( !zRight ){
130521 returnSingleText(v, sqlite3_data_directory);
130522 }else{
130523#ifndef SQLITE_OMIT_WSD
130524 if( zRight[0] ){
130525 int res;
130526 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
130527 if( rc!=SQLITE_OK || res==0 ){
130528 sqlite3ErrorMsg(pParse, "not a writable directory");
130529 goto pragma_out;
130530 }
130531 }
130532 sqlite3_free(sqlite3_data_directory);
130533 if( zRight[0] ){
130534 sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
130535 }else{
130536 sqlite3_data_directory = 0;
130537 }
130538#endif /* SQLITE_OMIT_WSD */
130539 }
130540 break;
130541 }
130542#endif
130543
130544#if SQLITE_ENABLE_LOCKING_STYLE
130545 /*
130546 ** PRAGMA [schema.]lock_proxy_file
130547 ** PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path"
130548 **
130549 ** Return or set the value of the lock_proxy_file flag. Changing
130550 ** the value sets a specific file to be used for database access locks.
130551 **
130552 */
130553 case PragTyp_LOCK_PROXY_FILE: {
130554 if( !zRight ){
130555 Pager *pPager = sqlite3BtreePager(pDb->pBt);
130556 char *proxy_file_path = NULL;
130557 sqlite3_file *pFile = sqlite3PagerFile(pPager);
130558 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
130559 &proxy_file_path);
130560 returnSingleText(v, proxy_file_path);
130561 }else{
130562 Pager *pPager = sqlite3BtreePager(pDb->pBt);
130563 sqlite3_file *pFile = sqlite3PagerFile(pPager);
130564 int res;
130565 if( zRight[0] ){
130566 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
130567 zRight);
130568 } else {
130569 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
130570 NULL);
130571 }
130572 if( res!=SQLITE_OK ){
130573 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
130574 goto pragma_out;
130575 }
130576 }
130577 break;
130578 }
130579#endif /* SQLITE_ENABLE_LOCKING_STYLE */
130580
130581 /*
130582 ** PRAGMA [schema.]synchronous
130583 ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA
130584 **
130585 ** Return or set the local value of the synchronous flag. Changing
130586 ** the local value does not make changes to the disk file and the
130587 ** default value will be restored the next time the database is
130588 ** opened.
130589 */
130590 case PragTyp_SYNCHRONOUS: {
130591 if( !zRight ){
130592 returnSingleInt(v, value: pDb->safety_level-1);
130593 }else{
130594 if( !db->autoCommit ){
130595 sqlite3ErrorMsg(pParse,
130596 zFormat: "Safety level may not be changed inside a transaction");
130597 }else if( iDb!=1 ){
130598 int iLevel = (getSafetyLevel(z: zRight,omitFull: 0,dflt: 1)+1) & PAGER_SYNCHRONOUS_MASK;
130599 if( iLevel==0 ) iLevel = 1;
130600 pDb->safety_level = iLevel;
130601 pDb->bSyncSet = 1;
130602 setAllPagerFlags(db);
130603 }
130604 }
130605 break;
130606 }
130607#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
130608
130609#ifndef SQLITE_OMIT_FLAG_PRAGMAS
130610 case PragTyp_FLAG: {
130611 if( zRight==0 ){
130612 setPragmaResultColumnNames(v, pPragma);
130613 returnSingleInt(v, value: (db->flags & pPragma->iArg)!=0 );
130614 }else{
130615 u64 mask = pPragma->iArg; /* Mask of bits to set or clear. */
130616 if( db->autoCommit==0 ){
130617 /* Foreign key support may not be enabled or disabled while not
130618 ** in auto-commit mode. */
130619 mask &= ~(SQLITE_ForeignKeys);
130620 }
130621#if SQLITE_USER_AUTHENTICATION
130622 if( db->auth.authLevel==UAUTH_User ){
130623 /* Do not allow non-admin users to modify the schema arbitrarily */
130624 mask &= ~(SQLITE_WriteSchema);
130625 }
130626#endif
130627
130628 if( sqlite3GetBoolean(z: zRight, dflt: 0) ){
130629 db->flags |= mask;
130630 }else{
130631 db->flags &= ~mask;
130632 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
130633 if( (mask & SQLITE_WriteSchema)!=0
130634 && sqlite3_stricmp(zLeft: zRight, zRight: "reset")==0
130635 ){
130636 /* IMP: R-60817-01178 If the argument is "RESET" then schema
130637 ** writing is disabled (as with "PRAGMA writable_schema=OFF") and,
130638 ** in addition, the schema is reloaded. */
130639 sqlite3ResetAllSchemasOfConnection(db);
130640 }
130641 }
130642
130643 /* Many of the flag-pragmas modify the code generated by the SQL
130644 ** compiler (eg. count_changes). So add an opcode to expire all
130645 ** compiled SQL statements after modifying a pragma value.
130646 */
130647 sqlite3VdbeAddOp0(p: v, OP_Expire);
130648 setAllPagerFlags(db);
130649 }
130650 break;
130651 }
130652#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
130653
130654#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
130655 /*
130656 ** PRAGMA table_info(<table>)
130657 **
130658 ** Return a single row for each column of the named table. The columns of
130659 ** the returned data set are:
130660 **
130661 ** cid: Column id (numbered from left to right, starting at 0)
130662 ** name: Column name
130663 ** type: Column declaration type.
130664 ** notnull: True if 'NOT NULL' is part of column declaration
130665 ** dflt_value: The default value for the column, if any.
130666 ** pk: Non-zero for PK fields.
130667 */
130668 case PragTyp_TABLE_INFO: if( zRight ){
130669 Table *pTab;
130670 sqlite3CodeVerifyNamedSchema(pParse, zDb);
130671 pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zName: zRight, zDbase: zDb);
130672 if( pTab ){
130673 int i, k;
130674 int nHidden = 0;
130675 Column *pCol;
130676 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
130677 pParse->nMem = 7;
130678 sqlite3ViewGetColumnNames(pParse, pTable: pTab);
130679 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
130680 int isHidden = 0;
130681 const Expr *pColExpr;
130682 if( pCol->colFlags & COLFLAG_NOINSERT ){
130683 if( pPragma->iArg==0 ){
130684 nHidden++;
130685 continue;
130686 }
130687 if( pCol->colFlags & COLFLAG_VIRTUAL ){
130688 isHidden = 2; /* GENERATED ALWAYS AS ... VIRTUAL */
130689 }else if( pCol->colFlags & COLFLAG_STORED ){
130690 isHidden = 3; /* GENERATED ALWAYS AS ... STORED */
130691 }else{ assert( pCol->colFlags & COLFLAG_HIDDEN );
130692 isHidden = 1; /* HIDDEN */
130693 }
130694 }
130695 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
130696 k = 0;
130697 }else if( pPk==0 ){
130698 k = 1;
130699 }else{
130700 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
130701 }
130702 pColExpr = sqlite3ColumnExpr(pTab,pCol);
130703 assert( pColExpr==0 || pColExpr->op==TK_SPAN || isHidden>=2 );
130704 assert( pColExpr==0 || !ExprHasProperty(pColExpr, EP_IntValue)
130705 || isHidden>=2 );
130706 sqlite3VdbeMultiLoad(p: v, iDest: 1, zTypes: pPragma->iArg ? "issisii" : "issisi",
130707 i-nHidden,
130708 pCol->zCnName,
130709 sqlite3ColumnType(pCol,zDflt: ""),
130710 pCol->notNull ? 1 : 0,
130711 (isHidden>=2 || pColExpr==0) ? 0 : pColExpr->u.zToken,
130712 k,
130713 isHidden);
130714 }
130715 }
130716 }
130717 break;
130718
130719 /*
130720 ** PRAGMA table_list
130721 **
130722 ** Return a single row for each table, virtual table, or view in the
130723 ** entire schema.
130724 **
130725 ** schema: Name of attached database hold this table
130726 ** name: Name of the table itself
130727 ** type: "table", "view", "virtual", "shadow"
130728 ** ncol: Number of columns
130729 ** wr: True for a WITHOUT ROWID table
130730 ** strict: True for a STRICT table
130731 */
130732 case PragTyp_TABLE_LIST: {
130733 int ii;
130734 pParse->nMem = 6;
130735 sqlite3CodeVerifyNamedSchema(pParse, zDb);
130736 for(ii=0; ii<db->nDb; ii++){
130737 HashElem *k;
130738 Hash *pHash;
130739 int initNCol;
130740 if( zDb && sqlite3_stricmp(zLeft: zDb, zRight: db->aDb[ii].zDbSName)!=0 ) continue;
130741
130742 /* Ensure that the Table.nCol field is initialized for all views
130743 ** and virtual tables. Each time we initialize a Table.nCol value
130744 ** for a table, that can potentially disrupt the hash table, so restart
130745 ** the initialization scan.
130746 */
130747 pHash = &db->aDb[ii].pSchema->tblHash;
130748 initNCol = sqliteHashCount(pHash);
130749 while( initNCol-- ){
130750 for(k=sqliteHashFirst(pHash); 1; k=sqliteHashNext(k) ){
130751 Table *pTab;
130752 if( k==0 ){ initNCol = 0; break; }
130753 pTab = sqliteHashData(k);
130754 if( pTab->nCol==0 ){
130755 char *zSql = sqlite3MPrintf(db, zFormat: "SELECT*FROM\"%w\"", pTab->zName);
130756 if( zSql ){
130757 sqlite3_stmt *pDummy = 0;
130758 (void)sqlite3_prepare(db, zSql, nByte: -1, ppStmt: &pDummy, pzTail: 0);
130759 (void)sqlite3_finalize(pStmt: pDummy);
130760 sqlite3DbFree(db, p: zSql);
130761 }
130762 pHash = &db->aDb[ii].pSchema->tblHash;
130763 break;
130764 }
130765 }
130766 }
130767
130768 for(k=sqliteHashFirst(pHash); k; k=sqliteHashNext(k) ){
130769 Table *pTab = sqliteHashData(k);
130770 const char *zType;
130771 if( zRight && sqlite3_stricmp(zLeft: zRight, zRight: pTab->zName)!=0 ) continue;
130772 if( IsView(pTab) ){
130773 zType = "view";
130774 }else if( IsVirtual(pTab) ){
130775 zType = "virtual";
130776 }else if( pTab->tabFlags & TF_Shadow ){
130777 zType = "shadow";
130778 }else{
130779 zType = "table";
130780 }
130781 sqlite3VdbeMultiLoad(p: v, iDest: 1, zTypes: "sssiii",
130782 db->aDb[ii].zDbSName,
130783 sqlite3PreferredTableName(zName: pTab->zName),
130784 zType,
130785 pTab->nCol,
130786 (pTab->tabFlags & TF_WithoutRowid)!=0,
130787 (pTab->tabFlags & TF_Strict)!=0
130788 );
130789 }
130790 }
130791 }
130792 break;
130793
130794#ifdef SQLITE_DEBUG
130795 case PragTyp_STATS: {
130796 Index *pIdx;
130797 HashElem *i;
130798 pParse->nMem = 5;
130799 sqlite3CodeVerifySchema(pParse, iDb);
130800 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
130801 Table *pTab = sqliteHashData(i);
130802 sqlite3VdbeMultiLoad(v, 1, "ssiii",
130803 sqlite3PreferredTableName(pTab->zName),
130804 0,
130805 pTab->szTabRow,
130806 pTab->nRowLogEst,
130807 pTab->tabFlags);
130808 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
130809 sqlite3VdbeMultiLoad(v, 2, "siiiX",
130810 pIdx->zName,
130811 pIdx->szIdxRow,
130812 pIdx->aiRowLogEst[0],
130813 pIdx->hasStat1);
130814 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
130815 }
130816 }
130817 }
130818 break;
130819#endif
130820
130821 case PragTyp_INDEX_INFO: if( zRight ){
130822 Index *pIdx;
130823 Table *pTab;
130824 pIdx = sqlite3FindIndex(db, zName: zRight, zDb);
130825 if( pIdx==0 ){
130826 /* If there is no index named zRight, check to see if there is a
130827 ** WITHOUT ROWID table named zRight, and if there is, show the
130828 ** structure of the PRIMARY KEY index for that table. */
130829 pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zName: zRight, zDbase: zDb);
130830 if( pTab && !HasRowid(pTab) ){
130831 pIdx = sqlite3PrimaryKeyIndex(pTab);
130832 }
130833 }
130834 if( pIdx ){
130835 int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
130836 int i;
130837 int mx;
130838 if( pPragma->iArg ){
130839 /* PRAGMA index_xinfo (newer version with more rows and columns) */
130840 mx = pIdx->nColumn;
130841 pParse->nMem = 6;
130842 }else{
130843 /* PRAGMA index_info (legacy version) */
130844 mx = pIdx->nKeyCol;
130845 pParse->nMem = 3;
130846 }
130847 pTab = pIdx->pTable;
130848 sqlite3CodeVerifySchema(pParse, iDb: iIdxDb);
130849 assert( pParse->nMem<=pPragma->nPragCName );
130850 for(i=0; i<mx; i++){
130851 i16 cnum = pIdx->aiColumn[i];
130852 sqlite3VdbeMultiLoad(p: v, iDest: 1, zTypes: "iisX", i, cnum,
130853 cnum<0 ? 0 : pTab->aCol[cnum].zCnName);
130854 if( pPragma->iArg ){
130855 sqlite3VdbeMultiLoad(p: v, iDest: 4, zTypes: "isiX",
130856 pIdx->aSortOrder[i],
130857 pIdx->azColl[i],
130858 i<pIdx->nKeyCol);
130859 }
130860 sqlite3VdbeAddOp2(p: v, OP_ResultRow, p1: 1, p2: pParse->nMem);
130861 }
130862 }
130863 }
130864 break;
130865
130866 case PragTyp_INDEX_LIST: if( zRight ){
130867 Index *pIdx;
130868 Table *pTab;
130869 int i;
130870 pTab = sqlite3FindTable(db, zName: zRight, zDatabase: zDb);
130871 if( pTab ){
130872 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
130873 pParse->nMem = 5;
130874 sqlite3CodeVerifySchema(pParse, iDb: iTabDb);
130875 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
130876 const char *azOrigin[] = { "c", "u", "pk" };
130877 sqlite3VdbeMultiLoad(p: v, iDest: 1, zTypes: "isisi",
130878 i,
130879 pIdx->zName,
130880 IsUniqueIndex(pIdx),
130881 azOrigin[pIdx->idxType],
130882 pIdx->pPartIdxWhere!=0);
130883 }
130884 }
130885 }
130886 break;
130887
130888 case PragTyp_DATABASE_LIST: {
130889 int i;
130890 pParse->nMem = 3;
130891 for(i=0; i<db->nDb; i++){
130892 if( db->aDb[i].pBt==0 ) continue;
130893 assert( db->aDb[i].zDbSName!=0 );
130894 sqlite3VdbeMultiLoad(p: v, iDest: 1, zTypes: "iss",
130895 i,
130896 db->aDb[i].zDbSName,
130897 sqlite3BtreeGetFilename(p: db->aDb[i].pBt));
130898 }
130899 }
130900 break;
130901
130902 case PragTyp_COLLATION_LIST: {
130903 int i = 0;
130904 HashElem *p;
130905 pParse->nMem = 2;
130906 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
130907 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
130908 sqlite3VdbeMultiLoad(p: v, iDest: 1, zTypes: "is", i++, pColl->zName);
130909 }
130910 }
130911 break;
130912
130913#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
130914 case PragTyp_FUNCTION_LIST: {
130915 int i;
130916 HashElem *j;
130917 FuncDef *p;
130918 int showInternFunc = (db->mDbFlags & DBFLAG_InternalFunc)!=0;
130919 pParse->nMem = 6;
130920 for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){
130921 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
130922 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
130923 pragmaFunclistLine(v, p, isBuiltin: 1, showInternFuncs: showInternFunc);
130924 }
130925 }
130926 for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){
130927 p = (FuncDef*)sqliteHashData(j);
130928 assert( (p->funcFlags & SQLITE_FUNC_BUILTIN)==0 );
130929 pragmaFunclistLine(v, p, isBuiltin: 0, showInternFuncs: showInternFunc);
130930 }
130931 }
130932 break;
130933
130934#ifndef SQLITE_OMIT_VIRTUALTABLE
130935 case PragTyp_MODULE_LIST: {
130936 HashElem *j;
130937 pParse->nMem = 1;
130938 for(j=sqliteHashFirst(&db->aModule); j; j=sqliteHashNext(j)){
130939 Module *pMod = (Module*)sqliteHashData(j);
130940 sqlite3VdbeMultiLoad(p: v, iDest: 1, zTypes: "s", pMod->zName);
130941 }
130942 }
130943 break;
130944#endif /* SQLITE_OMIT_VIRTUALTABLE */
130945
130946 case PragTyp_PRAGMA_LIST: {
130947 int i;
130948 for(i=0; i<ArraySize(aPragmaName); i++){
130949 sqlite3VdbeMultiLoad(p: v, iDest: 1, zTypes: "s", aPragmaName[i].zName);
130950 }
130951 }
130952 break;
130953#endif /* SQLITE_INTROSPECTION_PRAGMAS */
130954
130955#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
130956
130957#ifndef SQLITE_OMIT_FOREIGN_KEY
130958 case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
130959 FKey *pFK;
130960 Table *pTab;
130961 pTab = sqlite3FindTable(db, zName: zRight, zDatabase: zDb);
130962 if( pTab && IsOrdinaryTable(pTab) ){
130963 pFK = pTab->u.tab.pFKey;
130964 if( pFK ){
130965 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
130966 int i = 0;
130967 pParse->nMem = 8;
130968 sqlite3CodeVerifySchema(pParse, iDb: iTabDb);
130969 while(pFK){
130970 int j;
130971 for(j=0; j<pFK->nCol; j++){
130972 sqlite3VdbeMultiLoad(p: v, iDest: 1, zTypes: "iissssss",
130973 i,
130974 j,
130975 pFK->zTo,
130976 pTab->aCol[pFK->aCol[j].iFrom].zCnName,
130977 pFK->aCol[j].zCol,
130978 actionName(action: pFK->aAction[1]), /* ON UPDATE */
130979 actionName(action: pFK->aAction[0]), /* ON DELETE */
130980 "NONE");
130981 }
130982 ++i;
130983 pFK = pFK->pNextFrom;
130984 }
130985 }
130986 }
130987 }
130988 break;
130989#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
130990
130991#ifndef SQLITE_OMIT_FOREIGN_KEY
130992#ifndef SQLITE_OMIT_TRIGGER
130993 case PragTyp_FOREIGN_KEY_CHECK: {
130994 FKey *pFK; /* A foreign key constraint */
130995 Table *pTab; /* Child table contain "REFERENCES" keyword */
130996 Table *pParent; /* Parent table that child points to */
130997 Index *pIdx; /* Index in the parent table */
130998 int i; /* Loop counter: Foreign key number for pTab */
130999 int j; /* Loop counter: Field of the foreign key */
131000 HashElem *k; /* Loop counter: Next table in schema */
131001 int x; /* result variable */
131002 int regResult; /* 3 registers to hold a result row */
131003 int regKey; /* Register to hold key for checking the FK */
131004 int regRow; /* Registers to hold a row from pTab */
131005 int addrTop; /* Top of a loop checking foreign keys */
131006 int addrOk; /* Jump here if the key is OK */
131007 int *aiCols; /* child to parent column mapping */
131008
131009 regResult = pParse->nMem+1;
131010 pParse->nMem += 4;
131011 regKey = ++pParse->nMem;
131012 regRow = ++pParse->nMem;
131013 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
131014 while( k ){
131015 if( zRight ){
131016 pTab = sqlite3LocateTable(pParse, flags: 0, zName: zRight, zDbase: zDb);
131017 k = 0;
131018 }else{
131019 pTab = (Table*)sqliteHashData(k);
131020 k = sqliteHashNext(k);
131021 }
131022 if( pTab==0 || !IsOrdinaryTable(pTab) || pTab->u.tab.pFKey==0 ) continue;
131023 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
131024 zDb = db->aDb[iDb].zDbSName;
131025 sqlite3CodeVerifySchema(pParse, iDb);
131026 sqlite3TableLock(pParse, iDb, iTab: pTab->tnum, isWriteLock: 0, zName: pTab->zName);
131027 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
131028 sqlite3OpenTable(pParse, iCur: 0, iDb, pTab, OP_OpenRead);
131029 sqlite3VdbeLoadString(p: v, iDest: regResult, zStr: pTab->zName);
131030 assert( IsOrdinaryTable(pTab) );
131031 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
131032 pParent = sqlite3FindTable(db, zName: pFK->zTo, zDatabase: zDb);
131033 if( pParent==0 ) continue;
131034 pIdx = 0;
131035 sqlite3TableLock(pParse, iDb, iTab: pParent->tnum, isWriteLock: 0, zName: pParent->zName);
131036 x = sqlite3FkLocateIndex(pParse, pParent, pFKey: pFK, ppIdx: &pIdx, paiCol: 0);
131037 if( x==0 ){
131038 if( pIdx==0 ){
131039 sqlite3OpenTable(pParse, iCur: i, iDb, pTab: pParent, OP_OpenRead);
131040 }else{
131041 sqlite3VdbeAddOp3(p: v, OP_OpenRead, p1: i, p2: pIdx->tnum, p3: iDb);
131042 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
131043 }
131044 }else{
131045 k = 0;
131046 break;
131047 }
131048 }
131049 assert( pParse->nErr>0 || pFK==0 );
131050 if( pFK ) break;
131051 if( pParse->nTab<i ) pParse->nTab = i;
131052 addrTop = sqlite3VdbeAddOp1(p: v, OP_Rewind, p1: 0); VdbeCoverage(v);
131053 assert( IsOrdinaryTable(pTab) );
131054 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
131055 pParent = sqlite3FindTable(db, zName: pFK->zTo, zDatabase: zDb);
131056 pIdx = 0;
131057 aiCols = 0;
131058 if( pParent ){
131059 x = sqlite3FkLocateIndex(pParse, pParent, pFKey: pFK, ppIdx: &pIdx, paiCol: &aiCols);
131060 assert( x==0 || db->mallocFailed );
131061 }
131062 addrOk = sqlite3VdbeMakeLabel(pParse);
131063
131064 /* Generate code to read the child key values into registers
131065 ** regRow..regRow+n. If any of the child key values are NULL, this
131066 ** row cannot cause an FK violation. Jump directly to addrOk in
131067 ** this case. */
131068 if( regRow+pFK->nCol>pParse->nMem ) pParse->nMem = regRow+pFK->nCol;
131069 for(j=0; j<pFK->nCol; j++){
131070 int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom;
131071 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur: 0, iCol, regOut: regRow+j);
131072 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: regRow+j, p2: addrOk); VdbeCoverage(v);
131073 }
131074
131075 /* Generate code to query the parent index for a matching parent
131076 ** key. If a match is found, jump to addrOk. */
131077 if( pIdx ){
131078 sqlite3VdbeAddOp4(p: v, OP_MakeRecord, p1: regRow, p2: pFK->nCol, p3: regKey,
131079 zP4: sqlite3IndexAffinityStr(db,pIdx), p4type: pFK->nCol);
131080 sqlite3VdbeAddOp4Int(p: v, OP_Found, p1: i, p2: addrOk, p3: regKey, p4: 0);
131081 VdbeCoverage(v);
131082 }else if( pParent ){
131083 int jmp = sqlite3VdbeCurrentAddr(p: v)+2;
131084 sqlite3VdbeAddOp3(p: v, OP_SeekRowid, p1: i, p2: jmp, p3: regRow); VdbeCoverage(v);
131085 sqlite3VdbeGoto(p: v, iDest: addrOk);
131086 assert( pFK->nCol==1 || db->mallocFailed );
131087 }
131088
131089 /* Generate code to report an FK violation to the caller. */
131090 if( HasRowid(pTab) ){
131091 sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: 0, p2: regResult+1);
131092 }else{
131093 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: regResult+1);
131094 }
131095 sqlite3VdbeMultiLoad(p: v, iDest: regResult+2, zTypes: "siX", pFK->zTo, i-1);
131096 sqlite3VdbeAddOp2(p: v, OP_ResultRow, p1: regResult, p2: 4);
131097 sqlite3VdbeResolveLabel(v, x: addrOk);
131098 sqlite3DbFree(db, p: aiCols);
131099 }
131100 sqlite3VdbeAddOp2(p: v, OP_Next, p1: 0, p2: addrTop+1); VdbeCoverage(v);
131101 sqlite3VdbeJumpHere(p: v, addr: addrTop);
131102 }
131103 }
131104 break;
131105#endif /* !defined(SQLITE_OMIT_TRIGGER) */
131106#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
131107
131108#ifndef SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA
131109 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
131110 ** used will be case sensitive or not depending on the RHS.
131111 */
131112 case PragTyp_CASE_SENSITIVE_LIKE: {
131113 if( zRight ){
131114 sqlite3RegisterLikeFunctions(db, caseSensitive: sqlite3GetBoolean(z: zRight, dflt: 0));
131115 }
131116 }
131117 break;
131118#endif /* SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA */
131119
131120#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
131121# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
131122#endif
131123
131124#ifndef SQLITE_OMIT_INTEGRITY_CHECK
131125 /* PRAGMA integrity_check
131126 ** PRAGMA integrity_check(N)
131127 ** PRAGMA quick_check
131128 ** PRAGMA quick_check(N)
131129 **
131130 ** Verify the integrity of the database.
131131 **
131132 ** The "quick_check" is reduced version of
131133 ** integrity_check designed to detect most database corruption
131134 ** without the overhead of cross-checking indexes. Quick_check
131135 ** is linear time wherease integrity_check is O(NlogN).
131136 **
131137 ** The maximum nubmer of errors is 100 by default. A different default
131138 ** can be specified using a numeric parameter N.
131139 **
131140 ** Or, the parameter N can be the name of a table. In that case, only
131141 ** the one table named is verified. The freelist is only verified if
131142 ** the named table is "sqlite_schema" (or one of its aliases).
131143 **
131144 ** All schemas are checked by default. To check just a single
131145 ** schema, use the form:
131146 **
131147 ** PRAGMA schema.integrity_check;
131148 */
131149 case PragTyp_INTEGRITY_CHECK: {
131150 int i, j, addr, mxErr;
131151 Table *pObjTab = 0; /* Check only this one table, if not NULL */
131152
131153 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
131154
131155 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
131156 ** then iDb is set to the index of the database identified by <db>.
131157 ** In this case, the integrity of database iDb only is verified by
131158 ** the VDBE created below.
131159 **
131160 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
131161 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
131162 ** to -1 here, to indicate that the VDBE should verify the integrity
131163 ** of all attached databases. */
131164 assert( iDb>=0 );
131165 assert( iDb==0 || pId2->z );
131166 if( pId2->z==0 ) iDb = -1;
131167
131168 /* Initialize the VDBE program */
131169 pParse->nMem = 6;
131170
131171 /* Set the maximum error count */
131172 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
131173 if( zRight ){
131174 if( sqlite3GetInt32(zNum: zRight, pValue: &mxErr) ){
131175 if( mxErr<=0 ){
131176 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
131177 }
131178 }else{
131179 pObjTab = sqlite3LocateTable(pParse, flags: 0, zName: zRight,
131180 zDbase: iDb>=0 ? db->aDb[iDb].zDbSName : 0);
131181 }
131182 }
131183 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: mxErr-1, p2: 1); /* reg[1] holds errors left */
131184
131185 /* Do an integrity check on each database file */
131186 for(i=0; i<db->nDb; i++){
131187 HashElem *x; /* For looping over tables in the schema */
131188 Hash *pTbls; /* Set of all tables in the schema */
131189 int *aRoot; /* Array of root page numbers of all btrees */
131190 int cnt = 0; /* Number of entries in aRoot[] */
131191 int mxIdx = 0; /* Maximum number of indexes for any table */
131192
131193 if( OMIT_TEMPDB && i==1 ) continue;
131194 if( iDb>=0 && i!=iDb ) continue;
131195
131196 sqlite3CodeVerifySchema(pParse, iDb: i);
131197
131198 /* Do an integrity check of the B-Tree
131199 **
131200 ** Begin by finding the root pages numbers
131201 ** for all tables and indices in the database.
131202 */
131203 assert( sqlite3SchemaMutexHeld(db, i, 0) );
131204 pTbls = &db->aDb[i].pSchema->tblHash;
131205 for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
131206 Table *pTab = sqliteHashData(x); /* Current table */
131207 Index *pIdx; /* An index on pTab */
131208 int nIdx; /* Number of indexes on pTab */
131209 if( pObjTab && pObjTab!=pTab ) continue;
131210 if( HasRowid(pTab) ) cnt++;
131211 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
131212 if( nIdx>mxIdx ) mxIdx = nIdx;
131213 }
131214 if( cnt==0 ) continue;
131215 if( pObjTab ) cnt++;
131216 aRoot = sqlite3DbMallocRawNN(db, n: sizeof(int)*(cnt+1));
131217 if( aRoot==0 ) break;
131218 cnt = 0;
131219 if( pObjTab ) aRoot[++cnt] = 0;
131220 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
131221 Table *pTab = sqliteHashData(x);
131222 Index *pIdx;
131223 if( pObjTab && pObjTab!=pTab ) continue;
131224 if( HasRowid(pTab) ) aRoot[++cnt] = pTab->tnum;
131225 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
131226 aRoot[++cnt] = pIdx->tnum;
131227 }
131228 }
131229 aRoot[0] = cnt;
131230
131231 /* Make sure sufficient number of registers have been allocated */
131232 pParse->nMem = MAX( pParse->nMem, 8+mxIdx );
131233 sqlite3ClearTempRegCache(pParse);
131234
131235 /* Do the b-tree integrity checks */
131236 sqlite3VdbeAddOp4(p: v, OP_IntegrityCk, p1: 2, p2: cnt, p3: 1, zP4: (char*)aRoot,P4_INTARRAY);
131237 sqlite3VdbeChangeP5(p: v, p5: (u8)i);
131238 addr = sqlite3VdbeAddOp1(p: v, OP_IsNull, p1: 2); VdbeCoverage(v);
131239 sqlite3VdbeAddOp4(p: v, OP_String8, p1: 0, p2: 3, p3: 0,
131240 zP4: sqlite3MPrintf(db, zFormat: "*** in database %s ***\n", db->aDb[i].zDbSName),
131241 P4_DYNAMIC);
131242 sqlite3VdbeAddOp3(p: v, OP_Concat, p1: 2, p2: 3, p3: 3);
131243 integrityCheckResultRow(v);
131244 sqlite3VdbeJumpHere(p: v, addr);
131245
131246 /* Make sure all the indices are constructed correctly.
131247 */
131248 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
131249 Table *pTab = sqliteHashData(x);
131250 Index *pIdx, *pPk;
131251 Index *pPrior = 0;
131252 int loopTop;
131253 int iDataCur, iIdxCur;
131254 int r1 = -1;
131255 int bStrict;
131256
131257 if( !IsOrdinaryTable(pTab) ) continue;
131258 if( pObjTab && pObjTab!=pTab ) continue;
131259 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
131260 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, p5: 0,
131261 iBase: 1, aToOpen: 0, piDataCur: &iDataCur, piIdxCur: &iIdxCur);
131262 /* reg[7] counts the number of entries in the table.
131263 ** reg[8+i] counts the number of entries in the i-th index
131264 */
131265 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: 7);
131266 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
131267 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: 8+j); /* index entries counter */
131268 }
131269 assert( pParse->nMem>=8+j );
131270 assert( sqlite3NoTempsInRange(pParse,1,7+j) );
131271 sqlite3VdbeAddOp2(p: v, OP_Rewind, p1: iDataCur, p2: 0); VdbeCoverage(v);
131272 loopTop = sqlite3VdbeAddOp2(p: v, OP_AddImm, p1: 7, p2: 1);
131273 if( !isQuick ){
131274 /* Sanity check on record header decoding */
131275 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iDataCur, p2: pTab->nNVCol-1,p3: 3);
131276 sqlite3VdbeChangeP5(p: v, OPFLAG_TYPEOFARG);
131277 VdbeComment((v, "(right-most column)"));
131278 }
131279 /* Verify that all NOT NULL columns really are NOT NULL. At the
131280 ** same time verify the type of the content of STRICT tables */
131281 bStrict = (pTab->tabFlags & TF_Strict)!=0;
131282 for(j=0; j<pTab->nCol; j++){
131283 char *zErr;
131284 Column *pCol = pTab->aCol + j;
131285 int doError, jmp2;
131286 if( j==pTab->iPKey ) continue;
131287 if( pCol->notNull==0 && !bStrict ) continue;
131288 doError = bStrict ? sqlite3VdbeMakeLabel(pParse) : 0;
131289 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur: iDataCur, iCol: j, regOut: 3);
131290 if( sqlite3VdbeGetOp(p: v,addr: -1)->opcode==OP_Column ){
131291 sqlite3VdbeChangeP5(p: v, OPFLAG_TYPEOFARG);
131292 }
131293 if( pCol->notNull ){
131294 jmp2 = sqlite3VdbeAddOp1(p: v, OP_NotNull, p1: 3); VdbeCoverage(v);
131295 zErr = sqlite3MPrintf(db, zFormat: "NULL value in %s.%s", pTab->zName,
131296 pCol->zCnName);
131297 sqlite3VdbeAddOp4(p: v, OP_String8, p1: 0, p2: 3, p3: 0, zP4: zErr, P4_DYNAMIC);
131298 if( bStrict && pCol->eCType!=COLTYPE_ANY ){
131299 sqlite3VdbeGoto(p: v, iDest: doError);
131300 }else{
131301 integrityCheckResultRow(v);
131302 }
131303 sqlite3VdbeJumpHere(p: v, addr: jmp2);
131304 }
131305 if( (pTab->tabFlags & TF_Strict)!=0
131306 && pCol->eCType!=COLTYPE_ANY
131307 ){
131308 jmp2 = sqlite3VdbeAddOp3(p: v, OP_IsNullOrType, p1: 3, p2: 0,
131309 p3: sqlite3StdTypeMap[pCol->eCType-1]);
131310 VdbeCoverage(v);
131311 zErr = sqlite3MPrintf(db, zFormat: "non-%s value in %s.%s",
131312 sqlite3StdType[pCol->eCType-1],
131313 pTab->zName, pTab->aCol[j].zCnName);
131314 sqlite3VdbeAddOp4(p: v, OP_String8, p1: 0, p2: 3, p3: 0, zP4: zErr, P4_DYNAMIC);
131315 sqlite3VdbeResolveLabel(v, x: doError);
131316 integrityCheckResultRow(v);
131317 sqlite3VdbeJumpHere(p: v, addr: jmp2);
131318 }
131319 }
131320 /* Verify CHECK constraints */
131321 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
131322 ExprList *pCheck = sqlite3ExprListDup(db, p: pTab->pCheck, flags: 0);
131323 if( db->mallocFailed==0 ){
131324 int addrCkFault = sqlite3VdbeMakeLabel(pParse);
131325 int addrCkOk = sqlite3VdbeMakeLabel(pParse);
131326 char *zErr;
131327 int k;
131328 pParse->iSelfTab = iDataCur + 1;
131329 for(k=pCheck->nExpr-1; k>0; k--){
131330 sqlite3ExprIfFalse(pParse, pExpr: pCheck->a[k].pExpr, dest: addrCkFault, jumpIfNull: 0);
131331 }
131332 sqlite3ExprIfTrue(pParse, pExpr: pCheck->a[0].pExpr, dest: addrCkOk,
131333 SQLITE_JUMPIFNULL);
131334 sqlite3VdbeResolveLabel(v, x: addrCkFault);
131335 pParse->iSelfTab = 0;
131336 zErr = sqlite3MPrintf(db, zFormat: "CHECK constraint failed in %s",
131337 pTab->zName);
131338 sqlite3VdbeAddOp4(p: v, OP_String8, p1: 0, p2: 3, p3: 0, zP4: zErr, P4_DYNAMIC);
131339 integrityCheckResultRow(v);
131340 sqlite3VdbeResolveLabel(v, x: addrCkOk);
131341 }
131342 sqlite3ExprListDelete(db, pList: pCheck);
131343 }
131344 if( !isQuick ){ /* Omit the remaining tests for quick_check */
131345 /* Validate index entries for the current row */
131346 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
131347 int jmp2, jmp3, jmp4, jmp5;
131348 int ckUniq = sqlite3VdbeMakeLabel(pParse);
131349 if( pPk==pIdx ) continue;
131350 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, regOut: 0, prefixOnly: 0, piPartIdxLabel: &jmp3,
131351 pPrior, regPrior: r1);
131352 pPrior = pIdx;
131353 sqlite3VdbeAddOp2(p: v, OP_AddImm, p1: 8+j, p2: 1);/* increment entry count */
131354 /* Verify that an index entry exists for the current table row */
131355 jmp2 = sqlite3VdbeAddOp4Int(p: v, OP_Found, p1: iIdxCur+j, p2: ckUniq, p3: r1,
131356 p4: pIdx->nColumn); VdbeCoverage(v);
131357 sqlite3VdbeLoadString(p: v, iDest: 3, zStr: "row ");
131358 sqlite3VdbeAddOp3(p: v, OP_Concat, p1: 7, p2: 3, p3: 3);
131359 sqlite3VdbeLoadString(p: v, iDest: 4, zStr: " missing from index ");
131360 sqlite3VdbeAddOp3(p: v, OP_Concat, p1: 4, p2: 3, p3: 3);
131361 jmp5 = sqlite3VdbeLoadString(p: v, iDest: 4, zStr: pIdx->zName);
131362 sqlite3VdbeAddOp3(p: v, OP_Concat, p1: 4, p2: 3, p3: 3);
131363 jmp4 = integrityCheckResultRow(v);
131364 sqlite3VdbeJumpHere(p: v, addr: jmp2);
131365 /* For UNIQUE indexes, verify that only one entry exists with the
131366 ** current key. The entry is unique if (1) any column is NULL
131367 ** or (2) the next entry has a different key */
131368 if( IsUniqueIndex(pIdx) ){
131369 int uniqOk = sqlite3VdbeMakeLabel(pParse);
131370 int jmp6;
131371 int kk;
131372 for(kk=0; kk<pIdx->nKeyCol; kk++){
131373 int iCol = pIdx->aiColumn[kk];
131374 assert( iCol!=XN_ROWID && iCol<pTab->nCol );
131375 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
131376 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: r1+kk, p2: uniqOk);
131377 VdbeCoverage(v);
131378 }
131379 jmp6 = sqlite3VdbeAddOp1(p: v, OP_Next, p1: iIdxCur+j); VdbeCoverage(v);
131380 sqlite3VdbeGoto(p: v, iDest: uniqOk);
131381 sqlite3VdbeJumpHere(p: v, addr: jmp6);
131382 sqlite3VdbeAddOp4Int(p: v, OP_IdxGT, p1: iIdxCur+j, p2: uniqOk, p3: r1,
131383 p4: pIdx->nKeyCol); VdbeCoverage(v);
131384 sqlite3VdbeLoadString(p: v, iDest: 3, zStr: "non-unique entry in index ");
131385 sqlite3VdbeGoto(p: v, iDest: jmp5);
131386 sqlite3VdbeResolveLabel(v, x: uniqOk);
131387 }
131388 sqlite3VdbeJumpHere(p: v, addr: jmp4);
131389 sqlite3ResolvePartIdxLabel(pParse, iLabel: jmp3);
131390 }
131391 }
131392 sqlite3VdbeAddOp2(p: v, OP_Next, p1: iDataCur, p2: loopTop); VdbeCoverage(v);
131393 sqlite3VdbeJumpHere(p: v, addr: loopTop-1);
131394 if( !isQuick ){
131395 sqlite3VdbeLoadString(p: v, iDest: 2, zStr: "wrong # of entries in index ");
131396 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
131397 if( pPk==pIdx ) continue;
131398 sqlite3VdbeAddOp2(p: v, OP_Count, p1: iIdxCur+j, p2: 3);
131399 addr = sqlite3VdbeAddOp3(p: v, OP_Eq, p1: 8+j, p2: 0, p3: 3); VdbeCoverage(v);
131400 sqlite3VdbeChangeP5(p: v, SQLITE_NOTNULL);
131401 sqlite3VdbeLoadString(p: v, iDest: 4, zStr: pIdx->zName);
131402 sqlite3VdbeAddOp3(p: v, OP_Concat, p1: 4, p2: 2, p3: 3);
131403 integrityCheckResultRow(v);
131404 sqlite3VdbeJumpHere(p: v, addr);
131405 }
131406 }
131407 }
131408 }
131409 {
131410 static const int iLn = VDBE_OFFSET_LINENO(2);
131411 static const VdbeOpList endCode[] = {
131412 { OP_AddImm, 1, 0, 0}, /* 0 */
131413 { OP_IfNotZero, 1, 4, 0}, /* 1 */
131414 { OP_String8, 0, 3, 0}, /* 2 */
131415 { OP_ResultRow, 3, 1, 0}, /* 3 */
131416 { OP_Halt, 0, 0, 0}, /* 4 */
131417 { OP_String8, 0, 3, 0}, /* 5 */
131418 { OP_Goto, 0, 3, 0}, /* 6 */
131419 };
131420 VdbeOp *aOp;
131421
131422 aOp = sqlite3VdbeAddOpList(p: v, ArraySize(endCode), aOp: endCode, iLineno: iLn);
131423 if( aOp ){
131424 aOp[0].p2 = 1-mxErr;
131425 aOp[2].p4type = P4_STATIC;
131426 aOp[2].p4.z = "ok";
131427 aOp[5].p4type = P4_STATIC;
131428 aOp[5].p4.z = (char*)sqlite3ErrStr(SQLITE_CORRUPT);
131429 }
131430 sqlite3VdbeChangeP3(p: v, addr: 0, val: sqlite3VdbeCurrentAddr(p: v)-2);
131431 }
131432 }
131433 break;
131434#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
131435
131436#ifndef SQLITE_OMIT_UTF16
131437 /*
131438 ** PRAGMA encoding
131439 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
131440 **
131441 ** In its first form, this pragma returns the encoding of the main
131442 ** database. If the database is not initialized, it is initialized now.
131443 **
131444 ** The second form of this pragma is a no-op if the main database file
131445 ** has not already been initialized. In this case it sets the default
131446 ** encoding that will be used for the main database file if a new file
131447 ** is created. If an existing main database file is opened, then the
131448 ** default text encoding for the existing database is used.
131449 **
131450 ** In all cases new databases created using the ATTACH command are
131451 ** created to use the same default text encoding as the main database. If
131452 ** the main database has not been initialized and/or created when ATTACH
131453 ** is executed, this is done before the ATTACH operation.
131454 **
131455 ** In the second form this pragma sets the text encoding to be used in
131456 ** new database files created using this database handle. It is only
131457 ** useful if invoked immediately after the main database i
131458 */
131459 case PragTyp_ENCODING: {
131460 static const struct EncName {
131461 char *zName;
131462 u8 enc;
131463 } encnames[] = {
131464 { "UTF8", SQLITE_UTF8 },
131465 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
131466 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
131467 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
131468 { "UTF16le", SQLITE_UTF16LE },
131469 { "UTF16be", SQLITE_UTF16BE },
131470 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
131471 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
131472 { 0, 0 }
131473 };
131474 const struct EncName *pEnc;
131475 if( !zRight ){ /* "PRAGMA encoding" */
131476 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
131477 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
131478 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
131479 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
131480 returnSingleText(v, zValue: encnames[ENC(pParse->db)].zName);
131481 }else{ /* "PRAGMA encoding = XXX" */
131482 /* Only change the value of sqlite.enc if the database handle is not
131483 ** initialized. If the main database exists, the new sqlite.enc value
131484 ** will be overwritten when the schema is next loaded. If it does not
131485 ** already exists, it will be created to use the new encoding value.
131486 */
131487 if( (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
131488 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
131489 if( 0==sqlite3StrICmp(zLeft: zRight, zRight: pEnc->zName) ){
131490 u8 enc = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
131491 SCHEMA_ENC(db) = enc;
131492 sqlite3SetTextEncoding(db, enc);
131493 break;
131494 }
131495 }
131496 if( !pEnc->zName ){
131497 sqlite3ErrorMsg(pParse, zFormat: "unsupported encoding: %s", zRight);
131498 }
131499 }
131500 }
131501 }
131502 break;
131503#endif /* SQLITE_OMIT_UTF16 */
131504
131505#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
131506 /*
131507 ** PRAGMA [schema.]schema_version
131508 ** PRAGMA [schema.]schema_version = <integer>
131509 **
131510 ** PRAGMA [schema.]user_version
131511 ** PRAGMA [schema.]user_version = <integer>
131512 **
131513 ** PRAGMA [schema.]freelist_count
131514 **
131515 ** PRAGMA [schema.]data_version
131516 **
131517 ** PRAGMA [schema.]application_id
131518 ** PRAGMA [schema.]application_id = <integer>
131519 **
131520 ** The pragma's schema_version and user_version are used to set or get
131521 ** the value of the schema-version and user-version, respectively. Both
131522 ** the schema-version and the user-version are 32-bit signed integers
131523 ** stored in the database header.
131524 **
131525 ** The schema-cookie is usually only manipulated internally by SQLite. It
131526 ** is incremented by SQLite whenever the database schema is modified (by
131527 ** creating or dropping a table or index). The schema version is used by
131528 ** SQLite each time a query is executed to ensure that the internal cache
131529 ** of the schema used when compiling the SQL query matches the schema of
131530 ** the database against which the compiled query is actually executed.
131531 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
131532 ** the schema-version is potentially dangerous and may lead to program
131533 ** crashes or database corruption. Use with caution!
131534 **
131535 ** The user-version is not used internally by SQLite. It may be used by
131536 ** applications for any purpose.
131537 */
131538 case PragTyp_HEADER_VALUE: {
131539 int iCookie = pPragma->iArg; /* Which cookie to read or write */
131540 sqlite3VdbeUsesBtree(p: v, i: iDb);
131541 if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){
131542 /* Write the specified cookie value */
131543 static const VdbeOpList setCookie[] = {
131544 { OP_Transaction, 0, 1, 0}, /* 0 */
131545 { OP_SetCookie, 0, 0, 0}, /* 1 */
131546 };
131547 VdbeOp *aOp;
131548 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setCookie));
131549 aOp = sqlite3VdbeAddOpList(p: v, ArraySize(setCookie), aOp: setCookie, iLineno: 0);
131550 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
131551 aOp[0].p1 = iDb;
131552 aOp[1].p1 = iDb;
131553 aOp[1].p2 = iCookie;
131554 aOp[1].p3 = sqlite3Atoi(z: zRight);
131555 aOp[1].p5 = 1;
131556 }else{
131557 /* Read the specified cookie value */
131558 static const VdbeOpList readCookie[] = {
131559 { OP_Transaction, 0, 0, 0}, /* 0 */
131560 { OP_ReadCookie, 0, 1, 0}, /* 1 */
131561 { OP_ResultRow, 1, 1, 0}
131562 };
131563 VdbeOp *aOp;
131564 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(readCookie));
131565 aOp = sqlite3VdbeAddOpList(p: v, ArraySize(readCookie),aOp: readCookie,iLineno: 0);
131566 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
131567 aOp[0].p1 = iDb;
131568 aOp[1].p1 = iDb;
131569 aOp[1].p3 = iCookie;
131570 sqlite3VdbeReusable(p: v);
131571 }
131572 }
131573 break;
131574#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
131575
131576#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
131577 /*
131578 ** PRAGMA compile_options
131579 **
131580 ** Return the names of all compile-time options used in this build,
131581 ** one option per row.
131582 */
131583 case PragTyp_COMPILE_OPTIONS: {
131584 int i = 0;
131585 const char *zOpt;
131586 pParse->nMem = 1;
131587 while( (zOpt = sqlite3_compileoption_get(N: i++))!=0 ){
131588 sqlite3VdbeLoadString(p: v, iDest: 1, zStr: zOpt);
131589 sqlite3VdbeAddOp2(p: v, OP_ResultRow, p1: 1, p2: 1);
131590 }
131591 sqlite3VdbeReusable(p: v);
131592 }
131593 break;
131594#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
131595
131596#ifndef SQLITE_OMIT_WAL
131597 /*
131598 ** PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate
131599 **
131600 ** Checkpoint the database.
131601 */
131602 case PragTyp_WAL_CHECKPOINT: {
131603 int iBt = (pId2->z?iDb:SQLITE_MAX_DB);
131604 int eMode = SQLITE_CHECKPOINT_PASSIVE;
131605 if( zRight ){
131606 if( sqlite3StrICmp(zLeft: zRight, zRight: "full")==0 ){
131607 eMode = SQLITE_CHECKPOINT_FULL;
131608 }else if( sqlite3StrICmp(zLeft: zRight, zRight: "restart")==0 ){
131609 eMode = SQLITE_CHECKPOINT_RESTART;
131610 }else if( sqlite3StrICmp(zLeft: zRight, zRight: "truncate")==0 ){
131611 eMode = SQLITE_CHECKPOINT_TRUNCATE;
131612 }
131613 }
131614 pParse->nMem = 3;
131615 sqlite3VdbeAddOp3(p: v, OP_Checkpoint, p1: iBt, p2: eMode, p3: 1);
131616 sqlite3VdbeAddOp2(p: v, OP_ResultRow, p1: 1, p2: 3);
131617 }
131618 break;
131619
131620 /*
131621 ** PRAGMA wal_autocheckpoint
131622 ** PRAGMA wal_autocheckpoint = N
131623 **
131624 ** Configure a database connection to automatically checkpoint a database
131625 ** after accumulating N frames in the log. Or query for the current value
131626 ** of N.
131627 */
131628 case PragTyp_WAL_AUTOCHECKPOINT: {
131629 if( zRight ){
131630 sqlite3_wal_autocheckpoint(db, N: sqlite3Atoi(z: zRight));
131631 }
131632 returnSingleInt(v,
131633 value: db->xWalCallback==sqlite3WalDefaultHook ?
131634 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
131635 }
131636 break;
131637#endif
131638
131639 /*
131640 ** PRAGMA shrink_memory
131641 **
131642 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
131643 ** connection on which it is invoked to free up as much memory as it
131644 ** can, by calling sqlite3_db_release_memory().
131645 */
131646 case PragTyp_SHRINK_MEMORY: {
131647 sqlite3_db_release_memory(db);
131648 break;
131649 }
131650
131651 /*
131652 ** PRAGMA optimize
131653 ** PRAGMA optimize(MASK)
131654 ** PRAGMA schema.optimize
131655 ** PRAGMA schema.optimize(MASK)
131656 **
131657 ** Attempt to optimize the database. All schemas are optimized in the first
131658 ** two forms, and only the specified schema is optimized in the latter two.
131659 **
131660 ** The details of optimizations performed by this pragma are expected
131661 ** to change and improve over time. Applications should anticipate that
131662 ** this pragma will perform new optimizations in future releases.
131663 **
131664 ** The optional argument is a bitmask of optimizations to perform:
131665 **
131666 ** 0x0001 Debugging mode. Do not actually perform any optimizations
131667 ** but instead return one line of text for each optimization
131668 ** that would have been done. Off by default.
131669 **
131670 ** 0x0002 Run ANALYZE on tables that might benefit. On by default.
131671 ** See below for additional information.
131672 **
131673 ** 0x0004 (Not yet implemented) Record usage and performance
131674 ** information from the current session in the
131675 ** database file so that it will be available to "optimize"
131676 ** pragmas run by future database connections.
131677 **
131678 ** 0x0008 (Not yet implemented) Create indexes that might have
131679 ** been helpful to recent queries
131680 **
131681 ** The default MASK is and always shall be 0xfffe. 0xfffe means perform all
131682 ** of the optimizations listed above except Debug Mode, including new
131683 ** optimizations that have not yet been invented. If new optimizations are
131684 ** ever added that should be off by default, those off-by-default
131685 ** optimizations will have bitmasks of 0x10000 or larger.
131686 **
131687 ** DETERMINATION OF WHEN TO RUN ANALYZE
131688 **
131689 ** In the current implementation, a table is analyzed if only if all of
131690 ** the following are true:
131691 **
131692 ** (1) MASK bit 0x02 is set.
131693 **
131694 ** (2) The query planner used sqlite_stat1-style statistics for one or
131695 ** more indexes of the table at some point during the lifetime of
131696 ** the current connection.
131697 **
131698 ** (3) One or more indexes of the table are currently unanalyzed OR
131699 ** the number of rows in the table has increased by 25 times or more
131700 ** since the last time ANALYZE was run.
131701 **
131702 ** The rules for when tables are analyzed are likely to change in
131703 ** future releases.
131704 */
131705 case PragTyp_OPTIMIZE: {
131706 int iDbLast; /* Loop termination point for the schema loop */
131707 int iTabCur; /* Cursor for a table whose size needs checking */
131708 HashElem *k; /* Loop over tables of a schema */
131709 Schema *pSchema; /* The current schema */
131710 Table *pTab; /* A table in the schema */
131711 Index *pIdx; /* An index of the table */
131712 LogEst szThreshold; /* Size threshold above which reanalysis is needd */
131713 char *zSubSql; /* SQL statement for the OP_SqlExec opcode */
131714 u32 opMask; /* Mask of operations to perform */
131715
131716 if( zRight ){
131717 opMask = (u32)sqlite3Atoi(z: zRight);
131718 if( (opMask & 0x02)==0 ) break;
131719 }else{
131720 opMask = 0xfffe;
131721 }
131722 iTabCur = pParse->nTab++;
131723 for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
131724 if( iDb==1 ) continue;
131725 sqlite3CodeVerifySchema(pParse, iDb);
131726 pSchema = db->aDb[iDb].pSchema;
131727 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
131728 pTab = (Table*)sqliteHashData(k);
131729
131730 /* If table pTab has not been used in a way that would benefit from
131731 ** having analysis statistics during the current session, then skip it.
131732 ** This also has the effect of skipping virtual tables and views */
131733 if( (pTab->tabFlags & TF_StatsUsed)==0 ) continue;
131734
131735 /* Reanalyze if the table is 25 times larger than the last analysis */
131736 szThreshold = pTab->nRowLogEst + 46; assert( sqlite3LogEst(25)==46 );
131737 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
131738 if( !pIdx->hasStat1 ){
131739 szThreshold = 0; /* Always analyze if any index lacks statistics */
131740 break;
131741 }
131742 }
131743 if( szThreshold ){
131744 sqlite3OpenTable(pParse, iCur: iTabCur, iDb, pTab, OP_OpenRead);
131745 sqlite3VdbeAddOp3(p: v, OP_IfSmaller, p1: iTabCur,
131746 p2: sqlite3VdbeCurrentAddr(p: v)+2+(opMask&1), p3: szThreshold);
131747 VdbeCoverage(v);
131748 }
131749 zSubSql = sqlite3MPrintf(db, zFormat: "ANALYZE \"%w\".\"%w\"",
131750 db->aDb[iDb].zDbSName, pTab->zName);
131751 if( opMask & 0x01 ){
131752 int r1 = sqlite3GetTempReg(pParse);
131753 sqlite3VdbeAddOp4(p: v, OP_String8, p1: 0, p2: r1, p3: 0, zP4: zSubSql, P4_DYNAMIC);
131754 sqlite3VdbeAddOp2(p: v, OP_ResultRow, p1: r1, p2: 1);
131755 }else{
131756 sqlite3VdbeAddOp4(p: v, OP_SqlExec, p1: 0, p2: 0, p3: 0, zP4: zSubSql, P4_DYNAMIC);
131757 }
131758 }
131759 }
131760 sqlite3VdbeAddOp0(p: v, OP_Expire);
131761 break;
131762 }
131763
131764 /*
131765 ** PRAGMA busy_timeout
131766 ** PRAGMA busy_timeout = N
131767 **
131768 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
131769 ** if one is set. If no busy handler or a different busy handler is set
131770 ** then 0 is returned. Setting the busy_timeout to 0 or negative
131771 ** disables the timeout.
131772 */
131773 /*case PragTyp_BUSY_TIMEOUT*/ default: {
131774 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
131775 if( zRight ){
131776 sqlite3_busy_timeout(db, ms: sqlite3Atoi(z: zRight));
131777 }
131778 returnSingleInt(v, value: db->busyTimeout);
131779 break;
131780 }
131781
131782 /*
131783 ** PRAGMA soft_heap_limit
131784 ** PRAGMA soft_heap_limit = N
131785 **
131786 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
131787 ** sqlite3_soft_heap_limit64() interface with the argument N, if N is
131788 ** specified and is a non-negative integer.
131789 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
131790 ** returns the same integer that would be returned by the
131791 ** sqlite3_soft_heap_limit64(-1) C-language function.
131792 */
131793 case PragTyp_SOFT_HEAP_LIMIT: {
131794 sqlite3_int64 N;
131795 if( zRight && sqlite3DecOrHexToI64(z: zRight, pOut: &N)==SQLITE_OK ){
131796 sqlite3_soft_heap_limit64(n: N);
131797 }
131798 returnSingleInt(v, value: sqlite3_soft_heap_limit64(n: -1));
131799 break;
131800 }
131801
131802 /*
131803 ** PRAGMA hard_heap_limit
131804 ** PRAGMA hard_heap_limit = N
131805 **
131806 ** Invoke sqlite3_hard_heap_limit64() to query or set the hard heap
131807 ** limit. The hard heap limit can be activated or lowered by this
131808 ** pragma, but not raised or deactivated. Only the
131809 ** sqlite3_hard_heap_limit64() C-language API can raise or deactivate
131810 ** the hard heap limit. This allows an application to set a heap limit
131811 ** constraint that cannot be relaxed by an untrusted SQL script.
131812 */
131813 case PragTyp_HARD_HEAP_LIMIT: {
131814 sqlite3_int64 N;
131815 if( zRight && sqlite3DecOrHexToI64(z: zRight, pOut: &N)==SQLITE_OK ){
131816 sqlite3_int64 iPrior = sqlite3_hard_heap_limit64(n: -1);
131817 if( N>0 && (iPrior==0 || iPrior>N) ) sqlite3_hard_heap_limit64(n: N);
131818 }
131819 returnSingleInt(v, value: sqlite3_hard_heap_limit64(n: -1));
131820 break;
131821 }
131822
131823 /*
131824 ** PRAGMA threads
131825 ** PRAGMA threads = N
131826 **
131827 ** Configure the maximum number of worker threads. Return the new
131828 ** maximum, which might be less than requested.
131829 */
131830 case PragTyp_THREADS: {
131831 sqlite3_int64 N;
131832 if( zRight
131833 && sqlite3DecOrHexToI64(z: zRight, pOut: &N)==SQLITE_OK
131834 && N>=0
131835 ){
131836 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, newVal: (int)(N&0x7fffffff));
131837 }
131838 returnSingleInt(v, value: sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, newVal: -1));
131839 break;
131840 }
131841
131842 /*
131843 ** PRAGMA analysis_limit
131844 ** PRAGMA analysis_limit = N
131845 **
131846 ** Configure the maximum number of rows that ANALYZE will examine
131847 ** in each index that it looks at. Return the new limit.
131848 */
131849 case PragTyp_ANALYSIS_LIMIT: {
131850 sqlite3_int64 N;
131851 if( zRight
131852 && sqlite3DecOrHexToI64(z: zRight, pOut: &N)==SQLITE_OK /* IMP: R-40975-20399 */
131853 && N>=0
131854 ){
131855 db->nAnalysisLimit = (int)(N&0x7fffffff);
131856 }
131857 returnSingleInt(v, value: db->nAnalysisLimit); /* IMP: R-57594-65522 */
131858 break;
131859 }
131860
131861#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
131862 /*
131863 ** Report the current state of file logs for all databases
131864 */
131865 case PragTyp_LOCK_STATUS: {
131866 static const char *const azLockName[] = {
131867 "unlocked", "shared", "reserved", "pending", "exclusive"
131868 };
131869 int i;
131870 pParse->nMem = 2;
131871 for(i=0; i<db->nDb; i++){
131872 Btree *pBt;
131873 const char *zState = "unknown";
131874 int j;
131875 if( db->aDb[i].zDbSName==0 ) continue;
131876 pBt = db->aDb[i].pBt;
131877 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
131878 zState = "closed";
131879 }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0,
131880 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
131881 zState = azLockName[j];
131882 }
131883 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
131884 }
131885 break;
131886 }
131887#endif
131888
131889#if defined(SQLITE_ENABLE_CEROD)
131890 case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
131891 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
131892 sqlite3_activate_cerod(&zRight[6]);
131893 }
131894 }
131895 break;
131896#endif
131897
131898 } /* End of the PRAGMA switch */
131899
131900 /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
131901 ** purpose is to execute assert() statements to verify that if the
131902 ** PragFlg_NoColumns1 flag is set and the caller specified an argument
131903 ** to the PRAGMA, the implementation has not added any OP_ResultRow
131904 ** instructions to the VM. */
131905 if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){
131906 sqlite3VdbeVerifyNoResultRow(v);
131907 }
131908
131909pragma_out:
131910 sqlite3DbFree(db, p: zLeft);
131911 sqlite3DbFree(db, p: zRight);
131912}
131913#ifndef SQLITE_OMIT_VIRTUALTABLE
131914/*****************************************************************************
131915** Implementation of an eponymous virtual table that runs a pragma.
131916**
131917*/
131918typedef struct PragmaVtab PragmaVtab;
131919typedef struct PragmaVtabCursor PragmaVtabCursor;
131920struct PragmaVtab {
131921 sqlite3_vtab base; /* Base class. Must be first */
131922 sqlite3 *db; /* The database connection to which it belongs */
131923 const PragmaName *pName; /* Name of the pragma */
131924 u8 nHidden; /* Number of hidden columns */
131925 u8 iHidden; /* Index of the first hidden column */
131926};
131927struct PragmaVtabCursor {
131928 sqlite3_vtab_cursor base; /* Base class. Must be first */
131929 sqlite3_stmt *pPragma; /* The pragma statement to run */
131930 sqlite_int64 iRowid; /* Current rowid */
131931 char *azArg[2]; /* Value of the argument and schema */
131932};
131933
131934/*
131935** Pragma virtual table module xConnect method.
131936*/
131937static int pragmaVtabConnect(
131938 sqlite3 *db,
131939 void *pAux,
131940 int argc, const char *const*argv,
131941 sqlite3_vtab **ppVtab,
131942 char **pzErr
131943){
131944 const PragmaName *pPragma = (const PragmaName*)pAux;
131945 PragmaVtab *pTab = 0;
131946 int rc;
131947 int i, j;
131948 char cSep = '(';
131949 StrAccum acc;
131950 char zBuf[200];
131951
131952 UNUSED_PARAMETER(argc);
131953 UNUSED_PARAMETER(argv);
131954 sqlite3StrAccumInit(p: &acc, db: 0, zBase: zBuf, n: sizeof(zBuf), mx: 0);
131955 sqlite3_str_appendall(p: &acc, z: "CREATE TABLE x");
131956 for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
131957 sqlite3_str_appendf(p: &acc, zFormat: "%c\"%s\"", cSep, pragCName[j]);
131958 cSep = ',';
131959 }
131960 if( i==0 ){
131961 sqlite3_str_appendf(p: &acc, zFormat: "(\"%s\"", pPragma->zName);
131962 i++;
131963 }
131964 j = 0;
131965 if( pPragma->mPragFlg & PragFlg_Result1 ){
131966 sqlite3_str_appendall(p: &acc, z: ",arg HIDDEN");
131967 j++;
131968 }
131969 if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){
131970 sqlite3_str_appendall(p: &acc, z: ",schema HIDDEN");
131971 j++;
131972 }
131973 sqlite3_str_append(p: &acc, z: ")", N: 1);
131974 sqlite3StrAccumFinish(p: &acc);
131975 assert( strlen(zBuf) < sizeof(zBuf)-1 );
131976 rc = sqlite3_declare_vtab(db, zSQL: zBuf);
131977 if( rc==SQLITE_OK ){
131978 pTab = (PragmaVtab*)sqlite3_malloc(n: sizeof(PragmaVtab));
131979 if( pTab==0 ){
131980 rc = SQLITE_NOMEM;
131981 }else{
131982 memset(s: pTab, c: 0, n: sizeof(PragmaVtab));
131983 pTab->pName = pPragma;
131984 pTab->db = db;
131985 pTab->iHidden = i;
131986 pTab->nHidden = j;
131987 }
131988 }else{
131989 *pzErr = sqlite3_mprintf(zFormat: "%s", sqlite3_errmsg(db));
131990 }
131991
131992 *ppVtab = (sqlite3_vtab*)pTab;
131993 return rc;
131994}
131995
131996/*
131997** Pragma virtual table module xDisconnect method.
131998*/
131999static int pragmaVtabDisconnect(sqlite3_vtab *pVtab){
132000 PragmaVtab *pTab = (PragmaVtab*)pVtab;
132001 sqlite3_free(p: pTab);
132002 return SQLITE_OK;
132003}
132004
132005/* Figure out the best index to use to search a pragma virtual table.
132006**
132007** There are not really any index choices. But we want to encourage the
132008** query planner to give == constraints on as many hidden parameters as
132009** possible, and especially on the first hidden parameter. So return a
132010** high cost if hidden parameters are unconstrained.
132011*/
132012static int pragmaVtabBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
132013 PragmaVtab *pTab = (PragmaVtab*)tab;
132014 const struct sqlite3_index_constraint *pConstraint;
132015 int i, j;
132016 int seen[2];
132017
132018 pIdxInfo->estimatedCost = (double)1;
132019 if( pTab->nHidden==0 ){ return SQLITE_OK; }
132020 pConstraint = pIdxInfo->aConstraint;
132021 seen[0] = 0;
132022 seen[1] = 0;
132023 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
132024 if( pConstraint->usable==0 ) continue;
132025 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
132026 if( pConstraint->iColumn < pTab->iHidden ) continue;
132027 j = pConstraint->iColumn - pTab->iHidden;
132028 assert( j < 2 );
132029 seen[j] = i+1;
132030 }
132031 if( seen[0]==0 ){
132032 pIdxInfo->estimatedCost = (double)2147483647;
132033 pIdxInfo->estimatedRows = 2147483647;
132034 return SQLITE_OK;
132035 }
132036 j = seen[0]-1;
132037 pIdxInfo->aConstraintUsage[j].argvIndex = 1;
132038 pIdxInfo->aConstraintUsage[j].omit = 1;
132039 if( seen[1]==0 ) return SQLITE_OK;
132040 pIdxInfo->estimatedCost = (double)20;
132041 pIdxInfo->estimatedRows = 20;
132042 j = seen[1]-1;
132043 pIdxInfo->aConstraintUsage[j].argvIndex = 2;
132044 pIdxInfo->aConstraintUsage[j].omit = 1;
132045 return SQLITE_OK;
132046}
132047
132048/* Create a new cursor for the pragma virtual table */
132049static int pragmaVtabOpen(sqlite3_vtab *pVtab, sqlite3_vtab_cursor **ppCursor){
132050 PragmaVtabCursor *pCsr;
132051 pCsr = (PragmaVtabCursor*)sqlite3_malloc(n: sizeof(*pCsr));
132052 if( pCsr==0 ) return SQLITE_NOMEM;
132053 memset(s: pCsr, c: 0, n: sizeof(PragmaVtabCursor));
132054 pCsr->base.pVtab = pVtab;
132055 *ppCursor = &pCsr->base;
132056 return SQLITE_OK;
132057}
132058
132059/* Clear all content from pragma virtual table cursor. */
132060static void pragmaVtabCursorClear(PragmaVtabCursor *pCsr){
132061 int i;
132062 sqlite3_finalize(pStmt: pCsr->pPragma);
132063 pCsr->pPragma = 0;
132064 for(i=0; i<ArraySize(pCsr->azArg); i++){
132065 sqlite3_free(p: pCsr->azArg[i]);
132066 pCsr->azArg[i] = 0;
132067 }
132068}
132069
132070/* Close a pragma virtual table cursor */
132071static int pragmaVtabClose(sqlite3_vtab_cursor *cur){
132072 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)cur;
132073 pragmaVtabCursorClear(pCsr);
132074 sqlite3_free(p: pCsr);
132075 return SQLITE_OK;
132076}
132077
132078/* Advance the pragma virtual table cursor to the next row */
132079static int pragmaVtabNext(sqlite3_vtab_cursor *pVtabCursor){
132080 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
132081 int rc = SQLITE_OK;
132082
132083 /* Increment the xRowid value */
132084 pCsr->iRowid++;
132085 assert( pCsr->pPragma );
132086 if( SQLITE_ROW!=sqlite3_step(pStmt: pCsr->pPragma) ){
132087 rc = sqlite3_finalize(pStmt: pCsr->pPragma);
132088 pCsr->pPragma = 0;
132089 pragmaVtabCursorClear(pCsr);
132090 }
132091 return rc;
132092}
132093
132094/*
132095** Pragma virtual table module xFilter method.
132096*/
132097static int pragmaVtabFilter(
132098 sqlite3_vtab_cursor *pVtabCursor,
132099 int idxNum, const char *idxStr,
132100 int argc, sqlite3_value **argv
132101){
132102 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
132103 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
132104 int rc;
132105 int i, j;
132106 StrAccum acc;
132107 char *zSql;
132108
132109 UNUSED_PARAMETER(idxNum);
132110 UNUSED_PARAMETER(idxStr);
132111 pragmaVtabCursorClear(pCsr);
132112 j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1;
132113 for(i=0; i<argc; i++, j++){
132114 const char *zText = (const char*)sqlite3_value_text(pVal: argv[i]);
132115 assert( j<ArraySize(pCsr->azArg) );
132116 assert( pCsr->azArg[j]==0 );
132117 if( zText ){
132118 pCsr->azArg[j] = sqlite3_mprintf(zFormat: "%s", zText);
132119 if( pCsr->azArg[j]==0 ){
132120 return SQLITE_NOMEM;
132121 }
132122 }
132123 }
132124 sqlite3StrAccumInit(p: &acc, db: 0, zBase: 0, n: 0, mx: pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]);
132125 sqlite3_str_appendall(p: &acc, z: "PRAGMA ");
132126 if( pCsr->azArg[1] ){
132127 sqlite3_str_appendf(p: &acc, zFormat: "%Q.", pCsr->azArg[1]);
132128 }
132129 sqlite3_str_appendall(p: &acc, z: pTab->pName->zName);
132130 if( pCsr->azArg[0] ){
132131 sqlite3_str_appendf(p: &acc, zFormat: "=%Q", pCsr->azArg[0]);
132132 }
132133 zSql = sqlite3StrAccumFinish(p: &acc);
132134 if( zSql==0 ) return SQLITE_NOMEM;
132135 rc = sqlite3_prepare_v2(db: pTab->db, zSql, nByte: -1, ppStmt: &pCsr->pPragma, pzTail: 0);
132136 sqlite3_free(p: zSql);
132137 if( rc!=SQLITE_OK ){
132138 pTab->base.zErrMsg = sqlite3_mprintf(zFormat: "%s", sqlite3_errmsg(pTab->db));
132139 return rc;
132140 }
132141 return pragmaVtabNext(pVtabCursor);
132142}
132143
132144/*
132145** Pragma virtual table module xEof method.
132146*/
132147static int pragmaVtabEof(sqlite3_vtab_cursor *pVtabCursor){
132148 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
132149 return (pCsr->pPragma==0);
132150}
132151
132152/* The xColumn method simply returns the corresponding column from
132153** the PRAGMA.
132154*/
132155static int pragmaVtabColumn(
132156 sqlite3_vtab_cursor *pVtabCursor,
132157 sqlite3_context *ctx,
132158 int i
132159){
132160 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
132161 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
132162 if( i<pTab->iHidden ){
132163 sqlite3_result_value(pCtx: ctx, pValue: sqlite3_column_value(pStmt: pCsr->pPragma, i));
132164 }else{
132165 sqlite3_result_text(pCtx: ctx, z: pCsr->azArg[i-pTab->iHidden],n: -1,SQLITE_TRANSIENT);
132166 }
132167 return SQLITE_OK;
132168}
132169
132170/*
132171** Pragma virtual table module xRowid method.
132172*/
132173static int pragmaVtabRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *p){
132174 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
132175 *p = pCsr->iRowid;
132176 return SQLITE_OK;
132177}
132178
132179/* The pragma virtual table object */
132180static const sqlite3_module pragmaVtabModule = {
132181 0, /* iVersion */
132182 0, /* xCreate - create a table */
132183 pragmaVtabConnect, /* xConnect - connect to an existing table */
132184 pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */
132185 pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */
132186 0, /* xDestroy - Drop a table */
132187 pragmaVtabOpen, /* xOpen - open a cursor */
132188 pragmaVtabClose, /* xClose - close a cursor */
132189 pragmaVtabFilter, /* xFilter - configure scan constraints */
132190 pragmaVtabNext, /* xNext - advance a cursor */
132191 pragmaVtabEof, /* xEof */
132192 pragmaVtabColumn, /* xColumn - read data */
132193 pragmaVtabRowid, /* xRowid - read data */
132194 0, /* xUpdate - write data */
132195 0, /* xBegin - begin transaction */
132196 0, /* xSync - sync transaction */
132197 0, /* xCommit - commit transaction */
132198 0, /* xRollback - rollback transaction */
132199 0, /* xFindFunction - function overloading */
132200 0, /* xRename - rename the table */
132201 0, /* xSavepoint */
132202 0, /* xRelease */
132203 0, /* xRollbackTo */
132204 0 /* xShadowName */
132205};
132206
132207/*
132208** Check to see if zTabName is really the name of a pragma. If it is,
132209** then register an eponymous virtual table for that pragma and return
132210** a pointer to the Module object for the new virtual table.
132211*/
132212SQLITE_PRIVATE Module *sqlite3PragmaVtabRegister(sqlite3 *db, const char *zName){
132213 const PragmaName *pName;
132214 assert( sqlite3_strnicmp(zName, "pragma_", 7)==0 );
132215 pName = pragmaLocate(zName: zName+7);
132216 if( pName==0 ) return 0;
132217 if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0;
132218 assert( sqlite3HashFind(&db->aModule, zName)==0 );
132219 return sqlite3VtabCreateModule(db, zName, &pragmaVtabModule, (void*)pName, 0);
132220}
132221
132222#endif /* SQLITE_OMIT_VIRTUALTABLE */
132223
132224#endif /* SQLITE_OMIT_PRAGMA */
132225
132226/************** End of pragma.c **********************************************/
132227/************** Begin file prepare.c *****************************************/
132228/*
132229** 2005 May 25
132230**
132231** The author disclaims copyright to this source code. In place of
132232** a legal notice, here is a blessing:
132233**
132234** May you do good and not evil.
132235** May you find forgiveness for yourself and forgive others.
132236** May you share freely, never taking more than you give.
132237**
132238*************************************************************************
132239** This file contains the implementation of the sqlite3_prepare()
132240** interface, and routines that contribute to loading the database schema
132241** from disk.
132242*/
132243/* #include "sqliteInt.h" */
132244
132245/*
132246** Fill the InitData structure with an error message that indicates
132247** that the database is corrupt.
132248*/
132249static void corruptSchema(
132250 InitData *pData, /* Initialization context */
132251 char **azObj, /* Type and name of object being parsed */
132252 const char *zExtra /* Error information */
132253){
132254 sqlite3 *db = pData->db;
132255 if( db->mallocFailed ){
132256 pData->rc = SQLITE_NOMEM_BKPT;
132257 }else if( pData->pzErrMsg[0]!=0 ){
132258 /* A error message has already been generated. Do not overwrite it */
132259 }else if( pData->mInitFlags & (INITFLAG_AlterMask) ){
132260 static const char *azAlterType[] = {
132261 "rename",
132262 "drop column",
132263 "add column"
132264 };
132265 *pData->pzErrMsg = sqlite3MPrintf(db,
132266 zFormat: "error in %s %s after %s: %s", azObj[0], azObj[1],
132267 azAlterType[(pData->mInitFlags&INITFLAG_AlterMask)-1],
132268 zExtra
132269 );
132270 pData->rc = SQLITE_ERROR;
132271 }else if( db->flags & SQLITE_WriteSchema ){
132272 pData->rc = SQLITE_CORRUPT_BKPT;
132273 }else{
132274 char *z;
132275 const char *zObj = azObj[1] ? azObj[1] : "?";
132276 z = sqlite3MPrintf(db, zFormat: "malformed database schema (%s)", zObj);
132277 if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, zFormat: "%z - %s", z, zExtra);
132278 *pData->pzErrMsg = z;
132279 pData->rc = SQLITE_CORRUPT_BKPT;
132280 }
132281}
132282
132283/*
132284** Check to see if any sibling index (another index on the same table)
132285** of pIndex has the same root page number, and if it does, return true.
132286** This would indicate a corrupt schema.
132287*/
132288SQLITE_PRIVATE int sqlite3IndexHasDuplicateRootPage(Index *pIndex){
132289 Index *p;
132290 for(p=pIndex->pTable->pIndex; p; p=p->pNext){
132291 if( p->tnum==pIndex->tnum && p!=pIndex ) return 1;
132292 }
132293 return 0;
132294}
132295
132296/* forward declaration */
132297static int sqlite3Prepare(
132298 sqlite3 *db, /* Database handle. */
132299 const char *zSql, /* UTF-8 encoded SQL statement. */
132300 int nBytes, /* Length of zSql in bytes. */
132301 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
132302 Vdbe *pReprepare, /* VM being reprepared */
132303 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
132304 const char **pzTail /* OUT: End of parsed string */
132305);
132306
132307
132308/*
132309** This is the callback routine for the code that initializes the
132310** database. See sqlite3Init() below for additional information.
132311** This routine is also called from the OP_ParseSchema opcode of the VDBE.
132312**
132313** Each callback contains the following information:
132314**
132315** argv[0] = type of object: "table", "index", "trigger", or "view".
132316** argv[1] = name of thing being created
132317** argv[2] = associated table if an index or trigger
132318** argv[3] = root page number for table or index. 0 for trigger or view.
132319** argv[4] = SQL text for the CREATE statement.
132320**
132321*/
132322SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
132323 InitData *pData = (InitData*)pInit;
132324 sqlite3 *db = pData->db;
132325 int iDb = pData->iDb;
132326
132327 assert( argc==5 );
132328 UNUSED_PARAMETER2(NotUsed, argc);
132329 assert( sqlite3_mutex_held(db->mutex) );
132330 db->mDbFlags |= DBFLAG_EncodingFixed;
132331 if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
132332 pData->nInitRow++;
132333 if( db->mallocFailed ){
132334 corruptSchema(pData, azObj: argv, zExtra: 0);
132335 return 1;
132336 }
132337
132338 assert( iDb>=0 && iDb<db->nDb );
132339 if( argv[3]==0 ){
132340 corruptSchema(pData, azObj: argv, zExtra: 0);
132341 }else if( argv[4]
132342 && 'c'==sqlite3UpperToLower[(unsigned char)argv[4][0]]
132343 && 'r'==sqlite3UpperToLower[(unsigned char)argv[4][1]] ){
132344 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
132345 ** But because db->init.busy is set to 1, no VDBE code is generated
132346 ** or executed. All the parser does is build the internal data
132347 ** structures that describe the table, index, or view.
132348 **
132349 ** No other valid SQL statement, other than the variable CREATE statements,
132350 ** can begin with the letters "C" and "R". Thus, it is not possible run
132351 ** any other kind of statement while parsing the schema, even a corrupt
132352 ** schema.
132353 */
132354 int rc;
132355 u8 saved_iDb = db->init.iDb;
132356 sqlite3_stmt *pStmt;
132357 TESTONLY(int rcp); /* Return code from sqlite3_prepare() */
132358
132359 assert( db->init.busy );
132360 db->init.iDb = iDb;
132361 if( sqlite3GetUInt32(z: argv[3], pI: &db->init.newTnum)==0
132362 || (db->init.newTnum>pData->mxPage && pData->mxPage>0)
132363 ){
132364 if( sqlite3Config.bExtraSchemaChecks ){
132365 corruptSchema(pData, azObj: argv, zExtra: "invalid rootpage");
132366 }
132367 }
132368 db->init.orphanTrigger = 0;
132369 db->init.azInit = (const char**)argv;
132370 pStmt = 0;
132371 TESTONLY(rcp = ) sqlite3Prepare(db, zSql: argv[4], nBytes: -1, prepFlags: 0, pReprepare: 0, ppStmt: &pStmt, pzTail: 0);
132372 rc = db->errCode;
132373 assert( (rc&0xFF)==(rcp&0xFF) );
132374 db->init.iDb = saved_iDb;
132375 /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */
132376 if( SQLITE_OK!=rc ){
132377 if( db->init.orphanTrigger ){
132378 assert( iDb==1 );
132379 }else{
132380 if( rc > pData->rc ) pData->rc = rc;
132381 if( rc==SQLITE_NOMEM ){
132382 sqlite3OomFault(db);
132383 }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){
132384 corruptSchema(pData, azObj: argv, zExtra: sqlite3_errmsg(db));
132385 }
132386 }
132387 }
132388 db->init.azInit = sqlite3StdType; /* Any array of string ptrs will do */
132389 sqlite3_finalize(pStmt);
132390 }else if( argv[1]==0 || (argv[4]!=0 && argv[4][0]!=0) ){
132391 corruptSchema(pData, azObj: argv, zExtra: 0);
132392 }else{
132393 /* If the SQL column is blank it means this is an index that
132394 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
132395 ** constraint for a CREATE TABLE. The index should have already
132396 ** been created when we processed the CREATE TABLE. All we have
132397 ** to do here is record the root page number for that index.
132398 */
132399 Index *pIndex;
132400 pIndex = sqlite3FindIndex(db, zName: argv[1], zDb: db->aDb[iDb].zDbSName);
132401 if( pIndex==0 ){
132402 corruptSchema(pData, azObj: argv, zExtra: "orphan index");
132403 }else
132404 if( sqlite3GetUInt32(z: argv[3],pI: &pIndex->tnum)==0
132405 || pIndex->tnum<2
132406 || pIndex->tnum>pData->mxPage
132407 || sqlite3IndexHasDuplicateRootPage(pIndex)
132408 ){
132409 if( sqlite3Config.bExtraSchemaChecks ){
132410 corruptSchema(pData, azObj: argv, zExtra: "invalid rootpage");
132411 }
132412 }
132413 }
132414 return 0;
132415}
132416
132417/*
132418** Attempt to read the database schema and initialize internal
132419** data structures for a single database file. The index of the
132420** database file is given by iDb. iDb==0 is used for the main
132421** database. iDb==1 should never be used. iDb>=2 is used for
132422** auxiliary databases. Return one of the SQLITE_ error codes to
132423** indicate success or failure.
132424*/
132425SQLITE_PRIVATE int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg, u32 mFlags){
132426 int rc;
132427 int i;
132428#ifndef SQLITE_OMIT_DEPRECATED
132429 int size;
132430#endif
132431 Db *pDb;
132432 char const *azArg[6];
132433 int meta[5];
132434 InitData initData;
132435 const char *zSchemaTabName;
132436 int openedTransaction = 0;
132437 int mask = ((db->mDbFlags & DBFLAG_EncodingFixed) | ~DBFLAG_EncodingFixed);
132438
132439 assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 );
132440 assert( iDb>=0 && iDb<db->nDb );
132441 assert( db->aDb[iDb].pSchema );
132442 assert( sqlite3_mutex_held(db->mutex) );
132443 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
132444
132445 db->init.busy = 1;
132446
132447 /* Construct the in-memory representation schema tables (sqlite_schema or
132448 ** sqlite_temp_schema) by invoking the parser directly. The appropriate
132449 ** table name will be inserted automatically by the parser so we can just
132450 ** use the abbreviation "x" here. The parser will also automatically tag
132451 ** the schema table as read-only. */
132452 azArg[0] = "table";
132453 azArg[1] = zSchemaTabName = SCHEMA_TABLE(iDb);
132454 azArg[2] = azArg[1];
132455 azArg[3] = "1";
132456 azArg[4] = "CREATE TABLE x(type text,name text,tbl_name text,"
132457 "rootpage int,sql text)";
132458 azArg[5] = 0;
132459 initData.db = db;
132460 initData.iDb = iDb;
132461 initData.rc = SQLITE_OK;
132462 initData.pzErrMsg = pzErrMsg;
132463 initData.mInitFlags = mFlags;
132464 initData.nInitRow = 0;
132465 initData.mxPage = 0;
132466 sqlite3InitCallback(pInit: &initData, argc: 5, argv: (char **)azArg, NotUsed: 0);
132467 db->mDbFlags &= mask;
132468 if( initData.rc ){
132469 rc = initData.rc;
132470 goto error_out;
132471 }
132472
132473 /* Create a cursor to hold the database open
132474 */
132475 pDb = &db->aDb[iDb];
132476 if( pDb->pBt==0 ){
132477 assert( iDb==1 );
132478 DbSetProperty(db, 1, DB_SchemaLoaded);
132479 rc = SQLITE_OK;
132480 goto error_out;
132481 }
132482
132483 /* If there is not already a read-only (or read-write) transaction opened
132484 ** on the b-tree database, open one now. If a transaction is opened, it
132485 ** will be closed before this function returns. */
132486 sqlite3BtreeEnter(p: pDb->pBt);
132487 if( sqlite3BtreeTxnState(p: pDb->pBt)==SQLITE_TXN_NONE ){
132488 rc = sqlite3BtreeBeginTrans(p: pDb->pBt, wrflag: 0, pSchemaVersion: 0);
132489 if( rc!=SQLITE_OK ){
132490 sqlite3SetString(pz: pzErrMsg, db, zNew: sqlite3ErrStr(rc));
132491 goto initone_error_out;
132492 }
132493 openedTransaction = 1;
132494 }
132495
132496 /* Get the database meta information.
132497 **
132498 ** Meta values are as follows:
132499 ** meta[0] Schema cookie. Changes with each schema change.
132500 ** meta[1] File format of schema layer.
132501 ** meta[2] Size of the page cache.
132502 ** meta[3] Largest rootpage (auto/incr_vacuum mode)
132503 ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
132504 ** meta[5] User version
132505 ** meta[6] Incremental vacuum mode
132506 ** meta[7] unused
132507 ** meta[8] unused
132508 ** meta[9] unused
132509 **
132510 ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
132511 ** the possible values of meta[4].
132512 */
132513 for(i=0; i<ArraySize(meta); i++){
132514 sqlite3BtreeGetMeta(p: pDb->pBt, idx: i+1, pMeta: (u32 *)&meta[i]);
132515 }
132516 if( (db->flags & SQLITE_ResetDatabase)!=0 ){
132517 memset(s: meta, c: 0, n: sizeof(meta));
132518 }
132519 pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
132520
132521 /* If opening a non-empty database, check the text encoding. For the
132522 ** main database, set sqlite3.enc to the encoding of the main database.
132523 ** For an attached db, it is an error if the encoding is not the same
132524 ** as sqlite3.enc.
132525 */
132526 if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */
132527 if( iDb==0 && (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
132528 u8 encoding;
132529#ifndef SQLITE_OMIT_UTF16
132530 /* If opening the main database, set ENC(db). */
132531 encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
132532 if( encoding==0 ) encoding = SQLITE_UTF8;
132533#else
132534 encoding = SQLITE_UTF8;
132535#endif
132536 sqlite3SetTextEncoding(db, enc: encoding);
132537 }else{
132538 /* If opening an attached database, the encoding much match ENC(db) */
132539 if( (meta[BTREE_TEXT_ENCODING-1] & 3)!=ENC(db) ){
132540 sqlite3SetString(pz: pzErrMsg, db, zNew: "attached databases must use the same"
132541 " text encoding as main database");
132542 rc = SQLITE_ERROR;
132543 goto initone_error_out;
132544 }
132545 }
132546 }
132547 pDb->pSchema->enc = ENC(db);
132548
132549 if( pDb->pSchema->cache_size==0 ){
132550#ifndef SQLITE_OMIT_DEPRECATED
132551 size = sqlite3AbsInt32(x: meta[BTREE_DEFAULT_CACHE_SIZE-1]);
132552 if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
132553 pDb->pSchema->cache_size = size;
132554#else
132555 pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
132556#endif
132557 sqlite3BtreeSetCacheSize(p: pDb->pBt, mxPage: pDb->pSchema->cache_size);
132558 }
132559
132560 /*
132561 ** file_format==1 Version 3.0.0.
132562 ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN
132563 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
132564 ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants
132565 */
132566 pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
132567 if( pDb->pSchema->file_format==0 ){
132568 pDb->pSchema->file_format = 1;
132569 }
132570 if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
132571 sqlite3SetString(pz: pzErrMsg, db, zNew: "unsupported file format");
132572 rc = SQLITE_ERROR;
132573 goto initone_error_out;
132574 }
132575
132576 /* Ticket #2804: When we open a database in the newer file format,
132577 ** clear the legacy_file_format pragma flag so that a VACUUM will
132578 ** not downgrade the database and thus invalidate any descending
132579 ** indices that the user might have created.
132580 */
132581 if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
132582 db->flags &= ~(u64)SQLITE_LegacyFileFmt;
132583 }
132584
132585 /* Read the schema information out of the schema tables
132586 */
132587 assert( db->init.busy );
132588 initData.mxPage = sqlite3BtreeLastPage(p: pDb->pBt);
132589 {
132590 char *zSql;
132591 zSql = sqlite3MPrintf(db,
132592 zFormat: "SELECT*FROM\"%w\".%s ORDER BY rowid",
132593 db->aDb[iDb].zDbSName, zSchemaTabName);
132594#ifndef SQLITE_OMIT_AUTHORIZATION
132595 {
132596 sqlite3_xauth xAuth;
132597 xAuth = db->xAuth;
132598 db->xAuth = 0;
132599#endif
132600 rc = sqlite3_exec(db, zSql, xCallback: sqlite3InitCallback, pArg: &initData, pzErrMsg: 0);
132601#ifndef SQLITE_OMIT_AUTHORIZATION
132602 db->xAuth = xAuth;
132603 }
132604#endif
132605 if( rc==SQLITE_OK ) rc = initData.rc;
132606 sqlite3DbFree(db, p: zSql);
132607#ifndef SQLITE_OMIT_ANALYZE
132608 if( rc==SQLITE_OK ){
132609 sqlite3AnalysisLoad(db, iDb);
132610 }
132611#endif
132612 }
132613 assert( pDb == &(db->aDb[iDb]) );
132614 if( db->mallocFailed ){
132615 rc = SQLITE_NOMEM_BKPT;
132616 sqlite3ResetAllSchemasOfConnection(db);
132617 pDb = &db->aDb[iDb];
132618 }else
132619 if( rc==SQLITE_OK || (db->flags&SQLITE_NoSchemaError)){
132620 /* Hack: If the SQLITE_NoSchemaError flag is set, then consider
132621 ** the schema loaded, even if errors (other than OOM) occurred. In
132622 ** this situation the current sqlite3_prepare() operation will fail,
132623 ** but the following one will attempt to compile the supplied statement
132624 ** against whatever subset of the schema was loaded before the error
132625 ** occurred.
132626 **
132627 ** The primary purpose of this is to allow access to the sqlite_schema
132628 ** table even when its contents have been corrupted.
132629 */
132630 DbSetProperty(db, iDb, DB_SchemaLoaded);
132631 rc = SQLITE_OK;
132632 }
132633
132634 /* Jump here for an error that occurs after successfully allocating
132635 ** curMain and calling sqlite3BtreeEnter(). For an error that occurs
132636 ** before that point, jump to error_out.
132637 */
132638initone_error_out:
132639 if( openedTransaction ){
132640 sqlite3BtreeCommit(p: pDb->pBt);
132641 }
132642 sqlite3BtreeLeave(p: pDb->pBt);
132643
132644error_out:
132645 if( rc ){
132646 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
132647 sqlite3OomFault(db);
132648 }
132649 sqlite3ResetOneSchema(db, iDb);
132650 }
132651 db->init.busy = 0;
132652 return rc;
132653}
132654
132655/*
132656** Initialize all database files - the main database file, the file
132657** used to store temporary tables, and any additional database files
132658** created using ATTACH statements. Return a success code. If an
132659** error occurs, write an error message into *pzErrMsg.
132660**
132661** After a database is initialized, the DB_SchemaLoaded bit is set
132662** bit is set in the flags field of the Db structure.
132663*/
132664SQLITE_PRIVATE int sqlite3Init(sqlite3 *db, char **pzErrMsg){
132665 int i, rc;
132666 int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange);
132667
132668 assert( sqlite3_mutex_held(db->mutex) );
132669 assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) );
132670 assert( db->init.busy==0 );
132671 ENC(db) = SCHEMA_ENC(db);
132672 assert( db->nDb>0 );
132673 /* Do the main schema first */
132674 if( !DbHasProperty(db, 0, DB_SchemaLoaded) ){
132675 rc = sqlite3InitOne(db, iDb: 0, pzErrMsg, mFlags: 0);
132676 if( rc ) return rc;
132677 }
132678 /* All other schemas after the main schema. The "temp" schema must be last */
132679 for(i=db->nDb-1; i>0; i--){
132680 assert( i==1 || sqlite3BtreeHoldsMutex(db->aDb[i].pBt) );
132681 if( !DbHasProperty(db, i, DB_SchemaLoaded) ){
132682 rc = sqlite3InitOne(db, iDb: i, pzErrMsg, mFlags: 0);
132683 if( rc ) return rc;
132684 }
132685 }
132686 if( commit_internal ){
132687 sqlite3CommitInternalChanges(db);
132688 }
132689 return SQLITE_OK;
132690}
132691
132692/*
132693** This routine is a no-op if the database schema is already initialized.
132694** Otherwise, the schema is loaded. An error code is returned.
132695*/
132696SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse){
132697 int rc = SQLITE_OK;
132698 sqlite3 *db = pParse->db;
132699 assert( sqlite3_mutex_held(db->mutex) );
132700 if( !db->init.busy ){
132701 rc = sqlite3Init(db, pzErrMsg: &pParse->zErrMsg);
132702 if( rc!=SQLITE_OK ){
132703 pParse->rc = rc;
132704 pParse->nErr++;
132705 }else if( db->noSharedCache ){
132706 db->mDbFlags |= DBFLAG_SchemaKnownOk;
132707 }
132708 }
132709 return rc;
132710}
132711
132712
132713/*
132714** Check schema cookies in all databases. If any cookie is out
132715** of date set pParse->rc to SQLITE_SCHEMA. If all schema cookies
132716** make no changes to pParse->rc.
132717*/
132718static void schemaIsValid(Parse *pParse){
132719 sqlite3 *db = pParse->db;
132720 int iDb;
132721 int rc;
132722 int cookie;
132723
132724 assert( pParse->checkSchema );
132725 assert( sqlite3_mutex_held(db->mutex) );
132726 for(iDb=0; iDb<db->nDb; iDb++){
132727 int openedTransaction = 0; /* True if a transaction is opened */
132728 Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */
132729 if( pBt==0 ) continue;
132730
132731 /* If there is not already a read-only (or read-write) transaction opened
132732 ** on the b-tree database, open one now. If a transaction is opened, it
132733 ** will be closed immediately after reading the meta-value. */
132734 if( sqlite3BtreeTxnState(p: pBt)==SQLITE_TXN_NONE ){
132735 rc = sqlite3BtreeBeginTrans(p: pBt, wrflag: 0, pSchemaVersion: 0);
132736 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
132737 sqlite3OomFault(db);
132738 pParse->rc = SQLITE_NOMEM;
132739 }
132740 if( rc!=SQLITE_OK ) return;
132741 openedTransaction = 1;
132742 }
132743
132744 /* Read the schema cookie from the database. If it does not match the
132745 ** value stored as part of the in-memory schema representation,
132746 ** set Parse.rc to SQLITE_SCHEMA. */
132747 sqlite3BtreeGetMeta(p: pBt, BTREE_SCHEMA_VERSION, pMeta: (u32 *)&cookie);
132748 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
132749 if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
132750 sqlite3ResetOneSchema(db, iDb);
132751 pParse->rc = SQLITE_SCHEMA;
132752 }
132753
132754 /* Close the transaction, if one was opened. */
132755 if( openedTransaction ){
132756 sqlite3BtreeCommit(p: pBt);
132757 }
132758 }
132759}
132760
132761/*
132762** Convert a schema pointer into the iDb index that indicates
132763** which database file in db->aDb[] the schema refers to.
132764**
132765** If the same database is attached more than once, the first
132766** attached database is returned.
132767*/
132768SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
132769 int i = -32768;
132770
132771 /* If pSchema is NULL, then return -32768. This happens when code in
132772 ** expr.c is trying to resolve a reference to a transient table (i.e. one
132773 ** created by a sub-select). In this case the return value of this
132774 ** function should never be used.
132775 **
132776 ** We return -32768 instead of the more usual -1 simply because using
132777 ** -32768 as the incorrect index into db->aDb[] is much
132778 ** more likely to cause a segfault than -1 (of course there are assert()
132779 ** statements too, but it never hurts to play the odds) and
132780 ** -32768 will still fit into a 16-bit signed integer.
132781 */
132782 assert( sqlite3_mutex_held(db->mutex) );
132783 if( pSchema ){
132784 for(i=0; 1; i++){
132785 assert( i<db->nDb );
132786 if( db->aDb[i].pSchema==pSchema ){
132787 break;
132788 }
132789 }
132790 assert( i>=0 && i<db->nDb );
132791 }
132792 return i;
132793}
132794
132795/*
132796** Free all memory allocations in the pParse object
132797*/
132798SQLITE_PRIVATE void sqlite3ParserReset(Parse *pParse){
132799 sqlite3 *db = pParse->db;
132800 while( pParse->pCleanup ){
132801 ParseCleanup *pCleanup = pParse->pCleanup;
132802 pParse->pCleanup = pCleanup->pNext;
132803 pCleanup->xCleanup(db, pCleanup->pPtr);
132804 sqlite3DbFreeNN(db, p: pCleanup);
132805 }
132806 sqlite3DbFree(db, p: pParse->aLabel);
132807 if( pParse->pConstExpr ){
132808 sqlite3ExprListDelete(db, pList: pParse->pConstExpr);
132809 }
132810 if( db ){
132811 assert( db->lookaside.bDisable >= pParse->disableLookaside );
132812 db->lookaside.bDisable -= pParse->disableLookaside;
132813 db->lookaside.sz = db->lookaside.bDisable ? 0 : db->lookaside.szTrue;
132814 }
132815 pParse->disableLookaside = 0;
132816}
132817
132818/*
132819** Add a new cleanup operation to a Parser. The cleanup should happen when
132820** the parser object is destroyed. But, beware: the cleanup might happen
132821** immediately.
132822**
132823** Use this mechanism for uncommon cleanups. There is a higher setup
132824** cost for this mechansim (an extra malloc), so it should not be used
132825** for common cleanups that happen on most calls. But for less
132826** common cleanups, we save a single NULL-pointer comparison in
132827** sqlite3ParserReset(), which reduces the total CPU cycle count.
132828**
132829** If a memory allocation error occurs, then the cleanup happens immediately.
132830** When either SQLITE_DEBUG or SQLITE_COVERAGE_TEST are defined, the
132831** pParse->earlyCleanup flag is set in that case. Calling code show verify
132832** that test cases exist for which this happens, to guard against possible
132833** use-after-free errors following an OOM. The preferred way to do this is
132834** to immediately follow the call to this routine with:
132835**
132836** testcase( pParse->earlyCleanup );
132837**
132838** This routine returns a copy of its pPtr input (the third parameter)
132839** except if an early cleanup occurs, in which case it returns NULL. So
132840** another way to check for early cleanup is to check the return value.
132841** Or, stop using the pPtr parameter with this call and use only its
132842** return value thereafter. Something like this:
132843**
132844** pObj = sqlite3ParserAddCleanup(pParse, destructor, pObj);
132845*/
132846SQLITE_PRIVATE void *sqlite3ParserAddCleanup(
132847 Parse *pParse, /* Destroy when this Parser finishes */
132848 void (*xCleanup)(sqlite3*,void*), /* The cleanup routine */
132849 void *pPtr /* Pointer to object to be cleaned up */
132850){
132851 ParseCleanup *pCleanup = sqlite3DbMallocRaw(db: pParse->db, n: sizeof(*pCleanup));
132852 if( pCleanup ){
132853 pCleanup->pNext = pParse->pCleanup;
132854 pParse->pCleanup = pCleanup;
132855 pCleanup->pPtr = pPtr;
132856 pCleanup->xCleanup = xCleanup;
132857 }else{
132858 xCleanup(pParse->db, pPtr);
132859 pPtr = 0;
132860#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
132861 pParse->earlyCleanup = 1;
132862#endif
132863 }
132864 return pPtr;
132865}
132866
132867/*
132868** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
132869*/
132870static int sqlite3Prepare(
132871 sqlite3 *db, /* Database handle. */
132872 const char *zSql, /* UTF-8 encoded SQL statement. */
132873 int nBytes, /* Length of zSql in bytes. */
132874 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
132875 Vdbe *pReprepare, /* VM being reprepared */
132876 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
132877 const char **pzTail /* OUT: End of parsed string */
132878){
132879 char *zErrMsg = 0; /* Error message */
132880 int rc = SQLITE_OK; /* Result code */
132881 int i; /* Loop counter */
132882 Parse sParse; /* Parsing context */
132883
132884 memset(s: &sParse, c: 0, PARSE_HDR_SZ);
132885 memset(PARSE_TAIL(&sParse), c: 0, PARSE_TAIL_SZ);
132886 sParse.pReprepare = pReprepare;
132887 assert( ppStmt && *ppStmt==0 );
132888 /* assert( !db->mallocFailed ); // not true with SQLITE_USE_ALLOCA */
132889 assert( sqlite3_mutex_held(db->mutex) );
132890
132891 /* For a long-term use prepared statement avoid the use of
132892 ** lookaside memory.
132893 */
132894 if( prepFlags & SQLITE_PREPARE_PERSISTENT ){
132895 sParse.disableLookaside++;
132896 DisableLookaside;
132897 }
132898 sParse.disableVtab = (prepFlags & SQLITE_PREPARE_NO_VTAB)!=0;
132899
132900 /* Check to verify that it is possible to get a read lock on all
132901 ** database schemas. The inability to get a read lock indicates that
132902 ** some other database connection is holding a write-lock, which in
132903 ** turn means that the other connection has made uncommitted changes
132904 ** to the schema.
132905 **
132906 ** Were we to proceed and prepare the statement against the uncommitted
132907 ** schema changes and if those schema changes are subsequently rolled
132908 ** back and different changes are made in their place, then when this
132909 ** prepared statement goes to run the schema cookie would fail to detect
132910 ** the schema change. Disaster would follow.
132911 **
132912 ** This thread is currently holding mutexes on all Btrees (because
132913 ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
132914 ** is not possible for another thread to start a new schema change
132915 ** while this routine is running. Hence, we do not need to hold
132916 ** locks on the schema, we just need to make sure nobody else is
132917 ** holding them.
132918 **
132919 ** Note that setting READ_UNCOMMITTED overrides most lock detection,
132920 ** but it does *not* override schema lock detection, so this all still
132921 ** works even if READ_UNCOMMITTED is set.
132922 */
132923 if( !db->noSharedCache ){
132924 for(i=0; i<db->nDb; i++) {
132925 Btree *pBt = db->aDb[i].pBt;
132926 if( pBt ){
132927 assert( sqlite3BtreeHoldsMutex(pBt) );
132928 rc = sqlite3BtreeSchemaLocked(p: pBt);
132929 if( rc ){
132930 const char *zDb = db->aDb[i].zDbSName;
132931 sqlite3ErrorWithMsg(db, err_code: rc, zFormat: "database schema is locked: %s", zDb);
132932 testcase( db->flags & SQLITE_ReadUncommit );
132933 goto end_prepare;
132934 }
132935 }
132936 }
132937 }
132938
132939 sqlite3VtabUnlockList(db);
132940
132941 sParse.db = db;
132942 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
132943 char *zSqlCopy;
132944 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
132945 testcase( nBytes==mxLen );
132946 testcase( nBytes==mxLen+1 );
132947 if( nBytes>mxLen ){
132948 sqlite3ErrorWithMsg(db, SQLITE_TOOBIG, zFormat: "statement too long");
132949 rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
132950 goto end_prepare;
132951 }
132952 zSqlCopy = sqlite3DbStrNDup(db, z: zSql, n: nBytes);
132953 if( zSqlCopy ){
132954 sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg);
132955 sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
132956 sqlite3DbFree(db, p: zSqlCopy);
132957 }else{
132958 sParse.zTail = &zSql[nBytes];
132959 }
132960 }else{
132961 sqlite3RunParser(&sParse, zSql, &zErrMsg);
132962 }
132963 assert( 0==sParse.nQueryLoop );
132964
132965 if( pzTail ){
132966 *pzTail = sParse.zTail;
132967 }
132968
132969 if( db->init.busy==0 ){
132970 sqlite3VdbeSetSql(p: sParse.pVdbe, z: zSql, n: (int)(sParse.zTail-zSql), prepFlags);
132971 }
132972 if( db->mallocFailed ){
132973 sParse.rc = SQLITE_NOMEM_BKPT;
132974 sParse.checkSchema = 0;
132975 }
132976 if( sParse.rc!=SQLITE_OK && sParse.rc!=SQLITE_DONE ){
132977 if( sParse.checkSchema && db->init.busy==0 ){
132978 schemaIsValid(pParse: &sParse);
132979 }
132980 if( sParse.pVdbe ){
132981 sqlite3VdbeFinalize(p: sParse.pVdbe);
132982 }
132983 assert( 0==(*ppStmt) );
132984 rc = sParse.rc;
132985 if( zErrMsg ){
132986 sqlite3ErrorWithMsg(db, err_code: rc, zFormat: "%s", zErrMsg);
132987 sqlite3DbFree(db, p: zErrMsg);
132988 }else{
132989 sqlite3Error(db, err_code: rc);
132990 }
132991 }else{
132992 assert( zErrMsg==0 );
132993 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
132994 rc = SQLITE_OK;
132995 sqlite3ErrorClear(db);
132996 }
132997
132998
132999 /* Delete any TriggerPrg structures allocated while parsing this statement. */
133000 while( sParse.pTriggerPrg ){
133001 TriggerPrg *pT = sParse.pTriggerPrg;
133002 sParse.pTriggerPrg = pT->pNext;
133003 sqlite3DbFree(db, p: pT);
133004 }
133005
133006end_prepare:
133007
133008 sqlite3ParserReset(pParse: &sParse);
133009 return rc;
133010}
133011static int sqlite3LockAndPrepare(
133012 sqlite3 *db, /* Database handle. */
133013 const char *zSql, /* UTF-8 encoded SQL statement. */
133014 int nBytes, /* Length of zSql in bytes. */
133015 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
133016 Vdbe *pOld, /* VM being reprepared */
133017 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
133018 const char **pzTail /* OUT: End of parsed string */
133019){
133020 int rc;
133021 int cnt = 0;
133022
133023#ifdef SQLITE_ENABLE_API_ARMOR
133024 if( ppStmt==0 ) return SQLITE_MISUSE_BKPT;
133025#endif
133026 *ppStmt = 0;
133027 if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
133028 return SQLITE_MISUSE_BKPT;
133029 }
133030 sqlite3_mutex_enter(p: db->mutex);
133031 sqlite3BtreeEnterAll(db);
133032 do{
133033 /* Make multiple attempts to compile the SQL, until it either succeeds
133034 ** or encounters a permanent error. A schema problem after one schema
133035 ** reset is considered a permanent error. */
133036 rc = sqlite3Prepare(db, zSql, nBytes, prepFlags, pReprepare: pOld, ppStmt, pzTail);
133037 assert( rc==SQLITE_OK || *ppStmt==0 );
133038 if( rc==SQLITE_OK || db->mallocFailed ) break;
133039 }while( rc==SQLITE_ERROR_RETRY
133040 || (rc==SQLITE_SCHEMA && (sqlite3ResetOneSchema(db,iDb: -1), cnt++)==0) );
133041 sqlite3BtreeLeaveAll(db);
133042 rc = sqlite3ApiExit(db, rc);
133043 assert( (rc&db->errMask)==rc );
133044 db->busyHandler.nBusy = 0;
133045 sqlite3_mutex_leave(p: db->mutex);
133046 return rc;
133047}
133048
133049
133050/*
133051** Rerun the compilation of a statement after a schema change.
133052**
133053** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
133054** if the statement cannot be recompiled because another connection has
133055** locked the sqlite3_schema table, return SQLITE_LOCKED. If any other error
133056** occurs, return SQLITE_SCHEMA.
133057*/
133058SQLITE_PRIVATE int sqlite3Reprepare(Vdbe *p){
133059 int rc;
133060 sqlite3_stmt *pNew;
133061 const char *zSql;
133062 sqlite3 *db;
133063 u8 prepFlags;
133064
133065 assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
133066 zSql = sqlite3_sql(pStmt: (sqlite3_stmt *)p);
133067 assert( zSql!=0 ); /* Reprepare only called for prepare_v2() statements */
133068 db = sqlite3VdbeDb(v: p);
133069 assert( sqlite3_mutex_held(db->mutex) );
133070 prepFlags = sqlite3VdbePrepareFlags(v: p);
133071 rc = sqlite3LockAndPrepare(db, zSql, nBytes: -1, prepFlags, pOld: p, ppStmt: &pNew, pzTail: 0);
133072 if( rc ){
133073 if( rc==SQLITE_NOMEM ){
133074 sqlite3OomFault(db);
133075 }
133076 assert( pNew==0 );
133077 return rc;
133078 }else{
133079 assert( pNew!=0 );
133080 }
133081 sqlite3VdbeSwap(pA: (Vdbe*)pNew, pB: p);
133082 sqlite3TransferBindings(pFromStmt: pNew, pToStmt: (sqlite3_stmt*)p);
133083 sqlite3VdbeResetStepResult(p: (Vdbe*)pNew);
133084 sqlite3VdbeFinalize(p: (Vdbe*)pNew);
133085 return SQLITE_OK;
133086}
133087
133088
133089/*
133090** Two versions of the official API. Legacy and new use. In the legacy
133091** version, the original SQL text is not saved in the prepared statement
133092** and so if a schema change occurs, SQLITE_SCHEMA is returned by
133093** sqlite3_step(). In the new version, the original SQL text is retained
133094** and the statement is automatically recompiled if an schema change
133095** occurs.
133096*/
133097SQLITE_API int sqlite3_prepare(
133098 sqlite3 *db, /* Database handle. */
133099 const char *zSql, /* UTF-8 encoded SQL statement. */
133100 int nBytes, /* Length of zSql in bytes. */
133101 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
133102 const char **pzTail /* OUT: End of parsed string */
133103){
133104 int rc;
133105 rc = sqlite3LockAndPrepare(db,zSql,nBytes,prepFlags: 0,pOld: 0,ppStmt,pzTail);
133106 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
133107 return rc;
133108}
133109SQLITE_API int sqlite3_prepare_v2(
133110 sqlite3 *db, /* Database handle. */
133111 const char *zSql, /* UTF-8 encoded SQL statement. */
133112 int nBytes, /* Length of zSql in bytes. */
133113 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
133114 const char **pzTail /* OUT: End of parsed string */
133115){
133116 int rc;
133117 /* EVIDENCE-OF: R-37923-12173 The sqlite3_prepare_v2() interface works
133118 ** exactly the same as sqlite3_prepare_v3() with a zero prepFlags
133119 ** parameter.
133120 **
133121 ** Proof in that the 5th parameter to sqlite3LockAndPrepare is 0 */
133122 rc = sqlite3LockAndPrepare(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL,pOld: 0,
133123 ppStmt,pzTail);
133124 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );
133125 return rc;
133126}
133127SQLITE_API int sqlite3_prepare_v3(
133128 sqlite3 *db, /* Database handle. */
133129 const char *zSql, /* UTF-8 encoded SQL statement. */
133130 int nBytes, /* Length of zSql in bytes. */
133131 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
133132 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
133133 const char **pzTail /* OUT: End of parsed string */
133134){
133135 int rc;
133136 /* EVIDENCE-OF: R-56861-42673 sqlite3_prepare_v3() differs from
133137 ** sqlite3_prepare_v2() only in having the extra prepFlags parameter,
133138 ** which is a bit array consisting of zero or more of the
133139 ** SQLITE_PREPARE_* flags.
133140 **
133141 ** Proof by comparison to the implementation of sqlite3_prepare_v2()
133142 ** directly above. */
133143 rc = sqlite3LockAndPrepare(db,zSql,nBytes,
133144 SQLITE_PREPARE_SAVESQL|(prepFlags&SQLITE_PREPARE_MASK),
133145 pOld: 0,ppStmt,pzTail);
133146 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );
133147 return rc;
133148}
133149
133150
133151#ifndef SQLITE_OMIT_UTF16
133152/*
133153** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
133154*/
133155static int sqlite3Prepare16(
133156 sqlite3 *db, /* Database handle. */
133157 const void *zSql, /* UTF-16 encoded SQL statement. */
133158 int nBytes, /* Length of zSql in bytes. */
133159 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
133160 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
133161 const void **pzTail /* OUT: End of parsed string */
133162){
133163 /* This function currently works by first transforming the UTF-16
133164 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
133165 ** tricky bit is figuring out the pointer to return in *pzTail.
133166 */
133167 char *zSql8;
133168 const char *zTail8 = 0;
133169 int rc = SQLITE_OK;
133170
133171#ifdef SQLITE_ENABLE_API_ARMOR
133172 if( ppStmt==0 ) return SQLITE_MISUSE_BKPT;
133173#endif
133174 *ppStmt = 0;
133175 if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
133176 return SQLITE_MISUSE_BKPT;
133177 }
133178 if( nBytes>=0 ){
133179 int sz;
133180 const char *z = (const char*)zSql;
133181 for(sz=0; sz<nBytes && (z[sz]!=0 || z[sz+1]!=0); sz += 2){}
133182 nBytes = sz;
133183 }
133184 sqlite3_mutex_enter(p: db->mutex);
133185 zSql8 = sqlite3Utf16to8(db, z: zSql, nByte: nBytes, SQLITE_UTF16NATIVE);
133186 if( zSql8 ){
133187 rc = sqlite3LockAndPrepare(db, zSql: zSql8, nBytes: -1, prepFlags, pOld: 0, ppStmt, pzTail: &zTail8);
133188 }
133189
133190 if( zTail8 && pzTail ){
133191 /* If sqlite3_prepare returns a tail pointer, we calculate the
133192 ** equivalent pointer into the UTF-16 string by counting the unicode
133193 ** characters between zSql8 and zTail8, and then returning a pointer
133194 ** the same number of characters into the UTF-16 string.
133195 */
133196 int chars_parsed = sqlite3Utf8CharLen(zIn: zSql8, nByte: (int)(zTail8-zSql8));
133197 *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zIn: zSql, nChar: chars_parsed);
133198 }
133199 sqlite3DbFree(db, p: zSql8);
133200 rc = sqlite3ApiExit(db, rc);
133201 sqlite3_mutex_leave(p: db->mutex);
133202 return rc;
133203}
133204
133205/*
133206** Two versions of the official API. Legacy and new use. In the legacy
133207** version, the original SQL text is not saved in the prepared statement
133208** and so if a schema change occurs, SQLITE_SCHEMA is returned by
133209** sqlite3_step(). In the new version, the original SQL text is retained
133210** and the statement is automatically recompiled if an schema change
133211** occurs.
133212*/
133213SQLITE_API int sqlite3_prepare16(
133214 sqlite3 *db, /* Database handle. */
133215 const void *zSql, /* UTF-16 encoded SQL statement. */
133216 int nBytes, /* Length of zSql in bytes. */
133217 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
133218 const void **pzTail /* OUT: End of parsed string */
133219){
133220 int rc;
133221 rc = sqlite3Prepare16(db,zSql,nBytes,prepFlags: 0,ppStmt,pzTail);
133222 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
133223 return rc;
133224}
133225SQLITE_API int sqlite3_prepare16_v2(
133226 sqlite3 *db, /* Database handle. */
133227 const void *zSql, /* UTF-16 encoded SQL statement. */
133228 int nBytes, /* Length of zSql in bytes. */
133229 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
133230 const void **pzTail /* OUT: End of parsed string */
133231){
133232 int rc;
133233 rc = sqlite3Prepare16(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL,ppStmt,pzTail);
133234 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
133235 return rc;
133236}
133237SQLITE_API int sqlite3_prepare16_v3(
133238 sqlite3 *db, /* Database handle. */
133239 const void *zSql, /* UTF-16 encoded SQL statement. */
133240 int nBytes, /* Length of zSql in bytes. */
133241 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
133242 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
133243 const void **pzTail /* OUT: End of parsed string */
133244){
133245 int rc;
133246 rc = sqlite3Prepare16(db,zSql,nBytes,
133247 SQLITE_PREPARE_SAVESQL|(prepFlags&SQLITE_PREPARE_MASK),
133248 ppStmt,pzTail);
133249 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
133250 return rc;
133251}
133252
133253#endif /* SQLITE_OMIT_UTF16 */
133254
133255/************** End of prepare.c *********************************************/
133256/************** Begin file select.c ******************************************/
133257/*
133258** 2001 September 15
133259**
133260** The author disclaims copyright to this source code. In place of
133261** a legal notice, here is a blessing:
133262**
133263** May you do good and not evil.
133264** May you find forgiveness for yourself and forgive others.
133265** May you share freely, never taking more than you give.
133266**
133267*************************************************************************
133268** This file contains C code routines that are called by the parser
133269** to handle SELECT statements in SQLite.
133270*/
133271/* #include "sqliteInt.h" */
133272
133273/*
133274** An instance of the following object is used to record information about
133275** how to process the DISTINCT keyword, to simplify passing that information
133276** into the selectInnerLoop() routine.
133277*/
133278typedef struct DistinctCtx DistinctCtx;
133279struct DistinctCtx {
133280 u8 isTnct; /* True if the DISTINCT keyword is present */
133281 u8 eTnctType; /* One of the WHERE_DISTINCT_* operators */
133282 int tabTnct; /* Ephemeral table used for DISTINCT processing */
133283 int addrTnct; /* Address of OP_OpenEphemeral opcode for tabTnct */
133284};
133285
133286/*
133287** An instance of the following object is used to record information about
133288** the ORDER BY (or GROUP BY) clause of query is being coded.
133289**
133290** The aDefer[] array is used by the sorter-references optimization. For
133291** example, assuming there is no index that can be used for the ORDER BY,
133292** for the query:
133293**
133294** SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10;
133295**
133296** it may be more efficient to add just the "a" values to the sorter, and
133297** retrieve the associated "bigblob" values directly from table t1 as the
133298** 10 smallest "a" values are extracted from the sorter.
133299**
133300** When the sorter-reference optimization is used, there is one entry in the
133301** aDefer[] array for each database table that may be read as values are
133302** extracted from the sorter.
133303*/
133304typedef struct SortCtx SortCtx;
133305struct SortCtx {
133306 ExprList *pOrderBy; /* The ORDER BY (or GROUP BY clause) */
133307 int nOBSat; /* Number of ORDER BY terms satisfied by indices */
133308 int iECursor; /* Cursor number for the sorter */
133309 int regReturn; /* Register holding block-output return address */
133310 int labelBkOut; /* Start label for the block-output subroutine */
133311 int addrSortIndex; /* Address of the OP_SorterOpen or OP_OpenEphemeral */
133312 int labelDone; /* Jump here when done, ex: LIMIT reached */
133313 int labelOBLopt; /* Jump here when sorter is full */
133314 u8 sortFlags; /* Zero or more SORTFLAG_* bits */
133315#ifdef SQLITE_ENABLE_SORTER_REFERENCES
133316 u8 nDefer; /* Number of valid entries in aDefer[] */
133317 struct DeferredCsr {
133318 Table *pTab; /* Table definition */
133319 int iCsr; /* Cursor number for table */
133320 int nKey; /* Number of PK columns for table pTab (>=1) */
133321 } aDefer[4];
133322#endif
133323 struct RowLoadInfo *pDeferredRowLoad; /* Deferred row loading info or NULL */
133324};
133325#define SORTFLAG_UseSorter 0x01 /* Use SorterOpen instead of OpenEphemeral */
133326
133327/*
133328** Delete all the content of a Select structure. Deallocate the structure
133329** itself depending on the value of bFree
133330**
133331** If bFree==1, call sqlite3DbFree() on the p object.
133332** If bFree==0, Leave the first Select object unfreed
133333*/
133334static void clearSelect(sqlite3 *db, Select *p, int bFree){
133335 while( p ){
133336 Select *pPrior = p->pPrior;
133337 sqlite3ExprListDelete(db, pList: p->pEList);
133338 sqlite3SrcListDelete(db, pList: p->pSrc);
133339 sqlite3ExprDelete(db, p: p->pWhere);
133340 sqlite3ExprListDelete(db, pList: p->pGroupBy);
133341 sqlite3ExprDelete(db, p: p->pHaving);
133342 sqlite3ExprListDelete(db, pList: p->pOrderBy);
133343 sqlite3ExprDelete(db, p: p->pLimit);
133344 if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, pWith: p->pWith);
133345#ifndef SQLITE_OMIT_WINDOWFUNC
133346 if( OK_IF_ALWAYS_TRUE(p->pWinDefn) ){
133347 sqlite3WindowListDelete(db, p: p->pWinDefn);
133348 }
133349 while( p->pWin ){
133350 assert( p->pWin->ppThis==&p->pWin );
133351 sqlite3WindowUnlinkFromSelect(p->pWin);
133352 }
133353#endif
133354 if( bFree ) sqlite3DbFreeNN(db, p);
133355 p = pPrior;
133356 bFree = 1;
133357 }
133358}
133359
133360/*
133361** Initialize a SelectDest structure.
133362*/
133363SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){
133364 pDest->eDest = (u8)eDest;
133365 pDest->iSDParm = iParm;
133366 pDest->iSDParm2 = 0;
133367 pDest->zAffSdst = 0;
133368 pDest->iSdst = 0;
133369 pDest->nSdst = 0;
133370}
133371
133372
133373/*
133374** Allocate a new Select structure and return a pointer to that
133375** structure.
133376*/
133377SQLITE_PRIVATE Select *sqlite3SelectNew(
133378 Parse *pParse, /* Parsing context */
133379 ExprList *pEList, /* which columns to include in the result */
133380 SrcList *pSrc, /* the FROM clause -- which tables to scan */
133381 Expr *pWhere, /* the WHERE clause */
133382 ExprList *pGroupBy, /* the GROUP BY clause */
133383 Expr *pHaving, /* the HAVING clause */
133384 ExprList *pOrderBy, /* the ORDER BY clause */
133385 u32 selFlags, /* Flag parameters, such as SF_Distinct */
133386 Expr *pLimit /* LIMIT value. NULL means not used */
133387){
133388 Select *pNew, *pAllocated;
133389 Select standin;
133390 pAllocated = pNew = sqlite3DbMallocRawNN(db: pParse->db, n: sizeof(*pNew) );
133391 if( pNew==0 ){
133392 assert( pParse->db->mallocFailed );
133393 pNew = &standin;
133394 }
133395 if( pEList==0 ){
133396 pEList = sqlite3ExprListAppend(pParse, pList: 0,
133397 pExpr: sqlite3Expr(db: pParse->db,TK_ASTERISK,zToken: 0));
133398 }
133399 pNew->pEList = pEList;
133400 pNew->op = TK_SELECT;
133401 pNew->selFlags = selFlags;
133402 pNew->iLimit = 0;
133403 pNew->iOffset = 0;
133404 pNew->selId = ++pParse->nSelect;
133405 pNew->addrOpenEphm[0] = -1;
133406 pNew->addrOpenEphm[1] = -1;
133407 pNew->nSelectRow = 0;
133408 if( pSrc==0 ) pSrc = sqlite3DbMallocZero(db: pParse->db, n: sizeof(*pSrc));
133409 pNew->pSrc = pSrc;
133410 pNew->pWhere = pWhere;
133411 pNew->pGroupBy = pGroupBy;
133412 pNew->pHaving = pHaving;
133413 pNew->pOrderBy = pOrderBy;
133414 pNew->pPrior = 0;
133415 pNew->pNext = 0;
133416 pNew->pLimit = pLimit;
133417 pNew->pWith = 0;
133418#ifndef SQLITE_OMIT_WINDOWFUNC
133419 pNew->pWin = 0;
133420 pNew->pWinDefn = 0;
133421#endif
133422 if( pParse->db->mallocFailed ) {
133423 clearSelect(db: pParse->db, p: pNew, bFree: pNew!=&standin);
133424 pAllocated = 0;
133425 }else{
133426 assert( pNew->pSrc!=0 || pParse->nErr>0 );
133427 }
133428 return pAllocated;
133429}
133430
133431
133432/*
133433** Delete the given Select structure and all of its substructures.
133434*/
133435SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){
133436 if( OK_IF_ALWAYS_TRUE(p) ) clearSelect(db, p, bFree: 1);
133437}
133438
133439/*
133440** Return a pointer to the right-most SELECT statement in a compound.
133441*/
133442static Select *findRightmost(Select *p){
133443 while( p->pNext ) p = p->pNext;
133444 return p;
133445}
133446
133447/*
133448** Given 1 to 3 identifiers preceding the JOIN keyword, determine the
133449** type of join. Return an integer constant that expresses that type
133450** in terms of the following bit values:
133451**
133452** JT_INNER
133453** JT_CROSS
133454** JT_OUTER
133455** JT_NATURAL
133456** JT_LEFT
133457** JT_RIGHT
133458**
133459** A full outer join is the combination of JT_LEFT and JT_RIGHT.
133460**
133461** If an illegal or unsupported join type is seen, then still return
133462** a join type, but put an error in the pParse structure.
133463*/
133464SQLITE_PRIVATE int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
133465 int jointype = 0;
133466 Token *apAll[3];
133467 Token *p;
133468 /* 0123456789 123456789 123456789 123 */
133469 static const char zKeyText[] = "naturaleftouterightfullinnercross";
133470 static const struct {
133471 u8 i; /* Beginning of keyword text in zKeyText[] */
133472 u8 nChar; /* Length of the keyword in characters */
133473 u8 code; /* Join type mask */
133474 } aKeyword[] = {
133475 /* natural */ { 0, 7, JT_NATURAL },
133476 /* left */ { 6, 4, JT_LEFT|JT_OUTER },
133477 /* outer */ { 10, 5, JT_OUTER },
133478 /* right */ { 14, 5, JT_RIGHT|JT_OUTER },
133479 /* full */ { 19, 4, JT_LEFT|JT_RIGHT|JT_OUTER },
133480 /* inner */ { 23, 5, JT_INNER },
133481 /* cross */ { 28, 5, JT_INNER|JT_CROSS },
133482 };
133483 int i, j;
133484 apAll[0] = pA;
133485 apAll[1] = pB;
133486 apAll[2] = pC;
133487 for(i=0; i<3 && apAll[i]; i++){
133488 p = apAll[i];
133489 for(j=0; j<ArraySize(aKeyword); j++){
133490 if( p->n==aKeyword[j].nChar
133491 && sqlite3StrNICmp(zLeft: (char*)p->z, zRight: &zKeyText[aKeyword[j].i], N: p->n)==0 ){
133492 jointype |= aKeyword[j].code;
133493 break;
133494 }
133495 }
133496 testcase( j==0 || j==1 || j==2 || j==3 || j==4 || j==5 || j==6 );
133497 if( j>=ArraySize(aKeyword) ){
133498 jointype |= JT_ERROR;
133499 break;
133500 }
133501 }
133502 if(
133503 (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
133504 (jointype & JT_ERROR)!=0
133505 ){
133506 const char *zSp = " ";
133507 assert( pB!=0 );
133508 if( pC==0 ){ zSp++; }
133509 sqlite3ErrorMsg(pParse, zFormat: "unknown or unsupported join type: "
133510 "%T %T%s%T", pA, pB, zSp, pC);
133511 jointype = JT_INNER;
133512 }else if( (jointype & JT_OUTER)!=0
133513 && (jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ){
133514 sqlite3ErrorMsg(pParse,
133515 zFormat: "RIGHT and FULL OUTER JOINs are not currently supported");
133516 jointype = JT_INNER;
133517 }
133518 return jointype;
133519}
133520
133521/*
133522** Return the index of a column in a table. Return -1 if the column
133523** is not contained in the table.
133524*/
133525SQLITE_PRIVATE int sqlite3ColumnIndex(Table *pTab, const char *zCol){
133526 int i;
133527 u8 h = sqlite3StrIHash(z: zCol);
133528 Column *pCol;
133529 for(pCol=pTab->aCol, i=0; i<pTab->nCol; pCol++, i++){
133530 if( pCol->hName==h && sqlite3StrICmp(zLeft: pCol->zCnName, zRight: zCol)==0 ) return i;
133531 }
133532 return -1;
133533}
133534
133535/*
133536** Search the first N tables in pSrc, from left to right, looking for a
133537** table that has a column named zCol.
133538**
133539** When found, set *piTab and *piCol to the table index and column index
133540** of the matching column and return TRUE.
133541**
133542** If not found, return FALSE.
133543*/
133544static int tableAndColumnIndex(
133545 SrcList *pSrc, /* Array of tables to search */
133546 int N, /* Number of tables in pSrc->a[] to search */
133547 const char *zCol, /* Name of the column we are looking for */
133548 int *piTab, /* Write index of pSrc->a[] here */
133549 int *piCol, /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
133550 int bIgnoreHidden /* True to ignore hidden columns */
133551){
133552 int i; /* For looping over tables in pSrc */
133553 int iCol; /* Index of column matching zCol */
133554
133555 assert( (piTab==0)==(piCol==0) ); /* Both or neither are NULL */
133556 for(i=0; i<N; i++){
133557 iCol = sqlite3ColumnIndex(pTab: pSrc->a[i].pTab, zCol);
133558 if( iCol>=0
133559 && (bIgnoreHidden==0 || IsHiddenColumn(&pSrc->a[i].pTab->aCol[iCol])==0)
133560 ){
133561 if( piTab ){
133562 *piTab = i;
133563 *piCol = iCol;
133564 }
133565 return 1;
133566 }
133567 }
133568 return 0;
133569}
133570
133571/*
133572** This function is used to add terms implied by JOIN syntax to the
133573** WHERE clause expression of a SELECT statement. The new term, which
133574** is ANDed with the existing WHERE clause, is of the form:
133575**
133576** (tab1.col1 = tab2.col2)
133577**
133578** where tab1 is the iSrc'th table in SrcList pSrc and tab2 is the
133579** (iSrc+1)'th. Column col1 is column iColLeft of tab1, and col2 is
133580** column iColRight of tab2.
133581*/
133582static void addWhereTerm(
133583 Parse *pParse, /* Parsing context */
133584 SrcList *pSrc, /* List of tables in FROM clause */
133585 int iLeft, /* Index of first table to join in pSrc */
133586 int iColLeft, /* Index of column in first table */
133587 int iRight, /* Index of second table in pSrc */
133588 int iColRight, /* Index of column in second table */
133589 int isOuterJoin, /* True if this is an OUTER join */
133590 Expr **ppWhere /* IN/OUT: The WHERE clause to add to */
133591){
133592 sqlite3 *db = pParse->db;
133593 Expr *pE1;
133594 Expr *pE2;
133595 Expr *pEq;
133596
133597 assert( iLeft<iRight );
133598 assert( pSrc->nSrc>iRight );
133599 assert( pSrc->a[iLeft].pTab );
133600 assert( pSrc->a[iRight].pTab );
133601
133602 pE1 = sqlite3CreateColumnExpr(db, pSrc, iSrc: iLeft, iCol: iColLeft);
133603 pE2 = sqlite3CreateColumnExpr(db, pSrc, iSrc: iRight, iCol: iColRight);
133604
133605 pEq = sqlite3PExpr(pParse, TK_EQ, pLeft: pE1, pRight: pE2);
133606 assert( pE2!=0 || pEq==0 ); /* Due to db->mallocFailed test
133607 ** in sqlite3DbMallocRawNN() called from
133608 ** sqlite3PExpr(). */
133609 if( pEq && isOuterJoin ){
133610 ExprSetProperty(pEq, EP_FromJoin);
133611 assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) );
133612 ExprSetVVAProperty(pEq, EP_NoReduce);
133613 pEq->iRightJoinTable = pE2->iTable;
133614 }
133615 *ppWhere = sqlite3ExprAnd(pParse, pLeft: *ppWhere, pRight: pEq);
133616}
133617
133618/*
133619** Set the EP_FromJoin property on all terms of the given expression.
133620** And set the Expr.iRightJoinTable to iTable for every term in the
133621** expression.
133622**
133623** The EP_FromJoin property is used on terms of an expression to tell
133624** the LEFT OUTER JOIN processing logic that this term is part of the
133625** join restriction specified in the ON or USING clause and not a part
133626** of the more general WHERE clause. These terms are moved over to the
133627** WHERE clause during join processing but we need to remember that they
133628** originated in the ON or USING clause.
133629**
133630** The Expr.iRightJoinTable tells the WHERE clause processing that the
133631** expression depends on table iRightJoinTable even if that table is not
133632** explicitly mentioned in the expression. That information is needed
133633** for cases like this:
133634**
133635** SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
133636**
133637** The where clause needs to defer the handling of the t1.x=5
133638** term until after the t2 loop of the join. In that way, a
133639** NULL t2 row will be inserted whenever t1.x!=5. If we do not
133640** defer the handling of t1.x=5, it will be processed immediately
133641** after the t1 loop and rows with t1.x!=5 will never appear in
133642** the output, which is incorrect.
133643*/
133644SQLITE_PRIVATE void sqlite3SetJoinExpr(Expr *p, int iTable){
133645 while( p ){
133646 ExprSetProperty(p, EP_FromJoin);
133647 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
133648 ExprSetVVAProperty(p, EP_NoReduce);
133649 p->iRightJoinTable = iTable;
133650 if( p->op==TK_FUNCTION ){
133651 assert( ExprUseXList(p) );
133652 if( p->x.pList ){
133653 int i;
133654 for(i=0; i<p->x.pList->nExpr; i++){
133655 sqlite3SetJoinExpr(p: p->x.pList->a[i].pExpr, iTable);
133656 }
133657 }
133658 }
133659 sqlite3SetJoinExpr(p: p->pLeft, iTable);
133660 p = p->pRight;
133661 }
133662}
133663
133664/* Undo the work of sqlite3SetJoinExpr(). In the expression p, convert every
133665** term that is marked with EP_FromJoin and iRightJoinTable==iTable into
133666** an ordinary term that omits the EP_FromJoin mark.
133667**
133668** This happens when a LEFT JOIN is simplified into an ordinary JOIN.
133669*/
133670static void unsetJoinExpr(Expr *p, int iTable){
133671 while( p ){
133672 if( ExprHasProperty(p, EP_FromJoin)
133673 && (iTable<0 || p->iRightJoinTable==iTable) ){
133674 ExprClearProperty(p, EP_FromJoin);
133675 }
133676 if( p->op==TK_COLUMN && p->iTable==iTable ){
133677 ExprClearProperty(p, EP_CanBeNull);
133678 }
133679 if( p->op==TK_FUNCTION ){
133680 assert( ExprUseXList(p) );
133681 if( p->x.pList ){
133682 int i;
133683 for(i=0; i<p->x.pList->nExpr; i++){
133684 unsetJoinExpr(p: p->x.pList->a[i].pExpr, iTable);
133685 }
133686 }
133687 }
133688 unsetJoinExpr(p: p->pLeft, iTable);
133689 p = p->pRight;
133690 }
133691}
133692
133693/*
133694** This routine processes the join information for a SELECT statement.
133695** ON and USING clauses are converted into extra terms of the WHERE clause.
133696** NATURAL joins also create extra WHERE clause terms.
133697**
133698** The terms of a FROM clause are contained in the Select.pSrc structure.
133699** The left most table is the first entry in Select.pSrc. The right-most
133700** table is the last entry. The join operator is held in the entry to
133701** the left. Thus entry 0 contains the join operator for the join between
133702** entries 0 and 1. Any ON or USING clauses associated with the join are
133703** also attached to the left entry.
133704**
133705** This routine returns the number of errors encountered.
133706*/
133707static int sqliteProcessJoin(Parse *pParse, Select *p){
133708 SrcList *pSrc; /* All tables in the FROM clause */
133709 int i, j; /* Loop counters */
133710 SrcItem *pLeft; /* Left table being joined */
133711 SrcItem *pRight; /* Right table being joined */
133712
133713 pSrc = p->pSrc;
133714 pLeft = &pSrc->a[0];
133715 pRight = &pLeft[1];
133716 for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
133717 Table *pRightTab = pRight->pTab;
133718 int isOuter;
133719
133720 if( NEVER(pLeft->pTab==0 || pRightTab==0) ) continue;
133721 isOuter = (pRight->fg.jointype & JT_OUTER)!=0;
133722
133723 /* When the NATURAL keyword is present, add WHERE clause terms for
133724 ** every column that the two tables have in common.
133725 */
133726 if( pRight->fg.jointype & JT_NATURAL ){
133727 if( pRight->pOn || pRight->pUsing ){
133728 sqlite3ErrorMsg(pParse, zFormat: "a NATURAL join may not have "
133729 "an ON or USING clause", 0);
133730 return 1;
133731 }
133732 for(j=0; j<pRightTab->nCol; j++){
133733 char *zName; /* Name of column in the right table */
133734 int iLeft; /* Matching left table */
133735 int iLeftCol; /* Matching column in the left table */
133736
133737 if( IsHiddenColumn(&pRightTab->aCol[j]) ) continue;
133738 zName = pRightTab->aCol[j].zCnName;
133739 if( tableAndColumnIndex(pSrc, N: i+1, zCol: zName, piTab: &iLeft, piCol: &iLeftCol, bIgnoreHidden: 1) ){
133740 addWhereTerm(pParse, pSrc, iLeft, iColLeft: iLeftCol, iRight: i+1, iColRight: j,
133741 isOuterJoin: isOuter, ppWhere: &p->pWhere);
133742 }
133743 }
133744 }
133745
133746 /* Disallow both ON and USING clauses in the same join
133747 */
133748 if( pRight->pOn && pRight->pUsing ){
133749 sqlite3ErrorMsg(pParse, zFormat: "cannot have both ON and USING "
133750 "clauses in the same join");
133751 return 1;
133752 }
133753
133754 /* Add the ON clause to the end of the WHERE clause, connected by
133755 ** an AND operator.
133756 */
133757 if( pRight->pOn ){
133758 if( isOuter ) sqlite3SetJoinExpr(p: pRight->pOn, iTable: pRight->iCursor);
133759 p->pWhere = sqlite3ExprAnd(pParse, pLeft: p->pWhere, pRight: pRight->pOn);
133760 pRight->pOn = 0;
133761 }
133762
133763 /* Create extra terms on the WHERE clause for each column named
133764 ** in the USING clause. Example: If the two tables to be joined are
133765 ** A and B and the USING clause names X, Y, and Z, then add this
133766 ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
133767 ** Report an error if any column mentioned in the USING clause is
133768 ** not contained in both tables to be joined.
133769 */
133770 if( pRight->pUsing ){
133771 IdList *pList = pRight->pUsing;
133772 for(j=0; j<pList->nId; j++){
133773 char *zName; /* Name of the term in the USING clause */
133774 int iLeft; /* Table on the left with matching column name */
133775 int iLeftCol; /* Column number of matching column on the left */
133776 int iRightCol; /* Column number of matching column on the right */
133777
133778 zName = pList->a[j].zName;
133779 iRightCol = sqlite3ColumnIndex(pTab: pRightTab, zCol: zName);
133780 if( iRightCol<0
133781 || !tableAndColumnIndex(pSrc, N: i+1, zCol: zName, piTab: &iLeft, piCol: &iLeftCol, bIgnoreHidden: 0)
133782 ){
133783 sqlite3ErrorMsg(pParse, zFormat: "cannot join using column %s - column "
133784 "not present in both tables", zName);
133785 return 1;
133786 }
133787 addWhereTerm(pParse, pSrc, iLeft, iColLeft: iLeftCol, iRight: i+1, iColRight: iRightCol,
133788 isOuterJoin: isOuter, ppWhere: &p->pWhere);
133789 }
133790 }
133791 }
133792 return 0;
133793}
133794
133795/*
133796** An instance of this object holds information (beyond pParse and pSelect)
133797** needed to load the next result row that is to be added to the sorter.
133798*/
133799typedef struct RowLoadInfo RowLoadInfo;
133800struct RowLoadInfo {
133801 int regResult; /* Store results in array of registers here */
133802 u8 ecelFlags; /* Flag argument to ExprCodeExprList() */
133803#ifdef SQLITE_ENABLE_SORTER_REFERENCES
133804 ExprList *pExtra; /* Extra columns needed by sorter refs */
133805 int regExtraResult; /* Where to load the extra columns */
133806#endif
133807};
133808
133809/*
133810** This routine does the work of loading query data into an array of
133811** registers so that it can be added to the sorter.
133812*/
133813static void innerLoopLoadRow(
133814 Parse *pParse, /* Statement under construction */
133815 Select *pSelect, /* The query being coded */
133816 RowLoadInfo *pInfo /* Info needed to complete the row load */
133817){
133818 sqlite3ExprCodeExprList(pParse, pList: pSelect->pEList, target: pInfo->regResult,
133819 srcReg: 0, flags: pInfo->ecelFlags);
133820#ifdef SQLITE_ENABLE_SORTER_REFERENCES
133821 if( pInfo->pExtra ){
133822 sqlite3ExprCodeExprList(pParse, pInfo->pExtra, pInfo->regExtraResult, 0, 0);
133823 sqlite3ExprListDelete(pParse->db, pInfo->pExtra);
133824 }
133825#endif
133826}
133827
133828/*
133829** Code the OP_MakeRecord instruction that generates the entry to be
133830** added into the sorter.
133831**
133832** Return the register in which the result is stored.
133833*/
133834static int makeSorterRecord(
133835 Parse *pParse,
133836 SortCtx *pSort,
133837 Select *pSelect,
133838 int regBase,
133839 int nBase
133840){
133841 int nOBSat = pSort->nOBSat;
133842 Vdbe *v = pParse->pVdbe;
133843 int regOut = ++pParse->nMem;
133844 if( pSort->pDeferredRowLoad ){
133845 innerLoopLoadRow(pParse, pSelect, pInfo: pSort->pDeferredRowLoad);
133846 }
133847 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: regBase+nOBSat, p2: nBase-nOBSat, p3: regOut);
133848 return regOut;
133849}
133850
133851/*
133852** Generate code that will push the record in registers regData
133853** through regData+nData-1 onto the sorter.
133854*/
133855static void pushOntoSorter(
133856 Parse *pParse, /* Parser context */
133857 SortCtx *pSort, /* Information about the ORDER BY clause */
133858 Select *pSelect, /* The whole SELECT statement */
133859 int regData, /* First register holding data to be sorted */
133860 int regOrigData, /* First register holding data before packing */
133861 int nData, /* Number of elements in the regData data array */
133862 int nPrefixReg /* No. of reg prior to regData available for use */
133863){
133864 Vdbe *v = pParse->pVdbe; /* Stmt under construction */
133865 int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0);
133866 int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */
133867 int nBase = nExpr + bSeq + nData; /* Fields in sorter record */
133868 int regBase; /* Regs for sorter record */
133869 int regRecord = 0; /* Assembled sorter record */
133870 int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */
133871 int op; /* Opcode to add sorter record to sorter */
133872 int iLimit; /* LIMIT counter */
133873 int iSkip = 0; /* End of the sorter insert loop */
133874
133875 assert( bSeq==0 || bSeq==1 );
133876
133877 /* Three cases:
133878 ** (1) The data to be sorted has already been packed into a Record
133879 ** by a prior OP_MakeRecord. In this case nData==1 and regData
133880 ** will be completely unrelated to regOrigData.
133881 ** (2) All output columns are included in the sort record. In that
133882 ** case regData==regOrigData.
133883 ** (3) Some output columns are omitted from the sort record due to
133884 ** the SQLITE_ENABLE_SORTER_REFERENCE optimization, or due to the
133885 ** SQLITE_ECEL_OMITREF optimization, or due to the
133886 ** SortCtx.pDeferredRowLoad optimiation. In any of these cases
133887 ** regOrigData is 0 to prevent this routine from trying to copy
133888 ** values that might not yet exist.
133889 */
133890 assert( nData==1 || regData==regOrigData || regOrigData==0 );
133891
133892 if( nPrefixReg ){
133893 assert( nPrefixReg==nExpr+bSeq );
133894 regBase = regData - nPrefixReg;
133895 }else{
133896 regBase = pParse->nMem + 1;
133897 pParse->nMem += nBase;
133898 }
133899 assert( pSelect->iOffset==0 || pSelect->iLimit!=0 );
133900 iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit;
133901 pSort->labelDone = sqlite3VdbeMakeLabel(pParse);
133902 sqlite3ExprCodeExprList(pParse, pList: pSort->pOrderBy, target: regBase, srcReg: regOrigData,
133903 SQLITE_ECEL_DUP | (regOrigData? SQLITE_ECEL_REF : 0));
133904 if( bSeq ){
133905 sqlite3VdbeAddOp2(p: v, OP_Sequence, p1: pSort->iECursor, p2: regBase+nExpr);
133906 }
133907 if( nPrefixReg==0 && nData>0 ){
133908 sqlite3ExprCodeMove(pParse, iFrom: regData, iTo: regBase+nExpr+bSeq, nReg: nData);
133909 }
133910 if( nOBSat>0 ){
133911 int regPrevKey; /* The first nOBSat columns of the previous row */
133912 int addrFirst; /* Address of the OP_IfNot opcode */
133913 int addrJmp; /* Address of the OP_Jump opcode */
133914 VdbeOp *pOp; /* Opcode that opens the sorter */
133915 int nKey; /* Number of sorting key columns, including OP_Sequence */
133916 KeyInfo *pKI; /* Original KeyInfo on the sorter table */
133917
133918 regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
133919 regPrevKey = pParse->nMem+1;
133920 pParse->nMem += pSort->nOBSat;
133921 nKey = nExpr - pSort->nOBSat + bSeq;
133922 if( bSeq ){
133923 addrFirst = sqlite3VdbeAddOp1(p: v, OP_IfNot, p1: regBase+nExpr);
133924 }else{
133925 addrFirst = sqlite3VdbeAddOp1(p: v, OP_SequenceTest, p1: pSort->iECursor);
133926 }
133927 VdbeCoverage(v);
133928 sqlite3VdbeAddOp3(p: v, OP_Compare, p1: regPrevKey, p2: regBase, p3: pSort->nOBSat);
133929 pOp = sqlite3VdbeGetOp(p: v, addr: pSort->addrSortIndex);
133930 if( pParse->db->mallocFailed ) return;
133931 pOp->p2 = nKey + nData;
133932 pKI = pOp->p4.pKeyInfo;
133933 memset(s: pKI->aSortFlags, c: 0, n: pKI->nKeyField); /* Makes OP_Jump testable */
133934 sqlite3VdbeChangeP4(p: v, addr: -1, zP4: (char*)pKI, P4_KEYINFO);
133935 testcase( pKI->nAllField > pKI->nKeyField+2 );
133936 pOp->p4.pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pSort->pOrderBy,nOBSat,
133937 pKI->nAllField-pKI->nKeyField-1);
133938 pOp = 0; /* Ensure pOp not used after sqltie3VdbeAddOp3() */
133939 addrJmp = sqlite3VdbeCurrentAddr(p: v);
133940 sqlite3VdbeAddOp3(p: v, OP_Jump, p1: addrJmp+1, p2: 0, p3: addrJmp+1); VdbeCoverage(v);
133941 pSort->labelBkOut = sqlite3VdbeMakeLabel(pParse);
133942 pSort->regReturn = ++pParse->nMem;
133943 sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: pSort->regReturn, p2: pSort->labelBkOut);
133944 sqlite3VdbeAddOp1(p: v, OP_ResetSorter, p1: pSort->iECursor);
133945 if( iLimit ){
133946 sqlite3VdbeAddOp2(p: v, OP_IfNot, p1: iLimit, p2: pSort->labelDone);
133947 VdbeCoverage(v);
133948 }
133949 sqlite3VdbeJumpHere(p: v, addr: addrFirst);
133950 sqlite3ExprCodeMove(pParse, iFrom: regBase, iTo: regPrevKey, nReg: pSort->nOBSat);
133951 sqlite3VdbeJumpHere(p: v, addr: addrJmp);
133952 }
133953 if( iLimit ){
133954 /* At this point the values for the new sorter entry are stored
133955 ** in an array of registers. They need to be composed into a record
133956 ** and inserted into the sorter if either (a) there are currently
133957 ** less than LIMIT+OFFSET items or (b) the new record is smaller than
133958 ** the largest record currently in the sorter. If (b) is true and there
133959 ** are already LIMIT+OFFSET items in the sorter, delete the largest
133960 ** entry before inserting the new one. This way there are never more
133961 ** than LIMIT+OFFSET items in the sorter.
133962 **
133963 ** If the new record does not need to be inserted into the sorter,
133964 ** jump to the next iteration of the loop. If the pSort->labelOBLopt
133965 ** value is not zero, then it is a label of where to jump. Otherwise,
133966 ** just bypass the row insert logic. See the header comment on the
133967 ** sqlite3WhereOrderByLimitOptLabel() function for additional info.
133968 */
133969 int iCsr = pSort->iECursor;
133970 sqlite3VdbeAddOp2(p: v, OP_IfNotZero, p1: iLimit, p2: sqlite3VdbeCurrentAddr(p: v)+4);
133971 VdbeCoverage(v);
133972 sqlite3VdbeAddOp2(p: v, OP_Last, p1: iCsr, p2: 0);
133973 iSkip = sqlite3VdbeAddOp4Int(p: v, OP_IdxLE,
133974 p1: iCsr, p2: 0, p3: regBase+nOBSat, p4: nExpr-nOBSat);
133975 VdbeCoverage(v);
133976 sqlite3VdbeAddOp1(p: v, OP_Delete, p1: iCsr);
133977 }
133978 if( regRecord==0 ){
133979 regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
133980 }
133981 if( pSort->sortFlags & SORTFLAG_UseSorter ){
133982 op = OP_SorterInsert;
133983 }else{
133984 op = OP_IdxInsert;
133985 }
133986 sqlite3VdbeAddOp4Int(p: v, op, p1: pSort->iECursor, p2: regRecord,
133987 p3: regBase+nOBSat, p4: nBase-nOBSat);
133988 if( iSkip ){
133989 sqlite3VdbeChangeP2(p: v, addr: iSkip,
133990 val: pSort->labelOBLopt ? pSort->labelOBLopt : sqlite3VdbeCurrentAddr(p: v));
133991 }
133992}
133993
133994/*
133995** Add code to implement the OFFSET
133996*/
133997static void codeOffset(
133998 Vdbe *v, /* Generate code into this VM */
133999 int iOffset, /* Register holding the offset counter */
134000 int iContinue /* Jump here to skip the current record */
134001){
134002 if( iOffset>0 ){
134003 sqlite3VdbeAddOp3(p: v, OP_IfPos, p1: iOffset, p2: iContinue, p3: 1); VdbeCoverage(v);
134004 VdbeComment((v, "OFFSET"));
134005 }
134006}
134007
134008/*
134009** Add code that will check to make sure the array of registers starting at
134010** iMem form a distinct entry. This is used by both "SELECT DISTINCT ..." and
134011** distinct aggregates ("SELECT count(DISTINCT <expr>) ..."). Three strategies
134012** are available. Which is used depends on the value of parameter eTnctType,
134013** as follows:
134014**
134015** WHERE_DISTINCT_UNORDERED/WHERE_DISTINCT_NOOP:
134016** Build an ephemeral table that contains all entries seen before and
134017** skip entries which have been seen before.
134018**
134019** Parameter iTab is the cursor number of an ephemeral table that must
134020** be opened before the VM code generated by this routine is executed.
134021** The ephemeral cursor table is queried for a record identical to the
134022** record formed by the current array of registers. If one is found,
134023** jump to VM address addrRepeat. Otherwise, insert a new record into
134024** the ephemeral cursor and proceed.
134025**
134026** The returned value in this case is a copy of parameter iTab.
134027**
134028** WHERE_DISTINCT_ORDERED:
134029** In this case rows are being delivered sorted order. The ephermal
134030** table is not required. Instead, the current set of values
134031** is compared against previous row. If they match, the new row
134032** is not distinct and control jumps to VM address addrRepeat. Otherwise,
134033** the VM program proceeds with processing the new row.
134034**
134035** The returned value in this case is the register number of the first
134036** in an array of registers used to store the previous result row so that
134037** it can be compared to the next. The caller must ensure that this
134038** register is initialized to NULL. (The fixDistinctOpenEph() routine
134039** will take care of this initialization.)
134040**
134041** WHERE_DISTINCT_UNIQUE:
134042** In this case it has already been determined that the rows are distinct.
134043** No special action is required. The return value is zero.
134044**
134045** Parameter pEList is the list of expressions used to generated the
134046** contents of each row. It is used by this routine to determine (a)
134047** how many elements there are in the array of registers and (b) the
134048** collation sequences that should be used for the comparisons if
134049** eTnctType is WHERE_DISTINCT_ORDERED.
134050*/
134051static int codeDistinct(
134052 Parse *pParse, /* Parsing and code generating context */
134053 int eTnctType, /* WHERE_DISTINCT_* value */
134054 int iTab, /* A sorting index used to test for distinctness */
134055 int addrRepeat, /* Jump to here if not distinct */
134056 ExprList *pEList, /* Expression for each element */
134057 int regElem /* First element */
134058){
134059 int iRet = 0;
134060 int nResultCol = pEList->nExpr;
134061 Vdbe *v = pParse->pVdbe;
134062
134063 switch( eTnctType ){
134064 case WHERE_DISTINCT_ORDERED: {
134065 int i;
134066 int iJump; /* Jump destination */
134067 int regPrev; /* Previous row content */
134068
134069 /* Allocate space for the previous row */
134070 iRet = regPrev = pParse->nMem+1;
134071 pParse->nMem += nResultCol;
134072
134073 iJump = sqlite3VdbeCurrentAddr(p: v) + nResultCol;
134074 for(i=0; i<nResultCol; i++){
134075 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr: pEList->a[i].pExpr);
134076 if( i<nResultCol-1 ){
134077 sqlite3VdbeAddOp3(p: v, OP_Ne, p1: regElem+i, p2: iJump, p3: regPrev+i);
134078 VdbeCoverage(v);
134079 }else{
134080 sqlite3VdbeAddOp3(p: v, OP_Eq, p1: regElem+i, p2: addrRepeat, p3: regPrev+i);
134081 VdbeCoverage(v);
134082 }
134083 sqlite3VdbeChangeP4(p: v, addr: -1, zP4: (const char *)pColl, P4_COLLSEQ);
134084 sqlite3VdbeChangeP5(p: v, SQLITE_NULLEQ);
134085 }
134086 assert( sqlite3VdbeCurrentAddr(v)==iJump || pParse->db->mallocFailed );
134087 sqlite3VdbeAddOp3(p: v, OP_Copy, p1: regElem, p2: regPrev, p3: nResultCol-1);
134088 break;
134089 }
134090
134091 case WHERE_DISTINCT_UNIQUE: {
134092 /* nothing to do */
134093 break;
134094 }
134095
134096 default: {
134097 int r1 = sqlite3GetTempReg(pParse);
134098 sqlite3VdbeAddOp4Int(p: v, OP_Found, p1: iTab, p2: addrRepeat, p3: regElem, p4: nResultCol);
134099 VdbeCoverage(v);
134100 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: regElem, p2: nResultCol, p3: r1);
134101 sqlite3VdbeAddOp4Int(p: v, OP_IdxInsert, p1: iTab, p2: r1, p3: regElem, p4: nResultCol);
134102 sqlite3VdbeChangeP5(p: v, OPFLAG_USESEEKRESULT);
134103 sqlite3ReleaseTempReg(pParse, iReg: r1);
134104 iRet = iTab;
134105 break;
134106 }
134107 }
134108
134109 return iRet;
134110}
134111
134112/*
134113** This routine runs after codeDistinct(). It makes necessary
134114** adjustments to the OP_OpenEphemeral opcode that the codeDistinct()
134115** routine made use of. This processing must be done separately since
134116** sometimes codeDistinct is called before the OP_OpenEphemeral is actually
134117** laid down.
134118**
134119** WHERE_DISTINCT_NOOP:
134120** WHERE_DISTINCT_UNORDERED:
134121**
134122** No adjustments necessary. This function is a no-op.
134123**
134124** WHERE_DISTINCT_UNIQUE:
134125**
134126** The ephemeral table is not needed. So change the
134127** OP_OpenEphemeral opcode into an OP_Noop.
134128**
134129** WHERE_DISTINCT_ORDERED:
134130**
134131** The ephemeral table is not needed. But we do need register
134132** iVal to be initialized to NULL. So change the OP_OpenEphemeral
134133** into an OP_Null on the iVal register.
134134*/
134135static void fixDistinctOpenEph(
134136 Parse *pParse, /* Parsing and code generating context */
134137 int eTnctType, /* WHERE_DISTINCT_* value */
134138 int iVal, /* Value returned by codeDistinct() */
134139 int iOpenEphAddr /* Address of OP_OpenEphemeral instruction for iTab */
134140){
134141 if( pParse->nErr==0
134142 && (eTnctType==WHERE_DISTINCT_UNIQUE || eTnctType==WHERE_DISTINCT_ORDERED)
134143 ){
134144 Vdbe *v = pParse->pVdbe;
134145 sqlite3VdbeChangeToNoop(p: v, addr: iOpenEphAddr);
134146 if( sqlite3VdbeGetOp(p: v, addr: iOpenEphAddr+1)->opcode==OP_Explain ){
134147 sqlite3VdbeChangeToNoop(p: v, addr: iOpenEphAddr+1);
134148 }
134149 if( eTnctType==WHERE_DISTINCT_ORDERED ){
134150 /* Change the OP_OpenEphemeral to an OP_Null that sets the MEM_Cleared
134151 ** bit on the first register of the previous value. This will cause the
134152 ** OP_Ne added in codeDistinct() to always fail on the first iteration of
134153 ** the loop even if the first row is all NULLs. */
134154 VdbeOp *pOp = sqlite3VdbeGetOp(p: v, addr: iOpenEphAddr);
134155 pOp->opcode = OP_Null;
134156 pOp->p1 = 1;
134157 pOp->p2 = iVal;
134158 }
134159 }
134160}
134161
134162#ifdef SQLITE_ENABLE_SORTER_REFERENCES
134163/*
134164** This function is called as part of inner-loop generation for a SELECT
134165** statement with an ORDER BY that is not optimized by an index. It
134166** determines the expressions, if any, that the sorter-reference
134167** optimization should be used for. The sorter-reference optimization
134168** is used for SELECT queries like:
134169**
134170** SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10
134171**
134172** If the optimization is used for expression "bigblob", then instead of
134173** storing values read from that column in the sorter records, the PK of
134174** the row from table t1 is stored instead. Then, as records are extracted from
134175** the sorter to return to the user, the required value of bigblob is
134176** retrieved directly from table t1. If the values are very large, this
134177** can be more efficient than storing them directly in the sorter records.
134178**
134179** The ExprList_item.bSorterRef flag is set for each expression in pEList
134180** for which the sorter-reference optimization should be enabled.
134181** Additionally, the pSort->aDefer[] array is populated with entries
134182** for all cursors required to evaluate all selected expressions. Finally.
134183** output variable (*ppExtra) is set to an expression list containing
134184** expressions for all extra PK values that should be stored in the
134185** sorter records.
134186*/
134187static void selectExprDefer(
134188 Parse *pParse, /* Leave any error here */
134189 SortCtx *pSort, /* Sorter context */
134190 ExprList *pEList, /* Expressions destined for sorter */
134191 ExprList **ppExtra /* Expressions to append to sorter record */
134192){
134193 int i;
134194 int nDefer = 0;
134195 ExprList *pExtra = 0;
134196 for(i=0; i<pEList->nExpr; i++){
134197 struct ExprList_item *pItem = &pEList->a[i];
134198 if( pItem->u.x.iOrderByCol==0 ){
134199 Expr *pExpr = pItem->pExpr;
134200 Table *pTab;
134201 if( pExpr->op==TK_COLUMN
134202 && pExpr->iColumn>=0
134203 && ALWAYS( ExprUseYTab(pExpr) )
134204 && (pTab = pExpr->y.pTab)!=0
134205 && IsOrdinaryTable(pTab)
134206 && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF)!=0
134207 ){
134208 int j;
134209 for(j=0; j<nDefer; j++){
134210 if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
134211 }
134212 if( j==nDefer ){
134213 if( nDefer==ArraySize(pSort->aDefer) ){
134214 continue;
134215 }else{
134216 int nKey = 1;
134217 int k;
134218 Index *pPk = 0;
134219 if( !HasRowid(pTab) ){
134220 pPk = sqlite3PrimaryKeyIndex(pTab);
134221 nKey = pPk->nKeyCol;
134222 }
134223 for(k=0; k<nKey; k++){
134224 Expr *pNew = sqlite3PExpr(pParse, TK_COLUMN, 0, 0);
134225 if( pNew ){
134226 pNew->iTable = pExpr->iTable;
134227 assert( ExprUseYTab(pNew) );
134228 pNew->y.pTab = pExpr->y.pTab;
134229 pNew->iColumn = pPk ? pPk->aiColumn[k] : -1;
134230 pExtra = sqlite3ExprListAppend(pParse, pExtra, pNew);
134231 }
134232 }
134233 pSort->aDefer[nDefer].pTab = pExpr->y.pTab;
134234 pSort->aDefer[nDefer].iCsr = pExpr->iTable;
134235 pSort->aDefer[nDefer].nKey = nKey;
134236 nDefer++;
134237 }
134238 }
134239 pItem->bSorterRef = 1;
134240 }
134241 }
134242 }
134243 pSort->nDefer = (u8)nDefer;
134244 *ppExtra = pExtra;
134245}
134246#endif
134247
134248/*
134249** This routine generates the code for the inside of the inner loop
134250** of a SELECT.
134251**
134252** If srcTab is negative, then the p->pEList expressions
134253** are evaluated in order to get the data for this row. If srcTab is
134254** zero or more, then data is pulled from srcTab and p->pEList is used only
134255** to get the number of columns and the collation sequence for each column.
134256*/
134257static void selectInnerLoop(
134258 Parse *pParse, /* The parser context */
134259 Select *p, /* The complete select statement being coded */
134260 int srcTab, /* Pull data from this table if non-negative */
134261 SortCtx *pSort, /* If not NULL, info on how to process ORDER BY */
134262 DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */
134263 SelectDest *pDest, /* How to dispose of the results */
134264 int iContinue, /* Jump here to continue with next row */
134265 int iBreak /* Jump here to break out of the inner loop */
134266){
134267 Vdbe *v = pParse->pVdbe;
134268 int i;
134269 int hasDistinct; /* True if the DISTINCT keyword is present */
134270 int eDest = pDest->eDest; /* How to dispose of results */
134271 int iParm = pDest->iSDParm; /* First argument to disposal method */
134272 int nResultCol; /* Number of result columns */
134273 int nPrefixReg = 0; /* Number of extra registers before regResult */
134274 RowLoadInfo sRowLoadInfo; /* Info for deferred row loading */
134275
134276 /* Usually, regResult is the first cell in an array of memory cells
134277 ** containing the current result row. In this case regOrig is set to the
134278 ** same value. However, if the results are being sent to the sorter, the
134279 ** values for any expressions that are also part of the sort-key are omitted
134280 ** from this array. In this case regOrig is set to zero. */
134281 int regResult; /* Start of memory holding current results */
134282 int regOrig; /* Start of memory holding full result (or 0) */
134283
134284 assert( v );
134285 assert( p->pEList!=0 );
134286 hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
134287 if( pSort && pSort->pOrderBy==0 ) pSort = 0;
134288 if( pSort==0 && !hasDistinct ){
134289 assert( iContinue!=0 );
134290 codeOffset(v, iOffset: p->iOffset, iContinue);
134291 }
134292
134293 /* Pull the requested columns.
134294 */
134295 nResultCol = p->pEList->nExpr;
134296
134297 if( pDest->iSdst==0 ){
134298 if( pSort ){
134299 nPrefixReg = pSort->pOrderBy->nExpr;
134300 if( !(pSort->sortFlags & SORTFLAG_UseSorter) ) nPrefixReg++;
134301 pParse->nMem += nPrefixReg;
134302 }
134303 pDest->iSdst = pParse->nMem+1;
134304 pParse->nMem += nResultCol;
134305 }else if( pDest->iSdst+nResultCol > pParse->nMem ){
134306 /* This is an error condition that can result, for example, when a SELECT
134307 ** on the right-hand side of an INSERT contains more result columns than
134308 ** there are columns in the table on the left. The error will be caught
134309 ** and reported later. But we need to make sure enough memory is allocated
134310 ** to avoid other spurious errors in the meantime. */
134311 pParse->nMem += nResultCol;
134312 }
134313 pDest->nSdst = nResultCol;
134314 regOrig = regResult = pDest->iSdst;
134315 if( srcTab>=0 ){
134316 for(i=0; i<nResultCol; i++){
134317 sqlite3VdbeAddOp3(p: v, OP_Column, p1: srcTab, p2: i, p3: regResult+i);
134318 VdbeComment((v, "%s", p->pEList->a[i].zEName));
134319 }
134320 }else if( eDest!=SRT_Exists ){
134321#ifdef SQLITE_ENABLE_SORTER_REFERENCES
134322 ExprList *pExtra = 0;
134323#endif
134324 /* If the destination is an EXISTS(...) expression, the actual
134325 ** values returned by the SELECT are not required.
134326 */
134327 u8 ecelFlags; /* "ecel" is an abbreviation of "ExprCodeExprList" */
134328 ExprList *pEList;
134329 if( eDest==SRT_Mem || eDest==SRT_Output || eDest==SRT_Coroutine ){
134330 ecelFlags = SQLITE_ECEL_DUP;
134331 }else{
134332 ecelFlags = 0;
134333 }
134334 if( pSort && hasDistinct==0 && eDest!=SRT_EphemTab && eDest!=SRT_Table ){
134335 /* For each expression in p->pEList that is a copy of an expression in
134336 ** the ORDER BY clause (pSort->pOrderBy), set the associated
134337 ** iOrderByCol value to one more than the index of the ORDER BY
134338 ** expression within the sort-key that pushOntoSorter() will generate.
134339 ** This allows the p->pEList field to be omitted from the sorted record,
134340 ** saving space and CPU cycles. */
134341 ecelFlags |= (SQLITE_ECEL_OMITREF|SQLITE_ECEL_REF);
134342
134343 for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
134344 int j;
134345 if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
134346 p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
134347 }
134348 }
134349#ifdef SQLITE_ENABLE_SORTER_REFERENCES
134350 selectExprDefer(pParse, pSort, p->pEList, &pExtra);
134351 if( pExtra && pParse->db->mallocFailed==0 ){
134352 /* If there are any extra PK columns to add to the sorter records,
134353 ** allocate extra memory cells and adjust the OpenEphemeral
134354 ** instruction to account for the larger records. This is only
134355 ** required if there are one or more WITHOUT ROWID tables with
134356 ** composite primary keys in the SortCtx.aDefer[] array. */
134357 VdbeOp *pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
134358 pOp->p2 += (pExtra->nExpr - pSort->nDefer);
134359 pOp->p4.pKeyInfo->nAllField += (pExtra->nExpr - pSort->nDefer);
134360 pParse->nMem += pExtra->nExpr;
134361 }
134362#endif
134363
134364 /* Adjust nResultCol to account for columns that are omitted
134365 ** from the sorter by the optimizations in this branch */
134366 pEList = p->pEList;
134367 for(i=0; i<pEList->nExpr; i++){
134368 if( pEList->a[i].u.x.iOrderByCol>0
134369#ifdef SQLITE_ENABLE_SORTER_REFERENCES
134370 || pEList->a[i].bSorterRef
134371#endif
134372 ){
134373 nResultCol--;
134374 regOrig = 0;
134375 }
134376 }
134377
134378 testcase( regOrig );
134379 testcase( eDest==SRT_Set );
134380 testcase( eDest==SRT_Mem );
134381 testcase( eDest==SRT_Coroutine );
134382 testcase( eDest==SRT_Output );
134383 assert( eDest==SRT_Set || eDest==SRT_Mem
134384 || eDest==SRT_Coroutine || eDest==SRT_Output
134385 || eDest==SRT_Upfrom );
134386 }
134387 sRowLoadInfo.regResult = regResult;
134388 sRowLoadInfo.ecelFlags = ecelFlags;
134389#ifdef SQLITE_ENABLE_SORTER_REFERENCES
134390 sRowLoadInfo.pExtra = pExtra;
134391 sRowLoadInfo.regExtraResult = regResult + nResultCol;
134392 if( pExtra ) nResultCol += pExtra->nExpr;
134393#endif
134394 if( p->iLimit
134395 && (ecelFlags & SQLITE_ECEL_OMITREF)!=0
134396 && nPrefixReg>0
134397 ){
134398 assert( pSort!=0 );
134399 assert( hasDistinct==0 );
134400 pSort->pDeferredRowLoad = &sRowLoadInfo;
134401 regOrig = 0;
134402 }else{
134403 innerLoopLoadRow(pParse, pSelect: p, pInfo: &sRowLoadInfo);
134404 }
134405 }
134406
134407 /* If the DISTINCT keyword was present on the SELECT statement
134408 ** and this row has been seen before, then do not make this row
134409 ** part of the result.
134410 */
134411 if( hasDistinct ){
134412 int eType = pDistinct->eTnctType;
134413 int iTab = pDistinct->tabTnct;
134414 assert( nResultCol==p->pEList->nExpr );
134415 iTab = codeDistinct(pParse, eTnctType: eType, iTab, addrRepeat: iContinue, pEList: p->pEList, regElem: regResult);
134416 fixDistinctOpenEph(pParse, eTnctType: eType, iVal: iTab, iOpenEphAddr: pDistinct->addrTnct);
134417 if( pSort==0 ){
134418 codeOffset(v, iOffset: p->iOffset, iContinue);
134419 }
134420 }
134421
134422 switch( eDest ){
134423 /* In this mode, write each query result to the key of the temporary
134424 ** table iParm.
134425 */
134426#ifndef SQLITE_OMIT_COMPOUND_SELECT
134427 case SRT_Union: {
134428 int r1;
134429 r1 = sqlite3GetTempReg(pParse);
134430 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: regResult, p2: nResultCol, p3: r1);
134431 sqlite3VdbeAddOp4Int(p: v, OP_IdxInsert, p1: iParm, p2: r1, p3: regResult, p4: nResultCol);
134432 sqlite3ReleaseTempReg(pParse, iReg: r1);
134433 break;
134434 }
134435
134436 /* Construct a record from the query result, but instead of
134437 ** saving that record, use it as a key to delete elements from
134438 ** the temporary table iParm.
134439 */
134440 case SRT_Except: {
134441 sqlite3VdbeAddOp3(p: v, OP_IdxDelete, p1: iParm, p2: regResult, p3: nResultCol);
134442 break;
134443 }
134444#endif /* SQLITE_OMIT_COMPOUND_SELECT */
134445
134446 /* Store the result as data using a unique key.
134447 */
134448 case SRT_Fifo:
134449 case SRT_DistFifo:
134450 case SRT_Table:
134451 case SRT_EphemTab: {
134452 int r1 = sqlite3GetTempRange(pParse, nReg: nPrefixReg+1);
134453 testcase( eDest==SRT_Table );
134454 testcase( eDest==SRT_EphemTab );
134455 testcase( eDest==SRT_Fifo );
134456 testcase( eDest==SRT_DistFifo );
134457 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: regResult, p2: nResultCol, p3: r1+nPrefixReg);
134458#ifndef SQLITE_OMIT_CTE
134459 if( eDest==SRT_DistFifo ){
134460 /* If the destination is DistFifo, then cursor (iParm+1) is open
134461 ** on an ephemeral index. If the current row is already present
134462 ** in the index, do not write it to the output. If not, add the
134463 ** current row to the index and proceed with writing it to the
134464 ** output table as well. */
134465 int addr = sqlite3VdbeCurrentAddr(p: v) + 4;
134466 sqlite3VdbeAddOp4Int(p: v, OP_Found, p1: iParm+1, p2: addr, p3: r1, p4: 0);
134467 VdbeCoverage(v);
134468 sqlite3VdbeAddOp4Int(p: v, OP_IdxInsert, p1: iParm+1, p2: r1,p3: regResult,p4: nResultCol);
134469 assert( pSort==0 );
134470 }
134471#endif
134472 if( pSort ){
134473 assert( regResult==regOrig );
134474 pushOntoSorter(pParse, pSort, pSelect: p, regData: r1+nPrefixReg, regOrigData: regOrig, nData: 1, nPrefixReg);
134475 }else{
134476 int r2 = sqlite3GetTempReg(pParse);
134477 sqlite3VdbeAddOp2(p: v, OP_NewRowid, p1: iParm, p2: r2);
134478 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: iParm, p2: r1, p3: r2);
134479 sqlite3VdbeChangeP5(p: v, OPFLAG_APPEND);
134480 sqlite3ReleaseTempReg(pParse, iReg: r2);
134481 }
134482 sqlite3ReleaseTempRange(pParse, iReg: r1, nReg: nPrefixReg+1);
134483 break;
134484 }
134485
134486 case SRT_Upfrom: {
134487 if( pSort ){
134488 pushOntoSorter(
134489 pParse, pSort, pSelect: p, regData: regResult, regOrigData: regOrig, nData: nResultCol, nPrefixReg);
134490 }else{
134491 int i2 = pDest->iSDParm2;
134492 int r1 = sqlite3GetTempReg(pParse);
134493
134494 /* If the UPDATE FROM join is an aggregate that matches no rows, it
134495 ** might still be trying to return one row, because that is what
134496 ** aggregates do. Don't record that empty row in the output table. */
134497 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: regResult, p2: iBreak); VdbeCoverage(v);
134498
134499 sqlite3VdbeAddOp3(p: v, OP_MakeRecord,
134500 p1: regResult+(i2<0), p2: nResultCol-(i2<0), p3: r1);
134501 if( i2<0 ){
134502 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: iParm, p2: r1, p3: regResult);
134503 }else{
134504 sqlite3VdbeAddOp4Int(p: v, OP_IdxInsert, p1: iParm, p2: r1, p3: regResult, p4: i2);
134505 }
134506 }
134507 break;
134508 }
134509
134510#ifndef SQLITE_OMIT_SUBQUERY
134511 /* If we are creating a set for an "expr IN (SELECT ...)" construct,
134512 ** then there should be a single item on the stack. Write this
134513 ** item into the set table with bogus data.
134514 */
134515 case SRT_Set: {
134516 if( pSort ){
134517 /* At first glance you would think we could optimize out the
134518 ** ORDER BY in this case since the order of entries in the set
134519 ** does not matter. But there might be a LIMIT clause, in which
134520 ** case the order does matter */
134521 pushOntoSorter(
134522 pParse, pSort, pSelect: p, regData: regResult, regOrigData: regOrig, nData: nResultCol, nPrefixReg);
134523 }else{
134524 int r1 = sqlite3GetTempReg(pParse);
134525 assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol );
134526 sqlite3VdbeAddOp4(p: v, OP_MakeRecord, p1: regResult, p2: nResultCol,
134527 p3: r1, zP4: pDest->zAffSdst, p4type: nResultCol);
134528 sqlite3VdbeAddOp4Int(p: v, OP_IdxInsert, p1: iParm, p2: r1, p3: regResult, p4: nResultCol);
134529 sqlite3ReleaseTempReg(pParse, iReg: r1);
134530 }
134531 break;
134532 }
134533
134534
134535 /* If any row exist in the result set, record that fact and abort.
134536 */
134537 case SRT_Exists: {
134538 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 1, p2: iParm);
134539 /* The LIMIT clause will terminate the loop for us */
134540 break;
134541 }
134542
134543 /* If this is a scalar select that is part of an expression, then
134544 ** store the results in the appropriate memory cell or array of
134545 ** memory cells and break out of the scan loop.
134546 */
134547 case SRT_Mem: {
134548 if( pSort ){
134549 assert( nResultCol<=pDest->nSdst );
134550 pushOntoSorter(
134551 pParse, pSort, pSelect: p, regData: regResult, regOrigData: regOrig, nData: nResultCol, nPrefixReg);
134552 }else{
134553 assert( nResultCol==pDest->nSdst );
134554 assert( regResult==iParm );
134555 /* The LIMIT clause will jump out of the loop for us */
134556 }
134557 break;
134558 }
134559#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
134560
134561 case SRT_Coroutine: /* Send data to a co-routine */
134562 case SRT_Output: { /* Return the results */
134563 testcase( eDest==SRT_Coroutine );
134564 testcase( eDest==SRT_Output );
134565 if( pSort ){
134566 pushOntoSorter(pParse, pSort, pSelect: p, regData: regResult, regOrigData: regOrig, nData: nResultCol,
134567 nPrefixReg);
134568 }else if( eDest==SRT_Coroutine ){
134569 sqlite3VdbeAddOp1(p: v, OP_Yield, p1: pDest->iSDParm);
134570 }else{
134571 sqlite3VdbeAddOp2(p: v, OP_ResultRow, p1: regResult, p2: nResultCol);
134572 }
134573 break;
134574 }
134575
134576#ifndef SQLITE_OMIT_CTE
134577 /* Write the results into a priority queue that is order according to
134578 ** pDest->pOrderBy (in pSO). pDest->iSDParm (in iParm) is the cursor for an
134579 ** index with pSO->nExpr+2 columns. Build a key using pSO for the first
134580 ** pSO->nExpr columns, then make sure all keys are unique by adding a
134581 ** final OP_Sequence column. The last column is the record as a blob.
134582 */
134583 case SRT_DistQueue:
134584 case SRT_Queue: {
134585 int nKey;
134586 int r1, r2, r3;
134587 int addrTest = 0;
134588 ExprList *pSO;
134589 pSO = pDest->pOrderBy;
134590 assert( pSO );
134591 nKey = pSO->nExpr;
134592 r1 = sqlite3GetTempReg(pParse);
134593 r2 = sqlite3GetTempRange(pParse, nReg: nKey+2);
134594 r3 = r2+nKey+1;
134595 if( eDest==SRT_DistQueue ){
134596 /* If the destination is DistQueue, then cursor (iParm+1) is open
134597 ** on a second ephemeral index that holds all values every previously
134598 ** added to the queue. */
134599 addrTest = sqlite3VdbeAddOp4Int(p: v, OP_Found, p1: iParm+1, p2: 0,
134600 p3: regResult, p4: nResultCol);
134601 VdbeCoverage(v);
134602 }
134603 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: regResult, p2: nResultCol, p3: r3);
134604 if( eDest==SRT_DistQueue ){
134605 sqlite3VdbeAddOp2(p: v, OP_IdxInsert, p1: iParm+1, p2: r3);
134606 sqlite3VdbeChangeP5(p: v, OPFLAG_USESEEKRESULT);
134607 }
134608 for(i=0; i<nKey; i++){
134609 sqlite3VdbeAddOp2(p: v, OP_SCopy,
134610 p1: regResult + pSO->a[i].u.x.iOrderByCol - 1,
134611 p2: r2+i);
134612 }
134613 sqlite3VdbeAddOp2(p: v, OP_Sequence, p1: iParm, p2: r2+nKey);
134614 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: r2, p2: nKey+2, p3: r1);
134615 sqlite3VdbeAddOp4Int(p: v, OP_IdxInsert, p1: iParm, p2: r1, p3: r2, p4: nKey+2);
134616 if( addrTest ) sqlite3VdbeJumpHere(p: v, addr: addrTest);
134617 sqlite3ReleaseTempReg(pParse, iReg: r1);
134618 sqlite3ReleaseTempRange(pParse, iReg: r2, nReg: nKey+2);
134619 break;
134620 }
134621#endif /* SQLITE_OMIT_CTE */
134622
134623
134624
134625#if !defined(SQLITE_OMIT_TRIGGER)
134626 /* Discard the results. This is used for SELECT statements inside
134627 ** the body of a TRIGGER. The purpose of such selects is to call
134628 ** user-defined functions that have side effects. We do not care
134629 ** about the actual results of the select.
134630 */
134631 default: {
134632 assert( eDest==SRT_Discard );
134633 break;
134634 }
134635#endif
134636 }
134637
134638 /* Jump to the end of the loop if the LIMIT is reached. Except, if
134639 ** there is a sorter, in which case the sorter has already limited
134640 ** the output for us.
134641 */
134642 if( pSort==0 && p->iLimit ){
134643 sqlite3VdbeAddOp2(p: v, OP_DecrJumpZero, p1: p->iLimit, p2: iBreak); VdbeCoverage(v);
134644 }
134645}
134646
134647/*
134648** Allocate a KeyInfo object sufficient for an index of N key columns and
134649** X extra columns.
134650*/
134651SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *db, int N, int X){
134652 int nExtra = (N+X)*(sizeof(CollSeq*)+1) - sizeof(CollSeq*);
134653 KeyInfo *p = sqlite3DbMallocRawNN(db, n: sizeof(KeyInfo) + nExtra);
134654 if( p ){
134655 p->aSortFlags = (u8*)&p->aColl[N+X];
134656 p->nKeyField = (u16)N;
134657 p->nAllField = (u16)(N+X);
134658 p->enc = ENC(db);
134659 p->db = db;
134660 p->nRef = 1;
134661 memset(s: &p[1], c: 0, n: nExtra);
134662 }else{
134663 sqlite3OomFault(db);
134664 }
134665 return p;
134666}
134667
134668/*
134669** Deallocate a KeyInfo object
134670*/
134671SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo *p){
134672 if( p ){
134673 assert( p->nRef>0 );
134674 p->nRef--;
134675 if( p->nRef==0 ) sqlite3DbFreeNN(db: p->db, p);
134676 }
134677}
134678
134679/*
134680** Make a new pointer to a KeyInfo object
134681*/
134682SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoRef(KeyInfo *p){
134683 if( p ){
134684 assert( p->nRef>0 );
134685 p->nRef++;
134686 }
134687 return p;
134688}
134689
134690#ifdef SQLITE_DEBUG
134691/*
134692** Return TRUE if a KeyInfo object can be change. The KeyInfo object
134693** can only be changed if this is just a single reference to the object.
134694**
134695** This routine is used only inside of assert() statements.
134696*/
134697SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
134698#endif /* SQLITE_DEBUG */
134699
134700/*
134701** Given an expression list, generate a KeyInfo structure that records
134702** the collating sequence for each expression in that expression list.
134703**
134704** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
134705** KeyInfo structure is appropriate for initializing a virtual index to
134706** implement that clause. If the ExprList is the result set of a SELECT
134707** then the KeyInfo structure is appropriate for initializing a virtual
134708** index to implement a DISTINCT test.
134709**
134710** Space to hold the KeyInfo structure is obtained from malloc. The calling
134711** function is responsible for seeing that this structure is eventually
134712** freed.
134713*/
134714SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoFromExprList(
134715 Parse *pParse, /* Parsing context */
134716 ExprList *pList, /* Form the KeyInfo object from this ExprList */
134717 int iStart, /* Begin with this column of pList */
134718 int nExtra /* Add this many extra columns to the end */
134719){
134720 int nExpr;
134721 KeyInfo *pInfo;
134722 struct ExprList_item *pItem;
134723 sqlite3 *db = pParse->db;
134724 int i;
134725
134726 nExpr = pList->nExpr;
134727 pInfo = sqlite3KeyInfoAlloc(db, N: nExpr-iStart, X: nExtra+1);
134728 if( pInfo ){
134729 assert( sqlite3KeyInfoIsWriteable(pInfo) );
134730 for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){
134731 pInfo->aColl[i-iStart] = sqlite3ExprNNCollSeq(pParse, pExpr: pItem->pExpr);
134732 pInfo->aSortFlags[i-iStart] = pItem->sortFlags;
134733 }
134734 }
134735 return pInfo;
134736}
134737
134738/*
134739** Name of the connection operator, used for error messages.
134740*/
134741SQLITE_PRIVATE const char *sqlite3SelectOpName(int id){
134742 char *z;
134743 switch( id ){
134744 case TK_ALL: z = "UNION ALL"; break;
134745 case TK_INTERSECT: z = "INTERSECT"; break;
134746 case TK_EXCEPT: z = "EXCEPT"; break;
134747 default: z = "UNION"; break;
134748 }
134749 return z;
134750}
134751
134752#ifndef SQLITE_OMIT_EXPLAIN
134753/*
134754** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
134755** is a no-op. Otherwise, it adds a single row of output to the EQP result,
134756** where the caption is of the form:
134757**
134758** "USE TEMP B-TREE FOR xxx"
134759**
134760** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which
134761** is determined by the zUsage argument.
134762*/
134763static void explainTempTable(Parse *pParse, const char *zUsage){
134764 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s", zUsage));
134765}
134766
134767/*
134768** Assign expression b to lvalue a. A second, no-op, version of this macro
134769** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code
134770** in sqlite3Select() to assign values to structure member variables that
134771** only exist if SQLITE_OMIT_EXPLAIN is not defined without polluting the
134772** code with #ifndef directives.
134773*/
134774# define explainSetInteger(a, b) a = b
134775
134776#else
134777/* No-op versions of the explainXXX() functions and macros. */
134778# define explainTempTable(y,z)
134779# define explainSetInteger(y,z)
134780#endif
134781
134782
134783/*
134784** If the inner loop was generated using a non-null pOrderBy argument,
134785** then the results were placed in a sorter. After the loop is terminated
134786** we need to run the sorter and output the results. The following
134787** routine generates the code needed to do that.
134788*/
134789static void generateSortTail(
134790 Parse *pParse, /* Parsing context */
134791 Select *p, /* The SELECT statement */
134792 SortCtx *pSort, /* Information on the ORDER BY clause */
134793 int nColumn, /* Number of columns of data */
134794 SelectDest *pDest /* Write the sorted results here */
134795){
134796 Vdbe *v = pParse->pVdbe; /* The prepared statement */
134797 int addrBreak = pSort->labelDone; /* Jump here to exit loop */
134798 int addrContinue = sqlite3VdbeMakeLabel(pParse);/* Jump here for next cycle */
134799 int addr; /* Top of output loop. Jump for Next. */
134800 int addrOnce = 0;
134801 int iTab;
134802 ExprList *pOrderBy = pSort->pOrderBy;
134803 int eDest = pDest->eDest;
134804 int iParm = pDest->iSDParm;
134805 int regRow;
134806 int regRowid;
134807 int iCol;
134808 int nKey; /* Number of key columns in sorter record */
134809 int iSortTab; /* Sorter cursor to read from */
134810 int i;
134811 int bSeq; /* True if sorter record includes seq. no. */
134812 int nRefKey = 0;
134813 struct ExprList_item *aOutEx = p->pEList->a;
134814
134815 assert( addrBreak<0 );
134816 if( pSort->labelBkOut ){
134817 sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: pSort->regReturn, p2: pSort->labelBkOut);
134818 sqlite3VdbeGoto(p: v, iDest: addrBreak);
134819 sqlite3VdbeResolveLabel(v, x: pSort->labelBkOut);
134820 }
134821
134822#ifdef SQLITE_ENABLE_SORTER_REFERENCES
134823 /* Open any cursors needed for sorter-reference expressions */
134824 for(i=0; i<pSort->nDefer; i++){
134825 Table *pTab = pSort->aDefer[i].pTab;
134826 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
134827 sqlite3OpenTable(pParse, pSort->aDefer[i].iCsr, iDb, pTab, OP_OpenRead);
134828 nRefKey = MAX(nRefKey, pSort->aDefer[i].nKey);
134829 }
134830#endif
134831
134832 iTab = pSort->iECursor;
134833 if( eDest==SRT_Output || eDest==SRT_Coroutine || eDest==SRT_Mem ){
134834 regRowid = 0;
134835 regRow = pDest->iSdst;
134836 }else{
134837 regRowid = sqlite3GetTempReg(pParse);
134838 if( eDest==SRT_EphemTab || eDest==SRT_Table ){
134839 regRow = sqlite3GetTempReg(pParse);
134840 nColumn = 0;
134841 }else{
134842 regRow = sqlite3GetTempRange(pParse, nReg: nColumn);
134843 }
134844 }
134845 nKey = pOrderBy->nExpr - pSort->nOBSat;
134846 if( pSort->sortFlags & SORTFLAG_UseSorter ){
134847 int regSortOut = ++pParse->nMem;
134848 iSortTab = pParse->nTab++;
134849 if( pSort->labelBkOut ){
134850 addrOnce = sqlite3VdbeAddOp0(p: v, OP_Once); VdbeCoverage(v);
134851 }
134852 sqlite3VdbeAddOp3(p: v, OP_OpenPseudo, p1: iSortTab, p2: regSortOut,
134853 p3: nKey+1+nColumn+nRefKey);
134854 if( addrOnce ) sqlite3VdbeJumpHere(p: v, addr: addrOnce);
134855 addr = 1 + sqlite3VdbeAddOp2(p: v, OP_SorterSort, p1: iTab, p2: addrBreak);
134856 VdbeCoverage(v);
134857 codeOffset(v, iOffset: p->iOffset, iContinue: addrContinue);
134858 sqlite3VdbeAddOp3(p: v, OP_SorterData, p1: iTab, p2: regSortOut, p3: iSortTab);
134859 bSeq = 0;
134860 }else{
134861 addr = 1 + sqlite3VdbeAddOp2(p: v, OP_Sort, p1: iTab, p2: addrBreak); VdbeCoverage(v);
134862 codeOffset(v, iOffset: p->iOffset, iContinue: addrContinue);
134863 iSortTab = iTab;
134864 bSeq = 1;
134865 }
134866 for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){
134867#ifdef SQLITE_ENABLE_SORTER_REFERENCES
134868 if( aOutEx[i].bSorterRef ) continue;
134869#endif
134870 if( aOutEx[i].u.x.iOrderByCol==0 ) iCol++;
134871 }
134872#ifdef SQLITE_ENABLE_SORTER_REFERENCES
134873 if( pSort->nDefer ){
134874 int iKey = iCol+1;
134875 int regKey = sqlite3GetTempRange(pParse, nRefKey);
134876
134877 for(i=0; i<pSort->nDefer; i++){
134878 int iCsr = pSort->aDefer[i].iCsr;
134879 Table *pTab = pSort->aDefer[i].pTab;
134880 int nKey = pSort->aDefer[i].nKey;
134881
134882 sqlite3VdbeAddOp1(v, OP_NullRow, iCsr);
134883 if( HasRowid(pTab) ){
134884 sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iKey++, regKey);
134885 sqlite3VdbeAddOp3(v, OP_SeekRowid, iCsr,
134886 sqlite3VdbeCurrentAddr(v)+1, regKey);
134887 }else{
134888 int k;
134889 int iJmp;
134890 assert( sqlite3PrimaryKeyIndex(pTab)->nKeyCol==nKey );
134891 for(k=0; k<nKey; k++){
134892 sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iKey++, regKey+k);
134893 }
134894 iJmp = sqlite3VdbeCurrentAddr(v);
134895 sqlite3VdbeAddOp4Int(v, OP_SeekGE, iCsr, iJmp+2, regKey, nKey);
134896 sqlite3VdbeAddOp4Int(v, OP_IdxLE, iCsr, iJmp+3, regKey, nKey);
134897 sqlite3VdbeAddOp1(v, OP_NullRow, iCsr);
134898 }
134899 }
134900 sqlite3ReleaseTempRange(pParse, regKey, nRefKey);
134901 }
134902#endif
134903 for(i=nColumn-1; i>=0; i--){
134904#ifdef SQLITE_ENABLE_SORTER_REFERENCES
134905 if( aOutEx[i].bSorterRef ){
134906 sqlite3ExprCode(pParse, aOutEx[i].pExpr, regRow+i);
134907 }else
134908#endif
134909 {
134910 int iRead;
134911 if( aOutEx[i].u.x.iOrderByCol ){
134912 iRead = aOutEx[i].u.x.iOrderByCol-1;
134913 }else{
134914 iRead = iCol--;
134915 }
134916 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iSortTab, p2: iRead, p3: regRow+i);
134917 VdbeComment((v, "%s", aOutEx[i].zEName));
134918 }
134919 }
134920 switch( eDest ){
134921 case SRT_Table:
134922 case SRT_EphemTab: {
134923 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iSortTab, p2: nKey+bSeq, p3: regRow);
134924 sqlite3VdbeAddOp2(p: v, OP_NewRowid, p1: iParm, p2: regRowid);
134925 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: iParm, p2: regRow, p3: regRowid);
134926 sqlite3VdbeChangeP5(p: v, OPFLAG_APPEND);
134927 break;
134928 }
134929#ifndef SQLITE_OMIT_SUBQUERY
134930 case SRT_Set: {
134931 assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) );
134932 sqlite3VdbeAddOp4(p: v, OP_MakeRecord, p1: regRow, p2: nColumn, p3: regRowid,
134933 zP4: pDest->zAffSdst, p4type: nColumn);
134934 sqlite3VdbeAddOp4Int(p: v, OP_IdxInsert, p1: iParm, p2: regRowid, p3: regRow, p4: nColumn);
134935 break;
134936 }
134937 case SRT_Mem: {
134938 /* The LIMIT clause will terminate the loop for us */
134939 break;
134940 }
134941#endif
134942 case SRT_Upfrom: {
134943 int i2 = pDest->iSDParm2;
134944 int r1 = sqlite3GetTempReg(pParse);
134945 sqlite3VdbeAddOp3(p: v, OP_MakeRecord,p1: regRow+(i2<0),p2: nColumn-(i2<0),p3: r1);
134946 if( i2<0 ){
134947 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: iParm, p2: r1, p3: regRow);
134948 }else{
134949 sqlite3VdbeAddOp4Int(p: v, OP_IdxInsert, p1: iParm, p2: r1, p3: regRow, p4: i2);
134950 }
134951 break;
134952 }
134953 default: {
134954 assert( eDest==SRT_Output || eDest==SRT_Coroutine );
134955 testcase( eDest==SRT_Output );
134956 testcase( eDest==SRT_Coroutine );
134957 if( eDest==SRT_Output ){
134958 sqlite3VdbeAddOp2(p: v, OP_ResultRow, p1: pDest->iSdst, p2: nColumn);
134959 }else{
134960 sqlite3VdbeAddOp1(p: v, OP_Yield, p1: pDest->iSDParm);
134961 }
134962 break;
134963 }
134964 }
134965 if( regRowid ){
134966 if( eDest==SRT_Set ){
134967 sqlite3ReleaseTempRange(pParse, iReg: regRow, nReg: nColumn);
134968 }else{
134969 sqlite3ReleaseTempReg(pParse, iReg: regRow);
134970 }
134971 sqlite3ReleaseTempReg(pParse, iReg: regRowid);
134972 }
134973 /* The bottom of the loop
134974 */
134975 sqlite3VdbeResolveLabel(v, x: addrContinue);
134976 if( pSort->sortFlags & SORTFLAG_UseSorter ){
134977 sqlite3VdbeAddOp2(p: v, OP_SorterNext, p1: iTab, p2: addr); VdbeCoverage(v);
134978 }else{
134979 sqlite3VdbeAddOp2(p: v, OP_Next, p1: iTab, p2: addr); VdbeCoverage(v);
134980 }
134981 if( pSort->regReturn ) sqlite3VdbeAddOp1(p: v, OP_Return, p1: pSort->regReturn);
134982 sqlite3VdbeResolveLabel(v, x: addrBreak);
134983}
134984
134985/*
134986** Return a pointer to a string containing the 'declaration type' of the
134987** expression pExpr. The string may be treated as static by the caller.
134988**
134989** Also try to estimate the size of the returned value and return that
134990** result in *pEstWidth.
134991**
134992** The declaration type is the exact datatype definition extracted from the
134993** original CREATE TABLE statement if the expression is a column. The
134994** declaration type for a ROWID field is INTEGER. Exactly when an expression
134995** is considered a column can be complex in the presence of subqueries. The
134996** result-set expression in all of the following SELECT statements is
134997** considered a column by this function.
134998**
134999** SELECT col FROM tbl;
135000** SELECT (SELECT col FROM tbl;
135001** SELECT (SELECT col FROM tbl);
135002** SELECT abc FROM (SELECT col AS abc FROM tbl);
135003**
135004** The declaration type for any expression other than a column is NULL.
135005**
135006** This routine has either 3 or 6 parameters depending on whether or not
135007** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
135008*/
135009#ifdef SQLITE_ENABLE_COLUMN_METADATA
135010# define columnType(A,B,C,D,E) columnTypeImpl(A,B,C,D,E)
135011#else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
135012# define columnType(A,B,C,D,E) columnTypeImpl(A,B)
135013#endif
135014static const char *columnTypeImpl(
135015 NameContext *pNC,
135016#ifndef SQLITE_ENABLE_COLUMN_METADATA
135017 Expr *pExpr
135018#else
135019 Expr *pExpr,
135020 const char **pzOrigDb,
135021 const char **pzOrigTab,
135022 const char **pzOrigCol
135023#endif
135024){
135025 char const *zType = 0;
135026 int j;
135027#ifdef SQLITE_ENABLE_COLUMN_METADATA
135028 char const *zOrigDb = 0;
135029 char const *zOrigTab = 0;
135030 char const *zOrigCol = 0;
135031#endif
135032
135033 assert( pExpr!=0 );
135034 assert( pNC->pSrcList!=0 );
135035 switch( pExpr->op ){
135036 case TK_COLUMN: {
135037 /* The expression is a column. Locate the table the column is being
135038 ** extracted from in NameContext.pSrcList. This table may be real
135039 ** database table or a subquery.
135040 */
135041 Table *pTab = 0; /* Table structure column is extracted from */
135042 Select *pS = 0; /* Select the column is extracted from */
135043 int iCol = pExpr->iColumn; /* Index of column in pTab */
135044 while( pNC && !pTab ){
135045 SrcList *pTabList = pNC->pSrcList;
135046 for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
135047 if( j<pTabList->nSrc ){
135048 pTab = pTabList->a[j].pTab;
135049 pS = pTabList->a[j].pSelect;
135050 }else{
135051 pNC = pNC->pNext;
135052 }
135053 }
135054
135055 if( pTab==0 ){
135056 /* At one time, code such as "SELECT new.x" within a trigger would
135057 ** cause this condition to run. Since then, we have restructured how
135058 ** trigger code is generated and so this condition is no longer
135059 ** possible. However, it can still be true for statements like
135060 ** the following:
135061 **
135062 ** CREATE TABLE t1(col INTEGER);
135063 ** SELECT (SELECT t1.col) FROM FROM t1;
135064 **
135065 ** when columnType() is called on the expression "t1.col" in the
135066 ** sub-select. In this case, set the column type to NULL, even
135067 ** though it should really be "INTEGER".
135068 **
135069 ** This is not a problem, as the column type of "t1.col" is never
135070 ** used. When columnType() is called on the expression
135071 ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
135072 ** branch below. */
135073 break;
135074 }
135075
135076 assert( pTab && ExprUseYTab(pExpr) && pExpr->y.pTab==pTab );
135077 if( pS ){
135078 /* The "table" is actually a sub-select or a view in the FROM clause
135079 ** of the SELECT statement. Return the declaration type and origin
135080 ** data for the result-set column of the sub-select.
135081 */
135082 if( iCol<pS->pEList->nExpr
135083#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
135084 && iCol>=0
135085#else
135086 && ALWAYS(iCol>=0)
135087#endif
135088 ){
135089 /* If iCol is less than zero, then the expression requests the
135090 ** rowid of the sub-select or view. This expression is legal (see
135091 ** test case misc2.2.2) - it always evaluates to NULL.
135092 */
135093 NameContext sNC;
135094 Expr *p = pS->pEList->a[iCol].pExpr;
135095 sNC.pSrcList = pS->pSrc;
135096 sNC.pNext = pNC;
135097 sNC.pParse = pNC->pParse;
135098 zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol);
135099 }
135100 }else{
135101 /* A real table or a CTE table */
135102 assert( !pS );
135103#ifdef SQLITE_ENABLE_COLUMN_METADATA
135104 if( iCol<0 ) iCol = pTab->iPKey;
135105 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
135106 if( iCol<0 ){
135107 zType = "INTEGER";
135108 zOrigCol = "rowid";
135109 }else{
135110 zOrigCol = pTab->aCol[iCol].zCnName;
135111 zType = sqlite3ColumnType(pCol: &pTab->aCol[iCol],zDflt: 0);
135112 }
135113 zOrigTab = pTab->zName;
135114 if( pNC->pParse && pTab->pSchema ){
135115 int iDb = sqlite3SchemaToIndex(db: pNC->pParse->db, pSchema: pTab->pSchema);
135116 zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName;
135117 }
135118#else
135119 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
135120 if( iCol<0 ){
135121 zType = "INTEGER";
135122 }else{
135123 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
135124 }
135125#endif
135126 }
135127 break;
135128 }
135129#ifndef SQLITE_OMIT_SUBQUERY
135130 case TK_SELECT: {
135131 /* The expression is a sub-select. Return the declaration type and
135132 ** origin info for the single column in the result set of the SELECT
135133 ** statement.
135134 */
135135 NameContext sNC;
135136 Select *pS;
135137 Expr *p;
135138 assert( ExprUseXSelect(pExpr) );
135139 pS = pExpr->x.pSelect;
135140 p = pS->pEList->a[0].pExpr;
135141 sNC.pSrcList = pS->pSrc;
135142 sNC.pNext = pNC;
135143 sNC.pParse = pNC->pParse;
135144 zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
135145 break;
135146 }
135147#endif
135148 }
135149
135150#ifdef SQLITE_ENABLE_COLUMN_METADATA
135151 if( pzOrigDb ){
135152 assert( pzOrigTab && pzOrigCol );
135153 *pzOrigDb = zOrigDb;
135154 *pzOrigTab = zOrigTab;
135155 *pzOrigCol = zOrigCol;
135156 }
135157#endif
135158 return zType;
135159}
135160
135161/*
135162** Generate code that will tell the VDBE the declaration types of columns
135163** in the result set.
135164*/
135165static void generateColumnTypes(
135166 Parse *pParse, /* Parser context */
135167 SrcList *pTabList, /* List of tables */
135168 ExprList *pEList /* Expressions defining the result set */
135169){
135170#ifndef SQLITE_OMIT_DECLTYPE
135171 Vdbe *v = pParse->pVdbe;
135172 int i;
135173 NameContext sNC;
135174 sNC.pSrcList = pTabList;
135175 sNC.pParse = pParse;
135176 sNC.pNext = 0;
135177 for(i=0; i<pEList->nExpr; i++){
135178 Expr *p = pEList->a[i].pExpr;
135179 const char *zType;
135180#ifdef SQLITE_ENABLE_COLUMN_METADATA
135181 const char *zOrigDb = 0;
135182 const char *zOrigTab = 0;
135183 const char *zOrigCol = 0;
135184 zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
135185
135186 /* The vdbe must make its own copy of the column-type and other
135187 ** column specific strings, in case the schema is reset before this
135188 ** virtual machine is deleted.
135189 */
135190 sqlite3VdbeSetColName(p: v, idx: i, COLNAME_DATABASE, zName: zOrigDb, SQLITE_TRANSIENT);
135191 sqlite3VdbeSetColName(p: v, idx: i, COLNAME_TABLE, zName: zOrigTab, SQLITE_TRANSIENT);
135192 sqlite3VdbeSetColName(p: v, idx: i, COLNAME_COLUMN, zName: zOrigCol, SQLITE_TRANSIENT);
135193#else
135194 zType = columnType(&sNC, p, 0, 0, 0);
135195#endif
135196 sqlite3VdbeSetColName(p: v, idx: i, COLNAME_DECLTYPE, zName: zType, SQLITE_TRANSIENT);
135197 }
135198#endif /* !defined(SQLITE_OMIT_DECLTYPE) */
135199}
135200
135201
135202/*
135203** Compute the column names for a SELECT statement.
135204**
135205** The only guarantee that SQLite makes about column names is that if the
135206** column has an AS clause assigning it a name, that will be the name used.
135207** That is the only documented guarantee. However, countless applications
135208** developed over the years have made baseless assumptions about column names
135209** and will break if those assumptions changes. Hence, use extreme caution
135210** when modifying this routine to avoid breaking legacy.
135211**
135212** See Also: sqlite3ColumnsFromExprList()
135213**
135214** The PRAGMA short_column_names and PRAGMA full_column_names settings are
135215** deprecated. The default setting is short=ON, full=OFF. 99.9% of all
135216** applications should operate this way. Nevertheless, we need to support the
135217** other modes for legacy:
135218**
135219** short=OFF, full=OFF: Column name is the text of the expression has it
135220** originally appears in the SELECT statement. In
135221** other words, the zSpan of the result expression.
135222**
135223** short=ON, full=OFF: (This is the default setting). If the result
135224** refers directly to a table column, then the
135225** result column name is just the table column
135226** name: COLUMN. Otherwise use zSpan.
135227**
135228** full=ON, short=ANY: If the result refers directly to a table column,
135229** then the result column name with the table name
135230** prefix, ex: TABLE.COLUMN. Otherwise use zSpan.
135231*/
135232SQLITE_PRIVATE void sqlite3GenerateColumnNames(
135233 Parse *pParse, /* Parser context */
135234 Select *pSelect /* Generate column names for this SELECT statement */
135235){
135236 Vdbe *v = pParse->pVdbe;
135237 int i;
135238 Table *pTab;
135239 SrcList *pTabList;
135240 ExprList *pEList;
135241 sqlite3 *db = pParse->db;
135242 int fullName; /* TABLE.COLUMN if no AS clause and is a direct table ref */
135243 int srcName; /* COLUMN or TABLE.COLUMN if no AS clause and is direct */
135244
135245#ifndef SQLITE_OMIT_EXPLAIN
135246 /* If this is an EXPLAIN, skip this step */
135247 if( pParse->explain ){
135248 return;
135249 }
135250#endif
135251
135252 if( pParse->colNamesSet ) return;
135253 /* Column names are determined by the left-most term of a compound select */
135254 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
135255 SELECTTRACE(1,pParse,pSelect,("generating column names\n"));
135256 pTabList = pSelect->pSrc;
135257 pEList = pSelect->pEList;
135258 assert( v!=0 );
135259 assert( pTabList!=0 );
135260 pParse->colNamesSet = 1;
135261 fullName = (db->flags & SQLITE_FullColNames)!=0;
135262 srcName = (db->flags & SQLITE_ShortColNames)!=0 || fullName;
135263 sqlite3VdbeSetNumCols(p: v, nResColumn: pEList->nExpr);
135264 for(i=0; i<pEList->nExpr; i++){
135265 Expr *p = pEList->a[i].pExpr;
135266
135267 assert( p!=0 );
135268 assert( p->op!=TK_AGG_COLUMN ); /* Agg processing has not run yet */
135269 assert( p->op!=TK_COLUMN
135270 || (ExprUseYTab(p) && p->y.pTab!=0) ); /* Covering idx not yet coded */
135271 if( pEList->a[i].zEName && pEList->a[i].eEName==ENAME_NAME ){
135272 /* An AS clause always takes first priority */
135273 char *zName = pEList->a[i].zEName;
135274 sqlite3VdbeSetColName(p: v, idx: i, COLNAME_NAME, zName, SQLITE_TRANSIENT);
135275 }else if( srcName && p->op==TK_COLUMN ){
135276 char *zCol;
135277 int iCol = p->iColumn;
135278 pTab = p->y.pTab;
135279 assert( pTab!=0 );
135280 if( iCol<0 ) iCol = pTab->iPKey;
135281 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
135282 if( iCol<0 ){
135283 zCol = "rowid";
135284 }else{
135285 zCol = pTab->aCol[iCol].zCnName;
135286 }
135287 if( fullName ){
135288 char *zName = 0;
135289 zName = sqlite3MPrintf(db, zFormat: "%s.%s", pTab->zName, zCol);
135290 sqlite3VdbeSetColName(p: v, idx: i, COLNAME_NAME, zName, SQLITE_DYNAMIC);
135291 }else{
135292 sqlite3VdbeSetColName(p: v, idx: i, COLNAME_NAME, zName: zCol, SQLITE_TRANSIENT);
135293 }
135294 }else{
135295 const char *z = pEList->a[i].zEName;
135296 z = z==0 ? sqlite3MPrintf(db, zFormat: "column%d", i+1) : sqlite3DbStrDup(db, z);
135297 sqlite3VdbeSetColName(p: v, idx: i, COLNAME_NAME, zName: z, SQLITE_DYNAMIC);
135298 }
135299 }
135300 generateColumnTypes(pParse, pTabList, pEList);
135301}
135302
135303/*
135304** Given an expression list (which is really the list of expressions
135305** that form the result set of a SELECT statement) compute appropriate
135306** column names for a table that would hold the expression list.
135307**
135308** All column names will be unique.
135309**
135310** Only the column names are computed. Column.zType, Column.zColl,
135311** and other fields of Column are zeroed.
135312**
135313** Return SQLITE_OK on success. If a memory allocation error occurs,
135314** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM.
135315**
135316** The only guarantee that SQLite makes about column names is that if the
135317** column has an AS clause assigning it a name, that will be the name used.
135318** That is the only documented guarantee. However, countless applications
135319** developed over the years have made baseless assumptions about column names
135320** and will break if those assumptions changes. Hence, use extreme caution
135321** when modifying this routine to avoid breaking legacy.
135322**
135323** See Also: sqlite3GenerateColumnNames()
135324*/
135325SQLITE_PRIVATE int sqlite3ColumnsFromExprList(
135326 Parse *pParse, /* Parsing context */
135327 ExprList *pEList, /* Expr list from which to derive column names */
135328 i16 *pnCol, /* Write the number of columns here */
135329 Column **paCol /* Write the new column list here */
135330){
135331 sqlite3 *db = pParse->db; /* Database connection */
135332 int i, j; /* Loop counters */
135333 u32 cnt; /* Index added to make the name unique */
135334 Column *aCol, *pCol; /* For looping over result columns */
135335 int nCol; /* Number of columns in the result set */
135336 char *zName; /* Column name */
135337 int nName; /* Size of name in zName[] */
135338 Hash ht; /* Hash table of column names */
135339 Table *pTab;
135340
135341 sqlite3HashInit(pNew: &ht);
135342 if( pEList ){
135343 nCol = pEList->nExpr;
135344 aCol = sqlite3DbMallocZero(db, n: sizeof(aCol[0])*nCol);
135345 testcase( aCol==0 );
135346 if( NEVER(nCol>32767) ) nCol = 32767;
135347 }else{
135348 nCol = 0;
135349 aCol = 0;
135350 }
135351 assert( nCol==(i16)nCol );
135352 *pnCol = nCol;
135353 *paCol = aCol;
135354
135355 for(i=0, pCol=aCol; i<nCol && !db->mallocFailed; i++, pCol++){
135356 /* Get an appropriate name for the column
135357 */
135358 if( (zName = pEList->a[i].zEName)!=0 && pEList->a[i].eEName==ENAME_NAME ){
135359 /* If the column contains an "AS <name>" phrase, use <name> as the name */
135360 }else{
135361 Expr *pColExpr = sqlite3ExprSkipCollateAndLikely(pExpr: pEList->a[i].pExpr);
135362 while( ALWAYS(pColExpr!=0) && pColExpr->op==TK_DOT ){
135363 pColExpr = pColExpr->pRight;
135364 assert( pColExpr!=0 );
135365 }
135366 if( pColExpr->op==TK_COLUMN
135367 && ALWAYS( ExprUseYTab(pColExpr) )
135368 && (pTab = pColExpr->y.pTab)!=0
135369 ){
135370 /* For columns use the column name name */
135371 int iCol = pColExpr->iColumn;
135372 if( iCol<0 ) iCol = pTab->iPKey;
135373 zName = iCol>=0 ? pTab->aCol[iCol].zCnName : "rowid";
135374 }else if( pColExpr->op==TK_ID ){
135375 assert( !ExprHasProperty(pColExpr, EP_IntValue) );
135376 zName = pColExpr->u.zToken;
135377 }else{
135378 /* Use the original text of the column expression as its name */
135379 zName = pEList->a[i].zEName;
135380 }
135381 }
135382 if( zName && !sqlite3IsTrueOrFalse(zIn: zName) ){
135383 zName = sqlite3DbStrDup(db, z: zName);
135384 }else{
135385 zName = sqlite3MPrintf(db,zFormat: "column%d",i+1);
135386 }
135387
135388 /* Make sure the column name is unique. If the name is not unique,
135389 ** append an integer to the name so that it becomes unique.
135390 */
135391 cnt = 0;
135392 while( zName && sqlite3HashFind(pH: &ht, pKey: zName)!=0 ){
135393 nName = sqlite3Strlen30(z: zName);
135394 if( nName>0 ){
135395 for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){}
135396 if( zName[j]==':' ) nName = j;
135397 }
135398 zName = sqlite3MPrintf(db, zFormat: "%.*z:%u", nName, zName, ++cnt);
135399 if( cnt>3 ) sqlite3_randomness(N: sizeof(cnt), pBuf: &cnt);
135400 }
135401 pCol->zCnName = zName;
135402 pCol->hName = sqlite3StrIHash(z: zName);
135403 sqlite3ColumnPropertiesFromName(0, pCol);
135404 if( zName && sqlite3HashInsert(pH: &ht, pKey: zName, data: pCol)==pCol ){
135405 sqlite3OomFault(db);
135406 }
135407 }
135408 sqlite3HashClear(pH: &ht);
135409 if( db->mallocFailed ){
135410 for(j=0; j<i; j++){
135411 sqlite3DbFree(db, p: aCol[j].zCnName);
135412 }
135413 sqlite3DbFree(db, p: aCol);
135414 *paCol = 0;
135415 *pnCol = 0;
135416 return SQLITE_NOMEM_BKPT;
135417 }
135418 return SQLITE_OK;
135419}
135420
135421/*
135422** Add type and collation information to a column list based on
135423** a SELECT statement.
135424**
135425** The column list presumably came from selectColumnNamesFromExprList().
135426** The column list has only names, not types or collations. This
135427** routine goes through and adds the types and collations.
135428**
135429** This routine requires that all identifiers in the SELECT
135430** statement be resolved.
135431*/
135432SQLITE_PRIVATE void sqlite3SelectAddColumnTypeAndCollation(
135433 Parse *pParse, /* Parsing contexts */
135434 Table *pTab, /* Add column type information to this table */
135435 Select *pSelect, /* SELECT used to determine types and collations */
135436 char aff /* Default affinity for columns */
135437){
135438 sqlite3 *db = pParse->db;
135439 NameContext sNC;
135440 Column *pCol;
135441 CollSeq *pColl;
135442 int i;
135443 Expr *p;
135444 struct ExprList_item *a;
135445
135446 assert( pSelect!=0 );
135447 assert( (pSelect->selFlags & SF_Resolved)!=0 );
135448 assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed );
135449 if( db->mallocFailed ) return;
135450 memset(s: &sNC, c: 0, n: sizeof(sNC));
135451 sNC.pSrcList = pSelect->pSrc;
135452 a = pSelect->pEList->a;
135453 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
135454 const char *zType;
135455 i64 n, m;
135456 pTab->tabFlags |= (pCol->colFlags & COLFLAG_NOINSERT);
135457 p = a[i].pExpr;
135458 zType = columnType(&sNC, p, 0, 0, 0);
135459 /* pCol->szEst = ... // Column size est for SELECT tables never used */
135460 pCol->affinity = sqlite3ExprAffinity(pExpr: p);
135461 if( zType ){
135462 m = sqlite3Strlen30(z: zType);
135463 n = sqlite3Strlen30(z: pCol->zCnName);
135464 pCol->zCnName = sqlite3DbReallocOrFree(db, p: pCol->zCnName, n: n+m+2);
135465 if( pCol->zCnName ){
135466 memcpy(dest: &pCol->zCnName[n+1], src: zType, n: m+1);
135467 pCol->colFlags |= COLFLAG_HASTYPE;
135468 }else{
135469 testcase( pCol->colFlags & COLFLAG_HASTYPE );
135470 pCol->colFlags &= ~(COLFLAG_HASTYPE|COLFLAG_HASCOLL);
135471 }
135472 }
135473 if( pCol->affinity<=SQLITE_AFF_NONE ) pCol->affinity = aff;
135474 pColl = sqlite3ExprCollSeq(pParse, pExpr: p);
135475 if( pColl ){
135476 assert( pTab->pIndex==0 );
135477 sqlite3ColumnSetColl(db, pCol, zColl: pColl->zName);
135478 }
135479 }
135480 pTab->szTabRow = 1; /* Any non-zero value works */
135481}
135482
135483/*
135484** Given a SELECT statement, generate a Table structure that describes
135485** the result set of that SELECT.
135486*/
135487SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect, char aff){
135488 Table *pTab;
135489 sqlite3 *db = pParse->db;
135490 u64 savedFlags;
135491
135492 savedFlags = db->flags;
135493 db->flags &= ~(u64)SQLITE_FullColNames;
135494 db->flags |= SQLITE_ShortColNames;
135495 sqlite3SelectPrep(pParse, pSelect, 0);
135496 db->flags = savedFlags;
135497 if( pParse->nErr ) return 0;
135498 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
135499 pTab = sqlite3DbMallocZero(db, n: sizeof(Table) );
135500 if( pTab==0 ){
135501 return 0;
135502 }
135503 pTab->nTabRef = 1;
135504 pTab->zName = 0;
135505 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
135506 sqlite3ColumnsFromExprList(pParse, pEList: pSelect->pEList, pnCol: &pTab->nCol, paCol: &pTab->aCol);
135507 sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSelect, aff);
135508 pTab->iPKey = -1;
135509 if( db->mallocFailed ){
135510 sqlite3DeleteTable(db, pTable: pTab);
135511 return 0;
135512 }
135513 return pTab;
135514}
135515
135516/*
135517** Get a VDBE for the given parser context. Create a new one if necessary.
135518** If an error occurs, return NULL and leave a message in pParse.
135519*/
135520SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse *pParse){
135521 if( pParse->pVdbe ){
135522 return pParse->pVdbe;
135523 }
135524 if( pParse->pToplevel==0
135525 && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)
135526 ){
135527 pParse->okConstFactor = 1;
135528 }
135529 return sqlite3VdbeCreate(pParse);
135530}
135531
135532
135533/*
135534** Compute the iLimit and iOffset fields of the SELECT based on the
135535** pLimit expressions. pLimit->pLeft and pLimit->pRight hold the expressions
135536** that appear in the original SQL statement after the LIMIT and OFFSET
135537** keywords. Or NULL if those keywords are omitted. iLimit and iOffset
135538** are the integer memory register numbers for counters used to compute
135539** the limit and offset. If there is no limit and/or offset, then
135540** iLimit and iOffset are negative.
135541**
135542** This routine changes the values of iLimit and iOffset only if
135543** a limit or offset is defined by pLimit->pLeft and pLimit->pRight. iLimit
135544** and iOffset should have been preset to appropriate default values (zero)
135545** prior to calling this routine.
135546**
135547** The iOffset register (if it exists) is initialized to the value
135548** of the OFFSET. The iLimit register is initialized to LIMIT. Register
135549** iOffset+1 is initialized to LIMIT+OFFSET.
135550**
135551** Only if pLimit->pLeft!=0 do the limit registers get
135552** redefined. The UNION ALL operator uses this property to force
135553** the reuse of the same limit and offset registers across multiple
135554** SELECT statements.
135555*/
135556static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
135557 Vdbe *v = 0;
135558 int iLimit = 0;
135559 int iOffset;
135560 int n;
135561 Expr *pLimit = p->pLimit;
135562
135563 if( p->iLimit ) return;
135564
135565 /*
135566 ** "LIMIT -1" always shows all rows. There is some
135567 ** controversy about what the correct behavior should be.
135568 ** The current implementation interprets "LIMIT 0" to mean
135569 ** no rows.
135570 */
135571 if( pLimit ){
135572 assert( pLimit->op==TK_LIMIT );
135573 assert( pLimit->pLeft!=0 );
135574 p->iLimit = iLimit = ++pParse->nMem;
135575 v = sqlite3GetVdbe(pParse);
135576 assert( v!=0 );
135577 if( sqlite3ExprIsInteger(p: pLimit->pLeft, pValue: &n) ){
135578 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: n, p2: iLimit);
135579 VdbeComment((v, "LIMIT counter"));
135580 if( n==0 ){
135581 sqlite3VdbeGoto(p: v, iDest: iBreak);
135582 }else if( n>=0 && p->nSelectRow>sqlite3LogEst(x: (u64)n) ){
135583 p->nSelectRow = sqlite3LogEst(x: (u64)n);
135584 p->selFlags |= SF_FixedLimit;
135585 }
135586 }else{
135587 sqlite3ExprCode(pParse, pExpr: pLimit->pLeft, target: iLimit);
135588 sqlite3VdbeAddOp1(p: v, OP_MustBeInt, p1: iLimit); VdbeCoverage(v);
135589 VdbeComment((v, "LIMIT counter"));
135590 sqlite3VdbeAddOp2(p: v, OP_IfNot, p1: iLimit, p2: iBreak); VdbeCoverage(v);
135591 }
135592 if( pLimit->pRight ){
135593 p->iOffset = iOffset = ++pParse->nMem;
135594 pParse->nMem++; /* Allocate an extra register for limit+offset */
135595 sqlite3ExprCode(pParse, pExpr: pLimit->pRight, target: iOffset);
135596 sqlite3VdbeAddOp1(p: v, OP_MustBeInt, p1: iOffset); VdbeCoverage(v);
135597 VdbeComment((v, "OFFSET counter"));
135598 sqlite3VdbeAddOp3(p: v, OP_OffsetLimit, p1: iLimit, p2: iOffset+1, p3: iOffset);
135599 VdbeComment((v, "LIMIT+OFFSET"));
135600 }
135601 }
135602}
135603
135604#ifndef SQLITE_OMIT_COMPOUND_SELECT
135605/*
135606** Return the appropriate collating sequence for the iCol-th column of
135607** the result set for the compound-select statement "p". Return NULL if
135608** the column has no default collating sequence.
135609**
135610** The collating sequence for the compound select is taken from the
135611** left-most term of the select that has a collating sequence.
135612*/
135613static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
135614 CollSeq *pRet;
135615 if( p->pPrior ){
135616 pRet = multiSelectCollSeq(pParse, p: p->pPrior, iCol);
135617 }else{
135618 pRet = 0;
135619 }
135620 assert( iCol>=0 );
135621 /* iCol must be less than p->pEList->nExpr. Otherwise an error would
135622 ** have been thrown during name resolution and we would not have gotten
135623 ** this far */
135624 if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr) ){
135625 pRet = sqlite3ExprCollSeq(pParse, pExpr: p->pEList->a[iCol].pExpr);
135626 }
135627 return pRet;
135628}
135629
135630/*
135631** The select statement passed as the second parameter is a compound SELECT
135632** with an ORDER BY clause. This function allocates and returns a KeyInfo
135633** structure suitable for implementing the ORDER BY.
135634**
135635** Space to hold the KeyInfo structure is obtained from malloc. The calling
135636** function is responsible for ensuring that this structure is eventually
135637** freed.
135638*/
135639static KeyInfo *multiSelectOrderByKeyInfo(Parse *pParse, Select *p, int nExtra){
135640 ExprList *pOrderBy = p->pOrderBy;
135641 int nOrderBy = ALWAYS(pOrderBy!=0) ? pOrderBy->nExpr : 0;
135642 sqlite3 *db = pParse->db;
135643 KeyInfo *pRet = sqlite3KeyInfoAlloc(db, N: nOrderBy+nExtra, X: 1);
135644 if( pRet ){
135645 int i;
135646 for(i=0; i<nOrderBy; i++){
135647 struct ExprList_item *pItem = &pOrderBy->a[i];
135648 Expr *pTerm = pItem->pExpr;
135649 CollSeq *pColl;
135650
135651 if( pTerm->flags & EP_Collate ){
135652 pColl = sqlite3ExprCollSeq(pParse, pExpr: pTerm);
135653 }else{
135654 pColl = multiSelectCollSeq(pParse, p, iCol: pItem->u.x.iOrderByCol-1);
135655 if( pColl==0 ) pColl = db->pDfltColl;
135656 pOrderBy->a[i].pExpr =
135657 sqlite3ExprAddCollateString(pParse, pExpr: pTerm, zC: pColl->zName);
135658 }
135659 assert( sqlite3KeyInfoIsWriteable(pRet) );
135660 pRet->aColl[i] = pColl;
135661 pRet->aSortFlags[i] = pOrderBy->a[i].sortFlags;
135662 }
135663 }
135664
135665 return pRet;
135666}
135667
135668#ifndef SQLITE_OMIT_CTE
135669/*
135670** This routine generates VDBE code to compute the content of a WITH RECURSIVE
135671** query of the form:
135672**
135673** <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
135674** \___________/ \_______________/
135675** p->pPrior p
135676**
135677**
135678** There is exactly one reference to the recursive-table in the FROM clause
135679** of recursive-query, marked with the SrcList->a[].fg.isRecursive flag.
135680**
135681** The setup-query runs once to generate an initial set of rows that go
135682** into a Queue table. Rows are extracted from the Queue table one by
135683** one. Each row extracted from Queue is output to pDest. Then the single
135684** extracted row (now in the iCurrent table) becomes the content of the
135685** recursive-table for a recursive-query run. The output of the recursive-query
135686** is added back into the Queue table. Then another row is extracted from Queue
135687** and the iteration continues until the Queue table is empty.
135688**
135689** If the compound query operator is UNION then no duplicate rows are ever
135690** inserted into the Queue table. The iDistinct table keeps a copy of all rows
135691** that have ever been inserted into Queue and causes duplicates to be
135692** discarded. If the operator is UNION ALL, then duplicates are allowed.
135693**
135694** If the query has an ORDER BY, then entries in the Queue table are kept in
135695** ORDER BY order and the first entry is extracted for each cycle. Without
135696** an ORDER BY, the Queue table is just a FIFO.
135697**
135698** If a LIMIT clause is provided, then the iteration stops after LIMIT rows
135699** have been output to pDest. A LIMIT of zero means to output no rows and a
135700** negative LIMIT means to output all rows. If there is also an OFFSET clause
135701** with a positive value, then the first OFFSET outputs are discarded rather
135702** than being sent to pDest. The LIMIT count does not begin until after OFFSET
135703** rows have been skipped.
135704*/
135705static void generateWithRecursiveQuery(
135706 Parse *pParse, /* Parsing context */
135707 Select *p, /* The recursive SELECT to be coded */
135708 SelectDest *pDest /* What to do with query results */
135709){
135710 SrcList *pSrc = p->pSrc; /* The FROM clause of the recursive query */
135711 int nCol = p->pEList->nExpr; /* Number of columns in the recursive table */
135712 Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */
135713 Select *pSetup; /* The setup query */
135714 Select *pFirstRec; /* Left-most recursive term */
135715 int addrTop; /* Top of the loop */
135716 int addrCont, addrBreak; /* CONTINUE and BREAK addresses */
135717 int iCurrent = 0; /* The Current table */
135718 int regCurrent; /* Register holding Current table */
135719 int iQueue; /* The Queue table */
135720 int iDistinct = 0; /* To ensure unique results if UNION */
135721 int eDest = SRT_Fifo; /* How to write to Queue */
135722 SelectDest destQueue; /* SelectDest targetting the Queue table */
135723 int i; /* Loop counter */
135724 int rc; /* Result code */
135725 ExprList *pOrderBy; /* The ORDER BY clause */
135726 Expr *pLimit; /* Saved LIMIT and OFFSET */
135727 int regLimit, regOffset; /* Registers used by LIMIT and OFFSET */
135728
135729#ifndef SQLITE_OMIT_WINDOWFUNC
135730 if( p->pWin ){
135731 sqlite3ErrorMsg(pParse, zFormat: "cannot use window functions in recursive queries");
135732 return;
135733 }
135734#endif
135735
135736 /* Obtain authorization to do a recursive query */
135737 if( sqlite3AuthCheck(pParse, SQLITE_RECURSIVE, zArg1: 0, zArg2: 0, zArg3: 0) ) return;
135738
135739 /* Process the LIMIT and OFFSET clauses, if they exist */
135740 addrBreak = sqlite3VdbeMakeLabel(pParse);
135741 p->nSelectRow = 320; /* 4 billion rows */
135742 computeLimitRegisters(pParse, p, iBreak: addrBreak);
135743 pLimit = p->pLimit;
135744 regLimit = p->iLimit;
135745 regOffset = p->iOffset;
135746 p->pLimit = 0;
135747 p->iLimit = p->iOffset = 0;
135748 pOrderBy = p->pOrderBy;
135749
135750 /* Locate the cursor number of the Current table */
135751 for(i=0; ALWAYS(i<pSrc->nSrc); i++){
135752 if( pSrc->a[i].fg.isRecursive ){
135753 iCurrent = pSrc->a[i].iCursor;
135754 break;
135755 }
135756 }
135757
135758 /* Allocate cursors numbers for Queue and Distinct. The cursor number for
135759 ** the Distinct table must be exactly one greater than Queue in order
135760 ** for the SRT_DistFifo and SRT_DistQueue destinations to work. */
135761 iQueue = pParse->nTab++;
135762 if( p->op==TK_UNION ){
135763 eDest = pOrderBy ? SRT_DistQueue : SRT_DistFifo;
135764 iDistinct = pParse->nTab++;
135765 }else{
135766 eDest = pOrderBy ? SRT_Queue : SRT_Fifo;
135767 }
135768 sqlite3SelectDestInit(pDest: &destQueue, eDest, iParm: iQueue);
135769
135770 /* Allocate cursors for Current, Queue, and Distinct. */
135771 regCurrent = ++pParse->nMem;
135772 sqlite3VdbeAddOp3(p: v, OP_OpenPseudo, p1: iCurrent, p2: regCurrent, p3: nCol);
135773 if( pOrderBy ){
135774 KeyInfo *pKeyInfo = multiSelectOrderByKeyInfo(pParse, p, nExtra: 1);
135775 sqlite3VdbeAddOp4(p: v, OP_OpenEphemeral, p1: iQueue, p2: pOrderBy->nExpr+2, p3: 0,
135776 zP4: (char*)pKeyInfo, P4_KEYINFO);
135777 destQueue.pOrderBy = pOrderBy;
135778 }else{
135779 sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: iQueue, p2: nCol);
135780 }
135781 VdbeComment((v, "Queue table"));
135782 if( iDistinct ){
135783 p->addrOpenEphm[0] = sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: iDistinct, p2: 0);
135784 p->selFlags |= SF_UsesEphemeral;
135785 }
135786
135787 /* Detach the ORDER BY clause from the compound SELECT */
135788 p->pOrderBy = 0;
135789
135790 /* Figure out how many elements of the compound SELECT are part of the
135791 ** recursive query. Make sure no recursive elements use aggregate
135792 ** functions. Mark the recursive elements as UNION ALL even if they
135793 ** are really UNION because the distinctness will be enforced by the
135794 ** iDistinct table. pFirstRec is left pointing to the left-most
135795 ** recursive term of the CTE.
135796 */
135797 for(pFirstRec=p; ALWAYS(pFirstRec!=0); pFirstRec=pFirstRec->pPrior){
135798 if( pFirstRec->selFlags & SF_Aggregate ){
135799 sqlite3ErrorMsg(pParse, zFormat: "recursive aggregate queries not supported");
135800 goto end_of_recursive_query;
135801 }
135802 pFirstRec->op = TK_ALL;
135803 if( (pFirstRec->pPrior->selFlags & SF_Recursive)==0 ) break;
135804 }
135805
135806 /* Store the results of the setup-query in Queue. */
135807 pSetup = pFirstRec->pPrior;
135808 pSetup->pNext = 0;
135809 ExplainQueryPlan((pParse, 1, "SETUP"));
135810 rc = sqlite3Select(pParse, pSetup, &destQueue);
135811 pSetup->pNext = p;
135812 if( rc ) goto end_of_recursive_query;
135813
135814 /* Find the next row in the Queue and output that row */
135815 addrTop = sqlite3VdbeAddOp2(p: v, OP_Rewind, p1: iQueue, p2: addrBreak); VdbeCoverage(v);
135816
135817 /* Transfer the next row in Queue over to Current */
135818 sqlite3VdbeAddOp1(p: v, OP_NullRow, p1: iCurrent); /* To reset column cache */
135819 if( pOrderBy ){
135820 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iQueue, p2: pOrderBy->nExpr+1, p3: regCurrent);
135821 }else{
135822 sqlite3VdbeAddOp2(p: v, OP_RowData, p1: iQueue, p2: regCurrent);
135823 }
135824 sqlite3VdbeAddOp1(p: v, OP_Delete, p1: iQueue);
135825
135826 /* Output the single row in Current */
135827 addrCont = sqlite3VdbeMakeLabel(pParse);
135828 codeOffset(v, iOffset: regOffset, iContinue: addrCont);
135829 selectInnerLoop(pParse, p, srcTab: iCurrent,
135830 pSort: 0, pDistinct: 0, pDest, iContinue: addrCont, iBreak: addrBreak);
135831 if( regLimit ){
135832 sqlite3VdbeAddOp2(p: v, OP_DecrJumpZero, p1: regLimit, p2: addrBreak);
135833 VdbeCoverage(v);
135834 }
135835 sqlite3VdbeResolveLabel(v, x: addrCont);
135836
135837 /* Execute the recursive SELECT taking the single row in Current as
135838 ** the value for the recursive-table. Store the results in the Queue.
135839 */
135840 pFirstRec->pPrior = 0;
135841 ExplainQueryPlan((pParse, 1, "RECURSIVE STEP"));
135842 sqlite3Select(pParse, p, &destQueue);
135843 assert( pFirstRec->pPrior==0 );
135844 pFirstRec->pPrior = pSetup;
135845
135846 /* Keep running the loop until the Queue is empty */
135847 sqlite3VdbeGoto(p: v, iDest: addrTop);
135848 sqlite3VdbeResolveLabel(v, x: addrBreak);
135849
135850end_of_recursive_query:
135851 sqlite3ExprListDelete(db: pParse->db, pList: p->pOrderBy);
135852 p->pOrderBy = pOrderBy;
135853 p->pLimit = pLimit;
135854 return;
135855}
135856#endif /* SQLITE_OMIT_CTE */
135857
135858/* Forward references */
135859static int multiSelectOrderBy(
135860 Parse *pParse, /* Parsing context */
135861 Select *p, /* The right-most of SELECTs to be coded */
135862 SelectDest *pDest /* What to do with query results */
135863);
135864
135865/*
135866** Handle the special case of a compound-select that originates from a
135867** VALUES clause. By handling this as a special case, we avoid deep
135868** recursion, and thus do not need to enforce the SQLITE_LIMIT_COMPOUND_SELECT
135869** on a VALUES clause.
135870**
135871** Because the Select object originates from a VALUES clause:
135872** (1) There is no LIMIT or OFFSET or else there is a LIMIT of exactly 1
135873** (2) All terms are UNION ALL
135874** (3) There is no ORDER BY clause
135875**
135876** The "LIMIT of exactly 1" case of condition (1) comes about when a VALUES
135877** clause occurs within scalar expression (ex: "SELECT (VALUES(1),(2),(3))").
135878** The sqlite3CodeSubselect will have added the LIMIT 1 clause in tht case.
135879** Since the limit is exactly 1, we only need to evalutes the left-most VALUES.
135880*/
135881static int multiSelectValues(
135882 Parse *pParse, /* Parsing context */
135883 Select *p, /* The right-most of SELECTs to be coded */
135884 SelectDest *pDest /* What to do with query results */
135885){
135886 int nRow = 1;
135887 int rc = 0;
135888 int bShowAll = p->pLimit==0;
135889 assert( p->selFlags & SF_MultiValue );
135890 do{
135891 assert( p->selFlags & SF_Values );
135892 assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
135893 assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
135894#ifndef SQLITE_OMIT_WINDOWFUNC
135895 if( p->pWin ) return -1;
135896#endif
135897 if( p->pPrior==0 ) break;
135898 assert( p->pPrior->pNext==p );
135899 p = p->pPrior;
135900 nRow += bShowAll;
135901 }while(1);
135902 ExplainQueryPlan((pParse, 0, "SCAN %d CONSTANT ROW%s", nRow,
135903 nRow==1 ? "" : "S"));
135904 while( p ){
135905 selectInnerLoop(pParse, p, srcTab: -1, pSort: 0, pDistinct: 0, pDest, iContinue: 1, iBreak: 1);
135906 if( !bShowAll ) break;
135907 p->nSelectRow = nRow;
135908 p = p->pNext;
135909 }
135910 return rc;
135911}
135912
135913/*
135914** Return true if the SELECT statement which is known to be the recursive
135915** part of a recursive CTE still has its anchor terms attached. If the
135916** anchor terms have already been removed, then return false.
135917*/
135918static int hasAnchor(Select *p){
135919 while( p && (p->selFlags & SF_Recursive)!=0 ){ p = p->pPrior; }
135920 return p!=0;
135921}
135922
135923/*
135924** This routine is called to process a compound query form from
135925** two or more separate queries using UNION, UNION ALL, EXCEPT, or
135926** INTERSECT
135927**
135928** "p" points to the right-most of the two queries. the query on the
135929** left is p->pPrior. The left query could also be a compound query
135930** in which case this routine will be called recursively.
135931**
135932** The results of the total query are to be written into a destination
135933** of type eDest with parameter iParm.
135934**
135935** Example 1: Consider a three-way compound SQL statement.
135936**
135937** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
135938**
135939** This statement is parsed up as follows:
135940**
135941** SELECT c FROM t3
135942** |
135943** `-----> SELECT b FROM t2
135944** |
135945** `------> SELECT a FROM t1
135946**
135947** The arrows in the diagram above represent the Select.pPrior pointer.
135948** So if this routine is called with p equal to the t3 query, then
135949** pPrior will be the t2 query. p->op will be TK_UNION in this case.
135950**
135951** Notice that because of the way SQLite parses compound SELECTs, the
135952** individual selects always group from left to right.
135953*/
135954static int multiSelect(
135955 Parse *pParse, /* Parsing context */
135956 Select *p, /* The right-most of SELECTs to be coded */
135957 SelectDest *pDest /* What to do with query results */
135958){
135959 int rc = SQLITE_OK; /* Success code from a subroutine */
135960 Select *pPrior; /* Another SELECT immediately to our left */
135961 Vdbe *v; /* Generate code to this VDBE */
135962 SelectDest dest; /* Alternative data destination */
135963 Select *pDelete = 0; /* Chain of simple selects to delete */
135964 sqlite3 *db; /* Database connection */
135965
135966 /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only
135967 ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
135968 */
135969 assert( p && p->pPrior ); /* Calling function guarantees this much */
135970 assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
135971 assert( p->selFlags & SF_Compound );
135972 db = pParse->db;
135973 pPrior = p->pPrior;
135974 dest = *pDest;
135975 assert( pPrior->pOrderBy==0 );
135976 assert( pPrior->pLimit==0 );
135977
135978 v = sqlite3GetVdbe(pParse);
135979 assert( v!=0 ); /* The VDBE already created by calling function */
135980
135981 /* Create the destination temporary table if necessary
135982 */
135983 if( dest.eDest==SRT_EphemTab ){
135984 assert( p->pEList );
135985 sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: dest.iSDParm, p2: p->pEList->nExpr);
135986 dest.eDest = SRT_Table;
135987 }
135988
135989 /* Special handling for a compound-select that originates as a VALUES clause.
135990 */
135991 if( p->selFlags & SF_MultiValue ){
135992 rc = multiSelectValues(pParse, p, pDest: &dest);
135993 if( rc>=0 ) goto multi_select_end;
135994 rc = SQLITE_OK;
135995 }
135996
135997 /* Make sure all SELECTs in the statement have the same number of elements
135998 ** in their result sets.
135999 */
136000 assert( p->pEList && pPrior->pEList );
136001 assert( p->pEList->nExpr==pPrior->pEList->nExpr );
136002
136003#ifndef SQLITE_OMIT_CTE
136004 if( (p->selFlags & SF_Recursive)!=0 && hasAnchor(p) ){
136005 generateWithRecursiveQuery(pParse, p, pDest: &dest);
136006 }else
136007#endif
136008
136009 /* Compound SELECTs that have an ORDER BY clause are handled separately.
136010 */
136011 if( p->pOrderBy ){
136012 return multiSelectOrderBy(pParse, p, pDest);
136013 }else{
136014
136015#ifndef SQLITE_OMIT_EXPLAIN
136016 if( pPrior->pPrior==0 ){
136017 ExplainQueryPlan((pParse, 1, "COMPOUND QUERY"));
136018 ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY"));
136019 }
136020#endif
136021
136022 /* Generate code for the left and right SELECT statements.
136023 */
136024 switch( p->op ){
136025 case TK_ALL: {
136026 int addr = 0;
136027 int nLimit = 0; /* Initialize to suppress harmless compiler warning */
136028 assert( !pPrior->pLimit );
136029 pPrior->iLimit = p->iLimit;
136030 pPrior->iOffset = p->iOffset;
136031 pPrior->pLimit = p->pLimit;
136032 SELECTTRACE(1, pParse, p, ("multiSelect UNION ALL left...\n"));
136033 rc = sqlite3Select(pParse, pPrior, &dest);
136034 pPrior->pLimit = 0;
136035 if( rc ){
136036 goto multi_select_end;
136037 }
136038 p->pPrior = 0;
136039 p->iLimit = pPrior->iLimit;
136040 p->iOffset = pPrior->iOffset;
136041 if( p->iLimit ){
136042 addr = sqlite3VdbeAddOp1(p: v, OP_IfNot, p1: p->iLimit); VdbeCoverage(v);
136043 VdbeComment((v, "Jump ahead if LIMIT reached"));
136044 if( p->iOffset ){
136045 sqlite3VdbeAddOp3(p: v, OP_OffsetLimit,
136046 p1: p->iLimit, p2: p->iOffset+1, p3: p->iOffset);
136047 }
136048 }
136049 ExplainQueryPlan((pParse, 1, "UNION ALL"));
136050 SELECTTRACE(1, pParse, p, ("multiSelect UNION ALL right...\n"));
136051 rc = sqlite3Select(pParse, p, &dest);
136052 testcase( rc!=SQLITE_OK );
136053 pDelete = p->pPrior;
136054 p->pPrior = pPrior;
136055 p->nSelectRow = sqlite3LogEstAdd(a: p->nSelectRow, b: pPrior->nSelectRow);
136056 if( p->pLimit
136057 && sqlite3ExprIsInteger(p: p->pLimit->pLeft, pValue: &nLimit)
136058 && nLimit>0 && p->nSelectRow > sqlite3LogEst(x: (u64)nLimit)
136059 ){
136060 p->nSelectRow = sqlite3LogEst(x: (u64)nLimit);
136061 }
136062 if( addr ){
136063 sqlite3VdbeJumpHere(p: v, addr);
136064 }
136065 break;
136066 }
136067 case TK_EXCEPT:
136068 case TK_UNION: {
136069 int unionTab; /* Cursor number of the temp table holding result */
136070 u8 op = 0; /* One of the SRT_ operations to apply to self */
136071 int priorOp; /* The SRT_ operation to apply to prior selects */
136072 Expr *pLimit; /* Saved values of p->nLimit */
136073 int addr;
136074 SelectDest uniondest;
136075
136076 testcase( p->op==TK_EXCEPT );
136077 testcase( p->op==TK_UNION );
136078 priorOp = SRT_Union;
136079 if( dest.eDest==priorOp ){
136080 /* We can reuse a temporary table generated by a SELECT to our
136081 ** right.
136082 */
136083 assert( p->pLimit==0 ); /* Not allowed on leftward elements */
136084 unionTab = dest.iSDParm;
136085 }else{
136086 /* We will need to create our own temporary table to hold the
136087 ** intermediate results.
136088 */
136089 unionTab = pParse->nTab++;
136090 assert( p->pOrderBy==0 );
136091 addr = sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: unionTab, p2: 0);
136092 assert( p->addrOpenEphm[0] == -1 );
136093 p->addrOpenEphm[0] = addr;
136094 findRightmost(p)->selFlags |= SF_UsesEphemeral;
136095 assert( p->pEList );
136096 }
136097
136098
136099 /* Code the SELECT statements to our left
136100 */
136101 assert( !pPrior->pOrderBy );
136102 sqlite3SelectDestInit(pDest: &uniondest, eDest: priorOp, iParm: unionTab);
136103 SELECTTRACE(1, pParse, p, ("multiSelect EXCEPT/UNION left...\n"));
136104 rc = sqlite3Select(pParse, pPrior, &uniondest);
136105 if( rc ){
136106 goto multi_select_end;
136107 }
136108
136109 /* Code the current SELECT statement
136110 */
136111 if( p->op==TK_EXCEPT ){
136112 op = SRT_Except;
136113 }else{
136114 assert( p->op==TK_UNION );
136115 op = SRT_Union;
136116 }
136117 p->pPrior = 0;
136118 pLimit = p->pLimit;
136119 p->pLimit = 0;
136120 uniondest.eDest = op;
136121 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
136122 sqlite3SelectOpName(p->op)));
136123 SELECTTRACE(1, pParse, p, ("multiSelect EXCEPT/UNION right...\n"));
136124 rc = sqlite3Select(pParse, p, &uniondest);
136125 testcase( rc!=SQLITE_OK );
136126 assert( p->pOrderBy==0 );
136127 pDelete = p->pPrior;
136128 p->pPrior = pPrior;
136129 p->pOrderBy = 0;
136130 if( p->op==TK_UNION ){
136131 p->nSelectRow = sqlite3LogEstAdd(a: p->nSelectRow, b: pPrior->nSelectRow);
136132 }
136133 sqlite3ExprDelete(db, p: p->pLimit);
136134 p->pLimit = pLimit;
136135 p->iLimit = 0;
136136 p->iOffset = 0;
136137
136138 /* Convert the data in the temporary table into whatever form
136139 ** it is that we currently need.
136140 */
136141 assert( unionTab==dest.iSDParm || dest.eDest!=priorOp );
136142 assert( p->pEList || db->mallocFailed );
136143 if( dest.eDest!=priorOp && db->mallocFailed==0 ){
136144 int iCont, iBreak, iStart;
136145 iBreak = sqlite3VdbeMakeLabel(pParse);
136146 iCont = sqlite3VdbeMakeLabel(pParse);
136147 computeLimitRegisters(pParse, p, iBreak);
136148 sqlite3VdbeAddOp2(p: v, OP_Rewind, p1: unionTab, p2: iBreak); VdbeCoverage(v);
136149 iStart = sqlite3VdbeCurrentAddr(p: v);
136150 selectInnerLoop(pParse, p, srcTab: unionTab,
136151 pSort: 0, pDistinct: 0, pDest: &dest, iContinue: iCont, iBreak);
136152 sqlite3VdbeResolveLabel(v, x: iCont);
136153 sqlite3VdbeAddOp2(p: v, OP_Next, p1: unionTab, p2: iStart); VdbeCoverage(v);
136154 sqlite3VdbeResolveLabel(v, x: iBreak);
136155 sqlite3VdbeAddOp2(p: v, OP_Close, p1: unionTab, p2: 0);
136156 }
136157 break;
136158 }
136159 default: assert( p->op==TK_INTERSECT ); {
136160 int tab1, tab2;
136161 int iCont, iBreak, iStart;
136162 Expr *pLimit;
136163 int addr;
136164 SelectDest intersectdest;
136165 int r1;
136166
136167 /* INTERSECT is different from the others since it requires
136168 ** two temporary tables. Hence it has its own case. Begin
136169 ** by allocating the tables we will need.
136170 */
136171 tab1 = pParse->nTab++;
136172 tab2 = pParse->nTab++;
136173 assert( p->pOrderBy==0 );
136174
136175 addr = sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: tab1, p2: 0);
136176 assert( p->addrOpenEphm[0] == -1 );
136177 p->addrOpenEphm[0] = addr;
136178 findRightmost(p)->selFlags |= SF_UsesEphemeral;
136179 assert( p->pEList );
136180
136181 /* Code the SELECTs to our left into temporary table "tab1".
136182 */
136183 sqlite3SelectDestInit(pDest: &intersectdest, SRT_Union, iParm: tab1);
136184 SELECTTRACE(1, pParse, p, ("multiSelect INTERSECT left...\n"));
136185 rc = sqlite3Select(pParse, pPrior, &intersectdest);
136186 if( rc ){
136187 goto multi_select_end;
136188 }
136189
136190 /* Code the current SELECT into temporary table "tab2"
136191 */
136192 addr = sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: tab2, p2: 0);
136193 assert( p->addrOpenEphm[1] == -1 );
136194 p->addrOpenEphm[1] = addr;
136195 p->pPrior = 0;
136196 pLimit = p->pLimit;
136197 p->pLimit = 0;
136198 intersectdest.iSDParm = tab2;
136199 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
136200 sqlite3SelectOpName(p->op)));
136201 SELECTTRACE(1, pParse, p, ("multiSelect INTERSECT right...\n"));
136202 rc = sqlite3Select(pParse, p, &intersectdest);
136203 testcase( rc!=SQLITE_OK );
136204 pDelete = p->pPrior;
136205 p->pPrior = pPrior;
136206 if( p->nSelectRow>pPrior->nSelectRow ){
136207 p->nSelectRow = pPrior->nSelectRow;
136208 }
136209 sqlite3ExprDelete(db, p: p->pLimit);
136210 p->pLimit = pLimit;
136211
136212 /* Generate code to take the intersection of the two temporary
136213 ** tables.
136214 */
136215 if( rc ) break;
136216 assert( p->pEList );
136217 iBreak = sqlite3VdbeMakeLabel(pParse);
136218 iCont = sqlite3VdbeMakeLabel(pParse);
136219 computeLimitRegisters(pParse, p, iBreak);
136220 sqlite3VdbeAddOp2(p: v, OP_Rewind, p1: tab1, p2: iBreak); VdbeCoverage(v);
136221 r1 = sqlite3GetTempReg(pParse);
136222 iStart = sqlite3VdbeAddOp2(p: v, OP_RowData, p1: tab1, p2: r1);
136223 sqlite3VdbeAddOp4Int(p: v, OP_NotFound, p1: tab2, p2: iCont, p3: r1, p4: 0);
136224 VdbeCoverage(v);
136225 sqlite3ReleaseTempReg(pParse, iReg: r1);
136226 selectInnerLoop(pParse, p, srcTab: tab1,
136227 pSort: 0, pDistinct: 0, pDest: &dest, iContinue: iCont, iBreak);
136228 sqlite3VdbeResolveLabel(v, x: iCont);
136229 sqlite3VdbeAddOp2(p: v, OP_Next, p1: tab1, p2: iStart); VdbeCoverage(v);
136230 sqlite3VdbeResolveLabel(v, x: iBreak);
136231 sqlite3VdbeAddOp2(p: v, OP_Close, p1: tab2, p2: 0);
136232 sqlite3VdbeAddOp2(p: v, OP_Close, p1: tab1, p2: 0);
136233 break;
136234 }
136235 }
136236
136237 #ifndef SQLITE_OMIT_EXPLAIN
136238 if( p->pNext==0 ){
136239 ExplainQueryPlanPop(pParse);
136240 }
136241 #endif
136242 }
136243 if( pParse->nErr ) goto multi_select_end;
136244
136245 /* Compute collating sequences used by
136246 ** temporary tables needed to implement the compound select.
136247 ** Attach the KeyInfo structure to all temporary tables.
136248 **
136249 ** This section is run by the right-most SELECT statement only.
136250 ** SELECT statements to the left always skip this part. The right-most
136251 ** SELECT might also skip this part if it has no ORDER BY clause and
136252 ** no temp tables are required.
136253 */
136254 if( p->selFlags & SF_UsesEphemeral ){
136255 int i; /* Loop counter */
136256 KeyInfo *pKeyInfo; /* Collating sequence for the result set */
136257 Select *pLoop; /* For looping through SELECT statements */
136258 CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */
136259 int nCol; /* Number of columns in result set */
136260
136261 assert( p->pNext==0 );
136262 assert( p->pEList!=0 );
136263 nCol = p->pEList->nExpr;
136264 pKeyInfo = sqlite3KeyInfoAlloc(db, N: nCol, X: 1);
136265 if( !pKeyInfo ){
136266 rc = SQLITE_NOMEM_BKPT;
136267 goto multi_select_end;
136268 }
136269 for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
136270 *apColl = multiSelectCollSeq(pParse, p, iCol: i);
136271 if( 0==*apColl ){
136272 *apColl = db->pDfltColl;
136273 }
136274 }
136275
136276 for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
136277 for(i=0; i<2; i++){
136278 int addr = pLoop->addrOpenEphm[i];
136279 if( addr<0 ){
136280 /* If [0] is unused then [1] is also unused. So we can
136281 ** always safely abort as soon as the first unused slot is found */
136282 assert( pLoop->addrOpenEphm[1]<0 );
136283 break;
136284 }
136285 sqlite3VdbeChangeP2(p: v, addr, val: nCol);
136286 sqlite3VdbeChangeP4(p: v, addr, zP4: (char*)sqlite3KeyInfoRef(p: pKeyInfo),
136287 P4_KEYINFO);
136288 pLoop->addrOpenEphm[i] = -1;
136289 }
136290 }
136291 sqlite3KeyInfoUnref(p: pKeyInfo);
136292 }
136293
136294multi_select_end:
136295 pDest->iSdst = dest.iSdst;
136296 pDest->nSdst = dest.nSdst;
136297 if( pDelete ){
136298 sqlite3ParserAddCleanup(pParse,
136299 xCleanup: (void(*)(sqlite3*,void*))sqlite3SelectDelete,
136300 pPtr: pDelete);
136301 }
136302 return rc;
136303}
136304#endif /* SQLITE_OMIT_COMPOUND_SELECT */
136305
136306/*
136307** Error message for when two or more terms of a compound select have different
136308** size result sets.
136309*/
136310SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p){
136311 if( p->selFlags & SF_Values ){
136312 sqlite3ErrorMsg(pParse, zFormat: "all VALUES must have the same number of terms");
136313 }else{
136314 sqlite3ErrorMsg(pParse, zFormat: "SELECTs to the left and right of %s"
136315 " do not have the same number of result columns",
136316 sqlite3SelectOpName(id: p->op));
136317 }
136318}
136319
136320/*
136321** Code an output subroutine for a coroutine implementation of a
136322** SELECT statment.
136323**
136324** The data to be output is contained in pIn->iSdst. There are
136325** pIn->nSdst columns to be output. pDest is where the output should
136326** be sent.
136327**
136328** regReturn is the number of the register holding the subroutine
136329** return address.
136330**
136331** If regPrev>0 then it is the first register in a vector that
136332** records the previous output. mem[regPrev] is a flag that is false
136333** if there has been no previous output. If regPrev>0 then code is
136334** generated to suppress duplicates. pKeyInfo is used for comparing
136335** keys.
136336**
136337** If the LIMIT found in p->iLimit is reached, jump immediately to
136338** iBreak.
136339*/
136340static int generateOutputSubroutine(
136341 Parse *pParse, /* Parsing context */
136342 Select *p, /* The SELECT statement */
136343 SelectDest *pIn, /* Coroutine supplying data */
136344 SelectDest *pDest, /* Where to send the data */
136345 int regReturn, /* The return address register */
136346 int regPrev, /* Previous result register. No uniqueness if 0 */
136347 KeyInfo *pKeyInfo, /* For comparing with previous entry */
136348 int iBreak /* Jump here if we hit the LIMIT */
136349){
136350 Vdbe *v = pParse->pVdbe;
136351 int iContinue;
136352 int addr;
136353
136354 addr = sqlite3VdbeCurrentAddr(p: v);
136355 iContinue = sqlite3VdbeMakeLabel(pParse);
136356
136357 /* Suppress duplicates for UNION, EXCEPT, and INTERSECT
136358 */
136359 if( regPrev ){
136360 int addr1, addr2;
136361 addr1 = sqlite3VdbeAddOp1(p: v, OP_IfNot, p1: regPrev); VdbeCoverage(v);
136362 addr2 = sqlite3VdbeAddOp4(p: v, OP_Compare, p1: pIn->iSdst, p2: regPrev+1, p3: pIn->nSdst,
136363 zP4: (char*)sqlite3KeyInfoRef(p: pKeyInfo), P4_KEYINFO);
136364 sqlite3VdbeAddOp3(p: v, OP_Jump, p1: addr2+2, p2: iContinue, p3: addr2+2); VdbeCoverage(v);
136365 sqlite3VdbeJumpHere(p: v, addr: addr1);
136366 sqlite3VdbeAddOp3(p: v, OP_Copy, p1: pIn->iSdst, p2: regPrev+1, p3: pIn->nSdst-1);
136367 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 1, p2: regPrev);
136368 }
136369 if( pParse->db->mallocFailed ) return 0;
136370
136371 /* Suppress the first OFFSET entries if there is an OFFSET clause
136372 */
136373 codeOffset(v, iOffset: p->iOffset, iContinue);
136374
136375 assert( pDest->eDest!=SRT_Exists );
136376 assert( pDest->eDest!=SRT_Table );
136377 switch( pDest->eDest ){
136378 /* Store the result as data using a unique key.
136379 */
136380 case SRT_EphemTab: {
136381 int r1 = sqlite3GetTempReg(pParse);
136382 int r2 = sqlite3GetTempReg(pParse);
136383 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: pIn->iSdst, p2: pIn->nSdst, p3: r1);
136384 sqlite3VdbeAddOp2(p: v, OP_NewRowid, p1: pDest->iSDParm, p2: r2);
136385 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: pDest->iSDParm, p2: r1, p3: r2);
136386 sqlite3VdbeChangeP5(p: v, OPFLAG_APPEND);
136387 sqlite3ReleaseTempReg(pParse, iReg: r2);
136388 sqlite3ReleaseTempReg(pParse, iReg: r1);
136389 break;
136390 }
136391
136392#ifndef SQLITE_OMIT_SUBQUERY
136393 /* If we are creating a set for an "expr IN (SELECT ...)".
136394 */
136395 case SRT_Set: {
136396 int r1;
136397 testcase( pIn->nSdst>1 );
136398 r1 = sqlite3GetTempReg(pParse);
136399 sqlite3VdbeAddOp4(p: v, OP_MakeRecord, p1: pIn->iSdst, p2: pIn->nSdst,
136400 p3: r1, zP4: pDest->zAffSdst, p4type: pIn->nSdst);
136401 sqlite3VdbeAddOp4Int(p: v, OP_IdxInsert, p1: pDest->iSDParm, p2: r1,
136402 p3: pIn->iSdst, p4: pIn->nSdst);
136403 sqlite3ReleaseTempReg(pParse, iReg: r1);
136404 break;
136405 }
136406
136407 /* If this is a scalar select that is part of an expression, then
136408 ** store the results in the appropriate memory cell and break out
136409 ** of the scan loop. Note that the select might return multiple columns
136410 ** if it is the RHS of a row-value IN operator.
136411 */
136412 case SRT_Mem: {
136413 testcase( pIn->nSdst>1 );
136414 sqlite3ExprCodeMove(pParse, iFrom: pIn->iSdst, iTo: pDest->iSDParm, nReg: pIn->nSdst);
136415 /* The LIMIT clause will jump out of the loop for us */
136416 break;
136417 }
136418#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
136419
136420 /* The results are stored in a sequence of registers
136421 ** starting at pDest->iSdst. Then the co-routine yields.
136422 */
136423 case SRT_Coroutine: {
136424 if( pDest->iSdst==0 ){
136425 pDest->iSdst = sqlite3GetTempRange(pParse, nReg: pIn->nSdst);
136426 pDest->nSdst = pIn->nSdst;
136427 }
136428 sqlite3ExprCodeMove(pParse, iFrom: pIn->iSdst, iTo: pDest->iSdst, nReg: pIn->nSdst);
136429 sqlite3VdbeAddOp1(p: v, OP_Yield, p1: pDest->iSDParm);
136430 break;
136431 }
136432
136433 /* If none of the above, then the result destination must be
136434 ** SRT_Output. This routine is never called with any other
136435 ** destination other than the ones handled above or SRT_Output.
136436 **
136437 ** For SRT_Output, results are stored in a sequence of registers.
136438 ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
136439 ** return the next row of result.
136440 */
136441 default: {
136442 assert( pDest->eDest==SRT_Output );
136443 sqlite3VdbeAddOp2(p: v, OP_ResultRow, p1: pIn->iSdst, p2: pIn->nSdst);
136444 break;
136445 }
136446 }
136447
136448 /* Jump to the end of the loop if the LIMIT is reached.
136449 */
136450 if( p->iLimit ){
136451 sqlite3VdbeAddOp2(p: v, OP_DecrJumpZero, p1: p->iLimit, p2: iBreak); VdbeCoverage(v);
136452 }
136453
136454 /* Generate the subroutine return
136455 */
136456 sqlite3VdbeResolveLabel(v, x: iContinue);
136457 sqlite3VdbeAddOp1(p: v, OP_Return, p1: regReturn);
136458
136459 return addr;
136460}
136461
136462/*
136463** Alternative compound select code generator for cases when there
136464** is an ORDER BY clause.
136465**
136466** We assume a query of the following form:
136467**
136468** <selectA> <operator> <selectB> ORDER BY <orderbylist>
136469**
136470** <operator> is one of UNION ALL, UNION, EXCEPT, or INTERSECT. The idea
136471** is to code both <selectA> and <selectB> with the ORDER BY clause as
136472** co-routines. Then run the co-routines in parallel and merge the results
136473** into the output. In addition to the two coroutines (called selectA and
136474** selectB) there are 7 subroutines:
136475**
136476** outA: Move the output of the selectA coroutine into the output
136477** of the compound query.
136478**
136479** outB: Move the output of the selectB coroutine into the output
136480** of the compound query. (Only generated for UNION and
136481** UNION ALL. EXCEPT and INSERTSECT never output a row that
136482** appears only in B.)
136483**
136484** AltB: Called when there is data from both coroutines and A<B.
136485**
136486** AeqB: Called when there is data from both coroutines and A==B.
136487**
136488** AgtB: Called when there is data from both coroutines and A>B.
136489**
136490** EofA: Called when data is exhausted from selectA.
136491**
136492** EofB: Called when data is exhausted from selectB.
136493**
136494** The implementation of the latter five subroutines depend on which
136495** <operator> is used:
136496**
136497**
136498** UNION ALL UNION EXCEPT INTERSECT
136499** ------------- ----------------- -------------- -----------------
136500** AltB: outA, nextA outA, nextA outA, nextA nextA
136501**
136502** AeqB: outA, nextA nextA nextA outA, nextA
136503**
136504** AgtB: outB, nextB outB, nextB nextB nextB
136505**
136506** EofA: outB, nextB outB, nextB halt halt
136507**
136508** EofB: outA, nextA outA, nextA outA, nextA halt
136509**
136510** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA
136511** causes an immediate jump to EofA and an EOF on B following nextB causes
136512** an immediate jump to EofB. Within EofA and EofB, and EOF on entry or
136513** following nextX causes a jump to the end of the select processing.
136514**
136515** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled
136516** within the output subroutine. The regPrev register set holds the previously
136517** output value. A comparison is made against this value and the output
136518** is skipped if the next results would be the same as the previous.
136519**
136520** The implementation plan is to implement the two coroutines and seven
136521** subroutines first, then put the control logic at the bottom. Like this:
136522**
136523** goto Init
136524** coA: coroutine for left query (A)
136525** coB: coroutine for right query (B)
136526** outA: output one row of A
136527** outB: output one row of B (UNION and UNION ALL only)
136528** EofA: ...
136529** EofB: ...
136530** AltB: ...
136531** AeqB: ...
136532** AgtB: ...
136533** Init: initialize coroutine registers
136534** yield coA
136535** if eof(A) goto EofA
136536** yield coB
136537** if eof(B) goto EofB
136538** Cmpr: Compare A, B
136539** Jump AltB, AeqB, AgtB
136540** End: ...
136541**
136542** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
136543** actually called using Gosub and they do not Return. EofA and EofB loop
136544** until all data is exhausted then jump to the "end" labe. AltB, AeqB,
136545** and AgtB jump to either L2 or to one of EofA or EofB.
136546*/
136547#ifndef SQLITE_OMIT_COMPOUND_SELECT
136548static int multiSelectOrderBy(
136549 Parse *pParse, /* Parsing context */
136550 Select *p, /* The right-most of SELECTs to be coded */
136551 SelectDest *pDest /* What to do with query results */
136552){
136553 int i, j; /* Loop counters */
136554 Select *pPrior; /* Another SELECT immediately to our left */
136555 Vdbe *v; /* Generate code to this VDBE */
136556 SelectDest destA; /* Destination for coroutine A */
136557 SelectDest destB; /* Destination for coroutine B */
136558 int regAddrA; /* Address register for select-A coroutine */
136559 int regAddrB; /* Address register for select-B coroutine */
136560 int addrSelectA; /* Address of the select-A coroutine */
136561 int addrSelectB; /* Address of the select-B coroutine */
136562 int regOutA; /* Address register for the output-A subroutine */
136563 int regOutB; /* Address register for the output-B subroutine */
136564 int addrOutA; /* Address of the output-A subroutine */
136565 int addrOutB = 0; /* Address of the output-B subroutine */
136566 int addrEofA; /* Address of the select-A-exhausted subroutine */
136567 int addrEofA_noB; /* Alternate addrEofA if B is uninitialized */
136568 int addrEofB; /* Address of the select-B-exhausted subroutine */
136569 int addrAltB; /* Address of the A<B subroutine */
136570 int addrAeqB; /* Address of the A==B subroutine */
136571 int addrAgtB; /* Address of the A>B subroutine */
136572 int regLimitA; /* Limit register for select-A */
136573 int regLimitB; /* Limit register for select-A */
136574 int regPrev; /* A range of registers to hold previous output */
136575 int savedLimit; /* Saved value of p->iLimit */
136576 int savedOffset; /* Saved value of p->iOffset */
136577 int labelCmpr; /* Label for the start of the merge algorithm */
136578 int labelEnd; /* Label for the end of the overall SELECT stmt */
136579 int addr1; /* Jump instructions that get retargetted */
136580 int op; /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
136581 KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */
136582 KeyInfo *pKeyMerge; /* Comparison information for merging rows */
136583 sqlite3 *db; /* Database connection */
136584 ExprList *pOrderBy; /* The ORDER BY clause */
136585 int nOrderBy; /* Number of terms in the ORDER BY clause */
136586 u32 *aPermute; /* Mapping from ORDER BY terms to result set columns */
136587
136588 assert( p->pOrderBy!=0 );
136589 assert( pKeyDup==0 ); /* "Managed" code needs this. Ticket #3382. */
136590 db = pParse->db;
136591 v = pParse->pVdbe;
136592 assert( v!=0 ); /* Already thrown the error if VDBE alloc failed */
136593 labelEnd = sqlite3VdbeMakeLabel(pParse);
136594 labelCmpr = sqlite3VdbeMakeLabel(pParse);
136595
136596
136597 /* Patch up the ORDER BY clause
136598 */
136599 op = p->op;
136600 pPrior = p->pPrior;
136601 assert( pPrior->pOrderBy==0 );
136602 pOrderBy = p->pOrderBy;
136603 assert( pOrderBy );
136604 nOrderBy = pOrderBy->nExpr;
136605
136606 /* For operators other than UNION ALL we have to make sure that
136607 ** the ORDER BY clause covers every term of the result set. Add
136608 ** terms to the ORDER BY clause as necessary.
136609 */
136610 if( op!=TK_ALL ){
136611 for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
136612 struct ExprList_item *pItem;
136613 for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
136614 assert( pItem!=0 );
136615 assert( pItem->u.x.iOrderByCol>0 );
136616 if( pItem->u.x.iOrderByCol==i ) break;
136617 }
136618 if( j==nOrderBy ){
136619 Expr *pNew = sqlite3Expr(db, TK_INTEGER, zToken: 0);
136620 if( pNew==0 ) return SQLITE_NOMEM_BKPT;
136621 pNew->flags |= EP_IntValue;
136622 pNew->u.iValue = i;
136623 p->pOrderBy = pOrderBy = sqlite3ExprListAppend(pParse, pList: pOrderBy, pExpr: pNew);
136624 if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
136625 }
136626 }
136627 }
136628
136629 /* Compute the comparison permutation and keyinfo that is used with
136630 ** the permutation used to determine if the next
136631 ** row of results comes from selectA or selectB. Also add explicit
136632 ** collations to the ORDER BY clause terms so that when the subqueries
136633 ** to the right and the left are evaluated, they use the correct
136634 ** collation.
136635 */
136636 aPermute = sqlite3DbMallocRawNN(db, n: sizeof(u32)*(nOrderBy + 1));
136637 if( aPermute ){
136638 struct ExprList_item *pItem;
136639 aPermute[0] = nOrderBy;
136640 for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){
136641 assert( pItem!=0 );
136642 assert( pItem->u.x.iOrderByCol>0 );
136643 assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
136644 aPermute[i] = pItem->u.x.iOrderByCol - 1;
136645 }
136646 pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, nExtra: 1);
136647 }else{
136648 pKeyMerge = 0;
136649 }
136650
136651 /* Reattach the ORDER BY clause to the query.
136652 */
136653 p->pOrderBy = pOrderBy;
136654 pPrior->pOrderBy = sqlite3ExprListDup(db: pParse->db, p: pOrderBy, flags: 0);
136655
136656 /* Allocate a range of temporary registers and the KeyInfo needed
136657 ** for the logic that removes duplicate result rows when the
136658 ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
136659 */
136660 if( op==TK_ALL ){
136661 regPrev = 0;
136662 }else{
136663 int nExpr = p->pEList->nExpr;
136664 assert( nOrderBy>=nExpr || db->mallocFailed );
136665 regPrev = pParse->nMem+1;
136666 pParse->nMem += nExpr+1;
136667 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: regPrev);
136668 pKeyDup = sqlite3KeyInfoAlloc(db, N: nExpr, X: 1);
136669 if( pKeyDup ){
136670 assert( sqlite3KeyInfoIsWriteable(pKeyDup) );
136671 for(i=0; i<nExpr; i++){
136672 pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, iCol: i);
136673 pKeyDup->aSortFlags[i] = 0;
136674 }
136675 }
136676 }
136677
136678 /* Separate the left and the right query from one another
136679 */
136680 p->pPrior = 0;
136681 pPrior->pNext = 0;
136682 sqlite3ResolveOrderGroupBy(pParse, pSelect: p, pOrderBy: p->pOrderBy, zType: "ORDER");
136683 if( pPrior->pPrior==0 ){
136684 sqlite3ResolveOrderGroupBy(pParse, pSelect: pPrior, pOrderBy: pPrior->pOrderBy, zType: "ORDER");
136685 }
136686
136687 /* Compute the limit registers */
136688 computeLimitRegisters(pParse, p, iBreak: labelEnd);
136689 if( p->iLimit && op==TK_ALL ){
136690 regLimitA = ++pParse->nMem;
136691 regLimitB = ++pParse->nMem;
136692 sqlite3VdbeAddOp2(p: v, OP_Copy, p1: p->iOffset ? p->iOffset+1 : p->iLimit,
136693 p2: regLimitA);
136694 sqlite3VdbeAddOp2(p: v, OP_Copy, p1: regLimitA, p2: regLimitB);
136695 }else{
136696 regLimitA = regLimitB = 0;
136697 }
136698 sqlite3ExprDelete(db, p: p->pLimit);
136699 p->pLimit = 0;
136700
136701 regAddrA = ++pParse->nMem;
136702 regAddrB = ++pParse->nMem;
136703 regOutA = ++pParse->nMem;
136704 regOutB = ++pParse->nMem;
136705 sqlite3SelectDestInit(pDest: &destA, SRT_Coroutine, iParm: regAddrA);
136706 sqlite3SelectDestInit(pDest: &destB, SRT_Coroutine, iParm: regAddrB);
136707
136708 ExplainQueryPlan((pParse, 1, "MERGE (%s)", sqlite3SelectOpName(p->op)));
136709
136710 /* Generate a coroutine to evaluate the SELECT statement to the
136711 ** left of the compound operator - the "A" select.
136712 */
136713 addrSelectA = sqlite3VdbeCurrentAddr(p: v) + 1;
136714 addr1 = sqlite3VdbeAddOp3(p: v, OP_InitCoroutine, p1: regAddrA, p2: 0, p3: addrSelectA);
136715 VdbeComment((v, "left SELECT"));
136716 pPrior->iLimit = regLimitA;
136717 ExplainQueryPlan((pParse, 1, "LEFT"));
136718 sqlite3Select(pParse, pPrior, &destA);
136719 sqlite3VdbeEndCoroutine(v, regYield: regAddrA);
136720 sqlite3VdbeJumpHere(p: v, addr: addr1);
136721
136722 /* Generate a coroutine to evaluate the SELECT statement on
136723 ** the right - the "B" select
136724 */
136725 addrSelectB = sqlite3VdbeCurrentAddr(p: v) + 1;
136726 addr1 = sqlite3VdbeAddOp3(p: v, OP_InitCoroutine, p1: regAddrB, p2: 0, p3: addrSelectB);
136727 VdbeComment((v, "right SELECT"));
136728 savedLimit = p->iLimit;
136729 savedOffset = p->iOffset;
136730 p->iLimit = regLimitB;
136731 p->iOffset = 0;
136732 ExplainQueryPlan((pParse, 1, "RIGHT"));
136733 sqlite3Select(pParse, p, &destB);
136734 p->iLimit = savedLimit;
136735 p->iOffset = savedOffset;
136736 sqlite3VdbeEndCoroutine(v, regYield: regAddrB);
136737
136738 /* Generate a subroutine that outputs the current row of the A
136739 ** select as the next output row of the compound select.
136740 */
136741 VdbeNoopComment((v, "Output routine for A"));
136742 addrOutA = generateOutputSubroutine(pParse,
136743 p, pIn: &destA, pDest, regReturn: regOutA,
136744 regPrev, pKeyInfo: pKeyDup, iBreak: labelEnd);
136745
136746 /* Generate a subroutine that outputs the current row of the B
136747 ** select as the next output row of the compound select.
136748 */
136749 if( op==TK_ALL || op==TK_UNION ){
136750 VdbeNoopComment((v, "Output routine for B"));
136751 addrOutB = generateOutputSubroutine(pParse,
136752 p, pIn: &destB, pDest, regReturn: regOutB,
136753 regPrev, pKeyInfo: pKeyDup, iBreak: labelEnd);
136754 }
136755 sqlite3KeyInfoUnref(p: pKeyDup);
136756
136757 /* Generate a subroutine to run when the results from select A
136758 ** are exhausted and only data in select B remains.
136759 */
136760 if( op==TK_EXCEPT || op==TK_INTERSECT ){
136761 addrEofA_noB = addrEofA = labelEnd;
136762 }else{
136763 VdbeNoopComment((v, "eof-A subroutine"));
136764 addrEofA = sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: regOutB, p2: addrOutB);
136765 addrEofA_noB = sqlite3VdbeAddOp2(p: v, OP_Yield, p1: regAddrB, p2: labelEnd);
136766 VdbeCoverage(v);
136767 sqlite3VdbeGoto(p: v, iDest: addrEofA);
136768 p->nSelectRow = sqlite3LogEstAdd(a: p->nSelectRow, b: pPrior->nSelectRow);
136769 }
136770
136771 /* Generate a subroutine to run when the results from select B
136772 ** are exhausted and only data in select A remains.
136773 */
136774 if( op==TK_INTERSECT ){
136775 addrEofB = addrEofA;
136776 if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
136777 }else{
136778 VdbeNoopComment((v, "eof-B subroutine"));
136779 addrEofB = sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: regOutA, p2: addrOutA);
136780 sqlite3VdbeAddOp2(p: v, OP_Yield, p1: regAddrA, p2: labelEnd); VdbeCoverage(v);
136781 sqlite3VdbeGoto(p: v, iDest: addrEofB);
136782 }
136783
136784 /* Generate code to handle the case of A<B
136785 */
136786 VdbeNoopComment((v, "A-lt-B subroutine"));
136787 addrAltB = sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: regOutA, p2: addrOutA);
136788 sqlite3VdbeAddOp2(p: v, OP_Yield, p1: regAddrA, p2: addrEofA); VdbeCoverage(v);
136789 sqlite3VdbeGoto(p: v, iDest: labelCmpr);
136790
136791 /* Generate code to handle the case of A==B
136792 */
136793 if( op==TK_ALL ){
136794 addrAeqB = addrAltB;
136795 }else if( op==TK_INTERSECT ){
136796 addrAeqB = addrAltB;
136797 addrAltB++;
136798 }else{
136799 VdbeNoopComment((v, "A-eq-B subroutine"));
136800 addrAeqB =
136801 sqlite3VdbeAddOp2(p: v, OP_Yield, p1: regAddrA, p2: addrEofA); VdbeCoverage(v);
136802 sqlite3VdbeGoto(p: v, iDest: labelCmpr);
136803 }
136804
136805 /* Generate code to handle the case of A>B
136806 */
136807 VdbeNoopComment((v, "A-gt-B subroutine"));
136808 addrAgtB = sqlite3VdbeCurrentAddr(p: v);
136809 if( op==TK_ALL || op==TK_UNION ){
136810 sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: regOutB, p2: addrOutB);
136811 }
136812 sqlite3VdbeAddOp2(p: v, OP_Yield, p1: regAddrB, p2: addrEofB); VdbeCoverage(v);
136813 sqlite3VdbeGoto(p: v, iDest: labelCmpr);
136814
136815 /* This code runs once to initialize everything.
136816 */
136817 sqlite3VdbeJumpHere(p: v, addr: addr1);
136818 sqlite3VdbeAddOp2(p: v, OP_Yield, p1: regAddrA, p2: addrEofA_noB); VdbeCoverage(v);
136819 sqlite3VdbeAddOp2(p: v, OP_Yield, p1: regAddrB, p2: addrEofB); VdbeCoverage(v);
136820
136821 /* Implement the main merge loop
136822 */
136823 sqlite3VdbeResolveLabel(v, x: labelCmpr);
136824 sqlite3VdbeAddOp4(p: v, OP_Permutation, p1: 0, p2: 0, p3: 0, zP4: (char*)aPermute, P4_INTARRAY);
136825 sqlite3VdbeAddOp4(p: v, OP_Compare, p1: destA.iSdst, p2: destB.iSdst, p3: nOrderBy,
136826 zP4: (char*)pKeyMerge, P4_KEYINFO);
136827 sqlite3VdbeChangeP5(p: v, OPFLAG_PERMUTE);
136828 sqlite3VdbeAddOp3(p: v, OP_Jump, p1: addrAltB, p2: addrAeqB, p3: addrAgtB); VdbeCoverage(v);
136829
136830 /* Jump to the this point in order to terminate the query.
136831 */
136832 sqlite3VdbeResolveLabel(v, x: labelEnd);
136833
136834 /* Reassembly the compound query so that it will be freed correctly
136835 ** by the calling function */
136836 if( p->pPrior ){
136837 sqlite3SelectDelete(db, p: p->pPrior);
136838 }
136839 p->pPrior = pPrior;
136840 pPrior->pNext = p;
136841
136842 sqlite3ExprListDelete(db, pList: pPrior->pOrderBy);
136843 pPrior->pOrderBy = 0;
136844
136845 /*** TBD: Insert subroutine calls to close cursors on incomplete
136846 **** subqueries ****/
136847 ExplainQueryPlanPop(pParse);
136848 return pParse->nErr!=0;
136849}
136850#endif
136851
136852#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
136853
136854/* An instance of the SubstContext object describes an substitution edit
136855** to be performed on a parse tree.
136856**
136857** All references to columns in table iTable are to be replaced by corresponding
136858** expressions in pEList.
136859*/
136860typedef struct SubstContext {
136861 Parse *pParse; /* The parsing context */
136862 int iTable; /* Replace references to this table */
136863 int iNewTable; /* New table number */
136864 int isLeftJoin; /* Add TK_IF_NULL_ROW opcodes on each replacement */
136865 ExprList *pEList; /* Replacement expressions */
136866} SubstContext;
136867
136868/* Forward Declarations */
136869static void substExprList(SubstContext*, ExprList*);
136870static void substSelect(SubstContext*, Select*, int);
136871
136872/*
136873** Scan through the expression pExpr. Replace every reference to
136874** a column in table number iTable with a copy of the iColumn-th
136875** entry in pEList. (But leave references to the ROWID column
136876** unchanged.)
136877**
136878** This routine is part of the flattening procedure. A subquery
136879** whose result set is defined by pEList appears as entry in the
136880** FROM clause of a SELECT such that the VDBE cursor assigned to that
136881** FORM clause entry is iTable. This routine makes the necessary
136882** changes to pExpr so that it refers directly to the source table
136883** of the subquery rather the result set of the subquery.
136884*/
136885static Expr *substExpr(
136886 SubstContext *pSubst, /* Description of the substitution */
136887 Expr *pExpr /* Expr in which substitution occurs */
136888){
136889 if( pExpr==0 ) return 0;
136890 if( ExprHasProperty(pExpr, EP_FromJoin)
136891 && pExpr->iRightJoinTable==pSubst->iTable
136892 ){
136893 pExpr->iRightJoinTable = pSubst->iNewTable;
136894 }
136895 if( pExpr->op==TK_COLUMN
136896 && pExpr->iTable==pSubst->iTable
136897 && !ExprHasProperty(pExpr, EP_FixedCol)
136898 ){
136899#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
136900 if( pExpr->iColumn<0 ){
136901 pExpr->op = TK_NULL;
136902 }else
136903#endif
136904 {
136905 Expr *pNew;
136906 Expr *pCopy = pSubst->pEList->a[pExpr->iColumn].pExpr;
136907 Expr ifNullRow;
136908 assert( pSubst->pEList!=0 && pExpr->iColumn<pSubst->pEList->nExpr );
136909 assert( pExpr->pRight==0 );
136910 if( sqlite3ExprIsVector(pExpr: pCopy) ){
136911 sqlite3VectorErrorMsg(pParse: pSubst->pParse, pExpr: pCopy);
136912 }else{
136913 sqlite3 *db = pSubst->pParse->db;
136914 if( pSubst->isLeftJoin && pCopy->op!=TK_COLUMN ){
136915 memset(s: &ifNullRow, c: 0, n: sizeof(ifNullRow));
136916 ifNullRow.op = TK_IF_NULL_ROW;
136917 ifNullRow.pLeft = pCopy;
136918 ifNullRow.iTable = pSubst->iNewTable;
136919 ifNullRow.flags = EP_IfNullRow;
136920 pCopy = &ifNullRow;
136921 }
136922 testcase( ExprHasProperty(pCopy, EP_Subquery) );
136923 pNew = sqlite3ExprDup(db, p: pCopy, flags: 0);
136924 if( db->mallocFailed ){
136925 sqlite3ExprDelete(db, p: pNew);
136926 return pExpr;
136927 }
136928 if( pSubst->isLeftJoin ){
136929 ExprSetProperty(pNew, EP_CanBeNull);
136930 }
136931 if( ExprHasProperty(pExpr,EP_FromJoin) ){
136932 sqlite3SetJoinExpr(p: pNew, iTable: pExpr->iRightJoinTable);
136933 }
136934 sqlite3ExprDelete(db, p: pExpr);
136935 pExpr = pNew;
136936
136937 /* Ensure that the expression now has an implicit collation sequence,
136938 ** just as it did when it was a column of a view or sub-query. */
136939 if( pExpr->op!=TK_COLUMN && pExpr->op!=TK_COLLATE ){
136940 CollSeq *pColl = sqlite3ExprCollSeq(pParse: pSubst->pParse, pExpr);
136941 pExpr = sqlite3ExprAddCollateString(pParse: pSubst->pParse, pExpr,
136942 zC: (pColl ? pColl->zName : "BINARY")
136943 );
136944 }
136945 ExprClearProperty(pExpr, EP_Collate);
136946 }
136947 }
136948 }else{
136949 if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){
136950 pExpr->iTable = pSubst->iNewTable;
136951 }
136952 pExpr->pLeft = substExpr(pSubst, pExpr: pExpr->pLeft);
136953 pExpr->pRight = substExpr(pSubst, pExpr: pExpr->pRight);
136954 if( ExprUseXSelect(pExpr) ){
136955 substSelect(pSubst, pExpr->x.pSelect, 1);
136956 }else{
136957 substExprList(pSubst, pExpr->x.pList);
136958 }
136959#ifndef SQLITE_OMIT_WINDOWFUNC
136960 if( ExprHasProperty(pExpr, EP_WinFunc) ){
136961 Window *pWin = pExpr->y.pWin;
136962 pWin->pFilter = substExpr(pSubst, pExpr: pWin->pFilter);
136963 substExprList(pSubst, pWin->pPartition);
136964 substExprList(pSubst, pWin->pOrderBy);
136965 }
136966#endif
136967 }
136968 return pExpr;
136969}
136970static void substExprList(
136971 SubstContext *pSubst, /* Description of the substitution */
136972 ExprList *pList /* List to scan and in which to make substitutes */
136973){
136974 int i;
136975 if( pList==0 ) return;
136976 for(i=0; i<pList->nExpr; i++){
136977 pList->a[i].pExpr = substExpr(pSubst, pExpr: pList->a[i].pExpr);
136978 }
136979}
136980static void substSelect(
136981 SubstContext *pSubst, /* Description of the substitution */
136982 Select *p, /* SELECT statement in which to make substitutions */
136983 int doPrior /* Do substitutes on p->pPrior too */
136984){
136985 SrcList *pSrc;
136986 SrcItem *pItem;
136987 int i;
136988 if( !p ) return;
136989 do{
136990 substExprList(pSubst, pList: p->pEList);
136991 substExprList(pSubst, pList: p->pGroupBy);
136992 substExprList(pSubst, pList: p->pOrderBy);
136993 p->pHaving = substExpr(pSubst, pExpr: p->pHaving);
136994 p->pWhere = substExpr(pSubst, pExpr: p->pWhere);
136995 pSrc = p->pSrc;
136996 assert( pSrc!=0 );
136997 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
136998 substSelect(pSubst, p: pItem->pSelect, doPrior: 1);
136999 if( pItem->fg.isTabFunc ){
137000 substExprList(pSubst, pList: pItem->u1.pFuncArg);
137001 }
137002 }
137003 }while( doPrior && (p = p->pPrior)!=0 );
137004}
137005#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
137006
137007#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
137008/*
137009** pSelect is a SELECT statement and pSrcItem is one item in the FROM
137010** clause of that SELECT.
137011**
137012** This routine scans the entire SELECT statement and recomputes the
137013** pSrcItem->colUsed mask.
137014*/
137015static int recomputeColumnsUsedExpr(Walker *pWalker, Expr *pExpr){
137016 SrcItem *pItem;
137017 if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
137018 pItem = pWalker->u.pSrcItem;
137019 if( pItem->iCursor!=pExpr->iTable ) return WRC_Continue;
137020 if( pExpr->iColumn<0 ) return WRC_Continue;
137021 pItem->colUsed |= sqlite3ExprColUsed(pExpr);
137022 return WRC_Continue;
137023}
137024static void recomputeColumnsUsed(
137025 Select *pSelect, /* The complete SELECT statement */
137026 SrcItem *pSrcItem /* Which FROM clause item to recompute */
137027){
137028 Walker w;
137029 if( NEVER(pSrcItem->pTab==0) ) return;
137030 memset(s: &w, c: 0, n: sizeof(w));
137031 w.xExprCallback = recomputeColumnsUsedExpr;
137032 w.xSelectCallback = sqlite3SelectWalkNoop;
137033 w.u.pSrcItem = pSrcItem;
137034 pSrcItem->colUsed = 0;
137035 sqlite3WalkSelect(pWalker: &w, p: pSelect);
137036}
137037#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
137038
137039#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
137040/*
137041** Assign new cursor numbers to each of the items in pSrc. For each
137042** new cursor number assigned, set an entry in the aCsrMap[] array
137043** to map the old cursor number to the new:
137044**
137045** aCsrMap[iOld+1] = iNew;
137046**
137047** The array is guaranteed by the caller to be large enough for all
137048** existing cursor numbers in pSrc. aCsrMap[0] is the array size.
137049**
137050** If pSrc contains any sub-selects, call this routine recursively
137051** on the FROM clause of each such sub-select, with iExcept set to -1.
137052*/
137053static void srclistRenumberCursors(
137054 Parse *pParse, /* Parse context */
137055 int *aCsrMap, /* Array to store cursor mappings in */
137056 SrcList *pSrc, /* FROM clause to renumber */
137057 int iExcept /* FROM clause item to skip */
137058){
137059 int i;
137060 SrcItem *pItem;
137061 for(i=0, pItem=pSrc->a; i<pSrc->nSrc; i++, pItem++){
137062 if( i!=iExcept ){
137063 Select *p;
137064 assert( pItem->iCursor < aCsrMap[0] );
137065 if( !pItem->fg.isRecursive || aCsrMap[pItem->iCursor+1]==0 ){
137066 aCsrMap[pItem->iCursor+1] = pParse->nTab++;
137067 }
137068 pItem->iCursor = aCsrMap[pItem->iCursor+1];
137069 for(p=pItem->pSelect; p; p=p->pPrior){
137070 srclistRenumberCursors(pParse, aCsrMap, pSrc: p->pSrc, iExcept: -1);
137071 }
137072 }
137073 }
137074}
137075
137076/*
137077** *piCursor is a cursor number. Change it if it needs to be mapped.
137078*/
137079static void renumberCursorDoMapping(Walker *pWalker, int *piCursor){
137080 int *aCsrMap = pWalker->u.aiCol;
137081 int iCsr = *piCursor;
137082 if( iCsr < aCsrMap[0] && aCsrMap[iCsr+1]>0 ){
137083 *piCursor = aCsrMap[iCsr+1];
137084 }
137085}
137086
137087/*
137088** Expression walker callback used by renumberCursors() to update
137089** Expr objects to match newly assigned cursor numbers.
137090*/
137091static int renumberCursorsCb(Walker *pWalker, Expr *pExpr){
137092 int op = pExpr->op;
137093 if( op==TK_COLUMN || op==TK_IF_NULL_ROW ){
137094 renumberCursorDoMapping(pWalker, piCursor: &pExpr->iTable);
137095 }
137096 if( ExprHasProperty(pExpr, EP_FromJoin) ){
137097 renumberCursorDoMapping(pWalker, piCursor: &pExpr->iRightJoinTable);
137098 }
137099 return WRC_Continue;
137100}
137101
137102/*
137103** Assign a new cursor number to each cursor in the FROM clause (Select.pSrc)
137104** of the SELECT statement passed as the second argument, and to each
137105** cursor in the FROM clause of any FROM clause sub-selects, recursively.
137106** Except, do not assign a new cursor number to the iExcept'th element in
137107** the FROM clause of (*p). Update all expressions and other references
137108** to refer to the new cursor numbers.
137109**
137110** Argument aCsrMap is an array that may be used for temporary working
137111** space. Two guarantees are made by the caller:
137112**
137113** * the array is larger than the largest cursor number used within the
137114** select statement passed as an argument, and
137115**
137116** * the array entries for all cursor numbers that do *not* appear in
137117** FROM clauses of the select statement as described above are
137118** initialized to zero.
137119*/
137120static void renumberCursors(
137121 Parse *pParse, /* Parse context */
137122 Select *p, /* Select to renumber cursors within */
137123 int iExcept, /* FROM clause item to skip */
137124 int *aCsrMap /* Working space */
137125){
137126 Walker w;
137127 srclistRenumberCursors(pParse, aCsrMap, pSrc: p->pSrc, iExcept);
137128 memset(s: &w, c: 0, n: sizeof(w));
137129 w.u.aiCol = aCsrMap;
137130 w.xExprCallback = renumberCursorsCb;
137131 w.xSelectCallback = sqlite3SelectWalkNoop;
137132 sqlite3WalkSelect(pWalker: &w, p);
137133}
137134#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
137135
137136#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
137137/*
137138** This routine attempts to flatten subqueries as a performance optimization.
137139** This routine returns 1 if it makes changes and 0 if no flattening occurs.
137140**
137141** To understand the concept of flattening, consider the following
137142** query:
137143**
137144** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
137145**
137146** The default way of implementing this query is to execute the
137147** subquery first and store the results in a temporary table, then
137148** run the outer query on that temporary table. This requires two
137149** passes over the data. Furthermore, because the temporary table
137150** has no indices, the WHERE clause on the outer query cannot be
137151** optimized.
137152**
137153** This routine attempts to rewrite queries such as the above into
137154** a single flat select, like this:
137155**
137156** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
137157**
137158** The code generated for this simplification gives the same result
137159** but only has to scan the data once. And because indices might
137160** exist on the table t1, a complete scan of the data might be
137161** avoided.
137162**
137163** Flattening is subject to the following constraints:
137164**
137165** (**) We no longer attempt to flatten aggregate subqueries. Was:
137166** The subquery and the outer query cannot both be aggregates.
137167**
137168** (**) We no longer attempt to flatten aggregate subqueries. Was:
137169** (2) If the subquery is an aggregate then
137170** (2a) the outer query must not be a join and
137171** (2b) the outer query must not use subqueries
137172** other than the one FROM-clause subquery that is a candidate
137173** for flattening. (This is due to ticket [2f7170d73bf9abf80]
137174** from 2015-02-09.)
137175**
137176** (3) If the subquery is the right operand of a LEFT JOIN then
137177** (3a) the subquery may not be a join and
137178** (3b) the FROM clause of the subquery may not contain a virtual
137179** table and
137180** (3c) the outer query may not be an aggregate.
137181** (3d) the outer query may not be DISTINCT.
137182**
137183** (4) The subquery can not be DISTINCT.
137184**
137185** (**) At one point restrictions (4) and (5) defined a subset of DISTINCT
137186** sub-queries that were excluded from this optimization. Restriction
137187** (4) has since been expanded to exclude all DISTINCT subqueries.
137188**
137189** (**) We no longer attempt to flatten aggregate subqueries. Was:
137190** If the subquery is aggregate, the outer query may not be DISTINCT.
137191**
137192** (7) The subquery must have a FROM clause. TODO: For subqueries without
137193** A FROM clause, consider adding a FROM clause with the special
137194** table sqlite_once that consists of a single row containing a
137195** single NULL.
137196**
137197** (8) If the subquery uses LIMIT then the outer query may not be a join.
137198**
137199** (9) If the subquery uses LIMIT then the outer query may not be aggregate.
137200**
137201** (**) Restriction (10) was removed from the code on 2005-02-05 but we
137202** accidently carried the comment forward until 2014-09-15. Original
137203** constraint: "If the subquery is aggregate then the outer query
137204** may not use LIMIT."
137205**
137206** (11) The subquery and the outer query may not both have ORDER BY clauses.
137207**
137208** (**) Not implemented. Subsumed into restriction (3). Was previously
137209** a separate restriction deriving from ticket #350.
137210**
137211** (13) The subquery and outer query may not both use LIMIT.
137212**
137213** (14) The subquery may not use OFFSET.
137214**
137215** (15) If the outer query is part of a compound select, then the
137216** subquery may not use LIMIT.
137217** (See ticket #2339 and ticket [02a8e81d44]).
137218**
137219** (16) If the outer query is aggregate, then the subquery may not
137220** use ORDER BY. (Ticket #2942) This used to not matter
137221** until we introduced the group_concat() function.
137222**
137223** (17) If the subquery is a compound select, then
137224** (17a) all compound operators must be a UNION ALL, and
137225** (17b) no terms within the subquery compound may be aggregate
137226** or DISTINCT, and
137227** (17c) every term within the subquery compound must have a FROM clause
137228** (17d) the outer query may not be
137229** (17d1) aggregate, or
137230** (17d2) DISTINCT
137231** (17e) the subquery may not contain window functions, and
137232** (17f) the subquery must not be the RHS of a LEFT JOIN.
137233**
137234** The parent and sub-query may contain WHERE clauses. Subject to
137235** rules (11), (13) and (14), they may also contain ORDER BY,
137236** LIMIT and OFFSET clauses. The subquery cannot use any compound
137237** operator other than UNION ALL because all the other compound
137238** operators have an implied DISTINCT which is disallowed by
137239** restriction (4).
137240**
137241** Also, each component of the sub-query must return the same number
137242** of result columns. This is actually a requirement for any compound
137243** SELECT statement, but all the code here does is make sure that no
137244** such (illegal) sub-query is flattened. The caller will detect the
137245** syntax error and return a detailed message.
137246**
137247** (18) If the sub-query is a compound select, then all terms of the
137248** ORDER BY clause of the parent must be copies of a term returned
137249** by the parent query.
137250**
137251** (19) If the subquery uses LIMIT then the outer query may not
137252** have a WHERE clause.
137253**
137254** (20) If the sub-query is a compound select, then it must not use
137255** an ORDER BY clause. Ticket #3773. We could relax this constraint
137256** somewhat by saying that the terms of the ORDER BY clause must
137257** appear as unmodified result columns in the outer query. But we
137258** have other optimizations in mind to deal with that case.
137259**
137260** (21) If the subquery uses LIMIT then the outer query may not be
137261** DISTINCT. (See ticket [752e1646fc]).
137262**
137263** (22) The subquery may not be a recursive CTE.
137264**
137265** (23) If the outer query is a recursive CTE, then the sub-query may not be
137266** a compound query. This restriction is because transforming the
137267** parent to a compound query confuses the code that handles
137268** recursive queries in multiSelect().
137269**
137270** (**) We no longer attempt to flatten aggregate subqueries. Was:
137271** The subquery may not be an aggregate that uses the built-in min() or
137272** or max() functions. (Without this restriction, a query like:
137273** "SELECT x FROM (SELECT max(y), x FROM t1)" would not necessarily
137274** return the value X for which Y was maximal.)
137275**
137276** (25) If either the subquery or the parent query contains a window
137277** function in the select list or ORDER BY clause, flattening
137278** is not attempted.
137279**
137280**
137281** In this routine, the "p" parameter is a pointer to the outer query.
137282** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
137283** uses aggregates.
137284**
137285** If flattening is not attempted, this routine is a no-op and returns 0.
137286** If flattening is attempted this routine returns 1.
137287**
137288** All of the expression analysis must occur on both the outer query and
137289** the subquery before this routine runs.
137290*/
137291static int flattenSubquery(
137292 Parse *pParse, /* Parsing context */
137293 Select *p, /* The parent or outer SELECT statement */
137294 int iFrom, /* Index in p->pSrc->a[] of the inner subquery */
137295 int isAgg /* True if outer SELECT uses aggregate functions */
137296){
137297 const char *zSavedAuthContext = pParse->zAuthContext;
137298 Select *pParent; /* Current UNION ALL term of the other query */
137299 Select *pSub; /* The inner query or "subquery" */
137300 Select *pSub1; /* Pointer to the rightmost select in sub-query */
137301 SrcList *pSrc; /* The FROM clause of the outer query */
137302 SrcList *pSubSrc; /* The FROM clause of the subquery */
137303 int iParent; /* VDBE cursor number of the pSub result set temp table */
137304 int iNewParent = -1;/* Replacement table for iParent */
137305 int isLeftJoin = 0; /* True if pSub is the right side of a LEFT JOIN */
137306 int i; /* Loop counter */
137307 Expr *pWhere; /* The WHERE clause */
137308 SrcItem *pSubitem; /* The subquery */
137309 sqlite3 *db = pParse->db;
137310 Walker w; /* Walker to persist agginfo data */
137311 int *aCsrMap = 0;
137312
137313 /* Check to see if flattening is permitted. Return 0 if not.
137314 */
137315 assert( p!=0 );
137316 assert( p->pPrior==0 );
137317 if( OptimizationDisabled(db, SQLITE_QueryFlattener) ) return 0;
137318 pSrc = p->pSrc;
137319 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
137320 pSubitem = &pSrc->a[iFrom];
137321 iParent = pSubitem->iCursor;
137322 pSub = pSubitem->pSelect;
137323 assert( pSub!=0 );
137324
137325#ifndef SQLITE_OMIT_WINDOWFUNC
137326 if( p->pWin || pSub->pWin ) return 0; /* Restriction (25) */
137327#endif
137328
137329 pSubSrc = pSub->pSrc;
137330 assert( pSubSrc );
137331 /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
137332 ** not arbitrary expressions, we allowed some combining of LIMIT and OFFSET
137333 ** because they could be computed at compile-time. But when LIMIT and OFFSET
137334 ** became arbitrary expressions, we were forced to add restrictions (13)
137335 ** and (14). */
137336 if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */
137337 if( pSub->pLimit && pSub->pLimit->pRight ) return 0; /* Restriction (14) */
137338 if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
137339 return 0; /* Restriction (15) */
137340 }
137341 if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
137342 if( pSub->selFlags & SF_Distinct ) return 0; /* Restriction (4) */
137343 if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
137344 return 0; /* Restrictions (8)(9) */
137345 }
137346 if( p->pOrderBy && pSub->pOrderBy ){
137347 return 0; /* Restriction (11) */
137348 }
137349 if( isAgg && pSub->pOrderBy ) return 0; /* Restriction (16) */
137350 if( pSub->pLimit && p->pWhere ) return 0; /* Restriction (19) */
137351 if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
137352 return 0; /* Restriction (21) */
137353 }
137354 if( pSub->selFlags & (SF_Recursive) ){
137355 return 0; /* Restrictions (22) */
137356 }
137357
137358 /*
137359 ** If the subquery is the right operand of a LEFT JOIN, then the
137360 ** subquery may not be a join itself (3a). Example of why this is not
137361 ** allowed:
137362 **
137363 ** t1 LEFT OUTER JOIN (t2 JOIN t3)
137364 **
137365 ** If we flatten the above, we would get
137366 **
137367 ** (t1 LEFT OUTER JOIN t2) JOIN t3
137368 **
137369 ** which is not at all the same thing.
137370 **
137371 ** If the subquery is the right operand of a LEFT JOIN, then the outer
137372 ** query cannot be an aggregate. (3c) This is an artifact of the way
137373 ** aggregates are processed - there is no mechanism to determine if
137374 ** the LEFT JOIN table should be all-NULL.
137375 **
137376 ** See also tickets #306, #350, and #3300.
137377 */
137378 if( (pSubitem->fg.jointype & JT_OUTER)!=0 ){
137379 isLeftJoin = 1;
137380 if( pSubSrc->nSrc>1 /* (3a) */
137381 || isAgg /* (3b) */
137382 || IsVirtual(pSubSrc->a[0].pTab) /* (3c) */
137383 || (p->selFlags & SF_Distinct)!=0 /* (3d) */
137384 ){
137385 return 0;
137386 }
137387 }
137388#ifdef SQLITE_EXTRA_IFNULLROW
137389 else if( iFrom>0 && !isAgg ){
137390 /* Setting isLeftJoin to -1 causes OP_IfNullRow opcodes to be generated for
137391 ** every reference to any result column from subquery in a join, even
137392 ** though they are not necessary. This will stress-test the OP_IfNullRow
137393 ** opcode. */
137394 isLeftJoin = -1;
137395 }
137396#endif
137397
137398 /* Restriction (17): If the sub-query is a compound SELECT, then it must
137399 ** use only the UNION ALL operator. And none of the simple select queries
137400 ** that make up the compound SELECT are allowed to be aggregate or distinct
137401 ** queries.
137402 */
137403 if( pSub->pPrior ){
137404 if( pSub->pOrderBy ){
137405 return 0; /* Restriction (20) */
137406 }
137407 if( isAgg || (p->selFlags & SF_Distinct)!=0 || isLeftJoin>0 ){
137408 return 0; /* (17d1), (17d2), or (17f) */
137409 }
137410 for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
137411 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
137412 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
137413 assert( pSub->pSrc!=0 );
137414 assert( (pSub->selFlags & SF_Recursive)==0 );
137415 assert( pSub->pEList->nExpr==pSub1->pEList->nExpr );
137416 if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0 /* (17b) */
137417 || (pSub1->pPrior && pSub1->op!=TK_ALL) /* (17a) */
137418 || pSub1->pSrc->nSrc<1 /* (17c) */
137419#ifndef SQLITE_OMIT_WINDOWFUNC
137420 || pSub1->pWin /* (17e) */
137421#endif
137422 ){
137423 return 0;
137424 }
137425 testcase( pSub1->pSrc->nSrc>1 );
137426 }
137427
137428 /* Restriction (18). */
137429 if( p->pOrderBy ){
137430 int ii;
137431 for(ii=0; ii<p->pOrderBy->nExpr; ii++){
137432 if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
137433 }
137434 }
137435
137436 /* Restriction (23) */
137437 if( (p->selFlags & SF_Recursive) ) return 0;
137438
137439 if( pSrc->nSrc>1 ){
137440 if( pParse->nSelect>500 ) return 0;
137441 aCsrMap = sqlite3DbMallocZero(db, n: ((i64)pParse->nTab+1)*sizeof(int));
137442 if( aCsrMap ) aCsrMap[0] = pParse->nTab;
137443 }
137444 }
137445
137446 /***** If we reach this point, flattening is permitted. *****/
137447 SELECTTRACE(1,pParse,p,("flatten %u.%p from term %d\n",
137448 pSub->selId, pSub, iFrom));
137449
137450 /* Authorize the subquery */
137451 pParse->zAuthContext = pSubitem->zName;
137452 TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, zArg1: 0, zArg2: 0, zArg3: 0);
137453 testcase( i==SQLITE_DENY );
137454 pParse->zAuthContext = zSavedAuthContext;
137455
137456 /* Delete the transient structures associated with thesubquery */
137457 pSub1 = pSubitem->pSelect;
137458 sqlite3DbFree(db, p: pSubitem->zDatabase);
137459 sqlite3DbFree(db, p: pSubitem->zName);
137460 sqlite3DbFree(db, p: pSubitem->zAlias);
137461 pSubitem->zDatabase = 0;
137462 pSubitem->zName = 0;
137463 pSubitem->zAlias = 0;
137464 pSubitem->pSelect = 0;
137465 assert( pSubitem->pOn==0 );
137466
137467 /* If the sub-query is a compound SELECT statement, then (by restrictions
137468 ** 17 and 18 above) it must be a UNION ALL and the parent query must
137469 ** be of the form:
137470 **
137471 ** SELECT <expr-list> FROM (<sub-query>) <where-clause>
137472 **
137473 ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block
137474 ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
137475 ** OFFSET clauses and joins them to the left-hand-side of the original
137476 ** using UNION ALL operators. In this case N is the number of simple
137477 ** select statements in the compound sub-query.
137478 **
137479 ** Example:
137480 **
137481 ** SELECT a+1 FROM (
137482 ** SELECT x FROM tab
137483 ** UNION ALL
137484 ** SELECT y FROM tab
137485 ** UNION ALL
137486 ** SELECT abs(z*2) FROM tab2
137487 ** ) WHERE a!=5 ORDER BY 1
137488 **
137489 ** Transformed into:
137490 **
137491 ** SELECT x+1 FROM tab WHERE x+1!=5
137492 ** UNION ALL
137493 ** SELECT y+1 FROM tab WHERE y+1!=5
137494 ** UNION ALL
137495 ** SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5
137496 ** ORDER BY 1
137497 **
137498 ** We call this the "compound-subquery flattening".
137499 */
137500 for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
137501 Select *pNew;
137502 ExprList *pOrderBy = p->pOrderBy;
137503 Expr *pLimit = p->pLimit;
137504 Select *pPrior = p->pPrior;
137505 Table *pItemTab = pSubitem->pTab;
137506 pSubitem->pTab = 0;
137507 p->pOrderBy = 0;
137508 p->pPrior = 0;
137509 p->pLimit = 0;
137510 pNew = sqlite3SelectDup(db, pDup: p, flags: 0);
137511 p->pLimit = pLimit;
137512 p->pOrderBy = pOrderBy;
137513 p->op = TK_ALL;
137514 pSubitem->pTab = pItemTab;
137515 if( pNew==0 ){
137516 p->pPrior = pPrior;
137517 }else{
137518 pNew->selId = ++pParse->nSelect;
137519 if( aCsrMap && ALWAYS(db->mallocFailed==0) ){
137520 renumberCursors(pParse, p: pNew, iExcept: iFrom, aCsrMap);
137521 }
137522 pNew->pPrior = pPrior;
137523 if( pPrior ) pPrior->pNext = pNew;
137524 pNew->pNext = p;
137525 p->pPrior = pNew;
137526 SELECTTRACE(2,pParse,p,("compound-subquery flattener"
137527 " creates %u as peer\n",pNew->selId));
137528 }
137529 assert( pSubitem->pSelect==0 );
137530 }
137531 sqlite3DbFree(db, p: aCsrMap);
137532 if( db->mallocFailed ){
137533 pSubitem->pSelect = pSub1;
137534 return 1;
137535 }
137536
137537 /* Defer deleting the Table object associated with the
137538 ** subquery until code generation is
137539 ** complete, since there may still exist Expr.pTab entries that
137540 ** refer to the subquery even after flattening. Ticket #3346.
137541 **
137542 ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
137543 */
137544 if( ALWAYS(pSubitem->pTab!=0) ){
137545 Table *pTabToDel = pSubitem->pTab;
137546 if( pTabToDel->nTabRef==1 ){
137547 Parse *pToplevel = sqlite3ParseToplevel(pParse);
137548 sqlite3ParserAddCleanup(pParse: pToplevel,
137549 xCleanup: (void(*)(sqlite3*,void*))sqlite3DeleteTable,
137550 pPtr: pTabToDel);
137551 testcase( pToplevel->earlyCleanup );
137552 }else{
137553 pTabToDel->nTabRef--;
137554 }
137555 pSubitem->pTab = 0;
137556 }
137557
137558 /* The following loop runs once for each term in a compound-subquery
137559 ** flattening (as described above). If we are doing a different kind
137560 ** of flattening - a flattening other than a compound-subquery flattening -
137561 ** then this loop only runs once.
137562 **
137563 ** This loop moves all of the FROM elements of the subquery into the
137564 ** the FROM clause of the outer query. Before doing this, remember
137565 ** the cursor number for the original outer query FROM element in
137566 ** iParent. The iParent cursor will never be used. Subsequent code
137567 ** will scan expressions looking for iParent references and replace
137568 ** those references with expressions that resolve to the subquery FROM
137569 ** elements we are now copying in.
137570 */
137571 pSub = pSub1;
137572 for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
137573 int nSubSrc;
137574 u8 jointype = 0;
137575 assert( pSub!=0 );
137576 pSubSrc = pSub->pSrc; /* FROM clause of subquery */
137577 nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */
137578 pSrc = pParent->pSrc; /* FROM clause of the outer query */
137579
137580 if( pParent==p ){
137581 jointype = pSubitem->fg.jointype; /* First time through the loop */
137582 }
137583
137584 /* The subquery uses a single slot of the FROM clause of the outer
137585 ** query. If the subquery has more than one element in its FROM clause,
137586 ** then expand the outer query to make space for it to hold all elements
137587 ** of the subquery.
137588 **
137589 ** Example:
137590 **
137591 ** SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB;
137592 **
137593 ** The outer query has 3 slots in its FROM clause. One slot of the
137594 ** outer query (the middle slot) is used by the subquery. The next
137595 ** block of code will expand the outer query FROM clause to 4 slots.
137596 ** The middle slot is expanded to two slots in order to make space
137597 ** for the two elements in the FROM clause of the subquery.
137598 */
137599 if( nSubSrc>1 ){
137600 pSrc = sqlite3SrcListEnlarge(pParse, pSrc, nExtra: nSubSrc-1,iStart: iFrom+1);
137601 if( pSrc==0 ) break;
137602 pParent->pSrc = pSrc;
137603 }
137604
137605 /* Transfer the FROM clause terms from the subquery into the
137606 ** outer query.
137607 */
137608 for(i=0; i<nSubSrc; i++){
137609 sqlite3IdListDelete(db, pList: pSrc->a[i+iFrom].pUsing);
137610 assert( pSrc->a[i+iFrom].fg.isTabFunc==0 );
137611 pSrc->a[i+iFrom] = pSubSrc->a[i];
137612 iNewParent = pSubSrc->a[i].iCursor;
137613 memset(s: &pSubSrc->a[i], c: 0, n: sizeof(pSubSrc->a[i]));
137614 }
137615 pSrc->a[iFrom].fg.jointype = jointype;
137616
137617 /* Now begin substituting subquery result set expressions for
137618 ** references to the iParent in the outer query.
137619 **
137620 ** Example:
137621 **
137622 ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
137623 ** \ \_____________ subquery __________/ /
137624 ** \_____________________ outer query ______________________________/
137625 **
137626 ** We look at every expression in the outer query and every place we see
137627 ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
137628 */
137629 if( pSub->pOrderBy && (pParent->selFlags & SF_NoopOrderBy)==0 ){
137630 /* At this point, any non-zero iOrderByCol values indicate that the
137631 ** ORDER BY column expression is identical to the iOrderByCol'th
137632 ** expression returned by SELECT statement pSub. Since these values
137633 ** do not necessarily correspond to columns in SELECT statement pParent,
137634 ** zero them before transfering the ORDER BY clause.
137635 **
137636 ** Not doing this may cause an error if a subsequent call to this
137637 ** function attempts to flatten a compound sub-query into pParent
137638 ** (the only way this can happen is if the compound sub-query is
137639 ** currently part of pSub->pSrc). See ticket [d11a6e908f]. */
137640 ExprList *pOrderBy = pSub->pOrderBy;
137641 for(i=0; i<pOrderBy->nExpr; i++){
137642 pOrderBy->a[i].u.x.iOrderByCol = 0;
137643 }
137644 assert( pParent->pOrderBy==0 );
137645 pParent->pOrderBy = pOrderBy;
137646 pSub->pOrderBy = 0;
137647 }
137648 pWhere = pSub->pWhere;
137649 pSub->pWhere = 0;
137650 if( isLeftJoin>0 ){
137651 sqlite3SetJoinExpr(p: pWhere, iTable: iNewParent);
137652 }
137653 if( pWhere ){
137654 if( pParent->pWhere ){
137655 pParent->pWhere = sqlite3PExpr(pParse, TK_AND, pLeft: pWhere, pRight: pParent->pWhere);
137656 }else{
137657 pParent->pWhere = pWhere;
137658 }
137659 }
137660 if( db->mallocFailed==0 ){
137661 SubstContext x;
137662 x.pParse = pParse;
137663 x.iTable = iParent;
137664 x.iNewTable = iNewParent;
137665 x.isLeftJoin = isLeftJoin;
137666 x.pEList = pSub->pEList;
137667 substSelect(pSubst: &x, p: pParent, doPrior: 0);
137668 }
137669
137670 /* The flattened query is a compound if either the inner or the
137671 ** outer query is a compound. */
137672 pParent->selFlags |= pSub->selFlags & SF_Compound;
137673 assert( (pSub->selFlags & SF_Distinct)==0 ); /* restriction (17b) */
137674
137675 /*
137676 ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
137677 **
137678 ** One is tempted to try to add a and b to combine the limits. But this
137679 ** does not work if either limit is negative.
137680 */
137681 if( pSub->pLimit ){
137682 pParent->pLimit = pSub->pLimit;
137683 pSub->pLimit = 0;
137684 }
137685
137686 /* Recompute the SrcList_item.colUsed masks for the flattened
137687 ** tables. */
137688 for(i=0; i<nSubSrc; i++){
137689 recomputeColumnsUsed(pSelect: pParent, pSrcItem: &pSrc->a[i+iFrom]);
137690 }
137691 }
137692
137693 /* Finially, delete what is left of the subquery and return
137694 ** success.
137695 */
137696 sqlite3AggInfoPersistWalkerInit(pWalker: &w, pParse);
137697 sqlite3WalkSelect(pWalker: &w,p: pSub1);
137698 sqlite3SelectDelete(db, p: pSub1);
137699
137700#if SELECTTRACE_ENABLED
137701 if( sqlite3SelectTrace & 0x100 ){
137702 SELECTTRACE(0x100,pParse,p,("After flattening:\n"));
137703 sqlite3TreeViewSelect(0, p, 0);
137704 }
137705#endif
137706
137707 return 1;
137708}
137709#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
137710
137711/*
137712** A structure to keep track of all of the column values that are fixed to
137713** a known value due to WHERE clause constraints of the form COLUMN=VALUE.
137714*/
137715typedef struct WhereConst WhereConst;
137716struct WhereConst {
137717 Parse *pParse; /* Parsing context */
137718 u8 *pOomFault; /* Pointer to pParse->db->mallocFailed */
137719 int nConst; /* Number for COLUMN=CONSTANT terms */
137720 int nChng; /* Number of times a constant is propagated */
137721 int bHasAffBlob; /* At least one column in apExpr[] as affinity BLOB */
137722 Expr **apExpr; /* [i*2] is COLUMN and [i*2+1] is VALUE */
137723};
137724
137725/*
137726** Add a new entry to the pConst object. Except, do not add duplicate
137727** pColumn entires. Also, do not add if doing so would not be appropriate.
137728**
137729** The caller guarantees the pColumn is a column and pValue is a constant.
137730** This routine has to do some additional checks before completing the
137731** insert.
137732*/
137733static void constInsert(
137734 WhereConst *pConst, /* The WhereConst into which we are inserting */
137735 Expr *pColumn, /* The COLUMN part of the constraint */
137736 Expr *pValue, /* The VALUE part of the constraint */
137737 Expr *pExpr /* Overall expression: COLUMN=VALUE or VALUE=COLUMN */
137738){
137739 int i;
137740 assert( pColumn->op==TK_COLUMN );
137741 assert( sqlite3ExprIsConstant(pValue) );
137742
137743 if( ExprHasProperty(pColumn, EP_FixedCol) ) return;
137744 if( sqlite3ExprAffinity(pExpr: pValue)!=0 ) return;
137745 if( !sqlite3IsBinary(sqlite3ExprCompareCollSeq(pParse: pConst->pParse,p: pExpr)) ){
137746 return;
137747 }
137748
137749 /* 2018-10-25 ticket [cf5ed20f]
137750 ** Make sure the same pColumn is not inserted more than once */
137751 for(i=0; i<pConst->nConst; i++){
137752 const Expr *pE2 = pConst->apExpr[i*2];
137753 assert( pE2->op==TK_COLUMN );
137754 if( pE2->iTable==pColumn->iTable
137755 && pE2->iColumn==pColumn->iColumn
137756 ){
137757 return; /* Already present. Return without doing anything. */
137758 }
137759 }
137760 if( sqlite3ExprAffinity(pExpr: pColumn)==SQLITE_AFF_BLOB ){
137761 pConst->bHasAffBlob = 1;
137762 }
137763
137764 pConst->nConst++;
137765 pConst->apExpr = sqlite3DbReallocOrFree(db: pConst->pParse->db, p: pConst->apExpr,
137766 n: pConst->nConst*2*sizeof(Expr*));
137767 if( pConst->apExpr==0 ){
137768 pConst->nConst = 0;
137769 }else{
137770 pConst->apExpr[pConst->nConst*2-2] = pColumn;
137771 pConst->apExpr[pConst->nConst*2-1] = pValue;
137772 }
137773}
137774
137775/*
137776** Find all terms of COLUMN=VALUE or VALUE=COLUMN in pExpr where VALUE
137777** is a constant expression and where the term must be true because it
137778** is part of the AND-connected terms of the expression. For each term
137779** found, add it to the pConst structure.
137780*/
137781static void findConstInWhere(WhereConst *pConst, Expr *pExpr){
137782 Expr *pRight, *pLeft;
137783 if( NEVER(pExpr==0) ) return;
137784 if( ExprHasProperty(pExpr, EP_FromJoin) ) return;
137785 if( pExpr->op==TK_AND ){
137786 findConstInWhere(pConst, pExpr: pExpr->pRight);
137787 findConstInWhere(pConst, pExpr: pExpr->pLeft);
137788 return;
137789 }
137790 if( pExpr->op!=TK_EQ ) return;
137791 pRight = pExpr->pRight;
137792 pLeft = pExpr->pLeft;
137793 assert( pRight!=0 );
137794 assert( pLeft!=0 );
137795 if( pRight->op==TK_COLUMN && sqlite3ExprIsConstant(p: pLeft) ){
137796 constInsert(pConst,pColumn: pRight,pValue: pLeft,pExpr);
137797 }
137798 if( pLeft->op==TK_COLUMN && sqlite3ExprIsConstant(p: pRight) ){
137799 constInsert(pConst,pColumn: pLeft,pValue: pRight,pExpr);
137800 }
137801}
137802
137803/*
137804** This is a helper function for Walker callback propagateConstantExprRewrite().
137805**
137806** Argument pExpr is a candidate expression to be replaced by a value. If
137807** pExpr is equivalent to one of the columns named in pWalker->u.pConst,
137808** then overwrite it with the corresponding value. Except, do not do so
137809** if argument bIgnoreAffBlob is non-zero and the affinity of pExpr
137810** is SQLITE_AFF_BLOB.
137811*/
137812static int propagateConstantExprRewriteOne(
137813 WhereConst *pConst,
137814 Expr *pExpr,
137815 int bIgnoreAffBlob
137816){
137817 int i;
137818 if( pConst->pOomFault[0] ) return WRC_Prune;
137819 if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
137820 if( ExprHasProperty(pExpr, EP_FixedCol|EP_FromJoin) ){
137821 testcase( ExprHasProperty(pExpr, EP_FixedCol) );
137822 testcase( ExprHasProperty(pExpr, EP_FromJoin) );
137823 return WRC_Continue;
137824 }
137825 for(i=0; i<pConst->nConst; i++){
137826 Expr *pColumn = pConst->apExpr[i*2];
137827 if( pColumn==pExpr ) continue;
137828 if( pColumn->iTable!=pExpr->iTable ) continue;
137829 if( pColumn->iColumn!=pExpr->iColumn ) continue;
137830 if( bIgnoreAffBlob && sqlite3ExprAffinity(pExpr: pColumn)==SQLITE_AFF_BLOB ){
137831 break;
137832 }
137833 /* A match is found. Add the EP_FixedCol property */
137834 pConst->nChng++;
137835 ExprClearProperty(pExpr, EP_Leaf);
137836 ExprSetProperty(pExpr, EP_FixedCol);
137837 assert( pExpr->pLeft==0 );
137838 pExpr->pLeft = sqlite3ExprDup(db: pConst->pParse->db, p: pConst->apExpr[i*2+1], flags: 0);
137839 if( pConst->pParse->db->mallocFailed ) return WRC_Prune;
137840 break;
137841 }
137842 return WRC_Prune;
137843}
137844
137845/*
137846** This is a Walker expression callback. pExpr is a node from the WHERE
137847** clause of a SELECT statement. This function examines pExpr to see if
137848** any substitutions based on the contents of pWalker->u.pConst should
137849** be made to pExpr or its immediate children.
137850**
137851** A substitution is made if:
137852**
137853** + pExpr is a column with an affinity other than BLOB that matches
137854** one of the columns in pWalker->u.pConst, or
137855**
137856** + pExpr is a binary comparison operator (=, <=, >=, <, >) that
137857** uses an affinity other than TEXT and one of its immediate
137858** children is a column that matches one of the columns in
137859** pWalker->u.pConst.
137860*/
137861static int propagateConstantExprRewrite(Walker *pWalker, Expr *pExpr){
137862 WhereConst *pConst = pWalker->u.pConst;
137863 assert( TK_GT==TK_EQ+1 );
137864 assert( TK_LE==TK_EQ+2 );
137865 assert( TK_LT==TK_EQ+3 );
137866 assert( TK_GE==TK_EQ+4 );
137867 if( pConst->bHasAffBlob ){
137868 if( (pExpr->op>=TK_EQ && pExpr->op<=TK_GE)
137869 || pExpr->op==TK_IS
137870 ){
137871 propagateConstantExprRewriteOne(pConst, pExpr: pExpr->pLeft, bIgnoreAffBlob: 0);
137872 if( pConst->pOomFault[0] ) return WRC_Prune;
137873 if( sqlite3ExprAffinity(pExpr: pExpr->pLeft)!=SQLITE_AFF_TEXT ){
137874 propagateConstantExprRewriteOne(pConst, pExpr: pExpr->pRight, bIgnoreAffBlob: 0);
137875 }
137876 }
137877 }
137878 return propagateConstantExprRewriteOne(pConst, pExpr, bIgnoreAffBlob: pConst->bHasAffBlob);
137879}
137880
137881/*
137882** The WHERE-clause constant propagation optimization.
137883**
137884** If the WHERE clause contains terms of the form COLUMN=CONSTANT or
137885** CONSTANT=COLUMN that are top-level AND-connected terms that are not
137886** part of a ON clause from a LEFT JOIN, then throughout the query
137887** replace all other occurrences of COLUMN with CONSTANT.
137888**
137889** For example, the query:
137890**
137891** SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=t1.a AND t3.c=t2.b
137892**
137893** Is transformed into
137894**
137895** SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=39 AND t3.c=39
137896**
137897** Return true if any transformations where made and false if not.
137898**
137899** Implementation note: Constant propagation is tricky due to affinity
137900** and collating sequence interactions. Consider this example:
137901**
137902** CREATE TABLE t1(a INT,b TEXT);
137903** INSERT INTO t1 VALUES(123,'0123');
137904** SELECT * FROM t1 WHERE a=123 AND b=a;
137905** SELECT * FROM t1 WHERE a=123 AND b=123;
137906**
137907** The two SELECT statements above should return different answers. b=a
137908** is alway true because the comparison uses numeric affinity, but b=123
137909** is false because it uses text affinity and '0123' is not the same as '123'.
137910** To work around this, the expression tree is not actually changed from
137911** "b=a" to "b=123" but rather the "a" in "b=a" is tagged with EP_FixedCol
137912** and the "123" value is hung off of the pLeft pointer. Code generator
137913** routines know to generate the constant "123" instead of looking up the
137914** column value. Also, to avoid collation problems, this optimization is
137915** only attempted if the "a=123" term uses the default BINARY collation.
137916**
137917** 2021-05-25 forum post 6a06202608: Another troublesome case is...
137918**
137919** CREATE TABLE t1(x);
137920** INSERT INTO t1 VALUES(10.0);
137921** SELECT 1 FROM t1 WHERE x=10 AND x LIKE 10;
137922**
137923** The query should return no rows, because the t1.x value is '10.0' not '10'
137924** and '10.0' is not LIKE '10'. But if we are not careful, the first WHERE
137925** term "x=10" will cause the second WHERE term to become "10 LIKE 10",
137926** resulting in a false positive. To avoid this, constant propagation for
137927** columns with BLOB affinity is only allowed if the constant is used with
137928** operators ==, <=, <, >=, >, or IS in a way that will cause the correct
137929** type conversions to occur. See logic associated with the bHasAffBlob flag
137930** for details.
137931*/
137932static int propagateConstants(
137933 Parse *pParse, /* The parsing context */
137934 Select *p /* The query in which to propagate constants */
137935){
137936 WhereConst x;
137937 Walker w;
137938 int nChng = 0;
137939 x.pParse = pParse;
137940 x.pOomFault = &pParse->db->mallocFailed;
137941 do{
137942 x.nConst = 0;
137943 x.nChng = 0;
137944 x.apExpr = 0;
137945 x.bHasAffBlob = 0;
137946 findConstInWhere(pConst: &x, pExpr: p->pWhere);
137947 if( x.nConst ){
137948 memset(s: &w, c: 0, n: sizeof(w));
137949 w.pParse = pParse;
137950 w.xExprCallback = propagateConstantExprRewrite;
137951 w.xSelectCallback = sqlite3SelectWalkNoop;
137952 w.xSelectCallback2 = 0;
137953 w.walkerDepth = 0;
137954 w.u.pConst = &x;
137955 sqlite3WalkExpr(pWalker: &w, pExpr: p->pWhere);
137956 sqlite3DbFree(db: x.pParse->db, p: x.apExpr);
137957 nChng += x.nChng;
137958 }
137959 }while( x.nChng );
137960 return nChng;
137961}
137962
137963#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
137964# if !defined(SQLITE_OMIT_WINDOWFUNC)
137965/*
137966** This function is called to determine whether or not it is safe to
137967** push WHERE clause expression pExpr down to FROM clause sub-query
137968** pSubq, which contains at least one window function. Return 1
137969** if it is safe and the expression should be pushed down, or 0
137970** otherwise.
137971**
137972** It is only safe to push the expression down if it consists only
137973** of constants and copies of expressions that appear in the PARTITION
137974** BY clause of all window function used by the sub-query. It is safe
137975** to filter out entire partitions, but not rows within partitions, as
137976** this may change the results of the window functions.
137977**
137978** At the time this function is called it is guaranteed that
137979**
137980** * the sub-query uses only one distinct window frame, and
137981** * that the window frame has a PARTITION BY clase.
137982*/
137983static int pushDownWindowCheck(Parse *pParse, Select *pSubq, Expr *pExpr){
137984 assert( pSubq->pWin->pPartition );
137985 assert( (pSubq->selFlags & SF_MultiPart)==0 );
137986 assert( pSubq->pPrior==0 );
137987 return sqlite3ExprIsConstantOrGroupBy(pParse, p: pExpr, pGroupBy: pSubq->pWin->pPartition);
137988}
137989# endif /* SQLITE_OMIT_WINDOWFUNC */
137990#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
137991
137992#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
137993/*
137994** Make copies of relevant WHERE clause terms of the outer query into
137995** the WHERE clause of subquery. Example:
137996**
137997** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
137998**
137999** Transformed into:
138000**
138001** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
138002** WHERE x=5 AND y=10;
138003**
138004** The hope is that the terms added to the inner query will make it more
138005** efficient.
138006**
138007** Do not attempt this optimization if:
138008**
138009** (1) (** This restriction was removed on 2017-09-29. We used to
138010** disallow this optimization for aggregate subqueries, but now
138011** it is allowed by putting the extra terms on the HAVING clause.
138012** The added HAVING clause is pointless if the subquery lacks
138013** a GROUP BY clause. But such a HAVING clause is also harmless
138014** so there does not appear to be any reason to add extra logic
138015** to suppress it. **)
138016**
138017** (2) The inner query is the recursive part of a common table expression.
138018**
138019** (3) The inner query has a LIMIT clause (since the changes to the WHERE
138020** clause would change the meaning of the LIMIT).
138021**
138022** (4) The inner query is the right operand of a LEFT JOIN and the
138023** expression to be pushed down does not come from the ON clause
138024** on that LEFT JOIN.
138025**
138026** (5) The WHERE clause expression originates in the ON or USING clause
138027** of a LEFT JOIN where iCursor is not the right-hand table of that
138028** left join. An example:
138029**
138030** SELECT *
138031** FROM (SELECT 1 AS a1 UNION ALL SELECT 2) AS aa
138032** JOIN (SELECT 1 AS b2 UNION ALL SELECT 2) AS bb ON (a1=b2)
138033** LEFT JOIN (SELECT 8 AS c3 UNION ALL SELECT 9) AS cc ON (b2=2);
138034**
138035** The correct answer is three rows: (1,1,NULL),(2,2,8),(2,2,9).
138036** But if the (b2=2) term were to be pushed down into the bb subquery,
138037** then the (1,1,NULL) row would be suppressed.
138038**
138039** (6) Window functions make things tricky as changes to the WHERE clause
138040** of the inner query could change the window over which window
138041** functions are calculated. Therefore, do not attempt the optimization
138042** if:
138043**
138044** (6a) The inner query uses multiple incompatible window partitions.
138045**
138046** (6b) The inner query is a compound and uses window-functions.
138047**
138048** (6c) The WHERE clause does not consist entirely of constants and
138049** copies of expressions found in the PARTITION BY clause of
138050** all window-functions used by the sub-query. It is safe to
138051** filter out entire partitions, as this does not change the
138052** window over which any window-function is calculated.
138053**
138054** (7) The inner query is a Common Table Expression (CTE) that should
138055** be materialized. (This restriction is implemented in the calling
138056** routine.)
138057**
138058** Return 0 if no changes are made and non-zero if one or more WHERE clause
138059** terms are duplicated into the subquery.
138060*/
138061static int pushDownWhereTerms(
138062 Parse *pParse, /* Parse context (for malloc() and error reporting) */
138063 Select *pSubq, /* The subquery whose WHERE clause is to be augmented */
138064 Expr *pWhere, /* The WHERE clause of the outer query */
138065 int iCursor, /* Cursor number of the subquery */
138066 int isLeftJoin /* True if pSubq is the right term of a LEFT JOIN */
138067){
138068 Expr *pNew;
138069 int nChng = 0;
138070 if( pWhere==0 ) return 0;
138071 if( pSubq->selFlags & (SF_Recursive|SF_MultiPart) ) return 0;
138072
138073#ifndef SQLITE_OMIT_WINDOWFUNC
138074 if( pSubq->pPrior ){
138075 Select *pSel;
138076 for(pSel=pSubq; pSel; pSel=pSel->pPrior){
138077 if( pSel->pWin ) return 0; /* restriction (6b) */
138078 }
138079 }else{
138080 if( pSubq->pWin && pSubq->pWin->pPartition==0 ) return 0;
138081 }
138082#endif
138083
138084#ifdef SQLITE_DEBUG
138085 /* Only the first term of a compound can have a WITH clause. But make
138086 ** sure no other terms are marked SF_Recursive in case something changes
138087 ** in the future.
138088 */
138089 {
138090 Select *pX;
138091 for(pX=pSubq; pX; pX=pX->pPrior){
138092 assert( (pX->selFlags & (SF_Recursive))==0 );
138093 }
138094 }
138095#endif
138096
138097 if( pSubq->pLimit!=0 ){
138098 return 0; /* restriction (3) */
138099 }
138100 while( pWhere->op==TK_AND ){
138101 nChng += pushDownWhereTerms(pParse, pSubq, pWhere: pWhere->pRight,
138102 iCursor, isLeftJoin);
138103 pWhere = pWhere->pLeft;
138104 }
138105 if( isLeftJoin
138106 && (ExprHasProperty(pWhere,EP_FromJoin)==0
138107 || pWhere->iRightJoinTable!=iCursor)
138108 ){
138109 return 0; /* restriction (4) */
138110 }
138111 if( ExprHasProperty(pWhere,EP_FromJoin) && pWhere->iRightJoinTable!=iCursor ){
138112 return 0; /* restriction (5) */
138113 }
138114 if( sqlite3ExprIsTableConstant(p: pWhere, iCur: iCursor) ){
138115 nChng++;
138116 pSubq->selFlags |= SF_PushDown;
138117 while( pSubq ){
138118 SubstContext x;
138119 pNew = sqlite3ExprDup(db: pParse->db, p: pWhere, flags: 0);
138120 unsetJoinExpr(p: pNew, iTable: -1);
138121 x.pParse = pParse;
138122 x.iTable = iCursor;
138123 x.iNewTable = iCursor;
138124 x.isLeftJoin = 0;
138125 x.pEList = pSubq->pEList;
138126 pNew = substExpr(pSubst: &x, pExpr: pNew);
138127#ifndef SQLITE_OMIT_WINDOWFUNC
138128 if( pSubq->pWin && 0==pushDownWindowCheck(pParse, pSubq, pExpr: pNew) ){
138129 /* Restriction 6c has prevented push-down in this case */
138130 sqlite3ExprDelete(db: pParse->db, p: pNew);
138131 nChng--;
138132 break;
138133 }
138134#endif
138135 if( pSubq->selFlags & SF_Aggregate ){
138136 pSubq->pHaving = sqlite3ExprAnd(pParse, pLeft: pSubq->pHaving, pRight: pNew);
138137 }else{
138138 pSubq->pWhere = sqlite3ExprAnd(pParse, pLeft: pSubq->pWhere, pRight: pNew);
138139 }
138140 pSubq = pSubq->pPrior;
138141 }
138142 }
138143 return nChng;
138144}
138145#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
138146
138147/*
138148** The pFunc is the only aggregate function in the query. Check to see
138149** if the query is a candidate for the min/max optimization.
138150**
138151** If the query is a candidate for the min/max optimization, then set
138152** *ppMinMax to be an ORDER BY clause to be used for the optimization
138153** and return either WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX depending on
138154** whether pFunc is a min() or max() function.
138155**
138156** If the query is not a candidate for the min/max optimization, return
138157** WHERE_ORDERBY_NORMAL (which must be zero).
138158**
138159** This routine must be called after aggregate functions have been
138160** located but before their arguments have been subjected to aggregate
138161** analysis.
138162*/
138163static u8 minMaxQuery(sqlite3 *db, Expr *pFunc, ExprList **ppMinMax){
138164 int eRet = WHERE_ORDERBY_NORMAL; /* Return value */
138165 ExprList *pEList; /* Arguments to agg function */
138166 const char *zFunc; /* Name of aggregate function pFunc */
138167 ExprList *pOrderBy;
138168 u8 sortFlags = 0;
138169
138170 assert( *ppMinMax==0 );
138171 assert( pFunc->op==TK_AGG_FUNCTION );
138172 assert( !IsWindowFunc(pFunc) );
138173 assert( ExprUseXList(pFunc) );
138174 pEList = pFunc->x.pList;
138175 if( pEList==0
138176 || pEList->nExpr!=1
138177 || ExprHasProperty(pFunc, EP_WinFunc)
138178 || OptimizationDisabled(db, SQLITE_MinMaxOpt)
138179 ){
138180 return eRet;
138181 }
138182 assert( !ExprHasProperty(pFunc, EP_IntValue) );
138183 zFunc = pFunc->u.zToken;
138184 if( sqlite3StrICmp(zLeft: zFunc, zRight: "min")==0 ){
138185 eRet = WHERE_ORDERBY_MIN;
138186 if( sqlite3ExprCanBeNull(p: pEList->a[0].pExpr) ){
138187 sortFlags = KEYINFO_ORDER_BIGNULL;
138188 }
138189 }else if( sqlite3StrICmp(zLeft: zFunc, zRight: "max")==0 ){
138190 eRet = WHERE_ORDERBY_MAX;
138191 sortFlags = KEYINFO_ORDER_DESC;
138192 }else{
138193 return eRet;
138194 }
138195 *ppMinMax = pOrderBy = sqlite3ExprListDup(db, p: pEList, flags: 0);
138196 assert( pOrderBy!=0 || db->mallocFailed );
138197 if( pOrderBy ) pOrderBy->a[0].sortFlags = sortFlags;
138198 return eRet;
138199}
138200
138201/*
138202** The select statement passed as the first argument is an aggregate query.
138203** The second argument is the associated aggregate-info object. This
138204** function tests if the SELECT is of the form:
138205**
138206** SELECT count(*) FROM <tbl>
138207**
138208** where table is a database table, not a sub-select or view. If the query
138209** does match this pattern, then a pointer to the Table object representing
138210** <tbl> is returned. Otherwise, NULL is returned.
138211**
138212** This routine checks to see if it is safe to use the count optimization.
138213** A correct answer is still obtained (though perhaps more slowly) if
138214** this routine returns NULL when it could have returned a table pointer.
138215** But returning the pointer when NULL should have been returned can
138216** result in incorrect answers and/or crashes. So, when in doubt, return NULL.
138217*/
138218static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){
138219 Table *pTab;
138220 Expr *pExpr;
138221
138222 assert( !p->pGroupBy );
138223
138224 if( p->pWhere
138225 || p->pEList->nExpr!=1
138226 || p->pSrc->nSrc!=1
138227 || p->pSrc->a[0].pSelect
138228 || pAggInfo->nFunc!=1
138229 ){
138230 return 0;
138231 }
138232 pTab = p->pSrc->a[0].pTab;
138233 assert( pTab!=0 );
138234 assert( !IsView(pTab) );
138235 if( !IsOrdinaryTable(pTab) ) return 0;
138236 pExpr = p->pEList->a[0].pExpr;
138237 assert( pExpr!=0 );
138238 if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
138239 if( pExpr->pAggInfo!=pAggInfo ) return 0;
138240 if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0;
138241 assert( pAggInfo->aFunc[0].pFExpr==pExpr );
138242 testcase( ExprHasProperty(pExpr, EP_Distinct) );
138243 testcase( ExprHasProperty(pExpr, EP_WinFunc) );
138244 if( ExprHasProperty(pExpr, EP_Distinct|EP_WinFunc) ) return 0;
138245
138246 return pTab;
138247}
138248
138249/*
138250** If the source-list item passed as an argument was augmented with an
138251** INDEXED BY clause, then try to locate the specified index. If there
138252** was such a clause and the named index cannot be found, return
138253** SQLITE_ERROR and leave an error in pParse. Otherwise, populate
138254** pFrom->pIndex and return SQLITE_OK.
138255*/
138256SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *pParse, SrcItem *pFrom){
138257 Table *pTab = pFrom->pTab;
138258 char *zIndexedBy = pFrom->u1.zIndexedBy;
138259 Index *pIdx;
138260 assert( pTab!=0 );
138261 assert( pFrom->fg.isIndexedBy!=0 );
138262
138263 for(pIdx=pTab->pIndex;
138264 pIdx && sqlite3StrICmp(zLeft: pIdx->zName, zRight: zIndexedBy);
138265 pIdx=pIdx->pNext
138266 );
138267 if( !pIdx ){
138268 sqlite3ErrorMsg(pParse, zFormat: "no such index: %s", zIndexedBy, 0);
138269 pParse->checkSchema = 1;
138270 return SQLITE_ERROR;
138271 }
138272 assert( pFrom->fg.isCte==0 );
138273 pFrom->u2.pIBIndex = pIdx;
138274 return SQLITE_OK;
138275}
138276
138277/*
138278** Detect compound SELECT statements that use an ORDER BY clause with
138279** an alternative collating sequence.
138280**
138281** SELECT ... FROM t1 EXCEPT SELECT ... FROM t2 ORDER BY .. COLLATE ...
138282**
138283** These are rewritten as a subquery:
138284**
138285** SELECT * FROM (SELECT ... FROM t1 EXCEPT SELECT ... FROM t2)
138286** ORDER BY ... COLLATE ...
138287**
138288** This transformation is necessary because the multiSelectOrderBy() routine
138289** above that generates the code for a compound SELECT with an ORDER BY clause
138290** uses a merge algorithm that requires the same collating sequence on the
138291** result columns as on the ORDER BY clause. See ticket
138292** http://www.sqlite.org/src/info/6709574d2a
138293**
138294** This transformation is only needed for EXCEPT, INTERSECT, and UNION.
138295** The UNION ALL operator works fine with multiSelectOrderBy() even when
138296** there are COLLATE terms in the ORDER BY.
138297*/
138298static int convertCompoundSelectToSubquery(Walker *pWalker, Select *p){
138299 int i;
138300 Select *pNew;
138301 Select *pX;
138302 sqlite3 *db;
138303 struct ExprList_item *a;
138304 SrcList *pNewSrc;
138305 Parse *pParse;
138306 Token dummy;
138307
138308 if( p->pPrior==0 ) return WRC_Continue;
138309 if( p->pOrderBy==0 ) return WRC_Continue;
138310 for(pX=p; pX && (pX->op==TK_ALL || pX->op==TK_SELECT); pX=pX->pPrior){}
138311 if( pX==0 ) return WRC_Continue;
138312 a = p->pOrderBy->a;
138313#ifndef SQLITE_OMIT_WINDOWFUNC
138314 /* If iOrderByCol is already non-zero, then it has already been matched
138315 ** to a result column of the SELECT statement. This occurs when the
138316 ** SELECT is rewritten for window-functions processing and then passed
138317 ** to sqlite3SelectPrep() and similar a second time. The rewriting done
138318 ** by this function is not required in this case. */
138319 if( a[0].u.x.iOrderByCol ) return WRC_Continue;
138320#endif
138321 for(i=p->pOrderBy->nExpr-1; i>=0; i--){
138322 if( a[i].pExpr->flags & EP_Collate ) break;
138323 }
138324 if( i<0 ) return WRC_Continue;
138325
138326 /* If we reach this point, that means the transformation is required. */
138327
138328 pParse = pWalker->pParse;
138329 db = pParse->db;
138330 pNew = sqlite3DbMallocZero(db, n: sizeof(*pNew) );
138331 if( pNew==0 ) return WRC_Abort;
138332 memset(s: &dummy, c: 0, n: sizeof(dummy));
138333 pNewSrc = sqlite3SrcListAppendFromTerm(pParse,p: 0,pTable: 0,pDatabase: 0,pAlias: &dummy,pSubquery: pNew,pOn: 0,pUsing: 0);
138334 if( pNewSrc==0 ) return WRC_Abort;
138335 *pNew = *p;
138336 p->pSrc = pNewSrc;
138337 p->pEList = sqlite3ExprListAppend(pParse, pList: 0, pExpr: sqlite3Expr(db, TK_ASTERISK, zToken: 0));
138338 p->op = TK_SELECT;
138339 p->pWhere = 0;
138340 pNew->pGroupBy = 0;
138341 pNew->pHaving = 0;
138342 pNew->pOrderBy = 0;
138343 p->pPrior = 0;
138344 p->pNext = 0;
138345 p->pWith = 0;
138346#ifndef SQLITE_OMIT_WINDOWFUNC
138347 p->pWinDefn = 0;
138348#endif
138349 p->selFlags &= ~SF_Compound;
138350 assert( (p->selFlags & SF_Converted)==0 );
138351 p->selFlags |= SF_Converted;
138352 assert( pNew->pPrior!=0 );
138353 pNew->pPrior->pNext = pNew;
138354 pNew->pLimit = 0;
138355 return WRC_Continue;
138356}
138357
138358/*
138359** Check to see if the FROM clause term pFrom has table-valued function
138360** arguments. If it does, leave an error message in pParse and return
138361** non-zero, since pFrom is not allowed to be a table-valued function.
138362*/
138363static int cannotBeFunction(Parse *pParse, SrcItem *pFrom){
138364 if( pFrom->fg.isTabFunc ){
138365 sqlite3ErrorMsg(pParse, zFormat: "'%s' is not a function", pFrom->zName);
138366 return 1;
138367 }
138368 return 0;
138369}
138370
138371#ifndef SQLITE_OMIT_CTE
138372/*
138373** Argument pWith (which may be NULL) points to a linked list of nested
138374** WITH contexts, from inner to outermost. If the table identified by
138375** FROM clause element pItem is really a common-table-expression (CTE)
138376** then return a pointer to the CTE definition for that table. Otherwise
138377** return NULL.
138378**
138379** If a non-NULL value is returned, set *ppContext to point to the With
138380** object that the returned CTE belongs to.
138381*/
138382static struct Cte *searchWith(
138383 With *pWith, /* Current innermost WITH clause */
138384 SrcItem *pItem, /* FROM clause element to resolve */
138385 With **ppContext /* OUT: WITH clause return value belongs to */
138386){
138387 const char *zName = pItem->zName;
138388 With *p;
138389 assert( pItem->zDatabase==0 );
138390 assert( zName!=0 );
138391 for(p=pWith; p; p=p->pOuter){
138392 int i;
138393 for(i=0; i<p->nCte; i++){
138394 if( sqlite3StrICmp(zLeft: zName, zRight: p->a[i].zName)==0 ){
138395 *ppContext = p;
138396 return &p->a[i];
138397 }
138398 }
138399 if( p->bView ) break;
138400 }
138401 return 0;
138402}
138403
138404/* The code generator maintains a stack of active WITH clauses
138405** with the inner-most WITH clause being at the top of the stack.
138406**
138407** This routine pushes the WITH clause passed as the second argument
138408** onto the top of the stack. If argument bFree is true, then this
138409** WITH clause will never be popped from the stack but should instead
138410** be freed along with the Parse object. In other cases, when
138411** bFree==0, the With object will be freed along with the SELECT
138412** statement with which it is associated.
138413**
138414** This routine returns a copy of pWith. Or, if bFree is true and
138415** the pWith object is destroyed immediately due to an OOM condition,
138416** then this routine return NULL.
138417**
138418** If bFree is true, do not continue to use the pWith pointer after
138419** calling this routine, Instead, use only the return value.
138420*/
138421SQLITE_PRIVATE With *sqlite3WithPush(Parse *pParse, With *pWith, u8 bFree){
138422 if( pWith ){
138423 if( bFree ){
138424 pWith = (With*)sqlite3ParserAddCleanup(pParse,
138425 xCleanup: (void(*)(sqlite3*,void*))sqlite3WithDelete,
138426 pPtr: pWith);
138427 if( pWith==0 ) return 0;
138428 }
138429 if( pParse->nErr==0 ){
138430 assert( pParse->pWith!=pWith );
138431 pWith->pOuter = pParse->pWith;
138432 pParse->pWith = pWith;
138433 }
138434 }
138435 return pWith;
138436}
138437
138438/*
138439** This function checks if argument pFrom refers to a CTE declared by
138440** a WITH clause on the stack currently maintained by the parser (on the
138441** pParse->pWith linked list). And if currently processing a CTE
138442** CTE expression, through routine checks to see if the reference is
138443** a recursive reference to the CTE.
138444**
138445** If pFrom matches a CTE according to either of these two above, pFrom->pTab
138446** and other fields are populated accordingly.
138447**
138448** Return 0 if no match is found.
138449** Return 1 if a match is found.
138450** Return 2 if an error condition is detected.
138451*/
138452static int resolveFromTermToCte(
138453 Parse *pParse, /* The parsing context */
138454 Walker *pWalker, /* Current tree walker */
138455 SrcItem *pFrom /* The FROM clause term to check */
138456){
138457 Cte *pCte; /* Matched CTE (or NULL if no match) */
138458 With *pWith; /* The matching WITH */
138459
138460 assert( pFrom->pTab==0 );
138461 if( pParse->pWith==0 ){
138462 /* There are no WITH clauses in the stack. No match is possible */
138463 return 0;
138464 }
138465 if( pParse->nErr ){
138466 /* Prior errors might have left pParse->pWith in a goofy state, so
138467 ** go no further. */
138468 return 0;
138469 }
138470 if( pFrom->zDatabase!=0 ){
138471 /* The FROM term contains a schema qualifier (ex: main.t1) and so
138472 ** it cannot possibly be a CTE reference. */
138473 return 0;
138474 }
138475 if( pFrom->fg.notCte ){
138476 /* The FROM term is specifically excluded from matching a CTE.
138477 ** (1) It is part of a trigger that used to have zDatabase but had
138478 ** zDatabase removed by sqlite3FixTriggerStep().
138479 ** (2) This is the first term in the FROM clause of an UPDATE.
138480 */
138481 return 0;
138482 }
138483 pCte = searchWith(pWith: pParse->pWith, pItem: pFrom, ppContext: &pWith);
138484 if( pCte ){
138485 sqlite3 *db = pParse->db;
138486 Table *pTab;
138487 ExprList *pEList;
138488 Select *pSel;
138489 Select *pLeft; /* Left-most SELECT statement */
138490 Select *pRecTerm; /* Left-most recursive term */
138491 int bMayRecursive; /* True if compound joined by UNION [ALL] */
138492 With *pSavedWith; /* Initial value of pParse->pWith */
138493 int iRecTab = -1; /* Cursor for recursive table */
138494 CteUse *pCteUse;
138495
138496 /* If pCte->zCteErr is non-NULL at this point, then this is an illegal
138497 ** recursive reference to CTE pCte. Leave an error in pParse and return
138498 ** early. If pCte->zCteErr is NULL, then this is not a recursive reference.
138499 ** In this case, proceed. */
138500 if( pCte->zCteErr ){
138501 sqlite3ErrorMsg(pParse, zFormat: pCte->zCteErr, pCte->zName);
138502 return 2;
138503 }
138504 if( cannotBeFunction(pParse, pFrom) ) return 2;
138505
138506 assert( pFrom->pTab==0 );
138507 pTab = sqlite3DbMallocZero(db, n: sizeof(Table));
138508 if( pTab==0 ) return 2;
138509 pCteUse = pCte->pUse;
138510 if( pCteUse==0 ){
138511 pCte->pUse = pCteUse = sqlite3DbMallocZero(db, n: sizeof(pCteUse[0]));
138512 if( pCteUse==0
138513 || sqlite3ParserAddCleanup(pParse,xCleanup: sqlite3DbFree,pPtr: pCteUse)==0
138514 ){
138515 sqlite3DbFree(db, p: pTab);
138516 return 2;
138517 }
138518 pCteUse->eM10d = pCte->eM10d;
138519 }
138520 pFrom->pTab = pTab;
138521 pTab->nTabRef = 1;
138522 pTab->zName = sqlite3DbStrDup(db, z: pCte->zName);
138523 pTab->iPKey = -1;
138524 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
138525 pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
138526 pFrom->pSelect = sqlite3SelectDup(db, pDup: pCte->pSelect, flags: 0);
138527 if( db->mallocFailed ) return 2;
138528 pFrom->pSelect->selFlags |= SF_CopyCte;
138529 assert( pFrom->pSelect );
138530 if( pFrom->fg.isIndexedBy ){
138531 sqlite3ErrorMsg(pParse, zFormat: "no such index: \"%s\"", pFrom->u1.zIndexedBy);
138532 return 2;
138533 }
138534 pFrom->fg.isCte = 1;
138535 pFrom->u2.pCteUse = pCteUse;
138536 pCteUse->nUse++;
138537 if( pCteUse->nUse>=2 && pCteUse->eM10d==M10d_Any ){
138538 pCteUse->eM10d = M10d_Yes;
138539 }
138540
138541 /* Check if this is a recursive CTE. */
138542 pRecTerm = pSel = pFrom->pSelect;
138543 bMayRecursive = ( pSel->op==TK_ALL || pSel->op==TK_UNION );
138544 while( bMayRecursive && pRecTerm->op==pSel->op ){
138545 int i;
138546 SrcList *pSrc = pRecTerm->pSrc;
138547 assert( pRecTerm->pPrior!=0 );
138548 for(i=0; i<pSrc->nSrc; i++){
138549 SrcItem *pItem = &pSrc->a[i];
138550 if( pItem->zDatabase==0
138551 && pItem->zName!=0
138552 && 0==sqlite3StrICmp(zLeft: pItem->zName, zRight: pCte->zName)
138553 ){
138554 pItem->pTab = pTab;
138555 pTab->nTabRef++;
138556 pItem->fg.isRecursive = 1;
138557 if( pRecTerm->selFlags & SF_Recursive ){
138558 sqlite3ErrorMsg(pParse,
138559 zFormat: "multiple references to recursive table: %s", pCte->zName
138560 );
138561 return 2;
138562 }
138563 pRecTerm->selFlags |= SF_Recursive;
138564 if( iRecTab<0 ) iRecTab = pParse->nTab++;
138565 pItem->iCursor = iRecTab;
138566 }
138567 }
138568 if( (pRecTerm->selFlags & SF_Recursive)==0 ) break;
138569 pRecTerm = pRecTerm->pPrior;
138570 }
138571
138572 pCte->zCteErr = "circular reference: %s";
138573 pSavedWith = pParse->pWith;
138574 pParse->pWith = pWith;
138575 if( pSel->selFlags & SF_Recursive ){
138576 int rc;
138577 assert( pRecTerm!=0 );
138578 assert( (pRecTerm->selFlags & SF_Recursive)==0 );
138579 assert( pRecTerm->pNext!=0 );
138580 assert( (pRecTerm->pNext->selFlags & SF_Recursive)!=0 );
138581 assert( pRecTerm->pWith==0 );
138582 pRecTerm->pWith = pSel->pWith;
138583 rc = sqlite3WalkSelect(pWalker, p: pRecTerm);
138584 pRecTerm->pWith = 0;
138585 if( rc ){
138586 pParse->pWith = pSavedWith;
138587 return 2;
138588 }
138589 }else{
138590 if( sqlite3WalkSelect(pWalker, p: pSel) ){
138591 pParse->pWith = pSavedWith;
138592 return 2;
138593 }
138594 }
138595 pParse->pWith = pWith;
138596
138597 for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
138598 pEList = pLeft->pEList;
138599 if( pCte->pCols ){
138600 if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){
138601 sqlite3ErrorMsg(pParse, zFormat: "table %s has %d values for %d columns",
138602 pCte->zName, pEList->nExpr, pCte->pCols->nExpr
138603 );
138604 pParse->pWith = pSavedWith;
138605 return 2;
138606 }
138607 pEList = pCte->pCols;
138608 }
138609
138610 sqlite3ColumnsFromExprList(pParse, pEList, pnCol: &pTab->nCol, paCol: &pTab->aCol);
138611 if( bMayRecursive ){
138612 if( pSel->selFlags & SF_Recursive ){
138613 pCte->zCteErr = "multiple recursive references: %s";
138614 }else{
138615 pCte->zCteErr = "recursive reference in a subquery: %s";
138616 }
138617 sqlite3WalkSelect(pWalker, p: pSel);
138618 }
138619 pCte->zCteErr = 0;
138620 pParse->pWith = pSavedWith;
138621 return 1; /* Success */
138622 }
138623 return 0; /* No match */
138624}
138625#endif
138626
138627#ifndef SQLITE_OMIT_CTE
138628/*
138629** If the SELECT passed as the second argument has an associated WITH
138630** clause, pop it from the stack stored as part of the Parse object.
138631**
138632** This function is used as the xSelectCallback2() callback by
138633** sqlite3SelectExpand() when walking a SELECT tree to resolve table
138634** names and other FROM clause elements.
138635*/
138636SQLITE_PRIVATE void sqlite3SelectPopWith(Walker *pWalker, Select *p){
138637 Parse *pParse = pWalker->pParse;
138638 if( OK_IF_ALWAYS_TRUE(pParse->pWith) && p->pPrior==0 ){
138639 With *pWith = findRightmost(p)->pWith;
138640 if( pWith!=0 ){
138641 assert( pParse->pWith==pWith || pParse->nErr );
138642 pParse->pWith = pWith->pOuter;
138643 }
138644 }
138645}
138646#endif
138647
138648/*
138649** The SrcList_item structure passed as the second argument represents a
138650** sub-query in the FROM clause of a SELECT statement. This function
138651** allocates and populates the SrcList_item.pTab object. If successful,
138652** SQLITE_OK is returned. Otherwise, if an OOM error is encountered,
138653** SQLITE_NOMEM.
138654*/
138655SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse *pParse, SrcItem *pFrom){
138656 Select *pSel = pFrom->pSelect;
138657 Table *pTab;
138658
138659 assert( pSel );
138660 pFrom->pTab = pTab = sqlite3DbMallocZero(db: pParse->db, n: sizeof(Table));
138661 if( pTab==0 ) return SQLITE_NOMEM;
138662 pTab->nTabRef = 1;
138663 if( pFrom->zAlias ){
138664 pTab->zName = sqlite3DbStrDup(db: pParse->db, z: pFrom->zAlias);
138665 }else{
138666 pTab->zName = sqlite3MPrintf(db: pParse->db, zFormat: "subquery_%u", pSel->selId);
138667 }
138668 while( pSel->pPrior ){ pSel = pSel->pPrior; }
138669 sqlite3ColumnsFromExprList(pParse, pEList: pSel->pEList,pnCol: &pTab->nCol,paCol: &pTab->aCol);
138670 pTab->iPKey = -1;
138671 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
138672#ifndef SQLITE_ALLOW_ROWID_IN_VIEW
138673 /* The usual case - do not allow ROWID on a subquery */
138674 pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
138675#else
138676 pTab->tabFlags |= TF_Ephemeral; /* Legacy compatibility mode */
138677#endif
138678
138679
138680 return pParse->nErr ? SQLITE_ERROR : SQLITE_OK;
138681}
138682
138683/*
138684** This routine is a Walker callback for "expanding" a SELECT statement.
138685** "Expanding" means to do the following:
138686**
138687** (1) Make sure VDBE cursor numbers have been assigned to every
138688** element of the FROM clause.
138689**
138690** (2) Fill in the pTabList->a[].pTab fields in the SrcList that
138691** defines FROM clause. When views appear in the FROM clause,
138692** fill pTabList->a[].pSelect with a copy of the SELECT statement
138693** that implements the view. A copy is made of the view's SELECT
138694** statement so that we can freely modify or delete that statement
138695** without worrying about messing up the persistent representation
138696** of the view.
138697**
138698** (3) Add terms to the WHERE clause to accommodate the NATURAL keyword
138699** on joins and the ON and USING clause of joins.
138700**
138701** (4) Scan the list of columns in the result set (pEList) looking
138702** for instances of the "*" operator or the TABLE.* operator.
138703** If found, expand each "*" to be every column in every table
138704** and TABLE.* to be every column in TABLE.
138705**
138706*/
138707static int selectExpander(Walker *pWalker, Select *p){
138708 Parse *pParse = pWalker->pParse;
138709 int i, j, k, rc;
138710 SrcList *pTabList;
138711 ExprList *pEList;
138712 SrcItem *pFrom;
138713 sqlite3 *db = pParse->db;
138714 Expr *pE, *pRight, *pExpr;
138715 u16 selFlags = p->selFlags;
138716 u32 elistFlags = 0;
138717
138718 p->selFlags |= SF_Expanded;
138719 if( db->mallocFailed ){
138720 return WRC_Abort;
138721 }
138722 assert( p->pSrc!=0 );
138723 if( (selFlags & SF_Expanded)!=0 ){
138724 return WRC_Prune;
138725 }
138726 if( pWalker->eCode ){
138727 /* Renumber selId because it has been copied from a view */
138728 p->selId = ++pParse->nSelect;
138729 }
138730 pTabList = p->pSrc;
138731 pEList = p->pEList;
138732 if( pParse->pWith && (p->selFlags & SF_View) ){
138733 if( p->pWith==0 ){
138734 p->pWith = (With*)sqlite3DbMallocZero(db, n: sizeof(With));
138735 if( p->pWith==0 ){
138736 return WRC_Abort;
138737 }
138738 }
138739 p->pWith->bView = 1;
138740 }
138741 sqlite3WithPush(pParse, pWith: p->pWith, bFree: 0);
138742
138743 /* Make sure cursor numbers have been assigned to all entries in
138744 ** the FROM clause of the SELECT statement.
138745 */
138746 sqlite3SrcListAssignCursors(pParse, pList: pTabList);
138747
138748 /* Look up every table named in the FROM clause of the select. If
138749 ** an entry of the FROM clause is a subquery instead of a table or view,
138750 ** then create a transient table structure to describe the subquery.
138751 */
138752 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
138753 Table *pTab;
138754 assert( pFrom->fg.isRecursive==0 || pFrom->pTab!=0 );
138755 if( pFrom->pTab ) continue;
138756 assert( pFrom->fg.isRecursive==0 );
138757 if( pFrom->zName==0 ){
138758#ifndef SQLITE_OMIT_SUBQUERY
138759 Select *pSel = pFrom->pSelect;
138760 /* A sub-query in the FROM clause of a SELECT */
138761 assert( pSel!=0 );
138762 assert( pFrom->pTab==0 );
138763 if( sqlite3WalkSelect(pWalker, p: pSel) ) return WRC_Abort;
138764 if( sqlite3ExpandSubquery(pParse, pFrom) ) return WRC_Abort;
138765#endif
138766#ifndef SQLITE_OMIT_CTE
138767 }else if( (rc = resolveFromTermToCte(pParse, pWalker, pFrom))!=0 ){
138768 if( rc>1 ) return WRC_Abort;
138769 pTab = pFrom->pTab;
138770 assert( pTab!=0 );
138771#endif
138772 }else{
138773 /* An ordinary table or view name in the FROM clause */
138774 assert( pFrom->pTab==0 );
138775 pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, flags: 0, p: pFrom);
138776 if( pTab==0 ) return WRC_Abort;
138777 if( pTab->nTabRef>=0xffff ){
138778 sqlite3ErrorMsg(pParse, zFormat: "too many references to \"%s\": max 65535",
138779 pTab->zName);
138780 pFrom->pTab = 0;
138781 return WRC_Abort;
138782 }
138783 pTab->nTabRef++;
138784 if( !IsVirtual(pTab) && cannotBeFunction(pParse, pFrom) ){
138785 return WRC_Abort;
138786 }
138787#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
138788 if( !IsOrdinaryTable(pTab) ){
138789 i16 nCol;
138790 u8 eCodeOrig = pWalker->eCode;
138791 if( sqlite3ViewGetColumnNames(pParse, pTable: pTab) ) return WRC_Abort;
138792 assert( pFrom->pSelect==0 );
138793 if( IsView(pTab) ){
138794 if( (db->flags & SQLITE_EnableView)==0
138795 && pTab->pSchema!=db->aDb[1].pSchema
138796 ){
138797 sqlite3ErrorMsg(pParse, zFormat: "access to view \"%s\" prohibited",
138798 pTab->zName);
138799 }
138800 pFrom->pSelect = sqlite3SelectDup(db, pDup: pTab->u.view.pSelect, flags: 0);
138801 }
138802#ifndef SQLITE_OMIT_VIRTUALTABLE
138803 else if( ALWAYS(IsVirtual(pTab))
138804 && pFrom->fg.fromDDL
138805 && ALWAYS(pTab->u.vtab.p!=0)
138806 && pTab->u.vtab.p->eVtabRisk > ((db->flags & SQLITE_TrustedSchema)!=0)
138807 ){
138808 sqlite3ErrorMsg(pParse, zFormat: "unsafe use of virtual table \"%s\"",
138809 pTab->zName);
138810 }
138811 assert( SQLITE_VTABRISK_Normal==1 && SQLITE_VTABRISK_High==2 );
138812#endif
138813 nCol = pTab->nCol;
138814 pTab->nCol = -1;
138815 pWalker->eCode = 1; /* Turn on Select.selId renumbering */
138816 sqlite3WalkSelect(pWalker, p: pFrom->pSelect);
138817 pWalker->eCode = eCodeOrig;
138818 pTab->nCol = nCol;
138819 }
138820#endif
138821 }
138822
138823 /* Locate the index named by the INDEXED BY clause, if any. */
138824 if( pFrom->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pFrom) ){
138825 return WRC_Abort;
138826 }
138827 }
138828
138829 /* Process NATURAL keywords, and ON and USING clauses of joins.
138830 */
138831 if( pParse->nErr || db->mallocFailed || sqliteProcessJoin(pParse, p) ){
138832 return WRC_Abort;
138833 }
138834
138835 /* For every "*" that occurs in the column list, insert the names of
138836 ** all columns in all tables. And for every TABLE.* insert the names
138837 ** of all columns in TABLE. The parser inserted a special expression
138838 ** with the TK_ASTERISK operator for each "*" that it found in the column
138839 ** list. The following code just has to locate the TK_ASTERISK
138840 ** expressions and expand each one to the list of all columns in
138841 ** all tables.
138842 **
138843 ** The first loop just checks to see if there are any "*" operators
138844 ** that need expanding.
138845 */
138846 for(k=0; k<pEList->nExpr; k++){
138847 pE = pEList->a[k].pExpr;
138848 if( pE->op==TK_ASTERISK ) break;
138849 assert( pE->op!=TK_DOT || pE->pRight!=0 );
138850 assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
138851 if( pE->op==TK_DOT && pE->pRight->op==TK_ASTERISK ) break;
138852 elistFlags |= pE->flags;
138853 }
138854 if( k<pEList->nExpr ){
138855 /*
138856 ** If we get here it means the result set contains one or more "*"
138857 ** operators that need to be expanded. Loop through each expression
138858 ** in the result set and expand them one by one.
138859 */
138860 struct ExprList_item *a = pEList->a;
138861 ExprList *pNew = 0;
138862 int flags = pParse->db->flags;
138863 int longNames = (flags & SQLITE_FullColNames)!=0
138864 && (flags & SQLITE_ShortColNames)==0;
138865
138866 for(k=0; k<pEList->nExpr; k++){
138867 pE = a[k].pExpr;
138868 elistFlags |= pE->flags;
138869 pRight = pE->pRight;
138870 assert( pE->op!=TK_DOT || pRight!=0 );
138871 if( pE->op!=TK_ASTERISK
138872 && (pE->op!=TK_DOT || pRight->op!=TK_ASTERISK)
138873 ){
138874 /* This particular expression does not need to be expanded.
138875 */
138876 pNew = sqlite3ExprListAppend(pParse, pList: pNew, pExpr: a[k].pExpr);
138877 if( pNew ){
138878 pNew->a[pNew->nExpr-1].zEName = a[k].zEName;
138879 pNew->a[pNew->nExpr-1].eEName = a[k].eEName;
138880 a[k].zEName = 0;
138881 }
138882 a[k].pExpr = 0;
138883 }else{
138884 /* This expression is a "*" or a "TABLE.*" and needs to be
138885 ** expanded. */
138886 int tableSeen = 0; /* Set to 1 when TABLE matches */
138887 char *zTName = 0; /* text of name of TABLE */
138888 if( pE->op==TK_DOT ){
138889 assert( pE->pLeft!=0 );
138890 assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
138891 zTName = pE->pLeft->u.zToken;
138892 }
138893 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
138894 Table *pTab = pFrom->pTab;
138895 Select *pSub = pFrom->pSelect;
138896 char *zTabName = pFrom->zAlias;
138897 const char *zSchemaName = 0;
138898 int iDb;
138899 if( zTabName==0 ){
138900 zTabName = pTab->zName;
138901 }
138902 if( db->mallocFailed ) break;
138903 if( pSub==0 || (pSub->selFlags & SF_NestedFrom)==0 ){
138904 pSub = 0;
138905 if( zTName && sqlite3StrICmp(zLeft: zTName, zRight: zTabName)!=0 ){
138906 continue;
138907 }
138908 iDb = sqlite3SchemaToIndex(db, pSchema: pTab->pSchema);
138909 zSchemaName = iDb>=0 ? db->aDb[iDb].zDbSName : "*";
138910 }
138911 for(j=0; j<pTab->nCol; j++){
138912 char *zName = pTab->aCol[j].zCnName;
138913 char *zColname; /* The computed column name */
138914 char *zToFree; /* Malloced string that needs to be freed */
138915 Token sColname; /* Computed column name as a token */
138916
138917 assert( zName );
138918 if( zTName && pSub
138919 && sqlite3MatchEName(pItem: &pSub->pEList->a[j], zCol: 0, zTab: zTName, zDb: 0)==0
138920 ){
138921 continue;
138922 }
138923
138924 /* If a column is marked as 'hidden', omit it from the expanded
138925 ** result-set list unless the SELECT has the SF_IncludeHidden
138926 ** bit set.
138927 */
138928 if( (p->selFlags & SF_IncludeHidden)==0
138929 && IsHiddenColumn(&pTab->aCol[j])
138930 ){
138931 continue;
138932 }
138933 tableSeen = 1;
138934
138935 if( i>0 && zTName==0 ){
138936 if( (pFrom->fg.jointype & JT_NATURAL)!=0
138937 && tableAndColumnIndex(pSrc: pTabList, N: i, zCol: zName, piTab: 0, piCol: 0, bIgnoreHidden: 1)
138938 ){
138939 /* In a NATURAL join, omit the join columns from the
138940 ** table to the right of the join */
138941 continue;
138942 }
138943 if( sqlite3IdListIndex(pList: pFrom->pUsing, zName)>=0 ){
138944 /* In a join with a USING clause, omit columns in the
138945 ** using clause from the table on the right. */
138946 continue;
138947 }
138948 }
138949 pRight = sqlite3Expr(db, TK_ID, zToken: zName);
138950 zColname = zName;
138951 zToFree = 0;
138952 if( longNames || pTabList->nSrc>1 ){
138953 Expr *pLeft;
138954 pLeft = sqlite3Expr(db, TK_ID, zToken: zTabName);
138955 pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
138956 if( zSchemaName ){
138957 pLeft = sqlite3Expr(db, TK_ID, zToken: zSchemaName);
138958 pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight: pExpr);
138959 }
138960 if( longNames ){
138961 zColname = sqlite3MPrintf(db, zFormat: "%s.%s", zTabName, zName);
138962 zToFree = zColname;
138963 }
138964 }else{
138965 pExpr = pRight;
138966 }
138967 pNew = sqlite3ExprListAppend(pParse, pList: pNew, pExpr);
138968 sqlite3TokenInit(p: &sColname, z: zColname);
138969 sqlite3ExprListSetName(pParse, pList: pNew, pName: &sColname, dequote: 0);
138970 if( pNew && (p->selFlags & SF_NestedFrom)!=0 && !IN_RENAME_OBJECT ){
138971 struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
138972 sqlite3DbFree(db, p: pX->zEName);
138973 if( pSub ){
138974 pX->zEName = sqlite3DbStrDup(db, z: pSub->pEList->a[j].zEName);
138975 testcase( pX->zEName==0 );
138976 }else{
138977 pX->zEName = sqlite3MPrintf(db, zFormat: "%s.%s.%s",
138978 zSchemaName, zTabName, zColname);
138979 testcase( pX->zEName==0 );
138980 }
138981 pX->eEName = ENAME_TAB;
138982 }
138983 sqlite3DbFree(db, p: zToFree);
138984 }
138985 }
138986 if( !tableSeen ){
138987 if( zTName ){
138988 sqlite3ErrorMsg(pParse, zFormat: "no such table: %s", zTName);
138989 }else{
138990 sqlite3ErrorMsg(pParse, zFormat: "no tables specified");
138991 }
138992 }
138993 }
138994 }
138995 sqlite3ExprListDelete(db, pList: pEList);
138996 p->pEList = pNew;
138997 }
138998 if( p->pEList ){
138999 if( p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
139000 sqlite3ErrorMsg(pParse, zFormat: "too many columns in result set");
139001 return WRC_Abort;
139002 }
139003 if( (elistFlags & (EP_HasFunc|EP_Subquery))!=0 ){
139004 p->selFlags |= SF_ComplexResult;
139005 }
139006 }
139007 return WRC_Continue;
139008}
139009
139010#if SQLITE_DEBUG
139011/*
139012** Always assert. This xSelectCallback2 implementation proves that the
139013** xSelectCallback2 is never invoked.
139014*/
139015SQLITE_PRIVATE void sqlite3SelectWalkAssert2(Walker *NotUsed, Select *NotUsed2){
139016 UNUSED_PARAMETER2(NotUsed, NotUsed2);
139017 assert( 0 );
139018}
139019#endif
139020/*
139021** This routine "expands" a SELECT statement and all of its subqueries.
139022** For additional information on what it means to "expand" a SELECT
139023** statement, see the comment on the selectExpand worker callback above.
139024**
139025** Expanding a SELECT statement is the first step in processing a
139026** SELECT statement. The SELECT statement must be expanded before
139027** name resolution is performed.
139028**
139029** If anything goes wrong, an error message is written into pParse.
139030** The calling function can detect the problem by looking at pParse->nErr
139031** and/or pParse->db->mallocFailed.
139032*/
139033static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
139034 Walker w;
139035 w.xExprCallback = sqlite3ExprWalkNoop;
139036 w.pParse = pParse;
139037 if( OK_IF_ALWAYS_TRUE(pParse->hasCompound) ){
139038 w.xSelectCallback = convertCompoundSelectToSubquery;
139039 w.xSelectCallback2 = 0;
139040 sqlite3WalkSelect(pWalker: &w, p: pSelect);
139041 }
139042 w.xSelectCallback = selectExpander;
139043 w.xSelectCallback2 = sqlite3SelectPopWith;
139044 w.eCode = 0;
139045 sqlite3WalkSelect(pWalker: &w, p: pSelect);
139046}
139047
139048
139049#ifndef SQLITE_OMIT_SUBQUERY
139050/*
139051** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
139052** interface.
139053**
139054** For each FROM-clause subquery, add Column.zType and Column.zColl
139055** information to the Table structure that represents the result set
139056** of that subquery.
139057**
139058** The Table structure that represents the result set was constructed
139059** by selectExpander() but the type and collation information was omitted
139060** at that point because identifiers had not yet been resolved. This
139061** routine is called after identifier resolution.
139062*/
139063static void selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
139064 Parse *pParse;
139065 int i;
139066 SrcList *pTabList;
139067 SrcItem *pFrom;
139068
139069 assert( p->selFlags & SF_Resolved );
139070 if( p->selFlags & SF_HasTypeInfo ) return;
139071 p->selFlags |= SF_HasTypeInfo;
139072 pParse = pWalker->pParse;
139073 pTabList = p->pSrc;
139074 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
139075 Table *pTab = pFrom->pTab;
139076 assert( pTab!=0 );
139077 if( (pTab->tabFlags & TF_Ephemeral)!=0 ){
139078 /* A sub-query in the FROM clause of a SELECT */
139079 Select *pSel = pFrom->pSelect;
139080 if( pSel ){
139081 while( pSel->pPrior ) pSel = pSel->pPrior;
139082 sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSelect: pSel,
139083 SQLITE_AFF_NONE);
139084 }
139085 }
139086 }
139087}
139088#endif
139089
139090
139091/*
139092** This routine adds datatype and collating sequence information to
139093** the Table structures of all FROM-clause subqueries in a
139094** SELECT statement.
139095**
139096** Use this routine after name resolution.
139097*/
139098static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
139099#ifndef SQLITE_OMIT_SUBQUERY
139100 Walker w;
139101 w.xSelectCallback = sqlite3SelectWalkNoop;
139102 w.xSelectCallback2 = selectAddSubqueryTypeInfo;
139103 w.xExprCallback = sqlite3ExprWalkNoop;
139104 w.pParse = pParse;
139105 sqlite3WalkSelect(pWalker: &w, p: pSelect);
139106#endif
139107}
139108
139109
139110/*
139111** This routine sets up a SELECT statement for processing. The
139112** following is accomplished:
139113**
139114** * VDBE Cursor numbers are assigned to all FROM-clause terms.
139115** * Ephemeral Table objects are created for all FROM-clause subqueries.
139116** * ON and USING clauses are shifted into WHERE statements
139117** * Wildcards "*" and "TABLE.*" in result sets are expanded.
139118** * Identifiers in expression are matched to tables.
139119**
139120** This routine acts recursively on all subqueries within the SELECT.
139121*/
139122SQLITE_PRIVATE void sqlite3SelectPrep(
139123 Parse *pParse, /* The parser context */
139124 Select *p, /* The SELECT statement being coded. */
139125 NameContext *pOuterNC /* Name context for container */
139126){
139127 assert( p!=0 || pParse->db->mallocFailed );
139128 if( pParse->db->mallocFailed ) return;
139129 if( p->selFlags & SF_HasTypeInfo ) return;
139130 sqlite3SelectExpand(pParse, pSelect: p);
139131 if( pParse->nErr || pParse->db->mallocFailed ) return;
139132 sqlite3ResolveSelectNames(pParse, p, pOuterNC);
139133 if( pParse->nErr || pParse->db->mallocFailed ) return;
139134 sqlite3SelectAddTypeInfo(pParse, pSelect: p);
139135}
139136
139137/*
139138** Reset the aggregate accumulator.
139139**
139140** The aggregate accumulator is a set of memory cells that hold
139141** intermediate results while calculating an aggregate. This
139142** routine generates code that stores NULLs in all of those memory
139143** cells.
139144*/
139145static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
139146 Vdbe *v = pParse->pVdbe;
139147 int i;
139148 struct AggInfo_func *pFunc;
139149 int nReg = pAggInfo->nFunc + pAggInfo->nColumn;
139150 if( nReg==0 ) return;
139151 if( pParse->nErr || pParse->db->mallocFailed ) return;
139152#ifdef SQLITE_DEBUG
139153 /* Verify that all AggInfo registers are within the range specified by
139154 ** AggInfo.mnReg..AggInfo.mxReg */
139155 assert( nReg==pAggInfo->mxReg-pAggInfo->mnReg+1 );
139156 for(i=0; i<pAggInfo->nColumn; i++){
139157 assert( pAggInfo->aCol[i].iMem>=pAggInfo->mnReg
139158 && pAggInfo->aCol[i].iMem<=pAggInfo->mxReg );
139159 }
139160 for(i=0; i<pAggInfo->nFunc; i++){
139161 assert( pAggInfo->aFunc[i].iMem>=pAggInfo->mnReg
139162 && pAggInfo->aFunc[i].iMem<=pAggInfo->mxReg );
139163 }
139164#endif
139165 sqlite3VdbeAddOp3(p: v, OP_Null, p1: 0, p2: pAggInfo->mnReg, p3: pAggInfo->mxReg);
139166 for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
139167 if( pFunc->iDistinct>=0 ){
139168 Expr *pE = pFunc->pFExpr;
139169 assert( ExprUseXList(pE) );
139170 if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
139171 sqlite3ErrorMsg(pParse, zFormat: "DISTINCT aggregates must have exactly one "
139172 "argument");
139173 pFunc->iDistinct = -1;
139174 }else{
139175 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pList: pE->x.pList,iStart: 0,nExtra: 0);
139176 pFunc->iDistAddr = sqlite3VdbeAddOp4(p: v, OP_OpenEphemeral,
139177 p1: pFunc->iDistinct, p2: 0, p3: 0, zP4: (char*)pKeyInfo, P4_KEYINFO);
139178 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s(DISTINCT)",
139179 pFunc->pFunc->zName));
139180 }
139181 }
139182 }
139183}
139184
139185/*
139186** Invoke the OP_AggFinalize opcode for every aggregate function
139187** in the AggInfo structure.
139188*/
139189static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
139190 Vdbe *v = pParse->pVdbe;
139191 int i;
139192 struct AggInfo_func *pF;
139193 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
139194 ExprList *pList;
139195 assert( ExprUseXList(pF->pFExpr) );
139196 pList = pF->pFExpr->x.pList;
139197 sqlite3VdbeAddOp2(p: v, OP_AggFinal, p1: pF->iMem, p2: pList ? pList->nExpr : 0);
139198 sqlite3VdbeAppendP4(p: v, pP4: pF->pFunc, P4_FUNCDEF);
139199 }
139200}
139201
139202
139203/*
139204** Update the accumulator memory cells for an aggregate based on
139205** the current cursor position.
139206**
139207** If regAcc is non-zero and there are no min() or max() aggregates
139208** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator
139209** registers if register regAcc contains 0. The caller will take care
139210** of setting and clearing regAcc.
139211*/
139212static void updateAccumulator(
139213 Parse *pParse,
139214 int regAcc,
139215 AggInfo *pAggInfo,
139216 int eDistinctType
139217){
139218 Vdbe *v = pParse->pVdbe;
139219 int i;
139220 int regHit = 0;
139221 int addrHitTest = 0;
139222 struct AggInfo_func *pF;
139223 struct AggInfo_col *pC;
139224
139225 pAggInfo->directMode = 1;
139226 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
139227 int nArg;
139228 int addrNext = 0;
139229 int regAgg;
139230 ExprList *pList;
139231 assert( ExprUseXList(pF->pFExpr) );
139232 assert( !IsWindowFunc(pF->pFExpr) );
139233 pList = pF->pFExpr->x.pList;
139234 if( ExprHasProperty(pF->pFExpr, EP_WinFunc) ){
139235 Expr *pFilter = pF->pFExpr->y.pWin->pFilter;
139236 if( pAggInfo->nAccumulator
139237 && (pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
139238 && regAcc
139239 ){
139240 /* If regAcc==0, there there exists some min() or max() function
139241 ** without a FILTER clause that will ensure the magnet registers
139242 ** are populated. */
139243 if( regHit==0 ) regHit = ++pParse->nMem;
139244 /* If this is the first row of the group (regAcc contains 0), clear the
139245 ** "magnet" register regHit so that the accumulator registers
139246 ** are populated if the FILTER clause jumps over the the
139247 ** invocation of min() or max() altogether. Or, if this is not
139248 ** the first row (regAcc contains 1), set the magnet register so that
139249 ** the accumulators are not populated unless the min()/max() is invoked
139250 ** and indicates that they should be. */
139251 sqlite3VdbeAddOp2(p: v, OP_Copy, p1: regAcc, p2: regHit);
139252 }
139253 addrNext = sqlite3VdbeMakeLabel(pParse);
139254 sqlite3ExprIfFalse(pParse, pExpr: pFilter, dest: addrNext, SQLITE_JUMPIFNULL);
139255 }
139256 if( pList ){
139257 nArg = pList->nExpr;
139258 regAgg = sqlite3GetTempRange(pParse, nReg: nArg);
139259 sqlite3ExprCodeExprList(pParse, pList, target: regAgg, srcReg: 0, SQLITE_ECEL_DUP);
139260 }else{
139261 nArg = 0;
139262 regAgg = 0;
139263 }
139264 if( pF->iDistinct>=0 && pList ){
139265 if( addrNext==0 ){
139266 addrNext = sqlite3VdbeMakeLabel(pParse);
139267 }
139268 pF->iDistinct = codeDistinct(pParse, eTnctType: eDistinctType,
139269 iTab: pF->iDistinct, addrRepeat: addrNext, pEList: pList, regElem: regAgg);
139270 }
139271 if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
139272 CollSeq *pColl = 0;
139273 struct ExprList_item *pItem;
139274 int j;
139275 assert( pList!=0 ); /* pList!=0 if pF->pFunc has NEEDCOLL */
139276 for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
139277 pColl = sqlite3ExprCollSeq(pParse, pExpr: pItem->pExpr);
139278 }
139279 if( !pColl ){
139280 pColl = pParse->db->pDfltColl;
139281 }
139282 if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
139283 sqlite3VdbeAddOp4(p: v, OP_CollSeq, p1: regHit, p2: 0, p3: 0, zP4: (char *)pColl, P4_COLLSEQ);
139284 }
139285 sqlite3VdbeAddOp3(p: v, OP_AggStep, p1: 0, p2: regAgg, p3: pF->iMem);
139286 sqlite3VdbeAppendP4(p: v, pP4: pF->pFunc, P4_FUNCDEF);
139287 sqlite3VdbeChangeP5(p: v, p5: (u8)nArg);
139288 sqlite3ReleaseTempRange(pParse, iReg: regAgg, nReg: nArg);
139289 if( addrNext ){
139290 sqlite3VdbeResolveLabel(v, x: addrNext);
139291 }
139292 }
139293 if( regHit==0 && pAggInfo->nAccumulator ){
139294 regHit = regAcc;
139295 }
139296 if( regHit ){
139297 addrHitTest = sqlite3VdbeAddOp1(p: v, OP_If, p1: regHit); VdbeCoverage(v);
139298 }
139299 for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
139300 sqlite3ExprCode(pParse, pExpr: pC->pCExpr, target: pC->iMem);
139301 }
139302
139303 pAggInfo->directMode = 0;
139304 if( addrHitTest ){
139305 sqlite3VdbeJumpHereOrPopInst(p: v, addr: addrHitTest);
139306 }
139307}
139308
139309/*
139310** Add a single OP_Explain instruction to the VDBE to explain a simple
139311** count(*) query ("SELECT count(*) FROM pTab").
139312*/
139313#ifndef SQLITE_OMIT_EXPLAIN
139314static void explainSimpleCount(
139315 Parse *pParse, /* Parse context */
139316 Table *pTab, /* Table being queried */
139317 Index *pIdx /* Index used to optimize scan, or NULL */
139318){
139319 if( pParse->explain==2 ){
139320 int bCover = (pIdx!=0 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pIdx)));
139321 sqlite3VdbeExplain(pParse, bPush: 0, zFmt: "SCAN %s%s%s",
139322 pTab->zName,
139323 bCover ? " USING COVERING INDEX " : "",
139324 bCover ? pIdx->zName : ""
139325 );
139326 }
139327}
139328#else
139329# define explainSimpleCount(a,b,c)
139330#endif
139331
139332/*
139333** sqlite3WalkExpr() callback used by havingToWhere().
139334**
139335** If the node passed to the callback is a TK_AND node, return
139336** WRC_Continue to tell sqlite3WalkExpr() to iterate through child nodes.
139337**
139338** Otherwise, return WRC_Prune. In this case, also check if the
139339** sub-expression matches the criteria for being moved to the WHERE
139340** clause. If so, add it to the WHERE clause and replace the sub-expression
139341** within the HAVING expression with a constant "1".
139342*/
139343static int havingToWhereExprCb(Walker *pWalker, Expr *pExpr){
139344 if( pExpr->op!=TK_AND ){
139345 Select *pS = pWalker->u.pSelect;
139346 /* This routine is called before the HAVING clause of the current
139347 ** SELECT is analyzed for aggregates. So if pExpr->pAggInfo is set
139348 ** here, it indicates that the expression is a correlated reference to a
139349 ** column from an outer aggregate query, or an aggregate function that
139350 ** belongs to an outer query. Do not move the expression to the WHERE
139351 ** clause in this obscure case, as doing so may corrupt the outer Select
139352 ** statements AggInfo structure. */
139353 if( sqlite3ExprIsConstantOrGroupBy(pParse: pWalker->pParse, p: pExpr, pGroupBy: pS->pGroupBy)
139354 && ExprAlwaysFalse(pExpr)==0
139355 && pExpr->pAggInfo==0
139356 ){
139357 sqlite3 *db = pWalker->pParse->db;
139358 Expr *pNew = sqlite3Expr(db, TK_INTEGER, zToken: "1");
139359 if( pNew ){
139360 Expr *pWhere = pS->pWhere;
139361 SWAP(Expr, *pNew, *pExpr);
139362 pNew = sqlite3ExprAnd(pParse: pWalker->pParse, pLeft: pWhere, pRight: pNew);
139363 pS->pWhere = pNew;
139364 pWalker->eCode = 1;
139365 }
139366 }
139367 return WRC_Prune;
139368 }
139369 return WRC_Continue;
139370}
139371
139372/*
139373** Transfer eligible terms from the HAVING clause of a query, which is
139374** processed after grouping, to the WHERE clause, which is processed before
139375** grouping. For example, the query:
139376**
139377** SELECT * FROM <tables> WHERE a=? GROUP BY b HAVING b=? AND c=?
139378**
139379** can be rewritten as:
139380**
139381** SELECT * FROM <tables> WHERE a=? AND b=? GROUP BY b HAVING c=?
139382**
139383** A term of the HAVING expression is eligible for transfer if it consists
139384** entirely of constants and expressions that are also GROUP BY terms that
139385** use the "BINARY" collation sequence.
139386*/
139387static void havingToWhere(Parse *pParse, Select *p){
139388 Walker sWalker;
139389 memset(s: &sWalker, c: 0, n: sizeof(sWalker));
139390 sWalker.pParse = pParse;
139391 sWalker.xExprCallback = havingToWhereExprCb;
139392 sWalker.u.pSelect = p;
139393 sqlite3WalkExpr(pWalker: &sWalker, pExpr: p->pHaving);
139394#if SELECTTRACE_ENABLED
139395 if( sWalker.eCode && (sqlite3SelectTrace & 0x100)!=0 ){
139396 SELECTTRACE(0x100,pParse,p,("Move HAVING terms into WHERE:\n"));
139397 sqlite3TreeViewSelect(0, p, 0);
139398 }
139399#endif
139400}
139401
139402/*
139403** Check to see if the pThis entry of pTabList is a self-join of a prior view.
139404** If it is, then return the SrcList_item for the prior view. If it is not,
139405** then return 0.
139406*/
139407static SrcItem *isSelfJoinView(
139408 SrcList *pTabList, /* Search for self-joins in this FROM clause */
139409 SrcItem *pThis /* Search for prior reference to this subquery */
139410){
139411 SrcItem *pItem;
139412 assert( pThis->pSelect!=0 );
139413 if( pThis->pSelect->selFlags & SF_PushDown ) return 0;
139414 for(pItem = pTabList->a; pItem<pThis; pItem++){
139415 Select *pS1;
139416 if( pItem->pSelect==0 ) continue;
139417 if( pItem->fg.viaCoroutine ) continue;
139418 if( pItem->zName==0 ) continue;
139419 assert( pItem->pTab!=0 );
139420 assert( pThis->pTab!=0 );
139421 if( pItem->pTab->pSchema!=pThis->pTab->pSchema ) continue;
139422 if( sqlite3_stricmp(zLeft: pItem->zName, zRight: pThis->zName)!=0 ) continue;
139423 pS1 = pItem->pSelect;
139424 if( pItem->pTab->pSchema==0 && pThis->pSelect->selId!=pS1->selId ){
139425 /* The query flattener left two different CTE tables with identical
139426 ** names in the same FROM clause. */
139427 continue;
139428 }
139429 if( pItem->pSelect->selFlags & SF_PushDown ){
139430 /* The view was modified by some other optimization such as
139431 ** pushDownWhereTerms() */
139432 continue;
139433 }
139434 return pItem;
139435 }
139436 return 0;
139437}
139438
139439/*
139440** Deallocate a single AggInfo object
139441*/
139442static void agginfoFree(sqlite3 *db, AggInfo *p){
139443 sqlite3DbFree(db, p: p->aCol);
139444 sqlite3DbFree(db, p: p->aFunc);
139445 sqlite3DbFreeNN(db, p);
139446}
139447
139448#ifdef SQLITE_COUNTOFVIEW_OPTIMIZATION
139449/*
139450** Attempt to transform a query of the form
139451**
139452** SELECT count(*) FROM (SELECT x FROM t1 UNION ALL SELECT y FROM t2)
139453**
139454** Into this:
139455**
139456** SELECT (SELECT count(*) FROM t1)+(SELECT count(*) FROM t2)
139457**
139458** The transformation only works if all of the following are true:
139459**
139460** * The subquery is a UNION ALL of two or more terms
139461** * The subquery does not have a LIMIT clause
139462** * There is no WHERE or GROUP BY or HAVING clauses on the subqueries
139463** * The outer query is a simple count(*) with no WHERE clause or other
139464** extraneous syntax.
139465**
139466** Return TRUE if the optimization is undertaken.
139467*/
139468static int countOfViewOptimization(Parse *pParse, Select *p){
139469 Select *pSub, *pPrior;
139470 Expr *pExpr;
139471 Expr *pCount;
139472 sqlite3 *db;
139473 if( (p->selFlags & SF_Aggregate)==0 ) return 0; /* This is an aggregate */
139474 if( p->pEList->nExpr!=1 ) return 0; /* Single result column */
139475 if( p->pWhere ) return 0;
139476 if( p->pGroupBy ) return 0;
139477 pExpr = p->pEList->a[0].pExpr;
139478 if( pExpr->op!=TK_AGG_FUNCTION ) return 0; /* Result is an aggregate */
139479 assert( ExprUseUToken(pExpr) );
139480 if( sqlite3_stricmp(pExpr->u.zToken,"count") ) return 0; /* Is count() */
139481 assert( ExprUseXList(pExpr) );
139482 if( pExpr->x.pList!=0 ) return 0; /* Must be count(*) */
139483 if( p->pSrc->nSrc!=1 ) return 0; /* One table in FROM */
139484 pSub = p->pSrc->a[0].pSelect;
139485 if( pSub==0 ) return 0; /* The FROM is a subquery */
139486 if( pSub->pPrior==0 ) return 0; /* Must be a compound ry */
139487 do{
139488 if( pSub->op!=TK_ALL && pSub->pPrior ) return 0; /* Must be UNION ALL */
139489 if( pSub->pWhere ) return 0; /* No WHERE clause */
139490 if( pSub->pLimit ) return 0; /* No LIMIT clause */
139491 if( pSub->selFlags & SF_Aggregate ) return 0; /* Not an aggregate */
139492 pSub = pSub->pPrior; /* Repeat over compound */
139493 }while( pSub );
139494
139495 /* If we reach this point then it is OK to perform the transformation */
139496
139497 db = pParse->db;
139498 pCount = pExpr;
139499 pExpr = 0;
139500 pSub = p->pSrc->a[0].pSelect;
139501 p->pSrc->a[0].pSelect = 0;
139502 sqlite3SrcListDelete(db, p->pSrc);
139503 p->pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*p->pSrc));
139504 while( pSub ){
139505 Expr *pTerm;
139506 pPrior = pSub->pPrior;
139507 pSub->pPrior = 0;
139508 pSub->pNext = 0;
139509 pSub->selFlags |= SF_Aggregate;
139510 pSub->selFlags &= ~SF_Compound;
139511 pSub->nSelectRow = 0;
139512 sqlite3ExprListDelete(db, pSub->pEList);
139513 pTerm = pPrior ? sqlite3ExprDup(db, pCount, 0) : pCount;
139514 pSub->pEList = sqlite3ExprListAppend(pParse, 0, pTerm);
139515 pTerm = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
139516 sqlite3PExprAddSelect(pParse, pTerm, pSub);
139517 if( pExpr==0 ){
139518 pExpr = pTerm;
139519 }else{
139520 pExpr = sqlite3PExpr(pParse, TK_PLUS, pTerm, pExpr);
139521 }
139522 pSub = pPrior;
139523 }
139524 p->pEList->a[0].pExpr = pExpr;
139525 p->selFlags &= ~SF_Aggregate;
139526
139527#if SELECTTRACE_ENABLED
139528 if( sqlite3SelectTrace & 0x400 ){
139529 SELECTTRACE(0x400,pParse,p,("After count-of-view optimization:\n"));
139530 sqlite3TreeViewSelect(0, p, 0);
139531 }
139532#endif
139533 return 1;
139534}
139535#endif /* SQLITE_COUNTOFVIEW_OPTIMIZATION */
139536
139537/*
139538** Generate code for the SELECT statement given in the p argument.
139539**
139540** The results are returned according to the SelectDest structure.
139541** See comments in sqliteInt.h for further information.
139542**
139543** This routine returns the number of errors. If any errors are
139544** encountered, then an appropriate error message is left in
139545** pParse->zErrMsg.
139546**
139547** This routine does NOT free the Select structure passed in. The
139548** calling function needs to do that.
139549*/
139550SQLITE_PRIVATE int sqlite3Select(
139551 Parse *pParse, /* The parser context */
139552 Select *p, /* The SELECT statement being coded. */
139553 SelectDest *pDest /* What to do with the query results */
139554){
139555 int i, j; /* Loop counters */
139556 WhereInfo *pWInfo; /* Return from sqlite3WhereBegin() */
139557 Vdbe *v; /* The virtual machine under construction */
139558 int isAgg; /* True for select lists like "count(*)" */
139559 ExprList *pEList = 0; /* List of columns to extract. */
139560 SrcList *pTabList; /* List of tables to select from */
139561 Expr *pWhere; /* The WHERE clause. May be NULL */
139562 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
139563 Expr *pHaving; /* The HAVING clause. May be NULL */
139564 AggInfo *pAggInfo = 0; /* Aggregate information */
139565 int rc = 1; /* Value to return from this function */
139566 DistinctCtx sDistinct; /* Info on how to code the DISTINCT keyword */
139567 SortCtx sSort; /* Info on how to code the ORDER BY clause */
139568 int iEnd; /* Address of the end of the query */
139569 sqlite3 *db; /* The database connection */
139570 ExprList *pMinMaxOrderBy = 0; /* Added ORDER BY for min/max queries */
139571 u8 minMaxFlag; /* Flag for min/max queries */
139572
139573 db = pParse->db;
139574 v = sqlite3GetVdbe(pParse);
139575 if( p==0 || db->mallocFailed || pParse->nErr ){
139576 return 1;
139577 }
139578 if( sqlite3AuthCheck(pParse, SQLITE_SELECT, zArg1: 0, zArg2: 0, zArg3: 0) ) return 1;
139579#if SELECTTRACE_ENABLED
139580 SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->addrExplain));
139581 if( sqlite3SelectTrace & 0x100 ){
139582 sqlite3TreeViewSelect(0, p, 0);
139583 }
139584#endif
139585
139586 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
139587 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
139588 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue );
139589 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue );
139590 if( IgnorableDistinct(pDest) ){
139591 assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||
139592 pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard ||
139593 pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_DistFifo );
139594 /* All of these destinations are also able to ignore the ORDER BY clause */
139595 if( p->pOrderBy ){
139596#if SELECTTRACE_ENABLED
139597 SELECTTRACE(1,pParse,p, ("dropping superfluous ORDER BY:\n"));
139598 if( sqlite3SelectTrace & 0x100 ){
139599 sqlite3TreeViewExprList(0, p->pOrderBy, 0, "ORDERBY");
139600 }
139601#endif
139602 sqlite3ParserAddCleanup(pParse,
139603 xCleanup: (void(*)(sqlite3*,void*))sqlite3ExprListDelete,
139604 pPtr: p->pOrderBy);
139605 testcase( pParse->earlyCleanup );
139606 p->pOrderBy = 0;
139607 }
139608 p->selFlags &= ~SF_Distinct;
139609 p->selFlags |= SF_NoopOrderBy;
139610 }
139611 sqlite3SelectPrep(pParse, p, pOuterNC: 0);
139612 if( pParse->nErr || db->mallocFailed ){
139613 goto select_end;
139614 }
139615 assert( p->pEList!=0 );
139616#if SELECTTRACE_ENABLED
139617 if( sqlite3SelectTrace & 0x104 ){
139618 SELECTTRACE(0x104,pParse,p, ("after name resolution:\n"));
139619 sqlite3TreeViewSelect(0, p, 0);
139620 }
139621#endif
139622
139623 /* If the SF_UFSrcCheck flag is set, then this function is being called
139624 ** as part of populating the temp table for an UPDATE...FROM statement.
139625 ** In this case, it is an error if the target object (pSrc->a[0]) name
139626 ** or alias is duplicated within FROM clause (pSrc->a[1..n]).
139627 **
139628 ** Postgres disallows this case too. The reason is that some other
139629 ** systems handle this case differently, and not all the same way,
139630 ** which is just confusing. To avoid this, we follow PG's lead and
139631 ** disallow it altogether. */
139632 if( p->selFlags & SF_UFSrcCheck ){
139633 SrcItem *p0 = &p->pSrc->a[0];
139634 for(i=1; i<p->pSrc->nSrc; i++){
139635 SrcItem *p1 = &p->pSrc->a[i];
139636 if( p0->pTab==p1->pTab && 0==sqlite3_stricmp(zLeft: p0->zAlias, zRight: p1->zAlias) ){
139637 sqlite3ErrorMsg(pParse,
139638 zFormat: "target object/alias may not appear in FROM clause: %s",
139639 p0->zAlias ? p0->zAlias : p0->pTab->zName
139640 );
139641 goto select_end;
139642 }
139643 }
139644
139645 /* Clear the SF_UFSrcCheck flag. The check has already been performed,
139646 ** and leaving this flag set can cause errors if a compound sub-query
139647 ** in p->pSrc is flattened into this query and this function called
139648 ** again as part of compound SELECT processing. */
139649 p->selFlags &= ~SF_UFSrcCheck;
139650 }
139651
139652 if( pDest->eDest==SRT_Output ){
139653 sqlite3GenerateColumnNames(pParse, pSelect: p);
139654 }
139655
139656#ifndef SQLITE_OMIT_WINDOWFUNC
139657 if( sqlite3WindowRewrite(pParse, p) ){
139658 assert( db->mallocFailed || pParse->nErr>0 );
139659 goto select_end;
139660 }
139661#if SELECTTRACE_ENABLED
139662 if( p->pWin && (sqlite3SelectTrace & 0x108)!=0 ){
139663 SELECTTRACE(0x104,pParse,p, ("after window rewrite:\n"));
139664 sqlite3TreeViewSelect(0, p, 0);
139665 }
139666#endif
139667#endif /* SQLITE_OMIT_WINDOWFUNC */
139668 pTabList = p->pSrc;
139669 isAgg = (p->selFlags & SF_Aggregate)!=0;
139670 memset(s: &sSort, c: 0, n: sizeof(sSort));
139671 sSort.pOrderBy = p->pOrderBy;
139672
139673 /* Try to do various optimizations (flattening subqueries, and strength
139674 ** reduction of join operators) in the FROM clause up into the main query
139675 */
139676#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
139677 for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
139678 SrcItem *pItem = &pTabList->a[i];
139679 Select *pSub = pItem->pSelect;
139680 Table *pTab = pItem->pTab;
139681
139682 /* The expander should have already created transient Table objects
139683 ** even for FROM clause elements such as subqueries that do not correspond
139684 ** to a real table */
139685 assert( pTab!=0 );
139686
139687 /* Convert LEFT JOIN into JOIN if there are terms of the right table
139688 ** of the LEFT JOIN used in the WHERE clause.
139689 */
139690 if( (pItem->fg.jointype & JT_LEFT)!=0
139691 && sqlite3ExprImpliesNonNullRow(p: p->pWhere, iTab: pItem->iCursor)
139692 && OptimizationEnabled(db, SQLITE_SimplifyJoin)
139693 ){
139694 SELECTTRACE(0x100,pParse,p,
139695 ("LEFT-JOIN simplifies to JOIN on term %d\n",i));
139696 pItem->fg.jointype &= ~(JT_LEFT|JT_OUTER);
139697 unsetJoinExpr(p: p->pWhere, iTable: pItem->iCursor);
139698 }
139699
139700 /* No futher action if this term of the FROM clause is no a subquery */
139701 if( pSub==0 ) continue;
139702
139703 /* Catch mismatch in the declared columns of a view and the number of
139704 ** columns in the SELECT on the RHS */
139705 if( pTab->nCol!=pSub->pEList->nExpr ){
139706 sqlite3ErrorMsg(pParse, zFormat: "expected %d columns for '%s' but got %d",
139707 pTab->nCol, pTab->zName, pSub->pEList->nExpr);
139708 goto select_end;
139709 }
139710
139711 /* Do not try to flatten an aggregate subquery.
139712 **
139713 ** Flattening an aggregate subquery is only possible if the outer query
139714 ** is not a join. But if the outer query is not a join, then the subquery
139715 ** will be implemented as a co-routine and there is no advantage to
139716 ** flattening in that case.
139717 */
139718 if( (pSub->selFlags & SF_Aggregate)!=0 ) continue;
139719 assert( pSub->pGroupBy==0 );
139720
139721 /* If a FROM-clause subquery has an ORDER BY clause that is not
139722 ** really doing anything, then delete it now so that it does not
139723 ** interfere with query flattening. See the discussion at
139724 ** https://sqlite.org/forum/forumpost/2d76f2bcf65d256a
139725 **
139726 ** Beware of these cases where the ORDER BY clause may not be safely
139727 ** omitted:
139728 **
139729 ** (1) There is also a LIMIT clause
139730 ** (2) The subquery was added to help with window-function
139731 ** processing
139732 ** (3) The subquery is in the FROM clause of an UPDATE
139733 ** (4) The outer query uses an aggregate function other than
139734 ** the built-in count(), min(), or max().
139735 ** (5) The ORDER BY isn't going to accomplish anything because
139736 ** one of:
139737 ** (a) The outer query has a different ORDER BY clause
139738 ** (b) The subquery is part of a join
139739 ** See forum post 062d576715d277c8
139740 */
139741 if( pSub->pOrderBy!=0
139742 && (p->pOrderBy!=0 || pTabList->nSrc>1) /* Condition (5) */
139743 && pSub->pLimit==0 /* Condition (1) */
139744 && (pSub->selFlags & SF_OrderByReqd)==0 /* Condition (2) */
139745 && (p->selFlags & SF_OrderByReqd)==0 /* Condition (3) and (4) */
139746 && OptimizationEnabled(db, SQLITE_OmitOrderBy)
139747 ){
139748 SELECTTRACE(0x100,pParse,p,
139749 ("omit superfluous ORDER BY on %r FROM-clause subquery\n",i+1));
139750 sqlite3ExprListDelete(db, pList: pSub->pOrderBy);
139751 pSub->pOrderBy = 0;
139752 }
139753
139754 /* If the outer query contains a "complex" result set (that is,
139755 ** if the result set of the outer query uses functions or subqueries)
139756 ** and if the subquery contains an ORDER BY clause and if
139757 ** it will be implemented as a co-routine, then do not flatten. This
139758 ** restriction allows SQL constructs like this:
139759 **
139760 ** SELECT expensive_function(x)
139761 ** FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
139762 **
139763 ** The expensive_function() is only computed on the 10 rows that
139764 ** are output, rather than every row of the table.
139765 **
139766 ** The requirement that the outer query have a complex result set
139767 ** means that flattening does occur on simpler SQL constraints without
139768 ** the expensive_function() like:
139769 **
139770 ** SELECT x FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
139771 */
139772 if( pSub->pOrderBy!=0
139773 && i==0
139774 && (p->selFlags & SF_ComplexResult)!=0
139775 && (pTabList->nSrc==1
139776 || (pTabList->a[1].fg.jointype&(JT_LEFT|JT_CROSS))!=0)
139777 ){
139778 continue;
139779 }
139780
139781 if( flattenSubquery(pParse, p, iFrom: i, isAgg) ){
139782 if( pParse->nErr ) goto select_end;
139783 /* This subquery can be absorbed into its parent. */
139784 i = -1;
139785 }
139786 pTabList = p->pSrc;
139787 if( db->mallocFailed ) goto select_end;
139788 if( !IgnorableOrderby(pDest) ){
139789 sSort.pOrderBy = p->pOrderBy;
139790 }
139791 }
139792#endif
139793
139794#ifndef SQLITE_OMIT_COMPOUND_SELECT
139795 /* Handle compound SELECT statements using the separate multiSelect()
139796 ** procedure.
139797 */
139798 if( p->pPrior ){
139799 rc = multiSelect(pParse, p, pDest);
139800#if SELECTTRACE_ENABLED
139801 SELECTTRACE(0x1,pParse,p,("end compound-select processing\n"));
139802 if( (sqlite3SelectTrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)==0 ){
139803 sqlite3TreeViewSelect(0, p, 0);
139804 }
139805#endif
139806 if( p->pNext==0 ) ExplainQueryPlanPop(pParse);
139807 return rc;
139808 }
139809#endif
139810
139811 /* Do the WHERE-clause constant propagation optimization if this is
139812 ** a join. No need to speed time on this operation for non-join queries
139813 ** as the equivalent optimization will be handled by query planner in
139814 ** sqlite3WhereBegin().
139815 */
139816 if( p->pWhere!=0
139817 && p->pWhere->op==TK_AND
139818 && OptimizationEnabled(db, SQLITE_PropagateConst)
139819 && propagateConstants(pParse, p)
139820 ){
139821#if SELECTTRACE_ENABLED
139822 if( sqlite3SelectTrace & 0x100 ){
139823 SELECTTRACE(0x100,pParse,p,("After constant propagation:\n"));
139824 sqlite3TreeViewSelect(0, p, 0);
139825 }
139826#endif
139827 }else{
139828 SELECTTRACE(0x100,pParse,p,("Constant propagation not helpful\n"));
139829 }
139830
139831#ifdef SQLITE_COUNTOFVIEW_OPTIMIZATION
139832 if( OptimizationEnabled(db, SQLITE_QueryFlattener|SQLITE_CountOfView)
139833 && countOfViewOptimization(pParse, p)
139834 ){
139835 if( db->mallocFailed ) goto select_end;
139836 pEList = p->pEList;
139837 pTabList = p->pSrc;
139838 }
139839#endif
139840
139841 /* For each term in the FROM clause, do two things:
139842 ** (1) Authorized unreferenced tables
139843 ** (2) Generate code for all sub-queries
139844 */
139845 for(i=0; i<pTabList->nSrc; i++){
139846 SrcItem *pItem = &pTabList->a[i];
139847 SrcItem *pPrior;
139848 SelectDest dest;
139849 Select *pSub;
139850#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
139851 const char *zSavedAuthContext;
139852#endif
139853
139854 /* Issue SQLITE_READ authorizations with a fake column name for any
139855 ** tables that are referenced but from which no values are extracted.
139856 ** Examples of where these kinds of null SQLITE_READ authorizations
139857 ** would occur:
139858 **
139859 ** SELECT count(*) FROM t1; -- SQLITE_READ t1.""
139860 ** SELECT t1.* FROM t1, t2; -- SQLITE_READ t2.""
139861 **
139862 ** The fake column name is an empty string. It is possible for a table to
139863 ** have a column named by the empty string, in which case there is no way to
139864 ** distinguish between an unreferenced table and an actual reference to the
139865 ** "" column. The original design was for the fake column name to be a NULL,
139866 ** which would be unambiguous. But legacy authorization callbacks might
139867 ** assume the column name is non-NULL and segfault. The use of an empty
139868 ** string for the fake column name seems safer.
139869 */
139870 if( pItem->colUsed==0 && pItem->zName!=0 ){
139871 sqlite3AuthCheck(pParse, SQLITE_READ, zArg1: pItem->zName, zArg2: "", zArg3: pItem->zDatabase);
139872 }
139873
139874#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
139875 /* Generate code for all sub-queries in the FROM clause
139876 */
139877 pSub = pItem->pSelect;
139878 if( pSub==0 ) continue;
139879
139880 /* The code for a subquery should only be generated once. */
139881 assert( pItem->addrFillSub==0 );
139882
139883 /* Increment Parse.nHeight by the height of the largest expression
139884 ** tree referred to by this, the parent select. The child select
139885 ** may contain expression trees of at most
139886 ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
139887 ** more conservative than necessary, but much easier than enforcing
139888 ** an exact limit.
139889 */
139890 pParse->nHeight += sqlite3SelectExprHeight(p);
139891
139892 /* Make copies of constant WHERE-clause terms in the outer query down
139893 ** inside the subquery. This can help the subquery to run more efficiently.
139894 */
139895 if( OptimizationEnabled(db, SQLITE_PushDown)
139896 && (pItem->fg.isCte==0
139897 || (pItem->u2.pCteUse->eM10d!=M10d_Yes && pItem->u2.pCteUse->nUse<2))
139898 && pushDownWhereTerms(pParse, pSubq: pSub, pWhere: p->pWhere, iCursor: pItem->iCursor,
139899 isLeftJoin: (pItem->fg.jointype & JT_OUTER)!=0)
139900 ){
139901#if SELECTTRACE_ENABLED
139902 if( sqlite3SelectTrace & 0x100 ){
139903 SELECTTRACE(0x100,pParse,p,
139904 ("After WHERE-clause push-down into subquery %d:\n", pSub->selId));
139905 sqlite3TreeViewSelect(0, p, 0);
139906 }
139907#endif
139908 assert( pItem->pSelect && (pItem->pSelect->selFlags & SF_PushDown)!=0 );
139909 }else{
139910 SELECTTRACE(0x100,pParse,p,("Push-down not possible\n"));
139911 }
139912
139913 zSavedAuthContext = pParse->zAuthContext;
139914 pParse->zAuthContext = pItem->zName;
139915
139916 /* Generate code to implement the subquery
139917 **
139918 ** The subquery is implemented as a co-routine if:
139919 ** (1) the subquery is guaranteed to be the outer loop (so that
139920 ** it does not need to be computed more than once), and
139921 ** (2) the subquery is not a CTE that should be materialized
139922 **
139923 ** TODO: Are there other reasons beside (1) and (2) to use a co-routine
139924 ** implementation?
139925 */
139926 if( i==0
139927 && (pTabList->nSrc==1
139928 || (pTabList->a[1].fg.jointype&(JT_LEFT|JT_CROSS))!=0) /* (1) */
139929 && (pItem->fg.isCte==0 || pItem->u2.pCteUse->eM10d!=M10d_Yes) /* (2) */
139930 ){
139931 /* Implement a co-routine that will return a single row of the result
139932 ** set on each invocation.
139933 */
139934 int addrTop = sqlite3VdbeCurrentAddr(p: v)+1;
139935
139936 pItem->regReturn = ++pParse->nMem;
139937 sqlite3VdbeAddOp3(p: v, OP_InitCoroutine, p1: pItem->regReturn, p2: 0, p3: addrTop);
139938 VdbeComment((v, "%!S", pItem));
139939 pItem->addrFillSub = addrTop;
139940 sqlite3SelectDestInit(pDest: &dest, SRT_Coroutine, iParm: pItem->regReturn);
139941 ExplainQueryPlan((pParse, 1, "CO-ROUTINE %!S", pItem));
139942 sqlite3Select(pParse, p: pSub, pDest: &dest);
139943 pItem->pTab->nRowLogEst = pSub->nSelectRow;
139944 pItem->fg.viaCoroutine = 1;
139945 pItem->regResult = dest.iSdst;
139946 sqlite3VdbeEndCoroutine(v, regYield: pItem->regReturn);
139947 sqlite3VdbeJumpHere(p: v, addr: addrTop-1);
139948 sqlite3ClearTempRegCache(pParse);
139949 }else if( pItem->fg.isCte && pItem->u2.pCteUse->addrM9e>0 ){
139950 /* This is a CTE for which materialization code has already been
139951 ** generated. Invoke the subroutine to compute the materialization,
139952 ** the make the pItem->iCursor be a copy of the ephemerial table that
139953 ** holds the result of the materialization. */
139954 CteUse *pCteUse = pItem->u2.pCteUse;
139955 sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: pCteUse->regRtn, p2: pCteUse->addrM9e);
139956 if( pItem->iCursor!=pCteUse->iCur ){
139957 sqlite3VdbeAddOp2(p: v, OP_OpenDup, p1: pItem->iCursor, p2: pCteUse->iCur);
139958 VdbeComment((v, "%!S", pItem));
139959 }
139960 pSub->nSelectRow = pCteUse->nRowEst;
139961 }else if( (pPrior = isSelfJoinView(pTabList, pThis: pItem))!=0 ){
139962 /* This view has already been materialized by a prior entry in
139963 ** this same FROM clause. Reuse it. */
139964 if( pPrior->addrFillSub ){
139965 sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: pPrior->regReturn, p2: pPrior->addrFillSub);
139966 }
139967 sqlite3VdbeAddOp2(p: v, OP_OpenDup, p1: pItem->iCursor, p2: pPrior->iCursor);
139968 pSub->nSelectRow = pPrior->pSelect->nSelectRow;
139969 }else{
139970 /* Materialize the view. If the view is not correlated, generate a
139971 ** subroutine to do the materialization so that subsequent uses of
139972 ** the same view can reuse the materialization. */
139973 int topAddr;
139974 int onceAddr = 0;
139975 int retAddr;
139976
139977 pItem->regReturn = ++pParse->nMem;
139978 topAddr = sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: pItem->regReturn);
139979 pItem->addrFillSub = topAddr+1;
139980 if( pItem->fg.isCorrelated==0 ){
139981 /* If the subquery is not correlated and if we are not inside of
139982 ** a trigger, then we only need to compute the value of the subquery
139983 ** once. */
139984 onceAddr = sqlite3VdbeAddOp0(p: v, OP_Once); VdbeCoverage(v);
139985 VdbeComment((v, "materialize %!S", pItem));
139986 }else{
139987 VdbeNoopComment((v, "materialize %!S", pItem));
139988 }
139989 sqlite3SelectDestInit(pDest: &dest, SRT_EphemTab, iParm: pItem->iCursor);
139990 ExplainQueryPlan((pParse, 1, "MATERIALIZE %!S", pItem));
139991 sqlite3Select(pParse, p: pSub, pDest: &dest);
139992 pItem->pTab->nRowLogEst = pSub->nSelectRow;
139993 if( onceAddr ) sqlite3VdbeJumpHere(p: v, addr: onceAddr);
139994 retAddr = sqlite3VdbeAddOp1(p: v, OP_Return, p1: pItem->regReturn);
139995 VdbeComment((v, "end %!S", pItem));
139996 sqlite3VdbeChangeP1(p: v, addr: topAddr, val: retAddr);
139997 sqlite3ClearTempRegCache(pParse);
139998 if( pItem->fg.isCte && pItem->fg.isCorrelated==0 ){
139999 CteUse *pCteUse = pItem->u2.pCteUse;
140000 pCteUse->addrM9e = pItem->addrFillSub;
140001 pCteUse->regRtn = pItem->regReturn;
140002 pCteUse->iCur = pItem->iCursor;
140003 pCteUse->nRowEst = pSub->nSelectRow;
140004 }
140005 }
140006 if( db->mallocFailed ) goto select_end;
140007 pParse->nHeight -= sqlite3SelectExprHeight(p);
140008 pParse->zAuthContext = zSavedAuthContext;
140009#endif
140010 }
140011
140012 /* Various elements of the SELECT copied into local variables for
140013 ** convenience */
140014 pEList = p->pEList;
140015 pWhere = p->pWhere;
140016 pGroupBy = p->pGroupBy;
140017 pHaving = p->pHaving;
140018 sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
140019
140020#if SELECTTRACE_ENABLED
140021 if( sqlite3SelectTrace & 0x400 ){
140022 SELECTTRACE(0x400,pParse,p,("After all FROM-clause analysis:\n"));
140023 sqlite3TreeViewSelect(0, p, 0);
140024 }
140025#endif
140026
140027 /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
140028 ** if the select-list is the same as the ORDER BY list, then this query
140029 ** can be rewritten as a GROUP BY. In other words, this:
140030 **
140031 ** SELECT DISTINCT xyz FROM ... ORDER BY xyz
140032 **
140033 ** is transformed to:
140034 **
140035 ** SELECT xyz FROM ... GROUP BY xyz ORDER BY xyz
140036 **
140037 ** The second form is preferred as a single index (or temp-table) may be
140038 ** used for both the ORDER BY and DISTINCT processing. As originally
140039 ** written the query must use a temp-table for at least one of the ORDER
140040 ** BY and DISTINCT, and an index or separate temp-table for the other.
140041 */
140042 if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
140043 && sqlite3ExprListCompare(pA: sSort.pOrderBy, pB: pEList, iTab: -1)==0
140044#ifndef SQLITE_OMIT_WINDOWFUNC
140045 && p->pWin==0
140046#endif
140047 ){
140048 p->selFlags &= ~SF_Distinct;
140049 pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, p: pEList, flags: 0);
140050 p->selFlags |= SF_Aggregate;
140051 /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
140052 ** the sDistinct.isTnct is still set. Hence, isTnct represents the
140053 ** original setting of the SF_Distinct flag, not the current setting */
140054 assert( sDistinct.isTnct );
140055
140056#if SELECTTRACE_ENABLED
140057 if( sqlite3SelectTrace & 0x400 ){
140058 SELECTTRACE(0x400,pParse,p,("Transform DISTINCT into GROUP BY:\n"));
140059 sqlite3TreeViewSelect(0, p, 0);
140060 }
140061#endif
140062 }
140063
140064 /* If there is an ORDER BY clause, then create an ephemeral index to
140065 ** do the sorting. But this sorting ephemeral index might end up
140066 ** being unused if the data can be extracted in pre-sorted order.
140067 ** If that is the case, then the OP_OpenEphemeral instruction will be
140068 ** changed to an OP_Noop once we figure out that the sorting index is
140069 ** not needed. The sSort.addrSortIndex variable is used to facilitate
140070 ** that change.
140071 */
140072 if( sSort.pOrderBy ){
140073 KeyInfo *pKeyInfo;
140074 pKeyInfo = sqlite3KeyInfoFromExprList(
140075 pParse, pList: sSort.pOrderBy, iStart: 0, nExtra: pEList->nExpr);
140076 sSort.iECursor = pParse->nTab++;
140077 sSort.addrSortIndex =
140078 sqlite3VdbeAddOp4(p: v, OP_OpenEphemeral,
140079 p1: sSort.iECursor, p2: sSort.pOrderBy->nExpr+1+pEList->nExpr, p3: 0,
140080 zP4: (char*)pKeyInfo, P4_KEYINFO
140081 );
140082 }else{
140083 sSort.addrSortIndex = -1;
140084 }
140085
140086 /* If the output is destined for a temporary table, open that table.
140087 */
140088 if( pDest->eDest==SRT_EphemTab ){
140089 sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: pDest->iSDParm, p2: pEList->nExpr);
140090 }
140091
140092 /* Set the limiter.
140093 */
140094 iEnd = sqlite3VdbeMakeLabel(pParse);
140095 if( (p->selFlags & SF_FixedLimit)==0 ){
140096 p->nSelectRow = 320; /* 4 billion rows */
140097 }
140098 computeLimitRegisters(pParse, p, iBreak: iEnd);
140099 if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
140100 sqlite3VdbeChangeOpcode(p: v, addr: sSort.addrSortIndex, OP_SorterOpen);
140101 sSort.sortFlags |= SORTFLAG_UseSorter;
140102 }
140103
140104 /* Open an ephemeral index to use for the distinct set.
140105 */
140106 if( p->selFlags & SF_Distinct ){
140107 sDistinct.tabTnct = pParse->nTab++;
140108 sDistinct.addrTnct = sqlite3VdbeAddOp4(p: v, OP_OpenEphemeral,
140109 p1: sDistinct.tabTnct, p2: 0, p3: 0,
140110 zP4: (char*)sqlite3KeyInfoFromExprList(pParse, pList: p->pEList,iStart: 0,nExtra: 0),
140111 P4_KEYINFO);
140112 sqlite3VdbeChangeP5(p: v, BTREE_UNORDERED);
140113 sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED;
140114 }else{
140115 sDistinct.eTnctType = WHERE_DISTINCT_NOOP;
140116 }
140117
140118 if( !isAgg && pGroupBy==0 ){
140119 /* No aggregate functions and no GROUP BY clause */
140120 u16 wctrlFlags = (sDistinct.isTnct ? WHERE_WANT_DISTINCT : 0)
140121 | (p->selFlags & SF_FixedLimit);
140122#ifndef SQLITE_OMIT_WINDOWFUNC
140123 Window *pWin = p->pWin; /* Main window object (or NULL) */
140124 if( pWin ){
140125 sqlite3WindowCodeInit(pParse, p);
140126 }
140127#endif
140128 assert( WHERE_USE_LIMIT==SF_FixedLimit );
140129
140130
140131 /* Begin the database scan. */
140132 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
140133 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy,
140134 p->pEList, wctrlFlags, p->nSelectRow);
140135 if( pWInfo==0 ) goto select_end;
140136 if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
140137 p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
140138 }
140139 if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){
140140 sDistinct.eTnctType = sqlite3WhereIsDistinct(pWInfo);
140141 }
140142 if( sSort.pOrderBy ){
140143 sSort.nOBSat = sqlite3WhereIsOrdered(pWInfo);
140144 sSort.labelOBLopt = sqlite3WhereOrderByLimitOptLabel(pWInfo);
140145 if( sSort.nOBSat==sSort.pOrderBy->nExpr ){
140146 sSort.pOrderBy = 0;
140147 }
140148 }
140149 SELECTTRACE(1,pParse,p,("WhereBegin returns\n"));
140150
140151 /* If sorting index that was created by a prior OP_OpenEphemeral
140152 ** instruction ended up not being needed, then change the OP_OpenEphemeral
140153 ** into an OP_Noop.
140154 */
140155 if( sSort.addrSortIndex>=0 && sSort.pOrderBy==0 ){
140156 sqlite3VdbeChangeToNoop(p: v, addr: sSort.addrSortIndex);
140157 }
140158
140159 assert( p->pEList==pEList );
140160#ifndef SQLITE_OMIT_WINDOWFUNC
140161 if( pWin ){
140162 int addrGosub = sqlite3VdbeMakeLabel(pParse);
140163 int iCont = sqlite3VdbeMakeLabel(pParse);
140164 int iBreak = sqlite3VdbeMakeLabel(pParse);
140165 int regGosub = ++pParse->nMem;
140166
140167 sqlite3WindowCodeStep(pParse, p, pWInfo, regGosub, addrGosub);
140168
140169 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: iBreak);
140170 sqlite3VdbeResolveLabel(v, x: addrGosub);
140171 VdbeNoopComment((v, "inner-loop subroutine"));
140172 sSort.labelOBLopt = 0;
140173 selectInnerLoop(pParse, p, srcTab: -1, pSort: &sSort, pDistinct: &sDistinct, pDest, iContinue: iCont, iBreak);
140174 sqlite3VdbeResolveLabel(v, x: iCont);
140175 sqlite3VdbeAddOp1(p: v, OP_Return, p1: regGosub);
140176 VdbeComment((v, "end inner-loop subroutine"));
140177 sqlite3VdbeResolveLabel(v, x: iBreak);
140178 }else
140179#endif /* SQLITE_OMIT_WINDOWFUNC */
140180 {
140181 /* Use the standard inner loop. */
140182 selectInnerLoop(pParse, p, srcTab: -1, pSort: &sSort, pDistinct: &sDistinct, pDest,
140183 iContinue: sqlite3WhereContinueLabel(pWInfo),
140184 iBreak: sqlite3WhereBreakLabel(pWInfo));
140185
140186 /* End the database scan loop.
140187 */
140188 SELECTTRACE(1,pParse,p,("WhereEnd\n"));
140189 sqlite3WhereEnd(pWInfo);
140190 }
140191 }else{
140192 /* This case when there exist aggregate functions or a GROUP BY clause
140193 ** or both */
140194 NameContext sNC; /* Name context for processing aggregate information */
140195 int iAMem; /* First Mem address for storing current GROUP BY */
140196 int iBMem; /* First Mem address for previous GROUP BY */
140197 int iUseFlag; /* Mem address holding flag indicating that at least
140198 ** one row of the input to the aggregator has been
140199 ** processed */
140200 int iAbortFlag; /* Mem address which causes query abort if positive */
140201 int groupBySort; /* Rows come from source in GROUP BY order */
140202 int addrEnd; /* End of processing for this SELECT */
140203 int sortPTab = 0; /* Pseudotable used to decode sorting results */
140204 int sortOut = 0; /* Output register from the sorter */
140205 int orderByGrp = 0; /* True if the GROUP BY and ORDER BY are the same */
140206
140207 /* Remove any and all aliases between the result set and the
140208 ** GROUP BY clause.
140209 */
140210 if( pGroupBy ){
140211 int k; /* Loop counter */
140212 struct ExprList_item *pItem; /* For looping over expression in a list */
140213
140214 for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
140215 pItem->u.x.iAlias = 0;
140216 }
140217 for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
140218 pItem->u.x.iAlias = 0;
140219 }
140220 assert( 66==sqlite3LogEst(100) );
140221 if( p->nSelectRow>66 ) p->nSelectRow = 66;
140222
140223 /* If there is both a GROUP BY and an ORDER BY clause and they are
140224 ** identical, then it may be possible to disable the ORDER BY clause
140225 ** on the grounds that the GROUP BY will cause elements to come out
140226 ** in the correct order. It also may not - the GROUP BY might use a
140227 ** database index that causes rows to be grouped together as required
140228 ** but not actually sorted. Either way, record the fact that the
140229 ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp
140230 ** variable. */
140231 if( sSort.pOrderBy && pGroupBy->nExpr==sSort.pOrderBy->nExpr ){
140232 int ii;
140233 /* The GROUP BY processing doesn't care whether rows are delivered in
140234 ** ASC or DESC order - only that each group is returned contiguously.
140235 ** So set the ASC/DESC flags in the GROUP BY to match those in the
140236 ** ORDER BY to maximize the chances of rows being delivered in an
140237 ** order that makes the ORDER BY redundant. */
140238 for(ii=0; ii<pGroupBy->nExpr; ii++){
140239 u8 sortFlags = sSort.pOrderBy->a[ii].sortFlags & KEYINFO_ORDER_DESC;
140240 pGroupBy->a[ii].sortFlags = sortFlags;
140241 }
140242 if( sqlite3ExprListCompare(pA: pGroupBy, pB: sSort.pOrderBy, iTab: -1)==0 ){
140243 orderByGrp = 1;
140244 }
140245 }
140246 }else{
140247 assert( 0==sqlite3LogEst(1) );
140248 p->nSelectRow = 0;
140249 }
140250
140251 /* Create a label to jump to when we want to abort the query */
140252 addrEnd = sqlite3VdbeMakeLabel(pParse);
140253
140254 /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
140255 ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
140256 ** SELECT statement.
140257 */
140258 pAggInfo = sqlite3DbMallocZero(db, n: sizeof(*pAggInfo) );
140259 if( pAggInfo ){
140260 sqlite3ParserAddCleanup(pParse,
140261 xCleanup: (void(*)(sqlite3*,void*))agginfoFree, pPtr: pAggInfo);
140262 testcase( pParse->earlyCleanup );
140263 }
140264 if( db->mallocFailed ){
140265 goto select_end;
140266 }
140267 pAggInfo->selId = p->selId;
140268 memset(s: &sNC, c: 0, n: sizeof(sNC));
140269 sNC.pParse = pParse;
140270 sNC.pSrcList = pTabList;
140271 sNC.uNC.pAggInfo = pAggInfo;
140272 VVA_ONLY( sNC.ncFlags = NC_UAggInfo; )
140273 pAggInfo->mnReg = pParse->nMem+1;
140274 pAggInfo->nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
140275 pAggInfo->pGroupBy = pGroupBy;
140276 sqlite3ExprAnalyzeAggList(pNC: &sNC, pList: pEList);
140277 sqlite3ExprAnalyzeAggList(pNC: &sNC, pList: sSort.pOrderBy);
140278 if( pHaving ){
140279 if( pGroupBy ){
140280 assert( pWhere==p->pWhere );
140281 assert( pHaving==p->pHaving );
140282 assert( pGroupBy==p->pGroupBy );
140283 havingToWhere(pParse, p);
140284 pWhere = p->pWhere;
140285 }
140286 sqlite3ExprAnalyzeAggregates(pNC: &sNC, pExpr: pHaving);
140287 }
140288 pAggInfo->nAccumulator = pAggInfo->nColumn;
140289 if( p->pGroupBy==0 && p->pHaving==0 && pAggInfo->nFunc==1 ){
140290 minMaxFlag = minMaxQuery(db, pFunc: pAggInfo->aFunc[0].pFExpr, ppMinMax: &pMinMaxOrderBy);
140291 }else{
140292 minMaxFlag = WHERE_ORDERBY_NORMAL;
140293 }
140294 for(i=0; i<pAggInfo->nFunc; i++){
140295 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
140296 assert( ExprUseXList(pExpr) );
140297 sNC.ncFlags |= NC_InAggFunc;
140298 sqlite3ExprAnalyzeAggList(pNC: &sNC, pList: pExpr->x.pList);
140299#ifndef SQLITE_OMIT_WINDOWFUNC
140300 assert( !IsWindowFunc(pExpr) );
140301 if( ExprHasProperty(pExpr, EP_WinFunc) ){
140302 sqlite3ExprAnalyzeAggregates(pNC: &sNC, pExpr: pExpr->y.pWin->pFilter);
140303 }
140304#endif
140305 sNC.ncFlags &= ~NC_InAggFunc;
140306 }
140307 pAggInfo->mxReg = pParse->nMem;
140308 if( db->mallocFailed ) goto select_end;
140309#if SELECTTRACE_ENABLED
140310 if( sqlite3SelectTrace & 0x400 ){
140311 int ii;
140312 SELECTTRACE(0x400,pParse,p,("After aggregate analysis %p:\n", pAggInfo));
140313 sqlite3TreeViewSelect(0, p, 0);
140314 if( minMaxFlag ){
140315 sqlite3DebugPrintf("MIN/MAX Optimization (0x%02x) adds:\n", minMaxFlag);
140316 sqlite3TreeViewExprList(0, pMinMaxOrderBy, 0, "ORDERBY");
140317 }
140318 for(ii=0; ii<pAggInfo->nColumn; ii++){
140319 sqlite3DebugPrintf("agg-column[%d] iMem=%d\n",
140320 ii, pAggInfo->aCol[ii].iMem);
140321 sqlite3TreeViewExpr(0, pAggInfo->aCol[ii].pCExpr, 0);
140322 }
140323 for(ii=0; ii<pAggInfo->nFunc; ii++){
140324 sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
140325 ii, pAggInfo->aFunc[ii].iMem);
140326 sqlite3TreeViewExpr(0, pAggInfo->aFunc[ii].pFExpr, 0);
140327 }
140328 }
140329#endif
140330
140331
140332 /* Processing for aggregates with GROUP BY is very different and
140333 ** much more complex than aggregates without a GROUP BY.
140334 */
140335 if( pGroupBy ){
140336 KeyInfo *pKeyInfo; /* Keying information for the group by clause */
140337 int addr1; /* A-vs-B comparision jump */
140338 int addrOutputRow; /* Start of subroutine that outputs a result row */
140339 int regOutputRow; /* Return address register for output subroutine */
140340 int addrSetAbort; /* Set the abort flag and return */
140341 int addrTopOfLoop; /* Top of the input loop */
140342 int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */
140343 int addrReset; /* Subroutine for resetting the accumulator */
140344 int regReset; /* Return address register for reset subroutine */
140345 ExprList *pDistinct = 0;
140346 u16 distFlag = 0;
140347 int eDist = WHERE_DISTINCT_NOOP;
140348
140349 if( pAggInfo->nFunc==1
140350 && pAggInfo->aFunc[0].iDistinct>=0
140351 && ALWAYS(pAggInfo->aFunc[0].pFExpr!=0)
140352 && ALWAYS(ExprUseXList(pAggInfo->aFunc[0].pFExpr))
140353 && pAggInfo->aFunc[0].pFExpr->x.pList!=0
140354 ){
140355 Expr *pExpr = pAggInfo->aFunc[0].pFExpr->x.pList->a[0].pExpr;
140356 pExpr = sqlite3ExprDup(db, p: pExpr, flags: 0);
140357 pDistinct = sqlite3ExprListDup(db, p: pGroupBy, flags: 0);
140358 pDistinct = sqlite3ExprListAppend(pParse, pList: pDistinct, pExpr);
140359 distFlag = pDistinct ? (WHERE_WANT_DISTINCT|WHERE_AGG_DISTINCT) : 0;
140360 }
140361
140362 /* If there is a GROUP BY clause we might need a sorting index to
140363 ** implement it. Allocate that sorting index now. If it turns out
140364 ** that we do not need it after all, the OP_SorterOpen instruction
140365 ** will be converted into a Noop.
140366 */
140367 pAggInfo->sortingIdx = pParse->nTab++;
140368 pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pList: pGroupBy,
140369 iStart: 0, nExtra: pAggInfo->nColumn);
140370 addrSortingIdx = sqlite3VdbeAddOp4(p: v, OP_SorterOpen,
140371 p1: pAggInfo->sortingIdx, p2: pAggInfo->nSortingColumn,
140372 p3: 0, zP4: (char*)pKeyInfo, P4_KEYINFO);
140373
140374 /* Initialize memory locations used by GROUP BY aggregate processing
140375 */
140376 iUseFlag = ++pParse->nMem;
140377 iAbortFlag = ++pParse->nMem;
140378 regOutputRow = ++pParse->nMem;
140379 addrOutputRow = sqlite3VdbeMakeLabel(pParse);
140380 regReset = ++pParse->nMem;
140381 addrReset = sqlite3VdbeMakeLabel(pParse);
140382 iAMem = pParse->nMem + 1;
140383 pParse->nMem += pGroupBy->nExpr;
140384 iBMem = pParse->nMem + 1;
140385 pParse->nMem += pGroupBy->nExpr;
140386 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: iAbortFlag);
140387 VdbeComment((v, "clear abort flag"));
140388 sqlite3VdbeAddOp3(p: v, OP_Null, p1: 0, p2: iAMem, p3: iAMem+pGroupBy->nExpr-1);
140389
140390 /* Begin a loop that will extract all source rows in GROUP BY order.
140391 ** This might involve two separate loops with an OP_Sort in between, or
140392 ** it might be a single loop that uses an index to extract information
140393 ** in the right order to begin with.
140394 */
140395 sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: regReset, p2: addrReset);
140396 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
140397 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, pDistinct,
140398 WHERE_GROUPBY | (orderByGrp ? WHERE_SORTBYGROUP : 0) | distFlag, 0
140399 );
140400 if( pWInfo==0 ){
140401 sqlite3ExprListDelete(db, pList: pDistinct);
140402 goto select_end;
140403 }
140404 eDist = sqlite3WhereIsDistinct(pWInfo);
140405 SELECTTRACE(1,pParse,p,("WhereBegin returns\n"));
140406 if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
140407 /* The optimizer is able to deliver rows in group by order so
140408 ** we do not have to sort. The OP_OpenEphemeral table will be
140409 ** cancelled later because we still need to use the pKeyInfo
140410 */
140411 groupBySort = 0;
140412 }else{
140413 /* Rows are coming out in undetermined order. We have to push
140414 ** each row into a sorting index, terminate the first loop,
140415 ** then loop over the sorting index in order to get the output
140416 ** in sorted order
140417 */
140418 int regBase;
140419 int regRecord;
140420 int nCol;
140421 int nGroupBy;
140422
140423 explainTempTable(pParse,
140424 zUsage: (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
140425 "DISTINCT" : "GROUP BY");
140426
140427 groupBySort = 1;
140428 nGroupBy = pGroupBy->nExpr;
140429 nCol = nGroupBy;
140430 j = nGroupBy;
140431 for(i=0; i<pAggInfo->nColumn; i++){
140432 if( pAggInfo->aCol[i].iSorterColumn>=j ){
140433 nCol++;
140434 j++;
140435 }
140436 }
140437 regBase = sqlite3GetTempRange(pParse, nReg: nCol);
140438 sqlite3ExprCodeExprList(pParse, pList: pGroupBy, target: regBase, srcReg: 0, flags: 0);
140439 j = nGroupBy;
140440 for(i=0; i<pAggInfo->nColumn; i++){
140441 struct AggInfo_col *pCol = &pAggInfo->aCol[i];
140442 if( pCol->iSorterColumn>=j ){
140443 int r1 = j + regBase;
140444 sqlite3ExprCodeGetColumnOfTable(v,
140445 pTab: pCol->pTab, iTabCur: pCol->iTable, iCol: pCol->iColumn, regOut: r1);
140446 j++;
140447 }
140448 }
140449 regRecord = sqlite3GetTempReg(pParse);
140450 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: regBase, p2: nCol, p3: regRecord);
140451 sqlite3VdbeAddOp2(p: v, OP_SorterInsert, p1: pAggInfo->sortingIdx, p2: regRecord);
140452 sqlite3ReleaseTempReg(pParse, iReg: regRecord);
140453 sqlite3ReleaseTempRange(pParse, iReg: regBase, nReg: nCol);
140454 SELECTTRACE(1,pParse,p,("WhereEnd\n"));
140455 sqlite3WhereEnd(pWInfo);
140456 pAggInfo->sortingIdxPTab = sortPTab = pParse->nTab++;
140457 sortOut = sqlite3GetTempReg(pParse);
140458 sqlite3VdbeAddOp3(p: v, OP_OpenPseudo, p1: sortPTab, p2: sortOut, p3: nCol);
140459 sqlite3VdbeAddOp2(p: v, OP_SorterSort, p1: pAggInfo->sortingIdx, p2: addrEnd);
140460 VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v);
140461 pAggInfo->useSortingIdx = 1;
140462 }
140463
140464 /* If the index or temporary table used by the GROUP BY sort
140465 ** will naturally deliver rows in the order required by the ORDER BY
140466 ** clause, cancel the ephemeral table open coded earlier.
140467 **
140468 ** This is an optimization - the correct answer should result regardless.
140469 ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER to
140470 ** disable this optimization for testing purposes. */
140471 if( orderByGrp && OptimizationEnabled(db, SQLITE_GroupByOrder)
140472 && (groupBySort || sqlite3WhereIsSorted(pWInfo))
140473 ){
140474 sSort.pOrderBy = 0;
140475 sqlite3VdbeChangeToNoop(p: v, addr: sSort.addrSortIndex);
140476 }
140477
140478 /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
140479 ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
140480 ** Then compare the current GROUP BY terms against the GROUP BY terms
140481 ** from the previous row currently stored in a0, a1, a2...
140482 */
140483 addrTopOfLoop = sqlite3VdbeCurrentAddr(p: v);
140484 if( groupBySort ){
140485 sqlite3VdbeAddOp3(p: v, OP_SorterData, p1: pAggInfo->sortingIdx,
140486 p2: sortOut, p3: sortPTab);
140487 }
140488 for(j=0; j<pGroupBy->nExpr; j++){
140489 if( groupBySort ){
140490 sqlite3VdbeAddOp3(p: v, OP_Column, p1: sortPTab, p2: j, p3: iBMem+j);
140491 }else{
140492 pAggInfo->directMode = 1;
140493 sqlite3ExprCode(pParse, pExpr: pGroupBy->a[j].pExpr, target: iBMem+j);
140494 }
140495 }
140496 sqlite3VdbeAddOp4(p: v, OP_Compare, p1: iAMem, p2: iBMem, p3: pGroupBy->nExpr,
140497 zP4: (char*)sqlite3KeyInfoRef(p: pKeyInfo), P4_KEYINFO);
140498 addr1 = sqlite3VdbeCurrentAddr(p: v);
140499 sqlite3VdbeAddOp3(p: v, OP_Jump, p1: addr1+1, p2: 0, p3: addr1+1); VdbeCoverage(v);
140500
140501 /* Generate code that runs whenever the GROUP BY changes.
140502 ** Changes in the GROUP BY are detected by the previous code
140503 ** block. If there were no changes, this block is skipped.
140504 **
140505 ** This code copies current group by terms in b0,b1,b2,...
140506 ** over to a0,a1,a2. It then calls the output subroutine
140507 ** and resets the aggregate accumulator registers in preparation
140508 ** for the next GROUP BY batch.
140509 */
140510 sqlite3ExprCodeMove(pParse, iFrom: iBMem, iTo: iAMem, nReg: pGroupBy->nExpr);
140511 sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: regOutputRow, p2: addrOutputRow);
140512 VdbeComment((v, "output one row"));
140513 sqlite3VdbeAddOp2(p: v, OP_IfPos, p1: iAbortFlag, p2: addrEnd); VdbeCoverage(v);
140514 VdbeComment((v, "check abort flag"));
140515 sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: regReset, p2: addrReset);
140516 VdbeComment((v, "reset accumulator"));
140517
140518 /* Update the aggregate accumulators based on the content of
140519 ** the current row
140520 */
140521 sqlite3VdbeJumpHere(p: v, addr: addr1);
140522 updateAccumulator(pParse, regAcc: iUseFlag, pAggInfo, eDistinctType: eDist);
140523 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 1, p2: iUseFlag);
140524 VdbeComment((v, "indicate data in accumulator"));
140525
140526 /* End of the loop
140527 */
140528 if( groupBySort ){
140529 sqlite3VdbeAddOp2(p: v, OP_SorterNext, p1: pAggInfo->sortingIdx,p2: addrTopOfLoop);
140530 VdbeCoverage(v);
140531 }else{
140532 SELECTTRACE(1,pParse,p,("WhereEnd\n"));
140533 sqlite3WhereEnd(pWInfo);
140534 sqlite3VdbeChangeToNoop(p: v, addr: addrSortingIdx);
140535 }
140536 sqlite3ExprListDelete(db, pList: pDistinct);
140537
140538 /* Output the final row of result
140539 */
140540 sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: regOutputRow, p2: addrOutputRow);
140541 VdbeComment((v, "output final row"));
140542
140543 /* Jump over the subroutines
140544 */
140545 sqlite3VdbeGoto(p: v, iDest: addrEnd);
140546
140547 /* Generate a subroutine that outputs a single row of the result
140548 ** set. This subroutine first looks at the iUseFlag. If iUseFlag
140549 ** is less than or equal to zero, the subroutine is a no-op. If
140550 ** the processing calls for the query to abort, this subroutine
140551 ** increments the iAbortFlag memory location before returning in
140552 ** order to signal the caller to abort.
140553 */
140554 addrSetAbort = sqlite3VdbeCurrentAddr(p: v);
140555 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 1, p2: iAbortFlag);
140556 VdbeComment((v, "set abort flag"));
140557 sqlite3VdbeAddOp1(p: v, OP_Return, p1: regOutputRow);
140558 sqlite3VdbeResolveLabel(v, x: addrOutputRow);
140559 addrOutputRow = sqlite3VdbeCurrentAddr(p: v);
140560 sqlite3VdbeAddOp2(p: v, OP_IfPos, p1: iUseFlag, p2: addrOutputRow+2);
140561 VdbeCoverage(v);
140562 VdbeComment((v, "Groupby result generator entry point"));
140563 sqlite3VdbeAddOp1(p: v, OP_Return, p1: regOutputRow);
140564 finalizeAggFunctions(pParse, pAggInfo);
140565 sqlite3ExprIfFalse(pParse, pExpr: pHaving, dest: addrOutputRow+1, SQLITE_JUMPIFNULL);
140566 selectInnerLoop(pParse, p, srcTab: -1, pSort: &sSort,
140567 pDistinct: &sDistinct, pDest,
140568 iContinue: addrOutputRow+1, iBreak: addrSetAbort);
140569 sqlite3VdbeAddOp1(p: v, OP_Return, p1: regOutputRow);
140570 VdbeComment((v, "end groupby result generator"));
140571
140572 /* Generate a subroutine that will reset the group-by accumulator
140573 */
140574 sqlite3VdbeResolveLabel(v, x: addrReset);
140575 resetAccumulator(pParse, pAggInfo);
140576 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: iUseFlag);
140577 VdbeComment((v, "indicate accumulator empty"));
140578 sqlite3VdbeAddOp1(p: v, OP_Return, p1: regReset);
140579
140580 if( eDist!=WHERE_DISTINCT_NOOP ){
140581 struct AggInfo_func *pF = &pAggInfo->aFunc[0];
140582 fixDistinctOpenEph(pParse, eTnctType: eDist, iVal: pF->iDistinct, iOpenEphAddr: pF->iDistAddr);
140583 }
140584 } /* endif pGroupBy. Begin aggregate queries without GROUP BY: */
140585 else {
140586 Table *pTab;
140587 if( (pTab = isSimpleCount(p, pAggInfo))!=0 ){
140588 /* If isSimpleCount() returns a pointer to a Table structure, then
140589 ** the SQL statement is of the form:
140590 **
140591 ** SELECT count(*) FROM <tbl>
140592 **
140593 ** where the Table structure returned represents table <tbl>.
140594 **
140595 ** This statement is so common that it is optimized specially. The
140596 ** OP_Count instruction is executed either on the intkey table that
140597 ** contains the data for table <tbl> or on one of its indexes. It
140598 ** is better to execute the op on an index, as indexes are almost
140599 ** always spread across less pages than their corresponding tables.
140600 */
140601 const int iDb = sqlite3SchemaToIndex(db: pParse->db, pSchema: pTab->pSchema);
140602 const int iCsr = pParse->nTab++; /* Cursor to scan b-tree */
140603 Index *pIdx; /* Iterator variable */
140604 KeyInfo *pKeyInfo = 0; /* Keyinfo for scanned index */
140605 Index *pBest = 0; /* Best index found so far */
140606 Pgno iRoot = pTab->tnum; /* Root page of scanned b-tree */
140607
140608 sqlite3CodeVerifySchema(pParse, iDb);
140609 sqlite3TableLock(pParse, iDb, iTab: pTab->tnum, isWriteLock: 0, zName: pTab->zName);
140610
140611 /* Search for the index that has the lowest scan cost.
140612 **
140613 ** (2011-04-15) Do not do a full scan of an unordered index.
140614 **
140615 ** (2013-10-03) Do not count the entries in a partial index.
140616 **
140617 ** In practice the KeyInfo structure will not be used. It is only
140618 ** passed to keep OP_OpenRead happy.
140619 */
140620 if( !HasRowid(pTab) ) pBest = sqlite3PrimaryKeyIndex(pTab);
140621 if( !p->pSrc->a[0].fg.notIndexed ){
140622 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
140623 if( pIdx->bUnordered==0
140624 && pIdx->szIdxRow<pTab->szTabRow
140625 && pIdx->pPartIdxWhere==0
140626 && (!pBest || pIdx->szIdxRow<pBest->szIdxRow)
140627 ){
140628 pBest = pIdx;
140629 }
140630 }
140631 }
140632 if( pBest ){
140633 iRoot = pBest->tnum;
140634 pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pIdx: pBest);
140635 }
140636
140637 /* Open a read-only cursor, execute the OP_Count, close the cursor. */
140638 sqlite3VdbeAddOp4Int(p: v, OP_OpenRead, p1: iCsr, p2: (int)iRoot, p3: iDb, p4: 1);
140639 if( pKeyInfo ){
140640 sqlite3VdbeChangeP4(p: v, addr: -1, zP4: (char *)pKeyInfo, P4_KEYINFO);
140641 }
140642 sqlite3VdbeAddOp2(p: v, OP_Count, p1: iCsr, p2: pAggInfo->aFunc[0].iMem);
140643 sqlite3VdbeAddOp1(p: v, OP_Close, p1: iCsr);
140644 explainSimpleCount(pParse, pTab, pIdx: pBest);
140645 }else{
140646 int regAcc = 0; /* "populate accumulators" flag */
140647 ExprList *pDistinct = 0;
140648 u16 distFlag = 0;
140649 int eDist;
140650
140651 /* If there are accumulator registers but no min() or max() functions
140652 ** without FILTER clauses, allocate register regAcc. Register regAcc
140653 ** will contain 0 the first time the inner loop runs, and 1 thereafter.
140654 ** The code generated by updateAccumulator() uses this to ensure
140655 ** that the accumulator registers are (a) updated only once if
140656 ** there are no min() or max functions or (b) always updated for the
140657 ** first row visited by the aggregate, so that they are updated at
140658 ** least once even if the FILTER clause means the min() or max()
140659 ** function visits zero rows. */
140660 if( pAggInfo->nAccumulator ){
140661 for(i=0; i<pAggInfo->nFunc; i++){
140662 if( ExprHasProperty(pAggInfo->aFunc[i].pFExpr, EP_WinFunc) ){
140663 continue;
140664 }
140665 if( pAggInfo->aFunc[i].pFunc->funcFlags&SQLITE_FUNC_NEEDCOLL ){
140666 break;
140667 }
140668 }
140669 if( i==pAggInfo->nFunc ){
140670 regAcc = ++pParse->nMem;
140671 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: regAcc);
140672 }
140673 }else if( pAggInfo->nFunc==1 && pAggInfo->aFunc[0].iDistinct>=0 ){
140674 assert( ExprUseXList(pAggInfo->aFunc[0].pFExpr) );
140675 pDistinct = pAggInfo->aFunc[0].pFExpr->x.pList;
140676 distFlag = pDistinct ? (WHERE_WANT_DISTINCT|WHERE_AGG_DISTINCT) : 0;
140677 }
140678
140679 /* This case runs if the aggregate has no GROUP BY clause. The
140680 ** processing is much simpler since there is only a single row
140681 ** of output.
140682 */
140683 assert( p->pGroupBy==0 );
140684 resetAccumulator(pParse, pAggInfo);
140685
140686 /* If this query is a candidate for the min/max optimization, then
140687 ** minMaxFlag will have been previously set to either
140688 ** WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX and pMinMaxOrderBy will
140689 ** be an appropriate ORDER BY expression for the optimization.
140690 */
140691 assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 );
140692 assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
140693
140694 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
140695 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy,
140696 pDistinct, minMaxFlag|distFlag, 0);
140697 if( pWInfo==0 ){
140698 goto select_end;
140699 }
140700 SELECTTRACE(1,pParse,p,("WhereBegin returns\n"));
140701 eDist = sqlite3WhereIsDistinct(pWInfo);
140702 updateAccumulator(pParse, regAcc, pAggInfo, eDistinctType: eDist);
140703 if( eDist!=WHERE_DISTINCT_NOOP ){
140704 struct AggInfo_func *pF = &pAggInfo->aFunc[0];
140705 fixDistinctOpenEph(pParse, eTnctType: eDist, iVal: pF->iDistinct, iOpenEphAddr: pF->iDistAddr);
140706 }
140707
140708 if( regAcc ) sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 1, p2: regAcc);
140709 if( minMaxFlag ){
140710 sqlite3WhereMinMaxOptEarlyOut(v, pWInfo);
140711 }
140712 SELECTTRACE(1,pParse,p,("WhereEnd\n"));
140713 sqlite3WhereEnd(pWInfo);
140714 finalizeAggFunctions(pParse, pAggInfo);
140715 }
140716
140717 sSort.pOrderBy = 0;
140718 sqlite3ExprIfFalse(pParse, pExpr: pHaving, dest: addrEnd, SQLITE_JUMPIFNULL);
140719 selectInnerLoop(pParse, p, srcTab: -1, pSort: 0, pDistinct: 0,
140720 pDest, iContinue: addrEnd, iBreak: addrEnd);
140721 }
140722 sqlite3VdbeResolveLabel(v, x: addrEnd);
140723
140724 } /* endif aggregate query */
140725
140726 if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED ){
140727 explainTempTable(pParse, zUsage: "DISTINCT");
140728 }
140729
140730 /* If there is an ORDER BY clause, then we need to sort the results
140731 ** and send them to the callback one by one.
140732 */
140733 if( sSort.pOrderBy ){
140734 explainTempTable(pParse,
140735 zUsage: sSort.nOBSat>0 ? "RIGHT PART OF ORDER BY":"ORDER BY");
140736 assert( p->pEList==pEList );
140737 generateSortTail(pParse, p, pSort: &sSort, nColumn: pEList->nExpr, pDest);
140738 }
140739
140740 /* Jump here to skip this query
140741 */
140742 sqlite3VdbeResolveLabel(v, x: iEnd);
140743
140744 /* The SELECT has been coded. If there is an error in the Parse structure,
140745 ** set the return code to 1. Otherwise 0. */
140746 rc = (pParse->nErr>0);
140747
140748 /* Control jumps to here if an error is encountered above, or upon
140749 ** successful coding of the SELECT.
140750 */
140751select_end:
140752 assert( db->mallocFailed==0 || db->mallocFailed==1 );
140753 pParse->nErr += db->mallocFailed;
140754 sqlite3ExprListDelete(db, pList: pMinMaxOrderBy);
140755#ifdef SQLITE_DEBUG
140756 if( pAggInfo && !db->mallocFailed ){
140757 for(i=0; i<pAggInfo->nColumn; i++){
140758 Expr *pExpr = pAggInfo->aCol[i].pCExpr;
140759 assert( pExpr!=0 );
140760 assert( pExpr->pAggInfo==pAggInfo );
140761 assert( pExpr->iAgg==i );
140762 }
140763 for(i=0; i<pAggInfo->nFunc; i++){
140764 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
140765 assert( pExpr!=0 );
140766 assert( pExpr->pAggInfo==pAggInfo );
140767 assert( pExpr->iAgg==i );
140768 }
140769 }
140770#endif
140771
140772#if SELECTTRACE_ENABLED
140773 SELECTTRACE(0x1,pParse,p,("end processing\n"));
140774 if( (sqlite3SelectTrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)==0 ){
140775 sqlite3TreeViewSelect(0, p, 0);
140776 }
140777#endif
140778 ExplainQueryPlanPop(pParse);
140779 return rc;
140780}
140781
140782/************** End of select.c **********************************************/
140783/************** Begin file table.c *******************************************/
140784/*
140785** 2001 September 15
140786**
140787** The author disclaims copyright to this source code. In place of
140788** a legal notice, here is a blessing:
140789**
140790** May you do good and not evil.
140791** May you find forgiveness for yourself and forgive others.
140792** May you share freely, never taking more than you give.
140793**
140794*************************************************************************
140795** This file contains the sqlite3_get_table() and sqlite3_free_table()
140796** interface routines. These are just wrappers around the main
140797** interface routine of sqlite3_exec().
140798**
140799** These routines are in a separate files so that they will not be linked
140800** if they are not used.
140801*/
140802/* #include "sqliteInt.h" */
140803
140804#ifndef SQLITE_OMIT_GET_TABLE
140805
140806/*
140807** This structure is used to pass data from sqlite3_get_table() through
140808** to the callback function is uses to build the result.
140809*/
140810typedef struct TabResult {
140811 char **azResult; /* Accumulated output */
140812 char *zErrMsg; /* Error message text, if an error occurs */
140813 u32 nAlloc; /* Slots allocated for azResult[] */
140814 u32 nRow; /* Number of rows in the result */
140815 u32 nColumn; /* Number of columns in the result */
140816 u32 nData; /* Slots used in azResult[]. (nRow+1)*nColumn */
140817 int rc; /* Return code from sqlite3_exec() */
140818} TabResult;
140819
140820/*
140821** This routine is called once for each row in the result table. Its job
140822** is to fill in the TabResult structure appropriately, allocating new
140823** memory as necessary.
140824*/
140825static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
140826 TabResult *p = (TabResult*)pArg; /* Result accumulator */
140827 int need; /* Slots needed in p->azResult[] */
140828 int i; /* Loop counter */
140829 char *z; /* A single column of result */
140830
140831 /* Make sure there is enough space in p->azResult to hold everything
140832 ** we need to remember from this invocation of the callback.
140833 */
140834 if( p->nRow==0 && argv!=0 ){
140835 need = nCol*2;
140836 }else{
140837 need = nCol;
140838 }
140839 if( p->nData + need > p->nAlloc ){
140840 char **azNew;
140841 p->nAlloc = p->nAlloc*2 + need;
140842 azNew = sqlite3Realloc( pOld: p->azResult, nBytes: sizeof(char*)*p->nAlloc );
140843 if( azNew==0 ) goto malloc_failed;
140844 p->azResult = azNew;
140845 }
140846
140847 /* If this is the first row, then generate an extra row containing
140848 ** the names of all columns.
140849 */
140850 if( p->nRow==0 ){
140851 p->nColumn = nCol;
140852 for(i=0; i<nCol; i++){
140853 z = sqlite3_mprintf(zFormat: "%s", colv[i]);
140854 if( z==0 ) goto malloc_failed;
140855 p->azResult[p->nData++] = z;
140856 }
140857 }else if( (int)p->nColumn!=nCol ){
140858 sqlite3_free(p: p->zErrMsg);
140859 p->zErrMsg = sqlite3_mprintf(
140860 zFormat: "sqlite3_get_table() called with two or more incompatible queries"
140861 );
140862 p->rc = SQLITE_ERROR;
140863 return 1;
140864 }
140865
140866 /* Copy over the row data
140867 */
140868 if( argv!=0 ){
140869 for(i=0; i<nCol; i++){
140870 if( argv[i]==0 ){
140871 z = 0;
140872 }else{
140873 int n = sqlite3Strlen30(z: argv[i])+1;
140874 z = sqlite3_malloc64( n );
140875 if( z==0 ) goto malloc_failed;
140876 memcpy(dest: z, src: argv[i], n: n);
140877 }
140878 p->azResult[p->nData++] = z;
140879 }
140880 p->nRow++;
140881 }
140882 return 0;
140883
140884malloc_failed:
140885 p->rc = SQLITE_NOMEM_BKPT;
140886 return 1;
140887}
140888
140889/*
140890** Query the database. But instead of invoking a callback for each row,
140891** malloc() for space to hold the result and return the entire results
140892** at the conclusion of the call.
140893**
140894** The result that is written to ***pazResult is held in memory obtained
140895** from malloc(). But the caller cannot free this memory directly.
140896** Instead, the entire table should be passed to sqlite3_free_table() when
140897** the calling procedure is finished using it.
140898*/
140899SQLITE_API int sqlite3_get_table(
140900 sqlite3 *db, /* The database on which the SQL executes */
140901 const char *zSql, /* The SQL to be executed */
140902 char ***pazResult, /* Write the result table here */
140903 int *pnRow, /* Write the number of rows in the result here */
140904 int *pnColumn, /* Write the number of columns of result here */
140905 char **pzErrMsg /* Write error messages here */
140906){
140907 int rc;
140908 TabResult res;
140909
140910#ifdef SQLITE_ENABLE_API_ARMOR
140911 if( !sqlite3SafetyCheckOk(db) || pazResult==0 ) return SQLITE_MISUSE_BKPT;
140912#endif
140913 *pazResult = 0;
140914 if( pnColumn ) *pnColumn = 0;
140915 if( pnRow ) *pnRow = 0;
140916 if( pzErrMsg ) *pzErrMsg = 0;
140917 res.zErrMsg = 0;
140918 res.nRow = 0;
140919 res.nColumn = 0;
140920 res.nData = 1;
140921 res.nAlloc = 20;
140922 res.rc = SQLITE_OK;
140923 res.azResult = sqlite3_malloc64(n: sizeof(char*)*res.nAlloc );
140924 if( res.azResult==0 ){
140925 db->errCode = SQLITE_NOMEM;
140926 return SQLITE_NOMEM_BKPT;
140927 }
140928 res.azResult[0] = 0;
140929 rc = sqlite3_exec(db, zSql, xCallback: sqlite3_get_table_cb, pArg: &res, pzErrMsg);
140930 assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
140931 res.azResult[0] = SQLITE_INT_TO_PTR(res.nData);
140932 if( (rc&0xff)==SQLITE_ABORT ){
140933 sqlite3_free_table(result: &res.azResult[1]);
140934 if( res.zErrMsg ){
140935 if( pzErrMsg ){
140936 sqlite3_free(p: *pzErrMsg);
140937 *pzErrMsg = sqlite3_mprintf(zFormat: "%s",res.zErrMsg);
140938 }
140939 sqlite3_free(p: res.zErrMsg);
140940 }
140941 db->errCode = res.rc; /* Assume 32-bit assignment is atomic */
140942 return res.rc;
140943 }
140944 sqlite3_free(p: res.zErrMsg);
140945 if( rc!=SQLITE_OK ){
140946 sqlite3_free_table(result: &res.azResult[1]);
140947 return rc;
140948 }
140949 if( res.nAlloc>res.nData ){
140950 char **azNew;
140951 azNew = sqlite3Realloc( pOld: res.azResult, nBytes: sizeof(char*)*res.nData );
140952 if( azNew==0 ){
140953 sqlite3_free_table(result: &res.azResult[1]);
140954 db->errCode = SQLITE_NOMEM;
140955 return SQLITE_NOMEM_BKPT;
140956 }
140957 res.azResult = azNew;
140958 }
140959 *pazResult = &res.azResult[1];
140960 if( pnColumn ) *pnColumn = res.nColumn;
140961 if( pnRow ) *pnRow = res.nRow;
140962 return rc;
140963}
140964
140965/*
140966** This routine frees the space the sqlite3_get_table() malloced.
140967*/
140968SQLITE_API void sqlite3_free_table(
140969 char **azResult /* Result returned from sqlite3_get_table() */
140970){
140971 if( azResult ){
140972 int i, n;
140973 azResult--;
140974 assert( azResult!=0 );
140975 n = SQLITE_PTR_TO_INT(azResult[0]);
140976 for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(p: azResult[i]); }
140977 sqlite3_free(p: azResult);
140978 }
140979}
140980
140981#endif /* SQLITE_OMIT_GET_TABLE */
140982
140983/************** End of table.c ***********************************************/
140984/************** Begin file trigger.c *****************************************/
140985/*
140986**
140987** The author disclaims copyright to this source code. In place of
140988** a legal notice, here is a blessing:
140989**
140990** May you do good and not evil.
140991** May you find forgiveness for yourself and forgive others.
140992** May you share freely, never taking more than you give.
140993**
140994*************************************************************************
140995** This file contains the implementation for TRIGGERs
140996*/
140997/* #include "sqliteInt.h" */
140998
140999#ifndef SQLITE_OMIT_TRIGGER
141000/*
141001** Delete a linked list of TriggerStep structures.
141002*/
141003SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
141004 while( pTriggerStep ){
141005 TriggerStep * pTmp = pTriggerStep;
141006 pTriggerStep = pTriggerStep->pNext;
141007
141008 sqlite3ExprDelete(db, p: pTmp->pWhere);
141009 sqlite3ExprListDelete(db, pList: pTmp->pExprList);
141010 sqlite3SelectDelete(db, p: pTmp->pSelect);
141011 sqlite3IdListDelete(db, pList: pTmp->pIdList);
141012 sqlite3UpsertDelete(db, pTmp->pUpsert);
141013 sqlite3SrcListDelete(db, pList: pTmp->pFrom);
141014 sqlite3DbFree(db, p: pTmp->zSpan);
141015
141016 sqlite3DbFree(db, p: pTmp);
141017 }
141018}
141019
141020/*
141021** Given table pTab, return a list of all the triggers attached to
141022** the table. The list is connected by Trigger.pNext pointers.
141023**
141024** All of the triggers on pTab that are in the same database as pTab
141025** are already attached to pTab->pTrigger. But there might be additional
141026** triggers on pTab in the TEMP schema. This routine prepends all
141027** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
141028** and returns the combined list.
141029**
141030** To state it another way: This routine returns a list of all triggers
141031** that fire off of pTab. The list will include any TEMP triggers on
141032** pTab as well as the triggers lised in pTab->pTrigger.
141033*/
141034SQLITE_PRIVATE Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
141035 Schema *pTmpSchema; /* Schema of the pTab table */
141036 Trigger *pList; /* List of triggers to return */
141037 HashElem *p; /* Loop variable for TEMP triggers */
141038
141039 if( pParse->disableTriggers ){
141040 return 0;
141041 }
141042 pTmpSchema = pParse->db->aDb[1].pSchema;
141043 p = sqliteHashFirst(&pTmpSchema->trigHash);
141044 pList = pTab->pTrigger;
141045 while( p ){
141046 Trigger *pTrig = (Trigger *)sqliteHashData(p);
141047 if( pTrig->pTabSchema==pTab->pSchema
141048 && pTrig->table
141049 && 0==sqlite3StrICmp(zLeft: pTrig->table, zRight: pTab->zName)
141050 && pTrig->pTabSchema!=pTmpSchema
141051 ){
141052 pTrig->pNext = pList;
141053 pList = pTrig;
141054 }else if( pTrig->op==TK_RETURNING
141055#ifndef SQLITE_OMIT_VIRTUALTABLE
141056 && pParse->db->pVtabCtx==0
141057#endif
141058 ){
141059 assert( pParse->bReturning );
141060 assert( &(pParse->u1.pReturning->retTrig) == pTrig );
141061 pTrig->table = pTab->zName;
141062 pTrig->pTabSchema = pTab->pSchema;
141063 pTrig->pNext = pList;
141064 pList = pTrig;
141065 }
141066 p = sqliteHashNext(p);
141067 }
141068#if 0
141069 if( pList ){
141070 Trigger *pX;
141071 printf("Triggers for %s:", pTab->zName);
141072 for(pX=pList; pX; pX=pX->pNext){
141073 printf(" %s", pX->zName);
141074 }
141075 printf("\n");
141076 fflush(stdout);
141077 }
141078#endif
141079 return pList;
141080}
141081
141082/*
141083** This is called by the parser when it sees a CREATE TRIGGER statement
141084** up to the point of the BEGIN before the trigger actions. A Trigger
141085** structure is generated based on the information available and stored
141086** in pParse->pNewTrigger. After the trigger actions have been parsed, the
141087** sqlite3FinishTrigger() function is called to complete the trigger
141088** construction process.
141089*/
141090SQLITE_PRIVATE void sqlite3BeginTrigger(
141091 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
141092 Token *pName1, /* The name of the trigger */
141093 Token *pName2, /* The name of the trigger */
141094 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
141095 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
141096 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
141097 SrcList *pTableName,/* The name of the table/view the trigger applies to */
141098 Expr *pWhen, /* WHEN clause */
141099 int isTemp, /* True if the TEMPORARY keyword is present */
141100 int noErr /* Suppress errors if the trigger already exists */
141101){
141102 Trigger *pTrigger = 0; /* The new trigger */
141103 Table *pTab; /* Table that the trigger fires off of */
141104 char *zName = 0; /* Name of the trigger */
141105 sqlite3 *db = pParse->db; /* The database connection */
141106 int iDb; /* The database to store the trigger in */
141107 Token *pName; /* The unqualified db name */
141108 DbFixer sFix; /* State vector for the DB fixer */
141109
141110 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
141111 assert( pName2!=0 );
141112 assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
141113 assert( op>0 && op<0xff );
141114 if( isTemp ){
141115 /* If TEMP was specified, then the trigger name may not be qualified. */
141116 if( pName2->n>0 ){
141117 sqlite3ErrorMsg(pParse, zFormat: "temporary trigger may not have qualified name");
141118 goto trigger_cleanup;
141119 }
141120 iDb = 1;
141121 pName = pName1;
141122 }else{
141123 /* Figure out the db that the trigger will be created in */
141124 iDb = sqlite3TwoPartName(pParse, pName1, pName2, pUnqual: &pName);
141125 if( iDb<0 ){
141126 goto trigger_cleanup;
141127 }
141128 }
141129 if( !pTableName || db->mallocFailed ){
141130 goto trigger_cleanup;
141131 }
141132
141133 /* A long-standing parser bug is that this syntax was allowed:
141134 **
141135 ** CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
141136 ** ^^^^^^^^
141137 **
141138 ** To maintain backwards compatibility, ignore the database
141139 ** name on pTableName if we are reparsing out of the schema table
141140 */
141141 if( db->init.busy && iDb!=1 ){
141142 sqlite3DbFree(db, p: pTableName->a[0].zDatabase);
141143 pTableName->a[0].zDatabase = 0;
141144 }
141145
141146 /* If the trigger name was unqualified, and the table is a temp table,
141147 ** then set iDb to 1 to create the trigger in the temporary database.
141148 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
141149 ** exist, the error is caught by the block below.
141150 */
141151 pTab = sqlite3SrcListLookup(pParse, pSrc: pTableName);
141152 if( db->init.busy==0 && pName2->n==0 && pTab
141153 && pTab->pSchema==db->aDb[1].pSchema ){
141154 iDb = 1;
141155 }
141156
141157 /* Ensure the table name matches database name and that the table exists */
141158 if( db->mallocFailed ) goto trigger_cleanup;
141159 assert( pTableName->nSrc==1 );
141160 sqlite3FixInit(pFix: &sFix, pParse, iDb, zType: "trigger", pName);
141161 if( sqlite3FixSrcList(pFix: &sFix, pList: pTableName) ){
141162 goto trigger_cleanup;
141163 }
141164 pTab = sqlite3SrcListLookup(pParse, pSrc: pTableName);
141165 if( !pTab ){
141166 /* The table does not exist. */
141167 goto trigger_orphan_error;
141168 }
141169 if( IsVirtual(pTab) ){
141170 sqlite3ErrorMsg(pParse, zFormat: "cannot create triggers on virtual tables");
141171 goto trigger_orphan_error;
141172 }
141173
141174 /* Check that the trigger name is not reserved and that no trigger of the
141175 ** specified name exists */
141176 zName = sqlite3NameFromToken(db, pName);
141177 if( zName==0 ){
141178 assert( db->mallocFailed );
141179 goto trigger_cleanup;
141180 }
141181 if( sqlite3CheckObjectName(pParse, zName, zType: "trigger", zTblName: pTab->zName) ){
141182 goto trigger_cleanup;
141183 }
141184 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
141185 if( !IN_RENAME_OBJECT ){
141186 if( sqlite3HashFind(pH: &(db->aDb[iDb].pSchema->trigHash),pKey: zName) ){
141187 if( !noErr ){
141188 sqlite3ErrorMsg(pParse, zFormat: "trigger %T already exists", pName);
141189 }else{
141190 assert( !db->init.busy );
141191 sqlite3CodeVerifySchema(pParse, iDb);
141192 }
141193 goto trigger_cleanup;
141194 }
141195 }
141196
141197 /* Do not create a trigger on a system table */
141198 if( sqlite3StrNICmp(zLeft: pTab->zName, zRight: "sqlite_", N: 7)==0 ){
141199 sqlite3ErrorMsg(pParse, zFormat: "cannot create trigger on system table");
141200 goto trigger_cleanup;
141201 }
141202
141203 /* INSTEAD of triggers are only for views and views only support INSTEAD
141204 ** of triggers.
141205 */
141206 if( IsView(pTab) && tr_tm!=TK_INSTEAD ){
141207 sqlite3ErrorMsg(pParse, zFormat: "cannot create %s trigger on view: %S",
141208 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName->a);
141209 goto trigger_orphan_error;
141210 }
141211 if( !IsView(pTab) && tr_tm==TK_INSTEAD ){
141212 sqlite3ErrorMsg(pParse, zFormat: "cannot create INSTEAD OF"
141213 " trigger on table: %S", pTableName->a);
141214 goto trigger_orphan_error;
141215 }
141216
141217#ifndef SQLITE_OMIT_AUTHORIZATION
141218 if( !IN_RENAME_OBJECT ){
141219 int iTabDb = sqlite3SchemaToIndex(db, pSchema: pTab->pSchema);
141220 int code = SQLITE_CREATE_TRIGGER;
141221 const char *zDb = db->aDb[iTabDb].zDbSName;
141222 const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
141223 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
141224 if( sqlite3AuthCheck(pParse, code, zArg1: zName, zArg2: pTab->zName, zArg3: zDbTrig) ){
141225 goto trigger_cleanup;
141226 }
141227 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),zArg2: 0,zArg3: zDb)){
141228 goto trigger_cleanup;
141229 }
141230 }
141231#endif
141232
141233 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
141234 ** cannot appear on views. So we might as well translate every
141235 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
141236 ** elsewhere.
141237 */
141238 if (tr_tm == TK_INSTEAD){
141239 tr_tm = TK_BEFORE;
141240 }
141241
141242 /* Build the Trigger object */
141243 pTrigger = (Trigger*)sqlite3DbMallocZero(db, n: sizeof(Trigger));
141244 if( pTrigger==0 ) goto trigger_cleanup;
141245 pTrigger->zName = zName;
141246 zName = 0;
141247 pTrigger->table = sqlite3DbStrDup(db, z: pTableName->a[0].zName);
141248 pTrigger->pSchema = db->aDb[iDb].pSchema;
141249 pTrigger->pTabSchema = pTab->pSchema;
141250 pTrigger->op = (u8)op;
141251 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
141252 if( IN_RENAME_OBJECT ){
141253 sqlite3RenameTokenRemap(pParse, pTo: pTrigger->table, pFrom: pTableName->a[0].zName);
141254 pTrigger->pWhen = pWhen;
141255 pWhen = 0;
141256 }else{
141257 pTrigger->pWhen = sqlite3ExprDup(db, p: pWhen, EXPRDUP_REDUCE);
141258 }
141259 pTrigger->pColumns = pColumns;
141260 pColumns = 0;
141261 assert( pParse->pNewTrigger==0 );
141262 pParse->pNewTrigger = pTrigger;
141263
141264trigger_cleanup:
141265 sqlite3DbFree(db, p: zName);
141266 sqlite3SrcListDelete(db, pList: pTableName);
141267 sqlite3IdListDelete(db, pList: pColumns);
141268 sqlite3ExprDelete(db, p: pWhen);
141269 if( !pParse->pNewTrigger ){
141270 sqlite3DeleteTrigger(db, pTrigger);
141271 }else{
141272 assert( pParse->pNewTrigger==pTrigger );
141273 }
141274 return;
141275
141276trigger_orphan_error:
141277 if( db->init.iDb==1 ){
141278 /* Ticket #3810.
141279 ** Normally, whenever a table is dropped, all associated triggers are
141280 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
141281 ** and the table is dropped by a different database connection, the
141282 ** trigger is not visible to the database connection that does the
141283 ** drop so the trigger cannot be dropped. This results in an
141284 ** "orphaned trigger" - a trigger whose associated table is missing.
141285 **
141286 ** 2020-11-05 see also https://sqlite.org/forum/forumpost/157dc791df
141287 */
141288 db->init.orphanTrigger = 1;
141289 }
141290 goto trigger_cleanup;
141291}
141292
141293/*
141294** This routine is called after all of the trigger actions have been parsed
141295** in order to complete the process of building the trigger.
141296*/
141297SQLITE_PRIVATE void sqlite3FinishTrigger(
141298 Parse *pParse, /* Parser context */
141299 TriggerStep *pStepList, /* The triggered program */
141300 Token *pAll /* Token that describes the complete CREATE TRIGGER */
141301){
141302 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
141303 char *zName; /* Name of trigger */
141304 sqlite3 *db = pParse->db; /* The database */
141305 DbFixer sFix; /* Fixer object */
141306 int iDb; /* Database containing the trigger */
141307 Token nameToken; /* Trigger name for error reporting */
141308
141309 pParse->pNewTrigger = 0;
141310 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
141311 zName = pTrig->zName;
141312 iDb = sqlite3SchemaToIndex(db: pParse->db, pSchema: pTrig->pSchema);
141313 pTrig->step_list = pStepList;
141314 while( pStepList ){
141315 pStepList->pTrig = pTrig;
141316 pStepList = pStepList->pNext;
141317 }
141318 sqlite3TokenInit(p: &nameToken, z: pTrig->zName);
141319 sqlite3FixInit(pFix: &sFix, pParse, iDb, zType: "trigger", pName: &nameToken);
141320 if( sqlite3FixTriggerStep(pFix: &sFix, pStep: pTrig->step_list)
141321 || sqlite3FixExpr(pFix: &sFix, pExpr: pTrig->pWhen)
141322 ){
141323 goto triggerfinish_cleanup;
141324 }
141325
141326#ifndef SQLITE_OMIT_ALTERTABLE
141327 if( IN_RENAME_OBJECT ){
141328 assert( !db->init.busy );
141329 pParse->pNewTrigger = pTrig;
141330 pTrig = 0;
141331 }else
141332#endif
141333
141334 /* if we are not initializing,
141335 ** build the sqlite_schema entry
141336 */
141337 if( !db->init.busy ){
141338 Vdbe *v;
141339 char *z;
141340
141341 /* Make an entry in the sqlite_schema table */
141342 v = sqlite3GetVdbe(pParse);
141343 if( v==0 ) goto triggerfinish_cleanup;
141344 sqlite3BeginWriteOperation(pParse, setStatement: 0, iDb);
141345 z = sqlite3DbStrNDup(db, z: (char*)pAll->z, n: pAll->n);
141346 testcase( z==0 );
141347 sqlite3NestedParse(pParse,
141348 zFormat: "INSERT INTO %Q." LEGACY_SCHEMA_TABLE
141349 " VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
141350 db->aDb[iDb].zDbSName, zName,
141351 pTrig->table, z);
141352 sqlite3DbFree(db, p: z);
141353 sqlite3ChangeCookie(pParse, iDb);
141354 sqlite3VdbeAddParseSchemaOp(p: v, iDb,
141355 zWhere: sqlite3MPrintf(db, zFormat: "type='trigger' AND name='%q'", zName), p5: 0);
141356 }
141357
141358 if( db->init.busy ){
141359 Trigger *pLink = pTrig;
141360 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
141361 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
141362 assert( pLink!=0 );
141363 pTrig = sqlite3HashInsert(pH: pHash, pKey: zName, data: pTrig);
141364 if( pTrig ){
141365 sqlite3OomFault(db);
141366 }else if( pLink->pSchema==pLink->pTabSchema ){
141367 Table *pTab;
141368 pTab = sqlite3HashFind(pH: &pLink->pTabSchema->tblHash, pKey: pLink->table);
141369 assert( pTab!=0 );
141370 pLink->pNext = pTab->pTrigger;
141371 pTab->pTrigger = pLink;
141372 }
141373 }
141374
141375triggerfinish_cleanup:
141376 sqlite3DeleteTrigger(db, pTrig);
141377 assert( IN_RENAME_OBJECT || !pParse->pNewTrigger );
141378 sqlite3DeleteTriggerStep(db, pTriggerStep: pStepList);
141379}
141380
141381/*
141382** Duplicate a range of text from an SQL statement, then convert all
141383** whitespace characters into ordinary space characters.
141384*/
141385static char *triggerSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
141386 char *z = sqlite3DbSpanDup(db, zStart, zEnd);
141387 int i;
141388 if( z ) for(i=0; z[i]; i++) if( sqlite3Isspace(z[i]) ) z[i] = ' ';
141389 return z;
141390}
141391
141392/*
141393** Turn a SELECT statement (that the pSelect parameter points to) into
141394** a trigger step. Return a pointer to a TriggerStep structure.
141395**
141396** The parser calls this routine when it finds a SELECT statement in
141397** body of a TRIGGER.
141398*/
141399SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(
141400 sqlite3 *db, /* Database connection */
141401 Select *pSelect, /* The SELECT statement */
141402 const char *zStart, /* Start of SQL text */
141403 const char *zEnd /* End of SQL text */
141404){
141405 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, n: sizeof(TriggerStep));
141406 if( pTriggerStep==0 ) {
141407 sqlite3SelectDelete(db, p: pSelect);
141408 return 0;
141409 }
141410 pTriggerStep->op = TK_SELECT;
141411 pTriggerStep->pSelect = pSelect;
141412 pTriggerStep->orconf = OE_Default;
141413 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
141414 return pTriggerStep;
141415}
141416
141417/*
141418** Allocate space to hold a new trigger step. The allocated space
141419** holds both the TriggerStep object and the TriggerStep.target.z string.
141420**
141421** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
141422*/
141423static TriggerStep *triggerStepAllocate(
141424 Parse *pParse, /* Parser context */
141425 u8 op, /* Trigger opcode */
141426 Token *pName, /* The target name */
141427 const char *zStart, /* Start of SQL text */
141428 const char *zEnd /* End of SQL text */
141429){
141430 sqlite3 *db = pParse->db;
141431 TriggerStep *pTriggerStep;
141432
141433 pTriggerStep = sqlite3DbMallocZero(db, n: sizeof(TriggerStep) + pName->n + 1);
141434 if( pTriggerStep ){
141435 char *z = (char*)&pTriggerStep[1];
141436 memcpy(dest: z, src: pName->z, n: pName->n);
141437 sqlite3Dequote(z);
141438 pTriggerStep->zTarget = z;
141439 pTriggerStep->op = op;
141440 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
141441 if( IN_RENAME_OBJECT ){
141442 sqlite3RenameTokenMap(pParse, pPtr: pTriggerStep->zTarget, pToken: pName);
141443 }
141444 }
141445 return pTriggerStep;
141446}
141447
141448/*
141449** Build a trigger step out of an INSERT statement. Return a pointer
141450** to the new trigger step.
141451**
141452** The parser calls this routine when it sees an INSERT inside the
141453** body of a trigger.
141454*/
141455SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(
141456 Parse *pParse, /* Parser */
141457 Token *pTableName, /* Name of the table into which we insert */
141458 IdList *pColumn, /* List of columns in pTableName to insert into */
141459 Select *pSelect, /* A SELECT statement that supplies values */
141460 u8 orconf, /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
141461 Upsert *pUpsert, /* ON CONFLICT clauses for upsert */
141462 const char *zStart, /* Start of SQL text */
141463 const char *zEnd /* End of SQL text */
141464){
141465 sqlite3 *db = pParse->db;
141466 TriggerStep *pTriggerStep;
141467
141468 assert(pSelect != 0 || db->mallocFailed);
141469
141470 pTriggerStep = triggerStepAllocate(pParse, TK_INSERT, pName: pTableName,zStart,zEnd);
141471 if( pTriggerStep ){
141472 if( IN_RENAME_OBJECT ){
141473 pTriggerStep->pSelect = pSelect;
141474 pSelect = 0;
141475 }else{
141476 pTriggerStep->pSelect = sqlite3SelectDup(db, pDup: pSelect, EXPRDUP_REDUCE);
141477 }
141478 pTriggerStep->pIdList = pColumn;
141479 pTriggerStep->pUpsert = pUpsert;
141480 pTriggerStep->orconf = orconf;
141481 if( pUpsert ){
141482 sqlite3HasExplicitNulls(pParse, pList: pUpsert->pUpsertTarget);
141483 }
141484 }else{
141485 testcase( pColumn );
141486 sqlite3IdListDelete(db, pList: pColumn);
141487 testcase( pUpsert );
141488 sqlite3UpsertDelete(db, pUpsert);
141489 }
141490 sqlite3SelectDelete(db, p: pSelect);
141491
141492 return pTriggerStep;
141493}
141494
141495/*
141496** Construct a trigger step that implements an UPDATE statement and return
141497** a pointer to that trigger step. The parser calls this routine when it
141498** sees an UPDATE statement inside the body of a CREATE TRIGGER.
141499*/
141500SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(
141501 Parse *pParse, /* Parser */
141502 Token *pTableName, /* Name of the table to be updated */
141503 SrcList *pFrom,
141504 ExprList *pEList, /* The SET clause: list of column and new values */
141505 Expr *pWhere, /* The WHERE clause */
141506 u8 orconf, /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
141507 const char *zStart, /* Start of SQL text */
141508 const char *zEnd /* End of SQL text */
141509){
141510 sqlite3 *db = pParse->db;
141511 TriggerStep *pTriggerStep;
141512
141513 pTriggerStep = triggerStepAllocate(pParse, TK_UPDATE, pName: pTableName,zStart,zEnd);
141514 if( pTriggerStep ){
141515 if( IN_RENAME_OBJECT ){
141516 pTriggerStep->pExprList = pEList;
141517 pTriggerStep->pWhere = pWhere;
141518 pTriggerStep->pFrom = pFrom;
141519 pEList = 0;
141520 pWhere = 0;
141521 pFrom = 0;
141522 }else{
141523 pTriggerStep->pExprList = sqlite3ExprListDup(db, p: pEList, EXPRDUP_REDUCE);
141524 pTriggerStep->pWhere = sqlite3ExprDup(db, p: pWhere, EXPRDUP_REDUCE);
141525 pTriggerStep->pFrom = sqlite3SrcListDup(db, p: pFrom, EXPRDUP_REDUCE);
141526 }
141527 pTriggerStep->orconf = orconf;
141528 }
141529 sqlite3ExprListDelete(db, pList: pEList);
141530 sqlite3ExprDelete(db, p: pWhere);
141531 sqlite3SrcListDelete(db, pList: pFrom);
141532 return pTriggerStep;
141533}
141534
141535/*
141536** Construct a trigger step that implements a DELETE statement and return
141537** a pointer to that trigger step. The parser calls this routine when it
141538** sees a DELETE statement inside the body of a CREATE TRIGGER.
141539*/
141540SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(
141541 Parse *pParse, /* Parser */
141542 Token *pTableName, /* The table from which rows are deleted */
141543 Expr *pWhere, /* The WHERE clause */
141544 const char *zStart, /* Start of SQL text */
141545 const char *zEnd /* End of SQL text */
141546){
141547 sqlite3 *db = pParse->db;
141548 TriggerStep *pTriggerStep;
141549
141550 pTriggerStep = triggerStepAllocate(pParse, TK_DELETE, pName: pTableName,zStart,zEnd);
141551 if( pTriggerStep ){
141552 if( IN_RENAME_OBJECT ){
141553 pTriggerStep->pWhere = pWhere;
141554 pWhere = 0;
141555 }else{
141556 pTriggerStep->pWhere = sqlite3ExprDup(db, p: pWhere, EXPRDUP_REDUCE);
141557 }
141558 pTriggerStep->orconf = OE_Default;
141559 }
141560 sqlite3ExprDelete(db, p: pWhere);
141561 return pTriggerStep;
141562}
141563
141564/*
141565** Recursively delete a Trigger structure
141566*/
141567SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
141568 if( pTrigger==0 || pTrigger->bReturning ) return;
141569 sqlite3DeleteTriggerStep(db, pTriggerStep: pTrigger->step_list);
141570 sqlite3DbFree(db, p: pTrigger->zName);
141571 sqlite3DbFree(db, p: pTrigger->table);
141572 sqlite3ExprDelete(db, p: pTrigger->pWhen);
141573 sqlite3IdListDelete(db, pList: pTrigger->pColumns);
141574 sqlite3DbFree(db, p: pTrigger);
141575}
141576
141577/*
141578** This function is called to drop a trigger from the database schema.
141579**
141580** This may be called directly from the parser and therefore identifies
141581** the trigger by name. The sqlite3DropTriggerPtr() routine does the
141582** same job as this routine except it takes a pointer to the trigger
141583** instead of the trigger name.
141584**/
141585SQLITE_PRIVATE void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
141586 Trigger *pTrigger = 0;
141587 int i;
141588 const char *zDb;
141589 const char *zName;
141590 sqlite3 *db = pParse->db;
141591
141592 if( db->mallocFailed ) goto drop_trigger_cleanup;
141593 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
141594 goto drop_trigger_cleanup;
141595 }
141596
141597 assert( pName->nSrc==1 );
141598 zDb = pName->a[0].zDatabase;
141599 zName = pName->a[0].zName;
141600 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
141601 for(i=OMIT_TEMPDB; i<db->nDb; i++){
141602 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
141603 if( zDb && sqlite3DbIsNamed(db, iDb: j, zName: zDb)==0 ) continue;
141604 assert( sqlite3SchemaMutexHeld(db, j, 0) );
141605 pTrigger = sqlite3HashFind(pH: &(db->aDb[j].pSchema->trigHash), pKey: zName);
141606 if( pTrigger ) break;
141607 }
141608 if( !pTrigger ){
141609 if( !noErr ){
141610 sqlite3ErrorMsg(pParse, zFormat: "no such trigger: %S", pName->a);
141611 }else{
141612 sqlite3CodeVerifyNamedSchema(pParse, zDb);
141613 }
141614 pParse->checkSchema = 1;
141615 goto drop_trigger_cleanup;
141616 }
141617 sqlite3DropTriggerPtr(pParse, pTrigger);
141618
141619drop_trigger_cleanup:
141620 sqlite3SrcListDelete(db, pList: pName);
141621}
141622
141623/*
141624** Return a pointer to the Table structure for the table that a trigger
141625** is set on.
141626*/
141627static Table *tableOfTrigger(Trigger *pTrigger){
141628 return sqlite3HashFind(pH: &pTrigger->pTabSchema->tblHash, pKey: pTrigger->table);
141629}
141630
141631
141632/*
141633** Drop a trigger given a pointer to that trigger.
141634*/
141635SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
141636 Table *pTable;
141637 Vdbe *v;
141638 sqlite3 *db = pParse->db;
141639 int iDb;
141640
141641 iDb = sqlite3SchemaToIndex(db: pParse->db, pSchema: pTrigger->pSchema);
141642 assert( iDb>=0 && iDb<db->nDb );
141643 pTable = tableOfTrigger(pTrigger);
141644 assert( (pTable && pTable->pSchema==pTrigger->pSchema) || iDb==1 );
141645#ifndef SQLITE_OMIT_AUTHORIZATION
141646 if( pTable ){
141647 int code = SQLITE_DROP_TRIGGER;
141648 const char *zDb = db->aDb[iDb].zDbSName;
141649 const char *zTab = SCHEMA_TABLE(iDb);
141650 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
141651 if( sqlite3AuthCheck(pParse, code, zArg1: pTrigger->zName, zArg2: pTable->zName, zArg3: zDb) ||
141652 sqlite3AuthCheck(pParse, SQLITE_DELETE, zArg1: zTab, zArg2: 0, zArg3: zDb) ){
141653 return;
141654 }
141655 }
141656#endif
141657
141658 /* Generate code to destroy the database record of the trigger.
141659 */
141660 if( (v = sqlite3GetVdbe(pParse))!=0 ){
141661 sqlite3NestedParse(pParse,
141662 zFormat: "DELETE FROM %Q." LEGACY_SCHEMA_TABLE " WHERE name=%Q AND type='trigger'",
141663 db->aDb[iDb].zDbSName, pTrigger->zName
141664 );
141665 sqlite3ChangeCookie(pParse, iDb);
141666 sqlite3VdbeAddOp4(p: v, OP_DropTrigger, p1: iDb, p2: 0, p3: 0, zP4: pTrigger->zName, p4type: 0);
141667 }
141668}
141669
141670/*
141671** Remove a trigger from the hash tables of the sqlite* pointer.
141672*/
141673SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
141674 Trigger *pTrigger;
141675 Hash *pHash;
141676
141677 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
141678 pHash = &(db->aDb[iDb].pSchema->trigHash);
141679 pTrigger = sqlite3HashInsert(pH: pHash, pKey: zName, data: 0);
141680 if( ALWAYS(pTrigger) ){
141681 if( pTrigger->pSchema==pTrigger->pTabSchema ){
141682 Table *pTab = tableOfTrigger(pTrigger);
141683 if( pTab ){
141684 Trigger **pp;
141685 for(pp=&pTab->pTrigger; *pp; pp=&((*pp)->pNext)){
141686 if( *pp==pTrigger ){
141687 *pp = (*pp)->pNext;
141688 break;
141689 }
141690 }
141691 }
141692 }
141693 sqlite3DeleteTrigger(db, pTrigger);
141694 db->mDbFlags |= DBFLAG_SchemaChange;
141695 }
141696}
141697
141698/*
141699** pEList is the SET clause of an UPDATE statement. Each entry
141700** in pEList is of the format <id>=<expr>. If any of the entries
141701** in pEList have an <id> which matches an identifier in pIdList,
141702** then return TRUE. If pIdList==NULL, then it is considered a
141703** wildcard that matches anything. Likewise if pEList==NULL then
141704** it matches anything so always return true. Return false only
141705** if there is no match.
141706*/
141707static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
141708 int e;
141709 if( pIdList==0 || NEVER(pEList==0) ) return 1;
141710 for(e=0; e<pEList->nExpr; e++){
141711 if( sqlite3IdListIndex(pList: pIdList, zName: pEList->a[e].zEName)>=0 ) return 1;
141712 }
141713 return 0;
141714}
141715
141716/*
141717** Return a list of all triggers on table pTab if there exists at least
141718** one trigger that must be fired when an operation of type 'op' is
141719** performed on the table, and, if that operation is an UPDATE, if at
141720** least one of the columns in pChanges is being modified.
141721*/
141722SQLITE_PRIVATE Trigger *sqlite3TriggersExist(
141723 Parse *pParse, /* Parse context */
141724 Table *pTab, /* The table the contains the triggers */
141725 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
141726 ExprList *pChanges, /* Columns that change in an UPDATE statement */
141727 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
141728){
141729 int mask = 0;
141730 Trigger *pList = 0;
141731 Trigger *p;
141732
141733 pList = sqlite3TriggerList(pParse, pTab);
141734 assert( pList==0 || IsVirtual(pTab)==0
141735 || (pList->bReturning && pList->pNext==0) );
141736 if( pList!=0 ){
141737 p = pList;
141738 if( (pParse->db->flags & SQLITE_EnableTrigger)==0
141739 && pTab->pTrigger!=0
141740 ){
141741 /* The SQLITE_DBCONFIG_ENABLE_TRIGGER setting is off. That means that
141742 ** only TEMP triggers are allowed. Truncate the pList so that it
141743 ** includes only TEMP triggers */
141744 if( pList==pTab->pTrigger ){
141745 pList = 0;
141746 goto exit_triggers_exist;
141747 }
141748 while( ALWAYS(p->pNext) && p->pNext!=pTab->pTrigger ) p = p->pNext;
141749 p->pNext = 0;
141750 p = pList;
141751 }
141752 do{
141753 if( p->op==op && checkColumnOverlap(pIdList: p->pColumns, pEList: pChanges) ){
141754 mask |= p->tr_tm;
141755 }else if( p->op==TK_RETURNING ){
141756 /* The first time a RETURNING trigger is seen, the "op" value tells
141757 ** us what time of trigger it should be. */
141758 assert( sqlite3IsToplevel(pParse) );
141759 p->op = op;
141760 if( IsVirtual(pTab) ){
141761 if( op!=TK_INSERT ){
141762 sqlite3ErrorMsg(pParse,
141763 zFormat: "%s RETURNING is not available on virtual tables",
141764 op==TK_DELETE ? "DELETE" : "UPDATE");
141765 }
141766 p->tr_tm = TRIGGER_BEFORE;
141767 }else{
141768 p->tr_tm = TRIGGER_AFTER;
141769 }
141770 mask |= p->tr_tm;
141771 }else if( p->bReturning && p->op==TK_INSERT && op==TK_UPDATE
141772 && sqlite3IsToplevel(pParse) ){
141773 /* Also fire a RETURNING trigger for an UPSERT */
141774 mask |= p->tr_tm;
141775 }
141776 p = p->pNext;
141777 }while( p );
141778 }
141779exit_triggers_exist:
141780 if( pMask ){
141781 *pMask = mask;
141782 }
141783 return (mask ? pList : 0);
141784}
141785
141786/*
141787** Convert the pStep->zTarget string into a SrcList and return a pointer
141788** to that SrcList.
141789**
141790** This routine adds a specific database name, if needed, to the target when
141791** forming the SrcList. This prevents a trigger in one database from
141792** referring to a target in another database. An exception is when the
141793** trigger is in TEMP in which case it can refer to any other database it
141794** wants.
141795*/
141796SQLITE_PRIVATE SrcList *sqlite3TriggerStepSrc(
141797 Parse *pParse, /* The parsing context */
141798 TriggerStep *pStep /* The trigger containing the target token */
141799){
141800 sqlite3 *db = pParse->db;
141801 SrcList *pSrc; /* SrcList to be returned */
141802 char *zName = sqlite3DbStrDup(db, z: pStep->zTarget);
141803 pSrc = sqlite3SrcListAppend(pParse, pList: 0, pTable: 0, pDatabase: 0);
141804 assert( pSrc==0 || pSrc->nSrc==1 );
141805 assert( zName || pSrc==0 );
141806 if( pSrc ){
141807 Schema *pSchema = pStep->pTrig->pSchema;
141808 pSrc->a[0].zName = zName;
141809 if( pSchema!=db->aDb[1].pSchema ){
141810 pSrc->a[0].pSchema = pSchema;
141811 }
141812 if( pStep->pFrom ){
141813 SrcList *pDup = sqlite3SrcListDup(db, p: pStep->pFrom, flags: 0);
141814 pSrc = sqlite3SrcListAppendList(pParse, p1: pSrc, p2: pDup);
141815 }
141816 }else{
141817 sqlite3DbFree(db, p: zName);
141818 }
141819 return pSrc;
141820}
141821
141822/*
141823** Return true if the pExpr term from the RETURNING clause argument
141824** list is of the form "*". Raise an error if the terms if of the
141825** form "table.*".
141826*/
141827static int isAsteriskTerm(
141828 Parse *pParse, /* Parsing context */
141829 Expr *pTerm /* A term in the RETURNING clause */
141830){
141831 assert( pTerm!=0 );
141832 if( pTerm->op==TK_ASTERISK ) return 1;
141833 if( pTerm->op!=TK_DOT ) return 0;
141834 assert( pTerm->pRight!=0 );
141835 assert( pTerm->pLeft!=0 );
141836 if( pTerm->pRight->op!=TK_ASTERISK ) return 0;
141837 sqlite3ErrorMsg(pParse, zFormat: "RETURNING may not use \"TABLE.*\" wildcards");
141838 return 1;
141839}
141840
141841/* The input list pList is the list of result set terms from a RETURNING
141842** clause. The table that we are returning from is pTab.
141843**
141844** This routine makes a copy of the pList, and at the same time expands
141845** any "*" wildcards to be the complete set of columns from pTab.
141846*/
141847static ExprList *sqlite3ExpandReturning(
141848 Parse *pParse, /* Parsing context */
141849 ExprList *pList, /* The arguments to RETURNING */
141850 Table *pTab /* The table being updated */
141851){
141852 ExprList *pNew = 0;
141853 sqlite3 *db = pParse->db;
141854 int i;
141855
141856 for(i=0; i<pList->nExpr; i++){
141857 Expr *pOldExpr = pList->a[i].pExpr;
141858 if( NEVER(pOldExpr==0) ) continue;
141859 if( isAsteriskTerm(pParse, pTerm: pOldExpr) ){
141860 int jj;
141861 for(jj=0; jj<pTab->nCol; jj++){
141862 Expr *pNewExpr;
141863 if( IsHiddenColumn(pTab->aCol+jj) ) continue;
141864 pNewExpr = sqlite3Expr(db, TK_ID, zToken: pTab->aCol[jj].zCnName);
141865 pNew = sqlite3ExprListAppend(pParse, pList: pNew, pExpr: pNewExpr);
141866 if( !db->mallocFailed ){
141867 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
141868 pItem->zEName = sqlite3DbStrDup(db, z: pTab->aCol[jj].zCnName);
141869 pItem->eEName = ENAME_NAME;
141870 }
141871 }
141872 }else{
141873 Expr *pNewExpr = sqlite3ExprDup(db, p: pOldExpr, flags: 0);
141874 pNew = sqlite3ExprListAppend(pParse, pList: pNew, pExpr: pNewExpr);
141875 if( !db->mallocFailed && ALWAYS(pList->a[i].zEName!=0) ){
141876 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
141877 pItem->zEName = sqlite3DbStrDup(db, z: pList->a[i].zEName);
141878 pItem->eEName = pList->a[i].eEName;
141879 }
141880 }
141881 }
141882 return pNew;
141883}
141884
141885/*
141886** Generate code for the RETURNING trigger. Unlike other triggers
141887** that invoke a subprogram in the bytecode, the code for RETURNING
141888** is generated in-line.
141889*/
141890static void codeReturningTrigger(
141891 Parse *pParse, /* Parse context */
141892 Trigger *pTrigger, /* The trigger step that defines the RETURNING */
141893 Table *pTab, /* The table to code triggers from */
141894 int regIn /* The first in an array of registers */
141895){
141896 Vdbe *v = pParse->pVdbe;
141897 sqlite3 *db = pParse->db;
141898 ExprList *pNew;
141899 Returning *pReturning;
141900 Select sSelect;
141901 SrcList sFrom;
141902
141903 assert( v!=0 );
141904 assert( pParse->bReturning );
141905 pReturning = pParse->u1.pReturning;
141906 assert( pTrigger == &(pReturning->retTrig) );
141907 memset(s: &sSelect, c: 0, n: sizeof(sSelect));
141908 memset(s: &sFrom, c: 0, n: sizeof(sFrom));
141909 sSelect.pEList = sqlite3ExprListDup(db, p: pReturning->pReturnEL, flags: 0);
141910 sSelect.pSrc = &sFrom;
141911 sFrom.nSrc = 1;
141912 sFrom.a[0].pTab = pTab;
141913 sFrom.a[0].iCursor = -1;
141914 sqlite3SelectPrep(pParse, p: &sSelect, pOuterNC: 0);
141915 if( db->mallocFailed==0 && pParse->nErr==0 ){
141916 sqlite3GenerateColumnNames(pParse, pSelect: &sSelect);
141917 }
141918 sqlite3ExprListDelete(db, pList: sSelect.pEList);
141919 pNew = sqlite3ExpandReturning(pParse, pList: pReturning->pReturnEL, pTab);
141920 if( !db->mallocFailed ){
141921 NameContext sNC;
141922 memset(s: &sNC, c: 0, n: sizeof(sNC));
141923 if( pReturning->nRetCol==0 ){
141924 pReturning->nRetCol = pNew->nExpr;
141925 pReturning->iRetCur = pParse->nTab++;
141926 }
141927 sNC.pParse = pParse;
141928 sNC.uNC.iBaseReg = regIn;
141929 sNC.ncFlags = NC_UBaseReg;
141930 pParse->eTriggerOp = pTrigger->op;
141931 pParse->pTriggerTab = pTab;
141932 if( sqlite3ResolveExprListNames(pNC: &sNC, pList: pNew)==SQLITE_OK
141933 && !db->mallocFailed
141934 ){
141935 int i;
141936 int nCol = pNew->nExpr;
141937 int reg = pParse->nMem+1;
141938 pParse->nMem += nCol+2;
141939 pReturning->iRetReg = reg;
141940 for(i=0; i<nCol; i++){
141941 Expr *pCol = pNew->a[i].pExpr;
141942 assert( pCol!=0 ); /* Due to !db->mallocFailed ~9 lines above */
141943 sqlite3ExprCodeFactorable(pParse, pExpr: pCol, target: reg+i);
141944 }
141945 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: reg, p2: i, p3: reg+i);
141946 sqlite3VdbeAddOp2(p: v, OP_NewRowid, p1: pReturning->iRetCur, p2: reg+i+1);
141947 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: pReturning->iRetCur, p2: reg+i, p3: reg+i+1);
141948 }
141949 }
141950 sqlite3ExprListDelete(db, pList: pNew);
141951 pParse->eTriggerOp = 0;
141952 pParse->pTriggerTab = 0;
141953}
141954
141955
141956
141957/*
141958** Generate VDBE code for the statements inside the body of a single
141959** trigger.
141960*/
141961static int codeTriggerProgram(
141962 Parse *pParse, /* The parser context */
141963 TriggerStep *pStepList, /* List of statements inside the trigger body */
141964 int orconf /* Conflict algorithm. (OE_Abort, etc) */
141965){
141966 TriggerStep *pStep;
141967 Vdbe *v = pParse->pVdbe;
141968 sqlite3 *db = pParse->db;
141969
141970 assert( pParse->pTriggerTab && pParse->pToplevel );
141971 assert( pStepList );
141972 assert( v!=0 );
141973 for(pStep=pStepList; pStep; pStep=pStep->pNext){
141974 /* Figure out the ON CONFLICT policy that will be used for this step
141975 ** of the trigger program. If the statement that caused this trigger
141976 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
141977 ** the ON CONFLICT policy that was specified as part of the trigger
141978 ** step statement. Example:
141979 **
141980 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
141981 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
141982 ** END;
141983 **
141984 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
141985 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
141986 */
141987 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
141988 assert( pParse->okConstFactor==0 );
141989
141990#ifndef SQLITE_OMIT_TRACE
141991 if( pStep->zSpan ){
141992 sqlite3VdbeAddOp4(p: v, OP_Trace, p1: 0x7fffffff, p2: 1, p3: 0,
141993 zP4: sqlite3MPrintf(db, zFormat: "-- %s", pStep->zSpan),
141994 P4_DYNAMIC);
141995 }
141996#endif
141997
141998 switch( pStep->op ){
141999 case TK_UPDATE: {
142000 sqlite3Update(pParse,
142001 sqlite3TriggerStepSrc(pParse, pStep),
142002 sqlite3ExprListDup(db, p: pStep->pExprList, flags: 0),
142003 sqlite3ExprDup(db, p: pStep->pWhere, flags: 0),
142004 pParse->eOrconf, 0, 0, 0
142005 );
142006 sqlite3VdbeAddOp0(p: v, OP_ResetCount);
142007 break;
142008 }
142009 case TK_INSERT: {
142010 sqlite3Insert(pParse,
142011 pTabList: sqlite3TriggerStepSrc(pParse, pStep),
142012 pSelect: sqlite3SelectDup(db, pDup: pStep->pSelect, flags: 0),
142013 pColumn: sqlite3IdListDup(db, p: pStep->pIdList),
142014 onError: pParse->eOrconf,
142015 pUpsert: sqlite3UpsertDup(db, pStep->pUpsert)
142016 );
142017 sqlite3VdbeAddOp0(p: v, OP_ResetCount);
142018 break;
142019 }
142020 case TK_DELETE: {
142021 sqlite3DeleteFrom(pParse,
142022 pTabList: sqlite3TriggerStepSrc(pParse, pStep),
142023 pWhere: sqlite3ExprDup(db, p: pStep->pWhere, flags: 0), pOrderBy: 0, pLimit: 0
142024 );
142025 sqlite3VdbeAddOp0(p: v, OP_ResetCount);
142026 break;
142027 }
142028 default: assert( pStep->op==TK_SELECT ); {
142029 SelectDest sDest;
142030 Select *pSelect = sqlite3SelectDup(db, pDup: pStep->pSelect, flags: 0);
142031 sqlite3SelectDestInit(pDest: &sDest, SRT_Discard, iParm: 0);
142032 sqlite3Select(pParse, p: pSelect, pDest: &sDest);
142033 sqlite3SelectDelete(db, p: pSelect);
142034 break;
142035 }
142036 }
142037 }
142038
142039 return 0;
142040}
142041
142042#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
142043/*
142044** This function is used to add VdbeComment() annotations to a VDBE
142045** program. It is not used in production code, only for debugging.
142046*/
142047static const char *onErrorText(int onError){
142048 switch( onError ){
142049 case OE_Abort: return "abort";
142050 case OE_Rollback: return "rollback";
142051 case OE_Fail: return "fail";
142052 case OE_Replace: return "replace";
142053 case OE_Ignore: return "ignore";
142054 case OE_Default: return "default";
142055 }
142056 return "n/a";
142057}
142058#endif
142059
142060/*
142061** Parse context structure pFrom has just been used to create a sub-vdbe
142062** (trigger program). If an error has occurred, transfer error information
142063** from pFrom to pTo.
142064*/
142065static void transferParseError(Parse *pTo, Parse *pFrom){
142066 assert( pFrom->zErrMsg==0 || pFrom->nErr );
142067 assert( pTo->zErrMsg==0 || pTo->nErr );
142068 if( pTo->nErr==0 ){
142069 pTo->zErrMsg = pFrom->zErrMsg;
142070 pTo->nErr = pFrom->nErr;
142071 pTo->rc = pFrom->rc;
142072 }else{
142073 sqlite3DbFree(db: pFrom->db, p: pFrom->zErrMsg);
142074 }
142075}
142076
142077/*
142078** Create and populate a new TriggerPrg object with a sub-program
142079** implementing trigger pTrigger with ON CONFLICT policy orconf.
142080*/
142081static TriggerPrg *codeRowTrigger(
142082 Parse *pParse, /* Current parse context */
142083 Trigger *pTrigger, /* Trigger to code */
142084 Table *pTab, /* The table pTrigger is attached to */
142085 int orconf /* ON CONFLICT policy to code trigger program with */
142086){
142087 Parse *pTop = sqlite3ParseToplevel(pParse);
142088 sqlite3 *db = pParse->db; /* Database handle */
142089 TriggerPrg *pPrg; /* Value to return */
142090 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */
142091 Vdbe *v; /* Temporary VM */
142092 NameContext sNC; /* Name context for sub-vdbe */
142093 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
142094 Parse *pSubParse; /* Parse context for sub-vdbe */
142095 int iEndTrigger = 0; /* Label to jump to if WHEN is false */
142096
142097 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
142098 assert( pTop->pVdbe );
142099
142100 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
142101 ** are freed if an error occurs, link them into the Parse.pTriggerPrg
142102 ** list of the top-level Parse object sooner rather than later. */
142103 pPrg = sqlite3DbMallocZero(db, n: sizeof(TriggerPrg));
142104 if( !pPrg ) return 0;
142105 pPrg->pNext = pTop->pTriggerPrg;
142106 pTop->pTriggerPrg = pPrg;
142107 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, n: sizeof(SubProgram));
142108 if( !pProgram ) return 0;
142109 sqlite3VdbeLinkSubProgram(pVdbe: pTop->pVdbe, p: pProgram);
142110 pPrg->pTrigger = pTrigger;
142111 pPrg->orconf = orconf;
142112 pPrg->aColmask[0] = 0xffffffff;
142113 pPrg->aColmask[1] = 0xffffffff;
142114
142115 /* Allocate and populate a new Parse context to use for coding the
142116 ** trigger sub-program. */
142117 pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
142118 if( !pSubParse ) return 0;
142119 memset(s: &sNC, c: 0, n: sizeof(sNC));
142120 sNC.pParse = pSubParse;
142121 pSubParse->db = db;
142122 pSubParse->pTriggerTab = pTab;
142123 pSubParse->pToplevel = pTop;
142124 pSubParse->zAuthContext = pTrigger->zName;
142125 pSubParse->eTriggerOp = pTrigger->op;
142126 pSubParse->nQueryLoop = pParse->nQueryLoop;
142127 pSubParse->disableVtab = pParse->disableVtab;
142128
142129 v = sqlite3GetVdbe(pParse: pSubParse);
142130 if( v ){
142131 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
142132 pTrigger->zName, onErrorText(orconf),
142133 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
142134 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
142135 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
142136 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
142137 pTab->zName
142138 ));
142139#ifndef SQLITE_OMIT_TRACE
142140 if( pTrigger->zName ){
142141 sqlite3VdbeChangeP4(p: v, addr: -1,
142142 zP4: sqlite3MPrintf(db, zFormat: "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
142143 );
142144 }
142145#endif
142146
142147 /* If one was specified, code the WHEN clause. If it evaluates to false
142148 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
142149 ** OP_Halt inserted at the end of the program. */
142150 if( pTrigger->pWhen ){
142151 pWhen = sqlite3ExprDup(db, p: pTrigger->pWhen, flags: 0);
142152 if( db->mallocFailed==0
142153 && SQLITE_OK==sqlite3ResolveExprNames(pNC: &sNC, pExpr: pWhen)
142154 ){
142155 iEndTrigger = sqlite3VdbeMakeLabel(pParse: pSubParse);
142156 sqlite3ExprIfFalse(pParse: pSubParse, pExpr: pWhen, dest: iEndTrigger, SQLITE_JUMPIFNULL);
142157 }
142158 sqlite3ExprDelete(db, p: pWhen);
142159 }
142160
142161 /* Code the trigger program into the sub-vdbe. */
142162 codeTriggerProgram(pParse: pSubParse, pStepList: pTrigger->step_list, orconf);
142163
142164 /* Insert an OP_Halt at the end of the sub-program. */
142165 if( iEndTrigger ){
142166 sqlite3VdbeResolveLabel(v, x: iEndTrigger);
142167 }
142168 sqlite3VdbeAddOp0(p: v, OP_Halt);
142169 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
142170
142171 transferParseError(pTo: pParse, pFrom: pSubParse);
142172 if( db->mallocFailed==0 && pParse->nErr==0 ){
142173 pProgram->aOp = sqlite3VdbeTakeOpArray(p: v, pnOp: &pProgram->nOp, pnMaxArg: &pTop->nMaxArg);
142174 }
142175 pProgram->nMem = pSubParse->nMem;
142176 pProgram->nCsr = pSubParse->nTab;
142177 pProgram->token = (void *)pTrigger;
142178 pPrg->aColmask[0] = pSubParse->oldmask;
142179 pPrg->aColmask[1] = pSubParse->newmask;
142180 sqlite3VdbeDelete(p: v);
142181 }
142182
142183 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
142184 sqlite3ParserReset(pParse: pSubParse);
142185 sqlite3StackFree(db, pSubParse);
142186
142187 return pPrg;
142188}
142189
142190/*
142191** Return a pointer to a TriggerPrg object containing the sub-program for
142192** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
142193** TriggerPrg object exists, a new object is allocated and populated before
142194** being returned.
142195*/
142196static TriggerPrg *getRowTrigger(
142197 Parse *pParse, /* Current parse context */
142198 Trigger *pTrigger, /* Trigger to code */
142199 Table *pTab, /* The table trigger pTrigger is attached to */
142200 int orconf /* ON CONFLICT algorithm. */
142201){
142202 Parse *pRoot = sqlite3ParseToplevel(pParse);
142203 TriggerPrg *pPrg;
142204
142205 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
142206
142207 /* It may be that this trigger has already been coded (or is in the
142208 ** process of being coded). If this is the case, then an entry with
142209 ** a matching TriggerPrg.pTrigger field will be present somewhere
142210 ** in the Parse.pTriggerPrg list. Search for such an entry. */
142211 for(pPrg=pRoot->pTriggerPrg;
142212 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
142213 pPrg=pPrg->pNext
142214 );
142215
142216 /* If an existing TriggerPrg could not be located, create a new one. */
142217 if( !pPrg ){
142218 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
142219 }
142220
142221 return pPrg;
142222}
142223
142224/*
142225** Generate code for the trigger program associated with trigger p on
142226** table pTab. The reg, orconf and ignoreJump parameters passed to this
142227** function are the same as those described in the header function for
142228** sqlite3CodeRowTrigger()
142229*/
142230SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect(
142231 Parse *pParse, /* Parse context */
142232 Trigger *p, /* Trigger to code */
142233 Table *pTab, /* The table to code triggers from */
142234 int reg, /* Reg array containing OLD.* and NEW.* values */
142235 int orconf, /* ON CONFLICT policy */
142236 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
142237){
142238 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
142239 TriggerPrg *pPrg;
142240 pPrg = getRowTrigger(pParse, pTrigger: p, pTab, orconf);
142241 assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
142242
142243 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
142244 ** is a pointer to the sub-vdbe containing the trigger program. */
142245 if( pPrg ){
142246 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
142247
142248 sqlite3VdbeAddOp4(p: v, OP_Program, p1: reg, p2: ignoreJump, p3: ++pParse->nMem,
142249 zP4: (const char *)pPrg->pProgram, P4_SUBPROGRAM);
142250 VdbeComment(
142251 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
142252
142253 /* Set the P5 operand of the OP_Program instruction to non-zero if
142254 ** recursive invocation of this trigger program is disallowed. Recursive
142255 ** invocation is disallowed if (a) the sub-program is really a trigger,
142256 ** not a foreign key action, and (b) the flag to enable recursive triggers
142257 ** is clear. */
142258 sqlite3VdbeChangeP5(p: v, p5: (u8)bRecursive);
142259 }
142260}
142261
142262/*
142263** This is called to code the required FOR EACH ROW triggers for an operation
142264** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
142265** is given by the op parameter. The tr_tm parameter determines whether the
142266** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
142267** parameter pChanges is passed the list of columns being modified.
142268**
142269** If there are no triggers that fire at the specified time for the specified
142270** operation on pTab, this function is a no-op.
142271**
142272** The reg argument is the address of the first in an array of registers
142273** that contain the values substituted for the new.* and old.* references
142274** in the trigger program. If N is the number of columns in table pTab
142275** (a copy of pTab->nCol), then registers are populated as follows:
142276**
142277** Register Contains
142278** ------------------------------------------------------
142279** reg+0 OLD.rowid
142280** reg+1 OLD.* value of left-most column of pTab
142281** ... ...
142282** reg+N OLD.* value of right-most column of pTab
142283** reg+N+1 NEW.rowid
142284** reg+N+2 NEW.* value of left-most column of pTab
142285** ... ...
142286** reg+N+N+1 NEW.* value of right-most column of pTab
142287**
142288** For ON DELETE triggers, the registers containing the NEW.* values will
142289** never be accessed by the trigger program, so they are not allocated or
142290** populated by the caller (there is no data to populate them with anyway).
142291** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
142292** are never accessed, and so are not allocated by the caller. So, for an
142293** ON INSERT trigger, the value passed to this function as parameter reg
142294** is not a readable register, although registers (reg+N) through
142295** (reg+N+N+1) are.
142296**
142297** Parameter orconf is the default conflict resolution algorithm for the
142298** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
142299** is the instruction that control should jump to if a trigger program
142300** raises an IGNORE exception.
142301*/
142302SQLITE_PRIVATE void sqlite3CodeRowTrigger(
142303 Parse *pParse, /* Parse context */
142304 Trigger *pTrigger, /* List of triggers on table pTab */
142305 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
142306 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
142307 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
142308 Table *pTab, /* The table to code triggers from */
142309 int reg, /* The first in an array of registers (see above) */
142310 int orconf, /* ON CONFLICT policy */
142311 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
142312){
142313 Trigger *p; /* Used to iterate through pTrigger list */
142314
142315 assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
142316 assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
142317 assert( (op==TK_UPDATE)==(pChanges!=0) );
142318
142319 for(p=pTrigger; p; p=p->pNext){
142320
142321 /* Sanity checking: The schema for the trigger and for the table are
142322 ** always defined. The trigger must be in the same schema as the table
142323 ** or else it must be a TEMP trigger. */
142324 assert( p->pSchema!=0 );
142325 assert( p->pTabSchema!=0 );
142326 assert( p->pSchema==p->pTabSchema
142327 || p->pSchema==pParse->db->aDb[1].pSchema );
142328
142329 /* Determine whether we should code this trigger. One of two choices:
142330 ** 1. The trigger is an exact match to the current DML statement
142331 ** 2. This is a RETURNING trigger for INSERT but we are currently
142332 ** doing the UPDATE part of an UPSERT.
142333 */
142334 if( (p->op==op || (p->bReturning && p->op==TK_INSERT && op==TK_UPDATE))
142335 && p->tr_tm==tr_tm
142336 && checkColumnOverlap(pIdList: p->pColumns, pEList: pChanges)
142337 ){
142338 if( !p->bReturning ){
142339 sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
142340 }else if( sqlite3IsToplevel(pParse) ){
142341 codeReturningTrigger(pParse, pTrigger: p, pTab, regIn: reg);
142342 }
142343 }
142344 }
142345}
142346
142347/*
142348** Triggers may access values stored in the old.* or new.* pseudo-table.
142349** This function returns a 32-bit bitmask indicating which columns of the
142350** old.* or new.* tables actually are used by triggers. This information
142351** may be used by the caller, for example, to avoid having to load the entire
142352** old.* record into memory when executing an UPDATE or DELETE command.
142353**
142354** Bit 0 of the returned mask is set if the left-most column of the
142355** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
142356** the second leftmost column value is required, and so on. If there
142357** are more than 32 columns in the table, and at least one of the columns
142358** with an index greater than 32 may be accessed, 0xffffffff is returned.
142359**
142360** It is not possible to determine if the old.rowid or new.rowid column is
142361** accessed by triggers. The caller must always assume that it is.
142362**
142363** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
142364** applies to the old.* table. If 1, the new.* table.
142365**
142366** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
142367** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
142368** included in the returned mask if the TRIGGER_BEFORE bit is set in the
142369** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
142370** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
142371*/
142372SQLITE_PRIVATE u32 sqlite3TriggerColmask(
142373 Parse *pParse, /* Parse context */
142374 Trigger *pTrigger, /* List of triggers on table pTab */
142375 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
142376 int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */
142377 int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
142378 Table *pTab, /* The table to code triggers from */
142379 int orconf /* Default ON CONFLICT policy for trigger steps */
142380){
142381 const int op = pChanges ? TK_UPDATE : TK_DELETE;
142382 u32 mask = 0;
142383 Trigger *p;
142384
142385 assert( isNew==1 || isNew==0 );
142386 for(p=pTrigger; p; p=p->pNext){
142387 if( p->op==op
142388 && (tr_tm&p->tr_tm)
142389 && checkColumnOverlap(pIdList: p->pColumns,pEList: pChanges)
142390 ){
142391 if( p->bReturning ){
142392 mask = 0xffffffff;
142393 }else{
142394 TriggerPrg *pPrg;
142395 pPrg = getRowTrigger(pParse, pTrigger: p, pTab, orconf);
142396 if( pPrg ){
142397 mask |= pPrg->aColmask[isNew];
142398 }
142399 }
142400 }
142401 }
142402
142403 return mask;
142404}
142405
142406#endif /* !defined(SQLITE_OMIT_TRIGGER) */
142407
142408/************** End of trigger.c *********************************************/
142409/************** Begin file update.c ******************************************/
142410/*
142411** 2001 September 15
142412**
142413** The author disclaims copyright to this source code. In place of
142414** a legal notice, here is a blessing:
142415**
142416** May you do good and not evil.
142417** May you find forgiveness for yourself and forgive others.
142418** May you share freely, never taking more than you give.
142419**
142420*************************************************************************
142421** This file contains C code routines that are called by the parser
142422** to handle UPDATE statements.
142423*/
142424/* #include "sqliteInt.h" */
142425
142426#ifndef SQLITE_OMIT_VIRTUALTABLE
142427/* Forward declaration */
142428static void updateVirtualTable(
142429 Parse *pParse, /* The parsing context */
142430 SrcList *pSrc, /* The virtual table to be modified */
142431 Table *pTab, /* The virtual table */
142432 ExprList *pChanges, /* The columns to change in the UPDATE statement */
142433 Expr *pRowidExpr, /* Expression used to recompute the rowid */
142434 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
142435 Expr *pWhere, /* WHERE clause of the UPDATE statement */
142436 int onError /* ON CONFLICT strategy */
142437);
142438#endif /* SQLITE_OMIT_VIRTUALTABLE */
142439
142440/*
142441** The most recently coded instruction was an OP_Column to retrieve the
142442** i-th column of table pTab. This routine sets the P4 parameter of the
142443** OP_Column to the default value, if any.
142444**
142445** The default value of a column is specified by a DEFAULT clause in the
142446** column definition. This was either supplied by the user when the table
142447** was created, or added later to the table definition by an ALTER TABLE
142448** command. If the latter, then the row-records in the table btree on disk
142449** may not contain a value for the column and the default value, taken
142450** from the P4 parameter of the OP_Column instruction, is returned instead.
142451** If the former, then all row-records are guaranteed to include a value
142452** for the column and the P4 value is not required.
142453**
142454** Column definitions created by an ALTER TABLE command may only have
142455** literal default values specified: a number, null or a string. (If a more
142456** complicated default expression value was provided, it is evaluated
142457** when the ALTER TABLE is executed and one of the literal values written
142458** into the sqlite_schema table.)
142459**
142460** Therefore, the P4 parameter is only required if the default value for
142461** the column is a literal number, string or null. The sqlite3ValueFromExpr()
142462** function is capable of transforming these types of expressions into
142463** sqlite3_value objects.
142464**
142465** If column as REAL affinity and the table is an ordinary b-tree table
142466** (not a virtual table) then the value might have been stored as an
142467** integer. In that case, add an OP_RealAffinity opcode to make sure
142468** it has been converted into REAL.
142469*/
142470SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
142471 assert( pTab!=0 );
142472 if( !IsView(pTab) ){
142473 sqlite3_value *pValue = 0;
142474 u8 enc = ENC(sqlite3VdbeDb(v));
142475 Column *pCol = &pTab->aCol[i];
142476 VdbeComment((v, "%s.%s", pTab->zName, pCol->zCnName));
142477 assert( i<pTab->nCol );
142478 sqlite3ValueFromExpr(db: sqlite3VdbeDb(v),
142479 pExpr: sqlite3ColumnExpr(pTab,pCol), enc,
142480 affinity: pCol->affinity, ppVal: &pValue);
142481 if( pValue ){
142482 sqlite3VdbeAppendP4(p: v, pP4: pValue, P4_MEM);
142483 }
142484 }
142485#ifndef SQLITE_OMIT_FLOATING_POINT
142486 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL && !IsVirtual(pTab) ){
142487 sqlite3VdbeAddOp1(p: v, OP_RealAffinity, p1: iReg);
142488 }
142489#endif
142490}
142491
142492/*
142493** Check to see if column iCol of index pIdx references any of the
142494** columns defined by aXRef and chngRowid. Return true if it does
142495** and false if not. This is an optimization. False-positives are a
142496** performance degradation, but false-negatives can result in a corrupt
142497** index and incorrect answers.
142498**
142499** aXRef[j] will be non-negative if column j of the original table is
142500** being updated. chngRowid will be true if the rowid of the table is
142501** being updated.
142502*/
142503static int indexColumnIsBeingUpdated(
142504 Index *pIdx, /* The index to check */
142505 int iCol, /* Which column of the index to check */
142506 int *aXRef, /* aXRef[j]>=0 if column j is being updated */
142507 int chngRowid /* true if the rowid is being updated */
142508){
142509 i16 iIdxCol = pIdx->aiColumn[iCol];
142510 assert( iIdxCol!=XN_ROWID ); /* Cannot index rowid */
142511 if( iIdxCol>=0 ){
142512 return aXRef[iIdxCol]>=0;
142513 }
142514 assert( iIdxCol==XN_EXPR );
142515 assert( pIdx->aColExpr!=0 );
142516 assert( pIdx->aColExpr->a[iCol].pExpr!=0 );
142517 return sqlite3ExprReferencesUpdatedColumn(pExpr: pIdx->aColExpr->a[iCol].pExpr,
142518 aiChng: aXRef,chngRowid);
142519}
142520
142521/*
142522** Check to see if index pIdx is a partial index whose conditional
142523** expression might change values due to an UPDATE. Return true if
142524** the index is subject to change and false if the index is guaranteed
142525** to be unchanged. This is an optimization. False-positives are a
142526** performance degradation, but false-negatives can result in a corrupt
142527** index and incorrect answers.
142528**
142529** aXRef[j] will be non-negative if column j of the original table is
142530** being updated. chngRowid will be true if the rowid of the table is
142531** being updated.
142532*/
142533static int indexWhereClauseMightChange(
142534 Index *pIdx, /* The index to check */
142535 int *aXRef, /* aXRef[j]>=0 if column j is being updated */
142536 int chngRowid /* true if the rowid is being updated */
142537){
142538 if( pIdx->pPartIdxWhere==0 ) return 0;
142539 return sqlite3ExprReferencesUpdatedColumn(pExpr: pIdx->pPartIdxWhere,
142540 aiChng: aXRef, chngRowid);
142541}
142542
142543/*
142544** Allocate and return a pointer to an expression of type TK_ROW with
142545** Expr.iColumn set to value (iCol+1). The resolver will modify the
142546** expression to be a TK_COLUMN reading column iCol of the first
142547** table in the source-list (pSrc->a[0]).
142548*/
142549static Expr *exprRowColumn(Parse *pParse, int iCol){
142550 Expr *pRet = sqlite3PExpr(pParse, TK_ROW, pLeft: 0, pRight: 0);
142551 if( pRet ) pRet->iColumn = iCol+1;
142552 return pRet;
142553}
142554
142555/*
142556** Assuming both the pLimit and pOrderBy parameters are NULL, this function
142557** generates VM code to run the query:
142558**
142559** SELECT <other-columns>, pChanges FROM pTabList WHERE pWhere
142560**
142561** and write the results to the ephemeral table already opened as cursor
142562** iEph. None of pChanges, pTabList or pWhere are modified or consumed by
142563** this function, they must be deleted by the caller.
142564**
142565** Or, if pLimit and pOrderBy are not NULL, and pTab is not a view:
142566**
142567** SELECT <other-columns>, pChanges FROM pTabList
142568** WHERE pWhere
142569** GROUP BY <other-columns>
142570** ORDER BY pOrderBy LIMIT pLimit
142571**
142572** If pTab is a view, the GROUP BY clause is omitted.
142573**
142574** Exactly how results are written to table iEph, and exactly what
142575** the <other-columns> in the query above are is determined by the type
142576** of table pTabList->a[0].pTab.
142577**
142578** If the table is a WITHOUT ROWID table, then argument pPk must be its
142579** PRIMARY KEY. In this case <other-columns> are the primary key columns
142580** of the table, in order. The results of the query are written to ephemeral
142581** table iEph as index keys, using OP_IdxInsert.
142582**
142583** If the table is actually a view, then <other-columns> are all columns of
142584** the view. The results are written to the ephemeral table iEph as records
142585** with automatically assigned integer keys.
142586**
142587** If the table is a virtual or ordinary intkey table, then <other-columns>
142588** is its rowid. For a virtual table, the results are written to iEph as
142589** records with automatically assigned integer keys For intkey tables, the
142590** rowid value in <other-columns> is used as the integer key, and the
142591** remaining fields make up the table record.
142592*/
142593static void updateFromSelect(
142594 Parse *pParse, /* Parse context */
142595 int iEph, /* Cursor for open eph. table */
142596 Index *pPk, /* PK if table 0 is WITHOUT ROWID */
142597 ExprList *pChanges, /* List of expressions to return */
142598 SrcList *pTabList, /* List of tables to select from */
142599 Expr *pWhere, /* WHERE clause for query */
142600 ExprList *pOrderBy, /* ORDER BY clause */
142601 Expr *pLimit /* LIMIT clause */
142602){
142603 int i;
142604 SelectDest dest;
142605 Select *pSelect = 0;
142606 ExprList *pList = 0;
142607 ExprList *pGrp = 0;
142608 Expr *pLimit2 = 0;
142609 ExprList *pOrderBy2 = 0;
142610 sqlite3 *db = pParse->db;
142611 Table *pTab = pTabList->a[0].pTab;
142612 SrcList *pSrc;
142613 Expr *pWhere2;
142614 int eDest;
142615
142616#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
142617 if( pOrderBy && pLimit==0 ) {
142618 sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on UPDATE");
142619 return;
142620 }
142621 pOrderBy2 = sqlite3ExprListDup(db, pOrderBy, 0);
142622 pLimit2 = sqlite3ExprDup(db, pLimit, 0);
142623#else
142624 UNUSED_PARAMETER(pOrderBy);
142625 UNUSED_PARAMETER(pLimit);
142626#endif
142627
142628 pSrc = sqlite3SrcListDup(db, p: pTabList, flags: 0);
142629 pWhere2 = sqlite3ExprDup(db, p: pWhere, flags: 0);
142630
142631 assert( pTabList->nSrc>1 );
142632 if( pSrc ){
142633 pSrc->a[0].fg.notCte = 1;
142634 pSrc->a[0].iCursor = -1;
142635 pSrc->a[0].pTab->nTabRef--;
142636 pSrc->a[0].pTab = 0;
142637 }
142638 if( pPk ){
142639 for(i=0; i<pPk->nKeyCol; i++){
142640 Expr *pNew = exprRowColumn(pParse, iCol: pPk->aiColumn[i]);
142641#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
142642 if( pLimit ){
142643 pGrp = sqlite3ExprListAppend(pParse, pGrp, sqlite3ExprDup(db, pNew, 0));
142644 }
142645#endif
142646 pList = sqlite3ExprListAppend(pParse, pList, pExpr: pNew);
142647 }
142648 eDest = IsVirtual(pTab) ? SRT_Table : SRT_Upfrom;
142649 }else if( IsView(pTab) ){
142650 for(i=0; i<pTab->nCol; i++){
142651 pList = sqlite3ExprListAppend(pParse, pList, pExpr: exprRowColumn(pParse, iCol: i));
142652 }
142653 eDest = SRT_Table;
142654 }else{
142655 eDest = IsVirtual(pTab) ? SRT_Table : SRT_Upfrom;
142656 pList = sqlite3ExprListAppend(pParse, pList: 0, pExpr: sqlite3PExpr(pParse,TK_ROW,pLeft: 0,pRight: 0));
142657#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
142658 if( pLimit ){
142659 pGrp = sqlite3ExprListAppend(pParse, 0, sqlite3PExpr(pParse,TK_ROW,0,0));
142660 }
142661#endif
142662 }
142663 assert( pChanges!=0 || pParse->db->mallocFailed );
142664 if( pChanges ){
142665 for(i=0; i<pChanges->nExpr; i++){
142666 pList = sqlite3ExprListAppend(pParse, pList,
142667 pExpr: sqlite3ExprDup(db, p: pChanges->a[i].pExpr, flags: 0)
142668 );
142669 }
142670 }
142671 pSelect = sqlite3SelectNew(pParse, pEList: pList,
142672 pSrc, pWhere: pWhere2, pGroupBy: pGrp, pHaving: 0, pOrderBy: pOrderBy2, SF_UFSrcCheck|SF_IncludeHidden, pLimit: pLimit2
142673 );
142674 if( pSelect ) pSelect->selFlags |= SF_OrderByReqd;
142675 sqlite3SelectDestInit(pDest: &dest, eDest, iParm: iEph);
142676 dest.iSDParm2 = (pPk ? pPk->nKeyCol : -1);
142677 sqlite3Select(pParse, p: pSelect, pDest: &dest);
142678 sqlite3SelectDelete(db, p: pSelect);
142679}
142680
142681/*
142682** Process an UPDATE statement.
142683**
142684** UPDATE OR IGNORE tbl SET a=b, c=d FROM tbl2... WHERE e<5 AND f NOT NULL;
142685** \_______/ \_/ \______/ \_____/ \________________/
142686** onError | pChanges | pWhere
142687** \_______________________/
142688** pTabList
142689*/
142690SQLITE_PRIVATE void sqlite3Update(
142691 Parse *pParse, /* The parser context */
142692 SrcList *pTabList, /* The table in which we should change things */
142693 ExprList *pChanges, /* Things to be changed */
142694 Expr *pWhere, /* The WHERE clause. May be null */
142695 int onError, /* How to handle constraint errors */
142696 ExprList *pOrderBy, /* ORDER BY clause. May be null */
142697 Expr *pLimit, /* LIMIT clause. May be null */
142698 Upsert *pUpsert /* ON CONFLICT clause, or null */
142699){
142700 int i, j, k; /* Loop counters */
142701 Table *pTab; /* The table to be updated */
142702 int addrTop = 0; /* VDBE instruction address of the start of the loop */
142703 WhereInfo *pWInfo = 0; /* Information about the WHERE clause */
142704 Vdbe *v; /* The virtual database engine */
142705 Index *pIdx; /* For looping over indices */
142706 Index *pPk; /* The PRIMARY KEY index for WITHOUT ROWID tables */
142707 int nIdx; /* Number of indices that need updating */
142708 int nAllIdx; /* Total number of indexes */
142709 int iBaseCur; /* Base cursor number */
142710 int iDataCur; /* Cursor for the canonical data btree */
142711 int iIdxCur; /* Cursor for the first index */
142712 sqlite3 *db; /* The database structure */
142713 int *aRegIdx = 0; /* Registers for to each index and the main table */
142714 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
142715 ** an expression for the i-th column of the table.
142716 ** aXRef[i]==-1 if the i-th column is not changed. */
142717 u8 *aToOpen; /* 1 for tables and indices to be opened */
142718 u8 chngPk; /* PRIMARY KEY changed in a WITHOUT ROWID table */
142719 u8 chngRowid; /* Rowid changed in a normal table */
142720 u8 chngKey; /* Either chngPk or chngRowid */
142721 Expr *pRowidExpr = 0; /* Expression defining the new record number */
142722 int iRowidExpr = -1; /* Index of "rowid=" (or IPK) assignment in pChanges */
142723 AuthContext sContext; /* The authorization context */
142724 NameContext sNC; /* The name-context to resolve expressions in */
142725 int iDb; /* Database containing the table being updated */
142726 int eOnePass; /* ONEPASS_XXX value from where.c */
142727 int hasFK; /* True if foreign key processing is required */
142728 int labelBreak; /* Jump here to break out of UPDATE loop */
142729 int labelContinue; /* Jump here to continue next step of UPDATE loop */
142730 int flags; /* Flags for sqlite3WhereBegin() */
142731
142732#ifndef SQLITE_OMIT_TRIGGER
142733 int isView; /* True when updating a view (INSTEAD OF trigger) */
142734 Trigger *pTrigger; /* List of triggers on pTab, if required */
142735 int tmask; /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
142736#endif
142737 int newmask; /* Mask of NEW.* columns accessed by BEFORE triggers */
142738 int iEph = 0; /* Ephemeral table holding all primary key values */
142739 int nKey = 0; /* Number of elements in regKey for WITHOUT ROWID */
142740 int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */
142741 int addrOpen = 0; /* Address of OP_OpenEphemeral */
142742 int iPk = 0; /* First of nPk cells holding PRIMARY KEY value */
142743 i16 nPk = 0; /* Number of components of the PRIMARY KEY */
142744 int bReplace = 0; /* True if REPLACE conflict resolution might happen */
142745 int bFinishSeek = 1; /* The OP_FinishSeek opcode is needed */
142746 int nChangeFrom = 0; /* If there is a FROM, pChanges->nExpr, else 0 */
142747
142748 /* Register Allocations */
142749 int regRowCount = 0; /* A count of rows changed */
142750 int regOldRowid = 0; /* The old rowid */
142751 int regNewRowid = 0; /* The new rowid */
142752 int regNew = 0; /* Content of the NEW.* table in triggers */
142753 int regOld = 0; /* Content of OLD.* table in triggers */
142754 int regRowSet = 0; /* Rowset of rows to be updated */
142755 int regKey = 0; /* composite PRIMARY KEY value */
142756
142757 memset(s: &sContext, c: 0, n: sizeof(sContext));
142758 db = pParse->db;
142759 if( pParse->nErr || db->mallocFailed ){
142760 goto update_cleanup;
142761 }
142762
142763 /* Locate the table which we want to update.
142764 */
142765 pTab = sqlite3SrcListLookup(pParse, pSrc: pTabList);
142766 if( pTab==0 ) goto update_cleanup;
142767 iDb = sqlite3SchemaToIndex(db: pParse->db, pSchema: pTab->pSchema);
142768
142769 /* Figure out if we have any triggers and if the table being
142770 ** updated is a view.
142771 */
142772#ifndef SQLITE_OMIT_TRIGGER
142773 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges, pMask: &tmask);
142774 isView = IsView(pTab);
142775 assert( pTrigger || tmask==0 );
142776#else
142777# define pTrigger 0
142778# define isView 0
142779# define tmask 0
142780#endif
142781#ifdef SQLITE_OMIT_VIEW
142782# undef isView
142783# define isView 0
142784#endif
142785
142786 /* If there was a FROM clause, set nChangeFrom to the number of expressions
142787 ** in the change-list. Otherwise, set it to 0. There cannot be a FROM
142788 ** clause if this function is being called to generate code for part of
142789 ** an UPSERT statement. */
142790 nChangeFrom = (pTabList->nSrc>1) ? pChanges->nExpr : 0;
142791 assert( nChangeFrom==0 || pUpsert==0 );
142792
142793#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
142794 if( !isView && nChangeFrom==0 ){
142795 pWhere = sqlite3LimitWhere(
142796 pParse, pTabList, pWhere, pOrderBy, pLimit, "UPDATE"
142797 );
142798 pOrderBy = 0;
142799 pLimit = 0;
142800 }
142801#endif
142802
142803 if( sqlite3ViewGetColumnNames(pParse, pTable: pTab) ){
142804 goto update_cleanup;
142805 }
142806 if( sqlite3IsReadOnly(pParse, pTab, viewOk: tmask) ){
142807 goto update_cleanup;
142808 }
142809
142810 /* Allocate a cursors for the main database table and for all indices.
142811 ** The index cursors might not be used, but if they are used they
142812 ** need to occur right after the database cursor. So go ahead and
142813 ** allocate enough space, just in case.
142814 */
142815 iBaseCur = iDataCur = pParse->nTab++;
142816 iIdxCur = iDataCur+1;
142817 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
142818 testcase( pPk!=0 && pPk!=pTab->pIndex );
142819 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
142820 if( pPk==pIdx ){
142821 iDataCur = pParse->nTab;
142822 }
142823 pParse->nTab++;
142824 }
142825 if( pUpsert ){
142826 /* On an UPSERT, reuse the same cursors already opened by INSERT */
142827 iDataCur = pUpsert->iDataCur;
142828 iIdxCur = pUpsert->iIdxCur;
142829 pParse->nTab = iBaseCur;
142830 }
142831 pTabList->a[0].iCursor = iDataCur;
142832
142833 /* Allocate space for aXRef[], aRegIdx[], and aToOpen[].
142834 ** Initialize aXRef[] and aToOpen[] to their default values.
142835 */
142836 aXRef = sqlite3DbMallocRawNN(db, n: sizeof(int) * (pTab->nCol+nIdx+1) + nIdx+2 );
142837 if( aXRef==0 ) goto update_cleanup;
142838 aRegIdx = aXRef+pTab->nCol;
142839 aToOpen = (u8*)(aRegIdx+nIdx+1);
142840 memset(s: aToOpen, c: 1, n: nIdx+1);
142841 aToOpen[nIdx+1] = 0;
142842 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
142843
142844 /* Initialize the name-context */
142845 memset(s: &sNC, c: 0, n: sizeof(sNC));
142846 sNC.pParse = pParse;
142847 sNC.pSrcList = pTabList;
142848 sNC.uNC.pUpsert = pUpsert;
142849 sNC.ncFlags = NC_UUpsert;
142850
142851 /* Begin generating code. */
142852 v = sqlite3GetVdbe(pParse);
142853 if( v==0 ) goto update_cleanup;
142854
142855 /* Resolve the column names in all the expressions of the
142856 ** of the UPDATE statement. Also find the column index
142857 ** for each column to be updated in the pChanges array. For each
142858 ** column to be updated, make sure we have authorization to change
142859 ** that column.
142860 */
142861 chngRowid = chngPk = 0;
142862 for(i=0; i<pChanges->nExpr; i++){
142863 u8 hCol = sqlite3StrIHash(z: pChanges->a[i].zEName);
142864 /* If this is an UPDATE with a FROM clause, do not resolve expressions
142865 ** here. The call to sqlite3Select() below will do that. */
142866 if( nChangeFrom==0 && sqlite3ResolveExprNames(pNC: &sNC, pExpr: pChanges->a[i].pExpr) ){
142867 goto update_cleanup;
142868 }
142869 for(j=0; j<pTab->nCol; j++){
142870 if( pTab->aCol[j].hName==hCol
142871 && sqlite3StrICmp(zLeft: pTab->aCol[j].zCnName, zRight: pChanges->a[i].zEName)==0
142872 ){
142873 if( j==pTab->iPKey ){
142874 chngRowid = 1;
142875 pRowidExpr = pChanges->a[i].pExpr;
142876 iRowidExpr = i;
142877 }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY)!=0 ){
142878 chngPk = 1;
142879 }
142880#ifndef SQLITE_OMIT_GENERATED_COLUMNS
142881 else if( pTab->aCol[j].colFlags & COLFLAG_GENERATED ){
142882 testcase( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL );
142883 testcase( pTab->aCol[j].colFlags & COLFLAG_STORED );
142884 sqlite3ErrorMsg(pParse,
142885 zFormat: "cannot UPDATE generated column \"%s\"",
142886 pTab->aCol[j].zCnName);
142887 goto update_cleanup;
142888 }
142889#endif
142890 aXRef[j] = i;
142891 break;
142892 }
142893 }
142894 if( j>=pTab->nCol ){
142895 if( pPk==0 && sqlite3IsRowid(z: pChanges->a[i].zEName) ){
142896 j = -1;
142897 chngRowid = 1;
142898 pRowidExpr = pChanges->a[i].pExpr;
142899 iRowidExpr = i;
142900 }else{
142901 sqlite3ErrorMsg(pParse, zFormat: "no such column: %s", pChanges->a[i].zEName);
142902 pParse->checkSchema = 1;
142903 goto update_cleanup;
142904 }
142905 }
142906#ifndef SQLITE_OMIT_AUTHORIZATION
142907 {
142908 int rc;
142909 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, zArg1: pTab->zName,
142910 zArg2: j<0 ? "ROWID" : pTab->aCol[j].zCnName,
142911 zArg3: db->aDb[iDb].zDbSName);
142912 if( rc==SQLITE_DENY ){
142913 goto update_cleanup;
142914 }else if( rc==SQLITE_IGNORE ){
142915 aXRef[j] = -1;
142916 }
142917 }
142918#endif
142919 }
142920 assert( (chngRowid & chngPk)==0 );
142921 assert( chngRowid==0 || chngRowid==1 );
142922 assert( chngPk==0 || chngPk==1 );
142923 chngKey = chngRowid + chngPk;
142924
142925#ifndef SQLITE_OMIT_GENERATED_COLUMNS
142926 /* Mark generated columns as changing if their generator expressions
142927 ** reference any changing column. The actual aXRef[] value for
142928 ** generated expressions is not used, other than to check to see that it
142929 ** is non-negative, so the value of aXRef[] for generated columns can be
142930 ** set to any non-negative number. We use 99999 so that the value is
142931 ** obvious when looking at aXRef[] in a symbolic debugger.
142932 */
142933 if( pTab->tabFlags & TF_HasGenerated ){
142934 int bProgress;
142935 testcase( pTab->tabFlags & TF_HasVirtual );
142936 testcase( pTab->tabFlags & TF_HasStored );
142937 do{
142938 bProgress = 0;
142939 for(i=0; i<pTab->nCol; i++){
142940 if( aXRef[i]>=0 ) continue;
142941 if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 ) continue;
142942 if( sqlite3ExprReferencesUpdatedColumn(
142943 pExpr: sqlite3ColumnExpr(pTab, pCol: &pTab->aCol[i]),
142944 aiChng: aXRef, chngRowid)
142945 ){
142946 aXRef[i] = 99999;
142947 bProgress = 1;
142948 }
142949 }
142950 }while( bProgress );
142951 }
142952#endif
142953
142954 /* The SET expressions are not actually used inside the WHERE loop.
142955 ** So reset the colUsed mask. Unless this is a virtual table. In that
142956 ** case, set all bits of the colUsed mask (to ensure that the virtual
142957 ** table implementation makes all columns available).
142958 */
142959 pTabList->a[0].colUsed = IsVirtual(pTab) ? ALLBITS : 0;
142960
142961 hasFK = sqlite3FkRequired(pParse, pTab, aChange: aXRef, chngRowid: chngKey);
142962
142963 /* There is one entry in the aRegIdx[] array for each index on the table
142964 ** being updated. Fill in aRegIdx[] with a register number that will hold
142965 ** the key for accessing each index.
142966 */
142967 if( onError==OE_Replace ) bReplace = 1;
142968 for(nAllIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nAllIdx++){
142969 int reg;
142970 if( chngKey || hasFK>1 || pIdx==pPk
142971 || indexWhereClauseMightChange(pIdx,aXRef,chngRowid)
142972 ){
142973 reg = ++pParse->nMem;
142974 pParse->nMem += pIdx->nColumn;
142975 }else{
142976 reg = 0;
142977 for(i=0; i<pIdx->nKeyCol; i++){
142978 if( indexColumnIsBeingUpdated(pIdx, iCol: i, aXRef, chngRowid) ){
142979 reg = ++pParse->nMem;
142980 pParse->nMem += pIdx->nColumn;
142981 if( onError==OE_Default && pIdx->onError==OE_Replace ){
142982 bReplace = 1;
142983 }
142984 break;
142985 }
142986 }
142987 }
142988 if( reg==0 ) aToOpen[nAllIdx+1] = 0;
142989 aRegIdx[nAllIdx] = reg;
142990 }
142991 aRegIdx[nAllIdx] = ++pParse->nMem; /* Register storing the table record */
142992 if( bReplace ){
142993 /* If REPLACE conflict resolution might be invoked, open cursors on all
142994 ** indexes in case they are needed to delete records. */
142995 memset(s: aToOpen, c: 1, n: nIdx+1);
142996 }
142997
142998 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
142999 sqlite3BeginWriteOperation(pParse, setStatement: pTrigger || hasFK, iDb);
143000
143001 /* Allocate required registers. */
143002 if( !IsVirtual(pTab) ){
143003 /* For now, regRowSet and aRegIdx[nAllIdx] share the same register.
143004 ** If regRowSet turns out to be needed, then aRegIdx[nAllIdx] will be
143005 ** reallocated. aRegIdx[nAllIdx] is the register in which the main
143006 ** table record is written. regRowSet holds the RowSet for the
143007 ** two-pass update algorithm. */
143008 assert( aRegIdx[nAllIdx]==pParse->nMem );
143009 regRowSet = aRegIdx[nAllIdx];
143010 regOldRowid = regNewRowid = ++pParse->nMem;
143011 if( chngPk || pTrigger || hasFK ){
143012 regOld = pParse->nMem + 1;
143013 pParse->nMem += pTab->nCol;
143014 }
143015 if( chngKey || pTrigger || hasFK ){
143016 regNewRowid = ++pParse->nMem;
143017 }
143018 regNew = pParse->nMem + 1;
143019 pParse->nMem += pTab->nCol;
143020 }
143021
143022 /* Start the view context. */
143023 if( isView ){
143024 sqlite3AuthContextPush(pParse, pContext: &sContext, zContext: pTab->zName);
143025 }
143026
143027 /* If we are trying to update a view, realize that view into
143028 ** an ephemeral table.
143029 */
143030#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
143031 if( nChangeFrom==0 && isView ){
143032 sqlite3MaterializeView(pParse, pView: pTab,
143033 pWhere, pOrderBy, pLimit, iCur: iDataCur
143034 );
143035 pOrderBy = 0;
143036 pLimit = 0;
143037 }
143038#endif
143039
143040 /* Resolve the column names in all the expressions in the
143041 ** WHERE clause.
143042 */
143043 if( nChangeFrom==0 && sqlite3ResolveExprNames(pNC: &sNC, pExpr: pWhere) ){
143044 goto update_cleanup;
143045 }
143046
143047#ifndef SQLITE_OMIT_VIRTUALTABLE
143048 /* Virtual tables must be handled separately */
143049 if( IsVirtual(pTab) ){
143050 updateVirtualTable(pParse, pSrc: pTabList, pTab, pChanges, pRowidExpr, aXRef,
143051 pWhere, onError);
143052 goto update_cleanup;
143053 }
143054#endif
143055
143056 /* Jump to labelBreak to abandon further processing of this UPDATE */
143057 labelContinue = labelBreak = sqlite3VdbeMakeLabel(pParse);
143058
143059 /* Not an UPSERT. Normal processing. Begin by
143060 ** initialize the count of updated rows */
143061 if( (db->flags&SQLITE_CountRows)!=0
143062 && !pParse->pTriggerTab
143063 && !pParse->nested
143064 && !pParse->bReturning
143065 && pUpsert==0
143066 ){
143067 regRowCount = ++pParse->nMem;
143068 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: regRowCount);
143069 }
143070
143071 if( nChangeFrom==0 && HasRowid(pTab) ){
143072 sqlite3VdbeAddOp3(p: v, OP_Null, p1: 0, p2: regRowSet, p3: regOldRowid);
143073 iEph = pParse->nTab++;
143074 addrOpen = sqlite3VdbeAddOp3(p: v, OP_OpenEphemeral, p1: iEph, p2: 0, p3: regRowSet);
143075 }else{
143076 assert( pPk!=0 || HasRowid(pTab) );
143077 nPk = pPk ? pPk->nKeyCol : 0;
143078 iPk = pParse->nMem+1;
143079 pParse->nMem += nPk;
143080 pParse->nMem += nChangeFrom;
143081 regKey = ++pParse->nMem;
143082 if( pUpsert==0 ){
143083 int nEphCol = nPk + nChangeFrom + (isView ? pTab->nCol : 0);
143084 iEph = pParse->nTab++;
143085 if( pPk ) sqlite3VdbeAddOp3(p: v, OP_Null, p1: 0, p2: iPk, p3: iPk+nPk-1);
143086 addrOpen = sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: iEph, p2: nEphCol);
143087 if( pPk ){
143088 KeyInfo *pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pIdx: pPk);
143089 if( pKeyInfo ){
143090 pKeyInfo->nAllField = nEphCol;
143091 sqlite3VdbeAppendP4(p: v, pP4: pKeyInfo, P4_KEYINFO);
143092 }
143093 }
143094 if( nChangeFrom ){
143095 updateFromSelect(
143096 pParse, iEph, pPk, pChanges, pTabList, pWhere, pOrderBy, pLimit
143097 );
143098#ifndef SQLITE_OMIT_SUBQUERY
143099 if( isView ) iDataCur = iEph;
143100#endif
143101 }
143102 }
143103 }
143104
143105 if( nChangeFrom ){
143106 sqlite3MultiWrite(pParse);
143107 eOnePass = ONEPASS_OFF;
143108 nKey = nPk;
143109 regKey = iPk;
143110 }else{
143111 if( pUpsert ){
143112 /* If this is an UPSERT, then all cursors have already been opened by
143113 ** the outer INSERT and the data cursor should be pointing at the row
143114 ** that is to be updated. So bypass the code that searches for the
143115 ** row(s) to be updated.
143116 */
143117 pWInfo = 0;
143118 eOnePass = ONEPASS_SINGLE;
143119 sqlite3ExprIfFalse(pParse, pExpr: pWhere, dest: labelBreak, SQLITE_JUMPIFNULL);
143120 bFinishSeek = 0;
143121 }else{
143122 /* Begin the database scan.
143123 **
143124 ** Do not consider a single-pass strategy for a multi-row update if
143125 ** there are any triggers or foreign keys to process, or rows may
143126 ** be deleted as a result of REPLACE conflict handling. Any of these
143127 ** things might disturb a cursor being used to scan through the table
143128 ** or index, causing a single-pass approach to malfunction. */
143129 flags = WHERE_ONEPASS_DESIRED;
143130 if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){
143131 flags |= WHERE_ONEPASS_MULTIROW;
143132 }
143133 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, flags,iIdxCur);
143134 if( pWInfo==0 ) goto update_cleanup;
143135
143136 /* A one-pass strategy that might update more than one row may not
143137 ** be used if any column of the index used for the scan is being
143138 ** updated. Otherwise, if there is an index on "b", statements like
143139 ** the following could create an infinite loop:
143140 **
143141 ** UPDATE t1 SET b=b+1 WHERE b>?
143142 **
143143 ** Fall back to ONEPASS_OFF if where.c has selected a ONEPASS_MULTI
143144 ** strategy that uses an index for which one or more columns are being
143145 ** updated. */
143146 eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
143147 bFinishSeek = sqlite3WhereUsesDeferredSeek(pWInfo);
143148 if( eOnePass!=ONEPASS_SINGLE ){
143149 sqlite3MultiWrite(pParse);
143150 if( eOnePass==ONEPASS_MULTI ){
143151 int iCur = aiCurOnePass[1];
143152 if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){
143153 eOnePass = ONEPASS_OFF;
143154 }
143155 assert( iCur!=iDataCur || !HasRowid(pTab) );
143156 }
143157 }
143158 }
143159
143160 if( HasRowid(pTab) ){
143161 /* Read the rowid of the current row of the WHERE scan. In ONEPASS_OFF
143162 ** mode, write the rowid into the FIFO. In either of the one-pass modes,
143163 ** leave it in register regOldRowid. */
143164 sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: iDataCur, p2: regOldRowid);
143165 if( eOnePass==ONEPASS_OFF ){
143166 aRegIdx[nAllIdx] = ++pParse->nMem;
143167 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: iEph, p2: regRowSet, p3: regOldRowid);
143168 }else{
143169 if( ALWAYS(addrOpen) ) sqlite3VdbeChangeToNoop(p: v, addr: addrOpen);
143170 }
143171 }else{
143172 /* Read the PK of the current row into an array of registers. In
143173 ** ONEPASS_OFF mode, serialize the array into a record and store it in
143174 ** the ephemeral table. Or, in ONEPASS_SINGLE or MULTI mode, change
143175 ** the OP_OpenEphemeral instruction to a Noop (the ephemeral table
143176 ** is not required) and leave the PK fields in the array of registers. */
143177 for(i=0; i<nPk; i++){
143178 assert( pPk->aiColumn[i]>=0 );
143179 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur: iDataCur,
143180 iCol: pPk->aiColumn[i], regOut: iPk+i);
143181 }
143182 if( eOnePass ){
143183 if( addrOpen ) sqlite3VdbeChangeToNoop(p: v, addr: addrOpen);
143184 nKey = nPk;
143185 regKey = iPk;
143186 }else{
143187 sqlite3VdbeAddOp4(p: v, OP_MakeRecord, p1: iPk, p2: nPk, p3: regKey,
143188 zP4: sqlite3IndexAffinityStr(db, pIdx: pPk), p4type: nPk);
143189 sqlite3VdbeAddOp4Int(p: v, OP_IdxInsert, p1: iEph, p2: regKey, p3: iPk, p4: nPk);
143190 }
143191 }
143192 }
143193
143194 if( pUpsert==0 ){
143195 if( nChangeFrom==0 && eOnePass!=ONEPASS_MULTI ){
143196 sqlite3WhereEnd(pWInfo);
143197 }
143198
143199 if( !isView ){
143200 int addrOnce = 0;
143201
143202 /* Open every index that needs updating. */
143203 if( eOnePass!=ONEPASS_OFF ){
143204 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
143205 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
143206 }
143207
143208 if( eOnePass==ONEPASS_MULTI && (nIdx-(aiCurOnePass[1]>=0))>0 ){
143209 addrOnce = sqlite3VdbeAddOp0(p: v, OP_Once); VdbeCoverage(v);
143210 }
143211 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, p5: 0, iBase: iBaseCur,
143212 aToOpen, piDataCur: 0, piIdxCur: 0);
143213 if( addrOnce ){
143214 sqlite3VdbeJumpHereOrPopInst(p: v, addr: addrOnce);
143215 }
143216 }
143217
143218 /* Top of the update loop */
143219 if( eOnePass!=ONEPASS_OFF ){
143220 if( aiCurOnePass[0]!=iDataCur
143221 && aiCurOnePass[1]!=iDataCur
143222#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
143223 && !isView
143224#endif
143225 ){
143226 assert( pPk );
143227 sqlite3VdbeAddOp4Int(p: v, OP_NotFound, p1: iDataCur, p2: labelBreak, p3: regKey,p4: nKey);
143228 VdbeCoverage(v);
143229 }
143230 if( eOnePass!=ONEPASS_SINGLE ){
143231 labelContinue = sqlite3VdbeMakeLabel(pParse);
143232 }
143233 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: pPk ? regKey : regOldRowid, p2: labelBreak);
143234 VdbeCoverageIf(v, pPk==0);
143235 VdbeCoverageIf(v, pPk!=0);
143236 }else if( pPk || nChangeFrom ){
143237 labelContinue = sqlite3VdbeMakeLabel(pParse);
143238 sqlite3VdbeAddOp2(p: v, OP_Rewind, p1: iEph, p2: labelBreak); VdbeCoverage(v);
143239 addrTop = sqlite3VdbeCurrentAddr(p: v);
143240 if( nChangeFrom ){
143241 if( !isView ){
143242 if( pPk ){
143243 for(i=0; i<nPk; i++){
143244 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iEph, p2: i, p3: iPk+i);
143245 }
143246 sqlite3VdbeAddOp4Int(
143247 p: v, OP_NotFound, p1: iDataCur, p2: labelContinue, p3: iPk, p4: nPk
143248 ); VdbeCoverage(v);
143249 }else{
143250 sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: iEph, p2: regOldRowid);
143251 sqlite3VdbeAddOp3(
143252 p: v, OP_NotExists, p1: iDataCur, p2: labelContinue, p3: regOldRowid
143253 ); VdbeCoverage(v);
143254 }
143255 }
143256 }else{
143257 sqlite3VdbeAddOp2(p: v, OP_RowData, p1: iEph, p2: regKey);
143258 sqlite3VdbeAddOp4Int(p: v, OP_NotFound, p1: iDataCur, p2: labelContinue, p3: regKey,p4: 0);
143259 VdbeCoverage(v);
143260 }
143261 }else{
143262 sqlite3VdbeAddOp2(p: v, OP_Rewind, p1: iEph, p2: labelBreak); VdbeCoverage(v);
143263 labelContinue = sqlite3VdbeMakeLabel(pParse);
143264 addrTop = sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: iEph, p2: regOldRowid);
143265 VdbeCoverage(v);
143266 sqlite3VdbeAddOp3(p: v, OP_NotExists, p1: iDataCur, p2: labelContinue, p3: regOldRowid);
143267 VdbeCoverage(v);
143268 }
143269 }
143270
143271 /* If the rowid value will change, set register regNewRowid to
143272 ** contain the new value. If the rowid is not being modified,
143273 ** then regNewRowid is the same register as regOldRowid, which is
143274 ** already populated. */
143275 assert( chngKey || pTrigger || hasFK || regOldRowid==regNewRowid );
143276 if( chngRowid ){
143277 assert( iRowidExpr>=0 );
143278 if( nChangeFrom==0 ){
143279 sqlite3ExprCode(pParse, pExpr: pRowidExpr, target: regNewRowid);
143280 }else{
143281 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iEph, p2: iRowidExpr, p3: regNewRowid);
143282 }
143283 sqlite3VdbeAddOp1(p: v, OP_MustBeInt, p1: regNewRowid); VdbeCoverage(v);
143284 }
143285
143286 /* Compute the old pre-UPDATE content of the row being changed, if that
143287 ** information is needed */
143288 if( chngPk || hasFK || pTrigger ){
143289 u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0);
143290 oldmask |= sqlite3TriggerColmask(pParse,
143291 pTrigger, pChanges, isNew: 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, orconf: onError
143292 );
143293 for(i=0; i<pTab->nCol; i++){
143294 u32 colFlags = pTab->aCol[i].colFlags;
143295 k = sqlite3TableColumnToStorage(pTab, iCol: i) + regOld;
143296 if( oldmask==0xffffffff
143297 || (i<32 && (oldmask & MASKBIT32(i))!=0)
143298 || (colFlags & COLFLAG_PRIMKEY)!=0
143299 ){
143300 testcase( oldmask!=0xffffffff && i==31 );
143301 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur: iDataCur, iCol: i, regOut: k);
143302 }else{
143303 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: k);
143304 }
143305 }
143306 if( chngRowid==0 && pPk==0 ){
143307 sqlite3VdbeAddOp2(p: v, OP_Copy, p1: regOldRowid, p2: regNewRowid);
143308 }
143309 }
143310
143311 /* Populate the array of registers beginning at regNew with the new
143312 ** row data. This array is used to check constants, create the new
143313 ** table and index records, and as the values for any new.* references
143314 ** made by triggers.
143315 **
143316 ** If there are one or more BEFORE triggers, then do not populate the
143317 ** registers associated with columns that are (a) not modified by
143318 ** this UPDATE statement and (b) not accessed by new.* references. The
143319 ** values for registers not modified by the UPDATE must be reloaded from
143320 ** the database after the BEFORE triggers are fired anyway (as the trigger
143321 ** may have modified them). So not loading those that are not going to
143322 ** be used eliminates some redundant opcodes.
143323 */
143324 newmask = sqlite3TriggerColmask(
143325 pParse, pTrigger, pChanges, isNew: 1, TRIGGER_BEFORE, pTab, orconf: onError
143326 );
143327 for(i=0, k=regNew; i<pTab->nCol; i++, k++){
143328 if( i==pTab->iPKey ){
143329 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: k);
143330 }else if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)!=0 ){
143331 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) k--;
143332 }else{
143333 j = aXRef[i];
143334 if( j>=0 ){
143335 if( nChangeFrom ){
143336 int nOff = (isView ? pTab->nCol : nPk);
143337 assert( eOnePass==ONEPASS_OFF );
143338 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iEph, p2: nOff+j, p3: k);
143339 }else{
143340 sqlite3ExprCode(pParse, pExpr: pChanges->a[j].pExpr, target: k);
143341 }
143342 }else if( 0==(tmask&TRIGGER_BEFORE) || i>31 || (newmask & MASKBIT32(i)) ){
143343 /* This branch loads the value of a column that will not be changed
143344 ** into a register. This is done if there are no BEFORE triggers, or
143345 ** if there are one or more BEFORE triggers that use this value via
143346 ** a new.* reference in a trigger program.
143347 */
143348 testcase( i==31 );
143349 testcase( i==32 );
143350 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur: iDataCur, iCol: i, regOut: k);
143351 bFinishSeek = 0;
143352 }else{
143353 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: k);
143354 }
143355 }
143356 }
143357#ifndef SQLITE_OMIT_GENERATED_COLUMNS
143358 if( pTab->tabFlags & TF_HasGenerated ){
143359 testcase( pTab->tabFlags & TF_HasVirtual );
143360 testcase( pTab->tabFlags & TF_HasStored );
143361 sqlite3ComputeGeneratedColumns(pParse, iRegStore: regNew, pTab);
143362 }
143363#endif
143364
143365 /* Fire any BEFORE UPDATE triggers. This happens before constraints are
143366 ** verified. One could argue that this is wrong.
143367 */
143368 if( tmask&TRIGGER_BEFORE ){
143369 sqlite3TableAffinity(v, pTab, iReg: regNew);
143370 sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,
143371 TRIGGER_BEFORE, pTab, reg: regOldRowid, orconf: onError, ignoreJump: labelContinue);
143372
143373 if( !isView ){
143374 /* The row-trigger may have deleted the row being updated. In this
143375 ** case, jump to the next row. No updates or AFTER triggers are
143376 ** required. This behavior - what happens when the row being updated
143377 ** is deleted or renamed by a BEFORE trigger - is left undefined in the
143378 ** documentation.
143379 */
143380 if( pPk ){
143381 sqlite3VdbeAddOp4Int(p: v, OP_NotFound,p1: iDataCur,p2: labelContinue,p3: regKey,p4: nKey);
143382 VdbeCoverage(v);
143383 }else{
143384 sqlite3VdbeAddOp3(p: v, OP_NotExists, p1: iDataCur, p2: labelContinue,p3: regOldRowid);
143385 VdbeCoverage(v);
143386 }
143387
143388 /* After-BEFORE-trigger-reload-loop:
143389 ** If it did not delete it, the BEFORE trigger may still have modified
143390 ** some of the columns of the row being updated. Load the values for
143391 ** all columns not modified by the update statement into their registers
143392 ** in case this has happened. Only unmodified columns are reloaded.
143393 ** The values computed for modified columns use the values before the
143394 ** BEFORE trigger runs. See test case trigger1-18.0 (added 2018-04-26)
143395 ** for an example.
143396 */
143397 for(i=0, k=regNew; i<pTab->nCol; i++, k++){
143398 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
143399 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) k--;
143400 }else if( aXRef[i]<0 && i!=pTab->iPKey ){
143401 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur: iDataCur, iCol: i, regOut: k);
143402 }
143403 }
143404#ifndef SQLITE_OMIT_GENERATED_COLUMNS
143405 if( pTab->tabFlags & TF_HasGenerated ){
143406 testcase( pTab->tabFlags & TF_HasVirtual );
143407 testcase( pTab->tabFlags & TF_HasStored );
143408 sqlite3ComputeGeneratedColumns(pParse, iRegStore: regNew, pTab);
143409 }
143410#endif
143411 }
143412 }
143413
143414 if( !isView ){
143415 /* Do constraint checks. */
143416 assert( regOldRowid>0 );
143417 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
143418 regNewData: regNewRowid, regOldData: regOldRowid, pkChng: chngKey, overrideError: onError, ignoreDest: labelContinue, pbMayReplace: &bReplace,
143419 aiChng: aXRef, pUpsert: 0);
143420
143421 /* If REPLACE conflict handling may have been used, or if the PK of the
143422 ** row is changing, then the GenerateConstraintChecks() above may have
143423 ** moved cursor iDataCur. Reseek it. */
143424 if( bReplace || chngKey ){
143425 if( pPk ){
143426 sqlite3VdbeAddOp4Int(p: v, OP_NotFound,p1: iDataCur,p2: labelContinue,p3: regKey,p4: nKey);
143427 }else{
143428 sqlite3VdbeAddOp3(p: v, OP_NotExists, p1: iDataCur, p2: labelContinue,p3: regOldRowid);
143429 }
143430 VdbeCoverageNeverTaken(v);
143431 }
143432
143433 /* Do FK constraint checks. */
143434 if( hasFK ){
143435 sqlite3FkCheck(pParse, pTab, regOld: regOldRowid, regNew: 0, aChange: aXRef, bChngRowid: chngKey);
143436 }
143437
143438 /* Delete the index entries associated with the current record. */
143439 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx, iIdxNoSeek: -1);
143440
143441 /* We must run the OP_FinishSeek opcode to resolve a prior
143442 ** OP_DeferredSeek if there is any possibility that there have been
143443 ** no OP_Column opcodes since the OP_DeferredSeek was issued. But
143444 ** we want to avoid the OP_FinishSeek if possible, as running it
143445 ** costs CPU cycles. */
143446 if( bFinishSeek ){
143447 sqlite3VdbeAddOp1(p: v, OP_FinishSeek, p1: iDataCur);
143448 }
143449
143450 /* If changing the rowid value, or if there are foreign key constraints
143451 ** to process, delete the old record. Otherwise, add a noop OP_Delete
143452 ** to invoke the pre-update hook.
143453 **
143454 ** That (regNew==regnewRowid+1) is true is also important for the
143455 ** pre-update hook. If the caller invokes preupdate_new(), the returned
143456 ** value is copied from memory cell (regNewRowid+1+iCol), where iCol
143457 ** is the column index supplied by the user.
143458 */
143459 assert( regNew==regNewRowid+1 );
143460#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
143461 sqlite3VdbeAddOp3(v, OP_Delete, iDataCur,
143462 OPFLAG_ISUPDATE | ((hasFK>1 || chngKey) ? 0 : OPFLAG_ISNOOP),
143463 regNewRowid
143464 );
143465 if( eOnePass==ONEPASS_MULTI ){
143466 assert( hasFK==0 && chngKey==0 );
143467 sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION);
143468 }
143469 if( !pParse->nested ){
143470 sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
143471 }
143472#else
143473 if( hasFK>1 || chngKey ){
143474 sqlite3VdbeAddOp2(p: v, OP_Delete, p1: iDataCur, p2: 0);
143475 }
143476#endif
143477
143478 if( hasFK ){
143479 sqlite3FkCheck(pParse, pTab, regOld: 0, regNew: regNewRowid, aChange: aXRef, bChngRowid: chngKey);
143480 }
143481
143482 /* Insert the new index entries and the new record. */
143483 sqlite3CompleteInsertion(
143484 pParse, pTab, iDataCur, iIdxCur, regNewData: regNewRowid, aRegIdx,
143485 OPFLAG_ISUPDATE | (eOnePass==ONEPASS_MULTI ? OPFLAG_SAVEPOSITION : 0),
143486 appendBias: 0, useSeekResult: 0
143487 );
143488
143489 /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
143490 ** handle rows (possibly in other tables) that refer via a foreign key
143491 ** to the row just updated. */
143492 if( hasFK ){
143493 sqlite3FkActions(pParse, pTab, pChanges, regOld: regOldRowid, aChange: aXRef, bChngRowid: chngKey);
143494 }
143495 }
143496
143497 /* Increment the row counter
143498 */
143499 if( regRowCount ){
143500 sqlite3VdbeAddOp2(p: v, OP_AddImm, p1: regRowCount, p2: 1);
143501 }
143502
143503 sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,
143504 TRIGGER_AFTER, pTab, reg: regOldRowid, orconf: onError, ignoreJump: labelContinue);
143505
143506 /* Repeat the above with the next record to be updated, until
143507 ** all record selected by the WHERE clause have been updated.
143508 */
143509 if( eOnePass==ONEPASS_SINGLE ){
143510 /* Nothing to do at end-of-loop for a single-pass */
143511 }else if( eOnePass==ONEPASS_MULTI ){
143512 sqlite3VdbeResolveLabel(v, x: labelContinue);
143513 sqlite3WhereEnd(pWInfo);
143514 }else{
143515 sqlite3VdbeResolveLabel(v, x: labelContinue);
143516 sqlite3VdbeAddOp2(p: v, OP_Next, p1: iEph, p2: addrTop); VdbeCoverage(v);
143517 }
143518 sqlite3VdbeResolveLabel(v, x: labelBreak);
143519
143520 /* Update the sqlite_sequence table by storing the content of the
143521 ** maximum rowid counter values recorded while inserting into
143522 ** autoincrement tables.
143523 */
143524 if( pParse->nested==0 && pParse->pTriggerTab==0 && pUpsert==0 ){
143525 sqlite3AutoincrementEnd(pParse);
143526 }
143527
143528 /*
143529 ** Return the number of rows that were changed, if we are tracking
143530 ** that information.
143531 */
143532 if( regRowCount ){
143533 sqlite3VdbeAddOp2(p: v, OP_ChngCntRow, p1: regRowCount, p2: 1);
143534 sqlite3VdbeSetNumCols(p: v, nResColumn: 1);
143535 sqlite3VdbeSetColName(p: v, idx: 0, COLNAME_NAME, zName: "rows updated", SQLITE_STATIC);
143536 }
143537
143538update_cleanup:
143539 sqlite3AuthContextPop(pContext: &sContext);
143540 sqlite3DbFree(db, p: aXRef); /* Also frees aRegIdx[] and aToOpen[] */
143541 sqlite3SrcListDelete(db, pList: pTabList);
143542 sqlite3ExprListDelete(db, pList: pChanges);
143543 sqlite3ExprDelete(db, p: pWhere);
143544#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
143545 sqlite3ExprListDelete(db, pOrderBy);
143546 sqlite3ExprDelete(db, pLimit);
143547#endif
143548 return;
143549}
143550/* Make sure "isView" and other macros defined above are undefined. Otherwise
143551** they may interfere with compilation of other functions in this file
143552** (or in another file, if this file becomes part of the amalgamation). */
143553#ifdef isView
143554 #undef isView
143555#endif
143556#ifdef pTrigger
143557 #undef pTrigger
143558#endif
143559
143560#ifndef SQLITE_OMIT_VIRTUALTABLE
143561/*
143562** Generate code for an UPDATE of a virtual table.
143563**
143564** There are two possible strategies - the default and the special
143565** "onepass" strategy. Onepass is only used if the virtual table
143566** implementation indicates that pWhere may match at most one row.
143567**
143568** The default strategy is to create an ephemeral table that contains
143569** for each row to be changed:
143570**
143571** (A) The original rowid of that row.
143572** (B) The revised rowid for the row.
143573** (C) The content of every column in the row.
143574**
143575** Then loop through the contents of this ephemeral table executing a
143576** VUpdate for each row. When finished, drop the ephemeral table.
143577**
143578** The "onepass" strategy does not use an ephemeral table. Instead, it
143579** stores the same values (A, B and C above) in a register array and
143580** makes a single invocation of VUpdate.
143581*/
143582static void updateVirtualTable(
143583 Parse *pParse, /* The parsing context */
143584 SrcList *pSrc, /* The virtual table to be modified */
143585 Table *pTab, /* The virtual table */
143586 ExprList *pChanges, /* The columns to change in the UPDATE statement */
143587 Expr *pRowid, /* Expression used to recompute the rowid */
143588 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
143589 Expr *pWhere, /* WHERE clause of the UPDATE statement */
143590 int onError /* ON CONFLICT strategy */
143591){
143592 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */
143593 int ephemTab; /* Table holding the result of the SELECT */
143594 int i; /* Loop counter */
143595 sqlite3 *db = pParse->db; /* Database connection */
143596 const char *pVTab = (const char*)sqlite3GetVTable(db, pTab);
143597 WhereInfo *pWInfo = 0;
143598 int nArg = 2 + pTab->nCol; /* Number of arguments to VUpdate */
143599 int regArg; /* First register in VUpdate arg array */
143600 int regRec; /* Register in which to assemble record */
143601 int regRowid; /* Register for ephem table rowid */
143602 int iCsr = pSrc->a[0].iCursor; /* Cursor used for virtual table scan */
143603 int aDummy[2]; /* Unused arg for sqlite3WhereOkOnePass() */
143604 int eOnePass; /* True to use onepass strategy */
143605 int addr; /* Address of OP_OpenEphemeral */
143606
143607 /* Allocate nArg registers in which to gather the arguments for VUpdate. Then
143608 ** create and open the ephemeral table in which the records created from
143609 ** these arguments will be temporarily stored. */
143610 assert( v );
143611 ephemTab = pParse->nTab++;
143612 addr= sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: ephemTab, p2: nArg);
143613 regArg = pParse->nMem + 1;
143614 pParse->nMem += nArg;
143615 if( pSrc->nSrc>1 ){
143616 Index *pPk = 0;
143617 Expr *pRow;
143618 ExprList *pList;
143619 if( HasRowid(pTab) ){
143620 if( pRowid ){
143621 pRow = sqlite3ExprDup(db, p: pRowid, flags: 0);
143622 }else{
143623 pRow = sqlite3PExpr(pParse, TK_ROW, pLeft: 0, pRight: 0);
143624 }
143625 }else{
143626 i16 iPk; /* PRIMARY KEY column */
143627 pPk = sqlite3PrimaryKeyIndex(pTab);
143628 assert( pPk!=0 );
143629 assert( pPk->nKeyCol==1 );
143630 iPk = pPk->aiColumn[0];
143631 if( aXRef[iPk]>=0 ){
143632 pRow = sqlite3ExprDup(db, p: pChanges->a[aXRef[iPk]].pExpr, flags: 0);
143633 }else{
143634 pRow = exprRowColumn(pParse, iCol: iPk);
143635 }
143636 }
143637 pList = sqlite3ExprListAppend(pParse, pList: 0, pExpr: pRow);
143638
143639 for(i=0; i<pTab->nCol; i++){
143640 if( aXRef[i]>=0 ){
143641 pList = sqlite3ExprListAppend(pParse, pList,
143642 pExpr: sqlite3ExprDup(db, p: pChanges->a[aXRef[i]].pExpr, flags: 0)
143643 );
143644 }else{
143645 pList = sqlite3ExprListAppend(pParse, pList, pExpr: exprRowColumn(pParse, iCol: i));
143646 }
143647 }
143648
143649 updateFromSelect(pParse, iEph: ephemTab, pPk, pChanges: pList, pTabList: pSrc, pWhere, pOrderBy: 0, pLimit: 0);
143650 sqlite3ExprListDelete(db, pList);
143651 eOnePass = ONEPASS_OFF;
143652 }else{
143653 regRec = ++pParse->nMem;
143654 regRowid = ++pParse->nMem;
143655
143656 /* Start scanning the virtual table */
143657 pWInfo = sqlite3WhereBegin(pParse, pSrc,pWhere,0,0,WHERE_ONEPASS_DESIRED,0);
143658 if( pWInfo==0 ) return;
143659
143660 /* Populate the argument registers. */
143661 for(i=0; i<pTab->nCol; i++){
143662 assert( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 );
143663 if( aXRef[i]>=0 ){
143664 sqlite3ExprCode(pParse, pExpr: pChanges->a[aXRef[i]].pExpr, target: regArg+2+i);
143665 }else{
143666 sqlite3VdbeAddOp3(p: v, OP_VColumn, p1: iCsr, p2: i, p3: regArg+2+i);
143667 sqlite3VdbeChangeP5(p: v, OPFLAG_NOCHNG);/* For sqlite3_vtab_nochange() */
143668 }
143669 }
143670 if( HasRowid(pTab) ){
143671 sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: iCsr, p2: regArg);
143672 if( pRowid ){
143673 sqlite3ExprCode(pParse, pExpr: pRowid, target: regArg+1);
143674 }else{
143675 sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: iCsr, p2: regArg+1);
143676 }
143677 }else{
143678 Index *pPk; /* PRIMARY KEY index */
143679 i16 iPk; /* PRIMARY KEY column */
143680 pPk = sqlite3PrimaryKeyIndex(pTab);
143681 assert( pPk!=0 );
143682 assert( pPk->nKeyCol==1 );
143683 iPk = pPk->aiColumn[0];
143684 sqlite3VdbeAddOp3(p: v, OP_VColumn, p1: iCsr, p2: iPk, p3: regArg);
143685 sqlite3VdbeAddOp2(p: v, OP_SCopy, p1: regArg+2+iPk, p2: regArg+1);
143686 }
143687
143688 eOnePass = sqlite3WhereOkOnePass(pWInfo, aDummy);
143689
143690 /* There is no ONEPASS_MULTI on virtual tables */
143691 assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE );
143692
143693 if( eOnePass ){
143694 /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded
143695 ** above. */
143696 sqlite3VdbeChangeToNoop(p: v, addr);
143697 sqlite3VdbeAddOp1(p: v, OP_Close, p1: iCsr);
143698 }else{
143699 /* Create a record from the argument register contents and insert it into
143700 ** the ephemeral table. */
143701 sqlite3MultiWrite(pParse);
143702 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: regArg, p2: nArg, p3: regRec);
143703#if defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_NULL_TRIM)
143704 /* Signal an assert() within OP_MakeRecord that it is allowed to
143705 ** accept no-change records with serial_type 10 */
143706 sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG_MAGIC);
143707#endif
143708 sqlite3VdbeAddOp2(p: v, OP_NewRowid, p1: ephemTab, p2: regRowid);
143709 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: ephemTab, p2: regRec, p3: regRowid);
143710 }
143711 }
143712
143713
143714 if( eOnePass==ONEPASS_OFF ){
143715 /* End the virtual table scan */
143716 if( pSrc->nSrc==1 ){
143717 sqlite3WhereEnd(pWInfo);
143718 }
143719
143720 /* Begin scannning through the ephemeral table. */
143721 addr = sqlite3VdbeAddOp1(p: v, OP_Rewind, p1: ephemTab); VdbeCoverage(v);
143722
143723 /* Extract arguments from the current row of the ephemeral table and
143724 ** invoke the VUpdate method. */
143725 for(i=0; i<nArg; i++){
143726 sqlite3VdbeAddOp3(p: v, OP_Column, p1: ephemTab, p2: i, p3: regArg+i);
143727 }
143728 }
143729 sqlite3VtabMakeWritable(pParse, pTab);
143730 sqlite3VdbeAddOp4(p: v, OP_VUpdate, p1: 0, p2: nArg, p3: regArg, zP4: pVTab, P4_VTAB);
143731 sqlite3VdbeChangeP5(p: v, p5: onError==OE_Default ? OE_Abort : onError);
143732 sqlite3MayAbort(pParse);
143733
143734 /* End of the ephemeral table scan. Or, if using the onepass strategy,
143735 ** jump to here if the scan visited zero rows. */
143736 if( eOnePass==ONEPASS_OFF ){
143737 sqlite3VdbeAddOp2(p: v, OP_Next, p1: ephemTab, p2: addr+1); VdbeCoverage(v);
143738 sqlite3VdbeJumpHere(p: v, addr);
143739 sqlite3VdbeAddOp2(p: v, OP_Close, p1: ephemTab, p2: 0);
143740 }else{
143741 sqlite3WhereEnd(pWInfo);
143742 }
143743}
143744#endif /* SQLITE_OMIT_VIRTUALTABLE */
143745
143746/************** End of update.c **********************************************/
143747/************** Begin file upsert.c ******************************************/
143748/*
143749** 2018-04-12
143750**
143751** The author disclaims copyright to this source code. In place of
143752** a legal notice, here is a blessing:
143753**
143754** May you do good and not evil.
143755** May you find forgiveness for yourself and forgive others.
143756** May you share freely, never taking more than you give.
143757**
143758*************************************************************************
143759** This file contains code to implement various aspects of UPSERT
143760** processing and handling of the Upsert object.
143761*/
143762/* #include "sqliteInt.h" */
143763
143764#ifndef SQLITE_OMIT_UPSERT
143765/*
143766** Free a list of Upsert objects
143767*/
143768static void SQLITE_NOINLINE upsertDelete(sqlite3 *db, Upsert *p){
143769 do{
143770 Upsert *pNext = p->pNextUpsert;
143771 sqlite3ExprListDelete(db, pList: p->pUpsertTarget);
143772 sqlite3ExprDelete(db, p: p->pUpsertTargetWhere);
143773 sqlite3ExprListDelete(db, pList: p->pUpsertSet);
143774 sqlite3ExprDelete(db, p: p->pUpsertWhere);
143775 sqlite3DbFree(db, p: p->pToFree);
143776 sqlite3DbFree(db, p);
143777 p = pNext;
143778 }while( p );
143779}
143780SQLITE_PRIVATE void sqlite3UpsertDelete(sqlite3 *db, Upsert *p){
143781 if( p ) upsertDelete(db, p);
143782}
143783
143784
143785/*
143786** Duplicate an Upsert object.
143787*/
143788SQLITE_PRIVATE Upsert *sqlite3UpsertDup(sqlite3 *db, Upsert *p){
143789 if( p==0 ) return 0;
143790 return sqlite3UpsertNew(db,
143791 sqlite3ExprListDup(db, p: p->pUpsertTarget, flags: 0),
143792 sqlite3ExprDup(db, p: p->pUpsertTargetWhere, flags: 0),
143793 sqlite3ExprListDup(db, p: p->pUpsertSet, flags: 0),
143794 sqlite3ExprDup(db, p: p->pUpsertWhere, flags: 0),
143795 sqlite3UpsertDup(db, p: p->pNextUpsert)
143796 );
143797}
143798
143799/*
143800** Create a new Upsert object.
143801*/
143802SQLITE_PRIVATE Upsert *sqlite3UpsertNew(
143803 sqlite3 *db, /* Determines which memory allocator to use */
143804 ExprList *pTarget, /* Target argument to ON CONFLICT, or NULL */
143805 Expr *pTargetWhere, /* Optional WHERE clause on the target */
143806 ExprList *pSet, /* UPDATE columns, or NULL for a DO NOTHING */
143807 Expr *pWhere, /* WHERE clause for the ON CONFLICT UPDATE */
143808 Upsert *pNext /* Next ON CONFLICT clause in the list */
143809){
143810 Upsert *pNew;
143811 pNew = sqlite3DbMallocZero(db, n: sizeof(Upsert));
143812 if( pNew==0 ){
143813 sqlite3ExprListDelete(db, pList: pTarget);
143814 sqlite3ExprDelete(db, p: pTargetWhere);
143815 sqlite3ExprListDelete(db, pList: pSet);
143816 sqlite3ExprDelete(db, p: pWhere);
143817 sqlite3UpsertDelete(db, p: pNext);
143818 return 0;
143819 }else{
143820 pNew->pUpsertTarget = pTarget;
143821 pNew->pUpsertTargetWhere = pTargetWhere;
143822 pNew->pUpsertSet = pSet;
143823 pNew->pUpsertWhere = pWhere;
143824 pNew->isDoUpdate = pSet!=0;
143825 pNew->pNextUpsert = pNext;
143826 }
143827 return pNew;
143828}
143829
143830/*
143831** Analyze the ON CONFLICT clause described by pUpsert. Resolve all
143832** symbols in the conflict-target.
143833**
143834** Return SQLITE_OK if everything works, or an error code is something
143835** is wrong.
143836*/
143837SQLITE_PRIVATE int sqlite3UpsertAnalyzeTarget(
143838 Parse *pParse, /* The parsing context */
143839 SrcList *pTabList, /* Table into which we are inserting */
143840 Upsert *pUpsert /* The ON CONFLICT clauses */
143841){
143842 Table *pTab; /* That table into which we are inserting */
143843 int rc; /* Result code */
143844 int iCursor; /* Cursor used by pTab */
143845 Index *pIdx; /* One of the indexes of pTab */
143846 ExprList *pTarget; /* The conflict-target clause */
143847 Expr *pTerm; /* One term of the conflict-target clause */
143848 NameContext sNC; /* Context for resolving symbolic names */
143849 Expr sCol[2]; /* Index column converted into an Expr */
143850 int nClause = 0; /* Counter of ON CONFLICT clauses */
143851
143852 assert( pTabList->nSrc==1 );
143853 assert( pTabList->a[0].pTab!=0 );
143854 assert( pUpsert!=0 );
143855 assert( pUpsert->pUpsertTarget!=0 );
143856
143857 /* Resolve all symbolic names in the conflict-target clause, which
143858 ** includes both the list of columns and the optional partial-index
143859 ** WHERE clause.
143860 */
143861 memset(s: &sNC, c: 0, n: sizeof(sNC));
143862 sNC.pParse = pParse;
143863 sNC.pSrcList = pTabList;
143864 for(; pUpsert && pUpsert->pUpsertTarget;
143865 pUpsert=pUpsert->pNextUpsert, nClause++){
143866 rc = sqlite3ResolveExprListNames(pNC: &sNC, pList: pUpsert->pUpsertTarget);
143867 if( rc ) return rc;
143868 rc = sqlite3ResolveExprNames(pNC: &sNC, pExpr: pUpsert->pUpsertTargetWhere);
143869 if( rc ) return rc;
143870
143871 /* Check to see if the conflict target matches the rowid. */
143872 pTab = pTabList->a[0].pTab;
143873 pTarget = pUpsert->pUpsertTarget;
143874 iCursor = pTabList->a[0].iCursor;
143875 if( HasRowid(pTab)
143876 && pTarget->nExpr==1
143877 && (pTerm = pTarget->a[0].pExpr)->op==TK_COLUMN
143878 && pTerm->iColumn==XN_ROWID
143879 ){
143880 /* The conflict-target is the rowid of the primary table */
143881 assert( pUpsert->pUpsertIdx==0 );
143882 continue;
143883 }
143884
143885 /* Initialize sCol[0..1] to be an expression parse tree for a
143886 ** single column of an index. The sCol[0] node will be the TK_COLLATE
143887 ** operator and sCol[1] will be the TK_COLUMN operator. Code below
143888 ** will populate the specific collation and column number values
143889 ** prior to comparing against the conflict-target expression.
143890 */
143891 memset(s: sCol, c: 0, n: sizeof(sCol));
143892 sCol[0].op = TK_COLLATE;
143893 sCol[0].pLeft = &sCol[1];
143894 sCol[1].op = TK_COLUMN;
143895 sCol[1].iTable = pTabList->a[0].iCursor;
143896
143897 /* Check for matches against other indexes */
143898 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
143899 int ii, jj, nn;
143900 if( !IsUniqueIndex(pIdx) ) continue;
143901 if( pTarget->nExpr!=pIdx->nKeyCol ) continue;
143902 if( pIdx->pPartIdxWhere ){
143903 if( pUpsert->pUpsertTargetWhere==0 ) continue;
143904 if( sqlite3ExprCompare(pParse, pA: pUpsert->pUpsertTargetWhere,
143905 pB: pIdx->pPartIdxWhere, iTab: iCursor)!=0 ){
143906 continue;
143907 }
143908 }
143909 nn = pIdx->nKeyCol;
143910 for(ii=0; ii<nn; ii++){
143911 Expr *pExpr;
143912 sCol[0].u.zToken = (char*)pIdx->azColl[ii];
143913 if( pIdx->aiColumn[ii]==XN_EXPR ){
143914 assert( pIdx->aColExpr!=0 );
143915 assert( pIdx->aColExpr->nExpr>ii );
143916 pExpr = pIdx->aColExpr->a[ii].pExpr;
143917 if( pExpr->op!=TK_COLLATE ){
143918 sCol[0].pLeft = pExpr;
143919 pExpr = &sCol[0];
143920 }
143921 }else{
143922 sCol[0].pLeft = &sCol[1];
143923 sCol[1].iColumn = pIdx->aiColumn[ii];
143924 pExpr = &sCol[0];
143925 }
143926 for(jj=0; jj<nn; jj++){
143927 if( sqlite3ExprCompare(pParse,pA: pTarget->a[jj].pExpr,pB: pExpr,iTab: iCursor)<2 ){
143928 break; /* Column ii of the index matches column jj of target */
143929 }
143930 }
143931 if( jj>=nn ){
143932 /* The target contains no match for column jj of the index */
143933 break;
143934 }
143935 }
143936 if( ii<nn ){
143937 /* Column ii of the index did not match any term of the conflict target.
143938 ** Continue the search with the next index. */
143939 continue;
143940 }
143941 pUpsert->pUpsertIdx = pIdx;
143942 break;
143943 }
143944 if( pUpsert->pUpsertIdx==0 ){
143945 char zWhich[16];
143946 if( nClause==0 && pUpsert->pNextUpsert==0 ){
143947 zWhich[0] = 0;
143948 }else{
143949 sqlite3_snprintf(n: sizeof(zWhich),zBuf: zWhich,zFormat: "%r ", nClause+1);
143950 }
143951 sqlite3ErrorMsg(pParse, zFormat: "%sON CONFLICT clause does not match any "
143952 "PRIMARY KEY or UNIQUE constraint", zWhich);
143953 return SQLITE_ERROR;
143954 }
143955 }
143956 return SQLITE_OK;
143957}
143958
143959/*
143960** Return true if pUpsert is the last ON CONFLICT clause with a
143961** conflict target, or if pUpsert is followed by another ON CONFLICT
143962** clause that targets the INTEGER PRIMARY KEY.
143963*/
143964SQLITE_PRIVATE int sqlite3UpsertNextIsIPK(Upsert *pUpsert){
143965 Upsert *pNext;
143966 if( NEVER(pUpsert==0) ) return 0;
143967 pNext = pUpsert->pNextUpsert;
143968 if( pNext==0 ) return 1;
143969 if( pNext->pUpsertTarget==0 ) return 1;
143970 if( pNext->pUpsertIdx==0 ) return 1;
143971 return 0;
143972}
143973
143974/*
143975** Given the list of ON CONFLICT clauses described by pUpsert, and
143976** a particular index pIdx, return a pointer to the particular ON CONFLICT
143977** clause that applies to the index. Or, if the index is not subject to
143978** any ON CONFLICT clause, return NULL.
143979*/
143980SQLITE_PRIVATE Upsert *sqlite3UpsertOfIndex(Upsert *pUpsert, Index *pIdx){
143981 while(
143982 pUpsert
143983 && pUpsert->pUpsertTarget!=0
143984 && pUpsert->pUpsertIdx!=pIdx
143985 ){
143986 pUpsert = pUpsert->pNextUpsert;
143987 }
143988 return pUpsert;
143989}
143990
143991/*
143992** Generate bytecode that does an UPDATE as part of an upsert.
143993**
143994** If pIdx is NULL, then the UNIQUE constraint that failed was the IPK.
143995** In this case parameter iCur is a cursor open on the table b-tree that
143996** currently points to the conflicting table row. Otherwise, if pIdx
143997** is not NULL, then pIdx is the constraint that failed and iCur is a
143998** cursor points to the conflicting row.
143999*/
144000SQLITE_PRIVATE void sqlite3UpsertDoUpdate(
144001 Parse *pParse, /* The parsing and code-generating context */
144002 Upsert *pUpsert, /* The ON CONFLICT clause for the upsert */
144003 Table *pTab, /* The table being updated */
144004 Index *pIdx, /* The UNIQUE constraint that failed */
144005 int iCur /* Cursor for pIdx (or pTab if pIdx==NULL) */
144006){
144007 Vdbe *v = pParse->pVdbe;
144008 sqlite3 *db = pParse->db;
144009 SrcList *pSrc; /* FROM clause for the UPDATE */
144010 int iDataCur;
144011 int i;
144012 Upsert *pTop = pUpsert;
144013
144014 assert( v!=0 );
144015 assert( pUpsert!=0 );
144016 iDataCur = pUpsert->iDataCur;
144017 pUpsert = sqlite3UpsertOfIndex(pUpsert: pTop, pIdx);
144018 VdbeNoopComment((v, "Begin DO UPDATE of UPSERT"));
144019 if( pIdx && iCur!=iDataCur ){
144020 if( HasRowid(pTab) ){
144021 int regRowid = sqlite3GetTempReg(pParse);
144022 sqlite3VdbeAddOp2(p: v, OP_IdxRowid, p1: iCur, p2: regRowid);
144023 sqlite3VdbeAddOp3(p: v, OP_SeekRowid, p1: iDataCur, p2: 0, p3: regRowid);
144024 VdbeCoverage(v);
144025 sqlite3ReleaseTempReg(pParse, iReg: regRowid);
144026 }else{
144027 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
144028 int nPk = pPk->nKeyCol;
144029 int iPk = pParse->nMem+1;
144030 pParse->nMem += nPk;
144031 for(i=0; i<nPk; i++){
144032 int k;
144033 assert( pPk->aiColumn[i]>=0 );
144034 k = sqlite3TableColumnToIndex(pIdx, iCol: pPk->aiColumn[i]);
144035 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iCur, p2: k, p3: iPk+i);
144036 VdbeComment((v, "%s.%s", pIdx->zName,
144037 pTab->aCol[pPk->aiColumn[i]].zCnName));
144038 }
144039 sqlite3VdbeVerifyAbortable(v, OE_Abort);
144040 i = sqlite3VdbeAddOp4Int(p: v, OP_Found, p1: iDataCur, p2: 0, p3: iPk, p4: nPk);
144041 VdbeCoverage(v);
144042 sqlite3VdbeAddOp4(p: v, OP_Halt, SQLITE_CORRUPT, OE_Abort, p3: 0,
144043 zP4: "corrupt database", P4_STATIC);
144044 sqlite3MayAbort(pParse);
144045 sqlite3VdbeJumpHere(p: v, addr: i);
144046 }
144047 }
144048 /* pUpsert does not own pTop->pUpsertSrc - the outer INSERT statement does.
144049 ** So we have to make a copy before passing it down into sqlite3Update() */
144050 pSrc = sqlite3SrcListDup(db, p: pTop->pUpsertSrc, flags: 0);
144051 /* excluded.* columns of type REAL need to be converted to a hard real */
144052 for(i=0; i<pTab->nCol; i++){
144053 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
144054 sqlite3VdbeAddOp1(p: v, OP_RealAffinity, p1: pTop->regData+i);
144055 }
144056 }
144057 sqlite3Update(pParse, pTabList: pSrc, pChanges: sqlite3ExprListDup(db,p: pUpsert->pUpsertSet,flags: 0),
144058 pWhere: sqlite3ExprDup(db,p: pUpsert->pUpsertWhere,flags: 0), OE_Abort, pOrderBy: 0, pLimit: 0, pUpsert);
144059 VdbeNoopComment((v, "End DO UPDATE of UPSERT"));
144060}
144061
144062#endif /* SQLITE_OMIT_UPSERT */
144063
144064/************** End of upsert.c **********************************************/
144065/************** Begin file vacuum.c ******************************************/
144066/*
144067** 2003 April 6
144068**
144069** The author disclaims copyright to this source code. In place of
144070** a legal notice, here is a blessing:
144071**
144072** May you do good and not evil.
144073** May you find forgiveness for yourself and forgive others.
144074** May you share freely, never taking more than you give.
144075**
144076*************************************************************************
144077** This file contains code used to implement the VACUUM command.
144078**
144079** Most of the code in this file may be omitted by defining the
144080** SQLITE_OMIT_VACUUM macro.
144081*/
144082/* #include "sqliteInt.h" */
144083/* #include "vdbeInt.h" */
144084
144085#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
144086
144087/*
144088** Execute zSql on database db.
144089**
144090** If zSql returns rows, then each row will have exactly one
144091** column. (This will only happen if zSql begins with "SELECT".)
144092** Take each row of result and call execSql() again recursively.
144093**
144094** The execSqlF() routine does the same thing, except it accepts
144095** a format string as its third argument
144096*/
144097static int execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
144098 sqlite3_stmt *pStmt;
144099 int rc;
144100
144101 /* printf("SQL: [%s]\n", zSql); fflush(stdout); */
144102 rc = sqlite3_prepare_v2(db, zSql, nBytes: -1, ppStmt: &pStmt, pzTail: 0);
144103 if( rc!=SQLITE_OK ) return rc;
144104 while( SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
144105 const char *zSubSql = (const char*)sqlite3_column_text(pStmt,i: 0);
144106 assert( sqlite3_strnicmp(zSql,"SELECT",6)==0 );
144107 /* The secondary SQL must be one of CREATE TABLE, CREATE INDEX,
144108 ** or INSERT. Historically there have been attacks that first
144109 ** corrupt the sqlite_schema.sql field with other kinds of statements
144110 ** then run VACUUM to get those statements to execute at inappropriate
144111 ** times. */
144112 if( zSubSql
144113 && (strncmp(s1: zSubSql,s2: "CRE",n: 3)==0 || strncmp(s1: zSubSql,s2: "INS",n: 3)==0)
144114 ){
144115 rc = execSql(db, pzErrMsg, zSql: zSubSql);
144116 if( rc!=SQLITE_OK ) break;
144117 }
144118 }
144119 assert( rc!=SQLITE_ROW );
144120 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
144121 if( rc ){
144122 sqlite3SetString(pz: pzErrMsg, db, zNew: sqlite3_errmsg(db));
144123 }
144124 (void)sqlite3_finalize(pStmt);
144125 return rc;
144126}
144127static int execSqlF(sqlite3 *db, char **pzErrMsg, const char *zSql, ...){
144128 char *z;
144129 va_list ap;
144130 int rc;
144131 va_start(ap, zSql);
144132 z = sqlite3VMPrintf(db, zFormat: zSql, ap);
144133 va_end(ap);
144134 if( z==0 ) return SQLITE_NOMEM;
144135 rc = execSql(db, pzErrMsg, zSql: z);
144136 sqlite3DbFree(db, p: z);
144137 return rc;
144138}
144139
144140/*
144141** The VACUUM command is used to clean up the database,
144142** collapse free space, etc. It is modelled after the VACUUM command
144143** in PostgreSQL. The VACUUM command works as follows:
144144**
144145** (1) Create a new transient database file
144146** (2) Copy all content from the database being vacuumed into
144147** the new transient database file
144148** (3) Copy content from the transient database back into the
144149** original database.
144150**
144151** The transient database requires temporary disk space approximately
144152** equal to the size of the original database. The copy operation of
144153** step (3) requires additional temporary disk space approximately equal
144154** to the size of the original database for the rollback journal.
144155** Hence, temporary disk space that is approximately 2x the size of the
144156** original database is required. Every page of the database is written
144157** approximately 3 times: Once for step (2) and twice for step (3).
144158** Two writes per page are required in step (3) because the original
144159** database content must be written into the rollback journal prior to
144160** overwriting the database with the vacuumed content.
144161**
144162** Only 1x temporary space and only 1x writes would be required if
144163** the copy of step (3) were replaced by deleting the original database
144164** and renaming the transient database as the original. But that will
144165** not work if other processes are attached to the original database.
144166** And a power loss in between deleting the original and renaming the
144167** transient would cause the database file to appear to be deleted
144168** following reboot.
144169*/
144170SQLITE_PRIVATE void sqlite3Vacuum(Parse *pParse, Token *pNm, Expr *pInto){
144171 Vdbe *v = sqlite3GetVdbe(pParse);
144172 int iDb = 0;
144173 if( v==0 ) goto build_vacuum_end;
144174 if( pParse->nErr ) goto build_vacuum_end;
144175 if( pNm ){
144176#ifndef SQLITE_BUG_COMPATIBLE_20160819
144177 /* Default behavior: Report an error if the argument to VACUUM is
144178 ** not recognized */
144179 iDb = sqlite3TwoPartName(pParse, pName1: pNm, pName2: pNm, pUnqual: &pNm);
144180 if( iDb<0 ) goto build_vacuum_end;
144181#else
144182 /* When SQLITE_BUG_COMPATIBLE_20160819 is defined, unrecognized arguments
144183 ** to VACUUM are silently ignored. This is a back-out of a bug fix that
144184 ** occurred on 2016-08-19 (https://www.sqlite.org/src/info/083f9e6270).
144185 ** The buggy behavior is required for binary compatibility with some
144186 ** legacy applications. */
144187 iDb = sqlite3FindDb(pParse->db, pNm);
144188 if( iDb<0 ) iDb = 0;
144189#endif
144190 }
144191 if( iDb!=1 ){
144192 int iIntoReg = 0;
144193 if( pInto && sqlite3ResolveSelfReference(pParse,pTab: 0,type: 0,pExpr: pInto,pList: 0)==0 ){
144194 iIntoReg = ++pParse->nMem;
144195 sqlite3ExprCode(pParse, pExpr: pInto, target: iIntoReg);
144196 }
144197 sqlite3VdbeAddOp2(p: v, OP_Vacuum, p1: iDb, p2: iIntoReg);
144198 sqlite3VdbeUsesBtree(p: v, i: iDb);
144199 }
144200build_vacuum_end:
144201 sqlite3ExprDelete(db: pParse->db, p: pInto);
144202 return;
144203}
144204
144205/*
144206** This routine implements the OP_Vacuum opcode of the VDBE.
144207*/
144208SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3RunVacuum(
144209 char **pzErrMsg, /* Write error message here */
144210 sqlite3 *db, /* Database connection */
144211 int iDb, /* Which attached DB to vacuum */
144212 sqlite3_value *pOut /* Write results here, if not NULL. VACUUM INTO */
144213){
144214 int rc = SQLITE_OK; /* Return code from service routines */
144215 Btree *pMain; /* The database being vacuumed */
144216 Btree *pTemp; /* The temporary database we vacuum into */
144217 u32 saved_mDbFlags; /* Saved value of db->mDbFlags */
144218 u64 saved_flags; /* Saved value of db->flags */
144219 i64 saved_nChange; /* Saved value of db->nChange */
144220 i64 saved_nTotalChange; /* Saved value of db->nTotalChange */
144221 u32 saved_openFlags; /* Saved value of db->openFlags */
144222 u8 saved_mTrace; /* Saved trace settings */
144223 Db *pDb = 0; /* Database to detach at end of vacuum */
144224 int isMemDb; /* True if vacuuming a :memory: database */
144225 int nRes; /* Bytes of reserved space at the end of each page */
144226 int nDb; /* Number of attached databases */
144227 const char *zDbMain; /* Schema name of database to vacuum */
144228 const char *zOut; /* Name of output file */
144229
144230 if( !db->autoCommit ){
144231 sqlite3SetString(pz: pzErrMsg, db, zNew: "cannot VACUUM from within a transaction");
144232 return SQLITE_ERROR; /* IMP: R-12218-18073 */
144233 }
144234 if( db->nVdbeActive>1 ){
144235 sqlite3SetString(pz: pzErrMsg, db,zNew: "cannot VACUUM - SQL statements in progress");
144236 return SQLITE_ERROR; /* IMP: R-15610-35227 */
144237 }
144238 saved_openFlags = db->openFlags;
144239 if( pOut ){
144240 if( sqlite3_value_type(pVal: pOut)!=SQLITE_TEXT ){
144241 sqlite3SetString(pz: pzErrMsg, db, zNew: "non-text filename");
144242 return SQLITE_ERROR;
144243 }
144244 zOut = (const char*)sqlite3_value_text(pVal: pOut);
144245 db->openFlags &= ~SQLITE_OPEN_READONLY;
144246 db->openFlags |= SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE;
144247 }else{
144248 zOut = "";
144249 }
144250
144251 /* Save the current value of the database flags so that it can be
144252 ** restored before returning. Then set the writable-schema flag, and
144253 ** disable CHECK and foreign key constraints. */
144254 saved_flags = db->flags;
144255 saved_mDbFlags = db->mDbFlags;
144256 saved_nChange = db->nChange;
144257 saved_nTotalChange = db->nTotalChange;
144258 saved_mTrace = db->mTrace;
144259 db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
144260 db->mDbFlags |= DBFLAG_PreferBuiltin | DBFLAG_Vacuum;
144261 db->flags &= ~(u64)(SQLITE_ForeignKeys | SQLITE_ReverseOrder
144262 | SQLITE_Defensive | SQLITE_CountRows);
144263 db->mTrace = 0;
144264
144265 zDbMain = db->aDb[iDb].zDbSName;
144266 pMain = db->aDb[iDb].pBt;
144267 isMemDb = sqlite3PagerIsMemdb(pPager: sqlite3BtreePager(p: pMain));
144268
144269 /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
144270 ** can be set to 'off' for this file, as it is not recovered if a crash
144271 ** occurs anyway. The integrity of the database is maintained by a
144272 ** (possibly synchronous) transaction opened on the main database before
144273 ** sqlite3BtreeCopyFile() is called.
144274 **
144275 ** An optimisation would be to use a non-journaled pager.
144276 ** (Later:) I tried setting "PRAGMA vacuum_db.journal_mode=OFF" but
144277 ** that actually made the VACUUM run slower. Very little journalling
144278 ** actually occurs when doing a vacuum since the vacuum_db is initially
144279 ** empty. Only the journal header is written. Apparently it takes more
144280 ** time to parse and run the PRAGMA to turn journalling off than it does
144281 ** to write the journal header file.
144282 */
144283 nDb = db->nDb;
144284 rc = execSqlF(db, pzErrMsg, zSql: "ATTACH %Q AS vacuum_db", zOut);
144285 db->openFlags = saved_openFlags;
144286 if( rc!=SQLITE_OK ) goto end_of_vacuum;
144287 assert( (db->nDb-1)==nDb );
144288 pDb = &db->aDb[nDb];
144289 assert( strcmp(pDb->zDbSName,"vacuum_db")==0 );
144290 pTemp = pDb->pBt;
144291 if( pOut ){
144292 sqlite3_file *id = sqlite3PagerFile(pPager: sqlite3BtreePager(p: pTemp));
144293 i64 sz = 0;
144294 if( id->pMethods!=0 && (sqlite3OsFileSize(id, pSize: &sz)!=SQLITE_OK || sz>0) ){
144295 rc = SQLITE_ERROR;
144296 sqlite3SetString(pz: pzErrMsg, db, zNew: "output file already exists");
144297 goto end_of_vacuum;
144298 }
144299 db->mDbFlags |= DBFLAG_VacuumInto;
144300 }
144301 nRes = sqlite3BtreeGetRequestedReserve(p: pMain);
144302
144303 sqlite3BtreeSetCacheSize(p: pTemp, mxPage: db->aDb[iDb].pSchema->cache_size);
144304 sqlite3BtreeSetSpillSize(p: pTemp, mxPage: sqlite3BtreeSetSpillSize(p: pMain,mxPage: 0));
144305 sqlite3BtreeSetPagerFlags(p: pTemp, PAGER_SYNCHRONOUS_OFF|PAGER_CACHESPILL);
144306
144307 /* Begin a transaction and take an exclusive lock on the main database
144308 ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below,
144309 ** to ensure that we do not try to change the page-size on a WAL database.
144310 */
144311 rc = execSql(db, pzErrMsg, zSql: "BEGIN");
144312 if( rc!=SQLITE_OK ) goto end_of_vacuum;
144313 rc = sqlite3BtreeBeginTrans(p: pMain, wrflag: pOut==0 ? 2 : 0, pSchemaVersion: 0);
144314 if( rc!=SQLITE_OK ) goto end_of_vacuum;
144315
144316 /* Do not attempt to change the page size for a WAL database */
144317 if( sqlite3PagerGetJournalMode(pPager: sqlite3BtreePager(p: pMain))
144318 ==PAGER_JOURNALMODE_WAL
144319 && pOut==0
144320 ){
144321 db->nextPagesize = 0;
144322 }
144323
144324 if( sqlite3BtreeSetPageSize(p: pTemp, pageSize: sqlite3BtreeGetPageSize(p: pMain), nReserve: nRes, iFix: 0)
144325 || (!isMemDb && sqlite3BtreeSetPageSize(p: pTemp, pageSize: db->nextPagesize, nReserve: nRes, iFix: 0))
144326 || NEVER(db->mallocFailed)
144327 ){
144328 rc = SQLITE_NOMEM_BKPT;
144329 goto end_of_vacuum;
144330 }
144331
144332#ifndef SQLITE_OMIT_AUTOVACUUM
144333 sqlite3BtreeSetAutoVacuum(p: pTemp, autoVacuum: db->nextAutovac>=0 ? db->nextAutovac :
144334 sqlite3BtreeGetAutoVacuum(p: pMain));
144335#endif
144336
144337 /* Query the schema of the main database. Create a mirror schema
144338 ** in the temporary database.
144339 */
144340 db->init.iDb = nDb; /* force new CREATE statements into vacuum_db */
144341 rc = execSqlF(db, pzErrMsg,
144342 zSql: "SELECT sql FROM \"%w\".sqlite_schema"
144343 " WHERE type='table'AND name<>'sqlite_sequence'"
144344 " AND coalesce(rootpage,1)>0",
144345 zDbMain
144346 );
144347 if( rc!=SQLITE_OK ) goto end_of_vacuum;
144348 rc = execSqlF(db, pzErrMsg,
144349 zSql: "SELECT sql FROM \"%w\".sqlite_schema"
144350 " WHERE type='index'",
144351 zDbMain
144352 );
144353 if( rc!=SQLITE_OK ) goto end_of_vacuum;
144354 db->init.iDb = 0;
144355
144356 /* Loop through the tables in the main database. For each, do
144357 ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
144358 ** the contents to the temporary database.
144359 */
144360 rc = execSqlF(db, pzErrMsg,
144361 zSql: "SELECT'INSERT INTO vacuum_db.'||quote(name)"
144362 "||' SELECT*FROM\"%w\".'||quote(name)"
144363 "FROM vacuum_db.sqlite_schema "
144364 "WHERE type='table'AND coalesce(rootpage,1)>0",
144365 zDbMain
144366 );
144367 assert( (db->mDbFlags & DBFLAG_Vacuum)!=0 );
144368 db->mDbFlags &= ~DBFLAG_Vacuum;
144369 if( rc!=SQLITE_OK ) goto end_of_vacuum;
144370
144371 /* Copy the triggers, views, and virtual tables from the main database
144372 ** over to the temporary database. None of these objects has any
144373 ** associated storage, so all we have to do is copy their entries
144374 ** from the schema table.
144375 */
144376 rc = execSqlF(db, pzErrMsg,
144377 zSql: "INSERT INTO vacuum_db.sqlite_schema"
144378 " SELECT*FROM \"%w\".sqlite_schema"
144379 " WHERE type IN('view','trigger')"
144380 " OR(type='table'AND rootpage=0)",
144381 zDbMain
144382 );
144383 if( rc ) goto end_of_vacuum;
144384
144385 /* At this point, there is a write transaction open on both the
144386 ** vacuum database and the main database. Assuming no error occurs,
144387 ** both transactions are closed by this block - the main database
144388 ** transaction by sqlite3BtreeCopyFile() and the other by an explicit
144389 ** call to sqlite3BtreeCommit().
144390 */
144391 {
144392 u32 meta;
144393 int i;
144394
144395 /* This array determines which meta meta values are preserved in the
144396 ** vacuum. Even entries are the meta value number and odd entries
144397 ** are an increment to apply to the meta value after the vacuum.
144398 ** The increment is used to increase the schema cookie so that other
144399 ** connections to the same database will know to reread the schema.
144400 */
144401 static const unsigned char aCopy[] = {
144402 BTREE_SCHEMA_VERSION, 1, /* Add one to the old schema cookie */
144403 BTREE_DEFAULT_CACHE_SIZE, 0, /* Preserve the default page cache size */
144404 BTREE_TEXT_ENCODING, 0, /* Preserve the text encoding */
144405 BTREE_USER_VERSION, 0, /* Preserve the user version */
144406 BTREE_APPLICATION_ID, 0, /* Preserve the application id */
144407 };
144408
144409 assert( SQLITE_TXN_WRITE==sqlite3BtreeTxnState(pTemp) );
144410 assert( pOut!=0 || SQLITE_TXN_WRITE==sqlite3BtreeTxnState(pMain) );
144411
144412 /* Copy Btree meta values */
144413 for(i=0; i<ArraySize(aCopy); i+=2){
144414 /* GetMeta() and UpdateMeta() cannot fail in this context because
144415 ** we already have page 1 loaded into cache and marked dirty. */
144416 sqlite3BtreeGetMeta(p: pMain, idx: aCopy[i], pMeta: &meta);
144417 rc = sqlite3BtreeUpdateMeta(p: pTemp, idx: aCopy[i], iMeta: meta+aCopy[i+1]);
144418 if( NEVER(rc!=SQLITE_OK) ) goto end_of_vacuum;
144419 }
144420
144421 if( pOut==0 ){
144422 rc = sqlite3BtreeCopyFile(pTo: pMain, pFrom: pTemp);
144423 }
144424 if( rc!=SQLITE_OK ) goto end_of_vacuum;
144425 rc = sqlite3BtreeCommit(p: pTemp);
144426 if( rc!=SQLITE_OK ) goto end_of_vacuum;
144427#ifndef SQLITE_OMIT_AUTOVACUUM
144428 if( pOut==0 ){
144429 sqlite3BtreeSetAutoVacuum(p: pMain, autoVacuum: sqlite3BtreeGetAutoVacuum(p: pTemp));
144430 }
144431#endif
144432 }
144433
144434 assert( rc==SQLITE_OK );
144435 if( pOut==0 ){
144436 rc = sqlite3BtreeSetPageSize(p: pMain, pageSize: sqlite3BtreeGetPageSize(p: pTemp), nReserve: nRes,iFix: 1);
144437 }
144438
144439end_of_vacuum:
144440 /* Restore the original value of db->flags */
144441 db->init.iDb = 0;
144442 db->mDbFlags = saved_mDbFlags;
144443 db->flags = saved_flags;
144444 db->nChange = saved_nChange;
144445 db->nTotalChange = saved_nTotalChange;
144446 db->mTrace = saved_mTrace;
144447 sqlite3BtreeSetPageSize(p: pMain, pageSize: -1, nReserve: 0, iFix: 1);
144448
144449 /* Currently there is an SQL level transaction open on the vacuum
144450 ** database. No locks are held on any other files (since the main file
144451 ** was committed at the btree level). So it safe to end the transaction
144452 ** by manually setting the autoCommit flag to true and detaching the
144453 ** vacuum database. The vacuum_db journal file is deleted when the pager
144454 ** is closed by the DETACH.
144455 */
144456 db->autoCommit = 1;
144457
144458 if( pDb ){
144459 sqlite3BtreeClose(p: pDb->pBt);
144460 pDb->pBt = 0;
144461 pDb->pSchema = 0;
144462 }
144463
144464 /* This both clears the schemas and reduces the size of the db->aDb[]
144465 ** array. */
144466 sqlite3ResetAllSchemasOfConnection(db);
144467
144468 return rc;
144469}
144470
144471#endif /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */
144472
144473/************** End of vacuum.c **********************************************/
144474/************** Begin file vtab.c ********************************************/
144475/*
144476** 2006 June 10
144477**
144478** The author disclaims copyright to this source code. In place of
144479** a legal notice, here is a blessing:
144480**
144481** May you do good and not evil.
144482** May you find forgiveness for yourself and forgive others.
144483** May you share freely, never taking more than you give.
144484**
144485*************************************************************************
144486** This file contains code used to help implement virtual tables.
144487*/
144488#ifndef SQLITE_OMIT_VIRTUALTABLE
144489/* #include "sqliteInt.h" */
144490
144491/*
144492** Before a virtual table xCreate() or xConnect() method is invoked, the
144493** sqlite3.pVtabCtx member variable is set to point to an instance of
144494** this struct allocated on the stack. It is used by the implementation of
144495** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which
144496** are invoked only from within xCreate and xConnect methods.
144497*/
144498struct VtabCtx {
144499 VTable *pVTable; /* The virtual table being constructed */
144500 Table *pTab; /* The Table object to which the virtual table belongs */
144501 VtabCtx *pPrior; /* Parent context (if any) */
144502 int bDeclared; /* True after sqlite3_declare_vtab() is called */
144503};
144504
144505/*
144506** Construct and install a Module object for a virtual table. When this
144507** routine is called, it is guaranteed that all appropriate locks are held
144508** and the module is not already part of the connection.
144509**
144510** If there already exists a module with zName, replace it with the new one.
144511** If pModule==0, then delete the module zName if it exists.
144512*/
144513SQLITE_PRIVATE Module *sqlite3VtabCreateModule(
144514 sqlite3 *db, /* Database in which module is registered */
144515 const char *zName, /* Name assigned to this module */
144516 const sqlite3_module *pModule, /* The definition of the module */
144517 void *pAux, /* Context pointer for xCreate/xConnect */
144518 void (*xDestroy)(void *) /* Module destructor function */
144519){
144520 Module *pMod;
144521 Module *pDel;
144522 char *zCopy;
144523 if( pModule==0 ){
144524 zCopy = (char*)zName;
144525 pMod = 0;
144526 }else{
144527 int nName = sqlite3Strlen30(z: zName);
144528 pMod = (Module *)sqlite3Malloc(n: sizeof(Module) + nName + 1);
144529 if( pMod==0 ){
144530 sqlite3OomFault(db);
144531 return 0;
144532 }
144533 zCopy = (char *)(&pMod[1]);
144534 memcpy(dest: zCopy, src: zName, n: nName+1);
144535 pMod->zName = zCopy;
144536 pMod->pModule = pModule;
144537 pMod->pAux = pAux;
144538 pMod->xDestroy = xDestroy;
144539 pMod->pEpoTab = 0;
144540 pMod->nRefModule = 1;
144541 }
144542 pDel = (Module *)sqlite3HashInsert(pH: &db->aModule,pKey: zCopy,data: (void*)pMod);
144543 if( pDel ){
144544 if( pDel==pMod ){
144545 sqlite3OomFault(db);
144546 sqlite3DbFree(db, p: pDel);
144547 pMod = 0;
144548 }else{
144549 sqlite3VtabEponymousTableClear(db, pDel);
144550 sqlite3VtabModuleUnref(db, pDel);
144551 }
144552 }
144553 return pMod;
144554}
144555
144556/*
144557** The actual function that does the work of creating a new module.
144558** This function implements the sqlite3_create_module() and
144559** sqlite3_create_module_v2() interfaces.
144560*/
144561static int createModule(
144562 sqlite3 *db, /* Database in which module is registered */
144563 const char *zName, /* Name assigned to this module */
144564 const sqlite3_module *pModule, /* The definition of the module */
144565 void *pAux, /* Context pointer for xCreate/xConnect */
144566 void (*xDestroy)(void *) /* Module destructor function */
144567){
144568 int rc = SQLITE_OK;
144569
144570 sqlite3_mutex_enter(p: db->mutex);
144571 (void)sqlite3VtabCreateModule(db, zName, pModule, pAux, xDestroy);
144572 rc = sqlite3ApiExit(db, rc);
144573 if( rc!=SQLITE_OK && xDestroy ) xDestroy(pAux);
144574 sqlite3_mutex_leave(p: db->mutex);
144575 return rc;
144576}
144577
144578
144579/*
144580** External API function used to create a new virtual-table module.
144581*/
144582SQLITE_API int sqlite3_create_module(
144583 sqlite3 *db, /* Database in which module is registered */
144584 const char *zName, /* Name assigned to this module */
144585 const sqlite3_module *pModule, /* The definition of the module */
144586 void *pAux /* Context pointer for xCreate/xConnect */
144587){
144588#ifdef SQLITE_ENABLE_API_ARMOR
144589 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
144590#endif
144591 return createModule(db, zName, pModule, pAux, xDestroy: 0);
144592}
144593
144594/*
144595** External API function used to create a new virtual-table module.
144596*/
144597SQLITE_API int sqlite3_create_module_v2(
144598 sqlite3 *db, /* Database in which module is registered */
144599 const char *zName, /* Name assigned to this module */
144600 const sqlite3_module *pModule, /* The definition of the module */
144601 void *pAux, /* Context pointer for xCreate/xConnect */
144602 void (*xDestroy)(void *) /* Module destructor function */
144603){
144604#ifdef SQLITE_ENABLE_API_ARMOR
144605 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
144606#endif
144607 return createModule(db, zName, pModule, pAux, xDestroy);
144608}
144609
144610/*
144611** External API to drop all virtual-table modules, except those named
144612** on the azNames list.
144613*/
144614SQLITE_API int sqlite3_drop_modules(sqlite3 *db, const char** azNames){
144615 HashElem *pThis, *pNext;
144616#ifdef SQLITE_ENABLE_API_ARMOR
144617 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
144618#endif
144619 for(pThis=sqliteHashFirst(&db->aModule); pThis; pThis=pNext){
144620 Module *pMod = (Module*)sqliteHashData(pThis);
144621 pNext = sqliteHashNext(pThis);
144622 if( azNames ){
144623 int ii;
144624 for(ii=0; azNames[ii]!=0 && strcmp(s1: azNames[ii],s2: pMod->zName)!=0; ii++){}
144625 if( azNames[ii]!=0 ) continue;
144626 }
144627 createModule(db, zName: pMod->zName, pModule: 0, pAux: 0, xDestroy: 0);
144628 }
144629 return SQLITE_OK;
144630}
144631
144632/*
144633** Decrement the reference count on a Module object. Destroy the
144634** module when the reference count reaches zero.
144635*/
144636SQLITE_PRIVATE void sqlite3VtabModuleUnref(sqlite3 *db, Module *pMod){
144637 assert( pMod->nRefModule>0 );
144638 pMod->nRefModule--;
144639 if( pMod->nRefModule==0 ){
144640 if( pMod->xDestroy ){
144641 pMod->xDestroy(pMod->pAux);
144642 }
144643 assert( pMod->pEpoTab==0 );
144644 sqlite3DbFree(db, p: pMod);
144645 }
144646}
144647
144648/*
144649** Lock the virtual table so that it cannot be disconnected.
144650** Locks nest. Every lock should have a corresponding unlock.
144651** If an unlock is omitted, resources leaks will occur.
144652**
144653** If a disconnect is attempted while a virtual table is locked,
144654** the disconnect is deferred until all locks have been removed.
144655*/
144656SQLITE_PRIVATE void sqlite3VtabLock(VTable *pVTab){
144657 pVTab->nRef++;
144658}
144659
144660
144661/*
144662** pTab is a pointer to a Table structure representing a virtual-table.
144663** Return a pointer to the VTable object used by connection db to access
144664** this virtual-table, if one has been created, or NULL otherwise.
144665*/
144666SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){
144667 VTable *pVtab;
144668 assert( IsVirtual(pTab) );
144669 for(pVtab=pTab->u.vtab.p; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
144670 return pVtab;
144671}
144672
144673/*
144674** Decrement the ref-count on a virtual table object. When the ref-count
144675** reaches zero, call the xDisconnect() method to delete the object.
144676*/
144677SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *pVTab){
144678 sqlite3 *db = pVTab->db;
144679
144680 assert( db );
144681 assert( pVTab->nRef>0 );
144682 assert( db->eOpenState==SQLITE_STATE_OPEN
144683 || db->eOpenState==SQLITE_STATE_ZOMBIE );
144684
144685 pVTab->nRef--;
144686 if( pVTab->nRef==0 ){
144687 sqlite3_vtab *p = pVTab->pVtab;
144688 sqlite3VtabModuleUnref(db: pVTab->db, pMod: pVTab->pMod);
144689 if( p ){
144690 p->pModule->xDisconnect(p);
144691 }
144692 sqlite3DbFree(db, p: pVTab);
144693 }
144694}
144695
144696/*
144697** Table p is a virtual table. This function moves all elements in the
144698** p->u.vtab.p list to the sqlite3.pDisconnect lists of their associated
144699** database connections to be disconnected at the next opportunity.
144700** Except, if argument db is not NULL, then the entry associated with
144701** connection db is left in the p->u.vtab.p list.
144702*/
144703static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){
144704 VTable *pRet = 0;
144705 VTable *pVTable;
144706
144707 assert( IsVirtual(p) );
144708 pVTable = p->u.vtab.p;
144709 p->u.vtab.p = 0;
144710
144711 /* Assert that the mutex (if any) associated with the BtShared database
144712 ** that contains table p is held by the caller. See header comments
144713 ** above function sqlite3VtabUnlockList() for an explanation of why
144714 ** this makes it safe to access the sqlite3.pDisconnect list of any
144715 ** database connection that may have an entry in the p->u.vtab.p list.
144716 */
144717 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
144718
144719 while( pVTable ){
144720 sqlite3 *db2 = pVTable->db;
144721 VTable *pNext = pVTable->pNext;
144722 assert( db2 );
144723 if( db2==db ){
144724 pRet = pVTable;
144725 p->u.vtab.p = pRet;
144726 pRet->pNext = 0;
144727 }else{
144728 pVTable->pNext = db2->pDisconnect;
144729 db2->pDisconnect = pVTable;
144730 }
144731 pVTable = pNext;
144732 }
144733
144734 assert( !db || pRet );
144735 return pRet;
144736}
144737
144738/*
144739** Table *p is a virtual table. This function removes the VTable object
144740** for table *p associated with database connection db from the linked
144741** list in p->pVTab. It also decrements the VTable ref count. This is
144742** used when closing database connection db to free all of its VTable
144743** objects without disturbing the rest of the Schema object (which may
144744** be being used by other shared-cache connections).
144745*/
144746SQLITE_PRIVATE void sqlite3VtabDisconnect(sqlite3 *db, Table *p){
144747 VTable **ppVTab;
144748
144749 assert( IsVirtual(p) );
144750 assert( sqlite3BtreeHoldsAllMutexes(db) );
144751 assert( sqlite3_mutex_held(db->mutex) );
144752
144753 for(ppVTab=&p->u.vtab.p; *ppVTab; ppVTab=&(*ppVTab)->pNext){
144754 if( (*ppVTab)->db==db ){
144755 VTable *pVTab = *ppVTab;
144756 *ppVTab = pVTab->pNext;
144757 sqlite3VtabUnlock(pVTab);
144758 break;
144759 }
144760 }
144761}
144762
144763
144764/*
144765** Disconnect all the virtual table objects in the sqlite3.pDisconnect list.
144766**
144767** This function may only be called when the mutexes associated with all
144768** shared b-tree databases opened using connection db are held by the
144769** caller. This is done to protect the sqlite3.pDisconnect list. The
144770** sqlite3.pDisconnect list is accessed only as follows:
144771**
144772** 1) By this function. In this case, all BtShared mutexes and the mutex
144773** associated with the database handle itself must be held.
144774**
144775** 2) By function vtabDisconnectAll(), when it adds a VTable entry to
144776** the sqlite3.pDisconnect list. In this case either the BtShared mutex
144777** associated with the database the virtual table is stored in is held
144778** or, if the virtual table is stored in a non-sharable database, then
144779** the database handle mutex is held.
144780**
144781** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously
144782** by multiple threads. It is thread-safe.
144783*/
144784SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3 *db){
144785 VTable *p = db->pDisconnect;
144786
144787 assert( sqlite3BtreeHoldsAllMutexes(db) );
144788 assert( sqlite3_mutex_held(db->mutex) );
144789
144790 if( p ){
144791 db->pDisconnect = 0;
144792 sqlite3ExpirePreparedStatements(db, iCode: 0);
144793 do {
144794 VTable *pNext = p->pNext;
144795 sqlite3VtabUnlock(pVTab: p);
144796 p = pNext;
144797 }while( p );
144798 }
144799}
144800
144801/*
144802** Clear any and all virtual-table information from the Table record.
144803** This routine is called, for example, just before deleting the Table
144804** record.
144805**
144806** Since it is a virtual-table, the Table structure contains a pointer
144807** to the head of a linked list of VTable structures. Each VTable
144808** structure is associated with a single sqlite3* user of the schema.
144809** The reference count of the VTable structure associated with database
144810** connection db is decremented immediately (which may lead to the
144811** structure being xDisconnected and free). Any other VTable structures
144812** in the list are moved to the sqlite3.pDisconnect list of the associated
144813** database connection.
144814*/
144815SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table *p){
144816 assert( IsVirtual(p) );
144817 if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(db: 0, p);
144818 if( p->u.vtab.azArg ){
144819 int i;
144820 for(i=0; i<p->u.vtab.nArg; i++){
144821 if( i!=1 ) sqlite3DbFree(db, p: p->u.vtab.azArg[i]);
144822 }
144823 sqlite3DbFree(db, p: p->u.vtab.azArg);
144824 }
144825}
144826
144827/*
144828** Add a new module argument to pTable->u.vtab.azArg[].
144829** The string is not copied - the pointer is stored. The
144830** string will be freed automatically when the table is
144831** deleted.
144832*/
144833static void addModuleArgument(Parse *pParse, Table *pTable, char *zArg){
144834 sqlite3_int64 nBytes;
144835 char **azModuleArg;
144836 sqlite3 *db = pParse->db;
144837
144838 assert( IsVirtual(pTable) );
144839 nBytes = sizeof(char *)*(2+pTable->u.vtab.nArg);
144840 if( pTable->u.vtab.nArg+3>=db->aLimit[SQLITE_LIMIT_COLUMN] ){
144841 sqlite3ErrorMsg(pParse, zFormat: "too many columns on %s", pTable->zName);
144842 }
144843 azModuleArg = sqlite3DbRealloc(db, p: pTable->u.vtab.azArg, n: nBytes);
144844 if( azModuleArg==0 ){
144845 sqlite3DbFree(db, p: zArg);
144846 }else{
144847 int i = pTable->u.vtab.nArg++;
144848 azModuleArg[i] = zArg;
144849 azModuleArg[i+1] = 0;
144850 pTable->u.vtab.azArg = azModuleArg;
144851 }
144852}
144853
144854/*
144855** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
144856** statement. The module name has been parsed, but the optional list
144857** of parameters that follow the module name are still pending.
144858*/
144859SQLITE_PRIVATE void sqlite3VtabBeginParse(
144860 Parse *pParse, /* Parsing context */
144861 Token *pName1, /* Name of new table, or database name */
144862 Token *pName2, /* Name of new table or NULL */
144863 Token *pModuleName, /* Name of the module for the virtual table */
144864 int ifNotExists /* No error if the table already exists */
144865){
144866 Table *pTable; /* The new virtual table */
144867 sqlite3 *db; /* Database connection */
144868
144869 sqlite3StartTable(pParse, pName1, pName2, isTemp: 0, isView: 0, isVirtual: 1, noErr: ifNotExists);
144870 pTable = pParse->pNewTable;
144871 if( pTable==0 ) return;
144872 assert( 0==pTable->pIndex );
144873 pTable->eTabType = TABTYP_VTAB;
144874
144875 db = pParse->db;
144876
144877 assert( pTable->u.vtab.nArg==0 );
144878 addModuleArgument(pParse, pTable, zArg: sqlite3NameFromToken(db, pName: pModuleName));
144879 addModuleArgument(pParse, pTable, zArg: 0);
144880 addModuleArgument(pParse, pTable, zArg: sqlite3DbStrDup(db, z: pTable->zName));
144881 assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)
144882 || (pParse->sNameToken.z==pName1->z && pName2->z==0)
144883 );
144884 pParse->sNameToken.n = (int)(
144885 &pModuleName->z[pModuleName->n] - pParse->sNameToken.z
144886 );
144887
144888#ifndef SQLITE_OMIT_AUTHORIZATION
144889 /* Creating a virtual table invokes the authorization callback twice.
144890 ** The first invocation, to obtain permission to INSERT a row into the
144891 ** sqlite_schema table, has already been made by sqlite3StartTable().
144892 ** The second call, to obtain permission to create the table, is made now.
144893 */
144894 if( pTable->u.vtab.azArg ){
144895 int iDb = sqlite3SchemaToIndex(db, pSchema: pTable->pSchema);
144896 assert( iDb>=0 ); /* The database the table is being created in */
144897 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, zArg1: pTable->zName,
144898 zArg2: pTable->u.vtab.azArg[0], zArg3: pParse->db->aDb[iDb].zDbSName);
144899 }
144900#endif
144901}
144902
144903/*
144904** This routine takes the module argument that has been accumulating
144905** in pParse->zArg[] and appends it to the list of arguments on the
144906** virtual table currently under construction in pParse->pTable.
144907*/
144908static void addArgumentToVtab(Parse *pParse){
144909 if( pParse->sArg.z && pParse->pNewTable ){
144910 const char *z = (const char*)pParse->sArg.z;
144911 int n = pParse->sArg.n;
144912 sqlite3 *db = pParse->db;
144913 addModuleArgument(pParse, pTable: pParse->pNewTable, zArg: sqlite3DbStrNDup(db, z, n));
144914 }
144915}
144916
144917/*
144918** The parser calls this routine after the CREATE VIRTUAL TABLE statement
144919** has been completely parsed.
144920*/
144921SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
144922 Table *pTab = pParse->pNewTable; /* The table being constructed */
144923 sqlite3 *db = pParse->db; /* The database connection */
144924
144925 if( pTab==0 ) return;
144926 assert( IsVirtual(pTab) );
144927 addArgumentToVtab(pParse);
144928 pParse->sArg.z = 0;
144929 if( pTab->u.vtab.nArg<1 ) return;
144930
144931 /* If the CREATE VIRTUAL TABLE statement is being entered for the
144932 ** first time (in other words if the virtual table is actually being
144933 ** created now instead of just being read out of sqlite_schema) then
144934 ** do additional initialization work and store the statement text
144935 ** in the sqlite_schema table.
144936 */
144937 if( !db->init.busy ){
144938 char *zStmt;
144939 char *zWhere;
144940 int iDb;
144941 int iReg;
144942 Vdbe *v;
144943
144944 sqlite3MayAbort(pParse);
144945
144946 /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
144947 if( pEnd ){
144948 pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
144949 }
144950 zStmt = sqlite3MPrintf(db, zFormat: "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
144951
144952 /* A slot for the record has already been allocated in the
144953 ** schema table. We just need to update that slot with all
144954 ** the information we've collected.
144955 **
144956 ** The VM register number pParse->regRowid holds the rowid of an
144957 ** entry in the sqlite_schema table tht was created for this vtab
144958 ** by sqlite3StartTable().
144959 */
144960 iDb = sqlite3SchemaToIndex(db, pSchema: pTab->pSchema);
144961 sqlite3NestedParse(pParse,
144962 zFormat: "UPDATE %Q." LEGACY_SCHEMA_TABLE " "
144963 "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
144964 "WHERE rowid=#%d",
144965 db->aDb[iDb].zDbSName,
144966 pTab->zName,
144967 pTab->zName,
144968 zStmt,
144969 pParse->regRowid
144970 );
144971 v = sqlite3GetVdbe(pParse);
144972 sqlite3ChangeCookie(pParse, iDb);
144973
144974 sqlite3VdbeAddOp0(p: v, OP_Expire);
144975 zWhere = sqlite3MPrintf(db, zFormat: "name=%Q AND sql=%Q", pTab->zName, zStmt);
144976 sqlite3VdbeAddParseSchemaOp(p: v, iDb, zWhere, p5: 0);
144977 sqlite3DbFree(db, p: zStmt);
144978
144979 iReg = ++pParse->nMem;
144980 sqlite3VdbeLoadString(p: v, iDest: iReg, zStr: pTab->zName);
144981 sqlite3VdbeAddOp2(p: v, OP_VCreate, p1: iDb, p2: iReg);
144982 }else{
144983 /* If we are rereading the sqlite_schema table create the in-memory
144984 ** record of the table. */
144985 Table *pOld;
144986 Schema *pSchema = pTab->pSchema;
144987 const char *zName = pTab->zName;
144988 assert( zName!=0 );
144989 sqlite3MarkAllShadowTablesOf(db, pTab);
144990 pOld = sqlite3HashInsert(pH: &pSchema->tblHash, pKey: zName, data: pTab);
144991 if( pOld ){
144992 sqlite3OomFault(db);
144993 assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */
144994 return;
144995 }
144996 pParse->pNewTable = 0;
144997 }
144998}
144999
145000/*
145001** The parser calls this routine when it sees the first token
145002** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
145003*/
145004SQLITE_PRIVATE void sqlite3VtabArgInit(Parse *pParse){
145005 addArgumentToVtab(pParse);
145006 pParse->sArg.z = 0;
145007 pParse->sArg.n = 0;
145008}
145009
145010/*
145011** The parser calls this routine for each token after the first token
145012** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
145013*/
145014SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse *pParse, Token *p){
145015 Token *pArg = &pParse->sArg;
145016 if( pArg->z==0 ){
145017 pArg->z = p->z;
145018 pArg->n = p->n;
145019 }else{
145020 assert(pArg->z <= p->z);
145021 pArg->n = (int)(&p->z[p->n] - pArg->z);
145022 }
145023}
145024
145025/*
145026** Invoke a virtual table constructor (either xCreate or xConnect). The
145027** pointer to the function to invoke is passed as the fourth parameter
145028** to this procedure.
145029*/
145030static int vtabCallConstructor(
145031 sqlite3 *db,
145032 Table *pTab,
145033 Module *pMod,
145034 int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
145035 char **pzErr
145036){
145037 VtabCtx sCtx;
145038 VTable *pVTable;
145039 int rc;
145040 const char *const*azArg;
145041 int nArg = pTab->u.vtab.nArg;
145042 char *zErr = 0;
145043 char *zModuleName;
145044 int iDb;
145045 VtabCtx *pCtx;
145046
145047 assert( IsVirtual(pTab) );
145048 azArg = (const char *const*)pTab->u.vtab.azArg;
145049
145050 /* Check that the virtual-table is not already being initialized */
145051 for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){
145052 if( pCtx->pTab==pTab ){
145053 *pzErr = sqlite3MPrintf(db,
145054 zFormat: "vtable constructor called recursively: %s", pTab->zName
145055 );
145056 return SQLITE_LOCKED;
145057 }
145058 }
145059
145060 zModuleName = sqlite3DbStrDup(db, z: pTab->zName);
145061 if( !zModuleName ){
145062 return SQLITE_NOMEM_BKPT;
145063 }
145064
145065 pVTable = sqlite3MallocZero(n: sizeof(VTable));
145066 if( !pVTable ){
145067 sqlite3OomFault(db);
145068 sqlite3DbFree(db, p: zModuleName);
145069 return SQLITE_NOMEM_BKPT;
145070 }
145071 pVTable->db = db;
145072 pVTable->pMod = pMod;
145073 pVTable->eVtabRisk = SQLITE_VTABRISK_Normal;
145074
145075 iDb = sqlite3SchemaToIndex(db, pSchema: pTab->pSchema);
145076 pTab->u.vtab.azArg[1] = db->aDb[iDb].zDbSName;
145077
145078 /* Invoke the virtual table constructor */
145079 assert( &db->pVtabCtx );
145080 assert( xConstruct );
145081 sCtx.pTab = pTab;
145082 sCtx.pVTable = pVTable;
145083 sCtx.pPrior = db->pVtabCtx;
145084 sCtx.bDeclared = 0;
145085 db->pVtabCtx = &sCtx;
145086 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
145087 db->pVtabCtx = sCtx.pPrior;
145088 if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
145089 assert( sCtx.pTab==pTab );
145090
145091 if( SQLITE_OK!=rc ){
145092 if( zErr==0 ){
145093 *pzErr = sqlite3MPrintf(db, zFormat: "vtable constructor failed: %s", zModuleName);
145094 }else {
145095 *pzErr = sqlite3MPrintf(db, zFormat: "%s", zErr);
145096 sqlite3_free(p: zErr);
145097 }
145098 sqlite3DbFree(db, p: pVTable);
145099 }else if( ALWAYS(pVTable->pVtab) ){
145100 /* Justification of ALWAYS(): A correct vtab constructor must allocate
145101 ** the sqlite3_vtab object if successful. */
145102 memset(s: pVTable->pVtab, c: 0, n: sizeof(pVTable->pVtab[0]));
145103 pVTable->pVtab->pModule = pMod->pModule;
145104 pMod->nRefModule++;
145105 pVTable->nRef = 1;
145106 if( sCtx.bDeclared==0 ){
145107 const char *zFormat = "vtable constructor did not declare schema: %s";
145108 *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
145109 sqlite3VtabUnlock(pVTab: pVTable);
145110 rc = SQLITE_ERROR;
145111 }else{
145112 int iCol;
145113 u16 oooHidden = 0;
145114 /* If everything went according to plan, link the new VTable structure
145115 ** into the linked list headed by pTab->u.vtab.p. Then loop through the
145116 ** columns of the table to see if any of them contain the token "hidden".
145117 ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from
145118 ** the type string. */
145119 pVTable->pNext = pTab->u.vtab.p;
145120 pTab->u.vtab.p = pVTable;
145121
145122 for(iCol=0; iCol<pTab->nCol; iCol++){
145123 char *zType = sqlite3ColumnType(pCol: &pTab->aCol[iCol], zDflt: "");
145124 int nType;
145125 int i = 0;
145126 nType = sqlite3Strlen30(z: zType);
145127 for(i=0; i<nType; i++){
145128 if( 0==sqlite3StrNICmp(zLeft: "hidden", zRight: &zType[i], N: 6)
145129 && (i==0 || zType[i-1]==' ')
145130 && (zType[i+6]=='\0' || zType[i+6]==' ')
145131 ){
145132 break;
145133 }
145134 }
145135 if( i<nType ){
145136 int j;
145137 int nDel = 6 + (zType[i+6] ? 1 : 0);
145138 for(j=i; (j+nDel)<=nType; j++){
145139 zType[j] = zType[j+nDel];
145140 }
145141 if( zType[i]=='\0' && i>0 ){
145142 assert(zType[i-1]==' ');
145143 zType[i-1] = '\0';
145144 }
145145 pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN;
145146 pTab->tabFlags |= TF_HasHidden;
145147 oooHidden = TF_OOOHidden;
145148 }else{
145149 pTab->tabFlags |= oooHidden;
145150 }
145151 }
145152 }
145153 }
145154
145155 sqlite3DbFree(db, p: zModuleName);
145156 return rc;
145157}
145158
145159/*
145160** This function is invoked by the parser to call the xConnect() method
145161** of the virtual table pTab. If an error occurs, an error code is returned
145162** and an error left in pParse.
145163**
145164** This call is a no-op if table pTab is not a virtual table.
145165*/
145166SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
145167 sqlite3 *db = pParse->db;
145168 const char *zMod;
145169 Module *pMod;
145170 int rc;
145171
145172 assert( pTab );
145173 assert( IsVirtual(pTab) );
145174 if( sqlite3GetVTable(db, pTab) ){
145175 return SQLITE_OK;
145176 }
145177
145178 /* Locate the required virtual table module */
145179 zMod = pTab->u.vtab.azArg[0];
145180 pMod = (Module*)sqlite3HashFind(pH: &db->aModule, pKey: zMod);
145181
145182 if( !pMod ){
145183 const char *zModule = pTab->u.vtab.azArg[0];
145184 sqlite3ErrorMsg(pParse, zFormat: "no such module: %s", zModule);
145185 rc = SQLITE_ERROR;
145186 }else{
145187 char *zErr = 0;
145188 rc = vtabCallConstructor(db, pTab, pMod, xConstruct: pMod->pModule->xConnect, pzErr: &zErr);
145189 if( rc!=SQLITE_OK ){
145190 sqlite3ErrorMsg(pParse, zFormat: "%s", zErr);
145191 pParse->rc = rc;
145192 }
145193 sqlite3DbFree(db, p: zErr);
145194 }
145195
145196 return rc;
145197}
145198/*
145199** Grow the db->aVTrans[] array so that there is room for at least one
145200** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
145201*/
145202static int growVTrans(sqlite3 *db){
145203 const int ARRAY_INCR = 5;
145204
145205 /* Grow the sqlite3.aVTrans array if required */
145206 if( (db->nVTrans%ARRAY_INCR)==0 ){
145207 VTable **aVTrans;
145208 sqlite3_int64 nBytes = sizeof(sqlite3_vtab*)*
145209 ((sqlite3_int64)db->nVTrans + ARRAY_INCR);
145210 aVTrans = sqlite3DbRealloc(db, p: (void *)db->aVTrans, n: nBytes);
145211 if( !aVTrans ){
145212 return SQLITE_NOMEM_BKPT;
145213 }
145214 memset(s: &aVTrans[db->nVTrans], c: 0, n: sizeof(sqlite3_vtab *)*ARRAY_INCR);
145215 db->aVTrans = aVTrans;
145216 }
145217
145218 return SQLITE_OK;
145219}
145220
145221/*
145222** Add the virtual table pVTab to the array sqlite3.aVTrans[]. Space should
145223** have already been reserved using growVTrans().
145224*/
145225static void addToVTrans(sqlite3 *db, VTable *pVTab){
145226 /* Add pVtab to the end of sqlite3.aVTrans */
145227 db->aVTrans[db->nVTrans++] = pVTab;
145228 sqlite3VtabLock(pVTab);
145229}
145230
145231/*
145232** This function is invoked by the vdbe to call the xCreate method
145233** of the virtual table named zTab in database iDb.
145234**
145235** If an error occurs, *pzErr is set to point to an English language
145236** description of the error and an SQLITE_XXX error code is returned.
145237** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
145238*/
145239SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
145240 int rc = SQLITE_OK;
145241 Table *pTab;
145242 Module *pMod;
145243 const char *zMod;
145244
145245 pTab = sqlite3FindTable(db, zName: zTab, zDatabase: db->aDb[iDb].zDbSName);
145246 assert( pTab && IsVirtual(pTab) && !pTab->u.vtab.p );
145247
145248 /* Locate the required virtual table module */
145249 zMod = pTab->u.vtab.azArg[0];
145250 pMod = (Module*)sqlite3HashFind(pH: &db->aModule, pKey: zMod);
145251
145252 /* If the module has been registered and includes a Create method,
145253 ** invoke it now. If the module has not been registered, return an
145254 ** error. Otherwise, do nothing.
145255 */
145256 if( pMod==0 || pMod->pModule->xCreate==0 || pMod->pModule->xDestroy==0 ){
145257 *pzErr = sqlite3MPrintf(db, zFormat: "no such module: %s", zMod);
145258 rc = SQLITE_ERROR;
145259 }else{
145260 rc = vtabCallConstructor(db, pTab, pMod, xConstruct: pMod->pModule->xCreate, pzErr);
145261 }
145262
145263 /* Justification of ALWAYS(): The xConstructor method is required to
145264 ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
145265 if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){
145266 rc = growVTrans(db);
145267 if( rc==SQLITE_OK ){
145268 addToVTrans(db, pVTab: sqlite3GetVTable(db, pTab));
145269 }
145270 }
145271
145272 return rc;
145273}
145274
145275/*
145276** This function is used to set the schema of a virtual table. It is only
145277** valid to call this function from within the xCreate() or xConnect() of a
145278** virtual table module.
145279*/
145280SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
145281 VtabCtx *pCtx;
145282 int rc = SQLITE_OK;
145283 Table *pTab;
145284 char *zErr = 0;
145285 Parse sParse;
145286 int initBusy;
145287
145288#ifdef SQLITE_ENABLE_API_ARMOR
145289 if( !sqlite3SafetyCheckOk(db) || zCreateTable==0 ){
145290 return SQLITE_MISUSE_BKPT;
145291 }
145292#endif
145293 sqlite3_mutex_enter(p: db->mutex);
145294 pCtx = db->pVtabCtx;
145295 if( !pCtx || pCtx->bDeclared ){
145296 sqlite3Error(db, SQLITE_MISUSE);
145297 sqlite3_mutex_leave(p: db->mutex);
145298 return SQLITE_MISUSE_BKPT;
145299 }
145300 pTab = pCtx->pTab;
145301 assert( IsVirtual(pTab) );
145302
145303 memset(s: &sParse, c: 0, n: sizeof(sParse));
145304 sParse.eParseMode = PARSE_MODE_DECLARE_VTAB;
145305 sParse.db = db;
145306 /* We should never be able to reach this point while loading the
145307 ** schema. Nevertheless, defend against that (turn off db->init.busy)
145308 ** in case a bug arises. */
145309 assert( db->init.busy==0 );
145310 initBusy = db->init.busy;
145311 db->init.busy = 0;
145312 sParse.nQueryLoop = 1;
145313 if( SQLITE_OK==sqlite3RunParser(&sParse, zCreateTable, &zErr)
145314 && sParse.pNewTable
145315 && !db->mallocFailed
145316 && IsOrdinaryTable(sParse.pNewTable)
145317 ){
145318 if( !pTab->aCol ){
145319 Table *pNew = sParse.pNewTable;
145320 Index *pIdx;
145321 pTab->aCol = pNew->aCol;
145322 sqlite3ExprListDelete(db, pList: pNew->u.tab.pDfltList);
145323 pTab->nNVCol = pTab->nCol = pNew->nCol;
145324 pTab->tabFlags |= pNew->tabFlags & (TF_WithoutRowid|TF_NoVisibleRowid);
145325 pNew->nCol = 0;
145326 pNew->aCol = 0;
145327 assert( pTab->pIndex==0 );
145328 assert( HasRowid(pNew) || sqlite3PrimaryKeyIndex(pNew)!=0 );
145329 if( !HasRowid(pNew)
145330 && pCtx->pVTable->pMod->pModule->xUpdate!=0
145331 && sqlite3PrimaryKeyIndex(pTab: pNew)->nKeyCol!=1
145332 ){
145333 /* WITHOUT ROWID virtual tables must either be read-only (xUpdate==0)
145334 ** or else must have a single-column PRIMARY KEY */
145335 rc = SQLITE_ERROR;
145336 }
145337 pIdx = pNew->pIndex;
145338 if( pIdx ){
145339 assert( pIdx->pNext==0 );
145340 pTab->pIndex = pIdx;
145341 pNew->pIndex = 0;
145342 pIdx->pTable = pTab;
145343 }
145344 }
145345 pCtx->bDeclared = 1;
145346 }else{
145347 sqlite3ErrorWithMsg(db, SQLITE_ERROR, zFormat: (zErr ? "%s" : 0), zErr);
145348 sqlite3DbFree(db, p: zErr);
145349 rc = SQLITE_ERROR;
145350 }
145351 sParse.eParseMode = PARSE_MODE_NORMAL;
145352
145353 if( sParse.pVdbe ){
145354 sqlite3VdbeFinalize(p: sParse.pVdbe);
145355 }
145356 sqlite3DeleteTable(db, pTable: sParse.pNewTable);
145357 sqlite3ParserReset(pParse: &sParse);
145358 db->init.busy = initBusy;
145359
145360 assert( (rc&0xff)==rc );
145361 rc = sqlite3ApiExit(db, rc);
145362 sqlite3_mutex_leave(p: db->mutex);
145363 return rc;
145364}
145365
145366/*
145367** This function is invoked by the vdbe to call the xDestroy method
145368** of the virtual table named zTab in database iDb. This occurs
145369** when a DROP TABLE is mentioned.
145370**
145371** This call is a no-op if zTab is not a virtual table.
145372*/
145373SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){
145374 int rc = SQLITE_OK;
145375 Table *pTab;
145376
145377 pTab = sqlite3FindTable(db, zName: zTab, zDatabase: db->aDb[iDb].zDbSName);
145378 if( ALWAYS(pTab!=0)
145379 && ALWAYS(IsVirtual(pTab))
145380 && ALWAYS(pTab->u.vtab.p!=0)
145381 ){
145382 VTable *p;
145383 int (*xDestroy)(sqlite3_vtab *);
145384 for(p=pTab->u.vtab.p; p; p=p->pNext){
145385 assert( p->pVtab );
145386 if( p->pVtab->nRef>0 ){
145387 return SQLITE_LOCKED;
145388 }
145389 }
145390 p = vtabDisconnectAll(db, p: pTab);
145391 xDestroy = p->pMod->pModule->xDestroy;
145392 if( xDestroy==0 ) xDestroy = p->pMod->pModule->xDisconnect;
145393 assert( xDestroy!=0 );
145394 pTab->nTabRef++;
145395 rc = xDestroy(p->pVtab);
145396 /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
145397 if( rc==SQLITE_OK ){
145398 assert( pTab->u.vtab.p==p && p->pNext==0 );
145399 p->pVtab = 0;
145400 pTab->u.vtab.p = 0;
145401 sqlite3VtabUnlock(pVTab: p);
145402 }
145403 sqlite3DeleteTable(db, pTable: pTab);
145404 }
145405
145406 return rc;
145407}
145408
145409/*
145410** This function invokes either the xRollback or xCommit method
145411** of each of the virtual tables in the sqlite3.aVTrans array. The method
145412** called is identified by the second argument, "offset", which is
145413** the offset of the method to call in the sqlite3_module structure.
145414**
145415** The array is cleared after invoking the callbacks.
145416*/
145417static void callFinaliser(sqlite3 *db, int offset){
145418 int i;
145419 if( db->aVTrans ){
145420 VTable **aVTrans = db->aVTrans;
145421 db->aVTrans = 0;
145422 for(i=0; i<db->nVTrans; i++){
145423 VTable *pVTab = aVTrans[i];
145424 sqlite3_vtab *p = pVTab->pVtab;
145425 if( p ){
145426 int (*x)(sqlite3_vtab *);
145427 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
145428 if( x ) x(p);
145429 }
145430 pVTab->iSavepoint = 0;
145431 sqlite3VtabUnlock(pVTab);
145432 }
145433 sqlite3DbFree(db, p: aVTrans);
145434 db->nVTrans = 0;
145435 }
145436}
145437
145438/*
145439** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
145440** array. Return the error code for the first error that occurs, or
145441** SQLITE_OK if all xSync operations are successful.
145442**
145443** If an error message is available, leave it in p->zErrMsg.
145444*/
145445SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, Vdbe *p){
145446 int i;
145447 int rc = SQLITE_OK;
145448 VTable **aVTrans = db->aVTrans;
145449
145450 db->aVTrans = 0;
145451 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
145452 int (*x)(sqlite3_vtab *);
145453 sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
145454 if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
145455 rc = x(pVtab);
145456 sqlite3VtabImportErrmsg(p, pVtab);
145457 }
145458 }
145459 db->aVTrans = aVTrans;
145460 return rc;
145461}
145462
145463/*
145464** Invoke the xRollback method of all virtual tables in the
145465** sqlite3.aVTrans array. Then clear the array itself.
145466*/
145467SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db){
145468 callFinaliser(db, offsetof(sqlite3_module,xRollback));
145469 return SQLITE_OK;
145470}
145471
145472/*
145473** Invoke the xCommit method of all virtual tables in the
145474** sqlite3.aVTrans array. Then clear the array itself.
145475*/
145476SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db){
145477 callFinaliser(db, offsetof(sqlite3_module,xCommit));
145478 return SQLITE_OK;
145479}
145480
145481/*
145482** If the virtual table pVtab supports the transaction interface
145483** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
145484** not currently open, invoke the xBegin method now.
145485**
145486** If the xBegin call is successful, place the sqlite3_vtab pointer
145487** in the sqlite3.aVTrans array.
145488*/
145489SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){
145490 int rc = SQLITE_OK;
145491 const sqlite3_module *pModule;
145492
145493 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
145494 ** than zero, then this function is being called from within a
145495 ** virtual module xSync() callback. It is illegal to write to
145496 ** virtual module tables in this case, so return SQLITE_LOCKED.
145497 */
145498 if( sqlite3VtabInSync(db) ){
145499 return SQLITE_LOCKED;
145500 }
145501 if( !pVTab ){
145502 return SQLITE_OK;
145503 }
145504 pModule = pVTab->pVtab->pModule;
145505
145506 if( pModule->xBegin ){
145507 int i;
145508
145509 /* If pVtab is already in the aVTrans array, return early */
145510 for(i=0; i<db->nVTrans; i++){
145511 if( db->aVTrans[i]==pVTab ){
145512 return SQLITE_OK;
145513 }
145514 }
145515
145516 /* Invoke the xBegin method. If successful, add the vtab to the
145517 ** sqlite3.aVTrans[] array. */
145518 rc = growVTrans(db);
145519 if( rc==SQLITE_OK ){
145520 rc = pModule->xBegin(pVTab->pVtab);
145521 if( rc==SQLITE_OK ){
145522 int iSvpt = db->nStatement + db->nSavepoint;
145523 addToVTrans(db, pVTab);
145524 if( iSvpt && pModule->xSavepoint ){
145525 pVTab->iSavepoint = iSvpt;
145526 rc = pModule->xSavepoint(pVTab->pVtab, iSvpt-1);
145527 }
145528 }
145529 }
145530 }
145531 return rc;
145532}
145533
145534/*
145535** Invoke either the xSavepoint, xRollbackTo or xRelease method of all
145536** virtual tables that currently have an open transaction. Pass iSavepoint
145537** as the second argument to the virtual table method invoked.
145538**
145539** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is
145540** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is
145541** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with
145542** an open transaction is invoked.
145543**
145544** If any virtual table method returns an error code other than SQLITE_OK,
145545** processing is abandoned and the error returned to the caller of this
145546** function immediately. If all calls to virtual table methods are successful,
145547** SQLITE_OK is returned.
145548*/
145549SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){
145550 int rc = SQLITE_OK;
145551
145552 assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN );
145553 assert( iSavepoint>=-1 );
145554 if( db->aVTrans ){
145555 int i;
145556 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
145557 VTable *pVTab = db->aVTrans[i];
145558 const sqlite3_module *pMod = pVTab->pMod->pModule;
145559 if( pVTab->pVtab && pMod->iVersion>=2 ){
145560 int (*xMethod)(sqlite3_vtab *, int);
145561 sqlite3VtabLock(pVTab);
145562 switch( op ){
145563 case SAVEPOINT_BEGIN:
145564 xMethod = pMod->xSavepoint;
145565 pVTab->iSavepoint = iSavepoint+1;
145566 break;
145567 case SAVEPOINT_ROLLBACK:
145568 xMethod = pMod->xRollbackTo;
145569 break;
145570 default:
145571 xMethod = pMod->xRelease;
145572 break;
145573 }
145574 if( xMethod && pVTab->iSavepoint>iSavepoint ){
145575 rc = xMethod(pVTab->pVtab, iSavepoint);
145576 }
145577 sqlite3VtabUnlock(pVTab);
145578 }
145579 }
145580 }
145581 return rc;
145582}
145583
145584/*
145585** The first parameter (pDef) is a function implementation. The
145586** second parameter (pExpr) is the first argument to this function.
145587** If pExpr is a column in a virtual table, then let the virtual
145588** table implementation have an opportunity to overload the function.
145589**
145590** This routine is used to allow virtual table implementations to
145591** overload MATCH, LIKE, GLOB, and REGEXP operators.
145592**
145593** Return either the pDef argument (indicating no change) or a
145594** new FuncDef structure that is marked as ephemeral using the
145595** SQLITE_FUNC_EPHEM flag.
145596*/
145597SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(
145598 sqlite3 *db, /* Database connection for reporting malloc problems */
145599 FuncDef *pDef, /* Function to possibly overload */
145600 int nArg, /* Number of arguments to the function */
145601 Expr *pExpr /* First argument to the function */
145602){
145603 Table *pTab;
145604 sqlite3_vtab *pVtab;
145605 sqlite3_module *pMod;
145606 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
145607 void *pArg = 0;
145608 FuncDef *pNew;
145609 int rc = 0;
145610
145611 /* Check to see the left operand is a column in a virtual table */
145612 if( NEVER(pExpr==0) ) return pDef;
145613 if( pExpr->op!=TK_COLUMN ) return pDef;
145614 assert( ExprUseYTab(pExpr) );
145615 pTab = pExpr->y.pTab;
145616 if( pTab==0 ) return pDef;
145617 if( !IsVirtual(pTab) ) return pDef;
145618 pVtab = sqlite3GetVTable(db, pTab)->pVtab;
145619 assert( pVtab!=0 );
145620 assert( pVtab->pModule!=0 );
145621 pMod = (sqlite3_module *)pVtab->pModule;
145622 if( pMod->xFindFunction==0 ) return pDef;
145623
145624 /* Call the xFindFunction method on the virtual table implementation
145625 ** to see if the implementation wants to overload this function.
145626 **
145627 ** Though undocumented, we have historically always invoked xFindFunction
145628 ** with an all lower-case function name. Continue in this tradition to
145629 ** avoid any chance of an incompatibility.
145630 */
145631#ifdef SQLITE_DEBUG
145632 {
145633 int i;
145634 for(i=0; pDef->zName[i]; i++){
145635 unsigned char x = (unsigned char)pDef->zName[i];
145636 assert( x==sqlite3UpperToLower[x] );
145637 }
145638 }
145639#endif
145640 rc = pMod->xFindFunction(pVtab, nArg, pDef->zName, &xSFunc, &pArg);
145641 if( rc==0 ){
145642 return pDef;
145643 }
145644
145645 /* Create a new ephemeral function definition for the overloaded
145646 ** function */
145647 pNew = sqlite3DbMallocZero(db, n: sizeof(*pNew)
145648 + sqlite3Strlen30(z: pDef->zName) + 1);
145649 if( pNew==0 ){
145650 return pDef;
145651 }
145652 *pNew = *pDef;
145653 pNew->zName = (const char*)&pNew[1];
145654 memcpy(dest: (char*)&pNew[1], src: pDef->zName, n: sqlite3Strlen30(z: pDef->zName)+1);
145655 pNew->xSFunc = xSFunc;
145656 pNew->pUserData = pArg;
145657 pNew->funcFlags |= SQLITE_FUNC_EPHEM;
145658 return pNew;
145659}
145660
145661/*
145662** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
145663** array so that an OP_VBegin will get generated for it. Add pTab to the
145664** array if it is missing. If pTab is already in the array, this routine
145665** is a no-op.
145666*/
145667SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
145668 Parse *pToplevel = sqlite3ParseToplevel(pParse);
145669 int i, n;
145670 Table **apVtabLock;
145671
145672 assert( IsVirtual(pTab) );
145673 for(i=0; i<pToplevel->nVtabLock; i++){
145674 if( pTab==pToplevel->apVtabLock[i] ) return;
145675 }
145676 n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
145677 apVtabLock = sqlite3Realloc(pOld: pToplevel->apVtabLock, nBytes: n);
145678 if( apVtabLock ){
145679 pToplevel->apVtabLock = apVtabLock;
145680 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
145681 }else{
145682 sqlite3OomFault(db: pToplevel->db);
145683 }
145684}
145685
145686/*
145687** Check to see if virtual table module pMod can be have an eponymous
145688** virtual table instance. If it can, create one if one does not already
145689** exist. Return non-zero if either the eponymous virtual table instance
145690** exists when this routine returns or if an attempt to create it failed
145691** and an error message was left in pParse.
145692**
145693** An eponymous virtual table instance is one that is named after its
145694** module, and more importantly, does not require a CREATE VIRTUAL TABLE
145695** statement in order to come into existance. Eponymous virtual table
145696** instances always exist. They cannot be DROP-ed.
145697**
145698** Any virtual table module for which xConnect and xCreate are the same
145699** method can have an eponymous virtual table instance.
145700*/
145701SQLITE_PRIVATE int sqlite3VtabEponymousTableInit(Parse *pParse, Module *pMod){
145702 const sqlite3_module *pModule = pMod->pModule;
145703 Table *pTab;
145704 char *zErr = 0;
145705 int rc;
145706 sqlite3 *db = pParse->db;
145707 if( pMod->pEpoTab ) return 1;
145708 if( pModule->xCreate!=0 && pModule->xCreate!=pModule->xConnect ) return 0;
145709 pTab = sqlite3DbMallocZero(db, n: sizeof(Table));
145710 if( pTab==0 ) return 0;
145711 pTab->zName = sqlite3DbStrDup(db, z: pMod->zName);
145712 if( pTab->zName==0 ){
145713 sqlite3DbFree(db, p: pTab);
145714 return 0;
145715 }
145716 pMod->pEpoTab = pTab;
145717 pTab->nTabRef = 1;
145718 pTab->eTabType = TABTYP_VTAB;
145719 pTab->pSchema = db->aDb[0].pSchema;
145720 assert( pTab->u.vtab.nArg==0 );
145721 pTab->iPKey = -1;
145722 pTab->tabFlags |= TF_Eponymous;
145723 addModuleArgument(pParse, pTable: pTab, zArg: sqlite3DbStrDup(db, z: pTab->zName));
145724 addModuleArgument(pParse, pTable: pTab, zArg: 0);
145725 addModuleArgument(pParse, pTable: pTab, zArg: sqlite3DbStrDup(db, z: pTab->zName));
145726 rc = vtabCallConstructor(db, pTab, pMod, xConstruct: pModule->xConnect, pzErr: &zErr);
145727 if( rc ){
145728 sqlite3ErrorMsg(pParse, zFormat: "%s", zErr);
145729 sqlite3DbFree(db, p: zErr);
145730 sqlite3VtabEponymousTableClear(db, pMod);
145731 }
145732 return 1;
145733}
145734
145735/*
145736** Erase the eponymous virtual table instance associated with
145737** virtual table module pMod, if it exists.
145738*/
145739SQLITE_PRIVATE void sqlite3VtabEponymousTableClear(sqlite3 *db, Module *pMod){
145740 Table *pTab = pMod->pEpoTab;
145741 if( pTab!=0 ){
145742 /* Mark the table as Ephemeral prior to deleting it, so that the
145743 ** sqlite3DeleteTable() routine will know that it is not stored in
145744 ** the schema. */
145745 pTab->tabFlags |= TF_Ephemeral;
145746 sqlite3DeleteTable(db, pTable: pTab);
145747 pMod->pEpoTab = 0;
145748 }
145749}
145750
145751/*
145752** Return the ON CONFLICT resolution mode in effect for the virtual
145753** table update operation currently in progress.
145754**
145755** The results of this routine are undefined unless it is called from
145756** within an xUpdate method.
145757*/
145758SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *db){
145759 static const unsigned char aMap[] = {
145760 SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE
145761 };
145762#ifdef SQLITE_ENABLE_API_ARMOR
145763 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
145764#endif
145765 assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 );
145766 assert( OE_Ignore==4 && OE_Replace==5 );
145767 assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
145768 return (int)aMap[db->vtabOnConflict-1];
145769}
145770
145771/*
145772** Call from within the xCreate() or xConnect() methods to provide
145773** the SQLite core with additional information about the behavior
145774** of the virtual table being implemented.
145775*/
145776SQLITE_API int sqlite3_vtab_config(sqlite3 *db, int op, ...){
145777 va_list ap;
145778 int rc = SQLITE_OK;
145779 VtabCtx *p;
145780
145781#ifdef SQLITE_ENABLE_API_ARMOR
145782 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
145783#endif
145784 sqlite3_mutex_enter(p: db->mutex);
145785 p = db->pVtabCtx;
145786 if( !p ){
145787 rc = SQLITE_MISUSE_BKPT;
145788 }else{
145789 assert( p->pTab==0 || IsVirtual(p->pTab) );
145790 va_start(ap, op);
145791 switch( op ){
145792 case SQLITE_VTAB_CONSTRAINT_SUPPORT: {
145793 p->pVTable->bConstraint = (u8)va_arg(ap, int);
145794 break;
145795 }
145796 case SQLITE_VTAB_INNOCUOUS: {
145797 p->pVTable->eVtabRisk = SQLITE_VTABRISK_Low;
145798 break;
145799 }
145800 case SQLITE_VTAB_DIRECTONLY: {
145801 p->pVTable->eVtabRisk = SQLITE_VTABRISK_High;
145802 break;
145803 }
145804 default: {
145805 rc = SQLITE_MISUSE_BKPT;
145806 break;
145807 }
145808 }
145809 va_end(ap);
145810 }
145811
145812 if( rc!=SQLITE_OK ) sqlite3Error(db, err_code: rc);
145813 sqlite3_mutex_leave(p: db->mutex);
145814 return rc;
145815}
145816
145817#endif /* SQLITE_OMIT_VIRTUALTABLE */
145818
145819/************** End of vtab.c ************************************************/
145820/************** Begin file wherecode.c ***************************************/
145821/*
145822** 2015-06-06
145823**
145824** The author disclaims copyright to this source code. In place of
145825** a legal notice, here is a blessing:
145826**
145827** May you do good and not evil.
145828** May you find forgiveness for yourself and forgive others.
145829** May you share freely, never taking more than you give.
145830**
145831*************************************************************************
145832** This module contains C code that generates VDBE code used to process
145833** the WHERE clause of SQL statements.
145834**
145835** This file was split off from where.c on 2015-06-06 in order to reduce the
145836** size of where.c and make it easier to edit. This file contains the routines
145837** that actually generate the bulk of the WHERE loop code. The original where.c
145838** file retains the code that does query planning and analysis.
145839*/
145840/* #include "sqliteInt.h" */
145841/************** Include whereInt.h in the middle of wherecode.c **************/
145842/************** Begin file whereInt.h ****************************************/
145843/*
145844** 2013-11-12
145845**
145846** The author disclaims copyright to this source code. In place of
145847** a legal notice, here is a blessing:
145848**
145849** May you do good and not evil.
145850** May you find forgiveness for yourself and forgive others.
145851** May you share freely, never taking more than you give.
145852**
145853*************************************************************************
145854**
145855** This file contains structure and macro definitions for the query
145856** planner logic in "where.c". These definitions are broken out into
145857** a separate source file for easier editing.
145858*/
145859#ifndef SQLITE_WHEREINT_H
145860#define SQLITE_WHEREINT_H
145861
145862
145863/* Forward references
145864*/
145865typedef struct WhereClause WhereClause;
145866typedef struct WhereMaskSet WhereMaskSet;
145867typedef struct WhereOrInfo WhereOrInfo;
145868typedef struct WhereAndInfo WhereAndInfo;
145869typedef struct WhereLevel WhereLevel;
145870typedef struct WhereLoop WhereLoop;
145871typedef struct WherePath WherePath;
145872typedef struct WhereTerm WhereTerm;
145873typedef struct WhereLoopBuilder WhereLoopBuilder;
145874typedef struct WhereScan WhereScan;
145875typedef struct WhereOrCost WhereOrCost;
145876typedef struct WhereOrSet WhereOrSet;
145877
145878/*
145879** This object contains information needed to implement a single nested
145880** loop in WHERE clause.
145881**
145882** Contrast this object with WhereLoop. This object describes the
145883** implementation of the loop. WhereLoop describes the algorithm.
145884** This object contains a pointer to the WhereLoop algorithm as one of
145885** its elements.
145886**
145887** The WhereInfo object contains a single instance of this object for
145888** each term in the FROM clause (which is to say, for each of the
145889** nested loops as implemented). The order of WhereLevel objects determines
145890** the loop nested order, with WhereInfo.a[0] being the outer loop and
145891** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop.
145892*/
145893struct WhereLevel {
145894 int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */
145895 int iTabCur; /* The VDBE cursor used to access the table */
145896 int iIdxCur; /* The VDBE cursor used to access pIdx */
145897 int addrBrk; /* Jump here to break out of the loop */
145898 int addrNxt; /* Jump here to start the next IN combination */
145899 int addrSkip; /* Jump here for next iteration of skip-scan */
145900 int addrCont; /* Jump here to continue with the next loop cycle */
145901 int addrFirst; /* First instruction of interior of the loop */
145902 int addrBody; /* Beginning of the body of this loop */
145903 int regBignull; /* big-null flag reg. True if a NULL-scan is needed */
145904 int addrBignull; /* Jump here for next part of big-null scan */
145905#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
145906 u32 iLikeRepCntr; /* LIKE range processing counter register (times 2) */
145907 int addrLikeRep; /* LIKE range processing address */
145908#endif
145909 u8 iFrom; /* Which entry in the FROM clause */
145910 u8 op, p3, p5; /* Opcode, P3 & P5 of the opcode that ends the loop */
145911 int p1, p2; /* Operands of the opcode used to end the loop */
145912 union { /* Information that depends on pWLoop->wsFlags */
145913 struct {
145914 int nIn; /* Number of entries in aInLoop[] */
145915 struct InLoop {
145916 int iCur; /* The VDBE cursor used by this IN operator */
145917 int addrInTop; /* Top of the IN loop */
145918 int iBase; /* Base register of multi-key index record */
145919 int nPrefix; /* Number of prior entires in the key */
145920 u8 eEndLoopOp; /* IN Loop terminator. OP_Next or OP_Prev */
145921 } *aInLoop; /* Information about each nested IN operator */
145922 } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
145923 Index *pCoveringIdx; /* Possible covering index for WHERE_MULTI_OR */
145924 } u;
145925 struct WhereLoop *pWLoop; /* The selected WhereLoop object */
145926 Bitmask notReady; /* FROM entries not usable at this level */
145927#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
145928 int addrVisit; /* Address at which row is visited */
145929#endif
145930};
145931
145932/*
145933** Each instance of this object represents an algorithm for evaluating one
145934** term of a join. Every term of the FROM clause will have at least
145935** one corresponding WhereLoop object (unless INDEXED BY constraints
145936** prevent a query solution - which is an error) and many terms of the
145937** FROM clause will have multiple WhereLoop objects, each describing a
145938** potential way of implementing that FROM-clause term, together with
145939** dependencies and cost estimates for using the chosen algorithm.
145940**
145941** Query planning consists of building up a collection of these WhereLoop
145942** objects, then computing a particular sequence of WhereLoop objects, with
145943** one WhereLoop object per FROM clause term, that satisfy all dependencies
145944** and that minimize the overall cost.
145945*/
145946struct WhereLoop {
145947 Bitmask prereq; /* Bitmask of other loops that must run first */
145948 Bitmask maskSelf; /* Bitmask identifying table iTab */
145949#ifdef SQLITE_DEBUG
145950 char cId; /* Symbolic ID of this loop for debugging use */
145951#endif
145952 u8 iTab; /* Position in FROM clause of table for this loop */
145953 u8 iSortIdx; /* Sorting index number. 0==None */
145954 LogEst rSetup; /* One-time setup cost (ex: create transient index) */
145955 LogEst rRun; /* Cost of running each loop */
145956 LogEst nOut; /* Estimated number of output rows */
145957 union {
145958 struct { /* Information for internal btree tables */
145959 u16 nEq; /* Number of equality constraints */
145960 u16 nBtm; /* Size of BTM vector */
145961 u16 nTop; /* Size of TOP vector */
145962 u16 nDistinctCol; /* Index columns used to sort for DISTINCT */
145963 Index *pIndex; /* Index used, or NULL */
145964 } btree;
145965 struct { /* Information for virtual tables */
145966 int idxNum; /* Index number */
145967 u8 needFree; /* True if sqlite3_free(idxStr) is needed */
145968 i8 isOrdered; /* True if satisfies ORDER BY */
145969 u16 omitMask; /* Terms that may be omitted */
145970 char *idxStr; /* Index identifier string */
145971 } vtab;
145972 } u;
145973 u32 wsFlags; /* WHERE_* flags describing the plan */
145974 u16 nLTerm; /* Number of entries in aLTerm[] */
145975 u16 nSkip; /* Number of NULL aLTerm[] entries */
145976 /**** whereLoopXfer() copies fields above ***********************/
145977# define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot)
145978 u16 nLSlot; /* Number of slots allocated for aLTerm[] */
145979 WhereTerm **aLTerm; /* WhereTerms used */
145980 WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */
145981 WhereTerm *aLTermSpace[3]; /* Initial aLTerm[] space */
145982};
145983
145984/* This object holds the prerequisites and the cost of running a
145985** subquery on one operand of an OR operator in the WHERE clause.
145986** See WhereOrSet for additional information
145987*/
145988struct WhereOrCost {
145989 Bitmask prereq; /* Prerequisites */
145990 LogEst rRun; /* Cost of running this subquery */
145991 LogEst nOut; /* Number of outputs for this subquery */
145992};
145993
145994/* The WhereOrSet object holds a set of possible WhereOrCosts that
145995** correspond to the subquery(s) of OR-clause processing. Only the
145996** best N_OR_COST elements are retained.
145997*/
145998#define N_OR_COST 3
145999struct WhereOrSet {
146000 u16 n; /* Number of valid a[] entries */
146001 WhereOrCost a[N_OR_COST]; /* Set of best costs */
146002};
146003
146004/*
146005** Each instance of this object holds a sequence of WhereLoop objects
146006** that implement some or all of a query plan.
146007**
146008** Think of each WhereLoop object as a node in a graph with arcs
146009** showing dependencies and costs for travelling between nodes. (That is
146010** not a completely accurate description because WhereLoop costs are a
146011** vector, not a scalar, and because dependencies are many-to-one, not
146012** one-to-one as are graph nodes. But it is a useful visualization aid.)
146013** Then a WherePath object is a path through the graph that visits some
146014** or all of the WhereLoop objects once.
146015**
146016** The "solver" works by creating the N best WherePath objects of length
146017** 1. Then using those as a basis to compute the N best WherePath objects
146018** of length 2. And so forth until the length of WherePaths equals the
146019** number of nodes in the FROM clause. The best (lowest cost) WherePath
146020** at the end is the chosen query plan.
146021*/
146022struct WherePath {
146023 Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */
146024 Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */
146025 LogEst nRow; /* Estimated number of rows generated by this path */
146026 LogEst rCost; /* Total cost of this path */
146027 LogEst rUnsorted; /* Total cost of this path ignoring sorting costs */
146028 i8 isOrdered; /* No. of ORDER BY terms satisfied. -1 for unknown */
146029 WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */
146030};
146031
146032/*
146033** The query generator uses an array of instances of this structure to
146034** help it analyze the subexpressions of the WHERE clause. Each WHERE
146035** clause subexpression is separated from the others by AND operators,
146036** usually, or sometimes subexpressions separated by OR.
146037**
146038** All WhereTerms are collected into a single WhereClause structure.
146039** The following identity holds:
146040**
146041** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
146042**
146043** When a term is of the form:
146044**
146045** X <op> <expr>
146046**
146047** where X is a column name and <op> is one of certain operators,
146048** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the
146049** cursor number and column number for X. WhereTerm.eOperator records
146050** the <op> using a bitmask encoding defined by WO_xxx below. The
146051** use of a bitmask encoding for the operator allows us to search
146052** quickly for terms that match any of several different operators.
146053**
146054** A WhereTerm might also be two or more subterms connected by OR:
146055**
146056** (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR ....
146057**
146058** In this second case, wtFlag has the TERM_ORINFO bit set and eOperator==WO_OR
146059** and the WhereTerm.u.pOrInfo field points to auxiliary information that
146060** is collected about the OR clause.
146061**
146062** If a term in the WHERE clause does not match either of the two previous
146063** categories, then eOperator==0. The WhereTerm.pExpr field is still set
146064** to the original subexpression content and wtFlags is set up appropriately
146065** but no other fields in the WhereTerm object are meaningful.
146066**
146067** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers,
146068** but they do so indirectly. A single WhereMaskSet structure translates
146069** cursor number into bits and the translated bit is stored in the prereq
146070** fields. The translation is used in order to maximize the number of
146071** bits that will fit in a Bitmask. The VDBE cursor numbers might be
146072** spread out over the non-negative integers. For example, the cursor
146073** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45. The WhereMaskSet
146074** translates these sparse cursor numbers into consecutive integers
146075** beginning with 0 in order to make the best possible use of the available
146076** bits in the Bitmask. So, in the example above, the cursor numbers
146077** would be mapped into integers 0 through 7.
146078**
146079** The number of terms in a join is limited by the number of bits
146080** in prereqRight and prereqAll. The default is 64 bits, hence SQLite
146081** is only able to process joins with 64 or fewer tables.
146082*/
146083struct WhereTerm {
146084 Expr *pExpr; /* Pointer to the subexpression that is this term */
146085 WhereClause *pWC; /* The clause this term is part of */
146086 LogEst truthProb; /* Probability of truth for this expression */
146087 u16 wtFlags; /* TERM_xxx bit flags. See below */
146088 u16 eOperator; /* A WO_xx value describing <op> */
146089 u8 nChild; /* Number of children that must disable us */
146090 u8 eMatchOp; /* Op for vtab MATCH/LIKE/GLOB/REGEXP terms */
146091 int iParent; /* Disable pWC->a[iParent] when this term disabled */
146092 int leftCursor; /* Cursor number of X in "X <op> <expr>" */
146093 union {
146094 struct {
146095 int leftColumn; /* Column number of X in "X <op> <expr>" */
146096 int iField; /* Field in (?,?,?) IN (SELECT...) vector */
146097 } x; /* Opcode other than OP_OR or OP_AND */
146098 WhereOrInfo *pOrInfo; /* Extra information if (eOperator & WO_OR)!=0 */
146099 WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */
146100 } u;
146101 Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */
146102 Bitmask prereqAll; /* Bitmask of tables referenced by pExpr */
146103};
146104
146105/*
146106** Allowed values of WhereTerm.wtFlags
146107*/
146108#define TERM_DYNAMIC 0x0001 /* Need to call sqlite3ExprDelete(db, pExpr) */
146109#define TERM_VIRTUAL 0x0002 /* Added by the optimizer. Do not code */
146110#define TERM_CODED 0x0004 /* This term is already coded */
146111#define TERM_COPIED 0x0008 /* Has a child */
146112#define TERM_ORINFO 0x0010 /* Need to free the WhereTerm.u.pOrInfo object */
146113#define TERM_ANDINFO 0x0020 /* Need to free the WhereTerm.u.pAndInfo obj */
146114#define TERM_OR_OK 0x0040 /* Used during OR-clause processing */
146115#define TERM_VNULL 0x0080 /* Manufactured x>NULL or x<=NULL term */
146116#define TERM_LIKEOPT 0x0100 /* Virtual terms from the LIKE optimization */
146117#define TERM_LIKECOND 0x0200 /* Conditionally this LIKE operator term */
146118#define TERM_LIKE 0x0400 /* The original LIKE operator */
146119#define TERM_IS 0x0800 /* Term.pExpr is an IS operator */
146120#define TERM_VARSELECT 0x1000 /* Term.pExpr contains a correlated sub-query */
146121#define TERM_HEURTRUTH 0x2000 /* Heuristic truthProb used */
146122#ifdef SQLITE_ENABLE_STAT4
146123# define TERM_HIGHTRUTH 0x4000 /* Term excludes few rows */
146124#else
146125# define TERM_HIGHTRUTH 0 /* Only used with STAT4 */
146126#endif
146127
146128/*
146129** An instance of the WhereScan object is used as an iterator for locating
146130** terms in the WHERE clause that are useful to the query planner.
146131*/
146132struct WhereScan {
146133 WhereClause *pOrigWC; /* Original, innermost WhereClause */
146134 WhereClause *pWC; /* WhereClause currently being scanned */
146135 const char *zCollName; /* Required collating sequence, if not NULL */
146136 Expr *pIdxExpr; /* Search for this index expression */
146137 char idxaff; /* Must match this affinity, if zCollName!=NULL */
146138 unsigned char nEquiv; /* Number of entries in aiCur[] and aiColumn[] */
146139 unsigned char iEquiv; /* Next unused slot in aiCur[] and aiColumn[] */
146140 u32 opMask; /* Acceptable operators */
146141 int k; /* Resume scanning at this->pWC->a[this->k] */
146142 int aiCur[11]; /* Cursors in the equivalence class */
146143 i16 aiColumn[11]; /* Corresponding column number in the eq-class */
146144};
146145
146146/*
146147** An instance of the following structure holds all information about a
146148** WHERE clause. Mostly this is a container for one or more WhereTerms.
146149**
146150** Explanation of pOuter: For a WHERE clause of the form
146151**
146152** a AND ((b AND c) OR (d AND e)) AND f
146153**
146154** There are separate WhereClause objects for the whole clause and for
146155** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the
146156** subclauses points to the WhereClause object for the whole clause.
146157*/
146158struct WhereClause {
146159 WhereInfo *pWInfo; /* WHERE clause processing context */
146160 WhereClause *pOuter; /* Outer conjunction */
146161 u8 op; /* Split operator. TK_AND or TK_OR */
146162 u8 hasOr; /* True if any a[].eOperator is WO_OR */
146163 int nTerm; /* Number of terms */
146164 int nSlot; /* Number of entries in a[] */
146165 WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */
146166#if defined(SQLITE_SMALL_STACK)
146167 WhereTerm aStatic[1]; /* Initial static space for a[] */
146168#else
146169 WhereTerm aStatic[8]; /* Initial static space for a[] */
146170#endif
146171};
146172
146173/*
146174** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to
146175** a dynamically allocated instance of the following structure.
146176*/
146177struct WhereOrInfo {
146178 WhereClause wc; /* Decomposition into subterms */
146179 Bitmask indexable; /* Bitmask of all indexable tables in the clause */
146180};
146181
146182/*
146183** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to
146184** a dynamically allocated instance of the following structure.
146185*/
146186struct WhereAndInfo {
146187 WhereClause wc; /* The subexpression broken out */
146188};
146189
146190/*
146191** An instance of the following structure keeps track of a mapping
146192** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
146193**
146194** The VDBE cursor numbers are small integers contained in
146195** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE
146196** clause, the cursor numbers might not begin with 0 and they might
146197** contain gaps in the numbering sequence. But we want to make maximum
146198** use of the bits in our bitmasks. This structure provides a mapping
146199** from the sparse cursor numbers into consecutive integers beginning
146200** with 0.
146201**
146202** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
146203** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A.
146204**
146205** For example, if the WHERE clause expression used these VDBE
146206** cursors: 4, 5, 8, 29, 57, 73. Then the WhereMaskSet structure
146207** would map those cursor numbers into bits 0 through 5.
146208**
146209** Note that the mapping is not necessarily ordered. In the example
146210** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0,
146211** 57->5, 73->4. Or one of 719 other combinations might be used. It
146212** does not really matter. What is important is that sparse cursor
146213** numbers all get mapped into bit numbers that begin with 0 and contain
146214** no gaps.
146215*/
146216struct WhereMaskSet {
146217 int bVarSelect; /* Used by sqlite3WhereExprUsage() */
146218 int n; /* Number of assigned cursor values */
146219 int ix[BMS]; /* Cursor assigned to each bit */
146220};
146221
146222/*
146223** Initialize a WhereMaskSet object
146224*/
146225#define initMaskSet(P) (P)->n=0
146226
146227/*
146228** This object is a convenience wrapper holding all information needed
146229** to construct WhereLoop objects for a particular query.
146230*/
146231struct WhereLoopBuilder {
146232 WhereInfo *pWInfo; /* Information about this WHERE */
146233 WhereClause *pWC; /* WHERE clause terms */
146234 ExprList *pOrderBy; /* ORDER BY clause */
146235 WhereLoop *pNew; /* Template WhereLoop */
146236 WhereOrSet *pOrSet; /* Record best loops here, if not NULL */
146237#ifdef SQLITE_ENABLE_STAT4
146238 UnpackedRecord *pRec; /* Probe for stat4 (if required) */
146239 int nRecValid; /* Number of valid fields currently in pRec */
146240#endif
146241 unsigned char bldFlags1; /* First set of SQLITE_BLDF_* flags */
146242 unsigned char bldFlags2; /* Second set of SQLITE_BLDF_* flags */
146243 unsigned int iPlanLimit; /* Search limiter */
146244};
146245
146246/* Allowed values for WhereLoopBuider.bldFlags */
146247#define SQLITE_BLDF1_INDEXED 0x0001 /* An index is used */
146248#define SQLITE_BLDF1_UNIQUE 0x0002 /* All keys of a UNIQUE index used */
146249
146250#define SQLITE_BLDF2_2NDPASS 0x0004 /* Second builder pass needed */
146251
146252/* The WhereLoopBuilder.iPlanLimit is used to limit the number of
146253** index+constraint combinations the query planner will consider for a
146254** particular query. If this parameter is unlimited, then certain
146255** pathological queries can spend excess time in the sqlite3WhereBegin()
146256** routine. The limit is high enough that is should not impact real-world
146257** queries.
146258**
146259** SQLITE_QUERY_PLANNER_LIMIT is the baseline limit. The limit is
146260** increased by SQLITE_QUERY_PLANNER_LIMIT_INCR before each term of the FROM
146261** clause is processed, so that every table in a join is guaranteed to be
146262** able to propose a some index+constraint combinations even if the initial
146263** baseline limit was exhausted by prior tables of the join.
146264*/
146265#ifndef SQLITE_QUERY_PLANNER_LIMIT
146266# define SQLITE_QUERY_PLANNER_LIMIT 20000
146267#endif
146268#ifndef SQLITE_QUERY_PLANNER_LIMIT_INCR
146269# define SQLITE_QUERY_PLANNER_LIMIT_INCR 1000
146270#endif
146271
146272/*
146273** Each instance of this object records a change to a single node
146274** in an expression tree to cause that node to point to a column
146275** of an index rather than an expression or a virtual column. All
146276** such transformations need to be undone at the end of WHERE clause
146277** processing.
146278*/
146279typedef struct WhereExprMod WhereExprMod;
146280struct WhereExprMod {
146281 WhereExprMod *pNext; /* Next translation on a list of them all */
146282 Expr *pExpr; /* The Expr node that was transformed */
146283 Expr orig; /* Original value of the Expr node */
146284};
146285
146286/*
146287** The WHERE clause processing routine has two halves. The
146288** first part does the start of the WHERE loop and the second
146289** half does the tail of the WHERE loop. An instance of
146290** this structure is returned by the first half and passed
146291** into the second half to give some continuity.
146292**
146293** An instance of this object holds the complete state of the query
146294** planner.
146295*/
146296struct WhereInfo {
146297 Parse *pParse; /* Parsing and code generating context */
146298 SrcList *pTabList; /* List of tables in the join */
146299 ExprList *pOrderBy; /* The ORDER BY clause or NULL */
146300 ExprList *pResultSet; /* Result set of the query */
146301 Expr *pWhere; /* The complete WHERE clause */
146302 int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */
146303 int iContinue; /* Jump here to continue with next record */
146304 int iBreak; /* Jump here to break out of the loop */
146305 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
146306 u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
146307 LogEst iLimit; /* LIMIT if wctrlFlags has WHERE_USE_LIMIT */
146308 u8 nLevel; /* Number of nested loop */
146309 i8 nOBSat; /* Number of ORDER BY terms satisfied by indices */
146310 u8 eOnePass; /* ONEPASS_OFF, or _SINGLE, or _MULTI */
146311 u8 eDistinct; /* One of the WHERE_DISTINCT_* values */
146312 unsigned bDeferredSeek :1; /* Uses OP_DeferredSeek */
146313 unsigned untestedTerms :1; /* Not all WHERE terms resolved by outer loop */
146314 unsigned bOrderedInnerLoop:1;/* True if only the inner-most loop is ordered */
146315 unsigned sorted :1; /* True if really sorted (not just grouped) */
146316 LogEst nRowOut; /* Estimated number of output rows */
146317 int iTop; /* The very beginning of the WHERE loop */
146318 int iEndWhere; /* End of the WHERE clause itself */
146319 WhereLoop *pLoops; /* List of all WhereLoop objects */
146320 WhereExprMod *pExprMods; /* Expression modifications */
146321 Bitmask revMask; /* Mask of ORDER BY terms that need reversing */
146322 WhereClause sWC; /* Decomposition of the WHERE clause */
146323 WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */
146324 WhereLevel a[1]; /* Information about each nest loop in WHERE */
146325};
146326
146327/*
146328** Private interfaces - callable only by other where.c routines.
146329**
146330** where.c:
146331*/
146332SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet*,int);
146333#ifdef WHERETRACE_ENABLED
146334SQLITE_PRIVATE void sqlite3WhereClausePrint(WhereClause *pWC);
146335SQLITE_PRIVATE void sqlite3WhereTermPrint(WhereTerm *pTerm, int iTerm);
146336SQLITE_PRIVATE void sqlite3WhereLoopPrint(WhereLoop *p, WhereClause *pWC);
146337#endif
146338SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm(
146339 WhereClause *pWC, /* The WHERE clause to be searched */
146340 int iCur, /* Cursor number of LHS */
146341 int iColumn, /* Column number of LHS */
146342 Bitmask notReady, /* RHS must not overlap with this mask */
146343 u32 op, /* Mask of WO_xx values describing operator */
146344 Index *pIdx /* Must be compatible with this index, if not NULL */
146345);
146346
146347/* wherecode.c: */
146348#ifndef SQLITE_OMIT_EXPLAIN
146349SQLITE_PRIVATE int sqlite3WhereExplainOneScan(
146350 Parse *pParse, /* Parse context */
146351 SrcList *pTabList, /* Table list this loop refers to */
146352 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
146353 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
146354);
146355#else
146356# define sqlite3WhereExplainOneScan(u,v,w,x) 0
146357#endif /* SQLITE_OMIT_EXPLAIN */
146358#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
146359SQLITE_PRIVATE void sqlite3WhereAddScanStatus(
146360 Vdbe *v, /* Vdbe to add scanstatus entry to */
146361 SrcList *pSrclist, /* FROM clause pLvl reads data from */
146362 WhereLevel *pLvl, /* Level to add scanstatus() entry for */
146363 int addrExplain /* Address of OP_Explain (or 0) */
146364);
146365#else
146366# define sqlite3WhereAddScanStatus(a, b, c, d) ((void)d)
146367#endif
146368SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
146369 Parse *pParse, /* Parsing context */
146370 Vdbe *v, /* Prepared statement under construction */
146371 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
146372 int iLevel, /* Which level of pWInfo->a[] should be coded */
146373 WhereLevel *pLevel, /* The current level pointer */
146374 Bitmask notReady /* Which tables are currently available */
146375);
146376
146377/* whereexpr.c: */
146378SQLITE_PRIVATE void sqlite3WhereClauseInit(WhereClause*,WhereInfo*);
146379SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause*);
146380SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause*,Expr*,u8);
146381SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*);
146382SQLITE_PRIVATE Bitmask sqlite3WhereExprUsageNN(WhereMaskSet*, Expr*);
146383SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*);
146384SQLITE_PRIVATE void sqlite3WhereExprAnalyze(SrcList*, WhereClause*);
146385SQLITE_PRIVATE void sqlite3WhereTabFuncArgs(Parse*, SrcItem*, WhereClause*);
146386
146387
146388
146389
146390
146391/*
146392** Bitmasks for the operators on WhereTerm objects. These are all
146393** operators that are of interest to the query planner. An
146394** OR-ed combination of these values can be used when searching for
146395** particular WhereTerms within a WhereClause.
146396**
146397** Value constraints:
146398** WO_EQ == SQLITE_INDEX_CONSTRAINT_EQ
146399** WO_LT == SQLITE_INDEX_CONSTRAINT_LT
146400** WO_LE == SQLITE_INDEX_CONSTRAINT_LE
146401** WO_GT == SQLITE_INDEX_CONSTRAINT_GT
146402** WO_GE == SQLITE_INDEX_CONSTRAINT_GE
146403*/
146404#define WO_IN 0x0001
146405#define WO_EQ 0x0002
146406#define WO_LT (WO_EQ<<(TK_LT-TK_EQ))
146407#define WO_LE (WO_EQ<<(TK_LE-TK_EQ))
146408#define WO_GT (WO_EQ<<(TK_GT-TK_EQ))
146409#define WO_GE (WO_EQ<<(TK_GE-TK_EQ))
146410#define WO_AUX 0x0040 /* Op useful to virtual tables only */
146411#define WO_IS 0x0080
146412#define WO_ISNULL 0x0100
146413#define WO_OR 0x0200 /* Two or more OR-connected terms */
146414#define WO_AND 0x0400 /* Two or more AND-connected terms */
146415#define WO_EQUIV 0x0800 /* Of the form A==B, both columns */
146416#define WO_NOOP 0x1000 /* This term does not restrict search space */
146417
146418#define WO_ALL 0x1fff /* Mask of all possible WO_* values */
146419#define WO_SINGLE 0x01ff /* Mask of all non-compound WO_* values */
146420
146421/*
146422** These are definitions of bits in the WhereLoop.wsFlags field.
146423** The particular combination of bits in each WhereLoop help to
146424** determine the algorithm that WhereLoop represents.
146425*/
146426#define WHERE_COLUMN_EQ 0x00000001 /* x=EXPR */
146427#define WHERE_COLUMN_RANGE 0x00000002 /* x<EXPR and/or x>EXPR */
146428#define WHERE_COLUMN_IN 0x00000004 /* x IN (...) */
146429#define WHERE_COLUMN_NULL 0x00000008 /* x IS NULL */
146430#define WHERE_CONSTRAINT 0x0000000f /* Any of the WHERE_COLUMN_xxx values */
146431#define WHERE_TOP_LIMIT 0x00000010 /* x<EXPR or x<=EXPR constraint */
146432#define WHERE_BTM_LIMIT 0x00000020 /* x>EXPR or x>=EXPR constraint */
146433#define WHERE_BOTH_LIMIT 0x00000030 /* Both x>EXPR and x<EXPR */
146434#define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */
146435#define WHERE_IPK 0x00000100 /* x is the INTEGER PRIMARY KEY */
146436#define WHERE_INDEXED 0x00000200 /* WhereLoop.u.btree.pIndex is valid */
146437#define WHERE_VIRTUALTABLE 0x00000400 /* WhereLoop.u.vtab is valid */
146438#define WHERE_IN_ABLE 0x00000800 /* Able to support an IN operator */
146439#define WHERE_ONEROW 0x00001000 /* Selects no more than one row */
146440#define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */
146441#define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */
146442#define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */
146443#define WHERE_UNQ_WANTED 0x00010000 /* WHERE_ONEROW would have been helpful*/
146444#define WHERE_PARTIALIDX 0x00020000 /* The automatic index is partial */
146445#define WHERE_IN_EARLYOUT 0x00040000 /* Perhaps quit IN loops early */
146446#define WHERE_BIGNULL_SORT 0x00080000 /* Column nEq of index is BIGNULL */
146447#define WHERE_IN_SEEKSCAN 0x00100000 /* Seek-scan optimization for IN */
146448#define WHERE_TRANSCONS 0x00200000 /* Uses a transitive constraint */
146449
146450#endif /* !defined(SQLITE_WHEREINT_H) */
146451
146452/************** End of whereInt.h ********************************************/
146453/************** Continuing where we left off in wherecode.c ******************/
146454
146455#ifndef SQLITE_OMIT_EXPLAIN
146456
146457/*
146458** Return the name of the i-th column of the pIdx index.
146459*/
146460static const char *explainIndexColumnName(Index *pIdx, int i){
146461 i = pIdx->aiColumn[i];
146462 if( i==XN_EXPR ) return "<expr>";
146463 if( i==XN_ROWID ) return "rowid";
146464 return pIdx->pTable->aCol[i].zCnName;
146465}
146466
146467/*
146468** This routine is a helper for explainIndexRange() below
146469**
146470** pStr holds the text of an expression that we are building up one term
146471** at a time. This routine adds a new term to the end of the expression.
146472** Terms are separated by AND so add the "AND" text for second and subsequent
146473** terms only.
146474*/
146475static void explainAppendTerm(
146476 StrAccum *pStr, /* The text expression being built */
146477 Index *pIdx, /* Index to read column names from */
146478 int nTerm, /* Number of terms */
146479 int iTerm, /* Zero-based index of first term. */
146480 int bAnd, /* Non-zero to append " AND " */
146481 const char *zOp /* Name of the operator */
146482){
146483 int i;
146484
146485 assert( nTerm>=1 );
146486 if( bAnd ) sqlite3_str_append(p: pStr, z: " AND ", N: 5);
146487
146488 if( nTerm>1 ) sqlite3_str_append(p: pStr, z: "(", N: 1);
146489 for(i=0; i<nTerm; i++){
146490 if( i ) sqlite3_str_append(p: pStr, z: ",", N: 1);
146491 sqlite3_str_appendall(p: pStr, z: explainIndexColumnName(pIdx, i: iTerm+i));
146492 }
146493 if( nTerm>1 ) sqlite3_str_append(p: pStr, z: ")", N: 1);
146494
146495 sqlite3_str_append(p: pStr, z: zOp, N: 1);
146496
146497 if( nTerm>1 ) sqlite3_str_append(p: pStr, z: "(", N: 1);
146498 for(i=0; i<nTerm; i++){
146499 if( i ) sqlite3_str_append(p: pStr, z: ",", N: 1);
146500 sqlite3_str_append(p: pStr, z: "?", N: 1);
146501 }
146502 if( nTerm>1 ) sqlite3_str_append(p: pStr, z: ")", N: 1);
146503}
146504
146505/*
146506** Argument pLevel describes a strategy for scanning table pTab. This
146507** function appends text to pStr that describes the subset of table
146508** rows scanned by the strategy in the form of an SQL expression.
146509**
146510** For example, if the query:
146511**
146512** SELECT * FROM t1 WHERE a=1 AND b>2;
146513**
146514** is run and there is an index on (a, b), then this function returns a
146515** string similar to:
146516**
146517** "a=? AND b>?"
146518*/
146519static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop){
146520 Index *pIndex = pLoop->u.btree.pIndex;
146521 u16 nEq = pLoop->u.btree.nEq;
146522 u16 nSkip = pLoop->nSkip;
146523 int i, j;
146524
146525 if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
146526 sqlite3_str_append(p: pStr, z: " (", N: 2);
146527 for(i=0; i<nEq; i++){
146528 const char *z = explainIndexColumnName(pIdx: pIndex, i);
146529 if( i ) sqlite3_str_append(p: pStr, z: " AND ", N: 5);
146530 sqlite3_str_appendf(p: pStr, zFormat: i>=nSkip ? "%s=?" : "ANY(%s)", z);
146531 }
146532
146533 j = i;
146534 if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
146535 explainAppendTerm(pStr, pIdx: pIndex, nTerm: pLoop->u.btree.nBtm, iTerm: j, bAnd: i, zOp: ">");
146536 i = 1;
146537 }
146538 if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
146539 explainAppendTerm(pStr, pIdx: pIndex, nTerm: pLoop->u.btree.nTop, iTerm: j, bAnd: i, zOp: "<");
146540 }
146541 sqlite3_str_append(p: pStr, z: ")", N: 1);
146542}
146543
146544/*
146545** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
146546** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was
146547** defined at compile-time. If it is not a no-op, a single OP_Explain opcode
146548** is added to the output to describe the table scan strategy in pLevel.
146549**
146550** If an OP_Explain opcode is added to the VM, its address is returned.
146551** Otherwise, if no OP_Explain is coded, zero is returned.
146552*/
146553SQLITE_PRIVATE int sqlite3WhereExplainOneScan(
146554 Parse *pParse, /* Parse context */
146555 SrcList *pTabList, /* Table list this loop refers to */
146556 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
146557 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
146558){
146559 int ret = 0;
146560#if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
146561 if( sqlite3ParseToplevel(pParse)->explain==2 )
146562#endif
146563 {
146564 SrcItem *pItem = &pTabList->a[pLevel->iFrom];
146565 Vdbe *v = pParse->pVdbe; /* VM being constructed */
146566 sqlite3 *db = pParse->db; /* Database handle */
146567 int isSearch; /* True for a SEARCH. False for SCAN. */
146568 WhereLoop *pLoop; /* The controlling WhereLoop object */
146569 u32 flags; /* Flags that describe this loop */
146570 char *zMsg; /* Text to add to EQP output */
146571 StrAccum str; /* EQP output string */
146572 char zBuf[100]; /* Initial space for EQP output string */
146573
146574 pLoop = pLevel->pWLoop;
146575 flags = pLoop->wsFlags;
146576 if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_OR_SUBCLAUSE) ) return 0;
146577
146578 isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
146579 || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
146580 || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
146581
146582 sqlite3StrAccumInit(p: &str, db, zBase: zBuf, n: sizeof(zBuf), SQLITE_MAX_LENGTH);
146583 str.printfFlags = SQLITE_PRINTF_INTERNAL;
146584 sqlite3_str_appendf(p: &str, zFormat: "%s %S", isSearch ? "SEARCH" : "SCAN", pItem);
146585 if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){
146586 const char *zFmt = 0;
146587 Index *pIdx;
146588
146589 assert( pLoop->u.btree.pIndex!=0 );
146590 pIdx = pLoop->u.btree.pIndex;
146591 assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) );
146592 if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
146593 if( isSearch ){
146594 zFmt = "PRIMARY KEY";
146595 }
146596 }else if( flags & WHERE_PARTIALIDX ){
146597 zFmt = "AUTOMATIC PARTIAL COVERING INDEX";
146598 }else if( flags & WHERE_AUTO_INDEX ){
146599 zFmt = "AUTOMATIC COVERING INDEX";
146600 }else if( flags & WHERE_IDX_ONLY ){
146601 zFmt = "COVERING INDEX %s";
146602 }else{
146603 zFmt = "INDEX %s";
146604 }
146605 if( zFmt ){
146606 sqlite3_str_append(p: &str, z: " USING ", N: 7);
146607 sqlite3_str_appendf(p: &str, zFormat: zFmt, pIdx->zName);
146608 explainIndexRange(pStr: &str, pLoop);
146609 }
146610 }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
146611 const char *zRangeOp;
146612 if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
146613 zRangeOp = "=";
146614 }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
146615 zRangeOp = ">? AND rowid<";
146616 }else if( flags&WHERE_BTM_LIMIT ){
146617 zRangeOp = ">";
146618 }else{
146619 assert( flags&WHERE_TOP_LIMIT);
146620 zRangeOp = "<";
146621 }
146622 sqlite3_str_appendf(p: &str,
146623 zFormat: " USING INTEGER PRIMARY KEY (rowid%s?)",zRangeOp);
146624 }
146625#ifndef SQLITE_OMIT_VIRTUALTABLE
146626 else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
146627 sqlite3_str_appendf(p: &str, zFormat: " VIRTUAL TABLE INDEX %d:%s",
146628 pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
146629 }
146630#endif
146631#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
146632 if( pLoop->nOut>=10 ){
146633 sqlite3_str_appendf(&str, " (~%llu rows)",
146634 sqlite3LogEstToInt(pLoop->nOut));
146635 }else{
146636 sqlite3_str_append(&str, " (~1 row)", 9);
146637 }
146638#endif
146639 zMsg = sqlite3StrAccumFinish(p: &str);
146640 sqlite3ExplainBreakpoint("",zMsg);
146641 ret = sqlite3VdbeAddOp4(p: v, OP_Explain, p1: sqlite3VdbeCurrentAddr(p: v),
146642 p2: pParse->addrExplain, p3: 0, zP4: zMsg,P4_DYNAMIC);
146643 }
146644 return ret;
146645}
146646#endif /* SQLITE_OMIT_EXPLAIN */
146647
146648#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
146649/*
146650** Configure the VM passed as the first argument with an
146651** sqlite3_stmt_scanstatus() entry corresponding to the scan used to
146652** implement level pLvl. Argument pSrclist is a pointer to the FROM
146653** clause that the scan reads data from.
146654**
146655** If argument addrExplain is not 0, it must be the address of an
146656** OP_Explain instruction that describes the same loop.
146657*/
146658SQLITE_PRIVATE void sqlite3WhereAddScanStatus(
146659 Vdbe *v, /* Vdbe to add scanstatus entry to */
146660 SrcList *pSrclist, /* FROM clause pLvl reads data from */
146661 WhereLevel *pLvl, /* Level to add scanstatus() entry for */
146662 int addrExplain /* Address of OP_Explain (or 0) */
146663){
146664 const char *zObj = 0;
146665 WhereLoop *pLoop = pLvl->pWLoop;
146666 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){
146667 zObj = pLoop->u.btree.pIndex->zName;
146668 }else{
146669 zObj = pSrclist->a[pLvl->iFrom].zName;
146670 }
146671 sqlite3VdbeScanStatus(
146672 v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
146673 );
146674}
146675#endif
146676
146677
146678/*
146679** Disable a term in the WHERE clause. Except, do not disable the term
146680** if it controls a LEFT OUTER JOIN and it did not originate in the ON
146681** or USING clause of that join.
146682**
146683** Consider the term t2.z='ok' in the following queries:
146684**
146685** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
146686** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
146687** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
146688**
146689** The t2.z='ok' is disabled in the in (2) because it originates
146690** in the ON clause. The term is disabled in (3) because it is not part
146691** of a LEFT OUTER JOIN. In (1), the term is not disabled.
146692**
146693** Disabling a term causes that term to not be tested in the inner loop
146694** of the join. Disabling is an optimization. When terms are satisfied
146695** by indices, we disable them to prevent redundant tests in the inner
146696** loop. We would get the correct results if nothing were ever disabled,
146697** but joins might run a little slower. The trick is to disable as much
146698** as we can without disabling too much. If we disabled in (1), we'd get
146699** the wrong answer. See ticket #813.
146700**
146701** If all the children of a term are disabled, then that term is also
146702** automatically disabled. In this way, terms get disabled if derived
146703** virtual terms are tested first. For example:
146704**
146705** x GLOB 'abc*' AND x>='abc' AND x<'acd'
146706** \___________/ \______/ \_____/
146707** parent child1 child2
146708**
146709** Only the parent term was in the original WHERE clause. The child1
146710** and child2 terms were added by the LIKE optimization. If both of
146711** the virtual child terms are valid, then testing of the parent can be
146712** skipped.
146713**
146714** Usually the parent term is marked as TERM_CODED. But if the parent
146715** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead.
146716** The TERM_LIKECOND marking indicates that the term should be coded inside
146717** a conditional such that is only evaluated on the second pass of a
146718** LIKE-optimization loop, when scanning BLOBs instead of strings.
146719*/
146720static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
146721 int nLoop = 0;
146722 assert( pTerm!=0 );
146723 while( (pTerm->wtFlags & TERM_CODED)==0
146724 && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
146725 && (pLevel->notReady & pTerm->prereqAll)==0
146726 ){
146727 if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
146728 pTerm->wtFlags |= TERM_LIKECOND;
146729 }else{
146730 pTerm->wtFlags |= TERM_CODED;
146731 }
146732#ifdef WHERETRACE_ENABLED
146733 if( sqlite3WhereTrace & 0x20000 ){
146734 sqlite3DebugPrintf("DISABLE-");
146735 sqlite3WhereTermPrint(pTerm, (int)(pTerm - (pTerm->pWC->a)));
146736 }
146737#endif
146738 if( pTerm->iParent<0 ) break;
146739 pTerm = &pTerm->pWC->a[pTerm->iParent];
146740 assert( pTerm!=0 );
146741 pTerm->nChild--;
146742 if( pTerm->nChild!=0 ) break;
146743 nLoop++;
146744 }
146745}
146746
146747/*
146748** Code an OP_Affinity opcode to apply the column affinity string zAff
146749** to the n registers starting at base.
146750**
146751** As an optimization, SQLITE_AFF_BLOB and SQLITE_AFF_NONE entries (which
146752** are no-ops) at the beginning and end of zAff are ignored. If all entries
146753** in zAff are SQLITE_AFF_BLOB or SQLITE_AFF_NONE, then no code gets generated.
146754**
146755** This routine makes its own copy of zAff so that the caller is free
146756** to modify zAff after this routine returns.
146757*/
146758static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
146759 Vdbe *v = pParse->pVdbe;
146760 if( zAff==0 ){
146761 assert( pParse->db->mallocFailed );
146762 return;
146763 }
146764 assert( v!=0 );
146765
146766 /* Adjust base and n to skip over SQLITE_AFF_BLOB and SQLITE_AFF_NONE
146767 ** entries at the beginning and end of the affinity string.
146768 */
146769 assert( SQLITE_AFF_NONE<SQLITE_AFF_BLOB );
146770 while( n>0 && zAff[0]<=SQLITE_AFF_BLOB ){
146771 n--;
146772 base++;
146773 zAff++;
146774 }
146775 while( n>1 && zAff[n-1]<=SQLITE_AFF_BLOB ){
146776 n--;
146777 }
146778
146779 /* Code the OP_Affinity opcode if there is anything left to do. */
146780 if( n>0 ){
146781 sqlite3VdbeAddOp4(p: v, OP_Affinity, p1: base, p2: n, p3: 0, zP4: zAff, p4type: n);
146782 }
146783}
146784
146785/*
146786** Expression pRight, which is the RHS of a comparison operation, is
146787** either a vector of n elements or, if n==1, a scalar expression.
146788** Before the comparison operation, affinity zAff is to be applied
146789** to the pRight values. This function modifies characters within the
146790** affinity string to SQLITE_AFF_BLOB if either:
146791**
146792** * the comparison will be performed with no affinity, or
146793** * the affinity change in zAff is guaranteed not to change the value.
146794*/
146795static void updateRangeAffinityStr(
146796 Expr *pRight, /* RHS of comparison */
146797 int n, /* Number of vector elements in comparison */
146798 char *zAff /* Affinity string to modify */
146799){
146800 int i;
146801 for(i=0; i<n; i++){
146802 Expr *p = sqlite3VectorFieldSubexpr(pVector: pRight, i);
146803 if( sqlite3CompareAffinity(pExpr: p, aff2: zAff[i])==SQLITE_AFF_BLOB
146804 || sqlite3ExprNeedsNoAffinityChange(p, aff: zAff[i])
146805 ){
146806 zAff[i] = SQLITE_AFF_BLOB;
146807 }
146808 }
146809}
146810
146811
146812/*
146813** pX is an expression of the form: (vector) IN (SELECT ...)
146814** In other words, it is a vector IN operator with a SELECT clause on the
146815** LHS. But not all terms in the vector are indexable and the terms might
146816** not be in the correct order for indexing.
146817**
146818** This routine makes a copy of the input pX expression and then adjusts
146819** the vector on the LHS with corresponding changes to the SELECT so that
146820** the vector contains only index terms and those terms are in the correct
146821** order. The modified IN expression is returned. The caller is responsible
146822** for deleting the returned expression.
146823**
146824** Example:
146825**
146826** CREATE TABLE t1(a,b,c,d,e,f);
146827** CREATE INDEX t1x1 ON t1(e,c);
146828** SELECT * FROM t1 WHERE (a,b,c,d,e) IN (SELECT v,w,x,y,z FROM t2)
146829** \_______________________________________/
146830** The pX expression
146831**
146832** Since only columns e and c can be used with the index, in that order,
146833** the modified IN expression that is returned will be:
146834**
146835** (e,c) IN (SELECT z,x FROM t2)
146836**
146837** The reduced pX is different from the original (obviously) and thus is
146838** only used for indexing, to improve performance. The original unaltered
146839** IN expression must also be run on each output row for correctness.
146840*/
146841static Expr *removeUnindexableInClauseTerms(
146842 Parse *pParse, /* The parsing context */
146843 int iEq, /* Look at loop terms starting here */
146844 WhereLoop *pLoop, /* The current loop */
146845 Expr *pX /* The IN expression to be reduced */
146846){
146847 sqlite3 *db = pParse->db;
146848 Expr *pNew;
146849 pNew = sqlite3ExprDup(db, p: pX, flags: 0);
146850 if( db->mallocFailed==0 ){
146851 ExprList *pOrigRhs; /* Original unmodified RHS */
146852 ExprList *pOrigLhs; /* Original unmodified LHS */
146853 ExprList *pRhs = 0; /* New RHS after modifications */
146854 ExprList *pLhs = 0; /* New LHS after mods */
146855 int i; /* Loop counter */
146856 Select *pSelect; /* Pointer to the SELECT on the RHS */
146857
146858 assert( ExprUseXSelect(pNew) );
146859 pOrigRhs = pNew->x.pSelect->pEList;
146860 assert( pNew->pLeft!=0 );
146861 assert( ExprUseXList(pNew->pLeft) );
146862 pOrigLhs = pNew->pLeft->x.pList;
146863 for(i=iEq; i<pLoop->nLTerm; i++){
146864 if( pLoop->aLTerm[i]->pExpr==pX ){
146865 int iField;
146866 assert( (pLoop->aLTerm[i]->eOperator & (WO_OR|WO_AND))==0 );
146867 iField = pLoop->aLTerm[i]->u.x.iField - 1;
146868 if( pOrigRhs->a[iField].pExpr==0 ) continue; /* Duplicate PK column */
146869 pRhs = sqlite3ExprListAppend(pParse, pList: pRhs, pExpr: pOrigRhs->a[iField].pExpr);
146870 pOrigRhs->a[iField].pExpr = 0;
146871 assert( pOrigLhs->a[iField].pExpr!=0 );
146872 pLhs = sqlite3ExprListAppend(pParse, pList: pLhs, pExpr: pOrigLhs->a[iField].pExpr);
146873 pOrigLhs->a[iField].pExpr = 0;
146874 }
146875 }
146876 sqlite3ExprListDelete(db, pList: pOrigRhs);
146877 sqlite3ExprListDelete(db, pList: pOrigLhs);
146878 pNew->pLeft->x.pList = pLhs;
146879 pNew->x.pSelect->pEList = pRhs;
146880 if( pLhs && pLhs->nExpr==1 ){
146881 /* Take care here not to generate a TK_VECTOR containing only a
146882 ** single value. Since the parser never creates such a vector, some
146883 ** of the subroutines do not handle this case. */
146884 Expr *p = pLhs->a[0].pExpr;
146885 pLhs->a[0].pExpr = 0;
146886 sqlite3ExprDelete(db, p: pNew->pLeft);
146887 pNew->pLeft = p;
146888 }
146889 pSelect = pNew->x.pSelect;
146890 if( pSelect->pOrderBy ){
146891 /* If the SELECT statement has an ORDER BY clause, zero the
146892 ** iOrderByCol variables. These are set to non-zero when an
146893 ** ORDER BY term exactly matches one of the terms of the
146894 ** result-set. Since the result-set of the SELECT statement may
146895 ** have been modified or reordered, these variables are no longer
146896 ** set correctly. Since setting them is just an optimization,
146897 ** it's easiest just to zero them here. */
146898 ExprList *pOrderBy = pSelect->pOrderBy;
146899 for(i=0; i<pOrderBy->nExpr; i++){
146900 pOrderBy->a[i].u.x.iOrderByCol = 0;
146901 }
146902 }
146903
146904#if 0
146905 printf("For indexing, change the IN expr:\n");
146906 sqlite3TreeViewExpr(0, pX, 0);
146907 printf("Into:\n");
146908 sqlite3TreeViewExpr(0, pNew, 0);
146909#endif
146910 }
146911 return pNew;
146912}
146913
146914
146915/*
146916** Generate code for a single equality term of the WHERE clause. An equality
146917** term can be either X=expr or X IN (...). pTerm is the term to be
146918** coded.
146919**
146920** The current value for the constraint is left in a register, the index
146921** of which is returned. An attempt is made store the result in iTarget but
146922** this is only guaranteed for TK_ISNULL and TK_IN constraints. If the
146923** constraint is a TK_EQ or TK_IS, then the current value might be left in
146924** some other register and it is the caller's responsibility to compensate.
146925**
146926** For a constraint of the form X=expr, the expression is evaluated in
146927** straight-line code. For constraints of the form X IN (...)
146928** this routine sets up a loop that will iterate over all values of X.
146929*/
146930static int codeEqualityTerm(
146931 Parse *pParse, /* The parsing context */
146932 WhereTerm *pTerm, /* The term of the WHERE clause to be coded */
146933 WhereLevel *pLevel, /* The level of the FROM clause we are working on */
146934 int iEq, /* Index of the equality term within this level */
146935 int bRev, /* True for reverse-order IN operations */
146936 int iTarget /* Attempt to leave results in this register */
146937){
146938 Expr *pX = pTerm->pExpr;
146939 Vdbe *v = pParse->pVdbe;
146940 int iReg; /* Register holding results */
146941
146942 assert( pLevel->pWLoop->aLTerm[iEq]==pTerm );
146943 assert( iTarget>0 );
146944 if( pX->op==TK_EQ || pX->op==TK_IS ){
146945 iReg = sqlite3ExprCodeTarget(pParse, pExpr: pX->pRight, target: iTarget);
146946 }else if( pX->op==TK_ISNULL ){
146947 iReg = iTarget;
146948 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: iReg);
146949#ifndef SQLITE_OMIT_SUBQUERY
146950 }else{
146951 int eType = IN_INDEX_NOOP;
146952 int iTab;
146953 struct InLoop *pIn;
146954 WhereLoop *pLoop = pLevel->pWLoop;
146955 int i;
146956 int nEq = 0;
146957 int *aiMap = 0;
146958
146959 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
146960 && pLoop->u.btree.pIndex!=0
146961 && pLoop->u.btree.pIndex->aSortOrder[iEq]
146962 ){
146963 testcase( iEq==0 );
146964 testcase( bRev );
146965 bRev = !bRev;
146966 }
146967 assert( pX->op==TK_IN );
146968 iReg = iTarget;
146969
146970 for(i=0; i<iEq; i++){
146971 if( pLoop->aLTerm[i] && pLoop->aLTerm[i]->pExpr==pX ){
146972 disableTerm(pLevel, pTerm);
146973 return iTarget;
146974 }
146975 }
146976 for(i=iEq;i<pLoop->nLTerm; i++){
146977 assert( pLoop->aLTerm[i]!=0 );
146978 if( pLoop->aLTerm[i]->pExpr==pX ) nEq++;
146979 }
146980
146981 iTab = 0;
146982 if( !ExprUseXSelect(pX) || pX->x.pSelect->pEList->nExpr==1 ){
146983 eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, prRhsHasNull: 0, aiMap: 0, piTab: &iTab);
146984 }else{
146985 sqlite3 *db = pParse->db;
146986 pX = removeUnindexableInClauseTerms(pParse, iEq, pLoop, pX);
146987
146988 if( !db->mallocFailed ){
146989 aiMap = (int*)sqlite3DbMallocZero(db: pParse->db, n: sizeof(int)*nEq);
146990 eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, prRhsHasNull: 0, aiMap, piTab: &iTab);
146991 pTerm->pExpr->iTable = iTab;
146992 }
146993 sqlite3ExprDelete(db, p: pX);
146994 pX = pTerm->pExpr;
146995 }
146996
146997 if( eType==IN_INDEX_INDEX_DESC ){
146998 testcase( bRev );
146999 bRev = !bRev;
147000 }
147001 sqlite3VdbeAddOp2(p: v, op: bRev ? OP_Last : OP_Rewind, p1: iTab, p2: 0);
147002 VdbeCoverageIf(v, bRev);
147003 VdbeCoverageIf(v, !bRev);
147004
147005 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
147006 pLoop->wsFlags |= WHERE_IN_ABLE;
147007 if( pLevel->u.in.nIn==0 ){
147008 pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
147009 }
147010 if( iEq>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0 ){
147011 pLoop->wsFlags |= WHERE_IN_EARLYOUT;
147012 }
147013
147014 i = pLevel->u.in.nIn;
147015 pLevel->u.in.nIn += nEq;
147016 pLevel->u.in.aInLoop =
147017 sqlite3DbReallocOrFree(db: pParse->db, p: pLevel->u.in.aInLoop,
147018 n: sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
147019 pIn = pLevel->u.in.aInLoop;
147020 if( pIn ){
147021 int iMap = 0; /* Index in aiMap[] */
147022 pIn += i;
147023 for(i=iEq;i<pLoop->nLTerm; i++){
147024 if( pLoop->aLTerm[i]->pExpr==pX ){
147025 int iOut = iReg + i - iEq;
147026 if( eType==IN_INDEX_ROWID ){
147027 pIn->addrInTop = sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: iTab, p2: iOut);
147028 }else{
147029 int iCol = aiMap ? aiMap[iMap++] : 0;
147030 pIn->addrInTop = sqlite3VdbeAddOp3(p: v,OP_Column,p1: iTab, p2: iCol, p3: iOut);
147031 }
147032 sqlite3VdbeAddOp1(p: v, OP_IsNull, p1: iOut); VdbeCoverage(v);
147033 if( i==iEq ){
147034 pIn->iCur = iTab;
147035 pIn->eEndLoopOp = bRev ? OP_Prev : OP_Next;
147036 if( iEq>0 ){
147037 pIn->iBase = iReg - i;
147038 pIn->nPrefix = i;
147039 }else{
147040 pIn->nPrefix = 0;
147041 }
147042 }else{
147043 pIn->eEndLoopOp = OP_Noop;
147044 }
147045 pIn++;
147046 }
147047 }
147048 testcase( iEq>0
147049 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0
147050 && (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 );
147051 if( iEq>0
147052 && (pLoop->wsFlags & (WHERE_IN_SEEKSCAN|WHERE_VIRTUALTABLE))==0
147053 ){
147054 sqlite3VdbeAddOp3(p: v, OP_SeekHit, p1: pLevel->iIdxCur, p2: 0, p3: iEq);
147055 }
147056 }else{
147057 pLevel->u.in.nIn = 0;
147058 }
147059 sqlite3DbFree(db: pParse->db, p: aiMap);
147060#endif
147061 }
147062
147063 /* As an optimization, try to disable the WHERE clause term that is
147064 ** driving the index as it will always be true. The correct answer is
147065 ** obtained regardless, but we might get the answer with fewer CPU cycles
147066 ** by omitting the term.
147067 **
147068 ** But do not disable the term unless we are certain that the term is
147069 ** not a transitive constraint. For an example of where that does not
147070 ** work, see https://sqlite.org/forum/forumpost/eb8613976a (2021-05-04)
147071 */
147072 if( (pLevel->pWLoop->wsFlags & WHERE_TRANSCONS)==0
147073 || (pTerm->eOperator & WO_EQUIV)==0
147074 ){
147075 disableTerm(pLevel, pTerm);
147076 }
147077
147078 return iReg;
147079}
147080
147081/*
147082** Generate code that will evaluate all == and IN constraints for an
147083** index scan.
147084**
147085** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
147086** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10
147087** The index has as many as three equality constraints, but in this
147088** example, the third "c" value is an inequality. So only two
147089** constraints are coded. This routine will generate code to evaluate
147090** a==5 and b IN (1,2,3). The current values for a and b will be stored
147091** in consecutive registers and the index of the first register is returned.
147092**
147093** In the example above nEq==2. But this subroutine works for any value
147094** of nEq including 0. If nEq==0, this routine is nearly a no-op.
147095** The only thing it does is allocate the pLevel->iMem memory cell and
147096** compute the affinity string.
147097**
147098** The nExtraReg parameter is 0 or 1. It is 0 if all WHERE clause constraints
147099** are == or IN and are covered by the nEq. nExtraReg is 1 if there is
147100** an inequality constraint (such as the "c>=5 AND c<10" in the example) that
147101** occurs after the nEq quality constraints.
147102**
147103** This routine allocates a range of nEq+nExtraReg memory cells and returns
147104** the index of the first memory cell in that range. The code that
147105** calls this routine will use that memory range to store keys for
147106** start and termination conditions of the loop.
147107** key value of the loop. If one or more IN operators appear, then
147108** this routine allocates an additional nEq memory cells for internal
147109** use.
147110**
147111** Before returning, *pzAff is set to point to a buffer containing a
147112** copy of the column affinity string of the index allocated using
147113** sqlite3DbMalloc(). Except, entries in the copy of the string associated
147114** with equality constraints that use BLOB or NONE affinity are set to
147115** SQLITE_AFF_BLOB. This is to deal with SQL such as the following:
147116**
147117** CREATE TABLE t1(a TEXT PRIMARY KEY, b);
147118** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
147119**
147120** In the example above, the index on t1(a) has TEXT affinity. But since
147121** the right hand side of the equality constraint (t2.b) has BLOB/NONE affinity,
147122** no conversion should be attempted before using a t2.b value as part of
147123** a key to search the index. Hence the first byte in the returned affinity
147124** string in this example would be set to SQLITE_AFF_BLOB.
147125*/
147126static int codeAllEqualityTerms(
147127 Parse *pParse, /* Parsing context */
147128 WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */
147129 int bRev, /* Reverse the order of IN operators */
147130 int nExtraReg, /* Number of extra registers to allocate */
147131 char **pzAff /* OUT: Set to point to affinity string */
147132){
147133 u16 nEq; /* The number of == or IN constraints to code */
147134 u16 nSkip; /* Number of left-most columns to skip */
147135 Vdbe *v = pParse->pVdbe; /* The vm under construction */
147136 Index *pIdx; /* The index being used for this loop */
147137 WhereTerm *pTerm; /* A single constraint term */
147138 WhereLoop *pLoop; /* The WhereLoop object */
147139 int j; /* Loop counter */
147140 int regBase; /* Base register */
147141 int nReg; /* Number of registers to allocate */
147142 char *zAff; /* Affinity string to return */
147143
147144 /* This module is only called on query plans that use an index. */
147145 pLoop = pLevel->pWLoop;
147146 assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
147147 nEq = pLoop->u.btree.nEq;
147148 nSkip = pLoop->nSkip;
147149 pIdx = pLoop->u.btree.pIndex;
147150 assert( pIdx!=0 );
147151
147152 /* Figure out how many memory cells we will need then allocate them.
147153 */
147154 regBase = pParse->nMem + 1;
147155 nReg = pLoop->u.btree.nEq + nExtraReg;
147156 pParse->nMem += nReg;
147157
147158 zAff = sqlite3DbStrDup(db: pParse->db,z: sqlite3IndexAffinityStr(db: pParse->db,pIdx));
147159 assert( zAff!=0 || pParse->db->mallocFailed );
147160
147161 if( nSkip ){
147162 int iIdxCur = pLevel->iIdxCur;
147163 sqlite3VdbeAddOp3(p: v, OP_Null, p1: 0, p2: regBase, p3: regBase+nSkip-1);
147164 sqlite3VdbeAddOp1(p: v, op: (bRev?OP_Last:OP_Rewind), p1: iIdxCur);
147165 VdbeCoverageIf(v, bRev==0);
147166 VdbeCoverageIf(v, bRev!=0);
147167 VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
147168 j = sqlite3VdbeAddOp0(p: v, OP_Goto);
147169 pLevel->addrSkip = sqlite3VdbeAddOp4Int(p: v, op: (bRev?OP_SeekLT:OP_SeekGT),
147170 p1: iIdxCur, p2: 0, p3: regBase, p4: nSkip);
147171 VdbeCoverageIf(v, bRev==0);
147172 VdbeCoverageIf(v, bRev!=0);
147173 sqlite3VdbeJumpHere(p: v, addr: j);
147174 for(j=0; j<nSkip; j++){
147175 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iIdxCur, p2: j, p3: regBase+j);
147176 testcase( pIdx->aiColumn[j]==XN_EXPR );
147177 VdbeComment((v, "%s", explainIndexColumnName(pIdx, j)));
147178 }
147179 }
147180
147181 /* Evaluate the equality constraints
147182 */
147183 assert( zAff==0 || (int)strlen(zAff)>=nEq );
147184 for(j=nSkip; j<nEq; j++){
147185 int r1;
147186 pTerm = pLoop->aLTerm[j];
147187 assert( pTerm!=0 );
147188 /* The following testcase is true for indices with redundant columns.
147189 ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
147190 testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
147191 testcase( pTerm->wtFlags & TERM_VIRTUAL );
147192 r1 = codeEqualityTerm(pParse, pTerm, pLevel, iEq: j, bRev, iTarget: regBase+j);
147193 if( r1!=regBase+j ){
147194 if( nReg==1 ){
147195 sqlite3ReleaseTempReg(pParse, iReg: regBase);
147196 regBase = r1;
147197 }else{
147198 sqlite3VdbeAddOp2(p: v, OP_Copy, p1: r1, p2: regBase+j);
147199 }
147200 }
147201 if( pTerm->eOperator & WO_IN ){
147202 if( pTerm->pExpr->flags & EP_xIsSelect ){
147203 /* No affinity ever needs to be (or should be) applied to a value
147204 ** from the RHS of an "? IN (SELECT ...)" expression. The
147205 ** sqlite3FindInIndex() routine has already ensured that the
147206 ** affinity of the comparison has been applied to the value. */
147207 if( zAff ) zAff[j] = SQLITE_AFF_BLOB;
147208 }
147209 }else if( (pTerm->eOperator & WO_ISNULL)==0 ){
147210 Expr *pRight = pTerm->pExpr->pRight;
147211 if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(p: pRight) ){
147212 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: regBase+j, p2: pLevel->addrBrk);
147213 VdbeCoverage(v);
147214 }
147215 if( pParse->db->mallocFailed==0 && pParse->nErr==0 ){
147216 if( sqlite3CompareAffinity(pExpr: pRight, aff2: zAff[j])==SQLITE_AFF_BLOB ){
147217 zAff[j] = SQLITE_AFF_BLOB;
147218 }
147219 if( sqlite3ExprNeedsNoAffinityChange(p: pRight, aff: zAff[j]) ){
147220 zAff[j] = SQLITE_AFF_BLOB;
147221 }
147222 }
147223 }
147224 }
147225 *pzAff = zAff;
147226 return regBase;
147227}
147228
147229#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
147230/*
147231** If the most recently coded instruction is a constant range constraint
147232** (a string literal) that originated from the LIKE optimization, then
147233** set P3 and P5 on the OP_String opcode so that the string will be cast
147234** to a BLOB at appropriate times.
147235**
147236** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range
147237** expression: "x>='ABC' AND x<'abd'". But this requires that the range
147238** scan loop run twice, once for strings and a second time for BLOBs.
147239** The OP_String opcodes on the second pass convert the upper and lower
147240** bound string constants to blobs. This routine makes the necessary changes
147241** to the OP_String opcodes for that to happen.
147242**
147243** Except, of course, if SQLITE_LIKE_DOESNT_MATCH_BLOBS is defined, then
147244** only the one pass through the string space is required, so this routine
147245** becomes a no-op.
147246*/
147247static void whereLikeOptimizationStringFixup(
147248 Vdbe *v, /* prepared statement under construction */
147249 WhereLevel *pLevel, /* The loop that contains the LIKE operator */
147250 WhereTerm *pTerm /* The upper or lower bound just coded */
147251){
147252 if( pTerm->wtFlags & TERM_LIKEOPT ){
147253 VdbeOp *pOp;
147254 assert( pLevel->iLikeRepCntr>0 );
147255 pOp = sqlite3VdbeGetOp(p: v, addr: -1);
147256 assert( pOp!=0 );
147257 assert( pOp->opcode==OP_String8
147258 || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
147259 pOp->p3 = (int)(pLevel->iLikeRepCntr>>1); /* Register holding counter */
147260 pOp->p5 = (u8)(pLevel->iLikeRepCntr&1); /* ASC or DESC */
147261 }
147262}
147263#else
147264# define whereLikeOptimizationStringFixup(A,B,C)
147265#endif
147266
147267#ifdef SQLITE_ENABLE_CURSOR_HINTS
147268/*
147269** Information is passed from codeCursorHint() down to individual nodes of
147270** the expression tree (by sqlite3WalkExpr()) using an instance of this
147271** structure.
147272*/
147273struct CCurHint {
147274 int iTabCur; /* Cursor for the main table */
147275 int iIdxCur; /* Cursor for the index, if pIdx!=0. Unused otherwise */
147276 Index *pIdx; /* The index used to access the table */
147277};
147278
147279/*
147280** This function is called for every node of an expression that is a candidate
147281** for a cursor hint on an index cursor. For TK_COLUMN nodes that reference
147282** the table CCurHint.iTabCur, verify that the same column can be
147283** accessed through the index. If it cannot, then set pWalker->eCode to 1.
147284*/
147285static int codeCursorHintCheckExpr(Walker *pWalker, Expr *pExpr){
147286 struct CCurHint *pHint = pWalker->u.pCCurHint;
147287 assert( pHint->pIdx!=0 );
147288 if( pExpr->op==TK_COLUMN
147289 && pExpr->iTable==pHint->iTabCur
147290 && sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn)<0
147291 ){
147292 pWalker->eCode = 1;
147293 }
147294 return WRC_Continue;
147295}
147296
147297/*
147298** Test whether or not expression pExpr, which was part of a WHERE clause,
147299** should be included in the cursor-hint for a table that is on the rhs
147300** of a LEFT JOIN. Set Walker.eCode to non-zero before returning if the
147301** expression is not suitable.
147302**
147303** An expression is unsuitable if it might evaluate to non NULL even if
147304** a TK_COLUMN node that does affect the value of the expression is set
147305** to NULL. For example:
147306**
147307** col IS NULL
147308** col IS NOT NULL
147309** coalesce(col, 1)
147310** CASE WHEN col THEN 0 ELSE 1 END
147311*/
147312static int codeCursorHintIsOrFunction(Walker *pWalker, Expr *pExpr){
147313 if( pExpr->op==TK_IS
147314 || pExpr->op==TK_ISNULL || pExpr->op==TK_ISNOT
147315 || pExpr->op==TK_NOTNULL || pExpr->op==TK_CASE
147316 ){
147317 pWalker->eCode = 1;
147318 }else if( pExpr->op==TK_FUNCTION ){
147319 int d1;
147320 char d2[4];
147321 if( 0==sqlite3IsLikeFunction(pWalker->pParse->db, pExpr, &d1, d2) ){
147322 pWalker->eCode = 1;
147323 }
147324 }
147325
147326 return WRC_Continue;
147327}
147328
147329
147330/*
147331** This function is called on every node of an expression tree used as an
147332** argument to the OP_CursorHint instruction. If the node is a TK_COLUMN
147333** that accesses any table other than the one identified by
147334** CCurHint.iTabCur, then do the following:
147335**
147336** 1) allocate a register and code an OP_Column instruction to read
147337** the specified column into the new register, and
147338**
147339** 2) transform the expression node to a TK_REGISTER node that reads
147340** from the newly populated register.
147341**
147342** Also, if the node is a TK_COLUMN that does access the table idenified
147343** by pCCurHint.iTabCur, and an index is being used (which we will
147344** know because CCurHint.pIdx!=0) then transform the TK_COLUMN into
147345** an access of the index rather than the original table.
147346*/
147347static int codeCursorHintFixExpr(Walker *pWalker, Expr *pExpr){
147348 int rc = WRC_Continue;
147349 struct CCurHint *pHint = pWalker->u.pCCurHint;
147350 if( pExpr->op==TK_COLUMN ){
147351 if( pExpr->iTable!=pHint->iTabCur ){
147352 int reg = ++pWalker->pParse->nMem; /* Register for column value */
147353 sqlite3ExprCode(pWalker->pParse, pExpr, reg);
147354 pExpr->op = TK_REGISTER;
147355 pExpr->iTable = reg;
147356 }else if( pHint->pIdx!=0 ){
147357 pExpr->iTable = pHint->iIdxCur;
147358 pExpr->iColumn = sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn);
147359 assert( pExpr->iColumn>=0 );
147360 }
147361 }else if( pExpr->op==TK_AGG_FUNCTION ){
147362 /* An aggregate function in the WHERE clause of a query means this must
147363 ** be a correlated sub-query, and expression pExpr is an aggregate from
147364 ** the parent context. Do not walk the function arguments in this case.
147365 **
147366 ** todo: It should be possible to replace this node with a TK_REGISTER
147367 ** expression, as the result of the expression must be stored in a
147368 ** register at this point. The same holds for TK_AGG_COLUMN nodes. */
147369 rc = WRC_Prune;
147370 }
147371 return rc;
147372}
147373
147374/*
147375** Insert an OP_CursorHint instruction if it is appropriate to do so.
147376*/
147377static void codeCursorHint(
147378 SrcItem *pTabItem, /* FROM clause item */
147379 WhereInfo *pWInfo, /* The where clause */
147380 WhereLevel *pLevel, /* Which loop to provide hints for */
147381 WhereTerm *pEndRange /* Hint this end-of-scan boundary term if not NULL */
147382){
147383 Parse *pParse = pWInfo->pParse;
147384 sqlite3 *db = pParse->db;
147385 Vdbe *v = pParse->pVdbe;
147386 Expr *pExpr = 0;
147387 WhereLoop *pLoop = pLevel->pWLoop;
147388 int iCur;
147389 WhereClause *pWC;
147390 WhereTerm *pTerm;
147391 int i, j;
147392 struct CCurHint sHint;
147393 Walker sWalker;
147394
147395 if( OptimizationDisabled(db, SQLITE_CursorHints) ) return;
147396 iCur = pLevel->iTabCur;
147397 assert( iCur==pWInfo->pTabList->a[pLevel->iFrom].iCursor );
147398 sHint.iTabCur = iCur;
147399 sHint.iIdxCur = pLevel->iIdxCur;
147400 sHint.pIdx = pLoop->u.btree.pIndex;
147401 memset(&sWalker, 0, sizeof(sWalker));
147402 sWalker.pParse = pParse;
147403 sWalker.u.pCCurHint = &sHint;
147404 pWC = &pWInfo->sWC;
147405 for(i=0; i<pWC->nTerm; i++){
147406 pTerm = &pWC->a[i];
147407 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
147408 if( pTerm->prereqAll & pLevel->notReady ) continue;
147409
147410 /* Any terms specified as part of the ON(...) clause for any LEFT
147411 ** JOIN for which the current table is not the rhs are omitted
147412 ** from the cursor-hint.
147413 **
147414 ** If this table is the rhs of a LEFT JOIN, "IS" or "IS NULL" terms
147415 ** that were specified as part of the WHERE clause must be excluded.
147416 ** This is to address the following:
147417 **
147418 ** SELECT ... t1 LEFT JOIN t2 ON (t1.a=t2.b) WHERE t2.c IS NULL;
147419 **
147420 ** Say there is a single row in t2 that matches (t1.a=t2.b), but its
147421 ** t2.c values is not NULL. If the (t2.c IS NULL) constraint is
147422 ** pushed down to the cursor, this row is filtered out, causing
147423 ** SQLite to synthesize a row of NULL values. Which does match the
147424 ** WHERE clause, and so the query returns a row. Which is incorrect.
147425 **
147426 ** For the same reason, WHERE terms such as:
147427 **
147428 ** WHERE 1 = (t2.c IS NULL)
147429 **
147430 ** are also excluded. See codeCursorHintIsOrFunction() for details.
147431 */
147432 if( pTabItem->fg.jointype & JT_LEFT ){
147433 Expr *pExpr = pTerm->pExpr;
147434 if( !ExprHasProperty(pExpr, EP_FromJoin)
147435 || pExpr->iRightJoinTable!=pTabItem->iCursor
147436 ){
147437 sWalker.eCode = 0;
147438 sWalker.xExprCallback = codeCursorHintIsOrFunction;
147439 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
147440 if( sWalker.eCode ) continue;
147441 }
147442 }else{
147443 if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) continue;
147444 }
147445
147446 /* All terms in pWLoop->aLTerm[] except pEndRange are used to initialize
147447 ** the cursor. These terms are not needed as hints for a pure range
147448 ** scan (that has no == terms) so omit them. */
147449 if( pLoop->u.btree.nEq==0 && pTerm!=pEndRange ){
147450 for(j=0; j<pLoop->nLTerm && pLoop->aLTerm[j]!=pTerm; j++){}
147451 if( j<pLoop->nLTerm ) continue;
147452 }
147453
147454 /* No subqueries or non-deterministic functions allowed */
147455 if( sqlite3ExprContainsSubquery(pTerm->pExpr) ) continue;
147456
147457 /* For an index scan, make sure referenced columns are actually in
147458 ** the index. */
147459 if( sHint.pIdx!=0 ){
147460 sWalker.eCode = 0;
147461 sWalker.xExprCallback = codeCursorHintCheckExpr;
147462 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
147463 if( sWalker.eCode ) continue;
147464 }
147465
147466 /* If we survive all prior tests, that means this term is worth hinting */
147467 pExpr = sqlite3ExprAnd(pParse, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0));
147468 }
147469 if( pExpr!=0 ){
147470 sWalker.xExprCallback = codeCursorHintFixExpr;
147471 sqlite3WalkExpr(&sWalker, pExpr);
147472 sqlite3VdbeAddOp4(v, OP_CursorHint,
147473 (sHint.pIdx ? sHint.iIdxCur : sHint.iTabCur), 0, 0,
147474 (const char*)pExpr, P4_EXPR);
147475 }
147476}
147477#else
147478# define codeCursorHint(A,B,C,D) /* No-op */
147479#endif /* SQLITE_ENABLE_CURSOR_HINTS */
147480
147481/*
147482** Cursor iCur is open on an intkey b-tree (a table). Register iRowid contains
147483** a rowid value just read from cursor iIdxCur, open on index pIdx. This
147484** function generates code to do a deferred seek of cursor iCur to the
147485** rowid stored in register iRowid.
147486**
147487** Normally, this is just:
147488**
147489** OP_DeferredSeek $iCur $iRowid
147490**
147491** However, if the scan currently being coded is a branch of an OR-loop and
147492** the statement currently being coded is a SELECT, then P3 of OP_DeferredSeek
147493** is set to iIdxCur and P4 is set to point to an array of integers
147494** containing one entry for each column of the table cursor iCur is open
147495** on. For each table column, if the column is the i'th column of the
147496** index, then the corresponding array entry is set to (i+1). If the column
147497** does not appear in the index at all, the array entry is set to 0.
147498*/
147499static void codeDeferredSeek(
147500 WhereInfo *pWInfo, /* Where clause context */
147501 Index *pIdx, /* Index scan is using */
147502 int iCur, /* Cursor for IPK b-tree */
147503 int iIdxCur /* Index cursor */
147504){
147505 Parse *pParse = pWInfo->pParse; /* Parse context */
147506 Vdbe *v = pParse->pVdbe; /* Vdbe to generate code within */
147507
147508 assert( iIdxCur>0 );
147509 assert( pIdx->aiColumn[pIdx->nColumn-1]==-1 );
147510
147511 pWInfo->bDeferredSeek = 1;
147512 sqlite3VdbeAddOp3(p: v, OP_DeferredSeek, p1: iIdxCur, p2: 0, p3: iCur);
147513 if( (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)
147514 && DbMaskAllZero(sqlite3ParseToplevel(pParse)->writeMask)
147515 ){
147516 int i;
147517 Table *pTab = pIdx->pTable;
147518 u32 *ai = (u32*)sqlite3DbMallocZero(db: pParse->db, n: sizeof(u32)*(pTab->nCol+1));
147519 if( ai ){
147520 ai[0] = pTab->nCol;
147521 for(i=0; i<pIdx->nColumn-1; i++){
147522 int x1, x2;
147523 assert( pIdx->aiColumn[i]<pTab->nCol );
147524 x1 = pIdx->aiColumn[i];
147525 x2 = sqlite3TableColumnToStorage(pTab, iCol: x1);
147526 testcase( x1!=x2 );
147527 if( x1>=0 ) ai[x2+1] = i+1;
147528 }
147529 sqlite3VdbeChangeP4(p: v, addr: -1, zP4: (char*)ai, P4_INTARRAY);
147530 }
147531 }
147532}
147533
147534/*
147535** If the expression passed as the second argument is a vector, generate
147536** code to write the first nReg elements of the vector into an array
147537** of registers starting with iReg.
147538**
147539** If the expression is not a vector, then nReg must be passed 1. In
147540** this case, generate code to evaluate the expression and leave the
147541** result in register iReg.
147542*/
147543static void codeExprOrVector(Parse *pParse, Expr *p, int iReg, int nReg){
147544 assert( nReg>0 );
147545 if( p && sqlite3ExprIsVector(pExpr: p) ){
147546#ifndef SQLITE_OMIT_SUBQUERY
147547 if( ExprUseXSelect(p) ){
147548 Vdbe *v = pParse->pVdbe;
147549 int iSelect;
147550 assert( p->op==TK_SELECT );
147551 iSelect = sqlite3CodeSubselect(pParse, pExpr: p);
147552 sqlite3VdbeAddOp3(p: v, OP_Copy, p1: iSelect, p2: iReg, p3: nReg-1);
147553 }else
147554#endif
147555 {
147556 int i;
147557 const ExprList *pList;
147558 assert( ExprUseXList(p) );
147559 pList = p->x.pList;
147560 assert( nReg<=pList->nExpr );
147561 for(i=0; i<nReg; i++){
147562 sqlite3ExprCode(pParse, pExpr: pList->a[i].pExpr, target: iReg+i);
147563 }
147564 }
147565 }else{
147566 assert( nReg==1 || pParse->nErr );
147567 sqlite3ExprCode(pParse, pExpr: p, target: iReg);
147568 }
147569}
147570
147571/* An instance of the IdxExprTrans object carries information about a
147572** mapping from an expression on table columns into a column in an index
147573** down through the Walker.
147574*/
147575typedef struct IdxExprTrans {
147576 Expr *pIdxExpr; /* The index expression */
147577 int iTabCur; /* The cursor of the corresponding table */
147578 int iIdxCur; /* The cursor for the index */
147579 int iIdxCol; /* The column for the index */
147580 int iTabCol; /* The column for the table */
147581 WhereInfo *pWInfo; /* Complete WHERE clause information */
147582 sqlite3 *db; /* Database connection (for malloc()) */
147583} IdxExprTrans;
147584
147585/*
147586** Preserve pExpr on the WhereETrans list of the WhereInfo.
147587*/
147588static void preserveExpr(IdxExprTrans *pTrans, Expr *pExpr){
147589 WhereExprMod *pNew;
147590 pNew = sqlite3DbMallocRaw(db: pTrans->db, n: sizeof(*pNew));
147591 if( pNew==0 ) return;
147592 pNew->pNext = pTrans->pWInfo->pExprMods;
147593 pTrans->pWInfo->pExprMods = pNew;
147594 pNew->pExpr = pExpr;
147595 memcpy(dest: &pNew->orig, src: pExpr, n: sizeof(*pExpr));
147596}
147597
147598/* The walker node callback used to transform matching expressions into
147599** a reference to an index column for an index on an expression.
147600**
147601** If pExpr matches, then transform it into a reference to the index column
147602** that contains the value of pExpr.
147603*/
147604static int whereIndexExprTransNode(Walker *p, Expr *pExpr){
147605 IdxExprTrans *pX = p->u.pIdxTrans;
147606 if( sqlite3ExprCompare(pParse: 0, pA: pExpr, pB: pX->pIdxExpr, iTab: pX->iTabCur)==0 ){
147607 preserveExpr(pTrans: pX, pExpr);
147608 pExpr->affExpr = sqlite3ExprAffinity(pExpr);
147609 pExpr->op = TK_COLUMN;
147610 pExpr->iTable = pX->iIdxCur;
147611 pExpr->iColumn = pX->iIdxCol;
147612 testcase( ExprHasProperty(pExpr, EP_Skip) );
147613 testcase( ExprHasProperty(pExpr, EP_Unlikely) );
147614 ExprClearProperty(pExpr, EP_Skip|EP_Unlikely|EP_WinFunc|EP_Subrtn);
147615 pExpr->y.pTab = 0;
147616 return WRC_Prune;
147617 }else{
147618 return WRC_Continue;
147619 }
147620}
147621
147622#ifndef SQLITE_OMIT_GENERATED_COLUMNS
147623/* A walker node callback that translates a column reference to a table
147624** into a corresponding column reference of an index.
147625*/
147626static int whereIndexExprTransColumn(Walker *p, Expr *pExpr){
147627 if( pExpr->op==TK_COLUMN ){
147628 IdxExprTrans *pX = p->u.pIdxTrans;
147629 if( pExpr->iTable==pX->iTabCur && pExpr->iColumn==pX->iTabCol ){
147630 assert( ExprUseYTab(pExpr) && pExpr->y.pTab!=0 );
147631 preserveExpr(pTrans: pX, pExpr);
147632 pExpr->affExpr = sqlite3TableColumnAffinity(pTab: pExpr->y.pTab,iCol: pExpr->iColumn);
147633 pExpr->iTable = pX->iIdxCur;
147634 pExpr->iColumn = pX->iIdxCol;
147635 pExpr->y.pTab = 0;
147636 }
147637 }
147638 return WRC_Continue;
147639}
147640#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
147641
147642/*
147643** For an indexes on expression X, locate every instance of expression X
147644** in pExpr and change that subexpression into a reference to the appropriate
147645** column of the index.
147646**
147647** 2019-10-24: Updated to also translate references to a VIRTUAL column in
147648** the table into references to the corresponding (stored) column of the
147649** index.
147650*/
147651static void whereIndexExprTrans(
147652 Index *pIdx, /* The Index */
147653 int iTabCur, /* Cursor of the table that is being indexed */
147654 int iIdxCur, /* Cursor of the index itself */
147655 WhereInfo *pWInfo /* Transform expressions in this WHERE clause */
147656){
147657 int iIdxCol; /* Column number of the index */
147658 ExprList *aColExpr; /* Expressions that are indexed */
147659 Table *pTab;
147660 Walker w;
147661 IdxExprTrans x;
147662 aColExpr = pIdx->aColExpr;
147663 if( aColExpr==0 && !pIdx->bHasVCol ){
147664 /* The index does not reference any expressions or virtual columns
147665 ** so no translations are needed. */
147666 return;
147667 }
147668 pTab = pIdx->pTable;
147669 memset(s: &w, c: 0, n: sizeof(w));
147670 w.u.pIdxTrans = &x;
147671 x.iTabCur = iTabCur;
147672 x.iIdxCur = iIdxCur;
147673 x.pWInfo = pWInfo;
147674 x.db = pWInfo->pParse->db;
147675 for(iIdxCol=0; iIdxCol<pIdx->nColumn; iIdxCol++){
147676 i16 iRef = pIdx->aiColumn[iIdxCol];
147677 if( iRef==XN_EXPR ){
147678 assert( aColExpr!=0 && aColExpr->a[iIdxCol].pExpr!=0 );
147679 x.pIdxExpr = aColExpr->a[iIdxCol].pExpr;
147680 if( sqlite3ExprIsConstant(p: x.pIdxExpr) ) continue;
147681 w.xExprCallback = whereIndexExprTransNode;
147682#ifndef SQLITE_OMIT_GENERATED_COLUMNS
147683 }else if( iRef>=0
147684 && (pTab->aCol[iRef].colFlags & COLFLAG_VIRTUAL)!=0
147685 && ((pTab->aCol[iRef].colFlags & COLFLAG_HASCOLL)==0
147686 || sqlite3StrICmp(zLeft: sqlite3ColumnColl(pCol: &pTab->aCol[iRef]),
147687 zRight: sqlite3StrBINARY)==0)
147688 ){
147689 /* Check to see if there are direct references to generated columns
147690 ** that are contained in the index. Pulling the generated column
147691 ** out of the index is an optimization only - the main table is always
147692 ** available if the index cannot be used. To avoid unnecessary
147693 ** complication, omit this optimization if the collating sequence for
147694 ** the column is non-standard */
147695 x.iTabCol = iRef;
147696 w.xExprCallback = whereIndexExprTransColumn;
147697#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
147698 }else{
147699 continue;
147700 }
147701 x.iIdxCol = iIdxCol;
147702 sqlite3WalkExpr(pWalker: &w, pExpr: pWInfo->pWhere);
147703 sqlite3WalkExprList(pWalker: &w, p: pWInfo->pOrderBy);
147704 sqlite3WalkExprList(pWalker: &w, p: pWInfo->pResultSet);
147705 }
147706}
147707
147708/*
147709** The pTruth expression is always true because it is the WHERE clause
147710** a partial index that is driving a query loop. Look through all of the
147711** WHERE clause terms on the query, and if any of those terms must be
147712** true because pTruth is true, then mark those WHERE clause terms as
147713** coded.
147714*/
147715static void whereApplyPartialIndexConstraints(
147716 Expr *pTruth,
147717 int iTabCur,
147718 WhereClause *pWC
147719){
147720 int i;
147721 WhereTerm *pTerm;
147722 while( pTruth->op==TK_AND ){
147723 whereApplyPartialIndexConstraints(pTruth: pTruth->pLeft, iTabCur, pWC);
147724 pTruth = pTruth->pRight;
147725 }
147726 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
147727 Expr *pExpr;
147728 if( pTerm->wtFlags & TERM_CODED ) continue;
147729 pExpr = pTerm->pExpr;
147730 if( sqlite3ExprCompare(pParse: 0, pA: pExpr, pB: pTruth, iTab: iTabCur)==0 ){
147731 pTerm->wtFlags |= TERM_CODED;
147732 }
147733 }
147734}
147735
147736/*
147737** Generate code for the start of the iLevel-th loop in the WHERE clause
147738** implementation described by pWInfo.
147739*/
147740SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
147741 Parse *pParse, /* Parsing context */
147742 Vdbe *v, /* Prepared statement under construction */
147743 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
147744 int iLevel, /* Which level of pWInfo->a[] should be coded */
147745 WhereLevel *pLevel, /* The current level pointer */
147746 Bitmask notReady /* Which tables are currently available */
147747){
147748 int j, k; /* Loop counters */
147749 int iCur; /* The VDBE cursor for the table */
147750 int addrNxt; /* Where to jump to continue with the next IN case */
147751 int bRev; /* True if we need to scan in reverse order */
147752 WhereLoop *pLoop; /* The WhereLoop object being coded */
147753 WhereClause *pWC; /* Decomposition of the entire WHERE clause */
147754 WhereTerm *pTerm; /* A WHERE clause term */
147755 sqlite3 *db; /* Database connection */
147756 SrcItem *pTabItem; /* FROM clause term being coded */
147757 int addrBrk; /* Jump here to break out of the loop */
147758 int addrHalt; /* addrBrk for the outermost loop */
147759 int addrCont; /* Jump here to continue with next cycle */
147760 int iRowidReg = 0; /* Rowid is stored in this register, if not zero */
147761 int iReleaseReg = 0; /* Temp register to free before returning */
147762 Index *pIdx = 0; /* Index used by loop (if any) */
147763 int iLoop; /* Iteration of constraint generator loop */
147764
147765 pWC = &pWInfo->sWC;
147766 db = pParse->db;
147767 pLoop = pLevel->pWLoop;
147768 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
147769 iCur = pTabItem->iCursor;
147770 pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
147771 bRev = (pWInfo->revMask>>iLevel)&1;
147772 VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
147773#if WHERETRACE_ENABLED /* 0x20800 */
147774 if( sqlite3WhereTrace & 0x800 ){
147775 sqlite3DebugPrintf("Coding level %d of %d: notReady=%llx iFrom=%d\n",
147776 iLevel, pWInfo->nLevel, (u64)notReady, pLevel->iFrom);
147777 sqlite3WhereLoopPrint(pLoop, pWC);
147778 }
147779 if( sqlite3WhereTrace & 0x20000 ){
147780 if( iLevel==0 ){
147781 sqlite3DebugPrintf("WHERE clause being coded:\n");
147782 sqlite3TreeViewExpr(0, pWInfo->pWhere, 0);
147783 }
147784 sqlite3DebugPrintf("All WHERE-clause terms before coding:\n");
147785 sqlite3WhereClausePrint(pWC);
147786 }
147787#endif
147788
147789 /* Create labels for the "break" and "continue" instructions
147790 ** for the current loop. Jump to addrBrk to break out of a loop.
147791 ** Jump to cont to go immediately to the next iteration of the
147792 ** loop.
147793 **
147794 ** When there is an IN operator, we also have a "addrNxt" label that
147795 ** means to continue with the next IN value combination. When
147796 ** there are no IN operators in the constraints, the "addrNxt" label
147797 ** is the same as "addrBrk".
147798 */
147799 addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
147800 addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(pParse);
147801
147802 /* If this is the right table of a LEFT OUTER JOIN, allocate and
147803 ** initialize a memory cell that records if this table matches any
147804 ** row of the left table of the join.
147805 */
147806 assert( (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)
147807 || pLevel->iFrom>0 || (pTabItem[0].fg.jointype & JT_LEFT)==0
147808 );
147809 if( pLevel->iFrom>0 && (pTabItem[0].fg.jointype & JT_LEFT)!=0 ){
147810 pLevel->iLeftJoin = ++pParse->nMem;
147811 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: pLevel->iLeftJoin);
147812 VdbeComment((v, "init LEFT JOIN no-match flag"));
147813 }
147814
147815 /* Compute a safe address to jump to if we discover that the table for
147816 ** this loop is empty and can never contribute content. */
147817 for(j=iLevel; j>0 && pWInfo->a[j].iLeftJoin==0; j--){}
147818 addrHalt = pWInfo->a[j].addrBrk;
147819
147820 /* Special case of a FROM clause subquery implemented as a co-routine */
147821 if( pTabItem->fg.viaCoroutine ){
147822 int regYield = pTabItem->regReturn;
147823 sqlite3VdbeAddOp3(p: v, OP_InitCoroutine, p1: regYield, p2: 0, p3: pTabItem->addrFillSub);
147824 pLevel->p2 = sqlite3VdbeAddOp2(p: v, OP_Yield, p1: regYield, p2: addrBrk);
147825 VdbeCoverage(v);
147826 VdbeComment((v, "next row of %s", pTabItem->pTab->zName));
147827 pLevel->op = OP_Goto;
147828 }else
147829
147830#ifndef SQLITE_OMIT_VIRTUALTABLE
147831 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
147832 /* Case 1: The table is a virtual-table. Use the VFilter and VNext
147833 ** to access the data.
147834 */
147835 int iReg; /* P3 Value for OP_VFilter */
147836 int addrNotFound;
147837 int nConstraint = pLoop->nLTerm;
147838 int iIn; /* Counter for IN constraints */
147839
147840 iReg = sqlite3GetTempRange(pParse, nReg: nConstraint+2);
147841 addrNotFound = pLevel->addrBrk;
147842 for(j=0; j<nConstraint; j++){
147843 int iTarget = iReg+j+2;
147844 pTerm = pLoop->aLTerm[j];
147845 if( NEVER(pTerm==0) ) continue;
147846 if( pTerm->eOperator & WO_IN ){
147847 codeEqualityTerm(pParse, pTerm, pLevel, iEq: j, bRev, iTarget);
147848 addrNotFound = pLevel->addrNxt;
147849 }else{
147850 Expr *pRight = pTerm->pExpr->pRight;
147851 codeExprOrVector(pParse, p: pRight, iReg: iTarget, nReg: 1);
147852 }
147853 }
147854 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: pLoop->u.vtab.idxNum, p2: iReg);
147855 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: nConstraint, p2: iReg+1);
147856 sqlite3VdbeAddOp4(p: v, OP_VFilter, p1: iCur, p2: addrNotFound, p3: iReg,
147857 zP4: pLoop->u.vtab.idxStr,
147858 p4type: pLoop->u.vtab.needFree ? P4_DYNAMIC : P4_STATIC);
147859 VdbeCoverage(v);
147860 pLoop->u.vtab.needFree = 0;
147861 /* An OOM inside of AddOp4(OP_VFilter) instruction above might have freed
147862 ** the u.vtab.idxStr. NULL it out to prevent a use-after-free */
147863 if( db->mallocFailed ) pLoop->u.vtab.idxStr = 0;
147864 pLevel->p1 = iCur;
147865 pLevel->op = pWInfo->eOnePass ? OP_Noop : OP_VNext;
147866 pLevel->p2 = sqlite3VdbeCurrentAddr(p: v);
147867 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
147868 if( pLoop->wsFlags & WHERE_IN_ABLE ){
147869 iIn = pLevel->u.in.nIn;
147870 }else{
147871 iIn = 0;
147872 }
147873 for(j=nConstraint-1; j>=0; j--){
147874 pTerm = pLoop->aLTerm[j];
147875 if( (pTerm->eOperator & WO_IN)!=0 ) iIn--;
147876 if( j<16 && (pLoop->u.vtab.omitMask>>j)&1 ){
147877 disableTerm(pLevel, pTerm);
147878 }else if( (pTerm->eOperator & WO_IN)!=0
147879 && sqlite3ExprVectorSize(pExpr: pTerm->pExpr->pLeft)==1
147880 ){
147881 Expr *pCompare; /* The comparison operator */
147882 Expr *pRight; /* RHS of the comparison */
147883 VdbeOp *pOp; /* Opcode to access the value of the IN constraint */
147884
147885 /* Reload the constraint value into reg[iReg+j+2]. The same value
147886 ** was loaded into the same register prior to the OP_VFilter, but
147887 ** the xFilter implementation might have changed the datatype or
147888 ** encoding of the value in the register, so it *must* be reloaded. */
147889 assert( pLevel->u.in.aInLoop!=0 || db->mallocFailed );
147890 if( !db->mallocFailed ){
147891 assert( iIn>=0 && iIn<pLevel->u.in.nIn );
147892 pOp = sqlite3VdbeGetOp(p: v, addr: pLevel->u.in.aInLoop[iIn].addrInTop);
147893 assert( pOp->opcode==OP_Column || pOp->opcode==OP_Rowid );
147894 assert( pOp->opcode!=OP_Column || pOp->p3==iReg+j+2 );
147895 assert( pOp->opcode!=OP_Rowid || pOp->p2==iReg+j+2 );
147896 testcase( pOp->opcode==OP_Rowid );
147897 sqlite3VdbeAddOp3(p: v, op: pOp->opcode, p1: pOp->p1, p2: pOp->p2, p3: pOp->p3);
147898 }
147899
147900 /* Generate code that will continue to the next row if
147901 ** the IN constraint is not satisfied */
147902 pCompare = sqlite3PExpr(pParse, TK_EQ, pLeft: 0, pRight: 0);
147903 assert( pCompare!=0 || db->mallocFailed );
147904 if( pCompare ){
147905 pCompare->pLeft = pTerm->pExpr->pLeft;
147906 pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER, zToken: 0);
147907 if( pRight ){
147908 pRight->iTable = iReg+j+2;
147909 sqlite3ExprIfFalse(
147910 pParse, pExpr: pCompare, dest: pLevel->addrCont, SQLITE_JUMPIFNULL
147911 );
147912 }
147913 pCompare->pLeft = 0;
147914 sqlite3ExprDelete(db, p: pCompare);
147915 }
147916 }
147917 }
147918 assert( iIn==0 || db->mallocFailed );
147919 /* These registers need to be preserved in case there is an IN operator
147920 ** loop. So we could deallocate the registers here (and potentially
147921 ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0. But it seems
147922 ** simpler and safer to simply not reuse the registers.
147923 **
147924 ** sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
147925 */
147926 }else
147927#endif /* SQLITE_OMIT_VIRTUALTABLE */
147928
147929 if( (pLoop->wsFlags & WHERE_IPK)!=0
147930 && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
147931 ){
147932 /* Case 2: We can directly reference a single row using an
147933 ** equality comparison against the ROWID field. Or
147934 ** we reference multiple rows using a "rowid IN (...)"
147935 ** construct.
147936 */
147937 assert( pLoop->u.btree.nEq==1 );
147938 pTerm = pLoop->aLTerm[0];
147939 assert( pTerm!=0 );
147940 assert( pTerm->pExpr!=0 );
147941 testcase( pTerm->wtFlags & TERM_VIRTUAL );
147942 iReleaseReg = ++pParse->nMem;
147943 iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, iEq: 0, bRev, iTarget: iReleaseReg);
147944 if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReg: iReleaseReg);
147945 addrNxt = pLevel->addrNxt;
147946 sqlite3VdbeAddOp3(p: v, OP_SeekRowid, p1: iCur, p2: addrNxt, p3: iRowidReg);
147947 VdbeCoverage(v);
147948 pLevel->op = OP_Noop;
147949 }else if( (pLoop->wsFlags & WHERE_IPK)!=0
147950 && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
147951 ){
147952 /* Case 3: We have an inequality comparison against the ROWID field.
147953 */
147954 int testOp = OP_Noop;
147955 int start;
147956 int memEndValue = 0;
147957 WhereTerm *pStart, *pEnd;
147958
147959 j = 0;
147960 pStart = pEnd = 0;
147961 if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
147962 if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
147963 assert( pStart!=0 || pEnd!=0 );
147964 if( bRev ){
147965 pTerm = pStart;
147966 pStart = pEnd;
147967 pEnd = pTerm;
147968 }
147969 codeCursorHint(pTabItem, pWInfo, pLevel, pEnd);
147970 if( pStart ){
147971 Expr *pX; /* The expression that defines the start bound */
147972 int r1, rTemp; /* Registers for holding the start boundary */
147973 int op; /* Cursor seek operation */
147974
147975 /* The following constant maps TK_xx codes into corresponding
147976 ** seek opcodes. It depends on a particular ordering of TK_xx
147977 */
147978 const u8 aMoveOp[] = {
147979 /* TK_GT */ OP_SeekGT,
147980 /* TK_LE */ OP_SeekLE,
147981 /* TK_LT */ OP_SeekLT,
147982 /* TK_GE */ OP_SeekGE
147983 };
147984 assert( TK_LE==TK_GT+1 ); /* Make sure the ordering.. */
147985 assert( TK_LT==TK_GT+2 ); /* ... of the TK_xx values... */
147986 assert( TK_GE==TK_GT+3 ); /* ... is correcct. */
147987
147988 assert( (pStart->wtFlags & TERM_VNULL)==0 );
147989 testcase( pStart->wtFlags & TERM_VIRTUAL );
147990 pX = pStart->pExpr;
147991 assert( pX!=0 );
147992 testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
147993 if( sqlite3ExprIsVector(pExpr: pX->pRight) ){
147994 r1 = rTemp = sqlite3GetTempReg(pParse);
147995 codeExprOrVector(pParse, p: pX->pRight, iReg: r1, nReg: 1);
147996 testcase( pX->op==TK_GT );
147997 testcase( pX->op==TK_GE );
147998 testcase( pX->op==TK_LT );
147999 testcase( pX->op==TK_LE );
148000 op = aMoveOp[((pX->op - TK_GT - 1) & 0x3) | 0x1];
148001 assert( pX->op!=TK_GT || op==OP_SeekGE );
148002 assert( pX->op!=TK_GE || op==OP_SeekGE );
148003 assert( pX->op!=TK_LT || op==OP_SeekLE );
148004 assert( pX->op!=TK_LE || op==OP_SeekLE );
148005 }else{
148006 r1 = sqlite3ExprCodeTemp(pParse, pExpr: pX->pRight, pReg: &rTemp);
148007 disableTerm(pLevel, pTerm: pStart);
148008 op = aMoveOp[(pX->op - TK_GT)];
148009 }
148010 sqlite3VdbeAddOp3(p: v, op, p1: iCur, p2: addrBrk, p3: r1);
148011 VdbeComment((v, "pk"));
148012 VdbeCoverageIf(v, pX->op==TK_GT);
148013 VdbeCoverageIf(v, pX->op==TK_LE);
148014 VdbeCoverageIf(v, pX->op==TK_LT);
148015 VdbeCoverageIf(v, pX->op==TK_GE);
148016 sqlite3ReleaseTempReg(pParse, iReg: rTemp);
148017 }else{
148018 sqlite3VdbeAddOp2(p: v, op: bRev ? OP_Last : OP_Rewind, p1: iCur, p2: addrHalt);
148019 VdbeCoverageIf(v, bRev==0);
148020 VdbeCoverageIf(v, bRev!=0);
148021 }
148022 if( pEnd ){
148023 Expr *pX;
148024 pX = pEnd->pExpr;
148025 assert( pX!=0 );
148026 assert( (pEnd->wtFlags & TERM_VNULL)==0 );
148027 testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
148028 testcase( pEnd->wtFlags & TERM_VIRTUAL );
148029 memEndValue = ++pParse->nMem;
148030 codeExprOrVector(pParse, p: pX->pRight, iReg: memEndValue, nReg: 1);
148031 if( 0==sqlite3ExprIsVector(pExpr: pX->pRight)
148032 && (pX->op==TK_LT || pX->op==TK_GT)
148033 ){
148034 testOp = bRev ? OP_Le : OP_Ge;
148035 }else{
148036 testOp = bRev ? OP_Lt : OP_Gt;
148037 }
148038 if( 0==sqlite3ExprIsVector(pExpr: pX->pRight) ){
148039 disableTerm(pLevel, pTerm: pEnd);
148040 }
148041 }
148042 start = sqlite3VdbeCurrentAddr(p: v);
148043 pLevel->op = bRev ? OP_Prev : OP_Next;
148044 pLevel->p1 = iCur;
148045 pLevel->p2 = start;
148046 assert( pLevel->p5==0 );
148047 if( testOp!=OP_Noop ){
148048 iRowidReg = ++pParse->nMem;
148049 sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: iCur, p2: iRowidReg);
148050 sqlite3VdbeAddOp3(p: v, op: testOp, p1: memEndValue, p2: addrBrk, p3: iRowidReg);
148051 VdbeCoverageIf(v, testOp==OP_Le);
148052 VdbeCoverageIf(v, testOp==OP_Lt);
148053 VdbeCoverageIf(v, testOp==OP_Ge);
148054 VdbeCoverageIf(v, testOp==OP_Gt);
148055 sqlite3VdbeChangeP5(p: v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
148056 }
148057 }else if( pLoop->wsFlags & WHERE_INDEXED ){
148058 /* Case 4: A scan using an index.
148059 **
148060 ** The WHERE clause may contain zero or more equality
148061 ** terms ("==" or "IN" operators) that refer to the N
148062 ** left-most columns of the index. It may also contain
148063 ** inequality constraints (>, <, >= or <=) on the indexed
148064 ** column that immediately follows the N equalities. Only
148065 ** the right-most column can be an inequality - the rest must
148066 ** use the "==" and "IN" operators. For example, if the
148067 ** index is on (x,y,z), then the following clauses are all
148068 ** optimized:
148069 **
148070 ** x=5
148071 ** x=5 AND y=10
148072 ** x=5 AND y<10
148073 ** x=5 AND y>5 AND y<10
148074 ** x=5 AND y=5 AND z<=10
148075 **
148076 ** The z<10 term of the following cannot be used, only
148077 ** the x=5 term:
148078 **
148079 ** x=5 AND z<10
148080 **
148081 ** N may be zero if there are inequality constraints.
148082 ** If there are no inequality constraints, then N is at
148083 ** least one.
148084 **
148085 ** This case is also used when there are no WHERE clause
148086 ** constraints but an index is selected anyway, in order
148087 ** to force the output order to conform to an ORDER BY.
148088 */
148089 static const u8 aStartOp[] = {
148090 0,
148091 0,
148092 OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */
148093 OP_Last, /* 3: (!start_constraints && startEq && bRev) */
148094 OP_SeekGT, /* 4: (start_constraints && !startEq && !bRev) */
148095 OP_SeekLT, /* 5: (start_constraints && !startEq && bRev) */
148096 OP_SeekGE, /* 6: (start_constraints && startEq && !bRev) */
148097 OP_SeekLE /* 7: (start_constraints && startEq && bRev) */
148098 };
148099 static const u8 aEndOp[] = {
148100 OP_IdxGE, /* 0: (end_constraints && !bRev && !endEq) */
148101 OP_IdxGT, /* 1: (end_constraints && !bRev && endEq) */
148102 OP_IdxLE, /* 2: (end_constraints && bRev && !endEq) */
148103 OP_IdxLT, /* 3: (end_constraints && bRev && endEq) */
148104 };
148105 u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */
148106 u16 nBtm = pLoop->u.btree.nBtm; /* Length of BTM vector */
148107 u16 nTop = pLoop->u.btree.nTop; /* Length of TOP vector */
148108 int regBase; /* Base register holding constraint values */
148109 WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */
148110 WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */
148111 int startEq; /* True if range start uses ==, >= or <= */
148112 int endEq; /* True if range end uses ==, >= or <= */
148113 int start_constraints; /* Start of range is constrained */
148114 int nConstraint; /* Number of constraint terms */
148115 int iIdxCur; /* The VDBE cursor for the index */
148116 int nExtraReg = 0; /* Number of extra registers needed */
148117 int op; /* Instruction opcode */
148118 char *zStartAff; /* Affinity for start of range constraint */
148119 char *zEndAff = 0; /* Affinity for end of range constraint */
148120 u8 bSeekPastNull = 0; /* True to seek past initial nulls */
148121 u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */
148122 int omitTable; /* True if we use the index only */
148123 int regBignull = 0; /* big-null flag register */
148124 int addrSeekScan = 0; /* Opcode of the OP_SeekScan, if any */
148125
148126 pIdx = pLoop->u.btree.pIndex;
148127 iIdxCur = pLevel->iIdxCur;
148128 assert( nEq>=pLoop->nSkip );
148129
148130 /* Find any inequality constraint terms for the start and end
148131 ** of the range.
148132 */
148133 j = nEq;
148134 if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
148135 pRangeStart = pLoop->aLTerm[j++];
148136 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nBtm);
148137 /* Like optimization range constraints always occur in pairs */
148138 assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 ||
148139 (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
148140 }
148141 if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
148142 pRangeEnd = pLoop->aLTerm[j++];
148143 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nTop);
148144#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
148145 if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
148146 assert( pRangeStart!=0 ); /* LIKE opt constraints */
148147 assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */
148148 pLevel->iLikeRepCntr = (u32)++pParse->nMem;
148149 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 1, p2: (int)pLevel->iLikeRepCntr);
148150 VdbeComment((v, "LIKE loop counter"));
148151 pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(p: v);
148152 /* iLikeRepCntr actually stores 2x the counter register number. The
148153 ** bottom bit indicates whether the search order is ASC or DESC. */
148154 testcase( bRev );
148155 testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
148156 assert( (bRev & ~1)==0 );
148157 pLevel->iLikeRepCntr <<=1;
148158 pLevel->iLikeRepCntr |= bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC);
148159 }
148160#endif
148161 if( pRangeStart==0 ){
148162 j = pIdx->aiColumn[nEq];
148163 if( (j>=0 && pIdx->pTable->aCol[j].notNull==0) || j==XN_EXPR ){
148164 bSeekPastNull = 1;
148165 }
148166 }
148167 }
148168 assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
148169
148170 /* If the WHERE_BIGNULL_SORT flag is set, then index column nEq uses
148171 ** a non-default "big-null" sort (either ASC NULLS LAST or DESC NULLS
148172 ** FIRST). In both cases separate ordered scans are made of those
148173 ** index entries for which the column is null and for those for which
148174 ** it is not. For an ASC sort, the non-NULL entries are scanned first.
148175 ** For DESC, NULL entries are scanned first.
148176 */
148177 if( (pLoop->wsFlags & (WHERE_TOP_LIMIT|WHERE_BTM_LIMIT))==0
148178 && (pLoop->wsFlags & WHERE_BIGNULL_SORT)!=0
148179 ){
148180 assert( bSeekPastNull==0 && nExtraReg==0 && nBtm==0 && nTop==0 );
148181 assert( pRangeEnd==0 && pRangeStart==0 );
148182 testcase( pLoop->nSkip>0 );
148183 nExtraReg = 1;
148184 bSeekPastNull = 1;
148185 pLevel->regBignull = regBignull = ++pParse->nMem;
148186 if( pLevel->iLeftJoin ){
148187 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: regBignull);
148188 }
148189 pLevel->addrBignull = sqlite3VdbeMakeLabel(pParse);
148190 }
148191
148192 /* If we are doing a reverse order scan on an ascending index, or
148193 ** a forward order scan on a descending index, interchange the
148194 ** start and end terms (pRangeStart and pRangeEnd).
148195 */
148196 if( (nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC)) ){
148197 SWAP(WhereTerm *, pRangeEnd, pRangeStart);
148198 SWAP(u8, bSeekPastNull, bStopAtNull);
148199 SWAP(u8, nBtm, nTop);
148200 }
148201
148202 if( iLevel>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0 ){
148203 /* In case OP_SeekScan is used, ensure that the index cursor does not
148204 ** point to a valid row for the first iteration of this loop. */
148205 sqlite3VdbeAddOp1(p: v, OP_NullRow, p1: iIdxCur);
148206 }
148207
148208 /* Generate code to evaluate all constraint terms using == or IN
148209 ** and store the values of those terms in an array of registers
148210 ** starting at regBase.
148211 */
148212 codeCursorHint(pTabItem, pWInfo, pLevel, pRangeEnd);
148213 regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,pzAff: &zStartAff);
148214 assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq );
148215 if( zStartAff && nTop ){
148216 zEndAff = sqlite3DbStrDup(db, z: &zStartAff[nEq]);
148217 }
148218 addrNxt = (regBignull ? pLevel->addrBignull : pLevel->addrNxt);
148219
148220 testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
148221 testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
148222 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
148223 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
148224 startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
148225 endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
148226 start_constraints = pRangeStart || nEq>0;
148227
148228 /* Seek the index cursor to the start of the range. */
148229 nConstraint = nEq;
148230 if( pRangeStart ){
148231 Expr *pRight = pRangeStart->pExpr->pRight;
148232 codeExprOrVector(pParse, p: pRight, iReg: regBase+nEq, nReg: nBtm);
148233 whereLikeOptimizationStringFixup(v, pLevel, pTerm: pRangeStart);
148234 if( (pRangeStart->wtFlags & TERM_VNULL)==0
148235 && sqlite3ExprCanBeNull(p: pRight)
148236 ){
148237 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: regBase+nEq, p2: addrNxt);
148238 VdbeCoverage(v);
148239 }
148240 if( zStartAff ){
148241 updateRangeAffinityStr(pRight, n: nBtm, zAff: &zStartAff[nEq]);
148242 }
148243 nConstraint += nBtm;
148244 testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
148245 if( sqlite3ExprIsVector(pExpr: pRight)==0 ){
148246 disableTerm(pLevel, pTerm: pRangeStart);
148247 }else{
148248 startEq = 1;
148249 }
148250 bSeekPastNull = 0;
148251 }else if( bSeekPastNull ){
148252 startEq = 0;
148253 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: regBase+nEq);
148254 start_constraints = 1;
148255 nConstraint++;
148256 }else if( regBignull ){
148257 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: regBase+nEq);
148258 start_constraints = 1;
148259 nConstraint++;
148260 }
148261 codeApplyAffinity(pParse, base: regBase, n: nConstraint - bSeekPastNull, zAff: zStartAff);
148262 if( pLoop->nSkip>0 && nConstraint==pLoop->nSkip ){
148263 /* The skip-scan logic inside the call to codeAllEqualityConstraints()
148264 ** above has already left the cursor sitting on the correct row,
148265 ** so no further seeking is needed */
148266 }else{
148267 if( regBignull ){
148268 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 1, p2: regBignull);
148269 VdbeComment((v, "NULL-scan pass ctr"));
148270 }
148271
148272 op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
148273 assert( op!=0 );
148274 if( (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0 && op==OP_SeekGE ){
148275 assert( regBignull==0 );
148276 /* TUNING: The OP_SeekScan opcode seeks to reduce the number
148277 ** of expensive seek operations by replacing a single seek with
148278 ** 1 or more step operations. The question is, how many steps
148279 ** should we try before giving up and going with a seek. The cost
148280 ** of a seek is proportional to the logarithm of the of the number
148281 ** of entries in the tree, so basing the number of steps to try
148282 ** on the estimated number of rows in the btree seems like a good
148283 ** guess. */
148284 addrSeekScan = sqlite3VdbeAddOp1(p: v, OP_SeekScan,
148285 p1: (pIdx->aiRowLogEst[0]+9)/10);
148286 VdbeCoverage(v);
148287 }
148288 sqlite3VdbeAddOp4Int(p: v, op, p1: iIdxCur, p2: addrNxt, p3: regBase, p4: nConstraint);
148289 VdbeCoverage(v);
148290 VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind );
148291 VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last );
148292 VdbeCoverageIf(v, op==OP_SeekGT); testcase( op==OP_SeekGT );
148293 VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE );
148294 VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE );
148295 VdbeCoverageIf(v, op==OP_SeekLT); testcase( op==OP_SeekLT );
148296
148297 assert( bSeekPastNull==0 || bStopAtNull==0 );
148298 if( regBignull ){
148299 assert( bSeekPastNull==1 || bStopAtNull==1 );
148300 assert( bSeekPastNull==!bStopAtNull );
148301 assert( bStopAtNull==startEq );
148302 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: sqlite3VdbeCurrentAddr(p: v)+2);
148303 op = aStartOp[(nConstraint>1)*4 + 2 + bRev];
148304 sqlite3VdbeAddOp4Int(p: v, op, p1: iIdxCur, p2: addrNxt, p3: regBase,
148305 p4: nConstraint-startEq);
148306 VdbeCoverage(v);
148307 VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind );
148308 VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last );
148309 VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE );
148310 VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE );
148311 assert( op==OP_Rewind || op==OP_Last || op==OP_SeekGE || op==OP_SeekLE);
148312 }
148313 }
148314
148315 /* Load the value for the inequality constraint at the end of the
148316 ** range (if any).
148317 */
148318 nConstraint = nEq;
148319 assert( pLevel->p2==0 );
148320 if( pRangeEnd ){
148321 Expr *pRight = pRangeEnd->pExpr->pRight;
148322 if( addrSeekScan ){
148323 /* For a seek-scan that has a range on the lowest term of the index,
148324 ** we have to make the top of the loop be code that sets the end
148325 ** condition of the range. Otherwise, the OP_SeekScan might jump
148326 ** over that initialization, leaving the range-end value set to the
148327 ** range-start value, resulting in a wrong answer.
148328 ** See ticket 5981a8c041a3c2f3 (2021-11-02).
148329 */
148330 pLevel->p2 = sqlite3VdbeCurrentAddr(p: v);
148331 }
148332 codeExprOrVector(pParse, p: pRight, iReg: regBase+nEq, nReg: nTop);
148333 whereLikeOptimizationStringFixup(v, pLevel, pTerm: pRangeEnd);
148334 if( (pRangeEnd->wtFlags & TERM_VNULL)==0
148335 && sqlite3ExprCanBeNull(p: pRight)
148336 ){
148337 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: regBase+nEq, p2: addrNxt);
148338 VdbeCoverage(v);
148339 }
148340 if( zEndAff ){
148341 updateRangeAffinityStr(pRight, n: nTop, zAff: zEndAff);
148342 codeApplyAffinity(pParse, base: regBase+nEq, n: nTop, zAff: zEndAff);
148343 }else{
148344 assert( pParse->db->mallocFailed );
148345 }
148346 nConstraint += nTop;
148347 testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
148348
148349 if( sqlite3ExprIsVector(pExpr: pRight)==0 ){
148350 disableTerm(pLevel, pTerm: pRangeEnd);
148351 }else{
148352 endEq = 1;
148353 }
148354 }else if( bStopAtNull ){
148355 if( regBignull==0 ){
148356 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: regBase+nEq);
148357 endEq = 0;
148358 }
148359 nConstraint++;
148360 }
148361 sqlite3DbFree(db, p: zStartAff);
148362 sqlite3DbFree(db, p: zEndAff);
148363
148364 /* Top of the loop body */
148365 if( pLevel->p2==0 ) pLevel->p2 = sqlite3VdbeCurrentAddr(p: v);
148366
148367 /* Check if the index cursor is past the end of the range. */
148368 if( nConstraint ){
148369 if( regBignull ){
148370 /* Except, skip the end-of-range check while doing the NULL-scan */
148371 sqlite3VdbeAddOp2(p: v, OP_IfNot, p1: regBignull, p2: sqlite3VdbeCurrentAddr(p: v)+3);
148372 VdbeComment((v, "If NULL-scan 2nd pass"));
148373 VdbeCoverage(v);
148374 }
148375 op = aEndOp[bRev*2 + endEq];
148376 sqlite3VdbeAddOp4Int(p: v, op, p1: iIdxCur, p2: addrNxt, p3: regBase, p4: nConstraint);
148377 testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT );
148378 testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE );
148379 testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT );
148380 testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE );
148381 if( addrSeekScan ) sqlite3VdbeJumpHere(p: v, addr: addrSeekScan);
148382 }
148383 if( regBignull ){
148384 /* During a NULL-scan, check to see if we have reached the end of
148385 ** the NULLs */
148386 assert( bSeekPastNull==!bStopAtNull );
148387 assert( bSeekPastNull+bStopAtNull==1 );
148388 assert( nConstraint+bSeekPastNull>0 );
148389 sqlite3VdbeAddOp2(p: v, OP_If, p1: regBignull, p2: sqlite3VdbeCurrentAddr(p: v)+2);
148390 VdbeComment((v, "If NULL-scan 1st pass"));
148391 VdbeCoverage(v);
148392 op = aEndOp[bRev*2 + bSeekPastNull];
148393 sqlite3VdbeAddOp4Int(p: v, op, p1: iIdxCur, p2: addrNxt, p3: regBase,
148394 p4: nConstraint+bSeekPastNull);
148395 testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT );
148396 testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE );
148397 testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT );
148398 testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE );
148399 }
148400
148401 if( (pLoop->wsFlags & WHERE_IN_EARLYOUT)!=0 ){
148402 sqlite3VdbeAddOp3(p: v, OP_SeekHit, p1: iIdxCur, p2: nEq, p3: nEq);
148403 }
148404
148405 /* Seek the table cursor, if required */
148406 omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0
148407 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0;
148408 if( omitTable ){
148409 /* pIdx is a covering index. No need to access the main table. */
148410 }else if( HasRowid(pIdx->pTable) ){
148411 codeDeferredSeek(pWInfo, pIdx, iCur, iIdxCur);
148412 }else if( iCur!=iIdxCur ){
148413 Index *pPk = sqlite3PrimaryKeyIndex(pTab: pIdx->pTable);
148414 iRowidReg = sqlite3GetTempRange(pParse, nReg: pPk->nKeyCol);
148415 for(j=0; j<pPk->nKeyCol; j++){
148416 k = sqlite3TableColumnToIndex(pIdx, iCol: pPk->aiColumn[j]);
148417 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iIdxCur, p2: k, p3: iRowidReg+j);
148418 }
148419 sqlite3VdbeAddOp4Int(p: v, OP_NotFound, p1: iCur, p2: addrCont,
148420 p3: iRowidReg, p4: pPk->nKeyCol); VdbeCoverage(v);
148421 }
148422
148423 if( pLevel->iLeftJoin==0 ){
148424 /* If pIdx is an index on one or more expressions, then look through
148425 ** all the expressions in pWInfo and try to transform matching expressions
148426 ** into reference to index columns. Also attempt to translate references
148427 ** to virtual columns in the table into references to (stored) columns
148428 ** of the index.
148429 **
148430 ** Do not do this for the RHS of a LEFT JOIN. This is because the
148431 ** expression may be evaluated after OP_NullRow has been executed on
148432 ** the cursor. In this case it is important to do the full evaluation,
148433 ** as the result of the expression may not be NULL, even if all table
148434 ** column values are. https://www.sqlite.org/src/info/7fa8049685b50b5a
148435 **
148436 ** Also, do not do this when processing one index an a multi-index
148437 ** OR clause, since the transformation will become invalid once we
148438 ** move forward to the next index.
148439 ** https://sqlite.org/src/info/4e8e4857d32d401f
148440 */
148441 if( (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0 ){
148442 whereIndexExprTrans(pIdx, iTabCur: iCur, iIdxCur, pWInfo);
148443 }
148444
148445 /* If a partial index is driving the loop, try to eliminate WHERE clause
148446 ** terms from the query that must be true due to the WHERE clause of
148447 ** the partial index.
148448 **
148449 ** 2019-11-02 ticket 623eff57e76d45f6: This optimization does not work
148450 ** for a LEFT JOIN.
148451 */
148452 if( pIdx->pPartIdxWhere ){
148453 whereApplyPartialIndexConstraints(pTruth: pIdx->pPartIdxWhere, iTabCur: iCur, pWC);
148454 }
148455 }else{
148456 testcase( pIdx->pPartIdxWhere );
148457 /* The following assert() is not a requirement, merely an observation:
148458 ** The OR-optimization doesn't work for the right hand table of
148459 ** a LEFT JOIN: */
148460 assert( (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0 );
148461 }
148462
148463 /* Record the instruction used to terminate the loop. */
148464 if( pLoop->wsFlags & WHERE_ONEROW ){
148465 pLevel->op = OP_Noop;
148466 }else if( bRev ){
148467 pLevel->op = OP_Prev;
148468 }else{
148469 pLevel->op = OP_Next;
148470 }
148471 pLevel->p1 = iIdxCur;
148472 pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
148473 if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
148474 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
148475 }else{
148476 assert( pLevel->p5==0 );
148477 }
148478 if( omitTable ) pIdx = 0;
148479 }else
148480
148481#ifndef SQLITE_OMIT_OR_OPTIMIZATION
148482 if( pLoop->wsFlags & WHERE_MULTI_OR ){
148483 /* Case 5: Two or more separately indexed terms connected by OR
148484 **
148485 ** Example:
148486 **
148487 ** CREATE TABLE t1(a,b,c,d);
148488 ** CREATE INDEX i1 ON t1(a);
148489 ** CREATE INDEX i2 ON t1(b);
148490 ** CREATE INDEX i3 ON t1(c);
148491 **
148492 ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
148493 **
148494 ** In the example, there are three indexed terms connected by OR.
148495 ** The top of the loop looks like this:
148496 **
148497 ** Null 1 # Zero the rowset in reg 1
148498 **
148499 ** Then, for each indexed term, the following. The arguments to
148500 ** RowSetTest are such that the rowid of the current row is inserted
148501 ** into the RowSet. If it is already present, control skips the
148502 ** Gosub opcode and jumps straight to the code generated by WhereEnd().
148503 **
148504 ** sqlite3WhereBegin(<term>)
148505 ** RowSetTest # Insert rowid into rowset
148506 ** Gosub 2 A
148507 ** sqlite3WhereEnd()
148508 **
148509 ** Following the above, code to terminate the loop. Label A, the target
148510 ** of the Gosub above, jumps to the instruction right after the Goto.
148511 **
148512 ** Null 1 # Zero the rowset in reg 1
148513 ** Goto B # The loop is finished.
148514 **
148515 ** A: <loop body> # Return data, whatever.
148516 **
148517 ** Return 2 # Jump back to the Gosub
148518 **
148519 ** B: <after the loop>
148520 **
148521 ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
148522 ** use an ephemeral index instead of a RowSet to record the primary
148523 ** keys of the rows we have already seen.
148524 **
148525 */
148526 WhereClause *pOrWc; /* The OR-clause broken out into subterms */
148527 SrcList *pOrTab; /* Shortened table list or OR-clause generation */
148528 Index *pCov = 0; /* Potential covering index (or NULL) */
148529 int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */
148530
148531 int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */
148532 int regRowset = 0; /* Register for RowSet object */
148533 int regRowid = 0; /* Register holding rowid */
148534 int iLoopBody = sqlite3VdbeMakeLabel(pParse);/* Start of loop body */
148535 int iRetInit; /* Address of regReturn init */
148536 int untestedTerms = 0; /* Some terms not completely tested */
148537 int ii; /* Loop counter */
148538 Expr *pAndExpr = 0; /* An ".. AND (...)" expression */
148539 Table *pTab = pTabItem->pTab;
148540
148541 pTerm = pLoop->aLTerm[0];
148542 assert( pTerm!=0 );
148543 assert( pTerm->eOperator & WO_OR );
148544 assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
148545 pOrWc = &pTerm->u.pOrInfo->wc;
148546 pLevel->op = OP_Return;
148547 pLevel->p1 = regReturn;
148548
148549 /* Set up a new SrcList in pOrTab containing the table being scanned
148550 ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
148551 ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
148552 */
148553 if( pWInfo->nLevel>1 ){
148554 int nNotReady; /* The number of notReady tables */
148555 SrcItem *origSrc; /* Original list of tables */
148556 nNotReady = pWInfo->nLevel - iLevel - 1;
148557 pOrTab = sqlite3StackAllocRaw(db,
148558 sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
148559 if( pOrTab==0 ) return notReady;
148560 pOrTab->nAlloc = (u8)(nNotReady + 1);
148561 pOrTab->nSrc = pOrTab->nAlloc;
148562 memcpy(dest: pOrTab->a, src: pTabItem, n: sizeof(*pTabItem));
148563 origSrc = pWInfo->pTabList->a;
148564 for(k=1; k<=nNotReady; k++){
148565 memcpy(dest: &pOrTab->a[k], src: &origSrc[pLevel[k].iFrom], n: sizeof(pOrTab->a[k]));
148566 }
148567 }else{
148568 pOrTab = pWInfo->pTabList;
148569 }
148570
148571 /* Initialize the rowset register to contain NULL. An SQL NULL is
148572 ** equivalent to an empty rowset. Or, create an ephemeral index
148573 ** capable of holding primary keys in the case of a WITHOUT ROWID.
148574 **
148575 ** Also initialize regReturn to contain the address of the instruction
148576 ** immediately following the OP_Return at the bottom of the loop. This
148577 ** is required in a few obscure LEFT JOIN cases where control jumps
148578 ** over the top of the loop into the body of it. In this case the
148579 ** correct response for the end-of-loop code (the OP_Return) is to
148580 ** fall through to the next instruction, just as an OP_Next does if
148581 ** called on an uninitialized cursor.
148582 */
148583 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
148584 if( HasRowid(pTab) ){
148585 regRowset = ++pParse->nMem;
148586 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: regRowset);
148587 }else{
148588 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
148589 regRowset = pParse->nTab++;
148590 sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: regRowset, p2: pPk->nKeyCol);
148591 sqlite3VdbeSetP4KeyInfo(pParse, pIdx: pPk);
148592 }
148593 regRowid = ++pParse->nMem;
148594 }
148595 iRetInit = sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: regReturn);
148596
148597 /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y
148598 ** Then for every term xN, evaluate as the subexpression: xN AND z
148599 ** That way, terms in y that are factored into the disjunction will
148600 ** be picked up by the recursive calls to sqlite3WhereBegin() below.
148601 **
148602 ** Actually, each subexpression is converted to "xN AND w" where w is
148603 ** the "interesting" terms of z - terms that did not originate in the
148604 ** ON or USING clause of a LEFT JOIN, and terms that are usable as
148605 ** indices.
148606 **
148607 ** This optimization also only applies if the (x1 OR x2 OR ...) term
148608 ** is not contained in the ON clause of a LEFT JOIN.
148609 ** See ticket http://www.sqlite.org/src/info/f2369304e4
148610 */
148611 if( pWC->nTerm>1 ){
148612 int iTerm;
148613 for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
148614 Expr *pExpr = pWC->a[iTerm].pExpr;
148615 if( &pWC->a[iTerm] == pTerm ) continue;
148616 testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
148617 testcase( pWC->a[iTerm].wtFlags & TERM_CODED );
148618 if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED))!=0 ) continue;
148619 if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
148620 testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
148621 pExpr = sqlite3ExprDup(db, p: pExpr, flags: 0);
148622 pAndExpr = sqlite3ExprAnd(pParse, pLeft: pAndExpr, pRight: pExpr);
148623 }
148624 if( pAndExpr ){
148625 /* The extra 0x10000 bit on the opcode is masked off and does not
148626 ** become part of the new Expr.op. However, it does make the
148627 ** op==TK_AND comparison inside of sqlite3PExpr() false, and this
148628 ** prevents sqlite3PExpr() from applying the AND short-circuit
148629 ** optimization, which we do not want here. */
148630 pAndExpr = sqlite3PExpr(pParse, TK_AND|0x10000, pLeft: 0, pRight: pAndExpr);
148631 }
148632 }
148633
148634 /* Run a separate WHERE clause for each term of the OR clause. After
148635 ** eliminating duplicates from other WHERE clauses, the action for each
148636 ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
148637 */
148638 ExplainQueryPlan((pParse, 1, "MULTI-INDEX OR"));
148639 for(ii=0; ii<pOrWc->nTerm; ii++){
148640 WhereTerm *pOrTerm = &pOrWc->a[ii];
148641 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
148642 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
148643 Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
148644 Expr *pDelete; /* Local copy of OR clause term */
148645 int jmp1 = 0; /* Address of jump operation */
148646 testcase( (pTabItem[0].fg.jointype & JT_LEFT)!=0
148647 && !ExprHasProperty(pOrExpr, EP_FromJoin)
148648 ); /* See TH3 vtab25.400 and ticket 614b25314c766238 */
148649 pDelete = pOrExpr = sqlite3ExprDup(db, p: pOrExpr, flags: 0);
148650 if( db->mallocFailed ){
148651 sqlite3ExprDelete(db, p: pDelete);
148652 continue;
148653 }
148654 if( pAndExpr ){
148655 pAndExpr->pLeft = pOrExpr;
148656 pOrExpr = pAndExpr;
148657 }
148658 /* Loop through table entries that match term pOrTerm. */
148659 ExplainQueryPlan((pParse, 1, "INDEX %d", ii+1));
148660 WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
148661 pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
148662 WHERE_OR_SUBCLAUSE, iCovCur);
148663 assert( pSubWInfo || pParse->nErr || db->mallocFailed );
148664 if( pSubWInfo ){
148665 WhereLoop *pSubLoop;
148666 int addrExplain = sqlite3WhereExplainOneScan(
148667 pParse, pTabList: pOrTab, pLevel: &pSubWInfo->a[0], wctrlFlags: 0
148668 );
148669 sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
148670
148671 /* This is the sub-WHERE clause body. First skip over
148672 ** duplicate rows from prior sub-WHERE clauses, and record the
148673 ** rowid (or PRIMARY KEY) for the current row so that the same
148674 ** row will be skipped in subsequent sub-WHERE clauses.
148675 */
148676 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
148677 int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
148678 if( HasRowid(pTab) ){
148679 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur: iCur, iCol: -1, regOut: regRowid);
148680 jmp1 = sqlite3VdbeAddOp4Int(p: v, OP_RowSetTest, p1: regRowset, p2: 0,
148681 p3: regRowid, p4: iSet);
148682 VdbeCoverage(v);
148683 }else{
148684 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
148685 int nPk = pPk->nKeyCol;
148686 int iPk;
148687 int r;
148688
148689 /* Read the PK into an array of temp registers. */
148690 r = sqlite3GetTempRange(pParse, nReg: nPk);
148691 for(iPk=0; iPk<nPk; iPk++){
148692 int iCol = pPk->aiColumn[iPk];
148693 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur: iCur, iCol,regOut: r+iPk);
148694 }
148695
148696 /* Check if the temp table already contains this key. If so,
148697 ** the row has already been included in the result set and
148698 ** can be ignored (by jumping past the Gosub below). Otherwise,
148699 ** insert the key into the temp table and proceed with processing
148700 ** the row.
148701 **
148702 ** Use some of the same optimizations as OP_RowSetTest: If iSet
148703 ** is zero, assume that the key cannot already be present in
148704 ** the temp table. And if iSet is -1, assume that there is no
148705 ** need to insert the key into the temp table, as it will never
148706 ** be tested for. */
148707 if( iSet ){
148708 jmp1 = sqlite3VdbeAddOp4Int(p: v, OP_Found, p1: regRowset, p2: 0, p3: r, p4: nPk);
148709 VdbeCoverage(v);
148710 }
148711 if( iSet>=0 ){
148712 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: r, p2: nPk, p3: regRowid);
148713 sqlite3VdbeAddOp4Int(p: v, OP_IdxInsert, p1: regRowset, p2: regRowid,
148714 p3: r, p4: nPk);
148715 if( iSet ) sqlite3VdbeChangeP5(p: v, OPFLAG_USESEEKRESULT);
148716 }
148717
148718 /* Release the array of temp registers */
148719 sqlite3ReleaseTempRange(pParse, iReg: r, nReg: nPk);
148720 }
148721 }
148722
148723 /* Invoke the main loop body as a subroutine */
148724 sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: regReturn, p2: iLoopBody);
148725
148726 /* Jump here (skipping the main loop body subroutine) if the
148727 ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
148728 if( jmp1 ) sqlite3VdbeJumpHere(p: v, addr: jmp1);
148729
148730 /* The pSubWInfo->untestedTerms flag means that this OR term
148731 ** contained one or more AND term from a notReady table. The
148732 ** terms from the notReady table could not be tested and will
148733 ** need to be tested later.
148734 */
148735 if( pSubWInfo->untestedTerms ) untestedTerms = 1;
148736
148737 /* If all of the OR-connected terms are optimized using the same
148738 ** index, and the index is opened using the same cursor number
148739 ** by each call to sqlite3WhereBegin() made by this loop, it may
148740 ** be possible to use that index as a covering index.
148741 **
148742 ** If the call to sqlite3WhereBegin() above resulted in a scan that
148743 ** uses an index, and this is either the first OR-connected term
148744 ** processed or the index is the same as that used by all previous
148745 ** terms, set pCov to the candidate covering index. Otherwise, set
148746 ** pCov to NULL to indicate that no candidate covering index will
148747 ** be available.
148748 */
148749 pSubLoop = pSubWInfo->a[0].pWLoop;
148750 assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
148751 if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
148752 && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
148753 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
148754 ){
148755 assert( pSubWInfo->a[0].iIdxCur==iCovCur );
148756 pCov = pSubLoop->u.btree.pIndex;
148757 }else{
148758 pCov = 0;
148759 }
148760 if( sqlite3WhereUsesDeferredSeek(pSubWInfo) ){
148761 pWInfo->bDeferredSeek = 1;
148762 }
148763
148764 /* Finish the loop through table entries that match term pOrTerm. */
148765 sqlite3WhereEnd(pSubWInfo);
148766 ExplainQueryPlanPop(pParse);
148767 }
148768 sqlite3ExprDelete(db, p: pDelete);
148769 }
148770 }
148771 ExplainQueryPlanPop(pParse);
148772 assert( pLevel->pWLoop==pLoop );
148773 assert( (pLoop->wsFlags & WHERE_MULTI_OR)!=0 );
148774 assert( (pLoop->wsFlags & WHERE_IN_ABLE)==0 );
148775 pLevel->u.pCoveringIdx = pCov;
148776 if( pCov ) pLevel->iIdxCur = iCovCur;
148777 if( pAndExpr ){
148778 pAndExpr->pLeft = 0;
148779 sqlite3ExprDelete(db, p: pAndExpr);
148780 }
148781 sqlite3VdbeChangeP1(p: v, addr: iRetInit, val: sqlite3VdbeCurrentAddr(p: v));
148782 sqlite3VdbeGoto(p: v, iDest: pLevel->addrBrk);
148783 sqlite3VdbeResolveLabel(v, x: iLoopBody);
148784
148785 if( pWInfo->nLevel>1 ){ sqlite3StackFree(db, pOrTab); }
148786 if( !untestedTerms ) disableTerm(pLevel, pTerm);
148787 }else
148788#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
148789
148790 {
148791 /* Case 6: There is no usable index. We must do a complete
148792 ** scan of the entire table.
148793 */
148794 static const u8 aStep[] = { OP_Next, OP_Prev };
148795 static const u8 aStart[] = { OP_Rewind, OP_Last };
148796 assert( bRev==0 || bRev==1 );
148797 if( pTabItem->fg.isRecursive ){
148798 /* Tables marked isRecursive have only a single row that is stored in
148799 ** a pseudo-cursor. No need to Rewind or Next such cursors. */
148800 pLevel->op = OP_Noop;
148801 }else{
148802 codeCursorHint(pTabItem, pWInfo, pLevel, 0);
148803 pLevel->op = aStep[bRev];
148804 pLevel->p1 = iCur;
148805 pLevel->p2 = 1 + sqlite3VdbeAddOp2(p: v, op: aStart[bRev], p1: iCur, p2: addrHalt);
148806 VdbeCoverageIf(v, bRev==0);
148807 VdbeCoverageIf(v, bRev!=0);
148808 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
148809 }
148810 }
148811
148812#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
148813 pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
148814#endif
148815
148816 /* Insert code to test every subexpression that can be completely
148817 ** computed using the current set of tables.
148818 **
148819 ** This loop may run between one and three times, depending on the
148820 ** constraints to be generated. The value of stack variable iLoop
148821 ** determines the constraints coded by each iteration, as follows:
148822 **
148823 ** iLoop==1: Code only expressions that are entirely covered by pIdx.
148824 ** iLoop==2: Code remaining expressions that do not contain correlated
148825 ** sub-queries.
148826 ** iLoop==3: Code all remaining expressions.
148827 **
148828 ** An effort is made to skip unnecessary iterations of the loop.
148829 */
148830 iLoop = (pIdx ? 1 : 2);
148831 do{
148832 int iNext = 0; /* Next value for iLoop */
148833 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
148834 Expr *pE;
148835 int skipLikeAddr = 0;
148836 testcase( pTerm->wtFlags & TERM_VIRTUAL );
148837 testcase( pTerm->wtFlags & TERM_CODED );
148838 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
148839 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
148840 testcase( pWInfo->untestedTerms==0
148841 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 );
148842 pWInfo->untestedTerms = 1;
148843 continue;
148844 }
148845 pE = pTerm->pExpr;
148846 assert( pE!=0 );
148847 if( (pTabItem->fg.jointype&JT_LEFT) && !ExprHasProperty(pE,EP_FromJoin) ){
148848 continue;
148849 }
148850
148851 if( iLoop==1 && !sqlite3ExprCoveredByIndex(pExpr: pE, iCur: pLevel->iTabCur, pIdx) ){
148852 iNext = 2;
148853 continue;
148854 }
148855 if( iLoop<3 && (pTerm->wtFlags & TERM_VARSELECT) ){
148856 if( iNext==0 ) iNext = 3;
148857 continue;
148858 }
148859
148860 if( (pTerm->wtFlags & TERM_LIKECOND)!=0 ){
148861 /* If the TERM_LIKECOND flag is set, that means that the range search
148862 ** is sufficient to guarantee that the LIKE operator is true, so we
148863 ** can skip the call to the like(A,B) function. But this only works
148864 ** for strings. So do not skip the call to the function on the pass
148865 ** that compares BLOBs. */
148866#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
148867 continue;
148868#else
148869 u32 x = pLevel->iLikeRepCntr;
148870 if( x>0 ){
148871 skipLikeAddr = sqlite3VdbeAddOp1(p: v, op: (x&1)?OP_IfNot:OP_If,p1: (int)(x>>1));
148872 VdbeCoverageIf(v, (x&1)==1);
148873 VdbeCoverageIf(v, (x&1)==0);
148874 }
148875#endif
148876 }
148877#ifdef WHERETRACE_ENABLED /* 0xffff */
148878 if( sqlite3WhereTrace ){
148879 VdbeNoopComment((v, "WhereTerm[%d] (%p) priority=%d",
148880 pWC->nTerm-j, pTerm, iLoop));
148881 }
148882 if( sqlite3WhereTrace & 0x800 ){
148883 sqlite3DebugPrintf("Coding auxiliary constraint:\n");
148884 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
148885 }
148886#endif
148887 sqlite3ExprIfFalse(pParse, pExpr: pE, dest: addrCont, SQLITE_JUMPIFNULL);
148888 if( skipLikeAddr ) sqlite3VdbeJumpHere(p: v, addr: skipLikeAddr);
148889 pTerm->wtFlags |= TERM_CODED;
148890 }
148891 iLoop = iNext;
148892 }while( iLoop>0 );
148893
148894 /* Insert code to test for implied constraints based on transitivity
148895 ** of the "==" operator.
148896 **
148897 ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
148898 ** and we are coding the t1 loop and the t2 loop has not yet coded,
148899 ** then we cannot use the "t1.a=t2.b" constraint, but we can code
148900 ** the implied "t1.a=123" constraint.
148901 */
148902 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
148903 Expr *pE, sEAlt;
148904 WhereTerm *pAlt;
148905 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
148906 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
148907 if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
148908 if( pTerm->leftCursor!=iCur ) continue;
148909 if( pTabItem->fg.jointype & JT_LEFT ) continue;
148910 pE = pTerm->pExpr;
148911#ifdef WHERETRACE_ENABLED /* 0x800 */
148912 if( sqlite3WhereTrace & 0x800 ){
148913 sqlite3DebugPrintf("Coding transitive constraint:\n");
148914 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
148915 }
148916#endif
148917 assert( !ExprHasProperty(pE, EP_FromJoin) );
148918 assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
148919 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
148920 pAlt = sqlite3WhereFindTerm(pWC, iCur, iColumn: pTerm->u.x.leftColumn, notReady,
148921 WO_EQ|WO_IN|WO_IS, pIdx: 0);
148922 if( pAlt==0 ) continue;
148923 if( pAlt->wtFlags & (TERM_CODED) ) continue;
148924 if( (pAlt->eOperator & WO_IN)
148925 && ExprUseXSelect(pAlt->pExpr)
148926 && (pAlt->pExpr->x.pSelect->pEList->nExpr>1)
148927 ){
148928 continue;
148929 }
148930 testcase( pAlt->eOperator & WO_EQ );
148931 testcase( pAlt->eOperator & WO_IS );
148932 testcase( pAlt->eOperator & WO_IN );
148933 VdbeModuleComment((v, "begin transitive constraint"));
148934 sEAlt = *pAlt->pExpr;
148935 sEAlt.pLeft = pE->pLeft;
148936 sqlite3ExprIfFalse(pParse, pExpr: &sEAlt, dest: addrCont, SQLITE_JUMPIFNULL);
148937 pAlt->wtFlags |= TERM_CODED;
148938 }
148939
148940 /* For a LEFT OUTER JOIN, generate code that will record the fact that
148941 ** at least one row of the right table has matched the left table.
148942 */
148943 if( pLevel->iLeftJoin ){
148944 pLevel->addrFirst = sqlite3VdbeCurrentAddr(p: v);
148945 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 1, p2: pLevel->iLeftJoin);
148946 VdbeComment((v, "record LEFT JOIN hit"));
148947 for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
148948 testcase( pTerm->wtFlags & TERM_VIRTUAL );
148949 testcase( pTerm->wtFlags & TERM_CODED );
148950 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
148951 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
148952 assert( pWInfo->untestedTerms );
148953 continue;
148954 }
148955 assert( pTerm->pExpr );
148956 sqlite3ExprIfFalse(pParse, pExpr: pTerm->pExpr, dest: addrCont, SQLITE_JUMPIFNULL);
148957 pTerm->wtFlags |= TERM_CODED;
148958 }
148959 }
148960
148961#if WHERETRACE_ENABLED /* 0x20800 */
148962 if( sqlite3WhereTrace & 0x20000 ){
148963 sqlite3DebugPrintf("All WHERE-clause terms after coding level %d:\n",
148964 iLevel);
148965 sqlite3WhereClausePrint(pWC);
148966 }
148967 if( sqlite3WhereTrace & 0x800 ){
148968 sqlite3DebugPrintf("End Coding level %d: notReady=%llx\n",
148969 iLevel, (u64)pLevel->notReady);
148970 }
148971#endif
148972 return pLevel->notReady;
148973}
148974
148975/************** End of wherecode.c *******************************************/
148976/************** Begin file whereexpr.c ***************************************/
148977/*
148978** 2015-06-08
148979**
148980** The author disclaims copyright to this source code. In place of
148981** a legal notice, here is a blessing:
148982**
148983** May you do good and not evil.
148984** May you find forgiveness for yourself and forgive others.
148985** May you share freely, never taking more than you give.
148986**
148987*************************************************************************
148988** This module contains C code that generates VDBE code used to process
148989** the WHERE clause of SQL statements.
148990**
148991** This file was originally part of where.c but was split out to improve
148992** readability and editabiliity. This file contains utility routines for
148993** analyzing Expr objects in the WHERE clause.
148994*/
148995/* #include "sqliteInt.h" */
148996/* #include "whereInt.h" */
148997
148998/* Forward declarations */
148999static void exprAnalyze(SrcList*, WhereClause*, int);
149000
149001/*
149002** Deallocate all memory associated with a WhereOrInfo object.
149003*/
149004static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
149005 sqlite3WhereClauseClear(&p->wc);
149006 sqlite3DbFree(db, p);
149007}
149008
149009/*
149010** Deallocate all memory associated with a WhereAndInfo object.
149011*/
149012static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
149013 sqlite3WhereClauseClear(&p->wc);
149014 sqlite3DbFree(db, p);
149015}
149016
149017/*
149018** Add a single new WhereTerm entry to the WhereClause object pWC.
149019** The new WhereTerm object is constructed from Expr p and with wtFlags.
149020** The index in pWC->a[] of the new WhereTerm is returned on success.
149021** 0 is returned if the new WhereTerm could not be added due to a memory
149022** allocation error. The memory allocation failure will be recorded in
149023** the db->mallocFailed flag so that higher-level functions can detect it.
149024**
149025** This routine will increase the size of the pWC->a[] array as necessary.
149026**
149027** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
149028** for freeing the expression p is assumed by the WhereClause object pWC.
149029** This is true even if this routine fails to allocate a new WhereTerm.
149030**
149031** WARNING: This routine might reallocate the space used to store
149032** WhereTerms. All pointers to WhereTerms should be invalidated after
149033** calling this routine. Such pointers may be reinitialized by referencing
149034** the pWC->a[] array.
149035*/
149036static int whereClauseInsert(WhereClause *pWC, Expr *p, u16 wtFlags){
149037 WhereTerm *pTerm;
149038 int idx;
149039 testcase( wtFlags & TERM_VIRTUAL );
149040 if( pWC->nTerm>=pWC->nSlot ){
149041 WhereTerm *pOld = pWC->a;
149042 sqlite3 *db = pWC->pWInfo->pParse->db;
149043 pWC->a = sqlite3DbMallocRawNN(db, n: sizeof(pWC->a[0])*pWC->nSlot*2 );
149044 if( pWC->a==0 ){
149045 if( wtFlags & TERM_DYNAMIC ){
149046 sqlite3ExprDelete(db, p);
149047 }
149048 pWC->a = pOld;
149049 return 0;
149050 }
149051 memcpy(dest: pWC->a, src: pOld, n: sizeof(pWC->a[0])*pWC->nTerm);
149052 if( pOld!=pWC->aStatic ){
149053 sqlite3DbFree(db, p: pOld);
149054 }
149055 pWC->nSlot = sqlite3DbMallocSize(db, p: pWC->a)/sizeof(pWC->a[0]);
149056 }
149057 pTerm = &pWC->a[idx = pWC->nTerm++];
149058 if( p && ExprHasProperty(p, EP_Unlikely) ){
149059 pTerm->truthProb = sqlite3LogEst(x: p->iTable) - 270;
149060 }else{
149061 pTerm->truthProb = 1;
149062 }
149063 pTerm->pExpr = sqlite3ExprSkipCollateAndLikely(pExpr: p);
149064 pTerm->wtFlags = wtFlags;
149065 pTerm->pWC = pWC;
149066 pTerm->iParent = -1;
149067 memset(s: &pTerm->eOperator, c: 0,
149068 n: sizeof(WhereTerm) - offsetof(WhereTerm,eOperator));
149069 return idx;
149070}
149071
149072/*
149073** Return TRUE if the given operator is one of the operators that is
149074** allowed for an indexable WHERE clause term. The allowed operators are
149075** "=", "<", ">", "<=", ">=", "IN", "IS", and "IS NULL"
149076*/
149077static int allowedOp(int op){
149078 assert( TK_GT>TK_EQ && TK_GT<TK_GE );
149079 assert( TK_LT>TK_EQ && TK_LT<TK_GE );
149080 assert( TK_LE>TK_EQ && TK_LE<TK_GE );
149081 assert( TK_GE==TK_EQ+4 );
149082 return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL || op==TK_IS;
149083}
149084
149085/*
149086** Commute a comparison operator. Expressions of the form "X op Y"
149087** are converted into "Y op X".
149088*/
149089static u16 exprCommute(Parse *pParse, Expr *pExpr){
149090 if( pExpr->pLeft->op==TK_VECTOR
149091 || pExpr->pRight->op==TK_VECTOR
149092 || sqlite3BinaryCompareCollSeq(pParse, pLeft: pExpr->pLeft, pRight: pExpr->pRight) !=
149093 sqlite3BinaryCompareCollSeq(pParse, pLeft: pExpr->pRight, pRight: pExpr->pLeft)
149094 ){
149095 pExpr->flags ^= EP_Commuted;
149096 }
149097 SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
149098 if( pExpr->op>=TK_GT ){
149099 assert( TK_LT==TK_GT+2 );
149100 assert( TK_GE==TK_LE+2 );
149101 assert( TK_GT>TK_EQ );
149102 assert( TK_GT<TK_LE );
149103 assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
149104 pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
149105 }
149106 return 0;
149107}
149108
149109/*
149110** Translate from TK_xx operator to WO_xx bitmask.
149111*/
149112static u16 operatorMask(int op){
149113 u16 c;
149114 assert( allowedOp(op) );
149115 if( op==TK_IN ){
149116 c = WO_IN;
149117 }else if( op==TK_ISNULL ){
149118 c = WO_ISNULL;
149119 }else if( op==TK_IS ){
149120 c = WO_IS;
149121 }else{
149122 assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
149123 c = (u16)(WO_EQ<<(op-TK_EQ));
149124 }
149125 assert( op!=TK_ISNULL || c==WO_ISNULL );
149126 assert( op!=TK_IN || c==WO_IN );
149127 assert( op!=TK_EQ || c==WO_EQ );
149128 assert( op!=TK_LT || c==WO_LT );
149129 assert( op!=TK_LE || c==WO_LE );
149130 assert( op!=TK_GT || c==WO_GT );
149131 assert( op!=TK_GE || c==WO_GE );
149132 assert( op!=TK_IS || c==WO_IS );
149133 return c;
149134}
149135
149136
149137#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
149138/*
149139** Check to see if the given expression is a LIKE or GLOB operator that
149140** can be optimized using inequality constraints. Return TRUE if it is
149141** so and false if not.
149142**
149143** In order for the operator to be optimizible, the RHS must be a string
149144** literal that does not begin with a wildcard. The LHS must be a column
149145** that may only be NULL, a string, or a BLOB, never a number. (This means
149146** that virtual tables cannot participate in the LIKE optimization.) The
149147** collating sequence for the column on the LHS must be appropriate for
149148** the operator.
149149*/
149150static int isLikeOrGlob(
149151 Parse *pParse, /* Parsing and code generating context */
149152 Expr *pExpr, /* Test this expression */
149153 Expr **ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */
149154 int *pisComplete, /* True if the only wildcard is % in the last character */
149155 int *pnoCase /* True if uppercase is equivalent to lowercase */
149156){
149157 const u8 *z = 0; /* String on RHS of LIKE operator */
149158 Expr *pRight, *pLeft; /* Right and left size of LIKE operator */
149159 ExprList *pList; /* List of operands to the LIKE operator */
149160 u8 c; /* One character in z[] */
149161 int cnt; /* Number of non-wildcard prefix characters */
149162 u8 wc[4]; /* Wildcard characters */
149163 sqlite3 *db = pParse->db; /* Database connection */
149164 sqlite3_value *pVal = 0;
149165 int op; /* Opcode of pRight */
149166 int rc; /* Result code to return */
149167
149168 if( !sqlite3IsLikeFunction(db, pExpr, pIsNocase: pnoCase, aWc: (char*)wc) ){
149169 return 0;
149170 }
149171#ifdef SQLITE_EBCDIC
149172 if( *pnoCase ) return 0;
149173#endif
149174 assert( ExprUseXList(pExpr) );
149175 pList = pExpr->x.pList;
149176 pLeft = pList->a[1].pExpr;
149177
149178 pRight = sqlite3ExprSkipCollate(pExpr: pList->a[0].pExpr);
149179 op = pRight->op;
149180 if( op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
149181 Vdbe *pReprepare = pParse->pReprepare;
149182 int iCol = pRight->iColumn;
149183 pVal = sqlite3VdbeGetBoundValue(v: pReprepare, iVar: iCol, SQLITE_AFF_BLOB);
149184 if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
149185 z = sqlite3_value_text(pVal);
149186 }
149187 sqlite3VdbeSetVarmask(v: pParse->pVdbe, iVar: iCol);
149188 assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
149189 }else if( op==TK_STRING ){
149190 assert( !ExprHasProperty(pRight, EP_IntValue) );
149191 z = (u8*)pRight->u.zToken;
149192 }
149193 if( z ){
149194
149195 /* Count the number of prefix characters prior to the first wildcard */
149196 cnt = 0;
149197 while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
149198 cnt++;
149199 if( c==wc[3] && z[cnt]!=0 ) cnt++;
149200 }
149201
149202 /* The optimization is possible only if (1) the pattern does not begin
149203 ** with a wildcard and if (2) the non-wildcard prefix does not end with
149204 ** an (illegal 0xff) character, or (3) the pattern does not consist of
149205 ** a single escape character. The second condition is necessary so
149206 ** that we can increment the prefix key to find an upper bound for the
149207 ** range search. The third is because the caller assumes that the pattern
149208 ** consists of at least one character after all escapes have been
149209 ** removed. */
149210 if( cnt!=0 && 255!=(u8)z[cnt-1] && (cnt>1 || z[0]!=wc[3]) ){
149211 Expr *pPrefix;
149212
149213 /* A "complete" match if the pattern ends with "*" or "%" */
149214 *pisComplete = c==wc[0] && z[cnt+1]==0;
149215
149216 /* Get the pattern prefix. Remove all escapes from the prefix. */
149217 pPrefix = sqlite3Expr(db, TK_STRING, zToken: (char*)z);
149218 if( pPrefix ){
149219 int iFrom, iTo;
149220 char *zNew;
149221 assert( !ExprHasProperty(pPrefix, EP_IntValue) );
149222 zNew = pPrefix->u.zToken;
149223 zNew[cnt] = 0;
149224 for(iFrom=iTo=0; iFrom<cnt; iFrom++){
149225 if( zNew[iFrom]==wc[3] ) iFrom++;
149226 zNew[iTo++] = zNew[iFrom];
149227 }
149228 zNew[iTo] = 0;
149229 assert( iTo>0 );
149230
149231 /* If the LHS is not an ordinary column with TEXT affinity, then the
149232 ** pattern prefix boundaries (both the start and end boundaries) must
149233 ** not look like a number. Otherwise the pattern might be treated as
149234 ** a number, which will invalidate the LIKE optimization.
149235 **
149236 ** Getting this right has been a persistent source of bugs in the
149237 ** LIKE optimization. See, for example:
149238 ** 2018-09-10 https://sqlite.org/src/info/c94369cae9b561b1
149239 ** 2019-05-02 https://sqlite.org/src/info/b043a54c3de54b28
149240 ** 2019-06-10 https://sqlite.org/src/info/fd76310a5e843e07
149241 ** 2019-06-14 https://sqlite.org/src/info/ce8717f0885af975
149242 ** 2019-09-03 https://sqlite.org/src/info/0f0428096f17252a
149243 */
149244 if( pLeft->op!=TK_COLUMN
149245 || sqlite3ExprAffinity(pExpr: pLeft)!=SQLITE_AFF_TEXT
149246 || (ALWAYS( ExprUseYTab(pLeft) )
149247 && pLeft->y.pTab
149248 && IsVirtual(pLeft->y.pTab)) /* Might be numeric */
149249 ){
149250 int isNum;
149251 double rDummy;
149252 isNum = sqlite3AtoF(z: zNew, pResult: &rDummy, length: iTo, SQLITE_UTF8);
149253 if( isNum<=0 ){
149254 if( iTo==1 && zNew[0]=='-' ){
149255 isNum = +1;
149256 }else{
149257 zNew[iTo-1]++;
149258 isNum = sqlite3AtoF(z: zNew, pResult: &rDummy, length: iTo, SQLITE_UTF8);
149259 zNew[iTo-1]--;
149260 }
149261 }
149262 if( isNum>0 ){
149263 sqlite3ExprDelete(db, p: pPrefix);
149264 sqlite3ValueFree(v: pVal);
149265 return 0;
149266 }
149267 }
149268 }
149269 *ppPrefix = pPrefix;
149270
149271 /* If the RHS pattern is a bound parameter, make arrangements to
149272 ** reprepare the statement when that parameter is rebound */
149273 if( op==TK_VARIABLE ){
149274 Vdbe *v = pParse->pVdbe;
149275 sqlite3VdbeSetVarmask(v, iVar: pRight->iColumn);
149276 assert( !ExprHasProperty(pRight, EP_IntValue) );
149277 if( *pisComplete && pRight->u.zToken[1] ){
149278 /* If the rhs of the LIKE expression is a variable, and the current
149279 ** value of the variable means there is no need to invoke the LIKE
149280 ** function, then no OP_Variable will be added to the program.
149281 ** This causes problems for the sqlite3_bind_parameter_name()
149282 ** API. To work around them, add a dummy OP_Variable here.
149283 */
149284 int r1 = sqlite3GetTempReg(pParse);
149285 sqlite3ExprCodeTarget(pParse, pExpr: pRight, target: r1);
149286 sqlite3VdbeChangeP3(p: v, addr: sqlite3VdbeCurrentAddr(p: v)-1, val: 0);
149287 sqlite3ReleaseTempReg(pParse, iReg: r1);
149288 }
149289 }
149290 }else{
149291 z = 0;
149292 }
149293 }
149294
149295 rc = (z!=0);
149296 sqlite3ValueFree(v: pVal);
149297 return rc;
149298}
149299#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
149300
149301
149302#ifndef SQLITE_OMIT_VIRTUALTABLE
149303/*
149304** Check to see if the pExpr expression is a form that needs to be passed
149305** to the xBestIndex method of virtual tables. Forms of interest include:
149306**
149307** Expression Virtual Table Operator
149308** ----------------------- ---------------------------------
149309** 1. column MATCH expr SQLITE_INDEX_CONSTRAINT_MATCH
149310** 2. column GLOB expr SQLITE_INDEX_CONSTRAINT_GLOB
149311** 3. column LIKE expr SQLITE_INDEX_CONSTRAINT_LIKE
149312** 4. column REGEXP expr SQLITE_INDEX_CONSTRAINT_REGEXP
149313** 5. column != expr SQLITE_INDEX_CONSTRAINT_NE
149314** 6. expr != column SQLITE_INDEX_CONSTRAINT_NE
149315** 7. column IS NOT expr SQLITE_INDEX_CONSTRAINT_ISNOT
149316** 8. expr IS NOT column SQLITE_INDEX_CONSTRAINT_ISNOT
149317** 9. column IS NOT NULL SQLITE_INDEX_CONSTRAINT_ISNOTNULL
149318**
149319** In every case, "column" must be a column of a virtual table. If there
149320** is a match, set *ppLeft to the "column" expression, set *ppRight to the
149321** "expr" expression (even though in forms (6) and (8) the column is on the
149322** right and the expression is on the left). Also set *peOp2 to the
149323** appropriate virtual table operator. The return value is 1 or 2 if there
149324** is a match. The usual return is 1, but if the RHS is also a column
149325** of virtual table in forms (5) or (7) then return 2.
149326**
149327** If the expression matches none of the patterns above, return 0.
149328*/
149329static int isAuxiliaryVtabOperator(
149330 sqlite3 *db, /* Parsing context */
149331 Expr *pExpr, /* Test this expression */
149332 unsigned char *peOp2, /* OUT: 0 for MATCH, or else an op2 value */
149333 Expr **ppLeft, /* Column expression to left of MATCH/op2 */
149334 Expr **ppRight /* Expression to left of MATCH/op2 */
149335){
149336 if( pExpr->op==TK_FUNCTION ){
149337 static const struct Op2 {
149338 const char *zOp;
149339 unsigned char eOp2;
149340 } aOp[] = {
149341 { "match", SQLITE_INDEX_CONSTRAINT_MATCH },
149342 { "glob", SQLITE_INDEX_CONSTRAINT_GLOB },
149343 { "like", SQLITE_INDEX_CONSTRAINT_LIKE },
149344 { "regexp", SQLITE_INDEX_CONSTRAINT_REGEXP }
149345 };
149346 ExprList *pList;
149347 Expr *pCol; /* Column reference */
149348 int i;
149349
149350 assert( ExprUseXList(pExpr) );
149351 pList = pExpr->x.pList;
149352 if( pList==0 || pList->nExpr!=2 ){
149353 return 0;
149354 }
149355
149356 /* Built-in operators MATCH, GLOB, LIKE, and REGEXP attach to a
149357 ** virtual table on their second argument, which is the same as
149358 ** the left-hand side operand in their in-fix form.
149359 **
149360 ** vtab_column MATCH expression
149361 ** MATCH(expression,vtab_column)
149362 */
149363 pCol = pList->a[1].pExpr;
149364 assert( pCol->op!=TK_COLUMN || ExprUseYTab(pCol) );
149365 testcase( pCol->op==TK_COLUMN && pCol->y.pTab==0 );
149366 if( ExprIsVtab(pCol) ){
149367 for(i=0; i<ArraySize(aOp); i++){
149368 assert( !ExprHasProperty(pExpr, EP_IntValue) );
149369 if( sqlite3StrICmp(zLeft: pExpr->u.zToken, zRight: aOp[i].zOp)==0 ){
149370 *peOp2 = aOp[i].eOp2;
149371 *ppRight = pList->a[0].pExpr;
149372 *ppLeft = pCol;
149373 return 1;
149374 }
149375 }
149376 }
149377
149378 /* We can also match against the first column of overloaded
149379 ** functions where xFindFunction returns a value of at least
149380 ** SQLITE_INDEX_CONSTRAINT_FUNCTION.
149381 **
149382 ** OVERLOADED(vtab_column,expression)
149383 **
149384 ** Historically, xFindFunction expected to see lower-case function
149385 ** names. But for this use case, xFindFunction is expected to deal
149386 ** with function names in an arbitrary case.
149387 */
149388 pCol = pList->a[0].pExpr;
149389 assert( pCol->op!=TK_COLUMN || ExprUseYTab(pCol) );
149390 testcase( pCol->op==TK_COLUMN && pCol->y.pTab==0 );
149391 if( ExprIsVtab(pCol) ){
149392 sqlite3_vtab *pVtab;
149393 sqlite3_module *pMod;
149394 void (*xNotUsed)(sqlite3_context*,int,sqlite3_value**);
149395 void *pNotUsed;
149396 pVtab = sqlite3GetVTable(db, pTab: pCol->y.pTab)->pVtab;
149397 assert( pVtab!=0 );
149398 assert( pVtab->pModule!=0 );
149399 assert( !ExprHasProperty(pExpr, EP_IntValue) );
149400 pMod = (sqlite3_module *)pVtab->pModule;
149401 if( pMod->xFindFunction!=0 ){
149402 i = pMod->xFindFunction(pVtab,2, pExpr->u.zToken, &xNotUsed, &pNotUsed);
149403 if( i>=SQLITE_INDEX_CONSTRAINT_FUNCTION ){
149404 *peOp2 = i;
149405 *ppRight = pList->a[1].pExpr;
149406 *ppLeft = pCol;
149407 return 1;
149408 }
149409 }
149410 }
149411 }else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){
149412 int res = 0;
149413 Expr *pLeft = pExpr->pLeft;
149414 Expr *pRight = pExpr->pRight;
149415 assert( pLeft->op!=TK_COLUMN || ExprUseYTab(pLeft) );
149416 testcase( pLeft->op==TK_COLUMN && pLeft->y.pTab==0 );
149417 if( ExprIsVtab(pLeft) ){
149418 res++;
149419 }
149420 assert( pRight==0 || pRight->op!=TK_COLUMN || ExprUseYTab(pRight) );
149421 testcase( pRight && pRight->op==TK_COLUMN && pRight->y.pTab==0 );
149422 if( pRight && ExprIsVtab(pRight) ){
149423 res++;
149424 SWAP(Expr*, pLeft, pRight);
149425 }
149426 *ppLeft = pLeft;
149427 *ppRight = pRight;
149428 if( pExpr->op==TK_NE ) *peOp2 = SQLITE_INDEX_CONSTRAINT_NE;
149429 if( pExpr->op==TK_ISNOT ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOT;
149430 if( pExpr->op==TK_NOTNULL ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOTNULL;
149431 return res;
149432 }
149433 return 0;
149434}
149435#endif /* SQLITE_OMIT_VIRTUALTABLE */
149436
149437/*
149438** If the pBase expression originated in the ON or USING clause of
149439** a join, then transfer the appropriate markings over to derived.
149440*/
149441static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
149442 if( pDerived ){
149443 pDerived->flags |= pBase->flags & EP_FromJoin;
149444 pDerived->iRightJoinTable = pBase->iRightJoinTable;
149445 }
149446}
149447
149448/*
149449** Mark term iChild as being a child of term iParent
149450*/
149451static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){
149452 pWC->a[iChild].iParent = iParent;
149453 pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
149454 pWC->a[iParent].nChild++;
149455}
149456
149457/*
149458** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not
149459** a conjunction, then return just pTerm when N==0. If N is exceeds
149460** the number of available subterms, return NULL.
149461*/
149462static WhereTerm *whereNthSubterm(WhereTerm *pTerm, int N){
149463 if( pTerm->eOperator!=WO_AND ){
149464 return N==0 ? pTerm : 0;
149465 }
149466 if( N<pTerm->u.pAndInfo->wc.nTerm ){
149467 return &pTerm->u.pAndInfo->wc.a[N];
149468 }
149469 return 0;
149470}
149471
149472/*
149473** Subterms pOne and pTwo are contained within WHERE clause pWC. The
149474** two subterms are in disjunction - they are OR-ed together.
149475**
149476** If these two terms are both of the form: "A op B" with the same
149477** A and B values but different operators and if the operators are
149478** compatible (if one is = and the other is <, for example) then
149479** add a new virtual AND term to pWC that is the combination of the
149480** two.
149481**
149482** Some examples:
149483**
149484** x<y OR x=y --> x<=y
149485** x=y OR x=y --> x=y
149486** x<=y OR x<y --> x<=y
149487**
149488** The following is NOT generated:
149489**
149490** x<y OR x>y --> x!=y
149491*/
149492static void whereCombineDisjuncts(
149493 SrcList *pSrc, /* the FROM clause */
149494 WhereClause *pWC, /* The complete WHERE clause */
149495 WhereTerm *pOne, /* First disjunct */
149496 WhereTerm *pTwo /* Second disjunct */
149497){
149498 u16 eOp = pOne->eOperator | pTwo->eOperator;
149499 sqlite3 *db; /* Database connection (for malloc) */
149500 Expr *pNew; /* New virtual expression */
149501 int op; /* Operator for the combined expression */
149502 int idxNew; /* Index in pWC of the next virtual term */
149503
149504 if( (pOne->wtFlags | pTwo->wtFlags) & TERM_VNULL ) return;
149505 if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
149506 if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
149507 if( (eOp & (WO_EQ|WO_LT|WO_LE))!=eOp
149508 && (eOp & (WO_EQ|WO_GT|WO_GE))!=eOp ) return;
149509 assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
149510 assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
149511 if( sqlite3ExprCompare(pParse: 0,pA: pOne->pExpr->pLeft, pB: pTwo->pExpr->pLeft, iTab: -1) ) return;
149512 if( sqlite3ExprCompare(pParse: 0,pA: pOne->pExpr->pRight, pB: pTwo->pExpr->pRight,iTab: -1) )return;
149513 /* If we reach this point, it means the two subterms can be combined */
149514 if( (eOp & (eOp-1))!=0 ){
149515 if( eOp & (WO_LT|WO_LE) ){
149516 eOp = WO_LE;
149517 }else{
149518 assert( eOp & (WO_GT|WO_GE) );
149519 eOp = WO_GE;
149520 }
149521 }
149522 db = pWC->pWInfo->pParse->db;
149523 pNew = sqlite3ExprDup(db, p: pOne->pExpr, flags: 0);
149524 if( pNew==0 ) return;
149525 for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); }
149526 pNew->op = op;
149527 idxNew = whereClauseInsert(pWC, p: pNew, TERM_VIRTUAL|TERM_DYNAMIC);
149528 exprAnalyze(pSrc, pWC, idxNew);
149529}
149530
149531#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
149532/*
149533** Analyze a term that consists of two or more OR-connected
149534** subterms. So in:
149535**
149536** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
149537** ^^^^^^^^^^^^^^^^^^^^
149538**
149539** This routine analyzes terms such as the middle term in the above example.
149540** A WhereOrTerm object is computed and attached to the term under
149541** analysis, regardless of the outcome of the analysis. Hence:
149542**
149543** WhereTerm.wtFlags |= TERM_ORINFO
149544** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object
149545**
149546** The term being analyzed must have two or more of OR-connected subterms.
149547** A single subterm might be a set of AND-connected sub-subterms.
149548** Examples of terms under analysis:
149549**
149550** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
149551** (B) x=expr1 OR expr2=x OR x=expr3
149552** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
149553** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
149554** (E) (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
149555** (F) x>A OR (x=A AND y>=B)
149556**
149557** CASE 1:
149558**
149559** If all subterms are of the form T.C=expr for some single column of C and
149560** a single table T (as shown in example B above) then create a new virtual
149561** term that is an equivalent IN expression. In other words, if the term
149562** being analyzed is:
149563**
149564** x = expr1 OR expr2 = x OR x = expr3
149565**
149566** then create a new virtual term like this:
149567**
149568** x IN (expr1,expr2,expr3)
149569**
149570** CASE 2:
149571**
149572** If there are exactly two disjuncts and one side has x>A and the other side
149573** has x=A (for the same x and A) then add a new virtual conjunct term to the
149574** WHERE clause of the form "x>=A". Example:
149575**
149576** x>A OR (x=A AND y>B) adds: x>=A
149577**
149578** The added conjunct can sometimes be helpful in query planning.
149579**
149580** CASE 3:
149581**
149582** If all subterms are indexable by a single table T, then set
149583**
149584** WhereTerm.eOperator = WO_OR
149585** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T
149586**
149587** A subterm is "indexable" if it is of the form
149588** "T.C <op> <expr>" where C is any column of table T and
149589** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
149590** A subterm is also indexable if it is an AND of two or more
149591** subsubterms at least one of which is indexable. Indexable AND
149592** subterms have their eOperator set to WO_AND and they have
149593** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
149594**
149595** From another point of view, "indexable" means that the subterm could
149596** potentially be used with an index if an appropriate index exists.
149597** This analysis does not consider whether or not the index exists; that
149598** is decided elsewhere. This analysis only looks at whether subterms
149599** appropriate for indexing exist.
149600**
149601** All examples A through E above satisfy case 3. But if a term
149602** also satisfies case 1 (such as B) we know that the optimizer will
149603** always prefer case 1, so in that case we pretend that case 3 is not
149604** satisfied.
149605**
149606** It might be the case that multiple tables are indexable. For example,
149607** (E) above is indexable on tables P, Q, and R.
149608**
149609** Terms that satisfy case 3 are candidates for lookup by using
149610** separate indices to find rowids for each subterm and composing
149611** the union of all rowids using a RowSet object. This is similar
149612** to "bitmap indices" in other database engines.
149613**
149614** OTHERWISE:
149615**
149616** If none of cases 1, 2, or 3 apply, then leave the eOperator set to
149617** zero. This term is not useful for search.
149618*/
149619static void exprAnalyzeOrTerm(
149620 SrcList *pSrc, /* the FROM clause */
149621 WhereClause *pWC, /* the complete WHERE clause */
149622 int idxTerm /* Index of the OR-term to be analyzed */
149623){
149624 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
149625 Parse *pParse = pWInfo->pParse; /* Parser context */
149626 sqlite3 *db = pParse->db; /* Database connection */
149627 WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */
149628 Expr *pExpr = pTerm->pExpr; /* The expression of the term */
149629 int i; /* Loop counters */
149630 WhereClause *pOrWc; /* Breakup of pTerm into subterms */
149631 WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */
149632 WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */
149633 Bitmask chngToIN; /* Tables that might satisfy case 1 */
149634 Bitmask indexable; /* Tables that are indexable, satisfying case 2 */
149635
149636 /*
149637 ** Break the OR clause into its separate subterms. The subterms are
149638 ** stored in a WhereClause structure containing within the WhereOrInfo
149639 ** object that is attached to the original OR clause term.
149640 */
149641 assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
149642 assert( pExpr->op==TK_OR );
149643 pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, n: sizeof(*pOrInfo));
149644 if( pOrInfo==0 ) return;
149645 pTerm->wtFlags |= TERM_ORINFO;
149646 pOrWc = &pOrInfo->wc;
149647 memset(s: pOrWc->aStatic, c: 0, n: sizeof(pOrWc->aStatic));
149648 sqlite3WhereClauseInit(pOrWc, pWInfo);
149649 sqlite3WhereSplit(pOrWc, pExpr, TK_OR);
149650 sqlite3WhereExprAnalyze(pSrc, pOrWc);
149651 if( db->mallocFailed ) return;
149652 assert( pOrWc->nTerm>=2 );
149653
149654 /*
149655 ** Compute the set of tables that might satisfy cases 1 or 3.
149656 */
149657 indexable = ~(Bitmask)0;
149658 chngToIN = ~(Bitmask)0;
149659 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
149660 if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
149661 WhereAndInfo *pAndInfo;
149662 assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
149663 chngToIN = 0;
149664 pAndInfo = sqlite3DbMallocRawNN(db, n: sizeof(*pAndInfo));
149665 if( pAndInfo ){
149666 WhereClause *pAndWC;
149667 WhereTerm *pAndTerm;
149668 int j;
149669 Bitmask b = 0;
149670 pOrTerm->u.pAndInfo = pAndInfo;
149671 pOrTerm->wtFlags |= TERM_ANDINFO;
149672 pOrTerm->eOperator = WO_AND;
149673 pOrTerm->leftCursor = -1;
149674 pAndWC = &pAndInfo->wc;
149675 memset(s: pAndWC->aStatic, c: 0, n: sizeof(pAndWC->aStatic));
149676 sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
149677 sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
149678 sqlite3WhereExprAnalyze(pSrc, pAndWC);
149679 pAndWC->pOuter = pWC;
149680 if( !db->mallocFailed ){
149681 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
149682 assert( pAndTerm->pExpr );
149683 if( allowedOp(op: pAndTerm->pExpr->op)
149684 || pAndTerm->eOperator==WO_AUX
149685 ){
149686 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
149687 }
149688 }
149689 }
149690 indexable &= b;
149691 }
149692 }else if( pOrTerm->wtFlags & TERM_COPIED ){
149693 /* Skip this term for now. We revisit it when we process the
149694 ** corresponding TERM_VIRTUAL term */
149695 }else{
149696 Bitmask b;
149697 b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
149698 if( pOrTerm->wtFlags & TERM_VIRTUAL ){
149699 WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
149700 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
149701 }
149702 indexable &= b;
149703 if( (pOrTerm->eOperator & WO_EQ)==0 ){
149704 chngToIN = 0;
149705 }else{
149706 chngToIN &= b;
149707 }
149708 }
149709 }
149710
149711 /*
149712 ** Record the set of tables that satisfy case 3. The set might be
149713 ** empty.
149714 */
149715 pOrInfo->indexable = indexable;
149716 pTerm->eOperator = WO_OR;
149717 pTerm->leftCursor = -1;
149718 if( indexable ){
149719 pWC->hasOr = 1;
149720 }
149721
149722 /* For a two-way OR, attempt to implementation case 2.
149723 */
149724 if( indexable && pOrWc->nTerm==2 ){
149725 int iOne = 0;
149726 WhereTerm *pOne;
149727 while( (pOne = whereNthSubterm(pTerm: &pOrWc->a[0],N: iOne++))!=0 ){
149728 int iTwo = 0;
149729 WhereTerm *pTwo;
149730 while( (pTwo = whereNthSubterm(pTerm: &pOrWc->a[1],N: iTwo++))!=0 ){
149731 whereCombineDisjuncts(pSrc, pWC, pOne, pTwo);
149732 }
149733 }
149734 }
149735
149736 /*
149737 ** chngToIN holds a set of tables that *might* satisfy case 1. But
149738 ** we have to do some additional checking to see if case 1 really
149739 ** is satisfied.
149740 **
149741 ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means
149742 ** that there is no possibility of transforming the OR clause into an
149743 ** IN operator because one or more terms in the OR clause contain
149744 ** something other than == on a column in the single table. The 1-bit
149745 ** case means that every term of the OR clause is of the form
149746 ** "table.column=expr" for some single table. The one bit that is set
149747 ** will correspond to the common table. We still need to check to make
149748 ** sure the same column is used on all terms. The 2-bit case is when
149749 ** the all terms are of the form "table1.column=table2.column". It
149750 ** might be possible to form an IN operator with either table1.column
149751 ** or table2.column as the LHS if either is common to every term of
149752 ** the OR clause.
149753 **
149754 ** Note that terms of the form "table.column1=table.column2" (the
149755 ** same table on both sizes of the ==) cannot be optimized.
149756 */
149757 if( chngToIN ){
149758 int okToChngToIN = 0; /* True if the conversion to IN is valid */
149759 int iColumn = -1; /* Column index on lhs of IN operator */
149760 int iCursor = -1; /* Table cursor common to all terms */
149761 int j = 0; /* Loop counter */
149762
149763 /* Search for a table and column that appears on one side or the
149764 ** other of the == operator in every subterm. That table and column
149765 ** will be recorded in iCursor and iColumn. There might not be any
149766 ** such table and column. Set okToChngToIN if an appropriate table
149767 ** and column is found but leave okToChngToIN false if not found.
149768 */
149769 for(j=0; j<2 && !okToChngToIN; j++){
149770 Expr *pLeft = 0;
149771 pOrTerm = pOrWc->a;
149772 for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
149773 assert( pOrTerm->eOperator & WO_EQ );
149774 pOrTerm->wtFlags &= ~TERM_OR_OK;
149775 if( pOrTerm->leftCursor==iCursor ){
149776 /* This is the 2-bit case and we are on the second iteration and
149777 ** current term is from the first iteration. So skip this term. */
149778 assert( j==1 );
149779 continue;
149780 }
149781 if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
149782 pOrTerm->leftCursor))==0 ){
149783 /* This term must be of the form t1.a==t2.b where t2 is in the
149784 ** chngToIN set but t1 is not. This term will be either preceded
149785 ** or follwed by an inverted copy (t2.b==t1.a). Skip this term
149786 ** and use its inversion. */
149787 testcase( pOrTerm->wtFlags & TERM_COPIED );
149788 testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
149789 assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
149790 continue;
149791 }
149792 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
149793 iColumn = pOrTerm->u.x.leftColumn;
149794 iCursor = pOrTerm->leftCursor;
149795 pLeft = pOrTerm->pExpr->pLeft;
149796 break;
149797 }
149798 if( i<0 ){
149799 /* No candidate table+column was found. This can only occur
149800 ** on the second iteration */
149801 assert( j==1 );
149802 assert( IsPowerOfTwo(chngToIN) );
149803 assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) );
149804 break;
149805 }
149806 testcase( j==1 );
149807
149808 /* We have found a candidate table and column. Check to see if that
149809 ** table and column is common to every term in the OR clause */
149810 okToChngToIN = 1;
149811 for(; i>=0 && okToChngToIN; i--, pOrTerm++){
149812 assert( pOrTerm->eOperator & WO_EQ );
149813 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
149814 if( pOrTerm->leftCursor!=iCursor ){
149815 pOrTerm->wtFlags &= ~TERM_OR_OK;
149816 }else if( pOrTerm->u.x.leftColumn!=iColumn || (iColumn==XN_EXPR
149817 && sqlite3ExprCompare(pParse, pA: pOrTerm->pExpr->pLeft, pB: pLeft, iTab: -1)
149818 )){
149819 okToChngToIN = 0;
149820 }else{
149821 int affLeft, affRight;
149822 /* If the right-hand side is also a column, then the affinities
149823 ** of both right and left sides must be such that no type
149824 ** conversions are required on the right. (Ticket #2249)
149825 */
149826 affRight = sqlite3ExprAffinity(pExpr: pOrTerm->pExpr->pRight);
149827 affLeft = sqlite3ExprAffinity(pExpr: pOrTerm->pExpr->pLeft);
149828 if( affRight!=0 && affRight!=affLeft ){
149829 okToChngToIN = 0;
149830 }else{
149831 pOrTerm->wtFlags |= TERM_OR_OK;
149832 }
149833 }
149834 }
149835 }
149836
149837 /* At this point, okToChngToIN is true if original pTerm satisfies
149838 ** case 1. In that case, construct a new virtual term that is
149839 ** pTerm converted into an IN operator.
149840 */
149841 if( okToChngToIN ){
149842 Expr *pDup; /* A transient duplicate expression */
149843 ExprList *pList = 0; /* The RHS of the IN operator */
149844 Expr *pLeft = 0; /* The LHS of the IN operator */
149845 Expr *pNew; /* The complete IN operator */
149846
149847 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
149848 if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
149849 assert( pOrTerm->eOperator & WO_EQ );
149850 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
149851 assert( pOrTerm->leftCursor==iCursor );
149852 assert( pOrTerm->u.x.leftColumn==iColumn );
149853 pDup = sqlite3ExprDup(db, p: pOrTerm->pExpr->pRight, flags: 0);
149854 pList = sqlite3ExprListAppend(pParse: pWInfo->pParse, pList, pExpr: pDup);
149855 pLeft = pOrTerm->pExpr->pLeft;
149856 }
149857 assert( pLeft!=0 );
149858 pDup = sqlite3ExprDup(db, p: pLeft, flags: 0);
149859 pNew = sqlite3PExpr(pParse, TK_IN, pLeft: pDup, pRight: 0);
149860 if( pNew ){
149861 int idxNew;
149862 transferJoinMarkings(pDerived: pNew, pBase: pExpr);
149863 assert( ExprUseXList(pNew) );
149864 pNew->x.pList = pList;
149865 idxNew = whereClauseInsert(pWC, p: pNew, TERM_VIRTUAL|TERM_DYNAMIC);
149866 testcase( idxNew==0 );
149867 exprAnalyze(pSrc, pWC, idxNew);
149868 /* pTerm = &pWC->a[idxTerm]; // would be needed if pTerm where reused */
149869 markTermAsChild(pWC, iChild: idxNew, iParent: idxTerm);
149870 }else{
149871 sqlite3ExprListDelete(db, pList);
149872 }
149873 }
149874 }
149875}
149876#endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
149877
149878/*
149879** We already know that pExpr is a binary operator where both operands are
149880** column references. This routine checks to see if pExpr is an equivalence
149881** relation:
149882** 1. The SQLITE_Transitive optimization must be enabled
149883** 2. Must be either an == or an IS operator
149884** 3. Not originating in the ON clause of an OUTER JOIN
149885** 4. The affinities of A and B must be compatible
149886** 5a. Both operands use the same collating sequence OR
149887** 5b. The overall collating sequence is BINARY
149888** If this routine returns TRUE, that means that the RHS can be substituted
149889** for the LHS anyplace else in the WHERE clause where the LHS column occurs.
149890** This is an optimization. No harm comes from returning 0. But if 1 is
149891** returned when it should not be, then incorrect answers might result.
149892*/
149893static int termIsEquivalence(Parse *pParse, Expr *pExpr){
149894 char aff1, aff2;
149895 CollSeq *pColl;
149896 if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0;
149897 if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0;
149898 if( ExprHasProperty(pExpr, EP_FromJoin) ) return 0;
149899 aff1 = sqlite3ExprAffinity(pExpr: pExpr->pLeft);
149900 aff2 = sqlite3ExprAffinity(pExpr: pExpr->pRight);
149901 if( aff1!=aff2
149902 && (!sqlite3IsNumericAffinity(aff1) || !sqlite3IsNumericAffinity(aff2))
149903 ){
149904 return 0;
149905 }
149906 pColl = sqlite3ExprCompareCollSeq(pParse, p: pExpr);
149907 if( sqlite3IsBinary(pColl) ) return 1;
149908 return sqlite3ExprCollSeqMatch(pParse, pE1: pExpr->pLeft, pE2: pExpr->pRight);
149909}
149910
149911/*
149912** Recursively walk the expressions of a SELECT statement and generate
149913** a bitmask indicating which tables are used in that expression
149914** tree.
149915*/
149916static Bitmask exprSelectUsage(WhereMaskSet *pMaskSet, Select *pS){
149917 Bitmask mask = 0;
149918 while( pS ){
149919 SrcList *pSrc = pS->pSrc;
149920 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
149921 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
149922 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
149923 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
149924 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
149925 if( ALWAYS(pSrc!=0) ){
149926 int i;
149927 for(i=0; i<pSrc->nSrc; i++){
149928 mask |= exprSelectUsage(pMaskSet, pS: pSrc->a[i].pSelect);
149929 mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].pOn);
149930 if( pSrc->a[i].fg.isTabFunc ){
149931 mask |= sqlite3WhereExprListUsage(pMaskSet, pSrc->a[i].u1.pFuncArg);
149932 }
149933 }
149934 }
149935 pS = pS->pPrior;
149936 }
149937 return mask;
149938}
149939
149940/*
149941** Expression pExpr is one operand of a comparison operator that might
149942** be useful for indexing. This routine checks to see if pExpr appears
149943** in any index. Return TRUE (1) if pExpr is an indexed term and return
149944** FALSE (0) if not. If TRUE is returned, also set aiCurCol[0] to the cursor
149945** number of the table that is indexed and aiCurCol[1] to the column number
149946** of the column that is indexed, or XN_EXPR (-2) if an expression is being
149947** indexed.
149948**
149949** If pExpr is a TK_COLUMN column reference, then this routine always returns
149950** true even if that particular column is not indexed, because the column
149951** might be added to an automatic index later.
149952*/
149953static SQLITE_NOINLINE int exprMightBeIndexed2(
149954 SrcList *pFrom, /* The FROM clause */
149955 Bitmask mPrereq, /* Bitmask of FROM clause terms referenced by pExpr */
149956 int *aiCurCol, /* Write the referenced table cursor and column here */
149957 Expr *pExpr /* An operand of a comparison operator */
149958){
149959 Index *pIdx;
149960 int i;
149961 int iCur;
149962 for(i=0; mPrereq>1; i++, mPrereq>>=1){}
149963 iCur = pFrom->a[i].iCursor;
149964 for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
149965 if( pIdx->aColExpr==0 ) continue;
149966 for(i=0; i<pIdx->nKeyCol; i++){
149967 if( pIdx->aiColumn[i]!=XN_EXPR ) continue;
149968 if( sqlite3ExprCompareSkip(pA: pExpr, pB: pIdx->aColExpr->a[i].pExpr, iTab: iCur)==0 ){
149969 aiCurCol[0] = iCur;
149970 aiCurCol[1] = XN_EXPR;
149971 return 1;
149972 }
149973 }
149974 }
149975 return 0;
149976}
149977static int exprMightBeIndexed(
149978 SrcList *pFrom, /* The FROM clause */
149979 Bitmask mPrereq, /* Bitmask of FROM clause terms referenced by pExpr */
149980 int *aiCurCol, /* Write the referenced table cursor & column here */
149981 Expr *pExpr, /* An operand of a comparison operator */
149982 int op /* The specific comparison operator */
149983){
149984 /* If this expression is a vector to the left or right of a
149985 ** inequality constraint (>, <, >= or <=), perform the processing
149986 ** on the first element of the vector. */
149987 assert( TK_GT+1==TK_LE && TK_GT+2==TK_LT && TK_GT+3==TK_GE );
149988 assert( TK_IS<TK_GE && TK_ISNULL<TK_GE && TK_IN<TK_GE );
149989 assert( op<=TK_GE );
149990 if( pExpr->op==TK_VECTOR && (op>=TK_GT && ALWAYS(op<=TK_GE)) ){
149991 assert( ExprUseXList(pExpr) );
149992 pExpr = pExpr->x.pList->a[0].pExpr;
149993
149994 }
149995
149996 if( pExpr->op==TK_COLUMN ){
149997 aiCurCol[0] = pExpr->iTable;
149998 aiCurCol[1] = pExpr->iColumn;
149999 return 1;
150000 }
150001 if( mPrereq==0 ) return 0; /* No table references */
150002 if( (mPrereq&(mPrereq-1))!=0 ) return 0; /* Refs more than one table */
150003 return exprMightBeIndexed2(pFrom,mPrereq,aiCurCol,pExpr);
150004}
150005
150006
150007/*
150008** The input to this routine is an WhereTerm structure with only the
150009** "pExpr" field filled in. The job of this routine is to analyze the
150010** subexpression and populate all the other fields of the WhereTerm
150011** structure.
150012**
150013** If the expression is of the form "<expr> <op> X" it gets commuted
150014** to the standard form of "X <op> <expr>".
150015**
150016** If the expression is of the form "X <op> Y" where both X and Y are
150017** columns, then the original expression is unchanged and a new virtual
150018** term of the form "Y <op> X" is added to the WHERE clause and
150019** analyzed separately. The original term is marked with TERM_COPIED
150020** and the new term is marked with TERM_DYNAMIC (because it's pExpr
150021** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
150022** is a commuted copy of a prior term.) The original term has nChild=1
150023** and the copy has idxParent set to the index of the original term.
150024*/
150025static void exprAnalyze(
150026 SrcList *pSrc, /* the FROM clause */
150027 WhereClause *pWC, /* the WHERE clause */
150028 int idxTerm /* Index of the term to be analyzed */
150029){
150030 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
150031 WhereTerm *pTerm; /* The term to be analyzed */
150032 WhereMaskSet *pMaskSet; /* Set of table index masks */
150033 Expr *pExpr; /* The expression to be analyzed */
150034 Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */
150035 Bitmask prereqAll; /* Prerequesites of pExpr */
150036 Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */
150037 Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */
150038 int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */
150039 int noCase = 0; /* uppercase equivalent to lowercase */
150040 int op; /* Top-level operator. pExpr->op */
150041 Parse *pParse = pWInfo->pParse; /* Parsing context */
150042 sqlite3 *db = pParse->db; /* Database connection */
150043 unsigned char eOp2 = 0; /* op2 value for LIKE/REGEXP/GLOB */
150044 int nLeft; /* Number of elements on left side vector */
150045
150046 if( db->mallocFailed ){
150047 return;
150048 }
150049 pTerm = &pWC->a[idxTerm];
150050 pMaskSet = &pWInfo->sMaskSet;
150051 pExpr = pTerm->pExpr;
150052 assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
150053 prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
150054 op = pExpr->op;
150055 if( op==TK_IN ){
150056 assert( pExpr->pRight==0 );
150057 if( sqlite3ExprCheckIN(pParse, pIn: pExpr) ) return;
150058 if( ExprUseXSelect(pExpr) ){
150059 pTerm->prereqRight = exprSelectUsage(pMaskSet, pS: pExpr->x.pSelect);
150060 }else{
150061 pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
150062 }
150063 }else if( op==TK_ISNULL ){
150064 pTerm->prereqRight = 0;
150065 }else{
150066 pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
150067 }
150068 pMaskSet->bVarSelect = 0;
150069 prereqAll = sqlite3WhereExprUsageNN(pMaskSet, pExpr);
150070 if( pMaskSet->bVarSelect ) pTerm->wtFlags |= TERM_VARSELECT;
150071 if( ExprHasProperty(pExpr, EP_FromJoin) ){
150072 Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable);
150073 prereqAll |= x;
150074 extraRight = x-1; /* ON clause terms may not be used with an index
150075 ** on left table of a LEFT JOIN. Ticket #3015 */
150076 if( (prereqAll>>1)>=x ){
150077 sqlite3ErrorMsg(pParse, zFormat: "ON clause references tables to its right");
150078 return;
150079 }
150080 }
150081 pTerm->prereqAll = prereqAll;
150082 pTerm->leftCursor = -1;
150083 pTerm->iParent = -1;
150084 pTerm->eOperator = 0;
150085 if( allowedOp(op) ){
150086 int aiCurCol[2];
150087 Expr *pLeft = sqlite3ExprSkipCollate(pExpr: pExpr->pLeft);
150088 Expr *pRight = sqlite3ExprSkipCollate(pExpr: pExpr->pRight);
150089 u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
150090
150091 if( pTerm->u.x.iField>0 ){
150092 assert( op==TK_IN );
150093 assert( pLeft->op==TK_VECTOR );
150094 assert( ExprUseXList(pLeft) );
150095 pLeft = pLeft->x.pList->a[pTerm->u.x.iField-1].pExpr;
150096 }
150097
150098 if( exprMightBeIndexed(pFrom: pSrc, mPrereq: prereqLeft, aiCurCol, pExpr: pLeft, op) ){
150099 pTerm->leftCursor = aiCurCol[0];
150100 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
150101 pTerm->u.x.leftColumn = aiCurCol[1];
150102 pTerm->eOperator = operatorMask(op) & opMask;
150103 }
150104 if( op==TK_IS ) pTerm->wtFlags |= TERM_IS;
150105 if( pRight
150106 && exprMightBeIndexed(pFrom: pSrc, mPrereq: pTerm->prereqRight, aiCurCol, pExpr: pRight, op)
150107 && !ExprHasProperty(pRight, EP_FixedCol)
150108 ){
150109 WhereTerm *pNew;
150110 Expr *pDup;
150111 u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */
150112 assert( pTerm->u.x.iField==0 );
150113 if( pTerm->leftCursor>=0 ){
150114 int idxNew;
150115 pDup = sqlite3ExprDup(db, p: pExpr, flags: 0);
150116 if( db->mallocFailed ){
150117 sqlite3ExprDelete(db, p: pDup);
150118 return;
150119 }
150120 idxNew = whereClauseInsert(pWC, p: pDup, TERM_VIRTUAL|TERM_DYNAMIC);
150121 if( idxNew==0 ) return;
150122 pNew = &pWC->a[idxNew];
150123 markTermAsChild(pWC, iChild: idxNew, iParent: idxTerm);
150124 if( op==TK_IS ) pNew->wtFlags |= TERM_IS;
150125 pTerm = &pWC->a[idxTerm];
150126 pTerm->wtFlags |= TERM_COPIED;
150127
150128 if( termIsEquivalence(pParse, pExpr: pDup) ){
150129 pTerm->eOperator |= WO_EQUIV;
150130 eExtraOp = WO_EQUIV;
150131 }
150132 }else{
150133 pDup = pExpr;
150134 pNew = pTerm;
150135 }
150136 pNew->wtFlags |= exprCommute(pParse, pExpr: pDup);
150137 pNew->leftCursor = aiCurCol[0];
150138 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
150139 pNew->u.x.leftColumn = aiCurCol[1];
150140 testcase( (prereqLeft | extraRight) != prereqLeft );
150141 pNew->prereqRight = prereqLeft | extraRight;
150142 pNew->prereqAll = prereqAll;
150143 pNew->eOperator = (operatorMask(op: pDup->op) + eExtraOp) & opMask;
150144 }else
150145 if( op==TK_ISNULL
150146 && !ExprHasProperty(pExpr,EP_FromJoin)
150147 && 0==sqlite3ExprCanBeNull(p: pLeft)
150148 ){
150149 assert( !ExprHasProperty(pExpr, EP_IntValue) );
150150 pExpr->op = TK_TRUEFALSE;
150151 pExpr->u.zToken = "false";
150152 ExprSetProperty(pExpr, EP_IsFalse);
150153 pTerm->prereqAll = 0;
150154 pTerm->eOperator = 0;
150155 }
150156 }
150157
150158#ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
150159 /* If a term is the BETWEEN operator, create two new virtual terms
150160 ** that define the range that the BETWEEN implements. For example:
150161 **
150162 ** a BETWEEN b AND c
150163 **
150164 ** is converted into:
150165 **
150166 ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
150167 **
150168 ** The two new terms are added onto the end of the WhereClause object.
150169 ** The new terms are "dynamic" and are children of the original BETWEEN
150170 ** term. That means that if the BETWEEN term is coded, the children are
150171 ** skipped. Or, if the children are satisfied by an index, the original
150172 ** BETWEEN term is skipped.
150173 */
150174 else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
150175 ExprList *pList;
150176 int i;
150177 static const u8 ops[] = {TK_GE, TK_LE};
150178 assert( ExprUseXList(pExpr) );
150179 pList = pExpr->x.pList;
150180 assert( pList!=0 );
150181 assert( pList->nExpr==2 );
150182 for(i=0; i<2; i++){
150183 Expr *pNewExpr;
150184 int idxNew;
150185 pNewExpr = sqlite3PExpr(pParse, op: ops[i],
150186 pLeft: sqlite3ExprDup(db, p: pExpr->pLeft, flags: 0),
150187 pRight: sqlite3ExprDup(db, p: pList->a[i].pExpr, flags: 0));
150188 transferJoinMarkings(pDerived: pNewExpr, pBase: pExpr);
150189 idxNew = whereClauseInsert(pWC, p: pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
150190 testcase( idxNew==0 );
150191 exprAnalyze(pSrc, pWC, idxTerm: idxNew);
150192 pTerm = &pWC->a[idxTerm];
150193 markTermAsChild(pWC, iChild: idxNew, iParent: idxTerm);
150194 }
150195 }
150196#endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
150197
150198#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
150199 /* Analyze a term that is composed of two or more subterms connected by
150200 ** an OR operator.
150201 */
150202 else if( pExpr->op==TK_OR ){
150203 assert( pWC->op==TK_AND );
150204 exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
150205 pTerm = &pWC->a[idxTerm];
150206 }
150207#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
150208 /* The form "x IS NOT NULL" can sometimes be evaluated more efficiently
150209 ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
150210 ** virtual term of that form.
150211 **
150212 ** The virtual term must be tagged with TERM_VNULL.
150213 */
150214 else if( pExpr->op==TK_NOTNULL ){
150215 if( pExpr->pLeft->op==TK_COLUMN
150216 && pExpr->pLeft->iColumn>=0
150217 && !ExprHasProperty(pExpr, EP_FromJoin)
150218 ){
150219 Expr *pNewExpr;
150220 Expr *pLeft = pExpr->pLeft;
150221 int idxNew;
150222 WhereTerm *pNewTerm;
150223
150224 pNewExpr = sqlite3PExpr(pParse, TK_GT,
150225 pLeft: sqlite3ExprDup(db, p: pLeft, flags: 0),
150226 pRight: sqlite3ExprAlloc(db, TK_NULL, pToken: 0, dequote: 0));
150227
150228 idxNew = whereClauseInsert(pWC, p: pNewExpr,
150229 TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL);
150230 if( idxNew ){
150231 pNewTerm = &pWC->a[idxNew];
150232 pNewTerm->prereqRight = 0;
150233 pNewTerm->leftCursor = pLeft->iTable;
150234 pNewTerm->u.x.leftColumn = pLeft->iColumn;
150235 pNewTerm->eOperator = WO_GT;
150236 markTermAsChild(pWC, iChild: idxNew, iParent: idxTerm);
150237 pTerm = &pWC->a[idxTerm];
150238 pTerm->wtFlags |= TERM_COPIED;
150239 pNewTerm->prereqAll = pTerm->prereqAll;
150240 }
150241 }
150242 }
150243
150244
150245#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
150246 /* Add constraints to reduce the search space on a LIKE or GLOB
150247 ** operator.
150248 **
150249 ** A like pattern of the form "x LIKE 'aBc%'" is changed into constraints
150250 **
150251 ** x>='ABC' AND x<'abd' AND x LIKE 'aBc%'
150252 **
150253 ** The last character of the prefix "abc" is incremented to form the
150254 ** termination condition "abd". If case is not significant (the default
150255 ** for LIKE) then the lower-bound is made all uppercase and the upper-
150256 ** bound is made all lowercase so that the bounds also work when comparing
150257 ** BLOBs.
150258 */
150259 else if( pExpr->op==TK_FUNCTION
150260 && pWC->op==TK_AND
150261 && isLikeOrGlob(pParse, pExpr, ppPrefix: &pStr1, pisComplete: &isComplete, pnoCase: &noCase)
150262 ){
150263 Expr *pLeft; /* LHS of LIKE/GLOB operator */
150264 Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */
150265 Expr *pNewExpr1;
150266 Expr *pNewExpr2;
150267 int idxNew1;
150268 int idxNew2;
150269 const char *zCollSeqName; /* Name of collating sequence */
150270 const u16 wtFlags = TERM_LIKEOPT | TERM_VIRTUAL | TERM_DYNAMIC;
150271
150272 assert( ExprUseXList(pExpr) );
150273 pLeft = pExpr->x.pList->a[1].pExpr;
150274 pStr2 = sqlite3ExprDup(db, p: pStr1, flags: 0);
150275 assert( pStr1==0 || !ExprHasProperty(pStr1, EP_IntValue) );
150276 assert( pStr2==0 || !ExprHasProperty(pStr2, EP_IntValue) );
150277
150278
150279 /* Convert the lower bound to upper-case and the upper bound to
150280 ** lower-case (upper-case is less than lower-case in ASCII) so that
150281 ** the range constraints also work for BLOBs
150282 */
150283 if( noCase && !pParse->db->mallocFailed ){
150284 int i;
150285 char c;
150286 pTerm->wtFlags |= TERM_LIKE;
150287 for(i=0; (c = pStr1->u.zToken[i])!=0; i++){
150288 pStr1->u.zToken[i] = sqlite3Toupper(c);
150289 pStr2->u.zToken[i] = sqlite3Tolower(c);
150290 }
150291 }
150292
150293 if( !db->mallocFailed ){
150294 u8 c, *pC; /* Last character before the first wildcard */
150295 pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(z: pStr2->u.zToken)-1];
150296 c = *pC;
150297 if( noCase ){
150298 /* The point is to increment the last character before the first
150299 ** wildcard. But if we increment '@', that will push it into the
150300 ** alphabetic range where case conversions will mess up the
150301 ** inequality. To avoid this, make sure to also run the full
150302 ** LIKE on all candidate expressions by clearing the isComplete flag
150303 */
150304 if( c=='A'-1 ) isComplete = 0;
150305 c = sqlite3UpperToLower[c];
150306 }
150307 *pC = c + 1;
150308 }
150309 zCollSeqName = noCase ? "NOCASE" : sqlite3StrBINARY;
150310 pNewExpr1 = sqlite3ExprDup(db, p: pLeft, flags: 0);
150311 pNewExpr1 = sqlite3PExpr(pParse, TK_GE,
150312 pLeft: sqlite3ExprAddCollateString(pParse,pExpr: pNewExpr1,zC: zCollSeqName),
150313 pRight: pStr1);
150314 transferJoinMarkings(pDerived: pNewExpr1, pBase: pExpr);
150315 idxNew1 = whereClauseInsert(pWC, p: pNewExpr1, wtFlags);
150316 testcase( idxNew1==0 );
150317 exprAnalyze(pSrc, pWC, idxTerm: idxNew1);
150318 pNewExpr2 = sqlite3ExprDup(db, p: pLeft, flags: 0);
150319 pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
150320 pLeft: sqlite3ExprAddCollateString(pParse,pExpr: pNewExpr2,zC: zCollSeqName),
150321 pRight: pStr2);
150322 transferJoinMarkings(pDerived: pNewExpr2, pBase: pExpr);
150323 idxNew2 = whereClauseInsert(pWC, p: pNewExpr2, wtFlags);
150324 testcase( idxNew2==0 );
150325 exprAnalyze(pSrc, pWC, idxTerm: idxNew2);
150326 pTerm = &pWC->a[idxTerm];
150327 if( isComplete ){
150328 markTermAsChild(pWC, iChild: idxNew1, iParent: idxTerm);
150329 markTermAsChild(pWC, iChild: idxNew2, iParent: idxTerm);
150330 }
150331 }
150332#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
150333
150334 /* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create
150335 ** new terms for each component comparison - "a = ?" and "b = ?". The
150336 ** new terms completely replace the original vector comparison, which is
150337 ** no longer used.
150338 **
150339 ** This is only required if at least one side of the comparison operation
150340 ** is not a sub-select. */
150341 if( (pExpr->op==TK_EQ || pExpr->op==TK_IS)
150342 && (nLeft = sqlite3ExprVectorSize(pExpr: pExpr->pLeft))>1
150343 && sqlite3ExprVectorSize(pExpr: pExpr->pRight)==nLeft
150344 && ( (pExpr->pLeft->flags & EP_xIsSelect)==0
150345 || (pExpr->pRight->flags & EP_xIsSelect)==0)
150346 && pWC->op==TK_AND
150347 ){
150348 int i;
150349 for(i=0; i<nLeft; i++){
150350 int idxNew;
150351 Expr *pNew;
150352 Expr *pLeft = sqlite3ExprForVectorField(pParse, pVector: pExpr->pLeft, iField: i, nField: nLeft);
150353 Expr *pRight = sqlite3ExprForVectorField(pParse, pVector: pExpr->pRight, iField: i, nField: nLeft);
150354
150355 pNew = sqlite3PExpr(pParse, op: pExpr->op, pLeft, pRight);
150356 transferJoinMarkings(pDerived: pNew, pBase: pExpr);
150357 idxNew = whereClauseInsert(pWC, p: pNew, TERM_DYNAMIC);
150358 exprAnalyze(pSrc, pWC, idxTerm: idxNew);
150359 }
150360 pTerm = &pWC->a[idxTerm];
150361 pTerm->wtFlags |= TERM_CODED|TERM_VIRTUAL; /* Disable the original */
150362 pTerm->eOperator = 0;
150363 }
150364
150365 /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create
150366 ** a virtual term for each vector component. The expression object
150367 ** used by each such virtual term is pExpr (the full vector IN(...)
150368 ** expression). The WhereTerm.u.x.iField variable identifies the index within
150369 ** the vector on the LHS that the virtual term represents.
150370 **
150371 ** This only works if the RHS is a simple SELECT (not a compound) that does
150372 ** not use window functions.
150373 */
150374 else if( pExpr->op==TK_IN
150375 && pTerm->u.x.iField==0
150376 && pExpr->pLeft->op==TK_VECTOR
150377 && ALWAYS( ExprUseXSelect(pExpr) )
150378 && pExpr->x.pSelect->pPrior==0
150379#ifndef SQLITE_OMIT_WINDOWFUNC
150380 && pExpr->x.pSelect->pWin==0
150381#endif
150382 && pWC->op==TK_AND
150383 ){
150384 int i;
150385 for(i=0; i<sqlite3ExprVectorSize(pExpr: pExpr->pLeft); i++){
150386 int idxNew;
150387 idxNew = whereClauseInsert(pWC, p: pExpr, TERM_VIRTUAL);
150388 pWC->a[idxNew].u.x.iField = i+1;
150389 exprAnalyze(pSrc, pWC, idxTerm: idxNew);
150390 markTermAsChild(pWC, iChild: idxNew, iParent: idxTerm);
150391 }
150392 }
150393
150394#ifndef SQLITE_OMIT_VIRTUALTABLE
150395 /* Add a WO_AUX auxiliary term to the constraint set if the
150396 ** current expression is of the form "column OP expr" where OP
150397 ** is an operator that gets passed into virtual tables but which is
150398 ** not normally optimized for ordinary tables. In other words, OP
150399 ** is one of MATCH, LIKE, GLOB, REGEXP, !=, IS, IS NOT, or NOT NULL.
150400 ** This information is used by the xBestIndex methods of
150401 ** virtual tables. The native query optimizer does not attempt
150402 ** to do anything with MATCH functions.
150403 */
150404 else if( pWC->op==TK_AND ){
150405 Expr *pRight = 0, *pLeft = 0;
150406 int res = isAuxiliaryVtabOperator(db, pExpr, peOp2: &eOp2, ppLeft: &pLeft, ppRight: &pRight);
150407 while( res-- > 0 ){
150408 int idxNew;
150409 WhereTerm *pNewTerm;
150410 Bitmask prereqColumn, prereqExpr;
150411
150412 prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight);
150413 prereqColumn = sqlite3WhereExprUsage(pMaskSet, pLeft);
150414 if( (prereqExpr & prereqColumn)==0 ){
150415 Expr *pNewExpr;
150416 pNewExpr = sqlite3PExpr(pParse, TK_MATCH,
150417 pLeft: 0, pRight: sqlite3ExprDup(db, p: pRight, flags: 0));
150418 if( ExprHasProperty(pExpr, EP_FromJoin) && pNewExpr ){
150419 ExprSetProperty(pNewExpr, EP_FromJoin);
150420 pNewExpr->iRightJoinTable = pExpr->iRightJoinTable;
150421 }
150422 idxNew = whereClauseInsert(pWC, p: pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
150423 testcase( idxNew==0 );
150424 pNewTerm = &pWC->a[idxNew];
150425 pNewTerm->prereqRight = prereqExpr;
150426 pNewTerm->leftCursor = pLeft->iTable;
150427 pNewTerm->u.x.leftColumn = pLeft->iColumn;
150428 pNewTerm->eOperator = WO_AUX;
150429 pNewTerm->eMatchOp = eOp2;
150430 markTermAsChild(pWC, iChild: idxNew, iParent: idxTerm);
150431 pTerm = &pWC->a[idxTerm];
150432 pTerm->wtFlags |= TERM_COPIED;
150433 pNewTerm->prereqAll = pTerm->prereqAll;
150434 }
150435 SWAP(Expr*, pLeft, pRight);
150436 }
150437 }
150438#endif /* SQLITE_OMIT_VIRTUALTABLE */
150439
150440 /* Prevent ON clause terms of a LEFT JOIN from being used to drive
150441 ** an index for tables to the left of the join.
150442 */
150443 testcase( pTerm!=&pWC->a[idxTerm] );
150444 pTerm = &pWC->a[idxTerm];
150445 pTerm->prereqRight |= extraRight;
150446}
150447
150448/***************************************************************************
150449** Routines with file scope above. Interface to the rest of the where.c
150450** subsystem follows.
150451***************************************************************************/
150452
150453/*
150454** This routine identifies subexpressions in the WHERE clause where
150455** each subexpression is separated by the AND operator or some other
150456** operator specified in the op parameter. The WhereClause structure
150457** is filled with pointers to subexpressions. For example:
150458**
150459** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
150460** \________/ \_______________/ \________________/
150461** slot[0] slot[1] slot[2]
150462**
150463** The original WHERE clause in pExpr is unaltered. All this routine
150464** does is make slot[] entries point to substructure within pExpr.
150465**
150466** In the previous sentence and in the diagram, "slot[]" refers to
150467** the WhereClause.a[] array. The slot[] array grows as needed to contain
150468** all terms of the WHERE clause.
150469*/
150470SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
150471 Expr *pE2 = sqlite3ExprSkipCollateAndLikely(pExpr);
150472 pWC->op = op;
150473 assert( pE2!=0 || pExpr==0 );
150474 if( pE2==0 ) return;
150475 if( pE2->op!=op ){
150476 whereClauseInsert(pWC, p: pExpr, wtFlags: 0);
150477 }else{
150478 sqlite3WhereSplit(pWC, pExpr: pE2->pLeft, op);
150479 sqlite3WhereSplit(pWC, pExpr: pE2->pRight, op);
150480 }
150481}
150482
150483/*
150484** Initialize a preallocated WhereClause structure.
150485*/
150486SQLITE_PRIVATE void sqlite3WhereClauseInit(
150487 WhereClause *pWC, /* The WhereClause to be initialized */
150488 WhereInfo *pWInfo /* The WHERE processing context */
150489){
150490 pWC->pWInfo = pWInfo;
150491 pWC->hasOr = 0;
150492 pWC->pOuter = 0;
150493 pWC->nTerm = 0;
150494 pWC->nSlot = ArraySize(pWC->aStatic);
150495 pWC->a = pWC->aStatic;
150496}
150497
150498/*
150499** Deallocate a WhereClause structure. The WhereClause structure
150500** itself is not freed. This routine is the inverse of
150501** sqlite3WhereClauseInit().
150502*/
150503SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause *pWC){
150504 int i;
150505 WhereTerm *a;
150506 sqlite3 *db = pWC->pWInfo->pParse->db;
150507 for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
150508 if( a->wtFlags & TERM_DYNAMIC ){
150509 sqlite3ExprDelete(db, p: a->pExpr);
150510 }
150511 if( a->wtFlags & TERM_ORINFO ){
150512 whereOrInfoDelete(db, p: a->u.pOrInfo);
150513 }else if( a->wtFlags & TERM_ANDINFO ){
150514 whereAndInfoDelete(db, p: a->u.pAndInfo);
150515 }
150516 }
150517 if( pWC->a!=pWC->aStatic ){
150518 sqlite3DbFree(db, p: pWC->a);
150519 }
150520}
150521
150522
150523/*
150524** These routines walk (recursively) an expression tree and generate
150525** a bitmask indicating which tables are used in that expression
150526** tree.
150527*/
150528SQLITE_PRIVATE Bitmask sqlite3WhereExprUsageNN(WhereMaskSet *pMaskSet, Expr *p){
150529 Bitmask mask;
150530 if( p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){
150531 return sqlite3WhereGetMask(pMaskSet, p->iTable);
150532 }else if( ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){
150533 assert( p->op!=TK_IF_NULL_ROW );
150534 return 0;
150535 }
150536 mask = (p->op==TK_IF_NULL_ROW) ? sqlite3WhereGetMask(pMaskSet, p->iTable) : 0;
150537 if( p->pLeft ) mask |= sqlite3WhereExprUsageNN(pMaskSet, p: p->pLeft);
150538 if( p->pRight ){
150539 mask |= sqlite3WhereExprUsageNN(pMaskSet, p: p->pRight);
150540 assert( p->x.pList==0 );
150541 }else if( ExprUseXSelect(p) ){
150542 if( ExprHasProperty(p, EP_VarSelect) ) pMaskSet->bVarSelect = 1;
150543 mask |= exprSelectUsage(pMaskSet, pS: p->x.pSelect);
150544 }else if( p->x.pList ){
150545 mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
150546 }
150547#ifndef SQLITE_OMIT_WINDOWFUNC
150548 if( (p->op==TK_FUNCTION || p->op==TK_AGG_FUNCTION) && ExprUseYWin(p) ){
150549 assert( p->y.pWin!=0 );
150550 mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pPartition);
150551 mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pOrderBy);
150552 mask |= sqlite3WhereExprUsage(pMaskSet, p->y.pWin->pFilter);
150553 }
150554#endif
150555 return mask;
150556}
150557SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){
150558 return p ? sqlite3WhereExprUsageNN(pMaskSet,p) : 0;
150559}
150560SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprList *pList){
150561 int i;
150562 Bitmask mask = 0;
150563 if( pList ){
150564 for(i=0; i<pList->nExpr; i++){
150565 mask |= sqlite3WhereExprUsage(pMaskSet, p: pList->a[i].pExpr);
150566 }
150567 }
150568 return mask;
150569}
150570
150571
150572/*
150573** Call exprAnalyze on all terms in a WHERE clause.
150574**
150575** Note that exprAnalyze() might add new virtual terms onto the
150576** end of the WHERE clause. We do not want to analyze these new
150577** virtual terms, so start analyzing at the end and work forward
150578** so that the added virtual terms are never processed.
150579*/
150580SQLITE_PRIVATE void sqlite3WhereExprAnalyze(
150581 SrcList *pTabList, /* the FROM clause */
150582 WhereClause *pWC /* the WHERE clause to be analyzed */
150583){
150584 int i;
150585 for(i=pWC->nTerm-1; i>=0; i--){
150586 exprAnalyze(pSrc: pTabList, pWC, idxTerm: i);
150587 }
150588}
150589
150590/*
150591** For table-valued-functions, transform the function arguments into
150592** new WHERE clause terms.
150593**
150594** Each function argument translates into an equality constraint against
150595** a HIDDEN column in the table.
150596*/
150597SQLITE_PRIVATE void sqlite3WhereTabFuncArgs(
150598 Parse *pParse, /* Parsing context */
150599 SrcItem *pItem, /* The FROM clause term to process */
150600 WhereClause *pWC /* Xfer function arguments to here */
150601){
150602 Table *pTab;
150603 int j, k;
150604 ExprList *pArgs;
150605 Expr *pColRef;
150606 Expr *pTerm;
150607 if( pItem->fg.isTabFunc==0 ) return;
150608 pTab = pItem->pTab;
150609 assert( pTab!=0 );
150610 pArgs = pItem->u1.pFuncArg;
150611 if( pArgs==0 ) return;
150612 for(j=k=0; j<pArgs->nExpr; j++){
150613 Expr *pRhs;
150614 while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;}
150615 if( k>=pTab->nCol ){
150616 sqlite3ErrorMsg(pParse, zFormat: "too many arguments on %s() - max %d",
150617 pTab->zName, j);
150618 return;
150619 }
150620 pColRef = sqlite3ExprAlloc(db: pParse->db, TK_COLUMN, pToken: 0, dequote: 0);
150621 if( pColRef==0 ) return;
150622 pColRef->iTable = pItem->iCursor;
150623 pColRef->iColumn = k++;
150624 assert( ExprUseYTab(pColRef) );
150625 pColRef->y.pTab = pTab;
150626 pRhs = sqlite3PExpr(pParse, TK_UPLUS,
150627 pLeft: sqlite3ExprDup(db: pParse->db, p: pArgs->a[j].pExpr, flags: 0), pRight: 0);
150628 pTerm = sqlite3PExpr(pParse, TK_EQ, pLeft: pColRef, pRight: pRhs);
150629 if( pItem->fg.jointype & JT_LEFT ){
150630 sqlite3SetJoinExpr(p: pTerm, iTable: pItem->iCursor);
150631 }
150632 whereClauseInsert(pWC, p: pTerm, TERM_DYNAMIC);
150633 }
150634}
150635
150636/************** End of whereexpr.c *******************************************/
150637/************** Begin file where.c *******************************************/
150638/*
150639** 2001 September 15
150640**
150641** The author disclaims copyright to this source code. In place of
150642** a legal notice, here is a blessing:
150643**
150644** May you do good and not evil.
150645** May you find forgiveness for yourself and forgive others.
150646** May you share freely, never taking more than you give.
150647**
150648*************************************************************************
150649** This module contains C code that generates VDBE code used to process
150650** the WHERE clause of SQL statements. This module is responsible for
150651** generating the code that loops through a table looking for applicable
150652** rows. Indices are selected and used to speed the search when doing
150653** so is applicable. Because this module is responsible for selecting
150654** indices, you might also think of this module as the "query optimizer".
150655*/
150656/* #include "sqliteInt.h" */
150657/* #include "whereInt.h" */
150658
150659/*
150660** Extra information appended to the end of sqlite3_index_info but not
150661** visible to the xBestIndex function, at least not directly. The
150662** sqlite3_vtab_collation() interface knows how to reach it, however.
150663**
150664** This object is not an API and can be changed from one release to the
150665** next. As long as allocateIndexInfo() and sqlite3_vtab_collation()
150666** agree on the structure, all will be well.
150667*/
150668typedef struct HiddenIndexInfo HiddenIndexInfo;
150669struct HiddenIndexInfo {
150670 WhereClause *pWC; /* The Where clause being analyzed */
150671 Parse *pParse; /* The parsing context */
150672};
150673
150674/* Forward declaration of methods */
150675static int whereLoopResize(sqlite3*, WhereLoop*, int);
150676
150677/*
150678** Return the estimated number of output rows from a WHERE clause
150679*/
150680SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo *pWInfo){
150681 return pWInfo->nRowOut;
150682}
150683
150684/*
150685** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this
150686** WHERE clause returns outputs for DISTINCT processing.
150687*/
150688SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo *pWInfo){
150689 return pWInfo->eDistinct;
150690}
150691
150692/*
150693** Return the number of ORDER BY terms that are satisfied by the
150694** WHERE clause. A return of 0 means that the output must be
150695** completely sorted. A return equal to the number of ORDER BY
150696** terms means that no sorting is needed at all. A return that
150697** is positive but less than the number of ORDER BY terms means that
150698** block sorting is required.
150699*/
150700SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo *pWInfo){
150701 return pWInfo->nOBSat;
150702}
150703
150704/*
150705** In the ORDER BY LIMIT optimization, if the inner-most loop is known
150706** to emit rows in increasing order, and if the last row emitted by the
150707** inner-most loop did not fit within the sorter, then we can skip all
150708** subsequent rows for the current iteration of the inner loop (because they
150709** will not fit in the sorter either) and continue with the second inner
150710** loop - the loop immediately outside the inner-most.
150711**
150712** When a row does not fit in the sorter (because the sorter already
150713** holds LIMIT+OFFSET rows that are smaller), then a jump is made to the
150714** label returned by this function.
150715**
150716** If the ORDER BY LIMIT optimization applies, the jump destination should
150717** be the continuation for the second-inner-most loop. If the ORDER BY
150718** LIMIT optimization does not apply, then the jump destination should
150719** be the continuation for the inner-most loop.
150720**
150721** It is always safe for this routine to return the continuation of the
150722** inner-most loop, in the sense that a correct answer will result.
150723** Returning the continuation the second inner loop is an optimization
150724** that might make the code run a little faster, but should not change
150725** the final answer.
150726*/
150727SQLITE_PRIVATE int sqlite3WhereOrderByLimitOptLabel(WhereInfo *pWInfo){
150728 WhereLevel *pInner;
150729 if( !pWInfo->bOrderedInnerLoop ){
150730 /* The ORDER BY LIMIT optimization does not apply. Jump to the
150731 ** continuation of the inner-most loop. */
150732 return pWInfo->iContinue;
150733 }
150734 pInner = &pWInfo->a[pWInfo->nLevel-1];
150735 assert( pInner->addrNxt!=0 );
150736 return pInner->addrNxt;
150737}
150738
150739/*
150740** While generating code for the min/max optimization, after handling
150741** the aggregate-step call to min() or max(), check to see if any
150742** additional looping is required. If the output order is such that
150743** we are certain that the correct answer has already been found, then
150744** code an OP_Goto to by pass subsequent processing.
150745**
150746** Any extra OP_Goto that is coded here is an optimization. The
150747** correct answer should be obtained regardless. This OP_Goto just
150748** makes the answer appear faster.
150749*/
150750SQLITE_PRIVATE void sqlite3WhereMinMaxOptEarlyOut(Vdbe *v, WhereInfo *pWInfo){
150751 WhereLevel *pInner;
150752 int i;
150753 if( !pWInfo->bOrderedInnerLoop ) return;
150754 if( pWInfo->nOBSat==0 ) return;
150755 for(i=pWInfo->nLevel-1; i>=0; i--){
150756 pInner = &pWInfo->a[i];
150757 if( (pInner->pWLoop->wsFlags & WHERE_COLUMN_IN)!=0 ){
150758 sqlite3VdbeGoto(p: v, iDest: pInner->addrNxt);
150759 return;
150760 }
150761 }
150762 sqlite3VdbeGoto(p: v, iDest: pWInfo->iBreak);
150763}
150764
150765/*
150766** Return the VDBE address or label to jump to in order to continue
150767** immediately with the next row of a WHERE clause.
150768*/
150769SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo *pWInfo){
150770 assert( pWInfo->iContinue!=0 );
150771 return pWInfo->iContinue;
150772}
150773
150774/*
150775** Return the VDBE address or label to jump to in order to break
150776** out of a WHERE loop.
150777*/
150778SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo *pWInfo){
150779 return pWInfo->iBreak;
150780}
150781
150782/*
150783** Return ONEPASS_OFF (0) if an UPDATE or DELETE statement is unable to
150784** operate directly on the rowids returned by a WHERE clause. Return
150785** ONEPASS_SINGLE (1) if the statement can operation directly because only
150786** a single row is to be changed. Return ONEPASS_MULTI (2) if the one-pass
150787** optimization can be used on multiple
150788**
150789** If the ONEPASS optimization is used (if this routine returns true)
150790** then also write the indices of open cursors used by ONEPASS
150791** into aiCur[0] and aiCur[1]. iaCur[0] gets the cursor of the data
150792** table and iaCur[1] gets the cursor used by an auxiliary index.
150793** Either value may be -1, indicating that cursor is not used.
150794** Any cursors returned will have been opened for writing.
150795**
150796** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
150797** unable to use the ONEPASS optimization.
150798*/
150799SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){
150800 memcpy(dest: aiCur, src: pWInfo->aiCurOnePass, n: sizeof(int)*2);
150801#ifdef WHERETRACE_ENABLED
150802 if( sqlite3WhereTrace && pWInfo->eOnePass!=ONEPASS_OFF ){
150803 sqlite3DebugPrintf("%s cursors: %d %d\n",
150804 pWInfo->eOnePass==ONEPASS_SINGLE ? "ONEPASS_SINGLE" : "ONEPASS_MULTI",
150805 aiCur[0], aiCur[1]);
150806 }
150807#endif
150808 return pWInfo->eOnePass;
150809}
150810
150811/*
150812** Return TRUE if the WHERE loop uses the OP_DeferredSeek opcode to move
150813** the data cursor to the row selected by the index cursor.
150814*/
150815SQLITE_PRIVATE int sqlite3WhereUsesDeferredSeek(WhereInfo *pWInfo){
150816 return pWInfo->bDeferredSeek;
150817}
150818
150819/*
150820** Move the content of pSrc into pDest
150821*/
150822static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){
150823 pDest->n = pSrc->n;
150824 memcpy(dest: pDest->a, src: pSrc->a, n: pDest->n*sizeof(pDest->a[0]));
150825}
150826
150827/*
150828** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet.
150829**
150830** The new entry might overwrite an existing entry, or it might be
150831** appended, or it might be discarded. Do whatever is the right thing
150832** so that pSet keeps the N_OR_COST best entries seen so far.
150833*/
150834static int whereOrInsert(
150835 WhereOrSet *pSet, /* The WhereOrSet to be updated */
150836 Bitmask prereq, /* Prerequisites of the new entry */
150837 LogEst rRun, /* Run-cost of the new entry */
150838 LogEst nOut /* Number of outputs for the new entry */
150839){
150840 u16 i;
150841 WhereOrCost *p;
150842 for(i=pSet->n, p=pSet->a; i>0; i--, p++){
150843 if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
150844 goto whereOrInsert_done;
150845 }
150846 if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
150847 return 0;
150848 }
150849 }
150850 if( pSet->n<N_OR_COST ){
150851 p = &pSet->a[pSet->n++];
150852 p->nOut = nOut;
150853 }else{
150854 p = pSet->a;
150855 for(i=1; i<pSet->n; i++){
150856 if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
150857 }
150858 if( p->rRun<=rRun ) return 0;
150859 }
150860whereOrInsert_done:
150861 p->prereq = prereq;
150862 p->rRun = rRun;
150863 if( p->nOut>nOut ) p->nOut = nOut;
150864 return 1;
150865}
150866
150867/*
150868** Return the bitmask for the given cursor number. Return 0 if
150869** iCursor is not in the set.
150870*/
150871SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet *pMaskSet, int iCursor){
150872 int i;
150873 assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
150874 for(i=0; i<pMaskSet->n; i++){
150875 if( pMaskSet->ix[i]==iCursor ){
150876 return MASKBIT(i);
150877 }
150878 }
150879 return 0;
150880}
150881
150882/*
150883** Create a new mask for cursor iCursor.
150884**
150885** There is one cursor per table in the FROM clause. The number of
150886** tables in the FROM clause is limited by a test early in the
150887** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[]
150888** array will never overflow.
150889*/
150890static void createMask(WhereMaskSet *pMaskSet, int iCursor){
150891 assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
150892 pMaskSet->ix[pMaskSet->n++] = iCursor;
150893}
150894
150895/*
150896** If the right-hand branch of the expression is a TK_COLUMN, then return
150897** a pointer to the right-hand branch. Otherwise, return NULL.
150898*/
150899static Expr *whereRightSubexprIsColumn(Expr *p){
150900 p = sqlite3ExprSkipCollateAndLikely(pExpr: p->pRight);
150901 if( ALWAYS(p!=0) && p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){
150902 return p;
150903 }
150904 return 0;
150905}
150906
150907/*
150908** Advance to the next WhereTerm that matches according to the criteria
150909** established when the pScan object was initialized by whereScanInit().
150910** Return NULL if there are no more matching WhereTerms.
150911*/
150912static WhereTerm *whereScanNext(WhereScan *pScan){
150913 int iCur; /* The cursor on the LHS of the term */
150914 i16 iColumn; /* The column on the LHS of the term. -1 for IPK */
150915 Expr *pX; /* An expression being tested */
150916 WhereClause *pWC; /* Shorthand for pScan->pWC */
150917 WhereTerm *pTerm; /* The term being tested */
150918 int k = pScan->k; /* Where to start scanning */
150919
150920 assert( pScan->iEquiv<=pScan->nEquiv );
150921 pWC = pScan->pWC;
150922 while(1){
150923 iColumn = pScan->aiColumn[pScan->iEquiv-1];
150924 iCur = pScan->aiCur[pScan->iEquiv-1];
150925 assert( pWC!=0 );
150926 assert( iCur>=0 );
150927 do{
150928 for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
150929 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 || pTerm->leftCursor<0 );
150930 if( pTerm->leftCursor==iCur
150931 && pTerm->u.x.leftColumn==iColumn
150932 && (iColumn!=XN_EXPR
150933 || sqlite3ExprCompareSkip(pA: pTerm->pExpr->pLeft,
150934 pB: pScan->pIdxExpr,iTab: iCur)==0)
150935 && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin))
150936 ){
150937 if( (pTerm->eOperator & WO_EQUIV)!=0
150938 && pScan->nEquiv<ArraySize(pScan->aiCur)
150939 && (pX = whereRightSubexprIsColumn(p: pTerm->pExpr))!=0
150940 ){
150941 int j;
150942 for(j=0; j<pScan->nEquiv; j++){
150943 if( pScan->aiCur[j]==pX->iTable
150944 && pScan->aiColumn[j]==pX->iColumn ){
150945 break;
150946 }
150947 }
150948 if( j==pScan->nEquiv ){
150949 pScan->aiCur[j] = pX->iTable;
150950 pScan->aiColumn[j] = pX->iColumn;
150951 pScan->nEquiv++;
150952 }
150953 }
150954 if( (pTerm->eOperator & pScan->opMask)!=0 ){
150955 /* Verify the affinity and collating sequence match */
150956 if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
150957 CollSeq *pColl;
150958 Parse *pParse = pWC->pWInfo->pParse;
150959 pX = pTerm->pExpr;
150960 if( !sqlite3IndexAffinityOk(pExpr: pX, idx_affinity: pScan->idxaff) ){
150961 continue;
150962 }
150963 assert(pX->pLeft);
150964 pColl = sqlite3ExprCompareCollSeq(pParse, p: pX);
150965 if( pColl==0 ) pColl = pParse->db->pDfltColl;
150966 if( sqlite3StrICmp(zLeft: pColl->zName, zRight: pScan->zCollName) ){
150967 continue;
150968 }
150969 }
150970 if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
150971 && (pX = pTerm->pExpr->pRight, ALWAYS(pX!=0))
150972 && pX->op==TK_COLUMN
150973 && pX->iTable==pScan->aiCur[0]
150974 && pX->iColumn==pScan->aiColumn[0]
150975 ){
150976 testcase( pTerm->eOperator & WO_IS );
150977 continue;
150978 }
150979 pScan->pWC = pWC;
150980 pScan->k = k+1;
150981#ifdef WHERETRACE_ENABLED
150982 if( sqlite3WhereTrace & 0x20000 ){
150983 int ii;
150984 sqlite3DebugPrintf("SCAN-TERM %p: nEquiv=%d",
150985 pTerm, pScan->nEquiv);
150986 for(ii=0; ii<pScan->nEquiv; ii++){
150987 sqlite3DebugPrintf(" {%d:%d}",
150988 pScan->aiCur[ii], pScan->aiColumn[ii]);
150989 }
150990 sqlite3DebugPrintf("\n");
150991 }
150992#endif
150993 return pTerm;
150994 }
150995 }
150996 }
150997 pWC = pWC->pOuter;
150998 k = 0;
150999 }while( pWC!=0 );
151000 if( pScan->iEquiv>=pScan->nEquiv ) break;
151001 pWC = pScan->pOrigWC;
151002 k = 0;
151003 pScan->iEquiv++;
151004 }
151005 return 0;
151006}
151007
151008/*
151009** This is whereScanInit() for the case of an index on an expression.
151010** It is factored out into a separate tail-recursion subroutine so that
151011** the normal whereScanInit() routine, which is a high-runner, does not
151012** need to push registers onto the stack as part of its prologue.
151013*/
151014static SQLITE_NOINLINE WhereTerm *whereScanInitIndexExpr(WhereScan *pScan){
151015 pScan->idxaff = sqlite3ExprAffinity(pExpr: pScan->pIdxExpr);
151016 return whereScanNext(pScan);
151017}
151018
151019/*
151020** Initialize a WHERE clause scanner object. Return a pointer to the
151021** first match. Return NULL if there are no matches.
151022**
151023** The scanner will be searching the WHERE clause pWC. It will look
151024** for terms of the form "X <op> <expr>" where X is column iColumn of table
151025** iCur. Or if pIdx!=0 then X is column iColumn of index pIdx. pIdx
151026** must be one of the indexes of table iCur.
151027**
151028** The <op> must be one of the operators described by opMask.
151029**
151030** If the search is for X and the WHERE clause contains terms of the
151031** form X=Y then this routine might also return terms of the form
151032** "Y <op> <expr>". The number of levels of transitivity is limited,
151033** but is enough to handle most commonly occurring SQL statements.
151034**
151035** If X is not the INTEGER PRIMARY KEY then X must be compatible with
151036** index pIdx.
151037*/
151038static WhereTerm *whereScanInit(
151039 WhereScan *pScan, /* The WhereScan object being initialized */
151040 WhereClause *pWC, /* The WHERE clause to be scanned */
151041 int iCur, /* Cursor to scan for */
151042 int iColumn, /* Column to scan for */
151043 u32 opMask, /* Operator(s) to scan for */
151044 Index *pIdx /* Must be compatible with this index */
151045){
151046 pScan->pOrigWC = pWC;
151047 pScan->pWC = pWC;
151048 pScan->pIdxExpr = 0;
151049 pScan->idxaff = 0;
151050 pScan->zCollName = 0;
151051 pScan->opMask = opMask;
151052 pScan->k = 0;
151053 pScan->aiCur[0] = iCur;
151054 pScan->nEquiv = 1;
151055 pScan->iEquiv = 1;
151056 if( pIdx ){
151057 int j = iColumn;
151058 iColumn = pIdx->aiColumn[j];
151059 if( iColumn==XN_EXPR ){
151060 pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr;
151061 pScan->zCollName = pIdx->azColl[j];
151062 pScan->aiColumn[0] = XN_EXPR;
151063 return whereScanInitIndexExpr(pScan);
151064 }else if( iColumn==pIdx->pTable->iPKey ){
151065 iColumn = XN_ROWID;
151066 }else if( iColumn>=0 ){
151067 pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
151068 pScan->zCollName = pIdx->azColl[j];
151069 }
151070 }else if( iColumn==XN_EXPR ){
151071 return 0;
151072 }
151073 pScan->aiColumn[0] = iColumn;
151074 return whereScanNext(pScan);
151075}
151076
151077/*
151078** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
151079** where X is a reference to the iColumn of table iCur or of index pIdx
151080** if pIdx!=0 and <op> is one of the WO_xx operator codes specified by
151081** the op parameter. Return a pointer to the term. Return 0 if not found.
151082**
151083** If pIdx!=0 then it must be one of the indexes of table iCur.
151084** Search for terms matching the iColumn-th column of pIdx
151085** rather than the iColumn-th column of table iCur.
151086**
151087** The term returned might by Y=<expr> if there is another constraint in
151088** the WHERE clause that specifies that X=Y. Any such constraints will be
151089** identified by the WO_EQUIV bit in the pTerm->eOperator field. The
151090** aiCur[]/iaColumn[] arrays hold X and all its equivalents. There are 11
151091** slots in aiCur[]/aiColumn[] so that means we can look for X plus up to 10
151092** other equivalent values. Hence a search for X will return <expr> if X=A1
151093** and A1=A2 and A2=A3 and ... and A9=A10 and A10=<expr>.
151094**
151095** If there are multiple terms in the WHERE clause of the form "X <op> <expr>"
151096** then try for the one with no dependencies on <expr> - in other words where
151097** <expr> is a constant expression of some kind. Only return entries of
151098** the form "X <op> Y" where Y is a column in another table if no terms of
151099** the form "X <op> <const-expr>" exist. If no terms with a constant RHS
151100** exist, try to return a term that does not use WO_EQUIV.
151101*/
151102SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm(
151103 WhereClause *pWC, /* The WHERE clause to be searched */
151104 int iCur, /* Cursor number of LHS */
151105 int iColumn, /* Column number of LHS */
151106 Bitmask notReady, /* RHS must not overlap with this mask */
151107 u32 op, /* Mask of WO_xx values describing operator */
151108 Index *pIdx /* Must be compatible with this index, if not NULL */
151109){
151110 WhereTerm *pResult = 0;
151111 WhereTerm *p;
151112 WhereScan scan;
151113
151114 p = whereScanInit(pScan: &scan, pWC, iCur, iColumn, opMask: op, pIdx);
151115 op &= WO_EQ|WO_IS;
151116 while( p ){
151117 if( (p->prereqRight & notReady)==0 ){
151118 if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
151119 testcase( p->eOperator & WO_IS );
151120 return p;
151121 }
151122 if( pResult==0 ) pResult = p;
151123 }
151124 p = whereScanNext(pScan: &scan);
151125 }
151126 return pResult;
151127}
151128
151129/*
151130** This function searches pList for an entry that matches the iCol-th column
151131** of index pIdx.
151132**
151133** If such an expression is found, its index in pList->a[] is returned. If
151134** no expression is found, -1 is returned.
151135*/
151136static int findIndexCol(
151137 Parse *pParse, /* Parse context */
151138 ExprList *pList, /* Expression list to search */
151139 int iBase, /* Cursor for table associated with pIdx */
151140 Index *pIdx, /* Index to match column of */
151141 int iCol /* Column of index to match */
151142){
151143 int i;
151144 const char *zColl = pIdx->azColl[iCol];
151145
151146 for(i=0; i<pList->nExpr; i++){
151147 Expr *p = sqlite3ExprSkipCollateAndLikely(pExpr: pList->a[i].pExpr);
151148 if( ALWAYS(p!=0)
151149 && (p->op==TK_COLUMN || p->op==TK_AGG_COLUMN)
151150 && p->iColumn==pIdx->aiColumn[iCol]
151151 && p->iTable==iBase
151152 ){
151153 CollSeq *pColl = sqlite3ExprNNCollSeq(pParse, pExpr: pList->a[i].pExpr);
151154 if( 0==sqlite3StrICmp(zLeft: pColl->zName, zRight: zColl) ){
151155 return i;
151156 }
151157 }
151158 }
151159
151160 return -1;
151161}
151162
151163/*
151164** Return TRUE if the iCol-th column of index pIdx is NOT NULL
151165*/
151166static int indexColumnNotNull(Index *pIdx, int iCol){
151167 int j;
151168 assert( pIdx!=0 );
151169 assert( iCol>=0 && iCol<pIdx->nColumn );
151170 j = pIdx->aiColumn[iCol];
151171 if( j>=0 ){
151172 return pIdx->pTable->aCol[j].notNull;
151173 }else if( j==(-1) ){
151174 return 1;
151175 }else{
151176 assert( j==(-2) );
151177 return 0; /* Assume an indexed expression can always yield a NULL */
151178
151179 }
151180}
151181
151182/*
151183** Return true if the DISTINCT expression-list passed as the third argument
151184** is redundant.
151185**
151186** A DISTINCT list is redundant if any subset of the columns in the
151187** DISTINCT list are collectively unique and individually non-null.
151188*/
151189static int isDistinctRedundant(
151190 Parse *pParse, /* Parsing context */
151191 SrcList *pTabList, /* The FROM clause */
151192 WhereClause *pWC, /* The WHERE clause */
151193 ExprList *pDistinct /* The result set that needs to be DISTINCT */
151194){
151195 Table *pTab;
151196 Index *pIdx;
151197 int i;
151198 int iBase;
151199
151200 /* If there is more than one table or sub-select in the FROM clause of
151201 ** this query, then it will not be possible to show that the DISTINCT
151202 ** clause is redundant. */
151203 if( pTabList->nSrc!=1 ) return 0;
151204 iBase = pTabList->a[0].iCursor;
151205 pTab = pTabList->a[0].pTab;
151206
151207 /* If any of the expressions is an IPK column on table iBase, then return
151208 ** true. Note: The (p->iTable==iBase) part of this test may be false if the
151209 ** current SELECT is a correlated sub-query.
151210 */
151211 for(i=0; i<pDistinct->nExpr; i++){
151212 Expr *p = sqlite3ExprSkipCollateAndLikely(pExpr: pDistinct->a[i].pExpr);
151213 if( NEVER(p==0) ) continue;
151214 if( p->op!=TK_COLUMN && p->op!=TK_AGG_COLUMN ) continue;
151215 if( p->iTable==iBase && p->iColumn<0 ) return 1;
151216 }
151217
151218 /* Loop through all indices on the table, checking each to see if it makes
151219 ** the DISTINCT qualifier redundant. It does so if:
151220 **
151221 ** 1. The index is itself UNIQUE, and
151222 **
151223 ** 2. All of the columns in the index are either part of the pDistinct
151224 ** list, or else the WHERE clause contains a term of the form "col=X",
151225 ** where X is a constant value. The collation sequences of the
151226 ** comparison and select-list expressions must match those of the index.
151227 **
151228 ** 3. All of those index columns for which the WHERE clause does not
151229 ** contain a "col=X" term are subject to a NOT NULL constraint.
151230 */
151231 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
151232 if( !IsUniqueIndex(pIdx) ) continue;
151233 if( pIdx->pPartIdxWhere ) continue;
151234 for(i=0; i<pIdx->nKeyCol; i++){
151235 if( 0==sqlite3WhereFindTerm(pWC, iCur: iBase, iColumn: i, notReady: ~(Bitmask)0, WO_EQ, pIdx) ){
151236 if( findIndexCol(pParse, pList: pDistinct, iBase, pIdx, iCol: i)<0 ) break;
151237 if( indexColumnNotNull(pIdx, iCol: i)==0 ) break;
151238 }
151239 }
151240 if( i==pIdx->nKeyCol ){
151241 /* This index implies that the DISTINCT qualifier is redundant. */
151242 return 1;
151243 }
151244 }
151245
151246 return 0;
151247}
151248
151249
151250/*
151251** Estimate the logarithm of the input value to base 2.
151252*/
151253static LogEst estLog(LogEst N){
151254 return N<=10 ? 0 : sqlite3LogEst(x: N) - 33;
151255}
151256
151257/*
151258** Convert OP_Column opcodes to OP_Copy in previously generated code.
151259**
151260** This routine runs over generated VDBE code and translates OP_Column
151261** opcodes into OP_Copy when the table is being accessed via co-routine
151262** instead of via table lookup.
151263**
151264** If the iAutoidxCur is not zero, then any OP_Rowid instructions on
151265** cursor iTabCur are transformed into OP_Sequence opcode for the
151266** iAutoidxCur cursor, in order to generate unique rowids for the
151267** automatic index being generated.
151268*/
151269static void translateColumnToCopy(
151270 Parse *pParse, /* Parsing context */
151271 int iStart, /* Translate from this opcode to the end */
151272 int iTabCur, /* OP_Column/OP_Rowid references to this table */
151273 int iRegister, /* The first column is in this register */
151274 int iAutoidxCur /* If non-zero, cursor of autoindex being generated */
151275){
151276 Vdbe *v = pParse->pVdbe;
151277 VdbeOp *pOp = sqlite3VdbeGetOp(p: v, addr: iStart);
151278 int iEnd = sqlite3VdbeCurrentAddr(p: v);
151279 if( pParse->db->mallocFailed ) return;
151280 for(; iStart<iEnd; iStart++, pOp++){
151281 if( pOp->p1!=iTabCur ) continue;
151282 if( pOp->opcode==OP_Column ){
151283 pOp->opcode = OP_Copy;
151284 pOp->p1 = pOp->p2 + iRegister;
151285 pOp->p2 = pOp->p3;
151286 pOp->p3 = 0;
151287 }else if( pOp->opcode==OP_Rowid ){
151288 pOp->opcode = OP_Sequence;
151289 pOp->p1 = iAutoidxCur;
151290#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
151291 if( iAutoidxCur==0 ){
151292 pOp->opcode = OP_Null;
151293 pOp->p3 = 0;
151294 }
151295#endif
151296 }
151297 }
151298}
151299
151300/*
151301** Two routines for printing the content of an sqlite3_index_info
151302** structure. Used for testing and debugging only. If neither
151303** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
151304** are no-ops.
151305*/
151306#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED)
151307static void whereTraceIndexInfoInputs(sqlite3_index_info *p){
151308 int i;
151309 if( !sqlite3WhereTrace ) return;
151310 for(i=0; i<p->nConstraint; i++){
151311 sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n",
151312 i,
151313 p->aConstraint[i].iColumn,
151314 p->aConstraint[i].iTermOffset,
151315 p->aConstraint[i].op,
151316 p->aConstraint[i].usable);
151317 }
151318 for(i=0; i<p->nOrderBy; i++){
151319 sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n",
151320 i,
151321 p->aOrderBy[i].iColumn,
151322 p->aOrderBy[i].desc);
151323 }
151324}
151325static void whereTraceIndexInfoOutputs(sqlite3_index_info *p){
151326 int i;
151327 if( !sqlite3WhereTrace ) return;
151328 for(i=0; i<p->nConstraint; i++){
151329 sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n",
151330 i,
151331 p->aConstraintUsage[i].argvIndex,
151332 p->aConstraintUsage[i].omit);
151333 }
151334 sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum);
151335 sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr);
151336 sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed);
151337 sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost);
151338 sqlite3DebugPrintf(" estimatedRows=%lld\n", p->estimatedRows);
151339}
151340#else
151341#define whereTraceIndexInfoInputs(A)
151342#define whereTraceIndexInfoOutputs(A)
151343#endif
151344
151345#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
151346/*
151347** Return TRUE if the WHERE clause term pTerm is of a form where it
151348** could be used with an index to access pSrc, assuming an appropriate
151349** index existed.
151350*/
151351static int termCanDriveIndex(
151352 WhereTerm *pTerm, /* WHERE clause term to check */
151353 SrcItem *pSrc, /* Table we are trying to access */
151354 Bitmask notReady /* Tables in outer loops of the join */
151355){
151356 char aff;
151357 if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
151358 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) return 0;
151359 if( (pSrc->fg.jointype & JT_LEFT)
151360 && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
151361 && (pTerm->eOperator & WO_IS)
151362 ){
151363 /* Cannot use an IS term from the WHERE clause as an index driver for
151364 ** the RHS of a LEFT JOIN. Such a term can only be used if it is from
151365 ** the ON clause. */
151366 return 0;
151367 }
151368 if( (pTerm->prereqRight & notReady)!=0 ) return 0;
151369 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
151370 if( pTerm->u.x.leftColumn<0 ) return 0;
151371 aff = pSrc->pTab->aCol[pTerm->u.x.leftColumn].affinity;
151372 if( !sqlite3IndexAffinityOk(pExpr: pTerm->pExpr, idx_affinity: aff) ) return 0;
151373 testcase( pTerm->pExpr->op==TK_IS );
151374 return 1;
151375}
151376#endif
151377
151378
151379#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
151380/*
151381** Generate code to construct the Index object for an automatic index
151382** and to set up the WhereLevel object pLevel so that the code generator
151383** makes use of the automatic index.
151384*/
151385static void constructAutomaticIndex(
151386 Parse *pParse, /* The parsing context */
151387 WhereClause *pWC, /* The WHERE clause */
151388 SrcItem *pSrc, /* The FROM clause term to get the next index */
151389 Bitmask notReady, /* Mask of cursors that are not available */
151390 WhereLevel *pLevel /* Write new index here */
151391){
151392 int nKeyCol; /* Number of columns in the constructed index */
151393 WhereTerm *pTerm; /* A single term of the WHERE clause */
151394 WhereTerm *pWCEnd; /* End of pWC->a[] */
151395 Index *pIdx; /* Object describing the transient index */
151396 Vdbe *v; /* Prepared statement under construction */
151397 int addrInit; /* Address of the initialization bypass jump */
151398 Table *pTable; /* The table being indexed */
151399 int addrTop; /* Top of the index fill loop */
151400 int regRecord; /* Register holding an index record */
151401 int n; /* Column counter */
151402 int i; /* Loop counter */
151403 int mxBitCol; /* Maximum column in pSrc->colUsed */
151404 CollSeq *pColl; /* Collating sequence to on a column */
151405 WhereLoop *pLoop; /* The Loop object */
151406 char *zNotUsed; /* Extra space on the end of pIdx */
151407 Bitmask idxCols; /* Bitmap of columns used for indexing */
151408 Bitmask extraCols; /* Bitmap of additional columns */
151409 u8 sentWarning = 0; /* True if a warnning has been issued */
151410 Expr *pPartial = 0; /* Partial Index Expression */
151411 int iContinue = 0; /* Jump here to skip excluded rows */
151412 SrcItem *pTabItem; /* FROM clause term being indexed */
151413 int addrCounter = 0; /* Address where integer counter is initialized */
151414 int regBase; /* Array of registers where record is assembled */
151415
151416 /* Generate code to skip over the creation and initialization of the
151417 ** transient index on 2nd and subsequent iterations of the loop. */
151418 v = pParse->pVdbe;
151419 assert( v!=0 );
151420 addrInit = sqlite3VdbeAddOp0(p: v, OP_Once); VdbeCoverage(v);
151421
151422 /* Count the number of columns that will be added to the index
151423 ** and used to match WHERE clause constraints */
151424 nKeyCol = 0;
151425 pTable = pSrc->pTab;
151426 pWCEnd = &pWC->a[pWC->nTerm];
151427 pLoop = pLevel->pWLoop;
151428 idxCols = 0;
151429 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
151430 Expr *pExpr = pTerm->pExpr;
151431 assert( !ExprHasProperty(pExpr, EP_FromJoin) /* prereq always non-zero */
151432 || pExpr->iRightJoinTable!=pSrc->iCursor /* for the right-hand */
151433 || pLoop->prereq!=0 ); /* table of a LEFT JOIN */
151434 if( pLoop->prereq==0
151435 && (pTerm->wtFlags & TERM_VIRTUAL)==0
151436 && !ExprHasProperty(pExpr, EP_FromJoin)
151437 && sqlite3ExprIsTableConstant(p: pExpr, iCur: pSrc->iCursor) ){
151438 pPartial = sqlite3ExprAnd(pParse, pLeft: pPartial,
151439 pRight: sqlite3ExprDup(db: pParse->db, p: pExpr, flags: 0));
151440 }
151441 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
151442 int iCol;
151443 Bitmask cMask;
151444 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
151445 iCol = pTerm->u.x.leftColumn;
151446 cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
151447 testcase( iCol==BMS );
151448 testcase( iCol==BMS-1 );
151449 if( !sentWarning ){
151450 sqlite3_log(SQLITE_WARNING_AUTOINDEX,
151451 zFormat: "automatic index on %s(%s)", pTable->zName,
151452 pTable->aCol[iCol].zCnName);
151453 sentWarning = 1;
151454 }
151455 if( (idxCols & cMask)==0 ){
151456 if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){
151457 goto end_auto_index_create;
151458 }
151459 pLoop->aLTerm[nKeyCol++] = pTerm;
151460 idxCols |= cMask;
151461 }
151462 }
151463 }
151464 assert( nKeyCol>0 || pParse->db->mallocFailed );
151465 pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol;
151466 pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED
151467 | WHERE_AUTO_INDEX;
151468
151469 /* Count the number of additional columns needed to create a
151470 ** covering index. A "covering index" is an index that contains all
151471 ** columns that are needed by the query. With a covering index, the
151472 ** original table never needs to be accessed. Automatic indices must
151473 ** be a covering index because the index will not be updated if the
151474 ** original table changes and the index and table cannot both be used
151475 ** if they go out of sync.
151476 */
151477 extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1));
151478 mxBitCol = MIN(BMS-1,pTable->nCol);
151479 testcase( pTable->nCol==BMS-1 );
151480 testcase( pTable->nCol==BMS-2 );
151481 for(i=0; i<mxBitCol; i++){
151482 if( extraCols & MASKBIT(i) ) nKeyCol++;
151483 }
151484 if( pSrc->colUsed & MASKBIT(BMS-1) ){
151485 nKeyCol += pTable->nCol - BMS + 1;
151486 }
151487
151488 /* Construct the Index object to describe this index */
151489 pIdx = sqlite3AllocateIndexObject(db: pParse->db, nCol: nKeyCol+1, nExtra: 0, ppExtra: &zNotUsed);
151490 if( pIdx==0 ) goto end_auto_index_create;
151491 pLoop->u.btree.pIndex = pIdx;
151492 pIdx->zName = "auto-index";
151493 pIdx->pTable = pTable;
151494 n = 0;
151495 idxCols = 0;
151496 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
151497 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
151498 int iCol;
151499 Bitmask cMask;
151500 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
151501 iCol = pTerm->u.x.leftColumn;
151502 cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
151503 testcase( iCol==BMS-1 );
151504 testcase( iCol==BMS );
151505 if( (idxCols & cMask)==0 ){
151506 Expr *pX = pTerm->pExpr;
151507 idxCols |= cMask;
151508 pIdx->aiColumn[n] = pTerm->u.x.leftColumn;
151509 pColl = sqlite3ExprCompareCollSeq(pParse, p: pX);
151510 assert( pColl!=0 || pParse->nErr>0 ); /* TH3 collate01.800 */
151511 pIdx->azColl[n] = pColl ? pColl->zName : sqlite3StrBINARY;
151512 n++;
151513 }
151514 }
151515 }
151516 assert( (u32)n==pLoop->u.btree.nEq );
151517
151518 /* Add additional columns needed to make the automatic index into
151519 ** a covering index */
151520 for(i=0; i<mxBitCol; i++){
151521 if( extraCols & MASKBIT(i) ){
151522 pIdx->aiColumn[n] = i;
151523 pIdx->azColl[n] = sqlite3StrBINARY;
151524 n++;
151525 }
151526 }
151527 if( pSrc->colUsed & MASKBIT(BMS-1) ){
151528 for(i=BMS-1; i<pTable->nCol; i++){
151529 pIdx->aiColumn[n] = i;
151530 pIdx->azColl[n] = sqlite3StrBINARY;
151531 n++;
151532 }
151533 }
151534 assert( n==nKeyCol );
151535 pIdx->aiColumn[n] = XN_ROWID;
151536 pIdx->azColl[n] = sqlite3StrBINARY;
151537
151538 /* Create the automatic index */
151539 assert( pLevel->iIdxCur>=0 );
151540 pLevel->iIdxCur = pParse->nTab++;
151541 sqlite3VdbeAddOp2(p: v, OP_OpenAutoindex, p1: pLevel->iIdxCur, p2: nKeyCol+1);
151542 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
151543 VdbeComment((v, "for %s", pTable->zName));
151544
151545 /* Fill the automatic index with content */
151546 pTabItem = &pWC->pWInfo->pTabList->a[pLevel->iFrom];
151547 if( pTabItem->fg.viaCoroutine ){
151548 int regYield = pTabItem->regReturn;
151549 addrCounter = sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: 0);
151550 sqlite3VdbeAddOp3(p: v, OP_InitCoroutine, p1: regYield, p2: 0, p3: pTabItem->addrFillSub);
151551 addrTop = sqlite3VdbeAddOp1(p: v, OP_Yield, p1: regYield);
151552 VdbeCoverage(v);
151553 VdbeComment((v, "next row of %s", pTabItem->pTab->zName));
151554 }else{
151555 addrTop = sqlite3VdbeAddOp1(p: v, OP_Rewind, p1: pLevel->iTabCur); VdbeCoverage(v);
151556 }
151557 if( pPartial ){
151558 iContinue = sqlite3VdbeMakeLabel(pParse);
151559 sqlite3ExprIfFalse(pParse, pExpr: pPartial, dest: iContinue, SQLITE_JUMPIFNULL);
151560 pLoop->wsFlags |= WHERE_PARTIALIDX;
151561 }
151562 regRecord = sqlite3GetTempReg(pParse);
151563 regBase = sqlite3GenerateIndexKey(
151564 pParse, pIdx, iDataCur: pLevel->iTabCur, regOut: regRecord, prefixOnly: 0, piPartIdxLabel: 0, pPrior: 0, regPrior: 0
151565 );
151566 sqlite3VdbeAddOp2(p: v, OP_IdxInsert, p1: pLevel->iIdxCur, p2: regRecord);
151567 sqlite3VdbeChangeP5(p: v, OPFLAG_USESEEKRESULT);
151568 if( pPartial ) sqlite3VdbeResolveLabel(v, x: iContinue);
151569 if( pTabItem->fg.viaCoroutine ){
151570 sqlite3VdbeChangeP2(p: v, addr: addrCounter, val: regBase+n);
151571 testcase( pParse->db->mallocFailed );
151572 assert( pLevel->iIdxCur>0 );
151573 translateColumnToCopy(pParse, iStart: addrTop, iTabCur: pLevel->iTabCur,
151574 iRegister: pTabItem->regResult, iAutoidxCur: pLevel->iIdxCur);
151575 sqlite3VdbeGoto(p: v, iDest: addrTop);
151576 pTabItem->fg.viaCoroutine = 0;
151577 }else{
151578 sqlite3VdbeAddOp2(p: v, OP_Next, p1: pLevel->iTabCur, p2: addrTop+1); VdbeCoverage(v);
151579 sqlite3VdbeChangeP5(p: v, SQLITE_STMTSTATUS_AUTOINDEX);
151580 }
151581 sqlite3VdbeJumpHere(p: v, addr: addrTop);
151582 sqlite3ReleaseTempReg(pParse, iReg: regRecord);
151583
151584 /* Jump here when skipping the initialization */
151585 sqlite3VdbeJumpHere(p: v, addr: addrInit);
151586
151587end_auto_index_create:
151588 sqlite3ExprDelete(db: pParse->db, p: pPartial);
151589}
151590#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
151591
151592#ifndef SQLITE_OMIT_VIRTUALTABLE
151593/*
151594** Allocate and populate an sqlite3_index_info structure. It is the
151595** responsibility of the caller to eventually release the structure
151596** by passing the pointer returned by this function to sqlite3_free().
151597*/
151598static sqlite3_index_info *allocateIndexInfo(
151599 Parse *pParse, /* The parsing context */
151600 WhereClause *pWC, /* The WHERE clause being analyzed */
151601 Bitmask mUnusable, /* Ignore terms with these prereqs */
151602 SrcItem *pSrc, /* The FROM clause term that is the vtab */
151603 ExprList *pOrderBy, /* The ORDER BY clause */
151604 u16 *pmNoOmit /* Mask of terms not to omit */
151605){
151606 int i, j;
151607 int nTerm;
151608 struct sqlite3_index_constraint *pIdxCons;
151609 struct sqlite3_index_orderby *pIdxOrderBy;
151610 struct sqlite3_index_constraint_usage *pUsage;
151611 struct HiddenIndexInfo *pHidden;
151612 WhereTerm *pTerm;
151613 int nOrderBy;
151614 sqlite3_index_info *pIdxInfo;
151615 u16 mNoOmit = 0;
151616
151617 /* Count the number of possible WHERE clause constraints referring
151618 ** to this virtual table */
151619 for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
151620 if( pTerm->leftCursor != pSrc->iCursor ) continue;
151621 if( pTerm->prereqRight & mUnusable ) continue;
151622 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
151623 testcase( pTerm->eOperator & WO_IN );
151624 testcase( pTerm->eOperator & WO_ISNULL );
151625 testcase( pTerm->eOperator & WO_IS );
151626 testcase( pTerm->eOperator & WO_ALL );
151627 if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue;
151628 if( pTerm->wtFlags & TERM_VNULL ) continue;
151629 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
151630 assert( pTerm->u.x.leftColumn>=(-1) );
151631 nTerm++;
151632 }
151633
151634 /* If the ORDER BY clause contains only columns in the current
151635 ** virtual table then allocate space for the aOrderBy part of
151636 ** the sqlite3_index_info structure.
151637 */
151638 nOrderBy = 0;
151639 if( pOrderBy ){
151640 int n = pOrderBy->nExpr;
151641 for(i=0; i<n; i++){
151642 Expr *pExpr = pOrderBy->a[i].pExpr;
151643 if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
151644 if( pOrderBy->a[i].sortFlags & KEYINFO_ORDER_BIGNULL ) break;
151645 }
151646 if( i==n){
151647 nOrderBy = n;
151648 }
151649 }
151650
151651 /* Allocate the sqlite3_index_info structure
151652 */
151653 pIdxInfo = sqlite3DbMallocZero(db: pParse->db, n: sizeof(*pIdxInfo)
151654 + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
151655 + sizeof(*pIdxOrderBy)*nOrderBy + sizeof(*pHidden) );
151656 if( pIdxInfo==0 ){
151657 sqlite3ErrorMsg(pParse, zFormat: "out of memory");
151658 return 0;
151659 }
151660 pHidden = (struct HiddenIndexInfo*)&pIdxInfo[1];
151661 pIdxCons = (struct sqlite3_index_constraint*)&pHidden[1];
151662 pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
151663 pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
151664 pIdxInfo->nOrderBy = nOrderBy;
151665 pIdxInfo->aConstraint = pIdxCons;
151666 pIdxInfo->aOrderBy = pIdxOrderBy;
151667 pIdxInfo->aConstraintUsage = pUsage;
151668 pHidden->pWC = pWC;
151669 pHidden->pParse = pParse;
151670 for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
151671 u16 op;
151672 if( pTerm->leftCursor != pSrc->iCursor ) continue;
151673 if( pTerm->prereqRight & mUnusable ) continue;
151674 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
151675 testcase( pTerm->eOperator & WO_IN );
151676 testcase( pTerm->eOperator & WO_IS );
151677 testcase( pTerm->eOperator & WO_ISNULL );
151678 testcase( pTerm->eOperator & WO_ALL );
151679 if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue;
151680 if( pTerm->wtFlags & TERM_VNULL ) continue;
151681
151682 /* tag-20191211-002: WHERE-clause constraints are not useful to the
151683 ** right-hand table of a LEFT JOIN. See tag-20191211-001 for the
151684 ** equivalent restriction for ordinary tables. */
151685 if( (pSrc->fg.jointype & JT_LEFT)!=0
151686 && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
151687 ){
151688 continue;
151689 }
151690 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
151691 assert( pTerm->u.x.leftColumn>=(-1) );
151692 pIdxCons[j].iColumn = pTerm->u.x.leftColumn;
151693 pIdxCons[j].iTermOffset = i;
151694 op = pTerm->eOperator & WO_ALL;
151695 if( op==WO_IN ) op = WO_EQ;
151696 if( op==WO_AUX ){
151697 pIdxCons[j].op = pTerm->eMatchOp;
151698 }else if( op & (WO_ISNULL|WO_IS) ){
151699 if( op==WO_ISNULL ){
151700 pIdxCons[j].op = SQLITE_INDEX_CONSTRAINT_ISNULL;
151701 }else{
151702 pIdxCons[j].op = SQLITE_INDEX_CONSTRAINT_IS;
151703 }
151704 }else{
151705 pIdxCons[j].op = (u8)op;
151706 /* The direct assignment in the previous line is possible only because
151707 ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The
151708 ** following asserts verify this fact. */
151709 assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ );
151710 assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT );
151711 assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE );
151712 assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT );
151713 assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE );
151714 assert( pTerm->eOperator&(WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_AUX) );
151715
151716 if( op & (WO_LT|WO_LE|WO_GT|WO_GE)
151717 && sqlite3ExprIsVector(pExpr: pTerm->pExpr->pRight)
151718 ){
151719 testcase( j!=i );
151720 if( j<16 ) mNoOmit |= (1 << j);
151721 if( op==WO_LT ) pIdxCons[j].op = WO_LE;
151722 if( op==WO_GT ) pIdxCons[j].op = WO_GE;
151723 }
151724 }
151725
151726 j++;
151727 }
151728 pIdxInfo->nConstraint = j;
151729 for(i=0; i<nOrderBy; i++){
151730 Expr *pExpr = pOrderBy->a[i].pExpr;
151731 pIdxOrderBy[i].iColumn = pExpr->iColumn;
151732 pIdxOrderBy[i].desc = pOrderBy->a[i].sortFlags & KEYINFO_ORDER_DESC;
151733 }
151734
151735 *pmNoOmit = mNoOmit;
151736 return pIdxInfo;
151737}
151738
151739/*
151740** The table object reference passed as the second argument to this function
151741** must represent a virtual table. This function invokes the xBestIndex()
151742** method of the virtual table with the sqlite3_index_info object that
151743** comes in as the 3rd argument to this function.
151744**
151745** If an error occurs, pParse is populated with an error message and an
151746** appropriate error code is returned. A return of SQLITE_CONSTRAINT from
151747** xBestIndex is not considered an error. SQLITE_CONSTRAINT indicates that
151748** the current configuration of "unusable" flags in sqlite3_index_info can
151749** not result in a valid plan.
151750**
151751** Whether or not an error is returned, it is the responsibility of the
151752** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
151753** that this is required.
151754*/
151755static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
151756 sqlite3_vtab *pVtab = sqlite3GetVTable(db: pParse->db, pTab)->pVtab;
151757 int rc;
151758
151759 whereTraceIndexInfoInputs(p);
151760 rc = pVtab->pModule->xBestIndex(pVtab, p);
151761 whereTraceIndexInfoOutputs(p);
151762
151763 if( rc!=SQLITE_OK && rc!=SQLITE_CONSTRAINT ){
151764 if( rc==SQLITE_NOMEM ){
151765 sqlite3OomFault(db: pParse->db);
151766 }else if( !pVtab->zErrMsg ){
151767 sqlite3ErrorMsg(pParse, zFormat: "%s", sqlite3ErrStr(rc));
151768 }else{
151769 sqlite3ErrorMsg(pParse, zFormat: "%s", pVtab->zErrMsg);
151770 }
151771 }
151772 sqlite3_free(p: pVtab->zErrMsg);
151773 pVtab->zErrMsg = 0;
151774 return rc;
151775}
151776#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
151777
151778#ifdef SQLITE_ENABLE_STAT4
151779/*
151780** Estimate the location of a particular key among all keys in an
151781** index. Store the results in aStat as follows:
151782**
151783** aStat[0] Est. number of rows less than pRec
151784** aStat[1] Est. number of rows equal to pRec
151785**
151786** Return the index of the sample that is the smallest sample that
151787** is greater than or equal to pRec. Note that this index is not an index
151788** into the aSample[] array - it is an index into a virtual set of samples
151789** based on the contents of aSample[] and the number of fields in record
151790** pRec.
151791*/
151792static int whereKeyStats(
151793 Parse *pParse, /* Database connection */
151794 Index *pIdx, /* Index to consider domain of */
151795 UnpackedRecord *pRec, /* Vector of values to consider */
151796 int roundUp, /* Round up if true. Round down if false */
151797 tRowcnt *aStat /* OUT: stats written here */
151798){
151799 IndexSample *aSample = pIdx->aSample;
151800 int iCol; /* Index of required stats in anEq[] etc. */
151801 int i; /* Index of first sample >= pRec */
151802 int iSample; /* Smallest sample larger than or equal to pRec */
151803 int iMin = 0; /* Smallest sample not yet tested */
151804 int iTest; /* Next sample to test */
151805 int res; /* Result of comparison operation */
151806 int nField; /* Number of fields in pRec */
151807 tRowcnt iLower = 0; /* anLt[] + anEq[] of largest sample pRec is > */
151808
151809#ifndef SQLITE_DEBUG
151810 UNUSED_PARAMETER( pParse );
151811#endif
151812 assert( pRec!=0 );
151813 assert( pIdx->nSample>0 );
151814 assert( pRec->nField>0 && pRec->nField<=pIdx->nSampleCol );
151815
151816 /* Do a binary search to find the first sample greater than or equal
151817 ** to pRec. If pRec contains a single field, the set of samples to search
151818 ** is simply the aSample[] array. If the samples in aSample[] contain more
151819 ** than one fields, all fields following the first are ignored.
151820 **
151821 ** If pRec contains N fields, where N is more than one, then as well as the
151822 ** samples in aSample[] (truncated to N fields), the search also has to
151823 ** consider prefixes of those samples. For example, if the set of samples
151824 ** in aSample is:
151825 **
151826 ** aSample[0] = (a, 5)
151827 ** aSample[1] = (a, 10)
151828 ** aSample[2] = (b, 5)
151829 ** aSample[3] = (c, 100)
151830 ** aSample[4] = (c, 105)
151831 **
151832 ** Then the search space should ideally be the samples above and the
151833 ** unique prefixes [a], [b] and [c]. But since that is hard to organize,
151834 ** the code actually searches this set:
151835 **
151836 ** 0: (a)
151837 ** 1: (a, 5)
151838 ** 2: (a, 10)
151839 ** 3: (a, 10)
151840 ** 4: (b)
151841 ** 5: (b, 5)
151842 ** 6: (c)
151843 ** 7: (c, 100)
151844 ** 8: (c, 105)
151845 ** 9: (c, 105)
151846 **
151847 ** For each sample in the aSample[] array, N samples are present in the
151848 ** effective sample array. In the above, samples 0 and 1 are based on
151849 ** sample aSample[0]. Samples 2 and 3 on aSample[1] etc.
151850 **
151851 ** Often, sample i of each block of N effective samples has (i+1) fields.
151852 ** Except, each sample may be extended to ensure that it is greater than or
151853 ** equal to the previous sample in the array. For example, in the above,
151854 ** sample 2 is the first sample of a block of N samples, so at first it
151855 ** appears that it should be 1 field in size. However, that would make it
151856 ** smaller than sample 1, so the binary search would not work. As a result,
151857 ** it is extended to two fields. The duplicates that this creates do not
151858 ** cause any problems.
151859 */
151860 nField = pRec->nField;
151861 iCol = 0;
151862 iSample = pIdx->nSample * nField;
151863 do{
151864 int iSamp; /* Index in aSample[] of test sample */
151865 int n; /* Number of fields in test sample */
151866
151867 iTest = (iMin+iSample)/2;
151868 iSamp = iTest / nField;
151869 if( iSamp>0 ){
151870 /* The proposed effective sample is a prefix of sample aSample[iSamp].
151871 ** Specifically, the shortest prefix of at least (1 + iTest%nField)
151872 ** fields that is greater than the previous effective sample. */
151873 for(n=(iTest % nField) + 1; n<nField; n++){
151874 if( aSample[iSamp-1].anLt[n-1]!=aSample[iSamp].anLt[n-1] ) break;
151875 }
151876 }else{
151877 n = iTest + 1;
151878 }
151879
151880 pRec->nField = n;
151881 res = sqlite3VdbeRecordCompare(aSample[iSamp].n, aSample[iSamp].p, pRec);
151882 if( res<0 ){
151883 iLower = aSample[iSamp].anLt[n-1] + aSample[iSamp].anEq[n-1];
151884 iMin = iTest+1;
151885 }else if( res==0 && n<nField ){
151886 iLower = aSample[iSamp].anLt[n-1];
151887 iMin = iTest+1;
151888 res = -1;
151889 }else{
151890 iSample = iTest;
151891 iCol = n-1;
151892 }
151893 }while( res && iMin<iSample );
151894 i = iSample / nField;
151895
151896#ifdef SQLITE_DEBUG
151897 /* The following assert statements check that the binary search code
151898 ** above found the right answer. This block serves no purpose other
151899 ** than to invoke the asserts. */
151900 if( pParse->db->mallocFailed==0 ){
151901 if( res==0 ){
151902 /* If (res==0) is true, then pRec must be equal to sample i. */
151903 assert( i<pIdx->nSample );
151904 assert( iCol==nField-1 );
151905 pRec->nField = nField;
151906 assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)
151907 || pParse->db->mallocFailed
151908 );
151909 }else{
151910 /* Unless i==pIdx->nSample, indicating that pRec is larger than
151911 ** all samples in the aSample[] array, pRec must be smaller than the
151912 ** (iCol+1) field prefix of sample i. */
151913 assert( i<=pIdx->nSample && i>=0 );
151914 pRec->nField = iCol+1;
151915 assert( i==pIdx->nSample
151916 || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0
151917 || pParse->db->mallocFailed );
151918
151919 /* if i==0 and iCol==0, then record pRec is smaller than all samples
151920 ** in the aSample[] array. Otherwise, if (iCol>0) then pRec must
151921 ** be greater than or equal to the (iCol) field prefix of sample i.
151922 ** If (i>0), then pRec must also be greater than sample (i-1). */
151923 if( iCol>0 ){
151924 pRec->nField = iCol;
151925 assert( sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)<=0
151926 || pParse->db->mallocFailed );
151927 }
151928 if( i>0 ){
151929 pRec->nField = nField;
151930 assert( sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
151931 || pParse->db->mallocFailed );
151932 }
151933 }
151934 }
151935#endif /* ifdef SQLITE_DEBUG */
151936
151937 if( res==0 ){
151938 /* Record pRec is equal to sample i */
151939 assert( iCol==nField-1 );
151940 aStat[0] = aSample[i].anLt[iCol];
151941 aStat[1] = aSample[i].anEq[iCol];
151942 }else{
151943 /* At this point, the (iCol+1) field prefix of aSample[i] is the first
151944 ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec
151945 ** is larger than all samples in the array. */
151946 tRowcnt iUpper, iGap;
151947 if( i>=pIdx->nSample ){
151948 iUpper = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]);
151949 }else{
151950 iUpper = aSample[i].anLt[iCol];
151951 }
151952
151953 if( iLower>=iUpper ){
151954 iGap = 0;
151955 }else{
151956 iGap = iUpper - iLower;
151957 }
151958 if( roundUp ){
151959 iGap = (iGap*2)/3;
151960 }else{
151961 iGap = iGap/3;
151962 }
151963 aStat[0] = iLower + iGap;
151964 aStat[1] = pIdx->aAvgEq[nField-1];
151965 }
151966
151967 /* Restore the pRec->nField value before returning. */
151968 pRec->nField = nField;
151969 return i;
151970}
151971#endif /* SQLITE_ENABLE_STAT4 */
151972
151973/*
151974** If it is not NULL, pTerm is a term that provides an upper or lower
151975** bound on a range scan. Without considering pTerm, it is estimated
151976** that the scan will visit nNew rows. This function returns the number
151977** estimated to be visited after taking pTerm into account.
151978**
151979** If the user explicitly specified a likelihood() value for this term,
151980** then the return value is the likelihood multiplied by the number of
151981** input rows. Otherwise, this function assumes that an "IS NOT NULL" term
151982** has a likelihood of 0.50, and any other term a likelihood of 0.25.
151983*/
151984static LogEst whereRangeAdjust(WhereTerm *pTerm, LogEst nNew){
151985 LogEst nRet = nNew;
151986 if( pTerm ){
151987 if( pTerm->truthProb<=0 ){
151988 nRet += pTerm->truthProb;
151989 }else if( (pTerm->wtFlags & TERM_VNULL)==0 ){
151990 nRet -= 20; assert( 20==sqlite3LogEst(4) );
151991 }
151992 }
151993 return nRet;
151994}
151995
151996
151997#ifdef SQLITE_ENABLE_STAT4
151998/*
151999** Return the affinity for a single column of an index.
152000*/
152001SQLITE_PRIVATE char sqlite3IndexColumnAffinity(sqlite3 *db, Index *pIdx, int iCol){
152002 assert( iCol>=0 && iCol<pIdx->nColumn );
152003 if( !pIdx->zColAff ){
152004 if( sqlite3IndexAffinityStr(db, pIdx)==0 ) return SQLITE_AFF_BLOB;
152005 }
152006 assert( pIdx->zColAff[iCol]!=0 );
152007 return pIdx->zColAff[iCol];
152008}
152009#endif
152010
152011
152012#ifdef SQLITE_ENABLE_STAT4
152013/*
152014** This function is called to estimate the number of rows visited by a
152015** range-scan on a skip-scan index. For example:
152016**
152017** CREATE INDEX i1 ON t1(a, b, c);
152018** SELECT * FROM t1 WHERE a=? AND c BETWEEN ? AND ?;
152019**
152020** Value pLoop->nOut is currently set to the estimated number of rows
152021** visited for scanning (a=? AND b=?). This function reduces that estimate
152022** by some factor to account for the (c BETWEEN ? AND ?) expression based
152023** on the stat4 data for the index. this scan will be peformed multiple
152024** times (once for each (a,b) combination that matches a=?) is dealt with
152025** by the caller.
152026**
152027** It does this by scanning through all stat4 samples, comparing values
152028** extracted from pLower and pUpper with the corresponding column in each
152029** sample. If L and U are the number of samples found to be less than or
152030** equal to the values extracted from pLower and pUpper respectively, and
152031** N is the total number of samples, the pLoop->nOut value is adjusted
152032** as follows:
152033**
152034** nOut = nOut * ( min(U - L, 1) / N )
152035**
152036** If pLower is NULL, or a value cannot be extracted from the term, L is
152037** set to zero. If pUpper is NULL, or a value cannot be extracted from it,
152038** U is set to N.
152039**
152040** Normally, this function sets *pbDone to 1 before returning. However,
152041** if no value can be extracted from either pLower or pUpper (and so the
152042** estimate of the number of rows delivered remains unchanged), *pbDone
152043** is left as is.
152044**
152045** If an error occurs, an SQLite error code is returned. Otherwise,
152046** SQLITE_OK.
152047*/
152048static int whereRangeSkipScanEst(
152049 Parse *pParse, /* Parsing & code generating context */
152050 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
152051 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
152052 WhereLoop *pLoop, /* Update the .nOut value of this loop */
152053 int *pbDone /* Set to true if at least one expr. value extracted */
152054){
152055 Index *p = pLoop->u.btree.pIndex;
152056 int nEq = pLoop->u.btree.nEq;
152057 sqlite3 *db = pParse->db;
152058 int nLower = -1;
152059 int nUpper = p->nSample+1;
152060 int rc = SQLITE_OK;
152061 u8 aff = sqlite3IndexColumnAffinity(db, p, nEq);
152062 CollSeq *pColl;
152063
152064 sqlite3_value *p1 = 0; /* Value extracted from pLower */
152065 sqlite3_value *p2 = 0; /* Value extracted from pUpper */
152066 sqlite3_value *pVal = 0; /* Value extracted from record */
152067
152068 pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]);
152069 if( pLower ){
152070 rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1);
152071 nLower = 0;
152072 }
152073 if( pUpper && rc==SQLITE_OK ){
152074 rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2);
152075 nUpper = p2 ? 0 : p->nSample;
152076 }
152077
152078 if( p1 || p2 ){
152079 int i;
152080 int nDiff;
152081 for(i=0; rc==SQLITE_OK && i<p->nSample; i++){
152082 rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal);
152083 if( rc==SQLITE_OK && p1 ){
152084 int res = sqlite3MemCompare(p1, pVal, pColl);
152085 if( res>=0 ) nLower++;
152086 }
152087 if( rc==SQLITE_OK && p2 ){
152088 int res = sqlite3MemCompare(p2, pVal, pColl);
152089 if( res>=0 ) nUpper++;
152090 }
152091 }
152092 nDiff = (nUpper - nLower);
152093 if( nDiff<=0 ) nDiff = 1;
152094
152095 /* If there is both an upper and lower bound specified, and the
152096 ** comparisons indicate that they are close together, use the fallback
152097 ** method (assume that the scan visits 1/64 of the rows) for estimating
152098 ** the number of rows visited. Otherwise, estimate the number of rows
152099 ** using the method described in the header comment for this function. */
152100 if( nDiff!=1 || pUpper==0 || pLower==0 ){
152101 int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff));
152102 pLoop->nOut -= nAdjust;
152103 *pbDone = 1;
152104 WHERETRACE(0x10, ("range skip-scan regions: %u..%u adjust=%d est=%d\n",
152105 nLower, nUpper, nAdjust*-1, pLoop->nOut));
152106 }
152107
152108 }else{
152109 assert( *pbDone==0 );
152110 }
152111
152112 sqlite3ValueFree(p1);
152113 sqlite3ValueFree(p2);
152114 sqlite3ValueFree(pVal);
152115
152116 return rc;
152117}
152118#endif /* SQLITE_ENABLE_STAT4 */
152119
152120/*
152121** This function is used to estimate the number of rows that will be visited
152122** by scanning an index for a range of values. The range may have an upper
152123** bound, a lower bound, or both. The WHERE clause terms that set the upper
152124** and lower bounds are represented by pLower and pUpper respectively. For
152125** example, assuming that index p is on t1(a):
152126**
152127** ... FROM t1 WHERE a > ? AND a < ? ...
152128** |_____| |_____|
152129** | |
152130** pLower pUpper
152131**
152132** If either of the upper or lower bound is not present, then NULL is passed in
152133** place of the corresponding WhereTerm.
152134**
152135** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index
152136** column subject to the range constraint. Or, equivalently, the number of
152137** equality constraints optimized by the proposed index scan. For example,
152138** assuming index p is on t1(a, b), and the SQL query is:
152139**
152140** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
152141**
152142** then nEq is set to 1 (as the range restricted column, b, is the second
152143** left-most column of the index). Or, if the query is:
152144**
152145** ... FROM t1 WHERE a > ? AND a < ? ...
152146**
152147** then nEq is set to 0.
152148**
152149** When this function is called, *pnOut is set to the sqlite3LogEst() of the
152150** number of rows that the index scan is expected to visit without
152151** considering the range constraints. If nEq is 0, then *pnOut is the number of
152152** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced)
152153** to account for the range constraints pLower and pUpper.
152154**
152155** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be
152156** used, a single range inequality reduces the search space by a factor of 4.
152157** and a pair of constraints (x>? AND x<?) reduces the expected number of
152158** rows visited by a factor of 64.
152159*/
152160static int whereRangeScanEst(
152161 Parse *pParse, /* Parsing & code generating context */
152162 WhereLoopBuilder *pBuilder,
152163 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
152164 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
152165 WhereLoop *pLoop /* Modify the .nOut and maybe .rRun fields */
152166){
152167 int rc = SQLITE_OK;
152168 int nOut = pLoop->nOut;
152169 LogEst nNew;
152170
152171#ifdef SQLITE_ENABLE_STAT4
152172 Index *p = pLoop->u.btree.pIndex;
152173 int nEq = pLoop->u.btree.nEq;
152174
152175 if( p->nSample>0 && ALWAYS(nEq<p->nSampleCol)
152176 && OptimizationEnabled(pParse->db, SQLITE_Stat4)
152177 ){
152178 if( nEq==pBuilder->nRecValid ){
152179 UnpackedRecord *pRec = pBuilder->pRec;
152180 tRowcnt a[2];
152181 int nBtm = pLoop->u.btree.nBtm;
152182 int nTop = pLoop->u.btree.nTop;
152183
152184 /* Variable iLower will be set to the estimate of the number of rows in
152185 ** the index that are less than the lower bound of the range query. The
152186 ** lower bound being the concatenation of $P and $L, where $P is the
152187 ** key-prefix formed by the nEq values matched against the nEq left-most
152188 ** columns of the index, and $L is the value in pLower.
152189 **
152190 ** Or, if pLower is NULL or $L cannot be extracted from it (because it
152191 ** is not a simple variable or literal value), the lower bound of the
152192 ** range is $P. Due to a quirk in the way whereKeyStats() works, even
152193 ** if $L is available, whereKeyStats() is called for both ($P) and
152194 ** ($P:$L) and the larger of the two returned values is used.
152195 **
152196 ** Similarly, iUpper is to be set to the estimate of the number of rows
152197 ** less than the upper bound of the range query. Where the upper bound
152198 ** is either ($P) or ($P:$U). Again, even if $U is available, both values
152199 ** of iUpper are requested of whereKeyStats() and the smaller used.
152200 **
152201 ** The number of rows between the two bounds is then just iUpper-iLower.
152202 */
152203 tRowcnt iLower; /* Rows less than the lower bound */
152204 tRowcnt iUpper; /* Rows less than the upper bound */
152205 int iLwrIdx = -2; /* aSample[] for the lower bound */
152206 int iUprIdx = -1; /* aSample[] for the upper bound */
152207
152208 if( pRec ){
152209 testcase( pRec->nField!=pBuilder->nRecValid );
152210 pRec->nField = pBuilder->nRecValid;
152211 }
152212 /* Determine iLower and iUpper using ($P) only. */
152213 if( nEq==0 ){
152214 iLower = 0;
152215 iUpper = p->nRowEst0;
152216 }else{
152217 /* Note: this call could be optimized away - since the same values must
152218 ** have been requested when testing key $P in whereEqualScanEst(). */
152219 whereKeyStats(pParse, p, pRec, 0, a);
152220 iLower = a[0];
152221 iUpper = a[0] + a[1];
152222 }
152223
152224 assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 );
152225 assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
152226 assert( p->aSortOrder!=0 );
152227 if( p->aSortOrder[nEq] ){
152228 /* The roles of pLower and pUpper are swapped for a DESC index */
152229 SWAP(WhereTerm*, pLower, pUpper);
152230 SWAP(int, nBtm, nTop);
152231 }
152232
152233 /* If possible, improve on the iLower estimate using ($P:$L). */
152234 if( pLower ){
152235 int n; /* Values extracted from pExpr */
152236 Expr *pExpr = pLower->pExpr->pRight;
152237 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, nBtm, nEq, &n);
152238 if( rc==SQLITE_OK && n ){
152239 tRowcnt iNew;
152240 u16 mask = WO_GT|WO_LE;
152241 if( sqlite3ExprVectorSize(pExpr)>n ) mask = (WO_LE|WO_LT);
152242 iLwrIdx = whereKeyStats(pParse, p, pRec, 0, a);
152243 iNew = a[0] + ((pLower->eOperator & mask) ? a[1] : 0);
152244 if( iNew>iLower ) iLower = iNew;
152245 nOut--;
152246 pLower = 0;
152247 }
152248 }
152249
152250 /* If possible, improve on the iUpper estimate using ($P:$U). */
152251 if( pUpper ){
152252 int n; /* Values extracted from pExpr */
152253 Expr *pExpr = pUpper->pExpr->pRight;
152254 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, nTop, nEq, &n);
152255 if( rc==SQLITE_OK && n ){
152256 tRowcnt iNew;
152257 u16 mask = WO_GT|WO_LE;
152258 if( sqlite3ExprVectorSize(pExpr)>n ) mask = (WO_LE|WO_LT);
152259 iUprIdx = whereKeyStats(pParse, p, pRec, 1, a);
152260 iNew = a[0] + ((pUpper->eOperator & mask) ? a[1] : 0);
152261 if( iNew<iUpper ) iUpper = iNew;
152262 nOut--;
152263 pUpper = 0;
152264 }
152265 }
152266
152267 pBuilder->pRec = pRec;
152268 if( rc==SQLITE_OK ){
152269 if( iUpper>iLower ){
152270 nNew = sqlite3LogEst(iUpper - iLower);
152271 /* TUNING: If both iUpper and iLower are derived from the same
152272 ** sample, then assume they are 4x more selective. This brings
152273 ** the estimated selectivity more in line with what it would be
152274 ** if estimated without the use of STAT4 tables. */
152275 if( iLwrIdx==iUprIdx ) nNew -= 20; assert( 20==sqlite3LogEst(4) );
152276 }else{
152277 nNew = 10; assert( 10==sqlite3LogEst(2) );
152278 }
152279 if( nNew<nOut ){
152280 nOut = nNew;
152281 }
152282 WHERETRACE(0x10, ("STAT4 range scan: %u..%u est=%d\n",
152283 (u32)iLower, (u32)iUpper, nOut));
152284 }
152285 }else{
152286 int bDone = 0;
152287 rc = whereRangeSkipScanEst(pParse, pLower, pUpper, pLoop, &bDone);
152288 if( bDone ) return rc;
152289 }
152290 }
152291#else
152292 UNUSED_PARAMETER(pParse);
152293 UNUSED_PARAMETER(pBuilder);
152294 assert( pLower || pUpper );
152295#endif
152296 assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 );
152297 nNew = whereRangeAdjust(pTerm: pLower, nNew: nOut);
152298 nNew = whereRangeAdjust(pTerm: pUpper, nNew);
152299
152300 /* TUNING: If there is both an upper and lower limit and neither limit
152301 ** has an application-defined likelihood(), assume the range is
152302 ** reduced by an additional 75%. This means that, by default, an open-ended
152303 ** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the
152304 ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to
152305 ** match 1/64 of the index. */
152306 if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){
152307 nNew -= 20;
152308 }
152309
152310 nOut -= (pLower!=0) + (pUpper!=0);
152311 if( nNew<10 ) nNew = 10;
152312 if( nNew<nOut ) nOut = nNew;
152313#if defined(WHERETRACE_ENABLED)
152314 if( pLoop->nOut>nOut ){
152315 WHERETRACE(0x10,("Range scan lowers nOut from %d to %d\n",
152316 pLoop->nOut, nOut));
152317 }
152318#endif
152319 pLoop->nOut = (LogEst)nOut;
152320 return rc;
152321}
152322
152323#ifdef SQLITE_ENABLE_STAT4
152324/*
152325** Estimate the number of rows that will be returned based on
152326** an equality constraint x=VALUE and where that VALUE occurs in
152327** the histogram data. This only works when x is the left-most
152328** column of an index and sqlite_stat4 histogram data is available
152329** for that index. When pExpr==NULL that means the constraint is
152330** "x IS NULL" instead of "x=VALUE".
152331**
152332** Write the estimated row count into *pnRow and return SQLITE_OK.
152333** If unable to make an estimate, leave *pnRow unchanged and return
152334** non-zero.
152335**
152336** This routine can fail if it is unable to load a collating sequence
152337** required for string comparison, or if unable to allocate memory
152338** for a UTF conversion required for comparison. The error is stored
152339** in the pParse structure.
152340*/
152341static int whereEqualScanEst(
152342 Parse *pParse, /* Parsing & code generating context */
152343 WhereLoopBuilder *pBuilder,
152344 Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */
152345 tRowcnt *pnRow /* Write the revised row estimate here */
152346){
152347 Index *p = pBuilder->pNew->u.btree.pIndex;
152348 int nEq = pBuilder->pNew->u.btree.nEq;
152349 UnpackedRecord *pRec = pBuilder->pRec;
152350 int rc; /* Subfunction return code */
152351 tRowcnt a[2]; /* Statistics */
152352 int bOk;
152353
152354 assert( nEq>=1 );
152355 assert( nEq<=p->nColumn );
152356 assert( p->aSample!=0 );
152357 assert( p->nSample>0 );
152358 assert( pBuilder->nRecValid<nEq );
152359
152360 /* If values are not available for all fields of the index to the left
152361 ** of this one, no estimate can be made. Return SQLITE_NOTFOUND. */
152362 if( pBuilder->nRecValid<(nEq-1) ){
152363 return SQLITE_NOTFOUND;
152364 }
152365
152366 /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue()
152367 ** below would return the same value. */
152368 if( nEq>=p->nColumn ){
152369 *pnRow = 1;
152370 return SQLITE_OK;
152371 }
152372
152373 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, 1, nEq-1, &bOk);
152374 pBuilder->pRec = pRec;
152375 if( rc!=SQLITE_OK ) return rc;
152376 if( bOk==0 ) return SQLITE_NOTFOUND;
152377 pBuilder->nRecValid = nEq;
152378
152379 whereKeyStats(pParse, p, pRec, 0, a);
152380 WHERETRACE(0x10,("equality scan regions %s(%d): %d\n",
152381 p->zName, nEq-1, (int)a[1]));
152382 *pnRow = a[1];
152383
152384 return rc;
152385}
152386#endif /* SQLITE_ENABLE_STAT4 */
152387
152388#ifdef SQLITE_ENABLE_STAT4
152389/*
152390** Estimate the number of rows that will be returned based on
152391** an IN constraint where the right-hand side of the IN operator
152392** is a list of values. Example:
152393**
152394** WHERE x IN (1,2,3,4)
152395**
152396** Write the estimated row count into *pnRow and return SQLITE_OK.
152397** If unable to make an estimate, leave *pnRow unchanged and return
152398** non-zero.
152399**
152400** This routine can fail if it is unable to load a collating sequence
152401** required for string comparison, or if unable to allocate memory
152402** for a UTF conversion required for comparison. The error is stored
152403** in the pParse structure.
152404*/
152405static int whereInScanEst(
152406 Parse *pParse, /* Parsing & code generating context */
152407 WhereLoopBuilder *pBuilder,
152408 ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
152409 tRowcnt *pnRow /* Write the revised row estimate here */
152410){
152411 Index *p = pBuilder->pNew->u.btree.pIndex;
152412 i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]);
152413 int nRecValid = pBuilder->nRecValid;
152414 int rc = SQLITE_OK; /* Subfunction return code */
152415 tRowcnt nEst; /* Number of rows for a single term */
152416 tRowcnt nRowEst = 0; /* New estimate of the number of rows */
152417 int i; /* Loop counter */
152418
152419 assert( p->aSample!=0 );
152420 for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
152421 nEst = nRow0;
152422 rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
152423 nRowEst += nEst;
152424 pBuilder->nRecValid = nRecValid;
152425 }
152426
152427 if( rc==SQLITE_OK ){
152428 if( nRowEst > nRow0 ) nRowEst = nRow0;
152429 *pnRow = nRowEst;
152430 WHERETRACE(0x10,("IN row estimate: est=%d\n", nRowEst));
152431 }
152432 assert( pBuilder->nRecValid==nRecValid );
152433 return rc;
152434}
152435#endif /* SQLITE_ENABLE_STAT4 */
152436
152437
152438#ifdef WHERETRACE_ENABLED
152439/*
152440** Print the content of a WhereTerm object
152441*/
152442SQLITE_PRIVATE void sqlite3WhereTermPrint(WhereTerm *pTerm, int iTerm){
152443 if( pTerm==0 ){
152444 sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm);
152445 }else{
152446 char zType[8];
152447 char zLeft[50];
152448 memcpy(zType, "....", 5);
152449 if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V';
152450 if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E';
152451 if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L';
152452 if( pTerm->wtFlags & TERM_CODED ) zType[3] = 'C';
152453 if( pTerm->eOperator & WO_SINGLE ){
152454 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
152455 sqlite3_snprintf(sizeof(zLeft),zLeft,"left={%d:%d}",
152456 pTerm->leftCursor, pTerm->u.x.leftColumn);
152457 }else if( (pTerm->eOperator & WO_OR)!=0 && pTerm->u.pOrInfo!=0 ){
152458 sqlite3_snprintf(sizeof(zLeft),zLeft,"indexable=0x%llx",
152459 pTerm->u.pOrInfo->indexable);
152460 }else{
152461 sqlite3_snprintf(sizeof(zLeft),zLeft,"left=%d", pTerm->leftCursor);
152462 }
152463 sqlite3DebugPrintf(
152464 "TERM-%-3d %p %s %-12s op=%03x wtFlags=%04x",
152465 iTerm, pTerm, zType, zLeft, pTerm->eOperator, pTerm->wtFlags);
152466 /* The 0x10000 .wheretrace flag causes extra information to be
152467 ** shown about each Term */
152468 if( sqlite3WhereTrace & 0x10000 ){
152469 sqlite3DebugPrintf(" prob=%-3d prereq=%llx,%llx",
152470 pTerm->truthProb, (u64)pTerm->prereqAll, (u64)pTerm->prereqRight);
152471 }
152472 if( (pTerm->eOperator & (WO_OR|WO_AND))==0 && pTerm->u.x.iField ){
152473 sqlite3DebugPrintf(" iField=%d", pTerm->u.x.iField);
152474 }
152475 if( pTerm->iParent>=0 ){
152476 sqlite3DebugPrintf(" iParent=%d", pTerm->iParent);
152477 }
152478 sqlite3DebugPrintf("\n");
152479 sqlite3TreeViewExpr(0, pTerm->pExpr, 0);
152480 }
152481}
152482#endif
152483
152484#ifdef WHERETRACE_ENABLED
152485/*
152486** Show the complete content of a WhereClause
152487*/
152488SQLITE_PRIVATE void sqlite3WhereClausePrint(WhereClause *pWC){
152489 int i;
152490 for(i=0; i<pWC->nTerm; i++){
152491 sqlite3WhereTermPrint(&pWC->a[i], i);
152492 }
152493}
152494#endif
152495
152496#ifdef WHERETRACE_ENABLED
152497/*
152498** Print a WhereLoop object for debugging purposes
152499*/
152500SQLITE_PRIVATE void sqlite3WhereLoopPrint(WhereLoop *p, WhereClause *pWC){
152501 WhereInfo *pWInfo = pWC->pWInfo;
152502 int nb = 1+(pWInfo->pTabList->nSrc+3)/4;
152503 SrcItem *pItem = pWInfo->pTabList->a + p->iTab;
152504 Table *pTab = pItem->pTab;
152505 Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1;
152506 sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
152507 p->iTab, nb, p->maskSelf, nb, p->prereq & mAll);
152508 sqlite3DebugPrintf(" %12s",
152509 pItem->zAlias ? pItem->zAlias : pTab->zName);
152510 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
152511 const char *zName;
152512 if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
152513 if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){
152514 int i = sqlite3Strlen30(zName) - 1;
152515 while( zName[i]!='_' ) i--;
152516 zName += i;
152517 }
152518 sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq);
152519 }else{
152520 sqlite3DebugPrintf("%20s","");
152521 }
152522 }else{
152523 char *z;
152524 if( p->u.vtab.idxStr ){
152525 z = sqlite3_mprintf("(%d,\"%s\",%#x)",
152526 p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask);
152527 }else{
152528 z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
152529 }
152530 sqlite3DebugPrintf(" %-19s", z);
152531 sqlite3_free(z);
152532 }
152533 if( p->wsFlags & WHERE_SKIPSCAN ){
152534 sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->nSkip);
152535 }else{
152536 sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm);
152537 }
152538 sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
152539 if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){
152540 int i;
152541 for(i=0; i<p->nLTerm; i++){
152542 sqlite3WhereTermPrint(p->aLTerm[i], i);
152543 }
152544 }
152545}
152546#endif
152547
152548/*
152549** Convert bulk memory into a valid WhereLoop that can be passed
152550** to whereLoopClear harmlessly.
152551*/
152552static void whereLoopInit(WhereLoop *p){
152553 p->aLTerm = p->aLTermSpace;
152554 p->nLTerm = 0;
152555 p->nLSlot = ArraySize(p->aLTermSpace);
152556 p->wsFlags = 0;
152557}
152558
152559/*
152560** Clear the WhereLoop.u union. Leave WhereLoop.pLTerm intact.
152561*/
152562static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){
152563 if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){
152564 if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){
152565 sqlite3_free(p: p->u.vtab.idxStr);
152566 p->u.vtab.needFree = 0;
152567 p->u.vtab.idxStr = 0;
152568 }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){
152569 sqlite3DbFree(db, p: p->u.btree.pIndex->zColAff);
152570 sqlite3DbFreeNN(db, p: p->u.btree.pIndex);
152571 p->u.btree.pIndex = 0;
152572 }
152573 }
152574}
152575
152576/*
152577** Deallocate internal memory used by a WhereLoop object
152578*/
152579static void whereLoopClear(sqlite3 *db, WhereLoop *p){
152580 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p: p->aLTerm);
152581 whereLoopClearUnion(db, p);
152582 whereLoopInit(p);
152583}
152584
152585/*
152586** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
152587*/
152588static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){
152589 WhereTerm **paNew;
152590 if( p->nLSlot>=n ) return SQLITE_OK;
152591 n = (n+7)&~7;
152592 paNew = sqlite3DbMallocRawNN(db, n: sizeof(p->aLTerm[0])*n);
152593 if( paNew==0 ) return SQLITE_NOMEM_BKPT;
152594 memcpy(dest: paNew, src: p->aLTerm, n: sizeof(p->aLTerm[0])*p->nLSlot);
152595 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p: p->aLTerm);
152596 p->aLTerm = paNew;
152597 p->nLSlot = n;
152598 return SQLITE_OK;
152599}
152600
152601/*
152602** Transfer content from the second pLoop into the first.
152603*/
152604static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){
152605 whereLoopClearUnion(db, p: pTo);
152606 if( whereLoopResize(db, p: pTo, n: pFrom->nLTerm) ){
152607 memset(s: pTo, c: 0, WHERE_LOOP_XFER_SZ);
152608 return SQLITE_NOMEM_BKPT;
152609 }
152610 memcpy(dest: pTo, src: pFrom, WHERE_LOOP_XFER_SZ);
152611 memcpy(dest: pTo->aLTerm, src: pFrom->aLTerm, n: pTo->nLTerm*sizeof(pTo->aLTerm[0]));
152612 if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){
152613 pFrom->u.vtab.needFree = 0;
152614 }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){
152615 pFrom->u.btree.pIndex = 0;
152616 }
152617 return SQLITE_OK;
152618}
152619
152620/*
152621** Delete a WhereLoop object
152622*/
152623static void whereLoopDelete(sqlite3 *db, WhereLoop *p){
152624 whereLoopClear(db, p);
152625 sqlite3DbFreeNN(db, p);
152626}
152627
152628/*
152629** Free a WhereInfo structure
152630*/
152631static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
152632 int i;
152633 assert( pWInfo!=0 );
152634 for(i=0; i<pWInfo->nLevel; i++){
152635 WhereLevel *pLevel = &pWInfo->a[i];
152636 if( pLevel->pWLoop && (pLevel->pWLoop->wsFlags & WHERE_IN_ABLE)!=0 ){
152637 assert( (pLevel->pWLoop->wsFlags & WHERE_MULTI_OR)==0 );
152638 sqlite3DbFree(db, p: pLevel->u.in.aInLoop);
152639 }
152640 }
152641 sqlite3WhereClauseClear(pWC: &pWInfo->sWC);
152642 while( pWInfo->pLoops ){
152643 WhereLoop *p = pWInfo->pLoops;
152644 pWInfo->pLoops = p->pNextLoop;
152645 whereLoopDelete(db, p);
152646 }
152647 assert( pWInfo->pExprMods==0 );
152648 sqlite3DbFreeNN(db, p: pWInfo);
152649}
152650
152651/* Undo all Expr node modifications
152652*/
152653static void whereUndoExprMods(WhereInfo *pWInfo){
152654 while( pWInfo->pExprMods ){
152655 WhereExprMod *p = pWInfo->pExprMods;
152656 pWInfo->pExprMods = p->pNext;
152657 memcpy(dest: p->pExpr, src: &p->orig, n: sizeof(p->orig));
152658 sqlite3DbFree(db: pWInfo->pParse->db, p);
152659 }
152660}
152661
152662/*
152663** Return TRUE if all of the following are true:
152664**
152665** (1) X has the same or lower cost, or returns the same or fewer rows,
152666** than Y.
152667** (2) X uses fewer WHERE clause terms than Y
152668** (3) Every WHERE clause term used by X is also used by Y
152669** (4) X skips at least as many columns as Y
152670** (5) If X is a covering index, than Y is too
152671**
152672** Conditions (2) and (3) mean that X is a "proper subset" of Y.
152673** If X is a proper subset of Y then Y is a better choice and ought
152674** to have a lower cost. This routine returns TRUE when that cost
152675** relationship is inverted and needs to be adjusted. Constraint (4)
152676** was added because if X uses skip-scan less than Y it still might
152677** deserve a lower cost even if it is a proper subset of Y. Constraint (5)
152678** was added because a covering index probably deserves to have a lower cost
152679** than a non-covering index even if it is a proper subset.
152680*/
152681static int whereLoopCheaperProperSubset(
152682 const WhereLoop *pX, /* First WhereLoop to compare */
152683 const WhereLoop *pY /* Compare against this WhereLoop */
152684){
152685 int i, j;
152686 if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){
152687 return 0; /* X is not a subset of Y */
152688 }
152689 if( pX->rRun>pY->rRun && pX->nOut>pY->nOut ) return 0;
152690 if( pY->nSkip > pX->nSkip ) return 0;
152691 for(i=pX->nLTerm-1; i>=0; i--){
152692 if( pX->aLTerm[i]==0 ) continue;
152693 for(j=pY->nLTerm-1; j>=0; j--){
152694 if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
152695 }
152696 if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */
152697 }
152698 if( (pX->wsFlags&WHERE_IDX_ONLY)!=0
152699 && (pY->wsFlags&WHERE_IDX_ONLY)==0 ){
152700 return 0; /* Constraint (5) */
152701 }
152702 return 1; /* All conditions meet */
152703}
152704
152705/*
152706** Try to adjust the cost and number of output rows of WhereLoop pTemplate
152707** upwards or downwards so that:
152708**
152709** (1) pTemplate costs less than any other WhereLoops that are a proper
152710** subset of pTemplate
152711**
152712** (2) pTemplate costs more than any other WhereLoops for which pTemplate
152713** is a proper subset.
152714**
152715** To say "WhereLoop X is a proper subset of Y" means that X uses fewer
152716** WHERE clause terms than Y and that every WHERE clause term used by X is
152717** also used by Y.
152718*/
152719static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){
152720 if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return;
152721 for(; p; p=p->pNextLoop){
152722 if( p->iTab!=pTemplate->iTab ) continue;
152723 if( (p->wsFlags & WHERE_INDEXED)==0 ) continue;
152724 if( whereLoopCheaperProperSubset(pX: p, pY: pTemplate) ){
152725 /* Adjust pTemplate cost downward so that it is cheaper than its
152726 ** subset p. */
152727 WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n",
152728 pTemplate->rRun, pTemplate->nOut,
152729 MIN(p->rRun, pTemplate->rRun),
152730 MIN(p->nOut - 1, pTemplate->nOut)));
152731 pTemplate->rRun = MIN(p->rRun, pTemplate->rRun);
152732 pTemplate->nOut = MIN(p->nOut - 1, pTemplate->nOut);
152733 }else if( whereLoopCheaperProperSubset(pX: pTemplate, pY: p) ){
152734 /* Adjust pTemplate cost upward so that it is costlier than p since
152735 ** pTemplate is a proper subset of p */
152736 WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n",
152737 pTemplate->rRun, pTemplate->nOut,
152738 MAX(p->rRun, pTemplate->rRun),
152739 MAX(p->nOut + 1, pTemplate->nOut)));
152740 pTemplate->rRun = MAX(p->rRun, pTemplate->rRun);
152741 pTemplate->nOut = MAX(p->nOut + 1, pTemplate->nOut);
152742 }
152743 }
152744}
152745
152746/*
152747** Search the list of WhereLoops in *ppPrev looking for one that can be
152748** replaced by pTemplate.
152749**
152750** Return NULL if pTemplate does not belong on the WhereLoop list.
152751** In other words if pTemplate ought to be dropped from further consideration.
152752**
152753** If pX is a WhereLoop that pTemplate can replace, then return the
152754** link that points to pX.
152755**
152756** If pTemplate cannot replace any existing element of the list but needs
152757** to be added to the list as a new entry, then return a pointer to the
152758** tail of the list.
152759*/
152760static WhereLoop **whereLoopFindLesser(
152761 WhereLoop **ppPrev,
152762 const WhereLoop *pTemplate
152763){
152764 WhereLoop *p;
152765 for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){
152766 if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
152767 /* If either the iTab or iSortIdx values for two WhereLoop are different
152768 ** then those WhereLoops need to be considered separately. Neither is
152769 ** a candidate to replace the other. */
152770 continue;
152771 }
152772 /* In the current implementation, the rSetup value is either zero
152773 ** or the cost of building an automatic index (NlogN) and the NlogN
152774 ** is the same for compatible WhereLoops. */
152775 assert( p->rSetup==0 || pTemplate->rSetup==0
152776 || p->rSetup==pTemplate->rSetup );
152777
152778 /* whereLoopAddBtree() always generates and inserts the automatic index
152779 ** case first. Hence compatible candidate WhereLoops never have a larger
152780 ** rSetup. Call this SETUP-INVARIANT */
152781 assert( p->rSetup>=pTemplate->rSetup );
152782
152783 /* Any loop using an appliation-defined index (or PRIMARY KEY or
152784 ** UNIQUE constraint) with one or more == constraints is better
152785 ** than an automatic index. Unless it is a skip-scan. */
152786 if( (p->wsFlags & WHERE_AUTO_INDEX)!=0
152787 && (pTemplate->nSkip)==0
152788 && (pTemplate->wsFlags & WHERE_INDEXED)!=0
152789 && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0
152790 && (p->prereq & pTemplate->prereq)==pTemplate->prereq
152791 ){
152792 break;
152793 }
152794
152795 /* If existing WhereLoop p is better than pTemplate, pTemplate can be
152796 ** discarded. WhereLoop p is better if:
152797 ** (1) p has no more dependencies than pTemplate, and
152798 ** (2) p has an equal or lower cost than pTemplate
152799 */
152800 if( (p->prereq & pTemplate->prereq)==p->prereq /* (1) */
152801 && p->rSetup<=pTemplate->rSetup /* (2a) */
152802 && p->rRun<=pTemplate->rRun /* (2b) */
152803 && p->nOut<=pTemplate->nOut /* (2c) */
152804 ){
152805 return 0; /* Discard pTemplate */
152806 }
152807
152808 /* If pTemplate is always better than p, then cause p to be overwritten
152809 ** with pTemplate. pTemplate is better than p if:
152810 ** (1) pTemplate has no more dependences than p, and
152811 ** (2) pTemplate has an equal or lower cost than p.
152812 */
152813 if( (p->prereq & pTemplate->prereq)==pTemplate->prereq /* (1) */
152814 && p->rRun>=pTemplate->rRun /* (2a) */
152815 && p->nOut>=pTemplate->nOut /* (2b) */
152816 ){
152817 assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */
152818 break; /* Cause p to be overwritten by pTemplate */
152819 }
152820 }
152821 return ppPrev;
152822}
152823
152824/*
152825** Insert or replace a WhereLoop entry using the template supplied.
152826**
152827** An existing WhereLoop entry might be overwritten if the new template
152828** is better and has fewer dependencies. Or the template will be ignored
152829** and no insert will occur if an existing WhereLoop is faster and has
152830** fewer dependencies than the template. Otherwise a new WhereLoop is
152831** added based on the template.
152832**
152833** If pBuilder->pOrSet is not NULL then we care about only the
152834** prerequisites and rRun and nOut costs of the N best loops. That
152835** information is gathered in the pBuilder->pOrSet object. This special
152836** processing mode is used only for OR clause processing.
152837**
152838** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we
152839** still might overwrite similar loops with the new template if the
152840** new template is better. Loops may be overwritten if the following
152841** conditions are met:
152842**
152843** (1) They have the same iTab.
152844** (2) They have the same iSortIdx.
152845** (3) The template has same or fewer dependencies than the current loop
152846** (4) The template has the same or lower cost than the current loop
152847*/
152848static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){
152849 WhereLoop **ppPrev, *p;
152850 WhereInfo *pWInfo = pBuilder->pWInfo;
152851 sqlite3 *db = pWInfo->pParse->db;
152852 int rc;
152853
152854 /* Stop the search once we hit the query planner search limit */
152855 if( pBuilder->iPlanLimit==0 ){
152856 WHERETRACE(0xffffffff,("=== query planner search limit reached ===\n"));
152857 if( pBuilder->pOrSet ) pBuilder->pOrSet->n = 0;
152858 return SQLITE_DONE;
152859 }
152860 pBuilder->iPlanLimit--;
152861
152862 whereLoopAdjustCost(p: pWInfo->pLoops, pTemplate);
152863
152864 /* If pBuilder->pOrSet is defined, then only keep track of the costs
152865 ** and prereqs.
152866 */
152867 if( pBuilder->pOrSet!=0 ){
152868 if( pTemplate->nLTerm ){
152869#if WHERETRACE_ENABLED
152870 u16 n = pBuilder->pOrSet->n;
152871 int x =
152872#endif
152873 whereOrInsert(pSet: pBuilder->pOrSet, prereq: pTemplate->prereq, rRun: pTemplate->rRun,
152874 nOut: pTemplate->nOut);
152875#if WHERETRACE_ENABLED /* 0x8 */
152876 if( sqlite3WhereTrace & 0x8 ){
152877 sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n);
152878 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
152879 }
152880#endif
152881 }
152882 return SQLITE_OK;
152883 }
152884
152885 /* Look for an existing WhereLoop to replace with pTemplate
152886 */
152887 ppPrev = whereLoopFindLesser(ppPrev: &pWInfo->pLoops, pTemplate);
152888
152889 if( ppPrev==0 ){
152890 /* There already exists a WhereLoop on the list that is better
152891 ** than pTemplate, so just ignore pTemplate */
152892#if WHERETRACE_ENABLED /* 0x8 */
152893 if( sqlite3WhereTrace & 0x8 ){
152894 sqlite3DebugPrintf(" skip: ");
152895 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
152896 }
152897#endif
152898 return SQLITE_OK;
152899 }else{
152900 p = *ppPrev;
152901 }
152902
152903 /* If we reach this point it means that either p[] should be overwritten
152904 ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new
152905 ** WhereLoop and insert it.
152906 */
152907#if WHERETRACE_ENABLED /* 0x8 */
152908 if( sqlite3WhereTrace & 0x8 ){
152909 if( p!=0 ){
152910 sqlite3DebugPrintf("replace: ");
152911 sqlite3WhereLoopPrint(p, pBuilder->pWC);
152912 sqlite3DebugPrintf(" with: ");
152913 }else{
152914 sqlite3DebugPrintf(" add: ");
152915 }
152916 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
152917 }
152918#endif
152919 if( p==0 ){
152920 /* Allocate a new WhereLoop to add to the end of the list */
152921 *ppPrev = p = sqlite3DbMallocRawNN(db, n: sizeof(WhereLoop));
152922 if( p==0 ) return SQLITE_NOMEM_BKPT;
152923 whereLoopInit(p);
152924 p->pNextLoop = 0;
152925 }else{
152926 /* We will be overwriting WhereLoop p[]. But before we do, first
152927 ** go through the rest of the list and delete any other entries besides
152928 ** p[] that are also supplated by pTemplate */
152929 WhereLoop **ppTail = &p->pNextLoop;
152930 WhereLoop *pToDel;
152931 while( *ppTail ){
152932 ppTail = whereLoopFindLesser(ppPrev: ppTail, pTemplate);
152933 if( ppTail==0 ) break;
152934 pToDel = *ppTail;
152935 if( pToDel==0 ) break;
152936 *ppTail = pToDel->pNextLoop;
152937#if WHERETRACE_ENABLED /* 0x8 */
152938 if( sqlite3WhereTrace & 0x8 ){
152939 sqlite3DebugPrintf(" delete: ");
152940 sqlite3WhereLoopPrint(pToDel, pBuilder->pWC);
152941 }
152942#endif
152943 whereLoopDelete(db, p: pToDel);
152944 }
152945 }
152946 rc = whereLoopXfer(db, pTo: p, pFrom: pTemplate);
152947 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
152948 Index *pIndex = p->u.btree.pIndex;
152949 if( pIndex && pIndex->idxType==SQLITE_IDXTYPE_IPK ){
152950 p->u.btree.pIndex = 0;
152951 }
152952 }
152953 return rc;
152954}
152955
152956/*
152957** Adjust the WhereLoop.nOut value downward to account for terms of the
152958** WHERE clause that reference the loop but which are not used by an
152959** index.
152960*
152961** For every WHERE clause term that is not used by the index
152962** and which has a truth probability assigned by one of the likelihood(),
152963** likely(), or unlikely() SQL functions, reduce the estimated number
152964** of output rows by the probability specified.
152965**
152966** TUNING: For every WHERE clause term that is not used by the index
152967** and which does not have an assigned truth probability, heuristics
152968** described below are used to try to estimate the truth probability.
152969** TODO --> Perhaps this is something that could be improved by better
152970** table statistics.
152971**
152972** Heuristic 1: Estimate the truth probability as 93.75%. The 93.75%
152973** value corresponds to -1 in LogEst notation, so this means decrement
152974** the WhereLoop.nOut field for every such WHERE clause term.
152975**
152976** Heuristic 2: If there exists one or more WHERE clause terms of the
152977** form "x==EXPR" and EXPR is not a constant 0 or 1, then make sure the
152978** final output row estimate is no greater than 1/4 of the total number
152979** of rows in the table. In other words, assume that x==EXPR will filter
152980** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the
152981** "x" column is boolean or else -1 or 0 or 1 is a common default value
152982** on the "x" column and so in that case only cap the output row estimate
152983** at 1/2 instead of 1/4.
152984*/
152985static void whereLoopOutputAdjust(
152986 WhereClause *pWC, /* The WHERE clause */
152987 WhereLoop *pLoop, /* The loop to adjust downward */
152988 LogEst nRow /* Number of rows in the entire table */
152989){
152990 WhereTerm *pTerm, *pX;
152991 Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
152992 int i, j;
152993 LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */
152994
152995 assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
152996 for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){
152997 assert( pTerm!=0 );
152998 if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) break;
152999 if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue;
153000 if( (pTerm->prereqAll & notAllowed)!=0 ) continue;
153001 for(j=pLoop->nLTerm-1; j>=0; j--){
153002 pX = pLoop->aLTerm[j];
153003 if( pX==0 ) continue;
153004 if( pX==pTerm ) break;
153005 if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
153006 }
153007 if( j<0 ){
153008 if( pTerm->truthProb<=0 ){
153009 /* If a truth probability is specified using the likelihood() hints,
153010 ** then use the probability provided by the application. */
153011 pLoop->nOut += pTerm->truthProb;
153012 }else{
153013 /* In the absence of explicit truth probabilities, use heuristics to
153014 ** guess a reasonable truth probability. */
153015 pLoop->nOut--;
153016 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0
153017 && (pTerm->wtFlags & TERM_HIGHTRUTH)==0 /* tag-20200224-1 */
153018 ){
153019 Expr *pRight = pTerm->pExpr->pRight;
153020 int k = 0;
153021 testcase( pTerm->pExpr->op==TK_IS );
153022 if( sqlite3ExprIsInteger(p: pRight, pValue: &k) && k>=(-1) && k<=1 ){
153023 k = 10;
153024 }else{
153025 k = 20;
153026 }
153027 if( iReduce<k ){
153028 pTerm->wtFlags |= TERM_HEURTRUTH;
153029 iReduce = k;
153030 }
153031 }
153032 }
153033 }
153034 }
153035 if( pLoop->nOut > nRow-iReduce ) pLoop->nOut = nRow - iReduce;
153036}
153037
153038/*
153039** Term pTerm is a vector range comparison operation. The first comparison
153040** in the vector can be optimized using column nEq of the index. This
153041** function returns the total number of vector elements that can be used
153042** as part of the range comparison.
153043**
153044** For example, if the query is:
153045**
153046** WHERE a = ? AND (b, c, d) > (?, ?, ?)
153047**
153048** and the index:
153049**
153050** CREATE INDEX ... ON (a, b, c, d, e)
153051**
153052** then this function would be invoked with nEq=1. The value returned in
153053** this case is 3.
153054*/
153055static int whereRangeVectorLen(
153056 Parse *pParse, /* Parsing context */
153057 int iCur, /* Cursor open on pIdx */
153058 Index *pIdx, /* The index to be used for a inequality constraint */
153059 int nEq, /* Number of prior equality constraints on same index */
153060 WhereTerm *pTerm /* The vector inequality constraint */
153061){
153062 int nCmp = sqlite3ExprVectorSize(pExpr: pTerm->pExpr->pLeft);
153063 int i;
153064
153065 nCmp = MIN(nCmp, (pIdx->nColumn - nEq));
153066 for(i=1; i<nCmp; i++){
153067 /* Test if comparison i of pTerm is compatible with column (i+nEq)
153068 ** of the index. If not, exit the loop. */
153069 char aff; /* Comparison affinity */
153070 char idxaff = 0; /* Indexed columns affinity */
153071 CollSeq *pColl; /* Comparison collation sequence */
153072 Expr *pLhs, *pRhs;
153073
153074 assert( ExprUseXList(pTerm->pExpr->pLeft) );
153075 pLhs = pTerm->pExpr->pLeft->x.pList->a[i].pExpr;
153076 pRhs = pTerm->pExpr->pRight;
153077 if( ExprUseXSelect(pRhs) ){
153078 pRhs = pRhs->x.pSelect->pEList->a[i].pExpr;
153079 }else{
153080 pRhs = pRhs->x.pList->a[i].pExpr;
153081 }
153082
153083 /* Check that the LHS of the comparison is a column reference to
153084 ** the right column of the right source table. And that the sort
153085 ** order of the index column is the same as the sort order of the
153086 ** leftmost index column. */
153087 if( pLhs->op!=TK_COLUMN
153088 || pLhs->iTable!=iCur
153089 || pLhs->iColumn!=pIdx->aiColumn[i+nEq]
153090 || pIdx->aSortOrder[i+nEq]!=pIdx->aSortOrder[nEq]
153091 ){
153092 break;
153093 }
153094
153095 testcase( pLhs->iColumn==XN_ROWID );
153096 aff = sqlite3CompareAffinity(pExpr: pRhs, aff2: sqlite3ExprAffinity(pExpr: pLhs));
153097 idxaff = sqlite3TableColumnAffinity(pTab: pIdx->pTable, iCol: pLhs->iColumn);
153098 if( aff!=idxaff ) break;
153099
153100 pColl = sqlite3BinaryCompareCollSeq(pParse, pLeft: pLhs, pRight: pRhs);
153101 if( pColl==0 ) break;
153102 if( sqlite3StrICmp(zLeft: pColl->zName, zRight: pIdx->azColl[i+nEq]) ) break;
153103 }
153104 return i;
153105}
153106
153107/*
153108** Adjust the cost C by the costMult facter T. This only occurs if
153109** compiled with -DSQLITE_ENABLE_COSTMULT
153110*/
153111#ifdef SQLITE_ENABLE_COSTMULT
153112# define ApplyCostMultiplier(C,T) C += T
153113#else
153114# define ApplyCostMultiplier(C,T)
153115#endif
153116
153117/*
153118** We have so far matched pBuilder->pNew->u.btree.nEq terms of the
153119** index pIndex. Try to match one more.
153120**
153121** When this function is called, pBuilder->pNew->nOut contains the
153122** number of rows expected to be visited by filtering using the nEq
153123** terms only. If it is modified, this value is restored before this
153124** function returns.
153125**
153126** If pProbe->idxType==SQLITE_IDXTYPE_IPK, that means pIndex is
153127** a fake index used for the INTEGER PRIMARY KEY.
153128*/
153129static int whereLoopAddBtreeIndex(
153130 WhereLoopBuilder *pBuilder, /* The WhereLoop factory */
153131 SrcItem *pSrc, /* FROM clause term being analyzed */
153132 Index *pProbe, /* An index on pSrc */
153133 LogEst nInMul /* log(Number of iterations due to IN) */
153134){
153135 WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */
153136 Parse *pParse = pWInfo->pParse; /* Parsing context */
153137 sqlite3 *db = pParse->db; /* Database connection malloc context */
153138 WhereLoop *pNew; /* Template WhereLoop under construction */
153139 WhereTerm *pTerm; /* A WhereTerm under consideration */
153140 int opMask; /* Valid operators for constraints */
153141 WhereScan scan; /* Iterator for WHERE terms */
153142 Bitmask saved_prereq; /* Original value of pNew->prereq */
153143 u16 saved_nLTerm; /* Original value of pNew->nLTerm */
153144 u16 saved_nEq; /* Original value of pNew->u.btree.nEq */
153145 u16 saved_nBtm; /* Original value of pNew->u.btree.nBtm */
153146 u16 saved_nTop; /* Original value of pNew->u.btree.nTop */
153147 u16 saved_nSkip; /* Original value of pNew->nSkip */
153148 u32 saved_wsFlags; /* Original value of pNew->wsFlags */
153149 LogEst saved_nOut; /* Original value of pNew->nOut */
153150 int rc = SQLITE_OK; /* Return code */
153151 LogEst rSize; /* Number of rows in the table */
153152 LogEst rLogSize; /* Logarithm of table size */
153153 WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */
153154
153155 pNew = pBuilder->pNew;
153156 if( db->mallocFailed ) return SQLITE_NOMEM_BKPT;
153157 WHERETRACE(0x800, ("BEGIN %s.addBtreeIdx(%s), nEq=%d, nSkip=%d, rRun=%d\n",
153158 pProbe->pTable->zName,pProbe->zName,
153159 pNew->u.btree.nEq, pNew->nSkip, pNew->rRun));
153160
153161 assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
153162 assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 );
153163 if( pNew->wsFlags & WHERE_BTM_LIMIT ){
153164 opMask = WO_LT|WO_LE;
153165 }else{
153166 assert( pNew->u.btree.nBtm==0 );
153167 opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE|WO_ISNULL|WO_IS;
153168 }
153169 if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
153170
153171 assert( pNew->u.btree.nEq<pProbe->nColumn );
153172 assert( pNew->u.btree.nEq<pProbe->nKeyCol
153173 || pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY );
153174
153175 saved_nEq = pNew->u.btree.nEq;
153176 saved_nBtm = pNew->u.btree.nBtm;
153177 saved_nTop = pNew->u.btree.nTop;
153178 saved_nSkip = pNew->nSkip;
153179 saved_nLTerm = pNew->nLTerm;
153180 saved_wsFlags = pNew->wsFlags;
153181 saved_prereq = pNew->prereq;
153182 saved_nOut = pNew->nOut;
153183 pTerm = whereScanInit(pScan: &scan, pWC: pBuilder->pWC, iCur: pSrc->iCursor, iColumn: saved_nEq,
153184 opMask, pIdx: pProbe);
153185 pNew->rSetup = 0;
153186 rSize = pProbe->aiRowLogEst[0];
153187 rLogSize = estLog(N: rSize);
153188 for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(pScan: &scan)){
153189 u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */
153190 LogEst rCostIdx;
153191 LogEst nOutUnadjusted; /* nOut before IN() and WHERE adjustments */
153192 int nIn = 0;
153193#ifdef SQLITE_ENABLE_STAT4
153194 int nRecValid = pBuilder->nRecValid;
153195#endif
153196 if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
153197 && indexColumnNotNull(pIdx: pProbe, iCol: saved_nEq)
153198 ){
153199 continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */
153200 }
153201 if( pTerm->prereqRight & pNew->maskSelf ) continue;
153202
153203 /* Do not allow the upper bound of a LIKE optimization range constraint
153204 ** to mix with a lower range bound from some other source */
153205 if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue;
153206
153207 /* tag-20191211-001: Do not allow constraints from the WHERE clause to
153208 ** be used by the right table of a LEFT JOIN. Only constraints in the
153209 ** ON clause are allowed. See tag-20191211-002 for the vtab equivalent. */
153210 if( (pSrc->fg.jointype & JT_LEFT)!=0
153211 && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
153212 ){
153213 continue;
153214 }
153215
153216 if( IsUniqueIndex(pProbe) && saved_nEq==pProbe->nKeyCol-1 ){
153217 pBuilder->bldFlags1 |= SQLITE_BLDF1_UNIQUE;
153218 }else{
153219 pBuilder->bldFlags1 |= SQLITE_BLDF1_INDEXED;
153220 }
153221 pNew->wsFlags = saved_wsFlags;
153222 pNew->u.btree.nEq = saved_nEq;
153223 pNew->u.btree.nBtm = saved_nBtm;
153224 pNew->u.btree.nTop = saved_nTop;
153225 pNew->nLTerm = saved_nLTerm;
153226 if( whereLoopResize(db, p: pNew, n: pNew->nLTerm+1) ) break; /* OOM */
153227 pNew->aLTerm[pNew->nLTerm++] = pTerm;
153228 pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
153229
153230 assert( nInMul==0
153231 || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0
153232 || (pNew->wsFlags & WHERE_COLUMN_IN)!=0
153233 || (pNew->wsFlags & WHERE_SKIPSCAN)!=0
153234 );
153235
153236 if( eOp & WO_IN ){
153237 Expr *pExpr = pTerm->pExpr;
153238 if( ExprUseXSelect(pExpr) ){
153239 /* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */
153240 int i;
153241 nIn = 46; assert( 46==sqlite3LogEst(25) );
153242
153243 /* The expression may actually be of the form (x, y) IN (SELECT...).
153244 ** In this case there is a separate term for each of (x) and (y).
153245 ** However, the nIn multiplier should only be applied once, not once
153246 ** for each such term. The following loop checks that pTerm is the
153247 ** first such term in use, and sets nIn back to 0 if it is not. */
153248 for(i=0; i<pNew->nLTerm-1; i++){
153249 if( pNew->aLTerm[i] && pNew->aLTerm[i]->pExpr==pExpr ) nIn = 0;
153250 }
153251 }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
153252 /* "x IN (value, value, ...)" */
153253 nIn = sqlite3LogEst(x: pExpr->x.pList->nExpr);
153254 }
153255 if( pProbe->hasStat1 && rLogSize>=10 ){
153256 LogEst M, logK, x;
153257 /* Let:
153258 ** N = the total number of rows in the table
153259 ** K = the number of entries on the RHS of the IN operator
153260 ** M = the number of rows in the table that match terms to the
153261 ** to the left in the same index. If the IN operator is on
153262 ** the left-most index column, M==N.
153263 **
153264 ** Given the definitions above, it is better to omit the IN operator
153265 ** from the index lookup and instead do a scan of the M elements,
153266 ** testing each scanned row against the IN operator separately, if:
153267 **
153268 ** M*log(K) < K*log(N)
153269 **
153270 ** Our estimates for M, K, and N might be inaccurate, so we build in
153271 ** a safety margin of 2 (LogEst: 10) that favors using the IN operator
153272 ** with the index, as using an index has better worst-case behavior.
153273 ** If we do not have real sqlite_stat1 data, always prefer to use
153274 ** the index. Do not bother with this optimization on very small
153275 ** tables (less than 2 rows) as it is pointless in that case.
153276 */
153277 M = pProbe->aiRowLogEst[saved_nEq];
153278 logK = estLog(N: nIn);
153279 /* TUNING v----- 10 to bias toward indexed IN */
153280 x = M + logK + 10 - (nIn + rLogSize);
153281 if( x>=0 ){
153282 WHERETRACE(0x40,
153283 ("IN operator (N=%d M=%d logK=%d nIn=%d rLogSize=%d x=%d) "
153284 "prefers indexed lookup\n",
153285 saved_nEq, M, logK, nIn, rLogSize, x));
153286 }else if( nInMul<2 && OptimizationEnabled(db, SQLITE_SeekScan) ){
153287 WHERETRACE(0x40,
153288 ("IN operator (N=%d M=%d logK=%d nIn=%d rLogSize=%d x=%d"
153289 " nInMul=%d) prefers skip-scan\n",
153290 saved_nEq, M, logK, nIn, rLogSize, x, nInMul));
153291 pNew->wsFlags |= WHERE_IN_SEEKSCAN;
153292 }else{
153293 WHERETRACE(0x40,
153294 ("IN operator (N=%d M=%d logK=%d nIn=%d rLogSize=%d x=%d"
153295 " nInMul=%d) prefers normal scan\n",
153296 saved_nEq, M, logK, nIn, rLogSize, x, nInMul));
153297 continue;
153298 }
153299 }
153300 pNew->wsFlags |= WHERE_COLUMN_IN;
153301 }else if( eOp & (WO_EQ|WO_IS) ){
153302 int iCol = pProbe->aiColumn[saved_nEq];
153303 pNew->wsFlags |= WHERE_COLUMN_EQ;
153304 assert( saved_nEq==pNew->u.btree.nEq );
153305 if( iCol==XN_ROWID
153306 || (iCol>=0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
153307 ){
153308 if( iCol==XN_ROWID || pProbe->uniqNotNull
153309 || (pProbe->nKeyCol==1 && pProbe->onError && eOp==WO_EQ)
153310 ){
153311 pNew->wsFlags |= WHERE_ONEROW;
153312 }else{
153313 pNew->wsFlags |= WHERE_UNQ_WANTED;
153314 }
153315 }
153316 if( scan.iEquiv>1 ) pNew->wsFlags |= WHERE_TRANSCONS;
153317 }else if( eOp & WO_ISNULL ){
153318 pNew->wsFlags |= WHERE_COLUMN_NULL;
153319 }else if( eOp & (WO_GT|WO_GE) ){
153320 testcase( eOp & WO_GT );
153321 testcase( eOp & WO_GE );
153322 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
153323 pNew->u.btree.nBtm = whereRangeVectorLen(
153324 pParse, iCur: pSrc->iCursor, pIdx: pProbe, nEq: saved_nEq, pTerm
153325 );
153326 pBtm = pTerm;
153327 pTop = 0;
153328 if( pTerm->wtFlags & TERM_LIKEOPT ){
153329 /* Range constraints that come from the LIKE optimization are
153330 ** always used in pairs. */
153331 pTop = &pTerm[1];
153332 assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm );
153333 assert( pTop->wtFlags & TERM_LIKEOPT );
153334 assert( pTop->eOperator==WO_LT );
153335 if( whereLoopResize(db, p: pNew, n: pNew->nLTerm+1) ) break; /* OOM */
153336 pNew->aLTerm[pNew->nLTerm++] = pTop;
153337 pNew->wsFlags |= WHERE_TOP_LIMIT;
153338 pNew->u.btree.nTop = 1;
153339 }
153340 }else{
153341 assert( eOp & (WO_LT|WO_LE) );
153342 testcase( eOp & WO_LT );
153343 testcase( eOp & WO_LE );
153344 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
153345 pNew->u.btree.nTop = whereRangeVectorLen(
153346 pParse, iCur: pSrc->iCursor, pIdx: pProbe, nEq: saved_nEq, pTerm
153347 );
153348 pTop = pTerm;
153349 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
153350 pNew->aLTerm[pNew->nLTerm-2] : 0;
153351 }
153352
153353 /* At this point pNew->nOut is set to the number of rows expected to
153354 ** be visited by the index scan before considering term pTerm, or the
153355 ** values of nIn and nInMul. In other words, assuming that all
153356 ** "x IN(...)" terms are replaced with "x = ?". This block updates
153357 ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */
153358 assert( pNew->nOut==saved_nOut );
153359 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
153360 /* Adjust nOut using stat4 data. Or, if there is no stat4
153361 ** data, using some other estimate. */
153362 whereRangeScanEst(pParse, pBuilder, pLower: pBtm, pUpper: pTop, pLoop: pNew);
153363 }else{
153364 int nEq = ++pNew->u.btree.nEq;
153365 assert( eOp & (WO_ISNULL|WO_EQ|WO_IN|WO_IS) );
153366
153367 assert( pNew->nOut==saved_nOut );
153368 if( pTerm->truthProb<=0 && pProbe->aiColumn[saved_nEq]>=0 ){
153369 assert( (eOp & WO_IN) || nIn==0 );
153370 testcase( eOp & WO_IN );
153371 pNew->nOut += pTerm->truthProb;
153372 pNew->nOut -= nIn;
153373 }else{
153374#ifdef SQLITE_ENABLE_STAT4
153375 tRowcnt nOut = 0;
153376 if( nInMul==0
153377 && pProbe->nSample
153378 && ALWAYS(pNew->u.btree.nEq<=pProbe->nSampleCol)
153379 && ((eOp & WO_IN)==0 || ExprUseXList(pTerm->pExpr))
153380 && OptimizationEnabled(db, SQLITE_Stat4)
153381 ){
153382 Expr *pExpr = pTerm->pExpr;
153383 if( (eOp & (WO_EQ|WO_ISNULL|WO_IS))!=0 ){
153384 testcase( eOp & WO_EQ );
153385 testcase( eOp & WO_IS );
153386 testcase( eOp & WO_ISNULL );
153387 rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
153388 }else{
153389 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
153390 }
153391 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
153392 if( rc!=SQLITE_OK ) break; /* Jump out of the pTerm loop */
153393 if( nOut ){
153394 pNew->nOut = sqlite3LogEst(nOut);
153395 if( nEq==1
153396 /* TUNING: Mark terms as "low selectivity" if they seem likely
153397 ** to be true for half or more of the rows in the table.
153398 ** See tag-202002240-1 */
153399 && pNew->nOut+10 > pProbe->aiRowLogEst[0]
153400 ){
153401#if WHERETRACE_ENABLED /* 0x01 */
153402 if( sqlite3WhereTrace & 0x01 ){
153403 sqlite3DebugPrintf(
153404 "STAT4 determines term has low selectivity:\n");
153405 sqlite3WhereTermPrint(pTerm, 999);
153406 }
153407#endif
153408 pTerm->wtFlags |= TERM_HIGHTRUTH;
153409 if( pTerm->wtFlags & TERM_HEURTRUTH ){
153410 /* If the term has previously been used with an assumption of
153411 ** higher selectivity, then set the flag to rerun the
153412 ** loop computations. */
153413 pBuilder->bldFlags2 |= SQLITE_BLDF2_2NDPASS;
153414 }
153415 }
153416 if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
153417 pNew->nOut -= nIn;
153418 }
153419 }
153420 if( nOut==0 )
153421#endif
153422 {
153423 pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]);
153424 if( eOp & WO_ISNULL ){
153425 /* TUNING: If there is no likelihood() value, assume that a
153426 ** "col IS NULL" expression matches twice as many rows
153427 ** as (col=?). */
153428 pNew->nOut += 10;
153429 }
153430 }
153431 }
153432 }
153433
153434 /* Set rCostIdx to the cost of visiting selected rows in index. Add
153435 ** it to pNew->rRun, which is currently set to the cost of the index
153436 ** seek only. Then, if this is a non-covering index, add the cost of
153437 ** visiting the rows in the main table. */
153438 assert( pSrc->pTab->szTabRow>0 );
153439 rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
153440 pNew->rRun = sqlite3LogEstAdd(a: rLogSize, b: rCostIdx);
153441 if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
153442 pNew->rRun = sqlite3LogEstAdd(a: pNew->rRun, b: pNew->nOut + 16);
153443 }
153444 ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult);
153445
153446 nOutUnadjusted = pNew->nOut;
153447 pNew->rRun += nInMul + nIn;
153448 pNew->nOut += nInMul + nIn;
153449 whereLoopOutputAdjust(pWC: pBuilder->pWC, pLoop: pNew, nRow: rSize);
153450 rc = whereLoopInsert(pBuilder, pTemplate: pNew);
153451
153452 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
153453 pNew->nOut = saved_nOut;
153454 }else{
153455 pNew->nOut = nOutUnadjusted;
153456 }
153457
153458 if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
153459 && pNew->u.btree.nEq<pProbe->nColumn
153460 && (pNew->u.btree.nEq<pProbe->nKeyCol ||
153461 pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY)
153462 ){
153463 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul: nInMul+nIn);
153464 }
153465 pNew->nOut = saved_nOut;
153466#ifdef SQLITE_ENABLE_STAT4
153467 pBuilder->nRecValid = nRecValid;
153468#endif
153469 }
153470 pNew->prereq = saved_prereq;
153471 pNew->u.btree.nEq = saved_nEq;
153472 pNew->u.btree.nBtm = saved_nBtm;
153473 pNew->u.btree.nTop = saved_nTop;
153474 pNew->nSkip = saved_nSkip;
153475 pNew->wsFlags = saved_wsFlags;
153476 pNew->nOut = saved_nOut;
153477 pNew->nLTerm = saved_nLTerm;
153478
153479 /* Consider using a skip-scan if there are no WHERE clause constraints
153480 ** available for the left-most terms of the index, and if the average
153481 ** number of repeats in the left-most terms is at least 18.
153482 **
153483 ** The magic number 18 is selected on the basis that scanning 17 rows
153484 ** is almost always quicker than an index seek (even though if the index
153485 ** contains fewer than 2^17 rows we assume otherwise in other parts of
153486 ** the code). And, even if it is not, it should not be too much slower.
153487 ** On the other hand, the extra seeks could end up being significantly
153488 ** more expensive. */
153489 assert( 42==sqlite3LogEst(18) );
153490 if( saved_nEq==saved_nSkip
153491 && saved_nEq+1<pProbe->nKeyCol
153492 && saved_nEq==pNew->nLTerm
153493 && pProbe->noSkipScan==0
153494 && pProbe->hasStat1!=0
153495 && OptimizationEnabled(db, SQLITE_SkipScan)
153496 && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */
153497 && (rc = whereLoopResize(db, p: pNew, n: pNew->nLTerm+1))==SQLITE_OK
153498 ){
153499 LogEst nIter;
153500 pNew->u.btree.nEq++;
153501 pNew->nSkip++;
153502 pNew->aLTerm[pNew->nLTerm++] = 0;
153503 pNew->wsFlags |= WHERE_SKIPSCAN;
153504 nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
153505 pNew->nOut -= nIter;
153506 /* TUNING: Because uncertainties in the estimates for skip-scan queries,
153507 ** add a 1.375 fudge factor to make skip-scan slightly less likely. */
153508 nIter += 5;
153509 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul: nIter + nInMul);
153510 pNew->nOut = saved_nOut;
153511 pNew->u.btree.nEq = saved_nEq;
153512 pNew->nSkip = saved_nSkip;
153513 pNew->wsFlags = saved_wsFlags;
153514 }
153515
153516 WHERETRACE(0x800, ("END %s.addBtreeIdx(%s), nEq=%d, rc=%d\n",
153517 pProbe->pTable->zName, pProbe->zName, saved_nEq, rc));
153518 return rc;
153519}
153520
153521/*
153522** Return True if it is possible that pIndex might be useful in
153523** implementing the ORDER BY clause in pBuilder.
153524**
153525** Return False if pBuilder does not contain an ORDER BY clause or
153526** if there is no way for pIndex to be useful in implementing that
153527** ORDER BY clause.
153528*/
153529static int indexMightHelpWithOrderBy(
153530 WhereLoopBuilder *pBuilder,
153531 Index *pIndex,
153532 int iCursor
153533){
153534 ExprList *pOB;
153535 ExprList *aColExpr;
153536 int ii, jj;
153537
153538 if( pIndex->bUnordered ) return 0;
153539 if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0;
153540 for(ii=0; ii<pOB->nExpr; ii++){
153541 Expr *pExpr = sqlite3ExprSkipCollateAndLikely(pExpr: pOB->a[ii].pExpr);
153542 if( NEVER(pExpr==0) ) continue;
153543 if( pExpr->op==TK_COLUMN && pExpr->iTable==iCursor ){
153544 if( pExpr->iColumn<0 ) return 1;
153545 for(jj=0; jj<pIndex->nKeyCol; jj++){
153546 if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1;
153547 }
153548 }else if( (aColExpr = pIndex->aColExpr)!=0 ){
153549 for(jj=0; jj<pIndex->nKeyCol; jj++){
153550 if( pIndex->aiColumn[jj]!=XN_EXPR ) continue;
153551 if( sqlite3ExprCompareSkip(pA: pExpr,pB: aColExpr->a[jj].pExpr,iTab: iCursor)==0 ){
153552 return 1;
153553 }
153554 }
153555 }
153556 }
153557 return 0;
153558}
153559
153560/* Check to see if a partial index with pPartIndexWhere can be used
153561** in the current query. Return true if it can be and false if not.
153562*/
153563static int whereUsablePartialIndex(
153564 int iTab, /* The table for which we want an index */
153565 int isLeft, /* True if iTab is the right table of a LEFT JOIN */
153566 WhereClause *pWC, /* The WHERE clause of the query */
153567 Expr *pWhere /* The WHERE clause from the partial index */
153568){
153569 int i;
153570 WhereTerm *pTerm;
153571 Parse *pParse = pWC->pWInfo->pParse;
153572 while( pWhere->op==TK_AND ){
153573 if( !whereUsablePartialIndex(iTab,isLeft,pWC,pWhere: pWhere->pLeft) ) return 0;
153574 pWhere = pWhere->pRight;
153575 }
153576 if( pParse->db->flags & SQLITE_EnableQPSG ) pParse = 0;
153577 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
153578 Expr *pExpr;
153579 pExpr = pTerm->pExpr;
153580 if( (!ExprHasProperty(pExpr, EP_FromJoin) || pExpr->iRightJoinTable==iTab)
153581 && (isLeft==0 || ExprHasProperty(pExpr, EP_FromJoin))
153582 && sqlite3ExprImpliesExpr(pParse, pE1: pExpr, pE2: pWhere, iTab)
153583 && (pTerm->wtFlags & TERM_VNULL)==0
153584 ){
153585 return 1;
153586 }
153587 }
153588 return 0;
153589}
153590
153591/*
153592** Add all WhereLoop objects for a single table of the join where the table
153593** is identified by pBuilder->pNew->iTab. That table is guaranteed to be
153594** a b-tree table, not a virtual table.
153595**
153596** The costs (WhereLoop.rRun) of the b-tree loops added by this function
153597** are calculated as follows:
153598**
153599** For a full scan, assuming the table (or index) contains nRow rows:
153600**
153601** cost = nRow * 3.0 // full-table scan
153602** cost = nRow * K // scan of covering index
153603** cost = nRow * (K+3.0) // scan of non-covering index
153604**
153605** where K is a value between 1.1 and 3.0 set based on the relative
153606** estimated average size of the index and table records.
153607**
153608** For an index scan, where nVisit is the number of index rows visited
153609** by the scan, and nSeek is the number of seek operations required on
153610** the index b-tree:
153611**
153612** cost = nSeek * (log(nRow) + K * nVisit) // covering index
153613** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index
153614**
153615** Normally, nSeek is 1. nSeek values greater than 1 come about if the
153616** WHERE clause includes "x IN (....)" terms used in place of "x=?". Or when
153617** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans.
153618**
153619** The estimated values (nRow, nVisit, nSeek) often contain a large amount
153620** of uncertainty. For this reason, scoring is designed to pick plans that
153621** "do the least harm" if the estimates are inaccurate. For example, a
153622** log(nRow) factor is omitted from a non-covering index scan in order to
153623** bias the scoring in favor of using an index, since the worst-case
153624** performance of using an index is far better than the worst-case performance
153625** of a full table scan.
153626*/
153627static int whereLoopAddBtree(
153628 WhereLoopBuilder *pBuilder, /* WHERE clause information */
153629 Bitmask mPrereq /* Extra prerequesites for using this table */
153630){
153631 WhereInfo *pWInfo; /* WHERE analysis context */
153632 Index *pProbe; /* An index we are evaluating */
153633 Index sPk; /* A fake index object for the primary key */
153634 LogEst aiRowEstPk[2]; /* The aiRowLogEst[] value for the sPk index */
153635 i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */
153636 SrcList *pTabList; /* The FROM clause */
153637 SrcItem *pSrc; /* The FROM clause btree term to add */
153638 WhereLoop *pNew; /* Template WhereLoop object */
153639 int rc = SQLITE_OK; /* Return code */
153640 int iSortIdx = 1; /* Index number */
153641 int b; /* A boolean value */
153642 LogEst rSize; /* number of rows in the table */
153643 WhereClause *pWC; /* The parsed WHERE clause */
153644 Table *pTab; /* Table being queried */
153645
153646 pNew = pBuilder->pNew;
153647 pWInfo = pBuilder->pWInfo;
153648 pTabList = pWInfo->pTabList;
153649 pSrc = pTabList->a + pNew->iTab;
153650 pTab = pSrc->pTab;
153651 pWC = pBuilder->pWC;
153652 assert( !IsVirtual(pSrc->pTab) );
153653
153654 if( pSrc->fg.isIndexedBy ){
153655 assert( pSrc->fg.isCte==0 );
153656 /* An INDEXED BY clause specifies a particular index to use */
153657 pProbe = pSrc->u2.pIBIndex;
153658 }else if( !HasRowid(pTab) ){
153659 pProbe = pTab->pIndex;
153660 }else{
153661 /* There is no INDEXED BY clause. Create a fake Index object in local
153662 ** variable sPk to represent the rowid primary key index. Make this
153663 ** fake index the first in a chain of Index objects with all of the real
153664 ** indices to follow */
153665 Index *pFirst; /* First of real indices on the table */
153666 memset(s: &sPk, c: 0, n: sizeof(Index));
153667 sPk.nKeyCol = 1;
153668 sPk.nColumn = 1;
153669 sPk.aiColumn = &aiColumnPk;
153670 sPk.aiRowLogEst = aiRowEstPk;
153671 sPk.onError = OE_Replace;
153672 sPk.pTable = pTab;
153673 sPk.szIdxRow = pTab->szTabRow;
153674 sPk.idxType = SQLITE_IDXTYPE_IPK;
153675 aiRowEstPk[0] = pTab->nRowLogEst;
153676 aiRowEstPk[1] = 0;
153677 pFirst = pSrc->pTab->pIndex;
153678 if( pSrc->fg.notIndexed==0 ){
153679 /* The real indices of the table are only considered if the
153680 ** NOT INDEXED qualifier is omitted from the FROM clause */
153681 sPk.pNext = pFirst;
153682 }
153683 pProbe = &sPk;
153684 }
153685 rSize = pTab->nRowLogEst;
153686
153687#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
153688 /* Automatic indexes */
153689 if( !pBuilder->pOrSet /* Not part of an OR optimization */
153690 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0
153691 && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
153692 && !pSrc->fg.isIndexedBy /* Has no INDEXED BY clause */
153693 && !pSrc->fg.notIndexed /* Has no NOT INDEXED clause */
153694 && HasRowid(pTab) /* Not WITHOUT ROWID table. (FIXME: Why not?) */
153695 && !pSrc->fg.isCorrelated /* Not a correlated subquery */
153696 && !pSrc->fg.isRecursive /* Not a recursive common table expression. */
153697 ){
153698 /* Generate auto-index WhereLoops */
153699 LogEst rLogSize; /* Logarithm of the number of rows in the table */
153700 WhereTerm *pTerm;
153701 WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
153702 rLogSize = estLog(N: rSize);
153703 for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
153704 if( pTerm->prereqRight & pNew->maskSelf ) continue;
153705 if( termCanDriveIndex(pTerm, pSrc, notReady: 0) ){
153706 pNew->u.btree.nEq = 1;
153707 pNew->nSkip = 0;
153708 pNew->u.btree.pIndex = 0;
153709 pNew->nLTerm = 1;
153710 pNew->aLTerm[0] = pTerm;
153711 /* TUNING: One-time cost for computing the automatic index is
153712 ** estimated to be X*N*log2(N) where N is the number of rows in
153713 ** the table being indexed and where X is 7 (LogEst=28) for normal
153714 ** tables or 0.5 (LogEst=-10) for views and subqueries. The value
153715 ** of X is smaller for views and subqueries so that the query planner
153716 ** will be more aggressive about generating automatic indexes for
153717 ** those objects, since there is no opportunity to add schema
153718 ** indexes on subqueries and views. */
153719 pNew->rSetup = rLogSize + rSize;
153720 if( !IsView(pTab) && (pTab->tabFlags & TF_Ephemeral)==0 ){
153721 pNew->rSetup += 28;
153722 }else{
153723 pNew->rSetup -= 10;
153724 }
153725 ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
153726 if( pNew->rSetup<0 ) pNew->rSetup = 0;
153727 /* TUNING: Each index lookup yields 20 rows in the table. This
153728 ** is more than the usual guess of 10 rows, since we have no way
153729 ** of knowing how selective the index will ultimately be. It would
153730 ** not be unreasonable to make this value much larger. */
153731 pNew->nOut = 43; assert( 43==sqlite3LogEst(20) );
153732 pNew->rRun = sqlite3LogEstAdd(a: rLogSize,b: pNew->nOut);
153733 pNew->wsFlags = WHERE_AUTO_INDEX;
153734 pNew->prereq = mPrereq | pTerm->prereqRight;
153735 rc = whereLoopInsert(pBuilder, pTemplate: pNew);
153736 }
153737 }
153738 }
153739#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
153740
153741 /* Loop over all indices. If there was an INDEXED BY clause, then only
153742 ** consider index pProbe. */
153743 for(; rc==SQLITE_OK && pProbe;
153744 pProbe=(pSrc->fg.isIndexedBy ? 0 : pProbe->pNext), iSortIdx++
153745 ){
153746 int isLeft = (pSrc->fg.jointype & JT_OUTER)!=0;
153747 if( pProbe->pPartIdxWhere!=0
153748 && !whereUsablePartialIndex(iTab: pSrc->iCursor, isLeft, pWC,
153749 pWhere: pProbe->pPartIdxWhere)
153750 ){
153751 testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */
153752 continue; /* Partial index inappropriate for this query */
153753 }
153754 if( pProbe->bNoQuery ) continue;
153755 rSize = pProbe->aiRowLogEst[0];
153756 pNew->u.btree.nEq = 0;
153757 pNew->u.btree.nBtm = 0;
153758 pNew->u.btree.nTop = 0;
153759 pNew->nSkip = 0;
153760 pNew->nLTerm = 0;
153761 pNew->iSortIdx = 0;
153762 pNew->rSetup = 0;
153763 pNew->prereq = mPrereq;
153764 pNew->nOut = rSize;
153765 pNew->u.btree.pIndex = pProbe;
153766 b = indexMightHelpWithOrderBy(pBuilder, pIndex: pProbe, iCursor: pSrc->iCursor);
153767
153768 /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */
153769 assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 );
153770 if( pProbe->idxType==SQLITE_IDXTYPE_IPK ){
153771 /* Integer primary key index */
153772 pNew->wsFlags = WHERE_IPK;
153773
153774 /* Full table scan */
153775 pNew->iSortIdx = b ? iSortIdx : 0;
153776 /* TUNING: Cost of full table scan is 3.0*N. The 3.0 factor is an
153777 ** extra cost designed to discourage the use of full table scans,
153778 ** since index lookups have better worst-case performance if our
153779 ** stat guesses are wrong. Reduce the 3.0 penalty slightly
153780 ** (to 2.75) if we have valid STAT4 information for the table.
153781 ** At 2.75, a full table scan is preferred over using an index on
153782 ** a column with just two distinct values where each value has about
153783 ** an equal number of appearances. Without STAT4 data, we still want
153784 ** to use an index in that case, since the constraint might be for
153785 ** the scarcer of the two values, and in that case an index lookup is
153786 ** better.
153787 */
153788#ifdef SQLITE_ENABLE_STAT4
153789 pNew->rRun = rSize + 16 - 2*((pTab->tabFlags & TF_HasStat4)!=0);
153790#else
153791 pNew->rRun = rSize + 16;
153792#endif
153793 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
153794 whereLoopOutputAdjust(pWC, pLoop: pNew, nRow: rSize);
153795 rc = whereLoopInsert(pBuilder, pTemplate: pNew);
153796 pNew->nOut = rSize;
153797 if( rc ) break;
153798 }else{
153799 Bitmask m;
153800 if( pProbe->isCovering ){
153801 pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
153802 m = 0;
153803 }else{
153804 m = pSrc->colUsed & pProbe->colNotIdxed;
153805 pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED;
153806 }
153807
153808 /* Full scan via index */
153809 if( b
153810 || !HasRowid(pTab)
153811 || pProbe->pPartIdxWhere!=0
153812 || pSrc->fg.isIndexedBy
153813 || ( m==0
153814 && pProbe->bUnordered==0
153815 && (pProbe->szIdxRow<pTab->szTabRow)
153816 && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
153817 && sqlite3GlobalConfig.bUseCis
153818 && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
153819 )
153820 ){
153821 pNew->iSortIdx = b ? iSortIdx : 0;
153822
153823 /* The cost of visiting the index rows is N*K, where K is
153824 ** between 1.1 and 3.0, depending on the relative sizes of the
153825 ** index and table rows. */
153826 pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
153827 if( m!=0 ){
153828 /* If this is a non-covering index scan, add in the cost of
153829 ** doing table lookups. The cost will be 3x the number of
153830 ** lookups. Take into account WHERE clause terms that can be
153831 ** satisfied using just the index, and that do not require a
153832 ** table lookup. */
153833 LogEst nLookup = rSize + 16; /* Base cost: N*3 */
153834 int ii;
153835 int iCur = pSrc->iCursor;
153836 WhereClause *pWC2 = &pWInfo->sWC;
153837 for(ii=0; ii<pWC2->nTerm; ii++){
153838 WhereTerm *pTerm = &pWC2->a[ii];
153839 if( !sqlite3ExprCoveredByIndex(pExpr: pTerm->pExpr, iCur, pIdx: pProbe) ){
153840 break;
153841 }
153842 /* pTerm can be evaluated using just the index. So reduce
153843 ** the expected number of table lookups accordingly */
153844 if( pTerm->truthProb<=0 ){
153845 nLookup += pTerm->truthProb;
153846 }else{
153847 nLookup--;
153848 if( pTerm->eOperator & (WO_EQ|WO_IS) ) nLookup -= 19;
153849 }
153850 }
153851
153852 pNew->rRun = sqlite3LogEstAdd(a: pNew->rRun, b: nLookup);
153853 }
153854 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
153855 whereLoopOutputAdjust(pWC, pLoop: pNew, nRow: rSize);
153856 rc = whereLoopInsert(pBuilder, pTemplate: pNew);
153857 pNew->nOut = rSize;
153858 if( rc ) break;
153859 }
153860 }
153861
153862 pBuilder->bldFlags1 = 0;
153863 rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul: 0);
153864 if( pBuilder->bldFlags1==SQLITE_BLDF1_INDEXED ){
153865 /* If a non-unique index is used, or if a prefix of the key for
153866 ** unique index is used (making the index functionally non-unique)
153867 ** then the sqlite_stat1 data becomes important for scoring the
153868 ** plan */
153869 pTab->tabFlags |= TF_StatsUsed;
153870 }
153871#ifdef SQLITE_ENABLE_STAT4
153872 sqlite3Stat4ProbeFree(pBuilder->pRec);
153873 pBuilder->nRecValid = 0;
153874 pBuilder->pRec = 0;
153875#endif
153876 }
153877 return rc;
153878}
153879
153880#ifndef SQLITE_OMIT_VIRTUALTABLE
153881
153882/*
153883** Argument pIdxInfo is already populated with all constraints that may
153884** be used by the virtual table identified by pBuilder->pNew->iTab. This
153885** function marks a subset of those constraints usable, invokes the
153886** xBestIndex method and adds the returned plan to pBuilder.
153887**
153888** A constraint is marked usable if:
153889**
153890** * Argument mUsable indicates that its prerequisites are available, and
153891**
153892** * It is not one of the operators specified in the mExclude mask passed
153893** as the fourth argument (which in practice is either WO_IN or 0).
153894**
153895** Argument mPrereq is a mask of tables that must be scanned before the
153896** virtual table in question. These are added to the plans prerequisites
153897** before it is added to pBuilder.
153898**
153899** Output parameter *pbIn is set to true if the plan added to pBuilder
153900** uses one or more WO_IN terms, or false otherwise.
153901*/
153902static int whereLoopAddVirtualOne(
153903 WhereLoopBuilder *pBuilder,
153904 Bitmask mPrereq, /* Mask of tables that must be used. */
153905 Bitmask mUsable, /* Mask of usable tables */
153906 u16 mExclude, /* Exclude terms using these operators */
153907 sqlite3_index_info *pIdxInfo, /* Populated object for xBestIndex */
153908 u16 mNoOmit, /* Do not omit these constraints */
153909 int *pbIn /* OUT: True if plan uses an IN(...) op */
153910){
153911 WhereClause *pWC = pBuilder->pWC;
153912 struct sqlite3_index_constraint *pIdxCons;
153913 struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage;
153914 int i;
153915 int mxTerm;
153916 int rc = SQLITE_OK;
153917 WhereLoop *pNew = pBuilder->pNew;
153918 Parse *pParse = pBuilder->pWInfo->pParse;
153919 SrcItem *pSrc = &pBuilder->pWInfo->pTabList->a[pNew->iTab];
153920 int nConstraint = pIdxInfo->nConstraint;
153921
153922 assert( (mUsable & mPrereq)==mPrereq );
153923 *pbIn = 0;
153924 pNew->prereq = mPrereq;
153925
153926 /* Set the usable flag on the subset of constraints identified by
153927 ** arguments mUsable and mExclude. */
153928 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
153929 for(i=0; i<nConstraint; i++, pIdxCons++){
153930 WhereTerm *pTerm = &pWC->a[pIdxCons->iTermOffset];
153931 pIdxCons->usable = 0;
153932 if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight
153933 && (pTerm->eOperator & mExclude)==0
153934 ){
153935 pIdxCons->usable = 1;
153936 }
153937 }
153938
153939 /* Initialize the output fields of the sqlite3_index_info structure */
153940 memset(s: pUsage, c: 0, n: sizeof(pUsage[0])*nConstraint);
153941 assert( pIdxInfo->needToFreeIdxStr==0 );
153942 pIdxInfo->idxStr = 0;
153943 pIdxInfo->idxNum = 0;
153944 pIdxInfo->orderByConsumed = 0;
153945 pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2;
153946 pIdxInfo->estimatedRows = 25;
153947 pIdxInfo->idxFlags = 0;
153948 pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed;
153949
153950 /* Invoke the virtual table xBestIndex() method */
153951 rc = vtabBestIndex(pParse, pTab: pSrc->pTab, p: pIdxInfo);
153952 if( rc ){
153953 if( rc==SQLITE_CONSTRAINT ){
153954 /* If the xBestIndex method returns SQLITE_CONSTRAINT, that means
153955 ** that the particular combination of parameters provided is unusable.
153956 ** Make no entries in the loop table.
153957 */
153958 WHERETRACE(0xffff, (" ^^^^--- non-viable plan rejected!\n"));
153959 return SQLITE_OK;
153960 }
153961 return rc;
153962 }
153963
153964 mxTerm = -1;
153965 assert( pNew->nLSlot>=nConstraint );
153966 for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0;
153967 pNew->u.vtab.omitMask = 0;
153968 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
153969 for(i=0; i<nConstraint; i++, pIdxCons++){
153970 int iTerm;
153971 if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
153972 WhereTerm *pTerm;
153973 int j = pIdxCons->iTermOffset;
153974 if( iTerm>=nConstraint
153975 || j<0
153976 || j>=pWC->nTerm
153977 || pNew->aLTerm[iTerm]!=0
153978 || pIdxCons->usable==0
153979 ){
153980 sqlite3ErrorMsg(pParse,zFormat: "%s.xBestIndex malfunction",pSrc->pTab->zName);
153981 testcase( pIdxInfo->needToFreeIdxStr );
153982 return SQLITE_ERROR;
153983 }
153984 testcase( iTerm==nConstraint-1 );
153985 testcase( j==0 );
153986 testcase( j==pWC->nTerm-1 );
153987 pTerm = &pWC->a[j];
153988 pNew->prereq |= pTerm->prereqRight;
153989 assert( iTerm<pNew->nLSlot );
153990 pNew->aLTerm[iTerm] = pTerm;
153991 if( iTerm>mxTerm ) mxTerm = iTerm;
153992 testcase( iTerm==15 );
153993 testcase( iTerm==16 );
153994 if( pUsage[i].omit ){
153995 if( i<16 && ((1<<i)&mNoOmit)==0 ){
153996 testcase( i!=iTerm );
153997 pNew->u.vtab.omitMask |= 1<<iTerm;
153998 }else{
153999 testcase( i!=iTerm );
154000 }
154001 }
154002 if( (pTerm->eOperator & WO_IN)!=0 ){
154003 /* A virtual table that is constrained by an IN clause may not
154004 ** consume the ORDER BY clause because (1) the order of IN terms
154005 ** is not necessarily related to the order of output terms and
154006 ** (2) Multiple outputs from a single IN value will not merge
154007 ** together. */
154008 pIdxInfo->orderByConsumed = 0;
154009 pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE;
154010 *pbIn = 1; assert( (mExclude & WO_IN)==0 );
154011 }
154012 }
154013 }
154014
154015 pNew->nLTerm = mxTerm+1;
154016 for(i=0; i<=mxTerm; i++){
154017 if( pNew->aLTerm[i]==0 ){
154018 /* The non-zero argvIdx values must be contiguous. Raise an
154019 ** error if they are not */
154020 sqlite3ErrorMsg(pParse,zFormat: "%s.xBestIndex malfunction",pSrc->pTab->zName);
154021 testcase( pIdxInfo->needToFreeIdxStr );
154022 return SQLITE_ERROR;
154023 }
154024 }
154025 assert( pNew->nLTerm<=pNew->nLSlot );
154026 pNew->u.vtab.idxNum = pIdxInfo->idxNum;
154027 pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
154028 pIdxInfo->needToFreeIdxStr = 0;
154029 pNew->u.vtab.idxStr = pIdxInfo->idxStr;
154030 pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ?
154031 pIdxInfo->nOrderBy : 0);
154032 pNew->rSetup = 0;
154033 pNew->rRun = sqlite3LogEstFromDouble(x: pIdxInfo->estimatedCost);
154034 pNew->nOut = sqlite3LogEst(x: pIdxInfo->estimatedRows);
154035
154036 /* Set the WHERE_ONEROW flag if the xBestIndex() method indicated
154037 ** that the scan will visit at most one row. Clear it otherwise. */
154038 if( pIdxInfo->idxFlags & SQLITE_INDEX_SCAN_UNIQUE ){
154039 pNew->wsFlags |= WHERE_ONEROW;
154040 }else{
154041 pNew->wsFlags &= ~WHERE_ONEROW;
154042 }
154043 rc = whereLoopInsert(pBuilder, pTemplate: pNew);
154044 if( pNew->u.vtab.needFree ){
154045 sqlite3_free(p: pNew->u.vtab.idxStr);
154046 pNew->u.vtab.needFree = 0;
154047 }
154048 WHERETRACE(0xffff, (" bIn=%d prereqIn=%04llx prereqOut=%04llx\n",
154049 *pbIn, (sqlite3_uint64)mPrereq,
154050 (sqlite3_uint64)(pNew->prereq & ~mPrereq)));
154051
154052 return rc;
154053}
154054
154055/*
154056** If this function is invoked from within an xBestIndex() callback, it
154057** returns a pointer to a buffer containing the name of the collation
154058** sequence associated with element iCons of the sqlite3_index_info.aConstraint
154059** array. Or, if iCons is out of range or there is no active xBestIndex
154060** call, return NULL.
154061*/
154062SQLITE_API const char *sqlite3_vtab_collation(sqlite3_index_info *pIdxInfo, int iCons){
154063 HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
154064 const char *zRet = 0;
154065 if( iCons>=0 && iCons<pIdxInfo->nConstraint ){
154066 CollSeq *pC = 0;
154067 int iTerm = pIdxInfo->aConstraint[iCons].iTermOffset;
154068 Expr *pX = pHidden->pWC->a[iTerm].pExpr;
154069 if( pX->pLeft ){
154070 pC = sqlite3ExprCompareCollSeq(pParse: pHidden->pParse, p: pX);
154071 }
154072 zRet = (pC ? pC->zName : sqlite3StrBINARY);
154073 }
154074 return zRet;
154075}
154076
154077/*
154078** Add all WhereLoop objects for a table of the join identified by
154079** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table.
154080**
154081** If there are no LEFT or CROSS JOIN joins in the query, both mPrereq and
154082** mUnusable are set to 0. Otherwise, mPrereq is a mask of all FROM clause
154083** entries that occur before the virtual table in the FROM clause and are
154084** separated from it by at least one LEFT or CROSS JOIN. Similarly, the
154085** mUnusable mask contains all FROM clause entries that occur after the
154086** virtual table and are separated from it by at least one LEFT or
154087** CROSS JOIN.
154088**
154089** For example, if the query were:
154090**
154091** ... FROM t1, t2 LEFT JOIN t3, t4, vt CROSS JOIN t5, t6;
154092**
154093** then mPrereq corresponds to (t1, t2) and mUnusable to (t5, t6).
154094**
154095** All the tables in mPrereq must be scanned before the current virtual
154096** table. So any terms for which all prerequisites are satisfied by
154097** mPrereq may be specified as "usable" in all calls to xBestIndex.
154098** Conversely, all tables in mUnusable must be scanned after the current
154099** virtual table, so any terms for which the prerequisites overlap with
154100** mUnusable should always be configured as "not-usable" for xBestIndex.
154101*/
154102static int whereLoopAddVirtual(
154103 WhereLoopBuilder *pBuilder, /* WHERE clause information */
154104 Bitmask mPrereq, /* Tables that must be scanned before this one */
154105 Bitmask mUnusable /* Tables that must be scanned after this one */
154106){
154107 int rc = SQLITE_OK; /* Return code */
154108 WhereInfo *pWInfo; /* WHERE analysis context */
154109 Parse *pParse; /* The parsing context */
154110 WhereClause *pWC; /* The WHERE clause */
154111 SrcItem *pSrc; /* The FROM clause term to search */
154112 sqlite3_index_info *p; /* Object to pass to xBestIndex() */
154113 int nConstraint; /* Number of constraints in p */
154114 int bIn; /* True if plan uses IN(...) operator */
154115 WhereLoop *pNew;
154116 Bitmask mBest; /* Tables used by best possible plan */
154117 u16 mNoOmit;
154118
154119 assert( (mPrereq & mUnusable)==0 );
154120 pWInfo = pBuilder->pWInfo;
154121 pParse = pWInfo->pParse;
154122 pWC = pBuilder->pWC;
154123 pNew = pBuilder->pNew;
154124 pSrc = &pWInfo->pTabList->a[pNew->iTab];
154125 assert( IsVirtual(pSrc->pTab) );
154126 p = allocateIndexInfo(pParse, pWC, mUnusable, pSrc, pOrderBy: pBuilder->pOrderBy,
154127 pmNoOmit: &mNoOmit);
154128 if( p==0 ) return SQLITE_NOMEM_BKPT;
154129 pNew->rSetup = 0;
154130 pNew->wsFlags = WHERE_VIRTUALTABLE;
154131 pNew->nLTerm = 0;
154132 pNew->u.vtab.needFree = 0;
154133 nConstraint = p->nConstraint;
154134 if( whereLoopResize(db: pParse->db, p: pNew, n: nConstraint) ){
154135 sqlite3DbFree(db: pParse->db, p);
154136 return SQLITE_NOMEM_BKPT;
154137 }
154138
154139 /* First call xBestIndex() with all constraints usable. */
154140 WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pTab->zName));
154141 WHERETRACE(0x40, (" VirtualOne: all usable\n"));
154142 rc = whereLoopAddVirtualOne(pBuilder, mPrereq, ALLBITS, mExclude: 0, pIdxInfo: p, mNoOmit, pbIn: &bIn);
154143
154144 /* If the call to xBestIndex() with all terms enabled produced a plan
154145 ** that does not require any source tables (IOW: a plan with mBest==0)
154146 ** and does not use an IN(...) operator, then there is no point in making
154147 ** any further calls to xBestIndex() since they will all return the same
154148 ** result (if the xBestIndex() implementation is sane). */
154149 if( rc==SQLITE_OK && ((mBest = (pNew->prereq & ~mPrereq))!=0 || bIn) ){
154150 int seenZero = 0; /* True if a plan with no prereqs seen */
154151 int seenZeroNoIN = 0; /* Plan with no prereqs and no IN(...) seen */
154152 Bitmask mPrev = 0;
154153 Bitmask mBestNoIn = 0;
154154
154155 /* If the plan produced by the earlier call uses an IN(...) term, call
154156 ** xBestIndex again, this time with IN(...) terms disabled. */
154157 if( bIn ){
154158 WHERETRACE(0x40, (" VirtualOne: all usable w/o IN\n"));
154159 rc = whereLoopAddVirtualOne(
154160 pBuilder, mPrereq, ALLBITS, WO_IN, pIdxInfo: p, mNoOmit, pbIn: &bIn);
154161 assert( bIn==0 );
154162 mBestNoIn = pNew->prereq & ~mPrereq;
154163 if( mBestNoIn==0 ){
154164 seenZero = 1;
154165 seenZeroNoIN = 1;
154166 }
154167 }
154168
154169 /* Call xBestIndex once for each distinct value of (prereqRight & ~mPrereq)
154170 ** in the set of terms that apply to the current virtual table. */
154171 while( rc==SQLITE_OK ){
154172 int i;
154173 Bitmask mNext = ALLBITS;
154174 assert( mNext>0 );
154175 for(i=0; i<nConstraint; i++){
154176 Bitmask mThis = (
154177 pWC->a[p->aConstraint[i].iTermOffset].prereqRight & ~mPrereq
154178 );
154179 if( mThis>mPrev && mThis<mNext ) mNext = mThis;
154180 }
154181 mPrev = mNext;
154182 if( mNext==ALLBITS ) break;
154183 if( mNext==mBest || mNext==mBestNoIn ) continue;
154184 WHERETRACE(0x40, (" VirtualOne: mPrev=%04llx mNext=%04llx\n",
154185 (sqlite3_uint64)mPrev, (sqlite3_uint64)mNext));
154186 rc = whereLoopAddVirtualOne(
154187 pBuilder, mPrereq, mUsable: mNext|mPrereq, mExclude: 0, pIdxInfo: p, mNoOmit, pbIn: &bIn);
154188 if( pNew->prereq==mPrereq ){
154189 seenZero = 1;
154190 if( bIn==0 ) seenZeroNoIN = 1;
154191 }
154192 }
154193
154194 /* If the calls to xBestIndex() in the above loop did not find a plan
154195 ** that requires no source tables at all (i.e. one guaranteed to be
154196 ** usable), make a call here with all source tables disabled */
154197 if( rc==SQLITE_OK && seenZero==0 ){
154198 WHERETRACE(0x40, (" VirtualOne: all disabled\n"));
154199 rc = whereLoopAddVirtualOne(
154200 pBuilder, mPrereq, mUsable: mPrereq, mExclude: 0, pIdxInfo: p, mNoOmit, pbIn: &bIn);
154201 if( bIn==0 ) seenZeroNoIN = 1;
154202 }
154203
154204 /* If the calls to xBestIndex() have so far failed to find a plan
154205 ** that requires no source tables at all and does not use an IN(...)
154206 ** operator, make a final call to obtain one here. */
154207 if( rc==SQLITE_OK && seenZeroNoIN==0 ){
154208 WHERETRACE(0x40, (" VirtualOne: all disabled and w/o IN\n"));
154209 rc = whereLoopAddVirtualOne(
154210 pBuilder, mPrereq, mUsable: mPrereq, WO_IN, pIdxInfo: p, mNoOmit, pbIn: &bIn);
154211 }
154212 }
154213
154214 if( p->needToFreeIdxStr ) sqlite3_free(p: p->idxStr);
154215 sqlite3DbFreeNN(db: pParse->db, p);
154216 WHERETRACE(0x800, ("END %s.addVirtual(), rc=%d\n", pSrc->pTab->zName, rc));
154217 return rc;
154218}
154219#endif /* SQLITE_OMIT_VIRTUALTABLE */
154220
154221/*
154222** Add WhereLoop entries to handle OR terms. This works for either
154223** btrees or virtual tables.
154224*/
154225static int whereLoopAddOr(
154226 WhereLoopBuilder *pBuilder,
154227 Bitmask mPrereq,
154228 Bitmask mUnusable
154229){
154230 WhereInfo *pWInfo = pBuilder->pWInfo;
154231 WhereClause *pWC;
154232 WhereLoop *pNew;
154233 WhereTerm *pTerm, *pWCEnd;
154234 int rc = SQLITE_OK;
154235 int iCur;
154236 WhereClause tempWC;
154237 WhereLoopBuilder sSubBuild;
154238 WhereOrSet sSum, sCur;
154239 SrcItem *pItem;
154240
154241 pWC = pBuilder->pWC;
154242 pWCEnd = pWC->a + pWC->nTerm;
154243 pNew = pBuilder->pNew;
154244 memset(s: &sSum, c: 0, n: sizeof(sSum));
154245 pItem = pWInfo->pTabList->a + pNew->iTab;
154246 iCur = pItem->iCursor;
154247
154248 for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
154249 if( (pTerm->eOperator & WO_OR)!=0
154250 && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
154251 ){
154252 WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
154253 WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
154254 WhereTerm *pOrTerm;
154255 int once = 1;
154256 int i, j;
154257
154258 sSubBuild = *pBuilder;
154259 sSubBuild.pOrderBy = 0;
154260 sSubBuild.pOrSet = &sCur;
154261
154262 WHERETRACE(0x200, ("Begin processing OR-clause %p\n", pTerm));
154263 for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
154264 if( (pOrTerm->eOperator & WO_AND)!=0 ){
154265 sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
154266 }else if( pOrTerm->leftCursor==iCur ){
154267 tempWC.pWInfo = pWC->pWInfo;
154268 tempWC.pOuter = pWC;
154269 tempWC.op = TK_AND;
154270 tempWC.nTerm = 1;
154271 tempWC.a = pOrTerm;
154272 sSubBuild.pWC = &tempWC;
154273 }else{
154274 continue;
154275 }
154276 sCur.n = 0;
154277#ifdef WHERETRACE_ENABLED
154278 WHERETRACE(0x200, ("OR-term %d of %p has %d subterms:\n",
154279 (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm));
154280 if( sqlite3WhereTrace & 0x400 ){
154281 sqlite3WhereClausePrint(sSubBuild.pWC);
154282 }
154283#endif
154284#ifndef SQLITE_OMIT_VIRTUALTABLE
154285 if( IsVirtual(pItem->pTab) ){
154286 rc = whereLoopAddVirtual(pBuilder: &sSubBuild, mPrereq, mUnusable);
154287 }else
154288#endif
154289 {
154290 rc = whereLoopAddBtree(pBuilder: &sSubBuild, mPrereq);
154291 }
154292 if( rc==SQLITE_OK ){
154293 rc = whereLoopAddOr(pBuilder: &sSubBuild, mPrereq, mUnusable);
154294 }
154295 assert( rc==SQLITE_OK || rc==SQLITE_DONE || sCur.n==0
154296 || rc==SQLITE_NOMEM );
154297 testcase( rc==SQLITE_NOMEM && sCur.n>0 );
154298 testcase( rc==SQLITE_DONE );
154299 if( sCur.n==0 ){
154300 sSum.n = 0;
154301 break;
154302 }else if( once ){
154303 whereOrMove(pDest: &sSum, pSrc: &sCur);
154304 once = 0;
154305 }else{
154306 WhereOrSet sPrev;
154307 whereOrMove(pDest: &sPrev, pSrc: &sSum);
154308 sSum.n = 0;
154309 for(i=0; i<sPrev.n; i++){
154310 for(j=0; j<sCur.n; j++){
154311 whereOrInsert(pSet: &sSum, prereq: sPrev.a[i].prereq | sCur.a[j].prereq,
154312 rRun: sqlite3LogEstAdd(a: sPrev.a[i].rRun, b: sCur.a[j].rRun),
154313 nOut: sqlite3LogEstAdd(a: sPrev.a[i].nOut, b: sCur.a[j].nOut));
154314 }
154315 }
154316 }
154317 }
154318 pNew->nLTerm = 1;
154319 pNew->aLTerm[0] = pTerm;
154320 pNew->wsFlags = WHERE_MULTI_OR;
154321 pNew->rSetup = 0;
154322 pNew->iSortIdx = 0;
154323 memset(s: &pNew->u, c: 0, n: sizeof(pNew->u));
154324 for(i=0; rc==SQLITE_OK && i<sSum.n; i++){
154325 /* TUNING: Currently sSum.a[i].rRun is set to the sum of the costs
154326 ** of all sub-scans required by the OR-scan. However, due to rounding
154327 ** errors, it may be that the cost of the OR-scan is equal to its
154328 ** most expensive sub-scan. Add the smallest possible penalty
154329 ** (equivalent to multiplying the cost by 1.07) to ensure that
154330 ** this does not happen. Otherwise, for WHERE clauses such as the
154331 ** following where there is an index on "y":
154332 **
154333 ** WHERE likelihood(x=?, 0.99) OR y=?
154334 **
154335 ** the planner may elect to "OR" together a full-table scan and an
154336 ** index lookup. And other similarly odd results. */
154337 pNew->rRun = sSum.a[i].rRun + 1;
154338 pNew->nOut = sSum.a[i].nOut;
154339 pNew->prereq = sSum.a[i].prereq;
154340 rc = whereLoopInsert(pBuilder, pTemplate: pNew);
154341 }
154342 WHERETRACE(0x200, ("End processing OR-clause %p\n", pTerm));
154343 }
154344 }
154345 return rc;
154346}
154347
154348/*
154349** Add all WhereLoop objects for all tables
154350*/
154351static int whereLoopAddAll(WhereLoopBuilder *pBuilder){
154352 WhereInfo *pWInfo = pBuilder->pWInfo;
154353 Bitmask mPrereq = 0;
154354 Bitmask mPrior = 0;
154355 int iTab;
154356 SrcList *pTabList = pWInfo->pTabList;
154357 SrcItem *pItem;
154358 SrcItem *pEnd = &pTabList->a[pWInfo->nLevel];
154359 sqlite3 *db = pWInfo->pParse->db;
154360 int rc = SQLITE_OK;
154361 WhereLoop *pNew;
154362
154363 /* Loop over the tables in the join, from left to right */
154364 pNew = pBuilder->pNew;
154365 whereLoopInit(p: pNew);
154366 pBuilder->iPlanLimit = SQLITE_QUERY_PLANNER_LIMIT;
154367 for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
154368 Bitmask mUnusable = 0;
154369 pNew->iTab = iTab;
154370 pBuilder->iPlanLimit += SQLITE_QUERY_PLANNER_LIMIT_INCR;
154371 pNew->maskSelf = sqlite3WhereGetMask(pMaskSet: &pWInfo->sMaskSet, iCursor: pItem->iCursor);
154372 if( (pItem->fg.jointype & (JT_LEFT|JT_CROSS))!=0 ){
154373 /* This condition is true when pItem is the FROM clause term on the
154374 ** right-hand-side of a LEFT or CROSS JOIN. */
154375 mPrereq = mPrior;
154376 }else{
154377 mPrereq = 0;
154378 }
154379#ifndef SQLITE_OMIT_VIRTUALTABLE
154380 if( IsVirtual(pItem->pTab) ){
154381 SrcItem *p;
154382 for(p=&pItem[1]; p<pEnd; p++){
154383 if( mUnusable || (p->fg.jointype & (JT_LEFT|JT_CROSS)) ){
154384 mUnusable |= sqlite3WhereGetMask(pMaskSet: &pWInfo->sMaskSet, iCursor: p->iCursor);
154385 }
154386 }
154387 rc = whereLoopAddVirtual(pBuilder, mPrereq, mUnusable);
154388 }else
154389#endif /* SQLITE_OMIT_VIRTUALTABLE */
154390 {
154391 rc = whereLoopAddBtree(pBuilder, mPrereq);
154392 }
154393 if( rc==SQLITE_OK && pBuilder->pWC->hasOr ){
154394 rc = whereLoopAddOr(pBuilder, mPrereq, mUnusable);
154395 }
154396 mPrior |= pNew->maskSelf;
154397 if( rc || db->mallocFailed ){
154398 if( rc==SQLITE_DONE ){
154399 /* We hit the query planner search limit set by iPlanLimit */
154400 sqlite3_log(SQLITE_WARNING, zFormat: "abbreviated query algorithm search");
154401 rc = SQLITE_OK;
154402 }else{
154403 break;
154404 }
154405 }
154406 }
154407
154408 whereLoopClear(db, p: pNew);
154409 return rc;
154410}
154411
154412/*
154413** Examine a WherePath (with the addition of the extra WhereLoop of the 6th
154414** parameters) to see if it outputs rows in the requested ORDER BY
154415** (or GROUP BY) without requiring a separate sort operation. Return N:
154416**
154417** N>0: N terms of the ORDER BY clause are satisfied
154418** N==0: No terms of the ORDER BY clause are satisfied
154419** N<0: Unknown yet how many terms of ORDER BY might be satisfied.
154420**
154421** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as
154422** strict. With GROUP BY and DISTINCT the only requirement is that
154423** equivalent rows appear immediately adjacent to one another. GROUP BY
154424** and DISTINCT do not require rows to appear in any particular order as long
154425** as equivalent rows are grouped together. Thus for GROUP BY and DISTINCT
154426** the pOrderBy terms can be matched in any order. With ORDER BY, the
154427** pOrderBy terms must be matched in strict left-to-right order.
154428*/
154429static i8 wherePathSatisfiesOrderBy(
154430 WhereInfo *pWInfo, /* The WHERE clause */
154431 ExprList *pOrderBy, /* ORDER BY or GROUP BY or DISTINCT clause to check */
154432 WherePath *pPath, /* The WherePath to check */
154433 u16 wctrlFlags, /* WHERE_GROUPBY or _DISTINCTBY or _ORDERBY_LIMIT */
154434 u16 nLoop, /* Number of entries in pPath->aLoop[] */
154435 WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */
154436 Bitmask *pRevMask /* OUT: Mask of WhereLoops to run in reverse order */
154437){
154438 u8 revSet; /* True if rev is known */
154439 u8 rev; /* Composite sort order */
154440 u8 revIdx; /* Index sort order */
154441 u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */
154442 u8 distinctColumns; /* True if the loop has UNIQUE NOT NULL columns */
154443 u8 isMatch; /* iColumn matches a term of the ORDER BY clause */
154444 u16 eqOpMask; /* Allowed equality operators */
154445 u16 nKeyCol; /* Number of key columns in pIndex */
154446 u16 nColumn; /* Total number of ordered columns in the index */
154447 u16 nOrderBy; /* Number terms in the ORDER BY clause */
154448 int iLoop; /* Index of WhereLoop in pPath being processed */
154449 int i, j; /* Loop counters */
154450 int iCur; /* Cursor number for current WhereLoop */
154451 int iColumn; /* A column number within table iCur */
154452 WhereLoop *pLoop = 0; /* Current WhereLoop being processed. */
154453 WhereTerm *pTerm; /* A single term of the WHERE clause */
154454 Expr *pOBExpr; /* An expression from the ORDER BY clause */
154455 CollSeq *pColl; /* COLLATE function from an ORDER BY clause term */
154456 Index *pIndex; /* The index associated with pLoop */
154457 sqlite3 *db = pWInfo->pParse->db; /* Database connection */
154458 Bitmask obSat = 0; /* Mask of ORDER BY terms satisfied so far */
154459 Bitmask obDone; /* Mask of all ORDER BY terms */
154460 Bitmask orderDistinctMask; /* Mask of all well-ordered loops */
154461 Bitmask ready; /* Mask of inner loops */
154462
154463 /*
154464 ** We say the WhereLoop is "one-row" if it generates no more than one
154465 ** row of output. A WhereLoop is one-row if all of the following are true:
154466 ** (a) All index columns match with WHERE_COLUMN_EQ.
154467 ** (b) The index is unique
154468 ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row.
154469 ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags.
154470 **
154471 ** We say the WhereLoop is "order-distinct" if the set of columns from
154472 ** that WhereLoop that are in the ORDER BY clause are different for every
154473 ** row of the WhereLoop. Every one-row WhereLoop is automatically
154474 ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause
154475 ** is not order-distinct. To be order-distinct is not quite the same as being
154476 ** UNIQUE since a UNIQUE column or index can have multiple rows that
154477 ** are NULL and NULL values are equivalent for the purpose of order-distinct.
154478 ** To be order-distinct, the columns must be UNIQUE and NOT NULL.
154479 **
154480 ** The rowid for a table is always UNIQUE and NOT NULL so whenever the
154481 ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is
154482 ** automatically order-distinct.
154483 */
154484
154485 assert( pOrderBy!=0 );
154486 if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0;
154487
154488 nOrderBy = pOrderBy->nExpr;
154489 testcase( nOrderBy==BMS-1 );
154490 if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */
154491 isOrderDistinct = 1;
154492 obDone = MASKBIT(nOrderBy)-1;
154493 orderDistinctMask = 0;
154494 ready = 0;
154495 eqOpMask = WO_EQ | WO_IS | WO_ISNULL;
154496 if( wctrlFlags & (WHERE_ORDERBY_LIMIT|WHERE_ORDERBY_MAX|WHERE_ORDERBY_MIN) ){
154497 eqOpMask |= WO_IN;
154498 }
154499 for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){
154500 if( iLoop>0 ) ready |= pLoop->maskSelf;
154501 if( iLoop<nLoop ){
154502 pLoop = pPath->aLoop[iLoop];
154503 if( wctrlFlags & WHERE_ORDERBY_LIMIT ) continue;
154504 }else{
154505 pLoop = pLast;
154506 }
154507 if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){
154508 if( pLoop->u.vtab.isOrdered && (wctrlFlags & WHERE_DISTINCTBY)==0 ){
154509 obSat = obDone;
154510 }
154511 break;
154512 }else if( wctrlFlags & WHERE_DISTINCTBY ){
154513 pLoop->u.btree.nDistinctCol = 0;
154514 }
154515 iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
154516
154517 /* Mark off any ORDER BY term X that is a column in the table of
154518 ** the current loop for which there is term in the WHERE
154519 ** clause of the form X IS NULL or X=? that reference only outer
154520 ** loops.
154521 */
154522 for(i=0; i<nOrderBy; i++){
154523 if( MASKBIT(i) & obSat ) continue;
154524 pOBExpr = sqlite3ExprSkipCollateAndLikely(pExpr: pOrderBy->a[i].pExpr);
154525 if( NEVER(pOBExpr==0) ) continue;
154526 if( pOBExpr->op!=TK_COLUMN && pOBExpr->op!=TK_AGG_COLUMN ) continue;
154527 if( pOBExpr->iTable!=iCur ) continue;
154528 pTerm = sqlite3WhereFindTerm(pWC: &pWInfo->sWC, iCur, iColumn: pOBExpr->iColumn,
154529 notReady: ~ready, op: eqOpMask, pIdx: 0);
154530 if( pTerm==0 ) continue;
154531 if( pTerm->eOperator==WO_IN ){
154532 /* IN terms are only valid for sorting in the ORDER BY LIMIT
154533 ** optimization, and then only if they are actually used
154534 ** by the query plan */
154535 assert( wctrlFlags &
154536 (WHERE_ORDERBY_LIMIT|WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX) );
154537 for(j=0; j<pLoop->nLTerm && pTerm!=pLoop->aLTerm[j]; j++){}
154538 if( j>=pLoop->nLTerm ) continue;
154539 }
154540 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){
154541 Parse *pParse = pWInfo->pParse;
154542 CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pExpr: pOrderBy->a[i].pExpr);
154543 CollSeq *pColl2 = sqlite3ExprCompareCollSeq(pParse, p: pTerm->pExpr);
154544 assert( pColl1 );
154545 if( pColl2==0 || sqlite3StrICmp(zLeft: pColl1->zName, zRight: pColl2->zName) ){
154546 continue;
154547 }
154548 testcase( pTerm->pExpr->op==TK_IS );
154549 }
154550 obSat |= MASKBIT(i);
154551 }
154552
154553 if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){
154554 if( pLoop->wsFlags & WHERE_IPK ){
154555 pIndex = 0;
154556 nKeyCol = 0;
154557 nColumn = 1;
154558 }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
154559 return 0;
154560 }else{
154561 nKeyCol = pIndex->nKeyCol;
154562 nColumn = pIndex->nColumn;
154563 assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) );
154564 assert( pIndex->aiColumn[nColumn-1]==XN_ROWID
154565 || !HasRowid(pIndex->pTable));
154566 /* All relevant terms of the index must also be non-NULL in order
154567 ** for isOrderDistinct to be true. So the isOrderDistint value
154568 ** computed here might be a false positive. Corrections will be
154569 ** made at tag-20210426-1 below */
154570 isOrderDistinct = IsUniqueIndex(pIndex)
154571 && (pLoop->wsFlags & WHERE_SKIPSCAN)==0;
154572 }
154573
154574 /* Loop through all columns of the index and deal with the ones
154575 ** that are not constrained by == or IN.
154576 */
154577 rev = revSet = 0;
154578 distinctColumns = 0;
154579 for(j=0; j<nColumn; j++){
154580 u8 bOnce = 1; /* True to run the ORDER BY search loop */
154581
154582 assert( j>=pLoop->u.btree.nEq
154583 || (pLoop->aLTerm[j]==0)==(j<pLoop->nSkip)
154584 );
154585 if( j<pLoop->u.btree.nEq && j>=pLoop->nSkip ){
154586 u16 eOp = pLoop->aLTerm[j]->eOperator;
154587
154588 /* Skip over == and IS and ISNULL terms. (Also skip IN terms when
154589 ** doing WHERE_ORDERBY_LIMIT processing). Except, IS and ISNULL
154590 ** terms imply that the index is not UNIQUE NOT NULL in which case
154591 ** the loop need to be marked as not order-distinct because it can
154592 ** have repeated NULL rows.
154593 **
154594 ** If the current term is a column of an ((?,?) IN (SELECT...))
154595 ** expression for which the SELECT returns more than one column,
154596 ** check that it is the only column used by this loop. Otherwise,
154597 ** if it is one of two or more, none of the columns can be
154598 ** considered to match an ORDER BY term.
154599 */
154600 if( (eOp & eqOpMask)!=0 ){
154601 if( eOp & (WO_ISNULL|WO_IS) ){
154602 testcase( eOp & WO_ISNULL );
154603 testcase( eOp & WO_IS );
154604 testcase( isOrderDistinct );
154605 isOrderDistinct = 0;
154606 }
154607 continue;
154608 }else if( ALWAYS(eOp & WO_IN) ){
154609 /* ALWAYS() justification: eOp is an equality operator due to the
154610 ** j<pLoop->u.btree.nEq constraint above. Any equality other
154611 ** than WO_IN is captured by the previous "if". So this one
154612 ** always has to be WO_IN. */
154613 Expr *pX = pLoop->aLTerm[j]->pExpr;
154614 for(i=j+1; i<pLoop->u.btree.nEq; i++){
154615 if( pLoop->aLTerm[i]->pExpr==pX ){
154616 assert( (pLoop->aLTerm[i]->eOperator & WO_IN) );
154617 bOnce = 0;
154618 break;
154619 }
154620 }
154621 }
154622 }
154623
154624 /* Get the column number in the table (iColumn) and sort order
154625 ** (revIdx) for the j-th column of the index.
154626 */
154627 if( pIndex ){
154628 iColumn = pIndex->aiColumn[j];
154629 revIdx = pIndex->aSortOrder[j] & KEYINFO_ORDER_DESC;
154630 if( iColumn==pIndex->pTable->iPKey ) iColumn = XN_ROWID;
154631 }else{
154632 iColumn = XN_ROWID;
154633 revIdx = 0;
154634 }
154635
154636 /* An unconstrained column that might be NULL means that this
154637 ** WhereLoop is not well-ordered. tag-20210426-1
154638 */
154639 if( isOrderDistinct ){
154640 if( iColumn>=0
154641 && j>=pLoop->u.btree.nEq
154642 && pIndex->pTable->aCol[iColumn].notNull==0
154643 ){
154644 isOrderDistinct = 0;
154645 }
154646 if( iColumn==XN_EXPR ){
154647 isOrderDistinct = 0;
154648 }
154649 }
154650
154651 /* Find the ORDER BY term that corresponds to the j-th column
154652 ** of the index and mark that ORDER BY term off
154653 */
154654 isMatch = 0;
154655 for(i=0; bOnce && i<nOrderBy; i++){
154656 if( MASKBIT(i) & obSat ) continue;
154657 pOBExpr = sqlite3ExprSkipCollateAndLikely(pExpr: pOrderBy->a[i].pExpr);
154658 testcase( wctrlFlags & WHERE_GROUPBY );
154659 testcase( wctrlFlags & WHERE_DISTINCTBY );
154660 if( NEVER(pOBExpr==0) ) continue;
154661 if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0;
154662 if( iColumn>=XN_ROWID ){
154663 if( pOBExpr->op!=TK_COLUMN && pOBExpr->op!=TK_AGG_COLUMN ) continue;
154664 if( pOBExpr->iTable!=iCur ) continue;
154665 if( pOBExpr->iColumn!=iColumn ) continue;
154666 }else{
154667 Expr *pIdxExpr = pIndex->aColExpr->a[j].pExpr;
154668 if( sqlite3ExprCompareSkip(pA: pOBExpr, pB: pIdxExpr, iTab: iCur) ){
154669 continue;
154670 }
154671 }
154672 if( iColumn!=XN_ROWID ){
154673 pColl = sqlite3ExprNNCollSeq(pParse: pWInfo->pParse, pExpr: pOrderBy->a[i].pExpr);
154674 if( sqlite3StrICmp(zLeft: pColl->zName, zRight: pIndex->azColl[j])!=0 ) continue;
154675 }
154676 if( wctrlFlags & WHERE_DISTINCTBY ){
154677 pLoop->u.btree.nDistinctCol = j+1;
154678 }
154679 isMatch = 1;
154680 break;
154681 }
154682 if( isMatch && (wctrlFlags & WHERE_GROUPBY)==0 ){
154683 /* Make sure the sort order is compatible in an ORDER BY clause.
154684 ** Sort order is irrelevant for a GROUP BY clause. */
154685 if( revSet ){
154686 if( (rev ^ revIdx)!=(pOrderBy->a[i].sortFlags&KEYINFO_ORDER_DESC) ){
154687 isMatch = 0;
154688 }
154689 }else{
154690 rev = revIdx ^ (pOrderBy->a[i].sortFlags & KEYINFO_ORDER_DESC);
154691 if( rev ) *pRevMask |= MASKBIT(iLoop);
154692 revSet = 1;
154693 }
154694 }
154695 if( isMatch && (pOrderBy->a[i].sortFlags & KEYINFO_ORDER_BIGNULL) ){
154696 if( j==pLoop->u.btree.nEq ){
154697 pLoop->wsFlags |= WHERE_BIGNULL_SORT;
154698 }else{
154699 isMatch = 0;
154700 }
154701 }
154702 if( isMatch ){
154703 if( iColumn==XN_ROWID ){
154704 testcase( distinctColumns==0 );
154705 distinctColumns = 1;
154706 }
154707 obSat |= MASKBIT(i);
154708 }else{
154709 /* No match found */
154710 if( j==0 || j<nKeyCol ){
154711 testcase( isOrderDistinct!=0 );
154712 isOrderDistinct = 0;
154713 }
154714 break;
154715 }
154716 } /* end Loop over all index columns */
154717 if( distinctColumns ){
154718 testcase( isOrderDistinct==0 );
154719 isOrderDistinct = 1;
154720 }
154721 } /* end-if not one-row */
154722
154723 /* Mark off any other ORDER BY terms that reference pLoop */
154724 if( isOrderDistinct ){
154725 orderDistinctMask |= pLoop->maskSelf;
154726 for(i=0; i<nOrderBy; i++){
154727 Expr *p;
154728 Bitmask mTerm;
154729 if( MASKBIT(i) & obSat ) continue;
154730 p = pOrderBy->a[i].pExpr;
154731 mTerm = sqlite3WhereExprUsage(pMaskSet: &pWInfo->sMaskSet,p);
154732 if( mTerm==0 && !sqlite3ExprIsConstant(p) ) continue;
154733 if( (mTerm&~orderDistinctMask)==0 ){
154734 obSat |= MASKBIT(i);
154735 }
154736 }
154737 }
154738 } /* End the loop over all WhereLoops from outer-most down to inner-most */
154739 if( obSat==obDone ) return (i8)nOrderBy;
154740 if( !isOrderDistinct ){
154741 for(i=nOrderBy-1; i>0; i--){
154742 Bitmask m = ALWAYS(i<BMS) ? MASKBIT(i) - 1 : 0;
154743 if( (obSat&m)==m ) return i;
154744 }
154745 return 0;
154746 }
154747 return -1;
154748}
154749
154750
154751/*
154752** If the WHERE_GROUPBY flag is set in the mask passed to sqlite3WhereBegin(),
154753** the planner assumes that the specified pOrderBy list is actually a GROUP
154754** BY clause - and so any order that groups rows as required satisfies the
154755** request.
154756**
154757** Normally, in this case it is not possible for the caller to determine
154758** whether or not the rows are really being delivered in sorted order, or
154759** just in some other order that provides the required grouping. However,
154760** if the WHERE_SORTBYGROUP flag is also passed to sqlite3WhereBegin(), then
154761** this function may be called on the returned WhereInfo object. It returns
154762** true if the rows really will be sorted in the specified order, or false
154763** otherwise.
154764**
154765** For example, assuming:
154766**
154767** CREATE INDEX i1 ON t1(x, Y);
154768**
154769** then
154770**
154771** SELECT * FROM t1 GROUP BY x,y ORDER BY x,y; -- IsSorted()==1
154772** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0
154773*/
154774SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo *pWInfo){
154775 assert( pWInfo->wctrlFlags & WHERE_GROUPBY );
154776 assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP );
154777 return pWInfo->sorted;
154778}
154779
154780#ifdef WHERETRACE_ENABLED
154781/* For debugging use only: */
154782static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){
154783 static char zName[65];
154784 int i;
154785 for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; }
154786 if( pLast ) zName[i++] = pLast->cId;
154787 zName[i] = 0;
154788 return zName;
154789}
154790#endif
154791
154792/*
154793** Return the cost of sorting nRow rows, assuming that the keys have
154794** nOrderby columns and that the first nSorted columns are already in
154795** order.
154796*/
154797static LogEst whereSortingCost(
154798 WhereInfo *pWInfo,
154799 LogEst nRow,
154800 int nOrderBy,
154801 int nSorted
154802){
154803 /* TUNING: Estimated cost of a full external sort, where N is
154804 ** the number of rows to sort is:
154805 **
154806 ** cost = (3.0 * N * log(N)).
154807 **
154808 ** Or, if the order-by clause has X terms but only the last Y
154809 ** terms are out of order, then block-sorting will reduce the
154810 ** sorting cost to:
154811 **
154812 ** cost = (3.0 * N * log(N)) * (Y/X)
154813 **
154814 ** The (Y/X) term is implemented using stack variable rScale
154815 ** below.
154816 */
154817 LogEst rScale, rSortCost;
154818 assert( nOrderBy>0 && 66==sqlite3LogEst(100) );
154819 rScale = sqlite3LogEst(x: (nOrderBy-nSorted)*100/nOrderBy) - 66;
154820 rSortCost = nRow + rScale + 16;
154821
154822 /* Multiple by log(M) where M is the number of output rows.
154823 ** Use the LIMIT for M if it is smaller. Or if this sort is for
154824 ** a DISTINCT operator, M will be the number of distinct output
154825 ** rows, so fudge it downwards a bit.
154826 */
154827 if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT)!=0 && pWInfo->iLimit<nRow ){
154828 nRow = pWInfo->iLimit;
154829 }else if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT) ){
154830 /* TUNING: In the sort for a DISTINCT operator, assume that the DISTINCT
154831 ** reduces the number of output rows by a factor of 2 */
154832 if( nRow>10 ){ nRow -= 10; assert( 10==sqlite3LogEst(2) ); }
154833 }
154834 rSortCost += estLog(N: nRow);
154835 return rSortCost;
154836}
154837
154838/*
154839** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
154840** attempts to find the lowest cost path that visits each WhereLoop
154841** once. This path is then loaded into the pWInfo->a[].pWLoop fields.
154842**
154843** Assume that the total number of output rows that will need to be sorted
154844** will be nRowEst (in the 10*log2 representation). Or, ignore sorting
154845** costs if nRowEst==0.
154846**
154847** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation
154848** error occurs.
154849*/
154850static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
154851 int mxChoice; /* Maximum number of simultaneous paths tracked */
154852 int nLoop; /* Number of terms in the join */
154853 Parse *pParse; /* Parsing context */
154854 sqlite3 *db; /* The database connection */
154855 int iLoop; /* Loop counter over the terms of the join */
154856 int ii, jj; /* Loop counters */
154857 int mxI = 0; /* Index of next entry to replace */
154858 int nOrderBy; /* Number of ORDER BY clause terms */
154859 LogEst mxCost = 0; /* Maximum cost of a set of paths */
154860 LogEst mxUnsorted = 0; /* Maximum unsorted cost of a set of path */
154861 int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */
154862 WherePath *aFrom; /* All nFrom paths at the previous level */
154863 WherePath *aTo; /* The nTo best paths at the current level */
154864 WherePath *pFrom; /* An element of aFrom[] that we are working on */
154865 WherePath *pTo; /* An element of aTo[] that we are working on */
154866 WhereLoop *pWLoop; /* One of the WhereLoop objects */
154867 WhereLoop **pX; /* Used to divy up the pSpace memory */
154868 LogEst *aSortCost = 0; /* Sorting and partial sorting costs */
154869 char *pSpace; /* Temporary memory used by this routine */
154870 int nSpace; /* Bytes of space allocated at pSpace */
154871
154872 pParse = pWInfo->pParse;
154873 db = pParse->db;
154874 nLoop = pWInfo->nLevel;
154875 /* TUNING: For simple queries, only the best path is tracked.
154876 ** For 2-way joins, the 5 best paths are followed.
154877 ** For joins of 3 or more tables, track the 10 best paths */
154878 mxChoice = (nLoop<=1) ? 1 : (nLoop==2 ? 5 : 10);
154879 assert( nLoop<=pWInfo->pTabList->nSrc );
154880 WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d)\n", nRowEst));
154881
154882 /* If nRowEst is zero and there is an ORDER BY clause, ignore it. In this
154883 ** case the purpose of this call is to estimate the number of rows returned
154884 ** by the overall query. Once this estimate has been obtained, the caller
154885 ** will invoke this function a second time, passing the estimate as the
154886 ** nRowEst parameter. */
154887 if( pWInfo->pOrderBy==0 || nRowEst==0 ){
154888 nOrderBy = 0;
154889 }else{
154890 nOrderBy = pWInfo->pOrderBy->nExpr;
154891 }
154892
154893 /* Allocate and initialize space for aTo, aFrom and aSortCost[] */
154894 nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
154895 nSpace += sizeof(LogEst) * nOrderBy;
154896 pSpace = sqlite3DbMallocRawNN(db, n: nSpace);
154897 if( pSpace==0 ) return SQLITE_NOMEM_BKPT;
154898 aTo = (WherePath*)pSpace;
154899 aFrom = aTo+mxChoice;
154900 memset(s: aFrom, c: 0, n: sizeof(aFrom[0]));
154901 pX = (WhereLoop**)(aFrom+mxChoice);
154902 for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
154903 pFrom->aLoop = pX;
154904 }
154905 if( nOrderBy ){
154906 /* If there is an ORDER BY clause and it is not being ignored, set up
154907 ** space for the aSortCost[] array. Each element of the aSortCost array
154908 ** is either zero - meaning it has not yet been initialized - or the
154909 ** cost of sorting nRowEst rows of data where the first X terms of
154910 ** the ORDER BY clause are already in order, where X is the array
154911 ** index. */
154912 aSortCost = (LogEst*)pX;
154913 memset(s: aSortCost, c: 0, n: sizeof(LogEst) * nOrderBy);
154914 }
154915 assert( aSortCost==0 || &pSpace[nSpace]==(char*)&aSortCost[nOrderBy] );
154916 assert( aSortCost!=0 || &pSpace[nSpace]==(char*)pX );
154917
154918 /* Seed the search with a single WherePath containing zero WhereLoops.
154919 **
154920 ** TUNING: Do not let the number of iterations go above 28. If the cost
154921 ** of computing an automatic index is not paid back within the first 28
154922 ** rows, then do not use the automatic index. */
154923 aFrom[0].nRow = MIN(pParse->nQueryLoop, 48); assert( 48==sqlite3LogEst(28) );
154924 nFrom = 1;
154925 assert( aFrom[0].isOrdered==0 );
154926 if( nOrderBy ){
154927 /* If nLoop is zero, then there are no FROM terms in the query. Since
154928 ** in this case the query may return a maximum of one row, the results
154929 ** are already in the requested order. Set isOrdered to nOrderBy to
154930 ** indicate this. Or, if nLoop is greater than zero, set isOrdered to
154931 ** -1, indicating that the result set may or may not be ordered,
154932 ** depending on the loops added to the current plan. */
154933 aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy;
154934 }
154935
154936 /* Compute successively longer WherePaths using the previous generation
154937 ** of WherePaths as the basis for the next. Keep track of the mxChoice
154938 ** best paths at each generation */
154939 for(iLoop=0; iLoop<nLoop; iLoop++){
154940 nTo = 0;
154941 for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){
154942 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
154943 LogEst nOut; /* Rows visited by (pFrom+pWLoop) */
154944 LogEst rCost; /* Cost of path (pFrom+pWLoop) */
154945 LogEst rUnsorted; /* Unsorted cost of (pFrom+pWLoop) */
154946 i8 isOrdered = pFrom->isOrdered; /* isOrdered for (pFrom+pWLoop) */
154947 Bitmask maskNew; /* Mask of src visited by (..) */
154948 Bitmask revMask = 0; /* Mask of rev-order loops for (..) */
154949
154950 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
154951 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
154952 if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<3 ){
154953 /* Do not use an automatic index if the this loop is expected
154954 ** to run less than 1.25 times. It is tempting to also exclude
154955 ** automatic index usage on an outer loop, but sometimes an automatic
154956 ** index is useful in the outer loop of a correlated subquery. */
154957 assert( 10==sqlite3LogEst(2) );
154958 continue;
154959 }
154960
154961 /* At this point, pWLoop is a candidate to be the next loop.
154962 ** Compute its cost */
154963 rUnsorted = sqlite3LogEstAdd(a: pWLoop->rSetup,b: pWLoop->rRun + pFrom->nRow);
154964 rUnsorted = sqlite3LogEstAdd(a: rUnsorted, b: pFrom->rUnsorted);
154965 nOut = pFrom->nRow + pWLoop->nOut;
154966 maskNew = pFrom->maskLoop | pWLoop->maskSelf;
154967 if( isOrdered<0 ){
154968 isOrdered = wherePathSatisfiesOrderBy(pWInfo,
154969 pOrderBy: pWInfo->pOrderBy, pPath: pFrom, wctrlFlags: pWInfo->wctrlFlags,
154970 nLoop: iLoop, pLast: pWLoop, pRevMask: &revMask);
154971 }else{
154972 revMask = pFrom->revLoop;
154973 }
154974 if( isOrdered>=0 && isOrdered<nOrderBy ){
154975 if( aSortCost[isOrdered]==0 ){
154976 aSortCost[isOrdered] = whereSortingCost(
154977 pWInfo, nRow: nRowEst, nOrderBy, nSorted: isOrdered
154978 );
154979 }
154980 /* TUNING: Add a small extra penalty (5) to sorting as an
154981 ** extra encouragment to the query planner to select a plan
154982 ** where the rows emerge in the correct order without any sorting
154983 ** required. */
154984 rCost = sqlite3LogEstAdd(a: rUnsorted, b: aSortCost[isOrdered]) + 5;
154985
154986 WHERETRACE(0x002,
154987 ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
154988 aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy,
154989 rUnsorted, rCost));
154990 }else{
154991 rCost = rUnsorted;
154992 rUnsorted -= 2; /* TUNING: Slight bias in favor of no-sort plans */
154993 }
154994
154995 /* Check to see if pWLoop should be added to the set of
154996 ** mxChoice best-so-far paths.
154997 **
154998 ** First look for an existing path among best-so-far paths
154999 ** that covers the same set of loops and has the same isOrdered
155000 ** setting as the current path candidate.
155001 **
155002 ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent
155003 ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range
155004 ** of legal values for isOrdered, -1..64.
155005 */
155006 for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){
155007 if( pTo->maskLoop==maskNew
155008 && ((pTo->isOrdered^isOrdered)&0x80)==0
155009 ){
155010 testcase( jj==nTo-1 );
155011 break;
155012 }
155013 }
155014 if( jj>=nTo ){
155015 /* None of the existing best-so-far paths match the candidate. */
155016 if( nTo>=mxChoice
155017 && (rCost>mxCost || (rCost==mxCost && rUnsorted>=mxUnsorted))
155018 ){
155019 /* The current candidate is no better than any of the mxChoice
155020 ** paths currently in the best-so-far buffer. So discard
155021 ** this candidate as not viable. */
155022#ifdef WHERETRACE_ENABLED /* 0x4 */
155023 if( sqlite3WhereTrace&0x4 ){
155024 sqlite3DebugPrintf("Skip %s cost=%-3d,%3d,%3d order=%c\n",
155025 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
155026 isOrdered>=0 ? isOrdered+'0' : '?');
155027 }
155028#endif
155029 continue;
155030 }
155031 /* If we reach this points it means that the new candidate path
155032 ** needs to be added to the set of best-so-far paths. */
155033 if( nTo<mxChoice ){
155034 /* Increase the size of the aTo set by one */
155035 jj = nTo++;
155036 }else{
155037 /* New path replaces the prior worst to keep count below mxChoice */
155038 jj = mxI;
155039 }
155040 pTo = &aTo[jj];
155041#ifdef WHERETRACE_ENABLED /* 0x4 */
155042 if( sqlite3WhereTrace&0x4 ){
155043 sqlite3DebugPrintf("New %s cost=%-3d,%3d,%3d order=%c\n",
155044 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
155045 isOrdered>=0 ? isOrdered+'0' : '?');
155046 }
155047#endif
155048 }else{
155049 /* Control reaches here if best-so-far path pTo=aTo[jj] covers the
155050 ** same set of loops and has the same isOrdered setting as the
155051 ** candidate path. Check to see if the candidate should replace
155052 ** pTo or if the candidate should be skipped.
155053 **
155054 ** The conditional is an expanded vector comparison equivalent to:
155055 ** (pTo->rCost,pTo->nRow,pTo->rUnsorted) <= (rCost,nOut,rUnsorted)
155056 */
155057 if( pTo->rCost<rCost
155058 || (pTo->rCost==rCost
155059 && (pTo->nRow<nOut
155060 || (pTo->nRow==nOut && pTo->rUnsorted<=rUnsorted)
155061 )
155062 )
155063 ){
155064#ifdef WHERETRACE_ENABLED /* 0x4 */
155065 if( sqlite3WhereTrace&0x4 ){
155066 sqlite3DebugPrintf(
155067 "Skip %s cost=%-3d,%3d,%3d order=%c",
155068 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
155069 isOrdered>=0 ? isOrdered+'0' : '?');
155070 sqlite3DebugPrintf(" vs %s cost=%-3d,%3d,%3d order=%c\n",
155071 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
155072 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
155073 }
155074#endif
155075 /* Discard the candidate path from further consideration */
155076 testcase( pTo->rCost==rCost );
155077 continue;
155078 }
155079 testcase( pTo->rCost==rCost+1 );
155080 /* Control reaches here if the candidate path is better than the
155081 ** pTo path. Replace pTo with the candidate. */
155082#ifdef WHERETRACE_ENABLED /* 0x4 */
155083 if( sqlite3WhereTrace&0x4 ){
155084 sqlite3DebugPrintf(
155085 "Update %s cost=%-3d,%3d,%3d order=%c",
155086 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
155087 isOrdered>=0 ? isOrdered+'0' : '?');
155088 sqlite3DebugPrintf(" was %s cost=%-3d,%3d,%3d order=%c\n",
155089 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
155090 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
155091 }
155092#endif
155093 }
155094 /* pWLoop is a winner. Add it to the set of best so far */
155095 pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
155096 pTo->revLoop = revMask;
155097 pTo->nRow = nOut;
155098 pTo->rCost = rCost;
155099 pTo->rUnsorted = rUnsorted;
155100 pTo->isOrdered = isOrdered;
155101 memcpy(dest: pTo->aLoop, src: pFrom->aLoop, n: sizeof(WhereLoop*)*iLoop);
155102 pTo->aLoop[iLoop] = pWLoop;
155103 if( nTo>=mxChoice ){
155104 mxI = 0;
155105 mxCost = aTo[0].rCost;
155106 mxUnsorted = aTo[0].nRow;
155107 for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){
155108 if( pTo->rCost>mxCost
155109 || (pTo->rCost==mxCost && pTo->rUnsorted>mxUnsorted)
155110 ){
155111 mxCost = pTo->rCost;
155112 mxUnsorted = pTo->rUnsorted;
155113 mxI = jj;
155114 }
155115 }
155116 }
155117 }
155118 }
155119
155120#ifdef WHERETRACE_ENABLED /* >=2 */
155121 if( sqlite3WhereTrace & 0x02 ){
155122 sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
155123 for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){
155124 sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
155125 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
155126 pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?');
155127 if( pTo->isOrdered>0 ){
155128 sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop);
155129 }else{
155130 sqlite3DebugPrintf("\n");
155131 }
155132 }
155133 }
155134#endif
155135
155136 /* Swap the roles of aFrom and aTo for the next generation */
155137 pFrom = aTo;
155138 aTo = aFrom;
155139 aFrom = pFrom;
155140 nFrom = nTo;
155141 }
155142
155143 if( nFrom==0 ){
155144 sqlite3ErrorMsg(pParse, zFormat: "no query solution");
155145 sqlite3DbFreeNN(db, p: pSpace);
155146 return SQLITE_ERROR;
155147 }
155148
155149 /* Find the lowest cost path. pFrom will be left pointing to that path */
155150 pFrom = aFrom;
155151 for(ii=1; ii<nFrom; ii++){
155152 if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii];
155153 }
155154 assert( pWInfo->nLevel==nLoop );
155155 /* Load the lowest cost path into pWInfo */
155156 for(iLoop=0; iLoop<nLoop; iLoop++){
155157 WhereLevel *pLevel = pWInfo->a + iLoop;
155158 pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop];
155159 pLevel->iFrom = pWLoop->iTab;
155160 pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor;
155161 }
155162 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0
155163 && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0
155164 && pWInfo->eDistinct==WHERE_DISTINCT_NOOP
155165 && nRowEst
155166 ){
155167 Bitmask notUsed;
155168 int rc = wherePathSatisfiesOrderBy(pWInfo, pOrderBy: pWInfo->pResultSet, pPath: pFrom,
155169 WHERE_DISTINCTBY, nLoop: nLoop-1, pLast: pFrom->aLoop[nLoop-1], pRevMask: &notUsed);
155170 if( rc==pWInfo->pResultSet->nExpr ){
155171 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
155172 }
155173 }
155174 pWInfo->bOrderedInnerLoop = 0;
155175 if( pWInfo->pOrderBy ){
155176 if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){
155177 if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){
155178 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
155179 }
155180 }else{
155181 pWInfo->nOBSat = pFrom->isOrdered;
155182 pWInfo->revMask = pFrom->revLoop;
155183 if( pWInfo->nOBSat<=0 ){
155184 pWInfo->nOBSat = 0;
155185 if( nLoop>0 ){
155186 u32 wsFlags = pFrom->aLoop[nLoop-1]->wsFlags;
155187 if( (wsFlags & WHERE_ONEROW)==0
155188 && (wsFlags&(WHERE_IPK|WHERE_COLUMN_IN))!=(WHERE_IPK|WHERE_COLUMN_IN)
155189 ){
155190 Bitmask m = 0;
155191 int rc = wherePathSatisfiesOrderBy(pWInfo, pOrderBy: pWInfo->pOrderBy, pPath: pFrom,
155192 WHERE_ORDERBY_LIMIT, nLoop: nLoop-1, pLast: pFrom->aLoop[nLoop-1], pRevMask: &m);
155193 testcase( wsFlags & WHERE_IPK );
155194 testcase( wsFlags & WHERE_COLUMN_IN );
155195 if( rc==pWInfo->pOrderBy->nExpr ){
155196 pWInfo->bOrderedInnerLoop = 1;
155197 pWInfo->revMask = m;
155198 }
155199 }
155200 }
155201 }else if( nLoop
155202 && pWInfo->nOBSat==1
155203 && (pWInfo->wctrlFlags & (WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX))!=0
155204 ){
155205 pWInfo->bOrderedInnerLoop = 1;
155206 }
155207 }
155208 if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP)
155209 && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0
155210 ){
155211 Bitmask revMask = 0;
155212 int nOrder = wherePathSatisfiesOrderBy(pWInfo, pOrderBy: pWInfo->pOrderBy,
155213 pPath: pFrom, wctrlFlags: 0, nLoop: nLoop-1, pLast: pFrom->aLoop[nLoop-1], pRevMask: &revMask
155214 );
155215 assert( pWInfo->sorted==0 );
155216 if( nOrder==pWInfo->pOrderBy->nExpr ){
155217 pWInfo->sorted = 1;
155218 pWInfo->revMask = revMask;
155219 }
155220 }
155221 }
155222
155223
155224 pWInfo->nRowOut = pFrom->nRow;
155225
155226 /* Free temporary memory and return success */
155227 sqlite3DbFreeNN(db, p: pSpace);
155228 return SQLITE_OK;
155229}
155230
155231/*
155232** Most queries use only a single table (they are not joins) and have
155233** simple == constraints against indexed fields. This routine attempts
155234** to plan those simple cases using much less ceremony than the
155235** general-purpose query planner, and thereby yield faster sqlite3_prepare()
155236** times for the common case.
155237**
155238** Return non-zero on success, if this query can be handled by this
155239** no-frills query planner. Return zero if this query needs the
155240** general-purpose query planner.
155241*/
155242static int whereShortCut(WhereLoopBuilder *pBuilder){
155243 WhereInfo *pWInfo;
155244 SrcItem *pItem;
155245 WhereClause *pWC;
155246 WhereTerm *pTerm;
155247 WhereLoop *pLoop;
155248 int iCur;
155249 int j;
155250 Table *pTab;
155251 Index *pIdx;
155252 WhereScan scan;
155253
155254 pWInfo = pBuilder->pWInfo;
155255 if( pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE ) return 0;
155256 assert( pWInfo->pTabList->nSrc>=1 );
155257 pItem = pWInfo->pTabList->a;
155258 pTab = pItem->pTab;
155259 if( IsVirtual(pTab) ) return 0;
155260 if( pItem->fg.isIndexedBy ) return 0;
155261 iCur = pItem->iCursor;
155262 pWC = &pWInfo->sWC;
155263 pLoop = pBuilder->pNew;
155264 pLoop->wsFlags = 0;
155265 pLoop->nSkip = 0;
155266 pTerm = whereScanInit(pScan: &scan, pWC, iCur, iColumn: -1, WO_EQ|WO_IS, pIdx: 0);
155267 while( pTerm && pTerm->prereqRight ) pTerm = whereScanNext(pScan: &scan);
155268 if( pTerm ){
155269 testcase( pTerm->eOperator & WO_IS );
155270 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
155271 pLoop->aLTerm[0] = pTerm;
155272 pLoop->nLTerm = 1;
155273 pLoop->u.btree.nEq = 1;
155274 /* TUNING: Cost of a rowid lookup is 10 */
155275 pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
155276 }else{
155277 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
155278 int opMask;
155279 assert( pLoop->aLTermSpace==pLoop->aLTerm );
155280 if( !IsUniqueIndex(pIdx)
155281 || pIdx->pPartIdxWhere!=0
155282 || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
155283 ) continue;
155284 opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ;
155285 for(j=0; j<pIdx->nKeyCol; j++){
155286 pTerm = whereScanInit(pScan: &scan, pWC, iCur, iColumn: j, opMask, pIdx);
155287 while( pTerm && pTerm->prereqRight ) pTerm = whereScanNext(pScan: &scan);
155288 if( pTerm==0 ) break;
155289 testcase( pTerm->eOperator & WO_IS );
155290 pLoop->aLTerm[j] = pTerm;
155291 }
155292 if( j!=pIdx->nKeyCol ) continue;
155293 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
155294 if( pIdx->isCovering || (pItem->colUsed & pIdx->colNotIdxed)==0 ){
155295 pLoop->wsFlags |= WHERE_IDX_ONLY;
155296 }
155297 pLoop->nLTerm = j;
155298 pLoop->u.btree.nEq = j;
155299 pLoop->u.btree.pIndex = pIdx;
155300 /* TUNING: Cost of a unique index lookup is 15 */
155301 pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */
155302 break;
155303 }
155304 }
155305 if( pLoop->wsFlags ){
155306 pLoop->nOut = (LogEst)1;
155307 pWInfo->a[0].pWLoop = pLoop;
155308 assert( pWInfo->sMaskSet.n==1 && iCur==pWInfo->sMaskSet.ix[0] );
155309 pLoop->maskSelf = 1; /* sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); */
155310 pWInfo->a[0].iTabCur = iCur;
155311 pWInfo->nRowOut = 1;
155312 if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr;
155313 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
155314 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
155315 }
155316 if( scan.iEquiv>1 ) pLoop->wsFlags |= WHERE_TRANSCONS;
155317#ifdef SQLITE_DEBUG
155318 pLoop->cId = '0';
155319#endif
155320#ifdef WHERETRACE_ENABLED
155321 if( sqlite3WhereTrace ){
155322 sqlite3DebugPrintf("whereShortCut() used to compute solution\n");
155323 }
155324#endif
155325 return 1;
155326 }
155327 return 0;
155328}
155329
155330/*
155331** Helper function for exprIsDeterministic().
155332*/
155333static int exprNodeIsDeterministic(Walker *pWalker, Expr *pExpr){
155334 if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_ConstFunc)==0 ){
155335 pWalker->eCode = 0;
155336 return WRC_Abort;
155337 }
155338 return WRC_Continue;
155339}
155340
155341/*
155342** Return true if the expression contains no non-deterministic SQL
155343** functions. Do not consider non-deterministic SQL functions that are
155344** part of sub-select statements.
155345*/
155346static int exprIsDeterministic(Expr *p){
155347 Walker w;
155348 memset(s: &w, c: 0, n: sizeof(w));
155349 w.eCode = 1;
155350 w.xExprCallback = exprNodeIsDeterministic;
155351 w.xSelectCallback = sqlite3SelectWalkFail;
155352 sqlite3WalkExpr(pWalker: &w, pExpr: p);
155353 return w.eCode;
155354}
155355
155356
155357#ifdef WHERETRACE_ENABLED
155358/*
155359** Display all WhereLoops in pWInfo
155360*/
155361static void showAllWhereLoops(WhereInfo *pWInfo, WhereClause *pWC){
155362 if( sqlite3WhereTrace ){ /* Display all of the WhereLoop objects */
155363 WhereLoop *p;
155364 int i;
155365 static const char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz"
155366 "ABCDEFGHIJKLMNOPQRSTUVWYXZ";
155367 for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
155368 p->cId = zLabel[i%(sizeof(zLabel)-1)];
155369 sqlite3WhereLoopPrint(p, pWC);
155370 }
155371 }
155372}
155373# define WHERETRACE_ALL_LOOPS(W,C) showAllWhereLoops(W,C)
155374#else
155375# define WHERETRACE_ALL_LOOPS(W,C)
155376#endif
155377
155378/*
155379** Generate the beginning of the loop used for WHERE clause processing.
155380** The return value is a pointer to an opaque structure that contains
155381** information needed to terminate the loop. Later, the calling routine
155382** should invoke sqlite3WhereEnd() with the return value of this function
155383** in order to complete the WHERE clause processing.
155384**
155385** If an error occurs, this routine returns NULL.
155386**
155387** The basic idea is to do a nested loop, one loop for each table in
155388** the FROM clause of a select. (INSERT and UPDATE statements are the
155389** same as a SELECT with only a single table in the FROM clause.) For
155390** example, if the SQL is this:
155391**
155392** SELECT * FROM t1, t2, t3 WHERE ...;
155393**
155394** Then the code generated is conceptually like the following:
155395**
155396** foreach row1 in t1 do \ Code generated
155397** foreach row2 in t2 do |-- by sqlite3WhereBegin()
155398** foreach row3 in t3 do /
155399** ...
155400** end \ Code generated
155401** end |-- by sqlite3WhereEnd()
155402** end /
155403**
155404** Note that the loops might not be nested in the order in which they
155405** appear in the FROM clause if a different order is better able to make
155406** use of indices. Note also that when the IN operator appears in
155407** the WHERE clause, it might result in additional nested loops for
155408** scanning through all values on the right-hand side of the IN.
155409**
155410** There are Btree cursors associated with each table. t1 uses cursor
155411** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
155412** And so forth. This routine generates code to open those VDBE cursors
155413** and sqlite3WhereEnd() generates the code to close them.
155414**
155415** The code that sqlite3WhereBegin() generates leaves the cursors named
155416** in pTabList pointing at their appropriate entries. The [...] code
155417** can use OP_Column and OP_Rowid opcodes on these cursors to extract
155418** data from the various tables of the loop.
155419**
155420** If the WHERE clause is empty, the foreach loops must each scan their
155421** entire tables. Thus a three-way join is an O(N^3) operation. But if
155422** the tables have indices and there are terms in the WHERE clause that
155423** refer to those indices, a complete table scan can be avoided and the
155424** code will run much faster. Most of the work of this routine is checking
155425** to see if there are indices that can be used to speed up the loop.
155426**
155427** Terms of the WHERE clause are also used to limit which rows actually
155428** make it to the "..." in the middle of the loop. After each "foreach",
155429** terms of the WHERE clause that use only terms in that loop and outer
155430** loops are evaluated and if false a jump is made around all subsequent
155431** inner loops (or around the "..." if the test occurs within the inner-
155432** most loop)
155433**
155434** OUTER JOINS
155435**
155436** An outer join of tables t1 and t2 is conceptally coded as follows:
155437**
155438** foreach row1 in t1 do
155439** flag = 0
155440** foreach row2 in t2 do
155441** start:
155442** ...
155443** flag = 1
155444** end
155445** if flag==0 then
155446** move the row2 cursor to a null row
155447** goto start
155448** fi
155449** end
155450**
155451** ORDER BY CLAUSE PROCESSING
155452**
155453** pOrderBy is a pointer to the ORDER BY clause (or the GROUP BY clause
155454** if the WHERE_GROUPBY flag is set in wctrlFlags) of a SELECT statement
155455** if there is one. If there is no ORDER BY clause or if this routine
155456** is called from an UPDATE or DELETE statement, then pOrderBy is NULL.
155457**
155458** The iIdxCur parameter is the cursor number of an index. If
155459** WHERE_OR_SUBCLAUSE is set, iIdxCur is the cursor number of an index
155460** to use for OR clause processing. The WHERE clause should use this
155461** specific cursor. If WHERE_ONEPASS_DESIRED is set, then iIdxCur is
155462** the first cursor in an array of cursors for all indices. iIdxCur should
155463** be used to compute the appropriate cursor depending on which index is
155464** used.
155465*/
155466SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
155467 Parse *pParse, /* The parser context */
155468 SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */
155469 Expr *pWhere, /* The WHERE clause */
155470 ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */
155471 ExprList *pResultSet, /* Query result set. Req'd for DISTINCT */
155472 u16 wctrlFlags, /* The WHERE_* flags defined in sqliteInt.h */
155473 int iAuxArg /* If WHERE_OR_SUBCLAUSE is set, index cursor number
155474 ** If WHERE_USE_LIMIT, then the limit amount */
155475){
155476 int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */
155477 int nTabList; /* Number of elements in pTabList */
155478 WhereInfo *pWInfo; /* Will become the return value of this function */
155479 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
155480 Bitmask notReady; /* Cursors that are not yet positioned */
155481 WhereLoopBuilder sWLB; /* The WhereLoop builder */
155482 WhereMaskSet *pMaskSet; /* The expression mask set */
155483 WhereLevel *pLevel; /* A single level in pWInfo->a[] */
155484 WhereLoop *pLoop; /* Pointer to a single WhereLoop object */
155485 int ii; /* Loop counter */
155486 sqlite3 *db; /* Database connection */
155487 int rc; /* Return code */
155488 u8 bFordelete = 0; /* OPFLAG_FORDELETE or zero, as appropriate */
155489
155490 assert( (wctrlFlags & WHERE_ONEPASS_MULTIROW)==0 || (
155491 (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0
155492 && (wctrlFlags & WHERE_OR_SUBCLAUSE)==0
155493 ));
155494
155495 /* Only one of WHERE_OR_SUBCLAUSE or WHERE_USE_LIMIT */
155496 assert( (wctrlFlags & WHERE_OR_SUBCLAUSE)==0
155497 || (wctrlFlags & WHERE_USE_LIMIT)==0 );
155498
155499 /* Variable initialization */
155500 db = pParse->db;
155501 memset(s: &sWLB, c: 0, n: sizeof(sWLB));
155502
155503 /* An ORDER/GROUP BY clause of more than 63 terms cannot be optimized */
155504 testcase( pOrderBy && pOrderBy->nExpr==BMS-1 );
155505 if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0;
155506 sWLB.pOrderBy = pOrderBy;
155507
155508 /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via
155509 ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
155510 if( OptimizationDisabled(db, SQLITE_DistinctOpt) ){
155511 wctrlFlags &= ~WHERE_WANT_DISTINCT;
155512 }
155513
155514 /* The number of tables in the FROM clause is limited by the number of
155515 ** bits in a Bitmask
155516 */
155517 testcase( pTabList->nSrc==BMS );
155518 if( pTabList->nSrc>BMS ){
155519 sqlite3ErrorMsg(pParse, zFormat: "at most %d tables in a join", BMS);
155520 return 0;
155521 }
155522
155523 /* This function normally generates a nested loop for all tables in
155524 ** pTabList. But if the WHERE_OR_SUBCLAUSE flag is set, then we should
155525 ** only generate code for the first table in pTabList and assume that
155526 ** any cursors associated with subsequent tables are uninitialized.
155527 */
155528 nTabList = (wctrlFlags & WHERE_OR_SUBCLAUSE) ? 1 : pTabList->nSrc;
155529
155530 /* Allocate and initialize the WhereInfo structure that will become the
155531 ** return value. A single allocation is used to store the WhereInfo
155532 ** struct, the contents of WhereInfo.a[], the WhereClause structure
155533 ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
155534 ** field (type Bitmask) it must be aligned on an 8-byte boundary on
155535 ** some architectures. Hence the ROUND8() below.
155536 */
155537 nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
155538 pWInfo = sqlite3DbMallocRawNN(db, n: nByteWInfo + sizeof(WhereLoop));
155539 if( db->mallocFailed ){
155540 sqlite3DbFree(db, p: pWInfo);
155541 pWInfo = 0;
155542 goto whereBeginError;
155543 }
155544 pWInfo->pParse = pParse;
155545 pWInfo->pTabList = pTabList;
155546 pWInfo->pOrderBy = pOrderBy;
155547 pWInfo->pWhere = pWhere;
155548 pWInfo->pResultSet = pResultSet;
155549 pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
155550 pWInfo->nLevel = nTabList;
155551 pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(pParse);
155552 pWInfo->wctrlFlags = wctrlFlags;
155553 pWInfo->iLimit = iAuxArg;
155554 pWInfo->savedNQueryLoop = pParse->nQueryLoop;
155555 memset(s: &pWInfo->nOBSat, c: 0,
155556 offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat));
155557 memset(s: &pWInfo->a[0], c: 0, n: sizeof(WhereLoop)+nTabList*sizeof(WhereLevel));
155558 assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */
155559 pMaskSet = &pWInfo->sMaskSet;
155560 sWLB.pWInfo = pWInfo;
155561 sWLB.pWC = &pWInfo->sWC;
155562 sWLB.pNew = (WhereLoop*)(((char*)pWInfo)+nByteWInfo);
155563 assert( EIGHT_BYTE_ALIGNMENT(sWLB.pNew) );
155564 whereLoopInit(p: sWLB.pNew);
155565#ifdef SQLITE_DEBUG
155566 sWLB.pNew->cId = '*';
155567#endif
155568
155569 /* Split the WHERE clause into separate subexpressions where each
155570 ** subexpression is separated by an AND operator.
155571 */
155572 initMaskSet(pMaskSet);
155573 sqlite3WhereClauseInit(pWC: &pWInfo->sWC, pWInfo);
155574 sqlite3WhereSplit(pWC: &pWInfo->sWC, pExpr: pWhere, TK_AND);
155575
155576 /* Special case: No FROM clause
155577 */
155578 if( nTabList==0 ){
155579 if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr;
155580 if( wctrlFlags & WHERE_WANT_DISTINCT ){
155581 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
155582 }
155583 ExplainQueryPlan((pParse, 0, "SCAN CONSTANT ROW"));
155584 }else{
155585 /* Assign a bit from the bitmask to every term in the FROM clause.
155586 **
155587 ** The N-th term of the FROM clause is assigned a bitmask of 1<<N.
155588 **
155589 ** The rule of the previous sentence ensures thta if X is the bitmask for
155590 ** a table T, then X-1 is the bitmask for all other tables to the left of T.
155591 ** Knowing the bitmask for all tables to the left of a left join is
155592 ** important. Ticket #3015.
155593 **
155594 ** Note that bitmasks are created for all pTabList->nSrc tables in
155595 ** pTabList, not just the first nTabList tables. nTabList is normally
155596 ** equal to pTabList->nSrc but might be shortened to 1 if the
155597 ** WHERE_OR_SUBCLAUSE flag is set.
155598 */
155599 ii = 0;
155600 do{
155601 createMask(pMaskSet, iCursor: pTabList->a[ii].iCursor);
155602 sqlite3WhereTabFuncArgs(pParse, pItem: &pTabList->a[ii], pWC: &pWInfo->sWC);
155603 }while( (++ii)<pTabList->nSrc );
155604 #ifdef SQLITE_DEBUG
155605 {
155606 Bitmask mx = 0;
155607 for(ii=0; ii<pTabList->nSrc; ii++){
155608 Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor);
155609 assert( m>=mx );
155610 mx = m;
155611 }
155612 }
155613 #endif
155614 }
155615
155616 /* Analyze all of the subexpressions. */
155617 sqlite3WhereExprAnalyze(pTabList, pWC: &pWInfo->sWC);
155618 if( db->mallocFailed ) goto whereBeginError;
155619
155620 /* Special case: WHERE terms that do not refer to any tables in the join
155621 ** (constant expressions). Evaluate each such term, and jump over all the
155622 ** generated code if the result is not true.
155623 **
155624 ** Do not do this if the expression contains non-deterministic functions
155625 ** that are not within a sub-select. This is not strictly required, but
155626 ** preserves SQLite's legacy behaviour in the following two cases:
155627 **
155628 ** FROM ... WHERE random()>0; -- eval random() once per row
155629 ** FROM ... WHERE (SELECT random())>0; -- eval random() once overall
155630 */
155631 for(ii=0; ii<sWLB.pWC->nTerm; ii++){
155632 WhereTerm *pT = &sWLB.pWC->a[ii];
155633 if( pT->wtFlags & TERM_VIRTUAL ) continue;
155634 if( pT->prereqAll==0 && (nTabList==0 || exprIsDeterministic(p: pT->pExpr)) ){
155635 sqlite3ExprIfFalse(pParse, pExpr: pT->pExpr, dest: pWInfo->iBreak, SQLITE_JUMPIFNULL);
155636 pT->wtFlags |= TERM_CODED;
155637 }
155638 }
155639
155640 if( wctrlFlags & WHERE_WANT_DISTINCT ){
155641 if( isDistinctRedundant(pParse, pTabList, pWC: &pWInfo->sWC, pDistinct: pResultSet) ){
155642 /* The DISTINCT marking is pointless. Ignore it. */
155643 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
155644 }else if( pOrderBy==0 ){
155645 /* Try to ORDER BY the result set to make distinct processing easier */
155646 pWInfo->wctrlFlags |= WHERE_DISTINCTBY;
155647 pWInfo->pOrderBy = pResultSet;
155648 }
155649 }
155650
155651 /* Construct the WhereLoop objects */
155652#if defined(WHERETRACE_ENABLED)
155653 if( sqlite3WhereTrace & 0xffff ){
155654 sqlite3DebugPrintf("*** Optimizer Start *** (wctrlFlags: 0x%x",wctrlFlags);
155655 if( wctrlFlags & WHERE_USE_LIMIT ){
155656 sqlite3DebugPrintf(", limit: %d", iAuxArg);
155657 }
155658 sqlite3DebugPrintf(")\n");
155659 if( sqlite3WhereTrace & 0x100 ){
155660 Select sSelect;
155661 memset(&sSelect, 0, sizeof(sSelect));
155662 sSelect.selFlags = SF_WhereBegin;
155663 sSelect.pSrc = pTabList;
155664 sSelect.pWhere = pWhere;
155665 sSelect.pOrderBy = pOrderBy;
155666 sSelect.pEList = pResultSet;
155667 sqlite3TreeViewSelect(0, &sSelect, 0);
155668 }
155669 }
155670 if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */
155671 sqlite3DebugPrintf("---- WHERE clause at start of analysis:\n");
155672 sqlite3WhereClausePrint(sWLB.pWC);
155673 }
155674#endif
155675
155676 if( nTabList!=1 || whereShortCut(pBuilder: &sWLB)==0 ){
155677 rc = whereLoopAddAll(pBuilder: &sWLB);
155678 if( rc ) goto whereBeginError;
155679
155680#ifdef SQLITE_ENABLE_STAT4
155681 /* If one or more WhereTerm.truthProb values were used in estimating
155682 ** loop parameters, but then those truthProb values were subsequently
155683 ** changed based on STAT4 information while computing subsequent loops,
155684 ** then we need to rerun the whole loop building process so that all
155685 ** loops will be built using the revised truthProb values. */
155686 if( sWLB.bldFlags2 & SQLITE_BLDF2_2NDPASS ){
155687 WHERETRACE_ALL_LOOPS(pWInfo, sWLB.pWC);
155688 WHERETRACE(0xffff,
155689 ("**** Redo all loop computations due to"
155690 " TERM_HIGHTRUTH changes ****\n"));
155691 while( pWInfo->pLoops ){
155692 WhereLoop *p = pWInfo->pLoops;
155693 pWInfo->pLoops = p->pNextLoop;
155694 whereLoopDelete(db, p);
155695 }
155696 rc = whereLoopAddAll(&sWLB);
155697 if( rc ) goto whereBeginError;
155698 }
155699#endif
155700 WHERETRACE_ALL_LOOPS(pWInfo, sWLB.pWC);
155701
155702 wherePathSolver(pWInfo, nRowEst: 0);
155703 if( db->mallocFailed ) goto whereBeginError;
155704 if( pWInfo->pOrderBy ){
155705 wherePathSolver(pWInfo, nRowEst: pWInfo->nRowOut+1);
155706 if( db->mallocFailed ) goto whereBeginError;
155707 }
155708 }
155709 if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){
155710 pWInfo->revMask = ALLBITS;
155711 }
155712 if( pParse->nErr || db->mallocFailed ){
155713 goto whereBeginError;
155714 }
155715#ifdef WHERETRACE_ENABLED
155716 if( sqlite3WhereTrace ){
155717 sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
155718 if( pWInfo->nOBSat>0 ){
155719 sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask);
155720 }
155721 switch( pWInfo->eDistinct ){
155722 case WHERE_DISTINCT_UNIQUE: {
155723 sqlite3DebugPrintf(" DISTINCT=unique");
155724 break;
155725 }
155726 case WHERE_DISTINCT_ORDERED: {
155727 sqlite3DebugPrintf(" DISTINCT=ordered");
155728 break;
155729 }
155730 case WHERE_DISTINCT_UNORDERED: {
155731 sqlite3DebugPrintf(" DISTINCT=unordered");
155732 break;
155733 }
155734 }
155735 sqlite3DebugPrintf("\n");
155736 for(ii=0; ii<pWInfo->nLevel; ii++){
155737 sqlite3WhereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC);
155738 }
155739 }
155740#endif
155741
155742 /* Attempt to omit tables from the join that do not affect the result.
155743 ** For a table to not affect the result, the following must be true:
155744 **
155745 ** 1) The query must not be an aggregate.
155746 ** 2) The table must be the RHS of a LEFT JOIN.
155747 ** 3) Either the query must be DISTINCT, or else the ON or USING clause
155748 ** must contain a constraint that limits the scan of the table to
155749 ** at most a single row.
155750 ** 4) The table must not be referenced by any part of the query apart
155751 ** from its own USING or ON clause.
155752 **
155753 ** For example, given:
155754 **
155755 ** CREATE TABLE t1(ipk INTEGER PRIMARY KEY, v1);
155756 ** CREATE TABLE t2(ipk INTEGER PRIMARY KEY, v2);
155757 ** CREATE TABLE t3(ipk INTEGER PRIMARY KEY, v3);
155758 **
155759 ** then table t2 can be omitted from the following:
155760 **
155761 ** SELECT v1, v3 FROM t1
155762 ** LEFT JOIN t2 ON (t1.ipk=t2.ipk)
155763 ** LEFT JOIN t3 ON (t1.ipk=t3.ipk)
155764 **
155765 ** or from:
155766 **
155767 ** SELECT DISTINCT v1, v3 FROM t1
155768 ** LEFT JOIN t2
155769 ** LEFT JOIN t3 ON (t1.ipk=t3.ipk)
155770 */
155771 notReady = ~(Bitmask)0;
155772 if( pWInfo->nLevel>=2
155773 && pResultSet!=0 /* these two combine to guarantee */
155774 && 0==(wctrlFlags & WHERE_AGG_DISTINCT) /* condition (1) above */
155775 && OptimizationEnabled(db, SQLITE_OmitNoopJoin)
155776 ){
155777 int i;
155778 Bitmask tabUsed = sqlite3WhereExprListUsage(pMaskSet, pList: pResultSet);
155779 if( sWLB.pOrderBy ){
155780 tabUsed |= sqlite3WhereExprListUsage(pMaskSet, pList: sWLB.pOrderBy);
155781 }
155782 for(i=pWInfo->nLevel-1; i>=1; i--){
155783 WhereTerm *pTerm, *pEnd;
155784 SrcItem *pItem;
155785 pLoop = pWInfo->a[i].pWLoop;
155786 pItem = &pWInfo->pTabList->a[pLoop->iTab];
155787 if( (pItem->fg.jointype & JT_LEFT)==0 ) continue;
155788 if( (wctrlFlags & WHERE_WANT_DISTINCT)==0
155789 && (pLoop->wsFlags & WHERE_ONEROW)==0
155790 ){
155791 continue;
155792 }
155793 if( (tabUsed & pLoop->maskSelf)!=0 ) continue;
155794 pEnd = sWLB.pWC->a + sWLB.pWC->nTerm;
155795 for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
155796 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
155797 if( !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
155798 || pTerm->pExpr->iRightJoinTable!=pItem->iCursor
155799 ){
155800 break;
155801 }
155802 }
155803 }
155804 if( pTerm<pEnd ) continue;
155805 WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId));
155806 notReady &= ~pLoop->maskSelf;
155807 for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
155808 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
155809 pTerm->wtFlags |= TERM_CODED;
155810 }
155811 }
155812 if( i!=pWInfo->nLevel-1 ){
155813 int nByte = (pWInfo->nLevel-1-i) * sizeof(WhereLevel);
155814 memmove(dest: &pWInfo->a[i], src: &pWInfo->a[i+1], n: nByte);
155815 }
155816 pWInfo->nLevel--;
155817 nTabList--;
155818 }
155819 }
155820#if defined(WHERETRACE_ENABLED)
155821 if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */
155822 sqlite3DebugPrintf("---- WHERE clause at end of analysis:\n");
155823 sqlite3WhereClausePrint(sWLB.pWC);
155824 }
155825 WHERETRACE(0xffff,("*** Optimizer Finished ***\n"));
155826#endif
155827 pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
155828
155829 /* If the caller is an UPDATE or DELETE statement that is requesting
155830 ** to use a one-pass algorithm, determine if this is appropriate.
155831 **
155832 ** A one-pass approach can be used if the caller has requested one
155833 ** and either (a) the scan visits at most one row or (b) each
155834 ** of the following are true:
155835 **
155836 ** * the caller has indicated that a one-pass approach can be used
155837 ** with multiple rows (by setting WHERE_ONEPASS_MULTIROW), and
155838 ** * the table is not a virtual table, and
155839 ** * either the scan does not use the OR optimization or the caller
155840 ** is a DELETE operation (WHERE_DUPLICATES_OK is only specified
155841 ** for DELETE).
155842 **
155843 ** The last qualification is because an UPDATE statement uses
155844 ** WhereInfo.aiCurOnePass[1] to determine whether or not it really can
155845 ** use a one-pass approach, and this is not set accurately for scans
155846 ** that use the OR optimization.
155847 */
155848 assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
155849 if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 ){
155850 int wsFlags = pWInfo->a[0].pWLoop->wsFlags;
155851 int bOnerow = (wsFlags & WHERE_ONEROW)!=0;
155852 assert( !(wsFlags & WHERE_VIRTUALTABLE) || IsVirtual(pTabList->a[0].pTab) );
155853 if( bOnerow || (
155854 0!=(wctrlFlags & WHERE_ONEPASS_MULTIROW)
155855 && !IsVirtual(pTabList->a[0].pTab)
155856 && (0==(wsFlags & WHERE_MULTI_OR) || (wctrlFlags & WHERE_DUPLICATES_OK))
155857 )){
155858 pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE : ONEPASS_MULTI;
155859 if( HasRowid(pTabList->a[0].pTab) && (wsFlags & WHERE_IDX_ONLY) ){
155860 if( wctrlFlags & WHERE_ONEPASS_MULTIROW ){
155861 bFordelete = OPFLAG_FORDELETE;
155862 }
155863 pWInfo->a[0].pWLoop->wsFlags = (wsFlags & ~WHERE_IDX_ONLY);
155864 }
155865 }
155866 }
155867
155868 /* Open all tables in the pTabList and any indices selected for
155869 ** searching those tables.
155870 */
155871 for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
155872 Table *pTab; /* Table to open */
155873 int iDb; /* Index of database containing table/index */
155874 SrcItem *pTabItem;
155875
155876 pTabItem = &pTabList->a[pLevel->iFrom];
155877 pTab = pTabItem->pTab;
155878 iDb = sqlite3SchemaToIndex(db, pSchema: pTab->pSchema);
155879 pLoop = pLevel->pWLoop;
155880 if( (pTab->tabFlags & TF_Ephemeral)!=0 || IsView(pTab) ){
155881 /* Do nothing */
155882 }else
155883#ifndef SQLITE_OMIT_VIRTUALTABLE
155884 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
155885 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
155886 int iCur = pTabItem->iCursor;
155887 sqlite3VdbeAddOp4(p: v, OP_VOpen, p1: iCur, p2: 0, p3: 0, zP4: pVTab, P4_VTAB);
155888 }else if( IsVirtual(pTab) ){
155889 /* noop */
155890 }else
155891#endif
155892 if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
155893 && (wctrlFlags & WHERE_OR_SUBCLAUSE)==0 ){
155894 int op = OP_OpenRead;
155895 if( pWInfo->eOnePass!=ONEPASS_OFF ){
155896 op = OP_OpenWrite;
155897 pWInfo->aiCurOnePass[0] = pTabItem->iCursor;
155898 };
155899 sqlite3OpenTable(pParse, iCur: pTabItem->iCursor, iDb, pTab, opcode: op);
155900 assert( pTabItem->iCursor==pLevel->iTabCur );
155901 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS-1 );
155902 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS );
155903 if( pWInfo->eOnePass==ONEPASS_OFF
155904 && pTab->nCol<BMS
155905 && (pTab->tabFlags & (TF_HasGenerated|TF_WithoutRowid))==0
155906 ){
155907 /* If we know that only a prefix of the record will be used,
155908 ** it is advantageous to reduce the "column count" field in
155909 ** the P4 operand of the OP_OpenRead/Write opcode. */
155910 Bitmask b = pTabItem->colUsed;
155911 int n = 0;
155912 for(; b; b=b>>1, n++){}
155913 sqlite3VdbeChangeP4(p: v, addr: -1, SQLITE_INT_TO_PTR(n), P4_INT32);
155914 assert( n<=pTab->nCol );
155915 }
155916#ifdef SQLITE_ENABLE_CURSOR_HINTS
155917 if( pLoop->u.btree.pIndex!=0 ){
155918 sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ|bFordelete);
155919 }else
155920#endif
155921 {
155922 sqlite3VdbeChangeP5(p: v, p5: bFordelete);
155923 }
155924#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
155925 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, pTabItem->iCursor, 0, 0,
155926 (const u8*)&pTabItem->colUsed, P4_INT64);
155927#endif
155928 }else{
155929 sqlite3TableLock(pParse, iDb, iTab: pTab->tnum, isWriteLock: 0, zName: pTab->zName);
155930 }
155931 if( pLoop->wsFlags & WHERE_INDEXED ){
155932 Index *pIx = pLoop->u.btree.pIndex;
155933 int iIndexCur;
155934 int op = OP_OpenRead;
155935 /* iAuxArg is always set to a positive value if ONEPASS is possible */
155936 assert( iAuxArg!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 );
155937 if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIx)
155938 && (wctrlFlags & WHERE_OR_SUBCLAUSE)!=0
155939 ){
155940 /* This is one term of an OR-optimization using the PRIMARY KEY of a
155941 ** WITHOUT ROWID table. No need for a separate index */
155942 iIndexCur = pLevel->iTabCur;
155943 op = 0;
155944 }else if( pWInfo->eOnePass!=ONEPASS_OFF ){
155945 Index *pJ = pTabItem->pTab->pIndex;
155946 iIndexCur = iAuxArg;
155947 assert( wctrlFlags & WHERE_ONEPASS_DESIRED );
155948 while( ALWAYS(pJ) && pJ!=pIx ){
155949 iIndexCur++;
155950 pJ = pJ->pNext;
155951 }
155952 op = OP_OpenWrite;
155953 pWInfo->aiCurOnePass[1] = iIndexCur;
155954 }else if( iAuxArg && (wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 ){
155955 iIndexCur = iAuxArg;
155956 op = OP_ReopenIdx;
155957 }else{
155958 iIndexCur = pParse->nTab++;
155959 }
155960 pLevel->iIdxCur = iIndexCur;
155961 assert( pIx->pSchema==pTab->pSchema );
155962 assert( iIndexCur>=0 );
155963 if( op ){
155964 sqlite3VdbeAddOp3(p: v, op, p1: iIndexCur, p2: pIx->tnum, p3: iDb);
155965 sqlite3VdbeSetP4KeyInfo(pParse, pIdx: pIx);
155966 if( (pLoop->wsFlags & WHERE_CONSTRAINT)!=0
155967 && (pLoop->wsFlags & (WHERE_COLUMN_RANGE|WHERE_SKIPSCAN))==0
155968 && (pLoop->wsFlags & WHERE_BIGNULL_SORT)==0
155969 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0
155970 && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0
155971 && pWInfo->eDistinct!=WHERE_DISTINCT_ORDERED
155972 ){
155973 sqlite3VdbeChangeP5(p: v, OPFLAG_SEEKEQ);
155974 }
155975 VdbeComment((v, "%s", pIx->zName));
155976#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
155977 {
155978 u64 colUsed = 0;
155979 int ii, jj;
155980 for(ii=0; ii<pIx->nColumn; ii++){
155981 jj = pIx->aiColumn[ii];
155982 if( jj<0 ) continue;
155983 if( jj>63 ) jj = 63;
155984 if( (pTabItem->colUsed & MASKBIT(jj))==0 ) continue;
155985 colUsed |= ((u64)1)<<(ii<63 ? ii : 63);
155986 }
155987 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, iIndexCur, 0, 0,
155988 (u8*)&colUsed, P4_INT64);
155989 }
155990#endif /* SQLITE_ENABLE_COLUMN_USED_MASK */
155991 }
155992 }
155993 if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb);
155994 }
155995 pWInfo->iTop = sqlite3VdbeCurrentAddr(p: v);
155996 if( db->mallocFailed ) goto whereBeginError;
155997
155998 /* Generate the code to do the search. Each iteration of the for
155999 ** loop below generates code for a single nested loop of the VM
156000 ** program.
156001 */
156002 for(ii=0; ii<nTabList; ii++){
156003 int addrExplain;
156004 int wsFlags;
156005 if( pParse->nErr ) goto whereBeginError;
156006 pLevel = &pWInfo->a[ii];
156007 wsFlags = pLevel->pWLoop->wsFlags;
156008#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
156009 if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){
156010 constructAutomaticIndex(pParse, pWC: &pWInfo->sWC,
156011 pSrc: &pTabList->a[pLevel->iFrom], notReady, pLevel);
156012 if( db->mallocFailed ) goto whereBeginError;
156013 }
156014#endif
156015 addrExplain = sqlite3WhereExplainOneScan(
156016 pParse, pTabList, pLevel, wctrlFlags
156017 );
156018 pLevel->addrBody = sqlite3VdbeCurrentAddr(p: v);
156019 notReady = sqlite3WhereCodeOneLoopStart(pParse,v,pWInfo,iLevel: ii,pLevel,notReady);
156020 pWInfo->iContinue = pLevel->addrCont;
156021 if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_OR_SUBCLAUSE)==0 ){
156022 sqlite3WhereAddScanStatus(v, pTabList, pLevel, addrExplain);
156023 }
156024 }
156025
156026 /* Done. */
156027 VdbeModuleComment((v, "Begin WHERE-core"));
156028 pWInfo->iEndWhere = sqlite3VdbeCurrentAddr(p: v);
156029 return pWInfo;
156030
156031 /* Jump here if malloc fails */
156032whereBeginError:
156033 if( pWInfo ){
156034 testcase( pWInfo->pExprMods!=0 );
156035 whereUndoExprMods(pWInfo);
156036 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
156037 whereInfoFree(db, pWInfo);
156038 }
156039 return 0;
156040}
156041
156042/*
156043** Part of sqlite3WhereEnd() will rewrite opcodes to reference the
156044** index rather than the main table. In SQLITE_DEBUG mode, we want
156045** to trace those changes if PRAGMA vdbe_addoptrace=on. This routine
156046** does that.
156047*/
156048#ifndef SQLITE_DEBUG
156049# define OpcodeRewriteTrace(D,K,P) /* no-op */
156050#else
156051# define OpcodeRewriteTrace(D,K,P) sqlite3WhereOpcodeRewriteTrace(D,K,P)
156052 static void sqlite3WhereOpcodeRewriteTrace(
156053 sqlite3 *db,
156054 int pc,
156055 VdbeOp *pOp
156056 ){
156057 if( (db->flags & SQLITE_VdbeAddopTrace)==0 ) return;
156058 sqlite3VdbePrintOp(0, pc, pOp);
156059 }
156060#endif
156061
156062/*
156063** Generate the end of the WHERE loop. See comments on
156064** sqlite3WhereBegin() for additional information.
156065*/
156066SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
156067 Parse *pParse = pWInfo->pParse;
156068 Vdbe *v = pParse->pVdbe;
156069 int i;
156070 WhereLevel *pLevel;
156071 WhereLoop *pLoop;
156072 SrcList *pTabList = pWInfo->pTabList;
156073 sqlite3 *db = pParse->db;
156074 int iEnd = sqlite3VdbeCurrentAddr(p: v);
156075
156076 /* Generate loop termination code.
156077 */
156078 VdbeModuleComment((v, "End WHERE-core"));
156079 for(i=pWInfo->nLevel-1; i>=0; i--){
156080 int addr;
156081 pLevel = &pWInfo->a[i];
156082 pLoop = pLevel->pWLoop;
156083 if( pLevel->op!=OP_Noop ){
156084#ifndef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
156085 int addrSeek = 0;
156086 Index *pIdx;
156087 int n;
156088 if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED
156089 && i==pWInfo->nLevel-1 /* Ticket [ef9318757b152e3] 2017-10-21 */
156090 && (pLoop->wsFlags & WHERE_INDEXED)!=0
156091 && (pIdx = pLoop->u.btree.pIndex)->hasStat1
156092 && (n = pLoop->u.btree.nDistinctCol)>0
156093 && pIdx->aiRowLogEst[n]>=36
156094 ){
156095 int r1 = pParse->nMem+1;
156096 int j, op;
156097 for(j=0; j<n; j++){
156098 sqlite3VdbeAddOp3(p: v, OP_Column, p1: pLevel->iIdxCur, p2: j, p3: r1+j);
156099 }
156100 pParse->nMem += n+1;
156101 op = pLevel->op==OP_Prev ? OP_SeekLT : OP_SeekGT;
156102 addrSeek = sqlite3VdbeAddOp4Int(p: v, op, p1: pLevel->iIdxCur, p2: 0, p3: r1, p4: n);
156103 VdbeCoverageIf(v, op==OP_SeekLT);
156104 VdbeCoverageIf(v, op==OP_SeekGT);
156105 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 1, p2: pLevel->p2);
156106 }
156107#endif /* SQLITE_DISABLE_SKIPAHEAD_DISTINCT */
156108 /* The common case: Advance to the next row */
156109 sqlite3VdbeResolveLabel(v, x: pLevel->addrCont);
156110 sqlite3VdbeAddOp3(p: v, op: pLevel->op, p1: pLevel->p1, p2: pLevel->p2, p3: pLevel->p3);
156111 sqlite3VdbeChangeP5(p: v, p5: pLevel->p5);
156112 VdbeCoverage(v);
156113 VdbeCoverageIf(v, pLevel->op==OP_Next);
156114 VdbeCoverageIf(v, pLevel->op==OP_Prev);
156115 VdbeCoverageIf(v, pLevel->op==OP_VNext);
156116 if( pLevel->regBignull ){
156117 sqlite3VdbeResolveLabel(v, x: pLevel->addrBignull);
156118 sqlite3VdbeAddOp2(p: v, OP_DecrJumpZero, p1: pLevel->regBignull, p2: pLevel->p2-1);
156119 VdbeCoverage(v);
156120 }
156121#ifndef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
156122 if( addrSeek ) sqlite3VdbeJumpHere(p: v, addr: addrSeek);
156123#endif
156124 }else{
156125 sqlite3VdbeResolveLabel(v, x: pLevel->addrCont);
156126 }
156127 if( (pLoop->wsFlags & WHERE_IN_ABLE)!=0 && pLevel->u.in.nIn>0 ){
156128 struct InLoop *pIn;
156129 int j;
156130 sqlite3VdbeResolveLabel(v, x: pLevel->addrNxt);
156131 for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
156132 assert( sqlite3VdbeGetOp(v, pIn->addrInTop+1)->opcode==OP_IsNull
156133 || pParse->db->mallocFailed );
156134 sqlite3VdbeJumpHere(p: v, addr: pIn->addrInTop+1);
156135 if( pIn->eEndLoopOp!=OP_Noop ){
156136 if( pIn->nPrefix ){
156137 int bEarlyOut =
156138 (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
156139 && (pLoop->wsFlags & WHERE_IN_EARLYOUT)!=0;
156140 if( pLevel->iLeftJoin ){
156141 /* For LEFT JOIN queries, cursor pIn->iCur may not have been
156142 ** opened yet. This occurs for WHERE clauses such as
156143 ** "a = ? AND b IN (...)", where the index is on (a, b). If
156144 ** the RHS of the (a=?) is NULL, then the "b IN (...)" may
156145 ** never have been coded, but the body of the loop run to
156146 ** return the null-row. So, if the cursor is not open yet,
156147 ** jump over the OP_Next or OP_Prev instruction about to
156148 ** be coded. */
156149 sqlite3VdbeAddOp2(p: v, OP_IfNotOpen, p1: pIn->iCur,
156150 p2: sqlite3VdbeCurrentAddr(p: v) + 2 + bEarlyOut);
156151 VdbeCoverage(v);
156152 }
156153 if( bEarlyOut ){
156154 sqlite3VdbeAddOp4Int(p: v, OP_IfNoHope, p1: pLevel->iIdxCur,
156155 p2: sqlite3VdbeCurrentAddr(p: v)+2,
156156 p3: pIn->iBase, p4: pIn->nPrefix);
156157 VdbeCoverage(v);
156158 /* Retarget the OP_IsNull against the left operand of IN so
156159 ** it jumps past the OP_IfNoHope. This is because the
156160 ** OP_IsNull also bypasses the OP_Affinity opcode that is
156161 ** required by OP_IfNoHope. */
156162 sqlite3VdbeJumpHere(p: v, addr: pIn->addrInTop+1);
156163 }
156164 }
156165 sqlite3VdbeAddOp2(p: v, op: pIn->eEndLoopOp, p1: pIn->iCur, p2: pIn->addrInTop);
156166 VdbeCoverage(v);
156167 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Prev);
156168 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Next);
156169 }
156170 sqlite3VdbeJumpHere(p: v, addr: pIn->addrInTop-1);
156171 }
156172 }
156173 sqlite3VdbeResolveLabel(v, x: pLevel->addrBrk);
156174 if( pLevel->addrSkip ){
156175 sqlite3VdbeGoto(p: v, iDest: pLevel->addrSkip);
156176 VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName));
156177 sqlite3VdbeJumpHere(p: v, addr: pLevel->addrSkip);
156178 sqlite3VdbeJumpHere(p: v, addr: pLevel->addrSkip-2);
156179 }
156180#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
156181 if( pLevel->addrLikeRep ){
156182 sqlite3VdbeAddOp2(p: v, OP_DecrJumpZero, p1: (int)(pLevel->iLikeRepCntr>>1),
156183 p2: pLevel->addrLikeRep);
156184 VdbeCoverage(v);
156185 }
156186#endif
156187 if( pLevel->iLeftJoin ){
156188 int ws = pLoop->wsFlags;
156189 addr = sqlite3VdbeAddOp1(p: v, OP_IfPos, p1: pLevel->iLeftJoin); VdbeCoverage(v);
156190 assert( (ws & WHERE_IDX_ONLY)==0 || (ws & WHERE_INDEXED)!=0 );
156191 if( (ws & WHERE_IDX_ONLY)==0 ){
156192 assert( pLevel->iTabCur==pTabList->a[pLevel->iFrom].iCursor );
156193 sqlite3VdbeAddOp1(p: v, OP_NullRow, p1: pLevel->iTabCur);
156194 }
156195 if( (ws & WHERE_INDEXED)
156196 || ((ws & WHERE_MULTI_OR) && pLevel->u.pCoveringIdx)
156197 ){
156198 if( ws & WHERE_MULTI_OR ){
156199 Index *pIx = pLevel->u.pCoveringIdx;
156200 int iDb = sqlite3SchemaToIndex(db, pSchema: pIx->pSchema);
156201 sqlite3VdbeAddOp3(p: v, OP_ReopenIdx, p1: pLevel->iIdxCur, p2: pIx->tnum, p3: iDb);
156202 sqlite3VdbeSetP4KeyInfo(pParse, pIdx: pIx);
156203 }
156204 sqlite3VdbeAddOp1(p: v, OP_NullRow, p1: pLevel->iIdxCur);
156205 }
156206 if( pLevel->op==OP_Return ){
156207 sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: pLevel->p1, p2: pLevel->addrFirst);
156208 }else{
156209 sqlite3VdbeGoto(p: v, iDest: pLevel->addrFirst);
156210 }
156211 sqlite3VdbeJumpHere(p: v, addr);
156212 }
156213 VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
156214 pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
156215 }
156216
156217 /* The "break" point is here, just past the end of the outer loop.
156218 ** Set it.
156219 */
156220 sqlite3VdbeResolveLabel(v, x: pWInfo->iBreak);
156221
156222 assert( pWInfo->nLevel<=pTabList->nSrc );
156223 for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
156224 int k, last;
156225 VdbeOp *pOp, *pLastOp;
156226 Index *pIdx = 0;
156227 SrcItem *pTabItem = &pTabList->a[pLevel->iFrom];
156228 Table *pTab = pTabItem->pTab;
156229 assert( pTab!=0 );
156230 pLoop = pLevel->pWLoop;
156231
156232 /* For a co-routine, change all OP_Column references to the table of
156233 ** the co-routine into OP_Copy of result contained in a register.
156234 ** OP_Rowid becomes OP_Null.
156235 */
156236 if( pTabItem->fg.viaCoroutine ){
156237 testcase( pParse->db->mallocFailed );
156238 translateColumnToCopy(pParse, iStart: pLevel->addrBody, iTabCur: pLevel->iTabCur,
156239 iRegister: pTabItem->regResult, iAutoidxCur: 0);
156240 continue;
156241 }
156242
156243#ifdef SQLITE_ENABLE_EARLY_CURSOR_CLOSE
156244 /* Close all of the cursors that were opened by sqlite3WhereBegin.
156245 ** Except, do not close cursors that will be reused by the OR optimization
156246 ** (WHERE_OR_SUBCLAUSE). And do not close the OP_OpenWrite cursors
156247 ** created for the ONEPASS optimization.
156248 */
156249 if( (pTab->tabFlags & TF_Ephemeral)==0
156250 && !IsView(pTab)
156251 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0
156252 ){
156253 int ws = pLoop->wsFlags;
156254 if( pWInfo->eOnePass==ONEPASS_OFF && (ws & WHERE_IDX_ONLY)==0 ){
156255 sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor);
156256 }
156257 if( (ws & WHERE_INDEXED)!=0
156258 && (ws & (WHERE_IPK|WHERE_AUTO_INDEX))==0
156259 && pLevel->iIdxCur!=pWInfo->aiCurOnePass[1]
156260 ){
156261 sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur);
156262 }
156263 }
156264#endif
156265
156266 /* If this scan uses an index, make VDBE code substitutions to read data
156267 ** from the index instead of from the table where possible. In some cases
156268 ** this optimization prevents the table from ever being read, which can
156269 ** yield a significant performance boost.
156270 **
156271 ** Calls to the code generator in between sqlite3WhereBegin and
156272 ** sqlite3WhereEnd will have created code that references the table
156273 ** directly. This loop scans all that code looking for opcodes
156274 ** that reference the table and converts them into opcodes that
156275 ** reference the index.
156276 */
156277 if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){
156278 pIdx = pLoop->u.btree.pIndex;
156279 }else if( pLoop->wsFlags & WHERE_MULTI_OR ){
156280 pIdx = pLevel->u.pCoveringIdx;
156281 }
156282 if( pIdx
156283 && !db->mallocFailed
156284 ){
156285 if( pWInfo->eOnePass==ONEPASS_OFF || !HasRowid(pIdx->pTable) ){
156286 last = iEnd;
156287 }else{
156288 last = pWInfo->iEndWhere;
156289 }
156290 k = pLevel->addrBody + 1;
156291#ifdef SQLITE_DEBUG
156292 if( db->flags & SQLITE_VdbeAddopTrace ){
156293 printf("TRANSLATE opcodes in range %d..%d\n", k, last-1);
156294 }
156295 /* Proof that the "+1" on the k value above is safe */
156296 pOp = sqlite3VdbeGetOp(v, k - 1);
156297 assert( pOp->opcode!=OP_Column || pOp->p1!=pLevel->iTabCur );
156298 assert( pOp->opcode!=OP_Rowid || pOp->p1!=pLevel->iTabCur );
156299 assert( pOp->opcode!=OP_IfNullRow || pOp->p1!=pLevel->iTabCur );
156300#endif
156301 pOp = sqlite3VdbeGetOp(p: v, addr: k);
156302 pLastOp = pOp + (last - k);
156303 assert( pOp<=pLastOp );
156304 do{
156305 if( pOp->p1!=pLevel->iTabCur ){
156306 /* no-op */
156307 }else if( pOp->opcode==OP_Column
156308#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
156309 || pOp->opcode==OP_Offset
156310#endif
156311 ){
156312 int x = pOp->p2;
156313 assert( pIdx->pTable==pTab );
156314 if( !HasRowid(pTab) ){
156315 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
156316 x = pPk->aiColumn[x];
156317 assert( x>=0 );
156318 }else{
156319 testcase( x!=sqlite3StorageColumnToTable(pTab,x) );
156320 x = sqlite3StorageColumnToTable(pTab,iCol: x);
156321 }
156322 x = sqlite3TableColumnToIndex(pIdx, iCol: x);
156323 if( x>=0 ){
156324 pOp->p2 = x;
156325 pOp->p1 = pLevel->iIdxCur;
156326 OpcodeRewriteTrace(db, k, pOp);
156327 }
156328 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0
156329 || pWInfo->eOnePass );
156330 }else if( pOp->opcode==OP_Rowid ){
156331 pOp->p1 = pLevel->iIdxCur;
156332 pOp->opcode = OP_IdxRowid;
156333 OpcodeRewriteTrace(db, k, pOp);
156334 }else if( pOp->opcode==OP_IfNullRow ){
156335 pOp->p1 = pLevel->iIdxCur;
156336 OpcodeRewriteTrace(db, k, pOp);
156337 }
156338#ifdef SQLITE_DEBUG
156339 k++;
156340#endif
156341 }while( (++pOp)<pLastOp );
156342#ifdef SQLITE_DEBUG
156343 if( db->flags & SQLITE_VdbeAddopTrace ) printf("TRANSLATE complete\n");
156344#endif
156345 }
156346 }
156347
156348 /* Final cleanup
156349 */
156350 if( pWInfo->pExprMods ) whereUndoExprMods(pWInfo);
156351 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
156352 whereInfoFree(db, pWInfo);
156353 return;
156354}
156355
156356/************** End of where.c ***********************************************/
156357/************** Begin file window.c ******************************************/
156358/*
156359** 2018 May 08
156360**
156361** The author disclaims copyright to this source code. In place of
156362** a legal notice, here is a blessing:
156363**
156364** May you do good and not evil.
156365** May you find forgiveness for yourself and forgive others.
156366** May you share freely, never taking more than you give.
156367**
156368*************************************************************************
156369*/
156370/* #include "sqliteInt.h" */
156371
156372#ifndef SQLITE_OMIT_WINDOWFUNC
156373
156374/*
156375** SELECT REWRITING
156376**
156377** Any SELECT statement that contains one or more window functions in
156378** either the select list or ORDER BY clause (the only two places window
156379** functions may be used) is transformed by function sqlite3WindowRewrite()
156380** in order to support window function processing. For example, with the
156381** schema:
156382**
156383** CREATE TABLE t1(a, b, c, d, e, f, g);
156384**
156385** the statement:
156386**
156387** SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM t1 ORDER BY e;
156388**
156389** is transformed to:
156390**
156391** SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM (
156392** SELECT a, e, c, d, b FROM t1 ORDER BY c, d
156393** ) ORDER BY e;
156394**
156395** The flattening optimization is disabled when processing this transformed
156396** SELECT statement. This allows the implementation of the window function
156397** (in this case max()) to process rows sorted in order of (c, d), which
156398** makes things easier for obvious reasons. More generally:
156399**
156400** * FROM, WHERE, GROUP BY and HAVING clauses are all moved to
156401** the sub-query.
156402**
156403** * ORDER BY, LIMIT and OFFSET remain part of the parent query.
156404**
156405** * Terminals from each of the expression trees that make up the
156406** select-list and ORDER BY expressions in the parent query are
156407** selected by the sub-query. For the purposes of the transformation,
156408** terminals are column references and aggregate functions.
156409**
156410** If there is more than one window function in the SELECT that uses
156411** the same window declaration (the OVER bit), then a single scan may
156412** be used to process more than one window function. For example:
156413**
156414** SELECT max(b) OVER (PARTITION BY c ORDER BY d),
156415** min(e) OVER (PARTITION BY c ORDER BY d)
156416** FROM t1;
156417**
156418** is transformed in the same way as the example above. However:
156419**
156420** SELECT max(b) OVER (PARTITION BY c ORDER BY d),
156421** min(e) OVER (PARTITION BY a ORDER BY b)
156422** FROM t1;
156423**
156424** Must be transformed to:
156425**
156426** SELECT max(b) OVER (PARTITION BY c ORDER BY d) FROM (
156427** SELECT e, min(e) OVER (PARTITION BY a ORDER BY b), c, d, b FROM
156428** SELECT a, e, c, d, b FROM t1 ORDER BY a, b
156429** ) ORDER BY c, d
156430** ) ORDER BY e;
156431**
156432** so that both min() and max() may process rows in the order defined by
156433** their respective window declarations.
156434**
156435** INTERFACE WITH SELECT.C
156436**
156437** When processing the rewritten SELECT statement, code in select.c calls
156438** sqlite3WhereBegin() to begin iterating through the results of the
156439** sub-query, which is always implemented as a co-routine. It then calls
156440** sqlite3WindowCodeStep() to process rows and finish the scan by calling
156441** sqlite3WhereEnd().
156442**
156443** sqlite3WindowCodeStep() generates VM code so that, for each row returned
156444** by the sub-query a sub-routine (OP_Gosub) coded by select.c is invoked.
156445** When the sub-routine is invoked:
156446**
156447** * The results of all window-functions for the row are stored
156448** in the associated Window.regResult registers.
156449**
156450** * The required terminal values are stored in the current row of
156451** temp table Window.iEphCsr.
156452**
156453** In some cases, depending on the window frame and the specific window
156454** functions invoked, sqlite3WindowCodeStep() caches each entire partition
156455** in a temp table before returning any rows. In other cases it does not.
156456** This detail is encapsulated within this file, the code generated by
156457** select.c is the same in either case.
156458**
156459** BUILT-IN WINDOW FUNCTIONS
156460**
156461** This implementation features the following built-in window functions:
156462**
156463** row_number()
156464** rank()
156465** dense_rank()
156466** percent_rank()
156467** cume_dist()
156468** ntile(N)
156469** lead(expr [, offset [, default]])
156470** lag(expr [, offset [, default]])
156471** first_value(expr)
156472** last_value(expr)
156473** nth_value(expr, N)
156474**
156475** These are the same built-in window functions supported by Postgres.
156476** Although the behaviour of aggregate window functions (functions that
156477** can be used as either aggregates or window funtions) allows them to
156478** be implemented using an API, built-in window functions are much more
156479** esoteric. Additionally, some window functions (e.g. nth_value())
156480** may only be implemented by caching the entire partition in memory.
156481** As such, some built-in window functions use the same API as aggregate
156482** window functions and some are implemented directly using VDBE
156483** instructions. Additionally, for those functions that use the API, the
156484** window frame is sometimes modified before the SELECT statement is
156485** rewritten. For example, regardless of the specified window frame, the
156486** row_number() function always uses:
156487**
156488** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
156489**
156490** See sqlite3WindowUpdate() for details.
156491**
156492** As well as some of the built-in window functions, aggregate window
156493** functions min() and max() are implemented using VDBE instructions if
156494** the start of the window frame is declared as anything other than
156495** UNBOUNDED PRECEDING.
156496*/
156497
156498/*
156499** Implementation of built-in window function row_number(). Assumes that the
156500** window frame has been coerced to:
156501**
156502** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
156503*/
156504static void row_numberStepFunc(
156505 sqlite3_context *pCtx,
156506 int nArg,
156507 sqlite3_value **apArg
156508){
156509 i64 *p = (i64*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
156510 if( p ) (*p)++;
156511 UNUSED_PARAMETER(nArg);
156512 UNUSED_PARAMETER(apArg);
156513}
156514static void row_numberValueFunc(sqlite3_context *pCtx){
156515 i64 *p = (i64*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
156516 sqlite3_result_int64(pCtx, iVal: (p ? *p : 0));
156517}
156518
156519/*
156520** Context object type used by rank(), dense_rank(), percent_rank() and
156521** cume_dist().
156522*/
156523struct CallCount {
156524 i64 nValue;
156525 i64 nStep;
156526 i64 nTotal;
156527};
156528
156529/*
156530** Implementation of built-in window function dense_rank(). Assumes that
156531** the window frame has been set to:
156532**
156533** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
156534*/
156535static void dense_rankStepFunc(
156536 sqlite3_context *pCtx,
156537 int nArg,
156538 sqlite3_value **apArg
156539){
156540 struct CallCount *p;
156541 p = (struct CallCount*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
156542 if( p ) p->nStep = 1;
156543 UNUSED_PARAMETER(nArg);
156544 UNUSED_PARAMETER(apArg);
156545}
156546static void dense_rankValueFunc(sqlite3_context *pCtx){
156547 struct CallCount *p;
156548 p = (struct CallCount*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
156549 if( p ){
156550 if( p->nStep ){
156551 p->nValue++;
156552 p->nStep = 0;
156553 }
156554 sqlite3_result_int64(pCtx, iVal: p->nValue);
156555 }
156556}
156557
156558/*
156559** Implementation of built-in window function nth_value(). This
156560** implementation is used in "slow mode" only - when the EXCLUDE clause
156561** is not set to the default value "NO OTHERS".
156562*/
156563struct NthValueCtx {
156564 i64 nStep;
156565 sqlite3_value *pValue;
156566};
156567static void nth_valueStepFunc(
156568 sqlite3_context *pCtx,
156569 int nArg,
156570 sqlite3_value **apArg
156571){
156572 struct NthValueCtx *p;
156573 p = (struct NthValueCtx*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
156574 if( p ){
156575 i64 iVal;
156576 switch( sqlite3_value_numeric_type(pVal: apArg[1]) ){
156577 case SQLITE_INTEGER:
156578 iVal = sqlite3_value_int64(pVal: apArg[1]);
156579 break;
156580 case SQLITE_FLOAT: {
156581 double fVal = sqlite3_value_double(pVal: apArg[1]);
156582 if( ((i64)fVal)!=fVal ) goto error_out;
156583 iVal = (i64)fVal;
156584 break;
156585 }
156586 default:
156587 goto error_out;
156588 }
156589 if( iVal<=0 ) goto error_out;
156590
156591 p->nStep++;
156592 if( iVal==p->nStep ){
156593 p->pValue = sqlite3_value_dup(pOrig: apArg[0]);
156594 if( !p->pValue ){
156595 sqlite3_result_error_nomem(pCtx);
156596 }
156597 }
156598 }
156599 UNUSED_PARAMETER(nArg);
156600 UNUSED_PARAMETER(apArg);
156601 return;
156602
156603 error_out:
156604 sqlite3_result_error(
156605 pCtx, z: "second argument to nth_value must be a positive integer", n: -1
156606 );
156607}
156608static void nth_valueFinalizeFunc(sqlite3_context *pCtx){
156609 struct NthValueCtx *p;
156610 p = (struct NthValueCtx*)sqlite3_aggregate_context(p: pCtx, nByte: 0);
156611 if( p && p->pValue ){
156612 sqlite3_result_value(pCtx, pValue: p->pValue);
156613 sqlite3_value_free(pOld: p->pValue);
156614 p->pValue = 0;
156615 }
156616}
156617#define nth_valueInvFunc noopStepFunc
156618#define nth_valueValueFunc noopValueFunc
156619
156620static void first_valueStepFunc(
156621 sqlite3_context *pCtx,
156622 int nArg,
156623 sqlite3_value **apArg
156624){
156625 struct NthValueCtx *p;
156626 p = (struct NthValueCtx*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
156627 if( p && p->pValue==0 ){
156628 p->pValue = sqlite3_value_dup(pOrig: apArg[0]);
156629 if( !p->pValue ){
156630 sqlite3_result_error_nomem(pCtx);
156631 }
156632 }
156633 UNUSED_PARAMETER(nArg);
156634 UNUSED_PARAMETER(apArg);
156635}
156636static void first_valueFinalizeFunc(sqlite3_context *pCtx){
156637 struct NthValueCtx *p;
156638 p = (struct NthValueCtx*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
156639 if( p && p->pValue ){
156640 sqlite3_result_value(pCtx, pValue: p->pValue);
156641 sqlite3_value_free(pOld: p->pValue);
156642 p->pValue = 0;
156643 }
156644}
156645#define first_valueInvFunc noopStepFunc
156646#define first_valueValueFunc noopValueFunc
156647
156648/*
156649** Implementation of built-in window function rank(). Assumes that
156650** the window frame has been set to:
156651**
156652** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
156653*/
156654static void rankStepFunc(
156655 sqlite3_context *pCtx,
156656 int nArg,
156657 sqlite3_value **apArg
156658){
156659 struct CallCount *p;
156660 p = (struct CallCount*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
156661 if( p ){
156662 p->nStep++;
156663 if( p->nValue==0 ){
156664 p->nValue = p->nStep;
156665 }
156666 }
156667 UNUSED_PARAMETER(nArg);
156668 UNUSED_PARAMETER(apArg);
156669}
156670static void rankValueFunc(sqlite3_context *pCtx){
156671 struct CallCount *p;
156672 p = (struct CallCount*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
156673 if( p ){
156674 sqlite3_result_int64(pCtx, iVal: p->nValue);
156675 p->nValue = 0;
156676 }
156677}
156678
156679/*
156680** Implementation of built-in window function percent_rank(). Assumes that
156681** the window frame has been set to:
156682**
156683** GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
156684*/
156685static void percent_rankStepFunc(
156686 sqlite3_context *pCtx,
156687 int nArg,
156688 sqlite3_value **apArg
156689){
156690 struct CallCount *p;
156691 UNUSED_PARAMETER(nArg); assert( nArg==0 );
156692 UNUSED_PARAMETER(apArg);
156693 p = (struct CallCount*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
156694 if( p ){
156695 p->nTotal++;
156696 }
156697}
156698static void percent_rankInvFunc(
156699 sqlite3_context *pCtx,
156700 int nArg,
156701 sqlite3_value **apArg
156702){
156703 struct CallCount *p;
156704 UNUSED_PARAMETER(nArg); assert( nArg==0 );
156705 UNUSED_PARAMETER(apArg);
156706 p = (struct CallCount*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
156707 p->nStep++;
156708}
156709static void percent_rankValueFunc(sqlite3_context *pCtx){
156710 struct CallCount *p;
156711 p = (struct CallCount*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
156712 if( p ){
156713 p->nValue = p->nStep;
156714 if( p->nTotal>1 ){
156715 double r = (double)p->nValue / (double)(p->nTotal-1);
156716 sqlite3_result_double(pCtx, rVal: r);
156717 }else{
156718 sqlite3_result_double(pCtx, rVal: 0.0);
156719 }
156720 }
156721}
156722#define percent_rankFinalizeFunc percent_rankValueFunc
156723
156724/*
156725** Implementation of built-in window function cume_dist(). Assumes that
156726** the window frame has been set to:
156727**
156728** GROUPS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING
156729*/
156730static void cume_distStepFunc(
156731 sqlite3_context *pCtx,
156732 int nArg,
156733 sqlite3_value **apArg
156734){
156735 struct CallCount *p;
156736 UNUSED_PARAMETER(nArg); assert( nArg==0 );
156737 UNUSED_PARAMETER(apArg);
156738 p = (struct CallCount*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
156739 if( p ){
156740 p->nTotal++;
156741 }
156742}
156743static void cume_distInvFunc(
156744 sqlite3_context *pCtx,
156745 int nArg,
156746 sqlite3_value **apArg
156747){
156748 struct CallCount *p;
156749 UNUSED_PARAMETER(nArg); assert( nArg==0 );
156750 UNUSED_PARAMETER(apArg);
156751 p = (struct CallCount*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
156752 p->nStep++;
156753}
156754static void cume_distValueFunc(sqlite3_context *pCtx){
156755 struct CallCount *p;
156756 p = (struct CallCount*)sqlite3_aggregate_context(p: pCtx, nByte: 0);
156757 if( p ){
156758 double r = (double)(p->nStep) / (double)(p->nTotal);
156759 sqlite3_result_double(pCtx, rVal: r);
156760 }
156761}
156762#define cume_distFinalizeFunc cume_distValueFunc
156763
156764/*
156765** Context object for ntile() window function.
156766*/
156767struct NtileCtx {
156768 i64 nTotal; /* Total rows in partition */
156769 i64 nParam; /* Parameter passed to ntile(N) */
156770 i64 iRow; /* Current row */
156771};
156772
156773/*
156774** Implementation of ntile(). This assumes that the window frame has
156775** been coerced to:
156776**
156777** ROWS CURRENT ROW AND UNBOUNDED FOLLOWING
156778*/
156779static void ntileStepFunc(
156780 sqlite3_context *pCtx,
156781 int nArg,
156782 sqlite3_value **apArg
156783){
156784 struct NtileCtx *p;
156785 assert( nArg==1 ); UNUSED_PARAMETER(nArg);
156786 p = (struct NtileCtx*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
156787 if( p ){
156788 if( p->nTotal==0 ){
156789 p->nParam = sqlite3_value_int64(pVal: apArg[0]);
156790 if( p->nParam<=0 ){
156791 sqlite3_result_error(
156792 pCtx, z: "argument of ntile must be a positive integer", n: -1
156793 );
156794 }
156795 }
156796 p->nTotal++;
156797 }
156798}
156799static void ntileInvFunc(
156800 sqlite3_context *pCtx,
156801 int nArg,
156802 sqlite3_value **apArg
156803){
156804 struct NtileCtx *p;
156805 assert( nArg==1 ); UNUSED_PARAMETER(nArg);
156806 UNUSED_PARAMETER(apArg);
156807 p = (struct NtileCtx*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
156808 p->iRow++;
156809}
156810static void ntileValueFunc(sqlite3_context *pCtx){
156811 struct NtileCtx *p;
156812 p = (struct NtileCtx*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
156813 if( p && p->nParam>0 ){
156814 int nSize = (p->nTotal / p->nParam);
156815 if( nSize==0 ){
156816 sqlite3_result_int64(pCtx, iVal: p->iRow+1);
156817 }else{
156818 i64 nLarge = p->nTotal - p->nParam*nSize;
156819 i64 iSmall = nLarge*(nSize+1);
156820 i64 iRow = p->iRow;
156821
156822 assert( (nLarge*(nSize+1) + (p->nParam-nLarge)*nSize)==p->nTotal );
156823
156824 if( iRow<iSmall ){
156825 sqlite3_result_int64(pCtx, iVal: 1 + iRow/(nSize+1));
156826 }else{
156827 sqlite3_result_int64(pCtx, iVal: 1 + nLarge + (iRow-iSmall)/nSize);
156828 }
156829 }
156830 }
156831}
156832#define ntileFinalizeFunc ntileValueFunc
156833
156834/*
156835** Context object for last_value() window function.
156836*/
156837struct LastValueCtx {
156838 sqlite3_value *pVal;
156839 int nVal;
156840};
156841
156842/*
156843** Implementation of last_value().
156844*/
156845static void last_valueStepFunc(
156846 sqlite3_context *pCtx,
156847 int nArg,
156848 sqlite3_value **apArg
156849){
156850 struct LastValueCtx *p;
156851 UNUSED_PARAMETER(nArg);
156852 p = (struct LastValueCtx*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
156853 if( p ){
156854 sqlite3_value_free(pOld: p->pVal);
156855 p->pVal = sqlite3_value_dup(pOrig: apArg[0]);
156856 if( p->pVal==0 ){
156857 sqlite3_result_error_nomem(pCtx);
156858 }else{
156859 p->nVal++;
156860 }
156861 }
156862}
156863static void last_valueInvFunc(
156864 sqlite3_context *pCtx,
156865 int nArg,
156866 sqlite3_value **apArg
156867){
156868 struct LastValueCtx *p;
156869 UNUSED_PARAMETER(nArg);
156870 UNUSED_PARAMETER(apArg);
156871 p = (struct LastValueCtx*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
156872 if( ALWAYS(p) ){
156873 p->nVal--;
156874 if( p->nVal==0 ){
156875 sqlite3_value_free(pOld: p->pVal);
156876 p->pVal = 0;
156877 }
156878 }
156879}
156880static void last_valueValueFunc(sqlite3_context *pCtx){
156881 struct LastValueCtx *p;
156882 p = (struct LastValueCtx*)sqlite3_aggregate_context(p: pCtx, nByte: 0);
156883 if( p && p->pVal ){
156884 sqlite3_result_value(pCtx, pValue: p->pVal);
156885 }
156886}
156887static void last_valueFinalizeFunc(sqlite3_context *pCtx){
156888 struct LastValueCtx *p;
156889 p = (struct LastValueCtx*)sqlite3_aggregate_context(p: pCtx, nByte: sizeof(*p));
156890 if( p && p->pVal ){
156891 sqlite3_result_value(pCtx, pValue: p->pVal);
156892 sqlite3_value_free(pOld: p->pVal);
156893 p->pVal = 0;
156894 }
156895}
156896
156897/*
156898** Static names for the built-in window function names. These static
156899** names are used, rather than string literals, so that FuncDef objects
156900** can be associated with a particular window function by direct
156901** comparison of the zName pointer. Example:
156902**
156903** if( pFuncDef->zName==row_valueName ){ ... }
156904*/
156905static const char row_numberName[] = "row_number";
156906static const char dense_rankName[] = "dense_rank";
156907static const char rankName[] = "rank";
156908static const char percent_rankName[] = "percent_rank";
156909static const char cume_distName[] = "cume_dist";
156910static const char ntileName[] = "ntile";
156911static const char last_valueName[] = "last_value";
156912static const char nth_valueName[] = "nth_value";
156913static const char first_valueName[] = "first_value";
156914static const char leadName[] = "lead";
156915static const char lagName[] = "lag";
156916
156917/*
156918** No-op implementations of xStep() and xFinalize(). Used as place-holders
156919** for built-in window functions that never call those interfaces.
156920**
156921** The noopValueFunc() is called but is expected to do nothing. The
156922** noopStepFunc() is never called, and so it is marked with NO_TEST to
156923** let the test coverage routine know not to expect this function to be
156924** invoked.
156925*/
156926static void noopStepFunc( /*NO_TEST*/
156927 sqlite3_context *p, /*NO_TEST*/
156928 int n, /*NO_TEST*/
156929 sqlite3_value **a /*NO_TEST*/
156930){ /*NO_TEST*/
156931 UNUSED_PARAMETER(p); /*NO_TEST*/
156932 UNUSED_PARAMETER(n); /*NO_TEST*/
156933 UNUSED_PARAMETER(a); /*NO_TEST*/
156934 assert(0); /*NO_TEST*/
156935} /*NO_TEST*/
156936static void noopValueFunc(sqlite3_context *p){ UNUSED_PARAMETER(p); /*no-op*/ }
156937
156938/* Window functions that use all window interfaces: xStep, xFinal,
156939** xValue, and xInverse */
156940#define WINDOWFUNCALL(name,nArg,extra) { \
156941 nArg, (SQLITE_FUNC_BUILTIN|SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0, \
156942 name ## StepFunc, name ## FinalizeFunc, name ## ValueFunc, \
156943 name ## InvFunc, name ## Name, {0} \
156944}
156945
156946/* Window functions that are implemented using bytecode and thus have
156947** no-op routines for their methods */
156948#define WINDOWFUNCNOOP(name,nArg,extra) { \
156949 nArg, (SQLITE_FUNC_BUILTIN|SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0, \
156950 noopStepFunc, noopValueFunc, noopValueFunc, \
156951 noopStepFunc, name ## Name, {0} \
156952}
156953
156954/* Window functions that use all window interfaces: xStep, the
156955** same routine for xFinalize and xValue and which never call
156956** xInverse. */
156957#define WINDOWFUNCX(name,nArg,extra) { \
156958 nArg, (SQLITE_FUNC_BUILTIN|SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0, \
156959 name ## StepFunc, name ## ValueFunc, name ## ValueFunc, \
156960 noopStepFunc, name ## Name, {0} \
156961}
156962
156963
156964/*
156965** Register those built-in window functions that are not also aggregates.
156966*/
156967SQLITE_PRIVATE void sqlite3WindowFunctions(void){
156968 static FuncDef aWindowFuncs[] = {
156969 WINDOWFUNCX(row_number, 0, 0),
156970 WINDOWFUNCX(dense_rank, 0, 0),
156971 WINDOWFUNCX(rank, 0, 0),
156972 WINDOWFUNCALL(percent_rank, 0, 0),
156973 WINDOWFUNCALL(cume_dist, 0, 0),
156974 WINDOWFUNCALL(ntile, 1, 0),
156975 WINDOWFUNCALL(last_value, 1, 0),
156976 WINDOWFUNCALL(nth_value, 2, 0),
156977 WINDOWFUNCALL(first_value, 1, 0),
156978 WINDOWFUNCNOOP(lead, 1, 0),
156979 WINDOWFUNCNOOP(lead, 2, 0),
156980 WINDOWFUNCNOOP(lead, 3, 0),
156981 WINDOWFUNCNOOP(lag, 1, 0),
156982 WINDOWFUNCNOOP(lag, 2, 0),
156983 WINDOWFUNCNOOP(lag, 3, 0),
156984 };
156985 sqlite3InsertBuiltinFuncs(aDef: aWindowFuncs, ArraySize(aWindowFuncs));
156986}
156987
156988static Window *windowFind(Parse *pParse, Window *pList, const char *zName){
156989 Window *p;
156990 for(p=pList; p; p=p->pNextWin){
156991 if( sqlite3StrICmp(zLeft: p->zName, zRight: zName)==0 ) break;
156992 }
156993 if( p==0 ){
156994 sqlite3ErrorMsg(pParse, zFormat: "no such window: %s", zName);
156995 }
156996 return p;
156997}
156998
156999/*
157000** This function is called immediately after resolving the function name
157001** for a window function within a SELECT statement. Argument pList is a
157002** linked list of WINDOW definitions for the current SELECT statement.
157003** Argument pFunc is the function definition just resolved and pWin
157004** is the Window object representing the associated OVER clause. This
157005** function updates the contents of pWin as follows:
157006**
157007** * If the OVER clause refered to a named window (as in "max(x) OVER win"),
157008** search list pList for a matching WINDOW definition, and update pWin
157009** accordingly. If no such WINDOW clause can be found, leave an error
157010** in pParse.
157011**
157012** * If the function is a built-in window function that requires the
157013** window to be coerced (see "BUILT-IN WINDOW FUNCTIONS" at the top
157014** of this file), pWin is updated here.
157015*/
157016SQLITE_PRIVATE void sqlite3WindowUpdate(
157017 Parse *pParse,
157018 Window *pList, /* List of named windows for this SELECT */
157019 Window *pWin, /* Window frame to update */
157020 FuncDef *pFunc /* Window function definition */
157021){
157022 if( pWin->zName && pWin->eFrmType==0 ){
157023 Window *p = windowFind(pParse, pList, zName: pWin->zName);
157024 if( p==0 ) return;
157025 pWin->pPartition = sqlite3ExprListDup(db: pParse->db, p: p->pPartition, flags: 0);
157026 pWin->pOrderBy = sqlite3ExprListDup(db: pParse->db, p: p->pOrderBy, flags: 0);
157027 pWin->pStart = sqlite3ExprDup(db: pParse->db, p: p->pStart, flags: 0);
157028 pWin->pEnd = sqlite3ExprDup(db: pParse->db, p: p->pEnd, flags: 0);
157029 pWin->eStart = p->eStart;
157030 pWin->eEnd = p->eEnd;
157031 pWin->eFrmType = p->eFrmType;
157032 pWin->eExclude = p->eExclude;
157033 }else{
157034 sqlite3WindowChain(pParse, pWin, pList);
157035 }
157036 if( (pWin->eFrmType==TK_RANGE)
157037 && (pWin->pStart || pWin->pEnd)
157038 && (pWin->pOrderBy==0 || pWin->pOrderBy->nExpr!=1)
157039 ){
157040 sqlite3ErrorMsg(pParse,
157041 zFormat: "RANGE with offset PRECEDING/FOLLOWING requires one ORDER BY expression"
157042 );
157043 }else
157044 if( pFunc->funcFlags & SQLITE_FUNC_WINDOW ){
157045 sqlite3 *db = pParse->db;
157046 if( pWin->pFilter ){
157047 sqlite3ErrorMsg(pParse,
157048 zFormat: "FILTER clause may only be used with aggregate window functions"
157049 );
157050 }else{
157051 struct WindowUpdate {
157052 const char *zFunc;
157053 int eFrmType;
157054 int eStart;
157055 int eEnd;
157056 } aUp[] = {
157057 { row_numberName, TK_ROWS, TK_UNBOUNDED, TK_CURRENT },
157058 { dense_rankName, TK_RANGE, TK_UNBOUNDED, TK_CURRENT },
157059 { rankName, TK_RANGE, TK_UNBOUNDED, TK_CURRENT },
157060 { percent_rankName, TK_GROUPS, TK_CURRENT, TK_UNBOUNDED },
157061 { cume_distName, TK_GROUPS, TK_FOLLOWING, TK_UNBOUNDED },
157062 { ntileName, TK_ROWS, TK_CURRENT, TK_UNBOUNDED },
157063 { leadName, TK_ROWS, TK_UNBOUNDED, TK_UNBOUNDED },
157064 { lagName, TK_ROWS, TK_UNBOUNDED, TK_CURRENT },
157065 };
157066 int i;
157067 for(i=0; i<ArraySize(aUp); i++){
157068 if( pFunc->zName==aUp[i].zFunc ){
157069 sqlite3ExprDelete(db, p: pWin->pStart);
157070 sqlite3ExprDelete(db, p: pWin->pEnd);
157071 pWin->pEnd = pWin->pStart = 0;
157072 pWin->eFrmType = aUp[i].eFrmType;
157073 pWin->eStart = aUp[i].eStart;
157074 pWin->eEnd = aUp[i].eEnd;
157075 pWin->eExclude = 0;
157076 if( pWin->eStart==TK_FOLLOWING ){
157077 pWin->pStart = sqlite3Expr(db, TK_INTEGER, zToken: "1");
157078 }
157079 break;
157080 }
157081 }
157082 }
157083 }
157084 pWin->pFunc = pFunc;
157085}
157086
157087/*
157088** Context object passed through sqlite3WalkExprList() to
157089** selectWindowRewriteExprCb() by selectWindowRewriteEList().
157090*/
157091typedef struct WindowRewrite WindowRewrite;
157092struct WindowRewrite {
157093 Window *pWin;
157094 SrcList *pSrc;
157095 ExprList *pSub;
157096 Table *pTab;
157097 Select *pSubSelect; /* Current sub-select, if any */
157098};
157099
157100/*
157101** Callback function used by selectWindowRewriteEList(). If necessary,
157102** this function appends to the output expression-list and updates
157103** expression (*ppExpr) in place.
157104*/
157105static int selectWindowRewriteExprCb(Walker *pWalker, Expr *pExpr){
157106 struct WindowRewrite *p = pWalker->u.pRewrite;
157107 Parse *pParse = pWalker->pParse;
157108 assert( p!=0 );
157109 assert( p->pWin!=0 );
157110
157111 /* If this function is being called from within a scalar sub-select
157112 ** that used by the SELECT statement being processed, only process
157113 ** TK_COLUMN expressions that refer to it (the outer SELECT). Do
157114 ** not process aggregates or window functions at all, as they belong
157115 ** to the scalar sub-select. */
157116 if( p->pSubSelect ){
157117 if( pExpr->op!=TK_COLUMN ){
157118 return WRC_Continue;
157119 }else{
157120 int nSrc = p->pSrc->nSrc;
157121 int i;
157122 for(i=0; i<nSrc; i++){
157123 if( pExpr->iTable==p->pSrc->a[i].iCursor ) break;
157124 }
157125 if( i==nSrc ) return WRC_Continue;
157126 }
157127 }
157128
157129 switch( pExpr->op ){
157130
157131 case TK_FUNCTION:
157132 if( !ExprHasProperty(pExpr, EP_WinFunc) ){
157133 break;
157134 }else{
157135 Window *pWin;
157136 for(pWin=p->pWin; pWin; pWin=pWin->pNextWin){
157137 if( pExpr->y.pWin==pWin ){
157138 assert( pWin->pOwner==pExpr );
157139 return WRC_Prune;
157140 }
157141 }
157142 }
157143 /* no break */ deliberate_fall_through
157144
157145 case TK_AGG_FUNCTION:
157146 case TK_COLUMN: {
157147 int iCol = -1;
157148 if( pParse->db->mallocFailed ) return WRC_Abort;
157149 if( p->pSub ){
157150 int i;
157151 for(i=0; i<p->pSub->nExpr; i++){
157152 if( 0==sqlite3ExprCompare(pParse: 0, pA: p->pSub->a[i].pExpr, pB: pExpr, iTab: -1) ){
157153 iCol = i;
157154 break;
157155 }
157156 }
157157 }
157158 if( iCol<0 ){
157159 Expr *pDup = sqlite3ExprDup(db: pParse->db, p: pExpr, flags: 0);
157160 if( pDup && pDup->op==TK_AGG_FUNCTION ) pDup->op = TK_FUNCTION;
157161 p->pSub = sqlite3ExprListAppend(pParse, pList: p->pSub, pExpr: pDup);
157162 }
157163 if( p->pSub ){
157164 int f = pExpr->flags & EP_Collate;
157165 assert( ExprHasProperty(pExpr, EP_Static)==0 );
157166 ExprSetProperty(pExpr, EP_Static);
157167 sqlite3ExprDelete(db: pParse->db, p: pExpr);
157168 ExprClearProperty(pExpr, EP_Static);
157169 memset(s: pExpr, c: 0, n: sizeof(Expr));
157170
157171 pExpr->op = TK_COLUMN;
157172 pExpr->iColumn = (iCol<0 ? p->pSub->nExpr-1: iCol);
157173 pExpr->iTable = p->pWin->iEphCsr;
157174 pExpr->y.pTab = p->pTab;
157175 pExpr->flags = f;
157176 }
157177 if( pParse->db->mallocFailed ) return WRC_Abort;
157178 break;
157179 }
157180
157181 default: /* no-op */
157182 break;
157183 }
157184
157185 return WRC_Continue;
157186}
157187static int selectWindowRewriteSelectCb(Walker *pWalker, Select *pSelect){
157188 struct WindowRewrite *p = pWalker->u.pRewrite;
157189 Select *pSave = p->pSubSelect;
157190 if( pSave==pSelect ){
157191 return WRC_Continue;
157192 }else{
157193 p->pSubSelect = pSelect;
157194 sqlite3WalkSelect(pWalker, p: pSelect);
157195 p->pSubSelect = pSave;
157196 }
157197 return WRC_Prune;
157198}
157199
157200
157201/*
157202** Iterate through each expression in expression-list pEList. For each:
157203**
157204** * TK_COLUMN,
157205** * aggregate function, or
157206** * window function with a Window object that is not a member of the
157207** Window list passed as the second argument (pWin).
157208**
157209** Append the node to output expression-list (*ppSub). And replace it
157210** with a TK_COLUMN that reads the (N-1)th element of table
157211** pWin->iEphCsr, where N is the number of elements in (*ppSub) after
157212** appending the new one.
157213*/
157214static void selectWindowRewriteEList(
157215 Parse *pParse,
157216 Window *pWin,
157217 SrcList *pSrc,
157218 ExprList *pEList, /* Rewrite expressions in this list */
157219 Table *pTab,
157220 ExprList **ppSub /* IN/OUT: Sub-select expression-list */
157221){
157222 Walker sWalker;
157223 WindowRewrite sRewrite;
157224
157225 assert( pWin!=0 );
157226 memset(s: &sWalker, c: 0, n: sizeof(Walker));
157227 memset(s: &sRewrite, c: 0, n: sizeof(WindowRewrite));
157228
157229 sRewrite.pSub = *ppSub;
157230 sRewrite.pWin = pWin;
157231 sRewrite.pSrc = pSrc;
157232 sRewrite.pTab = pTab;
157233
157234 sWalker.pParse = pParse;
157235 sWalker.xExprCallback = selectWindowRewriteExprCb;
157236 sWalker.xSelectCallback = selectWindowRewriteSelectCb;
157237 sWalker.u.pRewrite = &sRewrite;
157238
157239 (void)sqlite3WalkExprList(pWalker: &sWalker, p: pEList);
157240
157241 *ppSub = sRewrite.pSub;
157242}
157243
157244/*
157245** Append a copy of each expression in expression-list pAppend to
157246** expression list pList. Return a pointer to the result list.
157247*/
157248static ExprList *exprListAppendList(
157249 Parse *pParse, /* Parsing context */
157250 ExprList *pList, /* List to which to append. Might be NULL */
157251 ExprList *pAppend, /* List of values to append. Might be NULL */
157252 int bIntToNull
157253){
157254 if( pAppend ){
157255 int i;
157256 int nInit = pList ? pList->nExpr : 0;
157257 for(i=0; i<pAppend->nExpr; i++){
157258 sqlite3 *db = pParse->db;
157259 Expr *pDup = sqlite3ExprDup(db, p: pAppend->a[i].pExpr, flags: 0);
157260 assert( pDup==0 || !ExprHasProperty(pDup, EP_MemToken) );
157261 if( db->mallocFailed ){
157262 sqlite3ExprDelete(db, p: pDup);
157263 break;
157264 }
157265 if( bIntToNull ){
157266 int iDummy;
157267 Expr *pSub;
157268 pSub = sqlite3ExprSkipCollateAndLikely(pExpr: pDup);
157269 if( sqlite3ExprIsInteger(p: pSub, pValue: &iDummy) ){
157270 pSub->op = TK_NULL;
157271 pSub->flags &= ~(EP_IntValue|EP_IsTrue|EP_IsFalse);
157272 pSub->u.zToken = 0;
157273 }
157274 }
157275 pList = sqlite3ExprListAppend(pParse, pList, pExpr: pDup);
157276 if( pList ) pList->a[nInit+i].sortFlags = pAppend->a[i].sortFlags;
157277 }
157278 }
157279 return pList;
157280}
157281
157282/*
157283** When rewriting a query, if the new subquery in the FROM clause
157284** contains TK_AGG_FUNCTION nodes that refer to an outer query,
157285** then we have to increase the Expr->op2 values of those nodes
157286** due to the extra subquery layer that was added.
157287**
157288** See also the incrAggDepth() routine in resolve.c
157289*/
157290static int sqlite3WindowExtraAggFuncDepth(Walker *pWalker, Expr *pExpr){
157291 if( pExpr->op==TK_AGG_FUNCTION
157292 && pExpr->op2>=pWalker->walkerDepth
157293 ){
157294 pExpr->op2++;
157295 }
157296 return WRC_Continue;
157297}
157298
157299static int disallowAggregatesInOrderByCb(Walker *pWalker, Expr *pExpr){
157300 if( pExpr->op==TK_AGG_FUNCTION && pExpr->pAggInfo==0 ){
157301 assert( !ExprHasProperty(pExpr, EP_IntValue) );
157302 sqlite3ErrorMsg(pParse: pWalker->pParse,
157303 zFormat: "misuse of aggregate: %s()", pExpr->u.zToken);
157304 }
157305 return WRC_Continue;
157306}
157307
157308/*
157309** If the SELECT statement passed as the second argument does not invoke
157310** any SQL window functions, this function is a no-op. Otherwise, it
157311** rewrites the SELECT statement so that window function xStep functions
157312** are invoked in the correct order as described under "SELECT REWRITING"
157313** at the top of this file.
157314*/
157315SQLITE_PRIVATE int sqlite3WindowRewrite(Parse *pParse, Select *p){
157316 int rc = SQLITE_OK;
157317 if( p->pWin && p->pPrior==0 && ALWAYS((p->selFlags & SF_WinRewrite)==0) ){
157318 Vdbe *v = sqlite3GetVdbe(pParse);
157319 sqlite3 *db = pParse->db;
157320 Select *pSub = 0; /* The subquery */
157321 SrcList *pSrc = p->pSrc;
157322 Expr *pWhere = p->pWhere;
157323 ExprList *pGroupBy = p->pGroupBy;
157324 Expr *pHaving = p->pHaving;
157325 ExprList *pSort = 0;
157326
157327 ExprList *pSublist = 0; /* Expression list for sub-query */
157328 Window *pMWin = p->pWin; /* Main window object */
157329 Window *pWin; /* Window object iterator */
157330 Table *pTab;
157331 Walker w;
157332
157333 u32 selFlags = p->selFlags;
157334
157335 pTab = sqlite3DbMallocZero(db, n: sizeof(Table));
157336 if( pTab==0 ){
157337 return sqlite3ErrorToParser(db, SQLITE_NOMEM);
157338 }
157339 sqlite3AggInfoPersistWalkerInit(pWalker: &w, pParse);
157340 sqlite3WalkSelect(pWalker: &w, p);
157341 if( (p->selFlags & SF_Aggregate)==0 ){
157342 w.xExprCallback = disallowAggregatesInOrderByCb;
157343 w.xSelectCallback = 0;
157344 sqlite3WalkExprList(pWalker: &w, p: p->pOrderBy);
157345 }
157346
157347 p->pSrc = 0;
157348 p->pWhere = 0;
157349 p->pGroupBy = 0;
157350 p->pHaving = 0;
157351 p->selFlags &= ~SF_Aggregate;
157352 p->selFlags |= SF_WinRewrite;
157353
157354 /* Create the ORDER BY clause for the sub-select. This is the concatenation
157355 ** of the window PARTITION and ORDER BY clauses. Then, if this makes it
157356 ** redundant, remove the ORDER BY from the parent SELECT. */
157357 pSort = exprListAppendList(pParse, pList: 0, pAppend: pMWin->pPartition, bIntToNull: 1);
157358 pSort = exprListAppendList(pParse, pList: pSort, pAppend: pMWin->pOrderBy, bIntToNull: 1);
157359 if( pSort && p->pOrderBy && p->pOrderBy->nExpr<=pSort->nExpr ){
157360 int nSave = pSort->nExpr;
157361 pSort->nExpr = p->pOrderBy->nExpr;
157362 if( sqlite3ExprListCompare(pA: pSort, pB: p->pOrderBy, iTab: -1)==0 ){
157363 sqlite3ExprListDelete(db, pList: p->pOrderBy);
157364 p->pOrderBy = 0;
157365 }
157366 pSort->nExpr = nSave;
157367 }
157368
157369 /* Assign a cursor number for the ephemeral table used to buffer rows.
157370 ** The OpenEphemeral instruction is coded later, after it is known how
157371 ** many columns the table will have. */
157372 pMWin->iEphCsr = pParse->nTab++;
157373 pParse->nTab += 3;
157374
157375 selectWindowRewriteEList(pParse, pWin: pMWin, pSrc, pEList: p->pEList, pTab, ppSub: &pSublist);
157376 selectWindowRewriteEList(pParse, pWin: pMWin, pSrc, pEList: p->pOrderBy, pTab, ppSub: &pSublist);
157377 pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0);
157378
157379 /* Append the PARTITION BY and ORDER BY expressions to the to the
157380 ** sub-select expression list. They are required to figure out where
157381 ** boundaries for partitions and sets of peer rows lie. */
157382 pSublist = exprListAppendList(pParse, pList: pSublist, pAppend: pMWin->pPartition, bIntToNull: 0);
157383 pSublist = exprListAppendList(pParse, pList: pSublist, pAppend: pMWin->pOrderBy, bIntToNull: 0);
157384
157385 /* Append the arguments passed to each window function to the
157386 ** sub-select expression list. Also allocate two registers for each
157387 ** window function - one for the accumulator, another for interim
157388 ** results. */
157389 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
157390 ExprList *pArgs;
157391 assert( ExprUseXList(pWin->pOwner) );
157392 pArgs = pWin->pOwner->x.pList;
157393 if( pWin->pFunc->funcFlags & SQLITE_FUNC_SUBTYPE ){
157394 selectWindowRewriteEList(pParse, pWin: pMWin, pSrc, pEList: pArgs, pTab, ppSub: &pSublist);
157395 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
157396 pWin->bExprArgs = 1;
157397 }else{
157398 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
157399 pSublist = exprListAppendList(pParse, pList: pSublist, pAppend: pArgs, bIntToNull: 0);
157400 }
157401 if( pWin->pFilter ){
157402 Expr *pFilter = sqlite3ExprDup(db, p: pWin->pFilter, flags: 0);
157403 pSublist = sqlite3ExprListAppend(pParse, pList: pSublist, pExpr: pFilter);
157404 }
157405 pWin->regAccum = ++pParse->nMem;
157406 pWin->regResult = ++pParse->nMem;
157407 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: pWin->regAccum);
157408 }
157409
157410 /* If there is no ORDER BY or PARTITION BY clause, and the window
157411 ** function accepts zero arguments, and there are no other columns
157412 ** selected (e.g. "SELECT row_number() OVER () FROM t1"), it is possible
157413 ** that pSublist is still NULL here. Add a constant expression here to
157414 ** keep everything legal in this case.
157415 */
157416 if( pSublist==0 ){
157417 pSublist = sqlite3ExprListAppend(pParse, pList: 0,
157418 pExpr: sqlite3Expr(db, TK_INTEGER, zToken: "0")
157419 );
157420 }
157421
157422 pSub = sqlite3SelectNew(
157423 pParse, pEList: pSublist, pSrc, pWhere, pGroupBy, pHaving, pOrderBy: pSort, selFlags: 0, pLimit: 0
157424 );
157425 SELECTTRACE(1,pParse,pSub,
157426 ("New window-function subquery in FROM clause of (%u/%p)\n",
157427 p->selId, p));
157428 p->pSrc = sqlite3SrcListAppend(pParse, pList: 0, pTable: 0, pDatabase: 0);
157429 assert( pSub!=0 || p->pSrc==0 ); /* Due to db->mallocFailed test inside
157430 ** of sqlite3DbMallocRawNN() called from
157431 ** sqlite3SrcListAppend() */
157432 if( p->pSrc ){
157433 Table *pTab2;
157434 p->pSrc->a[0].pSelect = pSub;
157435 sqlite3SrcListAssignCursors(pParse, pList: p->pSrc);
157436 pSub->selFlags |= SF_Expanded|SF_OrderByReqd;
157437 pTab2 = sqlite3ResultSetOfSelect(pParse, pSelect: pSub, SQLITE_AFF_NONE);
157438 pSub->selFlags |= (selFlags & SF_Aggregate);
157439 if( pTab2==0 ){
157440 /* Might actually be some other kind of error, but in that case
157441 ** pParse->nErr will be set, so if SQLITE_NOMEM is set, we will get
157442 ** the correct error message regardless. */
157443 rc = SQLITE_NOMEM;
157444 }else{
157445 memcpy(dest: pTab, src: pTab2, n: sizeof(Table));
157446 pTab->tabFlags |= TF_Ephemeral;
157447 p->pSrc->a[0].pTab = pTab;
157448 pTab = pTab2;
157449 memset(s: &w, c: 0, n: sizeof(w));
157450 w.xExprCallback = sqlite3WindowExtraAggFuncDepth;
157451 w.xSelectCallback = sqlite3WalkerDepthIncrease;
157452 w.xSelectCallback2 = sqlite3WalkerDepthDecrease;
157453 sqlite3WalkSelect(pWalker: &w, p: pSub);
157454 }
157455 }else{
157456 sqlite3SelectDelete(db, p: pSub);
157457 }
157458 if( db->mallocFailed ) rc = SQLITE_NOMEM;
157459
157460 /* Defer deleting the temporary table pTab because if an error occurred,
157461 ** there could still be references to that table embedded in the
157462 ** result-set or ORDER BY clause of the SELECT statement p. */
157463 sqlite3ParserAddCleanup(pParse, xCleanup: sqlite3DbFree, pPtr: pTab);
157464 }
157465
157466 if( rc ){
157467 if( pParse->nErr==0 ){
157468 assert( pParse->db->mallocFailed );
157469 sqlite3ErrorToParser(db: pParse->db, SQLITE_NOMEM);
157470 }
157471 }
157472 return rc;
157473}
157474
157475/*
157476** Unlink the Window object from the Select to which it is attached,
157477** if it is attached.
157478*/
157479SQLITE_PRIVATE void sqlite3WindowUnlinkFromSelect(Window *p){
157480 if( p->ppThis ){
157481 *p->ppThis = p->pNextWin;
157482 if( p->pNextWin ) p->pNextWin->ppThis = p->ppThis;
157483 p->ppThis = 0;
157484 }
157485}
157486
157487/*
157488** Free the Window object passed as the second argument.
157489*/
157490SQLITE_PRIVATE void sqlite3WindowDelete(sqlite3 *db, Window *p){
157491 if( p ){
157492 sqlite3WindowUnlinkFromSelect(p);
157493 sqlite3ExprDelete(db, p: p->pFilter);
157494 sqlite3ExprListDelete(db, pList: p->pPartition);
157495 sqlite3ExprListDelete(db, pList: p->pOrderBy);
157496 sqlite3ExprDelete(db, p: p->pEnd);
157497 sqlite3ExprDelete(db, p: p->pStart);
157498 sqlite3DbFree(db, p: p->zName);
157499 sqlite3DbFree(db, p: p->zBase);
157500 sqlite3DbFree(db, p);
157501 }
157502}
157503
157504/*
157505** Free the linked list of Window objects starting at the second argument.
157506*/
157507SQLITE_PRIVATE void sqlite3WindowListDelete(sqlite3 *db, Window *p){
157508 while( p ){
157509 Window *pNext = p->pNextWin;
157510 sqlite3WindowDelete(db, p);
157511 p = pNext;
157512 }
157513}
157514
157515/*
157516** The argument expression is an PRECEDING or FOLLOWING offset. The
157517** value should be a non-negative integer. If the value is not a
157518** constant, change it to NULL. The fact that it is then a non-negative
157519** integer will be caught later. But it is important not to leave
157520** variable values in the expression tree.
157521*/
157522static Expr *sqlite3WindowOffsetExpr(Parse *pParse, Expr *pExpr){
157523 if( 0==sqlite3ExprIsConstant(p: pExpr) ){
157524 if( IN_RENAME_OBJECT ) sqlite3RenameExprUnmap(pParse, pExpr);
157525 sqlite3ExprDelete(db: pParse->db, p: pExpr);
157526 pExpr = sqlite3ExprAlloc(db: pParse->db, TK_NULL, pToken: 0, dequote: 0);
157527 }
157528 return pExpr;
157529}
157530
157531/*
157532** Allocate and return a new Window object describing a Window Definition.
157533*/
157534SQLITE_PRIVATE Window *sqlite3WindowAlloc(
157535 Parse *pParse, /* Parsing context */
157536 int eType, /* Frame type. TK_RANGE, TK_ROWS, TK_GROUPS, or 0 */
157537 int eStart, /* Start type: CURRENT, PRECEDING, FOLLOWING, UNBOUNDED */
157538 Expr *pStart, /* Start window size if TK_PRECEDING or FOLLOWING */
157539 int eEnd, /* End type: CURRENT, FOLLOWING, TK_UNBOUNDED, PRECEDING */
157540 Expr *pEnd, /* End window size if TK_FOLLOWING or PRECEDING */
157541 u8 eExclude /* EXCLUDE clause */
157542){
157543 Window *pWin = 0;
157544 int bImplicitFrame = 0;
157545
157546 /* Parser assures the following: */
157547 assert( eType==0 || eType==TK_RANGE || eType==TK_ROWS || eType==TK_GROUPS );
157548 assert( eStart==TK_CURRENT || eStart==TK_PRECEDING
157549 || eStart==TK_UNBOUNDED || eStart==TK_FOLLOWING );
157550 assert( eEnd==TK_CURRENT || eEnd==TK_FOLLOWING
157551 || eEnd==TK_UNBOUNDED || eEnd==TK_PRECEDING );
157552 assert( (eStart==TK_PRECEDING || eStart==TK_FOLLOWING)==(pStart!=0) );
157553 assert( (eEnd==TK_FOLLOWING || eEnd==TK_PRECEDING)==(pEnd!=0) );
157554
157555 if( eType==0 ){
157556 bImplicitFrame = 1;
157557 eType = TK_RANGE;
157558 }
157559
157560 /* Additionally, the
157561 ** starting boundary type may not occur earlier in the following list than
157562 ** the ending boundary type:
157563 **
157564 ** UNBOUNDED PRECEDING
157565 ** <expr> PRECEDING
157566 ** CURRENT ROW
157567 ** <expr> FOLLOWING
157568 ** UNBOUNDED FOLLOWING
157569 **
157570 ** The parser ensures that "UNBOUNDED PRECEDING" cannot be used as an ending
157571 ** boundary, and than "UNBOUNDED FOLLOWING" cannot be used as a starting
157572 ** frame boundary.
157573 */
157574 if( (eStart==TK_CURRENT && eEnd==TK_PRECEDING)
157575 || (eStart==TK_FOLLOWING && (eEnd==TK_PRECEDING || eEnd==TK_CURRENT))
157576 ){
157577 sqlite3ErrorMsg(pParse, zFormat: "unsupported frame specification");
157578 goto windowAllocErr;
157579 }
157580
157581 pWin = (Window*)sqlite3DbMallocZero(db: pParse->db, n: sizeof(Window));
157582 if( pWin==0 ) goto windowAllocErr;
157583 pWin->eFrmType = eType;
157584 pWin->eStart = eStart;
157585 pWin->eEnd = eEnd;
157586 if( eExclude==0 && OptimizationDisabled(pParse->db, SQLITE_WindowFunc) ){
157587 eExclude = TK_NO;
157588 }
157589 pWin->eExclude = eExclude;
157590 pWin->bImplicitFrame = bImplicitFrame;
157591 pWin->pEnd = sqlite3WindowOffsetExpr(pParse, pExpr: pEnd);
157592 pWin->pStart = sqlite3WindowOffsetExpr(pParse, pExpr: pStart);
157593 return pWin;
157594
157595windowAllocErr:
157596 sqlite3ExprDelete(db: pParse->db, p: pEnd);
157597 sqlite3ExprDelete(db: pParse->db, p: pStart);
157598 return 0;
157599}
157600
157601/*
157602** Attach PARTITION and ORDER BY clauses pPartition and pOrderBy to window
157603** pWin. Also, if parameter pBase is not NULL, set pWin->zBase to the
157604** equivalent nul-terminated string.
157605*/
157606SQLITE_PRIVATE Window *sqlite3WindowAssemble(
157607 Parse *pParse,
157608 Window *pWin,
157609 ExprList *pPartition,
157610 ExprList *pOrderBy,
157611 Token *pBase
157612){
157613 if( pWin ){
157614 pWin->pPartition = pPartition;
157615 pWin->pOrderBy = pOrderBy;
157616 if( pBase ){
157617 pWin->zBase = sqlite3DbStrNDup(db: pParse->db, z: pBase->z, n: pBase->n);
157618 }
157619 }else{
157620 sqlite3ExprListDelete(db: pParse->db, pList: pPartition);
157621 sqlite3ExprListDelete(db: pParse->db, pList: pOrderBy);
157622 }
157623 return pWin;
157624}
157625
157626/*
157627** Window *pWin has just been created from a WINDOW clause. Tokne pBase
157628** is the base window. Earlier windows from the same WINDOW clause are
157629** stored in the linked list starting at pWin->pNextWin. This function
157630** either updates *pWin according to the base specification, or else
157631** leaves an error in pParse.
157632*/
157633SQLITE_PRIVATE void sqlite3WindowChain(Parse *pParse, Window *pWin, Window *pList){
157634 if( pWin->zBase ){
157635 sqlite3 *db = pParse->db;
157636 Window *pExist = windowFind(pParse, pList, zName: pWin->zBase);
157637 if( pExist ){
157638 const char *zErr = 0;
157639 /* Check for errors */
157640 if( pWin->pPartition ){
157641 zErr = "PARTITION clause";
157642 }else if( pExist->pOrderBy && pWin->pOrderBy ){
157643 zErr = "ORDER BY clause";
157644 }else if( pExist->bImplicitFrame==0 ){
157645 zErr = "frame specification";
157646 }
157647 if( zErr ){
157648 sqlite3ErrorMsg(pParse,
157649 zFormat: "cannot override %s of window: %s", zErr, pWin->zBase
157650 );
157651 }else{
157652 pWin->pPartition = sqlite3ExprListDup(db, p: pExist->pPartition, flags: 0);
157653 if( pExist->pOrderBy ){
157654 assert( pWin->pOrderBy==0 );
157655 pWin->pOrderBy = sqlite3ExprListDup(db, p: pExist->pOrderBy, flags: 0);
157656 }
157657 sqlite3DbFree(db, p: pWin->zBase);
157658 pWin->zBase = 0;
157659 }
157660 }
157661 }
157662}
157663
157664/*
157665** Attach window object pWin to expression p.
157666*/
157667SQLITE_PRIVATE void sqlite3WindowAttach(Parse *pParse, Expr *p, Window *pWin){
157668 if( p ){
157669 assert( p->op==TK_FUNCTION );
157670 assert( pWin );
157671 p->y.pWin = pWin;
157672 ExprSetProperty(p, EP_WinFunc);
157673 pWin->pOwner = p;
157674 if( (p->flags & EP_Distinct) && pWin->eFrmType!=TK_FILTER ){
157675 sqlite3ErrorMsg(pParse,
157676 zFormat: "DISTINCT is not supported for window functions"
157677 );
157678 }
157679 }else{
157680 sqlite3WindowDelete(db: pParse->db, p: pWin);
157681 }
157682}
157683
157684/*
157685** Possibly link window pWin into the list at pSel->pWin (window functions
157686** to be processed as part of SELECT statement pSel). The window is linked
157687** in if either (a) there are no other windows already linked to this
157688** SELECT, or (b) the windows already linked use a compatible window frame.
157689*/
157690SQLITE_PRIVATE void sqlite3WindowLink(Select *pSel, Window *pWin){
157691 if( pSel ){
157692 if( 0==pSel->pWin || 0==sqlite3WindowCompare(0, pSel->pWin, pWin, 0) ){
157693 pWin->pNextWin = pSel->pWin;
157694 if( pSel->pWin ){
157695 pSel->pWin->ppThis = &pWin->pNextWin;
157696 }
157697 pSel->pWin = pWin;
157698 pWin->ppThis = &pSel->pWin;
157699 }else{
157700 if( sqlite3ExprListCompare(pA: pWin->pPartition, pB: pSel->pWin->pPartition,iTab: -1) ){
157701 pSel->selFlags |= SF_MultiPart;
157702 }
157703 }
157704 }
157705}
157706
157707/*
157708** Return 0 if the two window objects are identical, 1 if they are
157709** different, or 2 if it cannot be determined if the objects are identical
157710** or not. Identical window objects can be processed in a single scan.
157711*/
157712SQLITE_PRIVATE int sqlite3WindowCompare(
157713 const Parse *pParse,
157714 const Window *p1,
157715 const Window *p2,
157716 int bFilter
157717){
157718 int res;
157719 if( NEVER(p1==0) || NEVER(p2==0) ) return 1;
157720 if( p1->eFrmType!=p2->eFrmType ) return 1;
157721 if( p1->eStart!=p2->eStart ) return 1;
157722 if( p1->eEnd!=p2->eEnd ) return 1;
157723 if( p1->eExclude!=p2->eExclude ) return 1;
157724 if( sqlite3ExprCompare(pParse, pA: p1->pStart, pB: p2->pStart, iTab: -1) ) return 1;
157725 if( sqlite3ExprCompare(pParse, pA: p1->pEnd, pB: p2->pEnd, iTab: -1) ) return 1;
157726 if( (res = sqlite3ExprListCompare(pA: p1->pPartition, pB: p2->pPartition, iTab: -1)) ){
157727 return res;
157728 }
157729 if( (res = sqlite3ExprListCompare(pA: p1->pOrderBy, pB: p2->pOrderBy, iTab: -1)) ){
157730 return res;
157731 }
157732 if( bFilter ){
157733 if( (res = sqlite3ExprCompare(pParse, pA: p1->pFilter, pB: p2->pFilter, iTab: -1)) ){
157734 return res;
157735 }
157736 }
157737 return 0;
157738}
157739
157740
157741/*
157742** This is called by code in select.c before it calls sqlite3WhereBegin()
157743** to begin iterating through the sub-query results. It is used to allocate
157744** and initialize registers and cursors used by sqlite3WindowCodeStep().
157745*/
157746SQLITE_PRIVATE void sqlite3WindowCodeInit(Parse *pParse, Select *pSelect){
157747 int nEphExpr = pSelect->pSrc->a[0].pSelect->pEList->nExpr;
157748 Window *pMWin = pSelect->pWin;
157749 Window *pWin;
157750 Vdbe *v = sqlite3GetVdbe(pParse);
157751
157752 sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: pMWin->iEphCsr, p2: nEphExpr);
157753 sqlite3VdbeAddOp2(p: v, OP_OpenDup, p1: pMWin->iEphCsr+1, p2: pMWin->iEphCsr);
157754 sqlite3VdbeAddOp2(p: v, OP_OpenDup, p1: pMWin->iEphCsr+2, p2: pMWin->iEphCsr);
157755 sqlite3VdbeAddOp2(p: v, OP_OpenDup, p1: pMWin->iEphCsr+3, p2: pMWin->iEphCsr);
157756
157757 /* Allocate registers to use for PARTITION BY values, if any. Initialize
157758 ** said registers to NULL. */
157759 if( pMWin->pPartition ){
157760 int nExpr = pMWin->pPartition->nExpr;
157761 pMWin->regPart = pParse->nMem+1;
157762 pParse->nMem += nExpr;
157763 sqlite3VdbeAddOp3(p: v, OP_Null, p1: 0, p2: pMWin->regPart, p3: pMWin->regPart+nExpr-1);
157764 }
157765
157766 pMWin->regOne = ++pParse->nMem;
157767 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 1, p2: pMWin->regOne);
157768
157769 if( pMWin->eExclude ){
157770 pMWin->regStartRowid = ++pParse->nMem;
157771 pMWin->regEndRowid = ++pParse->nMem;
157772 pMWin->csrApp = pParse->nTab++;
157773 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 1, p2: pMWin->regStartRowid);
157774 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: pMWin->regEndRowid);
157775 sqlite3VdbeAddOp2(p: v, OP_OpenDup, p1: pMWin->csrApp, p2: pMWin->iEphCsr);
157776 return;
157777 }
157778
157779 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
157780 FuncDef *p = pWin->pFunc;
157781 if( (p->funcFlags & SQLITE_FUNC_MINMAX) && pWin->eStart!=TK_UNBOUNDED ){
157782 /* The inline versions of min() and max() require a single ephemeral
157783 ** table and 3 registers. The registers are used as follows:
157784 **
157785 ** regApp+0: slot to copy min()/max() argument to for MakeRecord
157786 ** regApp+1: integer value used to ensure keys are unique
157787 ** regApp+2: output of MakeRecord
157788 */
157789 ExprList *pList;
157790 KeyInfo *pKeyInfo;
157791 assert( ExprUseXList(pWin->pOwner) );
157792 pList = pWin->pOwner->x.pList;
157793 pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pList, iStart: 0, nExtra: 0);
157794 pWin->csrApp = pParse->nTab++;
157795 pWin->regApp = pParse->nMem+1;
157796 pParse->nMem += 3;
157797 if( pKeyInfo && pWin->pFunc->zName[1]=='i' ){
157798 assert( pKeyInfo->aSortFlags[0]==0 );
157799 pKeyInfo->aSortFlags[0] = KEYINFO_ORDER_DESC;
157800 }
157801 sqlite3VdbeAddOp2(p: v, OP_OpenEphemeral, p1: pWin->csrApp, p2: 2);
157802 sqlite3VdbeAppendP4(p: v, pP4: pKeyInfo, P4_KEYINFO);
157803 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: pWin->regApp+1);
157804 }
157805 else if( p->zName==nth_valueName || p->zName==first_valueName ){
157806 /* Allocate two registers at pWin->regApp. These will be used to
157807 ** store the start and end index of the current frame. */
157808 pWin->regApp = pParse->nMem+1;
157809 pWin->csrApp = pParse->nTab++;
157810 pParse->nMem += 2;
157811 sqlite3VdbeAddOp2(p: v, OP_OpenDup, p1: pWin->csrApp, p2: pMWin->iEphCsr);
157812 }
157813 else if( p->zName==leadName || p->zName==lagName ){
157814 pWin->csrApp = pParse->nTab++;
157815 sqlite3VdbeAddOp2(p: v, OP_OpenDup, p1: pWin->csrApp, p2: pMWin->iEphCsr);
157816 }
157817 }
157818}
157819
157820#define WINDOW_STARTING_INT 0
157821#define WINDOW_ENDING_INT 1
157822#define WINDOW_NTH_VALUE_INT 2
157823#define WINDOW_STARTING_NUM 3
157824#define WINDOW_ENDING_NUM 4
157825
157826/*
157827** A "PRECEDING <expr>" (eCond==0) or "FOLLOWING <expr>" (eCond==1) or the
157828** value of the second argument to nth_value() (eCond==2) has just been
157829** evaluated and the result left in register reg. This function generates VM
157830** code to check that the value is a non-negative integer and throws an
157831** exception if it is not.
157832*/
157833static void windowCheckValue(Parse *pParse, int reg, int eCond){
157834 static const char *azErr[] = {
157835 "frame starting offset must be a non-negative integer",
157836 "frame ending offset must be a non-negative integer",
157837 "second argument to nth_value must be a positive integer",
157838 "frame starting offset must be a non-negative number",
157839 "frame ending offset must be a non-negative number",
157840 };
157841 static int aOp[] = { OP_Ge, OP_Ge, OP_Gt, OP_Ge, OP_Ge };
157842 Vdbe *v = sqlite3GetVdbe(pParse);
157843 int regZero = sqlite3GetTempReg(pParse);
157844 assert( eCond>=0 && eCond<ArraySize(azErr) );
157845 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: regZero);
157846 if( eCond>=WINDOW_STARTING_NUM ){
157847 int regString = sqlite3GetTempReg(pParse);
157848 sqlite3VdbeAddOp4(p: v, OP_String8, p1: 0, p2: regString, p3: 0, zP4: "", P4_STATIC);
157849 sqlite3VdbeAddOp3(p: v, OP_Ge, p1: regString, p2: sqlite3VdbeCurrentAddr(p: v)+2, p3: reg);
157850 sqlite3VdbeChangeP5(p: v, SQLITE_AFF_NUMERIC|SQLITE_JUMPIFNULL);
157851 VdbeCoverage(v);
157852 assert( eCond==3 || eCond==4 );
157853 VdbeCoverageIf(v, eCond==3);
157854 VdbeCoverageIf(v, eCond==4);
157855 }else{
157856 sqlite3VdbeAddOp2(p: v, OP_MustBeInt, p1: reg, p2: sqlite3VdbeCurrentAddr(p: v)+2);
157857 VdbeCoverage(v);
157858 assert( eCond==0 || eCond==1 || eCond==2 );
157859 VdbeCoverageIf(v, eCond==0);
157860 VdbeCoverageIf(v, eCond==1);
157861 VdbeCoverageIf(v, eCond==2);
157862 }
157863 sqlite3VdbeAddOp3(p: v, op: aOp[eCond], p1: regZero, p2: sqlite3VdbeCurrentAddr(p: v)+2, p3: reg);
157864 sqlite3VdbeChangeP5(p: v, SQLITE_AFF_NUMERIC);
157865 VdbeCoverageNeverNullIf(v, eCond==0); /* NULL case captured by */
157866 VdbeCoverageNeverNullIf(v, eCond==1); /* the OP_MustBeInt */
157867 VdbeCoverageNeverNullIf(v, eCond==2);
157868 VdbeCoverageNeverNullIf(v, eCond==3); /* NULL case caught by */
157869 VdbeCoverageNeverNullIf(v, eCond==4); /* the OP_Ge */
157870 sqlite3MayAbort(pParse);
157871 sqlite3VdbeAddOp2(p: v, OP_Halt, SQLITE_ERROR, OE_Abort);
157872 sqlite3VdbeAppendP4(p: v, pP4: (void*)azErr[eCond], P4_STATIC);
157873 sqlite3ReleaseTempReg(pParse, iReg: regZero);
157874}
157875
157876/*
157877** Return the number of arguments passed to the window-function associated
157878** with the object passed as the only argument to this function.
157879*/
157880static int windowArgCount(Window *pWin){
157881 const ExprList *pList;
157882 assert( ExprUseXList(pWin->pOwner) );
157883 pList = pWin->pOwner->x.pList;
157884 return (pList ? pList->nExpr : 0);
157885}
157886
157887typedef struct WindowCodeArg WindowCodeArg;
157888typedef struct WindowCsrAndReg WindowCsrAndReg;
157889
157890/*
157891** See comments above struct WindowCodeArg.
157892*/
157893struct WindowCsrAndReg {
157894 int csr; /* Cursor number */
157895 int reg; /* First in array of peer values */
157896};
157897
157898/*
157899** A single instance of this structure is allocated on the stack by
157900** sqlite3WindowCodeStep() and a pointer to it passed to the various helper
157901** routines. This is to reduce the number of arguments required by each
157902** helper function.
157903**
157904** regArg:
157905** Each window function requires an accumulator register (just as an
157906** ordinary aggregate function does). This variable is set to the first
157907** in an array of accumulator registers - one for each window function
157908** in the WindowCodeArg.pMWin list.
157909**
157910** eDelete:
157911** The window functions implementation sometimes caches the input rows
157912** that it processes in a temporary table. If it is not zero, this
157913** variable indicates when rows may be removed from the temp table (in
157914** order to reduce memory requirements - it would always be safe just
157915** to leave them there). Possible values for eDelete are:
157916**
157917** WINDOW_RETURN_ROW:
157918** An input row can be discarded after it is returned to the caller.
157919**
157920** WINDOW_AGGINVERSE:
157921** An input row can be discarded after the window functions xInverse()
157922** callbacks have been invoked in it.
157923**
157924** WINDOW_AGGSTEP:
157925** An input row can be discarded after the window functions xStep()
157926** callbacks have been invoked in it.
157927**
157928** start,current,end
157929** Consider a window-frame similar to the following:
157930**
157931** (ORDER BY a, b GROUPS BETWEEN 2 PRECEDING AND 2 FOLLOWING)
157932**
157933** The windows functions implmentation caches the input rows in a temp
157934** table, sorted by "a, b" (it actually populates the cache lazily, and
157935** aggressively removes rows once they are no longer required, but that's
157936** a mere detail). It keeps three cursors open on the temp table. One
157937** (current) that points to the next row to return to the query engine
157938** once its window function values have been calculated. Another (end)
157939** points to the next row to call the xStep() method of each window function
157940** on (so that it is 2 groups ahead of current). And a third (start) that
157941** points to the next row to call the xInverse() method of each window
157942** function on.
157943**
157944** Each cursor (start, current and end) consists of a VDBE cursor
157945** (WindowCsrAndReg.csr) and an array of registers (starting at
157946** WindowCodeArg.reg) that always contains a copy of the peer values
157947** read from the corresponding cursor.
157948**
157949** Depending on the window-frame in question, all three cursors may not
157950** be required. In this case both WindowCodeArg.csr and reg are set to
157951** 0.
157952*/
157953struct WindowCodeArg {
157954 Parse *pParse; /* Parse context */
157955 Window *pMWin; /* First in list of functions being processed */
157956 Vdbe *pVdbe; /* VDBE object */
157957 int addrGosub; /* OP_Gosub to this address to return one row */
157958 int regGosub; /* Register used with OP_Gosub(addrGosub) */
157959 int regArg; /* First in array of accumulator registers */
157960 int eDelete; /* See above */
157961 int regRowid;
157962
157963 WindowCsrAndReg start;
157964 WindowCsrAndReg current;
157965 WindowCsrAndReg end;
157966};
157967
157968/*
157969** Generate VM code to read the window frames peer values from cursor csr into
157970** an array of registers starting at reg.
157971*/
157972static void windowReadPeerValues(
157973 WindowCodeArg *p,
157974 int csr,
157975 int reg
157976){
157977 Window *pMWin = p->pMWin;
157978 ExprList *pOrderBy = pMWin->pOrderBy;
157979 if( pOrderBy ){
157980 Vdbe *v = sqlite3GetVdbe(pParse: p->pParse);
157981 ExprList *pPart = pMWin->pPartition;
157982 int iColOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0);
157983 int i;
157984 for(i=0; i<pOrderBy->nExpr; i++){
157985 sqlite3VdbeAddOp3(p: v, OP_Column, p1: csr, p2: iColOff+i, p3: reg+i);
157986 }
157987 }
157988}
157989
157990/*
157991** Generate VM code to invoke either xStep() (if bInverse is 0) or
157992** xInverse (if bInverse is non-zero) for each window function in the
157993** linked list starting at pMWin. Or, for built-in window functions
157994** that do not use the standard function API, generate the required
157995** inline VM code.
157996**
157997** If argument csr is greater than or equal to 0, then argument reg is
157998** the first register in an array of registers guaranteed to be large
157999** enough to hold the array of arguments for each function. In this case
158000** the arguments are extracted from the current row of csr into the
158001** array of registers before invoking OP_AggStep or OP_AggInverse
158002**
158003** Or, if csr is less than zero, then the array of registers at reg is
158004** already populated with all columns from the current row of the sub-query.
158005**
158006** If argument regPartSize is non-zero, then it is a register containing the
158007** number of rows in the current partition.
158008*/
158009static void windowAggStep(
158010 WindowCodeArg *p,
158011 Window *pMWin, /* Linked list of window functions */
158012 int csr, /* Read arguments from this cursor */
158013 int bInverse, /* True to invoke xInverse instead of xStep */
158014 int reg /* Array of registers */
158015){
158016 Parse *pParse = p->pParse;
158017 Vdbe *v = sqlite3GetVdbe(pParse);
158018 Window *pWin;
158019 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
158020 FuncDef *pFunc = pWin->pFunc;
158021 int regArg;
158022 int nArg = pWin->bExprArgs ? 0 : windowArgCount(pWin);
158023 int i;
158024
158025 assert( bInverse==0 || pWin->eStart!=TK_UNBOUNDED );
158026
158027 /* All OVER clauses in the same window function aggregate step must
158028 ** be the same. */
158029 assert( pWin==pMWin || sqlite3WindowCompare(pParse,pWin,pMWin,0)!=1 );
158030
158031 for(i=0; i<nArg; i++){
158032 if( i!=1 || pFunc->zName!=nth_valueName ){
158033 sqlite3VdbeAddOp3(p: v, OP_Column, p1: csr, p2: pWin->iArgCol+i, p3: reg+i);
158034 }else{
158035 sqlite3VdbeAddOp3(p: v, OP_Column, p1: pMWin->iEphCsr, p2: pWin->iArgCol+i, p3: reg+i);
158036 }
158037 }
158038 regArg = reg;
158039
158040 if( pMWin->regStartRowid==0
158041 && (pFunc->funcFlags & SQLITE_FUNC_MINMAX)
158042 && (pWin->eStart!=TK_UNBOUNDED)
158043 ){
158044 int addrIsNull = sqlite3VdbeAddOp1(p: v, OP_IsNull, p1: regArg);
158045 VdbeCoverage(v);
158046 if( bInverse==0 ){
158047 sqlite3VdbeAddOp2(p: v, OP_AddImm, p1: pWin->regApp+1, p2: 1);
158048 sqlite3VdbeAddOp2(p: v, OP_SCopy, p1: regArg, p2: pWin->regApp);
158049 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: pWin->regApp, p2: 2, p3: pWin->regApp+2);
158050 sqlite3VdbeAddOp2(p: v, OP_IdxInsert, p1: pWin->csrApp, p2: pWin->regApp+2);
158051 }else{
158052 sqlite3VdbeAddOp4Int(p: v, OP_SeekGE, p1: pWin->csrApp, p2: 0, p3: regArg, p4: 1);
158053 VdbeCoverageNeverTaken(v);
158054 sqlite3VdbeAddOp1(p: v, OP_Delete, p1: pWin->csrApp);
158055 sqlite3VdbeJumpHere(p: v, addr: sqlite3VdbeCurrentAddr(p: v)-2);
158056 }
158057 sqlite3VdbeJumpHere(p: v, addr: addrIsNull);
158058 }else if( pWin->regApp ){
158059 assert( pFunc->zName==nth_valueName
158060 || pFunc->zName==first_valueName
158061 );
158062 assert( bInverse==0 || bInverse==1 );
158063 sqlite3VdbeAddOp2(p: v, OP_AddImm, p1: pWin->regApp+1-bInverse, p2: 1);
158064 }else if( pFunc->xSFunc!=noopStepFunc ){
158065 int addrIf = 0;
158066 if( pWin->pFilter ){
158067 int regTmp;
158068 assert( ExprUseXList(pWin->pOwner) );
158069 assert( pWin->bExprArgs || !nArg ||nArg==pWin->pOwner->x.pList->nExpr );
158070 assert( pWin->bExprArgs || nArg ||pWin->pOwner->x.pList==0 );
158071 regTmp = sqlite3GetTempReg(pParse);
158072 sqlite3VdbeAddOp3(p: v, OP_Column, p1: csr, p2: pWin->iArgCol+nArg,p3: regTmp);
158073 addrIf = sqlite3VdbeAddOp3(p: v, OP_IfNot, p1: regTmp, p2: 0, p3: 1);
158074 VdbeCoverage(v);
158075 sqlite3ReleaseTempReg(pParse, iReg: regTmp);
158076 }
158077
158078 if( pWin->bExprArgs ){
158079 int iOp = sqlite3VdbeCurrentAddr(p: v);
158080 int iEnd;
158081
158082 assert( ExprUseXList(pWin->pOwner) );
158083 nArg = pWin->pOwner->x.pList->nExpr;
158084 regArg = sqlite3GetTempRange(pParse, nReg: nArg);
158085 sqlite3ExprCodeExprList(pParse, pList: pWin->pOwner->x.pList, target: regArg, srcReg: 0, flags: 0);
158086
158087 for(iEnd=sqlite3VdbeCurrentAddr(p: v); iOp<iEnd; iOp++){
158088 VdbeOp *pOp = sqlite3VdbeGetOp(p: v, addr: iOp);
158089 if( pOp->opcode==OP_Column && pOp->p1==pWin->iEphCsr ){
158090 pOp->p1 = csr;
158091 }
158092 }
158093 }
158094 if( pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
158095 CollSeq *pColl;
158096 assert( nArg>0 );
158097 assert( ExprUseXList(pWin->pOwner) );
158098 pColl = sqlite3ExprNNCollSeq(pParse, pExpr: pWin->pOwner->x.pList->a[0].pExpr);
158099 sqlite3VdbeAddOp4(p: v, OP_CollSeq, p1: 0,p2: 0,p3: 0, zP4: (const char*)pColl, P4_COLLSEQ);
158100 }
158101 sqlite3VdbeAddOp3(p: v, op: bInverse? OP_AggInverse : OP_AggStep,
158102 p1: bInverse, p2: regArg, p3: pWin->regAccum);
158103 sqlite3VdbeAppendP4(p: v, pP4: pFunc, P4_FUNCDEF);
158104 sqlite3VdbeChangeP5(p: v, p5: (u8)nArg);
158105 if( pWin->bExprArgs ){
158106 sqlite3ReleaseTempRange(pParse, iReg: regArg, nReg: nArg);
158107 }
158108 if( addrIf ) sqlite3VdbeJumpHere(p: v, addr: addrIf);
158109 }
158110 }
158111}
158112
158113/*
158114** Values that may be passed as the second argument to windowCodeOp().
158115*/
158116#define WINDOW_RETURN_ROW 1
158117#define WINDOW_AGGINVERSE 2
158118#define WINDOW_AGGSTEP 3
158119
158120/*
158121** Generate VM code to invoke either xValue() (bFin==0) or xFinalize()
158122** (bFin==1) for each window function in the linked list starting at
158123** pMWin. Or, for built-in window-functions that do not use the standard
158124** API, generate the equivalent VM code.
158125*/
158126static void windowAggFinal(WindowCodeArg *p, int bFin){
158127 Parse *pParse = p->pParse;
158128 Window *pMWin = p->pMWin;
158129 Vdbe *v = sqlite3GetVdbe(pParse);
158130 Window *pWin;
158131
158132 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
158133 if( pMWin->regStartRowid==0
158134 && (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX)
158135 && (pWin->eStart!=TK_UNBOUNDED)
158136 ){
158137 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: pWin->regResult);
158138 sqlite3VdbeAddOp1(p: v, OP_Last, p1: pWin->csrApp);
158139 VdbeCoverage(v);
158140 sqlite3VdbeAddOp3(p: v, OP_Column, p1: pWin->csrApp, p2: 0, p3: pWin->regResult);
158141 sqlite3VdbeJumpHere(p: v, addr: sqlite3VdbeCurrentAddr(p: v)-2);
158142 }else if( pWin->regApp ){
158143 assert( pMWin->regStartRowid==0 );
158144 }else{
158145 int nArg = windowArgCount(pWin);
158146 if( bFin ){
158147 sqlite3VdbeAddOp2(p: v, OP_AggFinal, p1: pWin->regAccum, p2: nArg);
158148 sqlite3VdbeAppendP4(p: v, pP4: pWin->pFunc, P4_FUNCDEF);
158149 sqlite3VdbeAddOp2(p: v, OP_Copy, p1: pWin->regAccum, p2: pWin->regResult);
158150 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: pWin->regAccum);
158151 }else{
158152 sqlite3VdbeAddOp3(p: v, OP_AggValue,p1: pWin->regAccum,p2: nArg,p3: pWin->regResult);
158153 sqlite3VdbeAppendP4(p: v, pP4: pWin->pFunc, P4_FUNCDEF);
158154 }
158155 }
158156 }
158157}
158158
158159/*
158160** Generate code to calculate the current values of all window functions in the
158161** p->pMWin list by doing a full scan of the current window frame. Store the
158162** results in the Window.regResult registers, ready to return the upper
158163** layer.
158164*/
158165static void windowFullScan(WindowCodeArg *p){
158166 Window *pWin;
158167 Parse *pParse = p->pParse;
158168 Window *pMWin = p->pMWin;
158169 Vdbe *v = p->pVdbe;
158170
158171 int regCRowid = 0; /* Current rowid value */
158172 int regCPeer = 0; /* Current peer values */
158173 int regRowid = 0; /* AggStep rowid value */
158174 int regPeer = 0; /* AggStep peer values */
158175
158176 int nPeer;
158177 int lblNext;
158178 int lblBrk;
158179 int addrNext;
158180 int csr;
158181
158182 VdbeModuleComment((v, "windowFullScan begin"));
158183
158184 assert( pMWin!=0 );
158185 csr = pMWin->csrApp;
158186 nPeer = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
158187
158188 lblNext = sqlite3VdbeMakeLabel(pParse);
158189 lblBrk = sqlite3VdbeMakeLabel(pParse);
158190
158191 regCRowid = sqlite3GetTempReg(pParse);
158192 regRowid = sqlite3GetTempReg(pParse);
158193 if( nPeer ){
158194 regCPeer = sqlite3GetTempRange(pParse, nReg: nPeer);
158195 regPeer = sqlite3GetTempRange(pParse, nReg: nPeer);
158196 }
158197
158198 sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: pMWin->iEphCsr, p2: regCRowid);
158199 windowReadPeerValues(p, csr: pMWin->iEphCsr, reg: regCPeer);
158200
158201 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
158202 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: pWin->regAccum);
158203 }
158204
158205 sqlite3VdbeAddOp3(p: v, OP_SeekGE, p1: csr, p2: lblBrk, p3: pMWin->regStartRowid);
158206 VdbeCoverage(v);
158207 addrNext = sqlite3VdbeCurrentAddr(p: v);
158208 sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: csr, p2: regRowid);
158209 sqlite3VdbeAddOp3(p: v, OP_Gt, p1: pMWin->regEndRowid, p2: lblBrk, p3: regRowid);
158210 VdbeCoverageNeverNull(v);
158211
158212 if( pMWin->eExclude==TK_CURRENT ){
158213 sqlite3VdbeAddOp3(p: v, OP_Eq, p1: regCRowid, p2: lblNext, p3: regRowid);
158214 VdbeCoverageNeverNull(v);
158215 }else if( pMWin->eExclude!=TK_NO ){
158216 int addr;
158217 int addrEq = 0;
158218 KeyInfo *pKeyInfo = 0;
158219
158220 if( pMWin->pOrderBy ){
158221 pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pList: pMWin->pOrderBy, iStart: 0, nExtra: 0);
158222 }
158223 if( pMWin->eExclude==TK_TIES ){
158224 addrEq = sqlite3VdbeAddOp3(p: v, OP_Eq, p1: regCRowid, p2: 0, p3: regRowid);
158225 VdbeCoverageNeverNull(v);
158226 }
158227 if( pKeyInfo ){
158228 windowReadPeerValues(p, csr, reg: regPeer);
158229 sqlite3VdbeAddOp3(p: v, OP_Compare, p1: regPeer, p2: regCPeer, p3: nPeer);
158230 sqlite3VdbeAppendP4(p: v, pP4: (void*)pKeyInfo, P4_KEYINFO);
158231 addr = sqlite3VdbeCurrentAddr(p: v)+1;
158232 sqlite3VdbeAddOp3(p: v, OP_Jump, p1: addr, p2: lblNext, p3: addr);
158233 VdbeCoverageEqNe(v);
158234 }else{
158235 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: lblNext);
158236 }
158237 if( addrEq ) sqlite3VdbeJumpHere(p: v, addr: addrEq);
158238 }
158239
158240 windowAggStep(p, pMWin, csr, bInverse: 0, reg: p->regArg);
158241
158242 sqlite3VdbeResolveLabel(v, x: lblNext);
158243 sqlite3VdbeAddOp2(p: v, OP_Next, p1: csr, p2: addrNext);
158244 VdbeCoverage(v);
158245 sqlite3VdbeJumpHere(p: v, addr: addrNext-1);
158246 sqlite3VdbeJumpHere(p: v, addr: addrNext+1);
158247 sqlite3ReleaseTempReg(pParse, iReg: regRowid);
158248 sqlite3ReleaseTempReg(pParse, iReg: regCRowid);
158249 if( nPeer ){
158250 sqlite3ReleaseTempRange(pParse, iReg: regPeer, nReg: nPeer);
158251 sqlite3ReleaseTempRange(pParse, iReg: regCPeer, nReg: nPeer);
158252 }
158253
158254 windowAggFinal(p, bFin: 1);
158255 VdbeModuleComment((v, "windowFullScan end"));
158256}
158257
158258/*
158259** Invoke the sub-routine at regGosub (generated by code in select.c) to
158260** return the current row of Window.iEphCsr. If all window functions are
158261** aggregate window functions that use the standard API, a single
158262** OP_Gosub instruction is all that this routine generates. Extra VM code
158263** for per-row processing is only generated for the following built-in window
158264** functions:
158265**
158266** nth_value()
158267** first_value()
158268** lag()
158269** lead()
158270*/
158271static void windowReturnOneRow(WindowCodeArg *p){
158272 Window *pMWin = p->pMWin;
158273 Vdbe *v = p->pVdbe;
158274
158275 if( pMWin->regStartRowid ){
158276 windowFullScan(p);
158277 }else{
158278 Parse *pParse = p->pParse;
158279 Window *pWin;
158280
158281 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
158282 FuncDef *pFunc = pWin->pFunc;
158283 assert( ExprUseXList(pWin->pOwner) );
158284 if( pFunc->zName==nth_valueName
158285 || pFunc->zName==first_valueName
158286 ){
158287 int csr = pWin->csrApp;
158288 int lbl = sqlite3VdbeMakeLabel(pParse);
158289 int tmpReg = sqlite3GetTempReg(pParse);
158290 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: pWin->regResult);
158291
158292 if( pFunc->zName==nth_valueName ){
158293 sqlite3VdbeAddOp3(p: v, OP_Column,p1: pMWin->iEphCsr,p2: pWin->iArgCol+1,p3: tmpReg);
158294 windowCheckValue(pParse, reg: tmpReg, eCond: 2);
158295 }else{
158296 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 1, p2: tmpReg);
158297 }
158298 sqlite3VdbeAddOp3(p: v, OP_Add, p1: tmpReg, p2: pWin->regApp, p3: tmpReg);
158299 sqlite3VdbeAddOp3(p: v, OP_Gt, p1: pWin->regApp+1, p2: lbl, p3: tmpReg);
158300 VdbeCoverageNeverNull(v);
158301 sqlite3VdbeAddOp3(p: v, OP_SeekRowid, p1: csr, p2: 0, p3: tmpReg);
158302 VdbeCoverageNeverTaken(v);
158303 sqlite3VdbeAddOp3(p: v, OP_Column, p1: csr, p2: pWin->iArgCol, p3: pWin->regResult);
158304 sqlite3VdbeResolveLabel(v, x: lbl);
158305 sqlite3ReleaseTempReg(pParse, iReg: tmpReg);
158306 }
158307 else if( pFunc->zName==leadName || pFunc->zName==lagName ){
158308 int nArg = pWin->pOwner->x.pList->nExpr;
158309 int csr = pWin->csrApp;
158310 int lbl = sqlite3VdbeMakeLabel(pParse);
158311 int tmpReg = sqlite3GetTempReg(pParse);
158312 int iEph = pMWin->iEphCsr;
158313
158314 if( nArg<3 ){
158315 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: pWin->regResult);
158316 }else{
158317 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iEph,p2: pWin->iArgCol+2,p3: pWin->regResult);
158318 }
158319 sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: iEph, p2: tmpReg);
158320 if( nArg<2 ){
158321 int val = (pFunc->zName==leadName ? 1 : -1);
158322 sqlite3VdbeAddOp2(p: v, OP_AddImm, p1: tmpReg, p2: val);
158323 }else{
158324 int op = (pFunc->zName==leadName ? OP_Add : OP_Subtract);
158325 int tmpReg2 = sqlite3GetTempReg(pParse);
158326 sqlite3VdbeAddOp3(p: v, OP_Column, p1: iEph, p2: pWin->iArgCol+1, p3: tmpReg2);
158327 sqlite3VdbeAddOp3(p: v, op, p1: tmpReg2, p2: tmpReg, p3: tmpReg);
158328 sqlite3ReleaseTempReg(pParse, iReg: tmpReg2);
158329 }
158330
158331 sqlite3VdbeAddOp3(p: v, OP_SeekRowid, p1: csr, p2: lbl, p3: tmpReg);
158332 VdbeCoverage(v);
158333 sqlite3VdbeAddOp3(p: v, OP_Column, p1: csr, p2: pWin->iArgCol, p3: pWin->regResult);
158334 sqlite3VdbeResolveLabel(v, x: lbl);
158335 sqlite3ReleaseTempReg(pParse, iReg: tmpReg);
158336 }
158337 }
158338 }
158339 sqlite3VdbeAddOp2(p: v, OP_Gosub, p1: p->regGosub, p2: p->addrGosub);
158340}
158341
158342/*
158343** Generate code to set the accumulator register for each window function
158344** in the linked list passed as the second argument to NULL. And perform
158345** any equivalent initialization required by any built-in window functions
158346** in the list.
158347*/
158348static int windowInitAccum(Parse *pParse, Window *pMWin){
158349 Vdbe *v = sqlite3GetVdbe(pParse);
158350 int regArg;
158351 int nArg = 0;
158352 Window *pWin;
158353 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
158354 FuncDef *pFunc = pWin->pFunc;
158355 assert( pWin->regAccum );
158356 sqlite3VdbeAddOp2(p: v, OP_Null, p1: 0, p2: pWin->regAccum);
158357 nArg = MAX(nArg, windowArgCount(pWin));
158358 if( pMWin->regStartRowid==0 ){
158359 if( pFunc->zName==nth_valueName || pFunc->zName==first_valueName ){
158360 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: pWin->regApp);
158361 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: pWin->regApp+1);
158362 }
158363
158364 if( (pFunc->funcFlags & SQLITE_FUNC_MINMAX) && pWin->csrApp ){
158365 assert( pWin->eStart!=TK_UNBOUNDED );
158366 sqlite3VdbeAddOp1(p: v, OP_ResetSorter, p1: pWin->csrApp);
158367 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: pWin->regApp+1);
158368 }
158369 }
158370 }
158371 regArg = pParse->nMem+1;
158372 pParse->nMem += nArg;
158373 return regArg;
158374}
158375
158376/*
158377** Return true if the current frame should be cached in the ephemeral table,
158378** even if there are no xInverse() calls required.
158379*/
158380static int windowCacheFrame(Window *pMWin){
158381 Window *pWin;
158382 if( pMWin->regStartRowid ) return 1;
158383 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
158384 FuncDef *pFunc = pWin->pFunc;
158385 if( (pFunc->zName==nth_valueName)
158386 || (pFunc->zName==first_valueName)
158387 || (pFunc->zName==leadName)
158388 || (pFunc->zName==lagName)
158389 ){
158390 return 1;
158391 }
158392 }
158393 return 0;
158394}
158395
158396/*
158397** regOld and regNew are each the first register in an array of size
158398** pOrderBy->nExpr. This function generates code to compare the two
158399** arrays of registers using the collation sequences and other comparison
158400** parameters specified by pOrderBy.
158401**
158402** If the two arrays are not equal, the contents of regNew is copied to
158403** regOld and control falls through. Otherwise, if the contents of the arrays
158404** are equal, an OP_Goto is executed. The address of the OP_Goto is returned.
158405*/
158406static void windowIfNewPeer(
158407 Parse *pParse,
158408 ExprList *pOrderBy,
158409 int regNew, /* First in array of new values */
158410 int regOld, /* First in array of old values */
158411 int addr /* Jump here */
158412){
158413 Vdbe *v = sqlite3GetVdbe(pParse);
158414 if( pOrderBy ){
158415 int nVal = pOrderBy->nExpr;
158416 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pList: pOrderBy, iStart: 0, nExtra: 0);
158417 sqlite3VdbeAddOp3(p: v, OP_Compare, p1: regOld, p2: regNew, p3: nVal);
158418 sqlite3VdbeAppendP4(p: v, pP4: (void*)pKeyInfo, P4_KEYINFO);
158419 sqlite3VdbeAddOp3(p: v, OP_Jump,
158420 p1: sqlite3VdbeCurrentAddr(p: v)+1, p2: addr, p3: sqlite3VdbeCurrentAddr(p: v)+1
158421 );
158422 VdbeCoverageEqNe(v);
158423 sqlite3VdbeAddOp3(p: v, OP_Copy, p1: regNew, p2: regOld, p3: nVal-1);
158424 }else{
158425 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: addr);
158426 }
158427}
158428
158429/*
158430** This function is called as part of generating VM programs for RANGE
158431** offset PRECEDING/FOLLOWING frame boundaries. Assuming "ASC" order for
158432** the ORDER BY term in the window, and that argument op is OP_Ge, it generates
158433** code equivalent to:
158434**
158435** if( csr1.peerVal + regVal >= csr2.peerVal ) goto lbl;
158436**
158437** The value of parameter op may also be OP_Gt or OP_Le. In these cases the
158438** operator in the above pseudo-code is replaced with ">" or "<=", respectively.
158439**
158440** If the sort-order for the ORDER BY term in the window is DESC, then the
158441** comparison is reversed. Instead of adding regVal to csr1.peerVal, it is
158442** subtracted. And the comparison operator is inverted to - ">=" becomes "<=",
158443** ">" becomes "<", and so on. So, with DESC sort order, if the argument op
158444** is OP_Ge, the generated code is equivalent to:
158445**
158446** if( csr1.peerVal - regVal <= csr2.peerVal ) goto lbl;
158447**
158448** A special type of arithmetic is used such that if csr1.peerVal is not
158449** a numeric type (real or integer), then the result of the addition
158450** or subtraction is a a copy of csr1.peerVal.
158451*/
158452static void windowCodeRangeTest(
158453 WindowCodeArg *p,
158454 int op, /* OP_Ge, OP_Gt, or OP_Le */
158455 int csr1, /* Cursor number for cursor 1 */
158456 int regVal, /* Register containing non-negative number */
158457 int csr2, /* Cursor number for cursor 2 */
158458 int lbl /* Jump destination if condition is true */
158459){
158460 Parse *pParse = p->pParse;
158461 Vdbe *v = sqlite3GetVdbe(pParse);
158462 ExprList *pOrderBy = p->pMWin->pOrderBy; /* ORDER BY clause for window */
158463 int reg1 = sqlite3GetTempReg(pParse); /* Reg. for csr1.peerVal+regVal */
158464 int reg2 = sqlite3GetTempReg(pParse); /* Reg. for csr2.peerVal */
158465 int regString = ++pParse->nMem; /* Reg. for constant value '' */
158466 int arith = OP_Add; /* OP_Add or OP_Subtract */
158467 int addrGe; /* Jump destination */
158468 int addrDone = sqlite3VdbeMakeLabel(pParse); /* Address past OP_Ge */
158469 CollSeq *pColl;
158470
158471 /* Read the peer-value from each cursor into a register */
158472 windowReadPeerValues(p, csr: csr1, reg: reg1);
158473 windowReadPeerValues(p, csr: csr2, reg: reg2);
158474
158475 assert( op==OP_Ge || op==OP_Gt || op==OP_Le );
158476 assert( pOrderBy && pOrderBy->nExpr==1 );
158477 if( pOrderBy->a[0].sortFlags & KEYINFO_ORDER_DESC ){
158478 switch( op ){
158479 case OP_Ge: op = OP_Le; break;
158480 case OP_Gt: op = OP_Lt; break;
158481 default: assert( op==OP_Le ); op = OP_Ge; break;
158482 }
158483 arith = OP_Subtract;
158484 }
158485
158486 VdbeModuleComment((v, "CodeRangeTest: if( R%d %s R%d %s R%d ) goto lbl",
158487 reg1, (arith==OP_Add ? "+" : "-"), regVal,
158488 ((op==OP_Ge) ? ">=" : (op==OP_Le) ? "<=" : (op==OP_Gt) ? ">" : "<"), reg2
158489 ));
158490
158491 /* If the BIGNULL flag is set for the ORDER BY, then it is required to
158492 ** consider NULL values to be larger than all other values, instead of
158493 ** the usual smaller. The VDBE opcodes OP_Ge and so on do not handle this
158494 ** (and adding that capability causes a performance regression), so
158495 ** instead if the BIGNULL flag is set then cases where either reg1 or
158496 ** reg2 are NULL are handled separately in the following block. The code
158497 ** generated is equivalent to:
158498 **
158499 ** if( reg1 IS NULL ){
158500 ** if( op==OP_Ge ) goto lbl;
158501 ** if( op==OP_Gt && reg2 IS NOT NULL ) goto lbl;
158502 ** if( op==OP_Le && reg2 IS NULL ) goto lbl;
158503 ** }else if( reg2 IS NULL ){
158504 ** if( op==OP_Le ) goto lbl;
158505 ** }
158506 **
158507 ** Additionally, if either reg1 or reg2 are NULL but the jump to lbl is
158508 ** not taken, control jumps over the comparison operator coded below this
158509 ** block. */
158510 if( pOrderBy->a[0].sortFlags & KEYINFO_ORDER_BIGNULL ){
158511 /* This block runs if reg1 contains a NULL. */
158512 int addr = sqlite3VdbeAddOp1(p: v, OP_NotNull, p1: reg1); VdbeCoverage(v);
158513 switch( op ){
158514 case OP_Ge:
158515 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: lbl);
158516 break;
158517 case OP_Gt:
158518 sqlite3VdbeAddOp2(p: v, OP_NotNull, p1: reg2, p2: lbl);
158519 VdbeCoverage(v);
158520 break;
158521 case OP_Le:
158522 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: reg2, p2: lbl);
158523 VdbeCoverage(v);
158524 break;
158525 default: assert( op==OP_Lt ); /* no-op */ break;
158526 }
158527 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: addrDone);
158528
158529 /* This block runs if reg1 is not NULL, but reg2 is. */
158530 sqlite3VdbeJumpHere(p: v, addr);
158531 sqlite3VdbeAddOp2(p: v, OP_IsNull, p1: reg2, p2: lbl); VdbeCoverage(v);
158532 if( op==OP_Gt || op==OP_Ge ){
158533 sqlite3VdbeChangeP2(p: v, addr: -1, val: addrDone);
158534 }
158535 }
158536
158537 /* Register reg1 currently contains csr1.peerVal (the peer-value from csr1).
158538 ** This block adds (or subtracts for DESC) the numeric value in regVal
158539 ** from it. Or, if reg1 is not numeric (it is a NULL, a text value or a blob),
158540 ** then leave reg1 as it is. In pseudo-code, this is implemented as:
158541 **
158542 ** if( reg1>='' ) goto addrGe;
158543 ** reg1 = reg1 +/- regVal
158544 ** addrGe:
158545 **
158546 ** Since all strings and blobs are greater-than-or-equal-to an empty string,
158547 ** the add/subtract is skipped for these, as required. If reg1 is a NULL,
158548 ** then the arithmetic is performed, but since adding or subtracting from
158549 ** NULL is always NULL anyway, this case is handled as required too. */
158550 sqlite3VdbeAddOp4(p: v, OP_String8, p1: 0, p2: regString, p3: 0, zP4: "", P4_STATIC);
158551 addrGe = sqlite3VdbeAddOp3(p: v, OP_Ge, p1: regString, p2: 0, p3: reg1);
158552 VdbeCoverage(v);
158553 if( (op==OP_Ge && arith==OP_Add) || (op==OP_Le && arith==OP_Subtract) ){
158554 sqlite3VdbeAddOp3(p: v, op, p1: reg2, p2: lbl, p3: reg1); VdbeCoverage(v);
158555 }
158556 sqlite3VdbeAddOp3(p: v, op: arith, p1: regVal, p2: reg1, p3: reg1);
158557 sqlite3VdbeJumpHere(p: v, addr: addrGe);
158558
158559 /* Compare registers reg2 and reg1, taking the jump if required. Note that
158560 ** control skips over this test if the BIGNULL flag is set and either
158561 ** reg1 or reg2 contain a NULL value. */
158562 sqlite3VdbeAddOp3(p: v, op, p1: reg2, p2: lbl, p3: reg1); VdbeCoverage(v);
158563 pColl = sqlite3ExprNNCollSeq(pParse, pExpr: pOrderBy->a[0].pExpr);
158564 sqlite3VdbeAppendP4(p: v, pP4: (void*)pColl, P4_COLLSEQ);
158565 sqlite3VdbeChangeP5(p: v, SQLITE_NULLEQ);
158566 sqlite3VdbeResolveLabel(v, x: addrDone);
158567
158568 assert( op==OP_Ge || op==OP_Gt || op==OP_Lt || op==OP_Le );
158569 testcase(op==OP_Ge); VdbeCoverageIf(v, op==OP_Ge);
158570 testcase(op==OP_Lt); VdbeCoverageIf(v, op==OP_Lt);
158571 testcase(op==OP_Le); VdbeCoverageIf(v, op==OP_Le);
158572 testcase(op==OP_Gt); VdbeCoverageIf(v, op==OP_Gt);
158573 sqlite3ReleaseTempReg(pParse, iReg: reg1);
158574 sqlite3ReleaseTempReg(pParse, iReg: reg2);
158575
158576 VdbeModuleComment((v, "CodeRangeTest: end"));
158577}
158578
158579/*
158580** Helper function for sqlite3WindowCodeStep(). Each call to this function
158581** generates VM code for a single RETURN_ROW, AGGSTEP or AGGINVERSE
158582** operation. Refer to the header comment for sqlite3WindowCodeStep() for
158583** details.
158584*/
158585static int windowCodeOp(
158586 WindowCodeArg *p, /* Context object */
158587 int op, /* WINDOW_RETURN_ROW, AGGSTEP or AGGINVERSE */
158588 int regCountdown, /* Register for OP_IfPos countdown */
158589 int jumpOnEof /* Jump here if stepped cursor reaches EOF */
158590){
158591 int csr, reg;
158592 Parse *pParse = p->pParse;
158593 Window *pMWin = p->pMWin;
158594 int ret = 0;
158595 Vdbe *v = p->pVdbe;
158596 int addrContinue = 0;
158597 int bPeer = (pMWin->eFrmType!=TK_ROWS);
158598
158599 int lblDone = sqlite3VdbeMakeLabel(pParse);
158600 int addrNextRange = 0;
158601
158602 /* Special case - WINDOW_AGGINVERSE is always a no-op if the frame
158603 ** starts with UNBOUNDED PRECEDING. */
158604 if( op==WINDOW_AGGINVERSE && pMWin->eStart==TK_UNBOUNDED ){
158605 assert( regCountdown==0 && jumpOnEof==0 );
158606 return 0;
158607 }
158608
158609 if( regCountdown>0 ){
158610 if( pMWin->eFrmType==TK_RANGE ){
158611 addrNextRange = sqlite3VdbeCurrentAddr(p: v);
158612 assert( op==WINDOW_AGGINVERSE || op==WINDOW_AGGSTEP );
158613 if( op==WINDOW_AGGINVERSE ){
158614 if( pMWin->eStart==TK_FOLLOWING ){
158615 windowCodeRangeTest(
158616 p, OP_Le, csr1: p->current.csr, regVal: regCountdown, csr2: p->start.csr, lbl: lblDone
158617 );
158618 }else{
158619 windowCodeRangeTest(
158620 p, OP_Ge, csr1: p->start.csr, regVal: regCountdown, csr2: p->current.csr, lbl: lblDone
158621 );
158622 }
158623 }else{
158624 windowCodeRangeTest(
158625 p, OP_Gt, csr1: p->end.csr, regVal: regCountdown, csr2: p->current.csr, lbl: lblDone
158626 );
158627 }
158628 }else{
158629 sqlite3VdbeAddOp3(p: v, OP_IfPos, p1: regCountdown, p2: lblDone, p3: 1);
158630 VdbeCoverage(v);
158631 }
158632 }
158633
158634 if( op==WINDOW_RETURN_ROW && pMWin->regStartRowid==0 ){
158635 windowAggFinal(p, bFin: 0);
158636 }
158637 addrContinue = sqlite3VdbeCurrentAddr(p: v);
158638
158639 /* If this is a (RANGE BETWEEN a FOLLOWING AND b FOLLOWING) or
158640 ** (RANGE BETWEEN b PRECEDING AND a PRECEDING) frame, ensure the
158641 ** start cursor does not advance past the end cursor within the
158642 ** temporary table. It otherwise might, if (a>b). Also ensure that,
158643 ** if the input cursor is still finding new rows, that the end
158644 ** cursor does not go past it to EOF. */
158645 if( pMWin->eStart==pMWin->eEnd && regCountdown
158646 && pMWin->eFrmType==TK_RANGE
158647 ){
158648 int regRowid1 = sqlite3GetTempReg(pParse);
158649 int regRowid2 = sqlite3GetTempReg(pParse);
158650 if( op==WINDOW_AGGINVERSE ){
158651 sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: p->start.csr, p2: regRowid1);
158652 sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: p->end.csr, p2: regRowid2);
158653 sqlite3VdbeAddOp3(p: v, OP_Ge, p1: regRowid2, p2: lblDone, p3: regRowid1);
158654 VdbeCoverage(v);
158655 }else if( p->regRowid ){
158656 sqlite3VdbeAddOp2(p: v, OP_Rowid, p1: p->end.csr, p2: regRowid1);
158657 sqlite3VdbeAddOp3(p: v, OP_Ge, p1: p->regRowid, p2: lblDone, p3: regRowid1);
158658 VdbeCoverageNeverNull(v);
158659 }
158660 sqlite3ReleaseTempReg(pParse, iReg: regRowid1);
158661 sqlite3ReleaseTempReg(pParse, iReg: regRowid2);
158662 assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING );
158663 }
158664
158665 switch( op ){
158666 case WINDOW_RETURN_ROW:
158667 csr = p->current.csr;
158668 reg = p->current.reg;
158669 windowReturnOneRow(p);
158670 break;
158671
158672 case WINDOW_AGGINVERSE:
158673 csr = p->start.csr;
158674 reg = p->start.reg;
158675 if( pMWin->regStartRowid ){
158676 assert( pMWin->regEndRowid );
158677 sqlite3VdbeAddOp2(p: v, OP_AddImm, p1: pMWin->regStartRowid, p2: 1);
158678 }else{
158679 windowAggStep(p, pMWin, csr, bInverse: 1, reg: p->regArg);
158680 }
158681 break;
158682
158683 default:
158684 assert( op==WINDOW_AGGSTEP );
158685 csr = p->end.csr;
158686 reg = p->end.reg;
158687 if( pMWin->regStartRowid ){
158688 assert( pMWin->regEndRowid );
158689 sqlite3VdbeAddOp2(p: v, OP_AddImm, p1: pMWin->regEndRowid, p2: 1);
158690 }else{
158691 windowAggStep(p, pMWin, csr, bInverse: 0, reg: p->regArg);
158692 }
158693 break;
158694 }
158695
158696 if( op==p->eDelete ){
158697 sqlite3VdbeAddOp1(p: v, OP_Delete, p1: csr);
158698 sqlite3VdbeChangeP5(p: v, OPFLAG_SAVEPOSITION);
158699 }
158700
158701 if( jumpOnEof ){
158702 sqlite3VdbeAddOp2(p: v, OP_Next, p1: csr, p2: sqlite3VdbeCurrentAddr(p: v)+2);
158703 VdbeCoverage(v);
158704 ret = sqlite3VdbeAddOp0(p: v, OP_Goto);
158705 }else{
158706 sqlite3VdbeAddOp2(p: v, OP_Next, p1: csr, p2: sqlite3VdbeCurrentAddr(p: v)+1+bPeer);
158707 VdbeCoverage(v);
158708 if( bPeer ){
158709 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: lblDone);
158710 }
158711 }
158712
158713 if( bPeer ){
158714 int nReg = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
158715 int regTmp = (nReg ? sqlite3GetTempRange(pParse, nReg) : 0);
158716 windowReadPeerValues(p, csr, reg: regTmp);
158717 windowIfNewPeer(pParse, pOrderBy: pMWin->pOrderBy, regNew: regTmp, regOld: reg, addr: addrContinue);
158718 sqlite3ReleaseTempRange(pParse, iReg: regTmp, nReg);
158719 }
158720
158721 if( addrNextRange ){
158722 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: addrNextRange);
158723 }
158724 sqlite3VdbeResolveLabel(v, x: lblDone);
158725 return ret;
158726}
158727
158728
158729/*
158730** Allocate and return a duplicate of the Window object indicated by the
158731** third argument. Set the Window.pOwner field of the new object to
158732** pOwner.
158733*/
158734SQLITE_PRIVATE Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p){
158735 Window *pNew = 0;
158736 if( ALWAYS(p) ){
158737 pNew = sqlite3DbMallocZero(db, n: sizeof(Window));
158738 if( pNew ){
158739 pNew->zName = sqlite3DbStrDup(db, z: p->zName);
158740 pNew->zBase = sqlite3DbStrDup(db, z: p->zBase);
158741 pNew->pFilter = sqlite3ExprDup(db, p: p->pFilter, flags: 0);
158742 pNew->pFunc = p->pFunc;
158743 pNew->pPartition = sqlite3ExprListDup(db, p: p->pPartition, flags: 0);
158744 pNew->pOrderBy = sqlite3ExprListDup(db, p: p->pOrderBy, flags: 0);
158745 pNew->eFrmType = p->eFrmType;
158746 pNew->eEnd = p->eEnd;
158747 pNew->eStart = p->eStart;
158748 pNew->eExclude = p->eExclude;
158749 pNew->regResult = p->regResult;
158750 pNew->regAccum = p->regAccum;
158751 pNew->iArgCol = p->iArgCol;
158752 pNew->iEphCsr = p->iEphCsr;
158753 pNew->bExprArgs = p->bExprArgs;
158754 pNew->pStart = sqlite3ExprDup(db, p: p->pStart, flags: 0);
158755 pNew->pEnd = sqlite3ExprDup(db, p: p->pEnd, flags: 0);
158756 pNew->pOwner = pOwner;
158757 pNew->bImplicitFrame = p->bImplicitFrame;
158758 }
158759 }
158760 return pNew;
158761}
158762
158763/*
158764** Return a copy of the linked list of Window objects passed as the
158765** second argument.
158766*/
158767SQLITE_PRIVATE Window *sqlite3WindowListDup(sqlite3 *db, Window *p){
158768 Window *pWin;
158769 Window *pRet = 0;
158770 Window **pp = &pRet;
158771
158772 for(pWin=p; pWin; pWin=pWin->pNextWin){
158773 *pp = sqlite3WindowDup(db, pOwner: 0, p: pWin);
158774 if( *pp==0 ) break;
158775 pp = &((*pp)->pNextWin);
158776 }
158777
158778 return pRet;
158779}
158780
158781/*
158782** Return true if it can be determined at compile time that expression
158783** pExpr evaluates to a value that, when cast to an integer, is greater
158784** than zero. False otherwise.
158785**
158786** If an OOM error occurs, this function sets the Parse.db.mallocFailed
158787** flag and returns zero.
158788*/
158789static int windowExprGtZero(Parse *pParse, Expr *pExpr){
158790 int ret = 0;
158791 sqlite3 *db = pParse->db;
158792 sqlite3_value *pVal = 0;
158793 sqlite3ValueFromExpr(db, pExpr, enc: db->enc, SQLITE_AFF_NUMERIC, ppVal: &pVal);
158794 if( pVal && sqlite3_value_int(pVal)>0 ){
158795 ret = 1;
158796 }
158797 sqlite3ValueFree(v: pVal);
158798 return ret;
158799}
158800
158801/*
158802** sqlite3WhereBegin() has already been called for the SELECT statement
158803** passed as the second argument when this function is invoked. It generates
158804** code to populate the Window.regResult register for each window function
158805** and invoke the sub-routine at instruction addrGosub once for each row.
158806** sqlite3WhereEnd() is always called before returning.
158807**
158808** This function handles several different types of window frames, which
158809** require slightly different processing. The following pseudo code is
158810** used to implement window frames of the form:
158811**
158812** ROWS BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
158813**
158814** Other window frame types use variants of the following:
158815**
158816** ... loop started by sqlite3WhereBegin() ...
158817** if( new partition ){
158818** Gosub flush
158819** }
158820** Insert new row into eph table.
158821**
158822** if( first row of partition ){
158823** // Rewind three cursors, all open on the eph table.
158824** Rewind(csrEnd);
158825** Rewind(csrStart);
158826** Rewind(csrCurrent);
158827**
158828** regEnd = <expr2> // FOLLOWING expression
158829** regStart = <expr1> // PRECEDING expression
158830** }else{
158831** // First time this branch is taken, the eph table contains two
158832** // rows. The first row in the partition, which all three cursors
158833** // currently point to, and the following row.
158834** AGGSTEP
158835** if( (regEnd--)<=0 ){
158836** RETURN_ROW
158837** if( (regStart--)<=0 ){
158838** AGGINVERSE
158839** }
158840** }
158841** }
158842** }
158843** flush:
158844** AGGSTEP
158845** while( 1 ){
158846** RETURN ROW
158847** if( csrCurrent is EOF ) break;
158848** if( (regStart--)<=0 ){
158849** AggInverse(csrStart)
158850** Next(csrStart)
158851** }
158852** }
158853**
158854** The pseudo-code above uses the following shorthand:
158855**
158856** AGGSTEP: invoke the aggregate xStep() function for each window function
158857** with arguments read from the current row of cursor csrEnd, then
158858** step cursor csrEnd forward one row (i.e. sqlite3BtreeNext()).
158859**
158860** RETURN_ROW: return a row to the caller based on the contents of the
158861** current row of csrCurrent and the current state of all
158862** aggregates. Then step cursor csrCurrent forward one row.
158863**
158864** AGGINVERSE: invoke the aggregate xInverse() function for each window
158865** functions with arguments read from the current row of cursor
158866** csrStart. Then step csrStart forward one row.
158867**
158868** There are two other ROWS window frames that are handled significantly
158869** differently from the above - "BETWEEN <expr> PRECEDING AND <expr> PRECEDING"
158870** and "BETWEEN <expr> FOLLOWING AND <expr> FOLLOWING". These are special
158871** cases because they change the order in which the three cursors (csrStart,
158872** csrCurrent and csrEnd) iterate through the ephemeral table. Cases that
158873** use UNBOUNDED or CURRENT ROW are much simpler variations on one of these
158874** three.
158875**
158876** ROWS BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
158877**
158878** ... loop started by sqlite3WhereBegin() ...
158879** if( new partition ){
158880** Gosub flush
158881** }
158882** Insert new row into eph table.
158883** if( first row of partition ){
158884** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
158885** regEnd = <expr2>
158886** regStart = <expr1>
158887** }else{
158888** if( (regEnd--)<=0 ){
158889** AGGSTEP
158890** }
158891** RETURN_ROW
158892** if( (regStart--)<=0 ){
158893** AGGINVERSE
158894** }
158895** }
158896** }
158897** flush:
158898** if( (regEnd--)<=0 ){
158899** AGGSTEP
158900** }
158901** RETURN_ROW
158902**
158903**
158904** ROWS BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
158905**
158906** ... loop started by sqlite3WhereBegin() ...
158907** if( new partition ){
158908** Gosub flush
158909** }
158910** Insert new row into eph table.
158911** if( first row of partition ){
158912** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
158913** regEnd = <expr2>
158914** regStart = regEnd - <expr1>
158915** }else{
158916** AGGSTEP
158917** if( (regEnd--)<=0 ){
158918** RETURN_ROW
158919** }
158920** if( (regStart--)<=0 ){
158921** AGGINVERSE
158922** }
158923** }
158924** }
158925** flush:
158926** AGGSTEP
158927** while( 1 ){
158928** if( (regEnd--)<=0 ){
158929** RETURN_ROW
158930** if( eof ) break;
158931** }
158932** if( (regStart--)<=0 ){
158933** AGGINVERSE
158934** if( eof ) break
158935** }
158936** }
158937** while( !eof csrCurrent ){
158938** RETURN_ROW
158939** }
158940**
158941** For the most part, the patterns above are adapted to support UNBOUNDED by
158942** assuming that it is equivalent to "infinity PRECEDING/FOLLOWING" and
158943** CURRENT ROW by assuming that it is equivilent to "0 PRECEDING/FOLLOWING".
158944** This is optimized of course - branches that will never be taken and
158945** conditions that are always true are omitted from the VM code. The only
158946** exceptional case is:
158947**
158948** ROWS BETWEEN <expr1> FOLLOWING AND UNBOUNDED FOLLOWING
158949**
158950** ... loop started by sqlite3WhereBegin() ...
158951** if( new partition ){
158952** Gosub flush
158953** }
158954** Insert new row into eph table.
158955** if( first row of partition ){
158956** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
158957** regStart = <expr1>
158958** }else{
158959** AGGSTEP
158960** }
158961** }
158962** flush:
158963** AGGSTEP
158964** while( 1 ){
158965** if( (regStart--)<=0 ){
158966** AGGINVERSE
158967** if( eof ) break
158968** }
158969** RETURN_ROW
158970** }
158971** while( !eof csrCurrent ){
158972** RETURN_ROW
158973** }
158974**
158975** Also requiring special handling are the cases:
158976**
158977** ROWS BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
158978** ROWS BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
158979**
158980** when (expr1 < expr2). This is detected at runtime, not by this function.
158981** To handle this case, the pseudo-code programs depicted above are modified
158982** slightly to be:
158983**
158984** ... loop started by sqlite3WhereBegin() ...
158985** if( new partition ){
158986** Gosub flush
158987** }
158988** Insert new row into eph table.
158989** if( first row of partition ){
158990** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
158991** regEnd = <expr2>
158992** regStart = <expr1>
158993** if( regEnd < regStart ){
158994** RETURN_ROW
158995** delete eph table contents
158996** continue
158997** }
158998** ...
158999**
159000** The new "continue" statement in the above jumps to the next iteration
159001** of the outer loop - the one started by sqlite3WhereBegin().
159002**
159003** The various GROUPS cases are implemented using the same patterns as
159004** ROWS. The VM code is modified slightly so that:
159005**
159006** 1. The else branch in the main loop is only taken if the row just
159007** added to the ephemeral table is the start of a new group. In
159008** other words, it becomes:
159009**
159010** ... loop started by sqlite3WhereBegin() ...
159011** if( new partition ){
159012** Gosub flush
159013** }
159014** Insert new row into eph table.
159015** if( first row of partition ){
159016** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
159017** regEnd = <expr2>
159018** regStart = <expr1>
159019** }else if( new group ){
159020** ...
159021** }
159022** }
159023**
159024** 2. Instead of processing a single row, each RETURN_ROW, AGGSTEP or
159025** AGGINVERSE step processes the current row of the relevant cursor and
159026** all subsequent rows belonging to the same group.
159027**
159028** RANGE window frames are a little different again. As for GROUPS, the
159029** main loop runs once per group only. And RETURN_ROW, AGGSTEP and AGGINVERSE
159030** deal in groups instead of rows. As for ROWS and GROUPS, there are three
159031** basic cases:
159032**
159033** RANGE BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
159034**
159035** ... loop started by sqlite3WhereBegin() ...
159036** if( new partition ){
159037** Gosub flush
159038** }
159039** Insert new row into eph table.
159040** if( first row of partition ){
159041** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
159042** regEnd = <expr2>
159043** regStart = <expr1>
159044** }else{
159045** AGGSTEP
159046** while( (csrCurrent.key + regEnd) < csrEnd.key ){
159047** RETURN_ROW
159048** while( csrStart.key + regStart) < csrCurrent.key ){
159049** AGGINVERSE
159050** }
159051** }
159052** }
159053** }
159054** flush:
159055** AGGSTEP
159056** while( 1 ){
159057** RETURN ROW
159058** if( csrCurrent is EOF ) break;
159059** while( csrStart.key + regStart) < csrCurrent.key ){
159060** AGGINVERSE
159061** }
159062** }
159063** }
159064**
159065** In the above notation, "csr.key" means the current value of the ORDER BY
159066** expression (there is only ever 1 for a RANGE that uses an <expr> FOLLOWING
159067** or <expr PRECEDING) read from cursor csr.
159068**
159069** RANGE BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
159070**
159071** ... loop started by sqlite3WhereBegin() ...
159072** if( new partition ){
159073** Gosub flush
159074** }
159075** Insert new row into eph table.
159076** if( first row of partition ){
159077** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
159078** regEnd = <expr2>
159079** regStart = <expr1>
159080** }else{
159081** while( (csrEnd.key + regEnd) <= csrCurrent.key ){
159082** AGGSTEP
159083** }
159084** while( (csrStart.key + regStart) < csrCurrent.key ){
159085** AGGINVERSE
159086** }
159087** RETURN_ROW
159088** }
159089** }
159090** flush:
159091** while( (csrEnd.key + regEnd) <= csrCurrent.key ){
159092** AGGSTEP
159093** }
159094** while( (csrStart.key + regStart) < csrCurrent.key ){
159095** AGGINVERSE
159096** }
159097** RETURN_ROW
159098**
159099** RANGE BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
159100**
159101** ... loop started by sqlite3WhereBegin() ...
159102** if( new partition ){
159103** Gosub flush
159104** }
159105** Insert new row into eph table.
159106** if( first row of partition ){
159107** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
159108** regEnd = <expr2>
159109** regStart = <expr1>
159110** }else{
159111** AGGSTEP
159112** while( (csrCurrent.key + regEnd) < csrEnd.key ){
159113** while( (csrCurrent.key + regStart) > csrStart.key ){
159114** AGGINVERSE
159115** }
159116** RETURN_ROW
159117** }
159118** }
159119** }
159120** flush:
159121** AGGSTEP
159122** while( 1 ){
159123** while( (csrCurrent.key + regStart) > csrStart.key ){
159124** AGGINVERSE
159125** if( eof ) break "while( 1 )" loop.
159126** }
159127** RETURN_ROW
159128** }
159129** while( !eof csrCurrent ){
159130** RETURN_ROW
159131** }
159132**
159133** The text above leaves out many details. Refer to the code and comments
159134** below for a more complete picture.
159135*/
159136SQLITE_PRIVATE void sqlite3WindowCodeStep(
159137 Parse *pParse, /* Parse context */
159138 Select *p, /* Rewritten SELECT statement */
159139 WhereInfo *pWInfo, /* Context returned by sqlite3WhereBegin() */
159140 int regGosub, /* Register for OP_Gosub */
159141 int addrGosub /* OP_Gosub here to return each row */
159142){
159143 Window *pMWin = p->pWin;
159144 ExprList *pOrderBy = pMWin->pOrderBy;
159145 Vdbe *v = sqlite3GetVdbe(pParse);
159146 int csrWrite; /* Cursor used to write to eph. table */
159147 int csrInput = p->pSrc->a[0].iCursor; /* Cursor of sub-select */
159148 int nInput = p->pSrc->a[0].pTab->nCol; /* Number of cols returned by sub */
159149 int iInput; /* To iterate through sub cols */
159150 int addrNe; /* Address of OP_Ne */
159151 int addrGosubFlush = 0; /* Address of OP_Gosub to flush: */
159152 int addrInteger = 0; /* Address of OP_Integer */
159153 int addrEmpty; /* Address of OP_Rewind in flush: */
159154 int regNew; /* Array of registers holding new input row */
159155 int regRecord; /* regNew array in record form */
159156 int regNewPeer = 0; /* Peer values for new row (part of regNew) */
159157 int regPeer = 0; /* Peer values for current row */
159158 int regFlushPart = 0; /* Register for "Gosub flush_partition" */
159159 WindowCodeArg s; /* Context object for sub-routines */
159160 int lblWhereEnd; /* Label just before sqlite3WhereEnd() code */
159161 int regStart = 0; /* Value of <expr> PRECEDING */
159162 int regEnd = 0; /* Value of <expr> FOLLOWING */
159163
159164 assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_CURRENT
159165 || pMWin->eStart==TK_FOLLOWING || pMWin->eStart==TK_UNBOUNDED
159166 );
159167 assert( pMWin->eEnd==TK_FOLLOWING || pMWin->eEnd==TK_CURRENT
159168 || pMWin->eEnd==TK_UNBOUNDED || pMWin->eEnd==TK_PRECEDING
159169 );
159170 assert( pMWin->eExclude==0 || pMWin->eExclude==TK_CURRENT
159171 || pMWin->eExclude==TK_GROUP || pMWin->eExclude==TK_TIES
159172 || pMWin->eExclude==TK_NO
159173 );
159174
159175 lblWhereEnd = sqlite3VdbeMakeLabel(pParse);
159176
159177 /* Fill in the context object */
159178 memset(s: &s, c: 0, n: sizeof(WindowCodeArg));
159179 s.pParse = pParse;
159180 s.pMWin = pMWin;
159181 s.pVdbe = v;
159182 s.regGosub = regGosub;
159183 s.addrGosub = addrGosub;
159184 s.current.csr = pMWin->iEphCsr;
159185 csrWrite = s.current.csr+1;
159186 s.start.csr = s.current.csr+2;
159187 s.end.csr = s.current.csr+3;
159188
159189 /* Figure out when rows may be deleted from the ephemeral table. There
159190 ** are four options - they may never be deleted (eDelete==0), they may
159191 ** be deleted as soon as they are no longer part of the window frame
159192 ** (eDelete==WINDOW_AGGINVERSE), they may be deleted as after the row
159193 ** has been returned to the caller (WINDOW_RETURN_ROW), or they may
159194 ** be deleted after they enter the frame (WINDOW_AGGSTEP). */
159195 switch( pMWin->eStart ){
159196 case TK_FOLLOWING:
159197 if( pMWin->eFrmType!=TK_RANGE
159198 && windowExprGtZero(pParse, pExpr: pMWin->pStart)
159199 ){
159200 s.eDelete = WINDOW_RETURN_ROW;
159201 }
159202 break;
159203 case TK_UNBOUNDED:
159204 if( windowCacheFrame(pMWin)==0 ){
159205 if( pMWin->eEnd==TK_PRECEDING ){
159206 if( pMWin->eFrmType!=TK_RANGE
159207 && windowExprGtZero(pParse, pExpr: pMWin->pEnd)
159208 ){
159209 s.eDelete = WINDOW_AGGSTEP;
159210 }
159211 }else{
159212 s.eDelete = WINDOW_RETURN_ROW;
159213 }
159214 }
159215 break;
159216 default:
159217 s.eDelete = WINDOW_AGGINVERSE;
159218 break;
159219 }
159220
159221 /* Allocate registers for the array of values from the sub-query, the
159222 ** samve values in record form, and the rowid used to insert said record
159223 ** into the ephemeral table. */
159224 regNew = pParse->nMem+1;
159225 pParse->nMem += nInput;
159226 regRecord = ++pParse->nMem;
159227 s.regRowid = ++pParse->nMem;
159228
159229 /* If the window frame contains an "<expr> PRECEDING" or "<expr> FOLLOWING"
159230 ** clause, allocate registers to store the results of evaluating each
159231 ** <expr>. */
159232 if( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING ){
159233 regStart = ++pParse->nMem;
159234 }
159235 if( pMWin->eEnd==TK_PRECEDING || pMWin->eEnd==TK_FOLLOWING ){
159236 regEnd = ++pParse->nMem;
159237 }
159238
159239 /* If this is not a "ROWS BETWEEN ..." frame, then allocate arrays of
159240 ** registers to store copies of the ORDER BY expressions (peer values)
159241 ** for the main loop, and for each cursor (start, current and end). */
159242 if( pMWin->eFrmType!=TK_ROWS ){
159243 int nPeer = (pOrderBy ? pOrderBy->nExpr : 0);
159244 regNewPeer = regNew + pMWin->nBufferCol;
159245 if( pMWin->pPartition ) regNewPeer += pMWin->pPartition->nExpr;
159246 regPeer = pParse->nMem+1; pParse->nMem += nPeer;
159247 s.start.reg = pParse->nMem+1; pParse->nMem += nPeer;
159248 s.current.reg = pParse->nMem+1; pParse->nMem += nPeer;
159249 s.end.reg = pParse->nMem+1; pParse->nMem += nPeer;
159250 }
159251
159252 /* Load the column values for the row returned by the sub-select
159253 ** into an array of registers starting at regNew. Assemble them into
159254 ** a record in register regRecord. */
159255 for(iInput=0; iInput<nInput; iInput++){
159256 sqlite3VdbeAddOp3(p: v, OP_Column, p1: csrInput, p2: iInput, p3: regNew+iInput);
159257 }
159258 sqlite3VdbeAddOp3(p: v, OP_MakeRecord, p1: regNew, p2: nInput, p3: regRecord);
159259
159260 /* An input row has just been read into an array of registers starting
159261 ** at regNew. If the window has a PARTITION clause, this block generates
159262 ** VM code to check if the input row is the start of a new partition.
159263 ** If so, it does an OP_Gosub to an address to be filled in later. The
159264 ** address of the OP_Gosub is stored in local variable addrGosubFlush. */
159265 if( pMWin->pPartition ){
159266 int addr;
159267 ExprList *pPart = pMWin->pPartition;
159268 int nPart = pPart->nExpr;
159269 int regNewPart = regNew + pMWin->nBufferCol;
159270 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pList: pPart, iStart: 0, nExtra: 0);
159271
159272 regFlushPart = ++pParse->nMem;
159273 addr = sqlite3VdbeAddOp3(p: v, OP_Compare, p1: regNewPart, p2: pMWin->regPart, p3: nPart);
159274 sqlite3VdbeAppendP4(p: v, pP4: (void*)pKeyInfo, P4_KEYINFO);
159275 sqlite3VdbeAddOp3(p: v, OP_Jump, p1: addr+2, p2: addr+4, p3: addr+2);
159276 VdbeCoverageEqNe(v);
159277 addrGosubFlush = sqlite3VdbeAddOp1(p: v, OP_Gosub, p1: regFlushPart);
159278 VdbeComment((v, "call flush_partition"));
159279 sqlite3VdbeAddOp3(p: v, OP_Copy, p1: regNewPart, p2: pMWin->regPart, p3: nPart-1);
159280 }
159281
159282 /* Insert the new row into the ephemeral table */
159283 sqlite3VdbeAddOp2(p: v, OP_NewRowid, p1: csrWrite, p2: s.regRowid);
159284 sqlite3VdbeAddOp3(p: v, OP_Insert, p1: csrWrite, p2: regRecord, p3: s.regRowid);
159285 addrNe = sqlite3VdbeAddOp3(p: v, OP_Ne, p1: pMWin->regOne, p2: 0, p3: s.regRowid);
159286 VdbeCoverageNeverNull(v);
159287
159288 /* This block is run for the first row of each partition */
159289 s.regArg = windowInitAccum(pParse, pMWin);
159290
159291 if( regStart ){
159292 sqlite3ExprCode(pParse, pExpr: pMWin->pStart, target: regStart);
159293 windowCheckValue(pParse, reg: regStart, eCond: 0 + (pMWin->eFrmType==TK_RANGE?3:0));
159294 }
159295 if( regEnd ){
159296 sqlite3ExprCode(pParse, pExpr: pMWin->pEnd, target: regEnd);
159297 windowCheckValue(pParse, reg: regEnd, eCond: 1 + (pMWin->eFrmType==TK_RANGE?3:0));
159298 }
159299
159300 if( pMWin->eFrmType!=TK_RANGE && pMWin->eStart==pMWin->eEnd && regStart ){
159301 int op = ((pMWin->eStart==TK_FOLLOWING) ? OP_Ge : OP_Le);
159302 int addrGe = sqlite3VdbeAddOp3(p: v, op, p1: regStart, p2: 0, p3: regEnd);
159303 VdbeCoverageNeverNullIf(v, op==OP_Ge); /* NeverNull because bound <expr> */
159304 VdbeCoverageNeverNullIf(v, op==OP_Le); /* values previously checked */
159305 windowAggFinal(p: &s, bFin: 0);
159306 sqlite3VdbeAddOp2(p: v, OP_Rewind, p1: s.current.csr, p2: 1);
159307 VdbeCoverageNeverTaken(v);
159308 windowReturnOneRow(p: &s);
159309 sqlite3VdbeAddOp1(p: v, OP_ResetSorter, p1: s.current.csr);
159310 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: lblWhereEnd);
159311 sqlite3VdbeJumpHere(p: v, addr: addrGe);
159312 }
159313 if( pMWin->eStart==TK_FOLLOWING && pMWin->eFrmType!=TK_RANGE && regEnd ){
159314 assert( pMWin->eEnd==TK_FOLLOWING );
159315 sqlite3VdbeAddOp3(p: v, OP_Subtract, p1: regStart, p2: regEnd, p3: regStart);
159316 }
159317
159318 if( pMWin->eStart!=TK_UNBOUNDED ){
159319 sqlite3VdbeAddOp2(p: v, OP_Rewind, p1: s.start.csr, p2: 1);
159320 VdbeCoverageNeverTaken(v);
159321 }
159322 sqlite3VdbeAddOp2(p: v, OP_Rewind, p1: s.current.csr, p2: 1);
159323 VdbeCoverageNeverTaken(v);
159324 sqlite3VdbeAddOp2(p: v, OP_Rewind, p1: s.end.csr, p2: 1);
159325 VdbeCoverageNeverTaken(v);
159326 if( regPeer && pOrderBy ){
159327 sqlite3VdbeAddOp3(p: v, OP_Copy, p1: regNewPeer, p2: regPeer, p3: pOrderBy->nExpr-1);
159328 sqlite3VdbeAddOp3(p: v, OP_Copy, p1: regPeer, p2: s.start.reg, p3: pOrderBy->nExpr-1);
159329 sqlite3VdbeAddOp3(p: v, OP_Copy, p1: regPeer, p2: s.current.reg, p3: pOrderBy->nExpr-1);
159330 sqlite3VdbeAddOp3(p: v, OP_Copy, p1: regPeer, p2: s.end.reg, p3: pOrderBy->nExpr-1);
159331 }
159332
159333 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: lblWhereEnd);
159334
159335 sqlite3VdbeJumpHere(p: v, addr: addrNe);
159336
159337 /* Beginning of the block executed for the second and subsequent rows. */
159338 if( regPeer ){
159339 windowIfNewPeer(pParse, pOrderBy, regNew: regNewPeer, regOld: regPeer, addr: lblWhereEnd);
159340 }
159341 if( pMWin->eStart==TK_FOLLOWING ){
159342 windowCodeOp(p: &s, WINDOW_AGGSTEP, regCountdown: 0, jumpOnEof: 0);
159343 if( pMWin->eEnd!=TK_UNBOUNDED ){
159344 if( pMWin->eFrmType==TK_RANGE ){
159345 int lbl = sqlite3VdbeMakeLabel(pParse);
159346 int addrNext = sqlite3VdbeCurrentAddr(p: v);
159347 windowCodeRangeTest(p: &s, OP_Ge, csr1: s.current.csr, regVal: regEnd, csr2: s.end.csr, lbl);
159348 windowCodeOp(p: &s, WINDOW_AGGINVERSE, regCountdown: regStart, jumpOnEof: 0);
159349 windowCodeOp(p: &s, WINDOW_RETURN_ROW, regCountdown: 0, jumpOnEof: 0);
159350 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: addrNext);
159351 sqlite3VdbeResolveLabel(v, x: lbl);
159352 }else{
159353 windowCodeOp(p: &s, WINDOW_RETURN_ROW, regCountdown: regEnd, jumpOnEof: 0);
159354 windowCodeOp(p: &s, WINDOW_AGGINVERSE, regCountdown: regStart, jumpOnEof: 0);
159355 }
159356 }
159357 }else
159358 if( pMWin->eEnd==TK_PRECEDING ){
159359 int bRPS = (pMWin->eStart==TK_PRECEDING && pMWin->eFrmType==TK_RANGE);
159360 windowCodeOp(p: &s, WINDOW_AGGSTEP, regCountdown: regEnd, jumpOnEof: 0);
159361 if( bRPS ) windowCodeOp(p: &s, WINDOW_AGGINVERSE, regCountdown: regStart, jumpOnEof: 0);
159362 windowCodeOp(p: &s, WINDOW_RETURN_ROW, regCountdown: 0, jumpOnEof: 0);
159363 if( !bRPS ) windowCodeOp(p: &s, WINDOW_AGGINVERSE, regCountdown: regStart, jumpOnEof: 0);
159364 }else{
159365 int addr = 0;
159366 windowCodeOp(p: &s, WINDOW_AGGSTEP, regCountdown: 0, jumpOnEof: 0);
159367 if( pMWin->eEnd!=TK_UNBOUNDED ){
159368 if( pMWin->eFrmType==TK_RANGE ){
159369 int lbl = 0;
159370 addr = sqlite3VdbeCurrentAddr(p: v);
159371 if( regEnd ){
159372 lbl = sqlite3VdbeMakeLabel(pParse);
159373 windowCodeRangeTest(p: &s, OP_Ge, csr1: s.current.csr, regVal: regEnd, csr2: s.end.csr, lbl);
159374 }
159375 windowCodeOp(p: &s, WINDOW_RETURN_ROW, regCountdown: 0, jumpOnEof: 0);
159376 windowCodeOp(p: &s, WINDOW_AGGINVERSE, regCountdown: regStart, jumpOnEof: 0);
159377 if( regEnd ){
159378 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: addr);
159379 sqlite3VdbeResolveLabel(v, x: lbl);
159380 }
159381 }else{
159382 if( regEnd ){
159383 addr = sqlite3VdbeAddOp3(p: v, OP_IfPos, p1: regEnd, p2: 0, p3: 1);
159384 VdbeCoverage(v);
159385 }
159386 windowCodeOp(p: &s, WINDOW_RETURN_ROW, regCountdown: 0, jumpOnEof: 0);
159387 windowCodeOp(p: &s, WINDOW_AGGINVERSE, regCountdown: regStart, jumpOnEof: 0);
159388 if( regEnd ) sqlite3VdbeJumpHere(p: v, addr);
159389 }
159390 }
159391 }
159392
159393 /* End of the main input loop */
159394 sqlite3VdbeResolveLabel(v, x: lblWhereEnd);
159395 sqlite3WhereEnd(pWInfo);
159396
159397 /* Fall through */
159398 if( pMWin->pPartition ){
159399 addrInteger = sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: regFlushPart);
159400 sqlite3VdbeJumpHere(p: v, addr: addrGosubFlush);
159401 }
159402
159403 s.regRowid = 0;
159404 addrEmpty = sqlite3VdbeAddOp1(p: v, OP_Rewind, p1: csrWrite);
159405 VdbeCoverage(v);
159406 if( pMWin->eEnd==TK_PRECEDING ){
159407 int bRPS = (pMWin->eStart==TK_PRECEDING && pMWin->eFrmType==TK_RANGE);
159408 windowCodeOp(p: &s, WINDOW_AGGSTEP, regCountdown: regEnd, jumpOnEof: 0);
159409 if( bRPS ) windowCodeOp(p: &s, WINDOW_AGGINVERSE, regCountdown: regStart, jumpOnEof: 0);
159410 windowCodeOp(p: &s, WINDOW_RETURN_ROW, regCountdown: 0, jumpOnEof: 0);
159411 }else if( pMWin->eStart==TK_FOLLOWING ){
159412 int addrStart;
159413 int addrBreak1;
159414 int addrBreak2;
159415 int addrBreak3;
159416 windowCodeOp(p: &s, WINDOW_AGGSTEP, regCountdown: 0, jumpOnEof: 0);
159417 if( pMWin->eFrmType==TK_RANGE ){
159418 addrStart = sqlite3VdbeCurrentAddr(p: v);
159419 addrBreak2 = windowCodeOp(p: &s, WINDOW_AGGINVERSE, regCountdown: regStart, jumpOnEof: 1);
159420 addrBreak1 = windowCodeOp(p: &s, WINDOW_RETURN_ROW, regCountdown: 0, jumpOnEof: 1);
159421 }else
159422 if( pMWin->eEnd==TK_UNBOUNDED ){
159423 addrStart = sqlite3VdbeCurrentAddr(p: v);
159424 addrBreak1 = windowCodeOp(p: &s, WINDOW_RETURN_ROW, regCountdown: regStart, jumpOnEof: 1);
159425 addrBreak2 = windowCodeOp(p: &s, WINDOW_AGGINVERSE, regCountdown: 0, jumpOnEof: 1);
159426 }else{
159427 assert( pMWin->eEnd==TK_FOLLOWING );
159428 addrStart = sqlite3VdbeCurrentAddr(p: v);
159429 addrBreak1 = windowCodeOp(p: &s, WINDOW_RETURN_ROW, regCountdown: regEnd, jumpOnEof: 1);
159430 addrBreak2 = windowCodeOp(p: &s, WINDOW_AGGINVERSE, regCountdown: regStart, jumpOnEof: 1);
159431 }
159432 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: addrStart);
159433 sqlite3VdbeJumpHere(p: v, addr: addrBreak2);
159434 addrStart = sqlite3VdbeCurrentAddr(p: v);
159435 addrBreak3 = windowCodeOp(p: &s, WINDOW_RETURN_ROW, regCountdown: 0, jumpOnEof: 1);
159436 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: addrStart);
159437 sqlite3VdbeJumpHere(p: v, addr: addrBreak1);
159438 sqlite3VdbeJumpHere(p: v, addr: addrBreak3);
159439 }else{
159440 int addrBreak;
159441 int addrStart;
159442 windowCodeOp(p: &s, WINDOW_AGGSTEP, regCountdown: 0, jumpOnEof: 0);
159443 addrStart = sqlite3VdbeCurrentAddr(p: v);
159444 addrBreak = windowCodeOp(p: &s, WINDOW_RETURN_ROW, regCountdown: 0, jumpOnEof: 1);
159445 windowCodeOp(p: &s, WINDOW_AGGINVERSE, regCountdown: regStart, jumpOnEof: 0);
159446 sqlite3VdbeAddOp2(p: v, OP_Goto, p1: 0, p2: addrStart);
159447 sqlite3VdbeJumpHere(p: v, addr: addrBreak);
159448 }
159449 sqlite3VdbeJumpHere(p: v, addr: addrEmpty);
159450
159451 sqlite3VdbeAddOp1(p: v, OP_ResetSorter, p1: s.current.csr);
159452 if( pMWin->pPartition ){
159453 if( pMWin->regStartRowid ){
159454 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 1, p2: pMWin->regStartRowid);
159455 sqlite3VdbeAddOp2(p: v, OP_Integer, p1: 0, p2: pMWin->regEndRowid);
159456 }
159457 sqlite3VdbeChangeP1(p: v, addr: addrInteger, val: sqlite3VdbeCurrentAddr(p: v));
159458 sqlite3VdbeAddOp1(p: v, OP_Return, p1: regFlushPart);
159459 }
159460}
159461
159462#endif /* SQLITE_OMIT_WINDOWFUNC */
159463
159464/************** End of window.c **********************************************/
159465/************** Begin file parse.c *******************************************/
159466/* This file is automatically generated by Lemon from input grammar
159467** source file "parse.y". */
159468/*
159469** 2001-09-15
159470**
159471** The author disclaims copyright to this source code. In place of
159472** a legal notice, here is a blessing:
159473**
159474** May you do good and not evil.
159475** May you find forgiveness for yourself and forgive others.
159476** May you share freely, never taking more than you give.
159477**
159478*************************************************************************
159479** This file contains SQLite's SQL parser.
159480**
159481** The canonical source code to this file ("parse.y") is a Lemon grammar
159482** file that specifies the input grammar and actions to take while parsing.
159483** That input file is processed by Lemon to generate a C-language
159484** implementation of a parser for the given grammer. You might be reading
159485** this comment as part of the translated C-code. Edits should be made
159486** to the original parse.y sources.
159487*/
159488
159489/* #include "sqliteInt.h" */
159490
159491/*
159492** Disable all error recovery processing in the parser push-down
159493** automaton.
159494*/
159495#define YYNOERRORRECOVERY 1
159496
159497/*
159498** Make yytestcase() the same as testcase()
159499*/
159500#define yytestcase(X) testcase(X)
159501
159502/*
159503** Indicate that sqlite3ParserFree() will never be called with a null
159504** pointer.
159505*/
159506#define YYPARSEFREENEVERNULL 1
159507
159508/*
159509** In the amalgamation, the parse.c file generated by lemon and the
159510** tokenize.c file are concatenated. In that case, sqlite3RunParser()
159511** has access to the the size of the yyParser object and so the parser
159512** engine can be allocated from stack. In that case, only the
159513** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked
159514** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be
159515** omitted.
159516*/
159517#ifdef SQLITE_AMALGAMATION
159518# define sqlite3Parser_ENGINEALWAYSONSTACK 1
159519#endif
159520
159521/*
159522** Alternative datatype for the argument to the malloc() routine passed
159523** into sqlite3ParserAlloc(). The default is size_t.
159524*/
159525#define YYMALLOCARGTYPE u64
159526
159527/*
159528** An instance of the following structure describes the event of a
159529** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT,
159530** TK_DELETE, or TK_INSTEAD. If the event is of the form
159531**
159532** UPDATE ON (a,b,c)
159533**
159534** Then the "b" IdList records the list "a,b,c".
159535*/
159536struct TrigEvent { int a; IdList * b; };
159537
159538struct FrameBound { int eType; Expr *pExpr; };
159539
159540/*
159541** Disable lookaside memory allocation for objects that might be
159542** shared across database connections.
159543*/
159544static void disableLookaside(Parse *pParse){
159545 sqlite3 *db = pParse->db;
159546 pParse->disableLookaside++;
159547 DisableLookaside;
159548}
159549
159550#if !defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) \
159551 && defined(SQLITE_UDL_CAPABLE_PARSER)
159552/*
159553** Issue an error message if an ORDER BY or LIMIT clause occurs on an
159554** UPDATE or DELETE statement.
159555*/
159556static void updateDeleteLimitError(
159557 Parse *pParse,
159558 ExprList *pOrderBy,
159559 Expr *pLimit
159560){
159561 if( pOrderBy ){
159562 sqlite3ErrorMsg(pParse, "syntax error near \"ORDER BY\"");
159563 }else{
159564 sqlite3ErrorMsg(pParse, "syntax error near \"LIMIT\"");
159565 }
159566 sqlite3ExprListDelete(pParse->db, pOrderBy);
159567 sqlite3ExprDelete(pParse->db, pLimit);
159568}
159569#endif /* SQLITE_ENABLE_UPDATE_DELETE_LIMIT */
159570
159571
159572 /*
159573 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
159574 ** all elements in the list. And make sure list length does not exceed
159575 ** SQLITE_LIMIT_COMPOUND_SELECT.
159576 */
159577 static void parserDoubleLinkSelect(Parse *pParse, Select *p){
159578 assert( p!=0 );
159579 if( p->pPrior ){
159580 Select *pNext = 0, *pLoop = p;
159581 int mxSelect, cnt = 1;
159582 while(1){
159583 pLoop->pNext = pNext;
159584 pLoop->selFlags |= SF_Compound;
159585 pNext = pLoop;
159586 pLoop = pLoop->pPrior;
159587 if( pLoop==0 ) break;
159588 cnt++;
159589 if( pLoop->pOrderBy || pLoop->pLimit ){
159590 sqlite3ErrorMsg(pParse,zFormat: "%s clause should come after %s not before",
159591 pLoop->pOrderBy!=0 ? "ORDER BY" : "LIMIT",
159592 sqlite3SelectOpName(id: pNext->op));
159593 break;
159594 }
159595 }
159596 if( (p->selFlags & SF_MultiValue)==0 &&
159597 (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
159598 cnt>mxSelect
159599 ){
159600 sqlite3ErrorMsg(pParse, zFormat: "too many terms in compound SELECT");
159601 }
159602 }
159603 }
159604
159605 /* Attach a With object describing the WITH clause to a Select
159606 ** object describing the query for which the WITH clause is a prefix.
159607 */
159608 static Select *attachWithToSelect(Parse *pParse, Select *pSelect, With *pWith){
159609 if( pSelect ){
159610 pSelect->pWith = pWith;
159611 parserDoubleLinkSelect(pParse, p: pSelect);
159612 }else{
159613 sqlite3WithDelete(db: pParse->db, pWith);
159614 }
159615 return pSelect;
159616 }
159617
159618
159619 /* Construct a new Expr object from a single identifier. Use the
159620 ** new Expr to populate pOut. Set the span of pOut to be the identifier
159621 ** that created the expression.
159622 */
159623 static Expr *tokenExpr(Parse *pParse, int op, Token t){
159624 Expr *p = sqlite3DbMallocRawNN(db: pParse->db, n: sizeof(Expr)+t.n+1);
159625 if( p ){
159626 /* memset(p, 0, sizeof(Expr)); */
159627 p->op = (u8)op;
159628 p->affExpr = 0;
159629 p->flags = EP_Leaf;
159630 ExprClearVVAProperties(p);
159631 p->iAgg = -1;
159632 p->pLeft = p->pRight = 0;
159633 p->pAggInfo = 0;
159634 memset(s: &p->x, c: 0, n: sizeof(p->x));
159635 memset(s: &p->y, c: 0, n: sizeof(p->y));
159636 p->op2 = 0;
159637 p->iTable = 0;
159638 p->iColumn = 0;
159639 p->u.zToken = (char*)&p[1];
159640 memcpy(dest: p->u.zToken, src: t.z, n: t.n);
159641 p->u.zToken[t.n] = 0;
159642 if( sqlite3Isquote(p->u.zToken[0]) ){
159643 sqlite3DequoteExpr(p);
159644 }
159645#if SQLITE_MAX_EXPR_DEPTH>0
159646 p->nHeight = 1;
159647#endif
159648 if( IN_RENAME_OBJECT ){
159649 return (Expr*)sqlite3RenameTokenMap(pParse, pPtr: (void*)p, pToken: &t);
159650 }
159651 }
159652 return p;
159653 }
159654
159655
159656 /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
159657 ** unary TK_ISNULL or TK_NOTNULL expression. */
159658 static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
159659 sqlite3 *db = pParse->db;
159660 if( pA && pY && pY->op==TK_NULL && !IN_RENAME_OBJECT ){
159661 pA->op = (u8)op;
159662 sqlite3ExprDelete(db, p: pA->pRight);
159663 pA->pRight = 0;
159664 }
159665 }
159666
159667 /* Add a single new term to an ExprList that is used to store a
159668 ** list of identifiers. Report an error if the ID list contains
159669 ** a COLLATE clause or an ASC or DESC keyword, except ignore the
159670 ** error while parsing a legacy schema.
159671 */
159672 static ExprList *parserAddExprIdListTerm(
159673 Parse *pParse,
159674 ExprList *pPrior,
159675 Token *pIdToken,
159676 int hasCollate,
159677 int sortOrder
159678 ){
159679 ExprList *p = sqlite3ExprListAppend(pParse, pList: pPrior, pExpr: 0);
159680 if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED)
159681 && pParse->db->init.busy==0
159682 ){
159683 sqlite3ErrorMsg(pParse, zFormat: "syntax error after column name \"%.*s\"",
159684 pIdToken->n, pIdToken->z);
159685 }
159686 sqlite3ExprListSetName(pParse, pList: p, pName: pIdToken, dequote: 1);
159687 return p;
159688 }
159689
159690#if TK_SPAN>255
159691# error too many tokens in the grammar
159692#endif
159693/**************** End of %include directives **********************************/
159694/* These constants specify the various numeric values for terminal symbols.
159695***************** Begin token definitions *************************************/
159696#ifndef TK_SEMI
159697#define TK_SEMI 1
159698#define TK_EXPLAIN 2
159699#define TK_QUERY 3
159700#define TK_PLAN 4
159701#define TK_BEGIN 5
159702#define TK_TRANSACTION 6
159703#define TK_DEFERRED 7
159704#define TK_IMMEDIATE 8
159705#define TK_EXCLUSIVE 9
159706#define TK_COMMIT 10
159707#define TK_END 11
159708#define TK_ROLLBACK 12
159709#define TK_SAVEPOINT 13
159710#define TK_RELEASE 14
159711#define TK_TO 15
159712#define TK_TABLE 16
159713#define TK_CREATE 17
159714#define TK_IF 18
159715#define TK_NOT 19
159716#define TK_EXISTS 20
159717#define TK_TEMP 21
159718#define TK_LP 22
159719#define TK_RP 23
159720#define TK_AS 24
159721#define TK_COMMA 25
159722#define TK_WITHOUT 26
159723#define TK_ABORT 27
159724#define TK_ACTION 28
159725#define TK_AFTER 29
159726#define TK_ANALYZE 30
159727#define TK_ASC 31
159728#define TK_ATTACH 32
159729#define TK_BEFORE 33
159730#define TK_BY 34
159731#define TK_CASCADE 35
159732#define TK_CAST 36
159733#define TK_CONFLICT 37
159734#define TK_DATABASE 38
159735#define TK_DESC 39
159736#define TK_DETACH 40
159737#define TK_EACH 41
159738#define TK_FAIL 42
159739#define TK_OR 43
159740#define TK_AND 44
159741#define TK_IS 45
159742#define TK_MATCH 46
159743#define TK_LIKE_KW 47
159744#define TK_BETWEEN 48
159745#define TK_IN 49
159746#define TK_ISNULL 50
159747#define TK_NOTNULL 51
159748#define TK_NE 52
159749#define TK_EQ 53
159750#define TK_GT 54
159751#define TK_LE 55
159752#define TK_LT 56
159753#define TK_GE 57
159754#define TK_ESCAPE 58
159755#define TK_ID 59
159756#define TK_COLUMNKW 60
159757#define TK_DO 61
159758#define TK_FOR 62
159759#define TK_IGNORE 63
159760#define TK_INITIALLY 64
159761#define TK_INSTEAD 65
159762#define TK_NO 66
159763#define TK_KEY 67
159764#define TK_OF 68
159765#define TK_OFFSET 69
159766#define TK_PRAGMA 70
159767#define TK_RAISE 71
159768#define TK_RECURSIVE 72
159769#define TK_REPLACE 73
159770#define TK_RESTRICT 74
159771#define TK_ROW 75
159772#define TK_ROWS 76
159773#define TK_TRIGGER 77
159774#define TK_VACUUM 78
159775#define TK_VIEW 79
159776#define TK_VIRTUAL 80
159777#define TK_WITH 81
159778#define TK_NULLS 82
159779#define TK_FIRST 83
159780#define TK_LAST 84
159781#define TK_CURRENT 85
159782#define TK_FOLLOWING 86
159783#define TK_PARTITION 87
159784#define TK_PRECEDING 88
159785#define TK_RANGE 89
159786#define TK_UNBOUNDED 90
159787#define TK_EXCLUDE 91
159788#define TK_GROUPS 92
159789#define TK_OTHERS 93
159790#define TK_TIES 94
159791#define TK_GENERATED 95
159792#define TK_ALWAYS 96
159793#define TK_MATERIALIZED 97
159794#define TK_REINDEX 98
159795#define TK_RENAME 99
159796#define TK_CTIME_KW 100
159797#define TK_ANY 101
159798#define TK_BITAND 102
159799#define TK_BITOR 103
159800#define TK_LSHIFT 104
159801#define TK_RSHIFT 105
159802#define TK_PLUS 106
159803#define TK_MINUS 107
159804#define TK_STAR 108
159805#define TK_SLASH 109
159806#define TK_REM 110
159807#define TK_CONCAT 111
159808#define TK_COLLATE 112
159809#define TK_BITNOT 113
159810#define TK_ON 114
159811#define TK_INDEXED 115
159812#define TK_STRING 116
159813#define TK_JOIN_KW 117
159814#define TK_CONSTRAINT 118
159815#define TK_DEFAULT 119
159816#define TK_NULL 120
159817#define TK_PRIMARY 121
159818#define TK_UNIQUE 122
159819#define TK_CHECK 123
159820#define TK_REFERENCES 124
159821#define TK_AUTOINCR 125
159822#define TK_INSERT 126
159823#define TK_DELETE 127
159824#define TK_UPDATE 128
159825#define TK_SET 129
159826#define TK_DEFERRABLE 130
159827#define TK_FOREIGN 131
159828#define TK_DROP 132
159829#define TK_UNION 133
159830#define TK_ALL 134
159831#define TK_EXCEPT 135
159832#define TK_INTERSECT 136
159833#define TK_SELECT 137
159834#define TK_VALUES 138
159835#define TK_DISTINCT 139
159836#define TK_DOT 140
159837#define TK_FROM 141
159838#define TK_JOIN 142
159839#define TK_USING 143
159840#define TK_ORDER 144
159841#define TK_GROUP 145
159842#define TK_HAVING 146
159843#define TK_LIMIT 147
159844#define TK_WHERE 148
159845#define TK_RETURNING 149
159846#define TK_INTO 150
159847#define TK_NOTHING 151
159848#define TK_FLOAT 152
159849#define TK_BLOB 153
159850#define TK_INTEGER 154
159851#define TK_VARIABLE 155
159852#define TK_CASE 156
159853#define TK_WHEN 157
159854#define TK_THEN 158
159855#define TK_ELSE 159
159856#define TK_INDEX 160
159857#define TK_ALTER 161
159858#define TK_ADD 162
159859#define TK_WINDOW 163
159860#define TK_OVER 164
159861#define TK_FILTER 165
159862#define TK_COLUMN 166
159863#define TK_AGG_FUNCTION 167
159864#define TK_AGG_COLUMN 168
159865#define TK_TRUEFALSE 169
159866#define TK_ISNOT 170
159867#define TK_FUNCTION 171
159868#define TK_UMINUS 172
159869#define TK_UPLUS 173
159870#define TK_TRUTH 174
159871#define TK_REGISTER 175
159872#define TK_VECTOR 176
159873#define TK_SELECT_COLUMN 177
159874#define TK_IF_NULL_ROW 178
159875#define TK_ASTERISK 179
159876#define TK_SPAN 180
159877#define TK_ERROR 181
159878#define TK_SPACE 182
159879#define TK_ILLEGAL 183
159880#endif
159881/**************** End token definitions ***************************************/
159882
159883/* The next sections is a series of control #defines.
159884** various aspects of the generated parser.
159885** YYCODETYPE is the data type used to store the integer codes
159886** that represent terminal and non-terminal symbols.
159887** "unsigned char" is used if there are fewer than
159888** 256 symbols. Larger types otherwise.
159889** YYNOCODE is a number of type YYCODETYPE that is not used for
159890** any terminal or nonterminal symbol.
159891** YYFALLBACK If defined, this indicates that one or more tokens
159892** (also known as: "terminal symbols") have fall-back
159893** values which should be used if the original symbol
159894** would not parse. This permits keywords to sometimes
159895** be used as identifiers, for example.
159896** YYACTIONTYPE is the data type used for "action codes" - numbers
159897** that indicate what to do in response to the next
159898** token.
159899** sqlite3ParserTOKENTYPE is the data type used for minor type for terminal
159900** symbols. Background: A "minor type" is a semantic
159901** value associated with a terminal or non-terminal
159902** symbols. For example, for an "ID" terminal symbol,
159903** the minor type might be the name of the identifier.
159904** Each non-terminal can have a different minor type.
159905** Terminal symbols all have the same minor type, though.
159906** This macros defines the minor type for terminal
159907** symbols.
159908** YYMINORTYPE is the data type used for all minor types.
159909** This is typically a union of many types, one of
159910** which is sqlite3ParserTOKENTYPE. The entry in the union
159911** for terminal symbols is called "yy0".
159912** YYSTACKDEPTH is the maximum depth of the parser's stack. If
159913** zero the stack is dynamically sized using realloc()
159914** sqlite3ParserARG_SDECL A static variable declaration for the %extra_argument
159915** sqlite3ParserARG_PDECL A parameter declaration for the %extra_argument
159916** sqlite3ParserARG_PARAM Code to pass %extra_argument as a subroutine parameter
159917** sqlite3ParserARG_STORE Code to store %extra_argument into yypParser
159918** sqlite3ParserARG_FETCH Code to extract %extra_argument from yypParser
159919** sqlite3ParserCTX_* As sqlite3ParserARG_ except for %extra_context
159920** YYERRORSYMBOL is the code number of the error symbol. If not
159921** defined, then do no error processing.
159922** YYNSTATE the combined number of states.
159923** YYNRULE the number of rules in the grammar
159924** YYNTOKEN Number of terminal symbols
159925** YY_MAX_SHIFT Maximum value for shift actions
159926** YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
159927** YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
159928** YY_ERROR_ACTION The yy_action[] code for syntax error
159929** YY_ACCEPT_ACTION The yy_action[] code for accept
159930** YY_NO_ACTION The yy_action[] code for no-op
159931** YY_MIN_REDUCE Minimum value for reduce actions
159932** YY_MAX_REDUCE Maximum value for reduce actions
159933*/
159934#ifndef INTERFACE
159935# define INTERFACE 1
159936#endif
159937/************* Begin control #defines *****************************************/
159938#define YYCODETYPE unsigned short int
159939#define YYNOCODE 318
159940#define YYACTIONTYPE unsigned short int
159941#define YYWILDCARD 101
159942#define sqlite3ParserTOKENTYPE Token
159943typedef union {
159944 int yyinit;
159945 sqlite3ParserTOKENTYPE yy0;
159946 With* yy43;
159947 u32 yy51;
159948 int yy64;
159949 struct FrameBound yy81;
159950 struct {int value; int mask;} yy83;
159951 TriggerStep* yy95;
159952 Upsert* yy138;
159953 IdList* yy240;
159954 Cte* yy255;
159955 Select* yy303;
159956 Window* yy375;
159957 u8 yy534;
159958 ExprList* yy562;
159959 struct TrigEvent yy570;
159960 const char* yy600;
159961 SrcList* yy607;
159962 Expr* yy626;
159963} YYMINORTYPE;
159964#ifndef YYSTACKDEPTH
159965#define YYSTACKDEPTH 100
159966#endif
159967#define sqlite3ParserARG_SDECL
159968#define sqlite3ParserARG_PDECL
159969#define sqlite3ParserARG_PARAM
159970#define sqlite3ParserARG_FETCH
159971#define sqlite3ParserARG_STORE
159972#define sqlite3ParserCTX_SDECL Parse *pParse;
159973#define sqlite3ParserCTX_PDECL ,Parse *pParse
159974#define sqlite3ParserCTX_PARAM ,pParse
159975#define sqlite3ParserCTX_FETCH Parse *pParse=yypParser->pParse;
159976#define sqlite3ParserCTX_STORE yypParser->pParse=pParse;
159977#define YYFALLBACK 1
159978#define YYNSTATE 572
159979#define YYNRULE 401
159980#define YYNRULE_WITH_ACTION 339
159981#define YYNTOKEN 184
159982#define YY_MAX_SHIFT 571
159983#define YY_MIN_SHIFTREDUCE 829
159984#define YY_MAX_SHIFTREDUCE 1229
159985#define YY_ERROR_ACTION 1230
159986#define YY_ACCEPT_ACTION 1231
159987#define YY_NO_ACTION 1232
159988#define YY_MIN_REDUCE 1233
159989#define YY_MAX_REDUCE 1633
159990/************* End control #defines *******************************************/
159991#define YY_NLOOKAHEAD ((int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])))
159992
159993/* Define the yytestcase() macro to be a no-op if is not already defined
159994** otherwise.
159995**
159996** Applications can choose to define yytestcase() in the %include section
159997** to a macro that can assist in verifying code coverage. For production
159998** code the yytestcase() macro should be turned off. But it is useful
159999** for testing.
160000*/
160001#ifndef yytestcase
160002# define yytestcase(X)
160003#endif
160004
160005
160006/* Next are the tables used to determine what action to take based on the
160007** current state and lookahead token. These tables are used to implement
160008** functions that take a state number and lookahead value and return an
160009** action integer.
160010**
160011** Suppose the action integer is N. Then the action is determined as
160012** follows
160013**
160014** 0 <= N <= YY_MAX_SHIFT Shift N. That is, push the lookahead
160015** token onto the stack and goto state N.
160016**
160017** N between YY_MIN_SHIFTREDUCE Shift to an arbitrary state then
160018** and YY_MAX_SHIFTREDUCE reduce by rule N-YY_MIN_SHIFTREDUCE.
160019**
160020** N == YY_ERROR_ACTION A syntax error has occurred.
160021**
160022** N == YY_ACCEPT_ACTION The parser accepts its input.
160023**
160024** N == YY_NO_ACTION No such action. Denotes unused
160025** slots in the yy_action[] table.
160026**
160027** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE
160028** and YY_MAX_REDUCE
160029**
160030** The action table is constructed as a single large table named yy_action[].
160031** Given state S and lookahead X, the action is computed as either:
160032**
160033** (A) N = yy_action[ yy_shift_ofst[S] + X ]
160034** (B) N = yy_default[S]
160035**
160036** The (A) formula is preferred. The B formula is used instead if
160037** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X.
160038**
160039** The formulas above are for computing the action when the lookahead is
160040** a terminal symbol. If the lookahead is a non-terminal (as occurs after
160041** a reduce action) then the yy_reduce_ofst[] array is used in place of
160042** the yy_shift_ofst[] array.
160043**
160044** The following are the tables generated in this section:
160045**
160046** yy_action[] A single table containing all actions.
160047** yy_lookahead[] A table containing the lookahead for each entry in
160048** yy_action. Used to detect hash collisions.
160049** yy_shift_ofst[] For each state, the offset into yy_action for
160050** shifting terminals.
160051** yy_reduce_ofst[] For each state, the offset into yy_action for
160052** shifting non-terminals after a reduce.
160053** yy_default[] Default action for each state.
160054**
160055*********** Begin parsing tables **********************************************/
160056#define YY_ACTTAB_COUNT (2037)
160057static const YYACTIONTYPE yy_action[] = {
160058 /* 0 */ 564, 115, 112, 220, 169, 199, 115, 112, 220, 564,
160059 /* 10 */ 375, 1266, 564, 376, 564, 270, 1309, 1309, 406, 407,
160060 /* 20 */ 1084, 199, 1513, 41, 41, 515, 489, 521, 558, 558,
160061 /* 30 */ 558, 965, 41, 41, 395, 41, 41, 51, 51, 966,
160062 /* 40 */ 296, 1269, 296, 122, 123, 113, 1207, 1207, 1041, 1044,
160063 /* 50 */ 1034, 1034, 120, 120, 121, 121, 121, 121, 564, 407,
160064 /* 60 */ 275, 275, 275, 275, 1268, 115, 112, 220, 115, 112,
160065 /* 70 */ 220, 1512, 846, 561, 516, 561, 115, 112, 220, 250,
160066 /* 80 */ 217, 71, 71, 122, 123, 113, 1207, 1207, 1041, 1044,
160067 /* 90 */ 1034, 1034, 120, 120, 121, 121, 121, 121, 440, 440,
160068 /* 100 */ 440, 1149, 119, 119, 119, 119, 118, 118, 117, 117,
160069 /* 110 */ 117, 116, 442, 1183, 1149, 116, 442, 1149, 546, 513,
160070 /* 120 */ 1548, 1554, 374, 442, 6, 1183, 1154, 522, 1154, 407,
160071 /* 130 */ 1556, 461, 373, 1554, 535, 99, 463, 332, 121, 121,
160072 /* 140 */ 121, 121, 119, 119, 119, 119, 118, 118, 117, 117,
160073 /* 150 */ 117, 116, 442, 122, 123, 113, 1207, 1207, 1041, 1044,
160074 /* 160 */ 1034, 1034, 120, 120, 121, 121, 121, 121, 1257, 1183,
160075 /* 170 */ 1184, 1185, 243, 1064, 564, 502, 499, 498, 567, 124,
160076 /* 180 */ 567, 1183, 1184, 1185, 474, 497, 119, 119, 119, 119,
160077 /* 190 */ 118, 118, 117, 117, 117, 116, 442, 70, 70, 407,
160078 /* 200 */ 121, 121, 121, 121, 114, 117, 117, 117, 116, 442,
160079 /* 210 */ 1409, 1469, 119, 119, 119, 119, 118, 118, 117, 117,
160080 /* 220 */ 117, 116, 442, 122, 123, 113, 1207, 1207, 1041, 1044,
160081 /* 230 */ 1034, 1034, 120, 120, 121, 121, 121, 121, 407, 1031,
160082 /* 240 */ 1031, 1042, 1045, 81, 382, 541, 378, 80, 119, 119,
160083 /* 250 */ 119, 119, 118, 118, 117, 117, 117, 116, 442, 381,
160084 /* 260 */ 463, 332, 122, 123, 113, 1207, 1207, 1041, 1044, 1034,
160085 /* 270 */ 1034, 120, 120, 121, 121, 121, 121, 262, 215, 512,
160086 /* 280 */ 1424, 422, 119, 119, 119, 119, 118, 118, 117, 117,
160087 /* 290 */ 117, 116, 442, 1231, 1, 1, 571, 2, 1235, 1573,
160088 /* 300 */ 571, 2, 1235, 307, 1149, 141, 1600, 307, 407, 141,
160089 /* 310 */ 1183, 361, 1317, 1035, 866, 531, 1317, 1149, 359, 1567,
160090 /* 320 */ 1149, 119, 119, 119, 119, 118, 118, 117, 117, 117,
160091 /* 330 */ 116, 442, 122, 123, 113, 1207, 1207, 1041, 1044, 1034,
160092 /* 340 */ 1034, 120, 120, 121, 121, 121, 121, 275, 275, 1001,
160093 /* 350 */ 426, 275, 275, 1128, 1627, 1021, 1627, 137, 542, 1541,
160094 /* 360 */ 561, 272, 950, 950, 561, 1423, 1183, 1184, 1185, 1594,
160095 /* 370 */ 866, 1012, 530, 315, 231, 1011, 468, 1276, 231, 119,
160096 /* 380 */ 119, 119, 119, 118, 118, 117, 117, 117, 116, 442,
160097 /* 390 */ 1570, 119, 119, 119, 119, 118, 118, 117, 117, 117,
160098 /* 400 */ 116, 442, 330, 359, 1567, 564, 446, 1011, 1011, 1013,
160099 /* 410 */ 446, 207, 564, 306, 555, 407, 363, 1021, 363, 346,
160100 /* 420 */ 184, 118, 118, 117, 117, 117, 116, 442, 71, 71,
160101 /* 430 */ 439, 438, 1126, 1012, 472, 71, 71, 1011, 205, 122,
160102 /* 440 */ 123, 113, 1207, 1207, 1041, 1044, 1034, 1034, 120, 120,
160103 /* 450 */ 121, 121, 121, 121, 219, 219, 472, 1183, 407, 570,
160104 /* 460 */ 1183, 1235, 503, 1477, 149, 546, 307, 489, 141, 1011,
160105 /* 470 */ 1011, 1013, 546, 140, 545, 1317, 1214, 191, 1214, 950,
160106 /* 480 */ 950, 514, 122, 123, 113, 1207, 1207, 1041, 1044, 1034,
160107 /* 490 */ 1034, 120, 120, 121, 121, 121, 121, 563, 119, 119,
160108 /* 500 */ 119, 119, 118, 118, 117, 117, 117, 116, 442, 283,
160109 /* 510 */ 275, 275, 415, 1183, 1184, 1185, 1183, 1184, 1185, 372,
160110 /* 520 */ 1183, 243, 344, 561, 502, 499, 498, 1539, 407, 1540,
160111 /* 530 */ 1183, 288, 870, 143, 497, 1549, 185, 231, 9, 6,
160112 /* 540 */ 253, 119, 119, 119, 119, 118, 118, 117, 117, 117,
160113 /* 550 */ 116, 442, 122, 123, 113, 1207, 1207, 1041, 1044, 1034,
160114 /* 560 */ 1034, 120, 120, 121, 121, 121, 121, 407, 137, 446,
160115 /* 570 */ 447, 863, 169, 1183, 397, 1204, 1183, 1184, 1185, 931,
160116 /* 580 */ 526, 1001, 98, 339, 564, 342, 1183, 1184, 1185, 306,
160117 /* 590 */ 555, 122, 123, 113, 1207, 1207, 1041, 1044, 1034, 1034,
160118 /* 600 */ 120, 120, 121, 121, 121, 121, 452, 71, 71, 275,
160119 /* 610 */ 275, 119, 119, 119, 119, 118, 118, 117, 117, 117,
160120 /* 620 */ 116, 442, 561, 417, 306, 555, 1183, 1307, 1307, 1183,
160121 /* 630 */ 1184, 1185, 1204, 1149, 330, 458, 318, 407, 363, 470,
160122 /* 640 */ 431, 1167, 32, 541, 527, 350, 1149, 1629, 393, 1149,
160123 /* 650 */ 119, 119, 119, 119, 118, 118, 117, 117, 117, 116,
160124 /* 660 */ 442, 122, 123, 113, 1207, 1207, 1041, 1044, 1034, 1034,
160125 /* 670 */ 120, 120, 121, 121, 121, 121, 407, 199, 472, 1183,
160126 /* 680 */ 1022, 472, 1183, 1184, 1185, 386, 151, 539, 1548, 277,
160127 /* 690 */ 400, 137, 6, 317, 5, 564, 562, 3, 920, 920,
160128 /* 700 */ 122, 123, 113, 1207, 1207, 1041, 1044, 1034, 1034, 120,
160129 /* 710 */ 120, 121, 121, 121, 121, 411, 505, 83, 71, 71,
160130 /* 720 */ 119, 119, 119, 119, 118, 118, 117, 117, 117, 116,
160131 /* 730 */ 442, 1183, 218, 428, 1183, 1183, 1184, 1185, 363, 261,
160132 /* 740 */ 278, 358, 508, 353, 507, 248, 407, 306, 555, 1539,
160133 /* 750 */ 1006, 349, 363, 291, 489, 302, 293, 1542, 281, 119,
160134 /* 760 */ 119, 119, 119, 118, 118, 117, 117, 117, 116, 442,
160135 /* 770 */ 122, 123, 113, 1207, 1207, 1041, 1044, 1034, 1034, 120,
160136 /* 780 */ 120, 121, 121, 121, 121, 407, 148, 1183, 1184, 1185,
160137 /* 790 */ 1183, 1184, 1185, 275, 275, 1304, 1257, 1283, 483, 1476,
160138 /* 800 */ 150, 489, 480, 564, 1187, 1304, 561, 1587, 1255, 122,
160139 /* 810 */ 123, 113, 1207, 1207, 1041, 1044, 1034, 1034, 120, 120,
160140 /* 820 */ 121, 121, 121, 121, 564, 886, 13, 13, 520, 119,
160141 /* 830 */ 119, 119, 119, 118, 118, 117, 117, 117, 116, 442,
160142 /* 840 */ 1183, 420, 417, 564, 269, 269, 1316, 13, 13, 1539,
160143 /* 850 */ 1546, 16, 16, 322, 6, 407, 506, 561, 1089, 1089,
160144 /* 860 */ 486, 1187, 425, 1539, 887, 292, 71, 71, 119, 119,
160145 /* 870 */ 119, 119, 118, 118, 117, 117, 117, 116, 442, 122,
160146 /* 880 */ 123, 113, 1207, 1207, 1041, 1044, 1034, 1034, 120, 120,
160147 /* 890 */ 121, 121, 121, 121, 564, 12, 1183, 1184, 1185, 407,
160148 /* 900 */ 275, 275, 451, 303, 834, 835, 836, 417, 489, 276,
160149 /* 910 */ 276, 1547, 284, 561, 319, 6, 321, 71, 71, 429,
160150 /* 920 */ 451, 450, 561, 952, 101, 113, 1207, 1207, 1041, 1044,
160151 /* 930 */ 1034, 1034, 120, 120, 121, 121, 121, 121, 119, 119,
160152 /* 940 */ 119, 119, 118, 118, 117, 117, 117, 116, 442, 1105,
160153 /* 950 */ 1183, 489, 564, 1312, 437, 455, 478, 564, 246, 245,
160154 /* 960 */ 244, 1409, 1545, 547, 1106, 405, 6, 1544, 196, 1258,
160155 /* 970 */ 413, 6, 105, 462, 103, 71, 71, 286, 564, 1107,
160156 /* 980 */ 13, 13, 119, 119, 119, 119, 118, 118, 117, 117,
160157 /* 990 */ 117, 116, 442, 451, 104, 427, 337, 320, 275, 275,
160158 /* 1000 */ 906, 13, 13, 564, 1482, 1105, 1183, 1184, 1185, 126,
160159 /* 1010 */ 907, 561, 546, 564, 407, 478, 295, 1321, 253, 200,
160160 /* 1020 */ 1106, 548, 1482, 1484, 280, 1409, 55, 55, 1287, 561,
160161 /* 1030 */ 478, 380, 423, 951, 407, 1107, 71, 71, 122, 123,
160162 /* 1040 */ 113, 1207, 1207, 1041, 1044, 1034, 1034, 120, 120, 121,
160163 /* 1050 */ 121, 121, 121, 1204, 407, 287, 552, 309, 122, 123,
160164 /* 1060 */ 113, 1207, 1207, 1041, 1044, 1034, 1034, 120, 120, 121,
160165 /* 1070 */ 121, 121, 121, 441, 1128, 1628, 146, 1628, 122, 111,
160166 /* 1080 */ 113, 1207, 1207, 1041, 1044, 1034, 1034, 120, 120, 121,
160167 /* 1090 */ 121, 121, 121, 404, 403, 1482, 424, 119, 119, 119,
160168 /* 1100 */ 119, 118, 118, 117, 117, 117, 116, 442, 1183, 564,
160169 /* 1110 */ 1204, 544, 1086, 858, 329, 361, 1086, 119, 119, 119,
160170 /* 1120 */ 119, 118, 118, 117, 117, 117, 116, 442, 564, 294,
160171 /* 1130 */ 144, 523, 56, 56, 224, 564, 510, 119, 119, 119,
160172 /* 1140 */ 119, 118, 118, 117, 117, 117, 116, 442, 484, 1409,
160173 /* 1150 */ 537, 15, 15, 1126, 434, 439, 438, 407, 13, 13,
160174 /* 1160 */ 1523, 12, 926, 1211, 1183, 1184, 1185, 925, 1213, 536,
160175 /* 1170 */ 858, 557, 413, 193, 1525, 494, 1212, 448, 1160, 1222,
160176 /* 1180 */ 1183, 564, 123, 113, 1207, 1207, 1041, 1044, 1034, 1034,
160177 /* 1190 */ 120, 120, 121, 121, 121, 121, 1521, 1149, 564, 965,
160178 /* 1200 */ 564, 1214, 247, 1214, 13, 13, 1409, 966, 538, 564,
160179 /* 1210 */ 1149, 108, 556, 1149, 4, 310, 392, 1227, 17, 194,
160180 /* 1220 */ 485, 43, 43, 57, 57, 306, 555, 524, 559, 1160,
160181 /* 1230 */ 464, 564, 44, 44, 392, 1127, 1183, 1184, 1185, 479,
160182 /* 1240 */ 119, 119, 119, 119, 118, 118, 117, 117, 117, 116,
160183 /* 1250 */ 442, 443, 564, 327, 13, 13, 564, 418, 1315, 414,
160184 /* 1260 */ 171, 564, 311, 553, 213, 529, 1253, 564, 517, 543,
160185 /* 1270 */ 412, 108, 556, 137, 4, 58, 58, 435, 314, 59,
160186 /* 1280 */ 59, 274, 217, 549, 60, 60, 349, 476, 559, 1353,
160187 /* 1290 */ 61, 61, 1021, 275, 275, 1228, 213, 564, 106, 106,
160188 /* 1300 */ 8, 275, 275, 275, 275, 107, 561, 443, 566, 565,
160189 /* 1310 */ 564, 443, 1011, 1228, 561, 564, 561, 564, 275, 275,
160190 /* 1320 */ 62, 62, 1352, 553, 247, 456, 564, 98, 110, 306,
160191 /* 1330 */ 555, 561, 564, 45, 45, 405, 1203, 533, 46, 46,
160192 /* 1340 */ 47, 47, 532, 465, 1011, 1011, 1013, 1014, 27, 49,
160193 /* 1350 */ 49, 564, 1021, 405, 469, 50, 50, 564, 106, 106,
160194 /* 1360 */ 305, 564, 84, 204, 405, 107, 564, 443, 566, 565,
160195 /* 1370 */ 405, 564, 1011, 564, 63, 63, 564, 1599, 564, 895,
160196 /* 1380 */ 64, 64, 457, 477, 65, 65, 147, 96, 38, 14,
160197 /* 1390 */ 14, 1528, 412, 564, 66, 66, 128, 128, 926, 67,
160198 /* 1400 */ 67, 52, 52, 925, 1011, 1011, 1013, 1014, 27, 1572,
160199 /* 1410 */ 1171, 445, 208, 1123, 279, 394, 68, 68, 228, 390,
160200 /* 1420 */ 390, 389, 264, 387, 1171, 445, 843, 877, 279, 108,
160201 /* 1430 */ 556, 453, 4, 390, 390, 389, 264, 387, 564, 225,
160202 /* 1440 */ 843, 313, 328, 1003, 98, 252, 559, 544, 471, 312,
160203 /* 1450 */ 252, 564, 208, 225, 564, 313, 473, 30, 252, 279,
160204 /* 1460 */ 466, 69, 69, 312, 390, 390, 389, 264, 387, 443,
160205 /* 1470 */ 333, 843, 98, 564, 53, 53, 323, 157, 157, 227,
160206 /* 1480 */ 495, 553, 249, 289, 225, 564, 313, 162, 31, 1501,
160207 /* 1490 */ 135, 564, 1500, 227, 312, 533, 158, 158, 885, 884,
160208 /* 1500 */ 534, 162, 873, 301, 135, 564, 481, 226, 76, 76,
160209 /* 1510 */ 1021, 347, 1071, 98, 54, 54, 106, 106, 1067, 564,
160210 /* 1520 */ 249, 226, 519, 107, 227, 443, 566, 565, 72, 72,
160211 /* 1530 */ 1011, 334, 162, 564, 230, 135, 108, 556, 959, 4,
160212 /* 1540 */ 252, 408, 129, 129, 564, 1349, 306, 555, 564, 923,
160213 /* 1550 */ 564, 110, 226, 559, 564, 408, 73, 73, 564, 873,
160214 /* 1560 */ 306, 555, 1011, 1011, 1013, 1014, 27, 130, 130, 1071,
160215 /* 1570 */ 449, 131, 131, 127, 127, 357, 443, 156, 156, 892,
160216 /* 1580 */ 893, 155, 155, 338, 449, 356, 408, 564, 553, 968,
160217 /* 1590 */ 969, 306, 555, 1015, 341, 564, 108, 556, 564, 4,
160218 /* 1600 */ 1132, 1286, 533, 564, 856, 343, 145, 532, 345, 1300,
160219 /* 1610 */ 136, 136, 1083, 559, 1083, 449, 564, 1021, 134, 134,
160220 /* 1620 */ 1284, 132, 132, 106, 106, 1285, 133, 133, 564, 352,
160221 /* 1630 */ 107, 564, 443, 566, 565, 1340, 443, 1011, 362, 75,
160222 /* 1640 */ 75, 1082, 564, 1082, 564, 924, 1561, 110, 553, 551,
160223 /* 1650 */ 1015, 77, 77, 1361, 74, 74, 1408, 1336, 1347, 550,
160224 /* 1660 */ 1414, 1265, 1256, 1244, 1243, 42, 42, 48, 48, 1011,
160225 /* 1670 */ 1011, 1013, 1014, 27, 1245, 1580, 490, 1021, 267, 202,
160226 /* 1680 */ 1333, 365, 11, 106, 106, 930, 367, 210, 369, 391,
160227 /* 1690 */ 107, 1395, 443, 566, 565, 223, 1390, 1011, 500, 454,
160228 /* 1700 */ 282, 1400, 285, 108, 556, 214, 4, 325, 1383, 1283,
160229 /* 1710 */ 475, 355, 1473, 1583, 1472, 1399, 371, 1222, 326, 398,
160230 /* 1720 */ 559, 290, 331, 197, 100, 556, 209, 4, 198, 1011,
160231 /* 1730 */ 1011, 1013, 1014, 27, 385, 256, 1520, 1518, 554, 1219,
160232 /* 1740 */ 416, 559, 83, 443, 173, 206, 182, 221, 459, 167,
160233 /* 1750 */ 177, 460, 175, 493, 233, 553, 79, 178, 1396, 179,
160234 /* 1760 */ 35, 180, 96, 1402, 443, 396, 36, 467, 1478, 1401,
160235 /* 1770 */ 482, 237, 1404, 399, 82, 186, 553, 1467, 89, 488,
160236 /* 1780 */ 190, 268, 239, 491, 1021, 340, 240, 401, 1246, 1489,
160237 /* 1790 */ 106, 106, 336, 509, 1294, 241, 1303, 107, 430, 443,
160238 /* 1800 */ 566, 565, 1302, 91, 1011, 1021, 1598, 1301, 1273, 215,
160239 /* 1810 */ 1597, 106, 106, 402, 877, 432, 354, 1272, 107, 1271,
160240 /* 1820 */ 443, 566, 565, 1596, 1566, 1011, 1293, 433, 518, 299,
160241 /* 1830 */ 300, 360, 95, 525, 1344, 364, 1011, 1011, 1013, 1014,
160242 /* 1840 */ 27, 254, 255, 1552, 436, 1551, 125, 544, 10, 379,
160243 /* 1850 */ 1326, 1453, 102, 97, 1345, 528, 304, 1011, 1011, 1013,
160244 /* 1860 */ 1014, 27, 366, 377, 1343, 1342, 368, 370, 1325, 384,
160245 /* 1870 */ 201, 383, 34, 1368, 1367, 568, 1177, 266, 263, 265,
160246 /* 1880 */ 1505, 159, 569, 1241, 1236, 1506, 160, 142, 1504, 1503,
160247 /* 1890 */ 297, 211, 830, 161, 212, 78, 444, 203, 308, 222,
160248 /* 1900 */ 1081, 139, 1079, 316, 174, 163, 1203, 229, 176, 909,
160249 /* 1910 */ 324, 232, 1095, 181, 409, 410, 172, 164, 165, 419,
160250 /* 1920 */ 183, 85, 86, 421, 166, 87, 88, 1098, 1094, 234,
160251 /* 1930 */ 235, 152, 18, 236, 335, 1087, 1216, 252, 187, 487,
160252 /* 1940 */ 238, 188, 37, 845, 492, 356, 242, 496, 351, 501,
160253 /* 1950 */ 189, 90, 19, 504, 348, 20, 875, 92, 298, 168,
160254 /* 1960 */ 888, 153, 93, 511, 94, 1165, 154, 1047, 1134, 39,
160255 /* 1970 */ 216, 1133, 271, 273, 958, 192, 953, 110, 1151, 1155,
160256 /* 1980 */ 251, 7, 21, 1159, 1139, 22, 1153, 33, 23, 24,
160257 /* 1990 */ 25, 540, 1158, 195, 98, 1062, 26, 1048, 1046, 1050,
160258 /* 2000 */ 1104, 1051, 1103, 257, 258, 28, 40, 1173, 1016, 857,
160259 /* 2010 */ 109, 29, 560, 388, 138, 1172, 259, 170, 260, 1232,
160260 /* 2020 */ 1232, 919, 1232, 1232, 1232, 1232, 1232, 1232, 1232, 1232,
160261 /* 2030 */ 1232, 1232, 1589, 1232, 1232, 1232, 1588,
160262};
160263static const YYCODETYPE yy_lookahead[] = {
160264 /* 0 */ 192, 273, 274, 275, 192, 192, 273, 274, 275, 192,
160265 /* 10 */ 218, 215, 192, 218, 192, 212, 234, 235, 205, 19,
160266 /* 20 */ 11, 192, 294, 215, 216, 203, 192, 203, 209, 210,
160267 /* 30 */ 211, 31, 215, 216, 205, 215, 216, 215, 216, 39,
160268 /* 40 */ 227, 215, 229, 43, 44, 45, 46, 47, 48, 49,
160269 /* 50 */ 50, 51, 52, 53, 54, 55, 56, 57, 192, 19,
160270 /* 60 */ 238, 239, 238, 239, 215, 273, 274, 275, 273, 274,
160271 /* 70 */ 275, 237, 21, 251, 252, 251, 273, 274, 275, 255,
160272 /* 80 */ 256, 215, 216, 43, 44, 45, 46, 47, 48, 49,
160273 /* 90 */ 50, 51, 52, 53, 54, 55, 56, 57, 209, 210,
160274 /* 100 */ 211, 76, 102, 103, 104, 105, 106, 107, 108, 109,
160275 /* 110 */ 110, 111, 112, 59, 89, 111, 112, 92, 252, 307,
160276 /* 120 */ 308, 313, 314, 112, 312, 59, 86, 261, 88, 19,
160277 /* 130 */ 313, 80, 315, 313, 314, 25, 127, 128, 54, 55,
160278 /* 140 */ 56, 57, 102, 103, 104, 105, 106, 107, 108, 109,
160279 /* 150 */ 110, 111, 112, 43, 44, 45, 46, 47, 48, 49,
160280 /* 160 */ 50, 51, 52, 53, 54, 55, 56, 57, 192, 115,
160281 /* 170 */ 116, 117, 118, 122, 192, 121, 122, 123, 202, 69,
160282 /* 180 */ 204, 115, 116, 117, 192, 131, 102, 103, 104, 105,
160283 /* 190 */ 106, 107, 108, 109, 110, 111, 112, 215, 216, 19,
160284 /* 200 */ 54, 55, 56, 57, 58, 108, 109, 110, 111, 112,
160285 /* 210 */ 192, 160, 102, 103, 104, 105, 106, 107, 108, 109,
160286 /* 220 */ 110, 111, 112, 43, 44, 45, 46, 47, 48, 49,
160287 /* 230 */ 50, 51, 52, 53, 54, 55, 56, 57, 19, 46,
160288 /* 240 */ 47, 48, 49, 24, 248, 192, 250, 67, 102, 103,
160289 /* 250 */ 104, 105, 106, 107, 108, 109, 110, 111, 112, 277,
160290 /* 260 */ 127, 128, 43, 44, 45, 46, 47, 48, 49, 50,
160291 /* 270 */ 51, 52, 53, 54, 55, 56, 57, 26, 164, 165,
160292 /* 280 */ 272, 263, 102, 103, 104, 105, 106, 107, 108, 109,
160293 /* 290 */ 110, 111, 112, 184, 185, 186, 187, 188, 189, 186,
160294 /* 300 */ 187, 188, 189, 194, 76, 196, 229, 194, 19, 196,
160295 /* 310 */ 59, 192, 203, 120, 59, 87, 203, 89, 310, 311,
160296 /* 320 */ 92, 102, 103, 104, 105, 106, 107, 108, 109, 110,
160297 /* 330 */ 111, 112, 43, 44, 45, 46, 47, 48, 49, 50,
160298 /* 340 */ 51, 52, 53, 54, 55, 56, 57, 238, 239, 73,
160299 /* 350 */ 231, 238, 239, 22, 23, 100, 25, 81, 305, 306,
160300 /* 360 */ 251, 23, 25, 25, 251, 272, 115, 116, 117, 214,
160301 /* 370 */ 115, 116, 144, 192, 265, 120, 114, 222, 265, 102,
160302 /* 380 */ 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
160303 /* 390 */ 192, 102, 103, 104, 105, 106, 107, 108, 109, 110,
160304 /* 400 */ 111, 112, 126, 310, 311, 192, 297, 152, 153, 154,
160305 /* 410 */ 297, 149, 192, 137, 138, 19, 192, 100, 192, 23,
160306 /* 420 */ 22, 106, 107, 108, 109, 110, 111, 112, 215, 216,
160307 /* 430 */ 106, 107, 101, 116, 192, 215, 216, 120, 149, 43,
160308 /* 440 */ 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
160309 /* 450 */ 54, 55, 56, 57, 117, 117, 192, 59, 19, 187,
160310 /* 460 */ 59, 189, 23, 282, 240, 252, 194, 192, 196, 152,
160311 /* 470 */ 153, 154, 252, 72, 261, 203, 152, 25, 154, 142,
160312 /* 480 */ 142, 261, 43, 44, 45, 46, 47, 48, 49, 50,
160313 /* 490 */ 51, 52, 53, 54, 55, 56, 57, 192, 102, 103,
160314 /* 500 */ 104, 105, 106, 107, 108, 109, 110, 111, 112, 267,
160315 /* 510 */ 238, 239, 237, 115, 116, 117, 115, 116, 117, 192,
160316 /* 520 */ 59, 118, 16, 251, 121, 122, 123, 303, 19, 303,
160317 /* 530 */ 59, 267, 23, 72, 131, 308, 22, 265, 22, 312,
160318 /* 540 */ 24, 102, 103, 104, 105, 106, 107, 108, 109, 110,
160319 /* 550 */ 111, 112, 43, 44, 45, 46, 47, 48, 49, 50,
160320 /* 560 */ 51, 52, 53, 54, 55, 56, 57, 19, 81, 297,
160321 /* 570 */ 295, 23, 192, 59, 203, 59, 115, 116, 117, 108,
160322 /* 580 */ 192, 73, 25, 77, 192, 79, 115, 116, 117, 137,
160323 /* 590 */ 138, 43, 44, 45, 46, 47, 48, 49, 50, 51,
160324 /* 600 */ 52, 53, 54, 55, 56, 57, 119, 215, 216, 238,
160325 /* 610 */ 239, 102, 103, 104, 105, 106, 107, 108, 109, 110,
160326 /* 620 */ 111, 112, 251, 192, 137, 138, 59, 234, 235, 115,
160327 /* 630 */ 116, 117, 116, 76, 126, 127, 128, 19, 192, 268,
160328 /* 640 */ 19, 23, 22, 192, 252, 24, 89, 300, 301, 92,
160329 /* 650 */ 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
160330 /* 660 */ 112, 43, 44, 45, 46, 47, 48, 49, 50, 51,
160331 /* 670 */ 52, 53, 54, 55, 56, 57, 19, 192, 192, 59,
160332 /* 680 */ 23, 192, 115, 116, 117, 200, 240, 307, 308, 22,
160333 /* 690 */ 205, 81, 312, 262, 22, 192, 133, 22, 135, 136,
160334 /* 700 */ 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
160335 /* 710 */ 53, 54, 55, 56, 57, 197, 95, 150, 215, 216,
160336 /* 720 */ 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
160337 /* 730 */ 112, 59, 192, 112, 59, 115, 116, 117, 192, 118,
160338 /* 740 */ 119, 120, 121, 122, 123, 124, 19, 137, 138, 303,
160339 /* 750 */ 23, 130, 192, 267, 192, 252, 267, 306, 203, 102,
160340 /* 760 */ 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
160341 /* 770 */ 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
160342 /* 780 */ 53, 54, 55, 56, 57, 19, 240, 115, 116, 117,
160343 /* 790 */ 115, 116, 117, 238, 239, 222, 192, 224, 280, 237,
160344 /* 800 */ 240, 192, 284, 192, 59, 232, 251, 140, 204, 43,
160345 /* 810 */ 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
160346 /* 820 */ 54, 55, 56, 57, 192, 35, 215, 216, 192, 102,
160347 /* 830 */ 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
160348 /* 840 */ 59, 230, 192, 192, 238, 239, 237, 215, 216, 303,
160349 /* 850 */ 308, 215, 216, 16, 312, 19, 66, 251, 126, 127,
160350 /* 860 */ 128, 116, 230, 303, 74, 203, 215, 216, 102, 103,
160351 /* 870 */ 104, 105, 106, 107, 108, 109, 110, 111, 112, 43,
160352 /* 880 */ 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
160353 /* 890 */ 54, 55, 56, 57, 192, 212, 115, 116, 117, 19,
160354 /* 900 */ 238, 239, 192, 252, 7, 8, 9, 192, 192, 238,
160355 /* 910 */ 239, 308, 262, 251, 77, 312, 79, 215, 216, 129,
160356 /* 920 */ 210, 211, 251, 142, 158, 45, 46, 47, 48, 49,
160357 /* 930 */ 50, 51, 52, 53, 54, 55, 56, 57, 102, 103,
160358 /* 940 */ 104, 105, 106, 107, 108, 109, 110, 111, 112, 12,
160359 /* 950 */ 59, 192, 192, 237, 252, 243, 192, 192, 126, 127,
160360 /* 960 */ 128, 192, 308, 203, 27, 253, 312, 308, 285, 207,
160361 /* 970 */ 208, 312, 157, 290, 159, 215, 216, 262, 192, 42,
160362 /* 980 */ 215, 216, 102, 103, 104, 105, 106, 107, 108, 109,
160363 /* 990 */ 110, 111, 112, 283, 158, 230, 237, 160, 238, 239,
160364 /* 1000 */ 63, 215, 216, 192, 192, 12, 115, 116, 117, 22,
160365 /* 1010 */ 73, 251, 252, 192, 19, 192, 230, 239, 24, 24,
160366 /* 1020 */ 27, 261, 210, 211, 99, 192, 215, 216, 225, 251,
160367 /* 1030 */ 192, 192, 263, 142, 19, 42, 215, 216, 43, 44,
160368 /* 1040 */ 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,
160369 /* 1050 */ 55, 56, 57, 59, 19, 291, 63, 132, 43, 44,
160370 /* 1060 */ 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,
160371 /* 1070 */ 55, 56, 57, 252, 22, 23, 22, 25, 43, 44,
160372 /* 1080 */ 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,
160373 /* 1090 */ 55, 56, 57, 106, 107, 283, 263, 102, 103, 104,
160374 /* 1100 */ 105, 106, 107, 108, 109, 110, 111, 112, 59, 192,
160375 /* 1110 */ 116, 144, 29, 59, 291, 192, 33, 102, 103, 104,
160376 /* 1120 */ 105, 106, 107, 108, 109, 110, 111, 112, 192, 291,
160377 /* 1130 */ 163, 19, 215, 216, 15, 192, 108, 102, 103, 104,
160378 /* 1140 */ 105, 106, 107, 108, 109, 110, 111, 112, 65, 192,
160379 /* 1150 */ 66, 215, 216, 101, 231, 106, 107, 19, 215, 216,
160380 /* 1160 */ 192, 212, 134, 114, 115, 116, 117, 139, 119, 85,
160381 /* 1170 */ 116, 207, 208, 230, 192, 19, 127, 192, 94, 60,
160382 /* 1180 */ 59, 192, 44, 45, 46, 47, 48, 49, 50, 51,
160383 /* 1190 */ 52, 53, 54, 55, 56, 57, 192, 76, 192, 31,
160384 /* 1200 */ 192, 152, 46, 154, 215, 216, 192, 39, 87, 192,
160385 /* 1210 */ 89, 19, 20, 92, 22, 192, 22, 23, 22, 230,
160386 /* 1220 */ 263, 215, 216, 215, 216, 137, 138, 115, 36, 145,
160387 /* 1230 */ 128, 192, 215, 216, 22, 23, 115, 116, 117, 290,
160388 /* 1240 */ 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
160389 /* 1250 */ 112, 59, 192, 151, 215, 216, 192, 61, 203, 298,
160390 /* 1260 */ 299, 192, 192, 71, 25, 144, 203, 192, 203, 230,
160391 /* 1270 */ 114, 19, 20, 81, 22, 215, 216, 263, 192, 215,
160392 /* 1280 */ 216, 255, 256, 203, 215, 216, 130, 19, 36, 192,
160393 /* 1290 */ 215, 216, 100, 238, 239, 101, 25, 192, 106, 107,
160394 /* 1300 */ 48, 238, 239, 238, 239, 113, 251, 115, 116, 117,
160395 /* 1310 */ 192, 59, 120, 101, 251, 192, 251, 192, 238, 239,
160396 /* 1320 */ 215, 216, 192, 71, 46, 243, 192, 25, 25, 137,
160397 /* 1330 */ 138, 251, 192, 215, 216, 253, 25, 85, 215, 216,
160398 /* 1340 */ 215, 216, 90, 243, 152, 153, 154, 155, 156, 215,
160399 /* 1350 */ 216, 192, 100, 253, 243, 215, 216, 192, 106, 107,
160400 /* 1360 */ 243, 192, 148, 149, 253, 113, 192, 115, 116, 117,
160401 /* 1370 */ 253, 192, 120, 192, 215, 216, 192, 23, 192, 25,
160402 /* 1380 */ 215, 216, 192, 115, 215, 216, 22, 148, 24, 215,
160403 /* 1390 */ 216, 192, 114, 192, 215, 216, 215, 216, 134, 215,
160404 /* 1400 */ 216, 215, 216, 139, 152, 153, 154, 155, 156, 0,
160405 /* 1410 */ 1, 2, 141, 23, 5, 25, 215, 216, 24, 10,
160406 /* 1420 */ 11, 12, 13, 14, 1, 2, 17, 125, 5, 19,
160407 /* 1430 */ 20, 268, 22, 10, 11, 12, 13, 14, 192, 30,
160408 /* 1440 */ 17, 32, 23, 23, 25, 25, 36, 144, 23, 40,
160409 /* 1450 */ 25, 192, 141, 30, 192, 32, 23, 22, 25, 5,
160410 /* 1460 */ 128, 215, 216, 40, 10, 11, 12, 13, 14, 59,
160411 /* 1470 */ 23, 17, 25, 192, 215, 216, 192, 215, 216, 70,
160412 /* 1480 */ 23, 71, 25, 151, 30, 192, 32, 78, 53, 192,
160413 /* 1490 */ 81, 192, 192, 70, 40, 85, 215, 216, 119, 120,
160414 /* 1500 */ 90, 78, 59, 254, 81, 192, 192, 98, 215, 216,
160415 /* 1510 */ 100, 23, 59, 25, 215, 216, 106, 107, 23, 192,
160416 /* 1520 */ 25, 98, 19, 113, 70, 115, 116, 117, 215, 216,
160417 /* 1530 */ 120, 192, 78, 192, 140, 81, 19, 20, 23, 22,
160418 /* 1540 */ 25, 132, 215, 216, 192, 192, 137, 138, 192, 23,
160419 /* 1550 */ 192, 25, 98, 36, 192, 132, 215, 216, 192, 116,
160420 /* 1560 */ 137, 138, 152, 153, 154, 155, 156, 215, 216, 116,
160421 /* 1570 */ 161, 215, 216, 215, 216, 120, 59, 215, 216, 7,
160422 /* 1580 */ 8, 215, 216, 192, 161, 130, 132, 192, 71, 83,
160423 /* 1590 */ 84, 137, 138, 59, 192, 192, 19, 20, 192, 22,
160424 /* 1600 */ 97, 225, 85, 192, 23, 192, 25, 90, 192, 192,
160425 /* 1610 */ 215, 216, 152, 36, 154, 161, 192, 100, 215, 216,
160426 /* 1620 */ 192, 215, 216, 106, 107, 225, 215, 216, 192, 192,
160427 /* 1630 */ 113, 192, 115, 116, 117, 257, 59, 120, 192, 215,
160428 /* 1640 */ 216, 152, 192, 154, 192, 23, 317, 25, 71, 235,
160429 /* 1650 */ 116, 215, 216, 192, 215, 216, 192, 192, 192, 192,
160430 /* 1660 */ 192, 192, 192, 192, 192, 215, 216, 215, 216, 152,
160431 /* 1670 */ 153, 154, 155, 156, 192, 192, 287, 100, 286, 241,
160432 /* 1680 */ 254, 254, 242, 106, 107, 108, 254, 213, 254, 190,
160433 /* 1690 */ 113, 270, 115, 116, 117, 296, 266, 120, 219, 258,
160434 /* 1700 */ 244, 270, 258, 19, 20, 228, 22, 292, 266, 224,
160435 /* 1710 */ 292, 218, 218, 195, 218, 270, 258, 60, 245, 270,
160436 /* 1720 */ 36, 245, 244, 248, 19, 20, 242, 22, 248, 152,
160437 /* 1730 */ 153, 154, 155, 156, 244, 140, 199, 199, 279, 38,
160438 /* 1740 */ 199, 36, 150, 59, 296, 149, 22, 296, 18, 43,
160439 /* 1750 */ 236, 199, 233, 18, 198, 71, 293, 236, 271, 236,
160440 /* 1760 */ 269, 236, 148, 271, 59, 245, 269, 245, 282, 271,
160441 /* 1770 */ 199, 198, 233, 245, 293, 233, 71, 245, 157, 62,
160442 /* 1780 */ 22, 199, 198, 220, 100, 199, 198, 220, 199, 289,
160443 /* 1790 */ 106, 107, 288, 114, 226, 198, 217, 113, 64, 115,
160444 /* 1800 */ 116, 117, 217, 22, 120, 100, 223, 217, 217, 164,
160445 /* 1810 */ 223, 106, 107, 220, 125, 24, 217, 219, 113, 217,
160446 /* 1820 */ 115, 116, 117, 217, 311, 120, 226, 112, 304, 281,
160447 /* 1830 */ 281, 220, 114, 143, 260, 259, 152, 153, 154, 155,
160448 /* 1840 */ 156, 199, 91, 316, 82, 316, 147, 144, 22, 199,
160449 /* 1850 */ 249, 276, 157, 146, 260, 145, 278, 152, 153, 154,
160450 /* 1860 */ 155, 156, 259, 248, 260, 260, 259, 259, 249, 245,
160451 /* 1870 */ 247, 246, 25, 264, 264, 201, 13, 6, 193, 193,
160452 /* 1880 */ 212, 206, 191, 191, 191, 212, 206, 221, 212, 212,
160453 /* 1890 */ 221, 213, 4, 206, 213, 212, 3, 22, 162, 15,
160454 /* 1900 */ 23, 16, 23, 138, 150, 129, 25, 24, 141, 20,
160455 /* 1910 */ 16, 143, 1, 141, 302, 302, 299, 129, 129, 61,
160456 /* 1920 */ 150, 53, 53, 37, 129, 53, 53, 115, 1, 34,
160457 /* 1930 */ 140, 5, 22, 114, 160, 68, 75, 25, 68, 41,
160458 /* 1940 */ 140, 114, 24, 20, 19, 130, 124, 67, 24, 67,
160459 /* 1950 */ 22, 22, 22, 96, 23, 22, 59, 22, 67, 37,
160460 /* 1960 */ 28, 23, 148, 22, 25, 23, 23, 23, 23, 22,
160461 /* 1970 */ 140, 97, 23, 23, 115, 22, 142, 25, 88, 75,
160462 /* 1980 */ 34, 44, 34, 75, 23, 34, 86, 22, 34, 34,
160463 /* 1990 */ 34, 24, 93, 25, 25, 23, 34, 23, 23, 23,
160464 /* 2000 */ 23, 11, 23, 25, 22, 22, 22, 1, 23, 23,
160465 /* 2010 */ 22, 22, 25, 15, 23, 1, 140, 25, 140, 318,
160466 /* 2020 */ 318, 134, 318, 318, 318, 318, 318, 318, 318, 318,
160467 /* 2030 */ 318, 318, 140, 318, 318, 318, 140, 318, 318, 318,
160468 /* 2040 */ 318, 318, 318, 318, 318, 318, 318, 318, 318, 318,
160469 /* 2050 */ 318, 318, 318, 318, 318, 318, 318, 318, 318, 318,
160470 /* 2060 */ 318, 318, 318, 318, 318, 318, 318, 318, 318, 318,
160471 /* 2070 */ 318, 318, 318, 318, 318, 318, 318, 318, 318, 318,
160472 /* 2080 */ 318, 318, 318, 318, 318, 318, 318, 318, 318, 318,
160473 /* 2090 */ 318, 318, 318, 318, 318, 318, 318, 318, 318, 318,
160474 /* 2100 */ 318, 318, 318, 318, 318, 318, 318, 318, 318, 318,
160475 /* 2110 */ 318, 318, 318, 318, 318, 318, 318, 318, 318, 318,
160476 /* 2120 */ 318, 318, 318, 318, 318, 318, 318, 318, 318, 318,
160477 /* 2130 */ 318, 318, 318, 318, 318, 318, 318, 318, 318, 318,
160478 /* 2140 */ 318, 318, 318, 318, 318, 318, 318, 318, 318, 318,
160479 /* 2150 */ 318, 318, 318, 318, 318, 318, 318, 318, 318, 318,
160480 /* 2160 */ 318, 318, 318, 318, 318, 318, 318, 318, 318, 318,
160481 /* 2170 */ 318, 318, 318, 318, 318, 318, 318, 318, 318, 318,
160482 /* 2180 */ 318, 318, 318, 318, 318, 318, 318, 318, 318, 318,
160483 /* 2190 */ 318, 318, 318, 318, 318, 318, 318, 318, 318, 318,
160484 /* 2200 */ 318, 318, 318, 318, 318, 318, 318, 318, 318, 318,
160485 /* 2210 */ 318, 318, 318, 318, 318, 318, 318, 318, 318, 318,
160486 /* 2220 */ 318,
160487};
160488#define YY_SHIFT_COUNT (571)
160489#define YY_SHIFT_MIN (0)
160490#define YY_SHIFT_MAX (2014)
160491static const unsigned short int yy_shift_ofst[] = {
160492 /* 0 */ 1423, 1409, 1454, 1192, 1192, 610, 1252, 1410, 1517, 1684,
160493 /* 10 */ 1684, 1684, 276, 0, 0, 180, 1015, 1684, 1684, 1684,
160494 /* 20 */ 1684, 1684, 1684, 1684, 1684, 1684, 1684, 1684, 1684, 1684,
160495 /* 30 */ 1049, 1049, 1121, 1121, 54, 487, 610, 610, 610, 610,
160496 /* 40 */ 610, 40, 110, 219, 289, 396, 439, 509, 548, 618,
160497 /* 50 */ 657, 727, 766, 836, 995, 1015, 1015, 1015, 1015, 1015,
160498 /* 60 */ 1015, 1015, 1015, 1015, 1015, 1015, 1015, 1015, 1015, 1015,
160499 /* 70 */ 1015, 1015, 1015, 1035, 1015, 1138, 880, 880, 1577, 1684,
160500 /* 80 */ 1684, 1684, 1684, 1684, 1684, 1684, 1684, 1684, 1684, 1684,
160501 /* 90 */ 1684, 1684, 1684, 1684, 1684, 1684, 1684, 1684, 1684, 1684,
160502 /* 100 */ 1684, 1684, 1684, 1684, 1684, 1684, 1684, 1684, 1684, 1684,
160503 /* 110 */ 1684, 1684, 1684, 1705, 1684, 1684, 1684, 1684, 1684, 1684,
160504 /* 120 */ 1684, 1684, 1684, 1684, 1684, 1684, 1684, 146, 84, 84,
160505 /* 130 */ 84, 84, 84, 277, 315, 401, 97, 461, 251, 66,
160506 /* 140 */ 66, 51, 1156, 66, 66, 324, 324, 66, 452, 452,
160507 /* 150 */ 452, 452, 133, 114, 114, 4, 11, 2037, 2037, 621,
160508 /* 160 */ 621, 621, 567, 398, 398, 398, 398, 937, 937, 228,
160509 /* 170 */ 251, 331, 1052, 66, 66, 66, 66, 66, 66, 66,
160510 /* 180 */ 66, 66, 66, 66, 66, 66, 66, 66, 66, 66,
160511 /* 190 */ 66, 66, 66, 557, 557, 66, 9, 25, 25, 745,
160512 /* 200 */ 745, 967, 1088, 2037, 2037, 2037, 2037, 2037, 2037, 2037,
160513 /* 210 */ 255, 317, 317, 514, 403, 620, 471, 672, 781, 891,
160514 /* 220 */ 675, 66, 66, 66, 66, 66, 66, 66, 66, 66,
160515 /* 230 */ 66, 508, 66, 66, 66, 66, 66, 66, 66, 66,
160516 /* 240 */ 66, 66, 66, 66, 790, 790, 790, 66, 66, 66,
160517 /* 250 */ 338, 66, 66, 66, 516, 1084, 66, 66, 993, 66,
160518 /* 260 */ 66, 66, 66, 66, 66, 66, 66, 732, 1083, 563,
160519 /* 270 */ 994, 994, 994, 994, 337, 563, 563, 1028, 987, 897,
160520 /* 280 */ 1119, 262, 1214, 1271, 1112, 1214, 1112, 1268, 1239, 262,
160521 /* 290 */ 262, 1239, 262, 1271, 1268, 1302, 1354, 1278, 1168, 1168,
160522 /* 300 */ 1168, 1112, 1303, 1303, 815, 1311, 1264, 1364, 1657, 1657,
160523 /* 310 */ 1595, 1595, 1701, 1701, 1595, 1592, 1596, 1724, 1706, 1730,
160524 /* 320 */ 1730, 1730, 1730, 1595, 1735, 1614, 1596, 1596, 1614, 1724,
160525 /* 330 */ 1706, 1614, 1706, 1614, 1595, 1735, 1621, 1717, 1595, 1735,
160526 /* 340 */ 1758, 1595, 1735, 1595, 1735, 1758, 1679, 1679, 1679, 1734,
160527 /* 350 */ 1781, 1781, 1758, 1679, 1689, 1679, 1734, 1679, 1679, 1645,
160528 /* 360 */ 1791, 1715, 1715, 1758, 1690, 1718, 1690, 1718, 1690, 1718,
160529 /* 370 */ 1690, 1718, 1595, 1751, 1751, 1762, 1762, 1699, 1703, 1826,
160530 /* 380 */ 1595, 1695, 1699, 1707, 1710, 1614, 1847, 1863, 1863, 1871,
160531 /* 390 */ 1871, 1871, 2037, 2037, 2037, 2037, 2037, 2037, 2037, 2037,
160532 /* 400 */ 2037, 2037, 2037, 2037, 2037, 2037, 2037, 193, 837, 1194,
160533 /* 410 */ 1212, 506, 832, 1054, 1390, 925, 1435, 1394, 1102, 1332,
160534 /* 420 */ 1419, 1196, 1420, 1425, 1433, 1447, 1457, 1488, 1443, 1379,
160535 /* 430 */ 1572, 1455, 1503, 1453, 1495, 1515, 1506, 1526, 1460, 1489,
160536 /* 440 */ 1581, 1622, 1534, 667, 1888, 1893, 1875, 1736, 1884, 1885,
160537 /* 450 */ 1877, 1879, 1765, 1754, 1776, 1881, 1881, 1883, 1767, 1889,
160538 /* 460 */ 1768, 1894, 1911, 1772, 1788, 1881, 1789, 1858, 1886, 1881,
160539 /* 470 */ 1770, 1868, 1869, 1872, 1873, 1795, 1812, 1895, 1790, 1927,
160540 /* 480 */ 1926, 1910, 1819, 1774, 1867, 1912, 1870, 1861, 1898, 1800,
160541 /* 490 */ 1827, 1918, 1923, 1925, 1815, 1822, 1928, 1880, 1929, 1930,
160542 /* 500 */ 1931, 1933, 1882, 1897, 1924, 1857, 1932, 1935, 1891, 1922,
160543 /* 510 */ 1938, 1814, 1941, 1942, 1943, 1944, 1939, 1945, 1947, 1874,
160544 /* 520 */ 1830, 1949, 1950, 1859, 1946, 1953, 1834, 1952, 1948, 1951,
160545 /* 530 */ 1954, 1955, 1890, 1904, 1900, 1937, 1908, 1899, 1956, 1961,
160546 /* 540 */ 1965, 1967, 1968, 1969, 1962, 1972, 1952, 1974, 1975, 1976,
160547 /* 550 */ 1977, 1978, 1979, 1982, 1990, 1983, 1984, 1985, 1986, 1988,
160548 /* 560 */ 1989, 1987, 1887, 1876, 1878, 1892, 1896, 1992, 1991, 1998,
160549 /* 570 */ 2006, 2014,
160550};
160551#define YY_REDUCE_COUNT (406)
160552#define YY_REDUCE_MIN (-272)
160553#define YY_REDUCE_MAX (1693)
160554static const short yy_reduce_ofst[] = {
160555 /* 0 */ 109, 113, 272, 760, -178, -176, -192, -183, -180, -134,
160556 /* 10 */ 213, 220, 371, -208, -205, -272, -197, 611, 632, 765,
160557 /* 20 */ 786, 392, 943, 989, 503, 651, 1039, -18, 702, 821,
160558 /* 30 */ 710, 812, -188, 380, -187, 555, 662, 1055, 1063, 1065,
160559 /* 40 */ 1080, -267, -267, -267, -267, -267, -267, -267, -267, -267,
160560 /* 50 */ -267, -267, -267, -267, -267, -267, -267, -267, -267, -267,
160561 /* 60 */ -267, -267, -267, -267, -267, -267, -267, -267, -267, -267,
160562 /* 70 */ -267, -267, -267, -267, -267, -267, -267, -267, 636, 811,
160563 /* 80 */ 917, 936, 1006, 1008, 1017, 1060, 1064, 1069, 1075, 1105,
160564 /* 90 */ 1118, 1123, 1125, 1134, 1140, 1159, 1165, 1169, 1174, 1179,
160565 /* 100 */ 1181, 1184, 1186, 1201, 1246, 1259, 1262, 1281, 1293, 1299,
160566 /* 110 */ 1313, 1327, 1341, 1352, 1356, 1358, 1362, 1366, 1395, 1403,
160567 /* 120 */ 1406, 1411, 1424, 1436, 1439, 1450, 1452, -267, -267, -267,
160568 /* 130 */ -267, -267, -267, -267, -267, 224, -267, 446, -24, 275,
160569 /* 140 */ 546, 518, 573, 560, 53, -181, -111, 485, 606, 671,
160570 /* 150 */ 606, 671, 683, 8, 93, -267, -267, -267, -267, 155,
160571 /* 160 */ 155, 155, 181, 242, 264, 486, 489, -218, 393, 227,
160572 /* 170 */ 604, 347, 347, -171, 431, 650, 715, -166, 562, 609,
160573 /* 180 */ 716, 764, 18, 823, 769, 833, 838, 957, 759, 119,
160574 /* 190 */ 923, 226, 1014, 542, 603, 451, 949, 654, 659, 762,
160575 /* 200 */ 964, -4, 778, 961, 712, 1082, 1100, 1111, 1026, 1117,
160576 /* 210 */ -204, -174, -151, -8, 77, 198, 305, 327, 388, 540,
160577 /* 220 */ 839, 968, 982, 985, 1004, 1023, 1070, 1086, 1097, 1130,
160578 /* 230 */ 1190, 1163, 1199, 1284, 1297, 1300, 1314, 1339, 1353, 1391,
160579 /* 240 */ 1402, 1413, 1416, 1417, 803, 1376, 1400, 1428, 1437, 1446,
160580 /* 250 */ 1378, 1461, 1464, 1465, 1249, 1329, 1466, 1467, 1414, 1468,
160581 /* 260 */ 305, 1469, 1470, 1471, 1472, 1482, 1483, 1389, 1392, 1438,
160582 /* 270 */ 1426, 1427, 1432, 1434, 1378, 1438, 1438, 1440, 1474, 1499,
160583 /* 280 */ 1399, 1421, 1430, 1456, 1441, 1442, 1444, 1415, 1473, 1431,
160584 /* 290 */ 1445, 1476, 1449, 1478, 1418, 1479, 1477, 1485, 1493, 1494,
160585 /* 300 */ 1496, 1458, 1475, 1480, 1459, 1490, 1484, 1518, 1448, 1451,
160586 /* 310 */ 1537, 1538, 1463, 1481, 1541, 1486, 1487, 1491, 1519, 1514,
160587 /* 320 */ 1521, 1523, 1525, 1552, 1556, 1520, 1492, 1498, 1522, 1497,
160588 /* 330 */ 1539, 1528, 1542, 1532, 1571, 1573, 1500, 1504, 1582, 1584,
160589 /* 340 */ 1563, 1586, 1588, 1589, 1597, 1567, 1579, 1585, 1590, 1568,
160590 /* 350 */ 1583, 1587, 1593, 1591, 1598, 1599, 1600, 1602, 1606, 1513,
160591 /* 360 */ 1524, 1548, 1549, 1611, 1574, 1576, 1594, 1603, 1604, 1607,
160592 /* 370 */ 1605, 1608, 1642, 1527, 1529, 1609, 1610, 1601, 1615, 1575,
160593 /* 380 */ 1650, 1578, 1619, 1623, 1625, 1624, 1674, 1685, 1686, 1691,
160594 /* 390 */ 1692, 1693, 1612, 1613, 1617, 1675, 1668, 1673, 1676, 1677,
160595 /* 400 */ 1680, 1666, 1669, 1678, 1681, 1683, 1687,
160596};
160597static const YYACTIONTYPE yy_default[] = {
160598 /* 0 */ 1633, 1633, 1633, 1462, 1230, 1341, 1230, 1230, 1230, 1462,
160599 /* 10 */ 1462, 1462, 1230, 1371, 1371, 1515, 1263, 1230, 1230, 1230,
160600 /* 20 */ 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1461, 1230, 1230,
160601 /* 30 */ 1230, 1230, 1550, 1550, 1230, 1230, 1230, 1230, 1230, 1230,
160602 /* 40 */ 1230, 1230, 1380, 1230, 1387, 1230, 1230, 1230, 1230, 1230,
160603 /* 50 */ 1463, 1464, 1230, 1230, 1230, 1514, 1516, 1479, 1394, 1393,
160604 /* 60 */ 1392, 1391, 1497, 1358, 1385, 1378, 1382, 1457, 1458, 1456,
160605 /* 70 */ 1460, 1464, 1463, 1230, 1381, 1428, 1442, 1427, 1230, 1230,
160606 /* 80 */ 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230,
160607 /* 90 */ 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230,
160608 /* 100 */ 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230,
160609 /* 110 */ 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230,
160610 /* 120 */ 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1436, 1441, 1447,
160611 /* 130 */ 1440, 1437, 1430, 1429, 1431, 1230, 1432, 1230, 1254, 1230,
160612 /* 140 */ 1230, 1251, 1305, 1230, 1230, 1230, 1230, 1230, 1534, 1533,
160613 /* 150 */ 1230, 1230, 1263, 1422, 1421, 1433, 1434, 1444, 1443, 1522,
160614 /* 160 */ 1586, 1585, 1480, 1230, 1230, 1230, 1230, 1230, 1230, 1550,
160615 /* 170 */ 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230,
160616 /* 180 */ 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230,
160617 /* 190 */ 1230, 1230, 1230, 1550, 1550, 1230, 1263, 1550, 1550, 1259,
160618 /* 200 */ 1259, 1365, 1230, 1529, 1332, 1332, 1332, 1332, 1341, 1332,
160619 /* 210 */ 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230,
160620 /* 220 */ 1230, 1230, 1230, 1230, 1230, 1519, 1517, 1230, 1230, 1230,
160621 /* 230 */ 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230,
160622 /* 240 */ 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230,
160623 /* 250 */ 1230, 1230, 1230, 1230, 1337, 1230, 1230, 1230, 1230, 1230,
160624 /* 260 */ 1230, 1230, 1230, 1230, 1230, 1230, 1579, 1230, 1492, 1319,
160625 /* 270 */ 1337, 1337, 1337, 1337, 1339, 1320, 1318, 1331, 1264, 1237,
160626 /* 280 */ 1625, 1397, 1386, 1338, 1360, 1386, 1360, 1622, 1384, 1397,
160627 /* 290 */ 1397, 1384, 1397, 1338, 1622, 1280, 1602, 1275, 1371, 1371,
160628 /* 300 */ 1371, 1360, 1365, 1365, 1459, 1338, 1331, 1230, 1625, 1625,
160629 /* 310 */ 1346, 1346, 1624, 1624, 1346, 1480, 1609, 1406, 1308, 1314,
160630 /* 320 */ 1314, 1314, 1314, 1346, 1248, 1384, 1609, 1609, 1384, 1406,
160631 /* 330 */ 1308, 1384, 1308, 1384, 1346, 1248, 1496, 1619, 1346, 1248,
160632 /* 340 */ 1470, 1346, 1248, 1346, 1248, 1470, 1306, 1306, 1306, 1295,
160633 /* 350 */ 1230, 1230, 1470, 1306, 1280, 1306, 1295, 1306, 1306, 1568,
160634 /* 360 */ 1230, 1474, 1474, 1470, 1364, 1359, 1364, 1359, 1364, 1359,
160635 /* 370 */ 1364, 1359, 1346, 1560, 1560, 1374, 1374, 1379, 1365, 1465,
160636 /* 380 */ 1346, 1230, 1379, 1377, 1375, 1384, 1298, 1582, 1582, 1578,
160637 /* 390 */ 1578, 1578, 1630, 1630, 1529, 1595, 1263, 1263, 1263, 1263,
160638 /* 400 */ 1595, 1282, 1282, 1264, 1264, 1263, 1595, 1230, 1230, 1230,
160639 /* 410 */ 1230, 1230, 1230, 1590, 1230, 1524, 1481, 1350, 1230, 1230,
160640 /* 420 */ 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230,
160641 /* 430 */ 1230, 1230, 1535, 1230, 1230, 1230, 1230, 1230, 1230, 1230,
160642 /* 440 */ 1230, 1230, 1230, 1411, 1230, 1233, 1526, 1230, 1230, 1230,
160643 /* 450 */ 1230, 1230, 1230, 1230, 1230, 1388, 1389, 1351, 1230, 1230,
160644 /* 460 */ 1230, 1230, 1230, 1230, 1230, 1403, 1230, 1230, 1230, 1398,
160645 /* 470 */ 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1621, 1230,
160646 /* 480 */ 1230, 1230, 1230, 1230, 1230, 1495, 1494, 1230, 1230, 1348,
160647 /* 490 */ 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230,
160648 /* 500 */ 1230, 1230, 1230, 1278, 1230, 1230, 1230, 1230, 1230, 1230,
160649 /* 510 */ 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230,
160650 /* 520 */ 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1376, 1230, 1230,
160651 /* 530 */ 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230,
160652 /* 540 */ 1230, 1230, 1565, 1366, 1230, 1230, 1612, 1230, 1230, 1230,
160653 /* 550 */ 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230, 1230,
160654 /* 560 */ 1230, 1606, 1322, 1413, 1230, 1412, 1416, 1252, 1230, 1242,
160655 /* 570 */ 1230, 1230,
160656};
160657/********** End of lemon-generated parsing tables *****************************/
160658
160659/* The next table maps tokens (terminal symbols) into fallback tokens.
160660** If a construct like the following:
160661**
160662** %fallback ID X Y Z.
160663**
160664** appears in the grammar, then ID becomes a fallback token for X, Y,
160665** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
160666** but it does not parse, the type of the token is changed to ID and
160667** the parse is retried before an error is thrown.
160668**
160669** This feature can be used, for example, to cause some keywords in a language
160670** to revert to identifiers if they keyword does not apply in the context where
160671** it appears.
160672*/
160673#ifdef YYFALLBACK
160674static const YYCODETYPE yyFallback[] = {
160675 0, /* $ => nothing */
160676 0, /* SEMI => nothing */
160677 59, /* EXPLAIN => ID */
160678 59, /* QUERY => ID */
160679 59, /* PLAN => ID */
160680 59, /* BEGIN => ID */
160681 0, /* TRANSACTION => nothing */
160682 59, /* DEFERRED => ID */
160683 59, /* IMMEDIATE => ID */
160684 59, /* EXCLUSIVE => ID */
160685 0, /* COMMIT => nothing */
160686 59, /* END => ID */
160687 59, /* ROLLBACK => ID */
160688 59, /* SAVEPOINT => ID */
160689 59, /* RELEASE => ID */
160690 0, /* TO => nothing */
160691 0, /* TABLE => nothing */
160692 0, /* CREATE => nothing */
160693 59, /* IF => ID */
160694 0, /* NOT => nothing */
160695 0, /* EXISTS => nothing */
160696 59, /* TEMP => ID */
160697 0, /* LP => nothing */
160698 0, /* RP => nothing */
160699 0, /* AS => nothing */
160700 0, /* COMMA => nothing */
160701 59, /* WITHOUT => ID */
160702 59, /* ABORT => ID */
160703 59, /* ACTION => ID */
160704 59, /* AFTER => ID */
160705 59, /* ANALYZE => ID */
160706 59, /* ASC => ID */
160707 59, /* ATTACH => ID */
160708 59, /* BEFORE => ID */
160709 59, /* BY => ID */
160710 59, /* CASCADE => ID */
160711 59, /* CAST => ID */
160712 59, /* CONFLICT => ID */
160713 59, /* DATABASE => ID */
160714 59, /* DESC => ID */
160715 59, /* DETACH => ID */
160716 59, /* EACH => ID */
160717 59, /* FAIL => ID */
160718 0, /* OR => nothing */
160719 0, /* AND => nothing */
160720 0, /* IS => nothing */
160721 59, /* MATCH => ID */
160722 59, /* LIKE_KW => ID */
160723 0, /* BETWEEN => nothing */
160724 0, /* IN => nothing */
160725 0, /* ISNULL => nothing */
160726 0, /* NOTNULL => nothing */
160727 0, /* NE => nothing */
160728 0, /* EQ => nothing */
160729 0, /* GT => nothing */
160730 0, /* LE => nothing */
160731 0, /* LT => nothing */
160732 0, /* GE => nothing */
160733 0, /* ESCAPE => nothing */
160734 0, /* ID => nothing */
160735 59, /* COLUMNKW => ID */
160736 59, /* DO => ID */
160737 59, /* FOR => ID */
160738 59, /* IGNORE => ID */
160739 59, /* INITIALLY => ID */
160740 59, /* INSTEAD => ID */
160741 59, /* NO => ID */
160742 59, /* KEY => ID */
160743 59, /* OF => ID */
160744 59, /* OFFSET => ID */
160745 59, /* PRAGMA => ID */
160746 59, /* RAISE => ID */
160747 59, /* RECURSIVE => ID */
160748 59, /* REPLACE => ID */
160749 59, /* RESTRICT => ID */
160750 59, /* ROW => ID */
160751 59, /* ROWS => ID */
160752 59, /* TRIGGER => ID */
160753 59, /* VACUUM => ID */
160754 59, /* VIEW => ID */
160755 59, /* VIRTUAL => ID */
160756 59, /* WITH => ID */
160757 59, /* NULLS => ID */
160758 59, /* FIRST => ID */
160759 59, /* LAST => ID */
160760 59, /* CURRENT => ID */
160761 59, /* FOLLOWING => ID */
160762 59, /* PARTITION => ID */
160763 59, /* PRECEDING => ID */
160764 59, /* RANGE => ID */
160765 59, /* UNBOUNDED => ID */
160766 59, /* EXCLUDE => ID */
160767 59, /* GROUPS => ID */
160768 59, /* OTHERS => ID */
160769 59, /* TIES => ID */
160770 59, /* GENERATED => ID */
160771 59, /* ALWAYS => ID */
160772 59, /* MATERIALIZED => ID */
160773 59, /* REINDEX => ID */
160774 59, /* RENAME => ID */
160775 59, /* CTIME_KW => ID */
160776 0, /* ANY => nothing */
160777 0, /* BITAND => nothing */
160778 0, /* BITOR => nothing */
160779 0, /* LSHIFT => nothing */
160780 0, /* RSHIFT => nothing */
160781 0, /* PLUS => nothing */
160782 0, /* MINUS => nothing */
160783 0, /* STAR => nothing */
160784 0, /* SLASH => nothing */
160785 0, /* REM => nothing */
160786 0, /* CONCAT => nothing */
160787 0, /* COLLATE => nothing */
160788 0, /* BITNOT => nothing */
160789 0, /* ON => nothing */
160790 0, /* INDEXED => nothing */
160791 0, /* STRING => nothing */
160792 0, /* JOIN_KW => nothing */
160793 0, /* CONSTRAINT => nothing */
160794 0, /* DEFAULT => nothing */
160795 0, /* NULL => nothing */
160796 0, /* PRIMARY => nothing */
160797 0, /* UNIQUE => nothing */
160798 0, /* CHECK => nothing */
160799 0, /* REFERENCES => nothing */
160800 0, /* AUTOINCR => nothing */
160801 0, /* INSERT => nothing */
160802 0, /* DELETE => nothing */
160803 0, /* UPDATE => nothing */
160804 0, /* SET => nothing */
160805 0, /* DEFERRABLE => nothing */
160806 0, /* FOREIGN => nothing */
160807 0, /* DROP => nothing */
160808 0, /* UNION => nothing */
160809 0, /* ALL => nothing */
160810 0, /* EXCEPT => nothing */
160811 0, /* INTERSECT => nothing */
160812 0, /* SELECT => nothing */
160813 0, /* VALUES => nothing */
160814 0, /* DISTINCT => nothing */
160815 0, /* DOT => nothing */
160816 0, /* FROM => nothing */
160817 0, /* JOIN => nothing */
160818 0, /* USING => nothing */
160819 0, /* ORDER => nothing */
160820 0, /* GROUP => nothing */
160821 0, /* HAVING => nothing */
160822 0, /* LIMIT => nothing */
160823 0, /* WHERE => nothing */
160824 0, /* RETURNING => nothing */
160825 0, /* INTO => nothing */
160826 0, /* NOTHING => nothing */
160827 0, /* FLOAT => nothing */
160828 0, /* BLOB => nothing */
160829 0, /* INTEGER => nothing */
160830 0, /* VARIABLE => nothing */
160831 0, /* CASE => nothing */
160832 0, /* WHEN => nothing */
160833 0, /* THEN => nothing */
160834 0, /* ELSE => nothing */
160835 0, /* INDEX => nothing */
160836 0, /* ALTER => nothing */
160837 0, /* ADD => nothing */
160838 0, /* WINDOW => nothing */
160839 0, /* OVER => nothing */
160840 0, /* FILTER => nothing */
160841 0, /* COLUMN => nothing */
160842 0, /* AGG_FUNCTION => nothing */
160843 0, /* AGG_COLUMN => nothing */
160844 0, /* TRUEFALSE => nothing */
160845 0, /* ISNOT => nothing */
160846 0, /* FUNCTION => nothing */
160847 0, /* UMINUS => nothing */
160848 0, /* UPLUS => nothing */
160849 0, /* TRUTH => nothing */
160850 0, /* REGISTER => nothing */
160851 0, /* VECTOR => nothing */
160852 0, /* SELECT_COLUMN => nothing */
160853 0, /* IF_NULL_ROW => nothing */
160854 0, /* ASTERISK => nothing */
160855 0, /* SPAN => nothing */
160856 0, /* ERROR => nothing */
160857 0, /* SPACE => nothing */
160858 0, /* ILLEGAL => nothing */
160859};
160860#endif /* YYFALLBACK */
160861
160862/* The following structure represents a single element of the
160863** parser's stack. Information stored includes:
160864**
160865** + The state number for the parser at this level of the stack.
160866**
160867** + The value of the token stored at this level of the stack.
160868** (In other words, the "major" token.)
160869**
160870** + The semantic value stored at this level of the stack. This is
160871** the information used by the action routines in the grammar.
160872** It is sometimes called the "minor" token.
160873**
160874** After the "shift" half of a SHIFTREDUCE action, the stateno field
160875** actually contains the reduce action for the second half of the
160876** SHIFTREDUCE.
160877*/
160878struct yyStackEntry {
160879 YYACTIONTYPE stateno; /* The state-number, or reduce action in SHIFTREDUCE */
160880 YYCODETYPE major; /* The major token value. This is the code
160881 ** number for the token at this stack level */
160882 YYMINORTYPE minor; /* The user-supplied minor token value. This
160883 ** is the value of the token */
160884};
160885typedef struct yyStackEntry yyStackEntry;
160886
160887/* The state of the parser is completely contained in an instance of
160888** the following structure */
160889struct yyParser {
160890 yyStackEntry *yytos; /* Pointer to top element of the stack */
160891#ifdef YYTRACKMAXSTACKDEPTH
160892 int yyhwm; /* High-water mark of the stack */
160893#endif
160894#ifndef YYNOERRORRECOVERY
160895 int yyerrcnt; /* Shifts left before out of the error */
160896#endif
160897 sqlite3ParserARG_SDECL /* A place to hold %extra_argument */
160898 sqlite3ParserCTX_SDECL /* A place to hold %extra_context */
160899#if YYSTACKDEPTH<=0
160900 int yystksz; /* Current side of the stack */
160901 yyStackEntry *yystack; /* The parser's stack */
160902 yyStackEntry yystk0; /* First stack entry */
160903#else
160904 yyStackEntry yystack[YYSTACKDEPTH]; /* The parser's stack */
160905 yyStackEntry *yystackEnd; /* Last entry in the stack */
160906#endif
160907};
160908typedef struct yyParser yyParser;
160909
160910/* #include <assert.h> */
160911#ifndef NDEBUG
160912/* #include <stdio.h> */
160913static FILE *yyTraceFILE = 0;
160914static char *yyTracePrompt = 0;
160915#endif /* NDEBUG */
160916
160917#ifndef NDEBUG
160918/*
160919** Turn parser tracing on by giving a stream to which to write the trace
160920** and a prompt to preface each trace message. Tracing is turned off
160921** by making either argument NULL
160922**
160923** Inputs:
160924** <ul>
160925** <li> A FILE* to which trace output should be written.
160926** If NULL, then tracing is turned off.
160927** <li> A prefix string written at the beginning of every
160928** line of trace output. If NULL, then tracing is
160929** turned off.
160930** </ul>
160931**
160932** Outputs:
160933** None.
160934*/
160935SQLITE_PRIVATE void sqlite3ParserTrace(FILE *TraceFILE, char *zTracePrompt){
160936 yyTraceFILE = TraceFILE;
160937 yyTracePrompt = zTracePrompt;
160938 if( yyTraceFILE==0 ) yyTracePrompt = 0;
160939 else if( yyTracePrompt==0 ) yyTraceFILE = 0;
160940}
160941#endif /* NDEBUG */
160942
160943#if defined(YYCOVERAGE) || !defined(NDEBUG)
160944/* For tracing shifts, the names of all terminals and nonterminals
160945** are required. The following table supplies these names */
160946static const char *const yyTokenName[] = {
160947 /* 0 */ "$",
160948 /* 1 */ "SEMI",
160949 /* 2 */ "EXPLAIN",
160950 /* 3 */ "QUERY",
160951 /* 4 */ "PLAN",
160952 /* 5 */ "BEGIN",
160953 /* 6 */ "TRANSACTION",
160954 /* 7 */ "DEFERRED",
160955 /* 8 */ "IMMEDIATE",
160956 /* 9 */ "EXCLUSIVE",
160957 /* 10 */ "COMMIT",
160958 /* 11 */ "END",
160959 /* 12 */ "ROLLBACK",
160960 /* 13 */ "SAVEPOINT",
160961 /* 14 */ "RELEASE",
160962 /* 15 */ "TO",
160963 /* 16 */ "TABLE",
160964 /* 17 */ "CREATE",
160965 /* 18 */ "IF",
160966 /* 19 */ "NOT",
160967 /* 20 */ "EXISTS",
160968 /* 21 */ "TEMP",
160969 /* 22 */ "LP",
160970 /* 23 */ "RP",
160971 /* 24 */ "AS",
160972 /* 25 */ "COMMA",
160973 /* 26 */ "WITHOUT",
160974 /* 27 */ "ABORT",
160975 /* 28 */ "ACTION",
160976 /* 29 */ "AFTER",
160977 /* 30 */ "ANALYZE",
160978 /* 31 */ "ASC",
160979 /* 32 */ "ATTACH",
160980 /* 33 */ "BEFORE",
160981 /* 34 */ "BY",
160982 /* 35 */ "CASCADE",
160983 /* 36 */ "CAST",
160984 /* 37 */ "CONFLICT",
160985 /* 38 */ "DATABASE",
160986 /* 39 */ "DESC",
160987 /* 40 */ "DETACH",
160988 /* 41 */ "EACH",
160989 /* 42 */ "FAIL",
160990 /* 43 */ "OR",
160991 /* 44 */ "AND",
160992 /* 45 */ "IS",
160993 /* 46 */ "MATCH",
160994 /* 47 */ "LIKE_KW",
160995 /* 48 */ "BETWEEN",
160996 /* 49 */ "IN",
160997 /* 50 */ "ISNULL",
160998 /* 51 */ "NOTNULL",
160999 /* 52 */ "NE",
161000 /* 53 */ "EQ",
161001 /* 54 */ "GT",
161002 /* 55 */ "LE",
161003 /* 56 */ "LT",
161004 /* 57 */ "GE",
161005 /* 58 */ "ESCAPE",
161006 /* 59 */ "ID",
161007 /* 60 */ "COLUMNKW",
161008 /* 61 */ "DO",
161009 /* 62 */ "FOR",
161010 /* 63 */ "IGNORE",
161011 /* 64 */ "INITIALLY",
161012 /* 65 */ "INSTEAD",
161013 /* 66 */ "NO",
161014 /* 67 */ "KEY",
161015 /* 68 */ "OF",
161016 /* 69 */ "OFFSET",
161017 /* 70 */ "PRAGMA",
161018 /* 71 */ "RAISE",
161019 /* 72 */ "RECURSIVE",
161020 /* 73 */ "REPLACE",
161021 /* 74 */ "RESTRICT",
161022 /* 75 */ "ROW",
161023 /* 76 */ "ROWS",
161024 /* 77 */ "TRIGGER",
161025 /* 78 */ "VACUUM",
161026 /* 79 */ "VIEW",
161027 /* 80 */ "VIRTUAL",
161028 /* 81 */ "WITH",
161029 /* 82 */ "NULLS",
161030 /* 83 */ "FIRST",
161031 /* 84 */ "LAST",
161032 /* 85 */ "CURRENT",
161033 /* 86 */ "FOLLOWING",
161034 /* 87 */ "PARTITION",
161035 /* 88 */ "PRECEDING",
161036 /* 89 */ "RANGE",
161037 /* 90 */ "UNBOUNDED",
161038 /* 91 */ "EXCLUDE",
161039 /* 92 */ "GROUPS",
161040 /* 93 */ "OTHERS",
161041 /* 94 */ "TIES",
161042 /* 95 */ "GENERATED",
161043 /* 96 */ "ALWAYS",
161044 /* 97 */ "MATERIALIZED",
161045 /* 98 */ "REINDEX",
161046 /* 99 */ "RENAME",
161047 /* 100 */ "CTIME_KW",
161048 /* 101 */ "ANY",
161049 /* 102 */ "BITAND",
161050 /* 103 */ "BITOR",
161051 /* 104 */ "LSHIFT",
161052 /* 105 */ "RSHIFT",
161053 /* 106 */ "PLUS",
161054 /* 107 */ "MINUS",
161055 /* 108 */ "STAR",
161056 /* 109 */ "SLASH",
161057 /* 110 */ "REM",
161058 /* 111 */ "CONCAT",
161059 /* 112 */ "COLLATE",
161060 /* 113 */ "BITNOT",
161061 /* 114 */ "ON",
161062 /* 115 */ "INDEXED",
161063 /* 116 */ "STRING",
161064 /* 117 */ "JOIN_KW",
161065 /* 118 */ "CONSTRAINT",
161066 /* 119 */ "DEFAULT",
161067 /* 120 */ "NULL",
161068 /* 121 */ "PRIMARY",
161069 /* 122 */ "UNIQUE",
161070 /* 123 */ "CHECK",
161071 /* 124 */ "REFERENCES",
161072 /* 125 */ "AUTOINCR",
161073 /* 126 */ "INSERT",
161074 /* 127 */ "DELETE",
161075 /* 128 */ "UPDATE",
161076 /* 129 */ "SET",
161077 /* 130 */ "DEFERRABLE",
161078 /* 131 */ "FOREIGN",
161079 /* 132 */ "DROP",
161080 /* 133 */ "UNION",
161081 /* 134 */ "ALL",
161082 /* 135 */ "EXCEPT",
161083 /* 136 */ "INTERSECT",
161084 /* 137 */ "SELECT",
161085 /* 138 */ "VALUES",
161086 /* 139 */ "DISTINCT",
161087 /* 140 */ "DOT",
161088 /* 141 */ "FROM",
161089 /* 142 */ "JOIN",
161090 /* 143 */ "USING",
161091 /* 144 */ "ORDER",
161092 /* 145 */ "GROUP",
161093 /* 146 */ "HAVING",
161094 /* 147 */ "LIMIT",
161095 /* 148 */ "WHERE",
161096 /* 149 */ "RETURNING",
161097 /* 150 */ "INTO",
161098 /* 151 */ "NOTHING",
161099 /* 152 */ "FLOAT",
161100 /* 153 */ "BLOB",
161101 /* 154 */ "INTEGER",
161102 /* 155 */ "VARIABLE",
161103 /* 156 */ "CASE",
161104 /* 157 */ "WHEN",
161105 /* 158 */ "THEN",
161106 /* 159 */ "ELSE",
161107 /* 160 */ "INDEX",
161108 /* 161 */ "ALTER",
161109 /* 162 */ "ADD",
161110 /* 163 */ "WINDOW",
161111 /* 164 */ "OVER",
161112 /* 165 */ "FILTER",
161113 /* 166 */ "COLUMN",
161114 /* 167 */ "AGG_FUNCTION",
161115 /* 168 */ "AGG_COLUMN",
161116 /* 169 */ "TRUEFALSE",
161117 /* 170 */ "ISNOT",
161118 /* 171 */ "FUNCTION",
161119 /* 172 */ "UMINUS",
161120 /* 173 */ "UPLUS",
161121 /* 174 */ "TRUTH",
161122 /* 175 */ "REGISTER",
161123 /* 176 */ "VECTOR",
161124 /* 177 */ "SELECT_COLUMN",
161125 /* 178 */ "IF_NULL_ROW",
161126 /* 179 */ "ASTERISK",
161127 /* 180 */ "SPAN",
161128 /* 181 */ "ERROR",
161129 /* 182 */ "SPACE",
161130 /* 183 */ "ILLEGAL",
161131 /* 184 */ "input",
161132 /* 185 */ "cmdlist",
161133 /* 186 */ "ecmd",
161134 /* 187 */ "cmdx",
161135 /* 188 */ "explain",
161136 /* 189 */ "cmd",
161137 /* 190 */ "transtype",
161138 /* 191 */ "trans_opt",
161139 /* 192 */ "nm",
161140 /* 193 */ "savepoint_opt",
161141 /* 194 */ "create_table",
161142 /* 195 */ "create_table_args",
161143 /* 196 */ "createkw",
161144 /* 197 */ "temp",
161145 /* 198 */ "ifnotexists",
161146 /* 199 */ "dbnm",
161147 /* 200 */ "columnlist",
161148 /* 201 */ "conslist_opt",
161149 /* 202 */ "table_option_set",
161150 /* 203 */ "select",
161151 /* 204 */ "table_option",
161152 /* 205 */ "columnname",
161153 /* 206 */ "carglist",
161154 /* 207 */ "typetoken",
161155 /* 208 */ "typename",
161156 /* 209 */ "signed",
161157 /* 210 */ "plus_num",
161158 /* 211 */ "minus_num",
161159 /* 212 */ "scanpt",
161160 /* 213 */ "scantok",
161161 /* 214 */ "ccons",
161162 /* 215 */ "term",
161163 /* 216 */ "expr",
161164 /* 217 */ "onconf",
161165 /* 218 */ "sortorder",
161166 /* 219 */ "autoinc",
161167 /* 220 */ "eidlist_opt",
161168 /* 221 */ "refargs",
161169 /* 222 */ "defer_subclause",
161170 /* 223 */ "generated",
161171 /* 224 */ "refarg",
161172 /* 225 */ "refact",
161173 /* 226 */ "init_deferred_pred_opt",
161174 /* 227 */ "conslist",
161175 /* 228 */ "tconscomma",
161176 /* 229 */ "tcons",
161177 /* 230 */ "sortlist",
161178 /* 231 */ "eidlist",
161179 /* 232 */ "defer_subclause_opt",
161180 /* 233 */ "orconf",
161181 /* 234 */ "resolvetype",
161182 /* 235 */ "raisetype",
161183 /* 236 */ "ifexists",
161184 /* 237 */ "fullname",
161185 /* 238 */ "selectnowith",
161186 /* 239 */ "oneselect",
161187 /* 240 */ "wqlist",
161188 /* 241 */ "multiselect_op",
161189 /* 242 */ "distinct",
161190 /* 243 */ "selcollist",
161191 /* 244 */ "from",
161192 /* 245 */ "where_opt",
161193 /* 246 */ "groupby_opt",
161194 /* 247 */ "having_opt",
161195 /* 248 */ "orderby_opt",
161196 /* 249 */ "limit_opt",
161197 /* 250 */ "window_clause",
161198 /* 251 */ "values",
161199 /* 252 */ "nexprlist",
161200 /* 253 */ "sclp",
161201 /* 254 */ "as",
161202 /* 255 */ "seltablist",
161203 /* 256 */ "stl_prefix",
161204 /* 257 */ "joinop",
161205 /* 258 */ "indexed_opt",
161206 /* 259 */ "on_opt",
161207 /* 260 */ "using_opt",
161208 /* 261 */ "exprlist",
161209 /* 262 */ "xfullname",
161210 /* 263 */ "idlist",
161211 /* 264 */ "nulls",
161212 /* 265 */ "with",
161213 /* 266 */ "where_opt_ret",
161214 /* 267 */ "setlist",
161215 /* 268 */ "insert_cmd",
161216 /* 269 */ "idlist_opt",
161217 /* 270 */ "upsert",
161218 /* 271 */ "returning",
161219 /* 272 */ "filter_over",
161220 /* 273 */ "likeop",
161221 /* 274 */ "between_op",
161222 /* 275 */ "in_op",
161223 /* 276 */ "paren_exprlist",
161224 /* 277 */ "case_operand",
161225 /* 278 */ "case_exprlist",
161226 /* 279 */ "case_else",
161227 /* 280 */ "uniqueflag",
161228 /* 281 */ "collate",
161229 /* 282 */ "vinto",
161230 /* 283 */ "nmnum",
161231 /* 284 */ "trigger_decl",
161232 /* 285 */ "trigger_cmd_list",
161233 /* 286 */ "trigger_time",
161234 /* 287 */ "trigger_event",
161235 /* 288 */ "foreach_clause",
161236 /* 289 */ "when_clause",
161237 /* 290 */ "trigger_cmd",
161238 /* 291 */ "trnm",
161239 /* 292 */ "tridxby",
161240 /* 293 */ "database_kw_opt",
161241 /* 294 */ "key_opt",
161242 /* 295 */ "add_column_fullname",
161243 /* 296 */ "kwcolumn_opt",
161244 /* 297 */ "create_vtab",
161245 /* 298 */ "vtabarglist",
161246 /* 299 */ "vtabarg",
161247 /* 300 */ "vtabargtoken",
161248 /* 301 */ "lp",
161249 /* 302 */ "anylist",
161250 /* 303 */ "wqitem",
161251 /* 304 */ "wqas",
161252 /* 305 */ "windowdefn_list",
161253 /* 306 */ "windowdefn",
161254 /* 307 */ "window",
161255 /* 308 */ "frame_opt",
161256 /* 309 */ "part_opt",
161257 /* 310 */ "filter_clause",
161258 /* 311 */ "over_clause",
161259 /* 312 */ "range_or_rows",
161260 /* 313 */ "frame_bound",
161261 /* 314 */ "frame_bound_s",
161262 /* 315 */ "frame_bound_e",
161263 /* 316 */ "frame_exclude_opt",
161264 /* 317 */ "frame_exclude",
161265};
161266#endif /* defined(YYCOVERAGE) || !defined(NDEBUG) */
161267
161268#ifndef NDEBUG
161269/* For tracing reduce actions, the names of all rules are required.
161270*/
161271static const char *const yyRuleName[] = {
161272 /* 0 */ "explain ::= EXPLAIN",
161273 /* 1 */ "explain ::= EXPLAIN QUERY PLAN",
161274 /* 2 */ "cmdx ::= cmd",
161275 /* 3 */ "cmd ::= BEGIN transtype trans_opt",
161276 /* 4 */ "transtype ::=",
161277 /* 5 */ "transtype ::= DEFERRED",
161278 /* 6 */ "transtype ::= IMMEDIATE",
161279 /* 7 */ "transtype ::= EXCLUSIVE",
161280 /* 8 */ "cmd ::= COMMIT|END trans_opt",
161281 /* 9 */ "cmd ::= ROLLBACK trans_opt",
161282 /* 10 */ "cmd ::= SAVEPOINT nm",
161283 /* 11 */ "cmd ::= RELEASE savepoint_opt nm",
161284 /* 12 */ "cmd ::= ROLLBACK trans_opt TO savepoint_opt nm",
161285 /* 13 */ "create_table ::= createkw temp TABLE ifnotexists nm dbnm",
161286 /* 14 */ "createkw ::= CREATE",
161287 /* 15 */ "ifnotexists ::=",
161288 /* 16 */ "ifnotexists ::= IF NOT EXISTS",
161289 /* 17 */ "temp ::= TEMP",
161290 /* 18 */ "temp ::=",
161291 /* 19 */ "create_table_args ::= LP columnlist conslist_opt RP table_option_set",
161292 /* 20 */ "create_table_args ::= AS select",
161293 /* 21 */ "table_option_set ::=",
161294 /* 22 */ "table_option_set ::= table_option_set COMMA table_option",
161295 /* 23 */ "table_option ::= WITHOUT nm",
161296 /* 24 */ "table_option ::= nm",
161297 /* 25 */ "columnname ::= nm typetoken",
161298 /* 26 */ "typetoken ::=",
161299 /* 27 */ "typetoken ::= typename LP signed RP",
161300 /* 28 */ "typetoken ::= typename LP signed COMMA signed RP",
161301 /* 29 */ "typename ::= typename ID|STRING",
161302 /* 30 */ "scanpt ::=",
161303 /* 31 */ "scantok ::=",
161304 /* 32 */ "ccons ::= CONSTRAINT nm",
161305 /* 33 */ "ccons ::= DEFAULT scantok term",
161306 /* 34 */ "ccons ::= DEFAULT LP expr RP",
161307 /* 35 */ "ccons ::= DEFAULT PLUS scantok term",
161308 /* 36 */ "ccons ::= DEFAULT MINUS scantok term",
161309 /* 37 */ "ccons ::= DEFAULT scantok ID|INDEXED",
161310 /* 38 */ "ccons ::= NOT NULL onconf",
161311 /* 39 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc",
161312 /* 40 */ "ccons ::= UNIQUE onconf",
161313 /* 41 */ "ccons ::= CHECK LP expr RP",
161314 /* 42 */ "ccons ::= REFERENCES nm eidlist_opt refargs",
161315 /* 43 */ "ccons ::= defer_subclause",
161316 /* 44 */ "ccons ::= COLLATE ID|STRING",
161317 /* 45 */ "generated ::= LP expr RP",
161318 /* 46 */ "generated ::= LP expr RP ID",
161319 /* 47 */ "autoinc ::=",
161320 /* 48 */ "autoinc ::= AUTOINCR",
161321 /* 49 */ "refargs ::=",
161322 /* 50 */ "refargs ::= refargs refarg",
161323 /* 51 */ "refarg ::= MATCH nm",
161324 /* 52 */ "refarg ::= ON INSERT refact",
161325 /* 53 */ "refarg ::= ON DELETE refact",
161326 /* 54 */ "refarg ::= ON UPDATE refact",
161327 /* 55 */ "refact ::= SET NULL",
161328 /* 56 */ "refact ::= SET DEFAULT",
161329 /* 57 */ "refact ::= CASCADE",
161330 /* 58 */ "refact ::= RESTRICT",
161331 /* 59 */ "refact ::= NO ACTION",
161332 /* 60 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt",
161333 /* 61 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt",
161334 /* 62 */ "init_deferred_pred_opt ::=",
161335 /* 63 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED",
161336 /* 64 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE",
161337 /* 65 */ "conslist_opt ::=",
161338 /* 66 */ "tconscomma ::= COMMA",
161339 /* 67 */ "tcons ::= CONSTRAINT nm",
161340 /* 68 */ "tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf",
161341 /* 69 */ "tcons ::= UNIQUE LP sortlist RP onconf",
161342 /* 70 */ "tcons ::= CHECK LP expr RP onconf",
161343 /* 71 */ "tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt",
161344 /* 72 */ "defer_subclause_opt ::=",
161345 /* 73 */ "onconf ::=",
161346 /* 74 */ "onconf ::= ON CONFLICT resolvetype",
161347 /* 75 */ "orconf ::=",
161348 /* 76 */ "orconf ::= OR resolvetype",
161349 /* 77 */ "resolvetype ::= IGNORE",
161350 /* 78 */ "resolvetype ::= REPLACE",
161351 /* 79 */ "cmd ::= DROP TABLE ifexists fullname",
161352 /* 80 */ "ifexists ::= IF EXISTS",
161353 /* 81 */ "ifexists ::=",
161354 /* 82 */ "cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select",
161355 /* 83 */ "cmd ::= DROP VIEW ifexists fullname",
161356 /* 84 */ "cmd ::= select",
161357 /* 85 */ "select ::= WITH wqlist selectnowith",
161358 /* 86 */ "select ::= WITH RECURSIVE wqlist selectnowith",
161359 /* 87 */ "select ::= selectnowith",
161360 /* 88 */ "selectnowith ::= selectnowith multiselect_op oneselect",
161361 /* 89 */ "multiselect_op ::= UNION",
161362 /* 90 */ "multiselect_op ::= UNION ALL",
161363 /* 91 */ "multiselect_op ::= EXCEPT|INTERSECT",
161364 /* 92 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt",
161365 /* 93 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt",
161366 /* 94 */ "values ::= VALUES LP nexprlist RP",
161367 /* 95 */ "values ::= values COMMA LP nexprlist RP",
161368 /* 96 */ "distinct ::= DISTINCT",
161369 /* 97 */ "distinct ::= ALL",
161370 /* 98 */ "distinct ::=",
161371 /* 99 */ "sclp ::=",
161372 /* 100 */ "selcollist ::= sclp scanpt expr scanpt as",
161373 /* 101 */ "selcollist ::= sclp scanpt STAR",
161374 /* 102 */ "selcollist ::= sclp scanpt nm DOT STAR",
161375 /* 103 */ "as ::= AS nm",
161376 /* 104 */ "as ::=",
161377 /* 105 */ "from ::=",
161378 /* 106 */ "from ::= FROM seltablist",
161379 /* 107 */ "stl_prefix ::= seltablist joinop",
161380 /* 108 */ "stl_prefix ::=",
161381 /* 109 */ "seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt",
161382 /* 110 */ "seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt",
161383 /* 111 */ "seltablist ::= stl_prefix LP select RP as on_opt using_opt",
161384 /* 112 */ "seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt",
161385 /* 113 */ "dbnm ::=",
161386 /* 114 */ "dbnm ::= DOT nm",
161387 /* 115 */ "fullname ::= nm",
161388 /* 116 */ "fullname ::= nm DOT nm",
161389 /* 117 */ "xfullname ::= nm",
161390 /* 118 */ "xfullname ::= nm DOT nm",
161391 /* 119 */ "xfullname ::= nm DOT nm AS nm",
161392 /* 120 */ "xfullname ::= nm AS nm",
161393 /* 121 */ "joinop ::= COMMA|JOIN",
161394 /* 122 */ "joinop ::= JOIN_KW JOIN",
161395 /* 123 */ "joinop ::= JOIN_KW nm JOIN",
161396 /* 124 */ "joinop ::= JOIN_KW nm nm JOIN",
161397 /* 125 */ "on_opt ::= ON expr",
161398 /* 126 */ "on_opt ::=",
161399 /* 127 */ "indexed_opt ::=",
161400 /* 128 */ "indexed_opt ::= INDEXED BY nm",
161401 /* 129 */ "indexed_opt ::= NOT INDEXED",
161402 /* 130 */ "using_opt ::= USING LP idlist RP",
161403 /* 131 */ "using_opt ::=",
161404 /* 132 */ "orderby_opt ::=",
161405 /* 133 */ "orderby_opt ::= ORDER BY sortlist",
161406 /* 134 */ "sortlist ::= sortlist COMMA expr sortorder nulls",
161407 /* 135 */ "sortlist ::= expr sortorder nulls",
161408 /* 136 */ "sortorder ::= ASC",
161409 /* 137 */ "sortorder ::= DESC",
161410 /* 138 */ "sortorder ::=",
161411 /* 139 */ "nulls ::= NULLS FIRST",
161412 /* 140 */ "nulls ::= NULLS LAST",
161413 /* 141 */ "nulls ::=",
161414 /* 142 */ "groupby_opt ::=",
161415 /* 143 */ "groupby_opt ::= GROUP BY nexprlist",
161416 /* 144 */ "having_opt ::=",
161417 /* 145 */ "having_opt ::= HAVING expr",
161418 /* 146 */ "limit_opt ::=",
161419 /* 147 */ "limit_opt ::= LIMIT expr",
161420 /* 148 */ "limit_opt ::= LIMIT expr OFFSET expr",
161421 /* 149 */ "limit_opt ::= LIMIT expr COMMA expr",
161422 /* 150 */ "cmd ::= with DELETE FROM xfullname indexed_opt where_opt_ret",
161423 /* 151 */ "where_opt ::=",
161424 /* 152 */ "where_opt ::= WHERE expr",
161425 /* 153 */ "where_opt_ret ::=",
161426 /* 154 */ "where_opt_ret ::= WHERE expr",
161427 /* 155 */ "where_opt_ret ::= RETURNING selcollist",
161428 /* 156 */ "where_opt_ret ::= WHERE expr RETURNING selcollist",
161429 /* 157 */ "cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret",
161430 /* 158 */ "setlist ::= setlist COMMA nm EQ expr",
161431 /* 159 */ "setlist ::= setlist COMMA LP idlist RP EQ expr",
161432 /* 160 */ "setlist ::= nm EQ expr",
161433 /* 161 */ "setlist ::= LP idlist RP EQ expr",
161434 /* 162 */ "cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert",
161435 /* 163 */ "cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES returning",
161436 /* 164 */ "upsert ::=",
161437 /* 165 */ "upsert ::= RETURNING selcollist",
161438 /* 166 */ "upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt upsert",
161439 /* 167 */ "upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING upsert",
161440 /* 168 */ "upsert ::= ON CONFLICT DO NOTHING returning",
161441 /* 169 */ "upsert ::= ON CONFLICT DO UPDATE SET setlist where_opt returning",
161442 /* 170 */ "returning ::= RETURNING selcollist",
161443 /* 171 */ "insert_cmd ::= INSERT orconf",
161444 /* 172 */ "insert_cmd ::= REPLACE",
161445 /* 173 */ "idlist_opt ::=",
161446 /* 174 */ "idlist_opt ::= LP idlist RP",
161447 /* 175 */ "idlist ::= idlist COMMA nm",
161448 /* 176 */ "idlist ::= nm",
161449 /* 177 */ "expr ::= LP expr RP",
161450 /* 178 */ "expr ::= ID|INDEXED",
161451 /* 179 */ "expr ::= JOIN_KW",
161452 /* 180 */ "expr ::= nm DOT nm",
161453 /* 181 */ "expr ::= nm DOT nm DOT nm",
161454 /* 182 */ "term ::= NULL|FLOAT|BLOB",
161455 /* 183 */ "term ::= STRING",
161456 /* 184 */ "term ::= INTEGER",
161457 /* 185 */ "expr ::= VARIABLE",
161458 /* 186 */ "expr ::= expr COLLATE ID|STRING",
161459 /* 187 */ "expr ::= CAST LP expr AS typetoken RP",
161460 /* 188 */ "expr ::= ID|INDEXED LP distinct exprlist RP",
161461 /* 189 */ "expr ::= ID|INDEXED LP STAR RP",
161462 /* 190 */ "expr ::= ID|INDEXED LP distinct exprlist RP filter_over",
161463 /* 191 */ "expr ::= ID|INDEXED LP STAR RP filter_over",
161464 /* 192 */ "term ::= CTIME_KW",
161465 /* 193 */ "expr ::= LP nexprlist COMMA expr RP",
161466 /* 194 */ "expr ::= expr AND expr",
161467 /* 195 */ "expr ::= expr OR expr",
161468 /* 196 */ "expr ::= expr LT|GT|GE|LE expr",
161469 /* 197 */ "expr ::= expr EQ|NE expr",
161470 /* 198 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr",
161471 /* 199 */ "expr ::= expr PLUS|MINUS expr",
161472 /* 200 */ "expr ::= expr STAR|SLASH|REM expr",
161473 /* 201 */ "expr ::= expr CONCAT expr",
161474 /* 202 */ "likeop ::= NOT LIKE_KW|MATCH",
161475 /* 203 */ "expr ::= expr likeop expr",
161476 /* 204 */ "expr ::= expr likeop expr ESCAPE expr",
161477 /* 205 */ "expr ::= expr ISNULL|NOTNULL",
161478 /* 206 */ "expr ::= expr NOT NULL",
161479 /* 207 */ "expr ::= expr IS expr",
161480 /* 208 */ "expr ::= expr IS NOT expr",
161481 /* 209 */ "expr ::= NOT expr",
161482 /* 210 */ "expr ::= BITNOT expr",
161483 /* 211 */ "expr ::= PLUS|MINUS expr",
161484 /* 212 */ "between_op ::= BETWEEN",
161485 /* 213 */ "between_op ::= NOT BETWEEN",
161486 /* 214 */ "expr ::= expr between_op expr AND expr",
161487 /* 215 */ "in_op ::= IN",
161488 /* 216 */ "in_op ::= NOT IN",
161489 /* 217 */ "expr ::= expr in_op LP exprlist RP",
161490 /* 218 */ "expr ::= LP select RP",
161491 /* 219 */ "expr ::= expr in_op LP select RP",
161492 /* 220 */ "expr ::= expr in_op nm dbnm paren_exprlist",
161493 /* 221 */ "expr ::= EXISTS LP select RP",
161494 /* 222 */ "expr ::= CASE case_operand case_exprlist case_else END",
161495 /* 223 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr",
161496 /* 224 */ "case_exprlist ::= WHEN expr THEN expr",
161497 /* 225 */ "case_else ::= ELSE expr",
161498 /* 226 */ "case_else ::=",
161499 /* 227 */ "case_operand ::= expr",
161500 /* 228 */ "case_operand ::=",
161501 /* 229 */ "exprlist ::=",
161502 /* 230 */ "nexprlist ::= nexprlist COMMA expr",
161503 /* 231 */ "nexprlist ::= expr",
161504 /* 232 */ "paren_exprlist ::=",
161505 /* 233 */ "paren_exprlist ::= LP exprlist RP",
161506 /* 234 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt",
161507 /* 235 */ "uniqueflag ::= UNIQUE",
161508 /* 236 */ "uniqueflag ::=",
161509 /* 237 */ "eidlist_opt ::=",
161510 /* 238 */ "eidlist_opt ::= LP eidlist RP",
161511 /* 239 */ "eidlist ::= eidlist COMMA nm collate sortorder",
161512 /* 240 */ "eidlist ::= nm collate sortorder",
161513 /* 241 */ "collate ::=",
161514 /* 242 */ "collate ::= COLLATE ID|STRING",
161515 /* 243 */ "cmd ::= DROP INDEX ifexists fullname",
161516 /* 244 */ "cmd ::= VACUUM vinto",
161517 /* 245 */ "cmd ::= VACUUM nm vinto",
161518 /* 246 */ "vinto ::= INTO expr",
161519 /* 247 */ "vinto ::=",
161520 /* 248 */ "cmd ::= PRAGMA nm dbnm",
161521 /* 249 */ "cmd ::= PRAGMA nm dbnm EQ nmnum",
161522 /* 250 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP",
161523 /* 251 */ "cmd ::= PRAGMA nm dbnm EQ minus_num",
161524 /* 252 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP",
161525 /* 253 */ "plus_num ::= PLUS INTEGER|FLOAT",
161526 /* 254 */ "minus_num ::= MINUS INTEGER|FLOAT",
161527 /* 255 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END",
161528 /* 256 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause",
161529 /* 257 */ "trigger_time ::= BEFORE|AFTER",
161530 /* 258 */ "trigger_time ::= INSTEAD OF",
161531 /* 259 */ "trigger_time ::=",
161532 /* 260 */ "trigger_event ::= DELETE|INSERT",
161533 /* 261 */ "trigger_event ::= UPDATE",
161534 /* 262 */ "trigger_event ::= UPDATE OF idlist",
161535 /* 263 */ "when_clause ::=",
161536 /* 264 */ "when_clause ::= WHEN expr",
161537 /* 265 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI",
161538 /* 266 */ "trigger_cmd_list ::= trigger_cmd SEMI",
161539 /* 267 */ "trnm ::= nm DOT nm",
161540 /* 268 */ "tridxby ::= INDEXED BY nm",
161541 /* 269 */ "tridxby ::= NOT INDEXED",
161542 /* 270 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist from where_opt scanpt",
161543 /* 271 */ "trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt",
161544 /* 272 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt",
161545 /* 273 */ "trigger_cmd ::= scanpt select scanpt",
161546 /* 274 */ "expr ::= RAISE LP IGNORE RP",
161547 /* 275 */ "expr ::= RAISE LP raisetype COMMA nm RP",
161548 /* 276 */ "raisetype ::= ROLLBACK",
161549 /* 277 */ "raisetype ::= ABORT",
161550 /* 278 */ "raisetype ::= FAIL",
161551 /* 279 */ "cmd ::= DROP TRIGGER ifexists fullname",
161552 /* 280 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt",
161553 /* 281 */ "cmd ::= DETACH database_kw_opt expr",
161554 /* 282 */ "key_opt ::=",
161555 /* 283 */ "key_opt ::= KEY expr",
161556 /* 284 */ "cmd ::= REINDEX",
161557 /* 285 */ "cmd ::= REINDEX nm dbnm",
161558 /* 286 */ "cmd ::= ANALYZE",
161559 /* 287 */ "cmd ::= ANALYZE nm dbnm",
161560 /* 288 */ "cmd ::= ALTER TABLE fullname RENAME TO nm",
161561 /* 289 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist",
161562 /* 290 */ "cmd ::= ALTER TABLE fullname DROP kwcolumn_opt nm",
161563 /* 291 */ "add_column_fullname ::= fullname",
161564 /* 292 */ "cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm",
161565 /* 293 */ "cmd ::= create_vtab",
161566 /* 294 */ "cmd ::= create_vtab LP vtabarglist RP",
161567 /* 295 */ "create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm",
161568 /* 296 */ "vtabarg ::=",
161569 /* 297 */ "vtabargtoken ::= ANY",
161570 /* 298 */ "vtabargtoken ::= lp anylist RP",
161571 /* 299 */ "lp ::= LP",
161572 /* 300 */ "with ::= WITH wqlist",
161573 /* 301 */ "with ::= WITH RECURSIVE wqlist",
161574 /* 302 */ "wqas ::= AS",
161575 /* 303 */ "wqas ::= AS MATERIALIZED",
161576 /* 304 */ "wqas ::= AS NOT MATERIALIZED",
161577 /* 305 */ "wqitem ::= nm eidlist_opt wqas LP select RP",
161578 /* 306 */ "wqlist ::= wqitem",
161579 /* 307 */ "wqlist ::= wqlist COMMA wqitem",
161580 /* 308 */ "windowdefn_list ::= windowdefn",
161581 /* 309 */ "windowdefn_list ::= windowdefn_list COMMA windowdefn",
161582 /* 310 */ "windowdefn ::= nm AS LP window RP",
161583 /* 311 */ "window ::= PARTITION BY nexprlist orderby_opt frame_opt",
161584 /* 312 */ "window ::= nm PARTITION BY nexprlist orderby_opt frame_opt",
161585 /* 313 */ "window ::= ORDER BY sortlist frame_opt",
161586 /* 314 */ "window ::= nm ORDER BY sortlist frame_opt",
161587 /* 315 */ "window ::= frame_opt",
161588 /* 316 */ "window ::= nm frame_opt",
161589 /* 317 */ "frame_opt ::=",
161590 /* 318 */ "frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt",
161591 /* 319 */ "frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt",
161592 /* 320 */ "range_or_rows ::= RANGE|ROWS|GROUPS",
161593 /* 321 */ "frame_bound_s ::= frame_bound",
161594 /* 322 */ "frame_bound_s ::= UNBOUNDED PRECEDING",
161595 /* 323 */ "frame_bound_e ::= frame_bound",
161596 /* 324 */ "frame_bound_e ::= UNBOUNDED FOLLOWING",
161597 /* 325 */ "frame_bound ::= expr PRECEDING|FOLLOWING",
161598 /* 326 */ "frame_bound ::= CURRENT ROW",
161599 /* 327 */ "frame_exclude_opt ::=",
161600 /* 328 */ "frame_exclude_opt ::= EXCLUDE frame_exclude",
161601 /* 329 */ "frame_exclude ::= NO OTHERS",
161602 /* 330 */ "frame_exclude ::= CURRENT ROW",
161603 /* 331 */ "frame_exclude ::= GROUP|TIES",
161604 /* 332 */ "window_clause ::= WINDOW windowdefn_list",
161605 /* 333 */ "filter_over ::= filter_clause over_clause",
161606 /* 334 */ "filter_over ::= over_clause",
161607 /* 335 */ "filter_over ::= filter_clause",
161608 /* 336 */ "over_clause ::= OVER LP window RP",
161609 /* 337 */ "over_clause ::= OVER nm",
161610 /* 338 */ "filter_clause ::= FILTER LP WHERE expr RP",
161611 /* 339 */ "input ::= cmdlist",
161612 /* 340 */ "cmdlist ::= cmdlist ecmd",
161613 /* 341 */ "cmdlist ::= ecmd",
161614 /* 342 */ "ecmd ::= SEMI",
161615 /* 343 */ "ecmd ::= cmdx SEMI",
161616 /* 344 */ "ecmd ::= explain cmdx SEMI",
161617 /* 345 */ "trans_opt ::=",
161618 /* 346 */ "trans_opt ::= TRANSACTION",
161619 /* 347 */ "trans_opt ::= TRANSACTION nm",
161620 /* 348 */ "savepoint_opt ::= SAVEPOINT",
161621 /* 349 */ "savepoint_opt ::=",
161622 /* 350 */ "cmd ::= create_table create_table_args",
161623 /* 351 */ "table_option_set ::= table_option",
161624 /* 352 */ "columnlist ::= columnlist COMMA columnname carglist",
161625 /* 353 */ "columnlist ::= columnname carglist",
161626 /* 354 */ "nm ::= ID|INDEXED",
161627 /* 355 */ "nm ::= STRING",
161628 /* 356 */ "nm ::= JOIN_KW",
161629 /* 357 */ "typetoken ::= typename",
161630 /* 358 */ "typename ::= ID|STRING",
161631 /* 359 */ "signed ::= plus_num",
161632 /* 360 */ "signed ::= minus_num",
161633 /* 361 */ "carglist ::= carglist ccons",
161634 /* 362 */ "carglist ::=",
161635 /* 363 */ "ccons ::= NULL onconf",
161636 /* 364 */ "ccons ::= GENERATED ALWAYS AS generated",
161637 /* 365 */ "ccons ::= AS generated",
161638 /* 366 */ "conslist_opt ::= COMMA conslist",
161639 /* 367 */ "conslist ::= conslist tconscomma tcons",
161640 /* 368 */ "conslist ::= tcons",
161641 /* 369 */ "tconscomma ::=",
161642 /* 370 */ "defer_subclause_opt ::= defer_subclause",
161643 /* 371 */ "resolvetype ::= raisetype",
161644 /* 372 */ "selectnowith ::= oneselect",
161645 /* 373 */ "oneselect ::= values",
161646 /* 374 */ "sclp ::= selcollist COMMA",
161647 /* 375 */ "as ::= ID|STRING",
161648 /* 376 */ "returning ::=",
161649 /* 377 */ "expr ::= term",
161650 /* 378 */ "likeop ::= LIKE_KW|MATCH",
161651 /* 379 */ "exprlist ::= nexprlist",
161652 /* 380 */ "nmnum ::= plus_num",
161653 /* 381 */ "nmnum ::= nm",
161654 /* 382 */ "nmnum ::= ON",
161655 /* 383 */ "nmnum ::= DELETE",
161656 /* 384 */ "nmnum ::= DEFAULT",
161657 /* 385 */ "plus_num ::= INTEGER|FLOAT",
161658 /* 386 */ "foreach_clause ::=",
161659 /* 387 */ "foreach_clause ::= FOR EACH ROW",
161660 /* 388 */ "trnm ::= nm",
161661 /* 389 */ "tridxby ::=",
161662 /* 390 */ "database_kw_opt ::= DATABASE",
161663 /* 391 */ "database_kw_opt ::=",
161664 /* 392 */ "kwcolumn_opt ::=",
161665 /* 393 */ "kwcolumn_opt ::= COLUMNKW",
161666 /* 394 */ "vtabarglist ::= vtabarg",
161667 /* 395 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
161668 /* 396 */ "vtabarg ::= vtabarg vtabargtoken",
161669 /* 397 */ "anylist ::=",
161670 /* 398 */ "anylist ::= anylist LP anylist RP",
161671 /* 399 */ "anylist ::= anylist ANY",
161672 /* 400 */ "with ::=",
161673};
161674#endif /* NDEBUG */
161675
161676
161677#if YYSTACKDEPTH<=0
161678/*
161679** Try to increase the size of the parser stack. Return the number
161680** of errors. Return 0 on success.
161681*/
161682static int yyGrowStack(yyParser *p){
161683 int newSize;
161684 int idx;
161685 yyStackEntry *pNew;
161686
161687 newSize = p->yystksz*2 + 100;
161688 idx = p->yytos ? (int)(p->yytos - p->yystack) : 0;
161689 if( p->yystack==&p->yystk0 ){
161690 pNew = malloc(newSize*sizeof(pNew[0]));
161691 if( pNew ) pNew[0] = p->yystk0;
161692 }else{
161693 pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
161694 }
161695 if( pNew ){
161696 p->yystack = pNew;
161697 p->yytos = &p->yystack[idx];
161698#ifndef NDEBUG
161699 if( yyTraceFILE ){
161700 fprintf(yyTraceFILE,"%sStack grows from %d to %d entries.\n",
161701 yyTracePrompt, p->yystksz, newSize);
161702 }
161703#endif
161704 p->yystksz = newSize;
161705 }
161706 return pNew==0;
161707}
161708#endif
161709
161710/* Datatype of the argument to the memory allocated passed as the
161711** second argument to sqlite3ParserAlloc() below. This can be changed by
161712** putting an appropriate #define in the %include section of the input
161713** grammar.
161714*/
161715#ifndef YYMALLOCARGTYPE
161716# define YYMALLOCARGTYPE size_t
161717#endif
161718
161719/* Initialize a new parser that has already been allocated.
161720*/
161721SQLITE_PRIVATE void sqlite3ParserInit(void *yypRawParser sqlite3ParserCTX_PDECL){
161722 yyParser *yypParser = (yyParser*)yypRawParser;
161723 sqlite3ParserCTX_STORE
161724#ifdef YYTRACKMAXSTACKDEPTH
161725 yypParser->yyhwm = 0;
161726#endif
161727#if YYSTACKDEPTH<=0
161728 yypParser->yytos = NULL;
161729 yypParser->yystack = NULL;
161730 yypParser->yystksz = 0;
161731 if( yyGrowStack(yypParser) ){
161732 yypParser->yystack = &yypParser->yystk0;
161733 yypParser->yystksz = 1;
161734 }
161735#endif
161736#ifndef YYNOERRORRECOVERY
161737 yypParser->yyerrcnt = -1;
161738#endif
161739 yypParser->yytos = yypParser->yystack;
161740 yypParser->yystack[0].stateno = 0;
161741 yypParser->yystack[0].major = 0;
161742#if YYSTACKDEPTH>0
161743 yypParser->yystackEnd = &yypParser->yystack[YYSTACKDEPTH-1];
161744#endif
161745}
161746
161747#ifndef sqlite3Parser_ENGINEALWAYSONSTACK
161748/*
161749** This function allocates a new parser.
161750** The only argument is a pointer to a function which works like
161751** malloc.
161752**
161753** Inputs:
161754** A pointer to the function used to allocate memory.
161755**
161756** Outputs:
161757** A pointer to a parser. This pointer is used in subsequent calls
161758** to sqlite3Parser and sqlite3ParserFree.
161759*/
161760SQLITE_PRIVATE void *sqlite3ParserAlloc(void *(*mallocProc)(YYMALLOCARGTYPE) sqlite3ParserCTX_PDECL){
161761 yyParser *yypParser;
161762 yypParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPE)sizeof(yyParser) );
161763 if( yypParser ){
161764 sqlite3ParserCTX_STORE
161765 sqlite3ParserInit(yypParser sqlite3ParserCTX_PARAM);
161766 }
161767 return (void*)yypParser;
161768}
161769#endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
161770
161771
161772/* The following function deletes the "minor type" or semantic value
161773** associated with a symbol. The symbol can be either a terminal
161774** or nonterminal. "yymajor" is the symbol code, and "yypminor" is
161775** a pointer to the value to be deleted. The code used to do the
161776** deletions is derived from the %destructor and/or %token_destructor
161777** directives of the input grammar.
161778*/
161779static void yy_destructor(
161780 yyParser *yypParser, /* The parser */
161781 YYCODETYPE yymajor, /* Type code for object to destroy */
161782 YYMINORTYPE *yypminor /* The object to be destroyed */
161783){
161784 sqlite3ParserARG_FETCH
161785 sqlite3ParserCTX_FETCH
161786 switch( yymajor ){
161787 /* Here is inserted the actions which take place when a
161788 ** terminal or non-terminal is destroyed. This can happen
161789 ** when the symbol is popped from the stack during a
161790 ** reduce or during error processing or when a parser is
161791 ** being destroyed before it is finished parsing.
161792 **
161793 ** Note: during a reduce, the only symbols destroyed are those
161794 ** which appear on the RHS of the rule, but which are *not* used
161795 ** inside the C code.
161796 */
161797/********* Begin destructor definitions ***************************************/
161798 case 203: /* select */
161799 case 238: /* selectnowith */
161800 case 239: /* oneselect */
161801 case 251: /* values */
161802{
161803sqlite3SelectDelete(db: pParse->db, p: (yypminor->yy303));
161804}
161805 break;
161806 case 215: /* term */
161807 case 216: /* expr */
161808 case 245: /* where_opt */
161809 case 247: /* having_opt */
161810 case 259: /* on_opt */
161811 case 266: /* where_opt_ret */
161812 case 277: /* case_operand */
161813 case 279: /* case_else */
161814 case 282: /* vinto */
161815 case 289: /* when_clause */
161816 case 294: /* key_opt */
161817 case 310: /* filter_clause */
161818{
161819sqlite3ExprDelete(db: pParse->db, p: (yypminor->yy626));
161820}
161821 break;
161822 case 220: /* eidlist_opt */
161823 case 230: /* sortlist */
161824 case 231: /* eidlist */
161825 case 243: /* selcollist */
161826 case 246: /* groupby_opt */
161827 case 248: /* orderby_opt */
161828 case 252: /* nexprlist */
161829 case 253: /* sclp */
161830 case 261: /* exprlist */
161831 case 267: /* setlist */
161832 case 276: /* paren_exprlist */
161833 case 278: /* case_exprlist */
161834 case 309: /* part_opt */
161835{
161836sqlite3ExprListDelete(db: pParse->db, pList: (yypminor->yy562));
161837}
161838 break;
161839 case 237: /* fullname */
161840 case 244: /* from */
161841 case 255: /* seltablist */
161842 case 256: /* stl_prefix */
161843 case 262: /* xfullname */
161844{
161845sqlite3SrcListDelete(db: pParse->db, pList: (yypminor->yy607));
161846}
161847 break;
161848 case 240: /* wqlist */
161849{
161850sqlite3WithDelete(db: pParse->db, pWith: (yypminor->yy43));
161851}
161852 break;
161853 case 250: /* window_clause */
161854 case 305: /* windowdefn_list */
161855{
161856sqlite3WindowListDelete(db: pParse->db, p: (yypminor->yy375));
161857}
161858 break;
161859 case 260: /* using_opt */
161860 case 263: /* idlist */
161861 case 269: /* idlist_opt */
161862{
161863sqlite3IdListDelete(db: pParse->db, pList: (yypminor->yy240));
161864}
161865 break;
161866 case 272: /* filter_over */
161867 case 306: /* windowdefn */
161868 case 307: /* window */
161869 case 308: /* frame_opt */
161870 case 311: /* over_clause */
161871{
161872sqlite3WindowDelete(db: pParse->db, p: (yypminor->yy375));
161873}
161874 break;
161875 case 285: /* trigger_cmd_list */
161876 case 290: /* trigger_cmd */
161877{
161878sqlite3DeleteTriggerStep(db: pParse->db, pTriggerStep: (yypminor->yy95));
161879}
161880 break;
161881 case 287: /* trigger_event */
161882{
161883sqlite3IdListDelete(db: pParse->db, pList: (yypminor->yy570).b);
161884}
161885 break;
161886 case 313: /* frame_bound */
161887 case 314: /* frame_bound_s */
161888 case 315: /* frame_bound_e */
161889{
161890sqlite3ExprDelete(db: pParse->db, p: (yypminor->yy81).pExpr);
161891}
161892 break;
161893/********* End destructor definitions *****************************************/
161894 default: break; /* If no destructor action specified: do nothing */
161895 }
161896}
161897
161898/*
161899** Pop the parser's stack once.
161900**
161901** If there is a destructor routine associated with the token which
161902** is popped from the stack, then call it.
161903*/
161904static void yy_pop_parser_stack(yyParser *pParser){
161905 yyStackEntry *yytos;
161906 assert( pParser->yytos!=0 );
161907 assert( pParser->yytos > pParser->yystack );
161908 yytos = pParser->yytos--;
161909#ifndef NDEBUG
161910 if( yyTraceFILE ){
161911 fprintf(yyTraceFILE,"%sPopping %s\n",
161912 yyTracePrompt,
161913 yyTokenName[yytos->major]);
161914 }
161915#endif
161916 yy_destructor(yypParser: pParser, yymajor: yytos->major, yypminor: &yytos->minor);
161917}
161918
161919/*
161920** Clear all secondary memory allocations from the parser
161921*/
161922SQLITE_PRIVATE void sqlite3ParserFinalize(void *p){
161923 yyParser *pParser = (yyParser*)p;
161924 while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
161925#if YYSTACKDEPTH<=0
161926 if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
161927#endif
161928}
161929
161930#ifndef sqlite3Parser_ENGINEALWAYSONSTACK
161931/*
161932** Deallocate and destroy a parser. Destructors are called for
161933** all stack elements before shutting the parser down.
161934**
161935** If the YYPARSEFREENEVERNULL macro exists (for example because it
161936** is defined in a %include section of the input grammar) then it is
161937** assumed that the input pointer is never NULL.
161938*/
161939SQLITE_PRIVATE void sqlite3ParserFree(
161940 void *p, /* The parser to be deleted */
161941 void (*freeProc)(void*) /* Function used to reclaim memory */
161942){
161943#ifndef YYPARSEFREENEVERNULL
161944 if( p==0 ) return;
161945#endif
161946 sqlite3ParserFinalize(p);
161947 (*freeProc)(p);
161948}
161949#endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
161950
161951/*
161952** Return the peak depth of the stack for a parser.
161953*/
161954#ifdef YYTRACKMAXSTACKDEPTH
161955SQLITE_PRIVATE int sqlite3ParserStackPeak(void *p){
161956 yyParser *pParser = (yyParser*)p;
161957 return pParser->yyhwm;
161958}
161959#endif
161960
161961/* This array of booleans keeps track of the parser statement
161962** coverage. The element yycoverage[X][Y] is set when the parser
161963** is in state X and has a lookahead token Y. In a well-tested
161964** systems, every element of this matrix should end up being set.
161965*/
161966#if defined(YYCOVERAGE)
161967static unsigned char yycoverage[YYNSTATE][YYNTOKEN];
161968#endif
161969
161970/*
161971** Write into out a description of every state/lookahead combination that
161972**
161973** (1) has not been used by the parser, and
161974** (2) is not a syntax error.
161975**
161976** Return the number of missed state/lookahead combinations.
161977*/
161978#if defined(YYCOVERAGE)
161979SQLITE_PRIVATE int sqlite3ParserCoverage(FILE *out){
161980 int stateno, iLookAhead, i;
161981 int nMissed = 0;
161982 for(stateno=0; stateno<YYNSTATE; stateno++){
161983 i = yy_shift_ofst[stateno];
161984 for(iLookAhead=0; iLookAhead<YYNTOKEN; iLookAhead++){
161985 if( yy_lookahead[i+iLookAhead]!=iLookAhead ) continue;
161986 if( yycoverage[stateno][iLookAhead]==0 ) nMissed++;
161987 if( out ){
161988 fprintf(out,"State %d lookahead %s %s\n", stateno,
161989 yyTokenName[iLookAhead],
161990 yycoverage[stateno][iLookAhead] ? "ok" : "missed");
161991 }
161992 }
161993 }
161994 return nMissed;
161995}
161996#endif
161997
161998/*
161999** Find the appropriate action for a parser given the terminal
162000** look-ahead token iLookAhead.
162001*/
162002static YYACTIONTYPE yy_find_shift_action(
162003 YYCODETYPE iLookAhead, /* The look-ahead token */
162004 YYACTIONTYPE stateno /* Current state number */
162005){
162006 int i;
162007
162008 if( stateno>YY_MAX_SHIFT ) return stateno;
162009 assert( stateno <= YY_SHIFT_COUNT );
162010#if defined(YYCOVERAGE)
162011 yycoverage[stateno][iLookAhead] = 1;
162012#endif
162013 do{
162014 i = yy_shift_ofst[stateno];
162015 assert( i>=0 );
162016 assert( i<=YY_ACTTAB_COUNT );
162017 assert( i+YYNTOKEN<=(int)YY_NLOOKAHEAD );
162018 assert( iLookAhead!=YYNOCODE );
162019 assert( iLookAhead < YYNTOKEN );
162020 i += iLookAhead;
162021 assert( i<(int)YY_NLOOKAHEAD );
162022 if( yy_lookahead[i]!=iLookAhead ){
162023#ifdef YYFALLBACK
162024 YYCODETYPE iFallback; /* Fallback token */
162025 assert( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0]) );
162026 iFallback = yyFallback[iLookAhead];
162027 if( iFallback!=0 ){
162028#ifndef NDEBUG
162029 if( yyTraceFILE ){
162030 fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
162031 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
162032 }
162033#endif
162034 assert( yyFallback[iFallback]==0 ); /* Fallback loop must terminate */
162035 iLookAhead = iFallback;
162036 continue;
162037 }
162038#endif
162039#ifdef YYWILDCARD
162040 {
162041 int j = i - iLookAhead + YYWILDCARD;
162042 assert( j<(int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])) );
162043 if( yy_lookahead[j]==YYWILDCARD && iLookAhead>0 ){
162044#ifndef NDEBUG
162045 if( yyTraceFILE ){
162046 fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
162047 yyTracePrompt, yyTokenName[iLookAhead],
162048 yyTokenName[YYWILDCARD]);
162049 }
162050#endif /* NDEBUG */
162051 return yy_action[j];
162052 }
162053 }
162054#endif /* YYWILDCARD */
162055 return yy_default[stateno];
162056 }else{
162057 assert( i>=0 && i<(int)(sizeof(yy_action)/sizeof(yy_action[0])) );
162058 return yy_action[i];
162059 }
162060 }while(1);
162061}
162062
162063/*
162064** Find the appropriate action for a parser given the non-terminal
162065** look-ahead token iLookAhead.
162066*/
162067static YYACTIONTYPE yy_find_reduce_action(
162068 YYACTIONTYPE stateno, /* Current state number */
162069 YYCODETYPE iLookAhead /* The look-ahead token */
162070){
162071 int i;
162072#ifdef YYERRORSYMBOL
162073 if( stateno>YY_REDUCE_COUNT ){
162074 return yy_default[stateno];
162075 }
162076#else
162077 assert( stateno<=YY_REDUCE_COUNT );
162078#endif
162079 i = yy_reduce_ofst[stateno];
162080 assert( iLookAhead!=YYNOCODE );
162081 i += iLookAhead;
162082#ifdef YYERRORSYMBOL
162083 if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
162084 return yy_default[stateno];
162085 }
162086#else
162087 assert( i>=0 && i<YY_ACTTAB_COUNT );
162088 assert( yy_lookahead[i]==iLookAhead );
162089#endif
162090 return yy_action[i];
162091}
162092
162093/*
162094** The following routine is called if the stack overflows.
162095*/
162096static void yyStackOverflow(yyParser *yypParser){
162097 sqlite3ParserARG_FETCH
162098 sqlite3ParserCTX_FETCH
162099#ifndef NDEBUG
162100 if( yyTraceFILE ){
162101 fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
162102 }
162103#endif
162104 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(pParser: yypParser);
162105 /* Here code is inserted which will execute if the parser
162106 ** stack every overflows */
162107/******** Begin %stack_overflow code ******************************************/
162108
162109 sqlite3ErrorMsg(pParse, zFormat: "parser stack overflow");
162110/******** End %stack_overflow code ********************************************/
162111 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument var */
162112 sqlite3ParserCTX_STORE
162113}
162114
162115/*
162116** Print tracing information for a SHIFT action
162117*/
162118#ifndef NDEBUG
162119static void yyTraceShift(yyParser *yypParser, int yyNewState, const char *zTag){
162120 if( yyTraceFILE ){
162121 if( yyNewState<YYNSTATE ){
162122 fprintf(yyTraceFILE,"%s%s '%s', go to state %d\n",
162123 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
162124 yyNewState);
162125 }else{
162126 fprintf(yyTraceFILE,"%s%s '%s', pending reduce %d\n",
162127 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
162128 yyNewState - YY_MIN_REDUCE);
162129 }
162130 }
162131}
162132#else
162133# define yyTraceShift(X,Y,Z)
162134#endif
162135
162136/*
162137** Perform a shift action.
162138*/
162139static void yy_shift(
162140 yyParser *yypParser, /* The parser to be shifted */
162141 YYACTIONTYPE yyNewState, /* The new state to shift in */
162142 YYCODETYPE yyMajor, /* The major token to shift in */
162143 sqlite3ParserTOKENTYPE yyMinor /* The minor token to shift in */
162144){
162145 yyStackEntry *yytos;
162146 yypParser->yytos++;
162147#ifdef YYTRACKMAXSTACKDEPTH
162148 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
162149 yypParser->yyhwm++;
162150 assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack) );
162151 }
162152#endif
162153#if YYSTACKDEPTH>0
162154 if( yypParser->yytos>yypParser->yystackEnd ){
162155 yypParser->yytos--;
162156 yyStackOverflow(yypParser);
162157 return;
162158 }
162159#else
162160 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz] ){
162161 if( yyGrowStack(yypParser) ){
162162 yypParser->yytos--;
162163 yyStackOverflow(yypParser);
162164 return;
162165 }
162166 }
162167#endif
162168 if( yyNewState > YY_MAX_SHIFT ){
162169 yyNewState += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE;
162170 }
162171 yytos = yypParser->yytos;
162172 yytos->stateno = yyNewState;
162173 yytos->major = yyMajor;
162174 yytos->minor.yy0 = yyMinor;
162175 yyTraceShift(yypParser, yyNewState, "Shift");
162176}
162177
162178/* For rule J, yyRuleInfoLhs[J] contains the symbol on the left-hand side
162179** of that rule */
162180static const YYCODETYPE yyRuleInfoLhs[] = {
162181 188, /* (0) explain ::= EXPLAIN */
162182 188, /* (1) explain ::= EXPLAIN QUERY PLAN */
162183 187, /* (2) cmdx ::= cmd */
162184 189, /* (3) cmd ::= BEGIN transtype trans_opt */
162185 190, /* (4) transtype ::= */
162186 190, /* (5) transtype ::= DEFERRED */
162187 190, /* (6) transtype ::= IMMEDIATE */
162188 190, /* (7) transtype ::= EXCLUSIVE */
162189 189, /* (8) cmd ::= COMMIT|END trans_opt */
162190 189, /* (9) cmd ::= ROLLBACK trans_opt */
162191 189, /* (10) cmd ::= SAVEPOINT nm */
162192 189, /* (11) cmd ::= RELEASE savepoint_opt nm */
162193 189, /* (12) cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
162194 194, /* (13) create_table ::= createkw temp TABLE ifnotexists nm dbnm */
162195 196, /* (14) createkw ::= CREATE */
162196 198, /* (15) ifnotexists ::= */
162197 198, /* (16) ifnotexists ::= IF NOT EXISTS */
162198 197, /* (17) temp ::= TEMP */
162199 197, /* (18) temp ::= */
162200 195, /* (19) create_table_args ::= LP columnlist conslist_opt RP table_option_set */
162201 195, /* (20) create_table_args ::= AS select */
162202 202, /* (21) table_option_set ::= */
162203 202, /* (22) table_option_set ::= table_option_set COMMA table_option */
162204 204, /* (23) table_option ::= WITHOUT nm */
162205 204, /* (24) table_option ::= nm */
162206 205, /* (25) columnname ::= nm typetoken */
162207 207, /* (26) typetoken ::= */
162208 207, /* (27) typetoken ::= typename LP signed RP */
162209 207, /* (28) typetoken ::= typename LP signed COMMA signed RP */
162210 208, /* (29) typename ::= typename ID|STRING */
162211 212, /* (30) scanpt ::= */
162212 213, /* (31) scantok ::= */
162213 214, /* (32) ccons ::= CONSTRAINT nm */
162214 214, /* (33) ccons ::= DEFAULT scantok term */
162215 214, /* (34) ccons ::= DEFAULT LP expr RP */
162216 214, /* (35) ccons ::= DEFAULT PLUS scantok term */
162217 214, /* (36) ccons ::= DEFAULT MINUS scantok term */
162218 214, /* (37) ccons ::= DEFAULT scantok ID|INDEXED */
162219 214, /* (38) ccons ::= NOT NULL onconf */
162220 214, /* (39) ccons ::= PRIMARY KEY sortorder onconf autoinc */
162221 214, /* (40) ccons ::= UNIQUE onconf */
162222 214, /* (41) ccons ::= CHECK LP expr RP */
162223 214, /* (42) ccons ::= REFERENCES nm eidlist_opt refargs */
162224 214, /* (43) ccons ::= defer_subclause */
162225 214, /* (44) ccons ::= COLLATE ID|STRING */
162226 223, /* (45) generated ::= LP expr RP */
162227 223, /* (46) generated ::= LP expr RP ID */
162228 219, /* (47) autoinc ::= */
162229 219, /* (48) autoinc ::= AUTOINCR */
162230 221, /* (49) refargs ::= */
162231 221, /* (50) refargs ::= refargs refarg */
162232 224, /* (51) refarg ::= MATCH nm */
162233 224, /* (52) refarg ::= ON INSERT refact */
162234 224, /* (53) refarg ::= ON DELETE refact */
162235 224, /* (54) refarg ::= ON UPDATE refact */
162236 225, /* (55) refact ::= SET NULL */
162237 225, /* (56) refact ::= SET DEFAULT */
162238 225, /* (57) refact ::= CASCADE */
162239 225, /* (58) refact ::= RESTRICT */
162240 225, /* (59) refact ::= NO ACTION */
162241 222, /* (60) defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
162242 222, /* (61) defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
162243 226, /* (62) init_deferred_pred_opt ::= */
162244 226, /* (63) init_deferred_pred_opt ::= INITIALLY DEFERRED */
162245 226, /* (64) init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
162246 201, /* (65) conslist_opt ::= */
162247 228, /* (66) tconscomma ::= COMMA */
162248 229, /* (67) tcons ::= CONSTRAINT nm */
162249 229, /* (68) tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
162250 229, /* (69) tcons ::= UNIQUE LP sortlist RP onconf */
162251 229, /* (70) tcons ::= CHECK LP expr RP onconf */
162252 229, /* (71) tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
162253 232, /* (72) defer_subclause_opt ::= */
162254 217, /* (73) onconf ::= */
162255 217, /* (74) onconf ::= ON CONFLICT resolvetype */
162256 233, /* (75) orconf ::= */
162257 233, /* (76) orconf ::= OR resolvetype */
162258 234, /* (77) resolvetype ::= IGNORE */
162259 234, /* (78) resolvetype ::= REPLACE */
162260 189, /* (79) cmd ::= DROP TABLE ifexists fullname */
162261 236, /* (80) ifexists ::= IF EXISTS */
162262 236, /* (81) ifexists ::= */
162263 189, /* (82) cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
162264 189, /* (83) cmd ::= DROP VIEW ifexists fullname */
162265 189, /* (84) cmd ::= select */
162266 203, /* (85) select ::= WITH wqlist selectnowith */
162267 203, /* (86) select ::= WITH RECURSIVE wqlist selectnowith */
162268 203, /* (87) select ::= selectnowith */
162269 238, /* (88) selectnowith ::= selectnowith multiselect_op oneselect */
162270 241, /* (89) multiselect_op ::= UNION */
162271 241, /* (90) multiselect_op ::= UNION ALL */
162272 241, /* (91) multiselect_op ::= EXCEPT|INTERSECT */
162273 239, /* (92) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
162274 239, /* (93) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */
162275 251, /* (94) values ::= VALUES LP nexprlist RP */
162276 251, /* (95) values ::= values COMMA LP nexprlist RP */
162277 242, /* (96) distinct ::= DISTINCT */
162278 242, /* (97) distinct ::= ALL */
162279 242, /* (98) distinct ::= */
162280 253, /* (99) sclp ::= */
162281 243, /* (100) selcollist ::= sclp scanpt expr scanpt as */
162282 243, /* (101) selcollist ::= sclp scanpt STAR */
162283 243, /* (102) selcollist ::= sclp scanpt nm DOT STAR */
162284 254, /* (103) as ::= AS nm */
162285 254, /* (104) as ::= */
162286 244, /* (105) from ::= */
162287 244, /* (106) from ::= FROM seltablist */
162288 256, /* (107) stl_prefix ::= seltablist joinop */
162289 256, /* (108) stl_prefix ::= */
162290 255, /* (109) seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
162291 255, /* (110) seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt */
162292 255, /* (111) seltablist ::= stl_prefix LP select RP as on_opt using_opt */
162293 255, /* (112) seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
162294 199, /* (113) dbnm ::= */
162295 199, /* (114) dbnm ::= DOT nm */
162296 237, /* (115) fullname ::= nm */
162297 237, /* (116) fullname ::= nm DOT nm */
162298 262, /* (117) xfullname ::= nm */
162299 262, /* (118) xfullname ::= nm DOT nm */
162300 262, /* (119) xfullname ::= nm DOT nm AS nm */
162301 262, /* (120) xfullname ::= nm AS nm */
162302 257, /* (121) joinop ::= COMMA|JOIN */
162303 257, /* (122) joinop ::= JOIN_KW JOIN */
162304 257, /* (123) joinop ::= JOIN_KW nm JOIN */
162305 257, /* (124) joinop ::= JOIN_KW nm nm JOIN */
162306 259, /* (125) on_opt ::= ON expr */
162307 259, /* (126) on_opt ::= */
162308 258, /* (127) indexed_opt ::= */
162309 258, /* (128) indexed_opt ::= INDEXED BY nm */
162310 258, /* (129) indexed_opt ::= NOT INDEXED */
162311 260, /* (130) using_opt ::= USING LP idlist RP */
162312 260, /* (131) using_opt ::= */
162313 248, /* (132) orderby_opt ::= */
162314 248, /* (133) orderby_opt ::= ORDER BY sortlist */
162315 230, /* (134) sortlist ::= sortlist COMMA expr sortorder nulls */
162316 230, /* (135) sortlist ::= expr sortorder nulls */
162317 218, /* (136) sortorder ::= ASC */
162318 218, /* (137) sortorder ::= DESC */
162319 218, /* (138) sortorder ::= */
162320 264, /* (139) nulls ::= NULLS FIRST */
162321 264, /* (140) nulls ::= NULLS LAST */
162322 264, /* (141) nulls ::= */
162323 246, /* (142) groupby_opt ::= */
162324 246, /* (143) groupby_opt ::= GROUP BY nexprlist */
162325 247, /* (144) having_opt ::= */
162326 247, /* (145) having_opt ::= HAVING expr */
162327 249, /* (146) limit_opt ::= */
162328 249, /* (147) limit_opt ::= LIMIT expr */
162329 249, /* (148) limit_opt ::= LIMIT expr OFFSET expr */
162330 249, /* (149) limit_opt ::= LIMIT expr COMMA expr */
162331 189, /* (150) cmd ::= with DELETE FROM xfullname indexed_opt where_opt_ret */
162332 245, /* (151) where_opt ::= */
162333 245, /* (152) where_opt ::= WHERE expr */
162334 266, /* (153) where_opt_ret ::= */
162335 266, /* (154) where_opt_ret ::= WHERE expr */
162336 266, /* (155) where_opt_ret ::= RETURNING selcollist */
162337 266, /* (156) where_opt_ret ::= WHERE expr RETURNING selcollist */
162338 189, /* (157) cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret */
162339 267, /* (158) setlist ::= setlist COMMA nm EQ expr */
162340 267, /* (159) setlist ::= setlist COMMA LP idlist RP EQ expr */
162341 267, /* (160) setlist ::= nm EQ expr */
162342 267, /* (161) setlist ::= LP idlist RP EQ expr */
162343 189, /* (162) cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
162344 189, /* (163) cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES returning */
162345 270, /* (164) upsert ::= */
162346 270, /* (165) upsert ::= RETURNING selcollist */
162347 270, /* (166) upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt upsert */
162348 270, /* (167) upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING upsert */
162349 270, /* (168) upsert ::= ON CONFLICT DO NOTHING returning */
162350 270, /* (169) upsert ::= ON CONFLICT DO UPDATE SET setlist where_opt returning */
162351 271, /* (170) returning ::= RETURNING selcollist */
162352 268, /* (171) insert_cmd ::= INSERT orconf */
162353 268, /* (172) insert_cmd ::= REPLACE */
162354 269, /* (173) idlist_opt ::= */
162355 269, /* (174) idlist_opt ::= LP idlist RP */
162356 263, /* (175) idlist ::= idlist COMMA nm */
162357 263, /* (176) idlist ::= nm */
162358 216, /* (177) expr ::= LP expr RP */
162359 216, /* (178) expr ::= ID|INDEXED */
162360 216, /* (179) expr ::= JOIN_KW */
162361 216, /* (180) expr ::= nm DOT nm */
162362 216, /* (181) expr ::= nm DOT nm DOT nm */
162363 215, /* (182) term ::= NULL|FLOAT|BLOB */
162364 215, /* (183) term ::= STRING */
162365 215, /* (184) term ::= INTEGER */
162366 216, /* (185) expr ::= VARIABLE */
162367 216, /* (186) expr ::= expr COLLATE ID|STRING */
162368 216, /* (187) expr ::= CAST LP expr AS typetoken RP */
162369 216, /* (188) expr ::= ID|INDEXED LP distinct exprlist RP */
162370 216, /* (189) expr ::= ID|INDEXED LP STAR RP */
162371 216, /* (190) expr ::= ID|INDEXED LP distinct exprlist RP filter_over */
162372 216, /* (191) expr ::= ID|INDEXED LP STAR RP filter_over */
162373 215, /* (192) term ::= CTIME_KW */
162374 216, /* (193) expr ::= LP nexprlist COMMA expr RP */
162375 216, /* (194) expr ::= expr AND expr */
162376 216, /* (195) expr ::= expr OR expr */
162377 216, /* (196) expr ::= expr LT|GT|GE|LE expr */
162378 216, /* (197) expr ::= expr EQ|NE expr */
162379 216, /* (198) expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */
162380 216, /* (199) expr ::= expr PLUS|MINUS expr */
162381 216, /* (200) expr ::= expr STAR|SLASH|REM expr */
162382 216, /* (201) expr ::= expr CONCAT expr */
162383 273, /* (202) likeop ::= NOT LIKE_KW|MATCH */
162384 216, /* (203) expr ::= expr likeop expr */
162385 216, /* (204) expr ::= expr likeop expr ESCAPE expr */
162386 216, /* (205) expr ::= expr ISNULL|NOTNULL */
162387 216, /* (206) expr ::= expr NOT NULL */
162388 216, /* (207) expr ::= expr IS expr */
162389 216, /* (208) expr ::= expr IS NOT expr */
162390 216, /* (209) expr ::= NOT expr */
162391 216, /* (210) expr ::= BITNOT expr */
162392 216, /* (211) expr ::= PLUS|MINUS expr */
162393 274, /* (212) between_op ::= BETWEEN */
162394 274, /* (213) between_op ::= NOT BETWEEN */
162395 216, /* (214) expr ::= expr between_op expr AND expr */
162396 275, /* (215) in_op ::= IN */
162397 275, /* (216) in_op ::= NOT IN */
162398 216, /* (217) expr ::= expr in_op LP exprlist RP */
162399 216, /* (218) expr ::= LP select RP */
162400 216, /* (219) expr ::= expr in_op LP select RP */
162401 216, /* (220) expr ::= expr in_op nm dbnm paren_exprlist */
162402 216, /* (221) expr ::= EXISTS LP select RP */
162403 216, /* (222) expr ::= CASE case_operand case_exprlist case_else END */
162404 278, /* (223) case_exprlist ::= case_exprlist WHEN expr THEN expr */
162405 278, /* (224) case_exprlist ::= WHEN expr THEN expr */
162406 279, /* (225) case_else ::= ELSE expr */
162407 279, /* (226) case_else ::= */
162408 277, /* (227) case_operand ::= expr */
162409 277, /* (228) case_operand ::= */
162410 261, /* (229) exprlist ::= */
162411 252, /* (230) nexprlist ::= nexprlist COMMA expr */
162412 252, /* (231) nexprlist ::= expr */
162413 276, /* (232) paren_exprlist ::= */
162414 276, /* (233) paren_exprlist ::= LP exprlist RP */
162415 189, /* (234) cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
162416 280, /* (235) uniqueflag ::= UNIQUE */
162417 280, /* (236) uniqueflag ::= */
162418 220, /* (237) eidlist_opt ::= */
162419 220, /* (238) eidlist_opt ::= LP eidlist RP */
162420 231, /* (239) eidlist ::= eidlist COMMA nm collate sortorder */
162421 231, /* (240) eidlist ::= nm collate sortorder */
162422 281, /* (241) collate ::= */
162423 281, /* (242) collate ::= COLLATE ID|STRING */
162424 189, /* (243) cmd ::= DROP INDEX ifexists fullname */
162425 189, /* (244) cmd ::= VACUUM vinto */
162426 189, /* (245) cmd ::= VACUUM nm vinto */
162427 282, /* (246) vinto ::= INTO expr */
162428 282, /* (247) vinto ::= */
162429 189, /* (248) cmd ::= PRAGMA nm dbnm */
162430 189, /* (249) cmd ::= PRAGMA nm dbnm EQ nmnum */
162431 189, /* (250) cmd ::= PRAGMA nm dbnm LP nmnum RP */
162432 189, /* (251) cmd ::= PRAGMA nm dbnm EQ minus_num */
162433 189, /* (252) cmd ::= PRAGMA nm dbnm LP minus_num RP */
162434 210, /* (253) plus_num ::= PLUS INTEGER|FLOAT */
162435 211, /* (254) minus_num ::= MINUS INTEGER|FLOAT */
162436 189, /* (255) cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
162437 284, /* (256) trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
162438 286, /* (257) trigger_time ::= BEFORE|AFTER */
162439 286, /* (258) trigger_time ::= INSTEAD OF */
162440 286, /* (259) trigger_time ::= */
162441 287, /* (260) trigger_event ::= DELETE|INSERT */
162442 287, /* (261) trigger_event ::= UPDATE */
162443 287, /* (262) trigger_event ::= UPDATE OF idlist */
162444 289, /* (263) when_clause ::= */
162445 289, /* (264) when_clause ::= WHEN expr */
162446 285, /* (265) trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
162447 285, /* (266) trigger_cmd_list ::= trigger_cmd SEMI */
162448 291, /* (267) trnm ::= nm DOT nm */
162449 292, /* (268) tridxby ::= INDEXED BY nm */
162450 292, /* (269) tridxby ::= NOT INDEXED */
162451 290, /* (270) trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist from where_opt scanpt */
162452 290, /* (271) trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
162453 290, /* (272) trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
162454 290, /* (273) trigger_cmd ::= scanpt select scanpt */
162455 216, /* (274) expr ::= RAISE LP IGNORE RP */
162456 216, /* (275) expr ::= RAISE LP raisetype COMMA nm RP */
162457 235, /* (276) raisetype ::= ROLLBACK */
162458 235, /* (277) raisetype ::= ABORT */
162459 235, /* (278) raisetype ::= FAIL */
162460 189, /* (279) cmd ::= DROP TRIGGER ifexists fullname */
162461 189, /* (280) cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
162462 189, /* (281) cmd ::= DETACH database_kw_opt expr */
162463 294, /* (282) key_opt ::= */
162464 294, /* (283) key_opt ::= KEY expr */
162465 189, /* (284) cmd ::= REINDEX */
162466 189, /* (285) cmd ::= REINDEX nm dbnm */
162467 189, /* (286) cmd ::= ANALYZE */
162468 189, /* (287) cmd ::= ANALYZE nm dbnm */
162469 189, /* (288) cmd ::= ALTER TABLE fullname RENAME TO nm */
162470 189, /* (289) cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
162471 189, /* (290) cmd ::= ALTER TABLE fullname DROP kwcolumn_opt nm */
162472 295, /* (291) add_column_fullname ::= fullname */
162473 189, /* (292) cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
162474 189, /* (293) cmd ::= create_vtab */
162475 189, /* (294) cmd ::= create_vtab LP vtabarglist RP */
162476 297, /* (295) create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
162477 299, /* (296) vtabarg ::= */
162478 300, /* (297) vtabargtoken ::= ANY */
162479 300, /* (298) vtabargtoken ::= lp anylist RP */
162480 301, /* (299) lp ::= LP */
162481 265, /* (300) with ::= WITH wqlist */
162482 265, /* (301) with ::= WITH RECURSIVE wqlist */
162483 304, /* (302) wqas ::= AS */
162484 304, /* (303) wqas ::= AS MATERIALIZED */
162485 304, /* (304) wqas ::= AS NOT MATERIALIZED */
162486 303, /* (305) wqitem ::= nm eidlist_opt wqas LP select RP */
162487 240, /* (306) wqlist ::= wqitem */
162488 240, /* (307) wqlist ::= wqlist COMMA wqitem */
162489 305, /* (308) windowdefn_list ::= windowdefn */
162490 305, /* (309) windowdefn_list ::= windowdefn_list COMMA windowdefn */
162491 306, /* (310) windowdefn ::= nm AS LP window RP */
162492 307, /* (311) window ::= PARTITION BY nexprlist orderby_opt frame_opt */
162493 307, /* (312) window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
162494 307, /* (313) window ::= ORDER BY sortlist frame_opt */
162495 307, /* (314) window ::= nm ORDER BY sortlist frame_opt */
162496 307, /* (315) window ::= frame_opt */
162497 307, /* (316) window ::= nm frame_opt */
162498 308, /* (317) frame_opt ::= */
162499 308, /* (318) frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
162500 308, /* (319) frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt */
162501 312, /* (320) range_or_rows ::= RANGE|ROWS|GROUPS */
162502 314, /* (321) frame_bound_s ::= frame_bound */
162503 314, /* (322) frame_bound_s ::= UNBOUNDED PRECEDING */
162504 315, /* (323) frame_bound_e ::= frame_bound */
162505 315, /* (324) frame_bound_e ::= UNBOUNDED FOLLOWING */
162506 313, /* (325) frame_bound ::= expr PRECEDING|FOLLOWING */
162507 313, /* (326) frame_bound ::= CURRENT ROW */
162508 316, /* (327) frame_exclude_opt ::= */
162509 316, /* (328) frame_exclude_opt ::= EXCLUDE frame_exclude */
162510 317, /* (329) frame_exclude ::= NO OTHERS */
162511 317, /* (330) frame_exclude ::= CURRENT ROW */
162512 317, /* (331) frame_exclude ::= GROUP|TIES */
162513 250, /* (332) window_clause ::= WINDOW windowdefn_list */
162514 272, /* (333) filter_over ::= filter_clause over_clause */
162515 272, /* (334) filter_over ::= over_clause */
162516 272, /* (335) filter_over ::= filter_clause */
162517 311, /* (336) over_clause ::= OVER LP window RP */
162518 311, /* (337) over_clause ::= OVER nm */
162519 310, /* (338) filter_clause ::= FILTER LP WHERE expr RP */
162520 184, /* (339) input ::= cmdlist */
162521 185, /* (340) cmdlist ::= cmdlist ecmd */
162522 185, /* (341) cmdlist ::= ecmd */
162523 186, /* (342) ecmd ::= SEMI */
162524 186, /* (343) ecmd ::= cmdx SEMI */
162525 186, /* (344) ecmd ::= explain cmdx SEMI */
162526 191, /* (345) trans_opt ::= */
162527 191, /* (346) trans_opt ::= TRANSACTION */
162528 191, /* (347) trans_opt ::= TRANSACTION nm */
162529 193, /* (348) savepoint_opt ::= SAVEPOINT */
162530 193, /* (349) savepoint_opt ::= */
162531 189, /* (350) cmd ::= create_table create_table_args */
162532 202, /* (351) table_option_set ::= table_option */
162533 200, /* (352) columnlist ::= columnlist COMMA columnname carglist */
162534 200, /* (353) columnlist ::= columnname carglist */
162535 192, /* (354) nm ::= ID|INDEXED */
162536 192, /* (355) nm ::= STRING */
162537 192, /* (356) nm ::= JOIN_KW */
162538 207, /* (357) typetoken ::= typename */
162539 208, /* (358) typename ::= ID|STRING */
162540 209, /* (359) signed ::= plus_num */
162541 209, /* (360) signed ::= minus_num */
162542 206, /* (361) carglist ::= carglist ccons */
162543 206, /* (362) carglist ::= */
162544 214, /* (363) ccons ::= NULL onconf */
162545 214, /* (364) ccons ::= GENERATED ALWAYS AS generated */
162546 214, /* (365) ccons ::= AS generated */
162547 201, /* (366) conslist_opt ::= COMMA conslist */
162548 227, /* (367) conslist ::= conslist tconscomma tcons */
162549 227, /* (368) conslist ::= tcons */
162550 228, /* (369) tconscomma ::= */
162551 232, /* (370) defer_subclause_opt ::= defer_subclause */
162552 234, /* (371) resolvetype ::= raisetype */
162553 238, /* (372) selectnowith ::= oneselect */
162554 239, /* (373) oneselect ::= values */
162555 253, /* (374) sclp ::= selcollist COMMA */
162556 254, /* (375) as ::= ID|STRING */
162557 271, /* (376) returning ::= */
162558 216, /* (377) expr ::= term */
162559 273, /* (378) likeop ::= LIKE_KW|MATCH */
162560 261, /* (379) exprlist ::= nexprlist */
162561 283, /* (380) nmnum ::= plus_num */
162562 283, /* (381) nmnum ::= nm */
162563 283, /* (382) nmnum ::= ON */
162564 283, /* (383) nmnum ::= DELETE */
162565 283, /* (384) nmnum ::= DEFAULT */
162566 210, /* (385) plus_num ::= INTEGER|FLOAT */
162567 288, /* (386) foreach_clause ::= */
162568 288, /* (387) foreach_clause ::= FOR EACH ROW */
162569 291, /* (388) trnm ::= nm */
162570 292, /* (389) tridxby ::= */
162571 293, /* (390) database_kw_opt ::= DATABASE */
162572 293, /* (391) database_kw_opt ::= */
162573 296, /* (392) kwcolumn_opt ::= */
162574 296, /* (393) kwcolumn_opt ::= COLUMNKW */
162575 298, /* (394) vtabarglist ::= vtabarg */
162576 298, /* (395) vtabarglist ::= vtabarglist COMMA vtabarg */
162577 299, /* (396) vtabarg ::= vtabarg vtabargtoken */
162578 302, /* (397) anylist ::= */
162579 302, /* (398) anylist ::= anylist LP anylist RP */
162580 302, /* (399) anylist ::= anylist ANY */
162581 265, /* (400) with ::= */
162582};
162583
162584/* For rule J, yyRuleInfoNRhs[J] contains the negative of the number
162585** of symbols on the right-hand side of that rule. */
162586static const signed char yyRuleInfoNRhs[] = {
162587 -1, /* (0) explain ::= EXPLAIN */
162588 -3, /* (1) explain ::= EXPLAIN QUERY PLAN */
162589 -1, /* (2) cmdx ::= cmd */
162590 -3, /* (3) cmd ::= BEGIN transtype trans_opt */
162591 0, /* (4) transtype ::= */
162592 -1, /* (5) transtype ::= DEFERRED */
162593 -1, /* (6) transtype ::= IMMEDIATE */
162594 -1, /* (7) transtype ::= EXCLUSIVE */
162595 -2, /* (8) cmd ::= COMMIT|END trans_opt */
162596 -2, /* (9) cmd ::= ROLLBACK trans_opt */
162597 -2, /* (10) cmd ::= SAVEPOINT nm */
162598 -3, /* (11) cmd ::= RELEASE savepoint_opt nm */
162599 -5, /* (12) cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
162600 -6, /* (13) create_table ::= createkw temp TABLE ifnotexists nm dbnm */
162601 -1, /* (14) createkw ::= CREATE */
162602 0, /* (15) ifnotexists ::= */
162603 -3, /* (16) ifnotexists ::= IF NOT EXISTS */
162604 -1, /* (17) temp ::= TEMP */
162605 0, /* (18) temp ::= */
162606 -5, /* (19) create_table_args ::= LP columnlist conslist_opt RP table_option_set */
162607 -2, /* (20) create_table_args ::= AS select */
162608 0, /* (21) table_option_set ::= */
162609 -3, /* (22) table_option_set ::= table_option_set COMMA table_option */
162610 -2, /* (23) table_option ::= WITHOUT nm */
162611 -1, /* (24) table_option ::= nm */
162612 -2, /* (25) columnname ::= nm typetoken */
162613 0, /* (26) typetoken ::= */
162614 -4, /* (27) typetoken ::= typename LP signed RP */
162615 -6, /* (28) typetoken ::= typename LP signed COMMA signed RP */
162616 -2, /* (29) typename ::= typename ID|STRING */
162617 0, /* (30) scanpt ::= */
162618 0, /* (31) scantok ::= */
162619 -2, /* (32) ccons ::= CONSTRAINT nm */
162620 -3, /* (33) ccons ::= DEFAULT scantok term */
162621 -4, /* (34) ccons ::= DEFAULT LP expr RP */
162622 -4, /* (35) ccons ::= DEFAULT PLUS scantok term */
162623 -4, /* (36) ccons ::= DEFAULT MINUS scantok term */
162624 -3, /* (37) ccons ::= DEFAULT scantok ID|INDEXED */
162625 -3, /* (38) ccons ::= NOT NULL onconf */
162626 -5, /* (39) ccons ::= PRIMARY KEY sortorder onconf autoinc */
162627 -2, /* (40) ccons ::= UNIQUE onconf */
162628 -4, /* (41) ccons ::= CHECK LP expr RP */
162629 -4, /* (42) ccons ::= REFERENCES nm eidlist_opt refargs */
162630 -1, /* (43) ccons ::= defer_subclause */
162631 -2, /* (44) ccons ::= COLLATE ID|STRING */
162632 -3, /* (45) generated ::= LP expr RP */
162633 -4, /* (46) generated ::= LP expr RP ID */
162634 0, /* (47) autoinc ::= */
162635 -1, /* (48) autoinc ::= AUTOINCR */
162636 0, /* (49) refargs ::= */
162637 -2, /* (50) refargs ::= refargs refarg */
162638 -2, /* (51) refarg ::= MATCH nm */
162639 -3, /* (52) refarg ::= ON INSERT refact */
162640 -3, /* (53) refarg ::= ON DELETE refact */
162641 -3, /* (54) refarg ::= ON UPDATE refact */
162642 -2, /* (55) refact ::= SET NULL */
162643 -2, /* (56) refact ::= SET DEFAULT */
162644 -1, /* (57) refact ::= CASCADE */
162645 -1, /* (58) refact ::= RESTRICT */
162646 -2, /* (59) refact ::= NO ACTION */
162647 -3, /* (60) defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
162648 -2, /* (61) defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
162649 0, /* (62) init_deferred_pred_opt ::= */
162650 -2, /* (63) init_deferred_pred_opt ::= INITIALLY DEFERRED */
162651 -2, /* (64) init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
162652 0, /* (65) conslist_opt ::= */
162653 -1, /* (66) tconscomma ::= COMMA */
162654 -2, /* (67) tcons ::= CONSTRAINT nm */
162655 -7, /* (68) tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
162656 -5, /* (69) tcons ::= UNIQUE LP sortlist RP onconf */
162657 -5, /* (70) tcons ::= CHECK LP expr RP onconf */
162658 -10, /* (71) tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
162659 0, /* (72) defer_subclause_opt ::= */
162660 0, /* (73) onconf ::= */
162661 -3, /* (74) onconf ::= ON CONFLICT resolvetype */
162662 0, /* (75) orconf ::= */
162663 -2, /* (76) orconf ::= OR resolvetype */
162664 -1, /* (77) resolvetype ::= IGNORE */
162665 -1, /* (78) resolvetype ::= REPLACE */
162666 -4, /* (79) cmd ::= DROP TABLE ifexists fullname */
162667 -2, /* (80) ifexists ::= IF EXISTS */
162668 0, /* (81) ifexists ::= */
162669 -9, /* (82) cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
162670 -4, /* (83) cmd ::= DROP VIEW ifexists fullname */
162671 -1, /* (84) cmd ::= select */
162672 -3, /* (85) select ::= WITH wqlist selectnowith */
162673 -4, /* (86) select ::= WITH RECURSIVE wqlist selectnowith */
162674 -1, /* (87) select ::= selectnowith */
162675 -3, /* (88) selectnowith ::= selectnowith multiselect_op oneselect */
162676 -1, /* (89) multiselect_op ::= UNION */
162677 -2, /* (90) multiselect_op ::= UNION ALL */
162678 -1, /* (91) multiselect_op ::= EXCEPT|INTERSECT */
162679 -9, /* (92) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
162680 -10, /* (93) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */
162681 -4, /* (94) values ::= VALUES LP nexprlist RP */
162682 -5, /* (95) values ::= values COMMA LP nexprlist RP */
162683 -1, /* (96) distinct ::= DISTINCT */
162684 -1, /* (97) distinct ::= ALL */
162685 0, /* (98) distinct ::= */
162686 0, /* (99) sclp ::= */
162687 -5, /* (100) selcollist ::= sclp scanpt expr scanpt as */
162688 -3, /* (101) selcollist ::= sclp scanpt STAR */
162689 -5, /* (102) selcollist ::= sclp scanpt nm DOT STAR */
162690 -2, /* (103) as ::= AS nm */
162691 0, /* (104) as ::= */
162692 0, /* (105) from ::= */
162693 -2, /* (106) from ::= FROM seltablist */
162694 -2, /* (107) stl_prefix ::= seltablist joinop */
162695 0, /* (108) stl_prefix ::= */
162696 -7, /* (109) seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
162697 -9, /* (110) seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt */
162698 -7, /* (111) seltablist ::= stl_prefix LP select RP as on_opt using_opt */
162699 -7, /* (112) seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
162700 0, /* (113) dbnm ::= */
162701 -2, /* (114) dbnm ::= DOT nm */
162702 -1, /* (115) fullname ::= nm */
162703 -3, /* (116) fullname ::= nm DOT nm */
162704 -1, /* (117) xfullname ::= nm */
162705 -3, /* (118) xfullname ::= nm DOT nm */
162706 -5, /* (119) xfullname ::= nm DOT nm AS nm */
162707 -3, /* (120) xfullname ::= nm AS nm */
162708 -1, /* (121) joinop ::= COMMA|JOIN */
162709 -2, /* (122) joinop ::= JOIN_KW JOIN */
162710 -3, /* (123) joinop ::= JOIN_KW nm JOIN */
162711 -4, /* (124) joinop ::= JOIN_KW nm nm JOIN */
162712 -2, /* (125) on_opt ::= ON expr */
162713 0, /* (126) on_opt ::= */
162714 0, /* (127) indexed_opt ::= */
162715 -3, /* (128) indexed_opt ::= INDEXED BY nm */
162716 -2, /* (129) indexed_opt ::= NOT INDEXED */
162717 -4, /* (130) using_opt ::= USING LP idlist RP */
162718 0, /* (131) using_opt ::= */
162719 0, /* (132) orderby_opt ::= */
162720 -3, /* (133) orderby_opt ::= ORDER BY sortlist */
162721 -5, /* (134) sortlist ::= sortlist COMMA expr sortorder nulls */
162722 -3, /* (135) sortlist ::= expr sortorder nulls */
162723 -1, /* (136) sortorder ::= ASC */
162724 -1, /* (137) sortorder ::= DESC */
162725 0, /* (138) sortorder ::= */
162726 -2, /* (139) nulls ::= NULLS FIRST */
162727 -2, /* (140) nulls ::= NULLS LAST */
162728 0, /* (141) nulls ::= */
162729 0, /* (142) groupby_opt ::= */
162730 -3, /* (143) groupby_opt ::= GROUP BY nexprlist */
162731 0, /* (144) having_opt ::= */
162732 -2, /* (145) having_opt ::= HAVING expr */
162733 0, /* (146) limit_opt ::= */
162734 -2, /* (147) limit_opt ::= LIMIT expr */
162735 -4, /* (148) limit_opt ::= LIMIT expr OFFSET expr */
162736 -4, /* (149) limit_opt ::= LIMIT expr COMMA expr */
162737 -6, /* (150) cmd ::= with DELETE FROM xfullname indexed_opt where_opt_ret */
162738 0, /* (151) where_opt ::= */
162739 -2, /* (152) where_opt ::= WHERE expr */
162740 0, /* (153) where_opt_ret ::= */
162741 -2, /* (154) where_opt_ret ::= WHERE expr */
162742 -2, /* (155) where_opt_ret ::= RETURNING selcollist */
162743 -4, /* (156) where_opt_ret ::= WHERE expr RETURNING selcollist */
162744 -9, /* (157) cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret */
162745 -5, /* (158) setlist ::= setlist COMMA nm EQ expr */
162746 -7, /* (159) setlist ::= setlist COMMA LP idlist RP EQ expr */
162747 -3, /* (160) setlist ::= nm EQ expr */
162748 -5, /* (161) setlist ::= LP idlist RP EQ expr */
162749 -7, /* (162) cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
162750 -8, /* (163) cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES returning */
162751 0, /* (164) upsert ::= */
162752 -2, /* (165) upsert ::= RETURNING selcollist */
162753 -12, /* (166) upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt upsert */
162754 -9, /* (167) upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING upsert */
162755 -5, /* (168) upsert ::= ON CONFLICT DO NOTHING returning */
162756 -8, /* (169) upsert ::= ON CONFLICT DO UPDATE SET setlist where_opt returning */
162757 -2, /* (170) returning ::= RETURNING selcollist */
162758 -2, /* (171) insert_cmd ::= INSERT orconf */
162759 -1, /* (172) insert_cmd ::= REPLACE */
162760 0, /* (173) idlist_opt ::= */
162761 -3, /* (174) idlist_opt ::= LP idlist RP */
162762 -3, /* (175) idlist ::= idlist COMMA nm */
162763 -1, /* (176) idlist ::= nm */
162764 -3, /* (177) expr ::= LP expr RP */
162765 -1, /* (178) expr ::= ID|INDEXED */
162766 -1, /* (179) expr ::= JOIN_KW */
162767 -3, /* (180) expr ::= nm DOT nm */
162768 -5, /* (181) expr ::= nm DOT nm DOT nm */
162769 -1, /* (182) term ::= NULL|FLOAT|BLOB */
162770 -1, /* (183) term ::= STRING */
162771 -1, /* (184) term ::= INTEGER */
162772 -1, /* (185) expr ::= VARIABLE */
162773 -3, /* (186) expr ::= expr COLLATE ID|STRING */
162774 -6, /* (187) expr ::= CAST LP expr AS typetoken RP */
162775 -5, /* (188) expr ::= ID|INDEXED LP distinct exprlist RP */
162776 -4, /* (189) expr ::= ID|INDEXED LP STAR RP */
162777 -6, /* (190) expr ::= ID|INDEXED LP distinct exprlist RP filter_over */
162778 -5, /* (191) expr ::= ID|INDEXED LP STAR RP filter_over */
162779 -1, /* (192) term ::= CTIME_KW */
162780 -5, /* (193) expr ::= LP nexprlist COMMA expr RP */
162781 -3, /* (194) expr ::= expr AND expr */
162782 -3, /* (195) expr ::= expr OR expr */
162783 -3, /* (196) expr ::= expr LT|GT|GE|LE expr */
162784 -3, /* (197) expr ::= expr EQ|NE expr */
162785 -3, /* (198) expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */
162786 -3, /* (199) expr ::= expr PLUS|MINUS expr */
162787 -3, /* (200) expr ::= expr STAR|SLASH|REM expr */
162788 -3, /* (201) expr ::= expr CONCAT expr */
162789 -2, /* (202) likeop ::= NOT LIKE_KW|MATCH */
162790 -3, /* (203) expr ::= expr likeop expr */
162791 -5, /* (204) expr ::= expr likeop expr ESCAPE expr */
162792 -2, /* (205) expr ::= expr ISNULL|NOTNULL */
162793 -3, /* (206) expr ::= expr NOT NULL */
162794 -3, /* (207) expr ::= expr IS expr */
162795 -4, /* (208) expr ::= expr IS NOT expr */
162796 -2, /* (209) expr ::= NOT expr */
162797 -2, /* (210) expr ::= BITNOT expr */
162798 -2, /* (211) expr ::= PLUS|MINUS expr */
162799 -1, /* (212) between_op ::= BETWEEN */
162800 -2, /* (213) between_op ::= NOT BETWEEN */
162801 -5, /* (214) expr ::= expr between_op expr AND expr */
162802 -1, /* (215) in_op ::= IN */
162803 -2, /* (216) in_op ::= NOT IN */
162804 -5, /* (217) expr ::= expr in_op LP exprlist RP */
162805 -3, /* (218) expr ::= LP select RP */
162806 -5, /* (219) expr ::= expr in_op LP select RP */
162807 -5, /* (220) expr ::= expr in_op nm dbnm paren_exprlist */
162808 -4, /* (221) expr ::= EXISTS LP select RP */
162809 -5, /* (222) expr ::= CASE case_operand case_exprlist case_else END */
162810 -5, /* (223) case_exprlist ::= case_exprlist WHEN expr THEN expr */
162811 -4, /* (224) case_exprlist ::= WHEN expr THEN expr */
162812 -2, /* (225) case_else ::= ELSE expr */
162813 0, /* (226) case_else ::= */
162814 -1, /* (227) case_operand ::= expr */
162815 0, /* (228) case_operand ::= */
162816 0, /* (229) exprlist ::= */
162817 -3, /* (230) nexprlist ::= nexprlist COMMA expr */
162818 -1, /* (231) nexprlist ::= expr */
162819 0, /* (232) paren_exprlist ::= */
162820 -3, /* (233) paren_exprlist ::= LP exprlist RP */
162821 -12, /* (234) cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
162822 -1, /* (235) uniqueflag ::= UNIQUE */
162823 0, /* (236) uniqueflag ::= */
162824 0, /* (237) eidlist_opt ::= */
162825 -3, /* (238) eidlist_opt ::= LP eidlist RP */
162826 -5, /* (239) eidlist ::= eidlist COMMA nm collate sortorder */
162827 -3, /* (240) eidlist ::= nm collate sortorder */
162828 0, /* (241) collate ::= */
162829 -2, /* (242) collate ::= COLLATE ID|STRING */
162830 -4, /* (243) cmd ::= DROP INDEX ifexists fullname */
162831 -2, /* (244) cmd ::= VACUUM vinto */
162832 -3, /* (245) cmd ::= VACUUM nm vinto */
162833 -2, /* (246) vinto ::= INTO expr */
162834 0, /* (247) vinto ::= */
162835 -3, /* (248) cmd ::= PRAGMA nm dbnm */
162836 -5, /* (249) cmd ::= PRAGMA nm dbnm EQ nmnum */
162837 -6, /* (250) cmd ::= PRAGMA nm dbnm LP nmnum RP */
162838 -5, /* (251) cmd ::= PRAGMA nm dbnm EQ minus_num */
162839 -6, /* (252) cmd ::= PRAGMA nm dbnm LP minus_num RP */
162840 -2, /* (253) plus_num ::= PLUS INTEGER|FLOAT */
162841 -2, /* (254) minus_num ::= MINUS INTEGER|FLOAT */
162842 -5, /* (255) cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
162843 -11, /* (256) trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
162844 -1, /* (257) trigger_time ::= BEFORE|AFTER */
162845 -2, /* (258) trigger_time ::= INSTEAD OF */
162846 0, /* (259) trigger_time ::= */
162847 -1, /* (260) trigger_event ::= DELETE|INSERT */
162848 -1, /* (261) trigger_event ::= UPDATE */
162849 -3, /* (262) trigger_event ::= UPDATE OF idlist */
162850 0, /* (263) when_clause ::= */
162851 -2, /* (264) when_clause ::= WHEN expr */
162852 -3, /* (265) trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
162853 -2, /* (266) trigger_cmd_list ::= trigger_cmd SEMI */
162854 -3, /* (267) trnm ::= nm DOT nm */
162855 -3, /* (268) tridxby ::= INDEXED BY nm */
162856 -2, /* (269) tridxby ::= NOT INDEXED */
162857 -9, /* (270) trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist from where_opt scanpt */
162858 -8, /* (271) trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
162859 -6, /* (272) trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
162860 -3, /* (273) trigger_cmd ::= scanpt select scanpt */
162861 -4, /* (274) expr ::= RAISE LP IGNORE RP */
162862 -6, /* (275) expr ::= RAISE LP raisetype COMMA nm RP */
162863 -1, /* (276) raisetype ::= ROLLBACK */
162864 -1, /* (277) raisetype ::= ABORT */
162865 -1, /* (278) raisetype ::= FAIL */
162866 -4, /* (279) cmd ::= DROP TRIGGER ifexists fullname */
162867 -6, /* (280) cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
162868 -3, /* (281) cmd ::= DETACH database_kw_opt expr */
162869 0, /* (282) key_opt ::= */
162870 -2, /* (283) key_opt ::= KEY expr */
162871 -1, /* (284) cmd ::= REINDEX */
162872 -3, /* (285) cmd ::= REINDEX nm dbnm */
162873 -1, /* (286) cmd ::= ANALYZE */
162874 -3, /* (287) cmd ::= ANALYZE nm dbnm */
162875 -6, /* (288) cmd ::= ALTER TABLE fullname RENAME TO nm */
162876 -7, /* (289) cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
162877 -6, /* (290) cmd ::= ALTER TABLE fullname DROP kwcolumn_opt nm */
162878 -1, /* (291) add_column_fullname ::= fullname */
162879 -8, /* (292) cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
162880 -1, /* (293) cmd ::= create_vtab */
162881 -4, /* (294) cmd ::= create_vtab LP vtabarglist RP */
162882 -8, /* (295) create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
162883 0, /* (296) vtabarg ::= */
162884 -1, /* (297) vtabargtoken ::= ANY */
162885 -3, /* (298) vtabargtoken ::= lp anylist RP */
162886 -1, /* (299) lp ::= LP */
162887 -2, /* (300) with ::= WITH wqlist */
162888 -3, /* (301) with ::= WITH RECURSIVE wqlist */
162889 -1, /* (302) wqas ::= AS */
162890 -2, /* (303) wqas ::= AS MATERIALIZED */
162891 -3, /* (304) wqas ::= AS NOT MATERIALIZED */
162892 -6, /* (305) wqitem ::= nm eidlist_opt wqas LP select RP */
162893 -1, /* (306) wqlist ::= wqitem */
162894 -3, /* (307) wqlist ::= wqlist COMMA wqitem */
162895 -1, /* (308) windowdefn_list ::= windowdefn */
162896 -3, /* (309) windowdefn_list ::= windowdefn_list COMMA windowdefn */
162897 -5, /* (310) windowdefn ::= nm AS LP window RP */
162898 -5, /* (311) window ::= PARTITION BY nexprlist orderby_opt frame_opt */
162899 -6, /* (312) window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
162900 -4, /* (313) window ::= ORDER BY sortlist frame_opt */
162901 -5, /* (314) window ::= nm ORDER BY sortlist frame_opt */
162902 -1, /* (315) window ::= frame_opt */
162903 -2, /* (316) window ::= nm frame_opt */
162904 0, /* (317) frame_opt ::= */
162905 -3, /* (318) frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
162906 -6, /* (319) frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt */
162907 -1, /* (320) range_or_rows ::= RANGE|ROWS|GROUPS */
162908 -1, /* (321) frame_bound_s ::= frame_bound */
162909 -2, /* (322) frame_bound_s ::= UNBOUNDED PRECEDING */
162910 -1, /* (323) frame_bound_e ::= frame_bound */
162911 -2, /* (324) frame_bound_e ::= UNBOUNDED FOLLOWING */
162912 -2, /* (325) frame_bound ::= expr PRECEDING|FOLLOWING */
162913 -2, /* (326) frame_bound ::= CURRENT ROW */
162914 0, /* (327) frame_exclude_opt ::= */
162915 -2, /* (328) frame_exclude_opt ::= EXCLUDE frame_exclude */
162916 -2, /* (329) frame_exclude ::= NO OTHERS */
162917 -2, /* (330) frame_exclude ::= CURRENT ROW */
162918 -1, /* (331) frame_exclude ::= GROUP|TIES */
162919 -2, /* (332) window_clause ::= WINDOW windowdefn_list */
162920 -2, /* (333) filter_over ::= filter_clause over_clause */
162921 -1, /* (334) filter_over ::= over_clause */
162922 -1, /* (335) filter_over ::= filter_clause */
162923 -4, /* (336) over_clause ::= OVER LP window RP */
162924 -2, /* (337) over_clause ::= OVER nm */
162925 -5, /* (338) filter_clause ::= FILTER LP WHERE expr RP */
162926 -1, /* (339) input ::= cmdlist */
162927 -2, /* (340) cmdlist ::= cmdlist ecmd */
162928 -1, /* (341) cmdlist ::= ecmd */
162929 -1, /* (342) ecmd ::= SEMI */
162930 -2, /* (343) ecmd ::= cmdx SEMI */
162931 -3, /* (344) ecmd ::= explain cmdx SEMI */
162932 0, /* (345) trans_opt ::= */
162933 -1, /* (346) trans_opt ::= TRANSACTION */
162934 -2, /* (347) trans_opt ::= TRANSACTION nm */
162935 -1, /* (348) savepoint_opt ::= SAVEPOINT */
162936 0, /* (349) savepoint_opt ::= */
162937 -2, /* (350) cmd ::= create_table create_table_args */
162938 -1, /* (351) table_option_set ::= table_option */
162939 -4, /* (352) columnlist ::= columnlist COMMA columnname carglist */
162940 -2, /* (353) columnlist ::= columnname carglist */
162941 -1, /* (354) nm ::= ID|INDEXED */
162942 -1, /* (355) nm ::= STRING */
162943 -1, /* (356) nm ::= JOIN_KW */
162944 -1, /* (357) typetoken ::= typename */
162945 -1, /* (358) typename ::= ID|STRING */
162946 -1, /* (359) signed ::= plus_num */
162947 -1, /* (360) signed ::= minus_num */
162948 -2, /* (361) carglist ::= carglist ccons */
162949 0, /* (362) carglist ::= */
162950 -2, /* (363) ccons ::= NULL onconf */
162951 -4, /* (364) ccons ::= GENERATED ALWAYS AS generated */
162952 -2, /* (365) ccons ::= AS generated */
162953 -2, /* (366) conslist_opt ::= COMMA conslist */
162954 -3, /* (367) conslist ::= conslist tconscomma tcons */
162955 -1, /* (368) conslist ::= tcons */
162956 0, /* (369) tconscomma ::= */
162957 -1, /* (370) defer_subclause_opt ::= defer_subclause */
162958 -1, /* (371) resolvetype ::= raisetype */
162959 -1, /* (372) selectnowith ::= oneselect */
162960 -1, /* (373) oneselect ::= values */
162961 -2, /* (374) sclp ::= selcollist COMMA */
162962 -1, /* (375) as ::= ID|STRING */
162963 0, /* (376) returning ::= */
162964 -1, /* (377) expr ::= term */
162965 -1, /* (378) likeop ::= LIKE_KW|MATCH */
162966 -1, /* (379) exprlist ::= nexprlist */
162967 -1, /* (380) nmnum ::= plus_num */
162968 -1, /* (381) nmnum ::= nm */
162969 -1, /* (382) nmnum ::= ON */
162970 -1, /* (383) nmnum ::= DELETE */
162971 -1, /* (384) nmnum ::= DEFAULT */
162972 -1, /* (385) plus_num ::= INTEGER|FLOAT */
162973 0, /* (386) foreach_clause ::= */
162974 -3, /* (387) foreach_clause ::= FOR EACH ROW */
162975 -1, /* (388) trnm ::= nm */
162976 0, /* (389) tridxby ::= */
162977 -1, /* (390) database_kw_opt ::= DATABASE */
162978 0, /* (391) database_kw_opt ::= */
162979 0, /* (392) kwcolumn_opt ::= */
162980 -1, /* (393) kwcolumn_opt ::= COLUMNKW */
162981 -1, /* (394) vtabarglist ::= vtabarg */
162982 -3, /* (395) vtabarglist ::= vtabarglist COMMA vtabarg */
162983 -2, /* (396) vtabarg ::= vtabarg vtabargtoken */
162984 0, /* (397) anylist ::= */
162985 -4, /* (398) anylist ::= anylist LP anylist RP */
162986 -2, /* (399) anylist ::= anylist ANY */
162987 0, /* (400) with ::= */
162988};
162989
162990static void yy_accept(yyParser*); /* Forward Declaration */
162991
162992/*
162993** Perform a reduce action and the shift that must immediately
162994** follow the reduce.
162995**
162996** The yyLookahead and yyLookaheadToken parameters provide reduce actions
162997** access to the lookahead token (if any). The yyLookahead will be YYNOCODE
162998** if the lookahead token has already been consumed. As this procedure is
162999** only called from one place, optimizing compilers will in-line it, which
163000** means that the extra parameters have no performance impact.
163001*/
163002static YYACTIONTYPE yy_reduce(
163003 yyParser *yypParser, /* The parser */
163004 unsigned int yyruleno, /* Number of the rule by which to reduce */
163005 int yyLookahead, /* Lookahead token, or YYNOCODE if none */
163006 sqlite3ParserTOKENTYPE yyLookaheadToken /* Value of the lookahead token */
163007 sqlite3ParserCTX_PDECL /* %extra_context */
163008){
163009 int yygoto; /* The next state */
163010 YYACTIONTYPE yyact; /* The next action */
163011 yyStackEntry *yymsp; /* The top of the parser's stack */
163012 int yysize; /* Amount to pop the stack */
163013 sqlite3ParserARG_FETCH
163014 (void)yyLookahead;
163015 (void)yyLookaheadToken;
163016 yymsp = yypParser->yytos;
163017
163018 switch( yyruleno ){
163019 /* Beginning here are the reduction cases. A typical example
163020 ** follows:
163021 ** case 0:
163022 ** #line <lineno> <grammarfile>
163023 ** { ... } // User supplied code
163024 ** #line <lineno> <thisfile>
163025 ** break;
163026 */
163027/********** Begin reduce actions **********************************************/
163028 YYMINORTYPE yylhsminor;
163029 case 0: /* explain ::= EXPLAIN */
163030{ pParse->explain = 1; }
163031 break;
163032 case 1: /* explain ::= EXPLAIN QUERY PLAN */
163033{ pParse->explain = 2; }
163034 break;
163035 case 2: /* cmdx ::= cmd */
163036{ sqlite3FinishCoding(pParse); }
163037 break;
163038 case 3: /* cmd ::= BEGIN transtype trans_opt */
163039{sqlite3BeginTransaction(pParse, type: yymsp[-1].minor.yy64);}
163040 break;
163041 case 4: /* transtype ::= */
163042{yymsp[1].minor.yy64 = TK_DEFERRED;}
163043 break;
163044 case 5: /* transtype ::= DEFERRED */
163045 case 6: /* transtype ::= IMMEDIATE */ yytestcase(yyruleno==6);
163046 case 7: /* transtype ::= EXCLUSIVE */ yytestcase(yyruleno==7);
163047 case 320: /* range_or_rows ::= RANGE|ROWS|GROUPS */ yytestcase(yyruleno==320);
163048{yymsp[0].minor.yy64 = yymsp[0].major; /*A-overwrites-X*/}
163049 break;
163050 case 8: /* cmd ::= COMMIT|END trans_opt */
163051 case 9: /* cmd ::= ROLLBACK trans_opt */ yytestcase(yyruleno==9);
163052{sqlite3EndTransaction(pParse,eType: yymsp[-1].major);}
163053 break;
163054 case 10: /* cmd ::= SAVEPOINT nm */
163055{
163056 sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, pName: &yymsp[0].minor.yy0);
163057}
163058 break;
163059 case 11: /* cmd ::= RELEASE savepoint_opt nm */
163060{
163061 sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, pName: &yymsp[0].minor.yy0);
163062}
163063 break;
163064 case 12: /* cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
163065{
163066 sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, pName: &yymsp[0].minor.yy0);
163067}
163068 break;
163069 case 13: /* create_table ::= createkw temp TABLE ifnotexists nm dbnm */
163070{
163071 sqlite3StartTable(pParse,pName1: &yymsp[-1].minor.yy0,pName2: &yymsp[0].minor.yy0,isTemp: yymsp[-4].minor.yy64,isView: 0,isVirtual: 0,noErr: yymsp[-2].minor.yy64);
163072}
163073 break;
163074 case 14: /* createkw ::= CREATE */
163075{disableLookaside(pParse);}
163076 break;
163077 case 15: /* ifnotexists ::= */
163078 case 18: /* temp ::= */ yytestcase(yyruleno==18);
163079 case 47: /* autoinc ::= */ yytestcase(yyruleno==47);
163080 case 62: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==62);
163081 case 72: /* defer_subclause_opt ::= */ yytestcase(yyruleno==72);
163082 case 81: /* ifexists ::= */ yytestcase(yyruleno==81);
163083 case 98: /* distinct ::= */ yytestcase(yyruleno==98);
163084 case 241: /* collate ::= */ yytestcase(yyruleno==241);
163085{yymsp[1].minor.yy64 = 0;}
163086 break;
163087 case 16: /* ifnotexists ::= IF NOT EXISTS */
163088{yymsp[-2].minor.yy64 = 1;}
163089 break;
163090 case 17: /* temp ::= TEMP */
163091{yymsp[0].minor.yy64 = pParse->db->init.busy==0;}
163092 break;
163093 case 19: /* create_table_args ::= LP columnlist conslist_opt RP table_option_set */
163094{
163095 sqlite3EndTable(pParse,pCons: &yymsp[-2].minor.yy0,pEnd: &yymsp[-1].minor.yy0,tabOpts: yymsp[0].minor.yy51,pSelect: 0);
163096}
163097 break;
163098 case 20: /* create_table_args ::= AS select */
163099{
163100 sqlite3EndTable(pParse,pCons: 0,pEnd: 0,tabOpts: 0,pSelect: yymsp[0].minor.yy303);
163101 sqlite3SelectDelete(db: pParse->db, p: yymsp[0].minor.yy303);
163102}
163103 break;
163104 case 21: /* table_option_set ::= */
163105{yymsp[1].minor.yy51 = 0;}
163106 break;
163107 case 22: /* table_option_set ::= table_option_set COMMA table_option */
163108{yylhsminor.yy51 = yymsp[-2].minor.yy51|yymsp[0].minor.yy51;}
163109 yymsp[-2].minor.yy51 = yylhsminor.yy51;
163110 break;
163111 case 23: /* table_option ::= WITHOUT nm */
163112{
163113 if( yymsp[0].minor.yy0.n==5 && sqlite3_strnicmp(zLeft: yymsp[0].minor.yy0.z,zRight: "rowid",N: 5)==0 ){
163114 yymsp[-1].minor.yy51 = TF_WithoutRowid | TF_NoVisibleRowid;
163115 }else{
163116 yymsp[-1].minor.yy51 = 0;
163117 sqlite3ErrorMsg(pParse, zFormat: "unknown table option: %.*s", yymsp[0].minor.yy0.n, yymsp[0].minor.yy0.z);
163118 }
163119}
163120 break;
163121 case 24: /* table_option ::= nm */
163122{
163123 if( yymsp[0].minor.yy0.n==6 && sqlite3_strnicmp(zLeft: yymsp[0].minor.yy0.z,zRight: "strict",N: 6)==0 ){
163124 yylhsminor.yy51 = TF_Strict;
163125 }else{
163126 yylhsminor.yy51 = 0;
163127 sqlite3ErrorMsg(pParse, zFormat: "unknown table option: %.*s", yymsp[0].minor.yy0.n, yymsp[0].minor.yy0.z);
163128 }
163129}
163130 yymsp[0].minor.yy51 = yylhsminor.yy51;
163131 break;
163132 case 25: /* columnname ::= nm typetoken */
163133{sqlite3AddColumn(pParse,sName: yymsp[-1].minor.yy0,sType: yymsp[0].minor.yy0);}
163134 break;
163135 case 26: /* typetoken ::= */
163136 case 65: /* conslist_opt ::= */ yytestcase(yyruleno==65);
163137 case 104: /* as ::= */ yytestcase(yyruleno==104);
163138{yymsp[1].minor.yy0.n = 0; yymsp[1].minor.yy0.z = 0;}
163139 break;
163140 case 27: /* typetoken ::= typename LP signed RP */
163141{
163142 yymsp[-3].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z);
163143}
163144 break;
163145 case 28: /* typetoken ::= typename LP signed COMMA signed RP */
163146{
163147 yymsp[-5].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
163148}
163149 break;
163150 case 29: /* typename ::= typename ID|STRING */
163151{yymsp[-1].minor.yy0.n=yymsp[0].minor.yy0.n+(int)(yymsp[0].minor.yy0.z-yymsp[-1].minor.yy0.z);}
163152 break;
163153 case 30: /* scanpt ::= */
163154{
163155 assert( yyLookahead!=YYNOCODE );
163156 yymsp[1].minor.yy600 = yyLookaheadToken.z;
163157}
163158 break;
163159 case 31: /* scantok ::= */
163160{
163161 assert( yyLookahead!=YYNOCODE );
163162 yymsp[1].minor.yy0 = yyLookaheadToken;
163163}
163164 break;
163165 case 32: /* ccons ::= CONSTRAINT nm */
163166 case 67: /* tcons ::= CONSTRAINT nm */ yytestcase(yyruleno==67);
163167{pParse->constraintName = yymsp[0].minor.yy0;}
163168 break;
163169 case 33: /* ccons ::= DEFAULT scantok term */
163170{sqlite3AddDefaultValue(pParse,pExpr: yymsp[0].minor.yy626,zStart: yymsp[-1].minor.yy0.z,zEnd: &yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n]);}
163171 break;
163172 case 34: /* ccons ::= DEFAULT LP expr RP */
163173{sqlite3AddDefaultValue(pParse,pExpr: yymsp[-1].minor.yy626,zStart: yymsp[-2].minor.yy0.z+1,zEnd: yymsp[0].minor.yy0.z);}
163174 break;
163175 case 35: /* ccons ::= DEFAULT PLUS scantok term */
163176{sqlite3AddDefaultValue(pParse,pExpr: yymsp[0].minor.yy626,zStart: yymsp[-2].minor.yy0.z,zEnd: &yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n]);}
163177 break;
163178 case 36: /* ccons ::= DEFAULT MINUS scantok term */
163179{
163180 Expr *p = sqlite3PExpr(pParse, TK_UMINUS, pLeft: yymsp[0].minor.yy626, pRight: 0);
163181 sqlite3AddDefaultValue(pParse,pExpr: p,zStart: yymsp[-2].minor.yy0.z,zEnd: &yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n]);
163182}
163183 break;
163184 case 37: /* ccons ::= DEFAULT scantok ID|INDEXED */
163185{
163186 Expr *p = tokenExpr(pParse, TK_STRING, t: yymsp[0].minor.yy0);
163187 if( p ){
163188 sqlite3ExprIdToTrueFalse(pExpr: p);
163189 testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
163190 }
163191 sqlite3AddDefaultValue(pParse,pExpr: p,zStart: yymsp[0].minor.yy0.z,zEnd: yymsp[0].minor.yy0.z+yymsp[0].minor.yy0.n);
163192}
163193 break;
163194 case 38: /* ccons ::= NOT NULL onconf */
163195{sqlite3AddNotNull(pParse, onError: yymsp[0].minor.yy64);}
163196 break;
163197 case 39: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
163198{sqlite3AddPrimaryKey(pParse,pList: 0,onError: yymsp[-1].minor.yy64,autoInc: yymsp[0].minor.yy64,sortOrder: yymsp[-2].minor.yy64);}
163199 break;
163200 case 40: /* ccons ::= UNIQUE onconf */
163201{sqlite3CreateIndex(pParse,pName1: 0,pName2: 0,pTblName: 0,pList: 0,onError: yymsp[0].minor.yy64,pStart: 0,pPIWhere: 0,sortOrder: 0,ifNotExist: 0,
163202 SQLITE_IDXTYPE_UNIQUE);}
163203 break;
163204 case 41: /* ccons ::= CHECK LP expr RP */
163205{sqlite3AddCheckConstraint(pParse,pCheckExpr: yymsp[-1].minor.yy626,zStart: yymsp[-2].minor.yy0.z,zEnd: yymsp[0].minor.yy0.z);}
163206 break;
163207 case 42: /* ccons ::= REFERENCES nm eidlist_opt refargs */
163208{sqlite3CreateForeignKey(pParse,pFromCol: 0,pTo: &yymsp[-2].minor.yy0,pToCol: yymsp[-1].minor.yy562,flags: yymsp[0].minor.yy64);}
163209 break;
163210 case 43: /* ccons ::= defer_subclause */
163211{sqlite3DeferForeignKey(pParse,isDeferred: yymsp[0].minor.yy64);}
163212 break;
163213 case 44: /* ccons ::= COLLATE ID|STRING */
163214{sqlite3AddCollateType(pParse, pToken: &yymsp[0].minor.yy0);}
163215 break;
163216 case 45: /* generated ::= LP expr RP */
163217{sqlite3AddGenerated(pParse,pExpr: yymsp[-1].minor.yy626,pType: 0);}
163218 break;
163219 case 46: /* generated ::= LP expr RP ID */
163220{sqlite3AddGenerated(pParse,pExpr: yymsp[-2].minor.yy626,pType: &yymsp[0].minor.yy0);}
163221 break;
163222 case 48: /* autoinc ::= AUTOINCR */
163223{yymsp[0].minor.yy64 = 1;}
163224 break;
163225 case 49: /* refargs ::= */
163226{ yymsp[1].minor.yy64 = OE_None*0x0101; /* EV: R-19803-45884 */}
163227 break;
163228 case 50: /* refargs ::= refargs refarg */
163229{ yymsp[-1].minor.yy64 = (yymsp[-1].minor.yy64 & ~yymsp[0].minor.yy83.mask) | yymsp[0].minor.yy83.value; }
163230 break;
163231 case 51: /* refarg ::= MATCH nm */
163232{ yymsp[-1].minor.yy83.value = 0; yymsp[-1].minor.yy83.mask = 0x000000; }
163233 break;
163234 case 52: /* refarg ::= ON INSERT refact */
163235{ yymsp[-2].minor.yy83.value = 0; yymsp[-2].minor.yy83.mask = 0x000000; }
163236 break;
163237 case 53: /* refarg ::= ON DELETE refact */
163238{ yymsp[-2].minor.yy83.value = yymsp[0].minor.yy64; yymsp[-2].minor.yy83.mask = 0x0000ff; }
163239 break;
163240 case 54: /* refarg ::= ON UPDATE refact */
163241{ yymsp[-2].minor.yy83.value = yymsp[0].minor.yy64<<8; yymsp[-2].minor.yy83.mask = 0x00ff00; }
163242 break;
163243 case 55: /* refact ::= SET NULL */
163244{ yymsp[-1].minor.yy64 = OE_SetNull; /* EV: R-33326-45252 */}
163245 break;
163246 case 56: /* refact ::= SET DEFAULT */
163247{ yymsp[-1].minor.yy64 = OE_SetDflt; /* EV: R-33326-45252 */}
163248 break;
163249 case 57: /* refact ::= CASCADE */
163250{ yymsp[0].minor.yy64 = OE_Cascade; /* EV: R-33326-45252 */}
163251 break;
163252 case 58: /* refact ::= RESTRICT */
163253{ yymsp[0].minor.yy64 = OE_Restrict; /* EV: R-33326-45252 */}
163254 break;
163255 case 59: /* refact ::= NO ACTION */
163256{ yymsp[-1].minor.yy64 = OE_None; /* EV: R-33326-45252 */}
163257 break;
163258 case 60: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
163259{yymsp[-2].minor.yy64 = 0;}
163260 break;
163261 case 61: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
163262 case 76: /* orconf ::= OR resolvetype */ yytestcase(yyruleno==76);
163263 case 171: /* insert_cmd ::= INSERT orconf */ yytestcase(yyruleno==171);
163264{yymsp[-1].minor.yy64 = yymsp[0].minor.yy64;}
163265 break;
163266 case 63: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */
163267 case 80: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==80);
163268 case 213: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==213);
163269 case 216: /* in_op ::= NOT IN */ yytestcase(yyruleno==216);
163270 case 242: /* collate ::= COLLATE ID|STRING */ yytestcase(yyruleno==242);
163271{yymsp[-1].minor.yy64 = 1;}
163272 break;
163273 case 64: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
163274{yymsp[-1].minor.yy64 = 0;}
163275 break;
163276 case 66: /* tconscomma ::= COMMA */
163277{pParse->constraintName.n = 0;}
163278 break;
163279 case 68: /* tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
163280{sqlite3AddPrimaryKey(pParse,pList: yymsp[-3].minor.yy562,onError: yymsp[0].minor.yy64,autoInc: yymsp[-2].minor.yy64,sortOrder: 0);}
163281 break;
163282 case 69: /* tcons ::= UNIQUE LP sortlist RP onconf */
163283{sqlite3CreateIndex(pParse,pName1: 0,pName2: 0,pTblName: 0,pList: yymsp[-2].minor.yy562,onError: yymsp[0].minor.yy64,pStart: 0,pPIWhere: 0,sortOrder: 0,ifNotExist: 0,
163284 SQLITE_IDXTYPE_UNIQUE);}
163285 break;
163286 case 70: /* tcons ::= CHECK LP expr RP onconf */
163287{sqlite3AddCheckConstraint(pParse,pCheckExpr: yymsp[-2].minor.yy626,zStart: yymsp[-3].minor.yy0.z,zEnd: yymsp[-1].minor.yy0.z);}
163288 break;
163289 case 71: /* tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
163290{
163291 sqlite3CreateForeignKey(pParse, pFromCol: yymsp[-6].minor.yy562, pTo: &yymsp[-3].minor.yy0, pToCol: yymsp[-2].minor.yy562, flags: yymsp[-1].minor.yy64);
163292 sqlite3DeferForeignKey(pParse, isDeferred: yymsp[0].minor.yy64);
163293}
163294 break;
163295 case 73: /* onconf ::= */
163296 case 75: /* orconf ::= */ yytestcase(yyruleno==75);
163297{yymsp[1].minor.yy64 = OE_Default;}
163298 break;
163299 case 74: /* onconf ::= ON CONFLICT resolvetype */
163300{yymsp[-2].minor.yy64 = yymsp[0].minor.yy64;}
163301 break;
163302 case 77: /* resolvetype ::= IGNORE */
163303{yymsp[0].minor.yy64 = OE_Ignore;}
163304 break;
163305 case 78: /* resolvetype ::= REPLACE */
163306 case 172: /* insert_cmd ::= REPLACE */ yytestcase(yyruleno==172);
163307{yymsp[0].minor.yy64 = OE_Replace;}
163308 break;
163309 case 79: /* cmd ::= DROP TABLE ifexists fullname */
163310{
163311 sqlite3DropTable(pParse, pName: yymsp[0].minor.yy607, isView: 0, noErr: yymsp[-1].minor.yy64);
163312}
163313 break;
163314 case 82: /* cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
163315{
163316 sqlite3CreateView(pParse, pBegin: &yymsp[-8].minor.yy0, pName1: &yymsp[-4].minor.yy0, pName2: &yymsp[-3].minor.yy0, pCNames: yymsp[-2].minor.yy562, pSelect: yymsp[0].minor.yy303, isTemp: yymsp[-7].minor.yy64, noErr: yymsp[-5].minor.yy64);
163317}
163318 break;
163319 case 83: /* cmd ::= DROP VIEW ifexists fullname */
163320{
163321 sqlite3DropTable(pParse, pName: yymsp[0].minor.yy607, isView: 1, noErr: yymsp[-1].minor.yy64);
163322}
163323 break;
163324 case 84: /* cmd ::= select */
163325{
163326 SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0, 0};
163327 sqlite3Select(pParse, p: yymsp[0].minor.yy303, pDest: &dest);
163328 sqlite3SelectDelete(db: pParse->db, p: yymsp[0].minor.yy303);
163329}
163330 break;
163331 case 85: /* select ::= WITH wqlist selectnowith */
163332{yymsp[-2].minor.yy303 = attachWithToSelect(pParse,pSelect: yymsp[0].minor.yy303,pWith: yymsp[-1].minor.yy43);}
163333 break;
163334 case 86: /* select ::= WITH RECURSIVE wqlist selectnowith */
163335{yymsp[-3].minor.yy303 = attachWithToSelect(pParse,pSelect: yymsp[0].minor.yy303,pWith: yymsp[-1].minor.yy43);}
163336 break;
163337 case 87: /* select ::= selectnowith */
163338{
163339 Select *p = yymsp[0].minor.yy303;
163340 if( p ){
163341 parserDoubleLinkSelect(pParse, p);
163342 }
163343 yymsp[0].minor.yy303 = p; /*A-overwrites-X*/
163344}
163345 break;
163346 case 88: /* selectnowith ::= selectnowith multiselect_op oneselect */
163347{
163348 Select *pRhs = yymsp[0].minor.yy303;
163349 Select *pLhs = yymsp[-2].minor.yy303;
163350 if( pRhs && pRhs->pPrior ){
163351 SrcList *pFrom;
163352 Token x;
163353 x.n = 0;
163354 parserDoubleLinkSelect(pParse, p: pRhs);
163355 pFrom = sqlite3SrcListAppendFromTerm(pParse,p: 0,pTable: 0,pDatabase: 0,pAlias: &x,pSubquery: pRhs,pOn: 0,pUsing: 0);
163356 pRhs = sqlite3SelectNew(pParse,pEList: 0,pSrc: pFrom,pWhere: 0,pGroupBy: 0,pHaving: 0,pOrderBy: 0,selFlags: 0,pLimit: 0);
163357 }
163358 if( pRhs ){
163359 pRhs->op = (u8)yymsp[-1].minor.yy64;
163360 pRhs->pPrior = pLhs;
163361 if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
163362 pRhs->selFlags &= ~SF_MultiValue;
163363 if( yymsp[-1].minor.yy64!=TK_ALL ) pParse->hasCompound = 1;
163364 }else{
163365 sqlite3SelectDelete(db: pParse->db, p: pLhs);
163366 }
163367 yymsp[-2].minor.yy303 = pRhs;
163368}
163369 break;
163370 case 89: /* multiselect_op ::= UNION */
163371 case 91: /* multiselect_op ::= EXCEPT|INTERSECT */ yytestcase(yyruleno==91);
163372{yymsp[0].minor.yy64 = yymsp[0].major; /*A-overwrites-OP*/}
163373 break;
163374 case 90: /* multiselect_op ::= UNION ALL */
163375{yymsp[-1].minor.yy64 = TK_ALL;}
163376 break;
163377 case 92: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
163378{
163379 yymsp[-8].minor.yy303 = sqlite3SelectNew(pParse,pEList: yymsp[-6].minor.yy562,pSrc: yymsp[-5].minor.yy607,pWhere: yymsp[-4].minor.yy626,pGroupBy: yymsp[-3].minor.yy562,pHaving: yymsp[-2].minor.yy626,pOrderBy: yymsp[-1].minor.yy562,selFlags: yymsp[-7].minor.yy64,pLimit: yymsp[0].minor.yy626);
163380}
163381 break;
163382 case 93: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */
163383{
163384 yymsp[-9].minor.yy303 = sqlite3SelectNew(pParse,pEList: yymsp[-7].minor.yy562,pSrc: yymsp[-6].minor.yy607,pWhere: yymsp[-5].minor.yy626,pGroupBy: yymsp[-4].minor.yy562,pHaving: yymsp[-3].minor.yy626,pOrderBy: yymsp[-1].minor.yy562,selFlags: yymsp[-8].minor.yy64,pLimit: yymsp[0].minor.yy626);
163385 if( yymsp[-9].minor.yy303 ){
163386 yymsp[-9].minor.yy303->pWinDefn = yymsp[-2].minor.yy375;
163387 }else{
163388 sqlite3WindowListDelete(db: pParse->db, p: yymsp[-2].minor.yy375);
163389 }
163390}
163391 break;
163392 case 94: /* values ::= VALUES LP nexprlist RP */
163393{
163394 yymsp[-3].minor.yy303 = sqlite3SelectNew(pParse,pEList: yymsp[-1].minor.yy562,pSrc: 0,pWhere: 0,pGroupBy: 0,pHaving: 0,pOrderBy: 0,SF_Values,pLimit: 0);
163395}
163396 break;
163397 case 95: /* values ::= values COMMA LP nexprlist RP */
163398{
163399 Select *pRight, *pLeft = yymsp[-4].minor.yy303;
163400 pRight = sqlite3SelectNew(pParse,pEList: yymsp[-1].minor.yy562,pSrc: 0,pWhere: 0,pGroupBy: 0,pHaving: 0,pOrderBy: 0,SF_Values|SF_MultiValue,pLimit: 0);
163401 if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
163402 if( pRight ){
163403 pRight->op = TK_ALL;
163404 pRight->pPrior = pLeft;
163405 yymsp[-4].minor.yy303 = pRight;
163406 }else{
163407 yymsp[-4].minor.yy303 = pLeft;
163408 }
163409}
163410 break;
163411 case 96: /* distinct ::= DISTINCT */
163412{yymsp[0].minor.yy64 = SF_Distinct;}
163413 break;
163414 case 97: /* distinct ::= ALL */
163415{yymsp[0].minor.yy64 = SF_All;}
163416 break;
163417 case 99: /* sclp ::= */
163418 case 132: /* orderby_opt ::= */ yytestcase(yyruleno==132);
163419 case 142: /* groupby_opt ::= */ yytestcase(yyruleno==142);
163420 case 229: /* exprlist ::= */ yytestcase(yyruleno==229);
163421 case 232: /* paren_exprlist ::= */ yytestcase(yyruleno==232);
163422 case 237: /* eidlist_opt ::= */ yytestcase(yyruleno==237);
163423{yymsp[1].minor.yy562 = 0;}
163424 break;
163425 case 100: /* selcollist ::= sclp scanpt expr scanpt as */
163426{
163427 yymsp[-4].minor.yy562 = sqlite3ExprListAppend(pParse, pList: yymsp[-4].minor.yy562, pExpr: yymsp[-2].minor.yy626);
163428 if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, pList: yymsp[-4].minor.yy562, pName: &yymsp[0].minor.yy0, dequote: 1);
163429 sqlite3ExprListSetSpan(pParse,pList: yymsp[-4].minor.yy562,zStart: yymsp[-3].minor.yy600,zEnd: yymsp[-1].minor.yy600);
163430}
163431 break;
163432 case 101: /* selcollist ::= sclp scanpt STAR */
163433{
163434 Expr *p = sqlite3Expr(db: pParse->db, TK_ASTERISK, zToken: 0);
163435 yymsp[-2].minor.yy562 = sqlite3ExprListAppend(pParse, pList: yymsp[-2].minor.yy562, pExpr: p);
163436}
163437 break;
163438 case 102: /* selcollist ::= sclp scanpt nm DOT STAR */
163439{
163440 Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK, pLeft: 0, pRight: 0);
163441 Expr *pLeft = sqlite3ExprAlloc(db: pParse->db, TK_ID, pToken: &yymsp[-2].minor.yy0, dequote: 1);
163442 Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
163443 yymsp[-4].minor.yy562 = sqlite3ExprListAppend(pParse,pList: yymsp[-4].minor.yy562, pExpr: pDot);
163444}
163445 break;
163446 case 103: /* as ::= AS nm */
163447 case 114: /* dbnm ::= DOT nm */ yytestcase(yyruleno==114);
163448 case 253: /* plus_num ::= PLUS INTEGER|FLOAT */ yytestcase(yyruleno==253);
163449 case 254: /* minus_num ::= MINUS INTEGER|FLOAT */ yytestcase(yyruleno==254);
163450{yymsp[-1].minor.yy0 = yymsp[0].minor.yy0;}
163451 break;
163452 case 105: /* from ::= */
163453 case 108: /* stl_prefix ::= */ yytestcase(yyruleno==108);
163454{yymsp[1].minor.yy607 = 0;}
163455 break;
163456 case 106: /* from ::= FROM seltablist */
163457{
163458 yymsp[-1].minor.yy607 = yymsp[0].minor.yy607;
163459 sqlite3SrcListShiftJoinType(p: yymsp[-1].minor.yy607);
163460}
163461 break;
163462 case 107: /* stl_prefix ::= seltablist joinop */
163463{
163464 if( ALWAYS(yymsp[-1].minor.yy607 && yymsp[-1].minor.yy607->nSrc>0) ) yymsp[-1].minor.yy607->a[yymsp[-1].minor.yy607->nSrc-1].fg.jointype = (u8)yymsp[0].minor.yy64;
163465}
163466 break;
163467 case 109: /* seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
163468{
163469 yymsp[-6].minor.yy607 = sqlite3SrcListAppendFromTerm(pParse,p: yymsp[-6].minor.yy607,pTable: &yymsp[-5].minor.yy0,pDatabase: &yymsp[-4].minor.yy0,pAlias: &yymsp[-3].minor.yy0,pSubquery: 0,pOn: yymsp[-1].minor.yy626,pUsing: yymsp[0].minor.yy240);
163470 sqlite3SrcListIndexedBy(pParse, p: yymsp[-6].minor.yy607, pIndexedBy: &yymsp[-2].minor.yy0);
163471}
163472 break;
163473 case 110: /* seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt */
163474{
163475 yymsp[-8].minor.yy607 = sqlite3SrcListAppendFromTerm(pParse,p: yymsp[-8].minor.yy607,pTable: &yymsp[-7].minor.yy0,pDatabase: &yymsp[-6].minor.yy0,pAlias: &yymsp[-2].minor.yy0,pSubquery: 0,pOn: yymsp[-1].minor.yy626,pUsing: yymsp[0].minor.yy240);
163476 sqlite3SrcListFuncArgs(pParse, p: yymsp[-8].minor.yy607, pList: yymsp[-4].minor.yy562);
163477}
163478 break;
163479 case 111: /* seltablist ::= stl_prefix LP select RP as on_opt using_opt */
163480{
163481 yymsp[-6].minor.yy607 = sqlite3SrcListAppendFromTerm(pParse,p: yymsp[-6].minor.yy607,pTable: 0,pDatabase: 0,pAlias: &yymsp[-2].minor.yy0,pSubquery: yymsp[-4].minor.yy303,pOn: yymsp[-1].minor.yy626,pUsing: yymsp[0].minor.yy240);
163482 }
163483 break;
163484 case 112: /* seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
163485{
163486 if( yymsp[-6].minor.yy607==0 && yymsp[-2].minor.yy0.n==0 && yymsp[-1].minor.yy626==0 && yymsp[0].minor.yy240==0 ){
163487 yymsp[-6].minor.yy607 = yymsp[-4].minor.yy607;
163488 }else if( yymsp[-4].minor.yy607->nSrc==1 ){
163489 yymsp[-6].minor.yy607 = sqlite3SrcListAppendFromTerm(pParse,p: yymsp[-6].minor.yy607,pTable: 0,pDatabase: 0,pAlias: &yymsp[-2].minor.yy0,pSubquery: 0,pOn: yymsp[-1].minor.yy626,pUsing: yymsp[0].minor.yy240);
163490 if( yymsp[-6].minor.yy607 ){
163491 SrcItem *pNew = &yymsp[-6].minor.yy607->a[yymsp[-6].minor.yy607->nSrc-1];
163492 SrcItem *pOld = yymsp[-4].minor.yy607->a;
163493 pNew->zName = pOld->zName;
163494 pNew->zDatabase = pOld->zDatabase;
163495 pNew->pSelect = pOld->pSelect;
163496 if( pOld->fg.isTabFunc ){
163497 pNew->u1.pFuncArg = pOld->u1.pFuncArg;
163498 pOld->u1.pFuncArg = 0;
163499 pOld->fg.isTabFunc = 0;
163500 pNew->fg.isTabFunc = 1;
163501 }
163502 pOld->zName = pOld->zDatabase = 0;
163503 pOld->pSelect = 0;
163504 }
163505 sqlite3SrcListDelete(db: pParse->db, pList: yymsp[-4].minor.yy607);
163506 }else{
163507 Select *pSubquery;
163508 sqlite3SrcListShiftJoinType(p: yymsp[-4].minor.yy607);
163509 pSubquery = sqlite3SelectNew(pParse,pEList: 0,pSrc: yymsp[-4].minor.yy607,pWhere: 0,pGroupBy: 0,pHaving: 0,pOrderBy: 0,SF_NestedFrom,pLimit: 0);
163510 yymsp[-6].minor.yy607 = sqlite3SrcListAppendFromTerm(pParse,p: yymsp[-6].minor.yy607,pTable: 0,pDatabase: 0,pAlias: &yymsp[-2].minor.yy0,pSubquery,pOn: yymsp[-1].minor.yy626,pUsing: yymsp[0].minor.yy240);
163511 }
163512 }
163513 break;
163514 case 113: /* dbnm ::= */
163515 case 127: /* indexed_opt ::= */ yytestcase(yyruleno==127);
163516{yymsp[1].minor.yy0.z=0; yymsp[1].minor.yy0.n=0;}
163517 break;
163518 case 115: /* fullname ::= nm */
163519{
163520 yylhsminor.yy607 = sqlite3SrcListAppend(pParse,pList: 0,pTable: &yymsp[0].minor.yy0,pDatabase: 0);
163521 if( IN_RENAME_OBJECT && yylhsminor.yy607 ) sqlite3RenameTokenMap(pParse, pPtr: yylhsminor.yy607->a[0].zName, pToken: &yymsp[0].minor.yy0);
163522}
163523 yymsp[0].minor.yy607 = yylhsminor.yy607;
163524 break;
163525 case 116: /* fullname ::= nm DOT nm */
163526{
163527 yylhsminor.yy607 = sqlite3SrcListAppend(pParse,pList: 0,pTable: &yymsp[-2].minor.yy0,pDatabase: &yymsp[0].minor.yy0);
163528 if( IN_RENAME_OBJECT && yylhsminor.yy607 ) sqlite3RenameTokenMap(pParse, pPtr: yylhsminor.yy607->a[0].zName, pToken: &yymsp[0].minor.yy0);
163529}
163530 yymsp[-2].minor.yy607 = yylhsminor.yy607;
163531 break;
163532 case 117: /* xfullname ::= nm */
163533{yymsp[0].minor.yy607 = sqlite3SrcListAppend(pParse,pList: 0,pTable: &yymsp[0].minor.yy0,pDatabase: 0); /*A-overwrites-X*/}
163534 break;
163535 case 118: /* xfullname ::= nm DOT nm */
163536{yymsp[-2].minor.yy607 = sqlite3SrcListAppend(pParse,pList: 0,pTable: &yymsp[-2].minor.yy0,pDatabase: &yymsp[0].minor.yy0); /*A-overwrites-X*/}
163537 break;
163538 case 119: /* xfullname ::= nm DOT nm AS nm */
163539{
163540 yymsp[-4].minor.yy607 = sqlite3SrcListAppend(pParse,pList: 0,pTable: &yymsp[-4].minor.yy0,pDatabase: &yymsp[-2].minor.yy0); /*A-overwrites-X*/
163541 if( yymsp[-4].minor.yy607 ) yymsp[-4].minor.yy607->a[0].zAlias = sqlite3NameFromToken(db: pParse->db, pName: &yymsp[0].minor.yy0);
163542}
163543 break;
163544 case 120: /* xfullname ::= nm AS nm */
163545{
163546 yymsp[-2].minor.yy607 = sqlite3SrcListAppend(pParse,pList: 0,pTable: &yymsp[-2].minor.yy0,pDatabase: 0); /*A-overwrites-X*/
163547 if( yymsp[-2].minor.yy607 ) yymsp[-2].minor.yy607->a[0].zAlias = sqlite3NameFromToken(db: pParse->db, pName: &yymsp[0].minor.yy0);
163548}
163549 break;
163550 case 121: /* joinop ::= COMMA|JOIN */
163551{ yymsp[0].minor.yy64 = JT_INNER; }
163552 break;
163553 case 122: /* joinop ::= JOIN_KW JOIN */
163554{yymsp[-1].minor.yy64 = sqlite3JoinType(pParse,pA: &yymsp[-1].minor.yy0,pB: 0,pC: 0); /*X-overwrites-A*/}
163555 break;
163556 case 123: /* joinop ::= JOIN_KW nm JOIN */
163557{yymsp[-2].minor.yy64 = sqlite3JoinType(pParse,pA: &yymsp[-2].minor.yy0,pB: &yymsp[-1].minor.yy0,pC: 0); /*X-overwrites-A*/}
163558 break;
163559 case 124: /* joinop ::= JOIN_KW nm nm JOIN */
163560{yymsp[-3].minor.yy64 = sqlite3JoinType(pParse,pA: &yymsp[-3].minor.yy0,pB: &yymsp[-2].minor.yy0,pC: &yymsp[-1].minor.yy0);/*X-overwrites-A*/}
163561 break;
163562 case 125: /* on_opt ::= ON expr */
163563 case 145: /* having_opt ::= HAVING expr */ yytestcase(yyruleno==145);
163564 case 152: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==152);
163565 case 154: /* where_opt_ret ::= WHERE expr */ yytestcase(yyruleno==154);
163566 case 225: /* case_else ::= ELSE expr */ yytestcase(yyruleno==225);
163567 case 246: /* vinto ::= INTO expr */ yytestcase(yyruleno==246);
163568{yymsp[-1].minor.yy626 = yymsp[0].minor.yy626;}
163569 break;
163570 case 126: /* on_opt ::= */
163571 case 144: /* having_opt ::= */ yytestcase(yyruleno==144);
163572 case 146: /* limit_opt ::= */ yytestcase(yyruleno==146);
163573 case 151: /* where_opt ::= */ yytestcase(yyruleno==151);
163574 case 153: /* where_opt_ret ::= */ yytestcase(yyruleno==153);
163575 case 226: /* case_else ::= */ yytestcase(yyruleno==226);
163576 case 228: /* case_operand ::= */ yytestcase(yyruleno==228);
163577 case 247: /* vinto ::= */ yytestcase(yyruleno==247);
163578{yymsp[1].minor.yy626 = 0;}
163579 break;
163580 case 128: /* indexed_opt ::= INDEXED BY nm */
163581{yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;}
163582 break;
163583 case 129: /* indexed_opt ::= NOT INDEXED */
163584{yymsp[-1].minor.yy0.z=0; yymsp[-1].minor.yy0.n=1;}
163585 break;
163586 case 130: /* using_opt ::= USING LP idlist RP */
163587{yymsp[-3].minor.yy240 = yymsp[-1].minor.yy240;}
163588 break;
163589 case 131: /* using_opt ::= */
163590 case 173: /* idlist_opt ::= */ yytestcase(yyruleno==173);
163591{yymsp[1].minor.yy240 = 0;}
163592 break;
163593 case 133: /* orderby_opt ::= ORDER BY sortlist */
163594 case 143: /* groupby_opt ::= GROUP BY nexprlist */ yytestcase(yyruleno==143);
163595{yymsp[-2].minor.yy562 = yymsp[0].minor.yy562;}
163596 break;
163597 case 134: /* sortlist ::= sortlist COMMA expr sortorder nulls */
163598{
163599 yymsp[-4].minor.yy562 = sqlite3ExprListAppend(pParse,pList: yymsp[-4].minor.yy562,pExpr: yymsp[-2].minor.yy626);
163600 sqlite3ExprListSetSortOrder(p: yymsp[-4].minor.yy562,iSortOrder: yymsp[-1].minor.yy64,eNulls: yymsp[0].minor.yy64);
163601}
163602 break;
163603 case 135: /* sortlist ::= expr sortorder nulls */
163604{
163605 yymsp[-2].minor.yy562 = sqlite3ExprListAppend(pParse,pList: 0,pExpr: yymsp[-2].minor.yy626); /*A-overwrites-Y*/
163606 sqlite3ExprListSetSortOrder(p: yymsp[-2].minor.yy562,iSortOrder: yymsp[-1].minor.yy64,eNulls: yymsp[0].minor.yy64);
163607}
163608 break;
163609 case 136: /* sortorder ::= ASC */
163610{yymsp[0].minor.yy64 = SQLITE_SO_ASC;}
163611 break;
163612 case 137: /* sortorder ::= DESC */
163613{yymsp[0].minor.yy64 = SQLITE_SO_DESC;}
163614 break;
163615 case 138: /* sortorder ::= */
163616 case 141: /* nulls ::= */ yytestcase(yyruleno==141);
163617{yymsp[1].minor.yy64 = SQLITE_SO_UNDEFINED;}
163618 break;
163619 case 139: /* nulls ::= NULLS FIRST */
163620{yymsp[-1].minor.yy64 = SQLITE_SO_ASC;}
163621 break;
163622 case 140: /* nulls ::= NULLS LAST */
163623{yymsp[-1].minor.yy64 = SQLITE_SO_DESC;}
163624 break;
163625 case 147: /* limit_opt ::= LIMIT expr */
163626{yymsp[-1].minor.yy626 = sqlite3PExpr(pParse,TK_LIMIT,pLeft: yymsp[0].minor.yy626,pRight: 0);}
163627 break;
163628 case 148: /* limit_opt ::= LIMIT expr OFFSET expr */
163629{yymsp[-3].minor.yy626 = sqlite3PExpr(pParse,TK_LIMIT,pLeft: yymsp[-2].minor.yy626,pRight: yymsp[0].minor.yy626);}
163630 break;
163631 case 149: /* limit_opt ::= LIMIT expr COMMA expr */
163632{yymsp[-3].minor.yy626 = sqlite3PExpr(pParse,TK_LIMIT,pLeft: yymsp[0].minor.yy626,pRight: yymsp[-2].minor.yy626);}
163633 break;
163634 case 150: /* cmd ::= with DELETE FROM xfullname indexed_opt where_opt_ret */
163635{
163636 sqlite3SrcListIndexedBy(pParse, p: yymsp[-2].minor.yy607, pIndexedBy: &yymsp[-1].minor.yy0);
163637 sqlite3DeleteFrom(pParse,pTabList: yymsp[-2].minor.yy607,pWhere: yymsp[0].minor.yy626,pOrderBy: 0,pLimit: 0);
163638}
163639 break;
163640 case 155: /* where_opt_ret ::= RETURNING selcollist */
163641{sqlite3AddReturning(pParse,pList: yymsp[0].minor.yy562); yymsp[-1].minor.yy626 = 0;}
163642 break;
163643 case 156: /* where_opt_ret ::= WHERE expr RETURNING selcollist */
163644{sqlite3AddReturning(pParse,pList: yymsp[0].minor.yy562); yymsp[-3].minor.yy626 = yymsp[-2].minor.yy626;}
163645 break;
163646 case 157: /* cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret */
163647{
163648 sqlite3SrcListIndexedBy(pParse, p: yymsp[-5].minor.yy607, pIndexedBy: &yymsp[-4].minor.yy0);
163649 sqlite3ExprListCheckLength(pParse,pEList: yymsp[-2].minor.yy562,zObject: "set list");
163650 yymsp[-5].minor.yy607 = sqlite3SrcListAppendList(pParse, p1: yymsp[-5].minor.yy607, p2: yymsp[-1].minor.yy607);
163651 sqlite3Update(pParse,pTabList: yymsp[-5].minor.yy607,pChanges: yymsp[-2].minor.yy562,pWhere: yymsp[0].minor.yy626,onError: yymsp[-6].minor.yy64,pOrderBy: 0,pLimit: 0,pUpsert: 0);
163652}
163653 break;
163654 case 158: /* setlist ::= setlist COMMA nm EQ expr */
163655{
163656 yymsp[-4].minor.yy562 = sqlite3ExprListAppend(pParse, pList: yymsp[-4].minor.yy562, pExpr: yymsp[0].minor.yy626);
163657 sqlite3ExprListSetName(pParse, pList: yymsp[-4].minor.yy562, pName: &yymsp[-2].minor.yy0, dequote: 1);
163658}
163659 break;
163660 case 159: /* setlist ::= setlist COMMA LP idlist RP EQ expr */
163661{
163662 yymsp[-6].minor.yy562 = sqlite3ExprListAppendVector(pParse, pList: yymsp[-6].minor.yy562, pColumns: yymsp[-3].minor.yy240, pExpr: yymsp[0].minor.yy626);
163663}
163664 break;
163665 case 160: /* setlist ::= nm EQ expr */
163666{
163667 yylhsminor.yy562 = sqlite3ExprListAppend(pParse, pList: 0, pExpr: yymsp[0].minor.yy626);
163668 sqlite3ExprListSetName(pParse, pList: yylhsminor.yy562, pName: &yymsp[-2].minor.yy0, dequote: 1);
163669}
163670 yymsp[-2].minor.yy562 = yylhsminor.yy562;
163671 break;
163672 case 161: /* setlist ::= LP idlist RP EQ expr */
163673{
163674 yymsp[-4].minor.yy562 = sqlite3ExprListAppendVector(pParse, pList: 0, pColumns: yymsp[-3].minor.yy240, pExpr: yymsp[0].minor.yy626);
163675}
163676 break;
163677 case 162: /* cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
163678{
163679 sqlite3Insert(pParse, pTabList: yymsp[-3].minor.yy607, pSelect: yymsp[-1].minor.yy303, pColumn: yymsp[-2].minor.yy240, onError: yymsp[-5].minor.yy64, pUpsert: yymsp[0].minor.yy138);
163680}
163681 break;
163682 case 163: /* cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES returning */
163683{
163684 sqlite3Insert(pParse, pTabList: yymsp[-4].minor.yy607, pSelect: 0, pColumn: yymsp[-3].minor.yy240, onError: yymsp[-6].minor.yy64, pUpsert: 0);
163685}
163686 break;
163687 case 164: /* upsert ::= */
163688{ yymsp[1].minor.yy138 = 0; }
163689 break;
163690 case 165: /* upsert ::= RETURNING selcollist */
163691{ yymsp[-1].minor.yy138 = 0; sqlite3AddReturning(pParse,pList: yymsp[0].minor.yy562); }
163692 break;
163693 case 166: /* upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt upsert */
163694{ yymsp[-11].minor.yy138 = sqlite3UpsertNew(db: pParse->db,pTarget: yymsp[-8].minor.yy562,pTargetWhere: yymsp[-6].minor.yy626,pSet: yymsp[-2].minor.yy562,pWhere: yymsp[-1].minor.yy626,pNext: yymsp[0].minor.yy138);}
163695 break;
163696 case 167: /* upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING upsert */
163697{ yymsp[-8].minor.yy138 = sqlite3UpsertNew(db: pParse->db,pTarget: yymsp[-5].minor.yy562,pTargetWhere: yymsp[-3].minor.yy626,pSet: 0,pWhere: 0,pNext: yymsp[0].minor.yy138); }
163698 break;
163699 case 168: /* upsert ::= ON CONFLICT DO NOTHING returning */
163700{ yymsp[-4].minor.yy138 = sqlite3UpsertNew(db: pParse->db,pTarget: 0,pTargetWhere: 0,pSet: 0,pWhere: 0,pNext: 0); }
163701 break;
163702 case 169: /* upsert ::= ON CONFLICT DO UPDATE SET setlist where_opt returning */
163703{ yymsp[-7].minor.yy138 = sqlite3UpsertNew(db: pParse->db,pTarget: 0,pTargetWhere: 0,pSet: yymsp[-2].minor.yy562,pWhere: yymsp[-1].minor.yy626,pNext: 0);}
163704 break;
163705 case 170: /* returning ::= RETURNING selcollist */
163706{sqlite3AddReturning(pParse,pList: yymsp[0].minor.yy562);}
163707 break;
163708 case 174: /* idlist_opt ::= LP idlist RP */
163709{yymsp[-2].minor.yy240 = yymsp[-1].minor.yy240;}
163710 break;
163711 case 175: /* idlist ::= idlist COMMA nm */
163712{yymsp[-2].minor.yy240 = sqlite3IdListAppend(pParse,pList: yymsp[-2].minor.yy240,pToken: &yymsp[0].minor.yy0);}
163713 break;
163714 case 176: /* idlist ::= nm */
163715{yymsp[0].minor.yy240 = sqlite3IdListAppend(pParse,pList: 0,pToken: &yymsp[0].minor.yy0); /*A-overwrites-Y*/}
163716 break;
163717 case 177: /* expr ::= LP expr RP */
163718{yymsp[-2].minor.yy626 = yymsp[-1].minor.yy626;}
163719 break;
163720 case 178: /* expr ::= ID|INDEXED */
163721 case 179: /* expr ::= JOIN_KW */ yytestcase(yyruleno==179);
163722{yymsp[0].minor.yy626=tokenExpr(pParse,TK_ID,t: yymsp[0].minor.yy0); /*A-overwrites-X*/}
163723 break;
163724 case 180: /* expr ::= nm DOT nm */
163725{
163726 Expr *temp1 = sqlite3ExprAlloc(db: pParse->db, TK_ID, pToken: &yymsp[-2].minor.yy0, dequote: 1);
163727 Expr *temp2 = sqlite3ExprAlloc(db: pParse->db, TK_ID, pToken: &yymsp[0].minor.yy0, dequote: 1);
163728 if( IN_RENAME_OBJECT ){
163729 sqlite3RenameTokenMap(pParse, pPtr: (void*)temp2, pToken: &yymsp[0].minor.yy0);
163730 sqlite3RenameTokenMap(pParse, pPtr: (void*)temp1, pToken: &yymsp[-2].minor.yy0);
163731 }
163732 yylhsminor.yy626 = sqlite3PExpr(pParse, TK_DOT, pLeft: temp1, pRight: temp2);
163733}
163734 yymsp[-2].minor.yy626 = yylhsminor.yy626;
163735 break;
163736 case 181: /* expr ::= nm DOT nm DOT nm */
163737{
163738 Expr *temp1 = sqlite3ExprAlloc(db: pParse->db, TK_ID, pToken: &yymsp[-4].minor.yy0, dequote: 1);
163739 Expr *temp2 = sqlite3ExprAlloc(db: pParse->db, TK_ID, pToken: &yymsp[-2].minor.yy0, dequote: 1);
163740 Expr *temp3 = sqlite3ExprAlloc(db: pParse->db, TK_ID, pToken: &yymsp[0].minor.yy0, dequote: 1);
163741 Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, pLeft: temp2, pRight: temp3);
163742 if( IN_RENAME_OBJECT ){
163743 sqlite3RenameTokenMap(pParse, pPtr: (void*)temp3, pToken: &yymsp[0].minor.yy0);
163744 sqlite3RenameTokenMap(pParse, pPtr: (void*)temp2, pToken: &yymsp[-2].minor.yy0);
163745 }
163746 yylhsminor.yy626 = sqlite3PExpr(pParse, TK_DOT, pLeft: temp1, pRight: temp4);
163747}
163748 yymsp[-4].minor.yy626 = yylhsminor.yy626;
163749 break;
163750 case 182: /* term ::= NULL|FLOAT|BLOB */
163751 case 183: /* term ::= STRING */ yytestcase(yyruleno==183);
163752{yymsp[0].minor.yy626=tokenExpr(pParse,op: yymsp[0].major,t: yymsp[0].minor.yy0); /*A-overwrites-X*/}
163753 break;
163754 case 184: /* term ::= INTEGER */
163755{
163756 yylhsminor.yy626 = sqlite3ExprAlloc(db: pParse->db, TK_INTEGER, pToken: &yymsp[0].minor.yy0, dequote: 1);
163757}
163758 yymsp[0].minor.yy626 = yylhsminor.yy626;
163759 break;
163760 case 185: /* expr ::= VARIABLE */
163761{
163762 if( !(yymsp[0].minor.yy0.z[0]=='#' && sqlite3Isdigit(yymsp[0].minor.yy0.z[1])) ){
163763 u32 n = yymsp[0].minor.yy0.n;
163764 yymsp[0].minor.yy626 = tokenExpr(pParse, TK_VARIABLE, t: yymsp[0].minor.yy0);
163765 sqlite3ExprAssignVarNumber(pParse, pExpr: yymsp[0].minor.yy626, n);
163766 }else{
163767 /* When doing a nested parse, one can include terms in an expression
163768 ** that look like this: #1 #2 ... These terms refer to registers
163769 ** in the virtual machine. #N is the N-th register. */
163770 Token t = yymsp[0].minor.yy0; /*A-overwrites-X*/
163771 assert( t.n>=2 );
163772 if( pParse->nested==0 ){
163773 sqlite3ErrorMsg(pParse, zFormat: "near \"%T\": syntax error", &t);
163774 yymsp[0].minor.yy626 = 0;
163775 }else{
163776 yymsp[0].minor.yy626 = sqlite3PExpr(pParse, TK_REGISTER, pLeft: 0, pRight: 0);
163777 if( yymsp[0].minor.yy626 ) sqlite3GetInt32(zNum: &t.z[1], pValue: &yymsp[0].minor.yy626->iTable);
163778 }
163779 }
163780}
163781 break;
163782 case 186: /* expr ::= expr COLLATE ID|STRING */
163783{
163784 yymsp[-2].minor.yy626 = sqlite3ExprAddCollateToken(pParse, pExpr: yymsp[-2].minor.yy626, pCollName: &yymsp[0].minor.yy0, dequote: 1);
163785}
163786 break;
163787 case 187: /* expr ::= CAST LP expr AS typetoken RP */
163788{
163789 yymsp[-5].minor.yy626 = sqlite3ExprAlloc(db: pParse->db, TK_CAST, pToken: &yymsp[-1].minor.yy0, dequote: 1);
163790 sqlite3ExprAttachSubtrees(db: pParse->db, pRoot: yymsp[-5].minor.yy626, pLeft: yymsp[-3].minor.yy626, pRight: 0);
163791}
163792 break;
163793 case 188: /* expr ::= ID|INDEXED LP distinct exprlist RP */
163794{
163795 yylhsminor.yy626 = sqlite3ExprFunction(pParse, pList: yymsp[-1].minor.yy562, pToken: &yymsp[-4].minor.yy0, eDistinct: yymsp[-2].minor.yy64);
163796}
163797 yymsp[-4].minor.yy626 = yylhsminor.yy626;
163798 break;
163799 case 189: /* expr ::= ID|INDEXED LP STAR RP */
163800{
163801 yylhsminor.yy626 = sqlite3ExprFunction(pParse, pList: 0, pToken: &yymsp[-3].minor.yy0, eDistinct: 0);
163802}
163803 yymsp[-3].minor.yy626 = yylhsminor.yy626;
163804 break;
163805 case 190: /* expr ::= ID|INDEXED LP distinct exprlist RP filter_over */
163806{
163807 yylhsminor.yy626 = sqlite3ExprFunction(pParse, pList: yymsp[-2].minor.yy562, pToken: &yymsp[-5].minor.yy0, eDistinct: yymsp[-3].minor.yy64);
163808 sqlite3WindowAttach(pParse, p: yylhsminor.yy626, pWin: yymsp[0].minor.yy375);
163809}
163810 yymsp[-5].minor.yy626 = yylhsminor.yy626;
163811 break;
163812 case 191: /* expr ::= ID|INDEXED LP STAR RP filter_over */
163813{
163814 yylhsminor.yy626 = sqlite3ExprFunction(pParse, pList: 0, pToken: &yymsp[-4].minor.yy0, eDistinct: 0);
163815 sqlite3WindowAttach(pParse, p: yylhsminor.yy626, pWin: yymsp[0].minor.yy375);
163816}
163817 yymsp[-4].minor.yy626 = yylhsminor.yy626;
163818 break;
163819 case 192: /* term ::= CTIME_KW */
163820{
163821 yylhsminor.yy626 = sqlite3ExprFunction(pParse, pList: 0, pToken: &yymsp[0].minor.yy0, eDistinct: 0);
163822}
163823 yymsp[0].minor.yy626 = yylhsminor.yy626;
163824 break;
163825 case 193: /* expr ::= LP nexprlist COMMA expr RP */
163826{
163827 ExprList *pList = sqlite3ExprListAppend(pParse, pList: yymsp[-3].minor.yy562, pExpr: yymsp[-1].minor.yy626);
163828 yymsp[-4].minor.yy626 = sqlite3PExpr(pParse, TK_VECTOR, pLeft: 0, pRight: 0);
163829 if( yymsp[-4].minor.yy626 ){
163830 yymsp[-4].minor.yy626->x.pList = pList;
163831 if( ALWAYS(pList->nExpr) ){
163832 yymsp[-4].minor.yy626->flags |= pList->a[0].pExpr->flags & EP_Propagate;
163833 }
163834 }else{
163835 sqlite3ExprListDelete(db: pParse->db, pList);
163836 }
163837}
163838 break;
163839 case 194: /* expr ::= expr AND expr */
163840{yymsp[-2].minor.yy626=sqlite3ExprAnd(pParse,pLeft: yymsp[-2].minor.yy626,pRight: yymsp[0].minor.yy626);}
163841 break;
163842 case 195: /* expr ::= expr OR expr */
163843 case 196: /* expr ::= expr LT|GT|GE|LE expr */ yytestcase(yyruleno==196);
163844 case 197: /* expr ::= expr EQ|NE expr */ yytestcase(yyruleno==197);
163845 case 198: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ yytestcase(yyruleno==198);
163846 case 199: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==199);
163847 case 200: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==200);
163848 case 201: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==201);
163849{yymsp[-2].minor.yy626=sqlite3PExpr(pParse,op: yymsp[-1].major,pLeft: yymsp[-2].minor.yy626,pRight: yymsp[0].minor.yy626);}
163850 break;
163851 case 202: /* likeop ::= NOT LIKE_KW|MATCH */
163852{yymsp[-1].minor.yy0=yymsp[0].minor.yy0; yymsp[-1].minor.yy0.n|=0x80000000; /*yymsp[-1].minor.yy0-overwrite-yymsp[0].minor.yy0*/}
163853 break;
163854 case 203: /* expr ::= expr likeop expr */
163855{
163856 ExprList *pList;
163857 int bNot = yymsp[-1].minor.yy0.n & 0x80000000;
163858 yymsp[-1].minor.yy0.n &= 0x7fffffff;
163859 pList = sqlite3ExprListAppend(pParse,pList: 0, pExpr: yymsp[0].minor.yy626);
163860 pList = sqlite3ExprListAppend(pParse,pList, pExpr: yymsp[-2].minor.yy626);
163861 yymsp[-2].minor.yy626 = sqlite3ExprFunction(pParse, pList, pToken: &yymsp[-1].minor.yy0, eDistinct: 0);
163862 if( bNot ) yymsp[-2].minor.yy626 = sqlite3PExpr(pParse, TK_NOT, pLeft: yymsp[-2].minor.yy626, pRight: 0);
163863 if( yymsp[-2].minor.yy626 ) yymsp[-2].minor.yy626->flags |= EP_InfixFunc;
163864}
163865 break;
163866 case 204: /* expr ::= expr likeop expr ESCAPE expr */
163867{
163868 ExprList *pList;
163869 int bNot = yymsp[-3].minor.yy0.n & 0x80000000;
163870 yymsp[-3].minor.yy0.n &= 0x7fffffff;
163871 pList = sqlite3ExprListAppend(pParse,pList: 0, pExpr: yymsp[-2].minor.yy626);
163872 pList = sqlite3ExprListAppend(pParse,pList, pExpr: yymsp[-4].minor.yy626);
163873 pList = sqlite3ExprListAppend(pParse,pList, pExpr: yymsp[0].minor.yy626);
163874 yymsp[-4].minor.yy626 = sqlite3ExprFunction(pParse, pList, pToken: &yymsp[-3].minor.yy0, eDistinct: 0);
163875 if( bNot ) yymsp[-4].minor.yy626 = sqlite3PExpr(pParse, TK_NOT, pLeft: yymsp[-4].minor.yy626, pRight: 0);
163876 if( yymsp[-4].minor.yy626 ) yymsp[-4].minor.yy626->flags |= EP_InfixFunc;
163877}
163878 break;
163879 case 205: /* expr ::= expr ISNULL|NOTNULL */
163880{yymsp[-1].minor.yy626 = sqlite3PExpr(pParse,op: yymsp[0].major,pLeft: yymsp[-1].minor.yy626,pRight: 0);}
163881 break;
163882 case 206: /* expr ::= expr NOT NULL */
163883{yymsp[-2].minor.yy626 = sqlite3PExpr(pParse,TK_NOTNULL,pLeft: yymsp[-2].minor.yy626,pRight: 0);}
163884 break;
163885 case 207: /* expr ::= expr IS expr */
163886{
163887 yymsp[-2].minor.yy626 = sqlite3PExpr(pParse,TK_IS,pLeft: yymsp[-2].minor.yy626,pRight: yymsp[0].minor.yy626);
163888 binaryToUnaryIfNull(pParse, pY: yymsp[0].minor.yy626, pA: yymsp[-2].minor.yy626, TK_ISNULL);
163889}
163890 break;
163891 case 208: /* expr ::= expr IS NOT expr */
163892{
163893 yymsp[-3].minor.yy626 = sqlite3PExpr(pParse,TK_ISNOT,pLeft: yymsp[-3].minor.yy626,pRight: yymsp[0].minor.yy626);
163894 binaryToUnaryIfNull(pParse, pY: yymsp[0].minor.yy626, pA: yymsp[-3].minor.yy626, TK_NOTNULL);
163895}
163896 break;
163897 case 209: /* expr ::= NOT expr */
163898 case 210: /* expr ::= BITNOT expr */ yytestcase(yyruleno==210);
163899{yymsp[-1].minor.yy626 = sqlite3PExpr(pParse, op: yymsp[-1].major, pLeft: yymsp[0].minor.yy626, pRight: 0);/*A-overwrites-B*/}
163900 break;
163901 case 211: /* expr ::= PLUS|MINUS expr */
163902{
163903 yymsp[-1].minor.yy626 = sqlite3PExpr(pParse, op: yymsp[-1].major==TK_PLUS ? TK_UPLUS : TK_UMINUS, pLeft: yymsp[0].minor.yy626, pRight: 0);
163904 /*A-overwrites-B*/
163905}
163906 break;
163907 case 212: /* between_op ::= BETWEEN */
163908 case 215: /* in_op ::= IN */ yytestcase(yyruleno==215);
163909{yymsp[0].minor.yy64 = 0;}
163910 break;
163911 case 214: /* expr ::= expr between_op expr AND expr */
163912{
163913 ExprList *pList = sqlite3ExprListAppend(pParse,pList: 0, pExpr: yymsp[-2].minor.yy626);
163914 pList = sqlite3ExprListAppend(pParse,pList, pExpr: yymsp[0].minor.yy626);
163915 yymsp[-4].minor.yy626 = sqlite3PExpr(pParse, TK_BETWEEN, pLeft: yymsp[-4].minor.yy626, pRight: 0);
163916 if( yymsp[-4].minor.yy626 ){
163917 yymsp[-4].minor.yy626->x.pList = pList;
163918 }else{
163919 sqlite3ExprListDelete(db: pParse->db, pList);
163920 }
163921 if( yymsp[-3].minor.yy64 ) yymsp[-4].minor.yy626 = sqlite3PExpr(pParse, TK_NOT, pLeft: yymsp[-4].minor.yy626, pRight: 0);
163922}
163923 break;
163924 case 217: /* expr ::= expr in_op LP exprlist RP */
163925{
163926 if( yymsp[-1].minor.yy562==0 ){
163927 /* Expressions of the form
163928 **
163929 ** expr1 IN ()
163930 ** expr1 NOT IN ()
163931 **
163932 ** simplify to constants 0 (false) and 1 (true), respectively,
163933 ** regardless of the value of expr1.
163934 */
163935 sqlite3ExprUnmapAndDelete(pParse, p: yymsp[-4].minor.yy626);
163936 yymsp[-4].minor.yy626 = sqlite3Expr(db: pParse->db, TK_INTEGER, zToken: yymsp[-3].minor.yy64 ? "1" : "0");
163937 }else{
163938 Expr *pRHS = yymsp[-1].minor.yy562->a[0].pExpr;
163939 if( yymsp[-1].minor.yy562->nExpr==1 && sqlite3ExprIsConstant(p: pRHS) && yymsp[-4].minor.yy626->op!=TK_VECTOR ){
163940 yymsp[-1].minor.yy562->a[0].pExpr = 0;
163941 sqlite3ExprListDelete(db: pParse->db, pList: yymsp[-1].minor.yy562);
163942 pRHS = sqlite3PExpr(pParse, TK_UPLUS, pLeft: pRHS, pRight: 0);
163943 yymsp[-4].minor.yy626 = sqlite3PExpr(pParse, TK_EQ, pLeft: yymsp[-4].minor.yy626, pRight: pRHS);
163944 }else{
163945 yymsp[-4].minor.yy626 = sqlite3PExpr(pParse, TK_IN, pLeft: yymsp[-4].minor.yy626, pRight: 0);
163946 if( yymsp[-4].minor.yy626==0 ){
163947 sqlite3ExprListDelete(db: pParse->db, pList: yymsp[-1].minor.yy562);
163948 }else if( yymsp[-4].minor.yy626->pLeft->op==TK_VECTOR ){
163949 int nExpr = yymsp[-4].minor.yy626->pLeft->x.pList->nExpr;
163950 Select *pSelectRHS = sqlite3ExprListToValues(pParse, nElem: nExpr, pEList: yymsp[-1].minor.yy562);
163951 if( pSelectRHS ){
163952 parserDoubleLinkSelect(pParse, p: pSelectRHS);
163953 sqlite3PExprAddSelect(pParse, pExpr: yymsp[-4].minor.yy626, pSelect: pSelectRHS);
163954 }
163955 }else{
163956 yymsp[-4].minor.yy626->x.pList = yymsp[-1].minor.yy562;
163957 sqlite3ExprSetHeightAndFlags(pParse, p: yymsp[-4].minor.yy626);
163958 }
163959 }
163960 if( yymsp[-3].minor.yy64 ) yymsp[-4].minor.yy626 = sqlite3PExpr(pParse, TK_NOT, pLeft: yymsp[-4].minor.yy626, pRight: 0);
163961 }
163962 }
163963 break;
163964 case 218: /* expr ::= LP select RP */
163965{
163966 yymsp[-2].minor.yy626 = sqlite3PExpr(pParse, TK_SELECT, pLeft: 0, pRight: 0);
163967 sqlite3PExprAddSelect(pParse, pExpr: yymsp[-2].minor.yy626, pSelect: yymsp[-1].minor.yy303);
163968 }
163969 break;
163970 case 219: /* expr ::= expr in_op LP select RP */
163971{
163972 yymsp[-4].minor.yy626 = sqlite3PExpr(pParse, TK_IN, pLeft: yymsp[-4].minor.yy626, pRight: 0);
163973 sqlite3PExprAddSelect(pParse, pExpr: yymsp[-4].minor.yy626, pSelect: yymsp[-1].minor.yy303);
163974 if( yymsp[-3].minor.yy64 ) yymsp[-4].minor.yy626 = sqlite3PExpr(pParse, TK_NOT, pLeft: yymsp[-4].minor.yy626, pRight: 0);
163975 }
163976 break;
163977 case 220: /* expr ::= expr in_op nm dbnm paren_exprlist */
163978{
163979 SrcList *pSrc = sqlite3SrcListAppend(pParse, pList: 0,pTable: &yymsp[-2].minor.yy0,pDatabase: &yymsp[-1].minor.yy0);
163980 Select *pSelect = sqlite3SelectNew(pParse, pEList: 0,pSrc,pWhere: 0,pGroupBy: 0,pHaving: 0,pOrderBy: 0,selFlags: 0,pLimit: 0);
163981 if( yymsp[0].minor.yy562 ) sqlite3SrcListFuncArgs(pParse, p: pSelect ? pSrc : 0, pList: yymsp[0].minor.yy562);
163982 yymsp[-4].minor.yy626 = sqlite3PExpr(pParse, TK_IN, pLeft: yymsp[-4].minor.yy626, pRight: 0);
163983 sqlite3PExprAddSelect(pParse, pExpr: yymsp[-4].minor.yy626, pSelect);
163984 if( yymsp[-3].minor.yy64 ) yymsp[-4].minor.yy626 = sqlite3PExpr(pParse, TK_NOT, pLeft: yymsp[-4].minor.yy626, pRight: 0);
163985 }
163986 break;
163987 case 221: /* expr ::= EXISTS LP select RP */
163988{
163989 Expr *p;
163990 p = yymsp[-3].minor.yy626 = sqlite3PExpr(pParse, TK_EXISTS, pLeft: 0, pRight: 0);
163991 sqlite3PExprAddSelect(pParse, pExpr: p, pSelect: yymsp[-1].minor.yy303);
163992 }
163993 break;
163994 case 222: /* expr ::= CASE case_operand case_exprlist case_else END */
163995{
163996 yymsp[-4].minor.yy626 = sqlite3PExpr(pParse, TK_CASE, pLeft: yymsp[-3].minor.yy626, pRight: 0);
163997 if( yymsp[-4].minor.yy626 ){
163998 yymsp[-4].minor.yy626->x.pList = yymsp[-1].minor.yy626 ? sqlite3ExprListAppend(pParse,pList: yymsp[-2].minor.yy562,pExpr: yymsp[-1].minor.yy626) : yymsp[-2].minor.yy562;
163999 sqlite3ExprSetHeightAndFlags(pParse, p: yymsp[-4].minor.yy626);
164000 }else{
164001 sqlite3ExprListDelete(db: pParse->db, pList: yymsp[-2].minor.yy562);
164002 sqlite3ExprDelete(db: pParse->db, p: yymsp[-1].minor.yy626);
164003 }
164004}
164005 break;
164006 case 223: /* case_exprlist ::= case_exprlist WHEN expr THEN expr */
164007{
164008 yymsp[-4].minor.yy562 = sqlite3ExprListAppend(pParse,pList: yymsp[-4].minor.yy562, pExpr: yymsp[-2].minor.yy626);
164009 yymsp[-4].minor.yy562 = sqlite3ExprListAppend(pParse,pList: yymsp[-4].minor.yy562, pExpr: yymsp[0].minor.yy626);
164010}
164011 break;
164012 case 224: /* case_exprlist ::= WHEN expr THEN expr */
164013{
164014 yymsp[-3].minor.yy562 = sqlite3ExprListAppend(pParse,pList: 0, pExpr: yymsp[-2].minor.yy626);
164015 yymsp[-3].minor.yy562 = sqlite3ExprListAppend(pParse,pList: yymsp[-3].minor.yy562, pExpr: yymsp[0].minor.yy626);
164016}
164017 break;
164018 case 227: /* case_operand ::= expr */
164019{yymsp[0].minor.yy626 = yymsp[0].minor.yy626; /*A-overwrites-X*/}
164020 break;
164021 case 230: /* nexprlist ::= nexprlist COMMA expr */
164022{yymsp[-2].minor.yy562 = sqlite3ExprListAppend(pParse,pList: yymsp[-2].minor.yy562,pExpr: yymsp[0].minor.yy626);}
164023 break;
164024 case 231: /* nexprlist ::= expr */
164025{yymsp[0].minor.yy562 = sqlite3ExprListAppend(pParse,pList: 0,pExpr: yymsp[0].minor.yy626); /*A-overwrites-Y*/}
164026 break;
164027 case 233: /* paren_exprlist ::= LP exprlist RP */
164028 case 238: /* eidlist_opt ::= LP eidlist RP */ yytestcase(yyruleno==238);
164029{yymsp[-2].minor.yy562 = yymsp[-1].minor.yy562;}
164030 break;
164031 case 234: /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
164032{
164033 sqlite3CreateIndex(pParse, pName1: &yymsp[-7].minor.yy0, pName2: &yymsp[-6].minor.yy0,
164034 pTblName: sqlite3SrcListAppend(pParse,pList: 0,pTable: &yymsp[-4].minor.yy0,pDatabase: 0), pList: yymsp[-2].minor.yy562, onError: yymsp[-10].minor.yy64,
164035 pStart: &yymsp[-11].minor.yy0, pPIWhere: yymsp[0].minor.yy626, SQLITE_SO_ASC, ifNotExist: yymsp[-8].minor.yy64, SQLITE_IDXTYPE_APPDEF);
164036 if( IN_RENAME_OBJECT && pParse->pNewIndex ){
164037 sqlite3RenameTokenMap(pParse, pPtr: pParse->pNewIndex->zName, pToken: &yymsp[-4].minor.yy0);
164038 }
164039}
164040 break;
164041 case 235: /* uniqueflag ::= UNIQUE */
164042 case 277: /* raisetype ::= ABORT */ yytestcase(yyruleno==277);
164043{yymsp[0].minor.yy64 = OE_Abort;}
164044 break;
164045 case 236: /* uniqueflag ::= */
164046{yymsp[1].minor.yy64 = OE_None;}
164047 break;
164048 case 239: /* eidlist ::= eidlist COMMA nm collate sortorder */
164049{
164050 yymsp[-4].minor.yy562 = parserAddExprIdListTerm(pParse, pPrior: yymsp[-4].minor.yy562, pIdToken: &yymsp[-2].minor.yy0, hasCollate: yymsp[-1].minor.yy64, sortOrder: yymsp[0].minor.yy64);
164051}
164052 break;
164053 case 240: /* eidlist ::= nm collate sortorder */
164054{
164055 yymsp[-2].minor.yy562 = parserAddExprIdListTerm(pParse, pPrior: 0, pIdToken: &yymsp[-2].minor.yy0, hasCollate: yymsp[-1].minor.yy64, sortOrder: yymsp[0].minor.yy64); /*A-overwrites-Y*/
164056}
164057 break;
164058 case 243: /* cmd ::= DROP INDEX ifexists fullname */
164059{sqlite3DropIndex(pParse, pName: yymsp[0].minor.yy607, ifExists: yymsp[-1].minor.yy64);}
164060 break;
164061 case 244: /* cmd ::= VACUUM vinto */
164062{sqlite3Vacuum(pParse,pNm: 0,pInto: yymsp[0].minor.yy626);}
164063 break;
164064 case 245: /* cmd ::= VACUUM nm vinto */
164065{sqlite3Vacuum(pParse,pNm: &yymsp[-1].minor.yy0,pInto: yymsp[0].minor.yy626);}
164066 break;
164067 case 248: /* cmd ::= PRAGMA nm dbnm */
164068{sqlite3Pragma(pParse,pId1: &yymsp[-1].minor.yy0,pId2: &yymsp[0].minor.yy0,pValue: 0,minusFlag: 0);}
164069 break;
164070 case 249: /* cmd ::= PRAGMA nm dbnm EQ nmnum */
164071{sqlite3Pragma(pParse,pId1: &yymsp[-3].minor.yy0,pId2: &yymsp[-2].minor.yy0,pValue: &yymsp[0].minor.yy0,minusFlag: 0);}
164072 break;
164073 case 250: /* cmd ::= PRAGMA nm dbnm LP nmnum RP */
164074{sqlite3Pragma(pParse,pId1: &yymsp[-4].minor.yy0,pId2: &yymsp[-3].minor.yy0,pValue: &yymsp[-1].minor.yy0,minusFlag: 0);}
164075 break;
164076 case 251: /* cmd ::= PRAGMA nm dbnm EQ minus_num */
164077{sqlite3Pragma(pParse,pId1: &yymsp[-3].minor.yy0,pId2: &yymsp[-2].minor.yy0,pValue: &yymsp[0].minor.yy0,minusFlag: 1);}
164078 break;
164079 case 252: /* cmd ::= PRAGMA nm dbnm LP minus_num RP */
164080{sqlite3Pragma(pParse,pId1: &yymsp[-4].minor.yy0,pId2: &yymsp[-3].minor.yy0,pValue: &yymsp[-1].minor.yy0,minusFlag: 1);}
164081 break;
164082 case 255: /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
164083{
164084 Token all;
164085 all.z = yymsp[-3].minor.yy0.z;
164086 all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n;
164087 sqlite3FinishTrigger(pParse, pStepList: yymsp[-1].minor.yy95, pAll: &all);
164088}
164089 break;
164090 case 256: /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
164091{
164092 sqlite3BeginTrigger(pParse, pName1: &yymsp[-7].minor.yy0, pName2: &yymsp[-6].minor.yy0, tr_tm: yymsp[-5].minor.yy64, op: yymsp[-4].minor.yy570.a, pColumns: yymsp[-4].minor.yy570.b, pTableName: yymsp[-2].minor.yy607, pWhen: yymsp[0].minor.yy626, isTemp: yymsp[-10].minor.yy64, noErr: yymsp[-8].minor.yy64);
164093 yymsp[-10].minor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0); /*A-overwrites-T*/
164094}
164095 break;
164096 case 257: /* trigger_time ::= BEFORE|AFTER */
164097{ yymsp[0].minor.yy64 = yymsp[0].major; /*A-overwrites-X*/ }
164098 break;
164099 case 258: /* trigger_time ::= INSTEAD OF */
164100{ yymsp[-1].minor.yy64 = TK_INSTEAD;}
164101 break;
164102 case 259: /* trigger_time ::= */
164103{ yymsp[1].minor.yy64 = TK_BEFORE; }
164104 break;
164105 case 260: /* trigger_event ::= DELETE|INSERT */
164106 case 261: /* trigger_event ::= UPDATE */ yytestcase(yyruleno==261);
164107{yymsp[0].minor.yy570.a = yymsp[0].major; /*A-overwrites-X*/ yymsp[0].minor.yy570.b = 0;}
164108 break;
164109 case 262: /* trigger_event ::= UPDATE OF idlist */
164110{yymsp[-2].minor.yy570.a = TK_UPDATE; yymsp[-2].minor.yy570.b = yymsp[0].minor.yy240;}
164111 break;
164112 case 263: /* when_clause ::= */
164113 case 282: /* key_opt ::= */ yytestcase(yyruleno==282);
164114{ yymsp[1].minor.yy626 = 0; }
164115 break;
164116 case 264: /* when_clause ::= WHEN expr */
164117 case 283: /* key_opt ::= KEY expr */ yytestcase(yyruleno==283);
164118{ yymsp[-1].minor.yy626 = yymsp[0].minor.yy626; }
164119 break;
164120 case 265: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
164121{
164122 assert( yymsp[-2].minor.yy95!=0 );
164123 yymsp[-2].minor.yy95->pLast->pNext = yymsp[-1].minor.yy95;
164124 yymsp[-2].minor.yy95->pLast = yymsp[-1].minor.yy95;
164125}
164126 break;
164127 case 266: /* trigger_cmd_list ::= trigger_cmd SEMI */
164128{
164129 assert( yymsp[-1].minor.yy95!=0 );
164130 yymsp[-1].minor.yy95->pLast = yymsp[-1].minor.yy95;
164131}
164132 break;
164133 case 267: /* trnm ::= nm DOT nm */
164134{
164135 yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;
164136 sqlite3ErrorMsg(pParse,
164137 zFormat: "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
164138 "statements within triggers");
164139}
164140 break;
164141 case 268: /* tridxby ::= INDEXED BY nm */
164142{
164143 sqlite3ErrorMsg(pParse,
164144 zFormat: "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
164145 "within triggers");
164146}
164147 break;
164148 case 269: /* tridxby ::= NOT INDEXED */
164149{
164150 sqlite3ErrorMsg(pParse,
164151 zFormat: "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
164152 "within triggers");
164153}
164154 break;
164155 case 270: /* trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist from where_opt scanpt */
164156{yylhsminor.yy95 = sqlite3TriggerUpdateStep(pParse, pTableName: &yymsp[-6].minor.yy0, pFrom: yymsp[-2].minor.yy607, pEList: yymsp[-3].minor.yy562, pWhere: yymsp[-1].minor.yy626, orconf: yymsp[-7].minor.yy64, zStart: yymsp[-8].minor.yy0.z, zEnd: yymsp[0].minor.yy600);}
164157 yymsp[-8].minor.yy95 = yylhsminor.yy95;
164158 break;
164159 case 271: /* trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
164160{
164161 yylhsminor.yy95 = sqlite3TriggerInsertStep(pParse,pTableName: &yymsp[-4].minor.yy0,pColumn: yymsp[-3].minor.yy240,pSelect: yymsp[-2].minor.yy303,orconf: yymsp[-6].minor.yy64,pUpsert: yymsp[-1].minor.yy138,zStart: yymsp[-7].minor.yy600,zEnd: yymsp[0].minor.yy600);/*yylhsminor.yy95-overwrites-yymsp[-6].minor.yy64*/
164162}
164163 yymsp[-7].minor.yy95 = yylhsminor.yy95;
164164 break;
164165 case 272: /* trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
164166{yylhsminor.yy95 = sqlite3TriggerDeleteStep(pParse, pTableName: &yymsp[-3].minor.yy0, pWhere: yymsp[-1].minor.yy626, zStart: yymsp[-5].minor.yy0.z, zEnd: yymsp[0].minor.yy600);}
164167 yymsp[-5].minor.yy95 = yylhsminor.yy95;
164168 break;
164169 case 273: /* trigger_cmd ::= scanpt select scanpt */
164170{yylhsminor.yy95 = sqlite3TriggerSelectStep(db: pParse->db, pSelect: yymsp[-1].minor.yy303, zStart: yymsp[-2].minor.yy600, zEnd: yymsp[0].minor.yy600); /*yylhsminor.yy95-overwrites-yymsp[-1].minor.yy303*/}
164171 yymsp[-2].minor.yy95 = yylhsminor.yy95;
164172 break;
164173 case 274: /* expr ::= RAISE LP IGNORE RP */
164174{
164175 yymsp[-3].minor.yy626 = sqlite3PExpr(pParse, TK_RAISE, pLeft: 0, pRight: 0);
164176 if( yymsp[-3].minor.yy626 ){
164177 yymsp[-3].minor.yy626->affExpr = OE_Ignore;
164178 }
164179}
164180 break;
164181 case 275: /* expr ::= RAISE LP raisetype COMMA nm RP */
164182{
164183 yymsp[-5].minor.yy626 = sqlite3ExprAlloc(db: pParse->db, TK_RAISE, pToken: &yymsp[-1].minor.yy0, dequote: 1);
164184 if( yymsp[-5].minor.yy626 ) {
164185 yymsp[-5].minor.yy626->affExpr = (char)yymsp[-3].minor.yy64;
164186 }
164187}
164188 break;
164189 case 276: /* raisetype ::= ROLLBACK */
164190{yymsp[0].minor.yy64 = OE_Rollback;}
164191 break;
164192 case 278: /* raisetype ::= FAIL */
164193{yymsp[0].minor.yy64 = OE_Fail;}
164194 break;
164195 case 279: /* cmd ::= DROP TRIGGER ifexists fullname */
164196{
164197 sqlite3DropTrigger(pParse,pName: yymsp[0].minor.yy607,noErr: yymsp[-1].minor.yy64);
164198}
164199 break;
164200 case 280: /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
164201{
164202 sqlite3Attach(pParse, p: yymsp[-3].minor.yy626, pDbname: yymsp[-1].minor.yy626, pKey: yymsp[0].minor.yy626);
164203}
164204 break;
164205 case 281: /* cmd ::= DETACH database_kw_opt expr */
164206{
164207 sqlite3Detach(pParse, pDbname: yymsp[0].minor.yy626);
164208}
164209 break;
164210 case 284: /* cmd ::= REINDEX */
164211{sqlite3Reindex(pParse, pName1: 0, pName2: 0);}
164212 break;
164213 case 285: /* cmd ::= REINDEX nm dbnm */
164214{sqlite3Reindex(pParse, pName1: &yymsp[-1].minor.yy0, pName2: &yymsp[0].minor.yy0);}
164215 break;
164216 case 286: /* cmd ::= ANALYZE */
164217{sqlite3Analyze(pParse, pName1: 0, pName2: 0);}
164218 break;
164219 case 287: /* cmd ::= ANALYZE nm dbnm */
164220{sqlite3Analyze(pParse, pName1: &yymsp[-1].minor.yy0, pName2: &yymsp[0].minor.yy0);}
164221 break;
164222 case 288: /* cmd ::= ALTER TABLE fullname RENAME TO nm */
164223{
164224 sqlite3AlterRenameTable(pParse,pSrc: yymsp[-3].minor.yy607,pName: &yymsp[0].minor.yy0);
164225}
164226 break;
164227 case 289: /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
164228{
164229 yymsp[-1].minor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-1].minor.yy0.z) + pParse->sLastToken.n;
164230 sqlite3AlterFinishAddColumn(pParse, pColDef: &yymsp[-1].minor.yy0);
164231}
164232 break;
164233 case 290: /* cmd ::= ALTER TABLE fullname DROP kwcolumn_opt nm */
164234{
164235 sqlite3AlterDropColumn(pParse, pSrc: yymsp[-3].minor.yy607, pName: &yymsp[0].minor.yy0);
164236}
164237 break;
164238 case 291: /* add_column_fullname ::= fullname */
164239{
164240 disableLookaside(pParse);
164241 sqlite3AlterBeginAddColumn(pParse, pSrc: yymsp[0].minor.yy607);
164242}
164243 break;
164244 case 292: /* cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
164245{
164246 sqlite3AlterRenameColumn(pParse, pSrc: yymsp[-5].minor.yy607, pOld: &yymsp[-2].minor.yy0, pNew: &yymsp[0].minor.yy0);
164247}
164248 break;
164249 case 293: /* cmd ::= create_vtab */
164250{sqlite3VtabFinishParse(pParse,pEnd: 0);}
164251 break;
164252 case 294: /* cmd ::= create_vtab LP vtabarglist RP */
164253{sqlite3VtabFinishParse(pParse,pEnd: &yymsp[0].minor.yy0);}
164254 break;
164255 case 295: /* create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
164256{
164257 sqlite3VtabBeginParse(pParse, pName1: &yymsp[-3].minor.yy0, pName2: &yymsp[-2].minor.yy0, pModuleName: &yymsp[0].minor.yy0, ifNotExists: yymsp[-4].minor.yy64);
164258}
164259 break;
164260 case 296: /* vtabarg ::= */
164261{sqlite3VtabArgInit(pParse);}
164262 break;
164263 case 297: /* vtabargtoken ::= ANY */
164264 case 298: /* vtabargtoken ::= lp anylist RP */ yytestcase(yyruleno==298);
164265 case 299: /* lp ::= LP */ yytestcase(yyruleno==299);
164266{sqlite3VtabArgExtend(pParse,p: &yymsp[0].minor.yy0);}
164267 break;
164268 case 300: /* with ::= WITH wqlist */
164269 case 301: /* with ::= WITH RECURSIVE wqlist */ yytestcase(yyruleno==301);
164270{ sqlite3WithPush(pParse, pWith: yymsp[0].minor.yy43, bFree: 1); }
164271 break;
164272 case 302: /* wqas ::= AS */
164273{yymsp[0].minor.yy534 = M10d_Any;}
164274 break;
164275 case 303: /* wqas ::= AS MATERIALIZED */
164276{yymsp[-1].minor.yy534 = M10d_Yes;}
164277 break;
164278 case 304: /* wqas ::= AS NOT MATERIALIZED */
164279{yymsp[-2].minor.yy534 = M10d_No;}
164280 break;
164281 case 305: /* wqitem ::= nm eidlist_opt wqas LP select RP */
164282{
164283 yymsp[-5].minor.yy255 = sqlite3CteNew(pParse, pName: &yymsp[-5].minor.yy0, pArglist: yymsp[-4].minor.yy562, pQuery: yymsp[-1].minor.yy303, eM10d: yymsp[-3].minor.yy534); /*A-overwrites-X*/
164284}
164285 break;
164286 case 306: /* wqlist ::= wqitem */
164287{
164288 yymsp[0].minor.yy43 = sqlite3WithAdd(pParse, pWith: 0, pCte: yymsp[0].minor.yy255); /*A-overwrites-X*/
164289}
164290 break;
164291 case 307: /* wqlist ::= wqlist COMMA wqitem */
164292{
164293 yymsp[-2].minor.yy43 = sqlite3WithAdd(pParse, pWith: yymsp[-2].minor.yy43, pCte: yymsp[0].minor.yy255);
164294}
164295 break;
164296 case 308: /* windowdefn_list ::= windowdefn */
164297{ yylhsminor.yy375 = yymsp[0].minor.yy375; }
164298 yymsp[0].minor.yy375 = yylhsminor.yy375;
164299 break;
164300 case 309: /* windowdefn_list ::= windowdefn_list COMMA windowdefn */
164301{
164302 assert( yymsp[0].minor.yy375!=0 );
164303 sqlite3WindowChain(pParse, pWin: yymsp[0].minor.yy375, pList: yymsp[-2].minor.yy375);
164304 yymsp[0].minor.yy375->pNextWin = yymsp[-2].minor.yy375;
164305 yylhsminor.yy375 = yymsp[0].minor.yy375;
164306}
164307 yymsp[-2].minor.yy375 = yylhsminor.yy375;
164308 break;
164309 case 310: /* windowdefn ::= nm AS LP window RP */
164310{
164311 if( ALWAYS(yymsp[-1].minor.yy375) ){
164312 yymsp[-1].minor.yy375->zName = sqlite3DbStrNDup(db: pParse->db, z: yymsp[-4].minor.yy0.z, n: yymsp[-4].minor.yy0.n);
164313 }
164314 yylhsminor.yy375 = yymsp[-1].minor.yy375;
164315}
164316 yymsp[-4].minor.yy375 = yylhsminor.yy375;
164317 break;
164318 case 311: /* window ::= PARTITION BY nexprlist orderby_opt frame_opt */
164319{
164320 yymsp[-4].minor.yy375 = sqlite3WindowAssemble(pParse, pWin: yymsp[0].minor.yy375, pPartition: yymsp[-2].minor.yy562, pOrderBy: yymsp[-1].minor.yy562, pBase: 0);
164321}
164322 break;
164323 case 312: /* window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
164324{
164325 yylhsminor.yy375 = sqlite3WindowAssemble(pParse, pWin: yymsp[0].minor.yy375, pPartition: yymsp[-2].minor.yy562, pOrderBy: yymsp[-1].minor.yy562, pBase: &yymsp[-5].minor.yy0);
164326}
164327 yymsp[-5].minor.yy375 = yylhsminor.yy375;
164328 break;
164329 case 313: /* window ::= ORDER BY sortlist frame_opt */
164330{
164331 yymsp[-3].minor.yy375 = sqlite3WindowAssemble(pParse, pWin: yymsp[0].minor.yy375, pPartition: 0, pOrderBy: yymsp[-1].minor.yy562, pBase: 0);
164332}
164333 break;
164334 case 314: /* window ::= nm ORDER BY sortlist frame_opt */
164335{
164336 yylhsminor.yy375 = sqlite3WindowAssemble(pParse, pWin: yymsp[0].minor.yy375, pPartition: 0, pOrderBy: yymsp[-1].minor.yy562, pBase: &yymsp[-4].minor.yy0);
164337}
164338 yymsp[-4].minor.yy375 = yylhsminor.yy375;
164339 break;
164340 case 315: /* window ::= frame_opt */
164341 case 334: /* filter_over ::= over_clause */ yytestcase(yyruleno==334);
164342{
164343 yylhsminor.yy375 = yymsp[0].minor.yy375;
164344}
164345 yymsp[0].minor.yy375 = yylhsminor.yy375;
164346 break;
164347 case 316: /* window ::= nm frame_opt */
164348{
164349 yylhsminor.yy375 = sqlite3WindowAssemble(pParse, pWin: yymsp[0].minor.yy375, pPartition: 0, pOrderBy: 0, pBase: &yymsp[-1].minor.yy0);
164350}
164351 yymsp[-1].minor.yy375 = yylhsminor.yy375;
164352 break;
164353 case 317: /* frame_opt ::= */
164354{
164355 yymsp[1].minor.yy375 = sqlite3WindowAlloc(pParse, eType: 0, TK_UNBOUNDED, pStart: 0, TK_CURRENT, pEnd: 0, eExclude: 0);
164356}
164357 break;
164358 case 318: /* frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
164359{
164360 yylhsminor.yy375 = sqlite3WindowAlloc(pParse, eType: yymsp[-2].minor.yy64, eStart: yymsp[-1].minor.yy81.eType, pStart: yymsp[-1].minor.yy81.pExpr, TK_CURRENT, pEnd: 0, eExclude: yymsp[0].minor.yy534);
164361}
164362 yymsp[-2].minor.yy375 = yylhsminor.yy375;
164363 break;
164364 case 319: /* frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt */
164365{
164366 yylhsminor.yy375 = sqlite3WindowAlloc(pParse, eType: yymsp[-5].minor.yy64, eStart: yymsp[-3].minor.yy81.eType, pStart: yymsp[-3].minor.yy81.pExpr, eEnd: yymsp[-1].minor.yy81.eType, pEnd: yymsp[-1].minor.yy81.pExpr, eExclude: yymsp[0].minor.yy534);
164367}
164368 yymsp[-5].minor.yy375 = yylhsminor.yy375;
164369 break;
164370 case 321: /* frame_bound_s ::= frame_bound */
164371 case 323: /* frame_bound_e ::= frame_bound */ yytestcase(yyruleno==323);
164372{yylhsminor.yy81 = yymsp[0].minor.yy81;}
164373 yymsp[0].minor.yy81 = yylhsminor.yy81;
164374 break;
164375 case 322: /* frame_bound_s ::= UNBOUNDED PRECEDING */
164376 case 324: /* frame_bound_e ::= UNBOUNDED FOLLOWING */ yytestcase(yyruleno==324);
164377 case 326: /* frame_bound ::= CURRENT ROW */ yytestcase(yyruleno==326);
164378{yylhsminor.yy81.eType = yymsp[-1].major; yylhsminor.yy81.pExpr = 0;}
164379 yymsp[-1].minor.yy81 = yylhsminor.yy81;
164380 break;
164381 case 325: /* frame_bound ::= expr PRECEDING|FOLLOWING */
164382{yylhsminor.yy81.eType = yymsp[0].major; yylhsminor.yy81.pExpr = yymsp[-1].minor.yy626;}
164383 yymsp[-1].minor.yy81 = yylhsminor.yy81;
164384 break;
164385 case 327: /* frame_exclude_opt ::= */
164386{yymsp[1].minor.yy534 = 0;}
164387 break;
164388 case 328: /* frame_exclude_opt ::= EXCLUDE frame_exclude */
164389{yymsp[-1].minor.yy534 = yymsp[0].minor.yy534;}
164390 break;
164391 case 329: /* frame_exclude ::= NO OTHERS */
164392 case 330: /* frame_exclude ::= CURRENT ROW */ yytestcase(yyruleno==330);
164393{yymsp[-1].minor.yy534 = yymsp[-1].major; /*A-overwrites-X*/}
164394 break;
164395 case 331: /* frame_exclude ::= GROUP|TIES */
164396{yymsp[0].minor.yy534 = yymsp[0].major; /*A-overwrites-X*/}
164397 break;
164398 case 332: /* window_clause ::= WINDOW windowdefn_list */
164399{ yymsp[-1].minor.yy375 = yymsp[0].minor.yy375; }
164400 break;
164401 case 333: /* filter_over ::= filter_clause over_clause */
164402{
164403 if( yymsp[0].minor.yy375 ){
164404 yymsp[0].minor.yy375->pFilter = yymsp[-1].minor.yy626;
164405 }else{
164406 sqlite3ExprDelete(db: pParse->db, p: yymsp[-1].minor.yy626);
164407 }
164408 yylhsminor.yy375 = yymsp[0].minor.yy375;
164409}
164410 yymsp[-1].minor.yy375 = yylhsminor.yy375;
164411 break;
164412 case 335: /* filter_over ::= filter_clause */
164413{
164414 yylhsminor.yy375 = (Window*)sqlite3DbMallocZero(db: pParse->db, n: sizeof(Window));
164415 if( yylhsminor.yy375 ){
164416 yylhsminor.yy375->eFrmType = TK_FILTER;
164417 yylhsminor.yy375->pFilter = yymsp[0].minor.yy626;
164418 }else{
164419 sqlite3ExprDelete(db: pParse->db, p: yymsp[0].minor.yy626);
164420 }
164421}
164422 yymsp[0].minor.yy375 = yylhsminor.yy375;
164423 break;
164424 case 336: /* over_clause ::= OVER LP window RP */
164425{
164426 yymsp[-3].minor.yy375 = yymsp[-1].minor.yy375;
164427 assert( yymsp[-3].minor.yy375!=0 );
164428}
164429 break;
164430 case 337: /* over_clause ::= OVER nm */
164431{
164432 yymsp[-1].minor.yy375 = (Window*)sqlite3DbMallocZero(db: pParse->db, n: sizeof(Window));
164433 if( yymsp[-1].minor.yy375 ){
164434 yymsp[-1].minor.yy375->zName = sqlite3DbStrNDup(db: pParse->db, z: yymsp[0].minor.yy0.z, n: yymsp[0].minor.yy0.n);
164435 }
164436}
164437 break;
164438 case 338: /* filter_clause ::= FILTER LP WHERE expr RP */
164439{ yymsp[-4].minor.yy626 = yymsp[-1].minor.yy626; }
164440 break;
164441 default:
164442 /* (339) input ::= cmdlist */ yytestcase(yyruleno==339);
164443 /* (340) cmdlist ::= cmdlist ecmd */ yytestcase(yyruleno==340);
164444 /* (341) cmdlist ::= ecmd (OPTIMIZED OUT) */ assert(yyruleno!=341);
164445 /* (342) ecmd ::= SEMI */ yytestcase(yyruleno==342);
164446 /* (343) ecmd ::= cmdx SEMI */ yytestcase(yyruleno==343);
164447 /* (344) ecmd ::= explain cmdx SEMI (NEVER REDUCES) */ assert(yyruleno!=344);
164448 /* (345) trans_opt ::= */ yytestcase(yyruleno==345);
164449 /* (346) trans_opt ::= TRANSACTION */ yytestcase(yyruleno==346);
164450 /* (347) trans_opt ::= TRANSACTION nm */ yytestcase(yyruleno==347);
164451 /* (348) savepoint_opt ::= SAVEPOINT */ yytestcase(yyruleno==348);
164452 /* (349) savepoint_opt ::= */ yytestcase(yyruleno==349);
164453 /* (350) cmd ::= create_table create_table_args */ yytestcase(yyruleno==350);
164454 /* (351) table_option_set ::= table_option (OPTIMIZED OUT) */ assert(yyruleno!=351);
164455 /* (352) columnlist ::= columnlist COMMA columnname carglist */ yytestcase(yyruleno==352);
164456 /* (353) columnlist ::= columnname carglist */ yytestcase(yyruleno==353);
164457 /* (354) nm ::= ID|INDEXED */ yytestcase(yyruleno==354);
164458 /* (355) nm ::= STRING */ yytestcase(yyruleno==355);
164459 /* (356) nm ::= JOIN_KW */ yytestcase(yyruleno==356);
164460 /* (357) typetoken ::= typename */ yytestcase(yyruleno==357);
164461 /* (358) typename ::= ID|STRING */ yytestcase(yyruleno==358);
164462 /* (359) signed ::= plus_num (OPTIMIZED OUT) */ assert(yyruleno!=359);
164463 /* (360) signed ::= minus_num (OPTIMIZED OUT) */ assert(yyruleno!=360);
164464 /* (361) carglist ::= carglist ccons */ yytestcase(yyruleno==361);
164465 /* (362) carglist ::= */ yytestcase(yyruleno==362);
164466 /* (363) ccons ::= NULL onconf */ yytestcase(yyruleno==363);
164467 /* (364) ccons ::= GENERATED ALWAYS AS generated */ yytestcase(yyruleno==364);
164468 /* (365) ccons ::= AS generated */ yytestcase(yyruleno==365);
164469 /* (366) conslist_opt ::= COMMA conslist */ yytestcase(yyruleno==366);
164470 /* (367) conslist ::= conslist tconscomma tcons */ yytestcase(yyruleno==367);
164471 /* (368) conslist ::= tcons (OPTIMIZED OUT) */ assert(yyruleno!=368);
164472 /* (369) tconscomma ::= */ yytestcase(yyruleno==369);
164473 /* (370) defer_subclause_opt ::= defer_subclause (OPTIMIZED OUT) */ assert(yyruleno!=370);
164474 /* (371) resolvetype ::= raisetype (OPTIMIZED OUT) */ assert(yyruleno!=371);
164475 /* (372) selectnowith ::= oneselect (OPTIMIZED OUT) */ assert(yyruleno!=372);
164476 /* (373) oneselect ::= values */ yytestcase(yyruleno==373);
164477 /* (374) sclp ::= selcollist COMMA */ yytestcase(yyruleno==374);
164478 /* (375) as ::= ID|STRING */ yytestcase(yyruleno==375);
164479 /* (376) returning ::= */ yytestcase(yyruleno==376);
164480 /* (377) expr ::= term (OPTIMIZED OUT) */ assert(yyruleno!=377);
164481 /* (378) likeop ::= LIKE_KW|MATCH */ yytestcase(yyruleno==378);
164482 /* (379) exprlist ::= nexprlist */ yytestcase(yyruleno==379);
164483 /* (380) nmnum ::= plus_num (OPTIMIZED OUT) */ assert(yyruleno!=380);
164484 /* (381) nmnum ::= nm (OPTIMIZED OUT) */ assert(yyruleno!=381);
164485 /* (382) nmnum ::= ON */ yytestcase(yyruleno==382);
164486 /* (383) nmnum ::= DELETE */ yytestcase(yyruleno==383);
164487 /* (384) nmnum ::= DEFAULT */ yytestcase(yyruleno==384);
164488 /* (385) plus_num ::= INTEGER|FLOAT */ yytestcase(yyruleno==385);
164489 /* (386) foreach_clause ::= */ yytestcase(yyruleno==386);
164490 /* (387) foreach_clause ::= FOR EACH ROW */ yytestcase(yyruleno==387);
164491 /* (388) trnm ::= nm */ yytestcase(yyruleno==388);
164492 /* (389) tridxby ::= */ yytestcase(yyruleno==389);
164493 /* (390) database_kw_opt ::= DATABASE */ yytestcase(yyruleno==390);
164494 /* (391) database_kw_opt ::= */ yytestcase(yyruleno==391);
164495 /* (392) kwcolumn_opt ::= */ yytestcase(yyruleno==392);
164496 /* (393) kwcolumn_opt ::= COLUMNKW */ yytestcase(yyruleno==393);
164497 /* (394) vtabarglist ::= vtabarg */ yytestcase(yyruleno==394);
164498 /* (395) vtabarglist ::= vtabarglist COMMA vtabarg */ yytestcase(yyruleno==395);
164499 /* (396) vtabarg ::= vtabarg vtabargtoken */ yytestcase(yyruleno==396);
164500 /* (397) anylist ::= */ yytestcase(yyruleno==397);
164501 /* (398) anylist ::= anylist LP anylist RP */ yytestcase(yyruleno==398);
164502 /* (399) anylist ::= anylist ANY */ yytestcase(yyruleno==399);
164503 /* (400) with ::= */ yytestcase(yyruleno==400);
164504 break;
164505/********** End reduce actions ************************************************/
164506 };
164507 assert( yyruleno<sizeof(yyRuleInfoLhs)/sizeof(yyRuleInfoLhs[0]) );
164508 yygoto = yyRuleInfoLhs[yyruleno];
164509 yysize = yyRuleInfoNRhs[yyruleno];
164510 yyact = yy_find_reduce_action(stateno: yymsp[yysize].stateno,iLookAhead: (YYCODETYPE)yygoto);
164511
164512 /* There are no SHIFTREDUCE actions on nonterminals because the table
164513 ** generator has simplified them to pure REDUCE actions. */
164514 assert( !(yyact>YY_MAX_SHIFT && yyact<=YY_MAX_SHIFTREDUCE) );
164515
164516 /* It is not possible for a REDUCE to be followed by an error */
164517 assert( yyact!=YY_ERROR_ACTION );
164518
164519 yymsp += yysize+1;
164520 yypParser->yytos = yymsp;
164521 yymsp->stateno = (YYACTIONTYPE)yyact;
164522 yymsp->major = (YYCODETYPE)yygoto;
164523 yyTraceShift(yypParser, yyact, "... then shift");
164524 return yyact;
164525}
164526
164527/*
164528** The following code executes when the parse fails
164529*/
164530#ifndef YYNOERRORRECOVERY
164531static void yy_parse_failed(
164532 yyParser *yypParser /* The parser */
164533){
164534 sqlite3ParserARG_FETCH
164535 sqlite3ParserCTX_FETCH
164536#ifndef NDEBUG
164537 if( yyTraceFILE ){
164538 fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
164539 }
164540#endif
164541 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
164542 /* Here code is inserted which will be executed whenever the
164543 ** parser fails */
164544/************ Begin %parse_failure code ***************************************/
164545/************ End %parse_failure code *****************************************/
164546 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
164547 sqlite3ParserCTX_STORE
164548}
164549#endif /* YYNOERRORRECOVERY */
164550
164551/*
164552** The following code executes when a syntax error first occurs.
164553*/
164554static void yy_syntax_error(
164555 yyParser *yypParser, /* The parser */
164556 int yymajor, /* The major type of the error token */
164557 sqlite3ParserTOKENTYPE yyminor /* The minor type of the error token */
164558){
164559 sqlite3ParserARG_FETCH
164560 sqlite3ParserCTX_FETCH
164561#define TOKEN yyminor
164562/************ Begin %syntax_error code ****************************************/
164563
164564 UNUSED_PARAMETER(yymajor); /* Silence some compiler warnings */
164565 if( TOKEN.z[0] ){
164566 sqlite3ErrorMsg(pParse, zFormat: "near \"%T\": syntax error", &TOKEN);
164567 }else{
164568 sqlite3ErrorMsg(pParse, zFormat: "incomplete input");
164569 }
164570/************ End %syntax_error code ******************************************/
164571 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
164572 sqlite3ParserCTX_STORE
164573}
164574
164575/*
164576** The following is executed when the parser accepts
164577*/
164578static void yy_accept(
164579 yyParser *yypParser /* The parser */
164580){
164581 sqlite3ParserARG_FETCH
164582 sqlite3ParserCTX_FETCH
164583#ifndef NDEBUG
164584 if( yyTraceFILE ){
164585 fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
164586 }
164587#endif
164588#ifndef YYNOERRORRECOVERY
164589 yypParser->yyerrcnt = -1;
164590#endif
164591 assert( yypParser->yytos==yypParser->yystack );
164592 /* Here code is inserted which will be executed whenever the
164593 ** parser accepts */
164594/*********** Begin %parse_accept code *****************************************/
164595/*********** End %parse_accept code *******************************************/
164596 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
164597 sqlite3ParserCTX_STORE
164598}
164599
164600/* The main parser program.
164601** The first argument is a pointer to a structure obtained from
164602** "sqlite3ParserAlloc" which describes the current state of the parser.
164603** The second argument is the major token number. The third is
164604** the minor token. The fourth optional argument is whatever the
164605** user wants (and specified in the grammar) and is available for
164606** use by the action routines.
164607**
164608** Inputs:
164609** <ul>
164610** <li> A pointer to the parser (an opaque structure.)
164611** <li> The major token number.
164612** <li> The minor token number.
164613** <li> An option argument of a grammar-specified type.
164614** </ul>
164615**
164616** Outputs:
164617** None.
164618*/
164619SQLITE_PRIVATE void sqlite3Parser(
164620 void *yyp, /* The parser */
164621 int yymajor, /* The major token code number */
164622 sqlite3ParserTOKENTYPE yyminor /* The value for the token */
164623 sqlite3ParserARG_PDECL /* Optional %extra_argument parameter */
164624){
164625 YYMINORTYPE yyminorunion;
164626 YYACTIONTYPE yyact; /* The parser action. */
164627#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
164628 int yyendofinput; /* True if we are at the end of input */
164629#endif
164630#ifdef YYERRORSYMBOL
164631 int yyerrorhit = 0; /* True if yymajor has invoked an error */
164632#endif
164633 yyParser *yypParser = (yyParser*)yyp; /* The parser */
164634 sqlite3ParserCTX_FETCH
164635 sqlite3ParserARG_STORE
164636
164637 assert( yypParser->yytos!=0 );
164638#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
164639 yyendofinput = (yymajor==0);
164640#endif
164641
164642 yyact = yypParser->yytos->stateno;
164643#ifndef NDEBUG
164644 if( yyTraceFILE ){
164645 if( yyact < YY_MIN_REDUCE ){
164646 fprintf(yyTraceFILE,"%sInput '%s' in state %d\n",
164647 yyTracePrompt,yyTokenName[yymajor],yyact);
164648 }else{
164649 fprintf(yyTraceFILE,"%sInput '%s' with pending reduce %d\n",
164650 yyTracePrompt,yyTokenName[yymajor],yyact-YY_MIN_REDUCE);
164651 }
164652 }
164653#endif
164654
164655 while(1){ /* Exit by "break" */
164656 assert( yypParser->yytos>=yypParser->yystack );
164657 assert( yyact==yypParser->yytos->stateno );
164658 yyact = yy_find_shift_action(iLookAhead: (YYCODETYPE)yymajor,stateno: yyact);
164659 if( yyact >= YY_MIN_REDUCE ){
164660 unsigned int yyruleno = yyact - YY_MIN_REDUCE; /* Reduce by this rule */
164661#ifndef NDEBUG
164662 assert( yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) );
164663 if( yyTraceFILE ){
164664 int yysize = yyRuleInfoNRhs[yyruleno];
164665 if( yysize ){
164666 fprintf(yyTraceFILE, "%sReduce %d [%s]%s, pop back to state %d.\n",
164667 yyTracePrompt,
164668 yyruleno, yyRuleName[yyruleno],
164669 yyruleno<YYNRULE_WITH_ACTION ? "" : " without external action",
164670 yypParser->yytos[yysize].stateno);
164671 }else{
164672 fprintf(yyTraceFILE, "%sReduce %d [%s]%s.\n",
164673 yyTracePrompt, yyruleno, yyRuleName[yyruleno],
164674 yyruleno<YYNRULE_WITH_ACTION ? "" : " without external action");
164675 }
164676 }
164677#endif /* NDEBUG */
164678
164679 /* Check that the stack is large enough to grow by a single entry
164680 ** if the RHS of the rule is empty. This ensures that there is room
164681 ** enough on the stack to push the LHS value */
164682 if( yyRuleInfoNRhs[yyruleno]==0 ){
164683#ifdef YYTRACKMAXSTACKDEPTH
164684 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
164685 yypParser->yyhwm++;
164686 assert( yypParser->yyhwm ==
164687 (int)(yypParser->yytos - yypParser->yystack));
164688 }
164689#endif
164690#if YYSTACKDEPTH>0
164691 if( yypParser->yytos>=yypParser->yystackEnd ){
164692 yyStackOverflow(yypParser);
164693 break;
164694 }
164695#else
164696 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz-1] ){
164697 if( yyGrowStack(yypParser) ){
164698 yyStackOverflow(yypParser);
164699 break;
164700 }
164701 }
164702#endif
164703 }
164704 yyact = yy_reduce(yypParser,yyruleno,yyLookahead: yymajor,yyLookaheadToken: yyminor sqlite3ParserCTX_PARAM);
164705 }else if( yyact <= YY_MAX_SHIFTREDUCE ){
164706 yy_shift(yypParser,yyNewState: yyact,yyMajor: (YYCODETYPE)yymajor,yyMinor: yyminor);
164707#ifndef YYNOERRORRECOVERY
164708 yypParser->yyerrcnt--;
164709#endif
164710 break;
164711 }else if( yyact==YY_ACCEPT_ACTION ){
164712 yypParser->yytos--;
164713 yy_accept(yypParser);
164714 return;
164715 }else{
164716 assert( yyact == YY_ERROR_ACTION );
164717 yyminorunion.yy0 = yyminor;
164718#ifdef YYERRORSYMBOL
164719 int yymx;
164720#endif
164721#ifndef NDEBUG
164722 if( yyTraceFILE ){
164723 fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
164724 }
164725#endif
164726#ifdef YYERRORSYMBOL
164727 /* A syntax error has occurred.
164728 ** The response to an error depends upon whether or not the
164729 ** grammar defines an error token "ERROR".
164730 **
164731 ** This is what we do if the grammar does define ERROR:
164732 **
164733 ** * Call the %syntax_error function.
164734 **
164735 ** * Begin popping the stack until we enter a state where
164736 ** it is legal to shift the error symbol, then shift
164737 ** the error symbol.
164738 **
164739 ** * Set the error count to three.
164740 **
164741 ** * Begin accepting and shifting new tokens. No new error
164742 ** processing will occur until three tokens have been
164743 ** shifted successfully.
164744 **
164745 */
164746 if( yypParser->yyerrcnt<0 ){
164747 yy_syntax_error(yypParser,yymajor,yyminor);
164748 }
164749 yymx = yypParser->yytos->major;
164750 if( yymx==YYERRORSYMBOL || yyerrorhit ){
164751#ifndef NDEBUG
164752 if( yyTraceFILE ){
164753 fprintf(yyTraceFILE,"%sDiscard input token %s\n",
164754 yyTracePrompt,yyTokenName[yymajor]);
164755 }
164756#endif
164757 yy_destructor(yypParser, (YYCODETYPE)yymajor, &yyminorunion);
164758 yymajor = YYNOCODE;
164759 }else{
164760 while( yypParser->yytos > yypParser->yystack ){
164761 yyact = yy_find_reduce_action(yypParser->yytos->stateno,
164762 YYERRORSYMBOL);
164763 if( yyact<=YY_MAX_SHIFTREDUCE ) break;
164764 yy_pop_parser_stack(yypParser);
164765 }
164766 if( yypParser->yytos <= yypParser->yystack || yymajor==0 ){
164767 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
164768 yy_parse_failed(yypParser);
164769#ifndef YYNOERRORRECOVERY
164770 yypParser->yyerrcnt = -1;
164771#endif
164772 yymajor = YYNOCODE;
164773 }else if( yymx!=YYERRORSYMBOL ){
164774 yy_shift(yypParser,yyact,YYERRORSYMBOL,yyminor);
164775 }
164776 }
164777 yypParser->yyerrcnt = 3;
164778 yyerrorhit = 1;
164779 if( yymajor==YYNOCODE ) break;
164780 yyact = yypParser->yytos->stateno;
164781#elif defined(YYNOERRORRECOVERY)
164782 /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
164783 ** do any kind of error recovery. Instead, simply invoke the syntax
164784 ** error routine and continue going as if nothing had happened.
164785 **
164786 ** Applications can set this macro (for example inside %include) if
164787 ** they intend to abandon the parse upon the first syntax error seen.
164788 */
164789 yy_syntax_error(yypParser,yymajor, yyminor);
164790 yy_destructor(yypParser,yymajor: (YYCODETYPE)yymajor,yypminor: &yyminorunion);
164791 break;
164792#else /* YYERRORSYMBOL is not defined */
164793 /* This is what we do if the grammar does not define ERROR:
164794 **
164795 ** * Report an error message, and throw away the input token.
164796 **
164797 ** * If the input token is $, then fail the parse.
164798 **
164799 ** As before, subsequent error messages are suppressed until
164800 ** three input tokens have been successfully shifted.
164801 */
164802 if( yypParser->yyerrcnt<=0 ){
164803 yy_syntax_error(yypParser,yymajor, yyminor);
164804 }
164805 yypParser->yyerrcnt = 3;
164806 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
164807 if( yyendofinput ){
164808 yy_parse_failed(yypParser);
164809#ifndef YYNOERRORRECOVERY
164810 yypParser->yyerrcnt = -1;
164811#endif
164812 }
164813 break;
164814#endif
164815 }
164816 }
164817#ifndef NDEBUG
164818 if( yyTraceFILE ){
164819 yyStackEntry *i;
164820 char cDiv = '[';
164821 fprintf(yyTraceFILE,"%sReturn. Stack=",yyTracePrompt);
164822 for(i=&yypParser->yystack[1]; i<=yypParser->yytos; i++){
164823 fprintf(yyTraceFILE,"%c%s", cDiv, yyTokenName[i->major]);
164824 cDiv = ' ';
164825 }
164826 fprintf(yyTraceFILE,"]\n");
164827 }
164828#endif
164829 return;
164830}
164831
164832/*
164833** Return the fallback token corresponding to canonical token iToken, or
164834** 0 if iToken has no fallback.
164835*/
164836SQLITE_PRIVATE int sqlite3ParserFallback(int iToken){
164837#ifdef YYFALLBACK
164838 assert( iToken<(int)(sizeof(yyFallback)/sizeof(yyFallback[0])) );
164839 return yyFallback[iToken];
164840#else
164841 (void)iToken;
164842 return 0;
164843#endif
164844}
164845
164846/************** End of parse.c ***********************************************/
164847/************** Begin file tokenize.c ****************************************/
164848/*
164849** 2001 September 15
164850**
164851** The author disclaims copyright to this source code. In place of
164852** a legal notice, here is a blessing:
164853**
164854** May you do good and not evil.
164855** May you find forgiveness for yourself and forgive others.
164856** May you share freely, never taking more than you give.
164857**
164858*************************************************************************
164859** An tokenizer for SQL
164860**
164861** This file contains C code that splits an SQL input string up into
164862** individual tokens and sends those tokens one-by-one over to the
164863** parser for analysis.
164864*/
164865/* #include "sqliteInt.h" */
164866/* #include <stdlib.h> */
164867
164868/* Character classes for tokenizing
164869**
164870** In the sqlite3GetToken() function, a switch() on aiClass[c] is implemented
164871** using a lookup table, whereas a switch() directly on c uses a binary search.
164872** The lookup table is much faster. To maximize speed, and to ensure that
164873** a lookup table is used, all of the classes need to be small integers and
164874** all of them need to be used within the switch.
164875*/
164876#define CC_X 0 /* The letter 'x', or start of BLOB literal */
164877#define CC_KYWD0 1 /* First letter of a keyword */
164878#define CC_KYWD 2 /* Alphabetics or '_'. Usable in a keyword */
164879#define CC_DIGIT 3 /* Digits */
164880#define CC_DOLLAR 4 /* '$' */
164881#define CC_VARALPHA 5 /* '@', '#', ':'. Alphabetic SQL variables */
164882#define CC_VARNUM 6 /* '?'. Numeric SQL variables */
164883#define CC_SPACE 7 /* Space characters */
164884#define CC_QUOTE 8 /* '"', '\'', or '`'. String literals, quoted ids */
164885#define CC_QUOTE2 9 /* '['. [...] style quoted ids */
164886#define CC_PIPE 10 /* '|'. Bitwise OR or concatenate */
164887#define CC_MINUS 11 /* '-'. Minus or SQL-style comment */
164888#define CC_LT 12 /* '<'. Part of < or <= or <> */
164889#define CC_GT 13 /* '>'. Part of > or >= */
164890#define CC_EQ 14 /* '='. Part of = or == */
164891#define CC_BANG 15 /* '!'. Part of != */
164892#define CC_SLASH 16 /* '/'. / or c-style comment */
164893#define CC_LP 17 /* '(' */
164894#define CC_RP 18 /* ')' */
164895#define CC_SEMI 19 /* ';' */
164896#define CC_PLUS 20 /* '+' */
164897#define CC_STAR 21 /* '*' */
164898#define CC_PERCENT 22 /* '%' */
164899#define CC_COMMA 23 /* ',' */
164900#define CC_AND 24 /* '&' */
164901#define CC_TILDA 25 /* '~' */
164902#define CC_DOT 26 /* '.' */
164903#define CC_ID 27 /* unicode characters usable in IDs */
164904#define CC_ILLEGAL 28 /* Illegal character */
164905#define CC_NUL 29 /* 0x00 */
164906#define CC_BOM 30 /* First byte of UTF8 BOM: 0xEF 0xBB 0xBF */
164907
164908static const unsigned char aiClass[] = {
164909#ifdef SQLITE_ASCII
164910/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
164911/* 0x */ 29, 28, 28, 28, 28, 28, 28, 28, 28, 7, 7, 28, 7, 7, 28, 28,
164912/* 1x */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
164913/* 2x */ 7, 15, 8, 5, 4, 22, 24, 8, 17, 18, 21, 20, 23, 11, 26, 16,
164914/* 3x */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 19, 12, 14, 13, 6,
164915/* 4x */ 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
164916/* 5x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 2, 2, 9, 28, 28, 28, 2,
164917/* 6x */ 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
164918/* 7x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 2, 2, 28, 10, 28, 25, 28,
164919/* 8x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
164920/* 9x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
164921/* Ax */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
164922/* Bx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
164923/* Cx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
164924/* Dx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
164925/* Ex */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 30,
164926/* Fx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27
164927#endif
164928#ifdef SQLITE_EBCDIC
164929/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
164930/* 0x */ 29, 28, 28, 28, 28, 7, 28, 28, 28, 28, 28, 28, 7, 7, 28, 28,
164931/* 1x */ 28, 28, 28, 28, 28, 7, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
164932/* 2x */ 28, 28, 28, 28, 28, 7, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
164933/* 3x */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
164934/* 4x */ 7, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 26, 12, 17, 20, 10,
164935/* 5x */ 24, 28, 28, 28, 28, 28, 28, 28, 28, 28, 15, 4, 21, 18, 19, 28,
164936/* 6x */ 11, 16, 28, 28, 28, 28, 28, 28, 28, 28, 28, 23, 22, 2, 13, 6,
164937/* 7x */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 8, 5, 5, 5, 8, 14, 8,
164938/* 8x */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28,
164939/* 9x */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28,
164940/* Ax */ 28, 25, 1, 1, 1, 1, 1, 0, 2, 2, 28, 28, 28, 28, 28, 28,
164941/* Bx */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 9, 28, 28, 28, 28, 28,
164942/* Cx */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28,
164943/* Dx */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28,
164944/* Ex */ 28, 28, 1, 1, 1, 1, 1, 0, 2, 2, 28, 28, 28, 28, 28, 28,
164945/* Fx */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 28, 28, 28, 28, 28, 28,
164946#endif
164947};
164948
164949/*
164950** The charMap() macro maps alphabetic characters (only) into their
164951** lower-case ASCII equivalent. On ASCII machines, this is just
164952** an upper-to-lower case map. On EBCDIC machines we also need
164953** to adjust the encoding. The mapping is only valid for alphabetics
164954** which are the only characters for which this feature is used.
164955**
164956** Used by keywordhash.h
164957*/
164958#ifdef SQLITE_ASCII
164959# define charMap(X) sqlite3UpperToLower[(unsigned char)X]
164960#endif
164961#ifdef SQLITE_EBCDIC
164962# define charMap(X) ebcdicToAscii[(unsigned char)X]
164963const unsigned char ebcdicToAscii[] = {
164964/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
164965 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
164966 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
164967 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
164968 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */
164969 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */
164970 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */
164971 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */
164972 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */
164973 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */
164974 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */
164975 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */
164976 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
164977 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */
164978 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */
164979 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */
164980 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */
164981};
164982#endif
164983
164984/*
164985** The sqlite3KeywordCode function looks up an identifier to determine if
164986** it is a keyword. If it is a keyword, the token code of that keyword is
164987** returned. If the input is not a keyword, TK_ID is returned.
164988**
164989** The implementation of this routine was generated by a program,
164990** mkkeywordhash.c, located in the tool subdirectory of the distribution.
164991** The output of the mkkeywordhash.c program is written into a file
164992** named keywordhash.h and then included into this source file by
164993** the #include below.
164994*/
164995/************** Include keywordhash.h in the middle of tokenize.c ************/
164996/************** Begin file keywordhash.h *************************************/
164997/***** This file contains automatically generated code ******
164998**
164999** The code in this file has been automatically generated by
165000**
165001** sqlite/tool/mkkeywordhash.c
165002**
165003** The code in this file implements a function that determines whether
165004** or not a given identifier is really an SQL keyword. The same thing
165005** might be implemented more directly using a hand-written hash table.
165006** But by using this automatically generated code, the size of the code
165007** is substantially reduced. This is important for embedded applications
165008** on platforms with limited memory.
165009*/
165010/* Hash score: 231 */
165011/* zKWText[] encodes 1007 bytes of keyword text in 667 bytes */
165012/* REINDEXEDESCAPEACHECKEYBEFOREIGNOREGEXPLAINSTEADDATABASELECT */
165013/* ABLEFTHENDEFERRABLELSEXCLUDELETEMPORARYISNULLSAVEPOINTERSECT */
165014/* IESNOTNULLIKEXCEPTRANSACTIONATURALTERAISEXCLUSIVEXISTS */
165015/* CONSTRAINTOFFSETRIGGERANGENERATEDETACHAVINGLOBEGINNEREFERENCES */
165016/* UNIQUERYWITHOUTERELEASEATTACHBETWEENOTHINGROUPSCASCADEFAULT */
165017/* CASECOLLATECREATECURRENT_DATEIMMEDIATEJOINSERTMATCHPLANALYZE */
165018/* PRAGMATERIALIZEDEFERREDISTINCTUPDATEVALUESVIRTUALWAYSWHENWHERE */
165019/* CURSIVEABORTAFTERENAMEANDROPARTITIONAUTOINCREMENTCASTCOLUMN */
165020/* COMMITCONFLICTCROSSCURRENT_TIMESTAMPRECEDINGFAILASTFILTER */
165021/* EPLACEFIRSTFOLLOWINGFROMFULLIMITIFORDERESTRICTOTHERSOVER */
165022/* ETURNINGRIGHTROLLBACKROWSUNBOUNDEDUNIONUSINGVACUUMVIEWINDOWBY */
165023/* INITIALLYPRIMARY */
165024static const char zKWText[666] = {
165025 'R','E','I','N','D','E','X','E','D','E','S','C','A','P','E','A','C','H',
165026 'E','C','K','E','Y','B','E','F','O','R','E','I','G','N','O','R','E','G',
165027 'E','X','P','L','A','I','N','S','T','E','A','D','D','A','T','A','B','A',
165028 'S','E','L','E','C','T','A','B','L','E','F','T','H','E','N','D','E','F',
165029 'E','R','R','A','B','L','E','L','S','E','X','C','L','U','D','E','L','E',
165030 'T','E','M','P','O','R','A','R','Y','I','S','N','U','L','L','S','A','V',
165031 'E','P','O','I','N','T','E','R','S','E','C','T','I','E','S','N','O','T',
165032 'N','U','L','L','I','K','E','X','C','E','P','T','R','A','N','S','A','C',
165033 'T','I','O','N','A','T','U','R','A','L','T','E','R','A','I','S','E','X',
165034 'C','L','U','S','I','V','E','X','I','S','T','S','C','O','N','S','T','R',
165035 'A','I','N','T','O','F','F','S','E','T','R','I','G','G','E','R','A','N',
165036 'G','E','N','E','R','A','T','E','D','E','T','A','C','H','A','V','I','N',
165037 'G','L','O','B','E','G','I','N','N','E','R','E','F','E','R','E','N','C',
165038 'E','S','U','N','I','Q','U','E','R','Y','W','I','T','H','O','U','T','E',
165039 'R','E','L','E','A','S','E','A','T','T','A','C','H','B','E','T','W','E',
165040 'E','N','O','T','H','I','N','G','R','O','U','P','S','C','A','S','C','A',
165041 'D','E','F','A','U','L','T','C','A','S','E','C','O','L','L','A','T','E',
165042 'C','R','E','A','T','E','C','U','R','R','E','N','T','_','D','A','T','E',
165043 'I','M','M','E','D','I','A','T','E','J','O','I','N','S','E','R','T','M',
165044 'A','T','C','H','P','L','A','N','A','L','Y','Z','E','P','R','A','G','M',
165045 'A','T','E','R','I','A','L','I','Z','E','D','E','F','E','R','R','E','D',
165046 'I','S','T','I','N','C','T','U','P','D','A','T','E','V','A','L','U','E',
165047 'S','V','I','R','T','U','A','L','W','A','Y','S','W','H','E','N','W','H',
165048 'E','R','E','C','U','R','S','I','V','E','A','B','O','R','T','A','F','T',
165049 'E','R','E','N','A','M','E','A','N','D','R','O','P','A','R','T','I','T',
165050 'I','O','N','A','U','T','O','I','N','C','R','E','M','E','N','T','C','A',
165051 'S','T','C','O','L','U','M','N','C','O','M','M','I','T','C','O','N','F',
165052 'L','I','C','T','C','R','O','S','S','C','U','R','R','E','N','T','_','T',
165053 'I','M','E','S','T','A','M','P','R','E','C','E','D','I','N','G','F','A',
165054 'I','L','A','S','T','F','I','L','T','E','R','E','P','L','A','C','E','F',
165055 'I','R','S','T','F','O','L','L','O','W','I','N','G','F','R','O','M','F',
165056 'U','L','L','I','M','I','T','I','F','O','R','D','E','R','E','S','T','R',
165057 'I','C','T','O','T','H','E','R','S','O','V','E','R','E','T','U','R','N',
165058 'I','N','G','R','I','G','H','T','R','O','L','L','B','A','C','K','R','O',
165059 'W','S','U','N','B','O','U','N','D','E','D','U','N','I','O','N','U','S',
165060 'I','N','G','V','A','C','U','U','M','V','I','E','W','I','N','D','O','W',
165061 'B','Y','I','N','I','T','I','A','L','L','Y','P','R','I','M','A','R','Y',
165062};
165063/* aKWHash[i] is the hash value for the i-th keyword */
165064static const unsigned char aKWHash[127] = {
165065 84, 92, 134, 82, 105, 29, 0, 0, 94, 0, 85, 72, 0,
165066 53, 35, 86, 15, 0, 42, 97, 54, 89, 135, 19, 0, 0,
165067 140, 0, 40, 129, 0, 22, 107, 0, 9, 0, 0, 123, 80,
165068 0, 78, 6, 0, 65, 103, 147, 0, 136, 115, 0, 0, 48,
165069 0, 90, 24, 0, 17, 0, 27, 70, 23, 26, 5, 60, 142,
165070 110, 122, 0, 73, 91, 71, 145, 61, 120, 74, 0, 49, 0,
165071 11, 41, 0, 113, 0, 0, 0, 109, 10, 111, 116, 125, 14,
165072 50, 124, 0, 100, 0, 18, 121, 144, 56, 130, 139, 88, 83,
165073 37, 30, 126, 0, 0, 108, 51, 131, 128, 0, 34, 0, 0,
165074 132, 0, 98, 38, 39, 0, 20, 45, 117, 93,
165075};
165076/* aKWNext[] forms the hash collision chain. If aKWHash[i]==0
165077** then the i-th keyword has no more hash collisions. Otherwise,
165078** the next keyword with the same hash is aKWHash[i]-1. */
165079static const unsigned char aKWNext[147] = {
165080 0, 0, 0, 0, 4, 0, 43, 0, 0, 106, 114, 0, 0,
165081 0, 2, 0, 0, 143, 0, 0, 0, 13, 0, 0, 0, 0,
165082 141, 0, 0, 119, 52, 0, 0, 137, 12, 0, 0, 62, 0,
165083 138, 0, 133, 0, 0, 36, 0, 0, 28, 77, 0, 0, 0,
165084 0, 59, 0, 47, 0, 0, 0, 0, 0, 0, 0, 0, 0,
165085 0, 69, 0, 0, 0, 0, 0, 146, 3, 0, 58, 0, 1,
165086 75, 0, 0, 0, 31, 0, 0, 0, 0, 0, 127, 0, 104,
165087 0, 64, 66, 63, 0, 0, 0, 0, 0, 46, 0, 16, 8,
165088 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 81, 101, 0,
165089 112, 21, 7, 67, 0, 79, 96, 118, 0, 0, 68, 0, 0,
165090 99, 44, 0, 55, 0, 76, 0, 95, 32, 33, 57, 25, 0,
165091 102, 0, 0, 87,
165092};
165093/* aKWLen[i] is the length (in bytes) of the i-th keyword */
165094static const unsigned char aKWLen[147] = {
165095 7, 7, 5, 4, 6, 4, 5, 3, 6, 7, 3, 6, 6,
165096 7, 7, 3, 8, 2, 6, 5, 4, 4, 3, 10, 4, 7,
165097 6, 9, 4, 2, 6, 5, 9, 9, 4, 7, 3, 2, 4,
165098 4, 6, 11, 6, 2, 7, 5, 5, 9, 6, 10, 4, 6,
165099 2, 3, 7, 5, 9, 6, 6, 4, 5, 5, 10, 6, 5,
165100 7, 4, 5, 7, 6, 7, 7, 6, 5, 7, 3, 7, 4,
165101 7, 6, 12, 9, 4, 6, 5, 4, 7, 6, 12, 8, 8,
165102 2, 6, 6, 7, 6, 4, 5, 9, 5, 5, 6, 3, 4,
165103 9, 13, 2, 2, 4, 6, 6, 8, 5, 17, 12, 7, 9,
165104 4, 4, 6, 7, 5, 9, 4, 4, 5, 2, 5, 8, 6,
165105 4, 9, 5, 8, 4, 3, 9, 5, 5, 6, 4, 6, 2,
165106 2, 9, 3, 7,
165107};
165108/* aKWOffset[i] is the index into zKWText[] of the start of
165109** the text for the i-th keyword. */
165110static const unsigned short int aKWOffset[147] = {
165111 0, 2, 2, 8, 9, 14, 16, 20, 23, 25, 25, 29, 33,
165112 36, 41, 46, 48, 53, 54, 59, 62, 65, 67, 69, 78, 81,
165113 86, 90, 90, 94, 99, 101, 105, 111, 119, 123, 123, 123, 126,
165114 129, 132, 137, 142, 146, 147, 152, 156, 160, 168, 174, 181, 184,
165115 184, 187, 189, 195, 198, 206, 211, 216, 219, 222, 226, 236, 239,
165116 244, 244, 248, 252, 259, 265, 271, 277, 277, 283, 284, 288, 295,
165117 299, 306, 312, 324, 333, 335, 341, 346, 348, 355, 359, 370, 377,
165118 378, 385, 391, 397, 402, 408, 412, 415, 424, 429, 433, 439, 441,
165119 444, 453, 455, 457, 466, 470, 476, 482, 490, 495, 495, 495, 511,
165120 520, 523, 527, 532, 539, 544, 553, 557, 560, 565, 567, 571, 579,
165121 585, 588, 597, 602, 610, 610, 614, 623, 628, 633, 639, 642, 645,
165122 648, 650, 655, 659,
165123};
165124/* aKWCode[i] is the parser symbol code for the i-th keyword */
165125static const unsigned char aKWCode[147] = {
165126 TK_REINDEX, TK_INDEXED, TK_INDEX, TK_DESC, TK_ESCAPE,
165127 TK_EACH, TK_CHECK, TK_KEY, TK_BEFORE, TK_FOREIGN,
165128 TK_FOR, TK_IGNORE, TK_LIKE_KW, TK_EXPLAIN, TK_INSTEAD,
165129 TK_ADD, TK_DATABASE, TK_AS, TK_SELECT, TK_TABLE,
165130 TK_JOIN_KW, TK_THEN, TK_END, TK_DEFERRABLE, TK_ELSE,
165131 TK_EXCLUDE, TK_DELETE, TK_TEMP, TK_TEMP, TK_OR,
165132 TK_ISNULL, TK_NULLS, TK_SAVEPOINT, TK_INTERSECT, TK_TIES,
165133 TK_NOTNULL, TK_NOT, TK_NO, TK_NULL, TK_LIKE_KW,
165134 TK_EXCEPT, TK_TRANSACTION,TK_ACTION, TK_ON, TK_JOIN_KW,
165135 TK_ALTER, TK_RAISE, TK_EXCLUSIVE, TK_EXISTS, TK_CONSTRAINT,
165136 TK_INTO, TK_OFFSET, TK_OF, TK_SET, TK_TRIGGER,
165137 TK_RANGE, TK_GENERATED, TK_DETACH, TK_HAVING, TK_LIKE_KW,
165138 TK_BEGIN, TK_JOIN_KW, TK_REFERENCES, TK_UNIQUE, TK_QUERY,
165139 TK_WITHOUT, TK_WITH, TK_JOIN_KW, TK_RELEASE, TK_ATTACH,
165140 TK_BETWEEN, TK_NOTHING, TK_GROUPS, TK_GROUP, TK_CASCADE,
165141 TK_ASC, TK_DEFAULT, TK_CASE, TK_COLLATE, TK_CREATE,
165142 TK_CTIME_KW, TK_IMMEDIATE, TK_JOIN, TK_INSERT, TK_MATCH,
165143 TK_PLAN, TK_ANALYZE, TK_PRAGMA, TK_MATERIALIZED, TK_DEFERRED,
165144 TK_DISTINCT, TK_IS, TK_UPDATE, TK_VALUES, TK_VIRTUAL,
165145 TK_ALWAYS, TK_WHEN, TK_WHERE, TK_RECURSIVE, TK_ABORT,
165146 TK_AFTER, TK_RENAME, TK_AND, TK_DROP, TK_PARTITION,
165147 TK_AUTOINCR, TK_TO, TK_IN, TK_CAST, TK_COLUMNKW,
165148 TK_COMMIT, TK_CONFLICT, TK_JOIN_KW, TK_CTIME_KW, TK_CTIME_KW,
165149 TK_CURRENT, TK_PRECEDING, TK_FAIL, TK_LAST, TK_FILTER,
165150 TK_REPLACE, TK_FIRST, TK_FOLLOWING, TK_FROM, TK_JOIN_KW,
165151 TK_LIMIT, TK_IF, TK_ORDER, TK_RESTRICT, TK_OTHERS,
165152 TK_OVER, TK_RETURNING, TK_JOIN_KW, TK_ROLLBACK, TK_ROWS,
165153 TK_ROW, TK_UNBOUNDED, TK_UNION, TK_USING, TK_VACUUM,
165154 TK_VIEW, TK_WINDOW, TK_DO, TK_BY, TK_INITIALLY,
165155 TK_ALL, TK_PRIMARY,
165156};
165157/* Hash table decoded:
165158** 0: INSERT
165159** 1: IS
165160** 2: ROLLBACK TRIGGER
165161** 3: IMMEDIATE
165162** 4: PARTITION
165163** 5: TEMP
165164** 6:
165165** 7:
165166** 8: VALUES WITHOUT
165167** 9:
165168** 10: MATCH
165169** 11: NOTHING
165170** 12:
165171** 13: OF
165172** 14: TIES IGNORE
165173** 15: PLAN
165174** 16: INSTEAD INDEXED
165175** 17:
165176** 18: TRANSACTION RIGHT
165177** 19: WHEN
165178** 20: SET HAVING
165179** 21: MATERIALIZED IF
165180** 22: ROWS
165181** 23: SELECT
165182** 24:
165183** 25:
165184** 26: VACUUM SAVEPOINT
165185** 27:
165186** 28: LIKE UNION VIRTUAL REFERENCES
165187** 29: RESTRICT
165188** 30:
165189** 31: THEN REGEXP
165190** 32: TO
165191** 33:
165192** 34: BEFORE
165193** 35:
165194** 36:
165195** 37: FOLLOWING COLLATE CASCADE
165196** 38: CREATE
165197** 39:
165198** 40: CASE REINDEX
165199** 41: EACH
165200** 42:
165201** 43: QUERY
165202** 44: AND ADD
165203** 45: PRIMARY ANALYZE
165204** 46:
165205** 47: ROW ASC DETACH
165206** 48: CURRENT_TIME CURRENT_DATE
165207** 49:
165208** 50:
165209** 51: EXCLUSIVE TEMPORARY
165210** 52:
165211** 53: DEFERRED
165212** 54: DEFERRABLE
165213** 55:
165214** 56: DATABASE
165215** 57:
165216** 58: DELETE VIEW GENERATED
165217** 59: ATTACH
165218** 60: END
165219** 61: EXCLUDE
165220** 62: ESCAPE DESC
165221** 63: GLOB
165222** 64: WINDOW ELSE
165223** 65: COLUMN
165224** 66: FIRST
165225** 67:
165226** 68: GROUPS ALL
165227** 69: DISTINCT DROP KEY
165228** 70: BETWEEN
165229** 71: INITIALLY
165230** 72: BEGIN
165231** 73: FILTER CHECK ACTION
165232** 74: GROUP INDEX
165233** 75:
165234** 76: EXISTS DEFAULT
165235** 77:
165236** 78: FOR CURRENT_TIMESTAMP
165237** 79: EXCEPT
165238** 80:
165239** 81: CROSS
165240** 82:
165241** 83:
165242** 84:
165243** 85: CAST
165244** 86: FOREIGN AUTOINCREMENT
165245** 87: COMMIT
165246** 88: CURRENT AFTER ALTER
165247** 89: FULL FAIL CONFLICT
165248** 90: EXPLAIN
165249** 91: CONSTRAINT
165250** 92: FROM ALWAYS
165251** 93:
165252** 94: ABORT
165253** 95:
165254** 96: AS DO
165255** 97: REPLACE WITH RELEASE
165256** 98: BY RENAME
165257** 99: RANGE RAISE
165258** 100: OTHERS
165259** 101: USING NULLS
165260** 102: PRAGMA
165261** 103: JOIN ISNULL OFFSET
165262** 104: NOT
165263** 105: OR LAST LEFT
165264** 106: LIMIT
165265** 107:
165266** 108:
165267** 109: IN
165268** 110: INTO
165269** 111: OVER RECURSIVE
165270** 112: ORDER OUTER
165271** 113:
165272** 114: INTERSECT UNBOUNDED
165273** 115:
165274** 116:
165275** 117: RETURNING ON
165276** 118:
165277** 119: WHERE
165278** 120: NO INNER
165279** 121: NULL
165280** 122:
165281** 123: TABLE
165282** 124: NATURAL NOTNULL
165283** 125: PRECEDING
165284** 126: UPDATE UNIQUE
165285*/
165286/* Check to see if z[0..n-1] is a keyword. If it is, write the
165287** parser symbol code for that keyword into *pType. Always
165288** return the integer n (the length of the token). */
165289static int keywordCode(const char *z, int n, int *pType){
165290 int i, j;
165291 const char *zKW;
165292 if( n>=2 ){
165293 i = ((charMap(z[0])*4) ^ (charMap(z[n-1])*3) ^ n*1) % 127;
165294 for(i=((int)aKWHash[i])-1; i>=0; i=((int)aKWNext[i])-1){
165295 if( aKWLen[i]!=n ) continue;
165296 zKW = &zKWText[aKWOffset[i]];
165297#ifdef SQLITE_ASCII
165298 if( (z[0]&~0x20)!=zKW[0] ) continue;
165299 if( (z[1]&~0x20)!=zKW[1] ) continue;
165300 j = 2;
165301 while( j<n && (z[j]&~0x20)==zKW[j] ){ j++; }
165302#endif
165303#ifdef SQLITE_EBCDIC
165304 if( toupper(z[0])!=zKW[0] ) continue;
165305 if( toupper(z[1])!=zKW[1] ) continue;
165306 j = 2;
165307 while( j<n && toupper(z[j])==zKW[j] ){ j++; }
165308#endif
165309 if( j<n ) continue;
165310 testcase( i==0 ); /* REINDEX */
165311 testcase( i==1 ); /* INDEXED */
165312 testcase( i==2 ); /* INDEX */
165313 testcase( i==3 ); /* DESC */
165314 testcase( i==4 ); /* ESCAPE */
165315 testcase( i==5 ); /* EACH */
165316 testcase( i==6 ); /* CHECK */
165317 testcase( i==7 ); /* KEY */
165318 testcase( i==8 ); /* BEFORE */
165319 testcase( i==9 ); /* FOREIGN */
165320 testcase( i==10 ); /* FOR */
165321 testcase( i==11 ); /* IGNORE */
165322 testcase( i==12 ); /* REGEXP */
165323 testcase( i==13 ); /* EXPLAIN */
165324 testcase( i==14 ); /* INSTEAD */
165325 testcase( i==15 ); /* ADD */
165326 testcase( i==16 ); /* DATABASE */
165327 testcase( i==17 ); /* AS */
165328 testcase( i==18 ); /* SELECT */
165329 testcase( i==19 ); /* TABLE */
165330 testcase( i==20 ); /* LEFT */
165331 testcase( i==21 ); /* THEN */
165332 testcase( i==22 ); /* END */
165333 testcase( i==23 ); /* DEFERRABLE */
165334 testcase( i==24 ); /* ELSE */
165335 testcase( i==25 ); /* EXCLUDE */
165336 testcase( i==26 ); /* DELETE */
165337 testcase( i==27 ); /* TEMPORARY */
165338 testcase( i==28 ); /* TEMP */
165339 testcase( i==29 ); /* OR */
165340 testcase( i==30 ); /* ISNULL */
165341 testcase( i==31 ); /* NULLS */
165342 testcase( i==32 ); /* SAVEPOINT */
165343 testcase( i==33 ); /* INTERSECT */
165344 testcase( i==34 ); /* TIES */
165345 testcase( i==35 ); /* NOTNULL */
165346 testcase( i==36 ); /* NOT */
165347 testcase( i==37 ); /* NO */
165348 testcase( i==38 ); /* NULL */
165349 testcase( i==39 ); /* LIKE */
165350 testcase( i==40 ); /* EXCEPT */
165351 testcase( i==41 ); /* TRANSACTION */
165352 testcase( i==42 ); /* ACTION */
165353 testcase( i==43 ); /* ON */
165354 testcase( i==44 ); /* NATURAL */
165355 testcase( i==45 ); /* ALTER */
165356 testcase( i==46 ); /* RAISE */
165357 testcase( i==47 ); /* EXCLUSIVE */
165358 testcase( i==48 ); /* EXISTS */
165359 testcase( i==49 ); /* CONSTRAINT */
165360 testcase( i==50 ); /* INTO */
165361 testcase( i==51 ); /* OFFSET */
165362 testcase( i==52 ); /* OF */
165363 testcase( i==53 ); /* SET */
165364 testcase( i==54 ); /* TRIGGER */
165365 testcase( i==55 ); /* RANGE */
165366 testcase( i==56 ); /* GENERATED */
165367 testcase( i==57 ); /* DETACH */
165368 testcase( i==58 ); /* HAVING */
165369 testcase( i==59 ); /* GLOB */
165370 testcase( i==60 ); /* BEGIN */
165371 testcase( i==61 ); /* INNER */
165372 testcase( i==62 ); /* REFERENCES */
165373 testcase( i==63 ); /* UNIQUE */
165374 testcase( i==64 ); /* QUERY */
165375 testcase( i==65 ); /* WITHOUT */
165376 testcase( i==66 ); /* WITH */
165377 testcase( i==67 ); /* OUTER */
165378 testcase( i==68 ); /* RELEASE */
165379 testcase( i==69 ); /* ATTACH */
165380 testcase( i==70 ); /* BETWEEN */
165381 testcase( i==71 ); /* NOTHING */
165382 testcase( i==72 ); /* GROUPS */
165383 testcase( i==73 ); /* GROUP */
165384 testcase( i==74 ); /* CASCADE */
165385 testcase( i==75 ); /* ASC */
165386 testcase( i==76 ); /* DEFAULT */
165387 testcase( i==77 ); /* CASE */
165388 testcase( i==78 ); /* COLLATE */
165389 testcase( i==79 ); /* CREATE */
165390 testcase( i==80 ); /* CURRENT_DATE */
165391 testcase( i==81 ); /* IMMEDIATE */
165392 testcase( i==82 ); /* JOIN */
165393 testcase( i==83 ); /* INSERT */
165394 testcase( i==84 ); /* MATCH */
165395 testcase( i==85 ); /* PLAN */
165396 testcase( i==86 ); /* ANALYZE */
165397 testcase( i==87 ); /* PRAGMA */
165398 testcase( i==88 ); /* MATERIALIZED */
165399 testcase( i==89 ); /* DEFERRED */
165400 testcase( i==90 ); /* DISTINCT */
165401 testcase( i==91 ); /* IS */
165402 testcase( i==92 ); /* UPDATE */
165403 testcase( i==93 ); /* VALUES */
165404 testcase( i==94 ); /* VIRTUAL */
165405 testcase( i==95 ); /* ALWAYS */
165406 testcase( i==96 ); /* WHEN */
165407 testcase( i==97 ); /* WHERE */
165408 testcase( i==98 ); /* RECURSIVE */
165409 testcase( i==99 ); /* ABORT */
165410 testcase( i==100 ); /* AFTER */
165411 testcase( i==101 ); /* RENAME */
165412 testcase( i==102 ); /* AND */
165413 testcase( i==103 ); /* DROP */
165414 testcase( i==104 ); /* PARTITION */
165415 testcase( i==105 ); /* AUTOINCREMENT */
165416 testcase( i==106 ); /* TO */
165417 testcase( i==107 ); /* IN */
165418 testcase( i==108 ); /* CAST */
165419 testcase( i==109 ); /* COLUMN */
165420 testcase( i==110 ); /* COMMIT */
165421 testcase( i==111 ); /* CONFLICT */
165422 testcase( i==112 ); /* CROSS */
165423 testcase( i==113 ); /* CURRENT_TIMESTAMP */
165424 testcase( i==114 ); /* CURRENT_TIME */
165425 testcase( i==115 ); /* CURRENT */
165426 testcase( i==116 ); /* PRECEDING */
165427 testcase( i==117 ); /* FAIL */
165428 testcase( i==118 ); /* LAST */
165429 testcase( i==119 ); /* FILTER */
165430 testcase( i==120 ); /* REPLACE */
165431 testcase( i==121 ); /* FIRST */
165432 testcase( i==122 ); /* FOLLOWING */
165433 testcase( i==123 ); /* FROM */
165434 testcase( i==124 ); /* FULL */
165435 testcase( i==125 ); /* LIMIT */
165436 testcase( i==126 ); /* IF */
165437 testcase( i==127 ); /* ORDER */
165438 testcase( i==128 ); /* RESTRICT */
165439 testcase( i==129 ); /* OTHERS */
165440 testcase( i==130 ); /* OVER */
165441 testcase( i==131 ); /* RETURNING */
165442 testcase( i==132 ); /* RIGHT */
165443 testcase( i==133 ); /* ROLLBACK */
165444 testcase( i==134 ); /* ROWS */
165445 testcase( i==135 ); /* ROW */
165446 testcase( i==136 ); /* UNBOUNDED */
165447 testcase( i==137 ); /* UNION */
165448 testcase( i==138 ); /* USING */
165449 testcase( i==139 ); /* VACUUM */
165450 testcase( i==140 ); /* VIEW */
165451 testcase( i==141 ); /* WINDOW */
165452 testcase( i==142 ); /* DO */
165453 testcase( i==143 ); /* BY */
165454 testcase( i==144 ); /* INITIALLY */
165455 testcase( i==145 ); /* ALL */
165456 testcase( i==146 ); /* PRIMARY */
165457 *pType = aKWCode[i];
165458 break;
165459 }
165460 }
165461 return n;
165462}
165463SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char *z, int n){
165464 int id = TK_ID;
165465 keywordCode(z: (char*)z, n, pType: &id);
165466 return id;
165467}
165468#define SQLITE_N_KEYWORD 147
165469SQLITE_API int sqlite3_keyword_name(int i,const char **pzName,int *pnName){
165470 if( i<0 || i>=SQLITE_N_KEYWORD ) return SQLITE_ERROR;
165471 *pzName = zKWText + aKWOffset[i];
165472 *pnName = aKWLen[i];
165473 return SQLITE_OK;
165474}
165475SQLITE_API int sqlite3_keyword_count(void){ return SQLITE_N_KEYWORD; }
165476SQLITE_API int sqlite3_keyword_check(const char *zName, int nName){
165477 return TK_ID!=sqlite3KeywordCode(z: (const u8*)zName, n: nName);
165478}
165479
165480/************** End of keywordhash.h *****************************************/
165481/************** Continuing where we left off in tokenize.c *******************/
165482
165483
165484/*
165485** If X is a character that can be used in an identifier then
165486** IdChar(X) will be true. Otherwise it is false.
165487**
165488** For ASCII, any character with the high-order bit set is
165489** allowed in an identifier. For 7-bit characters,
165490** sqlite3IsIdChar[X] must be 1.
165491**
165492** For EBCDIC, the rules are more complex but have the same
165493** end result.
165494**
165495** Ticket #1066. the SQL standard does not allow '$' in the
165496** middle of identifiers. But many SQL implementations do.
165497** SQLite will allow '$' in identifiers for compatibility.
165498** But the feature is undocumented.
165499*/
165500#ifdef SQLITE_ASCII
165501#define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
165502#endif
165503#ifdef SQLITE_EBCDIC
165504SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[] = {
165505/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
165506 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 4x */
165507 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */
165508 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 6x */
165509 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* 7x */
165510 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, /* 8x */
165511 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, /* 9x */
165512 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, /* Ax */
165513 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
165514 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Cx */
165515 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Dx */
165516 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */
165517 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */
165518};
165519#define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
165520#endif
165521
165522/* Make the IdChar function accessible from ctime.c and alter.c */
165523SQLITE_PRIVATE int sqlite3IsIdChar(u8 c){ return IdChar(c); }
165524
165525#ifndef SQLITE_OMIT_WINDOWFUNC
165526/*
165527** Return the id of the next token in string (*pz). Before returning, set
165528** (*pz) to point to the byte following the parsed token.
165529*/
165530static int getToken(const unsigned char **pz){
165531 const unsigned char *z = *pz;
165532 int t; /* Token type to return */
165533 do {
165534 z += sqlite3GetToken(z, &t);
165535 }while( t==TK_SPACE );
165536 if( t==TK_ID
165537 || t==TK_STRING
165538 || t==TK_JOIN_KW
165539 || t==TK_WINDOW
165540 || t==TK_OVER
165541 || sqlite3ParserFallback(iToken: t)==TK_ID
165542 ){
165543 t = TK_ID;
165544 }
165545 *pz = z;
165546 return t;
165547}
165548
165549/*
165550** The following three functions are called immediately after the tokenizer
165551** reads the keywords WINDOW, OVER and FILTER, respectively, to determine
165552** whether the token should be treated as a keyword or an SQL identifier.
165553** This cannot be handled by the usual lemon %fallback method, due to
165554** the ambiguity in some constructions. e.g.
165555**
165556** SELECT sum(x) OVER ...
165557**
165558** In the above, "OVER" might be a keyword, or it might be an alias for the
165559** sum(x) expression. If a "%fallback ID OVER" directive were added to
165560** grammar, then SQLite would always treat "OVER" as an alias, making it
165561** impossible to call a window-function without a FILTER clause.
165562**
165563** WINDOW is treated as a keyword if:
165564**
165565** * the following token is an identifier, or a keyword that can fallback
165566** to being an identifier, and
165567** * the token after than one is TK_AS.
165568**
165569** OVER is a keyword if:
165570**
165571** * the previous token was TK_RP, and
165572** * the next token is either TK_LP or an identifier.
165573**
165574** FILTER is a keyword if:
165575**
165576** * the previous token was TK_RP, and
165577** * the next token is TK_LP.
165578*/
165579static int analyzeWindowKeyword(const unsigned char *z){
165580 int t;
165581 t = getToken(pz: &z);
165582 if( t!=TK_ID ) return TK_ID;
165583 t = getToken(pz: &z);
165584 if( t!=TK_AS ) return TK_ID;
165585 return TK_WINDOW;
165586}
165587static int analyzeOverKeyword(const unsigned char *z, int lastToken){
165588 if( lastToken==TK_RP ){
165589 int t = getToken(pz: &z);
165590 if( t==TK_LP || t==TK_ID ) return TK_OVER;
165591 }
165592 return TK_ID;
165593}
165594static int analyzeFilterKeyword(const unsigned char *z, int lastToken){
165595 if( lastToken==TK_RP && getToken(pz: &z)==TK_LP ){
165596 return TK_FILTER;
165597 }
165598 return TK_ID;
165599}
165600#endif /* SQLITE_OMIT_WINDOWFUNC */
165601
165602/*
165603** Return the length (in bytes) of the token that begins at z[0].
165604** Store the token type in *tokenType before returning.
165605*/
165606SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
165607 int i, c;
165608 switch( aiClass[*z] ){ /* Switch on the character-class of the first byte
165609 ** of the token. See the comment on the CC_ defines
165610 ** above. */
165611 case CC_SPACE: {
165612 testcase( z[0]==' ' );
165613 testcase( z[0]=='\t' );
165614 testcase( z[0]=='\n' );
165615 testcase( z[0]=='\f' );
165616 testcase( z[0]=='\r' );
165617 for(i=1; sqlite3Isspace(z[i]); i++){}
165618 *tokenType = TK_SPACE;
165619 return i;
165620 }
165621 case CC_MINUS: {
165622 if( z[1]=='-' ){
165623 for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
165624 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
165625 return i;
165626 }
165627 *tokenType = TK_MINUS;
165628 return 1;
165629 }
165630 case CC_LP: {
165631 *tokenType = TK_LP;
165632 return 1;
165633 }
165634 case CC_RP: {
165635 *tokenType = TK_RP;
165636 return 1;
165637 }
165638 case CC_SEMI: {
165639 *tokenType = TK_SEMI;
165640 return 1;
165641 }
165642 case CC_PLUS: {
165643 *tokenType = TK_PLUS;
165644 return 1;
165645 }
165646 case CC_STAR: {
165647 *tokenType = TK_STAR;
165648 return 1;
165649 }
165650 case CC_SLASH: {
165651 if( z[1]!='*' || z[2]==0 ){
165652 *tokenType = TK_SLASH;
165653 return 1;
165654 }
165655 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
165656 if( c ) i++;
165657 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
165658 return i;
165659 }
165660 case CC_PERCENT: {
165661 *tokenType = TK_REM;
165662 return 1;
165663 }
165664 case CC_EQ: {
165665 *tokenType = TK_EQ;
165666 return 1 + (z[1]=='=');
165667 }
165668 case CC_LT: {
165669 if( (c=z[1])=='=' ){
165670 *tokenType = TK_LE;
165671 return 2;
165672 }else if( c=='>' ){
165673 *tokenType = TK_NE;
165674 return 2;
165675 }else if( c=='<' ){
165676 *tokenType = TK_LSHIFT;
165677 return 2;
165678 }else{
165679 *tokenType = TK_LT;
165680 return 1;
165681 }
165682 }
165683 case CC_GT: {
165684 if( (c=z[1])=='=' ){
165685 *tokenType = TK_GE;
165686 return 2;
165687 }else if( c=='>' ){
165688 *tokenType = TK_RSHIFT;
165689 return 2;
165690 }else{
165691 *tokenType = TK_GT;
165692 return 1;
165693 }
165694 }
165695 case CC_BANG: {
165696 if( z[1]!='=' ){
165697 *tokenType = TK_ILLEGAL;
165698 return 1;
165699 }else{
165700 *tokenType = TK_NE;
165701 return 2;
165702 }
165703 }
165704 case CC_PIPE: {
165705 if( z[1]!='|' ){
165706 *tokenType = TK_BITOR;
165707 return 1;
165708 }else{
165709 *tokenType = TK_CONCAT;
165710 return 2;
165711 }
165712 }
165713 case CC_COMMA: {
165714 *tokenType = TK_COMMA;
165715 return 1;
165716 }
165717 case CC_AND: {
165718 *tokenType = TK_BITAND;
165719 return 1;
165720 }
165721 case CC_TILDA: {
165722 *tokenType = TK_BITNOT;
165723 return 1;
165724 }
165725 case CC_QUOTE: {
165726 int delim = z[0];
165727 testcase( delim=='`' );
165728 testcase( delim=='\'' );
165729 testcase( delim=='"' );
165730 for(i=1; (c=z[i])!=0; i++){
165731 if( c==delim ){
165732 if( z[i+1]==delim ){
165733 i++;
165734 }else{
165735 break;
165736 }
165737 }
165738 }
165739 if( c=='\'' ){
165740 *tokenType = TK_STRING;
165741 return i+1;
165742 }else if( c!=0 ){
165743 *tokenType = TK_ID;
165744 return i+1;
165745 }else{
165746 *tokenType = TK_ILLEGAL;
165747 return i;
165748 }
165749 }
165750 case CC_DOT: {
165751#ifndef SQLITE_OMIT_FLOATING_POINT
165752 if( !sqlite3Isdigit(z[1]) )
165753#endif
165754 {
165755 *tokenType = TK_DOT;
165756 return 1;
165757 }
165758 /* If the next character is a digit, this is a floating point
165759 ** number that begins with ".". Fall thru into the next case */
165760 /* no break */ deliberate_fall_through
165761 }
165762 case CC_DIGIT: {
165763 testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' );
165764 testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' );
165765 testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' );
165766 testcase( z[0]=='9' );
165767 *tokenType = TK_INTEGER;
165768#ifndef SQLITE_OMIT_HEX_INTEGER
165769 if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){
165770 for(i=3; sqlite3Isxdigit(z[i]); i++){}
165771 return i;
165772 }
165773#endif
165774 for(i=0; sqlite3Isdigit(z[i]); i++){}
165775#ifndef SQLITE_OMIT_FLOATING_POINT
165776 if( z[i]=='.' ){
165777 i++;
165778 while( sqlite3Isdigit(z[i]) ){ i++; }
165779 *tokenType = TK_FLOAT;
165780 }
165781 if( (z[i]=='e' || z[i]=='E') &&
165782 ( sqlite3Isdigit(z[i+1])
165783 || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
165784 )
165785 ){
165786 i += 2;
165787 while( sqlite3Isdigit(z[i]) ){ i++; }
165788 *tokenType = TK_FLOAT;
165789 }
165790#endif
165791 while( IdChar(z[i]) ){
165792 *tokenType = TK_ILLEGAL;
165793 i++;
165794 }
165795 return i;
165796 }
165797 case CC_QUOTE2: {
165798 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
165799 *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
165800 return i;
165801 }
165802 case CC_VARNUM: {
165803 *tokenType = TK_VARIABLE;
165804 for(i=1; sqlite3Isdigit(z[i]); i++){}
165805 return i;
165806 }
165807 case CC_DOLLAR:
165808 case CC_VARALPHA: {
165809 int n = 0;
165810 testcase( z[0]=='$' ); testcase( z[0]=='@' );
165811 testcase( z[0]==':' ); testcase( z[0]=='#' );
165812 *tokenType = TK_VARIABLE;
165813 for(i=1; (c=z[i])!=0; i++){
165814 if( IdChar(c) ){
165815 n++;
165816#ifndef SQLITE_OMIT_TCL_VARIABLE
165817 }else if( c=='(' && n>0 ){
165818 do{
165819 i++;
165820 }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
165821 if( c==')' ){
165822 i++;
165823 }else{
165824 *tokenType = TK_ILLEGAL;
165825 }
165826 break;
165827 }else if( c==':' && z[i+1]==':' ){
165828 i++;
165829#endif
165830 }else{
165831 break;
165832 }
165833 }
165834 if( n==0 ) *tokenType = TK_ILLEGAL;
165835 return i;
165836 }
165837 case CC_KYWD0: {
165838 for(i=1; aiClass[z[i]]<=CC_KYWD; i++){}
165839 if( IdChar(z[i]) ){
165840 /* This token started out using characters that can appear in keywords,
165841 ** but z[i] is a character not allowed within keywords, so this must
165842 ** be an identifier instead */
165843 i++;
165844 break;
165845 }
165846 *tokenType = TK_ID;
165847 return keywordCode(z: (char*)z, n: i, pType: tokenType);
165848 }
165849 case CC_X: {
165850#ifndef SQLITE_OMIT_BLOB_LITERAL
165851 testcase( z[0]=='x' ); testcase( z[0]=='X' );
165852 if( z[1]=='\'' ){
165853 *tokenType = TK_BLOB;
165854 for(i=2; sqlite3Isxdigit(z[i]); i++){}
165855 if( z[i]!='\'' || i%2 ){
165856 *tokenType = TK_ILLEGAL;
165857 while( z[i] && z[i]!='\'' ){ i++; }
165858 }
165859 if( z[i] ) i++;
165860 return i;
165861 }
165862#endif
165863 /* If it is not a BLOB literal, then it must be an ID, since no
165864 ** SQL keywords start with the letter 'x'. Fall through */
165865 /* no break */ deliberate_fall_through
165866 }
165867 case CC_KYWD:
165868 case CC_ID: {
165869 i = 1;
165870 break;
165871 }
165872 case CC_BOM: {
165873 if( z[1]==0xbb && z[2]==0xbf ){
165874 *tokenType = TK_SPACE;
165875 return 3;
165876 }
165877 i = 1;
165878 break;
165879 }
165880 case CC_NUL: {
165881 *tokenType = TK_ILLEGAL;
165882 return 0;
165883 }
165884 default: {
165885 *tokenType = TK_ILLEGAL;
165886 return 1;
165887 }
165888 }
165889 while( IdChar(z[i]) ){ i++; }
165890 *tokenType = TK_ID;
165891 return i;
165892}
165893
165894/*
165895** Run the parser on the given SQL string. The parser structure is
165896** passed in. An SQLITE_ status code is returned. If an error occurs
165897** then an and attempt is made to write an error message into
165898** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
165899** error message.
165900*/
165901SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
165902 int nErr = 0; /* Number of errors encountered */
165903 void *pEngine; /* The LEMON-generated LALR(1) parser */
165904 int n = 0; /* Length of the next token token */
165905 int tokenType; /* type of the next token */
165906 int lastTokenParsed = -1; /* type of the previous token */
165907 sqlite3 *db = pParse->db; /* The database connection */
165908 int mxSqlLen; /* Max length of an SQL string */
165909 Parse *pParentParse = 0; /* Outer parse context, if any */
165910#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
165911 yyParser sEngine; /* Space to hold the Lemon-generated Parser object */
165912#endif
165913 VVA_ONLY( u8 startedWithOom = db->mallocFailed );
165914
165915 assert( zSql!=0 );
165916 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
165917 if( db->nVdbeActive==0 ){
165918 AtomicStore(&db->u1.isInterrupted, 0);
165919 }
165920 pParse->rc = SQLITE_OK;
165921 pParse->zTail = zSql;
165922 assert( pzErrMsg!=0 );
165923#ifdef SQLITE_DEBUG
165924 if( db->flags & SQLITE_ParserTrace ){
165925 printf("parser: [[[%s]]]\n", zSql);
165926 sqlite3ParserTrace(stdout, "parser: ");
165927 }else{
165928 sqlite3ParserTrace(0, 0);
165929 }
165930#endif
165931#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
165932 pEngine = &sEngine;
165933 sqlite3ParserInit(yypRawParser: pEngine, pParse);
165934#else
165935 pEngine = sqlite3ParserAlloc(sqlite3Malloc, pParse);
165936 if( pEngine==0 ){
165937 sqlite3OomFault(db);
165938 return SQLITE_NOMEM_BKPT;
165939 }
165940#endif
165941 assert( pParse->pNewTable==0 );
165942 assert( pParse->pNewTrigger==0 );
165943 assert( pParse->nVar==0 );
165944 assert( pParse->pVList==0 );
165945 pParentParse = db->pParse;
165946 db->pParse = pParse;
165947 while( 1 ){
165948 n = sqlite3GetToken(z: (u8*)zSql, tokenType: &tokenType);
165949 mxSqlLen -= n;
165950 if( mxSqlLen<0 ){
165951 pParse->rc = SQLITE_TOOBIG;
165952 break;
165953 }
165954#ifndef SQLITE_OMIT_WINDOWFUNC
165955 if( tokenType>=TK_WINDOW ){
165956 assert( tokenType==TK_SPACE || tokenType==TK_OVER || tokenType==TK_FILTER
165957 || tokenType==TK_ILLEGAL || tokenType==TK_WINDOW
165958 );
165959#else
165960 if( tokenType>=TK_SPACE ){
165961 assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL );
165962#endif /* SQLITE_OMIT_WINDOWFUNC */
165963 if( AtomicLoad(&db->u1.isInterrupted) ){
165964 pParse->rc = SQLITE_INTERRUPT;
165965 break;
165966 }
165967 if( tokenType==TK_SPACE ){
165968 zSql += n;
165969 continue;
165970 }
165971 if( zSql[0]==0 ){
165972 /* Upon reaching the end of input, call the parser two more times
165973 ** with tokens TK_SEMI and 0, in that order. */
165974 if( lastTokenParsed==TK_SEMI ){
165975 tokenType = 0;
165976 }else if( lastTokenParsed==0 ){
165977 break;
165978 }else{
165979 tokenType = TK_SEMI;
165980 }
165981 n = 0;
165982#ifndef SQLITE_OMIT_WINDOWFUNC
165983 }else if( tokenType==TK_WINDOW ){
165984 assert( n==6 );
165985 tokenType = analyzeWindowKeyword(z: (const u8*)&zSql[6]);
165986 }else if( tokenType==TK_OVER ){
165987 assert( n==4 );
165988 tokenType = analyzeOverKeyword(z: (const u8*)&zSql[4], lastToken: lastTokenParsed);
165989 }else if( tokenType==TK_FILTER ){
165990 assert( n==6 );
165991 tokenType = analyzeFilterKeyword(z: (const u8*)&zSql[6], lastToken: lastTokenParsed);
165992#endif /* SQLITE_OMIT_WINDOWFUNC */
165993 }else{
165994 sqlite3ErrorMsg(pParse, zFormat: "unrecognized token: \"%.*s\"", n, zSql);
165995 break;
165996 }
165997 }
165998 pParse->sLastToken.z = zSql;
165999 pParse->sLastToken.n = n;
166000 sqlite3Parser(yyp: pEngine, yymajor: tokenType, yyminor: pParse->sLastToken);
166001 lastTokenParsed = tokenType;
166002 zSql += n;
166003 assert( db->mallocFailed==0 || pParse->rc!=SQLITE_OK || startedWithOom );
166004 if( pParse->rc!=SQLITE_OK ) break;
166005 }
166006 assert( nErr==0 );
166007#ifdef YYTRACKMAXSTACKDEPTH
166008 sqlite3_mutex_enter(sqlite3MallocMutex());
166009 sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK,
166010 sqlite3ParserStackPeak(pEngine)
166011 );
166012 sqlite3_mutex_leave(sqlite3MallocMutex());
166013#endif /* YYDEBUG */
166014#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
166015 sqlite3ParserFinalize(p: pEngine);
166016#else
166017 sqlite3ParserFree(pEngine, sqlite3_free);
166018#endif
166019 if( db->mallocFailed ){
166020 pParse->rc = SQLITE_NOMEM_BKPT;
166021 }
166022 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
166023 pParse->zErrMsg = sqlite3MPrintf(db, zFormat: "%s", sqlite3ErrStr(pParse->rc));
166024 }
166025 assert( pzErrMsg!=0 );
166026 if( pParse->zErrMsg ){
166027 *pzErrMsg = pParse->zErrMsg;
166028 sqlite3_log(iErrCode: pParse->rc, zFormat: "%s in \"%s\"",
166029 *pzErrMsg, pParse->zTail);
166030 pParse->zErrMsg = 0;
166031 nErr++;
166032 }
166033 pParse->zTail = zSql;
166034 if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
166035 sqlite3VdbeDelete(p: pParse->pVdbe);
166036 pParse->pVdbe = 0;
166037 }
166038#ifndef SQLITE_OMIT_SHARED_CACHE
166039 if( pParse->nested==0 ){
166040 sqlite3DbFree(db, p: pParse->aTableLock);
166041 pParse->aTableLock = 0;
166042 pParse->nTableLock = 0;
166043 }
166044#endif
166045#ifndef SQLITE_OMIT_VIRTUALTABLE
166046 sqlite3_free(p: pParse->apVtabLock);
166047#endif
166048
166049 if( !IN_SPECIAL_PARSE ){
166050 /* If the pParse->declareVtab flag is set, do not delete any table
166051 ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
166052 ** will take responsibility for freeing the Table structure.
166053 */
166054 sqlite3DeleteTable(db, pTable: pParse->pNewTable);
166055 }
166056 if( !IN_RENAME_OBJECT ){
166057 sqlite3DeleteTrigger(db, pTrigger: pParse->pNewTrigger);
166058 }
166059 sqlite3DbFree(db, p: pParse->pVList);
166060 db->pParse = pParentParse;
166061 assert( nErr==0 || pParse->rc!=SQLITE_OK );
166062 return nErr;
166063}
166064
166065
166066#ifdef SQLITE_ENABLE_NORMALIZE
166067/*
166068** Insert a single space character into pStr if the current string
166069** ends with an identifier
166070*/
166071static void addSpaceSeparator(sqlite3_str *pStr){
166072 if( pStr->nChar && sqlite3IsIdChar(pStr->zText[pStr->nChar-1]) ){
166073 sqlite3_str_append(pStr, " ", 1);
166074 }
166075}
166076
166077/*
166078** Compute a normalization of the SQL given by zSql[0..nSql-1]. Return
166079** the normalization in space obtained from sqlite3DbMalloc(). Or return
166080** NULL if anything goes wrong or if zSql is NULL.
166081*/
166082SQLITE_PRIVATE char *sqlite3Normalize(
166083 Vdbe *pVdbe, /* VM being reprepared */
166084 const char *zSql /* The original SQL string */
166085){
166086 sqlite3 *db; /* The database connection */
166087 int i; /* Next unread byte of zSql[] */
166088 int n; /* length of current token */
166089 int tokenType; /* type of current token */
166090 int prevType = 0; /* Previous non-whitespace token */
166091 int nParen; /* Number of nested levels of parentheses */
166092 int iStartIN; /* Start of RHS of IN operator in z[] */
166093 int nParenAtIN; /* Value of nParent at start of RHS of IN operator */
166094 u32 j; /* Bytes of normalized SQL generated so far */
166095 sqlite3_str *pStr; /* The normalized SQL string under construction */
166096
166097 db = sqlite3VdbeDb(pVdbe);
166098 tokenType = -1;
166099 nParen = iStartIN = nParenAtIN = 0;
166100 pStr = sqlite3_str_new(db);
166101 assert( pStr!=0 ); /* sqlite3_str_new() never returns NULL */
166102 for(i=0; zSql[i] && pStr->accError==0; i+=n){
166103 if( tokenType!=TK_SPACE ){
166104 prevType = tokenType;
166105 }
166106 n = sqlite3GetToken((unsigned char*)zSql+i, &tokenType);
166107 if( NEVER(n<=0) ) break;
166108 switch( tokenType ){
166109 case TK_SPACE: {
166110 break;
166111 }
166112 case TK_NULL: {
166113 if( prevType==TK_IS || prevType==TK_NOT ){
166114 sqlite3_str_append(pStr, " NULL", 5);
166115 break;
166116 }
166117 /* Fall through */
166118 }
166119 case TK_STRING:
166120 case TK_INTEGER:
166121 case TK_FLOAT:
166122 case TK_VARIABLE:
166123 case TK_BLOB: {
166124 sqlite3_str_append(pStr, "?", 1);
166125 break;
166126 }
166127 case TK_LP: {
166128 nParen++;
166129 if( prevType==TK_IN ){
166130 iStartIN = pStr->nChar;
166131 nParenAtIN = nParen;
166132 }
166133 sqlite3_str_append(pStr, "(", 1);
166134 break;
166135 }
166136 case TK_RP: {
166137 if( iStartIN>0 && nParen==nParenAtIN ){
166138 assert( pStr->nChar>=(u32)iStartIN );
166139 pStr->nChar = iStartIN+1;
166140 sqlite3_str_append(pStr, "?,?,?", 5);
166141 iStartIN = 0;
166142 }
166143 nParen--;
166144 sqlite3_str_append(pStr, ")", 1);
166145 break;
166146 }
166147 case TK_ID: {
166148 iStartIN = 0;
166149 j = pStr->nChar;
166150 if( sqlite3Isquote(zSql[i]) ){
166151 char *zId = sqlite3DbStrNDup(db, zSql+i, n);
166152 int nId;
166153 int eType = 0;
166154 if( zId==0 ) break;
166155 sqlite3Dequote(zId);
166156 if( zSql[i]=='"' && sqlite3VdbeUsesDoubleQuotedString(pVdbe, zId) ){
166157 sqlite3_str_append(pStr, "?", 1);
166158 sqlite3DbFree(db, zId);
166159 break;
166160 }
166161 nId = sqlite3Strlen30(zId);
166162 if( sqlite3GetToken((u8*)zId, &eType)==nId && eType==TK_ID ){
166163 addSpaceSeparator(pStr);
166164 sqlite3_str_append(pStr, zId, nId);
166165 }else{
166166 sqlite3_str_appendf(pStr, "\"%w\"", zId);
166167 }
166168 sqlite3DbFree(db, zId);
166169 }else{
166170 addSpaceSeparator(pStr);
166171 sqlite3_str_append(pStr, zSql+i, n);
166172 }
166173 while( j<pStr->nChar ){
166174 pStr->zText[j] = sqlite3Tolower(pStr->zText[j]);
166175 j++;
166176 }
166177 break;
166178 }
166179 case TK_SELECT: {
166180 iStartIN = 0;
166181 /* fall through */
166182 }
166183 default: {
166184 if( sqlite3IsIdChar(zSql[i]) ) addSpaceSeparator(pStr);
166185 j = pStr->nChar;
166186 sqlite3_str_append(pStr, zSql+i, n);
166187 while( j<pStr->nChar ){
166188 pStr->zText[j] = sqlite3Toupper(pStr->zText[j]);
166189 j++;
166190 }
166191 break;
166192 }
166193 }
166194 }
166195 if( tokenType!=TK_SEMI ) sqlite3_str_append(pStr, ";", 1);
166196 return sqlite3_str_finish(pStr);
166197}
166198#endif /* SQLITE_ENABLE_NORMALIZE */
166199
166200/************** End of tokenize.c ********************************************/
166201/************** Begin file complete.c ****************************************/
166202/*
166203** 2001 September 15
166204**
166205** The author disclaims copyright to this source code. In place of
166206** a legal notice, here is a blessing:
166207**
166208** May you do good and not evil.
166209** May you find forgiveness for yourself and forgive others.
166210** May you share freely, never taking more than you give.
166211**
166212*************************************************************************
166213** An tokenizer for SQL
166214**
166215** This file contains C code that implements the sqlite3_complete() API.
166216** This code used to be part of the tokenizer.c source file. But by
166217** separating it out, the code will be automatically omitted from
166218** static links that do not use it.
166219*/
166220/* #include "sqliteInt.h" */
166221#ifndef SQLITE_OMIT_COMPLETE
166222
166223/*
166224** This is defined in tokenize.c. We just have to import the definition.
166225*/
166226#ifndef SQLITE_AMALGAMATION
166227#ifdef SQLITE_ASCII
166228#define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
166229#endif
166230#ifdef SQLITE_EBCDIC
166231SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[];
166232#define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
166233#endif
166234#endif /* SQLITE_AMALGAMATION */
166235
166236
166237/*
166238** Token types used by the sqlite3_complete() routine. See the header
166239** comments on that procedure for additional information.
166240*/
166241#define tkSEMI 0
166242#define tkWS 1
166243#define tkOTHER 2
166244#ifndef SQLITE_OMIT_TRIGGER
166245#define tkEXPLAIN 3
166246#define tkCREATE 4
166247#define tkTEMP 5
166248#define tkTRIGGER 6
166249#define tkEND 7
166250#endif
166251
166252/*
166253** Return TRUE if the given SQL string ends in a semicolon.
166254**
166255** Special handling is require for CREATE TRIGGER statements.
166256** Whenever the CREATE TRIGGER keywords are seen, the statement
166257** must end with ";END;".
166258**
166259** This implementation uses a state machine with 8 states:
166260**
166261** (0) INVALID We have not yet seen a non-whitespace character.
166262**
166263** (1) START At the beginning or end of an SQL statement. This routine
166264** returns 1 if it ends in the START state and 0 if it ends
166265** in any other state.
166266**
166267** (2) NORMAL We are in the middle of statement which ends with a single
166268** semicolon.
166269**
166270** (3) EXPLAIN The keyword EXPLAIN has been seen at the beginning of
166271** a statement.
166272**
166273** (4) CREATE The keyword CREATE has been seen at the beginning of a
166274** statement, possibly preceded by EXPLAIN and/or followed by
166275** TEMP or TEMPORARY
166276**
166277** (5) TRIGGER We are in the middle of a trigger definition that must be
166278** ended by a semicolon, the keyword END, and another semicolon.
166279**
166280** (6) SEMI We've seen the first semicolon in the ";END;" that occurs at
166281** the end of a trigger definition.
166282**
166283** (7) END We've seen the ";END" of the ";END;" that occurs at the end
166284** of a trigger definition.
166285**
166286** Transitions between states above are determined by tokens extracted
166287** from the input. The following tokens are significant:
166288**
166289** (0) tkSEMI A semicolon.
166290** (1) tkWS Whitespace.
166291** (2) tkOTHER Any other SQL token.
166292** (3) tkEXPLAIN The "explain" keyword.
166293** (4) tkCREATE The "create" keyword.
166294** (5) tkTEMP The "temp" or "temporary" keyword.
166295** (6) tkTRIGGER The "trigger" keyword.
166296** (7) tkEND The "end" keyword.
166297**
166298** Whitespace never causes a state transition and is always ignored.
166299** This means that a SQL string of all whitespace is invalid.
166300**
166301** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
166302** to recognize the end of a trigger can be omitted. All we have to do
166303** is look for a semicolon that is not part of an string or comment.
166304*/
166305SQLITE_API int sqlite3_complete(const char *zSql){
166306 u8 state = 0; /* Current state, using numbers defined in header comment */
166307 u8 token; /* Value of the next token */
166308
166309#ifndef SQLITE_OMIT_TRIGGER
166310 /* A complex statement machine used to detect the end of a CREATE TRIGGER
166311 ** statement. This is the normal case.
166312 */
166313 static const u8 trans[8][8] = {
166314 /* Token: */
166315 /* State: ** SEMI WS OTHER EXPLAIN CREATE TEMP TRIGGER END */
166316 /* 0 INVALID: */ { 1, 0, 2, 3, 4, 2, 2, 2, },
166317 /* 1 START: */ { 1, 1, 2, 3, 4, 2, 2, 2, },
166318 /* 2 NORMAL: */ { 1, 2, 2, 2, 2, 2, 2, 2, },
166319 /* 3 EXPLAIN: */ { 1, 3, 3, 2, 4, 2, 2, 2, },
166320 /* 4 CREATE: */ { 1, 4, 2, 2, 2, 4, 5, 2, },
166321 /* 5 TRIGGER: */ { 6, 5, 5, 5, 5, 5, 5, 5, },
166322 /* 6 SEMI: */ { 6, 6, 5, 5, 5, 5, 5, 7, },
166323 /* 7 END: */ { 1, 7, 5, 5, 5, 5, 5, 5, },
166324 };
166325#else
166326 /* If triggers are not supported by this compile then the statement machine
166327 ** used to detect the end of a statement is much simpler
166328 */
166329 static const u8 trans[3][3] = {
166330 /* Token: */
166331 /* State: ** SEMI WS OTHER */
166332 /* 0 INVALID: */ { 1, 0, 2, },
166333 /* 1 START: */ { 1, 1, 2, },
166334 /* 2 NORMAL: */ { 1, 2, 2, },
166335 };
166336#endif /* SQLITE_OMIT_TRIGGER */
166337
166338#ifdef SQLITE_ENABLE_API_ARMOR
166339 if( zSql==0 ){
166340 (void)SQLITE_MISUSE_BKPT;
166341 return 0;
166342 }
166343#endif
166344
166345 while( *zSql ){
166346 switch( *zSql ){
166347 case ';': { /* A semicolon */
166348 token = tkSEMI;
166349 break;
166350 }
166351 case ' ':
166352 case '\r':
166353 case '\t':
166354 case '\n':
166355 case '\f': { /* White space is ignored */
166356 token = tkWS;
166357 break;
166358 }
166359 case '/': { /* C-style comments */
166360 if( zSql[1]!='*' ){
166361 token = tkOTHER;
166362 break;
166363 }
166364 zSql += 2;
166365 while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
166366 if( zSql[0]==0 ) return 0;
166367 zSql++;
166368 token = tkWS;
166369 break;
166370 }
166371 case '-': { /* SQL-style comments from "--" to end of line */
166372 if( zSql[1]!='-' ){
166373 token = tkOTHER;
166374 break;
166375 }
166376 while( *zSql && *zSql!='\n' ){ zSql++; }
166377 if( *zSql==0 ) return state==1;
166378 token = tkWS;
166379 break;
166380 }
166381 case '[': { /* Microsoft-style identifiers in [...] */
166382 zSql++;
166383 while( *zSql && *zSql!=']' ){ zSql++; }
166384 if( *zSql==0 ) return 0;
166385 token = tkOTHER;
166386 break;
166387 }
166388 case '`': /* Grave-accent quoted symbols used by MySQL */
166389 case '"': /* single- and double-quoted strings */
166390 case '\'': {
166391 int c = *zSql;
166392 zSql++;
166393 while( *zSql && *zSql!=c ){ zSql++; }
166394 if( *zSql==0 ) return 0;
166395 token = tkOTHER;
166396 break;
166397 }
166398 default: {
166399#ifdef SQLITE_EBCDIC
166400 unsigned char c;
166401#endif
166402 if( IdChar((u8)*zSql) ){
166403 /* Keywords and unquoted identifiers */
166404 int nId;
166405 for(nId=1; IdChar(zSql[nId]); nId++){}
166406#ifdef SQLITE_OMIT_TRIGGER
166407 token = tkOTHER;
166408#else
166409 switch( *zSql ){
166410 case 'c': case 'C': {
166411 if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){
166412 token = tkCREATE;
166413 }else{
166414 token = tkOTHER;
166415 }
166416 break;
166417 }
166418 case 't': case 'T': {
166419 if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){
166420 token = tkTRIGGER;
166421 }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){
166422 token = tkTEMP;
166423 }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){
166424 token = tkTEMP;
166425 }else{
166426 token = tkOTHER;
166427 }
166428 break;
166429 }
166430 case 'e': case 'E': {
166431 if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){
166432 token = tkEND;
166433 }else
166434#ifndef SQLITE_OMIT_EXPLAIN
166435 if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){
166436 token = tkEXPLAIN;
166437 }else
166438#endif
166439 {
166440 token = tkOTHER;
166441 }
166442 break;
166443 }
166444 default: {
166445 token = tkOTHER;
166446 break;
166447 }
166448 }
166449#endif /* SQLITE_OMIT_TRIGGER */
166450 zSql += nId-1;
166451 }else{
166452 /* Operators and special symbols */
166453 token = tkOTHER;
166454 }
166455 break;
166456 }
166457 }
166458 state = trans[state][token];
166459 zSql++;
166460 }
166461 return state==1;
166462}
166463
166464#ifndef SQLITE_OMIT_UTF16
166465/*
166466** This routine is the same as the sqlite3_complete() routine described
166467** above, except that the parameter is required to be UTF-16 encoded, not
166468** UTF-8.
166469*/
166470SQLITE_API int sqlite3_complete16(const void *zSql){
166471 sqlite3_value *pVal;
166472 char const *zSql8;
166473 int rc;
166474
166475#ifndef SQLITE_OMIT_AUTOINIT
166476 rc = sqlite3_initialize();
166477 if( rc ) return rc;
166478#endif
166479 pVal = sqlite3ValueNew(0);
166480 sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
166481 zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8);
166482 if( zSql8 ){
166483 rc = sqlite3_complete(zSql8);
166484 }else{
166485 rc = SQLITE_NOMEM_BKPT;
166486 }
166487 sqlite3ValueFree(pVal);
166488 return rc & 0xff;
166489}
166490#endif /* SQLITE_OMIT_UTF16 */
166491#endif /* SQLITE_OMIT_COMPLETE */
166492
166493/************** End of complete.c ********************************************/
166494/************** Begin file main.c ********************************************/
166495/*
166496** 2001 September 15
166497**
166498** The author disclaims copyright to this source code. In place of
166499** a legal notice, here is a blessing:
166500**
166501** May you do good and not evil.
166502** May you find forgiveness for yourself and forgive others.
166503** May you share freely, never taking more than you give.
166504**
166505*************************************************************************
166506** Main file for the SQLite library. The routines in this file
166507** implement the programmer interface to the library. Routines in
166508** other files are for internal use by SQLite and should not be
166509** accessed by users of the library.
166510*/
166511/* #include "sqliteInt.h" */
166512
166513#ifdef SQLITE_ENABLE_FTS3
166514/************** Include fts3.h in the middle of main.c ***********************/
166515/************** Begin file fts3.h ********************************************/
166516/*
166517** 2006 Oct 10
166518**
166519** The author disclaims copyright to this source code. In place of
166520** a legal notice, here is a blessing:
166521**
166522** May you do good and not evil.
166523** May you find forgiveness for yourself and forgive others.
166524** May you share freely, never taking more than you give.
166525**
166526******************************************************************************
166527**
166528** This header file is used by programs that want to link against the
166529** FTS3 library. All it does is declare the sqlite3Fts3Init() interface.
166530*/
166531/* #include "sqlite3.h" */
166532
166533#if 0
166534extern "C" {
166535#endif /* __cplusplus */
166536
166537SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db);
166538
166539#if 0
166540} /* extern "C" */
166541#endif /* __cplusplus */
166542
166543/************** End of fts3.h ************************************************/
166544/************** Continuing where we left off in main.c ***********************/
166545#endif
166546#ifdef SQLITE_ENABLE_RTREE
166547/************** Include rtree.h in the middle of main.c **********************/
166548/************** Begin file rtree.h *******************************************/
166549/*
166550** 2008 May 26
166551**
166552** The author disclaims copyright to this source code. In place of
166553** a legal notice, here is a blessing:
166554**
166555** May you do good and not evil.
166556** May you find forgiveness for yourself and forgive others.
166557** May you share freely, never taking more than you give.
166558**
166559******************************************************************************
166560**
166561** This header file is used by programs that want to link against the
166562** RTREE library. All it does is declare the sqlite3RtreeInit() interface.
166563*/
166564/* #include "sqlite3.h" */
166565
166566#ifdef SQLITE_OMIT_VIRTUALTABLE
166567# undef SQLITE_ENABLE_RTREE
166568#endif
166569
166570#if 0
166571extern "C" {
166572#endif /* __cplusplus */
166573
166574SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db);
166575
166576#if 0
166577} /* extern "C" */
166578#endif /* __cplusplus */
166579
166580/************** End of rtree.h ***********************************************/
166581/************** Continuing where we left off in main.c ***********************/
166582#endif
166583#if defined(SQLITE_ENABLE_ICU) || defined(SQLITE_ENABLE_ICU_COLLATIONS)
166584/************** Include sqliteicu.h in the middle of main.c ******************/
166585/************** Begin file sqliteicu.h ***************************************/
166586/*
166587** 2008 May 26
166588**
166589** The author disclaims copyright to this source code. In place of
166590** a legal notice, here is a blessing:
166591**
166592** May you do good and not evil.
166593** May you find forgiveness for yourself and forgive others.
166594** May you share freely, never taking more than you give.
166595**
166596******************************************************************************
166597**
166598** This header file is used by programs that want to link against the
166599** ICU extension. All it does is declare the sqlite3IcuInit() interface.
166600*/
166601/* #include "sqlite3.h" */
166602
166603#if 0
166604extern "C" {
166605#endif /* __cplusplus */
166606
166607SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db);
166608
166609#if 0
166610} /* extern "C" */
166611#endif /* __cplusplus */
166612
166613/************** End of sqliteicu.h *******************************************/
166614/************** Continuing where we left off in main.c ***********************/
166615#endif
166616
166617/*
166618** This is an extension initializer that is a no-op and always
166619** succeeds, except that it fails if the fault-simulation is set
166620** to 500.
166621*/
166622static int sqlite3TestExtInit(sqlite3 *db){
166623 (void)db;
166624 return sqlite3FaultSim(iTest: 500);
166625}
166626
166627
166628/*
166629** Forward declarations of external module initializer functions
166630** for modules that need them.
166631*/
166632#ifdef SQLITE_ENABLE_FTS1
166633SQLITE_PRIVATE int sqlite3Fts1Init(sqlite3*);
166634#endif
166635#ifdef SQLITE_ENABLE_FTS2
166636SQLITE_PRIVATE int sqlite3Fts2Init(sqlite3*);
166637#endif
166638#ifdef SQLITE_ENABLE_FTS5
166639SQLITE_PRIVATE int sqlite3Fts5Init(sqlite3*);
166640#endif
166641#ifdef SQLITE_ENABLE_JSON1
166642SQLITE_PRIVATE int sqlite3Json1Init(sqlite3*);
166643#endif
166644#ifdef SQLITE_ENABLE_STMTVTAB
166645SQLITE_PRIVATE int sqlite3StmtVtabInit(sqlite3*);
166646#endif
166647
166648/*
166649** An array of pointers to extension initializer functions for
166650** built-in extensions.
166651*/
166652static int (*const sqlite3BuiltinExtensions[])(sqlite3*) = {
166653#ifdef SQLITE_ENABLE_FTS1
166654 sqlite3Fts1Init,
166655#endif
166656#ifdef SQLITE_ENABLE_FTS2
166657 sqlite3Fts2Init,
166658#endif
166659#ifdef SQLITE_ENABLE_FTS3
166660 sqlite3Fts3Init,
166661#endif
166662#ifdef SQLITE_ENABLE_FTS5
166663 sqlite3Fts5Init,
166664#endif
166665#if defined(SQLITE_ENABLE_ICU) || defined(SQLITE_ENABLE_ICU_COLLATIONS)
166666 sqlite3IcuInit,
166667#endif
166668#ifdef SQLITE_ENABLE_RTREE
166669 sqlite3RtreeInit,
166670#endif
166671#ifdef SQLITE_ENABLE_DBPAGE_VTAB
166672 sqlite3DbpageRegister,
166673#endif
166674#ifdef SQLITE_ENABLE_DBSTAT_VTAB
166675 sqlite3DbstatRegister,
166676#endif
166677 sqlite3TestExtInit,
166678#ifdef SQLITE_ENABLE_JSON1
166679 sqlite3Json1Init,
166680#endif
166681#ifdef SQLITE_ENABLE_STMTVTAB
166682 sqlite3StmtVtabInit,
166683#endif
166684#ifdef SQLITE_ENABLE_BYTECODE_VTAB
166685 sqlite3VdbeBytecodeVtabInit,
166686#endif
166687};
166688
166689#ifndef SQLITE_AMALGAMATION
166690/* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
166691** contains the text of SQLITE_VERSION macro.
166692*/
166693SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
166694#endif
166695
166696/* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
166697** a pointer to the to the sqlite3_version[] string constant.
166698*/
166699SQLITE_API const char *sqlite3_libversion(void){ return sqlite3_version; }
166700
166701/* IMPLEMENTATION-OF: R-25063-23286 The sqlite3_sourceid() function returns a
166702** pointer to a string constant whose value is the same as the
166703** SQLITE_SOURCE_ID C preprocessor macro. Except if SQLite is built using
166704** an edited copy of the amalgamation, then the last four characters of
166705** the hash might be different from SQLITE_SOURCE_ID.
166706*/
166707/* SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } */
166708
166709/* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
166710** returns an integer equal to SQLITE_VERSION_NUMBER.
166711*/
166712SQLITE_API int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
166713
166714/* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
166715** zero if and only if SQLite was compiled with mutexing code omitted due to
166716** the SQLITE_THREADSAFE compile-time option being set to 0.
166717*/
166718SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
166719
166720/*
166721** When compiling the test fixture or with debugging enabled (on Win32),
166722** this variable being set to non-zero will cause OSTRACE macros to emit
166723** extra diagnostic information.
166724*/
166725#ifdef SQLITE_HAVE_OS_TRACE
166726# ifndef SQLITE_DEBUG_OS_TRACE
166727# define SQLITE_DEBUG_OS_TRACE 0
166728# endif
166729 int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
166730#endif
166731
166732#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
166733/*
166734** If the following function pointer is not NULL and if
166735** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
166736** I/O active are written using this function. These messages
166737** are intended for debugging activity only.
166738*/
166739SQLITE_API void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...) = 0;
166740#endif
166741
166742/*
166743** If the following global variable points to a string which is the
166744** name of a directory, then that directory will be used to store
166745** temporary files.
166746**
166747** See also the "PRAGMA temp_store_directory" SQL command.
166748*/
166749SQLITE_API char *sqlite3_temp_directory = 0;
166750
166751/*
166752** If the following global variable points to a string which is the
166753** name of a directory, then that directory will be used to store
166754** all database files specified with a relative pathname.
166755**
166756** See also the "PRAGMA data_store_directory" SQL command.
166757*/
166758SQLITE_API char *sqlite3_data_directory = 0;
166759
166760/*
166761** Initialize SQLite.
166762**
166763** This routine must be called to initialize the memory allocation,
166764** VFS, and mutex subsystems prior to doing any serious work with
166765** SQLite. But as long as you do not compile with SQLITE_OMIT_AUTOINIT
166766** this routine will be called automatically by key routines such as
166767** sqlite3_open().
166768**
166769** This routine is a no-op except on its very first call for the process,
166770** or for the first call after a call to sqlite3_shutdown.
166771**
166772** The first thread to call this routine runs the initialization to
166773** completion. If subsequent threads call this routine before the first
166774** thread has finished the initialization process, then the subsequent
166775** threads must block until the first thread finishes with the initialization.
166776**
166777** The first thread might call this routine recursively. Recursive
166778** calls to this routine should not block, of course. Otherwise the
166779** initialization process would never complete.
166780**
166781** Let X be the first thread to enter this routine. Let Y be some other
166782** thread. Then while the initial invocation of this routine by X is
166783** incomplete, it is required that:
166784**
166785** * Calls to this routine from Y must block until the outer-most
166786** call by X completes.
166787**
166788** * Recursive calls to this routine from thread X return immediately
166789** without blocking.
166790*/
166791SQLITE_API int sqlite3_initialize(void){
166792 MUTEX_LOGIC( sqlite3_mutex *pMainMtx; ) /* The main static mutex */
166793 int rc; /* Result code */
166794#ifdef SQLITE_EXTRA_INIT
166795 int bRunExtraInit = 0; /* Extra initialization needed */
166796#endif
166797
166798#ifdef SQLITE_OMIT_WSD
166799 rc = sqlite3_wsd_init(4096, 24);
166800 if( rc!=SQLITE_OK ){
166801 return rc;
166802 }
166803#endif
166804
166805 /* If the following assert() fails on some obscure processor/compiler
166806 ** combination, the work-around is to set the correct pointer
166807 ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */
166808 assert( SQLITE_PTRSIZE==sizeof(char*) );
166809
166810 /* If SQLite is already completely initialized, then this call
166811 ** to sqlite3_initialize() should be a no-op. But the initialization
166812 ** must be complete. So isInit must not be set until the very end
166813 ** of this routine.
166814 */
166815 if( sqlite3GlobalConfig.isInit ){
166816 sqlite3MemoryBarrier();
166817 return SQLITE_OK;
166818 }
166819
166820 /* Make sure the mutex subsystem is initialized. If unable to
166821 ** initialize the mutex subsystem, return early with the error.
166822 ** If the system is so sick that we are unable to allocate a mutex,
166823 ** there is not much SQLite is going to be able to do.
166824 **
166825 ** The mutex subsystem must take care of serializing its own
166826 ** initialization.
166827 */
166828 rc = sqlite3MutexInit();
166829 if( rc ) return rc;
166830
166831 /* Initialize the malloc() system and the recursive pInitMutex mutex.
166832 ** This operation is protected by the STATIC_MAIN mutex. Note that
166833 ** MutexAlloc() is called for a static mutex prior to initializing the
166834 ** malloc subsystem - this implies that the allocation of a static
166835 ** mutex must not require support from the malloc subsystem.
166836 */
166837 MUTEX_LOGIC( pMainMtx = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )
166838 sqlite3_mutex_enter(p: pMainMtx);
166839 sqlite3GlobalConfig.isMutexInit = 1;
166840 if( !sqlite3GlobalConfig.isMallocInit ){
166841 rc = sqlite3MallocInit();
166842 }
166843 if( rc==SQLITE_OK ){
166844 sqlite3GlobalConfig.isMallocInit = 1;
166845 if( !sqlite3GlobalConfig.pInitMutex ){
166846 sqlite3GlobalConfig.pInitMutex =
166847 sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
166848 if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){
166849 rc = SQLITE_NOMEM_BKPT;
166850 }
166851 }
166852 }
166853 if( rc==SQLITE_OK ){
166854 sqlite3GlobalConfig.nRefInitMutex++;
166855 }
166856 sqlite3_mutex_leave(p: pMainMtx);
166857
166858 /* If rc is not SQLITE_OK at this point, then either the malloc
166859 ** subsystem could not be initialized or the system failed to allocate
166860 ** the pInitMutex mutex. Return an error in either case. */
166861 if( rc!=SQLITE_OK ){
166862 return rc;
166863 }
166864
166865 /* Do the rest of the initialization under the recursive mutex so
166866 ** that we will be able to handle recursive calls into
166867 ** sqlite3_initialize(). The recursive calls normally come through
166868 ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other
166869 ** recursive calls might also be possible.
166870 **
166871 ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
166872 ** to the xInit method, so the xInit method need not be threadsafe.
166873 **
166874 ** The following mutex is what serializes access to the appdef pcache xInit
166875 ** methods. The sqlite3_pcache_methods.xInit() all is embedded in the
166876 ** call to sqlite3PcacheInitialize().
166877 */
166878 sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex);
166879 if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){
166880 sqlite3GlobalConfig.inProgress = 1;
166881#ifdef SQLITE_ENABLE_SQLLOG
166882 {
166883 extern void sqlite3_init_sqllog(void);
166884 sqlite3_init_sqllog();
166885 }
166886#endif
166887 memset(s: &sqlite3BuiltinFunctions, c: 0, n: sizeof(sqlite3BuiltinFunctions));
166888 sqlite3RegisterBuiltinFunctions();
166889 if( sqlite3GlobalConfig.isPCacheInit==0 ){
166890 rc = sqlite3PcacheInitialize();
166891 }
166892 if( rc==SQLITE_OK ){
166893 sqlite3GlobalConfig.isPCacheInit = 1;
166894 rc = sqlite3OsInit();
166895 }
166896#ifndef SQLITE_OMIT_DESERIALIZE
166897 if( rc==SQLITE_OK ){
166898 rc = sqlite3MemdbInit();
166899 }
166900#endif
166901 if( rc==SQLITE_OK ){
166902 sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage,
166903 sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
166904 sqlite3MemoryBarrier();
166905 sqlite3GlobalConfig.isInit = 1;
166906#ifdef SQLITE_EXTRA_INIT
166907 bRunExtraInit = 1;
166908#endif
166909 }
166910 sqlite3GlobalConfig.inProgress = 0;
166911 }
166912 sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex);
166913
166914 /* Go back under the static mutex and clean up the recursive
166915 ** mutex to prevent a resource leak.
166916 */
166917 sqlite3_mutex_enter(p: pMainMtx);
166918 sqlite3GlobalConfig.nRefInitMutex--;
166919 if( sqlite3GlobalConfig.nRefInitMutex<=0 ){
166920 assert( sqlite3GlobalConfig.nRefInitMutex==0 );
166921 sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex);
166922 sqlite3GlobalConfig.pInitMutex = 0;
166923 }
166924 sqlite3_mutex_leave(p: pMainMtx);
166925
166926 /* The following is just a sanity check to make sure SQLite has
166927 ** been compiled correctly. It is important to run this code, but
166928 ** we don't want to run it too often and soak up CPU cycles for no
166929 ** reason. So we run it once during initialization.
166930 */
166931#ifndef NDEBUG
166932#ifndef SQLITE_OMIT_FLOATING_POINT
166933 /* This section of code's only "output" is via assert() statements. */
166934 if( rc==SQLITE_OK ){
166935 u64 x = (((u64)1)<<63)-1;
166936 double y;
166937 assert(sizeof(x)==8);
166938 assert(sizeof(x)==sizeof(y));
166939 memcpy(&y, &x, 8);
166940 assert( sqlite3IsNaN(y) );
166941 }
166942#endif
166943#endif
166944
166945 /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT
166946 ** compile-time option.
166947 */
166948#ifdef SQLITE_EXTRA_INIT
166949 if( bRunExtraInit ){
166950 int SQLITE_EXTRA_INIT(const char*);
166951 rc = SQLITE_EXTRA_INIT(0);
166952 }
166953#endif
166954
166955 return rc;
166956}
166957
166958/*
166959** Undo the effects of sqlite3_initialize(). Must not be called while
166960** there are outstanding database connections or memory allocations or
166961** while any part of SQLite is otherwise in use in any thread. This
166962** routine is not threadsafe. But it is safe to invoke this routine
166963** on when SQLite is already shut down. If SQLite is already shut down
166964** when this routine is invoked, then this routine is a harmless no-op.
166965*/
166966SQLITE_API int sqlite3_shutdown(void){
166967#ifdef SQLITE_OMIT_WSD
166968 int rc = sqlite3_wsd_init(4096, 24);
166969 if( rc!=SQLITE_OK ){
166970 return rc;
166971 }
166972#endif
166973
166974 if( sqlite3GlobalConfig.isInit ){
166975#ifdef SQLITE_EXTRA_SHUTDOWN
166976 void SQLITE_EXTRA_SHUTDOWN(void);
166977 SQLITE_EXTRA_SHUTDOWN();
166978#endif
166979 sqlite3_os_end();
166980 sqlite3_reset_auto_extension();
166981 sqlite3GlobalConfig.isInit = 0;
166982 }
166983 if( sqlite3GlobalConfig.isPCacheInit ){
166984 sqlite3PcacheShutdown();
166985 sqlite3GlobalConfig.isPCacheInit = 0;
166986 }
166987 if( sqlite3GlobalConfig.isMallocInit ){
166988 sqlite3MallocEnd();
166989 sqlite3GlobalConfig.isMallocInit = 0;
166990
166991#ifndef SQLITE_OMIT_SHUTDOWN_DIRECTORIES
166992 /* The heap subsystem has now been shutdown and these values are supposed
166993 ** to be NULL or point to memory that was obtained from sqlite3_malloc(),
166994 ** which would rely on that heap subsystem; therefore, make sure these
166995 ** values cannot refer to heap memory that was just invalidated when the
166996 ** heap subsystem was shutdown. This is only done if the current call to
166997 ** this function resulted in the heap subsystem actually being shutdown.
166998 */
166999 sqlite3_data_directory = 0;
167000 sqlite3_temp_directory = 0;
167001#endif
167002 }
167003 if( sqlite3GlobalConfig.isMutexInit ){
167004 sqlite3MutexEnd();
167005 sqlite3GlobalConfig.isMutexInit = 0;
167006 }
167007
167008 return SQLITE_OK;
167009}
167010
167011/*
167012** This API allows applications to modify the global configuration of
167013** the SQLite library at run-time.
167014**
167015** This routine should only be called when there are no outstanding
167016** database connections or memory allocations. This routine is not
167017** threadsafe. Failure to heed these warnings can lead to unpredictable
167018** behavior.
167019*/
167020SQLITE_API int sqlite3_config(int op, ...){
167021 va_list ap;
167022 int rc = SQLITE_OK;
167023
167024 /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while
167025 ** the SQLite library is in use. */
167026 if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE_BKPT;
167027
167028 va_start(ap, op);
167029 switch( op ){
167030
167031 /* Mutex configuration options are only available in a threadsafe
167032 ** compile.
167033 */
167034#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-54466-46756 */
167035 case SQLITE_CONFIG_SINGLETHREAD: {
167036 /* EVIDENCE-OF: R-02748-19096 This option sets the threading mode to
167037 ** Single-thread. */
167038 sqlite3GlobalConfig.bCoreMutex = 0; /* Disable mutex on core */
167039 sqlite3GlobalConfig.bFullMutex = 0; /* Disable mutex on connections */
167040 break;
167041 }
167042#endif
167043#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-20520-54086 */
167044 case SQLITE_CONFIG_MULTITHREAD: {
167045 /* EVIDENCE-OF: R-14374-42468 This option sets the threading mode to
167046 ** Multi-thread. */
167047 sqlite3GlobalConfig.bCoreMutex = 1; /* Enable mutex on core */
167048 sqlite3GlobalConfig.bFullMutex = 0; /* Disable mutex on connections */
167049 break;
167050 }
167051#endif
167052#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-59593-21810 */
167053 case SQLITE_CONFIG_SERIALIZED: {
167054 /* EVIDENCE-OF: R-41220-51800 This option sets the threading mode to
167055 ** Serialized. */
167056 sqlite3GlobalConfig.bCoreMutex = 1; /* Enable mutex on core */
167057 sqlite3GlobalConfig.bFullMutex = 1; /* Enable mutex on connections */
167058 break;
167059 }
167060#endif
167061#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-63666-48755 */
167062 case SQLITE_CONFIG_MUTEX: {
167063 /* Specify an alternative mutex implementation */
167064 sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*);
167065 break;
167066 }
167067#endif
167068#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-14450-37597 */
167069 case SQLITE_CONFIG_GETMUTEX: {
167070 /* Retrieve the current mutex implementation */
167071 *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex;
167072 break;
167073 }
167074#endif
167075
167076 case SQLITE_CONFIG_MALLOC: {
167077 /* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a
167078 ** single argument which is a pointer to an instance of the
167079 ** sqlite3_mem_methods structure. The argument specifies alternative
167080 ** low-level memory allocation routines to be used in place of the memory
167081 ** allocation routines built into SQLite. */
167082 sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*);
167083 break;
167084 }
167085 case SQLITE_CONFIG_GETMALLOC: {
167086 /* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a
167087 ** single argument which is a pointer to an instance of the
167088 ** sqlite3_mem_methods structure. The sqlite3_mem_methods structure is
167089 ** filled with the currently defined memory allocation routines. */
167090 if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
167091 *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m;
167092 break;
167093 }
167094 case SQLITE_CONFIG_MEMSTATUS: {
167095 /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes
167096 ** single argument of type int, interpreted as a boolean, which enables
167097 ** or disables the collection of memory allocation statistics. */
167098 sqlite3GlobalConfig.bMemstat = va_arg(ap, int);
167099 break;
167100 }
167101 case SQLITE_CONFIG_SMALL_MALLOC: {
167102 sqlite3GlobalConfig.bSmallMalloc = va_arg(ap, int);
167103 break;
167104 }
167105 case SQLITE_CONFIG_PAGECACHE: {
167106 /* EVIDENCE-OF: R-18761-36601 There are three arguments to
167107 ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory (pMem),
167108 ** the size of each page cache line (sz), and the number of cache lines
167109 ** (N). */
167110 sqlite3GlobalConfig.pPage = va_arg(ap, void*);
167111 sqlite3GlobalConfig.szPage = va_arg(ap, int);
167112 sqlite3GlobalConfig.nPage = va_arg(ap, int);
167113 break;
167114 }
167115 case SQLITE_CONFIG_PCACHE_HDRSZ: {
167116 /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes
167117 ** a single parameter which is a pointer to an integer and writes into
167118 ** that integer the number of extra bytes per page required for each page
167119 ** in SQLITE_CONFIG_PAGECACHE. */
167120 *va_arg(ap, int*) =
167121 sqlite3HeaderSizeBtree() +
167122 sqlite3HeaderSizePcache() +
167123 sqlite3HeaderSizePcache1();
167124 break;
167125 }
167126
167127 case SQLITE_CONFIG_PCACHE: {
167128 /* no-op */
167129 break;
167130 }
167131 case SQLITE_CONFIG_GETPCACHE: {
167132 /* now an error */
167133 rc = SQLITE_ERROR;
167134 break;
167135 }
167136
167137 case SQLITE_CONFIG_PCACHE2: {
167138 /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a
167139 ** single argument which is a pointer to an sqlite3_pcache_methods2
167140 ** object. This object specifies the interface to a custom page cache
167141 ** implementation. */
167142 sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*);
167143 break;
167144 }
167145 case SQLITE_CONFIG_GETPCACHE2: {
167146 /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a
167147 ** single argument which is a pointer to an sqlite3_pcache_methods2
167148 ** object. SQLite copies of the current page cache implementation into
167149 ** that object. */
167150 if( sqlite3GlobalConfig.pcache2.xInit==0 ){
167151 sqlite3PCacheSetDefault();
167152 }
167153 *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2;
167154 break;
167155 }
167156
167157/* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only
167158** available if SQLite is compiled with either SQLITE_ENABLE_MEMSYS3 or
167159** SQLITE_ENABLE_MEMSYS5 and returns SQLITE_ERROR if invoked otherwise. */
167160#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
167161 case SQLITE_CONFIG_HEAP: {
167162 /* EVIDENCE-OF: R-19854-42126 There are three arguments to
167163 ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the
167164 ** number of bytes in the memory buffer, and the minimum allocation size.
167165 */
167166 sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
167167 sqlite3GlobalConfig.nHeap = va_arg(ap, int);
167168 sqlite3GlobalConfig.mnReq = va_arg(ap, int);
167169
167170 if( sqlite3GlobalConfig.mnReq<1 ){
167171 sqlite3GlobalConfig.mnReq = 1;
167172 }else if( sqlite3GlobalConfig.mnReq>(1<<12) ){
167173 /* cap min request size at 2^12 */
167174 sqlite3GlobalConfig.mnReq = (1<<12);
167175 }
167176
167177 if( sqlite3GlobalConfig.pHeap==0 ){
167178 /* EVIDENCE-OF: R-49920-60189 If the first pointer (the memory pointer)
167179 ** is NULL, then SQLite reverts to using its default memory allocator
167180 ** (the system malloc() implementation), undoing any prior invocation of
167181 ** SQLITE_CONFIG_MALLOC.
167182 **
167183 ** Setting sqlite3GlobalConfig.m to all zeros will cause malloc to
167184 ** revert to its default implementation when sqlite3_initialize() is run
167185 */
167186 memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));
167187 }else{
167188 /* EVIDENCE-OF: R-61006-08918 If the memory pointer is not NULL then the
167189 ** alternative memory allocator is engaged to handle all of SQLites
167190 ** memory allocation needs. */
167191#ifdef SQLITE_ENABLE_MEMSYS3
167192 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
167193#endif
167194#ifdef SQLITE_ENABLE_MEMSYS5
167195 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
167196#endif
167197 }
167198 break;
167199 }
167200#endif
167201
167202 case SQLITE_CONFIG_LOOKASIDE: {
167203 sqlite3GlobalConfig.szLookaside = va_arg(ap, int);
167204 sqlite3GlobalConfig.nLookaside = va_arg(ap, int);
167205 break;
167206 }
167207
167208 /* Record a pointer to the logger function and its first argument.
167209 ** The default is NULL. Logging is disabled if the function pointer is
167210 ** NULL.
167211 */
167212 case SQLITE_CONFIG_LOG: {
167213 /* MSVC is picky about pulling func ptrs from va lists.
167214 ** http://support.microsoft.com/kb/47961
167215 ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*));
167216 */
167217 typedef void(*LOGFUNC_t)(void*,int,const char*);
167218 sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t);
167219 sqlite3GlobalConfig.pLogArg = va_arg(ap, void*);
167220 break;
167221 }
167222
167223 /* EVIDENCE-OF: R-55548-33817 The compile-time setting for URI filenames
167224 ** can be changed at start-time using the
167225 ** sqlite3_config(SQLITE_CONFIG_URI,1) or
167226 ** sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls.
167227 */
167228 case SQLITE_CONFIG_URI: {
167229 /* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single
167230 ** argument of type int. If non-zero, then URI handling is globally
167231 ** enabled. If the parameter is zero, then URI handling is globally
167232 ** disabled. */
167233 sqlite3GlobalConfig.bOpenUri = va_arg(ap, int);
167234 break;
167235 }
167236
167237 case SQLITE_CONFIG_COVERING_INDEX_SCAN: {
167238 /* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN
167239 ** option takes a single integer argument which is interpreted as a
167240 ** boolean in order to enable or disable the use of covering indices for
167241 ** full table scans in the query optimizer. */
167242 sqlite3GlobalConfig.bUseCis = va_arg(ap, int);
167243 break;
167244 }
167245
167246#ifdef SQLITE_ENABLE_SQLLOG
167247 case SQLITE_CONFIG_SQLLOG: {
167248 typedef void(*SQLLOGFUNC_t)(void*, sqlite3*, const char*, int);
167249 sqlite3GlobalConfig.xSqllog = va_arg(ap, SQLLOGFUNC_t);
167250 sqlite3GlobalConfig.pSqllogArg = va_arg(ap, void *);
167251 break;
167252 }
167253#endif
167254
167255 case SQLITE_CONFIG_MMAP_SIZE: {
167256 /* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit
167257 ** integer (sqlite3_int64) values that are the default mmap size limit
167258 ** (the default setting for PRAGMA mmap_size) and the maximum allowed
167259 ** mmap size limit. */
167260 sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64);
167261 sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64);
167262 /* EVIDENCE-OF: R-53367-43190 If either argument to this option is
167263 ** negative, then that argument is changed to its compile-time default.
167264 **
167265 ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be
167266 ** silently truncated if necessary so that it does not exceed the
167267 ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE
167268 ** compile-time option.
167269 */
167270 if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ){
167271 mxMmap = SQLITE_MAX_MMAP_SIZE;
167272 }
167273 if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE;
167274 if( szMmap>mxMmap) szMmap = mxMmap;
167275 sqlite3GlobalConfig.mxMmap = mxMmap;
167276 sqlite3GlobalConfig.szMmap = szMmap;
167277 break;
167278 }
167279
167280#if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) /* IMP: R-04780-55815 */
167281 case SQLITE_CONFIG_WIN32_HEAPSIZE: {
167282 /* EVIDENCE-OF: R-34926-03360 SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit
167283 ** unsigned integer value that specifies the maximum size of the created
167284 ** heap. */
167285 sqlite3GlobalConfig.nHeap = va_arg(ap, int);
167286 break;
167287 }
167288#endif
167289
167290 case SQLITE_CONFIG_PMASZ: {
167291 sqlite3GlobalConfig.szPma = va_arg(ap, unsigned int);
167292 break;
167293 }
167294
167295 case SQLITE_CONFIG_STMTJRNL_SPILL: {
167296 sqlite3GlobalConfig.nStmtSpill = va_arg(ap, int);
167297 break;
167298 }
167299
167300#ifdef SQLITE_ENABLE_SORTER_REFERENCES
167301 case SQLITE_CONFIG_SORTERREF_SIZE: {
167302 int iVal = va_arg(ap, int);
167303 if( iVal<0 ){
167304 iVal = SQLITE_DEFAULT_SORTERREF_SIZE;
167305 }
167306 sqlite3GlobalConfig.szSorterRef = (u32)iVal;
167307 break;
167308 }
167309#endif /* SQLITE_ENABLE_SORTER_REFERENCES */
167310
167311#ifndef SQLITE_OMIT_DESERIALIZE
167312 case SQLITE_CONFIG_MEMDB_MAXSIZE: {
167313 sqlite3GlobalConfig.mxMemdbSize = va_arg(ap, sqlite3_int64);
167314 break;
167315 }
167316#endif /* SQLITE_OMIT_DESERIALIZE */
167317
167318 default: {
167319 rc = SQLITE_ERROR;
167320 break;
167321 }
167322 }
167323 va_end(ap);
167324 return rc;
167325}
167326
167327/*
167328** Set up the lookaside buffers for a database connection.
167329** Return SQLITE_OK on success.
167330** If lookaside is already active, return SQLITE_BUSY.
167331**
167332** The sz parameter is the number of bytes in each lookaside slot.
167333** The cnt parameter is the number of slots. If pStart is NULL the
167334** space for the lookaside memory is obtained from sqlite3_malloc().
167335** If pStart is not NULL then it is sz*cnt bytes of memory to use for
167336** the lookaside memory.
167337*/
167338static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
167339#ifndef SQLITE_OMIT_LOOKASIDE
167340 void *pStart;
167341 sqlite3_int64 szAlloc = sz*(sqlite3_int64)cnt;
167342 int nBig; /* Number of full-size slots */
167343 int nSm; /* Number smaller LOOKASIDE_SMALL-byte slots */
167344
167345 if( sqlite3LookasideUsed(db,pHighwater: 0)>0 ){
167346 return SQLITE_BUSY;
167347 }
167348 /* Free any existing lookaside buffer for this handle before
167349 ** allocating a new one so we don't have to have space for
167350 ** both at the same time.
167351 */
167352 if( db->lookaside.bMalloced ){
167353 sqlite3_free(p: db->lookaside.pStart);
167354 }
167355 /* The size of a lookaside slot after ROUNDDOWN8 needs to be larger
167356 ** than a pointer to be useful.
167357 */
167358 sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */
167359 if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0;
167360 if( cnt<0 ) cnt = 0;
167361 if( sz==0 || cnt==0 ){
167362 sz = 0;
167363 pStart = 0;
167364 }else if( pBuf==0 ){
167365 sqlite3BeginBenignMalloc();
167366 pStart = sqlite3Malloc( n: szAlloc ); /* IMP: R-61949-35727 */
167367 sqlite3EndBenignMalloc();
167368 if( pStart ) szAlloc = sqlite3MallocSize(p: pStart);
167369 }else{
167370 pStart = pBuf;
167371 }
167372#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
167373 if( sz>=LOOKASIDE_SMALL*3 ){
167374 nBig = szAlloc/(3*LOOKASIDE_SMALL+sz);
167375 nSm = (szAlloc - sz*nBig)/LOOKASIDE_SMALL;
167376 }else if( sz>=LOOKASIDE_SMALL*2 ){
167377 nBig = szAlloc/(LOOKASIDE_SMALL+sz);
167378 nSm = (szAlloc - sz*nBig)/LOOKASIDE_SMALL;
167379 }else
167380#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
167381 if( sz>0 ){
167382 nBig = szAlloc/sz;
167383 nSm = 0;
167384 }else{
167385 nBig = nSm = 0;
167386 }
167387 db->lookaside.pStart = pStart;
167388 db->lookaside.pInit = 0;
167389 db->lookaside.pFree = 0;
167390 db->lookaside.sz = (u16)sz;
167391 db->lookaside.szTrue = (u16)sz;
167392 if( pStart ){
167393 int i;
167394 LookasideSlot *p;
167395 assert( sz > (int)sizeof(LookasideSlot*) );
167396 p = (LookasideSlot*)pStart;
167397 for(i=0; i<nBig; i++){
167398 p->pNext = db->lookaside.pInit;
167399 db->lookaside.pInit = p;
167400 p = (LookasideSlot*)&((u8*)p)[sz];
167401 }
167402#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
167403 db->lookaside.pSmallInit = 0;
167404 db->lookaside.pSmallFree = 0;
167405 db->lookaside.pMiddle = p;
167406 for(i=0; i<nSm; i++){
167407 p->pNext = db->lookaside.pSmallInit;
167408 db->lookaside.pSmallInit = p;
167409 p = (LookasideSlot*)&((u8*)p)[LOOKASIDE_SMALL];
167410 }
167411#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
167412 assert( ((uptr)p)<=szAlloc + (uptr)pStart );
167413 db->lookaside.pEnd = p;
167414 db->lookaside.bDisable = 0;
167415 db->lookaside.bMalloced = pBuf==0 ?1:0;
167416 db->lookaside.nSlot = nBig+nSm;
167417 }else{
167418 db->lookaside.pStart = db;
167419#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
167420 db->lookaside.pSmallInit = 0;
167421 db->lookaside.pSmallFree = 0;
167422 db->lookaside.pMiddle = db;
167423#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
167424 db->lookaside.pEnd = db;
167425 db->lookaside.bDisable = 1;
167426 db->lookaside.sz = 0;
167427 db->lookaside.bMalloced = 0;
167428 db->lookaside.nSlot = 0;
167429 }
167430 assert( sqlite3LookasideUsed(db,0)==0 );
167431#endif /* SQLITE_OMIT_LOOKASIDE */
167432 return SQLITE_OK;
167433}
167434
167435/*
167436** Return the mutex associated with a database connection.
167437*/
167438SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
167439#ifdef SQLITE_ENABLE_API_ARMOR
167440 if( !sqlite3SafetyCheckOk(db) ){
167441 (void)SQLITE_MISUSE_BKPT;
167442 return 0;
167443 }
167444#endif
167445 return db->mutex;
167446}
167447
167448/*
167449** Free up as much memory as we can from the given database
167450** connection.
167451*/
167452SQLITE_API int sqlite3_db_release_memory(sqlite3 *db){
167453 int i;
167454
167455#ifdef SQLITE_ENABLE_API_ARMOR
167456 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
167457#endif
167458 sqlite3_mutex_enter(p: db->mutex);
167459 sqlite3BtreeEnterAll(db);
167460 for(i=0; i<db->nDb; i++){
167461 Btree *pBt = db->aDb[i].pBt;
167462 if( pBt ){
167463 Pager *pPager = sqlite3BtreePager(p: pBt);
167464 sqlite3PagerShrink(pPager);
167465 }
167466 }
167467 sqlite3BtreeLeaveAll(db);
167468 sqlite3_mutex_leave(p: db->mutex);
167469 return SQLITE_OK;
167470}
167471
167472/*
167473** Flush any dirty pages in the pager-cache for any attached database
167474** to disk.
167475*/
167476SQLITE_API int sqlite3_db_cacheflush(sqlite3 *db){
167477 int i;
167478 int rc = SQLITE_OK;
167479 int bSeenBusy = 0;
167480
167481#ifdef SQLITE_ENABLE_API_ARMOR
167482 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
167483#endif
167484 sqlite3_mutex_enter(p: db->mutex);
167485 sqlite3BtreeEnterAll(db);
167486 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
167487 Btree *pBt = db->aDb[i].pBt;
167488 if( pBt && sqlite3BtreeTxnState(p: pBt)==SQLITE_TXN_WRITE ){
167489 Pager *pPager = sqlite3BtreePager(p: pBt);
167490 rc = sqlite3PagerFlush(pPager);
167491 if( rc==SQLITE_BUSY ){
167492 bSeenBusy = 1;
167493 rc = SQLITE_OK;
167494 }
167495 }
167496 }
167497 sqlite3BtreeLeaveAll(db);
167498 sqlite3_mutex_leave(p: db->mutex);
167499 return ((rc==SQLITE_OK && bSeenBusy) ? SQLITE_BUSY : rc);
167500}
167501
167502/*
167503** Configuration settings for an individual database connection
167504*/
167505SQLITE_API int sqlite3_db_config(sqlite3 *db, int op, ...){
167506 va_list ap;
167507 int rc;
167508 va_start(ap, op);
167509 switch( op ){
167510 case SQLITE_DBCONFIG_MAINDBNAME: {
167511 /* IMP: R-06824-28531 */
167512 /* IMP: R-36257-52125 */
167513 db->aDb[0].zDbSName = va_arg(ap,char*);
167514 rc = SQLITE_OK;
167515 break;
167516 }
167517 case SQLITE_DBCONFIG_LOOKASIDE: {
167518 void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */
167519 int sz = va_arg(ap, int); /* IMP: R-47871-25994 */
167520 int cnt = va_arg(ap, int); /* IMP: R-04460-53386 */
167521 rc = setupLookaside(db, pBuf, sz, cnt);
167522 break;
167523 }
167524 default: {
167525 static const struct {
167526 int op; /* The opcode */
167527 u32 mask; /* Mask of the bit in sqlite3.flags to set/clear */
167528 } aFlagOp[] = {
167529 { SQLITE_DBCONFIG_ENABLE_FKEY, SQLITE_ForeignKeys },
167530 { SQLITE_DBCONFIG_ENABLE_TRIGGER, SQLITE_EnableTrigger },
167531 { SQLITE_DBCONFIG_ENABLE_VIEW, SQLITE_EnableView },
167532 { SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, SQLITE_Fts3Tokenizer },
167533 { SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, SQLITE_LoadExtension },
167534 { SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE, SQLITE_NoCkptOnClose },
167535 { SQLITE_DBCONFIG_ENABLE_QPSG, SQLITE_EnableQPSG },
167536 { SQLITE_DBCONFIG_TRIGGER_EQP, SQLITE_TriggerEQP },
167537 { SQLITE_DBCONFIG_RESET_DATABASE, SQLITE_ResetDatabase },
167538 { SQLITE_DBCONFIG_DEFENSIVE, SQLITE_Defensive },
167539 { SQLITE_DBCONFIG_WRITABLE_SCHEMA, SQLITE_WriteSchema|
167540 SQLITE_NoSchemaError },
167541 { SQLITE_DBCONFIG_LEGACY_ALTER_TABLE, SQLITE_LegacyAlter },
167542 { SQLITE_DBCONFIG_DQS_DDL, SQLITE_DqsDDL },
167543 { SQLITE_DBCONFIG_DQS_DML, SQLITE_DqsDML },
167544 { SQLITE_DBCONFIG_LEGACY_FILE_FORMAT, SQLITE_LegacyFileFmt },
167545 { SQLITE_DBCONFIG_TRUSTED_SCHEMA, SQLITE_TrustedSchema },
167546 };
167547 unsigned int i;
167548 rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
167549 for(i=0; i<ArraySize(aFlagOp); i++){
167550 if( aFlagOp[i].op==op ){
167551 int onoff = va_arg(ap, int);
167552 int *pRes = va_arg(ap, int*);
167553 u64 oldFlags = db->flags;
167554 if( onoff>0 ){
167555 db->flags |= aFlagOp[i].mask;
167556 }else if( onoff==0 ){
167557 db->flags &= ~(u64)aFlagOp[i].mask;
167558 }
167559 if( oldFlags!=db->flags ){
167560 sqlite3ExpirePreparedStatements(db, iCode: 0);
167561 }
167562 if( pRes ){
167563 *pRes = (db->flags & aFlagOp[i].mask)!=0;
167564 }
167565 rc = SQLITE_OK;
167566 break;
167567 }
167568 }
167569 break;
167570 }
167571 }
167572 va_end(ap);
167573 return rc;
167574}
167575
167576/*
167577** This is the default collating function named "BINARY" which is always
167578** available.
167579*/
167580static int binCollFunc(
167581 void *NotUsed,
167582 int nKey1, const void *pKey1,
167583 int nKey2, const void *pKey2
167584){
167585 int rc, n;
167586 UNUSED_PARAMETER(NotUsed);
167587 n = nKey1<nKey2 ? nKey1 : nKey2;
167588 /* EVIDENCE-OF: R-65033-28449 The built-in BINARY collation compares
167589 ** strings byte by byte using the memcmp() function from the standard C
167590 ** library. */
167591 assert( pKey1 && pKey2 );
167592 rc = memcmp(s1: pKey1, s2: pKey2, n: n);
167593 if( rc==0 ){
167594 rc = nKey1 - nKey2;
167595 }
167596 return rc;
167597}
167598
167599/*
167600** This is the collating function named "RTRIM" which is always
167601** available. Ignore trailing spaces.
167602*/
167603static int rtrimCollFunc(
167604 void *pUser,
167605 int nKey1, const void *pKey1,
167606 int nKey2, const void *pKey2
167607){
167608 const u8 *pK1 = (const u8*)pKey1;
167609 const u8 *pK2 = (const u8*)pKey2;
167610 while( nKey1 && pK1[nKey1-1]==' ' ) nKey1--;
167611 while( nKey2 && pK2[nKey2-1]==' ' ) nKey2--;
167612 return binCollFunc(NotUsed: pUser, nKey1, pKey1, nKey2, pKey2);
167613}
167614
167615/*
167616** Return true if CollSeq is the default built-in BINARY.
167617*/
167618SQLITE_PRIVATE int sqlite3IsBinary(const CollSeq *p){
167619 assert( p==0 || p->xCmp!=binCollFunc || strcmp(p->zName,"BINARY")==0 );
167620 return p==0 || p->xCmp==binCollFunc;
167621}
167622
167623/*
167624** Another built-in collating sequence: NOCASE.
167625**
167626** This collating sequence is intended to be used for "case independent
167627** comparison". SQLite's knowledge of upper and lower case equivalents
167628** extends only to the 26 characters used in the English language.
167629**
167630** At the moment there is only a UTF-8 implementation.
167631*/
167632static int nocaseCollatingFunc(
167633 void *NotUsed,
167634 int nKey1, const void *pKey1,
167635 int nKey2, const void *pKey2
167636){
167637 int r = sqlite3StrNICmp(
167638 zLeft: (const char *)pKey1, zRight: (const char *)pKey2, N: (nKey1<nKey2)?nKey1:nKey2);
167639 UNUSED_PARAMETER(NotUsed);
167640 if( 0==r ){
167641 r = nKey1-nKey2;
167642 }
167643 return r;
167644}
167645
167646/*
167647** Return the ROWID of the most recent insert
167648*/
167649SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
167650#ifdef SQLITE_ENABLE_API_ARMOR
167651 if( !sqlite3SafetyCheckOk(db) ){
167652 (void)SQLITE_MISUSE_BKPT;
167653 return 0;
167654 }
167655#endif
167656 return db->lastRowid;
167657}
167658
167659/*
167660** Set the value returned by the sqlite3_last_insert_rowid() API function.
167661*/
167662SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3 *db, sqlite3_int64 iRowid){
167663#ifdef SQLITE_ENABLE_API_ARMOR
167664 if( !sqlite3SafetyCheckOk(db) ){
167665 (void)SQLITE_MISUSE_BKPT;
167666 return;
167667 }
167668#endif
167669 sqlite3_mutex_enter(p: db->mutex);
167670 db->lastRowid = iRowid;
167671 sqlite3_mutex_leave(p: db->mutex);
167672}
167673
167674/*
167675** Return the number of changes in the most recent call to sqlite3_exec().
167676*/
167677SQLITE_API sqlite3_int64 sqlite3_changes64(sqlite3 *db){
167678#ifdef SQLITE_ENABLE_API_ARMOR
167679 if( !sqlite3SafetyCheckOk(db) ){
167680 (void)SQLITE_MISUSE_BKPT;
167681 return 0;
167682 }
167683#endif
167684 return db->nChange;
167685}
167686SQLITE_API int sqlite3_changes(sqlite3 *db){
167687 return (int)sqlite3_changes64(db);
167688}
167689
167690/*
167691** Return the number of changes since the database handle was opened.
167692*/
167693SQLITE_API sqlite3_int64 sqlite3_total_changes64(sqlite3 *db){
167694#ifdef SQLITE_ENABLE_API_ARMOR
167695 if( !sqlite3SafetyCheckOk(db) ){
167696 (void)SQLITE_MISUSE_BKPT;
167697 return 0;
167698 }
167699#endif
167700 return db->nTotalChange;
167701}
167702SQLITE_API int sqlite3_total_changes(sqlite3 *db){
167703 return (int)sqlite3_total_changes64(db);
167704}
167705
167706/*
167707** Close all open savepoints. This function only manipulates fields of the
167708** database handle object, it does not close any savepoints that may be open
167709** at the b-tree/pager level.
167710*/
167711SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *db){
167712 while( db->pSavepoint ){
167713 Savepoint *pTmp = db->pSavepoint;
167714 db->pSavepoint = pTmp->pNext;
167715 sqlite3DbFree(db, p: pTmp);
167716 }
167717 db->nSavepoint = 0;
167718 db->nStatement = 0;
167719 db->isTransactionSavepoint = 0;
167720}
167721
167722/*
167723** Invoke the destructor function associated with FuncDef p, if any. Except,
167724** if this is not the last copy of the function, do not invoke it. Multiple
167725** copies of a single function are created when create_function() is called
167726** with SQLITE_ANY as the encoding.
167727*/
167728static void functionDestroy(sqlite3 *db, FuncDef *p){
167729 FuncDestructor *pDestructor;
167730 assert( (p->funcFlags & SQLITE_FUNC_BUILTIN)==0 );
167731 pDestructor = p->u.pDestructor;
167732 if( pDestructor ){
167733 pDestructor->nRef--;
167734 if( pDestructor->nRef==0 ){
167735 pDestructor->xDestroy(pDestructor->pUserData);
167736 sqlite3DbFree(db, p: pDestructor);
167737 }
167738 }
167739}
167740
167741/*
167742** Disconnect all sqlite3_vtab objects that belong to database connection
167743** db. This is called when db is being closed.
167744*/
167745static void disconnectAllVtab(sqlite3 *db){
167746#ifndef SQLITE_OMIT_VIRTUALTABLE
167747 int i;
167748 HashElem *p;
167749 sqlite3BtreeEnterAll(db);
167750 for(i=0; i<db->nDb; i++){
167751 Schema *pSchema = db->aDb[i].pSchema;
167752 if( pSchema ){
167753 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
167754 Table *pTab = (Table *)sqliteHashData(p);
167755 if( IsVirtual(pTab) ) sqlite3VtabDisconnect(db, p: pTab);
167756 }
167757 }
167758 }
167759 for(p=sqliteHashFirst(&db->aModule); p; p=sqliteHashNext(p)){
167760 Module *pMod = (Module *)sqliteHashData(p);
167761 if( pMod->pEpoTab ){
167762 sqlite3VtabDisconnect(db, p: pMod->pEpoTab);
167763 }
167764 }
167765 sqlite3VtabUnlockList(db);
167766 sqlite3BtreeLeaveAll(db);
167767#else
167768 UNUSED_PARAMETER(db);
167769#endif
167770}
167771
167772/*
167773** Return TRUE if database connection db has unfinalized prepared
167774** statements or unfinished sqlite3_backup objects.
167775*/
167776static int connectionIsBusy(sqlite3 *db){
167777 int j;
167778 assert( sqlite3_mutex_held(db->mutex) );
167779 if( db->pVdbe ) return 1;
167780 for(j=0; j<db->nDb; j++){
167781 Btree *pBt = db->aDb[j].pBt;
167782 if( pBt && sqlite3BtreeIsInBackup(p: pBt) ) return 1;
167783 }
167784 return 0;
167785}
167786
167787/*
167788** Close an existing SQLite database
167789*/
167790static int sqlite3Close(sqlite3 *db, int forceZombie){
167791 if( !db ){
167792 /* EVIDENCE-OF: R-63257-11740 Calling sqlite3_close() or
167793 ** sqlite3_close_v2() with a NULL pointer argument is a harmless no-op. */
167794 return SQLITE_OK;
167795 }
167796 if( !sqlite3SafetyCheckSickOrOk(db) ){
167797 return SQLITE_MISUSE_BKPT;
167798 }
167799 sqlite3_mutex_enter(p: db->mutex);
167800 if( db->mTrace & SQLITE_TRACE_CLOSE ){
167801 db->trace.xV2(SQLITE_TRACE_CLOSE, db->pTraceArg, db, 0);
167802 }
167803
167804 /* Force xDisconnect calls on all virtual tables */
167805 disconnectAllVtab(db);
167806
167807 /* If a transaction is open, the disconnectAllVtab() call above
167808 ** will not have called the xDisconnect() method on any virtual
167809 ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
167810 ** call will do so. We need to do this before the check for active
167811 ** SQL statements below, as the v-table implementation may be storing
167812 ** some prepared statements internally.
167813 */
167814 sqlite3VtabRollback(db);
167815
167816 /* Legacy behavior (sqlite3_close() behavior) is to return
167817 ** SQLITE_BUSY if the connection can not be closed immediately.
167818 */
167819 if( !forceZombie && connectionIsBusy(db) ){
167820 sqlite3ErrorWithMsg(db, SQLITE_BUSY, zFormat: "unable to close due to unfinalized "
167821 "statements or unfinished backups");
167822 sqlite3_mutex_leave(p: db->mutex);
167823 return SQLITE_BUSY;
167824 }
167825
167826#ifdef SQLITE_ENABLE_SQLLOG
167827 if( sqlite3GlobalConfig.xSqllog ){
167828 /* Closing the handle. Fourth parameter is passed the value 2. */
167829 sqlite3GlobalConfig.xSqllog(sqlite3GlobalConfig.pSqllogArg, db, 0, 2);
167830 }
167831#endif
167832
167833 /* Convert the connection into a zombie and then close it.
167834 */
167835 db->eOpenState = SQLITE_STATE_ZOMBIE;
167836 sqlite3LeaveMutexAndCloseZombie(db);
167837 return SQLITE_OK;
167838}
167839
167840/*
167841** Return the transaction state for a single databse, or the maximum
167842** transaction state over all attached databases if zSchema is null.
167843*/
167844SQLITE_API int sqlite3_txn_state(sqlite3 *db, const char *zSchema){
167845 int iDb, nDb;
167846 int iTxn = -1;
167847#ifdef SQLITE_ENABLE_API_ARMOR
167848 if( !sqlite3SafetyCheckOk(db) ){
167849 (void)SQLITE_MISUSE_BKPT;
167850 return -1;
167851 }
167852#endif
167853 sqlite3_mutex_enter(p: db->mutex);
167854 if( zSchema ){
167855 nDb = iDb = sqlite3FindDbName(db, zName: zSchema);
167856 if( iDb<0 ) nDb--;
167857 }else{
167858 iDb = 0;
167859 nDb = db->nDb-1;
167860 }
167861 for(; iDb<=nDb; iDb++){
167862 Btree *pBt = db->aDb[iDb].pBt;
167863 int x = pBt!=0 ? sqlite3BtreeTxnState(p: pBt) : SQLITE_TXN_NONE;
167864 if( x>iTxn ) iTxn = x;
167865 }
167866 sqlite3_mutex_leave(p: db->mutex);
167867 return iTxn;
167868}
167869
167870/*
167871** Two variations on the public interface for closing a database
167872** connection. The sqlite3_close() version returns SQLITE_BUSY and
167873** leaves the connection open if there are unfinalized prepared
167874** statements or unfinished sqlite3_backups. The sqlite3_close_v2()
167875** version forces the connection to become a zombie if there are
167876** unclosed resources, and arranges for deallocation when the last
167877** prepare statement or sqlite3_backup closes.
167878*/
167879SQLITE_API int sqlite3_close(sqlite3 *db){ return sqlite3Close(db,forceZombie: 0); }
167880SQLITE_API int sqlite3_close_v2(sqlite3 *db){ return sqlite3Close(db,forceZombie: 1); }
167881
167882
167883/*
167884** Close the mutex on database connection db.
167885**
167886** Furthermore, if database connection db is a zombie (meaning that there
167887** has been a prior call to sqlite3_close(db) or sqlite3_close_v2(db)) and
167888** every sqlite3_stmt has now been finalized and every sqlite3_backup has
167889** finished, then free all resources.
167890*/
167891SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){
167892 HashElem *i; /* Hash table iterator */
167893 int j;
167894
167895 /* If there are outstanding sqlite3_stmt or sqlite3_backup objects
167896 ** or if the connection has not yet been closed by sqlite3_close_v2(),
167897 ** then just leave the mutex and return.
167898 */
167899 if( db->eOpenState!=SQLITE_STATE_ZOMBIE || connectionIsBusy(db) ){
167900 sqlite3_mutex_leave(p: db->mutex);
167901 return;
167902 }
167903
167904 /* If we reach this point, it means that the database connection has
167905 ** closed all sqlite3_stmt and sqlite3_backup objects and has been
167906 ** passed to sqlite3_close (meaning that it is a zombie). Therefore,
167907 ** go ahead and free all resources.
167908 */
167909
167910 /* If a transaction is open, roll it back. This also ensures that if
167911 ** any database schemas have been modified by an uncommitted transaction
167912 ** they are reset. And that the required b-tree mutex is held to make
167913 ** the pager rollback and schema reset an atomic operation. */
167914 sqlite3RollbackAll(db, SQLITE_OK);
167915
167916 /* Free any outstanding Savepoint structures. */
167917 sqlite3CloseSavepoints(db);
167918
167919 /* Close all database connections */
167920 for(j=0; j<db->nDb; j++){
167921 struct Db *pDb = &db->aDb[j];
167922 if( pDb->pBt ){
167923 sqlite3BtreeClose(p: pDb->pBt);
167924 pDb->pBt = 0;
167925 if( j!=1 ){
167926 pDb->pSchema = 0;
167927 }
167928 }
167929 }
167930 /* Clear the TEMP schema separately and last */
167931 if( db->aDb[1].pSchema ){
167932 sqlite3SchemaClear(p: db->aDb[1].pSchema);
167933 }
167934 sqlite3VtabUnlockList(db);
167935
167936 /* Free up the array of auxiliary databases */
167937 sqlite3CollapseDatabaseArray(db);
167938 assert( db->nDb<=2 );
167939 assert( db->aDb==db->aDbStatic );
167940
167941 /* Tell the code in notify.c that the connection no longer holds any
167942 ** locks and does not require any further unlock-notify callbacks.
167943 */
167944 sqlite3ConnectionClosed(db);
167945
167946 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
167947 FuncDef *pNext, *p;
167948 p = sqliteHashData(i);
167949 do{
167950 functionDestroy(db, p);
167951 pNext = p->pNext;
167952 sqlite3DbFree(db, p);
167953 p = pNext;
167954 }while( p );
167955 }
167956 sqlite3HashClear(pH: &db->aFunc);
167957 for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
167958 CollSeq *pColl = (CollSeq *)sqliteHashData(i);
167959 /* Invoke any destructors registered for collation sequence user data. */
167960 for(j=0; j<3; j++){
167961 if( pColl[j].xDel ){
167962 pColl[j].xDel(pColl[j].pUser);
167963 }
167964 }
167965 sqlite3DbFree(db, p: pColl);
167966 }
167967 sqlite3HashClear(pH: &db->aCollSeq);
167968#ifndef SQLITE_OMIT_VIRTUALTABLE
167969 for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
167970 Module *pMod = (Module *)sqliteHashData(i);
167971 sqlite3VtabEponymousTableClear(db, pMod);
167972 sqlite3VtabModuleUnref(db, pMod);
167973 }
167974 sqlite3HashClear(pH: &db->aModule);
167975#endif
167976
167977 sqlite3Error(db, SQLITE_OK); /* Deallocates any cached error strings. */
167978 sqlite3ValueFree(v: db->pErr);
167979 sqlite3CloseExtensions(db);
167980#if SQLITE_USER_AUTHENTICATION
167981 sqlite3_free(db->auth.zAuthUser);
167982 sqlite3_free(db->auth.zAuthPW);
167983#endif
167984
167985 db->eOpenState = SQLITE_STATE_ERROR;
167986
167987 /* The temp-database schema is allocated differently from the other schema
167988 ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
167989 ** So it needs to be freed here. Todo: Why not roll the temp schema into
167990 ** the same sqliteMalloc() as the one that allocates the database
167991 ** structure?
167992 */
167993 sqlite3DbFree(db, p: db->aDb[1].pSchema);
167994 if( db->xAutovacDestr ){
167995 db->xAutovacDestr(db->pAutovacPagesArg);
167996 }
167997 sqlite3_mutex_leave(p: db->mutex);
167998 db->eOpenState = SQLITE_STATE_CLOSED;
167999 sqlite3_mutex_free(p: db->mutex);
168000 assert( sqlite3LookasideUsed(db,0)==0 );
168001 if( db->lookaside.bMalloced ){
168002 sqlite3_free(p: db->lookaside.pStart);
168003 }
168004 sqlite3_free(p: db);
168005}
168006
168007/*
168008** Rollback all database files. If tripCode is not SQLITE_OK, then
168009** any write cursors are invalidated ("tripped" - as in "tripping a circuit
168010** breaker") and made to return tripCode if there are any further
168011** attempts to use that cursor. Read cursors remain open and valid
168012** but are "saved" in case the table pages are moved around.
168013*/
168014SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db, int tripCode){
168015 int i;
168016 int inTrans = 0;
168017 int schemaChange;
168018 assert( sqlite3_mutex_held(db->mutex) );
168019 sqlite3BeginBenignMalloc();
168020
168021 /* Obtain all b-tree mutexes before making any calls to BtreeRollback().
168022 ** This is important in case the transaction being rolled back has
168023 ** modified the database schema. If the b-tree mutexes are not taken
168024 ** here, then another shared-cache connection might sneak in between
168025 ** the database rollback and schema reset, which can cause false
168026 ** corruption reports in some cases. */
168027 sqlite3BtreeEnterAll(db);
168028 schemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0 && db->init.busy==0;
168029
168030 for(i=0; i<db->nDb; i++){
168031 Btree *p = db->aDb[i].pBt;
168032 if( p ){
168033 if( sqlite3BtreeTxnState(p)==SQLITE_TXN_WRITE ){
168034 inTrans = 1;
168035 }
168036 sqlite3BtreeRollback(p, tripCode, writeOnly: !schemaChange);
168037 }
168038 }
168039 sqlite3VtabRollback(db);
168040 sqlite3EndBenignMalloc();
168041
168042 if( schemaChange ){
168043 sqlite3ExpirePreparedStatements(db, iCode: 0);
168044 sqlite3ResetAllSchemasOfConnection(db);
168045 }
168046 sqlite3BtreeLeaveAll(db);
168047
168048 /* Any deferred constraint violations have now been resolved. */
168049 db->nDeferredCons = 0;
168050 db->nDeferredImmCons = 0;
168051 db->flags &= ~(u64)(SQLITE_DeferFKs|SQLITE_CorruptRdOnly);
168052
168053 /* If one has been configured, invoke the rollback-hook callback */
168054 if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
168055 db->xRollbackCallback(db->pRollbackArg);
168056 }
168057}
168058
168059/*
168060** Return a static string containing the name corresponding to the error code
168061** specified in the argument.
168062*/
168063#if defined(SQLITE_NEED_ERR_NAME)
168064SQLITE_PRIVATE const char *sqlite3ErrName(int rc){
168065 const char *zName = 0;
168066 int i, origRc = rc;
168067 for(i=0; i<2 && zName==0; i++, rc &= 0xff){
168068 switch( rc ){
168069 case SQLITE_OK: zName = "SQLITE_OK"; break;
168070 case SQLITE_ERROR: zName = "SQLITE_ERROR"; break;
168071 case SQLITE_ERROR_SNAPSHOT: zName = "SQLITE_ERROR_SNAPSHOT"; break;
168072 case SQLITE_INTERNAL: zName = "SQLITE_INTERNAL"; break;
168073 case SQLITE_PERM: zName = "SQLITE_PERM"; break;
168074 case SQLITE_ABORT: zName = "SQLITE_ABORT"; break;
168075 case SQLITE_ABORT_ROLLBACK: zName = "SQLITE_ABORT_ROLLBACK"; break;
168076 case SQLITE_BUSY: zName = "SQLITE_BUSY"; break;
168077 case SQLITE_BUSY_RECOVERY: zName = "SQLITE_BUSY_RECOVERY"; break;
168078 case SQLITE_BUSY_SNAPSHOT: zName = "SQLITE_BUSY_SNAPSHOT"; break;
168079 case SQLITE_LOCKED: zName = "SQLITE_LOCKED"; break;
168080 case SQLITE_LOCKED_SHAREDCACHE: zName = "SQLITE_LOCKED_SHAREDCACHE";break;
168081 case SQLITE_NOMEM: zName = "SQLITE_NOMEM"; break;
168082 case SQLITE_READONLY: zName = "SQLITE_READONLY"; break;
168083 case SQLITE_READONLY_RECOVERY: zName = "SQLITE_READONLY_RECOVERY"; break;
168084 case SQLITE_READONLY_CANTINIT: zName = "SQLITE_READONLY_CANTINIT"; break;
168085 case SQLITE_READONLY_ROLLBACK: zName = "SQLITE_READONLY_ROLLBACK"; break;
168086 case SQLITE_READONLY_DBMOVED: zName = "SQLITE_READONLY_DBMOVED"; break;
168087 case SQLITE_READONLY_DIRECTORY: zName = "SQLITE_READONLY_DIRECTORY";break;
168088 case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break;
168089 case SQLITE_IOERR: zName = "SQLITE_IOERR"; break;
168090 case SQLITE_IOERR_READ: zName = "SQLITE_IOERR_READ"; break;
168091 case SQLITE_IOERR_SHORT_READ: zName = "SQLITE_IOERR_SHORT_READ"; break;
168092 case SQLITE_IOERR_WRITE: zName = "SQLITE_IOERR_WRITE"; break;
168093 case SQLITE_IOERR_FSYNC: zName = "SQLITE_IOERR_FSYNC"; break;
168094 case SQLITE_IOERR_DIR_FSYNC: zName = "SQLITE_IOERR_DIR_FSYNC"; break;
168095 case SQLITE_IOERR_TRUNCATE: zName = "SQLITE_IOERR_TRUNCATE"; break;
168096 case SQLITE_IOERR_FSTAT: zName = "SQLITE_IOERR_FSTAT"; break;
168097 case SQLITE_IOERR_UNLOCK: zName = "SQLITE_IOERR_UNLOCK"; break;
168098 case SQLITE_IOERR_RDLOCK: zName = "SQLITE_IOERR_RDLOCK"; break;
168099 case SQLITE_IOERR_DELETE: zName = "SQLITE_IOERR_DELETE"; break;
168100 case SQLITE_IOERR_NOMEM: zName = "SQLITE_IOERR_NOMEM"; break;
168101 case SQLITE_IOERR_ACCESS: zName = "SQLITE_IOERR_ACCESS"; break;
168102 case SQLITE_IOERR_CHECKRESERVEDLOCK:
168103 zName = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
168104 case SQLITE_IOERR_LOCK: zName = "SQLITE_IOERR_LOCK"; break;
168105 case SQLITE_IOERR_CLOSE: zName = "SQLITE_IOERR_CLOSE"; break;
168106 case SQLITE_IOERR_DIR_CLOSE: zName = "SQLITE_IOERR_DIR_CLOSE"; break;
168107 case SQLITE_IOERR_SHMOPEN: zName = "SQLITE_IOERR_SHMOPEN"; break;
168108 case SQLITE_IOERR_SHMSIZE: zName = "SQLITE_IOERR_SHMSIZE"; break;
168109 case SQLITE_IOERR_SHMLOCK: zName = "SQLITE_IOERR_SHMLOCK"; break;
168110 case SQLITE_IOERR_SHMMAP: zName = "SQLITE_IOERR_SHMMAP"; break;
168111 case SQLITE_IOERR_SEEK: zName = "SQLITE_IOERR_SEEK"; break;
168112 case SQLITE_IOERR_DELETE_NOENT: zName = "SQLITE_IOERR_DELETE_NOENT";break;
168113 case SQLITE_IOERR_MMAP: zName = "SQLITE_IOERR_MMAP"; break;
168114 case SQLITE_IOERR_GETTEMPPATH: zName = "SQLITE_IOERR_GETTEMPPATH"; break;
168115 case SQLITE_IOERR_CONVPATH: zName = "SQLITE_IOERR_CONVPATH"; break;
168116 case SQLITE_CORRUPT: zName = "SQLITE_CORRUPT"; break;
168117 case SQLITE_CORRUPT_VTAB: zName = "SQLITE_CORRUPT_VTAB"; break;
168118 case SQLITE_NOTFOUND: zName = "SQLITE_NOTFOUND"; break;
168119 case SQLITE_FULL: zName = "SQLITE_FULL"; break;
168120 case SQLITE_CANTOPEN: zName = "SQLITE_CANTOPEN"; break;
168121 case SQLITE_CANTOPEN_NOTEMPDIR: zName = "SQLITE_CANTOPEN_NOTEMPDIR";break;
168122 case SQLITE_CANTOPEN_ISDIR: zName = "SQLITE_CANTOPEN_ISDIR"; break;
168123 case SQLITE_CANTOPEN_FULLPATH: zName = "SQLITE_CANTOPEN_FULLPATH"; break;
168124 case SQLITE_CANTOPEN_CONVPATH: zName = "SQLITE_CANTOPEN_CONVPATH"; break;
168125 case SQLITE_CANTOPEN_SYMLINK: zName = "SQLITE_CANTOPEN_SYMLINK"; break;
168126 case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break;
168127 case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break;
168128 case SQLITE_SCHEMA: zName = "SQLITE_SCHEMA"; break;
168129 case SQLITE_TOOBIG: zName = "SQLITE_TOOBIG"; break;
168130 case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT"; break;
168131 case SQLITE_CONSTRAINT_UNIQUE: zName = "SQLITE_CONSTRAINT_UNIQUE"; break;
168132 case SQLITE_CONSTRAINT_TRIGGER: zName = "SQLITE_CONSTRAINT_TRIGGER";break;
168133 case SQLITE_CONSTRAINT_FOREIGNKEY:
168134 zName = "SQLITE_CONSTRAINT_FOREIGNKEY"; break;
168135 case SQLITE_CONSTRAINT_CHECK: zName = "SQLITE_CONSTRAINT_CHECK"; break;
168136 case SQLITE_CONSTRAINT_PRIMARYKEY:
168137 zName = "SQLITE_CONSTRAINT_PRIMARYKEY"; break;
168138 case SQLITE_CONSTRAINT_NOTNULL: zName = "SQLITE_CONSTRAINT_NOTNULL";break;
168139 case SQLITE_CONSTRAINT_COMMITHOOK:
168140 zName = "SQLITE_CONSTRAINT_COMMITHOOK"; break;
168141 case SQLITE_CONSTRAINT_VTAB: zName = "SQLITE_CONSTRAINT_VTAB"; break;
168142 case SQLITE_CONSTRAINT_FUNCTION:
168143 zName = "SQLITE_CONSTRAINT_FUNCTION"; break;
168144 case SQLITE_CONSTRAINT_ROWID: zName = "SQLITE_CONSTRAINT_ROWID"; break;
168145 case SQLITE_MISMATCH: zName = "SQLITE_MISMATCH"; break;
168146 case SQLITE_MISUSE: zName = "SQLITE_MISUSE"; break;
168147 case SQLITE_NOLFS: zName = "SQLITE_NOLFS"; break;
168148 case SQLITE_AUTH: zName = "SQLITE_AUTH"; break;
168149 case SQLITE_FORMAT: zName = "SQLITE_FORMAT"; break;
168150 case SQLITE_RANGE: zName = "SQLITE_RANGE"; break;
168151 case SQLITE_NOTADB: zName = "SQLITE_NOTADB"; break;
168152 case SQLITE_ROW: zName = "SQLITE_ROW"; break;
168153 case SQLITE_NOTICE: zName = "SQLITE_NOTICE"; break;
168154 case SQLITE_NOTICE_RECOVER_WAL: zName = "SQLITE_NOTICE_RECOVER_WAL";break;
168155 case SQLITE_NOTICE_RECOVER_ROLLBACK:
168156 zName = "SQLITE_NOTICE_RECOVER_ROLLBACK"; break;
168157 case SQLITE_WARNING: zName = "SQLITE_WARNING"; break;
168158 case SQLITE_WARNING_AUTOINDEX: zName = "SQLITE_WARNING_AUTOINDEX"; break;
168159 case SQLITE_DONE: zName = "SQLITE_DONE"; break;
168160 }
168161 }
168162 if( zName==0 ){
168163 static char zBuf[50];
168164 sqlite3_snprintf(sizeof(zBuf), zBuf, "SQLITE_UNKNOWN(%d)", origRc);
168165 zName = zBuf;
168166 }
168167 return zName;
168168}
168169#endif
168170
168171/*
168172** Return a static string that describes the kind of error specified in the
168173** argument.
168174*/
168175SQLITE_PRIVATE const char *sqlite3ErrStr(int rc){
168176 static const char* const aMsg[] = {
168177 /* SQLITE_OK */ "not an error",
168178 /* SQLITE_ERROR */ "SQL logic error",
168179 /* SQLITE_INTERNAL */ 0,
168180 /* SQLITE_PERM */ "access permission denied",
168181 /* SQLITE_ABORT */ "query aborted",
168182 /* SQLITE_BUSY */ "database is locked",
168183 /* SQLITE_LOCKED */ "database table is locked",
168184 /* SQLITE_NOMEM */ "out of memory",
168185 /* SQLITE_READONLY */ "attempt to write a readonly database",
168186 /* SQLITE_INTERRUPT */ "interrupted",
168187 /* SQLITE_IOERR */ "disk I/O error",
168188 /* SQLITE_CORRUPT */ "database disk image is malformed",
168189 /* SQLITE_NOTFOUND */ "unknown operation",
168190 /* SQLITE_FULL */ "database or disk is full",
168191 /* SQLITE_CANTOPEN */ "unable to open database file",
168192 /* SQLITE_PROTOCOL */ "locking protocol",
168193 /* SQLITE_EMPTY */ 0,
168194 /* SQLITE_SCHEMA */ "database schema has changed",
168195 /* SQLITE_TOOBIG */ "string or blob too big",
168196 /* SQLITE_CONSTRAINT */ "constraint failed",
168197 /* SQLITE_MISMATCH */ "datatype mismatch",
168198 /* SQLITE_MISUSE */ "bad parameter or other API misuse",
168199#ifdef SQLITE_DISABLE_LFS
168200 /* SQLITE_NOLFS */ "large file support is disabled",
168201#else
168202 /* SQLITE_NOLFS */ 0,
168203#endif
168204 /* SQLITE_AUTH */ "authorization denied",
168205 /* SQLITE_FORMAT */ 0,
168206 /* SQLITE_RANGE */ "column index out of range",
168207 /* SQLITE_NOTADB */ "file is not a database",
168208 /* SQLITE_NOTICE */ "notification message",
168209 /* SQLITE_WARNING */ "warning message",
168210 };
168211 const char *zErr = "unknown error";
168212 switch( rc ){
168213 case SQLITE_ABORT_ROLLBACK: {
168214 zErr = "abort due to ROLLBACK";
168215 break;
168216 }
168217 case SQLITE_ROW: {
168218 zErr = "another row available";
168219 break;
168220 }
168221 case SQLITE_DONE: {
168222 zErr = "no more rows available";
168223 break;
168224 }
168225 default: {
168226 rc &= 0xff;
168227 if( ALWAYS(rc>=0) && rc<ArraySize(aMsg) && aMsg[rc]!=0 ){
168228 zErr = aMsg[rc];
168229 }
168230 break;
168231 }
168232 }
168233 return zErr;
168234}
168235
168236/*
168237** This routine implements a busy callback that sleeps and tries
168238** again until a timeout value is reached. The timeout value is
168239** an integer number of milliseconds passed in as the first
168240** argument.
168241**
168242** Return non-zero to retry the lock. Return zero to stop trying
168243** and cause SQLite to return SQLITE_BUSY.
168244*/
168245static int sqliteDefaultBusyCallback(
168246 void *ptr, /* Database connection */
168247 int count /* Number of times table has been busy */
168248){
168249#if SQLITE_OS_WIN || HAVE_USLEEP
168250 /* This case is for systems that have support for sleeping for fractions of
168251 ** a second. Examples: All windows systems, unix systems with usleep() */
168252 static const u8 delays[] =
168253 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 };
168254 static const u8 totals[] =
168255 { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 };
168256# define NDELAY ArraySize(delays)
168257 sqlite3 *db = (sqlite3 *)ptr;
168258 int tmout = db->busyTimeout;
168259 int delay, prior;
168260
168261 assert( count>=0 );
168262 if( count < NDELAY ){
168263 delay = delays[count];
168264 prior = totals[count];
168265 }else{
168266 delay = delays[NDELAY-1];
168267 prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
168268 }
168269 if( prior + delay > tmout ){
168270 delay = tmout - prior;
168271 if( delay<=0 ) return 0;
168272 }
168273 sqlite3OsSleep(pVfs: db->pVfs, nMicro: delay*1000);
168274 return 1;
168275#else
168276 /* This case for unix systems that lack usleep() support. Sleeping
168277 ** must be done in increments of whole seconds */
168278 sqlite3 *db = (sqlite3 *)ptr;
168279 int tmout = ((sqlite3 *)ptr)->busyTimeout;
168280 if( (count+1)*1000 > tmout ){
168281 return 0;
168282 }
168283 sqlite3OsSleep(db->pVfs, 1000000);
168284 return 1;
168285#endif
168286}
168287
168288/*
168289** Invoke the given busy handler.
168290**
168291** This routine is called when an operation failed to acquire a
168292** lock on VFS file pFile.
168293**
168294** If this routine returns non-zero, the lock is retried. If it
168295** returns 0, the operation aborts with an SQLITE_BUSY error.
168296*/
168297SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p){
168298 int rc;
168299 if( p->xBusyHandler==0 || p->nBusy<0 ) return 0;
168300 rc = p->xBusyHandler(p->pBusyArg, p->nBusy);
168301 if( rc==0 ){
168302 p->nBusy = -1;
168303 }else{
168304 p->nBusy++;
168305 }
168306 return rc;
168307}
168308
168309/*
168310** This routine sets the busy callback for an Sqlite database to the
168311** given callback function with the given argument.
168312*/
168313SQLITE_API int sqlite3_busy_handler(
168314 sqlite3 *db,
168315 int (*xBusy)(void*,int),
168316 void *pArg
168317){
168318#ifdef SQLITE_ENABLE_API_ARMOR
168319 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
168320#endif
168321 sqlite3_mutex_enter(p: db->mutex);
168322 db->busyHandler.xBusyHandler = xBusy;
168323 db->busyHandler.pBusyArg = pArg;
168324 db->busyHandler.nBusy = 0;
168325 db->busyTimeout = 0;
168326 sqlite3_mutex_leave(p: db->mutex);
168327 return SQLITE_OK;
168328}
168329
168330#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
168331/*
168332** This routine sets the progress callback for an Sqlite database to the
168333** given callback function with the given argument. The progress callback will
168334** be invoked every nOps opcodes.
168335*/
168336SQLITE_API void sqlite3_progress_handler(
168337 sqlite3 *db,
168338 int nOps,
168339 int (*xProgress)(void*),
168340 void *pArg
168341){
168342#ifdef SQLITE_ENABLE_API_ARMOR
168343 if( !sqlite3SafetyCheckOk(db) ){
168344 (void)SQLITE_MISUSE_BKPT;
168345 return;
168346 }
168347#endif
168348 sqlite3_mutex_enter(p: db->mutex);
168349 if( nOps>0 ){
168350 db->xProgress = xProgress;
168351 db->nProgressOps = (unsigned)nOps;
168352 db->pProgressArg = pArg;
168353 }else{
168354 db->xProgress = 0;
168355 db->nProgressOps = 0;
168356 db->pProgressArg = 0;
168357 }
168358 sqlite3_mutex_leave(p: db->mutex);
168359}
168360#endif
168361
168362
168363/*
168364** This routine installs a default busy handler that waits for the
168365** specified number of milliseconds before returning 0.
168366*/
168367SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
168368#ifdef SQLITE_ENABLE_API_ARMOR
168369 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
168370#endif
168371 if( ms>0 ){
168372 sqlite3_busy_handler(db, xBusy: (int(*)(void*,int))sqliteDefaultBusyCallback,
168373 pArg: (void*)db);
168374 db->busyTimeout = ms;
168375 }else{
168376 sqlite3_busy_handler(db, xBusy: 0, pArg: 0);
168377 }
168378 return SQLITE_OK;
168379}
168380
168381/*
168382** Cause any pending operation to stop at its earliest opportunity.
168383*/
168384SQLITE_API void sqlite3_interrupt(sqlite3 *db){
168385#ifdef SQLITE_ENABLE_API_ARMOR
168386 if( !sqlite3SafetyCheckOk(db) && (db==0 || db->eOpenState!=SQLITE_STATE_ZOMBIE) ){
168387 (void)SQLITE_MISUSE_BKPT;
168388 return;
168389 }
168390#endif
168391 AtomicStore(&db->u1.isInterrupted, 1);
168392}
168393
168394
168395/*
168396** This function is exactly the same as sqlite3_create_function(), except
168397** that it is designed to be called by internal code. The difference is
168398** that if a malloc() fails in sqlite3_create_function(), an error code
168399** is returned and the mallocFailed flag cleared.
168400*/
168401SQLITE_PRIVATE int sqlite3CreateFunc(
168402 sqlite3 *db,
168403 const char *zFunctionName,
168404 int nArg,
168405 int enc,
168406 void *pUserData,
168407 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
168408 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
168409 void (*xFinal)(sqlite3_context*),
168410 void (*xValue)(sqlite3_context*),
168411 void (*xInverse)(sqlite3_context*,int,sqlite3_value **),
168412 FuncDestructor *pDestructor
168413){
168414 FuncDef *p;
168415 int extraFlags;
168416
168417 assert( sqlite3_mutex_held(db->mutex) );
168418 assert( xValue==0 || xSFunc==0 );
168419 if( zFunctionName==0 /* Must have a valid name */
168420 || (xSFunc!=0 && xFinal!=0) /* Not both xSFunc and xFinal */
168421 || ((xFinal==0)!=(xStep==0)) /* Both or neither of xFinal and xStep */
168422 || ((xValue==0)!=(xInverse==0)) /* Both or neither of xValue, xInverse */
168423 || (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG)
168424 || (255<sqlite3Strlen30(z: zFunctionName))
168425 ){
168426 return SQLITE_MISUSE_BKPT;
168427 }
168428
168429 assert( SQLITE_FUNC_CONSTANT==SQLITE_DETERMINISTIC );
168430 assert( SQLITE_FUNC_DIRECT==SQLITE_DIRECTONLY );
168431 extraFlags = enc & (SQLITE_DETERMINISTIC|SQLITE_DIRECTONLY|
168432 SQLITE_SUBTYPE|SQLITE_INNOCUOUS);
168433 enc &= (SQLITE_FUNC_ENCMASK|SQLITE_ANY);
168434
168435 /* The SQLITE_INNOCUOUS flag is the same bit as SQLITE_FUNC_UNSAFE. But
168436 ** the meaning is inverted. So flip the bit. */
168437 assert( SQLITE_FUNC_UNSAFE==SQLITE_INNOCUOUS );
168438 extraFlags ^= SQLITE_FUNC_UNSAFE;
168439
168440
168441#ifndef SQLITE_OMIT_UTF16
168442 /* If SQLITE_UTF16 is specified as the encoding type, transform this
168443 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
168444 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
168445 **
168446 ** If SQLITE_ANY is specified, add three versions of the function
168447 ** to the hash table.
168448 */
168449 switch( enc ){
168450 case SQLITE_UTF16:
168451 enc = SQLITE_UTF16NATIVE;
168452 break;
168453 case SQLITE_ANY: {
168454 int rc;
168455 rc = sqlite3CreateFunc(db, zFunctionName, nArg,
168456 enc: (SQLITE_UTF8|extraFlags)^SQLITE_FUNC_UNSAFE,
168457 pUserData, xSFunc, xStep, xFinal, xValue, xInverse, pDestructor);
168458 if( rc==SQLITE_OK ){
168459 rc = sqlite3CreateFunc(db, zFunctionName, nArg,
168460 enc: (SQLITE_UTF16LE|extraFlags)^SQLITE_FUNC_UNSAFE,
168461 pUserData, xSFunc, xStep, xFinal, xValue, xInverse, pDestructor);
168462 }
168463 if( rc!=SQLITE_OK ){
168464 return rc;
168465 }
168466 enc = SQLITE_UTF16BE;
168467 break;
168468 }
168469 case SQLITE_UTF8:
168470 case SQLITE_UTF16LE:
168471 case SQLITE_UTF16BE:
168472 break;
168473 default:
168474 enc = SQLITE_UTF8;
168475 break;
168476 }
168477#else
168478 enc = SQLITE_UTF8;
168479#endif
168480
168481 /* Check if an existing function is being overridden or deleted. If so,
168482 ** and there are active VMs, then return SQLITE_BUSY. If a function
168483 ** is being overridden/deleted but there are no active VMs, allow the
168484 ** operation to continue but invalidate all precompiled statements.
168485 */
168486 p = sqlite3FindFunction(db, zName: zFunctionName, nArg, enc: (u8)enc, createFlag: 0);
168487 if( p && (p->funcFlags & SQLITE_FUNC_ENCMASK)==(u32)enc && p->nArg==nArg ){
168488 if( db->nVdbeActive ){
168489 sqlite3ErrorWithMsg(db, SQLITE_BUSY,
168490 zFormat: "unable to delete/modify user-function due to active statements");
168491 assert( !db->mallocFailed );
168492 return SQLITE_BUSY;
168493 }else{
168494 sqlite3ExpirePreparedStatements(db, iCode: 0);
168495 }
168496 }else if( xSFunc==0 && xFinal==0 ){
168497 /* Trying to delete a function that does not exist. This is a no-op.
168498 ** https://sqlite.org/forum/forumpost/726219164b */
168499 return SQLITE_OK;
168500 }
168501
168502 p = sqlite3FindFunction(db, zName: zFunctionName, nArg, enc: (u8)enc, createFlag: 1);
168503 assert(p || db->mallocFailed);
168504 if( !p ){
168505 return SQLITE_NOMEM_BKPT;
168506 }
168507
168508 /* If an older version of the function with a configured destructor is
168509 ** being replaced invoke the destructor function here. */
168510 functionDestroy(db, p);
168511
168512 if( pDestructor ){
168513 pDestructor->nRef++;
168514 }
168515 p->u.pDestructor = pDestructor;
168516 p->funcFlags = (p->funcFlags & SQLITE_FUNC_ENCMASK) | extraFlags;
168517 testcase( p->funcFlags & SQLITE_DETERMINISTIC );
168518 testcase( p->funcFlags & SQLITE_DIRECTONLY );
168519 p->xSFunc = xSFunc ? xSFunc : xStep;
168520 p->xFinalize = xFinal;
168521 p->xValue = xValue;
168522 p->xInverse = xInverse;
168523 p->pUserData = pUserData;
168524 p->nArg = (u16)nArg;
168525 return SQLITE_OK;
168526}
168527
168528/*
168529** Worker function used by utf-8 APIs that create new functions:
168530**
168531** sqlite3_create_function()
168532** sqlite3_create_function_v2()
168533** sqlite3_create_window_function()
168534*/
168535static int createFunctionApi(
168536 sqlite3 *db,
168537 const char *zFunc,
168538 int nArg,
168539 int enc,
168540 void *p,
168541 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**),
168542 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
168543 void (*xFinal)(sqlite3_context*),
168544 void (*xValue)(sqlite3_context*),
168545 void (*xInverse)(sqlite3_context*,int,sqlite3_value**),
168546 void(*xDestroy)(void*)
168547){
168548 int rc = SQLITE_ERROR;
168549 FuncDestructor *pArg = 0;
168550
168551#ifdef SQLITE_ENABLE_API_ARMOR
168552 if( !sqlite3SafetyCheckOk(db) ){
168553 return SQLITE_MISUSE_BKPT;
168554 }
168555#endif
168556 sqlite3_mutex_enter(p: db->mutex);
168557 if( xDestroy ){
168558 pArg = (FuncDestructor *)sqlite3Malloc(n: sizeof(FuncDestructor));
168559 if( !pArg ){
168560 sqlite3OomFault(db);
168561 xDestroy(p);
168562 goto out;
168563 }
168564 pArg->nRef = 0;
168565 pArg->xDestroy = xDestroy;
168566 pArg->pUserData = p;
168567 }
168568 rc = sqlite3CreateFunc(db, zFunctionName: zFunc, nArg, enc, pUserData: p,
168569 xSFunc, xStep, xFinal, xValue, xInverse, pDestructor: pArg
168570 );
168571 if( pArg && pArg->nRef==0 ){
168572 assert( rc!=SQLITE_OK || (xStep==0 && xFinal==0) );
168573 xDestroy(p);
168574 sqlite3_free(p: pArg);
168575 }
168576
168577 out:
168578 rc = sqlite3ApiExit(db, rc);
168579 sqlite3_mutex_leave(p: db->mutex);
168580 return rc;
168581}
168582
168583/*
168584** Create new user functions.
168585*/
168586SQLITE_API int sqlite3_create_function(
168587 sqlite3 *db,
168588 const char *zFunc,
168589 int nArg,
168590 int enc,
168591 void *p,
168592 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
168593 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
168594 void (*xFinal)(sqlite3_context*)
168595){
168596 return createFunctionApi(db, zFunc, nArg, enc, p, xSFunc, xStep,
168597 xFinal, xValue: 0, xInverse: 0, xDestroy: 0);
168598}
168599SQLITE_API int sqlite3_create_function_v2(
168600 sqlite3 *db,
168601 const char *zFunc,
168602 int nArg,
168603 int enc,
168604 void *p,
168605 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
168606 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
168607 void (*xFinal)(sqlite3_context*),
168608 void (*xDestroy)(void *)
168609){
168610 return createFunctionApi(db, zFunc, nArg, enc, p, xSFunc, xStep,
168611 xFinal, xValue: 0, xInverse: 0, xDestroy);
168612}
168613SQLITE_API int sqlite3_create_window_function(
168614 sqlite3 *db,
168615 const char *zFunc,
168616 int nArg,
168617 int enc,
168618 void *p,
168619 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
168620 void (*xFinal)(sqlite3_context*),
168621 void (*xValue)(sqlite3_context*),
168622 void (*xInverse)(sqlite3_context*,int,sqlite3_value **),
168623 void (*xDestroy)(void *)
168624){
168625 return createFunctionApi(db, zFunc, nArg, enc, p, xSFunc: 0, xStep,
168626 xFinal, xValue, xInverse, xDestroy);
168627}
168628
168629#ifndef SQLITE_OMIT_UTF16
168630SQLITE_API int sqlite3_create_function16(
168631 sqlite3 *db,
168632 const void *zFunctionName,
168633 int nArg,
168634 int eTextRep,
168635 void *p,
168636 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**),
168637 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
168638 void (*xFinal)(sqlite3_context*)
168639){
168640 int rc;
168641 char *zFunc8;
168642
168643#ifdef SQLITE_ENABLE_API_ARMOR
168644 if( !sqlite3SafetyCheckOk(db) || zFunctionName==0 ) return SQLITE_MISUSE_BKPT;
168645#endif
168646 sqlite3_mutex_enter(p: db->mutex);
168647 assert( !db->mallocFailed );
168648 zFunc8 = sqlite3Utf16to8(db, z: zFunctionName, nByte: -1, SQLITE_UTF16NATIVE);
168649 rc = sqlite3CreateFunc(db, zFunctionName: zFunc8, nArg, enc: eTextRep, pUserData: p, xSFunc,xStep,xFinal,xValue: 0,xInverse: 0,pDestructor: 0);
168650 sqlite3DbFree(db, p: zFunc8);
168651 rc = sqlite3ApiExit(db, rc);
168652 sqlite3_mutex_leave(p: db->mutex);
168653 return rc;
168654}
168655#endif
168656
168657
168658/*
168659** The following is the implementation of an SQL function that always
168660** fails with an error message stating that the function is used in the
168661** wrong context. The sqlite3_overload_function() API might construct
168662** SQL function that use this routine so that the functions will exist
168663** for name resolution but are actually overloaded by the xFindFunction
168664** method of virtual tables.
168665*/
168666static void sqlite3InvalidFunction(
168667 sqlite3_context *context, /* The function calling context */
168668 int NotUsed, /* Number of arguments to the function */
168669 sqlite3_value **NotUsed2 /* Value of each argument */
168670){
168671 const char *zName = (const char*)sqlite3_user_data(p: context);
168672 char *zErr;
168673 UNUSED_PARAMETER2(NotUsed, NotUsed2);
168674 zErr = sqlite3_mprintf(
168675 zFormat: "unable to use function %s in the requested context", zName);
168676 sqlite3_result_error(pCtx: context, z: zErr, n: -1);
168677 sqlite3_free(p: zErr);
168678}
168679
168680/*
168681** Declare that a function has been overloaded by a virtual table.
168682**
168683** If the function already exists as a regular global function, then
168684** this routine is a no-op. If the function does not exist, then create
168685** a new one that always throws a run-time error.
168686**
168687** When virtual tables intend to provide an overloaded function, they
168688** should call this routine to make sure the global function exists.
168689** A global function must exist in order for name resolution to work
168690** properly.
168691*/
168692SQLITE_API int sqlite3_overload_function(
168693 sqlite3 *db,
168694 const char *zName,
168695 int nArg
168696){
168697 int rc;
168698 char *zCopy;
168699
168700#ifdef SQLITE_ENABLE_API_ARMOR
168701 if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){
168702 return SQLITE_MISUSE_BKPT;
168703 }
168704#endif
168705 sqlite3_mutex_enter(p: db->mutex);
168706 rc = sqlite3FindFunction(db, zName, nArg, SQLITE_UTF8, createFlag: 0)!=0;
168707 sqlite3_mutex_leave(p: db->mutex);
168708 if( rc ) return SQLITE_OK;
168709 zCopy = sqlite3_mprintf(zFormat: zName);
168710 if( zCopy==0 ) return SQLITE_NOMEM;
168711 return sqlite3_create_function_v2(db, zFunc: zName, nArg, SQLITE_UTF8,
168712 p: zCopy, xSFunc: sqlite3InvalidFunction, xStep: 0, xFinal: 0, xDestroy: sqlite3_free);
168713}
168714
168715#ifndef SQLITE_OMIT_TRACE
168716/*
168717** Register a trace function. The pArg from the previously registered trace
168718** is returned.
168719**
168720** A NULL trace function means that no tracing is executes. A non-NULL
168721** trace is a pointer to a function that is invoked at the start of each
168722** SQL statement.
168723*/
168724#ifndef SQLITE_OMIT_DEPRECATED
168725SQLITE_API void *sqlite3_trace(sqlite3 *db, void(*xTrace)(void*,const char*), void *pArg){
168726 void *pOld;
168727
168728#ifdef SQLITE_ENABLE_API_ARMOR
168729 if( !sqlite3SafetyCheckOk(db) ){
168730 (void)SQLITE_MISUSE_BKPT;
168731 return 0;
168732 }
168733#endif
168734 sqlite3_mutex_enter(p: db->mutex);
168735 pOld = db->pTraceArg;
168736 db->mTrace = xTrace ? SQLITE_TRACE_LEGACY : 0;
168737 db->trace.xLegacy = xTrace;
168738 db->pTraceArg = pArg;
168739 sqlite3_mutex_leave(p: db->mutex);
168740 return pOld;
168741}
168742#endif /* SQLITE_OMIT_DEPRECATED */
168743
168744/* Register a trace callback using the version-2 interface.
168745*/
168746SQLITE_API int sqlite3_trace_v2(
168747 sqlite3 *db, /* Trace this connection */
168748 unsigned mTrace, /* Mask of events to be traced */
168749 int(*xTrace)(unsigned,void*,void*,void*), /* Callback to invoke */
168750 void *pArg /* Context */
168751){
168752#ifdef SQLITE_ENABLE_API_ARMOR
168753 if( !sqlite3SafetyCheckOk(db) ){
168754 return SQLITE_MISUSE_BKPT;
168755 }
168756#endif
168757 sqlite3_mutex_enter(p: db->mutex);
168758 if( mTrace==0 ) xTrace = 0;
168759 if( xTrace==0 ) mTrace = 0;
168760 db->mTrace = mTrace;
168761 db->trace.xV2 = xTrace;
168762 db->pTraceArg = pArg;
168763 sqlite3_mutex_leave(p: db->mutex);
168764 return SQLITE_OK;
168765}
168766
168767#ifndef SQLITE_OMIT_DEPRECATED
168768/*
168769** Register a profile function. The pArg from the previously registered
168770** profile function is returned.
168771**
168772** A NULL profile function means that no profiling is executes. A non-NULL
168773** profile is a pointer to a function that is invoked at the conclusion of
168774** each SQL statement that is run.
168775*/
168776SQLITE_API void *sqlite3_profile(
168777 sqlite3 *db,
168778 void (*xProfile)(void*,const char*,sqlite_uint64),
168779 void *pArg
168780){
168781 void *pOld;
168782
168783#ifdef SQLITE_ENABLE_API_ARMOR
168784 if( !sqlite3SafetyCheckOk(db) ){
168785 (void)SQLITE_MISUSE_BKPT;
168786 return 0;
168787 }
168788#endif
168789 sqlite3_mutex_enter(p: db->mutex);
168790 pOld = db->pProfileArg;
168791 db->xProfile = xProfile;
168792 db->pProfileArg = pArg;
168793 db->mTrace &= SQLITE_TRACE_NONLEGACY_MASK;
168794 if( db->xProfile ) db->mTrace |= SQLITE_TRACE_XPROFILE;
168795 sqlite3_mutex_leave(p: db->mutex);
168796 return pOld;
168797}
168798#endif /* SQLITE_OMIT_DEPRECATED */
168799#endif /* SQLITE_OMIT_TRACE */
168800
168801/*
168802** Register a function to be invoked when a transaction commits.
168803** If the invoked function returns non-zero, then the commit becomes a
168804** rollback.
168805*/
168806SQLITE_API void *sqlite3_commit_hook(
168807 sqlite3 *db, /* Attach the hook to this database */
168808 int (*xCallback)(void*), /* Function to invoke on each commit */
168809 void *pArg /* Argument to the function */
168810){
168811 void *pOld;
168812
168813#ifdef SQLITE_ENABLE_API_ARMOR
168814 if( !sqlite3SafetyCheckOk(db) ){
168815 (void)SQLITE_MISUSE_BKPT;
168816 return 0;
168817 }
168818#endif
168819 sqlite3_mutex_enter(p: db->mutex);
168820 pOld = db->pCommitArg;
168821 db->xCommitCallback = xCallback;
168822 db->pCommitArg = pArg;
168823 sqlite3_mutex_leave(p: db->mutex);
168824 return pOld;
168825}
168826
168827/*
168828** Register a callback to be invoked each time a row is updated,
168829** inserted or deleted using this database connection.
168830*/
168831SQLITE_API void *sqlite3_update_hook(
168832 sqlite3 *db, /* Attach the hook to this database */
168833 void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
168834 void *pArg /* Argument to the function */
168835){
168836 void *pRet;
168837
168838#ifdef SQLITE_ENABLE_API_ARMOR
168839 if( !sqlite3SafetyCheckOk(db) ){
168840 (void)SQLITE_MISUSE_BKPT;
168841 return 0;
168842 }
168843#endif
168844 sqlite3_mutex_enter(p: db->mutex);
168845 pRet = db->pUpdateArg;
168846 db->xUpdateCallback = xCallback;
168847 db->pUpdateArg = pArg;
168848 sqlite3_mutex_leave(p: db->mutex);
168849 return pRet;
168850}
168851
168852/*
168853** Register a callback to be invoked each time a transaction is rolled
168854** back by this database connection.
168855*/
168856SQLITE_API void *sqlite3_rollback_hook(
168857 sqlite3 *db, /* Attach the hook to this database */
168858 void (*xCallback)(void*), /* Callback function */
168859 void *pArg /* Argument to the function */
168860){
168861 void *pRet;
168862
168863#ifdef SQLITE_ENABLE_API_ARMOR
168864 if( !sqlite3SafetyCheckOk(db) ){
168865 (void)SQLITE_MISUSE_BKPT;
168866 return 0;
168867 }
168868#endif
168869 sqlite3_mutex_enter(p: db->mutex);
168870 pRet = db->pRollbackArg;
168871 db->xRollbackCallback = xCallback;
168872 db->pRollbackArg = pArg;
168873 sqlite3_mutex_leave(p: db->mutex);
168874 return pRet;
168875}
168876
168877#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
168878/*
168879** Register a callback to be invoked each time a row is updated,
168880** inserted or deleted using this database connection.
168881*/
168882SQLITE_API void *sqlite3_preupdate_hook(
168883 sqlite3 *db, /* Attach the hook to this database */
168884 void(*xCallback)( /* Callback function */
168885 void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64),
168886 void *pArg /* First callback argument */
168887){
168888 void *pRet;
168889 sqlite3_mutex_enter(db->mutex);
168890 pRet = db->pPreUpdateArg;
168891 db->xPreUpdateCallback = xCallback;
168892 db->pPreUpdateArg = pArg;
168893 sqlite3_mutex_leave(db->mutex);
168894 return pRet;
168895}
168896#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
168897
168898/*
168899** Register a function to be invoked prior to each autovacuum that
168900** determines the number of pages to vacuum.
168901*/
168902SQLITE_API int sqlite3_autovacuum_pages(
168903 sqlite3 *db, /* Attach the hook to this database */
168904 unsigned int (*xCallback)(void*,const char*,u32,u32,u32),
168905 void *pArg, /* Argument to the function */
168906 void (*xDestructor)(void*) /* Destructor for pArg */
168907){
168908#ifdef SQLITE_ENABLE_API_ARMOR
168909 if( !sqlite3SafetyCheckOk(db) ){
168910 if( xDestructor ) xDestructor(pArg);
168911 return SQLITE_MISUSE_BKPT;
168912 }
168913#endif
168914 sqlite3_mutex_enter(p: db->mutex);
168915 if( db->xAutovacDestr ){
168916 db->xAutovacDestr(db->pAutovacPagesArg);
168917 }
168918 db->xAutovacPages = xCallback;
168919 db->pAutovacPagesArg = pArg;
168920 db->xAutovacDestr = xDestructor;
168921 sqlite3_mutex_leave(p: db->mutex);
168922 return SQLITE_OK;
168923}
168924
168925
168926#ifndef SQLITE_OMIT_WAL
168927/*
168928** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint().
168929** Invoke sqlite3_wal_checkpoint if the number of frames in the log file
168930** is greater than sqlite3.pWalArg cast to an integer (the value configured by
168931** wal_autocheckpoint()).
168932*/
168933SQLITE_PRIVATE int sqlite3WalDefaultHook(
168934 void *pClientData, /* Argument */
168935 sqlite3 *db, /* Connection */
168936 const char *zDb, /* Database */
168937 int nFrame /* Size of WAL */
168938){
168939 if( nFrame>=SQLITE_PTR_TO_INT(pClientData) ){
168940 sqlite3BeginBenignMalloc();
168941 sqlite3_wal_checkpoint(db, zDb);
168942 sqlite3EndBenignMalloc();
168943 }
168944 return SQLITE_OK;
168945}
168946#endif /* SQLITE_OMIT_WAL */
168947
168948/*
168949** Configure an sqlite3_wal_hook() callback to automatically checkpoint
168950** a database after committing a transaction if there are nFrame or
168951** more frames in the log file. Passing zero or a negative value as the
168952** nFrame parameter disables automatic checkpoints entirely.
168953**
168954** The callback registered by this function replaces any existing callback
168955** registered using sqlite3_wal_hook(). Likewise, registering a callback
168956** using sqlite3_wal_hook() disables the automatic checkpoint mechanism
168957** configured by this function.
168958*/
168959SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){
168960#ifdef SQLITE_OMIT_WAL
168961 UNUSED_PARAMETER(db);
168962 UNUSED_PARAMETER(nFrame);
168963#else
168964#ifdef SQLITE_ENABLE_API_ARMOR
168965 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
168966#endif
168967 if( nFrame>0 ){
168968 sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame));
168969 }else{
168970 sqlite3_wal_hook(db, 0, 0);
168971 }
168972#endif
168973 return SQLITE_OK;
168974}
168975
168976/*
168977** Register a callback to be invoked each time a transaction is written
168978** into the write-ahead-log by this database connection.
168979*/
168980SQLITE_API void *sqlite3_wal_hook(
168981 sqlite3 *db, /* Attach the hook to this db handle */
168982 int(*xCallback)(void *, sqlite3*, const char*, int),
168983 void *pArg /* First argument passed to xCallback() */
168984){
168985#ifndef SQLITE_OMIT_WAL
168986 void *pRet;
168987#ifdef SQLITE_ENABLE_API_ARMOR
168988 if( !sqlite3SafetyCheckOk(db) ){
168989 (void)SQLITE_MISUSE_BKPT;
168990 return 0;
168991 }
168992#endif
168993 sqlite3_mutex_enter(p: db->mutex);
168994 pRet = db->pWalArg;
168995 db->xWalCallback = xCallback;
168996 db->pWalArg = pArg;
168997 sqlite3_mutex_leave(p: db->mutex);
168998 return pRet;
168999#else
169000 return 0;
169001#endif
169002}
169003
169004/*
169005** Checkpoint database zDb.
169006*/
169007SQLITE_API int sqlite3_wal_checkpoint_v2(
169008 sqlite3 *db, /* Database handle */
169009 const char *zDb, /* Name of attached database (or NULL) */
169010 int eMode, /* SQLITE_CHECKPOINT_* value */
169011 int *pnLog, /* OUT: Size of WAL log in frames */
169012 int *pnCkpt /* OUT: Total number of frames checkpointed */
169013){
169014#ifdef SQLITE_OMIT_WAL
169015 return SQLITE_OK;
169016#else
169017 int rc; /* Return code */
169018 int iDb; /* Schema to checkpoint */
169019
169020#ifdef SQLITE_ENABLE_API_ARMOR
169021 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
169022#endif
169023
169024 /* Initialize the output variables to -1 in case an error occurs. */
169025 if( pnLog ) *pnLog = -1;
169026 if( pnCkpt ) *pnCkpt = -1;
169027
169028 assert( SQLITE_CHECKPOINT_PASSIVE==0 );
169029 assert( SQLITE_CHECKPOINT_FULL==1 );
169030 assert( SQLITE_CHECKPOINT_RESTART==2 );
169031 assert( SQLITE_CHECKPOINT_TRUNCATE==3 );
169032 if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_TRUNCATE ){
169033 /* EVIDENCE-OF: R-03996-12088 The M parameter must be a valid checkpoint
169034 ** mode: */
169035 return SQLITE_MISUSE;
169036 }
169037
169038 sqlite3_mutex_enter(p: db->mutex);
169039 if( zDb && zDb[0] ){
169040 iDb = sqlite3FindDbName(db, zName: zDb);
169041 }else{
169042 iDb = SQLITE_MAX_DB; /* This means process all schemas */
169043 }
169044 if( iDb<0 ){
169045 rc = SQLITE_ERROR;
169046 sqlite3ErrorWithMsg(db, SQLITE_ERROR, zFormat: "unknown database: %s", zDb);
169047 }else{
169048 db->busyHandler.nBusy = 0;
169049 rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt);
169050 sqlite3Error(db, err_code: rc);
169051 }
169052 rc = sqlite3ApiExit(db, rc);
169053
169054 /* If there are no active statements, clear the interrupt flag at this
169055 ** point. */
169056 if( db->nVdbeActive==0 ){
169057 AtomicStore(&db->u1.isInterrupted, 0);
169058 }
169059
169060 sqlite3_mutex_leave(p: db->mutex);
169061 return rc;
169062#endif
169063}
169064
169065
169066/*
169067** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
169068** to contains a zero-length string, all attached databases are
169069** checkpointed.
169070*/
169071SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
169072 /* EVIDENCE-OF: R-41613-20553 The sqlite3_wal_checkpoint(D,X) is equivalent to
169073 ** sqlite3_wal_checkpoint_v2(D,X,SQLITE_CHECKPOINT_PASSIVE,0,0). */
169074 return sqlite3_wal_checkpoint_v2(db,zDb,SQLITE_CHECKPOINT_PASSIVE,pnLog: 0,pnCkpt: 0);
169075}
169076
169077#ifndef SQLITE_OMIT_WAL
169078/*
169079** Run a checkpoint on database iDb. This is a no-op if database iDb is
169080** not currently open in WAL mode.
169081**
169082** If a transaction is open on the database being checkpointed, this
169083** function returns SQLITE_LOCKED and a checkpoint is not attempted. If
169084** an error occurs while running the checkpoint, an SQLite error code is
169085** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK.
169086**
169087** The mutex on database handle db should be held by the caller. The mutex
169088** associated with the specific b-tree being checkpointed is taken by
169089** this function while the checkpoint is running.
169090**
169091** If iDb is passed SQLITE_MAX_DB then all attached databases are
169092** checkpointed. If an error is encountered it is returned immediately -
169093** no attempt is made to checkpoint any remaining databases.
169094**
169095** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL, RESTART
169096** or TRUNCATE.
169097*/
169098SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){
169099 int rc = SQLITE_OK; /* Return code */
169100 int i; /* Used to iterate through attached dbs */
169101 int bBusy = 0; /* True if SQLITE_BUSY has been encountered */
169102
169103 assert( sqlite3_mutex_held(db->mutex) );
169104 assert( !pnLog || *pnLog==-1 );
169105 assert( !pnCkpt || *pnCkpt==-1 );
169106 testcase( iDb==SQLITE_MAX_ATTACHED ); /* See forum post a006d86f72 */
169107 testcase( iDb==SQLITE_MAX_DB );
169108
169109 for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
169110 if( i==iDb || iDb==SQLITE_MAX_DB ){
169111 rc = sqlite3BtreeCheckpoint(p: db->aDb[i].pBt, eMode, pnLog, pnCkpt);
169112 pnLog = 0;
169113 pnCkpt = 0;
169114 if( rc==SQLITE_BUSY ){
169115 bBusy = 1;
169116 rc = SQLITE_OK;
169117 }
169118 }
169119 }
169120
169121 return (rc==SQLITE_OK && bBusy) ? SQLITE_BUSY : rc;
169122}
169123#endif /* SQLITE_OMIT_WAL */
169124
169125/*
169126** This function returns true if main-memory should be used instead of
169127** a temporary file for transient pager files and statement journals.
169128** The value returned depends on the value of db->temp_store (runtime
169129** parameter) and the compile time value of SQLITE_TEMP_STORE. The
169130** following table describes the relationship between these two values
169131** and this functions return value.
169132**
169133** SQLITE_TEMP_STORE db->temp_store Location of temporary database
169134** ----------------- -------------- ------------------------------
169135** 0 any file (return 0)
169136** 1 1 file (return 0)
169137** 1 2 memory (return 1)
169138** 1 0 file (return 0)
169139** 2 1 file (return 0)
169140** 2 2 memory (return 1)
169141** 2 0 memory (return 1)
169142** 3 any memory (return 1)
169143*/
169144SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3 *db){
169145#if SQLITE_TEMP_STORE==1
169146 return ( db->temp_store==2 );
169147#endif
169148#if SQLITE_TEMP_STORE==2
169149 return ( db->temp_store!=1 );
169150#endif
169151#if SQLITE_TEMP_STORE==3
169152 UNUSED_PARAMETER(db);
169153 return 1;
169154#endif
169155#if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3
169156 UNUSED_PARAMETER(db);
169157 return 0;
169158#endif
169159}
169160
169161/*
169162** Return UTF-8 encoded English language explanation of the most recent
169163** error.
169164*/
169165SQLITE_API const char *sqlite3_errmsg(sqlite3 *db){
169166 const char *z;
169167 if( !db ){
169168 return sqlite3ErrStr(SQLITE_NOMEM_BKPT);
169169 }
169170 if( !sqlite3SafetyCheckSickOrOk(db) ){
169171 return sqlite3ErrStr(SQLITE_MISUSE_BKPT);
169172 }
169173 sqlite3_mutex_enter(p: db->mutex);
169174 if( db->mallocFailed ){
169175 z = sqlite3ErrStr(SQLITE_NOMEM_BKPT);
169176 }else{
169177 testcase( db->pErr==0 );
169178 z = db->errCode ? (char*)sqlite3_value_text(pVal: db->pErr) : 0;
169179 assert( !db->mallocFailed );
169180 if( z==0 ){
169181 z = sqlite3ErrStr(rc: db->errCode);
169182 }
169183 }
169184 sqlite3_mutex_leave(p: db->mutex);
169185 return z;
169186}
169187
169188#ifndef SQLITE_OMIT_UTF16
169189/*
169190** Return UTF-16 encoded English language explanation of the most recent
169191** error.
169192*/
169193SQLITE_API const void *sqlite3_errmsg16(sqlite3 *db){
169194 static const u16 outOfMem[] = {
169195 'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0
169196 };
169197 static const u16 misuse[] = {
169198 'b', 'a', 'd', ' ', 'p', 'a', 'r', 'a', 'm', 'e', 't', 'e', 'r', ' ',
169199 'o', 'r', ' ', 'o', 't', 'h', 'e', 'r', ' ', 'A', 'P', 'I', ' ',
169200 'm', 'i', 's', 'u', 's', 'e', 0
169201 };
169202
169203 const void *z;
169204 if( !db ){
169205 return (void *)outOfMem;
169206 }
169207 if( !sqlite3SafetyCheckSickOrOk(db) ){
169208 return (void *)misuse;
169209 }
169210 sqlite3_mutex_enter(p: db->mutex);
169211 if( db->mallocFailed ){
169212 z = (void *)outOfMem;
169213 }else{
169214 z = sqlite3_value_text16(pVal: db->pErr);
169215 if( z==0 ){
169216 sqlite3ErrorWithMsg(db, err_code: db->errCode, zFormat: sqlite3ErrStr(rc: db->errCode));
169217 z = sqlite3_value_text16(pVal: db->pErr);
169218 }
169219 /* A malloc() may have failed within the call to sqlite3_value_text16()
169220 ** above. If this is the case, then the db->mallocFailed flag needs to
169221 ** be cleared before returning. Do this directly, instead of via
169222 ** sqlite3ApiExit(), to avoid setting the database handle error message.
169223 */
169224 sqlite3OomClear(db);
169225 }
169226 sqlite3_mutex_leave(p: db->mutex);
169227 return z;
169228}
169229#endif /* SQLITE_OMIT_UTF16 */
169230
169231/*
169232** Return the most recent error code generated by an SQLite routine. If NULL is
169233** passed to this function, we assume a malloc() failed during sqlite3_open().
169234*/
169235SQLITE_API int sqlite3_errcode(sqlite3 *db){
169236 if( db && !sqlite3SafetyCheckSickOrOk(db) ){
169237 return SQLITE_MISUSE_BKPT;
169238 }
169239 if( !db || db->mallocFailed ){
169240 return SQLITE_NOMEM_BKPT;
169241 }
169242 return db->errCode & db->errMask;
169243}
169244SQLITE_API int sqlite3_extended_errcode(sqlite3 *db){
169245 if( db && !sqlite3SafetyCheckSickOrOk(db) ){
169246 return SQLITE_MISUSE_BKPT;
169247 }
169248 if( !db || db->mallocFailed ){
169249 return SQLITE_NOMEM_BKPT;
169250 }
169251 return db->errCode;
169252}
169253SQLITE_API int sqlite3_system_errno(sqlite3 *db){
169254 return db ? db->iSysErrno : 0;
169255}
169256
169257/*
169258** Return a string that describes the kind of error specified in the
169259** argument. For now, this simply calls the internal sqlite3ErrStr()
169260** function.
169261*/
169262SQLITE_API const char *sqlite3_errstr(int rc){
169263 return sqlite3ErrStr(rc);
169264}
169265
169266/*
169267** Create a new collating function for database "db". The name is zName
169268** and the encoding is enc.
169269*/
169270static int createCollation(
169271 sqlite3* db,
169272 const char *zName,
169273 u8 enc,
169274 void* pCtx,
169275 int(*xCompare)(void*,int,const void*,int,const void*),
169276 void(*xDel)(void*)
169277){
169278 CollSeq *pColl;
169279 int enc2;
169280
169281 assert( sqlite3_mutex_held(db->mutex) );
169282
169283 /* If SQLITE_UTF16 is specified as the encoding type, transform this
169284 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
169285 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
169286 */
169287 enc2 = enc;
169288 testcase( enc2==SQLITE_UTF16 );
169289 testcase( enc2==SQLITE_UTF16_ALIGNED );
169290 if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){
169291 enc2 = SQLITE_UTF16NATIVE;
169292 }
169293 if( enc2<SQLITE_UTF8 || enc2>SQLITE_UTF16BE ){
169294 return SQLITE_MISUSE_BKPT;
169295 }
169296
169297 /* Check if this call is removing or replacing an existing collation
169298 ** sequence. If so, and there are active VMs, return busy. If there
169299 ** are no active VMs, invalidate any pre-compiled statements.
169300 */
169301 pColl = sqlite3FindCollSeq(db, enc: (u8)enc2, zName, create: 0);
169302 if( pColl && pColl->xCmp ){
169303 if( db->nVdbeActive ){
169304 sqlite3ErrorWithMsg(db, SQLITE_BUSY,
169305 zFormat: "unable to delete/modify collation sequence due to active statements");
169306 return SQLITE_BUSY;
169307 }
169308 sqlite3ExpirePreparedStatements(db, iCode: 0);
169309
169310 /* If collation sequence pColl was created directly by a call to
169311 ** sqlite3_create_collation, and not generated by synthCollSeq(),
169312 ** then any copies made by synthCollSeq() need to be invalidated.
169313 ** Also, collation destructor - CollSeq.xDel() - function may need
169314 ** to be called.
169315 */
169316 if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
169317 CollSeq *aColl = sqlite3HashFind(pH: &db->aCollSeq, pKey: zName);
169318 int j;
169319 for(j=0; j<3; j++){
169320 CollSeq *p = &aColl[j];
169321 if( p->enc==pColl->enc ){
169322 if( p->xDel ){
169323 p->xDel(p->pUser);
169324 }
169325 p->xCmp = 0;
169326 }
169327 }
169328 }
169329 }
169330
169331 pColl = sqlite3FindCollSeq(db, enc: (u8)enc2, zName, create: 1);
169332 if( pColl==0 ) return SQLITE_NOMEM_BKPT;
169333 pColl->xCmp = xCompare;
169334 pColl->pUser = pCtx;
169335 pColl->xDel = xDel;
169336 pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
169337 sqlite3Error(db, SQLITE_OK);
169338 return SQLITE_OK;
169339}
169340
169341
169342/*
169343** This array defines hard upper bounds on limit values. The
169344** initializer must be kept in sync with the SQLITE_LIMIT_*
169345** #defines in sqlite3.h.
169346*/
169347static const int aHardLimit[] = {
169348 SQLITE_MAX_LENGTH,
169349 SQLITE_MAX_SQL_LENGTH,
169350 SQLITE_MAX_COLUMN,
169351 SQLITE_MAX_EXPR_DEPTH,
169352 SQLITE_MAX_COMPOUND_SELECT,
169353 SQLITE_MAX_VDBE_OP,
169354 SQLITE_MAX_FUNCTION_ARG,
169355 SQLITE_MAX_ATTACHED,
169356 SQLITE_MAX_LIKE_PATTERN_LENGTH,
169357 SQLITE_MAX_VARIABLE_NUMBER, /* IMP: R-38091-32352 */
169358 SQLITE_MAX_TRIGGER_DEPTH,
169359 SQLITE_MAX_WORKER_THREADS,
169360};
169361
169362/*
169363** Make sure the hard limits are set to reasonable values
169364*/
169365#if SQLITE_MAX_LENGTH<100
169366# error SQLITE_MAX_LENGTH must be at least 100
169367#endif
169368#if SQLITE_MAX_SQL_LENGTH<100
169369# error SQLITE_MAX_SQL_LENGTH must be at least 100
169370#endif
169371#if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH
169372# error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH
169373#endif
169374#if SQLITE_MAX_COMPOUND_SELECT<2
169375# error SQLITE_MAX_COMPOUND_SELECT must be at least 2
169376#endif
169377#if SQLITE_MAX_VDBE_OP<40
169378# error SQLITE_MAX_VDBE_OP must be at least 40
169379#endif
169380#if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>127
169381# error SQLITE_MAX_FUNCTION_ARG must be between 0 and 127
169382#endif
169383#if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>125
169384# error SQLITE_MAX_ATTACHED must be between 0 and 125
169385#endif
169386#if SQLITE_MAX_LIKE_PATTERN_LENGTH<1
169387# error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1
169388#endif
169389#if SQLITE_MAX_COLUMN>32767
169390# error SQLITE_MAX_COLUMN must not exceed 32767
169391#endif
169392#if SQLITE_MAX_TRIGGER_DEPTH<1
169393# error SQLITE_MAX_TRIGGER_DEPTH must be at least 1
169394#endif
169395#if SQLITE_MAX_WORKER_THREADS<0 || SQLITE_MAX_WORKER_THREADS>50
169396# error SQLITE_MAX_WORKER_THREADS must be between 0 and 50
169397#endif
169398
169399
169400/*
169401** Change the value of a limit. Report the old value.
169402** If an invalid limit index is supplied, report -1.
169403** Make no changes but still report the old value if the
169404** new limit is negative.
169405**
169406** A new lower limit does not shrink existing constructs.
169407** It merely prevents new constructs that exceed the limit
169408** from forming.
169409*/
169410SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
169411 int oldLimit;
169412
169413#ifdef SQLITE_ENABLE_API_ARMOR
169414 if( !sqlite3SafetyCheckOk(db) ){
169415 (void)SQLITE_MISUSE_BKPT;
169416 return -1;
169417 }
169418#endif
169419
169420 /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
169421 ** there is a hard upper bound set at compile-time by a C preprocessor
169422 ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to
169423 ** "_MAX_".)
169424 */
169425 assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH );
169426 assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH );
169427 assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN );
169428 assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH );
169429 assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT);
169430 assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP );
169431 assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG );
169432 assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED );
169433 assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]==
169434 SQLITE_MAX_LIKE_PATTERN_LENGTH );
169435 assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER);
169436 assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH );
169437 assert( aHardLimit[SQLITE_LIMIT_WORKER_THREADS]==SQLITE_MAX_WORKER_THREADS );
169438 assert( SQLITE_LIMIT_WORKER_THREADS==(SQLITE_N_LIMIT-1) );
169439
169440
169441 if( limitId<0 || limitId>=SQLITE_N_LIMIT ){
169442 return -1;
169443 }
169444 oldLimit = db->aLimit[limitId];
169445 if( newLimit>=0 ){ /* IMP: R-52476-28732 */
169446 if( newLimit>aHardLimit[limitId] ){
169447 newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */
169448 }else if( newLimit<1 && limitId==SQLITE_LIMIT_LENGTH ){
169449 newLimit = 1;
169450 }
169451 db->aLimit[limitId] = newLimit;
169452 }
169453 return oldLimit; /* IMP: R-53341-35419 */
169454}
169455
169456/*
169457** This function is used to parse both URIs and non-URI filenames passed by the
169458** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database
169459** URIs specified as part of ATTACH statements.
169460**
169461** The first argument to this function is the name of the VFS to use (or
169462** a NULL to signify the default VFS) if the URI does not contain a "vfs=xxx"
169463** query parameter. The second argument contains the URI (or non-URI filename)
169464** itself. When this function is called the *pFlags variable should contain
169465** the default flags to open the database handle with. The value stored in
169466** *pFlags may be updated before returning if the URI filename contains
169467** "cache=xxx" or "mode=xxx" query parameters.
169468**
169469** If successful, SQLITE_OK is returned. In this case *ppVfs is set to point to
169470** the VFS that should be used to open the database file. *pzFile is set to
169471** point to a buffer containing the name of the file to open. The value
169472** stored in *pzFile is a database name acceptable to sqlite3_uri_parameter()
169473** and is in the same format as names created using sqlite3_create_filename().
169474** The caller must invoke sqlite3_free_filename() (not sqlite3_free()!) on
169475** the value returned in *pzFile to avoid a memory leak.
169476**
169477** If an error occurs, then an SQLite error code is returned and *pzErrMsg
169478** may be set to point to a buffer containing an English language error
169479** message. It is the responsibility of the caller to eventually release
169480** this buffer by calling sqlite3_free().
169481*/
169482SQLITE_PRIVATE int sqlite3ParseUri(
169483 const char *zDefaultVfs, /* VFS to use if no "vfs=xxx" query option */
169484 const char *zUri, /* Nul-terminated URI to parse */
169485 unsigned int *pFlags, /* IN/OUT: SQLITE_OPEN_XXX flags */
169486 sqlite3_vfs **ppVfs, /* OUT: VFS to use */
169487 char **pzFile, /* OUT: Filename component of URI */
169488 char **pzErrMsg /* OUT: Error message (if rc!=SQLITE_OK) */
169489){
169490 int rc = SQLITE_OK;
169491 unsigned int flags = *pFlags;
169492 const char *zVfs = zDefaultVfs;
169493 char *zFile;
169494 char c;
169495 int nUri = sqlite3Strlen30(z: zUri);
169496
169497 assert( *pzErrMsg==0 );
169498
169499 if( ((flags & SQLITE_OPEN_URI) /* IMP: R-48725-32206 */
169500 || sqlite3GlobalConfig.bOpenUri) /* IMP: R-51689-46548 */
169501 && nUri>=5 && memcmp(s1: zUri, s2: "file:", n: 5)==0 /* IMP: R-57884-37496 */
169502 ){
169503 char *zOpt;
169504 int eState; /* Parser state when parsing URI */
169505 int iIn; /* Input character index */
169506 int iOut = 0; /* Output character index */
169507 u64 nByte = nUri+8; /* Bytes of space to allocate */
169508
169509 /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen
169510 ** method that there may be extra parameters following the file-name. */
169511 flags |= SQLITE_OPEN_URI;
169512
169513 for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&');
169514 zFile = sqlite3_malloc64(n: nByte);
169515 if( !zFile ) return SQLITE_NOMEM_BKPT;
169516
169517 memset(s: zFile, c: 0, n: 4); /* 4-byte of 0x00 is the start of DB name marker */
169518 zFile += 4;
169519
169520 iIn = 5;
169521#ifdef SQLITE_ALLOW_URI_AUTHORITY
169522 if( strncmp(zUri+5, "///", 3)==0 ){
169523 iIn = 7;
169524 /* The following condition causes URIs with five leading / characters
169525 ** like file://///host/path to be converted into UNCs like //host/path.
169526 ** The correct URI for that UNC has only two or four leading / characters
169527 ** file://host/path or file:////host/path. But 5 leading slashes is a
169528 ** common error, we are told, so we handle it as a special case. */
169529 if( strncmp(zUri+7, "///", 3)==0 ){ iIn++; }
169530 }else if( strncmp(zUri+5, "//localhost/", 12)==0 ){
169531 iIn = 16;
169532 }
169533#else
169534 /* Discard the scheme and authority segments of the URI. */
169535 if( zUri[5]=='/' && zUri[6]=='/' ){
169536 iIn = 7;
169537 while( zUri[iIn] && zUri[iIn]!='/' ) iIn++;
169538 if( iIn!=7 && (iIn!=16 || memcmp(s1: "localhost", s2: &zUri[7], n: 9)) ){
169539 *pzErrMsg = sqlite3_mprintf(zFormat: "invalid uri authority: %.*s",
169540 iIn-7, &zUri[7]);
169541 rc = SQLITE_ERROR;
169542 goto parse_uri_out;
169543 }
169544 }
169545#endif
169546
169547 /* Copy the filename and any query parameters into the zFile buffer.
169548 ** Decode %HH escape codes along the way.
169549 **
169550 ** Within this loop, variable eState may be set to 0, 1 or 2, depending
169551 ** on the parsing context. As follows:
169552 **
169553 ** 0: Parsing file-name.
169554 ** 1: Parsing name section of a name=value query parameter.
169555 ** 2: Parsing value section of a name=value query parameter.
169556 */
169557 eState = 0;
169558 while( (c = zUri[iIn])!=0 && c!='#' ){
169559 iIn++;
169560 if( c=='%'
169561 && sqlite3Isxdigit(zUri[iIn])
169562 && sqlite3Isxdigit(zUri[iIn+1])
169563 ){
169564 int octet = (sqlite3HexToInt(h: zUri[iIn++]) << 4);
169565 octet += sqlite3HexToInt(h: zUri[iIn++]);
169566
169567 assert( octet>=0 && octet<256 );
169568 if( octet==0 ){
169569#ifndef SQLITE_ENABLE_URI_00_ERROR
169570 /* This branch is taken when "%00" appears within the URI. In this
169571 ** case we ignore all text in the remainder of the path, name or
169572 ** value currently being parsed. So ignore the current character
169573 ** and skip to the next "?", "=" or "&", as appropriate. */
169574 while( (c = zUri[iIn])!=0 && c!='#'
169575 && (eState!=0 || c!='?')
169576 && (eState!=1 || (c!='=' && c!='&'))
169577 && (eState!=2 || c!='&')
169578 ){
169579 iIn++;
169580 }
169581 continue;
169582#else
169583 /* If ENABLE_URI_00_ERROR is defined, "%00" in a URI is an error. */
169584 *pzErrMsg = sqlite3_mprintf("unexpected %%00 in uri");
169585 rc = SQLITE_ERROR;
169586 goto parse_uri_out;
169587#endif
169588 }
169589 c = octet;
169590 }else if( eState==1 && (c=='&' || c=='=') ){
169591 if( zFile[iOut-1]==0 ){
169592 /* An empty option name. Ignore this option altogether. */
169593 while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++;
169594 continue;
169595 }
169596 if( c=='&' ){
169597 zFile[iOut++] = '\0';
169598 }else{
169599 eState = 2;
169600 }
169601 c = 0;
169602 }else if( (eState==0 && c=='?') || (eState==2 && c=='&') ){
169603 c = 0;
169604 eState = 1;
169605 }
169606 zFile[iOut++] = c;
169607 }
169608 if( eState==1 ) zFile[iOut++] = '\0';
169609 memset(s: zFile+iOut, c: 0, n: 4); /* end-of-options + empty journal filenames */
169610
169611 /* Check if there were any options specified that should be interpreted
169612 ** here. Options that are interpreted here include "vfs" and those that
169613 ** correspond to flags that may be passed to the sqlite3_open_v2()
169614 ** method. */
169615 zOpt = &zFile[sqlite3Strlen30(z: zFile)+1];
169616 while( zOpt[0] ){
169617 int nOpt = sqlite3Strlen30(z: zOpt);
169618 char *zVal = &zOpt[nOpt+1];
169619 int nVal = sqlite3Strlen30(z: zVal);
169620
169621 if( nOpt==3 && memcmp(s1: "vfs", s2: zOpt, n: 3)==0 ){
169622 zVfs = zVal;
169623 }else{
169624 struct OpenMode {
169625 const char *z;
169626 int mode;
169627 } *aMode = 0;
169628 char *zModeType = 0;
169629 int mask = 0;
169630 int limit = 0;
169631
169632 if( nOpt==5 && memcmp(s1: "cache", s2: zOpt, n: 5)==0 ){
169633 static struct OpenMode aCacheMode[] = {
169634 { "shared", SQLITE_OPEN_SHAREDCACHE },
169635 { "private", SQLITE_OPEN_PRIVATECACHE },
169636 { 0, 0 }
169637 };
169638
169639 mask = SQLITE_OPEN_SHAREDCACHE|SQLITE_OPEN_PRIVATECACHE;
169640 aMode = aCacheMode;
169641 limit = mask;
169642 zModeType = "cache";
169643 }
169644 if( nOpt==4 && memcmp(s1: "mode", s2: zOpt, n: 4)==0 ){
169645 static struct OpenMode aOpenMode[] = {
169646 { "ro", SQLITE_OPEN_READONLY },
169647 { "rw", SQLITE_OPEN_READWRITE },
169648 { "rwc", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE },
169649 { "memory", SQLITE_OPEN_MEMORY },
169650 { 0, 0 }
169651 };
169652
169653 mask = SQLITE_OPEN_READONLY | SQLITE_OPEN_READWRITE
169654 | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY;
169655 aMode = aOpenMode;
169656 limit = mask & flags;
169657 zModeType = "access";
169658 }
169659
169660 if( aMode ){
169661 int i;
169662 int mode = 0;
169663 for(i=0; aMode[i].z; i++){
169664 const char *z = aMode[i].z;
169665 if( nVal==sqlite3Strlen30(z) && 0==memcmp(s1: zVal, s2: z, n: nVal) ){
169666 mode = aMode[i].mode;
169667 break;
169668 }
169669 }
169670 if( mode==0 ){
169671 *pzErrMsg = sqlite3_mprintf(zFormat: "no such %s mode: %s", zModeType, zVal);
169672 rc = SQLITE_ERROR;
169673 goto parse_uri_out;
169674 }
169675 if( (mode & ~SQLITE_OPEN_MEMORY)>limit ){
169676 *pzErrMsg = sqlite3_mprintf(zFormat: "%s mode not allowed: %s",
169677 zModeType, zVal);
169678 rc = SQLITE_PERM;
169679 goto parse_uri_out;
169680 }
169681 flags = (flags & ~mask) | mode;
169682 }
169683 }
169684
169685 zOpt = &zVal[nVal+1];
169686 }
169687
169688 }else{
169689 zFile = sqlite3_malloc64(n: nUri+8);
169690 if( !zFile ) return SQLITE_NOMEM_BKPT;
169691 memset(s: zFile, c: 0, n: 4);
169692 zFile += 4;
169693 if( nUri ){
169694 memcpy(dest: zFile, src: zUri, n: nUri);
169695 }
169696 memset(s: zFile+nUri, c: 0, n: 4);
169697 flags &= ~SQLITE_OPEN_URI;
169698 }
169699
169700 *ppVfs = sqlite3_vfs_find(zVfs);
169701 if( *ppVfs==0 ){
169702 *pzErrMsg = sqlite3_mprintf(zFormat: "no such vfs: %s", zVfs);
169703 rc = SQLITE_ERROR;
169704 }
169705 parse_uri_out:
169706 if( rc!=SQLITE_OK ){
169707 sqlite3_free_filename(zFile);
169708 zFile = 0;
169709 }
169710 *pFlags = flags;
169711 *pzFile = zFile;
169712 return rc;
169713}
169714
169715/*
169716** This routine does the core work of extracting URI parameters from a
169717** database filename for the sqlite3_uri_parameter() interface.
169718*/
169719static const char *uriParameter(const char *zFilename, const char *zParam){
169720 zFilename += sqlite3Strlen30(z: zFilename) + 1;
169721 while( ALWAYS(zFilename!=0) && zFilename[0] ){
169722 int x = strcmp(s1: zFilename, s2: zParam);
169723 zFilename += sqlite3Strlen30(z: zFilename) + 1;
169724 if( x==0 ) return zFilename;
169725 zFilename += sqlite3Strlen30(z: zFilename) + 1;
169726 }
169727 return 0;
169728}
169729
169730
169731
169732/*
169733** This routine does the work of opening a database on behalf of
169734** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
169735** is UTF-8 encoded.
169736*/
169737static int openDatabase(
169738 const char *zFilename, /* Database filename UTF-8 encoded */
169739 sqlite3 **ppDb, /* OUT: Returned database handle */
169740 unsigned int flags, /* Operational flags */
169741 const char *zVfs /* Name of the VFS to use */
169742){
169743 sqlite3 *db; /* Store allocated handle here */
169744 int rc; /* Return code */
169745 int isThreadsafe; /* True for threadsafe connections */
169746 char *zOpen = 0; /* Filename argument to pass to BtreeOpen() */
169747 char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */
169748 int i; /* Loop counter */
169749
169750#ifdef SQLITE_ENABLE_API_ARMOR
169751 if( ppDb==0 ) return SQLITE_MISUSE_BKPT;
169752#endif
169753 *ppDb = 0;
169754#ifndef SQLITE_OMIT_AUTOINIT
169755 rc = sqlite3_initialize();
169756 if( rc ) return rc;
169757#endif
169758
169759 if( sqlite3GlobalConfig.bCoreMutex==0 ){
169760 isThreadsafe = 0;
169761 }else if( flags & SQLITE_OPEN_NOMUTEX ){
169762 isThreadsafe = 0;
169763 }else if( flags & SQLITE_OPEN_FULLMUTEX ){
169764 isThreadsafe = 1;
169765 }else{
169766 isThreadsafe = sqlite3GlobalConfig.bFullMutex;
169767 }
169768
169769 if( flags & SQLITE_OPEN_PRIVATECACHE ){
169770 flags &= ~SQLITE_OPEN_SHAREDCACHE;
169771 }else if( sqlite3GlobalConfig.sharedCacheEnabled ){
169772 flags |= SQLITE_OPEN_SHAREDCACHE;
169773 }
169774
169775 /* Remove harmful bits from the flags parameter
169776 **
169777 ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were
169778 ** dealt with in the previous code block. Besides these, the only
169779 ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY,
169780 ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE,
169781 ** SQLITE_OPEN_PRIVATECACHE, SQLITE_OPEN_EXRESCODE, and some reserved
169782 ** bits. Silently mask off all other flags.
169783 */
169784 flags &= ~( SQLITE_OPEN_DELETEONCLOSE |
169785 SQLITE_OPEN_EXCLUSIVE |
169786 SQLITE_OPEN_MAIN_DB |
169787 SQLITE_OPEN_TEMP_DB |
169788 SQLITE_OPEN_TRANSIENT_DB |
169789 SQLITE_OPEN_MAIN_JOURNAL |
169790 SQLITE_OPEN_TEMP_JOURNAL |
169791 SQLITE_OPEN_SUBJOURNAL |
169792 SQLITE_OPEN_SUPER_JOURNAL |
169793 SQLITE_OPEN_NOMUTEX |
169794 SQLITE_OPEN_FULLMUTEX |
169795 SQLITE_OPEN_WAL
169796 );
169797
169798 /* Allocate the sqlite data structure */
169799 db = sqlite3MallocZero( n: sizeof(sqlite3) );
169800 if( db==0 ) goto opendb_out;
169801 if( isThreadsafe
169802#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
169803 || sqlite3GlobalConfig.bCoreMutex
169804#endif
169805 ){
169806 db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
169807 if( db->mutex==0 ){
169808 sqlite3_free(p: db);
169809 db = 0;
169810 goto opendb_out;
169811 }
169812 if( isThreadsafe==0 ){
169813 sqlite3MutexWarnOnContention(db->mutex);
169814 }
169815 }
169816 sqlite3_mutex_enter(p: db->mutex);
169817 db->errMask = (flags & SQLITE_OPEN_EXRESCODE)!=0 ? 0xffffffff : 0xff;
169818 db->nDb = 2;
169819 db->eOpenState = SQLITE_STATE_BUSY;
169820 db->aDb = db->aDbStatic;
169821 db->lookaside.bDisable = 1;
169822 db->lookaside.sz = 0;
169823
169824 assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
169825 memcpy(dest: db->aLimit, src: aHardLimit, n: sizeof(db->aLimit));
169826 db->aLimit[SQLITE_LIMIT_WORKER_THREADS] = SQLITE_DEFAULT_WORKER_THREADS;
169827 db->autoCommit = 1;
169828 db->nextAutovac = -1;
169829 db->szMmap = sqlite3GlobalConfig.szMmap;
169830 db->nextPagesize = 0;
169831 db->init.azInit = sqlite3StdType; /* Any array of string ptrs will do */
169832#ifdef SQLITE_ENABLE_SORTER_MMAP
169833 /* Beginning with version 3.37.0, using the VFS xFetch() API to memory-map
169834 ** the temporary files used to do external sorts (see code in vdbesort.c)
169835 ** is disabled. It can still be used either by defining
169836 ** SQLITE_ENABLE_SORTER_MMAP at compile time or by using the
169837 ** SQLITE_TESTCTRL_SORTER_MMAP test-control at runtime. */
169838 db->nMaxSorterMmap = 0x7FFFFFFF;
169839#endif
169840 db->flags |= SQLITE_ShortColNames
169841 | SQLITE_EnableTrigger
169842 | SQLITE_EnableView
169843 | SQLITE_CacheSpill
169844#if !defined(SQLITE_TRUSTED_SCHEMA) || SQLITE_TRUSTED_SCHEMA+0!=0
169845 | SQLITE_TrustedSchema
169846#endif
169847/* The SQLITE_DQS compile-time option determines the default settings
169848** for SQLITE_DBCONFIG_DQS_DDL and SQLITE_DBCONFIG_DQS_DML.
169849**
169850** SQLITE_DQS SQLITE_DBCONFIG_DQS_DDL SQLITE_DBCONFIG_DQS_DML
169851** ---------- ----------------------- -----------------------
169852** undefined on on
169853** 3 on on
169854** 2 on off
169855** 1 off on
169856** 0 off off
169857**
169858** Legacy behavior is 3 (double-quoted string literals are allowed anywhere)
169859** and so that is the default. But developers are encouranged to use
169860** -DSQLITE_DQS=0 (best) or -DSQLITE_DQS=1 (second choice) if possible.
169861*/
169862#if !defined(SQLITE_DQS)
169863# define SQLITE_DQS 3
169864#endif
169865#if (SQLITE_DQS&1)==1
169866 | SQLITE_DqsDML
169867#endif
169868#if (SQLITE_DQS&2)==2
169869 | SQLITE_DqsDDL
169870#endif
169871
169872#if !defined(SQLITE_DEFAULT_AUTOMATIC_INDEX) || SQLITE_DEFAULT_AUTOMATIC_INDEX
169873 | SQLITE_AutoIndex
169874#endif
169875#if SQLITE_DEFAULT_CKPTFULLFSYNC
169876 | SQLITE_CkptFullFSync
169877#endif
169878#if SQLITE_DEFAULT_FILE_FORMAT<4
169879 | SQLITE_LegacyFileFmt
169880#endif
169881#ifdef SQLITE_ENABLE_LOAD_EXTENSION
169882 | SQLITE_LoadExtension
169883#endif
169884#if SQLITE_DEFAULT_RECURSIVE_TRIGGERS
169885 | SQLITE_RecTriggers
169886#endif
169887#if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS
169888 | SQLITE_ForeignKeys
169889#endif
169890#if defined(SQLITE_REVERSE_UNORDERED_SELECTS)
169891 | SQLITE_ReverseOrder
169892#endif
169893#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
169894 | SQLITE_CellSizeCk
169895#endif
169896#if defined(SQLITE_ENABLE_FTS3_TOKENIZER)
169897 | SQLITE_Fts3Tokenizer
169898#endif
169899#if defined(SQLITE_ENABLE_QPSG)
169900 | SQLITE_EnableQPSG
169901#endif
169902#if defined(SQLITE_DEFAULT_DEFENSIVE)
169903 | SQLITE_Defensive
169904#endif
169905#if defined(SQLITE_DEFAULT_LEGACY_ALTER_TABLE)
169906 | SQLITE_LegacyAlter
169907#endif
169908 ;
169909 sqlite3HashInit(pNew: &db->aCollSeq);
169910#ifndef SQLITE_OMIT_VIRTUALTABLE
169911 sqlite3HashInit(pNew: &db->aModule);
169912#endif
169913
169914 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
169915 ** and UTF-16, so add a version for each to avoid any unnecessary
169916 ** conversions. The only error that can occur here is a malloc() failure.
169917 **
169918 ** EVIDENCE-OF: R-52786-44878 SQLite defines three built-in collating
169919 ** functions:
169920 */
169921 createCollation(db, zName: sqlite3StrBINARY, SQLITE_UTF8, pCtx: 0, xCompare: binCollFunc, xDel: 0);
169922 createCollation(db, zName: sqlite3StrBINARY, SQLITE_UTF16BE, pCtx: 0, xCompare: binCollFunc, xDel: 0);
169923 createCollation(db, zName: sqlite3StrBINARY, SQLITE_UTF16LE, pCtx: 0, xCompare: binCollFunc, xDel: 0);
169924 createCollation(db, zName: "NOCASE", SQLITE_UTF8, pCtx: 0, xCompare: nocaseCollatingFunc, xDel: 0);
169925 createCollation(db, zName: "RTRIM", SQLITE_UTF8, pCtx: 0, xCompare: rtrimCollFunc, xDel: 0);
169926 if( db->mallocFailed ){
169927 goto opendb_out;
169928 }
169929
169930 /* Parse the filename/URI argument
169931 **
169932 ** Only allow sensible combinations of bits in the flags argument.
169933 ** Throw an error if any non-sense combination is used. If we
169934 ** do not block illegal combinations here, it could trigger
169935 ** assert() statements in deeper layers. Sensible combinations
169936 ** are:
169937 **
169938 ** 1: SQLITE_OPEN_READONLY
169939 ** 2: SQLITE_OPEN_READWRITE
169940 ** 6: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
169941 */
169942 db->openFlags = flags;
169943 assert( SQLITE_OPEN_READONLY == 0x01 );
169944 assert( SQLITE_OPEN_READWRITE == 0x02 );
169945 assert( SQLITE_OPEN_CREATE == 0x04 );
169946 testcase( (1<<(flags&7))==0x02 ); /* READONLY */
169947 testcase( (1<<(flags&7))==0x04 ); /* READWRITE */
169948 testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */
169949 if( ((1<<(flags&7)) & 0x46)==0 ){
169950 rc = SQLITE_MISUSE_BKPT; /* IMP: R-18321-05872 */
169951 }else{
169952 rc = sqlite3ParseUri(zDefaultVfs: zVfs, zUri: zFilename, pFlags: &flags, ppVfs: &db->pVfs, pzFile: &zOpen, pzErrMsg: &zErrMsg);
169953 }
169954 if( rc!=SQLITE_OK ){
169955 if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
169956 sqlite3ErrorWithMsg(db, err_code: rc, zFormat: zErrMsg ? "%s" : 0, zErrMsg);
169957 sqlite3_free(p: zErrMsg);
169958 goto opendb_out;
169959 }
169960
169961 /* Open the backend database driver */
169962 rc = sqlite3BtreeOpen(pVfs: db->pVfs, zFilename: zOpen, db, ppBtree: &db->aDb[0].pBt, flags: 0,
169963 vfsFlags: flags | SQLITE_OPEN_MAIN_DB);
169964 if( rc!=SQLITE_OK ){
169965 if( rc==SQLITE_IOERR_NOMEM ){
169966 rc = SQLITE_NOMEM_BKPT;
169967 }
169968 sqlite3Error(db, err_code: rc);
169969 goto opendb_out;
169970 }
169971 sqlite3BtreeEnter(p: db->aDb[0].pBt);
169972 db->aDb[0].pSchema = sqlite3SchemaGet(db, pBt: db->aDb[0].pBt);
169973 if( !db->mallocFailed ){
169974 sqlite3SetTextEncoding(db, SCHEMA_ENC(db));
169975 }
169976 sqlite3BtreeLeave(p: db->aDb[0].pBt);
169977 db->aDb[1].pSchema = sqlite3SchemaGet(db, pBt: 0);
169978
169979 /* The default safety_level for the main database is FULL; for the temp
169980 ** database it is OFF. This matches the pager layer defaults.
169981 */
169982 db->aDb[0].zDbSName = "main";
169983 db->aDb[0].safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
169984 db->aDb[1].zDbSName = "temp";
169985 db->aDb[1].safety_level = PAGER_SYNCHRONOUS_OFF;
169986
169987 db->eOpenState = SQLITE_STATE_OPEN;
169988 if( db->mallocFailed ){
169989 goto opendb_out;
169990 }
169991
169992 /* Register all built-in functions, but do not attempt to read the
169993 ** database schema yet. This is delayed until the first time the database
169994 ** is accessed.
169995 */
169996 sqlite3Error(db, SQLITE_OK);
169997 sqlite3RegisterPerConnectionBuiltinFunctions(db);
169998 rc = sqlite3_errcode(db);
169999
170000
170001 /* Load compiled-in extensions */
170002 for(i=0; rc==SQLITE_OK && i<ArraySize(sqlite3BuiltinExtensions); i++){
170003 rc = sqlite3BuiltinExtensions[i](db);
170004 }
170005
170006 /* Load automatic extensions - extensions that have been registered
170007 ** using the sqlite3_automatic_extension() API.
170008 */
170009 if( rc==SQLITE_OK ){
170010 sqlite3AutoLoadExtensions(db);
170011 rc = sqlite3_errcode(db);
170012 if( rc!=SQLITE_OK ){
170013 goto opendb_out;
170014 }
170015 }
170016
170017#ifdef SQLITE_ENABLE_INTERNAL_FUNCTIONS
170018 /* Testing use only!!! The -DSQLITE_ENABLE_INTERNAL_FUNCTIONS=1 compile-time
170019 ** option gives access to internal functions by default.
170020 ** Testing use only!!! */
170021 db->mDbFlags |= DBFLAG_InternalFunc;
170022#endif
170023
170024 /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
170025 ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
170026 ** mode. Doing nothing at all also makes NORMAL the default.
170027 */
170028#ifdef SQLITE_DEFAULT_LOCKING_MODE
170029 db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
170030 sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
170031 SQLITE_DEFAULT_LOCKING_MODE);
170032#endif
170033
170034 if( rc ) sqlite3Error(db, err_code: rc);
170035
170036 /* Enable the lookaside-malloc subsystem */
170037 setupLookaside(db, pBuf: 0, sqlite3GlobalConfig.szLookaside,
170038 sqlite3GlobalConfig.nLookaside);
170039
170040 sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT);
170041
170042opendb_out:
170043 if( db ){
170044 assert( db->mutex!=0 || isThreadsafe==0
170045 || sqlite3GlobalConfig.bFullMutex==0 );
170046 sqlite3_mutex_leave(p: db->mutex);
170047 }
170048 rc = sqlite3_errcode(db);
170049 assert( db!=0 || (rc&0xff)==SQLITE_NOMEM );
170050 if( (rc&0xff)==SQLITE_NOMEM ){
170051 sqlite3_close(db);
170052 db = 0;
170053 }else if( rc!=SQLITE_OK ){
170054 db->eOpenState = SQLITE_STATE_SICK;
170055 }
170056 *ppDb = db;
170057#ifdef SQLITE_ENABLE_SQLLOG
170058 if( sqlite3GlobalConfig.xSqllog ){
170059 /* Opening a db handle. Fourth parameter is passed 0. */
170060 void *pArg = sqlite3GlobalConfig.pSqllogArg;
170061 sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0);
170062 }
170063#endif
170064 sqlite3_free_filename(zOpen);
170065 return rc;
170066}
170067
170068
170069/*
170070** Open a new database handle.
170071*/
170072SQLITE_API int sqlite3_open(
170073 const char *zFilename,
170074 sqlite3 **ppDb
170075){
170076 return openDatabase(zFilename, ppDb,
170077 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, zVfs: 0);
170078}
170079SQLITE_API int sqlite3_open_v2(
170080 const char *filename, /* Database filename (UTF-8) */
170081 sqlite3 **ppDb, /* OUT: SQLite db handle */
170082 int flags, /* Flags */
170083 const char *zVfs /* Name of VFS module to use */
170084){
170085 return openDatabase(zFilename: filename, ppDb, flags: (unsigned int)flags, zVfs);
170086}
170087
170088#ifndef SQLITE_OMIT_UTF16
170089/*
170090** Open a new database handle.
170091*/
170092SQLITE_API int sqlite3_open16(
170093 const void *zFilename,
170094 sqlite3 **ppDb
170095){
170096 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
170097 sqlite3_value *pVal;
170098 int rc;
170099
170100#ifdef SQLITE_ENABLE_API_ARMOR
170101 if( ppDb==0 ) return SQLITE_MISUSE_BKPT;
170102#endif
170103 *ppDb = 0;
170104#ifndef SQLITE_OMIT_AUTOINIT
170105 rc = sqlite3_initialize();
170106 if( rc ) return rc;
170107#endif
170108 if( zFilename==0 ) zFilename = "\000\000";
170109 pVal = sqlite3ValueNew(db: 0);
170110 sqlite3ValueSetStr(v: pVal, n: -1, z: zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
170111 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
170112 if( zFilename8 ){
170113 rc = openDatabase(zFilename: zFilename8, ppDb,
170114 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, zVfs: 0);
170115 assert( *ppDb || rc==SQLITE_NOMEM );
170116 if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){
170117 SCHEMA_ENC(*ppDb) = ENC(*ppDb) = SQLITE_UTF16NATIVE;
170118 }
170119 }else{
170120 rc = SQLITE_NOMEM_BKPT;
170121 }
170122 sqlite3ValueFree(v: pVal);
170123
170124 return rc & 0xff;
170125}
170126#endif /* SQLITE_OMIT_UTF16 */
170127
170128/*
170129** Register a new collation sequence with the database handle db.
170130*/
170131SQLITE_API int sqlite3_create_collation(
170132 sqlite3* db,
170133 const char *zName,
170134 int enc,
170135 void* pCtx,
170136 int(*xCompare)(void*,int,const void*,int,const void*)
170137){
170138 return sqlite3_create_collation_v2(db, zName, eTextRep: enc, pArg: pCtx, xCompare, xDestroy: 0);
170139}
170140
170141/*
170142** Register a new collation sequence with the database handle db.
170143*/
170144SQLITE_API int sqlite3_create_collation_v2(
170145 sqlite3* db,
170146 const char *zName,
170147 int enc,
170148 void* pCtx,
170149 int(*xCompare)(void*,int,const void*,int,const void*),
170150 void(*xDel)(void*)
170151){
170152 int rc;
170153
170154#ifdef SQLITE_ENABLE_API_ARMOR
170155 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
170156#endif
170157 sqlite3_mutex_enter(p: db->mutex);
170158 assert( !db->mallocFailed );
170159 rc = createCollation(db, zName, enc: (u8)enc, pCtx, xCompare, xDel);
170160 rc = sqlite3ApiExit(db, rc);
170161 sqlite3_mutex_leave(p: db->mutex);
170162 return rc;
170163}
170164
170165#ifndef SQLITE_OMIT_UTF16
170166/*
170167** Register a new collation sequence with the database handle db.
170168*/
170169SQLITE_API int sqlite3_create_collation16(
170170 sqlite3* db,
170171 const void *zName,
170172 int enc,
170173 void* pCtx,
170174 int(*xCompare)(void*,int,const void*,int,const void*)
170175){
170176 int rc = SQLITE_OK;
170177 char *zName8;
170178
170179#ifdef SQLITE_ENABLE_API_ARMOR
170180 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
170181#endif
170182 sqlite3_mutex_enter(p: db->mutex);
170183 assert( !db->mallocFailed );
170184 zName8 = sqlite3Utf16to8(db, z: zName, nByte: -1, SQLITE_UTF16NATIVE);
170185 if( zName8 ){
170186 rc = createCollation(db, zName: zName8, enc: (u8)enc, pCtx, xCompare, xDel: 0);
170187 sqlite3DbFree(db, p: zName8);
170188 }
170189 rc = sqlite3ApiExit(db, rc);
170190 sqlite3_mutex_leave(p: db->mutex);
170191 return rc;
170192}
170193#endif /* SQLITE_OMIT_UTF16 */
170194
170195/*
170196** Register a collation sequence factory callback with the database handle
170197** db. Replace any previously installed collation sequence factory.
170198*/
170199SQLITE_API int sqlite3_collation_needed(
170200 sqlite3 *db,
170201 void *pCollNeededArg,
170202 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
170203){
170204#ifdef SQLITE_ENABLE_API_ARMOR
170205 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
170206#endif
170207 sqlite3_mutex_enter(p: db->mutex);
170208 db->xCollNeeded = xCollNeeded;
170209 db->xCollNeeded16 = 0;
170210 db->pCollNeededArg = pCollNeededArg;
170211 sqlite3_mutex_leave(p: db->mutex);
170212 return SQLITE_OK;
170213}
170214
170215#ifndef SQLITE_OMIT_UTF16
170216/*
170217** Register a collation sequence factory callback with the database handle
170218** db. Replace any previously installed collation sequence factory.
170219*/
170220SQLITE_API int sqlite3_collation_needed16(
170221 sqlite3 *db,
170222 void *pCollNeededArg,
170223 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
170224){
170225#ifdef SQLITE_ENABLE_API_ARMOR
170226 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
170227#endif
170228 sqlite3_mutex_enter(p: db->mutex);
170229 db->xCollNeeded = 0;
170230 db->xCollNeeded16 = xCollNeeded16;
170231 db->pCollNeededArg = pCollNeededArg;
170232 sqlite3_mutex_leave(p: db->mutex);
170233 return SQLITE_OK;
170234}
170235#endif /* SQLITE_OMIT_UTF16 */
170236
170237#ifndef SQLITE_OMIT_DEPRECATED
170238/*
170239** This function is now an anachronism. It used to be used to recover from a
170240** malloc() failure, but SQLite now does this automatically.
170241*/
170242SQLITE_API int sqlite3_global_recover(void){
170243 return SQLITE_OK;
170244}
170245#endif
170246
170247/*
170248** Test to see whether or not the database connection is in autocommit
170249** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on
170250** by default. Autocommit is disabled by a BEGIN statement and reenabled
170251** by the next COMMIT or ROLLBACK.
170252*/
170253SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){
170254#ifdef SQLITE_ENABLE_API_ARMOR
170255 if( !sqlite3SafetyCheckOk(db) ){
170256 (void)SQLITE_MISUSE_BKPT;
170257 return 0;
170258 }
170259#endif
170260 return db->autoCommit;
170261}
170262
170263/*
170264** The following routines are substitutes for constants SQLITE_CORRUPT,
170265** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_NOMEM and possibly other error
170266** constants. They serve two purposes:
170267**
170268** 1. Serve as a convenient place to set a breakpoint in a debugger
170269** to detect when version error conditions occurs.
170270**
170271** 2. Invoke sqlite3_log() to provide the source code location where
170272** a low-level error is first detected.
170273*/
170274SQLITE_PRIVATE int sqlite3ReportError(int iErr, int lineno, const char *zType){
170275 sqlite3_log(iErrCode: iErr, zFormat: "%s at line %d of [%.10s]",
170276 zType, lineno, 20+sqlite3_sourceid());
170277 return iErr;
170278}
170279SQLITE_PRIVATE int sqlite3CorruptError(int lineno){
170280 testcase( sqlite3GlobalConfig.xLog!=0 );
170281 return sqlite3ReportError(SQLITE_CORRUPT, lineno, zType: "database corruption");
170282}
170283SQLITE_PRIVATE int sqlite3MisuseError(int lineno){
170284 testcase( sqlite3GlobalConfig.xLog!=0 );
170285 return sqlite3ReportError(SQLITE_MISUSE, lineno, zType: "misuse");
170286}
170287SQLITE_PRIVATE int sqlite3CantopenError(int lineno){
170288 testcase( sqlite3GlobalConfig.xLog!=0 );
170289 return sqlite3ReportError(SQLITE_CANTOPEN, lineno, zType: "cannot open file");
170290}
170291#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_CORRUPT_PGNO)
170292SQLITE_PRIVATE int sqlite3CorruptPgnoError(int lineno, Pgno pgno){
170293 char zMsg[100];
170294 sqlite3_snprintf(sizeof(zMsg), zMsg, "database corruption page %d", pgno);
170295 testcase( sqlite3GlobalConfig.xLog!=0 );
170296 return sqlite3ReportError(SQLITE_CORRUPT, lineno, zMsg);
170297}
170298#endif
170299#ifdef SQLITE_DEBUG
170300SQLITE_PRIVATE int sqlite3NomemError(int lineno){
170301 testcase( sqlite3GlobalConfig.xLog!=0 );
170302 return sqlite3ReportError(SQLITE_NOMEM, lineno, "OOM");
170303}
170304SQLITE_PRIVATE int sqlite3IoerrnomemError(int lineno){
170305 testcase( sqlite3GlobalConfig.xLog!=0 );
170306 return sqlite3ReportError(SQLITE_IOERR_NOMEM, lineno, "I/O OOM error");
170307}
170308#endif
170309
170310#ifndef SQLITE_OMIT_DEPRECATED
170311/*
170312** This is a convenience routine that makes sure that all thread-specific
170313** data for this thread has been deallocated.
170314**
170315** SQLite no longer uses thread-specific data so this routine is now a
170316** no-op. It is retained for historical compatibility.
170317*/
170318SQLITE_API void sqlite3_thread_cleanup(void){
170319}
170320#endif
170321
170322/*
170323** Return meta information about a specific column of a database table.
170324** See comment in sqlite3.h (sqlite.h.in) for details.
170325*/
170326SQLITE_API int sqlite3_table_column_metadata(
170327 sqlite3 *db, /* Connection handle */
170328 const char *zDbName, /* Database name or NULL */
170329 const char *zTableName, /* Table name */
170330 const char *zColumnName, /* Column name */
170331 char const **pzDataType, /* OUTPUT: Declared data type */
170332 char const **pzCollSeq, /* OUTPUT: Collation sequence name */
170333 int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
170334 int *pPrimaryKey, /* OUTPUT: True if column part of PK */
170335 int *pAutoinc /* OUTPUT: True if column is auto-increment */
170336){
170337 int rc;
170338 char *zErrMsg = 0;
170339 Table *pTab = 0;
170340 Column *pCol = 0;
170341 int iCol = 0;
170342 char const *zDataType = 0;
170343 char const *zCollSeq = 0;
170344 int notnull = 0;
170345 int primarykey = 0;
170346 int autoinc = 0;
170347
170348
170349#ifdef SQLITE_ENABLE_API_ARMOR
170350 if( !sqlite3SafetyCheckOk(db) || zTableName==0 ){
170351 return SQLITE_MISUSE_BKPT;
170352 }
170353#endif
170354
170355 /* Ensure the database schema has been loaded */
170356 sqlite3_mutex_enter(p: db->mutex);
170357 sqlite3BtreeEnterAll(db);
170358 rc = sqlite3Init(db, pzErrMsg: &zErrMsg);
170359 if( SQLITE_OK!=rc ){
170360 goto error_out;
170361 }
170362
170363 /* Locate the table in question */
170364 pTab = sqlite3FindTable(db, zName: zTableName, zDatabase: zDbName);
170365 if( !pTab || IsView(pTab) ){
170366 pTab = 0;
170367 goto error_out;
170368 }
170369
170370 /* Find the column for which info is requested */
170371 if( zColumnName==0 ){
170372 /* Query for existance of table only */
170373 }else{
170374 for(iCol=0; iCol<pTab->nCol; iCol++){
170375 pCol = &pTab->aCol[iCol];
170376 if( 0==sqlite3StrICmp(zLeft: pCol->zCnName, zRight: zColumnName) ){
170377 break;
170378 }
170379 }
170380 if( iCol==pTab->nCol ){
170381 if( HasRowid(pTab) && sqlite3IsRowid(z: zColumnName) ){
170382 iCol = pTab->iPKey;
170383 pCol = iCol>=0 ? &pTab->aCol[iCol] : 0;
170384 }else{
170385 pTab = 0;
170386 goto error_out;
170387 }
170388 }
170389 }
170390
170391 /* The following block stores the meta information that will be returned
170392 ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
170393 ** and autoinc. At this point there are two possibilities:
170394 **
170395 ** 1. The specified column name was rowid", "oid" or "_rowid_"
170396 ** and there is no explicitly declared IPK column.
170397 **
170398 ** 2. The table is not a view and the column name identified an
170399 ** explicitly declared column. Copy meta information from *pCol.
170400 */
170401 if( pCol ){
170402 zDataType = sqlite3ColumnType(pCol,zDflt: 0);
170403 zCollSeq = sqlite3ColumnColl(pCol);
170404 notnull = pCol->notNull!=0;
170405 primarykey = (pCol->colFlags & COLFLAG_PRIMKEY)!=0;
170406 autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
170407 }else{
170408 zDataType = "INTEGER";
170409 primarykey = 1;
170410 }
170411 if( !zCollSeq ){
170412 zCollSeq = sqlite3StrBINARY;
170413 }
170414
170415error_out:
170416 sqlite3BtreeLeaveAll(db);
170417
170418 /* Whether the function call succeeded or failed, set the output parameters
170419 ** to whatever their local counterparts contain. If an error did occur,
170420 ** this has the effect of zeroing all output parameters.
170421 */
170422 if( pzDataType ) *pzDataType = zDataType;
170423 if( pzCollSeq ) *pzCollSeq = zCollSeq;
170424 if( pNotNull ) *pNotNull = notnull;
170425 if( pPrimaryKey ) *pPrimaryKey = primarykey;
170426 if( pAutoinc ) *pAutoinc = autoinc;
170427
170428 if( SQLITE_OK==rc && !pTab ){
170429 sqlite3DbFree(db, p: zErrMsg);
170430 zErrMsg = sqlite3MPrintf(db, zFormat: "no such table column: %s.%s", zTableName,
170431 zColumnName);
170432 rc = SQLITE_ERROR;
170433 }
170434 sqlite3ErrorWithMsg(db, err_code: rc, zFormat: (zErrMsg?"%s":0), zErrMsg);
170435 sqlite3DbFree(db, p: zErrMsg);
170436 rc = sqlite3ApiExit(db, rc);
170437 sqlite3_mutex_leave(p: db->mutex);
170438 return rc;
170439}
170440
170441/*
170442** Sleep for a little while. Return the amount of time slept.
170443*/
170444SQLITE_API int sqlite3_sleep(int ms){
170445 sqlite3_vfs *pVfs;
170446 int rc;
170447 pVfs = sqlite3_vfs_find(zVfs: 0);
170448 if( pVfs==0 ) return 0;
170449
170450 /* This function works in milliseconds, but the underlying OsSleep()
170451 ** API uses microseconds. Hence the 1000's.
170452 */
170453 rc = (sqlite3OsSleep(pVfs, nMicro: 1000*ms)/1000);
170454 return rc;
170455}
170456
170457/*
170458** Enable or disable the extended result codes.
170459*/
170460SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
170461#ifdef SQLITE_ENABLE_API_ARMOR
170462 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
170463#endif
170464 sqlite3_mutex_enter(p: db->mutex);
170465 db->errMask = onoff ? 0xffffffff : 0xff;
170466 sqlite3_mutex_leave(p: db->mutex);
170467 return SQLITE_OK;
170468}
170469
170470/*
170471** Invoke the xFileControl method on a particular database.
170472*/
170473SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
170474 int rc = SQLITE_ERROR;
170475 Btree *pBtree;
170476
170477#ifdef SQLITE_ENABLE_API_ARMOR
170478 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
170479#endif
170480 sqlite3_mutex_enter(p: db->mutex);
170481 pBtree = sqlite3DbNameToBtree(db, zDbName);
170482 if( pBtree ){
170483 Pager *pPager;
170484 sqlite3_file *fd;
170485 sqlite3BtreeEnter(p: pBtree);
170486 pPager = sqlite3BtreePager(p: pBtree);
170487 assert( pPager!=0 );
170488 fd = sqlite3PagerFile(pPager);
170489 assert( fd!=0 );
170490 if( op==SQLITE_FCNTL_FILE_POINTER ){
170491 *(sqlite3_file**)pArg = fd;
170492 rc = SQLITE_OK;
170493 }else if( op==SQLITE_FCNTL_VFS_POINTER ){
170494 *(sqlite3_vfs**)pArg = sqlite3PagerVfs(pPager);
170495 rc = SQLITE_OK;
170496 }else if( op==SQLITE_FCNTL_JOURNAL_POINTER ){
170497 *(sqlite3_file**)pArg = sqlite3PagerJrnlFile(pPager);
170498 rc = SQLITE_OK;
170499 }else if( op==SQLITE_FCNTL_DATA_VERSION ){
170500 *(unsigned int*)pArg = sqlite3PagerDataVersion(pPager);
170501 rc = SQLITE_OK;
170502 }else if( op==SQLITE_FCNTL_RESERVE_BYTES ){
170503 int iNew = *(int*)pArg;
170504 *(int*)pArg = sqlite3BtreeGetRequestedReserve(p: pBtree);
170505 if( iNew>=0 && iNew<=255 ){
170506 sqlite3BtreeSetPageSize(p: pBtree, pageSize: 0, nReserve: iNew, iFix: 0);
170507 }
170508 rc = SQLITE_OK;
170509 }else{
170510 int nSave = db->busyHandler.nBusy;
170511 rc = sqlite3OsFileControl(id: fd, op, pArg);
170512 db->busyHandler.nBusy = nSave;
170513 }
170514 sqlite3BtreeLeave(p: pBtree);
170515 }
170516 sqlite3_mutex_leave(p: db->mutex);
170517 return rc;
170518}
170519
170520/*
170521** Interface to the testing logic.
170522*/
170523SQLITE_API int sqlite3_test_control(int op, ...){
170524 int rc = 0;
170525#ifdef SQLITE_UNTESTABLE
170526 UNUSED_PARAMETER(op);
170527#else
170528 va_list ap;
170529 va_start(ap, op);
170530 switch( op ){
170531
170532 /*
170533 ** Save the current state of the PRNG.
170534 */
170535 case SQLITE_TESTCTRL_PRNG_SAVE: {
170536 sqlite3PrngSaveState();
170537 break;
170538 }
170539
170540 /*
170541 ** Restore the state of the PRNG to the last state saved using
170542 ** PRNG_SAVE. If PRNG_SAVE has never before been called, then
170543 ** this verb acts like PRNG_RESET.
170544 */
170545 case SQLITE_TESTCTRL_PRNG_RESTORE: {
170546 sqlite3PrngRestoreState();
170547 break;
170548 }
170549
170550 /* sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, int x, sqlite3 *db);
170551 **
170552 ** Control the seed for the pseudo-random number generator (PRNG) that
170553 ** is built into SQLite. Cases:
170554 **
170555 ** x!=0 && db!=0 Seed the PRNG to the current value of the
170556 ** schema cookie in the main database for db, or
170557 ** x if the schema cookie is zero. This case
170558 ** is convenient to use with database fuzzers
170559 ** as it allows the fuzzer some control over the
170560 ** the PRNG seed.
170561 **
170562 ** x!=0 && db==0 Seed the PRNG to the value of x.
170563 **
170564 ** x==0 && db==0 Revert to default behavior of using the
170565 ** xRandomness method on the primary VFS.
170566 **
170567 ** This test-control also resets the PRNG so that the new seed will
170568 ** be used for the next call to sqlite3_randomness().
170569 */
170570#ifndef SQLITE_OMIT_WSD
170571 case SQLITE_TESTCTRL_PRNG_SEED: {
170572 int x = va_arg(ap, int);
170573 int y;
170574 sqlite3 *db = va_arg(ap, sqlite3*);
170575 assert( db==0 || db->aDb[0].pSchema!=0 );
170576 if( db && (y = db->aDb[0].pSchema->schema_cookie)!=0 ){ x = y; }
170577 sqlite3Config.iPrngSeed = x;
170578 sqlite3_randomness(N: 0,pBuf: 0);
170579 break;
170580 }
170581#endif
170582
170583 /*
170584 ** sqlite3_test_control(BITVEC_TEST, size, program)
170585 **
170586 ** Run a test against a Bitvec object of size. The program argument
170587 ** is an array of integers that defines the test. Return -1 on a
170588 ** memory allocation error, 0 on success, or non-zero for an error.
170589 ** See the sqlite3BitvecBuiltinTest() for additional information.
170590 */
170591 case SQLITE_TESTCTRL_BITVEC_TEST: {
170592 int sz = va_arg(ap, int);
170593 int *aProg = va_arg(ap, int*);
170594 rc = sqlite3BitvecBuiltinTest(sz, aOp: aProg);
170595 break;
170596 }
170597
170598 /*
170599 ** sqlite3_test_control(FAULT_INSTALL, xCallback)
170600 **
170601 ** Arrange to invoke xCallback() whenever sqlite3FaultSim() is called,
170602 ** if xCallback is not NULL.
170603 **
170604 ** As a test of the fault simulator mechanism itself, sqlite3FaultSim(0)
170605 ** is called immediately after installing the new callback and the return
170606 ** value from sqlite3FaultSim(0) becomes the return from
170607 ** sqlite3_test_control().
170608 */
170609 case SQLITE_TESTCTRL_FAULT_INSTALL: {
170610 /* MSVC is picky about pulling func ptrs from va lists.
170611 ** http://support.microsoft.com/kb/47961
170612 ** sqlite3GlobalConfig.xTestCallback = va_arg(ap, int(*)(int));
170613 */
170614 typedef int(*TESTCALLBACKFUNC_t)(int);
170615 sqlite3GlobalConfig.xTestCallback = va_arg(ap, TESTCALLBACKFUNC_t);
170616 rc = sqlite3FaultSim(iTest: 0);
170617 break;
170618 }
170619
170620 /*
170621 ** sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
170622 **
170623 ** Register hooks to call to indicate which malloc() failures
170624 ** are benign.
170625 */
170626 case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: {
170627 typedef void (*void_function)(void);
170628 void_function xBenignBegin;
170629 void_function xBenignEnd;
170630 xBenignBegin = va_arg(ap, void_function);
170631 xBenignEnd = va_arg(ap, void_function);
170632 sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd);
170633 break;
170634 }
170635
170636 /*
170637 ** sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X)
170638 **
170639 ** Set the PENDING byte to the value in the argument, if X>0.
170640 ** Make no changes if X==0. Return the value of the pending byte
170641 ** as it existing before this routine was called.
170642 **
170643 ** IMPORTANT: Changing the PENDING byte from 0x40000000 results in
170644 ** an incompatible database file format. Changing the PENDING byte
170645 ** while any database connection is open results in undefined and
170646 ** deleterious behavior.
170647 */
170648 case SQLITE_TESTCTRL_PENDING_BYTE: {
170649 rc = PENDING_BYTE;
170650#ifndef SQLITE_OMIT_WSD
170651 {
170652 unsigned int newVal = va_arg(ap, unsigned int);
170653 if( newVal ) sqlite3PendingByte = newVal;
170654 }
170655#endif
170656 break;
170657 }
170658
170659 /*
170660 ** sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X)
170661 **
170662 ** This action provides a run-time test to see whether or not
170663 ** assert() was enabled at compile-time. If X is true and assert()
170664 ** is enabled, then the return value is true. If X is true and
170665 ** assert() is disabled, then the return value is zero. If X is
170666 ** false and assert() is enabled, then the assertion fires and the
170667 ** process aborts. If X is false and assert() is disabled, then the
170668 ** return value is zero.
170669 */
170670 case SQLITE_TESTCTRL_ASSERT: {
170671 volatile int x = 0;
170672 assert( /*side-effects-ok*/ (x = va_arg(ap,int))!=0 );
170673 rc = x;
170674 break;
170675 }
170676
170677
170678 /*
170679 ** sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X)
170680 **
170681 ** This action provides a run-time test to see how the ALWAYS and
170682 ** NEVER macros were defined at compile-time.
170683 **
170684 ** The return value is ALWAYS(X) if X is true, or 0 if X is false.
170685 **
170686 ** The recommended test is X==2. If the return value is 2, that means
170687 ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
170688 ** default setting. If the return value is 1, then ALWAYS() is either
170689 ** hard-coded to true or else it asserts if its argument is false.
170690 ** The first behavior (hard-coded to true) is the case if
170691 ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second
170692 ** behavior (assert if the argument to ALWAYS() is false) is the case if
170693 ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled.
170694 **
170695 ** The run-time test procedure might look something like this:
170696 **
170697 ** if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){
170698 ** // ALWAYS() and NEVER() are no-op pass-through macros
170699 ** }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){
170700 ** // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false.
170701 ** }else{
170702 ** // ALWAYS(x) is a constant 1. NEVER(x) is a constant 0.
170703 ** }
170704 */
170705 case SQLITE_TESTCTRL_ALWAYS: {
170706 int x = va_arg(ap,int);
170707 rc = x ? ALWAYS(x) : 0;
170708 break;
170709 }
170710
170711 /*
170712 ** sqlite3_test_control(SQLITE_TESTCTRL_BYTEORDER);
170713 **
170714 ** The integer returned reveals the byte-order of the computer on which
170715 ** SQLite is running:
170716 **
170717 ** 1 big-endian, determined at run-time
170718 ** 10 little-endian, determined at run-time
170719 ** 432101 big-endian, determined at compile-time
170720 ** 123410 little-endian, determined at compile-time
170721 */
170722 case SQLITE_TESTCTRL_BYTEORDER: {
170723 rc = SQLITE_BYTEORDER*100 + SQLITE_LITTLEENDIAN*10 + SQLITE_BIGENDIAN;
170724 break;
170725 }
170726
170727 /* sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N)
170728 **
170729 ** Enable or disable various optimizations for testing purposes. The
170730 ** argument N is a bitmask of optimizations to be disabled. For normal
170731 ** operation N should be 0. The idea is that a test program (like the
170732 ** SQL Logic Test or SLT test module) can run the same SQL multiple times
170733 ** with various optimizations disabled to verify that the same answer
170734 ** is obtained in every case.
170735 */
170736 case SQLITE_TESTCTRL_OPTIMIZATIONS: {
170737 sqlite3 *db = va_arg(ap, sqlite3*);
170738 db->dbOptFlags = va_arg(ap, u32);
170739 break;
170740 }
170741
170742 /* sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff);
170743 **
170744 ** If parameter onoff is non-zero, subsequent calls to localtime()
170745 ** and its variants fail. If onoff is zero, undo this setting.
170746 */
170747 case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
170748 sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int);
170749 break;
170750 }
170751
170752 /* sqlite3_test_control(SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, sqlite3*);
170753 **
170754 ** Toggle the ability to use internal functions on or off for
170755 ** the database connection given in the argument.
170756 */
170757 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS: {
170758 sqlite3 *db = va_arg(ap, sqlite3*);
170759 db->mDbFlags ^= DBFLAG_InternalFunc;
170760 break;
170761 }
170762
170763 /* sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int);
170764 **
170765 ** Set or clear a flag that indicates that the database file is always well-
170766 ** formed and never corrupt. This flag is clear by default, indicating that
170767 ** database files might have arbitrary corruption. Setting the flag during
170768 ** testing causes certain assert() statements in the code to be activated
170769 ** that demonstrat invariants on well-formed database files.
170770 */
170771 case SQLITE_TESTCTRL_NEVER_CORRUPT: {
170772 sqlite3GlobalConfig.neverCorrupt = va_arg(ap, int);
170773 break;
170774 }
170775
170776 /* sqlite3_test_control(SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS, int);
170777 **
170778 ** Set or clear a flag that causes SQLite to verify that type, name,
170779 ** and tbl_name fields of the sqlite_schema table. This is normally
170780 ** on, but it is sometimes useful to turn it off for testing.
170781 **
170782 ** 2020-07-22: Disabling EXTRA_SCHEMA_CHECKS also disables the
170783 ** verification of rootpage numbers when parsing the schema. This
170784 ** is useful to make it easier to reach strange internal error states
170785 ** during testing. The EXTRA_SCHEMA_CHECKS setting is always enabled
170786 ** in production.
170787 */
170788 case SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS: {
170789 sqlite3GlobalConfig.bExtraSchemaChecks = va_arg(ap, int);
170790 break;
170791 }
170792
170793 /* Set the threshold at which OP_Once counters reset back to zero.
170794 ** By default this is 0x7ffffffe (over 2 billion), but that value is
170795 ** too big to test in a reasonable amount of time, so this control is
170796 ** provided to set a small and easily reachable reset value.
170797 */
170798 case SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD: {
170799 sqlite3GlobalConfig.iOnceResetThreshold = va_arg(ap, int);
170800 break;
170801 }
170802
170803 /* sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE, xCallback, ptr);
170804 **
170805 ** Set the VDBE coverage callback function to xCallback with context
170806 ** pointer ptr.
170807 */
170808 case SQLITE_TESTCTRL_VDBE_COVERAGE: {
170809#ifdef SQLITE_VDBE_COVERAGE
170810 typedef void (*branch_callback)(void*,unsigned int,
170811 unsigned char,unsigned char);
170812 sqlite3GlobalConfig.xVdbeBranch = va_arg(ap,branch_callback);
170813 sqlite3GlobalConfig.pVdbeBranchArg = va_arg(ap,void*);
170814#endif
170815 break;
170816 }
170817
170818 /* sqlite3_test_control(SQLITE_TESTCTRL_SORTER_MMAP, db, nMax); */
170819 case SQLITE_TESTCTRL_SORTER_MMAP: {
170820 sqlite3 *db = va_arg(ap, sqlite3*);
170821 db->nMaxSorterMmap = va_arg(ap, int);
170822 break;
170823 }
170824
170825 /* sqlite3_test_control(SQLITE_TESTCTRL_ISINIT);
170826 **
170827 ** Return SQLITE_OK if SQLite has been initialized and SQLITE_ERROR if
170828 ** not.
170829 */
170830 case SQLITE_TESTCTRL_ISINIT: {
170831 if( sqlite3GlobalConfig.isInit==0 ) rc = SQLITE_ERROR;
170832 break;
170833 }
170834
170835 /* sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, dbName, onOff, tnum);
170836 **
170837 ** This test control is used to create imposter tables. "db" is a pointer
170838 ** to the database connection. dbName is the database name (ex: "main" or
170839 ** "temp") which will receive the imposter. "onOff" turns imposter mode on
170840 ** or off. "tnum" is the root page of the b-tree to which the imposter
170841 ** table should connect.
170842 **
170843 ** Enable imposter mode only when the schema has already been parsed. Then
170844 ** run a single CREATE TABLE statement to construct the imposter table in
170845 ** the parsed schema. Then turn imposter mode back off again.
170846 **
170847 ** If onOff==0 and tnum>0 then reset the schema for all databases, causing
170848 ** the schema to be reparsed the next time it is needed. This has the
170849 ** effect of erasing all imposter tables.
170850 */
170851 case SQLITE_TESTCTRL_IMPOSTER: {
170852 sqlite3 *db = va_arg(ap, sqlite3*);
170853 int iDb;
170854 sqlite3_mutex_enter(p: db->mutex);
170855 iDb = sqlite3FindDbName(db, va_arg(ap,const char*));
170856 if( iDb>=0 ){
170857 db->init.iDb = iDb;
170858 db->init.busy = db->init.imposterTable = va_arg(ap,int);
170859 db->init.newTnum = va_arg(ap,int);
170860 if( db->init.busy==0 && db->init.newTnum>0 ){
170861 sqlite3ResetAllSchemasOfConnection(db);
170862 }
170863 }
170864 sqlite3_mutex_leave(p: db->mutex);
170865 break;
170866 }
170867
170868#if defined(YYCOVERAGE)
170869 /* sqlite3_test_control(SQLITE_TESTCTRL_PARSER_COVERAGE, FILE *out)
170870 **
170871 ** This test control (only available when SQLite is compiled with
170872 ** -DYYCOVERAGE) writes a report onto "out" that shows all
170873 ** state/lookahead combinations in the parser state machine
170874 ** which are never exercised. If any state is missed, make the
170875 ** return code SQLITE_ERROR.
170876 */
170877 case SQLITE_TESTCTRL_PARSER_COVERAGE: {
170878 FILE *out = va_arg(ap, FILE*);
170879 if( sqlite3ParserCoverage(out) ) rc = SQLITE_ERROR;
170880 break;
170881 }
170882#endif /* defined(YYCOVERAGE) */
170883
170884 /* sqlite3_test_control(SQLITE_TESTCTRL_RESULT_INTREAL, sqlite3_context*);
170885 **
170886 ** This test-control causes the most recent sqlite3_result_int64() value
170887 ** to be interpreted as a MEM_IntReal instead of as an MEM_Int. Normally,
170888 ** MEM_IntReal values only arise during an INSERT operation of integer
170889 ** values into a REAL column, so they can be challenging to test. This
170890 ** test-control enables us to write an intreal() SQL function that can
170891 ** inject an intreal() value at arbitrary places in an SQL statement,
170892 ** for testing purposes.
170893 */
170894 case SQLITE_TESTCTRL_RESULT_INTREAL: {
170895 sqlite3_context *pCtx = va_arg(ap, sqlite3_context*);
170896 sqlite3ResultIntReal(pCtx);
170897 break;
170898 }
170899
170900 /* sqlite3_test_control(SQLITE_TESTCTRL_SEEK_COUNT,
170901 ** sqlite3 *db, // Database connection
170902 ** u64 *pnSeek // Write seek count here
170903 ** );
170904 **
170905 ** This test-control queries the seek-counter on the "main" database
170906 ** file. The seek-counter is written into *pnSeek and is then reset.
170907 ** The seek-count is only available if compiled with SQLITE_DEBUG.
170908 */
170909 case SQLITE_TESTCTRL_SEEK_COUNT: {
170910 sqlite3 *db = va_arg(ap, sqlite3*);
170911 u64 *pn = va_arg(ap, sqlite3_uint64*);
170912 *pn = sqlite3BtreeSeekCount(db->aDb->pBt);
170913 (void)db; /* Silence harmless unused variable warning */
170914 break;
170915 }
170916
170917 /* sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, op, ptr)
170918 **
170919 ** "ptr" is a pointer to a u32.
170920 **
170921 ** op==0 Store the current sqlite3SelectTrace in *ptr
170922 ** op==1 Set sqlite3SelectTrace to the value *ptr
170923 ** op==3 Store the current sqlite3WhereTrace in *ptr
170924 ** op==3 Set sqlite3WhereTrace to the value *ptr
170925 */
170926 case SQLITE_TESTCTRL_TRACEFLAGS: {
170927 int opTrace = va_arg(ap, int);
170928 u32 *ptr = va_arg(ap, u32*);
170929 switch( opTrace ){
170930 case 0: *ptr = sqlite3SelectTrace; break;
170931 case 1: sqlite3SelectTrace = *ptr; break;
170932 case 2: *ptr = sqlite3WhereTrace; break;
170933 case 3: sqlite3WhereTrace = *ptr; break;
170934 }
170935 break;
170936 }
170937
170938#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_WSD)
170939 /* sqlite3_test_control(SQLITE_TESTCTRL_TUNE, id, *piValue)
170940 **
170941 ** If "id" is an integer between 1 and SQLITE_NTUNE then set the value
170942 ** of the id-th tuning parameter to *piValue. If "id" is between -1
170943 ** and -SQLITE_NTUNE, then write the current value of the (-id)-th
170944 ** tuning parameter into *piValue.
170945 **
170946 ** Tuning parameters are for use during transient development builds,
170947 ** to help find the best values for constants in the query planner.
170948 ** Access tuning parameters using the Tuning(ID) macro. Set the
170949 ** parameters in the CLI using ".testctrl tune ID VALUE".
170950 **
170951 ** Transient use only. Tuning parameters should not be used in
170952 ** checked-in code.
170953 */
170954 case SQLITE_TESTCTRL_TUNE: {
170955 int id = va_arg(ap, int);
170956 int *piValue = va_arg(ap, int*);
170957 if( id>0 && id<=SQLITE_NTUNE ){
170958 Tuning(id) = *piValue;
170959 }else if( id<0 && id>=-SQLITE_NTUNE ){
170960 *piValue = Tuning(-id);
170961 }else{
170962 rc = SQLITE_NOTFOUND;
170963 }
170964 break;
170965 }
170966#endif
170967 }
170968 va_end(ap);
170969#endif /* SQLITE_UNTESTABLE */
170970 return rc;
170971}
170972
170973/*
170974** The Pager stores the Database filename, Journal filename, and WAL filename
170975** consecutively in memory, in that order. The database filename is prefixed
170976** by four zero bytes. Locate the start of the database filename by searching
170977** backwards for the first byte following four consecutive zero bytes.
170978**
170979** This only works if the filename passed in was obtained from the Pager.
170980*/
170981static const char *databaseName(const char *zName){
170982 while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
170983 zName--;
170984 }
170985 return zName;
170986}
170987
170988/*
170989** Append text z[] to the end of p[]. Return a pointer to the first
170990** character after then zero terminator on the new text in p[].
170991*/
170992static char *appendText(char *p, const char *z){
170993 size_t n = strlen(s: z);
170994 memcpy(dest: p, src: z, n: n+1);
170995 return p+n+1;
170996}
170997
170998/*
170999** Allocate memory to hold names for a database, journal file, WAL file,
171000** and query parameters. The pointer returned is valid for use by
171001** sqlite3_filename_database() and sqlite3_uri_parameter() and related
171002** functions.
171003**
171004** Memory layout must be compatible with that generated by the pager
171005** and expected by sqlite3_uri_parameter() and databaseName().
171006*/
171007SQLITE_API char *sqlite3_create_filename(
171008 const char *zDatabase,
171009 const char *zJournal,
171010 const char *zWal,
171011 int nParam,
171012 const char **azParam
171013){
171014 sqlite3_int64 nByte;
171015 int i;
171016 char *pResult, *p;
171017 nByte = strlen(s: zDatabase) + strlen(s: zJournal) + strlen(s: zWal) + 10;
171018 for(i=0; i<nParam*2; i++){
171019 nByte += strlen(s: azParam[i])+1;
171020 }
171021 pResult = p = sqlite3_malloc64( n: nByte );
171022 if( p==0 ) return 0;
171023 memset(s: p, c: 0, n: 4);
171024 p += 4;
171025 p = appendText(p, z: zDatabase);
171026 for(i=0; i<nParam*2; i++){
171027 p = appendText(p, z: azParam[i]);
171028 }
171029 *(p++) = 0;
171030 p = appendText(p, z: zJournal);
171031 p = appendText(p, z: zWal);
171032 *(p++) = 0;
171033 *(p++) = 0;
171034 assert( (sqlite3_int64)(p - pResult)==nByte );
171035 return pResult + 4;
171036}
171037
171038/*
171039** Free memory obtained from sqlite3_create_filename(). It is a severe
171040** error to call this routine with any parameter other than a pointer
171041** previously obtained from sqlite3_create_filename() or a NULL pointer.
171042*/
171043SQLITE_API void sqlite3_free_filename(char *p){
171044 if( p==0 ) return;
171045 p = (char*)databaseName(zName: p);
171046 sqlite3_free(p: p - 4);
171047}
171048
171049
171050/*
171051** This is a utility routine, useful to VFS implementations, that checks
171052** to see if a database file was a URI that contained a specific query
171053** parameter, and if so obtains the value of the query parameter.
171054**
171055** The zFilename argument is the filename pointer passed into the xOpen()
171056** method of a VFS implementation. The zParam argument is the name of the
171057** query parameter we seek. This routine returns the value of the zParam
171058** parameter if it exists. If the parameter does not exist, this routine
171059** returns a NULL pointer.
171060*/
171061SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){
171062 if( zFilename==0 || zParam==0 ) return 0;
171063 zFilename = databaseName(zName: zFilename);
171064 return uriParameter(zFilename, zParam);
171065}
171066
171067/*
171068** Return a pointer to the name of Nth query parameter of the filename.
171069*/
171070SQLITE_API const char *sqlite3_uri_key(const char *zFilename, int N){
171071 if( zFilename==0 || N<0 ) return 0;
171072 zFilename = databaseName(zName: zFilename);
171073 zFilename += sqlite3Strlen30(z: zFilename) + 1;
171074 while( ALWAYS(zFilename) && zFilename[0] && (N--)>0 ){
171075 zFilename += sqlite3Strlen30(z: zFilename) + 1;
171076 zFilename += sqlite3Strlen30(z: zFilename) + 1;
171077 }
171078 return zFilename[0] ? zFilename : 0;
171079}
171080
171081/*
171082** Return a boolean value for a query parameter.
171083*/
171084SQLITE_API int sqlite3_uri_boolean(const char *zFilename, const char *zParam, int bDflt){
171085 const char *z = sqlite3_uri_parameter(zFilename, zParam);
171086 bDflt = bDflt!=0;
171087 return z ? sqlite3GetBoolean(z, dflt: bDflt) : bDflt;
171088}
171089
171090/*
171091** Return a 64-bit integer value for a query parameter.
171092*/
171093SQLITE_API sqlite3_int64 sqlite3_uri_int64(
171094 const char *zFilename, /* Filename as passed to xOpen */
171095 const char *zParam, /* URI parameter sought */
171096 sqlite3_int64 bDflt /* return if parameter is missing */
171097){
171098 const char *z = sqlite3_uri_parameter(zFilename, zParam);
171099 sqlite3_int64 v;
171100 if( z && sqlite3DecOrHexToI64(z, pOut: &v)==0 ){
171101 bDflt = v;
171102 }
171103 return bDflt;
171104}
171105
171106/*
171107** Translate a filename that was handed to a VFS routine into the corresponding
171108** database, journal, or WAL file.
171109**
171110** It is an error to pass this routine a filename string that was not
171111** passed into the VFS from the SQLite core. Doing so is similar to
171112** passing free() a pointer that was not obtained from malloc() - it is
171113** an error that we cannot easily detect but that will likely cause memory
171114** corruption.
171115*/
171116SQLITE_API const char *sqlite3_filename_database(const char *zFilename){
171117 if( zFilename==0 ) return 0;
171118 return databaseName(zName: zFilename);
171119}
171120SQLITE_API const char *sqlite3_filename_journal(const char *zFilename){
171121 if( zFilename==0 ) return 0;
171122 zFilename = databaseName(zName: zFilename);
171123 zFilename += sqlite3Strlen30(z: zFilename) + 1;
171124 while( ALWAYS(zFilename) && zFilename[0] ){
171125 zFilename += sqlite3Strlen30(z: zFilename) + 1;
171126 zFilename += sqlite3Strlen30(z: zFilename) + 1;
171127 }
171128 return zFilename + 1;
171129}
171130SQLITE_API const char *sqlite3_filename_wal(const char *zFilename){
171131#ifdef SQLITE_OMIT_WAL
171132 return 0;
171133#else
171134 zFilename = sqlite3_filename_journal(zFilename);
171135 if( zFilename ) zFilename += sqlite3Strlen30(z: zFilename) + 1;
171136 return zFilename;
171137#endif
171138}
171139
171140/*
171141** Return the Btree pointer identified by zDbName. Return NULL if not found.
171142*/
171143SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3 *db, const char *zDbName){
171144 int iDb = zDbName ? sqlite3FindDbName(db, zName: zDbName) : 0;
171145 return iDb<0 ? 0 : db->aDb[iDb].pBt;
171146}
171147
171148/*
171149** Return the filename of the database associated with a database
171150** connection.
171151*/
171152SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){
171153 Btree *pBt;
171154#ifdef SQLITE_ENABLE_API_ARMOR
171155 if( !sqlite3SafetyCheckOk(db) ){
171156 (void)SQLITE_MISUSE_BKPT;
171157 return 0;
171158 }
171159#endif
171160 pBt = sqlite3DbNameToBtree(db, zDbName);
171161 return pBt ? sqlite3BtreeGetFilename(p: pBt) : 0;
171162}
171163
171164/*
171165** Return 1 if database is read-only or 0 if read/write. Return -1 if
171166** no such database exists.
171167*/
171168SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){
171169 Btree *pBt;
171170#ifdef SQLITE_ENABLE_API_ARMOR
171171 if( !sqlite3SafetyCheckOk(db) ){
171172 (void)SQLITE_MISUSE_BKPT;
171173 return -1;
171174 }
171175#endif
171176 pBt = sqlite3DbNameToBtree(db, zDbName);
171177 return pBt ? sqlite3BtreeIsReadonly(p: pBt) : -1;
171178}
171179
171180#ifdef SQLITE_ENABLE_SNAPSHOT
171181/*
171182** Obtain a snapshot handle for the snapshot of database zDb currently
171183** being read by handle db.
171184*/
171185SQLITE_API int sqlite3_snapshot_get(
171186 sqlite3 *db,
171187 const char *zDb,
171188 sqlite3_snapshot **ppSnapshot
171189){
171190 int rc = SQLITE_ERROR;
171191#ifndef SQLITE_OMIT_WAL
171192
171193#ifdef SQLITE_ENABLE_API_ARMOR
171194 if( !sqlite3SafetyCheckOk(db) ){
171195 return SQLITE_MISUSE_BKPT;
171196 }
171197#endif
171198 sqlite3_mutex_enter(db->mutex);
171199
171200 if( db->autoCommit==0 ){
171201 int iDb = sqlite3FindDbName(db, zDb);
171202 if( iDb==0 || iDb>1 ){
171203 Btree *pBt = db->aDb[iDb].pBt;
171204 if( SQLITE_TXN_WRITE!=sqlite3BtreeTxnState(pBt) ){
171205 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
171206 if( rc==SQLITE_OK ){
171207 rc = sqlite3PagerSnapshotGet(sqlite3BtreePager(pBt), ppSnapshot);
171208 }
171209 }
171210 }
171211 }
171212
171213 sqlite3_mutex_leave(db->mutex);
171214#endif /* SQLITE_OMIT_WAL */
171215 return rc;
171216}
171217
171218/*
171219** Open a read-transaction on the snapshot idendified by pSnapshot.
171220*/
171221SQLITE_API int sqlite3_snapshot_open(
171222 sqlite3 *db,
171223 const char *zDb,
171224 sqlite3_snapshot *pSnapshot
171225){
171226 int rc = SQLITE_ERROR;
171227#ifndef SQLITE_OMIT_WAL
171228
171229#ifdef SQLITE_ENABLE_API_ARMOR
171230 if( !sqlite3SafetyCheckOk(db) ){
171231 return SQLITE_MISUSE_BKPT;
171232 }
171233#endif
171234 sqlite3_mutex_enter(db->mutex);
171235 if( db->autoCommit==0 ){
171236 int iDb;
171237 iDb = sqlite3FindDbName(db, zDb);
171238 if( iDb==0 || iDb>1 ){
171239 Btree *pBt = db->aDb[iDb].pBt;
171240 if( sqlite3BtreeTxnState(pBt)!=SQLITE_TXN_WRITE ){
171241 Pager *pPager = sqlite3BtreePager(pBt);
171242 int bUnlock = 0;
171243 if( sqlite3BtreeTxnState(pBt)!=SQLITE_TXN_NONE ){
171244 if( db->nVdbeActive==0 ){
171245 rc = sqlite3PagerSnapshotCheck(pPager, pSnapshot);
171246 if( rc==SQLITE_OK ){
171247 bUnlock = 1;
171248 rc = sqlite3BtreeCommit(pBt);
171249 }
171250 }
171251 }else{
171252 rc = SQLITE_OK;
171253 }
171254 if( rc==SQLITE_OK ){
171255 rc = sqlite3PagerSnapshotOpen(pPager, pSnapshot);
171256 }
171257 if( rc==SQLITE_OK ){
171258 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
171259 sqlite3PagerSnapshotOpen(pPager, 0);
171260 }
171261 if( bUnlock ){
171262 sqlite3PagerSnapshotUnlock(pPager);
171263 }
171264 }
171265 }
171266 }
171267
171268 sqlite3_mutex_leave(db->mutex);
171269#endif /* SQLITE_OMIT_WAL */
171270 return rc;
171271}
171272
171273/*
171274** Recover as many snapshots as possible from the wal file associated with
171275** schema zDb of database db.
171276*/
171277SQLITE_API int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb){
171278 int rc = SQLITE_ERROR;
171279 int iDb;
171280#ifndef SQLITE_OMIT_WAL
171281
171282#ifdef SQLITE_ENABLE_API_ARMOR
171283 if( !sqlite3SafetyCheckOk(db) ){
171284 return SQLITE_MISUSE_BKPT;
171285 }
171286#endif
171287
171288 sqlite3_mutex_enter(db->mutex);
171289 iDb = sqlite3FindDbName(db, zDb);
171290 if( iDb==0 || iDb>1 ){
171291 Btree *pBt = db->aDb[iDb].pBt;
171292 if( SQLITE_TXN_NONE==sqlite3BtreeTxnState(pBt) ){
171293 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
171294 if( rc==SQLITE_OK ){
171295 rc = sqlite3PagerSnapshotRecover(sqlite3BtreePager(pBt));
171296 sqlite3BtreeCommit(pBt);
171297 }
171298 }
171299 }
171300 sqlite3_mutex_leave(db->mutex);
171301#endif /* SQLITE_OMIT_WAL */
171302 return rc;
171303}
171304
171305/*
171306** Free a snapshot handle obtained from sqlite3_snapshot_get().
171307*/
171308SQLITE_API void sqlite3_snapshot_free(sqlite3_snapshot *pSnapshot){
171309 sqlite3_free(pSnapshot);
171310}
171311#endif /* SQLITE_ENABLE_SNAPSHOT */
171312
171313#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
171314/*
171315** Given the name of a compile-time option, return true if that option
171316** was used and false if not.
171317**
171318** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix
171319** is not required for a match.
171320*/
171321SQLITE_API int sqlite3_compileoption_used(const char *zOptName){
171322 int i, n;
171323 int nOpt;
171324 const char **azCompileOpt;
171325
171326#if SQLITE_ENABLE_API_ARMOR
171327 if( zOptName==0 ){
171328 (void)SQLITE_MISUSE_BKPT;
171329 return 0;
171330 }
171331#endif
171332
171333 azCompileOpt = sqlite3CompileOptions(pnOpt: &nOpt);
171334
171335 if( sqlite3StrNICmp(zLeft: zOptName, zRight: "SQLITE_", N: 7)==0 ) zOptName += 7;
171336 n = sqlite3Strlen30(z: zOptName);
171337
171338 /* Since nOpt is normally in single digits, a linear search is
171339 ** adequate. No need for a binary search. */
171340 for(i=0; i<nOpt; i++){
171341 if( sqlite3StrNICmp(zLeft: zOptName, zRight: azCompileOpt[i], N: n)==0
171342 && sqlite3IsIdChar(c: (unsigned char)azCompileOpt[i][n])==0
171343 ){
171344 return 1;
171345 }
171346 }
171347 return 0;
171348}
171349
171350/*
171351** Return the N-th compile-time option string. If N is out of range,
171352** return a NULL pointer.
171353*/
171354SQLITE_API const char *sqlite3_compileoption_get(int N){
171355 int nOpt;
171356 const char **azCompileOpt;
171357 azCompileOpt = sqlite3CompileOptions(pnOpt: &nOpt);
171358 if( N>=0 && N<nOpt ){
171359 return azCompileOpt[N];
171360 }
171361 return 0;
171362}
171363#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
171364
171365/************** End of main.c ************************************************/
171366/************** Begin file notify.c ******************************************/
171367/*
171368** 2009 March 3
171369**
171370** The author disclaims copyright to this source code. In place of
171371** a legal notice, here is a blessing:
171372**
171373** May you do good and not evil.
171374** May you find forgiveness for yourself and forgive others.
171375** May you share freely, never taking more than you give.
171376**
171377*************************************************************************
171378**
171379** This file contains the implementation of the sqlite3_unlock_notify()
171380** API method and its associated functionality.
171381*/
171382/* #include "sqliteInt.h" */
171383/* #include "btreeInt.h" */
171384
171385/* Omit this entire file if SQLITE_ENABLE_UNLOCK_NOTIFY is not defined. */
171386#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
171387
171388/*
171389** Public interfaces:
171390**
171391** sqlite3ConnectionBlocked()
171392** sqlite3ConnectionUnlocked()
171393** sqlite3ConnectionClosed()
171394** sqlite3_unlock_notify()
171395*/
171396
171397#define assertMutexHeld() \
171398 assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN)) )
171399
171400/*
171401** Head of a linked list of all sqlite3 objects created by this process
171402** for which either sqlite3.pBlockingConnection or sqlite3.pUnlockConnection
171403** is not NULL. This variable may only accessed while the STATIC_MAIN
171404** mutex is held.
171405*/
171406static sqlite3 *SQLITE_WSD sqlite3BlockedList = 0;
171407
171408#ifndef NDEBUG
171409/*
171410** This function is a complex assert() that verifies the following
171411** properties of the blocked connections list:
171412**
171413** 1) Each entry in the list has a non-NULL value for either
171414** pUnlockConnection or pBlockingConnection, or both.
171415**
171416** 2) All entries in the list that share a common value for
171417** xUnlockNotify are grouped together.
171418**
171419** 3) If the argument db is not NULL, then none of the entries in the
171420** blocked connections list have pUnlockConnection or pBlockingConnection
171421** set to db. This is used when closing connection db.
171422*/
171423static void checkListProperties(sqlite3 *db){
171424 sqlite3 *p;
171425 for(p=sqlite3BlockedList; p; p=p->pNextBlocked){
171426 int seen = 0;
171427 sqlite3 *p2;
171428
171429 /* Verify property (1) */
171430 assert( p->pUnlockConnection || p->pBlockingConnection );
171431
171432 /* Verify property (2) */
171433 for(p2=sqlite3BlockedList; p2!=p; p2=p2->pNextBlocked){
171434 if( p2->xUnlockNotify==p->xUnlockNotify ) seen = 1;
171435 assert( p2->xUnlockNotify==p->xUnlockNotify || !seen );
171436 assert( db==0 || p->pUnlockConnection!=db );
171437 assert( db==0 || p->pBlockingConnection!=db );
171438 }
171439 }
171440}
171441#else
171442# define checkListProperties(x)
171443#endif
171444
171445/*
171446** Remove connection db from the blocked connections list. If connection
171447** db is not currently a part of the list, this function is a no-op.
171448*/
171449static void removeFromBlockedList(sqlite3 *db){
171450 sqlite3 **pp;
171451 assertMutexHeld();
171452 for(pp=&sqlite3BlockedList; *pp; pp = &(*pp)->pNextBlocked){
171453 if( *pp==db ){
171454 *pp = (*pp)->pNextBlocked;
171455 break;
171456 }
171457 }
171458}
171459
171460/*
171461** Add connection db to the blocked connections list. It is assumed
171462** that it is not already a part of the list.
171463*/
171464static void addToBlockedList(sqlite3 *db){
171465 sqlite3 **pp;
171466 assertMutexHeld();
171467 for(
171468 pp=&sqlite3BlockedList;
171469 *pp && (*pp)->xUnlockNotify!=db->xUnlockNotify;
171470 pp=&(*pp)->pNextBlocked
171471 );
171472 db->pNextBlocked = *pp;
171473 *pp = db;
171474}
171475
171476/*
171477** Obtain the STATIC_MAIN mutex.
171478*/
171479static void enterMutex(void){
171480 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN));
171481 checkListProperties(0);
171482}
171483
171484/*
171485** Release the STATIC_MAIN mutex.
171486*/
171487static void leaveMutex(void){
171488 assertMutexHeld();
171489 checkListProperties(0);
171490 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN));
171491}
171492
171493/*
171494** Register an unlock-notify callback.
171495**
171496** This is called after connection "db" has attempted some operation
171497** but has received an SQLITE_LOCKED error because another connection
171498** (call it pOther) in the same process was busy using the same shared
171499** cache. pOther is found by looking at db->pBlockingConnection.
171500**
171501** If there is no blocking connection, the callback is invoked immediately,
171502** before this routine returns.
171503**
171504** If pOther is already blocked on db, then report SQLITE_LOCKED, to indicate
171505** a deadlock.
171506**
171507** Otherwise, make arrangements to invoke xNotify when pOther drops
171508** its locks.
171509**
171510** Each call to this routine overrides any prior callbacks registered
171511** on the same "db". If xNotify==0 then any prior callbacks are immediately
171512** cancelled.
171513*/
171514SQLITE_API int sqlite3_unlock_notify(
171515 sqlite3 *db,
171516 void (*xNotify)(void **, int),
171517 void *pArg
171518){
171519 int rc = SQLITE_OK;
171520
171521 sqlite3_mutex_enter(db->mutex);
171522 enterMutex();
171523
171524 if( xNotify==0 ){
171525 removeFromBlockedList(db);
171526 db->pBlockingConnection = 0;
171527 db->pUnlockConnection = 0;
171528 db->xUnlockNotify = 0;
171529 db->pUnlockArg = 0;
171530 }else if( 0==db->pBlockingConnection ){
171531 /* The blocking transaction has been concluded. Or there never was a
171532 ** blocking transaction. In either case, invoke the notify callback
171533 ** immediately.
171534 */
171535 xNotify(&pArg, 1);
171536 }else{
171537 sqlite3 *p;
171538
171539 for(p=db->pBlockingConnection; p && p!=db; p=p->pUnlockConnection){}
171540 if( p ){
171541 rc = SQLITE_LOCKED; /* Deadlock detected. */
171542 }else{
171543 db->pUnlockConnection = db->pBlockingConnection;
171544 db->xUnlockNotify = xNotify;
171545 db->pUnlockArg = pArg;
171546 removeFromBlockedList(db);
171547 addToBlockedList(db);
171548 }
171549 }
171550
171551 leaveMutex();
171552 assert( !db->mallocFailed );
171553 sqlite3ErrorWithMsg(db, rc, (rc?"database is deadlocked":0));
171554 sqlite3_mutex_leave(db->mutex);
171555 return rc;
171556}
171557
171558/*
171559** This function is called while stepping or preparing a statement
171560** associated with connection db. The operation will return SQLITE_LOCKED
171561** to the user because it requires a lock that will not be available
171562** until connection pBlocker concludes its current transaction.
171563*/
171564SQLITE_PRIVATE void sqlite3ConnectionBlocked(sqlite3 *db, sqlite3 *pBlocker){
171565 enterMutex();
171566 if( db->pBlockingConnection==0 && db->pUnlockConnection==0 ){
171567 addToBlockedList(db);
171568 }
171569 db->pBlockingConnection = pBlocker;
171570 leaveMutex();
171571}
171572
171573/*
171574** This function is called when
171575** the transaction opened by database db has just finished. Locks held
171576** by database connection db have been released.
171577**
171578** This function loops through each entry in the blocked connections
171579** list and does the following:
171580**
171581** 1) If the sqlite3.pBlockingConnection member of a list entry is
171582** set to db, then set pBlockingConnection=0.
171583**
171584** 2) If the sqlite3.pUnlockConnection member of a list entry is
171585** set to db, then invoke the configured unlock-notify callback and
171586** set pUnlockConnection=0.
171587**
171588** 3) If the two steps above mean that pBlockingConnection==0 and
171589** pUnlockConnection==0, remove the entry from the blocked connections
171590** list.
171591*/
171592SQLITE_PRIVATE void sqlite3ConnectionUnlocked(sqlite3 *db){
171593 void (*xUnlockNotify)(void **, int) = 0; /* Unlock-notify cb to invoke */
171594 int nArg = 0; /* Number of entries in aArg[] */
171595 sqlite3 **pp; /* Iterator variable */
171596 void **aArg; /* Arguments to the unlock callback */
171597 void **aDyn = 0; /* Dynamically allocated space for aArg[] */
171598 void *aStatic[16]; /* Starter space for aArg[]. No malloc required */
171599
171600 aArg = aStatic;
171601 enterMutex(); /* Enter STATIC_MAIN mutex */
171602
171603 /* This loop runs once for each entry in the blocked-connections list. */
171604 for(pp=&sqlite3BlockedList; *pp; /* no-op */ ){
171605 sqlite3 *p = *pp;
171606
171607 /* Step 1. */
171608 if( p->pBlockingConnection==db ){
171609 p->pBlockingConnection = 0;
171610 }
171611
171612 /* Step 2. */
171613 if( p->pUnlockConnection==db ){
171614 assert( p->xUnlockNotify );
171615 if( p->xUnlockNotify!=xUnlockNotify && nArg!=0 ){
171616 xUnlockNotify(aArg, nArg);
171617 nArg = 0;
171618 }
171619
171620 sqlite3BeginBenignMalloc();
171621 assert( aArg==aDyn || (aDyn==0 && aArg==aStatic) );
171622 assert( nArg<=(int)ArraySize(aStatic) || aArg==aDyn );
171623 if( (!aDyn && nArg==(int)ArraySize(aStatic))
171624 || (aDyn && nArg==(int)(sqlite3MallocSize(aDyn)/sizeof(void*)))
171625 ){
171626 /* The aArg[] array needs to grow. */
171627 void **pNew = (void **)sqlite3Malloc(nArg*sizeof(void *)*2);
171628 if( pNew ){
171629 memcpy(pNew, aArg, nArg*sizeof(void *));
171630 sqlite3_free(aDyn);
171631 aDyn = aArg = pNew;
171632 }else{
171633 /* This occurs when the array of context pointers that need to
171634 ** be passed to the unlock-notify callback is larger than the
171635 ** aStatic[] array allocated on the stack and the attempt to
171636 ** allocate a larger array from the heap has failed.
171637 **
171638 ** This is a difficult situation to handle. Returning an error
171639 ** code to the caller is insufficient, as even if an error code
171640 ** is returned the transaction on connection db will still be
171641 ** closed and the unlock-notify callbacks on blocked connections
171642 ** will go unissued. This might cause the application to wait
171643 ** indefinitely for an unlock-notify callback that will never
171644 ** arrive.
171645 **
171646 ** Instead, invoke the unlock-notify callback with the context
171647 ** array already accumulated. We can then clear the array and
171648 ** begin accumulating any further context pointers without
171649 ** requiring any dynamic allocation. This is sub-optimal because
171650 ** it means that instead of one callback with a large array of
171651 ** context pointers the application will receive two or more
171652 ** callbacks with smaller arrays of context pointers, which will
171653 ** reduce the applications ability to prioritize multiple
171654 ** connections. But it is the best that can be done under the
171655 ** circumstances.
171656 */
171657 xUnlockNotify(aArg, nArg);
171658 nArg = 0;
171659 }
171660 }
171661 sqlite3EndBenignMalloc();
171662
171663 aArg[nArg++] = p->pUnlockArg;
171664 xUnlockNotify = p->xUnlockNotify;
171665 p->pUnlockConnection = 0;
171666 p->xUnlockNotify = 0;
171667 p->pUnlockArg = 0;
171668 }
171669
171670 /* Step 3. */
171671 if( p->pBlockingConnection==0 && p->pUnlockConnection==0 ){
171672 /* Remove connection p from the blocked connections list. */
171673 *pp = p->pNextBlocked;
171674 p->pNextBlocked = 0;
171675 }else{
171676 pp = &p->pNextBlocked;
171677 }
171678 }
171679
171680 if( nArg!=0 ){
171681 xUnlockNotify(aArg, nArg);
171682 }
171683 sqlite3_free(aDyn);
171684 leaveMutex(); /* Leave STATIC_MAIN mutex */
171685}
171686
171687/*
171688** This is called when the database connection passed as an argument is
171689** being closed. The connection is removed from the blocked list.
171690*/
171691SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db){
171692 sqlite3ConnectionUnlocked(db);
171693 enterMutex();
171694 removeFromBlockedList(db);
171695 checkListProperties(db);
171696 leaveMutex();
171697}
171698#endif
171699
171700/************** End of notify.c **********************************************/
171701/************** Begin file fts3.c ********************************************/
171702/*
171703** 2006 Oct 10
171704**
171705** The author disclaims copyright to this source code. In place of
171706** a legal notice, here is a blessing:
171707**
171708** May you do good and not evil.
171709** May you find forgiveness for yourself and forgive others.
171710** May you share freely, never taking more than you give.
171711**
171712******************************************************************************
171713**
171714** This is an SQLite module implementing full-text search.
171715*/
171716
171717/*
171718** The code in this file is only compiled if:
171719**
171720** * The FTS3 module is being built as an extension
171721** (in which case SQLITE_CORE is not defined), or
171722**
171723** * The FTS3 module is being built into the core of
171724** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
171725*/
171726
171727/* The full-text index is stored in a series of b+tree (-like)
171728** structures called segments which map terms to doclists. The
171729** structures are like b+trees in layout, but are constructed from the
171730** bottom up in optimal fashion and are not updatable. Since trees
171731** are built from the bottom up, things will be described from the
171732** bottom up.
171733**
171734**
171735**** Varints ****
171736** The basic unit of encoding is a variable-length integer called a
171737** varint. We encode variable-length integers in little-endian order
171738** using seven bits * per byte as follows:
171739**
171740** KEY:
171741** A = 0xxxxxxx 7 bits of data and one flag bit
171742** B = 1xxxxxxx 7 bits of data and one flag bit
171743**
171744** 7 bits - A
171745** 14 bits - BA
171746** 21 bits - BBA
171747** and so on.
171748**
171749** This is similar in concept to how sqlite encodes "varints" but
171750** the encoding is not the same. SQLite varints are big-endian
171751** are are limited to 9 bytes in length whereas FTS3 varints are
171752** little-endian and can be up to 10 bytes in length (in theory).
171753**
171754** Example encodings:
171755**
171756** 1: 0x01
171757** 127: 0x7f
171758** 128: 0x81 0x00
171759**
171760**
171761**** Document lists ****
171762** A doclist (document list) holds a docid-sorted list of hits for a
171763** given term. Doclists hold docids and associated token positions.
171764** A docid is the unique integer identifier for a single document.
171765** A position is the index of a word within the document. The first
171766** word of the document has a position of 0.
171767**
171768** FTS3 used to optionally store character offsets using a compile-time
171769** option. But that functionality is no longer supported.
171770**
171771** A doclist is stored like this:
171772**
171773** array {
171774** varint docid; (delta from previous doclist)
171775** array { (position list for column 0)
171776** varint position; (2 more than the delta from previous position)
171777** }
171778** array {
171779** varint POS_COLUMN; (marks start of position list for new column)
171780** varint column; (index of new column)
171781** array {
171782** varint position; (2 more than the delta from previous position)
171783** }
171784** }
171785** varint POS_END; (marks end of positions for this document.
171786** }
171787**
171788** Here, array { X } means zero or more occurrences of X, adjacent in
171789** memory. A "position" is an index of a token in the token stream
171790** generated by the tokenizer. Note that POS_END and POS_COLUMN occur
171791** in the same logical place as the position element, and act as sentinals
171792** ending a position list array. POS_END is 0. POS_COLUMN is 1.
171793** The positions numbers are not stored literally but rather as two more
171794** than the difference from the prior position, or the just the position plus
171795** 2 for the first position. Example:
171796**
171797** label: A B C D E F G H I J K
171798** value: 123 5 9 1 1 14 35 0 234 72 0
171799**
171800** The 123 value is the first docid. For column zero in this document
171801** there are two matches at positions 3 and 10 (5-2 and 9-2+3). The 1
171802** at D signals the start of a new column; the 1 at E indicates that the
171803** new column is column number 1. There are two positions at 12 and 45
171804** (14-2 and 35-2+12). The 0 at H indicate the end-of-document. The
171805** 234 at I is the delta to next docid (357). It has one position 70
171806** (72-2) and then terminates with the 0 at K.
171807**
171808** A "position-list" is the list of positions for multiple columns for
171809** a single docid. A "column-list" is the set of positions for a single
171810** column. Hence, a position-list consists of one or more column-lists,
171811** a document record consists of a docid followed by a position-list and
171812** a doclist consists of one or more document records.
171813**
171814** A bare doclist omits the position information, becoming an
171815** array of varint-encoded docids.
171816**
171817**** Segment leaf nodes ****
171818** Segment leaf nodes store terms and doclists, ordered by term. Leaf
171819** nodes are written using LeafWriter, and read using LeafReader (to
171820** iterate through a single leaf node's data) and LeavesReader (to
171821** iterate through a segment's entire leaf layer). Leaf nodes have
171822** the format:
171823**
171824** varint iHeight; (height from leaf level, always 0)
171825** varint nTerm; (length of first term)
171826** char pTerm[nTerm]; (content of first term)
171827** varint nDoclist; (length of term's associated doclist)
171828** char pDoclist[nDoclist]; (content of doclist)
171829** array {
171830** (further terms are delta-encoded)
171831** varint nPrefix; (length of prefix shared with previous term)
171832** varint nSuffix; (length of unshared suffix)
171833** char pTermSuffix[nSuffix];(unshared suffix of next term)
171834** varint nDoclist; (length of term's associated doclist)
171835** char pDoclist[nDoclist]; (content of doclist)
171836** }
171837**
171838** Here, array { X } means zero or more occurrences of X, adjacent in
171839** memory.
171840**
171841** Leaf nodes are broken into blocks which are stored contiguously in
171842** the %_segments table in sorted order. This means that when the end
171843** of a node is reached, the next term is in the node with the next
171844** greater node id.
171845**
171846** New data is spilled to a new leaf node when the current node
171847** exceeds LEAF_MAX bytes (default 2048). New data which itself is
171848** larger than STANDALONE_MIN (default 1024) is placed in a standalone
171849** node (a leaf node with a single term and doclist). The goal of
171850** these settings is to pack together groups of small doclists while
171851** making it efficient to directly access large doclists. The
171852** assumption is that large doclists represent terms which are more
171853** likely to be query targets.
171854**
171855** TODO(shess) It may be useful for blocking decisions to be more
171856** dynamic. For instance, it may make more sense to have a 2.5k leaf
171857** node rather than splitting into 2k and .5k nodes. My intuition is
171858** that this might extend through 2x or 4x the pagesize.
171859**
171860**
171861**** Segment interior nodes ****
171862** Segment interior nodes store blockids for subtree nodes and terms
171863** to describe what data is stored by the each subtree. Interior
171864** nodes are written using InteriorWriter, and read using
171865** InteriorReader. InteriorWriters are created as needed when
171866** SegmentWriter creates new leaf nodes, or when an interior node
171867** itself grows too big and must be split. The format of interior
171868** nodes:
171869**
171870** varint iHeight; (height from leaf level, always >0)
171871** varint iBlockid; (block id of node's leftmost subtree)
171872** optional {
171873** varint nTerm; (length of first term)
171874** char pTerm[nTerm]; (content of first term)
171875** array {
171876** (further terms are delta-encoded)
171877** varint nPrefix; (length of shared prefix with previous term)
171878** varint nSuffix; (length of unshared suffix)
171879** char pTermSuffix[nSuffix]; (unshared suffix of next term)
171880** }
171881** }
171882**
171883** Here, optional { X } means an optional element, while array { X }
171884** means zero or more occurrences of X, adjacent in memory.
171885**
171886** An interior node encodes n terms separating n+1 subtrees. The
171887** subtree blocks are contiguous, so only the first subtree's blockid
171888** is encoded. The subtree at iBlockid will contain all terms less
171889** than the first term encoded (or all terms if no term is encoded).
171890** Otherwise, for terms greater than or equal to pTerm[i] but less
171891** than pTerm[i+1], the subtree for that term will be rooted at
171892** iBlockid+i. Interior nodes only store enough term data to
171893** distinguish adjacent children (if the rightmost term of the left
171894** child is "something", and the leftmost term of the right child is
171895** "wicked", only "w" is stored).
171896**
171897** New data is spilled to a new interior node at the same height when
171898** the current node exceeds INTERIOR_MAX bytes (default 2048).
171899** INTERIOR_MIN_TERMS (default 7) keeps large terms from monopolizing
171900** interior nodes and making the tree too skinny. The interior nodes
171901** at a given height are naturally tracked by interior nodes at
171902** height+1, and so on.
171903**
171904**
171905**** Segment directory ****
171906** The segment directory in table %_segdir stores meta-information for
171907** merging and deleting segments, and also the root node of the
171908** segment's tree.
171909**
171910** The root node is the top node of the segment's tree after encoding
171911** the entire segment, restricted to ROOT_MAX bytes (default 1024).
171912** This could be either a leaf node or an interior node. If the top
171913** node requires more than ROOT_MAX bytes, it is flushed to %_segments
171914** and a new root interior node is generated (which should always fit
171915** within ROOT_MAX because it only needs space for 2 varints, the
171916** height and the blockid of the previous root).
171917**
171918** The meta-information in the segment directory is:
171919** level - segment level (see below)
171920** idx - index within level
171921** - (level,idx uniquely identify a segment)
171922** start_block - first leaf node
171923** leaves_end_block - last leaf node
171924** end_block - last block (including interior nodes)
171925** root - contents of root node
171926**
171927** If the root node is a leaf node, then start_block,
171928** leaves_end_block, and end_block are all 0.
171929**
171930**
171931**** Segment merging ****
171932** To amortize update costs, segments are grouped into levels and
171933** merged in batches. Each increase in level represents exponentially
171934** more documents.
171935**
171936** New documents (actually, document updates) are tokenized and
171937** written individually (using LeafWriter) to a level 0 segment, with
171938** incrementing idx. When idx reaches MERGE_COUNT (default 16), all
171939** level 0 segments are merged into a single level 1 segment. Level 1
171940** is populated like level 0, and eventually MERGE_COUNT level 1
171941** segments are merged to a single level 2 segment (representing
171942** MERGE_COUNT^2 updates), and so on.
171943**
171944** A segment merge traverses all segments at a given level in
171945** parallel, performing a straightforward sorted merge. Since segment
171946** leaf nodes are written in to the %_segments table in order, this
171947** merge traverses the underlying sqlite disk structures efficiently.
171948** After the merge, all segment blocks from the merged level are
171949** deleted.
171950**
171951** MERGE_COUNT controls how often we merge segments. 16 seems to be
171952** somewhat of a sweet spot for insertion performance. 32 and 64 show
171953** very similar performance numbers to 16 on insertion, though they're
171954** a tiny bit slower (perhaps due to more overhead in merge-time
171955** sorting). 8 is about 20% slower than 16, 4 about 50% slower than
171956** 16, 2 about 66% slower than 16.
171957**
171958** At query time, high MERGE_COUNT increases the number of segments
171959** which need to be scanned and merged. For instance, with 100k docs
171960** inserted:
171961**
171962** MERGE_COUNT segments
171963** 16 25
171964** 8 12
171965** 4 10
171966** 2 6
171967**
171968** This appears to have only a moderate impact on queries for very
171969** frequent terms (which are somewhat dominated by segment merge
171970** costs), and infrequent and non-existent terms still seem to be fast
171971** even with many segments.
171972**
171973** TODO(shess) That said, it would be nice to have a better query-side
171974** argument for MERGE_COUNT of 16. Also, it is possible/likely that
171975** optimizations to things like doclist merging will swing the sweet
171976** spot around.
171977**
171978**
171979**
171980**** Handling of deletions and updates ****
171981** Since we're using a segmented structure, with no docid-oriented
171982** index into the term index, we clearly cannot simply update the term
171983** index when a document is deleted or updated. For deletions, we
171984** write an empty doclist (varint(docid) varint(POS_END)), for updates
171985** we simply write the new doclist. Segment merges overwrite older
171986** data for a particular docid with newer data, so deletes or updates
171987** will eventually overtake the earlier data and knock it out. The
171988** query logic likewise merges doclists so that newer data knocks out
171989** older data.
171990*/
171991
171992/************** Include fts3Int.h in the middle of fts3.c ********************/
171993/************** Begin file fts3Int.h *****************************************/
171994/*
171995** 2009 Nov 12
171996**
171997** The author disclaims copyright to this source code. In place of
171998** a legal notice, here is a blessing:
171999**
172000** May you do good and not evil.
172001** May you find forgiveness for yourself and forgive others.
172002** May you share freely, never taking more than you give.
172003**
172004******************************************************************************
172005**
172006*/
172007#ifndef _FTSINT_H
172008#define _FTSINT_H
172009
172010#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
172011# define NDEBUG 1
172012#endif
172013
172014/* FTS3/FTS4 require virtual tables */
172015#ifdef SQLITE_OMIT_VIRTUALTABLE
172016# undef SQLITE_ENABLE_FTS3
172017# undef SQLITE_ENABLE_FTS4
172018#endif
172019
172020/*
172021** FTS4 is really an extension for FTS3. It is enabled using the
172022** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also all
172023** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
172024*/
172025#if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
172026# define SQLITE_ENABLE_FTS3
172027#endif
172028
172029#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
172030
172031/* If not building as part of the core, include sqlite3ext.h. */
172032#ifndef SQLITE_CORE
172033/* # include "sqlite3ext.h" */
172034SQLITE_EXTENSION_INIT3
172035#endif
172036
172037/* #include "sqlite3.h" */
172038/************** Include fts3_tokenizer.h in the middle of fts3Int.h **********/
172039/************** Begin file fts3_tokenizer.h **********************************/
172040/*
172041** 2006 July 10
172042**
172043** The author disclaims copyright to this source code.
172044**
172045*************************************************************************
172046** Defines the interface to tokenizers used by fulltext-search. There
172047** are three basic components:
172048**
172049** sqlite3_tokenizer_module is a singleton defining the tokenizer
172050** interface functions. This is essentially the class structure for
172051** tokenizers.
172052**
172053** sqlite3_tokenizer is used to define a particular tokenizer, perhaps
172054** including customization information defined at creation time.
172055**
172056** sqlite3_tokenizer_cursor is generated by a tokenizer to generate
172057** tokens from a particular input.
172058*/
172059#ifndef _FTS3_TOKENIZER_H_
172060#define _FTS3_TOKENIZER_H_
172061
172062/* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time.
172063** If tokenizers are to be allowed to call sqlite3_*() functions, then
172064** we will need a way to register the API consistently.
172065*/
172066/* #include "sqlite3.h" */
172067
172068/*
172069** Structures used by the tokenizer interface. When a new tokenizer
172070** implementation is registered, the caller provides a pointer to
172071** an sqlite3_tokenizer_module containing pointers to the callback
172072** functions that make up an implementation.
172073**
172074** When an fts3 table is created, it passes any arguments passed to
172075** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the
172076** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer
172077** implementation. The xCreate() function in turn returns an
172078** sqlite3_tokenizer structure representing the specific tokenizer to
172079** be used for the fts3 table (customized by the tokenizer clause arguments).
172080**
172081** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen()
172082** method is called. It returns an sqlite3_tokenizer_cursor object
172083** that may be used to tokenize a specific input buffer based on
172084** the tokenization rules supplied by a specific sqlite3_tokenizer
172085** object.
172086*/
172087typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module;
172088typedef struct sqlite3_tokenizer sqlite3_tokenizer;
172089typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor;
172090
172091struct sqlite3_tokenizer_module {
172092
172093 /*
172094 ** Structure version. Should always be set to 0 or 1.
172095 */
172096 int iVersion;
172097
172098 /*
172099 ** Create a new tokenizer. The values in the argv[] array are the
172100 ** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL
172101 ** TABLE statement that created the fts3 table. For example, if
172102 ** the following SQL is executed:
172103 **
172104 ** CREATE .. USING fts3( ... , tokenizer <tokenizer-name> arg1 arg2)
172105 **
172106 ** then argc is set to 2, and the argv[] array contains pointers
172107 ** to the strings "arg1" and "arg2".
172108 **
172109 ** This method should return either SQLITE_OK (0), or an SQLite error
172110 ** code. If SQLITE_OK is returned, then *ppTokenizer should be set
172111 ** to point at the newly created tokenizer structure. The generic
172112 ** sqlite3_tokenizer.pModule variable should not be initialized by
172113 ** this callback. The caller will do so.
172114 */
172115 int (*xCreate)(
172116 int argc, /* Size of argv array */
172117 const char *const*argv, /* Tokenizer argument strings */
172118 sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */
172119 );
172120
172121 /*
172122 ** Destroy an existing tokenizer. The fts3 module calls this method
172123 ** exactly once for each successful call to xCreate().
172124 */
172125 int (*xDestroy)(sqlite3_tokenizer *pTokenizer);
172126
172127 /*
172128 ** Create a tokenizer cursor to tokenize an input buffer. The caller
172129 ** is responsible for ensuring that the input buffer remains valid
172130 ** until the cursor is closed (using the xClose() method).
172131 */
172132 int (*xOpen)(
172133 sqlite3_tokenizer *pTokenizer, /* Tokenizer object */
172134 const char *pInput, int nBytes, /* Input buffer */
172135 sqlite3_tokenizer_cursor **ppCursor /* OUT: Created tokenizer cursor */
172136 );
172137
172138 /*
172139 ** Destroy an existing tokenizer cursor. The fts3 module calls this
172140 ** method exactly once for each successful call to xOpen().
172141 */
172142 int (*xClose)(sqlite3_tokenizer_cursor *pCursor);
172143
172144 /*
172145 ** Retrieve the next token from the tokenizer cursor pCursor. This
172146 ** method should either return SQLITE_OK and set the values of the
172147 ** "OUT" variables identified below, or SQLITE_DONE to indicate that
172148 ** the end of the buffer has been reached, or an SQLite error code.
172149 **
172150 ** *ppToken should be set to point at a buffer containing the
172151 ** normalized version of the token (i.e. after any case-folding and/or
172152 ** stemming has been performed). *pnBytes should be set to the length
172153 ** of this buffer in bytes. The input text that generated the token is
172154 ** identified by the byte offsets returned in *piStartOffset and
172155 ** *piEndOffset. *piStartOffset should be set to the index of the first
172156 ** byte of the token in the input buffer. *piEndOffset should be set
172157 ** to the index of the first byte just past the end of the token in
172158 ** the input buffer.
172159 **
172160 ** The buffer *ppToken is set to point at is managed by the tokenizer
172161 ** implementation. It is only required to be valid until the next call
172162 ** to xNext() or xClose().
172163 */
172164 /* TODO(shess) current implementation requires pInput to be
172165 ** nul-terminated. This should either be fixed, or pInput/nBytes
172166 ** should be converted to zInput.
172167 */
172168 int (*xNext)(
172169 sqlite3_tokenizer_cursor *pCursor, /* Tokenizer cursor */
172170 const char **ppToken, int *pnBytes, /* OUT: Normalized text for token */
172171 int *piStartOffset, /* OUT: Byte offset of token in input buffer */
172172 int *piEndOffset, /* OUT: Byte offset of end of token in input buffer */
172173 int *piPosition /* OUT: Number of tokens returned before this one */
172174 );
172175
172176 /***********************************************************************
172177 ** Methods below this point are only available if iVersion>=1.
172178 */
172179
172180 /*
172181 ** Configure the language id of a tokenizer cursor.
172182 */
172183 int (*xLanguageid)(sqlite3_tokenizer_cursor *pCsr, int iLangid);
172184};
172185
172186struct sqlite3_tokenizer {
172187 const sqlite3_tokenizer_module *pModule; /* The module for this tokenizer */
172188 /* Tokenizer implementations will typically add additional fields */
172189};
172190
172191struct sqlite3_tokenizer_cursor {
172192 sqlite3_tokenizer *pTokenizer; /* Tokenizer for this cursor. */
172193 /* Tokenizer implementations will typically add additional fields */
172194};
172195
172196int fts3_global_term_cnt(int iTerm, int iCol);
172197int fts3_term_cnt(int iTerm, int iCol);
172198
172199
172200#endif /* _FTS3_TOKENIZER_H_ */
172201
172202/************** End of fts3_tokenizer.h **************************************/
172203/************** Continuing where we left off in fts3Int.h ********************/
172204/************** Include fts3_hash.h in the middle of fts3Int.h ***************/
172205/************** Begin file fts3_hash.h ***************************************/
172206/*
172207** 2001 September 22
172208**
172209** The author disclaims copyright to this source code. In place of
172210** a legal notice, here is a blessing:
172211**
172212** May you do good and not evil.
172213** May you find forgiveness for yourself and forgive others.
172214** May you share freely, never taking more than you give.
172215**
172216*************************************************************************
172217** This is the header file for the generic hash-table implementation
172218** used in SQLite. We've modified it slightly to serve as a standalone
172219** hash table implementation for the full-text indexing module.
172220**
172221*/
172222#ifndef _FTS3_HASH_H_
172223#define _FTS3_HASH_H_
172224
172225/* Forward declarations of structures. */
172226typedef struct Fts3Hash Fts3Hash;
172227typedef struct Fts3HashElem Fts3HashElem;
172228
172229/* A complete hash table is an instance of the following structure.
172230** The internals of this structure are intended to be opaque -- client
172231** code should not attempt to access or modify the fields of this structure
172232** directly. Change this structure only by using the routines below.
172233** However, many of the "procedures" and "functions" for modifying and
172234** accessing this structure are really macros, so we can't really make
172235** this structure opaque.
172236*/
172237struct Fts3Hash {
172238 char keyClass; /* HASH_INT, _POINTER, _STRING, _BINARY */
172239 char copyKey; /* True if copy of key made on insert */
172240 int count; /* Number of entries in this table */
172241 Fts3HashElem *first; /* The first element of the array */
172242 int htsize; /* Number of buckets in the hash table */
172243 struct _fts3ht { /* the hash table */
172244 int count; /* Number of entries with this hash */
172245 Fts3HashElem *chain; /* Pointer to first entry with this hash */
172246 } *ht;
172247};
172248
172249/* Each element in the hash table is an instance of the following
172250** structure. All elements are stored on a single doubly-linked list.
172251**
172252** Again, this structure is intended to be opaque, but it can't really
172253** be opaque because it is used by macros.
172254*/
172255struct Fts3HashElem {
172256 Fts3HashElem *next, *prev; /* Next and previous elements in the table */
172257 void *data; /* Data associated with this element */
172258 void *pKey; int nKey; /* Key associated with this element */
172259};
172260
172261/*
172262** There are 2 different modes of operation for a hash table:
172263**
172264** FTS3_HASH_STRING pKey points to a string that is nKey bytes long
172265** (including the null-terminator, if any). Case
172266** is respected in comparisons.
172267**
172268** FTS3_HASH_BINARY pKey points to binary data nKey bytes long.
172269** memcmp() is used to compare keys.
172270**
172271** A copy of the key is made if the copyKey parameter to fts3HashInit is 1.
172272*/
172273#define FTS3_HASH_STRING 1
172274#define FTS3_HASH_BINARY 2
172275
172276/*
172277** Access routines. To delete, insert a NULL pointer.
172278*/
172279SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey);
172280SQLITE_PRIVATE void *sqlite3Fts3HashInsert(Fts3Hash*, const void *pKey, int nKey, void *pData);
172281SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash*, const void *pKey, int nKey);
172282SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash*);
172283SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(const Fts3Hash *, const void *, int);
172284
172285/*
172286** Shorthand for the functions above
172287*/
172288#define fts3HashInit sqlite3Fts3HashInit
172289#define fts3HashInsert sqlite3Fts3HashInsert
172290#define fts3HashFind sqlite3Fts3HashFind
172291#define fts3HashClear sqlite3Fts3HashClear
172292#define fts3HashFindElem sqlite3Fts3HashFindElem
172293
172294/*
172295** Macros for looping over all elements of a hash table. The idiom is
172296** like this:
172297**
172298** Fts3Hash h;
172299** Fts3HashElem *p;
172300** ...
172301** for(p=fts3HashFirst(&h); p; p=fts3HashNext(p)){
172302** SomeStructure *pData = fts3HashData(p);
172303** // do something with pData
172304** }
172305*/
172306#define fts3HashFirst(H) ((H)->first)
172307#define fts3HashNext(E) ((E)->next)
172308#define fts3HashData(E) ((E)->data)
172309#define fts3HashKey(E) ((E)->pKey)
172310#define fts3HashKeysize(E) ((E)->nKey)
172311
172312/*
172313** Number of entries in a hash table
172314*/
172315#define fts3HashCount(H) ((H)->count)
172316
172317#endif /* _FTS3_HASH_H_ */
172318
172319/************** End of fts3_hash.h *******************************************/
172320/************** Continuing where we left off in fts3Int.h ********************/
172321
172322/*
172323** This constant determines the maximum depth of an FTS expression tree
172324** that the library will create and use. FTS uses recursion to perform
172325** various operations on the query tree, so the disadvantage of a large
172326** limit is that it may allow very large queries to use large amounts
172327** of stack space (perhaps causing a stack overflow).
172328*/
172329#ifndef SQLITE_FTS3_MAX_EXPR_DEPTH
172330# define SQLITE_FTS3_MAX_EXPR_DEPTH 12
172331#endif
172332
172333
172334/*
172335** This constant controls how often segments are merged. Once there are
172336** FTS3_MERGE_COUNT segments of level N, they are merged into a single
172337** segment of level N+1.
172338*/
172339#define FTS3_MERGE_COUNT 16
172340
172341/*
172342** This is the maximum amount of data (in bytes) to store in the
172343** Fts3Table.pendingTerms hash table. Normally, the hash table is
172344** populated as documents are inserted/updated/deleted in a transaction
172345** and used to create a new segment when the transaction is committed.
172346** However if this limit is reached midway through a transaction, a new
172347** segment is created and the hash table cleared immediately.
172348*/
172349#define FTS3_MAX_PENDING_DATA (1*1024*1024)
172350
172351/*
172352** Macro to return the number of elements in an array. SQLite has a
172353** similar macro called ArraySize(). Use a different name to avoid
172354** a collision when building an amalgamation with built-in FTS3.
172355*/
172356#define SizeofArray(X) ((int)(sizeof(X)/sizeof(X[0])))
172357
172358
172359#ifndef MIN
172360# define MIN(x,y) ((x)<(y)?(x):(y))
172361#endif
172362#ifndef MAX
172363# define MAX(x,y) ((x)>(y)?(x):(y))
172364#endif
172365
172366/*
172367** Maximum length of a varint encoded integer. The varint format is different
172368** from that used by SQLite, so the maximum length is 10, not 9.
172369*/
172370#define FTS3_VARINT_MAX 10
172371
172372#define FTS3_BUFFER_PADDING 8
172373
172374/*
172375** FTS4 virtual tables may maintain multiple indexes - one index of all terms
172376** in the document set and zero or more prefix indexes. All indexes are stored
172377** as one or more b+-trees in the %_segments and %_segdir tables.
172378**
172379** It is possible to determine which index a b+-tree belongs to based on the
172380** value stored in the "%_segdir.level" column. Given this value L, the index
172381** that the b+-tree belongs to is (L<<10). In other words, all b+-trees with
172382** level values between 0 and 1023 (inclusive) belong to index 0, all levels
172383** between 1024 and 2047 to index 1, and so on.
172384**
172385** It is considered impossible for an index to use more than 1024 levels. In
172386** theory though this may happen, but only after at least
172387** (FTS3_MERGE_COUNT^1024) separate flushes of the pending-terms tables.
172388*/
172389#define FTS3_SEGDIR_MAXLEVEL 1024
172390#define FTS3_SEGDIR_MAXLEVEL_STR "1024"
172391
172392/*
172393** The testcase() macro is only used by the amalgamation. If undefined,
172394** make it a no-op.
172395*/
172396#ifndef testcase
172397# define testcase(X)
172398#endif
172399
172400/*
172401** Terminator values for position-lists and column-lists.
172402*/
172403#define POS_COLUMN (1) /* Column-list terminator */
172404#define POS_END (0) /* Position-list terminator */
172405
172406/*
172407** The assert_fts3_nc() macro is similar to the assert() macro, except that it
172408** is used for assert() conditions that are true only if it can be
172409** guranteed that the database is not corrupt.
172410*/
172411#ifdef SQLITE_DEBUG
172412SQLITE_API extern int sqlite3_fts3_may_be_corrupt;
172413# define assert_fts3_nc(x) assert(sqlite3_fts3_may_be_corrupt || (x))
172414#else
172415# define assert_fts3_nc(x) assert(x)
172416#endif
172417
172418/*
172419** This section provides definitions to allow the
172420** FTS3 extension to be compiled outside of the
172421** amalgamation.
172422*/
172423#ifndef SQLITE_AMALGAMATION
172424/*
172425** Macros indicating that conditional expressions are always true or
172426** false.
172427*/
172428#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
172429# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
172430#endif
172431#if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
172432# define ALWAYS(X) (1)
172433# define NEVER(X) (0)
172434#elif !defined(NDEBUG)
172435# define ALWAYS(X) ((X)?1:(assert(0),0))
172436# define NEVER(X) ((X)?(assert(0),1):0)
172437#else
172438# define ALWAYS(X) (X)
172439# define NEVER(X) (X)
172440#endif
172441
172442/*
172443** Internal types used by SQLite.
172444*/
172445typedef unsigned char u8; /* 1-byte (or larger) unsigned integer */
172446typedef short int i16; /* 2-byte (or larger) signed integer */
172447typedef unsigned int u32; /* 4-byte unsigned integer */
172448typedef sqlite3_uint64 u64; /* 8-byte unsigned integer */
172449typedef sqlite3_int64 i64; /* 8-byte signed integer */
172450
172451/*
172452** Macro used to suppress compiler warnings for unused parameters.
172453*/
172454#define UNUSED_PARAMETER(x) (void)(x)
172455
172456/*
172457** Activate assert() only if SQLITE_TEST is enabled.
172458*/
172459#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
172460# define NDEBUG 1
172461#endif
172462
172463/*
172464** The TESTONLY macro is used to enclose variable declarations or
172465** other bits of code that are needed to support the arguments
172466** within testcase() and assert() macros.
172467*/
172468#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
172469# define TESTONLY(X) X
172470#else
172471# define TESTONLY(X)
172472#endif
172473
172474#define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
172475#define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
172476
172477#define deliberate_fall_through
172478
172479#endif /* SQLITE_AMALGAMATION */
172480
172481#ifdef SQLITE_DEBUG
172482SQLITE_PRIVATE int sqlite3Fts3Corrupt(void);
172483# define FTS_CORRUPT_VTAB sqlite3Fts3Corrupt()
172484#else
172485# define FTS_CORRUPT_VTAB SQLITE_CORRUPT_VTAB
172486#endif
172487
172488typedef struct Fts3Table Fts3Table;
172489typedef struct Fts3Cursor Fts3Cursor;
172490typedef struct Fts3Expr Fts3Expr;
172491typedef struct Fts3Phrase Fts3Phrase;
172492typedef struct Fts3PhraseToken Fts3PhraseToken;
172493
172494typedef struct Fts3Doclist Fts3Doclist;
172495typedef struct Fts3SegFilter Fts3SegFilter;
172496typedef struct Fts3DeferredToken Fts3DeferredToken;
172497typedef struct Fts3SegReader Fts3SegReader;
172498typedef struct Fts3MultiSegReader Fts3MultiSegReader;
172499
172500typedef struct MatchinfoBuffer MatchinfoBuffer;
172501
172502/*
172503** A connection to a fulltext index is an instance of the following
172504** structure. The xCreate and xConnect methods create an instance
172505** of this structure and xDestroy and xDisconnect free that instance.
172506** All other methods receive a pointer to the structure as one of their
172507** arguments.
172508*/
172509struct Fts3Table {
172510 sqlite3_vtab base; /* Base class used by SQLite core */
172511 sqlite3 *db; /* The database connection */
172512 const char *zDb; /* logical database name */
172513 const char *zName; /* virtual table name */
172514 int nColumn; /* number of named columns in virtual table */
172515 char **azColumn; /* column names. malloced */
172516 u8 *abNotindexed; /* True for 'notindexed' columns */
172517 sqlite3_tokenizer *pTokenizer; /* tokenizer for inserts and queries */
172518 char *zContentTbl; /* content=xxx option, or NULL */
172519 char *zLanguageid; /* languageid=xxx option, or NULL */
172520 int nAutoincrmerge; /* Value configured by 'automerge' */
172521 u32 nLeafAdd; /* Number of leaf blocks added this trans */
172522 int bLock; /* Used to prevent recursive content= tbls */
172523
172524 /* Precompiled statements used by the implementation. Each of these
172525 ** statements is run and reset within a single virtual table API call.
172526 */
172527 sqlite3_stmt *aStmt[40];
172528 sqlite3_stmt *pSeekStmt; /* Cache for fts3CursorSeekStmt() */
172529
172530 char *zReadExprlist;
172531 char *zWriteExprlist;
172532
172533 int nNodeSize; /* Soft limit for node size */
172534 u8 bFts4; /* True for FTS4, false for FTS3 */
172535 u8 bHasStat; /* True if %_stat table exists (2==unknown) */
172536 u8 bHasDocsize; /* True if %_docsize table exists */
172537 u8 bDescIdx; /* True if doclists are in reverse order */
172538 u8 bIgnoreSavepoint; /* True to ignore xSavepoint invocations */
172539 int nPgsz; /* Page size for host database */
172540 char *zSegmentsTbl; /* Name of %_segments table */
172541 sqlite3_blob *pSegments; /* Blob handle open on %_segments table */
172542
172543 /*
172544 ** The following array of hash tables is used to buffer pending index
172545 ** updates during transactions. All pending updates buffered at any one
172546 ** time must share a common language-id (see the FTS4 langid= feature).
172547 ** The current language id is stored in variable iPrevLangid.
172548 **
172549 ** A single FTS4 table may have multiple full-text indexes. For each index
172550 ** there is an entry in the aIndex[] array. Index 0 is an index of all the
172551 ** terms that appear in the document set. Each subsequent index in aIndex[]
172552 ** is an index of prefixes of a specific length.
172553 **
172554 ** Variable nPendingData contains an estimate the memory consumed by the
172555 ** pending data structures, including hash table overhead, but not including
172556 ** malloc overhead. When nPendingData exceeds nMaxPendingData, all hash
172557 ** tables are flushed to disk. Variable iPrevDocid is the docid of the most
172558 ** recently inserted record.
172559 */
172560 int nIndex; /* Size of aIndex[] */
172561 struct Fts3Index {
172562 int nPrefix; /* Prefix length (0 for main terms index) */
172563 Fts3Hash hPending; /* Pending terms table for this index */
172564 } *aIndex;
172565 int nMaxPendingData; /* Max pending data before flush to disk */
172566 int nPendingData; /* Current bytes of pending data */
172567 sqlite_int64 iPrevDocid; /* Docid of most recently inserted document */
172568 int iPrevLangid; /* Langid of recently inserted document */
172569 int bPrevDelete; /* True if last operation was a delete */
172570
172571#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
172572 /* State variables used for validating that the transaction control
172573 ** methods of the virtual table are called at appropriate times. These
172574 ** values do not contribute to FTS functionality; they are used for
172575 ** verifying the operation of the SQLite core.
172576 */
172577 int inTransaction; /* True after xBegin but before xCommit/xRollback */
172578 int mxSavepoint; /* Largest valid xSavepoint integer */
172579#endif
172580
172581#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
172582 /* True to disable the incremental doclist optimization. This is controled
172583 ** by special insert command 'test-no-incr-doclist'. */
172584 int bNoIncrDoclist;
172585
172586 /* Number of segments in a level */
172587 int nMergeCount;
172588#endif
172589};
172590
172591/* Macro to find the number of segments to merge */
172592#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
172593# define MergeCount(P) ((P)->nMergeCount)
172594#else
172595# define MergeCount(P) FTS3_MERGE_COUNT
172596#endif
172597
172598/*
172599** When the core wants to read from the virtual table, it creates a
172600** virtual table cursor (an instance of the following structure) using
172601** the xOpen method. Cursors are destroyed using the xClose method.
172602*/
172603struct Fts3Cursor {
172604 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
172605 i16 eSearch; /* Search strategy (see below) */
172606 u8 isEof; /* True if at End Of Results */
172607 u8 isRequireSeek; /* True if must seek pStmt to %_content row */
172608 u8 bSeekStmt; /* True if pStmt is a seek */
172609 sqlite3_stmt *pStmt; /* Prepared statement in use by the cursor */
172610 Fts3Expr *pExpr; /* Parsed MATCH query string */
172611 int iLangid; /* Language being queried for */
172612 int nPhrase; /* Number of matchable phrases in query */
172613 Fts3DeferredToken *pDeferred; /* Deferred search tokens, if any */
172614 sqlite3_int64 iPrevId; /* Previous id read from aDoclist */
172615 char *pNextId; /* Pointer into the body of aDoclist */
172616 char *aDoclist; /* List of docids for full-text queries */
172617 int nDoclist; /* Size of buffer at aDoclist */
172618 u8 bDesc; /* True to sort in descending order */
172619 int eEvalmode; /* An FTS3_EVAL_XX constant */
172620 int nRowAvg; /* Average size of database rows, in pages */
172621 sqlite3_int64 nDoc; /* Documents in table */
172622 i64 iMinDocid; /* Minimum docid to return */
172623 i64 iMaxDocid; /* Maximum docid to return */
172624 int isMatchinfoNeeded; /* True when aMatchinfo[] needs filling in */
172625 MatchinfoBuffer *pMIBuffer; /* Buffer for matchinfo data */
172626};
172627
172628#define FTS3_EVAL_FILTER 0
172629#define FTS3_EVAL_NEXT 1
172630#define FTS3_EVAL_MATCHINFO 2
172631
172632/*
172633** The Fts3Cursor.eSearch member is always set to one of the following.
172634** Actualy, Fts3Cursor.eSearch can be greater than or equal to
172635** FTS3_FULLTEXT_SEARCH. If so, then Fts3Cursor.eSearch - 2 is the index
172636** of the column to be searched. For example, in
172637**
172638** CREATE VIRTUAL TABLE ex1 USING fts3(a,b,c,d);
172639** SELECT docid FROM ex1 WHERE b MATCH 'one two three';
172640**
172641** Because the LHS of the MATCH operator is 2nd column "b",
172642** Fts3Cursor.eSearch will be set to FTS3_FULLTEXT_SEARCH+1. (+0 for a,
172643** +1 for b, +2 for c, +3 for d.) If the LHS of MATCH were "ex1"
172644** indicating that all columns should be searched,
172645** then eSearch would be set to FTS3_FULLTEXT_SEARCH+4.
172646*/
172647#define FTS3_FULLSCAN_SEARCH 0 /* Linear scan of %_content table */
172648#define FTS3_DOCID_SEARCH 1 /* Lookup by rowid on %_content table */
172649#define FTS3_FULLTEXT_SEARCH 2 /* Full-text index search */
172650
172651/*
172652** The lower 16-bits of the sqlite3_index_info.idxNum value set by
172653** the xBestIndex() method contains the Fts3Cursor.eSearch value described
172654** above. The upper 16-bits contain a combination of the following
172655** bits, used to describe extra constraints on full-text searches.
172656*/
172657#define FTS3_HAVE_LANGID 0x00010000 /* languageid=? */
172658#define FTS3_HAVE_DOCID_GE 0x00020000 /* docid>=? */
172659#define FTS3_HAVE_DOCID_LE 0x00040000 /* docid<=? */
172660
172661struct Fts3Doclist {
172662 char *aAll; /* Array containing doclist (or NULL) */
172663 int nAll; /* Size of a[] in bytes */
172664 char *pNextDocid; /* Pointer to next docid */
172665
172666 sqlite3_int64 iDocid; /* Current docid (if pList!=0) */
172667 int bFreeList; /* True if pList should be sqlite3_free()d */
172668 char *pList; /* Pointer to position list following iDocid */
172669 int nList; /* Length of position list */
172670};
172671
172672/*
172673** A "phrase" is a sequence of one or more tokens that must match in
172674** sequence. A single token is the base case and the most common case.
172675** For a sequence of tokens contained in double-quotes (i.e. "one two three")
172676** nToken will be the number of tokens in the string.
172677*/
172678struct Fts3PhraseToken {
172679 char *z; /* Text of the token */
172680 int n; /* Number of bytes in buffer z */
172681 int isPrefix; /* True if token ends with a "*" character */
172682 int bFirst; /* True if token must appear at position 0 */
172683
172684 /* Variables above this point are populated when the expression is
172685 ** parsed (by code in fts3_expr.c). Below this point the variables are
172686 ** used when evaluating the expression. */
172687 Fts3DeferredToken *pDeferred; /* Deferred token object for this token */
172688 Fts3MultiSegReader *pSegcsr; /* Segment-reader for this token */
172689};
172690
172691struct Fts3Phrase {
172692 /* Cache of doclist for this phrase. */
172693 Fts3Doclist doclist;
172694 int bIncr; /* True if doclist is loaded incrementally */
172695 int iDoclistToken;
172696
172697 /* Used by sqlite3Fts3EvalPhrasePoslist() if this is a descendent of an
172698 ** OR condition. */
172699 char *pOrPoslist;
172700 i64 iOrDocid;
172701
172702 /* Variables below this point are populated by fts3_expr.c when parsing
172703 ** a MATCH expression. Everything above is part of the evaluation phase.
172704 */
172705 int nToken; /* Number of tokens in the phrase */
172706 int iColumn; /* Index of column this phrase must match */
172707 Fts3PhraseToken aToken[1]; /* One entry for each token in the phrase */
172708};
172709
172710/*
172711** A tree of these objects forms the RHS of a MATCH operator.
172712**
172713** If Fts3Expr.eType is FTSQUERY_PHRASE and isLoaded is true, then aDoclist
172714** points to a malloced buffer, size nDoclist bytes, containing the results
172715** of this phrase query in FTS3 doclist format. As usual, the initial
172716** "Length" field found in doclists stored on disk is omitted from this
172717** buffer.
172718**
172719** Variable aMI is used only for FTSQUERY_NEAR nodes to store the global
172720** matchinfo data. If it is not NULL, it points to an array of size nCol*3,
172721** where nCol is the number of columns in the queried FTS table. The array
172722** is populated as follows:
172723**
172724** aMI[iCol*3 + 0] = Undefined
172725** aMI[iCol*3 + 1] = Number of occurrences
172726** aMI[iCol*3 + 2] = Number of rows containing at least one instance
172727**
172728** The aMI array is allocated using sqlite3_malloc(). It should be freed
172729** when the expression node is.
172730*/
172731struct Fts3Expr {
172732 int eType; /* One of the FTSQUERY_XXX values defined below */
172733 int nNear; /* Valid if eType==FTSQUERY_NEAR */
172734 Fts3Expr *pParent; /* pParent->pLeft==this or pParent->pRight==this */
172735 Fts3Expr *pLeft; /* Left operand */
172736 Fts3Expr *pRight; /* Right operand */
172737 Fts3Phrase *pPhrase; /* Valid if eType==FTSQUERY_PHRASE */
172738
172739 /* The following are used by the fts3_eval.c module. */
172740 sqlite3_int64 iDocid; /* Current docid */
172741 u8 bEof; /* True this expression is at EOF already */
172742 u8 bStart; /* True if iDocid is valid */
172743 u8 bDeferred; /* True if this expression is entirely deferred */
172744
172745 /* The following are used by the fts3_snippet.c module. */
172746 int iPhrase; /* Index of this phrase in matchinfo() results */
172747 u32 *aMI; /* See above */
172748};
172749
172750/*
172751** Candidate values for Fts3Query.eType. Note that the order of the first
172752** four values is in order of precedence when parsing expressions. For
172753** example, the following:
172754**
172755** "a OR b AND c NOT d NEAR e"
172756**
172757** is equivalent to:
172758**
172759** "a OR (b AND (c NOT (d NEAR e)))"
172760*/
172761#define FTSQUERY_NEAR 1
172762#define FTSQUERY_NOT 2
172763#define FTSQUERY_AND 3
172764#define FTSQUERY_OR 4
172765#define FTSQUERY_PHRASE 5
172766
172767
172768/* fts3_write.c */
172769SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(sqlite3_vtab*,int,sqlite3_value**,sqlite3_int64*);
172770SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *);
172771SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *);
172772SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *);
172773SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(int, int, sqlite3_int64,
172774 sqlite3_int64, sqlite3_int64, const char *, int, Fts3SegReader**);
172775SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
172776 Fts3Table*,int,const char*,int,int,Fts3SegReader**);
172777SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *);
172778SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(Fts3Table*, int, int, int, sqlite3_stmt **);
172779SQLITE_PRIVATE int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char **, int*, int*);
172780
172781SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(Fts3Table *, sqlite3_stmt **);
172782SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(Fts3Table *, sqlite3_int64, sqlite3_stmt **);
172783
172784#ifndef SQLITE_DISABLE_FTS4_DEFERRED
172785SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *);
172786SQLITE_PRIVATE int sqlite3Fts3DeferToken(Fts3Cursor *, Fts3PhraseToken *, int);
172787SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *);
172788SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *);
172789SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(Fts3DeferredToken *, char **, int *);
172790#else
172791# define sqlite3Fts3FreeDeferredTokens(x)
172792# define sqlite3Fts3DeferToken(x,y,z) SQLITE_OK
172793# define sqlite3Fts3CacheDeferredDoclists(x) SQLITE_OK
172794# define sqlite3Fts3FreeDeferredDoclists(x)
172795# define sqlite3Fts3DeferredTokenList(x,y,z) SQLITE_OK
172796#endif
172797
172798SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *);
172799SQLITE_PRIVATE int sqlite3Fts3MaxLevel(Fts3Table *, int *);
172800
172801/* Special values interpreted by sqlite3SegReaderCursor() */
172802#define FTS3_SEGCURSOR_PENDING -1
172803#define FTS3_SEGCURSOR_ALL -2
172804
172805SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(Fts3Table*, Fts3MultiSegReader*, Fts3SegFilter*);
172806SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(Fts3Table *, Fts3MultiSegReader *);
172807SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(Fts3MultiSegReader *);
172808
172809SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(Fts3Table *,
172810 int, int, int, const char *, int, int, int, Fts3MultiSegReader *);
172811
172812/* Flags allowed as part of the 4th argument to SegmentReaderIterate() */
172813#define FTS3_SEGMENT_REQUIRE_POS 0x00000001
172814#define FTS3_SEGMENT_IGNORE_EMPTY 0x00000002
172815#define FTS3_SEGMENT_COLUMN_FILTER 0x00000004
172816#define FTS3_SEGMENT_PREFIX 0x00000008
172817#define FTS3_SEGMENT_SCAN 0x00000010
172818#define FTS3_SEGMENT_FIRST 0x00000020
172819
172820/* Type passed as 4th argument to SegmentReaderIterate() */
172821struct Fts3SegFilter {
172822 const char *zTerm;
172823 int nTerm;
172824 int iCol;
172825 int flags;
172826};
172827
172828struct Fts3MultiSegReader {
172829 /* Used internally by sqlite3Fts3SegReaderXXX() calls */
172830 Fts3SegReader **apSegment; /* Array of Fts3SegReader objects */
172831 int nSegment; /* Size of apSegment array */
172832 int nAdvance; /* How many seg-readers to advance */
172833 Fts3SegFilter *pFilter; /* Pointer to filter object */
172834 char *aBuffer; /* Buffer to merge doclists in */
172835 int nBuffer; /* Allocated size of aBuffer[] in bytes */
172836
172837 int iColFilter; /* If >=0, filter for this column */
172838 int bRestart;
172839
172840 /* Used by fts3.c only. */
172841 int nCost; /* Cost of running iterator */
172842 int bLookup; /* True if a lookup of a single entry. */
172843
172844 /* Output values. Valid only after Fts3SegReaderStep() returns SQLITE_ROW. */
172845 char *zTerm; /* Pointer to term buffer */
172846 int nTerm; /* Size of zTerm in bytes */
172847 char *aDoclist; /* Pointer to doclist buffer */
172848 int nDoclist; /* Size of aDoclist[] in bytes */
172849};
172850
172851SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table*,int,int);
172852
172853#define fts3GetVarint32(p, piVal) ( \
172854 (*(u8*)(p)&0x80) ? sqlite3Fts3GetVarint32(p, piVal) : (*piVal=*(u8*)(p), 1) \
172855)
172856
172857/* fts3.c */
172858SQLITE_PRIVATE void sqlite3Fts3ErrMsg(char**,const char*,...);
172859SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *, sqlite3_int64);
172860SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *, sqlite_int64 *);
172861SQLITE_PRIVATE int sqlite3Fts3GetVarintU(const char *, sqlite_uint64 *);
172862SQLITE_PRIVATE int sqlite3Fts3GetVarintBounded(const char*,const char*,sqlite3_int64*);
172863SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *, int *);
172864SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64);
172865SQLITE_PRIVATE void sqlite3Fts3Dequote(char *);
172866SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*);
172867SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *);
172868SQLITE_PRIVATE int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *);
172869SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int*, Fts3Table*);
172870SQLITE_PRIVATE int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc);
172871SQLITE_PRIVATE int sqlite3Fts3ReadInt(const char *z, int *pnOut);
172872
172873/* fts3_tokenizer.c */
172874SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *, int *);
172875SQLITE_PRIVATE int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *);
172876SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *,
172877 sqlite3_tokenizer **, char **
172878);
172879SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char);
172880
172881/* fts3_snippet.c */
172882SQLITE_PRIVATE void sqlite3Fts3Offsets(sqlite3_context*, Fts3Cursor*);
172883SQLITE_PRIVATE void sqlite3Fts3Snippet(sqlite3_context *, Fts3Cursor *, const char *,
172884 const char *, const char *, int, int
172885);
172886SQLITE_PRIVATE void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *);
172887SQLITE_PRIVATE void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p);
172888
172889/* fts3_expr.c */
172890SQLITE_PRIVATE int sqlite3Fts3ExprParse(sqlite3_tokenizer *, int,
172891 char **, int, int, int, const char *, int, Fts3Expr **, char **
172892);
172893SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *);
172894#ifdef SQLITE_TEST
172895SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db, Fts3Hash*);
172896SQLITE_PRIVATE int sqlite3Fts3InitTerm(sqlite3 *db);
172897#endif
172898SQLITE_PRIVATE void *sqlite3Fts3MallocZero(i64 nByte);
172899
172900SQLITE_PRIVATE int sqlite3Fts3OpenTokenizer(sqlite3_tokenizer *, int, const char *, int,
172901 sqlite3_tokenizer_cursor **
172902);
172903
172904/* fts3_aux.c */
172905SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db);
172906
172907SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *);
172908
172909SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
172910 Fts3Table*, Fts3MultiSegReader*, int, const char*, int);
172911SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
172912 Fts3Table *, Fts3MultiSegReader *, sqlite3_int64 *, char **, int *);
172913SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol, char **);
172914SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
172915SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
172916
172917/* fts3_tokenize_vtab.c */
172918SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3*, Fts3Hash *);
172919
172920/* fts3_unicode2.c (functions generated by parsing unicode text files) */
172921#ifndef SQLITE_DISABLE_FTS3_UNICODE
172922SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int, int);
172923SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int);
172924SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int);
172925#endif
172926
172927#endif /* !SQLITE_CORE || SQLITE_ENABLE_FTS3 */
172928#endif /* _FTSINT_H */
172929
172930/************** End of fts3Int.h *********************************************/
172931/************** Continuing where we left off in fts3.c ***********************/
172932#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
172933
172934#if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE)
172935# define SQLITE_CORE 1
172936#endif
172937
172938/* #include <assert.h> */
172939/* #include <stdlib.h> */
172940/* #include <stddef.h> */
172941/* #include <stdio.h> */
172942/* #include <string.h> */
172943/* #include <stdarg.h> */
172944
172945/* #include "fts3.h" */
172946#ifndef SQLITE_CORE
172947/* # include "sqlite3ext.h" */
172948 SQLITE_EXTENSION_INIT1
172949#endif
172950
172951static int fts3EvalNext(Fts3Cursor *pCsr);
172952static int fts3EvalStart(Fts3Cursor *pCsr);
172953static int fts3TermSegReaderCursor(
172954 Fts3Cursor *, const char *, int, int, Fts3MultiSegReader **);
172955
172956/*
172957** This variable is set to false when running tests for which the on disk
172958** structures should not be corrupt. Otherwise, true. If it is false, extra
172959** assert() conditions in the fts3 code are activated - conditions that are
172960** only true if it is guaranteed that the fts3 database is not corrupt.
172961*/
172962#ifdef SQLITE_DEBUG
172963SQLITE_API int sqlite3_fts3_may_be_corrupt = 1;
172964#endif
172965
172966/*
172967** Write a 64-bit variable-length integer to memory starting at p[0].
172968** The length of data written will be between 1 and FTS3_VARINT_MAX bytes.
172969** The number of bytes written is returned.
172970*/
172971SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *p, sqlite_int64 v){
172972 unsigned char *q = (unsigned char *) p;
172973 sqlite_uint64 vu = v;
172974 do{
172975 *q++ = (unsigned char) ((vu & 0x7f) | 0x80);
172976 vu >>= 7;
172977 }while( vu!=0 );
172978 q[-1] &= 0x7f; /* turn off high bit in final byte */
172979 assert( q - (unsigned char *)p <= FTS3_VARINT_MAX );
172980 return (int) (q - (unsigned char *)p);
172981}
172982
172983#define GETVARINT_STEP(v, ptr, shift, mask1, mask2, var, ret) \
172984 v = (v & mask1) | ( (*(const unsigned char*)(ptr++)) << shift ); \
172985 if( (v & mask2)==0 ){ var = v; return ret; }
172986#define GETVARINT_INIT(v, ptr, shift, mask1, mask2, var, ret) \
172987 v = (*ptr++); \
172988 if( (v & mask2)==0 ){ var = v; return ret; }
172989
172990SQLITE_PRIVATE int sqlite3Fts3GetVarintU(const char *pBuf, sqlite_uint64 *v){
172991 const unsigned char *p = (const unsigned char*)pBuf;
172992 const unsigned char *pStart = p;
172993 u32 a;
172994 u64 b;
172995 int shift;
172996
172997 GETVARINT_INIT(a, p, 0, 0x00, 0x80, *v, 1);
172998 GETVARINT_STEP(a, p, 7, 0x7F, 0x4000, *v, 2);
172999 GETVARINT_STEP(a, p, 14, 0x3FFF, 0x200000, *v, 3);
173000 GETVARINT_STEP(a, p, 21, 0x1FFFFF, 0x10000000, *v, 4);
173001 b = (a & 0x0FFFFFFF );
173002
173003 for(shift=28; shift<=63; shift+=7){
173004 u64 c = *p++;
173005 b += (c&0x7F) << shift;
173006 if( (c & 0x80)==0 ) break;
173007 }
173008 *v = b;
173009 return (int)(p - pStart);
173010}
173011
173012/*
173013** Read a 64-bit variable-length integer from memory starting at p[0].
173014** Return the number of bytes read, or 0 on error.
173015** The value is stored in *v.
173016*/
173017SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *pBuf, sqlite_int64 *v){
173018 return sqlite3Fts3GetVarintU(pBuf, v: (sqlite3_uint64*)v);
173019}
173020
173021/*
173022** Read a 64-bit variable-length integer from memory starting at p[0] and
173023** not extending past pEnd[-1].
173024** Return the number of bytes read, or 0 on error.
173025** The value is stored in *v.
173026*/
173027SQLITE_PRIVATE int sqlite3Fts3GetVarintBounded(
173028 const char *pBuf,
173029 const char *pEnd,
173030 sqlite_int64 *v
173031){
173032 const unsigned char *p = (const unsigned char*)pBuf;
173033 const unsigned char *pStart = p;
173034 const unsigned char *pX = (const unsigned char*)pEnd;
173035 u64 b = 0;
173036 int shift;
173037 for(shift=0; shift<=63; shift+=7){
173038 u64 c = p<pX ? *p : 0;
173039 p++;
173040 b += (c&0x7F) << shift;
173041 if( (c & 0x80)==0 ) break;
173042 }
173043 *v = b;
173044 return (int)(p - pStart);
173045}
173046
173047/*
173048** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to
173049** a non-negative 32-bit integer before it is returned.
173050*/
173051SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *p, int *pi){
173052 const unsigned char *ptr = (const unsigned char*)p;
173053 u32 a;
173054
173055#ifndef fts3GetVarint32
173056 GETVARINT_INIT(a, ptr, 0, 0x00, 0x80, *pi, 1);
173057#else
173058 a = (*ptr++);
173059 assert( a & 0x80 );
173060#endif
173061
173062 GETVARINT_STEP(a, ptr, 7, 0x7F, 0x4000, *pi, 2);
173063 GETVARINT_STEP(a, ptr, 14, 0x3FFF, 0x200000, *pi, 3);
173064 GETVARINT_STEP(a, ptr, 21, 0x1FFFFF, 0x10000000, *pi, 4);
173065 a = (a & 0x0FFFFFFF );
173066 *pi = (int)(a | ((u32)(*ptr & 0x07) << 28));
173067 assert( 0==(a & 0x80000000) );
173068 assert( *pi>=0 );
173069 return 5;
173070}
173071
173072/*
173073** Return the number of bytes required to encode v as a varint
173074*/
173075SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64 v){
173076 int i = 0;
173077 do{
173078 i++;
173079 v >>= 7;
173080 }while( v!=0 );
173081 return i;
173082}
173083
173084/*
173085** Convert an SQL-style quoted string into a normal string by removing
173086** the quote characters. The conversion is done in-place. If the
173087** input does not begin with a quote character, then this routine
173088** is a no-op.
173089**
173090** Examples:
173091**
173092** "abc" becomes abc
173093** 'xyz' becomes xyz
173094** [pqr] becomes pqr
173095** `mno` becomes mno
173096**
173097*/
173098SQLITE_PRIVATE void sqlite3Fts3Dequote(char *z){
173099 char quote; /* Quote character (if any ) */
173100
173101 quote = z[0];
173102 if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
173103 int iIn = 1; /* Index of next byte to read from input */
173104 int iOut = 0; /* Index of next byte to write to output */
173105
173106 /* If the first byte was a '[', then the close-quote character is a ']' */
173107 if( quote=='[' ) quote = ']';
173108
173109 while( z[iIn] ){
173110 if( z[iIn]==quote ){
173111 if( z[iIn+1]!=quote ) break;
173112 z[iOut++] = quote;
173113 iIn += 2;
173114 }else{
173115 z[iOut++] = z[iIn++];
173116 }
173117 }
173118 z[iOut] = '\0';
173119 }
173120}
173121
173122/*
173123** Read a single varint from the doclist at *pp and advance *pp to point
173124** to the first byte past the end of the varint. Add the value of the varint
173125** to *pVal.
173126*/
173127static void fts3GetDeltaVarint(char **pp, sqlite3_int64 *pVal){
173128 sqlite3_int64 iVal;
173129 *pp += sqlite3Fts3GetVarint(pBuf: *pp, v: &iVal);
173130 *pVal += iVal;
173131}
173132
173133/*
173134** When this function is called, *pp points to the first byte following a
173135** varint that is part of a doclist (or position-list, or any other list
173136** of varints). This function moves *pp to point to the start of that varint,
173137** and sets *pVal by the varint value.
173138**
173139** Argument pStart points to the first byte of the doclist that the
173140** varint is part of.
173141*/
173142static void fts3GetReverseVarint(
173143 char **pp,
173144 char *pStart,
173145 sqlite3_int64 *pVal
173146){
173147 sqlite3_int64 iVal;
173148 char *p;
173149
173150 /* Pointer p now points at the first byte past the varint we are
173151 ** interested in. So, unless the doclist is corrupt, the 0x80 bit is
173152 ** clear on character p[-1]. */
173153 for(p = (*pp)-2; p>=pStart && *p&0x80; p--);
173154 p++;
173155 *pp = p;
173156
173157 sqlite3Fts3GetVarint(pBuf: p, v: &iVal);
173158 *pVal = iVal;
173159}
173160
173161/*
173162** The xDisconnect() virtual table method.
173163*/
173164static int fts3DisconnectMethod(sqlite3_vtab *pVtab){
173165 Fts3Table *p = (Fts3Table *)pVtab;
173166 int i;
173167
173168 assert( p->nPendingData==0 );
173169 assert( p->pSegments==0 );
173170
173171 /* Free any prepared statements held */
173172 sqlite3_finalize(pStmt: p->pSeekStmt);
173173 for(i=0; i<SizeofArray(p->aStmt); i++){
173174 sqlite3_finalize(pStmt: p->aStmt[i]);
173175 }
173176 sqlite3_free(p: p->zSegmentsTbl);
173177 sqlite3_free(p: p->zReadExprlist);
173178 sqlite3_free(p: p->zWriteExprlist);
173179 sqlite3_free(p: p->zContentTbl);
173180 sqlite3_free(p: p->zLanguageid);
173181
173182 /* Invoke the tokenizer destructor to free the tokenizer. */
173183 p->pTokenizer->pModule->xDestroy(p->pTokenizer);
173184
173185 sqlite3_free(p);
173186 return SQLITE_OK;
173187}
173188
173189/*
173190** Write an error message into *pzErr
173191*/
173192SQLITE_PRIVATE void sqlite3Fts3ErrMsg(char **pzErr, const char *zFormat, ...){
173193 va_list ap;
173194 sqlite3_free(p: *pzErr);
173195 va_start(ap, zFormat);
173196 *pzErr = sqlite3_vmprintf(zFormat, ap);
173197 va_end(ap);
173198}
173199
173200/*
173201** Construct one or more SQL statements from the format string given
173202** and then evaluate those statements. The success code is written
173203** into *pRc.
173204**
173205** If *pRc is initially non-zero then this routine is a no-op.
173206*/
173207static void fts3DbExec(
173208 int *pRc, /* Success code */
173209 sqlite3 *db, /* Database in which to run SQL */
173210 const char *zFormat, /* Format string for SQL */
173211 ... /* Arguments to the format string */
173212){
173213 va_list ap;
173214 char *zSql;
173215 if( *pRc ) return;
173216 va_start(ap, zFormat);
173217 zSql = sqlite3_vmprintf(zFormat, ap);
173218 va_end(ap);
173219 if( zSql==0 ){
173220 *pRc = SQLITE_NOMEM;
173221 }else{
173222 *pRc = sqlite3_exec(db, zSql, xCallback: 0, pArg: 0, pzErrMsg: 0);
173223 sqlite3_free(p: zSql);
173224 }
173225}
173226
173227/*
173228** The xDestroy() virtual table method.
173229*/
173230static int fts3DestroyMethod(sqlite3_vtab *pVtab){
173231 Fts3Table *p = (Fts3Table *)pVtab;
173232 int rc = SQLITE_OK; /* Return code */
173233 const char *zDb = p->zDb; /* Name of database (e.g. "main", "temp") */
173234 sqlite3 *db = p->db; /* Database handle */
173235
173236 /* Drop the shadow tables */
173237 fts3DbExec(pRc: &rc, db,
173238 zFormat: "DROP TABLE IF EXISTS %Q.'%q_segments';"
173239 "DROP TABLE IF EXISTS %Q.'%q_segdir';"
173240 "DROP TABLE IF EXISTS %Q.'%q_docsize';"
173241 "DROP TABLE IF EXISTS %Q.'%q_stat';"
173242 "%s DROP TABLE IF EXISTS %Q.'%q_content';",
173243 zDb, p->zName,
173244 zDb, p->zName,
173245 zDb, p->zName,
173246 zDb, p->zName,
173247 (p->zContentTbl ? "--" : ""), zDb,p->zName
173248 );
173249
173250 /* If everything has worked, invoke fts3DisconnectMethod() to free the
173251 ** memory associated with the Fts3Table structure and return SQLITE_OK.
173252 ** Otherwise, return an SQLite error code.
173253 */
173254 return (rc==SQLITE_OK ? fts3DisconnectMethod(pVtab) : rc);
173255}
173256
173257
173258/*
173259** Invoke sqlite3_declare_vtab() to declare the schema for the FTS3 table
173260** passed as the first argument. This is done as part of the xConnect()
173261** and xCreate() methods.
173262**
173263** If *pRc is non-zero when this function is called, it is a no-op.
173264** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
173265** before returning.
173266*/
173267static void fts3DeclareVtab(int *pRc, Fts3Table *p){
173268 if( *pRc==SQLITE_OK ){
173269 int i; /* Iterator variable */
173270 int rc; /* Return code */
173271 char *zSql; /* SQL statement passed to declare_vtab() */
173272 char *zCols; /* List of user defined columns */
173273 const char *zLanguageid;
173274
173275 zLanguageid = (p->zLanguageid ? p->zLanguageid : "__langid");
173276 sqlite3_vtab_config(db: p->db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
173277
173278 /* Create a list of user columns for the virtual table */
173279 zCols = sqlite3_mprintf(zFormat: "%Q, ", p->azColumn[0]);
173280 for(i=1; zCols && i<p->nColumn; i++){
173281 zCols = sqlite3_mprintf(zFormat: "%z%Q, ", zCols, p->azColumn[i]);
173282 }
173283
173284 /* Create the whole "CREATE TABLE" statement to pass to SQLite */
173285 zSql = sqlite3_mprintf(
173286 zFormat: "CREATE TABLE x(%s %Q HIDDEN, docid HIDDEN, %Q HIDDEN)",
173287 zCols, p->zName, zLanguageid
173288 );
173289 if( !zCols || !zSql ){
173290 rc = SQLITE_NOMEM;
173291 }else{
173292 rc = sqlite3_declare_vtab(db: p->db, zCreateTable: zSql);
173293 }
173294
173295 sqlite3_free(p: zSql);
173296 sqlite3_free(p: zCols);
173297 *pRc = rc;
173298 }
173299}
173300
173301/*
173302** Create the %_stat table if it does not already exist.
173303*/
173304SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int *pRc, Fts3Table *p){
173305 fts3DbExec(pRc, db: p->db,
173306 zFormat: "CREATE TABLE IF NOT EXISTS %Q.'%q_stat'"
173307 "(id INTEGER PRIMARY KEY, value BLOB);",
173308 p->zDb, p->zName
173309 );
173310 if( (*pRc)==SQLITE_OK ) p->bHasStat = 1;
173311}
173312
173313/*
173314** Create the backing store tables (%_content, %_segments and %_segdir)
173315** required by the FTS3 table passed as the only argument. This is done
173316** as part of the vtab xCreate() method.
173317**
173318** If the p->bHasDocsize boolean is true (indicating that this is an
173319** FTS4 table, not an FTS3 table) then also create the %_docsize and
173320** %_stat tables required by FTS4.
173321*/
173322static int fts3CreateTables(Fts3Table *p){
173323 int rc = SQLITE_OK; /* Return code */
173324 int i; /* Iterator variable */
173325 sqlite3 *db = p->db; /* The database connection */
173326
173327 if( p->zContentTbl==0 ){
173328 const char *zLanguageid = p->zLanguageid;
173329 char *zContentCols; /* Columns of %_content table */
173330
173331 /* Create a list of user columns for the content table */
173332 zContentCols = sqlite3_mprintf(zFormat: "docid INTEGER PRIMARY KEY");
173333 for(i=0; zContentCols && i<p->nColumn; i++){
173334 char *z = p->azColumn[i];
173335 zContentCols = sqlite3_mprintf(zFormat: "%z, 'c%d%q'", zContentCols, i, z);
173336 }
173337 if( zLanguageid && zContentCols ){
173338 zContentCols = sqlite3_mprintf(zFormat: "%z, langid", zContentCols, zLanguageid);
173339 }
173340 if( zContentCols==0 ) rc = SQLITE_NOMEM;
173341
173342 /* Create the content table */
173343 fts3DbExec(pRc: &rc, db,
173344 zFormat: "CREATE TABLE %Q.'%q_content'(%s)",
173345 p->zDb, p->zName, zContentCols
173346 );
173347 sqlite3_free(p: zContentCols);
173348 }
173349
173350 /* Create other tables */
173351 fts3DbExec(pRc: &rc, db,
173352 zFormat: "CREATE TABLE %Q.'%q_segments'(blockid INTEGER PRIMARY KEY, block BLOB);",
173353 p->zDb, p->zName
173354 );
173355 fts3DbExec(pRc: &rc, db,
173356 zFormat: "CREATE TABLE %Q.'%q_segdir'("
173357 "level INTEGER,"
173358 "idx INTEGER,"
173359 "start_block INTEGER,"
173360 "leaves_end_block INTEGER,"
173361 "end_block INTEGER,"
173362 "root BLOB,"
173363 "PRIMARY KEY(level, idx)"
173364 ");",
173365 p->zDb, p->zName
173366 );
173367 if( p->bHasDocsize ){
173368 fts3DbExec(pRc: &rc, db,
173369 zFormat: "CREATE TABLE %Q.'%q_docsize'(docid INTEGER PRIMARY KEY, size BLOB);",
173370 p->zDb, p->zName
173371 );
173372 }
173373 assert( p->bHasStat==p->bFts4 );
173374 if( p->bHasStat ){
173375 sqlite3Fts3CreateStatTable(pRc: &rc, p);
173376 }
173377 return rc;
173378}
173379
173380/*
173381** Store the current database page-size in bytes in p->nPgsz.
173382**
173383** If *pRc is non-zero when this function is called, it is a no-op.
173384** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
173385** before returning.
173386*/
173387static void fts3DatabasePageSize(int *pRc, Fts3Table *p){
173388 if( *pRc==SQLITE_OK ){
173389 int rc; /* Return code */
173390 char *zSql; /* SQL text "PRAGMA %Q.page_size" */
173391 sqlite3_stmt *pStmt; /* Compiled "PRAGMA %Q.page_size" statement */
173392
173393 zSql = sqlite3_mprintf(zFormat: "PRAGMA %Q.page_size", p->zDb);
173394 if( !zSql ){
173395 rc = SQLITE_NOMEM;
173396 }else{
173397 rc = sqlite3_prepare(db: p->db, zSql, nBytes: -1, ppStmt: &pStmt, pzTail: 0);
173398 if( rc==SQLITE_OK ){
173399 sqlite3_step(pStmt);
173400 p->nPgsz = sqlite3_column_int(pStmt, i: 0);
173401 rc = sqlite3_finalize(pStmt);
173402 }else if( rc==SQLITE_AUTH ){
173403 p->nPgsz = 1024;
173404 rc = SQLITE_OK;
173405 }
173406 }
173407 assert( p->nPgsz>0 || rc!=SQLITE_OK );
173408 sqlite3_free(p: zSql);
173409 *pRc = rc;
173410 }
173411}
173412
173413/*
173414** "Special" FTS4 arguments are column specifications of the following form:
173415**
173416** <key> = <value>
173417**
173418** There may not be whitespace surrounding the "=" character. The <value>
173419** term may be quoted, but the <key> may not.
173420*/
173421static int fts3IsSpecialColumn(
173422 const char *z,
173423 int *pnKey,
173424 char **pzValue
173425){
173426 char *zValue;
173427 const char *zCsr = z;
173428
173429 while( *zCsr!='=' ){
173430 if( *zCsr=='\0' ) return 0;
173431 zCsr++;
173432 }
173433
173434 *pnKey = (int)(zCsr-z);
173435 zValue = sqlite3_mprintf(zFormat: "%s", &zCsr[1]);
173436 if( zValue ){
173437 sqlite3Fts3Dequote(z: zValue);
173438 }
173439 *pzValue = zValue;
173440 return 1;
173441}
173442
173443/*
173444** Append the output of a printf() style formatting to an existing string.
173445*/
173446static void fts3Appendf(
173447 int *pRc, /* IN/OUT: Error code */
173448 char **pz, /* IN/OUT: Pointer to string buffer */
173449 const char *zFormat, /* Printf format string to append */
173450 ... /* Arguments for printf format string */
173451){
173452 if( *pRc==SQLITE_OK ){
173453 va_list ap;
173454 char *z;
173455 va_start(ap, zFormat);
173456 z = sqlite3_vmprintf(zFormat, ap);
173457 va_end(ap);
173458 if( z && *pz ){
173459 char *z2 = sqlite3_mprintf(zFormat: "%s%s", *pz, z);
173460 sqlite3_free(p: z);
173461 z = z2;
173462 }
173463 if( z==0 ) *pRc = SQLITE_NOMEM;
173464 sqlite3_free(p: *pz);
173465 *pz = z;
173466 }
173467}
173468
173469/*
173470** Return a copy of input string zInput enclosed in double-quotes (") and
173471** with all double quote characters escaped. For example:
173472**
173473** fts3QuoteId("un \"zip\"") -> "un \"\"zip\"\""
173474**
173475** The pointer returned points to memory obtained from sqlite3_malloc(). It
173476** is the callers responsibility to call sqlite3_free() to release this
173477** memory.
173478*/
173479static char *fts3QuoteId(char const *zInput){
173480 sqlite3_int64 nRet;
173481 char *zRet;
173482 nRet = 2 + (int)strlen(s: zInput)*2 + 1;
173483 zRet = sqlite3_malloc64(n: nRet);
173484 if( zRet ){
173485 int i;
173486 char *z = zRet;
173487 *(z++) = '"';
173488 for(i=0; zInput[i]; i++){
173489 if( zInput[i]=='"' ) *(z++) = '"';
173490 *(z++) = zInput[i];
173491 }
173492 *(z++) = '"';
173493 *(z++) = '\0';
173494 }
173495 return zRet;
173496}
173497
173498/*
173499** Return a list of comma separated SQL expressions and a FROM clause that
173500** could be used in a SELECT statement such as the following:
173501**
173502** SELECT <list of expressions> FROM %_content AS x ...
173503**
173504** to return the docid, followed by each column of text data in order
173505** from left to write. If parameter zFunc is not NULL, then instead of
173506** being returned directly each column of text data is passed to an SQL
173507** function named zFunc first. For example, if zFunc is "unzip" and the
173508** table has the three user-defined columns "a", "b", and "c", the following
173509** string is returned:
173510**
173511** "docid, unzip(x.'a'), unzip(x.'b'), unzip(x.'c') FROM %_content AS x"
173512**
173513** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
173514** is the responsibility of the caller to eventually free it.
173515**
173516** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
173517** a NULL pointer is returned). Otherwise, if an OOM error is encountered
173518** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
173519** no error occurs, *pRc is left unmodified.
173520*/
173521static char *fts3ReadExprList(Fts3Table *p, const char *zFunc, int *pRc){
173522 char *zRet = 0;
173523 char *zFree = 0;
173524 char *zFunction;
173525 int i;
173526
173527 if( p->zContentTbl==0 ){
173528 if( !zFunc ){
173529 zFunction = "";
173530 }else{
173531 zFree = zFunction = fts3QuoteId(zInput: zFunc);
173532 }
173533 fts3Appendf(pRc, pz: &zRet, zFormat: "docid");
173534 for(i=0; i<p->nColumn; i++){
173535 fts3Appendf(pRc, pz: &zRet, zFormat: ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]);
173536 }
173537 if( p->zLanguageid ){
173538 fts3Appendf(pRc, pz: &zRet, zFormat: ", x.%Q", "langid");
173539 }
173540 sqlite3_free(p: zFree);
173541 }else{
173542 fts3Appendf(pRc, pz: &zRet, zFormat: "rowid");
173543 for(i=0; i<p->nColumn; i++){
173544 fts3Appendf(pRc, pz: &zRet, zFormat: ", x.'%q'", p->azColumn[i]);
173545 }
173546 if( p->zLanguageid ){
173547 fts3Appendf(pRc, pz: &zRet, zFormat: ", x.%Q", p->zLanguageid);
173548 }
173549 }
173550 fts3Appendf(pRc, pz: &zRet, zFormat: " FROM '%q'.'%q%s' AS x",
173551 p->zDb,
173552 (p->zContentTbl ? p->zContentTbl : p->zName),
173553 (p->zContentTbl ? "" : "_content")
173554 );
173555 return zRet;
173556}
173557
173558/*
173559** Return a list of N comma separated question marks, where N is the number
173560** of columns in the %_content table (one for the docid plus one for each
173561** user-defined text column).
173562**
173563** If argument zFunc is not NULL, then all but the first question mark
173564** is preceded by zFunc and an open bracket, and followed by a closed
173565** bracket. For example, if zFunc is "zip" and the FTS3 table has three
173566** user-defined text columns, the following string is returned:
173567**
173568** "?, zip(?), zip(?), zip(?)"
173569**
173570** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
173571** is the responsibility of the caller to eventually free it.
173572**
173573** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
173574** a NULL pointer is returned). Otherwise, if an OOM error is encountered
173575** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
173576** no error occurs, *pRc is left unmodified.
173577*/
173578static char *fts3WriteExprList(Fts3Table *p, const char *zFunc, int *pRc){
173579 char *zRet = 0;
173580 char *zFree = 0;
173581 char *zFunction;
173582 int i;
173583
173584 if( !zFunc ){
173585 zFunction = "";
173586 }else{
173587 zFree = zFunction = fts3QuoteId(zInput: zFunc);
173588 }
173589 fts3Appendf(pRc, pz: &zRet, zFormat: "?");
173590 for(i=0; i<p->nColumn; i++){
173591 fts3Appendf(pRc, pz: &zRet, zFormat: ",%s(?)", zFunction);
173592 }
173593 if( p->zLanguageid ){
173594 fts3Appendf(pRc, pz: &zRet, zFormat: ", ?");
173595 }
173596 sqlite3_free(p: zFree);
173597 return zRet;
173598}
173599
173600/*
173601** Buffer z contains a positive integer value encoded as utf-8 text.
173602** Decode this value and store it in *pnOut, returning the number of bytes
173603** consumed. If an overflow error occurs return a negative value.
173604*/
173605SQLITE_PRIVATE int sqlite3Fts3ReadInt(const char *z, int *pnOut){
173606 u64 iVal = 0;
173607 int i;
173608 for(i=0; z[i]>='0' && z[i]<='9'; i++){
173609 iVal = iVal*10 + (z[i] - '0');
173610 if( iVal>0x7FFFFFFF ) return -1;
173611 }
173612 *pnOut = (int)iVal;
173613 return i;
173614}
173615
173616/*
173617** This function interprets the string at (*pp) as a non-negative integer
173618** value. It reads the integer and sets *pnOut to the value read, then
173619** sets *pp to point to the byte immediately following the last byte of
173620** the integer value.
173621**
173622** Only decimal digits ('0'..'9') may be part of an integer value.
173623**
173624** If *pp does not being with a decimal digit SQLITE_ERROR is returned and
173625** the output value undefined. Otherwise SQLITE_OK is returned.
173626**
173627** This function is used when parsing the "prefix=" FTS4 parameter.
173628*/
173629static int fts3GobbleInt(const char **pp, int *pnOut){
173630 const int MAX_NPREFIX = 10000000;
173631 int nInt = 0; /* Output value */
173632 int nByte;
173633 nByte = sqlite3Fts3ReadInt(z: *pp, pnOut: &nInt);
173634 if( nInt>MAX_NPREFIX ){
173635 nInt = 0;
173636 }
173637 if( nByte==0 ){
173638 return SQLITE_ERROR;
173639 }
173640 *pnOut = nInt;
173641 *pp += nByte;
173642 return SQLITE_OK;
173643}
173644
173645/*
173646** This function is called to allocate an array of Fts3Index structures
173647** representing the indexes maintained by the current FTS table. FTS tables
173648** always maintain the main "terms" index, but may also maintain one or
173649** more "prefix" indexes, depending on the value of the "prefix=" parameter
173650** (if any) specified as part of the CREATE VIRTUAL TABLE statement.
173651**
173652** Argument zParam is passed the value of the "prefix=" option if one was
173653** specified, or NULL otherwise.
173654**
173655** If no error occurs, SQLITE_OK is returned and *apIndex set to point to
173656** the allocated array. *pnIndex is set to the number of elements in the
173657** array. If an error does occur, an SQLite error code is returned.
173658**
173659** Regardless of whether or not an error is returned, it is the responsibility
173660** of the caller to call sqlite3_free() on the output array to free it.
173661*/
173662static int fts3PrefixParameter(
173663 const char *zParam, /* ABC in prefix=ABC parameter to parse */
173664 int *pnIndex, /* OUT: size of *apIndex[] array */
173665 struct Fts3Index **apIndex /* OUT: Array of indexes for this table */
173666){
173667 struct Fts3Index *aIndex; /* Allocated array */
173668 int nIndex = 1; /* Number of entries in array */
173669
173670 if( zParam && zParam[0] ){
173671 const char *p;
173672 nIndex++;
173673 for(p=zParam; *p; p++){
173674 if( *p==',' ) nIndex++;
173675 }
173676 }
173677
173678 aIndex = sqlite3_malloc64(n: sizeof(struct Fts3Index) * nIndex);
173679 *apIndex = aIndex;
173680 if( !aIndex ){
173681 return SQLITE_NOMEM;
173682 }
173683
173684 memset(s: aIndex, c: 0, n: sizeof(struct Fts3Index) * nIndex);
173685 if( zParam ){
173686 const char *p = zParam;
173687 int i;
173688 for(i=1; i<nIndex; i++){
173689 int nPrefix = 0;
173690 if( fts3GobbleInt(pp: &p, pnOut: &nPrefix) ) return SQLITE_ERROR;
173691 assert( nPrefix>=0 );
173692 if( nPrefix==0 ){
173693 nIndex--;
173694 i--;
173695 }else{
173696 aIndex[i].nPrefix = nPrefix;
173697 }
173698 p++;
173699 }
173700 }
173701
173702 *pnIndex = nIndex;
173703 return SQLITE_OK;
173704}
173705
173706/*
173707** This function is called when initializing an FTS4 table that uses the
173708** content=xxx option. It determines the number of and names of the columns
173709** of the new FTS4 table.
173710**
173711** The third argument passed to this function is the value passed to the
173712** config=xxx option (i.e. "xxx"). This function queries the database for
173713** a table of that name. If found, the output variables are populated
173714** as follows:
173715**
173716** *pnCol: Set to the number of columns table xxx has,
173717**
173718** *pnStr: Set to the total amount of space required to store a copy
173719** of each columns name, including the nul-terminator.
173720**
173721** *pazCol: Set to point to an array of *pnCol strings. Each string is
173722** the name of the corresponding column in table xxx. The array
173723** and its contents are allocated using a single allocation. It
173724** is the responsibility of the caller to free this allocation
173725** by eventually passing the *pazCol value to sqlite3_free().
173726**
173727** If the table cannot be found, an error code is returned and the output
173728** variables are undefined. Or, if an OOM is encountered, SQLITE_NOMEM is
173729** returned (and the output variables are undefined).
173730*/
173731static int fts3ContentColumns(
173732 sqlite3 *db, /* Database handle */
173733 const char *zDb, /* Name of db (i.e. "main", "temp" etc.) */
173734 const char *zTbl, /* Name of content table */
173735 const char ***pazCol, /* OUT: Malloc'd array of column names */
173736 int *pnCol, /* OUT: Size of array *pazCol */
173737 int *pnStr, /* OUT: Bytes of string content */
173738 char **pzErr /* OUT: error message */
173739){
173740 int rc = SQLITE_OK; /* Return code */
173741 char *zSql; /* "SELECT *" statement on zTbl */
173742 sqlite3_stmt *pStmt = 0; /* Compiled version of zSql */
173743
173744 zSql = sqlite3_mprintf(zFormat: "SELECT * FROM %Q.%Q", zDb, zTbl);
173745 if( !zSql ){
173746 rc = SQLITE_NOMEM;
173747 }else{
173748 rc = sqlite3_prepare(db, zSql, nBytes: -1, ppStmt: &pStmt, pzTail: 0);
173749 if( rc!=SQLITE_OK ){
173750 sqlite3Fts3ErrMsg(pzErr, zFormat: "%s", sqlite3_errmsg(db));
173751 }
173752 }
173753 sqlite3_free(p: zSql);
173754
173755 if( rc==SQLITE_OK ){
173756 const char **azCol; /* Output array */
173757 sqlite3_int64 nStr = 0; /* Size of all column names (incl. 0x00) */
173758 int nCol; /* Number of table columns */
173759 int i; /* Used to iterate through columns */
173760
173761 /* Loop through the returned columns. Set nStr to the number of bytes of
173762 ** space required to store a copy of each column name, including the
173763 ** nul-terminator byte. */
173764 nCol = sqlite3_column_count(pStmt);
173765 for(i=0; i<nCol; i++){
173766 const char *zCol = sqlite3_column_name(pStmt, N: i);
173767 nStr += strlen(s: zCol) + 1;
173768 }
173769
173770 /* Allocate and populate the array to return. */
173771 azCol = (const char **)sqlite3_malloc64(n: sizeof(char *) * nCol + nStr);
173772 if( azCol==0 ){
173773 rc = SQLITE_NOMEM;
173774 }else{
173775 char *p = (char *)&azCol[nCol];
173776 for(i=0; i<nCol; i++){
173777 const char *zCol = sqlite3_column_name(pStmt, N: i);
173778 int n = (int)strlen(s: zCol)+1;
173779 memcpy(dest: p, src: zCol, n: n);
173780 azCol[i] = p;
173781 p += n;
173782 }
173783 }
173784 sqlite3_finalize(pStmt);
173785
173786 /* Set the output variables. */
173787 *pnCol = nCol;
173788 *pnStr = nStr;
173789 *pazCol = azCol;
173790 }
173791
173792 return rc;
173793}
173794
173795/*
173796** This function is the implementation of both the xConnect and xCreate
173797** methods of the FTS3 virtual table.
173798**
173799** The argv[] array contains the following:
173800**
173801** argv[0] -> module name ("fts3" or "fts4")
173802** argv[1] -> database name
173803** argv[2] -> table name
173804** argv[...] -> "column name" and other module argument fields.
173805*/
173806static int fts3InitVtab(
173807 int isCreate, /* True for xCreate, false for xConnect */
173808 sqlite3 *db, /* The SQLite database connection */
173809 void *pAux, /* Hash table containing tokenizers */
173810 int argc, /* Number of elements in argv array */
173811 const char * const *argv, /* xCreate/xConnect argument array */
173812 sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
173813 char **pzErr /* Write any error message here */
173814){
173815 Fts3Hash *pHash = (Fts3Hash *)pAux;
173816 Fts3Table *p = 0; /* Pointer to allocated vtab */
173817 int rc = SQLITE_OK; /* Return code */
173818 int i; /* Iterator variable */
173819 sqlite3_int64 nByte; /* Size of allocation used for *p */
173820 int iCol; /* Column index */
173821 int nString = 0; /* Bytes required to hold all column names */
173822 int nCol = 0; /* Number of columns in the FTS table */
173823 char *zCsr; /* Space for holding column names */
173824 int nDb; /* Bytes required to hold database name */
173825 int nName; /* Bytes required to hold table name */
173826 int isFts4 = (argv[0][3]=='4'); /* True for FTS4, false for FTS3 */
173827 const char **aCol; /* Array of column names */
173828 sqlite3_tokenizer *pTokenizer = 0; /* Tokenizer for this table */
173829
173830 int nIndex = 0; /* Size of aIndex[] array */
173831 struct Fts3Index *aIndex = 0; /* Array of indexes for this table */
173832
173833 /* The results of parsing supported FTS4 key=value options: */
173834 int bNoDocsize = 0; /* True to omit %_docsize table */
173835 int bDescIdx = 0; /* True to store descending indexes */
173836 char *zPrefix = 0; /* Prefix parameter value (or NULL) */
173837 char *zCompress = 0; /* compress=? parameter (or NULL) */
173838 char *zUncompress = 0; /* uncompress=? parameter (or NULL) */
173839 char *zContent = 0; /* content=? parameter (or NULL) */
173840 char *zLanguageid = 0; /* languageid=? parameter (or NULL) */
173841 char **azNotindexed = 0; /* The set of notindexed= columns */
173842 int nNotindexed = 0; /* Size of azNotindexed[] array */
173843
173844 assert( strlen(argv[0])==4 );
173845 assert( (sqlite3_strnicmp(argv[0], "fts4", 4)==0 && isFts4)
173846 || (sqlite3_strnicmp(argv[0], "fts3", 4)==0 && !isFts4)
173847 );
173848
173849 nDb = (int)strlen(s: argv[1]) + 1;
173850 nName = (int)strlen(s: argv[2]) + 1;
173851
173852 nByte = sizeof(const char *) * (argc-2);
173853 aCol = (const char **)sqlite3_malloc64(n: nByte);
173854 if( aCol ){
173855 memset(s: (void*)aCol, c: 0, n: nByte);
173856 azNotindexed = (char **)sqlite3_malloc64(n: nByte);
173857 }
173858 if( azNotindexed ){
173859 memset(s: azNotindexed, c: 0, n: nByte);
173860 }
173861 if( !aCol || !azNotindexed ){
173862 rc = SQLITE_NOMEM;
173863 goto fts3_init_out;
173864 }
173865
173866 /* Loop through all of the arguments passed by the user to the FTS3/4
173867 ** module (i.e. all the column names and special arguments). This loop
173868 ** does the following:
173869 **
173870 ** + Figures out the number of columns the FTSX table will have, and
173871 ** the number of bytes of space that must be allocated to store copies
173872 ** of the column names.
173873 **
173874 ** + If there is a tokenizer specification included in the arguments,
173875 ** initializes the tokenizer pTokenizer.
173876 */
173877 for(i=3; rc==SQLITE_OK && i<argc; i++){
173878 char const *z = argv[i];
173879 int nKey;
173880 char *zVal;
173881
173882 /* Check if this is a tokenizer specification */
173883 if( !pTokenizer
173884 && strlen(s: z)>8
173885 && 0==sqlite3_strnicmp(zLeft: z, zRight: "tokenize", N: 8)
173886 && 0==sqlite3Fts3IsIdChar(z[8])
173887 ){
173888 rc = sqlite3Fts3InitTokenizer(pHash, &z[9], &pTokenizer, pzErr);
173889 }
173890
173891 /* Check if it is an FTS4 special argument. */
173892 else if( isFts4 && fts3IsSpecialColumn(z, pnKey: &nKey, pzValue: &zVal) ){
173893 struct Fts4Option {
173894 const char *zOpt;
173895 int nOpt;
173896 } aFts4Opt[] = {
173897 { "matchinfo", 9 }, /* 0 -> MATCHINFO */
173898 { "prefix", 6 }, /* 1 -> PREFIX */
173899 { "compress", 8 }, /* 2 -> COMPRESS */
173900 { "uncompress", 10 }, /* 3 -> UNCOMPRESS */
173901 { "order", 5 }, /* 4 -> ORDER */
173902 { "content", 7 }, /* 5 -> CONTENT */
173903 { "languageid", 10 }, /* 6 -> LANGUAGEID */
173904 { "notindexed", 10 } /* 7 -> NOTINDEXED */
173905 };
173906
173907 int iOpt;
173908 if( !zVal ){
173909 rc = SQLITE_NOMEM;
173910 }else{
173911 for(iOpt=0; iOpt<SizeofArray(aFts4Opt); iOpt++){
173912 struct Fts4Option *pOp = &aFts4Opt[iOpt];
173913 if( nKey==pOp->nOpt && !sqlite3_strnicmp(zLeft: z, zRight: pOp->zOpt, N: pOp->nOpt) ){
173914 break;
173915 }
173916 }
173917 switch( iOpt ){
173918 case 0: /* MATCHINFO */
173919 if( strlen(s: zVal)!=4 || sqlite3_strnicmp(zLeft: zVal, zRight: "fts3", N: 4) ){
173920 sqlite3Fts3ErrMsg(pzErr, zFormat: "unrecognized matchinfo: %s", zVal);
173921 rc = SQLITE_ERROR;
173922 }
173923 bNoDocsize = 1;
173924 break;
173925
173926 case 1: /* PREFIX */
173927 sqlite3_free(p: zPrefix);
173928 zPrefix = zVal;
173929 zVal = 0;
173930 break;
173931
173932 case 2: /* COMPRESS */
173933 sqlite3_free(p: zCompress);
173934 zCompress = zVal;
173935 zVal = 0;
173936 break;
173937
173938 case 3: /* UNCOMPRESS */
173939 sqlite3_free(p: zUncompress);
173940 zUncompress = zVal;
173941 zVal = 0;
173942 break;
173943
173944 case 4: /* ORDER */
173945 if( (strlen(s: zVal)!=3 || sqlite3_strnicmp(zLeft: zVal, zRight: "asc", N: 3))
173946 && (strlen(s: zVal)!=4 || sqlite3_strnicmp(zLeft: zVal, zRight: "desc", N: 4))
173947 ){
173948 sqlite3Fts3ErrMsg(pzErr, zFormat: "unrecognized order: %s", zVal);
173949 rc = SQLITE_ERROR;
173950 }
173951 bDescIdx = (zVal[0]=='d' || zVal[0]=='D');
173952 break;
173953
173954 case 5: /* CONTENT */
173955 sqlite3_free(p: zContent);
173956 zContent = zVal;
173957 zVal = 0;
173958 break;
173959
173960 case 6: /* LANGUAGEID */
173961 assert( iOpt==6 );
173962 sqlite3_free(p: zLanguageid);
173963 zLanguageid = zVal;
173964 zVal = 0;
173965 break;
173966
173967 case 7: /* NOTINDEXED */
173968 azNotindexed[nNotindexed++] = zVal;
173969 zVal = 0;
173970 break;
173971
173972 default:
173973 assert( iOpt==SizeofArray(aFts4Opt) );
173974 sqlite3Fts3ErrMsg(pzErr, zFormat: "unrecognized parameter: %s", z);
173975 rc = SQLITE_ERROR;
173976 break;
173977 }
173978 sqlite3_free(p: zVal);
173979 }
173980 }
173981
173982 /* Otherwise, the argument is a column name. */
173983 else {
173984 nString += (int)(strlen(s: z) + 1);
173985 aCol[nCol++] = z;
173986 }
173987 }
173988
173989 /* If a content=xxx option was specified, the following:
173990 **
173991 ** 1. Ignore any compress= and uncompress= options.
173992 **
173993 ** 2. If no column names were specified as part of the CREATE VIRTUAL
173994 ** TABLE statement, use all columns from the content table.
173995 */
173996 if( rc==SQLITE_OK && zContent ){
173997 sqlite3_free(p: zCompress);
173998 sqlite3_free(p: zUncompress);
173999 zCompress = 0;
174000 zUncompress = 0;
174001 if( nCol==0 ){
174002 sqlite3_free(p: (void*)aCol);
174003 aCol = 0;
174004 rc = fts3ContentColumns(db, zDb: argv[1], zTbl: zContent,pazCol: &aCol,pnCol: &nCol,pnStr: &nString,pzErr);
174005
174006 /* If a languageid= option was specified, remove the language id
174007 ** column from the aCol[] array. */
174008 if( rc==SQLITE_OK && zLanguageid ){
174009 int j;
174010 for(j=0; j<nCol; j++){
174011 if( sqlite3_stricmp(zLeft: zLanguageid, zRight: aCol[j])==0 ){
174012 int k;
174013 for(k=j; k<nCol; k++) aCol[k] = aCol[k+1];
174014 nCol--;
174015 break;
174016 }
174017 }
174018 }
174019 }
174020 }
174021 if( rc!=SQLITE_OK ) goto fts3_init_out;
174022
174023 if( nCol==0 ){
174024 assert( nString==0 );
174025 aCol[0] = "content";
174026 nString = 8;
174027 nCol = 1;
174028 }
174029
174030 if( pTokenizer==0 ){
174031 rc = sqlite3Fts3InitTokenizer(pHash, "simple", &pTokenizer, pzErr);
174032 if( rc!=SQLITE_OK ) goto fts3_init_out;
174033 }
174034 assert( pTokenizer );
174035
174036 rc = fts3PrefixParameter(zParam: zPrefix, pnIndex: &nIndex, apIndex: &aIndex);
174037 if( rc==SQLITE_ERROR ){
174038 assert( zPrefix );
174039 sqlite3Fts3ErrMsg(pzErr, zFormat: "error parsing prefix parameter: %s", zPrefix);
174040 }
174041 if( rc!=SQLITE_OK ) goto fts3_init_out;
174042
174043 /* Allocate and populate the Fts3Table structure. */
174044 nByte = sizeof(Fts3Table) + /* Fts3Table */
174045 nCol * sizeof(char *) + /* azColumn */
174046 nIndex * sizeof(struct Fts3Index) + /* aIndex */
174047 nCol * sizeof(u8) + /* abNotindexed */
174048 nName + /* zName */
174049 nDb + /* zDb */
174050 nString; /* Space for azColumn strings */
174051 p = (Fts3Table*)sqlite3_malloc64(n: nByte);
174052 if( p==0 ){
174053 rc = SQLITE_NOMEM;
174054 goto fts3_init_out;
174055 }
174056 memset(s: p, c: 0, n: nByte);
174057 p->db = db;
174058 p->nColumn = nCol;
174059 p->nPendingData = 0;
174060 p->azColumn = (char **)&p[1];
174061 p->pTokenizer = pTokenizer;
174062 p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
174063 p->bHasDocsize = (isFts4 && bNoDocsize==0);
174064 p->bHasStat = (u8)isFts4;
174065 p->bFts4 = (u8)isFts4;
174066 p->bDescIdx = (u8)bDescIdx;
174067 p->nAutoincrmerge = 0xff; /* 0xff means setting unknown */
174068 p->zContentTbl = zContent;
174069 p->zLanguageid = zLanguageid;
174070 zContent = 0;
174071 zLanguageid = 0;
174072 TESTONLY( p->inTransaction = -1 );
174073 TESTONLY( p->mxSavepoint = -1 );
174074
174075 p->aIndex = (struct Fts3Index *)&p->azColumn[nCol];
174076 memcpy(dest: p->aIndex, src: aIndex, n: sizeof(struct Fts3Index) * nIndex);
174077 p->nIndex = nIndex;
174078 for(i=0; i<nIndex; i++){
174079 fts3HashInit(pNew: &p->aIndex[i].hPending, FTS3_HASH_STRING, copyKey: 1);
174080 }
174081 p->abNotindexed = (u8 *)&p->aIndex[nIndex];
174082
174083 /* Fill in the zName and zDb fields of the vtab structure. */
174084 zCsr = (char *)&p->abNotindexed[nCol];
174085 p->zName = zCsr;
174086 memcpy(dest: zCsr, src: argv[2], n: nName);
174087 zCsr += nName;
174088 p->zDb = zCsr;
174089 memcpy(dest: zCsr, src: argv[1], n: nDb);
174090 zCsr += nDb;
174091
174092 /* Fill in the azColumn array */
174093 for(iCol=0; iCol<nCol; iCol++){
174094 char *z;
174095 int n = 0;
174096 z = (char *)sqlite3Fts3NextToken(aCol[iCol], &n);
174097 if( n>0 ){
174098 memcpy(dest: zCsr, src: z, n: n);
174099 }
174100 zCsr[n] = '\0';
174101 sqlite3Fts3Dequote(z: zCsr);
174102 p->azColumn[iCol] = zCsr;
174103 zCsr += n+1;
174104 assert( zCsr <= &((char *)p)[nByte] );
174105 }
174106
174107 /* Fill in the abNotindexed array */
174108 for(iCol=0; iCol<nCol; iCol++){
174109 int n = (int)strlen(s: p->azColumn[iCol]);
174110 for(i=0; i<nNotindexed; i++){
174111 char *zNot = azNotindexed[i];
174112 if( zNot && n==(int)strlen(s: zNot)
174113 && 0==sqlite3_strnicmp(zLeft: p->azColumn[iCol], zRight: zNot, N: n)
174114 ){
174115 p->abNotindexed[iCol] = 1;
174116 sqlite3_free(p: zNot);
174117 azNotindexed[i] = 0;
174118 }
174119 }
174120 }
174121 for(i=0; i<nNotindexed; i++){
174122 if( azNotindexed[i] ){
174123 sqlite3Fts3ErrMsg(pzErr, zFormat: "no such column: %s", azNotindexed[i]);
174124 rc = SQLITE_ERROR;
174125 }
174126 }
174127
174128 if( rc==SQLITE_OK && (zCompress==0)!=(zUncompress==0) ){
174129 char const *zMiss = (zCompress==0 ? "compress" : "uncompress");
174130 rc = SQLITE_ERROR;
174131 sqlite3Fts3ErrMsg(pzErr, zFormat: "missing %s parameter in fts4 constructor", zMiss);
174132 }
174133 p->zReadExprlist = fts3ReadExprList(p, zFunc: zUncompress, pRc: &rc);
174134 p->zWriteExprlist = fts3WriteExprList(p, zFunc: zCompress, pRc: &rc);
174135 if( rc!=SQLITE_OK ) goto fts3_init_out;
174136
174137 /* If this is an xCreate call, create the underlying tables in the
174138 ** database. TODO: For xConnect(), it could verify that said tables exist.
174139 */
174140 if( isCreate ){
174141 rc = fts3CreateTables(p);
174142 }
174143
174144 /* Check to see if a legacy fts3 table has been "upgraded" by the
174145 ** addition of a %_stat table so that it can use incremental merge.
174146 */
174147 if( !isFts4 && !isCreate ){
174148 p->bHasStat = 2;
174149 }
174150
174151 /* Figure out the page-size for the database. This is required in order to
174152 ** estimate the cost of loading large doclists from the database. */
174153 fts3DatabasePageSize(pRc: &rc, p);
174154 p->nNodeSize = p->nPgsz-35;
174155
174156#if defined(SQLITE_DEBUG)||defined(SQLITE_TEST)
174157 p->nMergeCount = FTS3_MERGE_COUNT;
174158#endif
174159
174160 /* Declare the table schema to SQLite. */
174161 fts3DeclareVtab(pRc: &rc, p);
174162
174163fts3_init_out:
174164 sqlite3_free(p: zPrefix);
174165 sqlite3_free(p: aIndex);
174166 sqlite3_free(p: zCompress);
174167 sqlite3_free(p: zUncompress);
174168 sqlite3_free(p: zContent);
174169 sqlite3_free(p: zLanguageid);
174170 for(i=0; i<nNotindexed; i++) sqlite3_free(p: azNotindexed[i]);
174171 sqlite3_free(p: (void *)aCol);
174172 sqlite3_free(p: (void *)azNotindexed);
174173 if( rc!=SQLITE_OK ){
174174 if( p ){
174175 fts3DisconnectMethod(pVtab: (sqlite3_vtab *)p);
174176 }else if( pTokenizer ){
174177 pTokenizer->pModule->xDestroy(pTokenizer);
174178 }
174179 }else{
174180 assert( p->pSegments==0 );
174181 *ppVTab = &p->base;
174182 }
174183 return rc;
174184}
174185
174186/*
174187** The xConnect() and xCreate() methods for the virtual table. All the
174188** work is done in function fts3InitVtab().
174189*/
174190static int fts3ConnectMethod(
174191 sqlite3 *db, /* Database connection */
174192 void *pAux, /* Pointer to tokenizer hash table */
174193 int argc, /* Number of elements in argv array */
174194 const char * const *argv, /* xCreate/xConnect argument array */
174195 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
174196 char **pzErr /* OUT: sqlite3_malloc'd error message */
174197){
174198 return fts3InitVtab(isCreate: 0, db, pAux, argc, argv, ppVTab: ppVtab, pzErr);
174199}
174200static int fts3CreateMethod(
174201 sqlite3 *db, /* Database connection */
174202 void *pAux, /* Pointer to tokenizer hash table */
174203 int argc, /* Number of elements in argv array */
174204 const char * const *argv, /* xCreate/xConnect argument array */
174205 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
174206 char **pzErr /* OUT: sqlite3_malloc'd error message */
174207){
174208 return fts3InitVtab(isCreate: 1, db, pAux, argc, argv, ppVTab: ppVtab, pzErr);
174209}
174210
174211/*
174212** Set the pIdxInfo->estimatedRows variable to nRow. Unless this
174213** extension is currently being used by a version of SQLite too old to
174214** support estimatedRows. In that case this function is a no-op.
174215*/
174216static void fts3SetEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){
174217#if SQLITE_VERSION_NUMBER>=3008002
174218 if( sqlite3_libversion_number()>=3008002 ){
174219 pIdxInfo->estimatedRows = nRow;
174220 }
174221#endif
174222}
174223
174224/*
174225** Set the SQLITE_INDEX_SCAN_UNIQUE flag in pIdxInfo->flags. Unless this
174226** extension is currently being used by a version of SQLite too old to
174227** support index-info flags. In that case this function is a no-op.
174228*/
174229static void fts3SetUniqueFlag(sqlite3_index_info *pIdxInfo){
174230#if SQLITE_VERSION_NUMBER>=3008012
174231 if( sqlite3_libversion_number()>=3008012 ){
174232 pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_UNIQUE;
174233 }
174234#endif
174235}
174236
174237/*
174238** Implementation of the xBestIndex method for FTS3 tables. There
174239** are three possible strategies, in order of preference:
174240**
174241** 1. Direct lookup by rowid or docid.
174242** 2. Full-text search using a MATCH operator on a non-docid column.
174243** 3. Linear scan of %_content table.
174244*/
174245static int fts3BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
174246 Fts3Table *p = (Fts3Table *)pVTab;
174247 int i; /* Iterator variable */
174248 int iCons = -1; /* Index of constraint to use */
174249
174250 int iLangidCons = -1; /* Index of langid=x constraint, if present */
174251 int iDocidGe = -1; /* Index of docid>=x constraint, if present */
174252 int iDocidLe = -1; /* Index of docid<=x constraint, if present */
174253 int iIdx;
174254
174255 if( p->bLock ){
174256 return SQLITE_ERROR;
174257 }
174258
174259 /* By default use a full table scan. This is an expensive option,
174260 ** so search through the constraints to see if a more efficient
174261 ** strategy is possible.
174262 */
174263 pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
174264 pInfo->estimatedCost = 5000000;
174265 for(i=0; i<pInfo->nConstraint; i++){
174266 int bDocid; /* True if this constraint is on docid */
174267 struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i];
174268 if( pCons->usable==0 ){
174269 if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH ){
174270 /* There exists an unusable MATCH constraint. This means that if
174271 ** the planner does elect to use the results of this call as part
174272 ** of the overall query plan the user will see an "unable to use
174273 ** function MATCH in the requested context" error. To discourage
174274 ** this, return a very high cost here. */
174275 pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
174276 pInfo->estimatedCost = 1e50;
174277 fts3SetEstimatedRows(pIdxInfo: pInfo, nRow: ((sqlite3_int64)1) << 50);
174278 return SQLITE_OK;
174279 }
174280 continue;
174281 }
174282
174283 bDocid = (pCons->iColumn<0 || pCons->iColumn==p->nColumn+1);
174284
174285 /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */
174286 if( iCons<0 && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ && bDocid ){
174287 pInfo->idxNum = FTS3_DOCID_SEARCH;
174288 pInfo->estimatedCost = 1.0;
174289 iCons = i;
174290 }
174291
174292 /* A MATCH constraint. Use a full-text search.
174293 **
174294 ** If there is more than one MATCH constraint available, use the first
174295 ** one encountered. If there is both a MATCH constraint and a direct
174296 ** rowid/docid lookup, prefer the MATCH strategy. This is done even
174297 ** though the rowid/docid lookup is faster than a MATCH query, selecting
174298 ** it would lead to an "unable to use function MATCH in the requested
174299 ** context" error.
174300 */
174301 if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH
174302 && pCons->iColumn>=0 && pCons->iColumn<=p->nColumn
174303 ){
174304 pInfo->idxNum = FTS3_FULLTEXT_SEARCH + pCons->iColumn;
174305 pInfo->estimatedCost = 2.0;
174306 iCons = i;
174307 }
174308
174309 /* Equality constraint on the langid column */
174310 if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ
174311 && pCons->iColumn==p->nColumn + 2
174312 ){
174313 iLangidCons = i;
174314 }
174315
174316 if( bDocid ){
174317 switch( pCons->op ){
174318 case SQLITE_INDEX_CONSTRAINT_GE:
174319 case SQLITE_INDEX_CONSTRAINT_GT:
174320 iDocidGe = i;
174321 break;
174322
174323 case SQLITE_INDEX_CONSTRAINT_LE:
174324 case SQLITE_INDEX_CONSTRAINT_LT:
174325 iDocidLe = i;
174326 break;
174327 }
174328 }
174329 }
174330
174331 /* If using a docid=? or rowid=? strategy, set the UNIQUE flag. */
174332 if( pInfo->idxNum==FTS3_DOCID_SEARCH ) fts3SetUniqueFlag(pIdxInfo: pInfo);
174333
174334 iIdx = 1;
174335 if( iCons>=0 ){
174336 pInfo->aConstraintUsage[iCons].argvIndex = iIdx++;
174337 pInfo->aConstraintUsage[iCons].omit = 1;
174338 }
174339 if( iLangidCons>=0 ){
174340 pInfo->idxNum |= FTS3_HAVE_LANGID;
174341 pInfo->aConstraintUsage[iLangidCons].argvIndex = iIdx++;
174342 }
174343 if( iDocidGe>=0 ){
174344 pInfo->idxNum |= FTS3_HAVE_DOCID_GE;
174345 pInfo->aConstraintUsage[iDocidGe].argvIndex = iIdx++;
174346 }
174347 if( iDocidLe>=0 ){
174348 pInfo->idxNum |= FTS3_HAVE_DOCID_LE;
174349 pInfo->aConstraintUsage[iDocidLe].argvIndex = iIdx++;
174350 }
174351
174352 /* Regardless of the strategy selected, FTS can deliver rows in rowid (or
174353 ** docid) order. Both ascending and descending are possible.
174354 */
174355 if( pInfo->nOrderBy==1 ){
174356 struct sqlite3_index_orderby *pOrder = &pInfo->aOrderBy[0];
174357 if( pOrder->iColumn<0 || pOrder->iColumn==p->nColumn+1 ){
174358 if( pOrder->desc ){
174359 pInfo->idxStr = "DESC";
174360 }else{
174361 pInfo->idxStr = "ASC";
174362 }
174363 pInfo->orderByConsumed = 1;
174364 }
174365 }
174366
174367 assert( p->pSegments==0 );
174368 return SQLITE_OK;
174369}
174370
174371/*
174372** Implementation of xOpen method.
174373*/
174374static int fts3OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
174375 sqlite3_vtab_cursor *pCsr; /* Allocated cursor */
174376
174377 UNUSED_PARAMETER(pVTab);
174378
174379 /* Allocate a buffer large enough for an Fts3Cursor structure. If the
174380 ** allocation succeeds, zero it and return SQLITE_OK. Otherwise,
174381 ** if the allocation fails, return SQLITE_NOMEM.
174382 */
174383 *ppCsr = pCsr = (sqlite3_vtab_cursor *)sqlite3_malloc(n: sizeof(Fts3Cursor));
174384 if( !pCsr ){
174385 return SQLITE_NOMEM;
174386 }
174387 memset(s: pCsr, c: 0, n: sizeof(Fts3Cursor));
174388 return SQLITE_OK;
174389}
174390
174391/*
174392** Finalize the statement handle at pCsr->pStmt.
174393**
174394** Or, if that statement handle is one created by fts3CursorSeekStmt(),
174395** and the Fts3Table.pSeekStmt slot is currently NULL, save the statement
174396** pointer there instead of finalizing it.
174397*/
174398static void fts3CursorFinalizeStmt(Fts3Cursor *pCsr){
174399 if( pCsr->bSeekStmt ){
174400 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
174401 if( p->pSeekStmt==0 ){
174402 p->pSeekStmt = pCsr->pStmt;
174403 sqlite3_reset(pStmt: pCsr->pStmt);
174404 pCsr->pStmt = 0;
174405 }
174406 pCsr->bSeekStmt = 0;
174407 }
174408 sqlite3_finalize(pStmt: pCsr->pStmt);
174409}
174410
174411/*
174412** Free all resources currently held by the cursor passed as the only
174413** argument.
174414*/
174415static void fts3ClearCursor(Fts3Cursor *pCsr){
174416 fts3CursorFinalizeStmt(pCsr);
174417 sqlite3Fts3FreeDeferredTokens(pCsr);
174418 sqlite3_free(p: pCsr->aDoclist);
174419 sqlite3Fts3MIBufferFree(p: pCsr->pMIBuffer);
174420 sqlite3Fts3ExprFree(pCsr->pExpr);
174421 memset(s: &(&pCsr->base)[1], c: 0, n: sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
174422}
174423
174424/*
174425** Close the cursor. For additional information see the documentation
174426** on the xClose method of the virtual table interface.
174427*/
174428static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
174429 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
174430 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
174431 fts3ClearCursor(pCsr);
174432 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
174433 sqlite3_free(p: pCsr);
174434 return SQLITE_OK;
174435}
174436
174437/*
174438** If pCsr->pStmt has not been prepared (i.e. if pCsr->pStmt==0), then
174439** compose and prepare an SQL statement of the form:
174440**
174441** "SELECT <columns> FROM %_content WHERE rowid = ?"
174442**
174443** (or the equivalent for a content=xxx table) and set pCsr->pStmt to
174444** it. If an error occurs, return an SQLite error code.
174445*/
174446static int fts3CursorSeekStmt(Fts3Cursor *pCsr){
174447 int rc = SQLITE_OK;
174448 if( pCsr->pStmt==0 ){
174449 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
174450 char *zSql;
174451 if( p->pSeekStmt ){
174452 pCsr->pStmt = p->pSeekStmt;
174453 p->pSeekStmt = 0;
174454 }else{
174455 zSql = sqlite3_mprintf(zFormat: "SELECT %s WHERE rowid = ?", p->zReadExprlist);
174456 if( !zSql ) return SQLITE_NOMEM;
174457 p->bLock++;
174458 rc = sqlite3_prepare_v3(
174459 db: p->db, zSql,nBytes: -1,SQLITE_PREPARE_PERSISTENT,ppStmt: &pCsr->pStmt,pzTail: 0
174460 );
174461 p->bLock--;
174462 sqlite3_free(p: zSql);
174463 }
174464 if( rc==SQLITE_OK ) pCsr->bSeekStmt = 1;
174465 }
174466 return rc;
174467}
174468
174469/*
174470** Position the pCsr->pStmt statement so that it is on the row
174471** of the %_content table that contains the last match. Return
174472** SQLITE_OK on success.
174473*/
174474static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
174475 int rc = SQLITE_OK;
174476 if( pCsr->isRequireSeek ){
174477 rc = fts3CursorSeekStmt(pCsr);
174478 if( rc==SQLITE_OK ){
174479 Fts3Table *pTab = (Fts3Table*)pCsr->base.pVtab;
174480 pTab->bLock++;
174481 sqlite3_bind_int64(pStmt: pCsr->pStmt, i: 1, iValue: pCsr->iPrevId);
174482 pCsr->isRequireSeek = 0;
174483 if( SQLITE_ROW==sqlite3_step(pStmt: pCsr->pStmt) ){
174484 pTab->bLock--;
174485 return SQLITE_OK;
174486 }else{
174487 pTab->bLock--;
174488 rc = sqlite3_reset(pStmt: pCsr->pStmt);
174489 if( rc==SQLITE_OK && ((Fts3Table *)pCsr->base.pVtab)->zContentTbl==0 ){
174490 /* If no row was found and no error has occurred, then the %_content
174491 ** table is missing a row that is present in the full-text index.
174492 ** The data structures are corrupt. */
174493 rc = FTS_CORRUPT_VTAB;
174494 pCsr->isEof = 1;
174495 }
174496 }
174497 }
174498 }
174499
174500 if( rc!=SQLITE_OK && pContext ){
174501 sqlite3_result_error_code(pCtx: pContext, errCode: rc);
174502 }
174503 return rc;
174504}
174505
174506/*
174507** This function is used to process a single interior node when searching
174508** a b-tree for a term or term prefix. The node data is passed to this
174509** function via the zNode/nNode parameters. The term to search for is
174510** passed in zTerm/nTerm.
174511**
174512** If piFirst is not NULL, then this function sets *piFirst to the blockid
174513** of the child node that heads the sub-tree that may contain the term.
174514**
174515** If piLast is not NULL, then *piLast is set to the right-most child node
174516** that heads a sub-tree that may contain a term for which zTerm/nTerm is
174517** a prefix.
174518**
174519** If an OOM error occurs, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK.
174520*/
174521static int fts3ScanInteriorNode(
174522 const char *zTerm, /* Term to select leaves for */
174523 int nTerm, /* Size of term zTerm in bytes */
174524 const char *zNode, /* Buffer containing segment interior node */
174525 int nNode, /* Size of buffer at zNode */
174526 sqlite3_int64 *piFirst, /* OUT: Selected child node */
174527 sqlite3_int64 *piLast /* OUT: Selected child node */
174528){
174529 int rc = SQLITE_OK; /* Return code */
174530 const char *zCsr = zNode; /* Cursor to iterate through node */
174531 const char *zEnd = &zCsr[nNode];/* End of interior node buffer */
174532 char *zBuffer = 0; /* Buffer to load terms into */
174533 i64 nAlloc = 0; /* Size of allocated buffer */
174534 int isFirstTerm = 1; /* True when processing first term on page */
174535 u64 iChild; /* Block id of child node to descend to */
174536 int nBuffer = 0; /* Total term size */
174537
174538 /* Skip over the 'height' varint that occurs at the start of every
174539 ** interior node. Then load the blockid of the left-child of the b-tree
174540 ** node into variable iChild.
174541 **
174542 ** Even if the data structure on disk is corrupted, this (reading two
174543 ** varints from the buffer) does not risk an overread. If zNode is a
174544 ** root node, then the buffer comes from a SELECT statement. SQLite does
174545 ** not make this guarantee explicitly, but in practice there are always
174546 ** either more than 20 bytes of allocated space following the nNode bytes of
174547 ** contents, or two zero bytes. Or, if the node is read from the %_segments
174548 ** table, then there are always 20 bytes of zeroed padding following the
174549 ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details).
174550 */
174551 zCsr += sqlite3Fts3GetVarintU(pBuf: zCsr, v: &iChild);
174552 zCsr += sqlite3Fts3GetVarintU(pBuf: zCsr, v: &iChild);
174553 if( zCsr>zEnd ){
174554 return FTS_CORRUPT_VTAB;
174555 }
174556
174557 while( zCsr<zEnd && (piFirst || piLast) ){
174558 int cmp; /* memcmp() result */
174559 int nSuffix; /* Size of term suffix */
174560 int nPrefix = 0; /* Size of term prefix */
174561
174562 /* Load the next term on the node into zBuffer. Use realloc() to expand
174563 ** the size of zBuffer if required. */
174564 if( !isFirstTerm ){
174565 zCsr += fts3GetVarint32(zCsr, &nPrefix);
174566 if( nPrefix>nBuffer ){
174567 rc = FTS_CORRUPT_VTAB;
174568 goto finish_scan;
174569 }
174570 }
174571 isFirstTerm = 0;
174572 zCsr += fts3GetVarint32(zCsr, &nSuffix);
174573
174574 assert( nPrefix>=0 && nSuffix>=0 );
174575 if( nPrefix>zCsr-zNode || nSuffix>zEnd-zCsr || nSuffix==0 ){
174576 rc = FTS_CORRUPT_VTAB;
174577 goto finish_scan;
174578 }
174579 if( (i64)nPrefix+nSuffix>nAlloc ){
174580 char *zNew;
174581 nAlloc = ((i64)nPrefix+nSuffix) * 2;
174582 zNew = (char *)sqlite3_realloc64(pOld: zBuffer, n: nAlloc);
174583 if( !zNew ){
174584 rc = SQLITE_NOMEM;
174585 goto finish_scan;
174586 }
174587 zBuffer = zNew;
174588 }
174589 assert( zBuffer );
174590 memcpy(dest: &zBuffer[nPrefix], src: zCsr, n: nSuffix);
174591 nBuffer = nPrefix + nSuffix;
174592 zCsr += nSuffix;
174593
174594 /* Compare the term we are searching for with the term just loaded from
174595 ** the interior node. If the specified term is greater than or equal
174596 ** to the term from the interior node, then all terms on the sub-tree
174597 ** headed by node iChild are smaller than zTerm. No need to search
174598 ** iChild.
174599 **
174600 ** If the interior node term is larger than the specified term, then
174601 ** the tree headed by iChild may contain the specified term.
174602 */
174603 cmp = memcmp(s1: zTerm, s2: zBuffer, n: (nBuffer>nTerm ? nTerm : nBuffer));
174604 if( piFirst && (cmp<0 || (cmp==0 && nBuffer>nTerm)) ){
174605 *piFirst = (i64)iChild;
174606 piFirst = 0;
174607 }
174608
174609 if( piLast && cmp<0 ){
174610 *piLast = (i64)iChild;
174611 piLast = 0;
174612 }
174613
174614 iChild++;
174615 };
174616
174617 if( piFirst ) *piFirst = (i64)iChild;
174618 if( piLast ) *piLast = (i64)iChild;
174619
174620 finish_scan:
174621 sqlite3_free(p: zBuffer);
174622 return rc;
174623}
174624
174625
174626/*
174627** The buffer pointed to by argument zNode (size nNode bytes) contains an
174628** interior node of a b-tree segment. The zTerm buffer (size nTerm bytes)
174629** contains a term. This function searches the sub-tree headed by the zNode
174630** node for the range of leaf nodes that may contain the specified term
174631** or terms for which the specified term is a prefix.
174632**
174633** If piLeaf is not NULL, then *piLeaf is set to the blockid of the
174634** left-most leaf node in the tree that may contain the specified term.
174635** If piLeaf2 is not NULL, then *piLeaf2 is set to the blockid of the
174636** right-most leaf node that may contain a term for which the specified
174637** term is a prefix.
174638**
174639** It is possible that the range of returned leaf nodes does not contain
174640** the specified term or any terms for which it is a prefix. However, if the
174641** segment does contain any such terms, they are stored within the identified
174642** range. Because this function only inspects interior segment nodes (and
174643** never loads leaf nodes into memory), it is not possible to be sure.
174644**
174645** If an error occurs, an error code other than SQLITE_OK is returned.
174646*/
174647static int fts3SelectLeaf(
174648 Fts3Table *p, /* Virtual table handle */
174649 const char *zTerm, /* Term to select leaves for */
174650 int nTerm, /* Size of term zTerm in bytes */
174651 const char *zNode, /* Buffer containing segment interior node */
174652 int nNode, /* Size of buffer at zNode */
174653 sqlite3_int64 *piLeaf, /* Selected leaf node */
174654 sqlite3_int64 *piLeaf2 /* Selected leaf node */
174655){
174656 int rc = SQLITE_OK; /* Return code */
174657 int iHeight; /* Height of this node in tree */
174658
174659 assert( piLeaf || piLeaf2 );
174660
174661 fts3GetVarint32(zNode, &iHeight);
174662 rc = fts3ScanInteriorNode(zTerm, nTerm, zNode, nNode, piFirst: piLeaf, piLast: piLeaf2);
174663 assert_fts3_nc( !piLeaf2 || !piLeaf || rc!=SQLITE_OK || (*piLeaf<=*piLeaf2) );
174664
174665 if( rc==SQLITE_OK && iHeight>1 ){
174666 char *zBlob = 0; /* Blob read from %_segments table */
174667 int nBlob = 0; /* Size of zBlob in bytes */
174668
174669 if( piLeaf && piLeaf2 && (*piLeaf!=*piLeaf2) ){
174670 rc = sqlite3Fts3ReadBlock(p, *piLeaf, &zBlob, &nBlob, 0);
174671 if( rc==SQLITE_OK ){
174672 rc = fts3SelectLeaf(p, zTerm, nTerm, zNode: zBlob, nNode: nBlob, piLeaf, piLeaf2: 0);
174673 }
174674 sqlite3_free(p: zBlob);
174675 piLeaf = 0;
174676 zBlob = 0;
174677 }
174678
174679 if( rc==SQLITE_OK ){
174680 rc = sqlite3Fts3ReadBlock(p, piLeaf?*piLeaf:*piLeaf2, &zBlob, &nBlob, 0);
174681 }
174682 if( rc==SQLITE_OK ){
174683 int iNewHeight = 0;
174684 fts3GetVarint32(zBlob, &iNewHeight);
174685 if( iNewHeight>=iHeight ){
174686 rc = FTS_CORRUPT_VTAB;
174687 }else{
174688 rc = fts3SelectLeaf(p, zTerm, nTerm, zNode: zBlob, nNode: nBlob, piLeaf, piLeaf2);
174689 }
174690 }
174691 sqlite3_free(p: zBlob);
174692 }
174693
174694 return rc;
174695}
174696
174697/*
174698** This function is used to create delta-encoded serialized lists of FTS3
174699** varints. Each call to this function appends a single varint to a list.
174700*/
174701static void fts3PutDeltaVarint(
174702 char **pp, /* IN/OUT: Output pointer */
174703 sqlite3_int64 *piPrev, /* IN/OUT: Previous value written to list */
174704 sqlite3_int64 iVal /* Write this value to the list */
174705){
174706 assert_fts3_nc( iVal-*piPrev > 0 || (*piPrev==0 && iVal==0) );
174707 *pp += sqlite3Fts3PutVarint(p: *pp, v: iVal-*piPrev);
174708 *piPrev = iVal;
174709}
174710
174711/*
174712** When this function is called, *ppPoslist is assumed to point to the
174713** start of a position-list. After it returns, *ppPoslist points to the
174714** first byte after the position-list.
174715**
174716** A position list is list of positions (delta encoded) and columns for
174717** a single document record of a doclist. So, in other words, this
174718** routine advances *ppPoslist so that it points to the next docid in
174719** the doclist, or to the first byte past the end of the doclist.
174720**
174721** If pp is not NULL, then the contents of the position list are copied
174722** to *pp. *pp is set to point to the first byte past the last byte copied
174723** before this function returns.
174724*/
174725static void fts3PoslistCopy(char **pp, char **ppPoslist){
174726 char *pEnd = *ppPoslist;
174727 char c = 0;
174728
174729 /* The end of a position list is marked by a zero encoded as an FTS3
174730 ** varint. A single POS_END (0) byte. Except, if the 0 byte is preceded by
174731 ** a byte with the 0x80 bit set, then it is not a varint 0, but the tail
174732 ** of some other, multi-byte, value.
174733 **
174734 ** The following while-loop moves pEnd to point to the first byte that is not
174735 ** immediately preceded by a byte with the 0x80 bit set. Then increments
174736 ** pEnd once more so that it points to the byte immediately following the
174737 ** last byte in the position-list.
174738 */
174739 while( *pEnd | c ){
174740 c = *pEnd++ & 0x80;
174741 testcase( c!=0 && (*pEnd)==0 );
174742 }
174743 pEnd++; /* Advance past the POS_END terminator byte */
174744
174745 if( pp ){
174746 int n = (int)(pEnd - *ppPoslist);
174747 char *p = *pp;
174748 memcpy(dest: p, src: *ppPoslist, n: n);
174749 p += n;
174750 *pp = p;
174751 }
174752 *ppPoslist = pEnd;
174753}
174754
174755/*
174756** When this function is called, *ppPoslist is assumed to point to the
174757** start of a column-list. After it returns, *ppPoslist points to the
174758** to the terminator (POS_COLUMN or POS_END) byte of the column-list.
174759**
174760** A column-list is list of delta-encoded positions for a single column
174761** within a single document within a doclist.
174762**
174763** The column-list is terminated either by a POS_COLUMN varint (1) or
174764** a POS_END varint (0). This routine leaves *ppPoslist pointing to
174765** the POS_COLUMN or POS_END that terminates the column-list.
174766**
174767** If pp is not NULL, then the contents of the column-list are copied
174768** to *pp. *pp is set to point to the first byte past the last byte copied
174769** before this function returns. The POS_COLUMN or POS_END terminator
174770** is not copied into *pp.
174771*/
174772static void fts3ColumnlistCopy(char **pp, char **ppPoslist){
174773 char *pEnd = *ppPoslist;
174774 char c = 0;
174775
174776 /* A column-list is terminated by either a 0x01 or 0x00 byte that is
174777 ** not part of a multi-byte varint.
174778 */
174779 while( 0xFE & (*pEnd | c) ){
174780 c = *pEnd++ & 0x80;
174781 testcase( c!=0 && ((*pEnd)&0xfe)==0 );
174782 }
174783 if( pp ){
174784 int n = (int)(pEnd - *ppPoslist);
174785 char *p = *pp;
174786 memcpy(dest: p, src: *ppPoslist, n: n);
174787 p += n;
174788 *pp = p;
174789 }
174790 *ppPoslist = pEnd;
174791}
174792
174793/*
174794** Value used to signify the end of an position-list. This must be
174795** as large or larger than any value that might appear on the
174796** position-list, even a position list that has been corrupted.
174797*/
174798#define POSITION_LIST_END LARGEST_INT64
174799
174800/*
174801** This function is used to help parse position-lists. When this function is
174802** called, *pp may point to the start of the next varint in the position-list
174803** being parsed, or it may point to 1 byte past the end of the position-list
174804** (in which case **pp will be a terminator bytes POS_END (0) or
174805** (1)).
174806**
174807** If *pp points past the end of the current position-list, set *pi to
174808** POSITION_LIST_END and return. Otherwise, read the next varint from *pp,
174809** increment the current value of *pi by the value read, and set *pp to
174810** point to the next value before returning.
174811**
174812** Before calling this routine *pi must be initialized to the value of
174813** the previous position, or zero if we are reading the first position
174814** in the position-list. Because positions are delta-encoded, the value
174815** of the previous position is needed in order to compute the value of
174816** the next position.
174817*/
174818static void fts3ReadNextPos(
174819 char **pp, /* IN/OUT: Pointer into position-list buffer */
174820 sqlite3_int64 *pi /* IN/OUT: Value read from position-list */
174821){
174822 if( (**pp)&0xFE ){
174823 int iVal;
174824 *pp += fts3GetVarint32((*pp), &iVal);
174825 *pi += iVal;
174826 *pi -= 2;
174827 }else{
174828 *pi = POSITION_LIST_END;
174829 }
174830}
174831
174832/*
174833** If parameter iCol is not 0, write an POS_COLUMN (1) byte followed by
174834** the value of iCol encoded as a varint to *pp. This will start a new
174835** column list.
174836**
174837** Set *pp to point to the byte just after the last byte written before
174838** returning (do not modify it if iCol==0). Return the total number of bytes
174839** written (0 if iCol==0).
174840*/
174841static int fts3PutColNumber(char **pp, int iCol){
174842 int n = 0; /* Number of bytes written */
174843 if( iCol ){
174844 char *p = *pp; /* Output pointer */
174845 n = 1 + sqlite3Fts3PutVarint(p: &p[1], v: iCol);
174846 *p = 0x01;
174847 *pp = &p[n];
174848 }
174849 return n;
174850}
174851
174852/*
174853** Compute the union of two position lists. The output written
174854** into *pp contains all positions of both *pp1 and *pp2 in sorted
174855** order and with any duplicates removed. All pointers are
174856** updated appropriately. The caller is responsible for insuring
174857** that there is enough space in *pp to hold the complete output.
174858*/
174859static int fts3PoslistMerge(
174860 char **pp, /* Output buffer */
174861 char **pp1, /* Left input list */
174862 char **pp2 /* Right input list */
174863){
174864 char *p = *pp;
174865 char *p1 = *pp1;
174866 char *p2 = *pp2;
174867
174868 while( *p1 || *p2 ){
174869 int iCol1; /* The current column index in pp1 */
174870 int iCol2; /* The current column index in pp2 */
174871
174872 if( *p1==POS_COLUMN ){
174873 fts3GetVarint32(&p1[1], &iCol1);
174874 if( iCol1==0 ) return FTS_CORRUPT_VTAB;
174875 }
174876 else if( *p1==POS_END ) iCol1 = 0x7fffffff;
174877 else iCol1 = 0;
174878
174879 if( *p2==POS_COLUMN ){
174880 fts3GetVarint32(&p2[1], &iCol2);
174881 if( iCol2==0 ) return FTS_CORRUPT_VTAB;
174882 }
174883 else if( *p2==POS_END ) iCol2 = 0x7fffffff;
174884 else iCol2 = 0;
174885
174886 if( iCol1==iCol2 ){
174887 sqlite3_int64 i1 = 0; /* Last position from pp1 */
174888 sqlite3_int64 i2 = 0; /* Last position from pp2 */
174889 sqlite3_int64 iPrev = 0;
174890 int n = fts3PutColNumber(pp: &p, iCol: iCol1);
174891 p1 += n;
174892 p2 += n;
174893
174894 /* At this point, both p1 and p2 point to the start of column-lists
174895 ** for the same column (the column with index iCol1 and iCol2).
174896 ** A column-list is a list of non-negative delta-encoded varints, each
174897 ** incremented by 2 before being stored. Each list is terminated by a
174898 ** POS_END (0) or POS_COLUMN (1). The following block merges the two lists
174899 ** and writes the results to buffer p. p is left pointing to the byte
174900 ** after the list written. No terminator (POS_END or POS_COLUMN) is
174901 ** written to the output.
174902 */
174903 fts3GetDeltaVarint(pp: &p1, pVal: &i1);
174904 fts3GetDeltaVarint(pp: &p2, pVal: &i2);
174905 if( i1<2 || i2<2 ){
174906 break;
174907 }
174908 do {
174909 fts3PutDeltaVarint(pp: &p, piPrev: &iPrev, iVal: (i1<i2) ? i1 : i2);
174910 iPrev -= 2;
174911 if( i1==i2 ){
174912 fts3ReadNextPos(pp: &p1, pi: &i1);
174913 fts3ReadNextPos(pp: &p2, pi: &i2);
174914 }else if( i1<i2 ){
174915 fts3ReadNextPos(pp: &p1, pi: &i1);
174916 }else{
174917 fts3ReadNextPos(pp: &p2, pi: &i2);
174918 }
174919 }while( i1!=POSITION_LIST_END || i2!=POSITION_LIST_END );
174920 }else if( iCol1<iCol2 ){
174921 p1 += fts3PutColNumber(pp: &p, iCol: iCol1);
174922 fts3ColumnlistCopy(pp: &p, ppPoslist: &p1);
174923 }else{
174924 p2 += fts3PutColNumber(pp: &p, iCol: iCol2);
174925 fts3ColumnlistCopy(pp: &p, ppPoslist: &p2);
174926 }
174927 }
174928
174929 *p++ = POS_END;
174930 *pp = p;
174931 *pp1 = p1 + 1;
174932 *pp2 = p2 + 1;
174933 return SQLITE_OK;
174934}
174935
174936/*
174937** This function is used to merge two position lists into one. When it is
174938** called, *pp1 and *pp2 must both point to position lists. A position-list is
174939** the part of a doclist that follows each document id. For example, if a row
174940** contains:
174941**
174942** 'a b c'|'x y z'|'a b b a'
174943**
174944** Then the position list for this row for token 'b' would consist of:
174945**
174946** 0x02 0x01 0x02 0x03 0x03 0x00
174947**
174948** When this function returns, both *pp1 and *pp2 are left pointing to the
174949** byte following the 0x00 terminator of their respective position lists.
174950**
174951** If isSaveLeft is 0, an entry is added to the output position list for
174952** each position in *pp2 for which there exists one or more positions in
174953** *pp1 so that (pos(*pp2)>pos(*pp1) && pos(*pp2)-pos(*pp1)<=nToken). i.e.
174954** when the *pp1 token appears before the *pp2 token, but not more than nToken
174955** slots before it.
174956**
174957** e.g. nToken==1 searches for adjacent positions.
174958*/
174959static int fts3PoslistPhraseMerge(
174960 char **pp, /* IN/OUT: Preallocated output buffer */
174961 int nToken, /* Maximum difference in token positions */
174962 int isSaveLeft, /* Save the left position */
174963 int isExact, /* If *pp1 is exactly nTokens before *pp2 */
174964 char **pp1, /* IN/OUT: Left input list */
174965 char **pp2 /* IN/OUT: Right input list */
174966){
174967 char *p = *pp;
174968 char *p1 = *pp1;
174969 char *p2 = *pp2;
174970 int iCol1 = 0;
174971 int iCol2 = 0;
174972
174973 /* Never set both isSaveLeft and isExact for the same invocation. */
174974 assert( isSaveLeft==0 || isExact==0 );
174975
174976 assert_fts3_nc( p!=0 && *p1!=0 && *p2!=0 );
174977 if( *p1==POS_COLUMN ){
174978 p1++;
174979 p1 += fts3GetVarint32(p1, &iCol1);
174980 }
174981 if( *p2==POS_COLUMN ){
174982 p2++;
174983 p2 += fts3GetVarint32(p2, &iCol2);
174984 }
174985
174986 while( 1 ){
174987 if( iCol1==iCol2 ){
174988 char *pSave = p;
174989 sqlite3_int64 iPrev = 0;
174990 sqlite3_int64 iPos1 = 0;
174991 sqlite3_int64 iPos2 = 0;
174992
174993 if( iCol1 ){
174994 *p++ = POS_COLUMN;
174995 p += sqlite3Fts3PutVarint(p, v: iCol1);
174996 }
174997
174998 fts3GetDeltaVarint(pp: &p1, pVal: &iPos1); iPos1 -= 2;
174999 fts3GetDeltaVarint(pp: &p2, pVal: &iPos2); iPos2 -= 2;
175000 if( iPos1<0 || iPos2<0 ) break;
175001
175002 while( 1 ){
175003 if( iPos2==iPos1+nToken
175004 || (isExact==0 && iPos2>iPos1 && iPos2<=iPos1+nToken)
175005 ){
175006 sqlite3_int64 iSave;
175007 iSave = isSaveLeft ? iPos1 : iPos2;
175008 fts3PutDeltaVarint(pp: &p, piPrev: &iPrev, iVal: iSave+2); iPrev -= 2;
175009 pSave = 0;
175010 assert( p );
175011 }
175012 if( (!isSaveLeft && iPos2<=(iPos1+nToken)) || iPos2<=iPos1 ){
175013 if( (*p2&0xFE)==0 ) break;
175014 fts3GetDeltaVarint(pp: &p2, pVal: &iPos2); iPos2 -= 2;
175015 }else{
175016 if( (*p1&0xFE)==0 ) break;
175017 fts3GetDeltaVarint(pp: &p1, pVal: &iPos1); iPos1 -= 2;
175018 }
175019 }
175020
175021 if( pSave ){
175022 assert( pp && p );
175023 p = pSave;
175024 }
175025
175026 fts3ColumnlistCopy(pp: 0, ppPoslist: &p1);
175027 fts3ColumnlistCopy(pp: 0, ppPoslist: &p2);
175028 assert( (*p1&0xFE)==0 && (*p2&0xFE)==0 );
175029 if( 0==*p1 || 0==*p2 ) break;
175030
175031 p1++;
175032 p1 += fts3GetVarint32(p1, &iCol1);
175033 p2++;
175034 p2 += fts3GetVarint32(p2, &iCol2);
175035 }
175036
175037 /* Advance pointer p1 or p2 (whichever corresponds to the smaller of
175038 ** iCol1 and iCol2) so that it points to either the 0x00 that marks the
175039 ** end of the position list, or the 0x01 that precedes the next
175040 ** column-number in the position list.
175041 */
175042 else if( iCol1<iCol2 ){
175043 fts3ColumnlistCopy(pp: 0, ppPoslist: &p1);
175044 if( 0==*p1 ) break;
175045 p1++;
175046 p1 += fts3GetVarint32(p1, &iCol1);
175047 }else{
175048 fts3ColumnlistCopy(pp: 0, ppPoslist: &p2);
175049 if( 0==*p2 ) break;
175050 p2++;
175051 p2 += fts3GetVarint32(p2, &iCol2);
175052 }
175053 }
175054
175055 fts3PoslistCopy(pp: 0, ppPoslist: &p2);
175056 fts3PoslistCopy(pp: 0, ppPoslist: &p1);
175057 *pp1 = p1;
175058 *pp2 = p2;
175059 if( *pp==p ){
175060 return 0;
175061 }
175062 *p++ = 0x00;
175063 *pp = p;
175064 return 1;
175065}
175066
175067/*
175068** Merge two position-lists as required by the NEAR operator. The argument
175069** position lists correspond to the left and right phrases of an expression
175070** like:
175071**
175072** "phrase 1" NEAR "phrase number 2"
175073**
175074** Position list *pp1 corresponds to the left-hand side of the NEAR
175075** expression and *pp2 to the right. As usual, the indexes in the position
175076** lists are the offsets of the last token in each phrase (tokens "1" and "2"
175077** in the example above).
175078**
175079** The output position list - written to *pp - is a copy of *pp2 with those
175080** entries that are not sufficiently NEAR entries in *pp1 removed.
175081*/
175082static int fts3PoslistNearMerge(
175083 char **pp, /* Output buffer */
175084 char *aTmp, /* Temporary buffer space */
175085 int nRight, /* Maximum difference in token positions */
175086 int nLeft, /* Maximum difference in token positions */
175087 char **pp1, /* IN/OUT: Left input list */
175088 char **pp2 /* IN/OUT: Right input list */
175089){
175090 char *p1 = *pp1;
175091 char *p2 = *pp2;
175092
175093 char *pTmp1 = aTmp;
175094 char *pTmp2;
175095 char *aTmp2;
175096 int res = 1;
175097
175098 fts3PoslistPhraseMerge(pp: &pTmp1, nToken: nRight, isSaveLeft: 0, isExact: 0, pp1, pp2);
175099 aTmp2 = pTmp2 = pTmp1;
175100 *pp1 = p1;
175101 *pp2 = p2;
175102 fts3PoslistPhraseMerge(pp: &pTmp2, nToken: nLeft, isSaveLeft: 1, isExact: 0, pp1: pp2, pp2: pp1);
175103 if( pTmp1!=aTmp && pTmp2!=aTmp2 ){
175104 fts3PoslistMerge(pp, pp1: &aTmp, pp2: &aTmp2);
175105 }else if( pTmp1!=aTmp ){
175106 fts3PoslistCopy(pp, ppPoslist: &aTmp);
175107 }else if( pTmp2!=aTmp2 ){
175108 fts3PoslistCopy(pp, ppPoslist: &aTmp2);
175109 }else{
175110 res = 0;
175111 }
175112
175113 return res;
175114}
175115
175116/*
175117** An instance of this function is used to merge together the (potentially
175118** large number of) doclists for each term that matches a prefix query.
175119** See function fts3TermSelectMerge() for details.
175120*/
175121typedef struct TermSelect TermSelect;
175122struct TermSelect {
175123 char *aaOutput[16]; /* Malloc'd output buffers */
175124 int anOutput[16]; /* Size each output buffer in bytes */
175125};
175126
175127/*
175128** This function is used to read a single varint from a buffer. Parameter
175129** pEnd points 1 byte past the end of the buffer. When this function is
175130** called, if *pp points to pEnd or greater, then the end of the buffer
175131** has been reached. In this case *pp is set to 0 and the function returns.
175132**
175133** If *pp does not point to or past pEnd, then a single varint is read
175134** from *pp. *pp is then set to point 1 byte past the end of the read varint.
175135**
175136** If bDescIdx is false, the value read is added to *pVal before returning.
175137** If it is true, the value read is subtracted from *pVal before this
175138** function returns.
175139*/
175140static void fts3GetDeltaVarint3(
175141 char **pp, /* IN/OUT: Point to read varint from */
175142 char *pEnd, /* End of buffer */
175143 int bDescIdx, /* True if docids are descending */
175144 sqlite3_int64 *pVal /* IN/OUT: Integer value */
175145){
175146 if( *pp>=pEnd ){
175147 *pp = 0;
175148 }else{
175149 u64 iVal;
175150 *pp += sqlite3Fts3GetVarintU(pBuf: *pp, v: &iVal);
175151 if( bDescIdx ){
175152 *pVal = (i64)((u64)*pVal - iVal);
175153 }else{
175154 *pVal = (i64)((u64)*pVal + iVal);
175155 }
175156 }
175157}
175158
175159/*
175160** This function is used to write a single varint to a buffer. The varint
175161** is written to *pp. Before returning, *pp is set to point 1 byte past the
175162** end of the value written.
175163**
175164** If *pbFirst is zero when this function is called, the value written to
175165** the buffer is that of parameter iVal.
175166**
175167** If *pbFirst is non-zero when this function is called, then the value
175168** written is either (iVal-*piPrev) (if bDescIdx is zero) or (*piPrev-iVal)
175169** (if bDescIdx is non-zero).
175170**
175171** Before returning, this function always sets *pbFirst to 1 and *piPrev
175172** to the value of parameter iVal.
175173*/
175174static void fts3PutDeltaVarint3(
175175 char **pp, /* IN/OUT: Output pointer */
175176 int bDescIdx, /* True for descending docids */
175177 sqlite3_int64 *piPrev, /* IN/OUT: Previous value written to list */
175178 int *pbFirst, /* IN/OUT: True after first int written */
175179 sqlite3_int64 iVal /* Write this value to the list */
175180){
175181 sqlite3_uint64 iWrite;
175182 if( bDescIdx==0 || *pbFirst==0 ){
175183 assert_fts3_nc( *pbFirst==0 || iVal>=*piPrev );
175184 iWrite = (u64)iVal - (u64)*piPrev;
175185 }else{
175186 assert_fts3_nc( *piPrev>=iVal );
175187 iWrite = (u64)*piPrev - (u64)iVal;
175188 }
175189 assert( *pbFirst || *piPrev==0 );
175190 assert_fts3_nc( *pbFirst==0 || iWrite>0 );
175191 *pp += sqlite3Fts3PutVarint(p: *pp, v: iWrite);
175192 *piPrev = iVal;
175193 *pbFirst = 1;
175194}
175195
175196
175197/*
175198** This macro is used by various functions that merge doclists. The two
175199** arguments are 64-bit docid values. If the value of the stack variable
175200** bDescDoclist is 0 when this macro is invoked, then it returns (i1-i2).
175201** Otherwise, (i2-i1).
175202**
175203** Using this makes it easier to write code that can merge doclists that are
175204** sorted in either ascending or descending order.
175205*/
175206/* #define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i64)((u64)i1-i2)) */
175207#define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i1>i2?1:((i1==i2)?0:-1)))
175208
175209/*
175210** This function does an "OR" merge of two doclists (output contains all
175211** positions contained in either argument doclist). If the docids in the
175212** input doclists are sorted in ascending order, parameter bDescDoclist
175213** should be false. If they are sorted in ascending order, it should be
175214** passed a non-zero value.
175215**
175216** If no error occurs, *paOut is set to point at an sqlite3_malloc'd buffer
175217** containing the output doclist and SQLITE_OK is returned. In this case
175218** *pnOut is set to the number of bytes in the output doclist.
175219**
175220** If an error occurs, an SQLite error code is returned. The output values
175221** are undefined in this case.
175222*/
175223static int fts3DoclistOrMerge(
175224 int bDescDoclist, /* True if arguments are desc */
175225 char *a1, int n1, /* First doclist */
175226 char *a2, int n2, /* Second doclist */
175227 char **paOut, int *pnOut /* OUT: Malloc'd doclist */
175228){
175229 int rc = SQLITE_OK;
175230 sqlite3_int64 i1 = 0;
175231 sqlite3_int64 i2 = 0;
175232 sqlite3_int64 iPrev = 0;
175233 char *pEnd1 = &a1[n1];
175234 char *pEnd2 = &a2[n2];
175235 char *p1 = a1;
175236 char *p2 = a2;
175237 char *p;
175238 char *aOut;
175239 int bFirstOut = 0;
175240
175241 *paOut = 0;
175242 *pnOut = 0;
175243
175244 /* Allocate space for the output. Both the input and output doclists
175245 ** are delta encoded. If they are in ascending order (bDescDoclist==0),
175246 ** then the first docid in each list is simply encoded as a varint. For
175247 ** each subsequent docid, the varint stored is the difference between the
175248 ** current and previous docid (a positive number - since the list is in
175249 ** ascending order).
175250 **
175251 ** The first docid written to the output is therefore encoded using the
175252 ** same number of bytes as it is in whichever of the input lists it is
175253 ** read from. And each subsequent docid read from the same input list
175254 ** consumes either the same or less bytes as it did in the input (since
175255 ** the difference between it and the previous value in the output must
175256 ** be a positive value less than or equal to the delta value read from
175257 ** the input list). The same argument applies to all but the first docid
175258 ** read from the 'other' list. And to the contents of all position lists
175259 ** that will be copied and merged from the input to the output.
175260 **
175261 ** However, if the first docid copied to the output is a negative number,
175262 ** then the encoding of the first docid from the 'other' input list may
175263 ** be larger in the output than it was in the input (since the delta value
175264 ** may be a larger positive integer than the actual docid).
175265 **
175266 ** The space required to store the output is therefore the sum of the
175267 ** sizes of the two inputs, plus enough space for exactly one of the input
175268 ** docids to grow.
175269 **
175270 ** A symetric argument may be made if the doclists are in descending
175271 ** order.
175272 */
175273 aOut = sqlite3_malloc64(n: (i64)n1+n2+FTS3_VARINT_MAX-1+FTS3_BUFFER_PADDING);
175274 if( !aOut ) return SQLITE_NOMEM;
175275
175276 p = aOut;
175277 fts3GetDeltaVarint3(pp: &p1, pEnd: pEnd1, bDescIdx: 0, pVal: &i1);
175278 fts3GetDeltaVarint3(pp: &p2, pEnd: pEnd2, bDescIdx: 0, pVal: &i2);
175279 while( p1 || p2 ){
175280 sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
175281
175282 if( p2 && p1 && iDiff==0 ){
175283 fts3PutDeltaVarint3(pp: &p, bDescIdx: bDescDoclist, piPrev: &iPrev, pbFirst: &bFirstOut, iVal: i1);
175284 rc = fts3PoslistMerge(pp: &p, pp1: &p1, pp2: &p2);
175285 if( rc ) break;
175286 fts3GetDeltaVarint3(pp: &p1, pEnd: pEnd1, bDescIdx: bDescDoclist, pVal: &i1);
175287 fts3GetDeltaVarint3(pp: &p2, pEnd: pEnd2, bDescIdx: bDescDoclist, pVal: &i2);
175288 }else if( !p2 || (p1 && iDiff<0) ){
175289 fts3PutDeltaVarint3(pp: &p, bDescIdx: bDescDoclist, piPrev: &iPrev, pbFirst: &bFirstOut, iVal: i1);
175290 fts3PoslistCopy(pp: &p, ppPoslist: &p1);
175291 fts3GetDeltaVarint3(pp: &p1, pEnd: pEnd1, bDescIdx: bDescDoclist, pVal: &i1);
175292 }else{
175293 fts3PutDeltaVarint3(pp: &p, bDescIdx: bDescDoclist, piPrev: &iPrev, pbFirst: &bFirstOut, iVal: i2);
175294 fts3PoslistCopy(pp: &p, ppPoslist: &p2);
175295 fts3GetDeltaVarint3(pp: &p2, pEnd: pEnd2, bDescIdx: bDescDoclist, pVal: &i2);
175296 }
175297
175298 assert( (p-aOut)<=((p1?(p1-a1):n1)+(p2?(p2-a2):n2)+FTS3_VARINT_MAX-1) );
175299 }
175300
175301 if( rc!=SQLITE_OK ){
175302 sqlite3_free(p: aOut);
175303 p = aOut = 0;
175304 }else{
175305 assert( (p-aOut)<=n1+n2+FTS3_VARINT_MAX-1 );
175306 memset(s: &aOut[(p-aOut)], c: 0, FTS3_BUFFER_PADDING);
175307 }
175308 *paOut = aOut;
175309 *pnOut = (int)(p-aOut);
175310 return rc;
175311}
175312
175313/*
175314** This function does a "phrase" merge of two doclists. In a phrase merge,
175315** the output contains a copy of each position from the right-hand input
175316** doclist for which there is a position in the left-hand input doclist
175317** exactly nDist tokens before it.
175318**
175319** If the docids in the input doclists are sorted in ascending order,
175320** parameter bDescDoclist should be false. If they are sorted in ascending
175321** order, it should be passed a non-zero value.
175322**
175323** The right-hand input doclist is overwritten by this function.
175324*/
175325static int fts3DoclistPhraseMerge(
175326 int bDescDoclist, /* True if arguments are desc */
175327 int nDist, /* Distance from left to right (1=adjacent) */
175328 char *aLeft, int nLeft, /* Left doclist */
175329 char **paRight, int *pnRight /* IN/OUT: Right/output doclist */
175330){
175331 sqlite3_int64 i1 = 0;
175332 sqlite3_int64 i2 = 0;
175333 sqlite3_int64 iPrev = 0;
175334 char *aRight = *paRight;
175335 char *pEnd1 = &aLeft[nLeft];
175336 char *pEnd2 = &aRight[*pnRight];
175337 char *p1 = aLeft;
175338 char *p2 = aRight;
175339 char *p;
175340 int bFirstOut = 0;
175341 char *aOut;
175342
175343 assert( nDist>0 );
175344 if( bDescDoclist ){
175345 aOut = sqlite3_malloc64(n: (sqlite3_int64)*pnRight + FTS3_VARINT_MAX);
175346 if( aOut==0 ) return SQLITE_NOMEM;
175347 }else{
175348 aOut = aRight;
175349 }
175350 p = aOut;
175351
175352 fts3GetDeltaVarint3(pp: &p1, pEnd: pEnd1, bDescIdx: 0, pVal: &i1);
175353 fts3GetDeltaVarint3(pp: &p2, pEnd: pEnd2, bDescIdx: 0, pVal: &i2);
175354
175355 while( p1 && p2 ){
175356 sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
175357 if( iDiff==0 ){
175358 char *pSave = p;
175359 sqlite3_int64 iPrevSave = iPrev;
175360 int bFirstOutSave = bFirstOut;
175361
175362 fts3PutDeltaVarint3(pp: &p, bDescIdx: bDescDoclist, piPrev: &iPrev, pbFirst: &bFirstOut, iVal: i1);
175363 if( 0==fts3PoslistPhraseMerge(pp: &p, nToken: nDist, isSaveLeft: 0, isExact: 1, pp1: &p1, pp2: &p2) ){
175364 p = pSave;
175365 iPrev = iPrevSave;
175366 bFirstOut = bFirstOutSave;
175367 }
175368 fts3GetDeltaVarint3(pp: &p1, pEnd: pEnd1, bDescIdx: bDescDoclist, pVal: &i1);
175369 fts3GetDeltaVarint3(pp: &p2, pEnd: pEnd2, bDescIdx: bDescDoclist, pVal: &i2);
175370 }else if( iDiff<0 ){
175371 fts3PoslistCopy(pp: 0, ppPoslist: &p1);
175372 fts3GetDeltaVarint3(pp: &p1, pEnd: pEnd1, bDescIdx: bDescDoclist, pVal: &i1);
175373 }else{
175374 fts3PoslistCopy(pp: 0, ppPoslist: &p2);
175375 fts3GetDeltaVarint3(pp: &p2, pEnd: pEnd2, bDescIdx: bDescDoclist, pVal: &i2);
175376 }
175377 }
175378
175379 *pnRight = (int)(p - aOut);
175380 if( bDescDoclist ){
175381 sqlite3_free(p: aRight);
175382 *paRight = aOut;
175383 }
175384
175385 return SQLITE_OK;
175386}
175387
175388/*
175389** Argument pList points to a position list nList bytes in size. This
175390** function checks to see if the position list contains any entries for
175391** a token in position 0 (of any column). If so, it writes argument iDelta
175392** to the output buffer pOut, followed by a position list consisting only
175393** of the entries from pList at position 0, and terminated by an 0x00 byte.
175394** The value returned is the number of bytes written to pOut (if any).
175395*/
175396SQLITE_PRIVATE int sqlite3Fts3FirstFilter(
175397 sqlite3_int64 iDelta, /* Varint that may be written to pOut */
175398 char *pList, /* Position list (no 0x00 term) */
175399 int nList, /* Size of pList in bytes */
175400 char *pOut /* Write output here */
175401){
175402 int nOut = 0;
175403 int bWritten = 0; /* True once iDelta has been written */
175404 char *p = pList;
175405 char *pEnd = &pList[nList];
175406
175407 if( *p!=0x01 ){
175408 if( *p==0x02 ){
175409 nOut += sqlite3Fts3PutVarint(p: &pOut[nOut], v: iDelta);
175410 pOut[nOut++] = 0x02;
175411 bWritten = 1;
175412 }
175413 fts3ColumnlistCopy(pp: 0, ppPoslist: &p);
175414 }
175415
175416 while( p<pEnd ){
175417 sqlite3_int64 iCol;
175418 p++;
175419 p += sqlite3Fts3GetVarint(pBuf: p, v: &iCol);
175420 if( *p==0x02 ){
175421 if( bWritten==0 ){
175422 nOut += sqlite3Fts3PutVarint(p: &pOut[nOut], v: iDelta);
175423 bWritten = 1;
175424 }
175425 pOut[nOut++] = 0x01;
175426 nOut += sqlite3Fts3PutVarint(p: &pOut[nOut], v: iCol);
175427 pOut[nOut++] = 0x02;
175428 }
175429 fts3ColumnlistCopy(pp: 0, ppPoslist: &p);
175430 }
175431 if( bWritten ){
175432 pOut[nOut++] = 0x00;
175433 }
175434
175435 return nOut;
175436}
175437
175438
175439/*
175440** Merge all doclists in the TermSelect.aaOutput[] array into a single
175441** doclist stored in TermSelect.aaOutput[0]. If successful, delete all
175442** other doclists (except the aaOutput[0] one) and return SQLITE_OK.
175443**
175444** If an OOM error occurs, return SQLITE_NOMEM. In this case it is
175445** the responsibility of the caller to free any doclists left in the
175446** TermSelect.aaOutput[] array.
175447*/
175448static int fts3TermSelectFinishMerge(Fts3Table *p, TermSelect *pTS){
175449 char *aOut = 0;
175450 int nOut = 0;
175451 int i;
175452
175453 /* Loop through the doclists in the aaOutput[] array. Merge them all
175454 ** into a single doclist.
175455 */
175456 for(i=0; i<SizeofArray(pTS->aaOutput); i++){
175457 if( pTS->aaOutput[i] ){
175458 if( !aOut ){
175459 aOut = pTS->aaOutput[i];
175460 nOut = pTS->anOutput[i];
175461 pTS->aaOutput[i] = 0;
175462 }else{
175463 int nNew;
175464 char *aNew;
175465
175466 int rc = fts3DoclistOrMerge(bDescDoclist: p->bDescIdx,
175467 a1: pTS->aaOutput[i], n1: pTS->anOutput[i], a2: aOut, n2: nOut, paOut: &aNew, pnOut: &nNew
175468 );
175469 if( rc!=SQLITE_OK ){
175470 sqlite3_free(p: aOut);
175471 return rc;
175472 }
175473
175474 sqlite3_free(p: pTS->aaOutput[i]);
175475 sqlite3_free(p: aOut);
175476 pTS->aaOutput[i] = 0;
175477 aOut = aNew;
175478 nOut = nNew;
175479 }
175480 }
175481 }
175482
175483 pTS->aaOutput[0] = aOut;
175484 pTS->anOutput[0] = nOut;
175485 return SQLITE_OK;
175486}
175487
175488/*
175489** Merge the doclist aDoclist/nDoclist into the TermSelect object passed
175490** as the first argument. The merge is an "OR" merge (see function
175491** fts3DoclistOrMerge() for details).
175492**
175493** This function is called with the doclist for each term that matches
175494** a queried prefix. It merges all these doclists into one, the doclist
175495** for the specified prefix. Since there can be a very large number of
175496** doclists to merge, the merging is done pair-wise using the TermSelect
175497** object.
175498**
175499** This function returns SQLITE_OK if the merge is successful, or an
175500** SQLite error code (SQLITE_NOMEM) if an error occurs.
175501*/
175502static int fts3TermSelectMerge(
175503 Fts3Table *p, /* FTS table handle */
175504 TermSelect *pTS, /* TermSelect object to merge into */
175505 char *aDoclist, /* Pointer to doclist */
175506 int nDoclist /* Size of aDoclist in bytes */
175507){
175508 if( pTS->aaOutput[0]==0 ){
175509 /* If this is the first term selected, copy the doclist to the output
175510 ** buffer using memcpy().
175511 **
175512 ** Add FTS3_VARINT_MAX bytes of unused space to the end of the
175513 ** allocation. This is so as to ensure that the buffer is big enough
175514 ** to hold the current doclist AND'd with any other doclist. If the
175515 ** doclists are stored in order=ASC order, this padding would not be
175516 ** required (since the size of [doclistA AND doclistB] is always less
175517 ** than or equal to the size of [doclistA] in that case). But this is
175518 ** not true for order=DESC. For example, a doclist containing (1, -1)
175519 ** may be smaller than (-1), as in the first example the -1 may be stored
175520 ** as a single-byte delta, whereas in the second it must be stored as a
175521 ** FTS3_VARINT_MAX byte varint.
175522 **
175523 ** Similar padding is added in the fts3DoclistOrMerge() function.
175524 */
175525 pTS->aaOutput[0] = sqlite3_malloc(n: nDoclist + FTS3_VARINT_MAX + 1);
175526 pTS->anOutput[0] = nDoclist;
175527 if( pTS->aaOutput[0] ){
175528 memcpy(dest: pTS->aaOutput[0], src: aDoclist, n: nDoclist);
175529 memset(s: &pTS->aaOutput[0][nDoclist], c: 0, FTS3_VARINT_MAX);
175530 }else{
175531 return SQLITE_NOMEM;
175532 }
175533 }else{
175534 char *aMerge = aDoclist;
175535 int nMerge = nDoclist;
175536 int iOut;
175537
175538 for(iOut=0; iOut<SizeofArray(pTS->aaOutput); iOut++){
175539 if( pTS->aaOutput[iOut]==0 ){
175540 assert( iOut>0 );
175541 pTS->aaOutput[iOut] = aMerge;
175542 pTS->anOutput[iOut] = nMerge;
175543 break;
175544 }else{
175545 char *aNew;
175546 int nNew;
175547
175548 int rc = fts3DoclistOrMerge(bDescDoclist: p->bDescIdx, a1: aMerge, n1: nMerge,
175549 a2: pTS->aaOutput[iOut], n2: pTS->anOutput[iOut], paOut: &aNew, pnOut: &nNew
175550 );
175551 if( rc!=SQLITE_OK ){
175552 if( aMerge!=aDoclist ) sqlite3_free(p: aMerge);
175553 return rc;
175554 }
175555
175556 if( aMerge!=aDoclist ) sqlite3_free(p: aMerge);
175557 sqlite3_free(p: pTS->aaOutput[iOut]);
175558 pTS->aaOutput[iOut] = 0;
175559
175560 aMerge = aNew;
175561 nMerge = nNew;
175562 if( (iOut+1)==SizeofArray(pTS->aaOutput) ){
175563 pTS->aaOutput[iOut] = aMerge;
175564 pTS->anOutput[iOut] = nMerge;
175565 }
175566 }
175567 }
175568 }
175569 return SQLITE_OK;
175570}
175571
175572/*
175573** Append SegReader object pNew to the end of the pCsr->apSegment[] array.
175574*/
175575static int fts3SegReaderCursorAppend(
175576 Fts3MultiSegReader *pCsr,
175577 Fts3SegReader *pNew
175578){
175579 if( (pCsr->nSegment%16)==0 ){
175580 Fts3SegReader **apNew;
175581 sqlite3_int64 nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*);
175582 apNew = (Fts3SegReader **)sqlite3_realloc64(pOld: pCsr->apSegment, n: nByte);
175583 if( !apNew ){
175584 sqlite3Fts3SegReaderFree(pNew);
175585 return SQLITE_NOMEM;
175586 }
175587 pCsr->apSegment = apNew;
175588 }
175589 pCsr->apSegment[pCsr->nSegment++] = pNew;
175590 return SQLITE_OK;
175591}
175592
175593/*
175594** Add seg-reader objects to the Fts3MultiSegReader object passed as the
175595** 8th argument.
175596**
175597** This function returns SQLITE_OK if successful, or an SQLite error code
175598** otherwise.
175599*/
175600static int fts3SegReaderCursor(
175601 Fts3Table *p, /* FTS3 table handle */
175602 int iLangid, /* Language id */
175603 int iIndex, /* Index to search (from 0 to p->nIndex-1) */
175604 int iLevel, /* Level of segments to scan */
175605 const char *zTerm, /* Term to query for */
175606 int nTerm, /* Size of zTerm in bytes */
175607 int isPrefix, /* True for a prefix search */
175608 int isScan, /* True to scan from zTerm to EOF */
175609 Fts3MultiSegReader *pCsr /* Cursor object to populate */
175610){
175611 int rc = SQLITE_OK; /* Error code */
175612 sqlite3_stmt *pStmt = 0; /* Statement to iterate through segments */
175613 int rc2; /* Result of sqlite3_reset() */
175614
175615 /* If iLevel is less than 0 and this is not a scan, include a seg-reader
175616 ** for the pending-terms. If this is a scan, then this call must be being
175617 ** made by an fts4aux module, not an FTS table. In this case calling
175618 ** Fts3SegReaderPending might segfault, as the data structures used by
175619 ** fts4aux are not completely populated. So it's easiest to filter these
175620 ** calls out here. */
175621 if( iLevel<0 && p->aIndex && p->iPrevLangid==iLangid ){
175622 Fts3SegReader *pSeg = 0;
175623 rc = sqlite3Fts3SegReaderPending(p, iIndex, zTerm, nTerm, isPrefix||isScan, &pSeg);
175624 if( rc==SQLITE_OK && pSeg ){
175625 rc = fts3SegReaderCursorAppend(pCsr, pNew: pSeg);
175626 }
175627 }
175628
175629 if( iLevel!=FTS3_SEGCURSOR_PENDING ){
175630 if( rc==SQLITE_OK ){
175631 rc = sqlite3Fts3AllSegdirs(p, iLangid, iIndex, iLevel, &pStmt);
175632 }
175633
175634 while( rc==SQLITE_OK && SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
175635 Fts3SegReader *pSeg = 0;
175636
175637 /* Read the values returned by the SELECT into local variables. */
175638 sqlite3_int64 iStartBlock = sqlite3_column_int64(pStmt, i: 1);
175639 sqlite3_int64 iLeavesEndBlock = sqlite3_column_int64(pStmt, i: 2);
175640 sqlite3_int64 iEndBlock = sqlite3_column_int64(pStmt, i: 3);
175641 int nRoot = sqlite3_column_bytes(pStmt, i: 4);
175642 char const *zRoot = sqlite3_column_blob(pStmt, i: 4);
175643
175644 /* If zTerm is not NULL, and this segment is not stored entirely on its
175645 ** root node, the range of leaves scanned can be reduced. Do this. */
175646 if( iStartBlock && zTerm && zRoot ){
175647 sqlite3_int64 *pi = (isPrefix ? &iLeavesEndBlock : 0);
175648 rc = fts3SelectLeaf(p, zTerm, nTerm, zNode: zRoot, nNode: nRoot, piLeaf: &iStartBlock, piLeaf2: pi);
175649 if( rc!=SQLITE_OK ) goto finished;
175650 if( isPrefix==0 && isScan==0 ) iLeavesEndBlock = iStartBlock;
175651 }
175652
175653 rc = sqlite3Fts3SegReaderNew(pCsr->nSegment+1,
175654 (isPrefix==0 && isScan==0),
175655 iStartBlock, iLeavesEndBlock,
175656 iEndBlock, zRoot, nRoot, &pSeg
175657 );
175658 if( rc!=SQLITE_OK ) goto finished;
175659 rc = fts3SegReaderCursorAppend(pCsr, pNew: pSeg);
175660 }
175661 }
175662
175663 finished:
175664 rc2 = sqlite3_reset(pStmt);
175665 if( rc==SQLITE_DONE ) rc = rc2;
175666
175667 return rc;
175668}
175669
175670/*
175671** Set up a cursor object for iterating through a full-text index or a
175672** single level therein.
175673*/
175674SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(
175675 Fts3Table *p, /* FTS3 table handle */
175676 int iLangid, /* Language-id to search */
175677 int iIndex, /* Index to search (from 0 to p->nIndex-1) */
175678 int iLevel, /* Level of segments to scan */
175679 const char *zTerm, /* Term to query for */
175680 int nTerm, /* Size of zTerm in bytes */
175681 int isPrefix, /* True for a prefix search */
175682 int isScan, /* True to scan from zTerm to EOF */
175683 Fts3MultiSegReader *pCsr /* Cursor object to populate */
175684){
175685 assert( iIndex>=0 && iIndex<p->nIndex );
175686 assert( iLevel==FTS3_SEGCURSOR_ALL
175687 || iLevel==FTS3_SEGCURSOR_PENDING
175688 || iLevel>=0
175689 );
175690 assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
175691 assert( FTS3_SEGCURSOR_ALL<0 && FTS3_SEGCURSOR_PENDING<0 );
175692 assert( isPrefix==0 || isScan==0 );
175693
175694 memset(s: pCsr, c: 0, n: sizeof(Fts3MultiSegReader));
175695 return fts3SegReaderCursor(
175696 p, iLangid, iIndex, iLevel, zTerm, nTerm, isPrefix, isScan, pCsr
175697 );
175698}
175699
175700/*
175701** In addition to its current configuration, have the Fts3MultiSegReader
175702** passed as the 4th argument also scan the doclist for term zTerm/nTerm.
175703**
175704** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
175705*/
175706static int fts3SegReaderCursorAddZero(
175707 Fts3Table *p, /* FTS virtual table handle */
175708 int iLangid,
175709 const char *zTerm, /* Term to scan doclist of */
175710 int nTerm, /* Number of bytes in zTerm */
175711 Fts3MultiSegReader *pCsr /* Fts3MultiSegReader to modify */
175712){
175713 return fts3SegReaderCursor(p,
175714 iLangid, iIndex: 0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, isPrefix: 0, isScan: 0,pCsr
175715 );
175716}
175717
175718/*
175719** Open an Fts3MultiSegReader to scan the doclist for term zTerm/nTerm. Or,
175720** if isPrefix is true, to scan the doclist for all terms for which
175721** zTerm/nTerm is a prefix. If successful, return SQLITE_OK and write
175722** a pointer to the new Fts3MultiSegReader to *ppSegcsr. Otherwise, return
175723** an SQLite error code.
175724**
175725** It is the responsibility of the caller to free this object by eventually
175726** passing it to fts3SegReaderCursorFree()
175727**
175728** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
175729** Output parameter *ppSegcsr is set to 0 if an error occurs.
175730*/
175731static int fts3TermSegReaderCursor(
175732 Fts3Cursor *pCsr, /* Virtual table cursor handle */
175733 const char *zTerm, /* Term to query for */
175734 int nTerm, /* Size of zTerm in bytes */
175735 int isPrefix, /* True for a prefix search */
175736 Fts3MultiSegReader **ppSegcsr /* OUT: Allocated seg-reader cursor */
175737){
175738 Fts3MultiSegReader *pSegcsr; /* Object to allocate and return */
175739 int rc = SQLITE_NOMEM; /* Return code */
175740
175741 pSegcsr = sqlite3_malloc(n: sizeof(Fts3MultiSegReader));
175742 if( pSegcsr ){
175743 int i;
175744 int bFound = 0; /* True once an index has been found */
175745 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
175746
175747 if( isPrefix ){
175748 for(i=1; bFound==0 && i<p->nIndex; i++){
175749 if( p->aIndex[i].nPrefix==nTerm ){
175750 bFound = 1;
175751 rc = sqlite3Fts3SegReaderCursor(p, iLangid: pCsr->iLangid,
175752 iIndex: i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, isPrefix: 0, isScan: 0, pCsr: pSegcsr
175753 );
175754 pSegcsr->bLookup = 1;
175755 }
175756 }
175757
175758 for(i=1; bFound==0 && i<p->nIndex; i++){
175759 if( p->aIndex[i].nPrefix==nTerm+1 ){
175760 bFound = 1;
175761 rc = sqlite3Fts3SegReaderCursor(p, iLangid: pCsr->iLangid,
175762 iIndex: i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, isPrefix: 1, isScan: 0, pCsr: pSegcsr
175763 );
175764 if( rc==SQLITE_OK ){
175765 rc = fts3SegReaderCursorAddZero(
175766 p, iLangid: pCsr->iLangid, zTerm, nTerm, pCsr: pSegcsr
175767 );
175768 }
175769 }
175770 }
175771 }
175772
175773 if( bFound==0 ){
175774 rc = sqlite3Fts3SegReaderCursor(p, iLangid: pCsr->iLangid,
175775 iIndex: 0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, isPrefix, isScan: 0, pCsr: pSegcsr
175776 );
175777 pSegcsr->bLookup = !isPrefix;
175778 }
175779 }
175780
175781 *ppSegcsr = pSegcsr;
175782 return rc;
175783}
175784
175785/*
175786** Free an Fts3MultiSegReader allocated by fts3TermSegReaderCursor().
175787*/
175788static void fts3SegReaderCursorFree(Fts3MultiSegReader *pSegcsr){
175789 sqlite3Fts3SegReaderFinish(pSegcsr);
175790 sqlite3_free(p: pSegcsr);
175791}
175792
175793/*
175794** This function retrieves the doclist for the specified term (or term
175795** prefix) from the database.
175796*/
175797static int fts3TermSelect(
175798 Fts3Table *p, /* Virtual table handle */
175799 Fts3PhraseToken *pTok, /* Token to query for */
175800 int iColumn, /* Column to query (or -ve for all columns) */
175801 int *pnOut, /* OUT: Size of buffer at *ppOut */
175802 char **ppOut /* OUT: Malloced result buffer */
175803){
175804 int rc; /* Return code */
175805 Fts3MultiSegReader *pSegcsr; /* Seg-reader cursor for this term */
175806 TermSelect tsc; /* Object for pair-wise doclist merging */
175807 Fts3SegFilter filter; /* Segment term filter configuration */
175808
175809 pSegcsr = pTok->pSegcsr;
175810 memset(s: &tsc, c: 0, n: sizeof(TermSelect));
175811
175812 filter.flags = FTS3_SEGMENT_IGNORE_EMPTY | FTS3_SEGMENT_REQUIRE_POS
175813 | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX : 0)
175814 | (pTok->bFirst ? FTS3_SEGMENT_FIRST : 0)
175815 | (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER : 0);
175816 filter.iCol = iColumn;
175817 filter.zTerm = pTok->z;
175818 filter.nTerm = pTok->n;
175819
175820 rc = sqlite3Fts3SegReaderStart(p, pSegcsr, &filter);
175821 while( SQLITE_OK==rc
175822 && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pSegcsr))
175823 ){
175824 rc = fts3TermSelectMerge(p, pTS: &tsc, aDoclist: pSegcsr->aDoclist, nDoclist: pSegcsr->nDoclist);
175825 }
175826
175827 if( rc==SQLITE_OK ){
175828 rc = fts3TermSelectFinishMerge(p, pTS: &tsc);
175829 }
175830 if( rc==SQLITE_OK ){
175831 *ppOut = tsc.aaOutput[0];
175832 *pnOut = tsc.anOutput[0];
175833 }else{
175834 int i;
175835 for(i=0; i<SizeofArray(tsc.aaOutput); i++){
175836 sqlite3_free(p: tsc.aaOutput[i]);
175837 }
175838 }
175839
175840 fts3SegReaderCursorFree(pSegcsr);
175841 pTok->pSegcsr = 0;
175842 return rc;
175843}
175844
175845/*
175846** This function counts the total number of docids in the doclist stored
175847** in buffer aList[], size nList bytes.
175848**
175849** If the isPoslist argument is true, then it is assumed that the doclist
175850** contains a position-list following each docid. Otherwise, it is assumed
175851** that the doclist is simply a list of docids stored as delta encoded
175852** varints.
175853*/
175854static int fts3DoclistCountDocids(char *aList, int nList){
175855 int nDoc = 0; /* Return value */
175856 if( aList ){
175857 char *aEnd = &aList[nList]; /* Pointer to one byte after EOF */
175858 char *p = aList; /* Cursor */
175859 while( p<aEnd ){
175860 nDoc++;
175861 while( (*p++)&0x80 ); /* Skip docid varint */
175862 fts3PoslistCopy(pp: 0, ppPoslist: &p); /* Skip over position list */
175863 }
175864 }
175865
175866 return nDoc;
175867}
175868
175869/*
175870** Advance the cursor to the next row in the %_content table that
175871** matches the search criteria. For a MATCH search, this will be
175872** the next row that matches. For a full-table scan, this will be
175873** simply the next row in the %_content table. For a docid lookup,
175874** this routine simply sets the EOF flag.
175875**
175876** Return SQLITE_OK if nothing goes wrong. SQLITE_OK is returned
175877** even if we reach end-of-file. The fts3EofMethod() will be called
175878** subsequently to determine whether or not an EOF was hit.
175879*/
175880static int fts3NextMethod(sqlite3_vtab_cursor *pCursor){
175881 int rc;
175882 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
175883 if( pCsr->eSearch==FTS3_DOCID_SEARCH || pCsr->eSearch==FTS3_FULLSCAN_SEARCH ){
175884 Fts3Table *pTab = (Fts3Table*)pCursor->pVtab;
175885 pTab->bLock++;
175886 if( SQLITE_ROW!=sqlite3_step(pStmt: pCsr->pStmt) ){
175887 pCsr->isEof = 1;
175888 rc = sqlite3_reset(pStmt: pCsr->pStmt);
175889 }else{
175890 pCsr->iPrevId = sqlite3_column_int64(pStmt: pCsr->pStmt, i: 0);
175891 rc = SQLITE_OK;
175892 }
175893 pTab->bLock--;
175894 }else{
175895 rc = fts3EvalNext(pCsr: (Fts3Cursor *)pCursor);
175896 }
175897 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
175898 return rc;
175899}
175900
175901/*
175902** If the numeric type of argument pVal is "integer", then return it
175903** converted to a 64-bit signed integer. Otherwise, return a copy of
175904** the second parameter, iDefault.
175905*/
175906static sqlite3_int64 fts3DocidRange(sqlite3_value *pVal, i64 iDefault){
175907 if( pVal ){
175908 int eType = sqlite3_value_numeric_type(pVal);
175909 if( eType==SQLITE_INTEGER ){
175910 return sqlite3_value_int64(pVal);
175911 }
175912 }
175913 return iDefault;
175914}
175915
175916/*
175917** This is the xFilter interface for the virtual table. See
175918** the virtual table xFilter method documentation for additional
175919** information.
175920**
175921** If idxNum==FTS3_FULLSCAN_SEARCH then do a full table scan against
175922** the %_content table.
175923**
175924** If idxNum==FTS3_DOCID_SEARCH then do a docid lookup for a single entry
175925** in the %_content table.
175926**
175927** If idxNum>=FTS3_FULLTEXT_SEARCH then use the full text index. The
175928** column on the left-hand side of the MATCH operator is column
175929** number idxNum-FTS3_FULLTEXT_SEARCH, 0 indexed. argv[0] is the right-hand
175930** side of the MATCH operator.
175931*/
175932static int fts3FilterMethod(
175933 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
175934 int idxNum, /* Strategy index */
175935 const char *idxStr, /* Unused */
175936 int nVal, /* Number of elements in apVal */
175937 sqlite3_value **apVal /* Arguments for the indexing scheme */
175938){
175939 int rc = SQLITE_OK;
175940 char *zSql; /* SQL statement used to access %_content */
175941 int eSearch;
175942 Fts3Table *p = (Fts3Table *)pCursor->pVtab;
175943 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
175944
175945 sqlite3_value *pCons = 0; /* The MATCH or rowid constraint, if any */
175946 sqlite3_value *pLangid = 0; /* The "langid = ?" constraint, if any */
175947 sqlite3_value *pDocidGe = 0; /* The "docid >= ?" constraint, if any */
175948 sqlite3_value *pDocidLe = 0; /* The "docid <= ?" constraint, if any */
175949 int iIdx;
175950
175951 UNUSED_PARAMETER(idxStr);
175952 UNUSED_PARAMETER(nVal);
175953
175954 if( p->bLock ){
175955 return SQLITE_ERROR;
175956 }
175957
175958 eSearch = (idxNum & 0x0000FFFF);
175959 assert( eSearch>=0 && eSearch<=(FTS3_FULLTEXT_SEARCH+p->nColumn) );
175960 assert( p->pSegments==0 );
175961
175962 /* Collect arguments into local variables */
175963 iIdx = 0;
175964 if( eSearch!=FTS3_FULLSCAN_SEARCH ) pCons = apVal[iIdx++];
175965 if( idxNum & FTS3_HAVE_LANGID ) pLangid = apVal[iIdx++];
175966 if( idxNum & FTS3_HAVE_DOCID_GE ) pDocidGe = apVal[iIdx++];
175967 if( idxNum & FTS3_HAVE_DOCID_LE ) pDocidLe = apVal[iIdx++];
175968 assert( iIdx==nVal );
175969
175970 /* In case the cursor has been used before, clear it now. */
175971 fts3ClearCursor(pCsr);
175972
175973 /* Set the lower and upper bounds on docids to return */
175974 pCsr->iMinDocid = fts3DocidRange(pVal: pDocidGe, SMALLEST_INT64);
175975 pCsr->iMaxDocid = fts3DocidRange(pVal: pDocidLe, LARGEST_INT64);
175976
175977 if( idxStr ){
175978 pCsr->bDesc = (idxStr[0]=='D');
175979 }else{
175980 pCsr->bDesc = p->bDescIdx;
175981 }
175982 pCsr->eSearch = (i16)eSearch;
175983
175984 if( eSearch!=FTS3_DOCID_SEARCH && eSearch!=FTS3_FULLSCAN_SEARCH ){
175985 int iCol = eSearch-FTS3_FULLTEXT_SEARCH;
175986 const char *zQuery = (const char *)sqlite3_value_text(pVal: pCons);
175987
175988 if( zQuery==0 && sqlite3_value_type(pVal: pCons)!=SQLITE_NULL ){
175989 return SQLITE_NOMEM;
175990 }
175991
175992 pCsr->iLangid = 0;
175993 if( pLangid ) pCsr->iLangid = sqlite3_value_int(pVal: pLangid);
175994
175995 assert( p->base.zErrMsg==0 );
175996 rc = sqlite3Fts3ExprParse(p->pTokenizer, pCsr->iLangid,
175997 p->azColumn, p->bFts4, p->nColumn, iCol, zQuery, -1, &pCsr->pExpr,
175998 &p->base.zErrMsg
175999 );
176000 if( rc!=SQLITE_OK ){
176001 return rc;
176002 }
176003
176004 rc = fts3EvalStart(pCsr);
176005 sqlite3Fts3SegmentsClose(p);
176006 if( rc!=SQLITE_OK ) return rc;
176007 pCsr->pNextId = pCsr->aDoclist;
176008 pCsr->iPrevId = 0;
176009 }
176010
176011 /* Compile a SELECT statement for this cursor. For a full-table-scan, the
176012 ** statement loops through all rows of the %_content table. For a
176013 ** full-text query or docid lookup, the statement retrieves a single
176014 ** row by docid.
176015 */
176016 if( eSearch==FTS3_FULLSCAN_SEARCH ){
176017 if( pDocidGe || pDocidLe ){
176018 zSql = sqlite3_mprintf(
176019 zFormat: "SELECT %s WHERE rowid BETWEEN %lld AND %lld ORDER BY rowid %s",
176020 p->zReadExprlist, pCsr->iMinDocid, pCsr->iMaxDocid,
176021 (pCsr->bDesc ? "DESC" : "ASC")
176022 );
176023 }else{
176024 zSql = sqlite3_mprintf(zFormat: "SELECT %s ORDER BY rowid %s",
176025 p->zReadExprlist, (pCsr->bDesc ? "DESC" : "ASC")
176026 );
176027 }
176028 if( zSql ){
176029 p->bLock++;
176030 rc = sqlite3_prepare_v3(
176031 db: p->db,zSql,nBytes: -1,SQLITE_PREPARE_PERSISTENT,ppStmt: &pCsr->pStmt,pzTail: 0
176032 );
176033 p->bLock--;
176034 sqlite3_free(p: zSql);
176035 }else{
176036 rc = SQLITE_NOMEM;
176037 }
176038 }else if( eSearch==FTS3_DOCID_SEARCH ){
176039 rc = fts3CursorSeekStmt(pCsr);
176040 if( rc==SQLITE_OK ){
176041 rc = sqlite3_bind_value(pStmt: pCsr->pStmt, i: 1, pValue: pCons);
176042 }
176043 }
176044 if( rc!=SQLITE_OK ) return rc;
176045
176046 return fts3NextMethod(pCursor);
176047}
176048
176049/*
176050** This is the xEof method of the virtual table. SQLite calls this
176051** routine to find out if it has reached the end of a result set.
176052*/
176053static int fts3EofMethod(sqlite3_vtab_cursor *pCursor){
176054 Fts3Cursor *pCsr = (Fts3Cursor*)pCursor;
176055 if( pCsr->isEof ){
176056 fts3ClearCursor(pCsr);
176057 pCsr->isEof = 1;
176058 }
176059 return pCsr->isEof;
176060}
176061
176062/*
176063** This is the xRowid method. The SQLite core calls this routine to
176064** retrieve the rowid for the current row of the result set. fts3
176065** exposes %_content.docid as the rowid for the virtual table. The
176066** rowid should be written to *pRowid.
176067*/
176068static int fts3RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
176069 Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
176070 *pRowid = pCsr->iPrevId;
176071 return SQLITE_OK;
176072}
176073
176074/*
176075** This is the xColumn method, called by SQLite to request a value from
176076** the row that the supplied cursor currently points to.
176077**
176078** If:
176079**
176080** (iCol < p->nColumn) -> The value of the iCol'th user column.
176081** (iCol == p->nColumn) -> Magic column with the same name as the table.
176082** (iCol == p->nColumn+1) -> Docid column
176083** (iCol == p->nColumn+2) -> Langid column
176084*/
176085static int fts3ColumnMethod(
176086 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
176087 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
176088 int iCol /* Index of column to read value from */
176089){
176090 int rc = SQLITE_OK; /* Return Code */
176091 Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
176092 Fts3Table *p = (Fts3Table *)pCursor->pVtab;
176093
176094 /* The column value supplied by SQLite must be in range. */
176095 assert( iCol>=0 && iCol<=p->nColumn+2 );
176096
176097 switch( iCol-p->nColumn ){
176098 case 0:
176099 /* The special 'table-name' column */
176100 sqlite3_result_pointer(pCtx, pPtr: pCsr, zPType: "fts3cursor", xDestructor: 0);
176101 break;
176102
176103 case 1:
176104 /* The docid column */
176105 sqlite3_result_int64(pCtx, iVal: pCsr->iPrevId);
176106 break;
176107
176108 case 2:
176109 if( pCsr->pExpr ){
176110 sqlite3_result_int64(pCtx, iVal: pCsr->iLangid);
176111 break;
176112 }else if( p->zLanguageid==0 ){
176113 sqlite3_result_int(pCtx, iVal: 0);
176114 break;
176115 }else{
176116 iCol = p->nColumn;
176117 /* no break */ deliberate_fall_through
176118 }
176119
176120 default:
176121 /* A user column. Or, if this is a full-table scan, possibly the
176122 ** language-id column. Seek the cursor. */
176123 rc = fts3CursorSeek(pContext: 0, pCsr);
176124 if( rc==SQLITE_OK && sqlite3_data_count(pStmt: pCsr->pStmt)-1>iCol ){
176125 sqlite3_result_value(pCtx, pValue: sqlite3_column_value(pStmt: pCsr->pStmt, i: iCol+1));
176126 }
176127 break;
176128 }
176129
176130 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
176131 return rc;
176132}
176133
176134/*
176135** This function is the implementation of the xUpdate callback used by
176136** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
176137** inserted, updated or deleted.
176138*/
176139static int fts3UpdateMethod(
176140 sqlite3_vtab *pVtab, /* Virtual table handle */
176141 int nArg, /* Size of argument array */
176142 sqlite3_value **apVal, /* Array of arguments */
176143 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
176144){
176145 return sqlite3Fts3UpdateMethod(pVtab, nArg, apVal, pRowid);
176146}
176147
176148/*
176149** Implementation of xSync() method. Flush the contents of the pending-terms
176150** hash-table to the database.
176151*/
176152static int fts3SyncMethod(sqlite3_vtab *pVtab){
176153
176154 /* Following an incremental-merge operation, assuming that the input
176155 ** segments are not completely consumed (the usual case), they are updated
176156 ** in place to remove the entries that have already been merged. This
176157 ** involves updating the leaf block that contains the smallest unmerged
176158 ** entry and each block (if any) between the leaf and the root node. So
176159 ** if the height of the input segment b-trees is N, and input segments
176160 ** are merged eight at a time, updating the input segments at the end
176161 ** of an incremental-merge requires writing (8*(1+N)) blocks. N is usually
176162 ** small - often between 0 and 2. So the overhead of the incremental
176163 ** merge is somewhere between 8 and 24 blocks. To avoid this overhead
176164 ** dwarfing the actual productive work accomplished, the incremental merge
176165 ** is only attempted if it will write at least 64 leaf blocks. Hence
176166 ** nMinMerge.
176167 **
176168 ** Of course, updating the input segments also involves deleting a bunch
176169 ** of blocks from the segments table. But this is not considered overhead
176170 ** as it would also be required by a crisis-merge that used the same input
176171 ** segments.
176172 */
176173 const u32 nMinMerge = 64; /* Minimum amount of incr-merge work to do */
176174
176175 Fts3Table *p = (Fts3Table*)pVtab;
176176 int rc;
176177 i64 iLastRowid = sqlite3_last_insert_rowid(db: p->db);
176178
176179 rc = sqlite3Fts3PendingTermsFlush(p);
176180 if( rc==SQLITE_OK
176181 && p->nLeafAdd>(nMinMerge/16)
176182 && p->nAutoincrmerge && p->nAutoincrmerge!=0xff
176183 ){
176184 int mxLevel = 0; /* Maximum relative level value in db */
176185 int A; /* Incr-merge parameter A */
176186
176187 rc = sqlite3Fts3MaxLevel(p, &mxLevel);
176188 assert( rc==SQLITE_OK || mxLevel==0 );
176189 A = p->nLeafAdd * mxLevel;
176190 A += (A/2);
176191 if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, p->nAutoincrmerge);
176192 }
176193 sqlite3Fts3SegmentsClose(p);
176194 sqlite3_set_last_insert_rowid(db: p->db, iRowid: iLastRowid);
176195 return rc;
176196}
176197
176198/*
176199** If it is currently unknown whether or not the FTS table has an %_stat
176200** table (if p->bHasStat==2), attempt to determine this (set p->bHasStat
176201** to 0 or 1). Return SQLITE_OK if successful, or an SQLite error code
176202** if an error occurs.
176203*/
176204static int fts3SetHasStat(Fts3Table *p){
176205 int rc = SQLITE_OK;
176206 if( p->bHasStat==2 ){
176207 char *zTbl = sqlite3_mprintf(zFormat: "%s_stat", p->zName);
176208 if( zTbl ){
176209 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);
176210 sqlite3_free(p: zTbl);
176211 p->bHasStat = (res==SQLITE_OK);
176212 }else{
176213 rc = SQLITE_NOMEM;
176214 }
176215 }
176216 return rc;
176217}
176218
176219/*
176220** Implementation of xBegin() method.
176221*/
176222static int fts3BeginMethod(sqlite3_vtab *pVtab){
176223 Fts3Table *p = (Fts3Table*)pVtab;
176224 int rc;
176225 UNUSED_PARAMETER(pVtab);
176226 assert( p->pSegments==0 );
176227 assert( p->nPendingData==0 );
176228 assert( p->inTransaction!=1 );
176229 p->nLeafAdd = 0;
176230 rc = fts3SetHasStat(p);
176231#ifdef SQLITE_DEBUG
176232 if( rc==SQLITE_OK ){
176233 p->inTransaction = 1;
176234 p->mxSavepoint = -1;
176235 }
176236#endif
176237 return rc;
176238}
176239
176240/*
176241** Implementation of xCommit() method. This is a no-op. The contents of
176242** the pending-terms hash-table have already been flushed into the database
176243** by fts3SyncMethod().
176244*/
176245static int fts3CommitMethod(sqlite3_vtab *pVtab){
176246 TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
176247 UNUSED_PARAMETER(pVtab);
176248 assert( p->nPendingData==0 );
176249 assert( p->inTransaction!=0 );
176250 assert( p->pSegments==0 );
176251 TESTONLY( p->inTransaction = 0 );
176252 TESTONLY( p->mxSavepoint = -1; );
176253 return SQLITE_OK;
176254}
176255
176256/*
176257** Implementation of xRollback(). Discard the contents of the pending-terms
176258** hash-table. Any changes made to the database are reverted by SQLite.
176259*/
176260static int fts3RollbackMethod(sqlite3_vtab *pVtab){
176261 Fts3Table *p = (Fts3Table*)pVtab;
176262 sqlite3Fts3PendingTermsClear(p);
176263 assert( p->inTransaction!=0 );
176264 TESTONLY( p->inTransaction = 0 );
176265 TESTONLY( p->mxSavepoint = -1; );
176266 return SQLITE_OK;
176267}
176268
176269/*
176270** When called, *ppPoslist must point to the byte immediately following the
176271** end of a position-list. i.e. ( (*ppPoslist)[-1]==POS_END ). This function
176272** moves *ppPoslist so that it instead points to the first byte of the
176273** same position list.
176274*/
176275static void fts3ReversePoslist(char *pStart, char **ppPoslist){
176276 char *p = &(*ppPoslist)[-2];
176277 char c = 0;
176278
176279 /* Skip backwards passed any trailing 0x00 bytes added by NearTrim() */
176280 while( p>pStart && (c=*p--)==0 );
176281
176282 /* Search backwards for a varint with value zero (the end of the previous
176283 ** poslist). This is an 0x00 byte preceded by some byte that does not
176284 ** have the 0x80 bit set. */
176285 while( p>pStart && (*p & 0x80) | c ){
176286 c = *p--;
176287 }
176288 assert( p==pStart || c==0 );
176289
176290 /* At this point p points to that preceding byte without the 0x80 bit
176291 ** set. So to find the start of the poslist, skip forward 2 bytes then
176292 ** over a varint.
176293 **
176294 ** Normally. The other case is that p==pStart and the poslist to return
176295 ** is the first in the doclist. In this case do not skip forward 2 bytes.
176296 ** The second part of the if condition (c==0 && *ppPoslist>&p[2])
176297 ** is required for cases where the first byte of a doclist and the
176298 ** doclist is empty. For example, if the first docid is 10, a doclist
176299 ** that begins with:
176300 **
176301 ** 0x0A 0x00 <next docid delta varint>
176302 */
176303 if( p>pStart || (c==0 && *ppPoslist>&p[2]) ){ p = &p[2]; }
176304 while( *p++&0x80 );
176305 *ppPoslist = p;
176306}
176307
176308/*
176309** Helper function used by the implementation of the overloaded snippet(),
176310** offsets() and optimize() SQL functions.
176311**
176312** If the value passed as the third argument is a blob of size
176313** sizeof(Fts3Cursor*), then the blob contents are copied to the
176314** output variable *ppCsr and SQLITE_OK is returned. Otherwise, an error
176315** message is written to context pContext and SQLITE_ERROR returned. The
176316** string passed via zFunc is used as part of the error message.
176317*/
176318static int fts3FunctionArg(
176319 sqlite3_context *pContext, /* SQL function call context */
176320 const char *zFunc, /* Function name */
176321 sqlite3_value *pVal, /* argv[0] passed to function */
176322 Fts3Cursor **ppCsr /* OUT: Store cursor handle here */
176323){
176324 int rc;
176325 *ppCsr = (Fts3Cursor*)sqlite3_value_pointer(pVal, zPType: "fts3cursor");
176326 if( (*ppCsr)!=0 ){
176327 rc = SQLITE_OK;
176328 }else{
176329 char *zErr = sqlite3_mprintf(zFormat: "illegal first argument to %s", zFunc);
176330 sqlite3_result_error(pCtx: pContext, z: zErr, n: -1);
176331 sqlite3_free(p: zErr);
176332 rc = SQLITE_ERROR;
176333 }
176334 return rc;
176335}
176336
176337/*
176338** Implementation of the snippet() function for FTS3
176339*/
176340static void fts3SnippetFunc(
176341 sqlite3_context *pContext, /* SQLite function call context */
176342 int nVal, /* Size of apVal[] array */
176343 sqlite3_value **apVal /* Array of arguments */
176344){
176345 Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
176346 const char *zStart = "<b>";
176347 const char *zEnd = "</b>";
176348 const char *zEllipsis = "<b>...</b>";
176349 int iCol = -1;
176350 int nToken = 15; /* Default number of tokens in snippet */
176351
176352 /* There must be at least one argument passed to this function (otherwise
176353 ** the non-overloaded version would have been called instead of this one).
176354 */
176355 assert( nVal>=1 );
176356
176357 if( nVal>6 ){
176358 sqlite3_result_error(pCtx: pContext,
176359 z: "wrong number of arguments to function snippet()", n: -1);
176360 return;
176361 }
176362 if( fts3FunctionArg(pContext, zFunc: "snippet", pVal: apVal[0], ppCsr: &pCsr) ) return;
176363
176364 switch( nVal ){
176365 case 6: nToken = sqlite3_value_int(pVal: apVal[5]);
176366 /* no break */ deliberate_fall_through
176367 case 5: iCol = sqlite3_value_int(pVal: apVal[4]);
176368 /* no break */ deliberate_fall_through
176369 case 4: zEllipsis = (const char*)sqlite3_value_text(pVal: apVal[3]);
176370 /* no break */ deliberate_fall_through
176371 case 3: zEnd = (const char*)sqlite3_value_text(pVal: apVal[2]);
176372 /* no break */ deliberate_fall_through
176373 case 2: zStart = (const char*)sqlite3_value_text(pVal: apVal[1]);
176374 }
176375 if( !zEllipsis || !zEnd || !zStart ){
176376 sqlite3_result_error_nomem(pCtx: pContext);
176377 }else if( nToken==0 ){
176378 sqlite3_result_text(pCtx: pContext, z: "", n: -1, SQLITE_STATIC);
176379 }else if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
176380 sqlite3Fts3Snippet(pContext, pCsr, zStart, zEnd, zEllipsis, iCol, nToken);
176381 }
176382}
176383
176384/*
176385** Implementation of the offsets() function for FTS3
176386*/
176387static void fts3OffsetsFunc(
176388 sqlite3_context *pContext, /* SQLite function call context */
176389 int nVal, /* Size of argument array */
176390 sqlite3_value **apVal /* Array of arguments */
176391){
176392 Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
176393
176394 UNUSED_PARAMETER(nVal);
176395
176396 assert( nVal==1 );
176397 if( fts3FunctionArg(pContext, zFunc: "offsets", pVal: apVal[0], ppCsr: &pCsr) ) return;
176398 assert( pCsr );
176399 if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
176400 sqlite3Fts3Offsets(pContext, pCsr);
176401 }
176402}
176403
176404/*
176405** Implementation of the special optimize() function for FTS3. This
176406** function merges all segments in the database to a single segment.
176407** Example usage is:
176408**
176409** SELECT optimize(t) FROM t LIMIT 1;
176410**
176411** where 't' is the name of an FTS3 table.
176412*/
176413static void fts3OptimizeFunc(
176414 sqlite3_context *pContext, /* SQLite function call context */
176415 int nVal, /* Size of argument array */
176416 sqlite3_value **apVal /* Array of arguments */
176417){
176418 int rc; /* Return code */
176419 Fts3Table *p; /* Virtual table handle */
176420 Fts3Cursor *pCursor; /* Cursor handle passed through apVal[0] */
176421
176422 UNUSED_PARAMETER(nVal);
176423
176424 assert( nVal==1 );
176425 if( fts3FunctionArg(pContext, zFunc: "optimize", pVal: apVal[0], ppCsr: &pCursor) ) return;
176426 p = (Fts3Table *)pCursor->base.pVtab;
176427 assert( p );
176428
176429 rc = sqlite3Fts3Optimize(p);
176430
176431 switch( rc ){
176432 case SQLITE_OK:
176433 sqlite3_result_text(pCtx: pContext, z: "Index optimized", n: -1, SQLITE_STATIC);
176434 break;
176435 case SQLITE_DONE:
176436 sqlite3_result_text(pCtx: pContext, z: "Index already optimal", n: -1, SQLITE_STATIC);
176437 break;
176438 default:
176439 sqlite3_result_error_code(pCtx: pContext, errCode: rc);
176440 break;
176441 }
176442}
176443
176444/*
176445** Implementation of the matchinfo() function for FTS3
176446*/
176447static void fts3MatchinfoFunc(
176448 sqlite3_context *pContext, /* SQLite function call context */
176449 int nVal, /* Size of argument array */
176450 sqlite3_value **apVal /* Array of arguments */
176451){
176452 Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
176453 assert( nVal==1 || nVal==2 );
176454 if( SQLITE_OK==fts3FunctionArg(pContext, zFunc: "matchinfo", pVal: apVal[0], ppCsr: &pCsr) ){
176455 const char *zArg = 0;
176456 if( nVal>1 ){
176457 zArg = (const char *)sqlite3_value_text(pVal: apVal[1]);
176458 }
176459 sqlite3Fts3Matchinfo(pContext, pCsr, zArg);
176460 }
176461}
176462
176463/*
176464** This routine implements the xFindFunction method for the FTS3
176465** virtual table.
176466*/
176467static int fts3FindFunctionMethod(
176468 sqlite3_vtab *pVtab, /* Virtual table handle */
176469 int nArg, /* Number of SQL function arguments */
176470 const char *zName, /* Name of SQL function */
176471 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
176472 void **ppArg /* Unused */
176473){
176474 struct Overloaded {
176475 const char *zName;
176476 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
176477 } aOverload[] = {
176478 { "snippet", fts3SnippetFunc },
176479 { "offsets", fts3OffsetsFunc },
176480 { "optimize", fts3OptimizeFunc },
176481 { "matchinfo", fts3MatchinfoFunc },
176482 };
176483 int i; /* Iterator variable */
176484
176485 UNUSED_PARAMETER(pVtab);
176486 UNUSED_PARAMETER(nArg);
176487 UNUSED_PARAMETER(ppArg);
176488
176489 for(i=0; i<SizeofArray(aOverload); i++){
176490 if( strcmp(s1: zName, s2: aOverload[i].zName)==0 ){
176491 *pxFunc = aOverload[i].xFunc;
176492 return 1;
176493 }
176494 }
176495
176496 /* No function of the specified name was found. Return 0. */
176497 return 0;
176498}
176499
176500/*
176501** Implementation of FTS3 xRename method. Rename an fts3 table.
176502*/
176503static int fts3RenameMethod(
176504 sqlite3_vtab *pVtab, /* Virtual table handle */
176505 const char *zName /* New name of table */
176506){
176507 Fts3Table *p = (Fts3Table *)pVtab;
176508 sqlite3 *db = p->db; /* Database connection */
176509 int rc; /* Return Code */
176510
176511 /* At this point it must be known if the %_stat table exists or not.
176512 ** So bHasStat may not be 2. */
176513 rc = fts3SetHasStat(p);
176514
176515 /* As it happens, the pending terms table is always empty here. This is
176516 ** because an "ALTER TABLE RENAME TABLE" statement inside a transaction
176517 ** always opens a savepoint transaction. And the xSavepoint() method
176518 ** flushes the pending terms table. But leave the (no-op) call to
176519 ** PendingTermsFlush() in in case that changes.
176520 */
176521 assert( p->nPendingData==0 );
176522 if( rc==SQLITE_OK ){
176523 rc = sqlite3Fts3PendingTermsFlush(p);
176524 }
176525
176526 if( p->zContentTbl==0 ){
176527 fts3DbExec(pRc: &rc, db,
176528 zFormat: "ALTER TABLE %Q.'%q_content' RENAME TO '%q_content';",
176529 p->zDb, p->zName, zName
176530 );
176531 }
176532
176533 if( p->bHasDocsize ){
176534 fts3DbExec(pRc: &rc, db,
176535 zFormat: "ALTER TABLE %Q.'%q_docsize' RENAME TO '%q_docsize';",
176536 p->zDb, p->zName, zName
176537 );
176538 }
176539 if( p->bHasStat ){
176540 fts3DbExec(pRc: &rc, db,
176541 zFormat: "ALTER TABLE %Q.'%q_stat' RENAME TO '%q_stat';",
176542 p->zDb, p->zName, zName
176543 );
176544 }
176545 fts3DbExec(pRc: &rc, db,
176546 zFormat: "ALTER TABLE %Q.'%q_segments' RENAME TO '%q_segments';",
176547 p->zDb, p->zName, zName
176548 );
176549 fts3DbExec(pRc: &rc, db,
176550 zFormat: "ALTER TABLE %Q.'%q_segdir' RENAME TO '%q_segdir';",
176551 p->zDb, p->zName, zName
176552 );
176553 return rc;
176554}
176555
176556/*
176557** The xSavepoint() method.
176558**
176559** Flush the contents of the pending-terms table to disk.
176560*/
176561static int fts3SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
176562 int rc = SQLITE_OK;
176563 UNUSED_PARAMETER(iSavepoint);
176564 assert( ((Fts3Table *)pVtab)->inTransaction );
176565 assert( ((Fts3Table *)pVtab)->mxSavepoint <= iSavepoint );
176566 TESTONLY( ((Fts3Table *)pVtab)->mxSavepoint = iSavepoint );
176567 if( ((Fts3Table *)pVtab)->bIgnoreSavepoint==0 ){
176568 rc = fts3SyncMethod(pVtab);
176569 }
176570 return rc;
176571}
176572
176573/*
176574** The xRelease() method.
176575**
176576** This is a no-op.
176577*/
176578static int fts3ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
176579 TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
176580 UNUSED_PARAMETER(iSavepoint);
176581 UNUSED_PARAMETER(pVtab);
176582 assert( p->inTransaction );
176583 assert( p->mxSavepoint >= iSavepoint );
176584 TESTONLY( p->mxSavepoint = iSavepoint-1 );
176585 return SQLITE_OK;
176586}
176587
176588/*
176589** The xRollbackTo() method.
176590**
176591** Discard the contents of the pending terms table.
176592*/
176593static int fts3RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
176594 Fts3Table *p = (Fts3Table*)pVtab;
176595 UNUSED_PARAMETER(iSavepoint);
176596 assert( p->inTransaction );
176597 TESTONLY( p->mxSavepoint = iSavepoint );
176598 sqlite3Fts3PendingTermsClear(p);
176599 return SQLITE_OK;
176600}
176601
176602/*
176603** Return true if zName is the extension on one of the shadow tables used
176604** by this module.
176605*/
176606static int fts3ShadowName(const char *zName){
176607 static const char *azName[] = {
176608 "content", "docsize", "segdir", "segments", "stat",
176609 };
176610 unsigned int i;
176611 for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
176612 if( sqlite3_stricmp(zLeft: zName, zRight: azName[i])==0 ) return 1;
176613 }
176614 return 0;
176615}
176616
176617static const sqlite3_module fts3Module = {
176618 /* iVersion */ 3,
176619 /* xCreate */ fts3CreateMethod,
176620 /* xConnect */ fts3ConnectMethod,
176621 /* xBestIndex */ fts3BestIndexMethod,
176622 /* xDisconnect */ fts3DisconnectMethod,
176623 /* xDestroy */ fts3DestroyMethod,
176624 /* xOpen */ fts3OpenMethod,
176625 /* xClose */ fts3CloseMethod,
176626 /* xFilter */ fts3FilterMethod,
176627 /* xNext */ fts3NextMethod,
176628 /* xEof */ fts3EofMethod,
176629 /* xColumn */ fts3ColumnMethod,
176630 /* xRowid */ fts3RowidMethod,
176631 /* xUpdate */ fts3UpdateMethod,
176632 /* xBegin */ fts3BeginMethod,
176633 /* xSync */ fts3SyncMethod,
176634 /* xCommit */ fts3CommitMethod,
176635 /* xRollback */ fts3RollbackMethod,
176636 /* xFindFunction */ fts3FindFunctionMethod,
176637 /* xRename */ fts3RenameMethod,
176638 /* xSavepoint */ fts3SavepointMethod,
176639 /* xRelease */ fts3ReleaseMethod,
176640 /* xRollbackTo */ fts3RollbackToMethod,
176641 /* xShadowName */ fts3ShadowName,
176642};
176643
176644/*
176645** This function is registered as the module destructor (called when an
176646** FTS3 enabled database connection is closed). It frees the memory
176647** allocated for the tokenizer hash table.
176648*/
176649static void hashDestroy(void *p){
176650 Fts3Hash *pHash = (Fts3Hash *)p;
176651 sqlite3Fts3HashClear(pHash);
176652 sqlite3_free(p: pHash);
176653}
176654
176655/*
176656** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are
176657** implemented in files fts3_tokenizer1.c, fts3_porter.c and fts3_icu.c
176658** respectively. The following three forward declarations are for functions
176659** declared in these files used to retrieve the respective implementations.
176660**
176661** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed
176662** to by the argument to point to the "simple" tokenizer implementation.
176663** And so on.
176664*/
176665SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
176666SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule);
176667#ifndef SQLITE_DISABLE_FTS3_UNICODE
176668SQLITE_PRIVATE void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const**ppModule);
176669#endif
176670#ifdef SQLITE_ENABLE_ICU
176671SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule);
176672#endif
176673
176674/*
176675** Initialize the fts3 extension. If this extension is built as part
176676** of the sqlite library, then this function is called directly by
176677** SQLite. If fts3 is built as a dynamically loadable extension, this
176678** function is called by the sqlite3_extension_init() entry point.
176679*/
176680SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db){
176681 int rc = SQLITE_OK;
176682 Fts3Hash *pHash = 0;
176683 const sqlite3_tokenizer_module *pSimple = 0;
176684 const sqlite3_tokenizer_module *pPorter = 0;
176685#ifndef SQLITE_DISABLE_FTS3_UNICODE
176686 const sqlite3_tokenizer_module *pUnicode = 0;
176687#endif
176688
176689#ifdef SQLITE_ENABLE_ICU
176690 const sqlite3_tokenizer_module *pIcu = 0;
176691 sqlite3Fts3IcuTokenizerModule(&pIcu);
176692#endif
176693
176694#ifndef SQLITE_DISABLE_FTS3_UNICODE
176695 sqlite3Fts3UnicodeTokenizer(ppModule: &pUnicode);
176696#endif
176697
176698#ifdef SQLITE_TEST
176699 rc = sqlite3Fts3InitTerm(db);
176700 if( rc!=SQLITE_OK ) return rc;
176701#endif
176702
176703 rc = sqlite3Fts3InitAux(db);
176704 if( rc!=SQLITE_OK ) return rc;
176705
176706 sqlite3Fts3SimpleTokenizerModule(ppModule: &pSimple);
176707 sqlite3Fts3PorterTokenizerModule(ppModule: &pPorter);
176708
176709 /* Allocate and initialize the hash-table used to store tokenizers. */
176710 pHash = sqlite3_malloc(n: sizeof(Fts3Hash));
176711 if( !pHash ){
176712 rc = SQLITE_NOMEM;
176713 }else{
176714 sqlite3Fts3HashInit(pNew: pHash, FTS3_HASH_STRING, copyKey: 1);
176715 }
176716
176717 /* Load the built-in tokenizers into the hash table */
176718 if( rc==SQLITE_OK ){
176719 if( sqlite3Fts3HashInsert(pHash, pKey: "simple", nKey: 7, pData: (void *)pSimple)
176720 || sqlite3Fts3HashInsert(pHash, pKey: "porter", nKey: 7, pData: (void *)pPorter)
176721
176722#ifndef SQLITE_DISABLE_FTS3_UNICODE
176723 || sqlite3Fts3HashInsert(pHash, pKey: "unicode61", nKey: 10, pData: (void *)pUnicode)
176724#endif
176725#ifdef SQLITE_ENABLE_ICU
176726 || (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu))
176727#endif
176728 ){
176729 rc = SQLITE_NOMEM;
176730 }
176731 }
176732
176733#ifdef SQLITE_TEST
176734 if( rc==SQLITE_OK ){
176735 rc = sqlite3Fts3ExprInitTestInterface(db, pHash);
176736 }
176737#endif
176738
176739 /* Create the virtual table wrapper around the hash-table and overload
176740 ** the four scalar functions. If this is successful, register the
176741 ** module with sqlite.
176742 */
176743 if( SQLITE_OK==rc
176744 && SQLITE_OK==(rc = sqlite3Fts3InitHashTable(db, pHash, "fts3_tokenizer"))
176745 && SQLITE_OK==(rc = sqlite3_overload_function(db, zName: "snippet", nArg: -1))
176746 && SQLITE_OK==(rc = sqlite3_overload_function(db, zName: "offsets", nArg: 1))
176747 && SQLITE_OK==(rc = sqlite3_overload_function(db, zName: "matchinfo", nArg: 1))
176748 && SQLITE_OK==(rc = sqlite3_overload_function(db, zName: "matchinfo", nArg: 2))
176749 && SQLITE_OK==(rc = sqlite3_overload_function(db, zName: "optimize", nArg: 1))
176750 ){
176751 rc = sqlite3_create_module_v2(
176752 db, zName: "fts3", pModule: &fts3Module, pAux: (void *)pHash, xDestroy: hashDestroy
176753 );
176754 if( rc==SQLITE_OK ){
176755 rc = sqlite3_create_module_v2(
176756 db, zName: "fts4", pModule: &fts3Module, pAux: (void *)pHash, xDestroy: 0
176757 );
176758 }
176759 if( rc==SQLITE_OK ){
176760 rc = sqlite3Fts3InitTok(db, (void *)pHash);
176761 }
176762 return rc;
176763 }
176764
176765
176766 /* An error has occurred. Delete the hash table and return the error code. */
176767 assert( rc!=SQLITE_OK );
176768 if( pHash ){
176769 sqlite3Fts3HashClear(pHash);
176770 sqlite3_free(p: pHash);
176771 }
176772 return rc;
176773}
176774
176775/*
176776** Allocate an Fts3MultiSegReader for each token in the expression headed
176777** by pExpr.
176778**
176779** An Fts3SegReader object is a cursor that can seek or scan a range of
176780** entries within a single segment b-tree. An Fts3MultiSegReader uses multiple
176781** Fts3SegReader objects internally to provide an interface to seek or scan
176782** within the union of all segments of a b-tree. Hence the name.
176783**
176784** If the allocated Fts3MultiSegReader just seeks to a single entry in a
176785** segment b-tree (if the term is not a prefix or it is a prefix for which
176786** there exists prefix b-tree of the right length) then it may be traversed
176787** and merged incrementally. Otherwise, it has to be merged into an in-memory
176788** doclist and then traversed.
176789*/
176790static void fts3EvalAllocateReaders(
176791 Fts3Cursor *pCsr, /* FTS cursor handle */
176792 Fts3Expr *pExpr, /* Allocate readers for this expression */
176793 int *pnToken, /* OUT: Total number of tokens in phrase. */
176794 int *pnOr, /* OUT: Total number of OR nodes in expr. */
176795 int *pRc /* IN/OUT: Error code */
176796){
176797 if( pExpr && SQLITE_OK==*pRc ){
176798 if( pExpr->eType==FTSQUERY_PHRASE ){
176799 int i;
176800 int nToken = pExpr->pPhrase->nToken;
176801 *pnToken += nToken;
176802 for(i=0; i<nToken; i++){
176803 Fts3PhraseToken *pToken = &pExpr->pPhrase->aToken[i];
176804 int rc = fts3TermSegReaderCursor(pCsr,
176805 zTerm: pToken->z, nTerm: pToken->n, isPrefix: pToken->isPrefix, ppSegcsr: &pToken->pSegcsr
176806 );
176807 if( rc!=SQLITE_OK ){
176808 *pRc = rc;
176809 return;
176810 }
176811 }
176812 assert( pExpr->pPhrase->iDoclistToken==0 );
176813 pExpr->pPhrase->iDoclistToken = -1;
176814 }else{
176815 *pnOr += (pExpr->eType==FTSQUERY_OR);
176816 fts3EvalAllocateReaders(pCsr, pExpr: pExpr->pLeft, pnToken, pnOr, pRc);
176817 fts3EvalAllocateReaders(pCsr, pExpr: pExpr->pRight, pnToken, pnOr, pRc);
176818 }
176819 }
176820}
176821
176822/*
176823** Arguments pList/nList contain the doclist for token iToken of phrase p.
176824** It is merged into the main doclist stored in p->doclist.aAll/nAll.
176825**
176826** This function assumes that pList points to a buffer allocated using
176827** sqlite3_malloc(). This function takes responsibility for eventually
176828** freeing the buffer.
176829**
176830** SQLITE_OK is returned if successful, or SQLITE_NOMEM if an error occurs.
176831*/
176832static int fts3EvalPhraseMergeToken(
176833 Fts3Table *pTab, /* FTS Table pointer */
176834 Fts3Phrase *p, /* Phrase to merge pList/nList into */
176835 int iToken, /* Token pList/nList corresponds to */
176836 char *pList, /* Pointer to doclist */
176837 int nList /* Number of bytes in pList */
176838){
176839 int rc = SQLITE_OK;
176840 assert( iToken!=p->iDoclistToken );
176841
176842 if( pList==0 ){
176843 sqlite3_free(p: p->doclist.aAll);
176844 p->doclist.aAll = 0;
176845 p->doclist.nAll = 0;
176846 }
176847
176848 else if( p->iDoclistToken<0 ){
176849 p->doclist.aAll = pList;
176850 p->doclist.nAll = nList;
176851 }
176852
176853 else if( p->doclist.aAll==0 ){
176854 sqlite3_free(p: pList);
176855 }
176856
176857 else {
176858 char *pLeft;
176859 char *pRight;
176860 int nLeft;
176861 int nRight;
176862 int nDiff;
176863
176864 if( p->iDoclistToken<iToken ){
176865 pLeft = p->doclist.aAll;
176866 nLeft = p->doclist.nAll;
176867 pRight = pList;
176868 nRight = nList;
176869 nDiff = iToken - p->iDoclistToken;
176870 }else{
176871 pRight = p->doclist.aAll;
176872 nRight = p->doclist.nAll;
176873 pLeft = pList;
176874 nLeft = nList;
176875 nDiff = p->iDoclistToken - iToken;
176876 }
176877
176878 rc = fts3DoclistPhraseMerge(
176879 bDescDoclist: pTab->bDescIdx, nDist: nDiff, aLeft: pLeft, nLeft, paRight: &pRight, pnRight: &nRight
176880 );
176881 sqlite3_free(p: pLeft);
176882 p->doclist.aAll = pRight;
176883 p->doclist.nAll = nRight;
176884 }
176885
176886 if( iToken>p->iDoclistToken ) p->iDoclistToken = iToken;
176887 return rc;
176888}
176889
176890/*
176891** Load the doclist for phrase p into p->doclist.aAll/nAll. The loaded doclist
176892** does not take deferred tokens into account.
176893**
176894** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
176895*/
176896static int fts3EvalPhraseLoad(
176897 Fts3Cursor *pCsr, /* FTS Cursor handle */
176898 Fts3Phrase *p /* Phrase object */
176899){
176900 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
176901 int iToken;
176902 int rc = SQLITE_OK;
176903
176904 for(iToken=0; rc==SQLITE_OK && iToken<p->nToken; iToken++){
176905 Fts3PhraseToken *pToken = &p->aToken[iToken];
176906 assert( pToken->pDeferred==0 || pToken->pSegcsr==0 );
176907
176908 if( pToken->pSegcsr ){
176909 int nThis = 0;
176910 char *pThis = 0;
176911 rc = fts3TermSelect(p: pTab, pTok: pToken, iColumn: p->iColumn, pnOut: &nThis, ppOut: &pThis);
176912 if( rc==SQLITE_OK ){
176913 rc = fts3EvalPhraseMergeToken(pTab, p, iToken, pList: pThis, nList: nThis);
176914 }
176915 }
176916 assert( pToken->pSegcsr==0 );
176917 }
176918
176919 return rc;
176920}
176921
176922#ifndef SQLITE_DISABLE_FTS4_DEFERRED
176923/*
176924** This function is called on each phrase after the position lists for
176925** any deferred tokens have been loaded into memory. It updates the phrases
176926** current position list to include only those positions that are really
176927** instances of the phrase (after considering deferred tokens). If this
176928** means that the phrase does not appear in the current row, doclist.pList
176929** and doclist.nList are both zeroed.
176930**
176931** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
176932*/
176933static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){
176934 int iToken; /* Used to iterate through phrase tokens */
176935 char *aPoslist = 0; /* Position list for deferred tokens */
176936 int nPoslist = 0; /* Number of bytes in aPoslist */
176937 int iPrev = -1; /* Token number of previous deferred token */
176938
176939 assert( pPhrase->doclist.bFreeList==0 );
176940
176941 for(iToken=0; iToken<pPhrase->nToken; iToken++){
176942 Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
176943 Fts3DeferredToken *pDeferred = pToken->pDeferred;
176944
176945 if( pDeferred ){
176946 char *pList;
176947 int nList;
176948 int rc = sqlite3Fts3DeferredTokenList(pDeferred, &pList, &nList);
176949 if( rc!=SQLITE_OK ) return rc;
176950
176951 if( pList==0 ){
176952 sqlite3_free(p: aPoslist);
176953 pPhrase->doclist.pList = 0;
176954 pPhrase->doclist.nList = 0;
176955 return SQLITE_OK;
176956
176957 }else if( aPoslist==0 ){
176958 aPoslist = pList;
176959 nPoslist = nList;
176960
176961 }else{
176962 char *aOut = pList;
176963 char *p1 = aPoslist;
176964 char *p2 = aOut;
176965
176966 assert( iPrev>=0 );
176967 fts3PoslistPhraseMerge(pp: &aOut, nToken: iToken-iPrev, isSaveLeft: 0, isExact: 1, pp1: &p1, pp2: &p2);
176968 sqlite3_free(p: aPoslist);
176969 aPoslist = pList;
176970 nPoslist = (int)(aOut - aPoslist);
176971 if( nPoslist==0 ){
176972 sqlite3_free(p: aPoslist);
176973 pPhrase->doclist.pList = 0;
176974 pPhrase->doclist.nList = 0;
176975 return SQLITE_OK;
176976 }
176977 }
176978 iPrev = iToken;
176979 }
176980 }
176981
176982 if( iPrev>=0 ){
176983 int nMaxUndeferred = pPhrase->iDoclistToken;
176984 if( nMaxUndeferred<0 ){
176985 pPhrase->doclist.pList = aPoslist;
176986 pPhrase->doclist.nList = nPoslist;
176987 pPhrase->doclist.iDocid = pCsr->iPrevId;
176988 pPhrase->doclist.bFreeList = 1;
176989 }else{
176990 int nDistance;
176991 char *p1;
176992 char *p2;
176993 char *aOut;
176994
176995 if( nMaxUndeferred>iPrev ){
176996 p1 = aPoslist;
176997 p2 = pPhrase->doclist.pList;
176998 nDistance = nMaxUndeferred - iPrev;
176999 }else{
177000 p1 = pPhrase->doclist.pList;
177001 p2 = aPoslist;
177002 nDistance = iPrev - nMaxUndeferred;
177003 }
177004
177005 aOut = (char *)sqlite3_malloc(n: nPoslist+8);
177006 if( !aOut ){
177007 sqlite3_free(p: aPoslist);
177008 return SQLITE_NOMEM;
177009 }
177010
177011 pPhrase->doclist.pList = aOut;
177012 if( fts3PoslistPhraseMerge(pp: &aOut, nToken: nDistance, isSaveLeft: 0, isExact: 1, pp1: &p1, pp2: &p2) ){
177013 pPhrase->doclist.bFreeList = 1;
177014 pPhrase->doclist.nList = (int)(aOut - pPhrase->doclist.pList);
177015 }else{
177016 sqlite3_free(p: aOut);
177017 pPhrase->doclist.pList = 0;
177018 pPhrase->doclist.nList = 0;
177019 }
177020 sqlite3_free(p: aPoslist);
177021 }
177022 }
177023
177024 return SQLITE_OK;
177025}
177026#endif /* SQLITE_DISABLE_FTS4_DEFERRED */
177027
177028/*
177029** Maximum number of tokens a phrase may have to be considered for the
177030** incremental doclists strategy.
177031*/
177032#define MAX_INCR_PHRASE_TOKENS 4
177033
177034/*
177035** This function is called for each Fts3Phrase in a full-text query
177036** expression to initialize the mechanism for returning rows. Once this
177037** function has been called successfully on an Fts3Phrase, it may be
177038** used with fts3EvalPhraseNext() to iterate through the matching docids.
177039**
177040** If parameter bOptOk is true, then the phrase may (or may not) use the
177041** incremental loading strategy. Otherwise, the entire doclist is loaded into
177042** memory within this call.
177043**
177044** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
177045*/
177046static int fts3EvalPhraseStart(Fts3Cursor *pCsr, int bOptOk, Fts3Phrase *p){
177047 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
177048 int rc = SQLITE_OK; /* Error code */
177049 int i;
177050
177051 /* Determine if doclists may be loaded from disk incrementally. This is
177052 ** possible if the bOptOk argument is true, the FTS doclists will be
177053 ** scanned in forward order, and the phrase consists of
177054 ** MAX_INCR_PHRASE_TOKENS or fewer tokens, none of which are are "^first"
177055 ** tokens or prefix tokens that cannot use a prefix-index. */
177056 int bHaveIncr = 0;
177057 int bIncrOk = (bOptOk
177058 && pCsr->bDesc==pTab->bDescIdx
177059 && p->nToken<=MAX_INCR_PHRASE_TOKENS && p->nToken>0
177060#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
177061 && pTab->bNoIncrDoclist==0
177062#endif
177063 );
177064 for(i=0; bIncrOk==1 && i<p->nToken; i++){
177065 Fts3PhraseToken *pToken = &p->aToken[i];
177066 if( pToken->bFirst || (pToken->pSegcsr!=0 && !pToken->pSegcsr->bLookup) ){
177067 bIncrOk = 0;
177068 }
177069 if( pToken->pSegcsr ) bHaveIncr = 1;
177070 }
177071
177072 if( bIncrOk && bHaveIncr ){
177073 /* Use the incremental approach. */
177074 int iCol = (p->iColumn >= pTab->nColumn ? -1 : p->iColumn);
177075 for(i=0; rc==SQLITE_OK && i<p->nToken; i++){
177076 Fts3PhraseToken *pToken = &p->aToken[i];
177077 Fts3MultiSegReader *pSegcsr = pToken->pSegcsr;
177078 if( pSegcsr ){
177079 rc = sqlite3Fts3MsrIncrStart(pTab, pSegcsr, iCol, pToken->z, pToken->n);
177080 }
177081 }
177082 p->bIncr = 1;
177083 }else{
177084 /* Load the full doclist for the phrase into memory. */
177085 rc = fts3EvalPhraseLoad(pCsr, p);
177086 p->bIncr = 0;
177087 }
177088
177089 assert( rc!=SQLITE_OK || p->nToken<1 || p->aToken[0].pSegcsr==0 || p->bIncr );
177090 return rc;
177091}
177092
177093/*
177094** This function is used to iterate backwards (from the end to start)
177095** through doclists. It is used by this module to iterate through phrase
177096** doclists in reverse and by the fts3_write.c module to iterate through
177097** pending-terms lists when writing to databases with "order=desc".
177098**
177099** The doclist may be sorted in ascending (parameter bDescIdx==0) or
177100** descending (parameter bDescIdx==1) order of docid. Regardless, this
177101** function iterates from the end of the doclist to the beginning.
177102*/
177103SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(
177104 int bDescIdx, /* True if the doclist is desc */
177105 char *aDoclist, /* Pointer to entire doclist */
177106 int nDoclist, /* Length of aDoclist in bytes */
177107 char **ppIter, /* IN/OUT: Iterator pointer */
177108 sqlite3_int64 *piDocid, /* IN/OUT: Docid pointer */
177109 int *pnList, /* OUT: List length pointer */
177110 u8 *pbEof /* OUT: End-of-file flag */
177111){
177112 char *p = *ppIter;
177113
177114 assert( nDoclist>0 );
177115 assert( *pbEof==0 );
177116 assert_fts3_nc( p || *piDocid==0 );
177117 assert( !p || (p>aDoclist && p<&aDoclist[nDoclist]) );
177118
177119 if( p==0 ){
177120 sqlite3_int64 iDocid = 0;
177121 char *pNext = 0;
177122 char *pDocid = aDoclist;
177123 char *pEnd = &aDoclist[nDoclist];
177124 int iMul = 1;
177125
177126 while( pDocid<pEnd ){
177127 sqlite3_int64 iDelta;
177128 pDocid += sqlite3Fts3GetVarint(pBuf: pDocid, v: &iDelta);
177129 iDocid += (iMul * iDelta);
177130 pNext = pDocid;
177131 fts3PoslistCopy(pp: 0, ppPoslist: &pDocid);
177132 while( pDocid<pEnd && *pDocid==0 ) pDocid++;
177133 iMul = (bDescIdx ? -1 : 1);
177134 }
177135
177136 *pnList = (int)(pEnd - pNext);
177137 *ppIter = pNext;
177138 *piDocid = iDocid;
177139 }else{
177140 int iMul = (bDescIdx ? -1 : 1);
177141 sqlite3_int64 iDelta;
177142 fts3GetReverseVarint(pp: &p, pStart: aDoclist, pVal: &iDelta);
177143 *piDocid -= (iMul * iDelta);
177144
177145 if( p==aDoclist ){
177146 *pbEof = 1;
177147 }else{
177148 char *pSave = p;
177149 fts3ReversePoslist(pStart: aDoclist, ppPoslist: &p);
177150 *pnList = (int)(pSave - p);
177151 }
177152 *ppIter = p;
177153 }
177154}
177155
177156/*
177157** Iterate forwards through a doclist.
177158*/
177159SQLITE_PRIVATE void sqlite3Fts3DoclistNext(
177160 int bDescIdx, /* True if the doclist is desc */
177161 char *aDoclist, /* Pointer to entire doclist */
177162 int nDoclist, /* Length of aDoclist in bytes */
177163 char **ppIter, /* IN/OUT: Iterator pointer */
177164 sqlite3_int64 *piDocid, /* IN/OUT: Docid pointer */
177165 u8 *pbEof /* OUT: End-of-file flag */
177166){
177167 char *p = *ppIter;
177168
177169 assert( nDoclist>0 );
177170 assert( *pbEof==0 );
177171 assert_fts3_nc( p || *piDocid==0 );
177172 assert( !p || (p>=aDoclist && p<=&aDoclist[nDoclist]) );
177173
177174 if( p==0 ){
177175 p = aDoclist;
177176 p += sqlite3Fts3GetVarint(pBuf: p, v: piDocid);
177177 }else{
177178 fts3PoslistCopy(pp: 0, ppPoslist: &p);
177179 while( p<&aDoclist[nDoclist] && *p==0 ) p++;
177180 if( p>=&aDoclist[nDoclist] ){
177181 *pbEof = 1;
177182 }else{
177183 sqlite3_int64 iVar;
177184 p += sqlite3Fts3GetVarint(pBuf: p, v: &iVar);
177185 *piDocid += ((bDescIdx ? -1 : 1) * iVar);
177186 }
177187 }
177188
177189 *ppIter = p;
177190}
177191
177192/*
177193** Advance the iterator pDL to the next entry in pDL->aAll/nAll. Set *pbEof
177194** to true if EOF is reached.
177195*/
177196static void fts3EvalDlPhraseNext(
177197 Fts3Table *pTab,
177198 Fts3Doclist *pDL,
177199 u8 *pbEof
177200){
177201 char *pIter; /* Used to iterate through aAll */
177202 char *pEnd; /* 1 byte past end of aAll */
177203
177204 if( pDL->pNextDocid ){
177205 pIter = pDL->pNextDocid;
177206 assert( pDL->aAll!=0 || pIter==0 );
177207 }else{
177208 pIter = pDL->aAll;
177209 }
177210
177211 if( pIter==0 || pIter>=(pEnd = pDL->aAll + pDL->nAll) ){
177212 /* We have already reached the end of this doclist. EOF. */
177213 *pbEof = 1;
177214 }else{
177215 sqlite3_int64 iDelta;
177216 pIter += sqlite3Fts3GetVarint(pBuf: pIter, v: &iDelta);
177217 if( pTab->bDescIdx==0 || pDL->pNextDocid==0 ){
177218 pDL->iDocid += iDelta;
177219 }else{
177220 pDL->iDocid -= iDelta;
177221 }
177222 pDL->pList = pIter;
177223 fts3PoslistCopy(pp: 0, ppPoslist: &pIter);
177224 pDL->nList = (int)(pIter - pDL->pList);
177225
177226 /* pIter now points just past the 0x00 that terminates the position-
177227 ** list for document pDL->iDocid. However, if this position-list was
177228 ** edited in place by fts3EvalNearTrim(), then pIter may not actually
177229 ** point to the start of the next docid value. The following line deals
177230 ** with this case by advancing pIter past the zero-padding added by
177231 ** fts3EvalNearTrim(). */
177232 while( pIter<pEnd && *pIter==0 ) pIter++;
177233
177234 pDL->pNextDocid = pIter;
177235 assert( pIter>=&pDL->aAll[pDL->nAll] || *pIter );
177236 *pbEof = 0;
177237 }
177238}
177239
177240/*
177241** Helper type used by fts3EvalIncrPhraseNext() and incrPhraseTokenNext().
177242*/
177243typedef struct TokenDoclist TokenDoclist;
177244struct TokenDoclist {
177245 int bIgnore;
177246 sqlite3_int64 iDocid;
177247 char *pList;
177248 int nList;
177249};
177250
177251/*
177252** Token pToken is an incrementally loaded token that is part of a
177253** multi-token phrase. Advance it to the next matching document in the
177254** database and populate output variable *p with the details of the new
177255** entry. Or, if the iterator has reached EOF, set *pbEof to true.
177256**
177257** If an error occurs, return an SQLite error code. Otherwise, return
177258** SQLITE_OK.
177259*/
177260static int incrPhraseTokenNext(
177261 Fts3Table *pTab, /* Virtual table handle */
177262 Fts3Phrase *pPhrase, /* Phrase to advance token of */
177263 int iToken, /* Specific token to advance */
177264 TokenDoclist *p, /* OUT: Docid and doclist for new entry */
177265 u8 *pbEof /* OUT: True if iterator is at EOF */
177266){
177267 int rc = SQLITE_OK;
177268
177269 if( pPhrase->iDoclistToken==iToken ){
177270 assert( p->bIgnore==0 );
177271 assert( pPhrase->aToken[iToken].pSegcsr==0 );
177272 fts3EvalDlPhraseNext(pTab, pDL: &pPhrase->doclist, pbEof);
177273 p->pList = pPhrase->doclist.pList;
177274 p->nList = pPhrase->doclist.nList;
177275 p->iDocid = pPhrase->doclist.iDocid;
177276 }else{
177277 Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
177278 assert( pToken->pDeferred==0 );
177279 assert( pToken->pSegcsr || pPhrase->iDoclistToken>=0 );
177280 if( pToken->pSegcsr ){
177281 assert( p->bIgnore==0 );
177282 rc = sqlite3Fts3MsrIncrNext(
177283 pTab, pToken->pSegcsr, &p->iDocid, &p->pList, &p->nList
177284 );
177285 if( p->pList==0 ) *pbEof = 1;
177286 }else{
177287 p->bIgnore = 1;
177288 }
177289 }
177290
177291 return rc;
177292}
177293
177294
177295/*
177296** The phrase iterator passed as the second argument:
177297**
177298** * features at least one token that uses an incremental doclist, and
177299**
177300** * does not contain any deferred tokens.
177301**
177302** Advance it to the next matching documnent in the database and populate
177303** the Fts3Doclist.pList and nList fields.
177304**
177305** If there is no "next" entry and no error occurs, then *pbEof is set to
177306** 1 before returning. Otherwise, if no error occurs and the iterator is
177307** successfully advanced, *pbEof is set to 0.
177308**
177309** If an error occurs, return an SQLite error code. Otherwise, return
177310** SQLITE_OK.
177311*/
177312static int fts3EvalIncrPhraseNext(
177313 Fts3Cursor *pCsr, /* FTS Cursor handle */
177314 Fts3Phrase *p, /* Phrase object to advance to next docid */
177315 u8 *pbEof /* OUT: Set to 1 if EOF */
177316){
177317 int rc = SQLITE_OK;
177318 Fts3Doclist *pDL = &p->doclist;
177319 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
177320 u8 bEof = 0;
177321
177322 /* This is only called if it is guaranteed that the phrase has at least
177323 ** one incremental token. In which case the bIncr flag is set. */
177324 assert( p->bIncr==1 );
177325
177326 if( p->nToken==1 ){
177327 rc = sqlite3Fts3MsrIncrNext(pTab, p->aToken[0].pSegcsr,
177328 &pDL->iDocid, &pDL->pList, &pDL->nList
177329 );
177330 if( pDL->pList==0 ) bEof = 1;
177331 }else{
177332 int bDescDoclist = pCsr->bDesc;
177333 struct TokenDoclist a[MAX_INCR_PHRASE_TOKENS];
177334
177335 memset(s: a, c: 0, n: sizeof(a));
177336 assert( p->nToken<=MAX_INCR_PHRASE_TOKENS );
177337 assert( p->iDoclistToken<MAX_INCR_PHRASE_TOKENS );
177338
177339 while( bEof==0 ){
177340 int bMaxSet = 0;
177341 sqlite3_int64 iMax = 0; /* Largest docid for all iterators */
177342 int i; /* Used to iterate through tokens */
177343
177344 /* Advance the iterator for each token in the phrase once. */
177345 for(i=0; rc==SQLITE_OK && i<p->nToken && bEof==0; i++){
177346 rc = incrPhraseTokenNext(pTab, pPhrase: p, iToken: i, p: &a[i], pbEof: &bEof);
177347 if( a[i].bIgnore==0 && (bMaxSet==0 || DOCID_CMP(iMax, a[i].iDocid)<0) ){
177348 iMax = a[i].iDocid;
177349 bMaxSet = 1;
177350 }
177351 }
177352 assert( rc!=SQLITE_OK || (p->nToken>=1 && a[p->nToken-1].bIgnore==0) );
177353 assert( rc!=SQLITE_OK || bMaxSet );
177354
177355 /* Keep advancing iterators until they all point to the same document */
177356 for(i=0; i<p->nToken; i++){
177357 while( rc==SQLITE_OK && bEof==0
177358 && a[i].bIgnore==0 && DOCID_CMP(a[i].iDocid, iMax)<0
177359 ){
177360 rc = incrPhraseTokenNext(pTab, pPhrase: p, iToken: i, p: &a[i], pbEof: &bEof);
177361 if( DOCID_CMP(a[i].iDocid, iMax)>0 ){
177362 iMax = a[i].iDocid;
177363 i = 0;
177364 }
177365 }
177366 }
177367
177368 /* Check if the current entries really are a phrase match */
177369 if( bEof==0 ){
177370 int nList = 0;
177371 int nByte = a[p->nToken-1].nList;
177372 char *aDoclist = sqlite3_malloc(n: nByte+FTS3_BUFFER_PADDING);
177373 if( !aDoclist ) return SQLITE_NOMEM;
177374 memcpy(dest: aDoclist, src: a[p->nToken-1].pList, n: nByte+1);
177375 memset(s: &aDoclist[nByte], c: 0, FTS3_BUFFER_PADDING);
177376
177377 for(i=0; i<(p->nToken-1); i++){
177378 if( a[i].bIgnore==0 ){
177379 char *pL = a[i].pList;
177380 char *pR = aDoclist;
177381 char *pOut = aDoclist;
177382 int nDist = p->nToken-1-i;
177383 int res = fts3PoslistPhraseMerge(pp: &pOut, nToken: nDist, isSaveLeft: 0, isExact: 1, pp1: &pL, pp2: &pR);
177384 if( res==0 ) break;
177385 nList = (int)(pOut - aDoclist);
177386 }
177387 }
177388 if( i==(p->nToken-1) ){
177389 pDL->iDocid = iMax;
177390 pDL->pList = aDoclist;
177391 pDL->nList = nList;
177392 pDL->bFreeList = 1;
177393 break;
177394 }
177395 sqlite3_free(p: aDoclist);
177396 }
177397 }
177398 }
177399
177400 *pbEof = bEof;
177401 return rc;
177402}
177403
177404/*
177405** Attempt to move the phrase iterator to point to the next matching docid.
177406** If an error occurs, return an SQLite error code. Otherwise, return
177407** SQLITE_OK.
177408**
177409** If there is no "next" entry and no error occurs, then *pbEof is set to
177410** 1 before returning. Otherwise, if no error occurs and the iterator is
177411** successfully advanced, *pbEof is set to 0.
177412*/
177413static int fts3EvalPhraseNext(
177414 Fts3Cursor *pCsr, /* FTS Cursor handle */
177415 Fts3Phrase *p, /* Phrase object to advance to next docid */
177416 u8 *pbEof /* OUT: Set to 1 if EOF */
177417){
177418 int rc = SQLITE_OK;
177419 Fts3Doclist *pDL = &p->doclist;
177420 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
177421
177422 if( p->bIncr ){
177423 rc = fts3EvalIncrPhraseNext(pCsr, p, pbEof);
177424 }else if( pCsr->bDesc!=pTab->bDescIdx && pDL->nAll ){
177425 sqlite3Fts3DoclistPrev(bDescIdx: pTab->bDescIdx, aDoclist: pDL->aAll, nDoclist: pDL->nAll,
177426 ppIter: &pDL->pNextDocid, piDocid: &pDL->iDocid, pnList: &pDL->nList, pbEof
177427 );
177428 pDL->pList = pDL->pNextDocid;
177429 }else{
177430 fts3EvalDlPhraseNext(pTab, pDL, pbEof);
177431 }
177432
177433 return rc;
177434}
177435
177436/*
177437**
177438** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
177439** Otherwise, fts3EvalPhraseStart() is called on all phrases within the
177440** expression. Also the Fts3Expr.bDeferred variable is set to true for any
177441** expressions for which all descendent tokens are deferred.
177442**
177443** If parameter bOptOk is zero, then it is guaranteed that the
177444** Fts3Phrase.doclist.aAll/nAll variables contain the entire doclist for
177445** each phrase in the expression (subject to deferred token processing).
177446** Or, if bOptOk is non-zero, then one or more tokens within the expression
177447** may be loaded incrementally, meaning doclist.aAll/nAll is not available.
177448**
177449** If an error occurs within this function, *pRc is set to an SQLite error
177450** code before returning.
177451*/
177452static void fts3EvalStartReaders(
177453 Fts3Cursor *pCsr, /* FTS Cursor handle */
177454 Fts3Expr *pExpr, /* Expression to initialize phrases in */
177455 int *pRc /* IN/OUT: Error code */
177456){
177457 if( pExpr && SQLITE_OK==*pRc ){
177458 if( pExpr->eType==FTSQUERY_PHRASE ){
177459 int nToken = pExpr->pPhrase->nToken;
177460 if( nToken ){
177461 int i;
177462 for(i=0; i<nToken; i++){
177463 if( pExpr->pPhrase->aToken[i].pDeferred==0 ) break;
177464 }
177465 pExpr->bDeferred = (i==nToken);
177466 }
177467 *pRc = fts3EvalPhraseStart(pCsr, bOptOk: 1, p: pExpr->pPhrase);
177468 }else{
177469 fts3EvalStartReaders(pCsr, pExpr: pExpr->pLeft, pRc);
177470 fts3EvalStartReaders(pCsr, pExpr: pExpr->pRight, pRc);
177471 pExpr->bDeferred = (pExpr->pLeft->bDeferred && pExpr->pRight->bDeferred);
177472 }
177473 }
177474}
177475
177476/*
177477** An array of the following structures is assembled as part of the process
177478** of selecting tokens to defer before the query starts executing (as part
177479** of the xFilter() method). There is one element in the array for each
177480** token in the FTS expression.
177481**
177482** Tokens are divided into AND/NEAR clusters. All tokens in a cluster belong
177483** to phrases that are connected only by AND and NEAR operators (not OR or
177484** NOT). When determining tokens to defer, each AND/NEAR cluster is considered
177485** separately. The root of a tokens AND/NEAR cluster is stored in
177486** Fts3TokenAndCost.pRoot.
177487*/
177488typedef struct Fts3TokenAndCost Fts3TokenAndCost;
177489struct Fts3TokenAndCost {
177490 Fts3Phrase *pPhrase; /* The phrase the token belongs to */
177491 int iToken; /* Position of token in phrase */
177492 Fts3PhraseToken *pToken; /* The token itself */
177493 Fts3Expr *pRoot; /* Root of NEAR/AND cluster */
177494 int nOvfl; /* Number of overflow pages to load doclist */
177495 int iCol; /* The column the token must match */
177496};
177497
177498/*
177499** This function is used to populate an allocated Fts3TokenAndCost array.
177500**
177501** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
177502** Otherwise, if an error occurs during execution, *pRc is set to an
177503** SQLite error code.
177504*/
177505static void fts3EvalTokenCosts(
177506 Fts3Cursor *pCsr, /* FTS Cursor handle */
177507 Fts3Expr *pRoot, /* Root of current AND/NEAR cluster */
177508 Fts3Expr *pExpr, /* Expression to consider */
177509 Fts3TokenAndCost **ppTC, /* Write new entries to *(*ppTC)++ */
177510 Fts3Expr ***ppOr, /* Write new OR root to *(*ppOr)++ */
177511 int *pRc /* IN/OUT: Error code */
177512){
177513 if( *pRc==SQLITE_OK ){
177514 if( pExpr->eType==FTSQUERY_PHRASE ){
177515 Fts3Phrase *pPhrase = pExpr->pPhrase;
177516 int i;
177517 for(i=0; *pRc==SQLITE_OK && i<pPhrase->nToken; i++){
177518 Fts3TokenAndCost *pTC = (*ppTC)++;
177519 pTC->pPhrase = pPhrase;
177520 pTC->iToken = i;
177521 pTC->pRoot = pRoot;
177522 pTC->pToken = &pPhrase->aToken[i];
177523 pTC->iCol = pPhrase->iColumn;
177524 *pRc = sqlite3Fts3MsrOvfl(pCsr, pTC->pToken->pSegcsr, &pTC->nOvfl);
177525 }
177526 }else if( pExpr->eType!=FTSQUERY_NOT ){
177527 assert( pExpr->eType==FTSQUERY_OR
177528 || pExpr->eType==FTSQUERY_AND
177529 || pExpr->eType==FTSQUERY_NEAR
177530 );
177531 assert( pExpr->pLeft && pExpr->pRight );
177532 if( pExpr->eType==FTSQUERY_OR ){
177533 pRoot = pExpr->pLeft;
177534 **ppOr = pRoot;
177535 (*ppOr)++;
177536 }
177537 fts3EvalTokenCosts(pCsr, pRoot, pExpr: pExpr->pLeft, ppTC, ppOr, pRc);
177538 if( pExpr->eType==FTSQUERY_OR ){
177539 pRoot = pExpr->pRight;
177540 **ppOr = pRoot;
177541 (*ppOr)++;
177542 }
177543 fts3EvalTokenCosts(pCsr, pRoot, pExpr: pExpr->pRight, ppTC, ppOr, pRc);
177544 }
177545 }
177546}
177547
177548/*
177549** Determine the average document (row) size in pages. If successful,
177550** write this value to *pnPage and return SQLITE_OK. Otherwise, return
177551** an SQLite error code.
177552**
177553** The average document size in pages is calculated by first calculating
177554** determining the average size in bytes, B. If B is less than the amount
177555** of data that will fit on a single leaf page of an intkey table in
177556** this database, then the average docsize is 1. Otherwise, it is 1 plus
177557** the number of overflow pages consumed by a record B bytes in size.
177558*/
177559static int fts3EvalAverageDocsize(Fts3Cursor *pCsr, int *pnPage){
177560 int rc = SQLITE_OK;
177561 if( pCsr->nRowAvg==0 ){
177562 /* The average document size, which is required to calculate the cost
177563 ** of each doclist, has not yet been determined. Read the required
177564 ** data from the %_stat table to calculate it.
177565 **
177566 ** Entry 0 of the %_stat table is a blob containing (nCol+1) FTS3
177567 ** varints, where nCol is the number of columns in the FTS3 table.
177568 ** The first varint is the number of documents currently stored in
177569 ** the table. The following nCol varints contain the total amount of
177570 ** data stored in all rows of each column of the table, from left
177571 ** to right.
177572 */
177573 Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
177574 sqlite3_stmt *pStmt;
177575 sqlite3_int64 nDoc = 0;
177576 sqlite3_int64 nByte = 0;
177577 const char *pEnd;
177578 const char *a;
177579
177580 rc = sqlite3Fts3SelectDoctotal(p, &pStmt);
177581 if( rc!=SQLITE_OK ) return rc;
177582 a = sqlite3_column_blob(pStmt, i: 0);
177583 testcase( a==0 ); /* If %_stat.value set to X'' */
177584 if( a ){
177585 pEnd = &a[sqlite3_column_bytes(pStmt, i: 0)];
177586 a += sqlite3Fts3GetVarintBounded(pBuf: a, pEnd, v: &nDoc);
177587 while( a<pEnd ){
177588 a += sqlite3Fts3GetVarintBounded(pBuf: a, pEnd, v: &nByte);
177589 }
177590 }
177591 if( nDoc==0 || nByte==0 ){
177592 sqlite3_reset(pStmt);
177593 return FTS_CORRUPT_VTAB;
177594 }
177595
177596 pCsr->nDoc = nDoc;
177597 pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz);
177598 assert( pCsr->nRowAvg>0 );
177599 rc = sqlite3_reset(pStmt);
177600 }
177601
177602 *pnPage = pCsr->nRowAvg;
177603 return rc;
177604}
177605
177606/*
177607** This function is called to select the tokens (if any) that will be
177608** deferred. The array aTC[] has already been populated when this is
177609** called.
177610**
177611** This function is called once for each AND/NEAR cluster in the
177612** expression. Each invocation determines which tokens to defer within
177613** the cluster with root node pRoot. See comments above the definition
177614** of struct Fts3TokenAndCost for more details.
177615**
177616** If no error occurs, SQLITE_OK is returned and sqlite3Fts3DeferToken()
177617** called on each token to defer. Otherwise, an SQLite error code is
177618** returned.
177619*/
177620static int fts3EvalSelectDeferred(
177621 Fts3Cursor *pCsr, /* FTS Cursor handle */
177622 Fts3Expr *pRoot, /* Consider tokens with this root node */
177623 Fts3TokenAndCost *aTC, /* Array of expression tokens and costs */
177624 int nTC /* Number of entries in aTC[] */
177625){
177626 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
177627 int nDocSize = 0; /* Number of pages per doc loaded */
177628 int rc = SQLITE_OK; /* Return code */
177629 int ii; /* Iterator variable for various purposes */
177630 int nOvfl = 0; /* Total overflow pages used by doclists */
177631 int nToken = 0; /* Total number of tokens in cluster */
177632
177633 int nMinEst = 0; /* The minimum count for any phrase so far. */
177634 int nLoad4 = 1; /* (Phrases that will be loaded)^4. */
177635
177636 /* Tokens are never deferred for FTS tables created using the content=xxx
177637 ** option. The reason being that it is not guaranteed that the content
177638 ** table actually contains the same data as the index. To prevent this from
177639 ** causing any problems, the deferred token optimization is completely
177640 ** disabled for content=xxx tables. */
177641 if( pTab->zContentTbl ){
177642 return SQLITE_OK;
177643 }
177644
177645 /* Count the tokens in this AND/NEAR cluster. If none of the doclists
177646 ** associated with the tokens spill onto overflow pages, or if there is
177647 ** only 1 token, exit early. No tokens to defer in this case. */
177648 for(ii=0; ii<nTC; ii++){
177649 if( aTC[ii].pRoot==pRoot ){
177650 nOvfl += aTC[ii].nOvfl;
177651 nToken++;
177652 }
177653 }
177654 if( nOvfl==0 || nToken<2 ) return SQLITE_OK;
177655
177656 /* Obtain the average docsize (in pages). */
177657 rc = fts3EvalAverageDocsize(pCsr, pnPage: &nDocSize);
177658 assert( rc!=SQLITE_OK || nDocSize>0 );
177659
177660
177661 /* Iterate through all tokens in this AND/NEAR cluster, in ascending order
177662 ** of the number of overflow pages that will be loaded by the pager layer
177663 ** to retrieve the entire doclist for the token from the full-text index.
177664 ** Load the doclists for tokens that are either:
177665 **
177666 ** a. The cheapest token in the entire query (i.e. the one visited by the
177667 ** first iteration of this loop), or
177668 **
177669 ** b. Part of a multi-token phrase.
177670 **
177671 ** After each token doclist is loaded, merge it with the others from the
177672 ** same phrase and count the number of documents that the merged doclist
177673 ** contains. Set variable "nMinEst" to the smallest number of documents in
177674 ** any phrase doclist for which 1 or more token doclists have been loaded.
177675 ** Let nOther be the number of other phrases for which it is certain that
177676 ** one or more tokens will not be deferred.
177677 **
177678 ** Then, for each token, defer it if loading the doclist would result in
177679 ** loading N or more overflow pages into memory, where N is computed as:
177680 **
177681 ** (nMinEst + 4^nOther - 1) / (4^nOther)
177682 */
177683 for(ii=0; ii<nToken && rc==SQLITE_OK; ii++){
177684 int iTC; /* Used to iterate through aTC[] array. */
177685 Fts3TokenAndCost *pTC = 0; /* Set to cheapest remaining token. */
177686
177687 /* Set pTC to point to the cheapest remaining token. */
177688 for(iTC=0; iTC<nTC; iTC++){
177689 if( aTC[iTC].pToken && aTC[iTC].pRoot==pRoot
177690 && (!pTC || aTC[iTC].nOvfl<pTC->nOvfl)
177691 ){
177692 pTC = &aTC[iTC];
177693 }
177694 }
177695 assert( pTC );
177696
177697 if( ii && pTC->nOvfl>=((nMinEst+(nLoad4/4)-1)/(nLoad4/4))*nDocSize ){
177698 /* The number of overflow pages to load for this (and therefore all
177699 ** subsequent) tokens is greater than the estimated number of pages
177700 ** that will be loaded if all subsequent tokens are deferred.
177701 */
177702 Fts3PhraseToken *pToken = pTC->pToken;
177703 rc = sqlite3Fts3DeferToken(pCsr, pToken, pTC->iCol);
177704 fts3SegReaderCursorFree(pSegcsr: pToken->pSegcsr);
177705 pToken->pSegcsr = 0;
177706 }else{
177707 /* Set nLoad4 to the value of (4^nOther) for the next iteration of the
177708 ** for-loop. Except, limit the value to 2^24 to prevent it from
177709 ** overflowing the 32-bit integer it is stored in. */
177710 if( ii<12 ) nLoad4 = nLoad4*4;
177711
177712 if( ii==0 || (pTC->pPhrase->nToken>1 && ii!=nToken-1) ){
177713 /* Either this is the cheapest token in the entire query, or it is
177714 ** part of a multi-token phrase. Either way, the entire doclist will
177715 ** (eventually) be loaded into memory. It may as well be now. */
177716 Fts3PhraseToken *pToken = pTC->pToken;
177717 int nList = 0;
177718 char *pList = 0;
177719 rc = fts3TermSelect(p: pTab, pTok: pToken, iColumn: pTC->iCol, pnOut: &nList, ppOut: &pList);
177720 assert( rc==SQLITE_OK || pList==0 );
177721 if( rc==SQLITE_OK ){
177722 rc = fts3EvalPhraseMergeToken(
177723 pTab, p: pTC->pPhrase, iToken: pTC->iToken,pList,nList
177724 );
177725 }
177726 if( rc==SQLITE_OK ){
177727 int nCount;
177728 nCount = fts3DoclistCountDocids(
177729 aList: pTC->pPhrase->doclist.aAll, nList: pTC->pPhrase->doclist.nAll
177730 );
177731 if( ii==0 || nCount<nMinEst ) nMinEst = nCount;
177732 }
177733 }
177734 }
177735 pTC->pToken = 0;
177736 }
177737
177738 return rc;
177739}
177740
177741/*
177742** This function is called from within the xFilter method. It initializes
177743** the full-text query currently stored in pCsr->pExpr. To iterate through
177744** the results of a query, the caller does:
177745**
177746** fts3EvalStart(pCsr);
177747** while( 1 ){
177748** fts3EvalNext(pCsr);
177749** if( pCsr->bEof ) break;
177750** ... return row pCsr->iPrevId to the caller ...
177751** }
177752*/
177753static int fts3EvalStart(Fts3Cursor *pCsr){
177754 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
177755 int rc = SQLITE_OK;
177756 int nToken = 0;
177757 int nOr = 0;
177758
177759 /* Allocate a MultiSegReader for each token in the expression. */
177760 fts3EvalAllocateReaders(pCsr, pExpr: pCsr->pExpr, pnToken: &nToken, pnOr: &nOr, pRc: &rc);
177761
177762 /* Determine which, if any, tokens in the expression should be deferred. */
177763#ifndef SQLITE_DISABLE_FTS4_DEFERRED
177764 if( rc==SQLITE_OK && nToken>1 && pTab->bFts4 ){
177765 Fts3TokenAndCost *aTC;
177766 aTC = (Fts3TokenAndCost *)sqlite3_malloc64(
177767 n: sizeof(Fts3TokenAndCost) * nToken
177768 + sizeof(Fts3Expr *) * nOr * 2
177769 );
177770
177771 if( !aTC ){
177772 rc = SQLITE_NOMEM;
177773 }else{
177774 Fts3Expr **apOr = (Fts3Expr **)&aTC[nToken];
177775 int ii;
177776 Fts3TokenAndCost *pTC = aTC;
177777 Fts3Expr **ppOr = apOr;
177778
177779 fts3EvalTokenCosts(pCsr, pRoot: 0, pExpr: pCsr->pExpr, ppTC: &pTC, ppOr: &ppOr, pRc: &rc);
177780 nToken = (int)(pTC-aTC);
177781 nOr = (int)(ppOr-apOr);
177782
177783 if( rc==SQLITE_OK ){
177784 rc = fts3EvalSelectDeferred(pCsr, pRoot: 0, aTC, nTC: nToken);
177785 for(ii=0; rc==SQLITE_OK && ii<nOr; ii++){
177786 rc = fts3EvalSelectDeferred(pCsr, pRoot: apOr[ii], aTC, nTC: nToken);
177787 }
177788 }
177789
177790 sqlite3_free(p: aTC);
177791 }
177792 }
177793#endif
177794
177795 fts3EvalStartReaders(pCsr, pExpr: pCsr->pExpr, pRc: &rc);
177796 return rc;
177797}
177798
177799/*
177800** Invalidate the current position list for phrase pPhrase.
177801*/
177802static void fts3EvalInvalidatePoslist(Fts3Phrase *pPhrase){
177803 if( pPhrase->doclist.bFreeList ){
177804 sqlite3_free(p: pPhrase->doclist.pList);
177805 }
177806 pPhrase->doclist.pList = 0;
177807 pPhrase->doclist.nList = 0;
177808 pPhrase->doclist.bFreeList = 0;
177809}
177810
177811/*
177812** This function is called to edit the position list associated with
177813** the phrase object passed as the fifth argument according to a NEAR
177814** condition. For example:
177815**
177816** abc NEAR/5 "def ghi"
177817**
177818** Parameter nNear is passed the NEAR distance of the expression (5 in
177819** the example above). When this function is called, *paPoslist points to
177820** the position list, and *pnToken is the number of phrase tokens in the
177821** phrase on the other side of the NEAR operator to pPhrase. For example,
177822** if pPhrase refers to the "def ghi" phrase, then *paPoslist points to
177823** the position list associated with phrase "abc".
177824**
177825** All positions in the pPhrase position list that are not sufficiently
177826** close to a position in the *paPoslist position list are removed. If this
177827** leaves 0 positions, zero is returned. Otherwise, non-zero.
177828**
177829** Before returning, *paPoslist is set to point to the position lsit
177830** associated with pPhrase. And *pnToken is set to the number of tokens in
177831** pPhrase.
177832*/
177833static int fts3EvalNearTrim(
177834 int nNear, /* NEAR distance. As in "NEAR/nNear". */
177835 char *aTmp, /* Temporary space to use */
177836 char **paPoslist, /* IN/OUT: Position list */
177837 int *pnToken, /* IN/OUT: Tokens in phrase of *paPoslist */
177838 Fts3Phrase *pPhrase /* The phrase object to trim the doclist of */
177839){
177840 int nParam1 = nNear + pPhrase->nToken;
177841 int nParam2 = nNear + *pnToken;
177842 int nNew;
177843 char *p2;
177844 char *pOut;
177845 int res;
177846
177847 assert( pPhrase->doclist.pList );
177848
177849 p2 = pOut = pPhrase->doclist.pList;
177850 res = fts3PoslistNearMerge(
177851 pp: &pOut, aTmp, nRight: nParam1, nLeft: nParam2, pp1: paPoslist, pp2: &p2
177852 );
177853 if( res ){
177854 nNew = (int)(pOut - pPhrase->doclist.pList) - 1;
177855 assert_fts3_nc( nNew<=pPhrase->doclist.nList && nNew>0 );
177856 if( nNew>=0 && nNew<=pPhrase->doclist.nList ){
177857 assert( pPhrase->doclist.pList[nNew]=='\0' );
177858 memset(s: &pPhrase->doclist.pList[nNew], c: 0, n: pPhrase->doclist.nList - nNew);
177859 pPhrase->doclist.nList = nNew;
177860 }
177861 *paPoslist = pPhrase->doclist.pList;
177862 *pnToken = pPhrase->nToken;
177863 }
177864
177865 return res;
177866}
177867
177868/*
177869** This function is a no-op if *pRc is other than SQLITE_OK when it is called.
177870** Otherwise, it advances the expression passed as the second argument to
177871** point to the next matching row in the database. Expressions iterate through
177872** matching rows in docid order. Ascending order if Fts3Cursor.bDesc is zero,
177873** or descending if it is non-zero.
177874**
177875** If an error occurs, *pRc is set to an SQLite error code. Otherwise, if
177876** successful, the following variables in pExpr are set:
177877**
177878** Fts3Expr.bEof (non-zero if EOF - there is no next row)
177879** Fts3Expr.iDocid (valid if bEof==0. The docid of the next row)
177880**
177881** If the expression is of type FTSQUERY_PHRASE, and the expression is not
177882** at EOF, then the following variables are populated with the position list
177883** for the phrase for the visited row:
177884**
177885** FTs3Expr.pPhrase->doclist.nList (length of pList in bytes)
177886** FTs3Expr.pPhrase->doclist.pList (pointer to position list)
177887**
177888** It says above that this function advances the expression to the next
177889** matching row. This is usually true, but there are the following exceptions:
177890**
177891** 1. Deferred tokens are not taken into account. If a phrase consists
177892** entirely of deferred tokens, it is assumed to match every row in
177893** the db. In this case the position-list is not populated at all.
177894**
177895** Or, if a phrase contains one or more deferred tokens and one or
177896** more non-deferred tokens, then the expression is advanced to the
177897** next possible match, considering only non-deferred tokens. In other
177898** words, if the phrase is "A B C", and "B" is deferred, the expression
177899** is advanced to the next row that contains an instance of "A * C",
177900** where "*" may match any single token. The position list in this case
177901** is populated as for "A * C" before returning.
177902**
177903** 2. NEAR is treated as AND. If the expression is "x NEAR y", it is
177904** advanced to point to the next row that matches "x AND y".
177905**
177906** See sqlite3Fts3EvalTestDeferred() for details on testing if a row is
177907** really a match, taking into account deferred tokens and NEAR operators.
177908*/
177909static void fts3EvalNextRow(
177910 Fts3Cursor *pCsr, /* FTS Cursor handle */
177911 Fts3Expr *pExpr, /* Expr. to advance to next matching row */
177912 int *pRc /* IN/OUT: Error code */
177913){
177914 if( *pRc==SQLITE_OK ){
177915 int bDescDoclist = pCsr->bDesc; /* Used by DOCID_CMP() macro */
177916 assert( pExpr->bEof==0 );
177917 pExpr->bStart = 1;
177918
177919 switch( pExpr->eType ){
177920 case FTSQUERY_NEAR:
177921 case FTSQUERY_AND: {
177922 Fts3Expr *pLeft = pExpr->pLeft;
177923 Fts3Expr *pRight = pExpr->pRight;
177924 assert( !pLeft->bDeferred || !pRight->bDeferred );
177925
177926 if( pLeft->bDeferred ){
177927 /* LHS is entirely deferred. So we assume it matches every row.
177928 ** Advance the RHS iterator to find the next row visited. */
177929 fts3EvalNextRow(pCsr, pExpr: pRight, pRc);
177930 pExpr->iDocid = pRight->iDocid;
177931 pExpr->bEof = pRight->bEof;
177932 }else if( pRight->bDeferred ){
177933 /* RHS is entirely deferred. So we assume it matches every row.
177934 ** Advance the LHS iterator to find the next row visited. */
177935 fts3EvalNextRow(pCsr, pExpr: pLeft, pRc);
177936 pExpr->iDocid = pLeft->iDocid;
177937 pExpr->bEof = pLeft->bEof;
177938 }else{
177939 /* Neither the RHS or LHS are deferred. */
177940 fts3EvalNextRow(pCsr, pExpr: pLeft, pRc);
177941 fts3EvalNextRow(pCsr, pExpr: pRight, pRc);
177942 while( !pLeft->bEof && !pRight->bEof && *pRc==SQLITE_OK ){
177943 sqlite3_int64 iDiff = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
177944 if( iDiff==0 ) break;
177945 if( iDiff<0 ){
177946 fts3EvalNextRow(pCsr, pExpr: pLeft, pRc);
177947 }else{
177948 fts3EvalNextRow(pCsr, pExpr: pRight, pRc);
177949 }
177950 }
177951 pExpr->iDocid = pLeft->iDocid;
177952 pExpr->bEof = (pLeft->bEof || pRight->bEof);
177953 if( pExpr->eType==FTSQUERY_NEAR && pExpr->bEof ){
177954 assert( pRight->eType==FTSQUERY_PHRASE );
177955 if( pRight->pPhrase->doclist.aAll ){
177956 Fts3Doclist *pDl = &pRight->pPhrase->doclist;
177957 while( *pRc==SQLITE_OK && pRight->bEof==0 ){
177958 memset(s: pDl->pList, c: 0, n: pDl->nList);
177959 fts3EvalNextRow(pCsr, pExpr: pRight, pRc);
177960 }
177961 }
177962 if( pLeft->pPhrase && pLeft->pPhrase->doclist.aAll ){
177963 Fts3Doclist *pDl = &pLeft->pPhrase->doclist;
177964 while( *pRc==SQLITE_OK && pLeft->bEof==0 ){
177965 memset(s: pDl->pList, c: 0, n: pDl->nList);
177966 fts3EvalNextRow(pCsr, pExpr: pLeft, pRc);
177967 }
177968 }
177969 pRight->bEof = pLeft->bEof = 1;
177970 }
177971 }
177972 break;
177973 }
177974
177975 case FTSQUERY_OR: {
177976 Fts3Expr *pLeft = pExpr->pLeft;
177977 Fts3Expr *pRight = pExpr->pRight;
177978 sqlite3_int64 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
177979
177980 assert_fts3_nc( pLeft->bStart || pLeft->iDocid==pRight->iDocid );
177981 assert_fts3_nc( pRight->bStart || pLeft->iDocid==pRight->iDocid );
177982
177983 if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
177984 fts3EvalNextRow(pCsr, pExpr: pLeft, pRc);
177985 }else if( pLeft->bEof || iCmp>0 ){
177986 fts3EvalNextRow(pCsr, pExpr: pRight, pRc);
177987 }else{
177988 fts3EvalNextRow(pCsr, pExpr: pLeft, pRc);
177989 fts3EvalNextRow(pCsr, pExpr: pRight, pRc);
177990 }
177991
177992 pExpr->bEof = (pLeft->bEof && pRight->bEof);
177993 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
177994 if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
177995 pExpr->iDocid = pLeft->iDocid;
177996 }else{
177997 pExpr->iDocid = pRight->iDocid;
177998 }
177999
178000 break;
178001 }
178002
178003 case FTSQUERY_NOT: {
178004 Fts3Expr *pLeft = pExpr->pLeft;
178005 Fts3Expr *pRight = pExpr->pRight;
178006
178007 if( pRight->bStart==0 ){
178008 fts3EvalNextRow(pCsr, pExpr: pRight, pRc);
178009 assert( *pRc!=SQLITE_OK || pRight->bStart );
178010 }
178011
178012 fts3EvalNextRow(pCsr, pExpr: pLeft, pRc);
178013 if( pLeft->bEof==0 ){
178014 while( !*pRc
178015 && !pRight->bEof
178016 && DOCID_CMP(pLeft->iDocid, pRight->iDocid)>0
178017 ){
178018 fts3EvalNextRow(pCsr, pExpr: pRight, pRc);
178019 }
178020 }
178021 pExpr->iDocid = pLeft->iDocid;
178022 pExpr->bEof = pLeft->bEof;
178023 break;
178024 }
178025
178026 default: {
178027 Fts3Phrase *pPhrase = pExpr->pPhrase;
178028 fts3EvalInvalidatePoslist(pPhrase);
178029 *pRc = fts3EvalPhraseNext(pCsr, p: pPhrase, pbEof: &pExpr->bEof);
178030 pExpr->iDocid = pPhrase->doclist.iDocid;
178031 break;
178032 }
178033 }
178034 }
178035}
178036
178037/*
178038** If *pRc is not SQLITE_OK, or if pExpr is not the root node of a NEAR
178039** cluster, then this function returns 1 immediately.
178040**
178041** Otherwise, it checks if the current row really does match the NEAR
178042** expression, using the data currently stored in the position lists
178043** (Fts3Expr->pPhrase.doclist.pList/nList) for each phrase in the expression.
178044**
178045** If the current row is a match, the position list associated with each
178046** phrase in the NEAR expression is edited in place to contain only those
178047** phrase instances sufficiently close to their peers to satisfy all NEAR
178048** constraints. In this case it returns 1. If the NEAR expression does not
178049** match the current row, 0 is returned. The position lists may or may not
178050** be edited if 0 is returned.
178051*/
178052static int fts3EvalNearTest(Fts3Expr *pExpr, int *pRc){
178053 int res = 1;
178054
178055 /* The following block runs if pExpr is the root of a NEAR query.
178056 ** For example, the query:
178057 **
178058 ** "w" NEAR "x" NEAR "y" NEAR "z"
178059 **
178060 ** which is represented in tree form as:
178061 **
178062 ** |
178063 ** +--NEAR--+ <-- root of NEAR query
178064 ** | |
178065 ** +--NEAR--+ "z"
178066 ** | |
178067 ** +--NEAR--+ "y"
178068 ** | |
178069 ** "w" "x"
178070 **
178071 ** The right-hand child of a NEAR node is always a phrase. The
178072 ** left-hand child may be either a phrase or a NEAR node. There are
178073 ** no exceptions to this - it's the way the parser in fts3_expr.c works.
178074 */
178075 if( *pRc==SQLITE_OK
178076 && pExpr->eType==FTSQUERY_NEAR
178077 && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
178078 ){
178079 Fts3Expr *p;
178080 sqlite3_int64 nTmp = 0; /* Bytes of temp space */
178081 char *aTmp; /* Temp space for PoslistNearMerge() */
178082
178083 /* Allocate temporary working space. */
178084 for(p=pExpr; p->pLeft; p=p->pLeft){
178085 assert( p->pRight->pPhrase->doclist.nList>0 );
178086 nTmp += p->pRight->pPhrase->doclist.nList;
178087 }
178088 nTmp += p->pPhrase->doclist.nList;
178089 aTmp = sqlite3_malloc64(n: nTmp*2);
178090 if( !aTmp ){
178091 *pRc = SQLITE_NOMEM;
178092 res = 0;
178093 }else{
178094 char *aPoslist = p->pPhrase->doclist.pList;
178095 int nToken = p->pPhrase->nToken;
178096
178097 for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){
178098 Fts3Phrase *pPhrase = p->pRight->pPhrase;
178099 int nNear = p->nNear;
178100 res = fts3EvalNearTrim(nNear, aTmp, paPoslist: &aPoslist, pnToken: &nToken, pPhrase);
178101 }
178102
178103 aPoslist = pExpr->pRight->pPhrase->doclist.pList;
178104 nToken = pExpr->pRight->pPhrase->nToken;
178105 for(p=pExpr->pLeft; p && res; p=p->pLeft){
178106 int nNear;
178107 Fts3Phrase *pPhrase;
178108 assert( p->pParent && p->pParent->pLeft==p );
178109 nNear = p->pParent->nNear;
178110 pPhrase = (
178111 p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase
178112 );
178113 res = fts3EvalNearTrim(nNear, aTmp, paPoslist: &aPoslist, pnToken: &nToken, pPhrase);
178114 }
178115 }
178116
178117 sqlite3_free(p: aTmp);
178118 }
178119
178120 return res;
178121}
178122
178123/*
178124** This function is a helper function for sqlite3Fts3EvalTestDeferred().
178125** Assuming no error occurs or has occurred, It returns non-zero if the
178126** expression passed as the second argument matches the row that pCsr
178127** currently points to, or zero if it does not.
178128**
178129** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
178130** If an error occurs during execution of this function, *pRc is set to
178131** the appropriate SQLite error code. In this case the returned value is
178132** undefined.
178133*/
178134static int fts3EvalTestExpr(
178135 Fts3Cursor *pCsr, /* FTS cursor handle */
178136 Fts3Expr *pExpr, /* Expr to test. May or may not be root. */
178137 int *pRc /* IN/OUT: Error code */
178138){
178139 int bHit = 1; /* Return value */
178140 if( *pRc==SQLITE_OK ){
178141 switch( pExpr->eType ){
178142 case FTSQUERY_NEAR:
178143 case FTSQUERY_AND:
178144 bHit = (
178145 fts3EvalTestExpr(pCsr, pExpr: pExpr->pLeft, pRc)
178146 && fts3EvalTestExpr(pCsr, pExpr: pExpr->pRight, pRc)
178147 && fts3EvalNearTest(pExpr, pRc)
178148 );
178149
178150 /* If the NEAR expression does not match any rows, zero the doclist for
178151 ** all phrases involved in the NEAR. This is because the snippet(),
178152 ** offsets() and matchinfo() functions are not supposed to recognize
178153 ** any instances of phrases that are part of unmatched NEAR queries.
178154 ** For example if this expression:
178155 **
178156 ** ... MATCH 'a OR (b NEAR c)'
178157 **
178158 ** is matched against a row containing:
178159 **
178160 ** 'a b d e'
178161 **
178162 ** then any snippet() should ony highlight the "a" term, not the "b"
178163 ** (as "b" is part of a non-matching NEAR clause).
178164 */
178165 if( bHit==0
178166 && pExpr->eType==FTSQUERY_NEAR
178167 && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
178168 ){
178169 Fts3Expr *p;
178170 for(p=pExpr; p->pPhrase==0; p=p->pLeft){
178171 if( p->pRight->iDocid==pCsr->iPrevId ){
178172 fts3EvalInvalidatePoslist(pPhrase: p->pRight->pPhrase);
178173 }
178174 }
178175 if( p->iDocid==pCsr->iPrevId ){
178176 fts3EvalInvalidatePoslist(pPhrase: p->pPhrase);
178177 }
178178 }
178179
178180 break;
178181
178182 case FTSQUERY_OR: {
178183 int bHit1 = fts3EvalTestExpr(pCsr, pExpr: pExpr->pLeft, pRc);
178184 int bHit2 = fts3EvalTestExpr(pCsr, pExpr: pExpr->pRight, pRc);
178185 bHit = bHit1 || bHit2;
178186 break;
178187 }
178188
178189 case FTSQUERY_NOT:
178190 bHit = (
178191 fts3EvalTestExpr(pCsr, pExpr: pExpr->pLeft, pRc)
178192 && !fts3EvalTestExpr(pCsr, pExpr: pExpr->pRight, pRc)
178193 );
178194 break;
178195
178196 default: {
178197#ifndef SQLITE_DISABLE_FTS4_DEFERRED
178198 if( pCsr->pDeferred
178199 && (pExpr->iDocid==pCsr->iPrevId || pExpr->bDeferred)
178200 ){
178201 Fts3Phrase *pPhrase = pExpr->pPhrase;
178202 assert( pExpr->bDeferred || pPhrase->doclist.bFreeList==0 );
178203 if( pExpr->bDeferred ){
178204 fts3EvalInvalidatePoslist(pPhrase);
178205 }
178206 *pRc = fts3EvalDeferredPhrase(pCsr, pPhrase);
178207 bHit = (pPhrase->doclist.pList!=0);
178208 pExpr->iDocid = pCsr->iPrevId;
178209 }else
178210#endif
178211 {
178212 bHit = (
178213 pExpr->bEof==0 && pExpr->iDocid==pCsr->iPrevId
178214 && pExpr->pPhrase->doclist.nList>0
178215 );
178216 }
178217 break;
178218 }
178219 }
178220 }
178221 return bHit;
178222}
178223
178224/*
178225** This function is called as the second part of each xNext operation when
178226** iterating through the results of a full-text query. At this point the
178227** cursor points to a row that matches the query expression, with the
178228** following caveats:
178229**
178230** * Up until this point, "NEAR" operators in the expression have been
178231** treated as "AND".
178232**
178233** * Deferred tokens have not yet been considered.
178234**
178235** If *pRc is not SQLITE_OK when this function is called, it immediately
178236** returns 0. Otherwise, it tests whether or not after considering NEAR
178237** operators and deferred tokens the current row is still a match for the
178238** expression. It returns 1 if both of the following are true:
178239**
178240** 1. *pRc is SQLITE_OK when this function returns, and
178241**
178242** 2. After scanning the current FTS table row for the deferred tokens,
178243** it is determined that the row does *not* match the query.
178244**
178245** Or, if no error occurs and it seems the current row does match the FTS
178246** query, return 0.
178247*/
178248SQLITE_PRIVATE int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc){
178249 int rc = *pRc;
178250 int bMiss = 0;
178251 if( rc==SQLITE_OK ){
178252
178253 /* If there are one or more deferred tokens, load the current row into
178254 ** memory and scan it to determine the position list for each deferred
178255 ** token. Then, see if this row is really a match, considering deferred
178256 ** tokens and NEAR operators (neither of which were taken into account
178257 ** earlier, by fts3EvalNextRow()).
178258 */
178259 if( pCsr->pDeferred ){
178260 rc = fts3CursorSeek(pContext: 0, pCsr);
178261 if( rc==SQLITE_OK ){
178262 rc = sqlite3Fts3CacheDeferredDoclists(pCsr);
178263 }
178264 }
178265 bMiss = (0==fts3EvalTestExpr(pCsr, pExpr: pCsr->pExpr, pRc: &rc));
178266
178267 /* Free the position-lists accumulated for each deferred token above. */
178268 sqlite3Fts3FreeDeferredDoclists(pCsr);
178269 *pRc = rc;
178270 }
178271 return (rc==SQLITE_OK && bMiss);
178272}
178273
178274/*
178275** Advance to the next document that matches the FTS expression in
178276** Fts3Cursor.pExpr.
178277*/
178278static int fts3EvalNext(Fts3Cursor *pCsr){
178279 int rc = SQLITE_OK; /* Return Code */
178280 Fts3Expr *pExpr = pCsr->pExpr;
178281 assert( pCsr->isEof==0 );
178282 if( pExpr==0 ){
178283 pCsr->isEof = 1;
178284 }else{
178285 do {
178286 if( pCsr->isRequireSeek==0 ){
178287 sqlite3_reset(pStmt: pCsr->pStmt);
178288 }
178289 assert( sqlite3_data_count(pCsr->pStmt)==0 );
178290 fts3EvalNextRow(pCsr, pExpr, pRc: &rc);
178291 pCsr->isEof = pExpr->bEof;
178292 pCsr->isRequireSeek = 1;
178293 pCsr->isMatchinfoNeeded = 1;
178294 pCsr->iPrevId = pExpr->iDocid;
178295 }while( pCsr->isEof==0 && sqlite3Fts3EvalTestDeferred(pCsr, pRc: &rc) );
178296 }
178297
178298 /* Check if the cursor is past the end of the docid range specified
178299 ** by Fts3Cursor.iMinDocid/iMaxDocid. If so, set the EOF flag. */
178300 if( rc==SQLITE_OK && (
178301 (pCsr->bDesc==0 && pCsr->iPrevId>pCsr->iMaxDocid)
178302 || (pCsr->bDesc!=0 && pCsr->iPrevId<pCsr->iMinDocid)
178303 )){
178304 pCsr->isEof = 1;
178305 }
178306
178307 return rc;
178308}
178309
178310/*
178311** Restart interation for expression pExpr so that the next call to
178312** fts3EvalNext() visits the first row. Do not allow incremental
178313** loading or merging of phrase doclists for this iteration.
178314**
178315** If *pRc is other than SQLITE_OK when this function is called, it is
178316** a no-op. If an error occurs within this function, *pRc is set to an
178317** SQLite error code before returning.
178318*/
178319static void fts3EvalRestart(
178320 Fts3Cursor *pCsr,
178321 Fts3Expr *pExpr,
178322 int *pRc
178323){
178324 if( pExpr && *pRc==SQLITE_OK ){
178325 Fts3Phrase *pPhrase = pExpr->pPhrase;
178326
178327 if( pPhrase ){
178328 fts3EvalInvalidatePoslist(pPhrase);
178329 if( pPhrase->bIncr ){
178330 int i;
178331 for(i=0; i<pPhrase->nToken; i++){
178332 Fts3PhraseToken *pToken = &pPhrase->aToken[i];
178333 assert( pToken->pDeferred==0 );
178334 if( pToken->pSegcsr ){
178335 sqlite3Fts3MsrIncrRestart(pCsr: pToken->pSegcsr);
178336 }
178337 }
178338 *pRc = fts3EvalPhraseStart(pCsr, bOptOk: 0, p: pPhrase);
178339 }
178340 pPhrase->doclist.pNextDocid = 0;
178341 pPhrase->doclist.iDocid = 0;
178342 pPhrase->pOrPoslist = 0;
178343 }
178344
178345 pExpr->iDocid = 0;
178346 pExpr->bEof = 0;
178347 pExpr->bStart = 0;
178348
178349 fts3EvalRestart(pCsr, pExpr: pExpr->pLeft, pRc);
178350 fts3EvalRestart(pCsr, pExpr: pExpr->pRight, pRc);
178351 }
178352}
178353
178354/*
178355** After allocating the Fts3Expr.aMI[] array for each phrase in the
178356** expression rooted at pExpr, the cursor iterates through all rows matched
178357** by pExpr, calling this function for each row. This function increments
178358** the values in Fts3Expr.aMI[] according to the position-list currently
178359** found in Fts3Expr.pPhrase->doclist.pList for each of the phrase
178360** expression nodes.
178361*/
178362static void fts3EvalUpdateCounts(Fts3Expr *pExpr, int nCol){
178363 if( pExpr ){
178364 Fts3Phrase *pPhrase = pExpr->pPhrase;
178365 if( pPhrase && pPhrase->doclist.pList ){
178366 int iCol = 0;
178367 char *p = pPhrase->doclist.pList;
178368
178369 do{
178370 u8 c = 0;
178371 int iCnt = 0;
178372 while( 0xFE & (*p | c) ){
178373 if( (c&0x80)==0 ) iCnt++;
178374 c = *p++ & 0x80;
178375 }
178376
178377 /* aMI[iCol*3 + 1] = Number of occurrences
178378 ** aMI[iCol*3 + 2] = Number of rows containing at least one instance
178379 */
178380 pExpr->aMI[iCol*3 + 1] += iCnt;
178381 pExpr->aMI[iCol*3 + 2] += (iCnt>0);
178382 if( *p==0x00 ) break;
178383 p++;
178384 p += fts3GetVarint32(p, &iCol);
178385 }while( iCol<nCol );
178386 }
178387
178388 fts3EvalUpdateCounts(pExpr: pExpr->pLeft, nCol);
178389 fts3EvalUpdateCounts(pExpr: pExpr->pRight, nCol);
178390 }
178391}
178392
178393/*
178394** Expression pExpr must be of type FTSQUERY_PHRASE.
178395**
178396** If it is not already allocated and populated, this function allocates and
178397** populates the Fts3Expr.aMI[] array for expression pExpr. If pExpr is part
178398** of a NEAR expression, then it also allocates and populates the same array
178399** for all other phrases that are part of the NEAR expression.
178400**
178401** SQLITE_OK is returned if the aMI[] array is successfully allocated and
178402** populated. Otherwise, if an error occurs, an SQLite error code is returned.
178403*/
178404static int fts3EvalGatherStats(
178405 Fts3Cursor *pCsr, /* Cursor object */
178406 Fts3Expr *pExpr /* FTSQUERY_PHRASE expression */
178407){
178408 int rc = SQLITE_OK; /* Return code */
178409
178410 assert( pExpr->eType==FTSQUERY_PHRASE );
178411 if( pExpr->aMI==0 ){
178412 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
178413 Fts3Expr *pRoot; /* Root of NEAR expression */
178414 Fts3Expr *p; /* Iterator used for several purposes */
178415
178416 sqlite3_int64 iPrevId = pCsr->iPrevId;
178417 sqlite3_int64 iDocid;
178418 u8 bEof;
178419
178420 /* Find the root of the NEAR expression */
178421 pRoot = pExpr;
178422 while( pRoot->pParent && pRoot->pParent->eType==FTSQUERY_NEAR ){
178423 pRoot = pRoot->pParent;
178424 }
178425 iDocid = pRoot->iDocid;
178426 bEof = pRoot->bEof;
178427 assert( pRoot->bStart );
178428
178429 /* Allocate space for the aMSI[] array of each FTSQUERY_PHRASE node */
178430 for(p=pRoot; p; p=p->pLeft){
178431 Fts3Expr *pE = (p->eType==FTSQUERY_PHRASE?p:p->pRight);
178432 assert( pE->aMI==0 );
178433 pE->aMI = (u32 *)sqlite3_malloc64(n: pTab->nColumn * 3 * sizeof(u32));
178434 if( !pE->aMI ) return SQLITE_NOMEM;
178435 memset(s: pE->aMI, c: 0, n: pTab->nColumn * 3 * sizeof(u32));
178436 }
178437
178438 fts3EvalRestart(pCsr, pExpr: pRoot, pRc: &rc);
178439
178440 while( pCsr->isEof==0 && rc==SQLITE_OK ){
178441
178442 do {
178443 /* Ensure the %_content statement is reset. */
178444 if( pCsr->isRequireSeek==0 ) sqlite3_reset(pStmt: pCsr->pStmt);
178445 assert( sqlite3_data_count(pCsr->pStmt)==0 );
178446
178447 /* Advance to the next document */
178448 fts3EvalNextRow(pCsr, pExpr: pRoot, pRc: &rc);
178449 pCsr->isEof = pRoot->bEof;
178450 pCsr->isRequireSeek = 1;
178451 pCsr->isMatchinfoNeeded = 1;
178452 pCsr->iPrevId = pRoot->iDocid;
178453 }while( pCsr->isEof==0
178454 && pRoot->eType==FTSQUERY_NEAR
178455 && sqlite3Fts3EvalTestDeferred(pCsr, pRc: &rc)
178456 );
178457
178458 if( rc==SQLITE_OK && pCsr->isEof==0 ){
178459 fts3EvalUpdateCounts(pExpr: pRoot, nCol: pTab->nColumn);
178460 }
178461 }
178462
178463 pCsr->isEof = 0;
178464 pCsr->iPrevId = iPrevId;
178465
178466 if( bEof ){
178467 pRoot->bEof = bEof;
178468 }else{
178469 /* Caution: pRoot may iterate through docids in ascending or descending
178470 ** order. For this reason, even though it seems more defensive, the
178471 ** do loop can not be written:
178472 **
178473 ** do {...} while( pRoot->iDocid<iDocid && rc==SQLITE_OK );
178474 */
178475 fts3EvalRestart(pCsr, pExpr: pRoot, pRc: &rc);
178476 do {
178477 fts3EvalNextRow(pCsr, pExpr: pRoot, pRc: &rc);
178478 assert_fts3_nc( pRoot->bEof==0 );
178479 if( pRoot->bEof ) rc = FTS_CORRUPT_VTAB;
178480 }while( pRoot->iDocid!=iDocid && rc==SQLITE_OK );
178481 }
178482 }
178483 return rc;
178484}
178485
178486/*
178487** This function is used by the matchinfo() module to query a phrase
178488** expression node for the following information:
178489**
178490** 1. The total number of occurrences of the phrase in each column of
178491** the FTS table (considering all rows), and
178492**
178493** 2. For each column, the number of rows in the table for which the
178494** column contains at least one instance of the phrase.
178495**
178496** If no error occurs, SQLITE_OK is returned and the values for each column
178497** written into the array aiOut as follows:
178498**
178499** aiOut[iCol*3 + 1] = Number of occurrences
178500** aiOut[iCol*3 + 2] = Number of rows containing at least one instance
178501**
178502** Caveats:
178503**
178504** * If a phrase consists entirely of deferred tokens, then all output
178505** values are set to the number of documents in the table. In other
178506** words we assume that very common tokens occur exactly once in each
178507** column of each row of the table.
178508**
178509** * If a phrase contains some deferred tokens (and some non-deferred
178510** tokens), count the potential occurrence identified by considering
178511** the non-deferred tokens instead of actual phrase occurrences.
178512**
178513** * If the phrase is part of a NEAR expression, then only phrase instances
178514** that meet the NEAR constraint are included in the counts.
178515*/
178516SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(
178517 Fts3Cursor *pCsr, /* FTS cursor handle */
178518 Fts3Expr *pExpr, /* Phrase expression */
178519 u32 *aiOut /* Array to write results into (see above) */
178520){
178521 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
178522 int rc = SQLITE_OK;
178523 int iCol;
178524
178525 if( pExpr->bDeferred && pExpr->pParent->eType!=FTSQUERY_NEAR ){
178526 assert( pCsr->nDoc>0 );
178527 for(iCol=0; iCol<pTab->nColumn; iCol++){
178528 aiOut[iCol*3 + 1] = (u32)pCsr->nDoc;
178529 aiOut[iCol*3 + 2] = (u32)pCsr->nDoc;
178530 }
178531 }else{
178532 rc = fts3EvalGatherStats(pCsr, pExpr);
178533 if( rc==SQLITE_OK ){
178534 assert( pExpr->aMI );
178535 for(iCol=0; iCol<pTab->nColumn; iCol++){
178536 aiOut[iCol*3 + 1] = pExpr->aMI[iCol*3 + 1];
178537 aiOut[iCol*3 + 2] = pExpr->aMI[iCol*3 + 2];
178538 }
178539 }
178540 }
178541
178542 return rc;
178543}
178544
178545/*
178546** The expression pExpr passed as the second argument to this function
178547** must be of type FTSQUERY_PHRASE.
178548**
178549** The returned value is either NULL or a pointer to a buffer containing
178550** a position-list indicating the occurrences of the phrase in column iCol
178551** of the current row.
178552**
178553** More specifically, the returned buffer contains 1 varint for each
178554** occurrence of the phrase in the column, stored using the normal (delta+2)
178555** compression and is terminated by either an 0x01 or 0x00 byte. For example,
178556** if the requested column contains "a b X c d X X" and the position-list
178557** for 'X' is requested, the buffer returned may contain:
178558**
178559** 0x04 0x05 0x03 0x01 or 0x04 0x05 0x03 0x00
178560**
178561** This function works regardless of whether or not the phrase is deferred,
178562** incremental, or neither.
178563*/
178564SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(
178565 Fts3Cursor *pCsr, /* FTS3 cursor object */
178566 Fts3Expr *pExpr, /* Phrase to return doclist for */
178567 int iCol, /* Column to return position list for */
178568 char **ppOut /* OUT: Pointer to position list */
178569){
178570 Fts3Phrase *pPhrase = pExpr->pPhrase;
178571 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
178572 char *pIter;
178573 int iThis;
178574 sqlite3_int64 iDocid;
178575
178576 /* If this phrase is applies specifically to some column other than
178577 ** column iCol, return a NULL pointer. */
178578 *ppOut = 0;
178579 assert( iCol>=0 && iCol<pTab->nColumn );
178580 if( (pPhrase->iColumn<pTab->nColumn && pPhrase->iColumn!=iCol) ){
178581 return SQLITE_OK;
178582 }
178583
178584 iDocid = pExpr->iDocid;
178585 pIter = pPhrase->doclist.pList;
178586 if( iDocid!=pCsr->iPrevId || pExpr->bEof ){
178587 int rc = SQLITE_OK;
178588 int bDescDoclist = pTab->bDescIdx; /* For DOCID_CMP macro */
178589 int bOr = 0;
178590 u8 bTreeEof = 0;
178591 Fts3Expr *p; /* Used to iterate from pExpr to root */
178592 Fts3Expr *pNear; /* Most senior NEAR ancestor (or pExpr) */
178593 int bMatch;
178594
178595 /* Check if this phrase descends from an OR expression node. If not,
178596 ** return NULL. Otherwise, the entry that corresponds to docid
178597 ** pCsr->iPrevId may lie earlier in the doclist buffer. Or, if the
178598 ** tree that the node is part of has been marked as EOF, but the node
178599 ** itself is not EOF, then it may point to an earlier entry. */
178600 pNear = pExpr;
178601 for(p=pExpr->pParent; p; p=p->pParent){
178602 if( p->eType==FTSQUERY_OR ) bOr = 1;
178603 if( p->eType==FTSQUERY_NEAR ) pNear = p;
178604 if( p->bEof ) bTreeEof = 1;
178605 }
178606 if( bOr==0 ) return SQLITE_OK;
178607
178608 /* This is the descendent of an OR node. In this case we cannot use
178609 ** an incremental phrase. Load the entire doclist for the phrase
178610 ** into memory in this case. */
178611 if( pPhrase->bIncr ){
178612 int bEofSave = pNear->bEof;
178613 fts3EvalRestart(pCsr, pExpr: pNear, pRc: &rc);
178614 while( rc==SQLITE_OK && !pNear->bEof ){
178615 fts3EvalNextRow(pCsr, pExpr: pNear, pRc: &rc);
178616 if( bEofSave==0 && pNear->iDocid==iDocid ) break;
178617 }
178618 assert( rc!=SQLITE_OK || pPhrase->bIncr==0 );
178619 if( rc==SQLITE_OK && pNear->bEof!=bEofSave ){
178620 rc = FTS_CORRUPT_VTAB;
178621 }
178622 }
178623 if( bTreeEof ){
178624 while( rc==SQLITE_OK && !pNear->bEof ){
178625 fts3EvalNextRow(pCsr, pExpr: pNear, pRc: &rc);
178626 }
178627 }
178628 if( rc!=SQLITE_OK ) return rc;
178629
178630 bMatch = 1;
178631 for(p=pNear; p; p=p->pLeft){
178632 u8 bEof = 0;
178633 Fts3Expr *pTest = p;
178634 Fts3Phrase *pPh;
178635 assert( pTest->eType==FTSQUERY_NEAR || pTest->eType==FTSQUERY_PHRASE );
178636 if( pTest->eType==FTSQUERY_NEAR ) pTest = pTest->pRight;
178637 assert( pTest->eType==FTSQUERY_PHRASE );
178638 pPh = pTest->pPhrase;
178639
178640 pIter = pPh->pOrPoslist;
178641 iDocid = pPh->iOrDocid;
178642 if( pCsr->bDesc==bDescDoclist ){
178643 bEof = !pPh->doclist.nAll ||
178644 (pIter >= (pPh->doclist.aAll + pPh->doclist.nAll));
178645 while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)<0 ) && bEof==0 ){
178646 sqlite3Fts3DoclistNext(
178647 bDescIdx: bDescDoclist, aDoclist: pPh->doclist.aAll, nDoclist: pPh->doclist.nAll,
178648 ppIter: &pIter, piDocid: &iDocid, pbEof: &bEof
178649 );
178650 }
178651 }else{
178652 bEof = !pPh->doclist.nAll || (pIter && pIter<=pPh->doclist.aAll);
178653 while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)>0 ) && bEof==0 ){
178654 int dummy;
178655 sqlite3Fts3DoclistPrev(
178656 bDescIdx: bDescDoclist, aDoclist: pPh->doclist.aAll, nDoclist: pPh->doclist.nAll,
178657 ppIter: &pIter, piDocid: &iDocid, pnList: &dummy, pbEof: &bEof
178658 );
178659 }
178660 }
178661 pPh->pOrPoslist = pIter;
178662 pPh->iOrDocid = iDocid;
178663 if( bEof || iDocid!=pCsr->iPrevId ) bMatch = 0;
178664 }
178665
178666 if( bMatch ){
178667 pIter = pPhrase->pOrPoslist;
178668 }else{
178669 pIter = 0;
178670 }
178671 }
178672 if( pIter==0 ) return SQLITE_OK;
178673
178674 if( *pIter==0x01 ){
178675 pIter++;
178676 pIter += fts3GetVarint32(pIter, &iThis);
178677 }else{
178678 iThis = 0;
178679 }
178680 while( iThis<iCol ){
178681 fts3ColumnlistCopy(pp: 0, ppPoslist: &pIter);
178682 if( *pIter==0x00 ) return SQLITE_OK;
178683 pIter++;
178684 pIter += fts3GetVarint32(pIter, &iThis);
178685 }
178686 if( *pIter==0x00 ){
178687 pIter = 0;
178688 }
178689
178690 *ppOut = ((iCol==iThis)?pIter:0);
178691 return SQLITE_OK;
178692}
178693
178694/*
178695** Free all components of the Fts3Phrase structure that were allocated by
178696** the eval module. Specifically, this means to free:
178697**
178698** * the contents of pPhrase->doclist, and
178699** * any Fts3MultiSegReader objects held by phrase tokens.
178700*/
178701SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *pPhrase){
178702 if( pPhrase ){
178703 int i;
178704 sqlite3_free(p: pPhrase->doclist.aAll);
178705 fts3EvalInvalidatePoslist(pPhrase);
178706 memset(s: &pPhrase->doclist, c: 0, n: sizeof(Fts3Doclist));
178707 for(i=0; i<pPhrase->nToken; i++){
178708 fts3SegReaderCursorFree(pSegcsr: pPhrase->aToken[i].pSegcsr);
178709 pPhrase->aToken[i].pSegcsr = 0;
178710 }
178711 }
178712}
178713
178714
178715/*
178716** Return SQLITE_CORRUPT_VTAB.
178717*/
178718#ifdef SQLITE_DEBUG
178719SQLITE_PRIVATE int sqlite3Fts3Corrupt(){
178720 return SQLITE_CORRUPT_VTAB;
178721}
178722#endif
178723
178724#if !SQLITE_CORE
178725/*
178726** Initialize API pointer table, if required.
178727*/
178728#ifdef _WIN32
178729__declspec(dllexport)
178730#endif
178731SQLITE_API int sqlite3_fts3_init(
178732 sqlite3 *db,
178733 char **pzErrMsg,
178734 const sqlite3_api_routines *pApi
178735){
178736 SQLITE_EXTENSION_INIT2(pApi)
178737 return sqlite3Fts3Init(db);
178738}
178739#endif
178740
178741#endif
178742
178743/************** End of fts3.c ************************************************/
178744/************** Begin file fts3_aux.c ****************************************/
178745/*
178746** 2011 Jan 27
178747**
178748** The author disclaims copyright to this source code. In place of
178749** a legal notice, here is a blessing:
178750**
178751** May you do good and not evil.
178752** May you find forgiveness for yourself and forgive others.
178753** May you share freely, never taking more than you give.
178754**
178755******************************************************************************
178756**
178757*/
178758/* #include "fts3Int.h" */
178759#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
178760
178761/* #include <string.h> */
178762/* #include <assert.h> */
178763
178764typedef struct Fts3auxTable Fts3auxTable;
178765typedef struct Fts3auxCursor Fts3auxCursor;
178766
178767struct Fts3auxTable {
178768 sqlite3_vtab base; /* Base class used by SQLite core */
178769 Fts3Table *pFts3Tab;
178770};
178771
178772struct Fts3auxCursor {
178773 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
178774 Fts3MultiSegReader csr; /* Must be right after "base" */
178775 Fts3SegFilter filter;
178776 char *zStop;
178777 int nStop; /* Byte-length of string zStop */
178778 int iLangid; /* Language id to query */
178779 int isEof; /* True if cursor is at EOF */
178780 sqlite3_int64 iRowid; /* Current rowid */
178781
178782 int iCol; /* Current value of 'col' column */
178783 int nStat; /* Size of aStat[] array */
178784 struct Fts3auxColstats {
178785 sqlite3_int64 nDoc; /* 'documents' values for current csr row */
178786 sqlite3_int64 nOcc; /* 'occurrences' values for current csr row */
178787 } *aStat;
178788};
178789
178790/*
178791** Schema of the terms table.
178792*/
178793#define FTS3_AUX_SCHEMA \
178794 "CREATE TABLE x(term, col, documents, occurrences, languageid HIDDEN)"
178795
178796/*
178797** This function does all the work for both the xConnect and xCreate methods.
178798** These tables have no persistent representation of their own, so xConnect
178799** and xCreate are identical operations.
178800*/
178801static int fts3auxConnectMethod(
178802 sqlite3 *db, /* Database connection */
178803 void *pUnused, /* Unused */
178804 int argc, /* Number of elements in argv array */
178805 const char * const *argv, /* xCreate/xConnect argument array */
178806 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
178807 char **pzErr /* OUT: sqlite3_malloc'd error message */
178808){
178809 char const *zDb; /* Name of database (e.g. "main") */
178810 char const *zFts3; /* Name of fts3 table */
178811 int nDb; /* Result of strlen(zDb) */
178812 int nFts3; /* Result of strlen(zFts3) */
178813 sqlite3_int64 nByte; /* Bytes of space to allocate here */
178814 int rc; /* value returned by declare_vtab() */
178815 Fts3auxTable *p; /* Virtual table object to return */
178816
178817 UNUSED_PARAMETER(pUnused);
178818
178819 /* The user should invoke this in one of two forms:
178820 **
178821 ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table);
178822 ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table-db, fts4-table);
178823 */
178824 if( argc!=4 && argc!=5 ) goto bad_args;
178825
178826 zDb = argv[1];
178827 nDb = (int)strlen(s: zDb);
178828 if( argc==5 ){
178829 if( nDb==4 && 0==sqlite3_strnicmp(zLeft: "temp", zRight: zDb, N: 4) ){
178830 zDb = argv[3];
178831 nDb = (int)strlen(s: zDb);
178832 zFts3 = argv[4];
178833 }else{
178834 goto bad_args;
178835 }
178836 }else{
178837 zFts3 = argv[3];
178838 }
178839 nFts3 = (int)strlen(s: zFts3);
178840
178841 rc = sqlite3_declare_vtab(db, FTS3_AUX_SCHEMA);
178842 if( rc!=SQLITE_OK ) return rc;
178843
178844 nByte = sizeof(Fts3auxTable) + sizeof(Fts3Table) + nDb + nFts3 + 2;
178845 p = (Fts3auxTable *)sqlite3_malloc64(n: nByte);
178846 if( !p ) return SQLITE_NOMEM;
178847 memset(s: p, c: 0, n: nByte);
178848
178849 p->pFts3Tab = (Fts3Table *)&p[1];
178850 p->pFts3Tab->zDb = (char *)&p->pFts3Tab[1];
178851 p->pFts3Tab->zName = &p->pFts3Tab->zDb[nDb+1];
178852 p->pFts3Tab->db = db;
178853 p->pFts3Tab->nIndex = 1;
178854
178855 memcpy(dest: (char *)p->pFts3Tab->zDb, src: zDb, n: nDb);
178856 memcpy(dest: (char *)p->pFts3Tab->zName, src: zFts3, n: nFts3);
178857 sqlite3Fts3Dequote(z: (char *)p->pFts3Tab->zName);
178858
178859 *ppVtab = (sqlite3_vtab *)p;
178860 return SQLITE_OK;
178861
178862 bad_args:
178863 sqlite3Fts3ErrMsg(pzErr, zFormat: "invalid arguments to fts4aux constructor");
178864 return SQLITE_ERROR;
178865}
178866
178867/*
178868** This function does the work for both the xDisconnect and xDestroy methods.
178869** These tables have no persistent representation of their own, so xDisconnect
178870** and xDestroy are identical operations.
178871*/
178872static int fts3auxDisconnectMethod(sqlite3_vtab *pVtab){
178873 Fts3auxTable *p = (Fts3auxTable *)pVtab;
178874 Fts3Table *pFts3 = p->pFts3Tab;
178875 int i;
178876
178877 /* Free any prepared statements held */
178878 for(i=0; i<SizeofArray(pFts3->aStmt); i++){
178879 sqlite3_finalize(pStmt: pFts3->aStmt[i]);
178880 }
178881 sqlite3_free(p: pFts3->zSegmentsTbl);
178882 sqlite3_free(p);
178883 return SQLITE_OK;
178884}
178885
178886#define FTS4AUX_EQ_CONSTRAINT 1
178887#define FTS4AUX_GE_CONSTRAINT 2
178888#define FTS4AUX_LE_CONSTRAINT 4
178889
178890/*
178891** xBestIndex - Analyze a WHERE and ORDER BY clause.
178892*/
178893static int fts3auxBestIndexMethod(
178894 sqlite3_vtab *pVTab,
178895 sqlite3_index_info *pInfo
178896){
178897 int i;
178898 int iEq = -1;
178899 int iGe = -1;
178900 int iLe = -1;
178901 int iLangid = -1;
178902 int iNext = 1; /* Next free argvIndex value */
178903
178904 UNUSED_PARAMETER(pVTab);
178905
178906 /* This vtab delivers always results in "ORDER BY term ASC" order. */
178907 if( pInfo->nOrderBy==1
178908 && pInfo->aOrderBy[0].iColumn==0
178909 && pInfo->aOrderBy[0].desc==0
178910 ){
178911 pInfo->orderByConsumed = 1;
178912 }
178913
178914 /* Search for equality and range constraints on the "term" column.
178915 ** And equality constraints on the hidden "languageid" column. */
178916 for(i=0; i<pInfo->nConstraint; i++){
178917 if( pInfo->aConstraint[i].usable ){
178918 int op = pInfo->aConstraint[i].op;
178919 int iCol = pInfo->aConstraint[i].iColumn;
178920
178921 if( iCol==0 ){
178922 if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iEq = i;
178923 if( op==SQLITE_INDEX_CONSTRAINT_LT ) iLe = i;
178924 if( op==SQLITE_INDEX_CONSTRAINT_LE ) iLe = i;
178925 if( op==SQLITE_INDEX_CONSTRAINT_GT ) iGe = i;
178926 if( op==SQLITE_INDEX_CONSTRAINT_GE ) iGe = i;
178927 }
178928 if( iCol==4 ){
178929 if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iLangid = i;
178930 }
178931 }
178932 }
178933
178934 if( iEq>=0 ){
178935 pInfo->idxNum = FTS4AUX_EQ_CONSTRAINT;
178936 pInfo->aConstraintUsage[iEq].argvIndex = iNext++;
178937 pInfo->estimatedCost = 5;
178938 }else{
178939 pInfo->idxNum = 0;
178940 pInfo->estimatedCost = 20000;
178941 if( iGe>=0 ){
178942 pInfo->idxNum += FTS4AUX_GE_CONSTRAINT;
178943 pInfo->aConstraintUsage[iGe].argvIndex = iNext++;
178944 pInfo->estimatedCost /= 2;
178945 }
178946 if( iLe>=0 ){
178947 pInfo->idxNum += FTS4AUX_LE_CONSTRAINT;
178948 pInfo->aConstraintUsage[iLe].argvIndex = iNext++;
178949 pInfo->estimatedCost /= 2;
178950 }
178951 }
178952 if( iLangid>=0 ){
178953 pInfo->aConstraintUsage[iLangid].argvIndex = iNext++;
178954 pInfo->estimatedCost--;
178955 }
178956
178957 return SQLITE_OK;
178958}
178959
178960/*
178961** xOpen - Open a cursor.
178962*/
178963static int fts3auxOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
178964 Fts3auxCursor *pCsr; /* Pointer to cursor object to return */
178965
178966 UNUSED_PARAMETER(pVTab);
178967
178968 pCsr = (Fts3auxCursor *)sqlite3_malloc(n: sizeof(Fts3auxCursor));
178969 if( !pCsr ) return SQLITE_NOMEM;
178970 memset(s: pCsr, c: 0, n: sizeof(Fts3auxCursor));
178971
178972 *ppCsr = (sqlite3_vtab_cursor *)pCsr;
178973 return SQLITE_OK;
178974}
178975
178976/*
178977** xClose - Close a cursor.
178978*/
178979static int fts3auxCloseMethod(sqlite3_vtab_cursor *pCursor){
178980 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
178981 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
178982
178983 sqlite3Fts3SegmentsClose(pFts3);
178984 sqlite3Fts3SegReaderFinish(&pCsr->csr);
178985 sqlite3_free(p: (void *)pCsr->filter.zTerm);
178986 sqlite3_free(p: pCsr->zStop);
178987 sqlite3_free(p: pCsr->aStat);
178988 sqlite3_free(p: pCsr);
178989 return SQLITE_OK;
178990}
178991
178992static int fts3auxGrowStatArray(Fts3auxCursor *pCsr, int nSize){
178993 if( nSize>pCsr->nStat ){
178994 struct Fts3auxColstats *aNew;
178995 aNew = (struct Fts3auxColstats *)sqlite3_realloc64(pOld: pCsr->aStat,
178996 n: sizeof(struct Fts3auxColstats) * nSize
178997 );
178998 if( aNew==0 ) return SQLITE_NOMEM;
178999 memset(s: &aNew[pCsr->nStat], c: 0,
179000 n: sizeof(struct Fts3auxColstats) * (nSize - pCsr->nStat)
179001 );
179002 pCsr->aStat = aNew;
179003 pCsr->nStat = nSize;
179004 }
179005 return SQLITE_OK;
179006}
179007
179008/*
179009** xNext - Advance the cursor to the next row, if any.
179010*/
179011static int fts3auxNextMethod(sqlite3_vtab_cursor *pCursor){
179012 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
179013 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
179014 int rc;
179015
179016 /* Increment our pretend rowid value. */
179017 pCsr->iRowid++;
179018
179019 for(pCsr->iCol++; pCsr->iCol<pCsr->nStat; pCsr->iCol++){
179020 if( pCsr->aStat[pCsr->iCol].nDoc>0 ) return SQLITE_OK;
179021 }
179022
179023 rc = sqlite3Fts3SegReaderStep(pFts3, &pCsr->csr);
179024 if( rc==SQLITE_ROW ){
179025 int i = 0;
179026 int nDoclist = pCsr->csr.nDoclist;
179027 char *aDoclist = pCsr->csr.aDoclist;
179028 int iCol;
179029
179030 int eState = 0;
179031
179032 if( pCsr->zStop ){
179033 int n = (pCsr->nStop<pCsr->csr.nTerm) ? pCsr->nStop : pCsr->csr.nTerm;
179034 int mc = memcmp(s1: pCsr->zStop, s2: pCsr->csr.zTerm, n: n);
179035 if( mc<0 || (mc==0 && pCsr->csr.nTerm>pCsr->nStop) ){
179036 pCsr->isEof = 1;
179037 return SQLITE_OK;
179038 }
179039 }
179040
179041 if( fts3auxGrowStatArray(pCsr, nSize: 2) ) return SQLITE_NOMEM;
179042 memset(s: pCsr->aStat, c: 0, n: sizeof(struct Fts3auxColstats) * pCsr->nStat);
179043 iCol = 0;
179044 rc = SQLITE_OK;
179045
179046 while( i<nDoclist ){
179047 sqlite3_int64 v = 0;
179048
179049 i += sqlite3Fts3GetVarint(pBuf: &aDoclist[i], v: &v);
179050 switch( eState ){
179051 /* State 0. In this state the integer just read was a docid. */
179052 case 0:
179053 pCsr->aStat[0].nDoc++;
179054 eState = 1;
179055 iCol = 0;
179056 break;
179057
179058 /* State 1. In this state we are expecting either a 1, indicating
179059 ** that the following integer will be a column number, or the
179060 ** start of a position list for column 0.
179061 **
179062 ** The only difference between state 1 and state 2 is that if the
179063 ** integer encountered in state 1 is not 0 or 1, then we need to
179064 ** increment the column 0 "nDoc" count for this term.
179065 */
179066 case 1:
179067 assert( iCol==0 );
179068 if( v>1 ){
179069 pCsr->aStat[1].nDoc++;
179070 }
179071 eState = 2;
179072 /* fall through */
179073
179074 case 2:
179075 if( v==0 ){ /* 0x00. Next integer will be a docid. */
179076 eState = 0;
179077 }else if( v==1 ){ /* 0x01. Next integer will be a column number. */
179078 eState = 3;
179079 }else{ /* 2 or greater. A position. */
179080 pCsr->aStat[iCol+1].nOcc++;
179081 pCsr->aStat[0].nOcc++;
179082 }
179083 break;
179084
179085 /* State 3. The integer just read is a column number. */
179086 default: assert( eState==3 );
179087 iCol = (int)v;
179088 if( iCol<1 ){
179089 rc = SQLITE_CORRUPT_VTAB;
179090 break;
179091 }
179092 if( fts3auxGrowStatArray(pCsr, nSize: iCol+2) ) return SQLITE_NOMEM;
179093 pCsr->aStat[iCol+1].nDoc++;
179094 eState = 2;
179095 break;
179096 }
179097 }
179098
179099 pCsr->iCol = 0;
179100 }else{
179101 pCsr->isEof = 1;
179102 }
179103 return rc;
179104}
179105
179106/*
179107** xFilter - Initialize a cursor to point at the start of its data.
179108*/
179109static int fts3auxFilterMethod(
179110 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
179111 int idxNum, /* Strategy index */
179112 const char *idxStr, /* Unused */
179113 int nVal, /* Number of elements in apVal */
179114 sqlite3_value **apVal /* Arguments for the indexing scheme */
179115){
179116 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
179117 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
179118 int rc;
179119 int isScan = 0;
179120 int iLangVal = 0; /* Language id to query */
179121
179122 int iEq = -1; /* Index of term=? value in apVal */
179123 int iGe = -1; /* Index of term>=? value in apVal */
179124 int iLe = -1; /* Index of term<=? value in apVal */
179125 int iLangid = -1; /* Index of languageid=? value in apVal */
179126 int iNext = 0;
179127
179128 UNUSED_PARAMETER(nVal);
179129 UNUSED_PARAMETER(idxStr);
179130
179131 assert( idxStr==0 );
179132 assert( idxNum==FTS4AUX_EQ_CONSTRAINT || idxNum==0
179133 || idxNum==FTS4AUX_LE_CONSTRAINT || idxNum==FTS4AUX_GE_CONSTRAINT
179134 || idxNum==(FTS4AUX_LE_CONSTRAINT|FTS4AUX_GE_CONSTRAINT)
179135 );
179136
179137 if( idxNum==FTS4AUX_EQ_CONSTRAINT ){
179138 iEq = iNext++;
179139 }else{
179140 isScan = 1;
179141 if( idxNum & FTS4AUX_GE_CONSTRAINT ){
179142 iGe = iNext++;
179143 }
179144 if( idxNum & FTS4AUX_LE_CONSTRAINT ){
179145 iLe = iNext++;
179146 }
179147 }
179148 if( iNext<nVal ){
179149 iLangid = iNext++;
179150 }
179151
179152 /* In case this cursor is being reused, close and zero it. */
179153 testcase(pCsr->filter.zTerm);
179154 sqlite3Fts3SegReaderFinish(&pCsr->csr);
179155 sqlite3_free(p: (void *)pCsr->filter.zTerm);
179156 sqlite3_free(p: pCsr->aStat);
179157 sqlite3_free(p: pCsr->zStop);
179158 memset(s: &pCsr->csr, c: 0, n: ((u8*)&pCsr[1]) - (u8*)&pCsr->csr);
179159
179160 pCsr->filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
179161 if( isScan ) pCsr->filter.flags |= FTS3_SEGMENT_SCAN;
179162
179163 if( iEq>=0 || iGe>=0 ){
179164 const unsigned char *zStr = sqlite3_value_text(pVal: apVal[0]);
179165 assert( (iEq==0 && iGe==-1) || (iEq==-1 && iGe==0) );
179166 if( zStr ){
179167 pCsr->filter.zTerm = sqlite3_mprintf(zFormat: "%s", zStr);
179168 if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM;
179169 pCsr->filter.nTerm = (int)strlen(s: pCsr->filter.zTerm);
179170 }
179171 }
179172
179173 if( iLe>=0 ){
179174 pCsr->zStop = sqlite3_mprintf(zFormat: "%s", sqlite3_value_text(pVal: apVal[iLe]));
179175 if( pCsr->zStop==0 ) return SQLITE_NOMEM;
179176 pCsr->nStop = (int)strlen(s: pCsr->zStop);
179177 }
179178
179179 if( iLangid>=0 ){
179180 iLangVal = sqlite3_value_int(pVal: apVal[iLangid]);
179181
179182 /* If the user specified a negative value for the languageid, use zero
179183 ** instead. This works, as the "languageid=?" constraint will also
179184 ** be tested by the VDBE layer. The test will always be false (since
179185 ** this module will not return a row with a negative languageid), and
179186 ** so the overall query will return zero rows. */
179187 if( iLangVal<0 ) iLangVal = 0;
179188 }
179189 pCsr->iLangid = iLangVal;
179190
179191 rc = sqlite3Fts3SegReaderCursor(p: pFts3, iLangid: iLangVal, iIndex: 0, FTS3_SEGCURSOR_ALL,
179192 zTerm: pCsr->filter.zTerm, nTerm: pCsr->filter.nTerm, isPrefix: 0, isScan, pCsr: &pCsr->csr
179193 );
179194 if( rc==SQLITE_OK ){
179195 rc = sqlite3Fts3SegReaderStart(pFts3, &pCsr->csr, &pCsr->filter);
179196 }
179197
179198 if( rc==SQLITE_OK ) rc = fts3auxNextMethod(pCursor);
179199 return rc;
179200}
179201
179202/*
179203** xEof - Return true if the cursor is at EOF, or false otherwise.
179204*/
179205static int fts3auxEofMethod(sqlite3_vtab_cursor *pCursor){
179206 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
179207 return pCsr->isEof;
179208}
179209
179210/*
179211** xColumn - Return a column value.
179212*/
179213static int fts3auxColumnMethod(
179214 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
179215 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
179216 int iCol /* Index of column to read value from */
179217){
179218 Fts3auxCursor *p = (Fts3auxCursor *)pCursor;
179219
179220 assert( p->isEof==0 );
179221 switch( iCol ){
179222 case 0: /* term */
179223 sqlite3_result_text(pCtx, z: p->csr.zTerm, n: p->csr.nTerm, SQLITE_TRANSIENT);
179224 break;
179225
179226 case 1: /* col */
179227 if( p->iCol ){
179228 sqlite3_result_int(pCtx, iVal: p->iCol-1);
179229 }else{
179230 sqlite3_result_text(pCtx, z: "*", n: -1, SQLITE_STATIC);
179231 }
179232 break;
179233
179234 case 2: /* documents */
179235 sqlite3_result_int64(pCtx, iVal: p->aStat[p->iCol].nDoc);
179236 break;
179237
179238 case 3: /* occurrences */
179239 sqlite3_result_int64(pCtx, iVal: p->aStat[p->iCol].nOcc);
179240 break;
179241
179242 default: /* languageid */
179243 assert( iCol==4 );
179244 sqlite3_result_int(pCtx, iVal: p->iLangid);
179245 break;
179246 }
179247
179248 return SQLITE_OK;
179249}
179250
179251/*
179252** xRowid - Return the current rowid for the cursor.
179253*/
179254static int fts3auxRowidMethod(
179255 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
179256 sqlite_int64 *pRowid /* OUT: Rowid value */
179257){
179258 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
179259 *pRowid = pCsr->iRowid;
179260 return SQLITE_OK;
179261}
179262
179263/*
179264** Register the fts3aux module with database connection db. Return SQLITE_OK
179265** if successful or an error code if sqlite3_create_module() fails.
179266*/
179267SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db){
179268 static const sqlite3_module fts3aux_module = {
179269 0, /* iVersion */
179270 fts3auxConnectMethod, /* xCreate */
179271 fts3auxConnectMethod, /* xConnect */
179272 fts3auxBestIndexMethod, /* xBestIndex */
179273 fts3auxDisconnectMethod, /* xDisconnect */
179274 fts3auxDisconnectMethod, /* xDestroy */
179275 fts3auxOpenMethod, /* xOpen */
179276 fts3auxCloseMethod, /* xClose */
179277 fts3auxFilterMethod, /* xFilter */
179278 fts3auxNextMethod, /* xNext */
179279 fts3auxEofMethod, /* xEof */
179280 fts3auxColumnMethod, /* xColumn */
179281 fts3auxRowidMethod, /* xRowid */
179282 0, /* xUpdate */
179283 0, /* xBegin */
179284 0, /* xSync */
179285 0, /* xCommit */
179286 0, /* xRollback */
179287 0, /* xFindFunction */
179288 0, /* xRename */
179289 0, /* xSavepoint */
179290 0, /* xRelease */
179291 0, /* xRollbackTo */
179292 0 /* xShadowName */
179293 };
179294 int rc; /* Return code */
179295
179296 rc = sqlite3_create_module(db, zName: "fts4aux", pModule: &fts3aux_module, pAux: 0);
179297 return rc;
179298}
179299
179300#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
179301
179302/************** End of fts3_aux.c ********************************************/
179303/************** Begin file fts3_expr.c ***************************************/
179304/*
179305** 2008 Nov 28
179306**
179307** The author disclaims copyright to this source code. In place of
179308** a legal notice, here is a blessing:
179309**
179310** May you do good and not evil.
179311** May you find forgiveness for yourself and forgive others.
179312** May you share freely, never taking more than you give.
179313**
179314******************************************************************************
179315**
179316** This module contains code that implements a parser for fts3 query strings
179317** (the right-hand argument to the MATCH operator). Because the supported
179318** syntax is relatively simple, the whole tokenizer/parser system is
179319** hand-coded.
179320*/
179321/* #include "fts3Int.h" */
179322#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
179323
179324/*
179325** By default, this module parses the legacy syntax that has been
179326** traditionally used by fts3. Or, if SQLITE_ENABLE_FTS3_PARENTHESIS
179327** is defined, then it uses the new syntax. The differences between
179328** the new and the old syntaxes are:
179329**
179330** a) The new syntax supports parenthesis. The old does not.
179331**
179332** b) The new syntax supports the AND and NOT operators. The old does not.
179333**
179334** c) The old syntax supports the "-" token qualifier. This is not
179335** supported by the new syntax (it is replaced by the NOT operator).
179336**
179337** d) When using the old syntax, the OR operator has a greater precedence
179338** than an implicit AND. When using the new, both implicity and explicit
179339** AND operators have a higher precedence than OR.
179340**
179341** If compiled with SQLITE_TEST defined, then this module exports the
179342** symbol "int sqlite3_fts3_enable_parentheses". Setting this variable
179343** to zero causes the module to use the old syntax. If it is set to
179344** non-zero the new syntax is activated. This is so both syntaxes can
179345** be tested using a single build of testfixture.
179346**
179347** The following describes the syntax supported by the fts3 MATCH
179348** operator in a similar format to that used by the lemon parser
179349** generator. This module does not use actually lemon, it uses a
179350** custom parser.
179351**
179352** query ::= andexpr (OR andexpr)*.
179353**
179354** andexpr ::= notexpr (AND? notexpr)*.
179355**
179356** notexpr ::= nearexpr (NOT nearexpr|-TOKEN)*.
179357** notexpr ::= LP query RP.
179358**
179359** nearexpr ::= phrase (NEAR distance_opt nearexpr)*.
179360**
179361** distance_opt ::= .
179362** distance_opt ::= / INTEGER.
179363**
179364** phrase ::= TOKEN.
179365** phrase ::= COLUMN:TOKEN.
179366** phrase ::= "TOKEN TOKEN TOKEN...".
179367*/
179368
179369#ifdef SQLITE_TEST
179370SQLITE_API int sqlite3_fts3_enable_parentheses = 0;
179371#else
179372# ifdef SQLITE_ENABLE_FTS3_PARENTHESIS
179373# define sqlite3_fts3_enable_parentheses 1
179374# else
179375# define sqlite3_fts3_enable_parentheses 0
179376# endif
179377#endif
179378
179379/*
179380** Default span for NEAR operators.
179381*/
179382#define SQLITE_FTS3_DEFAULT_NEAR_PARAM 10
179383
179384/* #include <string.h> */
179385/* #include <assert.h> */
179386
179387/*
179388** isNot:
179389** This variable is used by function getNextNode(). When getNextNode() is
179390** called, it sets ParseContext.isNot to true if the 'next node' is a
179391** FTSQUERY_PHRASE with a unary "-" attached to it. i.e. "mysql" in the
179392** FTS3 query "sqlite -mysql". Otherwise, ParseContext.isNot is set to
179393** zero.
179394*/
179395typedef struct ParseContext ParseContext;
179396struct ParseContext {
179397 sqlite3_tokenizer *pTokenizer; /* Tokenizer module */
179398 int iLangid; /* Language id used with tokenizer */
179399 const char **azCol; /* Array of column names for fts3 table */
179400 int bFts4; /* True to allow FTS4-only syntax */
179401 int nCol; /* Number of entries in azCol[] */
179402 int iDefaultCol; /* Default column to query */
179403 int isNot; /* True if getNextNode() sees a unary - */
179404 sqlite3_context *pCtx; /* Write error message here */
179405 int nNest; /* Number of nested brackets */
179406};
179407
179408/*
179409** This function is equivalent to the standard isspace() function.
179410**
179411** The standard isspace() can be awkward to use safely, because although it
179412** is defined to accept an argument of type int, its behavior when passed
179413** an integer that falls outside of the range of the unsigned char type
179414** is undefined (and sometimes, "undefined" means segfault). This wrapper
179415** is defined to accept an argument of type char, and always returns 0 for
179416** any values that fall outside of the range of the unsigned char type (i.e.
179417** negative values).
179418*/
179419static int fts3isspace(char c){
179420 return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
179421}
179422
179423/*
179424** Allocate nByte bytes of memory using sqlite3_malloc(). If successful,
179425** zero the memory before returning a pointer to it. If unsuccessful,
179426** return NULL.
179427*/
179428SQLITE_PRIVATE void *sqlite3Fts3MallocZero(sqlite3_int64 nByte){
179429 void *pRet = sqlite3_malloc64(n: nByte);
179430 if( pRet ) memset(s: pRet, c: 0, n: nByte);
179431 return pRet;
179432}
179433
179434SQLITE_PRIVATE int sqlite3Fts3OpenTokenizer(
179435 sqlite3_tokenizer *pTokenizer,
179436 int iLangid,
179437 const char *z,
179438 int n,
179439 sqlite3_tokenizer_cursor **ppCsr
179440){
179441 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
179442 sqlite3_tokenizer_cursor *pCsr = 0;
179443 int rc;
179444
179445 rc = pModule->xOpen(pTokenizer, z, n, &pCsr);
179446 assert( rc==SQLITE_OK || pCsr==0 );
179447 if( rc==SQLITE_OK ){
179448 pCsr->pTokenizer = pTokenizer;
179449 if( pModule->iVersion>=1 ){
179450 rc = pModule->xLanguageid(pCsr, iLangid);
179451 if( rc!=SQLITE_OK ){
179452 pModule->xClose(pCsr);
179453 pCsr = 0;
179454 }
179455 }
179456 }
179457 *ppCsr = pCsr;
179458 return rc;
179459}
179460
179461/*
179462** Function getNextNode(), which is called by fts3ExprParse(), may itself
179463** call fts3ExprParse(). So this forward declaration is required.
179464*/
179465static int fts3ExprParse(ParseContext *, const char *, int, Fts3Expr **, int *);
179466
179467/*
179468** Extract the next token from buffer z (length n) using the tokenizer
179469** and other information (column names etc.) in pParse. Create an Fts3Expr
179470** structure of type FTSQUERY_PHRASE containing a phrase consisting of this
179471** single token and set *ppExpr to point to it. If the end of the buffer is
179472** reached before a token is found, set *ppExpr to zero. It is the
179473** responsibility of the caller to eventually deallocate the allocated
179474** Fts3Expr structure (if any) by passing it to sqlite3_free().
179475**
179476** Return SQLITE_OK if successful, or SQLITE_NOMEM if a memory allocation
179477** fails.
179478*/
179479static int getNextToken(
179480 ParseContext *pParse, /* fts3 query parse context */
179481 int iCol, /* Value for Fts3Phrase.iColumn */
179482 const char *z, int n, /* Input string */
179483 Fts3Expr **ppExpr, /* OUT: expression */
179484 int *pnConsumed /* OUT: Number of bytes consumed */
179485){
179486 sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
179487 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
179488 int rc;
179489 sqlite3_tokenizer_cursor *pCursor;
179490 Fts3Expr *pRet = 0;
179491 int i = 0;
179492
179493 /* Set variable i to the maximum number of bytes of input to tokenize. */
179494 for(i=0; i<n; i++){
179495 if( sqlite3_fts3_enable_parentheses && (z[i]=='(' || z[i]==')') ) break;
179496 if( z[i]=='"' ) break;
179497 }
179498
179499 *pnConsumed = i;
179500 rc = sqlite3Fts3OpenTokenizer(pTokenizer, iLangid: pParse->iLangid, z, n: i, ppCsr: &pCursor);
179501 if( rc==SQLITE_OK ){
179502 const char *zToken;
179503 int nToken = 0, iStart = 0, iEnd = 0, iPosition = 0;
179504 sqlite3_int64 nByte; /* total space to allocate */
179505
179506 rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
179507 if( rc==SQLITE_OK ){
179508 nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase) + nToken;
179509 pRet = (Fts3Expr *)sqlite3Fts3MallocZero(nByte);
179510 if( !pRet ){
179511 rc = SQLITE_NOMEM;
179512 }else{
179513 pRet->eType = FTSQUERY_PHRASE;
179514 pRet->pPhrase = (Fts3Phrase *)&pRet[1];
179515 pRet->pPhrase->nToken = 1;
179516 pRet->pPhrase->iColumn = iCol;
179517 pRet->pPhrase->aToken[0].n = nToken;
179518 pRet->pPhrase->aToken[0].z = (char *)&pRet->pPhrase[1];
179519 memcpy(dest: pRet->pPhrase->aToken[0].z, src: zToken, n: nToken);
179520
179521 if( iEnd<n && z[iEnd]=='*' ){
179522 pRet->pPhrase->aToken[0].isPrefix = 1;
179523 iEnd++;
179524 }
179525
179526 while( 1 ){
179527 if( !sqlite3_fts3_enable_parentheses
179528 && iStart>0 && z[iStart-1]=='-'
179529 ){
179530 pParse->isNot = 1;
179531 iStart--;
179532 }else if( pParse->bFts4 && iStart>0 && z[iStart-1]=='^' ){
179533 pRet->pPhrase->aToken[0].bFirst = 1;
179534 iStart--;
179535 }else{
179536 break;
179537 }
179538 }
179539
179540 }
179541 *pnConsumed = iEnd;
179542 }else if( i && rc==SQLITE_DONE ){
179543 rc = SQLITE_OK;
179544 }
179545
179546 pModule->xClose(pCursor);
179547 }
179548
179549 *ppExpr = pRet;
179550 return rc;
179551}
179552
179553
179554/*
179555** Enlarge a memory allocation. If an out-of-memory allocation occurs,
179556** then free the old allocation.
179557*/
179558static void *fts3ReallocOrFree(void *pOrig, sqlite3_int64 nNew){
179559 void *pRet = sqlite3_realloc64(pOld: pOrig, n: nNew);
179560 if( !pRet ){
179561 sqlite3_free(p: pOrig);
179562 }
179563 return pRet;
179564}
179565
179566/*
179567** Buffer zInput, length nInput, contains the contents of a quoted string
179568** that appeared as part of an fts3 query expression. Neither quote character
179569** is included in the buffer. This function attempts to tokenize the entire
179570** input buffer and create an Fts3Expr structure of type FTSQUERY_PHRASE
179571** containing the results.
179572**
179573** If successful, SQLITE_OK is returned and *ppExpr set to point at the
179574** allocated Fts3Expr structure. Otherwise, either SQLITE_NOMEM (out of memory
179575** error) or SQLITE_ERROR (tokenization error) is returned and *ppExpr set
179576** to 0.
179577*/
179578static int getNextString(
179579 ParseContext *pParse, /* fts3 query parse context */
179580 const char *zInput, int nInput, /* Input string */
179581 Fts3Expr **ppExpr /* OUT: expression */
179582){
179583 sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
179584 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
179585 int rc;
179586 Fts3Expr *p = 0;
179587 sqlite3_tokenizer_cursor *pCursor = 0;
179588 char *zTemp = 0;
179589 int nTemp = 0;
179590
179591 const int nSpace = sizeof(Fts3Expr) + sizeof(Fts3Phrase);
179592 int nToken = 0;
179593
179594 /* The final Fts3Expr data structure, including the Fts3Phrase,
179595 ** Fts3PhraseToken structures token buffers are all stored as a single
179596 ** allocation so that the expression can be freed with a single call to
179597 ** sqlite3_free(). Setting this up requires a two pass approach.
179598 **
179599 ** The first pass, in the block below, uses a tokenizer cursor to iterate
179600 ** through the tokens in the expression. This pass uses fts3ReallocOrFree()
179601 ** to assemble data in two dynamic buffers:
179602 **
179603 ** Buffer p: Points to the Fts3Expr structure, followed by the Fts3Phrase
179604 ** structure, followed by the array of Fts3PhraseToken
179605 ** structures. This pass only populates the Fts3PhraseToken array.
179606 **
179607 ** Buffer zTemp: Contains copies of all tokens.
179608 **
179609 ** The second pass, in the block that begins "if( rc==SQLITE_DONE )" below,
179610 ** appends buffer zTemp to buffer p, and fills in the Fts3Expr and Fts3Phrase
179611 ** structures.
179612 */
179613 rc = sqlite3Fts3OpenTokenizer(
179614 pTokenizer, iLangid: pParse->iLangid, z: zInput, n: nInput, ppCsr: &pCursor);
179615 if( rc==SQLITE_OK ){
179616 int ii;
179617 for(ii=0; rc==SQLITE_OK; ii++){
179618 const char *zByte;
179619 int nByte = 0, iBegin = 0, iEnd = 0, iPos = 0;
179620 rc = pModule->xNext(pCursor, &zByte, &nByte, &iBegin, &iEnd, &iPos);
179621 if( rc==SQLITE_OK ){
179622 Fts3PhraseToken *pToken;
179623
179624 p = fts3ReallocOrFree(pOrig: p, nNew: nSpace + ii*sizeof(Fts3PhraseToken));
179625 if( !p ) goto no_mem;
179626
179627 zTemp = fts3ReallocOrFree(pOrig: zTemp, nNew: nTemp + nByte);
179628 if( !zTemp ) goto no_mem;
179629
179630 assert( nToken==ii );
179631 pToken = &((Fts3Phrase *)(&p[1]))->aToken[ii];
179632 memset(s: pToken, c: 0, n: sizeof(Fts3PhraseToken));
179633
179634 memcpy(dest: &zTemp[nTemp], src: zByte, n: nByte);
179635 nTemp += nByte;
179636
179637 pToken->n = nByte;
179638 pToken->isPrefix = (iEnd<nInput && zInput[iEnd]=='*');
179639 pToken->bFirst = (iBegin>0 && zInput[iBegin-1]=='^');
179640 nToken = ii+1;
179641 }
179642 }
179643
179644 pModule->xClose(pCursor);
179645 pCursor = 0;
179646 }
179647
179648 if( rc==SQLITE_DONE ){
179649 int jj;
179650 char *zBuf = 0;
179651
179652 p = fts3ReallocOrFree(pOrig: p, nNew: nSpace + nToken*sizeof(Fts3PhraseToken) + nTemp);
179653 if( !p ) goto no_mem;
179654 memset(s: p, c: 0, n: (char *)&(((Fts3Phrase *)&p[1])->aToken[0])-(char *)p);
179655 p->eType = FTSQUERY_PHRASE;
179656 p->pPhrase = (Fts3Phrase *)&p[1];
179657 p->pPhrase->iColumn = pParse->iDefaultCol;
179658 p->pPhrase->nToken = nToken;
179659
179660 zBuf = (char *)&p->pPhrase->aToken[nToken];
179661 if( zTemp ){
179662 memcpy(dest: zBuf, src: zTemp, n: nTemp);
179663 sqlite3_free(p: zTemp);
179664 }else{
179665 assert( nTemp==0 );
179666 }
179667
179668 for(jj=0; jj<p->pPhrase->nToken; jj++){
179669 p->pPhrase->aToken[jj].z = zBuf;
179670 zBuf += p->pPhrase->aToken[jj].n;
179671 }
179672 rc = SQLITE_OK;
179673 }
179674
179675 *ppExpr = p;
179676 return rc;
179677no_mem:
179678
179679 if( pCursor ){
179680 pModule->xClose(pCursor);
179681 }
179682 sqlite3_free(p: zTemp);
179683 sqlite3_free(p);
179684 *ppExpr = 0;
179685 return SQLITE_NOMEM;
179686}
179687
179688/*
179689** The output variable *ppExpr is populated with an allocated Fts3Expr
179690** structure, or set to 0 if the end of the input buffer is reached.
179691**
179692** Returns an SQLite error code. SQLITE_OK if everything works, SQLITE_NOMEM
179693** if a malloc failure occurs, or SQLITE_ERROR if a parse error is encountered.
179694** If SQLITE_ERROR is returned, pContext is populated with an error message.
179695*/
179696static int getNextNode(
179697 ParseContext *pParse, /* fts3 query parse context */
179698 const char *z, int n, /* Input string */
179699 Fts3Expr **ppExpr, /* OUT: expression */
179700 int *pnConsumed /* OUT: Number of bytes consumed */
179701){
179702 static const struct Fts3Keyword {
179703 char *z; /* Keyword text */
179704 unsigned char n; /* Length of the keyword */
179705 unsigned char parenOnly; /* Only valid in paren mode */
179706 unsigned char eType; /* Keyword code */
179707 } aKeyword[] = {
179708 { "OR" , 2, 0, FTSQUERY_OR },
179709 { "AND", 3, 1, FTSQUERY_AND },
179710 { "NOT", 3, 1, FTSQUERY_NOT },
179711 { "NEAR", 4, 0, FTSQUERY_NEAR }
179712 };
179713 int ii;
179714 int iCol;
179715 int iColLen;
179716 int rc;
179717 Fts3Expr *pRet = 0;
179718
179719 const char *zInput = z;
179720 int nInput = n;
179721
179722 pParse->isNot = 0;
179723
179724 /* Skip over any whitespace before checking for a keyword, an open or
179725 ** close bracket, or a quoted string.
179726 */
179727 while( nInput>0 && fts3isspace(c: *zInput) ){
179728 nInput--;
179729 zInput++;
179730 }
179731 if( nInput==0 ){
179732 return SQLITE_DONE;
179733 }
179734
179735 /* See if we are dealing with a keyword. */
179736 for(ii=0; ii<(int)(sizeof(aKeyword)/sizeof(struct Fts3Keyword)); ii++){
179737 const struct Fts3Keyword *pKey = &aKeyword[ii];
179738
179739 if( (pKey->parenOnly & ~sqlite3_fts3_enable_parentheses)!=0 ){
179740 continue;
179741 }
179742
179743 if( nInput>=pKey->n && 0==memcmp(s1: zInput, s2: pKey->z, n: pKey->n) ){
179744 int nNear = SQLITE_FTS3_DEFAULT_NEAR_PARAM;
179745 int nKey = pKey->n;
179746 char cNext;
179747
179748 /* If this is a "NEAR" keyword, check for an explicit nearness. */
179749 if( pKey->eType==FTSQUERY_NEAR ){
179750 assert( nKey==4 );
179751 if( zInput[4]=='/' && zInput[5]>='0' && zInput[5]<='9' ){
179752 nKey += 1+sqlite3Fts3ReadInt(z: &zInput[nKey+1], pnOut: &nNear);
179753 }
179754 }
179755
179756 /* At this point this is probably a keyword. But for that to be true,
179757 ** the next byte must contain either whitespace, an open or close
179758 ** parenthesis, a quote character, or EOF.
179759 */
179760 cNext = zInput[nKey];
179761 if( fts3isspace(c: cNext)
179762 || cNext=='"' || cNext=='(' || cNext==')' || cNext==0
179763 ){
179764 pRet = (Fts3Expr *)sqlite3Fts3MallocZero(nByte: sizeof(Fts3Expr));
179765 if( !pRet ){
179766 return SQLITE_NOMEM;
179767 }
179768 pRet->eType = pKey->eType;
179769 pRet->nNear = nNear;
179770 *ppExpr = pRet;
179771 *pnConsumed = (int)((zInput - z) + nKey);
179772 return SQLITE_OK;
179773 }
179774
179775 /* Turns out that wasn't a keyword after all. This happens if the
179776 ** user has supplied a token such as "ORacle". Continue.
179777 */
179778 }
179779 }
179780
179781 /* See if we are dealing with a quoted phrase. If this is the case, then
179782 ** search for the closing quote and pass the whole string to getNextString()
179783 ** for processing. This is easy to do, as fts3 has no syntax for escaping
179784 ** a quote character embedded in a string.
179785 */
179786 if( *zInput=='"' ){
179787 for(ii=1; ii<nInput && zInput[ii]!='"'; ii++);
179788 *pnConsumed = (int)((zInput - z) + ii + 1);
179789 if( ii==nInput ){
179790 return SQLITE_ERROR;
179791 }
179792 return getNextString(pParse, zInput: &zInput[1], nInput: ii-1, ppExpr);
179793 }
179794
179795 if( sqlite3_fts3_enable_parentheses ){
179796 if( *zInput=='(' ){
179797 int nConsumed = 0;
179798 pParse->nNest++;
179799#if !defined(SQLITE_MAX_EXPR_DEPTH)
179800 if( pParse->nNest>1000 ) return SQLITE_ERROR;
179801#elif SQLITE_MAX_EXPR_DEPTH>0
179802 if( pParse->nNest>SQLITE_MAX_EXPR_DEPTH ) return SQLITE_ERROR;
179803#endif
179804 rc = fts3ExprParse(pParse, zInput+1, nInput-1, ppExpr, &nConsumed);
179805 *pnConsumed = (int)(zInput - z) + 1 + nConsumed;
179806 return rc;
179807 }else if( *zInput==')' ){
179808 pParse->nNest--;
179809 *pnConsumed = (int)((zInput - z) + 1);
179810 *ppExpr = 0;
179811 return SQLITE_DONE;
179812 }
179813 }
179814
179815 /* If control flows to this point, this must be a regular token, or
179816 ** the end of the input. Read a regular token using the sqlite3_tokenizer
179817 ** interface. Before doing so, figure out if there is an explicit
179818 ** column specifier for the token.
179819 **
179820 ** TODO: Strangely, it is not possible to associate a column specifier
179821 ** with a quoted phrase, only with a single token. Not sure if this was
179822 ** an implementation artifact or an intentional decision when fts3 was
179823 ** first implemented. Whichever it was, this module duplicates the
179824 ** limitation.
179825 */
179826 iCol = pParse->iDefaultCol;
179827 iColLen = 0;
179828 for(ii=0; ii<pParse->nCol; ii++){
179829 const char *zStr = pParse->azCol[ii];
179830 int nStr = (int)strlen(s: zStr);
179831 if( nInput>nStr && zInput[nStr]==':'
179832 && sqlite3_strnicmp(zLeft: zStr, zRight: zInput, N: nStr)==0
179833 ){
179834 iCol = ii;
179835 iColLen = (int)((zInput - z) + nStr + 1);
179836 break;
179837 }
179838 }
179839 rc = getNextToken(pParse, iCol, z: &z[iColLen], n: n-iColLen, ppExpr, pnConsumed);
179840 *pnConsumed += iColLen;
179841 return rc;
179842}
179843
179844/*
179845** The argument is an Fts3Expr structure for a binary operator (any type
179846** except an FTSQUERY_PHRASE). Return an integer value representing the
179847** precedence of the operator. Lower values have a higher precedence (i.e.
179848** group more tightly). For example, in the C language, the == operator
179849** groups more tightly than ||, and would therefore have a higher precedence.
179850**
179851** When using the new fts3 query syntax (when SQLITE_ENABLE_FTS3_PARENTHESIS
179852** is defined), the order of the operators in precedence from highest to
179853** lowest is:
179854**
179855** NEAR
179856** NOT
179857** AND (including implicit ANDs)
179858** OR
179859**
179860** Note that when using the old query syntax, the OR operator has a higher
179861** precedence than the AND operator.
179862*/
179863static int opPrecedence(Fts3Expr *p){
179864 assert( p->eType!=FTSQUERY_PHRASE );
179865 if( sqlite3_fts3_enable_parentheses ){
179866 return p->eType;
179867 }else if( p->eType==FTSQUERY_NEAR ){
179868 return 1;
179869 }else if( p->eType==FTSQUERY_OR ){
179870 return 2;
179871 }
179872 assert( p->eType==FTSQUERY_AND );
179873 return 3;
179874}
179875
179876/*
179877** Argument ppHead contains a pointer to the current head of a query
179878** expression tree being parsed. pPrev is the expression node most recently
179879** inserted into the tree. This function adds pNew, which is always a binary
179880** operator node, into the expression tree based on the relative precedence
179881** of pNew and the existing nodes of the tree. This may result in the head
179882** of the tree changing, in which case *ppHead is set to the new root node.
179883*/
179884static void insertBinaryOperator(
179885 Fts3Expr **ppHead, /* Pointer to the root node of a tree */
179886 Fts3Expr *pPrev, /* Node most recently inserted into the tree */
179887 Fts3Expr *pNew /* New binary node to insert into expression tree */
179888){
179889 Fts3Expr *pSplit = pPrev;
179890 while( pSplit->pParent && opPrecedence(p: pSplit->pParent)<=opPrecedence(p: pNew) ){
179891 pSplit = pSplit->pParent;
179892 }
179893
179894 if( pSplit->pParent ){
179895 assert( pSplit->pParent->pRight==pSplit );
179896 pSplit->pParent->pRight = pNew;
179897 pNew->pParent = pSplit->pParent;
179898 }else{
179899 *ppHead = pNew;
179900 }
179901 pNew->pLeft = pSplit;
179902 pSplit->pParent = pNew;
179903}
179904
179905/*
179906** Parse the fts3 query expression found in buffer z, length n. This function
179907** returns either when the end of the buffer is reached or an unmatched
179908** closing bracket - ')' - is encountered.
179909**
179910** If successful, SQLITE_OK is returned, *ppExpr is set to point to the
179911** parsed form of the expression and *pnConsumed is set to the number of
179912** bytes read from buffer z. Otherwise, *ppExpr is set to 0 and SQLITE_NOMEM
179913** (out of memory error) or SQLITE_ERROR (parse error) is returned.
179914*/
179915static int fts3ExprParse(
179916 ParseContext *pParse, /* fts3 query parse context */
179917 const char *z, int n, /* Text of MATCH query */
179918 Fts3Expr **ppExpr, /* OUT: Parsed query structure */
179919 int *pnConsumed /* OUT: Number of bytes consumed */
179920){
179921 Fts3Expr *pRet = 0;
179922 Fts3Expr *pPrev = 0;
179923 Fts3Expr *pNotBranch = 0; /* Only used in legacy parse mode */
179924 int nIn = n;
179925 const char *zIn = z;
179926 int rc = SQLITE_OK;
179927 int isRequirePhrase = 1;
179928
179929 while( rc==SQLITE_OK ){
179930 Fts3Expr *p = 0;
179931 int nByte = 0;
179932
179933 rc = getNextNode(pParse, z: zIn, n: nIn, ppExpr: &p, pnConsumed: &nByte);
179934 assert( nByte>0 || (rc!=SQLITE_OK && p==0) );
179935 if( rc==SQLITE_OK ){
179936 if( p ){
179937 int isPhrase;
179938
179939 if( !sqlite3_fts3_enable_parentheses
179940 && p->eType==FTSQUERY_PHRASE && pParse->isNot
179941 ){
179942 /* Create an implicit NOT operator. */
179943 Fts3Expr *pNot = sqlite3Fts3MallocZero(nByte: sizeof(Fts3Expr));
179944 if( !pNot ){
179945 sqlite3Fts3ExprFree(p);
179946 rc = SQLITE_NOMEM;
179947 goto exprparse_out;
179948 }
179949 pNot->eType = FTSQUERY_NOT;
179950 pNot->pRight = p;
179951 p->pParent = pNot;
179952 if( pNotBranch ){
179953 pNot->pLeft = pNotBranch;
179954 pNotBranch->pParent = pNot;
179955 }
179956 pNotBranch = pNot;
179957 p = pPrev;
179958 }else{
179959 int eType = p->eType;
179960 isPhrase = (eType==FTSQUERY_PHRASE || p->pLeft);
179961
179962 /* The isRequirePhrase variable is set to true if a phrase or
179963 ** an expression contained in parenthesis is required. If a
179964 ** binary operator (AND, OR, NOT or NEAR) is encounted when
179965 ** isRequirePhrase is set, this is a syntax error.
179966 */
179967 if( !isPhrase && isRequirePhrase ){
179968 sqlite3Fts3ExprFree(p);
179969 rc = SQLITE_ERROR;
179970 goto exprparse_out;
179971 }
179972
179973 if( isPhrase && !isRequirePhrase ){
179974 /* Insert an implicit AND operator. */
179975 Fts3Expr *pAnd;
179976 assert( pRet && pPrev );
179977 pAnd = sqlite3Fts3MallocZero(nByte: sizeof(Fts3Expr));
179978 if( !pAnd ){
179979 sqlite3Fts3ExprFree(p);
179980 rc = SQLITE_NOMEM;
179981 goto exprparse_out;
179982 }
179983 pAnd->eType = FTSQUERY_AND;
179984 insertBinaryOperator(ppHead: &pRet, pPrev, pNew: pAnd);
179985 pPrev = pAnd;
179986 }
179987
179988 /* This test catches attempts to make either operand of a NEAR
179989 ** operator something other than a phrase. For example, either of
179990 ** the following:
179991 **
179992 ** (bracketed expression) NEAR phrase
179993 ** phrase NEAR (bracketed expression)
179994 **
179995 ** Return an error in either case.
179996 */
179997 if( pPrev && (
179998 (eType==FTSQUERY_NEAR && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE)
179999 || (eType!=FTSQUERY_PHRASE && isPhrase && pPrev->eType==FTSQUERY_NEAR)
180000 )){
180001 sqlite3Fts3ExprFree(p);
180002 rc = SQLITE_ERROR;
180003 goto exprparse_out;
180004 }
180005
180006 if( isPhrase ){
180007 if( pRet ){
180008 assert( pPrev && pPrev->pLeft && pPrev->pRight==0 );
180009 pPrev->pRight = p;
180010 p->pParent = pPrev;
180011 }else{
180012 pRet = p;
180013 }
180014 }else{
180015 insertBinaryOperator(ppHead: &pRet, pPrev, pNew: p);
180016 }
180017 isRequirePhrase = !isPhrase;
180018 }
180019 pPrev = p;
180020 }
180021 assert( nByte>0 );
180022 }
180023 assert( rc!=SQLITE_OK || (nByte>0 && nByte<=nIn) );
180024 nIn -= nByte;
180025 zIn += nByte;
180026 }
180027
180028 if( rc==SQLITE_DONE && pRet && isRequirePhrase ){
180029 rc = SQLITE_ERROR;
180030 }
180031
180032 if( rc==SQLITE_DONE ){
180033 rc = SQLITE_OK;
180034 if( !sqlite3_fts3_enable_parentheses && pNotBranch ){
180035 if( !pRet ){
180036 rc = SQLITE_ERROR;
180037 }else{
180038 Fts3Expr *pIter = pNotBranch;
180039 while( pIter->pLeft ){
180040 pIter = pIter->pLeft;
180041 }
180042 pIter->pLeft = pRet;
180043 pRet->pParent = pIter;
180044 pRet = pNotBranch;
180045 }
180046 }
180047 }
180048 *pnConsumed = n - nIn;
180049
180050exprparse_out:
180051 if( rc!=SQLITE_OK ){
180052 sqlite3Fts3ExprFree(pRet);
180053 sqlite3Fts3ExprFree(pNotBranch);
180054 pRet = 0;
180055 }
180056 *ppExpr = pRet;
180057 return rc;
180058}
180059
180060/*
180061** Return SQLITE_ERROR if the maximum depth of the expression tree passed
180062** as the only argument is more than nMaxDepth.
180063*/
180064static int fts3ExprCheckDepth(Fts3Expr *p, int nMaxDepth){
180065 int rc = SQLITE_OK;
180066 if( p ){
180067 if( nMaxDepth<0 ){
180068 rc = SQLITE_TOOBIG;
180069 }else{
180070 rc = fts3ExprCheckDepth(p: p->pLeft, nMaxDepth: nMaxDepth-1);
180071 if( rc==SQLITE_OK ){
180072 rc = fts3ExprCheckDepth(p: p->pRight, nMaxDepth: nMaxDepth-1);
180073 }
180074 }
180075 }
180076 return rc;
180077}
180078
180079/*
180080** This function attempts to transform the expression tree at (*pp) to
180081** an equivalent but more balanced form. The tree is modified in place.
180082** If successful, SQLITE_OK is returned and (*pp) set to point to the
180083** new root expression node.
180084**
180085** nMaxDepth is the maximum allowable depth of the balanced sub-tree.
180086**
180087** Otherwise, if an error occurs, an SQLite error code is returned and
180088** expression (*pp) freed.
180089*/
180090static int fts3ExprBalance(Fts3Expr **pp, int nMaxDepth){
180091 int rc = SQLITE_OK; /* Return code */
180092 Fts3Expr *pRoot = *pp; /* Initial root node */
180093 Fts3Expr *pFree = 0; /* List of free nodes. Linked by pParent. */
180094 int eType = pRoot->eType; /* Type of node in this tree */
180095
180096 if( nMaxDepth==0 ){
180097 rc = SQLITE_ERROR;
180098 }
180099
180100 if( rc==SQLITE_OK ){
180101 if( (eType==FTSQUERY_AND || eType==FTSQUERY_OR) ){
180102 Fts3Expr **apLeaf;
180103 apLeaf = (Fts3Expr **)sqlite3_malloc64(n: sizeof(Fts3Expr *) * nMaxDepth);
180104 if( 0==apLeaf ){
180105 rc = SQLITE_NOMEM;
180106 }else{
180107 memset(s: apLeaf, c: 0, n: sizeof(Fts3Expr *) * nMaxDepth);
180108 }
180109
180110 if( rc==SQLITE_OK ){
180111 int i;
180112 Fts3Expr *p;
180113
180114 /* Set $p to point to the left-most leaf in the tree of eType nodes. */
180115 for(p=pRoot; p->eType==eType; p=p->pLeft){
180116 assert( p->pParent==0 || p->pParent->pLeft==p );
180117 assert( p->pLeft && p->pRight );
180118 }
180119
180120 /* This loop runs once for each leaf in the tree of eType nodes. */
180121 while( 1 ){
180122 int iLvl;
180123 Fts3Expr *pParent = p->pParent; /* Current parent of p */
180124
180125 assert( pParent==0 || pParent->pLeft==p );
180126 p->pParent = 0;
180127 if( pParent ){
180128 pParent->pLeft = 0;
180129 }else{
180130 pRoot = 0;
180131 }
180132 rc = fts3ExprBalance(pp: &p, nMaxDepth: nMaxDepth-1);
180133 if( rc!=SQLITE_OK ) break;
180134
180135 for(iLvl=0; p && iLvl<nMaxDepth; iLvl++){
180136 if( apLeaf[iLvl]==0 ){
180137 apLeaf[iLvl] = p;
180138 p = 0;
180139 }else{
180140 assert( pFree );
180141 pFree->pLeft = apLeaf[iLvl];
180142 pFree->pRight = p;
180143 pFree->pLeft->pParent = pFree;
180144 pFree->pRight->pParent = pFree;
180145
180146 p = pFree;
180147 pFree = pFree->pParent;
180148 p->pParent = 0;
180149 apLeaf[iLvl] = 0;
180150 }
180151 }
180152 if( p ){
180153 sqlite3Fts3ExprFree(p);
180154 rc = SQLITE_TOOBIG;
180155 break;
180156 }
180157
180158 /* If that was the last leaf node, break out of the loop */
180159 if( pParent==0 ) break;
180160
180161 /* Set $p to point to the next leaf in the tree of eType nodes */
180162 for(p=pParent->pRight; p->eType==eType; p=p->pLeft);
180163
180164 /* Remove pParent from the original tree. */
180165 assert( pParent->pParent==0 || pParent->pParent->pLeft==pParent );
180166 pParent->pRight->pParent = pParent->pParent;
180167 if( pParent->pParent ){
180168 pParent->pParent->pLeft = pParent->pRight;
180169 }else{
180170 assert( pParent==pRoot );
180171 pRoot = pParent->pRight;
180172 }
180173
180174 /* Link pParent into the free node list. It will be used as an
180175 ** internal node of the new tree. */
180176 pParent->pParent = pFree;
180177 pFree = pParent;
180178 }
180179
180180 if( rc==SQLITE_OK ){
180181 p = 0;
180182 for(i=0; i<nMaxDepth; i++){
180183 if( apLeaf[i] ){
180184 if( p==0 ){
180185 p = apLeaf[i];
180186 p->pParent = 0;
180187 }else{
180188 assert( pFree!=0 );
180189 pFree->pRight = p;
180190 pFree->pLeft = apLeaf[i];
180191 pFree->pLeft->pParent = pFree;
180192 pFree->pRight->pParent = pFree;
180193
180194 p = pFree;
180195 pFree = pFree->pParent;
180196 p->pParent = 0;
180197 }
180198 }
180199 }
180200 pRoot = p;
180201 }else{
180202 /* An error occurred. Delete the contents of the apLeaf[] array
180203 ** and pFree list. Everything else is cleaned up by the call to
180204 ** sqlite3Fts3ExprFree(pRoot) below. */
180205 Fts3Expr *pDel;
180206 for(i=0; i<nMaxDepth; i++){
180207 sqlite3Fts3ExprFree(apLeaf[i]);
180208 }
180209 while( (pDel=pFree)!=0 ){
180210 pFree = pDel->pParent;
180211 sqlite3_free(p: pDel);
180212 }
180213 }
180214
180215 assert( pFree==0 );
180216 sqlite3_free( p: apLeaf );
180217 }
180218 }else if( eType==FTSQUERY_NOT ){
180219 Fts3Expr *pLeft = pRoot->pLeft;
180220 Fts3Expr *pRight = pRoot->pRight;
180221
180222 pRoot->pLeft = 0;
180223 pRoot->pRight = 0;
180224 pLeft->pParent = 0;
180225 pRight->pParent = 0;
180226
180227 rc = fts3ExprBalance(pp: &pLeft, nMaxDepth: nMaxDepth-1);
180228 if( rc==SQLITE_OK ){
180229 rc = fts3ExprBalance(pp: &pRight, nMaxDepth: nMaxDepth-1);
180230 }
180231
180232 if( rc!=SQLITE_OK ){
180233 sqlite3Fts3ExprFree(pRight);
180234 sqlite3Fts3ExprFree(pLeft);
180235 }else{
180236 assert( pLeft && pRight );
180237 pRoot->pLeft = pLeft;
180238 pLeft->pParent = pRoot;
180239 pRoot->pRight = pRight;
180240 pRight->pParent = pRoot;
180241 }
180242 }
180243 }
180244
180245 if( rc!=SQLITE_OK ){
180246 sqlite3Fts3ExprFree(pRoot);
180247 pRoot = 0;
180248 }
180249 *pp = pRoot;
180250 return rc;
180251}
180252
180253/*
180254** This function is similar to sqlite3Fts3ExprParse(), with the following
180255** differences:
180256**
180257** 1. It does not do expression rebalancing.
180258** 2. It does not check that the expression does not exceed the
180259** maximum allowable depth.
180260** 3. Even if it fails, *ppExpr may still be set to point to an
180261** expression tree. It should be deleted using sqlite3Fts3ExprFree()
180262** in this case.
180263*/
180264static int fts3ExprParseUnbalanced(
180265 sqlite3_tokenizer *pTokenizer, /* Tokenizer module */
180266 int iLangid, /* Language id for tokenizer */
180267 char **azCol, /* Array of column names for fts3 table */
180268 int bFts4, /* True to allow FTS4-only syntax */
180269 int nCol, /* Number of entries in azCol[] */
180270 int iDefaultCol, /* Default column to query */
180271 const char *z, int n, /* Text of MATCH query */
180272 Fts3Expr **ppExpr /* OUT: Parsed query structure */
180273){
180274 int nParsed;
180275 int rc;
180276 ParseContext sParse;
180277
180278 memset(s: &sParse, c: 0, n: sizeof(ParseContext));
180279 sParse.pTokenizer = pTokenizer;
180280 sParse.iLangid = iLangid;
180281 sParse.azCol = (const char **)azCol;
180282 sParse.nCol = nCol;
180283 sParse.iDefaultCol = iDefaultCol;
180284 sParse.bFts4 = bFts4;
180285 if( z==0 ){
180286 *ppExpr = 0;
180287 return SQLITE_OK;
180288 }
180289 if( n<0 ){
180290 n = (int)strlen(s: z);
180291 }
180292 rc = fts3ExprParse(pParse: &sParse, z, n, ppExpr, pnConsumed: &nParsed);
180293 assert( rc==SQLITE_OK || *ppExpr==0 );
180294
180295 /* Check for mismatched parenthesis */
180296 if( rc==SQLITE_OK && sParse.nNest ){
180297 rc = SQLITE_ERROR;
180298 }
180299
180300 return rc;
180301}
180302
180303/*
180304** Parameters z and n contain a pointer to and length of a buffer containing
180305** an fts3 query expression, respectively. This function attempts to parse the
180306** query expression and create a tree of Fts3Expr structures representing the
180307** parsed expression. If successful, *ppExpr is set to point to the head
180308** of the parsed expression tree and SQLITE_OK is returned. If an error
180309** occurs, either SQLITE_NOMEM (out-of-memory error) or SQLITE_ERROR (parse
180310** error) is returned and *ppExpr is set to 0.
180311**
180312** If parameter n is a negative number, then z is assumed to point to a
180313** nul-terminated string and the length is determined using strlen().
180314**
180315** The first parameter, pTokenizer, is passed the fts3 tokenizer module to
180316** use to normalize query tokens while parsing the expression. The azCol[]
180317** array, which is assumed to contain nCol entries, should contain the names
180318** of each column in the target fts3 table, in order from left to right.
180319** Column names must be nul-terminated strings.
180320**
180321** The iDefaultCol parameter should be passed the index of the table column
180322** that appears on the left-hand-side of the MATCH operator (the default
180323** column to match against for tokens for which a column name is not explicitly
180324** specified as part of the query string), or -1 if tokens may by default
180325** match any table column.
180326*/
180327SQLITE_PRIVATE int sqlite3Fts3ExprParse(
180328 sqlite3_tokenizer *pTokenizer, /* Tokenizer module */
180329 int iLangid, /* Language id for tokenizer */
180330 char **azCol, /* Array of column names for fts3 table */
180331 int bFts4, /* True to allow FTS4-only syntax */
180332 int nCol, /* Number of entries in azCol[] */
180333 int iDefaultCol, /* Default column to query */
180334 const char *z, int n, /* Text of MATCH query */
180335 Fts3Expr **ppExpr, /* OUT: Parsed query structure */
180336 char **pzErr /* OUT: Error message (sqlite3_malloc) */
180337){
180338 int rc = fts3ExprParseUnbalanced(
180339 pTokenizer, iLangid, azCol, bFts4, nCol, iDefaultCol, z, n, ppExpr
180340 );
180341
180342 /* Rebalance the expression. And check that its depth does not exceed
180343 ** SQLITE_FTS3_MAX_EXPR_DEPTH. */
180344 if( rc==SQLITE_OK && *ppExpr ){
180345 rc = fts3ExprBalance(pp: ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH);
180346 if( rc==SQLITE_OK ){
180347 rc = fts3ExprCheckDepth(p: *ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH);
180348 }
180349 }
180350
180351 if( rc!=SQLITE_OK ){
180352 sqlite3Fts3ExprFree(*ppExpr);
180353 *ppExpr = 0;
180354 if( rc==SQLITE_TOOBIG ){
180355 sqlite3Fts3ErrMsg(pzErr,
180356 zFormat: "FTS expression tree is too large (maximum depth %d)",
180357 SQLITE_FTS3_MAX_EXPR_DEPTH
180358 );
180359 rc = SQLITE_ERROR;
180360 }else if( rc==SQLITE_ERROR ){
180361 sqlite3Fts3ErrMsg(pzErr, zFormat: "malformed MATCH expression: [%s]", z);
180362 }
180363 }
180364
180365 return rc;
180366}
180367
180368/*
180369** Free a single node of an expression tree.
180370*/
180371static void fts3FreeExprNode(Fts3Expr *p){
180372 assert( p->eType==FTSQUERY_PHRASE || p->pPhrase==0 );
180373 sqlite3Fts3EvalPhraseCleanup(pPhrase: p->pPhrase);
180374 sqlite3_free(p: p->aMI);
180375 sqlite3_free(p);
180376}
180377
180378/*
180379** Free a parsed fts3 query expression allocated by sqlite3Fts3ExprParse().
180380**
180381** This function would be simpler if it recursively called itself. But
180382** that would mean passing a sufficiently large expression to ExprParse()
180383** could cause a stack overflow.
180384*/
180385SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *pDel){
180386 Fts3Expr *p;
180387 assert( pDel==0 || pDel->pParent==0 );
180388 for(p=pDel; p && (p->pLeft||p->pRight); p=(p->pLeft ? p->pLeft : p->pRight)){
180389 assert( p->pParent==0 || p==p->pParent->pRight || p==p->pParent->pLeft );
180390 }
180391 while( p ){
180392 Fts3Expr *pParent = p->pParent;
180393 fts3FreeExprNode(p);
180394 if( pParent && p==pParent->pLeft && pParent->pRight ){
180395 p = pParent->pRight;
180396 while( p && (p->pLeft || p->pRight) ){
180397 assert( p==p->pParent->pRight || p==p->pParent->pLeft );
180398 p = (p->pLeft ? p->pLeft : p->pRight);
180399 }
180400 }else{
180401 p = pParent;
180402 }
180403 }
180404}
180405
180406/****************************************************************************
180407*****************************************************************************
180408** Everything after this point is just test code.
180409*/
180410
180411#ifdef SQLITE_TEST
180412
180413/* #include <stdio.h> */
180414
180415/*
180416** Return a pointer to a buffer containing a text representation of the
180417** expression passed as the first argument. The buffer is obtained from
180418** sqlite3_malloc(). It is the responsibility of the caller to use
180419** sqlite3_free() to release the memory. If an OOM condition is encountered,
180420** NULL is returned.
180421**
180422** If the second argument is not NULL, then its contents are prepended to
180423** the returned expression text and then freed using sqlite3_free().
180424*/
180425static char *exprToString(Fts3Expr *pExpr, char *zBuf){
180426 if( pExpr==0 ){
180427 return sqlite3_mprintf("");
180428 }
180429 switch( pExpr->eType ){
180430 case FTSQUERY_PHRASE: {
180431 Fts3Phrase *pPhrase = pExpr->pPhrase;
180432 int i;
180433 zBuf = sqlite3_mprintf(
180434 "%zPHRASE %d 0", zBuf, pPhrase->iColumn);
180435 for(i=0; zBuf && i<pPhrase->nToken; i++){
180436 zBuf = sqlite3_mprintf("%z %.*s%s", zBuf,
180437 pPhrase->aToken[i].n, pPhrase->aToken[i].z,
180438 (pPhrase->aToken[i].isPrefix?"+":"")
180439 );
180440 }
180441 return zBuf;
180442 }
180443
180444 case FTSQUERY_NEAR:
180445 zBuf = sqlite3_mprintf("%zNEAR/%d ", zBuf, pExpr->nNear);
180446 break;
180447 case FTSQUERY_NOT:
180448 zBuf = sqlite3_mprintf("%zNOT ", zBuf);
180449 break;
180450 case FTSQUERY_AND:
180451 zBuf = sqlite3_mprintf("%zAND ", zBuf);
180452 break;
180453 case FTSQUERY_OR:
180454 zBuf = sqlite3_mprintf("%zOR ", zBuf);
180455 break;
180456 }
180457
180458 if( zBuf ) zBuf = sqlite3_mprintf("%z{", zBuf);
180459 if( zBuf ) zBuf = exprToString(pExpr->pLeft, zBuf);
180460 if( zBuf ) zBuf = sqlite3_mprintf("%z} {", zBuf);
180461
180462 if( zBuf ) zBuf = exprToString(pExpr->pRight, zBuf);
180463 if( zBuf ) zBuf = sqlite3_mprintf("%z}", zBuf);
180464
180465 return zBuf;
180466}
180467
180468/*
180469** This is the implementation of a scalar SQL function used to test the
180470** expression parser. It should be called as follows:
180471**
180472** fts3_exprtest(<tokenizer>, <expr>, <column 1>, ...);
180473**
180474** The first argument, <tokenizer>, is the name of the fts3 tokenizer used
180475** to parse the query expression (see README.tokenizers). The second argument
180476** is the query expression to parse. Each subsequent argument is the name
180477** of a column of the fts3 table that the query expression may refer to.
180478** For example:
180479**
180480** SELECT fts3_exprtest('simple', 'Bill col2:Bloggs', 'col1', 'col2');
180481*/
180482static void fts3ExprTestCommon(
180483 int bRebalance,
180484 sqlite3_context *context,
180485 int argc,
180486 sqlite3_value **argv
180487){
180488 sqlite3_tokenizer *pTokenizer = 0;
180489 int rc;
180490 char **azCol = 0;
180491 const char *zExpr;
180492 int nExpr;
180493 int nCol;
180494 int ii;
180495 Fts3Expr *pExpr;
180496 char *zBuf = 0;
180497 Fts3Hash *pHash = (Fts3Hash*)sqlite3_user_data(context);
180498 const char *zTokenizer = 0;
180499 char *zErr = 0;
180500
180501 if( argc<3 ){
180502 sqlite3_result_error(context,
180503 "Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1
180504 );
180505 return;
180506 }
180507
180508 zTokenizer = (const char*)sqlite3_value_text(argv[0]);
180509 rc = sqlite3Fts3InitTokenizer(pHash, zTokenizer, &pTokenizer, &zErr);
180510 if( rc!=SQLITE_OK ){
180511 if( rc==SQLITE_NOMEM ){
180512 sqlite3_result_error_nomem(context);
180513 }else{
180514 sqlite3_result_error(context, zErr, -1);
180515 }
180516 sqlite3_free(zErr);
180517 return;
180518 }
180519
180520 zExpr = (const char *)sqlite3_value_text(argv[1]);
180521 nExpr = sqlite3_value_bytes(argv[1]);
180522 nCol = argc-2;
180523 azCol = (char **)sqlite3_malloc64(nCol*sizeof(char *));
180524 if( !azCol ){
180525 sqlite3_result_error_nomem(context);
180526 goto exprtest_out;
180527 }
180528 for(ii=0; ii<nCol; ii++){
180529 azCol[ii] = (char *)sqlite3_value_text(argv[ii+2]);
180530 }
180531
180532 if( bRebalance ){
180533 char *zDummy = 0;
180534 rc = sqlite3Fts3ExprParse(
180535 pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr, &zDummy
180536 );
180537 assert( rc==SQLITE_OK || pExpr==0 );
180538 sqlite3_free(zDummy);
180539 }else{
180540 rc = fts3ExprParseUnbalanced(
180541 pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr
180542 );
180543 }
180544
180545 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM ){
180546 sqlite3Fts3ExprFree(pExpr);
180547 sqlite3_result_error(context, "Error parsing expression", -1);
180548 }else if( rc==SQLITE_NOMEM || !(zBuf = exprToString(pExpr, 0)) ){
180549 sqlite3_result_error_nomem(context);
180550 }else{
180551 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
180552 sqlite3_free(zBuf);
180553 }
180554
180555 sqlite3Fts3ExprFree(pExpr);
180556
180557exprtest_out:
180558 if( pTokenizer ){
180559 rc = pTokenizer->pModule->xDestroy(pTokenizer);
180560 }
180561 sqlite3_free(azCol);
180562}
180563
180564static void fts3ExprTest(
180565 sqlite3_context *context,
180566 int argc,
180567 sqlite3_value **argv
180568){
180569 fts3ExprTestCommon(0, context, argc, argv);
180570}
180571static void fts3ExprTestRebalance(
180572 sqlite3_context *context,
180573 int argc,
180574 sqlite3_value **argv
180575){
180576 fts3ExprTestCommon(1, context, argc, argv);
180577}
180578
180579/*
180580** Register the query expression parser test function fts3_exprtest()
180581** with database connection db.
180582*/
180583SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db, Fts3Hash *pHash){
180584 int rc = sqlite3_create_function(
180585 db, "fts3_exprtest", -1, SQLITE_UTF8, (void*)pHash, fts3ExprTest, 0, 0
180586 );
180587 if( rc==SQLITE_OK ){
180588 rc = sqlite3_create_function(db, "fts3_exprtest_rebalance",
180589 -1, SQLITE_UTF8, (void*)pHash, fts3ExprTestRebalance, 0, 0
180590 );
180591 }
180592 return rc;
180593}
180594
180595#endif
180596#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
180597
180598/************** End of fts3_expr.c *******************************************/
180599/************** Begin file fts3_hash.c ***************************************/
180600/*
180601** 2001 September 22
180602**
180603** The author disclaims copyright to this source code. In place of
180604** a legal notice, here is a blessing:
180605**
180606** May you do good and not evil.
180607** May you find forgiveness for yourself and forgive others.
180608** May you share freely, never taking more than you give.
180609**
180610*************************************************************************
180611** This is the implementation of generic hash-tables used in SQLite.
180612** We've modified it slightly to serve as a standalone hash table
180613** implementation for the full-text indexing module.
180614*/
180615
180616/*
180617** The code in this file is only compiled if:
180618**
180619** * The FTS3 module is being built as an extension
180620** (in which case SQLITE_CORE is not defined), or
180621**
180622** * The FTS3 module is being built into the core of
180623** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
180624*/
180625/* #include "fts3Int.h" */
180626#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
180627
180628/* #include <assert.h> */
180629/* #include <stdlib.h> */
180630/* #include <string.h> */
180631
180632/* #include "fts3_hash.h" */
180633
180634/*
180635** Malloc and Free functions
180636*/
180637static void *fts3HashMalloc(sqlite3_int64 n){
180638 void *p = sqlite3_malloc64(n);
180639 if( p ){
180640 memset(s: p, c: 0, n: n);
180641 }
180642 return p;
180643}
180644static void fts3HashFree(void *p){
180645 sqlite3_free(p);
180646}
180647
180648/* Turn bulk memory into a hash table object by initializing the
180649** fields of the Hash structure.
180650**
180651** "pNew" is a pointer to the hash table that is to be initialized.
180652** keyClass is one of the constants
180653** FTS3_HASH_BINARY or FTS3_HASH_STRING. The value of keyClass
180654** determines what kind of key the hash table will use. "copyKey" is
180655** true if the hash table should make its own private copy of keys and
180656** false if it should just use the supplied pointer.
180657*/
180658SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey){
180659 assert( pNew!=0 );
180660 assert( keyClass>=FTS3_HASH_STRING && keyClass<=FTS3_HASH_BINARY );
180661 pNew->keyClass = keyClass;
180662 pNew->copyKey = copyKey;
180663 pNew->first = 0;
180664 pNew->count = 0;
180665 pNew->htsize = 0;
180666 pNew->ht = 0;
180667}
180668
180669/* Remove all entries from a hash table. Reclaim all memory.
180670** Call this routine to delete a hash table or to reset a hash table
180671** to the empty state.
180672*/
180673SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash *pH){
180674 Fts3HashElem *elem; /* For looping over all elements of the table */
180675
180676 assert( pH!=0 );
180677 elem = pH->first;
180678 pH->first = 0;
180679 fts3HashFree(p: pH->ht);
180680 pH->ht = 0;
180681 pH->htsize = 0;
180682 while( elem ){
180683 Fts3HashElem *next_elem = elem->next;
180684 if( pH->copyKey && elem->pKey ){
180685 fts3HashFree(p: elem->pKey);
180686 }
180687 fts3HashFree(p: elem);
180688 elem = next_elem;
180689 }
180690 pH->count = 0;
180691}
180692
180693/*
180694** Hash and comparison functions when the mode is FTS3_HASH_STRING
180695*/
180696static int fts3StrHash(const void *pKey, int nKey){
180697 const char *z = (const char *)pKey;
180698 unsigned h = 0;
180699 if( nKey<=0 ) nKey = (int) strlen(s: z);
180700 while( nKey > 0 ){
180701 h = (h<<3) ^ h ^ *z++;
180702 nKey--;
180703 }
180704 return (int)(h & 0x7fffffff);
180705}
180706static int fts3StrCompare(const void *pKey1, int n1, const void *pKey2, int n2){
180707 if( n1!=n2 ) return 1;
180708 return strncmp(s1: (const char*)pKey1,s2: (const char*)pKey2,n: n1);
180709}
180710
180711/*
180712** Hash and comparison functions when the mode is FTS3_HASH_BINARY
180713*/
180714static int fts3BinHash(const void *pKey, int nKey){
180715 int h = 0;
180716 const char *z = (const char *)pKey;
180717 while( nKey-- > 0 ){
180718 h = (h<<3) ^ h ^ *(z++);
180719 }
180720 return h & 0x7fffffff;
180721}
180722static int fts3BinCompare(const void *pKey1, int n1, const void *pKey2, int n2){
180723 if( n1!=n2 ) return 1;
180724 return memcmp(s1: pKey1,s2: pKey2,n: n1);
180725}
180726
180727/*
180728** Return a pointer to the appropriate hash function given the key class.
180729**
180730** The C syntax in this function definition may be unfamilar to some
180731** programmers, so we provide the following additional explanation:
180732**
180733** The name of the function is "ftsHashFunction". The function takes a
180734** single parameter "keyClass". The return value of ftsHashFunction()
180735** is a pointer to another function. Specifically, the return value
180736** of ftsHashFunction() is a pointer to a function that takes two parameters
180737** with types "const void*" and "int" and returns an "int".
180738*/
180739static int (*ftsHashFunction(int keyClass))(const void*,int){
180740 if( keyClass==FTS3_HASH_STRING ){
180741 return &fts3StrHash;
180742 }else{
180743 assert( keyClass==FTS3_HASH_BINARY );
180744 return &fts3BinHash;
180745 }
180746}
180747
180748/*
180749** Return a pointer to the appropriate hash function given the key class.
180750**
180751** For help in interpreted the obscure C code in the function definition,
180752** see the header comment on the previous function.
180753*/
180754static int (*ftsCompareFunction(int keyClass))(const void*,int,const void*,int){
180755 if( keyClass==FTS3_HASH_STRING ){
180756 return &fts3StrCompare;
180757 }else{
180758 assert( keyClass==FTS3_HASH_BINARY );
180759 return &fts3BinCompare;
180760 }
180761}
180762
180763/* Link an element into the hash table
180764*/
180765static void fts3HashInsertElement(
180766 Fts3Hash *pH, /* The complete hash table */
180767 struct _fts3ht *pEntry, /* The entry into which pNew is inserted */
180768 Fts3HashElem *pNew /* The element to be inserted */
180769){
180770 Fts3HashElem *pHead; /* First element already in pEntry */
180771 pHead = pEntry->chain;
180772 if( pHead ){
180773 pNew->next = pHead;
180774 pNew->prev = pHead->prev;
180775 if( pHead->prev ){ pHead->prev->next = pNew; }
180776 else { pH->first = pNew; }
180777 pHead->prev = pNew;
180778 }else{
180779 pNew->next = pH->first;
180780 if( pH->first ){ pH->first->prev = pNew; }
180781 pNew->prev = 0;
180782 pH->first = pNew;
180783 }
180784 pEntry->count++;
180785 pEntry->chain = pNew;
180786}
180787
180788
180789/* Resize the hash table so that it cantains "new_size" buckets.
180790** "new_size" must be a power of 2. The hash table might fail
180791** to resize if sqliteMalloc() fails.
180792**
180793** Return non-zero if a memory allocation error occurs.
180794*/
180795static int fts3Rehash(Fts3Hash *pH, int new_size){
180796 struct _fts3ht *new_ht; /* The new hash table */
180797 Fts3HashElem *elem, *next_elem; /* For looping over existing elements */
180798 int (*xHash)(const void*,int); /* The hash function */
180799
180800 assert( (new_size & (new_size-1))==0 );
180801 new_ht = (struct _fts3ht *)fts3HashMalloc( n: new_size*sizeof(struct _fts3ht) );
180802 if( new_ht==0 ) return 1;
180803 fts3HashFree(p: pH->ht);
180804 pH->ht = new_ht;
180805 pH->htsize = new_size;
180806 xHash = ftsHashFunction(keyClass: pH->keyClass);
180807 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
180808 int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
180809 next_elem = elem->next;
180810 fts3HashInsertElement(pH, pEntry: &new_ht[h], pNew: elem);
180811 }
180812 return 0;
180813}
180814
180815/* This function (for internal use only) locates an element in an
180816** hash table that matches the given key. The hash for this key has
180817** already been computed and is passed as the 4th parameter.
180818*/
180819static Fts3HashElem *fts3FindElementByHash(
180820 const Fts3Hash *pH, /* The pH to be searched */
180821 const void *pKey, /* The key we are searching for */
180822 int nKey,
180823 int h /* The hash for this key. */
180824){
180825 Fts3HashElem *elem; /* Used to loop thru the element list */
180826 int count; /* Number of elements left to test */
180827 int (*xCompare)(const void*,int,const void*,int); /* comparison function */
180828
180829 if( pH->ht ){
180830 struct _fts3ht *pEntry = &pH->ht[h];
180831 elem = pEntry->chain;
180832 count = pEntry->count;
180833 xCompare = ftsCompareFunction(keyClass: pH->keyClass);
180834 while( count-- && elem ){
180835 if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){
180836 return elem;
180837 }
180838 elem = elem->next;
180839 }
180840 }
180841 return 0;
180842}
180843
180844/* Remove a single entry from the hash table given a pointer to that
180845** element and a hash on the element's key.
180846*/
180847static void fts3RemoveElementByHash(
180848 Fts3Hash *pH, /* The pH containing "elem" */
180849 Fts3HashElem* elem, /* The element to be removed from the pH */
180850 int h /* Hash value for the element */
180851){
180852 struct _fts3ht *pEntry;
180853 if( elem->prev ){
180854 elem->prev->next = elem->next;
180855 }else{
180856 pH->first = elem->next;
180857 }
180858 if( elem->next ){
180859 elem->next->prev = elem->prev;
180860 }
180861 pEntry = &pH->ht[h];
180862 if( pEntry->chain==elem ){
180863 pEntry->chain = elem->next;
180864 }
180865 pEntry->count--;
180866 if( pEntry->count<=0 ){
180867 pEntry->chain = 0;
180868 }
180869 if( pH->copyKey && elem->pKey ){
180870 fts3HashFree(p: elem->pKey);
180871 }
180872 fts3HashFree( p: elem );
180873 pH->count--;
180874 if( pH->count<=0 ){
180875 assert( pH->first==0 );
180876 assert( pH->count==0 );
180877 fts3HashClear(pH);
180878 }
180879}
180880
180881SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(
180882 const Fts3Hash *pH,
180883 const void *pKey,
180884 int nKey
180885){
180886 int h; /* A hash on key */
180887 int (*xHash)(const void*,int); /* The hash function */
180888
180889 if( pH==0 || pH->ht==0 ) return 0;
180890 xHash = ftsHashFunction(keyClass: pH->keyClass);
180891 assert( xHash!=0 );
180892 h = (*xHash)(pKey,nKey);
180893 assert( (pH->htsize & (pH->htsize-1))==0 );
180894 return fts3FindElementByHash(pH,pKey,nKey, h: h & (pH->htsize-1));
180895}
180896
180897/*
180898** Attempt to locate an element of the hash table pH with a key
180899** that matches pKey,nKey. Return the data for this element if it is
180900** found, or NULL if there is no match.
180901*/
180902SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash *pH, const void *pKey, int nKey){
180903 Fts3HashElem *pElem; /* The element that matches key (if any) */
180904
180905 pElem = sqlite3Fts3HashFindElem(pH, pKey, nKey);
180906 return pElem ? pElem->data : 0;
180907}
180908
180909/* Insert an element into the hash table pH. The key is pKey,nKey
180910** and the data is "data".
180911**
180912** If no element exists with a matching key, then a new
180913** element is created. A copy of the key is made if the copyKey
180914** flag is set. NULL is returned.
180915**
180916** If another element already exists with the same key, then the
180917** new data replaces the old data and the old data is returned.
180918** The key is not copied in this instance. If a malloc fails, then
180919** the new data is returned and the hash table is unchanged.
180920**
180921** If the "data" parameter to this function is NULL, then the
180922** element corresponding to "key" is removed from the hash table.
180923*/
180924SQLITE_PRIVATE void *sqlite3Fts3HashInsert(
180925 Fts3Hash *pH, /* The hash table to insert into */
180926 const void *pKey, /* The key */
180927 int nKey, /* Number of bytes in the key */
180928 void *data /* The data */
180929){
180930 int hraw; /* Raw hash value of the key */
180931 int h; /* the hash of the key modulo hash table size */
180932 Fts3HashElem *elem; /* Used to loop thru the element list */
180933 Fts3HashElem *new_elem; /* New element added to the pH */
180934 int (*xHash)(const void*,int); /* The hash function */
180935
180936 assert( pH!=0 );
180937 xHash = ftsHashFunction(keyClass: pH->keyClass);
180938 assert( xHash!=0 );
180939 hraw = (*xHash)(pKey, nKey);
180940 assert( (pH->htsize & (pH->htsize-1))==0 );
180941 h = hraw & (pH->htsize-1);
180942 elem = fts3FindElementByHash(pH,pKey,nKey,h);
180943 if( elem ){
180944 void *old_data = elem->data;
180945 if( data==0 ){
180946 fts3RemoveElementByHash(pH,elem,h);
180947 }else{
180948 elem->data = data;
180949 }
180950 return old_data;
180951 }
180952 if( data==0 ) return 0;
180953 if( (pH->htsize==0 && fts3Rehash(pH,new_size: 8))
180954 || (pH->count>=pH->htsize && fts3Rehash(pH, new_size: pH->htsize*2))
180955 ){
180956 pH->count = 0;
180957 return data;
180958 }
180959 assert( pH->htsize>0 );
180960 new_elem = (Fts3HashElem*)fts3HashMalloc( n: sizeof(Fts3HashElem) );
180961 if( new_elem==0 ) return data;
180962 if( pH->copyKey && pKey!=0 ){
180963 new_elem->pKey = fts3HashMalloc( n: nKey );
180964 if( new_elem->pKey==0 ){
180965 fts3HashFree(p: new_elem);
180966 return data;
180967 }
180968 memcpy(dest: (void*)new_elem->pKey, src: pKey, n: nKey);
180969 }else{
180970 new_elem->pKey = (void*)pKey;
180971 }
180972 new_elem->nKey = nKey;
180973 pH->count++;
180974 assert( pH->htsize>0 );
180975 assert( (pH->htsize & (pH->htsize-1))==0 );
180976 h = hraw & (pH->htsize-1);
180977 fts3HashInsertElement(pH, pEntry: &pH->ht[h], pNew: new_elem);
180978 new_elem->data = data;
180979 return 0;
180980}
180981
180982#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
180983
180984/************** End of fts3_hash.c *******************************************/
180985/************** Begin file fts3_porter.c *************************************/
180986/*
180987** 2006 September 30
180988**
180989** The author disclaims copyright to this source code. In place of
180990** a legal notice, here is a blessing:
180991**
180992** May you do good and not evil.
180993** May you find forgiveness for yourself and forgive others.
180994** May you share freely, never taking more than you give.
180995**
180996*************************************************************************
180997** Implementation of the full-text-search tokenizer that implements
180998** a Porter stemmer.
180999*/
181000
181001/*
181002** The code in this file is only compiled if:
181003**
181004** * The FTS3 module is being built as an extension
181005** (in which case SQLITE_CORE is not defined), or
181006**
181007** * The FTS3 module is being built into the core of
181008** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
181009*/
181010/* #include "fts3Int.h" */
181011#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
181012
181013/* #include <assert.h> */
181014/* #include <stdlib.h> */
181015/* #include <stdio.h> */
181016/* #include <string.h> */
181017
181018/* #include "fts3_tokenizer.h" */
181019
181020/*
181021** Class derived from sqlite3_tokenizer
181022*/
181023typedef struct porter_tokenizer {
181024 sqlite3_tokenizer base; /* Base class */
181025} porter_tokenizer;
181026
181027/*
181028** Class derived from sqlite3_tokenizer_cursor
181029*/
181030typedef struct porter_tokenizer_cursor {
181031 sqlite3_tokenizer_cursor base;
181032 const char *zInput; /* input we are tokenizing */
181033 int nInput; /* size of the input */
181034 int iOffset; /* current position in zInput */
181035 int iToken; /* index of next token to be returned */
181036 char *zToken; /* storage for current token */
181037 int nAllocated; /* space allocated to zToken buffer */
181038} porter_tokenizer_cursor;
181039
181040
181041/*
181042** Create a new tokenizer instance.
181043*/
181044static int porterCreate(
181045 int argc, const char * const *argv,
181046 sqlite3_tokenizer **ppTokenizer
181047){
181048 porter_tokenizer *t;
181049
181050 UNUSED_PARAMETER(argc);
181051 UNUSED_PARAMETER(argv);
181052
181053 t = (porter_tokenizer *) sqlite3_malloc(n: sizeof(*t));
181054 if( t==NULL ) return SQLITE_NOMEM;
181055 memset(s: t, c: 0, n: sizeof(*t));
181056 *ppTokenizer = &t->base;
181057 return SQLITE_OK;
181058}
181059
181060/*
181061** Destroy a tokenizer
181062*/
181063static int porterDestroy(sqlite3_tokenizer *pTokenizer){
181064 sqlite3_free(p: pTokenizer);
181065 return SQLITE_OK;
181066}
181067
181068/*
181069** Prepare to begin tokenizing a particular string. The input
181070** string to be tokenized is zInput[0..nInput-1]. A cursor
181071** used to incrementally tokenize this string is returned in
181072** *ppCursor.
181073*/
181074static int porterOpen(
181075 sqlite3_tokenizer *pTokenizer, /* The tokenizer */
181076 const char *zInput, int nInput, /* String to be tokenized */
181077 sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
181078){
181079 porter_tokenizer_cursor *c;
181080
181081 UNUSED_PARAMETER(pTokenizer);
181082
181083 c = (porter_tokenizer_cursor *) sqlite3_malloc(n: sizeof(*c));
181084 if( c==NULL ) return SQLITE_NOMEM;
181085
181086 c->zInput = zInput;
181087 if( zInput==0 ){
181088 c->nInput = 0;
181089 }else if( nInput<0 ){
181090 c->nInput = (int)strlen(s: zInput);
181091 }else{
181092 c->nInput = nInput;
181093 }
181094 c->iOffset = 0; /* start tokenizing at the beginning */
181095 c->iToken = 0;
181096 c->zToken = NULL; /* no space allocated, yet. */
181097 c->nAllocated = 0;
181098
181099 *ppCursor = &c->base;
181100 return SQLITE_OK;
181101}
181102
181103/*
181104** Close a tokenization cursor previously opened by a call to
181105** porterOpen() above.
181106*/
181107static int porterClose(sqlite3_tokenizer_cursor *pCursor){
181108 porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
181109 sqlite3_free(p: c->zToken);
181110 sqlite3_free(p: c);
181111 return SQLITE_OK;
181112}
181113/*
181114** Vowel or consonant
181115*/
181116static const char cType[] = {
181117 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0,
181118 1, 1, 1, 2, 1
181119};
181120
181121/*
181122** isConsonant() and isVowel() determine if their first character in
181123** the string they point to is a consonant or a vowel, according
181124** to Porter ruls.
181125**
181126** A consonate is any letter other than 'a', 'e', 'i', 'o', or 'u'.
181127** 'Y' is a consonant unless it follows another consonant,
181128** in which case it is a vowel.
181129**
181130** In these routine, the letters are in reverse order. So the 'y' rule
181131** is that 'y' is a consonant unless it is followed by another
181132** consonent.
181133*/
181134static int isVowel(const char*);
181135static int isConsonant(const char *z){
181136 int j;
181137 char x = *z;
181138 if( x==0 ) return 0;
181139 assert( x>='a' && x<='z' );
181140 j = cType[x-'a'];
181141 if( j<2 ) return j;
181142 return z[1]==0 || isVowel(z + 1);
181143}
181144static int isVowel(const char *z){
181145 int j;
181146 char x = *z;
181147 if( x==0 ) return 0;
181148 assert( x>='a' && x<='z' );
181149 j = cType[x-'a'];
181150 if( j<2 ) return 1-j;
181151 return isConsonant(z: z + 1);
181152}
181153
181154/*
181155** Let any sequence of one or more vowels be represented by V and let
181156** C be sequence of one or more consonants. Then every word can be
181157** represented as:
181158**
181159** [C] (VC){m} [V]
181160**
181161** In prose: A word is an optional consonant followed by zero or
181162** vowel-consonant pairs followed by an optional vowel. "m" is the
181163** number of vowel consonant pairs. This routine computes the value
181164** of m for the first i bytes of a word.
181165**
181166** Return true if the m-value for z is 1 or more. In other words,
181167** return true if z contains at least one vowel that is followed
181168** by a consonant.
181169**
181170** In this routine z[] is in reverse order. So we are really looking
181171** for an instance of a consonant followed by a vowel.
181172*/
181173static int m_gt_0(const char *z){
181174 while( isVowel(z) ){ z++; }
181175 if( *z==0 ) return 0;
181176 while( isConsonant(z) ){ z++; }
181177 return *z!=0;
181178}
181179
181180/* Like mgt0 above except we are looking for a value of m which is
181181** exactly 1
181182*/
181183static int m_eq_1(const char *z){
181184 while( isVowel(z) ){ z++; }
181185 if( *z==0 ) return 0;
181186 while( isConsonant(z) ){ z++; }
181187 if( *z==0 ) return 0;
181188 while( isVowel(z) ){ z++; }
181189 if( *z==0 ) return 1;
181190 while( isConsonant(z) ){ z++; }
181191 return *z==0;
181192}
181193
181194/* Like mgt0 above except we are looking for a value of m>1 instead
181195** or m>0
181196*/
181197static int m_gt_1(const char *z){
181198 while( isVowel(z) ){ z++; }
181199 if( *z==0 ) return 0;
181200 while( isConsonant(z) ){ z++; }
181201 if( *z==0 ) return 0;
181202 while( isVowel(z) ){ z++; }
181203 if( *z==0 ) return 0;
181204 while( isConsonant(z) ){ z++; }
181205 return *z!=0;
181206}
181207
181208/*
181209** Return TRUE if there is a vowel anywhere within z[0..n-1]
181210*/
181211static int hasVowel(const char *z){
181212 while( isConsonant(z) ){ z++; }
181213 return *z!=0;
181214}
181215
181216/*
181217** Return TRUE if the word ends in a double consonant.
181218**
181219** The text is reversed here. So we are really looking at
181220** the first two characters of z[].
181221*/
181222static int doubleConsonant(const char *z){
181223 return isConsonant(z) && z[0]==z[1];
181224}
181225
181226/*
181227** Return TRUE if the word ends with three letters which
181228** are consonant-vowel-consonent and where the final consonant
181229** is not 'w', 'x', or 'y'.
181230**
181231** The word is reversed here. So we are really checking the
181232** first three letters and the first one cannot be in [wxy].
181233*/
181234static int star_oh(const char *z){
181235 return
181236 isConsonant(z) &&
181237 z[0]!='w' && z[0]!='x' && z[0]!='y' &&
181238 isVowel(z: z+1) &&
181239 isConsonant(z: z+2);
181240}
181241
181242/*
181243** If the word ends with zFrom and xCond() is true for the stem
181244** of the word that preceeds the zFrom ending, then change the
181245** ending to zTo.
181246**
181247** The input word *pz and zFrom are both in reverse order. zTo
181248** is in normal order.
181249**
181250** Return TRUE if zFrom matches. Return FALSE if zFrom does not
181251** match. Not that TRUE is returned even if xCond() fails and
181252** no substitution occurs.
181253*/
181254static int stem(
181255 char **pz, /* The word being stemmed (Reversed) */
181256 const char *zFrom, /* If the ending matches this... (Reversed) */
181257 const char *zTo, /* ... change the ending to this (not reversed) */
181258 int (*xCond)(const char*) /* Condition that must be true */
181259){
181260 char *z = *pz;
181261 while( *zFrom && *zFrom==*z ){ z++; zFrom++; }
181262 if( *zFrom!=0 ) return 0;
181263 if( xCond && !xCond(z) ) return 1;
181264 while( *zTo ){
181265 *(--z) = *(zTo++);
181266 }
181267 *pz = z;
181268 return 1;
181269}
181270
181271/*
181272** This is the fallback stemmer used when the porter stemmer is
181273** inappropriate. The input word is copied into the output with
181274** US-ASCII case folding. If the input word is too long (more
181275** than 20 bytes if it contains no digits or more than 6 bytes if
181276** it contains digits) then word is truncated to 20 or 6 bytes
181277** by taking 10 or 3 bytes from the beginning and end.
181278*/
181279static void copy_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
181280 int i, mx, j;
181281 int hasDigit = 0;
181282 for(i=0; i<nIn; i++){
181283 char c = zIn[i];
181284 if( c>='A' && c<='Z' ){
181285 zOut[i] = c - 'A' + 'a';
181286 }else{
181287 if( c>='0' && c<='9' ) hasDigit = 1;
181288 zOut[i] = c;
181289 }
181290 }
181291 mx = hasDigit ? 3 : 10;
181292 if( nIn>mx*2 ){
181293 for(j=mx, i=nIn-mx; i<nIn; i++, j++){
181294 zOut[j] = zOut[i];
181295 }
181296 i = j;
181297 }
181298 zOut[i] = 0;
181299 *pnOut = i;
181300}
181301
181302
181303/*
181304** Stem the input word zIn[0..nIn-1]. Store the output in zOut.
181305** zOut is at least big enough to hold nIn bytes. Write the actual
181306** size of the output word (exclusive of the '\0' terminator) into *pnOut.
181307**
181308** Any upper-case characters in the US-ASCII character set ([A-Z])
181309** are converted to lower case. Upper-case UTF characters are
181310** unchanged.
181311**
181312** Words that are longer than about 20 bytes are stemmed by retaining
181313** a few bytes from the beginning and the end of the word. If the
181314** word contains digits, 3 bytes are taken from the beginning and
181315** 3 bytes from the end. For long words without digits, 10 bytes
181316** are taken from each end. US-ASCII case folding still applies.
181317**
181318** If the input word contains not digits but does characters not
181319** in [a-zA-Z] then no stemming is attempted and this routine just
181320** copies the input into the input into the output with US-ASCII
181321** case folding.
181322**
181323** Stemming never increases the length of the word. So there is
181324** no chance of overflowing the zOut buffer.
181325*/
181326static void porter_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
181327 int i, j;
181328 char zReverse[28];
181329 char *z, *z2;
181330 if( nIn<3 || nIn>=(int)sizeof(zReverse)-7 ){
181331 /* The word is too big or too small for the porter stemmer.
181332 ** Fallback to the copy stemmer */
181333 copy_stemmer(zIn, nIn, zOut, pnOut);
181334 return;
181335 }
181336 for(i=0, j=sizeof(zReverse)-6; i<nIn; i++, j--){
181337 char c = zIn[i];
181338 if( c>='A' && c<='Z' ){
181339 zReverse[j] = c + 'a' - 'A';
181340 }else if( c>='a' && c<='z' ){
181341 zReverse[j] = c;
181342 }else{
181343 /* The use of a character not in [a-zA-Z] means that we fallback
181344 ** to the copy stemmer */
181345 copy_stemmer(zIn, nIn, zOut, pnOut);
181346 return;
181347 }
181348 }
181349 memset(s: &zReverse[sizeof(zReverse)-5], c: 0, n: 5);
181350 z = &zReverse[j+1];
181351
181352
181353 /* Step 1a */
181354 if( z[0]=='s' ){
181355 if(
181356 !stem(pz: &z, zFrom: "sess", zTo: "ss", xCond: 0) &&
181357 !stem(pz: &z, zFrom: "sei", zTo: "i", xCond: 0) &&
181358 !stem(pz: &z, zFrom: "ss", zTo: "ss", xCond: 0)
181359 ){
181360 z++;
181361 }
181362 }
181363
181364 /* Step 1b */
181365 z2 = z;
181366 if( stem(pz: &z, zFrom: "dee", zTo: "ee", xCond: m_gt_0) ){
181367 /* Do nothing. The work was all in the test */
181368 }else if(
181369 (stem(pz: &z, zFrom: "gni", zTo: "", xCond: hasVowel) || stem(pz: &z, zFrom: "de", zTo: "", xCond: hasVowel))
181370 && z!=z2
181371 ){
181372 if( stem(pz: &z, zFrom: "ta", zTo: "ate", xCond: 0) ||
181373 stem(pz: &z, zFrom: "lb", zTo: "ble", xCond: 0) ||
181374 stem(pz: &z, zFrom: "zi", zTo: "ize", xCond: 0) ){
181375 /* Do nothing. The work was all in the test */
181376 }else if( doubleConsonant(z) && (*z!='l' && *z!='s' && *z!='z') ){
181377 z++;
181378 }else if( m_eq_1(z) && star_oh(z) ){
181379 *(--z) = 'e';
181380 }
181381 }
181382
181383 /* Step 1c */
181384 if( z[0]=='y' && hasVowel(z: z+1) ){
181385 z[0] = 'i';
181386 }
181387
181388 /* Step 2 */
181389 switch( z[1] ){
181390 case 'a':
181391 if( !stem(pz: &z, zFrom: "lanoita", zTo: "ate", xCond: m_gt_0) ){
181392 stem(pz: &z, zFrom: "lanoit", zTo: "tion", xCond: m_gt_0);
181393 }
181394 break;
181395 case 'c':
181396 if( !stem(pz: &z, zFrom: "icne", zTo: "ence", xCond: m_gt_0) ){
181397 stem(pz: &z, zFrom: "icna", zTo: "ance", xCond: m_gt_0);
181398 }
181399 break;
181400 case 'e':
181401 stem(pz: &z, zFrom: "rezi", zTo: "ize", xCond: m_gt_0);
181402 break;
181403 case 'g':
181404 stem(pz: &z, zFrom: "igol", zTo: "log", xCond: m_gt_0);
181405 break;
181406 case 'l':
181407 if( !stem(pz: &z, zFrom: "ilb", zTo: "ble", xCond: m_gt_0)
181408 && !stem(pz: &z, zFrom: "illa", zTo: "al", xCond: m_gt_0)
181409 && !stem(pz: &z, zFrom: "iltne", zTo: "ent", xCond: m_gt_0)
181410 && !stem(pz: &z, zFrom: "ile", zTo: "e", xCond: m_gt_0)
181411 ){
181412 stem(pz: &z, zFrom: "ilsuo", zTo: "ous", xCond: m_gt_0);
181413 }
181414 break;
181415 case 'o':
181416 if( !stem(pz: &z, zFrom: "noitazi", zTo: "ize", xCond: m_gt_0)
181417 && !stem(pz: &z, zFrom: "noita", zTo: "ate", xCond: m_gt_0)
181418 ){
181419 stem(pz: &z, zFrom: "rota", zTo: "ate", xCond: m_gt_0);
181420 }
181421 break;
181422 case 's':
181423 if( !stem(pz: &z, zFrom: "msila", zTo: "al", xCond: m_gt_0)
181424 && !stem(pz: &z, zFrom: "ssenevi", zTo: "ive", xCond: m_gt_0)
181425 && !stem(pz: &z, zFrom: "ssenluf", zTo: "ful", xCond: m_gt_0)
181426 ){
181427 stem(pz: &z, zFrom: "ssensuo", zTo: "ous", xCond: m_gt_0);
181428 }
181429 break;
181430 case 't':
181431 if( !stem(pz: &z, zFrom: "itila", zTo: "al", xCond: m_gt_0)
181432 && !stem(pz: &z, zFrom: "itivi", zTo: "ive", xCond: m_gt_0)
181433 ){
181434 stem(pz: &z, zFrom: "itilib", zTo: "ble", xCond: m_gt_0);
181435 }
181436 break;
181437 }
181438
181439 /* Step 3 */
181440 switch( z[0] ){
181441 case 'e':
181442 if( !stem(pz: &z, zFrom: "etaci", zTo: "ic", xCond: m_gt_0)
181443 && !stem(pz: &z, zFrom: "evita", zTo: "", xCond: m_gt_0)
181444 ){
181445 stem(pz: &z, zFrom: "ezila", zTo: "al", xCond: m_gt_0);
181446 }
181447 break;
181448 case 'i':
181449 stem(pz: &z, zFrom: "itici", zTo: "ic", xCond: m_gt_0);
181450 break;
181451 case 'l':
181452 if( !stem(pz: &z, zFrom: "laci", zTo: "ic", xCond: m_gt_0) ){
181453 stem(pz: &z, zFrom: "luf", zTo: "", xCond: m_gt_0);
181454 }
181455 break;
181456 case 's':
181457 stem(pz: &z, zFrom: "ssen", zTo: "", xCond: m_gt_0);
181458 break;
181459 }
181460
181461 /* Step 4 */
181462 switch( z[1] ){
181463 case 'a':
181464 if( z[0]=='l' && m_gt_1(z: z+2) ){
181465 z += 2;
181466 }
181467 break;
181468 case 'c':
181469 if( z[0]=='e' && z[2]=='n' && (z[3]=='a' || z[3]=='e') && m_gt_1(z: z+4) ){
181470 z += 4;
181471 }
181472 break;
181473 case 'e':
181474 if( z[0]=='r' && m_gt_1(z: z+2) ){
181475 z += 2;
181476 }
181477 break;
181478 case 'i':
181479 if( z[0]=='c' && m_gt_1(z: z+2) ){
181480 z += 2;
181481 }
181482 break;
181483 case 'l':
181484 if( z[0]=='e' && z[2]=='b' && (z[3]=='a' || z[3]=='i') && m_gt_1(z: z+4) ){
181485 z += 4;
181486 }
181487 break;
181488 case 'n':
181489 if( z[0]=='t' ){
181490 if( z[2]=='a' ){
181491 if( m_gt_1(z: z+3) ){
181492 z += 3;
181493 }
181494 }else if( z[2]=='e' ){
181495 if( !stem(pz: &z, zFrom: "tneme", zTo: "", xCond: m_gt_1)
181496 && !stem(pz: &z, zFrom: "tnem", zTo: "", xCond: m_gt_1)
181497 ){
181498 stem(pz: &z, zFrom: "tne", zTo: "", xCond: m_gt_1);
181499 }
181500 }
181501 }
181502 break;
181503 case 'o':
181504 if( z[0]=='u' ){
181505 if( m_gt_1(z: z+2) ){
181506 z += 2;
181507 }
181508 }else if( z[3]=='s' || z[3]=='t' ){
181509 stem(pz: &z, zFrom: "noi", zTo: "", xCond: m_gt_1);
181510 }
181511 break;
181512 case 's':
181513 if( z[0]=='m' && z[2]=='i' && m_gt_1(z: z+3) ){
181514 z += 3;
181515 }
181516 break;
181517 case 't':
181518 if( !stem(pz: &z, zFrom: "eta", zTo: "", xCond: m_gt_1) ){
181519 stem(pz: &z, zFrom: "iti", zTo: "", xCond: m_gt_1);
181520 }
181521 break;
181522 case 'u':
181523 if( z[0]=='s' && z[2]=='o' && m_gt_1(z: z+3) ){
181524 z += 3;
181525 }
181526 break;
181527 case 'v':
181528 case 'z':
181529 if( z[0]=='e' && z[2]=='i' && m_gt_1(z: z+3) ){
181530 z += 3;
181531 }
181532 break;
181533 }
181534
181535 /* Step 5a */
181536 if( z[0]=='e' ){
181537 if( m_gt_1(z: z+1) ){
181538 z++;
181539 }else if( m_eq_1(z: z+1) && !star_oh(z: z+1) ){
181540 z++;
181541 }
181542 }
181543
181544 /* Step 5b */
181545 if( m_gt_1(z) && z[0]=='l' && z[1]=='l' ){
181546 z++;
181547 }
181548
181549 /* z[] is now the stemmed word in reverse order. Flip it back
181550 ** around into forward order and return.
181551 */
181552 *pnOut = i = (int)strlen(s: z);
181553 zOut[i] = 0;
181554 while( *z ){
181555 zOut[--i] = *(z++);
181556 }
181557}
181558
181559/*
181560** Characters that can be part of a token. We assume any character
181561** whose value is greater than 0x80 (any UTF character) can be
181562** part of a token. In other words, delimiters all must have
181563** values of 0x7f or lower.
181564*/
181565static const char porterIdChar[] = {
181566/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
181567 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
181568 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
181569 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
181570 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
181571 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
181572};
181573#define isDelim(C) (((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar[ch-0x30]))
181574
181575/*
181576** Extract the next token from a tokenization cursor. The cursor must
181577** have been opened by a prior call to porterOpen().
181578*/
181579static int porterNext(
181580 sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by porterOpen */
181581 const char **pzToken, /* OUT: *pzToken is the token text */
181582 int *pnBytes, /* OUT: Number of bytes in token */
181583 int *piStartOffset, /* OUT: Starting offset of token */
181584 int *piEndOffset, /* OUT: Ending offset of token */
181585 int *piPosition /* OUT: Position integer of token */
181586){
181587 porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
181588 const char *z = c->zInput;
181589
181590 while( c->iOffset<c->nInput ){
181591 int iStartOffset, ch;
181592
181593 /* Scan past delimiter characters */
181594 while( c->iOffset<c->nInput && isDelim(z[c->iOffset]) ){
181595 c->iOffset++;
181596 }
181597
181598 /* Count non-delimiter characters. */
181599 iStartOffset = c->iOffset;
181600 while( c->iOffset<c->nInput && !isDelim(z[c->iOffset]) ){
181601 c->iOffset++;
181602 }
181603
181604 if( c->iOffset>iStartOffset ){
181605 int n = c->iOffset-iStartOffset;
181606 if( n>c->nAllocated ){
181607 char *pNew;
181608 c->nAllocated = n+20;
181609 pNew = sqlite3_realloc(pOld: c->zToken, n: c->nAllocated);
181610 if( !pNew ) return SQLITE_NOMEM;
181611 c->zToken = pNew;
181612 }
181613 porter_stemmer(zIn: &z[iStartOffset], nIn: n, zOut: c->zToken, pnOut: pnBytes);
181614 *pzToken = c->zToken;
181615 *piStartOffset = iStartOffset;
181616 *piEndOffset = c->iOffset;
181617 *piPosition = c->iToken++;
181618 return SQLITE_OK;
181619 }
181620 }
181621 return SQLITE_DONE;
181622}
181623
181624/*
181625** The set of routines that implement the porter-stemmer tokenizer
181626*/
181627static const sqlite3_tokenizer_module porterTokenizerModule = {
181628 0,
181629 porterCreate,
181630 porterDestroy,
181631 porterOpen,
181632 porterClose,
181633 porterNext,
181634 0
181635};
181636
181637/*
181638** Allocate a new porter tokenizer. Return a pointer to the new
181639** tokenizer in *ppModule
181640*/
181641SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(
181642 sqlite3_tokenizer_module const**ppModule
181643){
181644 *ppModule = &porterTokenizerModule;
181645}
181646
181647#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
181648
181649/************** End of fts3_porter.c *****************************************/
181650/************** Begin file fts3_tokenizer.c **********************************/
181651/*
181652** 2007 June 22
181653**
181654** The author disclaims copyright to this source code. In place of
181655** a legal notice, here is a blessing:
181656**
181657** May you do good and not evil.
181658** May you find forgiveness for yourself and forgive others.
181659** May you share freely, never taking more than you give.
181660**
181661******************************************************************************
181662**
181663** This is part of an SQLite module implementing full-text search.
181664** This particular file implements the generic tokenizer interface.
181665*/
181666
181667/*
181668** The code in this file is only compiled if:
181669**
181670** * The FTS3 module is being built as an extension
181671** (in which case SQLITE_CORE is not defined), or
181672**
181673** * The FTS3 module is being built into the core of
181674** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
181675*/
181676/* #include "fts3Int.h" */
181677#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
181678
181679/* #include <assert.h> */
181680/* #include <string.h> */
181681
181682/*
181683** Return true if the two-argument version of fts3_tokenizer()
181684** has been activated via a prior call to sqlite3_db_config(db,
181685** SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0);
181686*/
181687static int fts3TokenizerEnabled(sqlite3_context *context){
181688 sqlite3 *db = sqlite3_context_db_handle(p: context);
181689 int isEnabled = 0;
181690 sqlite3_db_config(db,SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER,-1,&isEnabled);
181691 return isEnabled;
181692}
181693
181694/*
181695** Implementation of the SQL scalar function for accessing the underlying
181696** hash table. This function may be called as follows:
181697**
181698** SELECT <function-name>(<key-name>);
181699** SELECT <function-name>(<key-name>, <pointer>);
181700**
181701** where <function-name> is the name passed as the second argument
181702** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer').
181703**
181704** If the <pointer> argument is specified, it must be a blob value
181705** containing a pointer to be stored as the hash data corresponding
181706** to the string <key-name>. If <pointer> is not specified, then
181707** the string <key-name> must already exist in the has table. Otherwise,
181708** an error is returned.
181709**
181710** Whether or not the <pointer> argument is specified, the value returned
181711** is a blob containing the pointer stored as the hash data corresponding
181712** to string <key-name> (after the hash-table is updated, if applicable).
181713*/
181714static void fts3TokenizerFunc(
181715 sqlite3_context *context,
181716 int argc,
181717 sqlite3_value **argv
181718){
181719 Fts3Hash *pHash;
181720 void *pPtr = 0;
181721 const unsigned char *zName;
181722 int nName;
181723
181724 assert( argc==1 || argc==2 );
181725
181726 pHash = (Fts3Hash *)sqlite3_user_data(p: context);
181727
181728 zName = sqlite3_value_text(pVal: argv[0]);
181729 nName = sqlite3_value_bytes(pVal: argv[0])+1;
181730
181731 if( argc==2 ){
181732 if( fts3TokenizerEnabled(context) || sqlite3_value_frombind(pVal: argv[1]) ){
181733 void *pOld;
181734 int n = sqlite3_value_bytes(pVal: argv[1]);
181735 if( zName==0 || n!=sizeof(pPtr) ){
181736 sqlite3_result_error(pCtx: context, z: "argument type mismatch", n: -1);
181737 return;
181738 }
181739 pPtr = *(void **)sqlite3_value_blob(pVal: argv[1]);
181740 pOld = sqlite3Fts3HashInsert(pH: pHash, pKey: (void *)zName, nKey: nName, data: pPtr);
181741 if( pOld==pPtr ){
181742 sqlite3_result_error(pCtx: context, z: "out of memory", n: -1);
181743 }
181744 }else{
181745 sqlite3_result_error(pCtx: context, z: "fts3tokenize disabled", n: -1);
181746 return;
181747 }
181748 }else{
181749 if( zName ){
181750 pPtr = sqlite3Fts3HashFind(pH: pHash, pKey: zName, nKey: nName);
181751 }
181752 if( !pPtr ){
181753 char *zErr = sqlite3_mprintf(zFormat: "unknown tokenizer: %s", zName);
181754 sqlite3_result_error(pCtx: context, z: zErr, n: -1);
181755 sqlite3_free(p: zErr);
181756 return;
181757 }
181758 }
181759 if( fts3TokenizerEnabled(context) || sqlite3_value_frombind(pVal: argv[0]) ){
181760 sqlite3_result_blob(pCtx: context, z: (void *)&pPtr, n: sizeof(pPtr), SQLITE_TRANSIENT);
181761 }
181762}
181763
181764SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char c){
181765 static const char isFtsIdChar[] = {
181766 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
181767 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
181768 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
181769 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
181770 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
181771 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
181772 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
181773 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
181774 };
181775 return (c&0x80 || isFtsIdChar[(int)(c)]);
181776}
181777
181778SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *zStr, int *pn){
181779 const char *z1;
181780 const char *z2 = 0;
181781
181782 /* Find the start of the next token. */
181783 z1 = zStr;
181784 while( z2==0 ){
181785 char c = *z1;
181786 switch( c ){
181787 case '\0': return 0; /* No more tokens here */
181788 case '\'':
181789 case '"':
181790 case '`': {
181791 z2 = z1;
181792 while( *++z2 && (*z2!=c || *++z2==c) );
181793 break;
181794 }
181795 case '[':
181796 z2 = &z1[1];
181797 while( *z2 && z2[0]!=']' ) z2++;
181798 if( *z2 ) z2++;
181799 break;
181800
181801 default:
181802 if( sqlite3Fts3IsIdChar(c: *z1) ){
181803 z2 = &z1[1];
181804 while( sqlite3Fts3IsIdChar(c: *z2) ) z2++;
181805 }else{
181806 z1++;
181807 }
181808 }
181809 }
181810
181811 *pn = (int)(z2-z1);
181812 return z1;
181813}
181814
181815SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(
181816 Fts3Hash *pHash, /* Tokenizer hash table */
181817 const char *zArg, /* Tokenizer name */
181818 sqlite3_tokenizer **ppTok, /* OUT: Tokenizer (if applicable) */
181819 char **pzErr /* OUT: Set to malloced error message */
181820){
181821 int rc;
181822 char *z = (char *)zArg;
181823 int n = 0;
181824 char *zCopy;
181825 char *zEnd; /* Pointer to nul-term of zCopy */
181826 sqlite3_tokenizer_module *m;
181827
181828 zCopy = sqlite3_mprintf(zFormat: "%s", zArg);
181829 if( !zCopy ) return SQLITE_NOMEM;
181830 zEnd = &zCopy[strlen(s: zCopy)];
181831
181832 z = (char *)sqlite3Fts3NextToken(zStr: zCopy, pn: &n);
181833 if( z==0 ){
181834 assert( n==0 );
181835 z = zCopy;
181836 }
181837 z[n] = '\0';
181838 sqlite3Fts3Dequote(z);
181839
181840 m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pH: pHash,pKey: z,nKey: (int)strlen(s: z)+1);
181841 if( !m ){
181842 sqlite3Fts3ErrMsg(pzErr, zFormat: "unknown tokenizer: %s", z);
181843 rc = SQLITE_ERROR;
181844 }else{
181845 char const **aArg = 0;
181846 int iArg = 0;
181847 z = &z[n+1];
181848 while( z<zEnd && (NULL!=(z = (char *)sqlite3Fts3NextToken(zStr: z, pn: &n))) ){
181849 sqlite3_int64 nNew = sizeof(char *)*(iArg+1);
181850 char const **aNew = (const char **)sqlite3_realloc64(pOld: (void *)aArg, n: nNew);
181851 if( !aNew ){
181852 sqlite3_free(p: zCopy);
181853 sqlite3_free(p: (void *)aArg);
181854 return SQLITE_NOMEM;
181855 }
181856 aArg = aNew;
181857 aArg[iArg++] = z;
181858 z[n] = '\0';
181859 sqlite3Fts3Dequote(z);
181860 z = &z[n+1];
181861 }
181862 rc = m->xCreate(iArg, aArg, ppTok);
181863 assert( rc!=SQLITE_OK || *ppTok );
181864 if( rc!=SQLITE_OK ){
181865 sqlite3Fts3ErrMsg(pzErr, zFormat: "unknown tokenizer");
181866 }else{
181867 (*ppTok)->pModule = m;
181868 }
181869 sqlite3_free(p: (void *)aArg);
181870 }
181871
181872 sqlite3_free(p: zCopy);
181873 return rc;
181874}
181875
181876
181877#ifdef SQLITE_TEST
181878
181879#if defined(INCLUDE_SQLITE_TCL_H)
181880# include "sqlite_tcl.h"
181881#else
181882# include "tcl.h"
181883#endif
181884/* #include <string.h> */
181885
181886/*
181887** Implementation of a special SQL scalar function for testing tokenizers
181888** designed to be used in concert with the Tcl testing framework. This
181889** function must be called with two or more arguments:
181890**
181891** SELECT <function-name>(<key-name>, ..., <input-string>);
181892**
181893** where <function-name> is the name passed as the second argument
181894** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer')
181895** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test').
181896**
181897** The return value is a string that may be interpreted as a Tcl
181898** list. For each token in the <input-string>, three elements are
181899** added to the returned list. The first is the token position, the
181900** second is the token text (folded, stemmed, etc.) and the third is the
181901** substring of <input-string> associated with the token. For example,
181902** using the built-in "simple" tokenizer:
181903**
181904** SELECT fts_tokenizer_test('simple', 'I don't see how');
181905**
181906** will return the string:
181907**
181908** "{0 i I 1 dont don't 2 see see 3 how how}"
181909**
181910*/
181911static void testFunc(
181912 sqlite3_context *context,
181913 int argc,
181914 sqlite3_value **argv
181915){
181916 Fts3Hash *pHash;
181917 sqlite3_tokenizer_module *p;
181918 sqlite3_tokenizer *pTokenizer = 0;
181919 sqlite3_tokenizer_cursor *pCsr = 0;
181920
181921 const char *zErr = 0;
181922
181923 const char *zName;
181924 int nName;
181925 const char *zInput;
181926 int nInput;
181927
181928 const char *azArg[64];
181929
181930 const char *zToken;
181931 int nToken = 0;
181932 int iStart = 0;
181933 int iEnd = 0;
181934 int iPos = 0;
181935 int i;
181936
181937 Tcl_Obj *pRet;
181938
181939 if( argc<2 ){
181940 sqlite3_result_error(context, "insufficient arguments", -1);
181941 return;
181942 }
181943
181944 nName = sqlite3_value_bytes(argv[0]);
181945 zName = (const char *)sqlite3_value_text(argv[0]);
181946 nInput = sqlite3_value_bytes(argv[argc-1]);
181947 zInput = (const char *)sqlite3_value_text(argv[argc-1]);
181948
181949 pHash = (Fts3Hash *)sqlite3_user_data(context);
181950 p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
181951
181952 if( !p ){
181953 char *zErr2 = sqlite3_mprintf("unknown tokenizer: %s", zName);
181954 sqlite3_result_error(context, zErr2, -1);
181955 sqlite3_free(zErr2);
181956 return;
181957 }
181958
181959 pRet = Tcl_NewObj();
181960 Tcl_IncrRefCount(pRet);
181961
181962 for(i=1; i<argc-1; i++){
181963 azArg[i-1] = (const char *)sqlite3_value_text(argv[i]);
181964 }
181965
181966 if( SQLITE_OK!=p->xCreate(argc-2, azArg, &pTokenizer) ){
181967 zErr = "error in xCreate()";
181968 goto finish;
181969 }
181970 pTokenizer->pModule = p;
181971 if( sqlite3Fts3OpenTokenizer(pTokenizer, 0, zInput, nInput, &pCsr) ){
181972 zErr = "error in xOpen()";
181973 goto finish;
181974 }
181975
181976 while( SQLITE_OK==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){
181977 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iPos));
181978 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
181979 zToken = &zInput[iStart];
181980 nToken = iEnd-iStart;
181981 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
181982 }
181983
181984 if( SQLITE_OK!=p->xClose(pCsr) ){
181985 zErr = "error in xClose()";
181986 goto finish;
181987 }
181988 if( SQLITE_OK!=p->xDestroy(pTokenizer) ){
181989 zErr = "error in xDestroy()";
181990 goto finish;
181991 }
181992
181993finish:
181994 if( zErr ){
181995 sqlite3_result_error(context, zErr, -1);
181996 }else{
181997 sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
181998 }
181999 Tcl_DecrRefCount(pRet);
182000}
182001
182002static
182003int registerTokenizer(
182004 sqlite3 *db,
182005 char *zName,
182006 const sqlite3_tokenizer_module *p
182007){
182008 int rc;
182009 sqlite3_stmt *pStmt;
182010 const char zSql[] = "SELECT fts3_tokenizer(?, ?)";
182011
182012 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
182013 if( rc!=SQLITE_OK ){
182014 return rc;
182015 }
182016
182017 sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
182018 sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC);
182019 sqlite3_step(pStmt);
182020
182021 return sqlite3_finalize(pStmt);
182022}
182023
182024
182025static
182026int queryTokenizer(
182027 sqlite3 *db,
182028 char *zName,
182029 const sqlite3_tokenizer_module **pp
182030){
182031 int rc;
182032 sqlite3_stmt *pStmt;
182033 const char zSql[] = "SELECT fts3_tokenizer(?)";
182034
182035 *pp = 0;
182036 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
182037 if( rc!=SQLITE_OK ){
182038 return rc;
182039 }
182040
182041 sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
182042 if( SQLITE_ROW==sqlite3_step(pStmt) ){
182043 if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB
182044 && sqlite3_column_bytes(pStmt, 0)==sizeof(*pp)
182045 ){
182046 memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
182047 }
182048 }
182049
182050 return sqlite3_finalize(pStmt);
182051}
182052
182053SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
182054
182055/*
182056** Implementation of the scalar function fts3_tokenizer_internal_test().
182057** This function is used for testing only, it is not included in the
182058** build unless SQLITE_TEST is defined.
182059**
182060** The purpose of this is to test that the fts3_tokenizer() function
182061** can be used as designed by the C-code in the queryTokenizer and
182062** registerTokenizer() functions above. These two functions are repeated
182063** in the README.tokenizer file as an example, so it is important to
182064** test them.
182065**
182066** To run the tests, evaluate the fts3_tokenizer_internal_test() scalar
182067** function with no arguments. An assert() will fail if a problem is
182068** detected. i.e.:
182069**
182070** SELECT fts3_tokenizer_internal_test();
182071**
182072*/
182073static void intTestFunc(
182074 sqlite3_context *context,
182075 int argc,
182076 sqlite3_value **argv
182077){
182078 int rc;
182079 const sqlite3_tokenizer_module *p1;
182080 const sqlite3_tokenizer_module *p2;
182081 sqlite3 *db = (sqlite3 *)sqlite3_user_data(context);
182082
182083 UNUSED_PARAMETER(argc);
182084 UNUSED_PARAMETER(argv);
182085
182086 /* Test the query function */
182087 sqlite3Fts3SimpleTokenizerModule(&p1);
182088 rc = queryTokenizer(db, "simple", &p2);
182089 assert( rc==SQLITE_OK );
182090 assert( p1==p2 );
182091 rc = queryTokenizer(db, "nosuchtokenizer", &p2);
182092 assert( rc==SQLITE_ERROR );
182093 assert( p2==0 );
182094 assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") );
182095
182096 /* Test the storage function */
182097 if( fts3TokenizerEnabled(context) ){
182098 rc = registerTokenizer(db, "nosuchtokenizer", p1);
182099 assert( rc==SQLITE_OK );
182100 rc = queryTokenizer(db, "nosuchtokenizer", &p2);
182101 assert( rc==SQLITE_OK );
182102 assert( p2==p1 );
182103 }
182104
182105 sqlite3_result_text(context, "ok", -1, SQLITE_STATIC);
182106}
182107
182108#endif
182109
182110/*
182111** Set up SQL objects in database db used to access the contents of
182112** the hash table pointed to by argument pHash. The hash table must
182113** been initialized to use string keys, and to take a private copy
182114** of the key when a value is inserted. i.e. by a call similar to:
182115**
182116** sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
182117**
182118** This function adds a scalar function (see header comment above
182119** fts3TokenizerFunc() in this file for details) and, if ENABLE_TABLE is
182120** defined at compilation time, a temporary virtual table (see header
182121** comment above struct HashTableVtab) to the database schema. Both
182122** provide read/write access to the contents of *pHash.
182123**
182124** The third argument to this function, zName, is used as the name
182125** of both the scalar and, if created, the virtual table.
182126*/
182127SQLITE_PRIVATE int sqlite3Fts3InitHashTable(
182128 sqlite3 *db,
182129 Fts3Hash *pHash,
182130 const char *zName
182131){
182132 int rc = SQLITE_OK;
182133 void *p = (void *)pHash;
182134 const int any = SQLITE_UTF8|SQLITE_DIRECTONLY;
182135
182136#ifdef SQLITE_TEST
182137 char *zTest = 0;
182138 char *zTest2 = 0;
182139 void *pdb = (void *)db;
182140 zTest = sqlite3_mprintf("%s_test", zName);
182141 zTest2 = sqlite3_mprintf("%s_internal_test", zName);
182142 if( !zTest || !zTest2 ){
182143 rc = SQLITE_NOMEM;
182144 }
182145#endif
182146
182147 if( SQLITE_OK==rc ){
182148 rc = sqlite3_create_function(db, zFunc: zName, nArg: 1, enc: any, p, xSFunc: fts3TokenizerFunc, xStep: 0, xFinal: 0);
182149 }
182150 if( SQLITE_OK==rc ){
182151 rc = sqlite3_create_function(db, zFunc: zName, nArg: 2, enc: any, p, xSFunc: fts3TokenizerFunc, xStep: 0, xFinal: 0);
182152 }
182153#ifdef SQLITE_TEST
182154 if( SQLITE_OK==rc ){
182155 rc = sqlite3_create_function(db, zTest, -1, any, p, testFunc, 0, 0);
182156 }
182157 if( SQLITE_OK==rc ){
182158 rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0);
182159 }
182160#endif
182161
182162#ifdef SQLITE_TEST
182163 sqlite3_free(zTest);
182164 sqlite3_free(zTest2);
182165#endif
182166
182167 return rc;
182168}
182169
182170#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
182171
182172/************** End of fts3_tokenizer.c **************************************/
182173/************** Begin file fts3_tokenizer1.c *********************************/
182174/*
182175** 2006 Oct 10
182176**
182177** The author disclaims copyright to this source code. In place of
182178** a legal notice, here is a blessing:
182179**
182180** May you do good and not evil.
182181** May you find forgiveness for yourself and forgive others.
182182** May you share freely, never taking more than you give.
182183**
182184******************************************************************************
182185**
182186** Implementation of the "simple" full-text-search tokenizer.
182187*/
182188
182189/*
182190** The code in this file is only compiled if:
182191**
182192** * The FTS3 module is being built as an extension
182193** (in which case SQLITE_CORE is not defined), or
182194**
182195** * The FTS3 module is being built into the core of
182196** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
182197*/
182198/* #include "fts3Int.h" */
182199#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
182200
182201/* #include <assert.h> */
182202/* #include <stdlib.h> */
182203/* #include <stdio.h> */
182204/* #include <string.h> */
182205
182206/* #include "fts3_tokenizer.h" */
182207
182208typedef struct simple_tokenizer {
182209 sqlite3_tokenizer base;
182210 char delim[128]; /* flag ASCII delimiters */
182211} simple_tokenizer;
182212
182213typedef struct simple_tokenizer_cursor {
182214 sqlite3_tokenizer_cursor base;
182215 const char *pInput; /* input we are tokenizing */
182216 int nBytes; /* size of the input */
182217 int iOffset; /* current position in pInput */
182218 int iToken; /* index of next token to be returned */
182219 char *pToken; /* storage for current token */
182220 int nTokenAllocated; /* space allocated to zToken buffer */
182221} simple_tokenizer_cursor;
182222
182223
182224static int simpleDelim(simple_tokenizer *t, unsigned char c){
182225 return c<0x80 && t->delim[c];
182226}
182227static int fts3_isalnum(int x){
182228 return (x>='0' && x<='9') || (x>='A' && x<='Z') || (x>='a' && x<='z');
182229}
182230
182231/*
182232** Create a new tokenizer instance.
182233*/
182234static int simpleCreate(
182235 int argc, const char * const *argv,
182236 sqlite3_tokenizer **ppTokenizer
182237){
182238 simple_tokenizer *t;
182239
182240 t = (simple_tokenizer *) sqlite3_malloc(n: sizeof(*t));
182241 if( t==NULL ) return SQLITE_NOMEM;
182242 memset(s: t, c: 0, n: sizeof(*t));
182243
182244 /* TODO(shess) Delimiters need to remain the same from run to run,
182245 ** else we need to reindex. One solution would be a meta-table to
182246 ** track such information in the database, then we'd only want this
182247 ** information on the initial create.
182248 */
182249 if( argc>1 ){
182250 int i, n = (int)strlen(s: argv[1]);
182251 for(i=0; i<n; i++){
182252 unsigned char ch = argv[1][i];
182253 /* We explicitly don't support UTF-8 delimiters for now. */
182254 if( ch>=0x80 ){
182255 sqlite3_free(p: t);
182256 return SQLITE_ERROR;
182257 }
182258 t->delim[ch] = 1;
182259 }
182260 } else {
182261 /* Mark non-alphanumeric ASCII characters as delimiters */
182262 int i;
182263 for(i=1; i<0x80; i++){
182264 t->delim[i] = !fts3_isalnum(x: i) ? -1 : 0;
182265 }
182266 }
182267
182268 *ppTokenizer = &t->base;
182269 return SQLITE_OK;
182270}
182271
182272/*
182273** Destroy a tokenizer
182274*/
182275static int simpleDestroy(sqlite3_tokenizer *pTokenizer){
182276 sqlite3_free(p: pTokenizer);
182277 return SQLITE_OK;
182278}
182279
182280/*
182281** Prepare to begin tokenizing a particular string. The input
182282** string to be tokenized is pInput[0..nBytes-1]. A cursor
182283** used to incrementally tokenize this string is returned in
182284** *ppCursor.
182285*/
182286static int simpleOpen(
182287 sqlite3_tokenizer *pTokenizer, /* The tokenizer */
182288 const char *pInput, int nBytes, /* String to be tokenized */
182289 sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
182290){
182291 simple_tokenizer_cursor *c;
182292
182293 UNUSED_PARAMETER(pTokenizer);
182294
182295 c = (simple_tokenizer_cursor *) sqlite3_malloc(n: sizeof(*c));
182296 if( c==NULL ) return SQLITE_NOMEM;
182297
182298 c->pInput = pInput;
182299 if( pInput==0 ){
182300 c->nBytes = 0;
182301 }else if( nBytes<0 ){
182302 c->nBytes = (int)strlen(s: pInput);
182303 }else{
182304 c->nBytes = nBytes;
182305 }
182306 c->iOffset = 0; /* start tokenizing at the beginning */
182307 c->iToken = 0;
182308 c->pToken = NULL; /* no space allocated, yet. */
182309 c->nTokenAllocated = 0;
182310
182311 *ppCursor = &c->base;
182312 return SQLITE_OK;
182313}
182314
182315/*
182316** Close a tokenization cursor previously opened by a call to
182317** simpleOpen() above.
182318*/
182319static int simpleClose(sqlite3_tokenizer_cursor *pCursor){
182320 simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
182321 sqlite3_free(p: c->pToken);
182322 sqlite3_free(p: c);
182323 return SQLITE_OK;
182324}
182325
182326/*
182327** Extract the next token from a tokenization cursor. The cursor must
182328** have been opened by a prior call to simpleOpen().
182329*/
182330static int simpleNext(
182331 sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by simpleOpen */
182332 const char **ppToken, /* OUT: *ppToken is the token text */
182333 int *pnBytes, /* OUT: Number of bytes in token */
182334 int *piStartOffset, /* OUT: Starting offset of token */
182335 int *piEndOffset, /* OUT: Ending offset of token */
182336 int *piPosition /* OUT: Position integer of token */
182337){
182338 simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
182339 simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer;
182340 unsigned char *p = (unsigned char *)c->pInput;
182341
182342 while( c->iOffset<c->nBytes ){
182343 int iStartOffset;
182344
182345 /* Scan past delimiter characters */
182346 while( c->iOffset<c->nBytes && simpleDelim(t, c: p[c->iOffset]) ){
182347 c->iOffset++;
182348 }
182349
182350 /* Count non-delimiter characters. */
182351 iStartOffset = c->iOffset;
182352 while( c->iOffset<c->nBytes && !simpleDelim(t, c: p[c->iOffset]) ){
182353 c->iOffset++;
182354 }
182355
182356 if( c->iOffset>iStartOffset ){
182357 int i, n = c->iOffset-iStartOffset;
182358 if( n>c->nTokenAllocated ){
182359 char *pNew;
182360 c->nTokenAllocated = n+20;
182361 pNew = sqlite3_realloc(pOld: c->pToken, n: c->nTokenAllocated);
182362 if( !pNew ) return SQLITE_NOMEM;
182363 c->pToken = pNew;
182364 }
182365 for(i=0; i<n; i++){
182366 /* TODO(shess) This needs expansion to handle UTF-8
182367 ** case-insensitivity.
182368 */
182369 unsigned char ch = p[iStartOffset+i];
182370 c->pToken[i] = (char)((ch>='A' && ch<='Z') ? ch-'A'+'a' : ch);
182371 }
182372 *ppToken = c->pToken;
182373 *pnBytes = n;
182374 *piStartOffset = iStartOffset;
182375 *piEndOffset = c->iOffset;
182376 *piPosition = c->iToken++;
182377
182378 return SQLITE_OK;
182379 }
182380 }
182381 return SQLITE_DONE;
182382}
182383
182384/*
182385** The set of routines that implement the simple tokenizer
182386*/
182387static const sqlite3_tokenizer_module simpleTokenizerModule = {
182388 0,
182389 simpleCreate,
182390 simpleDestroy,
182391 simpleOpen,
182392 simpleClose,
182393 simpleNext,
182394 0,
182395};
182396
182397/*
182398** Allocate a new simple tokenizer. Return a pointer to the new
182399** tokenizer in *ppModule
182400*/
182401SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(
182402 sqlite3_tokenizer_module const**ppModule
182403){
182404 *ppModule = &simpleTokenizerModule;
182405}
182406
182407#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
182408
182409/************** End of fts3_tokenizer1.c *************************************/
182410/************** Begin file fts3_tokenize_vtab.c ******************************/
182411/*
182412** 2013 Apr 22
182413**
182414** The author disclaims copyright to this source code. In place of
182415** a legal notice, here is a blessing:
182416**
182417** May you do good and not evil.
182418** May you find forgiveness for yourself and forgive others.
182419** May you share freely, never taking more than you give.
182420**
182421******************************************************************************
182422**
182423** This file contains code for the "fts3tokenize" virtual table module.
182424** An fts3tokenize virtual table is created as follows:
182425**
182426** CREATE VIRTUAL TABLE <tbl> USING fts3tokenize(
182427** <tokenizer-name>, <arg-1>, ...
182428** );
182429**
182430** The table created has the following schema:
182431**
182432** CREATE TABLE <tbl>(input, token, start, end, position)
182433**
182434** When queried, the query must include a WHERE clause of type:
182435**
182436** input = <string>
182437**
182438** The virtual table module tokenizes this <string>, using the FTS3
182439** tokenizer specified by the arguments to the CREATE VIRTUAL TABLE
182440** statement and returns one row for each token in the result. With
182441** fields set as follows:
182442**
182443** input: Always set to a copy of <string>
182444** token: A token from the input.
182445** start: Byte offset of the token within the input <string>.
182446** end: Byte offset of the byte immediately following the end of the
182447** token within the input string.
182448** pos: Token offset of token within input.
182449**
182450*/
182451/* #include "fts3Int.h" */
182452#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
182453
182454/* #include <string.h> */
182455/* #include <assert.h> */
182456
182457typedef struct Fts3tokTable Fts3tokTable;
182458typedef struct Fts3tokCursor Fts3tokCursor;
182459
182460/*
182461** Virtual table structure.
182462*/
182463struct Fts3tokTable {
182464 sqlite3_vtab base; /* Base class used by SQLite core */
182465 const sqlite3_tokenizer_module *pMod;
182466 sqlite3_tokenizer *pTok;
182467};
182468
182469/*
182470** Virtual table cursor structure.
182471*/
182472struct Fts3tokCursor {
182473 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
182474 char *zInput; /* Input string */
182475 sqlite3_tokenizer_cursor *pCsr; /* Cursor to iterate through zInput */
182476 int iRowid; /* Current 'rowid' value */
182477 const char *zToken; /* Current 'token' value */
182478 int nToken; /* Size of zToken in bytes */
182479 int iStart; /* Current 'start' value */
182480 int iEnd; /* Current 'end' value */
182481 int iPos; /* Current 'pos' value */
182482};
182483
182484/*
182485** Query FTS for the tokenizer implementation named zName.
182486*/
182487static int fts3tokQueryTokenizer(
182488 Fts3Hash *pHash,
182489 const char *zName,
182490 const sqlite3_tokenizer_module **pp,
182491 char **pzErr
182492){
182493 sqlite3_tokenizer_module *p;
182494 int nName = (int)strlen(s: zName);
182495
182496 p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pH: pHash, pKey: zName, nKey: nName+1);
182497 if( !p ){
182498 sqlite3Fts3ErrMsg(pzErr, zFormat: "unknown tokenizer: %s", zName);
182499 return SQLITE_ERROR;
182500 }
182501
182502 *pp = p;
182503 return SQLITE_OK;
182504}
182505
182506/*
182507** The second argument, argv[], is an array of pointers to nul-terminated
182508** strings. This function makes a copy of the array and strings into a
182509** single block of memory. It then dequotes any of the strings that appear
182510** to be quoted.
182511**
182512** If successful, output parameter *pazDequote is set to point at the
182513** array of dequoted strings and SQLITE_OK is returned. The caller is
182514** responsible for eventually calling sqlite3_free() to free the array
182515** in this case. Or, if an error occurs, an SQLite error code is returned.
182516** The final value of *pazDequote is undefined in this case.
182517*/
182518static int fts3tokDequoteArray(
182519 int argc, /* Number of elements in argv[] */
182520 const char * const *argv, /* Input array */
182521 char ***pazDequote /* Output array */
182522){
182523 int rc = SQLITE_OK; /* Return code */
182524 if( argc==0 ){
182525 *pazDequote = 0;
182526 }else{
182527 int i;
182528 int nByte = 0;
182529 char **azDequote;
182530
182531 for(i=0; i<argc; i++){
182532 nByte += (int)(strlen(s: argv[i]) + 1);
182533 }
182534
182535 *pazDequote = azDequote = sqlite3_malloc64(n: sizeof(char *)*argc + nByte);
182536 if( azDequote==0 ){
182537 rc = SQLITE_NOMEM;
182538 }else{
182539 char *pSpace = (char *)&azDequote[argc];
182540 for(i=0; i<argc; i++){
182541 int n = (int)strlen(s: argv[i]);
182542 azDequote[i] = pSpace;
182543 memcpy(dest: pSpace, src: argv[i], n: n+1);
182544 sqlite3Fts3Dequote(z: pSpace);
182545 pSpace += (n+1);
182546 }
182547 }
182548 }
182549
182550 return rc;
182551}
182552
182553/*
182554** Schema of the tokenizer table.
182555*/
182556#define FTS3_TOK_SCHEMA "CREATE TABLE x(input, token, start, end, position)"
182557
182558/*
182559** This function does all the work for both the xConnect and xCreate methods.
182560** These tables have no persistent representation of their own, so xConnect
182561** and xCreate are identical operations.
182562**
182563** argv[0]: module name
182564** argv[1]: database name
182565** argv[2]: table name
182566** argv[3]: first argument (tokenizer name)
182567*/
182568static int fts3tokConnectMethod(
182569 sqlite3 *db, /* Database connection */
182570 void *pHash, /* Hash table of tokenizers */
182571 int argc, /* Number of elements in argv array */
182572 const char * const *argv, /* xCreate/xConnect argument array */
182573 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
182574 char **pzErr /* OUT: sqlite3_malloc'd error message */
182575){
182576 Fts3tokTable *pTab = 0;
182577 const sqlite3_tokenizer_module *pMod = 0;
182578 sqlite3_tokenizer *pTok = 0;
182579 int rc;
182580 char **azDequote = 0;
182581 int nDequote;
182582
182583 rc = sqlite3_declare_vtab(db, FTS3_TOK_SCHEMA);
182584 if( rc!=SQLITE_OK ) return rc;
182585
182586 nDequote = argc-3;
182587 rc = fts3tokDequoteArray(argc: nDequote, argv: &argv[3], pazDequote: &azDequote);
182588
182589 if( rc==SQLITE_OK ){
182590 const char *zModule;
182591 if( nDequote<1 ){
182592 zModule = "simple";
182593 }else{
182594 zModule = azDequote[0];
182595 }
182596 rc = fts3tokQueryTokenizer(pHash: (Fts3Hash*)pHash, zName: zModule, pp: &pMod, pzErr);
182597 }
182598
182599 assert( (rc==SQLITE_OK)==(pMod!=0) );
182600 if( rc==SQLITE_OK ){
182601 const char * const *azArg = 0;
182602 if( nDequote>1 ) azArg = (const char * const *)&azDequote[1];
182603 rc = pMod->xCreate((nDequote>1 ? nDequote-1 : 0), azArg, &pTok);
182604 }
182605
182606 if( rc==SQLITE_OK ){
182607 pTab = (Fts3tokTable *)sqlite3_malloc(n: sizeof(Fts3tokTable));
182608 if( pTab==0 ){
182609 rc = SQLITE_NOMEM;
182610 }
182611 }
182612
182613 if( rc==SQLITE_OK ){
182614 memset(s: pTab, c: 0, n: sizeof(Fts3tokTable));
182615 pTab->pMod = pMod;
182616 pTab->pTok = pTok;
182617 *ppVtab = &pTab->base;
182618 }else{
182619 if( pTok ){
182620 pMod->xDestroy(pTok);
182621 }
182622 }
182623
182624 sqlite3_free(p: azDequote);
182625 return rc;
182626}
182627
182628/*
182629** This function does the work for both the xDisconnect and xDestroy methods.
182630** These tables have no persistent representation of their own, so xDisconnect
182631** and xDestroy are identical operations.
182632*/
182633static int fts3tokDisconnectMethod(sqlite3_vtab *pVtab){
182634 Fts3tokTable *pTab = (Fts3tokTable *)pVtab;
182635
182636 pTab->pMod->xDestroy(pTab->pTok);
182637 sqlite3_free(p: pTab);
182638 return SQLITE_OK;
182639}
182640
182641/*
182642** xBestIndex - Analyze a WHERE and ORDER BY clause.
182643*/
182644static int fts3tokBestIndexMethod(
182645 sqlite3_vtab *pVTab,
182646 sqlite3_index_info *pInfo
182647){
182648 int i;
182649 UNUSED_PARAMETER(pVTab);
182650
182651 for(i=0; i<pInfo->nConstraint; i++){
182652 if( pInfo->aConstraint[i].usable
182653 && pInfo->aConstraint[i].iColumn==0
182654 && pInfo->aConstraint[i].op==SQLITE_INDEX_CONSTRAINT_EQ
182655 ){
182656 pInfo->idxNum = 1;
182657 pInfo->aConstraintUsage[i].argvIndex = 1;
182658 pInfo->aConstraintUsage[i].omit = 1;
182659 pInfo->estimatedCost = 1;
182660 return SQLITE_OK;
182661 }
182662 }
182663
182664 pInfo->idxNum = 0;
182665 assert( pInfo->estimatedCost>1000000.0 );
182666
182667 return SQLITE_OK;
182668}
182669
182670/*
182671** xOpen - Open a cursor.
182672*/
182673static int fts3tokOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
182674 Fts3tokCursor *pCsr;
182675 UNUSED_PARAMETER(pVTab);
182676
182677 pCsr = (Fts3tokCursor *)sqlite3_malloc(n: sizeof(Fts3tokCursor));
182678 if( pCsr==0 ){
182679 return SQLITE_NOMEM;
182680 }
182681 memset(s: pCsr, c: 0, n: sizeof(Fts3tokCursor));
182682
182683 *ppCsr = (sqlite3_vtab_cursor *)pCsr;
182684 return SQLITE_OK;
182685}
182686
182687/*
182688** Reset the tokenizer cursor passed as the only argument. As if it had
182689** just been returned by fts3tokOpenMethod().
182690*/
182691static void fts3tokResetCursor(Fts3tokCursor *pCsr){
182692 if( pCsr->pCsr ){
182693 Fts3tokTable *pTab = (Fts3tokTable *)(pCsr->base.pVtab);
182694 pTab->pMod->xClose(pCsr->pCsr);
182695 pCsr->pCsr = 0;
182696 }
182697 sqlite3_free(p: pCsr->zInput);
182698 pCsr->zInput = 0;
182699 pCsr->zToken = 0;
182700 pCsr->nToken = 0;
182701 pCsr->iStart = 0;
182702 pCsr->iEnd = 0;
182703 pCsr->iPos = 0;
182704 pCsr->iRowid = 0;
182705}
182706
182707/*
182708** xClose - Close a cursor.
182709*/
182710static int fts3tokCloseMethod(sqlite3_vtab_cursor *pCursor){
182711 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
182712
182713 fts3tokResetCursor(pCsr);
182714 sqlite3_free(p: pCsr);
182715 return SQLITE_OK;
182716}
182717
182718/*
182719** xNext - Advance the cursor to the next row, if any.
182720*/
182721static int fts3tokNextMethod(sqlite3_vtab_cursor *pCursor){
182722 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
182723 Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
182724 int rc; /* Return code */
182725
182726 pCsr->iRowid++;
182727 rc = pTab->pMod->xNext(pCsr->pCsr,
182728 &pCsr->zToken, &pCsr->nToken,
182729 &pCsr->iStart, &pCsr->iEnd, &pCsr->iPos
182730 );
182731
182732 if( rc!=SQLITE_OK ){
182733 fts3tokResetCursor(pCsr);
182734 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
182735 }
182736
182737 return rc;
182738}
182739
182740/*
182741** xFilter - Initialize a cursor to point at the start of its data.
182742*/
182743static int fts3tokFilterMethod(
182744 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
182745 int idxNum, /* Strategy index */
182746 const char *idxStr, /* Unused */
182747 int nVal, /* Number of elements in apVal */
182748 sqlite3_value **apVal /* Arguments for the indexing scheme */
182749){
182750 int rc = SQLITE_ERROR;
182751 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
182752 Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
182753 UNUSED_PARAMETER(idxStr);
182754 UNUSED_PARAMETER(nVal);
182755
182756 fts3tokResetCursor(pCsr);
182757 if( idxNum==1 ){
182758 const char *zByte = (const char *)sqlite3_value_text(pVal: apVal[0]);
182759 int nByte = sqlite3_value_bytes(pVal: apVal[0]);
182760 pCsr->zInput = sqlite3_malloc64(n: nByte+1);
182761 if( pCsr->zInput==0 ){
182762 rc = SQLITE_NOMEM;
182763 }else{
182764 if( nByte>0 ) memcpy(dest: pCsr->zInput, src: zByte, n: nByte);
182765 pCsr->zInput[nByte] = 0;
182766 rc = pTab->pMod->xOpen(pTab->pTok, pCsr->zInput, nByte, &pCsr->pCsr);
182767 if( rc==SQLITE_OK ){
182768 pCsr->pCsr->pTokenizer = pTab->pTok;
182769 }
182770 }
182771 }
182772
182773 if( rc!=SQLITE_OK ) return rc;
182774 return fts3tokNextMethod(pCursor);
182775}
182776
182777/*
182778** xEof - Return true if the cursor is at EOF, or false otherwise.
182779*/
182780static int fts3tokEofMethod(sqlite3_vtab_cursor *pCursor){
182781 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
182782 return (pCsr->zToken==0);
182783}
182784
182785/*
182786** xColumn - Return a column value.
182787*/
182788static int fts3tokColumnMethod(
182789 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
182790 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
182791 int iCol /* Index of column to read value from */
182792){
182793 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
182794
182795 /* CREATE TABLE x(input, token, start, end, position) */
182796 switch( iCol ){
182797 case 0:
182798 sqlite3_result_text(pCtx, z: pCsr->zInput, n: -1, SQLITE_TRANSIENT);
182799 break;
182800 case 1:
182801 sqlite3_result_text(pCtx, z: pCsr->zToken, n: pCsr->nToken, SQLITE_TRANSIENT);
182802 break;
182803 case 2:
182804 sqlite3_result_int(pCtx, iVal: pCsr->iStart);
182805 break;
182806 case 3:
182807 sqlite3_result_int(pCtx, iVal: pCsr->iEnd);
182808 break;
182809 default:
182810 assert( iCol==4 );
182811 sqlite3_result_int(pCtx, iVal: pCsr->iPos);
182812 break;
182813 }
182814 return SQLITE_OK;
182815}
182816
182817/*
182818** xRowid - Return the current rowid for the cursor.
182819*/
182820static int fts3tokRowidMethod(
182821 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
182822 sqlite_int64 *pRowid /* OUT: Rowid value */
182823){
182824 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
182825 *pRowid = (sqlite3_int64)pCsr->iRowid;
182826 return SQLITE_OK;
182827}
182828
182829/*
182830** Register the fts3tok module with database connection db. Return SQLITE_OK
182831** if successful or an error code if sqlite3_create_module() fails.
182832*/
182833SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3 *db, Fts3Hash *pHash){
182834 static const sqlite3_module fts3tok_module = {
182835 0, /* iVersion */
182836 fts3tokConnectMethod, /* xCreate */
182837 fts3tokConnectMethod, /* xConnect */
182838 fts3tokBestIndexMethod, /* xBestIndex */
182839 fts3tokDisconnectMethod, /* xDisconnect */
182840 fts3tokDisconnectMethod, /* xDestroy */
182841 fts3tokOpenMethod, /* xOpen */
182842 fts3tokCloseMethod, /* xClose */
182843 fts3tokFilterMethod, /* xFilter */
182844 fts3tokNextMethod, /* xNext */
182845 fts3tokEofMethod, /* xEof */
182846 fts3tokColumnMethod, /* xColumn */
182847 fts3tokRowidMethod, /* xRowid */
182848 0, /* xUpdate */
182849 0, /* xBegin */
182850 0, /* xSync */
182851 0, /* xCommit */
182852 0, /* xRollback */
182853 0, /* xFindFunction */
182854 0, /* xRename */
182855 0, /* xSavepoint */
182856 0, /* xRelease */
182857 0, /* xRollbackTo */
182858 0 /* xShadowName */
182859 };
182860 int rc; /* Return code */
182861
182862 rc = sqlite3_create_module(db, zName: "fts3tokenize", pModule: &fts3tok_module, pAux: (void*)pHash);
182863 return rc;
182864}
182865
182866#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
182867
182868/************** End of fts3_tokenize_vtab.c **********************************/
182869/************** Begin file fts3_write.c **************************************/
182870/*
182871** 2009 Oct 23
182872**
182873** The author disclaims copyright to this source code. In place of
182874** a legal notice, here is a blessing:
182875**
182876** May you do good and not evil.
182877** May you find forgiveness for yourself and forgive others.
182878** May you share freely, never taking more than you give.
182879**
182880******************************************************************************
182881**
182882** This file is part of the SQLite FTS3 extension module. Specifically,
182883** this file contains code to insert, update and delete rows from FTS3
182884** tables. It also contains code to merge FTS3 b-tree segments. Some
182885** of the sub-routines used to merge segments are also used by the query
182886** code in fts3.c.
182887*/
182888
182889/* #include "fts3Int.h" */
182890#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
182891
182892/* #include <string.h> */
182893/* #include <assert.h> */
182894/* #include <stdlib.h> */
182895/* #include <stdio.h> */
182896
182897#define FTS_MAX_APPENDABLE_HEIGHT 16
182898
182899/*
182900** When full-text index nodes are loaded from disk, the buffer that they
182901** are loaded into has the following number of bytes of padding at the end
182902** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
182903** of 920 bytes is allocated for it.
182904**
182905** This means that if we have a pointer into a buffer containing node data,
182906** it is always safe to read up to two varints from it without risking an
182907** overread, even if the node data is corrupted.
182908*/
182909#define FTS3_NODE_PADDING (FTS3_VARINT_MAX*2)
182910
182911/*
182912** Under certain circumstances, b-tree nodes (doclists) can be loaded into
182913** memory incrementally instead of all at once. This can be a big performance
182914** win (reduced IO and CPU) if SQLite stops calling the virtual table xNext()
182915** method before retrieving all query results (as may happen, for example,
182916** if a query has a LIMIT clause).
182917**
182918** Incremental loading is used for b-tree nodes FTS3_NODE_CHUNK_THRESHOLD
182919** bytes and larger. Nodes are loaded in chunks of FTS3_NODE_CHUNKSIZE bytes.
182920** The code is written so that the hard lower-limit for each of these values
182921** is 1. Clearly such small values would be inefficient, but can be useful
182922** for testing purposes.
182923**
182924** If this module is built with SQLITE_TEST defined, these constants may
182925** be overridden at runtime for testing purposes. File fts3_test.c contains
182926** a Tcl interface to read and write the values.
182927*/
182928#ifdef SQLITE_TEST
182929int test_fts3_node_chunksize = (4*1024);
182930int test_fts3_node_chunk_threshold = (4*1024)*4;
182931# define FTS3_NODE_CHUNKSIZE test_fts3_node_chunksize
182932# define FTS3_NODE_CHUNK_THRESHOLD test_fts3_node_chunk_threshold
182933#else
182934# define FTS3_NODE_CHUNKSIZE (4*1024)
182935# define FTS3_NODE_CHUNK_THRESHOLD (FTS3_NODE_CHUNKSIZE*4)
182936#endif
182937
182938/*
182939** The values that may be meaningfully bound to the :1 parameter in
182940** statements SQL_REPLACE_STAT and SQL_SELECT_STAT.
182941*/
182942#define FTS_STAT_DOCTOTAL 0
182943#define FTS_STAT_INCRMERGEHINT 1
182944#define FTS_STAT_AUTOINCRMERGE 2
182945
182946/*
182947** If FTS_LOG_MERGES is defined, call sqlite3_log() to report each automatic
182948** and incremental merge operation that takes place. This is used for
182949** debugging FTS only, it should not usually be turned on in production
182950** systems.
182951*/
182952#ifdef FTS3_LOG_MERGES
182953static void fts3LogMerge(int nMerge, sqlite3_int64 iAbsLevel){
182954 sqlite3_log(SQLITE_OK, "%d-way merge from level %d", nMerge, (int)iAbsLevel);
182955}
182956#else
182957#define fts3LogMerge(x, y)
182958#endif
182959
182960
182961typedef struct PendingList PendingList;
182962typedef struct SegmentNode SegmentNode;
182963typedef struct SegmentWriter SegmentWriter;
182964
182965/*
182966** An instance of the following data structure is used to build doclists
182967** incrementally. See function fts3PendingListAppend() for details.
182968*/
182969struct PendingList {
182970 int nData;
182971 char *aData;
182972 int nSpace;
182973 sqlite3_int64 iLastDocid;
182974 sqlite3_int64 iLastCol;
182975 sqlite3_int64 iLastPos;
182976};
182977
182978
182979/*
182980** Each cursor has a (possibly empty) linked list of the following objects.
182981*/
182982struct Fts3DeferredToken {
182983 Fts3PhraseToken *pToken; /* Pointer to corresponding expr token */
182984 int iCol; /* Column token must occur in */
182985 Fts3DeferredToken *pNext; /* Next in list of deferred tokens */
182986 PendingList *pList; /* Doclist is assembled here */
182987};
182988
182989/*
182990** An instance of this structure is used to iterate through the terms on
182991** a contiguous set of segment b-tree leaf nodes. Although the details of
182992** this structure are only manipulated by code in this file, opaque handles
182993** of type Fts3SegReader* are also used by code in fts3.c to iterate through
182994** terms when querying the full-text index. See functions:
182995**
182996** sqlite3Fts3SegReaderNew()
182997** sqlite3Fts3SegReaderFree()
182998** sqlite3Fts3SegReaderIterate()
182999**
183000** Methods used to manipulate Fts3SegReader structures:
183001**
183002** fts3SegReaderNext()
183003** fts3SegReaderFirstDocid()
183004** fts3SegReaderNextDocid()
183005*/
183006struct Fts3SegReader {
183007 int iIdx; /* Index within level, or 0x7FFFFFFF for PT */
183008 u8 bLookup; /* True for a lookup only */
183009 u8 rootOnly; /* True for a root-only reader */
183010
183011 sqlite3_int64 iStartBlock; /* Rowid of first leaf block to traverse */
183012 sqlite3_int64 iLeafEndBlock; /* Rowid of final leaf block to traverse */
183013 sqlite3_int64 iEndBlock; /* Rowid of final block in segment (or 0) */
183014 sqlite3_int64 iCurrentBlock; /* Current leaf block (or 0) */
183015
183016 char *aNode; /* Pointer to node data (or NULL) */
183017 int nNode; /* Size of buffer at aNode (or 0) */
183018 int nPopulate; /* If >0, bytes of buffer aNode[] loaded */
183019 sqlite3_blob *pBlob; /* If not NULL, blob handle to read node */
183020
183021 Fts3HashElem **ppNextElem;
183022
183023 /* Variables set by fts3SegReaderNext(). These may be read directly
183024 ** by the caller. They are valid from the time SegmentReaderNew() returns
183025 ** until SegmentReaderNext() returns something other than SQLITE_OK
183026 ** (i.e. SQLITE_DONE).
183027 */
183028 int nTerm; /* Number of bytes in current term */
183029 char *zTerm; /* Pointer to current term */
183030 int nTermAlloc; /* Allocated size of zTerm buffer */
183031 char *aDoclist; /* Pointer to doclist of current entry */
183032 int nDoclist; /* Size of doclist in current entry */
183033
183034 /* The following variables are used by fts3SegReaderNextDocid() to iterate
183035 ** through the current doclist (aDoclist/nDoclist).
183036 */
183037 char *pOffsetList;
183038 int nOffsetList; /* For descending pending seg-readers only */
183039 sqlite3_int64 iDocid;
183040};
183041
183042#define fts3SegReaderIsPending(p) ((p)->ppNextElem!=0)
183043#define fts3SegReaderIsRootOnly(p) ((p)->rootOnly!=0)
183044
183045/*
183046** An instance of this structure is used to create a segment b-tree in the
183047** database. The internal details of this type are only accessed by the
183048** following functions:
183049**
183050** fts3SegWriterAdd()
183051** fts3SegWriterFlush()
183052** fts3SegWriterFree()
183053*/
183054struct SegmentWriter {
183055 SegmentNode *pTree; /* Pointer to interior tree structure */
183056 sqlite3_int64 iFirst; /* First slot in %_segments written */
183057 sqlite3_int64 iFree; /* Next free slot in %_segments */
183058 char *zTerm; /* Pointer to previous term buffer */
183059 int nTerm; /* Number of bytes in zTerm */
183060 int nMalloc; /* Size of malloc'd buffer at zMalloc */
183061 char *zMalloc; /* Malloc'd space (possibly) used for zTerm */
183062 int nSize; /* Size of allocation at aData */
183063 int nData; /* Bytes of data in aData */
183064 char *aData; /* Pointer to block from malloc() */
183065 i64 nLeafData; /* Number of bytes of leaf data written */
183066};
183067
183068/*
183069** Type SegmentNode is used by the following three functions to create
183070** the interior part of the segment b+-tree structures (everything except
183071** the leaf nodes). These functions and type are only ever used by code
183072** within the fts3SegWriterXXX() family of functions described above.
183073**
183074** fts3NodeAddTerm()
183075** fts3NodeWrite()
183076** fts3NodeFree()
183077**
183078** When a b+tree is written to the database (either as a result of a merge
183079** or the pending-terms table being flushed), leaves are written into the
183080** database file as soon as they are completely populated. The interior of
183081** the tree is assembled in memory and written out only once all leaves have
183082** been populated and stored. This is Ok, as the b+-tree fanout is usually
183083** very large, meaning that the interior of the tree consumes relatively
183084** little memory.
183085*/
183086struct SegmentNode {
183087 SegmentNode *pParent; /* Parent node (or NULL for root node) */
183088 SegmentNode *pRight; /* Pointer to right-sibling */
183089 SegmentNode *pLeftmost; /* Pointer to left-most node of this depth */
183090 int nEntry; /* Number of terms written to node so far */
183091 char *zTerm; /* Pointer to previous term buffer */
183092 int nTerm; /* Number of bytes in zTerm */
183093 int nMalloc; /* Size of malloc'd buffer at zMalloc */
183094 char *zMalloc; /* Malloc'd space (possibly) used for zTerm */
183095 int nData; /* Bytes of valid data so far */
183096 char *aData; /* Node data */
183097};
183098
183099/*
183100** Valid values for the second argument to fts3SqlStmt().
183101*/
183102#define SQL_DELETE_CONTENT 0
183103#define SQL_IS_EMPTY 1
183104#define SQL_DELETE_ALL_CONTENT 2
183105#define SQL_DELETE_ALL_SEGMENTS 3
183106#define SQL_DELETE_ALL_SEGDIR 4
183107#define SQL_DELETE_ALL_DOCSIZE 5
183108#define SQL_DELETE_ALL_STAT 6
183109#define SQL_SELECT_CONTENT_BY_ROWID 7
183110#define SQL_NEXT_SEGMENT_INDEX 8
183111#define SQL_INSERT_SEGMENTS 9
183112#define SQL_NEXT_SEGMENTS_ID 10
183113#define SQL_INSERT_SEGDIR 11
183114#define SQL_SELECT_LEVEL 12
183115#define SQL_SELECT_LEVEL_RANGE 13
183116#define SQL_SELECT_LEVEL_COUNT 14
183117#define SQL_SELECT_SEGDIR_MAX_LEVEL 15
183118#define SQL_DELETE_SEGDIR_LEVEL 16
183119#define SQL_DELETE_SEGMENTS_RANGE 17
183120#define SQL_CONTENT_INSERT 18
183121#define SQL_DELETE_DOCSIZE 19
183122#define SQL_REPLACE_DOCSIZE 20
183123#define SQL_SELECT_DOCSIZE 21
183124#define SQL_SELECT_STAT 22
183125#define SQL_REPLACE_STAT 23
183126
183127#define SQL_SELECT_ALL_PREFIX_LEVEL 24
183128#define SQL_DELETE_ALL_TERMS_SEGDIR 25
183129#define SQL_DELETE_SEGDIR_RANGE 26
183130#define SQL_SELECT_ALL_LANGID 27
183131#define SQL_FIND_MERGE_LEVEL 28
183132#define SQL_MAX_LEAF_NODE_ESTIMATE 29
183133#define SQL_DELETE_SEGDIR_ENTRY 30
183134#define SQL_SHIFT_SEGDIR_ENTRY 31
183135#define SQL_SELECT_SEGDIR 32
183136#define SQL_CHOMP_SEGDIR 33
183137#define SQL_SEGMENT_IS_APPENDABLE 34
183138#define SQL_SELECT_INDEXES 35
183139#define SQL_SELECT_MXLEVEL 36
183140
183141#define SQL_SELECT_LEVEL_RANGE2 37
183142#define SQL_UPDATE_LEVEL_IDX 38
183143#define SQL_UPDATE_LEVEL 39
183144
183145/*
183146** This function is used to obtain an SQLite prepared statement handle
183147** for the statement identified by the second argument. If successful,
183148** *pp is set to the requested statement handle and SQLITE_OK returned.
183149** Otherwise, an SQLite error code is returned and *pp is set to 0.
183150**
183151** If argument apVal is not NULL, then it must point to an array with
183152** at least as many entries as the requested statement has bound
183153** parameters. The values are bound to the statements parameters before
183154** returning.
183155*/
183156static int fts3SqlStmt(
183157 Fts3Table *p, /* Virtual table handle */
183158 int eStmt, /* One of the SQL_XXX constants above */
183159 sqlite3_stmt **pp, /* OUT: Statement handle */
183160 sqlite3_value **apVal /* Values to bind to statement */
183161){
183162 const char *azSql[] = {
183163/* 0 */ "DELETE FROM %Q.'%q_content' WHERE rowid = ?",
183164/* 1 */ "SELECT NOT EXISTS(SELECT docid FROM %Q.'%q_content' WHERE rowid!=?)",
183165/* 2 */ "DELETE FROM %Q.'%q_content'",
183166/* 3 */ "DELETE FROM %Q.'%q_segments'",
183167/* 4 */ "DELETE FROM %Q.'%q_segdir'",
183168/* 5 */ "DELETE FROM %Q.'%q_docsize'",
183169/* 6 */ "DELETE FROM %Q.'%q_stat'",
183170/* 7 */ "SELECT %s WHERE rowid=?",
183171/* 8 */ "SELECT (SELECT max(idx) FROM %Q.'%q_segdir' WHERE level = ?) + 1",
183172/* 9 */ "REPLACE INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)",
183173/* 10 */ "SELECT coalesce((SELECT max(blockid) FROM %Q.'%q_segments') + 1, 1)",
183174/* 11 */ "REPLACE INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)",
183175
183176 /* Return segments in order from oldest to newest.*/
183177/* 12 */ "SELECT idx, start_block, leaves_end_block, end_block, root "
183178 "FROM %Q.'%q_segdir' WHERE level = ? ORDER BY idx ASC",
183179/* 13 */ "SELECT idx, start_block, leaves_end_block, end_block, root "
183180 "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?"
183181 "ORDER BY level DESC, idx ASC",
183182
183183/* 14 */ "SELECT count(*) FROM %Q.'%q_segdir' WHERE level = ?",
183184/* 15 */ "SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
183185
183186/* 16 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ?",
183187/* 17 */ "DELETE FROM %Q.'%q_segments' WHERE blockid BETWEEN ? AND ?",
183188/* 18 */ "INSERT INTO %Q.'%q_content' VALUES(%s)",
183189/* 19 */ "DELETE FROM %Q.'%q_docsize' WHERE docid = ?",
183190/* 20 */ "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)",
183191/* 21 */ "SELECT size FROM %Q.'%q_docsize' WHERE docid=?",
183192/* 22 */ "SELECT value FROM %Q.'%q_stat' WHERE id=?",
183193/* 23 */ "REPLACE INTO %Q.'%q_stat' VALUES(?,?)",
183194/* 24 */ "",
183195/* 25 */ "",
183196
183197/* 26 */ "DELETE FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
183198/* 27 */ "SELECT ? UNION SELECT level / (1024 * ?) FROM %Q.'%q_segdir'",
183199
183200/* This statement is used to determine which level to read the input from
183201** when performing an incremental merge. It returns the absolute level number
183202** of the oldest level in the db that contains at least ? segments. Or,
183203** if no level in the FTS index contains more than ? segments, the statement
183204** returns zero rows. */
183205/* 28 */ "SELECT level, count(*) AS cnt FROM %Q.'%q_segdir' "
183206 " GROUP BY level HAVING cnt>=?"
183207 " ORDER BY (level %% 1024) ASC, 2 DESC LIMIT 1",
183208
183209/* Estimate the upper limit on the number of leaf nodes in a new segment
183210** created by merging the oldest :2 segments from absolute level :1. See
183211** function sqlite3Fts3Incrmerge() for details. */
183212/* 29 */ "SELECT 2 * total(1 + leaves_end_block - start_block) "
183213 " FROM (SELECT * FROM %Q.'%q_segdir' "
183214 " WHERE level = ? ORDER BY idx ASC LIMIT ?"
183215 " )",
183216
183217/* SQL_DELETE_SEGDIR_ENTRY
183218** Delete the %_segdir entry on absolute level :1 with index :2. */
183219/* 30 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
183220
183221/* SQL_SHIFT_SEGDIR_ENTRY
183222** Modify the idx value for the segment with idx=:3 on absolute level :2
183223** to :1. */
183224/* 31 */ "UPDATE %Q.'%q_segdir' SET idx = ? WHERE level=? AND idx=?",
183225
183226/* SQL_SELECT_SEGDIR
183227** Read a single entry from the %_segdir table. The entry from absolute
183228** level :1 with index value :2. */
183229/* 32 */ "SELECT idx, start_block, leaves_end_block, end_block, root "
183230 "FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
183231
183232/* SQL_CHOMP_SEGDIR
183233** Update the start_block (:1) and root (:2) fields of the %_segdir
183234** entry located on absolute level :3 with index :4. */
183235/* 33 */ "UPDATE %Q.'%q_segdir' SET start_block = ?, root = ?"
183236 "WHERE level = ? AND idx = ?",
183237
183238/* SQL_SEGMENT_IS_APPENDABLE
183239** Return a single row if the segment with end_block=? is appendable. Or
183240** no rows otherwise. */
183241/* 34 */ "SELECT 1 FROM %Q.'%q_segments' WHERE blockid=? AND block IS NULL",
183242
183243/* SQL_SELECT_INDEXES
183244** Return the list of valid segment indexes for absolute level ? */
183245/* 35 */ "SELECT idx FROM %Q.'%q_segdir' WHERE level=? ORDER BY 1 ASC",
183246
183247/* SQL_SELECT_MXLEVEL
183248** Return the largest relative level in the FTS index or indexes. */
183249/* 36 */ "SELECT max( level %% 1024 ) FROM %Q.'%q_segdir'",
183250
183251 /* Return segments in order from oldest to newest.*/
183252/* 37 */ "SELECT level, idx, end_block "
183253 "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ? "
183254 "ORDER BY level DESC, idx ASC",
183255
183256 /* Update statements used while promoting segments */
183257/* 38 */ "UPDATE OR FAIL %Q.'%q_segdir' SET level=-1,idx=? "
183258 "WHERE level=? AND idx=?",
183259/* 39 */ "UPDATE OR FAIL %Q.'%q_segdir' SET level=? WHERE level=-1"
183260
183261 };
183262 int rc = SQLITE_OK;
183263 sqlite3_stmt *pStmt;
183264
183265 assert( SizeofArray(azSql)==SizeofArray(p->aStmt) );
183266 assert( eStmt<SizeofArray(azSql) && eStmt>=0 );
183267
183268 pStmt = p->aStmt[eStmt];
183269 if( !pStmt ){
183270 int f = SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_NO_VTAB;
183271 char *zSql;
183272 if( eStmt==SQL_CONTENT_INSERT ){
183273 zSql = sqlite3_mprintf(zFormat: azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist);
183274 }else if( eStmt==SQL_SELECT_CONTENT_BY_ROWID ){
183275 f &= ~SQLITE_PREPARE_NO_VTAB;
183276 zSql = sqlite3_mprintf(zFormat: azSql[eStmt], p->zReadExprlist);
183277 }else{
183278 zSql = sqlite3_mprintf(zFormat: azSql[eStmt], p->zDb, p->zName);
183279 }
183280 if( !zSql ){
183281 rc = SQLITE_NOMEM;
183282 }else{
183283 rc = sqlite3_prepare_v3(db: p->db, zSql, nBytes: -1, prepFlags: f, ppStmt: &pStmt, NULL);
183284 sqlite3_free(p: zSql);
183285 assert( rc==SQLITE_OK || pStmt==0 );
183286 p->aStmt[eStmt] = pStmt;
183287 }
183288 }
183289 if( apVal ){
183290 int i;
183291 int nParam = sqlite3_bind_parameter_count(pStmt);
183292 for(i=0; rc==SQLITE_OK && i<nParam; i++){
183293 rc = sqlite3_bind_value(pStmt, i: i+1, pValue: apVal[i]);
183294 }
183295 }
183296 *pp = pStmt;
183297 return rc;
183298}
183299
183300
183301static int fts3SelectDocsize(
183302 Fts3Table *pTab, /* FTS3 table handle */
183303 sqlite3_int64 iDocid, /* Docid to bind for SQL_SELECT_DOCSIZE */
183304 sqlite3_stmt **ppStmt /* OUT: Statement handle */
183305){
183306 sqlite3_stmt *pStmt = 0; /* Statement requested from fts3SqlStmt() */
183307 int rc; /* Return code */
183308
183309 rc = fts3SqlStmt(p: pTab, SQL_SELECT_DOCSIZE, pp: &pStmt, apVal: 0);
183310 if( rc==SQLITE_OK ){
183311 sqlite3_bind_int64(pStmt, i: 1, iValue: iDocid);
183312 rc = sqlite3_step(pStmt);
183313 if( rc!=SQLITE_ROW || sqlite3_column_type(pStmt, i: 0)!=SQLITE_BLOB ){
183314 rc = sqlite3_reset(pStmt);
183315 if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
183316 pStmt = 0;
183317 }else{
183318 rc = SQLITE_OK;
183319 }
183320 }
183321
183322 *ppStmt = pStmt;
183323 return rc;
183324}
183325
183326SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(
183327 Fts3Table *pTab, /* Fts3 table handle */
183328 sqlite3_stmt **ppStmt /* OUT: Statement handle */
183329){
183330 sqlite3_stmt *pStmt = 0;
183331 int rc;
183332 rc = fts3SqlStmt(p: pTab, SQL_SELECT_STAT, pp: &pStmt, apVal: 0);
183333 if( rc==SQLITE_OK ){
183334 sqlite3_bind_int(p: pStmt, i: 1, FTS_STAT_DOCTOTAL);
183335 if( sqlite3_step(pStmt)!=SQLITE_ROW
183336 || sqlite3_column_type(pStmt, i: 0)!=SQLITE_BLOB
183337 ){
183338 rc = sqlite3_reset(pStmt);
183339 if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
183340 pStmt = 0;
183341 }
183342 }
183343 *ppStmt = pStmt;
183344 return rc;
183345}
183346
183347SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(
183348 Fts3Table *pTab, /* Fts3 table handle */
183349 sqlite3_int64 iDocid, /* Docid to read size data for */
183350 sqlite3_stmt **ppStmt /* OUT: Statement handle */
183351){
183352 return fts3SelectDocsize(pTab, iDocid, ppStmt);
183353}
183354
183355/*
183356** Similar to fts3SqlStmt(). Except, after binding the parameters in
183357** array apVal[] to the SQL statement identified by eStmt, the statement
183358** is executed.
183359**
183360** Returns SQLITE_OK if the statement is successfully executed, or an
183361** SQLite error code otherwise.
183362*/
183363static void fts3SqlExec(
183364 int *pRC, /* Result code */
183365 Fts3Table *p, /* The FTS3 table */
183366 int eStmt, /* Index of statement to evaluate */
183367 sqlite3_value **apVal /* Parameters to bind */
183368){
183369 sqlite3_stmt *pStmt;
183370 int rc;
183371 if( *pRC ) return;
183372 rc = fts3SqlStmt(p, eStmt, pp: &pStmt, apVal);
183373 if( rc==SQLITE_OK ){
183374 sqlite3_step(pStmt);
183375 rc = sqlite3_reset(pStmt);
183376 }
183377 *pRC = rc;
183378}
183379
183380
183381/*
183382** This function ensures that the caller has obtained an exclusive
183383** shared-cache table-lock on the %_segdir table. This is required before
183384** writing data to the fts3 table. If this lock is not acquired first, then
183385** the caller may end up attempting to take this lock as part of committing
183386** a transaction, causing SQLite to return SQLITE_LOCKED or
183387** LOCKED_SHAREDCACHEto a COMMIT command.
183388**
183389** It is best to avoid this because if FTS3 returns any error when
183390** committing a transaction, the whole transaction will be rolled back.
183391** And this is not what users expect when they get SQLITE_LOCKED_SHAREDCACHE.
183392** It can still happen if the user locks the underlying tables directly
183393** instead of accessing them via FTS.
183394*/
183395static int fts3Writelock(Fts3Table *p){
183396 int rc = SQLITE_OK;
183397
183398 if( p->nPendingData==0 ){
183399 sqlite3_stmt *pStmt;
183400 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, pp: &pStmt, apVal: 0);
183401 if( rc==SQLITE_OK ){
183402 sqlite3_bind_null(pStmt, i: 1);
183403 sqlite3_step(pStmt);
183404 rc = sqlite3_reset(pStmt);
183405 }
183406 }
183407
183408 return rc;
183409}
183410
183411/*
183412** FTS maintains a separate indexes for each language-id (a 32-bit integer).
183413** Within each language id, a separate index is maintained to store the
183414** document terms, and each configured prefix size (configured the FTS
183415** "prefix=" option). And each index consists of multiple levels ("relative
183416** levels").
183417**
183418** All three of these values (the language id, the specific index and the
183419** level within the index) are encoded in 64-bit integer values stored
183420** in the %_segdir table on disk. This function is used to convert three
183421** separate component values into the single 64-bit integer value that
183422** can be used to query the %_segdir table.
183423**
183424** Specifically, each language-id/index combination is allocated 1024
183425** 64-bit integer level values ("absolute levels"). The main terms index
183426** for language-id 0 is allocate values 0-1023. The first prefix index
183427** (if any) for language-id 0 is allocated values 1024-2047. And so on.
183428** Language 1 indexes are allocated immediately following language 0.
183429**
183430** So, for a system with nPrefix prefix indexes configured, the block of
183431** absolute levels that corresponds to language-id iLangid and index
183432** iIndex starts at absolute level ((iLangid * (nPrefix+1) + iIndex) * 1024).
183433*/
183434static sqlite3_int64 getAbsoluteLevel(
183435 Fts3Table *p, /* FTS3 table handle */
183436 int iLangid, /* Language id */
183437 int iIndex, /* Index in p->aIndex[] */
183438 int iLevel /* Level of segments */
183439){
183440 sqlite3_int64 iBase; /* First absolute level for iLangid/iIndex */
183441 assert_fts3_nc( iLangid>=0 );
183442 assert( p->nIndex>0 );
183443 assert( iIndex>=0 && iIndex<p->nIndex );
183444
183445 iBase = ((sqlite3_int64)iLangid * p->nIndex + iIndex) * FTS3_SEGDIR_MAXLEVEL;
183446 return iBase + iLevel;
183447}
183448
183449/*
183450** Set *ppStmt to a statement handle that may be used to iterate through
183451** all rows in the %_segdir table, from oldest to newest. If successful,
183452** return SQLITE_OK. If an error occurs while preparing the statement,
183453** return an SQLite error code.
183454**
183455** There is only ever one instance of this SQL statement compiled for
183456** each FTS3 table.
183457**
183458** The statement returns the following columns from the %_segdir table:
183459**
183460** 0: idx
183461** 1: start_block
183462** 2: leaves_end_block
183463** 3: end_block
183464** 4: root
183465*/
183466SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(
183467 Fts3Table *p, /* FTS3 table */
183468 int iLangid, /* Language being queried */
183469 int iIndex, /* Index for p->aIndex[] */
183470 int iLevel, /* Level to select (relative level) */
183471 sqlite3_stmt **ppStmt /* OUT: Compiled statement */
183472){
183473 int rc;
183474 sqlite3_stmt *pStmt = 0;
183475
183476 assert( iLevel==FTS3_SEGCURSOR_ALL || iLevel>=0 );
183477 assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
183478 assert( iIndex>=0 && iIndex<p->nIndex );
183479
183480 if( iLevel<0 ){
183481 /* "SELECT * FROM %_segdir WHERE level BETWEEN ? AND ? ORDER BY ..." */
183482 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE, pp: &pStmt, apVal: 0);
183483 if( rc==SQLITE_OK ){
183484 sqlite3_bind_int64(pStmt, i: 1, iValue: getAbsoluteLevel(p, iLangid, iIndex, iLevel: 0));
183485 sqlite3_bind_int64(pStmt, i: 2,
183486 iValue: getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
183487 );
183488 }
183489 }else{
183490 /* "SELECT * FROM %_segdir WHERE level = ? ORDER BY ..." */
183491 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, pp: &pStmt, apVal: 0);
183492 if( rc==SQLITE_OK ){
183493 sqlite3_bind_int64(pStmt, i: 1, iValue: getAbsoluteLevel(p, iLangid, iIndex,iLevel));
183494 }
183495 }
183496 *ppStmt = pStmt;
183497 return rc;
183498}
183499
183500
183501/*
183502** Append a single varint to a PendingList buffer. SQLITE_OK is returned
183503** if successful, or an SQLite error code otherwise.
183504**
183505** This function also serves to allocate the PendingList structure itself.
183506** For example, to create a new PendingList structure containing two
183507** varints:
183508**
183509** PendingList *p = 0;
183510** fts3PendingListAppendVarint(&p, 1);
183511** fts3PendingListAppendVarint(&p, 2);
183512*/
183513static int fts3PendingListAppendVarint(
183514 PendingList **pp, /* IN/OUT: Pointer to PendingList struct */
183515 sqlite3_int64 i /* Value to append to data */
183516){
183517 PendingList *p = *pp;
183518
183519 /* Allocate or grow the PendingList as required. */
183520 if( !p ){
183521 p = sqlite3_malloc(n: sizeof(*p) + 100);
183522 if( !p ){
183523 return SQLITE_NOMEM;
183524 }
183525 p->nSpace = 100;
183526 p->aData = (char *)&p[1];
183527 p->nData = 0;
183528 }
183529 else if( p->nData+FTS3_VARINT_MAX+1>p->nSpace ){
183530 int nNew = p->nSpace * 2;
183531 p = sqlite3_realloc(pOld: p, n: sizeof(*p) + nNew);
183532 if( !p ){
183533 sqlite3_free(p: *pp);
183534 *pp = 0;
183535 return SQLITE_NOMEM;
183536 }
183537 p->nSpace = nNew;
183538 p->aData = (char *)&p[1];
183539 }
183540
183541 /* Append the new serialized varint to the end of the list. */
183542 p->nData += sqlite3Fts3PutVarint(p: &p->aData[p->nData], v: i);
183543 p->aData[p->nData] = '\0';
183544 *pp = p;
183545 return SQLITE_OK;
183546}
183547
183548/*
183549** Add a docid/column/position entry to a PendingList structure. Non-zero
183550** is returned if the structure is sqlite3_realloced as part of adding
183551** the entry. Otherwise, zero.
183552**
183553** If an OOM error occurs, *pRc is set to SQLITE_NOMEM before returning.
183554** Zero is always returned in this case. Otherwise, if no OOM error occurs,
183555** it is set to SQLITE_OK.
183556*/
183557static int fts3PendingListAppend(
183558 PendingList **pp, /* IN/OUT: PendingList structure */
183559 sqlite3_int64 iDocid, /* Docid for entry to add */
183560 sqlite3_int64 iCol, /* Column for entry to add */
183561 sqlite3_int64 iPos, /* Position of term for entry to add */
183562 int *pRc /* OUT: Return code */
183563){
183564 PendingList *p = *pp;
183565 int rc = SQLITE_OK;
183566
183567 assert( !p || p->iLastDocid<=iDocid );
183568
183569 if( !p || p->iLastDocid!=iDocid ){
183570 u64 iDelta = (u64)iDocid - (u64)(p ? p->iLastDocid : 0);
183571 if( p ){
183572 assert( p->nData<p->nSpace );
183573 assert( p->aData[p->nData]==0 );
183574 p->nData++;
183575 }
183576 if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(pp: &p, i: iDelta)) ){
183577 goto pendinglistappend_out;
183578 }
183579 p->iLastCol = -1;
183580 p->iLastPos = 0;
183581 p->iLastDocid = iDocid;
183582 }
183583 if( iCol>0 && p->iLastCol!=iCol ){
183584 if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(pp: &p, i: 1))
183585 || SQLITE_OK!=(rc = fts3PendingListAppendVarint(pp: &p, i: iCol))
183586 ){
183587 goto pendinglistappend_out;
183588 }
183589 p->iLastCol = iCol;
183590 p->iLastPos = 0;
183591 }
183592 if( iCol>=0 ){
183593 assert( iPos>p->iLastPos || (iPos==0 && p->iLastPos==0) );
183594 rc = fts3PendingListAppendVarint(pp: &p, i: 2+iPos-p->iLastPos);
183595 if( rc==SQLITE_OK ){
183596 p->iLastPos = iPos;
183597 }
183598 }
183599
183600 pendinglistappend_out:
183601 *pRc = rc;
183602 if( p!=*pp ){
183603 *pp = p;
183604 return 1;
183605 }
183606 return 0;
183607}
183608
183609/*
183610** Free a PendingList object allocated by fts3PendingListAppend().
183611*/
183612static void fts3PendingListDelete(PendingList *pList){
183613 sqlite3_free(p: pList);
183614}
183615
183616/*
183617** Add an entry to one of the pending-terms hash tables.
183618*/
183619static int fts3PendingTermsAddOne(
183620 Fts3Table *p,
183621 int iCol,
183622 int iPos,
183623 Fts3Hash *pHash, /* Pending terms hash table to add entry to */
183624 const char *zToken,
183625 int nToken
183626){
183627 PendingList *pList;
183628 int rc = SQLITE_OK;
183629
183630 pList = (PendingList *)fts3HashFind(pH: pHash, pKey: zToken, nKey: nToken);
183631 if( pList ){
183632 p->nPendingData -= (pList->nData + nToken + sizeof(Fts3HashElem));
183633 }
183634 if( fts3PendingListAppend(pp: &pList, iDocid: p->iPrevDocid, iCol, iPos, pRc: &rc) ){
183635 if( pList==fts3HashInsert(pH: pHash, pKey: zToken, nKey: nToken, data: pList) ){
183636 /* Malloc failed while inserting the new entry. This can only
183637 ** happen if there was no previous entry for this token.
183638 */
183639 assert( 0==fts3HashFind(pHash, zToken, nToken) );
183640 sqlite3_free(p: pList);
183641 rc = SQLITE_NOMEM;
183642 }
183643 }
183644 if( rc==SQLITE_OK ){
183645 p->nPendingData += (pList->nData + nToken + sizeof(Fts3HashElem));
183646 }
183647 return rc;
183648}
183649
183650/*
183651** Tokenize the nul-terminated string zText and add all tokens to the
183652** pending-terms hash-table. The docid used is that currently stored in
183653** p->iPrevDocid, and the column is specified by argument iCol.
183654**
183655** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
183656*/
183657static int fts3PendingTermsAdd(
183658 Fts3Table *p, /* Table into which text will be inserted */
183659 int iLangid, /* Language id to use */
183660 const char *zText, /* Text of document to be inserted */
183661 int iCol, /* Column into which text is being inserted */
183662 u32 *pnWord /* IN/OUT: Incr. by number tokens inserted */
183663){
183664 int rc;
183665 int iStart = 0;
183666 int iEnd = 0;
183667 int iPos = 0;
183668 int nWord = 0;
183669
183670 char const *zToken;
183671 int nToken = 0;
183672
183673 sqlite3_tokenizer *pTokenizer = p->pTokenizer;
183674 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
183675 sqlite3_tokenizer_cursor *pCsr;
183676 int (*xNext)(sqlite3_tokenizer_cursor *pCursor,
183677 const char**,int*,int*,int*,int*);
183678
183679 assert( pTokenizer && pModule );
183680
183681 /* If the user has inserted a NULL value, this function may be called with
183682 ** zText==0. In this case, add zero token entries to the hash table and
183683 ** return early. */
183684 if( zText==0 ){
183685 *pnWord = 0;
183686 return SQLITE_OK;
183687 }
183688
183689 rc = sqlite3Fts3OpenTokenizer(pTokenizer, iLangid, z: zText, n: -1, ppCsr: &pCsr);
183690 if( rc!=SQLITE_OK ){
183691 return rc;
183692 }
183693
183694 xNext = pModule->xNext;
183695 while( SQLITE_OK==rc
183696 && SQLITE_OK==(rc = xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos))
183697 ){
183698 int i;
183699 if( iPos>=nWord ) nWord = iPos+1;
183700
183701 /* Positions cannot be negative; we use -1 as a terminator internally.
183702 ** Tokens must have a non-zero length.
183703 */
183704 if( iPos<0 || !zToken || nToken<=0 ){
183705 rc = SQLITE_ERROR;
183706 break;
183707 }
183708
183709 /* Add the term to the terms index */
183710 rc = fts3PendingTermsAddOne(
183711 p, iCol, iPos, pHash: &p->aIndex[0].hPending, zToken, nToken
183712 );
183713
183714 /* Add the term to each of the prefix indexes that it is not too
183715 ** short for. */
183716 for(i=1; rc==SQLITE_OK && i<p->nIndex; i++){
183717 struct Fts3Index *pIndex = &p->aIndex[i];
183718 if( nToken<pIndex->nPrefix ) continue;
183719 rc = fts3PendingTermsAddOne(
183720 p, iCol, iPos, pHash: &pIndex->hPending, zToken, nToken: pIndex->nPrefix
183721 );
183722 }
183723 }
183724
183725 pModule->xClose(pCsr);
183726 *pnWord += nWord;
183727 return (rc==SQLITE_DONE ? SQLITE_OK : rc);
183728}
183729
183730/*
183731** Calling this function indicates that subsequent calls to
183732** fts3PendingTermsAdd() are to add term/position-list pairs for the
183733** contents of the document with docid iDocid.
183734*/
183735static int fts3PendingTermsDocid(
183736 Fts3Table *p, /* Full-text table handle */
183737 int bDelete, /* True if this op is a delete */
183738 int iLangid, /* Language id of row being written */
183739 sqlite_int64 iDocid /* Docid of row being written */
183740){
183741 assert( iLangid>=0 );
183742 assert( bDelete==1 || bDelete==0 );
183743
183744 /* TODO(shess) Explore whether partially flushing the buffer on
183745 ** forced-flush would provide better performance. I suspect that if
183746 ** we ordered the doclists by size and flushed the largest until the
183747 ** buffer was half empty, that would let the less frequent terms
183748 ** generate longer doclists.
183749 */
183750 if( iDocid<p->iPrevDocid
183751 || (iDocid==p->iPrevDocid && p->bPrevDelete==0)
183752 || p->iPrevLangid!=iLangid
183753 || p->nPendingData>p->nMaxPendingData
183754 ){
183755 int rc = sqlite3Fts3PendingTermsFlush(p);
183756 if( rc!=SQLITE_OK ) return rc;
183757 }
183758 p->iPrevDocid = iDocid;
183759 p->iPrevLangid = iLangid;
183760 p->bPrevDelete = bDelete;
183761 return SQLITE_OK;
183762}
183763
183764/*
183765** Discard the contents of the pending-terms hash tables.
183766*/
183767SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *p){
183768 int i;
183769 for(i=0; i<p->nIndex; i++){
183770 Fts3HashElem *pElem;
183771 Fts3Hash *pHash = &p->aIndex[i].hPending;
183772 for(pElem=fts3HashFirst(pHash); pElem; pElem=fts3HashNext(pElem)){
183773 PendingList *pList = (PendingList *)fts3HashData(pElem);
183774 fts3PendingListDelete(pList);
183775 }
183776 fts3HashClear(pH: pHash);
183777 }
183778 p->nPendingData = 0;
183779}
183780
183781/*
183782** This function is called by the xUpdate() method as part of an INSERT
183783** operation. It adds entries for each term in the new record to the
183784** pendingTerms hash table.
183785**
183786** Argument apVal is the same as the similarly named argument passed to
183787** fts3InsertData(). Parameter iDocid is the docid of the new row.
183788*/
183789static int fts3InsertTerms(
183790 Fts3Table *p,
183791 int iLangid,
183792 sqlite3_value **apVal,
183793 u32 *aSz
183794){
183795 int i; /* Iterator variable */
183796 for(i=2; i<p->nColumn+2; i++){
183797 int iCol = i-2;
183798 if( p->abNotindexed[iCol]==0 ){
183799 const char *zText = (const char *)sqlite3_value_text(pVal: apVal[i]);
183800 int rc = fts3PendingTermsAdd(p, iLangid, zText, iCol, pnWord: &aSz[iCol]);
183801 if( rc!=SQLITE_OK ){
183802 return rc;
183803 }
183804 aSz[p->nColumn] += sqlite3_value_bytes(pVal: apVal[i]);
183805 }
183806 }
183807 return SQLITE_OK;
183808}
183809
183810/*
183811** This function is called by the xUpdate() method for an INSERT operation.
183812** The apVal parameter is passed a copy of the apVal argument passed by
183813** SQLite to the xUpdate() method. i.e:
183814**
183815** apVal[0] Not used for INSERT.
183816** apVal[1] rowid
183817** apVal[2] Left-most user-defined column
183818** ...
183819** apVal[p->nColumn+1] Right-most user-defined column
183820** apVal[p->nColumn+2] Hidden column with same name as table
183821** apVal[p->nColumn+3] Hidden "docid" column (alias for rowid)
183822** apVal[p->nColumn+4] Hidden languageid column
183823*/
183824static int fts3InsertData(
183825 Fts3Table *p, /* Full-text table */
183826 sqlite3_value **apVal, /* Array of values to insert */
183827 sqlite3_int64 *piDocid /* OUT: Docid for row just inserted */
183828){
183829 int rc; /* Return code */
183830 sqlite3_stmt *pContentInsert; /* INSERT INTO %_content VALUES(...) */
183831
183832 if( p->zContentTbl ){
183833 sqlite3_value *pRowid = apVal[p->nColumn+3];
183834 if( sqlite3_value_type(pVal: pRowid)==SQLITE_NULL ){
183835 pRowid = apVal[1];
183836 }
183837 if( sqlite3_value_type(pVal: pRowid)!=SQLITE_INTEGER ){
183838 return SQLITE_CONSTRAINT;
183839 }
183840 *piDocid = sqlite3_value_int64(pVal: pRowid);
183841 return SQLITE_OK;
183842 }
183843
183844 /* Locate the statement handle used to insert data into the %_content
183845 ** table. The SQL for this statement is:
183846 **
183847 ** INSERT INTO %_content VALUES(?, ?, ?, ...)
183848 **
183849 ** The statement features N '?' variables, where N is the number of user
183850 ** defined columns in the FTS3 table, plus one for the docid field.
183851 */
183852 rc = fts3SqlStmt(p, SQL_CONTENT_INSERT, pp: &pContentInsert, apVal: &apVal[1]);
183853 if( rc==SQLITE_OK && p->zLanguageid ){
183854 rc = sqlite3_bind_int(
183855 p: pContentInsert, i: p->nColumn+2,
183856 iValue: sqlite3_value_int(pVal: apVal[p->nColumn+4])
183857 );
183858 }
183859 if( rc!=SQLITE_OK ) return rc;
183860
183861 /* There is a quirk here. The users INSERT statement may have specified
183862 ** a value for the "rowid" field, for the "docid" field, or for both.
183863 ** Which is a problem, since "rowid" and "docid" are aliases for the
183864 ** same value. For example:
183865 **
183866 ** INSERT INTO fts3tbl(rowid, docid) VALUES(1, 2);
183867 **
183868 ** In FTS3, this is an error. It is an error to specify non-NULL values
183869 ** for both docid and some other rowid alias.
183870 */
183871 if( SQLITE_NULL!=sqlite3_value_type(pVal: apVal[3+p->nColumn]) ){
183872 if( SQLITE_NULL==sqlite3_value_type(pVal: apVal[0])
183873 && SQLITE_NULL!=sqlite3_value_type(pVal: apVal[1])
183874 ){
183875 /* A rowid/docid conflict. */
183876 return SQLITE_ERROR;
183877 }
183878 rc = sqlite3_bind_value(pStmt: pContentInsert, i: 1, pValue: apVal[3+p->nColumn]);
183879 if( rc!=SQLITE_OK ) return rc;
183880 }
183881
183882 /* Execute the statement to insert the record. Set *piDocid to the
183883 ** new docid value.
183884 */
183885 sqlite3_step(pStmt: pContentInsert);
183886 rc = sqlite3_reset(pStmt: pContentInsert);
183887
183888 *piDocid = sqlite3_last_insert_rowid(db: p->db);
183889 return rc;
183890}
183891
183892
183893
183894/*
183895** Remove all data from the FTS3 table. Clear the hash table containing
183896** pending terms.
183897*/
183898static int fts3DeleteAll(Fts3Table *p, int bContent){
183899 int rc = SQLITE_OK; /* Return code */
183900
183901 /* Discard the contents of the pending-terms hash table. */
183902 sqlite3Fts3PendingTermsClear(p);
183903
183904 /* Delete everything from the shadow tables. Except, leave %_content as
183905 ** is if bContent is false. */
183906 assert( p->zContentTbl==0 || bContent==0 );
183907 if( bContent ) fts3SqlExec(pRC: &rc, p, SQL_DELETE_ALL_CONTENT, apVal: 0);
183908 fts3SqlExec(pRC: &rc, p, SQL_DELETE_ALL_SEGMENTS, apVal: 0);
183909 fts3SqlExec(pRC: &rc, p, SQL_DELETE_ALL_SEGDIR, apVal: 0);
183910 if( p->bHasDocsize ){
183911 fts3SqlExec(pRC: &rc, p, SQL_DELETE_ALL_DOCSIZE, apVal: 0);
183912 }
183913 if( p->bHasStat ){
183914 fts3SqlExec(pRC: &rc, p, SQL_DELETE_ALL_STAT, apVal: 0);
183915 }
183916 return rc;
183917}
183918
183919/*
183920**
183921*/
183922static int langidFromSelect(Fts3Table *p, sqlite3_stmt *pSelect){
183923 int iLangid = 0;
183924 if( p->zLanguageid ) iLangid = sqlite3_column_int(pStmt: pSelect, i: p->nColumn+1);
183925 return iLangid;
183926}
183927
183928/*
183929** The first element in the apVal[] array is assumed to contain the docid
183930** (an integer) of a row about to be deleted. Remove all terms from the
183931** full-text index.
183932*/
183933static void fts3DeleteTerms(
183934 int *pRC, /* Result code */
183935 Fts3Table *p, /* The FTS table to delete from */
183936 sqlite3_value *pRowid, /* The docid to be deleted */
183937 u32 *aSz, /* Sizes of deleted document written here */
183938 int *pbFound /* OUT: Set to true if row really does exist */
183939){
183940 int rc;
183941 sqlite3_stmt *pSelect;
183942
183943 assert( *pbFound==0 );
183944 if( *pRC ) return;
183945 rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, pp: &pSelect, apVal: &pRowid);
183946 if( rc==SQLITE_OK ){
183947 if( SQLITE_ROW==sqlite3_step(pStmt: pSelect) ){
183948 int i;
183949 int iLangid = langidFromSelect(p, pSelect);
183950 i64 iDocid = sqlite3_column_int64(pStmt: pSelect, i: 0);
183951 rc = fts3PendingTermsDocid(p, bDelete: 1, iLangid, iDocid);
183952 for(i=1; rc==SQLITE_OK && i<=p->nColumn; i++){
183953 int iCol = i-1;
183954 if( p->abNotindexed[iCol]==0 ){
183955 const char *zText = (const char *)sqlite3_column_text(pStmt: pSelect, i);
183956 rc = fts3PendingTermsAdd(p, iLangid, zText, iCol: -1, pnWord: &aSz[iCol]);
183957 aSz[p->nColumn] += sqlite3_column_bytes(pStmt: pSelect, i);
183958 }
183959 }
183960 if( rc!=SQLITE_OK ){
183961 sqlite3_reset(pStmt: pSelect);
183962 *pRC = rc;
183963 return;
183964 }
183965 *pbFound = 1;
183966 }
183967 rc = sqlite3_reset(pStmt: pSelect);
183968 }else{
183969 sqlite3_reset(pStmt: pSelect);
183970 }
183971 *pRC = rc;
183972}
183973
183974/*
183975** Forward declaration to account for the circular dependency between
183976** functions fts3SegmentMerge() and fts3AllocateSegdirIdx().
183977*/
183978static int fts3SegmentMerge(Fts3Table *, int, int, int);
183979
183980/*
183981** This function allocates a new level iLevel index in the segdir table.
183982** Usually, indexes are allocated within a level sequentially starting
183983** with 0, so the allocated index is one greater than the value returned
183984** by:
183985**
183986** SELECT max(idx) FROM %_segdir WHERE level = :iLevel
183987**
183988** However, if there are already FTS3_MERGE_COUNT indexes at the requested
183989** level, they are merged into a single level (iLevel+1) segment and the
183990** allocated index is 0.
183991**
183992** If successful, *piIdx is set to the allocated index slot and SQLITE_OK
183993** returned. Otherwise, an SQLite error code is returned.
183994*/
183995static int fts3AllocateSegdirIdx(
183996 Fts3Table *p,
183997 int iLangid, /* Language id */
183998 int iIndex, /* Index for p->aIndex */
183999 int iLevel,
184000 int *piIdx
184001){
184002 int rc; /* Return Code */
184003 sqlite3_stmt *pNextIdx; /* Query for next idx at level iLevel */
184004 int iNext = 0; /* Result of query pNextIdx */
184005
184006 assert( iLangid>=0 );
184007 assert( p->nIndex>=1 );
184008
184009 /* Set variable iNext to the next available segdir index at level iLevel. */
184010 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, pp: &pNextIdx, apVal: 0);
184011 if( rc==SQLITE_OK ){
184012 sqlite3_bind_int64(
184013 pStmt: pNextIdx, i: 1, iValue: getAbsoluteLevel(p, iLangid, iIndex, iLevel)
184014 );
184015 if( SQLITE_ROW==sqlite3_step(pStmt: pNextIdx) ){
184016 iNext = sqlite3_column_int(pStmt: pNextIdx, i: 0);
184017 }
184018 rc = sqlite3_reset(pStmt: pNextIdx);
184019 }
184020
184021 if( rc==SQLITE_OK ){
184022 /* If iNext is FTS3_MERGE_COUNT, indicating that level iLevel is already
184023 ** full, merge all segments in level iLevel into a single iLevel+1
184024 ** segment and allocate (newly freed) index 0 at level iLevel. Otherwise,
184025 ** if iNext is less than FTS3_MERGE_COUNT, allocate index iNext.
184026 */
184027 if( iNext>=MergeCount(p) ){
184028 fts3LogMerge(16, getAbsoluteLevel(p, iLangid, iIndex, iLevel));
184029 rc = fts3SegmentMerge(p, iLangid, iIndex, iLevel);
184030 *piIdx = 0;
184031 }else{
184032 *piIdx = iNext;
184033 }
184034 }
184035
184036 return rc;
184037}
184038
184039/*
184040** The %_segments table is declared as follows:
184041**
184042** CREATE TABLE %_segments(blockid INTEGER PRIMARY KEY, block BLOB)
184043**
184044** This function reads data from a single row of the %_segments table. The
184045** specific row is identified by the iBlockid parameter. If paBlob is not
184046** NULL, then a buffer is allocated using sqlite3_malloc() and populated
184047** with the contents of the blob stored in the "block" column of the
184048** identified table row is. Whether or not paBlob is NULL, *pnBlob is set
184049** to the size of the blob in bytes before returning.
184050**
184051** If an error occurs, or the table does not contain the specified row,
184052** an SQLite error code is returned. Otherwise, SQLITE_OK is returned. If
184053** paBlob is non-NULL, then it is the responsibility of the caller to
184054** eventually free the returned buffer.
184055**
184056** This function may leave an open sqlite3_blob* handle in the
184057** Fts3Table.pSegments variable. This handle is reused by subsequent calls
184058** to this function. The handle may be closed by calling the
184059** sqlite3Fts3SegmentsClose() function. Reusing a blob handle is a handy
184060** performance improvement, but the blob handle should always be closed
184061** before control is returned to the user (to prevent a lock being held
184062** on the database file for longer than necessary). Thus, any virtual table
184063** method (xFilter etc.) that may directly or indirectly call this function
184064** must call sqlite3Fts3SegmentsClose() before returning.
184065*/
184066SQLITE_PRIVATE int sqlite3Fts3ReadBlock(
184067 Fts3Table *p, /* FTS3 table handle */
184068 sqlite3_int64 iBlockid, /* Access the row with blockid=$iBlockid */
184069 char **paBlob, /* OUT: Blob data in malloc'd buffer */
184070 int *pnBlob, /* OUT: Size of blob data */
184071 int *pnLoad /* OUT: Bytes actually loaded */
184072){
184073 int rc; /* Return code */
184074
184075 /* pnBlob must be non-NULL. paBlob may be NULL or non-NULL. */
184076 assert( pnBlob );
184077
184078 if( p->pSegments ){
184079 rc = sqlite3_blob_reopen(pBlob: p->pSegments, iRow: iBlockid);
184080 }else{
184081 if( 0==p->zSegmentsTbl ){
184082 p->zSegmentsTbl = sqlite3_mprintf(zFormat: "%s_segments", p->zName);
184083 if( 0==p->zSegmentsTbl ) return SQLITE_NOMEM;
184084 }
184085 rc = sqlite3_blob_open(
184086 db: p->db, zDb: p->zDb, zTable: p->zSegmentsTbl, zColumn: "block", iRow: iBlockid, wrFlag: 0, ppBlob: &p->pSegments
184087 );
184088 }
184089
184090 if( rc==SQLITE_OK ){
184091 int nByte = sqlite3_blob_bytes(pBlob: p->pSegments);
184092 *pnBlob = nByte;
184093 if( paBlob ){
184094 char *aByte = sqlite3_malloc(n: nByte + FTS3_NODE_PADDING);
184095 if( !aByte ){
184096 rc = SQLITE_NOMEM;
184097 }else{
184098 if( pnLoad && nByte>(FTS3_NODE_CHUNK_THRESHOLD) ){
184099 nByte = FTS3_NODE_CHUNKSIZE;
184100 *pnLoad = nByte;
184101 }
184102 rc = sqlite3_blob_read(pBlob: p->pSegments, z: aByte, n: nByte, iOffset: 0);
184103 memset(s: &aByte[nByte], c: 0, FTS3_NODE_PADDING);
184104 if( rc!=SQLITE_OK ){
184105 sqlite3_free(p: aByte);
184106 aByte = 0;
184107 }
184108 }
184109 *paBlob = aByte;
184110 }
184111 }else if( rc==SQLITE_ERROR ){
184112 rc = FTS_CORRUPT_VTAB;
184113 }
184114
184115 return rc;
184116}
184117
184118/*
184119** Close the blob handle at p->pSegments, if it is open. See comments above
184120** the sqlite3Fts3ReadBlock() function for details.
184121*/
184122SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *p){
184123 sqlite3_blob_close(pBlob: p->pSegments);
184124 p->pSegments = 0;
184125}
184126
184127static int fts3SegReaderIncrRead(Fts3SegReader *pReader){
184128 int nRead; /* Number of bytes to read */
184129 int rc; /* Return code */
184130
184131 nRead = MIN(pReader->nNode - pReader->nPopulate, FTS3_NODE_CHUNKSIZE);
184132 rc = sqlite3_blob_read(
184133 pBlob: pReader->pBlob,
184134 z: &pReader->aNode[pReader->nPopulate],
184135 n: nRead,
184136 iOffset: pReader->nPopulate
184137 );
184138
184139 if( rc==SQLITE_OK ){
184140 pReader->nPopulate += nRead;
184141 memset(s: &pReader->aNode[pReader->nPopulate], c: 0, FTS3_NODE_PADDING);
184142 if( pReader->nPopulate==pReader->nNode ){
184143 sqlite3_blob_close(pBlob: pReader->pBlob);
184144 pReader->pBlob = 0;
184145 pReader->nPopulate = 0;
184146 }
184147 }
184148 return rc;
184149}
184150
184151static int fts3SegReaderRequire(Fts3SegReader *pReader, char *pFrom, int nByte){
184152 int rc = SQLITE_OK;
184153 assert( !pReader->pBlob
184154 || (pFrom>=pReader->aNode && pFrom<&pReader->aNode[pReader->nNode])
184155 );
184156 while( pReader->pBlob && rc==SQLITE_OK
184157 && (pFrom - pReader->aNode + nByte)>pReader->nPopulate
184158 ){
184159 rc = fts3SegReaderIncrRead(pReader);
184160 }
184161 return rc;
184162}
184163
184164/*
184165** Set an Fts3SegReader cursor to point at EOF.
184166*/
184167static void fts3SegReaderSetEof(Fts3SegReader *pSeg){
184168 if( !fts3SegReaderIsRootOnly(pSeg) ){
184169 sqlite3_free(p: pSeg->aNode);
184170 sqlite3_blob_close(pBlob: pSeg->pBlob);
184171 pSeg->pBlob = 0;
184172 }
184173 pSeg->aNode = 0;
184174}
184175
184176/*
184177** Move the iterator passed as the first argument to the next term in the
184178** segment. If successful, SQLITE_OK is returned. If there is no next term,
184179** SQLITE_DONE. Otherwise, an SQLite error code.
184180*/
184181static int fts3SegReaderNext(
184182 Fts3Table *p,
184183 Fts3SegReader *pReader,
184184 int bIncr
184185){
184186 int rc; /* Return code of various sub-routines */
184187 char *pNext; /* Cursor variable */
184188 int nPrefix; /* Number of bytes in term prefix */
184189 int nSuffix; /* Number of bytes in term suffix */
184190
184191 if( !pReader->aDoclist ){
184192 pNext = pReader->aNode;
184193 }else{
184194 pNext = &pReader->aDoclist[pReader->nDoclist];
184195 }
184196
184197 if( !pNext || pNext>=&pReader->aNode[pReader->nNode] ){
184198
184199 if( fts3SegReaderIsPending(pReader) ){
184200 Fts3HashElem *pElem = *(pReader->ppNextElem);
184201 sqlite3_free(p: pReader->aNode);
184202 pReader->aNode = 0;
184203 if( pElem ){
184204 char *aCopy;
184205 PendingList *pList = (PendingList *)fts3HashData(pElem);
184206 int nCopy = pList->nData+1;
184207
184208 int nTerm = fts3HashKeysize(pElem);
184209 if( (nTerm+1)>pReader->nTermAlloc ){
184210 sqlite3_free(p: pReader->zTerm);
184211 pReader->zTerm = (char*)sqlite3_malloc(n: (nTerm+1)*2);
184212 if( !pReader->zTerm ) return SQLITE_NOMEM;
184213 pReader->nTermAlloc = (nTerm+1)*2;
184214 }
184215 memcpy(dest: pReader->zTerm, fts3HashKey(pElem), n: nTerm);
184216 pReader->zTerm[nTerm] = '\0';
184217 pReader->nTerm = nTerm;
184218
184219 aCopy = (char*)sqlite3_malloc(n: nCopy);
184220 if( !aCopy ) return SQLITE_NOMEM;
184221 memcpy(dest: aCopy, src: pList->aData, n: nCopy);
184222 pReader->nNode = pReader->nDoclist = nCopy;
184223 pReader->aNode = pReader->aDoclist = aCopy;
184224 pReader->ppNextElem++;
184225 assert( pReader->aNode );
184226 }
184227 return SQLITE_OK;
184228 }
184229
184230 fts3SegReaderSetEof(pSeg: pReader);
184231
184232 /* If iCurrentBlock>=iLeafEndBlock, this is an EOF condition. All leaf
184233 ** blocks have already been traversed. */
184234#ifdef CORRUPT_DB
184235 assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock || CORRUPT_DB );
184236#endif
184237 if( pReader->iCurrentBlock>=pReader->iLeafEndBlock ){
184238 return SQLITE_OK;
184239 }
184240
184241 rc = sqlite3Fts3ReadBlock(
184242 p, iBlockid: ++pReader->iCurrentBlock, paBlob: &pReader->aNode, pnBlob: &pReader->nNode,
184243 pnLoad: (bIncr ? &pReader->nPopulate : 0)
184244 );
184245 if( rc!=SQLITE_OK ) return rc;
184246 assert( pReader->pBlob==0 );
184247 if( bIncr && pReader->nPopulate<pReader->nNode ){
184248 pReader->pBlob = p->pSegments;
184249 p->pSegments = 0;
184250 }
184251 pNext = pReader->aNode;
184252 }
184253
184254 assert( !fts3SegReaderIsPending(pReader) );
184255
184256 rc = fts3SegReaderRequire(pReader, pFrom: pNext, FTS3_VARINT_MAX*2);
184257 if( rc!=SQLITE_OK ) return rc;
184258
184259 /* Because of the FTS3_NODE_PADDING bytes of padding, the following is
184260 ** safe (no risk of overread) even if the node data is corrupted. */
184261 pNext += fts3GetVarint32(pNext, &nPrefix);
184262 pNext += fts3GetVarint32(pNext, &nSuffix);
184263 if( nSuffix<=0
184264 || (&pReader->aNode[pReader->nNode] - pNext)<nSuffix
184265 || nPrefix>pReader->nTerm
184266 ){
184267 return FTS_CORRUPT_VTAB;
184268 }
184269
184270 /* Both nPrefix and nSuffix were read by fts3GetVarint32() and so are
184271 ** between 0 and 0x7FFFFFFF. But the sum of the two may cause integer
184272 ** overflow - hence the (i64) casts. */
184273 if( (i64)nPrefix+nSuffix>(i64)pReader->nTermAlloc ){
184274 i64 nNew = ((i64)nPrefix+nSuffix)*2;
184275 char *zNew = sqlite3_realloc64(pOld: pReader->zTerm, n: nNew);
184276 if( !zNew ){
184277 return SQLITE_NOMEM;
184278 }
184279 pReader->zTerm = zNew;
184280 pReader->nTermAlloc = nNew;
184281 }
184282
184283 rc = fts3SegReaderRequire(pReader, pFrom: pNext, nByte: nSuffix+FTS3_VARINT_MAX);
184284 if( rc!=SQLITE_OK ) return rc;
184285
184286 memcpy(dest: &pReader->zTerm[nPrefix], src: pNext, n: nSuffix);
184287 pReader->nTerm = nPrefix+nSuffix;
184288 pNext += nSuffix;
184289 pNext += fts3GetVarint32(pNext, &pReader->nDoclist);
184290 pReader->aDoclist = pNext;
184291 pReader->pOffsetList = 0;
184292
184293 /* Check that the doclist does not appear to extend past the end of the
184294 ** b-tree node. And that the final byte of the doclist is 0x00. If either
184295 ** of these statements is untrue, then the data structure is corrupt.
184296 */
184297 if( pReader->nDoclist > pReader->nNode-(pReader->aDoclist-pReader->aNode)
184298 || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1])
184299 || pReader->nDoclist==0
184300 ){
184301 return FTS_CORRUPT_VTAB;
184302 }
184303 return SQLITE_OK;
184304}
184305
184306/*
184307** Set the SegReader to point to the first docid in the doclist associated
184308** with the current term.
184309*/
184310static int fts3SegReaderFirstDocid(Fts3Table *pTab, Fts3SegReader *pReader){
184311 int rc = SQLITE_OK;
184312 assert( pReader->aDoclist );
184313 assert( !pReader->pOffsetList );
184314 if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
184315 u8 bEof = 0;
184316 pReader->iDocid = 0;
184317 pReader->nOffsetList = 0;
184318 sqlite3Fts3DoclistPrev(bDescIdx: 0,
184319 aDoclist: pReader->aDoclist, nDoclist: pReader->nDoclist, ppIter: &pReader->pOffsetList,
184320 piDocid: &pReader->iDocid, pnList: &pReader->nOffsetList, pbEof: &bEof
184321 );
184322 }else{
184323 rc = fts3SegReaderRequire(pReader, pFrom: pReader->aDoclist, FTS3_VARINT_MAX);
184324 if( rc==SQLITE_OK ){
184325 int n = sqlite3Fts3GetVarint(pBuf: pReader->aDoclist, v: &pReader->iDocid);
184326 pReader->pOffsetList = &pReader->aDoclist[n];
184327 }
184328 }
184329 return rc;
184330}
184331
184332/*
184333** Advance the SegReader to point to the next docid in the doclist
184334** associated with the current term.
184335**
184336** If arguments ppOffsetList and pnOffsetList are not NULL, then
184337** *ppOffsetList is set to point to the first column-offset list
184338** in the doclist entry (i.e. immediately past the docid varint).
184339** *pnOffsetList is set to the length of the set of column-offset
184340** lists, not including the nul-terminator byte. For example:
184341*/
184342static int fts3SegReaderNextDocid(
184343 Fts3Table *pTab,
184344 Fts3SegReader *pReader, /* Reader to advance to next docid */
184345 char **ppOffsetList, /* OUT: Pointer to current position-list */
184346 int *pnOffsetList /* OUT: Length of *ppOffsetList in bytes */
184347){
184348 int rc = SQLITE_OK;
184349 char *p = pReader->pOffsetList;
184350 char c = 0;
184351
184352 assert( p );
184353
184354 if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
184355 /* A pending-terms seg-reader for an FTS4 table that uses order=desc.
184356 ** Pending-terms doclists are always built up in ascending order, so
184357 ** we have to iterate through them backwards here. */
184358 u8 bEof = 0;
184359 if( ppOffsetList ){
184360 *ppOffsetList = pReader->pOffsetList;
184361 *pnOffsetList = pReader->nOffsetList - 1;
184362 }
184363 sqlite3Fts3DoclistPrev(bDescIdx: 0,
184364 aDoclist: pReader->aDoclist, nDoclist: pReader->nDoclist, ppIter: &p, piDocid: &pReader->iDocid,
184365 pnList: &pReader->nOffsetList, pbEof: &bEof
184366 );
184367 if( bEof ){
184368 pReader->pOffsetList = 0;
184369 }else{
184370 pReader->pOffsetList = p;
184371 }
184372 }else{
184373 char *pEnd = &pReader->aDoclist[pReader->nDoclist];
184374
184375 /* Pointer p currently points at the first byte of an offset list. The
184376 ** following block advances it to point one byte past the end of
184377 ** the same offset list. */
184378 while( 1 ){
184379
184380 /* The following line of code (and the "p++" below the while() loop) is
184381 ** normally all that is required to move pointer p to the desired
184382 ** position. The exception is if this node is being loaded from disk
184383 ** incrementally and pointer "p" now points to the first byte past
184384 ** the populated part of pReader->aNode[].
184385 */
184386 while( *p | c ) c = *p++ & 0x80;
184387 assert( *p==0 );
184388
184389 if( pReader->pBlob==0 || p<&pReader->aNode[pReader->nPopulate] ) break;
184390 rc = fts3SegReaderIncrRead(pReader);
184391 if( rc!=SQLITE_OK ) return rc;
184392 }
184393 p++;
184394
184395 /* If required, populate the output variables with a pointer to and the
184396 ** size of the previous offset-list.
184397 */
184398 if( ppOffsetList ){
184399 *ppOffsetList = pReader->pOffsetList;
184400 *pnOffsetList = (int)(p - pReader->pOffsetList - 1);
184401 }
184402
184403 /* List may have been edited in place by fts3EvalNearTrim() */
184404 while( p<pEnd && *p==0 ) p++;
184405
184406 /* If there are no more entries in the doclist, set pOffsetList to
184407 ** NULL. Otherwise, set Fts3SegReader.iDocid to the next docid and
184408 ** Fts3SegReader.pOffsetList to point to the next offset list before
184409 ** returning.
184410 */
184411 if( p>=pEnd ){
184412 pReader->pOffsetList = 0;
184413 }else{
184414 rc = fts3SegReaderRequire(pReader, pFrom: p, FTS3_VARINT_MAX);
184415 if( rc==SQLITE_OK ){
184416 u64 iDelta;
184417 pReader->pOffsetList = p + sqlite3Fts3GetVarintU(pBuf: p, v: &iDelta);
184418 if( pTab->bDescIdx ){
184419 pReader->iDocid = (i64)((u64)pReader->iDocid - iDelta);
184420 }else{
184421 pReader->iDocid = (i64)((u64)pReader->iDocid + iDelta);
184422 }
184423 }
184424 }
184425 }
184426
184427 return rc;
184428}
184429
184430
184431SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(
184432 Fts3Cursor *pCsr,
184433 Fts3MultiSegReader *pMsr,
184434 int *pnOvfl
184435){
184436 Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
184437 int nOvfl = 0;
184438 int ii;
184439 int rc = SQLITE_OK;
184440 int pgsz = p->nPgsz;
184441
184442 assert( p->bFts4 );
184443 assert( pgsz>0 );
184444
184445 for(ii=0; rc==SQLITE_OK && ii<pMsr->nSegment; ii++){
184446 Fts3SegReader *pReader = pMsr->apSegment[ii];
184447 if( !fts3SegReaderIsPending(pReader)
184448 && !fts3SegReaderIsRootOnly(pReader)
184449 ){
184450 sqlite3_int64 jj;
184451 for(jj=pReader->iStartBlock; jj<=pReader->iLeafEndBlock; jj++){
184452 int nBlob;
184453 rc = sqlite3Fts3ReadBlock(p, iBlockid: jj, paBlob: 0, pnBlob: &nBlob, pnLoad: 0);
184454 if( rc!=SQLITE_OK ) break;
184455 if( (nBlob+35)>pgsz ){
184456 nOvfl += (nBlob + 34)/pgsz;
184457 }
184458 }
184459 }
184460 }
184461 *pnOvfl = nOvfl;
184462 return rc;
184463}
184464
184465/*
184466** Free all allocations associated with the iterator passed as the
184467** second argument.
184468*/
184469SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *pReader){
184470 if( pReader ){
184471 sqlite3_free(p: pReader->zTerm);
184472 if( !fts3SegReaderIsRootOnly(pReader) ){
184473 sqlite3_free(p: pReader->aNode);
184474 }
184475 sqlite3_blob_close(pBlob: pReader->pBlob);
184476 }
184477 sqlite3_free(p: pReader);
184478}
184479
184480/*
184481** Allocate a new SegReader object.
184482*/
184483SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(
184484 int iAge, /* Segment "age". */
184485 int bLookup, /* True for a lookup only */
184486 sqlite3_int64 iStartLeaf, /* First leaf to traverse */
184487 sqlite3_int64 iEndLeaf, /* Final leaf to traverse */
184488 sqlite3_int64 iEndBlock, /* Final block of segment */
184489 const char *zRoot, /* Buffer containing root node */
184490 int nRoot, /* Size of buffer containing root node */
184491 Fts3SegReader **ppReader /* OUT: Allocated Fts3SegReader */
184492){
184493 Fts3SegReader *pReader; /* Newly allocated SegReader object */
184494 int nExtra = 0; /* Bytes to allocate segment root node */
184495
184496 assert( zRoot!=0 || nRoot==0 );
184497#ifdef CORRUPT_DB
184498 assert( zRoot!=0 || CORRUPT_DB );
184499#endif
184500
184501 if( iStartLeaf==0 ){
184502 if( iEndLeaf!=0 ) return FTS_CORRUPT_VTAB;
184503 nExtra = nRoot + FTS3_NODE_PADDING;
184504 }
184505
184506 pReader = (Fts3SegReader *)sqlite3_malloc(n: sizeof(Fts3SegReader) + nExtra);
184507 if( !pReader ){
184508 return SQLITE_NOMEM;
184509 }
184510 memset(s: pReader, c: 0, n: sizeof(Fts3SegReader));
184511 pReader->iIdx = iAge;
184512 pReader->bLookup = bLookup!=0;
184513 pReader->iStartBlock = iStartLeaf;
184514 pReader->iLeafEndBlock = iEndLeaf;
184515 pReader->iEndBlock = iEndBlock;
184516
184517 if( nExtra ){
184518 /* The entire segment is stored in the root node. */
184519 pReader->aNode = (char *)&pReader[1];
184520 pReader->rootOnly = 1;
184521 pReader->nNode = nRoot;
184522 if( nRoot ) memcpy(dest: pReader->aNode, src: zRoot, n: nRoot);
184523 memset(s: &pReader->aNode[nRoot], c: 0, FTS3_NODE_PADDING);
184524 }else{
184525 pReader->iCurrentBlock = iStartLeaf-1;
184526 }
184527 *ppReader = pReader;
184528 return SQLITE_OK;
184529}
184530
184531/*
184532** This is a comparison function used as a qsort() callback when sorting
184533** an array of pending terms by term. This occurs as part of flushing
184534** the contents of the pending-terms hash table to the database.
184535*/
184536static int SQLITE_CDECL fts3CompareElemByTerm(
184537 const void *lhs,
184538 const void *rhs
184539){
184540 char *z1 = fts3HashKey(*(Fts3HashElem **)lhs);
184541 char *z2 = fts3HashKey(*(Fts3HashElem **)rhs);
184542 int n1 = fts3HashKeysize(*(Fts3HashElem **)lhs);
184543 int n2 = fts3HashKeysize(*(Fts3HashElem **)rhs);
184544
184545 int n = (n1<n2 ? n1 : n2);
184546 int c = memcmp(s1: z1, s2: z2, n: n);
184547 if( c==0 ){
184548 c = n1 - n2;
184549 }
184550 return c;
184551}
184552
184553/*
184554** This function is used to allocate an Fts3SegReader that iterates through
184555** a subset of the terms stored in the Fts3Table.pendingTerms array.
184556**
184557** If the isPrefixIter parameter is zero, then the returned SegReader iterates
184558** through each term in the pending-terms table. Or, if isPrefixIter is
184559** non-zero, it iterates through each term and its prefixes. For example, if
184560** the pending terms hash table contains the terms "sqlite", "mysql" and
184561** "firebird", then the iterator visits the following 'terms' (in the order
184562** shown):
184563**
184564** f fi fir fire fireb firebi firebir firebird
184565** m my mys mysq mysql
184566** s sq sql sqli sqlit sqlite
184567**
184568** Whereas if isPrefixIter is zero, the terms visited are:
184569**
184570** firebird mysql sqlite
184571*/
184572SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
184573 Fts3Table *p, /* Virtual table handle */
184574 int iIndex, /* Index for p->aIndex */
184575 const char *zTerm, /* Term to search for */
184576 int nTerm, /* Size of buffer zTerm */
184577 int bPrefix, /* True for a prefix iterator */
184578 Fts3SegReader **ppReader /* OUT: SegReader for pending-terms */
184579){
184580 Fts3SegReader *pReader = 0; /* Fts3SegReader object to return */
184581 Fts3HashElem *pE; /* Iterator variable */
184582 Fts3HashElem **aElem = 0; /* Array of term hash entries to scan */
184583 int nElem = 0; /* Size of array at aElem */
184584 int rc = SQLITE_OK; /* Return Code */
184585 Fts3Hash *pHash;
184586
184587 pHash = &p->aIndex[iIndex].hPending;
184588 if( bPrefix ){
184589 int nAlloc = 0; /* Size of allocated array at aElem */
184590
184591 for(pE=fts3HashFirst(pHash); pE; pE=fts3HashNext(pE)){
184592 char *zKey = (char *)fts3HashKey(pE);
184593 int nKey = fts3HashKeysize(pE);
184594 if( nTerm==0 || (nKey>=nTerm && 0==memcmp(s1: zKey, s2: zTerm, n: nTerm)) ){
184595 if( nElem==nAlloc ){
184596 Fts3HashElem **aElem2;
184597 nAlloc += 16;
184598 aElem2 = (Fts3HashElem **)sqlite3_realloc(
184599 pOld: aElem, n: nAlloc*sizeof(Fts3HashElem *)
184600 );
184601 if( !aElem2 ){
184602 rc = SQLITE_NOMEM;
184603 nElem = 0;
184604 break;
184605 }
184606 aElem = aElem2;
184607 }
184608
184609 aElem[nElem++] = pE;
184610 }
184611 }
184612
184613 /* If more than one term matches the prefix, sort the Fts3HashElem
184614 ** objects in term order using qsort(). This uses the same comparison
184615 ** callback as is used when flushing terms to disk.
184616 */
184617 if( nElem>1 ){
184618 qsort(base: aElem, nmemb: nElem, size: sizeof(Fts3HashElem *), compar: fts3CompareElemByTerm);
184619 }
184620
184621 }else{
184622 /* The query is a simple term lookup that matches at most one term in
184623 ** the index. All that is required is a straight hash-lookup.
184624 **
184625 ** Because the stack address of pE may be accessed via the aElem pointer
184626 ** below, the "Fts3HashElem *pE" must be declared so that it is valid
184627 ** within this entire function, not just this "else{...}" block.
184628 */
184629 pE = fts3HashFindElem(pH: pHash, pKey: zTerm, nKey: nTerm);
184630 if( pE ){
184631 aElem = &pE;
184632 nElem = 1;
184633 }
184634 }
184635
184636 if( nElem>0 ){
184637 sqlite3_int64 nByte;
184638 nByte = sizeof(Fts3SegReader) + (nElem+1)*sizeof(Fts3HashElem *);
184639 pReader = (Fts3SegReader *)sqlite3_malloc64(n: nByte);
184640 if( !pReader ){
184641 rc = SQLITE_NOMEM;
184642 }else{
184643 memset(s: pReader, c: 0, n: nByte);
184644 pReader->iIdx = 0x7FFFFFFF;
184645 pReader->ppNextElem = (Fts3HashElem **)&pReader[1];
184646 memcpy(dest: pReader->ppNextElem, src: aElem, n: nElem*sizeof(Fts3HashElem *));
184647 }
184648 }
184649
184650 if( bPrefix ){
184651 sqlite3_free(p: aElem);
184652 }
184653 *ppReader = pReader;
184654 return rc;
184655}
184656
184657/*
184658** Compare the entries pointed to by two Fts3SegReader structures.
184659** Comparison is as follows:
184660**
184661** 1) EOF is greater than not EOF.
184662**
184663** 2) The current terms (if any) are compared using memcmp(). If one
184664** term is a prefix of another, the longer term is considered the
184665** larger.
184666**
184667** 3) By segment age. An older segment is considered larger.
184668*/
184669static int fts3SegReaderCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
184670 int rc;
184671 if( pLhs->aNode && pRhs->aNode ){
184672 int rc2 = pLhs->nTerm - pRhs->nTerm;
184673 if( rc2<0 ){
184674 rc = memcmp(s1: pLhs->zTerm, s2: pRhs->zTerm, n: pLhs->nTerm);
184675 }else{
184676 rc = memcmp(s1: pLhs->zTerm, s2: pRhs->zTerm, n: pRhs->nTerm);
184677 }
184678 if( rc==0 ){
184679 rc = rc2;
184680 }
184681 }else{
184682 rc = (pLhs->aNode==0) - (pRhs->aNode==0);
184683 }
184684 if( rc==0 ){
184685 rc = pRhs->iIdx - pLhs->iIdx;
184686 }
184687 assert_fts3_nc( rc!=0 );
184688 return rc;
184689}
184690
184691/*
184692** A different comparison function for SegReader structures. In this
184693** version, it is assumed that each SegReader points to an entry in
184694** a doclist for identical terms. Comparison is made as follows:
184695**
184696** 1) EOF (end of doclist in this case) is greater than not EOF.
184697**
184698** 2) By current docid.
184699**
184700** 3) By segment age. An older segment is considered larger.
184701*/
184702static int fts3SegReaderDoclistCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
184703 int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
184704 if( rc==0 ){
184705 if( pLhs->iDocid==pRhs->iDocid ){
184706 rc = pRhs->iIdx - pLhs->iIdx;
184707 }else{
184708 rc = (pLhs->iDocid > pRhs->iDocid) ? 1 : -1;
184709 }
184710 }
184711 assert( pLhs->aNode && pRhs->aNode );
184712 return rc;
184713}
184714static int fts3SegReaderDoclistCmpRev(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
184715 int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
184716 if( rc==0 ){
184717 if( pLhs->iDocid==pRhs->iDocid ){
184718 rc = pRhs->iIdx - pLhs->iIdx;
184719 }else{
184720 rc = (pLhs->iDocid < pRhs->iDocid) ? 1 : -1;
184721 }
184722 }
184723 assert( pLhs->aNode && pRhs->aNode );
184724 return rc;
184725}
184726
184727/*
184728** Compare the term that the Fts3SegReader object passed as the first argument
184729** points to with the term specified by arguments zTerm and nTerm.
184730**
184731** If the pSeg iterator is already at EOF, return 0. Otherwise, return
184732** -ve if the pSeg term is less than zTerm/nTerm, 0 if the two terms are
184733** equal, or +ve if the pSeg term is greater than zTerm/nTerm.
184734*/
184735static int fts3SegReaderTermCmp(
184736 Fts3SegReader *pSeg, /* Segment reader object */
184737 const char *zTerm, /* Term to compare to */
184738 int nTerm /* Size of term zTerm in bytes */
184739){
184740 int res = 0;
184741 if( pSeg->aNode ){
184742 if( pSeg->nTerm>nTerm ){
184743 res = memcmp(s1: pSeg->zTerm, s2: zTerm, n: nTerm);
184744 }else{
184745 res = memcmp(s1: pSeg->zTerm, s2: zTerm, n: pSeg->nTerm);
184746 }
184747 if( res==0 ){
184748 res = pSeg->nTerm-nTerm;
184749 }
184750 }
184751 return res;
184752}
184753
184754/*
184755** Argument apSegment is an array of nSegment elements. It is known that
184756** the final (nSegment-nSuspect) members are already in sorted order
184757** (according to the comparison function provided). This function shuffles
184758** the array around until all entries are in sorted order.
184759*/
184760static void fts3SegReaderSort(
184761 Fts3SegReader **apSegment, /* Array to sort entries of */
184762 int nSegment, /* Size of apSegment array */
184763 int nSuspect, /* Unsorted entry count */
184764 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) /* Comparison function */
184765){
184766 int i; /* Iterator variable */
184767
184768 assert( nSuspect<=nSegment );
184769
184770 if( nSuspect==nSegment ) nSuspect--;
184771 for(i=nSuspect-1; i>=0; i--){
184772 int j;
184773 for(j=i; j<(nSegment-1); j++){
184774 Fts3SegReader *pTmp;
184775 if( xCmp(apSegment[j], apSegment[j+1])<0 ) break;
184776 pTmp = apSegment[j+1];
184777 apSegment[j+1] = apSegment[j];
184778 apSegment[j] = pTmp;
184779 }
184780 }
184781
184782#ifndef NDEBUG
184783 /* Check that the list really is sorted now. */
184784 for(i=0; i<(nSuspect-1); i++){
184785 assert( xCmp(apSegment[i], apSegment[i+1])<0 );
184786 }
184787#endif
184788}
184789
184790/*
184791** Insert a record into the %_segments table.
184792*/
184793static int fts3WriteSegment(
184794 Fts3Table *p, /* Virtual table handle */
184795 sqlite3_int64 iBlock, /* Block id for new block */
184796 char *z, /* Pointer to buffer containing block data */
184797 int n /* Size of buffer z in bytes */
184798){
184799 sqlite3_stmt *pStmt;
184800 int rc = fts3SqlStmt(p, SQL_INSERT_SEGMENTS, pp: &pStmt, apVal: 0);
184801 if( rc==SQLITE_OK ){
184802 sqlite3_bind_int64(pStmt, i: 1, iValue: iBlock);
184803 sqlite3_bind_blob(pStmt, i: 2, zData: z, nData: n, SQLITE_STATIC);
184804 sqlite3_step(pStmt);
184805 rc = sqlite3_reset(pStmt);
184806 sqlite3_bind_null(pStmt, i: 2);
184807 }
184808 return rc;
184809}
184810
184811/*
184812** Find the largest relative level number in the table. If successful, set
184813** *pnMax to this value and return SQLITE_OK. Otherwise, if an error occurs,
184814** set *pnMax to zero and return an SQLite error code.
184815*/
184816SQLITE_PRIVATE int sqlite3Fts3MaxLevel(Fts3Table *p, int *pnMax){
184817 int rc;
184818 int mxLevel = 0;
184819 sqlite3_stmt *pStmt = 0;
184820
184821 rc = fts3SqlStmt(p, SQL_SELECT_MXLEVEL, pp: &pStmt, apVal: 0);
184822 if( rc==SQLITE_OK ){
184823 if( SQLITE_ROW==sqlite3_step(pStmt) ){
184824 mxLevel = sqlite3_column_int(pStmt, i: 0);
184825 }
184826 rc = sqlite3_reset(pStmt);
184827 }
184828 *pnMax = mxLevel;
184829 return rc;
184830}
184831
184832/*
184833** Insert a record into the %_segdir table.
184834*/
184835static int fts3WriteSegdir(
184836 Fts3Table *p, /* Virtual table handle */
184837 sqlite3_int64 iLevel, /* Value for "level" field (absolute level) */
184838 int iIdx, /* Value for "idx" field */
184839 sqlite3_int64 iStartBlock, /* Value for "start_block" field */
184840 sqlite3_int64 iLeafEndBlock, /* Value for "leaves_end_block" field */
184841 sqlite3_int64 iEndBlock, /* Value for "end_block" field */
184842 sqlite3_int64 nLeafData, /* Bytes of leaf data in segment */
184843 char *zRoot, /* Blob value for "root" field */
184844 int nRoot /* Number of bytes in buffer zRoot */
184845){
184846 sqlite3_stmt *pStmt;
184847 int rc = fts3SqlStmt(p, SQL_INSERT_SEGDIR, pp: &pStmt, apVal: 0);
184848 if( rc==SQLITE_OK ){
184849 sqlite3_bind_int64(pStmt, i: 1, iValue: iLevel);
184850 sqlite3_bind_int(p: pStmt, i: 2, iValue: iIdx);
184851 sqlite3_bind_int64(pStmt, i: 3, iValue: iStartBlock);
184852 sqlite3_bind_int64(pStmt, i: 4, iValue: iLeafEndBlock);
184853 if( nLeafData==0 ){
184854 sqlite3_bind_int64(pStmt, i: 5, iValue: iEndBlock);
184855 }else{
184856 char *zEnd = sqlite3_mprintf(zFormat: "%lld %lld", iEndBlock, nLeafData);
184857 if( !zEnd ) return SQLITE_NOMEM;
184858 sqlite3_bind_text(pStmt, i: 5, zData: zEnd, nData: -1, xDel: sqlite3_free);
184859 }
184860 sqlite3_bind_blob(pStmt, i: 6, zData: zRoot, nData: nRoot, SQLITE_STATIC);
184861 sqlite3_step(pStmt);
184862 rc = sqlite3_reset(pStmt);
184863 sqlite3_bind_null(pStmt, i: 6);
184864 }
184865 return rc;
184866}
184867
184868/*
184869** Return the size of the common prefix (if any) shared by zPrev and
184870** zNext, in bytes. For example,
184871**
184872** fts3PrefixCompress("abc", 3, "abcdef", 6) // returns 3
184873** fts3PrefixCompress("abX", 3, "abcdef", 6) // returns 2
184874** fts3PrefixCompress("abX", 3, "Xbcdef", 6) // returns 0
184875*/
184876static int fts3PrefixCompress(
184877 const char *zPrev, /* Buffer containing previous term */
184878 int nPrev, /* Size of buffer zPrev in bytes */
184879 const char *zNext, /* Buffer containing next term */
184880 int nNext /* Size of buffer zNext in bytes */
184881){
184882 int n;
184883 for(n=0; n<nPrev && n<nNext && zPrev[n]==zNext[n]; n++);
184884 assert_fts3_nc( n<nNext );
184885 return n;
184886}
184887
184888/*
184889** Add term zTerm to the SegmentNode. It is guaranteed that zTerm is larger
184890** (according to memcmp) than the previous term.
184891*/
184892static int fts3NodeAddTerm(
184893 Fts3Table *p, /* Virtual table handle */
184894 SegmentNode **ppTree, /* IN/OUT: SegmentNode handle */
184895 int isCopyTerm, /* True if zTerm/nTerm is transient */
184896 const char *zTerm, /* Pointer to buffer containing term */
184897 int nTerm /* Size of term in bytes */
184898){
184899 SegmentNode *pTree = *ppTree;
184900 int rc;
184901 SegmentNode *pNew;
184902
184903 /* First try to append the term to the current node. Return early if
184904 ** this is possible.
184905 */
184906 if( pTree ){
184907 int nData = pTree->nData; /* Current size of node in bytes */
184908 int nReq = nData; /* Required space after adding zTerm */
184909 int nPrefix; /* Number of bytes of prefix compression */
184910 int nSuffix; /* Suffix length */
184911
184912 nPrefix = fts3PrefixCompress(zPrev: pTree->zTerm, nPrev: pTree->nTerm, zNext: zTerm, nNext: nTerm);
184913 nSuffix = nTerm-nPrefix;
184914
184915 /* If nSuffix is zero or less, then zTerm/nTerm must be a prefix of
184916 ** pWriter->zTerm/pWriter->nTerm. i.e. must be equal to or less than when
184917 ** compared with BINARY collation. This indicates corruption. */
184918 if( nSuffix<=0 ) return FTS_CORRUPT_VTAB;
184919
184920 nReq += sqlite3Fts3VarintLen(v: nPrefix)+sqlite3Fts3VarintLen(v: nSuffix)+nSuffix;
184921 if( nReq<=p->nNodeSize || !pTree->zTerm ){
184922
184923 if( nReq>p->nNodeSize ){
184924 /* An unusual case: this is the first term to be added to the node
184925 ** and the static node buffer (p->nNodeSize bytes) is not large
184926 ** enough. Use a separately malloced buffer instead This wastes
184927 ** p->nNodeSize bytes, but since this scenario only comes about when
184928 ** the database contain two terms that share a prefix of almost 2KB,
184929 ** this is not expected to be a serious problem.
184930 */
184931 assert( pTree->aData==(char *)&pTree[1] );
184932 pTree->aData = (char *)sqlite3_malloc(n: nReq);
184933 if( !pTree->aData ){
184934 return SQLITE_NOMEM;
184935 }
184936 }
184937
184938 if( pTree->zTerm ){
184939 /* There is no prefix-length field for first term in a node */
184940 nData += sqlite3Fts3PutVarint(p: &pTree->aData[nData], v: nPrefix);
184941 }
184942
184943 nData += sqlite3Fts3PutVarint(p: &pTree->aData[nData], v: nSuffix);
184944 memcpy(dest: &pTree->aData[nData], src: &zTerm[nPrefix], n: nSuffix);
184945 pTree->nData = nData + nSuffix;
184946 pTree->nEntry++;
184947
184948 if( isCopyTerm ){
184949 if( pTree->nMalloc<nTerm ){
184950 char *zNew = sqlite3_realloc(pOld: pTree->zMalloc, n: nTerm*2);
184951 if( !zNew ){
184952 return SQLITE_NOMEM;
184953 }
184954 pTree->nMalloc = nTerm*2;
184955 pTree->zMalloc = zNew;
184956 }
184957 pTree->zTerm = pTree->zMalloc;
184958 memcpy(dest: pTree->zTerm, src: zTerm, n: nTerm);
184959 pTree->nTerm = nTerm;
184960 }else{
184961 pTree->zTerm = (char *)zTerm;
184962 pTree->nTerm = nTerm;
184963 }
184964 return SQLITE_OK;
184965 }
184966 }
184967
184968 /* If control flows to here, it was not possible to append zTerm to the
184969 ** current node. Create a new node (a right-sibling of the current node).
184970 ** If this is the first node in the tree, the term is added to it.
184971 **
184972 ** Otherwise, the term is not added to the new node, it is left empty for
184973 ** now. Instead, the term is inserted into the parent of pTree. If pTree
184974 ** has no parent, one is created here.
184975 */
184976 pNew = (SegmentNode *)sqlite3_malloc(n: sizeof(SegmentNode) + p->nNodeSize);
184977 if( !pNew ){
184978 return SQLITE_NOMEM;
184979 }
184980 memset(s: pNew, c: 0, n: sizeof(SegmentNode));
184981 pNew->nData = 1 + FTS3_VARINT_MAX;
184982 pNew->aData = (char *)&pNew[1];
184983
184984 if( pTree ){
184985 SegmentNode *pParent = pTree->pParent;
184986 rc = fts3NodeAddTerm(p, ppTree: &pParent, isCopyTerm, zTerm, nTerm);
184987 if( pTree->pParent==0 ){
184988 pTree->pParent = pParent;
184989 }
184990 pTree->pRight = pNew;
184991 pNew->pLeftmost = pTree->pLeftmost;
184992 pNew->pParent = pParent;
184993 pNew->zMalloc = pTree->zMalloc;
184994 pNew->nMalloc = pTree->nMalloc;
184995 pTree->zMalloc = 0;
184996 }else{
184997 pNew->pLeftmost = pNew;
184998 rc = fts3NodeAddTerm(p, ppTree: &pNew, isCopyTerm, zTerm, nTerm);
184999 }
185000
185001 *ppTree = pNew;
185002 return rc;
185003}
185004
185005/*
185006** Helper function for fts3NodeWrite().
185007*/
185008static int fts3TreeFinishNode(
185009 SegmentNode *pTree,
185010 int iHeight,
185011 sqlite3_int64 iLeftChild
185012){
185013 int nStart;
185014 assert( iHeight>=1 && iHeight<128 );
185015 nStart = FTS3_VARINT_MAX - sqlite3Fts3VarintLen(v: iLeftChild);
185016 pTree->aData[nStart] = (char)iHeight;
185017 sqlite3Fts3PutVarint(p: &pTree->aData[nStart+1], v: iLeftChild);
185018 return nStart;
185019}
185020
185021/*
185022** Write the buffer for the segment node pTree and all of its peers to the
185023** database. Then call this function recursively to write the parent of
185024** pTree and its peers to the database.
185025**
185026** Except, if pTree is a root node, do not write it to the database. Instead,
185027** set output variables *paRoot and *pnRoot to contain the root node.
185028**
185029** If successful, SQLITE_OK is returned and output variable *piLast is
185030** set to the largest blockid written to the database (or zero if no
185031** blocks were written to the db). Otherwise, an SQLite error code is
185032** returned.
185033*/
185034static int fts3NodeWrite(
185035 Fts3Table *p, /* Virtual table handle */
185036 SegmentNode *pTree, /* SegmentNode handle */
185037 int iHeight, /* Height of this node in tree */
185038 sqlite3_int64 iLeaf, /* Block id of first leaf node */
185039 sqlite3_int64 iFree, /* Block id of next free slot in %_segments */
185040 sqlite3_int64 *piLast, /* OUT: Block id of last entry written */
185041 char **paRoot, /* OUT: Data for root node */
185042 int *pnRoot /* OUT: Size of root node in bytes */
185043){
185044 int rc = SQLITE_OK;
185045
185046 if( !pTree->pParent ){
185047 /* Root node of the tree. */
185048 int nStart = fts3TreeFinishNode(pTree, iHeight, iLeftChild: iLeaf);
185049 *piLast = iFree-1;
185050 *pnRoot = pTree->nData - nStart;
185051 *paRoot = &pTree->aData[nStart];
185052 }else{
185053 SegmentNode *pIter;
185054 sqlite3_int64 iNextFree = iFree;
185055 sqlite3_int64 iNextLeaf = iLeaf;
185056 for(pIter=pTree->pLeftmost; pIter && rc==SQLITE_OK; pIter=pIter->pRight){
185057 int nStart = fts3TreeFinishNode(pTree: pIter, iHeight, iLeftChild: iNextLeaf);
185058 int nWrite = pIter->nData - nStart;
185059
185060 rc = fts3WriteSegment(p, iBlock: iNextFree, z: &pIter->aData[nStart], n: nWrite);
185061 iNextFree++;
185062 iNextLeaf += (pIter->nEntry+1);
185063 }
185064 if( rc==SQLITE_OK ){
185065 assert( iNextLeaf==iFree );
185066 rc = fts3NodeWrite(
185067 p, pTree: pTree->pParent, iHeight: iHeight+1, iLeaf: iFree, iFree: iNextFree, piLast, paRoot, pnRoot
185068 );
185069 }
185070 }
185071
185072 return rc;
185073}
185074
185075/*
185076** Free all memory allocations associated with the tree pTree.
185077*/
185078static void fts3NodeFree(SegmentNode *pTree){
185079 if( pTree ){
185080 SegmentNode *p = pTree->pLeftmost;
185081 fts3NodeFree(pTree: p->pParent);
185082 while( p ){
185083 SegmentNode *pRight = p->pRight;
185084 if( p->aData!=(char *)&p[1] ){
185085 sqlite3_free(p: p->aData);
185086 }
185087 assert( pRight==0 || p->zMalloc==0 );
185088 sqlite3_free(p: p->zMalloc);
185089 sqlite3_free(p);
185090 p = pRight;
185091 }
185092 }
185093}
185094
185095/*
185096** Add a term to the segment being constructed by the SegmentWriter object
185097** *ppWriter. When adding the first term to a segment, *ppWriter should
185098** be passed NULL. This function will allocate a new SegmentWriter object
185099** and return it via the input/output variable *ppWriter in this case.
185100**
185101** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
185102*/
185103static int fts3SegWriterAdd(
185104 Fts3Table *p, /* Virtual table handle */
185105 SegmentWriter **ppWriter, /* IN/OUT: SegmentWriter handle */
185106 int isCopyTerm, /* True if buffer zTerm must be copied */
185107 const char *zTerm, /* Pointer to buffer containing term */
185108 int nTerm, /* Size of term in bytes */
185109 const char *aDoclist, /* Pointer to buffer containing doclist */
185110 int nDoclist /* Size of doclist in bytes */
185111){
185112 int nPrefix; /* Size of term prefix in bytes */
185113 int nSuffix; /* Size of term suffix in bytes */
185114 int nReq; /* Number of bytes required on leaf page */
185115 int nData;
185116 SegmentWriter *pWriter = *ppWriter;
185117
185118 if( !pWriter ){
185119 int rc;
185120 sqlite3_stmt *pStmt;
185121
185122 /* Allocate the SegmentWriter structure */
185123 pWriter = (SegmentWriter *)sqlite3_malloc(n: sizeof(SegmentWriter));
185124 if( !pWriter ) return SQLITE_NOMEM;
185125 memset(s: pWriter, c: 0, n: sizeof(SegmentWriter));
185126 *ppWriter = pWriter;
185127
185128 /* Allocate a buffer in which to accumulate data */
185129 pWriter->aData = (char *)sqlite3_malloc(n: p->nNodeSize);
185130 if( !pWriter->aData ) return SQLITE_NOMEM;
185131 pWriter->nSize = p->nNodeSize;
185132
185133 /* Find the next free blockid in the %_segments table */
185134 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, pp: &pStmt, apVal: 0);
185135 if( rc!=SQLITE_OK ) return rc;
185136 if( SQLITE_ROW==sqlite3_step(pStmt) ){
185137 pWriter->iFree = sqlite3_column_int64(pStmt, i: 0);
185138 pWriter->iFirst = pWriter->iFree;
185139 }
185140 rc = sqlite3_reset(pStmt);
185141 if( rc!=SQLITE_OK ) return rc;
185142 }
185143 nData = pWriter->nData;
185144
185145 nPrefix = fts3PrefixCompress(zPrev: pWriter->zTerm, nPrev: pWriter->nTerm, zNext: zTerm, nNext: nTerm);
185146 nSuffix = nTerm-nPrefix;
185147
185148 /* If nSuffix is zero or less, then zTerm/nTerm must be a prefix of
185149 ** pWriter->zTerm/pWriter->nTerm. i.e. must be equal to or less than when
185150 ** compared with BINARY collation. This indicates corruption. */
185151 if( nSuffix<=0 ) return FTS_CORRUPT_VTAB;
185152
185153 /* Figure out how many bytes are required by this new entry */
185154 nReq = sqlite3Fts3VarintLen(v: nPrefix) + /* varint containing prefix size */
185155 sqlite3Fts3VarintLen(v: nSuffix) + /* varint containing suffix size */
185156 nSuffix + /* Term suffix */
185157 sqlite3Fts3VarintLen(v: nDoclist) + /* Size of doclist */
185158 nDoclist; /* Doclist data */
185159
185160 if( nData>0 && nData+nReq>p->nNodeSize ){
185161 int rc;
185162
185163 /* The current leaf node is full. Write it out to the database. */
185164 if( pWriter->iFree==LARGEST_INT64 ) return FTS_CORRUPT_VTAB;
185165 rc = fts3WriteSegment(p, iBlock: pWriter->iFree++, z: pWriter->aData, n: nData);
185166 if( rc!=SQLITE_OK ) return rc;
185167 p->nLeafAdd++;
185168
185169 /* Add the current term to the interior node tree. The term added to
185170 ** the interior tree must:
185171 **
185172 ** a) be greater than the largest term on the leaf node just written
185173 ** to the database (still available in pWriter->zTerm), and
185174 **
185175 ** b) be less than or equal to the term about to be added to the new
185176 ** leaf node (zTerm/nTerm).
185177 **
185178 ** In other words, it must be the prefix of zTerm 1 byte longer than
185179 ** the common prefix (if any) of zTerm and pWriter->zTerm.
185180 */
185181 assert( nPrefix<nTerm );
185182 rc = fts3NodeAddTerm(p, ppTree: &pWriter->pTree, isCopyTerm, zTerm, nTerm: nPrefix+1);
185183 if( rc!=SQLITE_OK ) return rc;
185184
185185 nData = 0;
185186 pWriter->nTerm = 0;
185187
185188 nPrefix = 0;
185189 nSuffix = nTerm;
185190 nReq = 1 + /* varint containing prefix size */
185191 sqlite3Fts3VarintLen(v: nTerm) + /* varint containing suffix size */
185192 nTerm + /* Term suffix */
185193 sqlite3Fts3VarintLen(v: nDoclist) + /* Size of doclist */
185194 nDoclist; /* Doclist data */
185195 }
185196
185197 /* Increase the total number of bytes written to account for the new entry. */
185198 pWriter->nLeafData += nReq;
185199
185200 /* If the buffer currently allocated is too small for this entry, realloc
185201 ** the buffer to make it large enough.
185202 */
185203 if( nReq>pWriter->nSize ){
185204 char *aNew = sqlite3_realloc(pOld: pWriter->aData, n: nReq);
185205 if( !aNew ) return SQLITE_NOMEM;
185206 pWriter->aData = aNew;
185207 pWriter->nSize = nReq;
185208 }
185209 assert( nData+nReq<=pWriter->nSize );
185210
185211 /* Append the prefix-compressed term and doclist to the buffer. */
185212 nData += sqlite3Fts3PutVarint(p: &pWriter->aData[nData], v: nPrefix);
185213 nData += sqlite3Fts3PutVarint(p: &pWriter->aData[nData], v: nSuffix);
185214 assert( nSuffix>0 );
185215 memcpy(dest: &pWriter->aData[nData], src: &zTerm[nPrefix], n: nSuffix);
185216 nData += nSuffix;
185217 nData += sqlite3Fts3PutVarint(p: &pWriter->aData[nData], v: nDoclist);
185218 assert( nDoclist>0 );
185219 memcpy(dest: &pWriter->aData[nData], src: aDoclist, n: nDoclist);
185220 pWriter->nData = nData + nDoclist;
185221
185222 /* Save the current term so that it can be used to prefix-compress the next.
185223 ** If the isCopyTerm parameter is true, then the buffer pointed to by
185224 ** zTerm is transient, so take a copy of the term data. Otherwise, just
185225 ** store a copy of the pointer.
185226 */
185227 if( isCopyTerm ){
185228 if( nTerm>pWriter->nMalloc ){
185229 char *zNew = sqlite3_realloc(pOld: pWriter->zMalloc, n: nTerm*2);
185230 if( !zNew ){
185231 return SQLITE_NOMEM;
185232 }
185233 pWriter->nMalloc = nTerm*2;
185234 pWriter->zMalloc = zNew;
185235 pWriter->zTerm = zNew;
185236 }
185237 assert( pWriter->zTerm==pWriter->zMalloc );
185238 assert( nTerm>0 );
185239 memcpy(dest: pWriter->zTerm, src: zTerm, n: nTerm);
185240 }else{
185241 pWriter->zTerm = (char *)zTerm;
185242 }
185243 pWriter->nTerm = nTerm;
185244
185245 return SQLITE_OK;
185246}
185247
185248/*
185249** Flush all data associated with the SegmentWriter object pWriter to the
185250** database. This function must be called after all terms have been added
185251** to the segment using fts3SegWriterAdd(). If successful, SQLITE_OK is
185252** returned. Otherwise, an SQLite error code.
185253*/
185254static int fts3SegWriterFlush(
185255 Fts3Table *p, /* Virtual table handle */
185256 SegmentWriter *pWriter, /* SegmentWriter to flush to the db */
185257 sqlite3_int64 iLevel, /* Value for 'level' column of %_segdir */
185258 int iIdx /* Value for 'idx' column of %_segdir */
185259){
185260 int rc; /* Return code */
185261 if( pWriter->pTree ){
185262 sqlite3_int64 iLast = 0; /* Largest block id written to database */
185263 sqlite3_int64 iLastLeaf; /* Largest leaf block id written to db */
185264 char *zRoot = NULL; /* Pointer to buffer containing root node */
185265 int nRoot = 0; /* Size of buffer zRoot */
185266
185267 iLastLeaf = pWriter->iFree;
185268 rc = fts3WriteSegment(p, iBlock: pWriter->iFree++, z: pWriter->aData, n: pWriter->nData);
185269 if( rc==SQLITE_OK ){
185270 rc = fts3NodeWrite(p, pTree: pWriter->pTree, iHeight: 1,
185271 iLeaf: pWriter->iFirst, iFree: pWriter->iFree, piLast: &iLast, paRoot: &zRoot, pnRoot: &nRoot);
185272 }
185273 if( rc==SQLITE_OK ){
185274 rc = fts3WriteSegdir(p, iLevel, iIdx,
185275 iStartBlock: pWriter->iFirst, iLeafEndBlock: iLastLeaf, iEndBlock: iLast, nLeafData: pWriter->nLeafData, zRoot, nRoot);
185276 }
185277 }else{
185278 /* The entire tree fits on the root node. Write it to the segdir table. */
185279 rc = fts3WriteSegdir(p, iLevel, iIdx,
185280 iStartBlock: 0, iLeafEndBlock: 0, iEndBlock: 0, nLeafData: pWriter->nLeafData, zRoot: pWriter->aData, nRoot: pWriter->nData);
185281 }
185282 p->nLeafAdd++;
185283 return rc;
185284}
185285
185286/*
185287** Release all memory held by the SegmentWriter object passed as the
185288** first argument.
185289*/
185290static void fts3SegWriterFree(SegmentWriter *pWriter){
185291 if( pWriter ){
185292 sqlite3_free(p: pWriter->aData);
185293 sqlite3_free(p: pWriter->zMalloc);
185294 fts3NodeFree(pTree: pWriter->pTree);
185295 sqlite3_free(p: pWriter);
185296 }
185297}
185298
185299/*
185300** The first value in the apVal[] array is assumed to contain an integer.
185301** This function tests if there exist any documents with docid values that
185302** are different from that integer. i.e. if deleting the document with docid
185303** pRowid would mean the FTS3 table were empty.
185304**
185305** If successful, *pisEmpty is set to true if the table is empty except for
185306** document pRowid, or false otherwise, and SQLITE_OK is returned. If an
185307** error occurs, an SQLite error code is returned.
185308*/
185309static int fts3IsEmpty(Fts3Table *p, sqlite3_value *pRowid, int *pisEmpty){
185310 sqlite3_stmt *pStmt;
185311 int rc;
185312 if( p->zContentTbl ){
185313 /* If using the content=xxx option, assume the table is never empty */
185314 *pisEmpty = 0;
185315 rc = SQLITE_OK;
185316 }else{
185317 rc = fts3SqlStmt(p, SQL_IS_EMPTY, pp: &pStmt, apVal: &pRowid);
185318 if( rc==SQLITE_OK ){
185319 if( SQLITE_ROW==sqlite3_step(pStmt) ){
185320 *pisEmpty = sqlite3_column_int(pStmt, i: 0);
185321 }
185322 rc = sqlite3_reset(pStmt);
185323 }
185324 }
185325 return rc;
185326}
185327
185328/*
185329** Set *pnMax to the largest segment level in the database for the index
185330** iIndex.
185331**
185332** Segment levels are stored in the 'level' column of the %_segdir table.
185333**
185334** Return SQLITE_OK if successful, or an SQLite error code if not.
185335*/
185336static int fts3SegmentMaxLevel(
185337 Fts3Table *p,
185338 int iLangid,
185339 int iIndex,
185340 sqlite3_int64 *pnMax
185341){
185342 sqlite3_stmt *pStmt;
185343 int rc;
185344 assert( iIndex>=0 && iIndex<p->nIndex );
185345
185346 /* Set pStmt to the compiled version of:
185347 **
185348 ** SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
185349 **
185350 ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
185351 */
185352 rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, pp: &pStmt, apVal: 0);
185353 if( rc!=SQLITE_OK ) return rc;
185354 sqlite3_bind_int64(pStmt, i: 1, iValue: getAbsoluteLevel(p, iLangid, iIndex, iLevel: 0));
185355 sqlite3_bind_int64(pStmt, i: 2,
185356 iValue: getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
185357 );
185358 if( SQLITE_ROW==sqlite3_step(pStmt) ){
185359 *pnMax = sqlite3_column_int64(pStmt, i: 0);
185360 }
185361 return sqlite3_reset(pStmt);
185362}
185363
185364/*
185365** iAbsLevel is an absolute level that may be assumed to exist within
185366** the database. This function checks if it is the largest level number
185367** within its index. Assuming no error occurs, *pbMax is set to 1 if
185368** iAbsLevel is indeed the largest level, or 0 otherwise, and SQLITE_OK
185369** is returned. If an error occurs, an error code is returned and the
185370** final value of *pbMax is undefined.
185371*/
185372static int fts3SegmentIsMaxLevel(Fts3Table *p, i64 iAbsLevel, int *pbMax){
185373
185374 /* Set pStmt to the compiled version of:
185375 **
185376 ** SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
185377 **
185378 ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
185379 */
185380 sqlite3_stmt *pStmt;
185381 int rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, pp: &pStmt, apVal: 0);
185382 if( rc!=SQLITE_OK ) return rc;
185383 sqlite3_bind_int64(pStmt, i: 1, iValue: iAbsLevel+1);
185384 sqlite3_bind_int64(pStmt, i: 2,
185385 iValue: (((u64)iAbsLevel/FTS3_SEGDIR_MAXLEVEL)+1) * FTS3_SEGDIR_MAXLEVEL
185386 );
185387
185388 *pbMax = 0;
185389 if( SQLITE_ROW==sqlite3_step(pStmt) ){
185390 *pbMax = sqlite3_column_type(pStmt, i: 0)==SQLITE_NULL;
185391 }
185392 return sqlite3_reset(pStmt);
185393}
185394
185395/*
185396** Delete all entries in the %_segments table associated with the segment
185397** opened with seg-reader pSeg. This function does not affect the contents
185398** of the %_segdir table.
185399*/
185400static int fts3DeleteSegment(
185401 Fts3Table *p, /* FTS table handle */
185402 Fts3SegReader *pSeg /* Segment to delete */
185403){
185404 int rc = SQLITE_OK; /* Return code */
185405 if( pSeg->iStartBlock ){
185406 sqlite3_stmt *pDelete; /* SQL statement to delete rows */
185407 rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, pp: &pDelete, apVal: 0);
185408 if( rc==SQLITE_OK ){
185409 sqlite3_bind_int64(pStmt: pDelete, i: 1, iValue: pSeg->iStartBlock);
185410 sqlite3_bind_int64(pStmt: pDelete, i: 2, iValue: pSeg->iEndBlock);
185411 sqlite3_step(pStmt: pDelete);
185412 rc = sqlite3_reset(pStmt: pDelete);
185413 }
185414 }
185415 return rc;
185416}
185417
185418/*
185419** This function is used after merging multiple segments into a single large
185420** segment to delete the old, now redundant, segment b-trees. Specifically,
185421** it:
185422**
185423** 1) Deletes all %_segments entries for the segments associated with
185424** each of the SegReader objects in the array passed as the third
185425** argument, and
185426**
185427** 2) deletes all %_segdir entries with level iLevel, or all %_segdir
185428** entries regardless of level if (iLevel<0).
185429**
185430** SQLITE_OK is returned if successful, otherwise an SQLite error code.
185431*/
185432static int fts3DeleteSegdir(
185433 Fts3Table *p, /* Virtual table handle */
185434 int iLangid, /* Language id */
185435 int iIndex, /* Index for p->aIndex */
185436 int iLevel, /* Level of %_segdir entries to delete */
185437 Fts3SegReader **apSegment, /* Array of SegReader objects */
185438 int nReader /* Size of array apSegment */
185439){
185440 int rc = SQLITE_OK; /* Return Code */
185441 int i; /* Iterator variable */
185442 sqlite3_stmt *pDelete = 0; /* SQL statement to delete rows */
185443
185444 for(i=0; rc==SQLITE_OK && i<nReader; i++){
185445 rc = fts3DeleteSegment(p, pSeg: apSegment[i]);
185446 }
185447 if( rc!=SQLITE_OK ){
185448 return rc;
185449 }
185450
185451 assert( iLevel>=0 || iLevel==FTS3_SEGCURSOR_ALL );
185452 if( iLevel==FTS3_SEGCURSOR_ALL ){
185453 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_RANGE, pp: &pDelete, apVal: 0);
185454 if( rc==SQLITE_OK ){
185455 sqlite3_bind_int64(pStmt: pDelete, i: 1, iValue: getAbsoluteLevel(p, iLangid, iIndex, iLevel: 0));
185456 sqlite3_bind_int64(pStmt: pDelete, i: 2,
185457 iValue: getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
185458 );
185459 }
185460 }else{
185461 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, pp: &pDelete, apVal: 0);
185462 if( rc==SQLITE_OK ){
185463 sqlite3_bind_int64(
185464 pStmt: pDelete, i: 1, iValue: getAbsoluteLevel(p, iLangid, iIndex, iLevel)
185465 );
185466 }
185467 }
185468
185469 if( rc==SQLITE_OK ){
185470 sqlite3_step(pStmt: pDelete);
185471 rc = sqlite3_reset(pStmt: pDelete);
185472 }
185473
185474 return rc;
185475}
185476
185477/*
185478** When this function is called, buffer *ppList (size *pnList bytes) contains
185479** a position list that may (or may not) feature multiple columns. This
185480** function adjusts the pointer *ppList and the length *pnList so that they
185481** identify the subset of the position list that corresponds to column iCol.
185482**
185483** If there are no entries in the input position list for column iCol, then
185484** *pnList is set to zero before returning.
185485**
185486** If parameter bZero is non-zero, then any part of the input list following
185487** the end of the output list is zeroed before returning.
185488*/
185489static void fts3ColumnFilter(
185490 int iCol, /* Column to filter on */
185491 int bZero, /* Zero out anything following *ppList */
185492 char **ppList, /* IN/OUT: Pointer to position list */
185493 int *pnList /* IN/OUT: Size of buffer *ppList in bytes */
185494){
185495 char *pList = *ppList;
185496 int nList = *pnList;
185497 char *pEnd = &pList[nList];
185498 int iCurrent = 0;
185499 char *p = pList;
185500
185501 assert( iCol>=0 );
185502 while( 1 ){
185503 char c = 0;
185504 while( p<pEnd && (c | *p)&0xFE ) c = *p++ & 0x80;
185505
185506 if( iCol==iCurrent ){
185507 nList = (int)(p - pList);
185508 break;
185509 }
185510
185511 nList -= (int)(p - pList);
185512 pList = p;
185513 if( nList<=0 ){
185514 break;
185515 }
185516 p = &pList[1];
185517 p += fts3GetVarint32(p, &iCurrent);
185518 }
185519
185520 if( bZero && (pEnd - &pList[nList])>0){
185521 memset(s: &pList[nList], c: 0, n: pEnd - &pList[nList]);
185522 }
185523 *ppList = pList;
185524 *pnList = nList;
185525}
185526
185527/*
185528** Cache data in the Fts3MultiSegReader.aBuffer[] buffer (overwriting any
185529** existing data). Grow the buffer if required.
185530**
185531** If successful, return SQLITE_OK. Otherwise, if an OOM error is encountered
185532** trying to resize the buffer, return SQLITE_NOMEM.
185533*/
185534static int fts3MsrBufferData(
185535 Fts3MultiSegReader *pMsr, /* Multi-segment-reader handle */
185536 char *pList,
185537 int nList
185538){
185539 if( nList>pMsr->nBuffer ){
185540 char *pNew;
185541 pMsr->nBuffer = nList*2;
185542 pNew = (char *)sqlite3_realloc(pOld: pMsr->aBuffer, n: pMsr->nBuffer);
185543 if( !pNew ) return SQLITE_NOMEM;
185544 pMsr->aBuffer = pNew;
185545 }
185546
185547 assert( nList>0 );
185548 memcpy(dest: pMsr->aBuffer, src: pList, n: nList);
185549 return SQLITE_OK;
185550}
185551
185552SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
185553 Fts3Table *p, /* Virtual table handle */
185554 Fts3MultiSegReader *pMsr, /* Multi-segment-reader handle */
185555 sqlite3_int64 *piDocid, /* OUT: Docid value */
185556 char **paPoslist, /* OUT: Pointer to position list */
185557 int *pnPoslist /* OUT: Size of position list in bytes */
185558){
185559 int nMerge = pMsr->nAdvance;
185560 Fts3SegReader **apSegment = pMsr->apSegment;
185561 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
185562 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
185563 );
185564
185565 if( nMerge==0 ){
185566 *paPoslist = 0;
185567 return SQLITE_OK;
185568 }
185569
185570 while( 1 ){
185571 Fts3SegReader *pSeg;
185572 pSeg = pMsr->apSegment[0];
185573
185574 if( pSeg->pOffsetList==0 ){
185575 *paPoslist = 0;
185576 break;
185577 }else{
185578 int rc;
185579 char *pList;
185580 int nList;
185581 int j;
185582 sqlite3_int64 iDocid = apSegment[0]->iDocid;
185583
185584 rc = fts3SegReaderNextDocid(pTab: p, pReader: apSegment[0], ppOffsetList: &pList, pnOffsetList: &nList);
185585 j = 1;
185586 while( rc==SQLITE_OK
185587 && j<nMerge
185588 && apSegment[j]->pOffsetList
185589 && apSegment[j]->iDocid==iDocid
185590 ){
185591 rc = fts3SegReaderNextDocid(pTab: p, pReader: apSegment[j], ppOffsetList: 0, pnOffsetList: 0);
185592 j++;
185593 }
185594 if( rc!=SQLITE_OK ) return rc;
185595 fts3SegReaderSort(apSegment: pMsr->apSegment, nSegment: nMerge, nSuspect: j, xCmp);
185596
185597 if( nList>0 && fts3SegReaderIsPending(apSegment[0]) ){
185598 rc = fts3MsrBufferData(pMsr, pList, nList: nList+1);
185599 if( rc!=SQLITE_OK ) return rc;
185600 assert( (pMsr->aBuffer[nList] & 0xFE)==0x00 );
185601 pList = pMsr->aBuffer;
185602 }
185603
185604 if( pMsr->iColFilter>=0 ){
185605 fts3ColumnFilter(iCol: pMsr->iColFilter, bZero: 1, ppList: &pList, pnList: &nList);
185606 }
185607
185608 if( nList>0 ){
185609 *paPoslist = pList;
185610 *piDocid = iDocid;
185611 *pnPoslist = nList;
185612 break;
185613 }
185614 }
185615 }
185616
185617 return SQLITE_OK;
185618}
185619
185620static int fts3SegReaderStart(
185621 Fts3Table *p, /* Virtual table handle */
185622 Fts3MultiSegReader *pCsr, /* Cursor object */
185623 const char *zTerm, /* Term searched for (or NULL) */
185624 int nTerm /* Length of zTerm in bytes */
185625){
185626 int i;
185627 int nSeg = pCsr->nSegment;
185628
185629 /* If the Fts3SegFilter defines a specific term (or term prefix) to search
185630 ** for, then advance each segment iterator until it points to a term of
185631 ** equal or greater value than the specified term. This prevents many
185632 ** unnecessary merge/sort operations for the case where single segment
185633 ** b-tree leaf nodes contain more than one term.
185634 */
185635 for(i=0; pCsr->bRestart==0 && i<pCsr->nSegment; i++){
185636 int res = 0;
185637 Fts3SegReader *pSeg = pCsr->apSegment[i];
185638 do {
185639 int rc = fts3SegReaderNext(p, pReader: pSeg, bIncr: 0);
185640 if( rc!=SQLITE_OK ) return rc;
185641 }while( zTerm && (res = fts3SegReaderTermCmp(pSeg, zTerm, nTerm))<0 );
185642
185643 if( pSeg->bLookup && res!=0 ){
185644 fts3SegReaderSetEof(pSeg);
185645 }
185646 }
185647 fts3SegReaderSort(apSegment: pCsr->apSegment, nSegment: nSeg, nSuspect: nSeg, xCmp: fts3SegReaderCmp);
185648
185649 return SQLITE_OK;
185650}
185651
185652SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(
185653 Fts3Table *p, /* Virtual table handle */
185654 Fts3MultiSegReader *pCsr, /* Cursor object */
185655 Fts3SegFilter *pFilter /* Restrictions on range of iteration */
185656){
185657 pCsr->pFilter = pFilter;
185658 return fts3SegReaderStart(p, pCsr, zTerm: pFilter->zTerm, nTerm: pFilter->nTerm);
185659}
185660
185661SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
185662 Fts3Table *p, /* Virtual table handle */
185663 Fts3MultiSegReader *pCsr, /* Cursor object */
185664 int iCol, /* Column to match on. */
185665 const char *zTerm, /* Term to iterate through a doclist for */
185666 int nTerm /* Number of bytes in zTerm */
185667){
185668 int i;
185669 int rc;
185670 int nSegment = pCsr->nSegment;
185671 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
185672 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
185673 );
185674
185675 assert( pCsr->pFilter==0 );
185676 assert( zTerm && nTerm>0 );
185677
185678 /* Advance each segment iterator until it points to the term zTerm/nTerm. */
185679 rc = fts3SegReaderStart(p, pCsr, zTerm, nTerm);
185680 if( rc!=SQLITE_OK ) return rc;
185681
185682 /* Determine how many of the segments actually point to zTerm/nTerm. */
185683 for(i=0; i<nSegment; i++){
185684 Fts3SegReader *pSeg = pCsr->apSegment[i];
185685 if( !pSeg->aNode || fts3SegReaderTermCmp(pSeg, zTerm, nTerm) ){
185686 break;
185687 }
185688 }
185689 pCsr->nAdvance = i;
185690
185691 /* Advance each of the segments to point to the first docid. */
185692 for(i=0; i<pCsr->nAdvance; i++){
185693 rc = fts3SegReaderFirstDocid(pTab: p, pReader: pCsr->apSegment[i]);
185694 if( rc!=SQLITE_OK ) return rc;
185695 }
185696 fts3SegReaderSort(apSegment: pCsr->apSegment, nSegment: i, nSuspect: i, xCmp);
185697
185698 assert( iCol<0 || iCol<p->nColumn );
185699 pCsr->iColFilter = iCol;
185700
185701 return SQLITE_OK;
185702}
185703
185704/*
185705** This function is called on a MultiSegReader that has been started using
185706** sqlite3Fts3MsrIncrStart(). One or more calls to MsrIncrNext() may also
185707** have been made. Calling this function puts the MultiSegReader in such
185708** a state that if the next two calls are:
185709**
185710** sqlite3Fts3SegReaderStart()
185711** sqlite3Fts3SegReaderStep()
185712**
185713** then the entire doclist for the term is available in
185714** MultiSegReader.aDoclist/nDoclist.
185715*/
185716SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr){
185717 int i; /* Used to iterate through segment-readers */
185718
185719 assert( pCsr->zTerm==0 );
185720 assert( pCsr->nTerm==0 );
185721 assert( pCsr->aDoclist==0 );
185722 assert( pCsr->nDoclist==0 );
185723
185724 pCsr->nAdvance = 0;
185725 pCsr->bRestart = 1;
185726 for(i=0; i<pCsr->nSegment; i++){
185727 pCsr->apSegment[i]->pOffsetList = 0;
185728 pCsr->apSegment[i]->nOffsetList = 0;
185729 pCsr->apSegment[i]->iDocid = 0;
185730 }
185731
185732 return SQLITE_OK;
185733}
185734
185735static int fts3GrowSegReaderBuffer(Fts3MultiSegReader *pCsr, int nReq){
185736 if( nReq>pCsr->nBuffer ){
185737 char *aNew;
185738 pCsr->nBuffer = nReq*2;
185739 aNew = sqlite3_realloc(pOld: pCsr->aBuffer, n: pCsr->nBuffer);
185740 if( !aNew ){
185741 return SQLITE_NOMEM;
185742 }
185743 pCsr->aBuffer = aNew;
185744 }
185745 return SQLITE_OK;
185746}
185747
185748
185749SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(
185750 Fts3Table *p, /* Virtual table handle */
185751 Fts3MultiSegReader *pCsr /* Cursor object */
185752){
185753 int rc = SQLITE_OK;
185754
185755 int isIgnoreEmpty = (pCsr->pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY);
185756 int isRequirePos = (pCsr->pFilter->flags & FTS3_SEGMENT_REQUIRE_POS);
185757 int isColFilter = (pCsr->pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER);
185758 int isPrefix = (pCsr->pFilter->flags & FTS3_SEGMENT_PREFIX);
185759 int isScan = (pCsr->pFilter->flags & FTS3_SEGMENT_SCAN);
185760 int isFirst = (pCsr->pFilter->flags & FTS3_SEGMENT_FIRST);
185761
185762 Fts3SegReader **apSegment = pCsr->apSegment;
185763 int nSegment = pCsr->nSegment;
185764 Fts3SegFilter *pFilter = pCsr->pFilter;
185765 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
185766 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
185767 );
185768
185769 if( pCsr->nSegment==0 ) return SQLITE_OK;
185770
185771 do {
185772 int nMerge;
185773 int i;
185774
185775 /* Advance the first pCsr->nAdvance entries in the apSegment[] array
185776 ** forward. Then sort the list in order of current term again.
185777 */
185778 for(i=0; i<pCsr->nAdvance; i++){
185779 Fts3SegReader *pSeg = apSegment[i];
185780 if( pSeg->bLookup ){
185781 fts3SegReaderSetEof(pSeg);
185782 }else{
185783 rc = fts3SegReaderNext(p, pReader: pSeg, bIncr: 0);
185784 }
185785 if( rc!=SQLITE_OK ) return rc;
185786 }
185787 fts3SegReaderSort(apSegment, nSegment, nSuspect: pCsr->nAdvance, xCmp: fts3SegReaderCmp);
185788 pCsr->nAdvance = 0;
185789
185790 /* If all the seg-readers are at EOF, we're finished. return SQLITE_OK. */
185791 assert( rc==SQLITE_OK );
185792 if( apSegment[0]->aNode==0 ) break;
185793
185794 pCsr->nTerm = apSegment[0]->nTerm;
185795 pCsr->zTerm = apSegment[0]->zTerm;
185796
185797 /* If this is a prefix-search, and if the term that apSegment[0] points
185798 ** to does not share a suffix with pFilter->zTerm/nTerm, then all
185799 ** required callbacks have been made. In this case exit early.
185800 **
185801 ** Similarly, if this is a search for an exact match, and the first term
185802 ** of segment apSegment[0] is not a match, exit early.
185803 */
185804 if( pFilter->zTerm && !isScan ){
185805 if( pCsr->nTerm<pFilter->nTerm
185806 || (!isPrefix && pCsr->nTerm>pFilter->nTerm)
185807 || memcmp(s1: pCsr->zTerm, s2: pFilter->zTerm, n: pFilter->nTerm)
185808 ){
185809 break;
185810 }
185811 }
185812
185813 nMerge = 1;
185814 while( nMerge<nSegment
185815 && apSegment[nMerge]->aNode
185816 && apSegment[nMerge]->nTerm==pCsr->nTerm
185817 && 0==memcmp(s1: pCsr->zTerm, s2: apSegment[nMerge]->zTerm, n: pCsr->nTerm)
185818 ){
185819 nMerge++;
185820 }
185821
185822 assert( isIgnoreEmpty || (isRequirePos && !isColFilter) );
185823 if( nMerge==1
185824 && !isIgnoreEmpty
185825 && !isFirst
185826 && (p->bDescIdx==0 || fts3SegReaderIsPending(apSegment[0])==0)
185827 ){
185828 pCsr->nDoclist = apSegment[0]->nDoclist;
185829 if( fts3SegReaderIsPending(apSegment[0]) ){
185830 rc = fts3MsrBufferData(pMsr: pCsr, pList: apSegment[0]->aDoclist, nList: pCsr->nDoclist);
185831 pCsr->aDoclist = pCsr->aBuffer;
185832 }else{
185833 pCsr->aDoclist = apSegment[0]->aDoclist;
185834 }
185835 if( rc==SQLITE_OK ) rc = SQLITE_ROW;
185836 }else{
185837 int nDoclist = 0; /* Size of doclist */
185838 sqlite3_int64 iPrev = 0; /* Previous docid stored in doclist */
185839
185840 /* The current term of the first nMerge entries in the array
185841 ** of Fts3SegReader objects is the same. The doclists must be merged
185842 ** and a single term returned with the merged doclist.
185843 */
185844 for(i=0; i<nMerge; i++){
185845 fts3SegReaderFirstDocid(pTab: p, pReader: apSegment[i]);
185846 }
185847 fts3SegReaderSort(apSegment, nSegment: nMerge, nSuspect: nMerge, xCmp);
185848 while( apSegment[0]->pOffsetList ){
185849 int j; /* Number of segments that share a docid */
185850 char *pList = 0;
185851 int nList = 0;
185852 int nByte;
185853 sqlite3_int64 iDocid = apSegment[0]->iDocid;
185854 fts3SegReaderNextDocid(pTab: p, pReader: apSegment[0], ppOffsetList: &pList, pnOffsetList: &nList);
185855 j = 1;
185856 while( j<nMerge
185857 && apSegment[j]->pOffsetList
185858 && apSegment[j]->iDocid==iDocid
185859 ){
185860 fts3SegReaderNextDocid(pTab: p, pReader: apSegment[j], ppOffsetList: 0, pnOffsetList: 0);
185861 j++;
185862 }
185863
185864 if( isColFilter ){
185865 fts3ColumnFilter(iCol: pFilter->iCol, bZero: 0, ppList: &pList, pnList: &nList);
185866 }
185867
185868 if( !isIgnoreEmpty || nList>0 ){
185869
185870 /* Calculate the 'docid' delta value to write into the merged
185871 ** doclist. */
185872 sqlite3_int64 iDelta;
185873 if( p->bDescIdx && nDoclist>0 ){
185874 if( iPrev<=iDocid ) return FTS_CORRUPT_VTAB;
185875 iDelta = (i64)((u64)iPrev - (u64)iDocid);
185876 }else{
185877 if( nDoclist>0 && iPrev>=iDocid ) return FTS_CORRUPT_VTAB;
185878 iDelta = (i64)((u64)iDocid - (u64)iPrev);
185879 }
185880
185881 nByte = sqlite3Fts3VarintLen(v: iDelta) + (isRequirePos?nList+1:0);
185882
185883 rc = fts3GrowSegReaderBuffer(pCsr, nReq: nByte+nDoclist+FTS3_NODE_PADDING);
185884 if( rc ) return rc;
185885
185886 if( isFirst ){
185887 char *a = &pCsr->aBuffer[nDoclist];
185888 int nWrite;
185889
185890 nWrite = sqlite3Fts3FirstFilter(iDelta, pList, nList, pOut: a);
185891 if( nWrite ){
185892 iPrev = iDocid;
185893 nDoclist += nWrite;
185894 }
185895 }else{
185896 nDoclist += sqlite3Fts3PutVarint(p: &pCsr->aBuffer[nDoclist], v: iDelta);
185897 iPrev = iDocid;
185898 if( isRequirePos ){
185899 memcpy(dest: &pCsr->aBuffer[nDoclist], src: pList, n: nList);
185900 nDoclist += nList;
185901 pCsr->aBuffer[nDoclist++] = '\0';
185902 }
185903 }
185904 }
185905
185906 fts3SegReaderSort(apSegment, nSegment: nMerge, nSuspect: j, xCmp);
185907 }
185908 if( nDoclist>0 ){
185909 rc = fts3GrowSegReaderBuffer(pCsr, nReq: nDoclist+FTS3_NODE_PADDING);
185910 if( rc ) return rc;
185911 memset(s: &pCsr->aBuffer[nDoclist], c: 0, FTS3_NODE_PADDING);
185912 pCsr->aDoclist = pCsr->aBuffer;
185913 pCsr->nDoclist = nDoclist;
185914 rc = SQLITE_ROW;
185915 }
185916 }
185917 pCsr->nAdvance = nMerge;
185918 }while( rc==SQLITE_OK );
185919
185920 return rc;
185921}
185922
185923
185924SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(
185925 Fts3MultiSegReader *pCsr /* Cursor object */
185926){
185927 if( pCsr ){
185928 int i;
185929 for(i=0; i<pCsr->nSegment; i++){
185930 sqlite3Fts3SegReaderFree(pReader: pCsr->apSegment[i]);
185931 }
185932 sqlite3_free(p: pCsr->apSegment);
185933 sqlite3_free(p: pCsr->aBuffer);
185934
185935 pCsr->nSegment = 0;
185936 pCsr->apSegment = 0;
185937 pCsr->aBuffer = 0;
185938 }
185939}
185940
185941/*
185942** Decode the "end_block" field, selected by column iCol of the SELECT
185943** statement passed as the first argument.
185944**
185945** The "end_block" field may contain either an integer, or a text field
185946** containing the text representation of two non-negative integers separated
185947** by one or more space (0x20) characters. In the first case, set *piEndBlock
185948** to the integer value and *pnByte to zero before returning. In the second,
185949** set *piEndBlock to the first value and *pnByte to the second.
185950*/
185951static void fts3ReadEndBlockField(
185952 sqlite3_stmt *pStmt,
185953 int iCol,
185954 i64 *piEndBlock,
185955 i64 *pnByte
185956){
185957 const unsigned char *zText = sqlite3_column_text(pStmt, i: iCol);
185958 if( zText ){
185959 int i;
185960 int iMul = 1;
185961 u64 iVal = 0;
185962 for(i=0; zText[i]>='0' && zText[i]<='9'; i++){
185963 iVal = iVal*10 + (zText[i] - '0');
185964 }
185965 *piEndBlock = (i64)iVal;
185966 while( zText[i]==' ' ) i++;
185967 iVal = 0;
185968 if( zText[i]=='-' ){
185969 i++;
185970 iMul = -1;
185971 }
185972 for(/* no-op */; zText[i]>='0' && zText[i]<='9'; i++){
185973 iVal = iVal*10 + (zText[i] - '0');
185974 }
185975 *pnByte = ((i64)iVal * (i64)iMul);
185976 }
185977}
185978
185979
185980/*
185981** A segment of size nByte bytes has just been written to absolute level
185982** iAbsLevel. Promote any segments that should be promoted as a result.
185983*/
185984static int fts3PromoteSegments(
185985 Fts3Table *p, /* FTS table handle */
185986 sqlite3_int64 iAbsLevel, /* Absolute level just updated */
185987 sqlite3_int64 nByte /* Size of new segment at iAbsLevel */
185988){
185989 int rc = SQLITE_OK;
185990 sqlite3_stmt *pRange;
185991
185992 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE2, pp: &pRange, apVal: 0);
185993
185994 if( rc==SQLITE_OK ){
185995 int bOk = 0;
185996 i64 iLast = (iAbsLevel/FTS3_SEGDIR_MAXLEVEL + 1) * FTS3_SEGDIR_MAXLEVEL - 1;
185997 i64 nLimit = (nByte*3)/2;
185998
185999 /* Loop through all entries in the %_segdir table corresponding to
186000 ** segments in this index on levels greater than iAbsLevel. If there is
186001 ** at least one such segment, and it is possible to determine that all
186002 ** such segments are smaller than nLimit bytes in size, they will be
186003 ** promoted to level iAbsLevel. */
186004 sqlite3_bind_int64(pStmt: pRange, i: 1, iValue: iAbsLevel+1);
186005 sqlite3_bind_int64(pStmt: pRange, i: 2, iValue: iLast);
186006 while( SQLITE_ROW==sqlite3_step(pStmt: pRange) ){
186007 i64 nSize = 0, dummy;
186008 fts3ReadEndBlockField(pStmt: pRange, iCol: 2, piEndBlock: &dummy, pnByte: &nSize);
186009 if( nSize<=0 || nSize>nLimit ){
186010 /* If nSize==0, then the %_segdir.end_block field does not not
186011 ** contain a size value. This happens if it was written by an
186012 ** old version of FTS. In this case it is not possible to determine
186013 ** the size of the segment, and so segment promotion does not
186014 ** take place. */
186015 bOk = 0;
186016 break;
186017 }
186018 bOk = 1;
186019 }
186020 rc = sqlite3_reset(pStmt: pRange);
186021
186022 if( bOk ){
186023 int iIdx = 0;
186024 sqlite3_stmt *pUpdate1 = 0;
186025 sqlite3_stmt *pUpdate2 = 0;
186026
186027 if( rc==SQLITE_OK ){
186028 rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL_IDX, pp: &pUpdate1, apVal: 0);
186029 }
186030 if( rc==SQLITE_OK ){
186031 rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL, pp: &pUpdate2, apVal: 0);
186032 }
186033
186034 if( rc==SQLITE_OK ){
186035
186036 /* Loop through all %_segdir entries for segments in this index with
186037 ** levels equal to or greater than iAbsLevel. As each entry is visited,
186038 ** updated it to set (level = -1) and (idx = N), where N is 0 for the
186039 ** oldest segment in the range, 1 for the next oldest, and so on.
186040 **
186041 ** In other words, move all segments being promoted to level -1,
186042 ** setting the "idx" fields as appropriate to keep them in the same
186043 ** order. The contents of level -1 (which is never used, except
186044 ** transiently here), will be moved back to level iAbsLevel below. */
186045 sqlite3_bind_int64(pStmt: pRange, i: 1, iValue: iAbsLevel);
186046 while( SQLITE_ROW==sqlite3_step(pStmt: pRange) ){
186047 sqlite3_bind_int(p: pUpdate1, i: 1, iValue: iIdx++);
186048 sqlite3_bind_int(p: pUpdate1, i: 2, iValue: sqlite3_column_int(pStmt: pRange, i: 0));
186049 sqlite3_bind_int(p: pUpdate1, i: 3, iValue: sqlite3_column_int(pStmt: pRange, i: 1));
186050 sqlite3_step(pStmt: pUpdate1);
186051 rc = sqlite3_reset(pStmt: pUpdate1);
186052 if( rc!=SQLITE_OK ){
186053 sqlite3_reset(pStmt: pRange);
186054 break;
186055 }
186056 }
186057 }
186058 if( rc==SQLITE_OK ){
186059 rc = sqlite3_reset(pStmt: pRange);
186060 }
186061
186062 /* Move level -1 to level iAbsLevel */
186063 if( rc==SQLITE_OK ){
186064 sqlite3_bind_int64(pStmt: pUpdate2, i: 1, iValue: iAbsLevel);
186065 sqlite3_step(pStmt: pUpdate2);
186066 rc = sqlite3_reset(pStmt: pUpdate2);
186067 }
186068 }
186069 }
186070
186071
186072 return rc;
186073}
186074
186075/*
186076** Merge all level iLevel segments in the database into a single
186077** iLevel+1 segment. Or, if iLevel<0, merge all segments into a
186078** single segment with a level equal to the numerically largest level
186079** currently present in the database.
186080**
186081** If this function is called with iLevel<0, but there is only one
186082** segment in the database, SQLITE_DONE is returned immediately.
186083** Otherwise, if successful, SQLITE_OK is returned. If an error occurs,
186084** an SQLite error code is returned.
186085*/
186086static int fts3SegmentMerge(
186087 Fts3Table *p,
186088 int iLangid, /* Language id to merge */
186089 int iIndex, /* Index in p->aIndex[] to merge */
186090 int iLevel /* Level to merge */
186091){
186092 int rc; /* Return code */
186093 int iIdx = 0; /* Index of new segment */
186094 sqlite3_int64 iNewLevel = 0; /* Level/index to create new segment at */
186095 SegmentWriter *pWriter = 0; /* Used to write the new, merged, segment */
186096 Fts3SegFilter filter; /* Segment term filter condition */
186097 Fts3MultiSegReader csr; /* Cursor to iterate through level(s) */
186098 int bIgnoreEmpty = 0; /* True to ignore empty segments */
186099 i64 iMaxLevel = 0; /* Max level number for this index/langid */
186100
186101 assert( iLevel==FTS3_SEGCURSOR_ALL
186102 || iLevel==FTS3_SEGCURSOR_PENDING
186103 || iLevel>=0
186104 );
186105 assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
186106 assert( iIndex>=0 && iIndex<p->nIndex );
186107
186108 rc = sqlite3Fts3SegReaderCursor(p, iLangid, iIndex, iLevel, zTerm: 0, nTerm: 0, isPrefix: 1, isScan: 0, pCsr: &csr);
186109 if( rc!=SQLITE_OK || csr.nSegment==0 ) goto finished;
186110
186111 if( iLevel!=FTS3_SEGCURSOR_PENDING ){
186112 rc = fts3SegmentMaxLevel(p, iLangid, iIndex, pnMax: &iMaxLevel);
186113 if( rc!=SQLITE_OK ) goto finished;
186114 }
186115
186116 if( iLevel==FTS3_SEGCURSOR_ALL ){
186117 /* This call is to merge all segments in the database to a single
186118 ** segment. The level of the new segment is equal to the numerically
186119 ** greatest segment level currently present in the database for this
186120 ** index. The idx of the new segment is always 0. */
186121 if( csr.nSegment==1 && 0==fts3SegReaderIsPending(csr.apSegment[0]) ){
186122 rc = SQLITE_DONE;
186123 goto finished;
186124 }
186125 iNewLevel = iMaxLevel;
186126 bIgnoreEmpty = 1;
186127
186128 }else{
186129 /* This call is to merge all segments at level iLevel. find the next
186130 ** available segment index at level iLevel+1. The call to
186131 ** fts3AllocateSegdirIdx() will merge the segments at level iLevel+1 to
186132 ** a single iLevel+2 segment if necessary. */
186133 assert( FTS3_SEGCURSOR_PENDING==-1 );
186134 iNewLevel = getAbsoluteLevel(p, iLangid, iIndex, iLevel: iLevel+1);
186135 rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, iLevel: iLevel+1, piIdx: &iIdx);
186136 bIgnoreEmpty = (iLevel!=FTS3_SEGCURSOR_PENDING) && (iNewLevel>iMaxLevel);
186137 }
186138 if( rc!=SQLITE_OK ) goto finished;
186139
186140 assert( csr.nSegment>0 );
186141 assert_fts3_nc( iNewLevel>=getAbsoluteLevel(p, iLangid, iIndex, 0) );
186142 assert_fts3_nc(
186143 iNewLevel<getAbsoluteLevel(p, iLangid, iIndex,FTS3_SEGDIR_MAXLEVEL)
186144 );
186145
186146 memset(s: &filter, c: 0, n: sizeof(Fts3SegFilter));
186147 filter.flags = FTS3_SEGMENT_REQUIRE_POS;
186148 filter.flags |= (bIgnoreEmpty ? FTS3_SEGMENT_IGNORE_EMPTY : 0);
186149
186150 rc = sqlite3Fts3SegReaderStart(p, pCsr: &csr, pFilter: &filter);
186151 while( SQLITE_OK==rc ){
186152 rc = sqlite3Fts3SegReaderStep(p, pCsr: &csr);
186153 if( rc!=SQLITE_ROW ) break;
186154 rc = fts3SegWriterAdd(p, ppWriter: &pWriter, isCopyTerm: 1,
186155 zTerm: csr.zTerm, nTerm: csr.nTerm, aDoclist: csr.aDoclist, nDoclist: csr.nDoclist);
186156 }
186157 if( rc!=SQLITE_OK ) goto finished;
186158 assert_fts3_nc( pWriter || bIgnoreEmpty );
186159
186160 if( iLevel!=FTS3_SEGCURSOR_PENDING ){
186161 rc = fts3DeleteSegdir(
186162 p, iLangid, iIndex, iLevel, apSegment: csr.apSegment, nReader: csr.nSegment
186163 );
186164 if( rc!=SQLITE_OK ) goto finished;
186165 }
186166 if( pWriter ){
186167 rc = fts3SegWriterFlush(p, pWriter, iLevel: iNewLevel, iIdx);
186168 if( rc==SQLITE_OK ){
186169 if( iLevel==FTS3_SEGCURSOR_PENDING || iNewLevel<iMaxLevel ){
186170 rc = fts3PromoteSegments(p, iAbsLevel: iNewLevel, nByte: pWriter->nLeafData);
186171 }
186172 }
186173 }
186174
186175 finished:
186176 fts3SegWriterFree(pWriter);
186177 sqlite3Fts3SegReaderFinish(pCsr: &csr);
186178 return rc;
186179}
186180
186181
186182/*
186183** Flush the contents of pendingTerms to level 0 segments.
186184*/
186185SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *p){
186186 int rc = SQLITE_OK;
186187 int i;
186188
186189 for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
186190 rc = fts3SegmentMerge(p, iLangid: p->iPrevLangid, iIndex: i, FTS3_SEGCURSOR_PENDING);
186191 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
186192 }
186193 sqlite3Fts3PendingTermsClear(p);
186194
186195 /* Determine the auto-incr-merge setting if unknown. If enabled,
186196 ** estimate the number of leaf blocks of content to be written
186197 */
186198 if( rc==SQLITE_OK && p->bHasStat
186199 && p->nAutoincrmerge==0xff && p->nLeafAdd>0
186200 ){
186201 sqlite3_stmt *pStmt = 0;
186202 rc = fts3SqlStmt(p, SQL_SELECT_STAT, pp: &pStmt, apVal: 0);
186203 if( rc==SQLITE_OK ){
186204 sqlite3_bind_int(p: pStmt, i: 1, FTS_STAT_AUTOINCRMERGE);
186205 rc = sqlite3_step(pStmt);
186206 if( rc==SQLITE_ROW ){
186207 p->nAutoincrmerge = sqlite3_column_int(pStmt, i: 0);
186208 if( p->nAutoincrmerge==1 ) p->nAutoincrmerge = 8;
186209 }else if( rc==SQLITE_DONE ){
186210 p->nAutoincrmerge = 0;
186211 }
186212 rc = sqlite3_reset(pStmt);
186213 }
186214 }
186215 return rc;
186216}
186217
186218/*
186219** Encode N integers as varints into a blob.
186220*/
186221static void fts3EncodeIntArray(
186222 int N, /* The number of integers to encode */
186223 u32 *a, /* The integer values */
186224 char *zBuf, /* Write the BLOB here */
186225 int *pNBuf /* Write number of bytes if zBuf[] used here */
186226){
186227 int i, j;
186228 for(i=j=0; i<N; i++){
186229 j += sqlite3Fts3PutVarint(p: &zBuf[j], v: (sqlite3_int64)a[i]);
186230 }
186231 *pNBuf = j;
186232}
186233
186234/*
186235** Decode a blob of varints into N integers
186236*/
186237static void fts3DecodeIntArray(
186238 int N, /* The number of integers to decode */
186239 u32 *a, /* Write the integer values */
186240 const char *zBuf, /* The BLOB containing the varints */
186241 int nBuf /* size of the BLOB */
186242){
186243 int i = 0;
186244 if( nBuf && (zBuf[nBuf-1]&0x80)==0 ){
186245 int j;
186246 for(i=j=0; i<N && j<nBuf; i++){
186247 sqlite3_int64 x;
186248 j += sqlite3Fts3GetVarint(pBuf: &zBuf[j], v: &x);
186249 a[i] = (u32)(x & 0xffffffff);
186250 }
186251 }
186252 while( i<N ) a[i++] = 0;
186253}
186254
186255/*
186256** Insert the sizes (in tokens) for each column of the document
186257** with docid equal to p->iPrevDocid. The sizes are encoded as
186258** a blob of varints.
186259*/
186260static void fts3InsertDocsize(
186261 int *pRC, /* Result code */
186262 Fts3Table *p, /* Table into which to insert */
186263 u32 *aSz /* Sizes of each column, in tokens */
186264){
186265 char *pBlob; /* The BLOB encoding of the document size */
186266 int nBlob; /* Number of bytes in the BLOB */
186267 sqlite3_stmt *pStmt; /* Statement used to insert the encoding */
186268 int rc; /* Result code from subfunctions */
186269
186270 if( *pRC ) return;
186271 pBlob = sqlite3_malloc64( n: 10*(sqlite3_int64)p->nColumn );
186272 if( pBlob==0 ){
186273 *pRC = SQLITE_NOMEM;
186274 return;
186275 }
186276 fts3EncodeIntArray(N: p->nColumn, a: aSz, zBuf: pBlob, pNBuf: &nBlob);
186277 rc = fts3SqlStmt(p, SQL_REPLACE_DOCSIZE, pp: &pStmt, apVal: 0);
186278 if( rc ){
186279 sqlite3_free(p: pBlob);
186280 *pRC = rc;
186281 return;
186282 }
186283 sqlite3_bind_int64(pStmt, i: 1, iValue: p->iPrevDocid);
186284 sqlite3_bind_blob(pStmt, i: 2, zData: pBlob, nData: nBlob, xDel: sqlite3_free);
186285 sqlite3_step(pStmt);
186286 *pRC = sqlite3_reset(pStmt);
186287}
186288
186289/*
186290** Record 0 of the %_stat table contains a blob consisting of N varints,
186291** where N is the number of user defined columns in the fts3 table plus
186292** two. If nCol is the number of user defined columns, then values of the
186293** varints are set as follows:
186294**
186295** Varint 0: Total number of rows in the table.
186296**
186297** Varint 1..nCol: For each column, the total number of tokens stored in
186298** the column for all rows of the table.
186299**
186300** Varint 1+nCol: The total size, in bytes, of all text values in all
186301** columns of all rows of the table.
186302**
186303*/
186304static void fts3UpdateDocTotals(
186305 int *pRC, /* The result code */
186306 Fts3Table *p, /* Table being updated */
186307 u32 *aSzIns, /* Size increases */
186308 u32 *aSzDel, /* Size decreases */
186309 int nChng /* Change in the number of documents */
186310){
186311 char *pBlob; /* Storage for BLOB written into %_stat */
186312 int nBlob; /* Size of BLOB written into %_stat */
186313 u32 *a; /* Array of integers that becomes the BLOB */
186314 sqlite3_stmt *pStmt; /* Statement for reading and writing */
186315 int i; /* Loop counter */
186316 int rc; /* Result code from subfunctions */
186317
186318 const int nStat = p->nColumn+2;
186319
186320 if( *pRC ) return;
186321 a = sqlite3_malloc64( n: (sizeof(u32)+10)*(sqlite3_int64)nStat );
186322 if( a==0 ){
186323 *pRC = SQLITE_NOMEM;
186324 return;
186325 }
186326 pBlob = (char*)&a[nStat];
186327 rc = fts3SqlStmt(p, SQL_SELECT_STAT, pp: &pStmt, apVal: 0);
186328 if( rc ){
186329 sqlite3_free(p: a);
186330 *pRC = rc;
186331 return;
186332 }
186333 sqlite3_bind_int(p: pStmt, i: 1, FTS_STAT_DOCTOTAL);
186334 if( sqlite3_step(pStmt)==SQLITE_ROW ){
186335 fts3DecodeIntArray(N: nStat, a,
186336 zBuf: sqlite3_column_blob(pStmt, i: 0),
186337 nBuf: sqlite3_column_bytes(pStmt, i: 0));
186338 }else{
186339 memset(s: a, c: 0, n: sizeof(u32)*(nStat) );
186340 }
186341 rc = sqlite3_reset(pStmt);
186342 if( rc!=SQLITE_OK ){
186343 sqlite3_free(p: a);
186344 *pRC = rc;
186345 return;
186346 }
186347 if( nChng<0 && a[0]<(u32)(-nChng) ){
186348 a[0] = 0;
186349 }else{
186350 a[0] += nChng;
186351 }
186352 for(i=0; i<p->nColumn+1; i++){
186353 u32 x = a[i+1];
186354 if( x+aSzIns[i] < aSzDel[i] ){
186355 x = 0;
186356 }else{
186357 x = x + aSzIns[i] - aSzDel[i];
186358 }
186359 a[i+1] = x;
186360 }
186361 fts3EncodeIntArray(N: nStat, a, zBuf: pBlob, pNBuf: &nBlob);
186362 rc = fts3SqlStmt(p, SQL_REPLACE_STAT, pp: &pStmt, apVal: 0);
186363 if( rc ){
186364 sqlite3_free(p: a);
186365 *pRC = rc;
186366 return;
186367 }
186368 sqlite3_bind_int(p: pStmt, i: 1, FTS_STAT_DOCTOTAL);
186369 sqlite3_bind_blob(pStmt, i: 2, zData: pBlob, nData: nBlob, SQLITE_STATIC);
186370 sqlite3_step(pStmt);
186371 *pRC = sqlite3_reset(pStmt);
186372 sqlite3_bind_null(pStmt, i: 2);
186373 sqlite3_free(p: a);
186374}
186375
186376/*
186377** Merge the entire database so that there is one segment for each
186378** iIndex/iLangid combination.
186379*/
186380static int fts3DoOptimize(Fts3Table *p, int bReturnDone){
186381 int bSeenDone = 0;
186382 int rc;
186383 sqlite3_stmt *pAllLangid = 0;
186384
186385 rc = sqlite3Fts3PendingTermsFlush(p);
186386 if( rc==SQLITE_OK ){
186387 rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, pp: &pAllLangid, apVal: 0);
186388 }
186389 if( rc==SQLITE_OK ){
186390 int rc2;
186391 sqlite3_bind_int(p: pAllLangid, i: 1, iValue: p->iPrevLangid);
186392 sqlite3_bind_int(p: pAllLangid, i: 2, iValue: p->nIndex);
186393 while( sqlite3_step(pStmt: pAllLangid)==SQLITE_ROW ){
186394 int i;
186395 int iLangid = sqlite3_column_int(pStmt: pAllLangid, i: 0);
186396 for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
186397 rc = fts3SegmentMerge(p, iLangid, iIndex: i, FTS3_SEGCURSOR_ALL);
186398 if( rc==SQLITE_DONE ){
186399 bSeenDone = 1;
186400 rc = SQLITE_OK;
186401 }
186402 }
186403 }
186404 rc2 = sqlite3_reset(pStmt: pAllLangid);
186405 if( rc==SQLITE_OK ) rc = rc2;
186406 }
186407
186408 sqlite3Fts3SegmentsClose(p);
186409
186410 return (rc==SQLITE_OK && bReturnDone && bSeenDone) ? SQLITE_DONE : rc;
186411}
186412
186413/*
186414** This function is called when the user executes the following statement:
186415**
186416** INSERT INTO <tbl>(<tbl>) VALUES('rebuild');
186417**
186418** The entire FTS index is discarded and rebuilt. If the table is one
186419** created using the content=xxx option, then the new index is based on
186420** the current contents of the xxx table. Otherwise, it is rebuilt based
186421** on the contents of the %_content table.
186422*/
186423static int fts3DoRebuild(Fts3Table *p){
186424 int rc; /* Return Code */
186425
186426 rc = fts3DeleteAll(p, bContent: 0);
186427 if( rc==SQLITE_OK ){
186428 u32 *aSz = 0;
186429 u32 *aSzIns = 0;
186430 u32 *aSzDel = 0;
186431 sqlite3_stmt *pStmt = 0;
186432 int nEntry = 0;
186433
186434 /* Compose and prepare an SQL statement to loop through the content table */
186435 char *zSql = sqlite3_mprintf(zFormat: "SELECT %s" , p->zReadExprlist);
186436 if( !zSql ){
186437 rc = SQLITE_NOMEM;
186438 }else{
186439 rc = sqlite3_prepare_v2(db: p->db, zSql, nBytes: -1, ppStmt: &pStmt, pzTail: 0);
186440 sqlite3_free(p: zSql);
186441 }
186442
186443 if( rc==SQLITE_OK ){
186444 sqlite3_int64 nByte = sizeof(u32) * ((sqlite3_int64)p->nColumn+1)*3;
186445 aSz = (u32 *)sqlite3_malloc64(n: nByte);
186446 if( aSz==0 ){
186447 rc = SQLITE_NOMEM;
186448 }else{
186449 memset(s: aSz, c: 0, n: nByte);
186450 aSzIns = &aSz[p->nColumn+1];
186451 aSzDel = &aSzIns[p->nColumn+1];
186452 }
186453 }
186454
186455 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
186456 int iCol;
186457 int iLangid = langidFromSelect(p, pSelect: pStmt);
186458 rc = fts3PendingTermsDocid(p, bDelete: 0, iLangid, iDocid: sqlite3_column_int64(pStmt, i: 0));
186459 memset(s: aSz, c: 0, n: sizeof(aSz[0]) * (p->nColumn+1));
186460 for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
186461 if( p->abNotindexed[iCol]==0 ){
186462 const char *z = (const char *) sqlite3_column_text(pStmt, i: iCol+1);
186463 rc = fts3PendingTermsAdd(p, iLangid, zText: z, iCol, pnWord: &aSz[iCol]);
186464 aSz[p->nColumn] += sqlite3_column_bytes(pStmt, i: iCol+1);
186465 }
186466 }
186467 if( p->bHasDocsize ){
186468 fts3InsertDocsize(pRC: &rc, p, aSz);
186469 }
186470 if( rc!=SQLITE_OK ){
186471 sqlite3_finalize(pStmt);
186472 pStmt = 0;
186473 }else{
186474 nEntry++;
186475 for(iCol=0; iCol<=p->nColumn; iCol++){
186476 aSzIns[iCol] += aSz[iCol];
186477 }
186478 }
186479 }
186480 if( p->bFts4 ){
186481 fts3UpdateDocTotals(pRC: &rc, p, aSzIns, aSzDel, nChng: nEntry);
186482 }
186483 sqlite3_free(p: aSz);
186484
186485 if( pStmt ){
186486 int rc2 = sqlite3_finalize(pStmt);
186487 if( rc==SQLITE_OK ){
186488 rc = rc2;
186489 }
186490 }
186491 }
186492
186493 return rc;
186494}
186495
186496
186497/*
186498** This function opens a cursor used to read the input data for an
186499** incremental merge operation. Specifically, it opens a cursor to scan
186500** the oldest nSeg segments (idx=0 through idx=(nSeg-1)) in absolute
186501** level iAbsLevel.
186502*/
186503static int fts3IncrmergeCsr(
186504 Fts3Table *p, /* FTS3 table handle */
186505 sqlite3_int64 iAbsLevel, /* Absolute level to open */
186506 int nSeg, /* Number of segments to merge */
186507 Fts3MultiSegReader *pCsr /* Cursor object to populate */
186508){
186509 int rc; /* Return Code */
186510 sqlite3_stmt *pStmt = 0; /* Statement used to read %_segdir entry */
186511 sqlite3_int64 nByte; /* Bytes allocated at pCsr->apSegment[] */
186512
186513 /* Allocate space for the Fts3MultiSegReader.aCsr[] array */
186514 memset(s: pCsr, c: 0, n: sizeof(*pCsr));
186515 nByte = sizeof(Fts3SegReader *) * nSeg;
186516 pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc64(n: nByte);
186517
186518 if( pCsr->apSegment==0 ){
186519 rc = SQLITE_NOMEM;
186520 }else{
186521 memset(s: pCsr->apSegment, c: 0, n: nByte);
186522 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, pp: &pStmt, apVal: 0);
186523 }
186524 if( rc==SQLITE_OK ){
186525 int i;
186526 int rc2;
186527 sqlite3_bind_int64(pStmt, i: 1, iValue: iAbsLevel);
186528 assert( pCsr->nSegment==0 );
186529 for(i=0; rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW && i<nSeg; i++){
186530 rc = sqlite3Fts3SegReaderNew(iAge: i, bLookup: 0,
186531 iStartLeaf: sqlite3_column_int64(pStmt, i: 1), /* segdir.start_block */
186532 iEndLeaf: sqlite3_column_int64(pStmt, i: 2), /* segdir.leaves_end_block */
186533 iEndBlock: sqlite3_column_int64(pStmt, i: 3), /* segdir.end_block */
186534 zRoot: sqlite3_column_blob(pStmt, i: 4), /* segdir.root */
186535 nRoot: sqlite3_column_bytes(pStmt, i: 4), /* segdir.root */
186536 ppReader: &pCsr->apSegment[i]
186537 );
186538 pCsr->nSegment++;
186539 }
186540 rc2 = sqlite3_reset(pStmt);
186541 if( rc==SQLITE_OK ) rc = rc2;
186542 }
186543
186544 return rc;
186545}
186546
186547typedef struct IncrmergeWriter IncrmergeWriter;
186548typedef struct NodeWriter NodeWriter;
186549typedef struct Blob Blob;
186550typedef struct NodeReader NodeReader;
186551
186552/*
186553** An instance of the following structure is used as a dynamic buffer
186554** to build up nodes or other blobs of data in.
186555**
186556** The function blobGrowBuffer() is used to extend the allocation.
186557*/
186558struct Blob {
186559 char *a; /* Pointer to allocation */
186560 int n; /* Number of valid bytes of data in a[] */
186561 int nAlloc; /* Allocated size of a[] (nAlloc>=n) */
186562};
186563
186564/*
186565** This structure is used to build up buffers containing segment b-tree
186566** nodes (blocks).
186567*/
186568struct NodeWriter {
186569 sqlite3_int64 iBlock; /* Current block id */
186570 Blob key; /* Last key written to the current block */
186571 Blob block; /* Current block image */
186572};
186573
186574/*
186575** An object of this type contains the state required to create or append
186576** to an appendable b-tree segment.
186577*/
186578struct IncrmergeWriter {
186579 int nLeafEst; /* Space allocated for leaf blocks */
186580 int nWork; /* Number of leaf pages flushed */
186581 sqlite3_int64 iAbsLevel; /* Absolute level of input segments */
186582 int iIdx; /* Index of *output* segment in iAbsLevel+1 */
186583 sqlite3_int64 iStart; /* Block number of first allocated block */
186584 sqlite3_int64 iEnd; /* Block number of last allocated block */
186585 sqlite3_int64 nLeafData; /* Bytes of leaf page data so far */
186586 u8 bNoLeafData; /* If true, store 0 for segment size */
186587 NodeWriter aNodeWriter[FTS_MAX_APPENDABLE_HEIGHT];
186588};
186589
186590/*
186591** An object of the following type is used to read data from a single
186592** FTS segment node. See the following functions:
186593**
186594** nodeReaderInit()
186595** nodeReaderNext()
186596** nodeReaderRelease()
186597*/
186598struct NodeReader {
186599 const char *aNode;
186600 int nNode;
186601 int iOff; /* Current offset within aNode[] */
186602
186603 /* Output variables. Containing the current node entry. */
186604 sqlite3_int64 iChild; /* Pointer to child node */
186605 Blob term; /* Current term */
186606 const char *aDoclist; /* Pointer to doclist */
186607 int nDoclist; /* Size of doclist in bytes */
186608};
186609
186610/*
186611** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
186612** Otherwise, if the allocation at pBlob->a is not already at least nMin
186613** bytes in size, extend (realloc) it to be so.
186614**
186615** If an OOM error occurs, set *pRc to SQLITE_NOMEM and leave pBlob->a
186616** unmodified. Otherwise, if the allocation succeeds, update pBlob->nAlloc
186617** to reflect the new size of the pBlob->a[] buffer.
186618*/
186619static void blobGrowBuffer(Blob *pBlob, int nMin, int *pRc){
186620 if( *pRc==SQLITE_OK && nMin>pBlob->nAlloc ){
186621 int nAlloc = nMin;
186622 char *a = (char *)sqlite3_realloc(pOld: pBlob->a, n: nAlloc);
186623 if( a ){
186624 pBlob->nAlloc = nAlloc;
186625 pBlob->a = a;
186626 }else{
186627 *pRc = SQLITE_NOMEM;
186628 }
186629 }
186630}
186631
186632/*
186633** Attempt to advance the node-reader object passed as the first argument to
186634** the next entry on the node.
186635**
186636** Return an error code if an error occurs (SQLITE_NOMEM is possible).
186637** Otherwise return SQLITE_OK. If there is no next entry on the node
186638** (e.g. because the current entry is the last) set NodeReader->aNode to
186639** NULL to indicate EOF. Otherwise, populate the NodeReader structure output
186640** variables for the new entry.
186641*/
186642static int nodeReaderNext(NodeReader *p){
186643 int bFirst = (p->term.n==0); /* True for first term on the node */
186644 int nPrefix = 0; /* Bytes to copy from previous term */
186645 int nSuffix = 0; /* Bytes to append to the prefix */
186646 int rc = SQLITE_OK; /* Return code */
186647
186648 assert( p->aNode );
186649 if( p->iChild && bFirst==0 ) p->iChild++;
186650 if( p->iOff>=p->nNode ){
186651 /* EOF */
186652 p->aNode = 0;
186653 }else{
186654 if( bFirst==0 ){
186655 p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nPrefix);
186656 }
186657 p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nSuffix);
186658
186659 if( nPrefix>p->term.n || nSuffix>p->nNode-p->iOff || nSuffix==0 ){
186660 return FTS_CORRUPT_VTAB;
186661 }
186662 blobGrowBuffer(pBlob: &p->term, nMin: nPrefix+nSuffix, pRc: &rc);
186663 if( rc==SQLITE_OK && ALWAYS(p->term.a!=0) ){
186664 memcpy(dest: &p->term.a[nPrefix], src: &p->aNode[p->iOff], n: nSuffix);
186665 p->term.n = nPrefix+nSuffix;
186666 p->iOff += nSuffix;
186667 if( p->iChild==0 ){
186668 p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &p->nDoclist);
186669 if( (p->nNode-p->iOff)<p->nDoclist ){
186670 return FTS_CORRUPT_VTAB;
186671 }
186672 p->aDoclist = &p->aNode[p->iOff];
186673 p->iOff += p->nDoclist;
186674 }
186675 }
186676 }
186677
186678 assert_fts3_nc( p->iOff<=p->nNode );
186679 return rc;
186680}
186681
186682/*
186683** Release all dynamic resources held by node-reader object *p.
186684*/
186685static void nodeReaderRelease(NodeReader *p){
186686 sqlite3_free(p: p->term.a);
186687}
186688
186689/*
186690** Initialize a node-reader object to read the node in buffer aNode/nNode.
186691**
186692** If successful, SQLITE_OK is returned and the NodeReader object set to
186693** point to the first entry on the node (if any). Otherwise, an SQLite
186694** error code is returned.
186695*/
186696static int nodeReaderInit(NodeReader *p, const char *aNode, int nNode){
186697 memset(s: p, c: 0, n: sizeof(NodeReader));
186698 p->aNode = aNode;
186699 p->nNode = nNode;
186700
186701 /* Figure out if this is a leaf or an internal node. */
186702 if( aNode && aNode[0] ){
186703 /* An internal node. */
186704 p->iOff = 1 + sqlite3Fts3GetVarint(pBuf: &p->aNode[1], v: &p->iChild);
186705 }else{
186706 p->iOff = 1;
186707 }
186708
186709 return aNode ? nodeReaderNext(p) : SQLITE_OK;
186710}
186711
186712/*
186713** This function is called while writing an FTS segment each time a leaf o
186714** node is finished and written to disk. The key (zTerm/nTerm) is guaranteed
186715** to be greater than the largest key on the node just written, but smaller
186716** than or equal to the first key that will be written to the next leaf
186717** node.
186718**
186719** The block id of the leaf node just written to disk may be found in
186720** (pWriter->aNodeWriter[0].iBlock) when this function is called.
186721*/
186722static int fts3IncrmergePush(
186723 Fts3Table *p, /* Fts3 table handle */
186724 IncrmergeWriter *pWriter, /* Writer object */
186725 const char *zTerm, /* Term to write to internal node */
186726 int nTerm /* Bytes at zTerm */
186727){
186728 sqlite3_int64 iPtr = pWriter->aNodeWriter[0].iBlock;
186729 int iLayer;
186730
186731 assert( nTerm>0 );
186732 for(iLayer=1; ALWAYS(iLayer<FTS_MAX_APPENDABLE_HEIGHT); iLayer++){
186733 sqlite3_int64 iNextPtr = 0;
186734 NodeWriter *pNode = &pWriter->aNodeWriter[iLayer];
186735 int rc = SQLITE_OK;
186736 int nPrefix;
186737 int nSuffix;
186738 int nSpace;
186739
186740 /* Figure out how much space the key will consume if it is written to
186741 ** the current node of layer iLayer. Due to the prefix compression,
186742 ** the space required changes depending on which node the key is to
186743 ** be added to. */
186744 nPrefix = fts3PrefixCompress(zPrev: pNode->key.a, nPrev: pNode->key.n, zNext: zTerm, nNext: nTerm);
186745 nSuffix = nTerm - nPrefix;
186746 if(nSuffix<=0 ) return FTS_CORRUPT_VTAB;
186747 nSpace = sqlite3Fts3VarintLen(v: nPrefix);
186748 nSpace += sqlite3Fts3VarintLen(v: nSuffix) + nSuffix;
186749
186750 if( pNode->key.n==0 || (pNode->block.n + nSpace)<=p->nNodeSize ){
186751 /* If the current node of layer iLayer contains zero keys, or if adding
186752 ** the key to it will not cause it to grow to larger than nNodeSize
186753 ** bytes in size, write the key here. */
186754
186755 Blob *pBlk = &pNode->block;
186756 if( pBlk->n==0 ){
186757 blobGrowBuffer(pBlob: pBlk, nMin: p->nNodeSize, pRc: &rc);
186758 if( rc==SQLITE_OK ){
186759 pBlk->a[0] = (char)iLayer;
186760 pBlk->n = 1 + sqlite3Fts3PutVarint(p: &pBlk->a[1], v: iPtr);
186761 }
186762 }
186763 blobGrowBuffer(pBlob: pBlk, nMin: pBlk->n + nSpace, pRc: &rc);
186764 blobGrowBuffer(pBlob: &pNode->key, nMin: nTerm, pRc: &rc);
186765
186766 if( rc==SQLITE_OK ){
186767 if( pNode->key.n ){
186768 pBlk->n += sqlite3Fts3PutVarint(p: &pBlk->a[pBlk->n], v: nPrefix);
186769 }
186770 pBlk->n += sqlite3Fts3PutVarint(p: &pBlk->a[pBlk->n], v: nSuffix);
186771 memcpy(dest: &pBlk->a[pBlk->n], src: &zTerm[nPrefix], n: nSuffix);
186772 pBlk->n += nSuffix;
186773
186774 memcpy(dest: pNode->key.a, src: zTerm, n: nTerm);
186775 pNode->key.n = nTerm;
186776 }
186777 }else{
186778 /* Otherwise, flush the current node of layer iLayer to disk.
186779 ** Then allocate a new, empty sibling node. The key will be written
186780 ** into the parent of this node. */
186781 rc = fts3WriteSegment(p, iBlock: pNode->iBlock, z: pNode->block.a, n: pNode->block.n);
186782
186783 assert( pNode->block.nAlloc>=p->nNodeSize );
186784 pNode->block.a[0] = (char)iLayer;
186785 pNode->block.n = 1 + sqlite3Fts3PutVarint(p: &pNode->block.a[1], v: iPtr+1);
186786
186787 iNextPtr = pNode->iBlock;
186788 pNode->iBlock++;
186789 pNode->key.n = 0;
186790 }
186791
186792 if( rc!=SQLITE_OK || iNextPtr==0 ) return rc;
186793 iPtr = iNextPtr;
186794 }
186795
186796 assert( 0 );
186797 return 0;
186798}
186799
186800/*
186801** Append a term and (optionally) doclist to the FTS segment node currently
186802** stored in blob *pNode. The node need not contain any terms, but the
186803** header must be written before this function is called.
186804**
186805** A node header is a single 0x00 byte for a leaf node, or a height varint
186806** followed by the left-hand-child varint for an internal node.
186807**
186808** The term to be appended is passed via arguments zTerm/nTerm. For a
186809** leaf node, the doclist is passed as aDoclist/nDoclist. For an internal
186810** node, both aDoclist and nDoclist must be passed 0.
186811**
186812** If the size of the value in blob pPrev is zero, then this is the first
186813** term written to the node. Otherwise, pPrev contains a copy of the
186814** previous term. Before this function returns, it is updated to contain a
186815** copy of zTerm/nTerm.
186816**
186817** It is assumed that the buffer associated with pNode is already large
186818** enough to accommodate the new entry. The buffer associated with pPrev
186819** is extended by this function if requrired.
186820**
186821** If an error (i.e. OOM condition) occurs, an SQLite error code is
186822** returned. Otherwise, SQLITE_OK.
186823*/
186824static int fts3AppendToNode(
186825 Blob *pNode, /* Current node image to append to */
186826 Blob *pPrev, /* Buffer containing previous term written */
186827 const char *zTerm, /* New term to write */
186828 int nTerm, /* Size of zTerm in bytes */
186829 const char *aDoclist, /* Doclist (or NULL) to write */
186830 int nDoclist /* Size of aDoclist in bytes */
186831){
186832 int rc = SQLITE_OK; /* Return code */
186833 int bFirst = (pPrev->n==0); /* True if this is the first term written */
186834 int nPrefix; /* Size of term prefix in bytes */
186835 int nSuffix; /* Size of term suffix in bytes */
186836
186837 /* Node must have already been started. There must be a doclist for a
186838 ** leaf node, and there must not be a doclist for an internal node. */
186839 assert( pNode->n>0 );
186840 assert_fts3_nc( (pNode->a[0]=='\0')==(aDoclist!=0) );
186841
186842 blobGrowBuffer(pBlob: pPrev, nMin: nTerm, pRc: &rc);
186843 if( rc!=SQLITE_OK ) return rc;
186844
186845 nPrefix = fts3PrefixCompress(zPrev: pPrev->a, nPrev: pPrev->n, zNext: zTerm, nNext: nTerm);
186846 nSuffix = nTerm - nPrefix;
186847 if( nSuffix<=0 ) return FTS_CORRUPT_VTAB;
186848 memcpy(dest: pPrev->a, src: zTerm, n: nTerm);
186849 pPrev->n = nTerm;
186850
186851 if( bFirst==0 ){
186852 pNode->n += sqlite3Fts3PutVarint(p: &pNode->a[pNode->n], v: nPrefix);
186853 }
186854 pNode->n += sqlite3Fts3PutVarint(p: &pNode->a[pNode->n], v: nSuffix);
186855 memcpy(dest: &pNode->a[pNode->n], src: &zTerm[nPrefix], n: nSuffix);
186856 pNode->n += nSuffix;
186857
186858 if( aDoclist ){
186859 pNode->n += sqlite3Fts3PutVarint(p: &pNode->a[pNode->n], v: nDoclist);
186860 memcpy(dest: &pNode->a[pNode->n], src: aDoclist, n: nDoclist);
186861 pNode->n += nDoclist;
186862 }
186863
186864 assert( pNode->n<=pNode->nAlloc );
186865
186866 return SQLITE_OK;
186867}
186868
186869/*
186870** Append the current term and doclist pointed to by cursor pCsr to the
186871** appendable b-tree segment opened for writing by pWriter.
186872**
186873** Return SQLITE_OK if successful, or an SQLite error code otherwise.
186874*/
186875static int fts3IncrmergeAppend(
186876 Fts3Table *p, /* Fts3 table handle */
186877 IncrmergeWriter *pWriter, /* Writer object */
186878 Fts3MultiSegReader *pCsr /* Cursor containing term and doclist */
186879){
186880 const char *zTerm = pCsr->zTerm;
186881 int nTerm = pCsr->nTerm;
186882 const char *aDoclist = pCsr->aDoclist;
186883 int nDoclist = pCsr->nDoclist;
186884 int rc = SQLITE_OK; /* Return code */
186885 int nSpace; /* Total space in bytes required on leaf */
186886 int nPrefix; /* Size of prefix shared with previous term */
186887 int nSuffix; /* Size of suffix (nTerm - nPrefix) */
186888 NodeWriter *pLeaf; /* Object used to write leaf nodes */
186889
186890 pLeaf = &pWriter->aNodeWriter[0];
186891 nPrefix = fts3PrefixCompress(zPrev: pLeaf->key.a, nPrev: pLeaf->key.n, zNext: zTerm, nNext: nTerm);
186892 nSuffix = nTerm - nPrefix;
186893
186894 nSpace = sqlite3Fts3VarintLen(v: nPrefix);
186895 nSpace += sqlite3Fts3VarintLen(v: nSuffix) + nSuffix;
186896 nSpace += sqlite3Fts3VarintLen(v: nDoclist) + nDoclist;
186897
186898 /* If the current block is not empty, and if adding this term/doclist
186899 ** to the current block would make it larger than Fts3Table.nNodeSize
186900 ** bytes, write this block out to the database. */
186901 if( pLeaf->block.n>0 && (pLeaf->block.n + nSpace)>p->nNodeSize ){
186902 rc = fts3WriteSegment(p, iBlock: pLeaf->iBlock, z: pLeaf->block.a, n: pLeaf->block.n);
186903 pWriter->nWork++;
186904
186905 /* Add the current term to the parent node. The term added to the
186906 ** parent must:
186907 **
186908 ** a) be greater than the largest term on the leaf node just written
186909 ** to the database (still available in pLeaf->key), and
186910 **
186911 ** b) be less than or equal to the term about to be added to the new
186912 ** leaf node (zTerm/nTerm).
186913 **
186914 ** In other words, it must be the prefix of zTerm 1 byte longer than
186915 ** the common prefix (if any) of zTerm and pWriter->zTerm.
186916 */
186917 if( rc==SQLITE_OK ){
186918 rc = fts3IncrmergePush(p, pWriter, zTerm, nTerm: nPrefix+1);
186919 }
186920
186921 /* Advance to the next output block */
186922 pLeaf->iBlock++;
186923 pLeaf->key.n = 0;
186924 pLeaf->block.n = 0;
186925
186926 nSuffix = nTerm;
186927 nSpace = 1;
186928 nSpace += sqlite3Fts3VarintLen(v: nSuffix) + nSuffix;
186929 nSpace += sqlite3Fts3VarintLen(v: nDoclist) + nDoclist;
186930 }
186931
186932 pWriter->nLeafData += nSpace;
186933 blobGrowBuffer(pBlob: &pLeaf->block, nMin: pLeaf->block.n + nSpace, pRc: &rc);
186934 if( rc==SQLITE_OK ){
186935 if( pLeaf->block.n==0 ){
186936 pLeaf->block.n = 1;
186937 pLeaf->block.a[0] = '\0';
186938 }
186939 rc = fts3AppendToNode(
186940 pNode: &pLeaf->block, pPrev: &pLeaf->key, zTerm, nTerm, aDoclist, nDoclist
186941 );
186942 }
186943
186944 return rc;
186945}
186946
186947/*
186948** This function is called to release all dynamic resources held by the
186949** merge-writer object pWriter, and if no error has occurred, to flush
186950** all outstanding node buffers held by pWriter to disk.
186951**
186952** If *pRc is not SQLITE_OK when this function is called, then no attempt
186953** is made to write any data to disk. Instead, this function serves only
186954** to release outstanding resources.
186955**
186956** Otherwise, if *pRc is initially SQLITE_OK and an error occurs while
186957** flushing buffers to disk, *pRc is set to an SQLite error code before
186958** returning.
186959*/
186960static void fts3IncrmergeRelease(
186961 Fts3Table *p, /* FTS3 table handle */
186962 IncrmergeWriter *pWriter, /* Merge-writer object */
186963 int *pRc /* IN/OUT: Error code */
186964){
186965 int i; /* Used to iterate through non-root layers */
186966 int iRoot; /* Index of root in pWriter->aNodeWriter */
186967 NodeWriter *pRoot; /* NodeWriter for root node */
186968 int rc = *pRc; /* Error code */
186969
186970 /* Set iRoot to the index in pWriter->aNodeWriter[] of the output segment
186971 ** root node. If the segment fits entirely on a single leaf node, iRoot
186972 ** will be set to 0. If the root node is the parent of the leaves, iRoot
186973 ** will be 1. And so on. */
186974 for(iRoot=FTS_MAX_APPENDABLE_HEIGHT-1; iRoot>=0; iRoot--){
186975 NodeWriter *pNode = &pWriter->aNodeWriter[iRoot];
186976 if( pNode->block.n>0 ) break;
186977 assert( *pRc || pNode->block.nAlloc==0 );
186978 assert( *pRc || pNode->key.nAlloc==0 );
186979 sqlite3_free(p: pNode->block.a);
186980 sqlite3_free(p: pNode->key.a);
186981 }
186982
186983 /* Empty output segment. This is a no-op. */
186984 if( iRoot<0 ) return;
186985
186986 /* The entire output segment fits on a single node. Normally, this means
186987 ** the node would be stored as a blob in the "root" column of the %_segdir
186988 ** table. However, this is not permitted in this case. The problem is that
186989 ** space has already been reserved in the %_segments table, and so the
186990 ** start_block and end_block fields of the %_segdir table must be populated.
186991 ** And, by design or by accident, released versions of FTS cannot handle
186992 ** segments that fit entirely on the root node with start_block!=0.
186993 **
186994 ** Instead, create a synthetic root node that contains nothing but a
186995 ** pointer to the single content node. So that the segment consists of a
186996 ** single leaf and a single interior (root) node.
186997 **
186998 ** Todo: Better might be to defer allocating space in the %_segments
186999 ** table until we are sure it is needed.
187000 */
187001 if( iRoot==0 ){
187002 Blob *pBlock = &pWriter->aNodeWriter[1].block;
187003 blobGrowBuffer(pBlob: pBlock, nMin: 1 + FTS3_VARINT_MAX, pRc: &rc);
187004 if( rc==SQLITE_OK ){
187005 pBlock->a[0] = 0x01;
187006 pBlock->n = 1 + sqlite3Fts3PutVarint(
187007 p: &pBlock->a[1], v: pWriter->aNodeWriter[0].iBlock
187008 );
187009 }
187010 iRoot = 1;
187011 }
187012 pRoot = &pWriter->aNodeWriter[iRoot];
187013
187014 /* Flush all currently outstanding nodes to disk. */
187015 for(i=0; i<iRoot; i++){
187016 NodeWriter *pNode = &pWriter->aNodeWriter[i];
187017 if( pNode->block.n>0 && rc==SQLITE_OK ){
187018 rc = fts3WriteSegment(p, iBlock: pNode->iBlock, z: pNode->block.a, n: pNode->block.n);
187019 }
187020 sqlite3_free(p: pNode->block.a);
187021 sqlite3_free(p: pNode->key.a);
187022 }
187023
187024 /* Write the %_segdir record. */
187025 if( rc==SQLITE_OK ){
187026 rc = fts3WriteSegdir(p,
187027 iLevel: pWriter->iAbsLevel+1, /* level */
187028 iIdx: pWriter->iIdx, /* idx */
187029 iStartBlock: pWriter->iStart, /* start_block */
187030 iLeafEndBlock: pWriter->aNodeWriter[0].iBlock, /* leaves_end_block */
187031 iEndBlock: pWriter->iEnd, /* end_block */
187032 nLeafData: (pWriter->bNoLeafData==0 ? pWriter->nLeafData : 0), /* end_block */
187033 zRoot: pRoot->block.a, nRoot: pRoot->block.n /* root */
187034 );
187035 }
187036 sqlite3_free(p: pRoot->block.a);
187037 sqlite3_free(p: pRoot->key.a);
187038
187039 *pRc = rc;
187040}
187041
187042/*
187043** Compare the term in buffer zLhs (size in bytes nLhs) with that in
187044** zRhs (size in bytes nRhs) using memcmp. If one term is a prefix of
187045** the other, it is considered to be smaller than the other.
187046**
187047** Return -ve if zLhs is smaller than zRhs, 0 if it is equal, or +ve
187048** if it is greater.
187049*/
187050static int fts3TermCmp(
187051 const char *zLhs, int nLhs, /* LHS of comparison */
187052 const char *zRhs, int nRhs /* RHS of comparison */
187053){
187054 int nCmp = MIN(nLhs, nRhs);
187055 int res;
187056
187057 if( nCmp && ALWAYS(zLhs) && ALWAYS(zRhs) ){
187058 res = memcmp(s1: zLhs, s2: zRhs, n: nCmp);
187059 }else{
187060 res = 0;
187061 }
187062 if( res==0 ) res = nLhs - nRhs;
187063
187064 return res;
187065}
187066
187067
187068/*
187069** Query to see if the entry in the %_segments table with blockid iEnd is
187070** NULL. If no error occurs and the entry is NULL, set *pbRes 1 before
187071** returning. Otherwise, set *pbRes to 0.
187072**
187073** Or, if an error occurs while querying the database, return an SQLite
187074** error code. The final value of *pbRes is undefined in this case.
187075**
187076** This is used to test if a segment is an "appendable" segment. If it
187077** is, then a NULL entry has been inserted into the %_segments table
187078** with blockid %_segdir.end_block.
187079*/
187080static int fts3IsAppendable(Fts3Table *p, sqlite3_int64 iEnd, int *pbRes){
187081 int bRes = 0; /* Result to set *pbRes to */
187082 sqlite3_stmt *pCheck = 0; /* Statement to query database with */
187083 int rc; /* Return code */
187084
187085 rc = fts3SqlStmt(p, SQL_SEGMENT_IS_APPENDABLE, pp: &pCheck, apVal: 0);
187086 if( rc==SQLITE_OK ){
187087 sqlite3_bind_int64(pStmt: pCheck, i: 1, iValue: iEnd);
187088 if( SQLITE_ROW==sqlite3_step(pStmt: pCheck) ) bRes = 1;
187089 rc = sqlite3_reset(pStmt: pCheck);
187090 }
187091
187092 *pbRes = bRes;
187093 return rc;
187094}
187095
187096/*
187097** This function is called when initializing an incremental-merge operation.
187098** It checks if the existing segment with index value iIdx at absolute level
187099** (iAbsLevel+1) can be appended to by the incremental merge. If it can, the
187100** merge-writer object *pWriter is initialized to write to it.
187101**
187102** An existing segment can be appended to by an incremental merge if:
187103**
187104** * It was initially created as an appendable segment (with all required
187105** space pre-allocated), and
187106**
187107** * The first key read from the input (arguments zKey and nKey) is
187108** greater than the largest key currently stored in the potential
187109** output segment.
187110*/
187111static int fts3IncrmergeLoad(
187112 Fts3Table *p, /* Fts3 table handle */
187113 sqlite3_int64 iAbsLevel, /* Absolute level of input segments */
187114 int iIdx, /* Index of candidate output segment */
187115 const char *zKey, /* First key to write */
187116 int nKey, /* Number of bytes in nKey */
187117 IncrmergeWriter *pWriter /* Populate this object */
187118){
187119 int rc; /* Return code */
187120 sqlite3_stmt *pSelect = 0; /* SELECT to read %_segdir entry */
187121
187122 rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, pp: &pSelect, apVal: 0);
187123 if( rc==SQLITE_OK ){
187124 sqlite3_int64 iStart = 0; /* Value of %_segdir.start_block */
187125 sqlite3_int64 iLeafEnd = 0; /* Value of %_segdir.leaves_end_block */
187126 sqlite3_int64 iEnd = 0; /* Value of %_segdir.end_block */
187127 const char *aRoot = 0; /* Pointer to %_segdir.root buffer */
187128 int nRoot = 0; /* Size of aRoot[] in bytes */
187129 int rc2; /* Return code from sqlite3_reset() */
187130 int bAppendable = 0; /* Set to true if segment is appendable */
187131
187132 /* Read the %_segdir entry for index iIdx absolute level (iAbsLevel+1) */
187133 sqlite3_bind_int64(pStmt: pSelect, i: 1, iValue: iAbsLevel+1);
187134 sqlite3_bind_int(p: pSelect, i: 2, iValue: iIdx);
187135 if( sqlite3_step(pStmt: pSelect)==SQLITE_ROW ){
187136 iStart = sqlite3_column_int64(pStmt: pSelect, i: 1);
187137 iLeafEnd = sqlite3_column_int64(pStmt: pSelect, i: 2);
187138 fts3ReadEndBlockField(pStmt: pSelect, iCol: 3, piEndBlock: &iEnd, pnByte: &pWriter->nLeafData);
187139 if( pWriter->nLeafData<0 ){
187140 pWriter->nLeafData = pWriter->nLeafData * -1;
187141 }
187142 pWriter->bNoLeafData = (pWriter->nLeafData==0);
187143 nRoot = sqlite3_column_bytes(pStmt: pSelect, i: 4);
187144 aRoot = sqlite3_column_blob(pStmt: pSelect, i: 4);
187145 if( aRoot==0 ){
187146 sqlite3_reset(pStmt: pSelect);
187147 return nRoot ? SQLITE_NOMEM : FTS_CORRUPT_VTAB;
187148 }
187149 }else{
187150 return sqlite3_reset(pStmt: pSelect);
187151 }
187152
187153 /* Check for the zero-length marker in the %_segments table */
187154 rc = fts3IsAppendable(p, iEnd, pbRes: &bAppendable);
187155
187156 /* Check that zKey/nKey is larger than the largest key the candidate */
187157 if( rc==SQLITE_OK && bAppendable ){
187158 char *aLeaf = 0;
187159 int nLeaf = 0;
187160
187161 rc = sqlite3Fts3ReadBlock(p, iBlockid: iLeafEnd, paBlob: &aLeaf, pnBlob: &nLeaf, pnLoad: 0);
187162 if( rc==SQLITE_OK ){
187163 NodeReader reader;
187164 for(rc = nodeReaderInit(p: &reader, aNode: aLeaf, nNode: nLeaf);
187165 rc==SQLITE_OK && reader.aNode;
187166 rc = nodeReaderNext(p: &reader)
187167 ){
187168 assert( reader.aNode );
187169 }
187170 if( fts3TermCmp(zLhs: zKey, nLhs: nKey, zRhs: reader.term.a, nRhs: reader.term.n)<=0 ){
187171 bAppendable = 0;
187172 }
187173 nodeReaderRelease(p: &reader);
187174 }
187175 sqlite3_free(p: aLeaf);
187176 }
187177
187178 if( rc==SQLITE_OK && bAppendable ){
187179 /* It is possible to append to this segment. Set up the IncrmergeWriter
187180 ** object to do so. */
187181 int i;
187182 int nHeight = (int)aRoot[0];
187183 NodeWriter *pNode;
187184 if( nHeight<1 || nHeight>=FTS_MAX_APPENDABLE_HEIGHT ){
187185 sqlite3_reset(pStmt: pSelect);
187186 return FTS_CORRUPT_VTAB;
187187 }
187188
187189 pWriter->nLeafEst = (int)((iEnd - iStart) + 1)/FTS_MAX_APPENDABLE_HEIGHT;
187190 pWriter->iStart = iStart;
187191 pWriter->iEnd = iEnd;
187192 pWriter->iAbsLevel = iAbsLevel;
187193 pWriter->iIdx = iIdx;
187194
187195 for(i=nHeight+1; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
187196 pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
187197 }
187198
187199 pNode = &pWriter->aNodeWriter[nHeight];
187200 pNode->iBlock = pWriter->iStart + pWriter->nLeafEst*nHeight;
187201 blobGrowBuffer(pBlob: &pNode->block,
187202 MAX(nRoot, p->nNodeSize)+FTS3_NODE_PADDING, pRc: &rc
187203 );
187204 if( rc==SQLITE_OK ){
187205 memcpy(dest: pNode->block.a, src: aRoot, n: nRoot);
187206 pNode->block.n = nRoot;
187207 memset(s: &pNode->block.a[nRoot], c: 0, FTS3_NODE_PADDING);
187208 }
187209
187210 for(i=nHeight; i>=0 && rc==SQLITE_OK; i--){
187211 NodeReader reader;
187212 pNode = &pWriter->aNodeWriter[i];
187213
187214 if( pNode->block.a){
187215 rc = nodeReaderInit(p: &reader, aNode: pNode->block.a, nNode: pNode->block.n);
187216 while( reader.aNode && rc==SQLITE_OK ) rc = nodeReaderNext(p: &reader);
187217 blobGrowBuffer(pBlob: &pNode->key, nMin: reader.term.n, pRc: &rc);
187218 if( rc==SQLITE_OK ){
187219 assert_fts3_nc( reader.term.n>0 || reader.aNode==0 );
187220 if( reader.term.n>0 ){
187221 memcpy(dest: pNode->key.a, src: reader.term.a, n: reader.term.n);
187222 }
187223 pNode->key.n = reader.term.n;
187224 if( i>0 ){
187225 char *aBlock = 0;
187226 int nBlock = 0;
187227 pNode = &pWriter->aNodeWriter[i-1];
187228 pNode->iBlock = reader.iChild;
187229 rc = sqlite3Fts3ReadBlock(p, iBlockid: reader.iChild, paBlob: &aBlock, pnBlob: &nBlock,pnLoad: 0);
187230 blobGrowBuffer(pBlob: &pNode->block,
187231 MAX(nBlock, p->nNodeSize)+FTS3_NODE_PADDING, pRc: &rc
187232 );
187233 if( rc==SQLITE_OK ){
187234 memcpy(dest: pNode->block.a, src: aBlock, n: nBlock);
187235 pNode->block.n = nBlock;
187236 memset(s: &pNode->block.a[nBlock], c: 0, FTS3_NODE_PADDING);
187237 }
187238 sqlite3_free(p: aBlock);
187239 }
187240 }
187241 }
187242 nodeReaderRelease(p: &reader);
187243 }
187244 }
187245
187246 rc2 = sqlite3_reset(pStmt: pSelect);
187247 if( rc==SQLITE_OK ) rc = rc2;
187248 }
187249
187250 return rc;
187251}
187252
187253/*
187254** Determine the largest segment index value that exists within absolute
187255** level iAbsLevel+1. If no error occurs, set *piIdx to this value plus
187256** one before returning SQLITE_OK. Or, if there are no segments at all
187257** within level iAbsLevel, set *piIdx to zero.
187258**
187259** If an error occurs, return an SQLite error code. The final value of
187260** *piIdx is undefined in this case.
187261*/
187262static int fts3IncrmergeOutputIdx(
187263 Fts3Table *p, /* FTS Table handle */
187264 sqlite3_int64 iAbsLevel, /* Absolute index of input segments */
187265 int *piIdx /* OUT: Next free index at iAbsLevel+1 */
187266){
187267 int rc;
187268 sqlite3_stmt *pOutputIdx = 0; /* SQL used to find output index */
187269
187270 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, pp: &pOutputIdx, apVal: 0);
187271 if( rc==SQLITE_OK ){
187272 sqlite3_bind_int64(pStmt: pOutputIdx, i: 1, iValue: iAbsLevel+1);
187273 sqlite3_step(pStmt: pOutputIdx);
187274 *piIdx = sqlite3_column_int(pStmt: pOutputIdx, i: 0);
187275 rc = sqlite3_reset(pStmt: pOutputIdx);
187276 }
187277
187278 return rc;
187279}
187280
187281/*
187282** Allocate an appendable output segment on absolute level iAbsLevel+1
187283** with idx value iIdx.
187284**
187285** In the %_segdir table, a segment is defined by the values in three
187286** columns:
187287**
187288** start_block
187289** leaves_end_block
187290** end_block
187291**
187292** When an appendable segment is allocated, it is estimated that the
187293** maximum number of leaf blocks that may be required is the sum of the
187294** number of leaf blocks consumed by the input segments, plus the number
187295** of input segments, multiplied by two. This value is stored in stack
187296** variable nLeafEst.
187297**
187298** A total of 16*nLeafEst blocks are allocated when an appendable segment
187299** is created ((1 + end_block - start_block)==16*nLeafEst). The contiguous
187300** array of leaf nodes starts at the first block allocated. The array
187301** of interior nodes that are parents of the leaf nodes start at block
187302** (start_block + (1 + end_block - start_block) / 16). And so on.
187303**
187304** In the actual code below, the value "16" is replaced with the
187305** pre-processor macro FTS_MAX_APPENDABLE_HEIGHT.
187306*/
187307static int fts3IncrmergeWriter(
187308 Fts3Table *p, /* Fts3 table handle */
187309 sqlite3_int64 iAbsLevel, /* Absolute level of input segments */
187310 int iIdx, /* Index of new output segment */
187311 Fts3MultiSegReader *pCsr, /* Cursor that data will be read from */
187312 IncrmergeWriter *pWriter /* Populate this object */
187313){
187314 int rc; /* Return Code */
187315 int i; /* Iterator variable */
187316 int nLeafEst = 0; /* Blocks allocated for leaf nodes */
187317 sqlite3_stmt *pLeafEst = 0; /* SQL used to determine nLeafEst */
187318 sqlite3_stmt *pFirstBlock = 0; /* SQL used to determine first block */
187319
187320 /* Calculate nLeafEst. */
187321 rc = fts3SqlStmt(p, SQL_MAX_LEAF_NODE_ESTIMATE, pp: &pLeafEst, apVal: 0);
187322 if( rc==SQLITE_OK ){
187323 sqlite3_bind_int64(pStmt: pLeafEst, i: 1, iValue: iAbsLevel);
187324 sqlite3_bind_int64(pStmt: pLeafEst, i: 2, iValue: pCsr->nSegment);
187325 if( SQLITE_ROW==sqlite3_step(pStmt: pLeafEst) ){
187326 nLeafEst = sqlite3_column_int(pStmt: pLeafEst, i: 0);
187327 }
187328 rc = sqlite3_reset(pStmt: pLeafEst);
187329 }
187330 if( rc!=SQLITE_OK ) return rc;
187331
187332 /* Calculate the first block to use in the output segment */
187333 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, pp: &pFirstBlock, apVal: 0);
187334 if( rc==SQLITE_OK ){
187335 if( SQLITE_ROW==sqlite3_step(pStmt: pFirstBlock) ){
187336 pWriter->iStart = sqlite3_column_int64(pStmt: pFirstBlock, i: 0);
187337 pWriter->iEnd = pWriter->iStart - 1;
187338 pWriter->iEnd += nLeafEst * FTS_MAX_APPENDABLE_HEIGHT;
187339 }
187340 rc = sqlite3_reset(pStmt: pFirstBlock);
187341 }
187342 if( rc!=SQLITE_OK ) return rc;
187343
187344 /* Insert the marker in the %_segments table to make sure nobody tries
187345 ** to steal the space just allocated. This is also used to identify
187346 ** appendable segments. */
187347 rc = fts3WriteSegment(p, iBlock: pWriter->iEnd, z: 0, n: 0);
187348 if( rc!=SQLITE_OK ) return rc;
187349
187350 pWriter->iAbsLevel = iAbsLevel;
187351 pWriter->nLeafEst = nLeafEst;
187352 pWriter->iIdx = iIdx;
187353
187354 /* Set up the array of NodeWriter objects */
187355 for(i=0; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
187356 pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
187357 }
187358 return SQLITE_OK;
187359}
187360
187361/*
187362** Remove an entry from the %_segdir table. This involves running the
187363** following two statements:
187364**
187365** DELETE FROM %_segdir WHERE level = :iAbsLevel AND idx = :iIdx
187366** UPDATE %_segdir SET idx = idx - 1 WHERE level = :iAbsLevel AND idx > :iIdx
187367**
187368** The DELETE statement removes the specific %_segdir level. The UPDATE
187369** statement ensures that the remaining segments have contiguously allocated
187370** idx values.
187371*/
187372static int fts3RemoveSegdirEntry(
187373 Fts3Table *p, /* FTS3 table handle */
187374 sqlite3_int64 iAbsLevel, /* Absolute level to delete from */
187375 int iIdx /* Index of %_segdir entry to delete */
187376){
187377 int rc; /* Return code */
187378 sqlite3_stmt *pDelete = 0; /* DELETE statement */
187379
187380 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_ENTRY, pp: &pDelete, apVal: 0);
187381 if( rc==SQLITE_OK ){
187382 sqlite3_bind_int64(pStmt: pDelete, i: 1, iValue: iAbsLevel);
187383 sqlite3_bind_int(p: pDelete, i: 2, iValue: iIdx);
187384 sqlite3_step(pStmt: pDelete);
187385 rc = sqlite3_reset(pStmt: pDelete);
187386 }
187387
187388 return rc;
187389}
187390
187391/*
187392** One or more segments have just been removed from absolute level iAbsLevel.
187393** Update the 'idx' values of the remaining segments in the level so that
187394** the idx values are a contiguous sequence starting from 0.
187395*/
187396static int fts3RepackSegdirLevel(
187397 Fts3Table *p, /* FTS3 table handle */
187398 sqlite3_int64 iAbsLevel /* Absolute level to repack */
187399){
187400 int rc; /* Return code */
187401 int *aIdx = 0; /* Array of remaining idx values */
187402 int nIdx = 0; /* Valid entries in aIdx[] */
187403 int nAlloc = 0; /* Allocated size of aIdx[] */
187404 int i; /* Iterator variable */
187405 sqlite3_stmt *pSelect = 0; /* Select statement to read idx values */
187406 sqlite3_stmt *pUpdate = 0; /* Update statement to modify idx values */
187407
187408 rc = fts3SqlStmt(p, SQL_SELECT_INDEXES, pp: &pSelect, apVal: 0);
187409 if( rc==SQLITE_OK ){
187410 int rc2;
187411 sqlite3_bind_int64(pStmt: pSelect, i: 1, iValue: iAbsLevel);
187412 while( SQLITE_ROW==sqlite3_step(pStmt: pSelect) ){
187413 if( nIdx>=nAlloc ){
187414 int *aNew;
187415 nAlloc += 16;
187416 aNew = sqlite3_realloc(pOld: aIdx, n: nAlloc*sizeof(int));
187417 if( !aNew ){
187418 rc = SQLITE_NOMEM;
187419 break;
187420 }
187421 aIdx = aNew;
187422 }
187423 aIdx[nIdx++] = sqlite3_column_int(pStmt: pSelect, i: 0);
187424 }
187425 rc2 = sqlite3_reset(pStmt: pSelect);
187426 if( rc==SQLITE_OK ) rc = rc2;
187427 }
187428
187429 if( rc==SQLITE_OK ){
187430 rc = fts3SqlStmt(p, SQL_SHIFT_SEGDIR_ENTRY, pp: &pUpdate, apVal: 0);
187431 }
187432 if( rc==SQLITE_OK ){
187433 sqlite3_bind_int64(pStmt: pUpdate, i: 2, iValue: iAbsLevel);
187434 }
187435
187436 assert( p->bIgnoreSavepoint==0 );
187437 p->bIgnoreSavepoint = 1;
187438 for(i=0; rc==SQLITE_OK && i<nIdx; i++){
187439 if( aIdx[i]!=i ){
187440 sqlite3_bind_int(p: pUpdate, i: 3, iValue: aIdx[i]);
187441 sqlite3_bind_int(p: pUpdate, i: 1, iValue: i);
187442 sqlite3_step(pStmt: pUpdate);
187443 rc = sqlite3_reset(pStmt: pUpdate);
187444 }
187445 }
187446 p->bIgnoreSavepoint = 0;
187447
187448 sqlite3_free(p: aIdx);
187449 return rc;
187450}
187451
187452static void fts3StartNode(Blob *pNode, int iHeight, sqlite3_int64 iChild){
187453 pNode->a[0] = (char)iHeight;
187454 if( iChild ){
187455 assert( pNode->nAlloc>=1+sqlite3Fts3VarintLen(iChild) );
187456 pNode->n = 1 + sqlite3Fts3PutVarint(p: &pNode->a[1], v: iChild);
187457 }else{
187458 assert( pNode->nAlloc>=1 );
187459 pNode->n = 1;
187460 }
187461}
187462
187463/*
187464** The first two arguments are a pointer to and the size of a segment b-tree
187465** node. The node may be a leaf or an internal node.
187466**
187467** This function creates a new node image in blob object *pNew by copying
187468** all terms that are greater than or equal to zTerm/nTerm (for leaf nodes)
187469** or greater than zTerm/nTerm (for internal nodes) from aNode/nNode.
187470*/
187471static int fts3TruncateNode(
187472 const char *aNode, /* Current node image */
187473 int nNode, /* Size of aNode in bytes */
187474 Blob *pNew, /* OUT: Write new node image here */
187475 const char *zTerm, /* Omit all terms smaller than this */
187476 int nTerm, /* Size of zTerm in bytes */
187477 sqlite3_int64 *piBlock /* OUT: Block number in next layer down */
187478){
187479 NodeReader reader; /* Reader object */
187480 Blob prev = {0, 0, 0}; /* Previous term written to new node */
187481 int rc = SQLITE_OK; /* Return code */
187482 int bLeaf; /* True for a leaf node */
187483
187484 if( nNode<1 ) return FTS_CORRUPT_VTAB;
187485 bLeaf = aNode[0]=='\0';
187486
187487 /* Allocate required output space */
187488 blobGrowBuffer(pBlob: pNew, nMin: nNode, pRc: &rc);
187489 if( rc!=SQLITE_OK ) return rc;
187490 pNew->n = 0;
187491
187492 /* Populate new node buffer */
187493 for(rc = nodeReaderInit(p: &reader, aNode, nNode);
187494 rc==SQLITE_OK && reader.aNode;
187495 rc = nodeReaderNext(p: &reader)
187496 ){
187497 if( pNew->n==0 ){
187498 int res = fts3TermCmp(zLhs: reader.term.a, nLhs: reader.term.n, zRhs: zTerm, nRhs: nTerm);
187499 if( res<0 || (bLeaf==0 && res==0) ) continue;
187500 fts3StartNode(pNode: pNew, iHeight: (int)aNode[0], iChild: reader.iChild);
187501 *piBlock = reader.iChild;
187502 }
187503 rc = fts3AppendToNode(
187504 pNode: pNew, pPrev: &prev, zTerm: reader.term.a, nTerm: reader.term.n,
187505 aDoclist: reader.aDoclist, nDoclist: reader.nDoclist
187506 );
187507 if( rc!=SQLITE_OK ) break;
187508 }
187509 if( pNew->n==0 ){
187510 fts3StartNode(pNode: pNew, iHeight: (int)aNode[0], iChild: reader.iChild);
187511 *piBlock = reader.iChild;
187512 }
187513 assert( pNew->n<=pNew->nAlloc );
187514
187515 nodeReaderRelease(p: &reader);
187516 sqlite3_free(p: prev.a);
187517 return rc;
187518}
187519
187520/*
187521** Remove all terms smaller than zTerm/nTerm from segment iIdx in absolute
187522** level iAbsLevel. This may involve deleting entries from the %_segments
187523** table, and modifying existing entries in both the %_segments and %_segdir
187524** tables.
187525**
187526** SQLITE_OK is returned if the segment is updated successfully. Or an
187527** SQLite error code otherwise.
187528*/
187529static int fts3TruncateSegment(
187530 Fts3Table *p, /* FTS3 table handle */
187531 sqlite3_int64 iAbsLevel, /* Absolute level of segment to modify */
187532 int iIdx, /* Index within level of segment to modify */
187533 const char *zTerm, /* Remove terms smaller than this */
187534 int nTerm /* Number of bytes in buffer zTerm */
187535){
187536 int rc = SQLITE_OK; /* Return code */
187537 Blob root = {0,0,0}; /* New root page image */
187538 Blob block = {0,0,0}; /* Buffer used for any other block */
187539 sqlite3_int64 iBlock = 0; /* Block id */
187540 sqlite3_int64 iNewStart = 0; /* New value for iStartBlock */
187541 sqlite3_int64 iOldStart = 0; /* Old value for iStartBlock */
187542 sqlite3_stmt *pFetch = 0; /* Statement used to fetch segdir */
187543
187544 rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, pp: &pFetch, apVal: 0);
187545 if( rc==SQLITE_OK ){
187546 int rc2; /* sqlite3_reset() return code */
187547 sqlite3_bind_int64(pStmt: pFetch, i: 1, iValue: iAbsLevel);
187548 sqlite3_bind_int(p: pFetch, i: 2, iValue: iIdx);
187549 if( SQLITE_ROW==sqlite3_step(pStmt: pFetch) ){
187550 const char *aRoot = sqlite3_column_blob(pStmt: pFetch, i: 4);
187551 int nRoot = sqlite3_column_bytes(pStmt: pFetch, i: 4);
187552 iOldStart = sqlite3_column_int64(pStmt: pFetch, i: 1);
187553 rc = fts3TruncateNode(aNode: aRoot, nNode: nRoot, pNew: &root, zTerm, nTerm, piBlock: &iBlock);
187554 }
187555 rc2 = sqlite3_reset(pStmt: pFetch);
187556 if( rc==SQLITE_OK ) rc = rc2;
187557 }
187558
187559 while( rc==SQLITE_OK && iBlock ){
187560 char *aBlock = 0;
187561 int nBlock = 0;
187562 iNewStart = iBlock;
187563
187564 rc = sqlite3Fts3ReadBlock(p, iBlockid: iBlock, paBlob: &aBlock, pnBlob: &nBlock, pnLoad: 0);
187565 if( rc==SQLITE_OK ){
187566 rc = fts3TruncateNode(aNode: aBlock, nNode: nBlock, pNew: &block, zTerm, nTerm, piBlock: &iBlock);
187567 }
187568 if( rc==SQLITE_OK ){
187569 rc = fts3WriteSegment(p, iBlock: iNewStart, z: block.a, n: block.n);
187570 }
187571 sqlite3_free(p: aBlock);
187572 }
187573
187574 /* Variable iNewStart now contains the first valid leaf node. */
187575 if( rc==SQLITE_OK && iNewStart ){
187576 sqlite3_stmt *pDel = 0;
187577 rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, pp: &pDel, apVal: 0);
187578 if( rc==SQLITE_OK ){
187579 sqlite3_bind_int64(pStmt: pDel, i: 1, iValue: iOldStart);
187580 sqlite3_bind_int64(pStmt: pDel, i: 2, iValue: iNewStart-1);
187581 sqlite3_step(pStmt: pDel);
187582 rc = sqlite3_reset(pStmt: pDel);
187583 }
187584 }
187585
187586 if( rc==SQLITE_OK ){
187587 sqlite3_stmt *pChomp = 0;
187588 rc = fts3SqlStmt(p, SQL_CHOMP_SEGDIR, pp: &pChomp, apVal: 0);
187589 if( rc==SQLITE_OK ){
187590 sqlite3_bind_int64(pStmt: pChomp, i: 1, iValue: iNewStart);
187591 sqlite3_bind_blob(pStmt: pChomp, i: 2, zData: root.a, nData: root.n, SQLITE_STATIC);
187592 sqlite3_bind_int64(pStmt: pChomp, i: 3, iValue: iAbsLevel);
187593 sqlite3_bind_int(p: pChomp, i: 4, iValue: iIdx);
187594 sqlite3_step(pStmt: pChomp);
187595 rc = sqlite3_reset(pStmt: pChomp);
187596 sqlite3_bind_null(pStmt: pChomp, i: 2);
187597 }
187598 }
187599
187600 sqlite3_free(p: root.a);
187601 sqlite3_free(p: block.a);
187602 return rc;
187603}
187604
187605/*
187606** This function is called after an incrmental-merge operation has run to
187607** merge (or partially merge) two or more segments from absolute level
187608** iAbsLevel.
187609**
187610** Each input segment is either removed from the db completely (if all of
187611** its data was copied to the output segment by the incrmerge operation)
187612** or modified in place so that it no longer contains those entries that
187613** have been duplicated in the output segment.
187614*/
187615static int fts3IncrmergeChomp(
187616 Fts3Table *p, /* FTS table handle */
187617 sqlite3_int64 iAbsLevel, /* Absolute level containing segments */
187618 Fts3MultiSegReader *pCsr, /* Chomp all segments opened by this cursor */
187619 int *pnRem /* Number of segments not deleted */
187620){
187621 int i;
187622 int nRem = 0;
187623 int rc = SQLITE_OK;
187624
187625 for(i=pCsr->nSegment-1; i>=0 && rc==SQLITE_OK; i--){
187626 Fts3SegReader *pSeg = 0;
187627 int j;
187628
187629 /* Find the Fts3SegReader object with Fts3SegReader.iIdx==i. It is hiding
187630 ** somewhere in the pCsr->apSegment[] array. */
187631 for(j=0; ALWAYS(j<pCsr->nSegment); j++){
187632 pSeg = pCsr->apSegment[j];
187633 if( pSeg->iIdx==i ) break;
187634 }
187635 assert( j<pCsr->nSegment && pSeg->iIdx==i );
187636
187637 if( pSeg->aNode==0 ){
187638 /* Seg-reader is at EOF. Remove the entire input segment. */
187639 rc = fts3DeleteSegment(p, pSeg);
187640 if( rc==SQLITE_OK ){
187641 rc = fts3RemoveSegdirEntry(p, iAbsLevel, iIdx: pSeg->iIdx);
187642 }
187643 *pnRem = 0;
187644 }else{
187645 /* The incremental merge did not copy all the data from this
187646 ** segment to the upper level. The segment is modified in place
187647 ** so that it contains no keys smaller than zTerm/nTerm. */
187648 const char *zTerm = pSeg->zTerm;
187649 int nTerm = pSeg->nTerm;
187650 rc = fts3TruncateSegment(p, iAbsLevel, iIdx: pSeg->iIdx, zTerm, nTerm);
187651 nRem++;
187652 }
187653 }
187654
187655 if( rc==SQLITE_OK && nRem!=pCsr->nSegment ){
187656 rc = fts3RepackSegdirLevel(p, iAbsLevel);
187657 }
187658
187659 *pnRem = nRem;
187660 return rc;
187661}
187662
187663/*
187664** Store an incr-merge hint in the database.
187665*/
187666static int fts3IncrmergeHintStore(Fts3Table *p, Blob *pHint){
187667 sqlite3_stmt *pReplace = 0;
187668 int rc; /* Return code */
187669
187670 rc = fts3SqlStmt(p, SQL_REPLACE_STAT, pp: &pReplace, apVal: 0);
187671 if( rc==SQLITE_OK ){
187672 sqlite3_bind_int(p: pReplace, i: 1, FTS_STAT_INCRMERGEHINT);
187673 sqlite3_bind_blob(pStmt: pReplace, i: 2, zData: pHint->a, nData: pHint->n, SQLITE_STATIC);
187674 sqlite3_step(pStmt: pReplace);
187675 rc = sqlite3_reset(pStmt: pReplace);
187676 sqlite3_bind_null(pStmt: pReplace, i: 2);
187677 }
187678
187679 return rc;
187680}
187681
187682/*
187683** Load an incr-merge hint from the database. The incr-merge hint, if one
187684** exists, is stored in the rowid==1 row of the %_stat table.
187685**
187686** If successful, populate blob *pHint with the value read from the %_stat
187687** table and return SQLITE_OK. Otherwise, if an error occurs, return an
187688** SQLite error code.
187689*/
187690static int fts3IncrmergeHintLoad(Fts3Table *p, Blob *pHint){
187691 sqlite3_stmt *pSelect = 0;
187692 int rc;
187693
187694 pHint->n = 0;
187695 rc = fts3SqlStmt(p, SQL_SELECT_STAT, pp: &pSelect, apVal: 0);
187696 if( rc==SQLITE_OK ){
187697 int rc2;
187698 sqlite3_bind_int(p: pSelect, i: 1, FTS_STAT_INCRMERGEHINT);
187699 if( SQLITE_ROW==sqlite3_step(pStmt: pSelect) ){
187700 const char *aHint = sqlite3_column_blob(pStmt: pSelect, i: 0);
187701 int nHint = sqlite3_column_bytes(pStmt: pSelect, i: 0);
187702 if( aHint ){
187703 blobGrowBuffer(pBlob: pHint, nMin: nHint, pRc: &rc);
187704 if( rc==SQLITE_OK ){
187705 if( ALWAYS(pHint->a!=0) ) memcpy(dest: pHint->a, src: aHint, n: nHint);
187706 pHint->n = nHint;
187707 }
187708 }
187709 }
187710 rc2 = sqlite3_reset(pStmt: pSelect);
187711 if( rc==SQLITE_OK ) rc = rc2;
187712 }
187713
187714 return rc;
187715}
187716
187717/*
187718** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
187719** Otherwise, append an entry to the hint stored in blob *pHint. Each entry
187720** consists of two varints, the absolute level number of the input segments
187721** and the number of input segments.
187722**
187723** If successful, leave *pRc set to SQLITE_OK and return. If an error occurs,
187724** set *pRc to an SQLite error code before returning.
187725*/
187726static void fts3IncrmergeHintPush(
187727 Blob *pHint, /* Hint blob to append to */
187728 i64 iAbsLevel, /* First varint to store in hint */
187729 int nInput, /* Second varint to store in hint */
187730 int *pRc /* IN/OUT: Error code */
187731){
187732 blobGrowBuffer(pBlob: pHint, nMin: pHint->n + 2*FTS3_VARINT_MAX, pRc);
187733 if( *pRc==SQLITE_OK ){
187734 pHint->n += sqlite3Fts3PutVarint(p: &pHint->a[pHint->n], v: iAbsLevel);
187735 pHint->n += sqlite3Fts3PutVarint(p: &pHint->a[pHint->n], v: (i64)nInput);
187736 }
187737}
187738
187739/*
187740** Read the last entry (most recently pushed) from the hint blob *pHint
187741** and then remove the entry. Write the two values read to *piAbsLevel and
187742** *pnInput before returning.
187743**
187744** If no error occurs, return SQLITE_OK. If the hint blob in *pHint does
187745** not contain at least two valid varints, return SQLITE_CORRUPT_VTAB.
187746*/
187747static int fts3IncrmergeHintPop(Blob *pHint, i64 *piAbsLevel, int *pnInput){
187748 const int nHint = pHint->n;
187749 int i;
187750
187751 i = pHint->n-1;
187752 if( (pHint->a[i] & 0x80) ) return FTS_CORRUPT_VTAB;
187753 while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
187754 if( i==0 ) return FTS_CORRUPT_VTAB;
187755 i--;
187756 while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
187757
187758 pHint->n = i;
187759 i += sqlite3Fts3GetVarint(pBuf: &pHint->a[i], v: piAbsLevel);
187760 i += fts3GetVarint32(&pHint->a[i], pnInput);
187761 assert( i<=nHint );
187762 if( i!=nHint ) return FTS_CORRUPT_VTAB;
187763
187764 return SQLITE_OK;
187765}
187766
187767
187768/*
187769** Attempt an incremental merge that writes nMerge leaf blocks.
187770**
187771** Incremental merges happen nMin segments at a time. The segments
187772** to be merged are the nMin oldest segments (the ones with the smallest
187773** values for the _segdir.idx field) in the highest level that contains
187774** at least nMin segments. Multiple merges might occur in an attempt to
187775** write the quota of nMerge leaf blocks.
187776*/
187777SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){
187778 int rc; /* Return code */
187779 int nRem = nMerge; /* Number of leaf pages yet to be written */
187780 Fts3MultiSegReader *pCsr; /* Cursor used to read input data */
187781 Fts3SegFilter *pFilter; /* Filter used with cursor pCsr */
187782 IncrmergeWriter *pWriter; /* Writer object */
187783 int nSeg = 0; /* Number of input segments */
187784 sqlite3_int64 iAbsLevel = 0; /* Absolute level number to work on */
187785 Blob hint = {0, 0, 0}; /* Hint read from %_stat table */
187786 int bDirtyHint = 0; /* True if blob 'hint' has been modified */
187787
187788 /* Allocate space for the cursor, filter and writer objects */
187789 const int nAlloc = sizeof(*pCsr) + sizeof(*pFilter) + sizeof(*pWriter);
187790 pWriter = (IncrmergeWriter *)sqlite3_malloc(n: nAlloc);
187791 if( !pWriter ) return SQLITE_NOMEM;
187792 pFilter = (Fts3SegFilter *)&pWriter[1];
187793 pCsr = (Fts3MultiSegReader *)&pFilter[1];
187794
187795 rc = fts3IncrmergeHintLoad(p, pHint: &hint);
187796 while( rc==SQLITE_OK && nRem>0 ){
187797 const i64 nMod = FTS3_SEGDIR_MAXLEVEL * p->nIndex;
187798 sqlite3_stmt *pFindLevel = 0; /* SQL used to determine iAbsLevel */
187799 int bUseHint = 0; /* True if attempting to append */
187800 int iIdx = 0; /* Largest idx in level (iAbsLevel+1) */
187801
187802 /* Search the %_segdir table for the absolute level with the smallest
187803 ** relative level number that contains at least nMin segments, if any.
187804 ** If one is found, set iAbsLevel to the absolute level number and
187805 ** nSeg to nMin. If no level with at least nMin segments can be found,
187806 ** set nSeg to -1.
187807 */
187808 rc = fts3SqlStmt(p, SQL_FIND_MERGE_LEVEL, pp: &pFindLevel, apVal: 0);
187809 sqlite3_bind_int(p: pFindLevel, i: 1, MAX(2, nMin));
187810 if( sqlite3_step(pStmt: pFindLevel)==SQLITE_ROW ){
187811 iAbsLevel = sqlite3_column_int64(pStmt: pFindLevel, i: 0);
187812 nSeg = sqlite3_column_int(pStmt: pFindLevel, i: 1);
187813 assert( nSeg>=2 );
187814 }else{
187815 nSeg = -1;
187816 }
187817 rc = sqlite3_reset(pStmt: pFindLevel);
187818
187819 /* If the hint read from the %_stat table is not empty, check if the
187820 ** last entry in it specifies a relative level smaller than or equal
187821 ** to the level identified by the block above (if any). If so, this
187822 ** iteration of the loop will work on merging at the hinted level.
187823 */
187824 if( rc==SQLITE_OK && hint.n ){
187825 int nHint = hint.n;
187826 sqlite3_int64 iHintAbsLevel = 0; /* Hint level */
187827 int nHintSeg = 0; /* Hint number of segments */
187828
187829 rc = fts3IncrmergeHintPop(pHint: &hint, piAbsLevel: &iHintAbsLevel, pnInput: &nHintSeg);
187830 if( nSeg<0 || (iAbsLevel % nMod) >= (iHintAbsLevel % nMod) ){
187831 /* Based on the scan in the block above, it is known that there
187832 ** are no levels with a relative level smaller than that of
187833 ** iAbsLevel with more than nSeg segments, or if nSeg is -1,
187834 ** no levels with more than nMin segments. Use this to limit the
187835 ** value of nHintSeg to avoid a large memory allocation in case the
187836 ** merge-hint is corrupt*/
187837 iAbsLevel = iHintAbsLevel;
187838 nSeg = MIN(MAX(nMin,nSeg), nHintSeg);
187839 bUseHint = 1;
187840 bDirtyHint = 1;
187841 }else{
187842 /* This undoes the effect of the HintPop() above - so that no entry
187843 ** is removed from the hint blob. */
187844 hint.n = nHint;
187845 }
187846 }
187847
187848 /* If nSeg is less that zero, then there is no level with at least
187849 ** nMin segments and no hint in the %_stat table. No work to do.
187850 ** Exit early in this case. */
187851 if( nSeg<=0 ) break;
187852
187853 assert( nMod<=0x7FFFFFFF );
187854 if( iAbsLevel<0 || iAbsLevel>(nMod<<32) ){
187855 rc = FTS_CORRUPT_VTAB;
187856 break;
187857 }
187858
187859 /* Open a cursor to iterate through the contents of the oldest nSeg
187860 ** indexes of absolute level iAbsLevel. If this cursor is opened using
187861 ** the 'hint' parameters, it is possible that there are less than nSeg
187862 ** segments available in level iAbsLevel. In this case, no work is
187863 ** done on iAbsLevel - fall through to the next iteration of the loop
187864 ** to start work on some other level. */
187865 memset(s: pWriter, c: 0, n: nAlloc);
187866 pFilter->flags = FTS3_SEGMENT_REQUIRE_POS;
187867
187868 if( rc==SQLITE_OK ){
187869 rc = fts3IncrmergeOutputIdx(p, iAbsLevel, piIdx: &iIdx);
187870 assert( bUseHint==1 || bUseHint==0 );
187871 if( iIdx==0 || (bUseHint && iIdx==1) ){
187872 int bIgnore = 0;
187873 rc = fts3SegmentIsMaxLevel(p, iAbsLevel: iAbsLevel+1, pbMax: &bIgnore);
187874 if( bIgnore ){
187875 pFilter->flags |= FTS3_SEGMENT_IGNORE_EMPTY;
187876 }
187877 }
187878 }
187879
187880 if( rc==SQLITE_OK ){
187881 rc = fts3IncrmergeCsr(p, iAbsLevel, nSeg, pCsr);
187882 }
187883 if( SQLITE_OK==rc && pCsr->nSegment==nSeg
187884 && SQLITE_OK==(rc = sqlite3Fts3SegReaderStart(p, pCsr, pFilter))
187885 ){
187886 int bEmpty = 0;
187887 rc = sqlite3Fts3SegReaderStep(p, pCsr);
187888 if( rc==SQLITE_OK ){
187889 bEmpty = 1;
187890 }else if( rc!=SQLITE_ROW ){
187891 sqlite3Fts3SegReaderFinish(pCsr);
187892 break;
187893 }
187894 if( bUseHint && iIdx>0 ){
187895 const char *zKey = pCsr->zTerm;
187896 int nKey = pCsr->nTerm;
187897 rc = fts3IncrmergeLoad(p, iAbsLevel, iIdx: iIdx-1, zKey, nKey, pWriter);
187898 }else{
187899 rc = fts3IncrmergeWriter(p, iAbsLevel, iIdx, pCsr, pWriter);
187900 }
187901
187902 if( rc==SQLITE_OK && pWriter->nLeafEst ){
187903 fts3LogMerge(nSeg, iAbsLevel);
187904 if( bEmpty==0 ){
187905 do {
187906 rc = fts3IncrmergeAppend(p, pWriter, pCsr);
187907 if( rc==SQLITE_OK ) rc = sqlite3Fts3SegReaderStep(p, pCsr);
187908 if( pWriter->nWork>=nRem && rc==SQLITE_ROW ) rc = SQLITE_OK;
187909 }while( rc==SQLITE_ROW );
187910 }
187911
187912 /* Update or delete the input segments */
187913 if( rc==SQLITE_OK ){
187914 nRem -= (1 + pWriter->nWork);
187915 rc = fts3IncrmergeChomp(p, iAbsLevel, pCsr, pnRem: &nSeg);
187916 if( nSeg!=0 ){
187917 bDirtyHint = 1;
187918 fts3IncrmergeHintPush(pHint: &hint, iAbsLevel, nInput: nSeg, pRc: &rc);
187919 }
187920 }
187921 }
187922
187923 if( nSeg!=0 ){
187924 pWriter->nLeafData = pWriter->nLeafData * -1;
187925 }
187926 fts3IncrmergeRelease(p, pWriter, pRc: &rc);
187927 if( nSeg==0 && pWriter->bNoLeafData==0 ){
187928 fts3PromoteSegments(p, iAbsLevel: iAbsLevel+1, nByte: pWriter->nLeafData);
187929 }
187930 }
187931
187932 sqlite3Fts3SegReaderFinish(pCsr);
187933 }
187934
187935 /* Write the hint values into the %_stat table for the next incr-merger */
187936 if( bDirtyHint && rc==SQLITE_OK ){
187937 rc = fts3IncrmergeHintStore(p, pHint: &hint);
187938 }
187939
187940 sqlite3_free(p: pWriter);
187941 sqlite3_free(p: hint.a);
187942 return rc;
187943}
187944
187945/*
187946** Convert the text beginning at *pz into an integer and return
187947** its value. Advance *pz to point to the first character past
187948** the integer.
187949**
187950** This function used for parameters to merge= and incrmerge=
187951** commands.
187952*/
187953static int fts3Getint(const char **pz){
187954 const char *z = *pz;
187955 int i = 0;
187956 while( (*z)>='0' && (*z)<='9' && i<214748363 ) i = 10*i + *(z++) - '0';
187957 *pz = z;
187958 return i;
187959}
187960
187961/*
187962** Process statements of the form:
187963**
187964** INSERT INTO table(table) VALUES('merge=A,B');
187965**
187966** A and B are integers that decode to be the number of leaf pages
187967** written for the merge, and the minimum number of segments on a level
187968** before it will be selected for a merge, respectively.
187969*/
187970static int fts3DoIncrmerge(
187971 Fts3Table *p, /* FTS3 table handle */
187972 const char *zParam /* Nul-terminated string containing "A,B" */
187973){
187974 int rc;
187975 int nMin = (MergeCount(p) / 2);
187976 int nMerge = 0;
187977 const char *z = zParam;
187978
187979 /* Read the first integer value */
187980 nMerge = fts3Getint(pz: &z);
187981
187982 /* If the first integer value is followed by a ',', read the second
187983 ** integer value. */
187984 if( z[0]==',' && z[1]!='\0' ){
187985 z++;
187986 nMin = fts3Getint(pz: &z);
187987 }
187988
187989 if( z[0]!='\0' || nMin<2 ){
187990 rc = SQLITE_ERROR;
187991 }else{
187992 rc = SQLITE_OK;
187993 if( !p->bHasStat ){
187994 assert( p->bFts4==0 );
187995 sqlite3Fts3CreateStatTable(pRc: &rc, p);
187996 }
187997 if( rc==SQLITE_OK ){
187998 rc = sqlite3Fts3Incrmerge(p, nMerge, nMin);
187999 }
188000 sqlite3Fts3SegmentsClose(p);
188001 }
188002 return rc;
188003}
188004
188005/*
188006** Process statements of the form:
188007**
188008** INSERT INTO table(table) VALUES('automerge=X');
188009**
188010** where X is an integer. X==0 means to turn automerge off. X!=0 means
188011** turn it on. The setting is persistent.
188012*/
188013static int fts3DoAutoincrmerge(
188014 Fts3Table *p, /* FTS3 table handle */
188015 const char *zParam /* Nul-terminated string containing boolean */
188016){
188017 int rc = SQLITE_OK;
188018 sqlite3_stmt *pStmt = 0;
188019 p->nAutoincrmerge = fts3Getint(pz: &zParam);
188020 if( p->nAutoincrmerge==1 || p->nAutoincrmerge>MergeCount(p) ){
188021 p->nAutoincrmerge = 8;
188022 }
188023 if( !p->bHasStat ){
188024 assert( p->bFts4==0 );
188025 sqlite3Fts3CreateStatTable(pRc: &rc, p);
188026 if( rc ) return rc;
188027 }
188028 rc = fts3SqlStmt(p, SQL_REPLACE_STAT, pp: &pStmt, apVal: 0);
188029 if( rc ) return rc;
188030 sqlite3_bind_int(p: pStmt, i: 1, FTS_STAT_AUTOINCRMERGE);
188031 sqlite3_bind_int(p: pStmt, i: 2, iValue: p->nAutoincrmerge);
188032 sqlite3_step(pStmt);
188033 rc = sqlite3_reset(pStmt);
188034 return rc;
188035}
188036
188037/*
188038** Return a 64-bit checksum for the FTS index entry specified by the
188039** arguments to this function.
188040*/
188041static u64 fts3ChecksumEntry(
188042 const char *zTerm, /* Pointer to buffer containing term */
188043 int nTerm, /* Size of zTerm in bytes */
188044 int iLangid, /* Language id for current row */
188045 int iIndex, /* Index (0..Fts3Table.nIndex-1) */
188046 i64 iDocid, /* Docid for current row. */
188047 int iCol, /* Column number */
188048 int iPos /* Position */
188049){
188050 int i;
188051 u64 ret = (u64)iDocid;
188052
188053 ret += (ret<<3) + iLangid;
188054 ret += (ret<<3) + iIndex;
188055 ret += (ret<<3) + iCol;
188056 ret += (ret<<3) + iPos;
188057 for(i=0; i<nTerm; i++) ret += (ret<<3) + zTerm[i];
188058
188059 return ret;
188060}
188061
188062/*
188063** Return a checksum of all entries in the FTS index that correspond to
188064** language id iLangid. The checksum is calculated by XORing the checksums
188065** of each individual entry (see fts3ChecksumEntry()) together.
188066**
188067** If successful, the checksum value is returned and *pRc set to SQLITE_OK.
188068** Otherwise, if an error occurs, *pRc is set to an SQLite error code. The
188069** return value is undefined in this case.
188070*/
188071static u64 fts3ChecksumIndex(
188072 Fts3Table *p, /* FTS3 table handle */
188073 int iLangid, /* Language id to return cksum for */
188074 int iIndex, /* Index to cksum (0..p->nIndex-1) */
188075 int *pRc /* OUT: Return code */
188076){
188077 Fts3SegFilter filter;
188078 Fts3MultiSegReader csr;
188079 int rc;
188080 u64 cksum = 0;
188081
188082 assert( *pRc==SQLITE_OK );
188083
188084 memset(s: &filter, c: 0, n: sizeof(filter));
188085 memset(s: &csr, c: 0, n: sizeof(csr));
188086 filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
188087 filter.flags |= FTS3_SEGMENT_SCAN;
188088
188089 rc = sqlite3Fts3SegReaderCursor(
188090 p, iLangid, iIndex, FTS3_SEGCURSOR_ALL, zTerm: 0, nTerm: 0, isPrefix: 0, isScan: 1,pCsr: &csr
188091 );
188092 if( rc==SQLITE_OK ){
188093 rc = sqlite3Fts3SegReaderStart(p, pCsr: &csr, pFilter: &filter);
188094 }
188095
188096 if( rc==SQLITE_OK ){
188097 while( SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pCsr: &csr)) ){
188098 char *pCsr = csr.aDoclist;
188099 char *pEnd = &pCsr[csr.nDoclist];
188100
188101 i64 iDocid = 0;
188102 i64 iCol = 0;
188103 u64 iPos = 0;
188104
188105 pCsr += sqlite3Fts3GetVarint(pBuf: pCsr, v: &iDocid);
188106 while( pCsr<pEnd ){
188107 u64 iVal = 0;
188108 pCsr += sqlite3Fts3GetVarintU(pBuf: pCsr, v: &iVal);
188109 if( pCsr<pEnd ){
188110 if( iVal==0 || iVal==1 ){
188111 iCol = 0;
188112 iPos = 0;
188113 if( iVal ){
188114 pCsr += sqlite3Fts3GetVarint(pBuf: pCsr, v: &iCol);
188115 }else{
188116 pCsr += sqlite3Fts3GetVarintU(pBuf: pCsr, v: &iVal);
188117 if( p->bDescIdx ){
188118 iDocid = (i64)((u64)iDocid - iVal);
188119 }else{
188120 iDocid = (i64)((u64)iDocid + iVal);
188121 }
188122 }
188123 }else{
188124 iPos += (iVal - 2);
188125 cksum = cksum ^ fts3ChecksumEntry(
188126 zTerm: csr.zTerm, nTerm: csr.nTerm, iLangid, iIndex, iDocid,
188127 iCol: (int)iCol, iPos: (int)iPos
188128 );
188129 }
188130 }
188131 }
188132 }
188133 }
188134 sqlite3Fts3SegReaderFinish(pCsr: &csr);
188135
188136 *pRc = rc;
188137 return cksum;
188138}
188139
188140/*
188141** Check if the contents of the FTS index match the current contents of the
188142** content table. If no error occurs and the contents do match, set *pbOk
188143** to true and return SQLITE_OK. Or if the contents do not match, set *pbOk
188144** to false before returning.
188145**
188146** If an error occurs (e.g. an OOM or IO error), return an SQLite error
188147** code. The final value of *pbOk is undefined in this case.
188148*/
188149static int fts3IntegrityCheck(Fts3Table *p, int *pbOk){
188150 int rc = SQLITE_OK; /* Return code */
188151 u64 cksum1 = 0; /* Checksum based on FTS index contents */
188152 u64 cksum2 = 0; /* Checksum based on %_content contents */
188153 sqlite3_stmt *pAllLangid = 0; /* Statement to return all language-ids */
188154
188155 /* This block calculates the checksum according to the FTS index. */
188156 rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, pp: &pAllLangid, apVal: 0);
188157 if( rc==SQLITE_OK ){
188158 int rc2;
188159 sqlite3_bind_int(p: pAllLangid, i: 1, iValue: p->iPrevLangid);
188160 sqlite3_bind_int(p: pAllLangid, i: 2, iValue: p->nIndex);
188161 while( rc==SQLITE_OK && sqlite3_step(pStmt: pAllLangid)==SQLITE_ROW ){
188162 int iLangid = sqlite3_column_int(pStmt: pAllLangid, i: 0);
188163 int i;
188164 for(i=0; i<p->nIndex; i++){
188165 cksum1 = cksum1 ^ fts3ChecksumIndex(p, iLangid, iIndex: i, pRc: &rc);
188166 }
188167 }
188168 rc2 = sqlite3_reset(pStmt: pAllLangid);
188169 if( rc==SQLITE_OK ) rc = rc2;
188170 }
188171
188172 /* This block calculates the checksum according to the %_content table */
188173 if( rc==SQLITE_OK ){
188174 sqlite3_tokenizer_module const *pModule = p->pTokenizer->pModule;
188175 sqlite3_stmt *pStmt = 0;
188176 char *zSql;
188177
188178 zSql = sqlite3_mprintf(zFormat: "SELECT %s" , p->zReadExprlist);
188179 if( !zSql ){
188180 rc = SQLITE_NOMEM;
188181 }else{
188182 rc = sqlite3_prepare_v2(db: p->db, zSql, nBytes: -1, ppStmt: &pStmt, pzTail: 0);
188183 sqlite3_free(p: zSql);
188184 }
188185
188186 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
188187 i64 iDocid = sqlite3_column_int64(pStmt, i: 0);
188188 int iLang = langidFromSelect(p, pSelect: pStmt);
188189 int iCol;
188190
188191 for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
188192 if( p->abNotindexed[iCol]==0 ){
188193 const char *zText = (const char *)sqlite3_column_text(pStmt, i: iCol+1);
188194 sqlite3_tokenizer_cursor *pT = 0;
188195
188196 rc = sqlite3Fts3OpenTokenizer(pTokenizer: p->pTokenizer, iLangid: iLang, z: zText, n: -1, ppCsr: &pT);
188197 while( rc==SQLITE_OK ){
188198 char const *zToken; /* Buffer containing token */
188199 int nToken = 0; /* Number of bytes in token */
188200 int iDum1 = 0, iDum2 = 0; /* Dummy variables */
188201 int iPos = 0; /* Position of token in zText */
188202
188203 rc = pModule->xNext(pT, &zToken, &nToken, &iDum1, &iDum2, &iPos);
188204 if( rc==SQLITE_OK ){
188205 int i;
188206 cksum2 = cksum2 ^ fts3ChecksumEntry(
188207 zTerm: zToken, nTerm: nToken, iLangid: iLang, iIndex: 0, iDocid, iCol, iPos
188208 );
188209 for(i=1; i<p->nIndex; i++){
188210 if( p->aIndex[i].nPrefix<=nToken ){
188211 cksum2 = cksum2 ^ fts3ChecksumEntry(
188212 zTerm: zToken, nTerm: p->aIndex[i].nPrefix, iLangid: iLang, iIndex: i, iDocid, iCol, iPos
188213 );
188214 }
188215 }
188216 }
188217 }
188218 if( pT ) pModule->xClose(pT);
188219 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
188220 }
188221 }
188222 }
188223
188224 sqlite3_finalize(pStmt);
188225 }
188226
188227 *pbOk = (cksum1==cksum2);
188228 return rc;
188229}
188230
188231/*
188232** Run the integrity-check. If no error occurs and the current contents of
188233** the FTS index are correct, return SQLITE_OK. Or, if the contents of the
188234** FTS index are incorrect, return SQLITE_CORRUPT_VTAB.
188235**
188236** Or, if an error (e.g. an OOM or IO error) occurs, return an SQLite
188237** error code.
188238**
188239** The integrity-check works as follows. For each token and indexed token
188240** prefix in the document set, a 64-bit checksum is calculated (by code
188241** in fts3ChecksumEntry()) based on the following:
188242**
188243** + The index number (0 for the main index, 1 for the first prefix
188244** index etc.),
188245** + The token (or token prefix) text itself,
188246** + The language-id of the row it appears in,
188247** + The docid of the row it appears in,
188248** + The column it appears in, and
188249** + The tokens position within that column.
188250**
188251** The checksums for all entries in the index are XORed together to create
188252** a single checksum for the entire index.
188253**
188254** The integrity-check code calculates the same checksum in two ways:
188255**
188256** 1. By scanning the contents of the FTS index, and
188257** 2. By scanning and tokenizing the content table.
188258**
188259** If the two checksums are identical, the integrity-check is deemed to have
188260** passed.
188261*/
188262static int fts3DoIntegrityCheck(
188263 Fts3Table *p /* FTS3 table handle */
188264){
188265 int rc;
188266 int bOk = 0;
188267 rc = fts3IntegrityCheck(p, pbOk: &bOk);
188268 if( rc==SQLITE_OK && bOk==0 ) rc = FTS_CORRUPT_VTAB;
188269 return rc;
188270}
188271
188272/*
188273** Handle a 'special' INSERT of the form:
188274**
188275** "INSERT INTO tbl(tbl) VALUES(<expr>)"
188276**
188277** Argument pVal contains the result of <expr>. Currently the only
188278** meaningful value to insert is the text 'optimize'.
188279*/
188280static int fts3SpecialInsert(Fts3Table *p, sqlite3_value *pVal){
188281 int rc = SQLITE_ERROR; /* Return Code */
188282 const char *zVal = (const char *)sqlite3_value_text(pVal);
188283 int nVal = sqlite3_value_bytes(pVal);
188284
188285 if( !zVal ){
188286 return SQLITE_NOMEM;
188287 }else if( nVal==8 && 0==sqlite3_strnicmp(zLeft: zVal, zRight: "optimize", N: 8) ){
188288 rc = fts3DoOptimize(p, bReturnDone: 0);
188289 }else if( nVal==7 && 0==sqlite3_strnicmp(zLeft: zVal, zRight: "rebuild", N: 7) ){
188290 rc = fts3DoRebuild(p);
188291 }else if( nVal==15 && 0==sqlite3_strnicmp(zLeft: zVal, zRight: "integrity-check", N: 15) ){
188292 rc = fts3DoIntegrityCheck(p);
188293 }else if( nVal>6 && 0==sqlite3_strnicmp(zLeft: zVal, zRight: "merge=", N: 6) ){
188294 rc = fts3DoIncrmerge(p, zParam: &zVal[6]);
188295 }else if( nVal>10 && 0==sqlite3_strnicmp(zLeft: zVal, zRight: "automerge=", N: 10) ){
188296 rc = fts3DoAutoincrmerge(p, zParam: &zVal[10]);
188297#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
188298 }else{
188299 int v;
188300 if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){
188301 v = atoi(&zVal[9]);
188302 if( v>=24 && v<=p->nPgsz-35 ) p->nNodeSize = v;
188303 rc = SQLITE_OK;
188304 }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){
188305 v = atoi(&zVal[11]);
188306 if( v>=64 && v<=FTS3_MAX_PENDING_DATA ) p->nMaxPendingData = v;
188307 rc = SQLITE_OK;
188308 }else if( nVal>21 && 0==sqlite3_strnicmp(zVal,"test-no-incr-doclist=",21) ){
188309 p->bNoIncrDoclist = atoi(&zVal[21]);
188310 rc = SQLITE_OK;
188311 }else if( nVal>11 && 0==sqlite3_strnicmp(zVal,"mergecount=",11) ){
188312 v = atoi(&zVal[11]);
188313 if( v>=4 && v<=FTS3_MERGE_COUNT && (v&1)==0 ) p->nMergeCount = v;
188314 rc = SQLITE_OK;
188315 }
188316#endif
188317 }
188318 return rc;
188319}
188320
188321#ifndef SQLITE_DISABLE_FTS4_DEFERRED
188322/*
188323** Delete all cached deferred doclists. Deferred doclists are cached
188324** (allocated) by the sqlite3Fts3CacheDeferredDoclists() function.
188325*/
188326SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *pCsr){
188327 Fts3DeferredToken *pDef;
188328 for(pDef=pCsr->pDeferred; pDef; pDef=pDef->pNext){
188329 fts3PendingListDelete(pList: pDef->pList);
188330 pDef->pList = 0;
188331 }
188332}
188333
188334/*
188335** Free all entries in the pCsr->pDeffered list. Entries are added to
188336** this list using sqlite3Fts3DeferToken().
188337*/
188338SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *pCsr){
188339 Fts3DeferredToken *pDef;
188340 Fts3DeferredToken *pNext;
188341 for(pDef=pCsr->pDeferred; pDef; pDef=pNext){
188342 pNext = pDef->pNext;
188343 fts3PendingListDelete(pList: pDef->pList);
188344 sqlite3_free(p: pDef);
188345 }
188346 pCsr->pDeferred = 0;
188347}
188348
188349/*
188350** Generate deferred-doclists for all tokens in the pCsr->pDeferred list
188351** based on the row that pCsr currently points to.
188352**
188353** A deferred-doclist is like any other doclist with position information
188354** included, except that it only contains entries for a single row of the
188355** table, not for all rows.
188356*/
188357SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *pCsr){
188358 int rc = SQLITE_OK; /* Return code */
188359 if( pCsr->pDeferred ){
188360 int i; /* Used to iterate through table columns */
188361 sqlite3_int64 iDocid; /* Docid of the row pCsr points to */
188362 Fts3DeferredToken *pDef; /* Used to iterate through deferred tokens */
188363
188364 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
188365 sqlite3_tokenizer *pT = p->pTokenizer;
188366 sqlite3_tokenizer_module const *pModule = pT->pModule;
188367
188368 assert( pCsr->isRequireSeek==0 );
188369 iDocid = sqlite3_column_int64(pStmt: pCsr->pStmt, i: 0);
188370
188371 for(i=0; i<p->nColumn && rc==SQLITE_OK; i++){
188372 if( p->abNotindexed[i]==0 ){
188373 const char *zText = (const char *)sqlite3_column_text(pStmt: pCsr->pStmt, i: i+1);
188374 sqlite3_tokenizer_cursor *pTC = 0;
188375
188376 rc = sqlite3Fts3OpenTokenizer(pTokenizer: pT, iLangid: pCsr->iLangid, z: zText, n: -1, ppCsr: &pTC);
188377 while( rc==SQLITE_OK ){
188378 char const *zToken; /* Buffer containing token */
188379 int nToken = 0; /* Number of bytes in token */
188380 int iDum1 = 0, iDum2 = 0; /* Dummy variables */
188381 int iPos = 0; /* Position of token in zText */
188382
188383 rc = pModule->xNext(pTC, &zToken, &nToken, &iDum1, &iDum2, &iPos);
188384 for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
188385 Fts3PhraseToken *pPT = pDef->pToken;
188386 if( (pDef->iCol>=p->nColumn || pDef->iCol==i)
188387 && (pPT->bFirst==0 || iPos==0)
188388 && (pPT->n==nToken || (pPT->isPrefix && pPT->n<nToken))
188389 && (0==memcmp(s1: zToken, s2: pPT->z, n: pPT->n))
188390 ){
188391 fts3PendingListAppend(pp: &pDef->pList, iDocid, iCol: i, iPos, pRc: &rc);
188392 }
188393 }
188394 }
188395 if( pTC ) pModule->xClose(pTC);
188396 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
188397 }
188398 }
188399
188400 for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
188401 if( pDef->pList ){
188402 rc = fts3PendingListAppendVarint(pp: &pDef->pList, i: 0);
188403 }
188404 }
188405 }
188406
188407 return rc;
188408}
188409
188410SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(
188411 Fts3DeferredToken *p,
188412 char **ppData,
188413 int *pnData
188414){
188415 char *pRet;
188416 int nSkip;
188417 sqlite3_int64 dummy;
188418
188419 *ppData = 0;
188420 *pnData = 0;
188421
188422 if( p->pList==0 ){
188423 return SQLITE_OK;
188424 }
188425
188426 pRet = (char *)sqlite3_malloc(n: p->pList->nData);
188427 if( !pRet ) return SQLITE_NOMEM;
188428
188429 nSkip = sqlite3Fts3GetVarint(pBuf: p->pList->aData, v: &dummy);
188430 *pnData = p->pList->nData - nSkip;
188431 *ppData = pRet;
188432
188433 memcpy(dest: pRet, src: &p->pList->aData[nSkip], n: *pnData);
188434 return SQLITE_OK;
188435}
188436
188437/*
188438** Add an entry for token pToken to the pCsr->pDeferred list.
188439*/
188440SQLITE_PRIVATE int sqlite3Fts3DeferToken(
188441 Fts3Cursor *pCsr, /* Fts3 table cursor */
188442 Fts3PhraseToken *pToken, /* Token to defer */
188443 int iCol /* Column that token must appear in (or -1) */
188444){
188445 Fts3DeferredToken *pDeferred;
188446 pDeferred = sqlite3_malloc(n: sizeof(*pDeferred));
188447 if( !pDeferred ){
188448 return SQLITE_NOMEM;
188449 }
188450 memset(s: pDeferred, c: 0, n: sizeof(*pDeferred));
188451 pDeferred->pToken = pToken;
188452 pDeferred->pNext = pCsr->pDeferred;
188453 pDeferred->iCol = iCol;
188454 pCsr->pDeferred = pDeferred;
188455
188456 assert( pToken->pDeferred==0 );
188457 pToken->pDeferred = pDeferred;
188458
188459 return SQLITE_OK;
188460}
188461#endif
188462
188463/*
188464** SQLite value pRowid contains the rowid of a row that may or may not be
188465** present in the FTS3 table. If it is, delete it and adjust the contents
188466** of subsiduary data structures accordingly.
188467*/
188468static int fts3DeleteByRowid(
188469 Fts3Table *p,
188470 sqlite3_value *pRowid,
188471 int *pnChng, /* IN/OUT: Decrement if row is deleted */
188472 u32 *aSzDel
188473){
188474 int rc = SQLITE_OK; /* Return code */
188475 int bFound = 0; /* True if *pRowid really is in the table */
188476
188477 fts3DeleteTerms(pRC: &rc, p, pRowid, aSz: aSzDel, pbFound: &bFound);
188478 if( bFound && rc==SQLITE_OK ){
188479 int isEmpty = 0; /* Deleting *pRowid leaves the table empty */
188480 rc = fts3IsEmpty(p, pRowid, pisEmpty: &isEmpty);
188481 if( rc==SQLITE_OK ){
188482 if( isEmpty ){
188483 /* Deleting this row means the whole table is empty. In this case
188484 ** delete the contents of all three tables and throw away any
188485 ** data in the pendingTerms hash table. */
188486 rc = fts3DeleteAll(p, bContent: 1);
188487 *pnChng = 0;
188488 memset(s: aSzDel, c: 0, n: sizeof(u32) * (p->nColumn+1) * 2);
188489 }else{
188490 *pnChng = *pnChng - 1;
188491 if( p->zContentTbl==0 ){
188492 fts3SqlExec(pRC: &rc, p, SQL_DELETE_CONTENT, apVal: &pRowid);
188493 }
188494 if( p->bHasDocsize ){
188495 fts3SqlExec(pRC: &rc, p, SQL_DELETE_DOCSIZE, apVal: &pRowid);
188496 }
188497 }
188498 }
188499 }
188500
188501 return rc;
188502}
188503
188504/*
188505** This function does the work for the xUpdate method of FTS3 virtual
188506** tables. The schema of the virtual table being:
188507**
188508** CREATE TABLE <table name>(
188509** <user columns>,
188510** <table name> HIDDEN,
188511** docid HIDDEN,
188512** <langid> HIDDEN
188513** );
188514**
188515**
188516*/
188517SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(
188518 sqlite3_vtab *pVtab, /* FTS3 vtab object */
188519 int nArg, /* Size of argument array */
188520 sqlite3_value **apVal, /* Array of arguments */
188521 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
188522){
188523 Fts3Table *p = (Fts3Table *)pVtab;
188524 int rc = SQLITE_OK; /* Return Code */
188525 u32 *aSzIns = 0; /* Sizes of inserted documents */
188526 u32 *aSzDel = 0; /* Sizes of deleted documents */
188527 int nChng = 0; /* Net change in number of documents */
188528 int bInsertDone = 0;
188529
188530 /* At this point it must be known if the %_stat table exists or not.
188531 ** So bHasStat may not be 2. */
188532 assert( p->bHasStat==0 || p->bHasStat==1 );
188533
188534 assert( p->pSegments==0 );
188535 assert(
188536 nArg==1 /* DELETE operations */
188537 || nArg==(2 + p->nColumn + 3) /* INSERT or UPDATE operations */
188538 );
188539
188540 /* Check for a "special" INSERT operation. One of the form:
188541 **
188542 ** INSERT INTO xyz(xyz) VALUES('command');
188543 */
188544 if( nArg>1
188545 && sqlite3_value_type(pVal: apVal[0])==SQLITE_NULL
188546 && sqlite3_value_type(pVal: apVal[p->nColumn+2])!=SQLITE_NULL
188547 ){
188548 rc = fts3SpecialInsert(p, pVal: apVal[p->nColumn+2]);
188549 goto update_out;
188550 }
188551
188552 if( nArg>1 && sqlite3_value_int(pVal: apVal[2 + p->nColumn + 2])<0 ){
188553 rc = SQLITE_CONSTRAINT;
188554 goto update_out;
188555 }
188556
188557 /* Allocate space to hold the change in document sizes */
188558 aSzDel = sqlite3_malloc64(n: sizeof(aSzDel[0])*((sqlite3_int64)p->nColumn+1)*2);
188559 if( aSzDel==0 ){
188560 rc = SQLITE_NOMEM;
188561 goto update_out;
188562 }
188563 aSzIns = &aSzDel[p->nColumn+1];
188564 memset(s: aSzDel, c: 0, n: sizeof(aSzDel[0])*(p->nColumn+1)*2);
188565
188566 rc = fts3Writelock(p);
188567 if( rc!=SQLITE_OK ) goto update_out;
188568
188569 /* If this is an INSERT operation, or an UPDATE that modifies the rowid
188570 ** value, then this operation requires constraint handling.
188571 **
188572 ** If the on-conflict mode is REPLACE, this means that the existing row
188573 ** should be deleted from the database before inserting the new row. Or,
188574 ** if the on-conflict mode is other than REPLACE, then this method must
188575 ** detect the conflict and return SQLITE_CONSTRAINT before beginning to
188576 ** modify the database file.
188577 */
188578 if( nArg>1 && p->zContentTbl==0 ){
188579 /* Find the value object that holds the new rowid value. */
188580 sqlite3_value *pNewRowid = apVal[3+p->nColumn];
188581 if( sqlite3_value_type(pVal: pNewRowid)==SQLITE_NULL ){
188582 pNewRowid = apVal[1];
188583 }
188584
188585 if( sqlite3_value_type(pVal: pNewRowid)!=SQLITE_NULL && (
188586 sqlite3_value_type(pVal: apVal[0])==SQLITE_NULL
188587 || sqlite3_value_int64(pVal: apVal[0])!=sqlite3_value_int64(pVal: pNewRowid)
188588 )){
188589 /* The new rowid is not NULL (in this case the rowid will be
188590 ** automatically assigned and there is no chance of a conflict), and
188591 ** the statement is either an INSERT or an UPDATE that modifies the
188592 ** rowid column. So if the conflict mode is REPLACE, then delete any
188593 ** existing row with rowid=pNewRowid.
188594 **
188595 ** Or, if the conflict mode is not REPLACE, insert the new record into
188596 ** the %_content table. If we hit the duplicate rowid constraint (or any
188597 ** other error) while doing so, return immediately.
188598 **
188599 ** This branch may also run if pNewRowid contains a value that cannot
188600 ** be losslessly converted to an integer. In this case, the eventual
188601 ** call to fts3InsertData() (either just below or further on in this
188602 ** function) will return SQLITE_MISMATCH. If fts3DeleteByRowid is
188603 ** invoked, it will delete zero rows (since no row will have
188604 ** docid=$pNewRowid if $pNewRowid is not an integer value).
188605 */
188606 if( sqlite3_vtab_on_conflict(db: p->db)==SQLITE_REPLACE ){
188607 rc = fts3DeleteByRowid(p, pRowid: pNewRowid, pnChng: &nChng, aSzDel);
188608 }else{
188609 rc = fts3InsertData(p, apVal, piDocid: pRowid);
188610 bInsertDone = 1;
188611 }
188612 }
188613 }
188614 if( rc!=SQLITE_OK ){
188615 goto update_out;
188616 }
188617
188618 /* If this is a DELETE or UPDATE operation, remove the old record. */
188619 if( sqlite3_value_type(pVal: apVal[0])!=SQLITE_NULL ){
188620 assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER );
188621 rc = fts3DeleteByRowid(p, pRowid: apVal[0], pnChng: &nChng, aSzDel);
188622 }
188623
188624 /* If this is an INSERT or UPDATE operation, insert the new record. */
188625 if( nArg>1 && rc==SQLITE_OK ){
188626 int iLangid = sqlite3_value_int(pVal: apVal[2 + p->nColumn + 2]);
188627 if( bInsertDone==0 ){
188628 rc = fts3InsertData(p, apVal, piDocid: pRowid);
188629 if( rc==SQLITE_CONSTRAINT && p->zContentTbl==0 ){
188630 rc = FTS_CORRUPT_VTAB;
188631 }
188632 }
188633 if( rc==SQLITE_OK ){
188634 rc = fts3PendingTermsDocid(p, bDelete: 0, iLangid, iDocid: *pRowid);
188635 }
188636 if( rc==SQLITE_OK ){
188637 assert( p->iPrevDocid==*pRowid );
188638 rc = fts3InsertTerms(p, iLangid, apVal, aSz: aSzIns);
188639 }
188640 if( p->bHasDocsize ){
188641 fts3InsertDocsize(pRC: &rc, p, aSz: aSzIns);
188642 }
188643 nChng++;
188644 }
188645
188646 if( p->bFts4 ){
188647 fts3UpdateDocTotals(pRC: &rc, p, aSzIns, aSzDel, nChng);
188648 }
188649
188650 update_out:
188651 sqlite3_free(p: aSzDel);
188652 sqlite3Fts3SegmentsClose(p);
188653 return rc;
188654}
188655
188656/*
188657** Flush any data in the pending-terms hash table to disk. If successful,
188658** merge all segments in the database (including the new segment, if
188659** there was any data to flush) into a single segment.
188660*/
188661SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *p){
188662 int rc;
188663 rc = sqlite3_exec(db: p->db, zSql: "SAVEPOINT fts3", xCallback: 0, pArg: 0, pzErrMsg: 0);
188664 if( rc==SQLITE_OK ){
188665 rc = fts3DoOptimize(p, bReturnDone: 1);
188666 if( rc==SQLITE_OK || rc==SQLITE_DONE ){
188667 int rc2 = sqlite3_exec(db: p->db, zSql: "RELEASE fts3", xCallback: 0, pArg: 0, pzErrMsg: 0);
188668 if( rc2!=SQLITE_OK ) rc = rc2;
188669 }else{
188670 sqlite3_exec(db: p->db, zSql: "ROLLBACK TO fts3", xCallback: 0, pArg: 0, pzErrMsg: 0);
188671 sqlite3_exec(db: p->db, zSql: "RELEASE fts3", xCallback: 0, pArg: 0, pzErrMsg: 0);
188672 }
188673 }
188674 sqlite3Fts3SegmentsClose(p);
188675 return rc;
188676}
188677
188678#endif
188679
188680/************** End of fts3_write.c ******************************************/
188681/************** Begin file fts3_snippet.c ************************************/
188682/*
188683** 2009 Oct 23
188684**
188685** The author disclaims copyright to this source code. In place of
188686** a legal notice, here is a blessing:
188687**
188688** May you do good and not evil.
188689** May you find forgiveness for yourself and forgive others.
188690** May you share freely, never taking more than you give.
188691**
188692******************************************************************************
188693*/
188694
188695/* #include "fts3Int.h" */
188696#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
188697
188698/* #include <string.h> */
188699/* #include <assert.h> */
188700
188701#ifndef SQLITE_AMALGAMATION
188702typedef sqlite3_int64 i64;
188703#endif
188704
188705/*
188706** Characters that may appear in the second argument to matchinfo().
188707*/
188708#define FTS3_MATCHINFO_NPHRASE 'p' /* 1 value */
188709#define FTS3_MATCHINFO_NCOL 'c' /* 1 value */
188710#define FTS3_MATCHINFO_NDOC 'n' /* 1 value */
188711#define FTS3_MATCHINFO_AVGLENGTH 'a' /* nCol values */
188712#define FTS3_MATCHINFO_LENGTH 'l' /* nCol values */
188713#define FTS3_MATCHINFO_LCS 's' /* nCol values */
188714#define FTS3_MATCHINFO_HITS 'x' /* 3*nCol*nPhrase values */
188715#define FTS3_MATCHINFO_LHITS 'y' /* nCol*nPhrase values */
188716#define FTS3_MATCHINFO_LHITS_BM 'b' /* nCol*nPhrase values */
188717
188718/*
188719** The default value for the second argument to matchinfo().
188720*/
188721#define FTS3_MATCHINFO_DEFAULT "pcx"
188722
188723
188724/*
188725** Used as an fts3ExprIterate() context when loading phrase doclists to
188726** Fts3Expr.aDoclist[]/nDoclist.
188727*/
188728typedef struct LoadDoclistCtx LoadDoclistCtx;
188729struct LoadDoclistCtx {
188730 Fts3Cursor *pCsr; /* FTS3 Cursor */
188731 int nPhrase; /* Number of phrases seen so far */
188732 int nToken; /* Number of tokens seen so far */
188733};
188734
188735/*
188736** The following types are used as part of the implementation of the
188737** fts3BestSnippet() routine.
188738*/
188739typedef struct SnippetIter SnippetIter;
188740typedef struct SnippetPhrase SnippetPhrase;
188741typedef struct SnippetFragment SnippetFragment;
188742
188743struct SnippetIter {
188744 Fts3Cursor *pCsr; /* Cursor snippet is being generated from */
188745 int iCol; /* Extract snippet from this column */
188746 int nSnippet; /* Requested snippet length (in tokens) */
188747 int nPhrase; /* Number of phrases in query */
188748 SnippetPhrase *aPhrase; /* Array of size nPhrase */
188749 int iCurrent; /* First token of current snippet */
188750};
188751
188752struct SnippetPhrase {
188753 int nToken; /* Number of tokens in phrase */
188754 char *pList; /* Pointer to start of phrase position list */
188755 i64 iHead; /* Next value in position list */
188756 char *pHead; /* Position list data following iHead */
188757 i64 iTail; /* Next value in trailing position list */
188758 char *pTail; /* Position list data following iTail */
188759};
188760
188761struct SnippetFragment {
188762 int iCol; /* Column snippet is extracted from */
188763 int iPos; /* Index of first token in snippet */
188764 u64 covered; /* Mask of query phrases covered */
188765 u64 hlmask; /* Mask of snippet terms to highlight */
188766};
188767
188768/*
188769** This type is used as an fts3ExprIterate() context object while
188770** accumulating the data returned by the matchinfo() function.
188771*/
188772typedef struct MatchInfo MatchInfo;
188773struct MatchInfo {
188774 Fts3Cursor *pCursor; /* FTS3 Cursor */
188775 int nCol; /* Number of columns in table */
188776 int nPhrase; /* Number of matchable phrases in query */
188777 sqlite3_int64 nDoc; /* Number of docs in database */
188778 char flag;
188779 u32 *aMatchinfo; /* Pre-allocated buffer */
188780};
188781
188782/*
188783** An instance of this structure is used to manage a pair of buffers, each
188784** (nElem * sizeof(u32)) bytes in size. See the MatchinfoBuffer code below
188785** for details.
188786*/
188787struct MatchinfoBuffer {
188788 u8 aRef[3];
188789 int nElem;
188790 int bGlobal; /* Set if global data is loaded */
188791 char *zMatchinfo;
188792 u32 aMatchinfo[1];
188793};
188794
188795
188796/*
188797** The snippet() and offsets() functions both return text values. An instance
188798** of the following structure is used to accumulate those values while the
188799** functions are running. See fts3StringAppend() for details.
188800*/
188801typedef struct StrBuffer StrBuffer;
188802struct StrBuffer {
188803 char *z; /* Pointer to buffer containing string */
188804 int n; /* Length of z in bytes (excl. nul-term) */
188805 int nAlloc; /* Allocated size of buffer z in bytes */
188806};
188807
188808
188809/*************************************************************************
188810** Start of MatchinfoBuffer code.
188811*/
188812
188813/*
188814** Allocate a two-slot MatchinfoBuffer object.
188815*/
188816static MatchinfoBuffer *fts3MIBufferNew(size_t nElem, const char *zMatchinfo){
188817 MatchinfoBuffer *pRet;
188818 sqlite3_int64 nByte = sizeof(u32) * (2*(sqlite3_int64)nElem + 1)
188819 + sizeof(MatchinfoBuffer);
188820 sqlite3_int64 nStr = strlen(s: zMatchinfo);
188821
188822 pRet = sqlite3Fts3MallocZero(nByte: nByte + nStr+1);
188823 if( pRet ){
188824 pRet->aMatchinfo[0] = (u8*)(&pRet->aMatchinfo[1]) - (u8*)pRet;
188825 pRet->aMatchinfo[1+nElem] = pRet->aMatchinfo[0]
188826 + sizeof(u32)*((int)nElem+1);
188827 pRet->nElem = (int)nElem;
188828 pRet->zMatchinfo = ((char*)pRet) + nByte;
188829 memcpy(dest: pRet->zMatchinfo, src: zMatchinfo, n: nStr+1);
188830 pRet->aRef[0] = 1;
188831 }
188832
188833 return pRet;
188834}
188835
188836static void fts3MIBufferFree(void *p){
188837 MatchinfoBuffer *pBuf = (MatchinfoBuffer*)((u8*)p - ((u32*)p)[-1]);
188838
188839 assert( (u32*)p==&pBuf->aMatchinfo[1]
188840 || (u32*)p==&pBuf->aMatchinfo[pBuf->nElem+2]
188841 );
188842 if( (u32*)p==&pBuf->aMatchinfo[1] ){
188843 pBuf->aRef[1] = 0;
188844 }else{
188845 pBuf->aRef[2] = 0;
188846 }
188847
188848 if( pBuf->aRef[0]==0 && pBuf->aRef[1]==0 && pBuf->aRef[2]==0 ){
188849 sqlite3_free(p: pBuf);
188850 }
188851}
188852
188853static void (*fts3MIBufferAlloc(MatchinfoBuffer *p, u32 **paOut))(void*){
188854 void (*xRet)(void*) = 0;
188855 u32 *aOut = 0;
188856
188857 if( p->aRef[1]==0 ){
188858 p->aRef[1] = 1;
188859 aOut = &p->aMatchinfo[1];
188860 xRet = fts3MIBufferFree;
188861 }
188862 else if( p->aRef[2]==0 ){
188863 p->aRef[2] = 1;
188864 aOut = &p->aMatchinfo[p->nElem+2];
188865 xRet = fts3MIBufferFree;
188866 }else{
188867 aOut = (u32*)sqlite3_malloc64(n: p->nElem * sizeof(u32));
188868 if( aOut ){
188869 xRet = sqlite3_free;
188870 if( p->bGlobal ) memcpy(dest: aOut, src: &p->aMatchinfo[1], n: p->nElem*sizeof(u32));
188871 }
188872 }
188873
188874 *paOut = aOut;
188875 return xRet;
188876}
188877
188878static void fts3MIBufferSetGlobal(MatchinfoBuffer *p){
188879 p->bGlobal = 1;
188880 memcpy(dest: &p->aMatchinfo[2+p->nElem], src: &p->aMatchinfo[1], n: p->nElem*sizeof(u32));
188881}
188882
188883/*
188884** Free a MatchinfoBuffer object allocated using fts3MIBufferNew()
188885*/
188886SQLITE_PRIVATE void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p){
188887 if( p ){
188888 assert( p->aRef[0]==1 );
188889 p->aRef[0] = 0;
188890 if( p->aRef[0]==0 && p->aRef[1]==0 && p->aRef[2]==0 ){
188891 sqlite3_free(p);
188892 }
188893 }
188894}
188895
188896/*
188897** End of MatchinfoBuffer code.
188898*************************************************************************/
188899
188900
188901/*
188902** This function is used to help iterate through a position-list. A position
188903** list is a list of unique integers, sorted from smallest to largest. Each
188904** element of the list is represented by an FTS3 varint that takes the value
188905** of the difference between the current element and the previous one plus
188906** two. For example, to store the position-list:
188907**
188908** 4 9 113
188909**
188910** the three varints:
188911**
188912** 6 7 106
188913**
188914** are encoded.
188915**
188916** When this function is called, *pp points to the start of an element of
188917** the list. *piPos contains the value of the previous entry in the list.
188918** After it returns, *piPos contains the value of the next element of the
188919** list and *pp is advanced to the following varint.
188920*/
188921static void fts3GetDeltaPosition(char **pp, i64 *piPos){
188922 int iVal;
188923 *pp += fts3GetVarint32(*pp, &iVal);
188924 *piPos += (iVal-2);
188925}
188926
188927/*
188928** Helper function for fts3ExprIterate() (see below).
188929*/
188930static int fts3ExprIterate2(
188931 Fts3Expr *pExpr, /* Expression to iterate phrases of */
188932 int *piPhrase, /* Pointer to phrase counter */
188933 int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */
188934 void *pCtx /* Second argument to pass to callback */
188935){
188936 int rc; /* Return code */
188937 int eType = pExpr->eType; /* Type of expression node pExpr */
188938
188939 if( eType!=FTSQUERY_PHRASE ){
188940 assert( pExpr->pLeft && pExpr->pRight );
188941 rc = fts3ExprIterate2(pExpr: pExpr->pLeft, piPhrase, x, pCtx);
188942 if( rc==SQLITE_OK && eType!=FTSQUERY_NOT ){
188943 rc = fts3ExprIterate2(pExpr: pExpr->pRight, piPhrase, x, pCtx);
188944 }
188945 }else{
188946 rc = x(pExpr, *piPhrase, pCtx);
188947 (*piPhrase)++;
188948 }
188949 return rc;
188950}
188951
188952/*
188953** Iterate through all phrase nodes in an FTS3 query, except those that
188954** are part of a sub-tree that is the right-hand-side of a NOT operator.
188955** For each phrase node found, the supplied callback function is invoked.
188956**
188957** If the callback function returns anything other than SQLITE_OK,
188958** the iteration is abandoned and the error code returned immediately.
188959** Otherwise, SQLITE_OK is returned after a callback has been made for
188960** all eligible phrase nodes.
188961*/
188962static int fts3ExprIterate(
188963 Fts3Expr *pExpr, /* Expression to iterate phrases of */
188964 int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */
188965 void *pCtx /* Second argument to pass to callback */
188966){
188967 int iPhrase = 0; /* Variable used as the phrase counter */
188968 return fts3ExprIterate2(pExpr, piPhrase: &iPhrase, x, pCtx);
188969}
188970
188971
188972/*
188973** This is an fts3ExprIterate() callback used while loading the doclists
188974** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also
188975** fts3ExprLoadDoclists().
188976*/
188977static int fts3ExprLoadDoclistsCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
188978 int rc = SQLITE_OK;
188979 Fts3Phrase *pPhrase = pExpr->pPhrase;
188980 LoadDoclistCtx *p = (LoadDoclistCtx *)ctx;
188981
188982 UNUSED_PARAMETER(iPhrase);
188983
188984 p->nPhrase++;
188985 p->nToken += pPhrase->nToken;
188986
188987 return rc;
188988}
188989
188990/*
188991** Load the doclists for each phrase in the query associated with FTS3 cursor
188992** pCsr.
188993**
188994** If pnPhrase is not NULL, then *pnPhrase is set to the number of matchable
188995** phrases in the expression (all phrases except those directly or
188996** indirectly descended from the right-hand-side of a NOT operator). If
188997** pnToken is not NULL, then it is set to the number of tokens in all
188998** matchable phrases of the expression.
188999*/
189000static int fts3ExprLoadDoclists(
189001 Fts3Cursor *pCsr, /* Fts3 cursor for current query */
189002 int *pnPhrase, /* OUT: Number of phrases in query */
189003 int *pnToken /* OUT: Number of tokens in query */
189004){
189005 int rc; /* Return Code */
189006 LoadDoclistCtx sCtx = {0,0,0}; /* Context for fts3ExprIterate() */
189007 sCtx.pCsr = pCsr;
189008 rc = fts3ExprIterate(pExpr: pCsr->pExpr, x: fts3ExprLoadDoclistsCb, pCtx: (void *)&sCtx);
189009 if( pnPhrase ) *pnPhrase = sCtx.nPhrase;
189010 if( pnToken ) *pnToken = sCtx.nToken;
189011 return rc;
189012}
189013
189014static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
189015 (*(int *)ctx)++;
189016 pExpr->iPhrase = iPhrase;
189017 return SQLITE_OK;
189018}
189019static int fts3ExprPhraseCount(Fts3Expr *pExpr){
189020 int nPhrase = 0;
189021 (void)fts3ExprIterate(pExpr, x: fts3ExprPhraseCountCb, pCtx: (void *)&nPhrase);
189022 return nPhrase;
189023}
189024
189025/*
189026** Advance the position list iterator specified by the first two
189027** arguments so that it points to the first element with a value greater
189028** than or equal to parameter iNext.
189029*/
189030static void fts3SnippetAdvance(char **ppIter, i64 *piIter, int iNext){
189031 char *pIter = *ppIter;
189032 if( pIter ){
189033 i64 iIter = *piIter;
189034
189035 while( iIter<iNext ){
189036 if( 0==(*pIter & 0xFE) ){
189037 iIter = -1;
189038 pIter = 0;
189039 break;
189040 }
189041 fts3GetDeltaPosition(pp: &pIter, piPos: &iIter);
189042 }
189043
189044 *piIter = iIter;
189045 *ppIter = pIter;
189046 }
189047}
189048
189049/*
189050** Advance the snippet iterator to the next candidate snippet.
189051*/
189052static int fts3SnippetNextCandidate(SnippetIter *pIter){
189053 int i; /* Loop counter */
189054
189055 if( pIter->iCurrent<0 ){
189056 /* The SnippetIter object has just been initialized. The first snippet
189057 ** candidate always starts at offset 0 (even if this candidate has a
189058 ** score of 0.0).
189059 */
189060 pIter->iCurrent = 0;
189061
189062 /* Advance the 'head' iterator of each phrase to the first offset that
189063 ** is greater than or equal to (iNext+nSnippet).
189064 */
189065 for(i=0; i<pIter->nPhrase; i++){
189066 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
189067 fts3SnippetAdvance(ppIter: &pPhrase->pHead, piIter: &pPhrase->iHead, iNext: pIter->nSnippet);
189068 }
189069 }else{
189070 int iStart;
189071 int iEnd = 0x7FFFFFFF;
189072
189073 for(i=0; i<pIter->nPhrase; i++){
189074 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
189075 if( pPhrase->pHead && pPhrase->iHead<iEnd ){
189076 iEnd = pPhrase->iHead;
189077 }
189078 }
189079 if( iEnd==0x7FFFFFFF ){
189080 return 1;
189081 }
189082
189083 pIter->iCurrent = iStart = iEnd - pIter->nSnippet + 1;
189084 for(i=0; i<pIter->nPhrase; i++){
189085 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
189086 fts3SnippetAdvance(ppIter: &pPhrase->pHead, piIter: &pPhrase->iHead, iNext: iEnd+1);
189087 fts3SnippetAdvance(ppIter: &pPhrase->pTail, piIter: &pPhrase->iTail, iNext: iStart);
189088 }
189089 }
189090
189091 return 0;
189092}
189093
189094/*
189095** Retrieve information about the current candidate snippet of snippet
189096** iterator pIter.
189097*/
189098static void fts3SnippetDetails(
189099 SnippetIter *pIter, /* Snippet iterator */
189100 u64 mCovered, /* Bitmask of phrases already covered */
189101 int *piToken, /* OUT: First token of proposed snippet */
189102 int *piScore, /* OUT: "Score" for this snippet */
189103 u64 *pmCover, /* OUT: Bitmask of phrases covered */
189104 u64 *pmHighlight /* OUT: Bitmask of terms to highlight */
189105){
189106 int iStart = pIter->iCurrent; /* First token of snippet */
189107 int iScore = 0; /* Score of this snippet */
189108 int i; /* Loop counter */
189109 u64 mCover = 0; /* Mask of phrases covered by this snippet */
189110 u64 mHighlight = 0; /* Mask of tokens to highlight in snippet */
189111
189112 for(i=0; i<pIter->nPhrase; i++){
189113 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
189114 if( pPhrase->pTail ){
189115 char *pCsr = pPhrase->pTail;
189116 i64 iCsr = pPhrase->iTail;
189117
189118 while( iCsr<(iStart+pIter->nSnippet) && iCsr>=iStart ){
189119 int j;
189120 u64 mPhrase = (u64)1 << (i%64);
189121 u64 mPos = (u64)1 << (iCsr - iStart);
189122 assert( iCsr>=iStart && (iCsr - iStart)<=64 );
189123 assert( i>=0 );
189124 if( (mCover|mCovered)&mPhrase ){
189125 iScore++;
189126 }else{
189127 iScore += 1000;
189128 }
189129 mCover |= mPhrase;
189130
189131 for(j=0; j<pPhrase->nToken; j++){
189132 mHighlight |= (mPos>>j);
189133 }
189134
189135 if( 0==(*pCsr & 0x0FE) ) break;
189136 fts3GetDeltaPosition(pp: &pCsr, piPos: &iCsr);
189137 }
189138 }
189139 }
189140
189141 /* Set the output variables before returning. */
189142 *piToken = iStart;
189143 *piScore = iScore;
189144 *pmCover = mCover;
189145 *pmHighlight = mHighlight;
189146}
189147
189148/*
189149** This function is an fts3ExprIterate() callback used by fts3BestSnippet().
189150** Each invocation populates an element of the SnippetIter.aPhrase[] array.
189151*/
189152static int fts3SnippetFindPositions(Fts3Expr *pExpr, int iPhrase, void *ctx){
189153 SnippetIter *p = (SnippetIter *)ctx;
189154 SnippetPhrase *pPhrase = &p->aPhrase[iPhrase];
189155 char *pCsr;
189156 int rc;
189157
189158 pPhrase->nToken = pExpr->pPhrase->nToken;
189159 rc = sqlite3Fts3EvalPhrasePoslist(pCsr: p->pCsr, pExpr, iCol: p->iCol, ppOut: &pCsr);
189160 assert( rc==SQLITE_OK || pCsr==0 );
189161 if( pCsr ){
189162 i64 iFirst = 0;
189163 pPhrase->pList = pCsr;
189164 fts3GetDeltaPosition(pp: &pCsr, piPos: &iFirst);
189165 if( iFirst<0 ){
189166 rc = FTS_CORRUPT_VTAB;
189167 }else{
189168 pPhrase->pHead = pCsr;
189169 pPhrase->pTail = pCsr;
189170 pPhrase->iHead = iFirst;
189171 pPhrase->iTail = iFirst;
189172 }
189173 }else{
189174 assert( rc!=SQLITE_OK || (
189175 pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0
189176 ));
189177 }
189178
189179 return rc;
189180}
189181
189182/*
189183** Select the fragment of text consisting of nFragment contiguous tokens
189184** from column iCol that represent the "best" snippet. The best snippet
189185** is the snippet with the highest score, where scores are calculated
189186** by adding:
189187**
189188** (a) +1 point for each occurrence of a matchable phrase in the snippet.
189189**
189190** (b) +1000 points for the first occurrence of each matchable phrase in
189191** the snippet for which the corresponding mCovered bit is not set.
189192**
189193** The selected snippet parameters are stored in structure *pFragment before
189194** returning. The score of the selected snippet is stored in *piScore
189195** before returning.
189196*/
189197static int fts3BestSnippet(
189198 int nSnippet, /* Desired snippet length */
189199 Fts3Cursor *pCsr, /* Cursor to create snippet for */
189200 int iCol, /* Index of column to create snippet from */
189201 u64 mCovered, /* Mask of phrases already covered */
189202 u64 *pmSeen, /* IN/OUT: Mask of phrases seen */
189203 SnippetFragment *pFragment, /* OUT: Best snippet found */
189204 int *piScore /* OUT: Score of snippet pFragment */
189205){
189206 int rc; /* Return Code */
189207 int nList; /* Number of phrases in expression */
189208 SnippetIter sIter; /* Iterates through snippet candidates */
189209 sqlite3_int64 nByte; /* Number of bytes of space to allocate */
189210 int iBestScore = -1; /* Best snippet score found so far */
189211 int i; /* Loop counter */
189212
189213 memset(s: &sIter, c: 0, n: sizeof(sIter));
189214
189215 /* Iterate through the phrases in the expression to count them. The same
189216 ** callback makes sure the doclists are loaded for each phrase.
189217 */
189218 rc = fts3ExprLoadDoclists(pCsr, pnPhrase: &nList, pnToken: 0);
189219 if( rc!=SQLITE_OK ){
189220 return rc;
189221 }
189222
189223 /* Now that it is known how many phrases there are, allocate and zero
189224 ** the required space using malloc().
189225 */
189226 nByte = sizeof(SnippetPhrase) * nList;
189227 sIter.aPhrase = (SnippetPhrase *)sqlite3Fts3MallocZero(nByte);
189228 if( !sIter.aPhrase ){
189229 return SQLITE_NOMEM;
189230 }
189231
189232 /* Initialize the contents of the SnippetIter object. Then iterate through
189233 ** the set of phrases in the expression to populate the aPhrase[] array.
189234 */
189235 sIter.pCsr = pCsr;
189236 sIter.iCol = iCol;
189237 sIter.nSnippet = nSnippet;
189238 sIter.nPhrase = nList;
189239 sIter.iCurrent = -1;
189240 rc = fts3ExprIterate(pExpr: pCsr->pExpr, x: fts3SnippetFindPositions, pCtx: (void*)&sIter);
189241 if( rc==SQLITE_OK ){
189242
189243 /* Set the *pmSeen output variable. */
189244 for(i=0; i<nList; i++){
189245 if( sIter.aPhrase[i].pHead ){
189246 *pmSeen |= (u64)1 << (i%64);
189247 }
189248 }
189249
189250 /* Loop through all candidate snippets. Store the best snippet in
189251 ** *pFragment. Store its associated 'score' in iBestScore.
189252 */
189253 pFragment->iCol = iCol;
189254 while( !fts3SnippetNextCandidate(pIter: &sIter) ){
189255 int iPos;
189256 int iScore;
189257 u64 mCover;
189258 u64 mHighlite;
189259 fts3SnippetDetails(pIter: &sIter, mCovered, piToken: &iPos, piScore: &iScore, pmCover: &mCover,pmHighlight: &mHighlite);
189260 assert( iScore>=0 );
189261 if( iScore>iBestScore ){
189262 pFragment->iPos = iPos;
189263 pFragment->hlmask = mHighlite;
189264 pFragment->covered = mCover;
189265 iBestScore = iScore;
189266 }
189267 }
189268
189269 *piScore = iBestScore;
189270 }
189271 sqlite3_free(p: sIter.aPhrase);
189272 return rc;
189273}
189274
189275
189276/*
189277** Append a string to the string-buffer passed as the first argument.
189278**
189279** If nAppend is negative, then the length of the string zAppend is
189280** determined using strlen().
189281*/
189282static int fts3StringAppend(
189283 StrBuffer *pStr, /* Buffer to append to */
189284 const char *zAppend, /* Pointer to data to append to buffer */
189285 int nAppend /* Size of zAppend in bytes (or -1) */
189286){
189287 if( nAppend<0 ){
189288 nAppend = (int)strlen(s: zAppend);
189289 }
189290
189291 /* If there is insufficient space allocated at StrBuffer.z, use realloc()
189292 ** to grow the buffer until so that it is big enough to accomadate the
189293 ** appended data.
189294 */
189295 if( pStr->n+nAppend+1>=pStr->nAlloc ){
189296 sqlite3_int64 nAlloc = pStr->nAlloc+(sqlite3_int64)nAppend+100;
189297 char *zNew = sqlite3_realloc64(pOld: pStr->z, n: nAlloc);
189298 if( !zNew ){
189299 return SQLITE_NOMEM;
189300 }
189301 pStr->z = zNew;
189302 pStr->nAlloc = nAlloc;
189303 }
189304 assert( pStr->z!=0 && (pStr->nAlloc >= pStr->n+nAppend+1) );
189305
189306 /* Append the data to the string buffer. */
189307 memcpy(dest: &pStr->z[pStr->n], src: zAppend, n: nAppend);
189308 pStr->n += nAppend;
189309 pStr->z[pStr->n] = '\0';
189310
189311 return SQLITE_OK;
189312}
189313
189314/*
189315** The fts3BestSnippet() function often selects snippets that end with a
189316** query term. That is, the final term of the snippet is always a term
189317** that requires highlighting. For example, if 'X' is a highlighted term
189318** and '.' is a non-highlighted term, BestSnippet() may select:
189319**
189320** ........X.....X
189321**
189322** This function "shifts" the beginning of the snippet forward in the
189323** document so that there are approximately the same number of
189324** non-highlighted terms to the right of the final highlighted term as there
189325** are to the left of the first highlighted term. For example, to this:
189326**
189327** ....X.....X....
189328**
189329** This is done as part of extracting the snippet text, not when selecting
189330** the snippet. Snippet selection is done based on doclists only, so there
189331** is no way for fts3BestSnippet() to know whether or not the document
189332** actually contains terms that follow the final highlighted term.
189333*/
189334static int fts3SnippetShift(
189335 Fts3Table *pTab, /* FTS3 table snippet comes from */
189336 int iLangid, /* Language id to use in tokenizing */
189337 int nSnippet, /* Number of tokens desired for snippet */
189338 const char *zDoc, /* Document text to extract snippet from */
189339 int nDoc, /* Size of buffer zDoc in bytes */
189340 int *piPos, /* IN/OUT: First token of snippet */
189341 u64 *pHlmask /* IN/OUT: Mask of tokens to highlight */
189342){
189343 u64 hlmask = *pHlmask; /* Local copy of initial highlight-mask */
189344
189345 if( hlmask ){
189346 int nLeft; /* Tokens to the left of first highlight */
189347 int nRight; /* Tokens to the right of last highlight */
189348 int nDesired; /* Ideal number of tokens to shift forward */
189349
189350 for(nLeft=0; !(hlmask & ((u64)1 << nLeft)); nLeft++);
189351 for(nRight=0; !(hlmask & ((u64)1 << (nSnippet-1-nRight))); nRight++);
189352 assert( (nSnippet-1-nRight)<=63 && (nSnippet-1-nRight)>=0 );
189353 nDesired = (nLeft-nRight)/2;
189354
189355 /* Ideally, the start of the snippet should be pushed forward in the
189356 ** document nDesired tokens. This block checks if there are actually
189357 ** nDesired tokens to the right of the snippet. If so, *piPos and
189358 ** *pHlMask are updated to shift the snippet nDesired tokens to the
189359 ** right. Otherwise, the snippet is shifted by the number of tokens
189360 ** available.
189361 */
189362 if( nDesired>0 ){
189363 int nShift; /* Number of tokens to shift snippet by */
189364 int iCurrent = 0; /* Token counter */
189365 int rc; /* Return Code */
189366 sqlite3_tokenizer_module *pMod;
189367 sqlite3_tokenizer_cursor *pC;
189368 pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
189369
189370 /* Open a cursor on zDoc/nDoc. Check if there are (nSnippet+nDesired)
189371 ** or more tokens in zDoc/nDoc.
189372 */
189373 rc = sqlite3Fts3OpenTokenizer(pTokenizer: pTab->pTokenizer, iLangid, z: zDoc, n: nDoc, ppCsr: &pC);
189374 if( rc!=SQLITE_OK ){
189375 return rc;
189376 }
189377 while( rc==SQLITE_OK && iCurrent<(nSnippet+nDesired) ){
189378 const char *ZDUMMY; int DUMMY1 = 0, DUMMY2 = 0, DUMMY3 = 0;
189379 rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &DUMMY2, &DUMMY3, &iCurrent);
189380 }
189381 pMod->xClose(pC);
189382 if( rc!=SQLITE_OK && rc!=SQLITE_DONE ){ return rc; }
189383
189384 nShift = (rc==SQLITE_DONE)+iCurrent-nSnippet;
189385 assert( nShift<=nDesired );
189386 if( nShift>0 ){
189387 *piPos += nShift;
189388 *pHlmask = hlmask >> nShift;
189389 }
189390 }
189391 }
189392 return SQLITE_OK;
189393}
189394
189395/*
189396** Extract the snippet text for fragment pFragment from cursor pCsr and
189397** append it to string buffer pOut.
189398*/
189399static int fts3SnippetText(
189400 Fts3Cursor *pCsr, /* FTS3 Cursor */
189401 SnippetFragment *pFragment, /* Snippet to extract */
189402 int iFragment, /* Fragment number */
189403 int isLast, /* True for final fragment in snippet */
189404 int nSnippet, /* Number of tokens in extracted snippet */
189405 const char *zOpen, /* String inserted before highlighted term */
189406 const char *zClose, /* String inserted after highlighted term */
189407 const char *zEllipsis, /* String inserted between snippets */
189408 StrBuffer *pOut /* Write output here */
189409){
189410 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
189411 int rc; /* Return code */
189412 const char *zDoc; /* Document text to extract snippet from */
189413 int nDoc; /* Size of zDoc in bytes */
189414 int iCurrent = 0; /* Current token number of document */
189415 int iEnd = 0; /* Byte offset of end of current token */
189416 int isShiftDone = 0; /* True after snippet is shifted */
189417 int iPos = pFragment->iPos; /* First token of snippet */
189418 u64 hlmask = pFragment->hlmask; /* Highlight-mask for snippet */
189419 int iCol = pFragment->iCol+1; /* Query column to extract text from */
189420 sqlite3_tokenizer_module *pMod; /* Tokenizer module methods object */
189421 sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor open on zDoc/nDoc */
189422
189423 zDoc = (const char *)sqlite3_column_text(pStmt: pCsr->pStmt, i: iCol);
189424 if( zDoc==0 ){
189425 if( sqlite3_column_type(pStmt: pCsr->pStmt, i: iCol)!=SQLITE_NULL ){
189426 return SQLITE_NOMEM;
189427 }
189428 return SQLITE_OK;
189429 }
189430 nDoc = sqlite3_column_bytes(pStmt: pCsr->pStmt, i: iCol);
189431
189432 /* Open a token cursor on the document. */
189433 pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
189434 rc = sqlite3Fts3OpenTokenizer(pTokenizer: pTab->pTokenizer, iLangid: pCsr->iLangid, z: zDoc,n: nDoc,ppCsr: &pC);
189435 if( rc!=SQLITE_OK ){
189436 return rc;
189437 }
189438
189439 while( rc==SQLITE_OK ){
189440 const char *ZDUMMY; /* Dummy argument used with tokenizer */
189441 int DUMMY1 = -1; /* Dummy argument used with tokenizer */
189442 int iBegin = 0; /* Offset in zDoc of start of token */
189443 int iFin = 0; /* Offset in zDoc of end of token */
189444 int isHighlight = 0; /* True for highlighted terms */
189445
189446 /* Variable DUMMY1 is initialized to a negative value above. Elsewhere
189447 ** in the FTS code the variable that the third argument to xNext points to
189448 ** is initialized to zero before the first (*but not necessarily
189449 ** subsequent*) call to xNext(). This is done for a particular application
189450 ** that needs to know whether or not the tokenizer is being used for
189451 ** snippet generation or for some other purpose.
189452 **
189453 ** Extreme care is required when writing code to depend on this
189454 ** initialization. It is not a documented part of the tokenizer interface.
189455 ** If a tokenizer is used directly by any code outside of FTS, this
189456 ** convention might not be respected. */
189457 rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &iBegin, &iFin, &iCurrent);
189458 if( rc!=SQLITE_OK ){
189459 if( rc==SQLITE_DONE ){
189460 /* Special case - the last token of the snippet is also the last token
189461 ** of the column. Append any punctuation that occurred between the end
189462 ** of the previous token and the end of the document to the output.
189463 ** Then break out of the loop. */
189464 rc = fts3StringAppend(pStr: pOut, zAppend: &zDoc[iEnd], nAppend: -1);
189465 }
189466 break;
189467 }
189468 if( iCurrent<iPos ){ continue; }
189469
189470 if( !isShiftDone ){
189471 int n = nDoc - iBegin;
189472 rc = fts3SnippetShift(
189473 pTab, iLangid: pCsr->iLangid, nSnippet, zDoc: &zDoc[iBegin], nDoc: n, piPos: &iPos, pHlmask: &hlmask
189474 );
189475 isShiftDone = 1;
189476
189477 /* Now that the shift has been done, check if the initial "..." are
189478 ** required. They are required if (a) this is not the first fragment,
189479 ** or (b) this fragment does not begin at position 0 of its column.
189480 */
189481 if( rc==SQLITE_OK ){
189482 if( iPos>0 || iFragment>0 ){
189483 rc = fts3StringAppend(pStr: pOut, zAppend: zEllipsis, nAppend: -1);
189484 }else if( iBegin ){
189485 rc = fts3StringAppend(pStr: pOut, zAppend: zDoc, nAppend: iBegin);
189486 }
189487 }
189488 if( rc!=SQLITE_OK || iCurrent<iPos ) continue;
189489 }
189490
189491 if( iCurrent>=(iPos+nSnippet) ){
189492 if( isLast ){
189493 rc = fts3StringAppend(pStr: pOut, zAppend: zEllipsis, nAppend: -1);
189494 }
189495 break;
189496 }
189497
189498 /* Set isHighlight to true if this term should be highlighted. */
189499 isHighlight = (hlmask & ((u64)1 << (iCurrent-iPos)))!=0;
189500
189501 if( iCurrent>iPos ) rc = fts3StringAppend(pStr: pOut, zAppend: &zDoc[iEnd], nAppend: iBegin-iEnd);
189502 if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pStr: pOut, zAppend: zOpen, nAppend: -1);
189503 if( rc==SQLITE_OK ) rc = fts3StringAppend(pStr: pOut, zAppend: &zDoc[iBegin], nAppend: iFin-iBegin);
189504 if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pStr: pOut, zAppend: zClose, nAppend: -1);
189505
189506 iEnd = iFin;
189507 }
189508
189509 pMod->xClose(pC);
189510 return rc;
189511}
189512
189513
189514/*
189515** This function is used to count the entries in a column-list (a
189516** delta-encoded list of term offsets within a single column of a single
189517** row). When this function is called, *ppCollist should point to the
189518** beginning of the first varint in the column-list (the varint that
189519** contains the position of the first matching term in the column data).
189520** Before returning, *ppCollist is set to point to the first byte after
189521** the last varint in the column-list (either the 0x00 signifying the end
189522** of the position-list, or the 0x01 that precedes the column number of
189523** the next column in the position-list).
189524**
189525** The number of elements in the column-list is returned.
189526*/
189527static int fts3ColumnlistCount(char **ppCollist){
189528 char *pEnd = *ppCollist;
189529 char c = 0;
189530 int nEntry = 0;
189531
189532 /* A column-list is terminated by either a 0x01 or 0x00. */
189533 while( 0xFE & (*pEnd | c) ){
189534 c = *pEnd++ & 0x80;
189535 if( !c ) nEntry++;
189536 }
189537
189538 *ppCollist = pEnd;
189539 return nEntry;
189540}
189541
189542/*
189543** This function gathers 'y' or 'b' data for a single phrase.
189544*/
189545static int fts3ExprLHits(
189546 Fts3Expr *pExpr, /* Phrase expression node */
189547 MatchInfo *p /* Matchinfo context */
189548){
189549 Fts3Table *pTab = (Fts3Table *)p->pCursor->base.pVtab;
189550 int iStart;
189551 Fts3Phrase *pPhrase = pExpr->pPhrase;
189552 char *pIter = pPhrase->doclist.pList;
189553 int iCol = 0;
189554
189555 assert( p->flag==FTS3_MATCHINFO_LHITS_BM || p->flag==FTS3_MATCHINFO_LHITS );
189556 if( p->flag==FTS3_MATCHINFO_LHITS ){
189557 iStart = pExpr->iPhrase * p->nCol;
189558 }else{
189559 iStart = pExpr->iPhrase * ((p->nCol + 31) / 32);
189560 }
189561
189562 if( pIter ) while( 1 ){
189563 int nHit = fts3ColumnlistCount(ppCollist: &pIter);
189564 if( (pPhrase->iColumn>=pTab->nColumn || pPhrase->iColumn==iCol) ){
189565 if( p->flag==FTS3_MATCHINFO_LHITS ){
189566 p->aMatchinfo[iStart + iCol] = (u32)nHit;
189567 }else if( nHit ){
189568 p->aMatchinfo[iStart + (iCol+1)/32] |= (1 << (iCol&0x1F));
189569 }
189570 }
189571 assert( *pIter==0x00 || *pIter==0x01 );
189572 if( *pIter!=0x01 ) break;
189573 pIter++;
189574 pIter += fts3GetVarint32(pIter, &iCol);
189575 if( iCol>=p->nCol ) return FTS_CORRUPT_VTAB;
189576 }
189577 return SQLITE_OK;
189578}
189579
189580/*
189581** Gather the results for matchinfo directives 'y' and 'b'.
189582*/
189583static int fts3ExprLHitGather(
189584 Fts3Expr *pExpr,
189585 MatchInfo *p
189586){
189587 int rc = SQLITE_OK;
189588 assert( (pExpr->pLeft==0)==(pExpr->pRight==0) );
189589 if( pExpr->bEof==0 && pExpr->iDocid==p->pCursor->iPrevId ){
189590 if( pExpr->pLeft ){
189591 rc = fts3ExprLHitGather(pExpr: pExpr->pLeft, p);
189592 if( rc==SQLITE_OK ) rc = fts3ExprLHitGather(pExpr: pExpr->pRight, p);
189593 }else{
189594 rc = fts3ExprLHits(pExpr, p);
189595 }
189596 }
189597 return rc;
189598}
189599
189600/*
189601** fts3ExprIterate() callback used to collect the "global" matchinfo stats
189602** for a single query.
189603**
189604** fts3ExprIterate() callback to load the 'global' elements of a
189605** FTS3_MATCHINFO_HITS matchinfo array. The global stats are those elements
189606** of the matchinfo array that are constant for all rows returned by the
189607** current query.
189608**
189609** Argument pCtx is actually a pointer to a struct of type MatchInfo. This
189610** function populates Matchinfo.aMatchinfo[] as follows:
189611**
189612** for(iCol=0; iCol<nCol; iCol++){
189613** aMatchinfo[3*iPhrase*nCol + 3*iCol + 1] = X;
189614** aMatchinfo[3*iPhrase*nCol + 3*iCol + 2] = Y;
189615** }
189616**
189617** where X is the number of matches for phrase iPhrase is column iCol of all
189618** rows of the table. Y is the number of rows for which column iCol contains
189619** at least one instance of phrase iPhrase.
189620**
189621** If the phrase pExpr consists entirely of deferred tokens, then all X and
189622** Y values are set to nDoc, where nDoc is the number of documents in the
189623** file system. This is done because the full-text index doclist is required
189624** to calculate these values properly, and the full-text index doclist is
189625** not available for deferred tokens.
189626*/
189627static int fts3ExprGlobalHitsCb(
189628 Fts3Expr *pExpr, /* Phrase expression node */
189629 int iPhrase, /* Phrase number (numbered from zero) */
189630 void *pCtx /* Pointer to MatchInfo structure */
189631){
189632 MatchInfo *p = (MatchInfo *)pCtx;
189633 return sqlite3Fts3EvalPhraseStats(
189634 pCsr: p->pCursor, pExpr, aiOut: &p->aMatchinfo[3*iPhrase*p->nCol]
189635 );
189636}
189637
189638/*
189639** fts3ExprIterate() callback used to collect the "local" part of the
189640** FTS3_MATCHINFO_HITS array. The local stats are those elements of the
189641** array that are different for each row returned by the query.
189642*/
189643static int fts3ExprLocalHitsCb(
189644 Fts3Expr *pExpr, /* Phrase expression node */
189645 int iPhrase, /* Phrase number */
189646 void *pCtx /* Pointer to MatchInfo structure */
189647){
189648 int rc = SQLITE_OK;
189649 MatchInfo *p = (MatchInfo *)pCtx;
189650 int iStart = iPhrase * p->nCol * 3;
189651 int i;
189652
189653 for(i=0; i<p->nCol && rc==SQLITE_OK; i++){
189654 char *pCsr;
189655 rc = sqlite3Fts3EvalPhrasePoslist(pCsr: p->pCursor, pExpr, iCol: i, ppOut: &pCsr);
189656 if( pCsr ){
189657 p->aMatchinfo[iStart+i*3] = fts3ColumnlistCount(ppCollist: &pCsr);
189658 }else{
189659 p->aMatchinfo[iStart+i*3] = 0;
189660 }
189661 }
189662
189663 return rc;
189664}
189665
189666static int fts3MatchinfoCheck(
189667 Fts3Table *pTab,
189668 char cArg,
189669 char **pzErr
189670){
189671 if( (cArg==FTS3_MATCHINFO_NPHRASE)
189672 || (cArg==FTS3_MATCHINFO_NCOL)
189673 || (cArg==FTS3_MATCHINFO_NDOC && pTab->bFts4)
189674 || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bFts4)
189675 || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize)
189676 || (cArg==FTS3_MATCHINFO_LCS)
189677 || (cArg==FTS3_MATCHINFO_HITS)
189678 || (cArg==FTS3_MATCHINFO_LHITS)
189679 || (cArg==FTS3_MATCHINFO_LHITS_BM)
189680 ){
189681 return SQLITE_OK;
189682 }
189683 sqlite3Fts3ErrMsg(pzErr, zFormat: "unrecognized matchinfo request: %c", cArg);
189684 return SQLITE_ERROR;
189685}
189686
189687static size_t fts3MatchinfoSize(MatchInfo *pInfo, char cArg){
189688 size_t nVal; /* Number of integers output by cArg */
189689
189690 switch( cArg ){
189691 case FTS3_MATCHINFO_NDOC:
189692 case FTS3_MATCHINFO_NPHRASE:
189693 case FTS3_MATCHINFO_NCOL:
189694 nVal = 1;
189695 break;
189696
189697 case FTS3_MATCHINFO_AVGLENGTH:
189698 case FTS3_MATCHINFO_LENGTH:
189699 case FTS3_MATCHINFO_LCS:
189700 nVal = pInfo->nCol;
189701 break;
189702
189703 case FTS3_MATCHINFO_LHITS:
189704 nVal = pInfo->nCol * pInfo->nPhrase;
189705 break;
189706
189707 case FTS3_MATCHINFO_LHITS_BM:
189708 nVal = pInfo->nPhrase * ((pInfo->nCol + 31) / 32);
189709 break;
189710
189711 default:
189712 assert( cArg==FTS3_MATCHINFO_HITS );
189713 nVal = pInfo->nCol * pInfo->nPhrase * 3;
189714 break;
189715 }
189716
189717 return nVal;
189718}
189719
189720static int fts3MatchinfoSelectDoctotal(
189721 Fts3Table *pTab,
189722 sqlite3_stmt **ppStmt,
189723 sqlite3_int64 *pnDoc,
189724 const char **paLen,
189725 const char **ppEnd
189726){
189727 sqlite3_stmt *pStmt;
189728 const char *a;
189729 const char *pEnd;
189730 sqlite3_int64 nDoc;
189731 int n;
189732
189733
189734 if( !*ppStmt ){
189735 int rc = sqlite3Fts3SelectDoctotal(pTab, ppStmt);
189736 if( rc!=SQLITE_OK ) return rc;
189737 }
189738 pStmt = *ppStmt;
189739 assert( sqlite3_data_count(pStmt)==1 );
189740
189741 n = sqlite3_column_bytes(pStmt, i: 0);
189742 a = sqlite3_column_blob(pStmt, i: 0);
189743 if( a==0 ){
189744 return FTS_CORRUPT_VTAB;
189745 }
189746 pEnd = a + n;
189747 a += sqlite3Fts3GetVarintBounded(pBuf: a, pEnd, v: &nDoc);
189748 if( nDoc<=0 || a>pEnd ){
189749 return FTS_CORRUPT_VTAB;
189750 }
189751 *pnDoc = nDoc;
189752
189753 if( paLen ) *paLen = a;
189754 if( ppEnd ) *ppEnd = pEnd;
189755 return SQLITE_OK;
189756}
189757
189758/*
189759** An instance of the following structure is used to store state while
189760** iterating through a multi-column position-list corresponding to the
189761** hits for a single phrase on a single row in order to calculate the
189762** values for a matchinfo() FTS3_MATCHINFO_LCS request.
189763*/
189764typedef struct LcsIterator LcsIterator;
189765struct LcsIterator {
189766 Fts3Expr *pExpr; /* Pointer to phrase expression */
189767 int iPosOffset; /* Tokens count up to end of this phrase */
189768 char *pRead; /* Cursor used to iterate through aDoclist */
189769 int iPos; /* Current position */
189770};
189771
189772/*
189773** If LcsIterator.iCol is set to the following value, the iterator has
189774** finished iterating through all offsets for all columns.
189775*/
189776#define LCS_ITERATOR_FINISHED 0x7FFFFFFF;
189777
189778static int fts3MatchinfoLcsCb(
189779 Fts3Expr *pExpr, /* Phrase expression node */
189780 int iPhrase, /* Phrase number (numbered from zero) */
189781 void *pCtx /* Pointer to MatchInfo structure */
189782){
189783 LcsIterator *aIter = (LcsIterator *)pCtx;
189784 aIter[iPhrase].pExpr = pExpr;
189785 return SQLITE_OK;
189786}
189787
189788/*
189789** Advance the iterator passed as an argument to the next position. Return
189790** 1 if the iterator is at EOF or if it now points to the start of the
189791** position list for the next column.
189792*/
189793static int fts3LcsIteratorAdvance(LcsIterator *pIter){
189794 char *pRead;
189795 sqlite3_int64 iRead;
189796 int rc = 0;
189797
189798 if( NEVER(pIter==0) ) return 1;
189799 pRead = pIter->pRead;
189800 pRead += sqlite3Fts3GetVarint(pBuf: pRead, v: &iRead);
189801 if( iRead==0 || iRead==1 ){
189802 pRead = 0;
189803 rc = 1;
189804 }else{
189805 pIter->iPos += (int)(iRead-2);
189806 }
189807
189808 pIter->pRead = pRead;
189809 return rc;
189810}
189811
189812/*
189813** This function implements the FTS3_MATCHINFO_LCS matchinfo() flag.
189814**
189815** If the call is successful, the longest-common-substring lengths for each
189816** column are written into the first nCol elements of the pInfo->aMatchinfo[]
189817** array before returning. SQLITE_OK is returned in this case.
189818**
189819** Otherwise, if an error occurs, an SQLite error code is returned and the
189820** data written to the first nCol elements of pInfo->aMatchinfo[] is
189821** undefined.
189822*/
189823static int fts3MatchinfoLcs(Fts3Cursor *pCsr, MatchInfo *pInfo){
189824 LcsIterator *aIter;
189825 int i;
189826 int iCol;
189827 int nToken = 0;
189828 int rc = SQLITE_OK;
189829
189830 /* Allocate and populate the array of LcsIterator objects. The array
189831 ** contains one element for each matchable phrase in the query.
189832 **/
189833 aIter = sqlite3Fts3MallocZero(nByte: sizeof(LcsIterator) * pCsr->nPhrase);
189834 if( !aIter ) return SQLITE_NOMEM;
189835 (void)fts3ExprIterate(pExpr: pCsr->pExpr, x: fts3MatchinfoLcsCb, pCtx: (void*)aIter);
189836
189837 for(i=0; i<pInfo->nPhrase; i++){
189838 LcsIterator *pIter = &aIter[i];
189839 nToken -= pIter->pExpr->pPhrase->nToken;
189840 pIter->iPosOffset = nToken;
189841 }
189842
189843 for(iCol=0; iCol<pInfo->nCol; iCol++){
189844 int nLcs = 0; /* LCS value for this column */
189845 int nLive = 0; /* Number of iterators in aIter not at EOF */
189846
189847 for(i=0; i<pInfo->nPhrase; i++){
189848 LcsIterator *pIt = &aIter[i];
189849 rc = sqlite3Fts3EvalPhrasePoslist(pCsr, pExpr: pIt->pExpr, iCol, ppOut: &pIt->pRead);
189850 if( rc!=SQLITE_OK ) goto matchinfo_lcs_out;
189851 if( pIt->pRead ){
189852 pIt->iPos = pIt->iPosOffset;
189853 fts3LcsIteratorAdvance(pIter: pIt);
189854 if( pIt->pRead==0 ){
189855 rc = FTS_CORRUPT_VTAB;
189856 goto matchinfo_lcs_out;
189857 }
189858 nLive++;
189859 }
189860 }
189861
189862 while( nLive>0 ){
189863 LcsIterator *pAdv = 0; /* The iterator to advance by one position */
189864 int nThisLcs = 0; /* LCS for the current iterator positions */
189865
189866 for(i=0; i<pInfo->nPhrase; i++){
189867 LcsIterator *pIter = &aIter[i];
189868 if( pIter->pRead==0 ){
189869 /* This iterator is already at EOF for this column. */
189870 nThisLcs = 0;
189871 }else{
189872 if( pAdv==0 || pIter->iPos<pAdv->iPos ){
189873 pAdv = pIter;
189874 }
189875 if( nThisLcs==0 || pIter->iPos==pIter[-1].iPos ){
189876 nThisLcs++;
189877 }else{
189878 nThisLcs = 1;
189879 }
189880 if( nThisLcs>nLcs ) nLcs = nThisLcs;
189881 }
189882 }
189883 if( fts3LcsIteratorAdvance(pIter: pAdv) ) nLive--;
189884 }
189885
189886 pInfo->aMatchinfo[iCol] = nLcs;
189887 }
189888
189889 matchinfo_lcs_out:
189890 sqlite3_free(p: aIter);
189891 return rc;
189892}
189893
189894/*
189895** Populate the buffer pInfo->aMatchinfo[] with an array of integers to
189896** be returned by the matchinfo() function. Argument zArg contains the
189897** format string passed as the second argument to matchinfo (or the
189898** default value "pcx" if no second argument was specified). The format
189899** string has already been validated and the pInfo->aMatchinfo[] array
189900** is guaranteed to be large enough for the output.
189901**
189902** If bGlobal is true, then populate all fields of the matchinfo() output.
189903** If it is false, then assume that those fields that do not change between
189904** rows (i.e. FTS3_MATCHINFO_NPHRASE, NCOL, NDOC, AVGLENGTH and part of HITS)
189905** have already been populated.
189906**
189907** Return SQLITE_OK if successful, or an SQLite error code if an error
189908** occurs. If a value other than SQLITE_OK is returned, the state the
189909** pInfo->aMatchinfo[] buffer is left in is undefined.
189910*/
189911static int fts3MatchinfoValues(
189912 Fts3Cursor *pCsr, /* FTS3 cursor object */
189913 int bGlobal, /* True to grab the global stats */
189914 MatchInfo *pInfo, /* Matchinfo context object */
189915 const char *zArg /* Matchinfo format string */
189916){
189917 int rc = SQLITE_OK;
189918 int i;
189919 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
189920 sqlite3_stmt *pSelect = 0;
189921
189922 for(i=0; rc==SQLITE_OK && zArg[i]; i++){
189923 pInfo->flag = zArg[i];
189924 switch( zArg[i] ){
189925 case FTS3_MATCHINFO_NPHRASE:
189926 if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase;
189927 break;
189928
189929 case FTS3_MATCHINFO_NCOL:
189930 if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nCol;
189931 break;
189932
189933 case FTS3_MATCHINFO_NDOC:
189934 if( bGlobal ){
189935 sqlite3_int64 nDoc = 0;
189936 rc = fts3MatchinfoSelectDoctotal(pTab, ppStmt: &pSelect, pnDoc: &nDoc, paLen: 0, ppEnd: 0);
189937 pInfo->aMatchinfo[0] = (u32)nDoc;
189938 }
189939 break;
189940
189941 case FTS3_MATCHINFO_AVGLENGTH:
189942 if( bGlobal ){
189943 sqlite3_int64 nDoc; /* Number of rows in table */
189944 const char *a; /* Aggregate column length array */
189945 const char *pEnd; /* First byte past end of length array */
189946
189947 rc = fts3MatchinfoSelectDoctotal(pTab, ppStmt: &pSelect, pnDoc: &nDoc, paLen: &a, ppEnd: &pEnd);
189948 if( rc==SQLITE_OK ){
189949 int iCol;
189950 for(iCol=0; iCol<pInfo->nCol; iCol++){
189951 u32 iVal;
189952 sqlite3_int64 nToken;
189953 a += sqlite3Fts3GetVarint(pBuf: a, v: &nToken);
189954 if( a>pEnd ){
189955 rc = SQLITE_CORRUPT_VTAB;
189956 break;
189957 }
189958 iVal = (u32)(((u32)(nToken&0xffffffff)+nDoc/2)/nDoc);
189959 pInfo->aMatchinfo[iCol] = iVal;
189960 }
189961 }
189962 }
189963 break;
189964
189965 case FTS3_MATCHINFO_LENGTH: {
189966 sqlite3_stmt *pSelectDocsize = 0;
189967 rc = sqlite3Fts3SelectDocsize(pTab, iDocid: pCsr->iPrevId, ppStmt: &pSelectDocsize);
189968 if( rc==SQLITE_OK ){
189969 int iCol;
189970 const char *a = sqlite3_column_blob(pStmt: pSelectDocsize, i: 0);
189971 const char *pEnd = a + sqlite3_column_bytes(pStmt: pSelectDocsize, i: 0);
189972 for(iCol=0; iCol<pInfo->nCol; iCol++){
189973 sqlite3_int64 nToken;
189974 a += sqlite3Fts3GetVarintBounded(pBuf: a, pEnd, v: &nToken);
189975 if( a>pEnd ){
189976 rc = SQLITE_CORRUPT_VTAB;
189977 break;
189978 }
189979 pInfo->aMatchinfo[iCol] = (u32)nToken;
189980 }
189981 }
189982 sqlite3_reset(pStmt: pSelectDocsize);
189983 break;
189984 }
189985
189986 case FTS3_MATCHINFO_LCS:
189987 rc = fts3ExprLoadDoclists(pCsr, pnPhrase: 0, pnToken: 0);
189988 if( rc==SQLITE_OK ){
189989 rc = fts3MatchinfoLcs(pCsr, pInfo);
189990 }
189991 break;
189992
189993 case FTS3_MATCHINFO_LHITS_BM:
189994 case FTS3_MATCHINFO_LHITS: {
189995 size_t nZero = fts3MatchinfoSize(pInfo, cArg: zArg[i]) * sizeof(u32);
189996 memset(s: pInfo->aMatchinfo, c: 0, n: nZero);
189997 rc = fts3ExprLHitGather(pExpr: pCsr->pExpr, p: pInfo);
189998 break;
189999 }
190000
190001 default: {
190002 Fts3Expr *pExpr;
190003 assert( zArg[i]==FTS3_MATCHINFO_HITS );
190004 pExpr = pCsr->pExpr;
190005 rc = fts3ExprLoadDoclists(pCsr, pnPhrase: 0, pnToken: 0);
190006 if( rc!=SQLITE_OK ) break;
190007 if( bGlobal ){
190008 if( pCsr->pDeferred ){
190009 rc = fts3MatchinfoSelectDoctotal(pTab, ppStmt: &pSelect, pnDoc: &pInfo->nDoc,paLen: 0,ppEnd: 0);
190010 if( rc!=SQLITE_OK ) break;
190011 }
190012 rc = fts3ExprIterate(pExpr, x: fts3ExprGlobalHitsCb,pCtx: (void*)pInfo);
190013 sqlite3Fts3EvalTestDeferred(pCsr, pRc: &rc);
190014 if( rc!=SQLITE_OK ) break;
190015 }
190016 (void)fts3ExprIterate(pExpr, x: fts3ExprLocalHitsCb,pCtx: (void*)pInfo);
190017 break;
190018 }
190019 }
190020
190021 pInfo->aMatchinfo += fts3MatchinfoSize(pInfo, cArg: zArg[i]);
190022 }
190023
190024 sqlite3_reset(pStmt: pSelect);
190025 return rc;
190026}
190027
190028
190029/*
190030** Populate pCsr->aMatchinfo[] with data for the current row. The
190031** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32).
190032*/
190033static void fts3GetMatchinfo(
190034 sqlite3_context *pCtx, /* Return results here */
190035 Fts3Cursor *pCsr, /* FTS3 Cursor object */
190036 const char *zArg /* Second argument to matchinfo() function */
190037){
190038 MatchInfo sInfo;
190039 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
190040 int rc = SQLITE_OK;
190041 int bGlobal = 0; /* Collect 'global' stats as well as local */
190042
190043 u32 *aOut = 0;
190044 void (*xDestroyOut)(void*) = 0;
190045
190046 memset(s: &sInfo, c: 0, n: sizeof(MatchInfo));
190047 sInfo.pCursor = pCsr;
190048 sInfo.nCol = pTab->nColumn;
190049
190050 /* If there is cached matchinfo() data, but the format string for the
190051 ** cache does not match the format string for this request, discard
190052 ** the cached data. */
190053 if( pCsr->pMIBuffer && strcmp(s1: pCsr->pMIBuffer->zMatchinfo, s2: zArg) ){
190054 sqlite3Fts3MIBufferFree(p: pCsr->pMIBuffer);
190055 pCsr->pMIBuffer = 0;
190056 }
190057
190058 /* If Fts3Cursor.pMIBuffer is NULL, then this is the first time the
190059 ** matchinfo function has been called for this query. In this case
190060 ** allocate the array used to accumulate the matchinfo data and
190061 ** initialize those elements that are constant for every row.
190062 */
190063 if( pCsr->pMIBuffer==0 ){
190064 size_t nMatchinfo = 0; /* Number of u32 elements in match-info */
190065 int i; /* Used to iterate through zArg */
190066
190067 /* Determine the number of phrases in the query */
190068 pCsr->nPhrase = fts3ExprPhraseCount(pExpr: pCsr->pExpr);
190069 sInfo.nPhrase = pCsr->nPhrase;
190070
190071 /* Determine the number of integers in the buffer returned by this call. */
190072 for(i=0; zArg[i]; i++){
190073 char *zErr = 0;
190074 if( fts3MatchinfoCheck(pTab, cArg: zArg[i], pzErr: &zErr) ){
190075 sqlite3_result_error(pCtx, z: zErr, n: -1);
190076 sqlite3_free(p: zErr);
190077 return;
190078 }
190079 nMatchinfo += fts3MatchinfoSize(pInfo: &sInfo, cArg: zArg[i]);
190080 }
190081
190082 /* Allocate space for Fts3Cursor.aMatchinfo[] and Fts3Cursor.zMatchinfo. */
190083 pCsr->pMIBuffer = fts3MIBufferNew(nElem: nMatchinfo, zMatchinfo: zArg);
190084 if( !pCsr->pMIBuffer ) rc = SQLITE_NOMEM;
190085
190086 pCsr->isMatchinfoNeeded = 1;
190087 bGlobal = 1;
190088 }
190089
190090 if( rc==SQLITE_OK ){
190091 xDestroyOut = fts3MIBufferAlloc(p: pCsr->pMIBuffer, paOut: &aOut);
190092 if( xDestroyOut==0 ){
190093 rc = SQLITE_NOMEM;
190094 }
190095 }
190096
190097 if( rc==SQLITE_OK ){
190098 sInfo.aMatchinfo = aOut;
190099 sInfo.nPhrase = pCsr->nPhrase;
190100 rc = fts3MatchinfoValues(pCsr, bGlobal, pInfo: &sInfo, zArg);
190101 if( bGlobal ){
190102 fts3MIBufferSetGlobal(p: pCsr->pMIBuffer);
190103 }
190104 }
190105
190106 if( rc!=SQLITE_OK ){
190107 sqlite3_result_error_code(pCtx, errCode: rc);
190108 if( xDestroyOut ) xDestroyOut(aOut);
190109 }else{
190110 int n = pCsr->pMIBuffer->nElem * sizeof(u32);
190111 sqlite3_result_blob(pCtx, z: aOut, n, xDel: xDestroyOut);
190112 }
190113}
190114
190115/*
190116** Implementation of snippet() function.
190117*/
190118SQLITE_PRIVATE void sqlite3Fts3Snippet(
190119 sqlite3_context *pCtx, /* SQLite function call context */
190120 Fts3Cursor *pCsr, /* Cursor object */
190121 const char *zStart, /* Snippet start text - "<b>" */
190122 const char *zEnd, /* Snippet end text - "</b>" */
190123 const char *zEllipsis, /* Snippet ellipsis text - "<b>...</b>" */
190124 int iCol, /* Extract snippet from this column */
190125 int nToken /* Approximate number of tokens in snippet */
190126){
190127 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
190128 int rc = SQLITE_OK;
190129 int i;
190130 StrBuffer res = {0, 0, 0};
190131
190132 /* The returned text includes up to four fragments of text extracted from
190133 ** the data in the current row. The first iteration of the for(...) loop
190134 ** below attempts to locate a single fragment of text nToken tokens in
190135 ** size that contains at least one instance of all phrases in the query
190136 ** expression that appear in the current row. If such a fragment of text
190137 ** cannot be found, the second iteration of the loop attempts to locate
190138 ** a pair of fragments, and so on.
190139 */
190140 int nSnippet = 0; /* Number of fragments in this snippet */
190141 SnippetFragment aSnippet[4]; /* Maximum of 4 fragments per snippet */
190142 int nFToken = -1; /* Number of tokens in each fragment */
190143
190144 if( !pCsr->pExpr ){
190145 sqlite3_result_text(pCtx, z: "", n: 0, SQLITE_STATIC);
190146 return;
190147 }
190148
190149 /* Limit the snippet length to 64 tokens. */
190150 if( nToken<-64 ) nToken = -64;
190151 if( nToken>+64 ) nToken = +64;
190152
190153 for(nSnippet=1; 1; nSnippet++){
190154
190155 int iSnip; /* Loop counter 0..nSnippet-1 */
190156 u64 mCovered = 0; /* Bitmask of phrases covered by snippet */
190157 u64 mSeen = 0; /* Bitmask of phrases seen by BestSnippet() */
190158
190159 if( nToken>=0 ){
190160 nFToken = (nToken+nSnippet-1) / nSnippet;
190161 }else{
190162 nFToken = -1 * nToken;
190163 }
190164
190165 for(iSnip=0; iSnip<nSnippet; iSnip++){
190166 int iBestScore = -1; /* Best score of columns checked so far */
190167 int iRead; /* Used to iterate through columns */
190168 SnippetFragment *pFragment = &aSnippet[iSnip];
190169
190170 memset(s: pFragment, c: 0, n: sizeof(*pFragment));
190171
190172 /* Loop through all columns of the table being considered for snippets.
190173 ** If the iCol argument to this function was negative, this means all
190174 ** columns of the FTS3 table. Otherwise, only column iCol is considered.
190175 */
190176 for(iRead=0; iRead<pTab->nColumn; iRead++){
190177 SnippetFragment sF = {0, 0, 0, 0};
190178 int iS = 0;
190179 if( iCol>=0 && iRead!=iCol ) continue;
190180
190181 /* Find the best snippet of nFToken tokens in column iRead. */
190182 rc = fts3BestSnippet(nSnippet: nFToken, pCsr, iCol: iRead, mCovered, pmSeen: &mSeen, pFragment: &sF, piScore: &iS);
190183 if( rc!=SQLITE_OK ){
190184 goto snippet_out;
190185 }
190186 if( iS>iBestScore ){
190187 *pFragment = sF;
190188 iBestScore = iS;
190189 }
190190 }
190191
190192 mCovered |= pFragment->covered;
190193 }
190194
190195 /* If all query phrases seen by fts3BestSnippet() are present in at least
190196 ** one of the nSnippet snippet fragments, break out of the loop.
190197 */
190198 assert( (mCovered&mSeen)==mCovered );
190199 if( mSeen==mCovered || nSnippet==SizeofArray(aSnippet) ) break;
190200 }
190201
190202 assert( nFToken>0 );
190203
190204 for(i=0; i<nSnippet && rc==SQLITE_OK; i++){
190205 rc = fts3SnippetText(pCsr, pFragment: &aSnippet[i],
190206 iFragment: i, isLast: (i==nSnippet-1), nSnippet: nFToken, zOpen: zStart, zClose: zEnd, zEllipsis, pOut: &res
190207 );
190208 }
190209
190210 snippet_out:
190211 sqlite3Fts3SegmentsClose(p: pTab);
190212 if( rc!=SQLITE_OK ){
190213 sqlite3_result_error_code(pCtx, errCode: rc);
190214 sqlite3_free(p: res.z);
190215 }else{
190216 sqlite3_result_text(pCtx, z: res.z, n: -1, xDel: sqlite3_free);
190217 }
190218}
190219
190220
190221typedef struct TermOffset TermOffset;
190222typedef struct TermOffsetCtx TermOffsetCtx;
190223
190224struct TermOffset {
190225 char *pList; /* Position-list */
190226 i64 iPos; /* Position just read from pList */
190227 i64 iOff; /* Offset of this term from read positions */
190228};
190229
190230struct TermOffsetCtx {
190231 Fts3Cursor *pCsr;
190232 int iCol; /* Column of table to populate aTerm for */
190233 int iTerm;
190234 sqlite3_int64 iDocid;
190235 TermOffset *aTerm;
190236};
190237
190238/*
190239** This function is an fts3ExprIterate() callback used by sqlite3Fts3Offsets().
190240*/
190241static int fts3ExprTermOffsetInit(Fts3Expr *pExpr, int iPhrase, void *ctx){
190242 TermOffsetCtx *p = (TermOffsetCtx *)ctx;
190243 int nTerm; /* Number of tokens in phrase */
190244 int iTerm; /* For looping through nTerm phrase terms */
190245 char *pList; /* Pointer to position list for phrase */
190246 i64 iPos = 0; /* First position in position-list */
190247 int rc;
190248
190249 UNUSED_PARAMETER(iPhrase);
190250 rc = sqlite3Fts3EvalPhrasePoslist(pCsr: p->pCsr, pExpr, iCol: p->iCol, ppOut: &pList);
190251 nTerm = pExpr->pPhrase->nToken;
190252 if( pList ){
190253 fts3GetDeltaPosition(pp: &pList, piPos: &iPos);
190254 assert_fts3_nc( iPos>=0 );
190255 }
190256
190257 for(iTerm=0; iTerm<nTerm; iTerm++){
190258 TermOffset *pT = &p->aTerm[p->iTerm++];
190259 pT->iOff = nTerm-iTerm-1;
190260 pT->pList = pList;
190261 pT->iPos = iPos;
190262 }
190263
190264 return rc;
190265}
190266
190267/*
190268** Implementation of offsets() function.
190269*/
190270SQLITE_PRIVATE void sqlite3Fts3Offsets(
190271 sqlite3_context *pCtx, /* SQLite function call context */
190272 Fts3Cursor *pCsr /* Cursor object */
190273){
190274 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
190275 sqlite3_tokenizer_module const *pMod = pTab->pTokenizer->pModule;
190276 int rc; /* Return Code */
190277 int nToken; /* Number of tokens in query */
190278 int iCol; /* Column currently being processed */
190279 StrBuffer res = {0, 0, 0}; /* Result string */
190280 TermOffsetCtx sCtx; /* Context for fts3ExprTermOffsetInit() */
190281
190282 if( !pCsr->pExpr ){
190283 sqlite3_result_text(pCtx, z: "", n: 0, SQLITE_STATIC);
190284 return;
190285 }
190286
190287 memset(s: &sCtx, c: 0, n: sizeof(sCtx));
190288 assert( pCsr->isRequireSeek==0 );
190289
190290 /* Count the number of terms in the query */
190291 rc = fts3ExprLoadDoclists(pCsr, pnPhrase: 0, pnToken: &nToken);
190292 if( rc!=SQLITE_OK ) goto offsets_out;
190293
190294 /* Allocate the array of TermOffset iterators. */
190295 sCtx.aTerm = (TermOffset *)sqlite3Fts3MallocZero(nByte: sizeof(TermOffset)*nToken);
190296 if( 0==sCtx.aTerm ){
190297 rc = SQLITE_NOMEM;
190298 goto offsets_out;
190299 }
190300 sCtx.iDocid = pCsr->iPrevId;
190301 sCtx.pCsr = pCsr;
190302
190303 /* Loop through the table columns, appending offset information to
190304 ** string-buffer res for each column.
190305 */
190306 for(iCol=0; iCol<pTab->nColumn; iCol++){
190307 sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor */
190308 const char *ZDUMMY; /* Dummy argument used with xNext() */
190309 int NDUMMY = 0; /* Dummy argument used with xNext() */
190310 int iStart = 0;
190311 int iEnd = 0;
190312 int iCurrent = 0;
190313 const char *zDoc;
190314 int nDoc;
190315
190316 /* Initialize the contents of sCtx.aTerm[] for column iCol. This
190317 ** operation may fail if the database contains corrupt records.
190318 */
190319 sCtx.iCol = iCol;
190320 sCtx.iTerm = 0;
190321 rc = fts3ExprIterate(pExpr: pCsr->pExpr, x: fts3ExprTermOffsetInit, pCtx: (void*)&sCtx);
190322 if( rc!=SQLITE_OK ) goto offsets_out;
190323
190324 /* Retreive the text stored in column iCol. If an SQL NULL is stored
190325 ** in column iCol, jump immediately to the next iteration of the loop.
190326 ** If an OOM occurs while retrieving the data (this can happen if SQLite
190327 ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM
190328 ** to the caller.
190329 */
190330 zDoc = (const char *)sqlite3_column_text(pStmt: pCsr->pStmt, i: iCol+1);
190331 nDoc = sqlite3_column_bytes(pStmt: pCsr->pStmt, i: iCol+1);
190332 if( zDoc==0 ){
190333 if( sqlite3_column_type(pStmt: pCsr->pStmt, i: iCol+1)==SQLITE_NULL ){
190334 continue;
190335 }
190336 rc = SQLITE_NOMEM;
190337 goto offsets_out;
190338 }
190339
190340 /* Initialize a tokenizer iterator to iterate through column iCol. */
190341 rc = sqlite3Fts3OpenTokenizer(pTokenizer: pTab->pTokenizer, iLangid: pCsr->iLangid,
190342 z: zDoc, n: nDoc, ppCsr: &pC
190343 );
190344 if( rc!=SQLITE_OK ) goto offsets_out;
190345
190346 rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
190347 while( rc==SQLITE_OK ){
190348 int i; /* Used to loop through terms */
190349 int iMinPos = 0x7FFFFFFF; /* Position of next token */
190350 TermOffset *pTerm = 0; /* TermOffset associated with next token */
190351
190352 for(i=0; i<nToken; i++){
190353 TermOffset *pT = &sCtx.aTerm[i];
190354 if( pT->pList && (pT->iPos-pT->iOff)<iMinPos ){
190355 iMinPos = pT->iPos-pT->iOff;
190356 pTerm = pT;
190357 }
190358 }
190359
190360 if( !pTerm ){
190361 /* All offsets for this column have been gathered. */
190362 rc = SQLITE_DONE;
190363 }else{
190364 assert_fts3_nc( iCurrent<=iMinPos );
190365 if( 0==(0xFE&*pTerm->pList) ){
190366 pTerm->pList = 0;
190367 }else{
190368 fts3GetDeltaPosition(pp: &pTerm->pList, piPos: &pTerm->iPos);
190369 }
190370 while( rc==SQLITE_OK && iCurrent<iMinPos ){
190371 rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
190372 }
190373 if( rc==SQLITE_OK ){
190374 char aBuffer[64];
190375 sqlite3_snprintf(n: sizeof(aBuffer), zBuf: aBuffer,
190376 zFormat: "%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart
190377 );
190378 rc = fts3StringAppend(pStr: &res, zAppend: aBuffer, nAppend: -1);
190379 }else if( rc==SQLITE_DONE && pTab->zContentTbl==0 ){
190380 rc = FTS_CORRUPT_VTAB;
190381 }
190382 }
190383 }
190384 if( rc==SQLITE_DONE ){
190385 rc = SQLITE_OK;
190386 }
190387
190388 pMod->xClose(pC);
190389 if( rc!=SQLITE_OK ) goto offsets_out;
190390 }
190391
190392 offsets_out:
190393 sqlite3_free(p: sCtx.aTerm);
190394 assert( rc!=SQLITE_DONE );
190395 sqlite3Fts3SegmentsClose(p: pTab);
190396 if( rc!=SQLITE_OK ){
190397 sqlite3_result_error_code(pCtx, errCode: rc);
190398 sqlite3_free(p: res.z);
190399 }else{
190400 sqlite3_result_text(pCtx, z: res.z, n: res.n-1, xDel: sqlite3_free);
190401 }
190402 return;
190403}
190404
190405/*
190406** Implementation of matchinfo() function.
190407*/
190408SQLITE_PRIVATE void sqlite3Fts3Matchinfo(
190409 sqlite3_context *pContext, /* Function call context */
190410 Fts3Cursor *pCsr, /* FTS3 table cursor */
190411 const char *zArg /* Second arg to matchinfo() function */
190412){
190413 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
190414 const char *zFormat;
190415
190416 if( zArg ){
190417 zFormat = zArg;
190418 }else{
190419 zFormat = FTS3_MATCHINFO_DEFAULT;
190420 }
190421
190422 if( !pCsr->pExpr ){
190423 sqlite3_result_blob(pCtx: pContext, z: "", n: 0, SQLITE_STATIC);
190424 return;
190425 }else{
190426 /* Retrieve matchinfo() data. */
190427 fts3GetMatchinfo(pCtx: pContext, pCsr, zArg: zFormat);
190428 sqlite3Fts3SegmentsClose(p: pTab);
190429 }
190430}
190431
190432#endif
190433
190434/************** End of fts3_snippet.c ****************************************/
190435/************** Begin file fts3_unicode.c ************************************/
190436/*
190437** 2012 May 24
190438**
190439** The author disclaims copyright to this source code. In place of
190440** a legal notice, here is a blessing:
190441**
190442** May you do good and not evil.
190443** May you find forgiveness for yourself and forgive others.
190444** May you share freely, never taking more than you give.
190445**
190446******************************************************************************
190447**
190448** Implementation of the "unicode" full-text-search tokenizer.
190449*/
190450
190451#ifndef SQLITE_DISABLE_FTS3_UNICODE
190452
190453/* #include "fts3Int.h" */
190454#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
190455
190456/* #include <assert.h> */
190457/* #include <stdlib.h> */
190458/* #include <stdio.h> */
190459/* #include <string.h> */
190460
190461/* #include "fts3_tokenizer.h" */
190462
190463/*
190464** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
190465** from the sqlite3 source file utf.c. If this file is compiled as part
190466** of the amalgamation, they are not required.
190467*/
190468#ifndef SQLITE_AMALGAMATION
190469
190470static const unsigned char sqlite3Utf8Trans1[] = {
190471 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
190472 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
190473 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
190474 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
190475 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
190476 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
190477 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
190478 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
190479};
190480
190481#define READ_UTF8(zIn, zTerm, c) \
190482 c = *(zIn++); \
190483 if( c>=0xc0 ){ \
190484 c = sqlite3Utf8Trans1[c-0xc0]; \
190485 while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ \
190486 c = (c<<6) + (0x3f & *(zIn++)); \
190487 } \
190488 if( c<0x80 \
190489 || (c&0xFFFFF800)==0xD800 \
190490 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
190491 }
190492
190493#define WRITE_UTF8(zOut, c) { \
190494 if( c<0x00080 ){ \
190495 *zOut++ = (u8)(c&0xFF); \
190496 } \
190497 else if( c<0x00800 ){ \
190498 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); \
190499 *zOut++ = 0x80 + (u8)(c & 0x3F); \
190500 } \
190501 else if( c<0x10000 ){ \
190502 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); \
190503 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
190504 *zOut++ = 0x80 + (u8)(c & 0x3F); \
190505 }else{ \
190506 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); \
190507 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); \
190508 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
190509 *zOut++ = 0x80 + (u8)(c & 0x3F); \
190510 } \
190511}
190512
190513#endif /* ifndef SQLITE_AMALGAMATION */
190514
190515typedef struct unicode_tokenizer unicode_tokenizer;
190516typedef struct unicode_cursor unicode_cursor;
190517
190518struct unicode_tokenizer {
190519 sqlite3_tokenizer base;
190520 int eRemoveDiacritic;
190521 int nException;
190522 int *aiException;
190523};
190524
190525struct unicode_cursor {
190526 sqlite3_tokenizer_cursor base;
190527 const unsigned char *aInput; /* Input text being tokenized */
190528 int nInput; /* Size of aInput[] in bytes */
190529 int iOff; /* Current offset within aInput[] */
190530 int iToken; /* Index of next token to be returned */
190531 char *zToken; /* storage for current token */
190532 int nAlloc; /* space allocated at zToken */
190533};
190534
190535
190536/*
190537** Destroy a tokenizer allocated by unicodeCreate().
190538*/
190539static int unicodeDestroy(sqlite3_tokenizer *pTokenizer){
190540 if( pTokenizer ){
190541 unicode_tokenizer *p = (unicode_tokenizer *)pTokenizer;
190542 sqlite3_free(p: p->aiException);
190543 sqlite3_free(p);
190544 }
190545 return SQLITE_OK;
190546}
190547
190548/*
190549** As part of a tokenchars= or separators= option, the CREATE VIRTUAL TABLE
190550** statement has specified that the tokenizer for this table shall consider
190551** all characters in string zIn/nIn to be separators (if bAlnum==0) or
190552** token characters (if bAlnum==1).
190553**
190554** For each codepoint in the zIn/nIn string, this function checks if the
190555** sqlite3FtsUnicodeIsalnum() function already returns the desired result.
190556** If so, no action is taken. Otherwise, the codepoint is added to the
190557** unicode_tokenizer.aiException[] array. For the purposes of tokenization,
190558** the return value of sqlite3FtsUnicodeIsalnum() is inverted for all
190559** codepoints in the aiException[] array.
190560**
190561** If a standalone diacritic mark (one that sqlite3FtsUnicodeIsdiacritic()
190562** identifies as a diacritic) occurs in the zIn/nIn string it is ignored.
190563** It is not possible to change the behavior of the tokenizer with respect
190564** to these codepoints.
190565*/
190566static int unicodeAddExceptions(
190567 unicode_tokenizer *p, /* Tokenizer to add exceptions to */
190568 int bAlnum, /* Replace Isalnum() return value with this */
190569 const char *zIn, /* Array of characters to make exceptions */
190570 int nIn /* Length of z in bytes */
190571){
190572 const unsigned char *z = (const unsigned char *)zIn;
190573 const unsigned char *zTerm = &z[nIn];
190574 unsigned int iCode;
190575 int nEntry = 0;
190576
190577 assert( bAlnum==0 || bAlnum==1 );
190578
190579 while( z<zTerm ){
190580 READ_UTF8(z, zTerm, iCode);
190581 assert( (sqlite3FtsUnicodeIsalnum((int)iCode) & 0xFFFFFFFE)==0 );
190582 if( sqlite3FtsUnicodeIsalnum((int)iCode)!=bAlnum
190583 && sqlite3FtsUnicodeIsdiacritic((int)iCode)==0
190584 ){
190585 nEntry++;
190586 }
190587 }
190588
190589 if( nEntry ){
190590 int *aNew; /* New aiException[] array */
190591 int nNew; /* Number of valid entries in array aNew[] */
190592
190593 aNew = sqlite3_realloc64(pOld: p->aiException,n: (p->nException+nEntry)*sizeof(int));
190594 if( aNew==0 ) return SQLITE_NOMEM;
190595 nNew = p->nException;
190596
190597 z = (const unsigned char *)zIn;
190598 while( z<zTerm ){
190599 READ_UTF8(z, zTerm, iCode);
190600 if( sqlite3FtsUnicodeIsalnum((int)iCode)!=bAlnum
190601 && sqlite3FtsUnicodeIsdiacritic((int)iCode)==0
190602 ){
190603 int i, j;
190604 for(i=0; i<nNew && aNew[i]<(int)iCode; i++);
190605 for(j=nNew; j>i; j--) aNew[j] = aNew[j-1];
190606 aNew[i] = (int)iCode;
190607 nNew++;
190608 }
190609 }
190610 p->aiException = aNew;
190611 p->nException = nNew;
190612 }
190613
190614 return SQLITE_OK;
190615}
190616
190617/*
190618** Return true if the p->aiException[] array contains the value iCode.
190619*/
190620static int unicodeIsException(unicode_tokenizer *p, int iCode){
190621 if( p->nException>0 ){
190622 int *a = p->aiException;
190623 int iLo = 0;
190624 int iHi = p->nException-1;
190625
190626 while( iHi>=iLo ){
190627 int iTest = (iHi + iLo) / 2;
190628 if( iCode==a[iTest] ){
190629 return 1;
190630 }else if( iCode>a[iTest] ){
190631 iLo = iTest+1;
190632 }else{
190633 iHi = iTest-1;
190634 }
190635 }
190636 }
190637
190638 return 0;
190639}
190640
190641/*
190642** Return true if, for the purposes of tokenization, codepoint iCode is
190643** considered a token character (not a separator).
190644*/
190645static int unicodeIsAlnum(unicode_tokenizer *p, int iCode){
190646 assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 );
190647 return sqlite3FtsUnicodeIsalnum(iCode) ^ unicodeIsException(p, iCode);
190648}
190649
190650/*
190651** Create a new tokenizer instance.
190652*/
190653static int unicodeCreate(
190654 int nArg, /* Size of array argv[] */
190655 const char * const *azArg, /* Tokenizer creation arguments */
190656 sqlite3_tokenizer **pp /* OUT: New tokenizer handle */
190657){
190658 unicode_tokenizer *pNew; /* New tokenizer object */
190659 int i;
190660 int rc = SQLITE_OK;
190661
190662 pNew = (unicode_tokenizer *) sqlite3_malloc(n: sizeof(unicode_tokenizer));
190663 if( pNew==NULL ) return SQLITE_NOMEM;
190664 memset(s: pNew, c: 0, n: sizeof(unicode_tokenizer));
190665 pNew->eRemoveDiacritic = 1;
190666
190667 for(i=0; rc==SQLITE_OK && i<nArg; i++){
190668 const char *z = azArg[i];
190669 int n = (int)strlen(s: z);
190670
190671 if( n==19 && memcmp(s1: "remove_diacritics=1", s2: z, n: 19)==0 ){
190672 pNew->eRemoveDiacritic = 1;
190673 }
190674 else if( n==19 && memcmp(s1: "remove_diacritics=0", s2: z, n: 19)==0 ){
190675 pNew->eRemoveDiacritic = 0;
190676 }
190677 else if( n==19 && memcmp(s1: "remove_diacritics=2", s2: z, n: 19)==0 ){
190678 pNew->eRemoveDiacritic = 2;
190679 }
190680 else if( n>=11 && memcmp(s1: "tokenchars=", s2: z, n: 11)==0 ){
190681 rc = unicodeAddExceptions(p: pNew, bAlnum: 1, zIn: &z[11], nIn: n-11);
190682 }
190683 else if( n>=11 && memcmp(s1: "separators=", s2: z, n: 11)==0 ){
190684 rc = unicodeAddExceptions(p: pNew, bAlnum: 0, zIn: &z[11], nIn: n-11);
190685 }
190686 else{
190687 /* Unrecognized argument */
190688 rc = SQLITE_ERROR;
190689 }
190690 }
190691
190692 if( rc!=SQLITE_OK ){
190693 unicodeDestroy(pTokenizer: (sqlite3_tokenizer *)pNew);
190694 pNew = 0;
190695 }
190696 *pp = (sqlite3_tokenizer *)pNew;
190697 return rc;
190698}
190699
190700/*
190701** Prepare to begin tokenizing a particular string. The input
190702** string to be tokenized is pInput[0..nBytes-1]. A cursor
190703** used to incrementally tokenize this string is returned in
190704** *ppCursor.
190705*/
190706static int unicodeOpen(
190707 sqlite3_tokenizer *p, /* The tokenizer */
190708 const char *aInput, /* Input string */
190709 int nInput, /* Size of string aInput in bytes */
190710 sqlite3_tokenizer_cursor **pp /* OUT: New cursor object */
190711){
190712 unicode_cursor *pCsr;
190713
190714 pCsr = (unicode_cursor *)sqlite3_malloc(n: sizeof(unicode_cursor));
190715 if( pCsr==0 ){
190716 return SQLITE_NOMEM;
190717 }
190718 memset(s: pCsr, c: 0, n: sizeof(unicode_cursor));
190719
190720 pCsr->aInput = (const unsigned char *)aInput;
190721 if( aInput==0 ){
190722 pCsr->nInput = 0;
190723 pCsr->aInput = (const unsigned char*)"";
190724 }else if( nInput<0 ){
190725 pCsr->nInput = (int)strlen(s: aInput);
190726 }else{
190727 pCsr->nInput = nInput;
190728 }
190729
190730 *pp = &pCsr->base;
190731 UNUSED_PARAMETER(p);
190732 return SQLITE_OK;
190733}
190734
190735/*
190736** Close a tokenization cursor previously opened by a call to
190737** simpleOpen() above.
190738*/
190739static int unicodeClose(sqlite3_tokenizer_cursor *pCursor){
190740 unicode_cursor *pCsr = (unicode_cursor *) pCursor;
190741 sqlite3_free(p: pCsr->zToken);
190742 sqlite3_free(p: pCsr);
190743 return SQLITE_OK;
190744}
190745
190746/*
190747** Extract the next token from a tokenization cursor. The cursor must
190748** have been opened by a prior call to simpleOpen().
190749*/
190750static int unicodeNext(
190751 sqlite3_tokenizer_cursor *pC, /* Cursor returned by simpleOpen */
190752 const char **paToken, /* OUT: Token text */
190753 int *pnToken, /* OUT: Number of bytes at *paToken */
190754 int *piStart, /* OUT: Starting offset of token */
190755 int *piEnd, /* OUT: Ending offset of token */
190756 int *piPos /* OUT: Position integer of token */
190757){
190758 unicode_cursor *pCsr = (unicode_cursor *)pC;
190759 unicode_tokenizer *p = ((unicode_tokenizer *)pCsr->base.pTokenizer);
190760 unsigned int iCode = 0;
190761 char *zOut;
190762 const unsigned char *z = &pCsr->aInput[pCsr->iOff];
190763 const unsigned char *zStart = z;
190764 const unsigned char *zEnd;
190765 const unsigned char *zTerm = &pCsr->aInput[pCsr->nInput];
190766
190767 /* Scan past any delimiter characters before the start of the next token.
190768 ** Return SQLITE_DONE early if this takes us all the way to the end of
190769 ** the input. */
190770 while( z<zTerm ){
190771 READ_UTF8(z, zTerm, iCode);
190772 if( unicodeIsAlnum(p, iCode: (int)iCode) ) break;
190773 zStart = z;
190774 }
190775 if( zStart>=zTerm ) return SQLITE_DONE;
190776
190777 zOut = pCsr->zToken;
190778 do {
190779 int iOut;
190780
190781 /* Grow the output buffer if required. */
190782 if( (zOut-pCsr->zToken)>=(pCsr->nAlloc-4) ){
190783 char *zNew = sqlite3_realloc64(pOld: pCsr->zToken, n: pCsr->nAlloc+64);
190784 if( !zNew ) return SQLITE_NOMEM;
190785 zOut = &zNew[zOut - pCsr->zToken];
190786 pCsr->zToken = zNew;
190787 pCsr->nAlloc += 64;
190788 }
190789
190790 /* Write the folded case of the last character read to the output */
190791 zEnd = z;
190792 iOut = sqlite3FtsUnicodeFold((int)iCode, p->eRemoveDiacritic);
190793 if( iOut ){
190794 WRITE_UTF8(zOut, iOut);
190795 }
190796
190797 /* If the cursor is not at EOF, read the next character */
190798 if( z>=zTerm ) break;
190799 READ_UTF8(z, zTerm, iCode);
190800 }while( unicodeIsAlnum(p, iCode: (int)iCode)
190801 || sqlite3FtsUnicodeIsdiacritic((int)iCode)
190802 );
190803
190804 /* Set the output variables and return. */
190805 pCsr->iOff = (int)(z - pCsr->aInput);
190806 *paToken = pCsr->zToken;
190807 *pnToken = (int)(zOut - pCsr->zToken);
190808 *piStart = (int)(zStart - pCsr->aInput);
190809 *piEnd = (int)(zEnd - pCsr->aInput);
190810 *piPos = pCsr->iToken++;
190811 return SQLITE_OK;
190812}
190813
190814/*
190815** Set *ppModule to a pointer to the sqlite3_tokenizer_module
190816** structure for the unicode tokenizer.
190817*/
190818SQLITE_PRIVATE void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const **ppModule){
190819 static const sqlite3_tokenizer_module module = {
190820 0,
190821 unicodeCreate,
190822 unicodeDestroy,
190823 unicodeOpen,
190824 unicodeClose,
190825 unicodeNext,
190826 0,
190827 };
190828 *ppModule = &module;
190829}
190830
190831#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
190832#endif /* ifndef SQLITE_DISABLE_FTS3_UNICODE */
190833
190834/************** End of fts3_unicode.c ****************************************/
190835/************** Begin file fts3_unicode2.c ***********************************/
190836/*
190837** 2012-05-25
190838**
190839** The author disclaims copyright to this source code. In place of
190840** a legal notice, here is a blessing:
190841**
190842** May you do good and not evil.
190843** May you find forgiveness for yourself and forgive others.
190844** May you share freely, never taking more than you give.
190845**
190846******************************************************************************
190847*/
190848
190849/*
190850** DO NOT EDIT THIS MACHINE GENERATED FILE.
190851*/
190852
190853#ifndef SQLITE_DISABLE_FTS3_UNICODE
190854#if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4)
190855
190856/* #include <assert.h> */
190857
190858/*
190859** Return true if the argument corresponds to a unicode codepoint
190860** classified as either a letter or a number. Otherwise false.
190861**
190862** The results are undefined if the value passed to this function
190863** is less than zero.
190864*/
190865SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int c){
190866 /* Each unsigned integer in the following array corresponds to a contiguous
190867 ** range of unicode codepoints that are not either letters or numbers (i.e.
190868 ** codepoints for which this function should return 0).
190869 **
190870 ** The most significant 22 bits in each 32-bit value contain the first
190871 ** codepoint in the range. The least significant 10 bits are used to store
190872 ** the size of the range (always at least 1). In other words, the value
190873 ** ((C<<22) + N) represents a range of N codepoints starting with codepoint
190874 ** C. It is not possible to represent a range larger than 1023 codepoints
190875 ** using this format.
190876 */
190877 static const unsigned int aEntry[] = {
190878 0x00000030, 0x0000E807, 0x00016C06, 0x0001EC2F, 0x0002AC07,
190879 0x0002D001, 0x0002D803, 0x0002EC01, 0x0002FC01, 0x00035C01,
190880 0x0003DC01, 0x000B0804, 0x000B480E, 0x000B9407, 0x000BB401,
190881 0x000BBC81, 0x000DD401, 0x000DF801, 0x000E1002, 0x000E1C01,
190882 0x000FD801, 0x00120808, 0x00156806, 0x00162402, 0x00163C01,
190883 0x00164437, 0x0017CC02, 0x00180005, 0x00181816, 0x00187802,
190884 0x00192C15, 0x0019A804, 0x0019C001, 0x001B5001, 0x001B580F,
190885 0x001B9C07, 0x001BF402, 0x001C000E, 0x001C3C01, 0x001C4401,
190886 0x001CC01B, 0x001E980B, 0x001FAC09, 0x001FD804, 0x00205804,
190887 0x00206C09, 0x00209403, 0x0020A405, 0x0020C00F, 0x00216403,
190888 0x00217801, 0x0023901B, 0x00240004, 0x0024E803, 0x0024F812,
190889 0x00254407, 0x00258804, 0x0025C001, 0x00260403, 0x0026F001,
190890 0x0026F807, 0x00271C02, 0x00272C03, 0x00275C01, 0x00278802,
190891 0x0027C802, 0x0027E802, 0x00280403, 0x0028F001, 0x0028F805,
190892 0x00291C02, 0x00292C03, 0x00294401, 0x0029C002, 0x0029D401,
190893 0x002A0403, 0x002AF001, 0x002AF808, 0x002B1C03, 0x002B2C03,
190894 0x002B8802, 0x002BC002, 0x002C0403, 0x002CF001, 0x002CF807,
190895 0x002D1C02, 0x002D2C03, 0x002D5802, 0x002D8802, 0x002DC001,
190896 0x002E0801, 0x002EF805, 0x002F1803, 0x002F2804, 0x002F5C01,
190897 0x002FCC08, 0x00300403, 0x0030F807, 0x00311803, 0x00312804,
190898 0x00315402, 0x00318802, 0x0031FC01, 0x00320802, 0x0032F001,
190899 0x0032F807, 0x00331803, 0x00332804, 0x00335402, 0x00338802,
190900 0x00340802, 0x0034F807, 0x00351803, 0x00352804, 0x00355C01,
190901 0x00358802, 0x0035E401, 0x00360802, 0x00372801, 0x00373C06,
190902 0x00375801, 0x00376008, 0x0037C803, 0x0038C401, 0x0038D007,
190903 0x0038FC01, 0x00391C09, 0x00396802, 0x003AC401, 0x003AD006,
190904 0x003AEC02, 0x003B2006, 0x003C041F, 0x003CD00C, 0x003DC417,
190905 0x003E340B, 0x003E6424, 0x003EF80F, 0x003F380D, 0x0040AC14,
190906 0x00412806, 0x00415804, 0x00417803, 0x00418803, 0x00419C07,
190907 0x0041C404, 0x0042080C, 0x00423C01, 0x00426806, 0x0043EC01,
190908 0x004D740C, 0x004E400A, 0x00500001, 0x0059B402, 0x005A0001,
190909 0x005A6C02, 0x005BAC03, 0x005C4803, 0x005CC805, 0x005D4802,
190910 0x005DC802, 0x005ED023, 0x005F6004, 0x005F7401, 0x0060000F,
190911 0x0062A401, 0x0064800C, 0x0064C00C, 0x00650001, 0x00651002,
190912 0x0066C011, 0x00672002, 0x00677822, 0x00685C05, 0x00687802,
190913 0x0069540A, 0x0069801D, 0x0069FC01, 0x006A8007, 0x006AA006,
190914 0x006C0005, 0x006CD011, 0x006D6823, 0x006E0003, 0x006E840D,
190915 0x006F980E, 0x006FF004, 0x00709014, 0x0070EC05, 0x0071F802,
190916 0x00730008, 0x00734019, 0x0073B401, 0x0073C803, 0x00770027,
190917 0x0077F004, 0x007EF401, 0x007EFC03, 0x007F3403, 0x007F7403,
190918 0x007FB403, 0x007FF402, 0x00800065, 0x0081A806, 0x0081E805,
190919 0x00822805, 0x0082801A, 0x00834021, 0x00840002, 0x00840C04,
190920 0x00842002, 0x00845001, 0x00845803, 0x00847806, 0x00849401,
190921 0x00849C01, 0x0084A401, 0x0084B801, 0x0084E802, 0x00850005,
190922 0x00852804, 0x00853C01, 0x00864264, 0x00900027, 0x0091000B,
190923 0x0092704E, 0x00940200, 0x009C0475, 0x009E53B9, 0x00AD400A,
190924 0x00B39406, 0x00B3BC03, 0x00B3E404, 0x00B3F802, 0x00B5C001,
190925 0x00B5FC01, 0x00B7804F, 0x00B8C00C, 0x00BA001A, 0x00BA6C59,
190926 0x00BC00D6, 0x00BFC00C, 0x00C00005, 0x00C02019, 0x00C0A807,
190927 0x00C0D802, 0x00C0F403, 0x00C26404, 0x00C28001, 0x00C3EC01,
190928 0x00C64002, 0x00C6580A, 0x00C70024, 0x00C8001F, 0x00C8A81E,
190929 0x00C94001, 0x00C98020, 0x00CA2827, 0x00CB003F, 0x00CC0100,
190930 0x01370040, 0x02924037, 0x0293F802, 0x02983403, 0x0299BC10,
190931 0x029A7C01, 0x029BC008, 0x029C0017, 0x029C8002, 0x029E2402,
190932 0x02A00801, 0x02A01801, 0x02A02C01, 0x02A08C09, 0x02A0D804,
190933 0x02A1D004, 0x02A20002, 0x02A2D011, 0x02A33802, 0x02A38012,
190934 0x02A3E003, 0x02A4980A, 0x02A51C0D, 0x02A57C01, 0x02A60004,
190935 0x02A6CC1B, 0x02A77802, 0x02A8A40E, 0x02A90C01, 0x02A93002,
190936 0x02A97004, 0x02A9DC03, 0x02A9EC01, 0x02AAC001, 0x02AAC803,
190937 0x02AADC02, 0x02AAF802, 0x02AB0401, 0x02AB7802, 0x02ABAC07,
190938 0x02ABD402, 0x02AF8C0B, 0x03600001, 0x036DFC02, 0x036FFC02,
190939 0x037FFC01, 0x03EC7801, 0x03ECA401, 0x03EEC810, 0x03F4F802,
190940 0x03F7F002, 0x03F8001A, 0x03F88007, 0x03F8C023, 0x03F95013,
190941 0x03F9A004, 0x03FBFC01, 0x03FC040F, 0x03FC6807, 0x03FCEC06,
190942 0x03FD6C0B, 0x03FF8007, 0x03FFA007, 0x03FFE405, 0x04040003,
190943 0x0404DC09, 0x0405E411, 0x0406400C, 0x0407402E, 0x040E7C01,
190944 0x040F4001, 0x04215C01, 0x04247C01, 0x0424FC01, 0x04280403,
190945 0x04281402, 0x04283004, 0x0428E003, 0x0428FC01, 0x04294009,
190946 0x0429FC01, 0x042CE407, 0x04400003, 0x0440E016, 0x04420003,
190947 0x0442C012, 0x04440003, 0x04449C0E, 0x04450004, 0x04460003,
190948 0x0446CC0E, 0x04471404, 0x045AAC0D, 0x0491C004, 0x05BD442E,
190949 0x05BE3C04, 0x074000F6, 0x07440027, 0x0744A4B5, 0x07480046,
190950 0x074C0057, 0x075B0401, 0x075B6C01, 0x075BEC01, 0x075C5401,
190951 0x075CD401, 0x075D3C01, 0x075DBC01, 0x075E2401, 0x075EA401,
190952 0x075F0C01, 0x07BBC002, 0x07C0002C, 0x07C0C064, 0x07C2800F,
190953 0x07C2C40E, 0x07C3040F, 0x07C3440F, 0x07C4401F, 0x07C4C03C,
190954 0x07C5C02B, 0x07C7981D, 0x07C8402B, 0x07C90009, 0x07C94002,
190955 0x07CC0021, 0x07CCC006, 0x07CCDC46, 0x07CE0014, 0x07CE8025,
190956 0x07CF1805, 0x07CF8011, 0x07D0003F, 0x07D10001, 0x07D108B6,
190957 0x07D3E404, 0x07D4003E, 0x07D50004, 0x07D54018, 0x07D7EC46,
190958 0x07D9140B, 0x07DA0046, 0x07DC0074, 0x38000401, 0x38008060,
190959 0x380400F0,
190960 };
190961 static const unsigned int aAscii[4] = {
190962 0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001,
190963 };
190964
190965 if( (unsigned int)c<128 ){
190966 return ( (aAscii[c >> 5] & ((unsigned int)1 << (c & 0x001F)))==0 );
190967 }else if( (unsigned int)c<(1<<22) ){
190968 unsigned int key = (((unsigned int)c)<<10) | 0x000003FF;
190969 int iRes = 0;
190970 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
190971 int iLo = 0;
190972 while( iHi>=iLo ){
190973 int iTest = (iHi + iLo) / 2;
190974 if( key >= aEntry[iTest] ){
190975 iRes = iTest;
190976 iLo = iTest+1;
190977 }else{
190978 iHi = iTest-1;
190979 }
190980 }
190981 assert( aEntry[0]<key );
190982 assert( key>=aEntry[iRes] );
190983 return (((unsigned int)c) >= ((aEntry[iRes]>>10) + (aEntry[iRes]&0x3FF)));
190984 }
190985 return 1;
190986}
190987
190988
190989/*
190990** If the argument is a codepoint corresponding to a lowercase letter
190991** in the ASCII range with a diacritic added, return the codepoint
190992** of the ASCII letter only. For example, if passed 235 - "LATIN
190993** SMALL LETTER E WITH DIAERESIS" - return 65 ("LATIN SMALL LETTER
190994** E"). The resuls of passing a codepoint that corresponds to an
190995** uppercase letter are undefined.
190996*/
190997static int remove_diacritic(int c, int bComplex){
190998 unsigned short aDia[] = {
190999 0, 1797, 1848, 1859, 1891, 1928, 1940, 1995,
191000 2024, 2040, 2060, 2110, 2168, 2206, 2264, 2286,
191001 2344, 2383, 2472, 2488, 2516, 2596, 2668, 2732,
191002 2782, 2842, 2894, 2954, 2984, 3000, 3028, 3336,
191003 3456, 3696, 3712, 3728, 3744, 3766, 3832, 3896,
191004 3912, 3928, 3944, 3968, 4008, 4040, 4056, 4106,
191005 4138, 4170, 4202, 4234, 4266, 4296, 4312, 4344,
191006 4408, 4424, 4442, 4472, 4488, 4504, 6148, 6198,
191007 6264, 6280, 6360, 6429, 6505, 6529, 61448, 61468,
191008 61512, 61534, 61592, 61610, 61642, 61672, 61688, 61704,
191009 61726, 61784, 61800, 61816, 61836, 61880, 61896, 61914,
191010 61948, 61998, 62062, 62122, 62154, 62184, 62200, 62218,
191011 62252, 62302, 62364, 62410, 62442, 62478, 62536, 62554,
191012 62584, 62604, 62640, 62648, 62656, 62664, 62730, 62766,
191013 62830, 62890, 62924, 62974, 63032, 63050, 63082, 63118,
191014 63182, 63242, 63274, 63310, 63368, 63390,
191015 };
191016#define HIBIT ((unsigned char)0x80)
191017 unsigned char aChar[] = {
191018 '\0', 'a', 'c', 'e', 'i', 'n',
191019 'o', 'u', 'y', 'y', 'a', 'c',
191020 'd', 'e', 'e', 'g', 'h', 'i',
191021 'j', 'k', 'l', 'n', 'o', 'r',
191022 's', 't', 'u', 'u', 'w', 'y',
191023 'z', 'o', 'u', 'a', 'i', 'o',
191024 'u', 'u'|HIBIT, 'a'|HIBIT, 'g', 'k', 'o',
191025 'o'|HIBIT, 'j', 'g', 'n', 'a'|HIBIT, 'a',
191026 'e', 'i', 'o', 'r', 'u', 's',
191027 't', 'h', 'a', 'e', 'o'|HIBIT, 'o',
191028 'o'|HIBIT, 'y', '\0', '\0', '\0', '\0',
191029 '\0', '\0', '\0', '\0', 'a', 'b',
191030 'c'|HIBIT, 'd', 'd', 'e'|HIBIT, 'e', 'e'|HIBIT,
191031 'f', 'g', 'h', 'h', 'i', 'i'|HIBIT,
191032 'k', 'l', 'l'|HIBIT, 'l', 'm', 'n',
191033 'o'|HIBIT, 'p', 'r', 'r'|HIBIT, 'r', 's',
191034 's'|HIBIT, 't', 'u', 'u'|HIBIT, 'v', 'w',
191035 'w', 'x', 'y', 'z', 'h', 't',
191036 'w', 'y', 'a', 'a'|HIBIT, 'a'|HIBIT, 'a'|HIBIT,
191037 'e', 'e'|HIBIT, 'e'|HIBIT, 'i', 'o', 'o'|HIBIT,
191038 'o'|HIBIT, 'o'|HIBIT, 'u', 'u'|HIBIT, 'u'|HIBIT, 'y',
191039 };
191040
191041 unsigned int key = (((unsigned int)c)<<3) | 0x00000007;
191042 int iRes = 0;
191043 int iHi = sizeof(aDia)/sizeof(aDia[0]) - 1;
191044 int iLo = 0;
191045 while( iHi>=iLo ){
191046 int iTest = (iHi + iLo) / 2;
191047 if( key >= aDia[iTest] ){
191048 iRes = iTest;
191049 iLo = iTest+1;
191050 }else{
191051 iHi = iTest-1;
191052 }
191053 }
191054 assert( key>=aDia[iRes] );
191055 if( bComplex==0 && (aChar[iRes] & 0x80) ) return c;
191056 return (c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : ((int)aChar[iRes] & 0x7F);
191057}
191058
191059
191060/*
191061** Return true if the argument interpreted as a unicode codepoint
191062** is a diacritical modifier character.
191063*/
191064SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int c){
191065 unsigned int mask0 = 0x08029FDF;
191066 unsigned int mask1 = 0x000361F8;
191067 if( c<768 || c>817 ) return 0;
191068 return (c < 768+32) ?
191069 (mask0 & ((unsigned int)1 << (c-768))) :
191070 (mask1 & ((unsigned int)1 << (c-768-32)));
191071}
191072
191073
191074/*
191075** Interpret the argument as a unicode codepoint. If the codepoint
191076** is an upper case character that has a lower case equivalent,
191077** return the codepoint corresponding to the lower case version.
191078** Otherwise, return a copy of the argument.
191079**
191080** The results are undefined if the value passed to this function
191081** is less than zero.
191082*/
191083SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int c, int eRemoveDiacritic){
191084 /* Each entry in the following array defines a rule for folding a range
191085 ** of codepoints to lower case. The rule applies to a range of nRange
191086 ** codepoints starting at codepoint iCode.
191087 **
191088 ** If the least significant bit in flags is clear, then the rule applies
191089 ** to all nRange codepoints (i.e. all nRange codepoints are upper case and
191090 ** need to be folded). Or, if it is set, then the rule only applies to
191091 ** every second codepoint in the range, starting with codepoint C.
191092 **
191093 ** The 7 most significant bits in flags are an index into the aiOff[]
191094 ** array. If a specific codepoint C does require folding, then its lower
191095 ** case equivalent is ((C + aiOff[flags>>1]) & 0xFFFF).
191096 **
191097 ** The contents of this array are generated by parsing the CaseFolding.txt
191098 ** file distributed as part of the "Unicode Character Database". See
191099 ** http://www.unicode.org for details.
191100 */
191101 static const struct TableEntry {
191102 unsigned short iCode;
191103 unsigned char flags;
191104 unsigned char nRange;
191105 } aEntry[] = {
191106 {65, 14, 26}, {181, 64, 1}, {192, 14, 23},
191107 {216, 14, 7}, {256, 1, 48}, {306, 1, 6},
191108 {313, 1, 16}, {330, 1, 46}, {376, 116, 1},
191109 {377, 1, 6}, {383, 104, 1}, {385, 50, 1},
191110 {386, 1, 4}, {390, 44, 1}, {391, 0, 1},
191111 {393, 42, 2}, {395, 0, 1}, {398, 32, 1},
191112 {399, 38, 1}, {400, 40, 1}, {401, 0, 1},
191113 {403, 42, 1}, {404, 46, 1}, {406, 52, 1},
191114 {407, 48, 1}, {408, 0, 1}, {412, 52, 1},
191115 {413, 54, 1}, {415, 56, 1}, {416, 1, 6},
191116 {422, 60, 1}, {423, 0, 1}, {425, 60, 1},
191117 {428, 0, 1}, {430, 60, 1}, {431, 0, 1},
191118 {433, 58, 2}, {435, 1, 4}, {439, 62, 1},
191119 {440, 0, 1}, {444, 0, 1}, {452, 2, 1},
191120 {453, 0, 1}, {455, 2, 1}, {456, 0, 1},
191121 {458, 2, 1}, {459, 1, 18}, {478, 1, 18},
191122 {497, 2, 1}, {498, 1, 4}, {502, 122, 1},
191123 {503, 134, 1}, {504, 1, 40}, {544, 110, 1},
191124 {546, 1, 18}, {570, 70, 1}, {571, 0, 1},
191125 {573, 108, 1}, {574, 68, 1}, {577, 0, 1},
191126 {579, 106, 1}, {580, 28, 1}, {581, 30, 1},
191127 {582, 1, 10}, {837, 36, 1}, {880, 1, 4},
191128 {886, 0, 1}, {902, 18, 1}, {904, 16, 3},
191129 {908, 26, 1}, {910, 24, 2}, {913, 14, 17},
191130 {931, 14, 9}, {962, 0, 1}, {975, 4, 1},
191131 {976, 140, 1}, {977, 142, 1}, {981, 146, 1},
191132 {982, 144, 1}, {984, 1, 24}, {1008, 136, 1},
191133 {1009, 138, 1}, {1012, 130, 1}, {1013, 128, 1},
191134 {1015, 0, 1}, {1017, 152, 1}, {1018, 0, 1},
191135 {1021, 110, 3}, {1024, 34, 16}, {1040, 14, 32},
191136 {1120, 1, 34}, {1162, 1, 54}, {1216, 6, 1},
191137 {1217, 1, 14}, {1232, 1, 88}, {1329, 22, 38},
191138 {4256, 66, 38}, {4295, 66, 1}, {4301, 66, 1},
191139 {7680, 1, 150}, {7835, 132, 1}, {7838, 96, 1},
191140 {7840, 1, 96}, {7944, 150, 8}, {7960, 150, 6},
191141 {7976, 150, 8}, {7992, 150, 8}, {8008, 150, 6},
191142 {8025, 151, 8}, {8040, 150, 8}, {8072, 150, 8},
191143 {8088, 150, 8}, {8104, 150, 8}, {8120, 150, 2},
191144 {8122, 126, 2}, {8124, 148, 1}, {8126, 100, 1},
191145 {8136, 124, 4}, {8140, 148, 1}, {8152, 150, 2},
191146 {8154, 120, 2}, {8168, 150, 2}, {8170, 118, 2},
191147 {8172, 152, 1}, {8184, 112, 2}, {8186, 114, 2},
191148 {8188, 148, 1}, {8486, 98, 1}, {8490, 92, 1},
191149 {8491, 94, 1}, {8498, 12, 1}, {8544, 8, 16},
191150 {8579, 0, 1}, {9398, 10, 26}, {11264, 22, 47},
191151 {11360, 0, 1}, {11362, 88, 1}, {11363, 102, 1},
191152 {11364, 90, 1}, {11367, 1, 6}, {11373, 84, 1},
191153 {11374, 86, 1}, {11375, 80, 1}, {11376, 82, 1},
191154 {11378, 0, 1}, {11381, 0, 1}, {11390, 78, 2},
191155 {11392, 1, 100}, {11499, 1, 4}, {11506, 0, 1},
191156 {42560, 1, 46}, {42624, 1, 24}, {42786, 1, 14},
191157 {42802, 1, 62}, {42873, 1, 4}, {42877, 76, 1},
191158 {42878, 1, 10}, {42891, 0, 1}, {42893, 74, 1},
191159 {42896, 1, 4}, {42912, 1, 10}, {42922, 72, 1},
191160 {65313, 14, 26},
191161 };
191162 static const unsigned short aiOff[] = {
191163 1, 2, 8, 15, 16, 26, 28, 32,
191164 37, 38, 40, 48, 63, 64, 69, 71,
191165 79, 80, 116, 202, 203, 205, 206, 207,
191166 209, 210, 211, 213, 214, 217, 218, 219,
191167 775, 7264, 10792, 10795, 23228, 23256, 30204, 54721,
191168 54753, 54754, 54756, 54787, 54793, 54809, 57153, 57274,
191169 57921, 58019, 58363, 61722, 65268, 65341, 65373, 65406,
191170 65408, 65410, 65415, 65424, 65436, 65439, 65450, 65462,
191171 65472, 65476, 65478, 65480, 65482, 65488, 65506, 65511,
191172 65514, 65521, 65527, 65528, 65529,
191173 };
191174
191175 int ret = c;
191176
191177 assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 );
191178
191179 if( c<128 ){
191180 if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
191181 }else if( c<65536 ){
191182 const struct TableEntry *p;
191183 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
191184 int iLo = 0;
191185 int iRes = -1;
191186
191187 assert( c>aEntry[0].iCode );
191188 while( iHi>=iLo ){
191189 int iTest = (iHi + iLo) / 2;
191190 int cmp = (c - aEntry[iTest].iCode);
191191 if( cmp>=0 ){
191192 iRes = iTest;
191193 iLo = iTest+1;
191194 }else{
191195 iHi = iTest-1;
191196 }
191197 }
191198
191199 assert( iRes>=0 && c>=aEntry[iRes].iCode );
191200 p = &aEntry[iRes];
191201 if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
191202 ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
191203 assert( ret>0 );
191204 }
191205
191206 if( eRemoveDiacritic ){
191207 ret = remove_diacritic(c: ret, bComplex: eRemoveDiacritic==2);
191208 }
191209 }
191210
191211 else if( c>=66560 && c<66600 ){
191212 ret = c + 40;
191213 }
191214
191215 return ret;
191216}
191217#endif /* defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) */
191218#endif /* !defined(SQLITE_DISABLE_FTS3_UNICODE) */
191219
191220/************** End of fts3_unicode2.c ***************************************/
191221/************** Begin file json1.c *******************************************/
191222/*
191223** 2015-08-12
191224**
191225** The author disclaims copyright to this source code. In place of
191226** a legal notice, here is a blessing:
191227**
191228** May you do good and not evil.
191229** May you find forgiveness for yourself and forgive others.
191230** May you share freely, never taking more than you give.
191231**
191232******************************************************************************
191233**
191234** This SQLite extension implements JSON functions. The interface is
191235** modeled after MySQL JSON functions:
191236**
191237** https://dev.mysql.com/doc/refman/5.7/en/json.html
191238**
191239** For the time being, all JSON is stored as pure text. (We might add
191240** a JSONB type in the future which stores a binary encoding of JSON in
191241** a BLOB, but there is no support for JSONB in the current implementation.
191242** This implementation parses JSON text at 250 MB/s, so it is hard to see
191243** how JSONB might improve on that.)
191244*/
191245#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1)
191246#if !defined(SQLITEINT_H)
191247/* #include "sqlite3ext.h" */
191248#endif
191249SQLITE_EXTENSION_INIT1
191250/* #include <assert.h> */
191251/* #include <string.h> */
191252/* #include <stdlib.h> */
191253/* #include <stdarg.h> */
191254
191255/* Mark a function parameter as unused, to suppress nuisance compiler
191256** warnings. */
191257#ifndef UNUSED_PARAM
191258# define UNUSED_PARAM(X) (void)(X)
191259#endif
191260
191261#ifndef LARGEST_INT64
191262# define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
191263# define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
191264#endif
191265
191266#ifndef deliberate_fall_through
191267# define deliberate_fall_through
191268#endif
191269
191270/*
191271** Versions of isspace(), isalnum() and isdigit() to which it is safe
191272** to pass signed char values.
191273*/
191274#ifdef sqlite3Isdigit
191275 /* Use the SQLite core versions if this routine is part of the
191276 ** SQLite amalgamation */
191277# define safe_isdigit(x) sqlite3Isdigit(x)
191278# define safe_isalnum(x) sqlite3Isalnum(x)
191279# define safe_isxdigit(x) sqlite3Isxdigit(x)
191280#else
191281 /* Use the standard library for separate compilation */
191282#include <ctype.h> /* amalgamator: keep */
191283# define safe_isdigit(x) isdigit((unsigned char)(x))
191284# define safe_isalnum(x) isalnum((unsigned char)(x))
191285# define safe_isxdigit(x) isxdigit((unsigned char)(x))
191286#endif
191287
191288/*
191289** Growing our own isspace() routine this way is twice as fast as
191290** the library isspace() function, resulting in a 7% overall performance
191291** increase for the parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os).
191292*/
191293static const char jsonIsSpace[] = {
191294 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
191295 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
191296 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
191297 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
191298 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
191299 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
191300 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
191301 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
191302 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
191303 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
191304 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
191305 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
191306 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
191307 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
191308 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
191309 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
191310};
191311#define safe_isspace(x) (jsonIsSpace[(unsigned char)x])
191312
191313#ifndef SQLITE_AMALGAMATION
191314 /* Unsigned integer types. These are already defined in the sqliteInt.h,
191315 ** but the definitions need to be repeated for separate compilation. */
191316 typedef sqlite3_uint64 u64;
191317 typedef unsigned int u32;
191318 typedef unsigned short int u16;
191319 typedef unsigned char u8;
191320# if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
191321# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
191322# endif
191323# if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
191324# define ALWAYS(X) (1)
191325# define NEVER(X) (0)
191326# elif !defined(NDEBUG)
191327# define ALWAYS(X) ((X)?1:(assert(0),0))
191328# define NEVER(X) ((X)?(assert(0),1):0)
191329# else
191330# define ALWAYS(X) (X)
191331# define NEVER(X) (X)
191332# endif
191333# define testcase(X)
191334#endif
191335#if !defined(SQLITE_DEBUG) && !defined(SQLITE_COVERAGE_TEST)
191336# define VVA(X)
191337#else
191338# define VVA(X) X
191339#endif
191340
191341/*
191342** Some of the testcase() macros in this file are problematic for gcov
191343** in that they generate false-miss errors randomly. This is a gcov problem,
191344** not a problem in this case. But to work around it, we disable the
191345** problematic test cases for production builds.
191346*/
191347#define json_testcase(X)
191348
191349/* Objects */
191350typedef struct JsonString JsonString;
191351typedef struct JsonNode JsonNode;
191352typedef struct JsonParse JsonParse;
191353
191354/* An instance of this object represents a JSON string
191355** under construction. Really, this is a generic string accumulator
191356** that can be and is used to create strings other than JSON.
191357*/
191358struct JsonString {
191359 sqlite3_context *pCtx; /* Function context - put error messages here */
191360 char *zBuf; /* Append JSON content here */
191361 u64 nAlloc; /* Bytes of storage available in zBuf[] */
191362 u64 nUsed; /* Bytes of zBuf[] currently used */
191363 u8 bStatic; /* True if zBuf is static space */
191364 u8 bErr; /* True if an error has been encountered */
191365 char zSpace[100]; /* Initial static space */
191366};
191367
191368/* JSON type values
191369*/
191370#define JSON_NULL 0
191371#define JSON_TRUE 1
191372#define JSON_FALSE 2
191373#define JSON_INT 3
191374#define JSON_REAL 4
191375#define JSON_STRING 5
191376#define JSON_ARRAY 6
191377#define JSON_OBJECT 7
191378
191379/* The "subtype" set for JSON values */
191380#define JSON_SUBTYPE 74 /* Ascii for "J" */
191381
191382/*
191383** Names of the various JSON types:
191384*/
191385static const char * const jsonType[] = {
191386 "null", "true", "false", "integer", "real", "text", "array", "object"
191387};
191388
191389/* Bit values for the JsonNode.jnFlag field
191390*/
191391#define JNODE_RAW 0x01 /* Content is raw, not JSON encoded */
191392#define JNODE_ESCAPE 0x02 /* Content is text with \ escapes */
191393#define JNODE_REMOVE 0x04 /* Do not output */
191394#define JNODE_REPLACE 0x08 /* Replace with JsonNode.u.iReplace */
191395#define JNODE_PATCH 0x10 /* Patch with JsonNode.u.pPatch */
191396#define JNODE_APPEND 0x20 /* More ARRAY/OBJECT entries at u.iAppend */
191397#define JNODE_LABEL 0x40 /* Is a label of an object */
191398
191399
191400/* A single node of parsed JSON
191401*/
191402struct JsonNode {
191403 u8 eType; /* One of the JSON_ type values */
191404 u8 jnFlags; /* JNODE flags */
191405 u8 eU; /* Which union element to use */
191406 u32 n; /* Bytes of content, or number of sub-nodes */
191407 union {
191408 const char *zJContent; /* 1: Content for INT, REAL, and STRING */
191409 u32 iAppend; /* 2: More terms for ARRAY and OBJECT */
191410 u32 iKey; /* 3: Key for ARRAY objects in json_tree() */
191411 u32 iReplace; /* 4: Replacement content for JNODE_REPLACE */
191412 JsonNode *pPatch; /* 5: Node chain of patch for JNODE_PATCH */
191413 } u;
191414};
191415
191416/* A completely parsed JSON string
191417*/
191418struct JsonParse {
191419 u32 nNode; /* Number of slots of aNode[] used */
191420 u32 nAlloc; /* Number of slots of aNode[] allocated */
191421 JsonNode *aNode; /* Array of nodes containing the parse */
191422 const char *zJson; /* Original JSON string */
191423 u32 *aUp; /* Index of parent of each node */
191424 u8 oom; /* Set to true if out of memory */
191425 u8 nErr; /* Number of errors seen */
191426 u16 iDepth; /* Nesting depth */
191427 int nJson; /* Length of the zJson string in bytes */
191428 u32 iHold; /* Replace cache line with the lowest iHold value */
191429};
191430
191431/*
191432** Maximum nesting depth of JSON for this implementation.
191433**
191434** This limit is needed to avoid a stack overflow in the recursive
191435** descent parser. A depth of 2000 is far deeper than any sane JSON
191436** should go.
191437*/
191438#define JSON_MAX_DEPTH 2000
191439
191440/**************************************************************************
191441** Utility routines for dealing with JsonString objects
191442**************************************************************************/
191443
191444/* Set the JsonString object to an empty string
191445*/
191446static void jsonZero(JsonString *p){
191447 p->zBuf = p->zSpace;
191448 p->nAlloc = sizeof(p->zSpace);
191449 p->nUsed = 0;
191450 p->bStatic = 1;
191451}
191452
191453/* Initialize the JsonString object
191454*/
191455static void jsonInit(JsonString *p, sqlite3_context *pCtx){
191456 p->pCtx = pCtx;
191457 p->bErr = 0;
191458 jsonZero(p);
191459}
191460
191461
191462/* Free all allocated memory and reset the JsonString object back to its
191463** initial state.
191464*/
191465static void jsonReset(JsonString *p){
191466 if( !p->bStatic ) sqlite3_free(p: p->zBuf);
191467 jsonZero(p);
191468}
191469
191470
191471/* Report an out-of-memory (OOM) condition
191472*/
191473static void jsonOom(JsonString *p){
191474 p->bErr = 1;
191475 sqlite3_result_error_nomem(pCtx: p->pCtx);
191476 jsonReset(p);
191477}
191478
191479/* Enlarge pJson->zBuf so that it can hold at least N more bytes.
191480** Return zero on success. Return non-zero on an OOM error
191481*/
191482static int jsonGrow(JsonString *p, u32 N){
191483 u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
191484 char *zNew;
191485 if( p->bStatic ){
191486 if( p->bErr ) return 1;
191487 zNew = sqlite3_malloc64(n: nTotal);
191488 if( zNew==0 ){
191489 jsonOom(p);
191490 return SQLITE_NOMEM;
191491 }
191492 memcpy(dest: zNew, src: p->zBuf, n: (size_t)p->nUsed);
191493 p->zBuf = zNew;
191494 p->bStatic = 0;
191495 }else{
191496 zNew = sqlite3_realloc64(pOld: p->zBuf, n: nTotal);
191497 if( zNew==0 ){
191498 jsonOom(p);
191499 return SQLITE_NOMEM;
191500 }
191501 p->zBuf = zNew;
191502 }
191503 p->nAlloc = nTotal;
191504 return SQLITE_OK;
191505}
191506
191507/* Append N bytes from zIn onto the end of the JsonString string.
191508*/
191509static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){
191510 if( N==0 ) return;
191511 if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return;
191512 memcpy(dest: p->zBuf+p->nUsed, src: zIn, n: N);
191513 p->nUsed += N;
191514}
191515
191516/* Append formatted text (not to exceed N bytes) to the JsonString.
191517*/
191518static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){
191519 va_list ap;
191520 if( (p->nUsed + N >= p->nAlloc) && jsonGrow(p, N) ) return;
191521 va_start(ap, zFormat);
191522 sqlite3_vsnprintf(n: N, zBuf: p->zBuf+p->nUsed, zFormat, ap);
191523 va_end(ap);
191524 p->nUsed += (int)strlen(s: p->zBuf+p->nUsed);
191525}
191526
191527/* Append a single character
191528*/
191529static void jsonAppendChar(JsonString *p, char c){
191530 if( p->nUsed>=p->nAlloc && jsonGrow(p,N: 1)!=0 ) return;
191531 p->zBuf[p->nUsed++] = c;
191532}
191533
191534/* Append a comma separator to the output buffer, if the previous
191535** character is not '[' or '{'.
191536*/
191537static void jsonAppendSeparator(JsonString *p){
191538 char c;
191539 if( p->nUsed==0 ) return;
191540 c = p->zBuf[p->nUsed-1];
191541 if( c!='[' && c!='{' ) jsonAppendChar(p, c: ',');
191542}
191543
191544/* Append the N-byte string in zIn to the end of the JsonString string
191545** under construction. Enclose the string in "..." and escape
191546** any double-quotes or backslash characters contained within the
191547** string.
191548*/
191549static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
191550 u32 i;
191551 if( zIn==0 || ((N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N: N+2)!=0) ) return;
191552 p->zBuf[p->nUsed++] = '"';
191553 for(i=0; i<N; i++){
191554 unsigned char c = ((unsigned const char*)zIn)[i];
191555 if( c=='"' || c=='\\' ){
191556 json_simple_escape:
191557 if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N: N+3-i)!=0 ) return;
191558 p->zBuf[p->nUsed++] = '\\';
191559 }else if( c<=0x1f ){
191560 static const char aSpecial[] = {
191561 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
191562 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
191563 };
191564 assert( sizeof(aSpecial)==32 );
191565 assert( aSpecial['\b']=='b' );
191566 assert( aSpecial['\f']=='f' );
191567 assert( aSpecial['\n']=='n' );
191568 assert( aSpecial['\r']=='r' );
191569 assert( aSpecial['\t']=='t' );
191570 if( aSpecial[c] ){
191571 c = aSpecial[c];
191572 goto json_simple_escape;
191573 }
191574 if( (p->nUsed+N+7+i > p->nAlloc) && jsonGrow(p,N: N+7-i)!=0 ) return;
191575 p->zBuf[p->nUsed++] = '\\';
191576 p->zBuf[p->nUsed++] = 'u';
191577 p->zBuf[p->nUsed++] = '0';
191578 p->zBuf[p->nUsed++] = '0';
191579 p->zBuf[p->nUsed++] = '0' + (c>>4);
191580 c = "0123456789abcdef"[c&0xf];
191581 }
191582 p->zBuf[p->nUsed++] = c;
191583 }
191584 p->zBuf[p->nUsed++] = '"';
191585 assert( p->nUsed<p->nAlloc );
191586}
191587
191588/*
191589** Append a function parameter value to the JSON string under
191590** construction.
191591*/
191592static void jsonAppendValue(
191593 JsonString *p, /* Append to this JSON string */
191594 sqlite3_value *pValue /* Value to append */
191595){
191596 switch( sqlite3_value_type(pVal: pValue) ){
191597 case SQLITE_NULL: {
191598 jsonAppendRaw(p, zIn: "null", N: 4);
191599 break;
191600 }
191601 case SQLITE_INTEGER:
191602 case SQLITE_FLOAT: {
191603 const char *z = (const char*)sqlite3_value_text(pVal: pValue);
191604 u32 n = (u32)sqlite3_value_bytes(pVal: pValue);
191605 jsonAppendRaw(p, zIn: z, N: n);
191606 break;
191607 }
191608 case SQLITE_TEXT: {
191609 const char *z = (const char*)sqlite3_value_text(pVal: pValue);
191610 u32 n = (u32)sqlite3_value_bytes(pVal: pValue);
191611 if( sqlite3_value_subtype(pVal: pValue)==JSON_SUBTYPE ){
191612 jsonAppendRaw(p, zIn: z, N: n);
191613 }else{
191614 jsonAppendString(p, zIn: z, N: n);
191615 }
191616 break;
191617 }
191618 default: {
191619 if( p->bErr==0 ){
191620 sqlite3_result_error(pCtx: p->pCtx, z: "JSON cannot hold BLOB values", n: -1);
191621 p->bErr = 2;
191622 jsonReset(p);
191623 }
191624 break;
191625 }
191626 }
191627}
191628
191629
191630/* Make the JSON in p the result of the SQL function.
191631*/
191632static void jsonResult(JsonString *p){
191633 if( p->bErr==0 ){
191634 sqlite3_result_text64(pCtx: p->pCtx, z: p->zBuf, n: p->nUsed,
191635 xDel: p->bStatic ? SQLITE_TRANSIENT : sqlite3_free,
191636 SQLITE_UTF8);
191637 jsonZero(p);
191638 }
191639 assert( p->bStatic );
191640}
191641
191642/**************************************************************************
191643** Utility routines for dealing with JsonNode and JsonParse objects
191644**************************************************************************/
191645
191646/*
191647** Return the number of consecutive JsonNode slots need to represent
191648** the parsed JSON at pNode. The minimum answer is 1. For ARRAY and
191649** OBJECT types, the number might be larger.
191650**
191651** Appended elements are not counted. The value returned is the number
191652** by which the JsonNode counter should increment in order to go to the
191653** next peer value.
191654*/
191655static u32 jsonNodeSize(JsonNode *pNode){
191656 return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1;
191657}
191658
191659/*
191660** Reclaim all memory allocated by a JsonParse object. But do not
191661** delete the JsonParse object itself.
191662*/
191663static void jsonParseReset(JsonParse *pParse){
191664 sqlite3_free(p: pParse->aNode);
191665 pParse->aNode = 0;
191666 pParse->nNode = 0;
191667 pParse->nAlloc = 0;
191668 sqlite3_free(p: pParse->aUp);
191669 pParse->aUp = 0;
191670}
191671
191672/*
191673** Free a JsonParse object that was obtained from sqlite3_malloc().
191674*/
191675static void jsonParseFree(JsonParse *pParse){
191676 jsonParseReset(pParse);
191677 sqlite3_free(p: pParse);
191678}
191679
191680/*
191681** Convert the JsonNode pNode into a pure JSON string and
191682** append to pOut. Subsubstructure is also included. Return
191683** the number of JsonNode objects that are encoded.
191684*/
191685static void jsonRenderNode(
191686 JsonNode *pNode, /* The node to render */
191687 JsonString *pOut, /* Write JSON here */
191688 sqlite3_value **aReplace /* Replacement values */
191689){
191690 assert( pNode!=0 );
191691 if( pNode->jnFlags & (JNODE_REPLACE|JNODE_PATCH) ){
191692 if( (pNode->jnFlags & JNODE_REPLACE)!=0 && ALWAYS(aReplace!=0) ){
191693 assert( pNode->eU==4 );
191694 jsonAppendValue(p: pOut, pValue: aReplace[pNode->u.iReplace]);
191695 return;
191696 }
191697 assert( pNode->eU==5 );
191698 pNode = pNode->u.pPatch;
191699 }
191700 switch( pNode->eType ){
191701 default: {
191702 assert( pNode->eType==JSON_NULL );
191703 jsonAppendRaw(p: pOut, zIn: "null", N: 4);
191704 break;
191705 }
191706 case JSON_TRUE: {
191707 jsonAppendRaw(p: pOut, zIn: "true", N: 4);
191708 break;
191709 }
191710 case JSON_FALSE: {
191711 jsonAppendRaw(p: pOut, zIn: "false", N: 5);
191712 break;
191713 }
191714 case JSON_STRING: {
191715 if( pNode->jnFlags & JNODE_RAW ){
191716 assert( pNode->eU==1 );
191717 jsonAppendString(p: pOut, zIn: pNode->u.zJContent, N: pNode->n);
191718 break;
191719 }
191720 /* no break */ deliberate_fall_through
191721 }
191722 case JSON_REAL:
191723 case JSON_INT: {
191724 assert( pNode->eU==1 );
191725 jsonAppendRaw(p: pOut, zIn: pNode->u.zJContent, N: pNode->n);
191726 break;
191727 }
191728 case JSON_ARRAY: {
191729 u32 j = 1;
191730 jsonAppendChar(p: pOut, c: '[');
191731 for(;;){
191732 while( j<=pNode->n ){
191733 if( (pNode[j].jnFlags & JNODE_REMOVE)==0 ){
191734 jsonAppendSeparator(p: pOut);
191735 jsonRenderNode(pNode: &pNode[j], pOut, aReplace);
191736 }
191737 j += jsonNodeSize(pNode: &pNode[j]);
191738 }
191739 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
191740 assert( pNode->eU==2 );
191741 pNode = &pNode[pNode->u.iAppend];
191742 j = 1;
191743 }
191744 jsonAppendChar(p: pOut, c: ']');
191745 break;
191746 }
191747 case JSON_OBJECT: {
191748 u32 j = 1;
191749 jsonAppendChar(p: pOut, c: '{');
191750 for(;;){
191751 while( j<=pNode->n ){
191752 if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 ){
191753 jsonAppendSeparator(p: pOut);
191754 jsonRenderNode(pNode: &pNode[j], pOut, aReplace);
191755 jsonAppendChar(p: pOut, c: ':');
191756 jsonRenderNode(pNode: &pNode[j+1], pOut, aReplace);
191757 }
191758 j += 1 + jsonNodeSize(pNode: &pNode[j+1]);
191759 }
191760 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
191761 assert( pNode->eU==2 );
191762 pNode = &pNode[pNode->u.iAppend];
191763 j = 1;
191764 }
191765 jsonAppendChar(p: pOut, c: '}');
191766 break;
191767 }
191768 }
191769}
191770
191771/*
191772** Return a JsonNode and all its descendents as a JSON string.
191773*/
191774static void jsonReturnJson(
191775 JsonNode *pNode, /* Node to return */
191776 sqlite3_context *pCtx, /* Return value for this function */
191777 sqlite3_value **aReplace /* Array of replacement values */
191778){
191779 JsonString s;
191780 jsonInit(p: &s, pCtx);
191781 jsonRenderNode(pNode, pOut: &s, aReplace);
191782 jsonResult(p: &s);
191783 sqlite3_result_subtype(pCtx, JSON_SUBTYPE);
191784}
191785
191786/*
191787** Translate a single byte of Hex into an integer.
191788** This routine only works if h really is a valid hexadecimal
191789** character: 0..9a..fA..F
191790*/
191791static u8 jsonHexToInt(int h){
191792 assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') );
191793#ifdef SQLITE_EBCDIC
191794 h += 9*(1&~(h>>4));
191795#else
191796 h += 9*(1&(h>>6));
191797#endif
191798 return (u8)(h & 0xf);
191799}
191800
191801/*
191802** Convert a 4-byte hex string into an integer
191803*/
191804static u32 jsonHexToInt4(const char *z){
191805 u32 v;
191806 assert( safe_isxdigit(z[0]) );
191807 assert( safe_isxdigit(z[1]) );
191808 assert( safe_isxdigit(z[2]) );
191809 assert( safe_isxdigit(z[3]) );
191810 v = (jsonHexToInt(h: z[0])<<12)
191811 + (jsonHexToInt(h: z[1])<<8)
191812 + (jsonHexToInt(h: z[2])<<4)
191813 + jsonHexToInt(h: z[3]);
191814 return v;
191815}
191816
191817/*
191818** Make the JsonNode the return value of the function.
191819*/
191820static void jsonReturn(
191821 JsonNode *pNode, /* Node to return */
191822 sqlite3_context *pCtx, /* Return value for this function */
191823 sqlite3_value **aReplace /* Array of replacement values */
191824){
191825 switch( pNode->eType ){
191826 default: {
191827 assert( pNode->eType==JSON_NULL );
191828 sqlite3_result_null(pCtx);
191829 break;
191830 }
191831 case JSON_TRUE: {
191832 sqlite3_result_int(pCtx, iVal: 1);
191833 break;
191834 }
191835 case JSON_FALSE: {
191836 sqlite3_result_int(pCtx, iVal: 0);
191837 break;
191838 }
191839 case JSON_INT: {
191840 sqlite3_int64 i = 0;
191841 const char *z;
191842 assert( pNode->eU==1 );
191843 z = pNode->u.zJContent;
191844 if( z[0]=='-' ){ z++; }
191845 while( z[0]>='0' && z[0]<='9' ){
191846 unsigned v = *(z++) - '0';
191847 if( i>=LARGEST_INT64/10 ){
191848 if( i>LARGEST_INT64/10 ) goto int_as_real;
191849 if( z[0]>='0' && z[0]<='9' ) goto int_as_real;
191850 if( v==9 ) goto int_as_real;
191851 if( v==8 ){
191852 if( pNode->u.zJContent[0]=='-' ){
191853 sqlite3_result_int64(pCtx, SMALLEST_INT64);
191854 goto int_done;
191855 }else{
191856 goto int_as_real;
191857 }
191858 }
191859 }
191860 i = i*10 + v;
191861 }
191862 if( pNode->u.zJContent[0]=='-' ){ i = -i; }
191863 sqlite3_result_int64(pCtx, iVal: i);
191864 int_done:
191865 break;
191866 int_as_real: ; /* no break */ deliberate_fall_through
191867 }
191868 case JSON_REAL: {
191869 double r;
191870#ifdef SQLITE_AMALGAMATION
191871 const char *z;
191872 assert( pNode->eU==1 );
191873 z = pNode->u.zJContent;
191874 sqlite3AtoF(z, pResult: &r, length: sqlite3Strlen30(z), SQLITE_UTF8);
191875#else
191876 assert( pNode->eU==1 );
191877 r = strtod(pNode->u.zJContent, 0);
191878#endif
191879 sqlite3_result_double(pCtx, rVal: r);
191880 break;
191881 }
191882 case JSON_STRING: {
191883#if 0 /* Never happens because JNODE_RAW is only set by json_set(),
191884 ** json_insert() and json_replace() and those routines do not
191885 ** call jsonReturn() */
191886 if( pNode->jnFlags & JNODE_RAW ){
191887 assert( pNode->eU==1 );
191888 sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n,
191889 SQLITE_TRANSIENT);
191890 }else
191891#endif
191892 assert( (pNode->jnFlags & JNODE_RAW)==0 );
191893 if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){
191894 /* JSON formatted without any backslash-escapes */
191895 assert( pNode->eU==1 );
191896 sqlite3_result_text(pCtx, z: pNode->u.zJContent+1, n: pNode->n-2,
191897 SQLITE_TRANSIENT);
191898 }else{
191899 /* Translate JSON formatted string into raw text */
191900 u32 i;
191901 u32 n = pNode->n;
191902 const char *z;
191903 char *zOut;
191904 u32 j;
191905 assert( pNode->eU==1 );
191906 z = pNode->u.zJContent;
191907 zOut = sqlite3_malloc( n: n+1 );
191908 if( zOut==0 ){
191909 sqlite3_result_error_nomem(pCtx);
191910 break;
191911 }
191912 for(i=1, j=0; i<n-1; i++){
191913 char c = z[i];
191914 if( c!='\\' ){
191915 zOut[j++] = c;
191916 }else{
191917 c = z[++i];
191918 if( c=='u' ){
191919 u32 v = jsonHexToInt4(z: z+i+1);
191920 i += 4;
191921 if( v==0 ) break;
191922 if( v<=0x7f ){
191923 zOut[j++] = (char)v;
191924 }else if( v<=0x7ff ){
191925 zOut[j++] = (char)(0xc0 | (v>>6));
191926 zOut[j++] = 0x80 | (v&0x3f);
191927 }else{
191928 u32 vlo;
191929 if( (v&0xfc00)==0xd800
191930 && i<n-6
191931 && z[i+1]=='\\'
191932 && z[i+2]=='u'
191933 && ((vlo = jsonHexToInt4(z: z+i+3))&0xfc00)==0xdc00
191934 ){
191935 /* We have a surrogate pair */
191936 v = ((v&0x3ff)<<10) + (vlo&0x3ff) + 0x10000;
191937 i += 6;
191938 zOut[j++] = 0xf0 | (v>>18);
191939 zOut[j++] = 0x80 | ((v>>12)&0x3f);
191940 zOut[j++] = 0x80 | ((v>>6)&0x3f);
191941 zOut[j++] = 0x80 | (v&0x3f);
191942 }else{
191943 zOut[j++] = 0xe0 | (v>>12);
191944 zOut[j++] = 0x80 | ((v>>6)&0x3f);
191945 zOut[j++] = 0x80 | (v&0x3f);
191946 }
191947 }
191948 }else{
191949 if( c=='b' ){
191950 c = '\b';
191951 }else if( c=='f' ){
191952 c = '\f';
191953 }else if( c=='n' ){
191954 c = '\n';
191955 }else if( c=='r' ){
191956 c = '\r';
191957 }else if( c=='t' ){
191958 c = '\t';
191959 }
191960 zOut[j++] = c;
191961 }
191962 }
191963 }
191964 zOut[j] = 0;
191965 sqlite3_result_text(pCtx, z: zOut, n: j, xDel: sqlite3_free);
191966 }
191967 break;
191968 }
191969 case JSON_ARRAY:
191970 case JSON_OBJECT: {
191971 jsonReturnJson(pNode, pCtx, aReplace);
191972 break;
191973 }
191974 }
191975}
191976
191977/* Forward reference */
191978static int jsonParseAddNode(JsonParse*,u32,u32,const char*);
191979
191980/*
191981** A macro to hint to the compiler that a function should not be
191982** inlined.
191983*/
191984#if defined(__GNUC__)
191985# define JSON_NOINLINE __attribute__((noinline))
191986#elif defined(_MSC_VER) && _MSC_VER>=1310
191987# define JSON_NOINLINE __declspec(noinline)
191988#else
191989# define JSON_NOINLINE
191990#endif
191991
191992
191993static JSON_NOINLINE int jsonParseAddNodeExpand(
191994 JsonParse *pParse, /* Append the node to this object */
191995 u32 eType, /* Node type */
191996 u32 n, /* Content size or sub-node count */
191997 const char *zContent /* Content */
191998){
191999 u32 nNew;
192000 JsonNode *pNew;
192001 assert( pParse->nNode>=pParse->nAlloc );
192002 if( pParse->oom ) return -1;
192003 nNew = pParse->nAlloc*2 + 10;
192004 pNew = sqlite3_realloc64(pOld: pParse->aNode, n: sizeof(JsonNode)*nNew);
192005 if( pNew==0 ){
192006 pParse->oom = 1;
192007 return -1;
192008 }
192009 pParse->nAlloc = nNew;
192010 pParse->aNode = pNew;
192011 assert( pParse->nNode<pParse->nAlloc );
192012 return jsonParseAddNode(pParse, eType, n, zContent);
192013}
192014
192015/*
192016** Create a new JsonNode instance based on the arguments and append that
192017** instance to the JsonParse. Return the index in pParse->aNode[] of the
192018** new node, or -1 if a memory allocation fails.
192019*/
192020static int jsonParseAddNode(
192021 JsonParse *pParse, /* Append the node to this object */
192022 u32 eType, /* Node type */
192023 u32 n, /* Content size or sub-node count */
192024 const char *zContent /* Content */
192025){
192026 JsonNode *p;
192027 if( pParse->aNode==0 || pParse->nNode>=pParse->nAlloc ){
192028 return jsonParseAddNodeExpand(pParse, eType, n, zContent);
192029 }
192030 p = &pParse->aNode[pParse->nNode];
192031 p->eType = (u8)eType;
192032 p->jnFlags = 0;
192033 VVA( p->eU = zContent ? 1 : 0 );
192034 p->n = n;
192035 p->u.zJContent = zContent;
192036 return pParse->nNode++;
192037}
192038
192039/*
192040** Return true if z[] begins with 4 (or more) hexadecimal digits
192041*/
192042static int jsonIs4Hex(const char *z){
192043 int i;
192044 for(i=0; i<4; i++) if( !safe_isxdigit(z[i]) ) return 0;
192045 return 1;
192046}
192047
192048/*
192049** Parse a single JSON value which begins at pParse->zJson[i]. Return the
192050** index of the first character past the end of the value parsed.
192051**
192052** Return negative for a syntax error. Special cases: return -2 if the
192053** first non-whitespace character is '}' and return -3 if the first
192054** non-whitespace character is ']'.
192055*/
192056static int jsonParseValue(JsonParse *pParse, u32 i){
192057 char c;
192058 u32 j;
192059 int iThis;
192060 int x;
192061 JsonNode *pNode;
192062 const char *z = pParse->zJson;
192063 while( safe_isspace(z[i]) ){ i++; }
192064 if( (c = z[i])=='{' ){
192065 /* Parse object */
192066 iThis = jsonParseAddNode(pParse, JSON_OBJECT, n: 0, zContent: 0);
192067 if( iThis<0 ) return -1;
192068 for(j=i+1;;j++){
192069 while( safe_isspace(z[j]) ){ j++; }
192070 if( ++pParse->iDepth > JSON_MAX_DEPTH ) return -1;
192071 x = jsonParseValue(pParse, i: j);
192072 if( x<0 ){
192073 pParse->iDepth--;
192074 if( x==(-2) && pParse->nNode==(u32)iThis+1 ) return j+1;
192075 return -1;
192076 }
192077 if( pParse->oom ) return -1;
192078 pNode = &pParse->aNode[pParse->nNode-1];
192079 if( pNode->eType!=JSON_STRING ) return -1;
192080 pNode->jnFlags |= JNODE_LABEL;
192081 j = x;
192082 while( safe_isspace(z[j]) ){ j++; }
192083 if( z[j]!=':' ) return -1;
192084 j++;
192085 x = jsonParseValue(pParse, i: j);
192086 pParse->iDepth--;
192087 if( x<0 ) return -1;
192088 j = x;
192089 while( safe_isspace(z[j]) ){ j++; }
192090 c = z[j];
192091 if( c==',' ) continue;
192092 if( c!='}' ) return -1;
192093 break;
192094 }
192095 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
192096 return j+1;
192097 }else if( c=='[' ){
192098 /* Parse array */
192099 iThis = jsonParseAddNode(pParse, JSON_ARRAY, n: 0, zContent: 0);
192100 if( iThis<0 ) return -1;
192101 memset(s: &pParse->aNode[iThis].u, c: 0, n: sizeof(pParse->aNode[iThis].u));
192102 for(j=i+1;;j++){
192103 while( safe_isspace(z[j]) ){ j++; }
192104 if( ++pParse->iDepth > JSON_MAX_DEPTH ) return -1;
192105 x = jsonParseValue(pParse, i: j);
192106 pParse->iDepth--;
192107 if( x<0 ){
192108 if( x==(-3) && pParse->nNode==(u32)iThis+1 ) return j+1;
192109 return -1;
192110 }
192111 j = x;
192112 while( safe_isspace(z[j]) ){ j++; }
192113 c = z[j];
192114 if( c==',' ) continue;
192115 if( c!=']' ) return -1;
192116 break;
192117 }
192118 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
192119 return j+1;
192120 }else if( c=='"' ){
192121 /* Parse string */
192122 u8 jnFlags = 0;
192123 j = i+1;
192124 for(;;){
192125 c = z[j];
192126 if( (c & ~0x1f)==0 ){
192127 /* Control characters are not allowed in strings */
192128 return -1;
192129 }
192130 if( c=='\\' ){
192131 c = z[++j];
192132 if( c=='"' || c=='\\' || c=='/' || c=='b' || c=='f'
192133 || c=='n' || c=='r' || c=='t'
192134 || (c=='u' && jsonIs4Hex(z: z+j+1)) ){
192135 jnFlags = JNODE_ESCAPE;
192136 }else{
192137 return -1;
192138 }
192139 }else if( c=='"' ){
192140 break;
192141 }
192142 j++;
192143 }
192144 jsonParseAddNode(pParse, JSON_STRING, n: j+1-i, zContent: &z[i]);
192145 if( !pParse->oom ) pParse->aNode[pParse->nNode-1].jnFlags = jnFlags;
192146 return j+1;
192147 }else if( c=='n'
192148 && strncmp(s1: z+i,s2: "null",n: 4)==0
192149 && !safe_isalnum(z[i+4]) ){
192150 jsonParseAddNode(pParse, JSON_NULL, n: 0, zContent: 0);
192151 return i+4;
192152 }else if( c=='t'
192153 && strncmp(s1: z+i,s2: "true",n: 4)==0
192154 && !safe_isalnum(z[i+4]) ){
192155 jsonParseAddNode(pParse, JSON_TRUE, n: 0, zContent: 0);
192156 return i+4;
192157 }else if( c=='f'
192158 && strncmp(s1: z+i,s2: "false",n: 5)==0
192159 && !safe_isalnum(z[i+5]) ){
192160 jsonParseAddNode(pParse, JSON_FALSE, n: 0, zContent: 0);
192161 return i+5;
192162 }else if( c=='-' || (c>='0' && c<='9') ){
192163 /* Parse number */
192164 u8 seenDP = 0;
192165 u8 seenE = 0;
192166 assert( '-' < '0' );
192167 if( c<='0' ){
192168 j = c=='-' ? i+1 : i;
192169 if( z[j]=='0' && z[j+1]>='0' && z[j+1]<='9' ) return -1;
192170 }
192171 j = i+1;
192172 for(;; j++){
192173 c = z[j];
192174 if( c>='0' && c<='9' ) continue;
192175 if( c=='.' ){
192176 if( z[j-1]=='-' ) return -1;
192177 if( seenDP ) return -1;
192178 seenDP = 1;
192179 continue;
192180 }
192181 if( c=='e' || c=='E' ){
192182 if( z[j-1]<'0' ) return -1;
192183 if( seenE ) return -1;
192184 seenDP = seenE = 1;
192185 c = z[j+1];
192186 if( c=='+' || c=='-' ){
192187 j++;
192188 c = z[j+1];
192189 }
192190 if( c<'0' || c>'9' ) return -1;
192191 continue;
192192 }
192193 break;
192194 }
192195 if( z[j-1]<'0' ) return -1;
192196 jsonParseAddNode(pParse, eType: seenDP ? JSON_REAL : JSON_INT,
192197 n: j - i, zContent: &z[i]);
192198 return j;
192199 }else if( c=='}' ){
192200 return -2; /* End of {...} */
192201 }else if( c==']' ){
192202 return -3; /* End of [...] */
192203 }else if( c==0 ){
192204 return 0; /* End of file */
192205 }else{
192206 return -1; /* Syntax error */
192207 }
192208}
192209
192210/*
192211** Parse a complete JSON string. Return 0 on success or non-zero if there
192212** are any errors. If an error occurs, free all memory associated with
192213** pParse.
192214**
192215** pParse is uninitialized when this routine is called.
192216*/
192217static int jsonParse(
192218 JsonParse *pParse, /* Initialize and fill this JsonParse object */
192219 sqlite3_context *pCtx, /* Report errors here */
192220 const char *zJson /* Input JSON text to be parsed */
192221){
192222 int i;
192223 memset(s: pParse, c: 0, n: sizeof(*pParse));
192224 if( zJson==0 ) return 1;
192225 pParse->zJson = zJson;
192226 i = jsonParseValue(pParse, i: 0);
192227 if( pParse->oom ) i = -1;
192228 if( i>0 ){
192229 assert( pParse->iDepth==0 );
192230 while( safe_isspace(zJson[i]) ) i++;
192231 if( zJson[i] ) i = -1;
192232 }
192233 if( i<=0 ){
192234 if( pCtx!=0 ){
192235 if( pParse->oom ){
192236 sqlite3_result_error_nomem(pCtx);
192237 }else{
192238 sqlite3_result_error(pCtx, z: "malformed JSON", n: -1);
192239 }
192240 }
192241 jsonParseReset(pParse);
192242 return 1;
192243 }
192244 return 0;
192245}
192246
192247/* Mark node i of pParse as being a child of iParent. Call recursively
192248** to fill in all the descendants of node i.
192249*/
192250static void jsonParseFillInParentage(JsonParse *pParse, u32 i, u32 iParent){
192251 JsonNode *pNode = &pParse->aNode[i];
192252 u32 j;
192253 pParse->aUp[i] = iParent;
192254 switch( pNode->eType ){
192255 case JSON_ARRAY: {
192256 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode: pNode+j)){
192257 jsonParseFillInParentage(pParse, i: i+j, iParent: i);
192258 }
192259 break;
192260 }
192261 case JSON_OBJECT: {
192262 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode: pNode+j+1)+1){
192263 pParse->aUp[i+j] = i;
192264 jsonParseFillInParentage(pParse, i: i+j+1, iParent: i);
192265 }
192266 break;
192267 }
192268 default: {
192269 break;
192270 }
192271 }
192272}
192273
192274/*
192275** Compute the parentage of all nodes in a completed parse.
192276*/
192277static int jsonParseFindParents(JsonParse *pParse){
192278 u32 *aUp;
192279 assert( pParse->aUp==0 );
192280 aUp = pParse->aUp = sqlite3_malloc64( n: sizeof(u32)*pParse->nNode );
192281 if( aUp==0 ){
192282 pParse->oom = 1;
192283 return SQLITE_NOMEM;
192284 }
192285 jsonParseFillInParentage(pParse, i: 0, iParent: 0);
192286 return SQLITE_OK;
192287}
192288
192289/*
192290** Magic number used for the JSON parse cache in sqlite3_get_auxdata()
192291*/
192292#define JSON_CACHE_ID (-429938) /* First cache entry */
192293#define JSON_CACHE_SZ 4 /* Max number of cache entries */
192294
192295/*
192296** Obtain a complete parse of the JSON found in the first argument
192297** of the argv array. Use the sqlite3_get_auxdata() cache for this
192298** parse if it is available. If the cache is not available or if it
192299** is no longer valid, parse the JSON again and return the new parse,
192300** and also register the new parse so that it will be available for
192301** future sqlite3_get_auxdata() calls.
192302*/
192303static JsonParse *jsonParseCached(
192304 sqlite3_context *pCtx,
192305 sqlite3_value **argv,
192306 sqlite3_context *pErrCtx
192307){
192308 const char *zJson = (const char*)sqlite3_value_text(pVal: argv[0]);
192309 int nJson = sqlite3_value_bytes(pVal: argv[0]);
192310 JsonParse *p;
192311 JsonParse *pMatch = 0;
192312 int iKey;
192313 int iMinKey = 0;
192314 u32 iMinHold = 0xffffffff;
192315 u32 iMaxHold = 0;
192316 if( zJson==0 ) return 0;
192317 for(iKey=0; iKey<JSON_CACHE_SZ; iKey++){
192318 p = (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iKey);
192319 if( p==0 ){
192320 iMinKey = iKey;
192321 break;
192322 }
192323 if( pMatch==0
192324 && p->nJson==nJson
192325 && memcmp(s1: p->zJson,s2: zJson,n: nJson)==0
192326 ){
192327 p->nErr = 0;
192328 pMatch = p;
192329 }else if( p->iHold<iMinHold ){
192330 iMinHold = p->iHold;
192331 iMinKey = iKey;
192332 }
192333 if( p->iHold>iMaxHold ){
192334 iMaxHold = p->iHold;
192335 }
192336 }
192337 if( pMatch ){
192338 pMatch->nErr = 0;
192339 pMatch->iHold = iMaxHold+1;
192340 return pMatch;
192341 }
192342 p = sqlite3_malloc64( n: sizeof(*p) + nJson + 1 );
192343 if( p==0 ){
192344 sqlite3_result_error_nomem(pCtx);
192345 return 0;
192346 }
192347 memset(s: p, c: 0, n: sizeof(*p));
192348 p->zJson = (char*)&p[1];
192349 memcpy(dest: (char*)p->zJson, src: zJson, n: nJson+1);
192350 if( jsonParse(pParse: p, pCtx: pErrCtx, zJson: p->zJson) ){
192351 sqlite3_free(p);
192352 return 0;
192353 }
192354 p->nJson = nJson;
192355 p->iHold = iMaxHold+1;
192356 sqlite3_set_auxdata(pCtx, JSON_CACHE_ID+iMinKey, pAux: p,
192357 xDelete: (void(*)(void*))jsonParseFree);
192358 return (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iMinKey);
192359}
192360
192361/*
192362** Compare the OBJECT label at pNode against zKey,nKey. Return true on
192363** a match.
192364*/
192365static int jsonLabelCompare(JsonNode *pNode, const char *zKey, u32 nKey){
192366 assert( pNode->eU==1 );
192367 if( pNode->jnFlags & JNODE_RAW ){
192368 if( pNode->n!=nKey ) return 0;
192369 return strncmp(s1: pNode->u.zJContent, s2: zKey, n: nKey)==0;
192370 }else{
192371 if( pNode->n!=nKey+2 ) return 0;
192372 return strncmp(s1: pNode->u.zJContent+1, s2: zKey, n: nKey)==0;
192373 }
192374}
192375
192376/* forward declaration */
192377static JsonNode *jsonLookupAppend(JsonParse*,const char*,int*,const char**);
192378
192379/*
192380** Search along zPath to find the node specified. Return a pointer
192381** to that node, or NULL if zPath is malformed or if there is no such
192382** node.
192383**
192384** If pApnd!=0, then try to append new nodes to complete zPath if it is
192385** possible to do so and if no existing node corresponds to zPath. If
192386** new nodes are appended *pApnd is set to 1.
192387*/
192388static JsonNode *jsonLookupStep(
192389 JsonParse *pParse, /* The JSON to search */
192390 u32 iRoot, /* Begin the search at this node */
192391 const char *zPath, /* The path to search */
192392 int *pApnd, /* Append nodes to complete path if not NULL */
192393 const char **pzErr /* Make *pzErr point to any syntax error in zPath */
192394){
192395 u32 i, j, nKey;
192396 const char *zKey;
192397 JsonNode *pRoot = &pParse->aNode[iRoot];
192398 if( zPath[0]==0 ) return pRoot;
192399 if( pRoot->jnFlags & JNODE_REPLACE ) return 0;
192400 if( zPath[0]=='.' ){
192401 if( pRoot->eType!=JSON_OBJECT ) return 0;
192402 zPath++;
192403 if( zPath[0]=='"' ){
192404 zKey = zPath + 1;
192405 for(i=1; zPath[i] && zPath[i]!='"'; i++){}
192406 nKey = i-1;
192407 if( zPath[i] ){
192408 i++;
192409 }else{
192410 *pzErr = zPath;
192411 return 0;
192412 }
192413 }else{
192414 zKey = zPath;
192415 for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
192416 nKey = i;
192417 }
192418 if( nKey==0 ){
192419 *pzErr = zPath;
192420 return 0;
192421 }
192422 j = 1;
192423 for(;;){
192424 while( j<=pRoot->n ){
192425 if( jsonLabelCompare(pNode: pRoot+j, zKey, nKey) ){
192426 return jsonLookupStep(pParse, iRoot: iRoot+j+1, zPath: &zPath[i], pApnd, pzErr);
192427 }
192428 j++;
192429 j += jsonNodeSize(pNode: &pRoot[j]);
192430 }
192431 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
192432 assert( pRoot->eU==2 );
192433 iRoot += pRoot->u.iAppend;
192434 pRoot = &pParse->aNode[iRoot];
192435 j = 1;
192436 }
192437 if( pApnd ){
192438 u32 iStart, iLabel;
192439 JsonNode *pNode;
192440 iStart = jsonParseAddNode(pParse, JSON_OBJECT, n: 2, zContent: 0);
192441 iLabel = jsonParseAddNode(pParse, JSON_STRING, n: nKey, zContent: zKey);
192442 zPath += i;
192443 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
192444 if( pParse->oom ) return 0;
192445 if( pNode ){
192446 pRoot = &pParse->aNode[iRoot];
192447 assert( pRoot->eU==0 );
192448 pRoot->u.iAppend = iStart - iRoot;
192449 pRoot->jnFlags |= JNODE_APPEND;
192450 VVA( pRoot->eU = 2 );
192451 pParse->aNode[iLabel].jnFlags |= JNODE_RAW;
192452 }
192453 return pNode;
192454 }
192455 }else if( zPath[0]=='[' ){
192456 i = 0;
192457 j = 1;
192458 while( safe_isdigit(zPath[j]) ){
192459 i = i*10 + zPath[j] - '0';
192460 j++;
192461 }
192462 if( j<2 || zPath[j]!=']' ){
192463 if( zPath[1]=='#' ){
192464 JsonNode *pBase = pRoot;
192465 int iBase = iRoot;
192466 if( pRoot->eType!=JSON_ARRAY ) return 0;
192467 for(;;){
192468 while( j<=pBase->n ){
192469 if( (pBase[j].jnFlags & JNODE_REMOVE)==0 ) i++;
192470 j += jsonNodeSize(pNode: &pBase[j]);
192471 }
192472 if( (pBase->jnFlags & JNODE_APPEND)==0 ) break;
192473 assert( pBase->eU==2 );
192474 iBase += pBase->u.iAppend;
192475 pBase = &pParse->aNode[iBase];
192476 j = 1;
192477 }
192478 j = 2;
192479 if( zPath[2]=='-' && safe_isdigit(zPath[3]) ){
192480 unsigned int x = 0;
192481 j = 3;
192482 do{
192483 x = x*10 + zPath[j] - '0';
192484 j++;
192485 }while( safe_isdigit(zPath[j]) );
192486 if( x>i ) return 0;
192487 i -= x;
192488 }
192489 if( zPath[j]!=']' ){
192490 *pzErr = zPath;
192491 return 0;
192492 }
192493 }else{
192494 *pzErr = zPath;
192495 return 0;
192496 }
192497 }
192498 if( pRoot->eType!=JSON_ARRAY ) return 0;
192499 zPath += j + 1;
192500 j = 1;
192501 for(;;){
192502 while( j<=pRoot->n && (i>0 || (pRoot[j].jnFlags & JNODE_REMOVE)!=0) ){
192503 if( (pRoot[j].jnFlags & JNODE_REMOVE)==0 ) i--;
192504 j += jsonNodeSize(pNode: &pRoot[j]);
192505 }
192506 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
192507 assert( pRoot->eU==2 );
192508 iRoot += pRoot->u.iAppend;
192509 pRoot = &pParse->aNode[iRoot];
192510 j = 1;
192511 }
192512 if( j<=pRoot->n ){
192513 return jsonLookupStep(pParse, iRoot: iRoot+j, zPath, pApnd, pzErr);
192514 }
192515 if( i==0 && pApnd ){
192516 u32 iStart;
192517 JsonNode *pNode;
192518 iStart = jsonParseAddNode(pParse, JSON_ARRAY, n: 1, zContent: 0);
192519 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
192520 if( pParse->oom ) return 0;
192521 if( pNode ){
192522 pRoot = &pParse->aNode[iRoot];
192523 assert( pRoot->eU==0 );
192524 pRoot->u.iAppend = iStart - iRoot;
192525 pRoot->jnFlags |= JNODE_APPEND;
192526 VVA( pRoot->eU = 2 );
192527 }
192528 return pNode;
192529 }
192530 }else{
192531 *pzErr = zPath;
192532 }
192533 return 0;
192534}
192535
192536/*
192537** Append content to pParse that will complete zPath. Return a pointer
192538** to the inserted node, or return NULL if the append fails.
192539*/
192540static JsonNode *jsonLookupAppend(
192541 JsonParse *pParse, /* Append content to the JSON parse */
192542 const char *zPath, /* Description of content to append */
192543 int *pApnd, /* Set this flag to 1 */
192544 const char **pzErr /* Make this point to any syntax error */
192545){
192546 *pApnd = 1;
192547 if( zPath[0]==0 ){
192548 jsonParseAddNode(pParse, JSON_NULL, n: 0, zContent: 0);
192549 return pParse->oom ? 0 : &pParse->aNode[pParse->nNode-1];
192550 }
192551 if( zPath[0]=='.' ){
192552 jsonParseAddNode(pParse, JSON_OBJECT, n: 0, zContent: 0);
192553 }else if( strncmp(s1: zPath,s2: "[0]",n: 3)==0 ){
192554 jsonParseAddNode(pParse, JSON_ARRAY, n: 0, zContent: 0);
192555 }else{
192556 return 0;
192557 }
192558 if( pParse->oom ) return 0;
192559 return jsonLookupStep(pParse, iRoot: pParse->nNode-1, zPath, pApnd, pzErr);
192560}
192561
192562/*
192563** Return the text of a syntax error message on a JSON path. Space is
192564** obtained from sqlite3_malloc().
192565*/
192566static char *jsonPathSyntaxError(const char *zErr){
192567 return sqlite3_mprintf(zFormat: "JSON path error near '%q'", zErr);
192568}
192569
192570/*
192571** Do a node lookup using zPath. Return a pointer to the node on success.
192572** Return NULL if not found or if there is an error.
192573**
192574** On an error, write an error message into pCtx and increment the
192575** pParse->nErr counter.
192576**
192577** If pApnd!=NULL then try to append missing nodes and set *pApnd = 1 if
192578** nodes are appended.
192579*/
192580static JsonNode *jsonLookup(
192581 JsonParse *pParse, /* The JSON to search */
192582 const char *zPath, /* The path to search */
192583 int *pApnd, /* Append nodes to complete path if not NULL */
192584 sqlite3_context *pCtx /* Report errors here, if not NULL */
192585){
192586 const char *zErr = 0;
192587 JsonNode *pNode = 0;
192588 char *zMsg;
192589
192590 if( zPath==0 ) return 0;
192591 if( zPath[0]!='$' ){
192592 zErr = zPath;
192593 goto lookup_err;
192594 }
192595 zPath++;
192596 pNode = jsonLookupStep(pParse, iRoot: 0, zPath, pApnd, pzErr: &zErr);
192597 if( zErr==0 ) return pNode;
192598
192599lookup_err:
192600 pParse->nErr++;
192601 assert( zErr!=0 && pCtx!=0 );
192602 zMsg = jsonPathSyntaxError(zErr);
192603 if( zMsg ){
192604 sqlite3_result_error(pCtx, z: zMsg, n: -1);
192605 sqlite3_free(p: zMsg);
192606 }else{
192607 sqlite3_result_error_nomem(pCtx);
192608 }
192609 return 0;
192610}
192611
192612
192613/*
192614** Report the wrong number of arguments for json_insert(), json_replace()
192615** or json_set().
192616*/
192617static void jsonWrongNumArgs(
192618 sqlite3_context *pCtx,
192619 const char *zFuncName
192620){
192621 char *zMsg = sqlite3_mprintf(zFormat: "json_%s() needs an odd number of arguments",
192622 zFuncName);
192623 sqlite3_result_error(pCtx, z: zMsg, n: -1);
192624 sqlite3_free(p: zMsg);
192625}
192626
192627/*
192628** Mark all NULL entries in the Object passed in as JNODE_REMOVE.
192629*/
192630static void jsonRemoveAllNulls(JsonNode *pNode){
192631 int i, n;
192632 assert( pNode->eType==JSON_OBJECT );
192633 n = pNode->n;
192634 for(i=2; i<=n; i += jsonNodeSize(pNode: &pNode[i])+1){
192635 switch( pNode[i].eType ){
192636 case JSON_NULL:
192637 pNode[i].jnFlags |= JNODE_REMOVE;
192638 break;
192639 case JSON_OBJECT:
192640 jsonRemoveAllNulls(pNode: &pNode[i]);
192641 break;
192642 }
192643 }
192644}
192645
192646
192647/****************************************************************************
192648** SQL functions used for testing and debugging
192649****************************************************************************/
192650
192651#ifdef SQLITE_DEBUG
192652/*
192653** The json_parse(JSON) function returns a string which describes
192654** a parse of the JSON provided. Or it returns NULL if JSON is not
192655** well-formed.
192656*/
192657static void jsonParseFunc(
192658 sqlite3_context *ctx,
192659 int argc,
192660 sqlite3_value **argv
192661){
192662 JsonString s; /* Output string - not real JSON */
192663 JsonParse x; /* The parse */
192664 u32 i;
192665
192666 assert( argc==1 );
192667 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
192668 jsonParseFindParents(&x);
192669 jsonInit(&s, ctx);
192670 for(i=0; i<x.nNode; i++){
192671 const char *zType;
192672 if( x.aNode[i].jnFlags & JNODE_LABEL ){
192673 assert( x.aNode[i].eType==JSON_STRING );
192674 zType = "label";
192675 }else{
192676 zType = jsonType[x.aNode[i].eType];
192677 }
192678 jsonPrintf(100, &s,"node %3u: %7s n=%-4d up=%-4d",
192679 i, zType, x.aNode[i].n, x.aUp[i]);
192680 assert( x.aNode[i].eU==0 || x.aNode[i].eU==1 );
192681 if( x.aNode[i].u.zJContent!=0 ){
192682 assert( x.aNode[i].eU==1 );
192683 jsonAppendRaw(&s, " ", 1);
192684 jsonAppendRaw(&s, x.aNode[i].u.zJContent, x.aNode[i].n);
192685 }else{
192686 assert( x.aNode[i].eU==0 );
192687 }
192688 jsonAppendRaw(&s, "\n", 1);
192689 }
192690 jsonParseReset(&x);
192691 jsonResult(&s);
192692}
192693
192694/*
192695** The json_test1(JSON) function return true (1) if the input is JSON
192696** text generated by another json function. It returns (0) if the input
192697** is not known to be JSON.
192698*/
192699static void jsonTest1Func(
192700 sqlite3_context *ctx,
192701 int argc,
192702 sqlite3_value **argv
192703){
192704 UNUSED_PARAM(argc);
192705 sqlite3_result_int(ctx, sqlite3_value_subtype(argv[0])==JSON_SUBTYPE);
192706}
192707#endif /* SQLITE_DEBUG */
192708
192709/****************************************************************************
192710** Scalar SQL function implementations
192711****************************************************************************/
192712
192713/*
192714** Implementation of the json_QUOTE(VALUE) function. Return a JSON value
192715** corresponding to the SQL value input. Mostly this means putting
192716** double-quotes around strings and returning the unquoted string "null"
192717** when given a NULL input.
192718*/
192719static void jsonQuoteFunc(
192720 sqlite3_context *ctx,
192721 int argc,
192722 sqlite3_value **argv
192723){
192724 JsonString jx;
192725 UNUSED_PARAM(argc);
192726
192727 jsonInit(p: &jx, pCtx: ctx);
192728 jsonAppendValue(p: &jx, pValue: argv[0]);
192729 jsonResult(p: &jx);
192730 sqlite3_result_subtype(pCtx: ctx, JSON_SUBTYPE);
192731}
192732
192733/*
192734** Implementation of the json_array(VALUE,...) function. Return a JSON
192735** array that contains all values given in arguments. Or if any argument
192736** is a BLOB, throw an error.
192737*/
192738static void jsonArrayFunc(
192739 sqlite3_context *ctx,
192740 int argc,
192741 sqlite3_value **argv
192742){
192743 int i;
192744 JsonString jx;
192745
192746 jsonInit(p: &jx, pCtx: ctx);
192747 jsonAppendChar(p: &jx, c: '[');
192748 for(i=0; i<argc; i++){
192749 jsonAppendSeparator(p: &jx);
192750 jsonAppendValue(p: &jx, pValue: argv[i]);
192751 }
192752 jsonAppendChar(p: &jx, c: ']');
192753 jsonResult(p: &jx);
192754 sqlite3_result_subtype(pCtx: ctx, JSON_SUBTYPE);
192755}
192756
192757
192758/*
192759** json_array_length(JSON)
192760** json_array_length(JSON, PATH)
192761**
192762** Return the number of elements in the top-level JSON array.
192763** Return 0 if the input is not a well-formed JSON array.
192764*/
192765static void jsonArrayLengthFunc(
192766 sqlite3_context *ctx,
192767 int argc,
192768 sqlite3_value **argv
192769){
192770 JsonParse *p; /* The parse */
192771 sqlite3_int64 n = 0;
192772 u32 i;
192773 JsonNode *pNode;
192774
192775 p = jsonParseCached(pCtx: ctx, argv, pErrCtx: ctx);
192776 if( p==0 ) return;
192777 assert( p->nNode );
192778 if( argc==2 ){
192779 const char *zPath = (const char*)sqlite3_value_text(pVal: argv[1]);
192780 pNode = jsonLookup(pParse: p, zPath, pApnd: 0, pCtx: ctx);
192781 }else{
192782 pNode = p->aNode;
192783 }
192784 if( pNode==0 ){
192785 return;
192786 }
192787 if( pNode->eType==JSON_ARRAY ){
192788 assert( (pNode->jnFlags & JNODE_APPEND)==0 );
192789 for(i=1; i<=pNode->n; n++){
192790 i += jsonNodeSize(pNode: &pNode[i]);
192791 }
192792 }
192793 sqlite3_result_int64(pCtx: ctx, iVal: n);
192794}
192795
192796/*
192797** json_extract(JSON, PATH, ...)
192798**
192799** Return the element described by PATH. Return NULL if there is no
192800** PATH element. If there are multiple PATHs, then return a JSON array
192801** with the result from each path. Throw an error if the JSON or any PATH
192802** is malformed.
192803*/
192804static void jsonExtractFunc(
192805 sqlite3_context *ctx,
192806 int argc,
192807 sqlite3_value **argv
192808){
192809 JsonParse *p; /* The parse */
192810 JsonNode *pNode;
192811 const char *zPath;
192812 JsonString jx;
192813 int i;
192814
192815 if( argc<2 ) return;
192816 p = jsonParseCached(pCtx: ctx, argv, pErrCtx: ctx);
192817 if( p==0 ) return;
192818 jsonInit(p: &jx, pCtx: ctx);
192819 jsonAppendChar(p: &jx, c: '[');
192820 for(i=1; i<argc; i++){
192821 zPath = (const char*)sqlite3_value_text(pVal: argv[i]);
192822 pNode = jsonLookup(pParse: p, zPath, pApnd: 0, pCtx: ctx);
192823 if( p->nErr ) break;
192824 if( argc>2 ){
192825 jsonAppendSeparator(p: &jx);
192826 if( pNode ){
192827 jsonRenderNode(pNode, pOut: &jx, aReplace: 0);
192828 }else{
192829 jsonAppendRaw(p: &jx, zIn: "null", N: 4);
192830 }
192831 }else if( pNode ){
192832 jsonReturn(pNode, pCtx: ctx, aReplace: 0);
192833 }
192834 }
192835 if( argc>2 && i==argc ){
192836 jsonAppendChar(p: &jx, c: ']');
192837 jsonResult(p: &jx);
192838 sqlite3_result_subtype(pCtx: ctx, JSON_SUBTYPE);
192839 }
192840 jsonReset(p: &jx);
192841}
192842
192843/* This is the RFC 7396 MergePatch algorithm.
192844*/
192845static JsonNode *jsonMergePatch(
192846 JsonParse *pParse, /* The JSON parser that contains the TARGET */
192847 u32 iTarget, /* Node of the TARGET in pParse */
192848 JsonNode *pPatch /* The PATCH */
192849){
192850 u32 i, j;
192851 u32 iRoot;
192852 JsonNode *pTarget;
192853 if( pPatch->eType!=JSON_OBJECT ){
192854 return pPatch;
192855 }
192856 assert( iTarget>=0 && iTarget<pParse->nNode );
192857 pTarget = &pParse->aNode[iTarget];
192858 assert( (pPatch->jnFlags & JNODE_APPEND)==0 );
192859 if( pTarget->eType!=JSON_OBJECT ){
192860 jsonRemoveAllNulls(pNode: pPatch);
192861 return pPatch;
192862 }
192863 iRoot = iTarget;
192864 for(i=1; i<pPatch->n; i += jsonNodeSize(pNode: &pPatch[i+1])+1){
192865 u32 nKey;
192866 const char *zKey;
192867 assert( pPatch[i].eType==JSON_STRING );
192868 assert( pPatch[i].jnFlags & JNODE_LABEL );
192869 assert( pPatch[i].eU==1 );
192870 nKey = pPatch[i].n;
192871 zKey = pPatch[i].u.zJContent;
192872 assert( (pPatch[i].jnFlags & JNODE_RAW)==0 );
192873 for(j=1; j<pTarget->n; j += jsonNodeSize(pNode: &pTarget[j+1])+1 ){
192874 assert( pTarget[j].eType==JSON_STRING );
192875 assert( pTarget[j].jnFlags & JNODE_LABEL );
192876 assert( (pPatch[i].jnFlags & JNODE_RAW)==0 );
192877 if( pTarget[j].n==nKey && strncmp(s1: pTarget[j].u.zJContent,s2: zKey,n: nKey)==0 ){
192878 if( pTarget[j+1].jnFlags & (JNODE_REMOVE|JNODE_PATCH) ) break;
192879 if( pPatch[i+1].eType==JSON_NULL ){
192880 pTarget[j+1].jnFlags |= JNODE_REMOVE;
192881 }else{
192882 JsonNode *pNew = jsonMergePatch(pParse, iTarget: iTarget+j+1, pPatch: &pPatch[i+1]);
192883 if( pNew==0 ) return 0;
192884 pTarget = &pParse->aNode[iTarget];
192885 if( pNew!=&pTarget[j+1] ){
192886 assert( pTarget[j+1].eU==0
192887 || pTarget[j+1].eU==1
192888 || pTarget[j+1].eU==2 );
192889 testcase( pTarget[j+1].eU==1 );
192890 testcase( pTarget[j+1].eU==2 );
192891 VVA( pTarget[j+1].eU = 5 );
192892 pTarget[j+1].u.pPatch = pNew;
192893 pTarget[j+1].jnFlags |= JNODE_PATCH;
192894 }
192895 }
192896 break;
192897 }
192898 }
192899 if( j>=pTarget->n && pPatch[i+1].eType!=JSON_NULL ){
192900 int iStart, iPatch;
192901 iStart = jsonParseAddNode(pParse, JSON_OBJECT, n: 2, zContent: 0);
192902 jsonParseAddNode(pParse, JSON_STRING, n: nKey, zContent: zKey);
192903 iPatch = jsonParseAddNode(pParse, JSON_TRUE, n: 0, zContent: 0);
192904 if( pParse->oom ) return 0;
192905 jsonRemoveAllNulls(pNode: pPatch);
192906 pTarget = &pParse->aNode[iTarget];
192907 assert( pParse->aNode[iRoot].eU==0 || pParse->aNode[iRoot].eU==2 );
192908 testcase( pParse->aNode[iRoot].eU==2 );
192909 pParse->aNode[iRoot].jnFlags |= JNODE_APPEND;
192910 VVA( pParse->aNode[iRoot].eU = 2 );
192911 pParse->aNode[iRoot].u.iAppend = iStart - iRoot;
192912 iRoot = iStart;
192913 assert( pParse->aNode[iPatch].eU==0 );
192914 VVA( pParse->aNode[iPatch].eU = 5 );
192915 pParse->aNode[iPatch].jnFlags |= JNODE_PATCH;
192916 pParse->aNode[iPatch].u.pPatch = &pPatch[i+1];
192917 }
192918 }
192919 return pTarget;
192920}
192921
192922/*
192923** Implementation of the json_mergepatch(JSON1,JSON2) function. Return a JSON
192924** object that is the result of running the RFC 7396 MergePatch() algorithm
192925** on the two arguments.
192926*/
192927static void jsonPatchFunc(
192928 sqlite3_context *ctx,
192929 int argc,
192930 sqlite3_value **argv
192931){
192932 JsonParse x; /* The JSON that is being patched */
192933 JsonParse y; /* The patch */
192934 JsonNode *pResult; /* The result of the merge */
192935
192936 UNUSED_PARAM(argc);
192937 if( jsonParse(pParse: &x, pCtx: ctx, zJson: (const char*)sqlite3_value_text(pVal: argv[0])) ) return;
192938 if( jsonParse(pParse: &y, pCtx: ctx, zJson: (const char*)sqlite3_value_text(pVal: argv[1])) ){
192939 jsonParseReset(pParse: &x);
192940 return;
192941 }
192942 pResult = jsonMergePatch(pParse: &x, iTarget: 0, pPatch: y.aNode);
192943 assert( pResult!=0 || x.oom );
192944 if( pResult ){
192945 jsonReturnJson(pNode: pResult, pCtx: ctx, aReplace: 0);
192946 }else{
192947 sqlite3_result_error_nomem(pCtx: ctx);
192948 }
192949 jsonParseReset(pParse: &x);
192950 jsonParseReset(pParse: &y);
192951}
192952
192953
192954/*
192955** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
192956** object that contains all name/value given in arguments. Or if any name
192957** is not a string or if any value is a BLOB, throw an error.
192958*/
192959static void jsonObjectFunc(
192960 sqlite3_context *ctx,
192961 int argc,
192962 sqlite3_value **argv
192963){
192964 int i;
192965 JsonString jx;
192966 const char *z;
192967 u32 n;
192968
192969 if( argc&1 ){
192970 sqlite3_result_error(pCtx: ctx, z: "json_object() requires an even number "
192971 "of arguments", n: -1);
192972 return;
192973 }
192974 jsonInit(p: &jx, pCtx: ctx);
192975 jsonAppendChar(p: &jx, c: '{');
192976 for(i=0; i<argc; i+=2){
192977 if( sqlite3_value_type(pVal: argv[i])!=SQLITE_TEXT ){
192978 sqlite3_result_error(pCtx: ctx, z: "json_object() labels must be TEXT", n: -1);
192979 jsonReset(p: &jx);
192980 return;
192981 }
192982 jsonAppendSeparator(p: &jx);
192983 z = (const char*)sqlite3_value_text(pVal: argv[i]);
192984 n = (u32)sqlite3_value_bytes(pVal: argv[i]);
192985 jsonAppendString(p: &jx, zIn: z, N: n);
192986 jsonAppendChar(p: &jx, c: ':');
192987 jsonAppendValue(p: &jx, pValue: argv[i+1]);
192988 }
192989 jsonAppendChar(p: &jx, c: '}');
192990 jsonResult(p: &jx);
192991 sqlite3_result_subtype(pCtx: ctx, JSON_SUBTYPE);
192992}
192993
192994
192995/*
192996** json_remove(JSON, PATH, ...)
192997**
192998** Remove the named elements from JSON and return the result. malformed
192999** JSON or PATH arguments result in an error.
193000*/
193001static void jsonRemoveFunc(
193002 sqlite3_context *ctx,
193003 int argc,
193004 sqlite3_value **argv
193005){
193006 JsonParse x; /* The parse */
193007 JsonNode *pNode;
193008 const char *zPath;
193009 u32 i;
193010
193011 if( argc<1 ) return;
193012 if( jsonParse(pParse: &x, pCtx: ctx, zJson: (const char*)sqlite3_value_text(pVal: argv[0])) ) return;
193013 assert( x.nNode );
193014 for(i=1; i<(u32)argc; i++){
193015 zPath = (const char*)sqlite3_value_text(pVal: argv[i]);
193016 if( zPath==0 ) goto remove_done;
193017 pNode = jsonLookup(pParse: &x, zPath, pApnd: 0, pCtx: ctx);
193018 if( x.nErr ) goto remove_done;
193019 if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
193020 }
193021 if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
193022 jsonReturnJson(pNode: x.aNode, pCtx: ctx, aReplace: 0);
193023 }
193024remove_done:
193025 jsonParseReset(pParse: &x);
193026}
193027
193028/*
193029** json_replace(JSON, PATH, VALUE, ...)
193030**
193031** Replace the value at PATH with VALUE. If PATH does not already exist,
193032** this routine is a no-op. If JSON or PATH is malformed, throw an error.
193033*/
193034static void jsonReplaceFunc(
193035 sqlite3_context *ctx,
193036 int argc,
193037 sqlite3_value **argv
193038){
193039 JsonParse x; /* The parse */
193040 JsonNode *pNode;
193041 const char *zPath;
193042 u32 i;
193043
193044 if( argc<1 ) return;
193045 if( (argc&1)==0 ) {
193046 jsonWrongNumArgs(pCtx: ctx, zFuncName: "replace");
193047 return;
193048 }
193049 if( jsonParse(pParse: &x, pCtx: ctx, zJson: (const char*)sqlite3_value_text(pVal: argv[0])) ) return;
193050 assert( x.nNode );
193051 for(i=1; i<(u32)argc; i+=2){
193052 zPath = (const char*)sqlite3_value_text(pVal: argv[i]);
193053 pNode = jsonLookup(pParse: &x, zPath, pApnd: 0, pCtx: ctx);
193054 if( x.nErr ) goto replace_err;
193055 if( pNode ){
193056 assert( pNode->eU==0 || pNode->eU==1 || pNode->eU==4 );
193057 json_testcase( pNode->eU!=0 && pNode->eU!=1 );
193058 pNode->jnFlags |= (u8)JNODE_REPLACE;
193059 VVA( pNode->eU = 4 );
193060 pNode->u.iReplace = i + 1;
193061 }
193062 }
193063 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
193064 assert( x.aNode[0].eU==4 );
193065 sqlite3_result_value(pCtx: ctx, pValue: argv[x.aNode[0].u.iReplace]);
193066 }else{
193067 jsonReturnJson(pNode: x.aNode, pCtx: ctx, aReplace: argv);
193068 }
193069replace_err:
193070 jsonParseReset(pParse: &x);
193071}
193072
193073/*
193074** json_set(JSON, PATH, VALUE, ...)
193075**
193076** Set the value at PATH to VALUE. Create the PATH if it does not already
193077** exist. Overwrite existing values that do exist.
193078** If JSON or PATH is malformed, throw an error.
193079**
193080** json_insert(JSON, PATH, VALUE, ...)
193081**
193082** Create PATH and initialize it to VALUE. If PATH already exists, this
193083** routine is a no-op. If JSON or PATH is malformed, throw an error.
193084*/
193085static void jsonSetFunc(
193086 sqlite3_context *ctx,
193087 int argc,
193088 sqlite3_value **argv
193089){
193090 JsonParse x; /* The parse */
193091 JsonNode *pNode;
193092 const char *zPath;
193093 u32 i;
193094 int bApnd;
193095 int bIsSet = *(int*)sqlite3_user_data(p: ctx);
193096
193097 if( argc<1 ) return;
193098 if( (argc&1)==0 ) {
193099 jsonWrongNumArgs(pCtx: ctx, zFuncName: bIsSet ? "set" : "insert");
193100 return;
193101 }
193102 if( jsonParse(pParse: &x, pCtx: ctx, zJson: (const char*)sqlite3_value_text(pVal: argv[0])) ) return;
193103 assert( x.nNode );
193104 for(i=1; i<(u32)argc; i+=2){
193105 zPath = (const char*)sqlite3_value_text(pVal: argv[i]);
193106 bApnd = 0;
193107 pNode = jsonLookup(pParse: &x, zPath, pApnd: &bApnd, pCtx: ctx);
193108 if( x.oom ){
193109 sqlite3_result_error_nomem(pCtx: ctx);
193110 goto jsonSetDone;
193111 }else if( x.nErr ){
193112 goto jsonSetDone;
193113 }else if( pNode && (bApnd || bIsSet) ){
193114 json_testcase( pNode->eU!=0 && pNode->eU!=1 && pNode->eU!=4 );
193115 assert( pNode->eU!=3 || pNode->eU!=5 );
193116 VVA( pNode->eU = 4 );
193117 pNode->jnFlags |= (u8)JNODE_REPLACE;
193118 pNode->u.iReplace = i + 1;
193119 }
193120 }
193121 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
193122 assert( x.aNode[0].eU==4 );
193123 sqlite3_result_value(pCtx: ctx, pValue: argv[x.aNode[0].u.iReplace]);
193124 }else{
193125 jsonReturnJson(pNode: x.aNode, pCtx: ctx, aReplace: argv);
193126 }
193127jsonSetDone:
193128 jsonParseReset(pParse: &x);
193129}
193130
193131/*
193132** json_type(JSON)
193133** json_type(JSON, PATH)
193134**
193135** Return the top-level "type" of a JSON string. Throw an error if
193136** either the JSON or PATH inputs are not well-formed.
193137*/
193138static void jsonTypeFunc(
193139 sqlite3_context *ctx,
193140 int argc,
193141 sqlite3_value **argv
193142){
193143 JsonParse *p; /* The parse */
193144 const char *zPath;
193145 JsonNode *pNode;
193146
193147 p = jsonParseCached(pCtx: ctx, argv, pErrCtx: ctx);
193148 if( p==0 ) return;
193149 if( argc==2 ){
193150 zPath = (const char*)sqlite3_value_text(pVal: argv[1]);
193151 pNode = jsonLookup(pParse: p, zPath, pApnd: 0, pCtx: ctx);
193152 }else{
193153 pNode = p->aNode;
193154 }
193155 if( pNode ){
193156 sqlite3_result_text(pCtx: ctx, z: jsonType[pNode->eType], n: -1, SQLITE_STATIC);
193157 }
193158}
193159
193160/*
193161** json_valid(JSON)
193162**
193163** Return 1 if JSON is a well-formed JSON string according to RFC-7159.
193164** Return 0 otherwise.
193165*/
193166static void jsonValidFunc(
193167 sqlite3_context *ctx,
193168 int argc,
193169 sqlite3_value **argv
193170){
193171 JsonParse *p; /* The parse */
193172 UNUSED_PARAM(argc);
193173 p = jsonParseCached(pCtx: ctx, argv, pErrCtx: 0);
193174 sqlite3_result_int(pCtx: ctx, iVal: p!=0);
193175}
193176
193177
193178/****************************************************************************
193179** Aggregate SQL function implementations
193180****************************************************************************/
193181/*
193182** json_group_array(VALUE)
193183**
193184** Return a JSON array composed of all values in the aggregate.
193185*/
193186static void jsonArrayStep(
193187 sqlite3_context *ctx,
193188 int argc,
193189 sqlite3_value **argv
193190){
193191 JsonString *pStr;
193192 UNUSED_PARAM(argc);
193193 pStr = (JsonString*)sqlite3_aggregate_context(p: ctx, nByte: sizeof(*pStr));
193194 if( pStr ){
193195 if( pStr->zBuf==0 ){
193196 jsonInit(p: pStr, pCtx: ctx);
193197 jsonAppendChar(p: pStr, c: '[');
193198 }else if( pStr->nUsed>1 ){
193199 jsonAppendChar(p: pStr, c: ',');
193200 }
193201 pStr->pCtx = ctx;
193202 jsonAppendValue(p: pStr, pValue: argv[0]);
193203 }
193204}
193205static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){
193206 JsonString *pStr;
193207 pStr = (JsonString*)sqlite3_aggregate_context(p: ctx, nByte: 0);
193208 if( pStr ){
193209 pStr->pCtx = ctx;
193210 jsonAppendChar(p: pStr, c: ']');
193211 if( pStr->bErr ){
193212 if( pStr->bErr==1 ) sqlite3_result_error_nomem(pCtx: ctx);
193213 assert( pStr->bStatic );
193214 }else if( isFinal ){
193215 sqlite3_result_text(pCtx: ctx, z: pStr->zBuf, n: (int)pStr->nUsed,
193216 xDel: pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
193217 pStr->bStatic = 1;
193218 }else{
193219 sqlite3_result_text(pCtx: ctx, z: pStr->zBuf, n: (int)pStr->nUsed, SQLITE_TRANSIENT);
193220 pStr->nUsed--;
193221 }
193222 }else{
193223 sqlite3_result_text(pCtx: ctx, z: "[]", n: 2, SQLITE_STATIC);
193224 }
193225 sqlite3_result_subtype(pCtx: ctx, JSON_SUBTYPE);
193226}
193227static void jsonArrayValue(sqlite3_context *ctx){
193228 jsonArrayCompute(ctx, isFinal: 0);
193229}
193230static void jsonArrayFinal(sqlite3_context *ctx){
193231 jsonArrayCompute(ctx, isFinal: 1);
193232}
193233
193234#ifndef SQLITE_OMIT_WINDOWFUNC
193235/*
193236** This method works for both json_group_array() and json_group_object().
193237** It works by removing the first element of the group by searching forward
193238** to the first comma (",") that is not within a string and deleting all
193239** text through that comma.
193240*/
193241static void jsonGroupInverse(
193242 sqlite3_context *ctx,
193243 int argc,
193244 sqlite3_value **argv
193245){
193246 unsigned int i;
193247 int inStr = 0;
193248 int nNest = 0;
193249 char *z;
193250 char c;
193251 JsonString *pStr;
193252 UNUSED_PARAM(argc);
193253 UNUSED_PARAM(argv);
193254 pStr = (JsonString*)sqlite3_aggregate_context(p: ctx, nByte: 0);
193255#ifdef NEVER
193256 /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will
193257 ** always have been called to initalize it */
193258 if( NEVER(!pStr) ) return;
193259#endif
193260 z = pStr->zBuf;
193261 for(i=1; i<pStr->nUsed && ((c = z[i])!=',' || inStr || nNest); i++){
193262 if( c=='"' ){
193263 inStr = !inStr;
193264 }else if( c=='\\' ){
193265 i++;
193266 }else if( !inStr ){
193267 if( c=='{' || c=='[' ) nNest++;
193268 if( c=='}' || c==']' ) nNest--;
193269 }
193270 }
193271 if( i<pStr->nUsed ){
193272 pStr->nUsed -= i;
193273 memmove(dest: &z[1], src: &z[i+1], n: (size_t)pStr->nUsed-1);
193274 z[pStr->nUsed] = 0;
193275 }else{
193276 pStr->nUsed = 1;
193277 }
193278}
193279#else
193280# define jsonGroupInverse 0
193281#endif
193282
193283
193284/*
193285** json_group_obj(NAME,VALUE)
193286**
193287** Return a JSON object composed of all names and values in the aggregate.
193288*/
193289static void jsonObjectStep(
193290 sqlite3_context *ctx,
193291 int argc,
193292 sqlite3_value **argv
193293){
193294 JsonString *pStr;
193295 const char *z;
193296 u32 n;
193297 UNUSED_PARAM(argc);
193298 pStr = (JsonString*)sqlite3_aggregate_context(p: ctx, nByte: sizeof(*pStr));
193299 if( pStr ){
193300 if( pStr->zBuf==0 ){
193301 jsonInit(p: pStr, pCtx: ctx);
193302 jsonAppendChar(p: pStr, c: '{');
193303 }else if( pStr->nUsed>1 ){
193304 jsonAppendChar(p: pStr, c: ',');
193305 }
193306 pStr->pCtx = ctx;
193307 z = (const char*)sqlite3_value_text(pVal: argv[0]);
193308 n = (u32)sqlite3_value_bytes(pVal: argv[0]);
193309 jsonAppendString(p: pStr, zIn: z, N: n);
193310 jsonAppendChar(p: pStr, c: ':');
193311 jsonAppendValue(p: pStr, pValue: argv[1]);
193312 }
193313}
193314static void jsonObjectCompute(sqlite3_context *ctx, int isFinal){
193315 JsonString *pStr;
193316 pStr = (JsonString*)sqlite3_aggregate_context(p: ctx, nByte: 0);
193317 if( pStr ){
193318 jsonAppendChar(p: pStr, c: '}');
193319 if( pStr->bErr ){
193320 if( pStr->bErr==1 ) sqlite3_result_error_nomem(pCtx: ctx);
193321 assert( pStr->bStatic );
193322 }else if( isFinal ){
193323 sqlite3_result_text(pCtx: ctx, z: pStr->zBuf, n: (int)pStr->nUsed,
193324 xDel: pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
193325 pStr->bStatic = 1;
193326 }else{
193327 sqlite3_result_text(pCtx: ctx, z: pStr->zBuf, n: (int)pStr->nUsed, SQLITE_TRANSIENT);
193328 pStr->nUsed--;
193329 }
193330 }else{
193331 sqlite3_result_text(pCtx: ctx, z: "{}", n: 2, SQLITE_STATIC);
193332 }
193333 sqlite3_result_subtype(pCtx: ctx, JSON_SUBTYPE);
193334}
193335static void jsonObjectValue(sqlite3_context *ctx){
193336 jsonObjectCompute(ctx, isFinal: 0);
193337}
193338static void jsonObjectFinal(sqlite3_context *ctx){
193339 jsonObjectCompute(ctx, isFinal: 1);
193340}
193341
193342
193343
193344#ifndef SQLITE_OMIT_VIRTUALTABLE
193345/****************************************************************************
193346** The json_each virtual table
193347****************************************************************************/
193348typedef struct JsonEachCursor JsonEachCursor;
193349struct JsonEachCursor {
193350 sqlite3_vtab_cursor base; /* Base class - must be first */
193351 u32 iRowid; /* The rowid */
193352 u32 iBegin; /* The first node of the scan */
193353 u32 i; /* Index in sParse.aNode[] of current row */
193354 u32 iEnd; /* EOF when i equals or exceeds this value */
193355 u8 eType; /* Type of top-level element */
193356 u8 bRecursive; /* True for json_tree(). False for json_each() */
193357 char *zJson; /* Input JSON */
193358 char *zRoot; /* Path by which to filter zJson */
193359 JsonParse sParse; /* Parse of the input JSON */
193360};
193361
193362/* Constructor for the json_each virtual table */
193363static int jsonEachConnect(
193364 sqlite3 *db,
193365 void *pAux,
193366 int argc, const char *const*argv,
193367 sqlite3_vtab **ppVtab,
193368 char **pzErr
193369){
193370 sqlite3_vtab *pNew;
193371 int rc;
193372
193373/* Column numbers */
193374#define JEACH_KEY 0
193375#define JEACH_VALUE 1
193376#define JEACH_TYPE 2
193377#define JEACH_ATOM 3
193378#define JEACH_ID 4
193379#define JEACH_PARENT 5
193380#define JEACH_FULLKEY 6
193381#define JEACH_PATH 7
193382/* The xBestIndex method assumes that the JSON and ROOT columns are
193383** the last two columns in the table. Should this ever changes, be
193384** sure to update the xBestIndex method. */
193385#define JEACH_JSON 8
193386#define JEACH_ROOT 9
193387
193388 UNUSED_PARAM(pzErr);
193389 UNUSED_PARAM(argv);
193390 UNUSED_PARAM(argc);
193391 UNUSED_PARAM(pAux);
193392 rc = sqlite3_declare_vtab(db,
193393 zCreateTable: "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path,"
193394 "json HIDDEN,root HIDDEN)");
193395 if( rc==SQLITE_OK ){
193396 pNew = *ppVtab = sqlite3_malloc( n: sizeof(*pNew) );
193397 if( pNew==0 ) return SQLITE_NOMEM;
193398 memset(s: pNew, c: 0, n: sizeof(*pNew));
193399 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
193400 }
193401 return rc;
193402}
193403
193404/* destructor for json_each virtual table */
193405static int jsonEachDisconnect(sqlite3_vtab *pVtab){
193406 sqlite3_free(p: pVtab);
193407 return SQLITE_OK;
193408}
193409
193410/* constructor for a JsonEachCursor object for json_each(). */
193411static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
193412 JsonEachCursor *pCur;
193413
193414 UNUSED_PARAM(p);
193415 pCur = sqlite3_malloc( n: sizeof(*pCur) );
193416 if( pCur==0 ) return SQLITE_NOMEM;
193417 memset(s: pCur, c: 0, n: sizeof(*pCur));
193418 *ppCursor = &pCur->base;
193419 return SQLITE_OK;
193420}
193421
193422/* constructor for a JsonEachCursor object for json_tree(). */
193423static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
193424 int rc = jsonEachOpenEach(p, ppCursor);
193425 if( rc==SQLITE_OK ){
193426 JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor;
193427 pCur->bRecursive = 1;
193428 }
193429 return rc;
193430}
193431
193432/* Reset a JsonEachCursor back to its original state. Free any memory
193433** held. */
193434static void jsonEachCursorReset(JsonEachCursor *p){
193435 sqlite3_free(p: p->zJson);
193436 sqlite3_free(p: p->zRoot);
193437 jsonParseReset(pParse: &p->sParse);
193438 p->iRowid = 0;
193439 p->i = 0;
193440 p->iEnd = 0;
193441 p->eType = 0;
193442 p->zJson = 0;
193443 p->zRoot = 0;
193444}
193445
193446/* Destructor for a jsonEachCursor object */
193447static int jsonEachClose(sqlite3_vtab_cursor *cur){
193448 JsonEachCursor *p = (JsonEachCursor*)cur;
193449 jsonEachCursorReset(p);
193450 sqlite3_free(p: cur);
193451 return SQLITE_OK;
193452}
193453
193454/* Return TRUE if the jsonEachCursor object has been advanced off the end
193455** of the JSON object */
193456static int jsonEachEof(sqlite3_vtab_cursor *cur){
193457 JsonEachCursor *p = (JsonEachCursor*)cur;
193458 return p->i >= p->iEnd;
193459}
193460
193461/* Advance the cursor to the next element for json_tree() */
193462static int jsonEachNext(sqlite3_vtab_cursor *cur){
193463 JsonEachCursor *p = (JsonEachCursor*)cur;
193464 if( p->bRecursive ){
193465 if( p->sParse.aNode[p->i].jnFlags & JNODE_LABEL ) p->i++;
193466 p->i++;
193467 p->iRowid++;
193468 if( p->i<p->iEnd ){
193469 u32 iUp = p->sParse.aUp[p->i];
193470 JsonNode *pUp = &p->sParse.aNode[iUp];
193471 p->eType = pUp->eType;
193472 if( pUp->eType==JSON_ARRAY ){
193473 assert( pUp->eU==0 || pUp->eU==3 );
193474 json_testcase( pUp->eU==3 );
193475 VVA( pUp->eU = 3 );
193476 if( iUp==p->i-1 ){
193477 pUp->u.iKey = 0;
193478 }else{
193479 pUp->u.iKey++;
193480 }
193481 }
193482 }
193483 }else{
193484 switch( p->eType ){
193485 case JSON_ARRAY: {
193486 p->i += jsonNodeSize(pNode: &p->sParse.aNode[p->i]);
193487 p->iRowid++;
193488 break;
193489 }
193490 case JSON_OBJECT: {
193491 p->i += 1 + jsonNodeSize(pNode: &p->sParse.aNode[p->i+1]);
193492 p->iRowid++;
193493 break;
193494 }
193495 default: {
193496 p->i = p->iEnd;
193497 break;
193498 }
193499 }
193500 }
193501 return SQLITE_OK;
193502}
193503
193504/* Append the name of the path for element i to pStr
193505*/
193506static void jsonEachComputePath(
193507 JsonEachCursor *p, /* The cursor */
193508 JsonString *pStr, /* Write the path here */
193509 u32 i /* Path to this element */
193510){
193511 JsonNode *pNode, *pUp;
193512 u32 iUp;
193513 if( i==0 ){
193514 jsonAppendChar(p: pStr, c: '$');
193515 return;
193516 }
193517 iUp = p->sParse.aUp[i];
193518 jsonEachComputePath(p, pStr, i: iUp);
193519 pNode = &p->sParse.aNode[i];
193520 pUp = &p->sParse.aNode[iUp];
193521 if( pUp->eType==JSON_ARRAY ){
193522 assert( pUp->eU==3 || (pUp->eU==0 && pUp->u.iKey==0) );
193523 testcase( pUp->eU==0 );
193524 jsonPrintf(N: 30, p: pStr, zFormat: "[%d]", pUp->u.iKey);
193525 }else{
193526 assert( pUp->eType==JSON_OBJECT );
193527 if( (pNode->jnFlags & JNODE_LABEL)==0 ) pNode--;
193528 assert( pNode->eType==JSON_STRING );
193529 assert( pNode->jnFlags & JNODE_LABEL );
193530 assert( pNode->eU==1 );
193531 jsonPrintf(N: pNode->n+1, p: pStr, zFormat: ".%.*s", pNode->n-2, pNode->u.zJContent+1);
193532 }
193533}
193534
193535/* Return the value of a column */
193536static int jsonEachColumn(
193537 sqlite3_vtab_cursor *cur, /* The cursor */
193538 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
193539 int i /* Which column to return */
193540){
193541 JsonEachCursor *p = (JsonEachCursor*)cur;
193542 JsonNode *pThis = &p->sParse.aNode[p->i];
193543 switch( i ){
193544 case JEACH_KEY: {
193545 if( p->i==0 ) break;
193546 if( p->eType==JSON_OBJECT ){
193547 jsonReturn(pNode: pThis, pCtx: ctx, aReplace: 0);
193548 }else if( p->eType==JSON_ARRAY ){
193549 u32 iKey;
193550 if( p->bRecursive ){
193551 if( p->iRowid==0 ) break;
193552 assert( p->sParse.aNode[p->sParse.aUp[p->i]].eU==3 );
193553 iKey = p->sParse.aNode[p->sParse.aUp[p->i]].u.iKey;
193554 }else{
193555 iKey = p->iRowid;
193556 }
193557 sqlite3_result_int64(pCtx: ctx, iVal: (sqlite3_int64)iKey);
193558 }
193559 break;
193560 }
193561 case JEACH_VALUE: {
193562 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
193563 jsonReturn(pNode: pThis, pCtx: ctx, aReplace: 0);
193564 break;
193565 }
193566 case JEACH_TYPE: {
193567 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
193568 sqlite3_result_text(pCtx: ctx, z: jsonType[pThis->eType], n: -1, SQLITE_STATIC);
193569 break;
193570 }
193571 case JEACH_ATOM: {
193572 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
193573 if( pThis->eType>=JSON_ARRAY ) break;
193574 jsonReturn(pNode: pThis, pCtx: ctx, aReplace: 0);
193575 break;
193576 }
193577 case JEACH_ID: {
193578 sqlite3_result_int64(pCtx: ctx,
193579 iVal: (sqlite3_int64)p->i + ((pThis->jnFlags & JNODE_LABEL)!=0));
193580 break;
193581 }
193582 case JEACH_PARENT: {
193583 if( p->i>p->iBegin && p->bRecursive ){
193584 sqlite3_result_int64(pCtx: ctx, iVal: (sqlite3_int64)p->sParse.aUp[p->i]);
193585 }
193586 break;
193587 }
193588 case JEACH_FULLKEY: {
193589 JsonString x;
193590 jsonInit(p: &x, pCtx: ctx);
193591 if( p->bRecursive ){
193592 jsonEachComputePath(p, pStr: &x, i: p->i);
193593 }else{
193594 if( p->zRoot ){
193595 jsonAppendRaw(p: &x, zIn: p->zRoot, N: (int)strlen(s: p->zRoot));
193596 }else{
193597 jsonAppendChar(p: &x, c: '$');
193598 }
193599 if( p->eType==JSON_ARRAY ){
193600 jsonPrintf(N: 30, p: &x, zFormat: "[%d]", p->iRowid);
193601 }else if( p->eType==JSON_OBJECT ){
193602 assert( pThis->eU==1 );
193603 jsonPrintf(N: pThis->n, p: &x, zFormat: ".%.*s", pThis->n-2, pThis->u.zJContent+1);
193604 }
193605 }
193606 jsonResult(p: &x);
193607 break;
193608 }
193609 case JEACH_PATH: {
193610 if( p->bRecursive ){
193611 JsonString x;
193612 jsonInit(p: &x, pCtx: ctx);
193613 jsonEachComputePath(p, pStr: &x, i: p->sParse.aUp[p->i]);
193614 jsonResult(p: &x);
193615 break;
193616 }
193617 /* For json_each() path and root are the same so fall through
193618 ** into the root case */
193619 /* no break */ deliberate_fall_through
193620 }
193621 default: {
193622 const char *zRoot = p->zRoot;
193623 if( zRoot==0 ) zRoot = "$";
193624 sqlite3_result_text(pCtx: ctx, z: zRoot, n: -1, SQLITE_STATIC);
193625 break;
193626 }
193627 case JEACH_JSON: {
193628 assert( i==JEACH_JSON );
193629 sqlite3_result_text(pCtx: ctx, z: p->sParse.zJson, n: -1, SQLITE_STATIC);
193630 break;
193631 }
193632 }
193633 return SQLITE_OK;
193634}
193635
193636/* Return the current rowid value */
193637static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
193638 JsonEachCursor *p = (JsonEachCursor*)cur;
193639 *pRowid = p->iRowid;
193640 return SQLITE_OK;
193641}
193642
193643/* The query strategy is to look for an equality constraint on the json
193644** column. Without such a constraint, the table cannot operate. idxNum is
193645** 1 if the constraint is found, 3 if the constraint and zRoot are found,
193646** and 0 otherwise.
193647*/
193648static int jsonEachBestIndex(
193649 sqlite3_vtab *tab,
193650 sqlite3_index_info *pIdxInfo
193651){
193652 int i; /* Loop counter or computed array index */
193653 int aIdx[2]; /* Index of constraints for JSON and ROOT */
193654 int unusableMask = 0; /* Mask of unusable JSON and ROOT constraints */
193655 int idxMask = 0; /* Mask of usable == constraints JSON and ROOT */
193656 const struct sqlite3_index_constraint *pConstraint;
193657
193658 /* This implementation assumes that JSON and ROOT are the last two
193659 ** columns in the table */
193660 assert( JEACH_ROOT == JEACH_JSON+1 );
193661 UNUSED_PARAM(tab);
193662 aIdx[0] = aIdx[1] = -1;
193663 pConstraint = pIdxInfo->aConstraint;
193664 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
193665 int iCol;
193666 int iMask;
193667 if( pConstraint->iColumn < JEACH_JSON ) continue;
193668 iCol = pConstraint->iColumn - JEACH_JSON;
193669 assert( iCol==0 || iCol==1 );
193670 testcase( iCol==0 );
193671 iMask = 1 << iCol;
193672 if( pConstraint->usable==0 ){
193673 unusableMask |= iMask;
193674 }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
193675 aIdx[iCol] = i;
193676 idxMask |= iMask;
193677 }
193678 }
193679 if( (unusableMask & ~idxMask)!=0 ){
193680 /* If there are any unusable constraints on JSON or ROOT, then reject
193681 ** this entire plan */
193682 return SQLITE_CONSTRAINT;
193683 }
193684 if( aIdx[0]<0 ){
193685 /* No JSON input. Leave estimatedCost at the huge value that it was
193686 ** initialized to to discourage the query planner from selecting this
193687 ** plan. */
193688 pIdxInfo->idxNum = 0;
193689 }else{
193690 pIdxInfo->estimatedCost = 1.0;
193691 i = aIdx[0];
193692 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
193693 pIdxInfo->aConstraintUsage[i].omit = 1;
193694 if( aIdx[1]<0 ){
193695 pIdxInfo->idxNum = 1; /* Only JSON supplied. Plan 1 */
193696 }else{
193697 i = aIdx[1];
193698 pIdxInfo->aConstraintUsage[i].argvIndex = 2;
193699 pIdxInfo->aConstraintUsage[i].omit = 1;
193700 pIdxInfo->idxNum = 3; /* Both JSON and ROOT are supplied. Plan 3 */
193701 }
193702 }
193703 return SQLITE_OK;
193704}
193705
193706/* Start a search on a new JSON string */
193707static int jsonEachFilter(
193708 sqlite3_vtab_cursor *cur,
193709 int idxNum, const char *idxStr,
193710 int argc, sqlite3_value **argv
193711){
193712 JsonEachCursor *p = (JsonEachCursor*)cur;
193713 const char *z;
193714 const char *zRoot = 0;
193715 sqlite3_int64 n;
193716
193717 UNUSED_PARAM(idxStr);
193718 UNUSED_PARAM(argc);
193719 jsonEachCursorReset(p);
193720 if( idxNum==0 ) return SQLITE_OK;
193721 z = (const char*)sqlite3_value_text(pVal: argv[0]);
193722 if( z==0 ) return SQLITE_OK;
193723 n = sqlite3_value_bytes(pVal: argv[0]);
193724 p->zJson = sqlite3_malloc64( n: n+1 );
193725 if( p->zJson==0 ) return SQLITE_NOMEM;
193726 memcpy(dest: p->zJson, src: z, n: (size_t)n+1);
193727 if( jsonParse(pParse: &p->sParse, pCtx: 0, zJson: p->zJson) ){
193728 int rc = SQLITE_NOMEM;
193729 if( p->sParse.oom==0 ){
193730 sqlite3_free(p: cur->pVtab->zErrMsg);
193731 cur->pVtab->zErrMsg = sqlite3_mprintf(zFormat: "malformed JSON");
193732 if( cur->pVtab->zErrMsg ) rc = SQLITE_ERROR;
193733 }
193734 jsonEachCursorReset(p);
193735 return rc;
193736 }else if( p->bRecursive && jsonParseFindParents(pParse: &p->sParse) ){
193737 jsonEachCursorReset(p);
193738 return SQLITE_NOMEM;
193739 }else{
193740 JsonNode *pNode = 0;
193741 if( idxNum==3 ){
193742 const char *zErr = 0;
193743 zRoot = (const char*)sqlite3_value_text(pVal: argv[1]);
193744 if( zRoot==0 ) return SQLITE_OK;
193745 n = sqlite3_value_bytes(pVal: argv[1]);
193746 p->zRoot = sqlite3_malloc64( n: n+1 );
193747 if( p->zRoot==0 ) return SQLITE_NOMEM;
193748 memcpy(dest: p->zRoot, src: zRoot, n: (size_t)n+1);
193749 if( zRoot[0]!='$' ){
193750 zErr = zRoot;
193751 }else{
193752 pNode = jsonLookupStep(pParse: &p->sParse, iRoot: 0, zPath: p->zRoot+1, pApnd: 0, pzErr: &zErr);
193753 }
193754 if( zErr ){
193755 sqlite3_free(p: cur->pVtab->zErrMsg);
193756 cur->pVtab->zErrMsg = jsonPathSyntaxError(zErr);
193757 jsonEachCursorReset(p);
193758 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
193759 }else if( pNode==0 ){
193760 return SQLITE_OK;
193761 }
193762 }else{
193763 pNode = p->sParse.aNode;
193764 }
193765 p->iBegin = p->i = (int)(pNode - p->sParse.aNode);
193766 p->eType = pNode->eType;
193767 if( p->eType>=JSON_ARRAY ){
193768 assert( pNode->eU==0 );
193769 VVA( pNode->eU = 3 );
193770 pNode->u.iKey = 0;
193771 p->iEnd = p->i + pNode->n + 1;
193772 if( p->bRecursive ){
193773 p->eType = p->sParse.aNode[p->sParse.aUp[p->i]].eType;
193774 if( p->i>0 && (p->sParse.aNode[p->i-1].jnFlags & JNODE_LABEL)!=0 ){
193775 p->i--;
193776 }
193777 }else{
193778 p->i++;
193779 }
193780 }else{
193781 p->iEnd = p->i+1;
193782 }
193783 }
193784 return SQLITE_OK;
193785}
193786
193787/* The methods of the json_each virtual table */
193788static sqlite3_module jsonEachModule = {
193789 0, /* iVersion */
193790 0, /* xCreate */
193791 jsonEachConnect, /* xConnect */
193792 jsonEachBestIndex, /* xBestIndex */
193793 jsonEachDisconnect, /* xDisconnect */
193794 0, /* xDestroy */
193795 jsonEachOpenEach, /* xOpen - open a cursor */
193796 jsonEachClose, /* xClose - close a cursor */
193797 jsonEachFilter, /* xFilter - configure scan constraints */
193798 jsonEachNext, /* xNext - advance a cursor */
193799 jsonEachEof, /* xEof - check for end of scan */
193800 jsonEachColumn, /* xColumn - read data */
193801 jsonEachRowid, /* xRowid - read data */
193802 0, /* xUpdate */
193803 0, /* xBegin */
193804 0, /* xSync */
193805 0, /* xCommit */
193806 0, /* xRollback */
193807 0, /* xFindMethod */
193808 0, /* xRename */
193809 0, /* xSavepoint */
193810 0, /* xRelease */
193811 0, /* xRollbackTo */
193812 0 /* xShadowName */
193813};
193814
193815/* The methods of the json_tree virtual table. */
193816static sqlite3_module jsonTreeModule = {
193817 0, /* iVersion */
193818 0, /* xCreate */
193819 jsonEachConnect, /* xConnect */
193820 jsonEachBestIndex, /* xBestIndex */
193821 jsonEachDisconnect, /* xDisconnect */
193822 0, /* xDestroy */
193823 jsonEachOpenTree, /* xOpen - open a cursor */
193824 jsonEachClose, /* xClose - close a cursor */
193825 jsonEachFilter, /* xFilter - configure scan constraints */
193826 jsonEachNext, /* xNext - advance a cursor */
193827 jsonEachEof, /* xEof - check for end of scan */
193828 jsonEachColumn, /* xColumn - read data */
193829 jsonEachRowid, /* xRowid - read data */
193830 0, /* xUpdate */
193831 0, /* xBegin */
193832 0, /* xSync */
193833 0, /* xCommit */
193834 0, /* xRollback */
193835 0, /* xFindMethod */
193836 0, /* xRename */
193837 0, /* xSavepoint */
193838 0, /* xRelease */
193839 0, /* xRollbackTo */
193840 0 /* xShadowName */
193841};
193842#endif /* SQLITE_OMIT_VIRTUALTABLE */
193843
193844/****************************************************************************
193845** The following routines are the only publically visible identifiers in this
193846** file. Call the following routines in order to register the various SQL
193847** functions and the virtual table implemented by this file.
193848****************************************************************************/
193849
193850SQLITE_PRIVATE int sqlite3Json1Init(sqlite3 *db){
193851 int rc = SQLITE_OK;
193852 unsigned int i;
193853 static const struct {
193854 const char *zName;
193855 int nArg;
193856 int flag;
193857 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
193858 } aFunc[] = {
193859 { "json", 1, 0, jsonRemoveFunc },
193860 { "json_array", -1, 0, jsonArrayFunc },
193861 { "json_array_length", 1, 0, jsonArrayLengthFunc },
193862 { "json_array_length", 2, 0, jsonArrayLengthFunc },
193863 { "json_extract", -1, 0, jsonExtractFunc },
193864 { "json_insert", -1, 0, jsonSetFunc },
193865 { "json_object", -1, 0, jsonObjectFunc },
193866 { "json_patch", 2, 0, jsonPatchFunc },
193867 { "json_quote", 1, 0, jsonQuoteFunc },
193868 { "json_remove", -1, 0, jsonRemoveFunc },
193869 { "json_replace", -1, 0, jsonReplaceFunc },
193870 { "json_set", -1, 1, jsonSetFunc },
193871 { "json_type", 1, 0, jsonTypeFunc },
193872 { "json_type", 2, 0, jsonTypeFunc },
193873 { "json_valid", 1, 0, jsonValidFunc },
193874
193875#if SQLITE_DEBUG
193876 /* DEBUG and TESTING functions */
193877 { "json_parse", 1, 0, jsonParseFunc },
193878 { "json_test1", 1, 0, jsonTest1Func },
193879#endif
193880 };
193881 static const struct {
193882 const char *zName;
193883 int nArg;
193884 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
193885 void (*xFinal)(sqlite3_context*);
193886 void (*xValue)(sqlite3_context*);
193887 } aAgg[] = {
193888 { "json_group_array", 1,
193889 jsonArrayStep, jsonArrayFinal, jsonArrayValue },
193890 { "json_group_object", 2,
193891 jsonObjectStep, jsonObjectFinal, jsonObjectValue },
193892 };
193893#ifndef SQLITE_OMIT_VIRTUALTABLE
193894 static const struct {
193895 const char *zName;
193896 sqlite3_module *pModule;
193897 } aMod[] = {
193898 { "json_each", &jsonEachModule },
193899 { "json_tree", &jsonTreeModule },
193900 };
193901#endif
193902 static const int enc =
193903 SQLITE_UTF8 |
193904 SQLITE_DETERMINISTIC |
193905 SQLITE_INNOCUOUS;
193906 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
193907 rc = sqlite3_create_function(db, zFunc: aFunc[i].zName, nArg: aFunc[i].nArg, enc,
193908 p: (void*)&aFunc[i].flag,
193909 xSFunc: aFunc[i].xFunc, xStep: 0, xFinal: 0);
193910 }
193911#ifndef SQLITE_OMIT_WINDOWFUNC
193912 for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
193913 rc = sqlite3_create_window_function(db, zFunc: aAgg[i].zName, nArg: aAgg[i].nArg,
193914 SQLITE_SUBTYPE | enc, p: 0,
193915 xStep: aAgg[i].xStep, xFinal: aAgg[i].xFinal,
193916 xValue: aAgg[i].xValue, xInverse: jsonGroupInverse, xDestroy: 0);
193917 }
193918#endif
193919#ifndef SQLITE_OMIT_VIRTUALTABLE
193920 for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){
193921 rc = sqlite3_create_module(db, zName: aMod[i].zName, pModule: aMod[i].pModule, pAux: 0);
193922 }
193923#endif
193924 return rc;
193925}
193926
193927
193928#ifndef SQLITE_CORE
193929#ifdef _WIN32
193930__declspec(dllexport)
193931#endif
193932SQLITE_API int sqlite3_json_init(
193933 sqlite3 *db,
193934 char **pzErrMsg,
193935 const sqlite3_api_routines *pApi
193936){
193937 SQLITE_EXTENSION_INIT2(pApi);
193938 (void)pzErrMsg; /* Unused parameter */
193939 return sqlite3Json1Init(db);
193940}
193941#endif
193942#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1) */
193943
193944/************** End of json1.c ***********************************************/
193945/************** Begin file rtree.c *******************************************/
193946/*
193947** 2001 September 15
193948**
193949** The author disclaims copyright to this source code. In place of
193950** a legal notice, here is a blessing:
193951**
193952** May you do good and not evil.
193953** May you find forgiveness for yourself and forgive others.
193954** May you share freely, never taking more than you give.
193955**
193956*************************************************************************
193957** This file contains code for implementations of the r-tree and r*-tree
193958** algorithms packaged as an SQLite virtual table module.
193959*/
193960
193961/*
193962** Database Format of R-Tree Tables
193963** --------------------------------
193964**
193965** The data structure for a single virtual r-tree table is stored in three
193966** native SQLite tables declared as follows. In each case, the '%' character
193967** in the table name is replaced with the user-supplied name of the r-tree
193968** table.
193969**
193970** CREATE TABLE %_node(nodeno INTEGER PRIMARY KEY, data BLOB)
193971** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
193972** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER, ...)
193973**
193974** The data for each node of the r-tree structure is stored in the %_node
193975** table. For each node that is not the root node of the r-tree, there is
193976** an entry in the %_parent table associating the node with its parent.
193977** And for each row of data in the table, there is an entry in the %_rowid
193978** table that maps from the entries rowid to the id of the node that it
193979** is stored on. If the r-tree contains auxiliary columns, those are stored
193980** on the end of the %_rowid table.
193981**
193982** The root node of an r-tree always exists, even if the r-tree table is
193983** empty. The nodeno of the root node is always 1. All other nodes in the
193984** table must be the same size as the root node. The content of each node
193985** is formatted as follows:
193986**
193987** 1. If the node is the root node (node 1), then the first 2 bytes
193988** of the node contain the tree depth as a big-endian integer.
193989** For non-root nodes, the first 2 bytes are left unused.
193990**
193991** 2. The next 2 bytes contain the number of entries currently
193992** stored in the node.
193993**
193994** 3. The remainder of the node contains the node entries. Each entry
193995** consists of a single 8-byte integer followed by an even number
193996** of 4-byte coordinates. For leaf nodes the integer is the rowid
193997** of a record. For internal nodes it is the node number of a
193998** child page.
193999*/
194000
194001#if !defined(SQLITE_CORE) \
194002 || (defined(SQLITE_ENABLE_RTREE) && !defined(SQLITE_OMIT_VIRTUALTABLE))
194003
194004#ifndef SQLITE_CORE
194005/* #include "sqlite3ext.h" */
194006 SQLITE_EXTENSION_INIT1
194007#else
194008/* #include "sqlite3.h" */
194009#endif
194010SQLITE_PRIVATE int sqlite3GetToken(const unsigned char*,int*); /* In the SQLite core */
194011
194012/*
194013** If building separately, we will need some setup that is normally
194014** found in sqliteInt.h
194015*/
194016#if !defined(SQLITE_AMALGAMATION)
194017#include "sqlite3rtree.h"
194018typedef sqlite3_int64 i64;
194019typedef sqlite3_uint64 u64;
194020typedef unsigned char u8;
194021typedef unsigned short u16;
194022typedef unsigned int u32;
194023#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
194024# define NDEBUG 1
194025#endif
194026#if defined(NDEBUG) && defined(SQLITE_DEBUG)
194027# undef NDEBUG
194028#endif
194029#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
194030# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
194031#endif
194032#if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
194033# define ALWAYS(X) (1)
194034# define NEVER(X) (0)
194035#elif !defined(NDEBUG)
194036# define ALWAYS(X) ((X)?1:(assert(0),0))
194037# define NEVER(X) ((X)?(assert(0),1):0)
194038#else
194039# define ALWAYS(X) (X)
194040# define NEVER(X) (X)
194041#endif
194042#endif /* !defined(SQLITE_AMALGAMATION) */
194043
194044/* #include <string.h> */
194045/* #include <stdio.h> */
194046/* #include <assert.h> */
194047/* #include <stdlib.h> */
194048
194049/* The following macro is used to suppress compiler warnings.
194050*/
194051#ifndef UNUSED_PARAMETER
194052# define UNUSED_PARAMETER(x) (void)(x)
194053#endif
194054
194055typedef struct Rtree Rtree;
194056typedef struct RtreeCursor RtreeCursor;
194057typedef struct RtreeNode RtreeNode;
194058typedef struct RtreeCell RtreeCell;
194059typedef struct RtreeConstraint RtreeConstraint;
194060typedef struct RtreeMatchArg RtreeMatchArg;
194061typedef struct RtreeGeomCallback RtreeGeomCallback;
194062typedef union RtreeCoord RtreeCoord;
194063typedef struct RtreeSearchPoint RtreeSearchPoint;
194064
194065/* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */
194066#define RTREE_MAX_DIMENSIONS 5
194067
194068/* Maximum number of auxiliary columns */
194069#define RTREE_MAX_AUX_COLUMN 100
194070
194071/* Size of hash table Rtree.aHash. This hash table is not expected to
194072** ever contain very many entries, so a fixed number of buckets is
194073** used.
194074*/
194075#define HASHSIZE 97
194076
194077/* The xBestIndex method of this virtual table requires an estimate of
194078** the number of rows in the virtual table to calculate the costs of
194079** various strategies. If possible, this estimate is loaded from the
194080** sqlite_stat1 table (with RTREE_MIN_ROWEST as a hard-coded minimum).
194081** Otherwise, if no sqlite_stat1 entry is available, use
194082** RTREE_DEFAULT_ROWEST.
194083*/
194084#define RTREE_DEFAULT_ROWEST 1048576
194085#define RTREE_MIN_ROWEST 100
194086
194087/*
194088** An rtree virtual-table object.
194089*/
194090struct Rtree {
194091 sqlite3_vtab base; /* Base class. Must be first */
194092 sqlite3 *db; /* Host database connection */
194093 int iNodeSize; /* Size in bytes of each node in the node table */
194094 u8 nDim; /* Number of dimensions */
194095 u8 nDim2; /* Twice the number of dimensions */
194096 u8 eCoordType; /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */
194097 u8 nBytesPerCell; /* Bytes consumed per cell */
194098 u8 inWrTrans; /* True if inside write transaction */
194099 u8 nAux; /* # of auxiliary columns in %_rowid */
194100#ifdef SQLITE_ENABLE_GEOPOLY
194101 u8 nAuxNotNull; /* Number of initial not-null aux columns */
194102#endif
194103#ifdef SQLITE_DEBUG
194104 u8 bCorrupt; /* Shadow table corruption detected */
194105#endif
194106 int iDepth; /* Current depth of the r-tree structure */
194107 char *zDb; /* Name of database containing r-tree table */
194108 char *zName; /* Name of r-tree table */
194109 u32 nBusy; /* Current number of users of this structure */
194110 i64 nRowEst; /* Estimated number of rows in this table */
194111 u32 nCursor; /* Number of open cursors */
194112 u32 nNodeRef; /* Number RtreeNodes with positive nRef */
194113 char *zReadAuxSql; /* SQL for statement to read aux data */
194114
194115 /* List of nodes removed during a CondenseTree operation. List is
194116 ** linked together via the pointer normally used for hash chains -
194117 ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree
194118 ** headed by the node (leaf nodes have RtreeNode.iNode==0).
194119 */
194120 RtreeNode *pDeleted;
194121 int iReinsertHeight; /* Height of sub-trees Reinsert() has run on */
194122
194123 /* Blob I/O on xxx_node */
194124 sqlite3_blob *pNodeBlob;
194125
194126 /* Statements to read/write/delete a record from xxx_node */
194127 sqlite3_stmt *pWriteNode;
194128 sqlite3_stmt *pDeleteNode;
194129
194130 /* Statements to read/write/delete a record from xxx_rowid */
194131 sqlite3_stmt *pReadRowid;
194132 sqlite3_stmt *pWriteRowid;
194133 sqlite3_stmt *pDeleteRowid;
194134
194135 /* Statements to read/write/delete a record from xxx_parent */
194136 sqlite3_stmt *pReadParent;
194137 sqlite3_stmt *pWriteParent;
194138 sqlite3_stmt *pDeleteParent;
194139
194140 /* Statement for writing to the "aux:" fields, if there are any */
194141 sqlite3_stmt *pWriteAux;
194142
194143 RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */
194144};
194145
194146/* Possible values for Rtree.eCoordType: */
194147#define RTREE_COORD_REAL32 0
194148#define RTREE_COORD_INT32 1
194149
194150/*
194151** If SQLITE_RTREE_INT_ONLY is defined, then this virtual table will
194152** only deal with integer coordinates. No floating point operations
194153** will be done.
194154*/
194155#ifdef SQLITE_RTREE_INT_ONLY
194156 typedef sqlite3_int64 RtreeDValue; /* High accuracy coordinate */
194157 typedef int RtreeValue; /* Low accuracy coordinate */
194158# define RTREE_ZERO 0
194159#else
194160 typedef double RtreeDValue; /* High accuracy coordinate */
194161 typedef float RtreeValue; /* Low accuracy coordinate */
194162# define RTREE_ZERO 0.0
194163#endif
194164
194165/*
194166** Set the Rtree.bCorrupt flag
194167*/
194168#ifdef SQLITE_DEBUG
194169# define RTREE_IS_CORRUPT(X) ((X)->bCorrupt = 1)
194170#else
194171# define RTREE_IS_CORRUPT(X)
194172#endif
194173
194174/*
194175** When doing a search of an r-tree, instances of the following structure
194176** record intermediate results from the tree walk.
194177**
194178** The id is always a node-id. For iLevel>=1 the id is the node-id of
194179** the node that the RtreeSearchPoint represents. When iLevel==0, however,
194180** the id is of the parent node and the cell that RtreeSearchPoint
194181** represents is the iCell-th entry in the parent node.
194182*/
194183struct RtreeSearchPoint {
194184 RtreeDValue rScore; /* The score for this node. Smallest goes first. */
194185 sqlite3_int64 id; /* Node ID */
194186 u8 iLevel; /* 0=entries. 1=leaf node. 2+ for higher */
194187 u8 eWithin; /* PARTLY_WITHIN or FULLY_WITHIN */
194188 u8 iCell; /* Cell index within the node */
194189};
194190
194191/*
194192** The minimum number of cells allowed for a node is a third of the
194193** maximum. In Gutman's notation:
194194**
194195** m = M/3
194196**
194197** If an R*-tree "Reinsert" operation is required, the same number of
194198** cells are removed from the overfull node and reinserted into the tree.
194199*/
194200#define RTREE_MINCELLS(p) ((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3)
194201#define RTREE_REINSERT(p) RTREE_MINCELLS(p)
194202#define RTREE_MAXCELLS 51
194203
194204/*
194205** The smallest possible node-size is (512-64)==448 bytes. And the largest
194206** supported cell size is 48 bytes (8 byte rowid + ten 4 byte coordinates).
194207** Therefore all non-root nodes must contain at least 3 entries. Since
194208** 3^40 is greater than 2^64, an r-tree structure always has a depth of
194209** 40 or less.
194210*/
194211#define RTREE_MAX_DEPTH 40
194212
194213
194214/*
194215** Number of entries in the cursor RtreeNode cache. The first entry is
194216** used to cache the RtreeNode for RtreeCursor.sPoint. The remaining
194217** entries cache the RtreeNode for the first elements of the priority queue.
194218*/
194219#define RTREE_CACHE_SZ 5
194220
194221/*
194222** An rtree cursor object.
194223*/
194224struct RtreeCursor {
194225 sqlite3_vtab_cursor base; /* Base class. Must be first */
194226 u8 atEOF; /* True if at end of search */
194227 u8 bPoint; /* True if sPoint is valid */
194228 u8 bAuxValid; /* True if pReadAux is valid */
194229 int iStrategy; /* Copy of idxNum search parameter */
194230 int nConstraint; /* Number of entries in aConstraint */
194231 RtreeConstraint *aConstraint; /* Search constraints. */
194232 int nPointAlloc; /* Number of slots allocated for aPoint[] */
194233 int nPoint; /* Number of slots used in aPoint[] */
194234 int mxLevel; /* iLevel value for root of the tree */
194235 RtreeSearchPoint *aPoint; /* Priority queue for search points */
194236 sqlite3_stmt *pReadAux; /* Statement to read aux-data */
194237 RtreeSearchPoint sPoint; /* Cached next search point */
194238 RtreeNode *aNode[RTREE_CACHE_SZ]; /* Rtree node cache */
194239 u32 anQueue[RTREE_MAX_DEPTH+1]; /* Number of queued entries by iLevel */
194240};
194241
194242/* Return the Rtree of a RtreeCursor */
194243#define RTREE_OF_CURSOR(X) ((Rtree*)((X)->base.pVtab))
194244
194245/*
194246** A coordinate can be either a floating point number or a integer. All
194247** coordinates within a single R-Tree are always of the same time.
194248*/
194249union RtreeCoord {
194250 RtreeValue f; /* Floating point value */
194251 int i; /* Integer value */
194252 u32 u; /* Unsigned for byte-order conversions */
194253};
194254
194255/*
194256** The argument is an RtreeCoord. Return the value stored within the RtreeCoord
194257** formatted as a RtreeDValue (double or int64). This macro assumes that local
194258** variable pRtree points to the Rtree structure associated with the
194259** RtreeCoord.
194260*/
194261#ifdef SQLITE_RTREE_INT_ONLY
194262# define DCOORD(coord) ((RtreeDValue)coord.i)
194263#else
194264# define DCOORD(coord) ( \
194265 (pRtree->eCoordType==RTREE_COORD_REAL32) ? \
194266 ((double)coord.f) : \
194267 ((double)coord.i) \
194268 )
194269#endif
194270
194271/*
194272** A search constraint.
194273*/
194274struct RtreeConstraint {
194275 int iCoord; /* Index of constrained coordinate */
194276 int op; /* Constraining operation */
194277 union {
194278 RtreeDValue rValue; /* Constraint value. */
194279 int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*);
194280 int (*xQueryFunc)(sqlite3_rtree_query_info*);
194281 } u;
194282 sqlite3_rtree_query_info *pInfo; /* xGeom and xQueryFunc argument */
194283};
194284
194285/* Possible values for RtreeConstraint.op */
194286#define RTREE_EQ 0x41 /* A */
194287#define RTREE_LE 0x42 /* B */
194288#define RTREE_LT 0x43 /* C */
194289#define RTREE_GE 0x44 /* D */
194290#define RTREE_GT 0x45 /* E */
194291#define RTREE_MATCH 0x46 /* F: Old-style sqlite3_rtree_geometry_callback() */
194292#define RTREE_QUERY 0x47 /* G: New-style sqlite3_rtree_query_callback() */
194293
194294/* Special operators available only on cursors. Needs to be consecutive
194295** with the normal values above, but must be less than RTREE_MATCH. These
194296** are used in the cursor for contraints such as x=NULL (RTREE_FALSE) or
194297** x<'xyz' (RTREE_TRUE) */
194298#define RTREE_TRUE 0x3f /* ? */
194299#define RTREE_FALSE 0x40 /* @ */
194300
194301/*
194302** An rtree structure node.
194303*/
194304struct RtreeNode {
194305 RtreeNode *pParent; /* Parent node */
194306 i64 iNode; /* The node number */
194307 int nRef; /* Number of references to this node */
194308 int isDirty; /* True if the node needs to be written to disk */
194309 u8 *zData; /* Content of the node, as should be on disk */
194310 RtreeNode *pNext; /* Next node in this hash collision chain */
194311};
194312
194313/* Return the number of cells in a node */
194314#define NCELL(pNode) readInt16(&(pNode)->zData[2])
194315
194316/*
194317** A single cell from a node, deserialized
194318*/
194319struct RtreeCell {
194320 i64 iRowid; /* Node or entry ID */
194321 RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2]; /* Bounding box coordinates */
194322};
194323
194324
194325/*
194326** This object becomes the sqlite3_user_data() for the SQL functions
194327** that are created by sqlite3_rtree_geometry_callback() and
194328** sqlite3_rtree_query_callback() and which appear on the right of MATCH
194329** operators in order to constrain a search.
194330**
194331** xGeom and xQueryFunc are the callback functions. Exactly one of
194332** xGeom and xQueryFunc fields is non-NULL, depending on whether the
194333** SQL function was created using sqlite3_rtree_geometry_callback() or
194334** sqlite3_rtree_query_callback().
194335**
194336** This object is deleted automatically by the destructor mechanism in
194337** sqlite3_create_function_v2().
194338*/
194339struct RtreeGeomCallback {
194340 int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
194341 int (*xQueryFunc)(sqlite3_rtree_query_info*);
194342 void (*xDestructor)(void*);
194343 void *pContext;
194344};
194345
194346/*
194347** An instance of this structure (in the form of a BLOB) is returned by
194348** the SQL functions that sqlite3_rtree_geometry_callback() and
194349** sqlite3_rtree_query_callback() create, and is read as the right-hand
194350** operand to the MATCH operator of an R-Tree.
194351*/
194352struct RtreeMatchArg {
194353 u32 iSize; /* Size of this object */
194354 RtreeGeomCallback cb; /* Info about the callback functions */
194355 int nParam; /* Number of parameters to the SQL function */
194356 sqlite3_value **apSqlParam; /* Original SQL parameter values */
194357 RtreeDValue aParam[1]; /* Values for parameters to the SQL function */
194358};
194359
194360#ifndef MAX
194361# define MAX(x,y) ((x) < (y) ? (y) : (x))
194362#endif
194363#ifndef MIN
194364# define MIN(x,y) ((x) > (y) ? (y) : (x))
194365#endif
194366
194367/* What version of GCC is being used. 0 means GCC is not being used .
194368** Note that the GCC_VERSION macro will also be set correctly when using
194369** clang, since clang works hard to be gcc compatible. So the gcc
194370** optimizations will also work when compiling with clang.
194371*/
194372#ifndef GCC_VERSION
194373#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
194374# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
194375#else
194376# define GCC_VERSION 0
194377#endif
194378#endif
194379
194380/* The testcase() macro should already be defined in the amalgamation. If
194381** it is not, make it a no-op.
194382*/
194383#ifndef SQLITE_AMALGAMATION
194384# if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_DEBUG)
194385 unsigned int sqlite3RtreeTestcase = 0;
194386# define testcase(X) if( X ){ sqlite3RtreeTestcase += __LINE__; }
194387# else
194388# define testcase(X)
194389# endif
194390#endif
194391
194392/*
194393** Make sure that the compiler intrinsics we desire are enabled when
194394** compiling with an appropriate version of MSVC unless prevented by
194395** the SQLITE_DISABLE_INTRINSIC define.
194396*/
194397#if !defined(SQLITE_DISABLE_INTRINSIC)
194398# if defined(_MSC_VER) && _MSC_VER>=1400
194399# if !defined(_WIN32_WCE)
194400/* # include <intrin.h> */
194401# pragma intrinsic(_byteswap_ulong)
194402# pragma intrinsic(_byteswap_uint64)
194403# else
194404/* # include <cmnintrin.h> */
194405# endif
194406# endif
194407#endif
194408
194409/*
194410** Macros to determine whether the machine is big or little endian,
194411** and whether or not that determination is run-time or compile-time.
194412**
194413** For best performance, an attempt is made to guess at the byte-order
194414** using C-preprocessor macros. If that is unsuccessful, or if
194415** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
194416** at run-time.
194417*/
194418#ifndef SQLITE_BYTEORDER
194419#if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
194420 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
194421 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
194422 defined(__arm__)
194423# define SQLITE_BYTEORDER 1234
194424#elif defined(sparc) || defined(__ppc__)
194425# define SQLITE_BYTEORDER 4321
194426#else
194427# define SQLITE_BYTEORDER 0 /* 0 means "unknown at compile-time" */
194428#endif
194429#endif
194430
194431
194432/* What version of MSVC is being used. 0 means MSVC is not being used */
194433#ifndef MSVC_VERSION
194434#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
194435# define MSVC_VERSION _MSC_VER
194436#else
194437# define MSVC_VERSION 0
194438#endif
194439#endif
194440
194441/*
194442** Functions to deserialize a 16 bit integer, 32 bit real number and
194443** 64 bit integer. The deserialized value is returned.
194444*/
194445static int readInt16(u8 *p){
194446 return (p[0]<<8) + p[1];
194447}
194448static void readCoord(u8 *p, RtreeCoord *pCoord){
194449 assert( ((((char*)p) - (char*)0)&3)==0 ); /* p is always 4-byte aligned */
194450#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
194451 pCoord->u = _byteswap_ulong(*(u32*)p);
194452#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
194453 pCoord->u = __builtin_bswap32(*(u32*)p);
194454#elif SQLITE_BYTEORDER==4321
194455 pCoord->u = *(u32*)p;
194456#else
194457 pCoord->u = (
194458 (((u32)p[0]) << 24) +
194459 (((u32)p[1]) << 16) +
194460 (((u32)p[2]) << 8) +
194461 (((u32)p[3]) << 0)
194462 );
194463#endif
194464}
194465static i64 readInt64(u8 *p){
194466#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
194467 u64 x;
194468 memcpy(&x, p, 8);
194469 return (i64)_byteswap_uint64(x);
194470#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
194471 u64 x;
194472 memcpy(&x, p, 8);
194473 return (i64)__builtin_bswap64(x);
194474#elif SQLITE_BYTEORDER==4321
194475 i64 x;
194476 memcpy(&x, p, 8);
194477 return x;
194478#else
194479 return (i64)(
194480 (((u64)p[0]) << 56) +
194481 (((u64)p[1]) << 48) +
194482 (((u64)p[2]) << 40) +
194483 (((u64)p[3]) << 32) +
194484 (((u64)p[4]) << 24) +
194485 (((u64)p[5]) << 16) +
194486 (((u64)p[6]) << 8) +
194487 (((u64)p[7]) << 0)
194488 );
194489#endif
194490}
194491
194492/*
194493** Functions to serialize a 16 bit integer, 32 bit real number and
194494** 64 bit integer. The value returned is the number of bytes written
194495** to the argument buffer (always 2, 4 and 8 respectively).
194496*/
194497static void writeInt16(u8 *p, int i){
194498 p[0] = (i>> 8)&0xFF;
194499 p[1] = (i>> 0)&0xFF;
194500}
194501static int writeCoord(u8 *p, RtreeCoord *pCoord){
194502 u32 i;
194503 assert( ((((char*)p) - (char*)0)&3)==0 ); /* p is always 4-byte aligned */
194504 assert( sizeof(RtreeCoord)==4 );
194505 assert( sizeof(u32)==4 );
194506#if SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
194507 i = __builtin_bswap32(pCoord->u);
194508 memcpy(p, &i, 4);
194509#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
194510 i = _byteswap_ulong(pCoord->u);
194511 memcpy(p, &i, 4);
194512#elif SQLITE_BYTEORDER==4321
194513 i = pCoord->u;
194514 memcpy(p, &i, 4);
194515#else
194516 i = pCoord->u;
194517 p[0] = (i>>24)&0xFF;
194518 p[1] = (i>>16)&0xFF;
194519 p[2] = (i>> 8)&0xFF;
194520 p[3] = (i>> 0)&0xFF;
194521#endif
194522 return 4;
194523}
194524static int writeInt64(u8 *p, i64 i){
194525#if SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
194526 i = (i64)__builtin_bswap64((u64)i);
194527 memcpy(p, &i, 8);
194528#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
194529 i = (i64)_byteswap_uint64((u64)i);
194530 memcpy(p, &i, 8);
194531#elif SQLITE_BYTEORDER==4321
194532 memcpy(p, &i, 8);
194533#else
194534 p[0] = (i>>56)&0xFF;
194535 p[1] = (i>>48)&0xFF;
194536 p[2] = (i>>40)&0xFF;
194537 p[3] = (i>>32)&0xFF;
194538 p[4] = (i>>24)&0xFF;
194539 p[5] = (i>>16)&0xFF;
194540 p[6] = (i>> 8)&0xFF;
194541 p[7] = (i>> 0)&0xFF;
194542#endif
194543 return 8;
194544}
194545
194546/*
194547** Increment the reference count of node p.
194548*/
194549static void nodeReference(RtreeNode *p){
194550 if( p ){
194551 assert( p->nRef>0 );
194552 p->nRef++;
194553 }
194554}
194555
194556/*
194557** Clear the content of node p (set all bytes to 0x00).
194558*/
194559static void nodeZero(Rtree *pRtree, RtreeNode *p){
194560 memset(s: &p->zData[2], c: 0, n: pRtree->iNodeSize-2);
194561 p->isDirty = 1;
194562}
194563
194564/*
194565** Given a node number iNode, return the corresponding key to use
194566** in the Rtree.aHash table.
194567*/
194568static unsigned int nodeHash(i64 iNode){
194569 return ((unsigned)iNode) % HASHSIZE;
194570}
194571
194572/*
194573** Search the node hash table for node iNode. If found, return a pointer
194574** to it. Otherwise, return 0.
194575*/
194576static RtreeNode *nodeHashLookup(Rtree *pRtree, i64 iNode){
194577 RtreeNode *p;
194578 for(p=pRtree->aHash[nodeHash(iNode)]; p && p->iNode!=iNode; p=p->pNext);
194579 return p;
194580}
194581
194582/*
194583** Add node pNode to the node hash table.
194584*/
194585static void nodeHashInsert(Rtree *pRtree, RtreeNode *pNode){
194586 int iHash;
194587 assert( pNode->pNext==0 );
194588 iHash = nodeHash(iNode: pNode->iNode);
194589 pNode->pNext = pRtree->aHash[iHash];
194590 pRtree->aHash[iHash] = pNode;
194591}
194592
194593/*
194594** Remove node pNode from the node hash table.
194595*/
194596static void nodeHashDelete(Rtree *pRtree, RtreeNode *pNode){
194597 RtreeNode **pp;
194598 if( pNode->iNode!=0 ){
194599 pp = &pRtree->aHash[nodeHash(iNode: pNode->iNode)];
194600 for( ; (*pp)!=pNode; pp = &(*pp)->pNext){ assert(*pp); }
194601 *pp = pNode->pNext;
194602 pNode->pNext = 0;
194603 }
194604}
194605
194606/*
194607** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0),
194608** indicating that node has not yet been assigned a node number. It is
194609** assigned a node number when nodeWrite() is called to write the
194610** node contents out to the database.
194611*/
194612static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent){
194613 RtreeNode *pNode;
194614 pNode = (RtreeNode *)sqlite3_malloc64(n: sizeof(RtreeNode) + pRtree->iNodeSize);
194615 if( pNode ){
194616 memset(s: pNode, c: 0, n: sizeof(RtreeNode) + pRtree->iNodeSize);
194617 pNode->zData = (u8 *)&pNode[1];
194618 pNode->nRef = 1;
194619 pRtree->nNodeRef++;
194620 pNode->pParent = pParent;
194621 pNode->isDirty = 1;
194622 nodeReference(p: pParent);
194623 }
194624 return pNode;
194625}
194626
194627/*
194628** Clear the Rtree.pNodeBlob object
194629*/
194630static void nodeBlobReset(Rtree *pRtree){
194631 if( pRtree->pNodeBlob && pRtree->inWrTrans==0 && pRtree->nCursor==0 ){
194632 sqlite3_blob *pBlob = pRtree->pNodeBlob;
194633 pRtree->pNodeBlob = 0;
194634 sqlite3_blob_close(pBlob);
194635 }
194636}
194637
194638/*
194639** Obtain a reference to an r-tree node.
194640*/
194641static int nodeAcquire(
194642 Rtree *pRtree, /* R-tree structure */
194643 i64 iNode, /* Node number to load */
194644 RtreeNode *pParent, /* Either the parent node or NULL */
194645 RtreeNode **ppNode /* OUT: Acquired node */
194646){
194647 int rc = SQLITE_OK;
194648 RtreeNode *pNode = 0;
194649
194650 /* Check if the requested node is already in the hash table. If so,
194651 ** increase its reference count and return it.
194652 */
194653 if( (pNode = nodeHashLookup(pRtree, iNode))!=0 ){
194654 if( pParent && pParent!=pNode->pParent ){
194655 RTREE_IS_CORRUPT(pRtree);
194656 return SQLITE_CORRUPT_VTAB;
194657 }
194658 pNode->nRef++;
194659 *ppNode = pNode;
194660 return SQLITE_OK;
194661 }
194662
194663 if( pRtree->pNodeBlob ){
194664 sqlite3_blob *pBlob = pRtree->pNodeBlob;
194665 pRtree->pNodeBlob = 0;
194666 rc = sqlite3_blob_reopen(pBlob, iRow: iNode);
194667 pRtree->pNodeBlob = pBlob;
194668 if( rc ){
194669 nodeBlobReset(pRtree);
194670 if( rc==SQLITE_NOMEM ) return SQLITE_NOMEM;
194671 }
194672 }
194673 if( pRtree->pNodeBlob==0 ){
194674 char *zTab = sqlite3_mprintf(zFormat: "%s_node", pRtree->zName);
194675 if( zTab==0 ) return SQLITE_NOMEM;
194676 rc = sqlite3_blob_open(db: pRtree->db, zDb: pRtree->zDb, zTable: zTab, zColumn: "data", iRow: iNode, wrFlag: 0,
194677 ppBlob: &pRtree->pNodeBlob);
194678 sqlite3_free(p: zTab);
194679 }
194680 if( rc ){
194681 nodeBlobReset(pRtree);
194682 *ppNode = 0;
194683 /* If unable to open an sqlite3_blob on the desired row, that can only
194684 ** be because the shadow tables hold erroneous data. */
194685 if( rc==SQLITE_ERROR ){
194686 rc = SQLITE_CORRUPT_VTAB;
194687 RTREE_IS_CORRUPT(pRtree);
194688 }
194689 }else if( pRtree->iNodeSize==sqlite3_blob_bytes(pBlob: pRtree->pNodeBlob) ){
194690 pNode = (RtreeNode *)sqlite3_malloc64(n: sizeof(RtreeNode)+pRtree->iNodeSize);
194691 if( !pNode ){
194692 rc = SQLITE_NOMEM;
194693 }else{
194694 pNode->pParent = pParent;
194695 pNode->zData = (u8 *)&pNode[1];
194696 pNode->nRef = 1;
194697 pRtree->nNodeRef++;
194698 pNode->iNode = iNode;
194699 pNode->isDirty = 0;
194700 pNode->pNext = 0;
194701 rc = sqlite3_blob_read(pBlob: pRtree->pNodeBlob, z: pNode->zData,
194702 n: pRtree->iNodeSize, iOffset: 0);
194703 }
194704 }
194705
194706 /* If the root node was just loaded, set pRtree->iDepth to the height
194707 ** of the r-tree structure. A height of zero means all data is stored on
194708 ** the root node. A height of one means the children of the root node
194709 ** are the leaves, and so on. If the depth as specified on the root node
194710 ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt.
194711 */
194712 if( rc==SQLITE_OK && pNode && iNode==1 ){
194713 pRtree->iDepth = readInt16(p: pNode->zData);
194714 if( pRtree->iDepth>RTREE_MAX_DEPTH ){
194715 rc = SQLITE_CORRUPT_VTAB;
194716 RTREE_IS_CORRUPT(pRtree);
194717 }
194718 }
194719
194720 /* If no error has occurred so far, check if the "number of entries"
194721 ** field on the node is too large. If so, set the return code to
194722 ** SQLITE_CORRUPT_VTAB.
194723 */
194724 if( pNode && rc==SQLITE_OK ){
194725 if( NCELL(pNode)>((pRtree->iNodeSize-4)/pRtree->nBytesPerCell) ){
194726 rc = SQLITE_CORRUPT_VTAB;
194727 RTREE_IS_CORRUPT(pRtree);
194728 }
194729 }
194730
194731 if( rc==SQLITE_OK ){
194732 if( pNode!=0 ){
194733 nodeReference(p: pParent);
194734 nodeHashInsert(pRtree, pNode);
194735 }else{
194736 rc = SQLITE_CORRUPT_VTAB;
194737 RTREE_IS_CORRUPT(pRtree);
194738 }
194739 *ppNode = pNode;
194740 }else{
194741 if( pNode ){
194742 pRtree->nNodeRef--;
194743 sqlite3_free(p: pNode);
194744 }
194745 *ppNode = 0;
194746 }
194747
194748 return rc;
194749}
194750
194751/*
194752** Overwrite cell iCell of node pNode with the contents of pCell.
194753*/
194754static void nodeOverwriteCell(
194755 Rtree *pRtree, /* The overall R-Tree */
194756 RtreeNode *pNode, /* The node into which the cell is to be written */
194757 RtreeCell *pCell, /* The cell to write */
194758 int iCell /* Index into pNode into which pCell is written */
194759){
194760 int ii;
194761 u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
194762 p += writeInt64(p, i: pCell->iRowid);
194763 for(ii=0; ii<pRtree->nDim2; ii++){
194764 p += writeCoord(p, pCoord: &pCell->aCoord[ii]);
194765 }
194766 pNode->isDirty = 1;
194767}
194768
194769/*
194770** Remove the cell with index iCell from node pNode.
194771*/
194772static void nodeDeleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell){
194773 u8 *pDst = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
194774 u8 *pSrc = &pDst[pRtree->nBytesPerCell];
194775 int nByte = (NCELL(pNode) - iCell - 1) * pRtree->nBytesPerCell;
194776 memmove(dest: pDst, src: pSrc, n: nByte);
194777 writeInt16(p: &pNode->zData[2], NCELL(pNode)-1);
194778 pNode->isDirty = 1;
194779}
194780
194781/*
194782** Insert the contents of cell pCell into node pNode. If the insert
194783** is successful, return SQLITE_OK.
194784**
194785** If there is not enough free space in pNode, return SQLITE_FULL.
194786*/
194787static int nodeInsertCell(
194788 Rtree *pRtree, /* The overall R-Tree */
194789 RtreeNode *pNode, /* Write new cell into this node */
194790 RtreeCell *pCell /* The cell to be inserted */
194791){
194792 int nCell; /* Current number of cells in pNode */
194793 int nMaxCell; /* Maximum number of cells for pNode */
194794
194795 nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell;
194796 nCell = NCELL(pNode);
194797
194798 assert( nCell<=nMaxCell );
194799 if( nCell<nMaxCell ){
194800 nodeOverwriteCell(pRtree, pNode, pCell, iCell: nCell);
194801 writeInt16(p: &pNode->zData[2], i: nCell+1);
194802 pNode->isDirty = 1;
194803 }
194804
194805 return (nCell==nMaxCell);
194806}
194807
194808/*
194809** If the node is dirty, write it out to the database.
194810*/
194811static int nodeWrite(Rtree *pRtree, RtreeNode *pNode){
194812 int rc = SQLITE_OK;
194813 if( pNode->isDirty ){
194814 sqlite3_stmt *p = pRtree->pWriteNode;
194815 if( pNode->iNode ){
194816 sqlite3_bind_int64(pStmt: p, i: 1, iValue: pNode->iNode);
194817 }else{
194818 sqlite3_bind_null(pStmt: p, i: 1);
194819 }
194820 sqlite3_bind_blob(pStmt: p, i: 2, zData: pNode->zData, nData: pRtree->iNodeSize, SQLITE_STATIC);
194821 sqlite3_step(pStmt: p);
194822 pNode->isDirty = 0;
194823 rc = sqlite3_reset(pStmt: p);
194824 sqlite3_bind_null(pStmt: p, i: 2);
194825 if( pNode->iNode==0 && rc==SQLITE_OK ){
194826 pNode->iNode = sqlite3_last_insert_rowid(db: pRtree->db);
194827 nodeHashInsert(pRtree, pNode);
194828 }
194829 }
194830 return rc;
194831}
194832
194833/*
194834** Release a reference to a node. If the node is dirty and the reference
194835** count drops to zero, the node data is written to the database.
194836*/
194837static int nodeRelease(Rtree *pRtree, RtreeNode *pNode){
194838 int rc = SQLITE_OK;
194839 if( pNode ){
194840 assert( pNode->nRef>0 );
194841 assert( pRtree->nNodeRef>0 );
194842 pNode->nRef--;
194843 if( pNode->nRef==0 ){
194844 pRtree->nNodeRef--;
194845 if( pNode->iNode==1 ){
194846 pRtree->iDepth = -1;
194847 }
194848 if( pNode->pParent ){
194849 rc = nodeRelease(pRtree, pNode: pNode->pParent);
194850 }
194851 if( rc==SQLITE_OK ){
194852 rc = nodeWrite(pRtree, pNode);
194853 }
194854 nodeHashDelete(pRtree, pNode);
194855 sqlite3_free(p: pNode);
194856 }
194857 }
194858 return rc;
194859}
194860
194861/*
194862** Return the 64-bit integer value associated with cell iCell of
194863** node pNode. If pNode is a leaf node, this is a rowid. If it is
194864** an internal node, then the 64-bit integer is a child page number.
194865*/
194866static i64 nodeGetRowid(
194867 Rtree *pRtree, /* The overall R-Tree */
194868 RtreeNode *pNode, /* The node from which to extract the ID */
194869 int iCell /* The cell index from which to extract the ID */
194870){
194871 assert( iCell<NCELL(pNode) );
194872 return readInt64(p: &pNode->zData[4 + pRtree->nBytesPerCell*iCell]);
194873}
194874
194875/*
194876** Return coordinate iCoord from cell iCell in node pNode.
194877*/
194878static void nodeGetCoord(
194879 Rtree *pRtree, /* The overall R-Tree */
194880 RtreeNode *pNode, /* The node from which to extract a coordinate */
194881 int iCell, /* The index of the cell within the node */
194882 int iCoord, /* Which coordinate to extract */
194883 RtreeCoord *pCoord /* OUT: Space to write result to */
194884){
194885 readCoord(p: &pNode->zData[12 + pRtree->nBytesPerCell*iCell + 4*iCoord], pCoord);
194886}
194887
194888/*
194889** Deserialize cell iCell of node pNode. Populate the structure pointed
194890** to by pCell with the results.
194891*/
194892static void nodeGetCell(
194893 Rtree *pRtree, /* The overall R-Tree */
194894 RtreeNode *pNode, /* The node containing the cell to be read */
194895 int iCell, /* Index of the cell within the node */
194896 RtreeCell *pCell /* OUT: Write the cell contents here */
194897){
194898 u8 *pData;
194899 RtreeCoord *pCoord;
194900 int ii = 0;
194901 pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
194902 pData = pNode->zData + (12 + pRtree->nBytesPerCell*iCell);
194903 pCoord = pCell->aCoord;
194904 do{
194905 readCoord(p: pData, pCoord: &pCoord[ii]);
194906 readCoord(p: pData+4, pCoord: &pCoord[ii+1]);
194907 pData += 8;
194908 ii += 2;
194909 }while( ii<pRtree->nDim2 );
194910}
194911
194912
194913/* Forward declaration for the function that does the work of
194914** the virtual table module xCreate() and xConnect() methods.
194915*/
194916static int rtreeInit(
194917 sqlite3 *, void *, int, const char *const*, sqlite3_vtab **, char **, int
194918);
194919
194920/*
194921** Rtree virtual table module xCreate method.
194922*/
194923static int rtreeCreate(
194924 sqlite3 *db,
194925 void *pAux,
194926 int argc, const char *const*argv,
194927 sqlite3_vtab **ppVtab,
194928 char **pzErr
194929){
194930 return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
194931}
194932
194933/*
194934** Rtree virtual table module xConnect method.
194935*/
194936static int rtreeConnect(
194937 sqlite3 *db,
194938 void *pAux,
194939 int argc, const char *const*argv,
194940 sqlite3_vtab **ppVtab,
194941 char **pzErr
194942){
194943 return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
194944}
194945
194946/*
194947** Increment the r-tree reference count.
194948*/
194949static void rtreeReference(Rtree *pRtree){
194950 pRtree->nBusy++;
194951}
194952
194953/*
194954** Decrement the r-tree reference count. When the reference count reaches
194955** zero the structure is deleted.
194956*/
194957static void rtreeRelease(Rtree *pRtree){
194958 pRtree->nBusy--;
194959 if( pRtree->nBusy==0 ){
194960 pRtree->inWrTrans = 0;
194961 assert( pRtree->nCursor==0 );
194962 nodeBlobReset(pRtree);
194963 assert( pRtree->nNodeRef==0 || pRtree->bCorrupt );
194964 sqlite3_finalize(pStmt: pRtree->pWriteNode);
194965 sqlite3_finalize(pStmt: pRtree->pDeleteNode);
194966 sqlite3_finalize(pStmt: pRtree->pReadRowid);
194967 sqlite3_finalize(pStmt: pRtree->pWriteRowid);
194968 sqlite3_finalize(pStmt: pRtree->pDeleteRowid);
194969 sqlite3_finalize(pStmt: pRtree->pReadParent);
194970 sqlite3_finalize(pStmt: pRtree->pWriteParent);
194971 sqlite3_finalize(pStmt: pRtree->pDeleteParent);
194972 sqlite3_finalize(pStmt: pRtree->pWriteAux);
194973 sqlite3_free(p: pRtree->zReadAuxSql);
194974 sqlite3_free(p: pRtree);
194975 }
194976}
194977
194978/*
194979** Rtree virtual table module xDisconnect method.
194980*/
194981static int rtreeDisconnect(sqlite3_vtab *pVtab){
194982 rtreeRelease(pRtree: (Rtree *)pVtab);
194983 return SQLITE_OK;
194984}
194985
194986/*
194987** Rtree virtual table module xDestroy method.
194988*/
194989static int rtreeDestroy(sqlite3_vtab *pVtab){
194990 Rtree *pRtree = (Rtree *)pVtab;
194991 int rc;
194992 char *zCreate = sqlite3_mprintf(
194993 zFormat: "DROP TABLE '%q'.'%q_node';"
194994 "DROP TABLE '%q'.'%q_rowid';"
194995 "DROP TABLE '%q'.'%q_parent';",
194996 pRtree->zDb, pRtree->zName,
194997 pRtree->zDb, pRtree->zName,
194998 pRtree->zDb, pRtree->zName
194999 );
195000 if( !zCreate ){
195001 rc = SQLITE_NOMEM;
195002 }else{
195003 nodeBlobReset(pRtree);
195004 rc = sqlite3_exec(db: pRtree->db, zSql: zCreate, xCallback: 0, pArg: 0, pzErrMsg: 0);
195005 sqlite3_free(p: zCreate);
195006 }
195007 if( rc==SQLITE_OK ){
195008 rtreeRelease(pRtree);
195009 }
195010
195011 return rc;
195012}
195013
195014/*
195015** Rtree virtual table module xOpen method.
195016*/
195017static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
195018 int rc = SQLITE_NOMEM;
195019 Rtree *pRtree = (Rtree *)pVTab;
195020 RtreeCursor *pCsr;
195021
195022 pCsr = (RtreeCursor *)sqlite3_malloc64(n: sizeof(RtreeCursor));
195023 if( pCsr ){
195024 memset(s: pCsr, c: 0, n: sizeof(RtreeCursor));
195025 pCsr->base.pVtab = pVTab;
195026 rc = SQLITE_OK;
195027 pRtree->nCursor++;
195028 }
195029 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
195030
195031 return rc;
195032}
195033
195034
195035/*
195036** Reset a cursor back to its initial state.
195037*/
195038static void resetCursor(RtreeCursor *pCsr){
195039 Rtree *pRtree = (Rtree *)(pCsr->base.pVtab);
195040 int ii;
195041 sqlite3_stmt *pStmt;
195042 if( pCsr->aConstraint ){
195043 int i; /* Used to iterate through constraint array */
195044 for(i=0; i<pCsr->nConstraint; i++){
195045 sqlite3_rtree_query_info *pInfo = pCsr->aConstraint[i].pInfo;
195046 if( pInfo ){
195047 if( pInfo->xDelUser ) pInfo->xDelUser(pInfo->pUser);
195048 sqlite3_free(p: pInfo);
195049 }
195050 }
195051 sqlite3_free(p: pCsr->aConstraint);
195052 pCsr->aConstraint = 0;
195053 }
195054 for(ii=0; ii<RTREE_CACHE_SZ; ii++) nodeRelease(pRtree, pNode: pCsr->aNode[ii]);
195055 sqlite3_free(p: pCsr->aPoint);
195056 pStmt = pCsr->pReadAux;
195057 memset(s: pCsr, c: 0, n: sizeof(RtreeCursor));
195058 pCsr->base.pVtab = (sqlite3_vtab*)pRtree;
195059 pCsr->pReadAux = pStmt;
195060
195061}
195062
195063/*
195064** Rtree virtual table module xClose method.
195065*/
195066static int rtreeClose(sqlite3_vtab_cursor *cur){
195067 Rtree *pRtree = (Rtree *)(cur->pVtab);
195068 RtreeCursor *pCsr = (RtreeCursor *)cur;
195069 assert( pRtree->nCursor>0 );
195070 resetCursor(pCsr);
195071 sqlite3_finalize(pStmt: pCsr->pReadAux);
195072 sqlite3_free(p: pCsr);
195073 pRtree->nCursor--;
195074 nodeBlobReset(pRtree);
195075 return SQLITE_OK;
195076}
195077
195078/*
195079** Rtree virtual table module xEof method.
195080**
195081** Return non-zero if the cursor does not currently point to a valid
195082** record (i.e if the scan has finished), or zero otherwise.
195083*/
195084static int rtreeEof(sqlite3_vtab_cursor *cur){
195085 RtreeCursor *pCsr = (RtreeCursor *)cur;
195086 return pCsr->atEOF;
195087}
195088
195089/*
195090** Convert raw bits from the on-disk RTree record into a coordinate value.
195091** The on-disk format is big-endian and needs to be converted for little-
195092** endian platforms. The on-disk record stores integer coordinates if
195093** eInt is true and it stores 32-bit floating point records if eInt is
195094** false. a[] is the four bytes of the on-disk record to be decoded.
195095** Store the results in "r".
195096**
195097** There are five versions of this macro. The last one is generic. The
195098** other four are various architectures-specific optimizations.
195099*/
195100#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
195101#define RTREE_DECODE_COORD(eInt, a, r) { \
195102 RtreeCoord c; /* Coordinate decoded */ \
195103 c.u = _byteswap_ulong(*(u32*)a); \
195104 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
195105}
195106#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
195107#define RTREE_DECODE_COORD(eInt, a, r) { \
195108 RtreeCoord c; /* Coordinate decoded */ \
195109 c.u = __builtin_bswap32(*(u32*)a); \
195110 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
195111}
195112#elif SQLITE_BYTEORDER==1234
195113#define RTREE_DECODE_COORD(eInt, a, r) { \
195114 RtreeCoord c; /* Coordinate decoded */ \
195115 memcpy(&c.u,a,4); \
195116 c.u = ((c.u>>24)&0xff)|((c.u>>8)&0xff00)| \
195117 ((c.u&0xff)<<24)|((c.u&0xff00)<<8); \
195118 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
195119}
195120#elif SQLITE_BYTEORDER==4321
195121#define RTREE_DECODE_COORD(eInt, a, r) { \
195122 RtreeCoord c; /* Coordinate decoded */ \
195123 memcpy(&c.u,a,4); \
195124 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
195125}
195126#else
195127#define RTREE_DECODE_COORD(eInt, a, r) { \
195128 RtreeCoord c; /* Coordinate decoded */ \
195129 c.u = ((u32)a[0]<<24) + ((u32)a[1]<<16) \
195130 +((u32)a[2]<<8) + a[3]; \
195131 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
195132}
195133#endif
195134
195135/*
195136** Check the RTree node or entry given by pCellData and p against the MATCH
195137** constraint pConstraint.
195138*/
195139static int rtreeCallbackConstraint(
195140 RtreeConstraint *pConstraint, /* The constraint to test */
195141 int eInt, /* True if RTree holding integer coordinates */
195142 u8 *pCellData, /* Raw cell content */
195143 RtreeSearchPoint *pSearch, /* Container of this cell */
195144 sqlite3_rtree_dbl *prScore, /* OUT: score for the cell */
195145 int *peWithin /* OUT: visibility of the cell */
195146){
195147 sqlite3_rtree_query_info *pInfo = pConstraint->pInfo; /* Callback info */
195148 int nCoord = pInfo->nCoord; /* No. of coordinates */
195149 int rc; /* Callback return code */
195150 RtreeCoord c; /* Translator union */
195151 sqlite3_rtree_dbl aCoord[RTREE_MAX_DIMENSIONS*2]; /* Decoded coordinates */
195152
195153 assert( pConstraint->op==RTREE_MATCH || pConstraint->op==RTREE_QUERY );
195154 assert( nCoord==2 || nCoord==4 || nCoord==6 || nCoord==8 || nCoord==10 );
195155
195156 if( pConstraint->op==RTREE_QUERY && pSearch->iLevel==1 ){
195157 pInfo->iRowid = readInt64(p: pCellData);
195158 }
195159 pCellData += 8;
195160#ifndef SQLITE_RTREE_INT_ONLY
195161 if( eInt==0 ){
195162 switch( nCoord ){
195163 case 10: readCoord(p: pCellData+36, pCoord: &c); aCoord[9] = c.f;
195164 readCoord(p: pCellData+32, pCoord: &c); aCoord[8] = c.f;
195165 case 8: readCoord(p: pCellData+28, pCoord: &c); aCoord[7] = c.f;
195166 readCoord(p: pCellData+24, pCoord: &c); aCoord[6] = c.f;
195167 case 6: readCoord(p: pCellData+20, pCoord: &c); aCoord[5] = c.f;
195168 readCoord(p: pCellData+16, pCoord: &c); aCoord[4] = c.f;
195169 case 4: readCoord(p: pCellData+12, pCoord: &c); aCoord[3] = c.f;
195170 readCoord(p: pCellData+8, pCoord: &c); aCoord[2] = c.f;
195171 default: readCoord(p: pCellData+4, pCoord: &c); aCoord[1] = c.f;
195172 readCoord(p: pCellData, pCoord: &c); aCoord[0] = c.f;
195173 }
195174 }else
195175#endif
195176 {
195177 switch( nCoord ){
195178 case 10: readCoord(p: pCellData+36, pCoord: &c); aCoord[9] = c.i;
195179 readCoord(p: pCellData+32, pCoord: &c); aCoord[8] = c.i;
195180 case 8: readCoord(p: pCellData+28, pCoord: &c); aCoord[7] = c.i;
195181 readCoord(p: pCellData+24, pCoord: &c); aCoord[6] = c.i;
195182 case 6: readCoord(p: pCellData+20, pCoord: &c); aCoord[5] = c.i;
195183 readCoord(p: pCellData+16, pCoord: &c); aCoord[4] = c.i;
195184 case 4: readCoord(p: pCellData+12, pCoord: &c); aCoord[3] = c.i;
195185 readCoord(p: pCellData+8, pCoord: &c); aCoord[2] = c.i;
195186 default: readCoord(p: pCellData+4, pCoord: &c); aCoord[1] = c.i;
195187 readCoord(p: pCellData, pCoord: &c); aCoord[0] = c.i;
195188 }
195189 }
195190 if( pConstraint->op==RTREE_MATCH ){
195191 int eWithin = 0;
195192 rc = pConstraint->u.xGeom((sqlite3_rtree_geometry*)pInfo,
195193 nCoord, aCoord, &eWithin);
195194 if( eWithin==0 ) *peWithin = NOT_WITHIN;
195195 *prScore = RTREE_ZERO;
195196 }else{
195197 pInfo->aCoord = aCoord;
195198 pInfo->iLevel = pSearch->iLevel - 1;
195199 pInfo->rScore = pInfo->rParentScore = pSearch->rScore;
195200 pInfo->eWithin = pInfo->eParentWithin = pSearch->eWithin;
195201 rc = pConstraint->u.xQueryFunc(pInfo);
195202 if( pInfo->eWithin<*peWithin ) *peWithin = pInfo->eWithin;
195203 if( pInfo->rScore<*prScore || *prScore<RTREE_ZERO ){
195204 *prScore = pInfo->rScore;
195205 }
195206 }
195207 return rc;
195208}
195209
195210/*
195211** Check the internal RTree node given by pCellData against constraint p.
195212** If this constraint cannot be satisfied by any child within the node,
195213** set *peWithin to NOT_WITHIN.
195214*/
195215static void rtreeNonleafConstraint(
195216 RtreeConstraint *p, /* The constraint to test */
195217 int eInt, /* True if RTree holds integer coordinates */
195218 u8 *pCellData, /* Raw cell content as appears on disk */
195219 int *peWithin /* Adjust downward, as appropriate */
195220){
195221 sqlite3_rtree_dbl val; /* Coordinate value convert to a double */
195222
195223 /* p->iCoord might point to either a lower or upper bound coordinate
195224 ** in a coordinate pair. But make pCellData point to the lower bound.
195225 */
195226 pCellData += 8 + 4*(p->iCoord&0xfe);
195227
195228 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
195229 || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_TRUE
195230 || p->op==RTREE_FALSE );
195231 assert( ((((char*)pCellData) - (char*)0)&3)==0 ); /* 4-byte aligned */
195232 switch( p->op ){
195233 case RTREE_TRUE: return; /* Always satisfied */
195234 case RTREE_FALSE: break; /* Never satisfied */
195235 case RTREE_EQ:
195236 RTREE_DECODE_COORD(eInt, pCellData, val);
195237 /* val now holds the lower bound of the coordinate pair */
195238 if( p->u.rValue>=val ){
195239 pCellData += 4;
195240 RTREE_DECODE_COORD(eInt, pCellData, val);
195241 /* val now holds the upper bound of the coordinate pair */
195242 if( p->u.rValue<=val ) return;
195243 }
195244 break;
195245 case RTREE_LE:
195246 case RTREE_LT:
195247 RTREE_DECODE_COORD(eInt, pCellData, val);
195248 /* val now holds the lower bound of the coordinate pair */
195249 if( p->u.rValue>=val ) return;
195250 break;
195251
195252 default:
195253 pCellData += 4;
195254 RTREE_DECODE_COORD(eInt, pCellData, val);
195255 /* val now holds the upper bound of the coordinate pair */
195256 if( p->u.rValue<=val ) return;
195257 break;
195258 }
195259 *peWithin = NOT_WITHIN;
195260}
195261
195262/*
195263** Check the leaf RTree cell given by pCellData against constraint p.
195264** If this constraint is not satisfied, set *peWithin to NOT_WITHIN.
195265** If the constraint is satisfied, leave *peWithin unchanged.
195266**
195267** The constraint is of the form: xN op $val
195268**
195269** The op is given by p->op. The xN is p->iCoord-th coordinate in
195270** pCellData. $val is given by p->u.rValue.
195271*/
195272static void rtreeLeafConstraint(
195273 RtreeConstraint *p, /* The constraint to test */
195274 int eInt, /* True if RTree holds integer coordinates */
195275 u8 *pCellData, /* Raw cell content as appears on disk */
195276 int *peWithin /* Adjust downward, as appropriate */
195277){
195278 RtreeDValue xN; /* Coordinate value converted to a double */
195279
195280 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
195281 || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_TRUE
195282 || p->op==RTREE_FALSE );
195283 pCellData += 8 + p->iCoord*4;
195284 assert( ((((char*)pCellData) - (char*)0)&3)==0 ); /* 4-byte aligned */
195285 RTREE_DECODE_COORD(eInt, pCellData, xN);
195286 switch( p->op ){
195287 case RTREE_TRUE: return; /* Always satisfied */
195288 case RTREE_FALSE: break; /* Never satisfied */
195289 case RTREE_LE: if( xN <= p->u.rValue ) return; break;
195290 case RTREE_LT: if( xN < p->u.rValue ) return; break;
195291 case RTREE_GE: if( xN >= p->u.rValue ) return; break;
195292 case RTREE_GT: if( xN > p->u.rValue ) return; break;
195293 default: if( xN == p->u.rValue ) return; break;
195294 }
195295 *peWithin = NOT_WITHIN;
195296}
195297
195298/*
195299** One of the cells in node pNode is guaranteed to have a 64-bit
195300** integer value equal to iRowid. Return the index of this cell.
195301*/
195302static int nodeRowidIndex(
195303 Rtree *pRtree,
195304 RtreeNode *pNode,
195305 i64 iRowid,
195306 int *piIndex
195307){
195308 int ii;
195309 int nCell = NCELL(pNode);
195310 assert( nCell<200 );
195311 for(ii=0; ii<nCell; ii++){
195312 if( nodeGetRowid(pRtree, pNode, iCell: ii)==iRowid ){
195313 *piIndex = ii;
195314 return SQLITE_OK;
195315 }
195316 }
195317 RTREE_IS_CORRUPT(pRtree);
195318 return SQLITE_CORRUPT_VTAB;
195319}
195320
195321/*
195322** Return the index of the cell containing a pointer to node pNode
195323** in its parent. If pNode is the root node, return -1.
195324*/
195325static int nodeParentIndex(Rtree *pRtree, RtreeNode *pNode, int *piIndex){
195326 RtreeNode *pParent = pNode->pParent;
195327 if( ALWAYS(pParent) ){
195328 return nodeRowidIndex(pRtree, pNode: pParent, iRowid: pNode->iNode, piIndex);
195329 }else{
195330 *piIndex = -1;
195331 return SQLITE_OK;
195332 }
195333}
195334
195335/*
195336** Compare two search points. Return negative, zero, or positive if the first
195337** is less than, equal to, or greater than the second.
195338**
195339** The rScore is the primary key. Smaller rScore values come first.
195340** If the rScore is a tie, then use iLevel as the tie breaker with smaller
195341** iLevel values coming first. In this way, if rScore is the same for all
195342** SearchPoints, then iLevel becomes the deciding factor and the result
195343** is a depth-first search, which is the desired default behavior.
195344*/
195345static int rtreeSearchPointCompare(
195346 const RtreeSearchPoint *pA,
195347 const RtreeSearchPoint *pB
195348){
195349 if( pA->rScore<pB->rScore ) return -1;
195350 if( pA->rScore>pB->rScore ) return +1;
195351 if( pA->iLevel<pB->iLevel ) return -1;
195352 if( pA->iLevel>pB->iLevel ) return +1;
195353 return 0;
195354}
195355
195356/*
195357** Interchange two search points in a cursor.
195358*/
195359static void rtreeSearchPointSwap(RtreeCursor *p, int i, int j){
195360 RtreeSearchPoint t = p->aPoint[i];
195361 assert( i<j );
195362 p->aPoint[i] = p->aPoint[j];
195363 p->aPoint[j] = t;
195364 i++; j++;
195365 if( i<RTREE_CACHE_SZ ){
195366 if( j>=RTREE_CACHE_SZ ){
195367 nodeRelease(RTREE_OF_CURSOR(p), pNode: p->aNode[i]);
195368 p->aNode[i] = 0;
195369 }else{
195370 RtreeNode *pTemp = p->aNode[i];
195371 p->aNode[i] = p->aNode[j];
195372 p->aNode[j] = pTemp;
195373 }
195374 }
195375}
195376
195377/*
195378** Return the search point with the lowest current score.
195379*/
195380static RtreeSearchPoint *rtreeSearchPointFirst(RtreeCursor *pCur){
195381 return pCur->bPoint ? &pCur->sPoint : pCur->nPoint ? pCur->aPoint : 0;
195382}
195383
195384/*
195385** Get the RtreeNode for the search point with the lowest score.
195386*/
195387static RtreeNode *rtreeNodeOfFirstSearchPoint(RtreeCursor *pCur, int *pRC){
195388 sqlite3_int64 id;
195389 int ii = 1 - pCur->bPoint;
195390 assert( ii==0 || ii==1 );
195391 assert( pCur->bPoint || pCur->nPoint );
195392 if( pCur->aNode[ii]==0 ){
195393 assert( pRC!=0 );
195394 id = ii ? pCur->aPoint[0].id : pCur->sPoint.id;
195395 *pRC = nodeAcquire(RTREE_OF_CURSOR(pCur), iNode: id, pParent: 0, ppNode: &pCur->aNode[ii]);
195396 }
195397 return pCur->aNode[ii];
195398}
195399
195400/*
195401** Push a new element onto the priority queue
195402*/
195403static RtreeSearchPoint *rtreeEnqueue(
195404 RtreeCursor *pCur, /* The cursor */
195405 RtreeDValue rScore, /* Score for the new search point */
195406 u8 iLevel /* Level for the new search point */
195407){
195408 int i, j;
195409 RtreeSearchPoint *pNew;
195410 if( pCur->nPoint>=pCur->nPointAlloc ){
195411 int nNew = pCur->nPointAlloc*2 + 8;
195412 pNew = sqlite3_realloc64(pOld: pCur->aPoint, n: nNew*sizeof(pCur->aPoint[0]));
195413 if( pNew==0 ) return 0;
195414 pCur->aPoint = pNew;
195415 pCur->nPointAlloc = nNew;
195416 }
195417 i = pCur->nPoint++;
195418 pNew = pCur->aPoint + i;
195419 pNew->rScore = rScore;
195420 pNew->iLevel = iLevel;
195421 assert( iLevel<=RTREE_MAX_DEPTH );
195422 while( i>0 ){
195423 RtreeSearchPoint *pParent;
195424 j = (i-1)/2;
195425 pParent = pCur->aPoint + j;
195426 if( rtreeSearchPointCompare(pA: pNew, pB: pParent)>=0 ) break;
195427 rtreeSearchPointSwap(p: pCur, i: j, j: i);
195428 i = j;
195429 pNew = pParent;
195430 }
195431 return pNew;
195432}
195433
195434/*
195435** Allocate a new RtreeSearchPoint and return a pointer to it. Return
195436** NULL if malloc fails.
195437*/
195438static RtreeSearchPoint *rtreeSearchPointNew(
195439 RtreeCursor *pCur, /* The cursor */
195440 RtreeDValue rScore, /* Score for the new search point */
195441 u8 iLevel /* Level for the new search point */
195442){
195443 RtreeSearchPoint *pNew, *pFirst;
195444 pFirst = rtreeSearchPointFirst(pCur);
195445 pCur->anQueue[iLevel]++;
195446 if( pFirst==0
195447 || pFirst->rScore>rScore
195448 || (pFirst->rScore==rScore && pFirst->iLevel>iLevel)
195449 ){
195450 if( pCur->bPoint ){
195451 int ii;
195452 pNew = rtreeEnqueue(pCur, rScore, iLevel);
195453 if( pNew==0 ) return 0;
195454 ii = (int)(pNew - pCur->aPoint) + 1;
195455 assert( ii==1 );
195456 if( ALWAYS(ii<RTREE_CACHE_SZ) ){
195457 assert( pCur->aNode[ii]==0 );
195458 pCur->aNode[ii] = pCur->aNode[0];
195459 }else{
195460 nodeRelease(RTREE_OF_CURSOR(pCur), pNode: pCur->aNode[0]);
195461 }
195462 pCur->aNode[0] = 0;
195463 *pNew = pCur->sPoint;
195464 }
195465 pCur->sPoint.rScore = rScore;
195466 pCur->sPoint.iLevel = iLevel;
195467 pCur->bPoint = 1;
195468 return &pCur->sPoint;
195469 }else{
195470 return rtreeEnqueue(pCur, rScore, iLevel);
195471 }
195472}
195473
195474#if 0
195475/* Tracing routines for the RtreeSearchPoint queue */
195476static void tracePoint(RtreeSearchPoint *p, int idx, RtreeCursor *pCur){
195477 if( idx<0 ){ printf(" s"); }else{ printf("%2d", idx); }
195478 printf(" %d.%05lld.%02d %g %d",
195479 p->iLevel, p->id, p->iCell, p->rScore, p->eWithin
195480 );
195481 idx++;
195482 if( idx<RTREE_CACHE_SZ ){
195483 printf(" %p\n", pCur->aNode[idx]);
195484 }else{
195485 printf("\n");
195486 }
195487}
195488static void traceQueue(RtreeCursor *pCur, const char *zPrefix){
195489 int ii;
195490 printf("=== %9s ", zPrefix);
195491 if( pCur->bPoint ){
195492 tracePoint(&pCur->sPoint, -1, pCur);
195493 }
195494 for(ii=0; ii<pCur->nPoint; ii++){
195495 if( ii>0 || pCur->bPoint ) printf(" ");
195496 tracePoint(&pCur->aPoint[ii], ii, pCur);
195497 }
195498}
195499# define RTREE_QUEUE_TRACE(A,B) traceQueue(A,B)
195500#else
195501# define RTREE_QUEUE_TRACE(A,B) /* no-op */
195502#endif
195503
195504/* Remove the search point with the lowest current score.
195505*/
195506static void rtreeSearchPointPop(RtreeCursor *p){
195507 int i, j, k, n;
195508 i = 1 - p->bPoint;
195509 assert( i==0 || i==1 );
195510 if( p->aNode[i] ){
195511 nodeRelease(RTREE_OF_CURSOR(p), pNode: p->aNode[i]);
195512 p->aNode[i] = 0;
195513 }
195514 if( p->bPoint ){
195515 p->anQueue[p->sPoint.iLevel]--;
195516 p->bPoint = 0;
195517 }else if( ALWAYS(p->nPoint) ){
195518 p->anQueue[p->aPoint[0].iLevel]--;
195519 n = --p->nPoint;
195520 p->aPoint[0] = p->aPoint[n];
195521 if( n<RTREE_CACHE_SZ-1 ){
195522 p->aNode[1] = p->aNode[n+1];
195523 p->aNode[n+1] = 0;
195524 }
195525 i = 0;
195526 while( (j = i*2+1)<n ){
195527 k = j+1;
195528 if( k<n && rtreeSearchPointCompare(pA: &p->aPoint[k], pB: &p->aPoint[j])<0 ){
195529 if( rtreeSearchPointCompare(pA: &p->aPoint[k], pB: &p->aPoint[i])<0 ){
195530 rtreeSearchPointSwap(p, i, j: k);
195531 i = k;
195532 }else{
195533 break;
195534 }
195535 }else{
195536 if( rtreeSearchPointCompare(pA: &p->aPoint[j], pB: &p->aPoint[i])<0 ){
195537 rtreeSearchPointSwap(p, i, j);
195538 i = j;
195539 }else{
195540 break;
195541 }
195542 }
195543 }
195544 }
195545}
195546
195547
195548/*
195549** Continue the search on cursor pCur until the front of the queue
195550** contains an entry suitable for returning as a result-set row,
195551** or until the RtreeSearchPoint queue is empty, indicating that the
195552** query has completed.
195553*/
195554static int rtreeStepToLeaf(RtreeCursor *pCur){
195555 RtreeSearchPoint *p;
195556 Rtree *pRtree = RTREE_OF_CURSOR(pCur);
195557 RtreeNode *pNode;
195558 int eWithin;
195559 int rc = SQLITE_OK;
195560 int nCell;
195561 int nConstraint = pCur->nConstraint;
195562 int ii;
195563 int eInt;
195564 RtreeSearchPoint x;
195565
195566 eInt = pRtree->eCoordType==RTREE_COORD_INT32;
195567 while( (p = rtreeSearchPointFirst(pCur))!=0 && p->iLevel>0 ){
195568 u8 *pCellData;
195569 pNode = rtreeNodeOfFirstSearchPoint(pCur, pRC: &rc);
195570 if( rc ) return rc;
195571 nCell = NCELL(pNode);
195572 assert( nCell<200 );
195573 pCellData = pNode->zData + (4+pRtree->nBytesPerCell*p->iCell);
195574 while( p->iCell<nCell ){
195575 sqlite3_rtree_dbl rScore = (sqlite3_rtree_dbl)-1;
195576 eWithin = FULLY_WITHIN;
195577 for(ii=0; ii<nConstraint; ii++){
195578 RtreeConstraint *pConstraint = pCur->aConstraint + ii;
195579 if( pConstraint->op>=RTREE_MATCH ){
195580 rc = rtreeCallbackConstraint(pConstraint, eInt, pCellData, pSearch: p,
195581 prScore: &rScore, peWithin: &eWithin);
195582 if( rc ) return rc;
195583 }else if( p->iLevel==1 ){
195584 rtreeLeafConstraint(p: pConstraint, eInt, pCellData, peWithin: &eWithin);
195585 }else{
195586 rtreeNonleafConstraint(p: pConstraint, eInt, pCellData, peWithin: &eWithin);
195587 }
195588 if( eWithin==NOT_WITHIN ){
195589 p->iCell++;
195590 pCellData += pRtree->nBytesPerCell;
195591 break;
195592 }
195593 }
195594 if( eWithin==NOT_WITHIN ) continue;
195595 p->iCell++;
195596 x.iLevel = p->iLevel - 1;
195597 if( x.iLevel ){
195598 x.id = readInt64(p: pCellData);
195599 for(ii=0; ii<pCur->nPoint; ii++){
195600 if( pCur->aPoint[ii].id==x.id ){
195601 RTREE_IS_CORRUPT(pRtree);
195602 return SQLITE_CORRUPT_VTAB;
195603 }
195604 }
195605 x.iCell = 0;
195606 }else{
195607 x.id = p->id;
195608 x.iCell = p->iCell - 1;
195609 }
195610 if( p->iCell>=nCell ){
195611 RTREE_QUEUE_TRACE(pCur, "POP-S:");
195612 rtreeSearchPointPop(p: pCur);
195613 }
195614 if( rScore<RTREE_ZERO ) rScore = RTREE_ZERO;
195615 p = rtreeSearchPointNew(pCur, rScore, iLevel: x.iLevel);
195616 if( p==0 ) return SQLITE_NOMEM;
195617 p->eWithin = (u8)eWithin;
195618 p->id = x.id;
195619 p->iCell = x.iCell;
195620 RTREE_QUEUE_TRACE(pCur, "PUSH-S:");
195621 break;
195622 }
195623 if( p->iCell>=nCell ){
195624 RTREE_QUEUE_TRACE(pCur, "POP-Se:");
195625 rtreeSearchPointPop(p: pCur);
195626 }
195627 }
195628 pCur->atEOF = p==0;
195629 return SQLITE_OK;
195630}
195631
195632/*
195633** Rtree virtual table module xNext method.
195634*/
195635static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){
195636 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
195637 int rc = SQLITE_OK;
195638
195639 /* Move to the next entry that matches the configured constraints. */
195640 RTREE_QUEUE_TRACE(pCsr, "POP-Nx:");
195641 if( pCsr->bAuxValid ){
195642 pCsr->bAuxValid = 0;
195643 sqlite3_reset(pStmt: pCsr->pReadAux);
195644 }
195645 rtreeSearchPointPop(p: pCsr);
195646 rc = rtreeStepToLeaf(pCur: pCsr);
195647 return rc;
195648}
195649
195650/*
195651** Rtree virtual table module xRowid method.
195652*/
195653static int rtreeRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
195654 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
195655 RtreeSearchPoint *p = rtreeSearchPointFirst(pCur: pCsr);
195656 int rc = SQLITE_OK;
195657 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCur: pCsr, pRC: &rc);
195658 if( rc==SQLITE_OK && ALWAYS(p) ){
195659 *pRowid = nodeGetRowid(RTREE_OF_CURSOR(pCsr), pNode, iCell: p->iCell);
195660 }
195661 return rc;
195662}
195663
195664/*
195665** Rtree virtual table module xColumn method.
195666*/
195667static int rtreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
195668 Rtree *pRtree = (Rtree *)cur->pVtab;
195669 RtreeCursor *pCsr = (RtreeCursor *)cur;
195670 RtreeSearchPoint *p = rtreeSearchPointFirst(pCur: pCsr);
195671 RtreeCoord c;
195672 int rc = SQLITE_OK;
195673 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCur: pCsr, pRC: &rc);
195674
195675 if( rc ) return rc;
195676 if( NEVER(p==0) ) return SQLITE_OK;
195677 if( i==0 ){
195678 sqlite3_result_int64(pCtx: ctx, iVal: nodeGetRowid(pRtree, pNode, iCell: p->iCell));
195679 }else if( i<=pRtree->nDim2 ){
195680 nodeGetCoord(pRtree, pNode, iCell: p->iCell, iCoord: i-1, pCoord: &c);
195681#ifndef SQLITE_RTREE_INT_ONLY
195682 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
195683 sqlite3_result_double(pCtx: ctx, rVal: c.f);
195684 }else
195685#endif
195686 {
195687 assert( pRtree->eCoordType==RTREE_COORD_INT32 );
195688 sqlite3_result_int(pCtx: ctx, iVal: c.i);
195689 }
195690 }else{
195691 if( !pCsr->bAuxValid ){
195692 if( pCsr->pReadAux==0 ){
195693 rc = sqlite3_prepare_v3(db: pRtree->db, zSql: pRtree->zReadAuxSql, nBytes: -1, prepFlags: 0,
195694 ppStmt: &pCsr->pReadAux, pzTail: 0);
195695 if( rc ) return rc;
195696 }
195697 sqlite3_bind_int64(pStmt: pCsr->pReadAux, i: 1,
195698 iValue: nodeGetRowid(pRtree, pNode, iCell: p->iCell));
195699 rc = sqlite3_step(pStmt: pCsr->pReadAux);
195700 if( rc==SQLITE_ROW ){
195701 pCsr->bAuxValid = 1;
195702 }else{
195703 sqlite3_reset(pStmt: pCsr->pReadAux);
195704 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
195705 return rc;
195706 }
195707 }
195708 sqlite3_result_value(pCtx: ctx,
195709 pValue: sqlite3_column_value(pStmt: pCsr->pReadAux, i: i - pRtree->nDim2 + 1));
195710 }
195711 return SQLITE_OK;
195712}
195713
195714/*
195715** Use nodeAcquire() to obtain the leaf node containing the record with
195716** rowid iRowid. If successful, set *ppLeaf to point to the node and
195717** return SQLITE_OK. If there is no such record in the table, set
195718** *ppLeaf to 0 and return SQLITE_OK. If an error occurs, set *ppLeaf
195719** to zero and return an SQLite error code.
195720*/
195721static int findLeafNode(
195722 Rtree *pRtree, /* RTree to search */
195723 i64 iRowid, /* The rowid searching for */
195724 RtreeNode **ppLeaf, /* Write the node here */
195725 sqlite3_int64 *piNode /* Write the node-id here */
195726){
195727 int rc;
195728 *ppLeaf = 0;
195729 sqlite3_bind_int64(pStmt: pRtree->pReadRowid, i: 1, iValue: iRowid);
195730 if( sqlite3_step(pStmt: pRtree->pReadRowid)==SQLITE_ROW ){
195731 i64 iNode = sqlite3_column_int64(pStmt: pRtree->pReadRowid, i: 0);
195732 if( piNode ) *piNode = iNode;
195733 rc = nodeAcquire(pRtree, iNode, pParent: 0, ppNode: ppLeaf);
195734 sqlite3_reset(pStmt: pRtree->pReadRowid);
195735 }else{
195736 rc = sqlite3_reset(pStmt: pRtree->pReadRowid);
195737 }
195738 return rc;
195739}
195740
195741/*
195742** This function is called to configure the RtreeConstraint object passed
195743** as the second argument for a MATCH constraint. The value passed as the
195744** first argument to this function is the right-hand operand to the MATCH
195745** operator.
195746*/
195747static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){
195748 RtreeMatchArg *pBlob, *pSrc; /* BLOB returned by geometry function */
195749 sqlite3_rtree_query_info *pInfo; /* Callback information */
195750
195751 pSrc = sqlite3_value_pointer(pVal: pValue, zPType: "RtreeMatchArg");
195752 if( pSrc==0 ) return SQLITE_ERROR;
195753 pInfo = (sqlite3_rtree_query_info*)
195754 sqlite3_malloc64( n: sizeof(*pInfo)+pSrc->iSize );
195755 if( !pInfo ) return SQLITE_NOMEM;
195756 memset(s: pInfo, c: 0, n: sizeof(*pInfo));
195757 pBlob = (RtreeMatchArg*)&pInfo[1];
195758 memcpy(dest: pBlob, src: pSrc, n: pSrc->iSize);
195759 pInfo->pContext = pBlob->cb.pContext;
195760 pInfo->nParam = pBlob->nParam;
195761 pInfo->aParam = pBlob->aParam;
195762 pInfo->apSqlParam = pBlob->apSqlParam;
195763
195764 if( pBlob->cb.xGeom ){
195765 pCons->u.xGeom = pBlob->cb.xGeom;
195766 }else{
195767 pCons->op = RTREE_QUERY;
195768 pCons->u.xQueryFunc = pBlob->cb.xQueryFunc;
195769 }
195770 pCons->pInfo = pInfo;
195771 return SQLITE_OK;
195772}
195773
195774/*
195775** Rtree virtual table module xFilter method.
195776*/
195777static int rtreeFilter(
195778 sqlite3_vtab_cursor *pVtabCursor,
195779 int idxNum, const char *idxStr,
195780 int argc, sqlite3_value **argv
195781){
195782 Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
195783 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
195784 RtreeNode *pRoot = 0;
195785 int ii;
195786 int rc = SQLITE_OK;
195787 int iCell = 0;
195788
195789 rtreeReference(pRtree);
195790
195791 /* Reset the cursor to the same state as rtreeOpen() leaves it in. */
195792 resetCursor(pCsr);
195793
195794 pCsr->iStrategy = idxNum;
195795 if( idxNum==1 ){
195796 /* Special case - lookup by rowid. */
195797 RtreeNode *pLeaf; /* Leaf on which the required cell resides */
195798 RtreeSearchPoint *p; /* Search point for the leaf */
195799 i64 iRowid = sqlite3_value_int64(pVal: argv[0]);
195800 i64 iNode = 0;
195801 int eType = sqlite3_value_numeric_type(pVal: argv[0]);
195802 if( eType==SQLITE_INTEGER
195803 || (eType==SQLITE_FLOAT && sqlite3_value_double(pVal: argv[0])==iRowid)
195804 ){
195805 rc = findLeafNode(pRtree, iRowid, ppLeaf: &pLeaf, piNode: &iNode);
195806 }else{
195807 rc = SQLITE_OK;
195808 pLeaf = 0;
195809 }
195810 if( rc==SQLITE_OK && pLeaf!=0 ){
195811 p = rtreeSearchPointNew(pCur: pCsr, RTREE_ZERO, iLevel: 0);
195812 assert( p!=0 ); /* Always returns pCsr->sPoint */
195813 pCsr->aNode[0] = pLeaf;
195814 p->id = iNode;
195815 p->eWithin = PARTLY_WITHIN;
195816 rc = nodeRowidIndex(pRtree, pNode: pLeaf, iRowid, piIndex: &iCell);
195817 p->iCell = (u8)iCell;
195818 RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
195819 }else{
195820 pCsr->atEOF = 1;
195821 }
195822 }else{
195823 /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
195824 ** with the configured constraints.
195825 */
195826 rc = nodeAcquire(pRtree, iNode: 1, pParent: 0, ppNode: &pRoot);
195827 if( rc==SQLITE_OK && argc>0 ){
195828 pCsr->aConstraint = sqlite3_malloc64(n: sizeof(RtreeConstraint)*argc);
195829 pCsr->nConstraint = argc;
195830 if( !pCsr->aConstraint ){
195831 rc = SQLITE_NOMEM;
195832 }else{
195833 memset(s: pCsr->aConstraint, c: 0, n: sizeof(RtreeConstraint)*argc);
195834 memset(s: pCsr->anQueue, c: 0, n: sizeof(u32)*(pRtree->iDepth + 1));
195835 assert( (idxStr==0 && argc==0)
195836 || (idxStr && (int)strlen(idxStr)==argc*2) );
195837 for(ii=0; ii<argc; ii++){
195838 RtreeConstraint *p = &pCsr->aConstraint[ii];
195839 int eType = sqlite3_value_numeric_type(pVal: argv[ii]);
195840 p->op = idxStr[ii*2];
195841 p->iCoord = idxStr[ii*2+1]-'0';
195842 if( p->op>=RTREE_MATCH ){
195843 /* A MATCH operator. The right-hand-side must be a blob that
195844 ** can be cast into an RtreeMatchArg object. One created using
195845 ** an sqlite3_rtree_geometry_callback() SQL user function.
195846 */
195847 rc = deserializeGeometry(pValue: argv[ii], pCons: p);
195848 if( rc!=SQLITE_OK ){
195849 break;
195850 }
195851 p->pInfo->nCoord = pRtree->nDim2;
195852 p->pInfo->anQueue = pCsr->anQueue;
195853 p->pInfo->mxLevel = pRtree->iDepth + 1;
195854 }else if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
195855#ifdef SQLITE_RTREE_INT_ONLY
195856 p->u.rValue = sqlite3_value_int64(argv[ii]);
195857#else
195858 p->u.rValue = sqlite3_value_double(pVal: argv[ii]);
195859#endif
195860 }else{
195861 p->u.rValue = RTREE_ZERO;
195862 if( eType==SQLITE_NULL ){
195863 p->op = RTREE_FALSE;
195864 }else if( p->op==RTREE_LT || p->op==RTREE_LE ){
195865 p->op = RTREE_TRUE;
195866 }else{
195867 p->op = RTREE_FALSE;
195868 }
195869 }
195870 }
195871 }
195872 }
195873 if( rc==SQLITE_OK ){
195874 RtreeSearchPoint *pNew;
195875 assert( pCsr->bPoint==0 ); /* Due to the resetCursor() call above */
195876 pNew = rtreeSearchPointNew(pCur: pCsr, RTREE_ZERO, iLevel: (u8)(pRtree->iDepth+1));
195877 if( NEVER(pNew==0) ){ /* Because pCsr->bPoint was FALSE */
195878 return SQLITE_NOMEM;
195879 }
195880 pNew->id = 1;
195881 pNew->iCell = 0;
195882 pNew->eWithin = PARTLY_WITHIN;
195883 assert( pCsr->bPoint==1 );
195884 pCsr->aNode[0] = pRoot;
195885 pRoot = 0;
195886 RTREE_QUEUE_TRACE(pCsr, "PUSH-Fm:");
195887 rc = rtreeStepToLeaf(pCur: pCsr);
195888 }
195889 }
195890
195891 nodeRelease(pRtree, pNode: pRoot);
195892 rtreeRelease(pRtree);
195893 return rc;
195894}
195895
195896/*
195897** Rtree virtual table module xBestIndex method. There are three
195898** table scan strategies to choose from (in order from most to
195899** least desirable):
195900**
195901** idxNum idxStr Strategy
195902** ------------------------------------------------
195903** 1 Unused Direct lookup by rowid.
195904** 2 See below R-tree query or full-table scan.
195905** ------------------------------------------------
195906**
195907** If strategy 1 is used, then idxStr is not meaningful. If strategy
195908** 2 is used, idxStr is formatted to contain 2 bytes for each
195909** constraint used. The first two bytes of idxStr correspond to
195910** the constraint in sqlite3_index_info.aConstraintUsage[] with
195911** (argvIndex==1) etc.
195912**
195913** The first of each pair of bytes in idxStr identifies the constraint
195914** operator as follows:
195915**
195916** Operator Byte Value
195917** ----------------------
195918** = 0x41 ('A')
195919** <= 0x42 ('B')
195920** < 0x43 ('C')
195921** >= 0x44 ('D')
195922** > 0x45 ('E')
195923** MATCH 0x46 ('F')
195924** ----------------------
195925**
195926** The second of each pair of bytes identifies the coordinate column
195927** to which the constraint applies. The leftmost coordinate column
195928** is 'a', the second from the left 'b' etc.
195929*/
195930static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
195931 Rtree *pRtree = (Rtree*)tab;
195932 int rc = SQLITE_OK;
195933 int ii;
195934 int bMatch = 0; /* True if there exists a MATCH constraint */
195935 i64 nRow; /* Estimated rows returned by this scan */
195936
195937 int iIdx = 0;
195938 char zIdxStr[RTREE_MAX_DIMENSIONS*8+1];
195939 memset(s: zIdxStr, c: 0, n: sizeof(zIdxStr));
195940
195941 /* Check if there exists a MATCH constraint - even an unusable one. If there
195942 ** is, do not consider the lookup-by-rowid plan as using such a plan would
195943 ** require the VDBE to evaluate the MATCH constraint, which is not currently
195944 ** possible. */
195945 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
195946 if( pIdxInfo->aConstraint[ii].op==SQLITE_INDEX_CONSTRAINT_MATCH ){
195947 bMatch = 1;
195948 }
195949 }
195950
195951 assert( pIdxInfo->idxStr==0 );
195952 for(ii=0; ii<pIdxInfo->nConstraint && iIdx<(int)(sizeof(zIdxStr)-1); ii++){
195953 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
195954
195955 if( bMatch==0 && p->usable
195956 && p->iColumn<=0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ
195957 ){
195958 /* We have an equality constraint on the rowid. Use strategy 1. */
195959 int jj;
195960 for(jj=0; jj<ii; jj++){
195961 pIdxInfo->aConstraintUsage[jj].argvIndex = 0;
195962 pIdxInfo->aConstraintUsage[jj].omit = 0;
195963 }
195964 pIdxInfo->idxNum = 1;
195965 pIdxInfo->aConstraintUsage[ii].argvIndex = 1;
195966 pIdxInfo->aConstraintUsage[jj].omit = 1;
195967
195968 /* This strategy involves a two rowid lookups on an B-Tree structures
195969 ** and then a linear search of an R-Tree node. This should be
195970 ** considered almost as quick as a direct rowid lookup (for which
195971 ** sqlite uses an internal cost of 0.0). It is expected to return
195972 ** a single row.
195973 */
195974 pIdxInfo->estimatedCost = 30.0;
195975 pIdxInfo->estimatedRows = 1;
195976 pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE;
195977 return SQLITE_OK;
195978 }
195979
195980 if( p->usable
195981 && ((p->iColumn>0 && p->iColumn<=pRtree->nDim2)
195982 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH)
195983 ){
195984 u8 op;
195985 switch( p->op ){
195986 case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; break;
195987 case SQLITE_INDEX_CONSTRAINT_GT: op = RTREE_GT; break;
195988 case SQLITE_INDEX_CONSTRAINT_LE: op = RTREE_LE; break;
195989 case SQLITE_INDEX_CONSTRAINT_LT: op = RTREE_LT; break;
195990 case SQLITE_INDEX_CONSTRAINT_GE: op = RTREE_GE; break;
195991 case SQLITE_INDEX_CONSTRAINT_MATCH: op = RTREE_MATCH; break;
195992 default: op = 0; break;
195993 }
195994 if( op ){
195995 zIdxStr[iIdx++] = op;
195996 zIdxStr[iIdx++] = (char)(p->iColumn - 1 + '0');
195997 pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
195998 pIdxInfo->aConstraintUsage[ii].omit = 1;
195999 }
196000 }
196001 }
196002
196003 pIdxInfo->idxNum = 2;
196004 pIdxInfo->needToFreeIdxStr = 1;
196005 if( iIdx>0 && 0==(pIdxInfo->idxStr = sqlite3_mprintf(zFormat: "%s", zIdxStr)) ){
196006 return SQLITE_NOMEM;
196007 }
196008
196009 nRow = pRtree->nRowEst >> (iIdx/2);
196010 pIdxInfo->estimatedCost = (double)6.0 * (double)nRow;
196011 pIdxInfo->estimatedRows = nRow;
196012
196013 return rc;
196014}
196015
196016/*
196017** Return the N-dimensional volumn of the cell stored in *p.
196018*/
196019static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
196020 RtreeDValue area = (RtreeDValue)1;
196021 assert( pRtree->nDim>=1 && pRtree->nDim<=5 );
196022#ifndef SQLITE_RTREE_INT_ONLY
196023 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
196024 switch( pRtree->nDim ){
196025 case 5: area = p->aCoord[9].f - p->aCoord[8].f;
196026 case 4: area *= p->aCoord[7].f - p->aCoord[6].f;
196027 case 3: area *= p->aCoord[5].f - p->aCoord[4].f;
196028 case 2: area *= p->aCoord[3].f - p->aCoord[2].f;
196029 default: area *= p->aCoord[1].f - p->aCoord[0].f;
196030 }
196031 }else
196032#endif
196033 {
196034 switch( pRtree->nDim ){
196035 case 5: area = (i64)p->aCoord[9].i - (i64)p->aCoord[8].i;
196036 case 4: area *= (i64)p->aCoord[7].i - (i64)p->aCoord[6].i;
196037 case 3: area *= (i64)p->aCoord[5].i - (i64)p->aCoord[4].i;
196038 case 2: area *= (i64)p->aCoord[3].i - (i64)p->aCoord[2].i;
196039 default: area *= (i64)p->aCoord[1].i - (i64)p->aCoord[0].i;
196040 }
196041 }
196042 return area;
196043}
196044
196045/*
196046** Return the margin length of cell p. The margin length is the sum
196047** of the objects size in each dimension.
196048*/
196049static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){
196050 RtreeDValue margin = 0;
196051 int ii = pRtree->nDim2 - 2;
196052 do{
196053 margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
196054 ii -= 2;
196055 }while( ii>=0 );
196056 return margin;
196057}
196058
196059/*
196060** Store the union of cells p1 and p2 in p1.
196061*/
196062static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
196063 int ii = 0;
196064 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
196065 do{
196066 p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f);
196067 p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f);
196068 ii += 2;
196069 }while( ii<pRtree->nDim2 );
196070 }else{
196071 do{
196072 p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i);
196073 p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i);
196074 ii += 2;
196075 }while( ii<pRtree->nDim2 );
196076 }
196077}
196078
196079/*
196080** Return true if the area covered by p2 is a subset of the area covered
196081** by p1. False otherwise.
196082*/
196083static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
196084 int ii;
196085 int isInt = (pRtree->eCoordType==RTREE_COORD_INT32);
196086 for(ii=0; ii<pRtree->nDim2; ii+=2){
196087 RtreeCoord *a1 = &p1->aCoord[ii];
196088 RtreeCoord *a2 = &p2->aCoord[ii];
196089 if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f))
196090 || ( isInt && (a2[0].i<a1[0].i || a2[1].i>a1[1].i))
196091 ){
196092 return 0;
196093 }
196094 }
196095 return 1;
196096}
196097
196098/*
196099** Return the amount cell p would grow by if it were unioned with pCell.
196100*/
196101static RtreeDValue cellGrowth(Rtree *pRtree, RtreeCell *p, RtreeCell *pCell){
196102 RtreeDValue area;
196103 RtreeCell cell;
196104 memcpy(dest: &cell, src: p, n: sizeof(RtreeCell));
196105 area = cellArea(pRtree, p: &cell);
196106 cellUnion(pRtree, p1: &cell, p2: pCell);
196107 return (cellArea(pRtree, p: &cell)-area);
196108}
196109
196110static RtreeDValue cellOverlap(
196111 Rtree *pRtree,
196112 RtreeCell *p,
196113 RtreeCell *aCell,
196114 int nCell
196115){
196116 int ii;
196117 RtreeDValue overlap = RTREE_ZERO;
196118 for(ii=0; ii<nCell; ii++){
196119 int jj;
196120 RtreeDValue o = (RtreeDValue)1;
196121 for(jj=0; jj<pRtree->nDim2; jj+=2){
196122 RtreeDValue x1, x2;
196123 x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
196124 x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
196125 if( x2<x1 ){
196126 o = (RtreeDValue)0;
196127 break;
196128 }else{
196129 o = o * (x2-x1);
196130 }
196131 }
196132 overlap += o;
196133 }
196134 return overlap;
196135}
196136
196137
196138/*
196139** This function implements the ChooseLeaf algorithm from Gutman[84].
196140** ChooseSubTree in r*tree terminology.
196141*/
196142static int ChooseLeaf(
196143 Rtree *pRtree, /* Rtree table */
196144 RtreeCell *pCell, /* Cell to insert into rtree */
196145 int iHeight, /* Height of sub-tree rooted at pCell */
196146 RtreeNode **ppLeaf /* OUT: Selected leaf page */
196147){
196148 int rc;
196149 int ii;
196150 RtreeNode *pNode = 0;
196151 rc = nodeAcquire(pRtree, iNode: 1, pParent: 0, ppNode: &pNode);
196152
196153 for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){
196154 int iCell;
196155 sqlite3_int64 iBest = 0;
196156
196157 RtreeDValue fMinGrowth = RTREE_ZERO;
196158 RtreeDValue fMinArea = RTREE_ZERO;
196159
196160 int nCell = NCELL(pNode);
196161 RtreeCell cell;
196162 RtreeNode *pChild = 0;
196163
196164 RtreeCell *aCell = 0;
196165
196166 /* Select the child node which will be enlarged the least if pCell
196167 ** is inserted into it. Resolve ties by choosing the entry with
196168 ** the smallest area.
196169 */
196170 for(iCell=0; iCell<nCell; iCell++){
196171 int bBest = 0;
196172 RtreeDValue growth;
196173 RtreeDValue area;
196174 nodeGetCell(pRtree, pNode, iCell, pCell: &cell);
196175 growth = cellGrowth(pRtree, p: &cell, pCell);
196176 area = cellArea(pRtree, p: &cell);
196177 if( iCell==0||growth<fMinGrowth||(growth==fMinGrowth && area<fMinArea) ){
196178 bBest = 1;
196179 }
196180 if( bBest ){
196181 fMinGrowth = growth;
196182 fMinArea = area;
196183 iBest = cell.iRowid;
196184 }
196185 }
196186
196187 sqlite3_free(p: aCell);
196188 rc = nodeAcquire(pRtree, iNode: iBest, pParent: pNode, ppNode: &pChild);
196189 nodeRelease(pRtree, pNode);
196190 pNode = pChild;
196191 }
196192
196193 *ppLeaf = pNode;
196194 return rc;
196195}
196196
196197/*
196198** A cell with the same content as pCell has just been inserted into
196199** the node pNode. This function updates the bounding box cells in
196200** all ancestor elements.
196201*/
196202static int AdjustTree(
196203 Rtree *pRtree, /* Rtree table */
196204 RtreeNode *pNode, /* Adjust ancestry of this node. */
196205 RtreeCell *pCell /* This cell was just inserted */
196206){
196207 RtreeNode *p = pNode;
196208 int cnt = 0;
196209 int rc;
196210 while( p->pParent ){
196211 RtreeNode *pParent = p->pParent;
196212 RtreeCell cell;
196213 int iCell;
196214
196215 cnt++;
196216 if( NEVER(cnt>100) ){
196217 RTREE_IS_CORRUPT(pRtree);
196218 return SQLITE_CORRUPT_VTAB;
196219 }
196220 rc = nodeParentIndex(pRtree, pNode: p, piIndex: &iCell);
196221 if( NEVER(rc!=SQLITE_OK) ){
196222 RTREE_IS_CORRUPT(pRtree);
196223 return SQLITE_CORRUPT_VTAB;
196224 }
196225
196226 nodeGetCell(pRtree, pNode: pParent, iCell, pCell: &cell);
196227 if( !cellContains(pRtree, p1: &cell, p2: pCell) ){
196228 cellUnion(pRtree, p1: &cell, p2: pCell);
196229 nodeOverwriteCell(pRtree, pNode: pParent, pCell: &cell, iCell);
196230 }
196231
196232 p = pParent;
196233 }
196234 return SQLITE_OK;
196235}
196236
196237/*
196238** Write mapping (iRowid->iNode) to the <rtree>_rowid table.
196239*/
196240static int rowidWrite(Rtree *pRtree, sqlite3_int64 iRowid, sqlite3_int64 iNode){
196241 sqlite3_bind_int64(pStmt: pRtree->pWriteRowid, i: 1, iValue: iRowid);
196242 sqlite3_bind_int64(pStmt: pRtree->pWriteRowid, i: 2, iValue: iNode);
196243 sqlite3_step(pStmt: pRtree->pWriteRowid);
196244 return sqlite3_reset(pStmt: pRtree->pWriteRowid);
196245}
196246
196247/*
196248** Write mapping (iNode->iPar) to the <rtree>_parent table.
196249*/
196250static int parentWrite(Rtree *pRtree, sqlite3_int64 iNode, sqlite3_int64 iPar){
196251 sqlite3_bind_int64(pStmt: pRtree->pWriteParent, i: 1, iValue: iNode);
196252 sqlite3_bind_int64(pStmt: pRtree->pWriteParent, i: 2, iValue: iPar);
196253 sqlite3_step(pStmt: pRtree->pWriteParent);
196254 return sqlite3_reset(pStmt: pRtree->pWriteParent);
196255}
196256
196257static int rtreeInsertCell(Rtree *, RtreeNode *, RtreeCell *, int);
196258
196259
196260/*
196261** Arguments aIdx, aDistance and aSpare all point to arrays of size
196262** nIdx. The aIdx array contains the set of integers from 0 to
196263** (nIdx-1) in no particular order. This function sorts the values
196264** in aIdx according to the indexed values in aDistance. For
196265** example, assuming the inputs:
196266**
196267** aIdx = { 0, 1, 2, 3 }
196268** aDistance = { 5.0, 2.0, 7.0, 6.0 }
196269**
196270** this function sets the aIdx array to contain:
196271**
196272** aIdx = { 0, 1, 2, 3 }
196273**
196274** The aSpare array is used as temporary working space by the
196275** sorting algorithm.
196276*/
196277static void SortByDistance(
196278 int *aIdx,
196279 int nIdx,
196280 RtreeDValue *aDistance,
196281 int *aSpare
196282){
196283 if( nIdx>1 ){
196284 int iLeft = 0;
196285 int iRight = 0;
196286
196287 int nLeft = nIdx/2;
196288 int nRight = nIdx-nLeft;
196289 int *aLeft = aIdx;
196290 int *aRight = &aIdx[nLeft];
196291
196292 SortByDistance(aIdx: aLeft, nIdx: nLeft, aDistance, aSpare);
196293 SortByDistance(aIdx: aRight, nIdx: nRight, aDistance, aSpare);
196294
196295 memcpy(dest: aSpare, src: aLeft, n: sizeof(int)*nLeft);
196296 aLeft = aSpare;
196297
196298 while( iLeft<nLeft || iRight<nRight ){
196299 if( iLeft==nLeft ){
196300 aIdx[iLeft+iRight] = aRight[iRight];
196301 iRight++;
196302 }else if( iRight==nRight ){
196303 aIdx[iLeft+iRight] = aLeft[iLeft];
196304 iLeft++;
196305 }else{
196306 RtreeDValue fLeft = aDistance[aLeft[iLeft]];
196307 RtreeDValue fRight = aDistance[aRight[iRight]];
196308 if( fLeft<fRight ){
196309 aIdx[iLeft+iRight] = aLeft[iLeft];
196310 iLeft++;
196311 }else{
196312 aIdx[iLeft+iRight] = aRight[iRight];
196313 iRight++;
196314 }
196315 }
196316 }
196317
196318#if 0
196319 /* Check that the sort worked */
196320 {
196321 int jj;
196322 for(jj=1; jj<nIdx; jj++){
196323 RtreeDValue left = aDistance[aIdx[jj-1]];
196324 RtreeDValue right = aDistance[aIdx[jj]];
196325 assert( left<=right );
196326 }
196327 }
196328#endif
196329 }
196330}
196331
196332/*
196333** Arguments aIdx, aCell and aSpare all point to arrays of size
196334** nIdx. The aIdx array contains the set of integers from 0 to
196335** (nIdx-1) in no particular order. This function sorts the values
196336** in aIdx according to dimension iDim of the cells in aCell. The
196337** minimum value of dimension iDim is considered first, the
196338** maximum used to break ties.
196339**
196340** The aSpare array is used as temporary working space by the
196341** sorting algorithm.
196342*/
196343static void SortByDimension(
196344 Rtree *pRtree,
196345 int *aIdx,
196346 int nIdx,
196347 int iDim,
196348 RtreeCell *aCell,
196349 int *aSpare
196350){
196351 if( nIdx>1 ){
196352
196353 int iLeft = 0;
196354 int iRight = 0;
196355
196356 int nLeft = nIdx/2;
196357 int nRight = nIdx-nLeft;
196358 int *aLeft = aIdx;
196359 int *aRight = &aIdx[nLeft];
196360
196361 SortByDimension(pRtree, aIdx: aLeft, nIdx: nLeft, iDim, aCell, aSpare);
196362 SortByDimension(pRtree, aIdx: aRight, nIdx: nRight, iDim, aCell, aSpare);
196363
196364 memcpy(dest: aSpare, src: aLeft, n: sizeof(int)*nLeft);
196365 aLeft = aSpare;
196366 while( iLeft<nLeft || iRight<nRight ){
196367 RtreeDValue xleft1 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2]);
196368 RtreeDValue xleft2 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2+1]);
196369 RtreeDValue xright1 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2]);
196370 RtreeDValue xright2 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2+1]);
196371 if( (iLeft!=nLeft) && ((iRight==nRight)
196372 || (xleft1<xright1)
196373 || (xleft1==xright1 && xleft2<xright2)
196374 )){
196375 aIdx[iLeft+iRight] = aLeft[iLeft];
196376 iLeft++;
196377 }else{
196378 aIdx[iLeft+iRight] = aRight[iRight];
196379 iRight++;
196380 }
196381 }
196382
196383#if 0
196384 /* Check that the sort worked */
196385 {
196386 int jj;
196387 for(jj=1; jj<nIdx; jj++){
196388 RtreeDValue xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2];
196389 RtreeDValue xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1];
196390 RtreeDValue xright1 = aCell[aIdx[jj]].aCoord[iDim*2];
196391 RtreeDValue xright2 = aCell[aIdx[jj]].aCoord[iDim*2+1];
196392 assert( xleft1<=xright1 && (xleft1<xright1 || xleft2<=xright2) );
196393 }
196394 }
196395#endif
196396 }
196397}
196398
196399/*
196400** Implementation of the R*-tree variant of SplitNode from Beckman[1990].
196401*/
196402static int splitNodeStartree(
196403 Rtree *pRtree,
196404 RtreeCell *aCell,
196405 int nCell,
196406 RtreeNode *pLeft,
196407 RtreeNode *pRight,
196408 RtreeCell *pBboxLeft,
196409 RtreeCell *pBboxRight
196410){
196411 int **aaSorted;
196412 int *aSpare;
196413 int ii;
196414
196415 int iBestDim = 0;
196416 int iBestSplit = 0;
196417 RtreeDValue fBestMargin = RTREE_ZERO;
196418
196419 sqlite3_int64 nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
196420
196421 aaSorted = (int **)sqlite3_malloc64(n: nByte);
196422 if( !aaSorted ){
196423 return SQLITE_NOMEM;
196424 }
196425
196426 aSpare = &((int *)&aaSorted[pRtree->nDim])[pRtree->nDim*nCell];
196427 memset(s: aaSorted, c: 0, n: nByte);
196428 for(ii=0; ii<pRtree->nDim; ii++){
196429 int jj;
196430 aaSorted[ii] = &((int *)&aaSorted[pRtree->nDim])[ii*nCell];
196431 for(jj=0; jj<nCell; jj++){
196432 aaSorted[ii][jj] = jj;
196433 }
196434 SortByDimension(pRtree, aIdx: aaSorted[ii], nIdx: nCell, iDim: ii, aCell, aSpare);
196435 }
196436
196437 for(ii=0; ii<pRtree->nDim; ii++){
196438 RtreeDValue margin = RTREE_ZERO;
196439 RtreeDValue fBestOverlap = RTREE_ZERO;
196440 RtreeDValue fBestArea = RTREE_ZERO;
196441 int iBestLeft = 0;
196442 int nLeft;
196443
196444 for(
196445 nLeft=RTREE_MINCELLS(pRtree);
196446 nLeft<=(nCell-RTREE_MINCELLS(pRtree));
196447 nLeft++
196448 ){
196449 RtreeCell left;
196450 RtreeCell right;
196451 int kk;
196452 RtreeDValue overlap;
196453 RtreeDValue area;
196454
196455 memcpy(dest: &left, src: &aCell[aaSorted[ii][0]], n: sizeof(RtreeCell));
196456 memcpy(dest: &right, src: &aCell[aaSorted[ii][nCell-1]], n: sizeof(RtreeCell));
196457 for(kk=1; kk<(nCell-1); kk++){
196458 if( kk<nLeft ){
196459 cellUnion(pRtree, p1: &left, p2: &aCell[aaSorted[ii][kk]]);
196460 }else{
196461 cellUnion(pRtree, p1: &right, p2: &aCell[aaSorted[ii][kk]]);
196462 }
196463 }
196464 margin += cellMargin(pRtree, p: &left);
196465 margin += cellMargin(pRtree, p: &right);
196466 overlap = cellOverlap(pRtree, p: &left, aCell: &right, nCell: 1);
196467 area = cellArea(pRtree, p: &left) + cellArea(pRtree, p: &right);
196468 if( (nLeft==RTREE_MINCELLS(pRtree))
196469 || (overlap<fBestOverlap)
196470 || (overlap==fBestOverlap && area<fBestArea)
196471 ){
196472 iBestLeft = nLeft;
196473 fBestOverlap = overlap;
196474 fBestArea = area;
196475 }
196476 }
196477
196478 if( ii==0 || margin<fBestMargin ){
196479 iBestDim = ii;
196480 fBestMargin = margin;
196481 iBestSplit = iBestLeft;
196482 }
196483 }
196484
196485 memcpy(dest: pBboxLeft, src: &aCell[aaSorted[iBestDim][0]], n: sizeof(RtreeCell));
196486 memcpy(dest: pBboxRight, src: &aCell[aaSorted[iBestDim][iBestSplit]], n: sizeof(RtreeCell));
196487 for(ii=0; ii<nCell; ii++){
196488 RtreeNode *pTarget = (ii<iBestSplit)?pLeft:pRight;
196489 RtreeCell *pBbox = (ii<iBestSplit)?pBboxLeft:pBboxRight;
196490 RtreeCell *pCell = &aCell[aaSorted[iBestDim][ii]];
196491 nodeInsertCell(pRtree, pNode: pTarget, pCell);
196492 cellUnion(pRtree, p1: pBbox, p2: pCell);
196493 }
196494
196495 sqlite3_free(p: aaSorted);
196496 return SQLITE_OK;
196497}
196498
196499
196500static int updateMapping(
196501 Rtree *pRtree,
196502 i64 iRowid,
196503 RtreeNode *pNode,
196504 int iHeight
196505){
196506 int (*xSetMapping)(Rtree *, sqlite3_int64, sqlite3_int64);
196507 xSetMapping = ((iHeight==0)?rowidWrite:parentWrite);
196508 if( iHeight>0 ){
196509 RtreeNode *pChild = nodeHashLookup(pRtree, iNode: iRowid);
196510 RtreeNode *p;
196511 for(p=pNode; p; p=p->pParent){
196512 if( p==pChild ) return SQLITE_CORRUPT_VTAB;
196513 }
196514 if( pChild ){
196515 nodeRelease(pRtree, pNode: pChild->pParent);
196516 nodeReference(p: pNode);
196517 pChild->pParent = pNode;
196518 }
196519 }
196520 if( NEVER(pNode==0) ) return SQLITE_ERROR;
196521 return xSetMapping(pRtree, iRowid, pNode->iNode);
196522}
196523
196524static int SplitNode(
196525 Rtree *pRtree,
196526 RtreeNode *pNode,
196527 RtreeCell *pCell,
196528 int iHeight
196529){
196530 int i;
196531 int newCellIsRight = 0;
196532
196533 int rc = SQLITE_OK;
196534 int nCell = NCELL(pNode);
196535 RtreeCell *aCell;
196536 int *aiUsed;
196537
196538 RtreeNode *pLeft = 0;
196539 RtreeNode *pRight = 0;
196540
196541 RtreeCell leftbbox;
196542 RtreeCell rightbbox;
196543
196544 /* Allocate an array and populate it with a copy of pCell and
196545 ** all cells from node pLeft. Then zero the original node.
196546 */
196547 aCell = sqlite3_malloc64(n: (sizeof(RtreeCell)+sizeof(int))*(nCell+1));
196548 if( !aCell ){
196549 rc = SQLITE_NOMEM;
196550 goto splitnode_out;
196551 }
196552 aiUsed = (int *)&aCell[nCell+1];
196553 memset(s: aiUsed, c: 0, n: sizeof(int)*(nCell+1));
196554 for(i=0; i<nCell; i++){
196555 nodeGetCell(pRtree, pNode, iCell: i, pCell: &aCell[i]);
196556 }
196557 nodeZero(pRtree, p: pNode);
196558 memcpy(dest: &aCell[nCell], src: pCell, n: sizeof(RtreeCell));
196559 nCell++;
196560
196561 if( pNode->iNode==1 ){
196562 pRight = nodeNew(pRtree, pParent: pNode);
196563 pLeft = nodeNew(pRtree, pParent: pNode);
196564 pRtree->iDepth++;
196565 pNode->isDirty = 1;
196566 writeInt16(p: pNode->zData, i: pRtree->iDepth);
196567 }else{
196568 pLeft = pNode;
196569 pRight = nodeNew(pRtree, pParent: pLeft->pParent);
196570 pLeft->nRef++;
196571 }
196572
196573 if( !pLeft || !pRight ){
196574 rc = SQLITE_NOMEM;
196575 goto splitnode_out;
196576 }
196577
196578 memset(s: pLeft->zData, c: 0, n: pRtree->iNodeSize);
196579 memset(s: pRight->zData, c: 0, n: pRtree->iNodeSize);
196580
196581 rc = splitNodeStartree(pRtree, aCell, nCell, pLeft, pRight,
196582 pBboxLeft: &leftbbox, pBboxRight: &rightbbox);
196583 if( rc!=SQLITE_OK ){
196584 goto splitnode_out;
196585 }
196586
196587 /* Ensure both child nodes have node numbers assigned to them by calling
196588 ** nodeWrite(). Node pRight always needs a node number, as it was created
196589 ** by nodeNew() above. But node pLeft sometimes already has a node number.
196590 ** In this case avoid the all to nodeWrite().
196591 */
196592 if( SQLITE_OK!=(rc = nodeWrite(pRtree, pNode: pRight))
196593 || (0==pLeft->iNode && SQLITE_OK!=(rc = nodeWrite(pRtree, pNode: pLeft)))
196594 ){
196595 goto splitnode_out;
196596 }
196597
196598 rightbbox.iRowid = pRight->iNode;
196599 leftbbox.iRowid = pLeft->iNode;
196600
196601 if( pNode->iNode==1 ){
196602 rc = rtreeInsertCell(pRtree, pLeft->pParent, &leftbbox, iHeight+1);
196603 if( rc!=SQLITE_OK ){
196604 goto splitnode_out;
196605 }
196606 }else{
196607 RtreeNode *pParent = pLeft->pParent;
196608 int iCell;
196609 rc = nodeParentIndex(pRtree, pNode: pLeft, piIndex: &iCell);
196610 if( ALWAYS(rc==SQLITE_OK) ){
196611 nodeOverwriteCell(pRtree, pNode: pParent, pCell: &leftbbox, iCell);
196612 rc = AdjustTree(pRtree, pNode: pParent, pCell: &leftbbox);
196613 assert( rc==SQLITE_OK );
196614 }
196615 if( NEVER(rc!=SQLITE_OK) ){
196616 goto splitnode_out;
196617 }
196618 }
196619 if( (rc = rtreeInsertCell(pRtree, pRight->pParent, &rightbbox, iHeight+1)) ){
196620 goto splitnode_out;
196621 }
196622
196623 for(i=0; i<NCELL(pRight); i++){
196624 i64 iRowid = nodeGetRowid(pRtree, pNode: pRight, iCell: i);
196625 rc = updateMapping(pRtree, iRowid, pNode: pRight, iHeight);
196626 if( iRowid==pCell->iRowid ){
196627 newCellIsRight = 1;
196628 }
196629 if( rc!=SQLITE_OK ){
196630 goto splitnode_out;
196631 }
196632 }
196633 if( pNode->iNode==1 ){
196634 for(i=0; i<NCELL(pLeft); i++){
196635 i64 iRowid = nodeGetRowid(pRtree, pNode: pLeft, iCell: i);
196636 rc = updateMapping(pRtree, iRowid, pNode: pLeft, iHeight);
196637 if( rc!=SQLITE_OK ){
196638 goto splitnode_out;
196639 }
196640 }
196641 }else if( newCellIsRight==0 ){
196642 rc = updateMapping(pRtree, iRowid: pCell->iRowid, pNode: pLeft, iHeight);
196643 }
196644
196645 if( rc==SQLITE_OK ){
196646 rc = nodeRelease(pRtree, pNode: pRight);
196647 pRight = 0;
196648 }
196649 if( rc==SQLITE_OK ){
196650 rc = nodeRelease(pRtree, pNode: pLeft);
196651 pLeft = 0;
196652 }
196653
196654splitnode_out:
196655 nodeRelease(pRtree, pNode: pRight);
196656 nodeRelease(pRtree, pNode: pLeft);
196657 sqlite3_free(p: aCell);
196658 return rc;
196659}
196660
196661/*
196662** If node pLeaf is not the root of the r-tree and its pParent pointer is
196663** still NULL, load all ancestor nodes of pLeaf into memory and populate
196664** the pLeaf->pParent chain all the way up to the root node.
196665**
196666** This operation is required when a row is deleted (or updated - an update
196667** is implemented as a delete followed by an insert). SQLite provides the
196668** rowid of the row to delete, which can be used to find the leaf on which
196669** the entry resides (argument pLeaf). Once the leaf is located, this
196670** function is called to determine its ancestry.
196671*/
196672static int fixLeafParent(Rtree *pRtree, RtreeNode *pLeaf){
196673 int rc = SQLITE_OK;
196674 RtreeNode *pChild = pLeaf;
196675 while( rc==SQLITE_OK && pChild->iNode!=1 && pChild->pParent==0 ){
196676 int rc2 = SQLITE_OK; /* sqlite3_reset() return code */
196677 sqlite3_bind_int64(pStmt: pRtree->pReadParent, i: 1, iValue: pChild->iNode);
196678 rc = sqlite3_step(pStmt: pRtree->pReadParent);
196679 if( rc==SQLITE_ROW ){
196680 RtreeNode *pTest; /* Used to test for reference loops */
196681 i64 iNode; /* Node number of parent node */
196682
196683 /* Before setting pChild->pParent, test that we are not creating a
196684 ** loop of references (as we would if, say, pChild==pParent). We don't
196685 ** want to do this as it leads to a memory leak when trying to delete
196686 ** the referenced counted node structures.
196687 */
196688 iNode = sqlite3_column_int64(pStmt: pRtree->pReadParent, i: 0);
196689 for(pTest=pLeaf; pTest && pTest->iNode!=iNode; pTest=pTest->pParent);
196690 if( pTest==0 ){
196691 rc2 = nodeAcquire(pRtree, iNode, pParent: 0, ppNode: &pChild->pParent);
196692 }
196693 }
196694 rc = sqlite3_reset(pStmt: pRtree->pReadParent);
196695 if( rc==SQLITE_OK ) rc = rc2;
196696 if( rc==SQLITE_OK && !pChild->pParent ){
196697 RTREE_IS_CORRUPT(pRtree);
196698 rc = SQLITE_CORRUPT_VTAB;
196699 }
196700 pChild = pChild->pParent;
196701 }
196702 return rc;
196703}
196704
196705static int deleteCell(Rtree *, RtreeNode *, int, int);
196706
196707static int removeNode(Rtree *pRtree, RtreeNode *pNode, int iHeight){
196708 int rc;
196709 int rc2;
196710 RtreeNode *pParent = 0;
196711 int iCell;
196712
196713 assert( pNode->nRef==1 );
196714
196715 /* Remove the entry in the parent cell. */
196716 rc = nodeParentIndex(pRtree, pNode, piIndex: &iCell);
196717 if( rc==SQLITE_OK ){
196718 pParent = pNode->pParent;
196719 pNode->pParent = 0;
196720 rc = deleteCell(pRtree, pParent, iCell, iHeight+1);
196721 testcase( rc!=SQLITE_OK );
196722 }
196723 rc2 = nodeRelease(pRtree, pNode: pParent);
196724 if( rc==SQLITE_OK ){
196725 rc = rc2;
196726 }
196727 if( rc!=SQLITE_OK ){
196728 return rc;
196729 }
196730
196731 /* Remove the xxx_node entry. */
196732 sqlite3_bind_int64(pStmt: pRtree->pDeleteNode, i: 1, iValue: pNode->iNode);
196733 sqlite3_step(pStmt: pRtree->pDeleteNode);
196734 if( SQLITE_OK!=(rc = sqlite3_reset(pStmt: pRtree->pDeleteNode)) ){
196735 return rc;
196736 }
196737
196738 /* Remove the xxx_parent entry. */
196739 sqlite3_bind_int64(pStmt: pRtree->pDeleteParent, i: 1, iValue: pNode->iNode);
196740 sqlite3_step(pStmt: pRtree->pDeleteParent);
196741 if( SQLITE_OK!=(rc = sqlite3_reset(pStmt: pRtree->pDeleteParent)) ){
196742 return rc;
196743 }
196744
196745 /* Remove the node from the in-memory hash table and link it into
196746 ** the Rtree.pDeleted list. Its contents will be re-inserted later on.
196747 */
196748 nodeHashDelete(pRtree, pNode);
196749 pNode->iNode = iHeight;
196750 pNode->pNext = pRtree->pDeleted;
196751 pNode->nRef++;
196752 pRtree->pDeleted = pNode;
196753
196754 return SQLITE_OK;
196755}
196756
196757static int fixBoundingBox(Rtree *pRtree, RtreeNode *pNode){
196758 RtreeNode *pParent = pNode->pParent;
196759 int rc = SQLITE_OK;
196760 if( pParent ){
196761 int ii;
196762 int nCell = NCELL(pNode);
196763 RtreeCell box; /* Bounding box for pNode */
196764 nodeGetCell(pRtree, pNode, iCell: 0, pCell: &box);
196765 for(ii=1; ii<nCell; ii++){
196766 RtreeCell cell;
196767 nodeGetCell(pRtree, pNode, iCell: ii, pCell: &cell);
196768 cellUnion(pRtree, p1: &box, p2: &cell);
196769 }
196770 box.iRowid = pNode->iNode;
196771 rc = nodeParentIndex(pRtree, pNode, piIndex: &ii);
196772 if( rc==SQLITE_OK ){
196773 nodeOverwriteCell(pRtree, pNode: pParent, pCell: &box, iCell: ii);
196774 rc = fixBoundingBox(pRtree, pNode: pParent);
196775 }
196776 }
196777 return rc;
196778}
196779
196780/*
196781** Delete the cell at index iCell of node pNode. After removing the
196782** cell, adjust the r-tree data structure if required.
196783*/
196784static int deleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell, int iHeight){
196785 RtreeNode *pParent;
196786 int rc;
196787
196788 if( SQLITE_OK!=(rc = fixLeafParent(pRtree, pLeaf: pNode)) ){
196789 return rc;
196790 }
196791
196792 /* Remove the cell from the node. This call just moves bytes around
196793 ** the in-memory node image, so it cannot fail.
196794 */
196795 nodeDeleteCell(pRtree, pNode, iCell);
196796
196797 /* If the node is not the tree root and now has less than the minimum
196798 ** number of cells, remove it from the tree. Otherwise, update the
196799 ** cell in the parent node so that it tightly contains the updated
196800 ** node.
196801 */
196802 pParent = pNode->pParent;
196803 assert( pParent || pNode->iNode==1 );
196804 if( pParent ){
196805 if( NCELL(pNode)<RTREE_MINCELLS(pRtree) ){
196806 rc = removeNode(pRtree, pNode, iHeight);
196807 }else{
196808 rc = fixBoundingBox(pRtree, pNode);
196809 }
196810 }
196811
196812 return rc;
196813}
196814
196815static int Reinsert(
196816 Rtree *pRtree,
196817 RtreeNode *pNode,
196818 RtreeCell *pCell,
196819 int iHeight
196820){
196821 int *aOrder;
196822 int *aSpare;
196823 RtreeCell *aCell;
196824 RtreeDValue *aDistance;
196825 int nCell;
196826 RtreeDValue aCenterCoord[RTREE_MAX_DIMENSIONS];
196827 int iDim;
196828 int ii;
196829 int rc = SQLITE_OK;
196830 int n;
196831
196832 memset(s: aCenterCoord, c: 0, n: sizeof(RtreeDValue)*RTREE_MAX_DIMENSIONS);
196833
196834 nCell = NCELL(pNode)+1;
196835 n = (nCell+1)&(~1);
196836
196837 /* Allocate the buffers used by this operation. The allocation is
196838 ** relinquished before this function returns.
196839 */
196840 aCell = (RtreeCell *)sqlite3_malloc64(n: n * (
196841 sizeof(RtreeCell) + /* aCell array */
196842 sizeof(int) + /* aOrder array */
196843 sizeof(int) + /* aSpare array */
196844 sizeof(RtreeDValue) /* aDistance array */
196845 ));
196846 if( !aCell ){
196847 return SQLITE_NOMEM;
196848 }
196849 aOrder = (int *)&aCell[n];
196850 aSpare = (int *)&aOrder[n];
196851 aDistance = (RtreeDValue *)&aSpare[n];
196852
196853 for(ii=0; ii<nCell; ii++){
196854 if( ii==(nCell-1) ){
196855 memcpy(dest: &aCell[ii], src: pCell, n: sizeof(RtreeCell));
196856 }else{
196857 nodeGetCell(pRtree, pNode, iCell: ii, pCell: &aCell[ii]);
196858 }
196859 aOrder[ii] = ii;
196860 for(iDim=0; iDim<pRtree->nDim; iDim++){
196861 aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2]);
196862 aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2+1]);
196863 }
196864 }
196865 for(iDim=0; iDim<pRtree->nDim; iDim++){
196866 aCenterCoord[iDim] = (aCenterCoord[iDim]/(nCell*(RtreeDValue)2));
196867 }
196868
196869 for(ii=0; ii<nCell; ii++){
196870 aDistance[ii] = RTREE_ZERO;
196871 for(iDim=0; iDim<pRtree->nDim; iDim++){
196872 RtreeDValue coord = (DCOORD(aCell[ii].aCoord[iDim*2+1]) -
196873 DCOORD(aCell[ii].aCoord[iDim*2]));
196874 aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]);
196875 }
196876 }
196877
196878 SortByDistance(aIdx: aOrder, nIdx: nCell, aDistance, aSpare);
196879 nodeZero(pRtree, p: pNode);
196880
196881 for(ii=0; rc==SQLITE_OK && ii<(nCell-(RTREE_MINCELLS(pRtree)+1)); ii++){
196882 RtreeCell *p = &aCell[aOrder[ii]];
196883 nodeInsertCell(pRtree, pNode, pCell: p);
196884 if( p->iRowid==pCell->iRowid ){
196885 if( iHeight==0 ){
196886 rc = rowidWrite(pRtree, iRowid: p->iRowid, iNode: pNode->iNode);
196887 }else{
196888 rc = parentWrite(pRtree, iNode: p->iRowid, iPar: pNode->iNode);
196889 }
196890 }
196891 }
196892 if( rc==SQLITE_OK ){
196893 rc = fixBoundingBox(pRtree, pNode);
196894 }
196895 for(; rc==SQLITE_OK && ii<nCell; ii++){
196896 /* Find a node to store this cell in. pNode->iNode currently contains
196897 ** the height of the sub-tree headed by the cell.
196898 */
196899 RtreeNode *pInsert;
196900 RtreeCell *p = &aCell[aOrder[ii]];
196901 rc = ChooseLeaf(pRtree, pCell: p, iHeight, ppLeaf: &pInsert);
196902 if( rc==SQLITE_OK ){
196903 int rc2;
196904 rc = rtreeInsertCell(pRtree, pInsert, p, iHeight);
196905 rc2 = nodeRelease(pRtree, pNode: pInsert);
196906 if( rc==SQLITE_OK ){
196907 rc = rc2;
196908 }
196909 }
196910 }
196911
196912 sqlite3_free(p: aCell);
196913 return rc;
196914}
196915
196916/*
196917** Insert cell pCell into node pNode. Node pNode is the head of a
196918** subtree iHeight high (leaf nodes have iHeight==0).
196919*/
196920static int rtreeInsertCell(
196921 Rtree *pRtree,
196922 RtreeNode *pNode,
196923 RtreeCell *pCell,
196924 int iHeight
196925){
196926 int rc = SQLITE_OK;
196927 if( iHeight>0 ){
196928 RtreeNode *pChild = nodeHashLookup(pRtree, iNode: pCell->iRowid);
196929 if( pChild ){
196930 nodeRelease(pRtree, pNode: pChild->pParent);
196931 nodeReference(p: pNode);
196932 pChild->pParent = pNode;
196933 }
196934 }
196935 if( nodeInsertCell(pRtree, pNode, pCell) ){
196936 if( iHeight<=pRtree->iReinsertHeight || pNode->iNode==1){
196937 rc = SplitNode(pRtree, pNode, pCell, iHeight);
196938 }else{
196939 pRtree->iReinsertHeight = iHeight;
196940 rc = Reinsert(pRtree, pNode, pCell, iHeight);
196941 }
196942 }else{
196943 rc = AdjustTree(pRtree, pNode, pCell);
196944 if( ALWAYS(rc==SQLITE_OK) ){
196945 if( iHeight==0 ){
196946 rc = rowidWrite(pRtree, iRowid: pCell->iRowid, iNode: pNode->iNode);
196947 }else{
196948 rc = parentWrite(pRtree, iNode: pCell->iRowid, iPar: pNode->iNode);
196949 }
196950 }
196951 }
196952 return rc;
196953}
196954
196955static int reinsertNodeContent(Rtree *pRtree, RtreeNode *pNode){
196956 int ii;
196957 int rc = SQLITE_OK;
196958 int nCell = NCELL(pNode);
196959
196960 for(ii=0; rc==SQLITE_OK && ii<nCell; ii++){
196961 RtreeNode *pInsert;
196962 RtreeCell cell;
196963 nodeGetCell(pRtree, pNode, iCell: ii, pCell: &cell);
196964
196965 /* Find a node to store this cell in. pNode->iNode currently contains
196966 ** the height of the sub-tree headed by the cell.
196967 */
196968 rc = ChooseLeaf(pRtree, pCell: &cell, iHeight: (int)pNode->iNode, ppLeaf: &pInsert);
196969 if( rc==SQLITE_OK ){
196970 int rc2;
196971 rc = rtreeInsertCell(pRtree, pNode: pInsert, pCell: &cell, iHeight: (int)pNode->iNode);
196972 rc2 = nodeRelease(pRtree, pNode: pInsert);
196973 if( rc==SQLITE_OK ){
196974 rc = rc2;
196975 }
196976 }
196977 }
196978 return rc;
196979}
196980
196981/*
196982** Select a currently unused rowid for a new r-tree record.
196983*/
196984static int rtreeNewRowid(Rtree *pRtree, i64 *piRowid){
196985 int rc;
196986 sqlite3_bind_null(pStmt: pRtree->pWriteRowid, i: 1);
196987 sqlite3_bind_null(pStmt: pRtree->pWriteRowid, i: 2);
196988 sqlite3_step(pStmt: pRtree->pWriteRowid);
196989 rc = sqlite3_reset(pStmt: pRtree->pWriteRowid);
196990 *piRowid = sqlite3_last_insert_rowid(db: pRtree->db);
196991 return rc;
196992}
196993
196994/*
196995** Remove the entry with rowid=iDelete from the r-tree structure.
196996*/
196997static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){
196998 int rc; /* Return code */
196999 RtreeNode *pLeaf = 0; /* Leaf node containing record iDelete */
197000 int iCell; /* Index of iDelete cell in pLeaf */
197001 RtreeNode *pRoot = 0; /* Root node of rtree structure */
197002
197003
197004 /* Obtain a reference to the root node to initialize Rtree.iDepth */
197005 rc = nodeAcquire(pRtree, iNode: 1, pParent: 0, ppNode: &pRoot);
197006
197007 /* Obtain a reference to the leaf node that contains the entry
197008 ** about to be deleted.
197009 */
197010 if( rc==SQLITE_OK ){
197011 rc = findLeafNode(pRtree, iRowid: iDelete, ppLeaf: &pLeaf, piNode: 0);
197012 }
197013
197014#ifdef CORRUPT_DB
197015 assert( pLeaf!=0 || rc!=SQLITE_OK || CORRUPT_DB );
197016#endif
197017
197018 /* Delete the cell in question from the leaf node. */
197019 if( rc==SQLITE_OK && pLeaf ){
197020 int rc2;
197021 rc = nodeRowidIndex(pRtree, pNode: pLeaf, iRowid: iDelete, piIndex: &iCell);
197022 if( rc==SQLITE_OK ){
197023 rc = deleteCell(pRtree, pNode: pLeaf, iCell, iHeight: 0);
197024 }
197025 rc2 = nodeRelease(pRtree, pNode: pLeaf);
197026 if( rc==SQLITE_OK ){
197027 rc = rc2;
197028 }
197029 }
197030
197031 /* Delete the corresponding entry in the <rtree>_rowid table. */
197032 if( rc==SQLITE_OK ){
197033 sqlite3_bind_int64(pStmt: pRtree->pDeleteRowid, i: 1, iValue: iDelete);
197034 sqlite3_step(pStmt: pRtree->pDeleteRowid);
197035 rc = sqlite3_reset(pStmt: pRtree->pDeleteRowid);
197036 }
197037
197038 /* Check if the root node now has exactly one child. If so, remove
197039 ** it, schedule the contents of the child for reinsertion and
197040 ** reduce the tree height by one.
197041 **
197042 ** This is equivalent to copying the contents of the child into
197043 ** the root node (the operation that Gutman's paper says to perform
197044 ** in this scenario).
197045 */
197046 if( rc==SQLITE_OK && pRtree->iDepth>0 && NCELL(pRoot)==1 ){
197047 int rc2;
197048 RtreeNode *pChild = 0;
197049 i64 iChild = nodeGetRowid(pRtree, pNode: pRoot, iCell: 0);
197050 rc = nodeAcquire(pRtree, iNode: iChild, pParent: pRoot, ppNode: &pChild); /* tag-20210916a */
197051 if( rc==SQLITE_OK ){
197052 rc = removeNode(pRtree, pNode: pChild, iHeight: pRtree->iDepth-1);
197053 }
197054 rc2 = nodeRelease(pRtree, pNode: pChild);
197055 if( rc==SQLITE_OK ) rc = rc2;
197056 if( rc==SQLITE_OK ){
197057 pRtree->iDepth--;
197058 writeInt16(p: pRoot->zData, i: pRtree->iDepth);
197059 pRoot->isDirty = 1;
197060 }
197061 }
197062
197063 /* Re-insert the contents of any underfull nodes removed from the tree. */
197064 for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){
197065 if( rc==SQLITE_OK ){
197066 rc = reinsertNodeContent(pRtree, pNode: pLeaf);
197067 }
197068 pRtree->pDeleted = pLeaf->pNext;
197069 pRtree->nNodeRef--;
197070 sqlite3_free(p: pLeaf);
197071 }
197072
197073 /* Release the reference to the root node. */
197074 if( rc==SQLITE_OK ){
197075 rc = nodeRelease(pRtree, pNode: pRoot);
197076 }else{
197077 nodeRelease(pRtree, pNode: pRoot);
197078 }
197079
197080 return rc;
197081}
197082
197083/*
197084** Rounding constants for float->double conversion.
197085*/
197086#define RNDTOWARDS (1.0 - 1.0/8388608.0) /* Round towards zero */
197087#define RNDAWAY (1.0 + 1.0/8388608.0) /* Round away from zero */
197088
197089#if !defined(SQLITE_RTREE_INT_ONLY)
197090/*
197091** Convert an sqlite3_value into an RtreeValue (presumably a float)
197092** while taking care to round toward negative or positive, respectively.
197093*/
197094static RtreeValue rtreeValueDown(sqlite3_value *v){
197095 double d = sqlite3_value_double(pVal: v);
197096 float f = (float)d;
197097 if( f>d ){
197098 f = (float)(d*(d<0 ? RNDAWAY : RNDTOWARDS));
197099 }
197100 return f;
197101}
197102static RtreeValue rtreeValueUp(sqlite3_value *v){
197103 double d = sqlite3_value_double(pVal: v);
197104 float f = (float)d;
197105 if( f<d ){
197106 f = (float)(d*(d<0 ? RNDTOWARDS : RNDAWAY));
197107 }
197108 return f;
197109}
197110#endif /* !defined(SQLITE_RTREE_INT_ONLY) */
197111
197112/*
197113** A constraint has failed while inserting a row into an rtree table.
197114** Assuming no OOM error occurs, this function sets the error message
197115** (at pRtree->base.zErrMsg) to an appropriate value and returns
197116** SQLITE_CONSTRAINT.
197117**
197118** Parameter iCol is the index of the leftmost column involved in the
197119** constraint failure. If it is 0, then the constraint that failed is
197120** the unique constraint on the id column. Otherwise, it is the rtree
197121** (c1<=c2) constraint on columns iCol and iCol+1 that has failed.
197122**
197123** If an OOM occurs, SQLITE_NOMEM is returned instead of SQLITE_CONSTRAINT.
197124*/
197125static int rtreeConstraintError(Rtree *pRtree, int iCol){
197126 sqlite3_stmt *pStmt = 0;
197127 char *zSql;
197128 int rc;
197129
197130 assert( iCol==0 || iCol%2 );
197131 zSql = sqlite3_mprintf(zFormat: "SELECT * FROM %Q.%Q", pRtree->zDb, pRtree->zName);
197132 if( zSql ){
197133 rc = sqlite3_prepare_v2(db: pRtree->db, zSql, nBytes: -1, ppStmt: &pStmt, pzTail: 0);
197134 }else{
197135 rc = SQLITE_NOMEM;
197136 }
197137 sqlite3_free(p: zSql);
197138
197139 if( rc==SQLITE_OK ){
197140 if( iCol==0 ){
197141 const char *zCol = sqlite3_column_name(pStmt, N: 0);
197142 pRtree->base.zErrMsg = sqlite3_mprintf(
197143 zFormat: "UNIQUE constraint failed: %s.%s", pRtree->zName, zCol
197144 );
197145 }else{
197146 const char *zCol1 = sqlite3_column_name(pStmt, N: iCol);
197147 const char *zCol2 = sqlite3_column_name(pStmt, N: iCol+1);
197148 pRtree->base.zErrMsg = sqlite3_mprintf(
197149 zFormat: "rtree constraint failed: %s.(%s<=%s)", pRtree->zName, zCol1, zCol2
197150 );
197151 }
197152 }
197153
197154 sqlite3_finalize(pStmt);
197155 return (rc==SQLITE_OK ? SQLITE_CONSTRAINT : rc);
197156}
197157
197158
197159
197160/*
197161** The xUpdate method for rtree module virtual tables.
197162*/
197163static int rtreeUpdate(
197164 sqlite3_vtab *pVtab,
197165 int nData,
197166 sqlite3_value **aData,
197167 sqlite_int64 *pRowid
197168){
197169 Rtree *pRtree = (Rtree *)pVtab;
197170 int rc = SQLITE_OK;
197171 RtreeCell cell; /* New cell to insert if nData>1 */
197172 int bHaveRowid = 0; /* Set to 1 after new rowid is determined */
197173
197174 if( pRtree->nNodeRef ){
197175 /* Unable to write to the btree while another cursor is reading from it,
197176 ** since the write might do a rebalance which would disrupt the read
197177 ** cursor. */
197178 return SQLITE_LOCKED_VTAB;
197179 }
197180 rtreeReference(pRtree);
197181 assert(nData>=1);
197182
197183 cell.iRowid = 0; /* Used only to suppress a compiler warning */
197184
197185 /* Constraint handling. A write operation on an r-tree table may return
197186 ** SQLITE_CONSTRAINT for two reasons:
197187 **
197188 ** 1. A duplicate rowid value, or
197189 ** 2. The supplied data violates the "x2>=x1" constraint.
197190 **
197191 ** In the first case, if the conflict-handling mode is REPLACE, then
197192 ** the conflicting row can be removed before proceeding. In the second
197193 ** case, SQLITE_CONSTRAINT must be returned regardless of the
197194 ** conflict-handling mode specified by the user.
197195 */
197196 if( nData>1 ){
197197 int ii;
197198 int nn = nData - 4;
197199
197200 if( nn > pRtree->nDim2 ) nn = pRtree->nDim2;
197201 /* Populate the cell.aCoord[] array. The first coordinate is aData[3].
197202 **
197203 ** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared
197204 ** with "column" that are interpreted as table constraints.
197205 ** Example: CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5));
197206 ** This problem was discovered after years of use, so we silently ignore
197207 ** these kinds of misdeclared tables to avoid breaking any legacy.
197208 */
197209
197210#ifndef SQLITE_RTREE_INT_ONLY
197211 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
197212 for(ii=0; ii<nn; ii+=2){
197213 cell.aCoord[ii].f = rtreeValueDown(v: aData[ii+3]);
197214 cell.aCoord[ii+1].f = rtreeValueUp(v: aData[ii+4]);
197215 if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
197216 rc = rtreeConstraintError(pRtree, iCol: ii+1);
197217 goto constraint;
197218 }
197219 }
197220 }else
197221#endif
197222 {
197223 for(ii=0; ii<nn; ii+=2){
197224 cell.aCoord[ii].i = sqlite3_value_int(pVal: aData[ii+3]);
197225 cell.aCoord[ii+1].i = sqlite3_value_int(pVal: aData[ii+4]);
197226 if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
197227 rc = rtreeConstraintError(pRtree, iCol: ii+1);
197228 goto constraint;
197229 }
197230 }
197231 }
197232
197233 /* If a rowid value was supplied, check if it is already present in
197234 ** the table. If so, the constraint has failed. */
197235 if( sqlite3_value_type(pVal: aData[2])!=SQLITE_NULL ){
197236 cell.iRowid = sqlite3_value_int64(pVal: aData[2]);
197237 if( sqlite3_value_type(pVal: aData[0])==SQLITE_NULL
197238 || sqlite3_value_int64(pVal: aData[0])!=cell.iRowid
197239 ){
197240 int steprc;
197241 sqlite3_bind_int64(pStmt: pRtree->pReadRowid, i: 1, iValue: cell.iRowid);
197242 steprc = sqlite3_step(pStmt: pRtree->pReadRowid);
197243 rc = sqlite3_reset(pStmt: pRtree->pReadRowid);
197244 if( SQLITE_ROW==steprc ){
197245 if( sqlite3_vtab_on_conflict(db: pRtree->db)==SQLITE_REPLACE ){
197246 rc = rtreeDeleteRowid(pRtree, iDelete: cell.iRowid);
197247 }else{
197248 rc = rtreeConstraintError(pRtree, iCol: 0);
197249 goto constraint;
197250 }
197251 }
197252 }
197253 bHaveRowid = 1;
197254 }
197255 }
197256
197257 /* If aData[0] is not an SQL NULL value, it is the rowid of a
197258 ** record to delete from the r-tree table. The following block does
197259 ** just that.
197260 */
197261 if( sqlite3_value_type(pVal: aData[0])!=SQLITE_NULL ){
197262 rc = rtreeDeleteRowid(pRtree, iDelete: sqlite3_value_int64(pVal: aData[0]));
197263 }
197264
197265 /* If the aData[] array contains more than one element, elements
197266 ** (aData[2]..aData[argc-1]) contain a new record to insert into
197267 ** the r-tree structure.
197268 */
197269 if( rc==SQLITE_OK && nData>1 ){
197270 /* Insert the new record into the r-tree */
197271 RtreeNode *pLeaf = 0;
197272
197273 /* Figure out the rowid of the new row. */
197274 if( bHaveRowid==0 ){
197275 rc = rtreeNewRowid(pRtree, piRowid: &cell.iRowid);
197276 }
197277 *pRowid = cell.iRowid;
197278
197279 if( rc==SQLITE_OK ){
197280 rc = ChooseLeaf(pRtree, pCell: &cell, iHeight: 0, ppLeaf: &pLeaf);
197281 }
197282 if( rc==SQLITE_OK ){
197283 int rc2;
197284 pRtree->iReinsertHeight = -1;
197285 rc = rtreeInsertCell(pRtree, pNode: pLeaf, pCell: &cell, iHeight: 0);
197286 rc2 = nodeRelease(pRtree, pNode: pLeaf);
197287 if( rc==SQLITE_OK ){
197288 rc = rc2;
197289 }
197290 }
197291 if( rc==SQLITE_OK && pRtree->nAux ){
197292 sqlite3_stmt *pUp = pRtree->pWriteAux;
197293 int jj;
197294 sqlite3_bind_int64(pStmt: pUp, i: 1, iValue: *pRowid);
197295 for(jj=0; jj<pRtree->nAux; jj++){
197296 sqlite3_bind_value(pStmt: pUp, i: jj+2, pValue: aData[pRtree->nDim2+3+jj]);
197297 }
197298 sqlite3_step(pStmt: pUp);
197299 rc = sqlite3_reset(pStmt: pUp);
197300 }
197301 }
197302
197303constraint:
197304 rtreeRelease(pRtree);
197305 return rc;
197306}
197307
197308/*
197309** Called when a transaction starts.
197310*/
197311static int rtreeBeginTransaction(sqlite3_vtab *pVtab){
197312 Rtree *pRtree = (Rtree *)pVtab;
197313 assert( pRtree->inWrTrans==0 );
197314 pRtree->inWrTrans++;
197315 return SQLITE_OK;
197316}
197317
197318/*
197319** Called when a transaction completes (either by COMMIT or ROLLBACK).
197320** The sqlite3_blob object should be released at this point.
197321*/
197322static int rtreeEndTransaction(sqlite3_vtab *pVtab){
197323 Rtree *pRtree = (Rtree *)pVtab;
197324 pRtree->inWrTrans = 0;
197325 nodeBlobReset(pRtree);
197326 return SQLITE_OK;
197327}
197328
197329/*
197330** The xRename method for rtree module virtual tables.
197331*/
197332static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){
197333 Rtree *pRtree = (Rtree *)pVtab;
197334 int rc = SQLITE_NOMEM;
197335 char *zSql = sqlite3_mprintf(
197336 zFormat: "ALTER TABLE %Q.'%q_node' RENAME TO \"%w_node\";"
197337 "ALTER TABLE %Q.'%q_parent' RENAME TO \"%w_parent\";"
197338 "ALTER TABLE %Q.'%q_rowid' RENAME TO \"%w_rowid\";"
197339 , pRtree->zDb, pRtree->zName, zNewName
197340 , pRtree->zDb, pRtree->zName, zNewName
197341 , pRtree->zDb, pRtree->zName, zNewName
197342 );
197343 if( zSql ){
197344 nodeBlobReset(pRtree);
197345 rc = sqlite3_exec(db: pRtree->db, zSql, xCallback: 0, pArg: 0, pzErrMsg: 0);
197346 sqlite3_free(p: zSql);
197347 }
197348 return rc;
197349}
197350
197351/*
197352** The xSavepoint method.
197353**
197354** This module does not need to do anything to support savepoints. However,
197355** it uses this hook to close any open blob handle. This is done because a
197356** DROP TABLE command - which fortunately always opens a savepoint - cannot
197357** succeed if there are any open blob handles. i.e. if the blob handle were
197358** not closed here, the following would fail:
197359**
197360** BEGIN;
197361** INSERT INTO rtree...
197362** DROP TABLE <tablename>; -- Would fail with SQLITE_LOCKED
197363** COMMIT;
197364*/
197365static int rtreeSavepoint(sqlite3_vtab *pVtab, int iSavepoint){
197366 Rtree *pRtree = (Rtree *)pVtab;
197367 u8 iwt = pRtree->inWrTrans;
197368 UNUSED_PARAMETER(iSavepoint);
197369 pRtree->inWrTrans = 0;
197370 nodeBlobReset(pRtree);
197371 pRtree->inWrTrans = iwt;
197372 return SQLITE_OK;
197373}
197374
197375/*
197376** This function populates the pRtree->nRowEst variable with an estimate
197377** of the number of rows in the virtual table. If possible, this is based
197378** on sqlite_stat1 data. Otherwise, use RTREE_DEFAULT_ROWEST.
197379*/
197380static int rtreeQueryStat1(sqlite3 *db, Rtree *pRtree){
197381 const char *zFmt = "SELECT stat FROM %Q.sqlite_stat1 WHERE tbl = '%q_rowid'";
197382 char *zSql;
197383 sqlite3_stmt *p;
197384 int rc;
197385 i64 nRow = RTREE_MIN_ROWEST;
197386
197387 rc = sqlite3_table_column_metadata(
197388 db, zDbName: pRtree->zDb, zTableName: "sqlite_stat1",zColumnName: 0,pzDataType: 0,pzCollSeq: 0,pNotNull: 0,pPrimaryKey: 0,pAutoinc: 0
197389 );
197390 if( rc!=SQLITE_OK ){
197391 pRtree->nRowEst = RTREE_DEFAULT_ROWEST;
197392 return rc==SQLITE_ERROR ? SQLITE_OK : rc;
197393 }
197394 zSql = sqlite3_mprintf(zFormat: zFmt, pRtree->zDb, pRtree->zName);
197395 if( zSql==0 ){
197396 rc = SQLITE_NOMEM;
197397 }else{
197398 rc = sqlite3_prepare_v2(db, zSql, nBytes: -1, ppStmt: &p, pzTail: 0);
197399 if( rc==SQLITE_OK ){
197400 if( sqlite3_step(pStmt: p)==SQLITE_ROW ) nRow = sqlite3_column_int64(pStmt: p, i: 0);
197401 rc = sqlite3_finalize(pStmt: p);
197402 }
197403 sqlite3_free(p: zSql);
197404 }
197405 pRtree->nRowEst = MAX(nRow, RTREE_MIN_ROWEST);
197406 return rc;
197407}
197408
197409
197410/*
197411** Return true if zName is the extension on one of the shadow tables used
197412** by this module.
197413*/
197414static int rtreeShadowName(const char *zName){
197415 static const char *azName[] = {
197416 "node", "parent", "rowid"
197417 };
197418 unsigned int i;
197419 for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
197420 if( sqlite3_stricmp(zLeft: zName, zRight: azName[i])==0 ) return 1;
197421 }
197422 return 0;
197423}
197424
197425static sqlite3_module rtreeModule = {
197426 3, /* iVersion */
197427 rtreeCreate, /* xCreate - create a table */
197428 rtreeConnect, /* xConnect - connect to an existing table */
197429 rtreeBestIndex, /* xBestIndex - Determine search strategy */
197430 rtreeDisconnect, /* xDisconnect - Disconnect from a table */
197431 rtreeDestroy, /* xDestroy - Drop a table */
197432 rtreeOpen, /* xOpen - open a cursor */
197433 rtreeClose, /* xClose - close a cursor */
197434 rtreeFilter, /* xFilter - configure scan constraints */
197435 rtreeNext, /* xNext - advance a cursor */
197436 rtreeEof, /* xEof */
197437 rtreeColumn, /* xColumn - read data */
197438 rtreeRowid, /* xRowid - read data */
197439 rtreeUpdate, /* xUpdate - write data */
197440 rtreeBeginTransaction, /* xBegin - begin transaction */
197441 rtreeEndTransaction, /* xSync - sync transaction */
197442 rtreeEndTransaction, /* xCommit - commit transaction */
197443 rtreeEndTransaction, /* xRollback - rollback transaction */
197444 0, /* xFindFunction - function overloading */
197445 rtreeRename, /* xRename - rename the table */
197446 rtreeSavepoint, /* xSavepoint */
197447 0, /* xRelease */
197448 0, /* xRollbackTo */
197449 rtreeShadowName /* xShadowName */
197450};
197451
197452static int rtreeSqlInit(
197453 Rtree *pRtree,
197454 sqlite3 *db,
197455 const char *zDb,
197456 const char *zPrefix,
197457 int isCreate
197458){
197459 int rc = SQLITE_OK;
197460
197461 #define N_STATEMENT 8
197462 static const char *azSql[N_STATEMENT] = {
197463 /* Write the xxx_node table */
197464 "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(?1, ?2)",
197465 "DELETE FROM '%q'.'%q_node' WHERE nodeno = ?1",
197466
197467 /* Read and write the xxx_rowid table */
197468 "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = ?1",
197469 "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(?1, ?2)",
197470 "DELETE FROM '%q'.'%q_rowid' WHERE rowid = ?1",
197471
197472 /* Read and write the xxx_parent table */
197473 "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = ?1",
197474 "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(?1, ?2)",
197475 "DELETE FROM '%q'.'%q_parent' WHERE nodeno = ?1"
197476 };
197477 sqlite3_stmt **appStmt[N_STATEMENT];
197478 int i;
197479 const int f = SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_NO_VTAB;
197480
197481 pRtree->db = db;
197482
197483 if( isCreate ){
197484 char *zCreate;
197485 sqlite3_str *p = sqlite3_str_new(db);
197486 int ii;
197487 sqlite3_str_appendf(p,
197488 zFormat: "CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY,nodeno",
197489 zDb, zPrefix);
197490 for(ii=0; ii<pRtree->nAux; ii++){
197491 sqlite3_str_appendf(p,zFormat: ",a%d",ii);
197492 }
197493 sqlite3_str_appendf(p,
197494 zFormat: ");CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY,data);",
197495 zDb, zPrefix);
197496 sqlite3_str_appendf(p,
197497 zFormat: "CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY,parentnode);",
197498 zDb, zPrefix);
197499 sqlite3_str_appendf(p,
197500 zFormat: "INSERT INTO \"%w\".\"%w_node\"VALUES(1,zeroblob(%d))",
197501 zDb, zPrefix, pRtree->iNodeSize);
197502 zCreate = sqlite3_str_finish(p);
197503 if( !zCreate ){
197504 return SQLITE_NOMEM;
197505 }
197506 rc = sqlite3_exec(db, zSql: zCreate, xCallback: 0, pArg: 0, pzErrMsg: 0);
197507 sqlite3_free(p: zCreate);
197508 if( rc!=SQLITE_OK ){
197509 return rc;
197510 }
197511 }
197512
197513 appStmt[0] = &pRtree->pWriteNode;
197514 appStmt[1] = &pRtree->pDeleteNode;
197515 appStmt[2] = &pRtree->pReadRowid;
197516 appStmt[3] = &pRtree->pWriteRowid;
197517 appStmt[4] = &pRtree->pDeleteRowid;
197518 appStmt[5] = &pRtree->pReadParent;
197519 appStmt[6] = &pRtree->pWriteParent;
197520 appStmt[7] = &pRtree->pDeleteParent;
197521
197522 rc = rtreeQueryStat1(db, pRtree);
197523 for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){
197524 char *zSql;
197525 const char *zFormat;
197526 if( i!=3 || pRtree->nAux==0 ){
197527 zFormat = azSql[i];
197528 }else {
197529 /* An UPSERT is very slightly slower than REPLACE, but it is needed
197530 ** if there are auxiliary columns */
197531 zFormat = "INSERT INTO\"%w\".\"%w_rowid\"(rowid,nodeno)VALUES(?1,?2)"
197532 "ON CONFLICT(rowid)DO UPDATE SET nodeno=excluded.nodeno";
197533 }
197534 zSql = sqlite3_mprintf(zFormat, zDb, zPrefix);
197535 if( zSql ){
197536 rc = sqlite3_prepare_v3(db, zSql, nBytes: -1, prepFlags: f, ppStmt: appStmt[i], pzTail: 0);
197537 }else{
197538 rc = SQLITE_NOMEM;
197539 }
197540 sqlite3_free(p: zSql);
197541 }
197542 if( pRtree->nAux ){
197543 pRtree->zReadAuxSql = sqlite3_mprintf(
197544 zFormat: "SELECT * FROM \"%w\".\"%w_rowid\" WHERE rowid=?1",
197545 zDb, zPrefix);
197546 if( pRtree->zReadAuxSql==0 ){
197547 rc = SQLITE_NOMEM;
197548 }else{
197549 sqlite3_str *p = sqlite3_str_new(db);
197550 int ii;
197551 char *zSql;
197552 sqlite3_str_appendf(p, zFormat: "UPDATE \"%w\".\"%w_rowid\"SET ", zDb, zPrefix);
197553 for(ii=0; ii<pRtree->nAux; ii++){
197554 if( ii ) sqlite3_str_append(p, z: ",", N: 1);
197555#ifdef SQLITE_ENABLE_GEOPOLY
197556 if( ii<pRtree->nAuxNotNull ){
197557 sqlite3_str_appendf(p,"a%d=coalesce(?%d,a%d)",ii,ii+2,ii);
197558 }else
197559#endif
197560 {
197561 sqlite3_str_appendf(p,zFormat: "a%d=?%d",ii,ii+2);
197562 }
197563 }
197564 sqlite3_str_appendf(p, zFormat: " WHERE rowid=?1");
197565 zSql = sqlite3_str_finish(p);
197566 if( zSql==0 ){
197567 rc = SQLITE_NOMEM;
197568 }else{
197569 rc = sqlite3_prepare_v3(db, zSql, nBytes: -1, prepFlags: f, ppStmt: &pRtree->pWriteAux, pzTail: 0);
197570 sqlite3_free(p: zSql);
197571 }
197572 }
197573 }
197574
197575 return rc;
197576}
197577
197578/*
197579** The second argument to this function contains the text of an SQL statement
197580** that returns a single integer value. The statement is compiled and executed
197581** using database connection db. If successful, the integer value returned
197582** is written to *piVal and SQLITE_OK returned. Otherwise, an SQLite error
197583** code is returned and the value of *piVal after returning is not defined.
197584*/
197585static int getIntFromStmt(sqlite3 *db, const char *zSql, int *piVal){
197586 int rc = SQLITE_NOMEM;
197587 if( zSql ){
197588 sqlite3_stmt *pStmt = 0;
197589 rc = sqlite3_prepare_v2(db, zSql, nBytes: -1, ppStmt: &pStmt, pzTail: 0);
197590 if( rc==SQLITE_OK ){
197591 if( SQLITE_ROW==sqlite3_step(pStmt) ){
197592 *piVal = sqlite3_column_int(pStmt, i: 0);
197593 }
197594 rc = sqlite3_finalize(pStmt);
197595 }
197596 }
197597 return rc;
197598}
197599
197600/*
197601** This function is called from within the xConnect() or xCreate() method to
197602** determine the node-size used by the rtree table being created or connected
197603** to. If successful, pRtree->iNodeSize is populated and SQLITE_OK returned.
197604** Otherwise, an SQLite error code is returned.
197605**
197606** If this function is being called as part of an xConnect(), then the rtree
197607** table already exists. In this case the node-size is determined by inspecting
197608** the root node of the tree.
197609**
197610** Otherwise, for an xCreate(), use 64 bytes less than the database page-size.
197611** This ensures that each node is stored on a single database page. If the
197612** database page-size is so large that more than RTREE_MAXCELLS entries
197613** would fit in a single node, use a smaller node-size.
197614*/
197615static int getNodeSize(
197616 sqlite3 *db, /* Database handle */
197617 Rtree *pRtree, /* Rtree handle */
197618 int isCreate, /* True for xCreate, false for xConnect */
197619 char **pzErr /* OUT: Error message, if any */
197620){
197621 int rc;
197622 char *zSql;
197623 if( isCreate ){
197624 int iPageSize = 0;
197625 zSql = sqlite3_mprintf(zFormat: "PRAGMA %Q.page_size", pRtree->zDb);
197626 rc = getIntFromStmt(db, zSql, piVal: &iPageSize);
197627 if( rc==SQLITE_OK ){
197628 pRtree->iNodeSize = iPageSize-64;
197629 if( (4+pRtree->nBytesPerCell*RTREE_MAXCELLS)<pRtree->iNodeSize ){
197630 pRtree->iNodeSize = 4+pRtree->nBytesPerCell*RTREE_MAXCELLS;
197631 }
197632 }else{
197633 *pzErr = sqlite3_mprintf(zFormat: "%s", sqlite3_errmsg(db));
197634 }
197635 }else{
197636 zSql = sqlite3_mprintf(
197637 zFormat: "SELECT length(data) FROM '%q'.'%q_node' WHERE nodeno = 1",
197638 pRtree->zDb, pRtree->zName
197639 );
197640 rc = getIntFromStmt(db, zSql, piVal: &pRtree->iNodeSize);
197641 if( rc!=SQLITE_OK ){
197642 *pzErr = sqlite3_mprintf(zFormat: "%s", sqlite3_errmsg(db));
197643 }else if( pRtree->iNodeSize<(512-64) ){
197644 rc = SQLITE_CORRUPT_VTAB;
197645 RTREE_IS_CORRUPT(pRtree);
197646 *pzErr = sqlite3_mprintf(zFormat: "undersize RTree blobs in \"%q_node\"",
197647 pRtree->zName);
197648 }
197649 }
197650
197651 sqlite3_free(p: zSql);
197652 return rc;
197653}
197654
197655/*
197656** Return the length of a token
197657*/
197658static int rtreeTokenLength(const char *z){
197659 int dummy = 0;
197660 return sqlite3GetToken((const unsigned char*)z,&dummy);
197661}
197662
197663/*
197664** This function is the implementation of both the xConnect and xCreate
197665** methods of the r-tree virtual table.
197666**
197667** argv[0] -> module name
197668** argv[1] -> database name
197669** argv[2] -> table name
197670** argv[...] -> column names...
197671*/
197672static int rtreeInit(
197673 sqlite3 *db, /* Database connection */
197674 void *pAux, /* One of the RTREE_COORD_* constants */
197675 int argc, const char *const*argv, /* Parameters to CREATE TABLE statement */
197676 sqlite3_vtab **ppVtab, /* OUT: New virtual table */
197677 char **pzErr, /* OUT: Error message, if any */
197678 int isCreate /* True for xCreate, false for xConnect */
197679){
197680 int rc = SQLITE_OK;
197681 Rtree *pRtree;
197682 int nDb; /* Length of string argv[1] */
197683 int nName; /* Length of string argv[2] */
197684 int eCoordType = (pAux ? RTREE_COORD_INT32 : RTREE_COORD_REAL32);
197685 sqlite3_str *pSql;
197686 char *zSql;
197687 int ii = 4;
197688 int iErr;
197689
197690 const char *aErrMsg[] = {
197691 0, /* 0 */
197692 "Wrong number of columns for an rtree table", /* 1 */
197693 "Too few columns for an rtree table", /* 2 */
197694 "Too many columns for an rtree table", /* 3 */
197695 "Auxiliary rtree columns must be last" /* 4 */
197696 };
197697
197698 assert( RTREE_MAX_AUX_COLUMN<256 ); /* Aux columns counted by a u8 */
197699 if( argc<6 || argc>RTREE_MAX_AUX_COLUMN+3 ){
197700 *pzErr = sqlite3_mprintf(zFormat: "%s", aErrMsg[2 + (argc>=6)]);
197701 return SQLITE_ERROR;
197702 }
197703
197704 sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
197705
197706 /* Allocate the sqlite3_vtab structure */
197707 nDb = (int)strlen(s: argv[1]);
197708 nName = (int)strlen(s: argv[2]);
197709 pRtree = (Rtree *)sqlite3_malloc64(n: sizeof(Rtree)+nDb+nName+2);
197710 if( !pRtree ){
197711 return SQLITE_NOMEM;
197712 }
197713 memset(s: pRtree, c: 0, n: sizeof(Rtree)+nDb+nName+2);
197714 pRtree->nBusy = 1;
197715 pRtree->base.pModule = &rtreeModule;
197716 pRtree->zDb = (char *)&pRtree[1];
197717 pRtree->zName = &pRtree->zDb[nDb+1];
197718 pRtree->eCoordType = (u8)eCoordType;
197719 memcpy(dest: pRtree->zDb, src: argv[1], n: nDb);
197720 memcpy(dest: pRtree->zName, src: argv[2], n: nName);
197721
197722
197723 /* Create/Connect to the underlying relational database schema. If
197724 ** that is successful, call sqlite3_declare_vtab() to configure
197725 ** the r-tree table schema.
197726 */
197727 pSql = sqlite3_str_new(db);
197728 sqlite3_str_appendf(p: pSql, zFormat: "CREATE TABLE x(%.*s INT",
197729 rtreeTokenLength(z: argv[3]), argv[3]);
197730 for(ii=4; ii<argc; ii++){
197731 const char *zArg = argv[ii];
197732 if( zArg[0]=='+' ){
197733 pRtree->nAux++;
197734 sqlite3_str_appendf(p: pSql, zFormat: ",%.*s", rtreeTokenLength(z: zArg+1), zArg+1);
197735 }else if( pRtree->nAux>0 ){
197736 break;
197737 }else{
197738 static const char *azFormat[] = {",%.*s REAL", ",%.*s INT"};
197739 pRtree->nDim2++;
197740 sqlite3_str_appendf(p: pSql, zFormat: azFormat[eCoordType],
197741 rtreeTokenLength(z: zArg), zArg);
197742 }
197743 }
197744 sqlite3_str_appendf(p: pSql, zFormat: ");");
197745 zSql = sqlite3_str_finish(p: pSql);
197746 if( !zSql ){
197747 rc = SQLITE_NOMEM;
197748 }else if( ii<argc ){
197749 *pzErr = sqlite3_mprintf(zFormat: "%s", aErrMsg[4]);
197750 rc = SQLITE_ERROR;
197751 }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zCreateTable: zSql)) ){
197752 *pzErr = sqlite3_mprintf(zFormat: "%s", sqlite3_errmsg(db));
197753 }
197754 sqlite3_free(p: zSql);
197755 if( rc ) goto rtreeInit_fail;
197756 pRtree->nDim = pRtree->nDim2/2;
197757 if( pRtree->nDim<1 ){
197758 iErr = 2;
197759 }else if( pRtree->nDim2>RTREE_MAX_DIMENSIONS*2 ){
197760 iErr = 3;
197761 }else if( pRtree->nDim2 % 2 ){
197762 iErr = 1;
197763 }else{
197764 iErr = 0;
197765 }
197766 if( iErr ){
197767 *pzErr = sqlite3_mprintf(zFormat: "%s", aErrMsg[iErr]);
197768 goto rtreeInit_fail;
197769 }
197770 pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
197771
197772 /* Figure out the node size to use. */
197773 rc = getNodeSize(db, pRtree, isCreate, pzErr);
197774 if( rc ) goto rtreeInit_fail;
197775 rc = rtreeSqlInit(pRtree, db, zDb: argv[1], zPrefix: argv[2], isCreate);
197776 if( rc ){
197777 *pzErr = sqlite3_mprintf(zFormat: "%s", sqlite3_errmsg(db));
197778 goto rtreeInit_fail;
197779 }
197780
197781 *ppVtab = (sqlite3_vtab *)pRtree;
197782 return SQLITE_OK;
197783
197784rtreeInit_fail:
197785 if( rc==SQLITE_OK ) rc = SQLITE_ERROR;
197786 assert( *ppVtab==0 );
197787 assert( pRtree->nBusy==1 );
197788 rtreeRelease(pRtree);
197789 return rc;
197790}
197791
197792
197793/*
197794** Implementation of a scalar function that decodes r-tree nodes to
197795** human readable strings. This can be used for debugging and analysis.
197796**
197797** The scalar function takes two arguments: (1) the number of dimensions
197798** to the rtree (between 1 and 5, inclusive) and (2) a blob of data containing
197799** an r-tree node. For a two-dimensional r-tree structure called "rt", to
197800** deserialize all nodes, a statement like:
197801**
197802** SELECT rtreenode(2, data) FROM rt_node;
197803**
197804** The human readable string takes the form of a Tcl list with one
197805** entry for each cell in the r-tree node. Each entry is itself a
197806** list, containing the 8-byte rowid/pageno followed by the
197807** <num-dimension>*2 coordinates.
197808*/
197809static void rtreenode(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
197810 RtreeNode node;
197811 Rtree tree;
197812 int ii;
197813 int nData;
197814 int errCode;
197815 sqlite3_str *pOut;
197816
197817 UNUSED_PARAMETER(nArg);
197818 memset(s: &node, c: 0, n: sizeof(RtreeNode));
197819 memset(s: &tree, c: 0, n: sizeof(Rtree));
197820 tree.nDim = (u8)sqlite3_value_int(pVal: apArg[0]);
197821 if( tree.nDim<1 || tree.nDim>5 ) return;
197822 tree.nDim2 = tree.nDim*2;
197823 tree.nBytesPerCell = 8 + 8 * tree.nDim;
197824 node.zData = (u8 *)sqlite3_value_blob(pVal: apArg[1]);
197825 if( node.zData==0 ) return;
197826 nData = sqlite3_value_bytes(pVal: apArg[1]);
197827 if( nData<4 ) return;
197828 if( nData<NCELL(&node)*tree.nBytesPerCell ) return;
197829
197830 pOut = sqlite3_str_new(db: 0);
197831 for(ii=0; ii<NCELL(&node); ii++){
197832 RtreeCell cell;
197833 int jj;
197834
197835 nodeGetCell(pRtree: &tree, pNode: &node, iCell: ii, pCell: &cell);
197836 if( ii>0 ) sqlite3_str_append(p: pOut, z: " ", N: 1);
197837 sqlite3_str_appendf(p: pOut, zFormat: "{%lld", cell.iRowid);
197838 for(jj=0; jj<tree.nDim2; jj++){
197839#ifndef SQLITE_RTREE_INT_ONLY
197840 sqlite3_str_appendf(p: pOut, zFormat: " %g", (double)cell.aCoord[jj].f);
197841#else
197842 sqlite3_str_appendf(pOut, " %d", cell.aCoord[jj].i);
197843#endif
197844 }
197845 sqlite3_str_append(p: pOut, z: "}", N: 1);
197846 }
197847 errCode = sqlite3_str_errcode(p: pOut);
197848 sqlite3_result_text(pCtx: ctx, z: sqlite3_str_finish(p: pOut), n: -1, xDel: sqlite3_free);
197849 sqlite3_result_error_code(pCtx: ctx, errCode);
197850}
197851
197852/* This routine implements an SQL function that returns the "depth" parameter
197853** from the front of a blob that is an r-tree node. For example:
197854**
197855** SELECT rtreedepth(data) FROM rt_node WHERE nodeno=1;
197856**
197857** The depth value is 0 for all nodes other than the root node, and the root
197858** node always has nodeno=1, so the example above is the primary use for this
197859** routine. This routine is intended for testing and analysis only.
197860*/
197861static void rtreedepth(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
197862 UNUSED_PARAMETER(nArg);
197863 if( sqlite3_value_type(pVal: apArg[0])!=SQLITE_BLOB
197864 || sqlite3_value_bytes(pVal: apArg[0])<2
197865
197866 ){
197867 sqlite3_result_error(pCtx: ctx, z: "Invalid argument to rtreedepth()", n: -1);
197868 }else{
197869 u8 *zBlob = (u8 *)sqlite3_value_blob(pVal: apArg[0]);
197870 if( zBlob ){
197871 sqlite3_result_int(pCtx: ctx, iVal: readInt16(p: zBlob));
197872 }else{
197873 sqlite3_result_error_nomem(pCtx: ctx);
197874 }
197875 }
197876}
197877
197878/*
197879** Context object passed between the various routines that make up the
197880** implementation of integrity-check function rtreecheck().
197881*/
197882typedef struct RtreeCheck RtreeCheck;
197883struct RtreeCheck {
197884 sqlite3 *db; /* Database handle */
197885 const char *zDb; /* Database containing rtree table */
197886 const char *zTab; /* Name of rtree table */
197887 int bInt; /* True for rtree_i32 table */
197888 int nDim; /* Number of dimensions for this rtree tbl */
197889 sqlite3_stmt *pGetNode; /* Statement used to retrieve nodes */
197890 sqlite3_stmt *aCheckMapping[2]; /* Statements to query %_parent/%_rowid */
197891 int nLeaf; /* Number of leaf cells in table */
197892 int nNonLeaf; /* Number of non-leaf cells in table */
197893 int rc; /* Return code */
197894 char *zReport; /* Message to report */
197895 int nErr; /* Number of lines in zReport */
197896};
197897
197898#define RTREE_CHECK_MAX_ERROR 100
197899
197900/*
197901** Reset SQL statement pStmt. If the sqlite3_reset() call returns an error,
197902** and RtreeCheck.rc==SQLITE_OK, set RtreeCheck.rc to the error code.
197903*/
197904static void rtreeCheckReset(RtreeCheck *pCheck, sqlite3_stmt *pStmt){
197905 int rc = sqlite3_reset(pStmt);
197906 if( pCheck->rc==SQLITE_OK ) pCheck->rc = rc;
197907}
197908
197909/*
197910** The second and subsequent arguments to this function are a format string
197911** and printf style arguments. This function formats the string and attempts
197912** to compile it as an SQL statement.
197913**
197914** If successful, a pointer to the new SQL statement is returned. Otherwise,
197915** NULL is returned and an error code left in RtreeCheck.rc.
197916*/
197917static sqlite3_stmt *rtreeCheckPrepare(
197918 RtreeCheck *pCheck, /* RtreeCheck object */
197919 const char *zFmt, ... /* Format string and trailing args */
197920){
197921 va_list ap;
197922 char *z;
197923 sqlite3_stmt *pRet = 0;
197924
197925 va_start(ap, zFmt);
197926 z = sqlite3_vmprintf(zFormat: zFmt, ap);
197927
197928 if( pCheck->rc==SQLITE_OK ){
197929 if( z==0 ){
197930 pCheck->rc = SQLITE_NOMEM;
197931 }else{
197932 pCheck->rc = sqlite3_prepare_v2(db: pCheck->db, zSql: z, nBytes: -1, ppStmt: &pRet, pzTail: 0);
197933 }
197934 }
197935
197936 sqlite3_free(p: z);
197937 va_end(ap);
197938 return pRet;
197939}
197940
197941/*
197942** The second and subsequent arguments to this function are a printf()
197943** style format string and arguments. This function formats the string and
197944** appends it to the report being accumuated in pCheck.
197945*/
197946static void rtreeCheckAppendMsg(RtreeCheck *pCheck, const char *zFmt, ...){
197947 va_list ap;
197948 va_start(ap, zFmt);
197949 if( pCheck->rc==SQLITE_OK && pCheck->nErr<RTREE_CHECK_MAX_ERROR ){
197950 char *z = sqlite3_vmprintf(zFormat: zFmt, ap);
197951 if( z==0 ){
197952 pCheck->rc = SQLITE_NOMEM;
197953 }else{
197954 pCheck->zReport = sqlite3_mprintf(zFormat: "%z%s%z",
197955 pCheck->zReport, (pCheck->zReport ? "\n" : ""), z
197956 );
197957 if( pCheck->zReport==0 ){
197958 pCheck->rc = SQLITE_NOMEM;
197959 }
197960 }
197961 pCheck->nErr++;
197962 }
197963 va_end(ap);
197964}
197965
197966/*
197967** This function is a no-op if there is already an error code stored
197968** in the RtreeCheck object indicated by the first argument. NULL is
197969** returned in this case.
197970**
197971** Otherwise, the contents of rtree table node iNode are loaded from
197972** the database and copied into a buffer obtained from sqlite3_malloc().
197973** If no error occurs, a pointer to the buffer is returned and (*pnNode)
197974** is set to the size of the buffer in bytes.
197975**
197976** Or, if an error does occur, NULL is returned and an error code left
197977** in the RtreeCheck object. The final value of *pnNode is undefined in
197978** this case.
197979*/
197980static u8 *rtreeCheckGetNode(RtreeCheck *pCheck, i64 iNode, int *pnNode){
197981 u8 *pRet = 0; /* Return value */
197982
197983 if( pCheck->rc==SQLITE_OK && pCheck->pGetNode==0 ){
197984 pCheck->pGetNode = rtreeCheckPrepare(pCheck,
197985 zFmt: "SELECT data FROM %Q.'%q_node' WHERE nodeno=?",
197986 pCheck->zDb, pCheck->zTab
197987 );
197988 }
197989
197990 if( pCheck->rc==SQLITE_OK ){
197991 sqlite3_bind_int64(pStmt: pCheck->pGetNode, i: 1, iValue: iNode);
197992 if( sqlite3_step(pStmt: pCheck->pGetNode)==SQLITE_ROW ){
197993 int nNode = sqlite3_column_bytes(pStmt: pCheck->pGetNode, i: 0);
197994 const u8 *pNode = (const u8*)sqlite3_column_blob(pStmt: pCheck->pGetNode, i: 0);
197995 pRet = sqlite3_malloc64(n: nNode);
197996 if( pRet==0 ){
197997 pCheck->rc = SQLITE_NOMEM;
197998 }else{
197999 memcpy(dest: pRet, src: pNode, n: nNode);
198000 *pnNode = nNode;
198001 }
198002 }
198003 rtreeCheckReset(pCheck, pStmt: pCheck->pGetNode);
198004 if( pCheck->rc==SQLITE_OK && pRet==0 ){
198005 rtreeCheckAppendMsg(pCheck, zFmt: "Node %lld missing from database", iNode);
198006 }
198007 }
198008
198009 return pRet;
198010}
198011
198012/*
198013** This function is used to check that the %_parent (if bLeaf==0) or %_rowid
198014** (if bLeaf==1) table contains a specified entry. The schemas of the
198015** two tables are:
198016**
198017** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
198018** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER, ...)
198019**
198020** In both cases, this function checks that there exists an entry with
198021** IPK value iKey and the second column set to iVal.
198022**
198023*/
198024static void rtreeCheckMapping(
198025 RtreeCheck *pCheck, /* RtreeCheck object */
198026 int bLeaf, /* True for a leaf cell, false for interior */
198027 i64 iKey, /* Key for mapping */
198028 i64 iVal /* Expected value for mapping */
198029){
198030 int rc;
198031 sqlite3_stmt *pStmt;
198032 const char *azSql[2] = {
198033 "SELECT parentnode FROM %Q.'%q_parent' WHERE nodeno=?1",
198034 "SELECT nodeno FROM %Q.'%q_rowid' WHERE rowid=?1"
198035 };
198036
198037 assert( bLeaf==0 || bLeaf==1 );
198038 if( pCheck->aCheckMapping[bLeaf]==0 ){
198039 pCheck->aCheckMapping[bLeaf] = rtreeCheckPrepare(pCheck,
198040 zFmt: azSql[bLeaf], pCheck->zDb, pCheck->zTab
198041 );
198042 }
198043 if( pCheck->rc!=SQLITE_OK ) return;
198044
198045 pStmt = pCheck->aCheckMapping[bLeaf];
198046 sqlite3_bind_int64(pStmt, i: 1, iValue: iKey);
198047 rc = sqlite3_step(pStmt);
198048 if( rc==SQLITE_DONE ){
198049 rtreeCheckAppendMsg(pCheck, zFmt: "Mapping (%lld -> %lld) missing from %s table",
198050 iKey, iVal, (bLeaf ? "%_rowid" : "%_parent")
198051 );
198052 }else if( rc==SQLITE_ROW ){
198053 i64 ii = sqlite3_column_int64(pStmt, i: 0);
198054 if( ii!=iVal ){
198055 rtreeCheckAppendMsg(pCheck,
198056 zFmt: "Found (%lld -> %lld) in %s table, expected (%lld -> %lld)",
198057 iKey, ii, (bLeaf ? "%_rowid" : "%_parent"), iKey, iVal
198058 );
198059 }
198060 }
198061 rtreeCheckReset(pCheck, pStmt);
198062}
198063
198064/*
198065** Argument pCell points to an array of coordinates stored on an rtree page.
198066** This function checks that the coordinates are internally consistent (no
198067** x1>x2 conditions) and adds an error message to the RtreeCheck object
198068** if they are not.
198069**
198070** Additionally, if pParent is not NULL, then it is assumed to point to
198071** the array of coordinates on the parent page that bound the page
198072** containing pCell. In this case it is also verified that the two
198073** sets of coordinates are mutually consistent and an error message added
198074** to the RtreeCheck object if they are not.
198075*/
198076static void rtreeCheckCellCoord(
198077 RtreeCheck *pCheck,
198078 i64 iNode, /* Node id to use in error messages */
198079 int iCell, /* Cell number to use in error messages */
198080 u8 *pCell, /* Pointer to cell coordinates */
198081 u8 *pParent /* Pointer to parent coordinates */
198082){
198083 RtreeCoord c1, c2;
198084 RtreeCoord p1, p2;
198085 int i;
198086
198087 for(i=0; i<pCheck->nDim; i++){
198088 readCoord(p: &pCell[4*2*i], pCoord: &c1);
198089 readCoord(p: &pCell[4*(2*i + 1)], pCoord: &c2);
198090
198091 /* printf("%e, %e\n", c1.u.f, c2.u.f); */
198092 if( pCheck->bInt ? c1.i>c2.i : c1.f>c2.f ){
198093 rtreeCheckAppendMsg(pCheck,
198094 zFmt: "Dimension %d of cell %d on node %lld is corrupt", i, iCell, iNode
198095 );
198096 }
198097
198098 if( pParent ){
198099 readCoord(p: &pParent[4*2*i], pCoord: &p1);
198100 readCoord(p: &pParent[4*(2*i + 1)], pCoord: &p2);
198101
198102 if( (pCheck->bInt ? c1.i<p1.i : c1.f<p1.f)
198103 || (pCheck->bInt ? c2.i>p2.i : c2.f>p2.f)
198104 ){
198105 rtreeCheckAppendMsg(pCheck,
198106 zFmt: "Dimension %d of cell %d on node %lld is corrupt relative to parent"
198107 , i, iCell, iNode
198108 );
198109 }
198110 }
198111 }
198112}
198113
198114/*
198115** Run rtreecheck() checks on node iNode, which is at depth iDepth within
198116** the r-tree structure. Argument aParent points to the array of coordinates
198117** that bound node iNode on the parent node.
198118**
198119** If any problems are discovered, an error message is appended to the
198120** report accumulated in the RtreeCheck object.
198121*/
198122static void rtreeCheckNode(
198123 RtreeCheck *pCheck,
198124 int iDepth, /* Depth of iNode (0==leaf) */
198125 u8 *aParent, /* Buffer containing parent coords */
198126 i64 iNode /* Node to check */
198127){
198128 u8 *aNode = 0;
198129 int nNode = 0;
198130
198131 assert( iNode==1 || aParent!=0 );
198132 assert( pCheck->nDim>0 );
198133
198134 aNode = rtreeCheckGetNode(pCheck, iNode, pnNode: &nNode);
198135 if( aNode ){
198136 if( nNode<4 ){
198137 rtreeCheckAppendMsg(pCheck,
198138 zFmt: "Node %lld is too small (%d bytes)", iNode, nNode
198139 );
198140 }else{
198141 int nCell; /* Number of cells on page */
198142 int i; /* Used to iterate through cells */
198143 if( aParent==0 ){
198144 iDepth = readInt16(p: aNode);
198145 if( iDepth>RTREE_MAX_DEPTH ){
198146 rtreeCheckAppendMsg(pCheck, zFmt: "Rtree depth out of range (%d)", iDepth);
198147 sqlite3_free(p: aNode);
198148 return;
198149 }
198150 }
198151 nCell = readInt16(p: &aNode[2]);
198152 if( (4 + nCell*(8 + pCheck->nDim*2*4))>nNode ){
198153 rtreeCheckAppendMsg(pCheck,
198154 zFmt: "Node %lld is too small for cell count of %d (%d bytes)",
198155 iNode, nCell, nNode
198156 );
198157 }else{
198158 for(i=0; i<nCell; i++){
198159 u8 *pCell = &aNode[4 + i*(8 + pCheck->nDim*2*4)];
198160 i64 iVal = readInt64(p: pCell);
198161 rtreeCheckCellCoord(pCheck, iNode, iCell: i, pCell: &pCell[8], pParent: aParent);
198162
198163 if( iDepth>0 ){
198164 rtreeCheckMapping(pCheck, bLeaf: 0, iKey: iVal, iVal: iNode);
198165 rtreeCheckNode(pCheck, iDepth: iDepth-1, aParent: &pCell[8], iNode: iVal);
198166 pCheck->nNonLeaf++;
198167 }else{
198168 rtreeCheckMapping(pCheck, bLeaf: 1, iKey: iVal, iVal: iNode);
198169 pCheck->nLeaf++;
198170 }
198171 }
198172 }
198173 }
198174 sqlite3_free(p: aNode);
198175 }
198176}
198177
198178/*
198179** The second argument to this function must be either "_rowid" or
198180** "_parent". This function checks that the number of entries in the
198181** %_rowid or %_parent table is exactly nExpect. If not, it adds
198182** an error message to the report in the RtreeCheck object indicated
198183** by the first argument.
198184*/
198185static void rtreeCheckCount(RtreeCheck *pCheck, const char *zTbl, i64 nExpect){
198186 if( pCheck->rc==SQLITE_OK ){
198187 sqlite3_stmt *pCount;
198188 pCount = rtreeCheckPrepare(pCheck, zFmt: "SELECT count(*) FROM %Q.'%q%s'",
198189 pCheck->zDb, pCheck->zTab, zTbl
198190 );
198191 if( pCount ){
198192 if( sqlite3_step(pStmt: pCount)==SQLITE_ROW ){
198193 i64 nActual = sqlite3_column_int64(pStmt: pCount, i: 0);
198194 if( nActual!=nExpect ){
198195 rtreeCheckAppendMsg(pCheck, zFmt: "Wrong number of entries in %%%s table"
198196 " - expected %lld, actual %lld" , zTbl, nExpect, nActual
198197 );
198198 }
198199 }
198200 pCheck->rc = sqlite3_finalize(pStmt: pCount);
198201 }
198202 }
198203}
198204
198205/*
198206** This function does the bulk of the work for the rtree integrity-check.
198207** It is called by rtreecheck(), which is the SQL function implementation.
198208*/
198209static int rtreeCheckTable(
198210 sqlite3 *db, /* Database handle to access db through */
198211 const char *zDb, /* Name of db ("main", "temp" etc.) */
198212 const char *zTab, /* Name of rtree table to check */
198213 char **pzReport /* OUT: sqlite3_malloc'd report text */
198214){
198215 RtreeCheck check; /* Common context for various routines */
198216 sqlite3_stmt *pStmt = 0; /* Used to find column count of rtree table */
198217 int bEnd = 0; /* True if transaction should be closed */
198218 int nAux = 0; /* Number of extra columns. */
198219
198220 /* Initialize the context object */
198221 memset(s: &check, c: 0, n: sizeof(check));
198222 check.db = db;
198223 check.zDb = zDb;
198224 check.zTab = zTab;
198225
198226 /* If there is not already an open transaction, open one now. This is
198227 ** to ensure that the queries run as part of this integrity-check operate
198228 ** on a consistent snapshot. */
198229 if( sqlite3_get_autocommit(db) ){
198230 check.rc = sqlite3_exec(db, zSql: "BEGIN", xCallback: 0, pArg: 0, pzErrMsg: 0);
198231 bEnd = 1;
198232 }
198233
198234 /* Find the number of auxiliary columns */
198235 if( check.rc==SQLITE_OK ){
198236 pStmt = rtreeCheckPrepare(pCheck: &check, zFmt: "SELECT * FROM %Q.'%q_rowid'", zDb, zTab);
198237 if( pStmt ){
198238 nAux = sqlite3_column_count(pStmt) - 2;
198239 sqlite3_finalize(pStmt);
198240 }else
198241 if( check.rc!=SQLITE_NOMEM ){
198242 check.rc = SQLITE_OK;
198243 }
198244 }
198245
198246 /* Find number of dimensions in the rtree table. */
198247 pStmt = rtreeCheckPrepare(pCheck: &check, zFmt: "SELECT * FROM %Q.%Q", zDb, zTab);
198248 if( pStmt ){
198249 int rc;
198250 check.nDim = (sqlite3_column_count(pStmt) - 1 - nAux) / 2;
198251 if( check.nDim<1 ){
198252 rtreeCheckAppendMsg(pCheck: &check, zFmt: "Schema corrupt or not an rtree");
198253 }else if( SQLITE_ROW==sqlite3_step(pStmt) ){
198254 check.bInt = (sqlite3_column_type(pStmt, i: 1)==SQLITE_INTEGER);
198255 }
198256 rc = sqlite3_finalize(pStmt);
198257 if( rc!=SQLITE_CORRUPT ) check.rc = rc;
198258 }
198259
198260 /* Do the actual integrity-check */
198261 if( check.nDim>=1 ){
198262 if( check.rc==SQLITE_OK ){
198263 rtreeCheckNode(pCheck: &check, iDepth: 0, aParent: 0, iNode: 1);
198264 }
198265 rtreeCheckCount(pCheck: &check, zTbl: "_rowid", nExpect: check.nLeaf);
198266 rtreeCheckCount(pCheck: &check, zTbl: "_parent", nExpect: check.nNonLeaf);
198267 }
198268
198269 /* Finalize SQL statements used by the integrity-check */
198270 sqlite3_finalize(pStmt: check.pGetNode);
198271 sqlite3_finalize(pStmt: check.aCheckMapping[0]);
198272 sqlite3_finalize(pStmt: check.aCheckMapping[1]);
198273
198274 /* If one was opened, close the transaction */
198275 if( bEnd ){
198276 int rc = sqlite3_exec(db, zSql: "END", xCallback: 0, pArg: 0, pzErrMsg: 0);
198277 if( check.rc==SQLITE_OK ) check.rc = rc;
198278 }
198279 *pzReport = check.zReport;
198280 return check.rc;
198281}
198282
198283/*
198284** Usage:
198285**
198286** rtreecheck(<rtree-table>);
198287** rtreecheck(<database>, <rtree-table>);
198288**
198289** Invoking this SQL function runs an integrity-check on the named rtree
198290** table. The integrity-check verifies the following:
198291**
198292** 1. For each cell in the r-tree structure (%_node table), that:
198293**
198294** a) for each dimension, (coord1 <= coord2).
198295**
198296** b) unless the cell is on the root node, that the cell is bounded
198297** by the parent cell on the parent node.
198298**
198299** c) for leaf nodes, that there is an entry in the %_rowid
198300** table corresponding to the cell's rowid value that
198301** points to the correct node.
198302**
198303** d) for cells on non-leaf nodes, that there is an entry in the
198304** %_parent table mapping from the cell's child node to the
198305** node that it resides on.
198306**
198307** 2. That there are the same number of entries in the %_rowid table
198308** as there are leaf cells in the r-tree structure, and that there
198309** is a leaf cell that corresponds to each entry in the %_rowid table.
198310**
198311** 3. That there are the same number of entries in the %_parent table
198312** as there are non-leaf cells in the r-tree structure, and that
198313** there is a non-leaf cell that corresponds to each entry in the
198314** %_parent table.
198315*/
198316static void rtreecheck(
198317 sqlite3_context *ctx,
198318 int nArg,
198319 sqlite3_value **apArg
198320){
198321 if( nArg!=1 && nArg!=2 ){
198322 sqlite3_result_error(pCtx: ctx,
198323 z: "wrong number of arguments to function rtreecheck()", n: -1
198324 );
198325 }else{
198326 int rc;
198327 char *zReport = 0;
198328 const char *zDb = (const char*)sqlite3_value_text(pVal: apArg[0]);
198329 const char *zTab;
198330 if( nArg==1 ){
198331 zTab = zDb;
198332 zDb = "main";
198333 }else{
198334 zTab = (const char*)sqlite3_value_text(pVal: apArg[1]);
198335 }
198336 rc = rtreeCheckTable(db: sqlite3_context_db_handle(p: ctx), zDb, zTab, pzReport: &zReport);
198337 if( rc==SQLITE_OK ){
198338 sqlite3_result_text(pCtx: ctx, z: zReport ? zReport : "ok", n: -1, SQLITE_TRANSIENT);
198339 }else{
198340 sqlite3_result_error_code(pCtx: ctx, errCode: rc);
198341 }
198342 sqlite3_free(p: zReport);
198343 }
198344}
198345
198346/* Conditionally include the geopoly code */
198347#ifdef SQLITE_ENABLE_GEOPOLY
198348/************** Include geopoly.c in the middle of rtree.c *******************/
198349/************** Begin file geopoly.c *****************************************/
198350/*
198351** 2018-05-25
198352**
198353** The author disclaims copyright to this source code. In place of
198354** a legal notice, here is a blessing:
198355**
198356** May you do good and not evil.
198357** May you find forgiveness for yourself and forgive others.
198358** May you share freely, never taking more than you give.
198359**
198360******************************************************************************
198361**
198362** This file implements an alternative R-Tree virtual table that
198363** uses polygons to express the boundaries of 2-dimensional objects.
198364**
198365** This file is #include-ed onto the end of "rtree.c" so that it has
198366** access to all of the R-Tree internals.
198367*/
198368/* #include <stdlib.h> */
198369
198370/* Enable -DGEOPOLY_ENABLE_DEBUG for debugging facilities */
198371#ifdef GEOPOLY_ENABLE_DEBUG
198372 static int geo_debug = 0;
198373# define GEODEBUG(X) if(geo_debug)printf X
198374#else
198375# define GEODEBUG(X)
198376#endif
198377
198378#ifndef JSON_NULL /* The following stuff repeats things found in json1 */
198379/*
198380** Versions of isspace(), isalnum() and isdigit() to which it is safe
198381** to pass signed char values.
198382*/
198383#ifdef sqlite3Isdigit
198384 /* Use the SQLite core versions if this routine is part of the
198385 ** SQLite amalgamation */
198386# define safe_isdigit(x) sqlite3Isdigit(x)
198387# define safe_isalnum(x) sqlite3Isalnum(x)
198388# define safe_isxdigit(x) sqlite3Isxdigit(x)
198389#else
198390 /* Use the standard library for separate compilation */
198391#include <ctype.h> /* amalgamator: keep */
198392# define safe_isdigit(x) isdigit((unsigned char)(x))
198393# define safe_isalnum(x) isalnum((unsigned char)(x))
198394# define safe_isxdigit(x) isxdigit((unsigned char)(x))
198395#endif
198396
198397/*
198398** Growing our own isspace() routine this way is twice as fast as
198399** the library isspace() function.
198400*/
198401static const char geopolyIsSpace[] = {
198402 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
198403 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
198404 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
198405 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
198406 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
198407 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
198408 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
198409 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
198410 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
198411 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
198412 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
198413 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
198414 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
198415 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
198416 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
198417 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
198418};
198419#define safe_isspace(x) (geopolyIsSpace[(unsigned char)x])
198420#endif /* JSON NULL - back to original code */
198421
198422/* Compiler and version */
198423#ifndef GCC_VERSION
198424#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
198425# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
198426#else
198427# define GCC_VERSION 0
198428#endif
198429#endif
198430#ifndef MSVC_VERSION
198431#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
198432# define MSVC_VERSION _MSC_VER
198433#else
198434# define MSVC_VERSION 0
198435#endif
198436#endif
198437
198438/* Datatype for coordinates
198439*/
198440typedef float GeoCoord;
198441
198442/*
198443** Internal representation of a polygon.
198444**
198445** The polygon consists of a sequence of vertexes. There is a line
198446** segment between each pair of vertexes, and one final segment from
198447** the last vertex back to the first. (This differs from the GeoJSON
198448** standard in which the final vertex is a repeat of the first.)
198449**
198450** The polygon follows the right-hand rule. The area to the right of
198451** each segment is "outside" and the area to the left is "inside".
198452**
198453** The on-disk representation consists of a 4-byte header followed by
198454** the values. The 4-byte header is:
198455**
198456** encoding (1 byte) 0=big-endian, 1=little-endian
198457** nvertex (3 bytes) Number of vertexes as a big-endian integer
198458**
198459** Enough space is allocated for 4 coordinates, to work around over-zealous
198460** warnings coming from some compiler (notably, clang). In reality, the size
198461** of each GeoPoly memory allocate is adjusted as necessary so that the
198462** GeoPoly.a[] array at the end is the appropriate size.
198463*/
198464typedef struct GeoPoly GeoPoly;
198465struct GeoPoly {
198466 int nVertex; /* Number of vertexes */
198467 unsigned char hdr[4]; /* Header for on-disk representation */
198468 GeoCoord a[8]; /* 2*nVertex values. X (longitude) first, then Y */
198469};
198470
198471/* The size of a memory allocation needed for a GeoPoly object sufficient
198472** to hold N coordinate pairs.
198473*/
198474#define GEOPOLY_SZ(N) (sizeof(GeoPoly) + sizeof(GeoCoord)*2*((N)-4))
198475
198476/* Macros to access coordinates of a GeoPoly.
198477** We have to use these macros, rather than just say p->a[i] in order
198478** to silence (incorrect) UBSAN warnings if the array index is too large.
198479*/
198480#define GeoX(P,I) (((GeoCoord*)(P)->a)[(I)*2])
198481#define GeoY(P,I) (((GeoCoord*)(P)->a)[(I)*2+1])
198482
198483
198484/*
198485** State of a parse of a GeoJSON input.
198486*/
198487typedef struct GeoParse GeoParse;
198488struct GeoParse {
198489 const unsigned char *z; /* Unparsed input */
198490 int nVertex; /* Number of vertexes in a[] */
198491 int nAlloc; /* Space allocated to a[] */
198492 int nErr; /* Number of errors encountered */
198493 GeoCoord *a; /* Array of vertexes. From sqlite3_malloc64() */
198494};
198495
198496/* Do a 4-byte byte swap */
198497static void geopolySwab32(unsigned char *a){
198498 unsigned char t = a[0];
198499 a[0] = a[3];
198500 a[3] = t;
198501 t = a[1];
198502 a[1] = a[2];
198503 a[2] = t;
198504}
198505
198506/* Skip whitespace. Return the next non-whitespace character. */
198507static char geopolySkipSpace(GeoParse *p){
198508 while( safe_isspace(p->z[0]) ) p->z++;
198509 return p->z[0];
198510}
198511
198512/* Parse out a number. Write the value into *pVal if pVal!=0.
198513** return non-zero on success and zero if the next token is not a number.
198514*/
198515static int geopolyParseNumber(GeoParse *p, GeoCoord *pVal){
198516 char c = geopolySkipSpace(p);
198517 const unsigned char *z = p->z;
198518 int j = 0;
198519 int seenDP = 0;
198520 int seenE = 0;
198521 if( c=='-' ){
198522 j = 1;
198523 c = z[j];
198524 }
198525 if( c=='0' && z[j+1]>='0' && z[j+1]<='9' ) return 0;
198526 for(;; j++){
198527 c = z[j];
198528 if( safe_isdigit(c) ) continue;
198529 if( c=='.' ){
198530 if( z[j-1]=='-' ) return 0;
198531 if( seenDP ) return 0;
198532 seenDP = 1;
198533 continue;
198534 }
198535 if( c=='e' || c=='E' ){
198536 if( z[j-1]<'0' ) return 0;
198537 if( seenE ) return -1;
198538 seenDP = seenE = 1;
198539 c = z[j+1];
198540 if( c=='+' || c=='-' ){
198541 j++;
198542 c = z[j+1];
198543 }
198544 if( c<'0' || c>'9' ) return 0;
198545 continue;
198546 }
198547 break;
198548 }
198549 if( z[j-1]<'0' ) return 0;
198550 if( pVal ){
198551#ifdef SQLITE_AMALGAMATION
198552 /* The sqlite3AtoF() routine is much much faster than atof(), if it
198553 ** is available */
198554 double r;
198555 (void)sqlite3AtoF((const char*)p->z, &r, j, SQLITE_UTF8);
198556 *pVal = r;
198557#else
198558 *pVal = (GeoCoord)atof((const char*)p->z);
198559#endif
198560 }
198561 p->z += j;
198562 return 1;
198563}
198564
198565/*
198566** If the input is a well-formed JSON array of coordinates with at least
198567** four coordinates and where each coordinate is itself a two-value array,
198568** then convert the JSON into a GeoPoly object and return a pointer to
198569** that object.
198570**
198571** If any error occurs, return NULL.
198572*/
198573static GeoPoly *geopolyParseJson(const unsigned char *z, int *pRc){
198574 GeoParse s;
198575 int rc = SQLITE_OK;
198576 memset(&s, 0, sizeof(s));
198577 s.z = z;
198578 if( geopolySkipSpace(&s)=='[' ){
198579 s.z++;
198580 while( geopolySkipSpace(&s)=='[' ){
198581 int ii = 0;
198582 char c;
198583 s.z++;
198584 if( s.nVertex>=s.nAlloc ){
198585 GeoCoord *aNew;
198586 s.nAlloc = s.nAlloc*2 + 16;
198587 aNew = sqlite3_realloc64(s.a, s.nAlloc*sizeof(GeoCoord)*2 );
198588 if( aNew==0 ){
198589 rc = SQLITE_NOMEM;
198590 s.nErr++;
198591 break;
198592 }
198593 s.a = aNew;
198594 }
198595 while( geopolyParseNumber(&s, ii<=1 ? &s.a[s.nVertex*2+ii] : 0) ){
198596 ii++;
198597 if( ii==2 ) s.nVertex++;
198598 c = geopolySkipSpace(&s);
198599 s.z++;
198600 if( c==',' ) continue;
198601 if( c==']' && ii>=2 ) break;
198602 s.nErr++;
198603 rc = SQLITE_ERROR;
198604 goto parse_json_err;
198605 }
198606 if( geopolySkipSpace(&s)==',' ){
198607 s.z++;
198608 continue;
198609 }
198610 break;
198611 }
198612 if( geopolySkipSpace(&s)==']'
198613 && s.nVertex>=4
198614 && s.a[0]==s.a[s.nVertex*2-2]
198615 && s.a[1]==s.a[s.nVertex*2-1]
198616 && (s.z++, geopolySkipSpace(&s)==0)
198617 ){
198618 GeoPoly *pOut;
198619 int x = 1;
198620 s.nVertex--; /* Remove the redundant vertex at the end */
198621 pOut = sqlite3_malloc64( GEOPOLY_SZ((sqlite3_int64)s.nVertex) );
198622 x = 1;
198623 if( pOut==0 ) goto parse_json_err;
198624 pOut->nVertex = s.nVertex;
198625 memcpy(pOut->a, s.a, s.nVertex*2*sizeof(GeoCoord));
198626 pOut->hdr[0] = *(unsigned char*)&x;
198627 pOut->hdr[1] = (s.nVertex>>16)&0xff;
198628 pOut->hdr[2] = (s.nVertex>>8)&0xff;
198629 pOut->hdr[3] = s.nVertex&0xff;
198630 sqlite3_free(s.a);
198631 if( pRc ) *pRc = SQLITE_OK;
198632 return pOut;
198633 }else{
198634 s.nErr++;
198635 rc = SQLITE_ERROR;
198636 }
198637 }
198638parse_json_err:
198639 if( pRc ) *pRc = rc;
198640 sqlite3_free(s.a);
198641 return 0;
198642}
198643
198644/*
198645** Given a function parameter, try to interpret it as a polygon, either
198646** in the binary format or JSON text. Compute a GeoPoly object and
198647** return a pointer to that object. Or if the input is not a well-formed
198648** polygon, put an error message in sqlite3_context and return NULL.
198649*/
198650static GeoPoly *geopolyFuncParam(
198651 sqlite3_context *pCtx, /* Context for error messages */
198652 sqlite3_value *pVal, /* The value to decode */
198653 int *pRc /* Write error here */
198654){
198655 GeoPoly *p = 0;
198656 int nByte;
198657 testcase( pCtx==0 );
198658 if( sqlite3_value_type(pVal)==SQLITE_BLOB
198659 && (nByte = sqlite3_value_bytes(pVal))>=(4+6*sizeof(GeoCoord))
198660 ){
198661 const unsigned char *a = sqlite3_value_blob(pVal);
198662 int nVertex;
198663 if( a==0 ){
198664 if( pCtx ) sqlite3_result_error_nomem(pCtx);
198665 return 0;
198666 }
198667 nVertex = (a[1]<<16) + (a[2]<<8) + a[3];
198668 if( (a[0]==0 || a[0]==1)
198669 && (nVertex*2*sizeof(GeoCoord) + 4)==(unsigned int)nByte
198670 ){
198671 p = sqlite3_malloc64( sizeof(*p) + (nVertex-1)*2*sizeof(GeoCoord) );
198672 if( p==0 ){
198673 if( pRc ) *pRc = SQLITE_NOMEM;
198674 if( pCtx ) sqlite3_result_error_nomem(pCtx);
198675 }else{
198676 int x = 1;
198677 p->nVertex = nVertex;
198678 memcpy(p->hdr, a, nByte);
198679 if( a[0] != *(unsigned char*)&x ){
198680 int ii;
198681 for(ii=0; ii<nVertex; ii++){
198682 geopolySwab32((unsigned char*)&GeoX(p,ii));
198683 geopolySwab32((unsigned char*)&GeoY(p,ii));
198684 }
198685 p->hdr[0] ^= 1;
198686 }
198687 }
198688 }
198689 if( pRc ) *pRc = SQLITE_OK;
198690 return p;
198691 }else if( sqlite3_value_type(pVal)==SQLITE_TEXT ){
198692 const unsigned char *zJson = sqlite3_value_text(pVal);
198693 if( zJson==0 ){
198694 if( pRc ) *pRc = SQLITE_NOMEM;
198695 return 0;
198696 }
198697 return geopolyParseJson(zJson, pRc);
198698 }else{
198699 if( pRc ) *pRc = SQLITE_ERROR;
198700 return 0;
198701 }
198702}
198703
198704/*
198705** Implementation of the geopoly_blob(X) function.
198706**
198707** If the input is a well-formed Geopoly BLOB or JSON string
198708** then return the BLOB representation of the polygon. Otherwise
198709** return NULL.
198710*/
198711static void geopolyBlobFunc(
198712 sqlite3_context *context,
198713 int argc,
198714 sqlite3_value **argv
198715){
198716 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
198717 if( p ){
198718 sqlite3_result_blob(context, p->hdr,
198719 4+8*p->nVertex, SQLITE_TRANSIENT);
198720 sqlite3_free(p);
198721 }
198722}
198723
198724/*
198725** SQL function: geopoly_json(X)
198726**
198727** Interpret X as a polygon and render it as a JSON array
198728** of coordinates. Or, if X is not a valid polygon, return NULL.
198729*/
198730static void geopolyJsonFunc(
198731 sqlite3_context *context,
198732 int argc,
198733 sqlite3_value **argv
198734){
198735 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
198736 if( p ){
198737 sqlite3 *db = sqlite3_context_db_handle(context);
198738 sqlite3_str *x = sqlite3_str_new(db);
198739 int i;
198740 sqlite3_str_append(x, "[", 1);
198741 for(i=0; i<p->nVertex; i++){
198742 sqlite3_str_appendf(x, "[%!g,%!g],", GeoX(p,i), GeoY(p,i));
198743 }
198744 sqlite3_str_appendf(x, "[%!g,%!g]]", GeoX(p,0), GeoY(p,0));
198745 sqlite3_result_text(context, sqlite3_str_finish(x), -1, sqlite3_free);
198746 sqlite3_free(p);
198747 }
198748}
198749
198750/*
198751** SQL function: geopoly_svg(X, ....)
198752**
198753** Interpret X as a polygon and render it as a SVG <polyline>.
198754** Additional arguments are added as attributes to the <polyline>.
198755*/
198756static void geopolySvgFunc(
198757 sqlite3_context *context,
198758 int argc,
198759 sqlite3_value **argv
198760){
198761 GeoPoly *p;
198762 if( argc<1 ) return;
198763 p = geopolyFuncParam(context, argv[0], 0);
198764 if( p ){
198765 sqlite3 *db = sqlite3_context_db_handle(context);
198766 sqlite3_str *x = sqlite3_str_new(db);
198767 int i;
198768 char cSep = '\'';
198769 sqlite3_str_appendf(x, "<polyline points=");
198770 for(i=0; i<p->nVertex; i++){
198771 sqlite3_str_appendf(x, "%c%g,%g", cSep, GeoX(p,i), GeoY(p,i));
198772 cSep = ' ';
198773 }
198774 sqlite3_str_appendf(x, " %g,%g'", GeoX(p,0), GeoY(p,0));
198775 for(i=1; i<argc; i++){
198776 const char *z = (const char*)sqlite3_value_text(argv[i]);
198777 if( z && z[0] ){
198778 sqlite3_str_appendf(x, " %s", z);
198779 }
198780 }
198781 sqlite3_str_appendf(x, "></polyline>");
198782 sqlite3_result_text(context, sqlite3_str_finish(x), -1, sqlite3_free);
198783 sqlite3_free(p);
198784 }
198785}
198786
198787/*
198788** SQL Function: geopoly_xform(poly, A, B, C, D, E, F)
198789**
198790** Transform and/or translate a polygon as follows:
198791**
198792** x1 = A*x0 + B*y0 + E
198793** y1 = C*x0 + D*y0 + F
198794**
198795** For a translation:
198796**
198797** geopoly_xform(poly, 1, 0, 0, 1, x-offset, y-offset)
198798**
198799** Rotate by R around the point (0,0):
198800**
198801** geopoly_xform(poly, cos(R), sin(R), -sin(R), cos(R), 0, 0)
198802*/
198803static void geopolyXformFunc(
198804 sqlite3_context *context,
198805 int argc,
198806 sqlite3_value **argv
198807){
198808 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
198809 double A = sqlite3_value_double(argv[1]);
198810 double B = sqlite3_value_double(argv[2]);
198811 double C = sqlite3_value_double(argv[3]);
198812 double D = sqlite3_value_double(argv[4]);
198813 double E = sqlite3_value_double(argv[5]);
198814 double F = sqlite3_value_double(argv[6]);
198815 GeoCoord x1, y1, x0, y0;
198816 int ii;
198817 if( p ){
198818 for(ii=0; ii<p->nVertex; ii++){
198819 x0 = GeoX(p,ii);
198820 y0 = GeoY(p,ii);
198821 x1 = (GeoCoord)(A*x0 + B*y0 + E);
198822 y1 = (GeoCoord)(C*x0 + D*y0 + F);
198823 GeoX(p,ii) = x1;
198824 GeoY(p,ii) = y1;
198825 }
198826 sqlite3_result_blob(context, p->hdr,
198827 4+8*p->nVertex, SQLITE_TRANSIENT);
198828 sqlite3_free(p);
198829 }
198830}
198831
198832/*
198833** Compute the area enclosed by the polygon.
198834**
198835** This routine can also be used to detect polygons that rotate in
198836** the wrong direction. Polygons are suppose to be counter-clockwise (CCW).
198837** This routine returns a negative value for clockwise (CW) polygons.
198838*/
198839static double geopolyArea(GeoPoly *p){
198840 double rArea = 0.0;
198841 int ii;
198842 for(ii=0; ii<p->nVertex-1; ii++){
198843 rArea += (GeoX(p,ii) - GeoX(p,ii+1)) /* (x0 - x1) */
198844 * (GeoY(p,ii) + GeoY(p,ii+1)) /* (y0 + y1) */
198845 * 0.5;
198846 }
198847 rArea += (GeoX(p,ii) - GeoX(p,0)) /* (xN - x0) */
198848 * (GeoY(p,ii) + GeoY(p,0)) /* (yN + y0) */
198849 * 0.5;
198850 return rArea;
198851}
198852
198853/*
198854** Implementation of the geopoly_area(X) function.
198855**
198856** If the input is a well-formed Geopoly BLOB then return the area
198857** enclosed by the polygon. If the polygon circulates clockwise instead
198858** of counterclockwise (as it should) then return the negative of the
198859** enclosed area. Otherwise return NULL.
198860*/
198861static void geopolyAreaFunc(
198862 sqlite3_context *context,
198863 int argc,
198864 sqlite3_value **argv
198865){
198866 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
198867 if( p ){
198868 sqlite3_result_double(context, geopolyArea(p));
198869 sqlite3_free(p);
198870 }
198871}
198872
198873/*
198874** Implementation of the geopoly_ccw(X) function.
198875**
198876** If the rotation of polygon X is clockwise (incorrect) instead of
198877** counter-clockwise (the correct winding order according to RFC7946)
198878** then reverse the order of the vertexes in polygon X.
198879**
198880** In other words, this routine returns a CCW polygon regardless of the
198881** winding order of its input.
198882**
198883** Use this routine to sanitize historical inputs that that sometimes
198884** contain polygons that wind in the wrong direction.
198885*/
198886static void geopolyCcwFunc(
198887 sqlite3_context *context,
198888 int argc,
198889 sqlite3_value **argv
198890){
198891 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
198892 if( p ){
198893 if( geopolyArea(p)<0.0 ){
198894 int ii, jj;
198895 for(ii=1, jj=p->nVertex-1; ii<jj; ii++, jj--){
198896 GeoCoord t = GeoX(p,ii);
198897 GeoX(p,ii) = GeoX(p,jj);
198898 GeoX(p,jj) = t;
198899 t = GeoY(p,ii);
198900 GeoY(p,ii) = GeoY(p,jj);
198901 GeoY(p,jj) = t;
198902 }
198903 }
198904 sqlite3_result_blob(context, p->hdr,
198905 4+8*p->nVertex, SQLITE_TRANSIENT);
198906 sqlite3_free(p);
198907 }
198908}
198909
198910#define GEOPOLY_PI 3.1415926535897932385
198911
198912/* Fast approximation for sine(X) for X between -0.5*pi and 2*pi
198913*/
198914static double geopolySine(double r){
198915 assert( r>=-0.5*GEOPOLY_PI && r<=2.0*GEOPOLY_PI );
198916 if( r>=1.5*GEOPOLY_PI ){
198917 r -= 2.0*GEOPOLY_PI;
198918 }
198919 if( r>=0.5*GEOPOLY_PI ){
198920 return -geopolySine(r-GEOPOLY_PI);
198921 }else{
198922 double r2 = r*r;
198923 double r3 = r2*r;
198924 double r5 = r3*r2;
198925 return 0.9996949*r - 0.1656700*r3 + 0.0075134*r5;
198926 }
198927}
198928
198929/*
198930** Function: geopoly_regular(X,Y,R,N)
198931**
198932** Construct a simple, convex, regular polygon centered at X, Y
198933** with circumradius R and with N sides.
198934*/
198935static void geopolyRegularFunc(
198936 sqlite3_context *context,
198937 int argc,
198938 sqlite3_value **argv
198939){
198940 double x = sqlite3_value_double(argv[0]);
198941 double y = sqlite3_value_double(argv[1]);
198942 double r = sqlite3_value_double(argv[2]);
198943 int n = sqlite3_value_int(argv[3]);
198944 int i;
198945 GeoPoly *p;
198946
198947 if( n<3 || r<=0.0 ) return;
198948 if( n>1000 ) n = 1000;
198949 p = sqlite3_malloc64( sizeof(*p) + (n-1)*2*sizeof(GeoCoord) );
198950 if( p==0 ){
198951 sqlite3_result_error_nomem(context);
198952 return;
198953 }
198954 i = 1;
198955 p->hdr[0] = *(unsigned char*)&i;
198956 p->hdr[1] = 0;
198957 p->hdr[2] = (n>>8)&0xff;
198958 p->hdr[3] = n&0xff;
198959 for(i=0; i<n; i++){
198960 double rAngle = 2.0*GEOPOLY_PI*i/n;
198961 GeoX(p,i) = x - r*geopolySine(rAngle-0.5*GEOPOLY_PI);
198962 GeoY(p,i) = y + r*geopolySine(rAngle);
198963 }
198964 sqlite3_result_blob(context, p->hdr, 4+8*n, SQLITE_TRANSIENT);
198965 sqlite3_free(p);
198966}
198967
198968/*
198969** If pPoly is a polygon, compute its bounding box. Then:
198970**
198971** (1) if aCoord!=0 store the bounding box in aCoord, returning NULL
198972** (2) otherwise, compute a GeoPoly for the bounding box and return the
198973** new GeoPoly
198974**
198975** If pPoly is NULL but aCoord is not NULL, then compute a new GeoPoly from
198976** the bounding box in aCoord and return a pointer to that GeoPoly.
198977*/
198978static GeoPoly *geopolyBBox(
198979 sqlite3_context *context, /* For recording the error */
198980 sqlite3_value *pPoly, /* The polygon */
198981 RtreeCoord *aCoord, /* Results here */
198982 int *pRc /* Error code here */
198983){
198984 GeoPoly *pOut = 0;
198985 GeoPoly *p;
198986 float mnX, mxX, mnY, mxY;
198987 if( pPoly==0 && aCoord!=0 ){
198988 p = 0;
198989 mnX = aCoord[0].f;
198990 mxX = aCoord[1].f;
198991 mnY = aCoord[2].f;
198992 mxY = aCoord[3].f;
198993 goto geopolyBboxFill;
198994 }else{
198995 p = geopolyFuncParam(context, pPoly, pRc);
198996 }
198997 if( p ){
198998 int ii;
198999 mnX = mxX = GeoX(p,0);
199000 mnY = mxY = GeoY(p,0);
199001 for(ii=1; ii<p->nVertex; ii++){
199002 double r = GeoX(p,ii);
199003 if( r<mnX ) mnX = (float)r;
199004 else if( r>mxX ) mxX = (float)r;
199005 r = GeoY(p,ii);
199006 if( r<mnY ) mnY = (float)r;
199007 else if( r>mxY ) mxY = (float)r;
199008 }
199009 if( pRc ) *pRc = SQLITE_OK;
199010 if( aCoord==0 ){
199011 geopolyBboxFill:
199012 pOut = sqlite3_realloc64(p, GEOPOLY_SZ(4));
199013 if( pOut==0 ){
199014 sqlite3_free(p);
199015 if( context ) sqlite3_result_error_nomem(context);
199016 if( pRc ) *pRc = SQLITE_NOMEM;
199017 return 0;
199018 }
199019 pOut->nVertex = 4;
199020 ii = 1;
199021 pOut->hdr[0] = *(unsigned char*)&ii;
199022 pOut->hdr[1] = 0;
199023 pOut->hdr[2] = 0;
199024 pOut->hdr[3] = 4;
199025 GeoX(pOut,0) = mnX;
199026 GeoY(pOut,0) = mnY;
199027 GeoX(pOut,1) = mxX;
199028 GeoY(pOut,1) = mnY;
199029 GeoX(pOut,2) = mxX;
199030 GeoY(pOut,2) = mxY;
199031 GeoX(pOut,3) = mnX;
199032 GeoY(pOut,3) = mxY;
199033 }else{
199034 sqlite3_free(p);
199035 aCoord[0].f = mnX;
199036 aCoord[1].f = mxX;
199037 aCoord[2].f = mnY;
199038 aCoord[3].f = mxY;
199039 }
199040 }else if( aCoord ){
199041 memset(aCoord, 0, sizeof(RtreeCoord)*4);
199042 }
199043 return pOut;
199044}
199045
199046/*
199047** Implementation of the geopoly_bbox(X) SQL function.
199048*/
199049static void geopolyBBoxFunc(
199050 sqlite3_context *context,
199051 int argc,
199052 sqlite3_value **argv
199053){
199054 GeoPoly *p = geopolyBBox(context, argv[0], 0, 0);
199055 if( p ){
199056 sqlite3_result_blob(context, p->hdr,
199057 4+8*p->nVertex, SQLITE_TRANSIENT);
199058 sqlite3_free(p);
199059 }
199060}
199061
199062/*
199063** State vector for the geopoly_group_bbox() aggregate function.
199064*/
199065typedef struct GeoBBox GeoBBox;
199066struct GeoBBox {
199067 int isInit;
199068 RtreeCoord a[4];
199069};
199070
199071
199072/*
199073** Implementation of the geopoly_group_bbox(X) aggregate SQL function.
199074*/
199075static void geopolyBBoxStep(
199076 sqlite3_context *context,
199077 int argc,
199078 sqlite3_value **argv
199079){
199080 RtreeCoord a[4];
199081 int rc = SQLITE_OK;
199082 (void)geopolyBBox(context, argv[0], a, &rc);
199083 if( rc==SQLITE_OK ){
199084 GeoBBox *pBBox;
199085 pBBox = (GeoBBox*)sqlite3_aggregate_context(context, sizeof(*pBBox));
199086 if( pBBox==0 ) return;
199087 if( pBBox->isInit==0 ){
199088 pBBox->isInit = 1;
199089 memcpy(pBBox->a, a, sizeof(RtreeCoord)*4);
199090 }else{
199091 if( a[0].f < pBBox->a[0].f ) pBBox->a[0] = a[0];
199092 if( a[1].f > pBBox->a[1].f ) pBBox->a[1] = a[1];
199093 if( a[2].f < pBBox->a[2].f ) pBBox->a[2] = a[2];
199094 if( a[3].f > pBBox->a[3].f ) pBBox->a[3] = a[3];
199095 }
199096 }
199097}
199098static void geopolyBBoxFinal(
199099 sqlite3_context *context
199100){
199101 GeoPoly *p;
199102 GeoBBox *pBBox;
199103 pBBox = (GeoBBox*)sqlite3_aggregate_context(context, 0);
199104 if( pBBox==0 ) return;
199105 p = geopolyBBox(context, 0, pBBox->a, 0);
199106 if( p ){
199107 sqlite3_result_blob(context, p->hdr,
199108 4+8*p->nVertex, SQLITE_TRANSIENT);
199109 sqlite3_free(p);
199110 }
199111}
199112
199113
199114/*
199115** Determine if point (x0,y0) is beneath line segment (x1,y1)->(x2,y2).
199116** Returns:
199117**
199118** +2 x0,y0 is on the line segement
199119**
199120** +1 x0,y0 is beneath line segment
199121**
199122** 0 x0,y0 is not on or beneath the line segment or the line segment
199123** is vertical and x0,y0 is not on the line segment
199124**
199125** The left-most coordinate min(x1,x2) is not considered to be part of
199126** the line segment for the purposes of this analysis.
199127*/
199128static int pointBeneathLine(
199129 double x0, double y0,
199130 double x1, double y1,
199131 double x2, double y2
199132){
199133 double y;
199134 if( x0==x1 && y0==y1 ) return 2;
199135 if( x1<x2 ){
199136 if( x0<=x1 || x0>x2 ) return 0;
199137 }else if( x1>x2 ){
199138 if( x0<=x2 || x0>x1 ) return 0;
199139 }else{
199140 /* Vertical line segment */
199141 if( x0!=x1 ) return 0;
199142 if( y0<y1 && y0<y2 ) return 0;
199143 if( y0>y1 && y0>y2 ) return 0;
199144 return 2;
199145 }
199146 y = y1 + (y2-y1)*(x0-x1)/(x2-x1);
199147 if( y0==y ) return 2;
199148 if( y0<y ) return 1;
199149 return 0;
199150}
199151
199152/*
199153** SQL function: geopoly_contains_point(P,X,Y)
199154**
199155** Return +2 if point X,Y is within polygon P.
199156** Return +1 if point X,Y is on the polygon boundary.
199157** Return 0 if point X,Y is outside the polygon
199158*/
199159static void geopolyContainsPointFunc(
199160 sqlite3_context *context,
199161 int argc,
199162 sqlite3_value **argv
199163){
199164 GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
199165 double x0 = sqlite3_value_double(argv[1]);
199166 double y0 = sqlite3_value_double(argv[2]);
199167 int v = 0;
199168 int cnt = 0;
199169 int ii;
199170 if( p1==0 ) return;
199171 for(ii=0; ii<p1->nVertex-1; ii++){
199172 v = pointBeneathLine(x0,y0,GeoX(p1,ii), GeoY(p1,ii),
199173 GeoX(p1,ii+1),GeoY(p1,ii+1));
199174 if( v==2 ) break;
199175 cnt += v;
199176 }
199177 if( v!=2 ){
199178 v = pointBeneathLine(x0,y0,GeoX(p1,ii), GeoY(p1,ii),
199179 GeoX(p1,0), GeoY(p1,0));
199180 }
199181 if( v==2 ){
199182 sqlite3_result_int(context, 1);
199183 }else if( ((v+cnt)&1)==0 ){
199184 sqlite3_result_int(context, 0);
199185 }else{
199186 sqlite3_result_int(context, 2);
199187 }
199188 sqlite3_free(p1);
199189}
199190
199191/* Forward declaration */
199192static int geopolyOverlap(GeoPoly *p1, GeoPoly *p2);
199193
199194/*
199195** SQL function: geopoly_within(P1,P2)
199196**
199197** Return +2 if P1 and P2 are the same polygon
199198** Return +1 if P2 is contained within P1
199199** Return 0 if any part of P2 is on the outside of P1
199200**
199201*/
199202static void geopolyWithinFunc(
199203 sqlite3_context *context,
199204 int argc,
199205 sqlite3_value **argv
199206){
199207 GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
199208 GeoPoly *p2 = geopolyFuncParam(context, argv[1], 0);
199209 if( p1 && p2 ){
199210 int x = geopolyOverlap(p1, p2);
199211 if( x<0 ){
199212 sqlite3_result_error_nomem(context);
199213 }else{
199214 sqlite3_result_int(context, x==2 ? 1 : x==4 ? 2 : 0);
199215 }
199216 }
199217 sqlite3_free(p1);
199218 sqlite3_free(p2);
199219}
199220
199221/* Objects used by the overlap algorihm. */
199222typedef struct GeoEvent GeoEvent;
199223typedef struct GeoSegment GeoSegment;
199224typedef struct GeoOverlap GeoOverlap;
199225struct GeoEvent {
199226 double x; /* X coordinate at which event occurs */
199227 int eType; /* 0 for ADD, 1 for REMOVE */
199228 GeoSegment *pSeg; /* The segment to be added or removed */
199229 GeoEvent *pNext; /* Next event in the sorted list */
199230};
199231struct GeoSegment {
199232 double C, B; /* y = C*x + B */
199233 double y; /* Current y value */
199234 float y0; /* Initial y value */
199235 unsigned char side; /* 1 for p1, 2 for p2 */
199236 unsigned int idx; /* Which segment within the side */
199237 GeoSegment *pNext; /* Next segment in a list sorted by y */
199238};
199239struct GeoOverlap {
199240 GeoEvent *aEvent; /* Array of all events */
199241 GeoSegment *aSegment; /* Array of all segments */
199242 int nEvent; /* Number of events */
199243 int nSegment; /* Number of segments */
199244};
199245
199246/*
199247** Add a single segment and its associated events.
199248*/
199249static void geopolyAddOneSegment(
199250 GeoOverlap *p,
199251 GeoCoord x0,
199252 GeoCoord y0,
199253 GeoCoord x1,
199254 GeoCoord y1,
199255 unsigned char side,
199256 unsigned int idx
199257){
199258 GeoSegment *pSeg;
199259 GeoEvent *pEvent;
199260 if( x0==x1 ) return; /* Ignore vertical segments */
199261 if( x0>x1 ){
199262 GeoCoord t = x0;
199263 x0 = x1;
199264 x1 = t;
199265 t = y0;
199266 y0 = y1;
199267 y1 = t;
199268 }
199269 pSeg = p->aSegment + p->nSegment;
199270 p->nSegment++;
199271 pSeg->C = (y1-y0)/(x1-x0);
199272 pSeg->B = y1 - x1*pSeg->C;
199273 pSeg->y0 = y0;
199274 pSeg->side = side;
199275 pSeg->idx = idx;
199276 pEvent = p->aEvent + p->nEvent;
199277 p->nEvent++;
199278 pEvent->x = x0;
199279 pEvent->eType = 0;
199280 pEvent->pSeg = pSeg;
199281 pEvent = p->aEvent + p->nEvent;
199282 p->nEvent++;
199283 pEvent->x = x1;
199284 pEvent->eType = 1;
199285 pEvent->pSeg = pSeg;
199286}
199287
199288
199289
199290/*
199291** Insert all segments and events for polygon pPoly.
199292*/
199293static void geopolyAddSegments(
199294 GeoOverlap *p, /* Add segments to this Overlap object */
199295 GeoPoly *pPoly, /* Take all segments from this polygon */
199296 unsigned char side /* The side of pPoly */
199297){
199298 unsigned int i;
199299 GeoCoord *x;
199300 for(i=0; i<(unsigned)pPoly->nVertex-1; i++){
199301 x = &GeoX(pPoly,i);
199302 geopolyAddOneSegment(p, x[0], x[1], x[2], x[3], side, i);
199303 }
199304 x = &GeoX(pPoly,i);
199305 geopolyAddOneSegment(p, x[0], x[1], pPoly->a[0], pPoly->a[1], side, i);
199306}
199307
199308/*
199309** Merge two lists of sorted events by X coordinate
199310*/
199311static GeoEvent *geopolyEventMerge(GeoEvent *pLeft, GeoEvent *pRight){
199312 GeoEvent head, *pLast;
199313 head.pNext = 0;
199314 pLast = &head;
199315 while( pRight && pLeft ){
199316 if( pRight->x <= pLeft->x ){
199317 pLast->pNext = pRight;
199318 pLast = pRight;
199319 pRight = pRight->pNext;
199320 }else{
199321 pLast->pNext = pLeft;
199322 pLast = pLeft;
199323 pLeft = pLeft->pNext;
199324 }
199325 }
199326 pLast->pNext = pRight ? pRight : pLeft;
199327 return head.pNext;
199328}
199329
199330/*
199331** Sort an array of nEvent event objects into a list.
199332*/
199333static GeoEvent *geopolySortEventsByX(GeoEvent *aEvent, int nEvent){
199334 int mx = 0;
199335 int i, j;
199336 GeoEvent *p;
199337 GeoEvent *a[50];
199338 for(i=0; i<nEvent; i++){
199339 p = &aEvent[i];
199340 p->pNext = 0;
199341 for(j=0; j<mx && a[j]; j++){
199342 p = geopolyEventMerge(a[j], p);
199343 a[j] = 0;
199344 }
199345 a[j] = p;
199346 if( j>=mx ) mx = j+1;
199347 }
199348 p = 0;
199349 for(i=0; i<mx; i++){
199350 p = geopolyEventMerge(a[i], p);
199351 }
199352 return p;
199353}
199354
199355/*
199356** Merge two lists of sorted segments by Y, and then by C.
199357*/
199358static GeoSegment *geopolySegmentMerge(GeoSegment *pLeft, GeoSegment *pRight){
199359 GeoSegment head, *pLast;
199360 head.pNext = 0;
199361 pLast = &head;
199362 while( pRight && pLeft ){
199363 double r = pRight->y - pLeft->y;
199364 if( r==0.0 ) r = pRight->C - pLeft->C;
199365 if( r<0.0 ){
199366 pLast->pNext = pRight;
199367 pLast = pRight;
199368 pRight = pRight->pNext;
199369 }else{
199370 pLast->pNext = pLeft;
199371 pLast = pLeft;
199372 pLeft = pLeft->pNext;
199373 }
199374 }
199375 pLast->pNext = pRight ? pRight : pLeft;
199376 return head.pNext;
199377}
199378
199379/*
199380** Sort a list of GeoSegments in order of increasing Y and in the event of
199381** a tie, increasing C (slope).
199382*/
199383static GeoSegment *geopolySortSegmentsByYAndC(GeoSegment *pList){
199384 int mx = 0;
199385 int i;
199386 GeoSegment *p;
199387 GeoSegment *a[50];
199388 while( pList ){
199389 p = pList;
199390 pList = pList->pNext;
199391 p->pNext = 0;
199392 for(i=0; i<mx && a[i]; i++){
199393 p = geopolySegmentMerge(a[i], p);
199394 a[i] = 0;
199395 }
199396 a[i] = p;
199397 if( i>=mx ) mx = i+1;
199398 }
199399 p = 0;
199400 for(i=0; i<mx; i++){
199401 p = geopolySegmentMerge(a[i], p);
199402 }
199403 return p;
199404}
199405
199406/*
199407** Determine the overlap between two polygons
199408*/
199409static int geopolyOverlap(GeoPoly *p1, GeoPoly *p2){
199410 sqlite3_int64 nVertex = p1->nVertex + p2->nVertex + 2;
199411 GeoOverlap *p;
199412 sqlite3_int64 nByte;
199413 GeoEvent *pThisEvent;
199414 double rX;
199415 int rc = 0;
199416 int needSort = 0;
199417 GeoSegment *pActive = 0;
199418 GeoSegment *pSeg;
199419 unsigned char aOverlap[4];
199420
199421 nByte = sizeof(GeoEvent)*nVertex*2
199422 + sizeof(GeoSegment)*nVertex
199423 + sizeof(GeoOverlap);
199424 p = sqlite3_malloc64( nByte );
199425 if( p==0 ) return -1;
199426 p->aEvent = (GeoEvent*)&p[1];
199427 p->aSegment = (GeoSegment*)&p->aEvent[nVertex*2];
199428 p->nEvent = p->nSegment = 0;
199429 geopolyAddSegments(p, p1, 1);
199430 geopolyAddSegments(p, p2, 2);
199431 pThisEvent = geopolySortEventsByX(p->aEvent, p->nEvent);
199432 rX = pThisEvent && pThisEvent->x==0.0 ? -1.0 : 0.0;
199433 memset(aOverlap, 0, sizeof(aOverlap));
199434 while( pThisEvent ){
199435 if( pThisEvent->x!=rX ){
199436 GeoSegment *pPrev = 0;
199437 int iMask = 0;
199438 GEODEBUG(("Distinct X: %g\n", pThisEvent->x));
199439 rX = pThisEvent->x;
199440 if( needSort ){
199441 GEODEBUG(("SORT\n"));
199442 pActive = geopolySortSegmentsByYAndC(pActive);
199443 needSort = 0;
199444 }
199445 for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
199446 if( pPrev ){
199447 if( pPrev->y!=pSeg->y ){
199448 GEODEBUG(("MASK: %d\n", iMask));
199449 aOverlap[iMask] = 1;
199450 }
199451 }
199452 iMask ^= pSeg->side;
199453 pPrev = pSeg;
199454 }
199455 pPrev = 0;
199456 for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
199457 double y = pSeg->C*rX + pSeg->B;
199458 GEODEBUG(("Segment %d.%d %g->%g\n", pSeg->side, pSeg->idx, pSeg->y, y));
199459 pSeg->y = y;
199460 if( pPrev ){
199461 if( pPrev->y>pSeg->y && pPrev->side!=pSeg->side ){
199462 rc = 1;
199463 GEODEBUG(("Crossing: %d.%d and %d.%d\n",
199464 pPrev->side, pPrev->idx,
199465 pSeg->side, pSeg->idx));
199466 goto geopolyOverlapDone;
199467 }else if( pPrev->y!=pSeg->y ){
199468 GEODEBUG(("MASK: %d\n", iMask));
199469 aOverlap[iMask] = 1;
199470 }
199471 }
199472 iMask ^= pSeg->side;
199473 pPrev = pSeg;
199474 }
199475 }
199476 GEODEBUG(("%s %d.%d C=%g B=%g\n",
199477 pThisEvent->eType ? "RM " : "ADD",
199478 pThisEvent->pSeg->side, pThisEvent->pSeg->idx,
199479 pThisEvent->pSeg->C,
199480 pThisEvent->pSeg->B));
199481 if( pThisEvent->eType==0 ){
199482 /* Add a segment */
199483 pSeg = pThisEvent->pSeg;
199484 pSeg->y = pSeg->y0;
199485 pSeg->pNext = pActive;
199486 pActive = pSeg;
199487 needSort = 1;
199488 }else{
199489 /* Remove a segment */
199490 if( pActive==pThisEvent->pSeg ){
199491 pActive = ALWAYS(pActive) ? pActive->pNext : 0;
199492 }else{
199493 for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
199494 if( pSeg->pNext==pThisEvent->pSeg ){
199495 pSeg->pNext = ALWAYS(pSeg->pNext) ? pSeg->pNext->pNext : 0;
199496 break;
199497 }
199498 }
199499 }
199500 }
199501 pThisEvent = pThisEvent->pNext;
199502 }
199503 if( aOverlap[3]==0 ){
199504 rc = 0;
199505 }else if( aOverlap[1]!=0 && aOverlap[2]==0 ){
199506 rc = 3;
199507 }else if( aOverlap[1]==0 && aOverlap[2]!=0 ){
199508 rc = 2;
199509 }else if( aOverlap[1]==0 && aOverlap[2]==0 ){
199510 rc = 4;
199511 }else{
199512 rc = 1;
199513 }
199514
199515geopolyOverlapDone:
199516 sqlite3_free(p);
199517 return rc;
199518}
199519
199520/*
199521** SQL function: geopoly_overlap(P1,P2)
199522**
199523** Determine whether or not P1 and P2 overlap. Return value:
199524**
199525** 0 The two polygons are disjoint
199526** 1 They overlap
199527** 2 P1 is completely contained within P2
199528** 3 P2 is completely contained within P1
199529** 4 P1 and P2 are the same polygon
199530** NULL Either P1 or P2 or both are not valid polygons
199531*/
199532static void geopolyOverlapFunc(
199533 sqlite3_context *context,
199534 int argc,
199535 sqlite3_value **argv
199536){
199537 GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
199538 GeoPoly *p2 = geopolyFuncParam(context, argv[1], 0);
199539 if( p1 && p2 ){
199540 int x = geopolyOverlap(p1, p2);
199541 if( x<0 ){
199542 sqlite3_result_error_nomem(context);
199543 }else{
199544 sqlite3_result_int(context, x);
199545 }
199546 }
199547 sqlite3_free(p1);
199548 sqlite3_free(p2);
199549}
199550
199551/*
199552** Enable or disable debugging output
199553*/
199554static void geopolyDebugFunc(
199555 sqlite3_context *context,
199556 int argc,
199557 sqlite3_value **argv
199558){
199559#ifdef GEOPOLY_ENABLE_DEBUG
199560 geo_debug = sqlite3_value_int(argv[0]);
199561#endif
199562}
199563
199564/*
199565** This function is the implementation of both the xConnect and xCreate
199566** methods of the geopoly virtual table.
199567**
199568** argv[0] -> module name
199569** argv[1] -> database name
199570** argv[2] -> table name
199571** argv[...] -> column names...
199572*/
199573static int geopolyInit(
199574 sqlite3 *db, /* Database connection */
199575 void *pAux, /* One of the RTREE_COORD_* constants */
199576 int argc, const char *const*argv, /* Parameters to CREATE TABLE statement */
199577 sqlite3_vtab **ppVtab, /* OUT: New virtual table */
199578 char **pzErr, /* OUT: Error message, if any */
199579 int isCreate /* True for xCreate, false for xConnect */
199580){
199581 int rc = SQLITE_OK;
199582 Rtree *pRtree;
199583 sqlite3_int64 nDb; /* Length of string argv[1] */
199584 sqlite3_int64 nName; /* Length of string argv[2] */
199585 sqlite3_str *pSql;
199586 char *zSql;
199587 int ii;
199588
199589 sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
199590
199591 /* Allocate the sqlite3_vtab structure */
199592 nDb = strlen(argv[1]);
199593 nName = strlen(argv[2]);
199594 pRtree = (Rtree *)sqlite3_malloc64(sizeof(Rtree)+nDb+nName+2);
199595 if( !pRtree ){
199596 return SQLITE_NOMEM;
199597 }
199598 memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
199599 pRtree->nBusy = 1;
199600 pRtree->base.pModule = &rtreeModule;
199601 pRtree->zDb = (char *)&pRtree[1];
199602 pRtree->zName = &pRtree->zDb[nDb+1];
199603 pRtree->eCoordType = RTREE_COORD_REAL32;
199604 pRtree->nDim = 2;
199605 pRtree->nDim2 = 4;
199606 memcpy(pRtree->zDb, argv[1], nDb);
199607 memcpy(pRtree->zName, argv[2], nName);
199608
199609
199610 /* Create/Connect to the underlying relational database schema. If
199611 ** that is successful, call sqlite3_declare_vtab() to configure
199612 ** the r-tree table schema.
199613 */
199614 pSql = sqlite3_str_new(db);
199615 sqlite3_str_appendf(pSql, "CREATE TABLE x(_shape");
199616 pRtree->nAux = 1; /* Add one for _shape */
199617 pRtree->nAuxNotNull = 1; /* The _shape column is always not-null */
199618 for(ii=3; ii<argc; ii++){
199619 pRtree->nAux++;
199620 sqlite3_str_appendf(pSql, ",%s", argv[ii]);
199621 }
199622 sqlite3_str_appendf(pSql, ");");
199623 zSql = sqlite3_str_finish(pSql);
199624 if( !zSql ){
199625 rc = SQLITE_NOMEM;
199626 }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){
199627 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
199628 }
199629 sqlite3_free(zSql);
199630 if( rc ) goto geopolyInit_fail;
199631 pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
199632
199633 /* Figure out the node size to use. */
199634 rc = getNodeSize(db, pRtree, isCreate, pzErr);
199635 if( rc ) goto geopolyInit_fail;
199636 rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate);
199637 if( rc ){
199638 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
199639 goto geopolyInit_fail;
199640 }
199641
199642 *ppVtab = (sqlite3_vtab *)pRtree;
199643 return SQLITE_OK;
199644
199645geopolyInit_fail:
199646 if( rc==SQLITE_OK ) rc = SQLITE_ERROR;
199647 assert( *ppVtab==0 );
199648 assert( pRtree->nBusy==1 );
199649 rtreeRelease(pRtree);
199650 return rc;
199651}
199652
199653
199654/*
199655** GEOPOLY virtual table module xCreate method.
199656*/
199657static int geopolyCreate(
199658 sqlite3 *db,
199659 void *pAux,
199660 int argc, const char *const*argv,
199661 sqlite3_vtab **ppVtab,
199662 char **pzErr
199663){
199664 return geopolyInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
199665}
199666
199667/*
199668** GEOPOLY virtual table module xConnect method.
199669*/
199670static int geopolyConnect(
199671 sqlite3 *db,
199672 void *pAux,
199673 int argc, const char *const*argv,
199674 sqlite3_vtab **ppVtab,
199675 char **pzErr
199676){
199677 return geopolyInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
199678}
199679
199680
199681/*
199682** GEOPOLY virtual table module xFilter method.
199683**
199684** Query plans:
199685**
199686** 1 rowid lookup
199687** 2 search for objects overlapping the same bounding box
199688** that contains polygon argv[0]
199689** 3 search for objects overlapping the same bounding box
199690** that contains polygon argv[0]
199691** 4 full table scan
199692*/
199693static int geopolyFilter(
199694 sqlite3_vtab_cursor *pVtabCursor, /* The cursor to initialize */
199695 int idxNum, /* Query plan */
199696 const char *idxStr, /* Not Used */
199697 int argc, sqlite3_value **argv /* Parameters to the query plan */
199698){
199699 Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
199700 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
199701 RtreeNode *pRoot = 0;
199702 int rc = SQLITE_OK;
199703 int iCell = 0;
199704
199705 rtreeReference(pRtree);
199706
199707 /* Reset the cursor to the same state as rtreeOpen() leaves it in. */
199708 resetCursor(pCsr);
199709
199710 pCsr->iStrategy = idxNum;
199711 if( idxNum==1 ){
199712 /* Special case - lookup by rowid. */
199713 RtreeNode *pLeaf; /* Leaf on which the required cell resides */
199714 RtreeSearchPoint *p; /* Search point for the leaf */
199715 i64 iRowid = sqlite3_value_int64(argv[0]);
199716 i64 iNode = 0;
199717 rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode);
199718 if( rc==SQLITE_OK && pLeaf!=0 ){
199719 p = rtreeSearchPointNew(pCsr, RTREE_ZERO, 0);
199720 assert( p!=0 ); /* Always returns pCsr->sPoint */
199721 pCsr->aNode[0] = pLeaf;
199722 p->id = iNode;
199723 p->eWithin = PARTLY_WITHIN;
199724 rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
199725 p->iCell = (u8)iCell;
199726 RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
199727 }else{
199728 pCsr->atEOF = 1;
199729 }
199730 }else{
199731 /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
199732 ** with the configured constraints.
199733 */
199734 rc = nodeAcquire(pRtree, 1, 0, &pRoot);
199735 if( rc==SQLITE_OK && idxNum<=3 ){
199736 RtreeCoord bbox[4];
199737 RtreeConstraint *p;
199738 assert( argc==1 );
199739 assert( argv[0]!=0 );
199740 geopolyBBox(0, argv[0], bbox, &rc);
199741 if( rc ){
199742 goto geopoly_filter_end;
199743 }
199744 pCsr->aConstraint = p = sqlite3_malloc(sizeof(RtreeConstraint)*4);
199745 pCsr->nConstraint = 4;
199746 if( p==0 ){
199747 rc = SQLITE_NOMEM;
199748 }else{
199749 memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*4);
199750 memset(pCsr->anQueue, 0, sizeof(u32)*(pRtree->iDepth + 1));
199751 if( idxNum==2 ){
199752 /* Overlap query */
199753 p->op = 'B';
199754 p->iCoord = 0;
199755 p->u.rValue = bbox[1].f;
199756 p++;
199757 p->op = 'D';
199758 p->iCoord = 1;
199759 p->u.rValue = bbox[0].f;
199760 p++;
199761 p->op = 'B';
199762 p->iCoord = 2;
199763 p->u.rValue = bbox[3].f;
199764 p++;
199765 p->op = 'D';
199766 p->iCoord = 3;
199767 p->u.rValue = bbox[2].f;
199768 }else{
199769 /* Within query */
199770 p->op = 'D';
199771 p->iCoord = 0;
199772 p->u.rValue = bbox[0].f;
199773 p++;
199774 p->op = 'B';
199775 p->iCoord = 1;
199776 p->u.rValue = bbox[1].f;
199777 p++;
199778 p->op = 'D';
199779 p->iCoord = 2;
199780 p->u.rValue = bbox[2].f;
199781 p++;
199782 p->op = 'B';
199783 p->iCoord = 3;
199784 p->u.rValue = bbox[3].f;
199785 }
199786 }
199787 }
199788 if( rc==SQLITE_OK ){
199789 RtreeSearchPoint *pNew;
199790 pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, (u8)(pRtree->iDepth+1));
199791 if( pNew==0 ){
199792 rc = SQLITE_NOMEM;
199793 goto geopoly_filter_end;
199794 }
199795 pNew->id = 1;
199796 pNew->iCell = 0;
199797 pNew->eWithin = PARTLY_WITHIN;
199798 assert( pCsr->bPoint==1 );
199799 pCsr->aNode[0] = pRoot;
199800 pRoot = 0;
199801 RTREE_QUEUE_TRACE(pCsr, "PUSH-Fm:");
199802 rc = rtreeStepToLeaf(pCsr);
199803 }
199804 }
199805
199806geopoly_filter_end:
199807 nodeRelease(pRtree, pRoot);
199808 rtreeRelease(pRtree);
199809 return rc;
199810}
199811
199812/*
199813** Rtree virtual table module xBestIndex method. There are three
199814** table scan strategies to choose from (in order from most to
199815** least desirable):
199816**
199817** idxNum idxStr Strategy
199818** ------------------------------------------------
199819** 1 "rowid" Direct lookup by rowid.
199820** 2 "rtree" R-tree overlap query using geopoly_overlap()
199821** 3 "rtree" R-tree within query using geopoly_within()
199822** 4 "fullscan" full-table scan.
199823** ------------------------------------------------
199824*/
199825static int geopolyBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
199826 int ii;
199827 int iRowidTerm = -1;
199828 int iFuncTerm = -1;
199829 int idxNum = 0;
199830
199831 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
199832 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
199833 if( !p->usable ) continue;
199834 if( p->iColumn<0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
199835 iRowidTerm = ii;
199836 break;
199837 }
199838 if( p->iColumn==0 && p->op>=SQLITE_INDEX_CONSTRAINT_FUNCTION ){
199839 /* p->op==SQLITE_INDEX_CONSTRAINT_FUNCTION for geopoly_overlap()
199840 ** p->op==(SQLITE_INDEX_CONTRAINT_FUNCTION+1) for geopoly_within().
199841 ** See geopolyFindFunction() */
199842 iFuncTerm = ii;
199843 idxNum = p->op - SQLITE_INDEX_CONSTRAINT_FUNCTION + 2;
199844 }
199845 }
199846
199847 if( iRowidTerm>=0 ){
199848 pIdxInfo->idxNum = 1;
199849 pIdxInfo->idxStr = "rowid";
199850 pIdxInfo->aConstraintUsage[iRowidTerm].argvIndex = 1;
199851 pIdxInfo->aConstraintUsage[iRowidTerm].omit = 1;
199852 pIdxInfo->estimatedCost = 30.0;
199853 pIdxInfo->estimatedRows = 1;
199854 pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE;
199855 return SQLITE_OK;
199856 }
199857 if( iFuncTerm>=0 ){
199858 pIdxInfo->idxNum = idxNum;
199859 pIdxInfo->idxStr = "rtree";
199860 pIdxInfo->aConstraintUsage[iFuncTerm].argvIndex = 1;
199861 pIdxInfo->aConstraintUsage[iFuncTerm].omit = 0;
199862 pIdxInfo->estimatedCost = 300.0;
199863 pIdxInfo->estimatedRows = 10;
199864 return SQLITE_OK;
199865 }
199866 pIdxInfo->idxNum = 4;
199867 pIdxInfo->idxStr = "fullscan";
199868 pIdxInfo->estimatedCost = 3000000.0;
199869 pIdxInfo->estimatedRows = 100000;
199870 return SQLITE_OK;
199871}
199872
199873
199874/*
199875** GEOPOLY virtual table module xColumn method.
199876*/
199877static int geopolyColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
199878 Rtree *pRtree = (Rtree *)cur->pVtab;
199879 RtreeCursor *pCsr = (RtreeCursor *)cur;
199880 RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
199881 int rc = SQLITE_OK;
199882 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
199883
199884 if( rc ) return rc;
199885 if( p==0 ) return SQLITE_OK;
199886 if( i==0 && sqlite3_vtab_nochange(ctx) ) return SQLITE_OK;
199887 if( i<=pRtree->nAux ){
199888 if( !pCsr->bAuxValid ){
199889 if( pCsr->pReadAux==0 ){
199890 rc = sqlite3_prepare_v3(pRtree->db, pRtree->zReadAuxSql, -1, 0,
199891 &pCsr->pReadAux, 0);
199892 if( rc ) return rc;
199893 }
199894 sqlite3_bind_int64(pCsr->pReadAux, 1,
199895 nodeGetRowid(pRtree, pNode, p->iCell));
199896 rc = sqlite3_step(pCsr->pReadAux);
199897 if( rc==SQLITE_ROW ){
199898 pCsr->bAuxValid = 1;
199899 }else{
199900 sqlite3_reset(pCsr->pReadAux);
199901 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
199902 return rc;
199903 }
199904 }
199905 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pReadAux, i+2));
199906 }
199907 return SQLITE_OK;
199908}
199909
199910
199911/*
199912** The xUpdate method for GEOPOLY module virtual tables.
199913**
199914** For DELETE:
199915**
199916** argv[0] = the rowid to be deleted
199917**
199918** For INSERT:
199919**
199920** argv[0] = SQL NULL
199921** argv[1] = rowid to insert, or an SQL NULL to select automatically
199922** argv[2] = _shape column
199923** argv[3] = first application-defined column....
199924**
199925** For UPDATE:
199926**
199927** argv[0] = rowid to modify. Never NULL
199928** argv[1] = rowid after the change. Never NULL
199929** argv[2] = new value for _shape
199930** argv[3] = new value for first application-defined column....
199931*/
199932static int geopolyUpdate(
199933 sqlite3_vtab *pVtab,
199934 int nData,
199935 sqlite3_value **aData,
199936 sqlite_int64 *pRowid
199937){
199938 Rtree *pRtree = (Rtree *)pVtab;
199939 int rc = SQLITE_OK;
199940 RtreeCell cell; /* New cell to insert if nData>1 */
199941 i64 oldRowid; /* The old rowid */
199942 int oldRowidValid; /* True if oldRowid is valid */
199943 i64 newRowid; /* The new rowid */
199944 int newRowidValid; /* True if newRowid is valid */
199945 int coordChange = 0; /* Change in coordinates */
199946
199947 if( pRtree->nNodeRef ){
199948 /* Unable to write to the btree while another cursor is reading from it,
199949 ** since the write might do a rebalance which would disrupt the read
199950 ** cursor. */
199951 return SQLITE_LOCKED_VTAB;
199952 }
199953 rtreeReference(pRtree);
199954 assert(nData>=1);
199955
199956 oldRowidValid = sqlite3_value_type(aData[0])!=SQLITE_NULL;;
199957 oldRowid = oldRowidValid ? sqlite3_value_int64(aData[0]) : 0;
199958 newRowidValid = nData>1 && sqlite3_value_type(aData[1])!=SQLITE_NULL;
199959 newRowid = newRowidValid ? sqlite3_value_int64(aData[1]) : 0;
199960 cell.iRowid = newRowid;
199961
199962 if( nData>1 /* not a DELETE */
199963 && (!oldRowidValid /* INSERT */
199964 || !sqlite3_value_nochange(aData[2]) /* UPDATE _shape */
199965 || oldRowid!=newRowid) /* Rowid change */
199966 ){
199967 assert( aData[2]!=0 );
199968 geopolyBBox(0, aData[2], cell.aCoord, &rc);
199969 if( rc ){
199970 if( rc==SQLITE_ERROR ){
199971 pVtab->zErrMsg =
199972 sqlite3_mprintf("_shape does not contain a valid polygon");
199973 }
199974 goto geopoly_update_end;
199975 }
199976 coordChange = 1;
199977
199978 /* If a rowid value was supplied, check if it is already present in
199979 ** the table. If so, the constraint has failed. */
199980 if( newRowidValid && (!oldRowidValid || oldRowid!=newRowid) ){
199981 int steprc;
199982 sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
199983 steprc = sqlite3_step(pRtree->pReadRowid);
199984 rc = sqlite3_reset(pRtree->pReadRowid);
199985 if( SQLITE_ROW==steprc ){
199986 if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE ){
199987 rc = rtreeDeleteRowid(pRtree, cell.iRowid);
199988 }else{
199989 rc = rtreeConstraintError(pRtree, 0);
199990 }
199991 }
199992 }
199993 }
199994
199995 /* If aData[0] is not an SQL NULL value, it is the rowid of a
199996 ** record to delete from the r-tree table. The following block does
199997 ** just that.
199998 */
199999 if( rc==SQLITE_OK && (nData==1 || (coordChange && oldRowidValid)) ){
200000 rc = rtreeDeleteRowid(pRtree, oldRowid);
200001 }
200002
200003 /* If the aData[] array contains more than one element, elements
200004 ** (aData[2]..aData[argc-1]) contain a new record to insert into
200005 ** the r-tree structure.
200006 */
200007 if( rc==SQLITE_OK && nData>1 && coordChange ){
200008 /* Insert the new record into the r-tree */
200009 RtreeNode *pLeaf = 0;
200010 if( !newRowidValid ){
200011 rc = rtreeNewRowid(pRtree, &cell.iRowid);
200012 }
200013 *pRowid = cell.iRowid;
200014 if( rc==SQLITE_OK ){
200015 rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
200016 }
200017 if( rc==SQLITE_OK ){
200018 int rc2;
200019 pRtree->iReinsertHeight = -1;
200020 rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
200021 rc2 = nodeRelease(pRtree, pLeaf);
200022 if( rc==SQLITE_OK ){
200023 rc = rc2;
200024 }
200025 }
200026 }
200027
200028 /* Change the data */
200029 if( rc==SQLITE_OK && nData>1 ){
200030 sqlite3_stmt *pUp = pRtree->pWriteAux;
200031 int jj;
200032 int nChange = 0;
200033 sqlite3_bind_int64(pUp, 1, cell.iRowid);
200034 assert( pRtree->nAux>=1 );
200035 if( sqlite3_value_nochange(aData[2]) ){
200036 sqlite3_bind_null(pUp, 2);
200037 }else{
200038 GeoPoly *p = 0;
200039 if( sqlite3_value_type(aData[2])==SQLITE_TEXT
200040 && (p = geopolyFuncParam(0, aData[2], &rc))!=0
200041 && rc==SQLITE_OK
200042 ){
200043 sqlite3_bind_blob(pUp, 2, p->hdr, 4+8*p->nVertex, SQLITE_TRANSIENT);
200044 }else{
200045 sqlite3_bind_value(pUp, 2, aData[2]);
200046 }
200047 sqlite3_free(p);
200048 nChange = 1;
200049 }
200050 for(jj=1; jj<pRtree->nAux; jj++){
200051 nChange++;
200052 sqlite3_bind_value(pUp, jj+2, aData[jj+2]);
200053 }
200054 if( nChange ){
200055 sqlite3_step(pUp);
200056 rc = sqlite3_reset(pUp);
200057 }
200058 }
200059
200060geopoly_update_end:
200061 rtreeRelease(pRtree);
200062 return rc;
200063}
200064
200065/*
200066** Report that geopoly_overlap() is an overloaded function suitable
200067** for use in xBestIndex.
200068*/
200069static int geopolyFindFunction(
200070 sqlite3_vtab *pVtab,
200071 int nArg,
200072 const char *zName,
200073 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
200074 void **ppArg
200075){
200076 if( sqlite3_stricmp(zName, "geopoly_overlap")==0 ){
200077 *pxFunc = geopolyOverlapFunc;
200078 *ppArg = 0;
200079 return SQLITE_INDEX_CONSTRAINT_FUNCTION;
200080 }
200081 if( sqlite3_stricmp(zName, "geopoly_within")==0 ){
200082 *pxFunc = geopolyWithinFunc;
200083 *ppArg = 0;
200084 return SQLITE_INDEX_CONSTRAINT_FUNCTION+1;
200085 }
200086 return 0;
200087}
200088
200089
200090static sqlite3_module geopolyModule = {
200091 3, /* iVersion */
200092 geopolyCreate, /* xCreate - create a table */
200093 geopolyConnect, /* xConnect - connect to an existing table */
200094 geopolyBestIndex, /* xBestIndex - Determine search strategy */
200095 rtreeDisconnect, /* xDisconnect - Disconnect from a table */
200096 rtreeDestroy, /* xDestroy - Drop a table */
200097 rtreeOpen, /* xOpen - open a cursor */
200098 rtreeClose, /* xClose - close a cursor */
200099 geopolyFilter, /* xFilter - configure scan constraints */
200100 rtreeNext, /* xNext - advance a cursor */
200101 rtreeEof, /* xEof */
200102 geopolyColumn, /* xColumn - read data */
200103 rtreeRowid, /* xRowid - read data */
200104 geopolyUpdate, /* xUpdate - write data */
200105 rtreeBeginTransaction, /* xBegin - begin transaction */
200106 rtreeEndTransaction, /* xSync - sync transaction */
200107 rtreeEndTransaction, /* xCommit - commit transaction */
200108 rtreeEndTransaction, /* xRollback - rollback transaction */
200109 geopolyFindFunction, /* xFindFunction - function overloading */
200110 rtreeRename, /* xRename - rename the table */
200111 rtreeSavepoint, /* xSavepoint */
200112 0, /* xRelease */
200113 0, /* xRollbackTo */
200114 rtreeShadowName /* xShadowName */
200115};
200116
200117static int sqlite3_geopoly_init(sqlite3 *db){
200118 int rc = SQLITE_OK;
200119 static const struct {
200120 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
200121 signed char nArg;
200122 unsigned char bPure;
200123 const char *zName;
200124 } aFunc[] = {
200125 { geopolyAreaFunc, 1, 1, "geopoly_area" },
200126 { geopolyBlobFunc, 1, 1, "geopoly_blob" },
200127 { geopolyJsonFunc, 1, 1, "geopoly_json" },
200128 { geopolySvgFunc, -1, 1, "geopoly_svg" },
200129 { geopolyWithinFunc, 2, 1, "geopoly_within" },
200130 { geopolyContainsPointFunc, 3, 1, "geopoly_contains_point" },
200131 { geopolyOverlapFunc, 2, 1, "geopoly_overlap" },
200132 { geopolyDebugFunc, 1, 0, "geopoly_debug" },
200133 { geopolyBBoxFunc, 1, 1, "geopoly_bbox" },
200134 { geopolyXformFunc, 7, 1, "geopoly_xform" },
200135 { geopolyRegularFunc, 4, 1, "geopoly_regular" },
200136 { geopolyCcwFunc, 1, 1, "geopoly_ccw" },
200137 };
200138 static const struct {
200139 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
200140 void (*xFinal)(sqlite3_context*);
200141 const char *zName;
200142 } aAgg[] = {
200143 { geopolyBBoxStep, geopolyBBoxFinal, "geopoly_group_bbox" },
200144 };
200145 int i;
200146 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
200147 int enc;
200148 if( aFunc[i].bPure ){
200149 enc = SQLITE_UTF8|SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS;
200150 }else{
200151 enc = SQLITE_UTF8|SQLITE_DIRECTONLY;
200152 }
200153 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
200154 enc, 0,
200155 aFunc[i].xFunc, 0, 0);
200156 }
200157 for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
200158 rc = sqlite3_create_function(db, aAgg[i].zName, 1,
200159 SQLITE_UTF8|SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS, 0,
200160 0, aAgg[i].xStep, aAgg[i].xFinal);
200161 }
200162 if( rc==SQLITE_OK ){
200163 rc = sqlite3_create_module_v2(db, "geopoly", &geopolyModule, 0, 0);
200164 }
200165 return rc;
200166}
200167
200168/************** End of geopoly.c *********************************************/
200169/************** Continuing where we left off in rtree.c **********************/
200170#endif
200171
200172/*
200173** Register the r-tree module with database handle db. This creates the
200174** virtual table module "rtree" and the debugging/analysis scalar
200175** function "rtreenode".
200176*/
200177SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db){
200178 const int utf8 = SQLITE_UTF8;
200179 int rc;
200180
200181 rc = sqlite3_create_function(db, zFunc: "rtreenode", nArg: 2, enc: utf8, p: 0, xSFunc: rtreenode, xStep: 0, xFinal: 0);
200182 if( rc==SQLITE_OK ){
200183 rc = sqlite3_create_function(db, zFunc: "rtreedepth", nArg: 1, enc: utf8, p: 0,xSFunc: rtreedepth, xStep: 0, xFinal: 0);
200184 }
200185 if( rc==SQLITE_OK ){
200186 rc = sqlite3_create_function(db, zFunc: "rtreecheck", nArg: -1, enc: utf8, p: 0,xSFunc: rtreecheck, xStep: 0,xFinal: 0);
200187 }
200188 if( rc==SQLITE_OK ){
200189#ifdef SQLITE_RTREE_INT_ONLY
200190 void *c = (void *)RTREE_COORD_INT32;
200191#else
200192 void *c = (void *)RTREE_COORD_REAL32;
200193#endif
200194 rc = sqlite3_create_module_v2(db, zName: "rtree", pModule: &rtreeModule, pAux: c, xDestroy: 0);
200195 }
200196 if( rc==SQLITE_OK ){
200197 void *c = (void *)RTREE_COORD_INT32;
200198 rc = sqlite3_create_module_v2(db, zName: "rtree_i32", pModule: &rtreeModule, pAux: c, xDestroy: 0);
200199 }
200200#ifdef SQLITE_ENABLE_GEOPOLY
200201 if( rc==SQLITE_OK ){
200202 rc = sqlite3_geopoly_init(db);
200203 }
200204#endif
200205
200206 return rc;
200207}
200208
200209/*
200210** This routine deletes the RtreeGeomCallback object that was attached
200211** one of the SQL functions create by sqlite3_rtree_geometry_callback()
200212** or sqlite3_rtree_query_callback(). In other words, this routine is the
200213** destructor for an RtreeGeomCallback objecct. This routine is called when
200214** the corresponding SQL function is deleted.
200215*/
200216static void rtreeFreeCallback(void *p){
200217 RtreeGeomCallback *pInfo = (RtreeGeomCallback*)p;
200218 if( pInfo->xDestructor ) pInfo->xDestructor(pInfo->pContext);
200219 sqlite3_free(p);
200220}
200221
200222/*
200223** This routine frees the BLOB that is returned by geomCallback().
200224*/
200225static void rtreeMatchArgFree(void *pArg){
200226 int i;
200227 RtreeMatchArg *p = (RtreeMatchArg*)pArg;
200228 for(i=0; i<p->nParam; i++){
200229 sqlite3_value_free(pOld: p->apSqlParam[i]);
200230 }
200231 sqlite3_free(p);
200232}
200233
200234/*
200235** Each call to sqlite3_rtree_geometry_callback() or
200236** sqlite3_rtree_query_callback() creates an ordinary SQLite
200237** scalar function that is implemented by this routine.
200238**
200239** All this function does is construct an RtreeMatchArg object that
200240** contains the geometry-checking callback routines and a list of
200241** parameters to this function, then return that RtreeMatchArg object
200242** as a BLOB.
200243**
200244** The R-Tree MATCH operator will read the returned BLOB, deserialize
200245** the RtreeMatchArg object, and use the RtreeMatchArg object to figure
200246** out which elements of the R-Tree should be returned by the query.
200247*/
200248static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
200249 RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(p: ctx);
200250 RtreeMatchArg *pBlob;
200251 sqlite3_int64 nBlob;
200252 int memErr = 0;
200253
200254 nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue)
200255 + nArg*sizeof(sqlite3_value*);
200256 pBlob = (RtreeMatchArg *)sqlite3_malloc64(n: nBlob);
200257 if( !pBlob ){
200258 sqlite3_result_error_nomem(pCtx: ctx);
200259 }else{
200260 int i;
200261 pBlob->iSize = nBlob;
200262 pBlob->cb = pGeomCtx[0];
200263 pBlob->apSqlParam = (sqlite3_value**)&pBlob->aParam[nArg];
200264 pBlob->nParam = nArg;
200265 for(i=0; i<nArg; i++){
200266 pBlob->apSqlParam[i] = sqlite3_value_dup(pOrig: aArg[i]);
200267 if( pBlob->apSqlParam[i]==0 ) memErr = 1;
200268#ifdef SQLITE_RTREE_INT_ONLY
200269 pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
200270#else
200271 pBlob->aParam[i] = sqlite3_value_double(pVal: aArg[i]);
200272#endif
200273 }
200274 if( memErr ){
200275 sqlite3_result_error_nomem(pCtx: ctx);
200276 rtreeMatchArgFree(pArg: pBlob);
200277 }else{
200278 sqlite3_result_pointer(pCtx: ctx, pPtr: pBlob, zPType: "RtreeMatchArg", xDestructor: rtreeMatchArgFree);
200279 }
200280 }
200281}
200282
200283/*
200284** Register a new geometry function for use with the r-tree MATCH operator.
200285*/
200286SQLITE_API int sqlite3_rtree_geometry_callback(
200287 sqlite3 *db, /* Register SQL function on this connection */
200288 const char *zGeom, /* Name of the new SQL function */
200289 int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*), /* Callback */
200290 void *pContext /* Extra data associated with the callback */
200291){
200292 RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
200293
200294 /* Allocate and populate the context object. */
200295 pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(n: sizeof(RtreeGeomCallback));
200296 if( !pGeomCtx ) return SQLITE_NOMEM;
200297 pGeomCtx->xGeom = xGeom;
200298 pGeomCtx->xQueryFunc = 0;
200299 pGeomCtx->xDestructor = 0;
200300 pGeomCtx->pContext = pContext;
200301 return sqlite3_create_function_v2(db, zFunc: zGeom, nArg: -1, SQLITE_ANY,
200302 p: (void *)pGeomCtx, xSFunc: geomCallback, xStep: 0, xFinal: 0, xDestroy: rtreeFreeCallback
200303 );
200304}
200305
200306/*
200307** Register a new 2nd-generation geometry function for use with the
200308** r-tree MATCH operator.
200309*/
200310SQLITE_API int sqlite3_rtree_query_callback(
200311 sqlite3 *db, /* Register SQL function on this connection */
200312 const char *zQueryFunc, /* Name of new SQL function */
200313 int (*xQueryFunc)(sqlite3_rtree_query_info*), /* Callback */
200314 void *pContext, /* Extra data passed into the callback */
200315 void (*xDestructor)(void*) /* Destructor for the extra data */
200316){
200317 RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
200318
200319 /* Allocate and populate the context object. */
200320 pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(n: sizeof(RtreeGeomCallback));
200321 if( !pGeomCtx ){
200322 if( xDestructor ) xDestructor(pContext);
200323 return SQLITE_NOMEM;
200324 }
200325 pGeomCtx->xGeom = 0;
200326 pGeomCtx->xQueryFunc = xQueryFunc;
200327 pGeomCtx->xDestructor = xDestructor;
200328 pGeomCtx->pContext = pContext;
200329 return sqlite3_create_function_v2(db, zFunc: zQueryFunc, nArg: -1, SQLITE_ANY,
200330 p: (void *)pGeomCtx, xSFunc: geomCallback, xStep: 0, xFinal: 0, xDestroy: rtreeFreeCallback
200331 );
200332}
200333
200334#if !SQLITE_CORE
200335#ifdef _WIN32
200336__declspec(dllexport)
200337#endif
200338SQLITE_API int sqlite3_rtree_init(
200339 sqlite3 *db,
200340 char **pzErrMsg,
200341 const sqlite3_api_routines *pApi
200342){
200343 SQLITE_EXTENSION_INIT2(pApi)
200344 return sqlite3RtreeInit(db);
200345}
200346#endif
200347
200348#endif
200349
200350/************** End of rtree.c ***********************************************/
200351/************** Begin file icu.c *********************************************/
200352/*
200353** 2007 May 6
200354**
200355** The author disclaims copyright to this source code. In place of
200356** a legal notice, here is a blessing:
200357**
200358** May you do good and not evil.
200359** May you find forgiveness for yourself and forgive others.
200360** May you share freely, never taking more than you give.
200361**
200362*************************************************************************
200363** $Id: icu.c,v 1.7 2007/12/13 21:54:11 drh Exp $
200364**
200365** This file implements an integration between the ICU library
200366** ("International Components for Unicode", an open-source library
200367** for handling unicode data) and SQLite. The integration uses
200368** ICU to provide the following to SQLite:
200369**
200370** * An implementation of the SQL regexp() function (and hence REGEXP
200371** operator) using the ICU uregex_XX() APIs.
200372**
200373** * Implementations of the SQL scalar upper() and lower() functions
200374** for case mapping.
200375**
200376** * Integration of ICU and SQLite collation sequences.
200377**
200378** * An implementation of the LIKE operator that uses ICU to
200379** provide case-independent matching.
200380*/
200381
200382#if !defined(SQLITE_CORE) \
200383 || defined(SQLITE_ENABLE_ICU) \
200384 || defined(SQLITE_ENABLE_ICU_COLLATIONS)
200385
200386/* Include ICU headers */
200387#include <unicode/utypes.h>
200388#include <unicode/uregex.h>
200389#include <unicode/ustring.h>
200390#include <unicode/ucol.h>
200391
200392/* #include <assert.h> */
200393
200394#ifndef SQLITE_CORE
200395/* #include "sqlite3ext.h" */
200396 SQLITE_EXTENSION_INIT1
200397#else
200398/* #include "sqlite3.h" */
200399#endif
200400
200401/*
200402** This function is called when an ICU function called from within
200403** the implementation of an SQL scalar function returns an error.
200404**
200405** The scalar function context passed as the first argument is
200406** loaded with an error message based on the following two args.
200407*/
200408static void icuFunctionError(
200409 sqlite3_context *pCtx, /* SQLite scalar function context */
200410 const char *zName, /* Name of ICU function that failed */
200411 UErrorCode e /* Error code returned by ICU function */
200412){
200413 char zBuf[128];
200414 sqlite3_snprintf(128, zBuf, "ICU error: %s(): %s", zName, u_errorName(e));
200415 zBuf[127] = '\0';
200416 sqlite3_result_error(pCtx, zBuf, -1);
200417}
200418
200419#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
200420
200421/*
200422** Maximum length (in bytes) of the pattern in a LIKE or GLOB
200423** operator.
200424*/
200425#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
200426# define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
200427#endif
200428
200429/*
200430** Version of sqlite3_free() that is always a function, never a macro.
200431*/
200432static void xFree(void *p){
200433 sqlite3_free(p);
200434}
200435
200436/*
200437** This lookup table is used to help decode the first byte of
200438** a multi-byte UTF8 character. It is copied here from SQLite source
200439** code file utf8.c.
200440*/
200441static const unsigned char icuUtf8Trans1[] = {
200442 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
200443 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
200444 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
200445 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
200446 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
200447 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
200448 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
200449 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
200450};
200451
200452#define SQLITE_ICU_READ_UTF8(zIn, c) \
200453 c = *(zIn++); \
200454 if( c>=0xc0 ){ \
200455 c = icuUtf8Trans1[c-0xc0]; \
200456 while( (*zIn & 0xc0)==0x80 ){ \
200457 c = (c<<6) + (0x3f & *(zIn++)); \
200458 } \
200459 }
200460
200461#define SQLITE_ICU_SKIP_UTF8(zIn) \
200462 assert( *zIn ); \
200463 if( *(zIn++)>=0xc0 ){ \
200464 while( (*zIn & 0xc0)==0x80 ){zIn++;} \
200465 }
200466
200467
200468/*
200469** Compare two UTF-8 strings for equality where the first string is
200470** a "LIKE" expression. Return true (1) if they are the same and
200471** false (0) if they are different.
200472*/
200473static int icuLikeCompare(
200474 const uint8_t *zPattern, /* LIKE pattern */
200475 const uint8_t *zString, /* The UTF-8 string to compare against */
200476 const UChar32 uEsc /* The escape character */
200477){
200478 static const uint32_t MATCH_ONE = (uint32_t)'_';
200479 static const uint32_t MATCH_ALL = (uint32_t)'%';
200480
200481 int prevEscape = 0; /* True if the previous character was uEsc */
200482
200483 while( 1 ){
200484
200485 /* Read (and consume) the next character from the input pattern. */
200486 uint32_t uPattern;
200487 SQLITE_ICU_READ_UTF8(zPattern, uPattern);
200488 if( uPattern==0 ) break;
200489
200490 /* There are now 4 possibilities:
200491 **
200492 ** 1. uPattern is an unescaped match-all character "%",
200493 ** 2. uPattern is an unescaped match-one character "_",
200494 ** 3. uPattern is an unescaped escape character, or
200495 ** 4. uPattern is to be handled as an ordinary character
200496 */
200497 if( uPattern==MATCH_ALL && !prevEscape && uPattern!=(uint32_t)uEsc ){
200498 /* Case 1. */
200499 uint8_t c;
200500
200501 /* Skip any MATCH_ALL or MATCH_ONE characters that follow a
200502 ** MATCH_ALL. For each MATCH_ONE, skip one character in the
200503 ** test string.
200504 */
200505 while( (c=*zPattern) == MATCH_ALL || c == MATCH_ONE ){
200506 if( c==MATCH_ONE ){
200507 if( *zString==0 ) return 0;
200508 SQLITE_ICU_SKIP_UTF8(zString);
200509 }
200510 zPattern++;
200511 }
200512
200513 if( *zPattern==0 ) return 1;
200514
200515 while( *zString ){
200516 if( icuLikeCompare(zPattern, zString, uEsc) ){
200517 return 1;
200518 }
200519 SQLITE_ICU_SKIP_UTF8(zString);
200520 }
200521 return 0;
200522
200523 }else if( uPattern==MATCH_ONE && !prevEscape && uPattern!=(uint32_t)uEsc ){
200524 /* Case 2. */
200525 if( *zString==0 ) return 0;
200526 SQLITE_ICU_SKIP_UTF8(zString);
200527
200528 }else if( uPattern==(uint32_t)uEsc && !prevEscape ){
200529 /* Case 3. */
200530 prevEscape = 1;
200531
200532 }else{
200533 /* Case 4. */
200534 uint32_t uString;
200535 SQLITE_ICU_READ_UTF8(zString, uString);
200536 uString = (uint32_t)u_foldCase((UChar32)uString, U_FOLD_CASE_DEFAULT);
200537 uPattern = (uint32_t)u_foldCase((UChar32)uPattern, U_FOLD_CASE_DEFAULT);
200538 if( uString!=uPattern ){
200539 return 0;
200540 }
200541 prevEscape = 0;
200542 }
200543 }
200544
200545 return *zString==0;
200546}
200547
200548/*
200549** Implementation of the like() SQL function. This function implements
200550** the build-in LIKE operator. The first argument to the function is the
200551** pattern and the second argument is the string. So, the SQL statements:
200552**
200553** A LIKE B
200554**
200555** is implemented as like(B, A). If there is an escape character E,
200556**
200557** A LIKE B ESCAPE E
200558**
200559** is mapped to like(B, A, E).
200560*/
200561static void icuLikeFunc(
200562 sqlite3_context *context,
200563 int argc,
200564 sqlite3_value **argv
200565){
200566 const unsigned char *zA = sqlite3_value_text(argv[0]);
200567 const unsigned char *zB = sqlite3_value_text(argv[1]);
200568 UChar32 uEsc = 0;
200569
200570 /* Limit the length of the LIKE or GLOB pattern to avoid problems
200571 ** of deep recursion and N*N behavior in patternCompare().
200572 */
200573 if( sqlite3_value_bytes(argv[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH ){
200574 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
200575 return;
200576 }
200577
200578
200579 if( argc==3 ){
200580 /* The escape character string must consist of a single UTF-8 character.
200581 ** Otherwise, return an error.
200582 */
200583 int nE= sqlite3_value_bytes(argv[2]);
200584 const unsigned char *zE = sqlite3_value_text(argv[2]);
200585 int i = 0;
200586 if( zE==0 ) return;
200587 U8_NEXT(zE, i, nE, uEsc);
200588 if( i!=nE){
200589 sqlite3_result_error(context,
200590 "ESCAPE expression must be a single character", -1);
200591 return;
200592 }
200593 }
200594
200595 if( zA && zB ){
200596 sqlite3_result_int(context, icuLikeCompare(zA, zB, uEsc));
200597 }
200598}
200599
200600/*
200601** Function to delete compiled regexp objects. Registered as
200602** a destructor function with sqlite3_set_auxdata().
200603*/
200604static void icuRegexpDelete(void *p){
200605 URegularExpression *pExpr = (URegularExpression *)p;
200606 uregex_close(pExpr);
200607}
200608
200609/*
200610** Implementation of SQLite REGEXP operator. This scalar function takes
200611** two arguments. The first is a regular expression pattern to compile
200612** the second is a string to match against that pattern. If either
200613** argument is an SQL NULL, then NULL Is returned. Otherwise, the result
200614** is 1 if the string matches the pattern, or 0 otherwise.
200615**
200616** SQLite maps the regexp() function to the regexp() operator such
200617** that the following two are equivalent:
200618**
200619** zString REGEXP zPattern
200620** regexp(zPattern, zString)
200621**
200622** Uses the following ICU regexp APIs:
200623**
200624** uregex_open()
200625** uregex_matches()
200626** uregex_close()
200627*/
200628static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){
200629 UErrorCode status = U_ZERO_ERROR;
200630 URegularExpression *pExpr;
200631 UBool res;
200632 const UChar *zString = sqlite3_value_text16(apArg[1]);
200633
200634 (void)nArg; /* Unused parameter */
200635
200636 /* If the left hand side of the regexp operator is NULL,
200637 ** then the result is also NULL.
200638 */
200639 if( !zString ){
200640 return;
200641 }
200642
200643 pExpr = sqlite3_get_auxdata(p, 0);
200644 if( !pExpr ){
200645 const UChar *zPattern = sqlite3_value_text16(apArg[0]);
200646 if( !zPattern ){
200647 return;
200648 }
200649 pExpr = uregex_open(zPattern, -1, 0, 0, &status);
200650
200651 if( U_SUCCESS(status) ){
200652 sqlite3_set_auxdata(p, 0, pExpr, icuRegexpDelete);
200653 }else{
200654 assert(!pExpr);
200655 icuFunctionError(p, "uregex_open", status);
200656 return;
200657 }
200658 }
200659
200660 /* Configure the text that the regular expression operates on. */
200661 uregex_setText(pExpr, zString, -1, &status);
200662 if( !U_SUCCESS(status) ){
200663 icuFunctionError(p, "uregex_setText", status);
200664 return;
200665 }
200666
200667 /* Attempt the match */
200668 res = uregex_matches(pExpr, 0, &status);
200669 if( !U_SUCCESS(status) ){
200670 icuFunctionError(p, "uregex_matches", status);
200671 return;
200672 }
200673
200674 /* Set the text that the regular expression operates on to a NULL
200675 ** pointer. This is not really necessary, but it is tidier than
200676 ** leaving the regular expression object configured with an invalid
200677 ** pointer after this function returns.
200678 */
200679 uregex_setText(pExpr, 0, 0, &status);
200680
200681 /* Return 1 or 0. */
200682 sqlite3_result_int(p, res ? 1 : 0);
200683}
200684
200685/*
200686** Implementations of scalar functions for case mapping - upper() and
200687** lower(). Function upper() converts its input to upper-case (ABC).
200688** Function lower() converts to lower-case (abc).
200689**
200690** ICU provides two types of case mapping, "general" case mapping and
200691** "language specific". Refer to ICU documentation for the differences
200692** between the two.
200693**
200694** To utilise "general" case mapping, the upper() or lower() scalar
200695** functions are invoked with one argument:
200696**
200697** upper('ABC') -> 'abc'
200698** lower('abc') -> 'ABC'
200699**
200700** To access ICU "language specific" case mapping, upper() or lower()
200701** should be invoked with two arguments. The second argument is the name
200702** of the locale to use. Passing an empty string ("") or SQL NULL value
200703** as the second argument is the same as invoking the 1 argument version
200704** of upper() or lower().
200705**
200706** lower('I', 'en_us') -> 'i'
200707** lower('I', 'tr_tr') -> '\u131' (small dotless i)
200708**
200709** http://www.icu-project.org/userguide/posix.html#case_mappings
200710*/
200711static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){
200712 const UChar *zInput; /* Pointer to input string */
200713 UChar *zOutput = 0; /* Pointer to output buffer */
200714 int nInput; /* Size of utf-16 input string in bytes */
200715 int nOut; /* Size of output buffer in bytes */
200716 int cnt;
200717 int bToUpper; /* True for toupper(), false for tolower() */
200718 UErrorCode status;
200719 const char *zLocale = 0;
200720
200721 assert(nArg==1 || nArg==2);
200722 bToUpper = (sqlite3_user_data(p)!=0);
200723 if( nArg==2 ){
200724 zLocale = (const char *)sqlite3_value_text(apArg[1]);
200725 }
200726
200727 zInput = sqlite3_value_text16(apArg[0]);
200728 if( !zInput ){
200729 return;
200730 }
200731 nOut = nInput = sqlite3_value_bytes16(apArg[0]);
200732 if( nOut==0 ){
200733 sqlite3_result_text16(p, "", 0, SQLITE_STATIC);
200734 return;
200735 }
200736
200737 for(cnt=0; cnt<2; cnt++){
200738 UChar *zNew = sqlite3_realloc(zOutput, nOut);
200739 if( zNew==0 ){
200740 sqlite3_free(zOutput);
200741 sqlite3_result_error_nomem(p);
200742 return;
200743 }
200744 zOutput = zNew;
200745 status = U_ZERO_ERROR;
200746 if( bToUpper ){
200747 nOut = 2*u_strToUpper(zOutput,nOut/2,zInput,nInput/2,zLocale,&status);
200748 }else{
200749 nOut = 2*u_strToLower(zOutput,nOut/2,zInput,nInput/2,zLocale,&status);
200750 }
200751
200752 if( U_SUCCESS(status) ){
200753 sqlite3_result_text16(p, zOutput, nOut, xFree);
200754 }else if( status==U_BUFFER_OVERFLOW_ERROR ){
200755 assert( cnt==0 );
200756 continue;
200757 }else{
200758 icuFunctionError(p, bToUpper ? "u_strToUpper" : "u_strToLower", status);
200759 }
200760 return;
200761 }
200762 assert( 0 ); /* Unreachable */
200763}
200764
200765#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU) */
200766
200767/*
200768** Collation sequence destructor function. The pCtx argument points to
200769** a UCollator structure previously allocated using ucol_open().
200770*/
200771static void icuCollationDel(void *pCtx){
200772 UCollator *p = (UCollator *)pCtx;
200773 ucol_close(p);
200774}
200775
200776/*
200777** Collation sequence comparison function. The pCtx argument points to
200778** a UCollator structure previously allocated using ucol_open().
200779*/
200780static int icuCollationColl(
200781 void *pCtx,
200782 int nLeft,
200783 const void *zLeft,
200784 int nRight,
200785 const void *zRight
200786){
200787 UCollationResult res;
200788 UCollator *p = (UCollator *)pCtx;
200789 res = ucol_strcoll(p, (UChar *)zLeft, nLeft/2, (UChar *)zRight, nRight/2);
200790 switch( res ){
200791 case UCOL_LESS: return -1;
200792 case UCOL_GREATER: return +1;
200793 case UCOL_EQUAL: return 0;
200794 }
200795 assert(!"Unexpected return value from ucol_strcoll()");
200796 return 0;
200797}
200798
200799/*
200800** Implementation of the scalar function icu_load_collation().
200801**
200802** This scalar function is used to add ICU collation based collation
200803** types to an SQLite database connection. It is intended to be called
200804** as follows:
200805**
200806** SELECT icu_load_collation(<locale>, <collation-name>);
200807**
200808** Where <locale> is a string containing an ICU locale identifier (i.e.
200809** "en_AU", "tr_TR" etc.) and <collation-name> is the name of the
200810** collation sequence to create.
200811*/
200812static void icuLoadCollation(
200813 sqlite3_context *p,
200814 int nArg,
200815 sqlite3_value **apArg
200816){
200817 sqlite3 *db = (sqlite3 *)sqlite3_user_data(p);
200818 UErrorCode status = U_ZERO_ERROR;
200819 const char *zLocale; /* Locale identifier - (eg. "jp_JP") */
200820 const char *zName; /* SQL Collation sequence name (eg. "japanese") */
200821 UCollator *pUCollator; /* ICU library collation object */
200822 int rc; /* Return code from sqlite3_create_collation_x() */
200823
200824 assert(nArg==2);
200825 (void)nArg; /* Unused parameter */
200826 zLocale = (const char *)sqlite3_value_text(apArg[0]);
200827 zName = (const char *)sqlite3_value_text(apArg[1]);
200828
200829 if( !zLocale || !zName ){
200830 return;
200831 }
200832
200833 pUCollator = ucol_open(zLocale, &status);
200834 if( !U_SUCCESS(status) ){
200835 icuFunctionError(p, "ucol_open", status);
200836 return;
200837 }
200838 assert(p);
200839
200840 rc = sqlite3_create_collation_v2(db, zName, SQLITE_UTF16, (void *)pUCollator,
200841 icuCollationColl, icuCollationDel
200842 );
200843 if( rc!=SQLITE_OK ){
200844 ucol_close(pUCollator);
200845 sqlite3_result_error(p, "Error registering collation function", -1);
200846 }
200847}
200848
200849/*
200850** Register the ICU extension functions with database db.
200851*/
200852SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db){
200853# define SQLITEICU_EXTRAFLAGS (SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS)
200854 static const struct IcuScalar {
200855 const char *zName; /* Function name */
200856 unsigned char nArg; /* Number of arguments */
200857 unsigned int enc; /* Optimal text encoding */
200858 unsigned char iContext; /* sqlite3_user_data() context */
200859 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
200860 } scalars[] = {
200861 {"icu_load_collation",2,SQLITE_UTF8|SQLITE_DIRECTONLY,1, icuLoadCollation},
200862#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
200863 {"regexp", 2, SQLITE_ANY|SQLITEICU_EXTRAFLAGS, 0, icuRegexpFunc},
200864 {"lower", 1, SQLITE_UTF16|SQLITEICU_EXTRAFLAGS, 0, icuCaseFunc16},
200865 {"lower", 2, SQLITE_UTF16|SQLITEICU_EXTRAFLAGS, 0, icuCaseFunc16},
200866 {"upper", 1, SQLITE_UTF16|SQLITEICU_EXTRAFLAGS, 1, icuCaseFunc16},
200867 {"upper", 2, SQLITE_UTF16|SQLITEICU_EXTRAFLAGS, 1, icuCaseFunc16},
200868 {"lower", 1, SQLITE_UTF8|SQLITEICU_EXTRAFLAGS, 0, icuCaseFunc16},
200869 {"lower", 2, SQLITE_UTF8|SQLITEICU_EXTRAFLAGS, 0, icuCaseFunc16},
200870 {"upper", 1, SQLITE_UTF8|SQLITEICU_EXTRAFLAGS, 1, icuCaseFunc16},
200871 {"upper", 2, SQLITE_UTF8|SQLITEICU_EXTRAFLAGS, 1, icuCaseFunc16},
200872 {"like", 2, SQLITE_UTF8|SQLITEICU_EXTRAFLAGS, 0, icuLikeFunc},
200873 {"like", 3, SQLITE_UTF8|SQLITEICU_EXTRAFLAGS, 0, icuLikeFunc},
200874#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU) */
200875 };
200876 int rc = SQLITE_OK;
200877 int i;
200878
200879 for(i=0; rc==SQLITE_OK && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){
200880 const struct IcuScalar *p = &scalars[i];
200881 rc = sqlite3_create_function(
200882 db, p->zName, p->nArg, p->enc,
200883 p->iContext ? (void*)db : (void*)0,
200884 p->xFunc, 0, 0
200885 );
200886 }
200887
200888 return rc;
200889}
200890
200891#if !SQLITE_CORE
200892#ifdef _WIN32
200893__declspec(dllexport)
200894#endif
200895SQLITE_API int sqlite3_icu_init(
200896 sqlite3 *db,
200897 char **pzErrMsg,
200898 const sqlite3_api_routines *pApi
200899){
200900 SQLITE_EXTENSION_INIT2(pApi)
200901 return sqlite3IcuInit(db);
200902}
200903#endif
200904
200905#endif
200906
200907/************** End of icu.c *************************************************/
200908/************** Begin file fts3_icu.c ****************************************/
200909/*
200910** 2007 June 22
200911**
200912** The author disclaims copyright to this source code. In place of
200913** a legal notice, here is a blessing:
200914**
200915** May you do good and not evil.
200916** May you find forgiveness for yourself and forgive others.
200917** May you share freely, never taking more than you give.
200918**
200919*************************************************************************
200920** This file implements a tokenizer for fts3 based on the ICU library.
200921*/
200922/* #include "fts3Int.h" */
200923#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
200924#ifdef SQLITE_ENABLE_ICU
200925
200926/* #include <assert.h> */
200927/* #include <string.h> */
200928/* #include "fts3_tokenizer.h" */
200929
200930#include <unicode/ubrk.h>
200931/* #include <unicode/ucol.h> */
200932/* #include <unicode/ustring.h> */
200933#include <unicode/utf16.h>
200934
200935typedef struct IcuTokenizer IcuTokenizer;
200936typedef struct IcuCursor IcuCursor;
200937
200938struct IcuTokenizer {
200939 sqlite3_tokenizer base;
200940 char *zLocale;
200941};
200942
200943struct IcuCursor {
200944 sqlite3_tokenizer_cursor base;
200945
200946 UBreakIterator *pIter; /* ICU break-iterator object */
200947 int nChar; /* Number of UChar elements in pInput */
200948 UChar *aChar; /* Copy of input using utf-16 encoding */
200949 int *aOffset; /* Offsets of each character in utf-8 input */
200950
200951 int nBuffer;
200952 char *zBuffer;
200953
200954 int iToken;
200955};
200956
200957/*
200958** Create a new tokenizer instance.
200959*/
200960static int icuCreate(
200961 int argc, /* Number of entries in argv[] */
200962 const char * const *argv, /* Tokenizer creation arguments */
200963 sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */
200964){
200965 IcuTokenizer *p;
200966 int n = 0;
200967
200968 if( argc>0 ){
200969 n = strlen(argv[0])+1;
200970 }
200971 p = (IcuTokenizer *)sqlite3_malloc64(sizeof(IcuTokenizer)+n);
200972 if( !p ){
200973 return SQLITE_NOMEM;
200974 }
200975 memset(p, 0, sizeof(IcuTokenizer));
200976
200977 if( n ){
200978 p->zLocale = (char *)&p[1];
200979 memcpy(p->zLocale, argv[0], n);
200980 }
200981
200982 *ppTokenizer = (sqlite3_tokenizer *)p;
200983
200984 return SQLITE_OK;
200985}
200986
200987/*
200988** Destroy a tokenizer
200989*/
200990static int icuDestroy(sqlite3_tokenizer *pTokenizer){
200991 IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
200992 sqlite3_free(p);
200993 return SQLITE_OK;
200994}
200995
200996/*
200997** Prepare to begin tokenizing a particular string. The input
200998** string to be tokenized is pInput[0..nBytes-1]. A cursor
200999** used to incrementally tokenize this string is returned in
201000** *ppCursor.
201001*/
201002static int icuOpen(
201003 sqlite3_tokenizer *pTokenizer, /* The tokenizer */
201004 const char *zInput, /* Input string */
201005 int nInput, /* Length of zInput in bytes */
201006 sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
201007){
201008 IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
201009 IcuCursor *pCsr;
201010
201011 const int32_t opt = U_FOLD_CASE_DEFAULT;
201012 UErrorCode status = U_ZERO_ERROR;
201013 int nChar;
201014
201015 UChar32 c;
201016 int iInput = 0;
201017 int iOut = 0;
201018
201019 *ppCursor = 0;
201020
201021 if( zInput==0 ){
201022 nInput = 0;
201023 zInput = "";
201024 }else if( nInput<0 ){
201025 nInput = strlen(zInput);
201026 }
201027 nChar = nInput+1;
201028 pCsr = (IcuCursor *)sqlite3_malloc64(
201029 sizeof(IcuCursor) + /* IcuCursor */
201030 ((nChar+3)&~3) * sizeof(UChar) + /* IcuCursor.aChar[] */
201031 (nChar+1) * sizeof(int) /* IcuCursor.aOffset[] */
201032 );
201033 if( !pCsr ){
201034 return SQLITE_NOMEM;
201035 }
201036 memset(pCsr, 0, sizeof(IcuCursor));
201037 pCsr->aChar = (UChar *)&pCsr[1];
201038 pCsr->aOffset = (int *)&pCsr->aChar[(nChar+3)&~3];
201039
201040 pCsr->aOffset[iOut] = iInput;
201041 U8_NEXT(zInput, iInput, nInput, c);
201042 while( c>0 ){
201043 int isError = 0;
201044 c = u_foldCase(c, opt);
201045 U16_APPEND(pCsr->aChar, iOut, nChar, c, isError);
201046 if( isError ){
201047 sqlite3_free(pCsr);
201048 return SQLITE_ERROR;
201049 }
201050 pCsr->aOffset[iOut] = iInput;
201051
201052 if( iInput<nInput ){
201053 U8_NEXT(zInput, iInput, nInput, c);
201054 }else{
201055 c = 0;
201056 }
201057 }
201058
201059 pCsr->pIter = ubrk_open(UBRK_WORD, p->zLocale, pCsr->aChar, iOut, &status);
201060 if( !U_SUCCESS(status) ){
201061 sqlite3_free(pCsr);
201062 return SQLITE_ERROR;
201063 }
201064 pCsr->nChar = iOut;
201065
201066 ubrk_first(pCsr->pIter);
201067 *ppCursor = (sqlite3_tokenizer_cursor *)pCsr;
201068 return SQLITE_OK;
201069}
201070
201071/*
201072** Close a tokenization cursor previously opened by a call to icuOpen().
201073*/
201074static int icuClose(sqlite3_tokenizer_cursor *pCursor){
201075 IcuCursor *pCsr = (IcuCursor *)pCursor;
201076 ubrk_close(pCsr->pIter);
201077 sqlite3_free(pCsr->zBuffer);
201078 sqlite3_free(pCsr);
201079 return SQLITE_OK;
201080}
201081
201082/*
201083** Extract the next token from a tokenization cursor.
201084*/
201085static int icuNext(
201086 sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by simpleOpen */
201087 const char **ppToken, /* OUT: *ppToken is the token text */
201088 int *pnBytes, /* OUT: Number of bytes in token */
201089 int *piStartOffset, /* OUT: Starting offset of token */
201090 int *piEndOffset, /* OUT: Ending offset of token */
201091 int *piPosition /* OUT: Position integer of token */
201092){
201093 IcuCursor *pCsr = (IcuCursor *)pCursor;
201094
201095 int iStart = 0;
201096 int iEnd = 0;
201097 int nByte = 0;
201098
201099 while( iStart==iEnd ){
201100 UChar32 c;
201101
201102 iStart = ubrk_current(pCsr->pIter);
201103 iEnd = ubrk_next(pCsr->pIter);
201104 if( iEnd==UBRK_DONE ){
201105 return SQLITE_DONE;
201106 }
201107
201108 while( iStart<iEnd ){
201109 int iWhite = iStart;
201110 U16_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
201111 if( u_isspace(c) ){
201112 iStart = iWhite;
201113 }else{
201114 break;
201115 }
201116 }
201117 assert(iStart<=iEnd);
201118 }
201119
201120 do {
201121 UErrorCode status = U_ZERO_ERROR;
201122 if( nByte ){
201123 char *zNew = sqlite3_realloc(pCsr->zBuffer, nByte);
201124 if( !zNew ){
201125 return SQLITE_NOMEM;
201126 }
201127 pCsr->zBuffer = zNew;
201128 pCsr->nBuffer = nByte;
201129 }
201130
201131 u_strToUTF8(
201132 pCsr->zBuffer, pCsr->nBuffer, &nByte, /* Output vars */
201133 &pCsr->aChar[iStart], iEnd-iStart, /* Input vars */
201134 &status /* Output success/failure */
201135 );
201136 } while( nByte>pCsr->nBuffer );
201137
201138 *ppToken = pCsr->zBuffer;
201139 *pnBytes = nByte;
201140 *piStartOffset = pCsr->aOffset[iStart];
201141 *piEndOffset = pCsr->aOffset[iEnd];
201142 *piPosition = pCsr->iToken++;
201143
201144 return SQLITE_OK;
201145}
201146
201147/*
201148** The set of routines that implement the simple tokenizer
201149*/
201150static const sqlite3_tokenizer_module icuTokenizerModule = {
201151 0, /* iVersion */
201152 icuCreate, /* xCreate */
201153 icuDestroy, /* xCreate */
201154 icuOpen, /* xOpen */
201155 icuClose, /* xClose */
201156 icuNext, /* xNext */
201157 0, /* xLanguageid */
201158};
201159
201160/*
201161** Set *ppModule to point at the implementation of the ICU tokenizer.
201162*/
201163SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(
201164 sqlite3_tokenizer_module const**ppModule
201165){
201166 *ppModule = &icuTokenizerModule;
201167}
201168
201169#endif /* defined(SQLITE_ENABLE_ICU) */
201170#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
201171
201172/************** End of fts3_icu.c ********************************************/
201173/************** Begin file sqlite3rbu.c **************************************/
201174/*
201175** 2014 August 30
201176**
201177** The author disclaims copyright to this source code. In place of
201178** a legal notice, here is a blessing:
201179**
201180** May you do good and not evil.
201181** May you find forgiveness for yourself and forgive others.
201182** May you share freely, never taking more than you give.
201183**
201184*************************************************************************
201185**
201186**
201187** OVERVIEW
201188**
201189** The RBU extension requires that the RBU update be packaged as an
201190** SQLite database. The tables it expects to find are described in
201191** sqlite3rbu.h. Essentially, for each table xyz in the target database
201192** that the user wishes to write to, a corresponding data_xyz table is
201193** created in the RBU database and populated with one row for each row to
201194** update, insert or delete from the target table.
201195**
201196** The update proceeds in three stages:
201197**
201198** 1) The database is updated. The modified database pages are written
201199** to a *-oal file. A *-oal file is just like a *-wal file, except
201200** that it is named "<database>-oal" instead of "<database>-wal".
201201** Because regular SQLite clients do not look for file named
201202** "<database>-oal", they go on using the original database in
201203** rollback mode while the *-oal file is being generated.
201204**
201205** During this stage RBU does not update the database by writing
201206** directly to the target tables. Instead it creates "imposter"
201207** tables using the SQLITE_TESTCTRL_IMPOSTER interface that it uses
201208** to update each b-tree individually. All updates required by each
201209** b-tree are completed before moving on to the next, and all
201210** updates are done in sorted key order.
201211**
201212** 2) The "<database>-oal" file is moved to the equivalent "<database>-wal"
201213** location using a call to rename(2). Before doing this the RBU
201214** module takes an EXCLUSIVE lock on the database file, ensuring
201215** that there are no other active readers.
201216**
201217** Once the EXCLUSIVE lock is released, any other database readers
201218** detect the new *-wal file and read the database in wal mode. At
201219** this point they see the new version of the database - including
201220** the updates made as part of the RBU update.
201221**
201222** 3) The new *-wal file is checkpointed. This proceeds in the same way
201223** as a regular database checkpoint, except that a single frame is
201224** checkpointed each time sqlite3rbu_step() is called. If the RBU
201225** handle is closed before the entire *-wal file is checkpointed,
201226** the checkpoint progress is saved in the RBU database and the
201227** checkpoint can be resumed by another RBU client at some point in
201228** the future.
201229**
201230** POTENTIAL PROBLEMS
201231**
201232** The rename() call might not be portable. And RBU is not currently
201233** syncing the directory after renaming the file.
201234**
201235** When state is saved, any commit to the *-oal file and the commit to
201236** the RBU update database are not atomic. So if the power fails at the
201237** wrong moment they might get out of sync. As the main database will be
201238** committed before the RBU update database this will likely either just
201239** pass unnoticed, or result in SQLITE_CONSTRAINT errors (due to UNIQUE
201240** constraint violations).
201241**
201242** If some client does modify the target database mid RBU update, or some
201243** other error occurs, the RBU extension will keep throwing errors. It's
201244** not really clear how to get out of this state. The system could just
201245** by delete the RBU update database and *-oal file and have the device
201246** download the update again and start over.
201247**
201248** At present, for an UPDATE, both the new.* and old.* records are
201249** collected in the rbu_xyz table. And for both UPDATEs and DELETEs all
201250** fields are collected. This means we're probably writing a lot more
201251** data to disk when saving the state of an ongoing update to the RBU
201252** update database than is strictly necessary.
201253**
201254*/
201255
201256/* #include <assert.h> */
201257/* #include <string.h> */
201258/* #include <stdio.h> */
201259
201260/* #include "sqlite3.h" */
201261
201262#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU)
201263/************** Include sqlite3rbu.h in the middle of sqlite3rbu.c ***********/
201264/************** Begin file sqlite3rbu.h **************************************/
201265/*
201266** 2014 August 30
201267**
201268** The author disclaims copyright to this source code. In place of
201269** a legal notice, here is a blessing:
201270**
201271** May you do good and not evil.
201272** May you find forgiveness for yourself and forgive others.
201273** May you share freely, never taking more than you give.
201274**
201275*************************************************************************
201276**
201277** This file contains the public interface for the RBU extension.
201278*/
201279
201280/*
201281** SUMMARY
201282**
201283** Writing a transaction containing a large number of operations on
201284** b-tree indexes that are collectively larger than the available cache
201285** memory can be very inefficient.
201286**
201287** The problem is that in order to update a b-tree, the leaf page (at least)
201288** containing the entry being inserted or deleted must be modified. If the
201289** working set of leaves is larger than the available cache memory, then a
201290** single leaf that is modified more than once as part of the transaction
201291** may be loaded from or written to the persistent media multiple times.
201292** Additionally, because the index updates are likely to be applied in
201293** random order, access to pages within the database is also likely to be in
201294** random order, which is itself quite inefficient.
201295**
201296** One way to improve the situation is to sort the operations on each index
201297** by index key before applying them to the b-tree. This leads to an IO
201298** pattern that resembles a single linear scan through the index b-tree,
201299** and all but guarantees each modified leaf page is loaded and stored
201300** exactly once. SQLite uses this trick to improve the performance of
201301** CREATE INDEX commands. This extension allows it to be used to improve
201302** the performance of large transactions on existing databases.
201303**
201304** Additionally, this extension allows the work involved in writing the
201305** large transaction to be broken down into sub-transactions performed
201306** sequentially by separate processes. This is useful if the system cannot
201307** guarantee that a single update process will run for long enough to apply
201308** the entire update, for example because the update is being applied on a
201309** mobile device that is frequently rebooted. Even after the writer process
201310** has committed one or more sub-transactions, other database clients continue
201311** to read from the original database snapshot. In other words, partially
201312** applied transactions are not visible to other clients.
201313**
201314** "RBU" stands for "Resumable Bulk Update". As in a large database update
201315** transmitted via a wireless network to a mobile device. A transaction
201316** applied using this extension is hence refered to as an "RBU update".
201317**
201318**
201319** LIMITATIONS
201320**
201321** An "RBU update" transaction is subject to the following limitations:
201322**
201323** * The transaction must consist of INSERT, UPDATE and DELETE operations
201324** only.
201325**
201326** * INSERT statements may not use any default values.
201327**
201328** * UPDATE and DELETE statements must identify their target rows by
201329** non-NULL PRIMARY KEY values. Rows with NULL values stored in PRIMARY
201330** KEY fields may not be updated or deleted. If the table being written
201331** has no PRIMARY KEY, affected rows must be identified by rowid.
201332**
201333** * UPDATE statements may not modify PRIMARY KEY columns.
201334**
201335** * No triggers will be fired.
201336**
201337** * No foreign key violations are detected or reported.
201338**
201339** * CHECK constraints are not enforced.
201340**
201341** * No constraint handling mode except for "OR ROLLBACK" is supported.
201342**
201343**
201344** PREPARATION
201345**
201346** An "RBU update" is stored as a separate SQLite database. A database
201347** containing an RBU update is an "RBU database". For each table in the
201348** target database to be updated, the RBU database should contain a table
201349** named "data_<target name>" containing the same set of columns as the
201350** target table, and one more - "rbu_control". The data_% table should
201351** have no PRIMARY KEY or UNIQUE constraints, but each column should have
201352** the same type as the corresponding column in the target database.
201353** The "rbu_control" column should have no type at all. For example, if
201354** the target database contains:
201355**
201356** CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c UNIQUE);
201357**
201358** Then the RBU database should contain:
201359**
201360** CREATE TABLE data_t1(a INTEGER, b TEXT, c, rbu_control);
201361**
201362** The order of the columns in the data_% table does not matter.
201363**
201364** Instead of a regular table, the RBU database may also contain virtual
201365** tables or view named using the data_<target> naming scheme.
201366**
201367** Instead of the plain data_<target> naming scheme, RBU database tables
201368** may also be named data<integer>_<target>, where <integer> is any sequence
201369** of zero or more numeric characters (0-9). This can be significant because
201370** tables within the RBU database are always processed in order sorted by
201371** name. By judicious selection of the <integer> portion of the names
201372** of the RBU tables the user can therefore control the order in which they
201373** are processed. This can be useful, for example, to ensure that "external
201374** content" FTS4 tables are updated before their underlying content tables.
201375**
201376** If the target database table is a virtual table or a table that has no
201377** PRIMARY KEY declaration, the data_% table must also contain a column
201378** named "rbu_rowid". This column is mapped to the tables implicit primary
201379** key column - "rowid". Virtual tables for which the "rowid" column does
201380** not function like a primary key value cannot be updated using RBU. For
201381** example, if the target db contains either of the following:
201382**
201383** CREATE VIRTUAL TABLE x1 USING fts3(a, b);
201384** CREATE TABLE x1(a, b)
201385**
201386** then the RBU database should contain:
201387**
201388** CREATE TABLE data_x1(a, b, rbu_rowid, rbu_control);
201389**
201390** All non-hidden columns (i.e. all columns matched by "SELECT *") of the
201391** target table must be present in the input table. For virtual tables,
201392** hidden columns are optional - they are updated by RBU if present in
201393** the input table, or not otherwise. For example, to write to an fts4
201394** table with a hidden languageid column such as:
201395**
201396** CREATE VIRTUAL TABLE ft1 USING fts4(a, b, languageid='langid');
201397**
201398** Either of the following input table schemas may be used:
201399**
201400** CREATE TABLE data_ft1(a, b, langid, rbu_rowid, rbu_control);
201401** CREATE TABLE data_ft1(a, b, rbu_rowid, rbu_control);
201402**
201403** For each row to INSERT into the target database as part of the RBU
201404** update, the corresponding data_% table should contain a single record
201405** with the "rbu_control" column set to contain integer value 0. The
201406** other columns should be set to the values that make up the new record
201407** to insert.
201408**
201409** If the target database table has an INTEGER PRIMARY KEY, it is not
201410** possible to insert a NULL value into the IPK column. Attempting to
201411** do so results in an SQLITE_MISMATCH error.
201412**
201413** For each row to DELETE from the target database as part of the RBU
201414** update, the corresponding data_% table should contain a single record
201415** with the "rbu_control" column set to contain integer value 1. The
201416** real primary key values of the row to delete should be stored in the
201417** corresponding columns of the data_% table. The values stored in the
201418** other columns are not used.
201419**
201420** For each row to UPDATE from the target database as part of the RBU
201421** update, the corresponding data_% table should contain a single record
201422** with the "rbu_control" column set to contain a value of type text.
201423** The real primary key values identifying the row to update should be
201424** stored in the corresponding columns of the data_% table row, as should
201425** the new values of all columns being update. The text value in the
201426** "rbu_control" column must contain the same number of characters as
201427** there are columns in the target database table, and must consist entirely
201428** of 'x' and '.' characters (or in some special cases 'd' - see below). For
201429** each column that is being updated, the corresponding character is set to
201430** 'x'. For those that remain as they are, the corresponding character of the
201431** rbu_control value should be set to '.'. For example, given the tables
201432** above, the update statement:
201433**
201434** UPDATE t1 SET c = 'usa' WHERE a = 4;
201435**
201436** is represented by the data_t1 row created by:
201437**
201438** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..x');
201439**
201440** Instead of an 'x' character, characters of the rbu_control value specified
201441** for UPDATEs may also be set to 'd'. In this case, instead of updating the
201442** target table with the value stored in the corresponding data_% column, the
201443** user-defined SQL function "rbu_delta()" is invoked and the result stored in
201444** the target table column. rbu_delta() is invoked with two arguments - the
201445** original value currently stored in the target table column and the
201446** value specified in the data_xxx table.
201447**
201448** For example, this row:
201449**
201450** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..d');
201451**
201452** is similar to an UPDATE statement such as:
201453**
201454** UPDATE t1 SET c = rbu_delta(c, 'usa') WHERE a = 4;
201455**
201456** Finally, if an 'f' character appears in place of a 'd' or 's' in an
201457** ota_control string, the contents of the data_xxx table column is assumed
201458** to be a "fossil delta" - a patch to be applied to a blob value in the
201459** format used by the fossil source-code management system. In this case
201460** the existing value within the target database table must be of type BLOB.
201461** It is replaced by the result of applying the specified fossil delta to
201462** itself.
201463**
201464** If the target database table is a virtual table or a table with no PRIMARY
201465** KEY, the rbu_control value should not include a character corresponding
201466** to the rbu_rowid value. For example, this:
201467**
201468** INSERT INTO data_ft1(a, b, rbu_rowid, rbu_control)
201469** VALUES(NULL, 'usa', 12, '.x');
201470**
201471** causes a result similar to:
201472**
201473** UPDATE ft1 SET b = 'usa' WHERE rowid = 12;
201474**
201475** The data_xxx tables themselves should have no PRIMARY KEY declarations.
201476** However, RBU is more efficient if reading the rows in from each data_xxx
201477** table in "rowid" order is roughly the same as reading them sorted by
201478** the PRIMARY KEY of the corresponding target database table. In other
201479** words, rows should be sorted using the destination table PRIMARY KEY
201480** fields before they are inserted into the data_xxx tables.
201481**
201482** USAGE
201483**
201484** The API declared below allows an application to apply an RBU update
201485** stored on disk to an existing target database. Essentially, the
201486** application:
201487**
201488** 1) Opens an RBU handle using the sqlite3rbu_open() function.
201489**
201490** 2) Registers any required virtual table modules with the database
201491** handle returned by sqlite3rbu_db(). Also, if required, register
201492** the rbu_delta() implementation.
201493**
201494** 3) Calls the sqlite3rbu_step() function one or more times on
201495** the new handle. Each call to sqlite3rbu_step() performs a single
201496** b-tree operation, so thousands of calls may be required to apply
201497** a complete update.
201498**
201499** 4) Calls sqlite3rbu_close() to close the RBU update handle. If
201500** sqlite3rbu_step() has been called enough times to completely
201501** apply the update to the target database, then the RBU database
201502** is marked as fully applied. Otherwise, the state of the RBU
201503** update application is saved in the RBU database for later
201504** resumption.
201505**
201506** See comments below for more detail on APIs.
201507**
201508** If an update is only partially applied to the target database by the
201509** time sqlite3rbu_close() is called, various state information is saved
201510** within the RBU database. This allows subsequent processes to automatically
201511** resume the RBU update from where it left off.
201512**
201513** To remove all RBU extension state information, returning an RBU database
201514** to its original contents, it is sufficient to drop all tables that begin
201515** with the prefix "rbu_"
201516**
201517** DATABASE LOCKING
201518**
201519** An RBU update may not be applied to a database in WAL mode. Attempting
201520** to do so is an error (SQLITE_ERROR).
201521**
201522** While an RBU handle is open, a SHARED lock may be held on the target
201523** database file. This means it is possible for other clients to read the
201524** database, but not to write it.
201525**
201526** If an RBU update is started and then suspended before it is completed,
201527** then an external client writes to the database, then attempting to resume
201528** the suspended RBU update is also an error (SQLITE_BUSY).
201529*/
201530
201531#ifndef _SQLITE3RBU_H
201532#define _SQLITE3RBU_H
201533
201534/* #include "sqlite3.h" ** Required for error code definitions ** */
201535
201536#if 0
201537extern "C" {
201538#endif
201539
201540typedef struct sqlite3rbu sqlite3rbu;
201541
201542/*
201543** Open an RBU handle.
201544**
201545** Argument zTarget is the path to the target database. Argument zRbu is
201546** the path to the RBU database. Each call to this function must be matched
201547** by a call to sqlite3rbu_close(). When opening the databases, RBU passes
201548** the SQLITE_CONFIG_URI flag to sqlite3_open_v2(). So if either zTarget
201549** or zRbu begin with "file:", it will be interpreted as an SQLite
201550** database URI, not a regular file name.
201551**
201552** If the zState argument is passed a NULL value, the RBU extension stores
201553** the current state of the update (how many rows have been updated, which
201554** indexes are yet to be updated etc.) within the RBU database itself. This
201555** can be convenient, as it means that the RBU application does not need to
201556** organize removing a separate state file after the update is concluded.
201557** Or, if zState is non-NULL, it must be a path to a database file in which
201558** the RBU extension can store the state of the update.
201559**
201560** When resuming an RBU update, the zState argument must be passed the same
201561** value as when the RBU update was started.
201562**
201563** Once the RBU update is finished, the RBU extension does not
201564** automatically remove any zState database file, even if it created it.
201565**
201566** By default, RBU uses the default VFS to access the files on disk. To
201567** use a VFS other than the default, an SQLite "file:" URI containing a
201568** "vfs=..." option may be passed as the zTarget option.
201569**
201570** IMPORTANT NOTE FOR ZIPVFS USERS: The RBU extension works with all of
201571** SQLite's built-in VFSs, including the multiplexor VFS. However it does
201572** not work out of the box with zipvfs. Refer to the comment describing
201573** the zipvfs_create_vfs() API below for details on using RBU with zipvfs.
201574*/
201575SQLITE_API sqlite3rbu *sqlite3rbu_open(
201576 const char *zTarget,
201577 const char *zRbu,
201578 const char *zState
201579);
201580
201581/*
201582** Open an RBU handle to perform an RBU vacuum on database file zTarget.
201583** An RBU vacuum is similar to SQLite's built-in VACUUM command, except
201584** that it can be suspended and resumed like an RBU update.
201585**
201586** The second argument to this function identifies a database in which
201587** to store the state of the RBU vacuum operation if it is suspended. The
201588** first time sqlite3rbu_vacuum() is called, to start an RBU vacuum
201589** operation, the state database should either not exist or be empty
201590** (contain no tables). If an RBU vacuum is suspended by calling
201591** sqlite3rbu_close() on the RBU handle before sqlite3rbu_step() has
201592** returned SQLITE_DONE, the vacuum state is stored in the state database.
201593** The vacuum can be resumed by calling this function to open a new RBU
201594** handle specifying the same target and state databases.
201595**
201596** If the second argument passed to this function is NULL, then the
201597** name of the state database is "<database>-vacuum", where <database>
201598** is the name of the target database file. In this case, on UNIX, if the
201599** state database is not already present in the file-system, it is created
201600** with the same permissions as the target db is made.
201601**
201602** With an RBU vacuum, it is an SQLITE_MISUSE error if the name of the
201603** state database ends with "-vactmp". This name is reserved for internal
201604** use.
201605**
201606** This function does not delete the state database after an RBU vacuum
201607** is completed, even if it created it. However, if the call to
201608** sqlite3rbu_close() returns any value other than SQLITE_OK, the contents
201609** of the state tables within the state database are zeroed. This way,
201610** the next call to sqlite3rbu_vacuum() opens a handle that starts a
201611** new RBU vacuum operation.
201612**
201613** As with sqlite3rbu_open(), Zipvfs users should rever to the comment
201614** describing the sqlite3rbu_create_vfs() API function below for
201615** a description of the complications associated with using RBU with
201616** zipvfs databases.
201617*/
201618SQLITE_API sqlite3rbu *sqlite3rbu_vacuum(
201619 const char *zTarget,
201620 const char *zState
201621);
201622
201623/*
201624** Configure a limit for the amount of temp space that may be used by
201625** the RBU handle passed as the first argument. The new limit is specified
201626** in bytes by the second parameter. If it is positive, the limit is updated.
201627** If the second parameter to this function is passed zero, then the limit
201628** is removed entirely. If the second parameter is negative, the limit is
201629** not modified (this is useful for querying the current limit).
201630**
201631** In all cases the returned value is the current limit in bytes (zero
201632** indicates unlimited).
201633**
201634** If the temp space limit is exceeded during operation, an SQLITE_FULL
201635** error is returned.
201636*/
201637SQLITE_API sqlite3_int64 sqlite3rbu_temp_size_limit(sqlite3rbu*, sqlite3_int64);
201638
201639/*
201640** Return the current amount of temp file space, in bytes, currently used by
201641** the RBU handle passed as the only argument.
201642*/
201643SQLITE_API sqlite3_int64 sqlite3rbu_temp_size(sqlite3rbu*);
201644
201645/*
201646** Internally, each RBU connection uses a separate SQLite database
201647** connection to access the target and rbu update databases. This
201648** API allows the application direct access to these database handles.
201649**
201650** The first argument passed to this function must be a valid, open, RBU
201651** handle. The second argument should be passed zero to access the target
201652** database handle, or non-zero to access the rbu update database handle.
201653** Accessing the underlying database handles may be useful in the
201654** following scenarios:
201655**
201656** * If any target tables are virtual tables, it may be necessary to
201657** call sqlite3_create_module() on the target database handle to
201658** register the required virtual table implementations.
201659**
201660** * If the data_xxx tables in the RBU source database are virtual
201661** tables, the application may need to call sqlite3_create_module() on
201662** the rbu update db handle to any required virtual table
201663** implementations.
201664**
201665** * If the application uses the "rbu_delta()" feature described above,
201666** it must use sqlite3_create_function() or similar to register the
201667** rbu_delta() implementation with the target database handle.
201668**
201669** If an error has occurred, either while opening or stepping the RBU object,
201670** this function may return NULL. The error code and message may be collected
201671** when sqlite3rbu_close() is called.
201672**
201673** Database handles returned by this function remain valid until the next
201674** call to any sqlite3rbu_xxx() function other than sqlite3rbu_db().
201675*/
201676SQLITE_API sqlite3 *sqlite3rbu_db(sqlite3rbu*, int bRbu);
201677
201678/*
201679** Do some work towards applying the RBU update to the target db.
201680**
201681** Return SQLITE_DONE if the update has been completely applied, or
201682** SQLITE_OK if no error occurs but there remains work to do to apply
201683** the RBU update. If an error does occur, some other error code is
201684** returned.
201685**
201686** Once a call to sqlite3rbu_step() has returned a value other than
201687** SQLITE_OK, all subsequent calls on the same RBU handle are no-ops
201688** that immediately return the same value.
201689*/
201690SQLITE_API int sqlite3rbu_step(sqlite3rbu *pRbu);
201691
201692/*
201693** Force RBU to save its state to disk.
201694**
201695** If a power failure or application crash occurs during an update, following
201696** system recovery RBU may resume the update from the point at which the state
201697** was last saved. In other words, from the most recent successful call to
201698** sqlite3rbu_close() or this function.
201699**
201700** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
201701*/
201702SQLITE_API int sqlite3rbu_savestate(sqlite3rbu *pRbu);
201703
201704/*
201705** Close an RBU handle.
201706**
201707** If the RBU update has been completely applied, mark the RBU database
201708** as fully applied. Otherwise, assuming no error has occurred, save the
201709** current state of the RBU update appliation to the RBU database.
201710**
201711** If an error has already occurred as part of an sqlite3rbu_step()
201712** or sqlite3rbu_open() call, or if one occurs within this function, an
201713** SQLite error code is returned. Additionally, if pzErrmsg is not NULL,
201714** *pzErrmsg may be set to point to a buffer containing a utf-8 formatted
201715** English language error message. It is the responsibility of the caller to
201716** eventually free any such buffer using sqlite3_free().
201717**
201718** Otherwise, if no error occurs, this function returns SQLITE_OK if the
201719** update has been partially applied, or SQLITE_DONE if it has been
201720** completely applied.
201721*/
201722SQLITE_API int sqlite3rbu_close(sqlite3rbu *pRbu, char **pzErrmsg);
201723
201724/*
201725** Return the total number of key-value operations (inserts, deletes or
201726** updates) that have been performed on the target database since the
201727** current RBU update was started.
201728*/
201729SQLITE_API sqlite3_int64 sqlite3rbu_progress(sqlite3rbu *pRbu);
201730
201731/*
201732** Obtain permyriadage (permyriadage is to 10000 as percentage is to 100)
201733** progress indications for the two stages of an RBU update. This API may
201734** be useful for driving GUI progress indicators and similar.
201735**
201736** An RBU update is divided into two stages:
201737**
201738** * Stage 1, in which changes are accumulated in an oal/wal file, and
201739** * Stage 2, in which the contents of the wal file are copied into the
201740** main database.
201741**
201742** The update is visible to non-RBU clients during stage 2. During stage 1
201743** non-RBU reader clients may see the original database.
201744**
201745** If this API is called during stage 2 of the update, output variable
201746** (*pnOne) is set to 10000 to indicate that stage 1 has finished and (*pnTwo)
201747** to a value between 0 and 10000 to indicate the permyriadage progress of
201748** stage 2. A value of 5000 indicates that stage 2 is half finished,
201749** 9000 indicates that it is 90% finished, and so on.
201750**
201751** If this API is called during stage 1 of the update, output variable
201752** (*pnTwo) is set to 0 to indicate that stage 2 has not yet started. The
201753** value to which (*pnOne) is set depends on whether or not the RBU
201754** database contains an "rbu_count" table. The rbu_count table, if it
201755** exists, must contain the same columns as the following:
201756**
201757** CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID;
201758**
201759** There must be one row in the table for each source (data_xxx) table within
201760** the RBU database. The 'tbl' column should contain the name of the source
201761** table. The 'cnt' column should contain the number of rows within the
201762** source table.
201763**
201764** If the rbu_count table is present and populated correctly and this
201765** API is called during stage 1, the *pnOne output variable is set to the
201766** permyriadage progress of the same stage. If the rbu_count table does
201767** not exist, then (*pnOne) is set to -1 during stage 1. If the rbu_count
201768** table exists but is not correctly populated, the value of the *pnOne
201769** output variable during stage 1 is undefined.
201770*/
201771SQLITE_API void sqlite3rbu_bp_progress(sqlite3rbu *pRbu, int *pnOne, int*pnTwo);
201772
201773/*
201774** Obtain an indication as to the current stage of an RBU update or vacuum.
201775** This function always returns one of the SQLITE_RBU_STATE_XXX constants
201776** defined in this file. Return values should be interpreted as follows:
201777**
201778** SQLITE_RBU_STATE_OAL:
201779** RBU is currently building a *-oal file. The next call to sqlite3rbu_step()
201780** may either add further data to the *-oal file, or compute data that will
201781** be added by a subsequent call.
201782**
201783** SQLITE_RBU_STATE_MOVE:
201784** RBU has finished building the *-oal file. The next call to sqlite3rbu_step()
201785** will move the *-oal file to the equivalent *-wal path. If the current
201786** operation is an RBU update, then the updated version of the database
201787** file will become visible to ordinary SQLite clients following the next
201788** call to sqlite3rbu_step().
201789**
201790** SQLITE_RBU_STATE_CHECKPOINT:
201791** RBU is currently performing an incremental checkpoint. The next call to
201792** sqlite3rbu_step() will copy a page of data from the *-wal file into
201793** the target database file.
201794**
201795** SQLITE_RBU_STATE_DONE:
201796** The RBU operation has finished. Any subsequent calls to sqlite3rbu_step()
201797** will immediately return SQLITE_DONE.
201798**
201799** SQLITE_RBU_STATE_ERROR:
201800** An error has occurred. Any subsequent calls to sqlite3rbu_step() will
201801** immediately return the SQLite error code associated with the error.
201802*/
201803#define SQLITE_RBU_STATE_OAL 1
201804#define SQLITE_RBU_STATE_MOVE 2
201805#define SQLITE_RBU_STATE_CHECKPOINT 3
201806#define SQLITE_RBU_STATE_DONE 4
201807#define SQLITE_RBU_STATE_ERROR 5
201808
201809SQLITE_API int sqlite3rbu_state(sqlite3rbu *pRbu);
201810
201811/*
201812** Create an RBU VFS named zName that accesses the underlying file-system
201813** via existing VFS zParent. Or, if the zParent parameter is passed NULL,
201814** then the new RBU VFS uses the default system VFS to access the file-system.
201815** The new object is registered as a non-default VFS with SQLite before
201816** returning.
201817**
201818** Part of the RBU implementation uses a custom VFS object. Usually, this
201819** object is created and deleted automatically by RBU.
201820**
201821** The exception is for applications that also use zipvfs. In this case,
201822** the custom VFS must be explicitly created by the user before the RBU
201823** handle is opened. The RBU VFS should be installed so that the zipvfs
201824** VFS uses the RBU VFS, which in turn uses any other VFS layers in use
201825** (for example multiplexor) to access the file-system. For example,
201826** to assemble an RBU enabled VFS stack that uses both zipvfs and
201827** multiplexor (error checking omitted):
201828**
201829** // Create a VFS named "multiplex" (not the default).
201830** sqlite3_multiplex_initialize(0, 0);
201831**
201832** // Create an rbu VFS named "rbu" that uses multiplexor. If the
201833** // second argument were replaced with NULL, the "rbu" VFS would
201834** // access the file-system via the system default VFS, bypassing the
201835** // multiplexor.
201836** sqlite3rbu_create_vfs("rbu", "multiplex");
201837**
201838** // Create a zipvfs VFS named "zipvfs" that uses rbu.
201839** zipvfs_create_vfs_v3("zipvfs", "rbu", 0, xCompressorAlgorithmDetector);
201840**
201841** // Make zipvfs the default VFS.
201842** sqlite3_vfs_register(sqlite3_vfs_find("zipvfs"), 1);
201843**
201844** Because the default VFS created above includes a RBU functionality, it
201845** may be used by RBU clients. Attempting to use RBU with a zipvfs VFS stack
201846** that does not include the RBU layer results in an error.
201847**
201848** The overhead of adding the "rbu" VFS to the system is negligible for
201849** non-RBU users. There is no harm in an application accessing the
201850** file-system via "rbu" all the time, even if it only uses RBU functionality
201851** occasionally.
201852*/
201853SQLITE_API int sqlite3rbu_create_vfs(const char *zName, const char *zParent);
201854
201855/*
201856** Deregister and destroy an RBU vfs created by an earlier call to
201857** sqlite3rbu_create_vfs().
201858**
201859** VFS objects are not reference counted. If a VFS object is destroyed
201860** before all database handles that use it have been closed, the results
201861** are undefined.
201862*/
201863SQLITE_API void sqlite3rbu_destroy_vfs(const char *zName);
201864
201865#if 0
201866} /* end of the 'extern "C"' block */
201867#endif
201868
201869#endif /* _SQLITE3RBU_H */
201870
201871/************** End of sqlite3rbu.h ******************************************/
201872/************** Continuing where we left off in sqlite3rbu.c *****************/
201873
201874#if defined(_WIN32_WCE)
201875/* #include "windows.h" */
201876#endif
201877
201878/* Maximum number of prepared UPDATE statements held by this module */
201879#define SQLITE_RBU_UPDATE_CACHESIZE 16
201880
201881/* Delta checksums disabled by default. Compile with -DRBU_ENABLE_DELTA_CKSUM
201882** to enable checksum verification.
201883*/
201884#ifndef RBU_ENABLE_DELTA_CKSUM
201885# define RBU_ENABLE_DELTA_CKSUM 0
201886#endif
201887
201888/*
201889** Swap two objects of type TYPE.
201890*/
201891#if !defined(SQLITE_AMALGAMATION)
201892# define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
201893#endif
201894
201895/*
201896** Name of the URI option that causes RBU to take an exclusive lock as
201897** part of the incremental checkpoint operation.
201898*/
201899#define RBU_EXCLUSIVE_CHECKPOINT "rbu_exclusive_checkpoint"
201900
201901
201902/*
201903** The rbu_state table is used to save the state of a partially applied
201904** update so that it can be resumed later. The table consists of integer
201905** keys mapped to values as follows:
201906**
201907** RBU_STATE_STAGE:
201908** May be set to integer values 1, 2, 4 or 5. As follows:
201909** 1: the *-rbu file is currently under construction.
201910** 2: the *-rbu file has been constructed, but not yet moved
201911** to the *-wal path.
201912** 4: the checkpoint is underway.
201913** 5: the rbu update has been checkpointed.
201914**
201915** RBU_STATE_TBL:
201916** Only valid if STAGE==1. The target database name of the table
201917** currently being written.
201918**
201919** RBU_STATE_IDX:
201920** Only valid if STAGE==1. The target database name of the index
201921** currently being written, or NULL if the main table is currently being
201922** updated.
201923**
201924** RBU_STATE_ROW:
201925** Only valid if STAGE==1. Number of rows already processed for the current
201926** table/index.
201927**
201928** RBU_STATE_PROGRESS:
201929** Trbul number of sqlite3rbu_step() calls made so far as part of this
201930** rbu update.
201931**
201932** RBU_STATE_CKPT:
201933** Valid if STAGE==4. The 64-bit checksum associated with the wal-index
201934** header created by recovering the *-wal file. This is used to detect
201935** cases when another client appends frames to the *-wal file in the
201936** middle of an incremental checkpoint (an incremental checkpoint cannot
201937** be continued if this happens).
201938**
201939** RBU_STATE_COOKIE:
201940** Valid if STAGE==1. The current change-counter cookie value in the
201941** target db file.
201942**
201943** RBU_STATE_OALSZ:
201944** Valid if STAGE==1. The size in bytes of the *-oal file.
201945**
201946** RBU_STATE_DATATBL:
201947** Only valid if STAGE==1. The RBU database name of the table
201948** currently being read.
201949*/
201950#define RBU_STATE_STAGE 1
201951#define RBU_STATE_TBL 2
201952#define RBU_STATE_IDX 3
201953#define RBU_STATE_ROW 4
201954#define RBU_STATE_PROGRESS 5
201955#define RBU_STATE_CKPT 6
201956#define RBU_STATE_COOKIE 7
201957#define RBU_STATE_OALSZ 8
201958#define RBU_STATE_PHASEONESTEP 9
201959#define RBU_STATE_DATATBL 10
201960
201961#define RBU_STAGE_OAL 1
201962#define RBU_STAGE_MOVE 2
201963#define RBU_STAGE_CAPTURE 3
201964#define RBU_STAGE_CKPT 4
201965#define RBU_STAGE_DONE 5
201966
201967
201968#define RBU_CREATE_STATE \
201969 "CREATE TABLE IF NOT EXISTS %s.rbu_state(k INTEGER PRIMARY KEY, v)"
201970
201971typedef struct RbuFrame RbuFrame;
201972typedef struct RbuObjIter RbuObjIter;
201973typedef struct RbuState RbuState;
201974typedef struct RbuSpan RbuSpan;
201975typedef struct rbu_vfs rbu_vfs;
201976typedef struct rbu_file rbu_file;
201977typedef struct RbuUpdateStmt RbuUpdateStmt;
201978
201979#if !defined(SQLITE_AMALGAMATION)
201980typedef unsigned int u32;
201981typedef unsigned short u16;
201982typedef unsigned char u8;
201983typedef sqlite3_int64 i64;
201984#endif
201985
201986/*
201987** These values must match the values defined in wal.c for the equivalent
201988** locks. These are not magic numbers as they are part of the SQLite file
201989** format.
201990*/
201991#define WAL_LOCK_WRITE 0
201992#define WAL_LOCK_CKPT 1
201993#define WAL_LOCK_READ0 3
201994
201995#define SQLITE_FCNTL_RBUCNT 5149216
201996
201997/*
201998** A structure to store values read from the rbu_state table in memory.
201999*/
202000struct RbuState {
202001 int eStage;
202002 char *zTbl;
202003 char *zDataTbl;
202004 char *zIdx;
202005 i64 iWalCksum;
202006 int nRow;
202007 i64 nProgress;
202008 u32 iCookie;
202009 i64 iOalSz;
202010 i64 nPhaseOneStep;
202011};
202012
202013struct RbuUpdateStmt {
202014 char *zMask; /* Copy of update mask used with pUpdate */
202015 sqlite3_stmt *pUpdate; /* Last update statement (or NULL) */
202016 RbuUpdateStmt *pNext;
202017};
202018
202019struct RbuSpan {
202020 const char *zSpan;
202021 int nSpan;
202022};
202023
202024/*
202025** An iterator of this type is used to iterate through all objects in
202026** the target database that require updating. For each such table, the
202027** iterator visits, in order:
202028**
202029** * the table itself,
202030** * each index of the table (zero or more points to visit), and
202031** * a special "cleanup table" state.
202032**
202033** abIndexed:
202034** If the table has no indexes on it, abIndexed is set to NULL. Otherwise,
202035** it points to an array of flags nTblCol elements in size. The flag is
202036** set for each column that is either a part of the PK or a part of an
202037** index. Or clear otherwise.
202038**
202039** If there are one or more partial indexes on the table, all fields of
202040** this array set set to 1. This is because in that case, the module has
202041** no way to tell which fields will be required to add and remove entries
202042** from the partial indexes.
202043**
202044*/
202045struct RbuObjIter {
202046 sqlite3_stmt *pTblIter; /* Iterate through tables */
202047 sqlite3_stmt *pIdxIter; /* Index iterator */
202048 int nTblCol; /* Size of azTblCol[] array */
202049 char **azTblCol; /* Array of unquoted target column names */
202050 char **azTblType; /* Array of target column types */
202051 int *aiSrcOrder; /* src table col -> target table col */
202052 u8 *abTblPk; /* Array of flags, set on target PK columns */
202053 u8 *abNotNull; /* Array of flags, set on NOT NULL columns */
202054 u8 *abIndexed; /* Array of flags, set on indexed & PK cols */
202055 int eType; /* Table type - an RBU_PK_XXX value */
202056
202057 /* Output variables. zTbl==0 implies EOF. */
202058 int bCleanup; /* True in "cleanup" state */
202059 const char *zTbl; /* Name of target db table */
202060 const char *zDataTbl; /* Name of rbu db table (or null) */
202061 const char *zIdx; /* Name of target db index (or null) */
202062 int iTnum; /* Root page of current object */
202063 int iPkTnum; /* If eType==EXTERNAL, root of PK index */
202064 int bUnique; /* Current index is unique */
202065 int nIndex; /* Number of aux. indexes on table zTbl */
202066
202067 /* Statements created by rbuObjIterPrepareAll() */
202068 int nCol; /* Number of columns in current object */
202069 sqlite3_stmt *pSelect; /* Source data */
202070 sqlite3_stmt *pInsert; /* Statement for INSERT operations */
202071 sqlite3_stmt *pDelete; /* Statement for DELETE ops */
202072 sqlite3_stmt *pTmpInsert; /* Insert into rbu_tmp_$zDataTbl */
202073 int nIdxCol;
202074 RbuSpan *aIdxCol;
202075 char *zIdxSql;
202076
202077 /* Last UPDATE used (for PK b-tree updates only), or NULL. */
202078 RbuUpdateStmt *pRbuUpdate;
202079};
202080
202081/*
202082** Values for RbuObjIter.eType
202083**
202084** 0: Table does not exist (error)
202085** 1: Table has an implicit rowid.
202086** 2: Table has an explicit IPK column.
202087** 3: Table has an external PK index.
202088** 4: Table is WITHOUT ROWID.
202089** 5: Table is a virtual table.
202090*/
202091#define RBU_PK_NOTABLE 0
202092#define RBU_PK_NONE 1
202093#define RBU_PK_IPK 2
202094#define RBU_PK_EXTERNAL 3
202095#define RBU_PK_WITHOUT_ROWID 4
202096#define RBU_PK_VTAB 5
202097
202098
202099/*
202100** Within the RBU_STAGE_OAL stage, each call to sqlite3rbu_step() performs
202101** one of the following operations.
202102*/
202103#define RBU_INSERT 1 /* Insert on a main table b-tree */
202104#define RBU_DELETE 2 /* Delete a row from a main table b-tree */
202105#define RBU_REPLACE 3 /* Delete and then insert a row */
202106#define RBU_IDX_DELETE 4 /* Delete a row from an aux. index b-tree */
202107#define RBU_IDX_INSERT 5 /* Insert on an aux. index b-tree */
202108
202109#define RBU_UPDATE 6 /* Update a row in a main table b-tree */
202110
202111/*
202112** A single step of an incremental checkpoint - frame iWalFrame of the wal
202113** file should be copied to page iDbPage of the database file.
202114*/
202115struct RbuFrame {
202116 u32 iDbPage;
202117 u32 iWalFrame;
202118};
202119
202120/*
202121** RBU handle.
202122**
202123** nPhaseOneStep:
202124** If the RBU database contains an rbu_count table, this value is set to
202125** a running estimate of the number of b-tree operations required to
202126** finish populating the *-oal file. This allows the sqlite3_bp_progress()
202127** API to calculate the permyriadage progress of populating the *-oal file
202128** using the formula:
202129**
202130** permyriadage = (10000 * nProgress) / nPhaseOneStep
202131**
202132** nPhaseOneStep is initialized to the sum of:
202133**
202134** nRow * (nIndex + 1)
202135**
202136** for all source tables in the RBU database, where nRow is the number
202137** of rows in the source table and nIndex the number of indexes on the
202138** corresponding target database table.
202139**
202140** This estimate is accurate if the RBU update consists entirely of
202141** INSERT operations. However, it is inaccurate if:
202142**
202143** * the RBU update contains any UPDATE operations. If the PK specified
202144** for an UPDATE operation does not exist in the target table, then
202145** no b-tree operations are required on index b-trees. Or if the
202146** specified PK does exist, then (nIndex*2) such operations are
202147** required (one delete and one insert on each index b-tree).
202148**
202149** * the RBU update contains any DELETE operations for which the specified
202150** PK does not exist. In this case no operations are required on index
202151** b-trees.
202152**
202153** * the RBU update contains REPLACE operations. These are similar to
202154** UPDATE operations.
202155**
202156** nPhaseOneStep is updated to account for the conditions above during the
202157** first pass of each source table. The updated nPhaseOneStep value is
202158** stored in the rbu_state table if the RBU update is suspended.
202159*/
202160struct sqlite3rbu {
202161 int eStage; /* Value of RBU_STATE_STAGE field */
202162 sqlite3 *dbMain; /* target database handle */
202163 sqlite3 *dbRbu; /* rbu database handle */
202164 char *zTarget; /* Path to target db */
202165 char *zRbu; /* Path to rbu db */
202166 char *zState; /* Path to state db (or NULL if zRbu) */
202167 char zStateDb[5]; /* Db name for state ("stat" or "main") */
202168 int rc; /* Value returned by last rbu_step() call */
202169 char *zErrmsg; /* Error message if rc!=SQLITE_OK */
202170 int nStep; /* Rows processed for current object */
202171 int nProgress; /* Rows processed for all objects */
202172 RbuObjIter objiter; /* Iterator for skipping through tbl/idx */
202173 const char *zVfsName; /* Name of automatically created rbu vfs */
202174 rbu_file *pTargetFd; /* File handle open on target db */
202175 int nPagePerSector; /* Pages per sector for pTargetFd */
202176 i64 iOalSz;
202177 i64 nPhaseOneStep;
202178
202179 /* The following state variables are used as part of the incremental
202180 ** checkpoint stage (eStage==RBU_STAGE_CKPT). See comments surrounding
202181 ** function rbuSetupCheckpoint() for details. */
202182 u32 iMaxFrame; /* Largest iWalFrame value in aFrame[] */
202183 u32 mLock;
202184 int nFrame; /* Entries in aFrame[] array */
202185 int nFrameAlloc; /* Allocated size of aFrame[] array */
202186 RbuFrame *aFrame;
202187 int pgsz;
202188 u8 *aBuf;
202189 i64 iWalCksum;
202190 i64 szTemp; /* Current size of all temp files in use */
202191 i64 szTempLimit; /* Total size limit for temp files */
202192
202193 /* Used in RBU vacuum mode only */
202194 int nRbu; /* Number of RBU VFS in the stack */
202195 rbu_file *pRbuFd; /* Fd for main db of dbRbu */
202196};
202197
202198/*
202199** An rbu VFS is implemented using an instance of this structure.
202200**
202201** Variable pRbu is only non-NULL for automatically created RBU VFS objects.
202202** It is NULL for RBU VFS objects created explicitly using
202203** sqlite3rbu_create_vfs(). It is used to track the total amount of temp
202204** space used by the RBU handle.
202205*/
202206struct rbu_vfs {
202207 sqlite3_vfs base; /* rbu VFS shim methods */
202208 sqlite3_vfs *pRealVfs; /* Underlying VFS */
202209 sqlite3_mutex *mutex; /* Mutex to protect pMain */
202210 sqlite3rbu *pRbu; /* Owner RBU object */
202211 rbu_file *pMain; /* List of main db files */
202212 rbu_file *pMainRbu; /* List of main db files with pRbu!=0 */
202213};
202214
202215/*
202216** Each file opened by an rbu VFS is represented by an instance of
202217** the following structure.
202218**
202219** If this is a temporary file (pRbu!=0 && flags&DELETE_ON_CLOSE), variable
202220** "sz" is set to the current size of the database file.
202221*/
202222struct rbu_file {
202223 sqlite3_file base; /* sqlite3_file methods */
202224 sqlite3_file *pReal; /* Underlying file handle */
202225 rbu_vfs *pRbuVfs; /* Pointer to the rbu_vfs object */
202226 sqlite3rbu *pRbu; /* Pointer to rbu object (rbu target only) */
202227 i64 sz; /* Size of file in bytes (temp only) */
202228
202229 int openFlags; /* Flags this file was opened with */
202230 u32 iCookie; /* Cookie value for main db files */
202231 u8 iWriteVer; /* "write-version" value for main db files */
202232 u8 bNolock; /* True to fail EXCLUSIVE locks */
202233
202234 int nShm; /* Number of entries in apShm[] array */
202235 char **apShm; /* Array of mmap'd *-shm regions */
202236 char *zDel; /* Delete this when closing file */
202237
202238 const char *zWal; /* Wal filename for this main db file */
202239 rbu_file *pWalFd; /* Wal file descriptor for this main db */
202240 rbu_file *pMainNext; /* Next MAIN_DB file */
202241 rbu_file *pMainRbuNext; /* Next MAIN_DB file with pRbu!=0 */
202242};
202243
202244/*
202245** True for an RBU vacuum handle, or false otherwise.
202246*/
202247#define rbuIsVacuum(p) ((p)->zTarget==0)
202248
202249
202250/*************************************************************************
202251** The following three functions, found below:
202252**
202253** rbuDeltaGetInt()
202254** rbuDeltaChecksum()
202255** rbuDeltaApply()
202256**
202257** are lifted from the fossil source code (http://fossil-scm.org). They
202258** are used to implement the scalar SQL function rbu_fossil_delta().
202259*/
202260
202261/*
202262** Read bytes from *pz and convert them into a positive integer. When
202263** finished, leave *pz pointing to the first character past the end of
202264** the integer. The *pLen parameter holds the length of the string
202265** in *pz and is decremented once for each character in the integer.
202266*/
202267static unsigned int rbuDeltaGetInt(const char **pz, int *pLen){
202268 static const signed char zValue[] = {
202269 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
202270 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
202271 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
202272 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
202273 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
202274 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, 36,
202275 -1, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
202276 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, -1, -1, -1, 63, -1,
202277 };
202278 unsigned int v = 0;
202279 int c;
202280 unsigned char *z = (unsigned char*)*pz;
202281 unsigned char *zStart = z;
202282 while( (c = zValue[0x7f&*(z++)])>=0 ){
202283 v = (v<<6) + c;
202284 }
202285 z--;
202286 *pLen -= z - zStart;
202287 *pz = (char*)z;
202288 return v;
202289}
202290
202291#if RBU_ENABLE_DELTA_CKSUM
202292/*
202293** Compute a 32-bit checksum on the N-byte buffer. Return the result.
202294*/
202295static unsigned int rbuDeltaChecksum(const char *zIn, size_t N){
202296 const unsigned char *z = (const unsigned char *)zIn;
202297 unsigned sum0 = 0;
202298 unsigned sum1 = 0;
202299 unsigned sum2 = 0;
202300 unsigned sum3 = 0;
202301 while(N >= 16){
202302 sum0 += ((unsigned)z[0] + z[4] + z[8] + z[12]);
202303 sum1 += ((unsigned)z[1] + z[5] + z[9] + z[13]);
202304 sum2 += ((unsigned)z[2] + z[6] + z[10]+ z[14]);
202305 sum3 += ((unsigned)z[3] + z[7] + z[11]+ z[15]);
202306 z += 16;
202307 N -= 16;
202308 }
202309 while(N >= 4){
202310 sum0 += z[0];
202311 sum1 += z[1];
202312 sum2 += z[2];
202313 sum3 += z[3];
202314 z += 4;
202315 N -= 4;
202316 }
202317 sum3 += (sum2 << 8) + (sum1 << 16) + (sum0 << 24);
202318 switch(N){
202319 case 3: sum3 += (z[2] << 8);
202320 case 2: sum3 += (z[1] << 16);
202321 case 1: sum3 += (z[0] << 24);
202322 default: ;
202323 }
202324 return sum3;
202325}
202326#endif
202327
202328/*
202329** Apply a delta.
202330**
202331** The output buffer should be big enough to hold the whole output
202332** file and a NUL terminator at the end. The delta_output_size()
202333** routine will determine this size for you.
202334**
202335** The delta string should be null-terminated. But the delta string
202336** may contain embedded NUL characters (if the input and output are
202337** binary files) so we also have to pass in the length of the delta in
202338** the lenDelta parameter.
202339**
202340** This function returns the size of the output file in bytes (excluding
202341** the final NUL terminator character). Except, if the delta string is
202342** malformed or intended for use with a source file other than zSrc,
202343** then this routine returns -1.
202344**
202345** Refer to the delta_create() documentation above for a description
202346** of the delta file format.
202347*/
202348static int rbuDeltaApply(
202349 const char *zSrc, /* The source or pattern file */
202350 int lenSrc, /* Length of the source file */
202351 const char *zDelta, /* Delta to apply to the pattern */
202352 int lenDelta, /* Length of the delta */
202353 char *zOut /* Write the output into this preallocated buffer */
202354){
202355 unsigned int limit;
202356 unsigned int total = 0;
202357#if RBU_ENABLE_DELTA_CKSUM
202358 char *zOrigOut = zOut;
202359#endif
202360
202361 limit = rbuDeltaGetInt(&zDelta, &lenDelta);
202362 if( *zDelta!='\n' ){
202363 /* ERROR: size integer not terminated by "\n" */
202364 return -1;
202365 }
202366 zDelta++; lenDelta--;
202367 while( *zDelta && lenDelta>0 ){
202368 unsigned int cnt, ofst;
202369 cnt = rbuDeltaGetInt(&zDelta, &lenDelta);
202370 switch( zDelta[0] ){
202371 case '@': {
202372 zDelta++; lenDelta--;
202373 ofst = rbuDeltaGetInt(&zDelta, &lenDelta);
202374 if( lenDelta>0 && zDelta[0]!=',' ){
202375 /* ERROR: copy command not terminated by ',' */
202376 return -1;
202377 }
202378 zDelta++; lenDelta--;
202379 total += cnt;
202380 if( total>limit ){
202381 /* ERROR: copy exceeds output file size */
202382 return -1;
202383 }
202384 if( (int)(ofst+cnt) > lenSrc ){
202385 /* ERROR: copy extends past end of input */
202386 return -1;
202387 }
202388 memcpy(zOut, &zSrc[ofst], cnt);
202389 zOut += cnt;
202390 break;
202391 }
202392 case ':': {
202393 zDelta++; lenDelta--;
202394 total += cnt;
202395 if( total>limit ){
202396 /* ERROR: insert command gives an output larger than predicted */
202397 return -1;
202398 }
202399 if( (int)cnt>lenDelta ){
202400 /* ERROR: insert count exceeds size of delta */
202401 return -1;
202402 }
202403 memcpy(zOut, zDelta, cnt);
202404 zOut += cnt;
202405 zDelta += cnt;
202406 lenDelta -= cnt;
202407 break;
202408 }
202409 case ';': {
202410 zDelta++; lenDelta--;
202411 zOut[0] = 0;
202412#if RBU_ENABLE_DELTA_CKSUM
202413 if( cnt!=rbuDeltaChecksum(zOrigOut, total) ){
202414 /* ERROR: bad checksum */
202415 return -1;
202416 }
202417#endif
202418 if( total!=limit ){
202419 /* ERROR: generated size does not match predicted size */
202420 return -1;
202421 }
202422 return total;
202423 }
202424 default: {
202425 /* ERROR: unknown delta operator */
202426 return -1;
202427 }
202428 }
202429 }
202430 /* ERROR: unterminated delta */
202431 return -1;
202432}
202433
202434static int rbuDeltaOutputSize(const char *zDelta, int lenDelta){
202435 int size;
202436 size = rbuDeltaGetInt(&zDelta, &lenDelta);
202437 if( *zDelta!='\n' ){
202438 /* ERROR: size integer not terminated by "\n" */
202439 return -1;
202440 }
202441 return size;
202442}
202443
202444/*
202445** End of code taken from fossil.
202446*************************************************************************/
202447
202448/*
202449** Implementation of SQL scalar function rbu_fossil_delta().
202450**
202451** This function applies a fossil delta patch to a blob. Exactly two
202452** arguments must be passed to this function. The first is the blob to
202453** patch and the second the patch to apply. If no error occurs, this
202454** function returns the patched blob.
202455*/
202456static void rbuFossilDeltaFunc(
202457 sqlite3_context *context,
202458 int argc,
202459 sqlite3_value **argv
202460){
202461 const char *aDelta;
202462 int nDelta;
202463 const char *aOrig;
202464 int nOrig;
202465
202466 int nOut;
202467 int nOut2;
202468 char *aOut;
202469
202470 assert( argc==2 );
202471
202472 nOrig = sqlite3_value_bytes(argv[0]);
202473 aOrig = (const char*)sqlite3_value_blob(argv[0]);
202474 nDelta = sqlite3_value_bytes(argv[1]);
202475 aDelta = (const char*)sqlite3_value_blob(argv[1]);
202476
202477 /* Figure out the size of the output */
202478 nOut = rbuDeltaOutputSize(aDelta, nDelta);
202479 if( nOut<0 ){
202480 sqlite3_result_error(context, "corrupt fossil delta", -1);
202481 return;
202482 }
202483
202484 aOut = sqlite3_malloc(nOut+1);
202485 if( aOut==0 ){
202486 sqlite3_result_error_nomem(context);
202487 }else{
202488 nOut2 = rbuDeltaApply(aOrig, nOrig, aDelta, nDelta, aOut);
202489 if( nOut2!=nOut ){
202490 sqlite3_free(aOut);
202491 sqlite3_result_error(context, "corrupt fossil delta", -1);
202492 }else{
202493 sqlite3_result_blob(context, aOut, nOut, sqlite3_free);
202494 }
202495 }
202496}
202497
202498
202499/*
202500** Prepare the SQL statement in buffer zSql against database handle db.
202501** If successful, set *ppStmt to point to the new statement and return
202502** SQLITE_OK.
202503**
202504** Otherwise, if an error does occur, set *ppStmt to NULL and return
202505** an SQLite error code. Additionally, set output variable *pzErrmsg to
202506** point to a buffer containing an error message. It is the responsibility
202507** of the caller to (eventually) free this buffer using sqlite3_free().
202508*/
202509static int prepareAndCollectError(
202510 sqlite3 *db,
202511 sqlite3_stmt **ppStmt,
202512 char **pzErrmsg,
202513 const char *zSql
202514){
202515 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
202516 if( rc!=SQLITE_OK ){
202517 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
202518 *ppStmt = 0;
202519 }
202520 return rc;
202521}
202522
202523/*
202524** Reset the SQL statement passed as the first argument. Return a copy
202525** of the value returned by sqlite3_reset().
202526**
202527** If an error has occurred, then set *pzErrmsg to point to a buffer
202528** containing an error message. It is the responsibility of the caller
202529** to eventually free this buffer using sqlite3_free().
202530*/
202531static int resetAndCollectError(sqlite3_stmt *pStmt, char **pzErrmsg){
202532 int rc = sqlite3_reset(pStmt);
202533 if( rc!=SQLITE_OK ){
202534 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(sqlite3_db_handle(pStmt)));
202535 }
202536 return rc;
202537}
202538
202539/*
202540** Unless it is NULL, argument zSql points to a buffer allocated using
202541** sqlite3_malloc containing an SQL statement. This function prepares the SQL
202542** statement against database db and frees the buffer. If statement
202543** compilation is successful, *ppStmt is set to point to the new statement
202544** handle and SQLITE_OK is returned.
202545**
202546** Otherwise, if an error occurs, *ppStmt is set to NULL and an error code
202547** returned. In this case, *pzErrmsg may also be set to point to an error
202548** message. It is the responsibility of the caller to free this error message
202549** buffer using sqlite3_free().
202550**
202551** If argument zSql is NULL, this function assumes that an OOM has occurred.
202552** In this case SQLITE_NOMEM is returned and *ppStmt set to NULL.
202553*/
202554static int prepareFreeAndCollectError(
202555 sqlite3 *db,
202556 sqlite3_stmt **ppStmt,
202557 char **pzErrmsg,
202558 char *zSql
202559){
202560 int rc;
202561 assert( *pzErrmsg==0 );
202562 if( zSql==0 ){
202563 rc = SQLITE_NOMEM;
202564 *ppStmt = 0;
202565 }else{
202566 rc = prepareAndCollectError(db, ppStmt, pzErrmsg, zSql);
202567 sqlite3_free(zSql);
202568 }
202569 return rc;
202570}
202571
202572/*
202573** Free the RbuObjIter.azTblCol[] and RbuObjIter.abTblPk[] arrays allocated
202574** by an earlier call to rbuObjIterCacheTableInfo().
202575*/
202576static void rbuObjIterFreeCols(RbuObjIter *pIter){
202577 int i;
202578 for(i=0; i<pIter->nTblCol; i++){
202579 sqlite3_free(pIter->azTblCol[i]);
202580 sqlite3_free(pIter->azTblType[i]);
202581 }
202582 sqlite3_free(pIter->azTblCol);
202583 pIter->azTblCol = 0;
202584 pIter->azTblType = 0;
202585 pIter->aiSrcOrder = 0;
202586 pIter->abTblPk = 0;
202587 pIter->abNotNull = 0;
202588 pIter->nTblCol = 0;
202589 pIter->eType = 0; /* Invalid value */
202590}
202591
202592/*
202593** Finalize all statements and free all allocations that are specific to
202594** the current object (table/index pair).
202595*/
202596static void rbuObjIterClearStatements(RbuObjIter *pIter){
202597 RbuUpdateStmt *pUp;
202598
202599 sqlite3_finalize(pIter->pSelect);
202600 sqlite3_finalize(pIter->pInsert);
202601 sqlite3_finalize(pIter->pDelete);
202602 sqlite3_finalize(pIter->pTmpInsert);
202603 pUp = pIter->pRbuUpdate;
202604 while( pUp ){
202605 RbuUpdateStmt *pTmp = pUp->pNext;
202606 sqlite3_finalize(pUp->pUpdate);
202607 sqlite3_free(pUp);
202608 pUp = pTmp;
202609 }
202610 sqlite3_free(pIter->aIdxCol);
202611 sqlite3_free(pIter->zIdxSql);
202612
202613 pIter->pSelect = 0;
202614 pIter->pInsert = 0;
202615 pIter->pDelete = 0;
202616 pIter->pRbuUpdate = 0;
202617 pIter->pTmpInsert = 0;
202618 pIter->nCol = 0;
202619 pIter->nIdxCol = 0;
202620 pIter->aIdxCol = 0;
202621 pIter->zIdxSql = 0;
202622}
202623
202624/*
202625** Clean up any resources allocated as part of the iterator object passed
202626** as the only argument.
202627*/
202628static void rbuObjIterFinalize(RbuObjIter *pIter){
202629 rbuObjIterClearStatements(pIter);
202630 sqlite3_finalize(pIter->pTblIter);
202631 sqlite3_finalize(pIter->pIdxIter);
202632 rbuObjIterFreeCols(pIter);
202633 memset(pIter, 0, sizeof(RbuObjIter));
202634}
202635
202636/*
202637** Advance the iterator to the next position.
202638**
202639** If no error occurs, SQLITE_OK is returned and the iterator is left
202640** pointing to the next entry. Otherwise, an error code and message is
202641** left in the RBU handle passed as the first argument. A copy of the
202642** error code is returned.
202643*/
202644static int rbuObjIterNext(sqlite3rbu *p, RbuObjIter *pIter){
202645 int rc = p->rc;
202646 if( rc==SQLITE_OK ){
202647
202648 /* Free any SQLite statements used while processing the previous object */
202649 rbuObjIterClearStatements(pIter);
202650 if( pIter->zIdx==0 ){
202651 rc = sqlite3_exec(p->dbMain,
202652 "DROP TRIGGER IF EXISTS temp.rbu_insert_tr;"
202653 "DROP TRIGGER IF EXISTS temp.rbu_update1_tr;"
202654 "DROP TRIGGER IF EXISTS temp.rbu_update2_tr;"
202655 "DROP TRIGGER IF EXISTS temp.rbu_delete_tr;"
202656 , 0, 0, &p->zErrmsg
202657 );
202658 }
202659
202660 if( rc==SQLITE_OK ){
202661 if( pIter->bCleanup ){
202662 rbuObjIterFreeCols(pIter);
202663 pIter->bCleanup = 0;
202664 rc = sqlite3_step(pIter->pTblIter);
202665 if( rc!=SQLITE_ROW ){
202666 rc = resetAndCollectError(pIter->pTblIter, &p->zErrmsg);
202667 pIter->zTbl = 0;
202668 }else{
202669 pIter->zTbl = (const char*)sqlite3_column_text(pIter->pTblIter, 0);
202670 pIter->zDataTbl = (const char*)sqlite3_column_text(pIter->pTblIter,1);
202671 rc = (pIter->zDataTbl && pIter->zTbl) ? SQLITE_OK : SQLITE_NOMEM;
202672 }
202673 }else{
202674 if( pIter->zIdx==0 ){
202675 sqlite3_stmt *pIdx = pIter->pIdxIter;
202676 rc = sqlite3_bind_text(pIdx, 1, pIter->zTbl, -1, SQLITE_STATIC);
202677 }
202678 if( rc==SQLITE_OK ){
202679 rc = sqlite3_step(pIter->pIdxIter);
202680 if( rc!=SQLITE_ROW ){
202681 rc = resetAndCollectError(pIter->pIdxIter, &p->zErrmsg);
202682 pIter->bCleanup = 1;
202683 pIter->zIdx = 0;
202684 }else{
202685 pIter->zIdx = (const char*)sqlite3_column_text(pIter->pIdxIter, 0);
202686 pIter->iTnum = sqlite3_column_int(pIter->pIdxIter, 1);
202687 pIter->bUnique = sqlite3_column_int(pIter->pIdxIter, 2);
202688 rc = pIter->zIdx ? SQLITE_OK : SQLITE_NOMEM;
202689 }
202690 }
202691 }
202692 }
202693 }
202694
202695 if( rc!=SQLITE_OK ){
202696 rbuObjIterFinalize(pIter);
202697 p->rc = rc;
202698 }
202699 return rc;
202700}
202701
202702
202703/*
202704** The implementation of the rbu_target_name() SQL function. This function
202705** accepts one or two arguments. The first argument is the name of a table -
202706** the name of a table in the RBU database. The second, if it is present, is 1
202707** for a view or 0 for a table.
202708**
202709** For a non-vacuum RBU handle, if the table name matches the pattern:
202710**
202711** data[0-9]_<name>
202712**
202713** where <name> is any sequence of 1 or more characters, <name> is returned.
202714** Otherwise, if the only argument does not match the above pattern, an SQL
202715** NULL is returned.
202716**
202717** "data_t1" -> "t1"
202718** "data0123_t2" -> "t2"
202719** "dataAB_t3" -> NULL
202720**
202721** For an rbu vacuum handle, a copy of the first argument is returned if
202722** the second argument is either missing or 0 (not a view).
202723*/
202724static void rbuTargetNameFunc(
202725 sqlite3_context *pCtx,
202726 int argc,
202727 sqlite3_value **argv
202728){
202729 sqlite3rbu *p = sqlite3_user_data(pCtx);
202730 const char *zIn;
202731 assert( argc==1 || argc==2 );
202732
202733 zIn = (const char*)sqlite3_value_text(argv[0]);
202734 if( zIn ){
202735 if( rbuIsVacuum(p) ){
202736 assert( argc==2 || argc==1 );
202737 if( argc==1 || 0==sqlite3_value_int(argv[1]) ){
202738 sqlite3_result_text(pCtx, zIn, -1, SQLITE_STATIC);
202739 }
202740 }else{
202741 if( strlen(zIn)>4 && memcmp("data", zIn, 4)==0 ){
202742 int i;
202743 for(i=4; zIn[i]>='0' && zIn[i]<='9'; i++);
202744 if( zIn[i]=='_' && zIn[i+1] ){
202745 sqlite3_result_text(pCtx, &zIn[i+1], -1, SQLITE_STATIC);
202746 }
202747 }
202748 }
202749 }
202750}
202751
202752/*
202753** Initialize the iterator structure passed as the second argument.
202754**
202755** If no error occurs, SQLITE_OK is returned and the iterator is left
202756** pointing to the first entry. Otherwise, an error code and message is
202757** left in the RBU handle passed as the first argument. A copy of the
202758** error code is returned.
202759*/
202760static int rbuObjIterFirst(sqlite3rbu *p, RbuObjIter *pIter){
202761 int rc;
202762 memset(pIter, 0, sizeof(RbuObjIter));
202763
202764 rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pTblIter, &p->zErrmsg,
202765 sqlite3_mprintf(
202766 "SELECT rbu_target_name(name, type='view') AS target, name "
202767 "FROM sqlite_schema "
202768 "WHERE type IN ('table', 'view') AND target IS NOT NULL "
202769 " %s "
202770 "ORDER BY name"
202771 , rbuIsVacuum(p) ? "AND rootpage!=0 AND rootpage IS NOT NULL" : ""));
202772
202773 if( rc==SQLITE_OK ){
202774 rc = prepareAndCollectError(p->dbMain, &pIter->pIdxIter, &p->zErrmsg,
202775 "SELECT name, rootpage, sql IS NULL OR substr(8, 6)=='UNIQUE' "
202776 " FROM main.sqlite_schema "
202777 " WHERE type='index' AND tbl_name = ?"
202778 );
202779 }
202780
202781 pIter->bCleanup = 1;
202782 p->rc = rc;
202783 return rbuObjIterNext(p, pIter);
202784}
202785
202786/*
202787** This is a wrapper around "sqlite3_mprintf(zFmt, ...)". If an OOM occurs,
202788** an error code is stored in the RBU handle passed as the first argument.
202789**
202790** If an error has already occurred (p->rc is already set to something other
202791** than SQLITE_OK), then this function returns NULL without modifying the
202792** stored error code. In this case it still calls sqlite3_free() on any
202793** printf() parameters associated with %z conversions.
202794*/
202795static char *rbuMPrintf(sqlite3rbu *p, const char *zFmt, ...){
202796 char *zSql = 0;
202797 va_list ap;
202798 va_start(ap, zFmt);
202799 zSql = sqlite3_vmprintf(zFmt, ap);
202800 if( p->rc==SQLITE_OK ){
202801 if( zSql==0 ) p->rc = SQLITE_NOMEM;
202802 }else{
202803 sqlite3_free(zSql);
202804 zSql = 0;
202805 }
202806 va_end(ap);
202807 return zSql;
202808}
202809
202810/*
202811** Argument zFmt is a sqlite3_mprintf() style format string. The trailing
202812** arguments are the usual subsitution values. This function performs
202813** the printf() style substitutions and executes the result as an SQL
202814** statement on the RBU handles database.
202815**
202816** If an error occurs, an error code and error message is stored in the
202817** RBU handle. If an error has already occurred when this function is
202818** called, it is a no-op.
202819*/
202820static int rbuMPrintfExec(sqlite3rbu *p, sqlite3 *db, const char *zFmt, ...){
202821 va_list ap;
202822 char *zSql;
202823 va_start(ap, zFmt);
202824 zSql = sqlite3_vmprintf(zFmt, ap);
202825 if( p->rc==SQLITE_OK ){
202826 if( zSql==0 ){
202827 p->rc = SQLITE_NOMEM;
202828 }else{
202829 p->rc = sqlite3_exec(db, zSql, 0, 0, &p->zErrmsg);
202830 }
202831 }
202832 sqlite3_free(zSql);
202833 va_end(ap);
202834 return p->rc;
202835}
202836
202837/*
202838** Attempt to allocate and return a pointer to a zeroed block of nByte
202839** bytes.
202840**
202841** If an error (i.e. an OOM condition) occurs, return NULL and leave an
202842** error code in the rbu handle passed as the first argument. Or, if an
202843** error has already occurred when this function is called, return NULL
202844** immediately without attempting the allocation or modifying the stored
202845** error code.
202846*/
202847static void *rbuMalloc(sqlite3rbu *p, sqlite3_int64 nByte){
202848 void *pRet = 0;
202849 if( p->rc==SQLITE_OK ){
202850 assert( nByte>0 );
202851 pRet = sqlite3_malloc64(nByte);
202852 if( pRet==0 ){
202853 p->rc = SQLITE_NOMEM;
202854 }else{
202855 memset(pRet, 0, nByte);
202856 }
202857 }
202858 return pRet;
202859}
202860
202861
202862/*
202863** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that
202864** there is room for at least nCol elements. If an OOM occurs, store an
202865** error code in the RBU handle passed as the first argument.
202866*/
202867static void rbuAllocateIterArrays(sqlite3rbu *p, RbuObjIter *pIter, int nCol){
202868 sqlite3_int64 nByte = (2*sizeof(char*) + sizeof(int) + 3*sizeof(u8)) * nCol;
202869 char **azNew;
202870
202871 azNew = (char**)rbuMalloc(p, nByte);
202872 if( azNew ){
202873 pIter->azTblCol = azNew;
202874 pIter->azTblType = &azNew[nCol];
202875 pIter->aiSrcOrder = (int*)&pIter->azTblType[nCol];
202876 pIter->abTblPk = (u8*)&pIter->aiSrcOrder[nCol];
202877 pIter->abNotNull = (u8*)&pIter->abTblPk[nCol];
202878 pIter->abIndexed = (u8*)&pIter->abNotNull[nCol];
202879 }
202880}
202881
202882/*
202883** The first argument must be a nul-terminated string. This function
202884** returns a copy of the string in memory obtained from sqlite3_malloc().
202885** It is the responsibility of the caller to eventually free this memory
202886** using sqlite3_free().
202887**
202888** If an OOM condition is encountered when attempting to allocate memory,
202889** output variable (*pRc) is set to SQLITE_NOMEM before returning. Otherwise,
202890** if the allocation succeeds, (*pRc) is left unchanged.
202891*/
202892static char *rbuStrndup(const char *zStr, int *pRc){
202893 char *zRet = 0;
202894
202895 if( *pRc==SQLITE_OK ){
202896 if( zStr ){
202897 size_t nCopy = strlen(zStr) + 1;
202898 zRet = (char*)sqlite3_malloc64(nCopy);
202899 if( zRet ){
202900 memcpy(zRet, zStr, nCopy);
202901 }else{
202902 *pRc = SQLITE_NOMEM;
202903 }
202904 }
202905 }
202906
202907 return zRet;
202908}
202909
202910/*
202911** Finalize the statement passed as the second argument.
202912**
202913** If the sqlite3_finalize() call indicates that an error occurs, and the
202914** rbu handle error code is not already set, set the error code and error
202915** message accordingly.
202916*/
202917static void rbuFinalize(sqlite3rbu *p, sqlite3_stmt *pStmt){
202918 sqlite3 *db = sqlite3_db_handle(pStmt);
202919 int rc = sqlite3_finalize(pStmt);
202920 if( p->rc==SQLITE_OK && rc!=SQLITE_OK ){
202921 p->rc = rc;
202922 p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
202923 }
202924}
202925
202926/* Determine the type of a table.
202927**
202928** peType is of type (int*), a pointer to an output parameter of type
202929** (int). This call sets the output parameter as follows, depending
202930** on the type of the table specified by parameters dbName and zTbl.
202931**
202932** RBU_PK_NOTABLE: No such table.
202933** RBU_PK_NONE: Table has an implicit rowid.
202934** RBU_PK_IPK: Table has an explicit IPK column.
202935** RBU_PK_EXTERNAL: Table has an external PK index.
202936** RBU_PK_WITHOUT_ROWID: Table is WITHOUT ROWID.
202937** RBU_PK_VTAB: Table is a virtual table.
202938**
202939** Argument *piPk is also of type (int*), and also points to an output
202940** parameter. Unless the table has an external primary key index
202941** (i.e. unless *peType is set to 3), then *piPk is set to zero. Or,
202942** if the table does have an external primary key index, then *piPk
202943** is set to the root page number of the primary key index before
202944** returning.
202945**
202946** ALGORITHM:
202947**
202948** if( no entry exists in sqlite_schema ){
202949** return RBU_PK_NOTABLE
202950** }else if( sql for the entry starts with "CREATE VIRTUAL" ){
202951** return RBU_PK_VTAB
202952** }else if( "PRAGMA index_list()" for the table contains a "pk" index ){
202953** if( the index that is the pk exists in sqlite_schema ){
202954** *piPK = rootpage of that index.
202955** return RBU_PK_EXTERNAL
202956** }else{
202957** return RBU_PK_WITHOUT_ROWID
202958** }
202959** }else if( "PRAGMA table_info()" lists one or more "pk" columns ){
202960** return RBU_PK_IPK
202961** }else{
202962** return RBU_PK_NONE
202963** }
202964*/
202965static void rbuTableType(
202966 sqlite3rbu *p,
202967 const char *zTab,
202968 int *peType,
202969 int *piTnum,
202970 int *piPk
202971){
202972 /*
202973 ** 0) SELECT count(*) FROM sqlite_schema where name=%Q AND IsVirtual(%Q)
202974 ** 1) PRAGMA index_list = ?
202975 ** 2) SELECT count(*) FROM sqlite_schema where name=%Q
202976 ** 3) PRAGMA table_info = ?
202977 */
202978 sqlite3_stmt *aStmt[4] = {0, 0, 0, 0};
202979
202980 *peType = RBU_PK_NOTABLE;
202981 *piPk = 0;
202982
202983 assert( p->rc==SQLITE_OK );
202984 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[0], &p->zErrmsg,
202985 sqlite3_mprintf(
202986 "SELECT "
202987 " (sql COLLATE nocase BETWEEN 'CREATE VIRTUAL' AND 'CREATE VIRTUAM'),"
202988 " rootpage"
202989 " FROM sqlite_schema"
202990 " WHERE name=%Q", zTab
202991 ));
202992 if( p->rc!=SQLITE_OK || sqlite3_step(aStmt[0])!=SQLITE_ROW ){
202993 /* Either an error, or no such table. */
202994 goto rbuTableType_end;
202995 }
202996 if( sqlite3_column_int(aStmt[0], 0) ){
202997 *peType = RBU_PK_VTAB; /* virtual table */
202998 goto rbuTableType_end;
202999 }
203000 *piTnum = sqlite3_column_int(aStmt[0], 1);
203001
203002 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[1], &p->zErrmsg,
203003 sqlite3_mprintf("PRAGMA index_list=%Q",zTab)
203004 );
203005 if( p->rc ) goto rbuTableType_end;
203006 while( sqlite3_step(aStmt[1])==SQLITE_ROW ){
203007 const u8 *zOrig = sqlite3_column_text(aStmt[1], 3);
203008 const u8 *zIdx = sqlite3_column_text(aStmt[1], 1);
203009 if( zOrig && zIdx && zOrig[0]=='p' ){
203010 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[2], &p->zErrmsg,
203011 sqlite3_mprintf(
203012 "SELECT rootpage FROM sqlite_schema WHERE name = %Q", zIdx
203013 ));
203014 if( p->rc==SQLITE_OK ){
203015 if( sqlite3_step(aStmt[2])==SQLITE_ROW ){
203016 *piPk = sqlite3_column_int(aStmt[2], 0);
203017 *peType = RBU_PK_EXTERNAL;
203018 }else{
203019 *peType = RBU_PK_WITHOUT_ROWID;
203020 }
203021 }
203022 goto rbuTableType_end;
203023 }
203024 }
203025
203026 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[3], &p->zErrmsg,
203027 sqlite3_mprintf("PRAGMA table_info=%Q",zTab)
203028 );
203029 if( p->rc==SQLITE_OK ){
203030 while( sqlite3_step(aStmt[3])==SQLITE_ROW ){
203031 if( sqlite3_column_int(aStmt[3],5)>0 ){
203032 *peType = RBU_PK_IPK; /* explicit IPK column */
203033 goto rbuTableType_end;
203034 }
203035 }
203036 *peType = RBU_PK_NONE;
203037 }
203038
203039rbuTableType_end: {
203040 unsigned int i;
203041 for(i=0; i<sizeof(aStmt)/sizeof(aStmt[0]); i++){
203042 rbuFinalize(p, aStmt[i]);
203043 }
203044 }
203045}
203046
203047/*
203048** This is a helper function for rbuObjIterCacheTableInfo(). It populates
203049** the pIter->abIndexed[] array.
203050*/
203051static void rbuObjIterCacheIndexedCols(sqlite3rbu *p, RbuObjIter *pIter){
203052 sqlite3_stmt *pList = 0;
203053 int bIndex = 0;
203054
203055 if( p->rc==SQLITE_OK ){
203056 memcpy(pIter->abIndexed, pIter->abTblPk, sizeof(u8)*pIter->nTblCol);
203057 p->rc = prepareFreeAndCollectError(p->dbMain, &pList, &p->zErrmsg,
203058 sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
203059 );
203060 }
203061
203062 pIter->nIndex = 0;
203063 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pList) ){
203064 const char *zIdx = (const char*)sqlite3_column_text(pList, 1);
203065 int bPartial = sqlite3_column_int(pList, 4);
203066 sqlite3_stmt *pXInfo = 0;
203067 if( zIdx==0 ) break;
203068 if( bPartial ){
203069 memset(pIter->abIndexed, 0x01, sizeof(u8)*pIter->nTblCol);
203070 }
203071 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
203072 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
203073 );
203074 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
203075 int iCid = sqlite3_column_int(pXInfo, 1);
203076 if( iCid>=0 ) pIter->abIndexed[iCid] = 1;
203077 if( iCid==-2 ){
203078 memset(pIter->abIndexed, 0x01, sizeof(u8)*pIter->nTblCol);
203079 }
203080 }
203081 rbuFinalize(p, pXInfo);
203082 bIndex = 1;
203083 pIter->nIndex++;
203084 }
203085
203086 if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
203087 /* "PRAGMA index_list" includes the main PK b-tree */
203088 pIter->nIndex--;
203089 }
203090
203091 rbuFinalize(p, pList);
203092 if( bIndex==0 ) pIter->abIndexed = 0;
203093}
203094
203095
203096/*
203097** If they are not already populated, populate the pIter->azTblCol[],
203098** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to
203099** the table (not index) that the iterator currently points to.
203100**
203101** Return SQLITE_OK if successful, or an SQLite error code otherwise. If
203102** an error does occur, an error code and error message are also left in
203103** the RBU handle.
203104*/
203105static int rbuObjIterCacheTableInfo(sqlite3rbu *p, RbuObjIter *pIter){
203106 if( pIter->azTblCol==0 ){
203107 sqlite3_stmt *pStmt = 0;
203108 int nCol = 0;
203109 int i; /* for() loop iterator variable */
203110 int bRbuRowid = 0; /* If input table has column "rbu_rowid" */
203111 int iOrder = 0;
203112 int iTnum = 0;
203113
203114 /* Figure out the type of table this step will deal with. */
203115 assert( pIter->eType==0 );
203116 rbuTableType(p, pIter->zTbl, &pIter->eType, &iTnum, &pIter->iPkTnum);
203117 if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_NOTABLE ){
203118 p->rc = SQLITE_ERROR;
203119 p->zErrmsg = sqlite3_mprintf("no such table: %s", pIter->zTbl);
203120 }
203121 if( p->rc ) return p->rc;
203122 if( pIter->zIdx==0 ) pIter->iTnum = iTnum;
203123
203124 assert( pIter->eType==RBU_PK_NONE || pIter->eType==RBU_PK_IPK
203125 || pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_WITHOUT_ROWID
203126 || pIter->eType==RBU_PK_VTAB
203127 );
203128
203129 /* Populate the azTblCol[] and nTblCol variables based on the columns
203130 ** of the input table. Ignore any input table columns that begin with
203131 ** "rbu_". */
203132 p->rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
203133 sqlite3_mprintf("SELECT * FROM '%q'", pIter->zDataTbl)
203134 );
203135 if( p->rc==SQLITE_OK ){
203136 nCol = sqlite3_column_count(pStmt);
203137 rbuAllocateIterArrays(p, pIter, nCol);
203138 }
203139 for(i=0; p->rc==SQLITE_OK && i<nCol; i++){
203140 const char *zName = (const char*)sqlite3_column_name(pStmt, i);
203141 if( sqlite3_strnicmp("rbu_", zName, 4) ){
203142 char *zCopy = rbuStrndup(zName, &p->rc);
203143 pIter->aiSrcOrder[pIter->nTblCol] = pIter->nTblCol;
203144 pIter->azTblCol[pIter->nTblCol++] = zCopy;
203145 }
203146 else if( 0==sqlite3_stricmp("rbu_rowid", zName) ){
203147 bRbuRowid = 1;
203148 }
203149 }
203150 sqlite3_finalize(pStmt);
203151 pStmt = 0;
203152
203153 if( p->rc==SQLITE_OK
203154 && rbuIsVacuum(p)==0
203155 && bRbuRowid!=(pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
203156 ){
203157 p->rc = SQLITE_ERROR;
203158 p->zErrmsg = sqlite3_mprintf(
203159 "table %q %s rbu_rowid column", pIter->zDataTbl,
203160 (bRbuRowid ? "may not have" : "requires")
203161 );
203162 }
203163
203164 /* Check that all non-HIDDEN columns in the destination table are also
203165 ** present in the input table. Populate the abTblPk[], azTblType[] and
203166 ** aiTblOrder[] arrays at the same time. */
203167 if( p->rc==SQLITE_OK ){
203168 p->rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
203169 sqlite3_mprintf("PRAGMA table_info(%Q)", pIter->zTbl)
203170 );
203171 }
203172 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
203173 const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
203174 if( zName==0 ) break; /* An OOM - finalize() below returns S_NOMEM */
203175 for(i=iOrder; i<pIter->nTblCol; i++){
203176 if( 0==strcmp(zName, pIter->azTblCol[i]) ) break;
203177 }
203178 if( i==pIter->nTblCol ){
203179 p->rc = SQLITE_ERROR;
203180 p->zErrmsg = sqlite3_mprintf("column missing from %q: %s",
203181 pIter->zDataTbl, zName
203182 );
203183 }else{
203184 int iPk = sqlite3_column_int(pStmt, 5);
203185 int bNotNull = sqlite3_column_int(pStmt, 3);
203186 const char *zType = (const char*)sqlite3_column_text(pStmt, 2);
203187
203188 if( i!=iOrder ){
203189 SWAP(int, pIter->aiSrcOrder[i], pIter->aiSrcOrder[iOrder]);
203190 SWAP(char*, pIter->azTblCol[i], pIter->azTblCol[iOrder]);
203191 }
203192
203193 pIter->azTblType[iOrder] = rbuStrndup(zType, &p->rc);
203194 assert( iPk>=0 );
203195 pIter->abTblPk[iOrder] = (u8)iPk;
203196 pIter->abNotNull[iOrder] = (u8)bNotNull || (iPk!=0);
203197 iOrder++;
203198 }
203199 }
203200
203201 rbuFinalize(p, pStmt);
203202 rbuObjIterCacheIndexedCols(p, pIter);
203203 assert( pIter->eType!=RBU_PK_VTAB || pIter->abIndexed==0 );
203204 assert( pIter->eType!=RBU_PK_VTAB || pIter->nIndex==0 );
203205 }
203206
203207 return p->rc;
203208}
203209
203210/*
203211** This function constructs and returns a pointer to a nul-terminated
203212** string containing some SQL clause or list based on one or more of the
203213** column names currently stored in the pIter->azTblCol[] array.
203214*/
203215static char *rbuObjIterGetCollist(
203216 sqlite3rbu *p, /* RBU object */
203217 RbuObjIter *pIter /* Object iterator for column names */
203218){
203219 char *zList = 0;
203220 const char *zSep = "";
203221 int i;
203222 for(i=0; i<pIter->nTblCol; i++){
203223 const char *z = pIter->azTblCol[i];
203224 zList = rbuMPrintf(p, "%z%s\"%w\"", zList, zSep, z);
203225 zSep = ", ";
203226 }
203227 return zList;
203228}
203229
203230/*
203231** Return a comma separated list of the quoted PRIMARY KEY column names,
203232** in order, for the current table. Before each column name, add the text
203233** zPre. After each column name, add the zPost text. Use zSeparator as
203234** the separator text (usually ", ").
203235*/
203236static char *rbuObjIterGetPkList(
203237 sqlite3rbu *p, /* RBU object */
203238 RbuObjIter *pIter, /* Object iterator for column names */
203239 const char *zPre, /* Before each quoted column name */
203240 const char *zSeparator, /* Separator to use between columns */
203241 const char *zPost /* After each quoted column name */
203242){
203243 int iPk = 1;
203244 char *zRet = 0;
203245 const char *zSep = "";
203246 while( 1 ){
203247 int i;
203248 for(i=0; i<pIter->nTblCol; i++){
203249 if( (int)pIter->abTblPk[i]==iPk ){
203250 const char *zCol = pIter->azTblCol[i];
203251 zRet = rbuMPrintf(p, "%z%s%s\"%w\"%s", zRet, zSep, zPre, zCol, zPost);
203252 zSep = zSeparator;
203253 break;
203254 }
203255 }
203256 if( i==pIter->nTblCol ) break;
203257 iPk++;
203258 }
203259 return zRet;
203260}
203261
203262/*
203263** This function is called as part of restarting an RBU vacuum within
203264** stage 1 of the process (while the *-oal file is being built) while
203265** updating a table (not an index). The table may be a rowid table or
203266** a WITHOUT ROWID table. It queries the target database to find the
203267** largest key that has already been written to the target table and
203268** constructs a WHERE clause that can be used to extract the remaining
203269** rows from the source table. For a rowid table, the WHERE clause
203270** is of the form:
203271**
203272** "WHERE _rowid_ > ?"
203273**
203274** and for WITHOUT ROWID tables:
203275**
203276** "WHERE (key1, key2) > (?, ?)"
203277**
203278** Instead of "?" placeholders, the actual WHERE clauses created by
203279** this function contain literal SQL values.
203280*/
203281static char *rbuVacuumTableStart(
203282 sqlite3rbu *p, /* RBU handle */
203283 RbuObjIter *pIter, /* RBU iterator object */
203284 int bRowid, /* True for a rowid table */
203285 const char *zWrite /* Target table name prefix */
203286){
203287 sqlite3_stmt *pMax = 0;
203288 char *zRet = 0;
203289 if( bRowid ){
203290 p->rc = prepareFreeAndCollectError(p->dbMain, &pMax, &p->zErrmsg,
203291 sqlite3_mprintf(
203292 "SELECT max(_rowid_) FROM \"%s%w\"", zWrite, pIter->zTbl
203293 )
203294 );
203295 if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
203296 sqlite3_int64 iMax = sqlite3_column_int64(pMax, 0);
203297 zRet = rbuMPrintf(p, " WHERE _rowid_ > %lld ", iMax);
203298 }
203299 rbuFinalize(p, pMax);
203300 }else{
203301 char *zOrder = rbuObjIterGetPkList(p, pIter, "", ", ", " DESC");
203302 char *zSelect = rbuObjIterGetPkList(p, pIter, "quote(", "||','||", ")");
203303 char *zList = rbuObjIterGetPkList(p, pIter, "", ", ", "");
203304
203305 if( p->rc==SQLITE_OK ){
203306 p->rc = prepareFreeAndCollectError(p->dbMain, &pMax, &p->zErrmsg,
203307 sqlite3_mprintf(
203308 "SELECT %s FROM \"%s%w\" ORDER BY %s LIMIT 1",
203309 zSelect, zWrite, pIter->zTbl, zOrder
203310 )
203311 );
203312 if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
203313 const char *zVal = (const char*)sqlite3_column_text(pMax, 0);
203314 zRet = rbuMPrintf(p, " WHERE (%s) > (%s) ", zList, zVal);
203315 }
203316 rbuFinalize(p, pMax);
203317 }
203318
203319 sqlite3_free(zOrder);
203320 sqlite3_free(zSelect);
203321 sqlite3_free(zList);
203322 }
203323 return zRet;
203324}
203325
203326/*
203327** This function is called as part of restating an RBU vacuum when the
203328** current operation is writing content to an index. If possible, it
203329** queries the target index b-tree for the largest key already written to
203330** it, then composes and returns an expression that can be used in a WHERE
203331** clause to select the remaining required rows from the source table.
203332** It is only possible to return such an expression if:
203333**
203334** * The index contains no DESC columns, and
203335** * The last key written to the index before the operation was
203336** suspended does not contain any NULL values.
203337**
203338** The expression is of the form:
203339**
203340** (index-field1, index-field2, ...) > (?, ?, ...)
203341**
203342** except that the "?" placeholders are replaced with literal values.
203343**
203344** If the expression cannot be created, NULL is returned. In this case,
203345** the caller has to use an OFFSET clause to extract only the required
203346** rows from the sourct table, just as it does for an RBU update operation.
203347*/
203348char *rbuVacuumIndexStart(
203349 sqlite3rbu *p, /* RBU handle */
203350 RbuObjIter *pIter /* RBU iterator object */
203351){
203352 char *zOrder = 0;
203353 char *zLhs = 0;
203354 char *zSelect = 0;
203355 char *zVector = 0;
203356 char *zRet = 0;
203357 int bFailed = 0;
203358 const char *zSep = "";
203359 int iCol = 0;
203360 sqlite3_stmt *pXInfo = 0;
203361
203362 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
203363 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx)
203364 );
203365 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
203366 int iCid = sqlite3_column_int(pXInfo, 1);
203367 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
203368 const char *zCol;
203369 if( sqlite3_column_int(pXInfo, 3) ){
203370 bFailed = 1;
203371 break;
203372 }
203373
203374 if( iCid<0 ){
203375 if( pIter->eType==RBU_PK_IPK ){
203376 int i;
203377 for(i=0; pIter->abTblPk[i]==0; i++);
203378 assert( i<pIter->nTblCol );
203379 zCol = pIter->azTblCol[i];
203380 }else{
203381 zCol = "_rowid_";
203382 }
203383 }else{
203384 zCol = pIter->azTblCol[iCid];
203385 }
203386
203387 zLhs = rbuMPrintf(p, "%z%s \"%w\" COLLATE %Q",
203388 zLhs, zSep, zCol, zCollate
203389 );
203390 zOrder = rbuMPrintf(p, "%z%s \"rbu_imp_%d%w\" COLLATE %Q DESC",
203391 zOrder, zSep, iCol, zCol, zCollate
203392 );
203393 zSelect = rbuMPrintf(p, "%z%s quote(\"rbu_imp_%d%w\")",
203394 zSelect, zSep, iCol, zCol
203395 );
203396 zSep = ", ";
203397 iCol++;
203398 }
203399 rbuFinalize(p, pXInfo);
203400 if( bFailed ) goto index_start_out;
203401
203402 if( p->rc==SQLITE_OK ){
203403 sqlite3_stmt *pSel = 0;
203404
203405 p->rc = prepareFreeAndCollectError(p->dbMain, &pSel, &p->zErrmsg,
203406 sqlite3_mprintf("SELECT %s FROM \"rbu_imp_%w\" ORDER BY %s LIMIT 1",
203407 zSelect, pIter->zTbl, zOrder
203408 )
203409 );
203410 if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSel) ){
203411 zSep = "";
203412 for(iCol=0; iCol<pIter->nCol; iCol++){
203413 const char *zQuoted = (const char*)sqlite3_column_text(pSel, iCol);
203414 if( zQuoted==0 ){
203415 p->rc = SQLITE_NOMEM;
203416 }else if( zQuoted[0]=='N' ){
203417 bFailed = 1;
203418 break;
203419 }
203420 zVector = rbuMPrintf(p, "%z%s%s", zVector, zSep, zQuoted);
203421 zSep = ", ";
203422 }
203423
203424 if( !bFailed ){
203425 zRet = rbuMPrintf(p, "(%s) > (%s)", zLhs, zVector);
203426 }
203427 }
203428 rbuFinalize(p, pSel);
203429 }
203430
203431 index_start_out:
203432 sqlite3_free(zOrder);
203433 sqlite3_free(zSelect);
203434 sqlite3_free(zVector);
203435 sqlite3_free(zLhs);
203436 return zRet;
203437}
203438
203439/*
203440** This function is used to create a SELECT list (the list of SQL
203441** expressions that follows a SELECT keyword) for a SELECT statement
203442** used to read from an data_xxx or rbu_tmp_xxx table while updating the
203443** index object currently indicated by the iterator object passed as the
203444** second argument. A "PRAGMA index_xinfo = <idxname>" statement is used
203445** to obtain the required information.
203446**
203447** If the index is of the following form:
203448**
203449** CREATE INDEX i1 ON t1(c, b COLLATE nocase);
203450**
203451** and "t1" is a table with an explicit INTEGER PRIMARY KEY column
203452** "ipk", the returned string is:
203453**
203454** "`c` COLLATE 'BINARY', `b` COLLATE 'NOCASE', `ipk` COLLATE 'BINARY'"
203455**
203456** As well as the returned string, three other malloc'd strings are
203457** returned via output parameters. As follows:
203458**
203459** pzImposterCols: ...
203460** pzImposterPk: ...
203461** pzWhere: ...
203462*/
203463static char *rbuObjIterGetIndexCols(
203464 sqlite3rbu *p, /* RBU object */
203465 RbuObjIter *pIter, /* Object iterator for column names */
203466 char **pzImposterCols, /* OUT: Columns for imposter table */
203467 char **pzImposterPk, /* OUT: Imposter PK clause */
203468 char **pzWhere, /* OUT: WHERE clause */
203469 int *pnBind /* OUT: Trbul number of columns */
203470){
203471 int rc = p->rc; /* Error code */
203472 int rc2; /* sqlite3_finalize() return code */
203473 char *zRet = 0; /* String to return */
203474 char *zImpCols = 0; /* String to return via *pzImposterCols */
203475 char *zImpPK = 0; /* String to return via *pzImposterPK */
203476 char *zWhere = 0; /* String to return via *pzWhere */
203477 int nBind = 0; /* Value to return via *pnBind */
203478 const char *zCom = ""; /* Set to ", " later on */
203479 const char *zAnd = ""; /* Set to " AND " later on */
203480 sqlite3_stmt *pXInfo = 0; /* PRAGMA index_xinfo = ? */
203481
203482 if( rc==SQLITE_OK ){
203483 assert( p->zErrmsg==0 );
203484 rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
203485 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx)
203486 );
203487 }
203488
203489 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
203490 int iCid = sqlite3_column_int(pXInfo, 1);
203491 int bDesc = sqlite3_column_int(pXInfo, 3);
203492 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
203493 const char *zCol = 0;
203494 const char *zType;
203495
203496 if( iCid==-2 ){
203497 int iSeq = sqlite3_column_int(pXInfo, 0);
203498 zRet = sqlite3_mprintf("%z%s(%.*s) COLLATE %Q", zRet, zCom,
203499 pIter->aIdxCol[iSeq].nSpan, pIter->aIdxCol[iSeq].zSpan, zCollate
203500 );
203501 zType = "";
203502 }else {
203503 if( iCid<0 ){
203504 /* An integer primary key. If the table has an explicit IPK, use
203505 ** its name. Otherwise, use "rbu_rowid". */
203506 if( pIter->eType==RBU_PK_IPK ){
203507 int i;
203508 for(i=0; pIter->abTblPk[i]==0; i++);
203509 assert( i<pIter->nTblCol );
203510 zCol = pIter->azTblCol[i];
203511 }else if( rbuIsVacuum(p) ){
203512 zCol = "_rowid_";
203513 }else{
203514 zCol = "rbu_rowid";
203515 }
203516 zType = "INTEGER";
203517 }else{
203518 zCol = pIter->azTblCol[iCid];
203519 zType = pIter->azTblType[iCid];
203520 }
203521 zRet = sqlite3_mprintf("%z%s\"%w\" COLLATE %Q", zRet, zCom,zCol,zCollate);
203522 }
203523
203524 if( pIter->bUnique==0 || sqlite3_column_int(pXInfo, 5) ){
203525 const char *zOrder = (bDesc ? " DESC" : "");
203526 zImpPK = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\"%s",
203527 zImpPK, zCom, nBind, zCol, zOrder
203528 );
203529 }
203530 zImpCols = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\" %s COLLATE %Q",
203531 zImpCols, zCom, nBind, zCol, zType, zCollate
203532 );
203533 zWhere = sqlite3_mprintf(
203534 "%z%s\"rbu_imp_%d%w\" IS ?", zWhere, zAnd, nBind, zCol
203535 );
203536 if( zRet==0 || zImpPK==0 || zImpCols==0 || zWhere==0 ) rc = SQLITE_NOMEM;
203537 zCom = ", ";
203538 zAnd = " AND ";
203539 nBind++;
203540 }
203541
203542 rc2 = sqlite3_finalize(pXInfo);
203543 if( rc==SQLITE_OK ) rc = rc2;
203544
203545 if( rc!=SQLITE_OK ){
203546 sqlite3_free(zRet);
203547 sqlite3_free(zImpCols);
203548 sqlite3_free(zImpPK);
203549 sqlite3_free(zWhere);
203550 zRet = 0;
203551 zImpCols = 0;
203552 zImpPK = 0;
203553 zWhere = 0;
203554 p->rc = rc;
203555 }
203556
203557 *pzImposterCols = zImpCols;
203558 *pzImposterPk = zImpPK;
203559 *pzWhere = zWhere;
203560 *pnBind = nBind;
203561 return zRet;
203562}
203563
203564/*
203565** Assuming the current table columns are "a", "b" and "c", and the zObj
203566** paramter is passed "old", return a string of the form:
203567**
203568** "old.a, old.b, old.b"
203569**
203570** With the column names escaped.
203571**
203572** For tables with implicit rowids - RBU_PK_EXTERNAL and RBU_PK_NONE, append
203573** the text ", old._rowid_" to the returned value.
203574*/
203575static char *rbuObjIterGetOldlist(
203576 sqlite3rbu *p,
203577 RbuObjIter *pIter,
203578 const char *zObj
203579){
203580 char *zList = 0;
203581 if( p->rc==SQLITE_OK && pIter->abIndexed ){
203582 const char *zS = "";
203583 int i;
203584 for(i=0; i<pIter->nTblCol; i++){
203585 if( pIter->abIndexed[i] ){
203586 const char *zCol = pIter->azTblCol[i];
203587 zList = sqlite3_mprintf("%z%s%s.\"%w\"", zList, zS, zObj, zCol);
203588 }else{
203589 zList = sqlite3_mprintf("%z%sNULL", zList, zS);
203590 }
203591 zS = ", ";
203592 if( zList==0 ){
203593 p->rc = SQLITE_NOMEM;
203594 break;
203595 }
203596 }
203597
203598 /* For a table with implicit rowids, append "old._rowid_" to the list. */
203599 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
203600 zList = rbuMPrintf(p, "%z, %s._rowid_", zList, zObj);
203601 }
203602 }
203603 return zList;
203604}
203605
203606/*
203607** Return an expression that can be used in a WHERE clause to match the
203608** primary key of the current table. For example, if the table is:
203609**
203610** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, c));
203611**
203612** Return the string:
203613**
203614** "b = ?1 AND c = ?2"
203615*/
203616static char *rbuObjIterGetWhere(
203617 sqlite3rbu *p,
203618 RbuObjIter *pIter
203619){
203620 char *zList = 0;
203621 if( pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE ){
203622 zList = rbuMPrintf(p, "_rowid_ = ?%d", pIter->nTblCol+1);
203623 }else if( pIter->eType==RBU_PK_EXTERNAL ){
203624 const char *zSep = "";
203625 int i;
203626 for(i=0; i<pIter->nTblCol; i++){
203627 if( pIter->abTblPk[i] ){
203628 zList = rbuMPrintf(p, "%z%sc%d=?%d", zList, zSep, i, i+1);
203629 zSep = " AND ";
203630 }
203631 }
203632 zList = rbuMPrintf(p,
203633 "_rowid_ = (SELECT id FROM rbu_imposter2 WHERE %z)", zList
203634 );
203635
203636 }else{
203637 const char *zSep = "";
203638 int i;
203639 for(i=0; i<pIter->nTblCol; i++){
203640 if( pIter->abTblPk[i] ){
203641 const char *zCol = pIter->azTblCol[i];
203642 zList = rbuMPrintf(p, "%z%s\"%w\"=?%d", zList, zSep, zCol, i+1);
203643 zSep = " AND ";
203644 }
203645 }
203646 }
203647 return zList;
203648}
203649
203650/*
203651** The SELECT statement iterating through the keys for the current object
203652** (p->objiter.pSelect) currently points to a valid row. However, there
203653** is something wrong with the rbu_control value in the rbu_control value
203654** stored in the (p->nCol+1)'th column. Set the error code and error message
203655** of the RBU handle to something reflecting this.
203656*/
203657static void rbuBadControlError(sqlite3rbu *p){
203658 p->rc = SQLITE_ERROR;
203659 p->zErrmsg = sqlite3_mprintf("invalid rbu_control value");
203660}
203661
203662
203663/*
203664** Return a nul-terminated string containing the comma separated list of
203665** assignments that should be included following the "SET" keyword of
203666** an UPDATE statement used to update the table object that the iterator
203667** passed as the second argument currently points to if the rbu_control
203668** column of the data_xxx table entry is set to zMask.
203669**
203670** The memory for the returned string is obtained from sqlite3_malloc().
203671** It is the responsibility of the caller to eventually free it using
203672** sqlite3_free().
203673**
203674** If an OOM error is encountered when allocating space for the new
203675** string, an error code is left in the rbu handle passed as the first
203676** argument and NULL is returned. Or, if an error has already occurred
203677** when this function is called, NULL is returned immediately, without
203678** attempting the allocation or modifying the stored error code.
203679*/
203680static char *rbuObjIterGetSetlist(
203681 sqlite3rbu *p,
203682 RbuObjIter *pIter,
203683 const char *zMask
203684){
203685 char *zList = 0;
203686 if( p->rc==SQLITE_OK ){
203687 int i;
203688
203689 if( (int)strlen(zMask)!=pIter->nTblCol ){
203690 rbuBadControlError(p);
203691 }else{
203692 const char *zSep = "";
203693 for(i=0; i<pIter->nTblCol; i++){
203694 char c = zMask[pIter->aiSrcOrder[i]];
203695 if( c=='x' ){
203696 zList = rbuMPrintf(p, "%z%s\"%w\"=?%d",
203697 zList, zSep, pIter->azTblCol[i], i+1
203698 );
203699 zSep = ", ";
203700 }
203701 else if( c=='d' ){
203702 zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_delta(\"%w\", ?%d)",
203703 zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
203704 );
203705 zSep = ", ";
203706 }
203707 else if( c=='f' ){
203708 zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_fossil_delta(\"%w\", ?%d)",
203709 zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
203710 );
203711 zSep = ", ";
203712 }
203713 }
203714 }
203715 }
203716 return zList;
203717}
203718
203719/*
203720** Return a nul-terminated string consisting of nByte comma separated
203721** "?" expressions. For example, if nByte is 3, return a pointer to
203722** a buffer containing the string "?,?,?".
203723**
203724** The memory for the returned string is obtained from sqlite3_malloc().
203725** It is the responsibility of the caller to eventually free it using
203726** sqlite3_free().
203727**
203728** If an OOM error is encountered when allocating space for the new
203729** string, an error code is left in the rbu handle passed as the first
203730** argument and NULL is returned. Or, if an error has already occurred
203731** when this function is called, NULL is returned immediately, without
203732** attempting the allocation or modifying the stored error code.
203733*/
203734static char *rbuObjIterGetBindlist(sqlite3rbu *p, int nBind){
203735 char *zRet = 0;
203736 sqlite3_int64 nByte = 2*(sqlite3_int64)nBind + 1;
203737
203738 zRet = (char*)rbuMalloc(p, nByte);
203739 if( zRet ){
203740 int i;
203741 for(i=0; i<nBind; i++){
203742 zRet[i*2] = '?';
203743 zRet[i*2+1] = (i+1==nBind) ? '\0' : ',';
203744 }
203745 }
203746 return zRet;
203747}
203748
203749/*
203750** The iterator currently points to a table (not index) of type
203751** RBU_PK_WITHOUT_ROWID. This function creates the PRIMARY KEY
203752** declaration for the corresponding imposter table. For example,
203753** if the iterator points to a table created as:
203754**
203755** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, a DESC)) WITHOUT ROWID
203756**
203757** this function returns:
203758**
203759** PRIMARY KEY("b", "a" DESC)
203760*/
203761static char *rbuWithoutRowidPK(sqlite3rbu *p, RbuObjIter *pIter){
203762 char *z = 0;
203763 assert( pIter->zIdx==0 );
203764 if( p->rc==SQLITE_OK ){
203765 const char *zSep = "PRIMARY KEY(";
203766 sqlite3_stmt *pXList = 0; /* PRAGMA index_list = (pIter->zTbl) */
203767 sqlite3_stmt *pXInfo = 0; /* PRAGMA index_xinfo = <pk-index> */
203768
203769 p->rc = prepareFreeAndCollectError(p->dbMain, &pXList, &p->zErrmsg,
203770 sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
203771 );
203772 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXList) ){
203773 const char *zOrig = (const char*)sqlite3_column_text(pXList,3);
203774 if( zOrig && strcmp(zOrig, "pk")==0 ){
203775 const char *zIdx = (const char*)sqlite3_column_text(pXList,1);
203776 if( zIdx ){
203777 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
203778 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
203779 );
203780 }
203781 break;
203782 }
203783 }
203784 rbuFinalize(p, pXList);
203785
203786 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
203787 if( sqlite3_column_int(pXInfo, 5) ){
203788 /* int iCid = sqlite3_column_int(pXInfo, 0); */
203789 const char *zCol = (const char*)sqlite3_column_text(pXInfo, 2);
203790 const char *zDesc = sqlite3_column_int(pXInfo, 3) ? " DESC" : "";
203791 z = rbuMPrintf(p, "%z%s\"%w\"%s", z, zSep, zCol, zDesc);
203792 zSep = ", ";
203793 }
203794 }
203795 z = rbuMPrintf(p, "%z)", z);
203796 rbuFinalize(p, pXInfo);
203797 }
203798 return z;
203799}
203800
203801/*
203802** This function creates the second imposter table used when writing to
203803** a table b-tree where the table has an external primary key. If the
203804** iterator passed as the second argument does not currently point to
203805** a table (not index) with an external primary key, this function is a
203806** no-op.
203807**
203808** Assuming the iterator does point to a table with an external PK, this
203809** function creates a WITHOUT ROWID imposter table named "rbu_imposter2"
203810** used to access that PK index. For example, if the target table is
203811** declared as follows:
203812**
203813** CREATE TABLE t1(a, b TEXT, c REAL, PRIMARY KEY(b, c));
203814**
203815** then the imposter table schema is:
203816**
203817** CREATE TABLE rbu_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID;
203818**
203819*/
203820static void rbuCreateImposterTable2(sqlite3rbu *p, RbuObjIter *pIter){
203821 if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_EXTERNAL ){
203822 int tnum = pIter->iPkTnum; /* Root page of PK index */
203823 sqlite3_stmt *pQuery = 0; /* SELECT name ... WHERE rootpage = $tnum */
203824 const char *zIdx = 0; /* Name of PK index */
203825 sqlite3_stmt *pXInfo = 0; /* PRAGMA main.index_xinfo = $zIdx */
203826 const char *zComma = "";
203827 char *zCols = 0; /* Used to build up list of table cols */
203828 char *zPk = 0; /* Used to build up table PK declaration */
203829
203830 /* Figure out the name of the primary key index for the current table.
203831 ** This is needed for the argument to "PRAGMA index_xinfo". Set
203832 ** zIdx to point to a nul-terminated string containing this name. */
203833 p->rc = prepareAndCollectError(p->dbMain, &pQuery, &p->zErrmsg,
203834 "SELECT name FROM sqlite_schema WHERE rootpage = ?"
203835 );
203836 if( p->rc==SQLITE_OK ){
203837 sqlite3_bind_int(pQuery, 1, tnum);
203838 if( SQLITE_ROW==sqlite3_step(pQuery) ){
203839 zIdx = (const char*)sqlite3_column_text(pQuery, 0);
203840 }
203841 }
203842 if( zIdx ){
203843 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
203844 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
203845 );
203846 }
203847 rbuFinalize(p, pQuery);
203848
203849 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
203850 int bKey = sqlite3_column_int(pXInfo, 5);
203851 if( bKey ){
203852 int iCid = sqlite3_column_int(pXInfo, 1);
203853 int bDesc = sqlite3_column_int(pXInfo, 3);
203854 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
203855 zCols = rbuMPrintf(p, "%z%sc%d %s COLLATE %Q", zCols, zComma,
203856 iCid, pIter->azTblType[iCid], zCollate
203857 );
203858 zPk = rbuMPrintf(p, "%z%sc%d%s", zPk, zComma, iCid, bDesc?" DESC":"");
203859 zComma = ", ";
203860 }
203861 }
203862 zCols = rbuMPrintf(p, "%z, id INTEGER", zCols);
203863 rbuFinalize(p, pXInfo);
203864
203865 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
203866 rbuMPrintfExec(p, p->dbMain,
203867 "CREATE TABLE rbu_imposter2(%z, PRIMARY KEY(%z)) WITHOUT ROWID",
203868 zCols, zPk
203869 );
203870 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
203871 }
203872}
203873
203874/*
203875** If an error has already occurred when this function is called, it
203876** immediately returns zero (without doing any work). Or, if an error
203877** occurs during the execution of this function, it sets the error code
203878** in the sqlite3rbu object indicated by the first argument and returns
203879** zero.
203880**
203881** The iterator passed as the second argument is guaranteed to point to
203882** a table (not an index) when this function is called. This function
203883** attempts to create any imposter table required to write to the main
203884** table b-tree of the table before returning. Non-zero is returned if
203885** an imposter table are created, or zero otherwise.
203886**
203887** An imposter table is required in all cases except RBU_PK_VTAB. Only
203888** virtual tables are written to directly. The imposter table has the
203889** same schema as the actual target table (less any UNIQUE constraints).
203890** More precisely, the "same schema" means the same columns, types,
203891** collation sequences. For tables that do not have an external PRIMARY
203892** KEY, it also means the same PRIMARY KEY declaration.
203893*/
203894static void rbuCreateImposterTable(sqlite3rbu *p, RbuObjIter *pIter){
203895 if( p->rc==SQLITE_OK && pIter->eType!=RBU_PK_VTAB ){
203896 int tnum = pIter->iTnum;
203897 const char *zComma = "";
203898 char *zSql = 0;
203899 int iCol;
203900 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
203901
203902 for(iCol=0; p->rc==SQLITE_OK && iCol<pIter->nTblCol; iCol++){
203903 const char *zPk = "";
203904 const char *zCol = pIter->azTblCol[iCol];
203905 const char *zColl = 0;
203906
203907 p->rc = sqlite3_table_column_metadata(
203908 p->dbMain, "main", pIter->zTbl, zCol, 0, &zColl, 0, 0, 0
203909 );
203910
203911 if( pIter->eType==RBU_PK_IPK && pIter->abTblPk[iCol] ){
203912 /* If the target table column is an "INTEGER PRIMARY KEY", add
203913 ** "PRIMARY KEY" to the imposter table column declaration. */
203914 zPk = "PRIMARY KEY ";
203915 }
203916 zSql = rbuMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %Q%s",
203917 zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl,
203918 (pIter->abNotNull[iCol] ? " NOT NULL" : "")
203919 );
203920 zComma = ", ";
203921 }
203922
203923 if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
203924 char *zPk = rbuWithoutRowidPK(p, pIter);
203925 if( zPk ){
203926 zSql = rbuMPrintf(p, "%z, %z", zSql, zPk);
203927 }
203928 }
203929
203930 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
203931 rbuMPrintfExec(p, p->dbMain, "CREATE TABLE \"rbu_imp_%w\"(%z)%s",
203932 pIter->zTbl, zSql,
203933 (pIter->eType==RBU_PK_WITHOUT_ROWID ? " WITHOUT ROWID" : "")
203934 );
203935 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
203936 }
203937}
203938
203939/*
203940** Prepare a statement used to insert rows into the "rbu_tmp_xxx" table.
203941** Specifically a statement of the form:
203942**
203943** INSERT INTO rbu_tmp_xxx VALUES(?, ?, ? ...);
203944**
203945** The number of bound variables is equal to the number of columns in
203946** the target table, plus one (for the rbu_control column), plus one more
203947** (for the rbu_rowid column) if the target table is an implicit IPK or
203948** virtual table.
203949*/
203950static void rbuObjIterPrepareTmpInsert(
203951 sqlite3rbu *p,
203952 RbuObjIter *pIter,
203953 const char *zCollist,
203954 const char *zRbuRowid
203955){
203956 int bRbuRowid = (pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE);
203957 char *zBind = rbuObjIterGetBindlist(p, pIter->nTblCol + 1 + bRbuRowid);
203958 if( zBind ){
203959 assert( pIter->pTmpInsert==0 );
203960 p->rc = prepareFreeAndCollectError(
203961 p->dbRbu, &pIter->pTmpInsert, &p->zErrmsg, sqlite3_mprintf(
203962 "INSERT INTO %s.'rbu_tmp_%q'(rbu_control,%s%s) VALUES(%z)",
203963 p->zStateDb, pIter->zDataTbl, zCollist, zRbuRowid, zBind
203964 ));
203965 }
203966}
203967
203968static void rbuTmpInsertFunc(
203969 sqlite3_context *pCtx,
203970 int nVal,
203971 sqlite3_value **apVal
203972){
203973 sqlite3rbu *p = sqlite3_user_data(pCtx);
203974 int rc = SQLITE_OK;
203975 int i;
203976
203977 assert( sqlite3_value_int(apVal[0])!=0
203978 || p->objiter.eType==RBU_PK_EXTERNAL
203979 || p->objiter.eType==RBU_PK_NONE
203980 );
203981 if( sqlite3_value_int(apVal[0])!=0 ){
203982 p->nPhaseOneStep += p->objiter.nIndex;
203983 }
203984
203985 for(i=0; rc==SQLITE_OK && i<nVal; i++){
203986 rc = sqlite3_bind_value(p->objiter.pTmpInsert, i+1, apVal[i]);
203987 }
203988 if( rc==SQLITE_OK ){
203989 sqlite3_step(p->objiter.pTmpInsert);
203990 rc = sqlite3_reset(p->objiter.pTmpInsert);
203991 }
203992
203993 if( rc!=SQLITE_OK ){
203994 sqlite3_result_error_code(pCtx, rc);
203995 }
203996}
203997
203998static char *rbuObjIterGetIndexWhere(sqlite3rbu *p, RbuObjIter *pIter){
203999 sqlite3_stmt *pStmt = 0;
204000 int rc = p->rc;
204001 char *zRet = 0;
204002
204003 assert( pIter->zIdxSql==0 && pIter->nIdxCol==0 && pIter->aIdxCol==0 );
204004
204005 if( rc==SQLITE_OK ){
204006 rc = prepareAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
204007 "SELECT trim(sql) FROM sqlite_schema WHERE type='index' AND name=?"
204008 );
204009 }
204010 if( rc==SQLITE_OK ){
204011 int rc2;
204012 rc = sqlite3_bind_text(pStmt, 1, pIter->zIdx, -1, SQLITE_STATIC);
204013 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
204014 char *zSql = (char*)sqlite3_column_text(pStmt, 0);
204015 if( zSql ){
204016 pIter->zIdxSql = zSql = rbuStrndup(zSql, &rc);
204017 }
204018 if( zSql ){
204019 int nParen = 0; /* Number of open parenthesis */
204020 int i;
204021 int iIdxCol = 0;
204022 int nIdxAlloc = 0;
204023 for(i=0; zSql[i]; i++){
204024 char c = zSql[i];
204025
204026 /* If necessary, grow the pIter->aIdxCol[] array */
204027 if( iIdxCol==nIdxAlloc ){
204028 RbuSpan *aIdxCol = (RbuSpan*)sqlite3_realloc(
204029 pIter->aIdxCol, (nIdxAlloc+16)*sizeof(RbuSpan)
204030 );
204031 if( aIdxCol==0 ){
204032 rc = SQLITE_NOMEM;
204033 break;
204034 }
204035 pIter->aIdxCol = aIdxCol;
204036 nIdxAlloc += 16;
204037 }
204038
204039 if( c=='(' ){
204040 if( nParen==0 ){
204041 assert( iIdxCol==0 );
204042 pIter->aIdxCol[0].zSpan = &zSql[i+1];
204043 }
204044 nParen++;
204045 }
204046 else if( c==')' ){
204047 nParen--;
204048 if( nParen==0 ){
204049 int nSpan = &zSql[i] - pIter->aIdxCol[iIdxCol].zSpan;
204050 pIter->aIdxCol[iIdxCol++].nSpan = nSpan;
204051 i++;
204052 break;
204053 }
204054 }else if( c==',' && nParen==1 ){
204055 int nSpan = &zSql[i] - pIter->aIdxCol[iIdxCol].zSpan;
204056 pIter->aIdxCol[iIdxCol++].nSpan = nSpan;
204057 pIter->aIdxCol[iIdxCol].zSpan = &zSql[i+1];
204058 }else if( c=='"' || c=='\'' || c=='`' ){
204059 for(i++; 1; i++){
204060 if( zSql[i]==c ){
204061 if( zSql[i+1]!=c ) break;
204062 i++;
204063 }
204064 }
204065 }else if( c=='[' ){
204066 for(i++; 1; i++){
204067 if( zSql[i]==']' ) break;
204068 }
204069 }else if( c=='-' && zSql[i+1]=='-' ){
204070 for(i=i+2; zSql[i] && zSql[i]!='\n'; i++);
204071 if( zSql[i]=='\0' ) break;
204072 }else if( c=='/' && zSql[i+1]=='*' ){
204073 for(i=i+2; zSql[i] && (zSql[i]!='*' || zSql[i+1]!='/'); i++);
204074 if( zSql[i]=='\0' ) break;
204075 i++;
204076 }
204077 }
204078 if( zSql[i] ){
204079 zRet = rbuStrndup(&zSql[i], &rc);
204080 }
204081 pIter->nIdxCol = iIdxCol;
204082 }
204083 }
204084
204085 rc2 = sqlite3_finalize(pStmt);
204086 if( rc==SQLITE_OK ) rc = rc2;
204087 }
204088
204089 p->rc = rc;
204090 return zRet;
204091}
204092
204093/*
204094** Ensure that the SQLite statement handles required to update the
204095** target database object currently indicated by the iterator passed
204096** as the second argument are available.
204097*/
204098static int rbuObjIterPrepareAll(
204099 sqlite3rbu *p,
204100 RbuObjIter *pIter,
204101 int nOffset /* Add "LIMIT -1 OFFSET $nOffset" to SELECT */
204102){
204103 assert( pIter->bCleanup==0 );
204104 if( pIter->pSelect==0 && rbuObjIterCacheTableInfo(p, pIter)==SQLITE_OK ){
204105 const int tnum = pIter->iTnum;
204106 char *zCollist = 0; /* List of indexed columns */
204107 char **pz = &p->zErrmsg;
204108 const char *zIdx = pIter->zIdx;
204109 char *zLimit = 0;
204110
204111 if( nOffset ){
204112 zLimit = sqlite3_mprintf(" LIMIT -1 OFFSET %d", nOffset);
204113 if( !zLimit ) p->rc = SQLITE_NOMEM;
204114 }
204115
204116 if( zIdx ){
204117 const char *zTbl = pIter->zTbl;
204118 char *zImposterCols = 0; /* Columns for imposter table */
204119 char *zImposterPK = 0; /* Primary key declaration for imposter */
204120 char *zWhere = 0; /* WHERE clause on PK columns */
204121 char *zBind = 0;
204122 char *zPart = 0;
204123 int nBind = 0;
204124
204125 assert( pIter->eType!=RBU_PK_VTAB );
204126 zPart = rbuObjIterGetIndexWhere(p, pIter);
204127 zCollist = rbuObjIterGetIndexCols(
204128 p, pIter, &zImposterCols, &zImposterPK, &zWhere, &nBind
204129 );
204130 zBind = rbuObjIterGetBindlist(p, nBind);
204131
204132 /* Create the imposter table used to write to this index. */
204133 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
204134 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1,tnum);
204135 rbuMPrintfExec(p, p->dbMain,
204136 "CREATE TABLE \"rbu_imp_%w\"( %s, PRIMARY KEY( %s ) ) WITHOUT ROWID",
204137 zTbl, zImposterCols, zImposterPK
204138 );
204139 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
204140
204141 /* Create the statement to insert index entries */
204142 pIter->nCol = nBind;
204143 if( p->rc==SQLITE_OK ){
204144 p->rc = prepareFreeAndCollectError(
204145 p->dbMain, &pIter->pInsert, &p->zErrmsg,
204146 sqlite3_mprintf("INSERT INTO \"rbu_imp_%w\" VALUES(%s)", zTbl, zBind)
204147 );
204148 }
204149
204150 /* And to delete index entries */
204151 if( rbuIsVacuum(p)==0 && p->rc==SQLITE_OK ){
204152 p->rc = prepareFreeAndCollectError(
204153 p->dbMain, &pIter->pDelete, &p->zErrmsg,
204154 sqlite3_mprintf("DELETE FROM \"rbu_imp_%w\" WHERE %s", zTbl, zWhere)
204155 );
204156 }
204157
204158 /* Create the SELECT statement to read keys in sorted order */
204159 if( p->rc==SQLITE_OK ){
204160 char *zSql;
204161 if( rbuIsVacuum(p) ){
204162 char *zStart = 0;
204163 if( nOffset ){
204164 zStart = rbuVacuumIndexStart(p, pIter);
204165 if( zStart ){
204166 sqlite3_free(zLimit);
204167 zLimit = 0;
204168 }
204169 }
204170
204171 zSql = sqlite3_mprintf(
204172 "SELECT %s, 0 AS rbu_control FROM '%q' %s %s %s ORDER BY %s%s",
204173 zCollist,
204174 pIter->zDataTbl,
204175 zPart,
204176 (zStart ? (zPart ? "AND" : "WHERE") : ""), zStart,
204177 zCollist, zLimit
204178 );
204179 sqlite3_free(zStart);
204180 }else
204181
204182 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
204183 zSql = sqlite3_mprintf(
204184 "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' %s ORDER BY %s%s",
204185 zCollist, p->zStateDb, pIter->zDataTbl,
204186 zPart, zCollist, zLimit
204187 );
204188 }else{
204189 zSql = sqlite3_mprintf(
204190 "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' %s "
204191 "UNION ALL "
204192 "SELECT %s, rbu_control FROM '%q' "
204193 "%s %s typeof(rbu_control)='integer' AND rbu_control!=1 "
204194 "ORDER BY %s%s",
204195 zCollist, p->zStateDb, pIter->zDataTbl, zPart,
204196 zCollist, pIter->zDataTbl,
204197 zPart,
204198 (zPart ? "AND" : "WHERE"),
204199 zCollist, zLimit
204200 );
204201 }
204202 if( p->rc==SQLITE_OK ){
204203 p->rc = prepareFreeAndCollectError(p->dbRbu,&pIter->pSelect,pz,zSql);
204204 }else{
204205 sqlite3_free(zSql);
204206 }
204207 }
204208
204209 sqlite3_free(zImposterCols);
204210 sqlite3_free(zImposterPK);
204211 sqlite3_free(zWhere);
204212 sqlite3_free(zBind);
204213 sqlite3_free(zPart);
204214 }else{
204215 int bRbuRowid = (pIter->eType==RBU_PK_VTAB)
204216 ||(pIter->eType==RBU_PK_NONE)
204217 ||(pIter->eType==RBU_PK_EXTERNAL && rbuIsVacuum(p));
204218 const char *zTbl = pIter->zTbl; /* Table this step applies to */
204219 const char *zWrite; /* Imposter table name */
204220
204221 char *zBindings = rbuObjIterGetBindlist(p, pIter->nTblCol + bRbuRowid);
204222 char *zWhere = rbuObjIterGetWhere(p, pIter);
204223 char *zOldlist = rbuObjIterGetOldlist(p, pIter, "old");
204224 char *zNewlist = rbuObjIterGetOldlist(p, pIter, "new");
204225
204226 zCollist = rbuObjIterGetCollist(p, pIter);
204227 pIter->nCol = pIter->nTblCol;
204228
204229 /* Create the imposter table or tables (if required). */
204230 rbuCreateImposterTable(p, pIter);
204231 rbuCreateImposterTable2(p, pIter);
204232 zWrite = (pIter->eType==RBU_PK_VTAB ? "" : "rbu_imp_");
204233
204234 /* Create the INSERT statement to write to the target PK b-tree */
204235 if( p->rc==SQLITE_OK ){
204236 p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pInsert, pz,
204237 sqlite3_mprintf(
204238 "INSERT INTO \"%s%w\"(%s%s) VALUES(%s)",
204239 zWrite, zTbl, zCollist, (bRbuRowid ? ", _rowid_" : ""), zBindings
204240 )
204241 );
204242 }
204243
204244 /* Create the DELETE statement to write to the target PK b-tree.
204245 ** Because it only performs INSERT operations, this is not required for
204246 ** an rbu vacuum handle. */
204247 if( rbuIsVacuum(p)==0 && p->rc==SQLITE_OK ){
204248 p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pDelete, pz,
204249 sqlite3_mprintf(
204250 "DELETE FROM \"%s%w\" WHERE %s", zWrite, zTbl, zWhere
204251 )
204252 );
204253 }
204254
204255 if( rbuIsVacuum(p)==0 && pIter->abIndexed ){
204256 const char *zRbuRowid = "";
204257 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
204258 zRbuRowid = ", rbu_rowid";
204259 }
204260
204261 /* Create the rbu_tmp_xxx table and the triggers to populate it. */
204262 rbuMPrintfExec(p, p->dbRbu,
204263 "CREATE TABLE IF NOT EXISTS %s.'rbu_tmp_%q' AS "
204264 "SELECT *%s FROM '%q' WHERE 0;"
204265 , p->zStateDb, pIter->zDataTbl
204266 , (pIter->eType==RBU_PK_EXTERNAL ? ", 0 AS rbu_rowid" : "")
204267 , pIter->zDataTbl
204268 );
204269
204270 rbuMPrintfExec(p, p->dbMain,
204271 "CREATE TEMP TRIGGER rbu_delete_tr BEFORE DELETE ON \"%s%w\" "
204272 "BEGIN "
204273 " SELECT rbu_tmp_insert(3, %s);"
204274 "END;"
204275
204276 "CREATE TEMP TRIGGER rbu_update1_tr BEFORE UPDATE ON \"%s%w\" "
204277 "BEGIN "
204278 " SELECT rbu_tmp_insert(3, %s);"
204279 "END;"
204280
204281 "CREATE TEMP TRIGGER rbu_update2_tr AFTER UPDATE ON \"%s%w\" "
204282 "BEGIN "
204283 " SELECT rbu_tmp_insert(4, %s);"
204284 "END;",
204285 zWrite, zTbl, zOldlist,
204286 zWrite, zTbl, zOldlist,
204287 zWrite, zTbl, zNewlist
204288 );
204289
204290 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
204291 rbuMPrintfExec(p, p->dbMain,
204292 "CREATE TEMP TRIGGER rbu_insert_tr AFTER INSERT ON \"%s%w\" "
204293 "BEGIN "
204294 " SELECT rbu_tmp_insert(0, %s);"
204295 "END;",
204296 zWrite, zTbl, zNewlist
204297 );
204298 }
204299
204300 rbuObjIterPrepareTmpInsert(p, pIter, zCollist, zRbuRowid);
204301 }
204302
204303 /* Create the SELECT statement to read keys from data_xxx */
204304 if( p->rc==SQLITE_OK ){
204305 const char *zRbuRowid = "";
204306 char *zStart = 0;
204307 char *zOrder = 0;
204308 if( bRbuRowid ){
204309 zRbuRowid = rbuIsVacuum(p) ? ",_rowid_ " : ",rbu_rowid";
204310 }
204311
204312 if( rbuIsVacuum(p) ){
204313 if( nOffset ){
204314 zStart = rbuVacuumTableStart(p, pIter, bRbuRowid, zWrite);
204315 if( zStart ){
204316 sqlite3_free(zLimit);
204317 zLimit = 0;
204318 }
204319 }
204320 if( bRbuRowid ){
204321 zOrder = rbuMPrintf(p, "_rowid_");
204322 }else{
204323 zOrder = rbuObjIterGetPkList(p, pIter, "", ", ", "");
204324 }
204325 }
204326
204327 if( p->rc==SQLITE_OK ){
204328 p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz,
204329 sqlite3_mprintf(
204330 "SELECT %s,%s rbu_control%s FROM '%q'%s %s %s %s",
204331 zCollist,
204332 (rbuIsVacuum(p) ? "0 AS " : ""),
204333 zRbuRowid,
204334 pIter->zDataTbl, (zStart ? zStart : ""),
204335 (zOrder ? "ORDER BY" : ""), zOrder,
204336 zLimit
204337 )
204338 );
204339 }
204340 sqlite3_free(zStart);
204341 sqlite3_free(zOrder);
204342 }
204343
204344 sqlite3_free(zWhere);
204345 sqlite3_free(zOldlist);
204346 sqlite3_free(zNewlist);
204347 sqlite3_free(zBindings);
204348 }
204349 sqlite3_free(zCollist);
204350 sqlite3_free(zLimit);
204351 }
204352
204353 return p->rc;
204354}
204355
204356/*
204357** Set output variable *ppStmt to point to an UPDATE statement that may
204358** be used to update the imposter table for the main table b-tree of the
204359** table object that pIter currently points to, assuming that the
204360** rbu_control column of the data_xyz table contains zMask.
204361**
204362** If the zMask string does not specify any columns to update, then this
204363** is not an error. Output variable *ppStmt is set to NULL in this case.
204364*/
204365static int rbuGetUpdateStmt(
204366 sqlite3rbu *p, /* RBU handle */
204367 RbuObjIter *pIter, /* Object iterator */
204368 const char *zMask, /* rbu_control value ('x.x.') */
204369 sqlite3_stmt **ppStmt /* OUT: UPDATE statement handle */
204370){
204371 RbuUpdateStmt **pp;
204372 RbuUpdateStmt *pUp = 0;
204373 int nUp = 0;
204374
204375 /* In case an error occurs */
204376 *ppStmt = 0;
204377
204378 /* Search for an existing statement. If one is found, shift it to the front
204379 ** of the LRU queue and return immediately. Otherwise, leave nUp pointing
204380 ** to the number of statements currently in the cache and pUp to the
204381 ** last object in the list. */
204382 for(pp=&pIter->pRbuUpdate; *pp; pp=&((*pp)->pNext)){
204383 pUp = *pp;
204384 if( strcmp(pUp->zMask, zMask)==0 ){
204385 *pp = pUp->pNext;
204386 pUp->pNext = pIter->pRbuUpdate;
204387 pIter->pRbuUpdate = pUp;
204388 *ppStmt = pUp->pUpdate;
204389 return SQLITE_OK;
204390 }
204391 nUp++;
204392 }
204393 assert( pUp==0 || pUp->pNext==0 );
204394
204395 if( nUp>=SQLITE_RBU_UPDATE_CACHESIZE ){
204396 for(pp=&pIter->pRbuUpdate; *pp!=pUp; pp=&((*pp)->pNext));
204397 *pp = 0;
204398 sqlite3_finalize(pUp->pUpdate);
204399 pUp->pUpdate = 0;
204400 }else{
204401 pUp = (RbuUpdateStmt*)rbuMalloc(p, sizeof(RbuUpdateStmt)+pIter->nTblCol+1);
204402 }
204403
204404 if( pUp ){
204405 char *zWhere = rbuObjIterGetWhere(p, pIter);
204406 char *zSet = rbuObjIterGetSetlist(p, pIter, zMask);
204407 char *zUpdate = 0;
204408
204409 pUp->zMask = (char*)&pUp[1];
204410 memcpy(pUp->zMask, zMask, pIter->nTblCol);
204411 pUp->pNext = pIter->pRbuUpdate;
204412 pIter->pRbuUpdate = pUp;
204413
204414 if( zSet ){
204415 const char *zPrefix = "";
204416
204417 if( pIter->eType!=RBU_PK_VTAB ) zPrefix = "rbu_imp_";
204418 zUpdate = sqlite3_mprintf("UPDATE \"%s%w\" SET %s WHERE %s",
204419 zPrefix, pIter->zTbl, zSet, zWhere
204420 );
204421 p->rc = prepareFreeAndCollectError(
204422 p->dbMain, &pUp->pUpdate, &p->zErrmsg, zUpdate
204423 );
204424 *ppStmt = pUp->pUpdate;
204425 }
204426 sqlite3_free(zWhere);
204427 sqlite3_free(zSet);
204428 }
204429
204430 return p->rc;
204431}
204432
204433static sqlite3 *rbuOpenDbhandle(
204434 sqlite3rbu *p,
204435 const char *zName,
204436 int bUseVfs
204437){
204438 sqlite3 *db = 0;
204439 if( p->rc==SQLITE_OK ){
204440 const int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_URI;
204441 p->rc = sqlite3_open_v2(zName, &db, flags, bUseVfs ? p->zVfsName : 0);
204442 if( p->rc ){
204443 p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
204444 sqlite3_close(db);
204445 db = 0;
204446 }
204447 }
204448 return db;
204449}
204450
204451/*
204452** Free an RbuState object allocated by rbuLoadState().
204453*/
204454static void rbuFreeState(RbuState *p){
204455 if( p ){
204456 sqlite3_free(p->zTbl);
204457 sqlite3_free(p->zDataTbl);
204458 sqlite3_free(p->zIdx);
204459 sqlite3_free(p);
204460 }
204461}
204462
204463/*
204464** Allocate an RbuState object and load the contents of the rbu_state
204465** table into it. Return a pointer to the new object. It is the
204466** responsibility of the caller to eventually free the object using
204467** sqlite3_free().
204468**
204469** If an error occurs, leave an error code and message in the rbu handle
204470** and return NULL.
204471*/
204472static RbuState *rbuLoadState(sqlite3rbu *p){
204473 RbuState *pRet = 0;
204474 sqlite3_stmt *pStmt = 0;
204475 int rc;
204476 int rc2;
204477
204478 pRet = (RbuState*)rbuMalloc(p, sizeof(RbuState));
204479 if( pRet==0 ) return 0;
204480
204481 rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
204482 sqlite3_mprintf("SELECT k, v FROM %s.rbu_state", p->zStateDb)
204483 );
204484 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
204485 switch( sqlite3_column_int(pStmt, 0) ){
204486 case RBU_STATE_STAGE:
204487 pRet->eStage = sqlite3_column_int(pStmt, 1);
204488 if( pRet->eStage!=RBU_STAGE_OAL
204489 && pRet->eStage!=RBU_STAGE_MOVE
204490 && pRet->eStage!=RBU_STAGE_CKPT
204491 ){
204492 p->rc = SQLITE_CORRUPT;
204493 }
204494 break;
204495
204496 case RBU_STATE_TBL:
204497 pRet->zTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
204498 break;
204499
204500 case RBU_STATE_IDX:
204501 pRet->zIdx = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
204502 break;
204503
204504 case RBU_STATE_ROW:
204505 pRet->nRow = sqlite3_column_int(pStmt, 1);
204506 break;
204507
204508 case RBU_STATE_PROGRESS:
204509 pRet->nProgress = sqlite3_column_int64(pStmt, 1);
204510 break;
204511
204512 case RBU_STATE_CKPT:
204513 pRet->iWalCksum = sqlite3_column_int64(pStmt, 1);
204514 break;
204515
204516 case RBU_STATE_COOKIE:
204517 pRet->iCookie = (u32)sqlite3_column_int64(pStmt, 1);
204518 break;
204519
204520 case RBU_STATE_OALSZ:
204521 pRet->iOalSz = sqlite3_column_int64(pStmt, 1);
204522 break;
204523
204524 case RBU_STATE_PHASEONESTEP:
204525 pRet->nPhaseOneStep = sqlite3_column_int64(pStmt, 1);
204526 break;
204527
204528 case RBU_STATE_DATATBL:
204529 pRet->zDataTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
204530 break;
204531
204532 default:
204533 rc = SQLITE_CORRUPT;
204534 break;
204535 }
204536 }
204537 rc2 = sqlite3_finalize(pStmt);
204538 if( rc==SQLITE_OK ) rc = rc2;
204539
204540 p->rc = rc;
204541 return pRet;
204542}
204543
204544
204545/*
204546** Open the database handle and attach the RBU database as "rbu". If an
204547** error occurs, leave an error code and message in the RBU handle.
204548**
204549** If argument dbMain is not NULL, then it is a database handle already
204550** open on the target database. Use this handle instead of opening a new
204551** one.
204552*/
204553static void rbuOpenDatabase(sqlite3rbu *p, sqlite3 *dbMain, int *pbRetry){
204554 assert( p->rc || (p->dbMain==0 && p->dbRbu==0) );
204555 assert( p->rc || rbuIsVacuum(p) || p->zTarget!=0 );
204556 assert( dbMain==0 || rbuIsVacuum(p)==0 );
204557
204558 /* Open the RBU database */
204559 p->dbRbu = rbuOpenDbhandle(p, p->zRbu, 1);
204560 p->dbMain = dbMain;
204561
204562 if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
204563 sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
204564 if( p->zState==0 ){
204565 const char *zFile = sqlite3_db_filename(p->dbRbu, "main");
204566 p->zState = rbuMPrintf(p, "file://%s-vacuum?modeof=%s", zFile, zFile);
204567 }
204568 }
204569
204570 /* If using separate RBU and state databases, attach the state database to
204571 ** the RBU db handle now. */
204572 if( p->zState ){
204573 rbuMPrintfExec(p, p->dbRbu, "ATTACH %Q AS stat", p->zState);
204574 memcpy(p->zStateDb, "stat", 4);
204575 }else{
204576 memcpy(p->zStateDb, "main", 4);
204577 }
204578
204579#if 0
204580 if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
204581 p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, 0);
204582 }
204583#endif
204584
204585 /* If it has not already been created, create the rbu_state table */
204586 rbuMPrintfExec(p, p->dbRbu, RBU_CREATE_STATE, p->zStateDb);
204587
204588#if 0
204589 if( rbuIsVacuum(p) ){
204590 if( p->rc==SQLITE_OK ){
204591 int rc2;
204592 int bOk = 0;
204593 sqlite3_stmt *pCnt = 0;
204594 p->rc = prepareAndCollectError(p->dbRbu, &pCnt, &p->zErrmsg,
204595 "SELECT count(*) FROM stat.sqlite_schema"
204596 );
204597 if( p->rc==SQLITE_OK
204598 && sqlite3_step(pCnt)==SQLITE_ROW
204599 && 1==sqlite3_column_int(pCnt, 0)
204600 ){
204601 bOk = 1;
204602 }
204603 rc2 = sqlite3_finalize(pCnt);
204604 if( p->rc==SQLITE_OK ) p->rc = rc2;
204605
204606 if( p->rc==SQLITE_OK && bOk==0 ){
204607 p->rc = SQLITE_ERROR;
204608 p->zErrmsg = sqlite3_mprintf("invalid state database");
204609 }
204610
204611 if( p->rc==SQLITE_OK ){
204612 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0);
204613 }
204614 }
204615 }
204616#endif
204617
204618 if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
204619 int bOpen = 0;
204620 int rc;
204621 p->nRbu = 0;
204622 p->pRbuFd = 0;
204623 rc = sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
204624 if( rc!=SQLITE_NOTFOUND ) p->rc = rc;
204625 if( p->eStage>=RBU_STAGE_MOVE ){
204626 bOpen = 1;
204627 }else{
204628 RbuState *pState = rbuLoadState(p);
204629 if( pState ){
204630 bOpen = (pState->eStage>=RBU_STAGE_MOVE);
204631 rbuFreeState(pState);
204632 }
204633 }
204634 if( bOpen ) p->dbMain = rbuOpenDbhandle(p, p->zRbu, p->nRbu<=1);
204635 }
204636
204637 p->eStage = 0;
204638 if( p->rc==SQLITE_OK && p->dbMain==0 ){
204639 if( !rbuIsVacuum(p) ){
204640 p->dbMain = rbuOpenDbhandle(p, p->zTarget, 1);
204641 }else if( p->pRbuFd->pWalFd ){
204642 if( pbRetry ){
204643 p->pRbuFd->bNolock = 0;
204644 sqlite3_close(p->dbRbu);
204645 sqlite3_close(p->dbMain);
204646 p->dbMain = 0;
204647 p->dbRbu = 0;
204648 *pbRetry = 1;
204649 return;
204650 }
204651 p->rc = SQLITE_ERROR;
204652 p->zErrmsg = sqlite3_mprintf("cannot vacuum wal mode database");
204653 }else{
204654 char *zTarget;
204655 char *zExtra = 0;
204656 if( strlen(p->zRbu)>=5 && 0==memcmp("file:", p->zRbu, 5) ){
204657 zExtra = &p->zRbu[5];
204658 while( *zExtra ){
204659 if( *zExtra++=='?' ) break;
204660 }
204661 if( *zExtra=='\0' ) zExtra = 0;
204662 }
204663
204664 zTarget = sqlite3_mprintf("file:%s-vactmp?rbu_memory=1%s%s",
204665 sqlite3_db_filename(p->dbRbu, "main"),
204666 (zExtra==0 ? "" : "&"), (zExtra==0 ? "" : zExtra)
204667 );
204668
204669 if( zTarget==0 ){
204670 p->rc = SQLITE_NOMEM;
204671 return;
204672 }
204673 p->dbMain = rbuOpenDbhandle(p, zTarget, p->nRbu<=1);
204674 sqlite3_free(zTarget);
204675 }
204676 }
204677
204678 if( p->rc==SQLITE_OK ){
204679 p->rc = sqlite3_create_function(p->dbMain,
204680 "rbu_tmp_insert", -1, SQLITE_UTF8, (void*)p, rbuTmpInsertFunc, 0, 0
204681 );
204682 }
204683
204684 if( p->rc==SQLITE_OK ){
204685 p->rc = sqlite3_create_function(p->dbMain,
204686 "rbu_fossil_delta", 2, SQLITE_UTF8, 0, rbuFossilDeltaFunc, 0, 0
204687 );
204688 }
204689
204690 if( p->rc==SQLITE_OK ){
204691 p->rc = sqlite3_create_function(p->dbRbu,
204692 "rbu_target_name", -1, SQLITE_UTF8, (void*)p, rbuTargetNameFunc, 0, 0
204693 );
204694 }
204695
204696 if( p->rc==SQLITE_OK ){
204697 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
204698 }
204699 rbuMPrintfExec(p, p->dbMain, "SELECT * FROM sqlite_schema");
204700
204701 /* Mark the database file just opened as an RBU target database. If
204702 ** this call returns SQLITE_NOTFOUND, then the RBU vfs is not in use.
204703 ** This is an error. */
204704 if( p->rc==SQLITE_OK ){
204705 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
204706 }
204707
204708 if( p->rc==SQLITE_NOTFOUND ){
204709 p->rc = SQLITE_ERROR;
204710 p->zErrmsg = sqlite3_mprintf("rbu vfs not found");
204711 }
204712}
204713
204714/*
204715** This routine is a copy of the sqlite3FileSuffix3() routine from the core.
204716** It is a no-op unless SQLITE_ENABLE_8_3_NAMES is defined.
204717**
204718** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
204719** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
204720** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
204721** three characters, then shorten the suffix on z[] to be the last three
204722** characters of the original suffix.
204723**
204724** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
204725** do the suffix shortening regardless of URI parameter.
204726**
204727** Examples:
204728**
204729** test.db-journal => test.nal
204730** test.db-wal => test.wal
204731** test.db-shm => test.shm
204732** test.db-mj7f3319fa => test.9fa
204733*/
204734static void rbuFileSuffix3(const char *zBase, char *z){
204735#ifdef SQLITE_ENABLE_8_3_NAMES
204736#if SQLITE_ENABLE_8_3_NAMES<2
204737 if( sqlite3_uri_boolean(zBase, "8_3_names", 0) )
204738#endif
204739 {
204740 int i, sz;
204741 sz = (int)strlen(z)&0xffffff;
204742 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
204743 if( z[i]=='.' && sz>i+4 ) memmove(&z[i+1], &z[sz-3], 4);
204744 }
204745#endif
204746}
204747
204748/*
204749** Return the current wal-index header checksum for the target database
204750** as a 64-bit integer.
204751**
204752** The checksum is store in the first page of xShmMap memory as an 8-byte
204753** blob starting at byte offset 40.
204754*/
204755static i64 rbuShmChecksum(sqlite3rbu *p){
204756 i64 iRet = 0;
204757 if( p->rc==SQLITE_OK ){
204758 sqlite3_file *pDb = p->pTargetFd->pReal;
204759 u32 volatile *ptr;
204760 p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, (void volatile**)&ptr);
204761 if( p->rc==SQLITE_OK ){
204762 iRet = ((i64)ptr[10] << 32) + ptr[11];
204763 }
204764 }
204765 return iRet;
204766}
204767
204768/*
204769** This function is called as part of initializing or reinitializing an
204770** incremental checkpoint.
204771**
204772** It populates the sqlite3rbu.aFrame[] array with the set of
204773** (wal frame -> db page) copy operations required to checkpoint the
204774** current wal file, and obtains the set of shm locks required to safely
204775** perform the copy operations directly on the file-system.
204776**
204777** If argument pState is not NULL, then the incremental checkpoint is
204778** being resumed. In this case, if the checksum of the wal-index-header
204779** following recovery is not the same as the checksum saved in the RbuState
204780** object, then the rbu handle is set to DONE state. This occurs if some
204781** other client appends a transaction to the wal file in the middle of
204782** an incremental checkpoint.
204783*/
204784static void rbuSetupCheckpoint(sqlite3rbu *p, RbuState *pState){
204785
204786 /* If pState is NULL, then the wal file may not have been opened and
204787 ** recovered. Running a read-statement here to ensure that doing so
204788 ** does not interfere with the "capture" process below. */
204789 if( pState==0 ){
204790 p->eStage = 0;
204791 if( p->rc==SQLITE_OK ){
204792 p->rc = sqlite3_exec(p->dbMain, "SELECT * FROM sqlite_schema", 0, 0, 0);
204793 }
204794 }
204795
204796 /* Assuming no error has occurred, run a "restart" checkpoint with the
204797 ** sqlite3rbu.eStage variable set to CAPTURE. This turns on the following
204798 ** special behaviour in the rbu VFS:
204799 **
204800 ** * If the exclusive shm WRITER or READ0 lock cannot be obtained,
204801 ** the checkpoint fails with SQLITE_BUSY (normally SQLite would
204802 ** proceed with running a passive checkpoint instead of failing).
204803 **
204804 ** * Attempts to read from the *-wal file or write to the database file
204805 ** do not perform any IO. Instead, the frame/page combinations that
204806 ** would be read/written are recorded in the sqlite3rbu.aFrame[]
204807 ** array.
204808 **
204809 ** * Calls to xShmLock(UNLOCK) to release the exclusive shm WRITER,
204810 ** READ0 and CHECKPOINT locks taken as part of the checkpoint are
204811 ** no-ops. These locks will not be released until the connection
204812 ** is closed.
204813 **
204814 ** * Attempting to xSync() the database file causes an SQLITE_INTERNAL
204815 ** error.
204816 **
204817 ** As a result, unless an error (i.e. OOM or SQLITE_BUSY) occurs, the
204818 ** checkpoint below fails with SQLITE_INTERNAL, and leaves the aFrame[]
204819 ** array populated with a set of (frame -> page) mappings. Because the
204820 ** WRITER, CHECKPOINT and READ0 locks are still held, it is safe to copy
204821 ** data from the wal file into the database file according to the
204822 ** contents of aFrame[].
204823 */
204824 if( p->rc==SQLITE_OK ){
204825 int rc2;
204826 p->eStage = RBU_STAGE_CAPTURE;
204827 rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
204828 if( rc2!=SQLITE_INTERNAL ) p->rc = rc2;
204829 }
204830
204831 if( p->rc==SQLITE_OK && p->nFrame>0 ){
204832 p->eStage = RBU_STAGE_CKPT;
204833 p->nStep = (pState ? pState->nRow : 0);
204834 p->aBuf = rbuMalloc(p, p->pgsz);
204835 p->iWalCksum = rbuShmChecksum(p);
204836 }
204837
204838 if( p->rc==SQLITE_OK ){
204839 if( p->nFrame==0 || (pState && pState->iWalCksum!=p->iWalCksum) ){
204840 p->rc = SQLITE_DONE;
204841 p->eStage = RBU_STAGE_DONE;
204842 }else{
204843 int nSectorSize;
204844 sqlite3_file *pDb = p->pTargetFd->pReal;
204845 sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
204846 assert( p->nPagePerSector==0 );
204847 nSectorSize = pDb->pMethods->xSectorSize(pDb);
204848 if( nSectorSize>p->pgsz ){
204849 p->nPagePerSector = nSectorSize / p->pgsz;
204850 }else{
204851 p->nPagePerSector = 1;
204852 }
204853
204854 /* Call xSync() on the wal file. This causes SQLite to sync the
204855 ** directory in which the target database and the wal file reside, in
204856 ** case it has not been synced since the rename() call in
204857 ** rbuMoveOalFile(). */
204858 p->rc = pWal->pMethods->xSync(pWal, SQLITE_SYNC_NORMAL);
204859 }
204860 }
204861}
204862
204863/*
204864** Called when iAmt bytes are read from offset iOff of the wal file while
204865** the rbu object is in capture mode. Record the frame number of the frame
204866** being read in the aFrame[] array.
204867*/
204868static int rbuCaptureWalRead(sqlite3rbu *pRbu, i64 iOff, int iAmt){
204869 const u32 mReq = (1<<WAL_LOCK_WRITE)|(1<<WAL_LOCK_CKPT)|(1<<WAL_LOCK_READ0);
204870 u32 iFrame;
204871
204872 if( pRbu->mLock!=mReq ){
204873 pRbu->rc = SQLITE_BUSY;
204874 return SQLITE_INTERNAL;
204875 }
204876
204877 pRbu->pgsz = iAmt;
204878 if( pRbu->nFrame==pRbu->nFrameAlloc ){
204879 int nNew = (pRbu->nFrameAlloc ? pRbu->nFrameAlloc : 64) * 2;
204880 RbuFrame *aNew;
204881 aNew = (RbuFrame*)sqlite3_realloc64(pRbu->aFrame, nNew * sizeof(RbuFrame));
204882 if( aNew==0 ) return SQLITE_NOMEM;
204883 pRbu->aFrame = aNew;
204884 pRbu->nFrameAlloc = nNew;
204885 }
204886
204887 iFrame = (u32)((iOff-32) / (i64)(iAmt+24)) + 1;
204888 if( pRbu->iMaxFrame<iFrame ) pRbu->iMaxFrame = iFrame;
204889 pRbu->aFrame[pRbu->nFrame].iWalFrame = iFrame;
204890 pRbu->aFrame[pRbu->nFrame].iDbPage = 0;
204891 pRbu->nFrame++;
204892 return SQLITE_OK;
204893}
204894
204895/*
204896** Called when a page of data is written to offset iOff of the database
204897** file while the rbu handle is in capture mode. Record the page number
204898** of the page being written in the aFrame[] array.
204899*/
204900static int rbuCaptureDbWrite(sqlite3rbu *pRbu, i64 iOff){
204901 pRbu->aFrame[pRbu->nFrame-1].iDbPage = (u32)(iOff / pRbu->pgsz) + 1;
204902 return SQLITE_OK;
204903}
204904
204905/*
204906** This is called as part of an incremental checkpoint operation. Copy
204907** a single frame of data from the wal file into the database file, as
204908** indicated by the RbuFrame object.
204909*/
204910static void rbuCheckpointFrame(sqlite3rbu *p, RbuFrame *pFrame){
204911 sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
204912 sqlite3_file *pDb = p->pTargetFd->pReal;
204913 i64 iOff;
204914
204915 assert( p->rc==SQLITE_OK );
204916 iOff = (i64)(pFrame->iWalFrame-1) * (p->pgsz + 24) + 32 + 24;
204917 p->rc = pWal->pMethods->xRead(pWal, p->aBuf, p->pgsz, iOff);
204918 if( p->rc ) return;
204919
204920 iOff = (i64)(pFrame->iDbPage-1) * p->pgsz;
204921 p->rc = pDb->pMethods->xWrite(pDb, p->aBuf, p->pgsz, iOff);
204922}
204923
204924
204925/*
204926** Take an EXCLUSIVE lock on the database file. Return SQLITE_OK if
204927** successful, or an SQLite error code otherwise.
204928*/
204929static int rbuLockDatabase(sqlite3 *db){
204930 int rc = SQLITE_OK;
204931 sqlite3_file *fd = 0;
204932 sqlite3_file_control(db, "main", SQLITE_FCNTL_FILE_POINTER, &fd);
204933
204934 if( fd->pMethods ){
204935 rc = fd->pMethods->xLock(fd, SQLITE_LOCK_SHARED);
204936 if( rc==SQLITE_OK ){
204937 rc = fd->pMethods->xLock(fd, SQLITE_LOCK_EXCLUSIVE);
204938 }
204939 }
204940 return rc;
204941}
204942
204943/*
204944** Return true if the database handle passed as the only argument
204945** was opened with the rbu_exclusive_checkpoint=1 URI parameter
204946** specified. Or false otherwise.
204947*/
204948static int rbuExclusiveCheckpoint(sqlite3 *db){
204949 const char *zUri = sqlite3_db_filename(db, 0);
204950 return sqlite3_uri_boolean(zUri, RBU_EXCLUSIVE_CHECKPOINT, 0);
204951}
204952
204953#if defined(_WIN32_WCE)
204954static LPWSTR rbuWinUtf8ToUnicode(const char *zFilename){
204955 int nChar;
204956 LPWSTR zWideFilename;
204957
204958 nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
204959 if( nChar==0 ){
204960 return 0;
204961 }
204962 zWideFilename = sqlite3_malloc64( nChar*sizeof(zWideFilename[0]) );
204963 if( zWideFilename==0 ){
204964 return 0;
204965 }
204966 memset(zWideFilename, 0, nChar*sizeof(zWideFilename[0]));
204967 nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
204968 nChar);
204969 if( nChar==0 ){
204970 sqlite3_free(zWideFilename);
204971 zWideFilename = 0;
204972 }
204973 return zWideFilename;
204974}
204975#endif
204976
204977/*
204978** The RBU handle is currently in RBU_STAGE_OAL state, with a SHARED lock
204979** on the database file. This proc moves the *-oal file to the *-wal path,
204980** then reopens the database file (this time in vanilla, non-oal, WAL mode).
204981** If an error occurs, leave an error code and error message in the rbu
204982** handle.
204983*/
204984static void rbuMoveOalFile(sqlite3rbu *p){
204985 const char *zBase = sqlite3_db_filename(p->dbMain, "main");
204986 const char *zMove = zBase;
204987 char *zOal;
204988 char *zWal;
204989
204990 if( rbuIsVacuum(p) ){
204991 zMove = sqlite3_db_filename(p->dbRbu, "main");
204992 }
204993 zOal = sqlite3_mprintf("%s-oal", zMove);
204994 zWal = sqlite3_mprintf("%s-wal", zMove);
204995
204996 assert( p->eStage==RBU_STAGE_MOVE );
204997 assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
204998 if( zWal==0 || zOal==0 ){
204999 p->rc = SQLITE_NOMEM;
205000 }else{
205001 /* Move the *-oal file to *-wal. At this point connection p->db is
205002 ** holding a SHARED lock on the target database file (because it is
205003 ** in WAL mode). So no other connection may be writing the db.
205004 **
205005 ** In order to ensure that there are no database readers, an EXCLUSIVE
205006 ** lock is obtained here before the *-oal is moved to *-wal.
205007 */
205008 sqlite3 *dbMain = 0;
205009 rbuFileSuffix3(zBase, zWal);
205010 rbuFileSuffix3(zBase, zOal);
205011
205012 /* Re-open the databases. */
205013 rbuObjIterFinalize(&p->objiter);
205014 sqlite3_close(p->dbRbu);
205015 sqlite3_close(p->dbMain);
205016 p->dbMain = 0;
205017 p->dbRbu = 0;
205018
205019 dbMain = rbuOpenDbhandle(p, p->zTarget, 1);
205020 if( dbMain ){
205021 assert( p->rc==SQLITE_OK );
205022 p->rc = rbuLockDatabase(dbMain);
205023 }
205024
205025 if( p->rc==SQLITE_OK ){
205026#if defined(_WIN32_WCE)
205027 {
205028 LPWSTR zWideOal;
205029 LPWSTR zWideWal;
205030
205031 zWideOal = rbuWinUtf8ToUnicode(zOal);
205032 if( zWideOal ){
205033 zWideWal = rbuWinUtf8ToUnicode(zWal);
205034 if( zWideWal ){
205035 if( MoveFileW(zWideOal, zWideWal) ){
205036 p->rc = SQLITE_OK;
205037 }else{
205038 p->rc = SQLITE_IOERR;
205039 }
205040 sqlite3_free(zWideWal);
205041 }else{
205042 p->rc = SQLITE_IOERR_NOMEM;
205043 }
205044 sqlite3_free(zWideOal);
205045 }else{
205046 p->rc = SQLITE_IOERR_NOMEM;
205047 }
205048 }
205049#else
205050 p->rc = rename(zOal, zWal) ? SQLITE_IOERR : SQLITE_OK;
205051#endif
205052 }
205053
205054 if( p->rc!=SQLITE_OK
205055 || rbuIsVacuum(p)
205056 || rbuExclusiveCheckpoint(dbMain)==0
205057 ){
205058 sqlite3_close(dbMain);
205059 dbMain = 0;
205060 }
205061
205062 if( p->rc==SQLITE_OK ){
205063 rbuOpenDatabase(p, dbMain, 0);
205064 rbuSetupCheckpoint(p, 0);
205065 }
205066 }
205067
205068 sqlite3_free(zWal);
205069 sqlite3_free(zOal);
205070}
205071
205072/*
205073** The SELECT statement iterating through the keys for the current object
205074** (p->objiter.pSelect) currently points to a valid row. This function
205075** determines the type of operation requested by this row and returns
205076** one of the following values to indicate the result:
205077**
205078** * RBU_INSERT
205079** * RBU_DELETE
205080** * RBU_IDX_DELETE
205081** * RBU_UPDATE
205082**
205083** If RBU_UPDATE is returned, then output variable *pzMask is set to
205084** point to the text value indicating the columns to update.
205085**
205086** If the rbu_control field contains an invalid value, an error code and
205087** message are left in the RBU handle and zero returned.
205088*/
205089static int rbuStepType(sqlite3rbu *p, const char **pzMask){
205090 int iCol = p->objiter.nCol; /* Index of rbu_control column */
205091 int res = 0; /* Return value */
205092
205093 switch( sqlite3_column_type(p->objiter.pSelect, iCol) ){
205094 case SQLITE_INTEGER: {
205095 int iVal = sqlite3_column_int(p->objiter.pSelect, iCol);
205096 switch( iVal ){
205097 case 0: res = RBU_INSERT; break;
205098 case 1: res = RBU_DELETE; break;
205099 case 2: res = RBU_REPLACE; break;
205100 case 3: res = RBU_IDX_DELETE; break;
205101 case 4: res = RBU_IDX_INSERT; break;
205102 }
205103 break;
205104 }
205105
205106 case SQLITE_TEXT: {
205107 const unsigned char *z = sqlite3_column_text(p->objiter.pSelect, iCol);
205108 if( z==0 ){
205109 p->rc = SQLITE_NOMEM;
205110 }else{
205111 *pzMask = (const char*)z;
205112 }
205113 res = RBU_UPDATE;
205114
205115 break;
205116 }
205117
205118 default:
205119 break;
205120 }
205121
205122 if( res==0 ){
205123 rbuBadControlError(p);
205124 }
205125 return res;
205126}
205127
205128#ifdef SQLITE_DEBUG
205129/*
205130** Assert that column iCol of statement pStmt is named zName.
205131*/
205132static void assertColumnName(sqlite3_stmt *pStmt, int iCol, const char *zName){
205133 const char *zCol = sqlite3_column_name(pStmt, iCol);
205134 assert( 0==sqlite3_stricmp(zName, zCol) );
205135}
205136#else
205137# define assertColumnName(x,y,z)
205138#endif
205139
205140/*
205141** Argument eType must be one of RBU_INSERT, RBU_DELETE, RBU_IDX_INSERT or
205142** RBU_IDX_DELETE. This function performs the work of a single
205143** sqlite3rbu_step() call for the type of operation specified by eType.
205144*/
205145static void rbuStepOneOp(sqlite3rbu *p, int eType){
205146 RbuObjIter *pIter = &p->objiter;
205147 sqlite3_value *pVal;
205148 sqlite3_stmt *pWriter;
205149 int i;
205150
205151 assert( p->rc==SQLITE_OK );
205152 assert( eType!=RBU_DELETE || pIter->zIdx==0 );
205153 assert( eType==RBU_DELETE || eType==RBU_IDX_DELETE
205154 || eType==RBU_INSERT || eType==RBU_IDX_INSERT
205155 );
205156
205157 /* If this is a delete, decrement nPhaseOneStep by nIndex. If the DELETE
205158 ** statement below does actually delete a row, nPhaseOneStep will be
205159 ** incremented by the same amount when SQL function rbu_tmp_insert()
205160 ** is invoked by the trigger. */
205161 if( eType==RBU_DELETE ){
205162 p->nPhaseOneStep -= p->objiter.nIndex;
205163 }
205164
205165 if( eType==RBU_IDX_DELETE || eType==RBU_DELETE ){
205166 pWriter = pIter->pDelete;
205167 }else{
205168 pWriter = pIter->pInsert;
205169 }
205170
205171 for(i=0; i<pIter->nCol; i++){
205172 /* If this is an INSERT into a table b-tree and the table has an
205173 ** explicit INTEGER PRIMARY KEY, check that this is not an attempt
205174 ** to write a NULL into the IPK column. That is not permitted. */
205175 if( eType==RBU_INSERT
205176 && pIter->zIdx==0 && pIter->eType==RBU_PK_IPK && pIter->abTblPk[i]
205177 && sqlite3_column_type(pIter->pSelect, i)==SQLITE_NULL
205178 ){
205179 p->rc = SQLITE_MISMATCH;
205180 p->zErrmsg = sqlite3_mprintf("datatype mismatch");
205181 return;
205182 }
205183
205184 if( eType==RBU_DELETE && pIter->abTblPk[i]==0 ){
205185 continue;
205186 }
205187
205188 pVal = sqlite3_column_value(pIter->pSelect, i);
205189 p->rc = sqlite3_bind_value(pWriter, i+1, pVal);
205190 if( p->rc ) return;
205191 }
205192 if( pIter->zIdx==0 ){
205193 if( pIter->eType==RBU_PK_VTAB
205194 || pIter->eType==RBU_PK_NONE
205195 || (pIter->eType==RBU_PK_EXTERNAL && rbuIsVacuum(p))
205196 ){
205197 /* For a virtual table, or a table with no primary key, the
205198 ** SELECT statement is:
205199 **
205200 ** SELECT <cols>, rbu_control, rbu_rowid FROM ....
205201 **
205202 ** Hence column_value(pIter->nCol+1).
205203 */
205204 assertColumnName(pIter->pSelect, pIter->nCol+1,
205205 rbuIsVacuum(p) ? "rowid" : "rbu_rowid"
205206 );
205207 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
205208 p->rc = sqlite3_bind_value(pWriter, pIter->nCol+1, pVal);
205209 }
205210 }
205211 if( p->rc==SQLITE_OK ){
205212 sqlite3_step(pWriter);
205213 p->rc = resetAndCollectError(pWriter, &p->zErrmsg);
205214 }
205215}
205216
205217/*
205218** This function does the work for an sqlite3rbu_step() call.
205219**
205220** The object-iterator (p->objiter) currently points to a valid object,
205221** and the input cursor (p->objiter.pSelect) currently points to a valid
205222** input row. Perform whatever processing is required and return.
205223**
205224** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
205225** and message is left in the RBU handle and a copy of the error code
205226** returned.
205227*/
205228static int rbuStep(sqlite3rbu *p){
205229 RbuObjIter *pIter = &p->objiter;
205230 const char *zMask = 0;
205231 int eType = rbuStepType(p, &zMask);
205232
205233 if( eType ){
205234 assert( eType==RBU_INSERT || eType==RBU_DELETE
205235 || eType==RBU_REPLACE || eType==RBU_IDX_DELETE
205236 || eType==RBU_IDX_INSERT || eType==RBU_UPDATE
205237 );
205238 assert( eType!=RBU_UPDATE || pIter->zIdx==0 );
205239
205240 if( pIter->zIdx==0 && (eType==RBU_IDX_DELETE || eType==RBU_IDX_INSERT) ){
205241 rbuBadControlError(p);
205242 }
205243 else if( eType==RBU_REPLACE ){
205244 if( pIter->zIdx==0 ){
205245 p->nPhaseOneStep += p->objiter.nIndex;
205246 rbuStepOneOp(p, RBU_DELETE);
205247 }
205248 if( p->rc==SQLITE_OK ) rbuStepOneOp(p, RBU_INSERT);
205249 }
205250 else if( eType!=RBU_UPDATE ){
205251 rbuStepOneOp(p, eType);
205252 }
205253 else{
205254 sqlite3_value *pVal;
205255 sqlite3_stmt *pUpdate = 0;
205256 assert( eType==RBU_UPDATE );
205257 p->nPhaseOneStep -= p->objiter.nIndex;
205258 rbuGetUpdateStmt(p, pIter, zMask, &pUpdate);
205259 if( pUpdate ){
205260 int i;
205261 for(i=0; p->rc==SQLITE_OK && i<pIter->nCol; i++){
205262 char c = zMask[pIter->aiSrcOrder[i]];
205263 pVal = sqlite3_column_value(pIter->pSelect, i);
205264 if( pIter->abTblPk[i] || c!='.' ){
205265 p->rc = sqlite3_bind_value(pUpdate, i+1, pVal);
205266 }
205267 }
205268 if( p->rc==SQLITE_OK
205269 && (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
205270 ){
205271 /* Bind the rbu_rowid value to column _rowid_ */
205272 assertColumnName(pIter->pSelect, pIter->nCol+1, "rbu_rowid");
205273 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
205274 p->rc = sqlite3_bind_value(pUpdate, pIter->nCol+1, pVal);
205275 }
205276 if( p->rc==SQLITE_OK ){
205277 sqlite3_step(pUpdate);
205278 p->rc = resetAndCollectError(pUpdate, &p->zErrmsg);
205279 }
205280 }
205281 }
205282 }
205283 return p->rc;
205284}
205285
205286/*
205287** Increment the schema cookie of the main database opened by p->dbMain.
205288**
205289** Or, if this is an RBU vacuum, set the schema cookie of the main db
205290** opened by p->dbMain to one more than the schema cookie of the main
205291** db opened by p->dbRbu.
205292*/
205293static void rbuIncrSchemaCookie(sqlite3rbu *p){
205294 if( p->rc==SQLITE_OK ){
205295 sqlite3 *dbread = (rbuIsVacuum(p) ? p->dbRbu : p->dbMain);
205296 int iCookie = 1000000;
205297 sqlite3_stmt *pStmt;
205298
205299 p->rc = prepareAndCollectError(dbread, &pStmt, &p->zErrmsg,
205300 "PRAGMA schema_version"
205301 );
205302 if( p->rc==SQLITE_OK ){
205303 /* Coverage: it may be that this sqlite3_step() cannot fail. There
205304 ** is already a transaction open, so the prepared statement cannot
205305 ** throw an SQLITE_SCHEMA exception. The only database page the
205306 ** statement reads is page 1, which is guaranteed to be in the cache.
205307 ** And no memory allocations are required. */
205308 if( SQLITE_ROW==sqlite3_step(pStmt) ){
205309 iCookie = sqlite3_column_int(pStmt, 0);
205310 }
205311 rbuFinalize(p, pStmt);
205312 }
205313 if( p->rc==SQLITE_OK ){
205314 rbuMPrintfExec(p, p->dbMain, "PRAGMA schema_version = %d", iCookie+1);
205315 }
205316 }
205317}
205318
205319/*
205320** Update the contents of the rbu_state table within the rbu database. The
205321** value stored in the RBU_STATE_STAGE column is eStage. All other values
205322** are determined by inspecting the rbu handle passed as the first argument.
205323*/
205324static void rbuSaveState(sqlite3rbu *p, int eStage){
205325 if( p->rc==SQLITE_OK || p->rc==SQLITE_DONE ){
205326 sqlite3_stmt *pInsert = 0;
205327 rbu_file *pFd = (rbuIsVacuum(p) ? p->pRbuFd : p->pTargetFd);
205328 int rc;
205329
205330 assert( p->zErrmsg==0 );
205331 rc = prepareFreeAndCollectError(p->dbRbu, &pInsert, &p->zErrmsg,
205332 sqlite3_mprintf(
205333 "INSERT OR REPLACE INTO %s.rbu_state(k, v) VALUES "
205334 "(%d, %d), "
205335 "(%d, %Q), "
205336 "(%d, %Q), "
205337 "(%d, %d), "
205338 "(%d, %d), "
205339 "(%d, %lld), "
205340 "(%d, %lld), "
205341 "(%d, %lld), "
205342 "(%d, %lld), "
205343 "(%d, %Q) ",
205344 p->zStateDb,
205345 RBU_STATE_STAGE, eStage,
205346 RBU_STATE_TBL, p->objiter.zTbl,
205347 RBU_STATE_IDX, p->objiter.zIdx,
205348 RBU_STATE_ROW, p->nStep,
205349 RBU_STATE_PROGRESS, p->nProgress,
205350 RBU_STATE_CKPT, p->iWalCksum,
205351 RBU_STATE_COOKIE, (i64)pFd->iCookie,
205352 RBU_STATE_OALSZ, p->iOalSz,
205353 RBU_STATE_PHASEONESTEP, p->nPhaseOneStep,
205354 RBU_STATE_DATATBL, p->objiter.zDataTbl
205355 )
205356 );
205357 assert( pInsert==0 || rc==SQLITE_OK );
205358
205359 if( rc==SQLITE_OK ){
205360 sqlite3_step(pInsert);
205361 rc = sqlite3_finalize(pInsert);
205362 }
205363 if( rc!=SQLITE_OK ) p->rc = rc;
205364 }
205365}
205366
205367
205368/*
205369** The second argument passed to this function is the name of a PRAGMA
205370** setting - "page_size", "auto_vacuum", "user_version" or "application_id".
205371** This function executes the following on sqlite3rbu.dbRbu:
205372**
205373** "PRAGMA main.$zPragma"
205374**
205375** where $zPragma is the string passed as the second argument, then
205376** on sqlite3rbu.dbMain:
205377**
205378** "PRAGMA main.$zPragma = $val"
205379**
205380** where $val is the value returned by the first PRAGMA invocation.
205381**
205382** In short, it copies the value of the specified PRAGMA setting from
205383** dbRbu to dbMain.
205384*/
205385static void rbuCopyPragma(sqlite3rbu *p, const char *zPragma){
205386 if( p->rc==SQLITE_OK ){
205387 sqlite3_stmt *pPragma = 0;
205388 p->rc = prepareFreeAndCollectError(p->dbRbu, &pPragma, &p->zErrmsg,
205389 sqlite3_mprintf("PRAGMA main.%s", zPragma)
205390 );
205391 if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPragma) ){
205392 p->rc = rbuMPrintfExec(p, p->dbMain, "PRAGMA main.%s = %d",
205393 zPragma, sqlite3_column_int(pPragma, 0)
205394 );
205395 }
205396 rbuFinalize(p, pPragma);
205397 }
205398}
205399
205400/*
205401** The RBU handle passed as the only argument has just been opened and
205402** the state database is empty. If this RBU handle was opened for an
205403** RBU vacuum operation, create the schema in the target db.
205404*/
205405static void rbuCreateTargetSchema(sqlite3rbu *p){
205406 sqlite3_stmt *pSql = 0;
205407 sqlite3_stmt *pInsert = 0;
205408
205409 assert( rbuIsVacuum(p) );
205410 p->rc = sqlite3_exec(p->dbMain, "PRAGMA writable_schema=1", 0,0, &p->zErrmsg);
205411 if( p->rc==SQLITE_OK ){
205412 p->rc = prepareAndCollectError(p->dbRbu, &pSql, &p->zErrmsg,
205413 "SELECT sql FROM sqlite_schema WHERE sql!='' AND rootpage!=0"
205414 " AND name!='sqlite_sequence' "
205415 " ORDER BY type DESC"
205416 );
205417 }
205418
205419 while( p->rc==SQLITE_OK && sqlite3_step(pSql)==SQLITE_ROW ){
205420 const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
205421 p->rc = sqlite3_exec(p->dbMain, zSql, 0, 0, &p->zErrmsg);
205422 }
205423 rbuFinalize(p, pSql);
205424 if( p->rc!=SQLITE_OK ) return;
205425
205426 if( p->rc==SQLITE_OK ){
205427 p->rc = prepareAndCollectError(p->dbRbu, &pSql, &p->zErrmsg,
205428 "SELECT * FROM sqlite_schema WHERE rootpage=0 OR rootpage IS NULL"
205429 );
205430 }
205431
205432 if( p->rc==SQLITE_OK ){
205433 p->rc = prepareAndCollectError(p->dbMain, &pInsert, &p->zErrmsg,
205434 "INSERT INTO sqlite_schema VALUES(?,?,?,?,?)"
205435 );
205436 }
205437
205438 while( p->rc==SQLITE_OK && sqlite3_step(pSql)==SQLITE_ROW ){
205439 int i;
205440 for(i=0; i<5; i++){
205441 sqlite3_bind_value(pInsert, i+1, sqlite3_column_value(pSql, i));
205442 }
205443 sqlite3_step(pInsert);
205444 p->rc = sqlite3_reset(pInsert);
205445 }
205446 if( p->rc==SQLITE_OK ){
205447 p->rc = sqlite3_exec(p->dbMain, "PRAGMA writable_schema=0",0,0,&p->zErrmsg);
205448 }
205449
205450 rbuFinalize(p, pSql);
205451 rbuFinalize(p, pInsert);
205452}
205453
205454/*
205455** Step the RBU object.
205456*/
205457SQLITE_API int sqlite3rbu_step(sqlite3rbu *p){
205458 if( p ){
205459 switch( p->eStage ){
205460 case RBU_STAGE_OAL: {
205461 RbuObjIter *pIter = &p->objiter;
205462
205463 /* If this is an RBU vacuum operation and the state table was empty
205464 ** when this handle was opened, create the target database schema. */
205465 if( rbuIsVacuum(p) && p->nProgress==0 && p->rc==SQLITE_OK ){
205466 rbuCreateTargetSchema(p);
205467 rbuCopyPragma(p, "user_version");
205468 rbuCopyPragma(p, "application_id");
205469 }
205470
205471 while( p->rc==SQLITE_OK && pIter->zTbl ){
205472
205473 if( pIter->bCleanup ){
205474 /* Clean up the rbu_tmp_xxx table for the previous table. It
205475 ** cannot be dropped as there are currently active SQL statements.
205476 ** But the contents can be deleted. */
205477 if( rbuIsVacuum(p)==0 && pIter->abIndexed ){
205478 rbuMPrintfExec(p, p->dbRbu,
205479 "DELETE FROM %s.'rbu_tmp_%q'", p->zStateDb, pIter->zDataTbl
205480 );
205481 }
205482 }else{
205483 rbuObjIterPrepareAll(p, pIter, 0);
205484
205485 /* Advance to the next row to process. */
205486 if( p->rc==SQLITE_OK ){
205487 int rc = sqlite3_step(pIter->pSelect);
205488 if( rc==SQLITE_ROW ){
205489 p->nProgress++;
205490 p->nStep++;
205491 return rbuStep(p);
205492 }
205493 p->rc = sqlite3_reset(pIter->pSelect);
205494 p->nStep = 0;
205495 }
205496 }
205497
205498 rbuObjIterNext(p, pIter);
205499 }
205500
205501 if( p->rc==SQLITE_OK ){
205502 assert( pIter->zTbl==0 );
205503 rbuSaveState(p, RBU_STAGE_MOVE);
205504 rbuIncrSchemaCookie(p);
205505 if( p->rc==SQLITE_OK ){
205506 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
205507 }
205508 if( p->rc==SQLITE_OK ){
205509 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
205510 }
205511 p->eStage = RBU_STAGE_MOVE;
205512 }
205513 break;
205514 }
205515
205516 case RBU_STAGE_MOVE: {
205517 if( p->rc==SQLITE_OK ){
205518 rbuMoveOalFile(p);
205519 p->nProgress++;
205520 }
205521 break;
205522 }
205523
205524 case RBU_STAGE_CKPT: {
205525 if( p->rc==SQLITE_OK ){
205526 if( p->nStep>=p->nFrame ){
205527 sqlite3_file *pDb = p->pTargetFd->pReal;
205528
205529 /* Sync the db file */
205530 p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
205531
205532 /* Update nBackfill */
205533 if( p->rc==SQLITE_OK ){
205534 void volatile *ptr;
205535 p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, &ptr);
205536 if( p->rc==SQLITE_OK ){
205537 ((u32 volatile*)ptr)[24] = p->iMaxFrame;
205538 }
205539 }
205540
205541 if( p->rc==SQLITE_OK ){
205542 p->eStage = RBU_STAGE_DONE;
205543 p->rc = SQLITE_DONE;
205544 }
205545 }else{
205546 /* At one point the following block copied a single frame from the
205547 ** wal file to the database file. So that one call to sqlite3rbu_step()
205548 ** checkpointed a single frame.
205549 **
205550 ** However, if the sector-size is larger than the page-size, and the
205551 ** application calls sqlite3rbu_savestate() or close() immediately
205552 ** after this step, then rbu_step() again, then a power failure occurs,
205553 ** then the database page written here may be damaged. Work around
205554 ** this by checkpointing frames until the next page in the aFrame[]
205555 ** lies on a different disk sector to the current one. */
205556 u32 iSector;
205557 do{
205558 RbuFrame *pFrame = &p->aFrame[p->nStep];
205559 iSector = (pFrame->iDbPage-1) / p->nPagePerSector;
205560 rbuCheckpointFrame(p, pFrame);
205561 p->nStep++;
205562 }while( p->nStep<p->nFrame
205563 && iSector==((p->aFrame[p->nStep].iDbPage-1) / p->nPagePerSector)
205564 && p->rc==SQLITE_OK
205565 );
205566 }
205567 p->nProgress++;
205568 }
205569 break;
205570 }
205571
205572 default:
205573 break;
205574 }
205575 return p->rc;
205576 }else{
205577 return SQLITE_NOMEM;
205578 }
205579}
205580
205581/*
205582** Compare strings z1 and z2, returning 0 if they are identical, or non-zero
205583** otherwise. Either or both argument may be NULL. Two NULL values are
205584** considered equal, and NULL is considered distinct from all other values.
205585*/
205586static int rbuStrCompare(const char *z1, const char *z2){
205587 if( z1==0 && z2==0 ) return 0;
205588 if( z1==0 || z2==0 ) return 1;
205589 return (sqlite3_stricmp(z1, z2)!=0);
205590}
205591
205592/*
205593** This function is called as part of sqlite3rbu_open() when initializing
205594** an rbu handle in OAL stage. If the rbu update has not started (i.e.
205595** the rbu_state table was empty) it is a no-op. Otherwise, it arranges
205596** things so that the next call to sqlite3rbu_step() continues on from
205597** where the previous rbu handle left off.
205598**
205599** If an error occurs, an error code and error message are left in the
205600** rbu handle passed as the first argument.
205601*/
205602static void rbuSetupOal(sqlite3rbu *p, RbuState *pState){
205603 assert( p->rc==SQLITE_OK );
205604 if( pState->zTbl ){
205605 RbuObjIter *pIter = &p->objiter;
205606 int rc = SQLITE_OK;
205607
205608 while( rc==SQLITE_OK && pIter->zTbl && (pIter->bCleanup
205609 || rbuStrCompare(pIter->zIdx, pState->zIdx)
205610 || (pState->zDataTbl==0 && rbuStrCompare(pIter->zTbl, pState->zTbl))
205611 || (pState->zDataTbl && rbuStrCompare(pIter->zDataTbl, pState->zDataTbl))
205612 )){
205613 rc = rbuObjIterNext(p, pIter);
205614 }
205615
205616 if( rc==SQLITE_OK && !pIter->zTbl ){
205617 rc = SQLITE_ERROR;
205618 p->zErrmsg = sqlite3_mprintf("rbu_state mismatch error");
205619 }
205620
205621 if( rc==SQLITE_OK ){
205622 p->nStep = pState->nRow;
205623 rc = rbuObjIterPrepareAll(p, &p->objiter, p->nStep);
205624 }
205625
205626 p->rc = rc;
205627 }
205628}
205629
205630/*
205631** If there is a "*-oal" file in the file-system corresponding to the
205632** target database in the file-system, delete it. If an error occurs,
205633** leave an error code and error message in the rbu handle.
205634*/
205635static void rbuDeleteOalFile(sqlite3rbu *p){
205636 char *zOal = rbuMPrintf(p, "%s-oal", p->zTarget);
205637 if( zOal ){
205638 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
205639 assert( pVfs && p->rc==SQLITE_OK && p->zErrmsg==0 );
205640 pVfs->xDelete(pVfs, zOal, 0);
205641 sqlite3_free(zOal);
205642 }
205643}
205644
205645/*
205646** Allocate a private rbu VFS for the rbu handle passed as the only
205647** argument. This VFS will be used unless the call to sqlite3rbu_open()
205648** specified a URI with a vfs=? option in place of a target database
205649** file name.
205650*/
205651static void rbuCreateVfs(sqlite3rbu *p){
205652 int rnd;
205653 char zRnd[64];
205654
205655 assert( p->rc==SQLITE_OK );
205656 sqlite3_randomness(sizeof(int), (void*)&rnd);
205657 sqlite3_snprintf(sizeof(zRnd), zRnd, "rbu_vfs_%d", rnd);
205658 p->rc = sqlite3rbu_create_vfs(zRnd, 0);
205659 if( p->rc==SQLITE_OK ){
205660 sqlite3_vfs *pVfs = sqlite3_vfs_find(zRnd);
205661 assert( pVfs );
205662 p->zVfsName = pVfs->zName;
205663 ((rbu_vfs*)pVfs)->pRbu = p;
205664 }
205665}
205666
205667/*
205668** Destroy the private VFS created for the rbu handle passed as the only
205669** argument by an earlier call to rbuCreateVfs().
205670*/
205671static void rbuDeleteVfs(sqlite3rbu *p){
205672 if( p->zVfsName ){
205673 sqlite3rbu_destroy_vfs(p->zVfsName);
205674 p->zVfsName = 0;
205675 }
205676}
205677
205678/*
205679** This user-defined SQL function is invoked with a single argument - the
205680** name of a table expected to appear in the target database. It returns
205681** the number of auxilliary indexes on the table.
205682*/
205683static void rbuIndexCntFunc(
205684 sqlite3_context *pCtx,
205685 int nVal,
205686 sqlite3_value **apVal
205687){
205688 sqlite3rbu *p = (sqlite3rbu*)sqlite3_user_data(pCtx);
205689 sqlite3_stmt *pStmt = 0;
205690 char *zErrmsg = 0;
205691 int rc;
205692 sqlite3 *db = (rbuIsVacuum(p) ? p->dbRbu : p->dbMain);
205693
205694 assert( nVal==1 );
205695
205696 rc = prepareFreeAndCollectError(db, &pStmt, &zErrmsg,
205697 sqlite3_mprintf("SELECT count(*) FROM sqlite_schema "
205698 "WHERE type='index' AND tbl_name = %Q", sqlite3_value_text(apVal[0]))
205699 );
205700 if( rc!=SQLITE_OK ){
205701 sqlite3_result_error(pCtx, zErrmsg, -1);
205702 }else{
205703 int nIndex = 0;
205704 if( SQLITE_ROW==sqlite3_step(pStmt) ){
205705 nIndex = sqlite3_column_int(pStmt, 0);
205706 }
205707 rc = sqlite3_finalize(pStmt);
205708 if( rc==SQLITE_OK ){
205709 sqlite3_result_int(pCtx, nIndex);
205710 }else{
205711 sqlite3_result_error(pCtx, sqlite3_errmsg(db), -1);
205712 }
205713 }
205714
205715 sqlite3_free(zErrmsg);
205716}
205717
205718/*
205719** If the RBU database contains the rbu_count table, use it to initialize
205720** the sqlite3rbu.nPhaseOneStep variable. The schema of the rbu_count table
205721** is assumed to contain the same columns as:
205722**
205723** CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID;
205724**
205725** There should be one row in the table for each data_xxx table in the
205726** database. The 'tbl' column should contain the name of a data_xxx table,
205727** and the cnt column the number of rows it contains.
205728**
205729** sqlite3rbu.nPhaseOneStep is initialized to the sum of (1 + nIndex) * cnt
205730** for all rows in the rbu_count table, where nIndex is the number of
205731** indexes on the corresponding target database table.
205732*/
205733static void rbuInitPhaseOneSteps(sqlite3rbu *p){
205734 if( p->rc==SQLITE_OK ){
205735 sqlite3_stmt *pStmt = 0;
205736 int bExists = 0; /* True if rbu_count exists */
205737
205738 p->nPhaseOneStep = -1;
205739
205740 p->rc = sqlite3_create_function(p->dbRbu,
205741 "rbu_index_cnt", 1, SQLITE_UTF8, (void*)p, rbuIndexCntFunc, 0, 0
205742 );
205743
205744 /* Check for the rbu_count table. If it does not exist, or if an error
205745 ** occurs, nPhaseOneStep will be left set to -1. */
205746 if( p->rc==SQLITE_OK ){
205747 p->rc = prepareAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
205748 "SELECT 1 FROM sqlite_schema WHERE tbl_name = 'rbu_count'"
205749 );
205750 }
205751 if( p->rc==SQLITE_OK ){
205752 if( SQLITE_ROW==sqlite3_step(pStmt) ){
205753 bExists = 1;
205754 }
205755 p->rc = sqlite3_finalize(pStmt);
205756 }
205757
205758 if( p->rc==SQLITE_OK && bExists ){
205759 p->rc = prepareAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
205760 "SELECT sum(cnt * (1 + rbu_index_cnt(rbu_target_name(tbl))))"
205761 "FROM rbu_count"
205762 );
205763 if( p->rc==SQLITE_OK ){
205764 if( SQLITE_ROW==sqlite3_step(pStmt) ){
205765 p->nPhaseOneStep = sqlite3_column_int64(pStmt, 0);
205766 }
205767 p->rc = sqlite3_finalize(pStmt);
205768 }
205769 }
205770 }
205771}
205772
205773
205774static sqlite3rbu *openRbuHandle(
205775 const char *zTarget,
205776 const char *zRbu,
205777 const char *zState
205778){
205779 sqlite3rbu *p;
205780 size_t nTarget = zTarget ? strlen(zTarget) : 0;
205781 size_t nRbu = strlen(zRbu);
205782 size_t nByte = sizeof(sqlite3rbu) + nTarget+1 + nRbu+1;
205783
205784 p = (sqlite3rbu*)sqlite3_malloc64(nByte);
205785 if( p ){
205786 RbuState *pState = 0;
205787
205788 /* Create the custom VFS. */
205789 memset(p, 0, sizeof(sqlite3rbu));
205790 rbuCreateVfs(p);
205791
205792 /* Open the target, RBU and state databases */
205793 if( p->rc==SQLITE_OK ){
205794 char *pCsr = (char*)&p[1];
205795 int bRetry = 0;
205796 if( zTarget ){
205797 p->zTarget = pCsr;
205798 memcpy(p->zTarget, zTarget, nTarget+1);
205799 pCsr += nTarget+1;
205800 }
205801 p->zRbu = pCsr;
205802 memcpy(p->zRbu, zRbu, nRbu+1);
205803 pCsr += nRbu+1;
205804 if( zState ){
205805 p->zState = rbuMPrintf(p, "%s", zState);
205806 }
205807
205808 /* If the first attempt to open the database file fails and the bRetry
205809 ** flag it set, this means that the db was not opened because it seemed
205810 ** to be a wal-mode db. But, this may have happened due to an earlier
205811 ** RBU vacuum operation leaving an old wal file in the directory.
205812 ** If this is the case, it will have been checkpointed and deleted
205813 ** when the handle was closed and a second attempt to open the
205814 ** database may succeed. */
205815 rbuOpenDatabase(p, 0, &bRetry);
205816 if( bRetry ){
205817 rbuOpenDatabase(p, 0, 0);
205818 }
205819 }
205820
205821 if( p->rc==SQLITE_OK ){
205822 pState = rbuLoadState(p);
205823 assert( pState || p->rc!=SQLITE_OK );
205824 if( p->rc==SQLITE_OK ){
205825
205826 if( pState->eStage==0 ){
205827 rbuDeleteOalFile(p);
205828 rbuInitPhaseOneSteps(p);
205829 p->eStage = RBU_STAGE_OAL;
205830 }else{
205831 p->eStage = pState->eStage;
205832 p->nPhaseOneStep = pState->nPhaseOneStep;
205833 }
205834 p->nProgress = pState->nProgress;
205835 p->iOalSz = pState->iOalSz;
205836 }
205837 }
205838 assert( p->rc!=SQLITE_OK || p->eStage!=0 );
205839
205840 if( p->rc==SQLITE_OK && p->pTargetFd->pWalFd ){
205841 if( p->eStage==RBU_STAGE_OAL ){
205842 p->rc = SQLITE_ERROR;
205843 p->zErrmsg = sqlite3_mprintf("cannot update wal mode database");
205844 }else if( p->eStage==RBU_STAGE_MOVE ){
205845 p->eStage = RBU_STAGE_CKPT;
205846 p->nStep = 0;
205847 }
205848 }
205849
205850 if( p->rc==SQLITE_OK
205851 && (p->eStage==RBU_STAGE_OAL || p->eStage==RBU_STAGE_MOVE)
205852 && pState->eStage!=0
205853 ){
205854 rbu_file *pFd = (rbuIsVacuum(p) ? p->pRbuFd : p->pTargetFd);
205855 if( pFd->iCookie!=pState->iCookie ){
205856 /* At this point (pTargetFd->iCookie) contains the value of the
205857 ** change-counter cookie (the thing that gets incremented when a
205858 ** transaction is committed in rollback mode) currently stored on
205859 ** page 1 of the database file. */
205860 p->rc = SQLITE_BUSY;
205861 p->zErrmsg = sqlite3_mprintf("database modified during rbu %s",
205862 (rbuIsVacuum(p) ? "vacuum" : "update")
205863 );
205864 }
205865 }
205866
205867 if( p->rc==SQLITE_OK ){
205868 if( p->eStage==RBU_STAGE_OAL ){
205869 sqlite3 *db = p->dbMain;
205870 p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, &p->zErrmsg);
205871
205872 /* Point the object iterator at the first object */
205873 if( p->rc==SQLITE_OK ){
205874 p->rc = rbuObjIterFirst(p, &p->objiter);
205875 }
205876
205877 /* If the RBU database contains no data_xxx tables, declare the RBU
205878 ** update finished. */
205879 if( p->rc==SQLITE_OK && p->objiter.zTbl==0 ){
205880 p->rc = SQLITE_DONE;
205881 p->eStage = RBU_STAGE_DONE;
205882 }else{
205883 if( p->rc==SQLITE_OK && pState->eStage==0 && rbuIsVacuum(p) ){
205884 rbuCopyPragma(p, "page_size");
205885 rbuCopyPragma(p, "auto_vacuum");
205886 }
205887
205888 /* Open transactions both databases. The *-oal file is opened or
205889 ** created at this point. */
205890 if( p->rc==SQLITE_OK ){
205891 p->rc = sqlite3_exec(db, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
205892 }
205893
205894 /* Check if the main database is a zipvfs db. If it is, set the upper
205895 ** level pager to use "journal_mode=off". This prevents it from
205896 ** generating a large journal using a temp file. */
205897 if( p->rc==SQLITE_OK ){
205898 int frc = sqlite3_file_control(db, "main", SQLITE_FCNTL_ZIPVFS, 0);
205899 if( frc==SQLITE_OK ){
205900 p->rc = sqlite3_exec(
205901 db, "PRAGMA journal_mode=off",0,0,&p->zErrmsg);
205902 }
205903 }
205904
205905 if( p->rc==SQLITE_OK ){
205906 rbuSetupOal(p, pState);
205907 }
205908 }
205909 }else if( p->eStage==RBU_STAGE_MOVE ){
205910 /* no-op */
205911 }else if( p->eStage==RBU_STAGE_CKPT ){
205912 if( !rbuIsVacuum(p) && rbuExclusiveCheckpoint(p->dbMain) ){
205913 /* If the rbu_exclusive_checkpoint=1 URI parameter was specified
205914 ** and an incremental checkpoint is being resumed, attempt an
205915 ** exclusive lock on the db file. If this fails, so be it. */
205916 p->eStage = RBU_STAGE_DONE;
205917 rbuLockDatabase(p->dbMain);
205918 p->eStage = RBU_STAGE_CKPT;
205919 }
205920 rbuSetupCheckpoint(p, pState);
205921 }else if( p->eStage==RBU_STAGE_DONE ){
205922 p->rc = SQLITE_DONE;
205923 }else{
205924 p->rc = SQLITE_CORRUPT;
205925 }
205926 }
205927
205928 rbuFreeState(pState);
205929 }
205930
205931 return p;
205932}
205933
205934/*
205935** Allocate and return an RBU handle with all fields zeroed except for the
205936** error code, which is set to SQLITE_MISUSE.
205937*/
205938static sqlite3rbu *rbuMisuseError(void){
205939 sqlite3rbu *pRet;
205940 pRet = sqlite3_malloc64(sizeof(sqlite3rbu));
205941 if( pRet ){
205942 memset(pRet, 0, sizeof(sqlite3rbu));
205943 pRet->rc = SQLITE_MISUSE;
205944 }
205945 return pRet;
205946}
205947
205948/*
205949** Open and return a new RBU handle.
205950*/
205951SQLITE_API sqlite3rbu *sqlite3rbu_open(
205952 const char *zTarget,
205953 const char *zRbu,
205954 const char *zState
205955){
205956 if( zTarget==0 || zRbu==0 ){ return rbuMisuseError(); }
205957 return openRbuHandle(zTarget, zRbu, zState);
205958}
205959
205960/*
205961** Open a handle to begin or resume an RBU VACUUM operation.
205962*/
205963SQLITE_API sqlite3rbu *sqlite3rbu_vacuum(
205964 const char *zTarget,
205965 const char *zState
205966){
205967 if( zTarget==0 ){ return rbuMisuseError(); }
205968 if( zState ){
205969 int n = strlen(zState);
205970 if( n>=7 && 0==memcmp("-vactmp", &zState[n-7], 7) ){
205971 return rbuMisuseError();
205972 }
205973 }
205974 /* TODO: Check that both arguments are non-NULL */
205975 return openRbuHandle(0, zTarget, zState);
205976}
205977
205978/*
205979** Return the database handle used by pRbu.
205980*/
205981SQLITE_API sqlite3 *sqlite3rbu_db(sqlite3rbu *pRbu, int bRbu){
205982 sqlite3 *db = 0;
205983 if( pRbu ){
205984 db = (bRbu ? pRbu->dbRbu : pRbu->dbMain);
205985 }
205986 return db;
205987}
205988
205989
205990/*
205991** If the error code currently stored in the RBU handle is SQLITE_CONSTRAINT,
205992** then edit any error message string so as to remove all occurrences of
205993** the pattern "rbu_imp_[0-9]*".
205994*/
205995static void rbuEditErrmsg(sqlite3rbu *p){
205996 if( p->rc==SQLITE_CONSTRAINT && p->zErrmsg ){
205997 unsigned int i;
205998 size_t nErrmsg = strlen(p->zErrmsg);
205999 for(i=0; i<(nErrmsg-8); i++){
206000 if( memcmp(&p->zErrmsg[i], "rbu_imp_", 8)==0 ){
206001 int nDel = 8;
206002 while( p->zErrmsg[i+nDel]>='0' && p->zErrmsg[i+nDel]<='9' ) nDel++;
206003 memmove(&p->zErrmsg[i], &p->zErrmsg[i+nDel], nErrmsg + 1 - i - nDel);
206004 nErrmsg -= nDel;
206005 }
206006 }
206007 }
206008}
206009
206010/*
206011** Close the RBU handle.
206012*/
206013SQLITE_API int sqlite3rbu_close(sqlite3rbu *p, char **pzErrmsg){
206014 int rc;
206015 if( p ){
206016
206017 /* Commit the transaction to the *-oal file. */
206018 if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
206019 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
206020 }
206021
206022 /* Sync the db file if currently doing an incremental checkpoint */
206023 if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_CKPT ){
206024 sqlite3_file *pDb = p->pTargetFd->pReal;
206025 p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
206026 }
206027
206028 rbuSaveState(p, p->eStage);
206029
206030 if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
206031 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
206032 }
206033
206034 /* Close any open statement handles. */
206035 rbuObjIterFinalize(&p->objiter);
206036
206037 /* If this is an RBU vacuum handle and the vacuum has either finished
206038 ** successfully or encountered an error, delete the contents of the
206039 ** state table. This causes the next call to sqlite3rbu_vacuum()
206040 ** specifying the current target and state databases to start a new
206041 ** vacuum from scratch. */
206042 if( rbuIsVacuum(p) && p->rc!=SQLITE_OK && p->dbRbu ){
206043 int rc2 = sqlite3_exec(p->dbRbu, "DELETE FROM stat.rbu_state", 0, 0, 0);
206044 if( p->rc==SQLITE_DONE && rc2!=SQLITE_OK ) p->rc = rc2;
206045 }
206046
206047 /* Close the open database handle and VFS object. */
206048 sqlite3_close(p->dbRbu);
206049 sqlite3_close(p->dbMain);
206050 assert( p->szTemp==0 );
206051 rbuDeleteVfs(p);
206052 sqlite3_free(p->aBuf);
206053 sqlite3_free(p->aFrame);
206054
206055 rbuEditErrmsg(p);
206056 rc = p->rc;
206057 if( pzErrmsg ){
206058 *pzErrmsg = p->zErrmsg;
206059 }else{
206060 sqlite3_free(p->zErrmsg);
206061 }
206062 sqlite3_free(p->zState);
206063 sqlite3_free(p);
206064 }else{
206065 rc = SQLITE_NOMEM;
206066 *pzErrmsg = 0;
206067 }
206068 return rc;
206069}
206070
206071/*
206072** Return the total number of key-value operations (inserts, deletes or
206073** updates) that have been performed on the target database since the
206074** current RBU update was started.
206075*/
206076SQLITE_API sqlite3_int64 sqlite3rbu_progress(sqlite3rbu *pRbu){
206077 return pRbu->nProgress;
206078}
206079
206080/*
206081** Return permyriadage progress indications for the two main stages of
206082** an RBU update.
206083*/
206084SQLITE_API void sqlite3rbu_bp_progress(sqlite3rbu *p, int *pnOne, int *pnTwo){
206085 const int MAX_PROGRESS = 10000;
206086 switch( p->eStage ){
206087 case RBU_STAGE_OAL:
206088 if( p->nPhaseOneStep>0 ){
206089 *pnOne = (int)(MAX_PROGRESS * (i64)p->nProgress/(i64)p->nPhaseOneStep);
206090 }else{
206091 *pnOne = -1;
206092 }
206093 *pnTwo = 0;
206094 break;
206095
206096 case RBU_STAGE_MOVE:
206097 *pnOne = MAX_PROGRESS;
206098 *pnTwo = 0;
206099 break;
206100
206101 case RBU_STAGE_CKPT:
206102 *pnOne = MAX_PROGRESS;
206103 *pnTwo = (int)(MAX_PROGRESS * (i64)p->nStep / (i64)p->nFrame);
206104 break;
206105
206106 case RBU_STAGE_DONE:
206107 *pnOne = MAX_PROGRESS;
206108 *pnTwo = MAX_PROGRESS;
206109 break;
206110
206111 default:
206112 assert( 0 );
206113 }
206114}
206115
206116/*
206117** Return the current state of the RBU vacuum or update operation.
206118*/
206119SQLITE_API int sqlite3rbu_state(sqlite3rbu *p){
206120 int aRes[] = {
206121 0, SQLITE_RBU_STATE_OAL, SQLITE_RBU_STATE_MOVE,
206122 0, SQLITE_RBU_STATE_CHECKPOINT, SQLITE_RBU_STATE_DONE
206123 };
206124
206125 assert( RBU_STAGE_OAL==1 );
206126 assert( RBU_STAGE_MOVE==2 );
206127 assert( RBU_STAGE_CKPT==4 );
206128 assert( RBU_STAGE_DONE==5 );
206129 assert( aRes[RBU_STAGE_OAL]==SQLITE_RBU_STATE_OAL );
206130 assert( aRes[RBU_STAGE_MOVE]==SQLITE_RBU_STATE_MOVE );
206131 assert( aRes[RBU_STAGE_CKPT]==SQLITE_RBU_STATE_CHECKPOINT );
206132 assert( aRes[RBU_STAGE_DONE]==SQLITE_RBU_STATE_DONE );
206133
206134 if( p->rc!=SQLITE_OK && p->rc!=SQLITE_DONE ){
206135 return SQLITE_RBU_STATE_ERROR;
206136 }else{
206137 assert( p->rc!=SQLITE_DONE || p->eStage==RBU_STAGE_DONE );
206138 assert( p->eStage==RBU_STAGE_OAL
206139 || p->eStage==RBU_STAGE_MOVE
206140 || p->eStage==RBU_STAGE_CKPT
206141 || p->eStage==RBU_STAGE_DONE
206142 );
206143 return aRes[p->eStage];
206144 }
206145}
206146
206147SQLITE_API int sqlite3rbu_savestate(sqlite3rbu *p){
206148 int rc = p->rc;
206149 if( rc==SQLITE_DONE ) return SQLITE_OK;
206150
206151 assert( p->eStage>=RBU_STAGE_OAL && p->eStage<=RBU_STAGE_DONE );
206152 if( p->eStage==RBU_STAGE_OAL ){
206153 assert( rc!=SQLITE_DONE );
206154 if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, 0);
206155 }
206156
206157 /* Sync the db file */
206158 if( rc==SQLITE_OK && p->eStage==RBU_STAGE_CKPT ){
206159 sqlite3_file *pDb = p->pTargetFd->pReal;
206160 rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
206161 }
206162
206163 p->rc = rc;
206164 rbuSaveState(p, p->eStage);
206165 rc = p->rc;
206166
206167 if( p->eStage==RBU_STAGE_OAL ){
206168 assert( rc!=SQLITE_DONE );
206169 if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0);
206170 if( rc==SQLITE_OK ){
206171 const char *zBegin = rbuIsVacuum(p) ? "BEGIN" : "BEGIN IMMEDIATE";
206172 rc = sqlite3_exec(p->dbRbu, zBegin, 0, 0, 0);
206173 }
206174 if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbMain, "BEGIN IMMEDIATE", 0, 0,0);
206175 }
206176
206177 p->rc = rc;
206178 return rc;
206179}
206180
206181/**************************************************************************
206182** Beginning of RBU VFS shim methods. The VFS shim modifies the behaviour
206183** of a standard VFS in the following ways:
206184**
206185** 1. Whenever the first page of a main database file is read or
206186** written, the value of the change-counter cookie is stored in
206187** rbu_file.iCookie. Similarly, the value of the "write-version"
206188** database header field is stored in rbu_file.iWriteVer. This ensures
206189** that the values are always trustworthy within an open transaction.
206190**
206191** 2. Whenever an SQLITE_OPEN_WAL file is opened, the (rbu_file.pWalFd)
206192** member variable of the associated database file descriptor is set
206193** to point to the new file. A mutex protected linked list of all main
206194** db fds opened using a particular RBU VFS is maintained at
206195** rbu_vfs.pMain to facilitate this.
206196**
206197** 3. Using a new file-control "SQLITE_FCNTL_RBU", a main db rbu_file
206198** object can be marked as the target database of an RBU update. This
206199** turns on the following extra special behaviour:
206200**
206201** 3a. If xAccess() is called to check if there exists a *-wal file
206202** associated with an RBU target database currently in RBU_STAGE_OAL
206203** stage (preparing the *-oal file), the following special handling
206204** applies:
206205**
206206** * if the *-wal file does exist, return SQLITE_CANTOPEN. An RBU
206207** target database may not be in wal mode already.
206208**
206209** * if the *-wal file does not exist, set the output parameter to
206210** non-zero (to tell SQLite that it does exist) anyway.
206211**
206212** Then, when xOpen() is called to open the *-wal file associated with
206213** the RBU target in RBU_STAGE_OAL stage, instead of opening the *-wal
206214** file, the rbu vfs opens the corresponding *-oal file instead.
206215**
206216** 3b. The *-shm pages returned by xShmMap() for a target db file in
206217** RBU_STAGE_OAL mode are actually stored in heap memory. This is to
206218** avoid creating a *-shm file on disk. Additionally, xShmLock() calls
206219** are no-ops on target database files in RBU_STAGE_OAL mode. This is
206220** because assert() statements in some VFS implementations fail if
206221** xShmLock() is called before xShmMap().
206222**
206223** 3c. If an EXCLUSIVE lock is attempted on a target database file in any
206224** mode except RBU_STAGE_DONE (all work completed and checkpointed), it
206225** fails with an SQLITE_BUSY error. This is to stop RBU connections
206226** from automatically checkpointing a *-wal (or *-oal) file from within
206227** sqlite3_close().
206228**
206229** 3d. In RBU_STAGE_CAPTURE mode, all xRead() calls on the wal file, and
206230** all xWrite() calls on the target database file perform no IO.
206231** Instead the frame and page numbers that would be read and written
206232** are recorded. Additionally, successful attempts to obtain exclusive
206233** xShmLock() WRITER, CHECKPOINTER and READ0 locks on the target
206234** database file are recorded. xShmLock() calls to unlock the same
206235** locks are no-ops (so that once obtained, these locks are never
206236** relinquished). Finally, calls to xSync() on the target database
206237** file fail with SQLITE_INTERNAL errors.
206238*/
206239
206240static void rbuUnlockShm(rbu_file *p){
206241 assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
206242 if( p->pRbu ){
206243 int (*xShmLock)(sqlite3_file*,int,int,int) = p->pReal->pMethods->xShmLock;
206244 int i;
206245 for(i=0; i<SQLITE_SHM_NLOCK;i++){
206246 if( (1<<i) & p->pRbu->mLock ){
206247 xShmLock(p->pReal, i, 1, SQLITE_SHM_UNLOCK|SQLITE_SHM_EXCLUSIVE);
206248 }
206249 }
206250 p->pRbu->mLock = 0;
206251 }
206252}
206253
206254/*
206255*/
206256static int rbuUpdateTempSize(rbu_file *pFd, sqlite3_int64 nNew){
206257 sqlite3rbu *pRbu = pFd->pRbu;
206258 i64 nDiff = nNew - pFd->sz;
206259 pRbu->szTemp += nDiff;
206260 pFd->sz = nNew;
206261 assert( pRbu->szTemp>=0 );
206262 if( pRbu->szTempLimit && pRbu->szTemp>pRbu->szTempLimit ) return SQLITE_FULL;
206263 return SQLITE_OK;
206264}
206265
206266/*
206267** Add an item to the main-db lists, if it is not already present.
206268**
206269** There are two main-db lists. One for all file descriptors, and one
206270** for all file descriptors with rbu_file.pDb!=0. If the argument has
206271** rbu_file.pDb!=0, then it is assumed to already be present on the
206272** main list and is only added to the pDb!=0 list.
206273*/
206274static void rbuMainlistAdd(rbu_file *p){
206275 rbu_vfs *pRbuVfs = p->pRbuVfs;
206276 rbu_file *pIter;
206277 assert( (p->openFlags & SQLITE_OPEN_MAIN_DB) );
206278 sqlite3_mutex_enter(pRbuVfs->mutex);
206279 if( p->pRbu==0 ){
206280 for(pIter=pRbuVfs->pMain; pIter; pIter=pIter->pMainNext);
206281 p->pMainNext = pRbuVfs->pMain;
206282 pRbuVfs->pMain = p;
206283 }else{
206284 for(pIter=pRbuVfs->pMainRbu; pIter && pIter!=p; pIter=pIter->pMainRbuNext){}
206285 if( pIter==0 ){
206286 p->pMainRbuNext = pRbuVfs->pMainRbu;
206287 pRbuVfs->pMainRbu = p;
206288 }
206289 }
206290 sqlite3_mutex_leave(pRbuVfs->mutex);
206291}
206292
206293/*
206294** Remove an item from the main-db lists.
206295*/
206296static void rbuMainlistRemove(rbu_file *p){
206297 rbu_file **pp;
206298 sqlite3_mutex_enter(p->pRbuVfs->mutex);
206299 for(pp=&p->pRbuVfs->pMain; *pp && *pp!=p; pp=&((*pp)->pMainNext)){}
206300 if( *pp ) *pp = p->pMainNext;
206301 p->pMainNext = 0;
206302 for(pp=&p->pRbuVfs->pMainRbu; *pp && *pp!=p; pp=&((*pp)->pMainRbuNext)){}
206303 if( *pp ) *pp = p->pMainRbuNext;
206304 p->pMainRbuNext = 0;
206305 sqlite3_mutex_leave(p->pRbuVfs->mutex);
206306}
206307
206308/*
206309** Given that zWal points to a buffer containing a wal file name passed to
206310** either the xOpen() or xAccess() VFS method, search the main-db list for
206311** a file-handle opened by the same database connection on the corresponding
206312** database file.
206313**
206314** If parameter bRbu is true, only search for file-descriptors with
206315** rbu_file.pDb!=0.
206316*/
206317static rbu_file *rbuFindMaindb(rbu_vfs *pRbuVfs, const char *zWal, int bRbu){
206318 rbu_file *pDb;
206319 sqlite3_mutex_enter(pRbuVfs->mutex);
206320 if( bRbu ){
206321 for(pDb=pRbuVfs->pMainRbu; pDb && pDb->zWal!=zWal; pDb=pDb->pMainRbuNext){}
206322 }else{
206323 for(pDb=pRbuVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext){}
206324 }
206325 sqlite3_mutex_leave(pRbuVfs->mutex);
206326 return pDb;
206327}
206328
206329/*
206330** Close an rbu file.
206331*/
206332static int rbuVfsClose(sqlite3_file *pFile){
206333 rbu_file *p = (rbu_file*)pFile;
206334 int rc;
206335 int i;
206336
206337 /* Free the contents of the apShm[] array. And the array itself. */
206338 for(i=0; i<p->nShm; i++){
206339 sqlite3_free(p->apShm[i]);
206340 }
206341 sqlite3_free(p->apShm);
206342 p->apShm = 0;
206343 sqlite3_free(p->zDel);
206344
206345 if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
206346 rbuMainlistRemove(p);
206347 rbuUnlockShm(p);
206348 p->pReal->pMethods->xShmUnmap(p->pReal, 0);
206349 }
206350 else if( (p->openFlags & SQLITE_OPEN_DELETEONCLOSE) && p->pRbu ){
206351 rbuUpdateTempSize(p, 0);
206352 }
206353 assert( p->pMainNext==0 && p->pRbuVfs->pMain!=p );
206354
206355 /* Close the underlying file handle */
206356 rc = p->pReal->pMethods->xClose(p->pReal);
206357 return rc;
206358}
206359
206360
206361/*
206362** Read and return an unsigned 32-bit big-endian integer from the buffer
206363** passed as the only argument.
206364*/
206365static u32 rbuGetU32(u8 *aBuf){
206366 return ((u32)aBuf[0] << 24)
206367 + ((u32)aBuf[1] << 16)
206368 + ((u32)aBuf[2] << 8)
206369 + ((u32)aBuf[3]);
206370}
206371
206372/*
206373** Write an unsigned 32-bit value in big-endian format to the supplied
206374** buffer.
206375*/
206376static void rbuPutU32(u8 *aBuf, u32 iVal){
206377 aBuf[0] = (iVal >> 24) & 0xFF;
206378 aBuf[1] = (iVal >> 16) & 0xFF;
206379 aBuf[2] = (iVal >> 8) & 0xFF;
206380 aBuf[3] = (iVal >> 0) & 0xFF;
206381}
206382
206383static void rbuPutU16(u8 *aBuf, u16 iVal){
206384 aBuf[0] = (iVal >> 8) & 0xFF;
206385 aBuf[1] = (iVal >> 0) & 0xFF;
206386}
206387
206388/*
206389** Read data from an rbuVfs-file.
206390*/
206391static int rbuVfsRead(
206392 sqlite3_file *pFile,
206393 void *zBuf,
206394 int iAmt,
206395 sqlite_int64 iOfst
206396){
206397 rbu_file *p = (rbu_file*)pFile;
206398 sqlite3rbu *pRbu = p->pRbu;
206399 int rc;
206400
206401 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
206402 assert( p->openFlags & SQLITE_OPEN_WAL );
206403 rc = rbuCaptureWalRead(p->pRbu, iOfst, iAmt);
206404 }else{
206405 if( pRbu && pRbu->eStage==RBU_STAGE_OAL
206406 && (p->openFlags & SQLITE_OPEN_WAL)
206407 && iOfst>=pRbu->iOalSz
206408 ){
206409 rc = SQLITE_OK;
206410 memset(zBuf, 0, iAmt);
206411 }else{
206412 rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
206413#if 1
206414 /* If this is being called to read the first page of the target
206415 ** database as part of an rbu vacuum operation, synthesize the
206416 ** contents of the first page if it does not yet exist. Otherwise,
206417 ** SQLite will not check for a *-wal file. */
206418 if( pRbu && rbuIsVacuum(pRbu)
206419 && rc==SQLITE_IOERR_SHORT_READ && iOfst==0
206420 && (p->openFlags & SQLITE_OPEN_MAIN_DB)
206421 && pRbu->rc==SQLITE_OK
206422 ){
206423 sqlite3_file *pFd = (sqlite3_file*)pRbu->pRbuFd;
206424 rc = pFd->pMethods->xRead(pFd, zBuf, iAmt, iOfst);
206425 if( rc==SQLITE_OK ){
206426 u8 *aBuf = (u8*)zBuf;
206427 u32 iRoot = rbuGetU32(&aBuf[52]) ? 1 : 0;
206428 rbuPutU32(&aBuf[52], iRoot); /* largest root page number */
206429 rbuPutU32(&aBuf[36], 0); /* number of free pages */
206430 rbuPutU32(&aBuf[32], 0); /* first page on free list trunk */
206431 rbuPutU32(&aBuf[28], 1); /* size of db file in pages */
206432 rbuPutU32(&aBuf[24], pRbu->pRbuFd->iCookie+1); /* Change counter */
206433
206434 if( iAmt>100 ){
206435 memset(&aBuf[100], 0, iAmt-100);
206436 rbuPutU16(&aBuf[105], iAmt & 0xFFFF);
206437 aBuf[100] = 0x0D;
206438 }
206439 }
206440 }
206441#endif
206442 }
206443 if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
206444 /* These look like magic numbers. But they are stable, as they are part
206445 ** of the definition of the SQLite file format, which may not change. */
206446 u8 *pBuf = (u8*)zBuf;
206447 p->iCookie = rbuGetU32(&pBuf[24]);
206448 p->iWriteVer = pBuf[19];
206449 }
206450 }
206451 return rc;
206452}
206453
206454/*
206455** Write data to an rbuVfs-file.
206456*/
206457static int rbuVfsWrite(
206458 sqlite3_file *pFile,
206459 const void *zBuf,
206460 int iAmt,
206461 sqlite_int64 iOfst
206462){
206463 rbu_file *p = (rbu_file*)pFile;
206464 sqlite3rbu *pRbu = p->pRbu;
206465 int rc;
206466
206467 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
206468 assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
206469 rc = rbuCaptureDbWrite(p->pRbu, iOfst);
206470 }else{
206471 if( pRbu ){
206472 if( pRbu->eStage==RBU_STAGE_OAL
206473 && (p->openFlags & SQLITE_OPEN_WAL)
206474 && iOfst>=pRbu->iOalSz
206475 ){
206476 pRbu->iOalSz = iAmt + iOfst;
206477 }else if( p->openFlags & SQLITE_OPEN_DELETEONCLOSE ){
206478 i64 szNew = iAmt+iOfst;
206479 if( szNew>p->sz ){
206480 rc = rbuUpdateTempSize(p, szNew);
206481 if( rc!=SQLITE_OK ) return rc;
206482 }
206483 }
206484 }
206485 rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
206486 if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
206487 /* These look like magic numbers. But they are stable, as they are part
206488 ** of the definition of the SQLite file format, which may not change. */
206489 u8 *pBuf = (u8*)zBuf;
206490 p->iCookie = rbuGetU32(&pBuf[24]);
206491 p->iWriteVer = pBuf[19];
206492 }
206493 }
206494 return rc;
206495}
206496
206497/*
206498** Truncate an rbuVfs-file.
206499*/
206500static int rbuVfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
206501 rbu_file *p = (rbu_file*)pFile;
206502 if( (p->openFlags & SQLITE_OPEN_DELETEONCLOSE) && p->pRbu ){
206503 int rc = rbuUpdateTempSize(p, size);
206504 if( rc!=SQLITE_OK ) return rc;
206505 }
206506 return p->pReal->pMethods->xTruncate(p->pReal, size);
206507}
206508
206509/*
206510** Sync an rbuVfs-file.
206511*/
206512static int rbuVfsSync(sqlite3_file *pFile, int flags){
206513 rbu_file *p = (rbu_file *)pFile;
206514 if( p->pRbu && p->pRbu->eStage==RBU_STAGE_CAPTURE ){
206515 if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
206516 return SQLITE_INTERNAL;
206517 }
206518 return SQLITE_OK;
206519 }
206520 return p->pReal->pMethods->xSync(p->pReal, flags);
206521}
206522
206523/*
206524** Return the current file-size of an rbuVfs-file.
206525*/
206526static int rbuVfsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
206527 rbu_file *p = (rbu_file *)pFile;
206528 int rc;
206529 rc = p->pReal->pMethods->xFileSize(p->pReal, pSize);
206530
206531 /* If this is an RBU vacuum operation and this is the target database,
206532 ** pretend that it has at least one page. Otherwise, SQLite will not
206533 ** check for the existance of a *-wal file. rbuVfsRead() contains
206534 ** similar logic. */
206535 if( rc==SQLITE_OK && *pSize==0
206536 && p->pRbu && rbuIsVacuum(p->pRbu)
206537 && (p->openFlags & SQLITE_OPEN_MAIN_DB)
206538 ){
206539 *pSize = 1024;
206540 }
206541 return rc;
206542}
206543
206544/*
206545** Lock an rbuVfs-file.
206546*/
206547static int rbuVfsLock(sqlite3_file *pFile, int eLock){
206548 rbu_file *p = (rbu_file*)pFile;
206549 sqlite3rbu *pRbu = p->pRbu;
206550 int rc = SQLITE_OK;
206551
206552 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
206553 if( eLock==SQLITE_LOCK_EXCLUSIVE
206554 && (p->bNolock || (pRbu && pRbu->eStage!=RBU_STAGE_DONE))
206555 ){
206556 /* Do not allow EXCLUSIVE locks. Preventing SQLite from taking this
206557 ** prevents it from checkpointing the database from sqlite3_close(). */
206558 rc = SQLITE_BUSY;
206559 }else{
206560 rc = p->pReal->pMethods->xLock(p->pReal, eLock);
206561 }
206562
206563 return rc;
206564}
206565
206566/*
206567** Unlock an rbuVfs-file.
206568*/
206569static int rbuVfsUnlock(sqlite3_file *pFile, int eLock){
206570 rbu_file *p = (rbu_file *)pFile;
206571 return p->pReal->pMethods->xUnlock(p->pReal, eLock);
206572}
206573
206574/*
206575** Check if another file-handle holds a RESERVED lock on an rbuVfs-file.
206576*/
206577static int rbuVfsCheckReservedLock(sqlite3_file *pFile, int *pResOut){
206578 rbu_file *p = (rbu_file *)pFile;
206579 return p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
206580}
206581
206582/*
206583** File control method. For custom operations on an rbuVfs-file.
206584*/
206585static int rbuVfsFileControl(sqlite3_file *pFile, int op, void *pArg){
206586 rbu_file *p = (rbu_file *)pFile;
206587 int (*xControl)(sqlite3_file*,int,void*) = p->pReal->pMethods->xFileControl;
206588 int rc;
206589
206590 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB)
206591 || p->openFlags & (SQLITE_OPEN_TRANSIENT_DB|SQLITE_OPEN_TEMP_JOURNAL)
206592 );
206593 if( op==SQLITE_FCNTL_RBU ){
206594 sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
206595
206596 /* First try to find another RBU vfs lower down in the vfs stack. If
206597 ** one is found, this vfs will operate in pass-through mode. The lower
206598 ** level vfs will do the special RBU handling. */
206599 rc = xControl(p->pReal, op, pArg);
206600
206601 if( rc==SQLITE_NOTFOUND ){
206602 /* Now search for a zipvfs instance lower down in the VFS stack. If
206603 ** one is found, this is an error. */
206604 void *dummy = 0;
206605 rc = xControl(p->pReal, SQLITE_FCNTL_ZIPVFS, &dummy);
206606 if( rc==SQLITE_OK ){
206607 rc = SQLITE_ERROR;
206608 pRbu->zErrmsg = sqlite3_mprintf("rbu/zipvfs setup error");
206609 }else if( rc==SQLITE_NOTFOUND ){
206610 pRbu->pTargetFd = p;
206611 p->pRbu = pRbu;
206612 rbuMainlistAdd(p);
206613 if( p->pWalFd ) p->pWalFd->pRbu = pRbu;
206614 rc = SQLITE_OK;
206615 }
206616 }
206617 return rc;
206618 }
206619 else if( op==SQLITE_FCNTL_RBUCNT ){
206620 sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
206621 pRbu->nRbu++;
206622 pRbu->pRbuFd = p;
206623 p->bNolock = 1;
206624 }
206625
206626 rc = xControl(p->pReal, op, pArg);
206627 if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
206628 rbu_vfs *pRbuVfs = p->pRbuVfs;
206629 char *zIn = *(char**)pArg;
206630 char *zOut = sqlite3_mprintf("rbu(%s)/%z", pRbuVfs->base.zName, zIn);
206631 *(char**)pArg = zOut;
206632 if( zOut==0 ) rc = SQLITE_NOMEM;
206633 }
206634
206635 return rc;
206636}
206637
206638/*
206639** Return the sector-size in bytes for an rbuVfs-file.
206640*/
206641static int rbuVfsSectorSize(sqlite3_file *pFile){
206642 rbu_file *p = (rbu_file *)pFile;
206643 return p->pReal->pMethods->xSectorSize(p->pReal);
206644}
206645
206646/*
206647** Return the device characteristic flags supported by an rbuVfs-file.
206648*/
206649static int rbuVfsDeviceCharacteristics(sqlite3_file *pFile){
206650 rbu_file *p = (rbu_file *)pFile;
206651 return p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
206652}
206653
206654/*
206655** Take or release a shared-memory lock.
206656*/
206657static int rbuVfsShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
206658 rbu_file *p = (rbu_file*)pFile;
206659 sqlite3rbu *pRbu = p->pRbu;
206660 int rc = SQLITE_OK;
206661
206662#ifdef SQLITE_AMALGAMATION
206663 assert( WAL_CKPT_LOCK==1 );
206664#endif
206665
206666 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
206667 if( pRbu && (
206668 pRbu->eStage==RBU_STAGE_OAL
206669 || pRbu->eStage==RBU_STAGE_MOVE
206670 || pRbu->eStage==RBU_STAGE_DONE
206671 )){
206672 /* Prevent SQLite from taking a shm-lock on the target file when it
206673 ** is supplying heap memory to the upper layer in place of *-shm
206674 ** segments. */
206675 if( ofst==WAL_LOCK_CKPT && n==1 ) rc = SQLITE_BUSY;
206676 }else{
206677 int bCapture = 0;
206678 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
206679 bCapture = 1;
206680 }
206681 if( bCapture==0 || 0==(flags & SQLITE_SHM_UNLOCK) ){
206682 rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
206683 if( bCapture && rc==SQLITE_OK ){
206684 pRbu->mLock |= ((1<<n) - 1) << ofst;
206685 }
206686 }
206687 }
206688
206689 return rc;
206690}
206691
206692/*
206693** Obtain a pointer to a mapping of a single 32KiB page of the *-shm file.
206694*/
206695static int rbuVfsShmMap(
206696 sqlite3_file *pFile,
206697 int iRegion,
206698 int szRegion,
206699 int isWrite,
206700 void volatile **pp
206701){
206702 rbu_file *p = (rbu_file*)pFile;
206703 int rc = SQLITE_OK;
206704 int eStage = (p->pRbu ? p->pRbu->eStage : 0);
206705
206706 /* If not in RBU_STAGE_OAL, allow this call to pass through. Or, if this
206707 ** rbu is in the RBU_STAGE_OAL state, use heap memory for *-shm space
206708 ** instead of a file on disk. */
206709 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
206710 if( eStage==RBU_STAGE_OAL ){
206711 sqlite3_int64 nByte = (iRegion+1) * sizeof(char*);
206712 char **apNew = (char**)sqlite3_realloc64(p->apShm, nByte);
206713
206714 /* This is an RBU connection that uses its own heap memory for the
206715 ** pages of the *-shm file. Since no other process can have run
206716 ** recovery, the connection must request *-shm pages in order
206717 ** from start to finish. */
206718 assert( iRegion==p->nShm );
206719 if( apNew==0 ){
206720 rc = SQLITE_NOMEM;
206721 }else{
206722 memset(&apNew[p->nShm], 0, sizeof(char*) * (1 + iRegion - p->nShm));
206723 p->apShm = apNew;
206724 p->nShm = iRegion+1;
206725 }
206726
206727 if( rc==SQLITE_OK ){
206728 char *pNew = (char*)sqlite3_malloc64(szRegion);
206729 if( pNew==0 ){
206730 rc = SQLITE_NOMEM;
206731 }else{
206732 memset(pNew, 0, szRegion);
206733 p->apShm[iRegion] = pNew;
206734 }
206735 }
206736
206737 if( rc==SQLITE_OK ){
206738 *pp = p->apShm[iRegion];
206739 }else{
206740 *pp = 0;
206741 }
206742 }else{
206743 assert( p->apShm==0 );
206744 rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp);
206745 }
206746
206747 return rc;
206748}
206749
206750/*
206751** Memory barrier.
206752*/
206753static void rbuVfsShmBarrier(sqlite3_file *pFile){
206754 rbu_file *p = (rbu_file *)pFile;
206755 p->pReal->pMethods->xShmBarrier(p->pReal);
206756}
206757
206758/*
206759** The xShmUnmap method.
206760*/
206761static int rbuVfsShmUnmap(sqlite3_file *pFile, int delFlag){
206762 rbu_file *p = (rbu_file*)pFile;
206763 int rc = SQLITE_OK;
206764 int eStage = (p->pRbu ? p->pRbu->eStage : 0);
206765
206766 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
206767 if( eStage==RBU_STAGE_OAL || eStage==RBU_STAGE_MOVE ){
206768 /* no-op */
206769 }else{
206770 /* Release the checkpointer and writer locks */
206771 rbuUnlockShm(p);
206772 rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
206773 }
206774 return rc;
206775}
206776
206777/*
206778** Open an rbu file handle.
206779*/
206780static int rbuVfsOpen(
206781 sqlite3_vfs *pVfs,
206782 const char *zName,
206783 sqlite3_file *pFile,
206784 int flags,
206785 int *pOutFlags
206786){
206787 static sqlite3_io_methods rbuvfs_io_methods = {
206788 2, /* iVersion */
206789 rbuVfsClose, /* xClose */
206790 rbuVfsRead, /* xRead */
206791 rbuVfsWrite, /* xWrite */
206792 rbuVfsTruncate, /* xTruncate */
206793 rbuVfsSync, /* xSync */
206794 rbuVfsFileSize, /* xFileSize */
206795 rbuVfsLock, /* xLock */
206796 rbuVfsUnlock, /* xUnlock */
206797 rbuVfsCheckReservedLock, /* xCheckReservedLock */
206798 rbuVfsFileControl, /* xFileControl */
206799 rbuVfsSectorSize, /* xSectorSize */
206800 rbuVfsDeviceCharacteristics, /* xDeviceCharacteristics */
206801 rbuVfsShmMap, /* xShmMap */
206802 rbuVfsShmLock, /* xShmLock */
206803 rbuVfsShmBarrier, /* xShmBarrier */
206804 rbuVfsShmUnmap, /* xShmUnmap */
206805 0, 0 /* xFetch, xUnfetch */
206806 };
206807 rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
206808 sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
206809 rbu_file *pFd = (rbu_file *)pFile;
206810 int rc = SQLITE_OK;
206811 const char *zOpen = zName;
206812 int oflags = flags;
206813
206814 memset(pFd, 0, sizeof(rbu_file));
206815 pFd->pReal = (sqlite3_file*)&pFd[1];
206816 pFd->pRbuVfs = pRbuVfs;
206817 pFd->openFlags = flags;
206818 if( zName ){
206819 if( flags & SQLITE_OPEN_MAIN_DB ){
206820 /* A main database has just been opened. The following block sets
206821 ** (pFd->zWal) to point to a buffer owned by SQLite that contains
206822 ** the name of the *-wal file this db connection will use. SQLite
206823 ** happens to pass a pointer to this buffer when using xAccess()
206824 ** or xOpen() to operate on the *-wal file. */
206825 pFd->zWal = sqlite3_filename_wal(zName);
206826 }
206827 else if( flags & SQLITE_OPEN_WAL ){
206828 rbu_file *pDb = rbuFindMaindb(pRbuVfs, zName, 0);
206829 if( pDb ){
206830 if( pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
206831 /* This call is to open a *-wal file. Intead, open the *-oal. */
206832 size_t nOpen;
206833 if( rbuIsVacuum(pDb->pRbu) ){
206834 zOpen = sqlite3_db_filename(pDb->pRbu->dbRbu, "main");
206835 zOpen = sqlite3_filename_wal(zOpen);
206836 }
206837 nOpen = strlen(zOpen);
206838 ((char*)zOpen)[nOpen-3] = 'o';
206839 pFd->pRbu = pDb->pRbu;
206840 }
206841 pDb->pWalFd = pFd;
206842 }
206843 }
206844 }else{
206845 pFd->pRbu = pRbuVfs->pRbu;
206846 }
206847
206848 if( oflags & SQLITE_OPEN_MAIN_DB
206849 && sqlite3_uri_boolean(zName, "rbu_memory", 0)
206850 ){
206851 assert( oflags & SQLITE_OPEN_MAIN_DB );
206852 oflags = SQLITE_OPEN_TEMP_DB | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
206853 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
206854 zOpen = 0;
206855 }
206856
206857 if( rc==SQLITE_OK ){
206858 rc = pRealVfs->xOpen(pRealVfs, zOpen, pFd->pReal, oflags, pOutFlags);
206859 }
206860 if( pFd->pReal->pMethods ){
206861 /* The xOpen() operation has succeeded. Set the sqlite3_file.pMethods
206862 ** pointer and, if the file is a main database file, link it into the
206863 ** mutex protected linked list of all such files. */
206864 pFile->pMethods = &rbuvfs_io_methods;
206865 if( flags & SQLITE_OPEN_MAIN_DB ){
206866 rbuMainlistAdd(pFd);
206867 }
206868 }else{
206869 sqlite3_free(pFd->zDel);
206870 }
206871
206872 return rc;
206873}
206874
206875/*
206876** Delete the file located at zPath.
206877*/
206878static int rbuVfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
206879 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
206880 return pRealVfs->xDelete(pRealVfs, zPath, dirSync);
206881}
206882
206883/*
206884** Test for access permissions. Return true if the requested permission
206885** is available, or false otherwise.
206886*/
206887static int rbuVfsAccess(
206888 sqlite3_vfs *pVfs,
206889 const char *zPath,
206890 int flags,
206891 int *pResOut
206892){
206893 rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
206894 sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
206895 int rc;
206896
206897 rc = pRealVfs->xAccess(pRealVfs, zPath, flags, pResOut);
206898
206899 /* If this call is to check if a *-wal file associated with an RBU target
206900 ** database connection exists, and the RBU update is in RBU_STAGE_OAL,
206901 ** the following special handling is activated:
206902 **
206903 ** a) if the *-wal file does exist, return SQLITE_CANTOPEN. This
206904 ** ensures that the RBU extension never tries to update a database
206905 ** in wal mode, even if the first page of the database file has
206906 ** been damaged.
206907 **
206908 ** b) if the *-wal file does not exist, claim that it does anyway,
206909 ** causing SQLite to call xOpen() to open it. This call will also
206910 ** be intercepted (see the rbuVfsOpen() function) and the *-oal
206911 ** file opened instead.
206912 */
206913 if( rc==SQLITE_OK && flags==SQLITE_ACCESS_EXISTS ){
206914 rbu_file *pDb = rbuFindMaindb(pRbuVfs, zPath, 1);
206915 if( pDb && pDb->pRbu->eStage==RBU_STAGE_OAL ){
206916 assert( pDb->pRbu );
206917 if( *pResOut ){
206918 rc = SQLITE_CANTOPEN;
206919 }else{
206920 sqlite3_int64 sz = 0;
206921 rc = rbuVfsFileSize(&pDb->base, &sz);
206922 *pResOut = (sz>0);
206923 }
206924 }
206925 }
206926
206927 return rc;
206928}
206929
206930/*
206931** Populate buffer zOut with the full canonical pathname corresponding
206932** to the pathname in zPath. zOut is guaranteed to point to a buffer
206933** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
206934*/
206935static int rbuVfsFullPathname(
206936 sqlite3_vfs *pVfs,
206937 const char *zPath,
206938 int nOut,
206939 char *zOut
206940){
206941 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
206942 return pRealVfs->xFullPathname(pRealVfs, zPath, nOut, zOut);
206943}
206944
206945#ifndef SQLITE_OMIT_LOAD_EXTENSION
206946/*
206947** Open the dynamic library located at zPath and return a handle.
206948*/
206949static void *rbuVfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
206950 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
206951 return pRealVfs->xDlOpen(pRealVfs, zPath);
206952}
206953
206954/*
206955** Populate the buffer zErrMsg (size nByte bytes) with a human readable
206956** utf-8 string describing the most recent error encountered associated
206957** with dynamic libraries.
206958*/
206959static void rbuVfsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
206960 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
206961 pRealVfs->xDlError(pRealVfs, nByte, zErrMsg);
206962}
206963
206964/*
206965** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
206966*/
206967static void (*rbuVfsDlSym(
206968 sqlite3_vfs *pVfs,
206969 void *pArg,
206970 const char *zSym
206971))(void){
206972 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
206973 return pRealVfs->xDlSym(pRealVfs, pArg, zSym);
206974}
206975
206976/*
206977** Close the dynamic library handle pHandle.
206978*/
206979static void rbuVfsDlClose(sqlite3_vfs *pVfs, void *pHandle){
206980 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
206981 pRealVfs->xDlClose(pRealVfs, pHandle);
206982}
206983#endif /* SQLITE_OMIT_LOAD_EXTENSION */
206984
206985/*
206986** Populate the buffer pointed to by zBufOut with nByte bytes of
206987** random data.
206988*/
206989static int rbuVfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
206990 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
206991 return pRealVfs->xRandomness(pRealVfs, nByte, zBufOut);
206992}
206993
206994/*
206995** Sleep for nMicro microseconds. Return the number of microseconds
206996** actually slept.
206997*/
206998static int rbuVfsSleep(sqlite3_vfs *pVfs, int nMicro){
206999 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
207000 return pRealVfs->xSleep(pRealVfs, nMicro);
207001}
207002
207003/*
207004** Return the current time as a Julian Day number in *pTimeOut.
207005*/
207006static int rbuVfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
207007 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
207008 return pRealVfs->xCurrentTime(pRealVfs, pTimeOut);
207009}
207010
207011/*
207012** No-op.
207013*/
207014static int rbuVfsGetLastError(sqlite3_vfs *pVfs, int a, char *b){
207015 return 0;
207016}
207017
207018/*
207019** Deregister and destroy an RBU vfs created by an earlier call to
207020** sqlite3rbu_create_vfs().
207021*/
207022SQLITE_API void sqlite3rbu_destroy_vfs(const char *zName){
207023 sqlite3_vfs *pVfs = sqlite3_vfs_find(zName);
207024 if( pVfs && pVfs->xOpen==rbuVfsOpen ){
207025 sqlite3_mutex_free(((rbu_vfs*)pVfs)->mutex);
207026 sqlite3_vfs_unregister(pVfs);
207027 sqlite3_free(pVfs);
207028 }
207029}
207030
207031/*
207032** Create an RBU VFS named zName that accesses the underlying file-system
207033** via existing VFS zParent. The new object is registered as a non-default
207034** VFS with SQLite before returning.
207035*/
207036SQLITE_API int sqlite3rbu_create_vfs(const char *zName, const char *zParent){
207037
207038 /* Template for VFS */
207039 static sqlite3_vfs vfs_template = {
207040 1, /* iVersion */
207041 0, /* szOsFile */
207042 0, /* mxPathname */
207043 0, /* pNext */
207044 0, /* zName */
207045 0, /* pAppData */
207046 rbuVfsOpen, /* xOpen */
207047 rbuVfsDelete, /* xDelete */
207048 rbuVfsAccess, /* xAccess */
207049 rbuVfsFullPathname, /* xFullPathname */
207050
207051#ifndef SQLITE_OMIT_LOAD_EXTENSION
207052 rbuVfsDlOpen, /* xDlOpen */
207053 rbuVfsDlError, /* xDlError */
207054 rbuVfsDlSym, /* xDlSym */
207055 rbuVfsDlClose, /* xDlClose */
207056#else
207057 0, 0, 0, 0,
207058#endif
207059
207060 rbuVfsRandomness, /* xRandomness */
207061 rbuVfsSleep, /* xSleep */
207062 rbuVfsCurrentTime, /* xCurrentTime */
207063 rbuVfsGetLastError, /* xGetLastError */
207064 0, /* xCurrentTimeInt64 (version 2) */
207065 0, 0, 0 /* Unimplemented version 3 methods */
207066 };
207067
207068 rbu_vfs *pNew = 0; /* Newly allocated VFS */
207069 int rc = SQLITE_OK;
207070 size_t nName;
207071 size_t nByte;
207072
207073 nName = strlen(zName);
207074 nByte = sizeof(rbu_vfs) + nName + 1;
207075 pNew = (rbu_vfs*)sqlite3_malloc64(nByte);
207076 if( pNew==0 ){
207077 rc = SQLITE_NOMEM;
207078 }else{
207079 sqlite3_vfs *pParent; /* Parent VFS */
207080 memset(pNew, 0, nByte);
207081 pParent = sqlite3_vfs_find(zParent);
207082 if( pParent==0 ){
207083 rc = SQLITE_NOTFOUND;
207084 }else{
207085 char *zSpace;
207086 memcpy(&pNew->base, &vfs_template, sizeof(sqlite3_vfs));
207087 pNew->base.mxPathname = pParent->mxPathname;
207088 pNew->base.szOsFile = sizeof(rbu_file) + pParent->szOsFile;
207089 pNew->pRealVfs = pParent;
207090 pNew->base.zName = (const char*)(zSpace = (char*)&pNew[1]);
207091 memcpy(zSpace, zName, nName);
207092
207093 /* Allocate the mutex and register the new VFS (not as the default) */
207094 pNew->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE);
207095 if( pNew->mutex==0 ){
207096 rc = SQLITE_NOMEM;
207097 }else{
207098 rc = sqlite3_vfs_register(&pNew->base, 0);
207099 }
207100 }
207101
207102 if( rc!=SQLITE_OK ){
207103 sqlite3_mutex_free(pNew->mutex);
207104 sqlite3_free(pNew);
207105 }
207106 }
207107
207108 return rc;
207109}
207110
207111/*
207112** Configure the aggregate temp file size limit for this RBU handle.
207113*/
207114SQLITE_API sqlite3_int64 sqlite3rbu_temp_size_limit(sqlite3rbu *pRbu, sqlite3_int64 n){
207115 if( n>=0 ){
207116 pRbu->szTempLimit = n;
207117 }
207118 return pRbu->szTempLimit;
207119}
207120
207121SQLITE_API sqlite3_int64 sqlite3rbu_temp_size(sqlite3rbu *pRbu){
207122 return pRbu->szTemp;
207123}
207124
207125
207126/**************************************************************************/
207127
207128#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU) */
207129
207130/************** End of sqlite3rbu.c ******************************************/
207131/************** Begin file dbstat.c ******************************************/
207132/*
207133** 2010 July 12
207134**
207135** The author disclaims copyright to this source code. In place of
207136** a legal notice, here is a blessing:
207137**
207138** May you do good and not evil.
207139** May you find forgiveness for yourself and forgive others.
207140** May you share freely, never taking more than you give.
207141**
207142******************************************************************************
207143**
207144** This file contains an implementation of the "dbstat" virtual table.
207145**
207146** The dbstat virtual table is used to extract low-level storage
207147** information from an SQLite database in order to implement the
207148** "sqlite3_analyzer" utility. See the ../tool/spaceanal.tcl script
207149** for an example implementation.
207150**
207151** Additional information is available on the "dbstat.html" page of the
207152** official SQLite documentation.
207153*/
207154
207155/* #include "sqliteInt.h" ** Requires access to internal data structures ** */
207156#if (defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)) \
207157 && !defined(SQLITE_OMIT_VIRTUALTABLE)
207158
207159/*
207160** The pager and btree modules arrange objects in memory so that there are
207161** always approximately 200 bytes of addressable memory following each page
207162** buffer. This way small buffer overreads caused by corrupt database pages
207163** do not cause undefined behaviour. This module pads each page buffer
207164** by the following number of bytes for the same purpose.
207165*/
207166#define DBSTAT_PAGE_PADDING_BYTES 256
207167
207168/*
207169** Page paths:
207170**
207171** The value of the 'path' column describes the path taken from the
207172** root-node of the b-tree structure to each page. The value of the
207173** root-node path is '/'.
207174**
207175** The value of the path for the left-most child page of the root of
207176** a b-tree is '/000/'. (Btrees store content ordered from left to right
207177** so the pages to the left have smaller keys than the pages to the right.)
207178** The next to left-most child of the root page is
207179** '/001', and so on, each sibling page identified by a 3-digit hex
207180** value. The children of the 451st left-most sibling have paths such
207181** as '/1c2/000/, '/1c2/001/' etc.
207182**
207183** Overflow pages are specified by appending a '+' character and a
207184** six-digit hexadecimal value to the path to the cell they are linked
207185** from. For example, the three overflow pages in a chain linked from
207186** the left-most cell of the 450th child of the root page are identified
207187** by the paths:
207188**
207189** '/1c2/000+000000' // First page in overflow chain
207190** '/1c2/000+000001' // Second page in overflow chain
207191** '/1c2/000+000002' // Third page in overflow chain
207192**
207193** If the paths are sorted using the BINARY collation sequence, then
207194** the overflow pages associated with a cell will appear earlier in the
207195** sort-order than its child page:
207196**
207197** '/1c2/000/' // Left-most child of 451st child of root
207198*/
207199static const char zDbstatSchema[] =
207200 "CREATE TABLE x("
207201 " name TEXT," /* 0 Name of table or index */
207202 " path TEXT," /* 1 Path to page from root (NULL for agg) */
207203 " pageno INTEGER," /* 2 Page number (page count for aggregates) */
207204 " pagetype TEXT," /* 3 'internal', 'leaf', 'overflow', or NULL */
207205 " ncell INTEGER," /* 4 Cells on page (0 for overflow) */
207206 " payload INTEGER," /* 5 Bytes of payload on this page */
207207 " unused INTEGER," /* 6 Bytes of unused space on this page */
207208 " mx_payload INTEGER," /* 7 Largest payload size of all cells */
207209 " pgoffset INTEGER," /* 8 Offset of page in file (NULL for agg) */
207210 " pgsize INTEGER," /* 9 Size of the page (sum for aggregate) */
207211 " schema TEXT HIDDEN," /* 10 Database schema being analyzed */
207212 " aggregate BOOLEAN HIDDEN" /* 11 aggregate info for each table */
207213 ")"
207214;
207215
207216/* Forward reference to data structured used in this module */
207217typedef struct StatTable StatTable;
207218typedef struct StatCursor StatCursor;
207219typedef struct StatPage StatPage;
207220typedef struct StatCell StatCell;
207221
207222/* Size information for a single cell within a btree page */
207223struct StatCell {
207224 int nLocal; /* Bytes of local payload */
207225 u32 iChildPg; /* Child node (or 0 if this is a leaf) */
207226 int nOvfl; /* Entries in aOvfl[] */
207227 u32 *aOvfl; /* Array of overflow page numbers */
207228 int nLastOvfl; /* Bytes of payload on final overflow page */
207229 int iOvfl; /* Iterates through aOvfl[] */
207230};
207231
207232/* Size information for a single btree page */
207233struct StatPage {
207234 u32 iPgno; /* Page number */
207235 u8 *aPg; /* Page buffer from sqlite3_malloc() */
207236 int iCell; /* Current cell */
207237 char *zPath; /* Path to this page */
207238
207239 /* Variables populated by statDecodePage(): */
207240 u8 flags; /* Copy of flags byte */
207241 int nCell; /* Number of cells on page */
207242 int nUnused; /* Number of unused bytes on page */
207243 StatCell *aCell; /* Array of parsed cells */
207244 u32 iRightChildPg; /* Right-child page number (or 0) */
207245 int nMxPayload; /* Largest payload of any cell on the page */
207246};
207247
207248/* The cursor for scanning the dbstat virtual table */
207249struct StatCursor {
207250 sqlite3_vtab_cursor base; /* base class. MUST BE FIRST! */
207251 sqlite3_stmt *pStmt; /* Iterates through set of root pages */
207252 u8 isEof; /* After pStmt has returned SQLITE_DONE */
207253 u8 isAgg; /* Aggregate results for each table */
207254 int iDb; /* Schema used for this query */
207255
207256 StatPage aPage[32]; /* Pages in path to current page */
207257 int iPage; /* Current entry in aPage[] */
207258
207259 /* Values to return. */
207260 u32 iPageno; /* Value of 'pageno' column */
207261 char *zName; /* Value of 'name' column */
207262 char *zPath; /* Value of 'path' column */
207263 char *zPagetype; /* Value of 'pagetype' column */
207264 int nPage; /* Number of pages in current btree */
207265 int nCell; /* Value of 'ncell' column */
207266 int nMxPayload; /* Value of 'mx_payload' column */
207267 i64 nUnused; /* Value of 'unused' column */
207268 i64 nPayload; /* Value of 'payload' column */
207269 i64 iOffset; /* Value of 'pgOffset' column */
207270 i64 szPage; /* Value of 'pgSize' column */
207271};
207272
207273/* An instance of the DBSTAT virtual table */
207274struct StatTable {
207275 sqlite3_vtab base; /* base class. MUST BE FIRST! */
207276 sqlite3 *db; /* Database connection that owns this vtab */
207277 int iDb; /* Index of database to analyze */
207278};
207279
207280#ifndef get2byte
207281# define get2byte(x) ((x)[0]<<8 | (x)[1])
207282#endif
207283
207284/*
207285** Connect to or create a new DBSTAT virtual table.
207286*/
207287static int statConnect(
207288 sqlite3 *db,
207289 void *pAux,
207290 int argc, const char *const*argv,
207291 sqlite3_vtab **ppVtab,
207292 char **pzErr
207293){
207294 StatTable *pTab = 0;
207295 int rc = SQLITE_OK;
207296 int iDb;
207297
207298 if( argc>=4 ){
207299 Token nm;
207300 sqlite3TokenInit(&nm, (char*)argv[3]);
207301 iDb = sqlite3FindDb(db, &nm);
207302 if( iDb<0 ){
207303 *pzErr = sqlite3_mprintf("no such database: %s", argv[3]);
207304 return SQLITE_ERROR;
207305 }
207306 }else{
207307 iDb = 0;
207308 }
207309 sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
207310 rc = sqlite3_declare_vtab(db, zDbstatSchema);
207311 if( rc==SQLITE_OK ){
207312 pTab = (StatTable *)sqlite3_malloc64(sizeof(StatTable));
207313 if( pTab==0 ) rc = SQLITE_NOMEM_BKPT;
207314 }
207315
207316 assert( rc==SQLITE_OK || pTab==0 );
207317 if( rc==SQLITE_OK ){
207318 memset(pTab, 0, sizeof(StatTable));
207319 pTab->db = db;
207320 pTab->iDb = iDb;
207321 }
207322
207323 *ppVtab = (sqlite3_vtab*)pTab;
207324 return rc;
207325}
207326
207327/*
207328** Disconnect from or destroy the DBSTAT virtual table.
207329*/
207330static int statDisconnect(sqlite3_vtab *pVtab){
207331 sqlite3_free(pVtab);
207332 return SQLITE_OK;
207333}
207334
207335/*
207336** Compute the best query strategy and return the result in idxNum.
207337**
207338** idxNum-Bit Meaning
207339** ---------- ----------------------------------------------
207340** 0x01 There is a schema=? term in the WHERE clause
207341** 0x02 There is a name=? term in the WHERE clause
207342** 0x04 There is an aggregate=? term in the WHERE clause
207343** 0x08 Output should be ordered by name and path
207344*/
207345static int statBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
207346 int i;
207347 int iSchema = -1;
207348 int iName = -1;
207349 int iAgg = -1;
207350
207351 /* Look for a valid schema=? constraint. If found, change the idxNum to
207352 ** 1 and request the value of that constraint be sent to xFilter. And
207353 ** lower the cost estimate to encourage the constrained version to be
207354 ** used.
207355 */
207356 for(i=0; i<pIdxInfo->nConstraint; i++){
207357 if( pIdxInfo->aConstraint[i].op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
207358 if( pIdxInfo->aConstraint[i].usable==0 ){
207359 /* Force DBSTAT table should always be the right-most table in a join */
207360 return SQLITE_CONSTRAINT;
207361 }
207362 switch( pIdxInfo->aConstraint[i].iColumn ){
207363 case 0: { /* name */
207364 iName = i;
207365 break;
207366 }
207367 case 10: { /* schema */
207368 iSchema = i;
207369 break;
207370 }
207371 case 11: { /* aggregate */
207372 iAgg = i;
207373 break;
207374 }
207375 }
207376 }
207377 i = 0;
207378 if( iSchema>=0 ){
207379 pIdxInfo->aConstraintUsage[iSchema].argvIndex = ++i;
207380 pIdxInfo->aConstraintUsage[iSchema].omit = 1;
207381 pIdxInfo->idxNum |= 0x01;
207382 }
207383 if( iName>=0 ){
207384 pIdxInfo->aConstraintUsage[iName].argvIndex = ++i;
207385 pIdxInfo->idxNum |= 0x02;
207386 }
207387 if( iAgg>=0 ){
207388 pIdxInfo->aConstraintUsage[iAgg].argvIndex = ++i;
207389 pIdxInfo->idxNum |= 0x04;
207390 }
207391 pIdxInfo->estimatedCost = 1.0;
207392
207393 /* Records are always returned in ascending order of (name, path).
207394 ** If this will satisfy the client, set the orderByConsumed flag so that
207395 ** SQLite does not do an external sort.
207396 */
207397 if( ( pIdxInfo->nOrderBy==1
207398 && pIdxInfo->aOrderBy[0].iColumn==0
207399 && pIdxInfo->aOrderBy[0].desc==0
207400 ) ||
207401 ( pIdxInfo->nOrderBy==2
207402 && pIdxInfo->aOrderBy[0].iColumn==0
207403 && pIdxInfo->aOrderBy[0].desc==0
207404 && pIdxInfo->aOrderBy[1].iColumn==1
207405 && pIdxInfo->aOrderBy[1].desc==0
207406 )
207407 ){
207408 pIdxInfo->orderByConsumed = 1;
207409 pIdxInfo->idxNum |= 0x08;
207410 }
207411
207412 return SQLITE_OK;
207413}
207414
207415/*
207416** Open a new DBSTAT cursor.
207417*/
207418static int statOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
207419 StatTable *pTab = (StatTable *)pVTab;
207420 StatCursor *pCsr;
207421
207422 pCsr = (StatCursor *)sqlite3_malloc64(sizeof(StatCursor));
207423 if( pCsr==0 ){
207424 return SQLITE_NOMEM_BKPT;
207425 }else{
207426 memset(pCsr, 0, sizeof(StatCursor));
207427 pCsr->base.pVtab = pVTab;
207428 pCsr->iDb = pTab->iDb;
207429 }
207430
207431 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
207432 return SQLITE_OK;
207433}
207434
207435static void statClearCells(StatPage *p){
207436 int i;
207437 if( p->aCell ){
207438 for(i=0; i<p->nCell; i++){
207439 sqlite3_free(p->aCell[i].aOvfl);
207440 }
207441 sqlite3_free(p->aCell);
207442 }
207443 p->nCell = 0;
207444 p->aCell = 0;
207445}
207446
207447static void statClearPage(StatPage *p){
207448 u8 *aPg = p->aPg;
207449 statClearCells(p);
207450 sqlite3_free(p->zPath);
207451 memset(p, 0, sizeof(StatPage));
207452 p->aPg = aPg;
207453}
207454
207455static void statResetCsr(StatCursor *pCsr){
207456 int i;
207457 /* In some circumstances, specifically if an OOM has occurred, the call
207458 ** to sqlite3_reset() may cause the pager to be reset (emptied). It is
207459 ** important that statClearPage() is called to free any page refs before
207460 ** this happens. dbsqlfuzz 9ed3e4e3816219d3509d711636c38542bf3f40b1. */
207461 for(i=0; i<ArraySize(pCsr->aPage); i++){
207462 statClearPage(&pCsr->aPage[i]);
207463 sqlite3_free(pCsr->aPage[i].aPg);
207464 pCsr->aPage[i].aPg = 0;
207465 }
207466 sqlite3_reset(pCsr->pStmt);
207467 pCsr->iPage = 0;
207468 sqlite3_free(pCsr->zPath);
207469 pCsr->zPath = 0;
207470 pCsr->isEof = 0;
207471}
207472
207473/* Resize the space-used counters inside of the cursor */
207474static void statResetCounts(StatCursor *pCsr){
207475 pCsr->nCell = 0;
207476 pCsr->nMxPayload = 0;
207477 pCsr->nUnused = 0;
207478 pCsr->nPayload = 0;
207479 pCsr->szPage = 0;
207480 pCsr->nPage = 0;
207481}
207482
207483/*
207484** Close a DBSTAT cursor.
207485*/
207486static int statClose(sqlite3_vtab_cursor *pCursor){
207487 StatCursor *pCsr = (StatCursor *)pCursor;
207488 statResetCsr(pCsr);
207489 sqlite3_finalize(pCsr->pStmt);
207490 sqlite3_free(pCsr);
207491 return SQLITE_OK;
207492}
207493
207494/*
207495** For a single cell on a btree page, compute the number of bytes of
207496** content (payload) stored on that page. That is to say, compute the
207497** number of bytes of content not found on overflow pages.
207498*/
207499static int getLocalPayload(
207500 int nUsable, /* Usable bytes per page */
207501 u8 flags, /* Page flags */
207502 int nTotal /* Total record (payload) size */
207503){
207504 int nLocal;
207505 int nMinLocal;
207506 int nMaxLocal;
207507
207508 if( flags==0x0D ){ /* Table leaf node */
207509 nMinLocal = (nUsable - 12) * 32 / 255 - 23;
207510 nMaxLocal = nUsable - 35;
207511 }else{ /* Index interior and leaf nodes */
207512 nMinLocal = (nUsable - 12) * 32 / 255 - 23;
207513 nMaxLocal = (nUsable - 12) * 64 / 255 - 23;
207514 }
207515
207516 nLocal = nMinLocal + (nTotal - nMinLocal) % (nUsable - 4);
207517 if( nLocal>nMaxLocal ) nLocal = nMinLocal;
207518 return nLocal;
207519}
207520
207521/* Populate the StatPage object with information about the all
207522** cells found on the page currently under analysis.
207523*/
207524static int statDecodePage(Btree *pBt, StatPage *p){
207525 int nUnused;
207526 int iOff;
207527 int nHdr;
207528 int isLeaf;
207529 int szPage;
207530
207531 u8 *aData = p->aPg;
207532 u8 *aHdr = &aData[p->iPgno==1 ? 100 : 0];
207533
207534 p->flags = aHdr[0];
207535 if( p->flags==0x0A || p->flags==0x0D ){
207536 isLeaf = 1;
207537 nHdr = 8;
207538 }else if( p->flags==0x05 || p->flags==0x02 ){
207539 isLeaf = 0;
207540 nHdr = 12;
207541 }else{
207542 goto statPageIsCorrupt;
207543 }
207544 if( p->iPgno==1 ) nHdr += 100;
207545 p->nCell = get2byte(&aHdr[3]);
207546 p->nMxPayload = 0;
207547 szPage = sqlite3BtreeGetPageSize(pBt);
207548
207549 nUnused = get2byte(&aHdr[5]) - nHdr - 2*p->nCell;
207550 nUnused += (int)aHdr[7];
207551 iOff = get2byte(&aHdr[1]);
207552 while( iOff ){
207553 int iNext;
207554 if( iOff>=szPage ) goto statPageIsCorrupt;
207555 nUnused += get2byte(&aData[iOff+2]);
207556 iNext = get2byte(&aData[iOff]);
207557 if( iNext<iOff+4 && iNext>0 ) goto statPageIsCorrupt;
207558 iOff = iNext;
207559 }
207560 p->nUnused = nUnused;
207561 p->iRightChildPg = isLeaf ? 0 : sqlite3Get4byte(&aHdr[8]);
207562
207563 if( p->nCell ){
207564 int i; /* Used to iterate through cells */
207565 int nUsable; /* Usable bytes per page */
207566
207567 sqlite3BtreeEnter(pBt);
207568 nUsable = szPage - sqlite3BtreeGetReserveNoMutex(pBt);
207569 sqlite3BtreeLeave(pBt);
207570 p->aCell = sqlite3_malloc64((p->nCell+1) * sizeof(StatCell));
207571 if( p->aCell==0 ) return SQLITE_NOMEM_BKPT;
207572 memset(p->aCell, 0, (p->nCell+1) * sizeof(StatCell));
207573
207574 for(i=0; i<p->nCell; i++){
207575 StatCell *pCell = &p->aCell[i];
207576
207577 iOff = get2byte(&aData[nHdr+i*2]);
207578 if( iOff<nHdr || iOff>=szPage ) goto statPageIsCorrupt;
207579 if( !isLeaf ){
207580 pCell->iChildPg = sqlite3Get4byte(&aData[iOff]);
207581 iOff += 4;
207582 }
207583 if( p->flags==0x05 ){
207584 /* A table interior node. nPayload==0. */
207585 }else{
207586 u32 nPayload; /* Bytes of payload total (local+overflow) */
207587 int nLocal; /* Bytes of payload stored locally */
207588 iOff += getVarint32(&aData[iOff], nPayload);
207589 if( p->flags==0x0D ){
207590 u64 dummy;
207591 iOff += sqlite3GetVarint(&aData[iOff], &dummy);
207592 }
207593 if( nPayload>(u32)p->nMxPayload ) p->nMxPayload = nPayload;
207594 nLocal = getLocalPayload(nUsable, p->flags, nPayload);
207595 if( nLocal<0 ) goto statPageIsCorrupt;
207596 pCell->nLocal = nLocal;
207597 assert( nPayload>=(u32)nLocal );
207598 assert( nLocal<=(nUsable-35) );
207599 if( nPayload>(u32)nLocal ){
207600 int j;
207601 int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4);
207602 if( iOff+nLocal+4>nUsable || nPayload>0x7fffffff ){
207603 goto statPageIsCorrupt;
207604 }
207605 pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4);
207606 pCell->nOvfl = nOvfl;
207607 pCell->aOvfl = sqlite3_malloc64(sizeof(u32)*nOvfl);
207608 if( pCell->aOvfl==0 ) return SQLITE_NOMEM_BKPT;
207609 pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]);
207610 for(j=1; j<nOvfl; j++){
207611 int rc;
207612 u32 iPrev = pCell->aOvfl[j-1];
207613 DbPage *pPg = 0;
207614 rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPrev, &pPg, 0);
207615 if( rc!=SQLITE_OK ){
207616 assert( pPg==0 );
207617 return rc;
207618 }
207619 pCell->aOvfl[j] = sqlite3Get4byte(sqlite3PagerGetData(pPg));
207620 sqlite3PagerUnref(pPg);
207621 }
207622 }
207623 }
207624 }
207625 }
207626
207627 return SQLITE_OK;
207628
207629statPageIsCorrupt:
207630 p->flags = 0;
207631 statClearCells(p);
207632 return SQLITE_OK;
207633}
207634
207635/*
207636** Populate the pCsr->iOffset and pCsr->szPage member variables. Based on
207637** the current value of pCsr->iPageno.
207638*/
207639static void statSizeAndOffset(StatCursor *pCsr){
207640 StatTable *pTab = (StatTable *)((sqlite3_vtab_cursor *)pCsr)->pVtab;
207641 Btree *pBt = pTab->db->aDb[pTab->iDb].pBt;
207642 Pager *pPager = sqlite3BtreePager(pBt);
207643 sqlite3_file *fd;
207644 sqlite3_int64 x[2];
207645
207646 /* If connected to a ZIPVFS backend, find the page size and
207647 ** offset from ZIPVFS.
207648 */
207649 fd = sqlite3PagerFile(pPager);
207650 x[0] = pCsr->iPageno;
207651 if( sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){
207652 pCsr->iOffset = x[0];
207653 pCsr->szPage += x[1];
207654 }else{
207655 /* Not ZIPVFS: The default page size and offset */
207656 pCsr->szPage += sqlite3BtreeGetPageSize(pBt);
207657 pCsr->iOffset = (i64)pCsr->szPage * (pCsr->iPageno - 1);
207658 }
207659}
207660
207661/*
207662** Load a copy of the page data for page iPg into the buffer belonging
207663** to page object pPg. Allocate the buffer if necessary. Return SQLITE_OK
207664** if successful, or an SQLite error code otherwise.
207665*/
207666static int statGetPage(
207667 Btree *pBt, /* Load page from this b-tree */
207668 u32 iPg, /* Page number to load */
207669 StatPage *pPg /* Load page into this object */
207670){
207671 int pgsz = sqlite3BtreeGetPageSize(pBt);
207672 DbPage *pDbPage = 0;
207673 int rc;
207674
207675 if( pPg->aPg==0 ){
207676 pPg->aPg = (u8*)sqlite3_malloc(pgsz + DBSTAT_PAGE_PADDING_BYTES);
207677 if( pPg->aPg==0 ){
207678 return SQLITE_NOMEM_BKPT;
207679 }
207680 memset(&pPg->aPg[pgsz], 0, DBSTAT_PAGE_PADDING_BYTES);
207681 }
207682
207683 rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPg, &pDbPage, 0);
207684 if( rc==SQLITE_OK ){
207685 const u8 *a = sqlite3PagerGetData(pDbPage);
207686 memcpy(pPg->aPg, a, pgsz);
207687 sqlite3PagerUnref(pDbPage);
207688 }
207689
207690 return rc;
207691}
207692
207693/*
207694** Move a DBSTAT cursor to the next entry. Normally, the next
207695** entry will be the next page, but in aggregated mode (pCsr->isAgg!=0),
207696** the next entry is the next btree.
207697*/
207698static int statNext(sqlite3_vtab_cursor *pCursor){
207699 int rc;
207700 int nPayload;
207701 char *z;
207702 StatCursor *pCsr = (StatCursor *)pCursor;
207703 StatTable *pTab = (StatTable *)pCursor->pVtab;
207704 Btree *pBt = pTab->db->aDb[pCsr->iDb].pBt;
207705 Pager *pPager = sqlite3BtreePager(pBt);
207706
207707 sqlite3_free(pCsr->zPath);
207708 pCsr->zPath = 0;
207709
207710statNextRestart:
207711 if( pCsr->iPage<0 ){
207712 /* Start measuring space on the next btree */
207713 statResetCounts(pCsr);
207714 rc = sqlite3_step(pCsr->pStmt);
207715 if( rc==SQLITE_ROW ){
207716 int nPage;
207717 u32 iRoot = (u32)sqlite3_column_int64(pCsr->pStmt, 1);
207718 sqlite3PagerPagecount(pPager, &nPage);
207719 if( nPage==0 ){
207720 pCsr->isEof = 1;
207721 return sqlite3_reset(pCsr->pStmt);
207722 }
207723 rc = statGetPage(pBt, iRoot, &pCsr->aPage[0]);
207724 pCsr->aPage[0].iPgno = iRoot;
207725 pCsr->aPage[0].iCell = 0;
207726 if( !pCsr->isAgg ){
207727 pCsr->aPage[0].zPath = z = sqlite3_mprintf("/");
207728 if( z==0 ) rc = SQLITE_NOMEM_BKPT;
207729 }
207730 pCsr->iPage = 0;
207731 pCsr->nPage = 1;
207732 }else{
207733 pCsr->isEof = 1;
207734 return sqlite3_reset(pCsr->pStmt);
207735 }
207736 }else{
207737 /* Continue analyzing the btree previously started */
207738 StatPage *p = &pCsr->aPage[pCsr->iPage];
207739 if( !pCsr->isAgg ) statResetCounts(pCsr);
207740 while( p->iCell<p->nCell ){
207741 StatCell *pCell = &p->aCell[p->iCell];
207742 while( pCell->iOvfl<pCell->nOvfl ){
207743 int nUsable, iOvfl;
207744 sqlite3BtreeEnter(pBt);
207745 nUsable = sqlite3BtreeGetPageSize(pBt) -
207746 sqlite3BtreeGetReserveNoMutex(pBt);
207747 sqlite3BtreeLeave(pBt);
207748 pCsr->nPage++;
207749 statSizeAndOffset(pCsr);
207750 if( pCell->iOvfl<pCell->nOvfl-1 ){
207751 pCsr->nPayload += nUsable - 4;
207752 }else{
207753 pCsr->nPayload += pCell->nLastOvfl;
207754 pCsr->nUnused += nUsable - 4 - pCell->nLastOvfl;
207755 }
207756 iOvfl = pCell->iOvfl;
207757 pCell->iOvfl++;
207758 if( !pCsr->isAgg ){
207759 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
207760 pCsr->iPageno = pCell->aOvfl[iOvfl];
207761 pCsr->zPagetype = "overflow";
207762 pCsr->zPath = z = sqlite3_mprintf(
207763 "%s%.3x+%.6x", p->zPath, p->iCell, iOvfl
207764 );
207765 return z==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK;
207766 }
207767 }
207768 if( p->iRightChildPg ) break;
207769 p->iCell++;
207770 }
207771
207772 if( !p->iRightChildPg || p->iCell>p->nCell ){
207773 statClearPage(p);
207774 pCsr->iPage--;
207775 if( pCsr->isAgg && pCsr->iPage<0 ){
207776 /* label-statNext-done: When computing aggregate space usage over
207777 ** an entire btree, this is the exit point from this function */
207778 return SQLITE_OK;
207779 }
207780 goto statNextRestart; /* Tail recursion */
207781 }
207782 pCsr->iPage++;
207783 if( pCsr->iPage>=ArraySize(pCsr->aPage) ){
207784 statResetCsr(pCsr);
207785 return SQLITE_CORRUPT_BKPT;
207786 }
207787 assert( p==&pCsr->aPage[pCsr->iPage-1] );
207788
207789 if( p->iCell==p->nCell ){
207790 p[1].iPgno = p->iRightChildPg;
207791 }else{
207792 p[1].iPgno = p->aCell[p->iCell].iChildPg;
207793 }
207794 rc = statGetPage(pBt, p[1].iPgno, &p[1]);
207795 pCsr->nPage++;
207796 p[1].iCell = 0;
207797 if( !pCsr->isAgg ){
207798 p[1].zPath = z = sqlite3_mprintf("%s%.3x/", p->zPath, p->iCell);
207799 if( z==0 ) rc = SQLITE_NOMEM_BKPT;
207800 }
207801 p->iCell++;
207802 }
207803
207804
207805 /* Populate the StatCursor fields with the values to be returned
207806 ** by the xColumn() and xRowid() methods.
207807 */
207808 if( rc==SQLITE_OK ){
207809 int i;
207810 StatPage *p = &pCsr->aPage[pCsr->iPage];
207811 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
207812 pCsr->iPageno = p->iPgno;
207813
207814 rc = statDecodePage(pBt, p);
207815 if( rc==SQLITE_OK ){
207816 statSizeAndOffset(pCsr);
207817
207818 switch( p->flags ){
207819 case 0x05: /* table internal */
207820 case 0x02: /* index internal */
207821 pCsr->zPagetype = "internal";
207822 break;
207823 case 0x0D: /* table leaf */
207824 case 0x0A: /* index leaf */
207825 pCsr->zPagetype = "leaf";
207826 break;
207827 default:
207828 pCsr->zPagetype = "corrupted";
207829 break;
207830 }
207831 pCsr->nCell += p->nCell;
207832 pCsr->nUnused += p->nUnused;
207833 if( p->nMxPayload>pCsr->nMxPayload ) pCsr->nMxPayload = p->nMxPayload;
207834 if( !pCsr->isAgg ){
207835 pCsr->zPath = z = sqlite3_mprintf("%s", p->zPath);
207836 if( z==0 ) rc = SQLITE_NOMEM_BKPT;
207837 }
207838 nPayload = 0;
207839 for(i=0; i<p->nCell; i++){
207840 nPayload += p->aCell[i].nLocal;
207841 }
207842 pCsr->nPayload += nPayload;
207843
207844 /* If computing aggregate space usage by btree, continue with the
207845 ** next page. The loop will exit via the return at label-statNext-done
207846 */
207847 if( pCsr->isAgg ) goto statNextRestart;
207848 }
207849 }
207850
207851 return rc;
207852}
207853
207854static int statEof(sqlite3_vtab_cursor *pCursor){
207855 StatCursor *pCsr = (StatCursor *)pCursor;
207856 return pCsr->isEof;
207857}
207858
207859/* Initialize a cursor according to the query plan idxNum using the
207860** arguments in argv[0]. See statBestIndex() for a description of the
207861** meaning of the bits in idxNum.
207862*/
207863static int statFilter(
207864 sqlite3_vtab_cursor *pCursor,
207865 int idxNum, const char *idxStr,
207866 int argc, sqlite3_value **argv
207867){
207868 StatCursor *pCsr = (StatCursor *)pCursor;
207869 StatTable *pTab = (StatTable*)(pCursor->pVtab);
207870 sqlite3_str *pSql; /* Query of btrees to analyze */
207871 char *zSql; /* String value of pSql */
207872 int iArg = 0; /* Count of argv[] parameters used so far */
207873 int rc = SQLITE_OK; /* Result of this operation */
207874 const char *zName = 0; /* Only provide analysis of this table */
207875
207876 statResetCsr(pCsr);
207877 sqlite3_finalize(pCsr->pStmt);
207878 pCsr->pStmt = 0;
207879 if( idxNum & 0x01 ){
207880 /* schema=? constraint is present. Get its value */
207881 const char *zDbase = (const char*)sqlite3_value_text(argv[iArg++]);
207882 pCsr->iDb = sqlite3FindDbName(pTab->db, zDbase);
207883 if( pCsr->iDb<0 ){
207884 pCsr->iDb = 0;
207885 pCsr->isEof = 1;
207886 return SQLITE_OK;
207887 }
207888 }else{
207889 pCsr->iDb = pTab->iDb;
207890 }
207891 if( idxNum & 0x02 ){
207892 /* name=? constraint is present */
207893 zName = (const char*)sqlite3_value_text(argv[iArg++]);
207894 }
207895 if( idxNum & 0x04 ){
207896 /* aggregate=? constraint is present */
207897 pCsr->isAgg = sqlite3_value_double(argv[iArg++])!=0.0;
207898 }else{
207899 pCsr->isAgg = 0;
207900 }
207901 pSql = sqlite3_str_new(pTab->db);
207902 sqlite3_str_appendf(pSql,
207903 "SELECT * FROM ("
207904 "SELECT 'sqlite_schema' AS name,1 AS rootpage,'table' AS type"
207905 " UNION ALL "
207906 "SELECT name,rootpage,type"
207907 " FROM \"%w\".sqlite_schema WHERE rootpage!=0)",
207908 pTab->db->aDb[pCsr->iDb].zDbSName);
207909 if( zName ){
207910 sqlite3_str_appendf(pSql, "WHERE name=%Q", zName);
207911 }
207912 if( idxNum & 0x08 ){
207913 sqlite3_str_appendf(pSql, " ORDER BY name");
207914 }
207915 zSql = sqlite3_str_finish(pSql);
207916 if( zSql==0 ){
207917 return SQLITE_NOMEM_BKPT;
207918 }else{
207919 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
207920 sqlite3_free(zSql);
207921 }
207922
207923 if( rc==SQLITE_OK ){
207924 pCsr->iPage = -1;
207925 rc = statNext(pCursor);
207926 }
207927 return rc;
207928}
207929
207930static int statColumn(
207931 sqlite3_vtab_cursor *pCursor,
207932 sqlite3_context *ctx,
207933 int i
207934){
207935 StatCursor *pCsr = (StatCursor *)pCursor;
207936 switch( i ){
207937 case 0: /* name */
207938 sqlite3_result_text(ctx, pCsr->zName, -1, SQLITE_TRANSIENT);
207939 break;
207940 case 1: /* path */
207941 if( !pCsr->isAgg ){
207942 sqlite3_result_text(ctx, pCsr->zPath, -1, SQLITE_TRANSIENT);
207943 }
207944 break;
207945 case 2: /* pageno */
207946 if( pCsr->isAgg ){
207947 sqlite3_result_int64(ctx, pCsr->nPage);
207948 }else{
207949 sqlite3_result_int64(ctx, pCsr->iPageno);
207950 }
207951 break;
207952 case 3: /* pagetype */
207953 if( !pCsr->isAgg ){
207954 sqlite3_result_text(ctx, pCsr->zPagetype, -1, SQLITE_STATIC);
207955 }
207956 break;
207957 case 4: /* ncell */
207958 sqlite3_result_int(ctx, pCsr->nCell);
207959 break;
207960 case 5: /* payload */
207961 sqlite3_result_int(ctx, pCsr->nPayload);
207962 break;
207963 case 6: /* unused */
207964 sqlite3_result_int(ctx, pCsr->nUnused);
207965 break;
207966 case 7: /* mx_payload */
207967 sqlite3_result_int(ctx, pCsr->nMxPayload);
207968 break;
207969 case 8: /* pgoffset */
207970 if( !pCsr->isAgg ){
207971 sqlite3_result_int64(ctx, pCsr->iOffset);
207972 }
207973 break;
207974 case 9: /* pgsize */
207975 sqlite3_result_int(ctx, pCsr->szPage);
207976 break;
207977 case 10: { /* schema */
207978 sqlite3 *db = sqlite3_context_db_handle(ctx);
207979 int iDb = pCsr->iDb;
207980 sqlite3_result_text(ctx, db->aDb[iDb].zDbSName, -1, SQLITE_STATIC);
207981 break;
207982 }
207983 default: { /* aggregate */
207984 sqlite3_result_int(ctx, pCsr->isAgg);
207985 break;
207986 }
207987 }
207988 return SQLITE_OK;
207989}
207990
207991static int statRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
207992 StatCursor *pCsr = (StatCursor *)pCursor;
207993 *pRowid = pCsr->iPageno;
207994 return SQLITE_OK;
207995}
207996
207997/*
207998** Invoke this routine to register the "dbstat" virtual table module
207999*/
208000SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3 *db){
208001 static sqlite3_module dbstat_module = {
208002 0, /* iVersion */
208003 statConnect, /* xCreate */
208004 statConnect, /* xConnect */
208005 statBestIndex, /* xBestIndex */
208006 statDisconnect, /* xDisconnect */
208007 statDisconnect, /* xDestroy */
208008 statOpen, /* xOpen - open a cursor */
208009 statClose, /* xClose - close a cursor */
208010 statFilter, /* xFilter - configure scan constraints */
208011 statNext, /* xNext - advance a cursor */
208012 statEof, /* xEof - check for end of scan */
208013 statColumn, /* xColumn - read data */
208014 statRowid, /* xRowid - read data */
208015 0, /* xUpdate */
208016 0, /* xBegin */
208017 0, /* xSync */
208018 0, /* xCommit */
208019 0, /* xRollback */
208020 0, /* xFindMethod */
208021 0, /* xRename */
208022 0, /* xSavepoint */
208023 0, /* xRelease */
208024 0, /* xRollbackTo */
208025 0 /* xShadowName */
208026 };
208027 return sqlite3_create_module(db, "dbstat", &dbstat_module, 0);
208028}
208029#elif defined(SQLITE_ENABLE_DBSTAT_VTAB)
208030SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3 *db){ return SQLITE_OK; }
208031#endif /* SQLITE_ENABLE_DBSTAT_VTAB */
208032
208033/************** End of dbstat.c **********************************************/
208034/************** Begin file dbpage.c ******************************************/
208035/*
208036** 2017-10-11
208037**
208038** The author disclaims copyright to this source code. In place of
208039** a legal notice, here is a blessing:
208040**
208041** May you do good and not evil.
208042** May you find forgiveness for yourself and forgive others.
208043** May you share freely, never taking more than you give.
208044**
208045******************************************************************************
208046**
208047** This file contains an implementation of the "sqlite_dbpage" virtual table.
208048**
208049** The sqlite_dbpage virtual table is used to read or write whole raw
208050** pages of the database file. The pager interface is used so that
208051** uncommitted changes and changes recorded in the WAL file are correctly
208052** retrieved.
208053**
208054** Usage example:
208055**
208056** SELECT data FROM sqlite_dbpage('aux1') WHERE pgno=123;
208057**
208058** This is an eponymous virtual table so it does not need to be created before
208059** use. The optional argument to the sqlite_dbpage() table name is the
208060** schema for the database file that is to be read. The default schema is
208061** "main".
208062**
208063** The data field of sqlite_dbpage table can be updated. The new
208064** value must be a BLOB which is the correct page size, otherwise the
208065** update fails. Rows may not be deleted or inserted.
208066*/
208067
208068/* #include "sqliteInt.h" ** Requires access to internal data structures ** */
208069#if (defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)) \
208070 && !defined(SQLITE_OMIT_VIRTUALTABLE)
208071
208072typedef struct DbpageTable DbpageTable;
208073typedef struct DbpageCursor DbpageCursor;
208074
208075struct DbpageCursor {
208076 sqlite3_vtab_cursor base; /* Base class. Must be first */
208077 int pgno; /* Current page number */
208078 int mxPgno; /* Last page to visit on this scan */
208079 Pager *pPager; /* Pager being read/written */
208080 DbPage *pPage1; /* Page 1 of the database */
208081 int iDb; /* Index of database to analyze */
208082 int szPage; /* Size of each page in bytes */
208083};
208084
208085struct DbpageTable {
208086 sqlite3_vtab base; /* Base class. Must be first */
208087 sqlite3 *db; /* The database */
208088};
208089
208090/* Columns */
208091#define DBPAGE_COLUMN_PGNO 0
208092#define DBPAGE_COLUMN_DATA 1
208093#define DBPAGE_COLUMN_SCHEMA 2
208094
208095
208096
208097/*
208098** Connect to or create a dbpagevfs virtual table.
208099*/
208100static int dbpageConnect(
208101 sqlite3 *db,
208102 void *pAux,
208103 int argc, const char *const*argv,
208104 sqlite3_vtab **ppVtab,
208105 char **pzErr
208106){
208107 DbpageTable *pTab = 0;
208108 int rc = SQLITE_OK;
208109
208110 sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
208111 rc = sqlite3_declare_vtab(db,
208112 "CREATE TABLE x(pgno INTEGER PRIMARY KEY, data BLOB, schema HIDDEN)");
208113 if( rc==SQLITE_OK ){
208114 pTab = (DbpageTable *)sqlite3_malloc64(sizeof(DbpageTable));
208115 if( pTab==0 ) rc = SQLITE_NOMEM_BKPT;
208116 }
208117
208118 assert( rc==SQLITE_OK || pTab==0 );
208119 if( rc==SQLITE_OK ){
208120 memset(pTab, 0, sizeof(DbpageTable));
208121 pTab->db = db;
208122 }
208123
208124 *ppVtab = (sqlite3_vtab*)pTab;
208125 return rc;
208126}
208127
208128/*
208129** Disconnect from or destroy a dbpagevfs virtual table.
208130*/
208131static int dbpageDisconnect(sqlite3_vtab *pVtab){
208132 sqlite3_free(pVtab);
208133 return SQLITE_OK;
208134}
208135
208136/*
208137** idxNum:
208138**
208139** 0 schema=main, full table scan
208140** 1 schema=main, pgno=?1
208141** 2 schema=?1, full table scan
208142** 3 schema=?1, pgno=?2
208143*/
208144static int dbpageBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
208145 int i;
208146 int iPlan = 0;
208147
208148 /* If there is a schema= constraint, it must be honored. Report a
208149 ** ridiculously large estimated cost if the schema= constraint is
208150 ** unavailable
208151 */
208152 for(i=0; i<pIdxInfo->nConstraint; i++){
208153 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i];
208154 if( p->iColumn!=DBPAGE_COLUMN_SCHEMA ) continue;
208155 if( p->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
208156 if( !p->usable ){
208157 /* No solution. */
208158 return SQLITE_CONSTRAINT;
208159 }
208160 iPlan = 2;
208161 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
208162 pIdxInfo->aConstraintUsage[i].omit = 1;
208163 break;
208164 }
208165
208166 /* If we reach this point, it means that either there is no schema=
208167 ** constraint (in which case we use the "main" schema) or else the
208168 ** schema constraint was accepted. Lower the estimated cost accordingly
208169 */
208170 pIdxInfo->estimatedCost = 1.0e6;
208171
208172 /* Check for constraints against pgno */
208173 for(i=0; i<pIdxInfo->nConstraint; i++){
208174 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i];
208175 if( p->usable && p->iColumn<=0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
208176 pIdxInfo->estimatedRows = 1;
208177 pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE;
208178 pIdxInfo->estimatedCost = 1.0;
208179 pIdxInfo->aConstraintUsage[i].argvIndex = iPlan ? 2 : 1;
208180 pIdxInfo->aConstraintUsage[i].omit = 1;
208181 iPlan |= 1;
208182 break;
208183 }
208184 }
208185 pIdxInfo->idxNum = iPlan;
208186
208187 if( pIdxInfo->nOrderBy>=1
208188 && pIdxInfo->aOrderBy[0].iColumn<=0
208189 && pIdxInfo->aOrderBy[0].desc==0
208190 ){
208191 pIdxInfo->orderByConsumed = 1;
208192 }
208193 return SQLITE_OK;
208194}
208195
208196/*
208197** Open a new dbpagevfs cursor.
208198*/
208199static int dbpageOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
208200 DbpageCursor *pCsr;
208201
208202 pCsr = (DbpageCursor *)sqlite3_malloc64(sizeof(DbpageCursor));
208203 if( pCsr==0 ){
208204 return SQLITE_NOMEM_BKPT;
208205 }else{
208206 memset(pCsr, 0, sizeof(DbpageCursor));
208207 pCsr->base.pVtab = pVTab;
208208 pCsr->pgno = -1;
208209 }
208210
208211 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
208212 return SQLITE_OK;
208213}
208214
208215/*
208216** Close a dbpagevfs cursor.
208217*/
208218static int dbpageClose(sqlite3_vtab_cursor *pCursor){
208219 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
208220 if( pCsr->pPage1 ) sqlite3PagerUnrefPageOne(pCsr->pPage1);
208221 sqlite3_free(pCsr);
208222 return SQLITE_OK;
208223}
208224
208225/*
208226** Move a dbpagevfs cursor to the next entry in the file.
208227*/
208228static int dbpageNext(sqlite3_vtab_cursor *pCursor){
208229 int rc = SQLITE_OK;
208230 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
208231 pCsr->pgno++;
208232 return rc;
208233}
208234
208235static int dbpageEof(sqlite3_vtab_cursor *pCursor){
208236 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
208237 return pCsr->pgno > pCsr->mxPgno;
208238}
208239
208240/*
208241** idxNum:
208242**
208243** 0 schema=main, full table scan
208244** 1 schema=main, pgno=?1
208245** 2 schema=?1, full table scan
208246** 3 schema=?1, pgno=?2
208247**
208248** idxStr is not used
208249*/
208250static int dbpageFilter(
208251 sqlite3_vtab_cursor *pCursor,
208252 int idxNum, const char *idxStr,
208253 int argc, sqlite3_value **argv
208254){
208255 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
208256 DbpageTable *pTab = (DbpageTable *)pCursor->pVtab;
208257 int rc;
208258 sqlite3 *db = pTab->db;
208259 Btree *pBt;
208260
208261 /* Default setting is no rows of result */
208262 pCsr->pgno = 1;
208263 pCsr->mxPgno = 0;
208264
208265 if( idxNum & 2 ){
208266 const char *zSchema;
208267 assert( argc>=1 );
208268 zSchema = (const char*)sqlite3_value_text(argv[0]);
208269 pCsr->iDb = sqlite3FindDbName(db, zSchema);
208270 if( pCsr->iDb<0 ) return SQLITE_OK;
208271 }else{
208272 pCsr->iDb = 0;
208273 }
208274 pBt = db->aDb[pCsr->iDb].pBt;
208275 if( pBt==0 ) return SQLITE_OK;
208276 pCsr->pPager = sqlite3BtreePager(pBt);
208277 pCsr->szPage = sqlite3BtreeGetPageSize(pBt);
208278 pCsr->mxPgno = sqlite3BtreeLastPage(pBt);
208279 if( idxNum & 1 ){
208280 assert( argc>(idxNum>>1) );
208281 pCsr->pgno = sqlite3_value_int(argv[idxNum>>1]);
208282 if( pCsr->pgno<1 || pCsr->pgno>pCsr->mxPgno ){
208283 pCsr->pgno = 1;
208284 pCsr->mxPgno = 0;
208285 }else{
208286 pCsr->mxPgno = pCsr->pgno;
208287 }
208288 }else{
208289 assert( pCsr->pgno==1 );
208290 }
208291 if( pCsr->pPage1 ) sqlite3PagerUnrefPageOne(pCsr->pPage1);
208292 rc = sqlite3PagerGet(pCsr->pPager, 1, &pCsr->pPage1, 0);
208293 return rc;
208294}
208295
208296static int dbpageColumn(
208297 sqlite3_vtab_cursor *pCursor,
208298 sqlite3_context *ctx,
208299 int i
208300){
208301 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
208302 int rc = SQLITE_OK;
208303 switch( i ){
208304 case 0: { /* pgno */
208305 sqlite3_result_int(ctx, pCsr->pgno);
208306 break;
208307 }
208308 case 1: { /* data */
208309 DbPage *pDbPage = 0;
208310 rc = sqlite3PagerGet(pCsr->pPager, pCsr->pgno, (DbPage**)&pDbPage, 0);
208311 if( rc==SQLITE_OK ){
208312 sqlite3_result_blob(ctx, sqlite3PagerGetData(pDbPage), pCsr->szPage,
208313 SQLITE_TRANSIENT);
208314 }
208315 sqlite3PagerUnref(pDbPage);
208316 break;
208317 }
208318 default: { /* schema */
208319 sqlite3 *db = sqlite3_context_db_handle(ctx);
208320 sqlite3_result_text(ctx, db->aDb[pCsr->iDb].zDbSName, -1, SQLITE_STATIC);
208321 break;
208322 }
208323 }
208324 return SQLITE_OK;
208325}
208326
208327static int dbpageRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
208328 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
208329 *pRowid = pCsr->pgno;
208330 return SQLITE_OK;
208331}
208332
208333static int dbpageUpdate(
208334 sqlite3_vtab *pVtab,
208335 int argc,
208336 sqlite3_value **argv,
208337 sqlite_int64 *pRowid
208338){
208339 DbpageTable *pTab = (DbpageTable *)pVtab;
208340 Pgno pgno;
208341 DbPage *pDbPage = 0;
208342 int rc = SQLITE_OK;
208343 char *zErr = 0;
208344 const char *zSchema;
208345 int iDb;
208346 Btree *pBt;
208347 Pager *pPager;
208348 int szPage;
208349
208350 if( pTab->db->flags & SQLITE_Defensive ){
208351 zErr = "read-only";
208352 goto update_fail;
208353 }
208354 if( argc==1 ){
208355 zErr = "cannot delete";
208356 goto update_fail;
208357 }
208358 pgno = sqlite3_value_int(argv[0]);
208359 if( (Pgno)sqlite3_value_int(argv[1])!=pgno ){
208360 zErr = "cannot insert";
208361 goto update_fail;
208362 }
208363 zSchema = (const char*)sqlite3_value_text(argv[4]);
208364 iDb = zSchema ? sqlite3FindDbName(pTab->db, zSchema) : -1;
208365 if( iDb<0 ){
208366 zErr = "no such schema";
208367 goto update_fail;
208368 }
208369 pBt = pTab->db->aDb[iDb].pBt;
208370 if( pgno<1 || pBt==0 || pgno>(int)sqlite3BtreeLastPage(pBt) ){
208371 zErr = "bad page number";
208372 goto update_fail;
208373 }
208374 szPage = sqlite3BtreeGetPageSize(pBt);
208375 if( sqlite3_value_type(argv[3])!=SQLITE_BLOB
208376 || sqlite3_value_bytes(argv[3])!=szPage
208377 ){
208378 zErr = "bad page value";
208379 goto update_fail;
208380 }
208381 pPager = sqlite3BtreePager(pBt);
208382 rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pDbPage, 0);
208383 if( rc==SQLITE_OK ){
208384 rc = sqlite3PagerWrite(pDbPage);
208385 if( rc==SQLITE_OK ){
208386 memcpy(sqlite3PagerGetData(pDbPage),
208387 sqlite3_value_blob(argv[3]),
208388 szPage);
208389 }
208390 }
208391 sqlite3PagerUnref(pDbPage);
208392 return rc;
208393
208394update_fail:
208395 sqlite3_free(pVtab->zErrMsg);
208396 pVtab->zErrMsg = sqlite3_mprintf("%s", zErr);
208397 return SQLITE_ERROR;
208398}
208399
208400/* Since we do not know in advance which database files will be
208401** written by the sqlite_dbpage virtual table, start a write transaction
208402** on them all.
208403*/
208404static int dbpageBegin(sqlite3_vtab *pVtab){
208405 DbpageTable *pTab = (DbpageTable *)pVtab;
208406 sqlite3 *db = pTab->db;
208407 int i;
208408 for(i=0; i<db->nDb; i++){
208409 Btree *pBt = db->aDb[i].pBt;
208410 if( pBt ) sqlite3BtreeBeginTrans(pBt, 1, 0);
208411 }
208412 return SQLITE_OK;
208413}
208414
208415
208416/*
208417** Invoke this routine to register the "dbpage" virtual table module
208418*/
208419SQLITE_PRIVATE int sqlite3DbpageRegister(sqlite3 *db){
208420 static sqlite3_module dbpage_module = {
208421 0, /* iVersion */
208422 dbpageConnect, /* xCreate */
208423 dbpageConnect, /* xConnect */
208424 dbpageBestIndex, /* xBestIndex */
208425 dbpageDisconnect, /* xDisconnect */
208426 dbpageDisconnect, /* xDestroy */
208427 dbpageOpen, /* xOpen - open a cursor */
208428 dbpageClose, /* xClose - close a cursor */
208429 dbpageFilter, /* xFilter - configure scan constraints */
208430 dbpageNext, /* xNext - advance a cursor */
208431 dbpageEof, /* xEof - check for end of scan */
208432 dbpageColumn, /* xColumn - read data */
208433 dbpageRowid, /* xRowid - read data */
208434 dbpageUpdate, /* xUpdate */
208435 dbpageBegin, /* xBegin */
208436 0, /* xSync */
208437 0, /* xCommit */
208438 0, /* xRollback */
208439 0, /* xFindMethod */
208440 0, /* xRename */
208441 0, /* xSavepoint */
208442 0, /* xRelease */
208443 0, /* xRollbackTo */
208444 0 /* xShadowName */
208445 };
208446 return sqlite3_create_module(db, "sqlite_dbpage", &dbpage_module, 0);
208447}
208448#elif defined(SQLITE_ENABLE_DBPAGE_VTAB)
208449SQLITE_PRIVATE int sqlite3DbpageRegister(sqlite3 *db){ return SQLITE_OK; }
208450#endif /* SQLITE_ENABLE_DBSTAT_VTAB */
208451
208452/************** End of dbpage.c **********************************************/
208453/************** Begin file sqlite3session.c **********************************/
208454
208455#if defined(SQLITE_ENABLE_SESSION) && defined(SQLITE_ENABLE_PREUPDATE_HOOK)
208456/* #include "sqlite3session.h" */
208457/* #include <assert.h> */
208458/* #include <string.h> */
208459
208460#ifndef SQLITE_AMALGAMATION
208461/* # include "sqliteInt.h" */
208462/* # include "vdbeInt.h" */
208463#endif
208464
208465typedef struct SessionTable SessionTable;
208466typedef struct SessionChange SessionChange;
208467typedef struct SessionBuffer SessionBuffer;
208468typedef struct SessionInput SessionInput;
208469
208470/*
208471** Minimum chunk size used by streaming versions of functions.
208472*/
208473#ifndef SESSIONS_STRM_CHUNK_SIZE
208474# ifdef SQLITE_TEST
208475# define SESSIONS_STRM_CHUNK_SIZE 64
208476# else
208477# define SESSIONS_STRM_CHUNK_SIZE 1024
208478# endif
208479#endif
208480
208481static int sessions_strm_chunk_size = SESSIONS_STRM_CHUNK_SIZE;
208482
208483typedef struct SessionHook SessionHook;
208484struct SessionHook {
208485 void *pCtx;
208486 int (*xOld)(void*,int,sqlite3_value**);
208487 int (*xNew)(void*,int,sqlite3_value**);
208488 int (*xCount)(void*);
208489 int (*xDepth)(void*);
208490};
208491
208492/*
208493** Session handle structure.
208494*/
208495struct sqlite3_session {
208496 sqlite3 *db; /* Database handle session is attached to */
208497 char *zDb; /* Name of database session is attached to */
208498 int bEnableSize; /* True if changeset_size() enabled */
208499 int bEnable; /* True if currently recording */
208500 int bIndirect; /* True if all changes are indirect */
208501 int bAutoAttach; /* True to auto-attach tables */
208502 int rc; /* Non-zero if an error has occurred */
208503 void *pFilterCtx; /* First argument to pass to xTableFilter */
208504 int (*xTableFilter)(void *pCtx, const char *zTab);
208505 i64 nMalloc; /* Number of bytes of data allocated */
208506 i64 nMaxChangesetSize;
208507 sqlite3_value *pZeroBlob; /* Value containing X'' */
208508 sqlite3_session *pNext; /* Next session object on same db. */
208509 SessionTable *pTable; /* List of attached tables */
208510 SessionHook hook; /* APIs to grab new and old data with */
208511};
208512
208513/*
208514** Instances of this structure are used to build strings or binary records.
208515*/
208516struct SessionBuffer {
208517 u8 *aBuf; /* Pointer to changeset buffer */
208518 int nBuf; /* Size of buffer aBuf */
208519 int nAlloc; /* Size of allocation containing aBuf */
208520};
208521
208522/*
208523** An object of this type is used internally as an abstraction for
208524** input data. Input data may be supplied either as a single large buffer
208525** (e.g. sqlite3changeset_start()) or using a stream function (e.g.
208526** sqlite3changeset_start_strm()).
208527*/
208528struct SessionInput {
208529 int bNoDiscard; /* If true, do not discard in InputBuffer() */
208530 int iCurrent; /* Offset in aData[] of current change */
208531 int iNext; /* Offset in aData[] of next change */
208532 u8 *aData; /* Pointer to buffer containing changeset */
208533 int nData; /* Number of bytes in aData */
208534
208535 SessionBuffer buf; /* Current read buffer */
208536 int (*xInput)(void*, void*, int*); /* Input stream call (or NULL) */
208537 void *pIn; /* First argument to xInput */
208538 int bEof; /* Set to true after xInput finished */
208539};
208540
208541/*
208542** Structure for changeset iterators.
208543*/
208544struct sqlite3_changeset_iter {
208545 SessionInput in; /* Input buffer or stream */
208546 SessionBuffer tblhdr; /* Buffer to hold apValue/zTab/abPK/ */
208547 int bPatchset; /* True if this is a patchset */
208548 int bInvert; /* True to invert changeset */
208549 int bSkipEmpty; /* Skip noop UPDATE changes */
208550 int rc; /* Iterator error code */
208551 sqlite3_stmt *pConflict; /* Points to conflicting row, if any */
208552 char *zTab; /* Current table */
208553 int nCol; /* Number of columns in zTab */
208554 int op; /* Current operation */
208555 int bIndirect; /* True if current change was indirect */
208556 u8 *abPK; /* Primary key array */
208557 sqlite3_value **apValue; /* old.* and new.* values */
208558};
208559
208560/*
208561** Each session object maintains a set of the following structures, one
208562** for each table the session object is monitoring. The structures are
208563** stored in a linked list starting at sqlite3_session.pTable.
208564**
208565** The keys of the SessionTable.aChange[] hash table are all rows that have
208566** been modified in any way since the session object was attached to the
208567** table.
208568**
208569** The data associated with each hash-table entry is a structure containing
208570** a subset of the initial values that the modified row contained at the
208571** start of the session. Or no initial values if the row was inserted.
208572*/
208573struct SessionTable {
208574 SessionTable *pNext;
208575 char *zName; /* Local name of table */
208576 int nCol; /* Number of columns in table zName */
208577 int bStat1; /* True if this is sqlite_stat1 */
208578 const char **azCol; /* Column names */
208579 u8 *abPK; /* Array of primary key flags */
208580 int nEntry; /* Total number of entries in hash table */
208581 int nChange; /* Size of apChange[] array */
208582 SessionChange **apChange; /* Hash table buckets */
208583};
208584
208585/*
208586** RECORD FORMAT:
208587**
208588** The following record format is similar to (but not compatible with) that
208589** used in SQLite database files. This format is used as part of the
208590** change-set binary format, and so must be architecture independent.
208591**
208592** Unlike the SQLite database record format, each field is self-contained -
208593** there is no separation of header and data. Each field begins with a
208594** single byte describing its type, as follows:
208595**
208596** 0x00: Undefined value.
208597** 0x01: Integer value.
208598** 0x02: Real value.
208599** 0x03: Text value.
208600** 0x04: Blob value.
208601** 0x05: SQL NULL value.
208602**
208603** Note that the above match the definitions of SQLITE_INTEGER, SQLITE_TEXT
208604** and so on in sqlite3.h. For undefined and NULL values, the field consists
208605** only of the single type byte. For other types of values, the type byte
208606** is followed by:
208607**
208608** Text values:
208609** A varint containing the number of bytes in the value (encoded using
208610** UTF-8). Followed by a buffer containing the UTF-8 representation
208611** of the text value. There is no nul terminator.
208612**
208613** Blob values:
208614** A varint containing the number of bytes in the value, followed by
208615** a buffer containing the value itself.
208616**
208617** Integer values:
208618** An 8-byte big-endian integer value.
208619**
208620** Real values:
208621** An 8-byte big-endian IEEE 754-2008 real value.
208622**
208623** Varint values are encoded in the same way as varints in the SQLite
208624** record format.
208625**
208626** CHANGESET FORMAT:
208627**
208628** A changeset is a collection of DELETE, UPDATE and INSERT operations on
208629** one or more tables. Operations on a single table are grouped together,
208630** but may occur in any order (i.e. deletes, updates and inserts are all
208631** mixed together).
208632**
208633** Each group of changes begins with a table header:
208634**
208635** 1 byte: Constant 0x54 (capital 'T')
208636** Varint: Number of columns in the table.
208637** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
208638** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
208639**
208640** Followed by one or more changes to the table.
208641**
208642** 1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09).
208643** 1 byte: The "indirect-change" flag.
208644** old.* record: (delete and update only)
208645** new.* record: (insert and update only)
208646**
208647** The "old.*" and "new.*" records, if present, are N field records in the
208648** format described above under "RECORD FORMAT", where N is the number of
208649** columns in the table. The i'th field of each record is associated with
208650** the i'th column of the table, counting from left to right in the order
208651** in which columns were declared in the CREATE TABLE statement.
208652**
208653** The new.* record that is part of each INSERT change contains the values
208654** that make up the new row. Similarly, the old.* record that is part of each
208655** DELETE change contains the values that made up the row that was deleted
208656** from the database. In the changeset format, the records that are part
208657** of INSERT or DELETE changes never contain any undefined (type byte 0x00)
208658** fields.
208659**
208660** Within the old.* record associated with an UPDATE change, all fields
208661** associated with table columns that are not PRIMARY KEY columns and are
208662** not modified by the UPDATE change are set to "undefined". Other fields
208663** are set to the values that made up the row before the UPDATE that the
208664** change records took place. Within the new.* record, fields associated
208665** with table columns modified by the UPDATE change contain the new
208666** values. Fields associated with table columns that are not modified
208667** are set to "undefined".
208668**
208669** PATCHSET FORMAT:
208670**
208671** A patchset is also a collection of changes. It is similar to a changeset,
208672** but leaves undefined those fields that are not useful if no conflict
208673** resolution is required when applying the changeset.
208674**
208675** Each group of changes begins with a table header:
208676**
208677** 1 byte: Constant 0x50 (capital 'P')
208678** Varint: Number of columns in the table.
208679** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
208680** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
208681**
208682** Followed by one or more changes to the table.
208683**
208684** 1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09).
208685** 1 byte: The "indirect-change" flag.
208686** single record: (PK fields for DELETE, PK and modified fields for UPDATE,
208687** full record for INSERT).
208688**
208689** As in the changeset format, each field of the single record that is part
208690** of a patchset change is associated with the correspondingly positioned
208691** table column, counting from left to right within the CREATE TABLE
208692** statement.
208693**
208694** For a DELETE change, all fields within the record except those associated
208695** with PRIMARY KEY columns are omitted. The PRIMARY KEY fields contain the
208696** values identifying the row to delete.
208697**
208698** For an UPDATE change, all fields except those associated with PRIMARY KEY
208699** columns and columns that are modified by the UPDATE are set to "undefined".
208700** PRIMARY KEY fields contain the values identifying the table row to update,
208701** and fields associated with modified columns contain the new column values.
208702**
208703** The records associated with INSERT changes are in the same format as for
208704** changesets. It is not possible for a record associated with an INSERT
208705** change to contain a field set to "undefined".
208706**
208707** REBASE BLOB FORMAT:
208708**
208709** A rebase blob may be output by sqlite3changeset_apply_v2() and its
208710** streaming equivalent for use with the sqlite3_rebaser APIs to rebase
208711** existing changesets. A rebase blob contains one entry for each conflict
208712** resolved using either the OMIT or REPLACE strategies within the apply_v2()
208713** call.
208714**
208715** The format used for a rebase blob is very similar to that used for
208716** changesets. All entries related to a single table are grouped together.
208717**
208718** Each group of entries begins with a table header in changeset format:
208719**
208720** 1 byte: Constant 0x54 (capital 'T')
208721** Varint: Number of columns in the table.
208722** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
208723** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
208724**
208725** Followed by one or more entries associated with the table.
208726**
208727** 1 byte: Either SQLITE_INSERT (0x12), DELETE (0x09).
208728** 1 byte: Flag. 0x01 for REPLACE, 0x00 for OMIT.
208729** record: (in the record format defined above).
208730**
208731** In a rebase blob, the first field is set to SQLITE_INSERT if the change
208732** that caused the conflict was an INSERT or UPDATE, or to SQLITE_DELETE if
208733** it was a DELETE. The second field is set to 0x01 if the conflict
208734** resolution strategy was REPLACE, or 0x00 if it was OMIT.
208735**
208736** If the change that caused the conflict was a DELETE, then the single
208737** record is a copy of the old.* record from the original changeset. If it
208738** was an INSERT, then the single record is a copy of the new.* record. If
208739** the conflicting change was an UPDATE, then the single record is a copy
208740** of the new.* record with the PK fields filled in based on the original
208741** old.* record.
208742*/
208743
208744/*
208745** For each row modified during a session, there exists a single instance of
208746** this structure stored in a SessionTable.aChange[] hash table.
208747*/
208748struct SessionChange {
208749 u8 op; /* One of UPDATE, DELETE, INSERT */
208750 u8 bIndirect; /* True if this change is "indirect" */
208751 int nMaxSize; /* Max size of eventual changeset record */
208752 int nRecord; /* Number of bytes in buffer aRecord[] */
208753 u8 *aRecord; /* Buffer containing old.* record */
208754 SessionChange *pNext; /* For hash-table collisions */
208755};
208756
208757/*
208758** Write a varint with value iVal into the buffer at aBuf. Return the
208759** number of bytes written.
208760*/
208761static int sessionVarintPut(u8 *aBuf, int iVal){
208762 return putVarint32(aBuf, iVal);
208763}
208764
208765/*
208766** Return the number of bytes required to store value iVal as a varint.
208767*/
208768static int sessionVarintLen(int iVal){
208769 return sqlite3VarintLen(iVal);
208770}
208771
208772/*
208773** Read a varint value from aBuf[] into *piVal. Return the number of
208774** bytes read.
208775*/
208776static int sessionVarintGet(u8 *aBuf, int *piVal){
208777 return getVarint32(aBuf, *piVal);
208778}
208779
208780/* Load an unaligned and unsigned 32-bit integer */
208781#define SESSION_UINT32(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
208782
208783/*
208784** Read a 64-bit big-endian integer value from buffer aRec[]. Return
208785** the value read.
208786*/
208787static sqlite3_int64 sessionGetI64(u8 *aRec){
208788 u64 x = SESSION_UINT32(aRec);
208789 u32 y = SESSION_UINT32(aRec+4);
208790 x = (x<<32) + y;
208791 return (sqlite3_int64)x;
208792}
208793
208794/*
208795** Write a 64-bit big-endian integer value to the buffer aBuf[].
208796*/
208797static void sessionPutI64(u8 *aBuf, sqlite3_int64 i){
208798 aBuf[0] = (i>>56) & 0xFF;
208799 aBuf[1] = (i>>48) & 0xFF;
208800 aBuf[2] = (i>>40) & 0xFF;
208801 aBuf[3] = (i>>32) & 0xFF;
208802 aBuf[4] = (i>>24) & 0xFF;
208803 aBuf[5] = (i>>16) & 0xFF;
208804 aBuf[6] = (i>> 8) & 0xFF;
208805 aBuf[7] = (i>> 0) & 0xFF;
208806}
208807
208808/*
208809** This function is used to serialize the contents of value pValue (see
208810** comment titled "RECORD FORMAT" above).
208811**
208812** If it is non-NULL, the serialized form of the value is written to
208813** buffer aBuf. *pnWrite is set to the number of bytes written before
208814** returning. Or, if aBuf is NULL, the only thing this function does is
208815** set *pnWrite.
208816**
208817** If no error occurs, SQLITE_OK is returned. Or, if an OOM error occurs
208818** within a call to sqlite3_value_text() (may fail if the db is utf-16))
208819** SQLITE_NOMEM is returned.
208820*/
208821static int sessionSerializeValue(
208822 u8 *aBuf, /* If non-NULL, write serialized value here */
208823 sqlite3_value *pValue, /* Value to serialize */
208824 sqlite3_int64 *pnWrite /* IN/OUT: Increment by bytes written */
208825){
208826 int nByte; /* Size of serialized value in bytes */
208827
208828 if( pValue ){
208829 int eType; /* Value type (SQLITE_NULL, TEXT etc.) */
208830
208831 eType = sqlite3_value_type(pValue);
208832 if( aBuf ) aBuf[0] = eType;
208833
208834 switch( eType ){
208835 case SQLITE_NULL:
208836 nByte = 1;
208837 break;
208838
208839 case SQLITE_INTEGER:
208840 case SQLITE_FLOAT:
208841 if( aBuf ){
208842 /* TODO: SQLite does something special to deal with mixed-endian
208843 ** floating point values (e.g. ARM7). This code probably should
208844 ** too. */
208845 u64 i;
208846 if( eType==SQLITE_INTEGER ){
208847 i = (u64)sqlite3_value_int64(pValue);
208848 }else{
208849 double r;
208850 assert( sizeof(double)==8 && sizeof(u64)==8 );
208851 r = sqlite3_value_double(pValue);
208852 memcpy(&i, &r, 8);
208853 }
208854 sessionPutI64(&aBuf[1], i);
208855 }
208856 nByte = 9;
208857 break;
208858
208859 default: {
208860 u8 *z;
208861 int n;
208862 int nVarint;
208863
208864 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
208865 if( eType==SQLITE_TEXT ){
208866 z = (u8 *)sqlite3_value_text(pValue);
208867 }else{
208868 z = (u8 *)sqlite3_value_blob(pValue);
208869 }
208870 n = sqlite3_value_bytes(pValue);
208871 if( z==0 && (eType!=SQLITE_BLOB || n>0) ) return SQLITE_NOMEM;
208872 nVarint = sessionVarintLen(n);
208873
208874 if( aBuf ){
208875 sessionVarintPut(&aBuf[1], n);
208876 if( n>0 ) memcpy(&aBuf[nVarint + 1], z, n);
208877 }
208878
208879 nByte = 1 + nVarint + n;
208880 break;
208881 }
208882 }
208883 }else{
208884 nByte = 1;
208885 if( aBuf ) aBuf[0] = '\0';
208886 }
208887
208888 if( pnWrite ) *pnWrite += nByte;
208889 return SQLITE_OK;
208890}
208891
208892/*
208893** Allocate and return a pointer to a buffer nByte bytes in size. If
208894** pSession is not NULL, increase the sqlite3_session.nMalloc variable
208895** by the number of bytes allocated.
208896*/
208897static void *sessionMalloc64(sqlite3_session *pSession, i64 nByte){
208898 void *pRet = sqlite3_malloc64(nByte);
208899 if( pSession ) pSession->nMalloc += sqlite3_msize(pRet);
208900 return pRet;
208901}
208902
208903/*
208904** Free buffer pFree, which must have been allocated by an earlier
208905** call to sessionMalloc64(). If pSession is not NULL, decrease the
208906** sqlite3_session.nMalloc counter by the number of bytes freed.
208907*/
208908static void sessionFree(sqlite3_session *pSession, void *pFree){
208909 if( pSession ) pSession->nMalloc -= sqlite3_msize(pFree);
208910 sqlite3_free(pFree);
208911}
208912
208913/*
208914** This macro is used to calculate hash key values for data structures. In
208915** order to use this macro, the entire data structure must be represented
208916** as a series of unsigned integers. In order to calculate a hash-key value
208917** for a data structure represented as three such integers, the macro may
208918** then be used as follows:
208919**
208920** int hash_key_value;
208921** hash_key_value = HASH_APPEND(0, <value 1>);
208922** hash_key_value = HASH_APPEND(hash_key_value, <value 2>);
208923** hash_key_value = HASH_APPEND(hash_key_value, <value 3>);
208924**
208925** In practice, the data structures this macro is used for are the primary
208926** key values of modified rows.
208927*/
208928#define HASH_APPEND(hash, add) ((hash) << 3) ^ (hash) ^ (unsigned int)(add)
208929
208930/*
208931** Append the hash of the 64-bit integer passed as the second argument to the
208932** hash-key value passed as the first. Return the new hash-key value.
208933*/
208934static unsigned int sessionHashAppendI64(unsigned int h, i64 i){
208935 h = HASH_APPEND(h, i & 0xFFFFFFFF);
208936 return HASH_APPEND(h, (i>>32)&0xFFFFFFFF);
208937}
208938
208939/*
208940** Append the hash of the blob passed via the second and third arguments to
208941** the hash-key value passed as the first. Return the new hash-key value.
208942*/
208943static unsigned int sessionHashAppendBlob(unsigned int h, int n, const u8 *z){
208944 int i;
208945 for(i=0; i<n; i++) h = HASH_APPEND(h, z[i]);
208946 return h;
208947}
208948
208949/*
208950** Append the hash of the data type passed as the second argument to the
208951** hash-key value passed as the first. Return the new hash-key value.
208952*/
208953static unsigned int sessionHashAppendType(unsigned int h, int eType){
208954 return HASH_APPEND(h, eType);
208955}
208956
208957/*
208958** This function may only be called from within a pre-update callback.
208959** It calculates a hash based on the primary key values of the old.* or
208960** new.* row currently available and, assuming no error occurs, writes it to
208961** *piHash before returning. If the primary key contains one or more NULL
208962** values, *pbNullPK is set to true before returning.
208963**
208964** If an error occurs, an SQLite error code is returned and the final values
208965** of *piHash asn *pbNullPK are undefined. Otherwise, SQLITE_OK is returned
208966** and the output variables are set as described above.
208967*/
208968static int sessionPreupdateHash(
208969 sqlite3_session *pSession, /* Session object that owns pTab */
208970 SessionTable *pTab, /* Session table handle */
208971 int bNew, /* True to hash the new.* PK */
208972 int *piHash, /* OUT: Hash value */
208973 int *pbNullPK /* OUT: True if there are NULL values in PK */
208974){
208975 unsigned int h = 0; /* Hash value to return */
208976 int i; /* Used to iterate through columns */
208977
208978 assert( *pbNullPK==0 );
208979 assert( pTab->nCol==pSession->hook.xCount(pSession->hook.pCtx) );
208980 for(i=0; i<pTab->nCol; i++){
208981 if( pTab->abPK[i] ){
208982 int rc;
208983 int eType;
208984 sqlite3_value *pVal;
208985
208986 if( bNew ){
208987 rc = pSession->hook.xNew(pSession->hook.pCtx, i, &pVal);
208988 }else{
208989 rc = pSession->hook.xOld(pSession->hook.pCtx, i, &pVal);
208990 }
208991 if( rc!=SQLITE_OK ) return rc;
208992
208993 eType = sqlite3_value_type(pVal);
208994 h = sessionHashAppendType(h, eType);
208995 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
208996 i64 iVal;
208997 if( eType==SQLITE_INTEGER ){
208998 iVal = sqlite3_value_int64(pVal);
208999 }else{
209000 double rVal = sqlite3_value_double(pVal);
209001 assert( sizeof(iVal)==8 && sizeof(rVal)==8 );
209002 memcpy(&iVal, &rVal, 8);
209003 }
209004 h = sessionHashAppendI64(h, iVal);
209005 }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
209006 const u8 *z;
209007 int n;
209008 if( eType==SQLITE_TEXT ){
209009 z = (const u8 *)sqlite3_value_text(pVal);
209010 }else{
209011 z = (const u8 *)sqlite3_value_blob(pVal);
209012 }
209013 n = sqlite3_value_bytes(pVal);
209014 if( !z && (eType!=SQLITE_BLOB || n>0) ) return SQLITE_NOMEM;
209015 h = sessionHashAppendBlob(h, n, z);
209016 }else{
209017 assert( eType==SQLITE_NULL );
209018 assert( pTab->bStat1==0 || i!=1 );
209019 *pbNullPK = 1;
209020 }
209021 }
209022 }
209023
209024 *piHash = (h % pTab->nChange);
209025 return SQLITE_OK;
209026}
209027
209028/*
209029** The buffer that the argument points to contains a serialized SQL value.
209030** Return the number of bytes of space occupied by the value (including
209031** the type byte).
209032*/
209033static int sessionSerialLen(u8 *a){
209034 int e = *a;
209035 int n;
209036 if( e==0 || e==0xFF ) return 1;
209037 if( e==SQLITE_NULL ) return 1;
209038 if( e==SQLITE_INTEGER || e==SQLITE_FLOAT ) return 9;
209039 return sessionVarintGet(&a[1], &n) + 1 + n;
209040}
209041
209042/*
209043** Based on the primary key values stored in change aRecord, calculate a
209044** hash key. Assume the has table has nBucket buckets. The hash keys
209045** calculated by this function are compatible with those calculated by
209046** sessionPreupdateHash().
209047**
209048** The bPkOnly argument is non-zero if the record at aRecord[] is from
209049** a patchset DELETE. In this case the non-PK fields are omitted entirely.
209050*/
209051static unsigned int sessionChangeHash(
209052 SessionTable *pTab, /* Table handle */
209053 int bPkOnly, /* Record consists of PK fields only */
209054 u8 *aRecord, /* Change record */
209055 int nBucket /* Assume this many buckets in hash table */
209056){
209057 unsigned int h = 0; /* Value to return */
209058 int i; /* Used to iterate through columns */
209059 u8 *a = aRecord; /* Used to iterate through change record */
209060
209061 for(i=0; i<pTab->nCol; i++){
209062 int eType = *a;
209063 int isPK = pTab->abPK[i];
209064 if( bPkOnly && isPK==0 ) continue;
209065
209066 /* It is not possible for eType to be SQLITE_NULL here. The session
209067 ** module does not record changes for rows with NULL values stored in
209068 ** primary key columns. */
209069 assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT
209070 || eType==SQLITE_TEXT || eType==SQLITE_BLOB
209071 || eType==SQLITE_NULL || eType==0
209072 );
209073 assert( !isPK || (eType!=0 && eType!=SQLITE_NULL) );
209074
209075 if( isPK ){
209076 a++;
209077 h = sessionHashAppendType(h, eType);
209078 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
209079 h = sessionHashAppendI64(h, sessionGetI64(a));
209080 a += 8;
209081 }else{
209082 int n;
209083 a += sessionVarintGet(a, &n);
209084 h = sessionHashAppendBlob(h, n, a);
209085 a += n;
209086 }
209087 }else{
209088 a += sessionSerialLen(a);
209089 }
209090 }
209091 return (h % nBucket);
209092}
209093
209094/*
209095** Arguments aLeft and aRight are pointers to change records for table pTab.
209096** This function returns true if the two records apply to the same row (i.e.
209097** have the same values stored in the primary key columns), or false
209098** otherwise.
209099*/
209100static int sessionChangeEqual(
209101 SessionTable *pTab, /* Table used for PK definition */
209102 int bLeftPkOnly, /* True if aLeft[] contains PK fields only */
209103 u8 *aLeft, /* Change record */
209104 int bRightPkOnly, /* True if aRight[] contains PK fields only */
209105 u8 *aRight /* Change record */
209106){
209107 u8 *a1 = aLeft; /* Cursor to iterate through aLeft */
209108 u8 *a2 = aRight; /* Cursor to iterate through aRight */
209109 int iCol; /* Used to iterate through table columns */
209110
209111 for(iCol=0; iCol<pTab->nCol; iCol++){
209112 if( pTab->abPK[iCol] ){
209113 int n1 = sessionSerialLen(a1);
209114 int n2 = sessionSerialLen(a2);
209115
209116 if( n1!=n2 || memcmp(a1, a2, n1) ){
209117 return 0;
209118 }
209119 a1 += n1;
209120 a2 += n2;
209121 }else{
209122 if( bLeftPkOnly==0 ) a1 += sessionSerialLen(a1);
209123 if( bRightPkOnly==0 ) a2 += sessionSerialLen(a2);
209124 }
209125 }
209126
209127 return 1;
209128}
209129
209130/*
209131** Arguments aLeft and aRight both point to buffers containing change
209132** records with nCol columns. This function "merges" the two records into
209133** a single records which is written to the buffer at *paOut. *paOut is
209134** then set to point to one byte after the last byte written before
209135** returning.
209136**
209137** The merging of records is done as follows: For each column, if the
209138** aRight record contains a value for the column, copy the value from
209139** their. Otherwise, if aLeft contains a value, copy it. If neither
209140** record contains a value for a given column, then neither does the
209141** output record.
209142*/
209143static void sessionMergeRecord(
209144 u8 **paOut,
209145 int nCol,
209146 u8 *aLeft,
209147 u8 *aRight
209148){
209149 u8 *a1 = aLeft; /* Cursor used to iterate through aLeft */
209150 u8 *a2 = aRight; /* Cursor used to iterate through aRight */
209151 u8 *aOut = *paOut; /* Output cursor */
209152 int iCol; /* Used to iterate from 0 to nCol */
209153
209154 for(iCol=0; iCol<nCol; iCol++){
209155 int n1 = sessionSerialLen(a1);
209156 int n2 = sessionSerialLen(a2);
209157 if( *a2 ){
209158 memcpy(aOut, a2, n2);
209159 aOut += n2;
209160 }else{
209161 memcpy(aOut, a1, n1);
209162 aOut += n1;
209163 }
209164 a1 += n1;
209165 a2 += n2;
209166 }
209167
209168 *paOut = aOut;
209169}
209170
209171/*
209172** This is a helper function used by sessionMergeUpdate().
209173**
209174** When this function is called, both *paOne and *paTwo point to a value
209175** within a change record. Before it returns, both have been advanced so
209176** as to point to the next value in the record.
209177**
209178** If, when this function is called, *paTwo points to a valid value (i.e.
209179** *paTwo[0] is not 0x00 - the "no value" placeholder), a copy of the *paTwo
209180** pointer is returned and *pnVal is set to the number of bytes in the
209181** serialized value. Otherwise, a copy of *paOne is returned and *pnVal
209182** set to the number of bytes in the value at *paOne. If *paOne points
209183** to the "no value" placeholder, *pnVal is set to 1. In other words:
209184**
209185** if( *paTwo is valid ) return *paTwo;
209186** return *paOne;
209187**
209188*/
209189static u8 *sessionMergeValue(
209190 u8 **paOne, /* IN/OUT: Left-hand buffer pointer */
209191 u8 **paTwo, /* IN/OUT: Right-hand buffer pointer */
209192 int *pnVal /* OUT: Bytes in returned value */
209193){
209194 u8 *a1 = *paOne;
209195 u8 *a2 = *paTwo;
209196 u8 *pRet = 0;
209197 int n1;
209198
209199 assert( a1 );
209200 if( a2 ){
209201 int n2 = sessionSerialLen(a2);
209202 if( *a2 ){
209203 *pnVal = n2;
209204 pRet = a2;
209205 }
209206 *paTwo = &a2[n2];
209207 }
209208
209209 n1 = sessionSerialLen(a1);
209210 if( pRet==0 ){
209211 *pnVal = n1;
209212 pRet = a1;
209213 }
209214 *paOne = &a1[n1];
209215
209216 return pRet;
209217}
209218
209219/*
209220** This function is used by changeset_concat() to merge two UPDATE changes
209221** on the same row.
209222*/
209223static int sessionMergeUpdate(
209224 u8 **paOut, /* IN/OUT: Pointer to output buffer */
209225 SessionTable *pTab, /* Table change pertains to */
209226 int bPatchset, /* True if records are patchset records */
209227 u8 *aOldRecord1, /* old.* record for first change */
209228 u8 *aOldRecord2, /* old.* record for second change */
209229 u8 *aNewRecord1, /* new.* record for first change */
209230 u8 *aNewRecord2 /* new.* record for second change */
209231){
209232 u8 *aOld1 = aOldRecord1;
209233 u8 *aOld2 = aOldRecord2;
209234 u8 *aNew1 = aNewRecord1;
209235 u8 *aNew2 = aNewRecord2;
209236
209237 u8 *aOut = *paOut;
209238 int i;
209239
209240 if( bPatchset==0 ){
209241 int bRequired = 0;
209242
209243 assert( aOldRecord1 && aNewRecord1 );
209244
209245 /* Write the old.* vector first. */
209246 for(i=0; i<pTab->nCol; i++){
209247 int nOld;
209248 u8 *aOld;
209249 int nNew;
209250 u8 *aNew;
209251
209252 aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
209253 aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
209254 if( pTab->abPK[i] || nOld!=nNew || memcmp(aOld, aNew, nNew) ){
209255 if( pTab->abPK[i]==0 ) bRequired = 1;
209256 memcpy(aOut, aOld, nOld);
209257 aOut += nOld;
209258 }else{
209259 *(aOut++) = '\0';
209260 }
209261 }
209262
209263 if( !bRequired ) return 0;
209264 }
209265
209266 /* Write the new.* vector */
209267 aOld1 = aOldRecord1;
209268 aOld2 = aOldRecord2;
209269 aNew1 = aNewRecord1;
209270 aNew2 = aNewRecord2;
209271 for(i=0; i<pTab->nCol; i++){
209272 int nOld;
209273 u8 *aOld;
209274 int nNew;
209275 u8 *aNew;
209276
209277 aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
209278 aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
209279 if( bPatchset==0
209280 && (pTab->abPK[i] || (nOld==nNew && 0==memcmp(aOld, aNew, nNew)))
209281 ){
209282 *(aOut++) = '\0';
209283 }else{
209284 memcpy(aOut, aNew, nNew);
209285 aOut += nNew;
209286 }
209287 }
209288
209289 *paOut = aOut;
209290 return 1;
209291}
209292
209293/*
209294** This function is only called from within a pre-update-hook callback.
209295** It determines if the current pre-update-hook change affects the same row
209296** as the change stored in argument pChange. If so, it returns true. Otherwise
209297** if the pre-update-hook does not affect the same row as pChange, it returns
209298** false.
209299*/
209300static int sessionPreupdateEqual(
209301 sqlite3_session *pSession, /* Session object that owns SessionTable */
209302 SessionTable *pTab, /* Table associated with change */
209303 SessionChange *pChange, /* Change to compare to */
209304 int op /* Current pre-update operation */
209305){
209306 int iCol; /* Used to iterate through columns */
209307 u8 *a = pChange->aRecord; /* Cursor used to scan change record */
209308
209309 assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE );
209310 for(iCol=0; iCol<pTab->nCol; iCol++){
209311 if( !pTab->abPK[iCol] ){
209312 a += sessionSerialLen(a);
209313 }else{
209314 sqlite3_value *pVal; /* Value returned by preupdate_new/old */
209315 int rc; /* Error code from preupdate_new/old */
209316 int eType = *a++; /* Type of value from change record */
209317
209318 /* The following calls to preupdate_new() and preupdate_old() can not
209319 ** fail. This is because they cache their return values, and by the
209320 ** time control flows to here they have already been called once from
209321 ** within sessionPreupdateHash(). The first two asserts below verify
209322 ** this (that the method has already been called). */
209323 if( op==SQLITE_INSERT ){
209324 /* assert( db->pPreUpdate->pNewUnpacked || db->pPreUpdate->aNew ); */
209325 rc = pSession->hook.xNew(pSession->hook.pCtx, iCol, &pVal);
209326 }else{
209327 /* assert( db->pPreUpdate->pUnpacked ); */
209328 rc = pSession->hook.xOld(pSession->hook.pCtx, iCol, &pVal);
209329 }
209330 assert( rc==SQLITE_OK );
209331 if( sqlite3_value_type(pVal)!=eType ) return 0;
209332
209333 /* A SessionChange object never has a NULL value in a PK column */
209334 assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT
209335 || eType==SQLITE_BLOB || eType==SQLITE_TEXT
209336 );
209337
209338 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
209339 i64 iVal = sessionGetI64(a);
209340 a += 8;
209341 if( eType==SQLITE_INTEGER ){
209342 if( sqlite3_value_int64(pVal)!=iVal ) return 0;
209343 }else{
209344 double rVal;
209345 assert( sizeof(iVal)==8 && sizeof(rVal)==8 );
209346 memcpy(&rVal, &iVal, 8);
209347 if( sqlite3_value_double(pVal)!=rVal ) return 0;
209348 }
209349 }else{
209350 int n;
209351 const u8 *z;
209352 a += sessionVarintGet(a, &n);
209353 if( sqlite3_value_bytes(pVal)!=n ) return 0;
209354 if( eType==SQLITE_TEXT ){
209355 z = sqlite3_value_text(pVal);
209356 }else{
209357 z = sqlite3_value_blob(pVal);
209358 }
209359 if( n>0 && memcmp(a, z, n) ) return 0;
209360 a += n;
209361 }
209362 }
209363 }
209364
209365 return 1;
209366}
209367
209368/*
209369** If required, grow the hash table used to store changes on table pTab
209370** (part of the session pSession). If a fatal OOM error occurs, set the
209371** session object to failed and return SQLITE_ERROR. Otherwise, return
209372** SQLITE_OK.
209373**
209374** It is possible that a non-fatal OOM error occurs in this function. In
209375** that case the hash-table does not grow, but SQLITE_OK is returned anyway.
209376** Growing the hash table in this case is a performance optimization only,
209377** it is not required for correct operation.
209378*/
209379static int sessionGrowHash(
209380 sqlite3_session *pSession, /* For memory accounting. May be NULL */
209381 int bPatchset,
209382 SessionTable *pTab
209383){
209384 if( pTab->nChange==0 || pTab->nEntry>=(pTab->nChange/2) ){
209385 int i;
209386 SessionChange **apNew;
209387 sqlite3_int64 nNew = 2*(sqlite3_int64)(pTab->nChange ? pTab->nChange : 128);
209388
209389 apNew = (SessionChange**)sessionMalloc64(
209390 pSession, sizeof(SessionChange*) * nNew
209391 );
209392 if( apNew==0 ){
209393 if( pTab->nChange==0 ){
209394 return SQLITE_ERROR;
209395 }
209396 return SQLITE_OK;
209397 }
209398 memset(apNew, 0, sizeof(SessionChange *) * nNew);
209399
209400 for(i=0; i<pTab->nChange; i++){
209401 SessionChange *p;
209402 SessionChange *pNext;
209403 for(p=pTab->apChange[i]; p; p=pNext){
209404 int bPkOnly = (p->op==SQLITE_DELETE && bPatchset);
209405 int iHash = sessionChangeHash(pTab, bPkOnly, p->aRecord, nNew);
209406 pNext = p->pNext;
209407 p->pNext = apNew[iHash];
209408 apNew[iHash] = p;
209409 }
209410 }
209411
209412 sessionFree(pSession, pTab->apChange);
209413 pTab->nChange = nNew;
209414 pTab->apChange = apNew;
209415 }
209416
209417 return SQLITE_OK;
209418}
209419
209420/*
209421** This function queries the database for the names of the columns of table
209422** zThis, in schema zDb.
209423**
209424** Otherwise, if they are not NULL, variable *pnCol is set to the number
209425** of columns in the database table and variable *pzTab is set to point to a
209426** nul-terminated copy of the table name. *pazCol (if not NULL) is set to
209427** point to an array of pointers to column names. And *pabPK (again, if not
209428** NULL) is set to point to an array of booleans - true if the corresponding
209429** column is part of the primary key.
209430**
209431** For example, if the table is declared as:
209432**
209433** CREATE TABLE tbl1(w, x, y, z, PRIMARY KEY(w, z));
209434**
209435** Then the four output variables are populated as follows:
209436**
209437** *pnCol = 4
209438** *pzTab = "tbl1"
209439** *pazCol = {"w", "x", "y", "z"}
209440** *pabPK = {1, 0, 0, 1}
209441**
209442** All returned buffers are part of the same single allocation, which must
209443** be freed using sqlite3_free() by the caller
209444*/
209445static int sessionTableInfo(
209446 sqlite3_session *pSession, /* For memory accounting. May be NULL */
209447 sqlite3 *db, /* Database connection */
209448 const char *zDb, /* Name of attached database (e.g. "main") */
209449 const char *zThis, /* Table name */
209450 int *pnCol, /* OUT: number of columns */
209451 const char **pzTab, /* OUT: Copy of zThis */
209452 const char ***pazCol, /* OUT: Array of column names for table */
209453 u8 **pabPK /* OUT: Array of booleans - true for PK col */
209454){
209455 char *zPragma;
209456 sqlite3_stmt *pStmt;
209457 int rc;
209458 sqlite3_int64 nByte;
209459 int nDbCol = 0;
209460 int nThis;
209461 int i;
209462 u8 *pAlloc = 0;
209463 char **azCol = 0;
209464 u8 *abPK = 0;
209465
209466 assert( pazCol && pabPK );
209467
209468 nThis = sqlite3Strlen30(zThis);
209469 if( nThis==12 && 0==sqlite3_stricmp("sqlite_stat1", zThis) ){
209470 rc = sqlite3_table_column_metadata(db, zDb, zThis, 0, 0, 0, 0, 0, 0);
209471 if( rc==SQLITE_OK ){
209472 /* For sqlite_stat1, pretend that (tbl,idx) is the PRIMARY KEY. */
209473 zPragma = sqlite3_mprintf(
209474 "SELECT 0, 'tbl', '', 0, '', 1 UNION ALL "
209475 "SELECT 1, 'idx', '', 0, '', 2 UNION ALL "
209476 "SELECT 2, 'stat', '', 0, '', 0"
209477 );
209478 }else if( rc==SQLITE_ERROR ){
209479 zPragma = sqlite3_mprintf("");
209480 }else{
209481 *pazCol = 0;
209482 *pabPK = 0;
209483 *pnCol = 0;
209484 if( pzTab ) *pzTab = 0;
209485 return rc;
209486 }
209487 }else{
209488 zPragma = sqlite3_mprintf("PRAGMA '%q'.table_info('%q')", zDb, zThis);
209489 }
209490 if( !zPragma ){
209491 *pazCol = 0;
209492 *pabPK = 0;
209493 *pnCol = 0;
209494 if( pzTab ) *pzTab = 0;
209495 return SQLITE_NOMEM;
209496 }
209497
209498 rc = sqlite3_prepare_v2(db, zPragma, -1, &pStmt, 0);
209499 sqlite3_free(zPragma);
209500 if( rc!=SQLITE_OK ){
209501 *pazCol = 0;
209502 *pabPK = 0;
209503 *pnCol = 0;
209504 if( pzTab ) *pzTab = 0;
209505 return rc;
209506 }
209507
209508 nByte = nThis + 1;
209509 while( SQLITE_ROW==sqlite3_step(pStmt) ){
209510 nByte += sqlite3_column_bytes(pStmt, 1);
209511 nDbCol++;
209512 }
209513 rc = sqlite3_reset(pStmt);
209514
209515 if( rc==SQLITE_OK ){
209516 nByte += nDbCol * (sizeof(const char *) + sizeof(u8) + 1);
209517 pAlloc = sessionMalloc64(pSession, nByte);
209518 if( pAlloc==0 ){
209519 rc = SQLITE_NOMEM;
209520 }
209521 }
209522 if( rc==SQLITE_OK ){
209523 azCol = (char **)pAlloc;
209524 pAlloc = (u8 *)&azCol[nDbCol];
209525 abPK = (u8 *)pAlloc;
209526 pAlloc = &abPK[nDbCol];
209527 if( pzTab ){
209528 memcpy(pAlloc, zThis, nThis+1);
209529 *pzTab = (char *)pAlloc;
209530 pAlloc += nThis+1;
209531 }
209532
209533 i = 0;
209534 while( SQLITE_ROW==sqlite3_step(pStmt) ){
209535 int nName = sqlite3_column_bytes(pStmt, 1);
209536 const unsigned char *zName = sqlite3_column_text(pStmt, 1);
209537 if( zName==0 ) break;
209538 memcpy(pAlloc, zName, nName+1);
209539 azCol[i] = (char *)pAlloc;
209540 pAlloc += nName+1;
209541 abPK[i] = sqlite3_column_int(pStmt, 5);
209542 i++;
209543 }
209544 rc = sqlite3_reset(pStmt);
209545
209546 }
209547
209548 /* If successful, populate the output variables. Otherwise, zero them and
209549 ** free any allocation made. An error code will be returned in this case.
209550 */
209551 if( rc==SQLITE_OK ){
209552 *pazCol = (const char **)azCol;
209553 *pabPK = abPK;
209554 *pnCol = nDbCol;
209555 }else{
209556 *pazCol = 0;
209557 *pabPK = 0;
209558 *pnCol = 0;
209559 if( pzTab ) *pzTab = 0;
209560 sessionFree(pSession, azCol);
209561 }
209562 sqlite3_finalize(pStmt);
209563 return rc;
209564}
209565
209566/*
209567** This function is only called from within a pre-update handler for a
209568** write to table pTab, part of session pSession. If this is the first
209569** write to this table, initalize the SessionTable.nCol, azCol[] and
209570** abPK[] arrays accordingly.
209571**
209572** If an error occurs, an error code is stored in sqlite3_session.rc and
209573** non-zero returned. Or, if no error occurs but the table has no primary
209574** key, sqlite3_session.rc is left set to SQLITE_OK and non-zero returned to
209575** indicate that updates on this table should be ignored. SessionTable.abPK
209576** is set to NULL in this case.
209577*/
209578static int sessionInitTable(sqlite3_session *pSession, SessionTable *pTab){
209579 if( pTab->nCol==0 ){
209580 u8 *abPK;
209581 assert( pTab->azCol==0 || pTab->abPK==0 );
209582 pSession->rc = sessionTableInfo(pSession, pSession->db, pSession->zDb,
209583 pTab->zName, &pTab->nCol, 0, &pTab->azCol, &abPK
209584 );
209585 if( pSession->rc==SQLITE_OK ){
209586 int i;
209587 for(i=0; i<pTab->nCol; i++){
209588 if( abPK[i] ){
209589 pTab->abPK = abPK;
209590 break;
209591 }
209592 }
209593 if( 0==sqlite3_stricmp("sqlite_stat1", pTab->zName) ){
209594 pTab->bStat1 = 1;
209595 }
209596
209597 if( pSession->bEnableSize ){
209598 pSession->nMaxChangesetSize += (
209599 1 + sessionVarintLen(pTab->nCol) + pTab->nCol + strlen(pTab->zName)+1
209600 );
209601 }
209602 }
209603 }
209604 return (pSession->rc || pTab->abPK==0);
209605}
209606
209607/*
209608** Versions of the four methods in object SessionHook for use with the
209609** sqlite_stat1 table. The purpose of this is to substitute a zero-length
209610** blob each time a NULL value is read from the "idx" column of the
209611** sqlite_stat1 table.
209612*/
209613typedef struct SessionStat1Ctx SessionStat1Ctx;
209614struct SessionStat1Ctx {
209615 SessionHook hook;
209616 sqlite3_session *pSession;
209617};
209618static int sessionStat1Old(void *pCtx, int iCol, sqlite3_value **ppVal){
209619 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
209620 sqlite3_value *pVal = 0;
209621 int rc = p->hook.xOld(p->hook.pCtx, iCol, &pVal);
209622 if( rc==SQLITE_OK && iCol==1 && sqlite3_value_type(pVal)==SQLITE_NULL ){
209623 pVal = p->pSession->pZeroBlob;
209624 }
209625 *ppVal = pVal;
209626 return rc;
209627}
209628static int sessionStat1New(void *pCtx, int iCol, sqlite3_value **ppVal){
209629 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
209630 sqlite3_value *pVal = 0;
209631 int rc = p->hook.xNew(p->hook.pCtx, iCol, &pVal);
209632 if( rc==SQLITE_OK && iCol==1 && sqlite3_value_type(pVal)==SQLITE_NULL ){
209633 pVal = p->pSession->pZeroBlob;
209634 }
209635 *ppVal = pVal;
209636 return rc;
209637}
209638static int sessionStat1Count(void *pCtx){
209639 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
209640 return p->hook.xCount(p->hook.pCtx);
209641}
209642static int sessionStat1Depth(void *pCtx){
209643 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
209644 return p->hook.xDepth(p->hook.pCtx);
209645}
209646
209647static int sessionUpdateMaxSize(
209648 int op,
209649 sqlite3_session *pSession, /* Session object pTab is attached to */
209650 SessionTable *pTab, /* Table that change applies to */
209651 SessionChange *pC /* Update pC->nMaxSize */
209652){
209653 i64 nNew = 2;
209654 if( pC->op==SQLITE_INSERT ){
209655 if( op!=SQLITE_DELETE ){
209656 int ii;
209657 for(ii=0; ii<pTab->nCol; ii++){
209658 sqlite3_value *p = 0;
209659 pSession->hook.xNew(pSession->hook.pCtx, ii, &p);
209660 sessionSerializeValue(0, p, &nNew);
209661 }
209662 }
209663 }else if( op==SQLITE_DELETE ){
209664 nNew += pC->nRecord;
209665 if( sqlite3_preupdate_blobwrite(pSession->db)>=0 ){
209666 nNew += pC->nRecord;
209667 }
209668 }else{
209669 int ii;
209670 u8 *pCsr = pC->aRecord;
209671 for(ii=0; ii<pTab->nCol; ii++){
209672 int bChanged = 1;
209673 int nOld = 0;
209674 int eType;
209675 sqlite3_value *p = 0;
209676 pSession->hook.xNew(pSession->hook.pCtx, ii, &p);
209677 if( p==0 ){
209678 return SQLITE_NOMEM;
209679 }
209680
209681 eType = *pCsr++;
209682 switch( eType ){
209683 case SQLITE_NULL:
209684 bChanged = sqlite3_value_type(p)!=SQLITE_NULL;
209685 break;
209686
209687 case SQLITE_FLOAT:
209688 case SQLITE_INTEGER: {
209689 if( eType==sqlite3_value_type(p) ){
209690 sqlite3_int64 iVal = sessionGetI64(pCsr);
209691 if( eType==SQLITE_INTEGER ){
209692 bChanged = (iVal!=sqlite3_value_int64(p));
209693 }else{
209694 double dVal;
209695 memcpy(&dVal, &iVal, 8);
209696 bChanged = (dVal!=sqlite3_value_double(p));
209697 }
209698 }
209699 nOld = 8;
209700 pCsr += 8;
209701 break;
209702 }
209703
209704 default: {
209705 int nByte;
209706 nOld = sessionVarintGet(pCsr, &nByte);
209707 pCsr += nOld;
209708 nOld += nByte;
209709 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
209710 if( eType==sqlite3_value_type(p)
209711 && nByte==sqlite3_value_bytes(p)
209712 && (nByte==0 || 0==memcmp(pCsr, sqlite3_value_blob(p), nByte))
209713 ){
209714 bChanged = 0;
209715 }
209716 pCsr += nByte;
209717 break;
209718 }
209719 }
209720
209721 if( bChanged && pTab->abPK[ii] ){
209722 nNew = pC->nRecord + 2;
209723 break;
209724 }
209725
209726 if( bChanged ){
209727 nNew += 1 + nOld;
209728 sessionSerializeValue(0, p, &nNew);
209729 }else if( pTab->abPK[ii] ){
209730 nNew += 2 + nOld;
209731 }else{
209732 nNew += 2;
209733 }
209734 }
209735 }
209736
209737 if( nNew>pC->nMaxSize ){
209738 int nIncr = nNew - pC->nMaxSize;
209739 pC->nMaxSize = nNew;
209740 pSession->nMaxChangesetSize += nIncr;
209741 }
209742 return SQLITE_OK;
209743}
209744
209745/*
209746** This function is only called from with a pre-update-hook reporting a
209747** change on table pTab (attached to session pSession). The type of change
209748** (UPDATE, INSERT, DELETE) is specified by the first argument.
209749**
209750** Unless one is already present or an error occurs, an entry is added
209751** to the changed-rows hash table associated with table pTab.
209752*/
209753static void sessionPreupdateOneChange(
209754 int op, /* One of SQLITE_UPDATE, INSERT, DELETE */
209755 sqlite3_session *pSession, /* Session object pTab is attached to */
209756 SessionTable *pTab /* Table that change applies to */
209757){
209758 int iHash;
209759 int bNull = 0;
209760 int rc = SQLITE_OK;
209761 SessionStat1Ctx stat1 = {{0,0,0,0,0},0};
209762
209763 if( pSession->rc ) return;
209764
209765 /* Load table details if required */
209766 if( sessionInitTable(pSession, pTab) ) return;
209767
209768 /* Check the number of columns in this xPreUpdate call matches the
209769 ** number of columns in the table. */
209770 if( pTab->nCol!=pSession->hook.xCount(pSession->hook.pCtx) ){
209771 pSession->rc = SQLITE_SCHEMA;
209772 return;
209773 }
209774
209775 /* Grow the hash table if required */
209776 if( sessionGrowHash(pSession, 0, pTab) ){
209777 pSession->rc = SQLITE_NOMEM;
209778 return;
209779 }
209780
209781 if( pTab->bStat1 ){
209782 stat1.hook = pSession->hook;
209783 stat1.pSession = pSession;
209784 pSession->hook.pCtx = (void*)&stat1;
209785 pSession->hook.xNew = sessionStat1New;
209786 pSession->hook.xOld = sessionStat1Old;
209787 pSession->hook.xCount = sessionStat1Count;
209788 pSession->hook.xDepth = sessionStat1Depth;
209789 if( pSession->pZeroBlob==0 ){
209790 sqlite3_value *p = sqlite3ValueNew(0);
209791 if( p==0 ){
209792 rc = SQLITE_NOMEM;
209793 goto error_out;
209794 }
209795 sqlite3ValueSetStr(p, 0, "", 0, SQLITE_STATIC);
209796 pSession->pZeroBlob = p;
209797 }
209798 }
209799
209800 /* Calculate the hash-key for this change. If the primary key of the row
209801 ** includes a NULL value, exit early. Such changes are ignored by the
209802 ** session module. */
209803 rc = sessionPreupdateHash(pSession, pTab, op==SQLITE_INSERT, &iHash, &bNull);
209804 if( rc!=SQLITE_OK ) goto error_out;
209805
209806 if( bNull==0 ){
209807 /* Search the hash table for an existing record for this row. */
209808 SessionChange *pC;
209809 for(pC=pTab->apChange[iHash]; pC; pC=pC->pNext){
209810 if( sessionPreupdateEqual(pSession, pTab, pC, op) ) break;
209811 }
209812
209813 if( pC==0 ){
209814 /* Create a new change object containing all the old values (if
209815 ** this is an SQLITE_UPDATE or SQLITE_DELETE), or just the PK
209816 ** values (if this is an INSERT). */
209817 sqlite3_int64 nByte; /* Number of bytes to allocate */
209818 int i; /* Used to iterate through columns */
209819
209820 assert( rc==SQLITE_OK );
209821 pTab->nEntry++;
209822
209823 /* Figure out how large an allocation is required */
209824 nByte = sizeof(SessionChange);
209825 for(i=0; i<pTab->nCol; i++){
209826 sqlite3_value *p = 0;
209827 if( op!=SQLITE_INSERT ){
209828 TESTONLY(int trc = ) pSession->hook.xOld(pSession->hook.pCtx, i, &p);
209829 assert( trc==SQLITE_OK );
209830 }else if( pTab->abPK[i] ){
209831 TESTONLY(int trc = ) pSession->hook.xNew(pSession->hook.pCtx, i, &p);
209832 assert( trc==SQLITE_OK );
209833 }
209834
209835 /* This may fail if SQLite value p contains a utf-16 string that must
209836 ** be converted to utf-8 and an OOM error occurs while doing so. */
209837 rc = sessionSerializeValue(0, p, &nByte);
209838 if( rc!=SQLITE_OK ) goto error_out;
209839 }
209840
209841 /* Allocate the change object */
209842 pC = (SessionChange *)sessionMalloc64(pSession, nByte);
209843 if( !pC ){
209844 rc = SQLITE_NOMEM;
209845 goto error_out;
209846 }else{
209847 memset(pC, 0, sizeof(SessionChange));
209848 pC->aRecord = (u8 *)&pC[1];
209849 }
209850
209851 /* Populate the change object. None of the preupdate_old(),
209852 ** preupdate_new() or SerializeValue() calls below may fail as all
209853 ** required values and encodings have already been cached in memory.
209854 ** It is not possible for an OOM to occur in this block. */
209855 nByte = 0;
209856 for(i=0; i<pTab->nCol; i++){
209857 sqlite3_value *p = 0;
209858 if( op!=SQLITE_INSERT ){
209859 pSession->hook.xOld(pSession->hook.pCtx, i, &p);
209860 }else if( pTab->abPK[i] ){
209861 pSession->hook.xNew(pSession->hook.pCtx, i, &p);
209862 }
209863 sessionSerializeValue(&pC->aRecord[nByte], p, &nByte);
209864 }
209865
209866 /* Add the change to the hash-table */
209867 if( pSession->bIndirect || pSession->hook.xDepth(pSession->hook.pCtx) ){
209868 pC->bIndirect = 1;
209869 }
209870 pC->nRecord = nByte;
209871 pC->op = op;
209872 pC->pNext = pTab->apChange[iHash];
209873 pTab->apChange[iHash] = pC;
209874
209875 }else if( pC->bIndirect ){
209876 /* If the existing change is considered "indirect", but this current
209877 ** change is "direct", mark the change object as direct. */
209878 if( pSession->hook.xDepth(pSession->hook.pCtx)==0
209879 && pSession->bIndirect==0
209880 ){
209881 pC->bIndirect = 0;
209882 }
209883 }
209884
209885 assert( rc==SQLITE_OK );
209886 if( pSession->bEnableSize ){
209887 rc = sessionUpdateMaxSize(op, pSession, pTab, pC);
209888 }
209889 }
209890
209891
209892 /* If an error has occurred, mark the session object as failed. */
209893 error_out:
209894 if( pTab->bStat1 ){
209895 pSession->hook = stat1.hook;
209896 }
209897 if( rc!=SQLITE_OK ){
209898 pSession->rc = rc;
209899 }
209900}
209901
209902static int sessionFindTable(
209903 sqlite3_session *pSession,
209904 const char *zName,
209905 SessionTable **ppTab
209906){
209907 int rc = SQLITE_OK;
209908 int nName = sqlite3Strlen30(zName);
209909 SessionTable *pRet;
209910
209911 /* Search for an existing table */
209912 for(pRet=pSession->pTable; pRet; pRet=pRet->pNext){
209913 if( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) ) break;
209914 }
209915
209916 if( pRet==0 && pSession->bAutoAttach ){
209917 /* If there is a table-filter configured, invoke it. If it returns 0,
209918 ** do not automatically add the new table. */
209919 if( pSession->xTableFilter==0
209920 || pSession->xTableFilter(pSession->pFilterCtx, zName)
209921 ){
209922 rc = sqlite3session_attach(pSession, zName);
209923 if( rc==SQLITE_OK ){
209924 pRet = pSession->pTable;
209925 while( ALWAYS(pRet) && pRet->pNext ){
209926 pRet = pRet->pNext;
209927 }
209928 assert( pRet!=0 );
209929 assert( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) );
209930 }
209931 }
209932 }
209933
209934 assert( rc==SQLITE_OK || pRet==0 );
209935 *ppTab = pRet;
209936 return rc;
209937}
209938
209939/*
209940** The 'pre-update' hook registered by this module with SQLite databases.
209941*/
209942static void xPreUpdate(
209943 void *pCtx, /* Copy of third arg to preupdate_hook() */
209944 sqlite3 *db, /* Database handle */
209945 int op, /* SQLITE_UPDATE, DELETE or INSERT */
209946 char const *zDb, /* Database name */
209947 char const *zName, /* Table name */
209948 sqlite3_int64 iKey1, /* Rowid of row about to be deleted/updated */
209949 sqlite3_int64 iKey2 /* New rowid value (for a rowid UPDATE) */
209950){
209951 sqlite3_session *pSession;
209952 int nDb = sqlite3Strlen30(zDb);
209953
209954 assert( sqlite3_mutex_held(db->mutex) );
209955
209956 for(pSession=(sqlite3_session *)pCtx; pSession; pSession=pSession->pNext){
209957 SessionTable *pTab;
209958
209959 /* If this session is attached to a different database ("main", "temp"
209960 ** etc.), or if it is not currently enabled, there is nothing to do. Skip
209961 ** to the next session object attached to this database. */
209962 if( pSession->bEnable==0 ) continue;
209963 if( pSession->rc ) continue;
209964 if( sqlite3_strnicmp(zDb, pSession->zDb, nDb+1) ) continue;
209965
209966 pSession->rc = sessionFindTable(pSession, zName, &pTab);
209967 if( pTab ){
209968 assert( pSession->rc==SQLITE_OK );
209969 sessionPreupdateOneChange(op, pSession, pTab);
209970 if( op==SQLITE_UPDATE ){
209971 sessionPreupdateOneChange(SQLITE_INSERT, pSession, pTab);
209972 }
209973 }
209974 }
209975}
209976
209977/*
209978** The pre-update hook implementations.
209979*/
209980static int sessionPreupdateOld(void *pCtx, int iVal, sqlite3_value **ppVal){
209981 return sqlite3_preupdate_old((sqlite3*)pCtx, iVal, ppVal);
209982}
209983static int sessionPreupdateNew(void *pCtx, int iVal, sqlite3_value **ppVal){
209984 return sqlite3_preupdate_new((sqlite3*)pCtx, iVal, ppVal);
209985}
209986static int sessionPreupdateCount(void *pCtx){
209987 return sqlite3_preupdate_count((sqlite3*)pCtx);
209988}
209989static int sessionPreupdateDepth(void *pCtx){
209990 return sqlite3_preupdate_depth((sqlite3*)pCtx);
209991}
209992
209993/*
209994** Install the pre-update hooks on the session object passed as the only
209995** argument.
209996*/
209997static void sessionPreupdateHooks(
209998 sqlite3_session *pSession
209999){
210000 pSession->hook.pCtx = (void*)pSession->db;
210001 pSession->hook.xOld = sessionPreupdateOld;
210002 pSession->hook.xNew = sessionPreupdateNew;
210003 pSession->hook.xCount = sessionPreupdateCount;
210004 pSession->hook.xDepth = sessionPreupdateDepth;
210005}
210006
210007typedef struct SessionDiffCtx SessionDiffCtx;
210008struct SessionDiffCtx {
210009 sqlite3_stmt *pStmt;
210010 int nOldOff;
210011};
210012
210013/*
210014** The diff hook implementations.
210015*/
210016static int sessionDiffOld(void *pCtx, int iVal, sqlite3_value **ppVal){
210017 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
210018 *ppVal = sqlite3_column_value(p->pStmt, iVal+p->nOldOff);
210019 return SQLITE_OK;
210020}
210021static int sessionDiffNew(void *pCtx, int iVal, sqlite3_value **ppVal){
210022 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
210023 *ppVal = sqlite3_column_value(p->pStmt, iVal);
210024 return SQLITE_OK;
210025}
210026static int sessionDiffCount(void *pCtx){
210027 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
210028 return p->nOldOff ? p->nOldOff : sqlite3_column_count(p->pStmt);
210029}
210030static int sessionDiffDepth(void *pCtx){
210031 return 0;
210032}
210033
210034/*
210035** Install the diff hooks on the session object passed as the only
210036** argument.
210037*/
210038static void sessionDiffHooks(
210039 sqlite3_session *pSession,
210040 SessionDiffCtx *pDiffCtx
210041){
210042 pSession->hook.pCtx = (void*)pDiffCtx;
210043 pSession->hook.xOld = sessionDiffOld;
210044 pSession->hook.xNew = sessionDiffNew;
210045 pSession->hook.xCount = sessionDiffCount;
210046 pSession->hook.xDepth = sessionDiffDepth;
210047}
210048
210049static char *sessionExprComparePK(
210050 int nCol,
210051 const char *zDb1, const char *zDb2,
210052 const char *zTab,
210053 const char **azCol, u8 *abPK
210054){
210055 int i;
210056 const char *zSep = "";
210057 char *zRet = 0;
210058
210059 for(i=0; i<nCol; i++){
210060 if( abPK[i] ){
210061 zRet = sqlite3_mprintf("%z%s\"%w\".\"%w\".\"%w\"=\"%w\".\"%w\".\"%w\"",
210062 zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
210063 );
210064 zSep = " AND ";
210065 if( zRet==0 ) break;
210066 }
210067 }
210068
210069 return zRet;
210070}
210071
210072static char *sessionExprCompareOther(
210073 int nCol,
210074 const char *zDb1, const char *zDb2,
210075 const char *zTab,
210076 const char **azCol, u8 *abPK
210077){
210078 int i;
210079 const char *zSep = "";
210080 char *zRet = 0;
210081 int bHave = 0;
210082
210083 for(i=0; i<nCol; i++){
210084 if( abPK[i]==0 ){
210085 bHave = 1;
210086 zRet = sqlite3_mprintf(
210087 "%z%s\"%w\".\"%w\".\"%w\" IS NOT \"%w\".\"%w\".\"%w\"",
210088 zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
210089 );
210090 zSep = " OR ";
210091 if( zRet==0 ) break;
210092 }
210093 }
210094
210095 if( bHave==0 ){
210096 assert( zRet==0 );
210097 zRet = sqlite3_mprintf("0");
210098 }
210099
210100 return zRet;
210101}
210102
210103static char *sessionSelectFindNew(
210104 int nCol,
210105 const char *zDb1, /* Pick rows in this db only */
210106 const char *zDb2, /* But not in this one */
210107 const char *zTbl, /* Table name */
210108 const char *zExpr
210109){
210110 char *zRet = sqlite3_mprintf(
210111 "SELECT * FROM \"%w\".\"%w\" WHERE NOT EXISTS ("
210112 " SELECT 1 FROM \"%w\".\"%w\" WHERE %s"
210113 ")",
210114 zDb1, zTbl, zDb2, zTbl, zExpr
210115 );
210116 return zRet;
210117}
210118
210119static int sessionDiffFindNew(
210120 int op,
210121 sqlite3_session *pSession,
210122 SessionTable *pTab,
210123 const char *zDb1,
210124 const char *zDb2,
210125 char *zExpr
210126){
210127 int rc = SQLITE_OK;
210128 char *zStmt = sessionSelectFindNew(pTab->nCol, zDb1, zDb2, pTab->zName,zExpr);
210129
210130 if( zStmt==0 ){
210131 rc = SQLITE_NOMEM;
210132 }else{
210133 sqlite3_stmt *pStmt;
210134 rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
210135 if( rc==SQLITE_OK ){
210136 SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
210137 pDiffCtx->pStmt = pStmt;
210138 pDiffCtx->nOldOff = 0;
210139 while( SQLITE_ROW==sqlite3_step(pStmt) ){
210140 sessionPreupdateOneChange(op, pSession, pTab);
210141 }
210142 rc = sqlite3_finalize(pStmt);
210143 }
210144 sqlite3_free(zStmt);
210145 }
210146
210147 return rc;
210148}
210149
210150static int sessionDiffFindModified(
210151 sqlite3_session *pSession,
210152 SessionTable *pTab,
210153 const char *zFrom,
210154 const char *zExpr
210155){
210156 int rc = SQLITE_OK;
210157
210158 char *zExpr2 = sessionExprCompareOther(pTab->nCol,
210159 pSession->zDb, zFrom, pTab->zName, pTab->azCol, pTab->abPK
210160 );
210161 if( zExpr2==0 ){
210162 rc = SQLITE_NOMEM;
210163 }else{
210164 char *zStmt = sqlite3_mprintf(
210165 "SELECT * FROM \"%w\".\"%w\", \"%w\".\"%w\" WHERE %s AND (%z)",
210166 pSession->zDb, pTab->zName, zFrom, pTab->zName, zExpr, zExpr2
210167 );
210168 if( zStmt==0 ){
210169 rc = SQLITE_NOMEM;
210170 }else{
210171 sqlite3_stmt *pStmt;
210172 rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
210173
210174 if( rc==SQLITE_OK ){
210175 SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
210176 pDiffCtx->pStmt = pStmt;
210177 pDiffCtx->nOldOff = pTab->nCol;
210178 while( SQLITE_ROW==sqlite3_step(pStmt) ){
210179 sessionPreupdateOneChange(SQLITE_UPDATE, pSession, pTab);
210180 }
210181 rc = sqlite3_finalize(pStmt);
210182 }
210183 sqlite3_free(zStmt);
210184 }
210185 }
210186
210187 return rc;
210188}
210189
210190SQLITE_API int sqlite3session_diff(
210191 sqlite3_session *pSession,
210192 const char *zFrom,
210193 const char *zTbl,
210194 char **pzErrMsg
210195){
210196 const char *zDb = pSession->zDb;
210197 int rc = pSession->rc;
210198 SessionDiffCtx d;
210199
210200 memset(&d, 0, sizeof(d));
210201 sessionDiffHooks(pSession, &d);
210202
210203 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
210204 if( pzErrMsg ) *pzErrMsg = 0;
210205 if( rc==SQLITE_OK ){
210206 char *zExpr = 0;
210207 sqlite3 *db = pSession->db;
210208 SessionTable *pTo; /* Table zTbl */
210209
210210 /* Locate and if necessary initialize the target table object */
210211 rc = sessionFindTable(pSession, zTbl, &pTo);
210212 if( pTo==0 ) goto diff_out;
210213 if( sessionInitTable(pSession, pTo) ){
210214 rc = pSession->rc;
210215 goto diff_out;
210216 }
210217
210218 /* Check the table schemas match */
210219 if( rc==SQLITE_OK ){
210220 int bHasPk = 0;
210221 int bMismatch = 0;
210222 int nCol; /* Columns in zFrom.zTbl */
210223 u8 *abPK;
210224 const char **azCol = 0;
210225 rc = sessionTableInfo(0, db, zFrom, zTbl, &nCol, 0, &azCol, &abPK);
210226 if( rc==SQLITE_OK ){
210227 if( pTo->nCol!=nCol ){
210228 bMismatch = 1;
210229 }else{
210230 int i;
210231 for(i=0; i<nCol; i++){
210232 if( pTo->abPK[i]!=abPK[i] ) bMismatch = 1;
210233 if( sqlite3_stricmp(azCol[i], pTo->azCol[i]) ) bMismatch = 1;
210234 if( abPK[i] ) bHasPk = 1;
210235 }
210236 }
210237 }
210238 sqlite3_free((char*)azCol);
210239 if( bMismatch ){
210240 if( pzErrMsg ){
210241 *pzErrMsg = sqlite3_mprintf("table schemas do not match");
210242 }
210243 rc = SQLITE_SCHEMA;
210244 }
210245 if( bHasPk==0 ){
210246 /* Ignore tables with no primary keys */
210247 goto diff_out;
210248 }
210249 }
210250
210251 if( rc==SQLITE_OK ){
210252 zExpr = sessionExprComparePK(pTo->nCol,
210253 zDb, zFrom, pTo->zName, pTo->azCol, pTo->abPK
210254 );
210255 }
210256
210257 /* Find new rows */
210258 if( rc==SQLITE_OK ){
210259 rc = sessionDiffFindNew(SQLITE_INSERT, pSession, pTo, zDb, zFrom, zExpr);
210260 }
210261
210262 /* Find old rows */
210263 if( rc==SQLITE_OK ){
210264 rc = sessionDiffFindNew(SQLITE_DELETE, pSession, pTo, zFrom, zDb, zExpr);
210265 }
210266
210267 /* Find modified rows */
210268 if( rc==SQLITE_OK ){
210269 rc = sessionDiffFindModified(pSession, pTo, zFrom, zExpr);
210270 }
210271
210272 sqlite3_free(zExpr);
210273 }
210274
210275 diff_out:
210276 sessionPreupdateHooks(pSession);
210277 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
210278 return rc;
210279}
210280
210281/*
210282** Create a session object. This session object will record changes to
210283** database zDb attached to connection db.
210284*/
210285SQLITE_API int sqlite3session_create(
210286 sqlite3 *db, /* Database handle */
210287 const char *zDb, /* Name of db (e.g. "main") */
210288 sqlite3_session **ppSession /* OUT: New session object */
210289){
210290 sqlite3_session *pNew; /* Newly allocated session object */
210291 sqlite3_session *pOld; /* Session object already attached to db */
210292 int nDb = sqlite3Strlen30(zDb); /* Length of zDb in bytes */
210293
210294 /* Zero the output value in case an error occurs. */
210295 *ppSession = 0;
210296
210297 /* Allocate and populate the new session object. */
210298 pNew = (sqlite3_session *)sqlite3_malloc64(sizeof(sqlite3_session) + nDb + 1);
210299 if( !pNew ) return SQLITE_NOMEM;
210300 memset(pNew, 0, sizeof(sqlite3_session));
210301 pNew->db = db;
210302 pNew->zDb = (char *)&pNew[1];
210303 pNew->bEnable = 1;
210304 memcpy(pNew->zDb, zDb, nDb+1);
210305 sessionPreupdateHooks(pNew);
210306
210307 /* Add the new session object to the linked list of session objects
210308 ** attached to database handle $db. Do this under the cover of the db
210309 ** handle mutex. */
210310 sqlite3_mutex_enter(sqlite3_db_mutex(db));
210311 pOld = (sqlite3_session*)sqlite3_preupdate_hook(db, xPreUpdate, (void*)pNew);
210312 pNew->pNext = pOld;
210313 sqlite3_mutex_leave(sqlite3_db_mutex(db));
210314
210315 *ppSession = pNew;
210316 return SQLITE_OK;
210317}
210318
210319/*
210320** Free the list of table objects passed as the first argument. The contents
210321** of the changed-rows hash tables are also deleted.
210322*/
210323static void sessionDeleteTable(sqlite3_session *pSession, SessionTable *pList){
210324 SessionTable *pNext;
210325 SessionTable *pTab;
210326
210327 for(pTab=pList; pTab; pTab=pNext){
210328 int i;
210329 pNext = pTab->pNext;
210330 for(i=0; i<pTab->nChange; i++){
210331 SessionChange *p;
210332 SessionChange *pNextChange;
210333 for(p=pTab->apChange[i]; p; p=pNextChange){
210334 pNextChange = p->pNext;
210335 sessionFree(pSession, p);
210336 }
210337 }
210338 sessionFree(pSession, (char*)pTab->azCol); /* cast works around VC++ bug */
210339 sessionFree(pSession, pTab->apChange);
210340 sessionFree(pSession, pTab);
210341 }
210342}
210343
210344/*
210345** Delete a session object previously allocated using sqlite3session_create().
210346*/
210347SQLITE_API void sqlite3session_delete(sqlite3_session *pSession){
210348 sqlite3 *db = pSession->db;
210349 sqlite3_session *pHead;
210350 sqlite3_session **pp;
210351
210352 /* Unlink the session from the linked list of sessions attached to the
210353 ** database handle. Hold the db mutex while doing so. */
210354 sqlite3_mutex_enter(sqlite3_db_mutex(db));
210355 pHead = (sqlite3_session*)sqlite3_preupdate_hook(db, 0, 0);
210356 for(pp=&pHead; ALWAYS((*pp)!=0); pp=&((*pp)->pNext)){
210357 if( (*pp)==pSession ){
210358 *pp = (*pp)->pNext;
210359 if( pHead ) sqlite3_preupdate_hook(db, xPreUpdate, (void*)pHead);
210360 break;
210361 }
210362 }
210363 sqlite3_mutex_leave(sqlite3_db_mutex(db));
210364 sqlite3ValueFree(pSession->pZeroBlob);
210365
210366 /* Delete all attached table objects. And the contents of their
210367 ** associated hash-tables. */
210368 sessionDeleteTable(pSession, pSession->pTable);
210369
210370 /* Assert that all allocations have been freed and then free the
210371 ** session object itself. */
210372 assert( pSession->nMalloc==0 );
210373 sqlite3_free(pSession);
210374}
210375
210376/*
210377** Set a table filter on a Session Object.
210378*/
210379SQLITE_API void sqlite3session_table_filter(
210380 sqlite3_session *pSession,
210381 int(*xFilter)(void*, const char*),
210382 void *pCtx /* First argument passed to xFilter */
210383){
210384 pSession->bAutoAttach = 1;
210385 pSession->pFilterCtx = pCtx;
210386 pSession->xTableFilter = xFilter;
210387}
210388
210389/*
210390** Attach a table to a session. All subsequent changes made to the table
210391** while the session object is enabled will be recorded.
210392**
210393** Only tables that have a PRIMARY KEY defined may be attached. It does
210394** not matter if the PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias)
210395** or not.
210396*/
210397SQLITE_API int sqlite3session_attach(
210398 sqlite3_session *pSession, /* Session object */
210399 const char *zName /* Table name */
210400){
210401 int rc = SQLITE_OK;
210402 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
210403
210404 if( !zName ){
210405 pSession->bAutoAttach = 1;
210406 }else{
210407 SessionTable *pTab; /* New table object (if required) */
210408 int nName; /* Number of bytes in string zName */
210409
210410 /* First search for an existing entry. If one is found, this call is
210411 ** a no-op. Return early. */
210412 nName = sqlite3Strlen30(zName);
210413 for(pTab=pSession->pTable; pTab; pTab=pTab->pNext){
210414 if( 0==sqlite3_strnicmp(pTab->zName, zName, nName+1) ) break;
210415 }
210416
210417 if( !pTab ){
210418 /* Allocate new SessionTable object. */
210419 int nByte = sizeof(SessionTable) + nName + 1;
210420 pTab = (SessionTable*)sessionMalloc64(pSession, nByte);
210421 if( !pTab ){
210422 rc = SQLITE_NOMEM;
210423 }else{
210424 /* Populate the new SessionTable object and link it into the list.
210425 ** The new object must be linked onto the end of the list, not
210426 ** simply added to the start of it in order to ensure that tables
210427 ** appear in the correct order when a changeset or patchset is
210428 ** eventually generated. */
210429 SessionTable **ppTab;
210430 memset(pTab, 0, sizeof(SessionTable));
210431 pTab->zName = (char *)&pTab[1];
210432 memcpy(pTab->zName, zName, nName+1);
210433 for(ppTab=&pSession->pTable; *ppTab; ppTab=&(*ppTab)->pNext);
210434 *ppTab = pTab;
210435 }
210436 }
210437 }
210438
210439 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
210440 return rc;
210441}
210442
210443/*
210444** Ensure that there is room in the buffer to append nByte bytes of data.
210445** If not, use sqlite3_realloc() to grow the buffer so that there is.
210446**
210447** If successful, return zero. Otherwise, if an OOM condition is encountered,
210448** set *pRc to SQLITE_NOMEM and return non-zero.
210449*/
210450static int sessionBufferGrow(SessionBuffer *p, i64 nByte, int *pRc){
210451#define SESSION_MAX_BUFFER_SZ (0x7FFFFF00 - 1)
210452 i64 nReq = p->nBuf + nByte;
210453 if( *pRc==SQLITE_OK && nReq>p->nAlloc ){
210454 u8 *aNew;
210455 i64 nNew = p->nAlloc ? p->nAlloc : 128;
210456
210457 do {
210458 nNew = nNew*2;
210459 }while( nNew<nReq );
210460
210461 /* The value of SESSION_MAX_BUFFER_SZ is copied from the implementation
210462 ** of sqlite3_realloc64(). Allocations greater than this size in bytes
210463 ** always fail. It is used here to ensure that this routine can always
210464 ** allocate up to this limit - instead of up to the largest power of
210465 ** two smaller than the limit. */
210466 if( nNew>SESSION_MAX_BUFFER_SZ ){
210467 nNew = SESSION_MAX_BUFFER_SZ;
210468 if( nNew<nReq ){
210469 *pRc = SQLITE_NOMEM;
210470 return 1;
210471 }
210472 }
210473
210474 aNew = (u8 *)sqlite3_realloc64(p->aBuf, nNew);
210475 if( 0==aNew ){
210476 *pRc = SQLITE_NOMEM;
210477 }else{
210478 p->aBuf = aNew;
210479 p->nAlloc = nNew;
210480 }
210481 }
210482 return (*pRc!=SQLITE_OK);
210483}
210484
210485/*
210486** Append the value passed as the second argument to the buffer passed
210487** as the first.
210488**
210489** This function is a no-op if *pRc is non-zero when it is called.
210490** Otherwise, if an error occurs, *pRc is set to an SQLite error code
210491** before returning.
210492*/
210493static void sessionAppendValue(SessionBuffer *p, sqlite3_value *pVal, int *pRc){
210494 int rc = *pRc;
210495 if( rc==SQLITE_OK ){
210496 sqlite3_int64 nByte = 0;
210497 rc = sessionSerializeValue(0, pVal, &nByte);
210498 sessionBufferGrow(p, nByte, &rc);
210499 if( rc==SQLITE_OK ){
210500 rc = sessionSerializeValue(&p->aBuf[p->nBuf], pVal, 0);
210501 p->nBuf += nByte;
210502 }else{
210503 *pRc = rc;
210504 }
210505 }
210506}
210507
210508/*
210509** This function is a no-op if *pRc is other than SQLITE_OK when it is
210510** called. Otherwise, append a single byte to the buffer.
210511**
210512** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
210513** returning.
210514*/
210515static void sessionAppendByte(SessionBuffer *p, u8 v, int *pRc){
210516 if( 0==sessionBufferGrow(p, 1, pRc) ){
210517 p->aBuf[p->nBuf++] = v;
210518 }
210519}
210520
210521/*
210522** This function is a no-op if *pRc is other than SQLITE_OK when it is
210523** called. Otherwise, append a single varint to the buffer.
210524**
210525** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
210526** returning.
210527*/
210528static void sessionAppendVarint(SessionBuffer *p, int v, int *pRc){
210529 if( 0==sessionBufferGrow(p, 9, pRc) ){
210530 p->nBuf += sessionVarintPut(&p->aBuf[p->nBuf], v);
210531 }
210532}
210533
210534/*
210535** This function is a no-op if *pRc is other than SQLITE_OK when it is
210536** called. Otherwise, append a blob of data to the buffer.
210537**
210538** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
210539** returning.
210540*/
210541static void sessionAppendBlob(
210542 SessionBuffer *p,
210543 const u8 *aBlob,
210544 int nBlob,
210545 int *pRc
210546){
210547 if( nBlob>0 && 0==sessionBufferGrow(p, nBlob, pRc) ){
210548 memcpy(&p->aBuf[p->nBuf], aBlob, nBlob);
210549 p->nBuf += nBlob;
210550 }
210551}
210552
210553/*
210554** This function is a no-op if *pRc is other than SQLITE_OK when it is
210555** called. Otherwise, append a string to the buffer. All bytes in the string
210556** up to (but not including) the nul-terminator are written to the buffer.
210557**
210558** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
210559** returning.
210560*/
210561static void sessionAppendStr(
210562 SessionBuffer *p,
210563 const char *zStr,
210564 int *pRc
210565){
210566 int nStr = sqlite3Strlen30(zStr);
210567 if( 0==sessionBufferGrow(p, nStr, pRc) ){
210568 memcpy(&p->aBuf[p->nBuf], zStr, nStr);
210569 p->nBuf += nStr;
210570 }
210571}
210572
210573/*
210574** This function is a no-op if *pRc is other than SQLITE_OK when it is
210575** called. Otherwise, append the string representation of integer iVal
210576** to the buffer. No nul-terminator is written.
210577**
210578** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
210579** returning.
210580*/
210581static void sessionAppendInteger(
210582 SessionBuffer *p, /* Buffer to append to */
210583 int iVal, /* Value to write the string rep. of */
210584 int *pRc /* IN/OUT: Error code */
210585){
210586 char aBuf[24];
210587 sqlite3_snprintf(sizeof(aBuf)-1, aBuf, "%d", iVal);
210588 sessionAppendStr(p, aBuf, pRc);
210589}
210590
210591/*
210592** This function is a no-op if *pRc is other than SQLITE_OK when it is
210593** called. Otherwise, append the string zStr enclosed in quotes (") and
210594** with any embedded quote characters escaped to the buffer. No
210595** nul-terminator byte is written.
210596**
210597** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
210598** returning.
210599*/
210600static void sessionAppendIdent(
210601 SessionBuffer *p, /* Buffer to a append to */
210602 const char *zStr, /* String to quote, escape and append */
210603 int *pRc /* IN/OUT: Error code */
210604){
210605 int nStr = sqlite3Strlen30(zStr)*2 + 2 + 1;
210606 if( 0==sessionBufferGrow(p, nStr, pRc) ){
210607 char *zOut = (char *)&p->aBuf[p->nBuf];
210608 const char *zIn = zStr;
210609 *zOut++ = '"';
210610 while( *zIn ){
210611 if( *zIn=='"' ) *zOut++ = '"';
210612 *zOut++ = *(zIn++);
210613 }
210614 *zOut++ = '"';
210615 p->nBuf = (int)((u8 *)zOut - p->aBuf);
210616 }
210617}
210618
210619/*
210620** This function is a no-op if *pRc is other than SQLITE_OK when it is
210621** called. Otherwse, it appends the serialized version of the value stored
210622** in column iCol of the row that SQL statement pStmt currently points
210623** to to the buffer.
210624*/
210625static void sessionAppendCol(
210626 SessionBuffer *p, /* Buffer to append to */
210627 sqlite3_stmt *pStmt, /* Handle pointing to row containing value */
210628 int iCol, /* Column to read value from */
210629 int *pRc /* IN/OUT: Error code */
210630){
210631 if( *pRc==SQLITE_OK ){
210632 int eType = sqlite3_column_type(pStmt, iCol);
210633 sessionAppendByte(p, (u8)eType, pRc);
210634 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
210635 sqlite3_int64 i;
210636 u8 aBuf[8];
210637 if( eType==SQLITE_INTEGER ){
210638 i = sqlite3_column_int64(pStmt, iCol);
210639 }else{
210640 double r = sqlite3_column_double(pStmt, iCol);
210641 memcpy(&i, &r, 8);
210642 }
210643 sessionPutI64(aBuf, i);
210644 sessionAppendBlob(p, aBuf, 8, pRc);
210645 }
210646 if( eType==SQLITE_BLOB || eType==SQLITE_TEXT ){
210647 u8 *z;
210648 int nByte;
210649 if( eType==SQLITE_BLOB ){
210650 z = (u8 *)sqlite3_column_blob(pStmt, iCol);
210651 }else{
210652 z = (u8 *)sqlite3_column_text(pStmt, iCol);
210653 }
210654 nByte = sqlite3_column_bytes(pStmt, iCol);
210655 if( z || (eType==SQLITE_BLOB && nByte==0) ){
210656 sessionAppendVarint(p, nByte, pRc);
210657 sessionAppendBlob(p, z, nByte, pRc);
210658 }else{
210659 *pRc = SQLITE_NOMEM;
210660 }
210661 }
210662 }
210663}
210664
210665/*
210666**
210667** This function appends an update change to the buffer (see the comments
210668** under "CHANGESET FORMAT" at the top of the file). An update change
210669** consists of:
210670**
210671** 1 byte: SQLITE_UPDATE (0x17)
210672** n bytes: old.* record (see RECORD FORMAT)
210673** m bytes: new.* record (see RECORD FORMAT)
210674**
210675** The SessionChange object passed as the third argument contains the
210676** values that were stored in the row when the session began (the old.*
210677** values). The statement handle passed as the second argument points
210678** at the current version of the row (the new.* values).
210679**
210680** If all of the old.* values are equal to their corresponding new.* value
210681** (i.e. nothing has changed), then no data at all is appended to the buffer.
210682**
210683** Otherwise, the old.* record contains all primary key values and the
210684** original values of any fields that have been modified. The new.* record
210685** contains the new values of only those fields that have been modified.
210686*/
210687static int sessionAppendUpdate(
210688 SessionBuffer *pBuf, /* Buffer to append to */
210689 int bPatchset, /* True for "patchset", 0 for "changeset" */
210690 sqlite3_stmt *pStmt, /* Statement handle pointing at new row */
210691 SessionChange *p, /* Object containing old values */
210692 u8 *abPK /* Boolean array - true for PK columns */
210693){
210694 int rc = SQLITE_OK;
210695 SessionBuffer buf2 = {0,0,0}; /* Buffer to accumulate new.* record in */
210696 int bNoop = 1; /* Set to zero if any values are modified */
210697 int nRewind = pBuf->nBuf; /* Set to zero if any values are modified */
210698 int i; /* Used to iterate through columns */
210699 u8 *pCsr = p->aRecord; /* Used to iterate through old.* values */
210700
210701 assert( abPK!=0 );
210702 sessionAppendByte(pBuf, SQLITE_UPDATE, &rc);
210703 sessionAppendByte(pBuf, p->bIndirect, &rc);
210704 for(i=0; i<sqlite3_column_count(pStmt); i++){
210705 int bChanged = 0;
210706 int nAdvance;
210707 int eType = *pCsr;
210708 switch( eType ){
210709 case SQLITE_NULL:
210710 nAdvance = 1;
210711 if( sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
210712 bChanged = 1;
210713 }
210714 break;
210715
210716 case SQLITE_FLOAT:
210717 case SQLITE_INTEGER: {
210718 nAdvance = 9;
210719 if( eType==sqlite3_column_type(pStmt, i) ){
210720 sqlite3_int64 iVal = sessionGetI64(&pCsr[1]);
210721 if( eType==SQLITE_INTEGER ){
210722 if( iVal==sqlite3_column_int64(pStmt, i) ) break;
210723 }else{
210724 double dVal;
210725 memcpy(&dVal, &iVal, 8);
210726 if( dVal==sqlite3_column_double(pStmt, i) ) break;
210727 }
210728 }
210729 bChanged = 1;
210730 break;
210731 }
210732
210733 default: {
210734 int n;
210735 int nHdr = 1 + sessionVarintGet(&pCsr[1], &n);
210736 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
210737 nAdvance = nHdr + n;
210738 if( eType==sqlite3_column_type(pStmt, i)
210739 && n==sqlite3_column_bytes(pStmt, i)
210740 && (n==0 || 0==memcmp(&pCsr[nHdr], sqlite3_column_blob(pStmt, i), n))
210741 ){
210742 break;
210743 }
210744 bChanged = 1;
210745 }
210746 }
210747
210748 /* If at least one field has been modified, this is not a no-op. */
210749 if( bChanged ) bNoop = 0;
210750
210751 /* Add a field to the old.* record. This is omitted if this modules is
210752 ** currently generating a patchset. */
210753 if( bPatchset==0 ){
210754 if( bChanged || abPK[i] ){
210755 sessionAppendBlob(pBuf, pCsr, nAdvance, &rc);
210756 }else{
210757 sessionAppendByte(pBuf, 0, &rc);
210758 }
210759 }
210760
210761 /* Add a field to the new.* record. Or the only record if currently
210762 ** generating a patchset. */
210763 if( bChanged || (bPatchset && abPK[i]) ){
210764 sessionAppendCol(&buf2, pStmt, i, &rc);
210765 }else{
210766 sessionAppendByte(&buf2, 0, &rc);
210767 }
210768
210769 pCsr += nAdvance;
210770 }
210771
210772 if( bNoop ){
210773 pBuf->nBuf = nRewind;
210774 }else{
210775 sessionAppendBlob(pBuf, buf2.aBuf, buf2.nBuf, &rc);
210776 }
210777 sqlite3_free(buf2.aBuf);
210778
210779 return rc;
210780}
210781
210782/*
210783** Append a DELETE change to the buffer passed as the first argument. Use
210784** the changeset format if argument bPatchset is zero, or the patchset
210785** format otherwise.
210786*/
210787static int sessionAppendDelete(
210788 SessionBuffer *pBuf, /* Buffer to append to */
210789 int bPatchset, /* True for "patchset", 0 for "changeset" */
210790 SessionChange *p, /* Object containing old values */
210791 int nCol, /* Number of columns in table */
210792 u8 *abPK /* Boolean array - true for PK columns */
210793){
210794 int rc = SQLITE_OK;
210795
210796 sessionAppendByte(pBuf, SQLITE_DELETE, &rc);
210797 sessionAppendByte(pBuf, p->bIndirect, &rc);
210798
210799 if( bPatchset==0 ){
210800 sessionAppendBlob(pBuf, p->aRecord, p->nRecord, &rc);
210801 }else{
210802 int i;
210803 u8 *a = p->aRecord;
210804 for(i=0; i<nCol; i++){
210805 u8 *pStart = a;
210806 int eType = *a++;
210807
210808 switch( eType ){
210809 case 0:
210810 case SQLITE_NULL:
210811 assert( abPK[i]==0 );
210812 break;
210813
210814 case SQLITE_FLOAT:
210815 case SQLITE_INTEGER:
210816 a += 8;
210817 break;
210818
210819 default: {
210820 int n;
210821 a += sessionVarintGet(a, &n);
210822 a += n;
210823 break;
210824 }
210825 }
210826 if( abPK[i] ){
210827 sessionAppendBlob(pBuf, pStart, (int)(a-pStart), &rc);
210828 }
210829 }
210830 assert( (a - p->aRecord)==p->nRecord );
210831 }
210832
210833 return rc;
210834}
210835
210836/*
210837** Formulate and prepare a SELECT statement to retrieve a row from table
210838** zTab in database zDb based on its primary key. i.e.
210839**
210840** SELECT * FROM zDb.zTab WHERE pk1 = ? AND pk2 = ? AND ...
210841*/
210842static int sessionSelectStmt(
210843 sqlite3 *db, /* Database handle */
210844 const char *zDb, /* Database name */
210845 const char *zTab, /* Table name */
210846 int nCol, /* Number of columns in table */
210847 const char **azCol, /* Names of table columns */
210848 u8 *abPK, /* PRIMARY KEY array */
210849 sqlite3_stmt **ppStmt /* OUT: Prepared SELECT statement */
210850){
210851 int rc = SQLITE_OK;
210852 char *zSql = 0;
210853 int nSql = -1;
210854
210855 if( 0==sqlite3_stricmp("sqlite_stat1", zTab) ){
210856 zSql = sqlite3_mprintf(
210857 "SELECT tbl, ?2, stat FROM %Q.sqlite_stat1 WHERE tbl IS ?1 AND "
210858 "idx IS (CASE WHEN ?2=X'' THEN NULL ELSE ?2 END)", zDb
210859 );
210860 if( zSql==0 ) rc = SQLITE_NOMEM;
210861 }else{
210862 int i;
210863 const char *zSep = "";
210864 SessionBuffer buf = {0, 0, 0};
210865
210866 sessionAppendStr(&buf, "SELECT * FROM ", &rc);
210867 sessionAppendIdent(&buf, zDb, &rc);
210868 sessionAppendStr(&buf, ".", &rc);
210869 sessionAppendIdent(&buf, zTab, &rc);
210870 sessionAppendStr(&buf, " WHERE ", &rc);
210871 for(i=0; i<nCol; i++){
210872 if( abPK[i] ){
210873 sessionAppendStr(&buf, zSep, &rc);
210874 sessionAppendIdent(&buf, azCol[i], &rc);
210875 sessionAppendStr(&buf, " IS ?", &rc);
210876 sessionAppendInteger(&buf, i+1, &rc);
210877 zSep = " AND ";
210878 }
210879 }
210880 zSql = (char*)buf.aBuf;
210881 nSql = buf.nBuf;
210882 }
210883
210884 if( rc==SQLITE_OK ){
210885 rc = sqlite3_prepare_v2(db, zSql, nSql, ppStmt, 0);
210886 }
210887 sqlite3_free(zSql);
210888 return rc;
210889}
210890
210891/*
210892** Bind the PRIMARY KEY values from the change passed in argument pChange
210893** to the SELECT statement passed as the first argument. The SELECT statement
210894** is as prepared by function sessionSelectStmt().
210895**
210896** Return SQLITE_OK if all PK values are successfully bound, or an SQLite
210897** error code (e.g. SQLITE_NOMEM) otherwise.
210898*/
210899static int sessionSelectBind(
210900 sqlite3_stmt *pSelect, /* SELECT from sessionSelectStmt() */
210901 int nCol, /* Number of columns in table */
210902 u8 *abPK, /* PRIMARY KEY array */
210903 SessionChange *pChange /* Change structure */
210904){
210905 int i;
210906 int rc = SQLITE_OK;
210907 u8 *a = pChange->aRecord;
210908
210909 for(i=0; i<nCol && rc==SQLITE_OK; i++){
210910 int eType = *a++;
210911
210912 switch( eType ){
210913 case 0:
210914 case SQLITE_NULL:
210915 assert( abPK[i]==0 );
210916 break;
210917
210918 case SQLITE_INTEGER: {
210919 if( abPK[i] ){
210920 i64 iVal = sessionGetI64(a);
210921 rc = sqlite3_bind_int64(pSelect, i+1, iVal);
210922 }
210923 a += 8;
210924 break;
210925 }
210926
210927 case SQLITE_FLOAT: {
210928 if( abPK[i] ){
210929 double rVal;
210930 i64 iVal = sessionGetI64(a);
210931 memcpy(&rVal, &iVal, 8);
210932 rc = sqlite3_bind_double(pSelect, i+1, rVal);
210933 }
210934 a += 8;
210935 break;
210936 }
210937
210938 case SQLITE_TEXT: {
210939 int n;
210940 a += sessionVarintGet(a, &n);
210941 if( abPK[i] ){
210942 rc = sqlite3_bind_text(pSelect, i+1, (char *)a, n, SQLITE_TRANSIENT);
210943 }
210944 a += n;
210945 break;
210946 }
210947
210948 default: {
210949 int n;
210950 assert( eType==SQLITE_BLOB );
210951 a += sessionVarintGet(a, &n);
210952 if( abPK[i] ){
210953 rc = sqlite3_bind_blob(pSelect, i+1, a, n, SQLITE_TRANSIENT);
210954 }
210955 a += n;
210956 break;
210957 }
210958 }
210959 }
210960
210961 return rc;
210962}
210963
210964/*
210965** This function is a no-op if *pRc is set to other than SQLITE_OK when it
210966** is called. Otherwise, append a serialized table header (part of the binary
210967** changeset format) to buffer *pBuf. If an error occurs, set *pRc to an
210968** SQLite error code before returning.
210969*/
210970static void sessionAppendTableHdr(
210971 SessionBuffer *pBuf, /* Append header to this buffer */
210972 int bPatchset, /* Use the patchset format if true */
210973 SessionTable *pTab, /* Table object to append header for */
210974 int *pRc /* IN/OUT: Error code */
210975){
210976 /* Write a table header */
210977 sessionAppendByte(pBuf, (bPatchset ? 'P' : 'T'), pRc);
210978 sessionAppendVarint(pBuf, pTab->nCol, pRc);
210979 sessionAppendBlob(pBuf, pTab->abPK, pTab->nCol, pRc);
210980 sessionAppendBlob(pBuf, (u8 *)pTab->zName, (int)strlen(pTab->zName)+1, pRc);
210981}
210982
210983/*
210984** Generate either a changeset (if argument bPatchset is zero) or a patchset
210985** (if it is non-zero) based on the current contents of the session object
210986** passed as the first argument.
210987**
210988** If no error occurs, SQLITE_OK is returned and the new changeset/patchset
210989** stored in output variables *pnChangeset and *ppChangeset. Or, if an error
210990** occurs, an SQLite error code is returned and both output variables set
210991** to 0.
210992*/
210993static int sessionGenerateChangeset(
210994 sqlite3_session *pSession, /* Session object */
210995 int bPatchset, /* True for patchset, false for changeset */
210996 int (*xOutput)(void *pOut, const void *pData, int nData),
210997 void *pOut, /* First argument for xOutput */
210998 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
210999 void **ppChangeset /* OUT: Buffer containing changeset */
211000){
211001 sqlite3 *db = pSession->db; /* Source database handle */
211002 SessionTable *pTab; /* Used to iterate through attached tables */
211003 SessionBuffer buf = {0,0,0}; /* Buffer in which to accumlate changeset */
211004 int rc; /* Return code */
211005
211006 assert( xOutput==0 || (pnChangeset==0 && ppChangeset==0) );
211007 assert( xOutput!=0 || (pnChangeset!=0 && ppChangeset!=0) );
211008
211009 /* Zero the output variables in case an error occurs. If this session
211010 ** object is already in the error state (sqlite3_session.rc != SQLITE_OK),
211011 ** this call will be a no-op. */
211012 if( xOutput==0 ){
211013 assert( pnChangeset!=0 && ppChangeset!=0 );
211014 *pnChangeset = 0;
211015 *ppChangeset = 0;
211016 }
211017
211018 if( pSession->rc ) return pSession->rc;
211019 rc = sqlite3_exec(pSession->db, "SAVEPOINT changeset", 0, 0, 0);
211020 if( rc!=SQLITE_OK ) return rc;
211021
211022 sqlite3_mutex_enter(sqlite3_db_mutex(db));
211023
211024 for(pTab=pSession->pTable; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
211025 if( pTab->nEntry ){
211026 const char *zName = pTab->zName;
211027 int nCol = 0; /* Number of columns in table */
211028 u8 *abPK = 0; /* Primary key array */
211029 const char **azCol = 0; /* Table columns */
211030 int i; /* Used to iterate through hash buckets */
211031 sqlite3_stmt *pSel = 0; /* SELECT statement to query table pTab */
211032 int nRewind = buf.nBuf; /* Initial size of write buffer */
211033 int nNoop; /* Size of buffer after writing tbl header */
211034
211035 /* Check the table schema is still Ok. */
211036 rc = sessionTableInfo(0, db, pSession->zDb, zName, &nCol, 0,&azCol,&abPK);
211037 if( !rc && (pTab->nCol!=nCol || memcmp(abPK, pTab->abPK, nCol)) ){
211038 rc = SQLITE_SCHEMA;
211039 }
211040
211041 /* Write a table header */
211042 sessionAppendTableHdr(&buf, bPatchset, pTab, &rc);
211043
211044 /* Build and compile a statement to execute: */
211045 if( rc==SQLITE_OK ){
211046 rc = sessionSelectStmt(
211047 db, pSession->zDb, zName, nCol, azCol, abPK, &pSel);
211048 }
211049
211050 nNoop = buf.nBuf;
211051 for(i=0; i<pTab->nChange && rc==SQLITE_OK; i++){
211052 SessionChange *p; /* Used to iterate through changes */
211053
211054 for(p=pTab->apChange[i]; rc==SQLITE_OK && p; p=p->pNext){
211055 rc = sessionSelectBind(pSel, nCol, abPK, p);
211056 if( rc!=SQLITE_OK ) continue;
211057 if( sqlite3_step(pSel)==SQLITE_ROW ){
211058 if( p->op==SQLITE_INSERT ){
211059 int iCol;
211060 sessionAppendByte(&buf, SQLITE_INSERT, &rc);
211061 sessionAppendByte(&buf, p->bIndirect, &rc);
211062 for(iCol=0; iCol<nCol; iCol++){
211063 sessionAppendCol(&buf, pSel, iCol, &rc);
211064 }
211065 }else{
211066 assert( abPK!=0 ); /* Because sessionSelectStmt() returned ok */
211067 rc = sessionAppendUpdate(&buf, bPatchset, pSel, p, abPK);
211068 }
211069 }else if( p->op!=SQLITE_INSERT ){
211070 rc = sessionAppendDelete(&buf, bPatchset, p, nCol, abPK);
211071 }
211072 if( rc==SQLITE_OK ){
211073 rc = sqlite3_reset(pSel);
211074 }
211075
211076 /* If the buffer is now larger than sessions_strm_chunk_size, pass
211077 ** its contents to the xOutput() callback. */
211078 if( xOutput
211079 && rc==SQLITE_OK
211080 && buf.nBuf>nNoop
211081 && buf.nBuf>sessions_strm_chunk_size
211082 ){
211083 rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
211084 nNoop = -1;
211085 buf.nBuf = 0;
211086 }
211087
211088 }
211089 }
211090
211091 sqlite3_finalize(pSel);
211092 if( buf.nBuf==nNoop ){
211093 buf.nBuf = nRewind;
211094 }
211095 sqlite3_free((char*)azCol); /* cast works around VC++ bug */
211096 }
211097 }
211098
211099 if( rc==SQLITE_OK ){
211100 if( xOutput==0 ){
211101 *pnChangeset = buf.nBuf;
211102 *ppChangeset = buf.aBuf;
211103 buf.aBuf = 0;
211104 }else if( buf.nBuf>0 ){
211105 rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
211106 }
211107 }
211108
211109 sqlite3_free(buf.aBuf);
211110 sqlite3_exec(db, "RELEASE changeset", 0, 0, 0);
211111 sqlite3_mutex_leave(sqlite3_db_mutex(db));
211112 return rc;
211113}
211114
211115/*
211116** Obtain a changeset object containing all changes recorded by the
211117** session object passed as the first argument.
211118**
211119** It is the responsibility of the caller to eventually free the buffer
211120** using sqlite3_free().
211121*/
211122SQLITE_API int sqlite3session_changeset(
211123 sqlite3_session *pSession, /* Session object */
211124 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
211125 void **ppChangeset /* OUT: Buffer containing changeset */
211126){
211127 int rc;
211128
211129 if( pnChangeset==0 || ppChangeset==0 ) return SQLITE_MISUSE;
211130 rc = sessionGenerateChangeset(pSession, 0, 0, 0, pnChangeset,ppChangeset);
211131 assert( rc || pnChangeset==0
211132 || pSession->bEnableSize==0 || *pnChangeset<=pSession->nMaxChangesetSize
211133 );
211134 return rc;
211135}
211136
211137/*
211138** Streaming version of sqlite3session_changeset().
211139*/
211140SQLITE_API int sqlite3session_changeset_strm(
211141 sqlite3_session *pSession,
211142 int (*xOutput)(void *pOut, const void *pData, int nData),
211143 void *pOut
211144){
211145 if( xOutput==0 ) return SQLITE_MISUSE;
211146 return sessionGenerateChangeset(pSession, 0, xOutput, pOut, 0, 0);
211147}
211148
211149/*
211150** Streaming version of sqlite3session_patchset().
211151*/
211152SQLITE_API int sqlite3session_patchset_strm(
211153 sqlite3_session *pSession,
211154 int (*xOutput)(void *pOut, const void *pData, int nData),
211155 void *pOut
211156){
211157 if( xOutput==0 ) return SQLITE_MISUSE;
211158 return sessionGenerateChangeset(pSession, 1, xOutput, pOut, 0, 0);
211159}
211160
211161/*
211162** Obtain a patchset object containing all changes recorded by the
211163** session object passed as the first argument.
211164**
211165** It is the responsibility of the caller to eventually free the buffer
211166** using sqlite3_free().
211167*/
211168SQLITE_API int sqlite3session_patchset(
211169 sqlite3_session *pSession, /* Session object */
211170 int *pnPatchset, /* OUT: Size of buffer at *ppChangeset */
211171 void **ppPatchset /* OUT: Buffer containing changeset */
211172){
211173 if( pnPatchset==0 || ppPatchset==0 ) return SQLITE_MISUSE;
211174 return sessionGenerateChangeset(pSession, 1, 0, 0, pnPatchset, ppPatchset);
211175}
211176
211177/*
211178** Enable or disable the session object passed as the first argument.
211179*/
211180SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable){
211181 int ret;
211182 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
211183 if( bEnable>=0 ){
211184 pSession->bEnable = bEnable;
211185 }
211186 ret = pSession->bEnable;
211187 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
211188 return ret;
211189}
211190
211191/*
211192** Enable or disable the session object passed as the first argument.
211193*/
211194SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect){
211195 int ret;
211196 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
211197 if( bIndirect>=0 ){
211198 pSession->bIndirect = bIndirect;
211199 }
211200 ret = pSession->bIndirect;
211201 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
211202 return ret;
211203}
211204
211205/*
211206** Return true if there have been no changes to monitored tables recorded
211207** by the session object passed as the only argument.
211208*/
211209SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession){
211210 int ret = 0;
211211 SessionTable *pTab;
211212
211213 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
211214 for(pTab=pSession->pTable; pTab && ret==0; pTab=pTab->pNext){
211215 ret = (pTab->nEntry>0);
211216 }
211217 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
211218
211219 return (ret==0);
211220}
211221
211222/*
211223** Return the amount of heap memory in use.
211224*/
211225SQLITE_API sqlite3_int64 sqlite3session_memory_used(sqlite3_session *pSession){
211226 return pSession->nMalloc;
211227}
211228
211229/*
211230** Configure the session object passed as the first argument.
211231*/
211232SQLITE_API int sqlite3session_object_config(sqlite3_session *pSession, int op, void *pArg){
211233 int rc = SQLITE_OK;
211234 switch( op ){
211235 case SQLITE_SESSION_OBJCONFIG_SIZE: {
211236 int iArg = *(int*)pArg;
211237 if( iArg>=0 ){
211238 if( pSession->pTable ){
211239 rc = SQLITE_MISUSE;
211240 }else{
211241 pSession->bEnableSize = (iArg!=0);
211242 }
211243 }
211244 *(int*)pArg = pSession->bEnableSize;
211245 break;
211246 }
211247
211248 default:
211249 rc = SQLITE_MISUSE;
211250 }
211251
211252 return rc;
211253}
211254
211255/*
211256** Return the maximum size of sqlite3session_changeset() output.
211257*/
211258SQLITE_API sqlite3_int64 sqlite3session_changeset_size(sqlite3_session *pSession){
211259 return pSession->nMaxChangesetSize;
211260}
211261
211262/*
211263** Do the work for either sqlite3changeset_start() or start_strm().
211264*/
211265static int sessionChangesetStart(
211266 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
211267 int (*xInput)(void *pIn, void *pData, int *pnData),
211268 void *pIn,
211269 int nChangeset, /* Size of buffer pChangeset in bytes */
211270 void *pChangeset, /* Pointer to buffer containing changeset */
211271 int bInvert, /* True to invert changeset */
211272 int bSkipEmpty /* True to skip empty UPDATE changes */
211273){
211274 sqlite3_changeset_iter *pRet; /* Iterator to return */
211275 int nByte; /* Number of bytes to allocate for iterator */
211276
211277 assert( xInput==0 || (pChangeset==0 && nChangeset==0) );
211278
211279 /* Zero the output variable in case an error occurs. */
211280 *pp = 0;
211281
211282 /* Allocate and initialize the iterator structure. */
211283 nByte = sizeof(sqlite3_changeset_iter);
211284 pRet = (sqlite3_changeset_iter *)sqlite3_malloc(nByte);
211285 if( !pRet ) return SQLITE_NOMEM;
211286 memset(pRet, 0, sizeof(sqlite3_changeset_iter));
211287 pRet->in.aData = (u8 *)pChangeset;
211288 pRet->in.nData = nChangeset;
211289 pRet->in.xInput = xInput;
211290 pRet->in.pIn = pIn;
211291 pRet->in.bEof = (xInput ? 0 : 1);
211292 pRet->bInvert = bInvert;
211293 pRet->bSkipEmpty = bSkipEmpty;
211294
211295 /* Populate the output variable and return success. */
211296 *pp = pRet;
211297 return SQLITE_OK;
211298}
211299
211300/*
211301** Create an iterator used to iterate through the contents of a changeset.
211302*/
211303SQLITE_API int sqlite3changeset_start(
211304 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
211305 int nChangeset, /* Size of buffer pChangeset in bytes */
211306 void *pChangeset /* Pointer to buffer containing changeset */
211307){
211308 return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, 0, 0);
211309}
211310SQLITE_API int sqlite3changeset_start_v2(
211311 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
211312 int nChangeset, /* Size of buffer pChangeset in bytes */
211313 void *pChangeset, /* Pointer to buffer containing changeset */
211314 int flags
211315){
211316 int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT);
211317 return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, bInvert, 0);
211318}
211319
211320/*
211321** Streaming version of sqlite3changeset_start().
211322*/
211323SQLITE_API int sqlite3changeset_start_strm(
211324 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
211325 int (*xInput)(void *pIn, void *pData, int *pnData),
211326 void *pIn
211327){
211328 return sessionChangesetStart(pp, xInput, pIn, 0, 0, 0, 0);
211329}
211330SQLITE_API int sqlite3changeset_start_v2_strm(
211331 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
211332 int (*xInput)(void *pIn, void *pData, int *pnData),
211333 void *pIn,
211334 int flags
211335){
211336 int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT);
211337 return sessionChangesetStart(pp, xInput, pIn, 0, 0, bInvert, 0);
211338}
211339
211340/*
211341** If the SessionInput object passed as the only argument is a streaming
211342** object and the buffer is full, discard some data to free up space.
211343*/
211344static void sessionDiscardData(SessionInput *pIn){
211345 if( pIn->xInput && pIn->iNext>=sessions_strm_chunk_size ){
211346 int nMove = pIn->buf.nBuf - pIn->iNext;
211347 assert( nMove>=0 );
211348 if( nMove>0 ){
211349 memmove(pIn->buf.aBuf, &pIn->buf.aBuf[pIn->iNext], nMove);
211350 }
211351 pIn->buf.nBuf -= pIn->iNext;
211352 pIn->iNext = 0;
211353 pIn->nData = pIn->buf.nBuf;
211354 }
211355}
211356
211357/*
211358** Ensure that there are at least nByte bytes available in the buffer. Or,
211359** if there are not nByte bytes remaining in the input, that all available
211360** data is in the buffer.
211361**
211362** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
211363*/
211364static int sessionInputBuffer(SessionInput *pIn, int nByte){
211365 int rc = SQLITE_OK;
211366 if( pIn->xInput ){
211367 while( !pIn->bEof && (pIn->iNext+nByte)>=pIn->nData && rc==SQLITE_OK ){
211368 int nNew = sessions_strm_chunk_size;
211369
211370 if( pIn->bNoDiscard==0 ) sessionDiscardData(pIn);
211371 if( SQLITE_OK==sessionBufferGrow(&pIn->buf, nNew, &rc) ){
211372 rc = pIn->xInput(pIn->pIn, &pIn->buf.aBuf[pIn->buf.nBuf], &nNew);
211373 if( nNew==0 ){
211374 pIn->bEof = 1;
211375 }else{
211376 pIn->buf.nBuf += nNew;
211377 }
211378 }
211379
211380 pIn->aData = pIn->buf.aBuf;
211381 pIn->nData = pIn->buf.nBuf;
211382 }
211383 }
211384 return rc;
211385}
211386
211387/*
211388** When this function is called, *ppRec points to the start of a record
211389** that contains nCol values. This function advances the pointer *ppRec
211390** until it points to the byte immediately following that record.
211391*/
211392static void sessionSkipRecord(
211393 u8 **ppRec, /* IN/OUT: Record pointer */
211394 int nCol /* Number of values in record */
211395){
211396 u8 *aRec = *ppRec;
211397 int i;
211398 for(i=0; i<nCol; i++){
211399 int eType = *aRec++;
211400 if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
211401 int nByte;
211402 aRec += sessionVarintGet((u8*)aRec, &nByte);
211403 aRec += nByte;
211404 }else if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
211405 aRec += 8;
211406 }
211407 }
211408
211409 *ppRec = aRec;
211410}
211411
211412/*
211413** This function sets the value of the sqlite3_value object passed as the
211414** first argument to a copy of the string or blob held in the aData[]
211415** buffer. SQLITE_OK is returned if successful, or SQLITE_NOMEM if an OOM
211416** error occurs.
211417*/
211418static int sessionValueSetStr(
211419 sqlite3_value *pVal, /* Set the value of this object */
211420 u8 *aData, /* Buffer containing string or blob data */
211421 int nData, /* Size of buffer aData[] in bytes */
211422 u8 enc /* String encoding (0 for blobs) */
211423){
211424 /* In theory this code could just pass SQLITE_TRANSIENT as the final
211425 ** argument to sqlite3ValueSetStr() and have the copy created
211426 ** automatically. But doing so makes it difficult to detect any OOM
211427 ** error. Hence the code to create the copy externally. */
211428 u8 *aCopy = sqlite3_malloc64((sqlite3_int64)nData+1);
211429 if( aCopy==0 ) return SQLITE_NOMEM;
211430 memcpy(aCopy, aData, nData);
211431 sqlite3ValueSetStr(pVal, nData, (char*)aCopy, enc, sqlite3_free);
211432 return SQLITE_OK;
211433}
211434
211435/*
211436** Deserialize a single record from a buffer in memory. See "RECORD FORMAT"
211437** for details.
211438**
211439** When this function is called, *paChange points to the start of the record
211440** to deserialize. Assuming no error occurs, *paChange is set to point to
211441** one byte after the end of the same record before this function returns.
211442** If the argument abPK is NULL, then the record contains nCol values. Or,
211443** if abPK is other than NULL, then the record contains only the PK fields
211444** (in other words, it is a patchset DELETE record).
211445**
211446** If successful, each element of the apOut[] array (allocated by the caller)
211447** is set to point to an sqlite3_value object containing the value read
211448** from the corresponding position in the record. If that value is not
211449** included in the record (i.e. because the record is part of an UPDATE change
211450** and the field was not modified), the corresponding element of apOut[] is
211451** set to NULL.
211452**
211453** It is the responsibility of the caller to free all sqlite_value structures
211454** using sqlite3_free().
211455**
211456** If an error occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
211457** The apOut[] array may have been partially populated in this case.
211458*/
211459static int sessionReadRecord(
211460 SessionInput *pIn, /* Input data */
211461 int nCol, /* Number of values in record */
211462 u8 *abPK, /* Array of primary key flags, or NULL */
211463 sqlite3_value **apOut, /* Write values to this array */
211464 int *pbEmpty
211465){
211466 int i; /* Used to iterate through columns */
211467 int rc = SQLITE_OK;
211468
211469 assert( pbEmpty==0 || *pbEmpty==0 );
211470 if( pbEmpty ) *pbEmpty = 1;
211471 for(i=0; i<nCol && rc==SQLITE_OK; i++){
211472 int eType = 0; /* Type of value (SQLITE_NULL, TEXT etc.) */
211473 if( abPK && abPK[i]==0 ) continue;
211474 rc = sessionInputBuffer(pIn, 9);
211475 if( rc==SQLITE_OK ){
211476 if( pIn->iNext>=pIn->nData ){
211477 rc = SQLITE_CORRUPT_BKPT;
211478 }else{
211479 eType = pIn->aData[pIn->iNext++];
211480 assert( apOut[i]==0 );
211481 if( eType ){
211482 if( pbEmpty ) *pbEmpty = 0;
211483 apOut[i] = sqlite3ValueNew(0);
211484 if( !apOut[i] ) rc = SQLITE_NOMEM;
211485 }
211486 }
211487 }
211488
211489 if( rc==SQLITE_OK ){
211490 u8 *aVal = &pIn->aData[pIn->iNext];
211491 if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
211492 int nByte;
211493 pIn->iNext += sessionVarintGet(aVal, &nByte);
211494 rc = sessionInputBuffer(pIn, nByte);
211495 if( rc==SQLITE_OK ){
211496 if( nByte<0 || nByte>pIn->nData-pIn->iNext ){
211497 rc = SQLITE_CORRUPT_BKPT;
211498 }else{
211499 u8 enc = (eType==SQLITE_TEXT ? SQLITE_UTF8 : 0);
211500 rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
211501 pIn->iNext += nByte;
211502 }
211503 }
211504 }
211505 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
211506 sqlite3_int64 v = sessionGetI64(aVal);
211507 if( eType==SQLITE_INTEGER ){
211508 sqlite3VdbeMemSetInt64(apOut[i], v);
211509 }else{
211510 double d;
211511 memcpy(&d, &v, 8);
211512 sqlite3VdbeMemSetDouble(apOut[i], d);
211513 }
211514 pIn->iNext += 8;
211515 }
211516 }
211517 }
211518
211519 return rc;
211520}
211521
211522/*
211523** The input pointer currently points to the second byte of a table-header.
211524** Specifically, to the following:
211525**
211526** + number of columns in table (varint)
211527** + array of PK flags (1 byte per column),
211528** + table name (nul terminated).
211529**
211530** This function ensures that all of the above is present in the input
211531** buffer (i.e. that it can be accessed without any calls to xInput()).
211532** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
211533** The input pointer is not moved.
211534*/
211535static int sessionChangesetBufferTblhdr(SessionInput *pIn, int *pnByte){
211536 int rc = SQLITE_OK;
211537 int nCol = 0;
211538 int nRead = 0;
211539
211540 rc = sessionInputBuffer(pIn, 9);
211541 if( rc==SQLITE_OK ){
211542 nRead += sessionVarintGet(&pIn->aData[pIn->iNext + nRead], &nCol);
211543 /* The hard upper limit for the number of columns in an SQLite
211544 ** database table is, according to sqliteLimit.h, 32676. So
211545 ** consider any table-header that purports to have more than 65536
211546 ** columns to be corrupt. This is convenient because otherwise,
211547 ** if the (nCol>65536) condition below were omitted, a sufficiently
211548 ** large value for nCol may cause nRead to wrap around and become
211549 ** negative. Leading to a crash. */
211550 if( nCol<0 || nCol>65536 ){
211551 rc = SQLITE_CORRUPT_BKPT;
211552 }else{
211553 rc = sessionInputBuffer(pIn, nRead+nCol+100);
211554 nRead += nCol;
211555 }
211556 }
211557
211558 while( rc==SQLITE_OK ){
211559 while( (pIn->iNext + nRead)<pIn->nData && pIn->aData[pIn->iNext + nRead] ){
211560 nRead++;
211561 }
211562 if( (pIn->iNext + nRead)<pIn->nData ) break;
211563 rc = sessionInputBuffer(pIn, nRead + 100);
211564 }
211565 *pnByte = nRead+1;
211566 return rc;
211567}
211568
211569/*
211570** The input pointer currently points to the first byte of the first field
211571** of a record consisting of nCol columns. This function ensures the entire
211572** record is buffered. It does not move the input pointer.
211573**
211574** If successful, SQLITE_OK is returned and *pnByte is set to the size of
211575** the record in bytes. Otherwise, an SQLite error code is returned. The
211576** final value of *pnByte is undefined in this case.
211577*/
211578static int sessionChangesetBufferRecord(
211579 SessionInput *pIn, /* Input data */
211580 int nCol, /* Number of columns in record */
211581 int *pnByte /* OUT: Size of record in bytes */
211582){
211583 int rc = SQLITE_OK;
211584 int nByte = 0;
211585 int i;
211586 for(i=0; rc==SQLITE_OK && i<nCol; i++){
211587 int eType;
211588 rc = sessionInputBuffer(pIn, nByte + 10);
211589 if( rc==SQLITE_OK ){
211590 eType = pIn->aData[pIn->iNext + nByte++];
211591 if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
211592 int n;
211593 nByte += sessionVarintGet(&pIn->aData[pIn->iNext+nByte], &n);
211594 nByte += n;
211595 rc = sessionInputBuffer(pIn, nByte);
211596 }else if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
211597 nByte += 8;
211598 }
211599 }
211600 }
211601 *pnByte = nByte;
211602 return rc;
211603}
211604
211605/*
211606** The input pointer currently points to the second byte of a table-header.
211607** Specifically, to the following:
211608**
211609** + number of columns in table (varint)
211610** + array of PK flags (1 byte per column),
211611** + table name (nul terminated).
211612**
211613** This function decodes the table-header and populates the p->nCol,
211614** p->zTab and p->abPK[] variables accordingly. The p->apValue[] array is
211615** also allocated or resized according to the new value of p->nCol. The
211616** input pointer is left pointing to the byte following the table header.
211617**
211618** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code
211619** is returned and the final values of the various fields enumerated above
211620** are undefined.
211621*/
211622static int sessionChangesetReadTblhdr(sqlite3_changeset_iter *p){
211623 int rc;
211624 int nCopy;
211625 assert( p->rc==SQLITE_OK );
211626
211627 rc = sessionChangesetBufferTblhdr(&p->in, &nCopy);
211628 if( rc==SQLITE_OK ){
211629 int nByte;
211630 int nVarint;
211631 nVarint = sessionVarintGet(&p->in.aData[p->in.iNext], &p->nCol);
211632 if( p->nCol>0 ){
211633 nCopy -= nVarint;
211634 p->in.iNext += nVarint;
211635 nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy;
211636 p->tblhdr.nBuf = 0;
211637 sessionBufferGrow(&p->tblhdr, nByte, &rc);
211638 }else{
211639 rc = SQLITE_CORRUPT_BKPT;
211640 }
211641 }
211642
211643 if( rc==SQLITE_OK ){
211644 size_t iPK = sizeof(sqlite3_value*)*p->nCol*2;
211645 memset(p->tblhdr.aBuf, 0, iPK);
211646 memcpy(&p->tblhdr.aBuf[iPK], &p->in.aData[p->in.iNext], nCopy);
211647 p->in.iNext += nCopy;
211648 }
211649
211650 p->apValue = (sqlite3_value**)p->tblhdr.aBuf;
211651 if( p->apValue==0 ){
211652 p->abPK = 0;
211653 p->zTab = 0;
211654 }else{
211655 p->abPK = (u8*)&p->apValue[p->nCol*2];
211656 p->zTab = p->abPK ? (char*)&p->abPK[p->nCol] : 0;
211657 }
211658 return (p->rc = rc);
211659}
211660
211661/*
211662** Advance the changeset iterator to the next change. The differences between
211663** this function and sessionChangesetNext() are that
211664**
211665** * If pbEmpty is not NULL and the change is a no-op UPDATE (an UPDATE
211666** that modifies no columns), this function sets (*pbEmpty) to 1.
211667**
211668** * If the iterator is configured to skip no-op UPDATEs,
211669** sessionChangesetNext() does that. This function does not.
211670*/
211671static int sessionChangesetNextOne(
211672 sqlite3_changeset_iter *p, /* Changeset iterator */
211673 u8 **paRec, /* If non-NULL, store record pointer here */
211674 int *pnRec, /* If non-NULL, store size of record here */
211675 int *pbNew, /* If non-NULL, true if new table */
211676 int *pbEmpty
211677){
211678 int i;
211679 u8 op;
211680
211681 assert( (paRec==0 && pnRec==0) || (paRec && pnRec) );
211682 assert( pbEmpty==0 || *pbEmpty==0 );
211683
211684 /* If the iterator is in the error-state, return immediately. */
211685 if( p->rc!=SQLITE_OK ) return p->rc;
211686
211687 /* Free the current contents of p->apValue[], if any. */
211688 if( p->apValue ){
211689 for(i=0; i<p->nCol*2; i++){
211690 sqlite3ValueFree(p->apValue[i]);
211691 }
211692 memset(p->apValue, 0, sizeof(sqlite3_value*)*p->nCol*2);
211693 }
211694
211695 /* Make sure the buffer contains at least 10 bytes of input data, or all
211696 ** remaining data if there are less than 10 bytes available. This is
211697 ** sufficient either for the 'T' or 'P' byte and the varint that follows
211698 ** it, or for the two single byte values otherwise. */
211699 p->rc = sessionInputBuffer(&p->in, 2);
211700 if( p->rc!=SQLITE_OK ) return p->rc;
211701
211702 /* If the iterator is already at the end of the changeset, return DONE. */
211703 if( p->in.iNext>=p->in.nData ){
211704 return SQLITE_DONE;
211705 }
211706
211707 sessionDiscardData(&p->in);
211708 p->in.iCurrent = p->in.iNext;
211709
211710 op = p->in.aData[p->in.iNext++];
211711 while( op=='T' || op=='P' ){
211712 if( pbNew ) *pbNew = 1;
211713 p->bPatchset = (op=='P');
211714 if( sessionChangesetReadTblhdr(p) ) return p->rc;
211715 if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc;
211716 p->in.iCurrent = p->in.iNext;
211717 if( p->in.iNext>=p->in.nData ) return SQLITE_DONE;
211718 op = p->in.aData[p->in.iNext++];
211719 }
211720
211721 if( p->zTab==0 || (p->bPatchset && p->bInvert) ){
211722 /* The first record in the changeset is not a table header. Must be a
211723 ** corrupt changeset. */
211724 assert( p->in.iNext==1 || p->zTab );
211725 return (p->rc = SQLITE_CORRUPT_BKPT);
211726 }
211727
211728 p->op = op;
211729 p->bIndirect = p->in.aData[p->in.iNext++];
211730 if( p->op!=SQLITE_UPDATE && p->op!=SQLITE_DELETE && p->op!=SQLITE_INSERT ){
211731 return (p->rc = SQLITE_CORRUPT_BKPT);
211732 }
211733
211734 if( paRec ){
211735 int nVal; /* Number of values to buffer */
211736 if( p->bPatchset==0 && op==SQLITE_UPDATE ){
211737 nVal = p->nCol * 2;
211738 }else if( p->bPatchset && op==SQLITE_DELETE ){
211739 nVal = 0;
211740 for(i=0; i<p->nCol; i++) if( p->abPK[i] ) nVal++;
211741 }else{
211742 nVal = p->nCol;
211743 }
211744 p->rc = sessionChangesetBufferRecord(&p->in, nVal, pnRec);
211745 if( p->rc!=SQLITE_OK ) return p->rc;
211746 *paRec = &p->in.aData[p->in.iNext];
211747 p->in.iNext += *pnRec;
211748 }else{
211749 sqlite3_value **apOld = (p->bInvert ? &p->apValue[p->nCol] : p->apValue);
211750 sqlite3_value **apNew = (p->bInvert ? p->apValue : &p->apValue[p->nCol]);
211751
211752 /* If this is an UPDATE or DELETE, read the old.* record. */
211753 if( p->op!=SQLITE_INSERT && (p->bPatchset==0 || p->op==SQLITE_DELETE) ){
211754 u8 *abPK = p->bPatchset ? p->abPK : 0;
211755 p->rc = sessionReadRecord(&p->in, p->nCol, abPK, apOld, 0);
211756 if( p->rc!=SQLITE_OK ) return p->rc;
211757 }
211758
211759 /* If this is an INSERT or UPDATE, read the new.* record. */
211760 if( p->op!=SQLITE_DELETE ){
211761 p->rc = sessionReadRecord(&p->in, p->nCol, 0, apNew, pbEmpty);
211762 if( p->rc!=SQLITE_OK ) return p->rc;
211763 }
211764
211765 if( (p->bPatchset || p->bInvert) && p->op==SQLITE_UPDATE ){
211766 /* If this is an UPDATE that is part of a patchset, then all PK and
211767 ** modified fields are present in the new.* record. The old.* record
211768 ** is currently completely empty. This block shifts the PK fields from
211769 ** new.* to old.*, to accommodate the code that reads these arrays. */
211770 for(i=0; i<p->nCol; i++){
211771 assert( p->bPatchset==0 || p->apValue[i]==0 );
211772 if( p->abPK[i] ){
211773 assert( p->apValue[i]==0 );
211774 p->apValue[i] = p->apValue[i+p->nCol];
211775 if( p->apValue[i]==0 ) return (p->rc = SQLITE_CORRUPT_BKPT);
211776 p->apValue[i+p->nCol] = 0;
211777 }
211778 }
211779 }else if( p->bInvert ){
211780 if( p->op==SQLITE_INSERT ) p->op = SQLITE_DELETE;
211781 else if( p->op==SQLITE_DELETE ) p->op = SQLITE_INSERT;
211782 }
211783 }
211784
211785 return SQLITE_ROW;
211786}
211787
211788/*
211789** Advance the changeset iterator to the next change.
211790**
211791** If both paRec and pnRec are NULL, then this function works like the public
211792** API sqlite3changeset_next(). If SQLITE_ROW is returned, then the
211793** sqlite3changeset_new() and old() APIs may be used to query for values.
211794**
211795** Otherwise, if paRec and pnRec are not NULL, then a pointer to the change
211796** record is written to *paRec before returning and the number of bytes in
211797** the record to *pnRec.
211798**
211799** Either way, this function returns SQLITE_ROW if the iterator is
211800** successfully advanced to the next change in the changeset, an SQLite
211801** error code if an error occurs, or SQLITE_DONE if there are no further
211802** changes in the changeset.
211803*/
211804static int sessionChangesetNext(
211805 sqlite3_changeset_iter *p, /* Changeset iterator */
211806 u8 **paRec, /* If non-NULL, store record pointer here */
211807 int *pnRec, /* If non-NULL, store size of record here */
211808 int *pbNew /* If non-NULL, true if new table */
211809){
211810 int bEmpty;
211811 int rc;
211812 do {
211813 bEmpty = 0;
211814 rc = sessionChangesetNextOne(p, paRec, pnRec, pbNew, &bEmpty);
211815 }while( rc==SQLITE_ROW && p->bSkipEmpty && bEmpty);
211816 return rc;
211817}
211818
211819/*
211820** Advance an iterator created by sqlite3changeset_start() to the next
211821** change in the changeset. This function may return SQLITE_ROW, SQLITE_DONE
211822** or SQLITE_CORRUPT.
211823**
211824** This function may not be called on iterators passed to a conflict handler
211825** callback by changeset_apply().
211826*/
211827SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *p){
211828 return sessionChangesetNext(p, 0, 0, 0);
211829}
211830
211831/*
211832** The following function extracts information on the current change
211833** from a changeset iterator. It may only be called after changeset_next()
211834** has returned SQLITE_ROW.
211835*/
211836SQLITE_API int sqlite3changeset_op(
211837 sqlite3_changeset_iter *pIter, /* Iterator handle */
211838 const char **pzTab, /* OUT: Pointer to table name */
211839 int *pnCol, /* OUT: Number of columns in table */
211840 int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */
211841 int *pbIndirect /* OUT: True if change is indirect */
211842){
211843 *pOp = pIter->op;
211844 *pnCol = pIter->nCol;
211845 *pzTab = pIter->zTab;
211846 if( pbIndirect ) *pbIndirect = pIter->bIndirect;
211847 return SQLITE_OK;
211848}
211849
211850/*
211851** Return information regarding the PRIMARY KEY and number of columns in
211852** the database table affected by the change that pIter currently points
211853** to. This function may only be called after changeset_next() returns
211854** SQLITE_ROW.
211855*/
211856SQLITE_API int sqlite3changeset_pk(
211857 sqlite3_changeset_iter *pIter, /* Iterator object */
211858 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
211859 int *pnCol /* OUT: Number of entries in output array */
211860){
211861 *pabPK = pIter->abPK;
211862 if( pnCol ) *pnCol = pIter->nCol;
211863 return SQLITE_OK;
211864}
211865
211866/*
211867** This function may only be called while the iterator is pointing to an
211868** SQLITE_UPDATE or SQLITE_DELETE change (see sqlite3changeset_op()).
211869** Otherwise, SQLITE_MISUSE is returned.
211870**
211871** It sets *ppValue to point to an sqlite3_value structure containing the
211872** iVal'th value in the old.* record. Or, if that particular value is not
211873** included in the record (because the change is an UPDATE and the field
211874** was not modified and is not a PK column), set *ppValue to NULL.
211875**
211876** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
211877** not modified. Otherwise, SQLITE_OK.
211878*/
211879SQLITE_API int sqlite3changeset_old(
211880 sqlite3_changeset_iter *pIter, /* Changeset iterator */
211881 int iVal, /* Index of old.* value to retrieve */
211882 sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
211883){
211884 if( pIter->op!=SQLITE_UPDATE && pIter->op!=SQLITE_DELETE ){
211885 return SQLITE_MISUSE;
211886 }
211887 if( iVal<0 || iVal>=pIter->nCol ){
211888 return SQLITE_RANGE;
211889 }
211890 *ppValue = pIter->apValue[iVal];
211891 return SQLITE_OK;
211892}
211893
211894/*
211895** This function may only be called while the iterator is pointing to an
211896** SQLITE_UPDATE or SQLITE_INSERT change (see sqlite3changeset_op()).
211897** Otherwise, SQLITE_MISUSE is returned.
211898**
211899** It sets *ppValue to point to an sqlite3_value structure containing the
211900** iVal'th value in the new.* record. Or, if that particular value is not
211901** included in the record (because the change is an UPDATE and the field
211902** was not modified), set *ppValue to NULL.
211903**
211904** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
211905** not modified. Otherwise, SQLITE_OK.
211906*/
211907SQLITE_API int sqlite3changeset_new(
211908 sqlite3_changeset_iter *pIter, /* Changeset iterator */
211909 int iVal, /* Index of new.* value to retrieve */
211910 sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
211911){
211912 if( pIter->op!=SQLITE_UPDATE && pIter->op!=SQLITE_INSERT ){
211913 return SQLITE_MISUSE;
211914 }
211915 if( iVal<0 || iVal>=pIter->nCol ){
211916 return SQLITE_RANGE;
211917 }
211918 *ppValue = pIter->apValue[pIter->nCol+iVal];
211919 return SQLITE_OK;
211920}
211921
211922/*
211923** The following two macros are used internally. They are similar to the
211924** sqlite3changeset_new() and sqlite3changeset_old() functions, except that
211925** they omit all error checking and return a pointer to the requested value.
211926*/
211927#define sessionChangesetNew(pIter, iVal) (pIter)->apValue[(pIter)->nCol+(iVal)]
211928#define sessionChangesetOld(pIter, iVal) (pIter)->apValue[(iVal)]
211929
211930/*
211931** This function may only be called with a changeset iterator that has been
211932** passed to an SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT
211933** conflict-handler function. Otherwise, SQLITE_MISUSE is returned.
211934**
211935** If successful, *ppValue is set to point to an sqlite3_value structure
211936** containing the iVal'th value of the conflicting record.
211937**
211938** If value iVal is out-of-range or some other error occurs, an SQLite error
211939** code is returned. Otherwise, SQLITE_OK.
211940*/
211941SQLITE_API int sqlite3changeset_conflict(
211942 sqlite3_changeset_iter *pIter, /* Changeset iterator */
211943 int iVal, /* Index of conflict record value to fetch */
211944 sqlite3_value **ppValue /* OUT: Value from conflicting row */
211945){
211946 if( !pIter->pConflict ){
211947 return SQLITE_MISUSE;
211948 }
211949 if( iVal<0 || iVal>=pIter->nCol ){
211950 return SQLITE_RANGE;
211951 }
211952 *ppValue = sqlite3_column_value(pIter->pConflict, iVal);
211953 return SQLITE_OK;
211954}
211955
211956/*
211957** This function may only be called with an iterator passed to an
211958** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
211959** it sets the output variable to the total number of known foreign key
211960** violations in the destination database and returns SQLITE_OK.
211961**
211962** In all other cases this function returns SQLITE_MISUSE.
211963*/
211964SQLITE_API int sqlite3changeset_fk_conflicts(
211965 sqlite3_changeset_iter *pIter, /* Changeset iterator */
211966 int *pnOut /* OUT: Number of FK violations */
211967){
211968 if( pIter->pConflict || pIter->apValue ){
211969 return SQLITE_MISUSE;
211970 }
211971 *pnOut = pIter->nCol;
211972 return SQLITE_OK;
211973}
211974
211975
211976/*
211977** Finalize an iterator allocated with sqlite3changeset_start().
211978**
211979** This function may not be called on iterators passed to a conflict handler
211980** callback by changeset_apply().
211981*/
211982SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *p){
211983 int rc = SQLITE_OK;
211984 if( p ){
211985 int i; /* Used to iterate through p->apValue[] */
211986 rc = p->rc;
211987 if( p->apValue ){
211988 for(i=0; i<p->nCol*2; i++) sqlite3ValueFree(p->apValue[i]);
211989 }
211990 sqlite3_free(p->tblhdr.aBuf);
211991 sqlite3_free(p->in.buf.aBuf);
211992 sqlite3_free(p);
211993 }
211994 return rc;
211995}
211996
211997static int sessionChangesetInvert(
211998 SessionInput *pInput, /* Input changeset */
211999 int (*xOutput)(void *pOut, const void *pData, int nData),
212000 void *pOut,
212001 int *pnInverted, /* OUT: Number of bytes in output changeset */
212002 void **ppInverted /* OUT: Inverse of pChangeset */
212003){
212004 int rc = SQLITE_OK; /* Return value */
212005 SessionBuffer sOut; /* Output buffer */
212006 int nCol = 0; /* Number of cols in current table */
212007 u8 *abPK = 0; /* PK array for current table */
212008 sqlite3_value **apVal = 0; /* Space for values for UPDATE inversion */
212009 SessionBuffer sPK = {0, 0, 0}; /* PK array for current table */
212010
212011 /* Initialize the output buffer */
212012 memset(&sOut, 0, sizeof(SessionBuffer));
212013
212014 /* Zero the output variables in case an error occurs. */
212015 if( ppInverted ){
212016 *ppInverted = 0;
212017 *pnInverted = 0;
212018 }
212019
212020 while( 1 ){
212021 u8 eType;
212022
212023 /* Test for EOF. */
212024 if( (rc = sessionInputBuffer(pInput, 2)) ) goto finished_invert;
212025 if( pInput->iNext>=pInput->nData ) break;
212026 eType = pInput->aData[pInput->iNext];
212027
212028 switch( eType ){
212029 case 'T': {
212030 /* A 'table' record consists of:
212031 **
212032 ** * A constant 'T' character,
212033 ** * Number of columns in said table (a varint),
212034 ** * An array of nCol bytes (sPK),
212035 ** * A nul-terminated table name.
212036 */
212037 int nByte;
212038 int nVar;
212039 pInput->iNext++;
212040 if( (rc = sessionChangesetBufferTblhdr(pInput, &nByte)) ){
212041 goto finished_invert;
212042 }
212043 nVar = sessionVarintGet(&pInput->aData[pInput->iNext], &nCol);
212044 sPK.nBuf = 0;
212045 sessionAppendBlob(&sPK, &pInput->aData[pInput->iNext+nVar], nCol, &rc);
212046 sessionAppendByte(&sOut, eType, &rc);
212047 sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
212048 if( rc ) goto finished_invert;
212049
212050 pInput->iNext += nByte;
212051 sqlite3_free(apVal);
212052 apVal = 0;
212053 abPK = sPK.aBuf;
212054 break;
212055 }
212056
212057 case SQLITE_INSERT:
212058 case SQLITE_DELETE: {
212059 int nByte;
212060 int bIndirect = pInput->aData[pInput->iNext+1];
212061 int eType2 = (eType==SQLITE_DELETE ? SQLITE_INSERT : SQLITE_DELETE);
212062 pInput->iNext += 2;
212063 assert( rc==SQLITE_OK );
212064 rc = sessionChangesetBufferRecord(pInput, nCol, &nByte);
212065 sessionAppendByte(&sOut, eType2, &rc);
212066 sessionAppendByte(&sOut, bIndirect, &rc);
212067 sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
212068 pInput->iNext += nByte;
212069 if( rc ) goto finished_invert;
212070 break;
212071 }
212072
212073 case SQLITE_UPDATE: {
212074 int iCol;
212075
212076 if( 0==apVal ){
212077 apVal = (sqlite3_value **)sqlite3_malloc64(sizeof(apVal[0])*nCol*2);
212078 if( 0==apVal ){
212079 rc = SQLITE_NOMEM;
212080 goto finished_invert;
212081 }
212082 memset(apVal, 0, sizeof(apVal[0])*nCol*2);
212083 }
212084
212085 /* Write the header for the new UPDATE change. Same as the original. */
212086 sessionAppendByte(&sOut, eType, &rc);
212087 sessionAppendByte(&sOut, pInput->aData[pInput->iNext+1], &rc);
212088
212089 /* Read the old.* and new.* records for the update change. */
212090 pInput->iNext += 2;
212091 rc = sessionReadRecord(pInput, nCol, 0, &apVal[0], 0);
212092 if( rc==SQLITE_OK ){
212093 rc = sessionReadRecord(pInput, nCol, 0, &apVal[nCol], 0);
212094 }
212095
212096 /* Write the new old.* record. Consists of the PK columns from the
212097 ** original old.* record, and the other values from the original
212098 ** new.* record. */
212099 for(iCol=0; iCol<nCol; iCol++){
212100 sqlite3_value *pVal = apVal[iCol + (abPK[iCol] ? 0 : nCol)];
212101 sessionAppendValue(&sOut, pVal, &rc);
212102 }
212103
212104 /* Write the new new.* record. Consists of a copy of all values
212105 ** from the original old.* record, except for the PK columns, which
212106 ** are set to "undefined". */
212107 for(iCol=0; iCol<nCol; iCol++){
212108 sqlite3_value *pVal = (abPK[iCol] ? 0 : apVal[iCol]);
212109 sessionAppendValue(&sOut, pVal, &rc);
212110 }
212111
212112 for(iCol=0; iCol<nCol*2; iCol++){
212113 sqlite3ValueFree(apVal[iCol]);
212114 }
212115 memset(apVal, 0, sizeof(apVal[0])*nCol*2);
212116 if( rc!=SQLITE_OK ){
212117 goto finished_invert;
212118 }
212119
212120 break;
212121 }
212122
212123 default:
212124 rc = SQLITE_CORRUPT_BKPT;
212125 goto finished_invert;
212126 }
212127
212128 assert( rc==SQLITE_OK );
212129 if( xOutput && sOut.nBuf>=sessions_strm_chunk_size ){
212130 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
212131 sOut.nBuf = 0;
212132 if( rc!=SQLITE_OK ) goto finished_invert;
212133 }
212134 }
212135
212136 assert( rc==SQLITE_OK );
212137 if( pnInverted && ALWAYS(ppInverted) ){
212138 *pnInverted = sOut.nBuf;
212139 *ppInverted = sOut.aBuf;
212140 sOut.aBuf = 0;
212141 }else if( sOut.nBuf>0 && ALWAYS(xOutput!=0) ){
212142 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
212143 }
212144
212145 finished_invert:
212146 sqlite3_free(sOut.aBuf);
212147 sqlite3_free(apVal);
212148 sqlite3_free(sPK.aBuf);
212149 return rc;
212150}
212151
212152
212153/*
212154** Invert a changeset object.
212155*/
212156SQLITE_API int sqlite3changeset_invert(
212157 int nChangeset, /* Number of bytes in input */
212158 const void *pChangeset, /* Input changeset */
212159 int *pnInverted, /* OUT: Number of bytes in output changeset */
212160 void **ppInverted /* OUT: Inverse of pChangeset */
212161){
212162 SessionInput sInput;
212163
212164 /* Set up the input stream */
212165 memset(&sInput, 0, sizeof(SessionInput));
212166 sInput.nData = nChangeset;
212167 sInput.aData = (u8*)pChangeset;
212168
212169 return sessionChangesetInvert(&sInput, 0, 0, pnInverted, ppInverted);
212170}
212171
212172/*
212173** Streaming version of sqlite3changeset_invert().
212174*/
212175SQLITE_API int sqlite3changeset_invert_strm(
212176 int (*xInput)(void *pIn, void *pData, int *pnData),
212177 void *pIn,
212178 int (*xOutput)(void *pOut, const void *pData, int nData),
212179 void *pOut
212180){
212181 SessionInput sInput;
212182 int rc;
212183
212184 /* Set up the input stream */
212185 memset(&sInput, 0, sizeof(SessionInput));
212186 sInput.xInput = xInput;
212187 sInput.pIn = pIn;
212188
212189 rc = sessionChangesetInvert(&sInput, xOutput, pOut, 0, 0);
212190 sqlite3_free(sInput.buf.aBuf);
212191 return rc;
212192}
212193
212194
212195typedef struct SessionUpdate SessionUpdate;
212196struct SessionUpdate {
212197 sqlite3_stmt *pStmt;
212198 u32 *aMask;
212199 SessionUpdate *pNext;
212200};
212201
212202typedef struct SessionApplyCtx SessionApplyCtx;
212203struct SessionApplyCtx {
212204 sqlite3 *db;
212205 sqlite3_stmt *pDelete; /* DELETE statement */
212206 sqlite3_stmt *pInsert; /* INSERT statement */
212207 sqlite3_stmt *pSelect; /* SELECT statement */
212208 int nCol; /* Size of azCol[] and abPK[] arrays */
212209 const char **azCol; /* Array of column names */
212210 u8 *abPK; /* Boolean array - true if column is in PK */
212211 u32 *aUpdateMask; /* Used by sessionUpdateFind */
212212 SessionUpdate *pUp;
212213 int bStat1; /* True if table is sqlite_stat1 */
212214 int bDeferConstraints; /* True to defer constraints */
212215 int bInvertConstraints; /* Invert when iterating constraints buffer */
212216 SessionBuffer constraints; /* Deferred constraints are stored here */
212217 SessionBuffer rebase; /* Rebase information (if any) here */
212218 u8 bRebaseStarted; /* If table header is already in rebase */
212219 u8 bRebase; /* True to collect rebase information */
212220};
212221
212222/* Number of prepared UPDATE statements to cache. */
212223#define SESSION_UPDATE_CACHE_SZ 12
212224
212225/*
212226** Find a prepared UPDATE statement suitable for the UPDATE step currently
212227** being visited by the iterator. The UPDATE is of the form:
212228**
212229** UPDATE tbl SET col = ?, col2 = ? WHERE pk1 IS ? AND pk2 IS ?
212230*/
212231static int sessionUpdateFind(
212232 sqlite3_changeset_iter *pIter,
212233 SessionApplyCtx *p,
212234 int bPatchset,
212235 sqlite3_stmt **ppStmt
212236){
212237 int rc = SQLITE_OK;
212238 SessionUpdate *pUp = 0;
212239 int nCol = pIter->nCol;
212240 int nU32 = (pIter->nCol+33)/32;
212241 int ii;
212242
212243 if( p->aUpdateMask==0 ){
212244 p->aUpdateMask = sqlite3_malloc(nU32*sizeof(u32));
212245 if( p->aUpdateMask==0 ){
212246 rc = SQLITE_NOMEM;
212247 }
212248 }
212249
212250 if( rc==SQLITE_OK ){
212251 memset(p->aUpdateMask, 0, nU32*sizeof(u32));
212252 rc = SQLITE_CORRUPT;
212253 for(ii=0; ii<pIter->nCol; ii++){
212254 if( sessionChangesetNew(pIter, ii) ){
212255 p->aUpdateMask[ii/32] |= (1<<(ii%32));
212256 rc = SQLITE_OK;
212257 }
212258 }
212259 }
212260
212261 if( rc==SQLITE_OK ){
212262 if( bPatchset ) p->aUpdateMask[nCol/32] |= (1<<(nCol%32));
212263
212264 if( p->pUp ){
212265 int nUp = 0;
212266 SessionUpdate **pp = &p->pUp;
212267 while( 1 ){
212268 nUp++;
212269 if( 0==memcmp(p->aUpdateMask, (*pp)->aMask, nU32*sizeof(u32)) ){
212270 pUp = *pp;
212271 *pp = pUp->pNext;
212272 pUp->pNext = p->pUp;
212273 p->pUp = pUp;
212274 break;
212275 }
212276
212277 if( (*pp)->pNext ){
212278 pp = &(*pp)->pNext;
212279 }else{
212280 if( nUp>=SESSION_UPDATE_CACHE_SZ ){
212281 sqlite3_finalize((*pp)->pStmt);
212282 sqlite3_free(*pp);
212283 *pp = 0;
212284 }
212285 break;
212286 }
212287 }
212288 }
212289
212290 if( pUp==0 ){
212291 int nByte = sizeof(SessionUpdate) * nU32*sizeof(u32);
212292 int bStat1 = (sqlite3_stricmp(pIter->zTab, "sqlite_stat1")==0);
212293 pUp = (SessionUpdate*)sqlite3_malloc(nByte);
212294 if( pUp==0 ){
212295 rc = SQLITE_NOMEM;
212296 }else{
212297 const char *zSep = "";
212298 SessionBuffer buf;
212299
212300 memset(&buf, 0, sizeof(buf));
212301 pUp->aMask = (u32*)&pUp[1];
212302 memcpy(pUp->aMask, p->aUpdateMask, nU32*sizeof(u32));
212303
212304 sessionAppendStr(&buf, "UPDATE main.", &rc);
212305 sessionAppendIdent(&buf, pIter->zTab, &rc);
212306 sessionAppendStr(&buf, " SET ", &rc);
212307
212308 /* Create the assignments part of the UPDATE */
212309 for(ii=0; ii<pIter->nCol; ii++){
212310 if( p->abPK[ii]==0 && sessionChangesetNew(pIter, ii) ){
212311 sessionAppendStr(&buf, zSep, &rc);
212312 sessionAppendIdent(&buf, p->azCol[ii], &rc);
212313 sessionAppendStr(&buf, " = ?", &rc);
212314 sessionAppendInteger(&buf, ii*2+1, &rc);
212315 zSep = ", ";
212316 }
212317 }
212318
212319 /* Create the WHERE clause part of the UPDATE */
212320 zSep = "";
212321 sessionAppendStr(&buf, " WHERE ", &rc);
212322 for(ii=0; ii<pIter->nCol; ii++){
212323 if( p->abPK[ii] || (bPatchset==0 && sessionChangesetOld(pIter, ii)) ){
212324 sessionAppendStr(&buf, zSep, &rc);
212325 if( bStat1 && ii==1 ){
212326 assert( sqlite3_stricmp(p->azCol[ii], "idx")==0 );
212327 sessionAppendStr(&buf,
212328 "idx IS CASE "
212329 "WHEN length(?4)=0 AND typeof(?4)='blob' THEN NULL "
212330 "ELSE ?4 END ", &rc
212331 );
212332 }else{
212333 sessionAppendIdent(&buf, p->azCol[ii], &rc);
212334 sessionAppendStr(&buf, " IS ?", &rc);
212335 sessionAppendInteger(&buf, ii*2+2, &rc);
212336 }
212337 zSep = " AND ";
212338 }
212339 }
212340
212341 if( rc==SQLITE_OK ){
212342 char *zSql = (char*)buf.aBuf;
212343 rc = sqlite3_prepare_v2(p->db, zSql, buf.nBuf, &pUp->pStmt, 0);
212344 }
212345
212346 if( rc!=SQLITE_OK ){
212347 sqlite3_free(pUp);
212348 pUp = 0;
212349 }else{
212350 pUp->pNext = p->pUp;
212351 p->pUp = pUp;
212352 }
212353 sqlite3_free(buf.aBuf);
212354 }
212355 }
212356 }
212357
212358 assert( (rc==SQLITE_OK)==(pUp!=0) );
212359 if( pUp ){
212360 *ppStmt = pUp->pStmt;
212361 }else{
212362 *ppStmt = 0;
212363 }
212364 return rc;
212365}
212366
212367/*
212368** Free all cached UPDATE statements.
212369*/
212370static void sessionUpdateFree(SessionApplyCtx *p){
212371 SessionUpdate *pUp;
212372 SessionUpdate *pNext;
212373 for(pUp=p->pUp; pUp; pUp=pNext){
212374 pNext = pUp->pNext;
212375 sqlite3_finalize(pUp->pStmt);
212376 sqlite3_free(pUp);
212377 }
212378 p->pUp = 0;
212379 sqlite3_free(p->aUpdateMask);
212380 p->aUpdateMask = 0;
212381}
212382
212383/*
212384** Formulate a statement to DELETE a row from database db. Assuming a table
212385** structure like this:
212386**
212387** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
212388**
212389** The DELETE statement looks like this:
212390**
212391** DELETE FROM x WHERE a = :1 AND c = :3 AND (:5 OR b IS :2 AND d IS :4)
212392**
212393** Variable :5 (nCol+1) is a boolean. It should be set to 0 if we require
212394** matching b and d values, or 1 otherwise. The second case comes up if the
212395** conflict handler is invoked with NOTFOUND and returns CHANGESET_REPLACE.
212396**
212397** If successful, SQLITE_OK is returned and SessionApplyCtx.pDelete is left
212398** pointing to the prepared version of the SQL statement.
212399*/
212400static int sessionDeleteRow(
212401 sqlite3 *db, /* Database handle */
212402 const char *zTab, /* Table name */
212403 SessionApplyCtx *p /* Session changeset-apply context */
212404){
212405 int i;
212406 const char *zSep = "";
212407 int rc = SQLITE_OK;
212408 SessionBuffer buf = {0, 0, 0};
212409 int nPk = 0;
212410
212411 sessionAppendStr(&buf, "DELETE FROM main.", &rc);
212412 sessionAppendIdent(&buf, zTab, &rc);
212413 sessionAppendStr(&buf, " WHERE ", &rc);
212414
212415 for(i=0; i<p->nCol; i++){
212416 if( p->abPK[i] ){
212417 nPk++;
212418 sessionAppendStr(&buf, zSep, &rc);
212419 sessionAppendIdent(&buf, p->azCol[i], &rc);
212420 sessionAppendStr(&buf, " = ?", &rc);
212421 sessionAppendInteger(&buf, i+1, &rc);
212422 zSep = " AND ";
212423 }
212424 }
212425
212426 if( nPk<p->nCol ){
212427 sessionAppendStr(&buf, " AND (?", &rc);
212428 sessionAppendInteger(&buf, p->nCol+1, &rc);
212429 sessionAppendStr(&buf, " OR ", &rc);
212430
212431 zSep = "";
212432 for(i=0; i<p->nCol; i++){
212433 if( !p->abPK[i] ){
212434 sessionAppendStr(&buf, zSep, &rc);
212435 sessionAppendIdent(&buf, p->azCol[i], &rc);
212436 sessionAppendStr(&buf, " IS ?", &rc);
212437 sessionAppendInteger(&buf, i+1, &rc);
212438 zSep = "AND ";
212439 }
212440 }
212441 sessionAppendStr(&buf, ")", &rc);
212442 }
212443
212444 if( rc==SQLITE_OK ){
212445 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pDelete, 0);
212446 }
212447 sqlite3_free(buf.aBuf);
212448
212449 return rc;
212450}
212451
212452/*
212453** Formulate and prepare an SQL statement to query table zTab by primary
212454** key. Assuming the following table structure:
212455**
212456** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
212457**
212458** The SELECT statement looks like this:
212459**
212460** SELECT * FROM x WHERE a = ?1 AND c = ?3
212461**
212462** If successful, SQLITE_OK is returned and SessionApplyCtx.pSelect is left
212463** pointing to the prepared version of the SQL statement.
212464*/
212465static int sessionSelectRow(
212466 sqlite3 *db, /* Database handle */
212467 const char *zTab, /* Table name */
212468 SessionApplyCtx *p /* Session changeset-apply context */
212469){
212470 return sessionSelectStmt(
212471 db, "main", zTab, p->nCol, p->azCol, p->abPK, &p->pSelect);
212472}
212473
212474/*
212475** Formulate and prepare an INSERT statement to add a record to table zTab.
212476** For example:
212477**
212478** INSERT INTO main."zTab" VALUES(?1, ?2, ?3 ...);
212479**
212480** If successful, SQLITE_OK is returned and SessionApplyCtx.pInsert is left
212481** pointing to the prepared version of the SQL statement.
212482*/
212483static int sessionInsertRow(
212484 sqlite3 *db, /* Database handle */
212485 const char *zTab, /* Table name */
212486 SessionApplyCtx *p /* Session changeset-apply context */
212487){
212488 int rc = SQLITE_OK;
212489 int i;
212490 SessionBuffer buf = {0, 0, 0};
212491
212492 sessionAppendStr(&buf, "INSERT INTO main.", &rc);
212493 sessionAppendIdent(&buf, zTab, &rc);
212494 sessionAppendStr(&buf, "(", &rc);
212495 for(i=0; i<p->nCol; i++){
212496 if( i!=0 ) sessionAppendStr(&buf, ", ", &rc);
212497 sessionAppendIdent(&buf, p->azCol[i], &rc);
212498 }
212499
212500 sessionAppendStr(&buf, ") VALUES(?", &rc);
212501 for(i=1; i<p->nCol; i++){
212502 sessionAppendStr(&buf, ", ?", &rc);
212503 }
212504 sessionAppendStr(&buf, ")", &rc);
212505
212506 if( rc==SQLITE_OK ){
212507 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pInsert, 0);
212508 }
212509 sqlite3_free(buf.aBuf);
212510 return rc;
212511}
212512
212513static int sessionPrepare(sqlite3 *db, sqlite3_stmt **pp, const char *zSql){
212514 return sqlite3_prepare_v2(db, zSql, -1, pp, 0);
212515}
212516
212517/*
212518** Prepare statements for applying changes to the sqlite_stat1 table.
212519** These are similar to those created by sessionSelectRow(),
212520** sessionInsertRow(), sessionUpdateRow() and sessionDeleteRow() for
212521** other tables.
212522*/
212523static int sessionStat1Sql(sqlite3 *db, SessionApplyCtx *p){
212524 int rc = sessionSelectRow(db, "sqlite_stat1", p);
212525 if( rc==SQLITE_OK ){
212526 rc = sessionPrepare(db, &p->pInsert,
212527 "INSERT INTO main.sqlite_stat1 VALUES(?1, "
212528 "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END, "
212529 "?3)"
212530 );
212531 }
212532 if( rc==SQLITE_OK ){
212533 rc = sessionPrepare(db, &p->pDelete,
212534 "DELETE FROM main.sqlite_stat1 WHERE tbl=?1 AND idx IS "
212535 "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END "
212536 "AND (?4 OR stat IS ?3)"
212537 );
212538 }
212539 return rc;
212540}
212541
212542/*
212543** A wrapper around sqlite3_bind_value() that detects an extra problem.
212544** See comments in the body of this function for details.
212545*/
212546static int sessionBindValue(
212547 sqlite3_stmt *pStmt, /* Statement to bind value to */
212548 int i, /* Parameter number to bind to */
212549 sqlite3_value *pVal /* Value to bind */
212550){
212551 int eType = sqlite3_value_type(pVal);
212552 /* COVERAGE: The (pVal->z==0) branch is never true using current versions
212553 ** of SQLite. If a malloc fails in an sqlite3_value_xxx() function, either
212554 ** the (pVal->z) variable remains as it was or the type of the value is
212555 ** set to SQLITE_NULL. */
212556 if( (eType==SQLITE_TEXT || eType==SQLITE_BLOB) && pVal->z==0 ){
212557 /* This condition occurs when an earlier OOM in a call to
212558 ** sqlite3_value_text() or sqlite3_value_blob() (perhaps from within
212559 ** a conflict-handler) has zeroed the pVal->z pointer. Return NOMEM. */
212560 return SQLITE_NOMEM;
212561 }
212562 return sqlite3_bind_value(pStmt, i, pVal);
212563}
212564
212565/*
212566** Iterator pIter must point to an SQLITE_INSERT entry. This function
212567** transfers new.* values from the current iterator entry to statement
212568** pStmt. The table being inserted into has nCol columns.
212569**
212570** New.* value $i from the iterator is bound to variable ($i+1) of
212571** statement pStmt. If parameter abPK is NULL, all values from 0 to (nCol-1)
212572** are transfered to the statement. Otherwise, if abPK is not NULL, it points
212573** to an array nCol elements in size. In this case only those values for
212574** which abPK[$i] is true are read from the iterator and bound to the
212575** statement.
212576**
212577** An SQLite error code is returned if an error occurs. Otherwise, SQLITE_OK.
212578*/
212579static int sessionBindRow(
212580 sqlite3_changeset_iter *pIter, /* Iterator to read values from */
212581 int(*xValue)(sqlite3_changeset_iter *, int, sqlite3_value **),
212582 int nCol, /* Number of columns */
212583 u8 *abPK, /* If not NULL, bind only if true */
212584 sqlite3_stmt *pStmt /* Bind values to this statement */
212585){
212586 int i;
212587 int rc = SQLITE_OK;
212588
212589 /* Neither sqlite3changeset_old or sqlite3changeset_new can fail if the
212590 ** argument iterator points to a suitable entry. Make sure that xValue
212591 ** is one of these to guarantee that it is safe to ignore the return
212592 ** in the code below. */
212593 assert( xValue==sqlite3changeset_old || xValue==sqlite3changeset_new );
212594
212595 for(i=0; rc==SQLITE_OK && i<nCol; i++){
212596 if( !abPK || abPK[i] ){
212597 sqlite3_value *pVal = 0;
212598 (void)xValue(pIter, i, &pVal);
212599 if( pVal==0 ){
212600 /* The value in the changeset was "undefined". This indicates a
212601 ** corrupt changeset blob. */
212602 rc = SQLITE_CORRUPT_BKPT;
212603 }else{
212604 rc = sessionBindValue(pStmt, i+1, pVal);
212605 }
212606 }
212607 }
212608 return rc;
212609}
212610
212611/*
212612** SQL statement pSelect is as generated by the sessionSelectRow() function.
212613** This function binds the primary key values from the change that changeset
212614** iterator pIter points to to the SELECT and attempts to seek to the table
212615** entry. If a row is found, the SELECT statement left pointing at the row
212616** and SQLITE_ROW is returned. Otherwise, if no row is found and no error
212617** has occured, the statement is reset and SQLITE_OK is returned. If an
212618** error occurs, the statement is reset and an SQLite error code is returned.
212619**
212620** If this function returns SQLITE_ROW, the caller must eventually reset()
212621** statement pSelect. If any other value is returned, the statement does
212622** not require a reset().
212623**
212624** If the iterator currently points to an INSERT record, bind values from the
212625** new.* record to the SELECT statement. Or, if it points to a DELETE or
212626** UPDATE, bind values from the old.* record.
212627*/
212628static int sessionSeekToRow(
212629 sqlite3 *db, /* Database handle */
212630 sqlite3_changeset_iter *pIter, /* Changeset iterator */
212631 u8 *abPK, /* Primary key flags array */
212632 sqlite3_stmt *pSelect /* SELECT statement from sessionSelectRow() */
212633){
212634 int rc; /* Return code */
212635 int nCol; /* Number of columns in table */
212636 int op; /* Changset operation (SQLITE_UPDATE etc.) */
212637 const char *zDummy; /* Unused */
212638
212639 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
212640 rc = sessionBindRow(pIter,
212641 op==SQLITE_INSERT ? sqlite3changeset_new : sqlite3changeset_old,
212642 nCol, abPK, pSelect
212643 );
212644
212645 if( rc==SQLITE_OK ){
212646 rc = sqlite3_step(pSelect);
212647 if( rc!=SQLITE_ROW ) rc = sqlite3_reset(pSelect);
212648 }
212649
212650 return rc;
212651}
212652
212653/*
212654** This function is called from within sqlite3changeset_apply_v2() when
212655** a conflict is encountered and resolved using conflict resolution
212656** mode eType (either SQLITE_CHANGESET_OMIT or SQLITE_CHANGESET_REPLACE)..
212657** It adds a conflict resolution record to the buffer in
212658** SessionApplyCtx.rebase, which will eventually be returned to the caller
212659** of apply_v2() as the "rebase" buffer.
212660**
212661** Return SQLITE_OK if successful, or an SQLite error code otherwise.
212662*/
212663static int sessionRebaseAdd(
212664 SessionApplyCtx *p, /* Apply context */
212665 int eType, /* Conflict resolution (OMIT or REPLACE) */
212666 sqlite3_changeset_iter *pIter /* Iterator pointing at current change */
212667){
212668 int rc = SQLITE_OK;
212669 if( p->bRebase ){
212670 int i;
212671 int eOp = pIter->op;
212672 if( p->bRebaseStarted==0 ){
212673 /* Append a table-header to the rebase buffer */
212674 const char *zTab = pIter->zTab;
212675 sessionAppendByte(&p->rebase, 'T', &rc);
212676 sessionAppendVarint(&p->rebase, p->nCol, &rc);
212677 sessionAppendBlob(&p->rebase, p->abPK, p->nCol, &rc);
212678 sessionAppendBlob(&p->rebase, (u8*)zTab, (int)strlen(zTab)+1, &rc);
212679 p->bRebaseStarted = 1;
212680 }
212681
212682 assert( eType==SQLITE_CHANGESET_REPLACE||eType==SQLITE_CHANGESET_OMIT );
212683 assert( eOp==SQLITE_DELETE || eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE );
212684
212685 sessionAppendByte(&p->rebase,
212686 (eOp==SQLITE_DELETE ? SQLITE_DELETE : SQLITE_INSERT), &rc
212687 );
212688 sessionAppendByte(&p->rebase, (eType==SQLITE_CHANGESET_REPLACE), &rc);
212689 for(i=0; i<p->nCol; i++){
212690 sqlite3_value *pVal = 0;
212691 if( eOp==SQLITE_DELETE || (eOp==SQLITE_UPDATE && p->abPK[i]) ){
212692 sqlite3changeset_old(pIter, i, &pVal);
212693 }else{
212694 sqlite3changeset_new(pIter, i, &pVal);
212695 }
212696 sessionAppendValue(&p->rebase, pVal, &rc);
212697 }
212698 }
212699 return rc;
212700}
212701
212702/*
212703** Invoke the conflict handler for the change that the changeset iterator
212704** currently points to.
212705**
212706** Argument eType must be either CHANGESET_DATA or CHANGESET_CONFLICT.
212707** If argument pbReplace is NULL, then the type of conflict handler invoked
212708** depends solely on eType, as follows:
212709**
212710** eType value Value passed to xConflict
212711** -------------------------------------------------
212712** CHANGESET_DATA CHANGESET_NOTFOUND
212713** CHANGESET_CONFLICT CHANGESET_CONSTRAINT
212714**
212715** Or, if pbReplace is not NULL, then an attempt is made to find an existing
212716** record with the same primary key as the record about to be deleted, updated
212717** or inserted. If such a record can be found, it is available to the conflict
212718** handler as the "conflicting" record. In this case the type of conflict
212719** handler invoked is as follows:
212720**
212721** eType value PK Record found? Value passed to xConflict
212722** ----------------------------------------------------------------
212723** CHANGESET_DATA Yes CHANGESET_DATA
212724** CHANGESET_DATA No CHANGESET_NOTFOUND
212725** CHANGESET_CONFLICT Yes CHANGESET_CONFLICT
212726** CHANGESET_CONFLICT No CHANGESET_CONSTRAINT
212727**
212728** If pbReplace is not NULL, and a record with a matching PK is found, and
212729** the conflict handler function returns SQLITE_CHANGESET_REPLACE, *pbReplace
212730** is set to non-zero before returning SQLITE_OK.
212731**
212732** If the conflict handler returns SQLITE_CHANGESET_ABORT, SQLITE_ABORT is
212733** returned. Or, if the conflict handler returns an invalid value,
212734** SQLITE_MISUSE. If the conflict handler returns SQLITE_CHANGESET_OMIT,
212735** this function returns SQLITE_OK.
212736*/
212737static int sessionConflictHandler(
212738 int eType, /* Either CHANGESET_DATA or CONFLICT */
212739 SessionApplyCtx *p, /* changeset_apply() context */
212740 sqlite3_changeset_iter *pIter, /* Changeset iterator */
212741 int(*xConflict)(void *, int, sqlite3_changeset_iter*),
212742 void *pCtx, /* First argument for conflict handler */
212743 int *pbReplace /* OUT: Set to true if PK row is found */
212744){
212745 int res = 0; /* Value returned by conflict handler */
212746 int rc;
212747 int nCol;
212748 int op;
212749 const char *zDummy;
212750
212751 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
212752
212753 assert( eType==SQLITE_CHANGESET_CONFLICT || eType==SQLITE_CHANGESET_DATA );
212754 assert( SQLITE_CHANGESET_CONFLICT+1==SQLITE_CHANGESET_CONSTRAINT );
212755 assert( SQLITE_CHANGESET_DATA+1==SQLITE_CHANGESET_NOTFOUND );
212756
212757 /* Bind the new.* PRIMARY KEY values to the SELECT statement. */
212758 if( pbReplace ){
212759 rc = sessionSeekToRow(p->db, pIter, p->abPK, p->pSelect);
212760 }else{
212761 rc = SQLITE_OK;
212762 }
212763
212764 if( rc==SQLITE_ROW ){
212765 /* There exists another row with the new.* primary key. */
212766 pIter->pConflict = p->pSelect;
212767 res = xConflict(pCtx, eType, pIter);
212768 pIter->pConflict = 0;
212769 rc = sqlite3_reset(p->pSelect);
212770 }else if( rc==SQLITE_OK ){
212771 if( p->bDeferConstraints && eType==SQLITE_CHANGESET_CONFLICT ){
212772 /* Instead of invoking the conflict handler, append the change blob
212773 ** to the SessionApplyCtx.constraints buffer. */
212774 u8 *aBlob = &pIter->in.aData[pIter->in.iCurrent];
212775 int nBlob = pIter->in.iNext - pIter->in.iCurrent;
212776 sessionAppendBlob(&p->constraints, aBlob, nBlob, &rc);
212777 return SQLITE_OK;
212778 }else{
212779 /* No other row with the new.* primary key. */
212780 res = xConflict(pCtx, eType+1, pIter);
212781 if( res==SQLITE_CHANGESET_REPLACE ) rc = SQLITE_MISUSE;
212782 }
212783 }
212784
212785 if( rc==SQLITE_OK ){
212786 switch( res ){
212787 case SQLITE_CHANGESET_REPLACE:
212788 assert( pbReplace );
212789 *pbReplace = 1;
212790 break;
212791
212792 case SQLITE_CHANGESET_OMIT:
212793 break;
212794
212795 case SQLITE_CHANGESET_ABORT:
212796 rc = SQLITE_ABORT;
212797 break;
212798
212799 default:
212800 rc = SQLITE_MISUSE;
212801 break;
212802 }
212803 if( rc==SQLITE_OK ){
212804 rc = sessionRebaseAdd(p, res, pIter);
212805 }
212806 }
212807
212808 return rc;
212809}
212810
212811/*
212812** Attempt to apply the change that the iterator passed as the first argument
212813** currently points to to the database. If a conflict is encountered, invoke
212814** the conflict handler callback.
212815**
212816** If argument pbRetry is NULL, then ignore any CHANGESET_DATA conflict. If
212817** one is encountered, update or delete the row with the matching primary key
212818** instead. Or, if pbRetry is not NULL and a CHANGESET_DATA conflict occurs,
212819** invoke the conflict handler. If it returns CHANGESET_REPLACE, set *pbRetry
212820** to true before returning. In this case the caller will invoke this function
212821** again, this time with pbRetry set to NULL.
212822**
212823** If argument pbReplace is NULL and a CHANGESET_CONFLICT conflict is
212824** encountered invoke the conflict handler with CHANGESET_CONSTRAINT instead.
212825** Or, if pbReplace is not NULL, invoke it with CHANGESET_CONFLICT. If such
212826** an invocation returns SQLITE_CHANGESET_REPLACE, set *pbReplace to true
212827** before retrying. In this case the caller attempts to remove the conflicting
212828** row before invoking this function again, this time with pbReplace set
212829** to NULL.
212830**
212831** If any conflict handler returns SQLITE_CHANGESET_ABORT, this function
212832** returns SQLITE_ABORT. Otherwise, if no error occurs, SQLITE_OK is
212833** returned.
212834*/
212835static int sessionApplyOneOp(
212836 sqlite3_changeset_iter *pIter, /* Changeset iterator */
212837 SessionApplyCtx *p, /* changeset_apply() context */
212838 int(*xConflict)(void *, int, sqlite3_changeset_iter *),
212839 void *pCtx, /* First argument for the conflict handler */
212840 int *pbReplace, /* OUT: True to remove PK row and retry */
212841 int *pbRetry /* OUT: True to retry. */
212842){
212843 const char *zDummy;
212844 int op;
212845 int nCol;
212846 int rc = SQLITE_OK;
212847
212848 assert( p->pDelete && p->pInsert && p->pSelect );
212849 assert( p->azCol && p->abPK );
212850 assert( !pbReplace || *pbReplace==0 );
212851
212852 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
212853
212854 if( op==SQLITE_DELETE ){
212855
212856 /* Bind values to the DELETE statement. If conflict handling is required,
212857 ** bind values for all columns and set bound variable (nCol+1) to true.
212858 ** Or, if conflict handling is not required, bind just the PK column
212859 ** values and, if it exists, set (nCol+1) to false. Conflict handling
212860 ** is not required if:
212861 **
212862 ** * this is a patchset, or
212863 ** * (pbRetry==0), or
212864 ** * all columns of the table are PK columns (in this case there is
212865 ** no (nCol+1) variable to bind to).
212866 */
212867 u8 *abPK = (pIter->bPatchset ? p->abPK : 0);
212868 rc = sessionBindRow(pIter, sqlite3changeset_old, nCol, abPK, p->pDelete);
212869 if( rc==SQLITE_OK && sqlite3_bind_parameter_count(p->pDelete)>nCol ){
212870 rc = sqlite3_bind_int(p->pDelete, nCol+1, (pbRetry==0 || abPK));
212871 }
212872 if( rc!=SQLITE_OK ) return rc;
212873
212874 sqlite3_step(p->pDelete);
212875 rc = sqlite3_reset(p->pDelete);
212876 if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){
212877 rc = sessionConflictHandler(
212878 SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry
212879 );
212880 }else if( (rc&0xff)==SQLITE_CONSTRAINT ){
212881 rc = sessionConflictHandler(
212882 SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0
212883 );
212884 }
212885
212886 }else if( op==SQLITE_UPDATE ){
212887 int i;
212888 sqlite3_stmt *pUp = 0;
212889 int bPatchset = (pbRetry==0 || pIter->bPatchset);
212890
212891 rc = sessionUpdateFind(pIter, p, bPatchset, &pUp);
212892
212893 /* Bind values to the UPDATE statement. */
212894 for(i=0; rc==SQLITE_OK && i<nCol; i++){
212895 sqlite3_value *pOld = sessionChangesetOld(pIter, i);
212896 sqlite3_value *pNew = sessionChangesetNew(pIter, i);
212897 if( p->abPK[i] || (bPatchset==0 && pOld) ){
212898 rc = sessionBindValue(pUp, i*2+2, pOld);
212899 }
212900 if( rc==SQLITE_OK && pNew ){
212901 rc = sessionBindValue(pUp, i*2+1, pNew);
212902 }
212903 }
212904 if( rc!=SQLITE_OK ) return rc;
212905
212906 /* Attempt the UPDATE. In the case of a NOTFOUND or DATA conflict,
212907 ** the result will be SQLITE_OK with 0 rows modified. */
212908 sqlite3_step(pUp);
212909 rc = sqlite3_reset(pUp);
212910
212911 if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){
212912 /* A NOTFOUND or DATA error. Search the table to see if it contains
212913 ** a row with a matching primary key. If so, this is a DATA conflict.
212914 ** Otherwise, if there is no primary key match, it is a NOTFOUND. */
212915
212916 rc = sessionConflictHandler(
212917 SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry
212918 );
212919
212920 }else if( (rc&0xff)==SQLITE_CONSTRAINT ){
212921 /* This is always a CONSTRAINT conflict. */
212922 rc = sessionConflictHandler(
212923 SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0
212924 );
212925 }
212926
212927 }else{
212928 assert( op==SQLITE_INSERT );
212929 if( p->bStat1 ){
212930 /* Check if there is a conflicting row. For sqlite_stat1, this needs
212931 ** to be done using a SELECT, as there is no PRIMARY KEY in the
212932 ** database schema to throw an exception if a duplicate is inserted. */
212933 rc = sessionSeekToRow(p->db, pIter, p->abPK, p->pSelect);
212934 if( rc==SQLITE_ROW ){
212935 rc = SQLITE_CONSTRAINT;
212936 sqlite3_reset(p->pSelect);
212937 }
212938 }
212939
212940 if( rc==SQLITE_OK ){
212941 rc = sessionBindRow(pIter, sqlite3changeset_new, nCol, 0, p->pInsert);
212942 if( rc!=SQLITE_OK ) return rc;
212943
212944 sqlite3_step(p->pInsert);
212945 rc = sqlite3_reset(p->pInsert);
212946 }
212947
212948 if( (rc&0xff)==SQLITE_CONSTRAINT ){
212949 rc = sessionConflictHandler(
212950 SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, pbReplace
212951 );
212952 }
212953 }
212954
212955 return rc;
212956}
212957
212958/*
212959** Attempt to apply the change that the iterator passed as the first argument
212960** currently points to to the database. If a conflict is encountered, invoke
212961** the conflict handler callback.
212962**
212963** The difference between this function and sessionApplyOne() is that this
212964** function handles the case where the conflict-handler is invoked and
212965** returns SQLITE_CHANGESET_REPLACE - indicating that the change should be
212966** retried in some manner.
212967*/
212968static int sessionApplyOneWithRetry(
212969 sqlite3 *db, /* Apply change to "main" db of this handle */
212970 sqlite3_changeset_iter *pIter, /* Changeset iterator to read change from */
212971 SessionApplyCtx *pApply, /* Apply context */
212972 int(*xConflict)(void*, int, sqlite3_changeset_iter*),
212973 void *pCtx /* First argument passed to xConflict */
212974){
212975 int bReplace = 0;
212976 int bRetry = 0;
212977 int rc;
212978
212979 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, &bReplace, &bRetry);
212980 if( rc==SQLITE_OK ){
212981 /* If the bRetry flag is set, the change has not been applied due to an
212982 ** SQLITE_CHANGESET_DATA problem (i.e. this is an UPDATE or DELETE and
212983 ** a row with the correct PK is present in the db, but one or more other
212984 ** fields do not contain the expected values) and the conflict handler
212985 ** returned SQLITE_CHANGESET_REPLACE. In this case retry the operation,
212986 ** but pass NULL as the final argument so that sessionApplyOneOp() ignores
212987 ** the SQLITE_CHANGESET_DATA problem. */
212988 if( bRetry ){
212989 assert( pIter->op==SQLITE_UPDATE || pIter->op==SQLITE_DELETE );
212990 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
212991 }
212992
212993 /* If the bReplace flag is set, the change is an INSERT that has not
212994 ** been performed because the database already contains a row with the
212995 ** specified primary key and the conflict handler returned
212996 ** SQLITE_CHANGESET_REPLACE. In this case remove the conflicting row
212997 ** before reattempting the INSERT. */
212998 else if( bReplace ){
212999 assert( pIter->op==SQLITE_INSERT );
213000 rc = sqlite3_exec(db, "SAVEPOINT replace_op", 0, 0, 0);
213001 if( rc==SQLITE_OK ){
213002 rc = sessionBindRow(pIter,
213003 sqlite3changeset_new, pApply->nCol, pApply->abPK, pApply->pDelete);
213004 sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1);
213005 }
213006 if( rc==SQLITE_OK ){
213007 sqlite3_step(pApply->pDelete);
213008 rc = sqlite3_reset(pApply->pDelete);
213009 }
213010 if( rc==SQLITE_OK ){
213011 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
213012 }
213013 if( rc==SQLITE_OK ){
213014 rc = sqlite3_exec(db, "RELEASE replace_op", 0, 0, 0);
213015 }
213016 }
213017 }
213018
213019 return rc;
213020}
213021
213022/*
213023** Retry the changes accumulated in the pApply->constraints buffer.
213024*/
213025static int sessionRetryConstraints(
213026 sqlite3 *db,
213027 int bPatchset,
213028 const char *zTab,
213029 SessionApplyCtx *pApply,
213030 int(*xConflict)(void*, int, sqlite3_changeset_iter*),
213031 void *pCtx /* First argument passed to xConflict */
213032){
213033 int rc = SQLITE_OK;
213034
213035 while( pApply->constraints.nBuf ){
213036 sqlite3_changeset_iter *pIter2 = 0;
213037 SessionBuffer cons = pApply->constraints;
213038 memset(&pApply->constraints, 0, sizeof(SessionBuffer));
213039
213040 rc = sessionChangesetStart(
213041 &pIter2, 0, 0, cons.nBuf, cons.aBuf, pApply->bInvertConstraints, 1
213042 );
213043 if( rc==SQLITE_OK ){
213044 size_t nByte = 2*pApply->nCol*sizeof(sqlite3_value*);
213045 int rc2;
213046 pIter2->bPatchset = bPatchset;
213047 pIter2->zTab = (char*)zTab;
213048 pIter2->nCol = pApply->nCol;
213049 pIter2->abPK = pApply->abPK;
213050 sessionBufferGrow(&pIter2->tblhdr, nByte, &rc);
213051 pIter2->apValue = (sqlite3_value**)pIter2->tblhdr.aBuf;
213052 if( rc==SQLITE_OK ) memset(pIter2->apValue, 0, nByte);
213053
213054 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3changeset_next(pIter2) ){
213055 rc = sessionApplyOneWithRetry(db, pIter2, pApply, xConflict, pCtx);
213056 }
213057
213058 rc2 = sqlite3changeset_finalize(pIter2);
213059 if( rc==SQLITE_OK ) rc = rc2;
213060 }
213061 assert( pApply->bDeferConstraints || pApply->constraints.nBuf==0 );
213062
213063 sqlite3_free(cons.aBuf);
213064 if( rc!=SQLITE_OK ) break;
213065 if( pApply->constraints.nBuf>=cons.nBuf ){
213066 /* No progress was made on the last round. */
213067 pApply->bDeferConstraints = 0;
213068 }
213069 }
213070
213071 return rc;
213072}
213073
213074/*
213075** Argument pIter is a changeset iterator that has been initialized, but
213076** not yet passed to sqlite3changeset_next(). This function applies the
213077** changeset to the main database attached to handle "db". The supplied
213078** conflict handler callback is invoked to resolve any conflicts encountered
213079** while applying the change.
213080*/
213081static int sessionChangesetApply(
213082 sqlite3 *db, /* Apply change to "main" db of this handle */
213083 sqlite3_changeset_iter *pIter, /* Changeset to apply */
213084 int(*xFilter)(
213085 void *pCtx, /* Copy of sixth arg to _apply() */
213086 const char *zTab /* Table name */
213087 ),
213088 int(*xConflict)(
213089 void *pCtx, /* Copy of fifth arg to _apply() */
213090 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
213091 sqlite3_changeset_iter *p /* Handle describing change and conflict */
213092 ),
213093 void *pCtx, /* First argument passed to xConflict */
213094 void **ppRebase, int *pnRebase, /* OUT: Rebase information */
213095 int flags /* SESSION_APPLY_XXX flags */
213096){
213097 int schemaMismatch = 0;
213098 int rc = SQLITE_OK; /* Return code */
213099 const char *zTab = 0; /* Name of current table */
213100 int nTab = 0; /* Result of sqlite3Strlen30(zTab) */
213101 SessionApplyCtx sApply; /* changeset_apply() context object */
213102 int bPatchset;
213103
213104 assert( xConflict!=0 );
213105
213106 pIter->in.bNoDiscard = 1;
213107 memset(&sApply, 0, sizeof(sApply));
213108 sApply.bRebase = (ppRebase && pnRebase);
213109 sApply.bInvertConstraints = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
213110 sqlite3_mutex_enter(sqlite3_db_mutex(db));
213111 if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){
213112 rc = sqlite3_exec(db, "SAVEPOINT changeset_apply", 0, 0, 0);
213113 }
213114 if( rc==SQLITE_OK ){
213115 rc = sqlite3_exec(db, "PRAGMA defer_foreign_keys = 1", 0, 0, 0);
213116 }
213117 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3changeset_next(pIter) ){
213118 int nCol;
213119 int op;
213120 const char *zNew;
213121
213122 sqlite3changeset_op(pIter, &zNew, &nCol, &op, 0);
213123
213124 if( zTab==0 || sqlite3_strnicmp(zNew, zTab, nTab+1) ){
213125 u8 *abPK;
213126
213127 rc = sessionRetryConstraints(
213128 db, pIter->bPatchset, zTab, &sApply, xConflict, pCtx
213129 );
213130 if( rc!=SQLITE_OK ) break;
213131
213132 sessionUpdateFree(&sApply);
213133 sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
213134 sqlite3_finalize(sApply.pDelete);
213135 sqlite3_finalize(sApply.pInsert);
213136 sqlite3_finalize(sApply.pSelect);
213137 sApply.db = db;
213138 sApply.pDelete = 0;
213139 sApply.pInsert = 0;
213140 sApply.pSelect = 0;
213141 sApply.nCol = 0;
213142 sApply.azCol = 0;
213143 sApply.abPK = 0;
213144 sApply.bStat1 = 0;
213145 sApply.bDeferConstraints = 1;
213146 sApply.bRebaseStarted = 0;
213147 memset(&sApply.constraints, 0, sizeof(SessionBuffer));
213148
213149 /* If an xFilter() callback was specified, invoke it now. If the
213150 ** xFilter callback returns zero, skip this table. If it returns
213151 ** non-zero, proceed. */
213152 schemaMismatch = (xFilter && (0==xFilter(pCtx, zNew)));
213153 if( schemaMismatch ){
213154 zTab = sqlite3_mprintf("%s", zNew);
213155 if( zTab==0 ){
213156 rc = SQLITE_NOMEM;
213157 break;
213158 }
213159 nTab = (int)strlen(zTab);
213160 sApply.azCol = (const char **)zTab;
213161 }else{
213162 int nMinCol = 0;
213163 int i;
213164
213165 sqlite3changeset_pk(pIter, &abPK, 0);
213166 rc = sessionTableInfo(0,
213167 db, "main", zNew, &sApply.nCol, &zTab, &sApply.azCol, &sApply.abPK
213168 );
213169 if( rc!=SQLITE_OK ) break;
213170 for(i=0; i<sApply.nCol; i++){
213171 if( sApply.abPK[i] ) nMinCol = i+1;
213172 }
213173
213174 if( sApply.nCol==0 ){
213175 schemaMismatch = 1;
213176 sqlite3_log(SQLITE_SCHEMA,
213177 "sqlite3changeset_apply(): no such table: %s", zTab
213178 );
213179 }
213180 else if( sApply.nCol<nCol ){
213181 schemaMismatch = 1;
213182 sqlite3_log(SQLITE_SCHEMA,
213183 "sqlite3changeset_apply(): table %s has %d columns, "
213184 "expected %d or more",
213185 zTab, sApply.nCol, nCol
213186 );
213187 }
213188 else if( nCol<nMinCol || memcmp(sApply.abPK, abPK, nCol)!=0 ){
213189 schemaMismatch = 1;
213190 sqlite3_log(SQLITE_SCHEMA, "sqlite3changeset_apply(): "
213191 "primary key mismatch for table %s", zTab
213192 );
213193 }
213194 else{
213195 sApply.nCol = nCol;
213196 if( 0==sqlite3_stricmp(zTab, "sqlite_stat1") ){
213197 if( (rc = sessionStat1Sql(db, &sApply) ) ){
213198 break;
213199 }
213200 sApply.bStat1 = 1;
213201 }else{
213202 if( (rc = sessionSelectRow(db, zTab, &sApply))
213203 || (rc = sessionDeleteRow(db, zTab, &sApply))
213204 || (rc = sessionInsertRow(db, zTab, &sApply))
213205 ){
213206 break;
213207 }
213208 sApply.bStat1 = 0;
213209 }
213210 }
213211 nTab = sqlite3Strlen30(zTab);
213212 }
213213 }
213214
213215 /* If there is a schema mismatch on the current table, proceed to the
213216 ** next change. A log message has already been issued. */
213217 if( schemaMismatch ) continue;
213218
213219 rc = sessionApplyOneWithRetry(db, pIter, &sApply, xConflict, pCtx);
213220 }
213221
213222 bPatchset = pIter->bPatchset;
213223 if( rc==SQLITE_OK ){
213224 rc = sqlite3changeset_finalize(pIter);
213225 }else{
213226 sqlite3changeset_finalize(pIter);
213227 }
213228
213229 if( rc==SQLITE_OK ){
213230 rc = sessionRetryConstraints(db, bPatchset, zTab, &sApply, xConflict, pCtx);
213231 }
213232
213233 if( rc==SQLITE_OK ){
213234 int nFk, notUsed;
213235 sqlite3_db_status(db, SQLITE_DBSTATUS_DEFERRED_FKS, &nFk, &notUsed, 0);
213236 if( nFk!=0 ){
213237 int res = SQLITE_CHANGESET_ABORT;
213238 sqlite3_changeset_iter sIter;
213239 memset(&sIter, 0, sizeof(sIter));
213240 sIter.nCol = nFk;
213241 res = xConflict(pCtx, SQLITE_CHANGESET_FOREIGN_KEY, &sIter);
213242 if( res!=SQLITE_CHANGESET_OMIT ){
213243 rc = SQLITE_CONSTRAINT;
213244 }
213245 }
213246 }
213247 sqlite3_exec(db, "PRAGMA defer_foreign_keys = 0", 0, 0, 0);
213248
213249 if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){
213250 if( rc==SQLITE_OK ){
213251 rc = sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
213252 }else{
213253 sqlite3_exec(db, "ROLLBACK TO changeset_apply", 0, 0, 0);
213254 sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
213255 }
213256 }
213257
213258 assert( sApply.bRebase || sApply.rebase.nBuf==0 );
213259 if( rc==SQLITE_OK && bPatchset==0 && sApply.bRebase ){
213260 *ppRebase = (void*)sApply.rebase.aBuf;
213261 *pnRebase = sApply.rebase.nBuf;
213262 sApply.rebase.aBuf = 0;
213263 }
213264 sessionUpdateFree(&sApply);
213265 sqlite3_finalize(sApply.pInsert);
213266 sqlite3_finalize(sApply.pDelete);
213267 sqlite3_finalize(sApply.pSelect);
213268 sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
213269 sqlite3_free((char*)sApply.constraints.aBuf);
213270 sqlite3_free((char*)sApply.rebase.aBuf);
213271 sqlite3_mutex_leave(sqlite3_db_mutex(db));
213272 return rc;
213273}
213274
213275/*
213276** Apply the changeset passed via pChangeset/nChangeset to the main
213277** database attached to handle "db".
213278*/
213279SQLITE_API int sqlite3changeset_apply_v2(
213280 sqlite3 *db, /* Apply change to "main" db of this handle */
213281 int nChangeset, /* Size of changeset in bytes */
213282 void *pChangeset, /* Changeset blob */
213283 int(*xFilter)(
213284 void *pCtx, /* Copy of sixth arg to _apply() */
213285 const char *zTab /* Table name */
213286 ),
213287 int(*xConflict)(
213288 void *pCtx, /* Copy of sixth arg to _apply() */
213289 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
213290 sqlite3_changeset_iter *p /* Handle describing change and conflict */
213291 ),
213292 void *pCtx, /* First argument passed to xConflict */
213293 void **ppRebase, int *pnRebase,
213294 int flags
213295){
213296 sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
213297 int bInv = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
213298 int rc = sessionChangesetStart(&pIter, 0, 0, nChangeset, pChangeset, bInv, 1);
213299 if( rc==SQLITE_OK ){
213300 rc = sessionChangesetApply(
213301 db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
213302 );
213303 }
213304 return rc;
213305}
213306
213307/*
213308** Apply the changeset passed via pChangeset/nChangeset to the main database
213309** attached to handle "db". Invoke the supplied conflict handler callback
213310** to resolve any conflicts encountered while applying the change.
213311*/
213312SQLITE_API int sqlite3changeset_apply(
213313 sqlite3 *db, /* Apply change to "main" db of this handle */
213314 int nChangeset, /* Size of changeset in bytes */
213315 void *pChangeset, /* Changeset blob */
213316 int(*xFilter)(
213317 void *pCtx, /* Copy of sixth arg to _apply() */
213318 const char *zTab /* Table name */
213319 ),
213320 int(*xConflict)(
213321 void *pCtx, /* Copy of fifth arg to _apply() */
213322 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
213323 sqlite3_changeset_iter *p /* Handle describing change and conflict */
213324 ),
213325 void *pCtx /* First argument passed to xConflict */
213326){
213327 return sqlite3changeset_apply_v2(
213328 db, nChangeset, pChangeset, xFilter, xConflict, pCtx, 0, 0, 0
213329 );
213330}
213331
213332/*
213333** Apply the changeset passed via xInput/pIn to the main database
213334** attached to handle "db". Invoke the supplied conflict handler callback
213335** to resolve any conflicts encountered while applying the change.
213336*/
213337SQLITE_API int sqlite3changeset_apply_v2_strm(
213338 sqlite3 *db, /* Apply change to "main" db of this handle */
213339 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
213340 void *pIn, /* First arg for xInput */
213341 int(*xFilter)(
213342 void *pCtx, /* Copy of sixth arg to _apply() */
213343 const char *zTab /* Table name */
213344 ),
213345 int(*xConflict)(
213346 void *pCtx, /* Copy of sixth arg to _apply() */
213347 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
213348 sqlite3_changeset_iter *p /* Handle describing change and conflict */
213349 ),
213350 void *pCtx, /* First argument passed to xConflict */
213351 void **ppRebase, int *pnRebase,
213352 int flags
213353){
213354 sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
213355 int bInverse = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
213356 int rc = sessionChangesetStart(&pIter, xInput, pIn, 0, 0, bInverse, 1);
213357 if( rc==SQLITE_OK ){
213358 rc = sessionChangesetApply(
213359 db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
213360 );
213361 }
213362 return rc;
213363}
213364SQLITE_API int sqlite3changeset_apply_strm(
213365 sqlite3 *db, /* Apply change to "main" db of this handle */
213366 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
213367 void *pIn, /* First arg for xInput */
213368 int(*xFilter)(
213369 void *pCtx, /* Copy of sixth arg to _apply() */
213370 const char *zTab /* Table name */
213371 ),
213372 int(*xConflict)(
213373 void *pCtx, /* Copy of sixth arg to _apply() */
213374 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
213375 sqlite3_changeset_iter *p /* Handle describing change and conflict */
213376 ),
213377 void *pCtx /* First argument passed to xConflict */
213378){
213379 return sqlite3changeset_apply_v2_strm(
213380 db, xInput, pIn, xFilter, xConflict, pCtx, 0, 0, 0
213381 );
213382}
213383
213384/*
213385** sqlite3_changegroup handle.
213386*/
213387struct sqlite3_changegroup {
213388 int rc; /* Error code */
213389 int bPatch; /* True to accumulate patchsets */
213390 SessionTable *pList; /* List of tables in current patch */
213391};
213392
213393/*
213394** This function is called to merge two changes to the same row together as
213395** part of an sqlite3changeset_concat() operation. A new change object is
213396** allocated and a pointer to it stored in *ppNew.
213397*/
213398static int sessionChangeMerge(
213399 SessionTable *pTab, /* Table structure */
213400 int bRebase, /* True for a rebase hash-table */
213401 int bPatchset, /* True for patchsets */
213402 SessionChange *pExist, /* Existing change */
213403 int op2, /* Second change operation */
213404 int bIndirect, /* True if second change is indirect */
213405 u8 *aRec, /* Second change record */
213406 int nRec, /* Number of bytes in aRec */
213407 SessionChange **ppNew /* OUT: Merged change */
213408){
213409 SessionChange *pNew = 0;
213410 int rc = SQLITE_OK;
213411
213412 if( !pExist ){
213413 pNew = (SessionChange *)sqlite3_malloc64(sizeof(SessionChange) + nRec);
213414 if( !pNew ){
213415 return SQLITE_NOMEM;
213416 }
213417 memset(pNew, 0, sizeof(SessionChange));
213418 pNew->op = op2;
213419 pNew->bIndirect = bIndirect;
213420 pNew->aRecord = (u8*)&pNew[1];
213421 if( bIndirect==0 || bRebase==0 ){
213422 pNew->nRecord = nRec;
213423 memcpy(pNew->aRecord, aRec, nRec);
213424 }else{
213425 int i;
213426 u8 *pIn = aRec;
213427 u8 *pOut = pNew->aRecord;
213428 for(i=0; i<pTab->nCol; i++){
213429 int nIn = sessionSerialLen(pIn);
213430 if( *pIn==0 ){
213431 *pOut++ = 0;
213432 }else if( pTab->abPK[i]==0 ){
213433 *pOut++ = 0xFF;
213434 }else{
213435 memcpy(pOut, pIn, nIn);
213436 pOut += nIn;
213437 }
213438 pIn += nIn;
213439 }
213440 pNew->nRecord = pOut - pNew->aRecord;
213441 }
213442 }else if( bRebase ){
213443 if( pExist->op==SQLITE_DELETE && pExist->bIndirect ){
213444 *ppNew = pExist;
213445 }else{
213446 sqlite3_int64 nByte = nRec + pExist->nRecord + sizeof(SessionChange);
213447 pNew = (SessionChange*)sqlite3_malloc64(nByte);
213448 if( pNew==0 ){
213449 rc = SQLITE_NOMEM;
213450 }else{
213451 int i;
213452 u8 *a1 = pExist->aRecord;
213453 u8 *a2 = aRec;
213454 u8 *pOut;
213455
213456 memset(pNew, 0, nByte);
213457 pNew->bIndirect = bIndirect || pExist->bIndirect;
213458 pNew->op = op2;
213459 pOut = pNew->aRecord = (u8*)&pNew[1];
213460
213461 for(i=0; i<pTab->nCol; i++){
213462 int n1 = sessionSerialLen(a1);
213463 int n2 = sessionSerialLen(a2);
213464 if( *a1==0xFF || (pTab->abPK[i]==0 && bIndirect) ){
213465 *pOut++ = 0xFF;
213466 }else if( *a2==0 ){
213467 memcpy(pOut, a1, n1);
213468 pOut += n1;
213469 }else{
213470 memcpy(pOut, a2, n2);
213471 pOut += n2;
213472 }
213473 a1 += n1;
213474 a2 += n2;
213475 }
213476 pNew->nRecord = pOut - pNew->aRecord;
213477 }
213478 sqlite3_free(pExist);
213479 }
213480 }else{
213481 int op1 = pExist->op;
213482
213483 /*
213484 ** op1=INSERT, op2=INSERT -> Unsupported. Discard op2.
213485 ** op1=INSERT, op2=UPDATE -> INSERT.
213486 ** op1=INSERT, op2=DELETE -> (none)
213487 **
213488 ** op1=UPDATE, op2=INSERT -> Unsupported. Discard op2.
213489 ** op1=UPDATE, op2=UPDATE -> UPDATE.
213490 ** op1=UPDATE, op2=DELETE -> DELETE.
213491 **
213492 ** op1=DELETE, op2=INSERT -> UPDATE.
213493 ** op1=DELETE, op2=UPDATE -> Unsupported. Discard op2.
213494 ** op1=DELETE, op2=DELETE -> Unsupported. Discard op2.
213495 */
213496 if( (op1==SQLITE_INSERT && op2==SQLITE_INSERT)
213497 || (op1==SQLITE_UPDATE && op2==SQLITE_INSERT)
213498 || (op1==SQLITE_DELETE && op2==SQLITE_UPDATE)
213499 || (op1==SQLITE_DELETE && op2==SQLITE_DELETE)
213500 ){
213501 pNew = pExist;
213502 }else if( op1==SQLITE_INSERT && op2==SQLITE_DELETE ){
213503 sqlite3_free(pExist);
213504 assert( pNew==0 );
213505 }else{
213506 u8 *aExist = pExist->aRecord;
213507 sqlite3_int64 nByte;
213508 u8 *aCsr;
213509
213510 /* Allocate a new SessionChange object. Ensure that the aRecord[]
213511 ** buffer of the new object is large enough to hold any record that
213512 ** may be generated by combining the input records. */
213513 nByte = sizeof(SessionChange) + pExist->nRecord + nRec;
213514 pNew = (SessionChange *)sqlite3_malloc64(nByte);
213515 if( !pNew ){
213516 sqlite3_free(pExist);
213517 return SQLITE_NOMEM;
213518 }
213519 memset(pNew, 0, sizeof(SessionChange));
213520 pNew->bIndirect = (bIndirect && pExist->bIndirect);
213521 aCsr = pNew->aRecord = (u8 *)&pNew[1];
213522
213523 if( op1==SQLITE_INSERT ){ /* INSERT + UPDATE */
213524 u8 *a1 = aRec;
213525 assert( op2==SQLITE_UPDATE );
213526 pNew->op = SQLITE_INSERT;
213527 if( bPatchset==0 ) sessionSkipRecord(&a1, pTab->nCol);
213528 sessionMergeRecord(&aCsr, pTab->nCol, aExist, a1);
213529 }else if( op1==SQLITE_DELETE ){ /* DELETE + INSERT */
213530 assert( op2==SQLITE_INSERT );
213531 pNew->op = SQLITE_UPDATE;
213532 if( bPatchset ){
213533 memcpy(aCsr, aRec, nRec);
213534 aCsr += nRec;
213535 }else{
213536 if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aExist, 0,aRec,0) ){
213537 sqlite3_free(pNew);
213538 pNew = 0;
213539 }
213540 }
213541 }else if( op2==SQLITE_UPDATE ){ /* UPDATE + UPDATE */
213542 u8 *a1 = aExist;
213543 u8 *a2 = aRec;
213544 assert( op1==SQLITE_UPDATE );
213545 if( bPatchset==0 ){
213546 sessionSkipRecord(&a1, pTab->nCol);
213547 sessionSkipRecord(&a2, pTab->nCol);
213548 }
213549 pNew->op = SQLITE_UPDATE;
213550 if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aRec, aExist,a1,a2) ){
213551 sqlite3_free(pNew);
213552 pNew = 0;
213553 }
213554 }else{ /* UPDATE + DELETE */
213555 assert( op1==SQLITE_UPDATE && op2==SQLITE_DELETE );
213556 pNew->op = SQLITE_DELETE;
213557 if( bPatchset ){
213558 memcpy(aCsr, aRec, nRec);
213559 aCsr += nRec;
213560 }else{
213561 sessionMergeRecord(&aCsr, pTab->nCol, aRec, aExist);
213562 }
213563 }
213564
213565 if( pNew ){
213566 pNew->nRecord = (int)(aCsr - pNew->aRecord);
213567 }
213568 sqlite3_free(pExist);
213569 }
213570 }
213571
213572 *ppNew = pNew;
213573 return rc;
213574}
213575
213576/*
213577** Add all changes in the changeset traversed by the iterator passed as
213578** the first argument to the changegroup hash tables.
213579*/
213580static int sessionChangesetToHash(
213581 sqlite3_changeset_iter *pIter, /* Iterator to read from */
213582 sqlite3_changegroup *pGrp, /* Changegroup object to add changeset to */
213583 int bRebase /* True if hash table is for rebasing */
213584){
213585 u8 *aRec;
213586 int nRec;
213587 int rc = SQLITE_OK;
213588 SessionTable *pTab = 0;
213589
213590 while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, 0) ){
213591 const char *zNew;
213592 int nCol;
213593 int op;
213594 int iHash;
213595 int bIndirect;
213596 SessionChange *pChange;
213597 SessionChange *pExist = 0;
213598 SessionChange **pp;
213599
213600 if( pGrp->pList==0 ){
213601 pGrp->bPatch = pIter->bPatchset;
213602 }else if( pIter->bPatchset!=pGrp->bPatch ){
213603 rc = SQLITE_ERROR;
213604 break;
213605 }
213606
213607 sqlite3changeset_op(pIter, &zNew, &nCol, &op, &bIndirect);
213608 if( !pTab || sqlite3_stricmp(zNew, pTab->zName) ){
213609 /* Search the list for a matching table */
213610 int nNew = (int)strlen(zNew);
213611 u8 *abPK;
213612
213613 sqlite3changeset_pk(pIter, &abPK, 0);
213614 for(pTab = pGrp->pList; pTab; pTab=pTab->pNext){
213615 if( 0==sqlite3_strnicmp(pTab->zName, zNew, nNew+1) ) break;
213616 }
213617 if( !pTab ){
213618 SessionTable **ppTab;
213619
213620 pTab = sqlite3_malloc64(sizeof(SessionTable) + nCol + nNew+1);
213621 if( !pTab ){
213622 rc = SQLITE_NOMEM;
213623 break;
213624 }
213625 memset(pTab, 0, sizeof(SessionTable));
213626 pTab->nCol = nCol;
213627 pTab->abPK = (u8*)&pTab[1];
213628 memcpy(pTab->abPK, abPK, nCol);
213629 pTab->zName = (char*)&pTab->abPK[nCol];
213630 memcpy(pTab->zName, zNew, nNew+1);
213631
213632 /* The new object must be linked on to the end of the list, not
213633 ** simply added to the start of it. This is to ensure that the
213634 ** tables within the output of sqlite3changegroup_output() are in
213635 ** the right order. */
213636 for(ppTab=&pGrp->pList; *ppTab; ppTab=&(*ppTab)->pNext);
213637 *ppTab = pTab;
213638 }else if( pTab->nCol!=nCol || memcmp(pTab->abPK, abPK, nCol) ){
213639 rc = SQLITE_SCHEMA;
213640 break;
213641 }
213642 }
213643
213644 if( sessionGrowHash(0, pIter->bPatchset, pTab) ){
213645 rc = SQLITE_NOMEM;
213646 break;
213647 }
213648 iHash = sessionChangeHash(
213649 pTab, (pIter->bPatchset && op==SQLITE_DELETE), aRec, pTab->nChange
213650 );
213651
213652 /* Search for existing entry. If found, remove it from the hash table.
213653 ** Code below may link it back in.
213654 */
213655 for(pp=&pTab->apChange[iHash]; *pp; pp=&(*pp)->pNext){
213656 int bPkOnly1 = 0;
213657 int bPkOnly2 = 0;
213658 if( pIter->bPatchset ){
213659 bPkOnly1 = (*pp)->op==SQLITE_DELETE;
213660 bPkOnly2 = op==SQLITE_DELETE;
213661 }
213662 if( sessionChangeEqual(pTab, bPkOnly1, (*pp)->aRecord, bPkOnly2, aRec) ){
213663 pExist = *pp;
213664 *pp = (*pp)->pNext;
213665 pTab->nEntry--;
213666 break;
213667 }
213668 }
213669
213670 rc = sessionChangeMerge(pTab, bRebase,
213671 pIter->bPatchset, pExist, op, bIndirect, aRec, nRec, &pChange
213672 );
213673 if( rc ) break;
213674 if( pChange ){
213675 pChange->pNext = pTab->apChange[iHash];
213676 pTab->apChange[iHash] = pChange;
213677 pTab->nEntry++;
213678 }
213679 }
213680
213681 if( rc==SQLITE_OK ) rc = pIter->rc;
213682 return rc;
213683}
213684
213685/*
213686** Serialize a changeset (or patchset) based on all changesets (or patchsets)
213687** added to the changegroup object passed as the first argument.
213688**
213689** If xOutput is not NULL, then the changeset/patchset is returned to the
213690** user via one or more calls to xOutput, as with the other streaming
213691** interfaces.
213692**
213693** Or, if xOutput is NULL, then (*ppOut) is populated with a pointer to a
213694** buffer containing the output changeset before this function returns. In
213695** this case (*pnOut) is set to the size of the output buffer in bytes. It
213696** is the responsibility of the caller to free the output buffer using
213697** sqlite3_free() when it is no longer required.
213698**
213699** If successful, SQLITE_OK is returned. Or, if an error occurs, an SQLite
213700** error code. If an error occurs and xOutput is NULL, (*ppOut) and (*pnOut)
213701** are both set to 0 before returning.
213702*/
213703static int sessionChangegroupOutput(
213704 sqlite3_changegroup *pGrp,
213705 int (*xOutput)(void *pOut, const void *pData, int nData),
213706 void *pOut,
213707 int *pnOut,
213708 void **ppOut
213709){
213710 int rc = SQLITE_OK;
213711 SessionBuffer buf = {0, 0, 0};
213712 SessionTable *pTab;
213713 assert( xOutput==0 || (ppOut==0 && pnOut==0) );
213714
213715 /* Create the serialized output changeset based on the contents of the
213716 ** hash tables attached to the SessionTable objects in list p->pList.
213717 */
213718 for(pTab=pGrp->pList; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
213719 int i;
213720 if( pTab->nEntry==0 ) continue;
213721
213722 sessionAppendTableHdr(&buf, pGrp->bPatch, pTab, &rc);
213723 for(i=0; i<pTab->nChange; i++){
213724 SessionChange *p;
213725 for(p=pTab->apChange[i]; p; p=p->pNext){
213726 sessionAppendByte(&buf, p->op, &rc);
213727 sessionAppendByte(&buf, p->bIndirect, &rc);
213728 sessionAppendBlob(&buf, p->aRecord, p->nRecord, &rc);
213729 if( rc==SQLITE_OK && xOutput && buf.nBuf>=sessions_strm_chunk_size ){
213730 rc = xOutput(pOut, buf.aBuf, buf.nBuf);
213731 buf.nBuf = 0;
213732 }
213733 }
213734 }
213735 }
213736
213737 if( rc==SQLITE_OK ){
213738 if( xOutput ){
213739 if( buf.nBuf>0 ) rc = xOutput(pOut, buf.aBuf, buf.nBuf);
213740 }else if( ppOut ){
213741 *ppOut = buf.aBuf;
213742 if( pnOut ) *pnOut = buf.nBuf;
213743 buf.aBuf = 0;
213744 }
213745 }
213746 sqlite3_free(buf.aBuf);
213747
213748 return rc;
213749}
213750
213751/*
213752** Allocate a new, empty, sqlite3_changegroup.
213753*/
213754SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp){
213755 int rc = SQLITE_OK; /* Return code */
213756 sqlite3_changegroup *p; /* New object */
213757 p = (sqlite3_changegroup*)sqlite3_malloc(sizeof(sqlite3_changegroup));
213758 if( p==0 ){
213759 rc = SQLITE_NOMEM;
213760 }else{
213761 memset(p, 0, sizeof(sqlite3_changegroup));
213762 }
213763 *pp = p;
213764 return rc;
213765}
213766
213767/*
213768** Add the changeset currently stored in buffer pData, size nData bytes,
213769** to changeset-group p.
213770*/
213771SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup *pGrp, int nData, void *pData){
213772 sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */
213773 int rc; /* Return code */
213774
213775 rc = sqlite3changeset_start(&pIter, nData, pData);
213776 if( rc==SQLITE_OK ){
213777 rc = sessionChangesetToHash(pIter, pGrp, 0);
213778 }
213779 sqlite3changeset_finalize(pIter);
213780 return rc;
213781}
213782
213783/*
213784** Obtain a buffer containing a changeset representing the concatenation
213785** of all changesets added to the group so far.
213786*/
213787SQLITE_API int sqlite3changegroup_output(
213788 sqlite3_changegroup *pGrp,
213789 int *pnData,
213790 void **ppData
213791){
213792 return sessionChangegroupOutput(pGrp, 0, 0, pnData, ppData);
213793}
213794
213795/*
213796** Streaming versions of changegroup_add().
213797*/
213798SQLITE_API int sqlite3changegroup_add_strm(
213799 sqlite3_changegroup *pGrp,
213800 int (*xInput)(void *pIn, void *pData, int *pnData),
213801 void *pIn
213802){
213803 sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */
213804 int rc; /* Return code */
213805
213806 rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
213807 if( rc==SQLITE_OK ){
213808 rc = sessionChangesetToHash(pIter, pGrp, 0);
213809 }
213810 sqlite3changeset_finalize(pIter);
213811 return rc;
213812}
213813
213814/*
213815** Streaming versions of changegroup_output().
213816*/
213817SQLITE_API int sqlite3changegroup_output_strm(
213818 sqlite3_changegroup *pGrp,
213819 int (*xOutput)(void *pOut, const void *pData, int nData),
213820 void *pOut
213821){
213822 return sessionChangegroupOutput(pGrp, xOutput, pOut, 0, 0);
213823}
213824
213825/*
213826** Delete a changegroup object.
213827*/
213828SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup *pGrp){
213829 if( pGrp ){
213830 sessionDeleteTable(0, pGrp->pList);
213831 sqlite3_free(pGrp);
213832 }
213833}
213834
213835/*
213836** Combine two changesets together.
213837*/
213838SQLITE_API int sqlite3changeset_concat(
213839 int nLeft, /* Number of bytes in lhs input */
213840 void *pLeft, /* Lhs input changeset */
213841 int nRight /* Number of bytes in rhs input */,
213842 void *pRight, /* Rhs input changeset */
213843 int *pnOut, /* OUT: Number of bytes in output changeset */
213844 void **ppOut /* OUT: changeset (left <concat> right) */
213845){
213846 sqlite3_changegroup *pGrp;
213847 int rc;
213848
213849 rc = sqlite3changegroup_new(&pGrp);
213850 if( rc==SQLITE_OK ){
213851 rc = sqlite3changegroup_add(pGrp, nLeft, pLeft);
213852 }
213853 if( rc==SQLITE_OK ){
213854 rc = sqlite3changegroup_add(pGrp, nRight, pRight);
213855 }
213856 if( rc==SQLITE_OK ){
213857 rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
213858 }
213859 sqlite3changegroup_delete(pGrp);
213860
213861 return rc;
213862}
213863
213864/*
213865** Streaming version of sqlite3changeset_concat().
213866*/
213867SQLITE_API int sqlite3changeset_concat_strm(
213868 int (*xInputA)(void *pIn, void *pData, int *pnData),
213869 void *pInA,
213870 int (*xInputB)(void *pIn, void *pData, int *pnData),
213871 void *pInB,
213872 int (*xOutput)(void *pOut, const void *pData, int nData),
213873 void *pOut
213874){
213875 sqlite3_changegroup *pGrp;
213876 int rc;
213877
213878 rc = sqlite3changegroup_new(&pGrp);
213879 if( rc==SQLITE_OK ){
213880 rc = sqlite3changegroup_add_strm(pGrp, xInputA, pInA);
213881 }
213882 if( rc==SQLITE_OK ){
213883 rc = sqlite3changegroup_add_strm(pGrp, xInputB, pInB);
213884 }
213885 if( rc==SQLITE_OK ){
213886 rc = sqlite3changegroup_output_strm(pGrp, xOutput, pOut);
213887 }
213888 sqlite3changegroup_delete(pGrp);
213889
213890 return rc;
213891}
213892
213893/*
213894** Changeset rebaser handle.
213895*/
213896struct sqlite3_rebaser {
213897 sqlite3_changegroup grp; /* Hash table */
213898};
213899
213900/*
213901** Buffers a1 and a2 must both contain a sessions module record nCol
213902** fields in size. This function appends an nCol sessions module
213903** record to buffer pBuf that is a copy of a1, except that for
213904** each field that is undefined in a1[], swap in the field from a2[].
213905*/
213906static void sessionAppendRecordMerge(
213907 SessionBuffer *pBuf, /* Buffer to append to */
213908 int nCol, /* Number of columns in each record */
213909 u8 *a1, int n1, /* Record 1 */
213910 u8 *a2, int n2, /* Record 2 */
213911 int *pRc /* IN/OUT: error code */
213912){
213913 sessionBufferGrow(pBuf, n1+n2, pRc);
213914 if( *pRc==SQLITE_OK ){
213915 int i;
213916 u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
213917 for(i=0; i<nCol; i++){
213918 int nn1 = sessionSerialLen(a1);
213919 int nn2 = sessionSerialLen(a2);
213920 if( *a1==0 || *a1==0xFF ){
213921 memcpy(pOut, a2, nn2);
213922 pOut += nn2;
213923 }else{
213924 memcpy(pOut, a1, nn1);
213925 pOut += nn1;
213926 }
213927 a1 += nn1;
213928 a2 += nn2;
213929 }
213930
213931 pBuf->nBuf = pOut-pBuf->aBuf;
213932 assert( pBuf->nBuf<=pBuf->nAlloc );
213933 }
213934}
213935
213936/*
213937** This function is called when rebasing a local UPDATE change against one
213938** or more remote UPDATE changes. The aRec/nRec buffer contains the current
213939** old.* and new.* records for the change. The rebase buffer (a single
213940** record) is in aChange/nChange. The rebased change is appended to buffer
213941** pBuf.
213942**
213943** Rebasing the UPDATE involves:
213944**
213945** * Removing any changes to fields for which the corresponding field
213946** in the rebase buffer is set to "replaced" (type 0xFF). If this
213947** means the UPDATE change updates no fields, nothing is appended
213948** to the output buffer.
213949**
213950** * For each field modified by the local change for which the
213951** corresponding field in the rebase buffer is not "undefined" (0x00)
213952** or "replaced" (0xFF), the old.* value is replaced by the value
213953** in the rebase buffer.
213954*/
213955static void sessionAppendPartialUpdate(
213956 SessionBuffer *pBuf, /* Append record here */
213957 sqlite3_changeset_iter *pIter, /* Iterator pointed at local change */
213958 u8 *aRec, int nRec, /* Local change */
213959 u8 *aChange, int nChange, /* Record to rebase against */
213960 int *pRc /* IN/OUT: Return Code */
213961){
213962 sessionBufferGrow(pBuf, 2+nRec+nChange, pRc);
213963 if( *pRc==SQLITE_OK ){
213964 int bData = 0;
213965 u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
213966 int i;
213967 u8 *a1 = aRec;
213968 u8 *a2 = aChange;
213969
213970 *pOut++ = SQLITE_UPDATE;
213971 *pOut++ = pIter->bIndirect;
213972 for(i=0; i<pIter->nCol; i++){
213973 int n1 = sessionSerialLen(a1);
213974 int n2 = sessionSerialLen(a2);
213975 if( pIter->abPK[i] || a2[0]==0 ){
213976 if( !pIter->abPK[i] && a1[0] ) bData = 1;
213977 memcpy(pOut, a1, n1);
213978 pOut += n1;
213979 }else if( a2[0]!=0xFF ){
213980 bData = 1;
213981 memcpy(pOut, a2, n2);
213982 pOut += n2;
213983 }else{
213984 *pOut++ = '\0';
213985 }
213986 a1 += n1;
213987 a2 += n2;
213988 }
213989 if( bData ){
213990 a2 = aChange;
213991 for(i=0; i<pIter->nCol; i++){
213992 int n1 = sessionSerialLen(a1);
213993 int n2 = sessionSerialLen(a2);
213994 if( pIter->abPK[i] || a2[0]!=0xFF ){
213995 memcpy(pOut, a1, n1);
213996 pOut += n1;
213997 }else{
213998 *pOut++ = '\0';
213999 }
214000 a1 += n1;
214001 a2 += n2;
214002 }
214003 pBuf->nBuf = (pOut - pBuf->aBuf);
214004 }
214005 }
214006}
214007
214008/*
214009** pIter is configured to iterate through a changeset. This function rebases
214010** that changeset according to the current configuration of the rebaser
214011** object passed as the first argument. If no error occurs and argument xOutput
214012** is not NULL, then the changeset is returned to the caller by invoking
214013** xOutput zero or more times and SQLITE_OK returned. Or, if xOutput is NULL,
214014** then (*ppOut) is set to point to a buffer containing the rebased changeset
214015** before this function returns. In this case (*pnOut) is set to the size of
214016** the buffer in bytes. It is the responsibility of the caller to eventually
214017** free the (*ppOut) buffer using sqlite3_free().
214018**
214019** If an error occurs, an SQLite error code is returned. If ppOut and
214020** pnOut are not NULL, then the two output parameters are set to 0 before
214021** returning.
214022*/
214023static int sessionRebase(
214024 sqlite3_rebaser *p, /* Rebaser hash table */
214025 sqlite3_changeset_iter *pIter, /* Input data */
214026 int (*xOutput)(void *pOut, const void *pData, int nData),
214027 void *pOut, /* Context for xOutput callback */
214028 int *pnOut, /* OUT: Number of bytes in output changeset */
214029 void **ppOut /* OUT: Inverse of pChangeset */
214030){
214031 int rc = SQLITE_OK;
214032 u8 *aRec = 0;
214033 int nRec = 0;
214034 int bNew = 0;
214035 SessionTable *pTab = 0;
214036 SessionBuffer sOut = {0,0,0};
214037
214038 while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, &bNew) ){
214039 SessionChange *pChange = 0;
214040 int bDone = 0;
214041
214042 if( bNew ){
214043 const char *zTab = pIter->zTab;
214044 for(pTab=p->grp.pList; pTab; pTab=pTab->pNext){
214045 if( 0==sqlite3_stricmp(pTab->zName, zTab) ) break;
214046 }
214047 bNew = 0;
214048
214049 /* A patchset may not be rebased */
214050 if( pIter->bPatchset ){
214051 rc = SQLITE_ERROR;
214052 }
214053
214054 /* Append a table header to the output for this new table */
214055 sessionAppendByte(&sOut, pIter->bPatchset ? 'P' : 'T', &rc);
214056 sessionAppendVarint(&sOut, pIter->nCol, &rc);
214057 sessionAppendBlob(&sOut, pIter->abPK, pIter->nCol, &rc);
214058 sessionAppendBlob(&sOut,(u8*)pIter->zTab,(int)strlen(pIter->zTab)+1,&rc);
214059 }
214060
214061 if( pTab && rc==SQLITE_OK ){
214062 int iHash = sessionChangeHash(pTab, 0, aRec, pTab->nChange);
214063
214064 for(pChange=pTab->apChange[iHash]; pChange; pChange=pChange->pNext){
214065 if( sessionChangeEqual(pTab, 0, aRec, 0, pChange->aRecord) ){
214066 break;
214067 }
214068 }
214069 }
214070
214071 if( pChange ){
214072 assert( pChange->op==SQLITE_DELETE || pChange->op==SQLITE_INSERT );
214073 switch( pIter->op ){
214074 case SQLITE_INSERT:
214075 if( pChange->op==SQLITE_INSERT ){
214076 bDone = 1;
214077 if( pChange->bIndirect==0 ){
214078 sessionAppendByte(&sOut, SQLITE_UPDATE, &rc);
214079 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
214080 sessionAppendBlob(&sOut, pChange->aRecord, pChange->nRecord, &rc);
214081 sessionAppendBlob(&sOut, aRec, nRec, &rc);
214082 }
214083 }
214084 break;
214085
214086 case SQLITE_UPDATE:
214087 bDone = 1;
214088 if( pChange->op==SQLITE_DELETE ){
214089 if( pChange->bIndirect==0 ){
214090 u8 *pCsr = aRec;
214091 sessionSkipRecord(&pCsr, pIter->nCol);
214092 sessionAppendByte(&sOut, SQLITE_INSERT, &rc);
214093 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
214094 sessionAppendRecordMerge(&sOut, pIter->nCol,
214095 pCsr, nRec-(pCsr-aRec),
214096 pChange->aRecord, pChange->nRecord, &rc
214097 );
214098 }
214099 }else{
214100 sessionAppendPartialUpdate(&sOut, pIter,
214101 aRec, nRec, pChange->aRecord, pChange->nRecord, &rc
214102 );
214103 }
214104 break;
214105
214106 default:
214107 assert( pIter->op==SQLITE_DELETE );
214108 bDone = 1;
214109 if( pChange->op==SQLITE_INSERT ){
214110 sessionAppendByte(&sOut, SQLITE_DELETE, &rc);
214111 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
214112 sessionAppendRecordMerge(&sOut, pIter->nCol,
214113 pChange->aRecord, pChange->nRecord, aRec, nRec, &rc
214114 );
214115 }
214116 break;
214117 }
214118 }
214119
214120 if( bDone==0 ){
214121 sessionAppendByte(&sOut, pIter->op, &rc);
214122 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
214123 sessionAppendBlob(&sOut, aRec, nRec, &rc);
214124 }
214125 if( rc==SQLITE_OK && xOutput && sOut.nBuf>sessions_strm_chunk_size ){
214126 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
214127 sOut.nBuf = 0;
214128 }
214129 if( rc ) break;
214130 }
214131
214132 if( rc!=SQLITE_OK ){
214133 sqlite3_free(sOut.aBuf);
214134 memset(&sOut, 0, sizeof(sOut));
214135 }
214136
214137 if( rc==SQLITE_OK ){
214138 if( xOutput ){
214139 if( sOut.nBuf>0 ){
214140 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
214141 }
214142 }else if( ppOut ){
214143 *ppOut = (void*)sOut.aBuf;
214144 *pnOut = sOut.nBuf;
214145 sOut.aBuf = 0;
214146 }
214147 }
214148 sqlite3_free(sOut.aBuf);
214149 return rc;
214150}
214151
214152/*
214153** Create a new rebaser object.
214154*/
214155SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew){
214156 int rc = SQLITE_OK;
214157 sqlite3_rebaser *pNew;
214158
214159 pNew = sqlite3_malloc(sizeof(sqlite3_rebaser));
214160 if( pNew==0 ){
214161 rc = SQLITE_NOMEM;
214162 }else{
214163 memset(pNew, 0, sizeof(sqlite3_rebaser));
214164 }
214165 *ppNew = pNew;
214166 return rc;
214167}
214168
214169/*
214170** Call this one or more times to configure a rebaser.
214171*/
214172SQLITE_API int sqlite3rebaser_configure(
214173 sqlite3_rebaser *p,
214174 int nRebase, const void *pRebase
214175){
214176 sqlite3_changeset_iter *pIter = 0; /* Iterator opened on pData/nData */
214177 int rc; /* Return code */
214178 rc = sqlite3changeset_start(&pIter, nRebase, (void*)pRebase);
214179 if( rc==SQLITE_OK ){
214180 rc = sessionChangesetToHash(pIter, &p->grp, 1);
214181 }
214182 sqlite3changeset_finalize(pIter);
214183 return rc;
214184}
214185
214186/*
214187** Rebase a changeset according to current rebaser configuration
214188*/
214189SQLITE_API int sqlite3rebaser_rebase(
214190 sqlite3_rebaser *p,
214191 int nIn, const void *pIn,
214192 int *pnOut, void **ppOut
214193){
214194 sqlite3_changeset_iter *pIter = 0; /* Iterator to skip through input */
214195 int rc = sqlite3changeset_start(&pIter, nIn, (void*)pIn);
214196
214197 if( rc==SQLITE_OK ){
214198 rc = sessionRebase(p, pIter, 0, 0, pnOut, ppOut);
214199 sqlite3changeset_finalize(pIter);
214200 }
214201
214202 return rc;
214203}
214204
214205/*
214206** Rebase a changeset according to current rebaser configuration
214207*/
214208SQLITE_API int sqlite3rebaser_rebase_strm(
214209 sqlite3_rebaser *p,
214210 int (*xInput)(void *pIn, void *pData, int *pnData),
214211 void *pIn,
214212 int (*xOutput)(void *pOut, const void *pData, int nData),
214213 void *pOut
214214){
214215 sqlite3_changeset_iter *pIter = 0; /* Iterator to skip through input */
214216 int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
214217
214218 if( rc==SQLITE_OK ){
214219 rc = sessionRebase(p, pIter, xOutput, pOut, 0, 0);
214220 sqlite3changeset_finalize(pIter);
214221 }
214222
214223 return rc;
214224}
214225
214226/*
214227** Destroy a rebaser object
214228*/
214229SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p){
214230 if( p ){
214231 sessionDeleteTable(0, p->grp.pList);
214232 sqlite3_free(p);
214233 }
214234}
214235
214236/*
214237** Global configuration
214238*/
214239SQLITE_API int sqlite3session_config(int op, void *pArg){
214240 int rc = SQLITE_OK;
214241 switch( op ){
214242 case SQLITE_SESSION_CONFIG_STRMSIZE: {
214243 int *pInt = (int*)pArg;
214244 if( *pInt>0 ){
214245 sessions_strm_chunk_size = *pInt;
214246 }
214247 *pInt = sessions_strm_chunk_size;
214248 break;
214249 }
214250 default:
214251 rc = SQLITE_MISUSE;
214252 break;
214253 }
214254 return rc;
214255}
214256
214257#endif /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */
214258
214259/************** End of sqlite3session.c **************************************/
214260/************** Begin file fts5.c ********************************************/
214261
214262
214263#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS5)
214264
214265#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
214266# define NDEBUG 1
214267#endif
214268#if defined(NDEBUG) && defined(SQLITE_DEBUG)
214269# undef NDEBUG
214270#endif
214271
214272/*
214273** 2014 May 31
214274**
214275** The author disclaims copyright to this source code. In place of
214276** a legal notice, here is a blessing:
214277**
214278** May you do good and not evil.
214279** May you find forgiveness for yourself and forgive others.
214280** May you share freely, never taking more than you give.
214281**
214282******************************************************************************
214283**
214284** Interfaces to extend FTS5. Using the interfaces defined in this file,
214285** FTS5 may be extended with:
214286**
214287** * custom tokenizers, and
214288** * custom auxiliary functions.
214289*/
214290
214291
214292#ifndef _FTS5_H
214293#define _FTS5_H
214294
214295/* #include "sqlite3.h" */
214296
214297#if 0
214298extern "C" {
214299#endif
214300
214301/*************************************************************************
214302** CUSTOM AUXILIARY FUNCTIONS
214303**
214304** Virtual table implementations may overload SQL functions by implementing
214305** the sqlite3_module.xFindFunction() method.
214306*/
214307
214308typedef struct Fts5ExtensionApi Fts5ExtensionApi;
214309typedef struct Fts5Context Fts5Context;
214310typedef struct Fts5PhraseIter Fts5PhraseIter;
214311
214312typedef void (*fts5_extension_function)(
214313 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
214314 Fts5Context *pFts, /* First arg to pass to pApi functions */
214315 sqlite3_context *pCtx, /* Context for returning result/error */
214316 int nVal, /* Number of values in apVal[] array */
214317 sqlite3_value **apVal /* Array of trailing arguments */
214318);
214319
214320struct Fts5PhraseIter {
214321 const unsigned char *a;
214322 const unsigned char *b;
214323};
214324
214325/*
214326** EXTENSION API FUNCTIONS
214327**
214328** xUserData(pFts):
214329** Return a copy of the context pointer the extension function was
214330** registered with.
214331**
214332** xColumnTotalSize(pFts, iCol, pnToken):
214333** If parameter iCol is less than zero, set output variable *pnToken
214334** to the total number of tokens in the FTS5 table. Or, if iCol is
214335** non-negative but less than the number of columns in the table, return
214336** the total number of tokens in column iCol, considering all rows in
214337** the FTS5 table.
214338**
214339** If parameter iCol is greater than or equal to the number of columns
214340** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
214341** an OOM condition or IO error), an appropriate SQLite error code is
214342** returned.
214343**
214344** xColumnCount(pFts):
214345** Return the number of columns in the table.
214346**
214347** xColumnSize(pFts, iCol, pnToken):
214348** If parameter iCol is less than zero, set output variable *pnToken
214349** to the total number of tokens in the current row. Or, if iCol is
214350** non-negative but less than the number of columns in the table, set
214351** *pnToken to the number of tokens in column iCol of the current row.
214352**
214353** If parameter iCol is greater than or equal to the number of columns
214354** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
214355** an OOM condition or IO error), an appropriate SQLite error code is
214356** returned.
214357**
214358** This function may be quite inefficient if used with an FTS5 table
214359** created with the "columnsize=0" option.
214360**
214361** xColumnText:
214362** This function attempts to retrieve the text of column iCol of the
214363** current document. If successful, (*pz) is set to point to a buffer
214364** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
214365** (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
214366** if an error occurs, an SQLite error code is returned and the final values
214367** of (*pz) and (*pn) are undefined.
214368**
214369** xPhraseCount:
214370** Returns the number of phrases in the current query expression.
214371**
214372** xPhraseSize:
214373** Returns the number of tokens in phrase iPhrase of the query. Phrases
214374** are numbered starting from zero.
214375**
214376** xInstCount:
214377** Set *pnInst to the total number of occurrences of all phrases within
214378** the query within the current row. Return SQLITE_OK if successful, or
214379** an error code (i.e. SQLITE_NOMEM) if an error occurs.
214380**
214381** This API can be quite slow if used with an FTS5 table created with the
214382** "detail=none" or "detail=column" option. If the FTS5 table is created
214383** with either "detail=none" or "detail=column" and "content=" option
214384** (i.e. if it is a contentless table), then this API always returns 0.
214385**
214386** xInst:
214387** Query for the details of phrase match iIdx within the current row.
214388** Phrase matches are numbered starting from zero, so the iIdx argument
214389** should be greater than or equal to zero and smaller than the value
214390** output by xInstCount().
214391**
214392** Usually, output parameter *piPhrase is set to the phrase number, *piCol
214393** to the column in which it occurs and *piOff the token offset of the
214394** first token of the phrase. Returns SQLITE_OK if successful, or an error
214395** code (i.e. SQLITE_NOMEM) if an error occurs.
214396**
214397** This API can be quite slow if used with an FTS5 table created with the
214398** "detail=none" or "detail=column" option.
214399**
214400** xRowid:
214401** Returns the rowid of the current row.
214402**
214403** xTokenize:
214404** Tokenize text using the tokenizer belonging to the FTS5 table.
214405**
214406** xQueryPhrase(pFts5, iPhrase, pUserData, xCallback):
214407** This API function is used to query the FTS table for phrase iPhrase
214408** of the current query. Specifically, a query equivalent to:
214409**
214410** ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid
214411**
214412** with $p set to a phrase equivalent to the phrase iPhrase of the
214413** current query is executed. Any column filter that applies to
214414** phrase iPhrase of the current query is included in $p. For each
214415** row visited, the callback function passed as the fourth argument
214416** is invoked. The context and API objects passed to the callback
214417** function may be used to access the properties of each matched row.
214418** Invoking Api.xUserData() returns a copy of the pointer passed as
214419** the third argument to pUserData.
214420**
214421** If the callback function returns any value other than SQLITE_OK, the
214422** query is abandoned and the xQueryPhrase function returns immediately.
214423** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
214424** Otherwise, the error code is propagated upwards.
214425**
214426** If the query runs to completion without incident, SQLITE_OK is returned.
214427** Or, if some error occurs before the query completes or is aborted by
214428** the callback, an SQLite error code is returned.
214429**
214430**
214431** xSetAuxdata(pFts5, pAux, xDelete)
214432**
214433** Save the pointer passed as the second argument as the extension function's
214434** "auxiliary data". The pointer may then be retrieved by the current or any
214435** future invocation of the same fts5 extension function made as part of
214436** the same MATCH query using the xGetAuxdata() API.
214437**
214438** Each extension function is allocated a single auxiliary data slot for
214439** each FTS query (MATCH expression). If the extension function is invoked
214440** more than once for a single FTS query, then all invocations share a
214441** single auxiliary data context.
214442**
214443** If there is already an auxiliary data pointer when this function is
214444** invoked, then it is replaced by the new pointer. If an xDelete callback
214445** was specified along with the original pointer, it is invoked at this
214446** point.
214447**
214448** The xDelete callback, if one is specified, is also invoked on the
214449** auxiliary data pointer after the FTS5 query has finished.
214450**
214451** If an error (e.g. an OOM condition) occurs within this function,
214452** the auxiliary data is set to NULL and an error code returned. If the
214453** xDelete parameter was not NULL, it is invoked on the auxiliary data
214454** pointer before returning.
214455**
214456**
214457** xGetAuxdata(pFts5, bClear)
214458**
214459** Returns the current auxiliary data pointer for the fts5 extension
214460** function. See the xSetAuxdata() method for details.
214461**
214462** If the bClear argument is non-zero, then the auxiliary data is cleared
214463** (set to NULL) before this function returns. In this case the xDelete,
214464** if any, is not invoked.
214465**
214466**
214467** xRowCount(pFts5, pnRow)
214468**
214469** This function is used to retrieve the total number of rows in the table.
214470** In other words, the same value that would be returned by:
214471**
214472** SELECT count(*) FROM ftstable;
214473**
214474** xPhraseFirst()
214475** This function is used, along with type Fts5PhraseIter and the xPhraseNext
214476** method, to iterate through all instances of a single query phrase within
214477** the current row. This is the same information as is accessible via the
214478** xInstCount/xInst APIs. While the xInstCount/xInst APIs are more convenient
214479** to use, this API may be faster under some circumstances. To iterate
214480** through instances of phrase iPhrase, use the following code:
214481**
214482** Fts5PhraseIter iter;
214483** int iCol, iOff;
214484** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
214485** iCol>=0;
214486** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
214487** ){
214488** // An instance of phrase iPhrase at offset iOff of column iCol
214489** }
214490**
214491** The Fts5PhraseIter structure is defined above. Applications should not
214492** modify this structure directly - it should only be used as shown above
214493** with the xPhraseFirst() and xPhraseNext() API methods (and by
214494** xPhraseFirstColumn() and xPhraseNextColumn() as illustrated below).
214495**
214496** This API can be quite slow if used with an FTS5 table created with the
214497** "detail=none" or "detail=column" option. If the FTS5 table is created
214498** with either "detail=none" or "detail=column" and "content=" option
214499** (i.e. if it is a contentless table), then this API always iterates
214500** through an empty set (all calls to xPhraseFirst() set iCol to -1).
214501**
214502** xPhraseNext()
214503** See xPhraseFirst above.
214504**
214505** xPhraseFirstColumn()
214506** This function and xPhraseNextColumn() are similar to the xPhraseFirst()
214507** and xPhraseNext() APIs described above. The difference is that instead
214508** of iterating through all instances of a phrase in the current row, these
214509** APIs are used to iterate through the set of columns in the current row
214510** that contain one or more instances of a specified phrase. For example:
214511**
214512** Fts5PhraseIter iter;
214513** int iCol;
214514** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
214515** iCol>=0;
214516** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
214517** ){
214518** // Column iCol contains at least one instance of phrase iPhrase
214519** }
214520**
214521** This API can be quite slow if used with an FTS5 table created with the
214522** "detail=none" option. If the FTS5 table is created with either
214523** "detail=none" "content=" option (i.e. if it is a contentless table),
214524** then this API always iterates through an empty set (all calls to
214525** xPhraseFirstColumn() set iCol to -1).
214526**
214527** The information accessed using this API and its companion
214528** xPhraseFirstColumn() may also be obtained using xPhraseFirst/xPhraseNext
214529** (or xInst/xInstCount). The chief advantage of this API is that it is
214530** significantly more efficient than those alternatives when used with
214531** "detail=column" tables.
214532**
214533** xPhraseNextColumn()
214534** See xPhraseFirstColumn above.
214535*/
214536struct Fts5ExtensionApi {
214537 int iVersion; /* Currently always set to 3 */
214538
214539 void *(*xUserData)(Fts5Context*);
214540
214541 int (*xColumnCount)(Fts5Context*);
214542 int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow);
214543 int (*xColumnTotalSize)(Fts5Context*, int iCol, sqlite3_int64 *pnToken);
214544
214545 int (*xTokenize)(Fts5Context*,
214546 const char *pText, int nText, /* Text to tokenize */
214547 void *pCtx, /* Context passed to xToken() */
214548 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
214549 );
214550
214551 int (*xPhraseCount)(Fts5Context*);
214552 int (*xPhraseSize)(Fts5Context*, int iPhrase);
214553
214554 int (*xInstCount)(Fts5Context*, int *pnInst);
214555 int (*xInst)(Fts5Context*, int iIdx, int *piPhrase, int *piCol, int *piOff);
214556
214557 sqlite3_int64 (*xRowid)(Fts5Context*);
214558 int (*xColumnText)(Fts5Context*, int iCol, const char **pz, int *pn);
214559 int (*xColumnSize)(Fts5Context*, int iCol, int *pnToken);
214560
214561 int (*xQueryPhrase)(Fts5Context*, int iPhrase, void *pUserData,
214562 int(*)(const Fts5ExtensionApi*,Fts5Context*,void*)
214563 );
214564 int (*xSetAuxdata)(Fts5Context*, void *pAux, void(*xDelete)(void*));
214565 void *(*xGetAuxdata)(Fts5Context*, int bClear);
214566
214567 int (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*);
214568 void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff);
214569
214570 int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*);
214571 void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol);
214572};
214573
214574/*
214575** CUSTOM AUXILIARY FUNCTIONS
214576*************************************************************************/
214577
214578/*************************************************************************
214579** CUSTOM TOKENIZERS
214580**
214581** Applications may also register custom tokenizer types. A tokenizer
214582** is registered by providing fts5 with a populated instance of the
214583** following structure. All structure methods must be defined, setting
214584** any member of the fts5_tokenizer struct to NULL leads to undefined
214585** behaviour. The structure methods are expected to function as follows:
214586**
214587** xCreate:
214588** This function is used to allocate and initialize a tokenizer instance.
214589** A tokenizer instance is required to actually tokenize text.
214590**
214591** The first argument passed to this function is a copy of the (void*)
214592** pointer provided by the application when the fts5_tokenizer object
214593** was registered with FTS5 (the third argument to xCreateTokenizer()).
214594** The second and third arguments are an array of nul-terminated strings
214595** containing the tokenizer arguments, if any, specified following the
214596** tokenizer name as part of the CREATE VIRTUAL TABLE statement used
214597** to create the FTS5 table.
214598**
214599** The final argument is an output variable. If successful, (*ppOut)
214600** should be set to point to the new tokenizer handle and SQLITE_OK
214601** returned. If an error occurs, some value other than SQLITE_OK should
214602** be returned. In this case, fts5 assumes that the final value of *ppOut
214603** is undefined.
214604**
214605** xDelete:
214606** This function is invoked to delete a tokenizer handle previously
214607** allocated using xCreate(). Fts5 guarantees that this function will
214608** be invoked exactly once for each successful call to xCreate().
214609**
214610** xTokenize:
214611** This function is expected to tokenize the nText byte string indicated
214612** by argument pText. pText may or may not be nul-terminated. The first
214613** argument passed to this function is a pointer to an Fts5Tokenizer object
214614** returned by an earlier call to xCreate().
214615**
214616** The second argument indicates the reason that FTS5 is requesting
214617** tokenization of the supplied text. This is always one of the following
214618** four values:
214619**
214620** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
214621** or removed from the FTS table. The tokenizer is being invoked to
214622** determine the set of tokens to add to (or delete from) the
214623** FTS index.
214624**
214625** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
214626** against the FTS index. The tokenizer is being called to tokenize
214627** a bareword or quoted string specified as part of the query.
214628**
214629** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
214630** FTS5_TOKENIZE_QUERY, except that the bareword or quoted string is
214631** followed by a "*" character, indicating that the last token
214632** returned by the tokenizer will be treated as a token prefix.
214633**
214634** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
214635** satisfy an fts5_api.xTokenize() request made by an auxiliary
214636** function. Or an fts5_api.xColumnSize() request made by the same
214637** on a columnsize=0 database.
214638** </ul>
214639**
214640** For each token in the input string, the supplied callback xToken() must
214641** be invoked. The first argument to it should be a copy of the pointer
214642** passed as the second argument to xTokenize(). The third and fourth
214643** arguments are a pointer to a buffer containing the token text, and the
214644** size of the token in bytes. The 4th and 5th arguments are the byte offsets
214645** of the first byte of and first byte immediately following the text from
214646** which the token is derived within the input.
214647**
214648** The second argument passed to the xToken() callback ("tflags") should
214649** normally be set to 0. The exception is if the tokenizer supports
214650** synonyms. In this case see the discussion below for details.
214651**
214652** FTS5 assumes the xToken() callback is invoked for each token in the
214653** order that they occur within the input text.
214654**
214655** If an xToken() callback returns any value other than SQLITE_OK, then
214656** the tokenization should be abandoned and the xTokenize() method should
214657** immediately return a copy of the xToken() return value. Or, if the
214658** input buffer is exhausted, xTokenize() should return SQLITE_OK. Finally,
214659** if an error occurs with the xTokenize() implementation itself, it
214660** may abandon the tokenization and return any error code other than
214661** SQLITE_OK or SQLITE_DONE.
214662**
214663** SYNONYM SUPPORT
214664**
214665** Custom tokenizers may also support synonyms. Consider a case in which a
214666** user wishes to query for a phrase such as "first place". Using the
214667** built-in tokenizers, the FTS5 query 'first + place' will match instances
214668** of "first place" within the document set, but not alternative forms
214669** such as "1st place". In some applications, it would be better to match
214670** all instances of "first place" or "1st place" regardless of which form
214671** the user specified in the MATCH query text.
214672**
214673** There are several ways to approach this in FTS5:
214674**
214675** <ol><li> By mapping all synonyms to a single token. In this case, using
214676** the above example, this means that the tokenizer returns the
214677** same token for inputs "first" and "1st". Say that token is in
214678** fact "first", so that when the user inserts the document "I won
214679** 1st place" entries are added to the index for tokens "i", "won",
214680** "first" and "place". If the user then queries for '1st + place',
214681** the tokenizer substitutes "first" for "1st" and the query works
214682** as expected.
214683**
214684** <li> By querying the index for all synonyms of each query term
214685** separately. In this case, when tokenizing query text, the
214686** tokenizer may provide multiple synonyms for a single term
214687** within the document. FTS5 then queries the index for each
214688** synonym individually. For example, faced with the query:
214689**
214690** <codeblock>
214691** ... MATCH 'first place'</codeblock>
214692**
214693** the tokenizer offers both "1st" and "first" as synonyms for the
214694** first token in the MATCH query and FTS5 effectively runs a query
214695** similar to:
214696**
214697** <codeblock>
214698** ... MATCH '(first OR 1st) place'</codeblock>
214699**
214700** except that, for the purposes of auxiliary functions, the query
214701** still appears to contain just two phrases - "(first OR 1st)"
214702** being treated as a single phrase.
214703**
214704** <li> By adding multiple synonyms for a single term to the FTS index.
214705** Using this method, when tokenizing document text, the tokenizer
214706** provides multiple synonyms for each token. So that when a
214707** document such as "I won first place" is tokenized, entries are
214708** added to the FTS index for "i", "won", "first", "1st" and
214709** "place".
214710**
214711** This way, even if the tokenizer does not provide synonyms
214712** when tokenizing query text (it should not - to do so would be
214713** inefficient), it doesn't matter if the user queries for
214714** 'first + place' or '1st + place', as there are entries in the
214715** FTS index corresponding to both forms of the first token.
214716** </ol>
214717**
214718** Whether it is parsing document or query text, any call to xToken that
214719** specifies a <i>tflags</i> argument with the FTS5_TOKEN_COLOCATED bit
214720** is considered to supply a synonym for the previous token. For example,
214721** when parsing the document "I won first place", a tokenizer that supports
214722** synonyms would call xToken() 5 times, as follows:
214723**
214724** <codeblock>
214725** xToken(pCtx, 0, "i", 1, 0, 1);
214726** xToken(pCtx, 0, "won", 3, 2, 5);
214727** xToken(pCtx, 0, "first", 5, 6, 11);
214728** xToken(pCtx, FTS5_TOKEN_COLOCATED, "1st", 3, 6, 11);
214729** xToken(pCtx, 0, "place", 5, 12, 17);
214730**</codeblock>
214731**
214732** It is an error to specify the FTS5_TOKEN_COLOCATED flag the first time
214733** xToken() is called. Multiple synonyms may be specified for a single token
214734** by making multiple calls to xToken(FTS5_TOKEN_COLOCATED) in sequence.
214735** There is no limit to the number of synonyms that may be provided for a
214736** single token.
214737**
214738** In many cases, method (1) above is the best approach. It does not add
214739** extra data to the FTS index or require FTS5 to query for multiple terms,
214740** so it is efficient in terms of disk space and query speed. However, it
214741** does not support prefix queries very well. If, as suggested above, the
214742** token "first" is substituted for "1st" by the tokenizer, then the query:
214743**
214744** <codeblock>
214745** ... MATCH '1s*'</codeblock>
214746**
214747** will not match documents that contain the token "1st" (as the tokenizer
214748** will probably not map "1s" to any prefix of "first").
214749**
214750** For full prefix support, method (3) may be preferred. In this case,
214751** because the index contains entries for both "first" and "1st", prefix
214752** queries such as 'fi*' or '1s*' will match correctly. However, because
214753** extra entries are added to the FTS index, this method uses more space
214754** within the database.
214755**
214756** Method (2) offers a midpoint between (1) and (3). Using this method,
214757** a query such as '1s*' will match documents that contain the literal
214758** token "1st", but not "first" (assuming the tokenizer is not able to
214759** provide synonyms for prefixes). However, a non-prefix query like '1st'
214760** will match against "1st" and "first". This method does not require
214761** extra disk space, as no extra entries are added to the FTS index.
214762** On the other hand, it may require more CPU cycles to run MATCH queries,
214763** as separate queries of the FTS index are required for each synonym.
214764**
214765** When using methods (2) or (3), it is important that the tokenizer only
214766** provide synonyms when tokenizing document text (method (2)) or query
214767** text (method (3)), not both. Doing so will not cause any errors, but is
214768** inefficient.
214769*/
214770typedef struct Fts5Tokenizer Fts5Tokenizer;
214771typedef struct fts5_tokenizer fts5_tokenizer;
214772struct fts5_tokenizer {
214773 int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
214774 void (*xDelete)(Fts5Tokenizer*);
214775 int (*xTokenize)(Fts5Tokenizer*,
214776 void *pCtx,
214777 int flags, /* Mask of FTS5_TOKENIZE_* flags */
214778 const char *pText, int nText,
214779 int (*xToken)(
214780 void *pCtx, /* Copy of 2nd argument to xTokenize() */
214781 int tflags, /* Mask of FTS5_TOKEN_* flags */
214782 const char *pToken, /* Pointer to buffer containing token */
214783 int nToken, /* Size of token in bytes */
214784 int iStart, /* Byte offset of token within input text */
214785 int iEnd /* Byte offset of end of token within input text */
214786 )
214787 );
214788};
214789
214790/* Flags that may be passed as the third argument to xTokenize() */
214791#define FTS5_TOKENIZE_QUERY 0x0001
214792#define FTS5_TOKENIZE_PREFIX 0x0002
214793#define FTS5_TOKENIZE_DOCUMENT 0x0004
214794#define FTS5_TOKENIZE_AUX 0x0008
214795
214796/* Flags that may be passed by the tokenizer implementation back to FTS5
214797** as the third argument to the supplied xToken callback. */
214798#define FTS5_TOKEN_COLOCATED 0x0001 /* Same position as prev. token */
214799
214800/*
214801** END OF CUSTOM TOKENIZERS
214802*************************************************************************/
214803
214804/*************************************************************************
214805** FTS5 EXTENSION REGISTRATION API
214806*/
214807typedef struct fts5_api fts5_api;
214808struct fts5_api {
214809 int iVersion; /* Currently always set to 2 */
214810
214811 /* Create a new tokenizer */
214812 int (*xCreateTokenizer)(
214813 fts5_api *pApi,
214814 const char *zName,
214815 void *pContext,
214816 fts5_tokenizer *pTokenizer,
214817 void (*xDestroy)(void*)
214818 );
214819
214820 /* Find an existing tokenizer */
214821 int (*xFindTokenizer)(
214822 fts5_api *pApi,
214823 const char *zName,
214824 void **ppContext,
214825 fts5_tokenizer *pTokenizer
214826 );
214827
214828 /* Create a new auxiliary function */
214829 int (*xCreateFunction)(
214830 fts5_api *pApi,
214831 const char *zName,
214832 void *pContext,
214833 fts5_extension_function xFunction,
214834 void (*xDestroy)(void*)
214835 );
214836};
214837
214838/*
214839** END OF REGISTRATION API
214840*************************************************************************/
214841
214842#if 0
214843} /* end of the 'extern "C"' block */
214844#endif
214845
214846#endif /* _FTS5_H */
214847
214848/*
214849** 2014 May 31
214850**
214851** The author disclaims copyright to this source code. In place of
214852** a legal notice, here is a blessing:
214853**
214854** May you do good and not evil.
214855** May you find forgiveness for yourself and forgive others.
214856** May you share freely, never taking more than you give.
214857**
214858******************************************************************************
214859**
214860*/
214861#ifndef _FTS5INT_H
214862#define _FTS5INT_H
214863
214864/* #include "fts5.h" */
214865/* #include "sqlite3ext.h" */
214866SQLITE_EXTENSION_INIT1
214867
214868/* #include <string.h> */
214869/* #include <assert.h> */
214870
214871#ifndef SQLITE_AMALGAMATION
214872
214873typedef unsigned char u8;
214874typedef unsigned int u32;
214875typedef unsigned short u16;
214876typedef short i16;
214877typedef sqlite3_int64 i64;
214878typedef sqlite3_uint64 u64;
214879
214880#ifndef ArraySize
214881# define ArraySize(x) ((int)(sizeof(x) / sizeof(x[0])))
214882#endif
214883
214884#define testcase(x)
214885
214886#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
214887# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
214888#endif
214889#if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
214890# define ALWAYS(X) (1)
214891# define NEVER(X) (0)
214892#elif !defined(NDEBUG)
214893# define ALWAYS(X) ((X)?1:(assert(0),0))
214894# define NEVER(X) ((X)?(assert(0),1):0)
214895#else
214896# define ALWAYS(X) (X)
214897# define NEVER(X) (X)
214898#endif
214899
214900#define MIN(x,y) (((x) < (y)) ? (x) : (y))
214901#define MAX(x,y) (((x) > (y)) ? (x) : (y))
214902
214903/*
214904** Constants for the largest and smallest possible 64-bit signed integers.
214905*/
214906# define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
214907# define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
214908
214909#endif
214910
214911/* Truncate very long tokens to this many bytes. Hard limit is
214912** (65536-1-1-4-9)==65521 bytes. The limiting factor is the 16-bit offset
214913** field that occurs at the start of each leaf page (see fts5_index.c). */
214914#define FTS5_MAX_TOKEN_SIZE 32768
214915
214916/*
214917** Maximum number of prefix indexes on single FTS5 table. This must be
214918** less than 32. If it is set to anything large than that, an #error
214919** directive in fts5_index.c will cause the build to fail.
214920*/
214921#define FTS5_MAX_PREFIX_INDEXES 31
214922
214923/*
214924** Maximum segments permitted in a single index
214925*/
214926#define FTS5_MAX_SEGMENT 2000
214927
214928#define FTS5_DEFAULT_NEARDIST 10
214929#define FTS5_DEFAULT_RANK "bm25"
214930
214931/* Name of rank and rowid columns */
214932#define FTS5_RANK_NAME "rank"
214933#define FTS5_ROWID_NAME "rowid"
214934
214935#ifdef SQLITE_DEBUG
214936# define FTS5_CORRUPT sqlite3Fts5Corrupt()
214937static int sqlite3Fts5Corrupt(void);
214938#else
214939# define FTS5_CORRUPT SQLITE_CORRUPT_VTAB
214940#endif
214941
214942/*
214943** The assert_nc() macro is similar to the assert() macro, except that it
214944** is used for assert() conditions that are true only if it can be
214945** guranteed that the database is not corrupt.
214946*/
214947#ifdef SQLITE_DEBUG
214948SQLITE_API extern int sqlite3_fts5_may_be_corrupt;
214949# define assert_nc(x) assert(sqlite3_fts5_may_be_corrupt || (x))
214950#else
214951# define assert_nc(x) assert(x)
214952#endif
214953
214954/*
214955** A version of memcmp() that does not cause asan errors if one of the pointer
214956** parameters is NULL and the number of bytes to compare is zero.
214957*/
214958#define fts5Memcmp(s1, s2, n) ((n)<=0 ? 0 : memcmp((s1), (s2), (n)))
214959
214960/* Mark a function parameter as unused, to suppress nuisance compiler
214961** warnings. */
214962#ifndef UNUSED_PARAM
214963# define UNUSED_PARAM(X) (void)(X)
214964#endif
214965
214966#ifndef UNUSED_PARAM2
214967# define UNUSED_PARAM2(X, Y) (void)(X), (void)(Y)
214968#endif
214969
214970typedef struct Fts5Global Fts5Global;
214971typedef struct Fts5Colset Fts5Colset;
214972
214973/* If a NEAR() clump or phrase may only match a specific set of columns,
214974** then an object of the following type is used to record the set of columns.
214975** Each entry in the aiCol[] array is a column that may be matched.
214976**
214977** This object is used by fts5_expr.c and fts5_index.c.
214978*/
214979struct Fts5Colset {
214980 int nCol;
214981 int aiCol[1];
214982};
214983
214984
214985
214986/**************************************************************************
214987** Interface to code in fts5_config.c. fts5_config.c contains contains code
214988** to parse the arguments passed to the CREATE VIRTUAL TABLE statement.
214989*/
214990
214991typedef struct Fts5Config Fts5Config;
214992
214993/*
214994** An instance of the following structure encodes all information that can
214995** be gleaned from the CREATE VIRTUAL TABLE statement.
214996**
214997** And all information loaded from the %_config table.
214998**
214999** nAutomerge:
215000** The minimum number of segments that an auto-merge operation should
215001** attempt to merge together. A value of 1 sets the object to use the
215002** compile time default. Zero disables auto-merge altogether.
215003**
215004** zContent:
215005**
215006** zContentRowid:
215007** The value of the content_rowid= option, if one was specified. Or
215008** the string "rowid" otherwise. This text is not quoted - if it is
215009** used as part of an SQL statement it needs to be quoted appropriately.
215010**
215011** zContentExprlist:
215012**
215013** pzErrmsg:
215014** This exists in order to allow the fts5_index.c module to return a
215015** decent error message if it encounters a file-format version it does
215016** not understand.
215017**
215018** bColumnsize:
215019** True if the %_docsize table is created.
215020**
215021** bPrefixIndex:
215022** This is only used for debugging. If set to false, any prefix indexes
215023** are ignored. This value is configured using:
215024**
215025** INSERT INTO tbl(tbl, rank) VALUES('prefix-index', $bPrefixIndex);
215026**
215027*/
215028struct Fts5Config {
215029 sqlite3 *db; /* Database handle */
215030 char *zDb; /* Database holding FTS index (e.g. "main") */
215031 char *zName; /* Name of FTS index */
215032 int nCol; /* Number of columns */
215033 char **azCol; /* Column names */
215034 u8 *abUnindexed; /* True for unindexed columns */
215035 int nPrefix; /* Number of prefix indexes */
215036 int *aPrefix; /* Sizes in bytes of nPrefix prefix indexes */
215037 int eContent; /* An FTS5_CONTENT value */
215038 char *zContent; /* content table */
215039 char *zContentRowid; /* "content_rowid=" option value */
215040 int bColumnsize; /* "columnsize=" option value (dflt==1) */
215041 int eDetail; /* FTS5_DETAIL_XXX value */
215042 char *zContentExprlist;
215043 Fts5Tokenizer *pTok;
215044 fts5_tokenizer *pTokApi;
215045 int bLock; /* True when table is preparing statement */
215046 int ePattern; /* FTS_PATTERN_XXX constant */
215047
215048 /* Values loaded from the %_config table */
215049 int iCookie; /* Incremented when %_config is modified */
215050 int pgsz; /* Approximate page size used in %_data */
215051 int nAutomerge; /* 'automerge' setting */
215052 int nCrisisMerge; /* Maximum allowed segments per level */
215053 int nUsermerge; /* 'usermerge' setting */
215054 int nHashSize; /* Bytes of memory for in-memory hash */
215055 char *zRank; /* Name of rank function */
215056 char *zRankArgs; /* Arguments to rank function */
215057
215058 /* If non-NULL, points to sqlite3_vtab.base.zErrmsg. Often NULL. */
215059 char **pzErrmsg;
215060
215061#ifdef SQLITE_DEBUG
215062 int bPrefixIndex; /* True to use prefix-indexes */
215063#endif
215064};
215065
215066/* Current expected value of %_config table 'version' field */
215067#define FTS5_CURRENT_VERSION 4
215068
215069#define FTS5_CONTENT_NORMAL 0
215070#define FTS5_CONTENT_NONE 1
215071#define FTS5_CONTENT_EXTERNAL 2
215072
215073#define FTS5_DETAIL_FULL 0
215074#define FTS5_DETAIL_NONE 1
215075#define FTS5_DETAIL_COLUMNS 2
215076
215077#define FTS5_PATTERN_NONE 0
215078#define FTS5_PATTERN_LIKE 65 /* matches SQLITE_INDEX_CONSTRAINT_LIKE */
215079#define FTS5_PATTERN_GLOB 66 /* matches SQLITE_INDEX_CONSTRAINT_GLOB */
215080
215081static int sqlite3Fts5ConfigParse(
215082 Fts5Global*, sqlite3*, int, const char **, Fts5Config**, char**
215083);
215084static void sqlite3Fts5ConfigFree(Fts5Config*);
215085
215086static int sqlite3Fts5ConfigDeclareVtab(Fts5Config *pConfig);
215087
215088static int sqlite3Fts5Tokenize(
215089 Fts5Config *pConfig, /* FTS5 Configuration object */
215090 int flags, /* FTS5_TOKENIZE_* flags */
215091 const char *pText, int nText, /* Text to tokenize */
215092 void *pCtx, /* Context passed to xToken() */
215093 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
215094);
215095
215096static void sqlite3Fts5Dequote(char *z);
215097
215098/* Load the contents of the %_config table */
215099static int sqlite3Fts5ConfigLoad(Fts5Config*, int);
215100
215101/* Set the value of a single config attribute */
215102static int sqlite3Fts5ConfigSetValue(Fts5Config*, const char*, sqlite3_value*, int*);
215103
215104static int sqlite3Fts5ConfigParseRank(const char*, char**, char**);
215105
215106/*
215107** End of interface to code in fts5_config.c.
215108**************************************************************************/
215109
215110/**************************************************************************
215111** Interface to code in fts5_buffer.c.
215112*/
215113
215114/*
215115** Buffer object for the incremental building of string data.
215116*/
215117typedef struct Fts5Buffer Fts5Buffer;
215118struct Fts5Buffer {
215119 u8 *p;
215120 int n;
215121 int nSpace;
215122};
215123
215124static int sqlite3Fts5BufferSize(int*, Fts5Buffer*, u32);
215125static void sqlite3Fts5BufferAppendVarint(int*, Fts5Buffer*, i64);
215126static void sqlite3Fts5BufferAppendBlob(int*, Fts5Buffer*, u32, const u8*);
215127static void sqlite3Fts5BufferAppendString(int *, Fts5Buffer*, const char*);
215128static void sqlite3Fts5BufferFree(Fts5Buffer*);
215129static void sqlite3Fts5BufferZero(Fts5Buffer*);
215130static void sqlite3Fts5BufferSet(int*, Fts5Buffer*, int, const u8*);
215131static void sqlite3Fts5BufferAppendPrintf(int *, Fts5Buffer*, char *zFmt, ...);
215132
215133static char *sqlite3Fts5Mprintf(int *pRc, const char *zFmt, ...);
215134
215135#define fts5BufferZero(x) sqlite3Fts5BufferZero(x)
215136#define fts5BufferAppendVarint(a,b,c) sqlite3Fts5BufferAppendVarint(a,b,c)
215137#define fts5BufferFree(a) sqlite3Fts5BufferFree(a)
215138#define fts5BufferAppendBlob(a,b,c,d) sqlite3Fts5BufferAppendBlob(a,b,c,d)
215139#define fts5BufferSet(a,b,c,d) sqlite3Fts5BufferSet(a,b,c,d)
215140
215141#define fts5BufferGrow(pRc,pBuf,nn) ( \
215142 (u32)((pBuf)->n) + (u32)(nn) <= (u32)((pBuf)->nSpace) ? 0 : \
215143 sqlite3Fts5BufferSize((pRc),(pBuf),(nn)+(pBuf)->n) \
215144)
215145
215146/* Write and decode big-endian 32-bit integer values */
215147static void sqlite3Fts5Put32(u8*, int);
215148static int sqlite3Fts5Get32(const u8*);
215149
215150#define FTS5_POS2COLUMN(iPos) (int)(iPos >> 32)
215151#define FTS5_POS2OFFSET(iPos) (int)(iPos & 0x7FFFFFFF)
215152
215153typedef struct Fts5PoslistReader Fts5PoslistReader;
215154struct Fts5PoslistReader {
215155 /* Variables used only by sqlite3Fts5PoslistIterXXX() functions. */
215156 const u8 *a; /* Position list to iterate through */
215157 int n; /* Size of buffer at a[] in bytes */
215158 int i; /* Current offset in a[] */
215159
215160 u8 bFlag; /* For client use (any custom purpose) */
215161
215162 /* Output variables */
215163 u8 bEof; /* Set to true at EOF */
215164 i64 iPos; /* (iCol<<32) + iPos */
215165};
215166static int sqlite3Fts5PoslistReaderInit(
215167 const u8 *a, int n, /* Poslist buffer to iterate through */
215168 Fts5PoslistReader *pIter /* Iterator object to initialize */
215169);
215170static int sqlite3Fts5PoslistReaderNext(Fts5PoslistReader*);
215171
215172typedef struct Fts5PoslistWriter Fts5PoslistWriter;
215173struct Fts5PoslistWriter {
215174 i64 iPrev;
215175};
215176static int sqlite3Fts5PoslistWriterAppend(Fts5Buffer*, Fts5PoslistWriter*, i64);
215177static void sqlite3Fts5PoslistSafeAppend(Fts5Buffer*, i64*, i64);
215178
215179static int sqlite3Fts5PoslistNext64(
215180 const u8 *a, int n, /* Buffer containing poslist */
215181 int *pi, /* IN/OUT: Offset within a[] */
215182 i64 *piOff /* IN/OUT: Current offset */
215183);
215184
215185/* Malloc utility */
215186static void *sqlite3Fts5MallocZero(int *pRc, sqlite3_int64 nByte);
215187static char *sqlite3Fts5Strndup(int *pRc, const char *pIn, int nIn);
215188
215189/* Character set tests (like isspace(), isalpha() etc.) */
215190static int sqlite3Fts5IsBareword(char t);
215191
215192
215193/* Bucket of terms object used by the integrity-check in offsets=0 mode. */
215194typedef struct Fts5Termset Fts5Termset;
215195static int sqlite3Fts5TermsetNew(Fts5Termset**);
215196static int sqlite3Fts5TermsetAdd(Fts5Termset*, int, const char*, int, int *pbPresent);
215197static void sqlite3Fts5TermsetFree(Fts5Termset*);
215198
215199/*
215200** End of interface to code in fts5_buffer.c.
215201**************************************************************************/
215202
215203/**************************************************************************
215204** Interface to code in fts5_index.c. fts5_index.c contains contains code
215205** to access the data stored in the %_data table.
215206*/
215207
215208typedef struct Fts5Index Fts5Index;
215209typedef struct Fts5IndexIter Fts5IndexIter;
215210
215211struct Fts5IndexIter {
215212 i64 iRowid;
215213 const u8 *pData;
215214 int nData;
215215 u8 bEof;
215216};
215217
215218#define sqlite3Fts5IterEof(x) ((x)->bEof)
215219
215220/*
215221** Values used as part of the flags argument passed to IndexQuery().
215222*/
215223#define FTS5INDEX_QUERY_PREFIX 0x0001 /* Prefix query */
215224#define FTS5INDEX_QUERY_DESC 0x0002 /* Docs in descending rowid order */
215225#define FTS5INDEX_QUERY_TEST_NOIDX 0x0004 /* Do not use prefix index */
215226#define FTS5INDEX_QUERY_SCAN 0x0008 /* Scan query (fts5vocab) */
215227
215228/* The following are used internally by the fts5_index.c module. They are
215229** defined here only to make it easier to avoid clashes with the flags
215230** above. */
215231#define FTS5INDEX_QUERY_SKIPEMPTY 0x0010
215232#define FTS5INDEX_QUERY_NOOUTPUT 0x0020
215233
215234/*
215235** Create/destroy an Fts5Index object.
215236*/
215237static int sqlite3Fts5IndexOpen(Fts5Config *pConfig, int bCreate, Fts5Index**, char**);
215238static int sqlite3Fts5IndexClose(Fts5Index *p);
215239
215240/*
215241** Return a simple checksum value based on the arguments.
215242*/
215243static u64 sqlite3Fts5IndexEntryCksum(
215244 i64 iRowid,
215245 int iCol,
215246 int iPos,
215247 int iIdx,
215248 const char *pTerm,
215249 int nTerm
215250);
215251
215252/*
215253** Argument p points to a buffer containing utf-8 text that is n bytes in
215254** size. Return the number of bytes in the nChar character prefix of the
215255** buffer, or 0 if there are less than nChar characters in total.
215256*/
215257static int sqlite3Fts5IndexCharlenToBytelen(
215258 const char *p,
215259 int nByte,
215260 int nChar
215261);
215262
215263/*
215264** Open a new iterator to iterate though all rowids that match the
215265** specified token or token prefix.
215266*/
215267static int sqlite3Fts5IndexQuery(
215268 Fts5Index *p, /* FTS index to query */
215269 const char *pToken, int nToken, /* Token (or prefix) to query for */
215270 int flags, /* Mask of FTS5INDEX_QUERY_X flags */
215271 Fts5Colset *pColset, /* Match these columns only */
215272 Fts5IndexIter **ppIter /* OUT: New iterator object */
215273);
215274
215275/*
215276** The various operations on open token or token prefix iterators opened
215277** using sqlite3Fts5IndexQuery().
215278*/
215279static int sqlite3Fts5IterNext(Fts5IndexIter*);
215280static int sqlite3Fts5IterNextFrom(Fts5IndexIter*, i64 iMatch);
215281
215282/*
215283** Close an iterator opened by sqlite3Fts5IndexQuery().
215284*/
215285static void sqlite3Fts5IterClose(Fts5IndexIter*);
215286
215287/*
215288** Close the reader blob handle, if it is open.
215289*/
215290static void sqlite3Fts5IndexCloseReader(Fts5Index*);
215291
215292/*
215293** This interface is used by the fts5vocab module.
215294*/
215295static const char *sqlite3Fts5IterTerm(Fts5IndexIter*, int*);
215296static int sqlite3Fts5IterNextScan(Fts5IndexIter*);
215297static void *sqlite3Fts5StructureRef(Fts5Index*);
215298static void sqlite3Fts5StructureRelease(void*);
215299static int sqlite3Fts5StructureTest(Fts5Index*, void*);
215300
215301
215302/*
215303** Insert or remove data to or from the index. Each time a document is
215304** added to or removed from the index, this function is called one or more
215305** times.
215306**
215307** For an insert, it must be called once for each token in the new document.
215308** If the operation is a delete, it must be called (at least) once for each
215309** unique token in the document with an iCol value less than zero. The iPos
215310** argument is ignored for a delete.
215311*/
215312static int sqlite3Fts5IndexWrite(
215313 Fts5Index *p, /* Index to write to */
215314 int iCol, /* Column token appears in (-ve -> delete) */
215315 int iPos, /* Position of token within column */
215316 const char *pToken, int nToken /* Token to add or remove to or from index */
215317);
215318
215319/*
215320** Indicate that subsequent calls to sqlite3Fts5IndexWrite() pertain to
215321** document iDocid.
215322*/
215323static int sqlite3Fts5IndexBeginWrite(
215324 Fts5Index *p, /* Index to write to */
215325 int bDelete, /* True if current operation is a delete */
215326 i64 iDocid /* Docid to add or remove data from */
215327);
215328
215329/*
215330** Flush any data stored in the in-memory hash tables to the database.
215331** Also close any open blob handles.
215332*/
215333static int sqlite3Fts5IndexSync(Fts5Index *p);
215334
215335/*
215336** Discard any data stored in the in-memory hash tables. Do not write it
215337** to the database. Additionally, assume that the contents of the %_data
215338** table may have changed on disk. So any in-memory caches of %_data
215339** records must be invalidated.
215340*/
215341static int sqlite3Fts5IndexRollback(Fts5Index *p);
215342
215343/*
215344** Get or set the "averages" values.
215345*/
215346static int sqlite3Fts5IndexGetAverages(Fts5Index *p, i64 *pnRow, i64 *anSize);
215347static int sqlite3Fts5IndexSetAverages(Fts5Index *p, const u8*, int);
215348
215349/*
215350** Functions called by the storage module as part of integrity-check.
215351*/
215352static int sqlite3Fts5IndexIntegrityCheck(Fts5Index*, u64 cksum, int bUseCksum);
215353
215354/*
215355** Called during virtual module initialization to register UDF
215356** fts5_decode() with SQLite
215357*/
215358static int sqlite3Fts5IndexInit(sqlite3*);
215359
215360static int sqlite3Fts5IndexSetCookie(Fts5Index*, int);
215361
215362/*
215363** Return the total number of entries read from the %_data table by
215364** this connection since it was created.
215365*/
215366static int sqlite3Fts5IndexReads(Fts5Index *p);
215367
215368static int sqlite3Fts5IndexReinit(Fts5Index *p);
215369static int sqlite3Fts5IndexOptimize(Fts5Index *p);
215370static int sqlite3Fts5IndexMerge(Fts5Index *p, int nMerge);
215371static int sqlite3Fts5IndexReset(Fts5Index *p);
215372
215373static int sqlite3Fts5IndexLoadConfig(Fts5Index *p);
215374
215375/*
215376** End of interface to code in fts5_index.c.
215377**************************************************************************/
215378
215379/**************************************************************************
215380** Interface to code in fts5_varint.c.
215381*/
215382static int sqlite3Fts5GetVarint32(const unsigned char *p, u32 *v);
215383static int sqlite3Fts5GetVarintLen(u32 iVal);
215384static u8 sqlite3Fts5GetVarint(const unsigned char*, u64*);
215385static int sqlite3Fts5PutVarint(unsigned char *p, u64 v);
215386
215387#define fts5GetVarint32(a,b) sqlite3Fts5GetVarint32(a,(u32*)&b)
215388#define fts5GetVarint sqlite3Fts5GetVarint
215389
215390#define fts5FastGetVarint32(a, iOff, nVal) { \
215391 nVal = (a)[iOff++]; \
215392 if( nVal & 0x80 ){ \
215393 iOff--; \
215394 iOff += fts5GetVarint32(&(a)[iOff], nVal); \
215395 } \
215396}
215397
215398
215399/*
215400** End of interface to code in fts5_varint.c.
215401**************************************************************************/
215402
215403
215404/**************************************************************************
215405** Interface to code in fts5_main.c.
215406*/
215407
215408/*
215409** Virtual-table object.
215410*/
215411typedef struct Fts5Table Fts5Table;
215412struct Fts5Table {
215413 sqlite3_vtab base; /* Base class used by SQLite core */
215414 Fts5Config *pConfig; /* Virtual table configuration */
215415 Fts5Index *pIndex; /* Full-text index */
215416};
215417
215418static int sqlite3Fts5GetTokenizer(
215419 Fts5Global*,
215420 const char **azArg,
215421 int nArg,
215422 Fts5Config*,
215423 char **pzErr
215424);
215425
215426static Fts5Table *sqlite3Fts5TableFromCsrid(Fts5Global*, i64);
215427
215428static int sqlite3Fts5FlushToDisk(Fts5Table*);
215429
215430/*
215431** End of interface to code in fts5.c.
215432**************************************************************************/
215433
215434/**************************************************************************
215435** Interface to code in fts5_hash.c.
215436*/
215437typedef struct Fts5Hash Fts5Hash;
215438
215439/*
215440** Create a hash table, free a hash table.
215441*/
215442static int sqlite3Fts5HashNew(Fts5Config*, Fts5Hash**, int *pnSize);
215443static void sqlite3Fts5HashFree(Fts5Hash*);
215444
215445static int sqlite3Fts5HashWrite(
215446 Fts5Hash*,
215447 i64 iRowid, /* Rowid for this entry */
215448 int iCol, /* Column token appears in (-ve -> delete) */
215449 int iPos, /* Position of token within column */
215450 char bByte,
215451 const char *pToken, int nToken /* Token to add or remove to or from index */
215452);
215453
215454/*
215455** Empty (but do not delete) a hash table.
215456*/
215457static void sqlite3Fts5HashClear(Fts5Hash*);
215458
215459static int sqlite3Fts5HashQuery(
215460 Fts5Hash*, /* Hash table to query */
215461 int nPre,
215462 const char *pTerm, int nTerm, /* Query term */
215463 void **ppObj, /* OUT: Pointer to doclist for pTerm */
215464 int *pnDoclist /* OUT: Size of doclist in bytes */
215465);
215466
215467static int sqlite3Fts5HashScanInit(
215468 Fts5Hash*, /* Hash table to query */
215469 const char *pTerm, int nTerm /* Query prefix */
215470);
215471static void sqlite3Fts5HashScanNext(Fts5Hash*);
215472static int sqlite3Fts5HashScanEof(Fts5Hash*);
215473static void sqlite3Fts5HashScanEntry(Fts5Hash *,
215474 const char **pzTerm, /* OUT: term (nul-terminated) */
215475 const u8 **ppDoclist, /* OUT: pointer to doclist */
215476 int *pnDoclist /* OUT: size of doclist in bytes */
215477);
215478
215479
215480/*
215481** End of interface to code in fts5_hash.c.
215482**************************************************************************/
215483
215484/**************************************************************************
215485** Interface to code in fts5_storage.c. fts5_storage.c contains contains
215486** code to access the data stored in the %_content and %_docsize tables.
215487*/
215488
215489#define FTS5_STMT_SCAN_ASC 0 /* SELECT rowid, * FROM ... ORDER BY 1 ASC */
215490#define FTS5_STMT_SCAN_DESC 1 /* SELECT rowid, * FROM ... ORDER BY 1 DESC */
215491#define FTS5_STMT_LOOKUP 2 /* SELECT rowid, * FROM ... WHERE rowid=? */
215492
215493typedef struct Fts5Storage Fts5Storage;
215494
215495static int sqlite3Fts5StorageOpen(Fts5Config*, Fts5Index*, int, Fts5Storage**, char**);
215496static int sqlite3Fts5StorageClose(Fts5Storage *p);
215497static int sqlite3Fts5StorageRename(Fts5Storage*, const char *zName);
215498
215499static int sqlite3Fts5DropAll(Fts5Config*);
215500static int sqlite3Fts5CreateTable(Fts5Config*, const char*, const char*, int, char **);
215501
215502static int sqlite3Fts5StorageDelete(Fts5Storage *p, i64, sqlite3_value**);
215503static int sqlite3Fts5StorageContentInsert(Fts5Storage *p, sqlite3_value**, i64*);
215504static int sqlite3Fts5StorageIndexInsert(Fts5Storage *p, sqlite3_value**, i64);
215505
215506static int sqlite3Fts5StorageIntegrity(Fts5Storage *p, int iArg);
215507
215508static int sqlite3Fts5StorageStmt(Fts5Storage *p, int eStmt, sqlite3_stmt**, char**);
215509static void sqlite3Fts5StorageStmtRelease(Fts5Storage *p, int eStmt, sqlite3_stmt*);
215510
215511static int sqlite3Fts5StorageDocsize(Fts5Storage *p, i64 iRowid, int *aCol);
215512static int sqlite3Fts5StorageSize(Fts5Storage *p, int iCol, i64 *pnAvg);
215513static int sqlite3Fts5StorageRowCount(Fts5Storage *p, i64 *pnRow);
215514
215515static int sqlite3Fts5StorageSync(Fts5Storage *p);
215516static int sqlite3Fts5StorageRollback(Fts5Storage *p);
215517
215518static int sqlite3Fts5StorageConfigValue(
215519 Fts5Storage *p, const char*, sqlite3_value*, int
215520);
215521
215522static int sqlite3Fts5StorageDeleteAll(Fts5Storage *p);
215523static int sqlite3Fts5StorageRebuild(Fts5Storage *p);
215524static int sqlite3Fts5StorageOptimize(Fts5Storage *p);
215525static int sqlite3Fts5StorageMerge(Fts5Storage *p, int nMerge);
215526static int sqlite3Fts5StorageReset(Fts5Storage *p);
215527
215528/*
215529** End of interface to code in fts5_storage.c.
215530**************************************************************************/
215531
215532
215533/**************************************************************************
215534** Interface to code in fts5_expr.c.
215535*/
215536typedef struct Fts5Expr Fts5Expr;
215537typedef struct Fts5ExprNode Fts5ExprNode;
215538typedef struct Fts5Parse Fts5Parse;
215539typedef struct Fts5Token Fts5Token;
215540typedef struct Fts5ExprPhrase Fts5ExprPhrase;
215541typedef struct Fts5ExprNearset Fts5ExprNearset;
215542
215543struct Fts5Token {
215544 const char *p; /* Token text (not NULL terminated) */
215545 int n; /* Size of buffer p in bytes */
215546};
215547
215548/* Parse a MATCH expression. */
215549static int sqlite3Fts5ExprNew(
215550 Fts5Config *pConfig,
215551 int bPhraseToAnd,
215552 int iCol, /* Column on LHS of MATCH operator */
215553 const char *zExpr,
215554 Fts5Expr **ppNew,
215555 char **pzErr
215556);
215557static int sqlite3Fts5ExprPattern(
215558 Fts5Config *pConfig,
215559 int bGlob,
215560 int iCol,
215561 const char *zText,
215562 Fts5Expr **pp
215563);
215564
215565/*
215566** for(rc = sqlite3Fts5ExprFirst(pExpr, pIdx, bDesc);
215567** rc==SQLITE_OK && 0==sqlite3Fts5ExprEof(pExpr);
215568** rc = sqlite3Fts5ExprNext(pExpr)
215569** ){
215570** // The document with rowid iRowid matches the expression!
215571** i64 iRowid = sqlite3Fts5ExprRowid(pExpr);
215572** }
215573*/
215574static int sqlite3Fts5ExprFirst(Fts5Expr*, Fts5Index *pIdx, i64 iMin, int bDesc);
215575static int sqlite3Fts5ExprNext(Fts5Expr*, i64 iMax);
215576static int sqlite3Fts5ExprEof(Fts5Expr*);
215577static i64 sqlite3Fts5ExprRowid(Fts5Expr*);
215578
215579static void sqlite3Fts5ExprFree(Fts5Expr*);
215580static int sqlite3Fts5ExprAnd(Fts5Expr **pp1, Fts5Expr *p2);
215581
215582/* Called during startup to register a UDF with SQLite */
215583static int sqlite3Fts5ExprInit(Fts5Global*, sqlite3*);
215584
215585static int sqlite3Fts5ExprPhraseCount(Fts5Expr*);
215586static int sqlite3Fts5ExprPhraseSize(Fts5Expr*, int iPhrase);
215587static int sqlite3Fts5ExprPoslist(Fts5Expr*, int, const u8 **);
215588
215589typedef struct Fts5PoslistPopulator Fts5PoslistPopulator;
215590static Fts5PoslistPopulator *sqlite3Fts5ExprClearPoslists(Fts5Expr*, int);
215591static int sqlite3Fts5ExprPopulatePoslists(
215592 Fts5Config*, Fts5Expr*, Fts5PoslistPopulator*, int, const char*, int
215593);
215594static void sqlite3Fts5ExprCheckPoslists(Fts5Expr*, i64);
215595
215596static int sqlite3Fts5ExprClonePhrase(Fts5Expr*, int, Fts5Expr**);
215597
215598static int sqlite3Fts5ExprPhraseCollist(Fts5Expr *, int, const u8 **, int *);
215599
215600/*******************************************
215601** The fts5_expr.c API above this point is used by the other hand-written
215602** C code in this module. The interfaces below this point are called by
215603** the parser code in fts5parse.y. */
215604
215605static void sqlite3Fts5ParseError(Fts5Parse *pParse, const char *zFmt, ...);
215606
215607static Fts5ExprNode *sqlite3Fts5ParseNode(
215608 Fts5Parse *pParse,
215609 int eType,
215610 Fts5ExprNode *pLeft,
215611 Fts5ExprNode *pRight,
215612 Fts5ExprNearset *pNear
215613);
215614
215615static Fts5ExprNode *sqlite3Fts5ParseImplicitAnd(
215616 Fts5Parse *pParse,
215617 Fts5ExprNode *pLeft,
215618 Fts5ExprNode *pRight
215619);
215620
215621static Fts5ExprPhrase *sqlite3Fts5ParseTerm(
215622 Fts5Parse *pParse,
215623 Fts5ExprPhrase *pPhrase,
215624 Fts5Token *pToken,
215625 int bPrefix
215626);
215627
215628static void sqlite3Fts5ParseSetCaret(Fts5ExprPhrase*);
215629
215630static Fts5ExprNearset *sqlite3Fts5ParseNearset(
215631 Fts5Parse*,
215632 Fts5ExprNearset*,
215633 Fts5ExprPhrase*
215634);
215635
215636static Fts5Colset *sqlite3Fts5ParseColset(
215637 Fts5Parse*,
215638 Fts5Colset*,
215639 Fts5Token *
215640);
215641
215642static void sqlite3Fts5ParsePhraseFree(Fts5ExprPhrase*);
215643static void sqlite3Fts5ParseNearsetFree(Fts5ExprNearset*);
215644static void sqlite3Fts5ParseNodeFree(Fts5ExprNode*);
215645
215646static void sqlite3Fts5ParseSetDistance(Fts5Parse*, Fts5ExprNearset*, Fts5Token*);
215647static void sqlite3Fts5ParseSetColset(Fts5Parse*, Fts5ExprNode*, Fts5Colset*);
215648static Fts5Colset *sqlite3Fts5ParseColsetInvert(Fts5Parse*, Fts5Colset*);
215649static void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p);
215650static void sqlite3Fts5ParseNear(Fts5Parse *pParse, Fts5Token*);
215651
215652/*
215653** End of interface to code in fts5_expr.c.
215654**************************************************************************/
215655
215656
215657
215658/**************************************************************************
215659** Interface to code in fts5_aux.c.
215660*/
215661
215662static int sqlite3Fts5AuxInit(fts5_api*);
215663/*
215664** End of interface to code in fts5_aux.c.
215665**************************************************************************/
215666
215667/**************************************************************************
215668** Interface to code in fts5_tokenizer.c.
215669*/
215670
215671static int sqlite3Fts5TokenizerInit(fts5_api*);
215672static int sqlite3Fts5TokenizerPattern(
215673 int (*xCreate)(void*, const char**, int, Fts5Tokenizer**),
215674 Fts5Tokenizer *pTok
215675);
215676/*
215677** End of interface to code in fts5_tokenizer.c.
215678**************************************************************************/
215679
215680/**************************************************************************
215681** Interface to code in fts5_vocab.c.
215682*/
215683
215684static int sqlite3Fts5VocabInit(Fts5Global*, sqlite3*);
215685
215686/*
215687** End of interface to code in fts5_vocab.c.
215688**************************************************************************/
215689
215690
215691/**************************************************************************
215692** Interface to automatically generated code in fts5_unicode2.c.
215693*/
215694static int sqlite3Fts5UnicodeIsdiacritic(int c);
215695static int sqlite3Fts5UnicodeFold(int c, int bRemoveDiacritic);
215696
215697static int sqlite3Fts5UnicodeCatParse(const char*, u8*);
215698static int sqlite3Fts5UnicodeCategory(u32 iCode);
215699static void sqlite3Fts5UnicodeAscii(u8*, u8*);
215700/*
215701** End of interface to code in fts5_unicode2.c.
215702**************************************************************************/
215703
215704#endif
215705
215706#define FTS5_OR 1
215707#define FTS5_AND 2
215708#define FTS5_NOT 3
215709#define FTS5_TERM 4
215710#define FTS5_COLON 5
215711#define FTS5_MINUS 6
215712#define FTS5_LCP 7
215713#define FTS5_RCP 8
215714#define FTS5_STRING 9
215715#define FTS5_LP 10
215716#define FTS5_RP 11
215717#define FTS5_CARET 12
215718#define FTS5_COMMA 13
215719#define FTS5_PLUS 14
215720#define FTS5_STAR 15
215721
215722/* This file is automatically generated by Lemon from input grammar
215723** source file "fts5parse.y". */
215724/*
215725** 2000-05-29
215726**
215727** The author disclaims copyright to this source code. In place of
215728** a legal notice, here is a blessing:
215729**
215730** May you do good and not evil.
215731** May you find forgiveness for yourself and forgive others.
215732** May you share freely, never taking more than you give.
215733**
215734*************************************************************************
215735** Driver template for the LEMON parser generator.
215736**
215737** The "lemon" program processes an LALR(1) input grammar file, then uses
215738** this template to construct a parser. The "lemon" program inserts text
215739** at each "%%" line. Also, any "P-a-r-s-e" identifer prefix (without the
215740** interstitial "-" characters) contained in this template is changed into
215741** the value of the %name directive from the grammar. Otherwise, the content
215742** of this template is copied straight through into the generate parser
215743** source file.
215744**
215745** The following is the concatenation of all %include directives from the
215746** input grammar file:
215747*/
215748/************ Begin %include sections from the grammar ************************/
215749
215750/* #include "fts5Int.h" */
215751/* #include "fts5parse.h" */
215752
215753/*
215754** Disable all error recovery processing in the parser push-down
215755** automaton.
215756*/
215757#define fts5YYNOERRORRECOVERY 1
215758
215759/*
215760** Make fts5yytestcase() the same as testcase()
215761*/
215762#define fts5yytestcase(X) testcase(X)
215763
215764/*
215765** Indicate that sqlite3ParserFree() will never be called with a null
215766** pointer.
215767*/
215768#define fts5YYPARSEFREENOTNULL 1
215769
215770/*
215771** Alternative datatype for the argument to the malloc() routine passed
215772** into sqlite3ParserAlloc(). The default is size_t.
215773*/
215774#define fts5YYMALLOCARGTYPE u64
215775
215776/**************** End of %include directives **********************************/
215777/* These constants specify the various numeric values for terminal symbols.
215778***************** Begin token definitions *************************************/
215779#ifndef FTS5_OR
215780#define FTS5_OR 1
215781#define FTS5_AND 2
215782#define FTS5_NOT 3
215783#define FTS5_TERM 4
215784#define FTS5_COLON 5
215785#define FTS5_MINUS 6
215786#define FTS5_LCP 7
215787#define FTS5_RCP 8
215788#define FTS5_STRING 9
215789#define FTS5_LP 10
215790#define FTS5_RP 11
215791#define FTS5_CARET 12
215792#define FTS5_COMMA 13
215793#define FTS5_PLUS 14
215794#define FTS5_STAR 15
215795#endif
215796/**************** End token definitions ***************************************/
215797
215798/* The next sections is a series of control #defines.
215799** various aspects of the generated parser.
215800** fts5YYCODETYPE is the data type used to store the integer codes
215801** that represent terminal and non-terminal symbols.
215802** "unsigned char" is used if there are fewer than
215803** 256 symbols. Larger types otherwise.
215804** fts5YYNOCODE is a number of type fts5YYCODETYPE that is not used for
215805** any terminal or nonterminal symbol.
215806** fts5YYFALLBACK If defined, this indicates that one or more tokens
215807** (also known as: "terminal symbols") have fall-back
215808** values which should be used if the original symbol
215809** would not parse. This permits keywords to sometimes
215810** be used as identifiers, for example.
215811** fts5YYACTIONTYPE is the data type used for "action codes" - numbers
215812** that indicate what to do in response to the next
215813** token.
215814** sqlite3Fts5ParserFTS5TOKENTYPE is the data type used for minor type for terminal
215815** symbols. Background: A "minor type" is a semantic
215816** value associated with a terminal or non-terminal
215817** symbols. For example, for an "ID" terminal symbol,
215818** the minor type might be the name of the identifier.
215819** Each non-terminal can have a different minor type.
215820** Terminal symbols all have the same minor type, though.
215821** This macros defines the minor type for terminal
215822** symbols.
215823** fts5YYMINORTYPE is the data type used for all minor types.
215824** This is typically a union of many types, one of
215825** which is sqlite3Fts5ParserFTS5TOKENTYPE. The entry in the union
215826** for terminal symbols is called "fts5yy0".
215827** fts5YYSTACKDEPTH is the maximum depth of the parser's stack. If
215828** zero the stack is dynamically sized using realloc()
215829** sqlite3Fts5ParserARG_SDECL A static variable declaration for the %extra_argument
215830** sqlite3Fts5ParserARG_PDECL A parameter declaration for the %extra_argument
215831** sqlite3Fts5ParserARG_PARAM Code to pass %extra_argument as a subroutine parameter
215832** sqlite3Fts5ParserARG_STORE Code to store %extra_argument into fts5yypParser
215833** sqlite3Fts5ParserARG_FETCH Code to extract %extra_argument from fts5yypParser
215834** sqlite3Fts5ParserCTX_* As sqlite3Fts5ParserARG_ except for %extra_context
215835** fts5YYERRORSYMBOL is the code number of the error symbol. If not
215836** defined, then do no error processing.
215837** fts5YYNSTATE the combined number of states.
215838** fts5YYNRULE the number of rules in the grammar
215839** fts5YYNFTS5TOKEN Number of terminal symbols
215840** fts5YY_MAX_SHIFT Maximum value for shift actions
215841** fts5YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
215842** fts5YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
215843** fts5YY_ERROR_ACTION The fts5yy_action[] code for syntax error
215844** fts5YY_ACCEPT_ACTION The fts5yy_action[] code for accept
215845** fts5YY_NO_ACTION The fts5yy_action[] code for no-op
215846** fts5YY_MIN_REDUCE Minimum value for reduce actions
215847** fts5YY_MAX_REDUCE Maximum value for reduce actions
215848*/
215849#ifndef INTERFACE
215850# define INTERFACE 1
215851#endif
215852/************* Begin control #defines *****************************************/
215853#define fts5YYCODETYPE unsigned char
215854#define fts5YYNOCODE 27
215855#define fts5YYACTIONTYPE unsigned char
215856#define sqlite3Fts5ParserFTS5TOKENTYPE Fts5Token
215857typedef union {
215858 int fts5yyinit;
215859 sqlite3Fts5ParserFTS5TOKENTYPE fts5yy0;
215860 int fts5yy4;
215861 Fts5Colset* fts5yy11;
215862 Fts5ExprNode* fts5yy24;
215863 Fts5ExprNearset* fts5yy46;
215864 Fts5ExprPhrase* fts5yy53;
215865} fts5YYMINORTYPE;
215866#ifndef fts5YYSTACKDEPTH
215867#define fts5YYSTACKDEPTH 100
215868#endif
215869#define sqlite3Fts5ParserARG_SDECL Fts5Parse *pParse;
215870#define sqlite3Fts5ParserARG_PDECL ,Fts5Parse *pParse
215871#define sqlite3Fts5ParserARG_PARAM ,pParse
215872#define sqlite3Fts5ParserARG_FETCH Fts5Parse *pParse=fts5yypParser->pParse;
215873#define sqlite3Fts5ParserARG_STORE fts5yypParser->pParse=pParse;
215874#define sqlite3Fts5ParserCTX_SDECL
215875#define sqlite3Fts5ParserCTX_PDECL
215876#define sqlite3Fts5ParserCTX_PARAM
215877#define sqlite3Fts5ParserCTX_FETCH
215878#define sqlite3Fts5ParserCTX_STORE
215879#define fts5YYNSTATE 35
215880#define fts5YYNRULE 28
215881#define fts5YYNRULE_WITH_ACTION 28
215882#define fts5YYNFTS5TOKEN 16
215883#define fts5YY_MAX_SHIFT 34
215884#define fts5YY_MIN_SHIFTREDUCE 52
215885#define fts5YY_MAX_SHIFTREDUCE 79
215886#define fts5YY_ERROR_ACTION 80
215887#define fts5YY_ACCEPT_ACTION 81
215888#define fts5YY_NO_ACTION 82
215889#define fts5YY_MIN_REDUCE 83
215890#define fts5YY_MAX_REDUCE 110
215891/************* End control #defines *******************************************/
215892#define fts5YY_NLOOKAHEAD ((int)(sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0])))
215893
215894/* Define the fts5yytestcase() macro to be a no-op if is not already defined
215895** otherwise.
215896**
215897** Applications can choose to define fts5yytestcase() in the %include section
215898** to a macro that can assist in verifying code coverage. For production
215899** code the fts5yytestcase() macro should be turned off. But it is useful
215900** for testing.
215901*/
215902#ifndef fts5yytestcase
215903# define fts5yytestcase(X)
215904#endif
215905
215906
215907/* Next are the tables used to determine what action to take based on the
215908** current state and lookahead token. These tables are used to implement
215909** functions that take a state number and lookahead value and return an
215910** action integer.
215911**
215912** Suppose the action integer is N. Then the action is determined as
215913** follows
215914**
215915** 0 <= N <= fts5YY_MAX_SHIFT Shift N. That is, push the lookahead
215916** token onto the stack and goto state N.
215917**
215918** N between fts5YY_MIN_SHIFTREDUCE Shift to an arbitrary state then
215919** and fts5YY_MAX_SHIFTREDUCE reduce by rule N-fts5YY_MIN_SHIFTREDUCE.
215920**
215921** N == fts5YY_ERROR_ACTION A syntax error has occurred.
215922**
215923** N == fts5YY_ACCEPT_ACTION The parser accepts its input.
215924**
215925** N == fts5YY_NO_ACTION No such action. Denotes unused
215926** slots in the fts5yy_action[] table.
215927**
215928** N between fts5YY_MIN_REDUCE Reduce by rule N-fts5YY_MIN_REDUCE
215929** and fts5YY_MAX_REDUCE
215930**
215931** The action table is constructed as a single large table named fts5yy_action[].
215932** Given state S and lookahead X, the action is computed as either:
215933**
215934** (A) N = fts5yy_action[ fts5yy_shift_ofst[S] + X ]
215935** (B) N = fts5yy_default[S]
215936**
215937** The (A) formula is preferred. The B formula is used instead if
215938** fts5yy_lookahead[fts5yy_shift_ofst[S]+X] is not equal to X.
215939**
215940** The formulas above are for computing the action when the lookahead is
215941** a terminal symbol. If the lookahead is a non-terminal (as occurs after
215942** a reduce action) then the fts5yy_reduce_ofst[] array is used in place of
215943** the fts5yy_shift_ofst[] array.
215944**
215945** The following are the tables generated in this section:
215946**
215947** fts5yy_action[] A single table containing all actions.
215948** fts5yy_lookahead[] A table containing the lookahead for each entry in
215949** fts5yy_action. Used to detect hash collisions.
215950** fts5yy_shift_ofst[] For each state, the offset into fts5yy_action for
215951** shifting terminals.
215952** fts5yy_reduce_ofst[] For each state, the offset into fts5yy_action for
215953** shifting non-terminals after a reduce.
215954** fts5yy_default[] Default action for each state.
215955**
215956*********** Begin parsing tables **********************************************/
215957#define fts5YY_ACTTAB_COUNT (105)
215958static const fts5YYACTIONTYPE fts5yy_action[] = {
215959 /* 0 */ 81, 20, 96, 6, 28, 99, 98, 26, 26, 18,
215960 /* 10 */ 96, 6, 28, 17, 98, 56, 26, 19, 96, 6,
215961 /* 20 */ 28, 14, 98, 14, 26, 31, 92, 96, 6, 28,
215962 /* 30 */ 108, 98, 25, 26, 21, 96, 6, 28, 78, 98,
215963 /* 40 */ 58, 26, 29, 96, 6, 28, 107, 98, 22, 26,
215964 /* 50 */ 24, 16, 12, 11, 1, 13, 13, 24, 16, 23,
215965 /* 60 */ 11, 33, 34, 13, 97, 8, 27, 32, 98, 7,
215966 /* 70 */ 26, 3, 4, 5, 3, 4, 5, 3, 83, 4,
215967 /* 80 */ 5, 3, 63, 5, 3, 62, 12, 2, 86, 13,
215968 /* 90 */ 9, 30, 10, 10, 54, 57, 75, 78, 78, 53,
215969 /* 100 */ 57, 15, 82, 82, 71,
215970};
215971static const fts5YYCODETYPE fts5yy_lookahead[] = {
215972 /* 0 */ 16, 17, 18, 19, 20, 22, 22, 24, 24, 17,
215973 /* 10 */ 18, 19, 20, 7, 22, 9, 24, 17, 18, 19,
215974 /* 20 */ 20, 9, 22, 9, 24, 13, 17, 18, 19, 20,
215975 /* 30 */ 26, 22, 24, 24, 17, 18, 19, 20, 15, 22,
215976 /* 40 */ 9, 24, 17, 18, 19, 20, 26, 22, 21, 24,
215977 /* 50 */ 6, 7, 9, 9, 10, 12, 12, 6, 7, 21,
215978 /* 60 */ 9, 24, 25, 12, 18, 5, 20, 14, 22, 5,
215979 /* 70 */ 24, 3, 1, 2, 3, 1, 2, 3, 0, 1,
215980 /* 80 */ 2, 3, 11, 2, 3, 11, 9, 10, 5, 12,
215981 /* 90 */ 23, 24, 10, 10, 8, 9, 9, 15, 15, 8,
215982 /* 100 */ 9, 9, 27, 27, 11, 27, 27, 27, 27, 27,
215983 /* 110 */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
215984 /* 120 */ 27,
215985};
215986#define fts5YY_SHIFT_COUNT (34)
215987#define fts5YY_SHIFT_MIN (0)
215988#define fts5YY_SHIFT_MAX (93)
215989static const unsigned char fts5yy_shift_ofst[] = {
215990 /* 0 */ 44, 44, 44, 44, 44, 44, 51, 77, 43, 12,
215991 /* 10 */ 14, 83, 82, 14, 23, 23, 31, 31, 71, 74,
215992 /* 20 */ 78, 81, 86, 91, 6, 53, 53, 60, 64, 68,
215993 /* 30 */ 53, 87, 92, 53, 93,
215994};
215995#define fts5YY_REDUCE_COUNT (17)
215996#define fts5YY_REDUCE_MIN (-17)
215997#define fts5YY_REDUCE_MAX (67)
215998static const signed char fts5yy_reduce_ofst[] = {
215999 /* 0 */ -16, -8, 0, 9, 17, 25, 46, -17, -17, 37,
216000 /* 10 */ 67, 4, 4, 8, 4, 20, 27, 38,
216001};
216002static const fts5YYACTIONTYPE fts5yy_default[] = {
216003 /* 0 */ 80, 80, 80, 80, 80, 80, 95, 80, 80, 105,
216004 /* 10 */ 80, 110, 110, 80, 110, 110, 80, 80, 80, 80,
216005 /* 20 */ 80, 91, 80, 80, 80, 101, 100, 80, 80, 90,
216006 /* 30 */ 103, 80, 80, 104, 80,
216007};
216008/********** End of lemon-generated parsing tables *****************************/
216009
216010/* The next table maps tokens (terminal symbols) into fallback tokens.
216011** If a construct like the following:
216012**
216013** %fallback ID X Y Z.
216014**
216015** appears in the grammar, then ID becomes a fallback token for X, Y,
216016** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
216017** but it does not parse, the type of the token is changed to ID and
216018** the parse is retried before an error is thrown.
216019**
216020** This feature can be used, for example, to cause some keywords in a language
216021** to revert to identifiers if they keyword does not apply in the context where
216022** it appears.
216023*/
216024#ifdef fts5YYFALLBACK
216025static const fts5YYCODETYPE fts5yyFallback[] = {
216026};
216027#endif /* fts5YYFALLBACK */
216028
216029/* The following structure represents a single element of the
216030** parser's stack. Information stored includes:
216031**
216032** + The state number for the parser at this level of the stack.
216033**
216034** + The value of the token stored at this level of the stack.
216035** (In other words, the "major" token.)
216036**
216037** + The semantic value stored at this level of the stack. This is
216038** the information used by the action routines in the grammar.
216039** It is sometimes called the "minor" token.
216040**
216041** After the "shift" half of a SHIFTREDUCE action, the stateno field
216042** actually contains the reduce action for the second half of the
216043** SHIFTREDUCE.
216044*/
216045struct fts5yyStackEntry {
216046 fts5YYACTIONTYPE stateno; /* The state-number, or reduce action in SHIFTREDUCE */
216047 fts5YYCODETYPE major; /* The major token value. This is the code
216048 ** number for the token at this stack level */
216049 fts5YYMINORTYPE minor; /* The user-supplied minor token value. This
216050 ** is the value of the token */
216051};
216052typedef struct fts5yyStackEntry fts5yyStackEntry;
216053
216054/* The state of the parser is completely contained in an instance of
216055** the following structure */
216056struct fts5yyParser {
216057 fts5yyStackEntry *fts5yytos; /* Pointer to top element of the stack */
216058#ifdef fts5YYTRACKMAXSTACKDEPTH
216059 int fts5yyhwm; /* High-water mark of the stack */
216060#endif
216061#ifndef fts5YYNOERRORRECOVERY
216062 int fts5yyerrcnt; /* Shifts left before out of the error */
216063#endif
216064 sqlite3Fts5ParserARG_SDECL /* A place to hold %extra_argument */
216065 sqlite3Fts5ParserCTX_SDECL /* A place to hold %extra_context */
216066#if fts5YYSTACKDEPTH<=0
216067 int fts5yystksz; /* Current side of the stack */
216068 fts5yyStackEntry *fts5yystack; /* The parser's stack */
216069 fts5yyStackEntry fts5yystk0; /* First stack entry */
216070#else
216071 fts5yyStackEntry fts5yystack[fts5YYSTACKDEPTH]; /* The parser's stack */
216072 fts5yyStackEntry *fts5yystackEnd; /* Last entry in the stack */
216073#endif
216074};
216075typedef struct fts5yyParser fts5yyParser;
216076
216077/* #include <assert.h> */
216078#ifndef NDEBUG
216079/* #include <stdio.h> */
216080static FILE *fts5yyTraceFILE = 0;
216081static char *fts5yyTracePrompt = 0;
216082#endif /* NDEBUG */
216083
216084#ifndef NDEBUG
216085/*
216086** Turn parser tracing on by giving a stream to which to write the trace
216087** and a prompt to preface each trace message. Tracing is turned off
216088** by making either argument NULL
216089**
216090** Inputs:
216091** <ul>
216092** <li> A FILE* to which trace output should be written.
216093** If NULL, then tracing is turned off.
216094** <li> A prefix string written at the beginning of every
216095** line of trace output. If NULL, then tracing is
216096** turned off.
216097** </ul>
216098**
216099** Outputs:
216100** None.
216101*/
216102static void sqlite3Fts5ParserTrace(FILE *TraceFILE, char *zTracePrompt){
216103 fts5yyTraceFILE = TraceFILE;
216104 fts5yyTracePrompt = zTracePrompt;
216105 if( fts5yyTraceFILE==0 ) fts5yyTracePrompt = 0;
216106 else if( fts5yyTracePrompt==0 ) fts5yyTraceFILE = 0;
216107}
216108#endif /* NDEBUG */
216109
216110#if defined(fts5YYCOVERAGE) || !defined(NDEBUG)
216111/* For tracing shifts, the names of all terminals and nonterminals
216112** are required. The following table supplies these names */
216113static const char *const fts5yyTokenName[] = {
216114 /* 0 */ "$",
216115 /* 1 */ "OR",
216116 /* 2 */ "AND",
216117 /* 3 */ "NOT",
216118 /* 4 */ "TERM",
216119 /* 5 */ "COLON",
216120 /* 6 */ "MINUS",
216121 /* 7 */ "LCP",
216122 /* 8 */ "RCP",
216123 /* 9 */ "STRING",
216124 /* 10 */ "LP",
216125 /* 11 */ "RP",
216126 /* 12 */ "CARET",
216127 /* 13 */ "COMMA",
216128 /* 14 */ "PLUS",
216129 /* 15 */ "STAR",
216130 /* 16 */ "input",
216131 /* 17 */ "expr",
216132 /* 18 */ "cnearset",
216133 /* 19 */ "exprlist",
216134 /* 20 */ "colset",
216135 /* 21 */ "colsetlist",
216136 /* 22 */ "nearset",
216137 /* 23 */ "nearphrases",
216138 /* 24 */ "phrase",
216139 /* 25 */ "neardist_opt",
216140 /* 26 */ "star_opt",
216141};
216142#endif /* defined(fts5YYCOVERAGE) || !defined(NDEBUG) */
216143
216144#ifndef NDEBUG
216145/* For tracing reduce actions, the names of all rules are required.
216146*/
216147static const char *const fts5yyRuleName[] = {
216148 /* 0 */ "input ::= expr",
216149 /* 1 */ "colset ::= MINUS LCP colsetlist RCP",
216150 /* 2 */ "colset ::= LCP colsetlist RCP",
216151 /* 3 */ "colset ::= STRING",
216152 /* 4 */ "colset ::= MINUS STRING",
216153 /* 5 */ "colsetlist ::= colsetlist STRING",
216154 /* 6 */ "colsetlist ::= STRING",
216155 /* 7 */ "expr ::= expr AND expr",
216156 /* 8 */ "expr ::= expr OR expr",
216157 /* 9 */ "expr ::= expr NOT expr",
216158 /* 10 */ "expr ::= colset COLON LP expr RP",
216159 /* 11 */ "expr ::= LP expr RP",
216160 /* 12 */ "expr ::= exprlist",
216161 /* 13 */ "exprlist ::= cnearset",
216162 /* 14 */ "exprlist ::= exprlist cnearset",
216163 /* 15 */ "cnearset ::= nearset",
216164 /* 16 */ "cnearset ::= colset COLON nearset",
216165 /* 17 */ "nearset ::= phrase",
216166 /* 18 */ "nearset ::= CARET phrase",
216167 /* 19 */ "nearset ::= STRING LP nearphrases neardist_opt RP",
216168 /* 20 */ "nearphrases ::= phrase",
216169 /* 21 */ "nearphrases ::= nearphrases phrase",
216170 /* 22 */ "neardist_opt ::=",
216171 /* 23 */ "neardist_opt ::= COMMA STRING",
216172 /* 24 */ "phrase ::= phrase PLUS STRING star_opt",
216173 /* 25 */ "phrase ::= STRING star_opt",
216174 /* 26 */ "star_opt ::= STAR",
216175 /* 27 */ "star_opt ::=",
216176};
216177#endif /* NDEBUG */
216178
216179
216180#if fts5YYSTACKDEPTH<=0
216181/*
216182** Try to increase the size of the parser stack. Return the number
216183** of errors. Return 0 on success.
216184*/
216185static int fts5yyGrowStack(fts5yyParser *p){
216186 int newSize;
216187 int idx;
216188 fts5yyStackEntry *pNew;
216189
216190 newSize = p->fts5yystksz*2 + 100;
216191 idx = p->fts5yytos ? (int)(p->fts5yytos - p->fts5yystack) : 0;
216192 if( p->fts5yystack==&p->fts5yystk0 ){
216193 pNew = malloc(newSize*sizeof(pNew[0]));
216194 if( pNew ) pNew[0] = p->fts5yystk0;
216195 }else{
216196 pNew = realloc(p->fts5yystack, newSize*sizeof(pNew[0]));
216197 }
216198 if( pNew ){
216199 p->fts5yystack = pNew;
216200 p->fts5yytos = &p->fts5yystack[idx];
216201#ifndef NDEBUG
216202 if( fts5yyTraceFILE ){
216203 fprintf(fts5yyTraceFILE,"%sStack grows from %d to %d entries.\n",
216204 fts5yyTracePrompt, p->fts5yystksz, newSize);
216205 }
216206#endif
216207 p->fts5yystksz = newSize;
216208 }
216209 return pNew==0;
216210}
216211#endif
216212
216213/* Datatype of the argument to the memory allocated passed as the
216214** second argument to sqlite3Fts5ParserAlloc() below. This can be changed by
216215** putting an appropriate #define in the %include section of the input
216216** grammar.
216217*/
216218#ifndef fts5YYMALLOCARGTYPE
216219# define fts5YYMALLOCARGTYPE size_t
216220#endif
216221
216222/* Initialize a new parser that has already been allocated.
216223*/
216224static void sqlite3Fts5ParserInit(void *fts5yypRawParser sqlite3Fts5ParserCTX_PDECL){
216225 fts5yyParser *fts5yypParser = (fts5yyParser*)fts5yypRawParser;
216226 sqlite3Fts5ParserCTX_STORE
216227#ifdef fts5YYTRACKMAXSTACKDEPTH
216228 fts5yypParser->fts5yyhwm = 0;
216229#endif
216230#if fts5YYSTACKDEPTH<=0
216231 fts5yypParser->fts5yytos = NULL;
216232 fts5yypParser->fts5yystack = NULL;
216233 fts5yypParser->fts5yystksz = 0;
216234 if( fts5yyGrowStack(fts5yypParser) ){
216235 fts5yypParser->fts5yystack = &fts5yypParser->fts5yystk0;
216236 fts5yypParser->fts5yystksz = 1;
216237 }
216238#endif
216239#ifndef fts5YYNOERRORRECOVERY
216240 fts5yypParser->fts5yyerrcnt = -1;
216241#endif
216242 fts5yypParser->fts5yytos = fts5yypParser->fts5yystack;
216243 fts5yypParser->fts5yystack[0].stateno = 0;
216244 fts5yypParser->fts5yystack[0].major = 0;
216245#if fts5YYSTACKDEPTH>0
216246 fts5yypParser->fts5yystackEnd = &fts5yypParser->fts5yystack[fts5YYSTACKDEPTH-1];
216247#endif
216248}
216249
216250#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
216251/*
216252** This function allocates a new parser.
216253** The only argument is a pointer to a function which works like
216254** malloc.
216255**
216256** Inputs:
216257** A pointer to the function used to allocate memory.
216258**
216259** Outputs:
216260** A pointer to a parser. This pointer is used in subsequent calls
216261** to sqlite3Fts5Parser and sqlite3Fts5ParserFree.
216262*/
216263static void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(fts5YYMALLOCARGTYPE) sqlite3Fts5ParserCTX_PDECL){
216264 fts5yyParser *fts5yypParser;
216265 fts5yypParser = (fts5yyParser*)(*mallocProc)( (fts5YYMALLOCARGTYPE)sizeof(fts5yyParser) );
216266 if( fts5yypParser ){
216267 sqlite3Fts5ParserCTX_STORE
216268 sqlite3Fts5ParserInit(fts5yypRawParser: fts5yypParser sqlite3Fts5ParserCTX_PARAM);
216269 }
216270 return (void*)fts5yypParser;
216271}
216272#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
216273
216274
216275/* The following function deletes the "minor type" or semantic value
216276** associated with a symbol. The symbol can be either a terminal
216277** or nonterminal. "fts5yymajor" is the symbol code, and "fts5yypminor" is
216278** a pointer to the value to be deleted. The code used to do the
216279** deletions is derived from the %destructor and/or %token_destructor
216280** directives of the input grammar.
216281*/
216282static void fts5yy_destructor(
216283 fts5yyParser *fts5yypParser, /* The parser */
216284 fts5YYCODETYPE fts5yymajor, /* Type code for object to destroy */
216285 fts5YYMINORTYPE *fts5yypminor /* The object to be destroyed */
216286){
216287 sqlite3Fts5ParserARG_FETCH
216288 sqlite3Fts5ParserCTX_FETCH
216289 switch( fts5yymajor ){
216290 /* Here is inserted the actions which take place when a
216291 ** terminal or non-terminal is destroyed. This can happen
216292 ** when the symbol is popped from the stack during a
216293 ** reduce or during error processing or when a parser is
216294 ** being destroyed before it is finished parsing.
216295 **
216296 ** Note: during a reduce, the only symbols destroyed are those
216297 ** which appear on the RHS of the rule, but which are *not* used
216298 ** inside the C code.
216299 */
216300/********* Begin destructor definitions ***************************************/
216301 case 16: /* input */
216302{
216303 (void)pParse;
216304}
216305 break;
216306 case 17: /* expr */
216307 case 18: /* cnearset */
216308 case 19: /* exprlist */
216309{
216310 sqlite3Fts5ParseNodeFree((fts5yypminor->fts5yy24));
216311}
216312 break;
216313 case 20: /* colset */
216314 case 21: /* colsetlist */
216315{
216316 sqlite3_free(p: (fts5yypminor->fts5yy11));
216317}
216318 break;
216319 case 22: /* nearset */
216320 case 23: /* nearphrases */
216321{
216322 sqlite3Fts5ParseNearsetFree((fts5yypminor->fts5yy46));
216323}
216324 break;
216325 case 24: /* phrase */
216326{
216327 sqlite3Fts5ParsePhraseFree((fts5yypminor->fts5yy53));
216328}
216329 break;
216330/********* End destructor definitions *****************************************/
216331 default: break; /* If no destructor action specified: do nothing */
216332 }
216333}
216334
216335/*
216336** Pop the parser's stack once.
216337**
216338** If there is a destructor routine associated with the token which
216339** is popped from the stack, then call it.
216340*/
216341static void fts5yy_pop_parser_stack(fts5yyParser *pParser){
216342 fts5yyStackEntry *fts5yytos;
216343 assert( pParser->fts5yytos!=0 );
216344 assert( pParser->fts5yytos > pParser->fts5yystack );
216345 fts5yytos = pParser->fts5yytos--;
216346#ifndef NDEBUG
216347 if( fts5yyTraceFILE ){
216348 fprintf(fts5yyTraceFILE,"%sPopping %s\n",
216349 fts5yyTracePrompt,
216350 fts5yyTokenName[fts5yytos->major]);
216351 }
216352#endif
216353 fts5yy_destructor(fts5yypParser: pParser, fts5yymajor: fts5yytos->major, fts5yypminor: &fts5yytos->minor);
216354}
216355
216356/*
216357** Clear all secondary memory allocations from the parser
216358*/
216359static void sqlite3Fts5ParserFinalize(void *p){
216360 fts5yyParser *pParser = (fts5yyParser*)p;
216361 while( pParser->fts5yytos>pParser->fts5yystack ) fts5yy_pop_parser_stack(pParser);
216362#if fts5YYSTACKDEPTH<=0
216363 if( pParser->fts5yystack!=&pParser->fts5yystk0 ) free(pParser->fts5yystack);
216364#endif
216365}
216366
216367#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
216368/*
216369** Deallocate and destroy a parser. Destructors are called for
216370** all stack elements before shutting the parser down.
216371**
216372** If the fts5YYPARSEFREENEVERNULL macro exists (for example because it
216373** is defined in a %include section of the input grammar) then it is
216374** assumed that the input pointer is never NULL.
216375*/
216376static void sqlite3Fts5ParserFree(
216377 void *p, /* The parser to be deleted */
216378 void (*freeProc)(void*) /* Function used to reclaim memory */
216379){
216380#ifndef fts5YYPARSEFREENEVERNULL
216381 if( p==0 ) return;
216382#endif
216383 sqlite3Fts5ParserFinalize(p);
216384 (*freeProc)(p);
216385}
216386#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
216387
216388/*
216389** Return the peak depth of the stack for a parser.
216390*/
216391#ifdef fts5YYTRACKMAXSTACKDEPTH
216392static int sqlite3Fts5ParserStackPeak(void *p){
216393 fts5yyParser *pParser = (fts5yyParser*)p;
216394 return pParser->fts5yyhwm;
216395}
216396#endif
216397
216398/* This array of booleans keeps track of the parser statement
216399** coverage. The element fts5yycoverage[X][Y] is set when the parser
216400** is in state X and has a lookahead token Y. In a well-tested
216401** systems, every element of this matrix should end up being set.
216402*/
216403#if defined(fts5YYCOVERAGE)
216404static unsigned char fts5yycoverage[fts5YYNSTATE][fts5YYNFTS5TOKEN];
216405#endif
216406
216407/*
216408** Write into out a description of every state/lookahead combination that
216409**
216410** (1) has not been used by the parser, and
216411** (2) is not a syntax error.
216412**
216413** Return the number of missed state/lookahead combinations.
216414*/
216415#if defined(fts5YYCOVERAGE)
216416static int sqlite3Fts5ParserCoverage(FILE *out){
216417 int stateno, iLookAhead, i;
216418 int nMissed = 0;
216419 for(stateno=0; stateno<fts5YYNSTATE; stateno++){
216420 i = fts5yy_shift_ofst[stateno];
216421 for(iLookAhead=0; iLookAhead<fts5YYNFTS5TOKEN; iLookAhead++){
216422 if( fts5yy_lookahead[i+iLookAhead]!=iLookAhead ) continue;
216423 if( fts5yycoverage[stateno][iLookAhead]==0 ) nMissed++;
216424 if( out ){
216425 fprintf(out,"State %d lookahead %s %s\n", stateno,
216426 fts5yyTokenName[iLookAhead],
216427 fts5yycoverage[stateno][iLookAhead] ? "ok" : "missed");
216428 }
216429 }
216430 }
216431 return nMissed;
216432}
216433#endif
216434
216435/*
216436** Find the appropriate action for a parser given the terminal
216437** look-ahead token iLookAhead.
216438*/
216439static fts5YYACTIONTYPE fts5yy_find_shift_action(
216440 fts5YYCODETYPE iLookAhead, /* The look-ahead token */
216441 fts5YYACTIONTYPE stateno /* Current state number */
216442){
216443 int i;
216444
216445 if( stateno>fts5YY_MAX_SHIFT ) return stateno;
216446 assert( stateno <= fts5YY_SHIFT_COUNT );
216447#if defined(fts5YYCOVERAGE)
216448 fts5yycoverage[stateno][iLookAhead] = 1;
216449#endif
216450 do{
216451 i = fts5yy_shift_ofst[stateno];
216452 assert( i>=0 );
216453 assert( i<=fts5YY_ACTTAB_COUNT );
216454 assert( i+fts5YYNFTS5TOKEN<=(int)fts5YY_NLOOKAHEAD );
216455 assert( iLookAhead!=fts5YYNOCODE );
216456 assert( iLookAhead < fts5YYNFTS5TOKEN );
216457 i += iLookAhead;
216458 assert( i<(int)fts5YY_NLOOKAHEAD );
216459 if( fts5yy_lookahead[i]!=iLookAhead ){
216460#ifdef fts5YYFALLBACK
216461 fts5YYCODETYPE iFallback; /* Fallback token */
216462 assert( iLookAhead<sizeof(fts5yyFallback)/sizeof(fts5yyFallback[0]) );
216463 iFallback = fts5yyFallback[iLookAhead];
216464 if( iFallback!=0 ){
216465#ifndef NDEBUG
216466 if( fts5yyTraceFILE ){
216467 fprintf(fts5yyTraceFILE, "%sFALLBACK %s => %s\n",
216468 fts5yyTracePrompt, fts5yyTokenName[iLookAhead], fts5yyTokenName[iFallback]);
216469 }
216470#endif
216471 assert( fts5yyFallback[iFallback]==0 ); /* Fallback loop must terminate */
216472 iLookAhead = iFallback;
216473 continue;
216474 }
216475#endif
216476#ifdef fts5YYWILDCARD
216477 {
216478 int j = i - iLookAhead + fts5YYWILDCARD;
216479 assert( j<(int)(sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0])) );
216480 if( fts5yy_lookahead[j]==fts5YYWILDCARD && iLookAhead>0 ){
216481#ifndef NDEBUG
216482 if( fts5yyTraceFILE ){
216483 fprintf(fts5yyTraceFILE, "%sWILDCARD %s => %s\n",
216484 fts5yyTracePrompt, fts5yyTokenName[iLookAhead],
216485 fts5yyTokenName[fts5YYWILDCARD]);
216486 }
216487#endif /* NDEBUG */
216488 return fts5yy_action[j];
216489 }
216490 }
216491#endif /* fts5YYWILDCARD */
216492 return fts5yy_default[stateno];
216493 }else{
216494 assert( i>=0 && i<(int)(sizeof(fts5yy_action)/sizeof(fts5yy_action[0])) );
216495 return fts5yy_action[i];
216496 }
216497 }while(1);
216498}
216499
216500/*
216501** Find the appropriate action for a parser given the non-terminal
216502** look-ahead token iLookAhead.
216503*/
216504static fts5YYACTIONTYPE fts5yy_find_reduce_action(
216505 fts5YYACTIONTYPE stateno, /* Current state number */
216506 fts5YYCODETYPE iLookAhead /* The look-ahead token */
216507){
216508 int i;
216509#ifdef fts5YYERRORSYMBOL
216510 if( stateno>fts5YY_REDUCE_COUNT ){
216511 return fts5yy_default[stateno];
216512 }
216513#else
216514 assert( stateno<=fts5YY_REDUCE_COUNT );
216515#endif
216516 i = fts5yy_reduce_ofst[stateno];
216517 assert( iLookAhead!=fts5YYNOCODE );
216518 i += iLookAhead;
216519#ifdef fts5YYERRORSYMBOL
216520 if( i<0 || i>=fts5YY_ACTTAB_COUNT || fts5yy_lookahead[i]!=iLookAhead ){
216521 return fts5yy_default[stateno];
216522 }
216523#else
216524 assert( i>=0 && i<fts5YY_ACTTAB_COUNT );
216525 assert( fts5yy_lookahead[i]==iLookAhead );
216526#endif
216527 return fts5yy_action[i];
216528}
216529
216530/*
216531** The following routine is called if the stack overflows.
216532*/
216533static void fts5yyStackOverflow(fts5yyParser *fts5yypParser){
216534 sqlite3Fts5ParserARG_FETCH
216535 sqlite3Fts5ParserCTX_FETCH
216536#ifndef NDEBUG
216537 if( fts5yyTraceFILE ){
216538 fprintf(fts5yyTraceFILE,"%sStack Overflow!\n",fts5yyTracePrompt);
216539 }
216540#endif
216541 while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack ) fts5yy_pop_parser_stack(pParser: fts5yypParser);
216542 /* Here code is inserted which will execute if the parser
216543 ** stack every overflows */
216544/******** Begin %stack_overflow code ******************************************/
216545
216546 sqlite3Fts5ParseError(pParse, zFmt: "fts5: parser stack overflow");
216547/******** End %stack_overflow code ********************************************/
216548 sqlite3Fts5ParserARG_STORE /* Suppress warning about unused %extra_argument var */
216549 sqlite3Fts5ParserCTX_STORE
216550}
216551
216552/*
216553** Print tracing information for a SHIFT action
216554*/
216555#ifndef NDEBUG
216556static void fts5yyTraceShift(fts5yyParser *fts5yypParser, int fts5yyNewState, const char *zTag){
216557 if( fts5yyTraceFILE ){
216558 if( fts5yyNewState<fts5YYNSTATE ){
216559 fprintf(fts5yyTraceFILE,"%s%s '%s', go to state %d\n",
216560 fts5yyTracePrompt, zTag, fts5yyTokenName[fts5yypParser->fts5yytos->major],
216561 fts5yyNewState);
216562 }else{
216563 fprintf(fts5yyTraceFILE,"%s%s '%s', pending reduce %d\n",
216564 fts5yyTracePrompt, zTag, fts5yyTokenName[fts5yypParser->fts5yytos->major],
216565 fts5yyNewState - fts5YY_MIN_REDUCE);
216566 }
216567 }
216568}
216569#else
216570# define fts5yyTraceShift(X,Y,Z)
216571#endif
216572
216573/*
216574** Perform a shift action.
216575*/
216576static void fts5yy_shift(
216577 fts5yyParser *fts5yypParser, /* The parser to be shifted */
216578 fts5YYACTIONTYPE fts5yyNewState, /* The new state to shift in */
216579 fts5YYCODETYPE fts5yyMajor, /* The major token to shift in */
216580 sqlite3Fts5ParserFTS5TOKENTYPE fts5yyMinor /* The minor token to shift in */
216581){
216582 fts5yyStackEntry *fts5yytos;
216583 fts5yypParser->fts5yytos++;
216584#ifdef fts5YYTRACKMAXSTACKDEPTH
216585 if( (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack)>fts5yypParser->fts5yyhwm ){
216586 fts5yypParser->fts5yyhwm++;
216587 assert( fts5yypParser->fts5yyhwm == (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack) );
216588 }
216589#endif
216590#if fts5YYSTACKDEPTH>0
216591 if( fts5yypParser->fts5yytos>fts5yypParser->fts5yystackEnd ){
216592 fts5yypParser->fts5yytos--;
216593 fts5yyStackOverflow(fts5yypParser);
216594 return;
216595 }
216596#else
216597 if( fts5yypParser->fts5yytos>=&fts5yypParser->fts5yystack[fts5yypParser->fts5yystksz] ){
216598 if( fts5yyGrowStack(fts5yypParser) ){
216599 fts5yypParser->fts5yytos--;
216600 fts5yyStackOverflow(fts5yypParser);
216601 return;
216602 }
216603 }
216604#endif
216605 if( fts5yyNewState > fts5YY_MAX_SHIFT ){
216606 fts5yyNewState += fts5YY_MIN_REDUCE - fts5YY_MIN_SHIFTREDUCE;
216607 }
216608 fts5yytos = fts5yypParser->fts5yytos;
216609 fts5yytos->stateno = fts5yyNewState;
216610 fts5yytos->major = fts5yyMajor;
216611 fts5yytos->minor.fts5yy0 = fts5yyMinor;
216612 fts5yyTraceShift(fts5yypParser, fts5yyNewState, "Shift");
216613}
216614
216615/* For rule J, fts5yyRuleInfoLhs[J] contains the symbol on the left-hand side
216616** of that rule */
216617static const fts5YYCODETYPE fts5yyRuleInfoLhs[] = {
216618 16, /* (0) input ::= expr */
216619 20, /* (1) colset ::= MINUS LCP colsetlist RCP */
216620 20, /* (2) colset ::= LCP colsetlist RCP */
216621 20, /* (3) colset ::= STRING */
216622 20, /* (4) colset ::= MINUS STRING */
216623 21, /* (5) colsetlist ::= colsetlist STRING */
216624 21, /* (6) colsetlist ::= STRING */
216625 17, /* (7) expr ::= expr AND expr */
216626 17, /* (8) expr ::= expr OR expr */
216627 17, /* (9) expr ::= expr NOT expr */
216628 17, /* (10) expr ::= colset COLON LP expr RP */
216629 17, /* (11) expr ::= LP expr RP */
216630 17, /* (12) expr ::= exprlist */
216631 19, /* (13) exprlist ::= cnearset */
216632 19, /* (14) exprlist ::= exprlist cnearset */
216633 18, /* (15) cnearset ::= nearset */
216634 18, /* (16) cnearset ::= colset COLON nearset */
216635 22, /* (17) nearset ::= phrase */
216636 22, /* (18) nearset ::= CARET phrase */
216637 22, /* (19) nearset ::= STRING LP nearphrases neardist_opt RP */
216638 23, /* (20) nearphrases ::= phrase */
216639 23, /* (21) nearphrases ::= nearphrases phrase */
216640 25, /* (22) neardist_opt ::= */
216641 25, /* (23) neardist_opt ::= COMMA STRING */
216642 24, /* (24) phrase ::= phrase PLUS STRING star_opt */
216643 24, /* (25) phrase ::= STRING star_opt */
216644 26, /* (26) star_opt ::= STAR */
216645 26, /* (27) star_opt ::= */
216646};
216647
216648/* For rule J, fts5yyRuleInfoNRhs[J] contains the negative of the number
216649** of symbols on the right-hand side of that rule. */
216650static const signed char fts5yyRuleInfoNRhs[] = {
216651 -1, /* (0) input ::= expr */
216652 -4, /* (1) colset ::= MINUS LCP colsetlist RCP */
216653 -3, /* (2) colset ::= LCP colsetlist RCP */
216654 -1, /* (3) colset ::= STRING */
216655 -2, /* (4) colset ::= MINUS STRING */
216656 -2, /* (5) colsetlist ::= colsetlist STRING */
216657 -1, /* (6) colsetlist ::= STRING */
216658 -3, /* (7) expr ::= expr AND expr */
216659 -3, /* (8) expr ::= expr OR expr */
216660 -3, /* (9) expr ::= expr NOT expr */
216661 -5, /* (10) expr ::= colset COLON LP expr RP */
216662 -3, /* (11) expr ::= LP expr RP */
216663 -1, /* (12) expr ::= exprlist */
216664 -1, /* (13) exprlist ::= cnearset */
216665 -2, /* (14) exprlist ::= exprlist cnearset */
216666 -1, /* (15) cnearset ::= nearset */
216667 -3, /* (16) cnearset ::= colset COLON nearset */
216668 -1, /* (17) nearset ::= phrase */
216669 -2, /* (18) nearset ::= CARET phrase */
216670 -5, /* (19) nearset ::= STRING LP nearphrases neardist_opt RP */
216671 -1, /* (20) nearphrases ::= phrase */
216672 -2, /* (21) nearphrases ::= nearphrases phrase */
216673 0, /* (22) neardist_opt ::= */
216674 -2, /* (23) neardist_opt ::= COMMA STRING */
216675 -4, /* (24) phrase ::= phrase PLUS STRING star_opt */
216676 -2, /* (25) phrase ::= STRING star_opt */
216677 -1, /* (26) star_opt ::= STAR */
216678 0, /* (27) star_opt ::= */
216679};
216680
216681static void fts5yy_accept(fts5yyParser*); /* Forward Declaration */
216682
216683/*
216684** Perform a reduce action and the shift that must immediately
216685** follow the reduce.
216686**
216687** The fts5yyLookahead and fts5yyLookaheadToken parameters provide reduce actions
216688** access to the lookahead token (if any). The fts5yyLookahead will be fts5YYNOCODE
216689** if the lookahead token has already been consumed. As this procedure is
216690** only called from one place, optimizing compilers will in-line it, which
216691** means that the extra parameters have no performance impact.
216692*/
216693static fts5YYACTIONTYPE fts5yy_reduce(
216694 fts5yyParser *fts5yypParser, /* The parser */
216695 unsigned int fts5yyruleno, /* Number of the rule by which to reduce */
216696 int fts5yyLookahead, /* Lookahead token, or fts5YYNOCODE if none */
216697 sqlite3Fts5ParserFTS5TOKENTYPE fts5yyLookaheadToken /* Value of the lookahead token */
216698 sqlite3Fts5ParserCTX_PDECL /* %extra_context */
216699){
216700 int fts5yygoto; /* The next state */
216701 fts5YYACTIONTYPE fts5yyact; /* The next action */
216702 fts5yyStackEntry *fts5yymsp; /* The top of the parser's stack */
216703 int fts5yysize; /* Amount to pop the stack */
216704 sqlite3Fts5ParserARG_FETCH
216705 (void)fts5yyLookahead;
216706 (void)fts5yyLookaheadToken;
216707 fts5yymsp = fts5yypParser->fts5yytos;
216708
216709 switch( fts5yyruleno ){
216710 /* Beginning here are the reduction cases. A typical example
216711 ** follows:
216712 ** case 0:
216713 ** #line <lineno> <grammarfile>
216714 ** { ... } // User supplied code
216715 ** #line <lineno> <thisfile>
216716 ** break;
216717 */
216718/********** Begin reduce actions **********************************************/
216719 fts5YYMINORTYPE fts5yylhsminor;
216720 case 0: /* input ::= expr */
216721{ sqlite3Fts5ParseFinished(pParse, p: fts5yymsp[0].minor.fts5yy24); }
216722 break;
216723 case 1: /* colset ::= MINUS LCP colsetlist RCP */
216724{
216725 fts5yymsp[-3].minor.fts5yy11 = sqlite3Fts5ParseColsetInvert(pParse, fts5yymsp[-1].minor.fts5yy11);
216726}
216727 break;
216728 case 2: /* colset ::= LCP colsetlist RCP */
216729{ fts5yymsp[-2].minor.fts5yy11 = fts5yymsp[-1].minor.fts5yy11; }
216730 break;
216731 case 3: /* colset ::= STRING */
216732{
216733 fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
216734}
216735 fts5yymsp[0].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
216736 break;
216737 case 4: /* colset ::= MINUS STRING */
216738{
216739 fts5yymsp[-1].minor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
216740 fts5yymsp[-1].minor.fts5yy11 = sqlite3Fts5ParseColsetInvert(pParse, fts5yymsp[-1].minor.fts5yy11);
216741}
216742 break;
216743 case 5: /* colsetlist ::= colsetlist STRING */
216744{
216745 fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, fts5yymsp[-1].minor.fts5yy11, &fts5yymsp[0].minor.fts5yy0); }
216746 fts5yymsp[-1].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
216747 break;
216748 case 6: /* colsetlist ::= STRING */
216749{
216750 fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
216751}
216752 fts5yymsp[0].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
216753 break;
216754 case 7: /* expr ::= expr AND expr */
216755{
216756 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_AND, pLeft: fts5yymsp[-2].minor.fts5yy24, pRight: fts5yymsp[0].minor.fts5yy24, pNear: 0);
216757}
216758 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
216759 break;
216760 case 8: /* expr ::= expr OR expr */
216761{
216762 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_OR, pLeft: fts5yymsp[-2].minor.fts5yy24, pRight: fts5yymsp[0].minor.fts5yy24, pNear: 0);
216763}
216764 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
216765 break;
216766 case 9: /* expr ::= expr NOT expr */
216767{
216768 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_NOT, pLeft: fts5yymsp[-2].minor.fts5yy24, pRight: fts5yymsp[0].minor.fts5yy24, pNear: 0);
216769}
216770 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
216771 break;
216772 case 10: /* expr ::= colset COLON LP expr RP */
216773{
216774 sqlite3Fts5ParseSetColset(pParse, fts5yymsp[-1].minor.fts5yy24, fts5yymsp[-4].minor.fts5yy11);
216775 fts5yylhsminor.fts5yy24 = fts5yymsp[-1].minor.fts5yy24;
216776}
216777 fts5yymsp[-4].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
216778 break;
216779 case 11: /* expr ::= LP expr RP */
216780{fts5yymsp[-2].minor.fts5yy24 = fts5yymsp[-1].minor.fts5yy24;}
216781 break;
216782 case 12: /* expr ::= exprlist */
216783 case 13: /* exprlist ::= cnearset */ fts5yytestcase(fts5yyruleno==13);
216784{fts5yylhsminor.fts5yy24 = fts5yymsp[0].minor.fts5yy24;}
216785 fts5yymsp[0].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
216786 break;
216787 case 14: /* exprlist ::= exprlist cnearset */
216788{
216789 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseImplicitAnd(pParse, pLeft: fts5yymsp[-1].minor.fts5yy24, pRight: fts5yymsp[0].minor.fts5yy24);
216790}
216791 fts5yymsp[-1].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
216792 break;
216793 case 15: /* cnearset ::= nearset */
216794{
216795 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_STRING, pLeft: 0, pRight: 0, pNear: fts5yymsp[0].minor.fts5yy46);
216796}
216797 fts5yymsp[0].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
216798 break;
216799 case 16: /* cnearset ::= colset COLON nearset */
216800{
216801 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_STRING, pLeft: 0, pRight: 0, pNear: fts5yymsp[0].minor.fts5yy46);
216802 sqlite3Fts5ParseSetColset(pParse, fts5yylhsminor.fts5yy24, fts5yymsp[-2].minor.fts5yy11);
216803}
216804 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
216805 break;
216806 case 17: /* nearset ::= phrase */
216807{ fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53); }
216808 fts5yymsp[0].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
216809 break;
216810 case 18: /* nearset ::= CARET phrase */
216811{
216812 sqlite3Fts5ParseSetCaret(fts5yymsp[0].minor.fts5yy53);
216813 fts5yymsp[-1].minor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53);
216814}
216815 break;
216816 case 19: /* nearset ::= STRING LP nearphrases neardist_opt RP */
216817{
216818 sqlite3Fts5ParseNear(pParse, &fts5yymsp[-4].minor.fts5yy0);
216819 sqlite3Fts5ParseSetDistance(pParse, fts5yymsp[-2].minor.fts5yy46, &fts5yymsp[-1].minor.fts5yy0);
216820 fts5yylhsminor.fts5yy46 = fts5yymsp[-2].minor.fts5yy46;
216821}
216822 fts5yymsp[-4].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
216823 break;
216824 case 20: /* nearphrases ::= phrase */
216825{
216826 fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53);
216827}
216828 fts5yymsp[0].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
216829 break;
216830 case 21: /* nearphrases ::= nearphrases phrase */
216831{
216832 fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, fts5yymsp[-1].minor.fts5yy46, fts5yymsp[0].minor.fts5yy53);
216833}
216834 fts5yymsp[-1].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
216835 break;
216836 case 22: /* neardist_opt ::= */
216837{ fts5yymsp[1].minor.fts5yy0.p = 0; fts5yymsp[1].minor.fts5yy0.n = 0; }
216838 break;
216839 case 23: /* neardist_opt ::= COMMA STRING */
216840{ fts5yymsp[-1].minor.fts5yy0 = fts5yymsp[0].minor.fts5yy0; }
216841 break;
216842 case 24: /* phrase ::= phrase PLUS STRING star_opt */
216843{
216844 fts5yylhsminor.fts5yy53 = sqlite3Fts5ParseTerm(pParse, pPhrase: fts5yymsp[-3].minor.fts5yy53, pToken: &fts5yymsp[-1].minor.fts5yy0, bPrefix: fts5yymsp[0].minor.fts5yy4);
216845}
216846 fts5yymsp[-3].minor.fts5yy53 = fts5yylhsminor.fts5yy53;
216847 break;
216848 case 25: /* phrase ::= STRING star_opt */
216849{
216850 fts5yylhsminor.fts5yy53 = sqlite3Fts5ParseTerm(pParse, pPhrase: 0, pToken: &fts5yymsp[-1].minor.fts5yy0, bPrefix: fts5yymsp[0].minor.fts5yy4);
216851}
216852 fts5yymsp[-1].minor.fts5yy53 = fts5yylhsminor.fts5yy53;
216853 break;
216854 case 26: /* star_opt ::= STAR */
216855{ fts5yymsp[0].minor.fts5yy4 = 1; }
216856 break;
216857 case 27: /* star_opt ::= */
216858{ fts5yymsp[1].minor.fts5yy4 = 0; }
216859 break;
216860 default:
216861 break;
216862/********** End reduce actions ************************************************/
216863 };
216864 assert( fts5yyruleno<sizeof(fts5yyRuleInfoLhs)/sizeof(fts5yyRuleInfoLhs[0]) );
216865 fts5yygoto = fts5yyRuleInfoLhs[fts5yyruleno];
216866 fts5yysize = fts5yyRuleInfoNRhs[fts5yyruleno];
216867 fts5yyact = fts5yy_find_reduce_action(stateno: fts5yymsp[fts5yysize].stateno,iLookAhead: (fts5YYCODETYPE)fts5yygoto);
216868
216869 /* There are no SHIFTREDUCE actions on nonterminals because the table
216870 ** generator has simplified them to pure REDUCE actions. */
216871 assert( !(fts5yyact>fts5YY_MAX_SHIFT && fts5yyact<=fts5YY_MAX_SHIFTREDUCE) );
216872
216873 /* It is not possible for a REDUCE to be followed by an error */
216874 assert( fts5yyact!=fts5YY_ERROR_ACTION );
216875
216876 fts5yymsp += fts5yysize+1;
216877 fts5yypParser->fts5yytos = fts5yymsp;
216878 fts5yymsp->stateno = (fts5YYACTIONTYPE)fts5yyact;
216879 fts5yymsp->major = (fts5YYCODETYPE)fts5yygoto;
216880 fts5yyTraceShift(fts5yypParser, fts5yyact, "... then shift");
216881 return fts5yyact;
216882}
216883
216884/*
216885** The following code executes when the parse fails
216886*/
216887#ifndef fts5YYNOERRORRECOVERY
216888static void fts5yy_parse_failed(
216889 fts5yyParser *fts5yypParser /* The parser */
216890){
216891 sqlite3Fts5ParserARG_FETCH
216892 sqlite3Fts5ParserCTX_FETCH
216893#ifndef NDEBUG
216894 if( fts5yyTraceFILE ){
216895 fprintf(fts5yyTraceFILE,"%sFail!\n",fts5yyTracePrompt);
216896 }
216897#endif
216898 while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack ) fts5yy_pop_parser_stack(fts5yypParser);
216899 /* Here code is inserted which will be executed whenever the
216900 ** parser fails */
216901/************ Begin %parse_failure code ***************************************/
216902/************ End %parse_failure code *****************************************/
216903 sqlite3Fts5ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
216904 sqlite3Fts5ParserCTX_STORE
216905}
216906#endif /* fts5YYNOERRORRECOVERY */
216907
216908/*
216909** The following code executes when a syntax error first occurs.
216910*/
216911static void fts5yy_syntax_error(
216912 fts5yyParser *fts5yypParser, /* The parser */
216913 int fts5yymajor, /* The major type of the error token */
216914 sqlite3Fts5ParserFTS5TOKENTYPE fts5yyminor /* The minor type of the error token */
216915){
216916 sqlite3Fts5ParserARG_FETCH
216917 sqlite3Fts5ParserCTX_FETCH
216918#define FTS5TOKEN fts5yyminor
216919/************ Begin %syntax_error code ****************************************/
216920
216921 UNUSED_PARAM(fts5yymajor); /* Silence a compiler warning */
216922 sqlite3Fts5ParseError(
216923 pParse, zFmt: "fts5: syntax error near \"%.*s\"",FTS5TOKEN.n,FTS5TOKEN.p
216924 );
216925/************ End %syntax_error code ******************************************/
216926 sqlite3Fts5ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
216927 sqlite3Fts5ParserCTX_STORE
216928}
216929
216930/*
216931** The following is executed when the parser accepts
216932*/
216933static void fts5yy_accept(
216934 fts5yyParser *fts5yypParser /* The parser */
216935){
216936 sqlite3Fts5ParserARG_FETCH
216937 sqlite3Fts5ParserCTX_FETCH
216938#ifndef NDEBUG
216939 if( fts5yyTraceFILE ){
216940 fprintf(fts5yyTraceFILE,"%sAccept!\n",fts5yyTracePrompt);
216941 }
216942#endif
216943#ifndef fts5YYNOERRORRECOVERY
216944 fts5yypParser->fts5yyerrcnt = -1;
216945#endif
216946 assert( fts5yypParser->fts5yytos==fts5yypParser->fts5yystack );
216947 /* Here code is inserted which will be executed whenever the
216948 ** parser accepts */
216949/*********** Begin %parse_accept code *****************************************/
216950/*********** End %parse_accept code *******************************************/
216951 sqlite3Fts5ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
216952 sqlite3Fts5ParserCTX_STORE
216953}
216954
216955/* The main parser program.
216956** The first argument is a pointer to a structure obtained from
216957** "sqlite3Fts5ParserAlloc" which describes the current state of the parser.
216958** The second argument is the major token number. The third is
216959** the minor token. The fourth optional argument is whatever the
216960** user wants (and specified in the grammar) and is available for
216961** use by the action routines.
216962**
216963** Inputs:
216964** <ul>
216965** <li> A pointer to the parser (an opaque structure.)
216966** <li> The major token number.
216967** <li> The minor token number.
216968** <li> An option argument of a grammar-specified type.
216969** </ul>
216970**
216971** Outputs:
216972** None.
216973*/
216974static void sqlite3Fts5Parser(
216975 void *fts5yyp, /* The parser */
216976 int fts5yymajor, /* The major token code number */
216977 sqlite3Fts5ParserFTS5TOKENTYPE fts5yyminor /* The value for the token */
216978 sqlite3Fts5ParserARG_PDECL /* Optional %extra_argument parameter */
216979){
216980 fts5YYMINORTYPE fts5yyminorunion;
216981 fts5YYACTIONTYPE fts5yyact; /* The parser action. */
216982#if !defined(fts5YYERRORSYMBOL) && !defined(fts5YYNOERRORRECOVERY)
216983 int fts5yyendofinput; /* True if we are at the end of input */
216984#endif
216985#ifdef fts5YYERRORSYMBOL
216986 int fts5yyerrorhit = 0; /* True if fts5yymajor has invoked an error */
216987#endif
216988 fts5yyParser *fts5yypParser = (fts5yyParser*)fts5yyp; /* The parser */
216989 sqlite3Fts5ParserCTX_FETCH
216990 sqlite3Fts5ParserARG_STORE
216991
216992 assert( fts5yypParser->fts5yytos!=0 );
216993#if !defined(fts5YYERRORSYMBOL) && !defined(fts5YYNOERRORRECOVERY)
216994 fts5yyendofinput = (fts5yymajor==0);
216995#endif
216996
216997 fts5yyact = fts5yypParser->fts5yytos->stateno;
216998#ifndef NDEBUG
216999 if( fts5yyTraceFILE ){
217000 if( fts5yyact < fts5YY_MIN_REDUCE ){
217001 fprintf(fts5yyTraceFILE,"%sInput '%s' in state %d\n",
217002 fts5yyTracePrompt,fts5yyTokenName[fts5yymajor],fts5yyact);
217003 }else{
217004 fprintf(fts5yyTraceFILE,"%sInput '%s' with pending reduce %d\n",
217005 fts5yyTracePrompt,fts5yyTokenName[fts5yymajor],fts5yyact-fts5YY_MIN_REDUCE);
217006 }
217007 }
217008#endif
217009
217010 while(1){ /* Exit by "break" */
217011 assert( fts5yypParser->fts5yytos>=fts5yypParser->fts5yystack );
217012 assert( fts5yyact==fts5yypParser->fts5yytos->stateno );
217013 fts5yyact = fts5yy_find_shift_action(iLookAhead: (fts5YYCODETYPE)fts5yymajor,stateno: fts5yyact);
217014 if( fts5yyact >= fts5YY_MIN_REDUCE ){
217015 unsigned int fts5yyruleno = fts5yyact - fts5YY_MIN_REDUCE; /* Reduce by this rule */
217016#ifndef NDEBUG
217017 assert( fts5yyruleno<(int)(sizeof(fts5yyRuleName)/sizeof(fts5yyRuleName[0])) );
217018 if( fts5yyTraceFILE ){
217019 int fts5yysize = fts5yyRuleInfoNRhs[fts5yyruleno];
217020 if( fts5yysize ){
217021 fprintf(fts5yyTraceFILE, "%sReduce %d [%s]%s, pop back to state %d.\n",
217022 fts5yyTracePrompt,
217023 fts5yyruleno, fts5yyRuleName[fts5yyruleno],
217024 fts5yyruleno<fts5YYNRULE_WITH_ACTION ? "" : " without external action",
217025 fts5yypParser->fts5yytos[fts5yysize].stateno);
217026 }else{
217027 fprintf(fts5yyTraceFILE, "%sReduce %d [%s]%s.\n",
217028 fts5yyTracePrompt, fts5yyruleno, fts5yyRuleName[fts5yyruleno],
217029 fts5yyruleno<fts5YYNRULE_WITH_ACTION ? "" : " without external action");
217030 }
217031 }
217032#endif /* NDEBUG */
217033
217034 /* Check that the stack is large enough to grow by a single entry
217035 ** if the RHS of the rule is empty. This ensures that there is room
217036 ** enough on the stack to push the LHS value */
217037 if( fts5yyRuleInfoNRhs[fts5yyruleno]==0 ){
217038#ifdef fts5YYTRACKMAXSTACKDEPTH
217039 if( (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack)>fts5yypParser->fts5yyhwm ){
217040 fts5yypParser->fts5yyhwm++;
217041 assert( fts5yypParser->fts5yyhwm ==
217042 (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack));
217043 }
217044#endif
217045#if fts5YYSTACKDEPTH>0
217046 if( fts5yypParser->fts5yytos>=fts5yypParser->fts5yystackEnd ){
217047 fts5yyStackOverflow(fts5yypParser);
217048 break;
217049 }
217050#else
217051 if( fts5yypParser->fts5yytos>=&fts5yypParser->fts5yystack[fts5yypParser->fts5yystksz-1] ){
217052 if( fts5yyGrowStack(fts5yypParser) ){
217053 fts5yyStackOverflow(fts5yypParser);
217054 break;
217055 }
217056 }
217057#endif
217058 }
217059 fts5yyact = fts5yy_reduce(fts5yypParser,fts5yyruleno,fts5yyLookahead: fts5yymajor,fts5yyLookaheadToken: fts5yyminor sqlite3Fts5ParserCTX_PARAM);
217060 }else if( fts5yyact <= fts5YY_MAX_SHIFTREDUCE ){
217061 fts5yy_shift(fts5yypParser,fts5yyNewState: fts5yyact,fts5yyMajor: (fts5YYCODETYPE)fts5yymajor,fts5yyMinor: fts5yyminor);
217062#ifndef fts5YYNOERRORRECOVERY
217063 fts5yypParser->fts5yyerrcnt--;
217064#endif
217065 break;
217066 }else if( fts5yyact==fts5YY_ACCEPT_ACTION ){
217067 fts5yypParser->fts5yytos--;
217068 fts5yy_accept(fts5yypParser);
217069 return;
217070 }else{
217071 assert( fts5yyact == fts5YY_ERROR_ACTION );
217072 fts5yyminorunion.fts5yy0 = fts5yyminor;
217073#ifdef fts5YYERRORSYMBOL
217074 int fts5yymx;
217075#endif
217076#ifndef NDEBUG
217077 if( fts5yyTraceFILE ){
217078 fprintf(fts5yyTraceFILE,"%sSyntax Error!\n",fts5yyTracePrompt);
217079 }
217080#endif
217081#ifdef fts5YYERRORSYMBOL
217082 /* A syntax error has occurred.
217083 ** The response to an error depends upon whether or not the
217084 ** grammar defines an error token "ERROR".
217085 **
217086 ** This is what we do if the grammar does define ERROR:
217087 **
217088 ** * Call the %syntax_error function.
217089 **
217090 ** * Begin popping the stack until we enter a state where
217091 ** it is legal to shift the error symbol, then shift
217092 ** the error symbol.
217093 **
217094 ** * Set the error count to three.
217095 **
217096 ** * Begin accepting and shifting new tokens. No new error
217097 ** processing will occur until three tokens have been
217098 ** shifted successfully.
217099 **
217100 */
217101 if( fts5yypParser->fts5yyerrcnt<0 ){
217102 fts5yy_syntax_error(fts5yypParser,fts5yymajor,fts5yyminor);
217103 }
217104 fts5yymx = fts5yypParser->fts5yytos->major;
217105 if( fts5yymx==fts5YYERRORSYMBOL || fts5yyerrorhit ){
217106#ifndef NDEBUG
217107 if( fts5yyTraceFILE ){
217108 fprintf(fts5yyTraceFILE,"%sDiscard input token %s\n",
217109 fts5yyTracePrompt,fts5yyTokenName[fts5yymajor]);
217110 }
217111#endif
217112 fts5yy_destructor(fts5yypParser, (fts5YYCODETYPE)fts5yymajor, &fts5yyminorunion);
217113 fts5yymajor = fts5YYNOCODE;
217114 }else{
217115 while( fts5yypParser->fts5yytos > fts5yypParser->fts5yystack ){
217116 fts5yyact = fts5yy_find_reduce_action(fts5yypParser->fts5yytos->stateno,
217117 fts5YYERRORSYMBOL);
217118 if( fts5yyact<=fts5YY_MAX_SHIFTREDUCE ) break;
217119 fts5yy_pop_parser_stack(fts5yypParser);
217120 }
217121 if( fts5yypParser->fts5yytos <= fts5yypParser->fts5yystack || fts5yymajor==0 ){
217122 fts5yy_destructor(fts5yypParser,(fts5YYCODETYPE)fts5yymajor,&fts5yyminorunion);
217123 fts5yy_parse_failed(fts5yypParser);
217124#ifndef fts5YYNOERRORRECOVERY
217125 fts5yypParser->fts5yyerrcnt = -1;
217126#endif
217127 fts5yymajor = fts5YYNOCODE;
217128 }else if( fts5yymx!=fts5YYERRORSYMBOL ){
217129 fts5yy_shift(fts5yypParser,fts5yyact,fts5YYERRORSYMBOL,fts5yyminor);
217130 }
217131 }
217132 fts5yypParser->fts5yyerrcnt = 3;
217133 fts5yyerrorhit = 1;
217134 if( fts5yymajor==fts5YYNOCODE ) break;
217135 fts5yyact = fts5yypParser->fts5yytos->stateno;
217136#elif defined(fts5YYNOERRORRECOVERY)
217137 /* If the fts5YYNOERRORRECOVERY macro is defined, then do not attempt to
217138 ** do any kind of error recovery. Instead, simply invoke the syntax
217139 ** error routine and continue going as if nothing had happened.
217140 **
217141 ** Applications can set this macro (for example inside %include) if
217142 ** they intend to abandon the parse upon the first syntax error seen.
217143 */
217144 fts5yy_syntax_error(fts5yypParser,fts5yymajor, fts5yyminor);
217145 fts5yy_destructor(fts5yypParser,fts5yymajor: (fts5YYCODETYPE)fts5yymajor,fts5yypminor: &fts5yyminorunion);
217146 break;
217147#else /* fts5YYERRORSYMBOL is not defined */
217148 /* This is what we do if the grammar does not define ERROR:
217149 **
217150 ** * Report an error message, and throw away the input token.
217151 **
217152 ** * If the input token is $, then fail the parse.
217153 **
217154 ** As before, subsequent error messages are suppressed until
217155 ** three input tokens have been successfully shifted.
217156 */
217157 if( fts5yypParser->fts5yyerrcnt<=0 ){
217158 fts5yy_syntax_error(fts5yypParser,fts5yymajor, fts5yyminor);
217159 }
217160 fts5yypParser->fts5yyerrcnt = 3;
217161 fts5yy_destructor(fts5yypParser,(fts5YYCODETYPE)fts5yymajor,&fts5yyminorunion);
217162 if( fts5yyendofinput ){
217163 fts5yy_parse_failed(fts5yypParser);
217164#ifndef fts5YYNOERRORRECOVERY
217165 fts5yypParser->fts5yyerrcnt = -1;
217166#endif
217167 }
217168 break;
217169#endif
217170 }
217171 }
217172#ifndef NDEBUG
217173 if( fts5yyTraceFILE ){
217174 fts5yyStackEntry *i;
217175 char cDiv = '[';
217176 fprintf(fts5yyTraceFILE,"%sReturn. Stack=",fts5yyTracePrompt);
217177 for(i=&fts5yypParser->fts5yystack[1]; i<=fts5yypParser->fts5yytos; i++){
217178 fprintf(fts5yyTraceFILE,"%c%s", cDiv, fts5yyTokenName[i->major]);
217179 cDiv = ' ';
217180 }
217181 fprintf(fts5yyTraceFILE,"]\n");
217182 }
217183#endif
217184 return;
217185}
217186
217187/*
217188** Return the fallback token corresponding to canonical token iToken, or
217189** 0 if iToken has no fallback.
217190*/
217191static int sqlite3Fts5ParserFallback(int iToken){
217192#ifdef fts5YYFALLBACK
217193 assert( iToken<(int)(sizeof(fts5yyFallback)/sizeof(fts5yyFallback[0])) );
217194 return fts5yyFallback[iToken];
217195#else
217196 (void)iToken;
217197 return 0;
217198#endif
217199}
217200
217201/*
217202** 2014 May 31
217203**
217204** The author disclaims copyright to this source code. In place of
217205** a legal notice, here is a blessing:
217206**
217207** May you do good and not evil.
217208** May you find forgiveness for yourself and forgive others.
217209** May you share freely, never taking more than you give.
217210**
217211******************************************************************************
217212*/
217213
217214
217215/* #include "fts5Int.h" */
217216#include <math.h> /* amalgamator: keep */
217217
217218/*
217219** Object used to iterate through all "coalesced phrase instances" in
217220** a single column of the current row. If the phrase instances in the
217221** column being considered do not overlap, this object simply iterates
217222** through them. Or, if they do overlap (share one or more tokens in
217223** common), each set of overlapping instances is treated as a single
217224** match. See documentation for the highlight() auxiliary function for
217225** details.
217226**
217227** Usage is:
217228**
217229** for(rc = fts5CInstIterNext(pApi, pFts, iCol, &iter);
217230** (rc==SQLITE_OK && 0==fts5CInstIterEof(&iter);
217231** rc = fts5CInstIterNext(&iter)
217232** ){
217233** printf("instance starts at %d, ends at %d\n", iter.iStart, iter.iEnd);
217234** }
217235**
217236*/
217237typedef struct CInstIter CInstIter;
217238struct CInstIter {
217239 const Fts5ExtensionApi *pApi; /* API offered by current FTS version */
217240 Fts5Context *pFts; /* First arg to pass to pApi functions */
217241 int iCol; /* Column to search */
217242 int iInst; /* Next phrase instance index */
217243 int nInst; /* Total number of phrase instances */
217244
217245 /* Output variables */
217246 int iStart; /* First token in coalesced phrase instance */
217247 int iEnd; /* Last token in coalesced phrase instance */
217248};
217249
217250/*
217251** Advance the iterator to the next coalesced phrase instance. Return
217252** an SQLite error code if an error occurs, or SQLITE_OK otherwise.
217253*/
217254static int fts5CInstIterNext(CInstIter *pIter){
217255 int rc = SQLITE_OK;
217256 pIter->iStart = -1;
217257 pIter->iEnd = -1;
217258
217259 while( rc==SQLITE_OK && pIter->iInst<pIter->nInst ){
217260 int ip; int ic; int io;
217261 rc = pIter->pApi->xInst(pIter->pFts, pIter->iInst, &ip, &ic, &io);
217262 if( rc==SQLITE_OK ){
217263 if( ic==pIter->iCol ){
217264 int iEnd = io - 1 + pIter->pApi->xPhraseSize(pIter->pFts, ip);
217265 if( pIter->iStart<0 ){
217266 pIter->iStart = io;
217267 pIter->iEnd = iEnd;
217268 }else if( io<=pIter->iEnd ){
217269 if( iEnd>pIter->iEnd ) pIter->iEnd = iEnd;
217270 }else{
217271 break;
217272 }
217273 }
217274 pIter->iInst++;
217275 }
217276 }
217277
217278 return rc;
217279}
217280
217281/*
217282** Initialize the iterator object indicated by the final parameter to
217283** iterate through coalesced phrase instances in column iCol.
217284*/
217285static int fts5CInstIterInit(
217286 const Fts5ExtensionApi *pApi,
217287 Fts5Context *pFts,
217288 int iCol,
217289 CInstIter *pIter
217290){
217291 int rc;
217292
217293 memset(s: pIter, c: 0, n: sizeof(CInstIter));
217294 pIter->pApi = pApi;
217295 pIter->pFts = pFts;
217296 pIter->iCol = iCol;
217297 rc = pApi->xInstCount(pFts, &pIter->nInst);
217298
217299 if( rc==SQLITE_OK ){
217300 rc = fts5CInstIterNext(pIter);
217301 }
217302
217303 return rc;
217304}
217305
217306
217307
217308/*************************************************************************
217309** Start of highlight() implementation.
217310*/
217311typedef struct HighlightContext HighlightContext;
217312struct HighlightContext {
217313 CInstIter iter; /* Coalesced Instance Iterator */
217314 int iPos; /* Current token offset in zIn[] */
217315 int iRangeStart; /* First token to include */
217316 int iRangeEnd; /* If non-zero, last token to include */
217317 const char *zOpen; /* Opening highlight */
217318 const char *zClose; /* Closing highlight */
217319 const char *zIn; /* Input text */
217320 int nIn; /* Size of input text in bytes */
217321 int iOff; /* Current offset within zIn[] */
217322 char *zOut; /* Output value */
217323};
217324
217325/*
217326** Append text to the HighlightContext output string - p->zOut. Argument
217327** z points to a buffer containing n bytes of text to append. If n is
217328** negative, everything up until the first '\0' is appended to the output.
217329**
217330** If *pRc is set to any value other than SQLITE_OK when this function is
217331** called, it is a no-op. If an error (i.e. an OOM condition) is encountered,
217332** *pRc is set to an error code before returning.
217333*/
217334static void fts5HighlightAppend(
217335 int *pRc,
217336 HighlightContext *p,
217337 const char *z, int n
217338){
217339 if( *pRc==SQLITE_OK && z ){
217340 if( n<0 ) n = (int)strlen(s: z);
217341 p->zOut = sqlite3_mprintf(zFormat: "%z%.*s", p->zOut, n, z);
217342 if( p->zOut==0 ) *pRc = SQLITE_NOMEM;
217343 }
217344}
217345
217346/*
217347** Tokenizer callback used by implementation of highlight() function.
217348*/
217349static int fts5HighlightCb(
217350 void *pContext, /* Pointer to HighlightContext object */
217351 int tflags, /* Mask of FTS5_TOKEN_* flags */
217352 const char *pToken, /* Buffer containing token */
217353 int nToken, /* Size of token in bytes */
217354 int iStartOff, /* Start offset of token */
217355 int iEndOff /* End offset of token */
217356){
217357 HighlightContext *p = (HighlightContext*)pContext;
217358 int rc = SQLITE_OK;
217359 int iPos;
217360
217361 UNUSED_PARAM2(pToken, nToken);
217362
217363 if( tflags & FTS5_TOKEN_COLOCATED ) return SQLITE_OK;
217364 iPos = p->iPos++;
217365
217366 if( p->iRangeEnd>0 ){
217367 if( iPos<p->iRangeStart || iPos>p->iRangeEnd ) return SQLITE_OK;
217368 if( p->iRangeStart && iPos==p->iRangeStart ) p->iOff = iStartOff;
217369 }
217370
217371 if( iPos==p->iter.iStart ){
217372 fts5HighlightAppend(pRc: &rc, p, z: &p->zIn[p->iOff], n: iStartOff - p->iOff);
217373 fts5HighlightAppend(pRc: &rc, p, z: p->zOpen, n: -1);
217374 p->iOff = iStartOff;
217375 }
217376
217377 if( iPos==p->iter.iEnd ){
217378 if( p->iRangeEnd && p->iter.iStart<p->iRangeStart ){
217379 fts5HighlightAppend(pRc: &rc, p, z: p->zOpen, n: -1);
217380 }
217381 fts5HighlightAppend(pRc: &rc, p, z: &p->zIn[p->iOff], n: iEndOff - p->iOff);
217382 fts5HighlightAppend(pRc: &rc, p, z: p->zClose, n: -1);
217383 p->iOff = iEndOff;
217384 if( rc==SQLITE_OK ){
217385 rc = fts5CInstIterNext(pIter: &p->iter);
217386 }
217387 }
217388
217389 if( p->iRangeEnd>0 && iPos==p->iRangeEnd ){
217390 fts5HighlightAppend(pRc: &rc, p, z: &p->zIn[p->iOff], n: iEndOff - p->iOff);
217391 p->iOff = iEndOff;
217392 if( iPos>=p->iter.iStart && iPos<p->iter.iEnd ){
217393 fts5HighlightAppend(pRc: &rc, p, z: p->zClose, n: -1);
217394 }
217395 }
217396
217397 return rc;
217398}
217399
217400/*
217401** Implementation of highlight() function.
217402*/
217403static void fts5HighlightFunction(
217404 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
217405 Fts5Context *pFts, /* First arg to pass to pApi functions */
217406 sqlite3_context *pCtx, /* Context for returning result/error */
217407 int nVal, /* Number of values in apVal[] array */
217408 sqlite3_value **apVal /* Array of trailing arguments */
217409){
217410 HighlightContext ctx;
217411 int rc;
217412 int iCol;
217413
217414 if( nVal!=3 ){
217415 const char *zErr = "wrong number of arguments to function highlight()";
217416 sqlite3_result_error(pCtx, z: zErr, n: -1);
217417 return;
217418 }
217419
217420 iCol = sqlite3_value_int(pVal: apVal[0]);
217421 memset(s: &ctx, c: 0, n: sizeof(HighlightContext));
217422 ctx.zOpen = (const char*)sqlite3_value_text(pVal: apVal[1]);
217423 ctx.zClose = (const char*)sqlite3_value_text(pVal: apVal[2]);
217424 rc = pApi->xColumnText(pFts, iCol, &ctx.zIn, &ctx.nIn);
217425
217426 if( ctx.zIn ){
217427 if( rc==SQLITE_OK ){
217428 rc = fts5CInstIterInit(pApi, pFts, iCol, pIter: &ctx.iter);
217429 }
217430
217431 if( rc==SQLITE_OK ){
217432 rc = pApi->xTokenize(pFts, ctx.zIn, ctx.nIn, (void*)&ctx,fts5HighlightCb);
217433 }
217434 fts5HighlightAppend(pRc: &rc, p: &ctx, z: &ctx.zIn[ctx.iOff], n: ctx.nIn - ctx.iOff);
217435
217436 if( rc==SQLITE_OK ){
217437 sqlite3_result_text(pCtx, z: (const char*)ctx.zOut, n: -1, SQLITE_TRANSIENT);
217438 }
217439 sqlite3_free(p: ctx.zOut);
217440 }
217441 if( rc!=SQLITE_OK ){
217442 sqlite3_result_error_code(pCtx, errCode: rc);
217443 }
217444}
217445/*
217446** End of highlight() implementation.
217447**************************************************************************/
217448
217449/*
217450** Context object passed to the fts5SentenceFinderCb() function.
217451*/
217452typedef struct Fts5SFinder Fts5SFinder;
217453struct Fts5SFinder {
217454 int iPos; /* Current token position */
217455 int nFirstAlloc; /* Allocated size of aFirst[] */
217456 int nFirst; /* Number of entries in aFirst[] */
217457 int *aFirst; /* Array of first token in each sentence */
217458 const char *zDoc; /* Document being tokenized */
217459};
217460
217461/*
217462** Add an entry to the Fts5SFinder.aFirst[] array. Grow the array if
217463** necessary. Return SQLITE_OK if successful, or SQLITE_NOMEM if an
217464** error occurs.
217465*/
217466static int fts5SentenceFinderAdd(Fts5SFinder *p, int iAdd){
217467 if( p->nFirstAlloc==p->nFirst ){
217468 int nNew = p->nFirstAlloc ? p->nFirstAlloc*2 : 64;
217469 int *aNew;
217470
217471 aNew = (int*)sqlite3_realloc64(pOld: p->aFirst, n: nNew*sizeof(int));
217472 if( aNew==0 ) return SQLITE_NOMEM;
217473 p->aFirst = aNew;
217474 p->nFirstAlloc = nNew;
217475 }
217476 p->aFirst[p->nFirst++] = iAdd;
217477 return SQLITE_OK;
217478}
217479
217480/*
217481** This function is an xTokenize() callback used by the auxiliary snippet()
217482** function. Its job is to identify tokens that are the first in a sentence.
217483** For each such token, an entry is added to the SFinder.aFirst[] array.
217484*/
217485static int fts5SentenceFinderCb(
217486 void *pContext, /* Pointer to HighlightContext object */
217487 int tflags, /* Mask of FTS5_TOKEN_* flags */
217488 const char *pToken, /* Buffer containing token */
217489 int nToken, /* Size of token in bytes */
217490 int iStartOff, /* Start offset of token */
217491 int iEndOff /* End offset of token */
217492){
217493 int rc = SQLITE_OK;
217494
217495 UNUSED_PARAM2(pToken, nToken);
217496 UNUSED_PARAM(iEndOff);
217497
217498 if( (tflags & FTS5_TOKEN_COLOCATED)==0 ){
217499 Fts5SFinder *p = (Fts5SFinder*)pContext;
217500 if( p->iPos>0 ){
217501 int i;
217502 char c = 0;
217503 for(i=iStartOff-1; i>=0; i--){
217504 c = p->zDoc[i];
217505 if( c!=' ' && c!='\t' && c!='\n' && c!='\r' ) break;
217506 }
217507 if( i!=iStartOff-1 && (c=='.' || c==':') ){
217508 rc = fts5SentenceFinderAdd(p, iAdd: p->iPos);
217509 }
217510 }else{
217511 rc = fts5SentenceFinderAdd(p, iAdd: 0);
217512 }
217513 p->iPos++;
217514 }
217515 return rc;
217516}
217517
217518static int fts5SnippetScore(
217519 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
217520 Fts5Context *pFts, /* First arg to pass to pApi functions */
217521 int nDocsize, /* Size of column in tokens */
217522 unsigned char *aSeen, /* Array with one element per query phrase */
217523 int iCol, /* Column to score */
217524 int iPos, /* Starting offset to score */
217525 int nToken, /* Max tokens per snippet */
217526 int *pnScore, /* OUT: Score */
217527 int *piPos /* OUT: Adjusted offset */
217528){
217529 int rc;
217530 int i;
217531 int ip = 0;
217532 int ic = 0;
217533 int iOff = 0;
217534 int iFirst = -1;
217535 int nInst;
217536 int nScore = 0;
217537 int iLast = 0;
217538 sqlite3_int64 iEnd = (sqlite3_int64)iPos + nToken;
217539
217540 rc = pApi->xInstCount(pFts, &nInst);
217541 for(i=0; i<nInst && rc==SQLITE_OK; i++){
217542 rc = pApi->xInst(pFts, i, &ip, &ic, &iOff);
217543 if( rc==SQLITE_OK && ic==iCol && iOff>=iPos && iOff<iEnd ){
217544 nScore += (aSeen[ip] ? 1 : 1000);
217545 aSeen[ip] = 1;
217546 if( iFirst<0 ) iFirst = iOff;
217547 iLast = iOff + pApi->xPhraseSize(pFts, ip);
217548 }
217549 }
217550
217551 *pnScore = nScore;
217552 if( piPos ){
217553 sqlite3_int64 iAdj = iFirst - (nToken - (iLast-iFirst)) / 2;
217554 if( (iAdj+nToken)>nDocsize ) iAdj = nDocsize - nToken;
217555 if( iAdj<0 ) iAdj = 0;
217556 *piPos = (int)iAdj;
217557 }
217558
217559 return rc;
217560}
217561
217562/*
217563** Return the value in pVal interpreted as utf-8 text. Except, if pVal
217564** contains a NULL value, return a pointer to a static string zero
217565** bytes in length instead of a NULL pointer.
217566*/
217567static const char *fts5ValueToText(sqlite3_value *pVal){
217568 const char *zRet = (const char*)sqlite3_value_text(pVal);
217569 return zRet ? zRet : "";
217570}
217571
217572/*
217573** Implementation of snippet() function.
217574*/
217575static void fts5SnippetFunction(
217576 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
217577 Fts5Context *pFts, /* First arg to pass to pApi functions */
217578 sqlite3_context *pCtx, /* Context for returning result/error */
217579 int nVal, /* Number of values in apVal[] array */
217580 sqlite3_value **apVal /* Array of trailing arguments */
217581){
217582 HighlightContext ctx;
217583 int rc = SQLITE_OK; /* Return code */
217584 int iCol; /* 1st argument to snippet() */
217585 const char *zEllips; /* 4th argument to snippet() */
217586 int nToken; /* 5th argument to snippet() */
217587 int nInst = 0; /* Number of instance matches this row */
217588 int i; /* Used to iterate through instances */
217589 int nPhrase; /* Number of phrases in query */
217590 unsigned char *aSeen; /* Array of "seen instance" flags */
217591 int iBestCol; /* Column containing best snippet */
217592 int iBestStart = 0; /* First token of best snippet */
217593 int nBestScore = 0; /* Score of best snippet */
217594 int nColSize = 0; /* Total size of iBestCol in tokens */
217595 Fts5SFinder sFinder; /* Used to find the beginnings of sentences */
217596 int nCol;
217597
217598 if( nVal!=5 ){
217599 const char *zErr = "wrong number of arguments to function snippet()";
217600 sqlite3_result_error(pCtx, z: zErr, n: -1);
217601 return;
217602 }
217603
217604 nCol = pApi->xColumnCount(pFts);
217605 memset(s: &ctx, c: 0, n: sizeof(HighlightContext));
217606 iCol = sqlite3_value_int(pVal: apVal[0]);
217607 ctx.zOpen = fts5ValueToText(pVal: apVal[1]);
217608 ctx.zClose = fts5ValueToText(pVal: apVal[2]);
217609 zEllips = fts5ValueToText(pVal: apVal[3]);
217610 nToken = sqlite3_value_int(pVal: apVal[4]);
217611
217612 iBestCol = (iCol>=0 ? iCol : 0);
217613 nPhrase = pApi->xPhraseCount(pFts);
217614 aSeen = sqlite3_malloc(n: nPhrase);
217615 if( aSeen==0 ){
217616 rc = SQLITE_NOMEM;
217617 }
217618 if( rc==SQLITE_OK ){
217619 rc = pApi->xInstCount(pFts, &nInst);
217620 }
217621
217622 memset(s: &sFinder, c: 0, n: sizeof(Fts5SFinder));
217623 for(i=0; i<nCol; i++){
217624 if( iCol<0 || iCol==i ){
217625 int nDoc;
217626 int nDocsize;
217627 int ii;
217628 sFinder.iPos = 0;
217629 sFinder.nFirst = 0;
217630 rc = pApi->xColumnText(pFts, i, &sFinder.zDoc, &nDoc);
217631 if( rc!=SQLITE_OK ) break;
217632 rc = pApi->xTokenize(pFts,
217633 sFinder.zDoc, nDoc, (void*)&sFinder,fts5SentenceFinderCb
217634 );
217635 if( rc!=SQLITE_OK ) break;
217636 rc = pApi->xColumnSize(pFts, i, &nDocsize);
217637 if( rc!=SQLITE_OK ) break;
217638
217639 for(ii=0; rc==SQLITE_OK && ii<nInst; ii++){
217640 int ip, ic, io;
217641 int iAdj;
217642 int nScore;
217643 int jj;
217644
217645 rc = pApi->xInst(pFts, ii, &ip, &ic, &io);
217646 if( ic!=i ) continue;
217647 if( io>nDocsize ) rc = FTS5_CORRUPT;
217648 if( rc!=SQLITE_OK ) continue;
217649 memset(s: aSeen, c: 0, n: nPhrase);
217650 rc = fts5SnippetScore(pApi, pFts, nDocsize, aSeen, iCol: i,
217651 iPos: io, nToken, pnScore: &nScore, piPos: &iAdj
217652 );
217653 if( rc==SQLITE_OK && nScore>nBestScore ){
217654 nBestScore = nScore;
217655 iBestCol = i;
217656 iBestStart = iAdj;
217657 nColSize = nDocsize;
217658 }
217659
217660 if( rc==SQLITE_OK && sFinder.nFirst && nDocsize>nToken ){
217661 for(jj=0; jj<(sFinder.nFirst-1); jj++){
217662 if( sFinder.aFirst[jj+1]>io ) break;
217663 }
217664
217665 if( sFinder.aFirst[jj]<io ){
217666 memset(s: aSeen, c: 0, n: nPhrase);
217667 rc = fts5SnippetScore(pApi, pFts, nDocsize, aSeen, iCol: i,
217668 iPos: sFinder.aFirst[jj], nToken, pnScore: &nScore, piPos: 0
217669 );
217670
217671 nScore += (sFinder.aFirst[jj]==0 ? 120 : 100);
217672 if( rc==SQLITE_OK && nScore>nBestScore ){
217673 nBestScore = nScore;
217674 iBestCol = i;
217675 iBestStart = sFinder.aFirst[jj];
217676 nColSize = nDocsize;
217677 }
217678 }
217679 }
217680 }
217681 }
217682 }
217683
217684 if( rc==SQLITE_OK ){
217685 rc = pApi->xColumnText(pFts, iBestCol, &ctx.zIn, &ctx.nIn);
217686 }
217687 if( rc==SQLITE_OK && nColSize==0 ){
217688 rc = pApi->xColumnSize(pFts, iBestCol, &nColSize);
217689 }
217690 if( ctx.zIn ){
217691 if( rc==SQLITE_OK ){
217692 rc = fts5CInstIterInit(pApi, pFts, iCol: iBestCol, pIter: &ctx.iter);
217693 }
217694
217695 ctx.iRangeStart = iBestStart;
217696 ctx.iRangeEnd = iBestStart + nToken - 1;
217697
217698 if( iBestStart>0 ){
217699 fts5HighlightAppend(pRc: &rc, p: &ctx, z: zEllips, n: -1);
217700 }
217701
217702 /* Advance iterator ctx.iter so that it points to the first coalesced
217703 ** phrase instance at or following position iBestStart. */
217704 while( ctx.iter.iStart>=0 && ctx.iter.iStart<iBestStart && rc==SQLITE_OK ){
217705 rc = fts5CInstIterNext(pIter: &ctx.iter);
217706 }
217707
217708 if( rc==SQLITE_OK ){
217709 rc = pApi->xTokenize(pFts, ctx.zIn, ctx.nIn, (void*)&ctx,fts5HighlightCb);
217710 }
217711 if( ctx.iRangeEnd>=(nColSize-1) ){
217712 fts5HighlightAppend(pRc: &rc, p: &ctx, z: &ctx.zIn[ctx.iOff], n: ctx.nIn - ctx.iOff);
217713 }else{
217714 fts5HighlightAppend(pRc: &rc, p: &ctx, z: zEllips, n: -1);
217715 }
217716 }
217717 if( rc==SQLITE_OK ){
217718 sqlite3_result_text(pCtx, z: (const char*)ctx.zOut, n: -1, SQLITE_TRANSIENT);
217719 }else{
217720 sqlite3_result_error_code(pCtx, errCode: rc);
217721 }
217722 sqlite3_free(p: ctx.zOut);
217723 sqlite3_free(p: aSeen);
217724 sqlite3_free(p: sFinder.aFirst);
217725}
217726
217727/************************************************************************/
217728
217729/*
217730** The first time the bm25() function is called for a query, an instance
217731** of the following structure is allocated and populated.
217732*/
217733typedef struct Fts5Bm25Data Fts5Bm25Data;
217734struct Fts5Bm25Data {
217735 int nPhrase; /* Number of phrases in query */
217736 double avgdl; /* Average number of tokens in each row */
217737 double *aIDF; /* IDF for each phrase */
217738 double *aFreq; /* Array used to calculate phrase freq. */
217739};
217740
217741/*
217742** Callback used by fts5Bm25GetData() to count the number of rows in the
217743** table matched by each individual phrase within the query.
217744*/
217745static int fts5CountCb(
217746 const Fts5ExtensionApi *pApi,
217747 Fts5Context *pFts,
217748 void *pUserData /* Pointer to sqlite3_int64 variable */
217749){
217750 sqlite3_int64 *pn = (sqlite3_int64*)pUserData;
217751 UNUSED_PARAM2(pApi, pFts);
217752 (*pn)++;
217753 return SQLITE_OK;
217754}
217755
217756/*
217757** Set *ppData to point to the Fts5Bm25Data object for the current query.
217758** If the object has not already been allocated, allocate and populate it
217759** now.
217760*/
217761static int fts5Bm25GetData(
217762 const Fts5ExtensionApi *pApi,
217763 Fts5Context *pFts,
217764 Fts5Bm25Data **ppData /* OUT: bm25-data object for this query */
217765){
217766 int rc = SQLITE_OK; /* Return code */
217767 Fts5Bm25Data *p; /* Object to return */
217768
217769 p = (Fts5Bm25Data*)pApi->xGetAuxdata(pFts, 0);
217770 if( p==0 ){
217771 int nPhrase; /* Number of phrases in query */
217772 sqlite3_int64 nRow = 0; /* Number of rows in table */
217773 sqlite3_int64 nToken = 0; /* Number of tokens in table */
217774 sqlite3_int64 nByte; /* Bytes of space to allocate */
217775 int i;
217776
217777 /* Allocate the Fts5Bm25Data object */
217778 nPhrase = pApi->xPhraseCount(pFts);
217779 nByte = sizeof(Fts5Bm25Data) + nPhrase*2*sizeof(double);
217780 p = (Fts5Bm25Data*)sqlite3_malloc64(n: nByte);
217781 if( p==0 ){
217782 rc = SQLITE_NOMEM;
217783 }else{
217784 memset(s: p, c: 0, n: (size_t)nByte);
217785 p->nPhrase = nPhrase;
217786 p->aIDF = (double*)&p[1];
217787 p->aFreq = &p->aIDF[nPhrase];
217788 }
217789
217790 /* Calculate the average document length for this FTS5 table */
217791 if( rc==SQLITE_OK ) rc = pApi->xRowCount(pFts, &nRow);
217792 assert( rc!=SQLITE_OK || nRow>0 );
217793 if( rc==SQLITE_OK ) rc = pApi->xColumnTotalSize(pFts, -1, &nToken);
217794 if( rc==SQLITE_OK ) p->avgdl = (double)nToken / (double)nRow;
217795
217796 /* Calculate an IDF for each phrase in the query */
217797 for(i=0; rc==SQLITE_OK && i<nPhrase; i++){
217798 sqlite3_int64 nHit = 0;
217799 rc = pApi->xQueryPhrase(pFts, i, (void*)&nHit, fts5CountCb);
217800 if( rc==SQLITE_OK ){
217801 /* Calculate the IDF (Inverse Document Frequency) for phrase i.
217802 ** This is done using the standard BM25 formula as found on wikipedia:
217803 **
217804 ** IDF = log( (N - nHit + 0.5) / (nHit + 0.5) )
217805 **
217806 ** where "N" is the total number of documents in the set and nHit
217807 ** is the number that contain at least one instance of the phrase
217808 ** under consideration.
217809 **
217810 ** The problem with this is that if (N < 2*nHit), the IDF is
217811 ** negative. Which is undesirable. So the mimimum allowable IDF is
217812 ** (1e-6) - roughly the same as a term that appears in just over
217813 ** half of set of 5,000,000 documents. */
217814 double idf = log( x: (nRow - nHit + 0.5) / (nHit + 0.5) );
217815 if( idf<=0.0 ) idf = 1e-6;
217816 p->aIDF[i] = idf;
217817 }
217818 }
217819
217820 if( rc!=SQLITE_OK ){
217821 sqlite3_free(p);
217822 }else{
217823 rc = pApi->xSetAuxdata(pFts, p, sqlite3_free);
217824 }
217825 if( rc!=SQLITE_OK ) p = 0;
217826 }
217827 *ppData = p;
217828 return rc;
217829}
217830
217831/*
217832** Implementation of bm25() function.
217833*/
217834static void fts5Bm25Function(
217835 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
217836 Fts5Context *pFts, /* First arg to pass to pApi functions */
217837 sqlite3_context *pCtx, /* Context for returning result/error */
217838 int nVal, /* Number of values in apVal[] array */
217839 sqlite3_value **apVal /* Array of trailing arguments */
217840){
217841 const double k1 = 1.2; /* Constant "k1" from BM25 formula */
217842 const double b = 0.75; /* Constant "b" from BM25 formula */
217843 int rc; /* Error code */
217844 double score = 0.0; /* SQL function return value */
217845 Fts5Bm25Data *pData; /* Values allocated/calculated once only */
217846 int i; /* Iterator variable */
217847 int nInst = 0; /* Value returned by xInstCount() */
217848 double D = 0.0; /* Total number of tokens in row */
217849 double *aFreq = 0; /* Array of phrase freq. for current row */
217850
217851 /* Calculate the phrase frequency (symbol "f(qi,D)" in the documentation)
217852 ** for each phrase in the query for the current row. */
217853 rc = fts5Bm25GetData(pApi, pFts, ppData: &pData);
217854 if( rc==SQLITE_OK ){
217855 aFreq = pData->aFreq;
217856 memset(s: aFreq, c: 0, n: sizeof(double) * pData->nPhrase);
217857 rc = pApi->xInstCount(pFts, &nInst);
217858 }
217859 for(i=0; rc==SQLITE_OK && i<nInst; i++){
217860 int ip; int ic; int io;
217861 rc = pApi->xInst(pFts, i, &ip, &ic, &io);
217862 if( rc==SQLITE_OK ){
217863 double w = (nVal > ic) ? sqlite3_value_double(pVal: apVal[ic]) : 1.0;
217864 aFreq[ip] += w;
217865 }
217866 }
217867
217868 /* Figure out the total size of the current row in tokens. */
217869 if( rc==SQLITE_OK ){
217870 int nTok;
217871 rc = pApi->xColumnSize(pFts, -1, &nTok);
217872 D = (double)nTok;
217873 }
217874
217875 /* Determine and return the BM25 score for the current row. Or, if an
217876 ** error has occurred, throw an exception. */
217877 if( rc==SQLITE_OK ){
217878 for(i=0; i<pData->nPhrase; i++){
217879 score += pData->aIDF[i] * (
217880 ( aFreq[i] * (k1 + 1.0) ) /
217881 ( aFreq[i] + k1 * (1 - b + b * D / pData->avgdl) )
217882 );
217883 }
217884 sqlite3_result_double(pCtx, rVal: -1.0 * score);
217885 }else{
217886 sqlite3_result_error_code(pCtx, errCode: rc);
217887 }
217888}
217889
217890static int sqlite3Fts5AuxInit(fts5_api *pApi){
217891 struct Builtin {
217892 const char *zFunc; /* Function name (nul-terminated) */
217893 void *pUserData; /* User-data pointer */
217894 fts5_extension_function xFunc;/* Callback function */
217895 void (*xDestroy)(void*); /* Destructor function */
217896 } aBuiltin [] = {
217897 { "snippet", 0, fts5SnippetFunction, 0 },
217898 { "highlight", 0, fts5HighlightFunction, 0 },
217899 { "bm25", 0, fts5Bm25Function, 0 },
217900 };
217901 int rc = SQLITE_OK; /* Return code */
217902 int i; /* To iterate through builtin functions */
217903
217904 for(i=0; rc==SQLITE_OK && i<ArraySize(aBuiltin); i++){
217905 rc = pApi->xCreateFunction(pApi,
217906 aBuiltin[i].zFunc,
217907 aBuiltin[i].pUserData,
217908 aBuiltin[i].xFunc,
217909 aBuiltin[i].xDestroy
217910 );
217911 }
217912
217913 return rc;
217914}
217915
217916/*
217917** 2014 May 31
217918**
217919** The author disclaims copyright to this source code. In place of
217920** a legal notice, here is a blessing:
217921**
217922** May you do good and not evil.
217923** May you find forgiveness for yourself and forgive others.
217924** May you share freely, never taking more than you give.
217925**
217926******************************************************************************
217927*/
217928
217929
217930
217931/* #include "fts5Int.h" */
217932
217933static int sqlite3Fts5BufferSize(int *pRc, Fts5Buffer *pBuf, u32 nByte){
217934 if( (u32)pBuf->nSpace<nByte ){
217935 u64 nNew = pBuf->nSpace ? pBuf->nSpace : 64;
217936 u8 *pNew;
217937 while( nNew<nByte ){
217938 nNew = nNew * 2;
217939 }
217940 pNew = sqlite3_realloc64(pOld: pBuf->p, n: nNew);
217941 if( pNew==0 ){
217942 *pRc = SQLITE_NOMEM;
217943 return 1;
217944 }else{
217945 pBuf->nSpace = (int)nNew;
217946 pBuf->p = pNew;
217947 }
217948 }
217949 return 0;
217950}
217951
217952
217953/*
217954** Encode value iVal as an SQLite varint and append it to the buffer object
217955** pBuf. If an OOM error occurs, set the error code in p.
217956*/
217957static void sqlite3Fts5BufferAppendVarint(int *pRc, Fts5Buffer *pBuf, i64 iVal){
217958 if( fts5BufferGrow(pRc, pBuf, 9) ) return;
217959 pBuf->n += sqlite3Fts5PutVarint(p: &pBuf->p[pBuf->n], v: iVal);
217960}
217961
217962static void sqlite3Fts5Put32(u8 *aBuf, int iVal){
217963 aBuf[0] = (iVal>>24) & 0x00FF;
217964 aBuf[1] = (iVal>>16) & 0x00FF;
217965 aBuf[2] = (iVal>> 8) & 0x00FF;
217966 aBuf[3] = (iVal>> 0) & 0x00FF;
217967}
217968
217969static int sqlite3Fts5Get32(const u8 *aBuf){
217970 return (int)((((u32)aBuf[0])<<24) + (aBuf[1]<<16) + (aBuf[2]<<8) + aBuf[3]);
217971}
217972
217973/*
217974** Append buffer nData/pData to buffer pBuf. If an OOM error occurs, set
217975** the error code in p. If an error has already occurred when this function
217976** is called, it is a no-op.
217977*/
217978static void sqlite3Fts5BufferAppendBlob(
217979 int *pRc,
217980 Fts5Buffer *pBuf,
217981 u32 nData,
217982 const u8 *pData
217983){
217984 if( nData ){
217985 if( fts5BufferGrow(pRc, pBuf, nData) ) return;
217986 memcpy(dest: &pBuf->p[pBuf->n], src: pData, n: nData);
217987 pBuf->n += nData;
217988 }
217989}
217990
217991/*
217992** Append the nul-terminated string zStr to the buffer pBuf. This function
217993** ensures that the byte following the buffer data is set to 0x00, even
217994** though this byte is not included in the pBuf->n count.
217995*/
217996static void sqlite3Fts5BufferAppendString(
217997 int *pRc,
217998 Fts5Buffer *pBuf,
217999 const char *zStr
218000){
218001 int nStr = (int)strlen(s: zStr);
218002 sqlite3Fts5BufferAppendBlob(pRc, pBuf, nData: nStr+1, pData: (const u8*)zStr);
218003 pBuf->n--;
218004}
218005
218006/*
218007** Argument zFmt is a printf() style format string. This function performs
218008** the printf() style processing, then appends the results to buffer pBuf.
218009**
218010** Like sqlite3Fts5BufferAppendString(), this function ensures that the byte
218011** following the buffer data is set to 0x00, even though this byte is not
218012** included in the pBuf->n count.
218013*/
218014static void sqlite3Fts5BufferAppendPrintf(
218015 int *pRc,
218016 Fts5Buffer *pBuf,
218017 char *zFmt, ...
218018){
218019 if( *pRc==SQLITE_OK ){
218020 char *zTmp;
218021 va_list ap;
218022 va_start(ap, zFmt);
218023 zTmp = sqlite3_vmprintf(zFormat: zFmt, ap);
218024 va_end(ap);
218025
218026 if( zTmp==0 ){
218027 *pRc = SQLITE_NOMEM;
218028 }else{
218029 sqlite3Fts5BufferAppendString(pRc, pBuf, zStr: zTmp);
218030 sqlite3_free(p: zTmp);
218031 }
218032 }
218033}
218034
218035static char *sqlite3Fts5Mprintf(int *pRc, const char *zFmt, ...){
218036 char *zRet = 0;
218037 if( *pRc==SQLITE_OK ){
218038 va_list ap;
218039 va_start(ap, zFmt);
218040 zRet = sqlite3_vmprintf(zFormat: zFmt, ap);
218041 va_end(ap);
218042 if( zRet==0 ){
218043 *pRc = SQLITE_NOMEM;
218044 }
218045 }
218046 return zRet;
218047}
218048
218049
218050/*
218051** Free any buffer allocated by pBuf. Zero the structure before returning.
218052*/
218053static void sqlite3Fts5BufferFree(Fts5Buffer *pBuf){
218054 sqlite3_free(p: pBuf->p);
218055 memset(s: pBuf, c: 0, n: sizeof(Fts5Buffer));
218056}
218057
218058/*
218059** Zero the contents of the buffer object. But do not free the associated
218060** memory allocation.
218061*/
218062static void sqlite3Fts5BufferZero(Fts5Buffer *pBuf){
218063 pBuf->n = 0;
218064}
218065
218066/*
218067** Set the buffer to contain nData/pData. If an OOM error occurs, leave an
218068** the error code in p. If an error has already occurred when this function
218069** is called, it is a no-op.
218070*/
218071static void sqlite3Fts5BufferSet(
218072 int *pRc,
218073 Fts5Buffer *pBuf,
218074 int nData,
218075 const u8 *pData
218076){
218077 pBuf->n = 0;
218078 sqlite3Fts5BufferAppendBlob(pRc, pBuf, nData, pData);
218079}
218080
218081static int sqlite3Fts5PoslistNext64(
218082 const u8 *a, int n, /* Buffer containing poslist */
218083 int *pi, /* IN/OUT: Offset within a[] */
218084 i64 *piOff /* IN/OUT: Current offset */
218085){
218086 int i = *pi;
218087 if( i>=n ){
218088 /* EOF */
218089 *piOff = -1;
218090 return 1;
218091 }else{
218092 i64 iOff = *piOff;
218093 u32 iVal;
218094 fts5FastGetVarint32(a, i, iVal);
218095 if( iVal<=1 ){
218096 if( iVal==0 ){
218097 *pi = i;
218098 return 0;
218099 }
218100 fts5FastGetVarint32(a, i, iVal);
218101 iOff = ((i64)iVal) << 32;
218102 assert( iOff>=0 );
218103 fts5FastGetVarint32(a, i, iVal);
218104 if( iVal<2 ){
218105 /* This is a corrupt record. So stop parsing it here. */
218106 *piOff = -1;
218107 return 1;
218108 }
218109 *piOff = iOff + ((iVal-2) & 0x7FFFFFFF);
218110 }else{
218111 *piOff = (iOff & (i64)0x7FFFFFFF<<32)+((iOff + (iVal-2)) & 0x7FFFFFFF);
218112 }
218113 *pi = i;
218114 assert_nc( *piOff>=iOff );
218115 return 0;
218116 }
218117}
218118
218119
218120/*
218121** Advance the iterator object passed as the only argument. Return true
218122** if the iterator reaches EOF, or false otherwise.
218123*/
218124static int sqlite3Fts5PoslistReaderNext(Fts5PoslistReader *pIter){
218125 if( sqlite3Fts5PoslistNext64(a: pIter->a, n: pIter->n, pi: &pIter->i, piOff: &pIter->iPos) ){
218126 pIter->bEof = 1;
218127 }
218128 return pIter->bEof;
218129}
218130
218131static int sqlite3Fts5PoslistReaderInit(
218132 const u8 *a, int n, /* Poslist buffer to iterate through */
218133 Fts5PoslistReader *pIter /* Iterator object to initialize */
218134){
218135 memset(s: pIter, c: 0, n: sizeof(*pIter));
218136 pIter->a = a;
218137 pIter->n = n;
218138 sqlite3Fts5PoslistReaderNext(pIter);
218139 return pIter->bEof;
218140}
218141
218142/*
218143** Append position iPos to the position list being accumulated in buffer
218144** pBuf, which must be already be large enough to hold the new data.
218145** The previous position written to this list is *piPrev. *piPrev is set
218146** to iPos before returning.
218147*/
218148static void sqlite3Fts5PoslistSafeAppend(
218149 Fts5Buffer *pBuf,
218150 i64 *piPrev,
218151 i64 iPos
218152){
218153 if( iPos>=*piPrev ){
218154 static const i64 colmask = ((i64)(0x7FFFFFFF)) << 32;
218155 if( (iPos & colmask) != (*piPrev & colmask) ){
218156 pBuf->p[pBuf->n++] = 1;
218157 pBuf->n += sqlite3Fts5PutVarint(p: &pBuf->p[pBuf->n], v: (iPos>>32));
218158 *piPrev = (iPos & colmask);
218159 }
218160 pBuf->n += sqlite3Fts5PutVarint(p: &pBuf->p[pBuf->n], v: (iPos-*piPrev)+2);
218161 *piPrev = iPos;
218162 }
218163}
218164
218165static int sqlite3Fts5PoslistWriterAppend(
218166 Fts5Buffer *pBuf,
218167 Fts5PoslistWriter *pWriter,
218168 i64 iPos
218169){
218170 int rc = 0; /* Initialized only to suppress erroneous warning from Clang */
218171 if( fts5BufferGrow(&rc, pBuf, 5+5+5) ) return rc;
218172 sqlite3Fts5PoslistSafeAppend(pBuf, piPrev: &pWriter->iPrev, iPos);
218173 return SQLITE_OK;
218174}
218175
218176static void *sqlite3Fts5MallocZero(int *pRc, sqlite3_int64 nByte){
218177 void *pRet = 0;
218178 if( *pRc==SQLITE_OK ){
218179 pRet = sqlite3_malloc64(n: nByte);
218180 if( pRet==0 ){
218181 if( nByte>0 ) *pRc = SQLITE_NOMEM;
218182 }else{
218183 memset(s: pRet, c: 0, n: (size_t)nByte);
218184 }
218185 }
218186 return pRet;
218187}
218188
218189/*
218190** Return a nul-terminated copy of the string indicated by pIn. If nIn
218191** is non-negative, then it is the length of the string in bytes. Otherwise,
218192** the length of the string is determined using strlen().
218193**
218194** It is the responsibility of the caller to eventually free the returned
218195** buffer using sqlite3_free(). If an OOM error occurs, NULL is returned.
218196*/
218197static char *sqlite3Fts5Strndup(int *pRc, const char *pIn, int nIn){
218198 char *zRet = 0;
218199 if( *pRc==SQLITE_OK ){
218200 if( nIn<0 ){
218201 nIn = (int)strlen(s: pIn);
218202 }
218203 zRet = (char*)sqlite3_malloc(n: nIn+1);
218204 if( zRet ){
218205 memcpy(dest: zRet, src: pIn, n: nIn);
218206 zRet[nIn] = '\0';
218207 }else{
218208 *pRc = SQLITE_NOMEM;
218209 }
218210 }
218211 return zRet;
218212}
218213
218214
218215/*
218216** Return true if character 't' may be part of an FTS5 bareword, or false
218217** otherwise. Characters that may be part of barewords:
218218**
218219** * All non-ASCII characters,
218220** * The 52 upper and lower case ASCII characters, and
218221** * The 10 integer ASCII characters.
218222** * The underscore character "_" (0x5F).
218223** * The unicode "subsitute" character (0x1A).
218224*/
218225static int sqlite3Fts5IsBareword(char t){
218226 u8 aBareword[128] = {
218227 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 .. 0x0F */
218228 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, /* 0x10 .. 0x1F */
218229 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 .. 0x2F */
218230 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 0x30 .. 0x3F */
218231 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 .. 0x4F */
218232 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 0x50 .. 0x5F */
218233 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 .. 0x6F */
218234 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 /* 0x70 .. 0x7F */
218235 };
218236
218237 return (t & 0x80) || aBareword[(int)t];
218238}
218239
218240
218241/*************************************************************************
218242*/
218243typedef struct Fts5TermsetEntry Fts5TermsetEntry;
218244struct Fts5TermsetEntry {
218245 char *pTerm;
218246 int nTerm;
218247 int iIdx; /* Index (main or aPrefix[] entry) */
218248 Fts5TermsetEntry *pNext;
218249};
218250
218251struct Fts5Termset {
218252 Fts5TermsetEntry *apHash[512];
218253};
218254
218255static int sqlite3Fts5TermsetNew(Fts5Termset **pp){
218256 int rc = SQLITE_OK;
218257 *pp = sqlite3Fts5MallocZero(pRc: &rc, nByte: sizeof(Fts5Termset));
218258 return rc;
218259}
218260
218261static int sqlite3Fts5TermsetAdd(
218262 Fts5Termset *p,
218263 int iIdx,
218264 const char *pTerm, int nTerm,
218265 int *pbPresent
218266){
218267 int rc = SQLITE_OK;
218268 *pbPresent = 0;
218269 if( p ){
218270 int i;
218271 u32 hash = 13;
218272 Fts5TermsetEntry *pEntry;
218273
218274 /* Calculate a hash value for this term. This is the same hash checksum
218275 ** used by the fts5_hash.c module. This is not important for correct
218276 ** operation of the module, but is necessary to ensure that some tests
218277 ** designed to produce hash table collisions really do work. */
218278 for(i=nTerm-1; i>=0; i--){
218279 hash = (hash << 3) ^ hash ^ pTerm[i];
218280 }
218281 hash = (hash << 3) ^ hash ^ iIdx;
218282 hash = hash % ArraySize(p->apHash);
218283
218284 for(pEntry=p->apHash[hash]; pEntry; pEntry=pEntry->pNext){
218285 if( pEntry->iIdx==iIdx
218286 && pEntry->nTerm==nTerm
218287 && memcmp(s1: pEntry->pTerm, s2: pTerm, n: nTerm)==0
218288 ){
218289 *pbPresent = 1;
218290 break;
218291 }
218292 }
218293
218294 if( pEntry==0 ){
218295 pEntry = sqlite3Fts5MallocZero(pRc: &rc, nByte: sizeof(Fts5TermsetEntry) + nTerm);
218296 if( pEntry ){
218297 pEntry->pTerm = (char*)&pEntry[1];
218298 pEntry->nTerm = nTerm;
218299 pEntry->iIdx = iIdx;
218300 memcpy(dest: pEntry->pTerm, src: pTerm, n: nTerm);
218301 pEntry->pNext = p->apHash[hash];
218302 p->apHash[hash] = pEntry;
218303 }
218304 }
218305 }
218306
218307 return rc;
218308}
218309
218310static void sqlite3Fts5TermsetFree(Fts5Termset *p){
218311 if( p ){
218312 u32 i;
218313 for(i=0; i<ArraySize(p->apHash); i++){
218314 Fts5TermsetEntry *pEntry = p->apHash[i];
218315 while( pEntry ){
218316 Fts5TermsetEntry *pDel = pEntry;
218317 pEntry = pEntry->pNext;
218318 sqlite3_free(p: pDel);
218319 }
218320 }
218321 sqlite3_free(p);
218322 }
218323}
218324
218325/*
218326** 2014 Jun 09
218327**
218328** The author disclaims copyright to this source code. In place of
218329** a legal notice, here is a blessing:
218330**
218331** May you do good and not evil.
218332** May you find forgiveness for yourself and forgive others.
218333** May you share freely, never taking more than you give.
218334**
218335******************************************************************************
218336**
218337** This is an SQLite module implementing full-text search.
218338*/
218339
218340
218341/* #include "fts5Int.h" */
218342
218343#define FTS5_DEFAULT_PAGE_SIZE 4050
218344#define FTS5_DEFAULT_AUTOMERGE 4
218345#define FTS5_DEFAULT_USERMERGE 4
218346#define FTS5_DEFAULT_CRISISMERGE 16
218347#define FTS5_DEFAULT_HASHSIZE (1024*1024)
218348
218349/* Maximum allowed page size */
218350#define FTS5_MAX_PAGE_SIZE (64*1024)
218351
218352static int fts5_iswhitespace(char x){
218353 return (x==' ');
218354}
218355
218356static int fts5_isopenquote(char x){
218357 return (x=='"' || x=='\'' || x=='[' || x=='`');
218358}
218359
218360/*
218361** Argument pIn points to a character that is part of a nul-terminated
218362** string. Return a pointer to the first character following *pIn in
218363** the string that is not a white-space character.
218364*/
218365static const char *fts5ConfigSkipWhitespace(const char *pIn){
218366 const char *p = pIn;
218367 if( p ){
218368 while( fts5_iswhitespace(x: *p) ){ p++; }
218369 }
218370 return p;
218371}
218372
218373/*
218374** Argument pIn points to a character that is part of a nul-terminated
218375** string. Return a pointer to the first character following *pIn in
218376** the string that is not a "bareword" character.
218377*/
218378static const char *fts5ConfigSkipBareword(const char *pIn){
218379 const char *p = pIn;
218380 while ( sqlite3Fts5IsBareword(t: *p) ) p++;
218381 if( p==pIn ) p = 0;
218382 return p;
218383}
218384
218385static int fts5_isdigit(char a){
218386 return (a>='0' && a<='9');
218387}
218388
218389
218390
218391static const char *fts5ConfigSkipLiteral(const char *pIn){
218392 const char *p = pIn;
218393 switch( *p ){
218394 case 'n': case 'N':
218395 if( sqlite3_strnicmp(zLeft: "null", zRight: p, N: 4)==0 ){
218396 p = &p[4];
218397 }else{
218398 p = 0;
218399 }
218400 break;
218401
218402 case 'x': case 'X':
218403 p++;
218404 if( *p=='\'' ){
218405 p++;
218406 while( (*p>='a' && *p<='f')
218407 || (*p>='A' && *p<='F')
218408 || (*p>='0' && *p<='9')
218409 ){
218410 p++;
218411 }
218412 if( *p=='\'' && 0==((p-pIn)%2) ){
218413 p++;
218414 }else{
218415 p = 0;
218416 }
218417 }else{
218418 p = 0;
218419 }
218420 break;
218421
218422 case '\'':
218423 p++;
218424 while( p ){
218425 if( *p=='\'' ){
218426 p++;
218427 if( *p!='\'' ) break;
218428 }
218429 p++;
218430 if( *p==0 ) p = 0;
218431 }
218432 break;
218433
218434 default:
218435 /* maybe a number */
218436 if( *p=='+' || *p=='-' ) p++;
218437 while( fts5_isdigit(a: *p) ) p++;
218438
218439 /* At this point, if the literal was an integer, the parse is
218440 ** finished. Or, if it is a floating point value, it may continue
218441 ** with either a decimal point or an 'E' character. */
218442 if( *p=='.' && fts5_isdigit(a: p[1]) ){
218443 p += 2;
218444 while( fts5_isdigit(a: *p) ) p++;
218445 }
218446 if( p==pIn ) p = 0;
218447
218448 break;
218449 }
218450
218451 return p;
218452}
218453
218454/*
218455** The first character of the string pointed to by argument z is guaranteed
218456** to be an open-quote character (see function fts5_isopenquote()).
218457**
218458** This function searches for the corresponding close-quote character within
218459** the string and, if found, dequotes the string in place and adds a new
218460** nul-terminator byte.
218461**
218462** If the close-quote is found, the value returned is the byte offset of
218463** the character immediately following it. Or, if the close-quote is not
218464** found, -1 is returned. If -1 is returned, the buffer is left in an
218465** undefined state.
218466*/
218467static int fts5Dequote(char *z){
218468 char q;
218469 int iIn = 1;
218470 int iOut = 0;
218471 q = z[0];
218472
218473 /* Set stack variable q to the close-quote character */
218474 assert( q=='[' || q=='\'' || q=='"' || q=='`' );
218475 if( q=='[' ) q = ']';
218476
218477 while( z[iIn] ){
218478 if( z[iIn]==q ){
218479 if( z[iIn+1]!=q ){
218480 /* Character iIn was the close quote. */
218481 iIn++;
218482 break;
218483 }else{
218484 /* Character iIn and iIn+1 form an escaped quote character. Skip
218485 ** the input cursor past both and copy a single quote character
218486 ** to the output buffer. */
218487 iIn += 2;
218488 z[iOut++] = q;
218489 }
218490 }else{
218491 z[iOut++] = z[iIn++];
218492 }
218493 }
218494
218495 z[iOut] = '\0';
218496 return iIn;
218497}
218498
218499/*
218500** Convert an SQL-style quoted string into a normal string by removing
218501** the quote characters. The conversion is done in-place. If the
218502** input does not begin with a quote character, then this routine
218503** is a no-op.
218504**
218505** Examples:
218506**
218507** "abc" becomes abc
218508** 'xyz' becomes xyz
218509** [pqr] becomes pqr
218510** `mno` becomes mno
218511*/
218512static void sqlite3Fts5Dequote(char *z){
218513 char quote; /* Quote character (if any ) */
218514
218515 assert( 0==fts5_iswhitespace(z[0]) );
218516 quote = z[0];
218517 if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
218518 fts5Dequote(z);
218519 }
218520}
218521
218522
218523struct Fts5Enum {
218524 const char *zName;
218525 int eVal;
218526};
218527typedef struct Fts5Enum Fts5Enum;
218528
218529static int fts5ConfigSetEnum(
218530 const Fts5Enum *aEnum,
218531 const char *zEnum,
218532 int *peVal
218533){
218534 int nEnum = (int)strlen(s: zEnum);
218535 int i;
218536 int iVal = -1;
218537
218538 for(i=0; aEnum[i].zName; i++){
218539 if( sqlite3_strnicmp(zLeft: aEnum[i].zName, zRight: zEnum, N: nEnum)==0 ){
218540 if( iVal>=0 ) return SQLITE_ERROR;
218541 iVal = aEnum[i].eVal;
218542 }
218543 }
218544
218545 *peVal = iVal;
218546 return iVal<0 ? SQLITE_ERROR : SQLITE_OK;
218547}
218548
218549/*
218550** Parse a "special" CREATE VIRTUAL TABLE directive and update
218551** configuration object pConfig as appropriate.
218552**
218553** If successful, object pConfig is updated and SQLITE_OK returned. If
218554** an error occurs, an SQLite error code is returned and an error message
218555** may be left in *pzErr. It is the responsibility of the caller to
218556** eventually free any such error message using sqlite3_free().
218557*/
218558static int fts5ConfigParseSpecial(
218559 Fts5Global *pGlobal,
218560 Fts5Config *pConfig, /* Configuration object to update */
218561 const char *zCmd, /* Special command to parse */
218562 const char *zArg, /* Argument to parse */
218563 char **pzErr /* OUT: Error message */
218564){
218565 int rc = SQLITE_OK;
218566 int nCmd = (int)strlen(s: zCmd);
218567 if( sqlite3_strnicmp(zLeft: "prefix", zRight: zCmd, N: nCmd)==0 ){
218568 const int nByte = sizeof(int) * FTS5_MAX_PREFIX_INDEXES;
218569 const char *p;
218570 int bFirst = 1;
218571 if( pConfig->aPrefix==0 ){
218572 pConfig->aPrefix = sqlite3Fts5MallocZero(pRc: &rc, nByte);
218573 if( rc ) return rc;
218574 }
218575
218576 p = zArg;
218577 while( 1 ){
218578 int nPre = 0;
218579
218580 while( p[0]==' ' ) p++;
218581 if( bFirst==0 && p[0]==',' ){
218582 p++;
218583 while( p[0]==' ' ) p++;
218584 }else if( p[0]=='\0' ){
218585 break;
218586 }
218587 if( p[0]<'0' || p[0]>'9' ){
218588 *pzErr = sqlite3_mprintf(zFormat: "malformed prefix=... directive");
218589 rc = SQLITE_ERROR;
218590 break;
218591 }
218592
218593 if( pConfig->nPrefix==FTS5_MAX_PREFIX_INDEXES ){
218594 *pzErr = sqlite3_mprintf(
218595 zFormat: "too many prefix indexes (max %d)", FTS5_MAX_PREFIX_INDEXES
218596 );
218597 rc = SQLITE_ERROR;
218598 break;
218599 }
218600
218601 while( p[0]>='0' && p[0]<='9' && nPre<1000 ){
218602 nPre = nPre*10 + (p[0] - '0');
218603 p++;
218604 }
218605
218606 if( nPre<=0 || nPre>=1000 ){
218607 *pzErr = sqlite3_mprintf(zFormat: "prefix length out of range (max 999)");
218608 rc = SQLITE_ERROR;
218609 break;
218610 }
218611
218612 pConfig->aPrefix[pConfig->nPrefix] = nPre;
218613 pConfig->nPrefix++;
218614 bFirst = 0;
218615 }
218616 assert( pConfig->nPrefix<=FTS5_MAX_PREFIX_INDEXES );
218617 return rc;
218618 }
218619
218620 if( sqlite3_strnicmp(zLeft: "tokenize", zRight: zCmd, N: nCmd)==0 ){
218621 const char *p = (const char*)zArg;
218622 sqlite3_int64 nArg = strlen(s: zArg) + 1;
218623 char **azArg = sqlite3Fts5MallocZero(pRc: &rc, nByte: sizeof(char*) * nArg);
218624 char *pDel = sqlite3Fts5MallocZero(pRc: &rc, nByte: nArg * 2);
218625 char *pSpace = pDel;
218626
218627 if( azArg && pSpace ){
218628 if( pConfig->pTok ){
218629 *pzErr = sqlite3_mprintf(zFormat: "multiple tokenize=... directives");
218630 rc = SQLITE_ERROR;
218631 }else{
218632 for(nArg=0; p && *p; nArg++){
218633 const char *p2 = fts5ConfigSkipWhitespace(pIn: p);
218634 if( *p2=='\'' ){
218635 p = fts5ConfigSkipLiteral(pIn: p2);
218636 }else{
218637 p = fts5ConfigSkipBareword(pIn: p2);
218638 }
218639 if( p ){
218640 memcpy(dest: pSpace, src: p2, n: p-p2);
218641 azArg[nArg] = pSpace;
218642 sqlite3Fts5Dequote(z: pSpace);
218643 pSpace += (p - p2) + 1;
218644 p = fts5ConfigSkipWhitespace(pIn: p);
218645 }
218646 }
218647 if( p==0 ){
218648 *pzErr = sqlite3_mprintf(zFormat: "parse error in tokenize directive");
218649 rc = SQLITE_ERROR;
218650 }else{
218651 rc = sqlite3Fts5GetTokenizer(pGlobal,
218652 azArg: (const char**)azArg, nArg: (int)nArg, pConfig,
218653 pzErr
218654 );
218655 }
218656 }
218657 }
218658
218659 sqlite3_free(p: azArg);
218660 sqlite3_free(p: pDel);
218661 return rc;
218662 }
218663
218664 if( sqlite3_strnicmp(zLeft: "content", zRight: zCmd, N: nCmd)==0 ){
218665 if( pConfig->eContent!=FTS5_CONTENT_NORMAL ){
218666 *pzErr = sqlite3_mprintf(zFormat: "multiple content=... directives");
218667 rc = SQLITE_ERROR;
218668 }else{
218669 if( zArg[0] ){
218670 pConfig->eContent = FTS5_CONTENT_EXTERNAL;
218671 pConfig->zContent = sqlite3Fts5Mprintf(pRc: &rc, zFmt: "%Q.%Q", pConfig->zDb,zArg);
218672 }else{
218673 pConfig->eContent = FTS5_CONTENT_NONE;
218674 }
218675 }
218676 return rc;
218677 }
218678
218679 if( sqlite3_strnicmp(zLeft: "content_rowid", zRight: zCmd, N: nCmd)==0 ){
218680 if( pConfig->zContentRowid ){
218681 *pzErr = sqlite3_mprintf(zFormat: "multiple content_rowid=... directives");
218682 rc = SQLITE_ERROR;
218683 }else{
218684 pConfig->zContentRowid = sqlite3Fts5Strndup(pRc: &rc, pIn: zArg, nIn: -1);
218685 }
218686 return rc;
218687 }
218688
218689 if( sqlite3_strnicmp(zLeft: "columnsize", zRight: zCmd, N: nCmd)==0 ){
218690 if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1]!='\0' ){
218691 *pzErr = sqlite3_mprintf(zFormat: "malformed columnsize=... directive");
218692 rc = SQLITE_ERROR;
218693 }else{
218694 pConfig->bColumnsize = (zArg[0]=='1');
218695 }
218696 return rc;
218697 }
218698
218699 if( sqlite3_strnicmp(zLeft: "detail", zRight: zCmd, N: nCmd)==0 ){
218700 const Fts5Enum aDetail[] = {
218701 { "none", FTS5_DETAIL_NONE },
218702 { "full", FTS5_DETAIL_FULL },
218703 { "columns", FTS5_DETAIL_COLUMNS },
218704 { 0, 0 }
218705 };
218706
218707 if( (rc = fts5ConfigSetEnum(aEnum: aDetail, zEnum: zArg, peVal: &pConfig->eDetail)) ){
218708 *pzErr = sqlite3_mprintf(zFormat: "malformed detail=... directive");
218709 }
218710 return rc;
218711 }
218712
218713 *pzErr = sqlite3_mprintf(zFormat: "unrecognized option: \"%.*s\"", nCmd, zCmd);
218714 return SQLITE_ERROR;
218715}
218716
218717/*
218718** Allocate an instance of the default tokenizer ("simple") at
218719** Fts5Config.pTokenizer. Return SQLITE_OK if successful, or an SQLite error
218720** code if an error occurs.
218721*/
218722static int fts5ConfigDefaultTokenizer(Fts5Global *pGlobal, Fts5Config *pConfig){
218723 assert( pConfig->pTok==0 && pConfig->pTokApi==0 );
218724 return sqlite3Fts5GetTokenizer(pGlobal, azArg: 0, nArg: 0, pConfig, pzErr: 0);
218725}
218726
218727/*
218728** Gobble up the first bareword or quoted word from the input buffer zIn.
218729** Return a pointer to the character immediately following the last in
218730** the gobbled word if successful, or a NULL pointer otherwise (failed
218731** to find close-quote character).
218732**
218733** Before returning, set pzOut to point to a new buffer containing a
218734** nul-terminated, dequoted copy of the gobbled word. If the word was
218735** quoted, *pbQuoted is also set to 1 before returning.
218736**
218737** If *pRc is other than SQLITE_OK when this function is called, it is
218738** a no-op (NULL is returned). Otherwise, if an OOM occurs within this
218739** function, *pRc is set to SQLITE_NOMEM before returning. *pRc is *not*
218740** set if a parse error (failed to find close quote) occurs.
218741*/
218742static const char *fts5ConfigGobbleWord(
218743 int *pRc, /* IN/OUT: Error code */
218744 const char *zIn, /* Buffer to gobble string/bareword from */
218745 char **pzOut, /* OUT: malloc'd buffer containing str/bw */
218746 int *pbQuoted /* OUT: Set to true if dequoting required */
218747){
218748 const char *zRet = 0;
218749
218750 sqlite3_int64 nIn = strlen(s: zIn);
218751 char *zOut = sqlite3_malloc64(n: nIn+1);
218752
218753 assert( *pRc==SQLITE_OK );
218754 *pbQuoted = 0;
218755 *pzOut = 0;
218756
218757 if( zOut==0 ){
218758 *pRc = SQLITE_NOMEM;
218759 }else{
218760 memcpy(dest: zOut, src: zIn, n: (size_t)(nIn+1));
218761 if( fts5_isopenquote(x: zOut[0]) ){
218762 int ii = fts5Dequote(z: zOut);
218763 zRet = &zIn[ii];
218764 *pbQuoted = 1;
218765 }else{
218766 zRet = fts5ConfigSkipBareword(pIn: zIn);
218767 if( zRet ){
218768 zOut[zRet-zIn] = '\0';
218769 }
218770 }
218771 }
218772
218773 if( zRet==0 ){
218774 sqlite3_free(p: zOut);
218775 }else{
218776 *pzOut = zOut;
218777 }
218778
218779 return zRet;
218780}
218781
218782static int fts5ConfigParseColumn(
218783 Fts5Config *p,
218784 char *zCol,
218785 char *zArg,
218786 char **pzErr
218787){
218788 int rc = SQLITE_OK;
218789 if( 0==sqlite3_stricmp(zLeft: zCol, FTS5_RANK_NAME)
218790 || 0==sqlite3_stricmp(zLeft: zCol, FTS5_ROWID_NAME)
218791 ){
218792 *pzErr = sqlite3_mprintf(zFormat: "reserved fts5 column name: %s", zCol);
218793 rc = SQLITE_ERROR;
218794 }else if( zArg ){
218795 if( 0==sqlite3_stricmp(zLeft: zArg, zRight: "unindexed") ){
218796 p->abUnindexed[p->nCol] = 1;
218797 }else{
218798 *pzErr = sqlite3_mprintf(zFormat: "unrecognized column option: %s", zArg);
218799 rc = SQLITE_ERROR;
218800 }
218801 }
218802
218803 p->azCol[p->nCol++] = zCol;
218804 return rc;
218805}
218806
218807/*
218808** Populate the Fts5Config.zContentExprlist string.
218809*/
218810static int fts5ConfigMakeExprlist(Fts5Config *p){
218811 int i;
218812 int rc = SQLITE_OK;
218813 Fts5Buffer buf = {0, 0, 0};
218814
218815 sqlite3Fts5BufferAppendPrintf(pRc: &rc, pBuf: &buf, zFmt: "T.%Q", p->zContentRowid);
218816 if( p->eContent!=FTS5_CONTENT_NONE ){
218817 for(i=0; i<p->nCol; i++){
218818 if( p->eContent==FTS5_CONTENT_EXTERNAL ){
218819 sqlite3Fts5BufferAppendPrintf(pRc: &rc, pBuf: &buf, zFmt: ", T.%Q", p->azCol[i]);
218820 }else{
218821 sqlite3Fts5BufferAppendPrintf(pRc: &rc, pBuf: &buf, zFmt: ", T.c%d", i);
218822 }
218823 }
218824 }
218825
218826 assert( p->zContentExprlist==0 );
218827 p->zContentExprlist = (char*)buf.p;
218828 return rc;
218829}
218830
218831/*
218832** Arguments nArg/azArg contain the string arguments passed to the xCreate
218833** or xConnect method of the virtual table. This function attempts to
218834** allocate an instance of Fts5Config containing the results of parsing
218835** those arguments.
218836**
218837** If successful, SQLITE_OK is returned and *ppOut is set to point to the
218838** new Fts5Config object. If an error occurs, an SQLite error code is
218839** returned, *ppOut is set to NULL and an error message may be left in
218840** *pzErr. It is the responsibility of the caller to eventually free any
218841** such error message using sqlite3_free().
218842*/
218843static int sqlite3Fts5ConfigParse(
218844 Fts5Global *pGlobal,
218845 sqlite3 *db,
218846 int nArg, /* Number of arguments */
218847 const char **azArg, /* Array of nArg CREATE VIRTUAL TABLE args */
218848 Fts5Config **ppOut, /* OUT: Results of parse */
218849 char **pzErr /* OUT: Error message */
218850){
218851 int rc = SQLITE_OK; /* Return code */
218852 Fts5Config *pRet; /* New object to return */
218853 int i;
218854 sqlite3_int64 nByte;
218855
218856 *ppOut = pRet = (Fts5Config*)sqlite3_malloc(n: sizeof(Fts5Config));
218857 if( pRet==0 ) return SQLITE_NOMEM;
218858 memset(s: pRet, c: 0, n: sizeof(Fts5Config));
218859 pRet->db = db;
218860 pRet->iCookie = -1;
218861
218862 nByte = nArg * (sizeof(char*) + sizeof(u8));
218863 pRet->azCol = (char**)sqlite3Fts5MallocZero(pRc: &rc, nByte);
218864 pRet->abUnindexed = pRet->azCol ? (u8*)&pRet->azCol[nArg] : 0;
218865 pRet->zDb = sqlite3Fts5Strndup(pRc: &rc, pIn: azArg[1], nIn: -1);
218866 pRet->zName = sqlite3Fts5Strndup(pRc: &rc, pIn: azArg[2], nIn: -1);
218867 pRet->bColumnsize = 1;
218868 pRet->eDetail = FTS5_DETAIL_FULL;
218869#ifdef SQLITE_DEBUG
218870 pRet->bPrefixIndex = 1;
218871#endif
218872 if( rc==SQLITE_OK && sqlite3_stricmp(zLeft: pRet->zName, FTS5_RANK_NAME)==0 ){
218873 *pzErr = sqlite3_mprintf(zFormat: "reserved fts5 table name: %s", pRet->zName);
218874 rc = SQLITE_ERROR;
218875 }
218876
218877 for(i=3; rc==SQLITE_OK && i<nArg; i++){
218878 const char *zOrig = azArg[i];
218879 const char *z;
218880 char *zOne = 0;
218881 char *zTwo = 0;
218882 int bOption = 0;
218883 int bMustBeCol = 0;
218884
218885 z = fts5ConfigGobbleWord(pRc: &rc, zIn: zOrig, pzOut: &zOne, pbQuoted: &bMustBeCol);
218886 z = fts5ConfigSkipWhitespace(pIn: z);
218887 if( z && *z=='=' ){
218888 bOption = 1;
218889 assert( zOne!=0 );
218890 z++;
218891 if( bMustBeCol ) z = 0;
218892 }
218893 z = fts5ConfigSkipWhitespace(pIn: z);
218894 if( z && z[0] ){
218895 int bDummy;
218896 z = fts5ConfigGobbleWord(pRc: &rc, zIn: z, pzOut: &zTwo, pbQuoted: &bDummy);
218897 if( z && z[0] ) z = 0;
218898 }
218899
218900 if( rc==SQLITE_OK ){
218901 if( z==0 ){
218902 *pzErr = sqlite3_mprintf(zFormat: "parse error in \"%s\"", zOrig);
218903 rc = SQLITE_ERROR;
218904 }else{
218905 if( bOption ){
218906 rc = fts5ConfigParseSpecial(pGlobal, pConfig: pRet,
218907 ALWAYS(zOne)?zOne:"",
218908 zArg: zTwo?zTwo:"",
218909 pzErr
218910 );
218911 }else{
218912 rc = fts5ConfigParseColumn(p: pRet, zCol: zOne, zArg: zTwo, pzErr);
218913 zOne = 0;
218914 }
218915 }
218916 }
218917
218918 sqlite3_free(p: zOne);
218919 sqlite3_free(p: zTwo);
218920 }
218921
218922 /* If a tokenizer= option was successfully parsed, the tokenizer has
218923 ** already been allocated. Otherwise, allocate an instance of the default
218924 ** tokenizer (unicode61) now. */
218925 if( rc==SQLITE_OK && pRet->pTok==0 ){
218926 rc = fts5ConfigDefaultTokenizer(pGlobal, pConfig: pRet);
218927 }
218928
218929 /* If no zContent option was specified, fill in the default values. */
218930 if( rc==SQLITE_OK && pRet->zContent==0 ){
218931 const char *zTail = 0;
218932 assert( pRet->eContent==FTS5_CONTENT_NORMAL
218933 || pRet->eContent==FTS5_CONTENT_NONE
218934 );
218935 if( pRet->eContent==FTS5_CONTENT_NORMAL ){
218936 zTail = "content";
218937 }else if( pRet->bColumnsize ){
218938 zTail = "docsize";
218939 }
218940
218941 if( zTail ){
218942 pRet->zContent = sqlite3Fts5Mprintf(
218943 pRc: &rc, zFmt: "%Q.'%q_%s'", pRet->zDb, pRet->zName, zTail
218944 );
218945 }
218946 }
218947
218948 if( rc==SQLITE_OK && pRet->zContentRowid==0 ){
218949 pRet->zContentRowid = sqlite3Fts5Strndup(pRc: &rc, pIn: "rowid", nIn: -1);
218950 }
218951
218952 /* Formulate the zContentExprlist text */
218953 if( rc==SQLITE_OK ){
218954 rc = fts5ConfigMakeExprlist(p: pRet);
218955 }
218956
218957 if( rc!=SQLITE_OK ){
218958 sqlite3Fts5ConfigFree(pRet);
218959 *ppOut = 0;
218960 }
218961 return rc;
218962}
218963
218964/*
218965** Free the configuration object passed as the only argument.
218966*/
218967static void sqlite3Fts5ConfigFree(Fts5Config *pConfig){
218968 if( pConfig ){
218969 int i;
218970 if( pConfig->pTok ){
218971 pConfig->pTokApi->xDelete(pConfig->pTok);
218972 }
218973 sqlite3_free(p: pConfig->zDb);
218974 sqlite3_free(p: pConfig->zName);
218975 for(i=0; i<pConfig->nCol; i++){
218976 sqlite3_free(p: pConfig->azCol[i]);
218977 }
218978 sqlite3_free(p: pConfig->azCol);
218979 sqlite3_free(p: pConfig->aPrefix);
218980 sqlite3_free(p: pConfig->zRank);
218981 sqlite3_free(p: pConfig->zRankArgs);
218982 sqlite3_free(p: pConfig->zContent);
218983 sqlite3_free(p: pConfig->zContentRowid);
218984 sqlite3_free(p: pConfig->zContentExprlist);
218985 sqlite3_free(p: pConfig);
218986 }
218987}
218988
218989/*
218990** Call sqlite3_declare_vtab() based on the contents of the configuration
218991** object passed as the only argument. Return SQLITE_OK if successful, or
218992** an SQLite error code if an error occurs.
218993*/
218994static int sqlite3Fts5ConfigDeclareVtab(Fts5Config *pConfig){
218995 int i;
218996 int rc = SQLITE_OK;
218997 char *zSql;
218998
218999 zSql = sqlite3Fts5Mprintf(pRc: &rc, zFmt: "CREATE TABLE x(");
219000 for(i=0; zSql && i<pConfig->nCol; i++){
219001 const char *zSep = (i==0?"":", ");
219002 zSql = sqlite3Fts5Mprintf(pRc: &rc, zFmt: "%z%s%Q", zSql, zSep, pConfig->azCol[i]);
219003 }
219004 zSql = sqlite3Fts5Mprintf(pRc: &rc, zFmt: "%z, %Q HIDDEN, %s HIDDEN)",
219005 zSql, pConfig->zName, FTS5_RANK_NAME
219006 );
219007
219008 assert( zSql || rc==SQLITE_NOMEM );
219009 if( zSql ){
219010 rc = sqlite3_declare_vtab(db: pConfig->db, zCreateTable: zSql);
219011 sqlite3_free(p: zSql);
219012 }
219013
219014 return rc;
219015}
219016
219017/*
219018** Tokenize the text passed via the second and third arguments.
219019**
219020** The callback is invoked once for each token in the input text. The
219021** arguments passed to it are, in order:
219022**
219023** void *pCtx // Copy of 4th argument to sqlite3Fts5Tokenize()
219024** const char *pToken // Pointer to buffer containing token
219025** int nToken // Size of token in bytes
219026** int iStart // Byte offset of start of token within input text
219027** int iEnd // Byte offset of end of token within input text
219028** int iPos // Position of token in input (first token is 0)
219029**
219030** If the callback returns a non-zero value the tokenization is abandoned
219031** and no further callbacks are issued.
219032**
219033** This function returns SQLITE_OK if successful or an SQLite error code
219034** if an error occurs. If the tokenization was abandoned early because
219035** the callback returned SQLITE_DONE, this is not an error and this function
219036** still returns SQLITE_OK. Or, if the tokenization was abandoned early
219037** because the callback returned another non-zero value, it is assumed
219038** to be an SQLite error code and returned to the caller.
219039*/
219040static int sqlite3Fts5Tokenize(
219041 Fts5Config *pConfig, /* FTS5 Configuration object */
219042 int flags, /* FTS5_TOKENIZE_* flags */
219043 const char *pText, int nText, /* Text to tokenize */
219044 void *pCtx, /* Context passed to xToken() */
219045 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
219046){
219047 if( pText==0 ) return SQLITE_OK;
219048 return pConfig->pTokApi->xTokenize(
219049 pConfig->pTok, pCtx, flags, pText, nText, xToken
219050 );
219051}
219052
219053/*
219054** Argument pIn points to the first character in what is expected to be
219055** a comma-separated list of SQL literals followed by a ')' character.
219056** If it actually is this, return a pointer to the ')'. Otherwise, return
219057** NULL to indicate a parse error.
219058*/
219059static const char *fts5ConfigSkipArgs(const char *pIn){
219060 const char *p = pIn;
219061
219062 while( 1 ){
219063 p = fts5ConfigSkipWhitespace(pIn: p);
219064 p = fts5ConfigSkipLiteral(pIn: p);
219065 p = fts5ConfigSkipWhitespace(pIn: p);
219066 if( p==0 || *p==')' ) break;
219067 if( *p!=',' ){
219068 p = 0;
219069 break;
219070 }
219071 p++;
219072 }
219073
219074 return p;
219075}
219076
219077/*
219078** Parameter zIn contains a rank() function specification. The format of
219079** this is:
219080**
219081** + Bareword (function name)
219082** + Open parenthesis - "("
219083** + Zero or more SQL literals in a comma separated list
219084** + Close parenthesis - ")"
219085*/
219086static int sqlite3Fts5ConfigParseRank(
219087 const char *zIn, /* Input string */
219088 char **pzRank, /* OUT: Rank function name */
219089 char **pzRankArgs /* OUT: Rank function arguments */
219090){
219091 const char *p = zIn;
219092 const char *pRank;
219093 char *zRank = 0;
219094 char *zRankArgs = 0;
219095 int rc = SQLITE_OK;
219096
219097 *pzRank = 0;
219098 *pzRankArgs = 0;
219099
219100 if( p==0 ){
219101 rc = SQLITE_ERROR;
219102 }else{
219103 p = fts5ConfigSkipWhitespace(pIn: p);
219104 pRank = p;
219105 p = fts5ConfigSkipBareword(pIn: p);
219106
219107 if( p ){
219108 zRank = sqlite3Fts5MallocZero(pRc: &rc, nByte: 1 + p - pRank);
219109 if( zRank ) memcpy(dest: zRank, src: pRank, n: p-pRank);
219110 }else{
219111 rc = SQLITE_ERROR;
219112 }
219113
219114 if( rc==SQLITE_OK ){
219115 p = fts5ConfigSkipWhitespace(pIn: p);
219116 if( *p!='(' ) rc = SQLITE_ERROR;
219117 p++;
219118 }
219119 if( rc==SQLITE_OK ){
219120 const char *pArgs;
219121 p = fts5ConfigSkipWhitespace(pIn: p);
219122 pArgs = p;
219123 if( *p!=')' ){
219124 p = fts5ConfigSkipArgs(pIn: p);
219125 if( p==0 ){
219126 rc = SQLITE_ERROR;
219127 }else{
219128 zRankArgs = sqlite3Fts5MallocZero(pRc: &rc, nByte: 1 + p - pArgs);
219129 if( zRankArgs ) memcpy(dest: zRankArgs, src: pArgs, n: p-pArgs);
219130 }
219131 }
219132 }
219133 }
219134
219135 if( rc!=SQLITE_OK ){
219136 sqlite3_free(p: zRank);
219137 assert( zRankArgs==0 );
219138 }else{
219139 *pzRank = zRank;
219140 *pzRankArgs = zRankArgs;
219141 }
219142 return rc;
219143}
219144
219145static int sqlite3Fts5ConfigSetValue(
219146 Fts5Config *pConfig,
219147 const char *zKey,
219148 sqlite3_value *pVal,
219149 int *pbBadkey
219150){
219151 int rc = SQLITE_OK;
219152
219153 if( 0==sqlite3_stricmp(zLeft: zKey, zRight: "pgsz") ){
219154 int pgsz = 0;
219155 if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
219156 pgsz = sqlite3_value_int(pVal);
219157 }
219158 if( pgsz<32 || pgsz>FTS5_MAX_PAGE_SIZE ){
219159 *pbBadkey = 1;
219160 }else{
219161 pConfig->pgsz = pgsz;
219162 }
219163 }
219164
219165 else if( 0==sqlite3_stricmp(zLeft: zKey, zRight: "hashsize") ){
219166 int nHashSize = -1;
219167 if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
219168 nHashSize = sqlite3_value_int(pVal);
219169 }
219170 if( nHashSize<=0 ){
219171 *pbBadkey = 1;
219172 }else{
219173 pConfig->nHashSize = nHashSize;
219174 }
219175 }
219176
219177 else if( 0==sqlite3_stricmp(zLeft: zKey, zRight: "automerge") ){
219178 int nAutomerge = -1;
219179 if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
219180 nAutomerge = sqlite3_value_int(pVal);
219181 }
219182 if( nAutomerge<0 || nAutomerge>64 ){
219183 *pbBadkey = 1;
219184 }else{
219185 if( nAutomerge==1 ) nAutomerge = FTS5_DEFAULT_AUTOMERGE;
219186 pConfig->nAutomerge = nAutomerge;
219187 }
219188 }
219189
219190 else if( 0==sqlite3_stricmp(zLeft: zKey, zRight: "usermerge") ){
219191 int nUsermerge = -1;
219192 if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
219193 nUsermerge = sqlite3_value_int(pVal);
219194 }
219195 if( nUsermerge<2 || nUsermerge>16 ){
219196 *pbBadkey = 1;
219197 }else{
219198 pConfig->nUsermerge = nUsermerge;
219199 }
219200 }
219201
219202 else if( 0==sqlite3_stricmp(zLeft: zKey, zRight: "crisismerge") ){
219203 int nCrisisMerge = -1;
219204 if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
219205 nCrisisMerge = sqlite3_value_int(pVal);
219206 }
219207 if( nCrisisMerge<0 ){
219208 *pbBadkey = 1;
219209 }else{
219210 if( nCrisisMerge<=1 ) nCrisisMerge = FTS5_DEFAULT_CRISISMERGE;
219211 if( nCrisisMerge>=FTS5_MAX_SEGMENT ) nCrisisMerge = FTS5_MAX_SEGMENT-1;
219212 pConfig->nCrisisMerge = nCrisisMerge;
219213 }
219214 }
219215
219216 else if( 0==sqlite3_stricmp(zLeft: zKey, zRight: "rank") ){
219217 const char *zIn = (const char*)sqlite3_value_text(pVal);
219218 char *zRank;
219219 char *zRankArgs;
219220 rc = sqlite3Fts5ConfigParseRank(zIn, pzRank: &zRank, pzRankArgs: &zRankArgs);
219221 if( rc==SQLITE_OK ){
219222 sqlite3_free(p: pConfig->zRank);
219223 sqlite3_free(p: pConfig->zRankArgs);
219224 pConfig->zRank = zRank;
219225 pConfig->zRankArgs = zRankArgs;
219226 }else if( rc==SQLITE_ERROR ){
219227 rc = SQLITE_OK;
219228 *pbBadkey = 1;
219229 }
219230 }else{
219231 *pbBadkey = 1;
219232 }
219233 return rc;
219234}
219235
219236/*
219237** Load the contents of the %_config table into memory.
219238*/
219239static int sqlite3Fts5ConfigLoad(Fts5Config *pConfig, int iCookie){
219240 const char *zSelect = "SELECT k, v FROM %Q.'%q_config'";
219241 char *zSql;
219242 sqlite3_stmt *p = 0;
219243 int rc = SQLITE_OK;
219244 int iVersion = 0;
219245
219246 /* Set default values */
219247 pConfig->pgsz = FTS5_DEFAULT_PAGE_SIZE;
219248 pConfig->nAutomerge = FTS5_DEFAULT_AUTOMERGE;
219249 pConfig->nUsermerge = FTS5_DEFAULT_USERMERGE;
219250 pConfig->nCrisisMerge = FTS5_DEFAULT_CRISISMERGE;
219251 pConfig->nHashSize = FTS5_DEFAULT_HASHSIZE;
219252
219253 zSql = sqlite3Fts5Mprintf(pRc: &rc, zFmt: zSelect, pConfig->zDb, pConfig->zName);
219254 if( zSql ){
219255 rc = sqlite3_prepare_v2(db: pConfig->db, zSql, nBytes: -1, ppStmt: &p, pzTail: 0);
219256 sqlite3_free(p: zSql);
219257 }
219258
219259 assert( rc==SQLITE_OK || p==0 );
219260 if( rc==SQLITE_OK ){
219261 while( SQLITE_ROW==sqlite3_step(pStmt: p) ){
219262 const char *zK = (const char*)sqlite3_column_text(pStmt: p, i: 0);
219263 sqlite3_value *pVal = sqlite3_column_value(pStmt: p, i: 1);
219264 if( 0==sqlite3_stricmp(zLeft: zK, zRight: "version") ){
219265 iVersion = sqlite3_value_int(pVal);
219266 }else{
219267 int bDummy = 0;
219268 sqlite3Fts5ConfigSetValue(pConfig, zKey: zK, pVal, pbBadkey: &bDummy);
219269 }
219270 }
219271 rc = sqlite3_finalize(pStmt: p);
219272 }
219273
219274 if( rc==SQLITE_OK && iVersion!=FTS5_CURRENT_VERSION ){
219275 rc = SQLITE_ERROR;
219276 if( pConfig->pzErrmsg ){
219277 assert( 0==*pConfig->pzErrmsg );
219278 *pConfig->pzErrmsg = sqlite3_mprintf(
219279 zFormat: "invalid fts5 file format (found %d, expected %d) - run 'rebuild'",
219280 iVersion, FTS5_CURRENT_VERSION
219281 );
219282 }
219283 }
219284
219285 if( rc==SQLITE_OK ){
219286 pConfig->iCookie = iCookie;
219287 }
219288 return rc;
219289}
219290
219291/*
219292** 2014 May 31
219293**
219294** The author disclaims copyright to this source code. In place of
219295** a legal notice, here is a blessing:
219296**
219297** May you do good and not evil.
219298** May you find forgiveness for yourself and forgive others.
219299** May you share freely, never taking more than you give.
219300**
219301******************************************************************************
219302**
219303*/
219304
219305
219306
219307/* #include "fts5Int.h" */
219308/* #include "fts5parse.h" */
219309
219310/*
219311** All token types in the generated fts5parse.h file are greater than 0.
219312*/
219313#define FTS5_EOF 0
219314
219315#define FTS5_LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
219316
219317typedef struct Fts5ExprTerm Fts5ExprTerm;
219318
219319/*
219320** Functions generated by lemon from fts5parse.y.
219321*/
219322static void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(u64));
219323static void sqlite3Fts5ParserFree(void*, void (*freeProc)(void*));
219324static void sqlite3Fts5Parser(void*, int, Fts5Token, Fts5Parse*);
219325#ifndef NDEBUG
219326/* #include <stdio.h> */
219327static void sqlite3Fts5ParserTrace(FILE*, char*);
219328#endif
219329static int sqlite3Fts5ParserFallback(int);
219330
219331
219332struct Fts5Expr {
219333 Fts5Index *pIndex;
219334 Fts5Config *pConfig;
219335 Fts5ExprNode *pRoot;
219336 int bDesc; /* Iterate in descending rowid order */
219337 int nPhrase; /* Number of phrases in expression */
219338 Fts5ExprPhrase **apExprPhrase; /* Pointers to phrase objects */
219339};
219340
219341/*
219342** eType:
219343** Expression node type. Always one of:
219344**
219345** FTS5_AND (nChild, apChild valid)
219346** FTS5_OR (nChild, apChild valid)
219347** FTS5_NOT (nChild, apChild valid)
219348** FTS5_STRING (pNear valid)
219349** FTS5_TERM (pNear valid)
219350*/
219351struct Fts5ExprNode {
219352 int eType; /* Node type */
219353 int bEof; /* True at EOF */
219354 int bNomatch; /* True if entry is not a match */
219355
219356 /* Next method for this node. */
219357 int (*xNext)(Fts5Expr*, Fts5ExprNode*, int, i64);
219358
219359 i64 iRowid; /* Current rowid */
219360 Fts5ExprNearset *pNear; /* For FTS5_STRING - cluster of phrases */
219361
219362 /* Child nodes. For a NOT node, this array always contains 2 entries. For
219363 ** AND or OR nodes, it contains 2 or more entries. */
219364 int nChild; /* Number of child nodes */
219365 Fts5ExprNode *apChild[1]; /* Array of child nodes */
219366};
219367
219368#define Fts5NodeIsString(p) ((p)->eType==FTS5_TERM || (p)->eType==FTS5_STRING)
219369
219370/*
219371** Invoke the xNext method of an Fts5ExprNode object. This macro should be
219372** used as if it has the same signature as the xNext() methods themselves.
219373*/
219374#define fts5ExprNodeNext(a,b,c,d) (b)->xNext((a), (b), (c), (d))
219375
219376/*
219377** An instance of the following structure represents a single search term
219378** or term prefix.
219379*/
219380struct Fts5ExprTerm {
219381 u8 bPrefix; /* True for a prefix term */
219382 u8 bFirst; /* True if token must be first in column */
219383 char *zTerm; /* nul-terminated term */
219384 Fts5IndexIter *pIter; /* Iterator for this term */
219385 Fts5ExprTerm *pSynonym; /* Pointer to first in list of synonyms */
219386};
219387
219388/*
219389** A phrase. One or more terms that must appear in a contiguous sequence
219390** within a document for it to match.
219391*/
219392struct Fts5ExprPhrase {
219393 Fts5ExprNode *pNode; /* FTS5_STRING node this phrase is part of */
219394 Fts5Buffer poslist; /* Current position list */
219395 int nTerm; /* Number of entries in aTerm[] */
219396 Fts5ExprTerm aTerm[1]; /* Terms that make up this phrase */
219397};
219398
219399/*
219400** One or more phrases that must appear within a certain token distance of
219401** each other within each matching document.
219402*/
219403struct Fts5ExprNearset {
219404 int nNear; /* NEAR parameter */
219405 Fts5Colset *pColset; /* Columns to search (NULL -> all columns) */
219406 int nPhrase; /* Number of entries in aPhrase[] array */
219407 Fts5ExprPhrase *apPhrase[1]; /* Array of phrase pointers */
219408};
219409
219410
219411/*
219412** Parse context.
219413*/
219414struct Fts5Parse {
219415 Fts5Config *pConfig;
219416 char *zErr;
219417 int rc;
219418 int nPhrase; /* Size of apPhrase array */
219419 Fts5ExprPhrase **apPhrase; /* Array of all phrases */
219420 Fts5ExprNode *pExpr; /* Result of a successful parse */
219421 int bPhraseToAnd; /* Convert "a+b" to "a AND b" */
219422};
219423
219424static void sqlite3Fts5ParseError(Fts5Parse *pParse, const char *zFmt, ...){
219425 va_list ap;
219426 va_start(ap, zFmt);
219427 if( pParse->rc==SQLITE_OK ){
219428 assert( pParse->zErr==0 );
219429 pParse->zErr = sqlite3_vmprintf(zFormat: zFmt, ap);
219430 pParse->rc = SQLITE_ERROR;
219431 }
219432 va_end(ap);
219433}
219434
219435static int fts5ExprIsspace(char t){
219436 return t==' ' || t=='\t' || t=='\n' || t=='\r';
219437}
219438
219439/*
219440** Read the first token from the nul-terminated string at *pz.
219441*/
219442static int fts5ExprGetToken(
219443 Fts5Parse *pParse,
219444 const char **pz, /* IN/OUT: Pointer into buffer */
219445 Fts5Token *pToken
219446){
219447 const char *z = *pz;
219448 int tok;
219449
219450 /* Skip past any whitespace */
219451 while( fts5ExprIsspace(t: *z) ) z++;
219452
219453 pToken->p = z;
219454 pToken->n = 1;
219455 switch( *z ){
219456 case '(': tok = FTS5_LP; break;
219457 case ')': tok = FTS5_RP; break;
219458 case '{': tok = FTS5_LCP; break;
219459 case '}': tok = FTS5_RCP; break;
219460 case ':': tok = FTS5_COLON; break;
219461 case ',': tok = FTS5_COMMA; break;
219462 case '+': tok = FTS5_PLUS; break;
219463 case '*': tok = FTS5_STAR; break;
219464 case '-': tok = FTS5_MINUS; break;
219465 case '^': tok = FTS5_CARET; break;
219466 case '\0': tok = FTS5_EOF; break;
219467
219468 case '"': {
219469 const char *z2;
219470 tok = FTS5_STRING;
219471
219472 for(z2=&z[1]; 1; z2++){
219473 if( z2[0]=='"' ){
219474 z2++;
219475 if( z2[0]!='"' ) break;
219476 }
219477 if( z2[0]=='\0' ){
219478 sqlite3Fts5ParseError(pParse, zFmt: "unterminated string");
219479 return FTS5_EOF;
219480 }
219481 }
219482 pToken->n = (z2 - z);
219483 break;
219484 }
219485
219486 default: {
219487 const char *z2;
219488 if( sqlite3Fts5IsBareword(t: z[0])==0 ){
219489 sqlite3Fts5ParseError(pParse, zFmt: "fts5: syntax error near \"%.1s\"", z);
219490 return FTS5_EOF;
219491 }
219492 tok = FTS5_STRING;
219493 for(z2=&z[1]; sqlite3Fts5IsBareword(t: *z2); z2++);
219494 pToken->n = (z2 - z);
219495 if( pToken->n==2 && memcmp(s1: pToken->p, s2: "OR", n: 2)==0 ) tok = FTS5_OR;
219496 if( pToken->n==3 && memcmp(s1: pToken->p, s2: "NOT", n: 3)==0 ) tok = FTS5_NOT;
219497 if( pToken->n==3 && memcmp(s1: pToken->p, s2: "AND", n: 3)==0 ) tok = FTS5_AND;
219498 break;
219499 }
219500 }
219501
219502 *pz = &pToken->p[pToken->n];
219503 return tok;
219504}
219505
219506static void *fts5ParseAlloc(u64 t){ return sqlite3_malloc64(n: (sqlite3_int64)t);}
219507static void fts5ParseFree(void *p){ sqlite3_free(p); }
219508
219509static int sqlite3Fts5ExprNew(
219510 Fts5Config *pConfig, /* FTS5 Configuration */
219511 int bPhraseToAnd,
219512 int iCol,
219513 const char *zExpr, /* Expression text */
219514 Fts5Expr **ppNew,
219515 char **pzErr
219516){
219517 Fts5Parse sParse;
219518 Fts5Token token;
219519 const char *z = zExpr;
219520 int t; /* Next token type */
219521 void *pEngine;
219522 Fts5Expr *pNew;
219523
219524 *ppNew = 0;
219525 *pzErr = 0;
219526 memset(s: &sParse, c: 0, n: sizeof(sParse));
219527 sParse.bPhraseToAnd = bPhraseToAnd;
219528 pEngine = sqlite3Fts5ParserAlloc(mallocProc: fts5ParseAlloc);
219529 if( pEngine==0 ){ return SQLITE_NOMEM; }
219530 sParse.pConfig = pConfig;
219531
219532 do {
219533 t = fts5ExprGetToken(pParse: &sParse, pz: &z, pToken: &token);
219534 sqlite3Fts5Parser(pEngine, t, token, &sParse);
219535 }while( sParse.rc==SQLITE_OK && t!=FTS5_EOF );
219536 sqlite3Fts5ParserFree(pEngine, freeProc: fts5ParseFree);
219537
219538 /* If the LHS of the MATCH expression was a user column, apply the
219539 ** implicit column-filter. */
219540 if( iCol<pConfig->nCol && sParse.pExpr && sParse.rc==SQLITE_OK ){
219541 int n = sizeof(Fts5Colset);
219542 Fts5Colset *pColset = (Fts5Colset*)sqlite3Fts5MallocZero(pRc: &sParse.rc, nByte: n);
219543 if( pColset ){
219544 pColset->nCol = 1;
219545 pColset->aiCol[0] = iCol;
219546 sqlite3Fts5ParseSetColset(&sParse, sParse.pExpr, pColset);
219547 }
219548 }
219549
219550 assert( sParse.rc!=SQLITE_OK || sParse.zErr==0 );
219551 if( sParse.rc==SQLITE_OK ){
219552 *ppNew = pNew = sqlite3_malloc(n: sizeof(Fts5Expr));
219553 if( pNew==0 ){
219554 sParse.rc = SQLITE_NOMEM;
219555 sqlite3Fts5ParseNodeFree(sParse.pExpr);
219556 }else{
219557 if( !sParse.pExpr ){
219558 const int nByte = sizeof(Fts5ExprNode);
219559 pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(pRc: &sParse.rc, nByte);
219560 if( pNew->pRoot ){
219561 pNew->pRoot->bEof = 1;
219562 }
219563 }else{
219564 pNew->pRoot = sParse.pExpr;
219565 }
219566 pNew->pIndex = 0;
219567 pNew->pConfig = pConfig;
219568 pNew->apExprPhrase = sParse.apPhrase;
219569 pNew->nPhrase = sParse.nPhrase;
219570 pNew->bDesc = 0;
219571 sParse.apPhrase = 0;
219572 }
219573 }else{
219574 sqlite3Fts5ParseNodeFree(sParse.pExpr);
219575 }
219576
219577 sqlite3_free(p: sParse.apPhrase);
219578 *pzErr = sParse.zErr;
219579 return sParse.rc;
219580}
219581
219582/*
219583** This function is only called when using the special 'trigram' tokenizer.
219584** Argument zText contains the text of a LIKE or GLOB pattern matched
219585** against column iCol. This function creates and compiles an FTS5 MATCH
219586** expression that will match a superset of the rows matched by the LIKE or
219587** GLOB. If successful, SQLITE_OK is returned. Otherwise, an SQLite error
219588** code.
219589*/
219590static int sqlite3Fts5ExprPattern(
219591 Fts5Config *pConfig, int bGlob, int iCol, const char *zText, Fts5Expr **pp
219592){
219593 i64 nText = strlen(s: zText);
219594 char *zExpr = (char*)sqlite3_malloc64(n: nText*4 + 1);
219595 int rc = SQLITE_OK;
219596
219597 if( zExpr==0 ){
219598 rc = SQLITE_NOMEM;
219599 }else{
219600 char aSpec[3];
219601 int iOut = 0;
219602 int i = 0;
219603 int iFirst = 0;
219604
219605 if( bGlob==0 ){
219606 aSpec[0] = '_';
219607 aSpec[1] = '%';
219608 aSpec[2] = 0;
219609 }else{
219610 aSpec[0] = '*';
219611 aSpec[1] = '?';
219612 aSpec[2] = '[';
219613 }
219614
219615 while( i<=nText ){
219616 if( i==nText
219617 || zText[i]==aSpec[0] || zText[i]==aSpec[1] || zText[i]==aSpec[2]
219618 ){
219619 if( i-iFirst>=3 ){
219620 int jj;
219621 zExpr[iOut++] = '"';
219622 for(jj=iFirst; jj<i; jj++){
219623 zExpr[iOut++] = zText[jj];
219624 if( zText[jj]=='"' ) zExpr[iOut++] = '"';
219625 }
219626 zExpr[iOut++] = '"';
219627 zExpr[iOut++] = ' ';
219628 }
219629 if( zText[i]==aSpec[2] ){
219630 i += 2;
219631 if( zText[i-1]=='^' ) i++;
219632 while( i<nText && zText[i]!=']' ) i++;
219633 }
219634 iFirst = i+1;
219635 }
219636 i++;
219637 }
219638 if( iOut>0 ){
219639 int bAnd = 0;
219640 if( pConfig->eDetail!=FTS5_DETAIL_FULL ){
219641 bAnd = 1;
219642 if( pConfig->eDetail==FTS5_DETAIL_NONE ){
219643 iCol = pConfig->nCol;
219644 }
219645 }
219646 zExpr[iOut] = '\0';
219647 rc = sqlite3Fts5ExprNew(pConfig, bPhraseToAnd: bAnd, iCol, zExpr, ppNew: pp,pzErr: pConfig->pzErrmsg);
219648 }else{
219649 *pp = 0;
219650 }
219651 sqlite3_free(p: zExpr);
219652 }
219653
219654 return rc;
219655}
219656
219657/*
219658** Free the expression node object passed as the only argument.
219659*/
219660static void sqlite3Fts5ParseNodeFree(Fts5ExprNode *p){
219661 if( p ){
219662 int i;
219663 for(i=0; i<p->nChild; i++){
219664 sqlite3Fts5ParseNodeFree(p: p->apChild[i]);
219665 }
219666 sqlite3Fts5ParseNearsetFree(p->pNear);
219667 sqlite3_free(p);
219668 }
219669}
219670
219671/*
219672** Free the expression object passed as the only argument.
219673*/
219674static void sqlite3Fts5ExprFree(Fts5Expr *p){
219675 if( p ){
219676 sqlite3Fts5ParseNodeFree(p: p->pRoot);
219677 sqlite3_free(p: p->apExprPhrase);
219678 sqlite3_free(p);
219679 }
219680}
219681
219682static int sqlite3Fts5ExprAnd(Fts5Expr **pp1, Fts5Expr *p2){
219683 Fts5Parse sParse;
219684 memset(s: &sParse, c: 0, n: sizeof(sParse));
219685
219686 if( *pp1 ){
219687 Fts5Expr *p1 = *pp1;
219688 int nPhrase = p1->nPhrase + p2->nPhrase;
219689
219690 p1->pRoot = sqlite3Fts5ParseNode(pParse: &sParse, FTS5_AND, pLeft: p1->pRoot, pRight: p2->pRoot,pNear: 0);
219691 p2->pRoot = 0;
219692
219693 if( sParse.rc==SQLITE_OK ){
219694 Fts5ExprPhrase **ap = (Fts5ExprPhrase**)sqlite3_realloc(
219695 pOld: p1->apExprPhrase, n: nPhrase * sizeof(Fts5ExprPhrase*)
219696 );
219697 if( ap==0 ){
219698 sParse.rc = SQLITE_NOMEM;
219699 }else{
219700 int i;
219701 memmove(dest: &ap[p2->nPhrase], src: ap, n: p1->nPhrase*sizeof(Fts5ExprPhrase*));
219702 for(i=0; i<p2->nPhrase; i++){
219703 ap[i] = p2->apExprPhrase[i];
219704 }
219705 p1->nPhrase = nPhrase;
219706 p1->apExprPhrase = ap;
219707 }
219708 }
219709 sqlite3_free(p: p2->apExprPhrase);
219710 sqlite3_free(p: p2);
219711 }else{
219712 *pp1 = p2;
219713 }
219714
219715 return sParse.rc;
219716}
219717
219718/*
219719** Argument pTerm must be a synonym iterator. Return the current rowid
219720** that it points to.
219721*/
219722static i64 fts5ExprSynonymRowid(Fts5ExprTerm *pTerm, int bDesc, int *pbEof){
219723 i64 iRet = 0;
219724 int bRetValid = 0;
219725 Fts5ExprTerm *p;
219726
219727 assert( pTerm );
219728 assert( pTerm->pSynonym );
219729 assert( bDesc==0 || bDesc==1 );
219730 for(p=pTerm; p; p=p->pSynonym){
219731 if( 0==sqlite3Fts5IterEof(p->pIter) ){
219732 i64 iRowid = p->pIter->iRowid;
219733 if( bRetValid==0 || (bDesc!=(iRowid<iRet)) ){
219734 iRet = iRowid;
219735 bRetValid = 1;
219736 }
219737 }
219738 }
219739
219740 if( pbEof && bRetValid==0 ) *pbEof = 1;
219741 return iRet;
219742}
219743
219744/*
219745** Argument pTerm must be a synonym iterator.
219746*/
219747static int fts5ExprSynonymList(
219748 Fts5ExprTerm *pTerm,
219749 i64 iRowid,
219750 Fts5Buffer *pBuf, /* Use this buffer for space if required */
219751 u8 **pa, int *pn
219752){
219753 Fts5PoslistReader aStatic[4];
219754 Fts5PoslistReader *aIter = aStatic;
219755 int nIter = 0;
219756 int nAlloc = 4;
219757 int rc = SQLITE_OK;
219758 Fts5ExprTerm *p;
219759
219760 assert( pTerm->pSynonym );
219761 for(p=pTerm; p; p=p->pSynonym){
219762 Fts5IndexIter *pIter = p->pIter;
219763 if( sqlite3Fts5IterEof(pIter)==0 && pIter->iRowid==iRowid ){
219764 if( pIter->nData==0 ) continue;
219765 if( nIter==nAlloc ){
219766 sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * nAlloc * 2;
219767 Fts5PoslistReader *aNew = (Fts5PoslistReader*)sqlite3_malloc64(n: nByte);
219768 if( aNew==0 ){
219769 rc = SQLITE_NOMEM;
219770 goto synonym_poslist_out;
219771 }
219772 memcpy(dest: aNew, src: aIter, n: sizeof(Fts5PoslistReader) * nIter);
219773 nAlloc = nAlloc*2;
219774 if( aIter!=aStatic ) sqlite3_free(p: aIter);
219775 aIter = aNew;
219776 }
219777 sqlite3Fts5PoslistReaderInit(a: pIter->pData, n: pIter->nData, pIter: &aIter[nIter]);
219778 assert( aIter[nIter].bEof==0 );
219779 nIter++;
219780 }
219781 }
219782
219783 if( nIter==1 ){
219784 *pa = (u8*)aIter[0].a;
219785 *pn = aIter[0].n;
219786 }else{
219787 Fts5PoslistWriter writer = {0};
219788 i64 iPrev = -1;
219789 fts5BufferZero(pBuf);
219790 while( 1 ){
219791 int i;
219792 i64 iMin = FTS5_LARGEST_INT64;
219793 for(i=0; i<nIter; i++){
219794 if( aIter[i].bEof==0 ){
219795 if( aIter[i].iPos==iPrev ){
219796 if( sqlite3Fts5PoslistReaderNext(pIter: &aIter[i]) ) continue;
219797 }
219798 if( aIter[i].iPos<iMin ){
219799 iMin = aIter[i].iPos;
219800 }
219801 }
219802 }
219803 if( iMin==FTS5_LARGEST_INT64 || rc!=SQLITE_OK ) break;
219804 rc = sqlite3Fts5PoslistWriterAppend(pBuf, pWriter: &writer, iPos: iMin);
219805 iPrev = iMin;
219806 }
219807 if( rc==SQLITE_OK ){
219808 *pa = pBuf->p;
219809 *pn = pBuf->n;
219810 }
219811 }
219812
219813 synonym_poslist_out:
219814 if( aIter!=aStatic ) sqlite3_free(p: aIter);
219815 return rc;
219816}
219817
219818
219819/*
219820** All individual term iterators in pPhrase are guaranteed to be valid and
219821** pointing to the same rowid when this function is called. This function
219822** checks if the current rowid really is a match, and if so populates
219823** the pPhrase->poslist buffer accordingly. Output parameter *pbMatch
219824** is set to true if this is really a match, or false otherwise.
219825**
219826** SQLITE_OK is returned if an error occurs, or an SQLite error code
219827** otherwise. It is not considered an error code if the current rowid is
219828** not a match.
219829*/
219830static int fts5ExprPhraseIsMatch(
219831 Fts5ExprNode *pNode, /* Node pPhrase belongs to */
219832 Fts5ExprPhrase *pPhrase, /* Phrase object to initialize */
219833 int *pbMatch /* OUT: Set to true if really a match */
219834){
219835 Fts5PoslistWriter writer = {0};
219836 Fts5PoslistReader aStatic[4];
219837 Fts5PoslistReader *aIter = aStatic;
219838 int i;
219839 int rc = SQLITE_OK;
219840 int bFirst = pPhrase->aTerm[0].bFirst;
219841
219842 fts5BufferZero(&pPhrase->poslist);
219843
219844 /* If the aStatic[] array is not large enough, allocate a large array
219845 ** using sqlite3_malloc(). This approach could be improved upon. */
219846 if( pPhrase->nTerm>ArraySize(aStatic) ){
219847 sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * pPhrase->nTerm;
219848 aIter = (Fts5PoslistReader*)sqlite3_malloc64(n: nByte);
219849 if( !aIter ) return SQLITE_NOMEM;
219850 }
219851 memset(s: aIter, c: 0, n: sizeof(Fts5PoslistReader) * pPhrase->nTerm);
219852
219853 /* Initialize a term iterator for each term in the phrase */
219854 for(i=0; i<pPhrase->nTerm; i++){
219855 Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
219856 int n = 0;
219857 int bFlag = 0;
219858 u8 *a = 0;
219859 if( pTerm->pSynonym ){
219860 Fts5Buffer buf = {0, 0, 0};
219861 rc = fts5ExprSynonymList(pTerm, iRowid: pNode->iRowid, pBuf: &buf, pa: &a, pn: &n);
219862 if( rc ){
219863 sqlite3_free(p: a);
219864 goto ismatch_out;
219865 }
219866 if( a==buf.p ) bFlag = 1;
219867 }else{
219868 a = (u8*)pTerm->pIter->pData;
219869 n = pTerm->pIter->nData;
219870 }
219871 sqlite3Fts5PoslistReaderInit(a, n, pIter: &aIter[i]);
219872 aIter[i].bFlag = (u8)bFlag;
219873 if( aIter[i].bEof ) goto ismatch_out;
219874 }
219875
219876 while( 1 ){
219877 int bMatch;
219878 i64 iPos = aIter[0].iPos;
219879 do {
219880 bMatch = 1;
219881 for(i=0; i<pPhrase->nTerm; i++){
219882 Fts5PoslistReader *pPos = &aIter[i];
219883 i64 iAdj = iPos + i;
219884 if( pPos->iPos!=iAdj ){
219885 bMatch = 0;
219886 while( pPos->iPos<iAdj ){
219887 if( sqlite3Fts5PoslistReaderNext(pIter: pPos) ) goto ismatch_out;
219888 }
219889 if( pPos->iPos>iAdj ) iPos = pPos->iPos-i;
219890 }
219891 }
219892 }while( bMatch==0 );
219893
219894 /* Append position iPos to the output */
219895 if( bFirst==0 || FTS5_POS2OFFSET(iPos)==0 ){
219896 rc = sqlite3Fts5PoslistWriterAppend(pBuf: &pPhrase->poslist, pWriter: &writer, iPos);
219897 if( rc!=SQLITE_OK ) goto ismatch_out;
219898 }
219899
219900 for(i=0; i<pPhrase->nTerm; i++){
219901 if( sqlite3Fts5PoslistReaderNext(pIter: &aIter[i]) ) goto ismatch_out;
219902 }
219903 }
219904
219905 ismatch_out:
219906 *pbMatch = (pPhrase->poslist.n>0);
219907 for(i=0; i<pPhrase->nTerm; i++){
219908 if( aIter[i].bFlag ) sqlite3_free(p: (u8*)aIter[i].a);
219909 }
219910 if( aIter!=aStatic ) sqlite3_free(p: aIter);
219911 return rc;
219912}
219913
219914typedef struct Fts5LookaheadReader Fts5LookaheadReader;
219915struct Fts5LookaheadReader {
219916 const u8 *a; /* Buffer containing position list */
219917 int n; /* Size of buffer a[] in bytes */
219918 int i; /* Current offset in position list */
219919 i64 iPos; /* Current position */
219920 i64 iLookahead; /* Next position */
219921};
219922
219923#define FTS5_LOOKAHEAD_EOF (((i64)1) << 62)
219924
219925static int fts5LookaheadReaderNext(Fts5LookaheadReader *p){
219926 p->iPos = p->iLookahead;
219927 if( sqlite3Fts5PoslistNext64(a: p->a, n: p->n, pi: &p->i, piOff: &p->iLookahead) ){
219928 p->iLookahead = FTS5_LOOKAHEAD_EOF;
219929 }
219930 return (p->iPos==FTS5_LOOKAHEAD_EOF);
219931}
219932
219933static int fts5LookaheadReaderInit(
219934 const u8 *a, int n, /* Buffer to read position list from */
219935 Fts5LookaheadReader *p /* Iterator object to initialize */
219936){
219937 memset(s: p, c: 0, n: sizeof(Fts5LookaheadReader));
219938 p->a = a;
219939 p->n = n;
219940 fts5LookaheadReaderNext(p);
219941 return fts5LookaheadReaderNext(p);
219942}
219943
219944typedef struct Fts5NearTrimmer Fts5NearTrimmer;
219945struct Fts5NearTrimmer {
219946 Fts5LookaheadReader reader; /* Input iterator */
219947 Fts5PoslistWriter writer; /* Writer context */
219948 Fts5Buffer *pOut; /* Output poslist */
219949};
219950
219951/*
219952** The near-set object passed as the first argument contains more than
219953** one phrase. All phrases currently point to the same row. The
219954** Fts5ExprPhrase.poslist buffers are populated accordingly. This function
219955** tests if the current row contains instances of each phrase sufficiently
219956** close together to meet the NEAR constraint. Non-zero is returned if it
219957** does, or zero otherwise.
219958**
219959** If in/out parameter (*pRc) is set to other than SQLITE_OK when this
219960** function is called, it is a no-op. Or, if an error (e.g. SQLITE_NOMEM)
219961** occurs within this function (*pRc) is set accordingly before returning.
219962** The return value is undefined in both these cases.
219963**
219964** If no error occurs and non-zero (a match) is returned, the position-list
219965** of each phrase object is edited to contain only those entries that
219966** meet the constraint before returning.
219967*/
219968static int fts5ExprNearIsMatch(int *pRc, Fts5ExprNearset *pNear){
219969 Fts5NearTrimmer aStatic[4];
219970 Fts5NearTrimmer *a = aStatic;
219971 Fts5ExprPhrase **apPhrase = pNear->apPhrase;
219972
219973 int i;
219974 int rc = *pRc;
219975 int bMatch;
219976
219977 assert( pNear->nPhrase>1 );
219978
219979 /* If the aStatic[] array is not large enough, allocate a large array
219980 ** using sqlite3_malloc(). This approach could be improved upon. */
219981 if( pNear->nPhrase>ArraySize(aStatic) ){
219982 sqlite3_int64 nByte = sizeof(Fts5NearTrimmer) * pNear->nPhrase;
219983 a = (Fts5NearTrimmer*)sqlite3Fts5MallocZero(pRc: &rc, nByte);
219984 }else{
219985 memset(s: aStatic, c: 0, n: sizeof(aStatic));
219986 }
219987 if( rc!=SQLITE_OK ){
219988 *pRc = rc;
219989 return 0;
219990 }
219991
219992 /* Initialize a lookahead iterator for each phrase. After passing the
219993 ** buffer and buffer size to the lookaside-reader init function, zero
219994 ** the phrase poslist buffer. The new poslist for the phrase (containing
219995 ** the same entries as the original with some entries removed on account
219996 ** of the NEAR constraint) is written over the original even as it is
219997 ** being read. This is safe as the entries for the new poslist are a
219998 ** subset of the old, so it is not possible for data yet to be read to
219999 ** be overwritten. */
220000 for(i=0; i<pNear->nPhrase; i++){
220001 Fts5Buffer *pPoslist = &apPhrase[i]->poslist;
220002 fts5LookaheadReaderInit(a: pPoslist->p, n: pPoslist->n, p: &a[i].reader);
220003 pPoslist->n = 0;
220004 a[i].pOut = pPoslist;
220005 }
220006
220007 while( 1 ){
220008 int iAdv;
220009 i64 iMin;
220010 i64 iMax;
220011
220012 /* This block advances the phrase iterators until they point to a set of
220013 ** entries that together comprise a match. */
220014 iMax = a[0].reader.iPos;
220015 do {
220016 bMatch = 1;
220017 for(i=0; i<pNear->nPhrase; i++){
220018 Fts5LookaheadReader *pPos = &a[i].reader;
220019 iMin = iMax - pNear->apPhrase[i]->nTerm - pNear->nNear;
220020 if( pPos->iPos<iMin || pPos->iPos>iMax ){
220021 bMatch = 0;
220022 while( pPos->iPos<iMin ){
220023 if( fts5LookaheadReaderNext(p: pPos) ) goto ismatch_out;
220024 }
220025 if( pPos->iPos>iMax ) iMax = pPos->iPos;
220026 }
220027 }
220028 }while( bMatch==0 );
220029
220030 /* Add an entry to each output position list */
220031 for(i=0; i<pNear->nPhrase; i++){
220032 i64 iPos = a[i].reader.iPos;
220033 Fts5PoslistWriter *pWriter = &a[i].writer;
220034 if( a[i].pOut->n==0 || iPos!=pWriter->iPrev ){
220035 sqlite3Fts5PoslistWriterAppend(pBuf: a[i].pOut, pWriter, iPos);
220036 }
220037 }
220038
220039 iAdv = 0;
220040 iMin = a[0].reader.iLookahead;
220041 for(i=0; i<pNear->nPhrase; i++){
220042 if( a[i].reader.iLookahead < iMin ){
220043 iMin = a[i].reader.iLookahead;
220044 iAdv = i;
220045 }
220046 }
220047 if( fts5LookaheadReaderNext(p: &a[iAdv].reader) ) goto ismatch_out;
220048 }
220049
220050 ismatch_out: {
220051 int bRet = a[0].pOut->n>0;
220052 *pRc = rc;
220053 if( a!=aStatic ) sqlite3_free(p: a);
220054 return bRet;
220055 }
220056}
220057
220058/*
220059** Advance iterator pIter until it points to a value equal to or laster
220060** than the initial value of *piLast. If this means the iterator points
220061** to a value laster than *piLast, update *piLast to the new lastest value.
220062**
220063** If the iterator reaches EOF, set *pbEof to true before returning. If
220064** an error occurs, set *pRc to an error code. If either *pbEof or *pRc
220065** are set, return a non-zero value. Otherwise, return zero.
220066*/
220067static int fts5ExprAdvanceto(
220068 Fts5IndexIter *pIter, /* Iterator to advance */
220069 int bDesc, /* True if iterator is "rowid DESC" */
220070 i64 *piLast, /* IN/OUT: Lastest rowid seen so far */
220071 int *pRc, /* OUT: Error code */
220072 int *pbEof /* OUT: Set to true if EOF */
220073){
220074 i64 iLast = *piLast;
220075 i64 iRowid;
220076
220077 iRowid = pIter->iRowid;
220078 if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
220079 int rc = sqlite3Fts5IterNextFrom(pIter, iMatch: iLast);
220080 if( rc || sqlite3Fts5IterEof(pIter) ){
220081 *pRc = rc;
220082 *pbEof = 1;
220083 return 1;
220084 }
220085 iRowid = pIter->iRowid;
220086 assert( (bDesc==0 && iRowid>=iLast) || (bDesc==1 && iRowid<=iLast) );
220087 }
220088 *piLast = iRowid;
220089
220090 return 0;
220091}
220092
220093static int fts5ExprSynonymAdvanceto(
220094 Fts5ExprTerm *pTerm, /* Term iterator to advance */
220095 int bDesc, /* True if iterator is "rowid DESC" */
220096 i64 *piLast, /* IN/OUT: Lastest rowid seen so far */
220097 int *pRc /* OUT: Error code */
220098){
220099 int rc = SQLITE_OK;
220100 i64 iLast = *piLast;
220101 Fts5ExprTerm *p;
220102 int bEof = 0;
220103
220104 for(p=pTerm; rc==SQLITE_OK && p; p=p->pSynonym){
220105 if( sqlite3Fts5IterEof(p->pIter)==0 ){
220106 i64 iRowid = p->pIter->iRowid;
220107 if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
220108 rc = sqlite3Fts5IterNextFrom(p->pIter, iMatch: iLast);
220109 }
220110 }
220111 }
220112
220113 if( rc!=SQLITE_OK ){
220114 *pRc = rc;
220115 bEof = 1;
220116 }else{
220117 *piLast = fts5ExprSynonymRowid(pTerm, bDesc, pbEof: &bEof);
220118 }
220119 return bEof;
220120}
220121
220122
220123static int fts5ExprNearTest(
220124 int *pRc,
220125 Fts5Expr *pExpr, /* Expression that pNear is a part of */
220126 Fts5ExprNode *pNode /* The "NEAR" node (FTS5_STRING) */
220127){
220128 Fts5ExprNearset *pNear = pNode->pNear;
220129 int rc = *pRc;
220130
220131 if( pExpr->pConfig->eDetail!=FTS5_DETAIL_FULL ){
220132 Fts5ExprTerm *pTerm;
220133 Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
220134 pPhrase->poslist.n = 0;
220135 for(pTerm=&pPhrase->aTerm[0]; pTerm; pTerm=pTerm->pSynonym){
220136 Fts5IndexIter *pIter = pTerm->pIter;
220137 if( sqlite3Fts5IterEof(pIter)==0 ){
220138 if( pIter->iRowid==pNode->iRowid && pIter->nData>0 ){
220139 pPhrase->poslist.n = 1;
220140 }
220141 }
220142 }
220143 return pPhrase->poslist.n;
220144 }else{
220145 int i;
220146
220147 /* Check that each phrase in the nearset matches the current row.
220148 ** Populate the pPhrase->poslist buffers at the same time. If any
220149 ** phrase is not a match, break out of the loop early. */
220150 for(i=0; rc==SQLITE_OK && i<pNear->nPhrase; i++){
220151 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
220152 if( pPhrase->nTerm>1 || pPhrase->aTerm[0].pSynonym
220153 || pNear->pColset || pPhrase->aTerm[0].bFirst
220154 ){
220155 int bMatch = 0;
220156 rc = fts5ExprPhraseIsMatch(pNode, pPhrase, pbMatch: &bMatch);
220157 if( bMatch==0 ) break;
220158 }else{
220159 Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
220160 fts5BufferSet(&rc, &pPhrase->poslist, pIter->nData, pIter->pData);
220161 }
220162 }
220163
220164 *pRc = rc;
220165 if( i==pNear->nPhrase && (i==1 || fts5ExprNearIsMatch(pRc, pNear)) ){
220166 return 1;
220167 }
220168 return 0;
220169 }
220170}
220171
220172
220173/*
220174** Initialize all term iterators in the pNear object. If any term is found
220175** to match no documents at all, return immediately without initializing any
220176** further iterators.
220177**
220178** If an error occurs, return an SQLite error code. Otherwise, return
220179** SQLITE_OK. It is not considered an error if some term matches zero
220180** documents.
220181*/
220182static int fts5ExprNearInitAll(
220183 Fts5Expr *pExpr,
220184 Fts5ExprNode *pNode
220185){
220186 Fts5ExprNearset *pNear = pNode->pNear;
220187 int i;
220188
220189 assert( pNode->bNomatch==0 );
220190 for(i=0; i<pNear->nPhrase; i++){
220191 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
220192 if( pPhrase->nTerm==0 ){
220193 pNode->bEof = 1;
220194 return SQLITE_OK;
220195 }else{
220196 int j;
220197 for(j=0; j<pPhrase->nTerm; j++){
220198 Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
220199 Fts5ExprTerm *p;
220200 int bHit = 0;
220201
220202 for(p=pTerm; p; p=p->pSynonym){
220203 int rc;
220204 if( p->pIter ){
220205 sqlite3Fts5IterClose(p->pIter);
220206 p->pIter = 0;
220207 }
220208 rc = sqlite3Fts5IndexQuery(
220209 p: pExpr->pIndex, pToken: p->zTerm, nToken: (int)strlen(s: p->zTerm),
220210 flags: (pTerm->bPrefix ? FTS5INDEX_QUERY_PREFIX : 0) |
220211 (pExpr->bDesc ? FTS5INDEX_QUERY_DESC : 0),
220212 pColset: pNear->pColset,
220213 ppIter: &p->pIter
220214 );
220215 assert( (rc==SQLITE_OK)==(p->pIter!=0) );
220216 if( rc!=SQLITE_OK ) return rc;
220217 if( 0==sqlite3Fts5IterEof(p->pIter) ){
220218 bHit = 1;
220219 }
220220 }
220221
220222 if( bHit==0 ){
220223 pNode->bEof = 1;
220224 return SQLITE_OK;
220225 }
220226 }
220227 }
220228 }
220229
220230 pNode->bEof = 0;
220231 return SQLITE_OK;
220232}
220233
220234/*
220235** If pExpr is an ASC iterator, this function returns a value with the
220236** same sign as:
220237**
220238** (iLhs - iRhs)
220239**
220240** Otherwise, if this is a DESC iterator, the opposite is returned:
220241**
220242** (iRhs - iLhs)
220243*/
220244static int fts5RowidCmp(
220245 Fts5Expr *pExpr,
220246 i64 iLhs,
220247 i64 iRhs
220248){
220249 assert( pExpr->bDesc==0 || pExpr->bDesc==1 );
220250 if( pExpr->bDesc==0 ){
220251 if( iLhs<iRhs ) return -1;
220252 return (iLhs > iRhs);
220253 }else{
220254 if( iLhs>iRhs ) return -1;
220255 return (iLhs < iRhs);
220256 }
220257}
220258
220259static void fts5ExprSetEof(Fts5ExprNode *pNode){
220260 int i;
220261 pNode->bEof = 1;
220262 pNode->bNomatch = 0;
220263 for(i=0; i<pNode->nChild; i++){
220264 fts5ExprSetEof(pNode: pNode->apChild[i]);
220265 }
220266}
220267
220268static void fts5ExprNodeZeroPoslist(Fts5ExprNode *pNode){
220269 if( pNode->eType==FTS5_STRING || pNode->eType==FTS5_TERM ){
220270 Fts5ExprNearset *pNear = pNode->pNear;
220271 int i;
220272 for(i=0; i<pNear->nPhrase; i++){
220273 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
220274 pPhrase->poslist.n = 0;
220275 }
220276 }else{
220277 int i;
220278 for(i=0; i<pNode->nChild; i++){
220279 fts5ExprNodeZeroPoslist(pNode: pNode->apChild[i]);
220280 }
220281 }
220282}
220283
220284
220285
220286/*
220287** Compare the values currently indicated by the two nodes as follows:
220288**
220289** res = (*p1) - (*p2)
220290**
220291** Nodes that point to values that come later in the iteration order are
220292** considered to be larger. Nodes at EOF are the largest of all.
220293**
220294** This means that if the iteration order is ASC, then numerically larger
220295** rowids are considered larger. Or if it is the default DESC, numerically
220296** smaller rowids are larger.
220297*/
220298static int fts5NodeCompare(
220299 Fts5Expr *pExpr,
220300 Fts5ExprNode *p1,
220301 Fts5ExprNode *p2
220302){
220303 if( p2->bEof ) return -1;
220304 if( p1->bEof ) return +1;
220305 return fts5RowidCmp(pExpr, iLhs: p1->iRowid, iRhs: p2->iRowid);
220306}
220307
220308/*
220309** All individual term iterators in pNear are guaranteed to be valid when
220310** this function is called. This function checks if all term iterators
220311** point to the same rowid, and if not, advances them until they do.
220312** If an EOF is reached before this happens, *pbEof is set to true before
220313** returning.
220314**
220315** SQLITE_OK is returned if an error occurs, or an SQLite error code
220316** otherwise. It is not considered an error code if an iterator reaches
220317** EOF.
220318*/
220319static int fts5ExprNodeTest_STRING(
220320 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
220321 Fts5ExprNode *pNode
220322){
220323 Fts5ExprNearset *pNear = pNode->pNear;
220324 Fts5ExprPhrase *pLeft = pNear->apPhrase[0];
220325 int rc = SQLITE_OK;
220326 i64 iLast; /* Lastest rowid any iterator points to */
220327 int i, j; /* Phrase and token index, respectively */
220328 int bMatch; /* True if all terms are at the same rowid */
220329 const int bDesc = pExpr->bDesc;
220330
220331 /* Check that this node should not be FTS5_TERM */
220332 assert( pNear->nPhrase>1
220333 || pNear->apPhrase[0]->nTerm>1
220334 || pNear->apPhrase[0]->aTerm[0].pSynonym
220335 || pNear->apPhrase[0]->aTerm[0].bFirst
220336 );
220337
220338 /* Initialize iLast, the "lastest" rowid any iterator points to. If the
220339 ** iterator skips through rowids in the default ascending order, this means
220340 ** the maximum rowid. Or, if the iterator is "ORDER BY rowid DESC", then it
220341 ** means the minimum rowid. */
220342 if( pLeft->aTerm[0].pSynonym ){
220343 iLast = fts5ExprSynonymRowid(pTerm: &pLeft->aTerm[0], bDesc, pbEof: 0);
220344 }else{
220345 iLast = pLeft->aTerm[0].pIter->iRowid;
220346 }
220347
220348 do {
220349 bMatch = 1;
220350 for(i=0; i<pNear->nPhrase; i++){
220351 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
220352 for(j=0; j<pPhrase->nTerm; j++){
220353 Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
220354 if( pTerm->pSynonym ){
220355 i64 iRowid = fts5ExprSynonymRowid(pTerm, bDesc, pbEof: 0);
220356 if( iRowid==iLast ) continue;
220357 bMatch = 0;
220358 if( fts5ExprSynonymAdvanceto(pTerm, bDesc, piLast: &iLast, pRc: &rc) ){
220359 pNode->bNomatch = 0;
220360 pNode->bEof = 1;
220361 return rc;
220362 }
220363 }else{
220364 Fts5IndexIter *pIter = pPhrase->aTerm[j].pIter;
220365 if( pIter->iRowid==iLast || pIter->bEof ) continue;
220366 bMatch = 0;
220367 if( fts5ExprAdvanceto(pIter, bDesc, piLast: &iLast, pRc: &rc, pbEof: &pNode->bEof) ){
220368 return rc;
220369 }
220370 }
220371 }
220372 }
220373 }while( bMatch==0 );
220374
220375 pNode->iRowid = iLast;
220376 pNode->bNomatch = ((0==fts5ExprNearTest(pRc: &rc, pExpr, pNode)) && rc==SQLITE_OK);
220377 assert( pNode->bEof==0 || pNode->bNomatch==0 );
220378
220379 return rc;
220380}
220381
220382/*
220383** Advance the first term iterator in the first phrase of pNear. Set output
220384** variable *pbEof to true if it reaches EOF or if an error occurs.
220385**
220386** Return SQLITE_OK if successful, or an SQLite error code if an error
220387** occurs.
220388*/
220389static int fts5ExprNodeNext_STRING(
220390 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
220391 Fts5ExprNode *pNode, /* FTS5_STRING or FTS5_TERM node */
220392 int bFromValid,
220393 i64 iFrom
220394){
220395 Fts5ExprTerm *pTerm = &pNode->pNear->apPhrase[0]->aTerm[0];
220396 int rc = SQLITE_OK;
220397
220398 pNode->bNomatch = 0;
220399 if( pTerm->pSynonym ){
220400 int bEof = 1;
220401 Fts5ExprTerm *p;
220402
220403 /* Find the firstest rowid any synonym points to. */
220404 i64 iRowid = fts5ExprSynonymRowid(pTerm, bDesc: pExpr->bDesc, pbEof: 0);
220405
220406 /* Advance each iterator that currently points to iRowid. Or, if iFrom
220407 ** is valid - each iterator that points to a rowid before iFrom. */
220408 for(p=pTerm; p; p=p->pSynonym){
220409 if( sqlite3Fts5IterEof(p->pIter)==0 ){
220410 i64 ii = p->pIter->iRowid;
220411 if( ii==iRowid
220412 || (bFromValid && ii!=iFrom && (ii>iFrom)==pExpr->bDesc)
220413 ){
220414 if( bFromValid ){
220415 rc = sqlite3Fts5IterNextFrom(p->pIter, iMatch: iFrom);
220416 }else{
220417 rc = sqlite3Fts5IterNext(p->pIter);
220418 }
220419 if( rc!=SQLITE_OK ) break;
220420 if( sqlite3Fts5IterEof(p->pIter)==0 ){
220421 bEof = 0;
220422 }
220423 }else{
220424 bEof = 0;
220425 }
220426 }
220427 }
220428
220429 /* Set the EOF flag if either all synonym iterators are at EOF or an
220430 ** error has occurred. */
220431 pNode->bEof = (rc || bEof);
220432 }else{
220433 Fts5IndexIter *pIter = pTerm->pIter;
220434
220435 assert( Fts5NodeIsString(pNode) );
220436 if( bFromValid ){
220437 rc = sqlite3Fts5IterNextFrom(pIter, iMatch: iFrom);
220438 }else{
220439 rc = sqlite3Fts5IterNext(pIter);
220440 }
220441
220442 pNode->bEof = (rc || sqlite3Fts5IterEof(pIter));
220443 }
220444
220445 if( pNode->bEof==0 ){
220446 assert( rc==SQLITE_OK );
220447 rc = fts5ExprNodeTest_STRING(pExpr, pNode);
220448 }
220449
220450 return rc;
220451}
220452
220453
220454static int fts5ExprNodeTest_TERM(
220455 Fts5Expr *pExpr, /* Expression that pNear is a part of */
220456 Fts5ExprNode *pNode /* The "NEAR" node (FTS5_TERM) */
220457){
220458 /* As this "NEAR" object is actually a single phrase that consists
220459 ** of a single term only, grab pointers into the poslist managed by the
220460 ** fts5_index.c iterator object. This is much faster than synthesizing
220461 ** a new poslist the way we have to for more complicated phrase or NEAR
220462 ** expressions. */
220463 Fts5ExprPhrase *pPhrase = pNode->pNear->apPhrase[0];
220464 Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
220465
220466 assert( pNode->eType==FTS5_TERM );
220467 assert( pNode->pNear->nPhrase==1 && pPhrase->nTerm==1 );
220468 assert( pPhrase->aTerm[0].pSynonym==0 );
220469
220470 pPhrase->poslist.n = pIter->nData;
220471 if( pExpr->pConfig->eDetail==FTS5_DETAIL_FULL ){
220472 pPhrase->poslist.p = (u8*)pIter->pData;
220473 }
220474 pNode->iRowid = pIter->iRowid;
220475 pNode->bNomatch = (pPhrase->poslist.n==0);
220476 return SQLITE_OK;
220477}
220478
220479/*
220480** xNext() method for a node of type FTS5_TERM.
220481*/
220482static int fts5ExprNodeNext_TERM(
220483 Fts5Expr *pExpr,
220484 Fts5ExprNode *pNode,
220485 int bFromValid,
220486 i64 iFrom
220487){
220488 int rc;
220489 Fts5IndexIter *pIter = pNode->pNear->apPhrase[0]->aTerm[0].pIter;
220490
220491 assert( pNode->bEof==0 );
220492 if( bFromValid ){
220493 rc = sqlite3Fts5IterNextFrom(pIter, iMatch: iFrom);
220494 }else{
220495 rc = sqlite3Fts5IterNext(pIter);
220496 }
220497 if( rc==SQLITE_OK && sqlite3Fts5IterEof(pIter)==0 ){
220498 rc = fts5ExprNodeTest_TERM(pExpr, pNode);
220499 }else{
220500 pNode->bEof = 1;
220501 pNode->bNomatch = 0;
220502 }
220503 return rc;
220504}
220505
220506static void fts5ExprNodeTest_OR(
220507 Fts5Expr *pExpr, /* Expression of which pNode is a part */
220508 Fts5ExprNode *pNode /* Expression node to test */
220509){
220510 Fts5ExprNode *pNext = pNode->apChild[0];
220511 int i;
220512
220513 for(i=1; i<pNode->nChild; i++){
220514 Fts5ExprNode *pChild = pNode->apChild[i];
220515 int cmp = fts5NodeCompare(pExpr, p1: pNext, p2: pChild);
220516 if( cmp>0 || (cmp==0 && pChild->bNomatch==0) ){
220517 pNext = pChild;
220518 }
220519 }
220520 pNode->iRowid = pNext->iRowid;
220521 pNode->bEof = pNext->bEof;
220522 pNode->bNomatch = pNext->bNomatch;
220523}
220524
220525static int fts5ExprNodeNext_OR(
220526 Fts5Expr *pExpr,
220527 Fts5ExprNode *pNode,
220528 int bFromValid,
220529 i64 iFrom
220530){
220531 int i;
220532 i64 iLast = pNode->iRowid;
220533
220534 for(i=0; i<pNode->nChild; i++){
220535 Fts5ExprNode *p1 = pNode->apChild[i];
220536 assert( p1->bEof || fts5RowidCmp(pExpr, p1->iRowid, iLast)>=0 );
220537 if( p1->bEof==0 ){
220538 if( (p1->iRowid==iLast)
220539 || (bFromValid && fts5RowidCmp(pExpr, iLhs: p1->iRowid, iRhs: iFrom)<0)
220540 ){
220541 int rc = fts5ExprNodeNext(pExpr, p1, bFromValid, iFrom);
220542 if( rc!=SQLITE_OK ){
220543 pNode->bNomatch = 0;
220544 return rc;
220545 }
220546 }
220547 }
220548 }
220549
220550 fts5ExprNodeTest_OR(pExpr, pNode);
220551 return SQLITE_OK;
220552}
220553
220554/*
220555** Argument pNode is an FTS5_AND node.
220556*/
220557static int fts5ExprNodeTest_AND(
220558 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
220559 Fts5ExprNode *pAnd /* FTS5_AND node to advance */
220560){
220561 int iChild;
220562 i64 iLast = pAnd->iRowid;
220563 int rc = SQLITE_OK;
220564 int bMatch;
220565
220566 assert( pAnd->bEof==0 );
220567 do {
220568 pAnd->bNomatch = 0;
220569 bMatch = 1;
220570 for(iChild=0; iChild<pAnd->nChild; iChild++){
220571 Fts5ExprNode *pChild = pAnd->apChild[iChild];
220572 int cmp = fts5RowidCmp(pExpr, iLhs: iLast, iRhs: pChild->iRowid);
220573 if( cmp>0 ){
220574 /* Advance pChild until it points to iLast or laster */
220575 rc = fts5ExprNodeNext(pExpr, pChild, 1, iLast);
220576 if( rc!=SQLITE_OK ){
220577 pAnd->bNomatch = 0;
220578 return rc;
220579 }
220580 }
220581
220582 /* If the child node is now at EOF, so is the parent AND node. Otherwise,
220583 ** the child node is guaranteed to have advanced at least as far as
220584 ** rowid iLast. So if it is not at exactly iLast, pChild->iRowid is the
220585 ** new lastest rowid seen so far. */
220586 assert( pChild->bEof || fts5RowidCmp(pExpr, iLast, pChild->iRowid)<=0 );
220587 if( pChild->bEof ){
220588 fts5ExprSetEof(pNode: pAnd);
220589 bMatch = 1;
220590 break;
220591 }else if( iLast!=pChild->iRowid ){
220592 bMatch = 0;
220593 iLast = pChild->iRowid;
220594 }
220595
220596 if( pChild->bNomatch ){
220597 pAnd->bNomatch = 1;
220598 }
220599 }
220600 }while( bMatch==0 );
220601
220602 if( pAnd->bNomatch && pAnd!=pExpr->pRoot ){
220603 fts5ExprNodeZeroPoslist(pNode: pAnd);
220604 }
220605 pAnd->iRowid = iLast;
220606 return SQLITE_OK;
220607}
220608
220609static int fts5ExprNodeNext_AND(
220610 Fts5Expr *pExpr,
220611 Fts5ExprNode *pNode,
220612 int bFromValid,
220613 i64 iFrom
220614){
220615 int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom);
220616 if( rc==SQLITE_OK ){
220617 rc = fts5ExprNodeTest_AND(pExpr, pAnd: pNode);
220618 }else{
220619 pNode->bNomatch = 0;
220620 }
220621 return rc;
220622}
220623
220624static int fts5ExprNodeTest_NOT(
220625 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
220626 Fts5ExprNode *pNode /* FTS5_NOT node to advance */
220627){
220628 int rc = SQLITE_OK;
220629 Fts5ExprNode *p1 = pNode->apChild[0];
220630 Fts5ExprNode *p2 = pNode->apChild[1];
220631 assert( pNode->nChild==2 );
220632
220633 while( rc==SQLITE_OK && p1->bEof==0 ){
220634 int cmp = fts5NodeCompare(pExpr, p1, p2);
220635 if( cmp>0 ){
220636 rc = fts5ExprNodeNext(pExpr, p2, 1, p1->iRowid);
220637 cmp = fts5NodeCompare(pExpr, p1, p2);
220638 }
220639 assert( rc!=SQLITE_OK || cmp<=0 );
220640 if( cmp || p2->bNomatch ) break;
220641 rc = fts5ExprNodeNext(pExpr, p1, 0, 0);
220642 }
220643 pNode->bEof = p1->bEof;
220644 pNode->bNomatch = p1->bNomatch;
220645 pNode->iRowid = p1->iRowid;
220646 if( p1->bEof ){
220647 fts5ExprNodeZeroPoslist(pNode: p2);
220648 }
220649 return rc;
220650}
220651
220652static int fts5ExprNodeNext_NOT(
220653 Fts5Expr *pExpr,
220654 Fts5ExprNode *pNode,
220655 int bFromValid,
220656 i64 iFrom
220657){
220658 int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom);
220659 if( rc==SQLITE_OK ){
220660 rc = fts5ExprNodeTest_NOT(pExpr, pNode);
220661 }
220662 if( rc!=SQLITE_OK ){
220663 pNode->bNomatch = 0;
220664 }
220665 return rc;
220666}
220667
220668/*
220669** If pNode currently points to a match, this function returns SQLITE_OK
220670** without modifying it. Otherwise, pNode is advanced until it does point
220671** to a match or EOF is reached.
220672*/
220673static int fts5ExprNodeTest(
220674 Fts5Expr *pExpr, /* Expression of which pNode is a part */
220675 Fts5ExprNode *pNode /* Expression node to test */
220676){
220677 int rc = SQLITE_OK;
220678 if( pNode->bEof==0 ){
220679 switch( pNode->eType ){
220680
220681 case FTS5_STRING: {
220682 rc = fts5ExprNodeTest_STRING(pExpr, pNode);
220683 break;
220684 }
220685
220686 case FTS5_TERM: {
220687 rc = fts5ExprNodeTest_TERM(pExpr, pNode);
220688 break;
220689 }
220690
220691 case FTS5_AND: {
220692 rc = fts5ExprNodeTest_AND(pExpr, pAnd: pNode);
220693 break;
220694 }
220695
220696 case FTS5_OR: {
220697 fts5ExprNodeTest_OR(pExpr, pNode);
220698 break;
220699 }
220700
220701 default: assert( pNode->eType==FTS5_NOT ); {
220702 rc = fts5ExprNodeTest_NOT(pExpr, pNode);
220703 break;
220704 }
220705 }
220706 }
220707 return rc;
220708}
220709
220710
220711/*
220712** Set node pNode, which is part of expression pExpr, to point to the first
220713** match. If there are no matches, set the Node.bEof flag to indicate EOF.
220714**
220715** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
220716** It is not an error if there are no matches.
220717*/
220718static int fts5ExprNodeFirst(Fts5Expr *pExpr, Fts5ExprNode *pNode){
220719 int rc = SQLITE_OK;
220720 pNode->bEof = 0;
220721 pNode->bNomatch = 0;
220722
220723 if( Fts5NodeIsString(pNode) ){
220724 /* Initialize all term iterators in the NEAR object. */
220725 rc = fts5ExprNearInitAll(pExpr, pNode);
220726 }else if( pNode->xNext==0 ){
220727 pNode->bEof = 1;
220728 }else{
220729 int i;
220730 int nEof = 0;
220731 for(i=0; i<pNode->nChild && rc==SQLITE_OK; i++){
220732 Fts5ExprNode *pChild = pNode->apChild[i];
220733 rc = fts5ExprNodeFirst(pExpr, pNode: pNode->apChild[i]);
220734 assert( pChild->bEof==0 || pChild->bEof==1 );
220735 nEof += pChild->bEof;
220736 }
220737 pNode->iRowid = pNode->apChild[0]->iRowid;
220738
220739 switch( pNode->eType ){
220740 case FTS5_AND:
220741 if( nEof>0 ) fts5ExprSetEof(pNode);
220742 break;
220743
220744 case FTS5_OR:
220745 if( pNode->nChild==nEof ) fts5ExprSetEof(pNode);
220746 break;
220747
220748 default:
220749 assert( pNode->eType==FTS5_NOT );
220750 pNode->bEof = pNode->apChild[0]->bEof;
220751 break;
220752 }
220753 }
220754
220755 if( rc==SQLITE_OK ){
220756 rc = fts5ExprNodeTest(pExpr, pNode);
220757 }
220758 return rc;
220759}
220760
220761
220762/*
220763** Begin iterating through the set of documents in index pIdx matched by
220764** the MATCH expression passed as the first argument. If the "bDesc"
220765** parameter is passed a non-zero value, iteration is in descending rowid
220766** order. Or, if it is zero, in ascending order.
220767**
220768** If iterating in ascending rowid order (bDesc==0), the first document
220769** visited is that with the smallest rowid that is larger than or equal
220770** to parameter iFirst. Or, if iterating in ascending order (bDesc==1),
220771** then the first document visited must have a rowid smaller than or
220772** equal to iFirst.
220773**
220774** Return SQLITE_OK if successful, or an SQLite error code otherwise. It
220775** is not considered an error if the query does not match any documents.
220776*/
220777static int sqlite3Fts5ExprFirst(Fts5Expr *p, Fts5Index *pIdx, i64 iFirst, int bDesc){
220778 Fts5ExprNode *pRoot = p->pRoot;
220779 int rc; /* Return code */
220780
220781 p->pIndex = pIdx;
220782 p->bDesc = bDesc;
220783 rc = fts5ExprNodeFirst(pExpr: p, pNode: pRoot);
220784
220785 /* If not at EOF but the current rowid occurs earlier than iFirst in
220786 ** the iteration order, move to document iFirst or later. */
220787 if( rc==SQLITE_OK
220788 && 0==pRoot->bEof
220789 && fts5RowidCmp(pExpr: p, iLhs: pRoot->iRowid, iRhs: iFirst)<0
220790 ){
220791 rc = fts5ExprNodeNext(p, pRoot, 1, iFirst);
220792 }
220793
220794 /* If the iterator is not at a real match, skip forward until it is. */
220795 while( pRoot->bNomatch && rc==SQLITE_OK ){
220796 assert( pRoot->bEof==0 );
220797 rc = fts5ExprNodeNext(p, pRoot, 0, 0);
220798 }
220799 return rc;
220800}
220801
220802/*
220803** Move to the next document
220804**
220805** Return SQLITE_OK if successful, or an SQLite error code otherwise. It
220806** is not considered an error if the query does not match any documents.
220807*/
220808static int sqlite3Fts5ExprNext(Fts5Expr *p, i64 iLast){
220809 int rc;
220810 Fts5ExprNode *pRoot = p->pRoot;
220811 assert( pRoot->bEof==0 && pRoot->bNomatch==0 );
220812 do {
220813 rc = fts5ExprNodeNext(p, pRoot, 0, 0);
220814 assert( pRoot->bNomatch==0 || (rc==SQLITE_OK && pRoot->bEof==0) );
220815 }while( pRoot->bNomatch );
220816 if( fts5RowidCmp(pExpr: p, iLhs: pRoot->iRowid, iRhs: iLast)>0 ){
220817 pRoot->bEof = 1;
220818 }
220819 return rc;
220820}
220821
220822static int sqlite3Fts5ExprEof(Fts5Expr *p){
220823 return p->pRoot->bEof;
220824}
220825
220826static i64 sqlite3Fts5ExprRowid(Fts5Expr *p){
220827 return p->pRoot->iRowid;
220828}
220829
220830static int fts5ParseStringFromToken(Fts5Token *pToken, char **pz){
220831 int rc = SQLITE_OK;
220832 *pz = sqlite3Fts5Strndup(pRc: &rc, pIn: pToken->p, nIn: pToken->n);
220833 return rc;
220834}
220835
220836/*
220837** Free the phrase object passed as the only argument.
220838*/
220839static void fts5ExprPhraseFree(Fts5ExprPhrase *pPhrase){
220840 if( pPhrase ){
220841 int i;
220842 for(i=0; i<pPhrase->nTerm; i++){
220843 Fts5ExprTerm *pSyn;
220844 Fts5ExprTerm *pNext;
220845 Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
220846 sqlite3_free(p: pTerm->zTerm);
220847 sqlite3Fts5IterClose(pTerm->pIter);
220848 for(pSyn=pTerm->pSynonym; pSyn; pSyn=pNext){
220849 pNext = pSyn->pSynonym;
220850 sqlite3Fts5IterClose(pSyn->pIter);
220851 fts5BufferFree((Fts5Buffer*)&pSyn[1]);
220852 sqlite3_free(p: pSyn);
220853 }
220854 }
220855 if( pPhrase->poslist.nSpace>0 ) fts5BufferFree(&pPhrase->poslist);
220856 sqlite3_free(p: pPhrase);
220857 }
220858}
220859
220860/*
220861** Set the "bFirst" flag on the first token of the phrase passed as the
220862** only argument.
220863*/
220864static void sqlite3Fts5ParseSetCaret(Fts5ExprPhrase *pPhrase){
220865 if( pPhrase && pPhrase->nTerm ){
220866 pPhrase->aTerm[0].bFirst = 1;
220867 }
220868}
220869
220870/*
220871** If argument pNear is NULL, then a new Fts5ExprNearset object is allocated
220872** and populated with pPhrase. Or, if pNear is not NULL, phrase pPhrase is
220873** appended to it and the results returned.
220874**
220875** If an OOM error occurs, both the pNear and pPhrase objects are freed and
220876** NULL returned.
220877*/
220878static Fts5ExprNearset *sqlite3Fts5ParseNearset(
220879 Fts5Parse *pParse, /* Parse context */
220880 Fts5ExprNearset *pNear, /* Existing nearset, or NULL */
220881 Fts5ExprPhrase *pPhrase /* Recently parsed phrase */
220882){
220883 const int SZALLOC = 8;
220884 Fts5ExprNearset *pRet = 0;
220885
220886 if( pParse->rc==SQLITE_OK ){
220887 if( pPhrase==0 ){
220888 return pNear;
220889 }
220890 if( pNear==0 ){
220891 sqlite3_int64 nByte;
220892 nByte = sizeof(Fts5ExprNearset) + SZALLOC * sizeof(Fts5ExprPhrase*);
220893 pRet = sqlite3_malloc64(n: nByte);
220894 if( pRet==0 ){
220895 pParse->rc = SQLITE_NOMEM;
220896 }else{
220897 memset(s: pRet, c: 0, n: (size_t)nByte);
220898 }
220899 }else if( (pNear->nPhrase % SZALLOC)==0 ){
220900 int nNew = pNear->nPhrase + SZALLOC;
220901 sqlite3_int64 nByte;
220902
220903 nByte = sizeof(Fts5ExprNearset) + nNew * sizeof(Fts5ExprPhrase*);
220904 pRet = (Fts5ExprNearset*)sqlite3_realloc64(pOld: pNear, n: nByte);
220905 if( pRet==0 ){
220906 pParse->rc = SQLITE_NOMEM;
220907 }
220908 }else{
220909 pRet = pNear;
220910 }
220911 }
220912
220913 if( pRet==0 ){
220914 assert( pParse->rc!=SQLITE_OK );
220915 sqlite3Fts5ParseNearsetFree(pNear);
220916 sqlite3Fts5ParsePhraseFree(pPhrase);
220917 }else{
220918 if( pRet->nPhrase>0 ){
220919 Fts5ExprPhrase *pLast = pRet->apPhrase[pRet->nPhrase-1];
220920 assert( pLast==pParse->apPhrase[pParse->nPhrase-2] );
220921 if( pPhrase->nTerm==0 ){
220922 fts5ExprPhraseFree(pPhrase);
220923 pRet->nPhrase--;
220924 pParse->nPhrase--;
220925 pPhrase = pLast;
220926 }else if( pLast->nTerm==0 ){
220927 fts5ExprPhraseFree(pPhrase: pLast);
220928 pParse->apPhrase[pParse->nPhrase-2] = pPhrase;
220929 pParse->nPhrase--;
220930 pRet->nPhrase--;
220931 }
220932 }
220933 pRet->apPhrase[pRet->nPhrase++] = pPhrase;
220934 }
220935 return pRet;
220936}
220937
220938typedef struct TokenCtx TokenCtx;
220939struct TokenCtx {
220940 Fts5ExprPhrase *pPhrase;
220941 int rc;
220942};
220943
220944/*
220945** Callback for tokenizing terms used by ParseTerm().
220946*/
220947static int fts5ParseTokenize(
220948 void *pContext, /* Pointer to Fts5InsertCtx object */
220949 int tflags, /* Mask of FTS5_TOKEN_* flags */
220950 const char *pToken, /* Buffer containing token */
220951 int nToken, /* Size of token in bytes */
220952 int iUnused1, /* Start offset of token */
220953 int iUnused2 /* End offset of token */
220954){
220955 int rc = SQLITE_OK;
220956 const int SZALLOC = 8;
220957 TokenCtx *pCtx = (TokenCtx*)pContext;
220958 Fts5ExprPhrase *pPhrase = pCtx->pPhrase;
220959
220960 UNUSED_PARAM2(iUnused1, iUnused2);
220961
220962 /* If an error has already occurred, this is a no-op */
220963 if( pCtx->rc!=SQLITE_OK ) return pCtx->rc;
220964 if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
220965
220966 if( pPhrase && pPhrase->nTerm>0 && (tflags & FTS5_TOKEN_COLOCATED) ){
220967 Fts5ExprTerm *pSyn;
220968 sqlite3_int64 nByte = sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer) + nToken+1;
220969 pSyn = (Fts5ExprTerm*)sqlite3_malloc64(n: nByte);
220970 if( pSyn==0 ){
220971 rc = SQLITE_NOMEM;
220972 }else{
220973 memset(s: pSyn, c: 0, n: (size_t)nByte);
220974 pSyn->zTerm = ((char*)pSyn) + sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer);
220975 memcpy(dest: pSyn->zTerm, src: pToken, n: nToken);
220976 pSyn->pSynonym = pPhrase->aTerm[pPhrase->nTerm-1].pSynonym;
220977 pPhrase->aTerm[pPhrase->nTerm-1].pSynonym = pSyn;
220978 }
220979 }else{
220980 Fts5ExprTerm *pTerm;
220981 if( pPhrase==0 || (pPhrase->nTerm % SZALLOC)==0 ){
220982 Fts5ExprPhrase *pNew;
220983 int nNew = SZALLOC + (pPhrase ? pPhrase->nTerm : 0);
220984
220985 pNew = (Fts5ExprPhrase*)sqlite3_realloc64(pOld: pPhrase,
220986 n: sizeof(Fts5ExprPhrase) + sizeof(Fts5ExprTerm) * nNew
220987 );
220988 if( pNew==0 ){
220989 rc = SQLITE_NOMEM;
220990 }else{
220991 if( pPhrase==0 ) memset(s: pNew, c: 0, n: sizeof(Fts5ExprPhrase));
220992 pCtx->pPhrase = pPhrase = pNew;
220993 pNew->nTerm = nNew - SZALLOC;
220994 }
220995 }
220996
220997 if( rc==SQLITE_OK ){
220998 pTerm = &pPhrase->aTerm[pPhrase->nTerm++];
220999 memset(s: pTerm, c: 0, n: sizeof(Fts5ExprTerm));
221000 pTerm->zTerm = sqlite3Fts5Strndup(pRc: &rc, pIn: pToken, nIn: nToken);
221001 }
221002 }
221003
221004 pCtx->rc = rc;
221005 return rc;
221006}
221007
221008
221009/*
221010** Free the phrase object passed as the only argument.
221011*/
221012static void sqlite3Fts5ParsePhraseFree(Fts5ExprPhrase *pPhrase){
221013 fts5ExprPhraseFree(pPhrase);
221014}
221015
221016/*
221017** Free the phrase object passed as the second argument.
221018*/
221019static void sqlite3Fts5ParseNearsetFree(Fts5ExprNearset *pNear){
221020 if( pNear ){
221021 int i;
221022 for(i=0; i<pNear->nPhrase; i++){
221023 fts5ExprPhraseFree(pPhrase: pNear->apPhrase[i]);
221024 }
221025 sqlite3_free(p: pNear->pColset);
221026 sqlite3_free(p: pNear);
221027 }
221028}
221029
221030static void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p){
221031 assert( pParse->pExpr==0 );
221032 pParse->pExpr = p;
221033}
221034
221035static int parseGrowPhraseArray(Fts5Parse *pParse){
221036 if( (pParse->nPhrase % 8)==0 ){
221037 sqlite3_int64 nByte = sizeof(Fts5ExprPhrase*) * (pParse->nPhrase + 8);
221038 Fts5ExprPhrase **apNew;
221039 apNew = (Fts5ExprPhrase**)sqlite3_realloc64(pOld: pParse->apPhrase, n: nByte);
221040 if( apNew==0 ){
221041 pParse->rc = SQLITE_NOMEM;
221042 return SQLITE_NOMEM;
221043 }
221044 pParse->apPhrase = apNew;
221045 }
221046 return SQLITE_OK;
221047}
221048
221049/*
221050** This function is called by the parser to process a string token. The
221051** string may or may not be quoted. In any case it is tokenized and a
221052** phrase object consisting of all tokens returned.
221053*/
221054static Fts5ExprPhrase *sqlite3Fts5ParseTerm(
221055 Fts5Parse *pParse, /* Parse context */
221056 Fts5ExprPhrase *pAppend, /* Phrase to append to */
221057 Fts5Token *pToken, /* String to tokenize */
221058 int bPrefix /* True if there is a trailing "*" */
221059){
221060 Fts5Config *pConfig = pParse->pConfig;
221061 TokenCtx sCtx; /* Context object passed to callback */
221062 int rc; /* Tokenize return code */
221063 char *z = 0;
221064
221065 memset(s: &sCtx, c: 0, n: sizeof(TokenCtx));
221066 sCtx.pPhrase = pAppend;
221067
221068 rc = fts5ParseStringFromToken(pToken, pz: &z);
221069 if( rc==SQLITE_OK ){
221070 int flags = FTS5_TOKENIZE_QUERY | (bPrefix ? FTS5_TOKENIZE_PREFIX : 0);
221071 int n;
221072 sqlite3Fts5Dequote(z);
221073 n = (int)strlen(s: z);
221074 rc = sqlite3Fts5Tokenize(pConfig, flags, pText: z, nText: n, pCtx: &sCtx, xToken: fts5ParseTokenize);
221075 }
221076 sqlite3_free(p: z);
221077 if( rc || (rc = sCtx.rc) ){
221078 pParse->rc = rc;
221079 fts5ExprPhraseFree(pPhrase: sCtx.pPhrase);
221080 sCtx.pPhrase = 0;
221081 }else{
221082
221083 if( pAppend==0 ){
221084 if( parseGrowPhraseArray(pParse) ){
221085 fts5ExprPhraseFree(pPhrase: sCtx.pPhrase);
221086 return 0;
221087 }
221088 pParse->nPhrase++;
221089 }
221090
221091 if( sCtx.pPhrase==0 ){
221092 /* This happens when parsing a token or quoted phrase that contains
221093 ** no token characters at all. (e.g ... MATCH '""'). */
221094 sCtx.pPhrase = sqlite3Fts5MallocZero(pRc: &pParse->rc, nByte: sizeof(Fts5ExprPhrase));
221095 }else if( sCtx.pPhrase->nTerm ){
221096 sCtx.pPhrase->aTerm[sCtx.pPhrase->nTerm-1].bPrefix = (u8)bPrefix;
221097 }
221098 pParse->apPhrase[pParse->nPhrase-1] = sCtx.pPhrase;
221099 }
221100
221101 return sCtx.pPhrase;
221102}
221103
221104/*
221105** Create a new FTS5 expression by cloning phrase iPhrase of the
221106** expression passed as the second argument.
221107*/
221108static int sqlite3Fts5ExprClonePhrase(
221109 Fts5Expr *pExpr,
221110 int iPhrase,
221111 Fts5Expr **ppNew
221112){
221113 int rc = SQLITE_OK; /* Return code */
221114 Fts5ExprPhrase *pOrig; /* The phrase extracted from pExpr */
221115 Fts5Expr *pNew = 0; /* Expression to return via *ppNew */
221116 TokenCtx sCtx = {0,0}; /* Context object for fts5ParseTokenize */
221117
221118 pOrig = pExpr->apExprPhrase[iPhrase];
221119 pNew = (Fts5Expr*)sqlite3Fts5MallocZero(pRc: &rc, nByte: sizeof(Fts5Expr));
221120 if( rc==SQLITE_OK ){
221121 pNew->apExprPhrase = (Fts5ExprPhrase**)sqlite3Fts5MallocZero(pRc: &rc,
221122 nByte: sizeof(Fts5ExprPhrase*));
221123 }
221124 if( rc==SQLITE_OK ){
221125 pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(pRc: &rc,
221126 nByte: sizeof(Fts5ExprNode));
221127 }
221128 if( rc==SQLITE_OK ){
221129 pNew->pRoot->pNear = (Fts5ExprNearset*)sqlite3Fts5MallocZero(pRc: &rc,
221130 nByte: sizeof(Fts5ExprNearset) + sizeof(Fts5ExprPhrase*));
221131 }
221132 if( rc==SQLITE_OK ){
221133 Fts5Colset *pColsetOrig = pOrig->pNode->pNear->pColset;
221134 if( pColsetOrig ){
221135 sqlite3_int64 nByte;
221136 Fts5Colset *pColset;
221137 nByte = sizeof(Fts5Colset) + (pColsetOrig->nCol-1) * sizeof(int);
221138 pColset = (Fts5Colset*)sqlite3Fts5MallocZero(pRc: &rc, nByte);
221139 if( pColset ){
221140 memcpy(dest: pColset, src: pColsetOrig, n: (size_t)nByte);
221141 }
221142 pNew->pRoot->pNear->pColset = pColset;
221143 }
221144 }
221145
221146 if( pOrig->nTerm ){
221147 int i; /* Used to iterate through phrase terms */
221148 for(i=0; rc==SQLITE_OK && i<pOrig->nTerm; i++){
221149 int tflags = 0;
221150 Fts5ExprTerm *p;
221151 for(p=&pOrig->aTerm[i]; p && rc==SQLITE_OK; p=p->pSynonym){
221152 const char *zTerm = p->zTerm;
221153 rc = fts5ParseTokenize(pContext: (void*)&sCtx, tflags, pToken: zTerm, nToken: (int)strlen(s: zTerm),
221154 iUnused1: 0, iUnused2: 0);
221155 tflags = FTS5_TOKEN_COLOCATED;
221156 }
221157 if( rc==SQLITE_OK ){
221158 sCtx.pPhrase->aTerm[i].bPrefix = pOrig->aTerm[i].bPrefix;
221159 sCtx.pPhrase->aTerm[i].bFirst = pOrig->aTerm[i].bFirst;
221160 }
221161 }
221162 }else{
221163 /* This happens when parsing a token or quoted phrase that contains
221164 ** no token characters at all. (e.g ... MATCH '""'). */
221165 sCtx.pPhrase = sqlite3Fts5MallocZero(pRc: &rc, nByte: sizeof(Fts5ExprPhrase));
221166 }
221167
221168 if( rc==SQLITE_OK && ALWAYS(sCtx.pPhrase) ){
221169 /* All the allocations succeeded. Put the expression object together. */
221170 pNew->pIndex = pExpr->pIndex;
221171 pNew->pConfig = pExpr->pConfig;
221172 pNew->nPhrase = 1;
221173 pNew->apExprPhrase[0] = sCtx.pPhrase;
221174 pNew->pRoot->pNear->apPhrase[0] = sCtx.pPhrase;
221175 pNew->pRoot->pNear->nPhrase = 1;
221176 sCtx.pPhrase->pNode = pNew->pRoot;
221177
221178 if( pOrig->nTerm==1
221179 && pOrig->aTerm[0].pSynonym==0
221180 && pOrig->aTerm[0].bFirst==0
221181 ){
221182 pNew->pRoot->eType = FTS5_TERM;
221183 pNew->pRoot->xNext = fts5ExprNodeNext_TERM;
221184 }else{
221185 pNew->pRoot->eType = FTS5_STRING;
221186 pNew->pRoot->xNext = fts5ExprNodeNext_STRING;
221187 }
221188 }else{
221189 sqlite3Fts5ExprFree(p: pNew);
221190 fts5ExprPhraseFree(pPhrase: sCtx.pPhrase);
221191 pNew = 0;
221192 }
221193
221194 *ppNew = pNew;
221195 return rc;
221196}
221197
221198
221199/*
221200** Token pTok has appeared in a MATCH expression where the NEAR operator
221201** is expected. If token pTok does not contain "NEAR", store an error
221202** in the pParse object.
221203*/
221204static void sqlite3Fts5ParseNear(Fts5Parse *pParse, Fts5Token *pTok){
221205 if( pTok->n!=4 || memcmp(s1: "NEAR", s2: pTok->p, n: 4) ){
221206 sqlite3Fts5ParseError(
221207 pParse, zFmt: "fts5: syntax error near \"%.*s\"", pTok->n, pTok->p
221208 );
221209 }
221210}
221211
221212static void sqlite3Fts5ParseSetDistance(
221213 Fts5Parse *pParse,
221214 Fts5ExprNearset *pNear,
221215 Fts5Token *p
221216){
221217 if( pNear ){
221218 int nNear = 0;
221219 int i;
221220 if( p->n ){
221221 for(i=0; i<p->n; i++){
221222 char c = (char)p->p[i];
221223 if( c<'0' || c>'9' ){
221224 sqlite3Fts5ParseError(
221225 pParse, zFmt: "expected integer, got \"%.*s\"", p->n, p->p
221226 );
221227 return;
221228 }
221229 nNear = nNear * 10 + (p->p[i] - '0');
221230 }
221231 }else{
221232 nNear = FTS5_DEFAULT_NEARDIST;
221233 }
221234 pNear->nNear = nNear;
221235 }
221236}
221237
221238/*
221239** The second argument passed to this function may be NULL, or it may be
221240** an existing Fts5Colset object. This function returns a pointer to
221241** a new colset object containing the contents of (p) with new value column
221242** number iCol appended.
221243**
221244** If an OOM error occurs, store an error code in pParse and return NULL.
221245** The old colset object (if any) is not freed in this case.
221246*/
221247static Fts5Colset *fts5ParseColset(
221248 Fts5Parse *pParse, /* Store SQLITE_NOMEM here if required */
221249 Fts5Colset *p, /* Existing colset object */
221250 int iCol /* New column to add to colset object */
221251){
221252 int nCol = p ? p->nCol : 0; /* Num. columns already in colset object */
221253 Fts5Colset *pNew; /* New colset object to return */
221254
221255 assert( pParse->rc==SQLITE_OK );
221256 assert( iCol>=0 && iCol<pParse->pConfig->nCol );
221257
221258 pNew = sqlite3_realloc64(pOld: p, n: sizeof(Fts5Colset) + sizeof(int)*nCol);
221259 if( pNew==0 ){
221260 pParse->rc = SQLITE_NOMEM;
221261 }else{
221262 int *aiCol = pNew->aiCol;
221263 int i, j;
221264 for(i=0; i<nCol; i++){
221265 if( aiCol[i]==iCol ) return pNew;
221266 if( aiCol[i]>iCol ) break;
221267 }
221268 for(j=nCol; j>i; j--){
221269 aiCol[j] = aiCol[j-1];
221270 }
221271 aiCol[i] = iCol;
221272 pNew->nCol = nCol+1;
221273
221274#ifndef NDEBUG
221275 /* Check that the array is in order and contains no duplicate entries. */
221276 for(i=1; i<pNew->nCol; i++) assert( pNew->aiCol[i]>pNew->aiCol[i-1] );
221277#endif
221278 }
221279
221280 return pNew;
221281}
221282
221283/*
221284** Allocate and return an Fts5Colset object specifying the inverse of
221285** the colset passed as the second argument. Free the colset passed
221286** as the second argument before returning.
221287*/
221288static Fts5Colset *sqlite3Fts5ParseColsetInvert(Fts5Parse *pParse, Fts5Colset *p){
221289 Fts5Colset *pRet;
221290 int nCol = pParse->pConfig->nCol;
221291
221292 pRet = (Fts5Colset*)sqlite3Fts5MallocZero(pRc: &pParse->rc,
221293 nByte: sizeof(Fts5Colset) + sizeof(int)*nCol
221294 );
221295 if( pRet ){
221296 int i;
221297 int iOld = 0;
221298 for(i=0; i<nCol; i++){
221299 if( iOld>=p->nCol || p->aiCol[iOld]!=i ){
221300 pRet->aiCol[pRet->nCol++] = i;
221301 }else{
221302 iOld++;
221303 }
221304 }
221305 }
221306
221307 sqlite3_free(p);
221308 return pRet;
221309}
221310
221311static Fts5Colset *sqlite3Fts5ParseColset(
221312 Fts5Parse *pParse, /* Store SQLITE_NOMEM here if required */
221313 Fts5Colset *pColset, /* Existing colset object */
221314 Fts5Token *p
221315){
221316 Fts5Colset *pRet = 0;
221317 int iCol;
221318 char *z; /* Dequoted copy of token p */
221319
221320 z = sqlite3Fts5Strndup(pRc: &pParse->rc, pIn: p->p, nIn: p->n);
221321 if( pParse->rc==SQLITE_OK ){
221322 Fts5Config *pConfig = pParse->pConfig;
221323 sqlite3Fts5Dequote(z);
221324 for(iCol=0; iCol<pConfig->nCol; iCol++){
221325 if( 0==sqlite3_stricmp(zLeft: pConfig->azCol[iCol], zRight: z) ) break;
221326 }
221327 if( iCol==pConfig->nCol ){
221328 sqlite3Fts5ParseError(pParse, zFmt: "no such column: %s", z);
221329 }else{
221330 pRet = fts5ParseColset(pParse, p: pColset, iCol);
221331 }
221332 sqlite3_free(p: z);
221333 }
221334
221335 if( pRet==0 ){
221336 assert( pParse->rc!=SQLITE_OK );
221337 sqlite3_free(p: pColset);
221338 }
221339
221340 return pRet;
221341}
221342
221343/*
221344** If argument pOrig is NULL, or if (*pRc) is set to anything other than
221345** SQLITE_OK when this function is called, NULL is returned.
221346**
221347** Otherwise, a copy of (*pOrig) is made into memory obtained from
221348** sqlite3Fts5MallocZero() and a pointer to it returned. If the allocation
221349** fails, (*pRc) is set to SQLITE_NOMEM and NULL is returned.
221350*/
221351static Fts5Colset *fts5CloneColset(int *pRc, Fts5Colset *pOrig){
221352 Fts5Colset *pRet;
221353 if( pOrig ){
221354 sqlite3_int64 nByte = sizeof(Fts5Colset) + (pOrig->nCol-1) * sizeof(int);
221355 pRet = (Fts5Colset*)sqlite3Fts5MallocZero(pRc, nByte);
221356 if( pRet ){
221357 memcpy(dest: pRet, src: pOrig, n: (size_t)nByte);
221358 }
221359 }else{
221360 pRet = 0;
221361 }
221362 return pRet;
221363}
221364
221365/*
221366** Remove from colset pColset any columns that are not also in colset pMerge.
221367*/
221368static void fts5MergeColset(Fts5Colset *pColset, Fts5Colset *pMerge){
221369 int iIn = 0; /* Next input in pColset */
221370 int iMerge = 0; /* Next input in pMerge */
221371 int iOut = 0; /* Next output slot in pColset */
221372
221373 while( iIn<pColset->nCol && iMerge<pMerge->nCol ){
221374 int iDiff = pColset->aiCol[iIn] - pMerge->aiCol[iMerge];
221375 if( iDiff==0 ){
221376 pColset->aiCol[iOut++] = pMerge->aiCol[iMerge];
221377 iMerge++;
221378 iIn++;
221379 }else if( iDiff>0 ){
221380 iMerge++;
221381 }else{
221382 iIn++;
221383 }
221384 }
221385 pColset->nCol = iOut;
221386}
221387
221388/*
221389** Recursively apply colset pColset to expression node pNode and all of
221390** its decendents. If (*ppFree) is not NULL, it contains a spare copy
221391** of pColset. This function may use the spare copy and set (*ppFree) to
221392** zero, or it may create copies of pColset using fts5CloneColset().
221393*/
221394static void fts5ParseSetColset(
221395 Fts5Parse *pParse,
221396 Fts5ExprNode *pNode,
221397 Fts5Colset *pColset,
221398 Fts5Colset **ppFree
221399){
221400 if( pParse->rc==SQLITE_OK ){
221401 assert( pNode->eType==FTS5_TERM || pNode->eType==FTS5_STRING
221402 || pNode->eType==FTS5_AND || pNode->eType==FTS5_OR
221403 || pNode->eType==FTS5_NOT || pNode->eType==FTS5_EOF
221404 );
221405 if( pNode->eType==FTS5_STRING || pNode->eType==FTS5_TERM ){
221406 Fts5ExprNearset *pNear = pNode->pNear;
221407 if( pNear->pColset ){
221408 fts5MergeColset(pColset: pNear->pColset, pMerge: pColset);
221409 if( pNear->pColset->nCol==0 ){
221410 pNode->eType = FTS5_EOF;
221411 pNode->xNext = 0;
221412 }
221413 }else if( *ppFree ){
221414 pNear->pColset = pColset;
221415 *ppFree = 0;
221416 }else{
221417 pNear->pColset = fts5CloneColset(pRc: &pParse->rc, pOrig: pColset);
221418 }
221419 }else{
221420 int i;
221421 assert( pNode->eType!=FTS5_EOF || pNode->nChild==0 );
221422 for(i=0; i<pNode->nChild; i++){
221423 fts5ParseSetColset(pParse, pNode: pNode->apChild[i], pColset, ppFree);
221424 }
221425 }
221426 }
221427}
221428
221429/*
221430** Apply colset pColset to expression node pExpr and all of its descendents.
221431*/
221432static void sqlite3Fts5ParseSetColset(
221433 Fts5Parse *pParse,
221434 Fts5ExprNode *pExpr,
221435 Fts5Colset *pColset
221436){
221437 Fts5Colset *pFree = pColset;
221438 if( pParse->pConfig->eDetail==FTS5_DETAIL_NONE ){
221439 sqlite3Fts5ParseError(pParse,
221440 zFmt: "fts5: column queries are not supported (detail=none)"
221441 );
221442 }else{
221443 fts5ParseSetColset(pParse, pNode: pExpr, pColset, ppFree: &pFree);
221444 }
221445 sqlite3_free(p: pFree);
221446}
221447
221448static void fts5ExprAssignXNext(Fts5ExprNode *pNode){
221449 switch( pNode->eType ){
221450 case FTS5_STRING: {
221451 Fts5ExprNearset *pNear = pNode->pNear;
221452 if( pNear->nPhrase==1 && pNear->apPhrase[0]->nTerm==1
221453 && pNear->apPhrase[0]->aTerm[0].pSynonym==0
221454 && pNear->apPhrase[0]->aTerm[0].bFirst==0
221455 ){
221456 pNode->eType = FTS5_TERM;
221457 pNode->xNext = fts5ExprNodeNext_TERM;
221458 }else{
221459 pNode->xNext = fts5ExprNodeNext_STRING;
221460 }
221461 break;
221462 };
221463
221464 case FTS5_OR: {
221465 pNode->xNext = fts5ExprNodeNext_OR;
221466 break;
221467 };
221468
221469 case FTS5_AND: {
221470 pNode->xNext = fts5ExprNodeNext_AND;
221471 break;
221472 };
221473
221474 default: assert( pNode->eType==FTS5_NOT ); {
221475 pNode->xNext = fts5ExprNodeNext_NOT;
221476 break;
221477 };
221478 }
221479}
221480
221481static void fts5ExprAddChildren(Fts5ExprNode *p, Fts5ExprNode *pSub){
221482 if( p->eType!=FTS5_NOT && pSub->eType==p->eType ){
221483 int nByte = sizeof(Fts5ExprNode*) * pSub->nChild;
221484 memcpy(dest: &p->apChild[p->nChild], src: pSub->apChild, n: nByte);
221485 p->nChild += pSub->nChild;
221486 sqlite3_free(p: pSub);
221487 }else{
221488 p->apChild[p->nChild++] = pSub;
221489 }
221490}
221491
221492/*
221493** This function is used when parsing LIKE or GLOB patterns against
221494** trigram indexes that specify either detail=column or detail=none.
221495** It converts a phrase:
221496**
221497** abc + def + ghi
221498**
221499** into an AND tree:
221500**
221501** abc AND def AND ghi
221502*/
221503static Fts5ExprNode *fts5ParsePhraseToAnd(
221504 Fts5Parse *pParse,
221505 Fts5ExprNearset *pNear
221506){
221507 int nTerm = pNear->apPhrase[0]->nTerm;
221508 int ii;
221509 int nByte;
221510 Fts5ExprNode *pRet;
221511
221512 assert( pNear->nPhrase==1 );
221513 assert( pParse->bPhraseToAnd );
221514
221515 nByte = sizeof(Fts5ExprNode) + nTerm*sizeof(Fts5ExprNode*);
221516 pRet = (Fts5ExprNode*)sqlite3Fts5MallocZero(pRc: &pParse->rc, nByte);
221517 if( pRet ){
221518 pRet->eType = FTS5_AND;
221519 pRet->nChild = nTerm;
221520 fts5ExprAssignXNext(pNode: pRet);
221521 pParse->nPhrase--;
221522 for(ii=0; ii<nTerm; ii++){
221523 Fts5ExprPhrase *pPhrase = (Fts5ExprPhrase*)sqlite3Fts5MallocZero(
221524 pRc: &pParse->rc, nByte: sizeof(Fts5ExprPhrase)
221525 );
221526 if( pPhrase ){
221527 if( parseGrowPhraseArray(pParse) ){
221528 fts5ExprPhraseFree(pPhrase);
221529 }else{
221530 pParse->apPhrase[pParse->nPhrase++] = pPhrase;
221531 pPhrase->nTerm = 1;
221532 pPhrase->aTerm[0].zTerm = sqlite3Fts5Strndup(
221533 pRc: &pParse->rc, pIn: pNear->apPhrase[0]->aTerm[ii].zTerm, nIn: -1
221534 );
221535 pRet->apChild[ii] = sqlite3Fts5ParseNode(pParse, FTS5_STRING,
221536 pLeft: 0, pRight: 0, pNear: sqlite3Fts5ParseNearset(pParse, pNear: 0, pPhrase)
221537 );
221538 }
221539 }
221540 }
221541
221542 if( pParse->rc ){
221543 sqlite3Fts5ParseNodeFree(p: pRet);
221544 pRet = 0;
221545 }else{
221546 sqlite3Fts5ParseNearsetFree(pNear);
221547 }
221548 }
221549
221550 return pRet;
221551}
221552
221553/*
221554** Allocate and return a new expression object. If anything goes wrong (i.e.
221555** OOM error), leave an error code in pParse and return NULL.
221556*/
221557static Fts5ExprNode *sqlite3Fts5ParseNode(
221558 Fts5Parse *pParse, /* Parse context */
221559 int eType, /* FTS5_STRING, AND, OR or NOT */
221560 Fts5ExprNode *pLeft, /* Left hand child expression */
221561 Fts5ExprNode *pRight, /* Right hand child expression */
221562 Fts5ExprNearset *pNear /* For STRING expressions, the near cluster */
221563){
221564 Fts5ExprNode *pRet = 0;
221565
221566 if( pParse->rc==SQLITE_OK ){
221567 int nChild = 0; /* Number of children of returned node */
221568 sqlite3_int64 nByte; /* Bytes of space to allocate for this node */
221569
221570 assert( (eType!=FTS5_STRING && !pNear)
221571 || (eType==FTS5_STRING && !pLeft && !pRight)
221572 );
221573 if( eType==FTS5_STRING && pNear==0 ) return 0;
221574 if( eType!=FTS5_STRING && pLeft==0 ) return pRight;
221575 if( eType!=FTS5_STRING && pRight==0 ) return pLeft;
221576
221577 if( eType==FTS5_STRING
221578 && pParse->bPhraseToAnd
221579 && pNear->apPhrase[0]->nTerm>1
221580 ){
221581 pRet = fts5ParsePhraseToAnd(pParse, pNear);
221582 }else{
221583 if( eType==FTS5_NOT ){
221584 nChild = 2;
221585 }else if( eType==FTS5_AND || eType==FTS5_OR ){
221586 nChild = 2;
221587 if( pLeft->eType==eType ) nChild += pLeft->nChild-1;
221588 if( pRight->eType==eType ) nChild += pRight->nChild-1;
221589 }
221590
221591 nByte = sizeof(Fts5ExprNode) + sizeof(Fts5ExprNode*)*(nChild-1);
221592 pRet = (Fts5ExprNode*)sqlite3Fts5MallocZero(pRc: &pParse->rc, nByte);
221593
221594 if( pRet ){
221595 pRet->eType = eType;
221596 pRet->pNear = pNear;
221597 fts5ExprAssignXNext(pNode: pRet);
221598 if( eType==FTS5_STRING ){
221599 int iPhrase;
221600 for(iPhrase=0; iPhrase<pNear->nPhrase; iPhrase++){
221601 pNear->apPhrase[iPhrase]->pNode = pRet;
221602 if( pNear->apPhrase[iPhrase]->nTerm==0 ){
221603 pRet->xNext = 0;
221604 pRet->eType = FTS5_EOF;
221605 }
221606 }
221607
221608 if( pParse->pConfig->eDetail!=FTS5_DETAIL_FULL ){
221609 Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
221610 if( pNear->nPhrase!=1
221611 || pPhrase->nTerm>1
221612 || (pPhrase->nTerm>0 && pPhrase->aTerm[0].bFirst)
221613 ){
221614 sqlite3Fts5ParseError(pParse,
221615 zFmt: "fts5: %s queries are not supported (detail!=full)",
221616 pNear->nPhrase==1 ? "phrase": "NEAR"
221617 );
221618 sqlite3_free(p: pRet);
221619 pRet = 0;
221620 }
221621 }
221622 }else{
221623 fts5ExprAddChildren(p: pRet, pSub: pLeft);
221624 fts5ExprAddChildren(p: pRet, pSub: pRight);
221625 }
221626 }
221627 }
221628 }
221629
221630 if( pRet==0 ){
221631 assert( pParse->rc!=SQLITE_OK );
221632 sqlite3Fts5ParseNodeFree(p: pLeft);
221633 sqlite3Fts5ParseNodeFree(p: pRight);
221634 sqlite3Fts5ParseNearsetFree(pNear);
221635 }
221636 return pRet;
221637}
221638
221639static Fts5ExprNode *sqlite3Fts5ParseImplicitAnd(
221640 Fts5Parse *pParse, /* Parse context */
221641 Fts5ExprNode *pLeft, /* Left hand child expression */
221642 Fts5ExprNode *pRight /* Right hand child expression */
221643){
221644 Fts5ExprNode *pRet = 0;
221645 Fts5ExprNode *pPrev;
221646
221647 if( pParse->rc ){
221648 sqlite3Fts5ParseNodeFree(p: pLeft);
221649 sqlite3Fts5ParseNodeFree(p: pRight);
221650 }else{
221651
221652 assert( pLeft->eType==FTS5_STRING
221653 || pLeft->eType==FTS5_TERM
221654 || pLeft->eType==FTS5_EOF
221655 || pLeft->eType==FTS5_AND
221656 );
221657 assert( pRight->eType==FTS5_STRING
221658 || pRight->eType==FTS5_TERM
221659 || pRight->eType==FTS5_EOF
221660 );
221661
221662 if( pLeft->eType==FTS5_AND ){
221663 pPrev = pLeft->apChild[pLeft->nChild-1];
221664 }else{
221665 pPrev = pLeft;
221666 }
221667 assert( pPrev->eType==FTS5_STRING
221668 || pPrev->eType==FTS5_TERM
221669 || pPrev->eType==FTS5_EOF
221670 );
221671
221672 if( pRight->eType==FTS5_EOF ){
221673 assert( pParse->apPhrase[pParse->nPhrase-1]==pRight->pNear->apPhrase[0] );
221674 sqlite3Fts5ParseNodeFree(p: pRight);
221675 pRet = pLeft;
221676 pParse->nPhrase--;
221677 }
221678 else if( pPrev->eType==FTS5_EOF ){
221679 Fts5ExprPhrase **ap;
221680
221681 if( pPrev==pLeft ){
221682 pRet = pRight;
221683 }else{
221684 pLeft->apChild[pLeft->nChild-1] = pRight;
221685 pRet = pLeft;
221686 }
221687
221688 ap = &pParse->apPhrase[pParse->nPhrase-1-pRight->pNear->nPhrase];
221689 assert( ap[0]==pPrev->pNear->apPhrase[0] );
221690 memmove(dest: ap, src: &ap[1], n: sizeof(Fts5ExprPhrase*)*pRight->pNear->nPhrase);
221691 pParse->nPhrase--;
221692
221693 sqlite3Fts5ParseNodeFree(p: pPrev);
221694 }
221695 else{
221696 pRet = sqlite3Fts5ParseNode(pParse, FTS5_AND, pLeft, pRight, pNear: 0);
221697 }
221698 }
221699
221700 return pRet;
221701}
221702
221703#ifdef SQLITE_TEST
221704static char *fts5ExprTermPrint(Fts5ExprTerm *pTerm){
221705 sqlite3_int64 nByte = 0;
221706 Fts5ExprTerm *p;
221707 char *zQuoted;
221708
221709 /* Determine the maximum amount of space required. */
221710 for(p=pTerm; p; p=p->pSynonym){
221711 nByte += (int)strlen(pTerm->zTerm) * 2 + 3 + 2;
221712 }
221713 zQuoted = sqlite3_malloc64(nByte);
221714
221715 if( zQuoted ){
221716 int i = 0;
221717 for(p=pTerm; p; p=p->pSynonym){
221718 char *zIn = p->zTerm;
221719 zQuoted[i++] = '"';
221720 while( *zIn ){
221721 if( *zIn=='"' ) zQuoted[i++] = '"';
221722 zQuoted[i++] = *zIn++;
221723 }
221724 zQuoted[i++] = '"';
221725 if( p->pSynonym ) zQuoted[i++] = '|';
221726 }
221727 if( pTerm->bPrefix ){
221728 zQuoted[i++] = ' ';
221729 zQuoted[i++] = '*';
221730 }
221731 zQuoted[i++] = '\0';
221732 }
221733 return zQuoted;
221734}
221735
221736static char *fts5PrintfAppend(char *zApp, const char *zFmt, ...){
221737 char *zNew;
221738 va_list ap;
221739 va_start(ap, zFmt);
221740 zNew = sqlite3_vmprintf(zFmt, ap);
221741 va_end(ap);
221742 if( zApp && zNew ){
221743 char *zNew2 = sqlite3_mprintf("%s%s", zApp, zNew);
221744 sqlite3_free(zNew);
221745 zNew = zNew2;
221746 }
221747 sqlite3_free(zApp);
221748 return zNew;
221749}
221750
221751/*
221752** Compose a tcl-readable representation of expression pExpr. Return a
221753** pointer to a buffer containing that representation. It is the
221754** responsibility of the caller to at some point free the buffer using
221755** sqlite3_free().
221756*/
221757static char *fts5ExprPrintTcl(
221758 Fts5Config *pConfig,
221759 const char *zNearsetCmd,
221760 Fts5ExprNode *pExpr
221761){
221762 char *zRet = 0;
221763 if( pExpr->eType==FTS5_STRING || pExpr->eType==FTS5_TERM ){
221764 Fts5ExprNearset *pNear = pExpr->pNear;
221765 int i;
221766 int iTerm;
221767
221768 zRet = fts5PrintfAppend(zRet, "%s ", zNearsetCmd);
221769 if( zRet==0 ) return 0;
221770 if( pNear->pColset ){
221771 int *aiCol = pNear->pColset->aiCol;
221772 int nCol = pNear->pColset->nCol;
221773 if( nCol==1 ){
221774 zRet = fts5PrintfAppend(zRet, "-col %d ", aiCol[0]);
221775 }else{
221776 zRet = fts5PrintfAppend(zRet, "-col {%d", aiCol[0]);
221777 for(i=1; i<pNear->pColset->nCol; i++){
221778 zRet = fts5PrintfAppend(zRet, " %d", aiCol[i]);
221779 }
221780 zRet = fts5PrintfAppend(zRet, "} ");
221781 }
221782 if( zRet==0 ) return 0;
221783 }
221784
221785 if( pNear->nPhrase>1 ){
221786 zRet = fts5PrintfAppend(zRet, "-near %d ", pNear->nNear);
221787 if( zRet==0 ) return 0;
221788 }
221789
221790 zRet = fts5PrintfAppend(zRet, "--");
221791 if( zRet==0 ) return 0;
221792
221793 for(i=0; i<pNear->nPhrase; i++){
221794 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
221795
221796 zRet = fts5PrintfAppend(zRet, " {");
221797 for(iTerm=0; zRet && iTerm<pPhrase->nTerm; iTerm++){
221798 char *zTerm = pPhrase->aTerm[iTerm].zTerm;
221799 zRet = fts5PrintfAppend(zRet, "%s%s", iTerm==0?"":" ", zTerm);
221800 if( pPhrase->aTerm[iTerm].bPrefix ){
221801 zRet = fts5PrintfAppend(zRet, "*");
221802 }
221803 }
221804
221805 if( zRet ) zRet = fts5PrintfAppend(zRet, "}");
221806 if( zRet==0 ) return 0;
221807 }
221808
221809 }else{
221810 char const *zOp = 0;
221811 int i;
221812 switch( pExpr->eType ){
221813 case FTS5_AND: zOp = "AND"; break;
221814 case FTS5_NOT: zOp = "NOT"; break;
221815 default:
221816 assert( pExpr->eType==FTS5_OR );
221817 zOp = "OR";
221818 break;
221819 }
221820
221821 zRet = sqlite3_mprintf("%s", zOp);
221822 for(i=0; zRet && i<pExpr->nChild; i++){
221823 char *z = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->apChild[i]);
221824 if( !z ){
221825 sqlite3_free(zRet);
221826 zRet = 0;
221827 }else{
221828 zRet = fts5PrintfAppend(zRet, " [%z]", z);
221829 }
221830 }
221831 }
221832
221833 return zRet;
221834}
221835
221836static char *fts5ExprPrint(Fts5Config *pConfig, Fts5ExprNode *pExpr){
221837 char *zRet = 0;
221838 if( pExpr->eType==0 ){
221839 return sqlite3_mprintf("\"\"");
221840 }else
221841 if( pExpr->eType==FTS5_STRING || pExpr->eType==FTS5_TERM ){
221842 Fts5ExprNearset *pNear = pExpr->pNear;
221843 int i;
221844 int iTerm;
221845
221846 if( pNear->pColset ){
221847 int ii;
221848 Fts5Colset *pColset = pNear->pColset;
221849 if( pColset->nCol>1 ) zRet = fts5PrintfAppend(zRet, "{");
221850 for(ii=0; ii<pColset->nCol; ii++){
221851 zRet = fts5PrintfAppend(zRet, "%s%s",
221852 pConfig->azCol[pColset->aiCol[ii]], ii==pColset->nCol-1 ? "" : " "
221853 );
221854 }
221855 if( zRet ){
221856 zRet = fts5PrintfAppend(zRet, "%s : ", pColset->nCol>1 ? "}" : "");
221857 }
221858 if( zRet==0 ) return 0;
221859 }
221860
221861 if( pNear->nPhrase>1 ){
221862 zRet = fts5PrintfAppend(zRet, "NEAR(");
221863 if( zRet==0 ) return 0;
221864 }
221865
221866 for(i=0; i<pNear->nPhrase; i++){
221867 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
221868 if( i!=0 ){
221869 zRet = fts5PrintfAppend(zRet, " ");
221870 if( zRet==0 ) return 0;
221871 }
221872 for(iTerm=0; iTerm<pPhrase->nTerm; iTerm++){
221873 char *zTerm = fts5ExprTermPrint(&pPhrase->aTerm[iTerm]);
221874 if( zTerm ){
221875 zRet = fts5PrintfAppend(zRet, "%s%s", iTerm==0?"":" + ", zTerm);
221876 sqlite3_free(zTerm);
221877 }
221878 if( zTerm==0 || zRet==0 ){
221879 sqlite3_free(zRet);
221880 return 0;
221881 }
221882 }
221883 }
221884
221885 if( pNear->nPhrase>1 ){
221886 zRet = fts5PrintfAppend(zRet, ", %d)", pNear->nNear);
221887 if( zRet==0 ) return 0;
221888 }
221889
221890 }else{
221891 char const *zOp = 0;
221892 int i;
221893
221894 switch( pExpr->eType ){
221895 case FTS5_AND: zOp = " AND "; break;
221896 case FTS5_NOT: zOp = " NOT "; break;
221897 default:
221898 assert( pExpr->eType==FTS5_OR );
221899 zOp = " OR ";
221900 break;
221901 }
221902
221903 for(i=0; i<pExpr->nChild; i++){
221904 char *z = fts5ExprPrint(pConfig, pExpr->apChild[i]);
221905 if( z==0 ){
221906 sqlite3_free(zRet);
221907 zRet = 0;
221908 }else{
221909 int e = pExpr->apChild[i]->eType;
221910 int b = (e!=FTS5_STRING && e!=FTS5_TERM && e!=FTS5_EOF);
221911 zRet = fts5PrintfAppend(zRet, "%s%s%z%s",
221912 (i==0 ? "" : zOp),
221913 (b?"(":""), z, (b?")":"")
221914 );
221915 }
221916 if( zRet==0 ) break;
221917 }
221918 }
221919
221920 return zRet;
221921}
221922
221923/*
221924** The implementation of user-defined scalar functions fts5_expr() (bTcl==0)
221925** and fts5_expr_tcl() (bTcl!=0).
221926*/
221927static void fts5ExprFunction(
221928 sqlite3_context *pCtx, /* Function call context */
221929 int nArg, /* Number of args */
221930 sqlite3_value **apVal, /* Function arguments */
221931 int bTcl
221932){
221933 Fts5Global *pGlobal = (Fts5Global*)sqlite3_user_data(pCtx);
221934 sqlite3 *db = sqlite3_context_db_handle(pCtx);
221935 const char *zExpr = 0;
221936 char *zErr = 0;
221937 Fts5Expr *pExpr = 0;
221938 int rc;
221939 int i;
221940
221941 const char **azConfig; /* Array of arguments for Fts5Config */
221942 const char *zNearsetCmd = "nearset";
221943 int nConfig; /* Size of azConfig[] */
221944 Fts5Config *pConfig = 0;
221945 int iArg = 1;
221946
221947 if( nArg<1 ){
221948 zErr = sqlite3_mprintf("wrong number of arguments to function %s",
221949 bTcl ? "fts5_expr_tcl" : "fts5_expr"
221950 );
221951 sqlite3_result_error(pCtx, zErr, -1);
221952 sqlite3_free(zErr);
221953 return;
221954 }
221955
221956 if( bTcl && nArg>1 ){
221957 zNearsetCmd = (const char*)sqlite3_value_text(apVal[1]);
221958 iArg = 2;
221959 }
221960
221961 nConfig = 3 + (nArg-iArg);
221962 azConfig = (const char**)sqlite3_malloc64(sizeof(char*) * nConfig);
221963 if( azConfig==0 ){
221964 sqlite3_result_error_nomem(pCtx);
221965 return;
221966 }
221967 azConfig[0] = 0;
221968 azConfig[1] = "main";
221969 azConfig[2] = "tbl";
221970 for(i=3; iArg<nArg; iArg++){
221971 const char *z = (const char*)sqlite3_value_text(apVal[iArg]);
221972 azConfig[i++] = (z ? z : "");
221973 }
221974
221975 zExpr = (const char*)sqlite3_value_text(apVal[0]);
221976 if( zExpr==0 ) zExpr = "";
221977
221978 rc = sqlite3Fts5ConfigParse(pGlobal, db, nConfig, azConfig, &pConfig, &zErr);
221979 if( rc==SQLITE_OK ){
221980 rc = sqlite3Fts5ExprNew(pConfig, 0, pConfig->nCol, zExpr, &pExpr, &zErr);
221981 }
221982 if( rc==SQLITE_OK ){
221983 char *zText;
221984 if( pExpr->pRoot->xNext==0 ){
221985 zText = sqlite3_mprintf("");
221986 }else if( bTcl ){
221987 zText = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->pRoot);
221988 }else{
221989 zText = fts5ExprPrint(pConfig, pExpr->pRoot);
221990 }
221991 if( zText==0 ){
221992 rc = SQLITE_NOMEM;
221993 }else{
221994 sqlite3_result_text(pCtx, zText, -1, SQLITE_TRANSIENT);
221995 sqlite3_free(zText);
221996 }
221997 }
221998
221999 if( rc!=SQLITE_OK ){
222000 if( zErr ){
222001 sqlite3_result_error(pCtx, zErr, -1);
222002 sqlite3_free(zErr);
222003 }else{
222004 sqlite3_result_error_code(pCtx, rc);
222005 }
222006 }
222007 sqlite3_free((void *)azConfig);
222008 sqlite3Fts5ConfigFree(pConfig);
222009 sqlite3Fts5ExprFree(pExpr);
222010}
222011
222012static void fts5ExprFunctionHr(
222013 sqlite3_context *pCtx, /* Function call context */
222014 int nArg, /* Number of args */
222015 sqlite3_value **apVal /* Function arguments */
222016){
222017 fts5ExprFunction(pCtx, nArg, apVal, 0);
222018}
222019static void fts5ExprFunctionTcl(
222020 sqlite3_context *pCtx, /* Function call context */
222021 int nArg, /* Number of args */
222022 sqlite3_value **apVal /* Function arguments */
222023){
222024 fts5ExprFunction(pCtx, nArg, apVal, 1);
222025}
222026
222027/*
222028** The implementation of an SQLite user-defined-function that accepts a
222029** single integer as an argument. If the integer is an alpha-numeric
222030** unicode code point, 1 is returned. Otherwise 0.
222031*/
222032static void fts5ExprIsAlnum(
222033 sqlite3_context *pCtx, /* Function call context */
222034 int nArg, /* Number of args */
222035 sqlite3_value **apVal /* Function arguments */
222036){
222037 int iCode;
222038 u8 aArr[32];
222039 if( nArg!=1 ){
222040 sqlite3_result_error(pCtx,
222041 "wrong number of arguments to function fts5_isalnum", -1
222042 );
222043 return;
222044 }
222045 memset(aArr, 0, sizeof(aArr));
222046 sqlite3Fts5UnicodeCatParse("L*", aArr);
222047 sqlite3Fts5UnicodeCatParse("N*", aArr);
222048 sqlite3Fts5UnicodeCatParse("Co", aArr);
222049 iCode = sqlite3_value_int(apVal[0]);
222050 sqlite3_result_int(pCtx, aArr[sqlite3Fts5UnicodeCategory((u32)iCode)]);
222051}
222052
222053static void fts5ExprFold(
222054 sqlite3_context *pCtx, /* Function call context */
222055 int nArg, /* Number of args */
222056 sqlite3_value **apVal /* Function arguments */
222057){
222058 if( nArg!=1 && nArg!=2 ){
222059 sqlite3_result_error(pCtx,
222060 "wrong number of arguments to function fts5_fold", -1
222061 );
222062 }else{
222063 int iCode;
222064 int bRemoveDiacritics = 0;
222065 iCode = sqlite3_value_int(apVal[0]);
222066 if( nArg==2 ) bRemoveDiacritics = sqlite3_value_int(apVal[1]);
222067 sqlite3_result_int(pCtx, sqlite3Fts5UnicodeFold(iCode, bRemoveDiacritics));
222068 }
222069}
222070#endif /* ifdef SQLITE_TEST */
222071
222072/*
222073** This is called during initialization to register the fts5_expr() scalar
222074** UDF with the SQLite handle passed as the only argument.
222075*/
222076static int sqlite3Fts5ExprInit(Fts5Global *pGlobal, sqlite3 *db){
222077#ifdef SQLITE_TEST
222078 struct Fts5ExprFunc {
222079 const char *z;
222080 void (*x)(sqlite3_context*,int,sqlite3_value**);
222081 } aFunc[] = {
222082 { "fts5_expr", fts5ExprFunctionHr },
222083 { "fts5_expr_tcl", fts5ExprFunctionTcl },
222084 { "fts5_isalnum", fts5ExprIsAlnum },
222085 { "fts5_fold", fts5ExprFold },
222086 };
222087 int i;
222088 int rc = SQLITE_OK;
222089 void *pCtx = (void*)pGlobal;
222090
222091 for(i=0; rc==SQLITE_OK && i<ArraySize(aFunc); i++){
222092 struct Fts5ExprFunc *p = &aFunc[i];
222093 rc = sqlite3_create_function(db, p->z, -1, SQLITE_UTF8, pCtx, p->x, 0, 0);
222094 }
222095#else
222096 int rc = SQLITE_OK;
222097 UNUSED_PARAM2(pGlobal,db);
222098#endif
222099
222100 /* Avoid warnings indicating that sqlite3Fts5ParserTrace() and
222101 ** sqlite3Fts5ParserFallback() are unused */
222102#ifndef NDEBUG
222103 (void)sqlite3Fts5ParserTrace;
222104#endif
222105 (void)sqlite3Fts5ParserFallback;
222106
222107 return rc;
222108}
222109
222110/*
222111** Return the number of phrases in expression pExpr.
222112*/
222113static int sqlite3Fts5ExprPhraseCount(Fts5Expr *pExpr){
222114 return (pExpr ? pExpr->nPhrase : 0);
222115}
222116
222117/*
222118** Return the number of terms in the iPhrase'th phrase in pExpr.
222119*/
222120static int sqlite3Fts5ExprPhraseSize(Fts5Expr *pExpr, int iPhrase){
222121 if( iPhrase<0 || iPhrase>=pExpr->nPhrase ) return 0;
222122 return pExpr->apExprPhrase[iPhrase]->nTerm;
222123}
222124
222125/*
222126** This function is used to access the current position list for phrase
222127** iPhrase.
222128*/
222129static int sqlite3Fts5ExprPoslist(Fts5Expr *pExpr, int iPhrase, const u8 **pa){
222130 int nRet;
222131 Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase];
222132 Fts5ExprNode *pNode = pPhrase->pNode;
222133 if( pNode->bEof==0 && pNode->iRowid==pExpr->pRoot->iRowid ){
222134 *pa = pPhrase->poslist.p;
222135 nRet = pPhrase->poslist.n;
222136 }else{
222137 *pa = 0;
222138 nRet = 0;
222139 }
222140 return nRet;
222141}
222142
222143struct Fts5PoslistPopulator {
222144 Fts5PoslistWriter writer;
222145 int bOk; /* True if ok to populate */
222146 int bMiss;
222147};
222148
222149/*
222150** Clear the position lists associated with all phrases in the expression
222151** passed as the first argument. Argument bLive is true if the expression
222152** might be pointing to a real entry, otherwise it has just been reset.
222153**
222154** At present this function is only used for detail=col and detail=none
222155** fts5 tables. This implies that all phrases must be at most 1 token
222156** in size, as phrase matches are not supported without detail=full.
222157*/
222158static Fts5PoslistPopulator *sqlite3Fts5ExprClearPoslists(Fts5Expr *pExpr, int bLive){
222159 Fts5PoslistPopulator *pRet;
222160 pRet = sqlite3_malloc64(n: sizeof(Fts5PoslistPopulator)*pExpr->nPhrase);
222161 if( pRet ){
222162 int i;
222163 memset(s: pRet, c: 0, n: sizeof(Fts5PoslistPopulator)*pExpr->nPhrase);
222164 for(i=0; i<pExpr->nPhrase; i++){
222165 Fts5Buffer *pBuf = &pExpr->apExprPhrase[i]->poslist;
222166 Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode;
222167 assert( pExpr->apExprPhrase[i]->nTerm<=1 );
222168 if( bLive &&
222169 (pBuf->n==0 || pNode->iRowid!=pExpr->pRoot->iRowid || pNode->bEof)
222170 ){
222171 pRet[i].bMiss = 1;
222172 }else{
222173 pBuf->n = 0;
222174 }
222175 }
222176 }
222177 return pRet;
222178}
222179
222180struct Fts5ExprCtx {
222181 Fts5Expr *pExpr;
222182 Fts5PoslistPopulator *aPopulator;
222183 i64 iOff;
222184};
222185typedef struct Fts5ExprCtx Fts5ExprCtx;
222186
222187/*
222188** TODO: Make this more efficient!
222189*/
222190static int fts5ExprColsetTest(Fts5Colset *pColset, int iCol){
222191 int i;
222192 for(i=0; i<pColset->nCol; i++){
222193 if( pColset->aiCol[i]==iCol ) return 1;
222194 }
222195 return 0;
222196}
222197
222198static int fts5ExprPopulatePoslistsCb(
222199 void *pCtx, /* Copy of 2nd argument to xTokenize() */
222200 int tflags, /* Mask of FTS5_TOKEN_* flags */
222201 const char *pToken, /* Pointer to buffer containing token */
222202 int nToken, /* Size of token in bytes */
222203 int iUnused1, /* Byte offset of token within input text */
222204 int iUnused2 /* Byte offset of end of token within input text */
222205){
222206 Fts5ExprCtx *p = (Fts5ExprCtx*)pCtx;
222207 Fts5Expr *pExpr = p->pExpr;
222208 int i;
222209
222210 UNUSED_PARAM2(iUnused1, iUnused2);
222211
222212 if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
222213 if( (tflags & FTS5_TOKEN_COLOCATED)==0 ) p->iOff++;
222214 for(i=0; i<pExpr->nPhrase; i++){
222215 Fts5ExprTerm *pTerm;
222216 if( p->aPopulator[i].bOk==0 ) continue;
222217 for(pTerm=&pExpr->apExprPhrase[i]->aTerm[0]; pTerm; pTerm=pTerm->pSynonym){
222218 int nTerm = (int)strlen(s: pTerm->zTerm);
222219 if( (nTerm==nToken || (nTerm<nToken && pTerm->bPrefix))
222220 && memcmp(s1: pTerm->zTerm, s2: pToken, n: nTerm)==0
222221 ){
222222 int rc = sqlite3Fts5PoslistWriterAppend(
222223 pBuf: &pExpr->apExprPhrase[i]->poslist, pWriter: &p->aPopulator[i].writer, iPos: p->iOff
222224 );
222225 if( rc ) return rc;
222226 break;
222227 }
222228 }
222229 }
222230 return SQLITE_OK;
222231}
222232
222233static int sqlite3Fts5ExprPopulatePoslists(
222234 Fts5Config *pConfig,
222235 Fts5Expr *pExpr,
222236 Fts5PoslistPopulator *aPopulator,
222237 int iCol,
222238 const char *z, int n
222239){
222240 int i;
222241 Fts5ExprCtx sCtx;
222242 sCtx.pExpr = pExpr;
222243 sCtx.aPopulator = aPopulator;
222244 sCtx.iOff = (((i64)iCol) << 32) - 1;
222245
222246 for(i=0; i<pExpr->nPhrase; i++){
222247 Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode;
222248 Fts5Colset *pColset = pNode->pNear->pColset;
222249 if( (pColset && 0==fts5ExprColsetTest(pColset, iCol))
222250 || aPopulator[i].bMiss
222251 ){
222252 aPopulator[i].bOk = 0;
222253 }else{
222254 aPopulator[i].bOk = 1;
222255 }
222256 }
222257
222258 return sqlite3Fts5Tokenize(pConfig,
222259 FTS5_TOKENIZE_DOCUMENT, pText: z, nText: n, pCtx: (void*)&sCtx, xToken: fts5ExprPopulatePoslistsCb
222260 );
222261}
222262
222263static void fts5ExprClearPoslists(Fts5ExprNode *pNode){
222264 if( pNode->eType==FTS5_TERM || pNode->eType==FTS5_STRING ){
222265 pNode->pNear->apPhrase[0]->poslist.n = 0;
222266 }else{
222267 int i;
222268 for(i=0; i<pNode->nChild; i++){
222269 fts5ExprClearPoslists(pNode: pNode->apChild[i]);
222270 }
222271 }
222272}
222273
222274static int fts5ExprCheckPoslists(Fts5ExprNode *pNode, i64 iRowid){
222275 pNode->iRowid = iRowid;
222276 pNode->bEof = 0;
222277 switch( pNode->eType ){
222278 case FTS5_TERM:
222279 case FTS5_STRING:
222280 return (pNode->pNear->apPhrase[0]->poslist.n>0);
222281
222282 case FTS5_AND: {
222283 int i;
222284 for(i=0; i<pNode->nChild; i++){
222285 if( fts5ExprCheckPoslists(pNode: pNode->apChild[i], iRowid)==0 ){
222286 fts5ExprClearPoslists(pNode);
222287 return 0;
222288 }
222289 }
222290 break;
222291 }
222292
222293 case FTS5_OR: {
222294 int i;
222295 int bRet = 0;
222296 for(i=0; i<pNode->nChild; i++){
222297 if( fts5ExprCheckPoslists(pNode: pNode->apChild[i], iRowid) ){
222298 bRet = 1;
222299 }
222300 }
222301 return bRet;
222302 }
222303
222304 default: {
222305 assert( pNode->eType==FTS5_NOT );
222306 if( 0==fts5ExprCheckPoslists(pNode: pNode->apChild[0], iRowid)
222307 || 0!=fts5ExprCheckPoslists(pNode: pNode->apChild[1], iRowid)
222308 ){
222309 fts5ExprClearPoslists(pNode);
222310 return 0;
222311 }
222312 break;
222313 }
222314 }
222315 return 1;
222316}
222317
222318static void sqlite3Fts5ExprCheckPoslists(Fts5Expr *pExpr, i64 iRowid){
222319 fts5ExprCheckPoslists(pNode: pExpr->pRoot, iRowid);
222320}
222321
222322/*
222323** This function is only called for detail=columns tables.
222324*/
222325static int sqlite3Fts5ExprPhraseCollist(
222326 Fts5Expr *pExpr,
222327 int iPhrase,
222328 const u8 **ppCollist,
222329 int *pnCollist
222330){
222331 Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase];
222332 Fts5ExprNode *pNode = pPhrase->pNode;
222333 int rc = SQLITE_OK;
222334
222335 assert( iPhrase>=0 && iPhrase<pExpr->nPhrase );
222336 assert( pExpr->pConfig->eDetail==FTS5_DETAIL_COLUMNS );
222337
222338 if( pNode->bEof==0
222339 && pNode->iRowid==pExpr->pRoot->iRowid
222340 && pPhrase->poslist.n>0
222341 ){
222342 Fts5ExprTerm *pTerm = &pPhrase->aTerm[0];
222343 if( pTerm->pSynonym ){
222344 Fts5Buffer *pBuf = (Fts5Buffer*)&pTerm->pSynonym[1];
222345 rc = fts5ExprSynonymList(
222346 pTerm, iRowid: pNode->iRowid, pBuf, pa: (u8**)ppCollist, pn: pnCollist
222347 );
222348 }else{
222349 *ppCollist = pPhrase->aTerm[0].pIter->pData;
222350 *pnCollist = pPhrase->aTerm[0].pIter->nData;
222351 }
222352 }else{
222353 *ppCollist = 0;
222354 *pnCollist = 0;
222355 }
222356
222357 return rc;
222358}
222359
222360/*
222361** 2014 August 11
222362**
222363** The author disclaims copyright to this source code. In place of
222364** a legal notice, here is a blessing:
222365**
222366** May you do good and not evil.
222367** May you find forgiveness for yourself and forgive others.
222368** May you share freely, never taking more than you give.
222369**
222370******************************************************************************
222371**
222372*/
222373
222374
222375
222376/* #include "fts5Int.h" */
222377
222378typedef struct Fts5HashEntry Fts5HashEntry;
222379
222380/*
222381** This file contains the implementation of an in-memory hash table used
222382** to accumuluate "term -> doclist" content before it is flused to a level-0
222383** segment.
222384*/
222385
222386
222387struct Fts5Hash {
222388 int eDetail; /* Copy of Fts5Config.eDetail */
222389 int *pnByte; /* Pointer to bytes counter */
222390 int nEntry; /* Number of entries currently in hash */
222391 int nSlot; /* Size of aSlot[] array */
222392 Fts5HashEntry *pScan; /* Current ordered scan item */
222393 Fts5HashEntry **aSlot; /* Array of hash slots */
222394};
222395
222396/*
222397** Each entry in the hash table is represented by an object of the
222398** following type. Each object, its key (a nul-terminated string) and
222399** its current data are stored in a single memory allocation. The
222400** key immediately follows the object in memory. The position list
222401** data immediately follows the key data in memory.
222402**
222403** The data that follows the key is in a similar, but not identical format
222404** to the doclist data stored in the database. It is:
222405**
222406** * Rowid, as a varint
222407** * Position list, without 0x00 terminator.
222408** * Size of previous position list and rowid, as a 4 byte
222409** big-endian integer.
222410**
222411** iRowidOff:
222412** Offset of last rowid written to data area. Relative to first byte of
222413** structure.
222414**
222415** nData:
222416** Bytes of data written since iRowidOff.
222417*/
222418struct Fts5HashEntry {
222419 Fts5HashEntry *pHashNext; /* Next hash entry with same hash-key */
222420 Fts5HashEntry *pScanNext; /* Next entry in sorted order */
222421
222422 int nAlloc; /* Total size of allocation */
222423 int iSzPoslist; /* Offset of space for 4-byte poslist size */
222424 int nData; /* Total bytes of data (incl. structure) */
222425 int nKey; /* Length of key in bytes */
222426 u8 bDel; /* Set delete-flag @ iSzPoslist */
222427 u8 bContent; /* Set content-flag (detail=none mode) */
222428 i16 iCol; /* Column of last value written */
222429 int iPos; /* Position of last value written */
222430 i64 iRowid; /* Rowid of last value written */
222431};
222432
222433/*
222434** Eqivalent to:
222435**
222436** char *fts5EntryKey(Fts5HashEntry *pEntry){ return zKey; }
222437*/
222438#define fts5EntryKey(p) ( ((char *)(&(p)[1])) )
222439
222440
222441/*
222442** Allocate a new hash table.
222443*/
222444static int sqlite3Fts5HashNew(Fts5Config *pConfig, Fts5Hash **ppNew, int *pnByte){
222445 int rc = SQLITE_OK;
222446 Fts5Hash *pNew;
222447
222448 *ppNew = pNew = (Fts5Hash*)sqlite3_malloc(n: sizeof(Fts5Hash));
222449 if( pNew==0 ){
222450 rc = SQLITE_NOMEM;
222451 }else{
222452 sqlite3_int64 nByte;
222453 memset(s: pNew, c: 0, n: sizeof(Fts5Hash));
222454 pNew->pnByte = pnByte;
222455 pNew->eDetail = pConfig->eDetail;
222456
222457 pNew->nSlot = 1024;
222458 nByte = sizeof(Fts5HashEntry*) * pNew->nSlot;
222459 pNew->aSlot = (Fts5HashEntry**)sqlite3_malloc64(n: nByte);
222460 if( pNew->aSlot==0 ){
222461 sqlite3_free(p: pNew);
222462 *ppNew = 0;
222463 rc = SQLITE_NOMEM;
222464 }else{
222465 memset(s: pNew->aSlot, c: 0, n: (size_t)nByte);
222466 }
222467 }
222468 return rc;
222469}
222470
222471/*
222472** Free a hash table object.
222473*/
222474static void sqlite3Fts5HashFree(Fts5Hash *pHash){
222475 if( pHash ){
222476 sqlite3Fts5HashClear(pHash);
222477 sqlite3_free(p: pHash->aSlot);
222478 sqlite3_free(p: pHash);
222479 }
222480}
222481
222482/*
222483** Empty (but do not delete) a hash table.
222484*/
222485static void sqlite3Fts5HashClear(Fts5Hash *pHash){
222486 int i;
222487 for(i=0; i<pHash->nSlot; i++){
222488 Fts5HashEntry *pNext;
222489 Fts5HashEntry *pSlot;
222490 for(pSlot=pHash->aSlot[i]; pSlot; pSlot=pNext){
222491 pNext = pSlot->pHashNext;
222492 sqlite3_free(p: pSlot);
222493 }
222494 }
222495 memset(s: pHash->aSlot, c: 0, n: pHash->nSlot * sizeof(Fts5HashEntry*));
222496 pHash->nEntry = 0;
222497}
222498
222499static unsigned int fts5HashKey(int nSlot, const u8 *p, int n){
222500 int i;
222501 unsigned int h = 13;
222502 for(i=n-1; i>=0; i--){
222503 h = (h << 3) ^ h ^ p[i];
222504 }
222505 return (h % nSlot);
222506}
222507
222508static unsigned int fts5HashKey2(int nSlot, u8 b, const u8 *p, int n){
222509 int i;
222510 unsigned int h = 13;
222511 for(i=n-1; i>=0; i--){
222512 h = (h << 3) ^ h ^ p[i];
222513 }
222514 h = (h << 3) ^ h ^ b;
222515 return (h % nSlot);
222516}
222517
222518/*
222519** Resize the hash table by doubling the number of slots.
222520*/
222521static int fts5HashResize(Fts5Hash *pHash){
222522 int nNew = pHash->nSlot*2;
222523 int i;
222524 Fts5HashEntry **apNew;
222525 Fts5HashEntry **apOld = pHash->aSlot;
222526
222527 apNew = (Fts5HashEntry**)sqlite3_malloc64(n: nNew*sizeof(Fts5HashEntry*));
222528 if( !apNew ) return SQLITE_NOMEM;
222529 memset(s: apNew, c: 0, n: nNew*sizeof(Fts5HashEntry*));
222530
222531 for(i=0; i<pHash->nSlot; i++){
222532 while( apOld[i] ){
222533 unsigned int iHash;
222534 Fts5HashEntry *p = apOld[i];
222535 apOld[i] = p->pHashNext;
222536 iHash = fts5HashKey(nSlot: nNew, p: (u8*)fts5EntryKey(p),
222537 n: (int)strlen(fts5EntryKey(p)));
222538 p->pHashNext = apNew[iHash];
222539 apNew[iHash] = p;
222540 }
222541 }
222542
222543 sqlite3_free(p: apOld);
222544 pHash->nSlot = nNew;
222545 pHash->aSlot = apNew;
222546 return SQLITE_OK;
222547}
222548
222549static int fts5HashAddPoslistSize(
222550 Fts5Hash *pHash,
222551 Fts5HashEntry *p,
222552 Fts5HashEntry *p2
222553){
222554 int nRet = 0;
222555 if( p->iSzPoslist ){
222556 u8 *pPtr = p2 ? (u8*)p2 : (u8*)p;
222557 int nData = p->nData;
222558 if( pHash->eDetail==FTS5_DETAIL_NONE ){
222559 assert( nData==p->iSzPoslist );
222560 if( p->bDel ){
222561 pPtr[nData++] = 0x00;
222562 if( p->bContent ){
222563 pPtr[nData++] = 0x00;
222564 }
222565 }
222566 }else{
222567 int nSz = (nData - p->iSzPoslist - 1); /* Size in bytes */
222568 int nPos = nSz*2 + p->bDel; /* Value of nPos field */
222569
222570 assert( p->bDel==0 || p->bDel==1 );
222571 if( nPos<=127 ){
222572 pPtr[p->iSzPoslist] = (u8)nPos;
222573 }else{
222574 int nByte = sqlite3Fts5GetVarintLen(iVal: (u32)nPos);
222575 memmove(dest: &pPtr[p->iSzPoslist + nByte], src: &pPtr[p->iSzPoslist + 1], n: nSz);
222576 sqlite3Fts5PutVarint(p: &pPtr[p->iSzPoslist], v: nPos);
222577 nData += (nByte-1);
222578 }
222579 }
222580
222581 nRet = nData - p->nData;
222582 if( p2==0 ){
222583 p->iSzPoslist = 0;
222584 p->bDel = 0;
222585 p->bContent = 0;
222586 p->nData = nData;
222587 }
222588 }
222589 return nRet;
222590}
222591
222592/*
222593** Add an entry to the in-memory hash table. The key is the concatenation
222594** of bByte and (pToken/nToken). The value is (iRowid/iCol/iPos).
222595**
222596** (bByte || pToken) -> (iRowid,iCol,iPos)
222597**
222598** Or, if iCol is negative, then the value is a delete marker.
222599*/
222600static int sqlite3Fts5HashWrite(
222601 Fts5Hash *pHash,
222602 i64 iRowid, /* Rowid for this entry */
222603 int iCol, /* Column token appears in (-ve -> delete) */
222604 int iPos, /* Position of token within column */
222605 char bByte, /* First byte of token */
222606 const char *pToken, int nToken /* Token to add or remove to or from index */
222607){
222608 unsigned int iHash;
222609 Fts5HashEntry *p;
222610 u8 *pPtr;
222611 int nIncr = 0; /* Amount to increment (*pHash->pnByte) by */
222612 int bNew; /* If non-delete entry should be written */
222613
222614 bNew = (pHash->eDetail==FTS5_DETAIL_FULL);
222615
222616 /* Attempt to locate an existing hash entry */
222617 iHash = fts5HashKey2(nSlot: pHash->nSlot, b: (u8)bByte, p: (const u8*)pToken, n: nToken);
222618 for(p=pHash->aSlot[iHash]; p; p=p->pHashNext){
222619 char *zKey = fts5EntryKey(p);
222620 if( zKey[0]==bByte
222621 && p->nKey==nToken
222622 && memcmp(s1: &zKey[1], s2: pToken, n: nToken)==0
222623 ){
222624 break;
222625 }
222626 }
222627
222628 /* If an existing hash entry cannot be found, create a new one. */
222629 if( p==0 ){
222630 /* Figure out how much space to allocate */
222631 char *zKey;
222632 sqlite3_int64 nByte = sizeof(Fts5HashEntry) + (nToken+1) + 1 + 64;
222633 if( nByte<128 ) nByte = 128;
222634
222635 /* Grow the Fts5Hash.aSlot[] array if necessary. */
222636 if( (pHash->nEntry*2)>=pHash->nSlot ){
222637 int rc = fts5HashResize(pHash);
222638 if( rc!=SQLITE_OK ) return rc;
222639 iHash = fts5HashKey2(nSlot: pHash->nSlot, b: (u8)bByte, p: (const u8*)pToken, n: nToken);
222640 }
222641
222642 /* Allocate new Fts5HashEntry and add it to the hash table. */
222643 p = (Fts5HashEntry*)sqlite3_malloc64(n: nByte);
222644 if( !p ) return SQLITE_NOMEM;
222645 memset(s: p, c: 0, n: sizeof(Fts5HashEntry));
222646 p->nAlloc = (int)nByte;
222647 zKey = fts5EntryKey(p);
222648 zKey[0] = bByte;
222649 memcpy(dest: &zKey[1], src: pToken, n: nToken);
222650 assert( iHash==fts5HashKey(pHash->nSlot, (u8*)zKey, nToken+1) );
222651 p->nKey = nToken;
222652 zKey[nToken+1] = '\0';
222653 p->nData = nToken+1 + 1 + sizeof(Fts5HashEntry);
222654 p->pHashNext = pHash->aSlot[iHash];
222655 pHash->aSlot[iHash] = p;
222656 pHash->nEntry++;
222657
222658 /* Add the first rowid field to the hash-entry */
222659 p->nData += sqlite3Fts5PutVarint(p: &((u8*)p)[p->nData], v: iRowid);
222660 p->iRowid = iRowid;
222661
222662 p->iSzPoslist = p->nData;
222663 if( pHash->eDetail!=FTS5_DETAIL_NONE ){
222664 p->nData += 1;
222665 p->iCol = (pHash->eDetail==FTS5_DETAIL_FULL ? 0 : -1);
222666 }
222667
222668 }else{
222669
222670 /* Appending to an existing hash-entry. Check that there is enough
222671 ** space to append the largest possible new entry. Worst case scenario
222672 ** is:
222673 **
222674 ** + 9 bytes for a new rowid,
222675 ** + 4 byte reserved for the "poslist size" varint.
222676 ** + 1 byte for a "new column" byte,
222677 ** + 3 bytes for a new column number (16-bit max) as a varint,
222678 ** + 5 bytes for the new position offset (32-bit max).
222679 */
222680 if( (p->nAlloc - p->nData) < (9 + 4 + 1 + 3 + 5) ){
222681 sqlite3_int64 nNew = p->nAlloc * 2;
222682 Fts5HashEntry *pNew;
222683 Fts5HashEntry **pp;
222684 pNew = (Fts5HashEntry*)sqlite3_realloc64(pOld: p, n: nNew);
222685 if( pNew==0 ) return SQLITE_NOMEM;
222686 pNew->nAlloc = (int)nNew;
222687 for(pp=&pHash->aSlot[iHash]; *pp!=p; pp=&(*pp)->pHashNext);
222688 *pp = pNew;
222689 p = pNew;
222690 }
222691 nIncr -= p->nData;
222692 }
222693 assert( (p->nAlloc - p->nData) >= (9 + 4 + 1 + 3 + 5) );
222694
222695 pPtr = (u8*)p;
222696
222697 /* If this is a new rowid, append the 4-byte size field for the previous
222698 ** entry, and the new rowid for this entry. */
222699 if( iRowid!=p->iRowid ){
222700 u64 iDiff = (u64)iRowid - (u64)p->iRowid;
222701 fts5HashAddPoslistSize(pHash, p, p2: 0);
222702 p->nData += sqlite3Fts5PutVarint(p: &pPtr[p->nData], v: iDiff);
222703 p->iRowid = iRowid;
222704 bNew = 1;
222705 p->iSzPoslist = p->nData;
222706 if( pHash->eDetail!=FTS5_DETAIL_NONE ){
222707 p->nData += 1;
222708 p->iCol = (pHash->eDetail==FTS5_DETAIL_FULL ? 0 : -1);
222709 p->iPos = 0;
222710 }
222711 }
222712
222713 if( iCol>=0 ){
222714 if( pHash->eDetail==FTS5_DETAIL_NONE ){
222715 p->bContent = 1;
222716 }else{
222717 /* Append a new column value, if necessary */
222718 assert_nc( iCol>=p->iCol );
222719 if( iCol!=p->iCol ){
222720 if( pHash->eDetail==FTS5_DETAIL_FULL ){
222721 pPtr[p->nData++] = 0x01;
222722 p->nData += sqlite3Fts5PutVarint(p: &pPtr[p->nData], v: iCol);
222723 p->iCol = (i16)iCol;
222724 p->iPos = 0;
222725 }else{
222726 bNew = 1;
222727 p->iCol = (i16)(iPos = iCol);
222728 }
222729 }
222730
222731 /* Append the new position offset, if necessary */
222732 if( bNew ){
222733 p->nData += sqlite3Fts5PutVarint(p: &pPtr[p->nData], v: iPos - p->iPos + 2);
222734 p->iPos = iPos;
222735 }
222736 }
222737 }else{
222738 /* This is a delete. Set the delete flag. */
222739 p->bDel = 1;
222740 }
222741
222742 nIncr += p->nData;
222743 *pHash->pnByte += nIncr;
222744 return SQLITE_OK;
222745}
222746
222747
222748/*
222749** Arguments pLeft and pRight point to linked-lists of hash-entry objects,
222750** each sorted in key order. This function merges the two lists into a
222751** single list and returns a pointer to its first element.
222752*/
222753static Fts5HashEntry *fts5HashEntryMerge(
222754 Fts5HashEntry *pLeft,
222755 Fts5HashEntry *pRight
222756){
222757 Fts5HashEntry *p1 = pLeft;
222758 Fts5HashEntry *p2 = pRight;
222759 Fts5HashEntry *pRet = 0;
222760 Fts5HashEntry **ppOut = &pRet;
222761
222762 while( p1 || p2 ){
222763 if( p1==0 ){
222764 *ppOut = p2;
222765 p2 = 0;
222766 }else if( p2==0 ){
222767 *ppOut = p1;
222768 p1 = 0;
222769 }else{
222770 int i = 0;
222771 char *zKey1 = fts5EntryKey(p1);
222772 char *zKey2 = fts5EntryKey(p2);
222773 while( zKey1[i]==zKey2[i] ) i++;
222774
222775 if( ((u8)zKey1[i])>((u8)zKey2[i]) ){
222776 /* p2 is smaller */
222777 *ppOut = p2;
222778 ppOut = &p2->pScanNext;
222779 p2 = p2->pScanNext;
222780 }else{
222781 /* p1 is smaller */
222782 *ppOut = p1;
222783 ppOut = &p1->pScanNext;
222784 p1 = p1->pScanNext;
222785 }
222786 *ppOut = 0;
222787 }
222788 }
222789
222790 return pRet;
222791}
222792
222793/*
222794** Extract all tokens from hash table iHash and link them into a list
222795** in sorted order. The hash table is cleared before returning. It is
222796** the responsibility of the caller to free the elements of the returned
222797** list.
222798*/
222799static int fts5HashEntrySort(
222800 Fts5Hash *pHash,
222801 const char *pTerm, int nTerm, /* Query prefix, if any */
222802 Fts5HashEntry **ppSorted
222803){
222804 const int nMergeSlot = 32;
222805 Fts5HashEntry **ap;
222806 Fts5HashEntry *pList;
222807 int iSlot;
222808 int i;
222809
222810 *ppSorted = 0;
222811 ap = sqlite3_malloc64(n: sizeof(Fts5HashEntry*) * nMergeSlot);
222812 if( !ap ) return SQLITE_NOMEM;
222813 memset(s: ap, c: 0, n: sizeof(Fts5HashEntry*) * nMergeSlot);
222814
222815 for(iSlot=0; iSlot<pHash->nSlot; iSlot++){
222816 Fts5HashEntry *pIter;
222817 for(pIter=pHash->aSlot[iSlot]; pIter; pIter=pIter->pHashNext){
222818 if( pTerm==0
222819 || (pIter->nKey+1>=nTerm && 0==memcmp(fts5EntryKey(pIter), s2: pTerm, n: nTerm))
222820 ){
222821 Fts5HashEntry *pEntry = pIter;
222822 pEntry->pScanNext = 0;
222823 for(i=0; ap[i]; i++){
222824 pEntry = fts5HashEntryMerge(pLeft: pEntry, pRight: ap[i]);
222825 ap[i] = 0;
222826 }
222827 ap[i] = pEntry;
222828 }
222829 }
222830 }
222831
222832 pList = 0;
222833 for(i=0; i<nMergeSlot; i++){
222834 pList = fts5HashEntryMerge(pLeft: pList, pRight: ap[i]);
222835 }
222836
222837 pHash->nEntry = 0;
222838 sqlite3_free(p: ap);
222839 *ppSorted = pList;
222840 return SQLITE_OK;
222841}
222842
222843/*
222844** Query the hash table for a doclist associated with term pTerm/nTerm.
222845*/
222846static int sqlite3Fts5HashQuery(
222847 Fts5Hash *pHash, /* Hash table to query */
222848 int nPre,
222849 const char *pTerm, int nTerm, /* Query term */
222850 void **ppOut, /* OUT: Pointer to new object */
222851 int *pnDoclist /* OUT: Size of doclist in bytes */
222852){
222853 unsigned int iHash = fts5HashKey(nSlot: pHash->nSlot, p: (const u8*)pTerm, n: nTerm);
222854 char *zKey = 0;
222855 Fts5HashEntry *p;
222856
222857 for(p=pHash->aSlot[iHash]; p; p=p->pHashNext){
222858 zKey = fts5EntryKey(p);
222859 assert( p->nKey+1==(int)strlen(zKey) );
222860 if( nTerm==p->nKey+1 && memcmp(s1: zKey, s2: pTerm, n: nTerm)==0 ) break;
222861 }
222862
222863 if( p ){
222864 int nHashPre = sizeof(Fts5HashEntry) + nTerm + 1;
222865 int nList = p->nData - nHashPre;
222866 u8 *pRet = (u8*)(*ppOut = sqlite3_malloc64(n: nPre + nList + 10));
222867 if( pRet ){
222868 Fts5HashEntry *pFaux = (Fts5HashEntry*)&pRet[nPre-nHashPre];
222869 memcpy(dest: &pRet[nPre], src: &((u8*)p)[nHashPre], n: nList);
222870 nList += fts5HashAddPoslistSize(pHash, p, p2: pFaux);
222871 *pnDoclist = nList;
222872 }else{
222873 *pnDoclist = 0;
222874 return SQLITE_NOMEM;
222875 }
222876 }else{
222877 *ppOut = 0;
222878 *pnDoclist = 0;
222879 }
222880
222881 return SQLITE_OK;
222882}
222883
222884static int sqlite3Fts5HashScanInit(
222885 Fts5Hash *p, /* Hash table to query */
222886 const char *pTerm, int nTerm /* Query prefix */
222887){
222888 return fts5HashEntrySort(pHash: p, pTerm, nTerm, ppSorted: &p->pScan);
222889}
222890
222891static void sqlite3Fts5HashScanNext(Fts5Hash *p){
222892 assert( !sqlite3Fts5HashScanEof(p) );
222893 p->pScan = p->pScan->pScanNext;
222894}
222895
222896static int sqlite3Fts5HashScanEof(Fts5Hash *p){
222897 return (p->pScan==0);
222898}
222899
222900static void sqlite3Fts5HashScanEntry(
222901 Fts5Hash *pHash,
222902 const char **pzTerm, /* OUT: term (nul-terminated) */
222903 const u8 **ppDoclist, /* OUT: pointer to doclist */
222904 int *pnDoclist /* OUT: size of doclist in bytes */
222905){
222906 Fts5HashEntry *p;
222907 if( (p = pHash->pScan) ){
222908 char *zKey = fts5EntryKey(p);
222909 int nTerm = (int)strlen(s: zKey);
222910 fts5HashAddPoslistSize(pHash, p, p2: 0);
222911 *pzTerm = zKey;
222912 *ppDoclist = (const u8*)&zKey[nTerm+1];
222913 *pnDoclist = p->nData - (sizeof(Fts5HashEntry) + nTerm + 1);
222914 }else{
222915 *pzTerm = 0;
222916 *ppDoclist = 0;
222917 *pnDoclist = 0;
222918 }
222919}
222920
222921/*
222922** 2014 May 31
222923**
222924** The author disclaims copyright to this source code. In place of
222925** a legal notice, here is a blessing:
222926**
222927** May you do good and not evil.
222928** May you find forgiveness for yourself and forgive others.
222929** May you share freely, never taking more than you give.
222930**
222931******************************************************************************
222932**
222933** Low level access to the FTS index stored in the database file. The
222934** routines in this file file implement all read and write access to the
222935** %_data table. Other parts of the system access this functionality via
222936** the interface defined in fts5Int.h.
222937*/
222938
222939
222940/* #include "fts5Int.h" */
222941
222942/*
222943** Overview:
222944**
222945** The %_data table contains all the FTS indexes for an FTS5 virtual table.
222946** As well as the main term index, there may be up to 31 prefix indexes.
222947** The format is similar to FTS3/4, except that:
222948**
222949** * all segment b-tree leaf data is stored in fixed size page records
222950** (e.g. 1000 bytes). A single doclist may span multiple pages. Care is
222951** taken to ensure it is possible to iterate in either direction through
222952** the entries in a doclist, or to seek to a specific entry within a
222953** doclist, without loading it into memory.
222954**
222955** * large doclists that span many pages have associated "doclist index"
222956** records that contain a copy of the first rowid on each page spanned by
222957** the doclist. This is used to speed up seek operations, and merges of
222958** large doclists with very small doclists.
222959**
222960** * extra fields in the "structure record" record the state of ongoing
222961** incremental merge operations.
222962**
222963*/
222964
222965
222966#define FTS5_OPT_WORK_UNIT 1000 /* Number of leaf pages per optimize step */
222967#define FTS5_WORK_UNIT 64 /* Number of leaf pages in unit of work */
222968
222969#define FTS5_MIN_DLIDX_SIZE 4 /* Add dlidx if this many empty pages */
222970
222971#define FTS5_MAIN_PREFIX '0'
222972
222973#if FTS5_MAX_PREFIX_INDEXES > 31
222974# error "FTS5_MAX_PREFIX_INDEXES is too large"
222975#endif
222976
222977/*
222978** Details:
222979**
222980** The %_data table managed by this module,
222981**
222982** CREATE TABLE %_data(id INTEGER PRIMARY KEY, block BLOB);
222983**
222984** , contains the following 5 types of records. See the comments surrounding
222985** the FTS5_*_ROWID macros below for a description of how %_data rowids are
222986** assigned to each fo them.
222987**
222988** 1. Structure Records:
222989**
222990** The set of segments that make up an index - the index structure - are
222991** recorded in a single record within the %_data table. The record consists
222992** of a single 32-bit configuration cookie value followed by a list of
222993** SQLite varints. If the FTS table features more than one index (because
222994** there are one or more prefix indexes), it is guaranteed that all share
222995** the same cookie value.
222996**
222997** Immediately following the configuration cookie, the record begins with
222998** three varints:
222999**
223000** + number of levels,
223001** + total number of segments on all levels,
223002** + value of write counter.
223003**
223004** Then, for each level from 0 to nMax:
223005**
223006** + number of input segments in ongoing merge.
223007** + total number of segments in level.
223008** + for each segment from oldest to newest:
223009** + segment id (always > 0)
223010** + first leaf page number (often 1, always greater than 0)
223011** + final leaf page number
223012**
223013** 2. The Averages Record:
223014**
223015** A single record within the %_data table. The data is a list of varints.
223016** The first value is the number of rows in the index. Then, for each column
223017** from left to right, the total number of tokens in the column for all
223018** rows of the table.
223019**
223020** 3. Segment leaves:
223021**
223022** TERM/DOCLIST FORMAT:
223023**
223024** Most of each segment leaf is taken up by term/doclist data. The
223025** general format of term/doclist, starting with the first term
223026** on the leaf page, is:
223027**
223028** varint : size of first term
223029** blob: first term data
223030** doclist: first doclist
223031** zero-or-more {
223032** varint: number of bytes in common with previous term
223033** varint: number of bytes of new term data (nNew)
223034** blob: nNew bytes of new term data
223035** doclist: next doclist
223036** }
223037**
223038** doclist format:
223039**
223040** varint: first rowid
223041** poslist: first poslist
223042** zero-or-more {
223043** varint: rowid delta (always > 0)
223044** poslist: next poslist
223045** }
223046**
223047** poslist format:
223048**
223049** varint: size of poslist in bytes multiplied by 2, not including
223050** this field. Plus 1 if this entry carries the "delete" flag.
223051** collist: collist for column 0
223052** zero-or-more {
223053** 0x01 byte
223054** varint: column number (I)
223055** collist: collist for column I
223056** }
223057**
223058** collist format:
223059**
223060** varint: first offset + 2
223061** zero-or-more {
223062** varint: offset delta + 2
223063** }
223064**
223065** PAGE FORMAT
223066**
223067** Each leaf page begins with a 4-byte header containing 2 16-bit
223068** unsigned integer fields in big-endian format. They are:
223069**
223070** * The byte offset of the first rowid on the page, if it exists
223071** and occurs before the first term (otherwise 0).
223072**
223073** * The byte offset of the start of the page footer. If the page
223074** footer is 0 bytes in size, then this field is the same as the
223075** size of the leaf page in bytes.
223076**
223077** The page footer consists of a single varint for each term located
223078** on the page. Each varint is the byte offset of the current term
223079** within the page, delta-compressed against the previous value. In
223080** other words, the first varint in the footer is the byte offset of
223081** the first term, the second is the byte offset of the second less that
223082** of the first, and so on.
223083**
223084** The term/doclist format described above is accurate if the entire
223085** term/doclist data fits on a single leaf page. If this is not the case,
223086** the format is changed in two ways:
223087**
223088** + if the first rowid on a page occurs before the first term, it
223089** is stored as a literal value:
223090**
223091** varint: first rowid
223092**
223093** + the first term on each page is stored in the same way as the
223094** very first term of the segment:
223095**
223096** varint : size of first term
223097** blob: first term data
223098**
223099** 5. Segment doclist indexes:
223100**
223101** Doclist indexes are themselves b-trees, however they usually consist of
223102** a single leaf record only. The format of each doclist index leaf page
223103** is:
223104**
223105** * Flags byte. Bits are:
223106** 0x01: Clear if leaf is also the root page, otherwise set.
223107**
223108** * Page number of fts index leaf page. As a varint.
223109**
223110** * First rowid on page indicated by previous field. As a varint.
223111**
223112** * A list of varints, one for each subsequent termless page. A
223113** positive delta if the termless page contains at least one rowid,
223114** or an 0x00 byte otherwise.
223115**
223116** Internal doclist index nodes are:
223117**
223118** * Flags byte. Bits are:
223119** 0x01: Clear for root page, otherwise set.
223120**
223121** * Page number of first child page. As a varint.
223122**
223123** * Copy of first rowid on page indicated by previous field. As a varint.
223124**
223125** * A list of delta-encoded varints - the first rowid on each subsequent
223126** child page.
223127**
223128*/
223129
223130/*
223131** Rowids for the averages and structure records in the %_data table.
223132*/
223133#define FTS5_AVERAGES_ROWID 1 /* Rowid used for the averages record */
223134#define FTS5_STRUCTURE_ROWID 10 /* The structure record */
223135
223136/*
223137** Macros determining the rowids used by segment leaves and dlidx leaves
223138** and nodes. All nodes and leaves are stored in the %_data table with large
223139** positive rowids.
223140**
223141** Each segment has a unique non-zero 16-bit id.
223142**
223143** The rowid for each segment leaf is found by passing the segment id and
223144** the leaf page number to the FTS5_SEGMENT_ROWID macro. Leaves are numbered
223145** sequentially starting from 1.
223146*/
223147#define FTS5_DATA_ID_B 16 /* Max seg id number 65535 */
223148#define FTS5_DATA_DLI_B 1 /* Doclist-index flag (1 bit) */
223149#define FTS5_DATA_HEIGHT_B 5 /* Max dlidx tree height of 32 */
223150#define FTS5_DATA_PAGE_B 31 /* Max page number of 2147483648 */
223151
223152#define fts5_dri(segid, dlidx, height, pgno) ( \
223153 ((i64)(segid) << (FTS5_DATA_PAGE_B+FTS5_DATA_HEIGHT_B+FTS5_DATA_DLI_B)) + \
223154 ((i64)(dlidx) << (FTS5_DATA_PAGE_B + FTS5_DATA_HEIGHT_B)) + \
223155 ((i64)(height) << (FTS5_DATA_PAGE_B)) + \
223156 ((i64)(pgno)) \
223157)
223158
223159#define FTS5_SEGMENT_ROWID(segid, pgno) fts5_dri(segid, 0, 0, pgno)
223160#define FTS5_DLIDX_ROWID(segid, height, pgno) fts5_dri(segid, 1, height, pgno)
223161
223162#ifdef SQLITE_DEBUG
223163static int sqlite3Fts5Corrupt() { return SQLITE_CORRUPT_VTAB; }
223164#endif
223165
223166
223167/*
223168** Each time a blob is read from the %_data table, it is padded with this
223169** many zero bytes. This makes it easier to decode the various record formats
223170** without overreading if the records are corrupt.
223171*/
223172#define FTS5_DATA_ZERO_PADDING 8
223173#define FTS5_DATA_PADDING 20
223174
223175typedef struct Fts5Data Fts5Data;
223176typedef struct Fts5DlidxIter Fts5DlidxIter;
223177typedef struct Fts5DlidxLvl Fts5DlidxLvl;
223178typedef struct Fts5DlidxWriter Fts5DlidxWriter;
223179typedef struct Fts5Iter Fts5Iter;
223180typedef struct Fts5PageWriter Fts5PageWriter;
223181typedef struct Fts5SegIter Fts5SegIter;
223182typedef struct Fts5DoclistIter Fts5DoclistIter;
223183typedef struct Fts5SegWriter Fts5SegWriter;
223184typedef struct Fts5Structure Fts5Structure;
223185typedef struct Fts5StructureLevel Fts5StructureLevel;
223186typedef struct Fts5StructureSegment Fts5StructureSegment;
223187
223188struct Fts5Data {
223189 u8 *p; /* Pointer to buffer containing record */
223190 int nn; /* Size of record in bytes */
223191 int szLeaf; /* Size of leaf without page-index */
223192};
223193
223194/*
223195** One object per %_data table.
223196*/
223197struct Fts5Index {
223198 Fts5Config *pConfig; /* Virtual table configuration */
223199 char *zDataTbl; /* Name of %_data table */
223200 int nWorkUnit; /* Leaf pages in a "unit" of work */
223201
223202 /*
223203 ** Variables related to the accumulation of tokens and doclists within the
223204 ** in-memory hash tables before they are flushed to disk.
223205 */
223206 Fts5Hash *pHash; /* Hash table for in-memory data */
223207 int nPendingData; /* Current bytes of pending data */
223208 i64 iWriteRowid; /* Rowid for current doc being written */
223209 int bDelete; /* Current write is a delete */
223210
223211 /* Error state. */
223212 int rc; /* Current error code */
223213
223214 /* State used by the fts5DataXXX() functions. */
223215 sqlite3_blob *pReader; /* RO incr-blob open on %_data table */
223216 sqlite3_stmt *pWriter; /* "INSERT ... %_data VALUES(?,?)" */
223217 sqlite3_stmt *pDeleter; /* "DELETE FROM %_data ... id>=? AND id<=?" */
223218 sqlite3_stmt *pIdxWriter; /* "INSERT ... %_idx VALUES(?,?,?,?)" */
223219 sqlite3_stmt *pIdxDeleter; /* "DELETE FROM %_idx WHERE segid=? */
223220 sqlite3_stmt *pIdxSelect;
223221 int nRead; /* Total number of blocks read */
223222
223223 sqlite3_stmt *pDataVersion;
223224 i64 iStructVersion; /* data_version when pStruct read */
223225 Fts5Structure *pStruct; /* Current db structure (or NULL) */
223226};
223227
223228struct Fts5DoclistIter {
223229 u8 *aEof; /* Pointer to 1 byte past end of doclist */
223230
223231 /* Output variables. aPoslist==0 at EOF */
223232 i64 iRowid;
223233 u8 *aPoslist;
223234 int nPoslist;
223235 int nSize;
223236};
223237
223238/*
223239** The contents of the "structure" record for each index are represented
223240** using an Fts5Structure record in memory. Which uses instances of the
223241** other Fts5StructureXXX types as components.
223242*/
223243struct Fts5StructureSegment {
223244 int iSegid; /* Segment id */
223245 int pgnoFirst; /* First leaf page number in segment */
223246 int pgnoLast; /* Last leaf page number in segment */
223247};
223248struct Fts5StructureLevel {
223249 int nMerge; /* Number of segments in incr-merge */
223250 int nSeg; /* Total number of segments on level */
223251 Fts5StructureSegment *aSeg; /* Array of segments. aSeg[0] is oldest. */
223252};
223253struct Fts5Structure {
223254 int nRef; /* Object reference count */
223255 u64 nWriteCounter; /* Total leaves written to level 0 */
223256 int nSegment; /* Total segments in this structure */
223257 int nLevel; /* Number of levels in this index */
223258 Fts5StructureLevel aLevel[1]; /* Array of nLevel level objects */
223259};
223260
223261/*
223262** An object of type Fts5SegWriter is used to write to segments.
223263*/
223264struct Fts5PageWriter {
223265 int pgno; /* Page number for this page */
223266 int iPrevPgidx; /* Previous value written into pgidx */
223267 Fts5Buffer buf; /* Buffer containing leaf data */
223268 Fts5Buffer pgidx; /* Buffer containing page-index */
223269 Fts5Buffer term; /* Buffer containing previous term on page */
223270};
223271struct Fts5DlidxWriter {
223272 int pgno; /* Page number for this page */
223273 int bPrevValid; /* True if iPrev is valid */
223274 i64 iPrev; /* Previous rowid value written to page */
223275 Fts5Buffer buf; /* Buffer containing page data */
223276};
223277struct Fts5SegWriter {
223278 int iSegid; /* Segid to write to */
223279 Fts5PageWriter writer; /* PageWriter object */
223280 i64 iPrevRowid; /* Previous rowid written to current leaf */
223281 u8 bFirstRowidInDoclist; /* True if next rowid is first in doclist */
223282 u8 bFirstRowidInPage; /* True if next rowid is first in page */
223283 /* TODO1: Can use (writer.pgidx.n==0) instead of bFirstTermInPage */
223284 u8 bFirstTermInPage; /* True if next term will be first in leaf */
223285 int nLeafWritten; /* Number of leaf pages written */
223286 int nEmpty; /* Number of contiguous term-less nodes */
223287
223288 int nDlidx; /* Allocated size of aDlidx[] array */
223289 Fts5DlidxWriter *aDlidx; /* Array of Fts5DlidxWriter objects */
223290
223291 /* Values to insert into the %_idx table */
223292 Fts5Buffer btterm; /* Next term to insert into %_idx table */
223293 int iBtPage; /* Page number corresponding to btterm */
223294};
223295
223296typedef struct Fts5CResult Fts5CResult;
223297struct Fts5CResult {
223298 u16 iFirst; /* aSeg[] index of firstest iterator */
223299 u8 bTermEq; /* True if the terms are equal */
223300};
223301
223302/*
223303** Object for iterating through a single segment, visiting each term/rowid
223304** pair in the segment.
223305**
223306** pSeg:
223307** The segment to iterate through.
223308**
223309** iLeafPgno:
223310** Current leaf page number within segment.
223311**
223312** iLeafOffset:
223313** Byte offset within the current leaf that is the first byte of the
223314** position list data (one byte passed the position-list size field).
223315** rowid field of the current entry. Usually this is the size field of the
223316** position list data. The exception is if the rowid for the current entry
223317** is the last thing on the leaf page.
223318**
223319** pLeaf:
223320** Buffer containing current leaf page data. Set to NULL at EOF.
223321**
223322** iTermLeafPgno, iTermLeafOffset:
223323** Leaf page number containing the last term read from the segment. And
223324** the offset immediately following the term data.
223325**
223326** flags:
223327** Mask of FTS5_SEGITER_XXX values. Interpreted as follows:
223328**
223329** FTS5_SEGITER_ONETERM:
223330** If set, set the iterator to point to EOF after the current doclist
223331** has been exhausted. Do not proceed to the next term in the segment.
223332**
223333** FTS5_SEGITER_REVERSE:
223334** This flag is only ever set if FTS5_SEGITER_ONETERM is also set. If
223335** it is set, iterate through rowid in descending order instead of the
223336** default ascending order.
223337**
223338** iRowidOffset/nRowidOffset/aRowidOffset:
223339** These are used if the FTS5_SEGITER_REVERSE flag is set.
223340**
223341** For each rowid on the page corresponding to the current term, the
223342** corresponding aRowidOffset[] entry is set to the byte offset of the
223343** start of the "position-list-size" field within the page.
223344**
223345** iTermIdx:
223346** Index of current term on iTermLeafPgno.
223347*/
223348struct Fts5SegIter {
223349 Fts5StructureSegment *pSeg; /* Segment to iterate through */
223350 int flags; /* Mask of configuration flags */
223351 int iLeafPgno; /* Current leaf page number */
223352 Fts5Data *pLeaf; /* Current leaf data */
223353 Fts5Data *pNextLeaf; /* Leaf page (iLeafPgno+1) */
223354 i64 iLeafOffset; /* Byte offset within current leaf */
223355
223356 /* Next method */
223357 void (*xNext)(Fts5Index*, Fts5SegIter*, int*);
223358
223359 /* The page and offset from which the current term was read. The offset
223360 ** is the offset of the first rowid in the current doclist. */
223361 int iTermLeafPgno;
223362 int iTermLeafOffset;
223363
223364 int iPgidxOff; /* Next offset in pgidx */
223365 int iEndofDoclist;
223366
223367 /* The following are only used if the FTS5_SEGITER_REVERSE flag is set. */
223368 int iRowidOffset; /* Current entry in aRowidOffset[] */
223369 int nRowidOffset; /* Allocated size of aRowidOffset[] array */
223370 int *aRowidOffset; /* Array of offset to rowid fields */
223371
223372 Fts5DlidxIter *pDlidx; /* If there is a doclist-index */
223373
223374 /* Variables populated based on current entry. */
223375 Fts5Buffer term; /* Current term */
223376 i64 iRowid; /* Current rowid */
223377 int nPos; /* Number of bytes in current position list */
223378 u8 bDel; /* True if the delete flag is set */
223379};
223380
223381/*
223382** Argument is a pointer to an Fts5Data structure that contains a
223383** leaf page.
223384*/
223385#define ASSERT_SZLEAF_OK(x) assert( \
223386 (x)->szLeaf==(x)->nn || (x)->szLeaf==fts5GetU16(&(x)->p[2]) \
223387)
223388
223389#define FTS5_SEGITER_ONETERM 0x01
223390#define FTS5_SEGITER_REVERSE 0x02
223391
223392/*
223393** Argument is a pointer to an Fts5Data structure that contains a leaf
223394** page. This macro evaluates to true if the leaf contains no terms, or
223395** false if it contains at least one term.
223396*/
223397#define fts5LeafIsTermless(x) ((x)->szLeaf >= (x)->nn)
223398
223399#define fts5LeafTermOff(x, i) (fts5GetU16(&(x)->p[(x)->szLeaf + (i)*2]))
223400
223401#define fts5LeafFirstRowidOff(x) (fts5GetU16((x)->p))
223402
223403/*
223404** Object for iterating through the merged results of one or more segments,
223405** visiting each term/rowid pair in the merged data.
223406**
223407** nSeg is always a power of two greater than or equal to the number of
223408** segments that this object is merging data from. Both the aSeg[] and
223409** aFirst[] arrays are sized at nSeg entries. The aSeg[] array is padded
223410** with zeroed objects - these are handled as if they were iterators opened
223411** on empty segments.
223412**
223413** The results of comparing segments aSeg[N] and aSeg[N+1], where N is an
223414** even number, is stored in aFirst[(nSeg+N)/2]. The "result" of the
223415** comparison in this context is the index of the iterator that currently
223416** points to the smaller term/rowid combination. Iterators at EOF are
223417** considered to be greater than all other iterators.
223418**
223419** aFirst[1] contains the index in aSeg[] of the iterator that points to
223420** the smallest key overall. aFirst[0] is unused.
223421**
223422** poslist:
223423** Used by sqlite3Fts5IterPoslist() when the poslist needs to be buffered.
223424** There is no way to tell if this is populated or not.
223425*/
223426struct Fts5Iter {
223427 Fts5IndexIter base; /* Base class containing output vars */
223428
223429 Fts5Index *pIndex; /* Index that owns this iterator */
223430 Fts5Buffer poslist; /* Buffer containing current poslist */
223431 Fts5Colset *pColset; /* Restrict matches to these columns */
223432
223433 /* Invoked to set output variables. */
223434 void (*xSetOutputs)(Fts5Iter*, Fts5SegIter*);
223435
223436 int nSeg; /* Size of aSeg[] array */
223437 int bRev; /* True to iterate in reverse order */
223438 u8 bSkipEmpty; /* True to skip deleted entries */
223439
223440 i64 iSwitchRowid; /* Firstest rowid of other than aFirst[1] */
223441 Fts5CResult *aFirst; /* Current merge state (see above) */
223442 Fts5SegIter aSeg[1]; /* Array of segment iterators */
223443};
223444
223445
223446/*
223447** An instance of the following type is used to iterate through the contents
223448** of a doclist-index record.
223449**
223450** pData:
223451** Record containing the doclist-index data.
223452**
223453** bEof:
223454** Set to true once iterator has reached EOF.
223455**
223456** iOff:
223457** Set to the current offset within record pData.
223458*/
223459struct Fts5DlidxLvl {
223460 Fts5Data *pData; /* Data for current page of this level */
223461 int iOff; /* Current offset into pData */
223462 int bEof; /* At EOF already */
223463 int iFirstOff; /* Used by reverse iterators */
223464
223465 /* Output variables */
223466 int iLeafPgno; /* Page number of current leaf page */
223467 i64 iRowid; /* First rowid on leaf iLeafPgno */
223468};
223469struct Fts5DlidxIter {
223470 int nLvl;
223471 int iSegid;
223472 Fts5DlidxLvl aLvl[1];
223473};
223474
223475static void fts5PutU16(u8 *aOut, u16 iVal){
223476 aOut[0] = (iVal>>8);
223477 aOut[1] = (iVal&0xFF);
223478}
223479
223480static u16 fts5GetU16(const u8 *aIn){
223481 return ((u16)aIn[0] << 8) + aIn[1];
223482}
223483
223484/*
223485** Allocate and return a buffer at least nByte bytes in size.
223486**
223487** If an OOM error is encountered, return NULL and set the error code in
223488** the Fts5Index handle passed as the first argument.
223489*/
223490static void *fts5IdxMalloc(Fts5Index *p, sqlite3_int64 nByte){
223491 return sqlite3Fts5MallocZero(pRc: &p->rc, nByte);
223492}
223493
223494/*
223495** Compare the contents of the pLeft buffer with the pRight/nRight blob.
223496**
223497** Return -ve if pLeft is smaller than pRight, 0 if they are equal or
223498** +ve if pRight is smaller than pLeft. In other words:
223499**
223500** res = *pLeft - *pRight
223501*/
223502#ifdef SQLITE_DEBUG
223503static int fts5BufferCompareBlob(
223504 Fts5Buffer *pLeft, /* Left hand side of comparison */
223505 const u8 *pRight, int nRight /* Right hand side of comparison */
223506){
223507 int nCmp = MIN(pLeft->n, nRight);
223508 int res = memcmp(pLeft->p, pRight, nCmp);
223509 return (res==0 ? (pLeft->n - nRight) : res);
223510}
223511#endif
223512
223513/*
223514** Compare the contents of the two buffers using memcmp(). If one buffer
223515** is a prefix of the other, it is considered the lesser.
223516**
223517** Return -ve if pLeft is smaller than pRight, 0 if they are equal or
223518** +ve if pRight is smaller than pLeft. In other words:
223519**
223520** res = *pLeft - *pRight
223521*/
223522static int fts5BufferCompare(Fts5Buffer *pLeft, Fts5Buffer *pRight){
223523 int nCmp, res;
223524 nCmp = MIN(pLeft->n, pRight->n);
223525 assert( nCmp<=0 || pLeft->p!=0 );
223526 assert( nCmp<=0 || pRight->p!=0 );
223527 res = fts5Memcmp(pLeft->p, pRight->p, nCmp);
223528 return (res==0 ? (pLeft->n - pRight->n) : res);
223529}
223530
223531static int fts5LeafFirstTermOff(Fts5Data *pLeaf){
223532 int ret;
223533 fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf], ret);
223534 return ret;
223535}
223536
223537/*
223538** Close the read-only blob handle, if it is open.
223539*/
223540static void sqlite3Fts5IndexCloseReader(Fts5Index *p){
223541 if( p->pReader ){
223542 sqlite3_blob *pReader = p->pReader;
223543 p->pReader = 0;
223544 sqlite3_blob_close(pBlob: pReader);
223545 }
223546}
223547
223548/*
223549** Retrieve a record from the %_data table.
223550**
223551** If an error occurs, NULL is returned and an error left in the
223552** Fts5Index object.
223553*/
223554static Fts5Data *fts5DataRead(Fts5Index *p, i64 iRowid){
223555 Fts5Data *pRet = 0;
223556 if( p->rc==SQLITE_OK ){
223557 int rc = SQLITE_OK;
223558
223559 if( p->pReader ){
223560 /* This call may return SQLITE_ABORT if there has been a savepoint
223561 ** rollback since it was last used. In this case a new blob handle
223562 ** is required. */
223563 sqlite3_blob *pBlob = p->pReader;
223564 p->pReader = 0;
223565 rc = sqlite3_blob_reopen(pBlob, iRow: iRowid);
223566 assert( p->pReader==0 );
223567 p->pReader = pBlob;
223568 if( rc!=SQLITE_OK ){
223569 sqlite3Fts5IndexCloseReader(p);
223570 }
223571 if( rc==SQLITE_ABORT ) rc = SQLITE_OK;
223572 }
223573
223574 /* If the blob handle is not open at this point, open it and seek
223575 ** to the requested entry. */
223576 if( p->pReader==0 && rc==SQLITE_OK ){
223577 Fts5Config *pConfig = p->pConfig;
223578 rc = sqlite3_blob_open(db: pConfig->db,
223579 zDb: pConfig->zDb, zTable: p->zDataTbl, zColumn: "block", iRow: iRowid, wrFlag: 0, ppBlob: &p->pReader
223580 );
223581 }
223582
223583 /* If either of the sqlite3_blob_open() or sqlite3_blob_reopen() calls
223584 ** above returned SQLITE_ERROR, return SQLITE_CORRUPT_VTAB instead.
223585 ** All the reasons those functions might return SQLITE_ERROR - missing
223586 ** table, missing row, non-blob/text in block column - indicate
223587 ** backing store corruption. */
223588 if( rc==SQLITE_ERROR ) rc = FTS5_CORRUPT;
223589
223590 if( rc==SQLITE_OK ){
223591 u8 *aOut = 0; /* Read blob data into this buffer */
223592 int nByte = sqlite3_blob_bytes(pBlob: p->pReader);
223593 sqlite3_int64 nAlloc = sizeof(Fts5Data) + nByte + FTS5_DATA_PADDING;
223594 pRet = (Fts5Data*)sqlite3_malloc64(n: nAlloc);
223595 if( pRet ){
223596 pRet->nn = nByte;
223597 aOut = pRet->p = (u8*)&pRet[1];
223598 }else{
223599 rc = SQLITE_NOMEM;
223600 }
223601
223602 if( rc==SQLITE_OK ){
223603 rc = sqlite3_blob_read(pBlob: p->pReader, z: aOut, n: nByte, iOffset: 0);
223604 }
223605 if( rc!=SQLITE_OK ){
223606 sqlite3_free(p: pRet);
223607 pRet = 0;
223608 }else{
223609 /* TODO1: Fix this */
223610 pRet->p[nByte] = 0x00;
223611 pRet->p[nByte+1] = 0x00;
223612 pRet->szLeaf = fts5GetU16(aIn: &pRet->p[2]);
223613 }
223614 }
223615 p->rc = rc;
223616 p->nRead++;
223617 }
223618
223619 assert( (pRet==0)==(p->rc!=SQLITE_OK) );
223620 return pRet;
223621}
223622
223623
223624/*
223625** Release a reference to data record returned by an earlier call to
223626** fts5DataRead().
223627*/
223628static void fts5DataRelease(Fts5Data *pData){
223629 sqlite3_free(p: pData);
223630}
223631
223632static Fts5Data *fts5LeafRead(Fts5Index *p, i64 iRowid){
223633 Fts5Data *pRet = fts5DataRead(p, iRowid);
223634 if( pRet ){
223635 if( pRet->nn<4 || pRet->szLeaf>pRet->nn ){
223636 p->rc = FTS5_CORRUPT;
223637 fts5DataRelease(pData: pRet);
223638 pRet = 0;
223639 }
223640 }
223641 return pRet;
223642}
223643
223644static int fts5IndexPrepareStmt(
223645 Fts5Index *p,
223646 sqlite3_stmt **ppStmt,
223647 char *zSql
223648){
223649 if( p->rc==SQLITE_OK ){
223650 if( zSql ){
223651 p->rc = sqlite3_prepare_v3(db: p->pConfig->db, zSql, nBytes: -1,
223652 SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_NO_VTAB,
223653 ppStmt, pzTail: 0);
223654 }else{
223655 p->rc = SQLITE_NOMEM;
223656 }
223657 }
223658 sqlite3_free(p: zSql);
223659 return p->rc;
223660}
223661
223662
223663/*
223664** INSERT OR REPLACE a record into the %_data table.
223665*/
223666static void fts5DataWrite(Fts5Index *p, i64 iRowid, const u8 *pData, int nData){
223667 if( p->rc!=SQLITE_OK ) return;
223668
223669 if( p->pWriter==0 ){
223670 Fts5Config *pConfig = p->pConfig;
223671 fts5IndexPrepareStmt(p, ppStmt: &p->pWriter, zSql: sqlite3_mprintf(
223672 zFormat: "REPLACE INTO '%q'.'%q_data'(id, block) VALUES(?,?)",
223673 pConfig->zDb, pConfig->zName
223674 ));
223675 if( p->rc ) return;
223676 }
223677
223678 sqlite3_bind_int64(pStmt: p->pWriter, i: 1, iValue: iRowid);
223679 sqlite3_bind_blob(pStmt: p->pWriter, i: 2, zData: pData, nData, SQLITE_STATIC);
223680 sqlite3_step(pStmt: p->pWriter);
223681 p->rc = sqlite3_reset(pStmt: p->pWriter);
223682 sqlite3_bind_null(pStmt: p->pWriter, i: 2);
223683}
223684
223685/*
223686** Execute the following SQL:
223687**
223688** DELETE FROM %_data WHERE id BETWEEN $iFirst AND $iLast
223689*/
223690static void fts5DataDelete(Fts5Index *p, i64 iFirst, i64 iLast){
223691 if( p->rc!=SQLITE_OK ) return;
223692
223693 if( p->pDeleter==0 ){
223694 Fts5Config *pConfig = p->pConfig;
223695 char *zSql = sqlite3_mprintf(
223696 zFormat: "DELETE FROM '%q'.'%q_data' WHERE id>=? AND id<=?",
223697 pConfig->zDb, pConfig->zName
223698 );
223699 if( fts5IndexPrepareStmt(p, ppStmt: &p->pDeleter, zSql) ) return;
223700 }
223701
223702 sqlite3_bind_int64(pStmt: p->pDeleter, i: 1, iValue: iFirst);
223703 sqlite3_bind_int64(pStmt: p->pDeleter, i: 2, iValue: iLast);
223704 sqlite3_step(pStmt: p->pDeleter);
223705 p->rc = sqlite3_reset(pStmt: p->pDeleter);
223706}
223707
223708/*
223709** Remove all records associated with segment iSegid.
223710*/
223711static void fts5DataRemoveSegment(Fts5Index *p, int iSegid){
223712 i64 iFirst = FTS5_SEGMENT_ROWID(iSegid, 0);
223713 i64 iLast = FTS5_SEGMENT_ROWID(iSegid+1, 0)-1;
223714 fts5DataDelete(p, iFirst, iLast);
223715 if( p->pIdxDeleter==0 ){
223716 Fts5Config *pConfig = p->pConfig;
223717 fts5IndexPrepareStmt(p, ppStmt: &p->pIdxDeleter, zSql: sqlite3_mprintf(
223718 zFormat: "DELETE FROM '%q'.'%q_idx' WHERE segid=?",
223719 pConfig->zDb, pConfig->zName
223720 ));
223721 }
223722 if( p->rc==SQLITE_OK ){
223723 sqlite3_bind_int(p: p->pIdxDeleter, i: 1, iValue: iSegid);
223724 sqlite3_step(pStmt: p->pIdxDeleter);
223725 p->rc = sqlite3_reset(pStmt: p->pIdxDeleter);
223726 }
223727}
223728
223729/*
223730** Release a reference to an Fts5Structure object returned by an earlier
223731** call to fts5StructureRead() or fts5StructureDecode().
223732*/
223733static void fts5StructureRelease(Fts5Structure *pStruct){
223734 if( pStruct && 0>=(--pStruct->nRef) ){
223735 int i;
223736 assert( pStruct->nRef==0 );
223737 for(i=0; i<pStruct->nLevel; i++){
223738 sqlite3_free(p: pStruct->aLevel[i].aSeg);
223739 }
223740 sqlite3_free(p: pStruct);
223741 }
223742}
223743
223744static void fts5StructureRef(Fts5Structure *pStruct){
223745 pStruct->nRef++;
223746}
223747
223748static void *sqlite3Fts5StructureRef(Fts5Index *p){
223749 fts5StructureRef(pStruct: p->pStruct);
223750 return (void*)p->pStruct;
223751}
223752static void sqlite3Fts5StructureRelease(void *p){
223753 if( p ){
223754 fts5StructureRelease(pStruct: (Fts5Structure*)p);
223755 }
223756}
223757static int sqlite3Fts5StructureTest(Fts5Index *p, void *pStruct){
223758 if( p->pStruct!=(Fts5Structure*)pStruct ){
223759 return SQLITE_ABORT;
223760 }
223761 return SQLITE_OK;
223762}
223763
223764/*
223765** Ensure that structure object (*pp) is writable.
223766**
223767** This function is a no-op if (*pRc) is not SQLITE_OK when it is called. If
223768** an error occurs, (*pRc) is set to an SQLite error code before returning.
223769*/
223770static void fts5StructureMakeWritable(int *pRc, Fts5Structure **pp){
223771 Fts5Structure *p = *pp;
223772 if( *pRc==SQLITE_OK && p->nRef>1 ){
223773 i64 nByte = sizeof(Fts5Structure)+(p->nLevel-1)*sizeof(Fts5StructureLevel);
223774 Fts5Structure *pNew;
223775 pNew = (Fts5Structure*)sqlite3Fts5MallocZero(pRc, nByte);
223776 if( pNew ){
223777 int i;
223778 memcpy(dest: pNew, src: p, n: nByte);
223779 for(i=0; i<p->nLevel; i++) pNew->aLevel[i].aSeg = 0;
223780 for(i=0; i<p->nLevel; i++){
223781 Fts5StructureLevel *pLvl = &pNew->aLevel[i];
223782 nByte = sizeof(Fts5StructureSegment) * pNew->aLevel[i].nSeg;
223783 pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(pRc, nByte);
223784 if( pLvl->aSeg==0 ){
223785 for(i=0; i<p->nLevel; i++){
223786 sqlite3_free(p: pNew->aLevel[i].aSeg);
223787 }
223788 sqlite3_free(p: pNew);
223789 return;
223790 }
223791 memcpy(dest: pLvl->aSeg, src: p->aLevel[i].aSeg, n: nByte);
223792 }
223793 p->nRef--;
223794 pNew->nRef = 1;
223795 }
223796 *pp = pNew;
223797 }
223798}
223799
223800/*
223801** Deserialize and return the structure record currently stored in serialized
223802** form within buffer pData/nData.
223803**
223804** The Fts5Structure.aLevel[] and each Fts5StructureLevel.aSeg[] array
223805** are over-allocated by one slot. This allows the structure contents
223806** to be more easily edited.
223807**
223808** If an error occurs, *ppOut is set to NULL and an SQLite error code
223809** returned. Otherwise, *ppOut is set to point to the new object and
223810** SQLITE_OK returned.
223811*/
223812static int fts5StructureDecode(
223813 const u8 *pData, /* Buffer containing serialized structure */
223814 int nData, /* Size of buffer pData in bytes */
223815 int *piCookie, /* Configuration cookie value */
223816 Fts5Structure **ppOut /* OUT: Deserialized object */
223817){
223818 int rc = SQLITE_OK;
223819 int i = 0;
223820 int iLvl;
223821 int nLevel = 0;
223822 int nSegment = 0;
223823 sqlite3_int64 nByte; /* Bytes of space to allocate at pRet */
223824 Fts5Structure *pRet = 0; /* Structure object to return */
223825
223826 /* Grab the cookie value */
223827 if( piCookie ) *piCookie = sqlite3Fts5Get32(aBuf: pData);
223828 i = 4;
223829
223830 /* Read the total number of levels and segments from the start of the
223831 ** structure record. */
223832 i += fts5GetVarint32(&pData[i], nLevel);
223833 i += fts5GetVarint32(&pData[i], nSegment);
223834 if( nLevel>FTS5_MAX_SEGMENT || nLevel<0
223835 || nSegment>FTS5_MAX_SEGMENT || nSegment<0
223836 ){
223837 return FTS5_CORRUPT;
223838 }
223839 nByte = (
223840 sizeof(Fts5Structure) + /* Main structure */
223841 sizeof(Fts5StructureLevel) * (nLevel-1) /* aLevel[] array */
223842 );
223843 pRet = (Fts5Structure*)sqlite3Fts5MallocZero(pRc: &rc, nByte);
223844
223845 if( pRet ){
223846 pRet->nRef = 1;
223847 pRet->nLevel = nLevel;
223848 pRet->nSegment = nSegment;
223849 i += sqlite3Fts5GetVarint(&pData[i], &pRet->nWriteCounter);
223850
223851 for(iLvl=0; rc==SQLITE_OK && iLvl<nLevel; iLvl++){
223852 Fts5StructureLevel *pLvl = &pRet->aLevel[iLvl];
223853 int nTotal = 0;
223854 int iSeg;
223855
223856 if( i>=nData ){
223857 rc = FTS5_CORRUPT;
223858 }else{
223859 i += fts5GetVarint32(&pData[i], pLvl->nMerge);
223860 i += fts5GetVarint32(&pData[i], nTotal);
223861 if( nTotal<pLvl->nMerge ) rc = FTS5_CORRUPT;
223862 pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(pRc: &rc,
223863 nByte: nTotal * sizeof(Fts5StructureSegment)
223864 );
223865 nSegment -= nTotal;
223866 }
223867
223868 if( rc==SQLITE_OK ){
223869 pLvl->nSeg = nTotal;
223870 for(iSeg=0; iSeg<nTotal; iSeg++){
223871 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
223872 if( i>=nData ){
223873 rc = FTS5_CORRUPT;
223874 break;
223875 }
223876 i += fts5GetVarint32(&pData[i], pSeg->iSegid);
223877 i += fts5GetVarint32(&pData[i], pSeg->pgnoFirst);
223878 i += fts5GetVarint32(&pData[i], pSeg->pgnoLast);
223879 if( pSeg->pgnoLast<pSeg->pgnoFirst ){
223880 rc = FTS5_CORRUPT;
223881 break;
223882 }
223883 }
223884 if( iLvl>0 && pLvl[-1].nMerge && nTotal==0 ) rc = FTS5_CORRUPT;
223885 if( iLvl==nLevel-1 && pLvl->nMerge ) rc = FTS5_CORRUPT;
223886 }
223887 }
223888 if( nSegment!=0 && rc==SQLITE_OK ) rc = FTS5_CORRUPT;
223889
223890 if( rc!=SQLITE_OK ){
223891 fts5StructureRelease(pStruct: pRet);
223892 pRet = 0;
223893 }
223894 }
223895
223896 *ppOut = pRet;
223897 return rc;
223898}
223899
223900/*
223901** Add a level to the Fts5Structure.aLevel[] array of structure object
223902** (*ppStruct).
223903*/
223904static void fts5StructureAddLevel(int *pRc, Fts5Structure **ppStruct){
223905 fts5StructureMakeWritable(pRc, pp: ppStruct);
223906 if( *pRc==SQLITE_OK ){
223907 Fts5Structure *pStruct = *ppStruct;
223908 int nLevel = pStruct->nLevel;
223909 sqlite3_int64 nByte = (
223910 sizeof(Fts5Structure) + /* Main structure */
223911 sizeof(Fts5StructureLevel) * (nLevel+1) /* aLevel[] array */
223912 );
223913
223914 pStruct = sqlite3_realloc64(pOld: pStruct, n: nByte);
223915 if( pStruct ){
223916 memset(s: &pStruct->aLevel[nLevel], c: 0, n: sizeof(Fts5StructureLevel));
223917 pStruct->nLevel++;
223918 *ppStruct = pStruct;
223919 }else{
223920 *pRc = SQLITE_NOMEM;
223921 }
223922 }
223923}
223924
223925/*
223926** Extend level iLvl so that there is room for at least nExtra more
223927** segments.
223928*/
223929static void fts5StructureExtendLevel(
223930 int *pRc,
223931 Fts5Structure *pStruct,
223932 int iLvl,
223933 int nExtra,
223934 int bInsert
223935){
223936 if( *pRc==SQLITE_OK ){
223937 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
223938 Fts5StructureSegment *aNew;
223939 sqlite3_int64 nByte;
223940
223941 nByte = (pLvl->nSeg + nExtra) * sizeof(Fts5StructureSegment);
223942 aNew = sqlite3_realloc64(pOld: pLvl->aSeg, n: nByte);
223943 if( aNew ){
223944 if( bInsert==0 ){
223945 memset(s: &aNew[pLvl->nSeg], c: 0, n: sizeof(Fts5StructureSegment) * nExtra);
223946 }else{
223947 int nMove = pLvl->nSeg * sizeof(Fts5StructureSegment);
223948 memmove(dest: &aNew[nExtra], src: aNew, n: nMove);
223949 memset(s: aNew, c: 0, n: sizeof(Fts5StructureSegment) * nExtra);
223950 }
223951 pLvl->aSeg = aNew;
223952 }else{
223953 *pRc = SQLITE_NOMEM;
223954 }
223955 }
223956}
223957
223958static Fts5Structure *fts5StructureReadUncached(Fts5Index *p){
223959 Fts5Structure *pRet = 0;
223960 Fts5Config *pConfig = p->pConfig;
223961 int iCookie; /* Configuration cookie */
223962 Fts5Data *pData;
223963
223964 pData = fts5DataRead(p, FTS5_STRUCTURE_ROWID);
223965 if( p->rc==SQLITE_OK ){
223966 /* TODO: Do we need this if the leaf-index is appended? Probably... */
223967 memset(s: &pData->p[pData->nn], c: 0, FTS5_DATA_PADDING);
223968 p->rc = fts5StructureDecode(pData: pData->p, nData: pData->nn, piCookie: &iCookie, ppOut: &pRet);
223969 if( p->rc==SQLITE_OK && (pConfig->pgsz==0 || pConfig->iCookie!=iCookie) ){
223970 p->rc = sqlite3Fts5ConfigLoad(pConfig, iCookie);
223971 }
223972 fts5DataRelease(pData);
223973 if( p->rc!=SQLITE_OK ){
223974 fts5StructureRelease(pStruct: pRet);
223975 pRet = 0;
223976 }
223977 }
223978
223979 return pRet;
223980}
223981
223982static i64 fts5IndexDataVersion(Fts5Index *p){
223983 i64 iVersion = 0;
223984
223985 if( p->rc==SQLITE_OK ){
223986 if( p->pDataVersion==0 ){
223987 p->rc = fts5IndexPrepareStmt(p, ppStmt: &p->pDataVersion,
223988 zSql: sqlite3_mprintf(zFormat: "PRAGMA %Q.data_version", p->pConfig->zDb)
223989 );
223990 if( p->rc ) return 0;
223991 }
223992
223993 if( SQLITE_ROW==sqlite3_step(pStmt: p->pDataVersion) ){
223994 iVersion = sqlite3_column_int64(pStmt: p->pDataVersion, i: 0);
223995 }
223996 p->rc = sqlite3_reset(pStmt: p->pDataVersion);
223997 }
223998
223999 return iVersion;
224000}
224001
224002/*
224003** Read, deserialize and return the structure record.
224004**
224005** The Fts5Structure.aLevel[] and each Fts5StructureLevel.aSeg[] array
224006** are over-allocated as described for function fts5StructureDecode()
224007** above.
224008**
224009** If an error occurs, NULL is returned and an error code left in the
224010** Fts5Index handle. If an error has already occurred when this function
224011** is called, it is a no-op.
224012*/
224013static Fts5Structure *fts5StructureRead(Fts5Index *p){
224014
224015 if( p->pStruct==0 ){
224016 p->iStructVersion = fts5IndexDataVersion(p);
224017 if( p->rc==SQLITE_OK ){
224018 p->pStruct = fts5StructureReadUncached(p);
224019 }
224020 }
224021
224022#if 0
224023 else{
224024 Fts5Structure *pTest = fts5StructureReadUncached(p);
224025 if( pTest ){
224026 int i, j;
224027 assert_nc( p->pStruct->nSegment==pTest->nSegment );
224028 assert_nc( p->pStruct->nLevel==pTest->nLevel );
224029 for(i=0; i<pTest->nLevel; i++){
224030 assert_nc( p->pStruct->aLevel[i].nMerge==pTest->aLevel[i].nMerge );
224031 assert_nc( p->pStruct->aLevel[i].nSeg==pTest->aLevel[i].nSeg );
224032 for(j=0; j<pTest->aLevel[i].nSeg; j++){
224033 Fts5StructureSegment *p1 = &pTest->aLevel[i].aSeg[j];
224034 Fts5StructureSegment *p2 = &p->pStruct->aLevel[i].aSeg[j];
224035 assert_nc( p1->iSegid==p2->iSegid );
224036 assert_nc( p1->pgnoFirst==p2->pgnoFirst );
224037 assert_nc( p1->pgnoLast==p2->pgnoLast );
224038 }
224039 }
224040 fts5StructureRelease(pTest);
224041 }
224042 }
224043#endif
224044
224045 if( p->rc!=SQLITE_OK ) return 0;
224046 assert( p->iStructVersion!=0 );
224047 assert( p->pStruct!=0 );
224048 fts5StructureRef(pStruct: p->pStruct);
224049 return p->pStruct;
224050}
224051
224052static void fts5StructureInvalidate(Fts5Index *p){
224053 if( p->pStruct ){
224054 fts5StructureRelease(pStruct: p->pStruct);
224055 p->pStruct = 0;
224056 }
224057}
224058
224059/*
224060** Return the total number of segments in index structure pStruct. This
224061** function is only ever used as part of assert() conditions.
224062*/
224063#ifdef SQLITE_DEBUG
224064static int fts5StructureCountSegments(Fts5Structure *pStruct){
224065 int nSegment = 0; /* Total number of segments */
224066 if( pStruct ){
224067 int iLvl; /* Used to iterate through levels */
224068 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
224069 nSegment += pStruct->aLevel[iLvl].nSeg;
224070 }
224071 }
224072
224073 return nSegment;
224074}
224075#endif
224076
224077#define fts5BufferSafeAppendBlob(pBuf, pBlob, nBlob) { \
224078 assert( (pBuf)->nSpace>=((pBuf)->n+nBlob) ); \
224079 memcpy(&(pBuf)->p[(pBuf)->n], pBlob, nBlob); \
224080 (pBuf)->n += nBlob; \
224081}
224082
224083#define fts5BufferSafeAppendVarint(pBuf, iVal) { \
224084 (pBuf)->n += sqlite3Fts5PutVarint(&(pBuf)->p[(pBuf)->n], (iVal)); \
224085 assert( (pBuf)->nSpace>=(pBuf)->n ); \
224086}
224087
224088
224089/*
224090** Serialize and store the "structure" record.
224091**
224092** If an error occurs, leave an error code in the Fts5Index object. If an
224093** error has already occurred, this function is a no-op.
224094*/
224095static void fts5StructureWrite(Fts5Index *p, Fts5Structure *pStruct){
224096 if( p->rc==SQLITE_OK ){
224097 Fts5Buffer buf; /* Buffer to serialize record into */
224098 int iLvl; /* Used to iterate through levels */
224099 int iCookie; /* Cookie value to store */
224100
224101 assert( pStruct->nSegment==fts5StructureCountSegments(pStruct) );
224102 memset(s: &buf, c: 0, n: sizeof(Fts5Buffer));
224103
224104 /* Append the current configuration cookie */
224105 iCookie = p->pConfig->iCookie;
224106 if( iCookie<0 ) iCookie = 0;
224107
224108 if( 0==sqlite3Fts5BufferSize(pRc: &p->rc, pBuf: &buf, nByte: 4+9+9+9) ){
224109 sqlite3Fts5Put32(aBuf: buf.p, iVal: iCookie);
224110 buf.n = 4;
224111 fts5BufferSafeAppendVarint(&buf, pStruct->nLevel);
224112 fts5BufferSafeAppendVarint(&buf, pStruct->nSegment);
224113 fts5BufferSafeAppendVarint(&buf, (i64)pStruct->nWriteCounter);
224114 }
224115
224116 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
224117 int iSeg; /* Used to iterate through segments */
224118 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
224119 fts5BufferAppendVarint(&p->rc, &buf, pLvl->nMerge);
224120 fts5BufferAppendVarint(&p->rc, &buf, pLvl->nSeg);
224121 assert( pLvl->nMerge<=pLvl->nSeg );
224122
224123 for(iSeg=0; iSeg<pLvl->nSeg; iSeg++){
224124 fts5BufferAppendVarint(&p->rc, &buf, pLvl->aSeg[iSeg].iSegid);
224125 fts5BufferAppendVarint(&p->rc, &buf, pLvl->aSeg[iSeg].pgnoFirst);
224126 fts5BufferAppendVarint(&p->rc, &buf, pLvl->aSeg[iSeg].pgnoLast);
224127 }
224128 }
224129
224130 fts5DataWrite(p, FTS5_STRUCTURE_ROWID, pData: buf.p, nData: buf.n);
224131 fts5BufferFree(&buf);
224132 }
224133}
224134
224135#if 0
224136static void fts5DebugStructure(int*,Fts5Buffer*,Fts5Structure*);
224137static void fts5PrintStructure(const char *zCaption, Fts5Structure *pStruct){
224138 int rc = SQLITE_OK;
224139 Fts5Buffer buf;
224140 memset(&buf, 0, sizeof(buf));
224141 fts5DebugStructure(&rc, &buf, pStruct);
224142 fprintf(stdout, "%s: %s\n", zCaption, buf.p);
224143 fflush(stdout);
224144 fts5BufferFree(&buf);
224145}
224146#else
224147# define fts5PrintStructure(x,y)
224148#endif
224149
224150static int fts5SegmentSize(Fts5StructureSegment *pSeg){
224151 return 1 + pSeg->pgnoLast - pSeg->pgnoFirst;
224152}
224153
224154/*
224155** Return a copy of index structure pStruct. Except, promote as many
224156** segments as possible to level iPromote. If an OOM occurs, NULL is
224157** returned.
224158*/
224159static void fts5StructurePromoteTo(
224160 Fts5Index *p,
224161 int iPromote,
224162 int szPromote,
224163 Fts5Structure *pStruct
224164){
224165 int il, is;
224166 Fts5StructureLevel *pOut = &pStruct->aLevel[iPromote];
224167
224168 if( pOut->nMerge==0 ){
224169 for(il=iPromote+1; il<pStruct->nLevel; il++){
224170 Fts5StructureLevel *pLvl = &pStruct->aLevel[il];
224171 if( pLvl->nMerge ) return;
224172 for(is=pLvl->nSeg-1; is>=0; is--){
224173 int sz = fts5SegmentSize(pSeg: &pLvl->aSeg[is]);
224174 if( sz>szPromote ) return;
224175 fts5StructureExtendLevel(pRc: &p->rc, pStruct, iLvl: iPromote, nExtra: 1, bInsert: 1);
224176 if( p->rc ) return;
224177 memcpy(dest: pOut->aSeg, src: &pLvl->aSeg[is], n: sizeof(Fts5StructureSegment));
224178 pOut->nSeg++;
224179 pLvl->nSeg--;
224180 }
224181 }
224182 }
224183}
224184
224185/*
224186** A new segment has just been written to level iLvl of index structure
224187** pStruct. This function determines if any segments should be promoted
224188** as a result. Segments are promoted in two scenarios:
224189**
224190** a) If the segment just written is smaller than one or more segments
224191** within the previous populated level, it is promoted to the previous
224192** populated level.
224193**
224194** b) If the segment just written is larger than the newest segment on
224195** the next populated level, then that segment, and any other adjacent
224196** segments that are also smaller than the one just written, are
224197** promoted.
224198**
224199** If one or more segments are promoted, the structure object is updated
224200** to reflect this.
224201*/
224202static void fts5StructurePromote(
224203 Fts5Index *p, /* FTS5 backend object */
224204 int iLvl, /* Index level just updated */
224205 Fts5Structure *pStruct /* Index structure */
224206){
224207 if( p->rc==SQLITE_OK ){
224208 int iTst;
224209 int iPromote = -1;
224210 int szPromote = 0; /* Promote anything this size or smaller */
224211 Fts5StructureSegment *pSeg; /* Segment just written */
224212 int szSeg; /* Size of segment just written */
224213 int nSeg = pStruct->aLevel[iLvl].nSeg;
224214
224215 if( nSeg==0 ) return;
224216 pSeg = &pStruct->aLevel[iLvl].aSeg[pStruct->aLevel[iLvl].nSeg-1];
224217 szSeg = (1 + pSeg->pgnoLast - pSeg->pgnoFirst);
224218
224219 /* Check for condition (a) */
224220 for(iTst=iLvl-1; iTst>=0 && pStruct->aLevel[iTst].nSeg==0; iTst--);
224221 if( iTst>=0 ){
224222 int i;
224223 int szMax = 0;
224224 Fts5StructureLevel *pTst = &pStruct->aLevel[iTst];
224225 assert( pTst->nMerge==0 );
224226 for(i=0; i<pTst->nSeg; i++){
224227 int sz = pTst->aSeg[i].pgnoLast - pTst->aSeg[i].pgnoFirst + 1;
224228 if( sz>szMax ) szMax = sz;
224229 }
224230 if( szMax>=szSeg ){
224231 /* Condition (a) is true. Promote the newest segment on level
224232 ** iLvl to level iTst. */
224233 iPromote = iTst;
224234 szPromote = szMax;
224235 }
224236 }
224237
224238 /* If condition (a) is not met, assume (b) is true. StructurePromoteTo()
224239 ** is a no-op if it is not. */
224240 if( iPromote<0 ){
224241 iPromote = iLvl;
224242 szPromote = szSeg;
224243 }
224244 fts5StructurePromoteTo(p, iPromote, szPromote, pStruct);
224245 }
224246}
224247
224248
224249/*
224250** Advance the iterator passed as the only argument. If the end of the
224251** doclist-index page is reached, return non-zero.
224252*/
224253static int fts5DlidxLvlNext(Fts5DlidxLvl *pLvl){
224254 Fts5Data *pData = pLvl->pData;
224255
224256 if( pLvl->iOff==0 ){
224257 assert( pLvl->bEof==0 );
224258 pLvl->iOff = 1;
224259 pLvl->iOff += fts5GetVarint32(&pData->p[1], pLvl->iLeafPgno);
224260 pLvl->iOff += fts5GetVarint(&pData->p[pLvl->iOff], (u64*)&pLvl->iRowid);
224261 pLvl->iFirstOff = pLvl->iOff;
224262 }else{
224263 int iOff;
224264 for(iOff=pLvl->iOff; iOff<pData->nn; iOff++){
224265 if( pData->p[iOff] ) break;
224266 }
224267
224268 if( iOff<pData->nn ){
224269 i64 iVal;
224270 pLvl->iLeafPgno += (iOff - pLvl->iOff) + 1;
224271 iOff += fts5GetVarint(&pData->p[iOff], (u64*)&iVal);
224272 pLvl->iRowid += iVal;
224273 pLvl->iOff = iOff;
224274 }else{
224275 pLvl->bEof = 1;
224276 }
224277 }
224278
224279 return pLvl->bEof;
224280}
224281
224282/*
224283** Advance the iterator passed as the only argument.
224284*/
224285static int fts5DlidxIterNextR(Fts5Index *p, Fts5DlidxIter *pIter, int iLvl){
224286 Fts5DlidxLvl *pLvl = &pIter->aLvl[iLvl];
224287
224288 assert( iLvl<pIter->nLvl );
224289 if( fts5DlidxLvlNext(pLvl) ){
224290 if( (iLvl+1) < pIter->nLvl ){
224291 fts5DlidxIterNextR(p, pIter, iLvl: iLvl+1);
224292 if( pLvl[1].bEof==0 ){
224293 fts5DataRelease(pData: pLvl->pData);
224294 memset(s: pLvl, c: 0, n: sizeof(Fts5DlidxLvl));
224295 pLvl->pData = fts5DataRead(p,
224296 FTS5_DLIDX_ROWID(pIter->iSegid, iLvl, pLvl[1].iLeafPgno)
224297 );
224298 if( pLvl->pData ) fts5DlidxLvlNext(pLvl);
224299 }
224300 }
224301 }
224302
224303 return pIter->aLvl[0].bEof;
224304}
224305static int fts5DlidxIterNext(Fts5Index *p, Fts5DlidxIter *pIter){
224306 return fts5DlidxIterNextR(p, pIter, iLvl: 0);
224307}
224308
224309/*
224310** The iterator passed as the first argument has the following fields set
224311** as follows. This function sets up the rest of the iterator so that it
224312** points to the first rowid in the doclist-index.
224313**
224314** pData:
224315** pointer to doclist-index record,
224316**
224317** When this function is called pIter->iLeafPgno is the page number the
224318** doclist is associated with (the one featuring the term).
224319*/
224320static int fts5DlidxIterFirst(Fts5DlidxIter *pIter){
224321 int i;
224322 for(i=0; i<pIter->nLvl; i++){
224323 fts5DlidxLvlNext(pLvl: &pIter->aLvl[i]);
224324 }
224325 return pIter->aLvl[0].bEof;
224326}
224327
224328
224329static int fts5DlidxIterEof(Fts5Index *p, Fts5DlidxIter *pIter){
224330 return p->rc!=SQLITE_OK || pIter->aLvl[0].bEof;
224331}
224332
224333static void fts5DlidxIterLast(Fts5Index *p, Fts5DlidxIter *pIter){
224334 int i;
224335
224336 /* Advance each level to the last entry on the last page */
224337 for(i=pIter->nLvl-1; p->rc==SQLITE_OK && i>=0; i--){
224338 Fts5DlidxLvl *pLvl = &pIter->aLvl[i];
224339 while( fts5DlidxLvlNext(pLvl)==0 );
224340 pLvl->bEof = 0;
224341
224342 if( i>0 ){
224343 Fts5DlidxLvl *pChild = &pLvl[-1];
224344 fts5DataRelease(pData: pChild->pData);
224345 memset(s: pChild, c: 0, n: sizeof(Fts5DlidxLvl));
224346 pChild->pData = fts5DataRead(p,
224347 FTS5_DLIDX_ROWID(pIter->iSegid, i-1, pLvl->iLeafPgno)
224348 );
224349 }
224350 }
224351}
224352
224353/*
224354** Move the iterator passed as the only argument to the previous entry.
224355*/
224356static int fts5DlidxLvlPrev(Fts5DlidxLvl *pLvl){
224357 int iOff = pLvl->iOff;
224358
224359 assert( pLvl->bEof==0 );
224360 if( iOff<=pLvl->iFirstOff ){
224361 pLvl->bEof = 1;
224362 }else{
224363 u8 *a = pLvl->pData->p;
224364 i64 iVal;
224365 int iLimit;
224366 int ii;
224367 int nZero = 0;
224368
224369 /* Currently iOff points to the first byte of a varint. This block
224370 ** decrements iOff until it points to the first byte of the previous
224371 ** varint. Taking care not to read any memory locations that occur
224372 ** before the buffer in memory. */
224373 iLimit = (iOff>9 ? iOff-9 : 0);
224374 for(iOff--; iOff>iLimit; iOff--){
224375 if( (a[iOff-1] & 0x80)==0 ) break;
224376 }
224377
224378 fts5GetVarint(&a[iOff], (u64*)&iVal);
224379 pLvl->iRowid -= iVal;
224380 pLvl->iLeafPgno--;
224381
224382 /* Skip backwards past any 0x00 varints. */
224383 for(ii=iOff-1; ii>=pLvl->iFirstOff && a[ii]==0x00; ii--){
224384 nZero++;
224385 }
224386 if( ii>=pLvl->iFirstOff && (a[ii] & 0x80) ){
224387 /* The byte immediately before the last 0x00 byte has the 0x80 bit
224388 ** set. So the last 0x00 is only a varint 0 if there are 8 more 0x80
224389 ** bytes before a[ii]. */
224390 int bZero = 0; /* True if last 0x00 counts */
224391 if( (ii-8)>=pLvl->iFirstOff ){
224392 int j;
224393 for(j=1; j<=8 && (a[ii-j] & 0x80); j++);
224394 bZero = (j>8);
224395 }
224396 if( bZero==0 ) nZero--;
224397 }
224398 pLvl->iLeafPgno -= nZero;
224399 pLvl->iOff = iOff - nZero;
224400 }
224401
224402 return pLvl->bEof;
224403}
224404
224405static int fts5DlidxIterPrevR(Fts5Index *p, Fts5DlidxIter *pIter, int iLvl){
224406 Fts5DlidxLvl *pLvl = &pIter->aLvl[iLvl];
224407
224408 assert( iLvl<pIter->nLvl );
224409 if( fts5DlidxLvlPrev(pLvl) ){
224410 if( (iLvl+1) < pIter->nLvl ){
224411 fts5DlidxIterPrevR(p, pIter, iLvl: iLvl+1);
224412 if( pLvl[1].bEof==0 ){
224413 fts5DataRelease(pData: pLvl->pData);
224414 memset(s: pLvl, c: 0, n: sizeof(Fts5DlidxLvl));
224415 pLvl->pData = fts5DataRead(p,
224416 FTS5_DLIDX_ROWID(pIter->iSegid, iLvl, pLvl[1].iLeafPgno)
224417 );
224418 if( pLvl->pData ){
224419 while( fts5DlidxLvlNext(pLvl)==0 );
224420 pLvl->bEof = 0;
224421 }
224422 }
224423 }
224424 }
224425
224426 return pIter->aLvl[0].bEof;
224427}
224428static int fts5DlidxIterPrev(Fts5Index *p, Fts5DlidxIter *pIter){
224429 return fts5DlidxIterPrevR(p, pIter, iLvl: 0);
224430}
224431
224432/*
224433** Free a doclist-index iterator object allocated by fts5DlidxIterInit().
224434*/
224435static void fts5DlidxIterFree(Fts5DlidxIter *pIter){
224436 if( pIter ){
224437 int i;
224438 for(i=0; i<pIter->nLvl; i++){
224439 fts5DataRelease(pData: pIter->aLvl[i].pData);
224440 }
224441 sqlite3_free(p: pIter);
224442 }
224443}
224444
224445static Fts5DlidxIter *fts5DlidxIterInit(
224446 Fts5Index *p, /* Fts5 Backend to iterate within */
224447 int bRev, /* True for ORDER BY ASC */
224448 int iSegid, /* Segment id */
224449 int iLeafPg /* Leaf page number to load dlidx for */
224450){
224451 Fts5DlidxIter *pIter = 0;
224452 int i;
224453 int bDone = 0;
224454
224455 for(i=0; p->rc==SQLITE_OK && bDone==0; i++){
224456 sqlite3_int64 nByte = sizeof(Fts5DlidxIter) + i * sizeof(Fts5DlidxLvl);
224457 Fts5DlidxIter *pNew;
224458
224459 pNew = (Fts5DlidxIter*)sqlite3_realloc64(pOld: pIter, n: nByte);
224460 if( pNew==0 ){
224461 p->rc = SQLITE_NOMEM;
224462 }else{
224463 i64 iRowid = FTS5_DLIDX_ROWID(iSegid, i, iLeafPg);
224464 Fts5DlidxLvl *pLvl = &pNew->aLvl[i];
224465 pIter = pNew;
224466 memset(s: pLvl, c: 0, n: sizeof(Fts5DlidxLvl));
224467 pLvl->pData = fts5DataRead(p, iRowid);
224468 if( pLvl->pData && (pLvl->pData->p[0] & 0x0001)==0 ){
224469 bDone = 1;
224470 }
224471 pIter->nLvl = i+1;
224472 }
224473 }
224474
224475 if( p->rc==SQLITE_OK ){
224476 pIter->iSegid = iSegid;
224477 if( bRev==0 ){
224478 fts5DlidxIterFirst(pIter);
224479 }else{
224480 fts5DlidxIterLast(p, pIter);
224481 }
224482 }
224483
224484 if( p->rc!=SQLITE_OK ){
224485 fts5DlidxIterFree(pIter);
224486 pIter = 0;
224487 }
224488
224489 return pIter;
224490}
224491
224492static i64 fts5DlidxIterRowid(Fts5DlidxIter *pIter){
224493 return pIter->aLvl[0].iRowid;
224494}
224495static int fts5DlidxIterPgno(Fts5DlidxIter *pIter){
224496 return pIter->aLvl[0].iLeafPgno;
224497}
224498
224499/*
224500** Load the next leaf page into the segment iterator.
224501*/
224502static void fts5SegIterNextPage(
224503 Fts5Index *p, /* FTS5 backend object */
224504 Fts5SegIter *pIter /* Iterator to advance to next page */
224505){
224506 Fts5Data *pLeaf;
224507 Fts5StructureSegment *pSeg = pIter->pSeg;
224508 fts5DataRelease(pData: pIter->pLeaf);
224509 pIter->iLeafPgno++;
224510 if( pIter->pNextLeaf ){
224511 pIter->pLeaf = pIter->pNextLeaf;
224512 pIter->pNextLeaf = 0;
224513 }else if( pIter->iLeafPgno<=pSeg->pgnoLast ){
224514 pIter->pLeaf = fts5LeafRead(p,
224515 FTS5_SEGMENT_ROWID(pSeg->iSegid, pIter->iLeafPgno)
224516 );
224517 }else{
224518 pIter->pLeaf = 0;
224519 }
224520 pLeaf = pIter->pLeaf;
224521
224522 if( pLeaf ){
224523 pIter->iPgidxOff = pLeaf->szLeaf;
224524 if( fts5LeafIsTermless(pLeaf) ){
224525 pIter->iEndofDoclist = pLeaf->nn+1;
224526 }else{
224527 pIter->iPgidxOff += fts5GetVarint32(&pLeaf->p[pIter->iPgidxOff],
224528 pIter->iEndofDoclist
224529 );
224530 }
224531 }
224532}
224533
224534/*
224535** Argument p points to a buffer containing a varint to be interpreted as a
224536** position list size field. Read the varint and return the number of bytes
224537** read. Before returning, set *pnSz to the number of bytes in the position
224538** list, and *pbDel to true if the delete flag is set, or false otherwise.
224539*/
224540static int fts5GetPoslistSize(const u8 *p, int *pnSz, int *pbDel){
224541 int nSz;
224542 int n = 0;
224543 fts5FastGetVarint32(p, n, nSz);
224544 assert_nc( nSz>=0 );
224545 *pnSz = nSz/2;
224546 *pbDel = nSz & 0x0001;
224547 return n;
224548}
224549
224550/*
224551** Fts5SegIter.iLeafOffset currently points to the first byte of a
224552** position-list size field. Read the value of the field and store it
224553** in the following variables:
224554**
224555** Fts5SegIter.nPos
224556** Fts5SegIter.bDel
224557**
224558** Leave Fts5SegIter.iLeafOffset pointing to the first byte of the
224559** position list content (if any).
224560*/
224561static void fts5SegIterLoadNPos(Fts5Index *p, Fts5SegIter *pIter){
224562 if( p->rc==SQLITE_OK ){
224563 int iOff = pIter->iLeafOffset; /* Offset to read at */
224564 ASSERT_SZLEAF_OK(pIter->pLeaf);
224565 if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
224566 int iEod = MIN(pIter->iEndofDoclist, pIter->pLeaf->szLeaf);
224567 pIter->bDel = 0;
224568 pIter->nPos = 1;
224569 if( iOff<iEod && pIter->pLeaf->p[iOff]==0 ){
224570 pIter->bDel = 1;
224571 iOff++;
224572 if( iOff<iEod && pIter->pLeaf->p[iOff]==0 ){
224573 pIter->nPos = 1;
224574 iOff++;
224575 }else{
224576 pIter->nPos = 0;
224577 }
224578 }
224579 }else{
224580 int nSz;
224581 fts5FastGetVarint32(pIter->pLeaf->p, iOff, nSz);
224582 pIter->bDel = (nSz & 0x0001);
224583 pIter->nPos = nSz>>1;
224584 assert_nc( pIter->nPos>=0 );
224585 }
224586 pIter->iLeafOffset = iOff;
224587 }
224588}
224589
224590static void fts5SegIterLoadRowid(Fts5Index *p, Fts5SegIter *pIter){
224591 u8 *a = pIter->pLeaf->p; /* Buffer to read data from */
224592 i64 iOff = pIter->iLeafOffset;
224593
224594 ASSERT_SZLEAF_OK(pIter->pLeaf);
224595 if( iOff>=pIter->pLeaf->szLeaf ){
224596 fts5SegIterNextPage(p, pIter);
224597 if( pIter->pLeaf==0 ){
224598 if( p->rc==SQLITE_OK ) p->rc = FTS5_CORRUPT;
224599 return;
224600 }
224601 iOff = 4;
224602 a = pIter->pLeaf->p;
224603 }
224604 iOff += sqlite3Fts5GetVarint(&a[iOff], (u64*)&pIter->iRowid);
224605 pIter->iLeafOffset = iOff;
224606}
224607
224608/*
224609** Fts5SegIter.iLeafOffset currently points to the first byte of the
224610** "nSuffix" field of a term. Function parameter nKeep contains the value
224611** of the "nPrefix" field (if there was one - it is passed 0 if this is
224612** the first term in the segment).
224613**
224614** This function populates:
224615**
224616** Fts5SegIter.term
224617** Fts5SegIter.rowid
224618**
224619** accordingly and leaves (Fts5SegIter.iLeafOffset) set to the content of
224620** the first position list. The position list belonging to document
224621** (Fts5SegIter.iRowid).
224622*/
224623static void fts5SegIterLoadTerm(Fts5Index *p, Fts5SegIter *pIter, int nKeep){
224624 u8 *a = pIter->pLeaf->p; /* Buffer to read data from */
224625 i64 iOff = pIter->iLeafOffset; /* Offset to read at */
224626 int nNew; /* Bytes of new data */
224627
224628 iOff += fts5GetVarint32(&a[iOff], nNew);
224629 if( iOff+nNew>pIter->pLeaf->szLeaf || nKeep>pIter->term.n || nNew==0 ){
224630 p->rc = FTS5_CORRUPT;
224631 return;
224632 }
224633 pIter->term.n = nKeep;
224634 fts5BufferAppendBlob(&p->rc, &pIter->term, nNew, &a[iOff]);
224635 assert( pIter->term.n<=pIter->term.nSpace );
224636 iOff += nNew;
224637 pIter->iTermLeafOffset = iOff;
224638 pIter->iTermLeafPgno = pIter->iLeafPgno;
224639 pIter->iLeafOffset = iOff;
224640
224641 if( pIter->iPgidxOff>=pIter->pLeaf->nn ){
224642 pIter->iEndofDoclist = pIter->pLeaf->nn+1;
224643 }else{
224644 int nExtra;
224645 pIter->iPgidxOff += fts5GetVarint32(&a[pIter->iPgidxOff], nExtra);
224646 pIter->iEndofDoclist += nExtra;
224647 }
224648
224649 fts5SegIterLoadRowid(p, pIter);
224650}
224651
224652static void fts5SegIterNext(Fts5Index*, Fts5SegIter*, int*);
224653static void fts5SegIterNext_Reverse(Fts5Index*, Fts5SegIter*, int*);
224654static void fts5SegIterNext_None(Fts5Index*, Fts5SegIter*, int*);
224655
224656static void fts5SegIterSetNext(Fts5Index *p, Fts5SegIter *pIter){
224657 if( pIter->flags & FTS5_SEGITER_REVERSE ){
224658 pIter->xNext = fts5SegIterNext_Reverse;
224659 }else if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
224660 pIter->xNext = fts5SegIterNext_None;
224661 }else{
224662 pIter->xNext = fts5SegIterNext;
224663 }
224664}
224665
224666/*
224667** Initialize the iterator object pIter to iterate through the entries in
224668** segment pSeg. The iterator is left pointing to the first entry when
224669** this function returns.
224670**
224671** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
224672** an error has already occurred when this function is called, it is a no-op.
224673*/
224674static void fts5SegIterInit(
224675 Fts5Index *p, /* FTS index object */
224676 Fts5StructureSegment *pSeg, /* Description of segment */
224677 Fts5SegIter *pIter /* Object to populate */
224678){
224679 if( pSeg->pgnoFirst==0 ){
224680 /* This happens if the segment is being used as an input to an incremental
224681 ** merge and all data has already been "trimmed". See function
224682 ** fts5TrimSegments() for details. In this case leave the iterator empty.
224683 ** The caller will see the (pIter->pLeaf==0) and assume the iterator is
224684 ** at EOF already. */
224685 assert( pIter->pLeaf==0 );
224686 return;
224687 }
224688
224689 if( p->rc==SQLITE_OK ){
224690 memset(s: pIter, c: 0, n: sizeof(*pIter));
224691 fts5SegIterSetNext(p, pIter);
224692 pIter->pSeg = pSeg;
224693 pIter->iLeafPgno = pSeg->pgnoFirst-1;
224694 fts5SegIterNextPage(p, pIter);
224695 }
224696
224697 if( p->rc==SQLITE_OK ){
224698 pIter->iLeafOffset = 4;
224699 assert( pIter->pLeaf!=0 );
224700 assert_nc( pIter->pLeaf->nn>4 );
224701 assert_nc( fts5LeafFirstTermOff(pIter->pLeaf)==4 );
224702 pIter->iPgidxOff = pIter->pLeaf->szLeaf+1;
224703 fts5SegIterLoadTerm(p, pIter, nKeep: 0);
224704 fts5SegIterLoadNPos(p, pIter);
224705 }
224706}
224707
224708/*
224709** This function is only ever called on iterators created by calls to
224710** Fts5IndexQuery() with the FTS5INDEX_QUERY_DESC flag set.
224711**
224712** The iterator is in an unusual state when this function is called: the
224713** Fts5SegIter.iLeafOffset variable is set to the offset of the start of
224714** the position-list size field for the first relevant rowid on the page.
224715** Fts5SegIter.rowid is set, but nPos and bDel are not.
224716**
224717** This function advances the iterator so that it points to the last
224718** relevant rowid on the page and, if necessary, initializes the
224719** aRowidOffset[] and iRowidOffset variables. At this point the iterator
224720** is in its regular state - Fts5SegIter.iLeafOffset points to the first
224721** byte of the position list content associated with said rowid.
224722*/
224723static void fts5SegIterReverseInitPage(Fts5Index *p, Fts5SegIter *pIter){
224724 int eDetail = p->pConfig->eDetail;
224725 int n = pIter->pLeaf->szLeaf;
224726 int i = pIter->iLeafOffset;
224727 u8 *a = pIter->pLeaf->p;
224728 int iRowidOffset = 0;
224729
224730 if( n>pIter->iEndofDoclist ){
224731 n = pIter->iEndofDoclist;
224732 }
224733
224734 ASSERT_SZLEAF_OK(pIter->pLeaf);
224735 while( 1 ){
224736 u64 iDelta = 0;
224737
224738 if( eDetail==FTS5_DETAIL_NONE ){
224739 /* todo */
224740 if( i<n && a[i]==0 ){
224741 i++;
224742 if( i<n && a[i]==0 ) i++;
224743 }
224744 }else{
224745 int nPos;
224746 int bDummy;
224747 i += fts5GetPoslistSize(p: &a[i], pnSz: &nPos, pbDel: &bDummy);
224748 i += nPos;
224749 }
224750 if( i>=n ) break;
224751 i += fts5GetVarint(&a[i], &iDelta);
224752 pIter->iRowid += iDelta;
224753
224754 /* If necessary, grow the pIter->aRowidOffset[] array. */
224755 if( iRowidOffset>=pIter->nRowidOffset ){
224756 int nNew = pIter->nRowidOffset + 8;
224757 int *aNew = (int*)sqlite3_realloc64(pOld: pIter->aRowidOffset,n: nNew*sizeof(int));
224758 if( aNew==0 ){
224759 p->rc = SQLITE_NOMEM;
224760 break;
224761 }
224762 pIter->aRowidOffset = aNew;
224763 pIter->nRowidOffset = nNew;
224764 }
224765
224766 pIter->aRowidOffset[iRowidOffset++] = pIter->iLeafOffset;
224767 pIter->iLeafOffset = i;
224768 }
224769 pIter->iRowidOffset = iRowidOffset;
224770 fts5SegIterLoadNPos(p, pIter);
224771}
224772
224773/*
224774**
224775*/
224776static void fts5SegIterReverseNewPage(Fts5Index *p, Fts5SegIter *pIter){
224777 assert( pIter->flags & FTS5_SEGITER_REVERSE );
224778 assert( pIter->flags & FTS5_SEGITER_ONETERM );
224779
224780 fts5DataRelease(pData: pIter->pLeaf);
224781 pIter->pLeaf = 0;
224782 while( p->rc==SQLITE_OK && pIter->iLeafPgno>pIter->iTermLeafPgno ){
224783 Fts5Data *pNew;
224784 pIter->iLeafPgno--;
224785 pNew = fts5DataRead(p, FTS5_SEGMENT_ROWID(
224786 pIter->pSeg->iSegid, pIter->iLeafPgno
224787 ));
224788 if( pNew ){
224789 /* iTermLeafOffset may be equal to szLeaf if the term is the last
224790 ** thing on the page - i.e. the first rowid is on the following page.
224791 ** In this case leave pIter->pLeaf==0, this iterator is at EOF. */
224792 if( pIter->iLeafPgno==pIter->iTermLeafPgno ){
224793 assert( pIter->pLeaf==0 );
224794 if( pIter->iTermLeafOffset<pNew->szLeaf ){
224795 pIter->pLeaf = pNew;
224796 pIter->iLeafOffset = pIter->iTermLeafOffset;
224797 }
224798 }else{
224799 int iRowidOff;
224800 iRowidOff = fts5LeafFirstRowidOff(pNew);
224801 if( iRowidOff ){
224802 if( iRowidOff>=pNew->szLeaf ){
224803 p->rc = FTS5_CORRUPT;
224804 }else{
224805 pIter->pLeaf = pNew;
224806 pIter->iLeafOffset = iRowidOff;
224807 }
224808 }
224809 }
224810
224811 if( pIter->pLeaf ){
224812 u8 *a = &pIter->pLeaf->p[pIter->iLeafOffset];
224813 pIter->iLeafOffset += fts5GetVarint(a, (u64*)&pIter->iRowid);
224814 break;
224815 }else{
224816 fts5DataRelease(pData: pNew);
224817 }
224818 }
224819 }
224820
224821 if( pIter->pLeaf ){
224822 pIter->iEndofDoclist = pIter->pLeaf->nn+1;
224823 fts5SegIterReverseInitPage(p, pIter);
224824 }
224825}
224826
224827/*
224828** Return true if the iterator passed as the second argument currently
224829** points to a delete marker. A delete marker is an entry with a 0 byte
224830** position-list.
224831*/
224832static int fts5MultiIterIsEmpty(Fts5Index *p, Fts5Iter *pIter){
224833 Fts5SegIter *pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
224834 return (p->rc==SQLITE_OK && pSeg->pLeaf && pSeg->nPos==0);
224835}
224836
224837/*
224838** Advance iterator pIter to the next entry.
224839**
224840** This version of fts5SegIterNext() is only used by reverse iterators.
224841*/
224842static void fts5SegIterNext_Reverse(
224843 Fts5Index *p, /* FTS5 backend object */
224844 Fts5SegIter *pIter, /* Iterator to advance */
224845 int *pbUnused /* Unused */
224846){
224847 assert( pIter->flags & FTS5_SEGITER_REVERSE );
224848 assert( pIter->pNextLeaf==0 );
224849 UNUSED_PARAM(pbUnused);
224850
224851 if( pIter->iRowidOffset>0 ){
224852 u8 *a = pIter->pLeaf->p;
224853 int iOff;
224854 u64 iDelta;
224855
224856 pIter->iRowidOffset--;
224857 pIter->iLeafOffset = pIter->aRowidOffset[pIter->iRowidOffset];
224858 fts5SegIterLoadNPos(p, pIter);
224859 iOff = pIter->iLeafOffset;
224860 if( p->pConfig->eDetail!=FTS5_DETAIL_NONE ){
224861 iOff += pIter->nPos;
224862 }
224863 fts5GetVarint(&a[iOff], &iDelta);
224864 pIter->iRowid -= iDelta;
224865 }else{
224866 fts5SegIterReverseNewPage(p, pIter);
224867 }
224868}
224869
224870/*
224871** Advance iterator pIter to the next entry.
224872**
224873** This version of fts5SegIterNext() is only used if detail=none and the
224874** iterator is not a reverse direction iterator.
224875*/
224876static void fts5SegIterNext_None(
224877 Fts5Index *p, /* FTS5 backend object */
224878 Fts5SegIter *pIter, /* Iterator to advance */
224879 int *pbNewTerm /* OUT: Set for new term */
224880){
224881 int iOff;
224882
224883 assert( p->rc==SQLITE_OK );
224884 assert( (pIter->flags & FTS5_SEGITER_REVERSE)==0 );
224885 assert( p->pConfig->eDetail==FTS5_DETAIL_NONE );
224886
224887 ASSERT_SZLEAF_OK(pIter->pLeaf);
224888 iOff = pIter->iLeafOffset;
224889
224890 /* Next entry is on the next page */
224891 if( pIter->pSeg && iOff>=pIter->pLeaf->szLeaf ){
224892 fts5SegIterNextPage(p, pIter);
224893 if( p->rc || pIter->pLeaf==0 ) return;
224894 pIter->iRowid = 0;
224895 iOff = 4;
224896 }
224897
224898 if( iOff<pIter->iEndofDoclist ){
224899 /* Next entry is on the current page */
224900 i64 iDelta;
224901 iOff += sqlite3Fts5GetVarint(&pIter->pLeaf->p[iOff], (u64*)&iDelta);
224902 pIter->iLeafOffset = iOff;
224903 pIter->iRowid += iDelta;
224904 }else if( (pIter->flags & FTS5_SEGITER_ONETERM)==0 ){
224905 if( pIter->pSeg ){
224906 int nKeep = 0;
224907 if( iOff!=fts5LeafFirstTermOff(pLeaf: pIter->pLeaf) ){
224908 iOff += fts5GetVarint32(&pIter->pLeaf->p[iOff], nKeep);
224909 }
224910 pIter->iLeafOffset = iOff;
224911 fts5SegIterLoadTerm(p, pIter, nKeep);
224912 }else{
224913 const u8 *pList = 0;
224914 const char *zTerm = 0;
224915 int nList;
224916 sqlite3Fts5HashScanNext(p: p->pHash);
224917 sqlite3Fts5HashScanEntry(pHash: p->pHash, pzTerm: &zTerm, ppDoclist: &pList, pnDoclist: &nList);
224918 if( pList==0 ) goto next_none_eof;
224919 pIter->pLeaf->p = (u8*)pList;
224920 pIter->pLeaf->nn = nList;
224921 pIter->pLeaf->szLeaf = nList;
224922 pIter->iEndofDoclist = nList;
224923 sqlite3Fts5BufferSet(pRc: &p->rc,pBuf: &pIter->term, nData: (int)strlen(s: zTerm), pData: (u8*)zTerm);
224924 pIter->iLeafOffset = fts5GetVarint(pList, (u64*)&pIter->iRowid);
224925 }
224926
224927 if( pbNewTerm ) *pbNewTerm = 1;
224928 }else{
224929 goto next_none_eof;
224930 }
224931
224932 fts5SegIterLoadNPos(p, pIter);
224933
224934 return;
224935 next_none_eof:
224936 fts5DataRelease(pData: pIter->pLeaf);
224937 pIter->pLeaf = 0;
224938}
224939
224940
224941/*
224942** Advance iterator pIter to the next entry.
224943**
224944** If an error occurs, Fts5Index.rc is set to an appropriate error code. It
224945** is not considered an error if the iterator reaches EOF. If an error has
224946** already occurred when this function is called, it is a no-op.
224947*/
224948static void fts5SegIterNext(
224949 Fts5Index *p, /* FTS5 backend object */
224950 Fts5SegIter *pIter, /* Iterator to advance */
224951 int *pbNewTerm /* OUT: Set for new term */
224952){
224953 Fts5Data *pLeaf = pIter->pLeaf;
224954 int iOff;
224955 int bNewTerm = 0;
224956 int nKeep = 0;
224957 u8 *a;
224958 int n;
224959
224960 assert( pbNewTerm==0 || *pbNewTerm==0 );
224961 assert( p->pConfig->eDetail!=FTS5_DETAIL_NONE );
224962
224963 /* Search for the end of the position list within the current page. */
224964 a = pLeaf->p;
224965 n = pLeaf->szLeaf;
224966
224967 ASSERT_SZLEAF_OK(pLeaf);
224968 iOff = pIter->iLeafOffset + pIter->nPos;
224969
224970 if( iOff<n ){
224971 /* The next entry is on the current page. */
224972 assert_nc( iOff<=pIter->iEndofDoclist );
224973 if( iOff>=pIter->iEndofDoclist ){
224974 bNewTerm = 1;
224975 if( iOff!=fts5LeafFirstTermOff(pLeaf) ){
224976 iOff += fts5GetVarint32(&a[iOff], nKeep);
224977 }
224978 }else{
224979 u64 iDelta;
224980 iOff += sqlite3Fts5GetVarint(&a[iOff], &iDelta);
224981 pIter->iRowid += iDelta;
224982 assert_nc( iDelta>0 );
224983 }
224984 pIter->iLeafOffset = iOff;
224985
224986 }else if( pIter->pSeg==0 ){
224987 const u8 *pList = 0;
224988 const char *zTerm = 0;
224989 int nList = 0;
224990 assert( (pIter->flags & FTS5_SEGITER_ONETERM) || pbNewTerm );
224991 if( 0==(pIter->flags & FTS5_SEGITER_ONETERM) ){
224992 sqlite3Fts5HashScanNext(p: p->pHash);
224993 sqlite3Fts5HashScanEntry(pHash: p->pHash, pzTerm: &zTerm, ppDoclist: &pList, pnDoclist: &nList);
224994 }
224995 if( pList==0 ){
224996 fts5DataRelease(pData: pIter->pLeaf);
224997 pIter->pLeaf = 0;
224998 }else{
224999 pIter->pLeaf->p = (u8*)pList;
225000 pIter->pLeaf->nn = nList;
225001 pIter->pLeaf->szLeaf = nList;
225002 pIter->iEndofDoclist = nList+1;
225003 sqlite3Fts5BufferSet(pRc: &p->rc, pBuf: &pIter->term, nData: (int)strlen(s: zTerm),
225004 pData: (u8*)zTerm);
225005 pIter->iLeafOffset = fts5GetVarint(pList, (u64*)&pIter->iRowid);
225006 *pbNewTerm = 1;
225007 }
225008 }else{
225009 iOff = 0;
225010 /* Next entry is not on the current page */
225011 while( iOff==0 ){
225012 fts5SegIterNextPage(p, pIter);
225013 pLeaf = pIter->pLeaf;
225014 if( pLeaf==0 ) break;
225015 ASSERT_SZLEAF_OK(pLeaf);
225016 if( (iOff = fts5LeafFirstRowidOff(pLeaf)) && iOff<pLeaf->szLeaf ){
225017 iOff += sqlite3Fts5GetVarint(&pLeaf->p[iOff], (u64*)&pIter->iRowid);
225018 pIter->iLeafOffset = iOff;
225019
225020 if( pLeaf->nn>pLeaf->szLeaf ){
225021 pIter->iPgidxOff = pLeaf->szLeaf + fts5GetVarint32(
225022 &pLeaf->p[pLeaf->szLeaf], pIter->iEndofDoclist
225023 );
225024 }
225025 }
225026 else if( pLeaf->nn>pLeaf->szLeaf ){
225027 pIter->iPgidxOff = pLeaf->szLeaf + fts5GetVarint32(
225028 &pLeaf->p[pLeaf->szLeaf], iOff
225029 );
225030 pIter->iLeafOffset = iOff;
225031 pIter->iEndofDoclist = iOff;
225032 bNewTerm = 1;
225033 }
225034 assert_nc( iOff<pLeaf->szLeaf );
225035 if( iOff>pLeaf->szLeaf ){
225036 p->rc = FTS5_CORRUPT;
225037 return;
225038 }
225039 }
225040 }
225041
225042 /* Check if the iterator is now at EOF. If so, return early. */
225043 if( pIter->pLeaf ){
225044 if( bNewTerm ){
225045 if( pIter->flags & FTS5_SEGITER_ONETERM ){
225046 fts5DataRelease(pData: pIter->pLeaf);
225047 pIter->pLeaf = 0;
225048 }else{
225049 fts5SegIterLoadTerm(p, pIter, nKeep);
225050 fts5SegIterLoadNPos(p, pIter);
225051 if( pbNewTerm ) *pbNewTerm = 1;
225052 }
225053 }else{
225054 /* The following could be done by calling fts5SegIterLoadNPos(). But
225055 ** this block is particularly performance critical, so equivalent
225056 ** code is inlined. */
225057 int nSz;
225058 assert_nc( pIter->iLeafOffset<=pIter->pLeaf->nn );
225059 fts5FastGetVarint32(pIter->pLeaf->p, pIter->iLeafOffset, nSz);
225060 pIter->bDel = (nSz & 0x0001);
225061 pIter->nPos = nSz>>1;
225062 assert_nc( pIter->nPos>=0 );
225063 }
225064 }
225065}
225066
225067#define SWAPVAL(T, a, b) { T tmp; tmp=a; a=b; b=tmp; }
225068
225069#define fts5IndexSkipVarint(a, iOff) { \
225070 int iEnd = iOff+9; \
225071 while( (a[iOff++] & 0x80) && iOff<iEnd ); \
225072}
225073
225074/*
225075** Iterator pIter currently points to the first rowid in a doclist. This
225076** function sets the iterator up so that iterates in reverse order through
225077** the doclist.
225078*/
225079static void fts5SegIterReverse(Fts5Index *p, Fts5SegIter *pIter){
225080 Fts5DlidxIter *pDlidx = pIter->pDlidx;
225081 Fts5Data *pLast = 0;
225082 int pgnoLast = 0;
225083
225084 if( pDlidx ){
225085 int iSegid = pIter->pSeg->iSegid;
225086 pgnoLast = fts5DlidxIterPgno(pIter: pDlidx);
225087 pLast = fts5LeafRead(p, FTS5_SEGMENT_ROWID(iSegid, pgnoLast));
225088 }else{
225089 Fts5Data *pLeaf = pIter->pLeaf; /* Current leaf data */
225090
225091 /* Currently, Fts5SegIter.iLeafOffset points to the first byte of
225092 ** position-list content for the current rowid. Back it up so that it
225093 ** points to the start of the position-list size field. */
225094 int iPoslist;
225095 if( pIter->iTermLeafPgno==pIter->iLeafPgno ){
225096 iPoslist = pIter->iTermLeafOffset;
225097 }else{
225098 iPoslist = 4;
225099 }
225100 fts5IndexSkipVarint(pLeaf->p, iPoslist);
225101 pIter->iLeafOffset = iPoslist;
225102
225103 /* If this condition is true then the largest rowid for the current
225104 ** term may not be stored on the current page. So search forward to
225105 ** see where said rowid really is. */
225106 if( pIter->iEndofDoclist>=pLeaf->szLeaf ){
225107 int pgno;
225108 Fts5StructureSegment *pSeg = pIter->pSeg;
225109
225110 /* The last rowid in the doclist may not be on the current page. Search
225111 ** forward to find the page containing the last rowid. */
225112 for(pgno=pIter->iLeafPgno+1; !p->rc && pgno<=pSeg->pgnoLast; pgno++){
225113 i64 iAbs = FTS5_SEGMENT_ROWID(pSeg->iSegid, pgno);
225114 Fts5Data *pNew = fts5LeafRead(p, iRowid: iAbs);
225115 if( pNew ){
225116 int iRowid, bTermless;
225117 iRowid = fts5LeafFirstRowidOff(pNew);
225118 bTermless = fts5LeafIsTermless(pNew);
225119 if( iRowid ){
225120 SWAPVAL(Fts5Data*, pNew, pLast);
225121 pgnoLast = pgno;
225122 }
225123 fts5DataRelease(pData: pNew);
225124 if( bTermless==0 ) break;
225125 }
225126 }
225127 }
225128 }
225129
225130 /* If pLast is NULL at this point, then the last rowid for this doclist
225131 ** lies on the page currently indicated by the iterator. In this case
225132 ** pIter->iLeafOffset is already set to point to the position-list size
225133 ** field associated with the first relevant rowid on the page.
225134 **
225135 ** Or, if pLast is non-NULL, then it is the page that contains the last
225136 ** rowid. In this case configure the iterator so that it points to the
225137 ** first rowid on this page.
225138 */
225139 if( pLast ){
225140 int iOff;
225141 fts5DataRelease(pData: pIter->pLeaf);
225142 pIter->pLeaf = pLast;
225143 pIter->iLeafPgno = pgnoLast;
225144 iOff = fts5LeafFirstRowidOff(pLast);
225145 if( iOff>pLast->szLeaf ){
225146 p->rc = FTS5_CORRUPT;
225147 return;
225148 }
225149 iOff += fts5GetVarint(&pLast->p[iOff], (u64*)&pIter->iRowid);
225150 pIter->iLeafOffset = iOff;
225151
225152 if( fts5LeafIsTermless(pLast) ){
225153 pIter->iEndofDoclist = pLast->nn+1;
225154 }else{
225155 pIter->iEndofDoclist = fts5LeafFirstTermOff(pLeaf: pLast);
225156 }
225157 }
225158
225159 fts5SegIterReverseInitPage(p, pIter);
225160}
225161
225162/*
225163** Iterator pIter currently points to the first rowid of a doclist.
225164** There is a doclist-index associated with the final term on the current
225165** page. If the current term is the last term on the page, load the
225166** doclist-index from disk and initialize an iterator at (pIter->pDlidx).
225167*/
225168static void fts5SegIterLoadDlidx(Fts5Index *p, Fts5SegIter *pIter){
225169 int iSeg = pIter->pSeg->iSegid;
225170 int bRev = (pIter->flags & FTS5_SEGITER_REVERSE);
225171 Fts5Data *pLeaf = pIter->pLeaf; /* Current leaf data */
225172
225173 assert( pIter->flags & FTS5_SEGITER_ONETERM );
225174 assert( pIter->pDlidx==0 );
225175
225176 /* Check if the current doclist ends on this page. If it does, return
225177 ** early without loading the doclist-index (as it belongs to a different
225178 ** term. */
225179 if( pIter->iTermLeafPgno==pIter->iLeafPgno
225180 && pIter->iEndofDoclist<pLeaf->szLeaf
225181 ){
225182 return;
225183 }
225184
225185 pIter->pDlidx = fts5DlidxIterInit(p, bRev, iSegid: iSeg, iLeafPg: pIter->iTermLeafPgno);
225186}
225187
225188/*
225189** The iterator object passed as the second argument currently contains
225190** no valid values except for the Fts5SegIter.pLeaf member variable. This
225191** function searches the leaf page for a term matching (pTerm/nTerm).
225192**
225193** If the specified term is found on the page, then the iterator is left
225194** pointing to it. If argument bGe is zero and the term is not found,
225195** the iterator is left pointing at EOF.
225196**
225197** If bGe is non-zero and the specified term is not found, then the
225198** iterator is left pointing to the smallest term in the segment that
225199** is larger than the specified term, even if this term is not on the
225200** current page.
225201*/
225202static void fts5LeafSeek(
225203 Fts5Index *p, /* Leave any error code here */
225204 int bGe, /* True for a >= search */
225205 Fts5SegIter *pIter, /* Iterator to seek */
225206 const u8 *pTerm, int nTerm /* Term to search for */
225207){
225208 u32 iOff;
225209 const u8 *a = pIter->pLeaf->p;
225210 u32 n = (u32)pIter->pLeaf->nn;
225211
225212 u32 nMatch = 0;
225213 u32 nKeep = 0;
225214 u32 nNew = 0;
225215 u32 iTermOff;
225216 u32 iPgidx; /* Current offset in pgidx */
225217 int bEndOfPage = 0;
225218
225219 assert( p->rc==SQLITE_OK );
225220
225221 iPgidx = (u32)pIter->pLeaf->szLeaf;
225222 iPgidx += fts5GetVarint32(&a[iPgidx], iTermOff);
225223 iOff = iTermOff;
225224 if( iOff>n ){
225225 p->rc = FTS5_CORRUPT;
225226 return;
225227 }
225228
225229 while( 1 ){
225230
225231 /* Figure out how many new bytes are in this term */
225232 fts5FastGetVarint32(a, iOff, nNew);
225233 if( nKeep<nMatch ){
225234 goto search_failed;
225235 }
225236
225237 assert( nKeep>=nMatch );
225238 if( nKeep==nMatch ){
225239 u32 nCmp;
225240 u32 i;
225241 nCmp = (u32)MIN(nNew, nTerm-nMatch);
225242 for(i=0; i<nCmp; i++){
225243 if( a[iOff+i]!=pTerm[nMatch+i] ) break;
225244 }
225245 nMatch += i;
225246
225247 if( (u32)nTerm==nMatch ){
225248 if( i==nNew ){
225249 goto search_success;
225250 }else{
225251 goto search_failed;
225252 }
225253 }else if( i<nNew && a[iOff+i]>pTerm[nMatch] ){
225254 goto search_failed;
225255 }
225256 }
225257
225258 if( iPgidx>=n ){
225259 bEndOfPage = 1;
225260 break;
225261 }
225262
225263 iPgidx += fts5GetVarint32(&a[iPgidx], nKeep);
225264 iTermOff += nKeep;
225265 iOff = iTermOff;
225266
225267 if( iOff>=n ){
225268 p->rc = FTS5_CORRUPT;
225269 return;
225270 }
225271
225272 /* Read the nKeep field of the next term. */
225273 fts5FastGetVarint32(a, iOff, nKeep);
225274 }
225275
225276 search_failed:
225277 if( bGe==0 ){
225278 fts5DataRelease(pData: pIter->pLeaf);
225279 pIter->pLeaf = 0;
225280 return;
225281 }else if( bEndOfPage ){
225282 do {
225283 fts5SegIterNextPage(p, pIter);
225284 if( pIter->pLeaf==0 ) return;
225285 a = pIter->pLeaf->p;
225286 if( fts5LeafIsTermless(pIter->pLeaf)==0 ){
225287 iPgidx = (u32)pIter->pLeaf->szLeaf;
225288 iPgidx += fts5GetVarint32(&pIter->pLeaf->p[iPgidx], iOff);
225289 if( iOff<4 || (i64)iOff>=pIter->pLeaf->szLeaf ){
225290 p->rc = FTS5_CORRUPT;
225291 return;
225292 }else{
225293 nKeep = 0;
225294 iTermOff = iOff;
225295 n = (u32)pIter->pLeaf->nn;
225296 iOff += fts5GetVarint32(&a[iOff], nNew);
225297 break;
225298 }
225299 }
225300 }while( 1 );
225301 }
225302
225303 search_success:
225304 if( (i64)iOff+nNew>n || nNew<1 ){
225305 p->rc = FTS5_CORRUPT;
225306 return;
225307 }
225308 pIter->iLeafOffset = iOff + nNew;
225309 pIter->iTermLeafOffset = pIter->iLeafOffset;
225310 pIter->iTermLeafPgno = pIter->iLeafPgno;
225311
225312 fts5BufferSet(&p->rc, &pIter->term, nKeep, pTerm);
225313 fts5BufferAppendBlob(&p->rc, &pIter->term, nNew, &a[iOff]);
225314
225315 if( iPgidx>=n ){
225316 pIter->iEndofDoclist = pIter->pLeaf->nn+1;
225317 }else{
225318 int nExtra;
225319 iPgidx += fts5GetVarint32(&a[iPgidx], nExtra);
225320 pIter->iEndofDoclist = iTermOff + nExtra;
225321 }
225322 pIter->iPgidxOff = iPgidx;
225323
225324 fts5SegIterLoadRowid(p, pIter);
225325 fts5SegIterLoadNPos(p, pIter);
225326}
225327
225328static sqlite3_stmt *fts5IdxSelectStmt(Fts5Index *p){
225329 if( p->pIdxSelect==0 ){
225330 Fts5Config *pConfig = p->pConfig;
225331 fts5IndexPrepareStmt(p, ppStmt: &p->pIdxSelect, zSql: sqlite3_mprintf(
225332 zFormat: "SELECT pgno FROM '%q'.'%q_idx' WHERE "
225333 "segid=? AND term<=? ORDER BY term DESC LIMIT 1",
225334 pConfig->zDb, pConfig->zName
225335 ));
225336 }
225337 return p->pIdxSelect;
225338}
225339
225340/*
225341** Initialize the object pIter to point to term pTerm/nTerm within segment
225342** pSeg. If there is no such term in the index, the iterator is set to EOF.
225343**
225344** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
225345** an error has already occurred when this function is called, it is a no-op.
225346*/
225347static void fts5SegIterSeekInit(
225348 Fts5Index *p, /* FTS5 backend */
225349 const u8 *pTerm, int nTerm, /* Term to seek to */
225350 int flags, /* Mask of FTS5INDEX_XXX flags */
225351 Fts5StructureSegment *pSeg, /* Description of segment */
225352 Fts5SegIter *pIter /* Object to populate */
225353){
225354 int iPg = 1;
225355 int bGe = (flags & FTS5INDEX_QUERY_SCAN);
225356 int bDlidx = 0; /* True if there is a doclist-index */
225357 sqlite3_stmt *pIdxSelect = 0;
225358
225359 assert( bGe==0 || (flags & FTS5INDEX_QUERY_DESC)==0 );
225360 assert( pTerm && nTerm );
225361 memset(s: pIter, c: 0, n: sizeof(*pIter));
225362 pIter->pSeg = pSeg;
225363
225364 /* This block sets stack variable iPg to the leaf page number that may
225365 ** contain term (pTerm/nTerm), if it is present in the segment. */
225366 pIdxSelect = fts5IdxSelectStmt(p);
225367 if( p->rc ) return;
225368 sqlite3_bind_int(p: pIdxSelect, i: 1, iValue: pSeg->iSegid);
225369 sqlite3_bind_blob(pStmt: pIdxSelect, i: 2, zData: pTerm, nData: nTerm, SQLITE_STATIC);
225370 if( SQLITE_ROW==sqlite3_step(pStmt: pIdxSelect) ){
225371 i64 val = sqlite3_column_int(pStmt: pIdxSelect, i: 0);
225372 iPg = (int)(val>>1);
225373 bDlidx = (val & 0x0001);
225374 }
225375 p->rc = sqlite3_reset(pStmt: pIdxSelect);
225376 sqlite3_bind_null(pStmt: pIdxSelect, i: 2);
225377
225378 if( iPg<pSeg->pgnoFirst ){
225379 iPg = pSeg->pgnoFirst;
225380 bDlidx = 0;
225381 }
225382
225383 pIter->iLeafPgno = iPg - 1;
225384 fts5SegIterNextPage(p, pIter);
225385
225386 if( pIter->pLeaf ){
225387 fts5LeafSeek(p, bGe, pIter, pTerm, nTerm);
225388 }
225389
225390 if( p->rc==SQLITE_OK && bGe==0 ){
225391 pIter->flags |= FTS5_SEGITER_ONETERM;
225392 if( pIter->pLeaf ){
225393 if( flags & FTS5INDEX_QUERY_DESC ){
225394 pIter->flags |= FTS5_SEGITER_REVERSE;
225395 }
225396 if( bDlidx ){
225397 fts5SegIterLoadDlidx(p, pIter);
225398 }
225399 if( flags & FTS5INDEX_QUERY_DESC ){
225400 fts5SegIterReverse(p, pIter);
225401 }
225402 }
225403 }
225404
225405 fts5SegIterSetNext(p, pIter);
225406
225407 /* Either:
225408 **
225409 ** 1) an error has occurred, or
225410 ** 2) the iterator points to EOF, or
225411 ** 3) the iterator points to an entry with term (pTerm/nTerm), or
225412 ** 4) the FTS5INDEX_QUERY_SCAN flag was set and the iterator points
225413 ** to an entry with a term greater than or equal to (pTerm/nTerm).
225414 */
225415 assert_nc( p->rc!=SQLITE_OK /* 1 */
225416 || pIter->pLeaf==0 /* 2 */
225417 || fts5BufferCompareBlob(&pIter->term, pTerm, nTerm)==0 /* 3 */
225418 || (bGe && fts5BufferCompareBlob(&pIter->term, pTerm, nTerm)>0) /* 4 */
225419 );
225420}
225421
225422/*
225423** Initialize the object pIter to point to term pTerm/nTerm within the
225424** in-memory hash table. If there is no such term in the hash-table, the
225425** iterator is set to EOF.
225426**
225427** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
225428** an error has already occurred when this function is called, it is a no-op.
225429*/
225430static void fts5SegIterHashInit(
225431 Fts5Index *p, /* FTS5 backend */
225432 const u8 *pTerm, int nTerm, /* Term to seek to */
225433 int flags, /* Mask of FTS5INDEX_XXX flags */
225434 Fts5SegIter *pIter /* Object to populate */
225435){
225436 int nList = 0;
225437 const u8 *z = 0;
225438 int n = 0;
225439 Fts5Data *pLeaf = 0;
225440
225441 assert( p->pHash );
225442 assert( p->rc==SQLITE_OK );
225443
225444 if( pTerm==0 || (flags & FTS5INDEX_QUERY_SCAN) ){
225445 const u8 *pList = 0;
225446
225447 p->rc = sqlite3Fts5HashScanInit(p: p->pHash, pTerm: (const char*)pTerm, nTerm);
225448 sqlite3Fts5HashScanEntry(pHash: p->pHash, pzTerm: (const char**)&z, ppDoclist: &pList, pnDoclist: &nList);
225449 n = (z ? (int)strlen(s: (const char*)z) : 0);
225450 if( pList ){
225451 pLeaf = fts5IdxMalloc(p, nByte: sizeof(Fts5Data));
225452 if( pLeaf ){
225453 pLeaf->p = (u8*)pList;
225454 }
225455 }
225456 }else{
225457 p->rc = sqlite3Fts5HashQuery(pHash: p->pHash, nPre: sizeof(Fts5Data),
225458 pTerm: (const char*)pTerm, nTerm, ppOut: (void**)&pLeaf, pnDoclist: &nList
225459 );
225460 if( pLeaf ){
225461 pLeaf->p = (u8*)&pLeaf[1];
225462 }
225463 z = pTerm;
225464 n = nTerm;
225465 pIter->flags |= FTS5_SEGITER_ONETERM;
225466 }
225467
225468 if( pLeaf ){
225469 sqlite3Fts5BufferSet(pRc: &p->rc, pBuf: &pIter->term, nData: n, pData: z);
225470 pLeaf->nn = pLeaf->szLeaf = nList;
225471 pIter->pLeaf = pLeaf;
225472 pIter->iLeafOffset = fts5GetVarint(pLeaf->p, (u64*)&pIter->iRowid);
225473 pIter->iEndofDoclist = pLeaf->nn;
225474
225475 if( flags & FTS5INDEX_QUERY_DESC ){
225476 pIter->flags |= FTS5_SEGITER_REVERSE;
225477 fts5SegIterReverseInitPage(p, pIter);
225478 }else{
225479 fts5SegIterLoadNPos(p, pIter);
225480 }
225481 }
225482
225483 fts5SegIterSetNext(p, pIter);
225484}
225485
225486/*
225487** Zero the iterator passed as the only argument.
225488*/
225489static void fts5SegIterClear(Fts5SegIter *pIter){
225490 fts5BufferFree(&pIter->term);
225491 fts5DataRelease(pData: pIter->pLeaf);
225492 fts5DataRelease(pData: pIter->pNextLeaf);
225493 fts5DlidxIterFree(pIter: pIter->pDlidx);
225494 sqlite3_free(p: pIter->aRowidOffset);
225495 memset(s: pIter, c: 0, n: sizeof(Fts5SegIter));
225496}
225497
225498#ifdef SQLITE_DEBUG
225499
225500/*
225501** This function is used as part of the big assert() procedure implemented by
225502** fts5AssertMultiIterSetup(). It ensures that the result currently stored
225503** in *pRes is the correct result of comparing the current positions of the
225504** two iterators.
225505*/
225506static void fts5AssertComparisonResult(
225507 Fts5Iter *pIter,
225508 Fts5SegIter *p1,
225509 Fts5SegIter *p2,
225510 Fts5CResult *pRes
225511){
225512 int i1 = p1 - pIter->aSeg;
225513 int i2 = p2 - pIter->aSeg;
225514
225515 if( p1->pLeaf || p2->pLeaf ){
225516 if( p1->pLeaf==0 ){
225517 assert( pRes->iFirst==i2 );
225518 }else if( p2->pLeaf==0 ){
225519 assert( pRes->iFirst==i1 );
225520 }else{
225521 int nMin = MIN(p1->term.n, p2->term.n);
225522 int res = fts5Memcmp(p1->term.p, p2->term.p, nMin);
225523 if( res==0 ) res = p1->term.n - p2->term.n;
225524
225525 if( res==0 ){
225526 assert( pRes->bTermEq==1 );
225527 assert( p1->iRowid!=p2->iRowid );
225528 res = ((p1->iRowid > p2->iRowid)==pIter->bRev) ? -1 : 1;
225529 }else{
225530 assert( pRes->bTermEq==0 );
225531 }
225532
225533 if( res<0 ){
225534 assert( pRes->iFirst==i1 );
225535 }else{
225536 assert( pRes->iFirst==i2 );
225537 }
225538 }
225539 }
225540}
225541
225542/*
225543** This function is a no-op unless SQLITE_DEBUG is defined when this module
225544** is compiled. In that case, this function is essentially an assert()
225545** statement used to verify that the contents of the pIter->aFirst[] array
225546** are correct.
225547*/
225548static void fts5AssertMultiIterSetup(Fts5Index *p, Fts5Iter *pIter){
225549 if( p->rc==SQLITE_OK ){
225550 Fts5SegIter *pFirst = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
225551 int i;
225552
225553 assert( (pFirst->pLeaf==0)==pIter->base.bEof );
225554
225555 /* Check that pIter->iSwitchRowid is set correctly. */
225556 for(i=0; i<pIter->nSeg; i++){
225557 Fts5SegIter *p1 = &pIter->aSeg[i];
225558 assert( p1==pFirst
225559 || p1->pLeaf==0
225560 || fts5BufferCompare(&pFirst->term, &p1->term)
225561 || p1->iRowid==pIter->iSwitchRowid
225562 || (p1->iRowid<pIter->iSwitchRowid)==pIter->bRev
225563 );
225564 }
225565
225566 for(i=0; i<pIter->nSeg; i+=2){
225567 Fts5SegIter *p1 = &pIter->aSeg[i];
225568 Fts5SegIter *p2 = &pIter->aSeg[i+1];
225569 Fts5CResult *pRes = &pIter->aFirst[(pIter->nSeg + i) / 2];
225570 fts5AssertComparisonResult(pIter, p1, p2, pRes);
225571 }
225572
225573 for(i=1; i<(pIter->nSeg / 2); i+=2){
225574 Fts5SegIter *p1 = &pIter->aSeg[ pIter->aFirst[i*2].iFirst ];
225575 Fts5SegIter *p2 = &pIter->aSeg[ pIter->aFirst[i*2+1].iFirst ];
225576 Fts5CResult *pRes = &pIter->aFirst[i];
225577 fts5AssertComparisonResult(pIter, p1, p2, pRes);
225578 }
225579 }
225580}
225581#else
225582# define fts5AssertMultiIterSetup(x,y)
225583#endif
225584
225585/*
225586** Do the comparison necessary to populate pIter->aFirst[iOut].
225587**
225588** If the returned value is non-zero, then it is the index of an entry
225589** in the pIter->aSeg[] array that is (a) not at EOF, and (b) pointing
225590** to a key that is a duplicate of another, higher priority,
225591** segment-iterator in the pSeg->aSeg[] array.
225592*/
225593static int fts5MultiIterDoCompare(Fts5Iter *pIter, int iOut){
225594 int i1; /* Index of left-hand Fts5SegIter */
225595 int i2; /* Index of right-hand Fts5SegIter */
225596 int iRes;
225597 Fts5SegIter *p1; /* Left-hand Fts5SegIter */
225598 Fts5SegIter *p2; /* Right-hand Fts5SegIter */
225599 Fts5CResult *pRes = &pIter->aFirst[iOut];
225600
225601 assert( iOut<pIter->nSeg && iOut>0 );
225602 assert( pIter->bRev==0 || pIter->bRev==1 );
225603
225604 if( iOut>=(pIter->nSeg/2) ){
225605 i1 = (iOut - pIter->nSeg/2) * 2;
225606 i2 = i1 + 1;
225607 }else{
225608 i1 = pIter->aFirst[iOut*2].iFirst;
225609 i2 = pIter->aFirst[iOut*2+1].iFirst;
225610 }
225611 p1 = &pIter->aSeg[i1];
225612 p2 = &pIter->aSeg[i2];
225613
225614 pRes->bTermEq = 0;
225615 if( p1->pLeaf==0 ){ /* If p1 is at EOF */
225616 iRes = i2;
225617 }else if( p2->pLeaf==0 ){ /* If p2 is at EOF */
225618 iRes = i1;
225619 }else{
225620 int res = fts5BufferCompare(pLeft: &p1->term, pRight: &p2->term);
225621 if( res==0 ){
225622 assert_nc( i2>i1 );
225623 assert_nc( i2!=0 );
225624 pRes->bTermEq = 1;
225625 if( p1->iRowid==p2->iRowid ){
225626 p1->bDel = p2->bDel;
225627 return i2;
225628 }
225629 res = ((p1->iRowid > p2->iRowid)==pIter->bRev) ? -1 : +1;
225630 }
225631 assert( res!=0 );
225632 if( res<0 ){
225633 iRes = i1;
225634 }else{
225635 iRes = i2;
225636 }
225637 }
225638
225639 pRes->iFirst = (u16)iRes;
225640 return 0;
225641}
225642
225643/*
225644** Move the seg-iter so that it points to the first rowid on page iLeafPgno.
225645** It is an error if leaf iLeafPgno does not exist or contains no rowids.
225646*/
225647static void fts5SegIterGotoPage(
225648 Fts5Index *p, /* FTS5 backend object */
225649 Fts5SegIter *pIter, /* Iterator to advance */
225650 int iLeafPgno
225651){
225652 assert( iLeafPgno>pIter->iLeafPgno );
225653
225654 if( iLeafPgno>pIter->pSeg->pgnoLast ){
225655 p->rc = FTS5_CORRUPT;
225656 }else{
225657 fts5DataRelease(pData: pIter->pNextLeaf);
225658 pIter->pNextLeaf = 0;
225659 pIter->iLeafPgno = iLeafPgno-1;
225660 fts5SegIterNextPage(p, pIter);
225661 assert( p->rc!=SQLITE_OK || pIter->iLeafPgno==iLeafPgno );
225662
225663 if( p->rc==SQLITE_OK && ALWAYS(pIter->pLeaf!=0) ){
225664 int iOff;
225665 u8 *a = pIter->pLeaf->p;
225666 int n = pIter->pLeaf->szLeaf;
225667
225668 iOff = fts5LeafFirstRowidOff(pIter->pLeaf);
225669 if( iOff<4 || iOff>=n ){
225670 p->rc = FTS5_CORRUPT;
225671 }else{
225672 iOff += fts5GetVarint(&a[iOff], (u64*)&pIter->iRowid);
225673 pIter->iLeafOffset = iOff;
225674 fts5SegIterLoadNPos(p, pIter);
225675 }
225676 }
225677 }
225678}
225679
225680/*
225681** Advance the iterator passed as the second argument until it is at or
225682** past rowid iFrom. Regardless of the value of iFrom, the iterator is
225683** always advanced at least once.
225684*/
225685static void fts5SegIterNextFrom(
225686 Fts5Index *p, /* FTS5 backend object */
225687 Fts5SegIter *pIter, /* Iterator to advance */
225688 i64 iMatch /* Advance iterator at least this far */
225689){
225690 int bRev = (pIter->flags & FTS5_SEGITER_REVERSE);
225691 Fts5DlidxIter *pDlidx = pIter->pDlidx;
225692 int iLeafPgno = pIter->iLeafPgno;
225693 int bMove = 1;
225694
225695 assert( pIter->flags & FTS5_SEGITER_ONETERM );
225696 assert( pIter->pDlidx );
225697 assert( pIter->pLeaf );
225698
225699 if( bRev==0 ){
225700 while( !fts5DlidxIterEof(p, pIter: pDlidx) && iMatch>fts5DlidxIterRowid(pIter: pDlidx) ){
225701 iLeafPgno = fts5DlidxIterPgno(pIter: pDlidx);
225702 fts5DlidxIterNext(p, pIter: pDlidx);
225703 }
225704 assert_nc( iLeafPgno>=pIter->iLeafPgno || p->rc );
225705 if( iLeafPgno>pIter->iLeafPgno ){
225706 fts5SegIterGotoPage(p, pIter, iLeafPgno);
225707 bMove = 0;
225708 }
225709 }else{
225710 assert( pIter->pNextLeaf==0 );
225711 assert( iMatch<pIter->iRowid );
225712 while( !fts5DlidxIterEof(p, pIter: pDlidx) && iMatch<fts5DlidxIterRowid(pIter: pDlidx) ){
225713 fts5DlidxIterPrev(p, pIter: pDlidx);
225714 }
225715 iLeafPgno = fts5DlidxIterPgno(pIter: pDlidx);
225716
225717 assert( fts5DlidxIterEof(p, pDlidx) || iLeafPgno<=pIter->iLeafPgno );
225718
225719 if( iLeafPgno<pIter->iLeafPgno ){
225720 pIter->iLeafPgno = iLeafPgno+1;
225721 fts5SegIterReverseNewPage(p, pIter);
225722 bMove = 0;
225723 }
225724 }
225725
225726 do{
225727 if( bMove && p->rc==SQLITE_OK ) pIter->xNext(p, pIter, 0);
225728 if( pIter->pLeaf==0 ) break;
225729 if( bRev==0 && pIter->iRowid>=iMatch ) break;
225730 if( bRev!=0 && pIter->iRowid<=iMatch ) break;
225731 bMove = 1;
225732 }while( p->rc==SQLITE_OK );
225733}
225734
225735
225736/*
225737** Free the iterator object passed as the second argument.
225738*/
225739static void fts5MultiIterFree(Fts5Iter *pIter){
225740 if( pIter ){
225741 int i;
225742 for(i=0; i<pIter->nSeg; i++){
225743 fts5SegIterClear(pIter: &pIter->aSeg[i]);
225744 }
225745 fts5BufferFree(&pIter->poslist);
225746 sqlite3_free(p: pIter);
225747 }
225748}
225749
225750static void fts5MultiIterAdvanced(
225751 Fts5Index *p, /* FTS5 backend to iterate within */
225752 Fts5Iter *pIter, /* Iterator to update aFirst[] array for */
225753 int iChanged, /* Index of sub-iterator just advanced */
225754 int iMinset /* Minimum entry in aFirst[] to set */
225755){
225756 int i;
225757 for(i=(pIter->nSeg+iChanged)/2; i>=iMinset && p->rc==SQLITE_OK; i=i/2){
225758 int iEq;
225759 if( (iEq = fts5MultiIterDoCompare(pIter, iOut: i)) ){
225760 Fts5SegIter *pSeg = &pIter->aSeg[iEq];
225761 assert( p->rc==SQLITE_OK );
225762 pSeg->xNext(p, pSeg, 0);
225763 i = pIter->nSeg + iEq;
225764 }
225765 }
225766}
225767
225768/*
225769** Sub-iterator iChanged of iterator pIter has just been advanced. It still
225770** points to the same term though - just a different rowid. This function
225771** attempts to update the contents of the pIter->aFirst[] accordingly.
225772** If it does so successfully, 0 is returned. Otherwise 1.
225773**
225774** If non-zero is returned, the caller should call fts5MultiIterAdvanced()
225775** on the iterator instead. That function does the same as this one, except
225776** that it deals with more complicated cases as well.
225777*/
225778static int fts5MultiIterAdvanceRowid(
225779 Fts5Iter *pIter, /* Iterator to update aFirst[] array for */
225780 int iChanged, /* Index of sub-iterator just advanced */
225781 Fts5SegIter **ppFirst
225782){
225783 Fts5SegIter *pNew = &pIter->aSeg[iChanged];
225784
225785 if( pNew->iRowid==pIter->iSwitchRowid
225786 || (pNew->iRowid<pIter->iSwitchRowid)==pIter->bRev
225787 ){
225788 int i;
225789 Fts5SegIter *pOther = &pIter->aSeg[iChanged ^ 0x0001];
225790 pIter->iSwitchRowid = pIter->bRev ? SMALLEST_INT64 : LARGEST_INT64;
225791 for(i=(pIter->nSeg+iChanged)/2; 1; i=i/2){
225792 Fts5CResult *pRes = &pIter->aFirst[i];
225793
225794 assert( pNew->pLeaf );
225795 assert( pRes->bTermEq==0 || pOther->pLeaf );
225796
225797 if( pRes->bTermEq ){
225798 if( pNew->iRowid==pOther->iRowid ){
225799 return 1;
225800 }else if( (pOther->iRowid>pNew->iRowid)==pIter->bRev ){
225801 pIter->iSwitchRowid = pOther->iRowid;
225802 pNew = pOther;
225803 }else if( (pOther->iRowid>pIter->iSwitchRowid)==pIter->bRev ){
225804 pIter->iSwitchRowid = pOther->iRowid;
225805 }
225806 }
225807 pRes->iFirst = (u16)(pNew - pIter->aSeg);
225808 if( i==1 ) break;
225809
225810 pOther = &pIter->aSeg[ pIter->aFirst[i ^ 0x0001].iFirst ];
225811 }
225812 }
225813
225814 *ppFirst = pNew;
225815 return 0;
225816}
225817
225818/*
225819** Set the pIter->bEof variable based on the state of the sub-iterators.
225820*/
225821static void fts5MultiIterSetEof(Fts5Iter *pIter){
225822 Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
225823 pIter->base.bEof = pSeg->pLeaf==0;
225824 pIter->iSwitchRowid = pSeg->iRowid;
225825}
225826
225827/*
225828** Move the iterator to the next entry.
225829**
225830** If an error occurs, an error code is left in Fts5Index.rc. It is not
225831** considered an error if the iterator reaches EOF, or if it is already at
225832** EOF when this function is called.
225833*/
225834static void fts5MultiIterNext(
225835 Fts5Index *p,
225836 Fts5Iter *pIter,
225837 int bFrom, /* True if argument iFrom is valid */
225838 i64 iFrom /* Advance at least as far as this */
225839){
225840 int bUseFrom = bFrom;
225841 assert( pIter->base.bEof==0 );
225842 while( p->rc==SQLITE_OK ){
225843 int iFirst = pIter->aFirst[1].iFirst;
225844 int bNewTerm = 0;
225845 Fts5SegIter *pSeg = &pIter->aSeg[iFirst];
225846 assert( p->rc==SQLITE_OK );
225847 if( bUseFrom && pSeg->pDlidx ){
225848 fts5SegIterNextFrom(p, pIter: pSeg, iMatch: iFrom);
225849 }else{
225850 pSeg->xNext(p, pSeg, &bNewTerm);
225851 }
225852
225853 if( pSeg->pLeaf==0 || bNewTerm
225854 || fts5MultiIterAdvanceRowid(pIter, iChanged: iFirst, ppFirst: &pSeg)
225855 ){
225856 fts5MultiIterAdvanced(p, pIter, iChanged: iFirst, iMinset: 1);
225857 fts5MultiIterSetEof(pIter);
225858 pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
225859 if( pSeg->pLeaf==0 ) return;
225860 }
225861
225862 fts5AssertMultiIterSetup(p, pIter);
225863 assert( pSeg==&pIter->aSeg[pIter->aFirst[1].iFirst] && pSeg->pLeaf );
225864 if( pIter->bSkipEmpty==0 || pSeg->nPos ){
225865 pIter->xSetOutputs(pIter, pSeg);
225866 return;
225867 }
225868 bUseFrom = 0;
225869 }
225870}
225871
225872static void fts5MultiIterNext2(
225873 Fts5Index *p,
225874 Fts5Iter *pIter,
225875 int *pbNewTerm /* OUT: True if *might* be new term */
225876){
225877 assert( pIter->bSkipEmpty );
225878 if( p->rc==SQLITE_OK ){
225879 *pbNewTerm = 0;
225880 do{
225881 int iFirst = pIter->aFirst[1].iFirst;
225882 Fts5SegIter *pSeg = &pIter->aSeg[iFirst];
225883 int bNewTerm = 0;
225884
225885 assert( p->rc==SQLITE_OK );
225886 pSeg->xNext(p, pSeg, &bNewTerm);
225887 if( pSeg->pLeaf==0 || bNewTerm
225888 || fts5MultiIterAdvanceRowid(pIter, iChanged: iFirst, ppFirst: &pSeg)
225889 ){
225890 fts5MultiIterAdvanced(p, pIter, iChanged: iFirst, iMinset: 1);
225891 fts5MultiIterSetEof(pIter);
225892 *pbNewTerm = 1;
225893 }
225894 fts5AssertMultiIterSetup(p, pIter);
225895
225896 }while( fts5MultiIterIsEmpty(p, pIter) );
225897 }
225898}
225899
225900static void fts5IterSetOutputs_Noop(Fts5Iter *pUnused1, Fts5SegIter *pUnused2){
225901 UNUSED_PARAM2(pUnused1, pUnused2);
225902}
225903
225904static Fts5Iter *fts5MultiIterAlloc(
225905 Fts5Index *p, /* FTS5 backend to iterate within */
225906 int nSeg
225907){
225908 Fts5Iter *pNew;
225909 int nSlot; /* Power of two >= nSeg */
225910
225911 for(nSlot=2; nSlot<nSeg; nSlot=nSlot*2);
225912 pNew = fts5IdxMalloc(p,
225913 nByte: sizeof(Fts5Iter) + /* pNew */
225914 sizeof(Fts5SegIter) * (nSlot-1) + /* pNew->aSeg[] */
225915 sizeof(Fts5CResult) * nSlot /* pNew->aFirst[] */
225916 );
225917 if( pNew ){
225918 pNew->nSeg = nSlot;
225919 pNew->aFirst = (Fts5CResult*)&pNew->aSeg[nSlot];
225920 pNew->pIndex = p;
225921 pNew->xSetOutputs = fts5IterSetOutputs_Noop;
225922 }
225923 return pNew;
225924}
225925
225926static void fts5PoslistCallback(
225927 Fts5Index *pUnused,
225928 void *pContext,
225929 const u8 *pChunk, int nChunk
225930){
225931 UNUSED_PARAM(pUnused);
225932 assert_nc( nChunk>=0 );
225933 if( nChunk>0 ){
225934 fts5BufferSafeAppendBlob((Fts5Buffer*)pContext, pChunk, nChunk);
225935 }
225936}
225937
225938typedef struct PoslistCallbackCtx PoslistCallbackCtx;
225939struct PoslistCallbackCtx {
225940 Fts5Buffer *pBuf; /* Append to this buffer */
225941 Fts5Colset *pColset; /* Restrict matches to this column */
225942 int eState; /* See above */
225943};
225944
225945typedef struct PoslistOffsetsCtx PoslistOffsetsCtx;
225946struct PoslistOffsetsCtx {
225947 Fts5Buffer *pBuf; /* Append to this buffer */
225948 Fts5Colset *pColset; /* Restrict matches to this column */
225949 int iRead;
225950 int iWrite;
225951};
225952
225953/*
225954** TODO: Make this more efficient!
225955*/
225956static int fts5IndexColsetTest(Fts5Colset *pColset, int iCol){
225957 int i;
225958 for(i=0; i<pColset->nCol; i++){
225959 if( pColset->aiCol[i]==iCol ) return 1;
225960 }
225961 return 0;
225962}
225963
225964static void fts5PoslistOffsetsCallback(
225965 Fts5Index *pUnused,
225966 void *pContext,
225967 const u8 *pChunk, int nChunk
225968){
225969 PoslistOffsetsCtx *pCtx = (PoslistOffsetsCtx*)pContext;
225970 UNUSED_PARAM(pUnused);
225971 assert_nc( nChunk>=0 );
225972 if( nChunk>0 ){
225973 int i = 0;
225974 while( i<nChunk ){
225975 int iVal;
225976 i += fts5GetVarint32(&pChunk[i], iVal);
225977 iVal += pCtx->iRead - 2;
225978 pCtx->iRead = iVal;
225979 if( fts5IndexColsetTest(pColset: pCtx->pColset, iCol: iVal) ){
225980 fts5BufferSafeAppendVarint(pCtx->pBuf, iVal + 2 - pCtx->iWrite);
225981 pCtx->iWrite = iVal;
225982 }
225983 }
225984 }
225985}
225986
225987static void fts5PoslistFilterCallback(
225988 Fts5Index *pUnused,
225989 void *pContext,
225990 const u8 *pChunk, int nChunk
225991){
225992 PoslistCallbackCtx *pCtx = (PoslistCallbackCtx*)pContext;
225993 UNUSED_PARAM(pUnused);
225994 assert_nc( nChunk>=0 );
225995 if( nChunk>0 ){
225996 /* Search through to find the first varint with value 1. This is the
225997 ** start of the next columns hits. */
225998 int i = 0;
225999 int iStart = 0;
226000
226001 if( pCtx->eState==2 ){
226002 int iCol;
226003 fts5FastGetVarint32(pChunk, i, iCol);
226004 if( fts5IndexColsetTest(pColset: pCtx->pColset, iCol) ){
226005 pCtx->eState = 1;
226006 fts5BufferSafeAppendVarint(pCtx->pBuf, 1);
226007 }else{
226008 pCtx->eState = 0;
226009 }
226010 }
226011
226012 do {
226013 while( i<nChunk && pChunk[i]!=0x01 ){
226014 while( pChunk[i] & 0x80 ) i++;
226015 i++;
226016 }
226017 if( pCtx->eState ){
226018 fts5BufferSafeAppendBlob(pCtx->pBuf, &pChunk[iStart], i-iStart);
226019 }
226020 if( i<nChunk ){
226021 int iCol;
226022 iStart = i;
226023 i++;
226024 if( i>=nChunk ){
226025 pCtx->eState = 2;
226026 }else{
226027 fts5FastGetVarint32(pChunk, i, iCol);
226028 pCtx->eState = fts5IndexColsetTest(pColset: pCtx->pColset, iCol);
226029 if( pCtx->eState ){
226030 fts5BufferSafeAppendBlob(pCtx->pBuf, &pChunk[iStart], i-iStart);
226031 iStart = i;
226032 }
226033 }
226034 }
226035 }while( i<nChunk );
226036 }
226037}
226038
226039static void fts5ChunkIterate(
226040 Fts5Index *p, /* Index object */
226041 Fts5SegIter *pSeg, /* Poslist of this iterator */
226042 void *pCtx, /* Context pointer for xChunk callback */
226043 void (*xChunk)(Fts5Index*, void*, const u8*, int)
226044){
226045 int nRem = pSeg->nPos; /* Number of bytes still to come */
226046 Fts5Data *pData = 0;
226047 u8 *pChunk = &pSeg->pLeaf->p[pSeg->iLeafOffset];
226048 int nChunk = MIN(nRem, pSeg->pLeaf->szLeaf - pSeg->iLeafOffset);
226049 int pgno = pSeg->iLeafPgno;
226050 int pgnoSave = 0;
226051
226052 /* This function does not work with detail=none databases. */
226053 assert( p->pConfig->eDetail!=FTS5_DETAIL_NONE );
226054
226055 if( (pSeg->flags & FTS5_SEGITER_REVERSE)==0 ){
226056 pgnoSave = pgno+1;
226057 }
226058
226059 while( 1 ){
226060 xChunk(p, pCtx, pChunk, nChunk);
226061 nRem -= nChunk;
226062 fts5DataRelease(pData);
226063 if( nRem<=0 ){
226064 break;
226065 }else if( pSeg->pSeg==0 ){
226066 p->rc = FTS5_CORRUPT;
226067 return;
226068 }else{
226069 pgno++;
226070 pData = fts5LeafRead(p, FTS5_SEGMENT_ROWID(pSeg->pSeg->iSegid, pgno));
226071 if( pData==0 ) break;
226072 pChunk = &pData->p[4];
226073 nChunk = MIN(nRem, pData->szLeaf - 4);
226074 if( pgno==pgnoSave ){
226075 assert( pSeg->pNextLeaf==0 );
226076 pSeg->pNextLeaf = pData;
226077 pData = 0;
226078 }
226079 }
226080 }
226081}
226082
226083/*
226084** Iterator pIter currently points to a valid entry (not EOF). This
226085** function appends the position list data for the current entry to
226086** buffer pBuf. It does not make a copy of the position-list size
226087** field.
226088*/
226089static void fts5SegiterPoslist(
226090 Fts5Index *p,
226091 Fts5SegIter *pSeg,
226092 Fts5Colset *pColset,
226093 Fts5Buffer *pBuf
226094){
226095 assert( pBuf!=0 );
226096 assert( pSeg!=0 );
226097 if( 0==fts5BufferGrow(&p->rc, pBuf, pSeg->nPos+FTS5_DATA_ZERO_PADDING) ){
226098 assert( pBuf->p!=0 );
226099 assert( pBuf->nSpace >= pBuf->n+pSeg->nPos+FTS5_DATA_ZERO_PADDING );
226100 memset(s: &pBuf->p[pBuf->n+pSeg->nPos], c: 0, FTS5_DATA_ZERO_PADDING);
226101 if( pColset==0 ){
226102 fts5ChunkIterate(p, pSeg, pCtx: (void*)pBuf, xChunk: fts5PoslistCallback);
226103 }else{
226104 if( p->pConfig->eDetail==FTS5_DETAIL_FULL ){
226105 PoslistCallbackCtx sCtx;
226106 sCtx.pBuf = pBuf;
226107 sCtx.pColset = pColset;
226108 sCtx.eState = fts5IndexColsetTest(pColset, iCol: 0);
226109 assert( sCtx.eState==0 || sCtx.eState==1 );
226110 fts5ChunkIterate(p, pSeg, pCtx: (void*)&sCtx, xChunk: fts5PoslistFilterCallback);
226111 }else{
226112 PoslistOffsetsCtx sCtx;
226113 memset(s: &sCtx, c: 0, n: sizeof(sCtx));
226114 sCtx.pBuf = pBuf;
226115 sCtx.pColset = pColset;
226116 fts5ChunkIterate(p, pSeg, pCtx: (void*)&sCtx, xChunk: fts5PoslistOffsetsCallback);
226117 }
226118 }
226119 }
226120}
226121
226122/*
226123** Parameter pPos points to a buffer containing a position list, size nPos.
226124** This function filters it according to pColset (which must be non-NULL)
226125** and sets pIter->base.pData/nData to point to the new position list.
226126** If memory is required for the new position list, use buffer pIter->poslist.
226127** Or, if the new position list is a contiguous subset of the input, set
226128** pIter->base.pData/nData to point directly to it.
226129**
226130** This function is a no-op if *pRc is other than SQLITE_OK when it is
226131** called. If an OOM error is encountered, *pRc is set to SQLITE_NOMEM
226132** before returning.
226133*/
226134static void fts5IndexExtractColset(
226135 int *pRc,
226136 Fts5Colset *pColset, /* Colset to filter on */
226137 const u8 *pPos, int nPos, /* Position list */
226138 Fts5Iter *pIter
226139){
226140 if( *pRc==SQLITE_OK ){
226141 const u8 *p = pPos;
226142 const u8 *aCopy = p;
226143 const u8 *pEnd = &p[nPos]; /* One byte past end of position list */
226144 int i = 0;
226145 int iCurrent = 0;
226146
226147 if( pColset->nCol>1 && sqlite3Fts5BufferSize(pRc, pBuf: &pIter->poslist, nByte: nPos) ){
226148 return;
226149 }
226150
226151 while( 1 ){
226152 while( pColset->aiCol[i]<iCurrent ){
226153 i++;
226154 if( i==pColset->nCol ){
226155 pIter->base.pData = pIter->poslist.p;
226156 pIter->base.nData = pIter->poslist.n;
226157 return;
226158 }
226159 }
226160
226161 /* Advance pointer p until it points to pEnd or an 0x01 byte that is
226162 ** not part of a varint */
226163 while( p<pEnd && *p!=0x01 ){
226164 while( *p++ & 0x80 );
226165 }
226166
226167 if( pColset->aiCol[i]==iCurrent ){
226168 if( pColset->nCol==1 ){
226169 pIter->base.pData = aCopy;
226170 pIter->base.nData = p-aCopy;
226171 return;
226172 }
226173 fts5BufferSafeAppendBlob(&pIter->poslist, aCopy, p-aCopy);
226174 }
226175 if( p>=pEnd ){
226176 pIter->base.pData = pIter->poslist.p;
226177 pIter->base.nData = pIter->poslist.n;
226178 return;
226179 }
226180 aCopy = p++;
226181 iCurrent = *p++;
226182 if( iCurrent & 0x80 ){
226183 p--;
226184 p += fts5GetVarint32(p, iCurrent);
226185 }
226186 }
226187 }
226188
226189}
226190
226191/*
226192** xSetOutputs callback used by detail=none tables.
226193*/
226194static void fts5IterSetOutputs_None(Fts5Iter *pIter, Fts5SegIter *pSeg){
226195 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_NONE );
226196 pIter->base.iRowid = pSeg->iRowid;
226197 pIter->base.nData = pSeg->nPos;
226198}
226199
226200/*
226201** xSetOutputs callback used by detail=full and detail=col tables when no
226202** column filters are specified.
226203*/
226204static void fts5IterSetOutputs_Nocolset(Fts5Iter *pIter, Fts5SegIter *pSeg){
226205 pIter->base.iRowid = pSeg->iRowid;
226206 pIter->base.nData = pSeg->nPos;
226207
226208 assert( pIter->pIndex->pConfig->eDetail!=FTS5_DETAIL_NONE );
226209 assert( pIter->pColset==0 );
226210
226211 if( pSeg->iLeafOffset+pSeg->nPos<=pSeg->pLeaf->szLeaf ){
226212 /* All data is stored on the current page. Populate the output
226213 ** variables to point into the body of the page object. */
226214 pIter->base.pData = &pSeg->pLeaf->p[pSeg->iLeafOffset];
226215 }else{
226216 /* The data is distributed over two or more pages. Copy it into the
226217 ** Fts5Iter.poslist buffer and then set the output pointer to point
226218 ** to this buffer. */
226219 fts5BufferZero(&pIter->poslist);
226220 fts5SegiterPoslist(p: pIter->pIndex, pSeg, pColset: 0, pBuf: &pIter->poslist);
226221 pIter->base.pData = pIter->poslist.p;
226222 }
226223}
226224
226225/*
226226** xSetOutputs callback used when the Fts5Colset object has nCol==0 (match
226227** against no columns at all).
226228*/
226229static void fts5IterSetOutputs_ZeroColset(Fts5Iter *pIter, Fts5SegIter *pSeg){
226230 UNUSED_PARAM(pSeg);
226231 pIter->base.nData = 0;
226232}
226233
226234/*
226235** xSetOutputs callback used by detail=col when there is a column filter
226236** and there are 100 or more columns. Also called as a fallback from
226237** fts5IterSetOutputs_Col100 if the column-list spans more than one page.
226238*/
226239static void fts5IterSetOutputs_Col(Fts5Iter *pIter, Fts5SegIter *pSeg){
226240 fts5BufferZero(&pIter->poslist);
226241 fts5SegiterPoslist(p: pIter->pIndex, pSeg, pColset: pIter->pColset, pBuf: &pIter->poslist);
226242 pIter->base.iRowid = pSeg->iRowid;
226243 pIter->base.pData = pIter->poslist.p;
226244 pIter->base.nData = pIter->poslist.n;
226245}
226246
226247/*
226248** xSetOutputs callback used when:
226249**
226250** * detail=col,
226251** * there is a column filter, and
226252** * the table contains 100 or fewer columns.
226253**
226254** The last point is to ensure all column numbers are stored as
226255** single-byte varints.
226256*/
226257static void fts5IterSetOutputs_Col100(Fts5Iter *pIter, Fts5SegIter *pSeg){
226258
226259 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_COLUMNS );
226260 assert( pIter->pColset );
226261
226262 if( pSeg->iLeafOffset+pSeg->nPos>pSeg->pLeaf->szLeaf ){
226263 fts5IterSetOutputs_Col(pIter, pSeg);
226264 }else{
226265 u8 *a = (u8*)&pSeg->pLeaf->p[pSeg->iLeafOffset];
226266 u8 *pEnd = (u8*)&a[pSeg->nPos];
226267 int iPrev = 0;
226268 int *aiCol = pIter->pColset->aiCol;
226269 int *aiColEnd = &aiCol[pIter->pColset->nCol];
226270
226271 u8 *aOut = pIter->poslist.p;
226272 int iPrevOut = 0;
226273
226274 pIter->base.iRowid = pSeg->iRowid;
226275
226276 while( a<pEnd ){
226277 iPrev += (int)a++[0] - 2;
226278 while( *aiCol<iPrev ){
226279 aiCol++;
226280 if( aiCol==aiColEnd ) goto setoutputs_col_out;
226281 }
226282 if( *aiCol==iPrev ){
226283 *aOut++ = (u8)((iPrev - iPrevOut) + 2);
226284 iPrevOut = iPrev;
226285 }
226286 }
226287
226288setoutputs_col_out:
226289 pIter->base.pData = pIter->poslist.p;
226290 pIter->base.nData = aOut - pIter->poslist.p;
226291 }
226292}
226293
226294/*
226295** xSetOutputs callback used by detail=full when there is a column filter.
226296*/
226297static void fts5IterSetOutputs_Full(Fts5Iter *pIter, Fts5SegIter *pSeg){
226298 Fts5Colset *pColset = pIter->pColset;
226299 pIter->base.iRowid = pSeg->iRowid;
226300
226301 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_FULL );
226302 assert( pColset );
226303
226304 if( pSeg->iLeafOffset+pSeg->nPos<=pSeg->pLeaf->szLeaf ){
226305 /* All data is stored on the current page. Populate the output
226306 ** variables to point into the body of the page object. */
226307 const u8 *a = &pSeg->pLeaf->p[pSeg->iLeafOffset];
226308 int *pRc = &pIter->pIndex->rc;
226309 fts5BufferZero(&pIter->poslist);
226310 fts5IndexExtractColset(pRc, pColset, pPos: a, nPos: pSeg->nPos, pIter);
226311 }else{
226312 /* The data is distributed over two or more pages. Copy it into the
226313 ** Fts5Iter.poslist buffer and then set the output pointer to point
226314 ** to this buffer. */
226315 fts5BufferZero(&pIter->poslist);
226316 fts5SegiterPoslist(p: pIter->pIndex, pSeg, pColset, pBuf: &pIter->poslist);
226317 pIter->base.pData = pIter->poslist.p;
226318 pIter->base.nData = pIter->poslist.n;
226319 }
226320}
226321
226322static void fts5IterSetOutputCb(int *pRc, Fts5Iter *pIter){
226323 assert( pIter!=0 || (*pRc)!=SQLITE_OK );
226324 if( *pRc==SQLITE_OK ){
226325 Fts5Config *pConfig = pIter->pIndex->pConfig;
226326 if( pConfig->eDetail==FTS5_DETAIL_NONE ){
226327 pIter->xSetOutputs = fts5IterSetOutputs_None;
226328 }
226329
226330 else if( pIter->pColset==0 ){
226331 pIter->xSetOutputs = fts5IterSetOutputs_Nocolset;
226332 }
226333
226334 else if( pIter->pColset->nCol==0 ){
226335 pIter->xSetOutputs = fts5IterSetOutputs_ZeroColset;
226336 }
226337
226338 else if( pConfig->eDetail==FTS5_DETAIL_FULL ){
226339 pIter->xSetOutputs = fts5IterSetOutputs_Full;
226340 }
226341
226342 else{
226343 assert( pConfig->eDetail==FTS5_DETAIL_COLUMNS );
226344 if( pConfig->nCol<=100 ){
226345 pIter->xSetOutputs = fts5IterSetOutputs_Col100;
226346 sqlite3Fts5BufferSize(pRc, pBuf: &pIter->poslist, nByte: pConfig->nCol);
226347 }else{
226348 pIter->xSetOutputs = fts5IterSetOutputs_Col;
226349 }
226350 }
226351 }
226352}
226353
226354
226355/*
226356** Allocate a new Fts5Iter object.
226357**
226358** The new object will be used to iterate through data in structure pStruct.
226359** If iLevel is -ve, then all data in all segments is merged. Or, if iLevel
226360** is zero or greater, data from the first nSegment segments on level iLevel
226361** is merged.
226362**
226363** The iterator initially points to the first term/rowid entry in the
226364** iterated data.
226365*/
226366static void fts5MultiIterNew(
226367 Fts5Index *p, /* FTS5 backend to iterate within */
226368 Fts5Structure *pStruct, /* Structure of specific index */
226369 int flags, /* FTS5INDEX_QUERY_XXX flags */
226370 Fts5Colset *pColset, /* Colset to filter on (or NULL) */
226371 const u8 *pTerm, int nTerm, /* Term to seek to (or NULL/0) */
226372 int iLevel, /* Level to iterate (-1 for all) */
226373 int nSegment, /* Number of segments to merge (iLevel>=0) */
226374 Fts5Iter **ppOut /* New object */
226375){
226376 int nSeg = 0; /* Number of segment-iters in use */
226377 int iIter = 0; /* */
226378 int iSeg; /* Used to iterate through segments */
226379 Fts5StructureLevel *pLvl;
226380 Fts5Iter *pNew;
226381
226382 assert( (pTerm==0 && nTerm==0) || iLevel<0 );
226383
226384 /* Allocate space for the new multi-seg-iterator. */
226385 if( p->rc==SQLITE_OK ){
226386 if( iLevel<0 ){
226387 assert( pStruct->nSegment==fts5StructureCountSegments(pStruct) );
226388 nSeg = pStruct->nSegment;
226389 nSeg += (p->pHash ? 1 : 0);
226390 }else{
226391 nSeg = MIN(pStruct->aLevel[iLevel].nSeg, nSegment);
226392 }
226393 }
226394 *ppOut = pNew = fts5MultiIterAlloc(p, nSeg);
226395 if( pNew==0 ){
226396 assert( p->rc!=SQLITE_OK );
226397 goto fts5MultiIterNew_post_check;
226398 }
226399 pNew->bRev = (0!=(flags & FTS5INDEX_QUERY_DESC));
226400 pNew->bSkipEmpty = (0!=(flags & FTS5INDEX_QUERY_SKIPEMPTY));
226401 pNew->pColset = pColset;
226402 if( (flags & FTS5INDEX_QUERY_NOOUTPUT)==0 ){
226403 fts5IterSetOutputCb(pRc: &p->rc, pIter: pNew);
226404 }
226405
226406 /* Initialize each of the component segment iterators. */
226407 if( p->rc==SQLITE_OK ){
226408 if( iLevel<0 ){
226409 Fts5StructureLevel *pEnd = &pStruct->aLevel[pStruct->nLevel];
226410 if( p->pHash ){
226411 /* Add a segment iterator for the current contents of the hash table. */
226412 Fts5SegIter *pIter = &pNew->aSeg[iIter++];
226413 fts5SegIterHashInit(p, pTerm, nTerm, flags, pIter);
226414 }
226415 for(pLvl=&pStruct->aLevel[0]; pLvl<pEnd; pLvl++){
226416 for(iSeg=pLvl->nSeg-1; iSeg>=0; iSeg--){
226417 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
226418 Fts5SegIter *pIter = &pNew->aSeg[iIter++];
226419 if( pTerm==0 ){
226420 fts5SegIterInit(p, pSeg, pIter);
226421 }else{
226422 fts5SegIterSeekInit(p, pTerm, nTerm, flags, pSeg, pIter);
226423 }
226424 }
226425 }
226426 }else{
226427 pLvl = &pStruct->aLevel[iLevel];
226428 for(iSeg=nSeg-1; iSeg>=0; iSeg--){
226429 fts5SegIterInit(p, pSeg: &pLvl->aSeg[iSeg], pIter: &pNew->aSeg[iIter++]);
226430 }
226431 }
226432 assert( iIter==nSeg );
226433 }
226434
226435 /* If the above was successful, each component iterators now points
226436 ** to the first entry in its segment. In this case initialize the
226437 ** aFirst[] array. Or, if an error has occurred, free the iterator
226438 ** object and set the output variable to NULL. */
226439 if( p->rc==SQLITE_OK ){
226440 for(iIter=pNew->nSeg-1; iIter>0; iIter--){
226441 int iEq;
226442 if( (iEq = fts5MultiIterDoCompare(pIter: pNew, iOut: iIter)) ){
226443 Fts5SegIter *pSeg = &pNew->aSeg[iEq];
226444 if( p->rc==SQLITE_OK ) pSeg->xNext(p, pSeg, 0);
226445 fts5MultiIterAdvanced(p, pIter: pNew, iChanged: iEq, iMinset: iIter);
226446 }
226447 }
226448 fts5MultiIterSetEof(pIter: pNew);
226449 fts5AssertMultiIterSetup(p, pNew);
226450
226451 if( pNew->bSkipEmpty && fts5MultiIterIsEmpty(p, pIter: pNew) ){
226452 fts5MultiIterNext(p, pIter: pNew, bFrom: 0, iFrom: 0);
226453 }else if( pNew->base.bEof==0 ){
226454 Fts5SegIter *pSeg = &pNew->aSeg[pNew->aFirst[1].iFirst];
226455 pNew->xSetOutputs(pNew, pSeg);
226456 }
226457
226458 }else{
226459 fts5MultiIterFree(pIter: pNew);
226460 *ppOut = 0;
226461 }
226462
226463fts5MultiIterNew_post_check:
226464 assert( (*ppOut)!=0 || p->rc!=SQLITE_OK );
226465 return;
226466}
226467
226468/*
226469** Create an Fts5Iter that iterates through the doclist provided
226470** as the second argument.
226471*/
226472static void fts5MultiIterNew2(
226473 Fts5Index *p, /* FTS5 backend to iterate within */
226474 Fts5Data *pData, /* Doclist to iterate through */
226475 int bDesc, /* True for descending rowid order */
226476 Fts5Iter **ppOut /* New object */
226477){
226478 Fts5Iter *pNew;
226479 pNew = fts5MultiIterAlloc(p, nSeg: 2);
226480 if( pNew ){
226481 Fts5SegIter *pIter = &pNew->aSeg[1];
226482
226483 pIter->flags = FTS5_SEGITER_ONETERM;
226484 if( pData->szLeaf>0 ){
226485 pIter->pLeaf = pData;
226486 pIter->iLeafOffset = fts5GetVarint(pData->p, (u64*)&pIter->iRowid);
226487 pIter->iEndofDoclist = pData->nn;
226488 pNew->aFirst[1].iFirst = 1;
226489 if( bDesc ){
226490 pNew->bRev = 1;
226491 pIter->flags |= FTS5_SEGITER_REVERSE;
226492 fts5SegIterReverseInitPage(p, pIter);
226493 }else{
226494 fts5SegIterLoadNPos(p, pIter);
226495 }
226496 pData = 0;
226497 }else{
226498 pNew->base.bEof = 1;
226499 }
226500 fts5SegIterSetNext(p, pIter);
226501
226502 *ppOut = pNew;
226503 }
226504
226505 fts5DataRelease(pData);
226506}
226507
226508/*
226509** Return true if the iterator is at EOF or if an error has occurred.
226510** False otherwise.
226511*/
226512static int fts5MultiIterEof(Fts5Index *p, Fts5Iter *pIter){
226513 assert( pIter!=0 || p->rc!=SQLITE_OK );
226514 assert( p->rc!=SQLITE_OK
226515 || (pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf==0)==pIter->base.bEof
226516 );
226517 return (p->rc || pIter->base.bEof);
226518}
226519
226520/*
226521** Return the rowid of the entry that the iterator currently points
226522** to. If the iterator points to EOF when this function is called the
226523** results are undefined.
226524*/
226525static i64 fts5MultiIterRowid(Fts5Iter *pIter){
226526 assert( pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf );
226527 return pIter->aSeg[ pIter->aFirst[1].iFirst ].iRowid;
226528}
226529
226530/*
226531** Move the iterator to the next entry at or following iMatch.
226532*/
226533static void fts5MultiIterNextFrom(
226534 Fts5Index *p,
226535 Fts5Iter *pIter,
226536 i64 iMatch
226537){
226538 while( 1 ){
226539 i64 iRowid;
226540 fts5MultiIterNext(p, pIter, bFrom: 1, iFrom: iMatch);
226541 if( fts5MultiIterEof(p, pIter) ) break;
226542 iRowid = fts5MultiIterRowid(pIter);
226543 if( pIter->bRev==0 && iRowid>=iMatch ) break;
226544 if( pIter->bRev!=0 && iRowid<=iMatch ) break;
226545 }
226546}
226547
226548/*
226549** Return a pointer to a buffer containing the term associated with the
226550** entry that the iterator currently points to.
226551*/
226552static const u8 *fts5MultiIterTerm(Fts5Iter *pIter, int *pn){
226553 Fts5SegIter *p = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
226554 *pn = p->term.n;
226555 return p->term.p;
226556}
226557
226558/*
226559** Allocate a new segment-id for the structure pStruct. The new segment
226560** id must be between 1 and 65335 inclusive, and must not be used by
226561** any currently existing segment. If a free segment id cannot be found,
226562** SQLITE_FULL is returned.
226563**
226564** If an error has already occurred, this function is a no-op. 0 is
226565** returned in this case.
226566*/
226567static int fts5AllocateSegid(Fts5Index *p, Fts5Structure *pStruct){
226568 int iSegid = 0;
226569
226570 if( p->rc==SQLITE_OK ){
226571 if( pStruct->nSegment>=FTS5_MAX_SEGMENT ){
226572 p->rc = SQLITE_FULL;
226573 }else{
226574 /* FTS5_MAX_SEGMENT is currently defined as 2000. So the following
226575 ** array is 63 elements, or 252 bytes, in size. */
226576 u32 aUsed[(FTS5_MAX_SEGMENT+31) / 32];
226577 int iLvl, iSeg;
226578 int i;
226579 u32 mask;
226580 memset(s: aUsed, c: 0, n: sizeof(aUsed));
226581 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
226582 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
226583 int iId = pStruct->aLevel[iLvl].aSeg[iSeg].iSegid;
226584 if( iId<=FTS5_MAX_SEGMENT && iId>0 ){
226585 aUsed[(iId-1) / 32] |= (u32)1 << ((iId-1) % 32);
226586 }
226587 }
226588 }
226589
226590 for(i=0; aUsed[i]==0xFFFFFFFF; i++);
226591 mask = aUsed[i];
226592 for(iSegid=0; mask & ((u32)1 << iSegid); iSegid++);
226593 iSegid += 1 + i*32;
226594
226595#ifdef SQLITE_DEBUG
226596 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
226597 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
226598 assert_nc( iSegid!=pStruct->aLevel[iLvl].aSeg[iSeg].iSegid );
226599 }
226600 }
226601 assert_nc( iSegid>0 && iSegid<=FTS5_MAX_SEGMENT );
226602
226603 {
226604 sqlite3_stmt *pIdxSelect = fts5IdxSelectStmt(p);
226605 if( p->rc==SQLITE_OK ){
226606 u8 aBlob[2] = {0xff, 0xff};
226607 sqlite3_bind_int(pIdxSelect, 1, iSegid);
226608 sqlite3_bind_blob(pIdxSelect, 2, aBlob, 2, SQLITE_STATIC);
226609 assert_nc( sqlite3_step(pIdxSelect)!=SQLITE_ROW );
226610 p->rc = sqlite3_reset(pIdxSelect);
226611 sqlite3_bind_null(pIdxSelect, 2);
226612 }
226613 }
226614#endif
226615 }
226616 }
226617
226618 return iSegid;
226619}
226620
226621/*
226622** Discard all data currently cached in the hash-tables.
226623*/
226624static void fts5IndexDiscardData(Fts5Index *p){
226625 assert( p->pHash || p->nPendingData==0 );
226626 if( p->pHash ){
226627 sqlite3Fts5HashClear(pHash: p->pHash);
226628 p->nPendingData = 0;
226629 }
226630}
226631
226632/*
226633** Return the size of the prefix, in bytes, that buffer
226634** (pNew/<length-unknown>) shares with buffer (pOld/nOld).
226635**
226636** Buffer (pNew/<length-unknown>) is guaranteed to be greater
226637** than buffer (pOld/nOld).
226638*/
226639static int fts5PrefixCompress(int nOld, const u8 *pOld, const u8 *pNew){
226640 int i;
226641 for(i=0; i<nOld; i++){
226642 if( pOld[i]!=pNew[i] ) break;
226643 }
226644 return i;
226645}
226646
226647static void fts5WriteDlidxClear(
226648 Fts5Index *p,
226649 Fts5SegWriter *pWriter,
226650 int bFlush /* If true, write dlidx to disk */
226651){
226652 int i;
226653 assert( bFlush==0 || (pWriter->nDlidx>0 && pWriter->aDlidx[0].buf.n>0) );
226654 for(i=0; i<pWriter->nDlidx; i++){
226655 Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[i];
226656 if( pDlidx->buf.n==0 ) break;
226657 if( bFlush ){
226658 assert( pDlidx->pgno!=0 );
226659 fts5DataWrite(p,
226660 FTS5_DLIDX_ROWID(pWriter->iSegid, i, pDlidx->pgno),
226661 pData: pDlidx->buf.p, nData: pDlidx->buf.n
226662 );
226663 }
226664 sqlite3Fts5BufferZero(pBuf: &pDlidx->buf);
226665 pDlidx->bPrevValid = 0;
226666 }
226667}
226668
226669/*
226670** Grow the pWriter->aDlidx[] array to at least nLvl elements in size.
226671** Any new array elements are zeroed before returning.
226672*/
226673static int fts5WriteDlidxGrow(
226674 Fts5Index *p,
226675 Fts5SegWriter *pWriter,
226676 int nLvl
226677){
226678 if( p->rc==SQLITE_OK && nLvl>=pWriter->nDlidx ){
226679 Fts5DlidxWriter *aDlidx = (Fts5DlidxWriter*)sqlite3_realloc64(
226680 pOld: pWriter->aDlidx, n: sizeof(Fts5DlidxWriter) * nLvl
226681 );
226682 if( aDlidx==0 ){
226683 p->rc = SQLITE_NOMEM;
226684 }else{
226685 size_t nByte = sizeof(Fts5DlidxWriter) * (nLvl - pWriter->nDlidx);
226686 memset(s: &aDlidx[pWriter->nDlidx], c: 0, n: nByte);
226687 pWriter->aDlidx = aDlidx;
226688 pWriter->nDlidx = nLvl;
226689 }
226690 }
226691 return p->rc;
226692}
226693
226694/*
226695** If the current doclist-index accumulating in pWriter->aDlidx[] is large
226696** enough, flush it to disk and return 1. Otherwise discard it and return
226697** zero.
226698*/
226699static int fts5WriteFlushDlidx(Fts5Index *p, Fts5SegWriter *pWriter){
226700 int bFlag = 0;
226701
226702 /* If there were FTS5_MIN_DLIDX_SIZE or more empty leaf pages written
226703 ** to the database, also write the doclist-index to disk. */
226704 if( pWriter->aDlidx[0].buf.n>0 && pWriter->nEmpty>=FTS5_MIN_DLIDX_SIZE ){
226705 bFlag = 1;
226706 }
226707 fts5WriteDlidxClear(p, pWriter, bFlush: bFlag);
226708 pWriter->nEmpty = 0;
226709 return bFlag;
226710}
226711
226712/*
226713** This function is called whenever processing of the doclist for the
226714** last term on leaf page (pWriter->iBtPage) is completed.
226715**
226716** The doclist-index for that term is currently stored in-memory within the
226717** Fts5SegWriter.aDlidx[] array. If it is large enough, this function
226718** writes it out to disk. Or, if it is too small to bother with, discards
226719** it.
226720**
226721** Fts5SegWriter.btterm currently contains the first term on page iBtPage.
226722*/
226723static void fts5WriteFlushBtree(Fts5Index *p, Fts5SegWriter *pWriter){
226724 int bFlag;
226725
226726 assert( pWriter->iBtPage || pWriter->nEmpty==0 );
226727 if( pWriter->iBtPage==0 ) return;
226728 bFlag = fts5WriteFlushDlidx(p, pWriter);
226729
226730 if( p->rc==SQLITE_OK ){
226731 const char *z = (pWriter->btterm.n>0?(const char*)pWriter->btterm.p:"");
226732 /* The following was already done in fts5WriteInit(): */
226733 /* sqlite3_bind_int(p->pIdxWriter, 1, pWriter->iSegid); */
226734 sqlite3_bind_blob(pStmt: p->pIdxWriter, i: 2, zData: z, nData: pWriter->btterm.n, SQLITE_STATIC);
226735 sqlite3_bind_int64(pStmt: p->pIdxWriter, i: 3, iValue: bFlag + ((i64)pWriter->iBtPage<<1));
226736 sqlite3_step(pStmt: p->pIdxWriter);
226737 p->rc = sqlite3_reset(pStmt: p->pIdxWriter);
226738 sqlite3_bind_null(pStmt: p->pIdxWriter, i: 2);
226739 }
226740 pWriter->iBtPage = 0;
226741}
226742
226743/*
226744** This is called once for each leaf page except the first that contains
226745** at least one term. Argument (nTerm/pTerm) is the split-key - a term that
226746** is larger than all terms written to earlier leaves, and equal to or
226747** smaller than the first term on the new leaf.
226748**
226749** If an error occurs, an error code is left in Fts5Index.rc. If an error
226750** has already occurred when this function is called, it is a no-op.
226751*/
226752static void fts5WriteBtreeTerm(
226753 Fts5Index *p, /* FTS5 backend object */
226754 Fts5SegWriter *pWriter, /* Writer object */
226755 int nTerm, const u8 *pTerm /* First term on new page */
226756){
226757 fts5WriteFlushBtree(p, pWriter);
226758 if( p->rc==SQLITE_OK ){
226759 fts5BufferSet(&p->rc, &pWriter->btterm, nTerm, pTerm);
226760 pWriter->iBtPage = pWriter->writer.pgno;
226761 }
226762}
226763
226764/*
226765** This function is called when flushing a leaf page that contains no
226766** terms at all to disk.
226767*/
226768static void fts5WriteBtreeNoTerm(
226769 Fts5Index *p, /* FTS5 backend object */
226770 Fts5SegWriter *pWriter /* Writer object */
226771){
226772 /* If there were no rowids on the leaf page either and the doclist-index
226773 ** has already been started, append an 0x00 byte to it. */
226774 if( pWriter->bFirstRowidInPage && pWriter->aDlidx[0].buf.n>0 ){
226775 Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[0];
226776 assert( pDlidx->bPrevValid );
226777 sqlite3Fts5BufferAppendVarint(pRc: &p->rc, pBuf: &pDlidx->buf, iVal: 0);
226778 }
226779
226780 /* Increment the "number of sequential leaves without a term" counter. */
226781 pWriter->nEmpty++;
226782}
226783
226784static i64 fts5DlidxExtractFirstRowid(Fts5Buffer *pBuf){
226785 i64 iRowid;
226786 int iOff;
226787
226788 iOff = 1 + fts5GetVarint(&pBuf->p[1], (u64*)&iRowid);
226789 fts5GetVarint(&pBuf->p[iOff], (u64*)&iRowid);
226790 return iRowid;
226791}
226792
226793/*
226794** Rowid iRowid has just been appended to the current leaf page. It is the
226795** first on the page. This function appends an appropriate entry to the current
226796** doclist-index.
226797*/
226798static void fts5WriteDlidxAppend(
226799 Fts5Index *p,
226800 Fts5SegWriter *pWriter,
226801 i64 iRowid
226802){
226803 int i;
226804 int bDone = 0;
226805
226806 for(i=0; p->rc==SQLITE_OK && bDone==0; i++){
226807 i64 iVal;
226808 Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[i];
226809
226810 if( pDlidx->buf.n>=p->pConfig->pgsz ){
226811 /* The current doclist-index page is full. Write it to disk and push
226812 ** a copy of iRowid (which will become the first rowid on the next
226813 ** doclist-index leaf page) up into the next level of the b-tree
226814 ** hierarchy. If the node being flushed is currently the root node,
226815 ** also push its first rowid upwards. */
226816 pDlidx->buf.p[0] = 0x01; /* Not the root node */
226817 fts5DataWrite(p,
226818 FTS5_DLIDX_ROWID(pWriter->iSegid, i, pDlidx->pgno),
226819 pData: pDlidx->buf.p, nData: pDlidx->buf.n
226820 );
226821 fts5WriteDlidxGrow(p, pWriter, nLvl: i+2);
226822 pDlidx = &pWriter->aDlidx[i];
226823 if( p->rc==SQLITE_OK && pDlidx[1].buf.n==0 ){
226824 i64 iFirst = fts5DlidxExtractFirstRowid(pBuf: &pDlidx->buf);
226825
226826 /* This was the root node. Push its first rowid up to the new root. */
226827 pDlidx[1].pgno = pDlidx->pgno;
226828 sqlite3Fts5BufferAppendVarint(pRc: &p->rc, pBuf: &pDlidx[1].buf, iVal: 0);
226829 sqlite3Fts5BufferAppendVarint(pRc: &p->rc, pBuf: &pDlidx[1].buf, iVal: pDlidx->pgno);
226830 sqlite3Fts5BufferAppendVarint(pRc: &p->rc, pBuf: &pDlidx[1].buf, iVal: iFirst);
226831 pDlidx[1].bPrevValid = 1;
226832 pDlidx[1].iPrev = iFirst;
226833 }
226834
226835 sqlite3Fts5BufferZero(pBuf: &pDlidx->buf);
226836 pDlidx->bPrevValid = 0;
226837 pDlidx->pgno++;
226838 }else{
226839 bDone = 1;
226840 }
226841
226842 if( pDlidx->bPrevValid ){
226843 iVal = iRowid - pDlidx->iPrev;
226844 }else{
226845 i64 iPgno = (i==0 ? pWriter->writer.pgno : pDlidx[-1].pgno);
226846 assert( pDlidx->buf.n==0 );
226847 sqlite3Fts5BufferAppendVarint(pRc: &p->rc, pBuf: &pDlidx->buf, iVal: !bDone);
226848 sqlite3Fts5BufferAppendVarint(pRc: &p->rc, pBuf: &pDlidx->buf, iVal: iPgno);
226849 iVal = iRowid;
226850 }
226851
226852 sqlite3Fts5BufferAppendVarint(pRc: &p->rc, pBuf: &pDlidx->buf, iVal);
226853 pDlidx->bPrevValid = 1;
226854 pDlidx->iPrev = iRowid;
226855 }
226856}
226857
226858static void fts5WriteFlushLeaf(Fts5Index *p, Fts5SegWriter *pWriter){
226859 static const u8 zero[] = { 0x00, 0x00, 0x00, 0x00 };
226860 Fts5PageWriter *pPage = &pWriter->writer;
226861 i64 iRowid;
226862
226863 assert( (pPage->pgidx.n==0)==(pWriter->bFirstTermInPage) );
226864
226865 /* Set the szLeaf header field. */
226866 assert( 0==fts5GetU16(&pPage->buf.p[2]) );
226867 fts5PutU16(aOut: &pPage->buf.p[2], iVal: (u16)pPage->buf.n);
226868
226869 if( pWriter->bFirstTermInPage ){
226870 /* No term was written to this page. */
226871 assert( pPage->pgidx.n==0 );
226872 fts5WriteBtreeNoTerm(p, pWriter);
226873 }else{
226874 /* Append the pgidx to the page buffer. Set the szLeaf header field. */
226875 fts5BufferAppendBlob(&p->rc, &pPage->buf, pPage->pgidx.n, pPage->pgidx.p);
226876 }
226877
226878 /* Write the page out to disk */
226879 iRowid = FTS5_SEGMENT_ROWID(pWriter->iSegid, pPage->pgno);
226880 fts5DataWrite(p, iRowid, pData: pPage->buf.p, nData: pPage->buf.n);
226881
226882 /* Initialize the next page. */
226883 fts5BufferZero(&pPage->buf);
226884 fts5BufferZero(&pPage->pgidx);
226885 fts5BufferAppendBlob(&p->rc, &pPage->buf, 4, zero);
226886 pPage->iPrevPgidx = 0;
226887 pPage->pgno++;
226888
226889 /* Increase the leaves written counter */
226890 pWriter->nLeafWritten++;
226891
226892 /* The new leaf holds no terms or rowids */
226893 pWriter->bFirstTermInPage = 1;
226894 pWriter->bFirstRowidInPage = 1;
226895}
226896
226897/*
226898** Append term pTerm/nTerm to the segment being written by the writer passed
226899** as the second argument.
226900**
226901** If an error occurs, set the Fts5Index.rc error code. If an error has
226902** already occurred, this function is a no-op.
226903*/
226904static void fts5WriteAppendTerm(
226905 Fts5Index *p,
226906 Fts5SegWriter *pWriter,
226907 int nTerm, const u8 *pTerm
226908){
226909 int nPrefix; /* Bytes of prefix compression for term */
226910 Fts5PageWriter *pPage = &pWriter->writer;
226911 Fts5Buffer *pPgidx = &pWriter->writer.pgidx;
226912 int nMin = MIN(pPage->term.n, nTerm);
226913
226914 assert( p->rc==SQLITE_OK );
226915 assert( pPage->buf.n>=4 );
226916 assert( pPage->buf.n>4 || pWriter->bFirstTermInPage );
226917
226918 /* If the current leaf page is full, flush it to disk. */
226919 if( (pPage->buf.n + pPgidx->n + nTerm + 2)>=p->pConfig->pgsz ){
226920 if( pPage->buf.n>4 ){
226921 fts5WriteFlushLeaf(p, pWriter);
226922 if( p->rc!=SQLITE_OK ) return;
226923 }
226924 fts5BufferGrow(&p->rc, &pPage->buf, nTerm+FTS5_DATA_PADDING);
226925 }
226926
226927 /* TODO1: Updating pgidx here. */
226928 pPgidx->n += sqlite3Fts5PutVarint(
226929 p: &pPgidx->p[pPgidx->n], v: pPage->buf.n - pPage->iPrevPgidx
226930 );
226931 pPage->iPrevPgidx = pPage->buf.n;
226932#if 0
226933 fts5PutU16(&pPgidx->p[pPgidx->n], pPage->buf.n);
226934 pPgidx->n += 2;
226935#endif
226936
226937 if( pWriter->bFirstTermInPage ){
226938 nPrefix = 0;
226939 if( pPage->pgno!=1 ){
226940 /* This is the first term on a leaf that is not the leftmost leaf in
226941 ** the segment b-tree. In this case it is necessary to add a term to
226942 ** the b-tree hierarchy that is (a) larger than the largest term
226943 ** already written to the segment and (b) smaller than or equal to
226944 ** this term. In other words, a prefix of (pTerm/nTerm) that is one
226945 ** byte longer than the longest prefix (pTerm/nTerm) shares with the
226946 ** previous term.
226947 **
226948 ** Usually, the previous term is available in pPage->term. The exception
226949 ** is if this is the first term written in an incremental-merge step.
226950 ** In this case the previous term is not available, so just write a
226951 ** copy of (pTerm/nTerm) into the parent node. This is slightly
226952 ** inefficient, but still correct. */
226953 int n = nTerm;
226954 if( pPage->term.n ){
226955 n = 1 + fts5PrefixCompress(nOld: nMin, pOld: pPage->term.p, pNew: pTerm);
226956 }
226957 fts5WriteBtreeTerm(p, pWriter, nTerm: n, pTerm);
226958 if( p->rc!=SQLITE_OK ) return;
226959 pPage = &pWriter->writer;
226960 }
226961 }else{
226962 nPrefix = fts5PrefixCompress(nOld: nMin, pOld: pPage->term.p, pNew: pTerm);
226963 fts5BufferAppendVarint(&p->rc, &pPage->buf, nPrefix);
226964 }
226965
226966 /* Append the number of bytes of new data, then the term data itself
226967 ** to the page. */
226968 fts5BufferAppendVarint(&p->rc, &pPage->buf, nTerm - nPrefix);
226969 fts5BufferAppendBlob(&p->rc, &pPage->buf, nTerm - nPrefix, &pTerm[nPrefix]);
226970
226971 /* Update the Fts5PageWriter.term field. */
226972 fts5BufferSet(&p->rc, &pPage->term, nTerm, pTerm);
226973 pWriter->bFirstTermInPage = 0;
226974
226975 pWriter->bFirstRowidInPage = 0;
226976 pWriter->bFirstRowidInDoclist = 1;
226977
226978 assert( p->rc || (pWriter->nDlidx>0 && pWriter->aDlidx[0].buf.n==0) );
226979 pWriter->aDlidx[0].pgno = pPage->pgno;
226980}
226981
226982/*
226983** Append a rowid and position-list size field to the writers output.
226984*/
226985static void fts5WriteAppendRowid(
226986 Fts5Index *p,
226987 Fts5SegWriter *pWriter,
226988 i64 iRowid
226989){
226990 if( p->rc==SQLITE_OK ){
226991 Fts5PageWriter *pPage = &pWriter->writer;
226992
226993 if( (pPage->buf.n + pPage->pgidx.n)>=p->pConfig->pgsz ){
226994 fts5WriteFlushLeaf(p, pWriter);
226995 }
226996
226997 /* If this is to be the first rowid written to the page, set the
226998 ** rowid-pointer in the page-header. Also append a value to the dlidx
226999 ** buffer, in case a doclist-index is required. */
227000 if( pWriter->bFirstRowidInPage ){
227001 fts5PutU16(aOut: pPage->buf.p, iVal: (u16)pPage->buf.n);
227002 fts5WriteDlidxAppend(p, pWriter, iRowid);
227003 }
227004
227005 /* Write the rowid. */
227006 if( pWriter->bFirstRowidInDoclist || pWriter->bFirstRowidInPage ){
227007 fts5BufferAppendVarint(&p->rc, &pPage->buf, iRowid);
227008 }else{
227009 assert_nc( p->rc || iRowid>pWriter->iPrevRowid );
227010 fts5BufferAppendVarint(&p->rc, &pPage->buf, iRowid - pWriter->iPrevRowid);
227011 }
227012 pWriter->iPrevRowid = iRowid;
227013 pWriter->bFirstRowidInDoclist = 0;
227014 pWriter->bFirstRowidInPage = 0;
227015 }
227016}
227017
227018static void fts5WriteAppendPoslistData(
227019 Fts5Index *p,
227020 Fts5SegWriter *pWriter,
227021 const u8 *aData,
227022 int nData
227023){
227024 Fts5PageWriter *pPage = &pWriter->writer;
227025 const u8 *a = aData;
227026 int n = nData;
227027
227028 assert( p->pConfig->pgsz>0 );
227029 while( p->rc==SQLITE_OK
227030 && (pPage->buf.n + pPage->pgidx.n + n)>=p->pConfig->pgsz
227031 ){
227032 int nReq = p->pConfig->pgsz - pPage->buf.n - pPage->pgidx.n;
227033 int nCopy = 0;
227034 while( nCopy<nReq ){
227035 i64 dummy;
227036 nCopy += fts5GetVarint(&a[nCopy], (u64*)&dummy);
227037 }
227038 fts5BufferAppendBlob(&p->rc, &pPage->buf, nCopy, a);
227039 a += nCopy;
227040 n -= nCopy;
227041 fts5WriteFlushLeaf(p, pWriter);
227042 }
227043 if( n>0 ){
227044 fts5BufferAppendBlob(&p->rc, &pPage->buf, n, a);
227045 }
227046}
227047
227048/*
227049** Flush any data cached by the writer object to the database. Free any
227050** allocations associated with the writer.
227051*/
227052static void fts5WriteFinish(
227053 Fts5Index *p,
227054 Fts5SegWriter *pWriter, /* Writer object */
227055 int *pnLeaf /* OUT: Number of leaf pages in b-tree */
227056){
227057 int i;
227058 Fts5PageWriter *pLeaf = &pWriter->writer;
227059 if( p->rc==SQLITE_OK ){
227060 assert( pLeaf->pgno>=1 );
227061 if( pLeaf->buf.n>4 ){
227062 fts5WriteFlushLeaf(p, pWriter);
227063 }
227064 *pnLeaf = pLeaf->pgno-1;
227065 if( pLeaf->pgno>1 ){
227066 fts5WriteFlushBtree(p, pWriter);
227067 }
227068 }
227069 fts5BufferFree(&pLeaf->term);
227070 fts5BufferFree(&pLeaf->buf);
227071 fts5BufferFree(&pLeaf->pgidx);
227072 fts5BufferFree(&pWriter->btterm);
227073
227074 for(i=0; i<pWriter->nDlidx; i++){
227075 sqlite3Fts5BufferFree(pBuf: &pWriter->aDlidx[i].buf);
227076 }
227077 sqlite3_free(p: pWriter->aDlidx);
227078}
227079
227080static void fts5WriteInit(
227081 Fts5Index *p,
227082 Fts5SegWriter *pWriter,
227083 int iSegid
227084){
227085 const int nBuffer = p->pConfig->pgsz + FTS5_DATA_PADDING;
227086
227087 memset(s: pWriter, c: 0, n: sizeof(Fts5SegWriter));
227088 pWriter->iSegid = iSegid;
227089
227090 fts5WriteDlidxGrow(p, pWriter, nLvl: 1);
227091 pWriter->writer.pgno = 1;
227092 pWriter->bFirstTermInPage = 1;
227093 pWriter->iBtPage = 1;
227094
227095 assert( pWriter->writer.buf.n==0 );
227096 assert( pWriter->writer.pgidx.n==0 );
227097
227098 /* Grow the two buffers to pgsz + padding bytes in size. */
227099 sqlite3Fts5BufferSize(pRc: &p->rc, pBuf: &pWriter->writer.pgidx, nByte: nBuffer);
227100 sqlite3Fts5BufferSize(pRc: &p->rc, pBuf: &pWriter->writer.buf, nByte: nBuffer);
227101
227102 if( p->pIdxWriter==0 ){
227103 Fts5Config *pConfig = p->pConfig;
227104 fts5IndexPrepareStmt(p, ppStmt: &p->pIdxWriter, zSql: sqlite3_mprintf(
227105 zFormat: "INSERT INTO '%q'.'%q_idx'(segid,term,pgno) VALUES(?,?,?)",
227106 pConfig->zDb, pConfig->zName
227107 ));
227108 }
227109
227110 if( p->rc==SQLITE_OK ){
227111 /* Initialize the 4-byte leaf-page header to 0x00. */
227112 memset(s: pWriter->writer.buf.p, c: 0, n: 4);
227113 pWriter->writer.buf.n = 4;
227114
227115 /* Bind the current output segment id to the index-writer. This is an
227116 ** optimization over binding the same value over and over as rows are
227117 ** inserted into %_idx by the current writer. */
227118 sqlite3_bind_int(p: p->pIdxWriter, i: 1, iValue: pWriter->iSegid);
227119 }
227120}
227121
227122/*
227123** Iterator pIter was used to iterate through the input segments of on an
227124** incremental merge operation. This function is called if the incremental
227125** merge step has finished but the input has not been completely exhausted.
227126*/
227127static void fts5TrimSegments(Fts5Index *p, Fts5Iter *pIter){
227128 int i;
227129 Fts5Buffer buf;
227130 memset(s: &buf, c: 0, n: sizeof(Fts5Buffer));
227131 for(i=0; i<pIter->nSeg && p->rc==SQLITE_OK; i++){
227132 Fts5SegIter *pSeg = &pIter->aSeg[i];
227133 if( pSeg->pSeg==0 ){
227134 /* no-op */
227135 }else if( pSeg->pLeaf==0 ){
227136 /* All keys from this input segment have been transfered to the output.
227137 ** Set both the first and last page-numbers to 0 to indicate that the
227138 ** segment is now empty. */
227139 pSeg->pSeg->pgnoLast = 0;
227140 pSeg->pSeg->pgnoFirst = 0;
227141 }else{
227142 int iOff = pSeg->iTermLeafOffset; /* Offset on new first leaf page */
227143 i64 iLeafRowid;
227144 Fts5Data *pData;
227145 int iId = pSeg->pSeg->iSegid;
227146 u8 aHdr[4] = {0x00, 0x00, 0x00, 0x00};
227147
227148 iLeafRowid = FTS5_SEGMENT_ROWID(iId, pSeg->iTermLeafPgno);
227149 pData = fts5LeafRead(p, iRowid: iLeafRowid);
227150 if( pData ){
227151 if( iOff>pData->szLeaf ){
227152 /* This can occur if the pages that the segments occupy overlap - if
227153 ** a single page has been assigned to more than one segment. In
227154 ** this case a prior iteration of this loop may have corrupted the
227155 ** segment currently being trimmed. */
227156 p->rc = FTS5_CORRUPT;
227157 }else{
227158 fts5BufferZero(&buf);
227159 fts5BufferGrow(&p->rc, &buf, pData->nn);
227160 fts5BufferAppendBlob(&p->rc, &buf, sizeof(aHdr), aHdr);
227161 fts5BufferAppendVarint(&p->rc, &buf, pSeg->term.n);
227162 fts5BufferAppendBlob(&p->rc, &buf, pSeg->term.n, pSeg->term.p);
227163 fts5BufferAppendBlob(&p->rc, &buf, pData->szLeaf-iOff,&pData->p[iOff]);
227164 if( p->rc==SQLITE_OK ){
227165 /* Set the szLeaf field */
227166 fts5PutU16(aOut: &buf.p[2], iVal: (u16)buf.n);
227167 }
227168
227169 /* Set up the new page-index array */
227170 fts5BufferAppendVarint(&p->rc, &buf, 4);
227171 if( pSeg->iLeafPgno==pSeg->iTermLeafPgno
227172 && pSeg->iEndofDoclist<pData->szLeaf
227173 && pSeg->iPgidxOff<=pData->nn
227174 ){
227175 int nDiff = pData->szLeaf - pSeg->iEndofDoclist;
227176 fts5BufferAppendVarint(&p->rc, &buf, buf.n - 1 - nDiff - 4);
227177 fts5BufferAppendBlob(&p->rc, &buf,
227178 pData->nn - pSeg->iPgidxOff, &pData->p[pSeg->iPgidxOff]
227179 );
227180 }
227181
227182 pSeg->pSeg->pgnoFirst = pSeg->iTermLeafPgno;
227183 fts5DataDelete(p, FTS5_SEGMENT_ROWID(iId, 1), iLast: iLeafRowid);
227184 fts5DataWrite(p, iRowid: iLeafRowid, pData: buf.p, nData: buf.n);
227185 }
227186 fts5DataRelease(pData);
227187 }
227188 }
227189 }
227190 fts5BufferFree(&buf);
227191}
227192
227193static void fts5MergeChunkCallback(
227194 Fts5Index *p,
227195 void *pCtx,
227196 const u8 *pChunk, int nChunk
227197){
227198 Fts5SegWriter *pWriter = (Fts5SegWriter*)pCtx;
227199 fts5WriteAppendPoslistData(p, pWriter, aData: pChunk, nData: nChunk);
227200}
227201
227202/*
227203**
227204*/
227205static void fts5IndexMergeLevel(
227206 Fts5Index *p, /* FTS5 backend object */
227207 Fts5Structure **ppStruct, /* IN/OUT: Stucture of index */
227208 int iLvl, /* Level to read input from */
227209 int *pnRem /* Write up to this many output leaves */
227210){
227211 Fts5Structure *pStruct = *ppStruct;
227212 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
227213 Fts5StructureLevel *pLvlOut;
227214 Fts5Iter *pIter = 0; /* Iterator to read input data */
227215 int nRem = pnRem ? *pnRem : 0; /* Output leaf pages left to write */
227216 int nInput; /* Number of input segments */
227217 Fts5SegWriter writer; /* Writer object */
227218 Fts5StructureSegment *pSeg; /* Output segment */
227219 Fts5Buffer term;
227220 int bOldest; /* True if the output segment is the oldest */
227221 int eDetail = p->pConfig->eDetail;
227222 const int flags = FTS5INDEX_QUERY_NOOUTPUT;
227223 int bTermWritten = 0; /* True if current term already output */
227224
227225 assert( iLvl<pStruct->nLevel );
227226 assert( pLvl->nMerge<=pLvl->nSeg );
227227
227228 memset(s: &writer, c: 0, n: sizeof(Fts5SegWriter));
227229 memset(s: &term, c: 0, n: sizeof(Fts5Buffer));
227230 if( pLvl->nMerge ){
227231 pLvlOut = &pStruct->aLevel[iLvl+1];
227232 assert( pLvlOut->nSeg>0 );
227233 nInput = pLvl->nMerge;
227234 pSeg = &pLvlOut->aSeg[pLvlOut->nSeg-1];
227235
227236 fts5WriteInit(p, pWriter: &writer, iSegid: pSeg->iSegid);
227237 writer.writer.pgno = pSeg->pgnoLast+1;
227238 writer.iBtPage = 0;
227239 }else{
227240 int iSegid = fts5AllocateSegid(p, pStruct);
227241
227242 /* Extend the Fts5Structure object as required to ensure the output
227243 ** segment exists. */
227244 if( iLvl==pStruct->nLevel-1 ){
227245 fts5StructureAddLevel(pRc: &p->rc, ppStruct);
227246 pStruct = *ppStruct;
227247 }
227248 fts5StructureExtendLevel(pRc: &p->rc, pStruct, iLvl: iLvl+1, nExtra: 1, bInsert: 0);
227249 if( p->rc ) return;
227250 pLvl = &pStruct->aLevel[iLvl];
227251 pLvlOut = &pStruct->aLevel[iLvl+1];
227252
227253 fts5WriteInit(p, pWriter: &writer, iSegid);
227254
227255 /* Add the new segment to the output level */
227256 pSeg = &pLvlOut->aSeg[pLvlOut->nSeg];
227257 pLvlOut->nSeg++;
227258 pSeg->pgnoFirst = 1;
227259 pSeg->iSegid = iSegid;
227260 pStruct->nSegment++;
227261
227262 /* Read input from all segments in the input level */
227263 nInput = pLvl->nSeg;
227264 }
227265 bOldest = (pLvlOut->nSeg==1 && pStruct->nLevel==iLvl+2);
227266
227267 assert( iLvl>=0 );
227268 for(fts5MultiIterNew(p, pStruct, flags, pColset: 0, pTerm: 0, nTerm: 0, iLevel: iLvl, nSegment: nInput, ppOut: &pIter);
227269 fts5MultiIterEof(p, pIter)==0;
227270 fts5MultiIterNext(p, pIter, bFrom: 0, iFrom: 0)
227271 ){
227272 Fts5SegIter *pSegIter = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
227273 int nPos; /* position-list size field value */
227274 int nTerm;
227275 const u8 *pTerm;
227276
227277 pTerm = fts5MultiIterTerm(pIter, pn: &nTerm);
227278 if( nTerm!=term.n || fts5Memcmp(pTerm, term.p, nTerm) ){
227279 if( pnRem && writer.nLeafWritten>nRem ){
227280 break;
227281 }
227282 fts5BufferSet(&p->rc, &term, nTerm, pTerm);
227283 bTermWritten =0;
227284 }
227285
227286 /* Check for key annihilation. */
227287 if( pSegIter->nPos==0 && (bOldest || pSegIter->bDel==0) ) continue;
227288
227289 if( p->rc==SQLITE_OK && bTermWritten==0 ){
227290 /* This is a new term. Append a term to the output segment. */
227291 fts5WriteAppendTerm(p, pWriter: &writer, nTerm, pTerm);
227292 bTermWritten = 1;
227293 }
227294
227295 /* Append the rowid to the output */
227296 /* WRITEPOSLISTSIZE */
227297 fts5WriteAppendRowid(p, pWriter: &writer, iRowid: fts5MultiIterRowid(pIter));
227298
227299 if( eDetail==FTS5_DETAIL_NONE ){
227300 if( pSegIter->bDel ){
227301 fts5BufferAppendVarint(&p->rc, &writer.writer.buf, 0);
227302 if( pSegIter->nPos>0 ){
227303 fts5BufferAppendVarint(&p->rc, &writer.writer.buf, 0);
227304 }
227305 }
227306 }else{
227307 /* Append the position-list data to the output */
227308 nPos = pSegIter->nPos*2 + pSegIter->bDel;
227309 fts5BufferAppendVarint(&p->rc, &writer.writer.buf, nPos);
227310 fts5ChunkIterate(p, pSeg: pSegIter, pCtx: (void*)&writer, xChunk: fts5MergeChunkCallback);
227311 }
227312 }
227313
227314 /* Flush the last leaf page to disk. Set the output segment b-tree height
227315 ** and last leaf page number at the same time. */
227316 fts5WriteFinish(p, pWriter: &writer, pnLeaf: &pSeg->pgnoLast);
227317
227318 assert( pIter!=0 || p->rc!=SQLITE_OK );
227319 if( fts5MultiIterEof(p, pIter) ){
227320 int i;
227321
227322 /* Remove the redundant segments from the %_data table */
227323 for(i=0; i<nInput; i++){
227324 fts5DataRemoveSegment(p, iSegid: pLvl->aSeg[i].iSegid);
227325 }
227326
227327 /* Remove the redundant segments from the input level */
227328 if( pLvl->nSeg!=nInput ){
227329 int nMove = (pLvl->nSeg - nInput) * sizeof(Fts5StructureSegment);
227330 memmove(dest: pLvl->aSeg, src: &pLvl->aSeg[nInput], n: nMove);
227331 }
227332 pStruct->nSegment -= nInput;
227333 pLvl->nSeg -= nInput;
227334 pLvl->nMerge = 0;
227335 if( pSeg->pgnoLast==0 ){
227336 pLvlOut->nSeg--;
227337 pStruct->nSegment--;
227338 }
227339 }else{
227340 assert( pSeg->pgnoLast>0 );
227341 fts5TrimSegments(p, pIter);
227342 pLvl->nMerge = nInput;
227343 }
227344
227345 fts5MultiIterFree(pIter);
227346 fts5BufferFree(&term);
227347 if( pnRem ) *pnRem -= writer.nLeafWritten;
227348}
227349
227350/*
227351** Do up to nPg pages of automerge work on the index.
227352**
227353** Return true if any changes were actually made, or false otherwise.
227354*/
227355static int fts5IndexMerge(
227356 Fts5Index *p, /* FTS5 backend object */
227357 Fts5Structure **ppStruct, /* IN/OUT: Current structure of index */
227358 int nPg, /* Pages of work to do */
227359 int nMin /* Minimum number of segments to merge */
227360){
227361 int nRem = nPg;
227362 int bRet = 0;
227363 Fts5Structure *pStruct = *ppStruct;
227364 while( nRem>0 && p->rc==SQLITE_OK ){
227365 int iLvl; /* To iterate through levels */
227366 int iBestLvl = 0; /* Level offering the most input segments */
227367 int nBest = 0; /* Number of input segments on best level */
227368
227369 /* Set iBestLvl to the level to read input segments from. */
227370 assert( pStruct->nLevel>0 );
227371 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
227372 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
227373 if( pLvl->nMerge ){
227374 if( pLvl->nMerge>nBest ){
227375 iBestLvl = iLvl;
227376 nBest = pLvl->nMerge;
227377 }
227378 break;
227379 }
227380 if( pLvl->nSeg>nBest ){
227381 nBest = pLvl->nSeg;
227382 iBestLvl = iLvl;
227383 }
227384 }
227385
227386 /* If nBest is still 0, then the index must be empty. */
227387#ifdef SQLITE_DEBUG
227388 for(iLvl=0; nBest==0 && iLvl<pStruct->nLevel; iLvl++){
227389 assert( pStruct->aLevel[iLvl].nSeg==0 );
227390 }
227391#endif
227392
227393 if( nBest<nMin && pStruct->aLevel[iBestLvl].nMerge==0 ){
227394 break;
227395 }
227396 bRet = 1;
227397 fts5IndexMergeLevel(p, ppStruct: &pStruct, iLvl: iBestLvl, pnRem: &nRem);
227398 if( p->rc==SQLITE_OK && pStruct->aLevel[iBestLvl].nMerge==0 ){
227399 fts5StructurePromote(p, iLvl: iBestLvl+1, pStruct);
227400 }
227401 }
227402 *ppStruct = pStruct;
227403 return bRet;
227404}
227405
227406/*
227407** A total of nLeaf leaf pages of data has just been flushed to a level-0
227408** segment. This function updates the write-counter accordingly and, if
227409** necessary, performs incremental merge work.
227410**
227411** If an error occurs, set the Fts5Index.rc error code. If an error has
227412** already occurred, this function is a no-op.
227413*/
227414static void fts5IndexAutomerge(
227415 Fts5Index *p, /* FTS5 backend object */
227416 Fts5Structure **ppStruct, /* IN/OUT: Current structure of index */
227417 int nLeaf /* Number of output leaves just written */
227418){
227419 if( p->rc==SQLITE_OK && p->pConfig->nAutomerge>0 && ALWAYS((*ppStruct)!=0) ){
227420 Fts5Structure *pStruct = *ppStruct;
227421 u64 nWrite; /* Initial value of write-counter */
227422 int nWork; /* Number of work-quanta to perform */
227423 int nRem; /* Number of leaf pages left to write */
227424
227425 /* Update the write-counter. While doing so, set nWork. */
227426 nWrite = pStruct->nWriteCounter;
227427 nWork = (int)(((nWrite + nLeaf) / p->nWorkUnit) - (nWrite / p->nWorkUnit));
227428 pStruct->nWriteCounter += nLeaf;
227429 nRem = (int)(p->nWorkUnit * nWork * pStruct->nLevel);
227430
227431 fts5IndexMerge(p, ppStruct, nPg: nRem, nMin: p->pConfig->nAutomerge);
227432 }
227433}
227434
227435static void fts5IndexCrisismerge(
227436 Fts5Index *p, /* FTS5 backend object */
227437 Fts5Structure **ppStruct /* IN/OUT: Current structure of index */
227438){
227439 const int nCrisis = p->pConfig->nCrisisMerge;
227440 Fts5Structure *pStruct = *ppStruct;
227441 int iLvl = 0;
227442
227443 assert( p->rc!=SQLITE_OK || pStruct->nLevel>0 );
227444 while( p->rc==SQLITE_OK && pStruct->aLevel[iLvl].nSeg>=nCrisis ){
227445 fts5IndexMergeLevel(p, ppStruct: &pStruct, iLvl, pnRem: 0);
227446 assert( p->rc!=SQLITE_OK || pStruct->nLevel>(iLvl+1) );
227447 fts5StructurePromote(p, iLvl: iLvl+1, pStruct);
227448 iLvl++;
227449 }
227450 *ppStruct = pStruct;
227451}
227452
227453static int fts5IndexReturn(Fts5Index *p){
227454 int rc = p->rc;
227455 p->rc = SQLITE_OK;
227456 return rc;
227457}
227458
227459typedef struct Fts5FlushCtx Fts5FlushCtx;
227460struct Fts5FlushCtx {
227461 Fts5Index *pIdx;
227462 Fts5SegWriter writer;
227463};
227464
227465/*
227466** Buffer aBuf[] contains a list of varints, all small enough to fit
227467** in a 32-bit integer. Return the size of the largest prefix of this
227468** list nMax bytes or less in size.
227469*/
227470static int fts5PoslistPrefix(const u8 *aBuf, int nMax){
227471 int ret;
227472 u32 dummy;
227473 ret = fts5GetVarint32(aBuf, dummy);
227474 if( ret<nMax ){
227475 while( 1 ){
227476 int i = fts5GetVarint32(&aBuf[ret], dummy);
227477 if( (ret + i) > nMax ) break;
227478 ret += i;
227479 }
227480 }
227481 return ret;
227482}
227483
227484/*
227485** Flush the contents of in-memory hash table iHash to a new level-0
227486** segment on disk. Also update the corresponding structure record.
227487**
227488** If an error occurs, set the Fts5Index.rc error code. If an error has
227489** already occurred, this function is a no-op.
227490*/
227491static void fts5FlushOneHash(Fts5Index *p){
227492 Fts5Hash *pHash = p->pHash;
227493 Fts5Structure *pStruct;
227494 int iSegid;
227495 int pgnoLast = 0; /* Last leaf page number in segment */
227496
227497 /* Obtain a reference to the index structure and allocate a new segment-id
227498 ** for the new level-0 segment. */
227499 pStruct = fts5StructureRead(p);
227500 iSegid = fts5AllocateSegid(p, pStruct);
227501 fts5StructureInvalidate(p);
227502
227503 if( iSegid ){
227504 const int pgsz = p->pConfig->pgsz;
227505 int eDetail = p->pConfig->eDetail;
227506 Fts5StructureSegment *pSeg; /* New segment within pStruct */
227507 Fts5Buffer *pBuf; /* Buffer in which to assemble leaf page */
227508 Fts5Buffer *pPgidx; /* Buffer in which to assemble pgidx */
227509
227510 Fts5SegWriter writer;
227511 fts5WriteInit(p, pWriter: &writer, iSegid);
227512
227513 pBuf = &writer.writer.buf;
227514 pPgidx = &writer.writer.pgidx;
227515
227516 /* fts5WriteInit() should have initialized the buffers to (most likely)
227517 ** the maximum space required. */
227518 assert( p->rc || pBuf->nSpace>=(pgsz + FTS5_DATA_PADDING) );
227519 assert( p->rc || pPgidx->nSpace>=(pgsz + FTS5_DATA_PADDING) );
227520
227521 /* Begin scanning through hash table entries. This loop runs once for each
227522 ** term/doclist currently stored within the hash table. */
227523 if( p->rc==SQLITE_OK ){
227524 p->rc = sqlite3Fts5HashScanInit(p: pHash, pTerm: 0, nTerm: 0);
227525 }
227526 while( p->rc==SQLITE_OK && 0==sqlite3Fts5HashScanEof(p: pHash) ){
227527 const char *zTerm; /* Buffer containing term */
227528 const u8 *pDoclist; /* Pointer to doclist for this term */
227529 int nDoclist; /* Size of doclist in bytes */
227530
227531 /* Write the term for this entry to disk. */
227532 sqlite3Fts5HashScanEntry(pHash, pzTerm: &zTerm, ppDoclist: &pDoclist, pnDoclist: &nDoclist);
227533 fts5WriteAppendTerm(p, pWriter: &writer, nTerm: (int)strlen(s: zTerm), pTerm: (const u8*)zTerm);
227534 if( p->rc!=SQLITE_OK ) break;
227535
227536 assert( writer.bFirstRowidInPage==0 );
227537 if( pgsz>=(pBuf->n + pPgidx->n + nDoclist + 1) ){
227538 /* The entire doclist will fit on the current leaf. */
227539 fts5BufferSafeAppendBlob(pBuf, pDoclist, nDoclist);
227540 }else{
227541 i64 iRowid = 0;
227542 u64 iDelta = 0;
227543 int iOff = 0;
227544
227545 /* The entire doclist will not fit on this leaf. The following
227546 ** loop iterates through the poslists that make up the current
227547 ** doclist. */
227548 while( p->rc==SQLITE_OK && iOff<nDoclist ){
227549 iOff += fts5GetVarint(&pDoclist[iOff], &iDelta);
227550 iRowid += iDelta;
227551
227552 if( writer.bFirstRowidInPage ){
227553 fts5PutU16(aOut: &pBuf->p[0], iVal: (u16)pBuf->n); /* first rowid on page */
227554 pBuf->n += sqlite3Fts5PutVarint(p: &pBuf->p[pBuf->n], v: iRowid);
227555 writer.bFirstRowidInPage = 0;
227556 fts5WriteDlidxAppend(p, pWriter: &writer, iRowid);
227557 if( p->rc!=SQLITE_OK ) break;
227558 }else{
227559 pBuf->n += sqlite3Fts5PutVarint(p: &pBuf->p[pBuf->n], v: iDelta);
227560 }
227561 assert( pBuf->n<=pBuf->nSpace );
227562
227563 if( eDetail==FTS5_DETAIL_NONE ){
227564 if( iOff<nDoclist && pDoclist[iOff]==0 ){
227565 pBuf->p[pBuf->n++] = 0;
227566 iOff++;
227567 if( iOff<nDoclist && pDoclist[iOff]==0 ){
227568 pBuf->p[pBuf->n++] = 0;
227569 iOff++;
227570 }
227571 }
227572 if( (pBuf->n + pPgidx->n)>=pgsz ){
227573 fts5WriteFlushLeaf(p, pWriter: &writer);
227574 }
227575 }else{
227576 int bDummy;
227577 int nPos;
227578 int nCopy = fts5GetPoslistSize(p: &pDoclist[iOff], pnSz: &nPos, pbDel: &bDummy);
227579 nCopy += nPos;
227580 if( (pBuf->n + pPgidx->n + nCopy) <= pgsz ){
227581 /* The entire poslist will fit on the current leaf. So copy
227582 ** it in one go. */
227583 fts5BufferSafeAppendBlob(pBuf, &pDoclist[iOff], nCopy);
227584 }else{
227585 /* The entire poslist will not fit on this leaf. So it needs
227586 ** to be broken into sections. The only qualification being
227587 ** that each varint must be stored contiguously. */
227588 const u8 *pPoslist = &pDoclist[iOff];
227589 int iPos = 0;
227590 while( p->rc==SQLITE_OK ){
227591 int nSpace = pgsz - pBuf->n - pPgidx->n;
227592 int n = 0;
227593 if( (nCopy - iPos)<=nSpace ){
227594 n = nCopy - iPos;
227595 }else{
227596 n = fts5PoslistPrefix(aBuf: &pPoslist[iPos], nMax: nSpace);
227597 }
227598 assert( n>0 );
227599 fts5BufferSafeAppendBlob(pBuf, &pPoslist[iPos], n);
227600 iPos += n;
227601 if( (pBuf->n + pPgidx->n)>=pgsz ){
227602 fts5WriteFlushLeaf(p, pWriter: &writer);
227603 }
227604 if( iPos>=nCopy ) break;
227605 }
227606 }
227607 iOff += nCopy;
227608 }
227609 }
227610 }
227611
227612 /* TODO2: Doclist terminator written here. */
227613 /* pBuf->p[pBuf->n++] = '\0'; */
227614 assert( pBuf->n<=pBuf->nSpace );
227615 if( p->rc==SQLITE_OK ) sqlite3Fts5HashScanNext(p: pHash);
227616 }
227617 sqlite3Fts5HashClear(pHash);
227618 fts5WriteFinish(p, pWriter: &writer, pnLeaf: &pgnoLast);
227619
227620 /* Update the Fts5Structure. It is written back to the database by the
227621 ** fts5StructureRelease() call below. */
227622 if( pStruct->nLevel==0 ){
227623 fts5StructureAddLevel(pRc: &p->rc, ppStruct: &pStruct);
227624 }
227625 fts5StructureExtendLevel(pRc: &p->rc, pStruct, iLvl: 0, nExtra: 1, bInsert: 0);
227626 if( p->rc==SQLITE_OK ){
227627 pSeg = &pStruct->aLevel[0].aSeg[ pStruct->aLevel[0].nSeg++ ];
227628 pSeg->iSegid = iSegid;
227629 pSeg->pgnoFirst = 1;
227630 pSeg->pgnoLast = pgnoLast;
227631 pStruct->nSegment++;
227632 }
227633 fts5StructurePromote(p, iLvl: 0, pStruct);
227634 }
227635
227636 fts5IndexAutomerge(p, ppStruct: &pStruct, nLeaf: pgnoLast);
227637 fts5IndexCrisismerge(p, ppStruct: &pStruct);
227638 fts5StructureWrite(p, pStruct);
227639 fts5StructureRelease(pStruct);
227640}
227641
227642/*
227643** Flush any data stored in the in-memory hash tables to the database.
227644*/
227645static void fts5IndexFlush(Fts5Index *p){
227646 /* Unless it is empty, flush the hash table to disk */
227647 if( p->nPendingData ){
227648 assert( p->pHash );
227649 p->nPendingData = 0;
227650 fts5FlushOneHash(p);
227651 }
227652}
227653
227654static Fts5Structure *fts5IndexOptimizeStruct(
227655 Fts5Index *p,
227656 Fts5Structure *pStruct
227657){
227658 Fts5Structure *pNew = 0;
227659 sqlite3_int64 nByte = sizeof(Fts5Structure);
227660 int nSeg = pStruct->nSegment;
227661 int i;
227662
227663 /* Figure out if this structure requires optimization. A structure does
227664 ** not require optimization if either:
227665 **
227666 ** + it consists of fewer than two segments, or
227667 ** + all segments are on the same level, or
227668 ** + all segments except one are currently inputs to a merge operation.
227669 **
227670 ** In the first case, return NULL. In the second, increment the ref-count
227671 ** on *pStruct and return a copy of the pointer to it.
227672 */
227673 if( nSeg<2 ) return 0;
227674 for(i=0; i<pStruct->nLevel; i++){
227675 int nThis = pStruct->aLevel[i].nSeg;
227676 if( nThis==nSeg || (nThis==nSeg-1 && pStruct->aLevel[i].nMerge==nThis) ){
227677 fts5StructureRef(pStruct);
227678 return pStruct;
227679 }
227680 assert( pStruct->aLevel[i].nMerge<=nThis );
227681 }
227682
227683 nByte += (pStruct->nLevel+1) * sizeof(Fts5StructureLevel);
227684 pNew = (Fts5Structure*)sqlite3Fts5MallocZero(pRc: &p->rc, nByte);
227685
227686 if( pNew ){
227687 Fts5StructureLevel *pLvl;
227688 nByte = nSeg * sizeof(Fts5StructureSegment);
227689 pNew->nLevel = pStruct->nLevel+1;
227690 pNew->nRef = 1;
227691 pNew->nWriteCounter = pStruct->nWriteCounter;
227692 pLvl = &pNew->aLevel[pStruct->nLevel];
227693 pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(pRc: &p->rc, nByte);
227694 if( pLvl->aSeg ){
227695 int iLvl, iSeg;
227696 int iSegOut = 0;
227697 /* Iterate through all segments, from oldest to newest. Add them to
227698 ** the new Fts5Level object so that pLvl->aSeg[0] is the oldest
227699 ** segment in the data structure. */
227700 for(iLvl=pStruct->nLevel-1; iLvl>=0; iLvl--){
227701 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
227702 pLvl->aSeg[iSegOut] = pStruct->aLevel[iLvl].aSeg[iSeg];
227703 iSegOut++;
227704 }
227705 }
227706 pNew->nSegment = pLvl->nSeg = nSeg;
227707 }else{
227708 sqlite3_free(p: pNew);
227709 pNew = 0;
227710 }
227711 }
227712
227713 return pNew;
227714}
227715
227716static int sqlite3Fts5IndexOptimize(Fts5Index *p){
227717 Fts5Structure *pStruct;
227718 Fts5Structure *pNew = 0;
227719
227720 assert( p->rc==SQLITE_OK );
227721 fts5IndexFlush(p);
227722 pStruct = fts5StructureRead(p);
227723 fts5StructureInvalidate(p);
227724
227725 if( pStruct ){
227726 pNew = fts5IndexOptimizeStruct(p, pStruct);
227727 }
227728 fts5StructureRelease(pStruct);
227729
227730 assert( pNew==0 || pNew->nSegment>0 );
227731 if( pNew ){
227732 int iLvl;
227733 for(iLvl=0; pNew->aLevel[iLvl].nSeg==0; iLvl++){}
227734 while( p->rc==SQLITE_OK && pNew->aLevel[iLvl].nSeg>0 ){
227735 int nRem = FTS5_OPT_WORK_UNIT;
227736 fts5IndexMergeLevel(p, ppStruct: &pNew, iLvl, pnRem: &nRem);
227737 }
227738
227739 fts5StructureWrite(p, pStruct: pNew);
227740 fts5StructureRelease(pStruct: pNew);
227741 }
227742
227743 return fts5IndexReturn(p);
227744}
227745
227746/*
227747** This is called to implement the special "VALUES('merge', $nMerge)"
227748** INSERT command.
227749*/
227750static int sqlite3Fts5IndexMerge(Fts5Index *p, int nMerge){
227751 Fts5Structure *pStruct = fts5StructureRead(p);
227752 if( pStruct ){
227753 int nMin = p->pConfig->nUsermerge;
227754 fts5StructureInvalidate(p);
227755 if( nMerge<0 ){
227756 Fts5Structure *pNew = fts5IndexOptimizeStruct(p, pStruct);
227757 fts5StructureRelease(pStruct);
227758 pStruct = pNew;
227759 nMin = 2;
227760 nMerge = nMerge*-1;
227761 }
227762 if( pStruct && pStruct->nLevel ){
227763 if( fts5IndexMerge(p, ppStruct: &pStruct, nPg: nMerge, nMin) ){
227764 fts5StructureWrite(p, pStruct);
227765 }
227766 }
227767 fts5StructureRelease(pStruct);
227768 }
227769 return fts5IndexReturn(p);
227770}
227771
227772static void fts5AppendRowid(
227773 Fts5Index *p,
227774 i64 iDelta,
227775 Fts5Iter *pUnused,
227776 Fts5Buffer *pBuf
227777){
227778 UNUSED_PARAM(pUnused);
227779 fts5BufferAppendVarint(&p->rc, pBuf, iDelta);
227780}
227781
227782static void fts5AppendPoslist(
227783 Fts5Index *p,
227784 i64 iDelta,
227785 Fts5Iter *pMulti,
227786 Fts5Buffer *pBuf
227787){
227788 int nData = pMulti->base.nData;
227789 int nByte = nData + 9 + 9 + FTS5_DATA_ZERO_PADDING;
227790 assert( nData>0 );
227791 if( p->rc==SQLITE_OK && 0==fts5BufferGrow(&p->rc, pBuf, nByte) ){
227792 fts5BufferSafeAppendVarint(pBuf, iDelta);
227793 fts5BufferSafeAppendVarint(pBuf, nData*2);
227794 fts5BufferSafeAppendBlob(pBuf, pMulti->base.pData, nData);
227795 memset(s: &pBuf->p[pBuf->n], c: 0, FTS5_DATA_ZERO_PADDING);
227796 }
227797}
227798
227799
227800static void fts5DoclistIterNext(Fts5DoclistIter *pIter){
227801 u8 *p = pIter->aPoslist + pIter->nSize + pIter->nPoslist;
227802
227803 assert( pIter->aPoslist || (p==0 && pIter->aPoslist==0) );
227804 if( p>=pIter->aEof ){
227805 pIter->aPoslist = 0;
227806 }else{
227807 i64 iDelta;
227808
227809 p += fts5GetVarint(p, (u64*)&iDelta);
227810 pIter->iRowid += iDelta;
227811
227812 /* Read position list size */
227813 if( p[0] & 0x80 ){
227814 int nPos;
227815 pIter->nSize = fts5GetVarint32(p, nPos);
227816 pIter->nPoslist = (nPos>>1);
227817 }else{
227818 pIter->nPoslist = ((int)(p[0])) >> 1;
227819 pIter->nSize = 1;
227820 }
227821
227822 pIter->aPoslist = p;
227823 if( &pIter->aPoslist[pIter->nPoslist]>pIter->aEof ){
227824 pIter->aPoslist = 0;
227825 }
227826 }
227827}
227828
227829static void fts5DoclistIterInit(
227830 Fts5Buffer *pBuf,
227831 Fts5DoclistIter *pIter
227832){
227833 memset(s: pIter, c: 0, n: sizeof(*pIter));
227834 if( pBuf->n>0 ){
227835 pIter->aPoslist = pBuf->p;
227836 pIter->aEof = &pBuf->p[pBuf->n];
227837 fts5DoclistIterNext(pIter);
227838 }
227839}
227840
227841#if 0
227842/*
227843** Append a doclist to buffer pBuf.
227844**
227845** This function assumes that space within the buffer has already been
227846** allocated.
227847*/
227848static void fts5MergeAppendDocid(
227849 Fts5Buffer *pBuf, /* Buffer to write to */
227850 i64 *piLastRowid, /* IN/OUT: Previous rowid written (if any) */
227851 i64 iRowid /* Rowid to append */
227852){
227853 assert( pBuf->n!=0 || (*piLastRowid)==0 );
227854 fts5BufferSafeAppendVarint(pBuf, iRowid - *piLastRowid);
227855 *piLastRowid = iRowid;
227856}
227857#endif
227858
227859#define fts5MergeAppendDocid(pBuf, iLastRowid, iRowid) { \
227860 assert( (pBuf)->n!=0 || (iLastRowid)==0 ); \
227861 fts5BufferSafeAppendVarint((pBuf), (iRowid) - (iLastRowid)); \
227862 (iLastRowid) = (iRowid); \
227863}
227864
227865/*
227866** Swap the contents of buffer *p1 with that of *p2.
227867*/
227868static void fts5BufferSwap(Fts5Buffer *p1, Fts5Buffer *p2){
227869 Fts5Buffer tmp = *p1;
227870 *p1 = *p2;
227871 *p2 = tmp;
227872}
227873
227874static void fts5NextRowid(Fts5Buffer *pBuf, int *piOff, i64 *piRowid){
227875 int i = *piOff;
227876 if( i>=pBuf->n ){
227877 *piOff = -1;
227878 }else{
227879 u64 iVal;
227880 *piOff = i + sqlite3Fts5GetVarint(&pBuf->p[i], &iVal);
227881 *piRowid += iVal;
227882 }
227883}
227884
227885/*
227886** This is the equivalent of fts5MergePrefixLists() for detail=none mode.
227887** In this case the buffers consist of a delta-encoded list of rowids only.
227888*/
227889static void fts5MergeRowidLists(
227890 Fts5Index *p, /* FTS5 backend object */
227891 Fts5Buffer *p1, /* First list to merge */
227892 int nBuf, /* Number of entries in apBuf[] */
227893 Fts5Buffer *aBuf /* Array of other lists to merge into p1 */
227894){
227895 int i1 = 0;
227896 int i2 = 0;
227897 i64 iRowid1 = 0;
227898 i64 iRowid2 = 0;
227899 i64 iOut = 0;
227900 Fts5Buffer *p2 = &aBuf[0];
227901 Fts5Buffer out;
227902
227903 (void)nBuf;
227904 memset(s: &out, c: 0, n: sizeof(out));
227905 assert( nBuf==1 );
227906 sqlite3Fts5BufferSize(pRc: &p->rc, pBuf: &out, nByte: p1->n + p2->n);
227907 if( p->rc ) return;
227908
227909 fts5NextRowid(pBuf: p1, piOff: &i1, piRowid: &iRowid1);
227910 fts5NextRowid(pBuf: p2, piOff: &i2, piRowid: &iRowid2);
227911 while( i1>=0 || i2>=0 ){
227912 if( i1>=0 && (i2<0 || iRowid1<iRowid2) ){
227913 assert( iOut==0 || iRowid1>iOut );
227914 fts5BufferSafeAppendVarint(&out, iRowid1 - iOut);
227915 iOut = iRowid1;
227916 fts5NextRowid(pBuf: p1, piOff: &i1, piRowid: &iRowid1);
227917 }else{
227918 assert( iOut==0 || iRowid2>iOut );
227919 fts5BufferSafeAppendVarint(&out, iRowid2 - iOut);
227920 iOut = iRowid2;
227921 if( i1>=0 && iRowid1==iRowid2 ){
227922 fts5NextRowid(pBuf: p1, piOff: &i1, piRowid: &iRowid1);
227923 }
227924 fts5NextRowid(pBuf: p2, piOff: &i2, piRowid: &iRowid2);
227925 }
227926 }
227927
227928 fts5BufferSwap(p1: &out, p2: p1);
227929 fts5BufferFree(&out);
227930}
227931
227932typedef struct PrefixMerger PrefixMerger;
227933struct PrefixMerger {
227934 Fts5DoclistIter iter; /* Doclist iterator */
227935 i64 iPos; /* For iterating through a position list */
227936 int iOff;
227937 u8 *aPos;
227938 PrefixMerger *pNext; /* Next in docid/poslist order */
227939};
227940
227941static void fts5PrefixMergerInsertByRowid(
227942 PrefixMerger **ppHead,
227943 PrefixMerger *p
227944){
227945 if( p->iter.aPoslist ){
227946 PrefixMerger **pp = ppHead;
227947 while( *pp && p->iter.iRowid>(*pp)->iter.iRowid ){
227948 pp = &(*pp)->pNext;
227949 }
227950 p->pNext = *pp;
227951 *pp = p;
227952 }
227953}
227954
227955static void fts5PrefixMergerInsertByPosition(
227956 PrefixMerger **ppHead,
227957 PrefixMerger *p
227958){
227959 if( p->iPos>=0 ){
227960 PrefixMerger **pp = ppHead;
227961 while( *pp && p->iPos>(*pp)->iPos ){
227962 pp = &(*pp)->pNext;
227963 }
227964 p->pNext = *pp;
227965 *pp = p;
227966 }
227967}
227968
227969
227970/*
227971** Array aBuf[] contains nBuf doclists. These are all merged in with the
227972** doclist in buffer p1.
227973*/
227974static void fts5MergePrefixLists(
227975 Fts5Index *p, /* FTS5 backend object */
227976 Fts5Buffer *p1, /* First list to merge */
227977 int nBuf, /* Number of buffers in array aBuf[] */
227978 Fts5Buffer *aBuf /* Other lists to merge in */
227979){
227980#define fts5PrefixMergerNextPosition(p) \
227981 sqlite3Fts5PoslistNext64((p)->aPos,(p)->iter.nPoslist,&(p)->iOff,&(p)->iPos)
227982#define FTS5_MERGE_NLIST 16
227983 PrefixMerger aMerger[FTS5_MERGE_NLIST];
227984 PrefixMerger *pHead = 0;
227985 int i;
227986 int nOut = 0;
227987 Fts5Buffer out = {0, 0, 0};
227988 Fts5Buffer tmp = {0, 0, 0};
227989 i64 iLastRowid = 0;
227990
227991 /* Initialize a doclist-iterator for each input buffer. Arrange them in
227992 ** a linked-list starting at pHead in ascending order of rowid. Avoid
227993 ** linking any iterators already at EOF into the linked list at all. */
227994 assert( nBuf+1<=sizeof(aMerger)/sizeof(aMerger[0]) );
227995 memset(s: aMerger, c: 0, n: sizeof(PrefixMerger)*(nBuf+1));
227996 pHead = &aMerger[nBuf];
227997 fts5DoclistIterInit(pBuf: p1, pIter: &pHead->iter);
227998 for(i=0; i<nBuf; i++){
227999 fts5DoclistIterInit(pBuf: &aBuf[i], pIter: &aMerger[i].iter);
228000 fts5PrefixMergerInsertByRowid(ppHead: &pHead, p: &aMerger[i]);
228001 nOut += aBuf[i].n;
228002 }
228003 if( nOut==0 ) return;
228004 nOut += p1->n + 9 + 10*nBuf;
228005
228006 /* The maximum size of the output is equal to the sum of the
228007 ** input sizes + 1 varint (9 bytes). The extra varint is because if the
228008 ** first rowid in one input is a large negative number, and the first in
228009 ** the other a non-negative number, the delta for the non-negative
228010 ** number will be larger on disk than the literal integer value
228011 ** was.
228012 **
228013 ** Or, if the input position-lists are corrupt, then the output might
228014 ** include up to (nBuf+1) extra 10-byte positions created by interpreting -1
228015 ** (the value PoslistNext64() uses for EOF) as a position and appending
228016 ** it to the output. This can happen at most once for each input
228017 ** position-list, hence (nBuf+1) 10 byte paddings. */
228018 if( sqlite3Fts5BufferSize(pRc: &p->rc, pBuf: &out, nByte: nOut) ) return;
228019
228020 while( pHead ){
228021 fts5MergeAppendDocid(&out, iLastRowid, pHead->iter.iRowid);
228022
228023 if( pHead->pNext && iLastRowid==pHead->pNext->iter.iRowid ){
228024 /* Merge data from two or more poslists */
228025 i64 iPrev = 0;
228026 int nTmp = FTS5_DATA_ZERO_PADDING;
228027 int nMerge = 0;
228028 PrefixMerger *pSave = pHead;
228029 PrefixMerger *pThis = 0;
228030 int nTail = 0;
228031
228032 pHead = 0;
228033 while( pSave && pSave->iter.iRowid==iLastRowid ){
228034 PrefixMerger *pNext = pSave->pNext;
228035 pSave->iOff = 0;
228036 pSave->iPos = 0;
228037 pSave->aPos = &pSave->iter.aPoslist[pSave->iter.nSize];
228038 fts5PrefixMergerNextPosition(pSave);
228039 nTmp += pSave->iter.nPoslist + 10;
228040 nMerge++;
228041 fts5PrefixMergerInsertByPosition(ppHead: &pHead, p: pSave);
228042 pSave = pNext;
228043 }
228044
228045 if( pHead==0 || pHead->pNext==0 ){
228046 p->rc = FTS5_CORRUPT;
228047 break;
228048 }
228049
228050 /* See the earlier comment in this function for an explanation of why
228051 ** corrupt input position lists might cause the output to consume
228052 ** at most nMerge*10 bytes of unexpected space. */
228053 if( sqlite3Fts5BufferSize(pRc: &p->rc, pBuf: &tmp, nByte: nTmp+nMerge*10) ){
228054 break;
228055 }
228056 fts5BufferZero(&tmp);
228057
228058 pThis = pHead;
228059 pHead = pThis->pNext;
228060 sqlite3Fts5PoslistSafeAppend(pBuf: &tmp, piPrev: &iPrev, iPos: pThis->iPos);
228061 fts5PrefixMergerNextPosition(pThis);
228062 fts5PrefixMergerInsertByPosition(ppHead: &pHead, p: pThis);
228063
228064 while( pHead->pNext ){
228065 pThis = pHead;
228066 if( pThis->iPos!=iPrev ){
228067 sqlite3Fts5PoslistSafeAppend(pBuf: &tmp, piPrev: &iPrev, iPos: pThis->iPos);
228068 }
228069 fts5PrefixMergerNextPosition(pThis);
228070 pHead = pThis->pNext;
228071 fts5PrefixMergerInsertByPosition(ppHead: &pHead, p: pThis);
228072 }
228073
228074 if( pHead->iPos!=iPrev ){
228075 sqlite3Fts5PoslistSafeAppend(pBuf: &tmp, piPrev: &iPrev, iPos: pHead->iPos);
228076 }
228077 nTail = pHead->iter.nPoslist - pHead->iOff;
228078
228079 /* WRITEPOSLISTSIZE */
228080 assert_nc( tmp.n+nTail<=nTmp );
228081 assert( tmp.n+nTail<=nTmp+nMerge*10 );
228082 if( tmp.n+nTail>nTmp-FTS5_DATA_ZERO_PADDING ){
228083 if( p->rc==SQLITE_OK ) p->rc = FTS5_CORRUPT;
228084 break;
228085 }
228086 fts5BufferSafeAppendVarint(&out, (tmp.n+nTail) * 2);
228087 fts5BufferSafeAppendBlob(&out, tmp.p, tmp.n);
228088 if( nTail>0 ){
228089 fts5BufferSafeAppendBlob(&out, &pHead->aPos[pHead->iOff], nTail);
228090 }
228091
228092 pHead = pSave;
228093 for(i=0; i<nBuf+1; i++){
228094 PrefixMerger *pX = &aMerger[i];
228095 if( pX->iter.aPoslist && pX->iter.iRowid==iLastRowid ){
228096 fts5DoclistIterNext(pIter: &pX->iter);
228097 fts5PrefixMergerInsertByRowid(ppHead: &pHead, p: pX);
228098 }
228099 }
228100
228101 }else{
228102 /* Copy poslist from pHead to output */
228103 PrefixMerger *pThis = pHead;
228104 Fts5DoclistIter *pI = &pThis->iter;
228105 fts5BufferSafeAppendBlob(&out, pI->aPoslist, pI->nPoslist+pI->nSize);
228106 fts5DoclistIterNext(pIter: pI);
228107 pHead = pThis->pNext;
228108 fts5PrefixMergerInsertByRowid(ppHead: &pHead, p: pThis);
228109 }
228110 }
228111
228112 fts5BufferFree(p1);
228113 fts5BufferFree(&tmp);
228114 memset(s: &out.p[out.n], c: 0, FTS5_DATA_ZERO_PADDING);
228115 *p1 = out;
228116}
228117
228118static void fts5SetupPrefixIter(
228119 Fts5Index *p, /* Index to read from */
228120 int bDesc, /* True for "ORDER BY rowid DESC" */
228121 int iIdx, /* Index to scan for data */
228122 u8 *pToken, /* Buffer containing prefix to match */
228123 int nToken, /* Size of buffer pToken in bytes */
228124 Fts5Colset *pColset, /* Restrict matches to these columns */
228125 Fts5Iter **ppIter /* OUT: New iterator */
228126){
228127 Fts5Structure *pStruct;
228128 Fts5Buffer *aBuf;
228129 int nBuf = 32;
228130 int nMerge = 1;
228131
228132 void (*xMerge)(Fts5Index*, Fts5Buffer*, int, Fts5Buffer*);
228133 void (*xAppend)(Fts5Index*, i64, Fts5Iter*, Fts5Buffer*);
228134 if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
228135 xMerge = fts5MergeRowidLists;
228136 xAppend = fts5AppendRowid;
228137 }else{
228138 nMerge = FTS5_MERGE_NLIST-1;
228139 nBuf = nMerge*8; /* Sufficient to merge (16^8)==(2^32) lists */
228140 xMerge = fts5MergePrefixLists;
228141 xAppend = fts5AppendPoslist;
228142 }
228143
228144 aBuf = (Fts5Buffer*)fts5IdxMalloc(p, nByte: sizeof(Fts5Buffer)*nBuf);
228145 pStruct = fts5StructureRead(p);
228146
228147 if( aBuf && pStruct ){
228148 const int flags = FTS5INDEX_QUERY_SCAN
228149 | FTS5INDEX_QUERY_SKIPEMPTY
228150 | FTS5INDEX_QUERY_NOOUTPUT;
228151 int i;
228152 i64 iLastRowid = 0;
228153 Fts5Iter *p1 = 0; /* Iterator used to gather data from index */
228154 Fts5Data *pData;
228155 Fts5Buffer doclist;
228156 int bNewTerm = 1;
228157
228158 memset(s: &doclist, c: 0, n: sizeof(doclist));
228159 if( iIdx!=0 ){
228160 int dummy = 0;
228161 const int f2 = FTS5INDEX_QUERY_SKIPEMPTY|FTS5INDEX_QUERY_NOOUTPUT;
228162 pToken[0] = FTS5_MAIN_PREFIX;
228163 fts5MultiIterNew(p, pStruct, flags: f2, pColset, pTerm: pToken, nTerm: nToken, iLevel: -1, nSegment: 0, ppOut: &p1);
228164 fts5IterSetOutputCb(pRc: &p->rc, pIter: p1);
228165 for(;
228166 fts5MultiIterEof(p, pIter: p1)==0;
228167 fts5MultiIterNext2(p, pIter: p1, pbNewTerm: &dummy)
228168 ){
228169 Fts5SegIter *pSeg = &p1->aSeg[ p1->aFirst[1].iFirst ];
228170 p1->xSetOutputs(p1, pSeg);
228171 if( p1->base.nData ){
228172 xAppend(p, p1->base.iRowid-iLastRowid, p1, &doclist);
228173 iLastRowid = p1->base.iRowid;
228174 }
228175 }
228176 fts5MultiIterFree(pIter: p1);
228177 }
228178
228179 pToken[0] = FTS5_MAIN_PREFIX + iIdx;
228180 fts5MultiIterNew(p, pStruct, flags, pColset, pTerm: pToken, nTerm: nToken, iLevel: -1, nSegment: 0, ppOut: &p1);
228181 fts5IterSetOutputCb(pRc: &p->rc, pIter: p1);
228182 for( /* no-op */ ;
228183 fts5MultiIterEof(p, pIter: p1)==0;
228184 fts5MultiIterNext2(p, pIter: p1, pbNewTerm: &bNewTerm)
228185 ){
228186 Fts5SegIter *pSeg = &p1->aSeg[ p1->aFirst[1].iFirst ];
228187 int nTerm = pSeg->term.n;
228188 const u8 *pTerm = pSeg->term.p;
228189 p1->xSetOutputs(p1, pSeg);
228190
228191 assert_nc( memcmp(pToken, pTerm, MIN(nToken, nTerm))<=0 );
228192 if( bNewTerm ){
228193 if( nTerm<nToken || memcmp(s1: pToken, s2: pTerm, n: nToken) ) break;
228194 }
228195
228196 if( p1->base.nData==0 ) continue;
228197
228198 if( p1->base.iRowid<=iLastRowid && doclist.n>0 ){
228199 for(i=0; p->rc==SQLITE_OK && doclist.n; i++){
228200 int i1 = i*nMerge;
228201 int iStore;
228202 assert( i1+nMerge<=nBuf );
228203 for(iStore=i1; iStore<i1+nMerge; iStore++){
228204 if( aBuf[iStore].n==0 ){
228205 fts5BufferSwap(p1: &doclist, p2: &aBuf[iStore]);
228206 fts5BufferZero(&doclist);
228207 break;
228208 }
228209 }
228210 if( iStore==i1+nMerge ){
228211 xMerge(p, &doclist, nMerge, &aBuf[i1]);
228212 for(iStore=i1; iStore<i1+nMerge; iStore++){
228213 fts5BufferZero(&aBuf[iStore]);
228214 }
228215 }
228216 }
228217 iLastRowid = 0;
228218 }
228219
228220 xAppend(p, p1->base.iRowid-iLastRowid, p1, &doclist);
228221 iLastRowid = p1->base.iRowid;
228222 }
228223
228224 assert( (nBuf%nMerge)==0 );
228225 for(i=0; i<nBuf; i+=nMerge){
228226 int iFree;
228227 if( p->rc==SQLITE_OK ){
228228 xMerge(p, &doclist, nMerge, &aBuf[i]);
228229 }
228230 for(iFree=i; iFree<i+nMerge; iFree++){
228231 fts5BufferFree(&aBuf[iFree]);
228232 }
228233 }
228234 fts5MultiIterFree(pIter: p1);
228235
228236 pData = fts5IdxMalloc(p, nByte: sizeof(Fts5Data)+doclist.n+FTS5_DATA_ZERO_PADDING);
228237 if( pData ){
228238 pData->p = (u8*)&pData[1];
228239 pData->nn = pData->szLeaf = doclist.n;
228240 if( doclist.n ) memcpy(dest: pData->p, src: doclist.p, n: doclist.n);
228241 fts5MultiIterNew2(p, pData, bDesc, ppOut: ppIter);
228242 }
228243 fts5BufferFree(&doclist);
228244 }
228245
228246 fts5StructureRelease(pStruct);
228247 sqlite3_free(p: aBuf);
228248}
228249
228250
228251/*
228252** Indicate that all subsequent calls to sqlite3Fts5IndexWrite() pertain
228253** to the document with rowid iRowid.
228254*/
228255static int sqlite3Fts5IndexBeginWrite(Fts5Index *p, int bDelete, i64 iRowid){
228256 assert( p->rc==SQLITE_OK );
228257
228258 /* Allocate the hash table if it has not already been allocated */
228259 if( p->pHash==0 ){
228260 p->rc = sqlite3Fts5HashNew(pConfig: p->pConfig, ppNew: &p->pHash, pnByte: &p->nPendingData);
228261 }
228262
228263 /* Flush the hash table to disk if required */
228264 if( iRowid<p->iWriteRowid
228265 || (iRowid==p->iWriteRowid && p->bDelete==0)
228266 || (p->nPendingData > p->pConfig->nHashSize)
228267 ){
228268 fts5IndexFlush(p);
228269 }
228270
228271 p->iWriteRowid = iRowid;
228272 p->bDelete = bDelete;
228273 return fts5IndexReturn(p);
228274}
228275
228276/*
228277** Commit data to disk.
228278*/
228279static int sqlite3Fts5IndexSync(Fts5Index *p){
228280 assert( p->rc==SQLITE_OK );
228281 fts5IndexFlush(p);
228282 sqlite3Fts5IndexCloseReader(p);
228283 return fts5IndexReturn(p);
228284}
228285
228286/*
228287** Discard any data stored in the in-memory hash tables. Do not write it
228288** to the database. Additionally, assume that the contents of the %_data
228289** table may have changed on disk. So any in-memory caches of %_data
228290** records must be invalidated.
228291*/
228292static int sqlite3Fts5IndexRollback(Fts5Index *p){
228293 sqlite3Fts5IndexCloseReader(p);
228294 fts5IndexDiscardData(p);
228295 fts5StructureInvalidate(p);
228296 /* assert( p->rc==SQLITE_OK ); */
228297 return SQLITE_OK;
228298}
228299
228300/*
228301** The %_data table is completely empty when this function is called. This
228302** function populates it with the initial structure objects for each index,
228303** and the initial version of the "averages" record (a zero-byte blob).
228304*/
228305static int sqlite3Fts5IndexReinit(Fts5Index *p){
228306 Fts5Structure s;
228307 fts5StructureInvalidate(p);
228308 fts5IndexDiscardData(p);
228309 memset(s: &s, c: 0, n: sizeof(Fts5Structure));
228310 fts5DataWrite(p, FTS5_AVERAGES_ROWID, pData: (const u8*)"", nData: 0);
228311 fts5StructureWrite(p, pStruct: &s);
228312 return fts5IndexReturn(p);
228313}
228314
228315/*
228316** Open a new Fts5Index handle. If the bCreate argument is true, create
228317** and initialize the underlying %_data table.
228318**
228319** If successful, set *pp to point to the new object and return SQLITE_OK.
228320** Otherwise, set *pp to NULL and return an SQLite error code.
228321*/
228322static int sqlite3Fts5IndexOpen(
228323 Fts5Config *pConfig,
228324 int bCreate,
228325 Fts5Index **pp,
228326 char **pzErr
228327){
228328 int rc = SQLITE_OK;
228329 Fts5Index *p; /* New object */
228330
228331 *pp = p = (Fts5Index*)sqlite3Fts5MallocZero(pRc: &rc, nByte: sizeof(Fts5Index));
228332 if( rc==SQLITE_OK ){
228333 p->pConfig = pConfig;
228334 p->nWorkUnit = FTS5_WORK_UNIT;
228335 p->zDataTbl = sqlite3Fts5Mprintf(pRc: &rc, zFmt: "%s_data", pConfig->zName);
228336 if( p->zDataTbl && bCreate ){
228337 rc = sqlite3Fts5CreateTable(
228338 pConfig, "data", "id INTEGER PRIMARY KEY, block BLOB", 0, pzErr
228339 );
228340 if( rc==SQLITE_OK ){
228341 rc = sqlite3Fts5CreateTable(pConfig, "idx",
228342 "segid, term, pgno, PRIMARY KEY(segid, term)",
228343 1, pzErr
228344 );
228345 }
228346 if( rc==SQLITE_OK ){
228347 rc = sqlite3Fts5IndexReinit(p);
228348 }
228349 }
228350 }
228351
228352 assert( rc!=SQLITE_OK || p->rc==SQLITE_OK );
228353 if( rc ){
228354 sqlite3Fts5IndexClose(p);
228355 *pp = 0;
228356 }
228357 return rc;
228358}
228359
228360/*
228361** Close a handle opened by an earlier call to sqlite3Fts5IndexOpen().
228362*/
228363static int sqlite3Fts5IndexClose(Fts5Index *p){
228364 int rc = SQLITE_OK;
228365 if( p ){
228366 assert( p->pReader==0 );
228367 fts5StructureInvalidate(p);
228368 sqlite3_finalize(pStmt: p->pWriter);
228369 sqlite3_finalize(pStmt: p->pDeleter);
228370 sqlite3_finalize(pStmt: p->pIdxWriter);
228371 sqlite3_finalize(pStmt: p->pIdxDeleter);
228372 sqlite3_finalize(pStmt: p->pIdxSelect);
228373 sqlite3_finalize(pStmt: p->pDataVersion);
228374 sqlite3Fts5HashFree(pHash: p->pHash);
228375 sqlite3_free(p: p->zDataTbl);
228376 sqlite3_free(p);
228377 }
228378 return rc;
228379}
228380
228381/*
228382** Argument p points to a buffer containing utf-8 text that is n bytes in
228383** size. Return the number of bytes in the nChar character prefix of the
228384** buffer, or 0 if there are less than nChar characters in total.
228385*/
228386static int sqlite3Fts5IndexCharlenToBytelen(
228387 const char *p,
228388 int nByte,
228389 int nChar
228390){
228391 int n = 0;
228392 int i;
228393 for(i=0; i<nChar; i++){
228394 if( n>=nByte ) return 0; /* Input contains fewer than nChar chars */
228395 if( (unsigned char)p[n++]>=0xc0 ){
228396 if( n>=nByte ) return 0;
228397 while( (p[n] & 0xc0)==0x80 ){
228398 n++;
228399 if( n>=nByte ){
228400 if( i+1==nChar ) break;
228401 return 0;
228402 }
228403 }
228404 }
228405 }
228406 return n;
228407}
228408
228409/*
228410** pIn is a UTF-8 encoded string, nIn bytes in size. Return the number of
228411** unicode characters in the string.
228412*/
228413static int fts5IndexCharlen(const char *pIn, int nIn){
228414 int nChar = 0;
228415 int i = 0;
228416 while( i<nIn ){
228417 if( (unsigned char)pIn[i++]>=0xc0 ){
228418 while( i<nIn && (pIn[i] & 0xc0)==0x80 ) i++;
228419 }
228420 nChar++;
228421 }
228422 return nChar;
228423}
228424
228425/*
228426** Insert or remove data to or from the index. Each time a document is
228427** added to or removed from the index, this function is called one or more
228428** times.
228429**
228430** For an insert, it must be called once for each token in the new document.
228431** If the operation is a delete, it must be called (at least) once for each
228432** unique token in the document with an iCol value less than zero. The iPos
228433** argument is ignored for a delete.
228434*/
228435static int sqlite3Fts5IndexWrite(
228436 Fts5Index *p, /* Index to write to */
228437 int iCol, /* Column token appears in (-ve -> delete) */
228438 int iPos, /* Position of token within column */
228439 const char *pToken, int nToken /* Token to add or remove to or from index */
228440){
228441 int i; /* Used to iterate through indexes */
228442 int rc = SQLITE_OK; /* Return code */
228443 Fts5Config *pConfig = p->pConfig;
228444
228445 assert( p->rc==SQLITE_OK );
228446 assert( (iCol<0)==p->bDelete );
228447
228448 /* Add the entry to the main terms index. */
228449 rc = sqlite3Fts5HashWrite(
228450 pHash: p->pHash, iRowid: p->iWriteRowid, iCol, iPos, FTS5_MAIN_PREFIX, pToken, nToken
228451 );
228452
228453 for(i=0; i<pConfig->nPrefix && rc==SQLITE_OK; i++){
228454 const int nChar = pConfig->aPrefix[i];
228455 int nByte = sqlite3Fts5IndexCharlenToBytelen(p: pToken, nByte: nToken, nChar);
228456 if( nByte ){
228457 rc = sqlite3Fts5HashWrite(pHash: p->pHash,
228458 iRowid: p->iWriteRowid, iCol, iPos, bByte: (char)(FTS5_MAIN_PREFIX+i+1), pToken,
228459 nToken: nByte
228460 );
228461 }
228462 }
228463
228464 return rc;
228465}
228466
228467/*
228468** Open a new iterator to iterate though all rowid that match the
228469** specified token or token prefix.
228470*/
228471static int sqlite3Fts5IndexQuery(
228472 Fts5Index *p, /* FTS index to query */
228473 const char *pToken, int nToken, /* Token (or prefix) to query for */
228474 int flags, /* Mask of FTS5INDEX_QUERY_X flags */
228475 Fts5Colset *pColset, /* Match these columns only */
228476 Fts5IndexIter **ppIter /* OUT: New iterator object */
228477){
228478 Fts5Config *pConfig = p->pConfig;
228479 Fts5Iter *pRet = 0;
228480 Fts5Buffer buf = {0, 0, 0};
228481
228482 /* If the QUERY_SCAN flag is set, all other flags must be clear. */
228483 assert( (flags & FTS5INDEX_QUERY_SCAN)==0 || flags==FTS5INDEX_QUERY_SCAN );
228484
228485 if( sqlite3Fts5BufferSize(pRc: &p->rc, pBuf: &buf, nByte: nToken+1)==0 ){
228486 int iIdx = 0; /* Index to search */
228487 int iPrefixIdx = 0; /* +1 prefix index */
228488 if( nToken ) memcpy(dest: &buf.p[1], src: pToken, n: nToken);
228489
228490 /* Figure out which index to search and set iIdx accordingly. If this
228491 ** is a prefix query for which there is no prefix index, set iIdx to
228492 ** greater than pConfig->nPrefix to indicate that the query will be
228493 ** satisfied by scanning multiple terms in the main index.
228494 **
228495 ** If the QUERY_TEST_NOIDX flag was specified, then this must be a
228496 ** prefix-query. Instead of using a prefix-index (if one exists),
228497 ** evaluate the prefix query using the main FTS index. This is used
228498 ** for internal sanity checking by the integrity-check in debug
228499 ** mode only. */
228500#ifdef SQLITE_DEBUG
228501 if( pConfig->bPrefixIndex==0 || (flags & FTS5INDEX_QUERY_TEST_NOIDX) ){
228502 assert( flags & FTS5INDEX_QUERY_PREFIX );
228503 iIdx = 1+pConfig->nPrefix;
228504 }else
228505#endif
228506 if( flags & FTS5INDEX_QUERY_PREFIX ){
228507 int nChar = fts5IndexCharlen(pIn: pToken, nIn: nToken);
228508 for(iIdx=1; iIdx<=pConfig->nPrefix; iIdx++){
228509 int nIdxChar = pConfig->aPrefix[iIdx-1];
228510 if( nIdxChar==nChar ) break;
228511 if( nIdxChar==nChar+1 ) iPrefixIdx = iIdx;
228512 }
228513 }
228514
228515 if( iIdx<=pConfig->nPrefix ){
228516 /* Straight index lookup */
228517 Fts5Structure *pStruct = fts5StructureRead(p);
228518 buf.p[0] = (u8)(FTS5_MAIN_PREFIX + iIdx);
228519 if( pStruct ){
228520 fts5MultiIterNew(p, pStruct, flags: flags | FTS5INDEX_QUERY_SKIPEMPTY,
228521 pColset, pTerm: buf.p, nTerm: nToken+1, iLevel: -1, nSegment: 0, ppOut: &pRet
228522 );
228523 fts5StructureRelease(pStruct);
228524 }
228525 }else{
228526 /* Scan multiple terms in the main index */
228527 int bDesc = (flags & FTS5INDEX_QUERY_DESC)!=0;
228528 fts5SetupPrefixIter(p, bDesc, iIdx: iPrefixIdx, pToken: buf.p, nToken: nToken+1, pColset,ppIter: &pRet);
228529 if( pRet==0 ){
228530 assert( p->rc!=SQLITE_OK );
228531 }else{
228532 assert( pRet->pColset==0 );
228533 fts5IterSetOutputCb(pRc: &p->rc, pIter: pRet);
228534 if( p->rc==SQLITE_OK ){
228535 Fts5SegIter *pSeg = &pRet->aSeg[pRet->aFirst[1].iFirst];
228536 if( pSeg->pLeaf ) pRet->xSetOutputs(pRet, pSeg);
228537 }
228538 }
228539 }
228540
228541 if( p->rc ){
228542 sqlite3Fts5IterClose((Fts5IndexIter*)pRet);
228543 pRet = 0;
228544 sqlite3Fts5IndexCloseReader(p);
228545 }
228546
228547 *ppIter = (Fts5IndexIter*)pRet;
228548 sqlite3Fts5BufferFree(pBuf: &buf);
228549 }
228550 return fts5IndexReturn(p);
228551}
228552
228553/*
228554** Return true if the iterator passed as the only argument is at EOF.
228555*/
228556/*
228557** Move to the next matching rowid.
228558*/
228559static int sqlite3Fts5IterNext(Fts5IndexIter *pIndexIter){
228560 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
228561 assert( pIter->pIndex->rc==SQLITE_OK );
228562 fts5MultiIterNext(p: pIter->pIndex, pIter, bFrom: 0, iFrom: 0);
228563 return fts5IndexReturn(p: pIter->pIndex);
228564}
228565
228566/*
228567** Move to the next matching term/rowid. Used by the fts5vocab module.
228568*/
228569static int sqlite3Fts5IterNextScan(Fts5IndexIter *pIndexIter){
228570 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
228571 Fts5Index *p = pIter->pIndex;
228572
228573 assert( pIter->pIndex->rc==SQLITE_OK );
228574
228575 fts5MultiIterNext(p, pIter, bFrom: 0, iFrom: 0);
228576 if( p->rc==SQLITE_OK ){
228577 Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
228578 if( pSeg->pLeaf && pSeg->term.p[0]!=FTS5_MAIN_PREFIX ){
228579 fts5DataRelease(pData: pSeg->pLeaf);
228580 pSeg->pLeaf = 0;
228581 pIter->base.bEof = 1;
228582 }
228583 }
228584
228585 return fts5IndexReturn(p: pIter->pIndex);
228586}
228587
228588/*
228589** Move to the next matching rowid that occurs at or after iMatch. The
228590** definition of "at or after" depends on whether this iterator iterates
228591** in ascending or descending rowid order.
228592*/
228593static int sqlite3Fts5IterNextFrom(Fts5IndexIter *pIndexIter, i64 iMatch){
228594 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
228595 fts5MultiIterNextFrom(p: pIter->pIndex, pIter, iMatch);
228596 return fts5IndexReturn(p: pIter->pIndex);
228597}
228598
228599/*
228600** Return the current term.
228601*/
228602static const char *sqlite3Fts5IterTerm(Fts5IndexIter *pIndexIter, int *pn){
228603 int n;
228604 const char *z = (const char*)fts5MultiIterTerm(pIter: (Fts5Iter*)pIndexIter, pn: &n);
228605 assert_nc( z || n<=1 );
228606 *pn = n-1;
228607 return (z ? &z[1] : 0);
228608}
228609
228610/*
228611** Close an iterator opened by an earlier call to sqlite3Fts5IndexQuery().
228612*/
228613static void sqlite3Fts5IterClose(Fts5IndexIter *pIndexIter){
228614 if( pIndexIter ){
228615 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
228616 Fts5Index *pIndex = pIter->pIndex;
228617 fts5MultiIterFree(pIter);
228618 sqlite3Fts5IndexCloseReader(p: pIndex);
228619 }
228620}
228621
228622/*
228623** Read and decode the "averages" record from the database.
228624**
228625** Parameter anSize must point to an array of size nCol, where nCol is
228626** the number of user defined columns in the FTS table.
228627*/
228628static int sqlite3Fts5IndexGetAverages(Fts5Index *p, i64 *pnRow, i64 *anSize){
228629 int nCol = p->pConfig->nCol;
228630 Fts5Data *pData;
228631
228632 *pnRow = 0;
228633 memset(s: anSize, c: 0, n: sizeof(i64) * nCol);
228634 pData = fts5DataRead(p, FTS5_AVERAGES_ROWID);
228635 if( p->rc==SQLITE_OK && pData->nn ){
228636 int i = 0;
228637 int iCol;
228638 i += fts5GetVarint(&pData->p[i], (u64*)pnRow);
228639 for(iCol=0; i<pData->nn && iCol<nCol; iCol++){
228640 i += fts5GetVarint(&pData->p[i], (u64*)&anSize[iCol]);
228641 }
228642 }
228643
228644 fts5DataRelease(pData);
228645 return fts5IndexReturn(p);
228646}
228647
228648/*
228649** Replace the current "averages" record with the contents of the buffer
228650** supplied as the second argument.
228651*/
228652static int sqlite3Fts5IndexSetAverages(Fts5Index *p, const u8 *pData, int nData){
228653 assert( p->rc==SQLITE_OK );
228654 fts5DataWrite(p, FTS5_AVERAGES_ROWID, pData, nData);
228655 return fts5IndexReturn(p);
228656}
228657
228658/*
228659** Return the total number of blocks this module has read from the %_data
228660** table since it was created.
228661*/
228662static int sqlite3Fts5IndexReads(Fts5Index *p){
228663 return p->nRead;
228664}
228665
228666/*
228667** Set the 32-bit cookie value stored at the start of all structure
228668** records to the value passed as the second argument.
228669**
228670** Return SQLITE_OK if successful, or an SQLite error code if an error
228671** occurs.
228672*/
228673static int sqlite3Fts5IndexSetCookie(Fts5Index *p, int iNew){
228674 int rc; /* Return code */
228675 Fts5Config *pConfig = p->pConfig; /* Configuration object */
228676 u8 aCookie[4]; /* Binary representation of iNew */
228677 sqlite3_blob *pBlob = 0;
228678
228679 assert( p->rc==SQLITE_OK );
228680 sqlite3Fts5Put32(aBuf: aCookie, iVal: iNew);
228681
228682 rc = sqlite3_blob_open(db: pConfig->db, zDb: pConfig->zDb, zTable: p->zDataTbl,
228683 zColumn: "block", FTS5_STRUCTURE_ROWID, wrFlag: 1, ppBlob: &pBlob
228684 );
228685 if( rc==SQLITE_OK ){
228686 sqlite3_blob_write(pBlob, z: aCookie, n: 4, iOffset: 0);
228687 rc = sqlite3_blob_close(pBlob);
228688 }
228689
228690 return rc;
228691}
228692
228693static int sqlite3Fts5IndexLoadConfig(Fts5Index *p){
228694 Fts5Structure *pStruct;
228695 pStruct = fts5StructureRead(p);
228696 fts5StructureRelease(pStruct);
228697 return fts5IndexReturn(p);
228698}
228699
228700
228701/*************************************************************************
228702**************************************************************************
228703** Below this point is the implementation of the integrity-check
228704** functionality.
228705*/
228706
228707/*
228708** Return a simple checksum value based on the arguments.
228709*/
228710static u64 sqlite3Fts5IndexEntryCksum(
228711 i64 iRowid,
228712 int iCol,
228713 int iPos,
228714 int iIdx,
228715 const char *pTerm,
228716 int nTerm
228717){
228718 int i;
228719 u64 ret = iRowid;
228720 ret += (ret<<3) + iCol;
228721 ret += (ret<<3) + iPos;
228722 if( iIdx>=0 ) ret += (ret<<3) + (FTS5_MAIN_PREFIX + iIdx);
228723 for(i=0; i<nTerm; i++) ret += (ret<<3) + pTerm[i];
228724 return ret;
228725}
228726
228727#ifdef SQLITE_DEBUG
228728/*
228729** This function is purely an internal test. It does not contribute to
228730** FTS functionality, or even the integrity-check, in any way.
228731**
228732** Instead, it tests that the same set of pgno/rowid combinations are
228733** visited regardless of whether the doclist-index identified by parameters
228734** iSegid/iLeaf is iterated in forwards or reverse order.
228735*/
228736static void fts5TestDlidxReverse(
228737 Fts5Index *p,
228738 int iSegid, /* Segment id to load from */
228739 int iLeaf /* Load doclist-index for this leaf */
228740){
228741 Fts5DlidxIter *pDlidx = 0;
228742 u64 cksum1 = 13;
228743 u64 cksum2 = 13;
228744
228745 for(pDlidx=fts5DlidxIterInit(p, 0, iSegid, iLeaf);
228746 fts5DlidxIterEof(p, pDlidx)==0;
228747 fts5DlidxIterNext(p, pDlidx)
228748 ){
228749 i64 iRowid = fts5DlidxIterRowid(pDlidx);
228750 int pgno = fts5DlidxIterPgno(pDlidx);
228751 assert( pgno>iLeaf );
228752 cksum1 += iRowid + ((i64)pgno<<32);
228753 }
228754 fts5DlidxIterFree(pDlidx);
228755 pDlidx = 0;
228756
228757 for(pDlidx=fts5DlidxIterInit(p, 1, iSegid, iLeaf);
228758 fts5DlidxIterEof(p, pDlidx)==0;
228759 fts5DlidxIterPrev(p, pDlidx)
228760 ){
228761 i64 iRowid = fts5DlidxIterRowid(pDlidx);
228762 int pgno = fts5DlidxIterPgno(pDlidx);
228763 assert( fts5DlidxIterPgno(pDlidx)>iLeaf );
228764 cksum2 += iRowid + ((i64)pgno<<32);
228765 }
228766 fts5DlidxIterFree(pDlidx);
228767 pDlidx = 0;
228768
228769 if( p->rc==SQLITE_OK && cksum1!=cksum2 ) p->rc = FTS5_CORRUPT;
228770}
228771
228772static int fts5QueryCksum(
228773 Fts5Index *p, /* Fts5 index object */
228774 int iIdx,
228775 const char *z, /* Index key to query for */
228776 int n, /* Size of index key in bytes */
228777 int flags, /* Flags for Fts5IndexQuery */
228778 u64 *pCksum /* IN/OUT: Checksum value */
228779){
228780 int eDetail = p->pConfig->eDetail;
228781 u64 cksum = *pCksum;
228782 Fts5IndexIter *pIter = 0;
228783 int rc = sqlite3Fts5IndexQuery(p, z, n, flags, 0, &pIter);
228784
228785 while( rc==SQLITE_OK && ALWAYS(pIter!=0) && 0==sqlite3Fts5IterEof(pIter) ){
228786 i64 rowid = pIter->iRowid;
228787
228788 if( eDetail==FTS5_DETAIL_NONE ){
228789 cksum ^= sqlite3Fts5IndexEntryCksum(rowid, 0, 0, iIdx, z, n);
228790 }else{
228791 Fts5PoslistReader sReader;
228792 for(sqlite3Fts5PoslistReaderInit(pIter->pData, pIter->nData, &sReader);
228793 sReader.bEof==0;
228794 sqlite3Fts5PoslistReaderNext(&sReader)
228795 ){
228796 int iCol = FTS5_POS2COLUMN(sReader.iPos);
228797 int iOff = FTS5_POS2OFFSET(sReader.iPos);
228798 cksum ^= sqlite3Fts5IndexEntryCksum(rowid, iCol, iOff, iIdx, z, n);
228799 }
228800 }
228801 if( rc==SQLITE_OK ){
228802 rc = sqlite3Fts5IterNext(pIter);
228803 }
228804 }
228805 sqlite3Fts5IterClose(pIter);
228806
228807 *pCksum = cksum;
228808 return rc;
228809}
228810
228811/*
228812** Check if buffer z[], size n bytes, contains as series of valid utf-8
228813** encoded codepoints. If so, return 0. Otherwise, if the buffer does not
228814** contain valid utf-8, return non-zero.
228815*/
228816static int fts5TestUtf8(const char *z, int n){
228817 int i = 0;
228818 assert_nc( n>0 );
228819 while( i<n ){
228820 if( (z[i] & 0x80)==0x00 ){
228821 i++;
228822 }else
228823 if( (z[i] & 0xE0)==0xC0 ){
228824 if( i+1>=n || (z[i+1] & 0xC0)!=0x80 ) return 1;
228825 i += 2;
228826 }else
228827 if( (z[i] & 0xF0)==0xE0 ){
228828 if( i+2>=n || (z[i+1] & 0xC0)!=0x80 || (z[i+2] & 0xC0)!=0x80 ) return 1;
228829 i += 3;
228830 }else
228831 if( (z[i] & 0xF8)==0xF0 ){
228832 if( i+3>=n || (z[i+1] & 0xC0)!=0x80 || (z[i+2] & 0xC0)!=0x80 ) return 1;
228833 if( (z[i+2] & 0xC0)!=0x80 ) return 1;
228834 i += 3;
228835 }else{
228836 return 1;
228837 }
228838 }
228839
228840 return 0;
228841}
228842
228843/*
228844** This function is also purely an internal test. It does not contribute to
228845** FTS functionality, or even the integrity-check, in any way.
228846*/
228847static void fts5TestTerm(
228848 Fts5Index *p,
228849 Fts5Buffer *pPrev, /* Previous term */
228850 const char *z, int n, /* Possibly new term to test */
228851 u64 expected,
228852 u64 *pCksum
228853){
228854 int rc = p->rc;
228855 if( pPrev->n==0 ){
228856 fts5BufferSet(&rc, pPrev, n, (const u8*)z);
228857 }else
228858 if( rc==SQLITE_OK && (pPrev->n!=n || memcmp(pPrev->p, z, n)) ){
228859 u64 cksum3 = *pCksum;
228860 const char *zTerm = (const char*)&pPrev->p[1]; /* term sans prefix-byte */
228861 int nTerm = pPrev->n-1; /* Size of zTerm in bytes */
228862 int iIdx = (pPrev->p[0] - FTS5_MAIN_PREFIX);
228863 int flags = (iIdx==0 ? 0 : FTS5INDEX_QUERY_PREFIX);
228864 u64 ck1 = 0;
228865 u64 ck2 = 0;
228866
228867 /* Check that the results returned for ASC and DESC queries are
228868 ** the same. If not, call this corruption. */
228869 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, flags, &ck1);
228870 if( rc==SQLITE_OK ){
228871 int f = flags|FTS5INDEX_QUERY_DESC;
228872 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
228873 }
228874 if( rc==SQLITE_OK && ck1!=ck2 ) rc = FTS5_CORRUPT;
228875
228876 /* If this is a prefix query, check that the results returned if the
228877 ** the index is disabled are the same. In both ASC and DESC order.
228878 **
228879 ** This check may only be performed if the hash table is empty. This
228880 ** is because the hash table only supports a single scan query at
228881 ** a time, and the multi-iter loop from which this function is called
228882 ** is already performing such a scan.
228883 **
228884 ** Also only do this if buffer zTerm contains nTerm bytes of valid
228885 ** utf-8. Otherwise, the last part of the buffer contents might contain
228886 ** a non-utf-8 sequence that happens to be a prefix of a valid utf-8
228887 ** character stored in the main fts index, which will cause the
228888 ** test to fail. */
228889 if( p->nPendingData==0 && 0==fts5TestUtf8(zTerm, nTerm) ){
228890 if( iIdx>0 && rc==SQLITE_OK ){
228891 int f = flags|FTS5INDEX_QUERY_TEST_NOIDX;
228892 ck2 = 0;
228893 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
228894 if( rc==SQLITE_OK && ck1!=ck2 ) rc = FTS5_CORRUPT;
228895 }
228896 if( iIdx>0 && rc==SQLITE_OK ){
228897 int f = flags|FTS5INDEX_QUERY_TEST_NOIDX|FTS5INDEX_QUERY_DESC;
228898 ck2 = 0;
228899 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
228900 if( rc==SQLITE_OK && ck1!=ck2 ) rc = FTS5_CORRUPT;
228901 }
228902 }
228903
228904 cksum3 ^= ck1;
228905 fts5BufferSet(&rc, pPrev, n, (const u8*)z);
228906
228907 if( rc==SQLITE_OK && cksum3!=expected ){
228908 rc = FTS5_CORRUPT;
228909 }
228910 *pCksum = cksum3;
228911 }
228912 p->rc = rc;
228913}
228914
228915#else
228916# define fts5TestDlidxReverse(x,y,z)
228917# define fts5TestTerm(u,v,w,x,y,z)
228918#endif
228919
228920/*
228921** Check that:
228922**
228923** 1) All leaves of pSeg between iFirst and iLast (inclusive) exist and
228924** contain zero terms.
228925** 2) All leaves of pSeg between iNoRowid and iLast (inclusive) exist and
228926** contain zero rowids.
228927*/
228928static void fts5IndexIntegrityCheckEmpty(
228929 Fts5Index *p,
228930 Fts5StructureSegment *pSeg, /* Segment to check internal consistency */
228931 int iFirst,
228932 int iNoRowid,
228933 int iLast
228934){
228935 int i;
228936
228937 /* Now check that the iter.nEmpty leaves following the current leaf
228938 ** (a) exist and (b) contain no terms. */
228939 for(i=iFirst; p->rc==SQLITE_OK && i<=iLast; i++){
228940 Fts5Data *pLeaf = fts5DataRead(p, FTS5_SEGMENT_ROWID(pSeg->iSegid, i));
228941 if( pLeaf ){
228942 if( !fts5LeafIsTermless(pLeaf) ) p->rc = FTS5_CORRUPT;
228943 if( i>=iNoRowid && 0!=fts5LeafFirstRowidOff(pLeaf) ) p->rc = FTS5_CORRUPT;
228944 }
228945 fts5DataRelease(pData: pLeaf);
228946 }
228947}
228948
228949static void fts5IntegrityCheckPgidx(Fts5Index *p, Fts5Data *pLeaf){
228950 int iTermOff = 0;
228951 int ii;
228952
228953 Fts5Buffer buf1 = {0,0,0};
228954 Fts5Buffer buf2 = {0,0,0};
228955
228956 ii = pLeaf->szLeaf;
228957 while( ii<pLeaf->nn && p->rc==SQLITE_OK ){
228958 int res;
228959 int iOff;
228960 int nIncr;
228961
228962 ii += fts5GetVarint32(&pLeaf->p[ii], nIncr);
228963 iTermOff += nIncr;
228964 iOff = iTermOff;
228965
228966 if( iOff>=pLeaf->szLeaf ){
228967 p->rc = FTS5_CORRUPT;
228968 }else if( iTermOff==nIncr ){
228969 int nByte;
228970 iOff += fts5GetVarint32(&pLeaf->p[iOff], nByte);
228971 if( (iOff+nByte)>pLeaf->szLeaf ){
228972 p->rc = FTS5_CORRUPT;
228973 }else{
228974 fts5BufferSet(&p->rc, &buf1, nByte, &pLeaf->p[iOff]);
228975 }
228976 }else{
228977 int nKeep, nByte;
228978 iOff += fts5GetVarint32(&pLeaf->p[iOff], nKeep);
228979 iOff += fts5GetVarint32(&pLeaf->p[iOff], nByte);
228980 if( nKeep>buf1.n || (iOff+nByte)>pLeaf->szLeaf ){
228981 p->rc = FTS5_CORRUPT;
228982 }else{
228983 buf1.n = nKeep;
228984 fts5BufferAppendBlob(&p->rc, &buf1, nByte, &pLeaf->p[iOff]);
228985 }
228986
228987 if( p->rc==SQLITE_OK ){
228988 res = fts5BufferCompare(pLeft: &buf1, pRight: &buf2);
228989 if( res<=0 ) p->rc = FTS5_CORRUPT;
228990 }
228991 }
228992 fts5BufferSet(&p->rc, &buf2, buf1.n, buf1.p);
228993 }
228994
228995 fts5BufferFree(&buf1);
228996 fts5BufferFree(&buf2);
228997}
228998
228999static void fts5IndexIntegrityCheckSegment(
229000 Fts5Index *p, /* FTS5 backend object */
229001 Fts5StructureSegment *pSeg /* Segment to check internal consistency */
229002){
229003 Fts5Config *pConfig = p->pConfig;
229004 sqlite3_stmt *pStmt = 0;
229005 int rc2;
229006 int iIdxPrevLeaf = pSeg->pgnoFirst-1;
229007 int iDlidxPrevLeaf = pSeg->pgnoLast;
229008
229009 if( pSeg->pgnoFirst==0 ) return;
229010
229011 fts5IndexPrepareStmt(p, ppStmt: &pStmt, zSql: sqlite3_mprintf(
229012 zFormat: "SELECT segid, term, (pgno>>1), (pgno&1) FROM %Q.'%q_idx' WHERE segid=%d "
229013 "ORDER BY 1, 2",
229014 pConfig->zDb, pConfig->zName, pSeg->iSegid
229015 ));
229016
229017 /* Iterate through the b-tree hierarchy. */
229018 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
229019 i64 iRow; /* Rowid for this leaf */
229020 Fts5Data *pLeaf; /* Data for this leaf */
229021
229022 const char *zIdxTerm = (const char*)sqlite3_column_blob(pStmt, i: 1);
229023 int nIdxTerm = sqlite3_column_bytes(pStmt, i: 1);
229024 int iIdxLeaf = sqlite3_column_int(pStmt, i: 2);
229025 int bIdxDlidx = sqlite3_column_int(pStmt, i: 3);
229026
229027 /* If the leaf in question has already been trimmed from the segment,
229028 ** ignore this b-tree entry. Otherwise, load it into memory. */
229029 if( iIdxLeaf<pSeg->pgnoFirst ) continue;
229030 iRow = FTS5_SEGMENT_ROWID(pSeg->iSegid, iIdxLeaf);
229031 pLeaf = fts5LeafRead(p, iRowid: iRow);
229032 if( pLeaf==0 ) break;
229033
229034 /* Check that the leaf contains at least one term, and that it is equal
229035 ** to or larger than the split-key in zIdxTerm. Also check that if there
229036 ** is also a rowid pointer within the leaf page header, it points to a
229037 ** location before the term. */
229038 if( pLeaf->nn<=pLeaf->szLeaf ){
229039 p->rc = FTS5_CORRUPT;
229040 }else{
229041 int iOff; /* Offset of first term on leaf */
229042 int iRowidOff; /* Offset of first rowid on leaf */
229043 int nTerm; /* Size of term on leaf in bytes */
229044 int res; /* Comparison of term and split-key */
229045
229046 iOff = fts5LeafFirstTermOff(pLeaf);
229047 iRowidOff = fts5LeafFirstRowidOff(pLeaf);
229048 if( iRowidOff>=iOff || iOff>=pLeaf->szLeaf ){
229049 p->rc = FTS5_CORRUPT;
229050 }else{
229051 iOff += fts5GetVarint32(&pLeaf->p[iOff], nTerm);
229052 res = fts5Memcmp(&pLeaf->p[iOff], zIdxTerm, MIN(nTerm, nIdxTerm));
229053 if( res==0 ) res = nTerm - nIdxTerm;
229054 if( res<0 ) p->rc = FTS5_CORRUPT;
229055 }
229056
229057 fts5IntegrityCheckPgidx(p, pLeaf);
229058 }
229059 fts5DataRelease(pData: pLeaf);
229060 if( p->rc ) break;
229061
229062 /* Now check that the iter.nEmpty leaves following the current leaf
229063 ** (a) exist and (b) contain no terms. */
229064 fts5IndexIntegrityCheckEmpty(
229065 p, pSeg, iFirst: iIdxPrevLeaf+1, iNoRowid: iDlidxPrevLeaf+1, iLast: iIdxLeaf-1
229066 );
229067 if( p->rc ) break;
229068
229069 /* If there is a doclist-index, check that it looks right. */
229070 if( bIdxDlidx ){
229071 Fts5DlidxIter *pDlidx = 0; /* For iterating through doclist index */
229072 int iPrevLeaf = iIdxLeaf;
229073 int iSegid = pSeg->iSegid;
229074 int iPg = 0;
229075 i64 iKey;
229076
229077 for(pDlidx=fts5DlidxIterInit(p, bRev: 0, iSegid, iLeafPg: iIdxLeaf);
229078 fts5DlidxIterEof(p, pIter: pDlidx)==0;
229079 fts5DlidxIterNext(p, pIter: pDlidx)
229080 ){
229081
229082 /* Check any rowid-less pages that occur before the current leaf. */
229083 for(iPg=iPrevLeaf+1; iPg<fts5DlidxIterPgno(pIter: pDlidx); iPg++){
229084 iKey = FTS5_SEGMENT_ROWID(iSegid, iPg);
229085 pLeaf = fts5DataRead(p, iRowid: iKey);
229086 if( pLeaf ){
229087 if( fts5LeafFirstRowidOff(pLeaf)!=0 ) p->rc = FTS5_CORRUPT;
229088 fts5DataRelease(pData: pLeaf);
229089 }
229090 }
229091 iPrevLeaf = fts5DlidxIterPgno(pIter: pDlidx);
229092
229093 /* Check that the leaf page indicated by the iterator really does
229094 ** contain the rowid suggested by the same. */
229095 iKey = FTS5_SEGMENT_ROWID(iSegid, iPrevLeaf);
229096 pLeaf = fts5DataRead(p, iRowid: iKey);
229097 if( pLeaf ){
229098 i64 iRowid;
229099 int iRowidOff = fts5LeafFirstRowidOff(pLeaf);
229100 ASSERT_SZLEAF_OK(pLeaf);
229101 if( iRowidOff>=pLeaf->szLeaf ){
229102 p->rc = FTS5_CORRUPT;
229103 }else{
229104 fts5GetVarint(&pLeaf->p[iRowidOff], (u64*)&iRowid);
229105 if( iRowid!=fts5DlidxIterRowid(pIter: pDlidx) ) p->rc = FTS5_CORRUPT;
229106 }
229107 fts5DataRelease(pData: pLeaf);
229108 }
229109 }
229110
229111 iDlidxPrevLeaf = iPg;
229112 fts5DlidxIterFree(pIter: pDlidx);
229113 fts5TestDlidxReverse(p, iSegid, iIdxLeaf);
229114 }else{
229115 iDlidxPrevLeaf = pSeg->pgnoLast;
229116 /* TODO: Check there is no doclist index */
229117 }
229118
229119 iIdxPrevLeaf = iIdxLeaf;
229120 }
229121
229122 rc2 = sqlite3_finalize(pStmt);
229123 if( p->rc==SQLITE_OK ) p->rc = rc2;
229124
229125 /* Page iter.iLeaf must now be the rightmost leaf-page in the segment */
229126#if 0
229127 if( p->rc==SQLITE_OK && iter.iLeaf!=pSeg->pgnoLast ){
229128 p->rc = FTS5_CORRUPT;
229129 }
229130#endif
229131}
229132
229133
229134/*
229135** Run internal checks to ensure that the FTS index (a) is internally
229136** consistent and (b) contains entries for which the XOR of the checksums
229137** as calculated by sqlite3Fts5IndexEntryCksum() is cksum.
229138**
229139** Return SQLITE_CORRUPT if any of the internal checks fail, or if the
229140** checksum does not match. Return SQLITE_OK if all checks pass without
229141** error, or some other SQLite error code if another error (e.g. OOM)
229142** occurs.
229143*/
229144static int sqlite3Fts5IndexIntegrityCheck(Fts5Index *p, u64 cksum, int bUseCksum){
229145 int eDetail = p->pConfig->eDetail;
229146 u64 cksum2 = 0; /* Checksum based on contents of indexes */
229147 Fts5Buffer poslist = {0,0,0}; /* Buffer used to hold a poslist */
229148 Fts5Iter *pIter; /* Used to iterate through entire index */
229149 Fts5Structure *pStruct; /* Index structure */
229150 int iLvl, iSeg;
229151
229152#ifdef SQLITE_DEBUG
229153 /* Used by extra internal tests only run if NDEBUG is not defined */
229154 u64 cksum3 = 0; /* Checksum based on contents of indexes */
229155 Fts5Buffer term = {0,0,0}; /* Buffer used to hold most recent term */
229156#endif
229157 const int flags = FTS5INDEX_QUERY_NOOUTPUT;
229158
229159 /* Load the FTS index structure */
229160 pStruct = fts5StructureRead(p);
229161 if( pStruct==0 ){
229162 assert( p->rc!=SQLITE_OK );
229163 return fts5IndexReturn(p);
229164 }
229165
229166 /* Check that the internal nodes of each segment match the leaves */
229167 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
229168 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
229169 Fts5StructureSegment *pSeg = &pStruct->aLevel[iLvl].aSeg[iSeg];
229170 fts5IndexIntegrityCheckSegment(p, pSeg);
229171 }
229172 }
229173
229174 /* The cksum argument passed to this function is a checksum calculated
229175 ** based on all expected entries in the FTS index (including prefix index
229176 ** entries). This block checks that a checksum calculated based on the
229177 ** actual contents of FTS index is identical.
229178 **
229179 ** Two versions of the same checksum are calculated. The first (stack
229180 ** variable cksum2) based on entries extracted from the full-text index
229181 ** while doing a linear scan of each individual index in turn.
229182 **
229183 ** As each term visited by the linear scans, a separate query for the
229184 ** same term is performed. cksum3 is calculated based on the entries
229185 ** extracted by these queries.
229186 */
229187 for(fts5MultiIterNew(p, pStruct, flags, pColset: 0, pTerm: 0, nTerm: 0, iLevel: -1, nSegment: 0, ppOut: &pIter);
229188 fts5MultiIterEof(p, pIter)==0;
229189 fts5MultiIterNext(p, pIter, bFrom: 0, iFrom: 0)
229190 ){
229191 int n; /* Size of term in bytes */
229192 i64 iPos = 0; /* Position read from poslist */
229193 int iOff = 0; /* Offset within poslist */
229194 i64 iRowid = fts5MultiIterRowid(pIter);
229195 char *z = (char*)fts5MultiIterTerm(pIter, pn: &n);
229196
229197 /* If this is a new term, query for it. Update cksum3 with the results. */
229198 fts5TestTerm(p, &term, z, n, cksum2, &cksum3);
229199
229200 if( eDetail==FTS5_DETAIL_NONE ){
229201 if( 0==fts5MultiIterIsEmpty(p, pIter) ){
229202 cksum2 ^= sqlite3Fts5IndexEntryCksum(iRowid, iCol: 0, iPos: 0, iIdx: -1, pTerm: z, nTerm: n);
229203 }
229204 }else{
229205 poslist.n = 0;
229206 fts5SegiterPoslist(p, pSeg: &pIter->aSeg[pIter->aFirst[1].iFirst], pColset: 0, pBuf: &poslist);
229207 fts5BufferAppendBlob(&p->rc, &poslist, 4, (const u8*)"\0\0\0\0");
229208 while( 0==sqlite3Fts5PoslistNext64(a: poslist.p, n: poslist.n, pi: &iOff, piOff: &iPos) ){
229209 int iCol = FTS5_POS2COLUMN(iPos);
229210 int iTokOff = FTS5_POS2OFFSET(iPos);
229211 cksum2 ^= sqlite3Fts5IndexEntryCksum(iRowid, iCol, iPos: iTokOff, iIdx: -1, pTerm: z, nTerm: n);
229212 }
229213 }
229214 }
229215 fts5TestTerm(p, &term, 0, 0, cksum2, &cksum3);
229216
229217 fts5MultiIterFree(pIter);
229218 if( p->rc==SQLITE_OK && bUseCksum && cksum!=cksum2 ) p->rc = FTS5_CORRUPT;
229219
229220 fts5StructureRelease(pStruct);
229221#ifdef SQLITE_DEBUG
229222 fts5BufferFree(&term);
229223#endif
229224 fts5BufferFree(&poslist);
229225 return fts5IndexReturn(p);
229226}
229227
229228/*************************************************************************
229229**************************************************************************
229230** Below this point is the implementation of the fts5_decode() scalar
229231** function only.
229232*/
229233
229234#ifdef SQLITE_TEST
229235/*
229236** Decode a segment-data rowid from the %_data table. This function is
229237** the opposite of macro FTS5_SEGMENT_ROWID().
229238*/
229239static void fts5DecodeRowid(
229240 i64 iRowid, /* Rowid from %_data table */
229241 int *piSegid, /* OUT: Segment id */
229242 int *pbDlidx, /* OUT: Dlidx flag */
229243 int *piHeight, /* OUT: Height */
229244 int *piPgno /* OUT: Page number */
229245){
229246 *piPgno = (int)(iRowid & (((i64)1 << FTS5_DATA_PAGE_B) - 1));
229247 iRowid >>= FTS5_DATA_PAGE_B;
229248
229249 *piHeight = (int)(iRowid & (((i64)1 << FTS5_DATA_HEIGHT_B) - 1));
229250 iRowid >>= FTS5_DATA_HEIGHT_B;
229251
229252 *pbDlidx = (int)(iRowid & 0x0001);
229253 iRowid >>= FTS5_DATA_DLI_B;
229254
229255 *piSegid = (int)(iRowid & (((i64)1 << FTS5_DATA_ID_B) - 1));
229256}
229257#endif /* SQLITE_TEST */
229258
229259#ifdef SQLITE_TEST
229260static void fts5DebugRowid(int *pRc, Fts5Buffer *pBuf, i64 iKey){
229261 int iSegid, iHeight, iPgno, bDlidx; /* Rowid compenents */
229262 fts5DecodeRowid(iKey, &iSegid, &bDlidx, &iHeight, &iPgno);
229263
229264 if( iSegid==0 ){
229265 if( iKey==FTS5_AVERAGES_ROWID ){
229266 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{averages} ");
229267 }else{
229268 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{structure}");
229269 }
229270 }
229271 else{
229272 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{%ssegid=%d h=%d pgno=%d}",
229273 bDlidx ? "dlidx " : "", iSegid, iHeight, iPgno
229274 );
229275 }
229276}
229277#endif /* SQLITE_TEST */
229278
229279#ifdef SQLITE_TEST
229280static void fts5DebugStructure(
229281 int *pRc, /* IN/OUT: error code */
229282 Fts5Buffer *pBuf,
229283 Fts5Structure *p
229284){
229285 int iLvl, iSeg; /* Iterate through levels, segments */
229286
229287 for(iLvl=0; iLvl<p->nLevel; iLvl++){
229288 Fts5StructureLevel *pLvl = &p->aLevel[iLvl];
229289 sqlite3Fts5BufferAppendPrintf(pRc, pBuf,
229290 " {lvl=%d nMerge=%d nSeg=%d", iLvl, pLvl->nMerge, pLvl->nSeg
229291 );
229292 for(iSeg=0; iSeg<pLvl->nSeg; iSeg++){
229293 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
229294 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " {id=%d leaves=%d..%d}",
229295 pSeg->iSegid, pSeg->pgnoFirst, pSeg->pgnoLast
229296 );
229297 }
229298 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "}");
229299 }
229300}
229301#endif /* SQLITE_TEST */
229302
229303#ifdef SQLITE_TEST
229304/*
229305** This is part of the fts5_decode() debugging aid.
229306**
229307** Arguments pBlob/nBlob contain a serialized Fts5Structure object. This
229308** function appends a human-readable representation of the same object
229309** to the buffer passed as the second argument.
229310*/
229311static void fts5DecodeStructure(
229312 int *pRc, /* IN/OUT: error code */
229313 Fts5Buffer *pBuf,
229314 const u8 *pBlob, int nBlob
229315){
229316 int rc; /* Return code */
229317 Fts5Structure *p = 0; /* Decoded structure object */
229318
229319 rc = fts5StructureDecode(pBlob, nBlob, 0, &p);
229320 if( rc!=SQLITE_OK ){
229321 *pRc = rc;
229322 return;
229323 }
229324
229325 fts5DebugStructure(pRc, pBuf, p);
229326 fts5StructureRelease(p);
229327}
229328#endif /* SQLITE_TEST */
229329
229330#ifdef SQLITE_TEST
229331/*
229332** This is part of the fts5_decode() debugging aid.
229333**
229334** Arguments pBlob/nBlob contain an "averages" record. This function
229335** appends a human-readable representation of record to the buffer passed
229336** as the second argument.
229337*/
229338static void fts5DecodeAverages(
229339 int *pRc, /* IN/OUT: error code */
229340 Fts5Buffer *pBuf,
229341 const u8 *pBlob, int nBlob
229342){
229343 int i = 0;
229344 const char *zSpace = "";
229345
229346 while( i<nBlob ){
229347 u64 iVal;
229348 i += sqlite3Fts5GetVarint(&pBlob[i], &iVal);
229349 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "%s%d", zSpace, (int)iVal);
229350 zSpace = " ";
229351 }
229352}
229353#endif /* SQLITE_TEST */
229354
229355#ifdef SQLITE_TEST
229356/*
229357** Buffer (a/n) is assumed to contain a list of serialized varints. Read
229358** each varint and append its string representation to buffer pBuf. Return
229359** after either the input buffer is exhausted or a 0 value is read.
229360**
229361** The return value is the number of bytes read from the input buffer.
229362*/
229363static int fts5DecodePoslist(int *pRc, Fts5Buffer *pBuf, const u8 *a, int n){
229364 int iOff = 0;
229365 while( iOff<n ){
229366 int iVal;
229367 iOff += fts5GetVarint32(&a[iOff], iVal);
229368 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " %d", iVal);
229369 }
229370 return iOff;
229371}
229372#endif /* SQLITE_TEST */
229373
229374#ifdef SQLITE_TEST
229375/*
229376** The start of buffer (a/n) contains the start of a doclist. The doclist
229377** may or may not finish within the buffer. This function appends a text
229378** representation of the part of the doclist that is present to buffer
229379** pBuf.
229380**
229381** The return value is the number of bytes read from the input buffer.
229382*/
229383static int fts5DecodeDoclist(int *pRc, Fts5Buffer *pBuf, const u8 *a, int n){
229384 i64 iDocid = 0;
229385 int iOff = 0;
229386
229387 if( n>0 ){
229388 iOff = sqlite3Fts5GetVarint(a, (u64*)&iDocid);
229389 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " id=%lld", iDocid);
229390 }
229391 while( iOff<n ){
229392 int nPos;
229393 int bDel;
229394 iOff += fts5GetPoslistSize(&a[iOff], &nPos, &bDel);
229395 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " nPos=%d%s", nPos, bDel?"*":"");
229396 iOff += fts5DecodePoslist(pRc, pBuf, &a[iOff], MIN(n-iOff, nPos));
229397 if( iOff<n ){
229398 i64 iDelta;
229399 iOff += sqlite3Fts5GetVarint(&a[iOff], (u64*)&iDelta);
229400 iDocid += iDelta;
229401 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " id=%lld", iDocid);
229402 }
229403 }
229404
229405 return iOff;
229406}
229407#endif /* SQLITE_TEST */
229408
229409#ifdef SQLITE_TEST
229410/*
229411** This function is part of the fts5_decode() debugging function. It is
229412** only ever used with detail=none tables.
229413**
229414** Buffer (pData/nData) contains a doclist in the format used by detail=none
229415** tables. This function appends a human-readable version of that list to
229416** buffer pBuf.
229417**
229418** If *pRc is other than SQLITE_OK when this function is called, it is a
229419** no-op. If an OOM or other error occurs within this function, *pRc is
229420** set to an SQLite error code before returning. The final state of buffer
229421** pBuf is undefined in this case.
229422*/
229423static void fts5DecodeRowidList(
229424 int *pRc, /* IN/OUT: Error code */
229425 Fts5Buffer *pBuf, /* Buffer to append text to */
229426 const u8 *pData, int nData /* Data to decode list-of-rowids from */
229427){
229428 int i = 0;
229429 i64 iRowid = 0;
229430
229431 while( i<nData ){
229432 const char *zApp = "";
229433 u64 iVal;
229434 i += sqlite3Fts5GetVarint(&pData[i], &iVal);
229435 iRowid += iVal;
229436
229437 if( i<nData && pData[i]==0x00 ){
229438 i++;
229439 if( i<nData && pData[i]==0x00 ){
229440 i++;
229441 zApp = "+";
229442 }else{
229443 zApp = "*";
229444 }
229445 }
229446
229447 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " %lld%s", iRowid, zApp);
229448 }
229449}
229450#endif /* SQLITE_TEST */
229451
229452#ifdef SQLITE_TEST
229453/*
229454** The implementation of user-defined scalar function fts5_decode().
229455*/
229456static void fts5DecodeFunction(
229457 sqlite3_context *pCtx, /* Function call context */
229458 int nArg, /* Number of args (always 2) */
229459 sqlite3_value **apVal /* Function arguments */
229460){
229461 i64 iRowid; /* Rowid for record being decoded */
229462 int iSegid,iHeight,iPgno,bDlidx;/* Rowid components */
229463 const u8 *aBlob; int n; /* Record to decode */
229464 u8 *a = 0;
229465 Fts5Buffer s; /* Build up text to return here */
229466 int rc = SQLITE_OK; /* Return code */
229467 sqlite3_int64 nSpace = 0;
229468 int eDetailNone = (sqlite3_user_data(pCtx)!=0);
229469
229470 assert( nArg==2 );
229471 UNUSED_PARAM(nArg);
229472 memset(&s, 0, sizeof(Fts5Buffer));
229473 iRowid = sqlite3_value_int64(apVal[0]);
229474
229475 /* Make a copy of the second argument (a blob) in aBlob[]. The aBlob[]
229476 ** copy is followed by FTS5_DATA_ZERO_PADDING 0x00 bytes, which prevents
229477 ** buffer overreads even if the record is corrupt. */
229478 n = sqlite3_value_bytes(apVal[1]);
229479 aBlob = sqlite3_value_blob(apVal[1]);
229480 nSpace = n + FTS5_DATA_ZERO_PADDING;
229481 a = (u8*)sqlite3Fts5MallocZero(&rc, nSpace);
229482 if( a==0 ) goto decode_out;
229483 if( n>0 ) memcpy(a, aBlob, n);
229484
229485 fts5DecodeRowid(iRowid, &iSegid, &bDlidx, &iHeight, &iPgno);
229486
229487 fts5DebugRowid(&rc, &s, iRowid);
229488 if( bDlidx ){
229489 Fts5Data dlidx;
229490 Fts5DlidxLvl lvl;
229491
229492 dlidx.p = a;
229493 dlidx.nn = n;
229494
229495 memset(&lvl, 0, sizeof(Fts5DlidxLvl));
229496 lvl.pData = &dlidx;
229497 lvl.iLeafPgno = iPgno;
229498
229499 for(fts5DlidxLvlNext(&lvl); lvl.bEof==0; fts5DlidxLvlNext(&lvl)){
229500 sqlite3Fts5BufferAppendPrintf(&rc, &s,
229501 " %d(%lld)", lvl.iLeafPgno, lvl.iRowid
229502 );
229503 }
229504 }else if( iSegid==0 ){
229505 if( iRowid==FTS5_AVERAGES_ROWID ){
229506 fts5DecodeAverages(&rc, &s, a, n);
229507 }else{
229508 fts5DecodeStructure(&rc, &s, a, n);
229509 }
229510 }else if( eDetailNone ){
229511 Fts5Buffer term; /* Current term read from page */
229512 int szLeaf;
229513 int iPgidxOff = szLeaf = fts5GetU16(&a[2]);
229514 int iTermOff;
229515 int nKeep = 0;
229516 int iOff;
229517
229518 memset(&term, 0, sizeof(Fts5Buffer));
229519
229520 /* Decode any entries that occur before the first term. */
229521 if( szLeaf<n ){
229522 iPgidxOff += fts5GetVarint32(&a[iPgidxOff], iTermOff);
229523 }else{
229524 iTermOff = szLeaf;
229525 }
229526 fts5DecodeRowidList(&rc, &s, &a[4], iTermOff-4);
229527
229528 iOff = iTermOff;
229529 while( iOff<szLeaf ){
229530 int nAppend;
229531
229532 /* Read the term data for the next term*/
229533 iOff += fts5GetVarint32(&a[iOff], nAppend);
229534 term.n = nKeep;
229535 fts5BufferAppendBlob(&rc, &term, nAppend, &a[iOff]);
229536 sqlite3Fts5BufferAppendPrintf(
229537 &rc, &s, " term=%.*s", term.n, (const char*)term.p
229538 );
229539 iOff += nAppend;
229540
229541 /* Figure out where the doclist for this term ends */
229542 if( iPgidxOff<n ){
229543 int nIncr;
229544 iPgidxOff += fts5GetVarint32(&a[iPgidxOff], nIncr);
229545 iTermOff += nIncr;
229546 }else{
229547 iTermOff = szLeaf;
229548 }
229549
229550 fts5DecodeRowidList(&rc, &s, &a[iOff], iTermOff-iOff);
229551 iOff = iTermOff;
229552 if( iOff<szLeaf ){
229553 iOff += fts5GetVarint32(&a[iOff], nKeep);
229554 }
229555 }
229556
229557 fts5BufferFree(&term);
229558 }else{
229559 Fts5Buffer term; /* Current term read from page */
229560 int szLeaf; /* Offset of pgidx in a[] */
229561 int iPgidxOff;
229562 int iPgidxPrev = 0; /* Previous value read from pgidx */
229563 int iTermOff = 0;
229564 int iRowidOff = 0;
229565 int iOff;
229566 int nDoclist;
229567
229568 memset(&term, 0, sizeof(Fts5Buffer));
229569
229570 if( n<4 ){
229571 sqlite3Fts5BufferSet(&rc, &s, 7, (const u8*)"corrupt");
229572 goto decode_out;
229573 }else{
229574 iRowidOff = fts5GetU16(&a[0]);
229575 iPgidxOff = szLeaf = fts5GetU16(&a[2]);
229576 if( iPgidxOff<n ){
229577 fts5GetVarint32(&a[iPgidxOff], iTermOff);
229578 }else if( iPgidxOff>n ){
229579 rc = FTS5_CORRUPT;
229580 goto decode_out;
229581 }
229582 }
229583
229584 /* Decode the position list tail at the start of the page */
229585 if( iRowidOff!=0 ){
229586 iOff = iRowidOff;
229587 }else if( iTermOff!=0 ){
229588 iOff = iTermOff;
229589 }else{
229590 iOff = szLeaf;
229591 }
229592 if( iOff>n ){
229593 rc = FTS5_CORRUPT;
229594 goto decode_out;
229595 }
229596 fts5DecodePoslist(&rc, &s, &a[4], iOff-4);
229597
229598 /* Decode any more doclist data that appears on the page before the
229599 ** first term. */
229600 nDoclist = (iTermOff ? iTermOff : szLeaf) - iOff;
229601 if( nDoclist+iOff>n ){
229602 rc = FTS5_CORRUPT;
229603 goto decode_out;
229604 }
229605 fts5DecodeDoclist(&rc, &s, &a[iOff], nDoclist);
229606
229607 while( iPgidxOff<n && rc==SQLITE_OK ){
229608 int bFirst = (iPgidxOff==szLeaf); /* True for first term on page */
229609 int nByte; /* Bytes of data */
229610 int iEnd;
229611
229612 iPgidxOff += fts5GetVarint32(&a[iPgidxOff], nByte);
229613 iPgidxPrev += nByte;
229614 iOff = iPgidxPrev;
229615
229616 if( iPgidxOff<n ){
229617 fts5GetVarint32(&a[iPgidxOff], nByte);
229618 iEnd = iPgidxPrev + nByte;
229619 }else{
229620 iEnd = szLeaf;
229621 }
229622 if( iEnd>szLeaf ){
229623 rc = FTS5_CORRUPT;
229624 break;
229625 }
229626
229627 if( bFirst==0 ){
229628 iOff += fts5GetVarint32(&a[iOff], nByte);
229629 if( nByte>term.n ){
229630 rc = FTS5_CORRUPT;
229631 break;
229632 }
229633 term.n = nByte;
229634 }
229635 iOff += fts5GetVarint32(&a[iOff], nByte);
229636 if( iOff+nByte>n ){
229637 rc = FTS5_CORRUPT;
229638 break;
229639 }
229640 fts5BufferAppendBlob(&rc, &term, nByte, &a[iOff]);
229641 iOff += nByte;
229642
229643 sqlite3Fts5BufferAppendPrintf(
229644 &rc, &s, " term=%.*s", term.n, (const char*)term.p
229645 );
229646 iOff += fts5DecodeDoclist(&rc, &s, &a[iOff], iEnd-iOff);
229647 }
229648
229649 fts5BufferFree(&term);
229650 }
229651
229652 decode_out:
229653 sqlite3_free(a);
229654 if( rc==SQLITE_OK ){
229655 sqlite3_result_text(pCtx, (const char*)s.p, s.n, SQLITE_TRANSIENT);
229656 }else{
229657 sqlite3_result_error_code(pCtx, rc);
229658 }
229659 fts5BufferFree(&s);
229660}
229661#endif /* SQLITE_TEST */
229662
229663#ifdef SQLITE_TEST
229664/*
229665** The implementation of user-defined scalar function fts5_rowid().
229666*/
229667static void fts5RowidFunction(
229668 sqlite3_context *pCtx, /* Function call context */
229669 int nArg, /* Number of args (always 2) */
229670 sqlite3_value **apVal /* Function arguments */
229671){
229672 const char *zArg;
229673 if( nArg==0 ){
229674 sqlite3_result_error(pCtx, "should be: fts5_rowid(subject, ....)", -1);
229675 }else{
229676 zArg = (const char*)sqlite3_value_text(apVal[0]);
229677 if( 0==sqlite3_stricmp(zArg, "segment") ){
229678 i64 iRowid;
229679 int segid, pgno;
229680 if( nArg!=3 ){
229681 sqlite3_result_error(pCtx,
229682 "should be: fts5_rowid('segment', segid, pgno))", -1
229683 );
229684 }else{
229685 segid = sqlite3_value_int(apVal[1]);
229686 pgno = sqlite3_value_int(apVal[2]);
229687 iRowid = FTS5_SEGMENT_ROWID(segid, pgno);
229688 sqlite3_result_int64(pCtx, iRowid);
229689 }
229690 }else{
229691 sqlite3_result_error(pCtx,
229692 "first arg to fts5_rowid() must be 'segment'" , -1
229693 );
229694 }
229695 }
229696}
229697#endif /* SQLITE_TEST */
229698
229699/*
229700** This is called as part of registering the FTS5 module with database
229701** connection db. It registers several user-defined scalar functions useful
229702** with FTS5.
229703**
229704** If successful, SQLITE_OK is returned. If an error occurs, some other
229705** SQLite error code is returned instead.
229706*/
229707static int sqlite3Fts5IndexInit(sqlite3 *db){
229708#ifdef SQLITE_TEST
229709 int rc = sqlite3_create_function(
229710 db, "fts5_decode", 2, SQLITE_UTF8, 0, fts5DecodeFunction, 0, 0
229711 );
229712
229713 if( rc==SQLITE_OK ){
229714 rc = sqlite3_create_function(
229715 db, "fts5_decode_none", 2,
229716 SQLITE_UTF8, (void*)db, fts5DecodeFunction, 0, 0
229717 );
229718 }
229719
229720 if( rc==SQLITE_OK ){
229721 rc = sqlite3_create_function(
229722 db, "fts5_rowid", -1, SQLITE_UTF8, 0, fts5RowidFunction, 0, 0
229723 );
229724 }
229725 return rc;
229726#else
229727 return SQLITE_OK;
229728 UNUSED_PARAM(db);
229729#endif
229730}
229731
229732
229733static int sqlite3Fts5IndexReset(Fts5Index *p){
229734 assert( p->pStruct==0 || p->iStructVersion!=0 );
229735 if( fts5IndexDataVersion(p)!=p->iStructVersion ){
229736 fts5StructureInvalidate(p);
229737 }
229738 return fts5IndexReturn(p);
229739}
229740
229741/*
229742** 2014 Jun 09
229743**
229744** The author disclaims copyright to this source code. In place of
229745** a legal notice, here is a blessing:
229746**
229747** May you do good and not evil.
229748** May you find forgiveness for yourself and forgive others.
229749** May you share freely, never taking more than you give.
229750**
229751******************************************************************************
229752**
229753** This is an SQLite module implementing full-text search.
229754*/
229755
229756
229757/* #include "fts5Int.h" */
229758
229759/*
229760** This variable is set to false when running tests for which the on disk
229761** structures should not be corrupt. Otherwise, true. If it is false, extra
229762** assert() conditions in the fts5 code are activated - conditions that are
229763** only true if it is guaranteed that the fts5 database is not corrupt.
229764*/
229765#ifdef SQLITE_DEBUG
229766SQLITE_API int sqlite3_fts5_may_be_corrupt = 1;
229767#endif
229768
229769
229770typedef struct Fts5Auxdata Fts5Auxdata;
229771typedef struct Fts5Auxiliary Fts5Auxiliary;
229772typedef struct Fts5Cursor Fts5Cursor;
229773typedef struct Fts5FullTable Fts5FullTable;
229774typedef struct Fts5Sorter Fts5Sorter;
229775typedef struct Fts5TokenizerModule Fts5TokenizerModule;
229776
229777/*
229778** NOTES ON TRANSACTIONS:
229779**
229780** SQLite invokes the following virtual table methods as transactions are
229781** opened and closed by the user:
229782**
229783** xBegin(): Start of a new transaction.
229784** xSync(): Initial part of two-phase commit.
229785** xCommit(): Final part of two-phase commit.
229786** xRollback(): Rollback the transaction.
229787**
229788** Anything that is required as part of a commit that may fail is performed
229789** in the xSync() callback. Current versions of SQLite ignore any errors
229790** returned by xCommit().
229791**
229792** And as sub-transactions are opened/closed:
229793**
229794** xSavepoint(int S): Open savepoint S.
229795** xRelease(int S): Commit and close savepoint S.
229796** xRollbackTo(int S): Rollback to start of savepoint S.
229797**
229798** During a write-transaction the fts5_index.c module may cache some data
229799** in-memory. It is flushed to disk whenever xSync(), xRelease() or
229800** xSavepoint() is called. And discarded whenever xRollback() or xRollbackTo()
229801** is called.
229802**
229803** Additionally, if SQLITE_DEBUG is defined, an instance of the following
229804** structure is used to record the current transaction state. This information
229805** is not required, but it is used in the assert() statements executed by
229806** function fts5CheckTransactionState() (see below).
229807*/
229808struct Fts5TransactionState {
229809 int eState; /* 0==closed, 1==open, 2==synced */
229810 int iSavepoint; /* Number of open savepoints (0 -> none) */
229811};
229812
229813/*
229814** A single object of this type is allocated when the FTS5 module is
229815** registered with a database handle. It is used to store pointers to
229816** all registered FTS5 extensions - tokenizers and auxiliary functions.
229817*/
229818struct Fts5Global {
229819 fts5_api api; /* User visible part of object (see fts5.h) */
229820 sqlite3 *db; /* Associated database connection */
229821 i64 iNextId; /* Used to allocate unique cursor ids */
229822 Fts5Auxiliary *pAux; /* First in list of all aux. functions */
229823 Fts5TokenizerModule *pTok; /* First in list of all tokenizer modules */
229824 Fts5TokenizerModule *pDfltTok; /* Default tokenizer module */
229825 Fts5Cursor *pCsr; /* First in list of all open cursors */
229826};
229827
229828/*
229829** Each auxiliary function registered with the FTS5 module is represented
229830** by an object of the following type. All such objects are stored as part
229831** of the Fts5Global.pAux list.
229832*/
229833struct Fts5Auxiliary {
229834 Fts5Global *pGlobal; /* Global context for this function */
229835 char *zFunc; /* Function name (nul-terminated) */
229836 void *pUserData; /* User-data pointer */
229837 fts5_extension_function xFunc; /* Callback function */
229838 void (*xDestroy)(void*); /* Destructor function */
229839 Fts5Auxiliary *pNext; /* Next registered auxiliary function */
229840};
229841
229842/*
229843** Each tokenizer module registered with the FTS5 module is represented
229844** by an object of the following type. All such objects are stored as part
229845** of the Fts5Global.pTok list.
229846*/
229847struct Fts5TokenizerModule {
229848 char *zName; /* Name of tokenizer */
229849 void *pUserData; /* User pointer passed to xCreate() */
229850 fts5_tokenizer x; /* Tokenizer functions */
229851 void (*xDestroy)(void*); /* Destructor function */
229852 Fts5TokenizerModule *pNext; /* Next registered tokenizer module */
229853};
229854
229855struct Fts5FullTable {
229856 Fts5Table p; /* Public class members from fts5Int.h */
229857 Fts5Storage *pStorage; /* Document store */
229858 Fts5Global *pGlobal; /* Global (connection wide) data */
229859 Fts5Cursor *pSortCsr; /* Sort data from this cursor */
229860#ifdef SQLITE_DEBUG
229861 struct Fts5TransactionState ts;
229862#endif
229863};
229864
229865struct Fts5MatchPhrase {
229866 Fts5Buffer *pPoslist; /* Pointer to current poslist */
229867 int nTerm; /* Size of phrase in terms */
229868};
229869
229870/*
229871** pStmt:
229872** SELECT rowid, <fts> FROM <fts> ORDER BY +rank;
229873**
229874** aIdx[]:
229875** There is one entry in the aIdx[] array for each phrase in the query,
229876** the value of which is the offset within aPoslist[] following the last
229877** byte of the position list for the corresponding phrase.
229878*/
229879struct Fts5Sorter {
229880 sqlite3_stmt *pStmt;
229881 i64 iRowid; /* Current rowid */
229882 const u8 *aPoslist; /* Position lists for current row */
229883 int nIdx; /* Number of entries in aIdx[] */
229884 int aIdx[1]; /* Offsets into aPoslist for current row */
229885};
229886
229887
229888/*
229889** Virtual-table cursor object.
229890**
229891** iSpecial:
229892** If this is a 'special' query (refer to function fts5SpecialMatch()),
229893** then this variable contains the result of the query.
229894**
229895** iFirstRowid, iLastRowid:
229896** These variables are only used for FTS5_PLAN_MATCH cursors. Assuming the
229897** cursor iterates in ascending order of rowids, iFirstRowid is the lower
229898** limit of rowids to return, and iLastRowid the upper. In other words, the
229899** WHERE clause in the user's query might have been:
229900**
229901** <tbl> MATCH <expr> AND rowid BETWEEN $iFirstRowid AND $iLastRowid
229902**
229903** If the cursor iterates in descending order of rowid, iFirstRowid
229904** is the upper limit (i.e. the "first" rowid visited) and iLastRowid
229905** the lower.
229906*/
229907struct Fts5Cursor {
229908 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
229909 Fts5Cursor *pNext; /* Next cursor in Fts5Cursor.pCsr list */
229910 int *aColumnSize; /* Values for xColumnSize() */
229911 i64 iCsrId; /* Cursor id */
229912
229913 /* Zero from this point onwards on cursor reset */
229914 int ePlan; /* FTS5_PLAN_XXX value */
229915 int bDesc; /* True for "ORDER BY rowid DESC" queries */
229916 i64 iFirstRowid; /* Return no rowids earlier than this */
229917 i64 iLastRowid; /* Return no rowids later than this */
229918 sqlite3_stmt *pStmt; /* Statement used to read %_content */
229919 Fts5Expr *pExpr; /* Expression for MATCH queries */
229920 Fts5Sorter *pSorter; /* Sorter for "ORDER BY rank" queries */
229921 int csrflags; /* Mask of cursor flags (see below) */
229922 i64 iSpecial; /* Result of special query */
229923
229924 /* "rank" function. Populated on demand from vtab.xColumn(). */
229925 char *zRank; /* Custom rank function */
229926 char *zRankArgs; /* Custom rank function args */
229927 Fts5Auxiliary *pRank; /* Rank callback (or NULL) */
229928 int nRankArg; /* Number of trailing arguments for rank() */
229929 sqlite3_value **apRankArg; /* Array of trailing arguments */
229930 sqlite3_stmt *pRankArgStmt; /* Origin of objects in apRankArg[] */
229931
229932 /* Auxiliary data storage */
229933 Fts5Auxiliary *pAux; /* Currently executing extension function */
229934 Fts5Auxdata *pAuxdata; /* First in linked list of saved aux-data */
229935
229936 /* Cache used by auxiliary functions xInst() and xInstCount() */
229937 Fts5PoslistReader *aInstIter; /* One for each phrase */
229938 int nInstAlloc; /* Size of aInst[] array (entries / 3) */
229939 int nInstCount; /* Number of phrase instances */
229940 int *aInst; /* 3 integers per phrase instance */
229941};
229942
229943/*
229944** Bits that make up the "idxNum" parameter passed indirectly by
229945** xBestIndex() to xFilter().
229946*/
229947#define FTS5_BI_MATCH 0x0001 /* <tbl> MATCH ? */
229948#define FTS5_BI_RANK 0x0002 /* rank MATCH ? */
229949#define FTS5_BI_ROWID_EQ 0x0004 /* rowid == ? */
229950#define FTS5_BI_ROWID_LE 0x0008 /* rowid <= ? */
229951#define FTS5_BI_ROWID_GE 0x0010 /* rowid >= ? */
229952
229953#define FTS5_BI_ORDER_RANK 0x0020
229954#define FTS5_BI_ORDER_ROWID 0x0040
229955#define FTS5_BI_ORDER_DESC 0x0080
229956
229957/*
229958** Values for Fts5Cursor.csrflags
229959*/
229960#define FTS5CSR_EOF 0x01
229961#define FTS5CSR_REQUIRE_CONTENT 0x02
229962#define FTS5CSR_REQUIRE_DOCSIZE 0x04
229963#define FTS5CSR_REQUIRE_INST 0x08
229964#define FTS5CSR_FREE_ZRANK 0x10
229965#define FTS5CSR_REQUIRE_RESEEK 0x20
229966#define FTS5CSR_REQUIRE_POSLIST 0x40
229967
229968#define BitFlagAllTest(x,y) (((x) & (y))==(y))
229969#define BitFlagTest(x,y) (((x) & (y))!=0)
229970
229971
229972/*
229973** Macros to Set(), Clear() and Test() cursor flags.
229974*/
229975#define CsrFlagSet(pCsr, flag) ((pCsr)->csrflags |= (flag))
229976#define CsrFlagClear(pCsr, flag) ((pCsr)->csrflags &= ~(flag))
229977#define CsrFlagTest(pCsr, flag) ((pCsr)->csrflags & (flag))
229978
229979struct Fts5Auxdata {
229980 Fts5Auxiliary *pAux; /* Extension to which this belongs */
229981 void *pPtr; /* Pointer value */
229982 void(*xDelete)(void*); /* Destructor */
229983 Fts5Auxdata *pNext; /* Next object in linked list */
229984};
229985
229986#ifdef SQLITE_DEBUG
229987#define FTS5_BEGIN 1
229988#define FTS5_SYNC 2
229989#define FTS5_COMMIT 3
229990#define FTS5_ROLLBACK 4
229991#define FTS5_SAVEPOINT 5
229992#define FTS5_RELEASE 6
229993#define FTS5_ROLLBACKTO 7
229994static void fts5CheckTransactionState(Fts5FullTable *p, int op, int iSavepoint){
229995 switch( op ){
229996 case FTS5_BEGIN:
229997 assert( p->ts.eState==0 );
229998 p->ts.eState = 1;
229999 p->ts.iSavepoint = -1;
230000 break;
230001
230002 case FTS5_SYNC:
230003 assert( p->ts.eState==1 );
230004 p->ts.eState = 2;
230005 break;
230006
230007 case FTS5_COMMIT:
230008 assert( p->ts.eState==2 );
230009 p->ts.eState = 0;
230010 break;
230011
230012 case FTS5_ROLLBACK:
230013 assert( p->ts.eState==1 || p->ts.eState==2 || p->ts.eState==0 );
230014 p->ts.eState = 0;
230015 break;
230016
230017 case FTS5_SAVEPOINT:
230018 assert( p->ts.eState==1 );
230019 assert( iSavepoint>=0 );
230020 assert( iSavepoint>=p->ts.iSavepoint );
230021 p->ts.iSavepoint = iSavepoint;
230022 break;
230023
230024 case FTS5_RELEASE:
230025 assert( p->ts.eState==1 );
230026 assert( iSavepoint>=0 );
230027 assert( iSavepoint<=p->ts.iSavepoint );
230028 p->ts.iSavepoint = iSavepoint-1;
230029 break;
230030
230031 case FTS5_ROLLBACKTO:
230032 assert( p->ts.eState==1 );
230033 assert( iSavepoint>=-1 );
230034 /* The following assert() can fail if another vtab strikes an error
230035 ** within an xSavepoint() call then SQLite calls xRollbackTo() - without
230036 ** having called xSavepoint() on this vtab. */
230037 /* assert( iSavepoint<=p->ts.iSavepoint ); */
230038 p->ts.iSavepoint = iSavepoint;
230039 break;
230040 }
230041}
230042#else
230043# define fts5CheckTransactionState(x,y,z)
230044#endif
230045
230046/*
230047** Return true if pTab is a contentless table.
230048*/
230049static int fts5IsContentless(Fts5FullTable *pTab){
230050 return pTab->p.pConfig->eContent==FTS5_CONTENT_NONE;
230051}
230052
230053/*
230054** Delete a virtual table handle allocated by fts5InitVtab().
230055*/
230056static void fts5FreeVtab(Fts5FullTable *pTab){
230057 if( pTab ){
230058 sqlite3Fts5IndexClose(p: pTab->p.pIndex);
230059 sqlite3Fts5StorageClose(p: pTab->pStorage);
230060 sqlite3Fts5ConfigFree(pConfig: pTab->p.pConfig);
230061 sqlite3_free(p: pTab);
230062 }
230063}
230064
230065/*
230066** The xDisconnect() virtual table method.
230067*/
230068static int fts5DisconnectMethod(sqlite3_vtab *pVtab){
230069 fts5FreeVtab(pTab: (Fts5FullTable*)pVtab);
230070 return SQLITE_OK;
230071}
230072
230073/*
230074** The xDestroy() virtual table method.
230075*/
230076static int fts5DestroyMethod(sqlite3_vtab *pVtab){
230077 Fts5Table *pTab = (Fts5Table*)pVtab;
230078 int rc = sqlite3Fts5DropAll(pTab->pConfig);
230079 if( rc==SQLITE_OK ){
230080 fts5FreeVtab(pTab: (Fts5FullTable*)pVtab);
230081 }
230082 return rc;
230083}
230084
230085/*
230086** This function is the implementation of both the xConnect and xCreate
230087** methods of the FTS3 virtual table.
230088**
230089** The argv[] array contains the following:
230090**
230091** argv[0] -> module name ("fts5")
230092** argv[1] -> database name
230093** argv[2] -> table name
230094** argv[...] -> "column name" and other module argument fields.
230095*/
230096static int fts5InitVtab(
230097 int bCreate, /* True for xCreate, false for xConnect */
230098 sqlite3 *db, /* The SQLite database connection */
230099 void *pAux, /* Hash table containing tokenizers */
230100 int argc, /* Number of elements in argv array */
230101 const char * const *argv, /* xCreate/xConnect argument array */
230102 sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
230103 char **pzErr /* Write any error message here */
230104){
230105 Fts5Global *pGlobal = (Fts5Global*)pAux;
230106 const char **azConfig = (const char**)argv;
230107 int rc = SQLITE_OK; /* Return code */
230108 Fts5Config *pConfig = 0; /* Results of parsing argc/argv */
230109 Fts5FullTable *pTab = 0; /* New virtual table object */
230110
230111 /* Allocate the new vtab object and parse the configuration */
230112 pTab = (Fts5FullTable*)sqlite3Fts5MallocZero(pRc: &rc, nByte: sizeof(Fts5FullTable));
230113 if( rc==SQLITE_OK ){
230114 rc = sqlite3Fts5ConfigParse(pGlobal, db, nArg: argc, azArg: azConfig, ppOut: &pConfig, pzErr);
230115 assert( (rc==SQLITE_OK && *pzErr==0) || pConfig==0 );
230116 }
230117 if( rc==SQLITE_OK ){
230118 pTab->p.pConfig = pConfig;
230119 pTab->pGlobal = pGlobal;
230120 }
230121
230122 /* Open the index sub-system */
230123 if( rc==SQLITE_OK ){
230124 rc = sqlite3Fts5IndexOpen(pConfig, bCreate, pp: &pTab->p.pIndex, pzErr);
230125 }
230126
230127 /* Open the storage sub-system */
230128 if( rc==SQLITE_OK ){
230129 rc = sqlite3Fts5StorageOpen(
230130 pConfig, pTab->p.pIndex, bCreate, &pTab->pStorage, pzErr
230131 );
230132 }
230133
230134 /* Call sqlite3_declare_vtab() */
230135 if( rc==SQLITE_OK ){
230136 rc = sqlite3Fts5ConfigDeclareVtab(pConfig);
230137 }
230138
230139 /* Load the initial configuration */
230140 if( rc==SQLITE_OK ){
230141 assert( pConfig->pzErrmsg==0 );
230142 pConfig->pzErrmsg = pzErr;
230143 rc = sqlite3Fts5IndexLoadConfig(p: pTab->p.pIndex);
230144 sqlite3Fts5IndexRollback(p: pTab->p.pIndex);
230145 pConfig->pzErrmsg = 0;
230146 }
230147
230148 if( rc!=SQLITE_OK ){
230149 fts5FreeVtab(pTab);
230150 pTab = 0;
230151 }else if( bCreate ){
230152 fts5CheckTransactionState(pTab, FTS5_BEGIN, 0);
230153 }
230154 *ppVTab = (sqlite3_vtab*)pTab;
230155 return rc;
230156}
230157
230158/*
230159** The xConnect() and xCreate() methods for the virtual table. All the
230160** work is done in function fts5InitVtab().
230161*/
230162static int fts5ConnectMethod(
230163 sqlite3 *db, /* Database connection */
230164 void *pAux, /* Pointer to tokenizer hash table */
230165 int argc, /* Number of elements in argv array */
230166 const char * const *argv, /* xCreate/xConnect argument array */
230167 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
230168 char **pzErr /* OUT: sqlite3_malloc'd error message */
230169){
230170 return fts5InitVtab(bCreate: 0, db, pAux, argc, argv, ppVTab: ppVtab, pzErr);
230171}
230172static int fts5CreateMethod(
230173 sqlite3 *db, /* Database connection */
230174 void *pAux, /* Pointer to tokenizer hash table */
230175 int argc, /* Number of elements in argv array */
230176 const char * const *argv, /* xCreate/xConnect argument array */
230177 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
230178 char **pzErr /* OUT: sqlite3_malloc'd error message */
230179){
230180 return fts5InitVtab(bCreate: 1, db, pAux, argc, argv, ppVTab: ppVtab, pzErr);
230181}
230182
230183/*
230184** The different query plans.
230185*/
230186#define FTS5_PLAN_MATCH 1 /* (<tbl> MATCH ?) */
230187#define FTS5_PLAN_SOURCE 2 /* A source cursor for SORTED_MATCH */
230188#define FTS5_PLAN_SPECIAL 3 /* An internal query */
230189#define FTS5_PLAN_SORTED_MATCH 4 /* (<tbl> MATCH ? ORDER BY rank) */
230190#define FTS5_PLAN_SCAN 5 /* No usable constraint */
230191#define FTS5_PLAN_ROWID 6 /* (rowid = ?) */
230192
230193/*
230194** Set the SQLITE_INDEX_SCAN_UNIQUE flag in pIdxInfo->flags. Unless this
230195** extension is currently being used by a version of SQLite too old to
230196** support index-info flags. In that case this function is a no-op.
230197*/
230198static void fts5SetUniqueFlag(sqlite3_index_info *pIdxInfo){
230199#if SQLITE_VERSION_NUMBER>=3008012
230200#ifndef SQLITE_CORE
230201 if( sqlite3_libversion_number()>=3008012 )
230202#endif
230203 {
230204 pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_UNIQUE;
230205 }
230206#endif
230207}
230208
230209static int fts5UsePatternMatch(
230210 Fts5Config *pConfig,
230211 struct sqlite3_index_constraint *p
230212){
230213 assert( FTS5_PATTERN_GLOB==SQLITE_INDEX_CONSTRAINT_GLOB );
230214 assert( FTS5_PATTERN_LIKE==SQLITE_INDEX_CONSTRAINT_LIKE );
230215 if( pConfig->ePattern==FTS5_PATTERN_GLOB && p->op==FTS5_PATTERN_GLOB ){
230216 return 1;
230217 }
230218 if( pConfig->ePattern==FTS5_PATTERN_LIKE
230219 && (p->op==FTS5_PATTERN_LIKE || p->op==FTS5_PATTERN_GLOB)
230220 ){
230221 return 1;
230222 }
230223 return 0;
230224}
230225
230226/*
230227** Implementation of the xBestIndex method for FTS5 tables. Within the
230228** WHERE constraint, it searches for the following:
230229**
230230** 1. A MATCH constraint against the table column.
230231** 2. A MATCH constraint against the "rank" column.
230232** 3. A MATCH constraint against some other column.
230233** 4. An == constraint against the rowid column.
230234** 5. A < or <= constraint against the rowid column.
230235** 6. A > or >= constraint against the rowid column.
230236**
230237** Within the ORDER BY, the following are supported:
230238**
230239** 5. ORDER BY rank [ASC|DESC]
230240** 6. ORDER BY rowid [ASC|DESC]
230241**
230242** Information for the xFilter call is passed via both the idxNum and
230243** idxStr variables. Specifically, idxNum is a bitmask of the following
230244** flags used to encode the ORDER BY clause:
230245**
230246** FTS5_BI_ORDER_RANK
230247** FTS5_BI_ORDER_ROWID
230248** FTS5_BI_ORDER_DESC
230249**
230250** idxStr is used to encode data from the WHERE clause. For each argument
230251** passed to the xFilter method, the following is appended to idxStr:
230252**
230253** Match against table column: "m"
230254** Match against rank column: "r"
230255** Match against other column: "M<column-number>"
230256** LIKE against other column: "L<column-number>"
230257** GLOB against other column: "G<column-number>"
230258** Equality constraint against the rowid: "="
230259** A < or <= against the rowid: "<"
230260** A > or >= against the rowid: ">"
230261**
230262** This function ensures that there is at most one "r" or "=". And that if
230263** there exists an "=" then there is no "<" or ">".
230264**
230265** Costs are assigned as follows:
230266**
230267** a) If an unusable MATCH operator is present in the WHERE clause, the
230268** cost is unconditionally set to 1e50 (a really big number).
230269**
230270** a) If a MATCH operator is present, the cost depends on the other
230271** constraints also present. As follows:
230272**
230273** * No other constraints: cost=1000.0
230274** * One rowid range constraint: cost=750.0
230275** * Both rowid range constraints: cost=500.0
230276** * An == rowid constraint: cost=100.0
230277**
230278** b) Otherwise, if there is no MATCH:
230279**
230280** * No other constraints: cost=1000000.0
230281** * One rowid range constraint: cost=750000.0
230282** * Both rowid range constraints: cost=250000.0
230283** * An == rowid constraint: cost=10.0
230284**
230285** Costs are not modified by the ORDER BY clause.
230286*/
230287static int fts5BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
230288 Fts5Table *pTab = (Fts5Table*)pVTab;
230289 Fts5Config *pConfig = pTab->pConfig;
230290 const int nCol = pConfig->nCol;
230291 int idxFlags = 0; /* Parameter passed through to xFilter() */
230292 int i;
230293
230294 char *idxStr;
230295 int iIdxStr = 0;
230296 int iCons = 0;
230297
230298 int bSeenEq = 0;
230299 int bSeenGt = 0;
230300 int bSeenLt = 0;
230301 int bSeenMatch = 0;
230302 int bSeenRank = 0;
230303
230304
230305 assert( SQLITE_INDEX_CONSTRAINT_EQ<SQLITE_INDEX_CONSTRAINT_MATCH );
230306 assert( SQLITE_INDEX_CONSTRAINT_GT<SQLITE_INDEX_CONSTRAINT_MATCH );
230307 assert( SQLITE_INDEX_CONSTRAINT_LE<SQLITE_INDEX_CONSTRAINT_MATCH );
230308 assert( SQLITE_INDEX_CONSTRAINT_GE<SQLITE_INDEX_CONSTRAINT_MATCH );
230309 assert( SQLITE_INDEX_CONSTRAINT_LE<SQLITE_INDEX_CONSTRAINT_MATCH );
230310
230311 if( pConfig->bLock ){
230312 pTab->base.zErrMsg = sqlite3_mprintf(
230313 zFormat: "recursively defined fts5 content table"
230314 );
230315 return SQLITE_ERROR;
230316 }
230317
230318 idxStr = (char*)sqlite3_malloc(n: pInfo->nConstraint * 8 + 1);
230319 if( idxStr==0 ) return SQLITE_NOMEM;
230320 pInfo->idxStr = idxStr;
230321 pInfo->needToFreeIdxStr = 1;
230322
230323 for(i=0; i<pInfo->nConstraint; i++){
230324 struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
230325 int iCol = p->iColumn;
230326 if( p->op==SQLITE_INDEX_CONSTRAINT_MATCH
230327 || (p->op==SQLITE_INDEX_CONSTRAINT_EQ && iCol>=nCol)
230328 ){
230329 /* A MATCH operator or equivalent */
230330 if( p->usable==0 || iCol<0 ){
230331 /* As there exists an unusable MATCH constraint this is an
230332 ** unusable plan. Set a prohibitively high cost. */
230333 pInfo->estimatedCost = 1e50;
230334 assert( iIdxStr < pInfo->nConstraint*6 + 1 );
230335 idxStr[iIdxStr] = 0;
230336 return SQLITE_OK;
230337 }else{
230338 if( iCol==nCol+1 ){
230339 if( bSeenRank ) continue;
230340 idxStr[iIdxStr++] = 'r';
230341 bSeenRank = 1;
230342 }else if( iCol>=0 ){
230343 bSeenMatch = 1;
230344 idxStr[iIdxStr++] = 'M';
230345 sqlite3_snprintf(n: 6, zBuf: &idxStr[iIdxStr], zFormat: "%d", iCol);
230346 idxStr += strlen(s: &idxStr[iIdxStr]);
230347 assert( idxStr[iIdxStr]=='\0' );
230348 }
230349 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
230350 pInfo->aConstraintUsage[i].omit = 1;
230351 }
230352 }else if( p->usable ){
230353 if( iCol>=0 && iCol<nCol && fts5UsePatternMatch(pConfig, p) ){
230354 assert( p->op==FTS5_PATTERN_LIKE || p->op==FTS5_PATTERN_GLOB );
230355 idxStr[iIdxStr++] = p->op==FTS5_PATTERN_LIKE ? 'L' : 'G';
230356 sqlite3_snprintf(n: 6, zBuf: &idxStr[iIdxStr], zFormat: "%d", iCol);
230357 idxStr += strlen(s: &idxStr[iIdxStr]);
230358 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
230359 assert( idxStr[iIdxStr]=='\0' );
230360 }else if( bSeenEq==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ && iCol<0 ){
230361 idxStr[iIdxStr++] = '=';
230362 bSeenEq = 1;
230363 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
230364 }
230365 }
230366 }
230367
230368 if( bSeenEq==0 ){
230369 for(i=0; i<pInfo->nConstraint; i++){
230370 struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
230371 if( p->iColumn<0 && p->usable ){
230372 int op = p->op;
230373 if( op==SQLITE_INDEX_CONSTRAINT_LT || op==SQLITE_INDEX_CONSTRAINT_LE ){
230374 if( bSeenLt ) continue;
230375 idxStr[iIdxStr++] = '<';
230376 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
230377 bSeenLt = 1;
230378 }else
230379 if( op==SQLITE_INDEX_CONSTRAINT_GT || op==SQLITE_INDEX_CONSTRAINT_GE ){
230380 if( bSeenGt ) continue;
230381 idxStr[iIdxStr++] = '>';
230382 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
230383 bSeenGt = 1;
230384 }
230385 }
230386 }
230387 }
230388 idxStr[iIdxStr] = '\0';
230389
230390 /* Set idxFlags flags for the ORDER BY clause */
230391 if( pInfo->nOrderBy==1 ){
230392 int iSort = pInfo->aOrderBy[0].iColumn;
230393 if( iSort==(pConfig->nCol+1) && bSeenMatch ){
230394 idxFlags |= FTS5_BI_ORDER_RANK;
230395 }else if( iSort==-1 ){
230396 idxFlags |= FTS5_BI_ORDER_ROWID;
230397 }
230398 if( BitFlagTest(idxFlags, FTS5_BI_ORDER_RANK|FTS5_BI_ORDER_ROWID) ){
230399 pInfo->orderByConsumed = 1;
230400 if( pInfo->aOrderBy[0].desc ){
230401 idxFlags |= FTS5_BI_ORDER_DESC;
230402 }
230403 }
230404 }
230405
230406 /* Calculate the estimated cost based on the flags set in idxFlags. */
230407 if( bSeenEq ){
230408 pInfo->estimatedCost = bSeenMatch ? 100.0 : 10.0;
230409 if( bSeenMatch==0 ) fts5SetUniqueFlag(pIdxInfo: pInfo);
230410 }else if( bSeenLt && bSeenGt ){
230411 pInfo->estimatedCost = bSeenMatch ? 500.0 : 250000.0;
230412 }else if( bSeenLt || bSeenGt ){
230413 pInfo->estimatedCost = bSeenMatch ? 750.0 : 750000.0;
230414 }else{
230415 pInfo->estimatedCost = bSeenMatch ? 1000.0 : 1000000.0;
230416 }
230417
230418 pInfo->idxNum = idxFlags;
230419 return SQLITE_OK;
230420}
230421
230422static int fts5NewTransaction(Fts5FullTable *pTab){
230423 Fts5Cursor *pCsr;
230424 for(pCsr=pTab->pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
230425 if( pCsr->base.pVtab==(sqlite3_vtab*)pTab ) return SQLITE_OK;
230426 }
230427 return sqlite3Fts5StorageReset(p: pTab->pStorage);
230428}
230429
230430/*
230431** Implementation of xOpen method.
230432*/
230433static int fts5OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
230434 Fts5FullTable *pTab = (Fts5FullTable*)pVTab;
230435 Fts5Config *pConfig = pTab->p.pConfig;
230436 Fts5Cursor *pCsr = 0; /* New cursor object */
230437 sqlite3_int64 nByte; /* Bytes of space to allocate */
230438 int rc; /* Return code */
230439
230440 rc = fts5NewTransaction(pTab);
230441 if( rc==SQLITE_OK ){
230442 nByte = sizeof(Fts5Cursor) + pConfig->nCol * sizeof(int);
230443 pCsr = (Fts5Cursor*)sqlite3_malloc64(n: nByte);
230444 if( pCsr ){
230445 Fts5Global *pGlobal = pTab->pGlobal;
230446 memset(s: pCsr, c: 0, n: (size_t)nByte);
230447 pCsr->aColumnSize = (int*)&pCsr[1];
230448 pCsr->pNext = pGlobal->pCsr;
230449 pGlobal->pCsr = pCsr;
230450 pCsr->iCsrId = ++pGlobal->iNextId;
230451 }else{
230452 rc = SQLITE_NOMEM;
230453 }
230454 }
230455 *ppCsr = (sqlite3_vtab_cursor*)pCsr;
230456 return rc;
230457}
230458
230459static int fts5StmtType(Fts5Cursor *pCsr){
230460 if( pCsr->ePlan==FTS5_PLAN_SCAN ){
230461 return (pCsr->bDesc) ? FTS5_STMT_SCAN_DESC : FTS5_STMT_SCAN_ASC;
230462 }
230463 return FTS5_STMT_LOOKUP;
230464}
230465
230466/*
230467** This function is called after the cursor passed as the only argument
230468** is moved to point at a different row. It clears all cached data
230469** specific to the previous row stored by the cursor object.
230470*/
230471static void fts5CsrNewrow(Fts5Cursor *pCsr){
230472 CsrFlagSet(pCsr,
230473 FTS5CSR_REQUIRE_CONTENT
230474 | FTS5CSR_REQUIRE_DOCSIZE
230475 | FTS5CSR_REQUIRE_INST
230476 | FTS5CSR_REQUIRE_POSLIST
230477 );
230478}
230479
230480static void fts5FreeCursorComponents(Fts5Cursor *pCsr){
230481 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
230482 Fts5Auxdata *pData;
230483 Fts5Auxdata *pNext;
230484
230485 sqlite3_free(p: pCsr->aInstIter);
230486 sqlite3_free(p: pCsr->aInst);
230487 if( pCsr->pStmt ){
230488 int eStmt = fts5StmtType(pCsr);
230489 sqlite3Fts5StorageStmtRelease(p: pTab->pStorage, eStmt, pCsr->pStmt);
230490 }
230491 if( pCsr->pSorter ){
230492 Fts5Sorter *pSorter = pCsr->pSorter;
230493 sqlite3_finalize(pStmt: pSorter->pStmt);
230494 sqlite3_free(p: pSorter);
230495 }
230496
230497 if( pCsr->ePlan!=FTS5_PLAN_SOURCE ){
230498 sqlite3Fts5ExprFree(p: pCsr->pExpr);
230499 }
230500
230501 for(pData=pCsr->pAuxdata; pData; pData=pNext){
230502 pNext = pData->pNext;
230503 if( pData->xDelete ) pData->xDelete(pData->pPtr);
230504 sqlite3_free(p: pData);
230505 }
230506
230507 sqlite3_finalize(pStmt: pCsr->pRankArgStmt);
230508 sqlite3_free(p: pCsr->apRankArg);
230509
230510 if( CsrFlagTest(pCsr, FTS5CSR_FREE_ZRANK) ){
230511 sqlite3_free(p: pCsr->zRank);
230512 sqlite3_free(p: pCsr->zRankArgs);
230513 }
230514
230515 sqlite3Fts5IndexCloseReader(p: pTab->p.pIndex);
230516 memset(s: &pCsr->ePlan, c: 0, n: sizeof(Fts5Cursor) - ((u8*)&pCsr->ePlan - (u8*)pCsr));
230517}
230518
230519
230520/*
230521** Close the cursor. For additional information see the documentation
230522** on the xClose method of the virtual table interface.
230523*/
230524static int fts5CloseMethod(sqlite3_vtab_cursor *pCursor){
230525 if( pCursor ){
230526 Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab);
230527 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
230528 Fts5Cursor **pp;
230529
230530 fts5FreeCursorComponents(pCsr);
230531 /* Remove the cursor from the Fts5Global.pCsr list */
230532 for(pp=&pTab->pGlobal->pCsr; (*pp)!=pCsr; pp=&(*pp)->pNext);
230533 *pp = pCsr->pNext;
230534
230535 sqlite3_free(p: pCsr);
230536 }
230537 return SQLITE_OK;
230538}
230539
230540static int fts5SorterNext(Fts5Cursor *pCsr){
230541 Fts5Sorter *pSorter = pCsr->pSorter;
230542 int rc;
230543
230544 rc = sqlite3_step(pStmt: pSorter->pStmt);
230545 if( rc==SQLITE_DONE ){
230546 rc = SQLITE_OK;
230547 CsrFlagSet(pCsr, FTS5CSR_EOF);
230548 }else if( rc==SQLITE_ROW ){
230549 const u8 *a;
230550 const u8 *aBlob;
230551 int nBlob;
230552 int i;
230553 int iOff = 0;
230554 rc = SQLITE_OK;
230555
230556 pSorter->iRowid = sqlite3_column_int64(pStmt: pSorter->pStmt, i: 0);
230557 nBlob = sqlite3_column_bytes(pStmt: pSorter->pStmt, i: 1);
230558 aBlob = a = sqlite3_column_blob(pStmt: pSorter->pStmt, i: 1);
230559
230560 /* nBlob==0 in detail=none mode. */
230561 if( nBlob>0 ){
230562 for(i=0; i<(pSorter->nIdx-1); i++){
230563 int iVal;
230564 a += fts5GetVarint32(a, iVal);
230565 iOff += iVal;
230566 pSorter->aIdx[i] = iOff;
230567 }
230568 pSorter->aIdx[i] = &aBlob[nBlob] - a;
230569 pSorter->aPoslist = a;
230570 }
230571
230572 fts5CsrNewrow(pCsr);
230573 }
230574
230575 return rc;
230576}
230577
230578
230579/*
230580** Set the FTS5CSR_REQUIRE_RESEEK flag on all FTS5_PLAN_MATCH cursors
230581** open on table pTab.
230582*/
230583static void fts5TripCursors(Fts5FullTable *pTab){
230584 Fts5Cursor *pCsr;
230585 for(pCsr=pTab->pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
230586 if( pCsr->ePlan==FTS5_PLAN_MATCH
230587 && pCsr->base.pVtab==(sqlite3_vtab*)pTab
230588 ){
230589 CsrFlagSet(pCsr, FTS5CSR_REQUIRE_RESEEK);
230590 }
230591 }
230592}
230593
230594/*
230595** If the REQUIRE_RESEEK flag is set on the cursor passed as the first
230596** argument, close and reopen all Fts5IndexIter iterators that the cursor
230597** is using. Then attempt to move the cursor to a rowid equal to or laster
230598** (in the cursors sort order - ASC or DESC) than the current rowid.
230599**
230600** If the new rowid is not equal to the old, set output parameter *pbSkip
230601** to 1 before returning. Otherwise, leave it unchanged.
230602**
230603** Return SQLITE_OK if successful or if no reseek was required, or an
230604** error code if an error occurred.
230605*/
230606static int fts5CursorReseek(Fts5Cursor *pCsr, int *pbSkip){
230607 int rc = SQLITE_OK;
230608 assert( *pbSkip==0 );
230609 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_RESEEK) ){
230610 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
230611 int bDesc = pCsr->bDesc;
230612 i64 iRowid = sqlite3Fts5ExprRowid(p: pCsr->pExpr);
230613
230614 rc = sqlite3Fts5ExprFirst(p: pCsr->pExpr, pIdx: pTab->p.pIndex, iFirst: iRowid, bDesc);
230615 if( rc==SQLITE_OK && iRowid!=sqlite3Fts5ExprRowid(p: pCsr->pExpr) ){
230616 *pbSkip = 1;
230617 }
230618
230619 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_RESEEK);
230620 fts5CsrNewrow(pCsr);
230621 if( sqlite3Fts5ExprEof(p: pCsr->pExpr) ){
230622 CsrFlagSet(pCsr, FTS5CSR_EOF);
230623 *pbSkip = 1;
230624 }
230625 }
230626 return rc;
230627}
230628
230629
230630/*
230631** Advance the cursor to the next row in the table that matches the
230632** search criteria.
230633**
230634** Return SQLITE_OK if nothing goes wrong. SQLITE_OK is returned
230635** even if we reach end-of-file. The fts5EofMethod() will be called
230636** subsequently to determine whether or not an EOF was hit.
230637*/
230638static int fts5NextMethod(sqlite3_vtab_cursor *pCursor){
230639 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
230640 int rc;
230641
230642 assert( (pCsr->ePlan<3)==
230643 (pCsr->ePlan==FTS5_PLAN_MATCH || pCsr->ePlan==FTS5_PLAN_SOURCE)
230644 );
230645 assert( !CsrFlagTest(pCsr, FTS5CSR_EOF) );
230646
230647 if( pCsr->ePlan<3 ){
230648 int bSkip = 0;
230649 if( (rc = fts5CursorReseek(pCsr, pbSkip: &bSkip)) || bSkip ) return rc;
230650 rc = sqlite3Fts5ExprNext(p: pCsr->pExpr, iLast: pCsr->iLastRowid);
230651 CsrFlagSet(pCsr, sqlite3Fts5ExprEof(pCsr->pExpr));
230652 fts5CsrNewrow(pCsr);
230653 }else{
230654 switch( pCsr->ePlan ){
230655 case FTS5_PLAN_SPECIAL: {
230656 CsrFlagSet(pCsr, FTS5CSR_EOF);
230657 rc = SQLITE_OK;
230658 break;
230659 }
230660
230661 case FTS5_PLAN_SORTED_MATCH: {
230662 rc = fts5SorterNext(pCsr);
230663 break;
230664 }
230665
230666 default: {
230667 Fts5Config *pConfig = ((Fts5Table*)pCursor->pVtab)->pConfig;
230668 pConfig->bLock++;
230669 rc = sqlite3_step(pStmt: pCsr->pStmt);
230670 pConfig->bLock--;
230671 if( rc!=SQLITE_ROW ){
230672 CsrFlagSet(pCsr, FTS5CSR_EOF);
230673 rc = sqlite3_reset(pStmt: pCsr->pStmt);
230674 if( rc!=SQLITE_OK ){
230675 pCursor->pVtab->zErrMsg = sqlite3_mprintf(
230676 zFormat: "%s", sqlite3_errmsg(db: pConfig->db)
230677 );
230678 }
230679 }else{
230680 rc = SQLITE_OK;
230681 }
230682 break;
230683 }
230684 }
230685 }
230686
230687 return rc;
230688}
230689
230690
230691static int fts5PrepareStatement(
230692 sqlite3_stmt **ppStmt,
230693 Fts5Config *pConfig,
230694 const char *zFmt,
230695 ...
230696){
230697 sqlite3_stmt *pRet = 0;
230698 int rc;
230699 char *zSql;
230700 va_list ap;
230701
230702 va_start(ap, zFmt);
230703 zSql = sqlite3_vmprintf(zFormat: zFmt, ap);
230704 if( zSql==0 ){
230705 rc = SQLITE_NOMEM;
230706 }else{
230707 rc = sqlite3_prepare_v3(db: pConfig->db, zSql, nBytes: -1,
230708 SQLITE_PREPARE_PERSISTENT, ppStmt: &pRet, pzTail: 0);
230709 if( rc!=SQLITE_OK ){
230710 *pConfig->pzErrmsg = sqlite3_mprintf(zFormat: "%s", sqlite3_errmsg(db: pConfig->db));
230711 }
230712 sqlite3_free(p: zSql);
230713 }
230714
230715 va_end(ap);
230716 *ppStmt = pRet;
230717 return rc;
230718}
230719
230720static int fts5CursorFirstSorted(
230721 Fts5FullTable *pTab,
230722 Fts5Cursor *pCsr,
230723 int bDesc
230724){
230725 Fts5Config *pConfig = pTab->p.pConfig;
230726 Fts5Sorter *pSorter;
230727 int nPhrase;
230728 sqlite3_int64 nByte;
230729 int rc;
230730 const char *zRank = pCsr->zRank;
230731 const char *zRankArgs = pCsr->zRankArgs;
230732
230733 nPhrase = sqlite3Fts5ExprPhraseCount(pExpr: pCsr->pExpr);
230734 nByte = sizeof(Fts5Sorter) + sizeof(int) * (nPhrase-1);
230735 pSorter = (Fts5Sorter*)sqlite3_malloc64(n: nByte);
230736 if( pSorter==0 ) return SQLITE_NOMEM;
230737 memset(s: pSorter, c: 0, n: (size_t)nByte);
230738 pSorter->nIdx = nPhrase;
230739
230740 /* TODO: It would be better to have some system for reusing statement
230741 ** handles here, rather than preparing a new one for each query. But that
230742 ** is not possible as SQLite reference counts the virtual table objects.
230743 ** And since the statement required here reads from this very virtual
230744 ** table, saving it creates a circular reference.
230745 **
230746 ** If SQLite a built-in statement cache, this wouldn't be a problem. */
230747 rc = fts5PrepareStatement(ppStmt: &pSorter->pStmt, pConfig,
230748 zFmt: "SELECT rowid, rank FROM %Q.%Q ORDER BY %s(\"%w\"%s%s) %s",
230749 pConfig->zDb, pConfig->zName, zRank, pConfig->zName,
230750 (zRankArgs ? ", " : ""),
230751 (zRankArgs ? zRankArgs : ""),
230752 bDesc ? "DESC" : "ASC"
230753 );
230754
230755 pCsr->pSorter = pSorter;
230756 if( rc==SQLITE_OK ){
230757 assert( pTab->pSortCsr==0 );
230758 pTab->pSortCsr = pCsr;
230759 rc = fts5SorterNext(pCsr);
230760 pTab->pSortCsr = 0;
230761 }
230762
230763 if( rc!=SQLITE_OK ){
230764 sqlite3_finalize(pStmt: pSorter->pStmt);
230765 sqlite3_free(p: pSorter);
230766 pCsr->pSorter = 0;
230767 }
230768
230769 return rc;
230770}
230771
230772static int fts5CursorFirst(Fts5FullTable *pTab, Fts5Cursor *pCsr, int bDesc){
230773 int rc;
230774 Fts5Expr *pExpr = pCsr->pExpr;
230775 rc = sqlite3Fts5ExprFirst(p: pExpr, pIdx: pTab->p.pIndex, iFirst: pCsr->iFirstRowid, bDesc);
230776 if( sqlite3Fts5ExprEof(p: pExpr) ){
230777 CsrFlagSet(pCsr, FTS5CSR_EOF);
230778 }
230779 fts5CsrNewrow(pCsr);
230780 return rc;
230781}
230782
230783/*
230784** Process a "special" query. A special query is identified as one with a
230785** MATCH expression that begins with a '*' character. The remainder of
230786** the text passed to the MATCH operator are used as the special query
230787** parameters.
230788*/
230789static int fts5SpecialMatch(
230790 Fts5FullTable *pTab,
230791 Fts5Cursor *pCsr,
230792 const char *zQuery
230793){
230794 int rc = SQLITE_OK; /* Return code */
230795 const char *z = zQuery; /* Special query text */
230796 int n; /* Number of bytes in text at z */
230797
230798 while( z[0]==' ' ) z++;
230799 for(n=0; z[n] && z[n]!=' '; n++);
230800
230801 assert( pTab->p.base.zErrMsg==0 );
230802 pCsr->ePlan = FTS5_PLAN_SPECIAL;
230803
230804 if( n==5 && 0==sqlite3_strnicmp(zLeft: "reads", zRight: z, N: n) ){
230805 pCsr->iSpecial = sqlite3Fts5IndexReads(p: pTab->p.pIndex);
230806 }
230807 else if( n==2 && 0==sqlite3_strnicmp(zLeft: "id", zRight: z, N: n) ){
230808 pCsr->iSpecial = pCsr->iCsrId;
230809 }
230810 else{
230811 /* An unrecognized directive. Return an error message. */
230812 pTab->p.base.zErrMsg = sqlite3_mprintf(zFormat: "unknown special query: %.*s", n, z);
230813 rc = SQLITE_ERROR;
230814 }
230815
230816 return rc;
230817}
230818
230819/*
230820** Search for an auxiliary function named zName that can be used with table
230821** pTab. If one is found, return a pointer to the corresponding Fts5Auxiliary
230822** structure. Otherwise, if no such function exists, return NULL.
230823*/
230824static Fts5Auxiliary *fts5FindAuxiliary(Fts5FullTable *pTab, const char *zName){
230825 Fts5Auxiliary *pAux;
230826
230827 for(pAux=pTab->pGlobal->pAux; pAux; pAux=pAux->pNext){
230828 if( sqlite3_stricmp(zLeft: zName, zRight: pAux->zFunc)==0 ) return pAux;
230829 }
230830
230831 /* No function of the specified name was found. Return 0. */
230832 return 0;
230833}
230834
230835
230836static int fts5FindRankFunction(Fts5Cursor *pCsr){
230837 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
230838 Fts5Config *pConfig = pTab->p.pConfig;
230839 int rc = SQLITE_OK;
230840 Fts5Auxiliary *pAux = 0;
230841 const char *zRank = pCsr->zRank;
230842 const char *zRankArgs = pCsr->zRankArgs;
230843
230844 if( zRankArgs ){
230845 char *zSql = sqlite3Fts5Mprintf(pRc: &rc, zFmt: "SELECT %s", zRankArgs);
230846 if( zSql ){
230847 sqlite3_stmt *pStmt = 0;
230848 rc = sqlite3_prepare_v3(db: pConfig->db, zSql, nBytes: -1,
230849 SQLITE_PREPARE_PERSISTENT, ppStmt: &pStmt, pzTail: 0);
230850 sqlite3_free(p: zSql);
230851 assert( rc==SQLITE_OK || pCsr->pRankArgStmt==0 );
230852 if( rc==SQLITE_OK ){
230853 if( SQLITE_ROW==sqlite3_step(pStmt) ){
230854 sqlite3_int64 nByte;
230855 pCsr->nRankArg = sqlite3_column_count(pStmt);
230856 nByte = sizeof(sqlite3_value*)*pCsr->nRankArg;
230857 pCsr->apRankArg = (sqlite3_value**)sqlite3Fts5MallocZero(pRc: &rc, nByte);
230858 if( rc==SQLITE_OK ){
230859 int i;
230860 for(i=0; i<pCsr->nRankArg; i++){
230861 pCsr->apRankArg[i] = sqlite3_column_value(pStmt, i);
230862 }
230863 }
230864 pCsr->pRankArgStmt = pStmt;
230865 }else{
230866 rc = sqlite3_finalize(pStmt);
230867 assert( rc!=SQLITE_OK );
230868 }
230869 }
230870 }
230871 }
230872
230873 if( rc==SQLITE_OK ){
230874 pAux = fts5FindAuxiliary(pTab, zName: zRank);
230875 if( pAux==0 ){
230876 assert( pTab->p.base.zErrMsg==0 );
230877 pTab->p.base.zErrMsg = sqlite3_mprintf(zFormat: "no such function: %s", zRank);
230878 rc = SQLITE_ERROR;
230879 }
230880 }
230881
230882 pCsr->pRank = pAux;
230883 return rc;
230884}
230885
230886
230887static int fts5CursorParseRank(
230888 Fts5Config *pConfig,
230889 Fts5Cursor *pCsr,
230890 sqlite3_value *pRank
230891){
230892 int rc = SQLITE_OK;
230893 if( pRank ){
230894 const char *z = (const char*)sqlite3_value_text(pVal: pRank);
230895 char *zRank = 0;
230896 char *zRankArgs = 0;
230897
230898 if( z==0 ){
230899 if( sqlite3_value_type(pVal: pRank)==SQLITE_NULL ) rc = SQLITE_ERROR;
230900 }else{
230901 rc = sqlite3Fts5ConfigParseRank(zIn: z, pzRank: &zRank, pzRankArgs: &zRankArgs);
230902 }
230903 if( rc==SQLITE_OK ){
230904 pCsr->zRank = zRank;
230905 pCsr->zRankArgs = zRankArgs;
230906 CsrFlagSet(pCsr, FTS5CSR_FREE_ZRANK);
230907 }else if( rc==SQLITE_ERROR ){
230908 pCsr->base.pVtab->zErrMsg = sqlite3_mprintf(
230909 zFormat: "parse error in rank function: %s", z
230910 );
230911 }
230912 }else{
230913 if( pConfig->zRank ){
230914 pCsr->zRank = (char*)pConfig->zRank;
230915 pCsr->zRankArgs = (char*)pConfig->zRankArgs;
230916 }else{
230917 pCsr->zRank = (char*)FTS5_DEFAULT_RANK;
230918 pCsr->zRankArgs = 0;
230919 }
230920 }
230921 return rc;
230922}
230923
230924static i64 fts5GetRowidLimit(sqlite3_value *pVal, i64 iDefault){
230925 if( pVal ){
230926 int eType = sqlite3_value_numeric_type(pVal);
230927 if( eType==SQLITE_INTEGER ){
230928 return sqlite3_value_int64(pVal);
230929 }
230930 }
230931 return iDefault;
230932}
230933
230934/*
230935** This is the xFilter interface for the virtual table. See
230936** the virtual table xFilter method documentation for additional
230937** information.
230938**
230939** There are three possible query strategies:
230940**
230941** 1. Full-text search using a MATCH operator.
230942** 2. A by-rowid lookup.
230943** 3. A full-table scan.
230944*/
230945static int fts5FilterMethod(
230946 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
230947 int idxNum, /* Strategy index */
230948 const char *idxStr, /* Unused */
230949 int nVal, /* Number of elements in apVal */
230950 sqlite3_value **apVal /* Arguments for the indexing scheme */
230951){
230952 Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab);
230953 Fts5Config *pConfig = pTab->p.pConfig;
230954 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
230955 int rc = SQLITE_OK; /* Error code */
230956 int bDesc; /* True if ORDER BY [rank|rowid] DESC */
230957 int bOrderByRank; /* True if ORDER BY rank */
230958 sqlite3_value *pRank = 0; /* rank MATCH ? expression (or NULL) */
230959 sqlite3_value *pRowidEq = 0; /* rowid = ? expression (or NULL) */
230960 sqlite3_value *pRowidLe = 0; /* rowid <= ? expression (or NULL) */
230961 sqlite3_value *pRowidGe = 0; /* rowid >= ? expression (or NULL) */
230962 int iCol; /* Column on LHS of MATCH operator */
230963 char **pzErrmsg = pConfig->pzErrmsg;
230964 int i;
230965 int iIdxStr = 0;
230966 Fts5Expr *pExpr = 0;
230967
230968 if( pConfig->bLock ){
230969 pTab->p.base.zErrMsg = sqlite3_mprintf(
230970 zFormat: "recursively defined fts5 content table"
230971 );
230972 return SQLITE_ERROR;
230973 }
230974
230975 if( pCsr->ePlan ){
230976 fts5FreeCursorComponents(pCsr);
230977 memset(s: &pCsr->ePlan, c: 0, n: sizeof(Fts5Cursor) - ((u8*)&pCsr->ePlan-(u8*)pCsr));
230978 }
230979
230980 assert( pCsr->pStmt==0 );
230981 assert( pCsr->pExpr==0 );
230982 assert( pCsr->csrflags==0 );
230983 assert( pCsr->pRank==0 );
230984 assert( pCsr->zRank==0 );
230985 assert( pCsr->zRankArgs==0 );
230986 assert( pTab->pSortCsr==0 || nVal==0 );
230987
230988 assert( pzErrmsg==0 || pzErrmsg==&pTab->p.base.zErrMsg );
230989 pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
230990
230991 /* Decode the arguments passed through to this function. */
230992 for(i=0; i<nVal; i++){
230993 switch( idxStr[iIdxStr++] ){
230994 case 'r':
230995 pRank = apVal[i];
230996 break;
230997 case 'M': {
230998 const char *zText = (const char*)sqlite3_value_text(pVal: apVal[i]);
230999 if( zText==0 ) zText = "";
231000 iCol = 0;
231001 do{
231002 iCol = iCol*10 + (idxStr[iIdxStr]-'0');
231003 iIdxStr++;
231004 }while( idxStr[iIdxStr]>='0' && idxStr[iIdxStr]<='9' );
231005
231006 if( zText[0]=='*' ){
231007 /* The user has issued a query of the form "MATCH '*...'". This
231008 ** indicates that the MATCH expression is not a full text query,
231009 ** but a request for an internal parameter. */
231010 rc = fts5SpecialMatch(pTab, pCsr, zQuery: &zText[1]);
231011 goto filter_out;
231012 }else{
231013 char **pzErr = &pTab->p.base.zErrMsg;
231014 rc = sqlite3Fts5ExprNew(pConfig, bPhraseToAnd: 0, iCol, zExpr: zText, ppNew: &pExpr, pzErr);
231015 if( rc==SQLITE_OK ){
231016 rc = sqlite3Fts5ExprAnd(pp1: &pCsr->pExpr, p2: pExpr);
231017 pExpr = 0;
231018 }
231019 if( rc!=SQLITE_OK ) goto filter_out;
231020 }
231021
231022 break;
231023 }
231024 case 'L':
231025 case 'G': {
231026 int bGlob = (idxStr[iIdxStr-1]=='G');
231027 const char *zText = (const char*)sqlite3_value_text(pVal: apVal[i]);
231028 iCol = 0;
231029 do{
231030 iCol = iCol*10 + (idxStr[iIdxStr]-'0');
231031 iIdxStr++;
231032 }while( idxStr[iIdxStr]>='0' && idxStr[iIdxStr]<='9' );
231033 if( zText ){
231034 rc = sqlite3Fts5ExprPattern(pConfig, bGlob, iCol, zText, pp: &pExpr);
231035 }
231036 if( rc==SQLITE_OK ){
231037 rc = sqlite3Fts5ExprAnd(pp1: &pCsr->pExpr, p2: pExpr);
231038 pExpr = 0;
231039 }
231040 if( rc!=SQLITE_OK ) goto filter_out;
231041 break;
231042 }
231043 case '=':
231044 pRowidEq = apVal[i];
231045 break;
231046 case '<':
231047 pRowidLe = apVal[i];
231048 break;
231049 default: assert( idxStr[iIdxStr-1]=='>' );
231050 pRowidGe = apVal[i];
231051 break;
231052 }
231053 }
231054 bOrderByRank = ((idxNum & FTS5_BI_ORDER_RANK) ? 1 : 0);
231055 pCsr->bDesc = bDesc = ((idxNum & FTS5_BI_ORDER_DESC) ? 1 : 0);
231056
231057 /* Set the cursor upper and lower rowid limits. Only some strategies
231058 ** actually use them. This is ok, as the xBestIndex() method leaves the
231059 ** sqlite3_index_constraint.omit flag clear for range constraints
231060 ** on the rowid field. */
231061 if( pRowidEq ){
231062 pRowidLe = pRowidGe = pRowidEq;
231063 }
231064 if( bDesc ){
231065 pCsr->iFirstRowid = fts5GetRowidLimit(pVal: pRowidLe, LARGEST_INT64);
231066 pCsr->iLastRowid = fts5GetRowidLimit(pVal: pRowidGe, SMALLEST_INT64);
231067 }else{
231068 pCsr->iLastRowid = fts5GetRowidLimit(pVal: pRowidLe, LARGEST_INT64);
231069 pCsr->iFirstRowid = fts5GetRowidLimit(pVal: pRowidGe, SMALLEST_INT64);
231070 }
231071
231072 if( pTab->pSortCsr ){
231073 /* If pSortCsr is non-NULL, then this call is being made as part of
231074 ** processing for a "... MATCH <expr> ORDER BY rank" query (ePlan is
231075 ** set to FTS5_PLAN_SORTED_MATCH). pSortCsr is the cursor that will
231076 ** return results to the user for this query. The current cursor
231077 ** (pCursor) is used to execute the query issued by function
231078 ** fts5CursorFirstSorted() above. */
231079 assert( pRowidEq==0 && pRowidLe==0 && pRowidGe==0 && pRank==0 );
231080 assert( nVal==0 && bOrderByRank==0 && bDesc==0 );
231081 assert( pCsr->iLastRowid==LARGEST_INT64 );
231082 assert( pCsr->iFirstRowid==SMALLEST_INT64 );
231083 if( pTab->pSortCsr->bDesc ){
231084 pCsr->iLastRowid = pTab->pSortCsr->iFirstRowid;
231085 pCsr->iFirstRowid = pTab->pSortCsr->iLastRowid;
231086 }else{
231087 pCsr->iLastRowid = pTab->pSortCsr->iLastRowid;
231088 pCsr->iFirstRowid = pTab->pSortCsr->iFirstRowid;
231089 }
231090 pCsr->ePlan = FTS5_PLAN_SOURCE;
231091 pCsr->pExpr = pTab->pSortCsr->pExpr;
231092 rc = fts5CursorFirst(pTab, pCsr, bDesc);
231093 }else if( pCsr->pExpr ){
231094 rc = fts5CursorParseRank(pConfig, pCsr, pRank);
231095 if( rc==SQLITE_OK ){
231096 if( bOrderByRank ){
231097 pCsr->ePlan = FTS5_PLAN_SORTED_MATCH;
231098 rc = fts5CursorFirstSorted(pTab, pCsr, bDesc);
231099 }else{
231100 pCsr->ePlan = FTS5_PLAN_MATCH;
231101 rc = fts5CursorFirst(pTab, pCsr, bDesc);
231102 }
231103 }
231104 }else if( pConfig->zContent==0 ){
231105 *pConfig->pzErrmsg = sqlite3_mprintf(
231106 zFormat: "%s: table does not support scanning", pConfig->zName
231107 );
231108 rc = SQLITE_ERROR;
231109 }else{
231110 /* This is either a full-table scan (ePlan==FTS5_PLAN_SCAN) or a lookup
231111 ** by rowid (ePlan==FTS5_PLAN_ROWID). */
231112 pCsr->ePlan = (pRowidEq ? FTS5_PLAN_ROWID : FTS5_PLAN_SCAN);
231113 rc = sqlite3Fts5StorageStmt(
231114 p: pTab->pStorage, eStmt: fts5StmtType(pCsr), &pCsr->pStmt, &pTab->p.base.zErrMsg
231115 );
231116 if( rc==SQLITE_OK ){
231117 if( pRowidEq!=0 ){
231118 assert( pCsr->ePlan==FTS5_PLAN_ROWID );
231119 sqlite3_bind_value(pStmt: pCsr->pStmt, i: 1, pValue: pRowidEq);
231120 }else{
231121 sqlite3_bind_int64(pStmt: pCsr->pStmt, i: 1, iValue: pCsr->iFirstRowid);
231122 sqlite3_bind_int64(pStmt: pCsr->pStmt, i: 2, iValue: pCsr->iLastRowid);
231123 }
231124 rc = fts5NextMethod(pCursor);
231125 }
231126 }
231127
231128 filter_out:
231129 sqlite3Fts5ExprFree(p: pExpr);
231130 pConfig->pzErrmsg = pzErrmsg;
231131 return rc;
231132}
231133
231134/*
231135** This is the xEof method of the virtual table. SQLite calls this
231136** routine to find out if it has reached the end of a result set.
231137*/
231138static int fts5EofMethod(sqlite3_vtab_cursor *pCursor){
231139 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
231140 return (CsrFlagTest(pCsr, FTS5CSR_EOF) ? 1 : 0);
231141}
231142
231143/*
231144** Return the rowid that the cursor currently points to.
231145*/
231146static i64 fts5CursorRowid(Fts5Cursor *pCsr){
231147 assert( pCsr->ePlan==FTS5_PLAN_MATCH
231148 || pCsr->ePlan==FTS5_PLAN_SORTED_MATCH
231149 || pCsr->ePlan==FTS5_PLAN_SOURCE
231150 );
231151 if( pCsr->pSorter ){
231152 return pCsr->pSorter->iRowid;
231153 }else{
231154 return sqlite3Fts5ExprRowid(p: pCsr->pExpr);
231155 }
231156}
231157
231158/*
231159** This is the xRowid method. The SQLite core calls this routine to
231160** retrieve the rowid for the current row of the result set. fts5
231161** exposes %_content.rowid as the rowid for the virtual table. The
231162** rowid should be written to *pRowid.
231163*/
231164static int fts5RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
231165 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
231166 int ePlan = pCsr->ePlan;
231167
231168 assert( CsrFlagTest(pCsr, FTS5CSR_EOF)==0 );
231169 switch( ePlan ){
231170 case FTS5_PLAN_SPECIAL:
231171 *pRowid = 0;
231172 break;
231173
231174 case FTS5_PLAN_SOURCE:
231175 case FTS5_PLAN_MATCH:
231176 case FTS5_PLAN_SORTED_MATCH:
231177 *pRowid = fts5CursorRowid(pCsr);
231178 break;
231179
231180 default:
231181 *pRowid = sqlite3_column_int64(pStmt: pCsr->pStmt, i: 0);
231182 break;
231183 }
231184
231185 return SQLITE_OK;
231186}
231187
231188/*
231189** If the cursor requires seeking (bSeekRequired flag is set), seek it.
231190** Return SQLITE_OK if no error occurs, or an SQLite error code otherwise.
231191**
231192** If argument bErrormsg is true and an error occurs, an error message may
231193** be left in sqlite3_vtab.zErrMsg.
231194*/
231195static int fts5SeekCursor(Fts5Cursor *pCsr, int bErrormsg){
231196 int rc = SQLITE_OK;
231197
231198 /* If the cursor does not yet have a statement handle, obtain one now. */
231199 if( pCsr->pStmt==0 ){
231200 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
231201 int eStmt = fts5StmtType(pCsr);
231202 rc = sqlite3Fts5StorageStmt(
231203 p: pTab->pStorage, eStmt, &pCsr->pStmt, (bErrormsg?&pTab->p.base.zErrMsg:0)
231204 );
231205 assert( rc!=SQLITE_OK || pTab->p.base.zErrMsg==0 );
231206 assert( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_CONTENT) );
231207 }
231208
231209 if( rc==SQLITE_OK && CsrFlagTest(pCsr, FTS5CSR_REQUIRE_CONTENT) ){
231210 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
231211 assert( pCsr->pExpr );
231212 sqlite3_reset(pStmt: pCsr->pStmt);
231213 sqlite3_bind_int64(pStmt: pCsr->pStmt, i: 1, iValue: fts5CursorRowid(pCsr));
231214 pTab->pConfig->bLock++;
231215 rc = sqlite3_step(pStmt: pCsr->pStmt);
231216 pTab->pConfig->bLock--;
231217 if( rc==SQLITE_ROW ){
231218 rc = SQLITE_OK;
231219 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_CONTENT);
231220 }else{
231221 rc = sqlite3_reset(pStmt: pCsr->pStmt);
231222 if( rc==SQLITE_OK ){
231223 rc = FTS5_CORRUPT;
231224 }else if( pTab->pConfig->pzErrmsg ){
231225 *pTab->pConfig->pzErrmsg = sqlite3_mprintf(
231226 zFormat: "%s", sqlite3_errmsg(db: pTab->pConfig->db)
231227 );
231228 }
231229 }
231230 }
231231 return rc;
231232}
231233
231234static void fts5SetVtabError(Fts5FullTable *p, const char *zFormat, ...){
231235 va_list ap; /* ... printf arguments */
231236 va_start(ap, zFormat);
231237 assert( p->p.base.zErrMsg==0 );
231238 p->p.base.zErrMsg = sqlite3_vmprintf(zFormat, ap);
231239 va_end(ap);
231240}
231241
231242/*
231243** This function is called to handle an FTS INSERT command. In other words,
231244** an INSERT statement of the form:
231245**
231246** INSERT INTO fts(fts) VALUES($pCmd)
231247** INSERT INTO fts(fts, rank) VALUES($pCmd, $pVal)
231248**
231249** Argument pVal is the value assigned to column "fts" by the INSERT
231250** statement. This function returns SQLITE_OK if successful, or an SQLite
231251** error code if an error occurs.
231252**
231253** The commands implemented by this function are documented in the "Special
231254** INSERT Directives" section of the documentation. It should be updated if
231255** more commands are added to this function.
231256*/
231257static int fts5SpecialInsert(
231258 Fts5FullTable *pTab, /* Fts5 table object */
231259 const char *zCmd, /* Text inserted into table-name column */
231260 sqlite3_value *pVal /* Value inserted into rank column */
231261){
231262 Fts5Config *pConfig = pTab->p.pConfig;
231263 int rc = SQLITE_OK;
231264 int bError = 0;
231265
231266 if( 0==sqlite3_stricmp(zLeft: "delete-all", zRight: zCmd) ){
231267 if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
231268 fts5SetVtabError(p: pTab,
231269 zFormat: "'delete-all' may only be used with a "
231270 "contentless or external content fts5 table"
231271 );
231272 rc = SQLITE_ERROR;
231273 }else{
231274 rc = sqlite3Fts5StorageDeleteAll(p: pTab->pStorage);
231275 }
231276 }else if( 0==sqlite3_stricmp(zLeft: "rebuild", zRight: zCmd) ){
231277 if( pConfig->eContent==FTS5_CONTENT_NONE ){
231278 fts5SetVtabError(p: pTab,
231279 zFormat: "'rebuild' may not be used with a contentless fts5 table"
231280 );
231281 rc = SQLITE_ERROR;
231282 }else{
231283 rc = sqlite3Fts5StorageRebuild(p: pTab->pStorage);
231284 }
231285 }else if( 0==sqlite3_stricmp(zLeft: "optimize", zRight: zCmd) ){
231286 rc = sqlite3Fts5StorageOptimize(p: pTab->pStorage);
231287 }else if( 0==sqlite3_stricmp(zLeft: "merge", zRight: zCmd) ){
231288 int nMerge = sqlite3_value_int(pVal);
231289 rc = sqlite3Fts5StorageMerge(p: pTab->pStorage, nMerge);
231290 }else if( 0==sqlite3_stricmp(zLeft: "integrity-check", zRight: zCmd) ){
231291 int iArg = sqlite3_value_int(pVal);
231292 rc = sqlite3Fts5StorageIntegrity(p: pTab->pStorage, iArg);
231293#ifdef SQLITE_DEBUG
231294 }else if( 0==sqlite3_stricmp("prefix-index", zCmd) ){
231295 pConfig->bPrefixIndex = sqlite3_value_int(pVal);
231296#endif
231297 }else{
231298 rc = sqlite3Fts5IndexLoadConfig(p: pTab->p.pIndex);
231299 if( rc==SQLITE_OK ){
231300 rc = sqlite3Fts5ConfigSetValue(pConfig: pTab->p.pConfig, zKey: zCmd, pVal, pbBadkey: &bError);
231301 }
231302 if( rc==SQLITE_OK ){
231303 if( bError ){
231304 rc = SQLITE_ERROR;
231305 }else{
231306 rc = sqlite3Fts5StorageConfigValue(p: pTab->pStorage, zCmd, pVal, 0);
231307 }
231308 }
231309 }
231310 return rc;
231311}
231312
231313static int fts5SpecialDelete(
231314 Fts5FullTable *pTab,
231315 sqlite3_value **apVal
231316){
231317 int rc = SQLITE_OK;
231318 int eType1 = sqlite3_value_type(pVal: apVal[1]);
231319 if( eType1==SQLITE_INTEGER ){
231320 sqlite3_int64 iDel = sqlite3_value_int64(pVal: apVal[1]);
231321 rc = sqlite3Fts5StorageDelete(p: pTab->pStorage, iDel, &apVal[2]);
231322 }
231323 return rc;
231324}
231325
231326static void fts5StorageInsert(
231327 int *pRc,
231328 Fts5FullTable *pTab,
231329 sqlite3_value **apVal,
231330 i64 *piRowid
231331){
231332 int rc = *pRc;
231333 if( rc==SQLITE_OK ){
231334 rc = sqlite3Fts5StorageContentInsert(p: pTab->pStorage, apVal, piRowid);
231335 }
231336 if( rc==SQLITE_OK ){
231337 rc = sqlite3Fts5StorageIndexInsert(p: pTab->pStorage, apVal, *piRowid);
231338 }
231339 *pRc = rc;
231340}
231341
231342/*
231343** This function is the implementation of the xUpdate callback used by
231344** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
231345** inserted, updated or deleted.
231346**
231347** A delete specifies a single argument - the rowid of the row to remove.
231348**
231349** Update and insert operations pass:
231350**
231351** 1. The "old" rowid, or NULL.
231352** 2. The "new" rowid.
231353** 3. Values for each of the nCol matchable columns.
231354** 4. Values for the two hidden columns (<tablename> and "rank").
231355*/
231356static int fts5UpdateMethod(
231357 sqlite3_vtab *pVtab, /* Virtual table handle */
231358 int nArg, /* Size of argument array */
231359 sqlite3_value **apVal, /* Array of arguments */
231360 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
231361){
231362 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
231363 Fts5Config *pConfig = pTab->p.pConfig;
231364 int eType0; /* value_type() of apVal[0] */
231365 int rc = SQLITE_OK; /* Return code */
231366
231367 /* A transaction must be open when this is called. */
231368 assert( pTab->ts.eState==1 );
231369
231370 assert( pVtab->zErrMsg==0 );
231371 assert( nArg==1 || nArg==(2+pConfig->nCol+2) );
231372 assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER
231373 || sqlite3_value_type(apVal[0])==SQLITE_NULL
231374 );
231375 assert( pTab->p.pConfig->pzErrmsg==0 );
231376 pTab->p.pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
231377
231378 /* Put any active cursors into REQUIRE_SEEK state. */
231379 fts5TripCursors(pTab);
231380
231381 eType0 = sqlite3_value_type(pVal: apVal[0]);
231382 if( eType0==SQLITE_NULL
231383 && sqlite3_value_type(pVal: apVal[2+pConfig->nCol])!=SQLITE_NULL
231384 ){
231385 /* A "special" INSERT op. These are handled separately. */
231386 const char *z = (const char*)sqlite3_value_text(pVal: apVal[2+pConfig->nCol]);
231387 if( pConfig->eContent!=FTS5_CONTENT_NORMAL
231388 && 0==sqlite3_stricmp(zLeft: "delete", zRight: z)
231389 ){
231390 rc = fts5SpecialDelete(pTab, apVal);
231391 }else{
231392 rc = fts5SpecialInsert(pTab, zCmd: z, pVal: apVal[2 + pConfig->nCol + 1]);
231393 }
231394 }else{
231395 /* A regular INSERT, UPDATE or DELETE statement. The trick here is that
231396 ** any conflict on the rowid value must be detected before any
231397 ** modifications are made to the database file. There are 4 cases:
231398 **
231399 ** 1) DELETE
231400 ** 2) UPDATE (rowid not modified)
231401 ** 3) UPDATE (rowid modified)
231402 ** 4) INSERT
231403 **
231404 ** Cases 3 and 4 may violate the rowid constraint.
231405 */
231406 int eConflict = SQLITE_ABORT;
231407 if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
231408 eConflict = sqlite3_vtab_on_conflict(db: pConfig->db);
231409 }
231410
231411 assert( eType0==SQLITE_INTEGER || eType0==SQLITE_NULL );
231412 assert( nArg!=1 || eType0==SQLITE_INTEGER );
231413
231414 /* Filter out attempts to run UPDATE or DELETE on contentless tables.
231415 ** This is not suported. */
231416 if( eType0==SQLITE_INTEGER && fts5IsContentless(pTab) ){
231417 pTab->p.base.zErrMsg = sqlite3_mprintf(
231418 zFormat: "cannot %s contentless fts5 table: %s",
231419 (nArg>1 ? "UPDATE" : "DELETE from"), pConfig->zName
231420 );
231421 rc = SQLITE_ERROR;
231422 }
231423
231424 /* DELETE */
231425 else if( nArg==1 ){
231426 i64 iDel = sqlite3_value_int64(pVal: apVal[0]); /* Rowid to delete */
231427 rc = sqlite3Fts5StorageDelete(p: pTab->pStorage, iDel, 0);
231428 }
231429
231430 /* INSERT or UPDATE */
231431 else{
231432 int eType1 = sqlite3_value_numeric_type(pVal: apVal[1]);
231433
231434 if( eType1!=SQLITE_INTEGER && eType1!=SQLITE_NULL ){
231435 rc = SQLITE_MISMATCH;
231436 }
231437
231438 else if( eType0!=SQLITE_INTEGER ){
231439 /* If this is a REPLACE, first remove the current entry (if any) */
231440 if( eConflict==SQLITE_REPLACE && eType1==SQLITE_INTEGER ){
231441 i64 iNew = sqlite3_value_int64(pVal: apVal[1]); /* Rowid to delete */
231442 rc = sqlite3Fts5StorageDelete(p: pTab->pStorage, iNew, 0);
231443 }
231444 fts5StorageInsert(pRc: &rc, pTab, apVal, piRowid: pRowid);
231445 }
231446
231447 /* UPDATE */
231448 else{
231449 i64 iOld = sqlite3_value_int64(pVal: apVal[0]); /* Old rowid */
231450 i64 iNew = sqlite3_value_int64(pVal: apVal[1]); /* New rowid */
231451 if( eType1==SQLITE_INTEGER && iOld!=iNew ){
231452 if( eConflict==SQLITE_REPLACE ){
231453 rc = sqlite3Fts5StorageDelete(p: pTab->pStorage, iOld, 0);
231454 if( rc==SQLITE_OK ){
231455 rc = sqlite3Fts5StorageDelete(p: pTab->pStorage, iNew, 0);
231456 }
231457 fts5StorageInsert(pRc: &rc, pTab, apVal, piRowid: pRowid);
231458 }else{
231459 rc = sqlite3Fts5StorageContentInsert(p: pTab->pStorage, apVal, pRowid);
231460 if( rc==SQLITE_OK ){
231461 rc = sqlite3Fts5StorageDelete(p: pTab->pStorage, iOld, 0);
231462 }
231463 if( rc==SQLITE_OK ){
231464 rc = sqlite3Fts5StorageIndexInsert(p: pTab->pStorage, apVal,*pRowid);
231465 }
231466 }
231467 }else{
231468 rc = sqlite3Fts5StorageDelete(p: pTab->pStorage, iOld, 0);
231469 fts5StorageInsert(pRc: &rc, pTab, apVal, piRowid: pRowid);
231470 }
231471 }
231472 }
231473 }
231474
231475 pTab->p.pConfig->pzErrmsg = 0;
231476 return rc;
231477}
231478
231479/*
231480** Implementation of xSync() method.
231481*/
231482static int fts5SyncMethod(sqlite3_vtab *pVtab){
231483 int rc;
231484 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
231485 fts5CheckTransactionState(pTab, FTS5_SYNC, 0);
231486 pTab->p.pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
231487 fts5TripCursors(pTab);
231488 rc = sqlite3Fts5StorageSync(p: pTab->pStorage);
231489 pTab->p.pConfig->pzErrmsg = 0;
231490 return rc;
231491}
231492
231493/*
231494** Implementation of xBegin() method.
231495*/
231496static int fts5BeginMethod(sqlite3_vtab *pVtab){
231497 fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_BEGIN, 0);
231498 fts5NewTransaction(pTab: (Fts5FullTable*)pVtab);
231499 return SQLITE_OK;
231500}
231501
231502/*
231503** Implementation of xCommit() method. This is a no-op. The contents of
231504** the pending-terms hash-table have already been flushed into the database
231505** by fts5SyncMethod().
231506*/
231507static int fts5CommitMethod(sqlite3_vtab *pVtab){
231508 UNUSED_PARAM(pVtab); /* Call below is a no-op for NDEBUG builds */
231509 fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_COMMIT, 0);
231510 return SQLITE_OK;
231511}
231512
231513/*
231514** Implementation of xRollback(). Discard the contents of the pending-terms
231515** hash-table. Any changes made to the database are reverted by SQLite.
231516*/
231517static int fts5RollbackMethod(sqlite3_vtab *pVtab){
231518 int rc;
231519 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
231520 fts5CheckTransactionState(pTab, FTS5_ROLLBACK, 0);
231521 rc = sqlite3Fts5StorageRollback(p: pTab->pStorage);
231522 return rc;
231523}
231524
231525static int fts5CsrPoslist(Fts5Cursor*, int, const u8**, int*);
231526
231527static void *fts5ApiUserData(Fts5Context *pCtx){
231528 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
231529 return pCsr->pAux->pUserData;
231530}
231531
231532static int fts5ApiColumnCount(Fts5Context *pCtx){
231533 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
231534 return ((Fts5Table*)(pCsr->base.pVtab))->pConfig->nCol;
231535}
231536
231537static int fts5ApiColumnTotalSize(
231538 Fts5Context *pCtx,
231539 int iCol,
231540 sqlite3_int64 *pnToken
231541){
231542 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
231543 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
231544 return sqlite3Fts5StorageSize(p: pTab->pStorage, iCol, pnAvg: pnToken);
231545}
231546
231547static int fts5ApiRowCount(Fts5Context *pCtx, i64 *pnRow){
231548 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
231549 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
231550 return sqlite3Fts5StorageRowCount(p: pTab->pStorage, pnRow);
231551}
231552
231553static int fts5ApiTokenize(
231554 Fts5Context *pCtx,
231555 const char *pText, int nText,
231556 void *pUserData,
231557 int (*xToken)(void*, int, const char*, int, int, int)
231558){
231559 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
231560 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
231561 return sqlite3Fts5Tokenize(
231562 pConfig: pTab->pConfig, FTS5_TOKENIZE_AUX, pText, nText, pCtx: pUserData, xToken
231563 );
231564}
231565
231566static int fts5ApiPhraseCount(Fts5Context *pCtx){
231567 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
231568 return sqlite3Fts5ExprPhraseCount(pExpr: pCsr->pExpr);
231569}
231570
231571static int fts5ApiPhraseSize(Fts5Context *pCtx, int iPhrase){
231572 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
231573 return sqlite3Fts5ExprPhraseSize(pExpr: pCsr->pExpr, iPhrase);
231574}
231575
231576static int fts5ApiColumnText(
231577 Fts5Context *pCtx,
231578 int iCol,
231579 const char **pz,
231580 int *pn
231581){
231582 int rc = SQLITE_OK;
231583 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
231584 if( fts5IsContentless(pTab: (Fts5FullTable*)(pCsr->base.pVtab))
231585 || pCsr->ePlan==FTS5_PLAN_SPECIAL
231586 ){
231587 *pz = 0;
231588 *pn = 0;
231589 }else{
231590 rc = fts5SeekCursor(pCsr, bErrormsg: 0);
231591 if( rc==SQLITE_OK ){
231592 *pz = (const char*)sqlite3_column_text(pStmt: pCsr->pStmt, i: iCol+1);
231593 *pn = sqlite3_column_bytes(pStmt: pCsr->pStmt, i: iCol+1);
231594 }
231595 }
231596 return rc;
231597}
231598
231599static int fts5CsrPoslist(
231600 Fts5Cursor *pCsr,
231601 int iPhrase,
231602 const u8 **pa,
231603 int *pn
231604){
231605 Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
231606 int rc = SQLITE_OK;
231607 int bLive = (pCsr->pSorter==0);
231608
231609 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_POSLIST) ){
231610
231611 if( pConfig->eDetail!=FTS5_DETAIL_FULL ){
231612 Fts5PoslistPopulator *aPopulator;
231613 int i;
231614 aPopulator = sqlite3Fts5ExprClearPoslists(pExpr: pCsr->pExpr, bLive);
231615 if( aPopulator==0 ) rc = SQLITE_NOMEM;
231616 for(i=0; i<pConfig->nCol && rc==SQLITE_OK; i++){
231617 int n; const char *z;
231618 rc = fts5ApiColumnText(pCtx: (Fts5Context*)pCsr, iCol: i, pz: &z, pn: &n);
231619 if( rc==SQLITE_OK ){
231620 rc = sqlite3Fts5ExprPopulatePoslists(
231621 pConfig, pExpr: pCsr->pExpr, aPopulator, iCol: i, z, n
231622 );
231623 }
231624 }
231625 sqlite3_free(p: aPopulator);
231626
231627 if( pCsr->pSorter ){
231628 sqlite3Fts5ExprCheckPoslists(pExpr: pCsr->pExpr, iRowid: pCsr->pSorter->iRowid);
231629 }
231630 }
231631 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_POSLIST);
231632 }
231633
231634 if( pCsr->pSorter && pConfig->eDetail==FTS5_DETAIL_FULL ){
231635 Fts5Sorter *pSorter = pCsr->pSorter;
231636 int i1 = (iPhrase==0 ? 0 : pSorter->aIdx[iPhrase-1]);
231637 *pn = pSorter->aIdx[iPhrase] - i1;
231638 *pa = &pSorter->aPoslist[i1];
231639 }else{
231640 *pn = sqlite3Fts5ExprPoslist(pExpr: pCsr->pExpr, iPhrase, pa);
231641 }
231642
231643 return rc;
231644}
231645
231646/*
231647** Ensure that the Fts5Cursor.nInstCount and aInst[] variables are populated
231648** correctly for the current view. Return SQLITE_OK if successful, or an
231649** SQLite error code otherwise.
231650*/
231651static int fts5CacheInstArray(Fts5Cursor *pCsr){
231652 int rc = SQLITE_OK;
231653 Fts5PoslistReader *aIter; /* One iterator for each phrase */
231654 int nIter; /* Number of iterators/phrases */
231655 int nCol = ((Fts5Table*)pCsr->base.pVtab)->pConfig->nCol;
231656
231657 nIter = sqlite3Fts5ExprPhraseCount(pExpr: pCsr->pExpr);
231658 if( pCsr->aInstIter==0 ){
231659 sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * nIter;
231660 pCsr->aInstIter = (Fts5PoslistReader*)sqlite3Fts5MallocZero(pRc: &rc, nByte);
231661 }
231662 aIter = pCsr->aInstIter;
231663
231664 if( aIter ){
231665 int nInst = 0; /* Number instances seen so far */
231666 int i;
231667
231668 /* Initialize all iterators */
231669 for(i=0; i<nIter && rc==SQLITE_OK; i++){
231670 const u8 *a;
231671 int n;
231672 rc = fts5CsrPoslist(pCsr, iPhrase: i, pa: &a, pn: &n);
231673 if( rc==SQLITE_OK ){
231674 sqlite3Fts5PoslistReaderInit(a, n, pIter: &aIter[i]);
231675 }
231676 }
231677
231678 if( rc==SQLITE_OK ){
231679 while( 1 ){
231680 int *aInst;
231681 int iBest = -1;
231682 for(i=0; i<nIter; i++){
231683 if( (aIter[i].bEof==0)
231684 && (iBest<0 || aIter[i].iPos<aIter[iBest].iPos)
231685 ){
231686 iBest = i;
231687 }
231688 }
231689 if( iBest<0 ) break;
231690
231691 nInst++;
231692 if( nInst>=pCsr->nInstAlloc ){
231693 int nNewSize = pCsr->nInstAlloc ? pCsr->nInstAlloc*2 : 32;
231694 aInst = (int*)sqlite3_realloc64(
231695 pOld: pCsr->aInst, n: nNewSize*sizeof(int)*3
231696 );
231697 if( aInst ){
231698 pCsr->aInst = aInst;
231699 pCsr->nInstAlloc = nNewSize;
231700 }else{
231701 nInst--;
231702 rc = SQLITE_NOMEM;
231703 break;
231704 }
231705 }
231706
231707 aInst = &pCsr->aInst[3 * (nInst-1)];
231708 aInst[0] = iBest;
231709 aInst[1] = FTS5_POS2COLUMN(aIter[iBest].iPos);
231710 aInst[2] = FTS5_POS2OFFSET(aIter[iBest].iPos);
231711 if( aInst[1]<0 || aInst[1]>=nCol ){
231712 rc = FTS5_CORRUPT;
231713 break;
231714 }
231715 sqlite3Fts5PoslistReaderNext(pIter: &aIter[iBest]);
231716 }
231717 }
231718
231719 pCsr->nInstCount = nInst;
231720 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_INST);
231721 }
231722 return rc;
231723}
231724
231725static int fts5ApiInstCount(Fts5Context *pCtx, int *pnInst){
231726 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
231727 int rc = SQLITE_OK;
231728 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_INST)==0
231729 || SQLITE_OK==(rc = fts5CacheInstArray(pCsr)) ){
231730 *pnInst = pCsr->nInstCount;
231731 }
231732 return rc;
231733}
231734
231735static int fts5ApiInst(
231736 Fts5Context *pCtx,
231737 int iIdx,
231738 int *piPhrase,
231739 int *piCol,
231740 int *piOff
231741){
231742 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
231743 int rc = SQLITE_OK;
231744 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_INST)==0
231745 || SQLITE_OK==(rc = fts5CacheInstArray(pCsr))
231746 ){
231747 if( iIdx<0 || iIdx>=pCsr->nInstCount ){
231748 rc = SQLITE_RANGE;
231749#if 0
231750 }else if( fts5IsOffsetless((Fts5Table*)pCsr->base.pVtab) ){
231751 *piPhrase = pCsr->aInst[iIdx*3];
231752 *piCol = pCsr->aInst[iIdx*3 + 2];
231753 *piOff = -1;
231754#endif
231755 }else{
231756 *piPhrase = pCsr->aInst[iIdx*3];
231757 *piCol = pCsr->aInst[iIdx*3 + 1];
231758 *piOff = pCsr->aInst[iIdx*3 + 2];
231759 }
231760 }
231761 return rc;
231762}
231763
231764static sqlite3_int64 fts5ApiRowid(Fts5Context *pCtx){
231765 return fts5CursorRowid(pCsr: (Fts5Cursor*)pCtx);
231766}
231767
231768static int fts5ColumnSizeCb(
231769 void *pContext, /* Pointer to int */
231770 int tflags,
231771 const char *pUnused, /* Buffer containing token */
231772 int nUnused, /* Size of token in bytes */
231773 int iUnused1, /* Start offset of token */
231774 int iUnused2 /* End offset of token */
231775){
231776 int *pCnt = (int*)pContext;
231777 UNUSED_PARAM2(pUnused, nUnused);
231778 UNUSED_PARAM2(iUnused1, iUnused2);
231779 if( (tflags & FTS5_TOKEN_COLOCATED)==0 ){
231780 (*pCnt)++;
231781 }
231782 return SQLITE_OK;
231783}
231784
231785static int fts5ApiColumnSize(Fts5Context *pCtx, int iCol, int *pnToken){
231786 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
231787 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
231788 Fts5Config *pConfig = pTab->p.pConfig;
231789 int rc = SQLITE_OK;
231790
231791 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_DOCSIZE) ){
231792 if( pConfig->bColumnsize ){
231793 i64 iRowid = fts5CursorRowid(pCsr);
231794 rc = sqlite3Fts5StorageDocsize(p: pTab->pStorage, iRowid, aCol: pCsr->aColumnSize);
231795 }else if( pConfig->zContent==0 ){
231796 int i;
231797 for(i=0; i<pConfig->nCol; i++){
231798 if( pConfig->abUnindexed[i]==0 ){
231799 pCsr->aColumnSize[i] = -1;
231800 }
231801 }
231802 }else{
231803 int i;
231804 for(i=0; rc==SQLITE_OK && i<pConfig->nCol; i++){
231805 if( pConfig->abUnindexed[i]==0 ){
231806 const char *z; int n;
231807 void *p = (void*)(&pCsr->aColumnSize[i]);
231808 pCsr->aColumnSize[i] = 0;
231809 rc = fts5ApiColumnText(pCtx, iCol: i, pz: &z, pn: &n);
231810 if( rc==SQLITE_OK ){
231811 rc = sqlite3Fts5Tokenize(
231812 pConfig, FTS5_TOKENIZE_AUX, pText: z, nText: n, pCtx: p, xToken: fts5ColumnSizeCb
231813 );
231814 }
231815 }
231816 }
231817 }
231818 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_DOCSIZE);
231819 }
231820 if( iCol<0 ){
231821 int i;
231822 *pnToken = 0;
231823 for(i=0; i<pConfig->nCol; i++){
231824 *pnToken += pCsr->aColumnSize[i];
231825 }
231826 }else if( iCol<pConfig->nCol ){
231827 *pnToken = pCsr->aColumnSize[iCol];
231828 }else{
231829 *pnToken = 0;
231830 rc = SQLITE_RANGE;
231831 }
231832 return rc;
231833}
231834
231835/*
231836** Implementation of the xSetAuxdata() method.
231837*/
231838static int fts5ApiSetAuxdata(
231839 Fts5Context *pCtx, /* Fts5 context */
231840 void *pPtr, /* Pointer to save as auxdata */
231841 void(*xDelete)(void*) /* Destructor for pPtr (or NULL) */
231842){
231843 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
231844 Fts5Auxdata *pData;
231845
231846 /* Search through the cursors list of Fts5Auxdata objects for one that
231847 ** corresponds to the currently executing auxiliary function. */
231848 for(pData=pCsr->pAuxdata; pData; pData=pData->pNext){
231849 if( pData->pAux==pCsr->pAux ) break;
231850 }
231851
231852 if( pData ){
231853 if( pData->xDelete ){
231854 pData->xDelete(pData->pPtr);
231855 }
231856 }else{
231857 int rc = SQLITE_OK;
231858 pData = (Fts5Auxdata*)sqlite3Fts5MallocZero(pRc: &rc, nByte: sizeof(Fts5Auxdata));
231859 if( pData==0 ){
231860 if( xDelete ) xDelete(pPtr);
231861 return rc;
231862 }
231863 pData->pAux = pCsr->pAux;
231864 pData->pNext = pCsr->pAuxdata;
231865 pCsr->pAuxdata = pData;
231866 }
231867
231868 pData->xDelete = xDelete;
231869 pData->pPtr = pPtr;
231870 return SQLITE_OK;
231871}
231872
231873static void *fts5ApiGetAuxdata(Fts5Context *pCtx, int bClear){
231874 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
231875 Fts5Auxdata *pData;
231876 void *pRet = 0;
231877
231878 for(pData=pCsr->pAuxdata; pData; pData=pData->pNext){
231879 if( pData->pAux==pCsr->pAux ) break;
231880 }
231881
231882 if( pData ){
231883 pRet = pData->pPtr;
231884 if( bClear ){
231885 pData->pPtr = 0;
231886 pData->xDelete = 0;
231887 }
231888 }
231889
231890 return pRet;
231891}
231892
231893static void fts5ApiPhraseNext(
231894 Fts5Context *pUnused,
231895 Fts5PhraseIter *pIter,
231896 int *piCol, int *piOff
231897){
231898 UNUSED_PARAM(pUnused);
231899 if( pIter->a>=pIter->b ){
231900 *piCol = -1;
231901 *piOff = -1;
231902 }else{
231903 int iVal;
231904 pIter->a += fts5GetVarint32(pIter->a, iVal);
231905 if( iVal==1 ){
231906 pIter->a += fts5GetVarint32(pIter->a, iVal);
231907 *piCol = iVal;
231908 *piOff = 0;
231909 pIter->a += fts5GetVarint32(pIter->a, iVal);
231910 }
231911 *piOff += (iVal-2);
231912 }
231913}
231914
231915static int fts5ApiPhraseFirst(
231916 Fts5Context *pCtx,
231917 int iPhrase,
231918 Fts5PhraseIter *pIter,
231919 int *piCol, int *piOff
231920){
231921 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
231922 int n;
231923 int rc = fts5CsrPoslist(pCsr, iPhrase, pa: &pIter->a, pn: &n);
231924 if( rc==SQLITE_OK ){
231925 assert( pIter->a || n==0 );
231926 pIter->b = (pIter->a ? &pIter->a[n] : 0);
231927 *piCol = 0;
231928 *piOff = 0;
231929 fts5ApiPhraseNext(pUnused: pCtx, pIter, piCol, piOff);
231930 }
231931 return rc;
231932}
231933
231934static void fts5ApiPhraseNextColumn(
231935 Fts5Context *pCtx,
231936 Fts5PhraseIter *pIter,
231937 int *piCol
231938){
231939 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
231940 Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
231941
231942 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
231943 if( pIter->a>=pIter->b ){
231944 *piCol = -1;
231945 }else{
231946 int iIncr;
231947 pIter->a += fts5GetVarint32(&pIter->a[0], iIncr);
231948 *piCol += (iIncr-2);
231949 }
231950 }else{
231951 while( 1 ){
231952 int dummy;
231953 if( pIter->a>=pIter->b ){
231954 *piCol = -1;
231955 return;
231956 }
231957 if( pIter->a[0]==0x01 ) break;
231958 pIter->a += fts5GetVarint32(pIter->a, dummy);
231959 }
231960 pIter->a += 1 + fts5GetVarint32(&pIter->a[1], *piCol);
231961 }
231962}
231963
231964static int fts5ApiPhraseFirstColumn(
231965 Fts5Context *pCtx,
231966 int iPhrase,
231967 Fts5PhraseIter *pIter,
231968 int *piCol
231969){
231970 int rc = SQLITE_OK;
231971 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
231972 Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
231973
231974 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
231975 Fts5Sorter *pSorter = pCsr->pSorter;
231976 int n;
231977 if( pSorter ){
231978 int i1 = (iPhrase==0 ? 0 : pSorter->aIdx[iPhrase-1]);
231979 n = pSorter->aIdx[iPhrase] - i1;
231980 pIter->a = &pSorter->aPoslist[i1];
231981 }else{
231982 rc = sqlite3Fts5ExprPhraseCollist(pExpr: pCsr->pExpr, iPhrase, ppCollist: &pIter->a, pnCollist: &n);
231983 }
231984 if( rc==SQLITE_OK ){
231985 assert( pIter->a || n==0 );
231986 pIter->b = (pIter->a ? &pIter->a[n] : 0);
231987 *piCol = 0;
231988 fts5ApiPhraseNextColumn(pCtx, pIter, piCol);
231989 }
231990 }else{
231991 int n;
231992 rc = fts5CsrPoslist(pCsr, iPhrase, pa: &pIter->a, pn: &n);
231993 if( rc==SQLITE_OK ){
231994 assert( pIter->a || n==0 );
231995 pIter->b = (pIter->a ? &pIter->a[n] : 0);
231996 if( n<=0 ){
231997 *piCol = -1;
231998 }else if( pIter->a[0]==0x01 ){
231999 pIter->a += 1 + fts5GetVarint32(&pIter->a[1], *piCol);
232000 }else{
232001 *piCol = 0;
232002 }
232003 }
232004 }
232005
232006 return rc;
232007}
232008
232009
232010static int fts5ApiQueryPhrase(Fts5Context*, int, void*,
232011 int(*)(const Fts5ExtensionApi*, Fts5Context*, void*)
232012);
232013
232014static const Fts5ExtensionApi sFts5Api = {
232015 2, /* iVersion */
232016 fts5ApiUserData,
232017 fts5ApiColumnCount,
232018 fts5ApiRowCount,
232019 fts5ApiColumnTotalSize,
232020 fts5ApiTokenize,
232021 fts5ApiPhraseCount,
232022 fts5ApiPhraseSize,
232023 fts5ApiInstCount,
232024 fts5ApiInst,
232025 fts5ApiRowid,
232026 fts5ApiColumnText,
232027 fts5ApiColumnSize,
232028 fts5ApiQueryPhrase,
232029 fts5ApiSetAuxdata,
232030 fts5ApiGetAuxdata,
232031 fts5ApiPhraseFirst,
232032 fts5ApiPhraseNext,
232033 fts5ApiPhraseFirstColumn,
232034 fts5ApiPhraseNextColumn,
232035};
232036
232037/*
232038** Implementation of API function xQueryPhrase().
232039*/
232040static int fts5ApiQueryPhrase(
232041 Fts5Context *pCtx,
232042 int iPhrase,
232043 void *pUserData,
232044 int(*xCallback)(const Fts5ExtensionApi*, Fts5Context*, void*)
232045){
232046 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
232047 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
232048 int rc;
232049 Fts5Cursor *pNew = 0;
232050
232051 rc = fts5OpenMethod(pVTab: pCsr->base.pVtab, ppCsr: (sqlite3_vtab_cursor**)&pNew);
232052 if( rc==SQLITE_OK ){
232053 pNew->ePlan = FTS5_PLAN_MATCH;
232054 pNew->iFirstRowid = SMALLEST_INT64;
232055 pNew->iLastRowid = LARGEST_INT64;
232056 pNew->base.pVtab = (sqlite3_vtab*)pTab;
232057 rc = sqlite3Fts5ExprClonePhrase(pExpr: pCsr->pExpr, iPhrase, ppNew: &pNew->pExpr);
232058 }
232059
232060 if( rc==SQLITE_OK ){
232061 for(rc = fts5CursorFirst(pTab, pCsr: pNew, bDesc: 0);
232062 rc==SQLITE_OK && CsrFlagTest(pNew, FTS5CSR_EOF)==0;
232063 rc = fts5NextMethod(pCursor: (sqlite3_vtab_cursor*)pNew)
232064 ){
232065 rc = xCallback(&sFts5Api, (Fts5Context*)pNew, pUserData);
232066 if( rc!=SQLITE_OK ){
232067 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
232068 break;
232069 }
232070 }
232071 }
232072
232073 fts5CloseMethod(pCursor: (sqlite3_vtab_cursor*)pNew);
232074 return rc;
232075}
232076
232077static void fts5ApiInvoke(
232078 Fts5Auxiliary *pAux,
232079 Fts5Cursor *pCsr,
232080 sqlite3_context *context,
232081 int argc,
232082 sqlite3_value **argv
232083){
232084 assert( pCsr->pAux==0 );
232085 pCsr->pAux = pAux;
232086 pAux->xFunc(&sFts5Api, (Fts5Context*)pCsr, context, argc, argv);
232087 pCsr->pAux = 0;
232088}
232089
232090static Fts5Cursor *fts5CursorFromCsrid(Fts5Global *pGlobal, i64 iCsrId){
232091 Fts5Cursor *pCsr;
232092 for(pCsr=pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
232093 if( pCsr->iCsrId==iCsrId ) break;
232094 }
232095 return pCsr;
232096}
232097
232098static void fts5ApiCallback(
232099 sqlite3_context *context,
232100 int argc,
232101 sqlite3_value **argv
232102){
232103
232104 Fts5Auxiliary *pAux;
232105 Fts5Cursor *pCsr;
232106 i64 iCsrId;
232107
232108 assert( argc>=1 );
232109 pAux = (Fts5Auxiliary*)sqlite3_user_data(p: context);
232110 iCsrId = sqlite3_value_int64(pVal: argv[0]);
232111
232112 pCsr = fts5CursorFromCsrid(pGlobal: pAux->pGlobal, iCsrId);
232113 if( pCsr==0 || pCsr->ePlan==0 ){
232114 char *zErr = sqlite3_mprintf(zFormat: "no such cursor: %lld", iCsrId);
232115 sqlite3_result_error(pCtx: context, z: zErr, n: -1);
232116 sqlite3_free(p: zErr);
232117 }else{
232118 fts5ApiInvoke(pAux, pCsr, context, argc: argc-1, argv: &argv[1]);
232119 }
232120}
232121
232122
232123/*
232124** Given cursor id iId, return a pointer to the corresponding Fts5Table
232125** object. Or NULL If the cursor id does not exist.
232126*/
232127static Fts5Table *sqlite3Fts5TableFromCsrid(
232128 Fts5Global *pGlobal, /* FTS5 global context for db handle */
232129 i64 iCsrId /* Id of cursor to find */
232130){
232131 Fts5Cursor *pCsr;
232132 pCsr = fts5CursorFromCsrid(pGlobal, iCsrId);
232133 if( pCsr ){
232134 return (Fts5Table*)pCsr->base.pVtab;
232135 }
232136 return 0;
232137}
232138
232139/*
232140** Return a "position-list blob" corresponding to the current position of
232141** cursor pCsr via sqlite3_result_blob(). A position-list blob contains
232142** the current position-list for each phrase in the query associated with
232143** cursor pCsr.
232144**
232145** A position-list blob begins with (nPhrase-1) varints, where nPhrase is
232146** the number of phrases in the query. Following the varints are the
232147** concatenated position lists for each phrase, in order.
232148**
232149** The first varint (if it exists) contains the size of the position list
232150** for phrase 0. The second (same disclaimer) contains the size of position
232151** list 1. And so on. There is no size field for the final position list,
232152** as it can be derived from the total size of the blob.
232153*/
232154static int fts5PoslistBlob(sqlite3_context *pCtx, Fts5Cursor *pCsr){
232155 int i;
232156 int rc = SQLITE_OK;
232157 int nPhrase = sqlite3Fts5ExprPhraseCount(pExpr: pCsr->pExpr);
232158 Fts5Buffer val;
232159
232160 memset(s: &val, c: 0, n: sizeof(Fts5Buffer));
232161 switch( ((Fts5Table*)(pCsr->base.pVtab))->pConfig->eDetail ){
232162 case FTS5_DETAIL_FULL:
232163
232164 /* Append the varints */
232165 for(i=0; i<(nPhrase-1); i++){
232166 const u8 *dummy;
232167 int nByte = sqlite3Fts5ExprPoslist(pExpr: pCsr->pExpr, iPhrase: i, pa: &dummy);
232168 sqlite3Fts5BufferAppendVarint(pRc: &rc, pBuf: &val, iVal: nByte);
232169 }
232170
232171 /* Append the position lists */
232172 for(i=0; i<nPhrase; i++){
232173 const u8 *pPoslist;
232174 int nPoslist;
232175 nPoslist = sqlite3Fts5ExprPoslist(pExpr: pCsr->pExpr, iPhrase: i, pa: &pPoslist);
232176 sqlite3Fts5BufferAppendBlob(pRc: &rc, pBuf: &val, nData: nPoslist, pData: pPoslist);
232177 }
232178 break;
232179
232180 case FTS5_DETAIL_COLUMNS:
232181
232182 /* Append the varints */
232183 for(i=0; rc==SQLITE_OK && i<(nPhrase-1); i++){
232184 const u8 *dummy;
232185 int nByte;
232186 rc = sqlite3Fts5ExprPhraseCollist(pExpr: pCsr->pExpr, iPhrase: i, ppCollist: &dummy, pnCollist: &nByte);
232187 sqlite3Fts5BufferAppendVarint(pRc: &rc, pBuf: &val, iVal: nByte);
232188 }
232189
232190 /* Append the position lists */
232191 for(i=0; rc==SQLITE_OK && i<nPhrase; i++){
232192 const u8 *pPoslist;
232193 int nPoslist;
232194 rc = sqlite3Fts5ExprPhraseCollist(pExpr: pCsr->pExpr, iPhrase: i, ppCollist: &pPoslist, pnCollist: &nPoslist);
232195 sqlite3Fts5BufferAppendBlob(pRc: &rc, pBuf: &val, nData: nPoslist, pData: pPoslist);
232196 }
232197 break;
232198
232199 default:
232200 break;
232201 }
232202
232203 sqlite3_result_blob(pCtx, z: val.p, n: val.n, xDel: sqlite3_free);
232204 return rc;
232205}
232206
232207/*
232208** This is the xColumn method, called by SQLite to request a value from
232209** the row that the supplied cursor currently points to.
232210*/
232211static int fts5ColumnMethod(
232212 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
232213 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
232214 int iCol /* Index of column to read value from */
232215){
232216 Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab);
232217 Fts5Config *pConfig = pTab->p.pConfig;
232218 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
232219 int rc = SQLITE_OK;
232220
232221 assert( CsrFlagTest(pCsr, FTS5CSR_EOF)==0 );
232222
232223 if( pCsr->ePlan==FTS5_PLAN_SPECIAL ){
232224 if( iCol==pConfig->nCol ){
232225 sqlite3_result_int64(pCtx, iVal: pCsr->iSpecial);
232226 }
232227 }else
232228
232229 if( iCol==pConfig->nCol ){
232230 /* User is requesting the value of the special column with the same name
232231 ** as the table. Return the cursor integer id number. This value is only
232232 ** useful in that it may be passed as the first argument to an FTS5
232233 ** auxiliary function. */
232234 sqlite3_result_int64(pCtx, iVal: pCsr->iCsrId);
232235 }else if( iCol==pConfig->nCol+1 ){
232236
232237 /* The value of the "rank" column. */
232238 if( pCsr->ePlan==FTS5_PLAN_SOURCE ){
232239 fts5PoslistBlob(pCtx, pCsr);
232240 }else if(
232241 pCsr->ePlan==FTS5_PLAN_MATCH
232242 || pCsr->ePlan==FTS5_PLAN_SORTED_MATCH
232243 ){
232244 if( pCsr->pRank || SQLITE_OK==(rc = fts5FindRankFunction(pCsr)) ){
232245 fts5ApiInvoke(pAux: pCsr->pRank, pCsr, context: pCtx, argc: pCsr->nRankArg, argv: pCsr->apRankArg);
232246 }
232247 }
232248 }else if( !fts5IsContentless(pTab) ){
232249 pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
232250 rc = fts5SeekCursor(pCsr, bErrormsg: 1);
232251 if( rc==SQLITE_OK ){
232252 sqlite3_result_value(pCtx, pValue: sqlite3_column_value(pStmt: pCsr->pStmt, i: iCol+1));
232253 }
232254 pConfig->pzErrmsg = 0;
232255 }
232256 return rc;
232257}
232258
232259
232260/*
232261** This routine implements the xFindFunction method for the FTS3
232262** virtual table.
232263*/
232264static int fts5FindFunctionMethod(
232265 sqlite3_vtab *pVtab, /* Virtual table handle */
232266 int nUnused, /* Number of SQL function arguments */
232267 const char *zName, /* Name of SQL function */
232268 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
232269 void **ppArg /* OUT: User data for *pxFunc */
232270){
232271 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
232272 Fts5Auxiliary *pAux;
232273
232274 UNUSED_PARAM(nUnused);
232275 pAux = fts5FindAuxiliary(pTab, zName);
232276 if( pAux ){
232277 *pxFunc = fts5ApiCallback;
232278 *ppArg = (void*)pAux;
232279 return 1;
232280 }
232281
232282 /* No function of the specified name was found. Return 0. */
232283 return 0;
232284}
232285
232286/*
232287** Implementation of FTS5 xRename method. Rename an fts5 table.
232288*/
232289static int fts5RenameMethod(
232290 sqlite3_vtab *pVtab, /* Virtual table handle */
232291 const char *zName /* New name of table */
232292){
232293 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
232294 return sqlite3Fts5StorageRename(pTab->pStorage, zName);
232295}
232296
232297static int sqlite3Fts5FlushToDisk(Fts5Table *pTab){
232298 fts5TripCursors(pTab: (Fts5FullTable*)pTab);
232299 return sqlite3Fts5StorageSync(p: ((Fts5FullTable*)pTab)->pStorage);
232300}
232301
232302/*
232303** The xSavepoint() method.
232304**
232305** Flush the contents of the pending-terms table to disk.
232306*/
232307static int fts5SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
232308 UNUSED_PARAM(iSavepoint); /* Call below is a no-op for NDEBUG builds */
232309 fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_SAVEPOINT, iSavepoint);
232310 return sqlite3Fts5FlushToDisk(pTab: (Fts5Table*)pVtab);
232311}
232312
232313/*
232314** The xRelease() method.
232315**
232316** This is a no-op.
232317*/
232318static int fts5ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
232319 UNUSED_PARAM(iSavepoint); /* Call below is a no-op for NDEBUG builds */
232320 fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_RELEASE, iSavepoint);
232321 return sqlite3Fts5FlushToDisk(pTab: (Fts5Table*)pVtab);
232322}
232323
232324/*
232325** The xRollbackTo() method.
232326**
232327** Discard the contents of the pending terms table.
232328*/
232329static int fts5RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
232330 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
232331 UNUSED_PARAM(iSavepoint); /* Call below is a no-op for NDEBUG builds */
232332 fts5CheckTransactionState(pTab, FTS5_ROLLBACKTO, iSavepoint);
232333 fts5TripCursors(pTab);
232334 return sqlite3Fts5StorageRollback(p: pTab->pStorage);
232335}
232336
232337/*
232338** Register a new auxiliary function with global context pGlobal.
232339*/
232340static int fts5CreateAux(
232341 fts5_api *pApi, /* Global context (one per db handle) */
232342 const char *zName, /* Name of new function */
232343 void *pUserData, /* User data for aux. function */
232344 fts5_extension_function xFunc, /* Aux. function implementation */
232345 void(*xDestroy)(void*) /* Destructor for pUserData */
232346){
232347 Fts5Global *pGlobal = (Fts5Global*)pApi;
232348 int rc = sqlite3_overload_function(db: pGlobal->db, zName, nArg: -1);
232349 if( rc==SQLITE_OK ){
232350 Fts5Auxiliary *pAux;
232351 sqlite3_int64 nName; /* Size of zName in bytes, including \0 */
232352 sqlite3_int64 nByte; /* Bytes of space to allocate */
232353
232354 nName = strlen(s: zName) + 1;
232355 nByte = sizeof(Fts5Auxiliary) + nName;
232356 pAux = (Fts5Auxiliary*)sqlite3_malloc64(n: nByte);
232357 if( pAux ){
232358 memset(s: pAux, c: 0, n: (size_t)nByte);
232359 pAux->zFunc = (char*)&pAux[1];
232360 memcpy(dest: pAux->zFunc, src: zName, n: nName);
232361 pAux->pGlobal = pGlobal;
232362 pAux->pUserData = pUserData;
232363 pAux->xFunc = xFunc;
232364 pAux->xDestroy = xDestroy;
232365 pAux->pNext = pGlobal->pAux;
232366 pGlobal->pAux = pAux;
232367 }else{
232368 rc = SQLITE_NOMEM;
232369 }
232370 }
232371
232372 return rc;
232373}
232374
232375/*
232376** Register a new tokenizer. This is the implementation of the
232377** fts5_api.xCreateTokenizer() method.
232378*/
232379static int fts5CreateTokenizer(
232380 fts5_api *pApi, /* Global context (one per db handle) */
232381 const char *zName, /* Name of new function */
232382 void *pUserData, /* User data for aux. function */
232383 fts5_tokenizer *pTokenizer, /* Tokenizer implementation */
232384 void(*xDestroy)(void*) /* Destructor for pUserData */
232385){
232386 Fts5Global *pGlobal = (Fts5Global*)pApi;
232387 Fts5TokenizerModule *pNew;
232388 sqlite3_int64 nName; /* Size of zName and its \0 terminator */
232389 sqlite3_int64 nByte; /* Bytes of space to allocate */
232390 int rc = SQLITE_OK;
232391
232392 nName = strlen(s: zName) + 1;
232393 nByte = sizeof(Fts5TokenizerModule) + nName;
232394 pNew = (Fts5TokenizerModule*)sqlite3_malloc64(n: nByte);
232395 if( pNew ){
232396 memset(s: pNew, c: 0, n: (size_t)nByte);
232397 pNew->zName = (char*)&pNew[1];
232398 memcpy(dest: pNew->zName, src: zName, n: nName);
232399 pNew->pUserData = pUserData;
232400 pNew->x = *pTokenizer;
232401 pNew->xDestroy = xDestroy;
232402 pNew->pNext = pGlobal->pTok;
232403 pGlobal->pTok = pNew;
232404 if( pNew->pNext==0 ){
232405 pGlobal->pDfltTok = pNew;
232406 }
232407 }else{
232408 rc = SQLITE_NOMEM;
232409 }
232410
232411 return rc;
232412}
232413
232414static Fts5TokenizerModule *fts5LocateTokenizer(
232415 Fts5Global *pGlobal,
232416 const char *zName
232417){
232418 Fts5TokenizerModule *pMod = 0;
232419
232420 if( zName==0 ){
232421 pMod = pGlobal->pDfltTok;
232422 }else{
232423 for(pMod=pGlobal->pTok; pMod; pMod=pMod->pNext){
232424 if( sqlite3_stricmp(zLeft: zName, zRight: pMod->zName)==0 ) break;
232425 }
232426 }
232427
232428 return pMod;
232429}
232430
232431/*
232432** Find a tokenizer. This is the implementation of the
232433** fts5_api.xFindTokenizer() method.
232434*/
232435static int fts5FindTokenizer(
232436 fts5_api *pApi, /* Global context (one per db handle) */
232437 const char *zName, /* Name of new function */
232438 void **ppUserData,
232439 fts5_tokenizer *pTokenizer /* Populate this object */
232440){
232441 int rc = SQLITE_OK;
232442 Fts5TokenizerModule *pMod;
232443
232444 pMod = fts5LocateTokenizer(pGlobal: (Fts5Global*)pApi, zName);
232445 if( pMod ){
232446 *pTokenizer = pMod->x;
232447 *ppUserData = pMod->pUserData;
232448 }else{
232449 memset(s: pTokenizer, c: 0, n: sizeof(fts5_tokenizer));
232450 rc = SQLITE_ERROR;
232451 }
232452
232453 return rc;
232454}
232455
232456static int sqlite3Fts5GetTokenizer(
232457 Fts5Global *pGlobal,
232458 const char **azArg,
232459 int nArg,
232460 Fts5Config *pConfig,
232461 char **pzErr
232462){
232463 Fts5TokenizerModule *pMod;
232464 int rc = SQLITE_OK;
232465
232466 pMod = fts5LocateTokenizer(pGlobal, zName: nArg==0 ? 0 : azArg[0]);
232467 if( pMod==0 ){
232468 assert( nArg>0 );
232469 rc = SQLITE_ERROR;
232470 *pzErr = sqlite3_mprintf(zFormat: "no such tokenizer: %s", azArg[0]);
232471 }else{
232472 rc = pMod->x.xCreate(
232473 pMod->pUserData, (azArg?&azArg[1]:0), (nArg?nArg-1:0), &pConfig->pTok
232474 );
232475 pConfig->pTokApi = &pMod->x;
232476 if( rc!=SQLITE_OK ){
232477 if( pzErr ) *pzErr = sqlite3_mprintf(zFormat: "error in tokenizer constructor");
232478 }else{
232479 pConfig->ePattern = sqlite3Fts5TokenizerPattern(
232480 xCreate: pMod->x.xCreate, pTok: pConfig->pTok
232481 );
232482 }
232483 }
232484
232485 if( rc!=SQLITE_OK ){
232486 pConfig->pTokApi = 0;
232487 pConfig->pTok = 0;
232488 }
232489
232490 return rc;
232491}
232492
232493static void fts5ModuleDestroy(void *pCtx){
232494 Fts5TokenizerModule *pTok, *pNextTok;
232495 Fts5Auxiliary *pAux, *pNextAux;
232496 Fts5Global *pGlobal = (Fts5Global*)pCtx;
232497
232498 for(pAux=pGlobal->pAux; pAux; pAux=pNextAux){
232499 pNextAux = pAux->pNext;
232500 if( pAux->xDestroy ) pAux->xDestroy(pAux->pUserData);
232501 sqlite3_free(p: pAux);
232502 }
232503
232504 for(pTok=pGlobal->pTok; pTok; pTok=pNextTok){
232505 pNextTok = pTok->pNext;
232506 if( pTok->xDestroy ) pTok->xDestroy(pTok->pUserData);
232507 sqlite3_free(p: pTok);
232508 }
232509
232510 sqlite3_free(p: pGlobal);
232511}
232512
232513static void fts5Fts5Func(
232514 sqlite3_context *pCtx, /* Function call context */
232515 int nArg, /* Number of args */
232516 sqlite3_value **apArg /* Function arguments */
232517){
232518 Fts5Global *pGlobal = (Fts5Global*)sqlite3_user_data(p: pCtx);
232519 fts5_api **ppApi;
232520 UNUSED_PARAM(nArg);
232521 assert( nArg==1 );
232522 ppApi = (fts5_api**)sqlite3_value_pointer(pVal: apArg[0], zPType: "fts5_api_ptr");
232523 if( ppApi ) *ppApi = &pGlobal->api;
232524}
232525
232526/*
232527** Implementation of fts5_source_id() function.
232528*/
232529static void fts5SourceIdFunc(
232530 sqlite3_context *pCtx, /* Function call context */
232531 int nArg, /* Number of args */
232532 sqlite3_value **apUnused /* Function arguments */
232533){
232534 assert( nArg==0 );
232535 UNUSED_PARAM2(nArg, apUnused);
232536 sqlite3_result_text(pCtx, z: "fts5: 2022-01-06 13:25:41 872ba256cbf61d9290b571c0e6d82a20c224ca3ad82971edc46b29818d5d17a0", n: -1, SQLITE_TRANSIENT);
232537}
232538
232539/*
232540** Return true if zName is the extension on one of the shadow tables used
232541** by this module.
232542*/
232543static int fts5ShadowName(const char *zName){
232544 static const char *azName[] = {
232545 "config", "content", "data", "docsize", "idx"
232546 };
232547 unsigned int i;
232548 for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
232549 if( sqlite3_stricmp(zLeft: zName, zRight: azName[i])==0 ) return 1;
232550 }
232551 return 0;
232552}
232553
232554static int fts5Init(sqlite3 *db){
232555 static const sqlite3_module fts5Mod = {
232556 /* iVersion */ 3,
232557 /* xCreate */ fts5CreateMethod,
232558 /* xConnect */ fts5ConnectMethod,
232559 /* xBestIndex */ fts5BestIndexMethod,
232560 /* xDisconnect */ fts5DisconnectMethod,
232561 /* xDestroy */ fts5DestroyMethod,
232562 /* xOpen */ fts5OpenMethod,
232563 /* xClose */ fts5CloseMethod,
232564 /* xFilter */ fts5FilterMethod,
232565 /* xNext */ fts5NextMethod,
232566 /* xEof */ fts5EofMethod,
232567 /* xColumn */ fts5ColumnMethod,
232568 /* xRowid */ fts5RowidMethod,
232569 /* xUpdate */ fts5UpdateMethod,
232570 /* xBegin */ fts5BeginMethod,
232571 /* xSync */ fts5SyncMethod,
232572 /* xCommit */ fts5CommitMethod,
232573 /* xRollback */ fts5RollbackMethod,
232574 /* xFindFunction */ fts5FindFunctionMethod,
232575 /* xRename */ fts5RenameMethod,
232576 /* xSavepoint */ fts5SavepointMethod,
232577 /* xRelease */ fts5ReleaseMethod,
232578 /* xRollbackTo */ fts5RollbackToMethod,
232579 /* xShadowName */ fts5ShadowName
232580 };
232581
232582 int rc;
232583 Fts5Global *pGlobal = 0;
232584
232585 pGlobal = (Fts5Global*)sqlite3_malloc(n: sizeof(Fts5Global));
232586 if( pGlobal==0 ){
232587 rc = SQLITE_NOMEM;
232588 }else{
232589 void *p = (void*)pGlobal;
232590 memset(s: pGlobal, c: 0, n: sizeof(Fts5Global));
232591 pGlobal->db = db;
232592 pGlobal->api.iVersion = 2;
232593 pGlobal->api.xCreateFunction = fts5CreateAux;
232594 pGlobal->api.xCreateTokenizer = fts5CreateTokenizer;
232595 pGlobal->api.xFindTokenizer = fts5FindTokenizer;
232596 rc = sqlite3_create_module_v2(db, zName: "fts5", pModule: &fts5Mod, pAux: p, xDestroy: fts5ModuleDestroy);
232597 if( rc==SQLITE_OK ) rc = sqlite3Fts5IndexInit(db);
232598 if( rc==SQLITE_OK ) rc = sqlite3Fts5ExprInit(pGlobal, db);
232599 if( rc==SQLITE_OK ) rc = sqlite3Fts5AuxInit(pApi: &pGlobal->api);
232600 if( rc==SQLITE_OK ) rc = sqlite3Fts5TokenizerInit(&pGlobal->api);
232601 if( rc==SQLITE_OK ) rc = sqlite3Fts5VocabInit(pGlobal, db);
232602 if( rc==SQLITE_OK ){
232603 rc = sqlite3_create_function(
232604 db, zFunc: "fts5", nArg: 1, SQLITE_UTF8, p, xSFunc: fts5Fts5Func, xStep: 0, xFinal: 0
232605 );
232606 }
232607 if( rc==SQLITE_OK ){
232608 rc = sqlite3_create_function(
232609 db, zFunc: "fts5_source_id", nArg: 0, SQLITE_UTF8, p, xSFunc: fts5SourceIdFunc, xStep: 0, xFinal: 0
232610 );
232611 }
232612 }
232613
232614 /* If SQLITE_FTS5_ENABLE_TEST_MI is defined, assume that the file
232615 ** fts5_test_mi.c is compiled and linked into the executable. And call
232616 ** its entry point to enable the matchinfo() demo. */
232617#ifdef SQLITE_FTS5_ENABLE_TEST_MI
232618 if( rc==SQLITE_OK ){
232619 extern int sqlite3Fts5TestRegisterMatchinfo(sqlite3*);
232620 rc = sqlite3Fts5TestRegisterMatchinfo(db);
232621 }
232622#endif
232623
232624 return rc;
232625}
232626
232627/*
232628** The following functions are used to register the module with SQLite. If
232629** this module is being built as part of the SQLite core (SQLITE_CORE is
232630** defined), then sqlite3_open() will call sqlite3Fts5Init() directly.
232631**
232632** Or, if this module is being built as a loadable extension,
232633** sqlite3Fts5Init() is omitted and the two standard entry points
232634** sqlite3_fts_init() and sqlite3_fts5_init() defined instead.
232635*/
232636#ifndef SQLITE_CORE
232637#ifdef _WIN32
232638__declspec(dllexport)
232639#endif
232640SQLITE_API int sqlite3_fts_init(
232641 sqlite3 *db,
232642 char **pzErrMsg,
232643 const sqlite3_api_routines *pApi
232644){
232645 SQLITE_EXTENSION_INIT2(pApi);
232646 (void)pzErrMsg; /* Unused parameter */
232647 return fts5Init(db);
232648}
232649
232650#ifdef _WIN32
232651__declspec(dllexport)
232652#endif
232653SQLITE_API int sqlite3_fts5_init(
232654 sqlite3 *db,
232655 char **pzErrMsg,
232656 const sqlite3_api_routines *pApi
232657){
232658 SQLITE_EXTENSION_INIT2(pApi);
232659 (void)pzErrMsg; /* Unused parameter */
232660 return fts5Init(db);
232661}
232662#else
232663SQLITE_PRIVATE int sqlite3Fts5Init(sqlite3 *db){
232664 return fts5Init(db);
232665}
232666#endif
232667
232668/*
232669** 2014 May 31
232670**
232671** The author disclaims copyright to this source code. In place of
232672** a legal notice, here is a blessing:
232673**
232674** May you do good and not evil.
232675** May you find forgiveness for yourself and forgive others.
232676** May you share freely, never taking more than you give.
232677**
232678******************************************************************************
232679**
232680*/
232681
232682
232683
232684/* #include "fts5Int.h" */
232685
232686struct Fts5Storage {
232687 Fts5Config *pConfig;
232688 Fts5Index *pIndex;
232689 int bTotalsValid; /* True if nTotalRow/aTotalSize[] are valid */
232690 i64 nTotalRow; /* Total number of rows in FTS table */
232691 i64 *aTotalSize; /* Total sizes of each column */
232692 sqlite3_stmt *aStmt[11];
232693};
232694
232695
232696#if FTS5_STMT_SCAN_ASC!=0
232697# error "FTS5_STMT_SCAN_ASC mismatch"
232698#endif
232699#if FTS5_STMT_SCAN_DESC!=1
232700# error "FTS5_STMT_SCAN_DESC mismatch"
232701#endif
232702#if FTS5_STMT_LOOKUP!=2
232703# error "FTS5_STMT_LOOKUP mismatch"
232704#endif
232705
232706#define FTS5_STMT_INSERT_CONTENT 3
232707#define FTS5_STMT_REPLACE_CONTENT 4
232708#define FTS5_STMT_DELETE_CONTENT 5
232709#define FTS5_STMT_REPLACE_DOCSIZE 6
232710#define FTS5_STMT_DELETE_DOCSIZE 7
232711#define FTS5_STMT_LOOKUP_DOCSIZE 8
232712#define FTS5_STMT_REPLACE_CONFIG 9
232713#define FTS5_STMT_SCAN 10
232714
232715/*
232716** Prepare the two insert statements - Fts5Storage.pInsertContent and
232717** Fts5Storage.pInsertDocsize - if they have not already been prepared.
232718** Return SQLITE_OK if successful, or an SQLite error code if an error
232719** occurs.
232720*/
232721static int fts5StorageGetStmt(
232722 Fts5Storage *p, /* Storage handle */
232723 int eStmt, /* FTS5_STMT_XXX constant */
232724 sqlite3_stmt **ppStmt, /* OUT: Prepared statement handle */
232725 char **pzErrMsg /* OUT: Error message (if any) */
232726){
232727 int rc = SQLITE_OK;
232728
232729 /* If there is no %_docsize table, there should be no requests for
232730 ** statements to operate on it. */
232731 assert( p->pConfig->bColumnsize || (
232732 eStmt!=FTS5_STMT_REPLACE_DOCSIZE
232733 && eStmt!=FTS5_STMT_DELETE_DOCSIZE
232734 && eStmt!=FTS5_STMT_LOOKUP_DOCSIZE
232735 ));
232736
232737 assert( eStmt>=0 && eStmt<ArraySize(p->aStmt) );
232738 if( p->aStmt[eStmt]==0 ){
232739 const char *azStmt[] = {
232740 "SELECT %s FROM %s T WHERE T.%Q >= ? AND T.%Q <= ? ORDER BY T.%Q ASC",
232741 "SELECT %s FROM %s T WHERE T.%Q <= ? AND T.%Q >= ? ORDER BY T.%Q DESC",
232742 "SELECT %s FROM %s T WHERE T.%Q=?", /* LOOKUP */
232743
232744 "INSERT INTO %Q.'%q_content' VALUES(%s)", /* INSERT_CONTENT */
232745 "REPLACE INTO %Q.'%q_content' VALUES(%s)", /* REPLACE_CONTENT */
232746 "DELETE FROM %Q.'%q_content' WHERE id=?", /* DELETE_CONTENT */
232747 "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)", /* REPLACE_DOCSIZE */
232748 "DELETE FROM %Q.'%q_docsize' WHERE id=?", /* DELETE_DOCSIZE */
232749
232750 "SELECT sz FROM %Q.'%q_docsize' WHERE id=?", /* LOOKUP_DOCSIZE */
232751
232752 "REPLACE INTO %Q.'%q_config' VALUES(?,?)", /* REPLACE_CONFIG */
232753 "SELECT %s FROM %s AS T", /* SCAN */
232754 };
232755 Fts5Config *pC = p->pConfig;
232756 char *zSql = 0;
232757
232758 switch( eStmt ){
232759 case FTS5_STMT_SCAN:
232760 zSql = sqlite3_mprintf(zFormat: azStmt[eStmt],
232761 pC->zContentExprlist, pC->zContent
232762 );
232763 break;
232764
232765 case FTS5_STMT_SCAN_ASC:
232766 case FTS5_STMT_SCAN_DESC:
232767 zSql = sqlite3_mprintf(zFormat: azStmt[eStmt], pC->zContentExprlist,
232768 pC->zContent, pC->zContentRowid, pC->zContentRowid,
232769 pC->zContentRowid
232770 );
232771 break;
232772
232773 case FTS5_STMT_LOOKUP:
232774 zSql = sqlite3_mprintf(zFormat: azStmt[eStmt],
232775 pC->zContentExprlist, pC->zContent, pC->zContentRowid
232776 );
232777 break;
232778
232779 case FTS5_STMT_INSERT_CONTENT:
232780 case FTS5_STMT_REPLACE_CONTENT: {
232781 int nCol = pC->nCol + 1;
232782 char *zBind;
232783 int i;
232784
232785 zBind = sqlite3_malloc64(n: 1 + nCol*2);
232786 if( zBind ){
232787 for(i=0; i<nCol; i++){
232788 zBind[i*2] = '?';
232789 zBind[i*2 + 1] = ',';
232790 }
232791 zBind[i*2-1] = '\0';
232792 zSql = sqlite3_mprintf(zFormat: azStmt[eStmt], pC->zDb, pC->zName, zBind);
232793 sqlite3_free(p: zBind);
232794 }
232795 break;
232796 }
232797
232798 default:
232799 zSql = sqlite3_mprintf(zFormat: azStmt[eStmt], pC->zDb, pC->zName);
232800 break;
232801 }
232802
232803 if( zSql==0 ){
232804 rc = SQLITE_NOMEM;
232805 }else{
232806 int f = SQLITE_PREPARE_PERSISTENT;
232807 if( eStmt>FTS5_STMT_LOOKUP ) f |= SQLITE_PREPARE_NO_VTAB;
232808 p->pConfig->bLock++;
232809 rc = sqlite3_prepare_v3(db: pC->db, zSql, nBytes: -1, prepFlags: f, ppStmt: &p->aStmt[eStmt], pzTail: 0);
232810 p->pConfig->bLock--;
232811 sqlite3_free(p: zSql);
232812 if( rc!=SQLITE_OK && pzErrMsg ){
232813 *pzErrMsg = sqlite3_mprintf(zFormat: "%s", sqlite3_errmsg(db: pC->db));
232814 }
232815 }
232816 }
232817
232818 *ppStmt = p->aStmt[eStmt];
232819 sqlite3_reset(pStmt: *ppStmt);
232820 return rc;
232821}
232822
232823
232824static int fts5ExecPrintf(
232825 sqlite3 *db,
232826 char **pzErr,
232827 const char *zFormat,
232828 ...
232829){
232830 int rc;
232831 va_list ap; /* ... printf arguments */
232832 char *zSql;
232833
232834 va_start(ap, zFormat);
232835 zSql = sqlite3_vmprintf(zFormat, ap);
232836
232837 if( zSql==0 ){
232838 rc = SQLITE_NOMEM;
232839 }else{
232840 rc = sqlite3_exec(db, zSql, xCallback: 0, pArg: 0, pzErrMsg: pzErr);
232841 sqlite3_free(p: zSql);
232842 }
232843
232844 va_end(ap);
232845 return rc;
232846}
232847
232848/*
232849** Drop all shadow tables. Return SQLITE_OK if successful or an SQLite error
232850** code otherwise.
232851*/
232852static int sqlite3Fts5DropAll(Fts5Config *pConfig){
232853 int rc = fts5ExecPrintf(db: pConfig->db, pzErr: 0,
232854 zFormat: "DROP TABLE IF EXISTS %Q.'%q_data';"
232855 "DROP TABLE IF EXISTS %Q.'%q_idx';"
232856 "DROP TABLE IF EXISTS %Q.'%q_config';",
232857 pConfig->zDb, pConfig->zName,
232858 pConfig->zDb, pConfig->zName,
232859 pConfig->zDb, pConfig->zName
232860 );
232861 if( rc==SQLITE_OK && pConfig->bColumnsize ){
232862 rc = fts5ExecPrintf(db: pConfig->db, pzErr: 0,
232863 zFormat: "DROP TABLE IF EXISTS %Q.'%q_docsize';",
232864 pConfig->zDb, pConfig->zName
232865 );
232866 }
232867 if( rc==SQLITE_OK && pConfig->eContent==FTS5_CONTENT_NORMAL ){
232868 rc = fts5ExecPrintf(db: pConfig->db, pzErr: 0,
232869 zFormat: "DROP TABLE IF EXISTS %Q.'%q_content';",
232870 pConfig->zDb, pConfig->zName
232871 );
232872 }
232873 return rc;
232874}
232875
232876static void fts5StorageRenameOne(
232877 Fts5Config *pConfig, /* Current FTS5 configuration */
232878 int *pRc, /* IN/OUT: Error code */
232879 const char *zTail, /* Tail of table name e.g. "data", "config" */
232880 const char *zName /* New name of FTS5 table */
232881){
232882 if( *pRc==SQLITE_OK ){
232883 *pRc = fts5ExecPrintf(db: pConfig->db, pzErr: 0,
232884 zFormat: "ALTER TABLE %Q.'%q_%s' RENAME TO '%q_%s';",
232885 pConfig->zDb, pConfig->zName, zTail, zName, zTail
232886 );
232887 }
232888}
232889
232890static int sqlite3Fts5StorageRename(Fts5Storage *pStorage, const char *zName){
232891 Fts5Config *pConfig = pStorage->pConfig;
232892 int rc = sqlite3Fts5StorageSync(p: pStorage);
232893
232894 fts5StorageRenameOne(pConfig, pRc: &rc, zTail: "data", zName);
232895 fts5StorageRenameOne(pConfig, pRc: &rc, zTail: "idx", zName);
232896 fts5StorageRenameOne(pConfig, pRc: &rc, zTail: "config", zName);
232897 if( pConfig->bColumnsize ){
232898 fts5StorageRenameOne(pConfig, pRc: &rc, zTail: "docsize", zName);
232899 }
232900 if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
232901 fts5StorageRenameOne(pConfig, pRc: &rc, zTail: "content", zName);
232902 }
232903 return rc;
232904}
232905
232906/*
232907** Create the shadow table named zPost, with definition zDefn. Return
232908** SQLITE_OK if successful, or an SQLite error code otherwise.
232909*/
232910static int sqlite3Fts5CreateTable(
232911 Fts5Config *pConfig, /* FTS5 configuration */
232912 const char *zPost, /* Shadow table to create (e.g. "content") */
232913 const char *zDefn, /* Columns etc. for shadow table */
232914 int bWithout, /* True for without rowid */
232915 char **pzErr /* OUT: Error message */
232916){
232917 int rc;
232918 char *zErr = 0;
232919
232920 rc = fts5ExecPrintf(db: pConfig->db, pzErr: &zErr, zFormat: "CREATE TABLE %Q.'%q_%q'(%s)%s",
232921 pConfig->zDb, pConfig->zName, zPost, zDefn,
232922#ifndef SQLITE_FTS5_NO_WITHOUT_ROWID
232923 bWithout?" WITHOUT ROWID":
232924#endif
232925 ""
232926 );
232927 if( zErr ){
232928 *pzErr = sqlite3_mprintf(
232929 zFormat: "fts5: error creating shadow table %q_%s: %s",
232930 pConfig->zName, zPost, zErr
232931 );
232932 sqlite3_free(p: zErr);
232933 }
232934
232935 return rc;
232936}
232937
232938/*
232939** Open a new Fts5Index handle. If the bCreate argument is true, create
232940** and initialize the underlying tables
232941**
232942** If successful, set *pp to point to the new object and return SQLITE_OK.
232943** Otherwise, set *pp to NULL and return an SQLite error code.
232944*/
232945static int sqlite3Fts5StorageOpen(
232946 Fts5Config *pConfig,
232947 Fts5Index *pIndex,
232948 int bCreate,
232949 Fts5Storage **pp,
232950 char **pzErr /* OUT: Error message */
232951){
232952 int rc = SQLITE_OK;
232953 Fts5Storage *p; /* New object */
232954 sqlite3_int64 nByte; /* Bytes of space to allocate */
232955
232956 nByte = sizeof(Fts5Storage) /* Fts5Storage object */
232957 + pConfig->nCol * sizeof(i64); /* Fts5Storage.aTotalSize[] */
232958 *pp = p = (Fts5Storage*)sqlite3_malloc64(n: nByte);
232959 if( !p ) return SQLITE_NOMEM;
232960
232961 memset(s: p, c: 0, n: (size_t)nByte);
232962 p->aTotalSize = (i64*)&p[1];
232963 p->pConfig = pConfig;
232964 p->pIndex = pIndex;
232965
232966 if( bCreate ){
232967 if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
232968 int nDefn = 32 + pConfig->nCol*10;
232969 char *zDefn = sqlite3_malloc64(n: 32 + (sqlite3_int64)pConfig->nCol * 10);
232970 if( zDefn==0 ){
232971 rc = SQLITE_NOMEM;
232972 }else{
232973 int i;
232974 int iOff;
232975 sqlite3_snprintf(n: nDefn, zBuf: zDefn, zFormat: "id INTEGER PRIMARY KEY");
232976 iOff = (int)strlen(s: zDefn);
232977 for(i=0; i<pConfig->nCol; i++){
232978 sqlite3_snprintf(n: nDefn-iOff, zBuf: &zDefn[iOff], zFormat: ", c%d", i);
232979 iOff += (int)strlen(s: &zDefn[iOff]);
232980 }
232981 rc = sqlite3Fts5CreateTable(pConfig, zPost: "content", zDefn, bWithout: 0, pzErr);
232982 }
232983 sqlite3_free(p: zDefn);
232984 }
232985
232986 if( rc==SQLITE_OK && pConfig->bColumnsize ){
232987 rc = sqlite3Fts5CreateTable(
232988 pConfig, zPost: "docsize", zDefn: "id INTEGER PRIMARY KEY, sz BLOB", bWithout: 0, pzErr
232989 );
232990 }
232991 if( rc==SQLITE_OK ){
232992 rc = sqlite3Fts5CreateTable(
232993 pConfig, zPost: "config", zDefn: "k PRIMARY KEY, v", bWithout: 1, pzErr
232994 );
232995 }
232996 if( rc==SQLITE_OK ){
232997 rc = sqlite3Fts5StorageConfigValue(p, "version", 0, FTS5_CURRENT_VERSION);
232998 }
232999 }
233000
233001 if( rc ){
233002 sqlite3Fts5StorageClose(p);
233003 *pp = 0;
233004 }
233005 return rc;
233006}
233007
233008/*
233009** Close a handle opened by an earlier call to sqlite3Fts5StorageOpen().
233010*/
233011static int sqlite3Fts5StorageClose(Fts5Storage *p){
233012 int rc = SQLITE_OK;
233013 if( p ){
233014 int i;
233015
233016 /* Finalize all SQL statements */
233017 for(i=0; i<ArraySize(p->aStmt); i++){
233018 sqlite3_finalize(pStmt: p->aStmt[i]);
233019 }
233020
233021 sqlite3_free(p);
233022 }
233023 return rc;
233024}
233025
233026typedef struct Fts5InsertCtx Fts5InsertCtx;
233027struct Fts5InsertCtx {
233028 Fts5Storage *pStorage;
233029 int iCol;
233030 int szCol; /* Size of column value in tokens */
233031};
233032
233033/*
233034** Tokenization callback used when inserting tokens into the FTS index.
233035*/
233036static int fts5StorageInsertCallback(
233037 void *pContext, /* Pointer to Fts5InsertCtx object */
233038 int tflags,
233039 const char *pToken, /* Buffer containing token */
233040 int nToken, /* Size of token in bytes */
233041 int iUnused1, /* Start offset of token */
233042 int iUnused2 /* End offset of token */
233043){
233044 Fts5InsertCtx *pCtx = (Fts5InsertCtx*)pContext;
233045 Fts5Index *pIdx = pCtx->pStorage->pIndex;
233046 UNUSED_PARAM2(iUnused1, iUnused2);
233047 if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
233048 if( (tflags & FTS5_TOKEN_COLOCATED)==0 || pCtx->szCol==0 ){
233049 pCtx->szCol++;
233050 }
233051 return sqlite3Fts5IndexWrite(p: pIdx, iCol: pCtx->iCol, iPos: pCtx->szCol-1, pToken, nToken);
233052}
233053
233054/*
233055** If a row with rowid iDel is present in the %_content table, add the
233056** delete-markers to the FTS index necessary to delete it. Do not actually
233057** remove the %_content row at this time though.
233058*/
233059static int fts5StorageDeleteFromIndex(
233060 Fts5Storage *p,
233061 i64 iDel,
233062 sqlite3_value **apVal
233063){
233064 Fts5Config *pConfig = p->pConfig;
233065 sqlite3_stmt *pSeek = 0; /* SELECT to read row iDel from %_data */
233066 int rc; /* Return code */
233067 int rc2; /* sqlite3_reset() return code */
233068 int iCol;
233069 Fts5InsertCtx ctx;
233070
233071 if( apVal==0 ){
233072 rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP, ppStmt: &pSeek, pzErrMsg: 0);
233073 if( rc!=SQLITE_OK ) return rc;
233074 sqlite3_bind_int64(pStmt: pSeek, i: 1, iValue: iDel);
233075 if( sqlite3_step(pStmt: pSeek)!=SQLITE_ROW ){
233076 return sqlite3_reset(pStmt: pSeek);
233077 }
233078 }
233079
233080 ctx.pStorage = p;
233081 ctx.iCol = -1;
233082 rc = sqlite3Fts5IndexBeginWrite(p: p->pIndex, bDelete: 1, iRowid: iDel);
233083 for(iCol=1; rc==SQLITE_OK && iCol<=pConfig->nCol; iCol++){
233084 if( pConfig->abUnindexed[iCol-1]==0 ){
233085 const char *zText;
233086 int nText;
233087 assert( pSeek==0 || apVal==0 );
233088 assert( pSeek!=0 || apVal!=0 );
233089 if( pSeek ){
233090 zText = (const char*)sqlite3_column_text(pStmt: pSeek, i: iCol);
233091 nText = sqlite3_column_bytes(pStmt: pSeek, i: iCol);
233092 }else if( ALWAYS(apVal) ){
233093 zText = (const char*)sqlite3_value_text(pVal: apVal[iCol-1]);
233094 nText = sqlite3_value_bytes(pVal: apVal[iCol-1]);
233095 }else{
233096 continue;
233097 }
233098 ctx.szCol = 0;
233099 rc = sqlite3Fts5Tokenize(pConfig, FTS5_TOKENIZE_DOCUMENT,
233100 pText: zText, nText, pCtx: (void*)&ctx, xToken: fts5StorageInsertCallback
233101 );
233102 p->aTotalSize[iCol-1] -= (i64)ctx.szCol;
233103 if( p->aTotalSize[iCol-1]<0 ){
233104 rc = FTS5_CORRUPT;
233105 }
233106 }
233107 }
233108 if( rc==SQLITE_OK && p->nTotalRow<1 ){
233109 rc = FTS5_CORRUPT;
233110 }else{
233111 p->nTotalRow--;
233112 }
233113
233114 rc2 = sqlite3_reset(pStmt: pSeek);
233115 if( rc==SQLITE_OK ) rc = rc2;
233116 return rc;
233117}
233118
233119
233120/*
233121** Insert a record into the %_docsize table. Specifically, do:
233122**
233123** INSERT OR REPLACE INTO %_docsize(id, sz) VALUES(iRowid, pBuf);
233124**
233125** If there is no %_docsize table (as happens if the columnsize=0 option
233126** is specified when the FTS5 table is created), this function is a no-op.
233127*/
233128static int fts5StorageInsertDocsize(
233129 Fts5Storage *p, /* Storage module to write to */
233130 i64 iRowid, /* id value */
233131 Fts5Buffer *pBuf /* sz value */
233132){
233133 int rc = SQLITE_OK;
233134 if( p->pConfig->bColumnsize ){
233135 sqlite3_stmt *pReplace = 0;
233136 rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_DOCSIZE, ppStmt: &pReplace, pzErrMsg: 0);
233137 if( rc==SQLITE_OK ){
233138 sqlite3_bind_int64(pStmt: pReplace, i: 1, iValue: iRowid);
233139 sqlite3_bind_blob(pStmt: pReplace, i: 2, zData: pBuf->p, nData: pBuf->n, SQLITE_STATIC);
233140 sqlite3_step(pStmt: pReplace);
233141 rc = sqlite3_reset(pStmt: pReplace);
233142 sqlite3_bind_null(pStmt: pReplace, i: 2);
233143 }
233144 }
233145 return rc;
233146}
233147
233148/*
233149** Load the contents of the "averages" record from disk into the
233150** p->nTotalRow and p->aTotalSize[] variables. If successful, and if
233151** argument bCache is true, set the p->bTotalsValid flag to indicate
233152** that the contents of aTotalSize[] and nTotalRow are valid until
233153** further notice.
233154**
233155** Return SQLITE_OK if successful, or an SQLite error code if an error
233156** occurs.
233157*/
233158static int fts5StorageLoadTotals(Fts5Storage *p, int bCache){
233159 int rc = SQLITE_OK;
233160 if( p->bTotalsValid==0 ){
233161 rc = sqlite3Fts5IndexGetAverages(p: p->pIndex, pnRow: &p->nTotalRow, anSize: p->aTotalSize);
233162 p->bTotalsValid = bCache;
233163 }
233164 return rc;
233165}
233166
233167/*
233168** Store the current contents of the p->nTotalRow and p->aTotalSize[]
233169** variables in the "averages" record on disk.
233170**
233171** Return SQLITE_OK if successful, or an SQLite error code if an error
233172** occurs.
233173*/
233174static int fts5StorageSaveTotals(Fts5Storage *p){
233175 int nCol = p->pConfig->nCol;
233176 int i;
233177 Fts5Buffer buf;
233178 int rc = SQLITE_OK;
233179 memset(s: &buf, c: 0, n: sizeof(buf));
233180
233181 sqlite3Fts5BufferAppendVarint(pRc: &rc, pBuf: &buf, iVal: p->nTotalRow);
233182 for(i=0; i<nCol; i++){
233183 sqlite3Fts5BufferAppendVarint(pRc: &rc, pBuf: &buf, iVal: p->aTotalSize[i]);
233184 }
233185 if( rc==SQLITE_OK ){
233186 rc = sqlite3Fts5IndexSetAverages(p: p->pIndex, pData: buf.p, nData: buf.n);
233187 }
233188 sqlite3_free(p: buf.p);
233189
233190 return rc;
233191}
233192
233193/*
233194** Remove a row from the FTS table.
233195*/
233196static int sqlite3Fts5StorageDelete(Fts5Storage *p, i64 iDel, sqlite3_value **apVal){
233197 Fts5Config *pConfig = p->pConfig;
233198 int rc;
233199 sqlite3_stmt *pDel = 0;
233200
233201 assert( pConfig->eContent!=FTS5_CONTENT_NORMAL || apVal==0 );
233202 rc = fts5StorageLoadTotals(p, bCache: 1);
233203
233204 /* Delete the index records */
233205 if( rc==SQLITE_OK ){
233206 rc = fts5StorageDeleteFromIndex(p, iDel, apVal);
233207 }
233208
233209 /* Delete the %_docsize record */
233210 if( rc==SQLITE_OK && pConfig->bColumnsize ){
233211 rc = fts5StorageGetStmt(p, FTS5_STMT_DELETE_DOCSIZE, ppStmt: &pDel, pzErrMsg: 0);
233212 if( rc==SQLITE_OK ){
233213 sqlite3_bind_int64(pStmt: pDel, i: 1, iValue: iDel);
233214 sqlite3_step(pStmt: pDel);
233215 rc = sqlite3_reset(pStmt: pDel);
233216 }
233217 }
233218
233219 /* Delete the %_content record */
233220 if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
233221 if( rc==SQLITE_OK ){
233222 rc = fts5StorageGetStmt(p, FTS5_STMT_DELETE_CONTENT, ppStmt: &pDel, pzErrMsg: 0);
233223 }
233224 if( rc==SQLITE_OK ){
233225 sqlite3_bind_int64(pStmt: pDel, i: 1, iValue: iDel);
233226 sqlite3_step(pStmt: pDel);
233227 rc = sqlite3_reset(pStmt: pDel);
233228 }
233229 }
233230
233231 return rc;
233232}
233233
233234/*
233235** Delete all entries in the FTS5 index.
233236*/
233237static int sqlite3Fts5StorageDeleteAll(Fts5Storage *p){
233238 Fts5Config *pConfig = p->pConfig;
233239 int rc;
233240
233241 p->bTotalsValid = 0;
233242
233243 /* Delete the contents of the %_data and %_docsize tables. */
233244 rc = fts5ExecPrintf(db: pConfig->db, pzErr: 0,
233245 zFormat: "DELETE FROM %Q.'%q_data';"
233246 "DELETE FROM %Q.'%q_idx';",
233247 pConfig->zDb, pConfig->zName,
233248 pConfig->zDb, pConfig->zName
233249 );
233250 if( rc==SQLITE_OK && pConfig->bColumnsize ){
233251 rc = fts5ExecPrintf(db: pConfig->db, pzErr: 0,
233252 zFormat: "DELETE FROM %Q.'%q_docsize';",
233253 pConfig->zDb, pConfig->zName
233254 );
233255 }
233256
233257 /* Reinitialize the %_data table. This call creates the initial structure
233258 ** and averages records. */
233259 if( rc==SQLITE_OK ){
233260 rc = sqlite3Fts5IndexReinit(p: p->pIndex);
233261 }
233262 if( rc==SQLITE_OK ){
233263 rc = sqlite3Fts5StorageConfigValue(p, "version", 0, FTS5_CURRENT_VERSION);
233264 }
233265 return rc;
233266}
233267
233268static int sqlite3Fts5StorageRebuild(Fts5Storage *p){
233269 Fts5Buffer buf = {0,0,0};
233270 Fts5Config *pConfig = p->pConfig;
233271 sqlite3_stmt *pScan = 0;
233272 Fts5InsertCtx ctx;
233273 int rc, rc2;
233274
233275 memset(s: &ctx, c: 0, n: sizeof(Fts5InsertCtx));
233276 ctx.pStorage = p;
233277 rc = sqlite3Fts5StorageDeleteAll(p);
233278 if( rc==SQLITE_OK ){
233279 rc = fts5StorageLoadTotals(p, bCache: 1);
233280 }
233281
233282 if( rc==SQLITE_OK ){
233283 rc = fts5StorageGetStmt(p, FTS5_STMT_SCAN, ppStmt: &pScan, pzErrMsg: 0);
233284 }
233285
233286 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt: pScan) ){
233287 i64 iRowid = sqlite3_column_int64(pStmt: pScan, i: 0);
233288
233289 sqlite3Fts5BufferZero(pBuf: &buf);
233290 rc = sqlite3Fts5IndexBeginWrite(p: p->pIndex, bDelete: 0, iRowid);
233291 for(ctx.iCol=0; rc==SQLITE_OK && ctx.iCol<pConfig->nCol; ctx.iCol++){
233292 ctx.szCol = 0;
233293 if( pConfig->abUnindexed[ctx.iCol]==0 ){
233294 const char *zText = (const char*)sqlite3_column_text(pStmt: pScan, i: ctx.iCol+1);
233295 int nText = sqlite3_column_bytes(pStmt: pScan, i: ctx.iCol+1);
233296 rc = sqlite3Fts5Tokenize(pConfig,
233297 FTS5_TOKENIZE_DOCUMENT,
233298 pText: zText, nText,
233299 pCtx: (void*)&ctx,
233300 xToken: fts5StorageInsertCallback
233301 );
233302 }
233303 sqlite3Fts5BufferAppendVarint(pRc: &rc, pBuf: &buf, iVal: ctx.szCol);
233304 p->aTotalSize[ctx.iCol] += (i64)ctx.szCol;
233305 }
233306 p->nTotalRow++;
233307
233308 if( rc==SQLITE_OK ){
233309 rc = fts5StorageInsertDocsize(p, iRowid, pBuf: &buf);
233310 }
233311 }
233312 sqlite3_free(p: buf.p);
233313 rc2 = sqlite3_reset(pStmt: pScan);
233314 if( rc==SQLITE_OK ) rc = rc2;
233315
233316 /* Write the averages record */
233317 if( rc==SQLITE_OK ){
233318 rc = fts5StorageSaveTotals(p);
233319 }
233320 return rc;
233321}
233322
233323static int sqlite3Fts5StorageOptimize(Fts5Storage *p){
233324 return sqlite3Fts5IndexOptimize(p: p->pIndex);
233325}
233326
233327static int sqlite3Fts5StorageMerge(Fts5Storage *p, int nMerge){
233328 return sqlite3Fts5IndexMerge(p: p->pIndex, nMerge);
233329}
233330
233331static int sqlite3Fts5StorageReset(Fts5Storage *p){
233332 return sqlite3Fts5IndexReset(p: p->pIndex);
233333}
233334
233335/*
233336** Allocate a new rowid. This is used for "external content" tables when
233337** a NULL value is inserted into the rowid column. The new rowid is allocated
233338** by inserting a dummy row into the %_docsize table. The dummy will be
233339** overwritten later.
233340**
233341** If the %_docsize table does not exist, SQLITE_MISMATCH is returned. In
233342** this case the user is required to provide a rowid explicitly.
233343*/
233344static int fts5StorageNewRowid(Fts5Storage *p, i64 *piRowid){
233345 int rc = SQLITE_MISMATCH;
233346 if( p->pConfig->bColumnsize ){
233347 sqlite3_stmt *pReplace = 0;
233348 rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_DOCSIZE, ppStmt: &pReplace, pzErrMsg: 0);
233349 if( rc==SQLITE_OK ){
233350 sqlite3_bind_null(pStmt: pReplace, i: 1);
233351 sqlite3_bind_null(pStmt: pReplace, i: 2);
233352 sqlite3_step(pStmt: pReplace);
233353 rc = sqlite3_reset(pStmt: pReplace);
233354 }
233355 if( rc==SQLITE_OK ){
233356 *piRowid = sqlite3_last_insert_rowid(db: p->pConfig->db);
233357 }
233358 }
233359 return rc;
233360}
233361
233362/*
233363** Insert a new row into the FTS content table.
233364*/
233365static int sqlite3Fts5StorageContentInsert(
233366 Fts5Storage *p,
233367 sqlite3_value **apVal,
233368 i64 *piRowid
233369){
233370 Fts5Config *pConfig = p->pConfig;
233371 int rc = SQLITE_OK;
233372
233373 /* Insert the new row into the %_content table. */
233374 if( pConfig->eContent!=FTS5_CONTENT_NORMAL ){
233375 if( sqlite3_value_type(pVal: apVal[1])==SQLITE_INTEGER ){
233376 *piRowid = sqlite3_value_int64(pVal: apVal[1]);
233377 }else{
233378 rc = fts5StorageNewRowid(p, piRowid);
233379 }
233380 }else{
233381 sqlite3_stmt *pInsert = 0; /* Statement to write %_content table */
233382 int i; /* Counter variable */
233383 rc = fts5StorageGetStmt(p, FTS5_STMT_INSERT_CONTENT, ppStmt: &pInsert, pzErrMsg: 0);
233384 for(i=1; rc==SQLITE_OK && i<=pConfig->nCol+1; i++){
233385 rc = sqlite3_bind_value(pStmt: pInsert, i, pValue: apVal[i]);
233386 }
233387 if( rc==SQLITE_OK ){
233388 sqlite3_step(pStmt: pInsert);
233389 rc = sqlite3_reset(pStmt: pInsert);
233390 }
233391 *piRowid = sqlite3_last_insert_rowid(db: pConfig->db);
233392 }
233393
233394 return rc;
233395}
233396
233397/*
233398** Insert new entries into the FTS index and %_docsize table.
233399*/
233400static int sqlite3Fts5StorageIndexInsert(
233401 Fts5Storage *p,
233402 sqlite3_value **apVal,
233403 i64 iRowid
233404){
233405 Fts5Config *pConfig = p->pConfig;
233406 int rc = SQLITE_OK; /* Return code */
233407 Fts5InsertCtx ctx; /* Tokenization callback context object */
233408 Fts5Buffer buf; /* Buffer used to build up %_docsize blob */
233409
233410 memset(s: &buf, c: 0, n: sizeof(Fts5Buffer));
233411 ctx.pStorage = p;
233412 rc = fts5StorageLoadTotals(p, bCache: 1);
233413
233414 if( rc==SQLITE_OK ){
233415 rc = sqlite3Fts5IndexBeginWrite(p: p->pIndex, bDelete: 0, iRowid);
233416 }
233417 for(ctx.iCol=0; rc==SQLITE_OK && ctx.iCol<pConfig->nCol; ctx.iCol++){
233418 ctx.szCol = 0;
233419 if( pConfig->abUnindexed[ctx.iCol]==0 ){
233420 const char *zText = (const char*)sqlite3_value_text(pVal: apVal[ctx.iCol+2]);
233421 int nText = sqlite3_value_bytes(pVal: apVal[ctx.iCol+2]);
233422 rc = sqlite3Fts5Tokenize(pConfig,
233423 FTS5_TOKENIZE_DOCUMENT,
233424 pText: zText, nText,
233425 pCtx: (void*)&ctx,
233426 xToken: fts5StorageInsertCallback
233427 );
233428 }
233429 sqlite3Fts5BufferAppendVarint(pRc: &rc, pBuf: &buf, iVal: ctx.szCol);
233430 p->aTotalSize[ctx.iCol] += (i64)ctx.szCol;
233431 }
233432 p->nTotalRow++;
233433
233434 /* Write the %_docsize record */
233435 if( rc==SQLITE_OK ){
233436 rc = fts5StorageInsertDocsize(p, iRowid, pBuf: &buf);
233437 }
233438 sqlite3_free(p: buf.p);
233439
233440 return rc;
233441}
233442
233443static int fts5StorageCount(Fts5Storage *p, const char *zSuffix, i64 *pnRow){
233444 Fts5Config *pConfig = p->pConfig;
233445 char *zSql;
233446 int rc;
233447
233448 zSql = sqlite3_mprintf(zFormat: "SELECT count(*) FROM %Q.'%q_%s'",
233449 pConfig->zDb, pConfig->zName, zSuffix
233450 );
233451 if( zSql==0 ){
233452 rc = SQLITE_NOMEM;
233453 }else{
233454 sqlite3_stmt *pCnt = 0;
233455 rc = sqlite3_prepare_v2(db: pConfig->db, zSql, nBytes: -1, ppStmt: &pCnt, pzTail: 0);
233456 if( rc==SQLITE_OK ){
233457 if( SQLITE_ROW==sqlite3_step(pStmt: pCnt) ){
233458 *pnRow = sqlite3_column_int64(pStmt: pCnt, i: 0);
233459 }
233460 rc = sqlite3_finalize(pStmt: pCnt);
233461 }
233462 }
233463
233464 sqlite3_free(p: zSql);
233465 return rc;
233466}
233467
233468/*
233469** Context object used by sqlite3Fts5StorageIntegrity().
233470*/
233471typedef struct Fts5IntegrityCtx Fts5IntegrityCtx;
233472struct Fts5IntegrityCtx {
233473 i64 iRowid;
233474 int iCol;
233475 int szCol;
233476 u64 cksum;
233477 Fts5Termset *pTermset;
233478 Fts5Config *pConfig;
233479};
233480
233481
233482/*
233483** Tokenization callback used by integrity check.
233484*/
233485static int fts5StorageIntegrityCallback(
233486 void *pContext, /* Pointer to Fts5IntegrityCtx object */
233487 int tflags,
233488 const char *pToken, /* Buffer containing token */
233489 int nToken, /* Size of token in bytes */
233490 int iUnused1, /* Start offset of token */
233491 int iUnused2 /* End offset of token */
233492){
233493 Fts5IntegrityCtx *pCtx = (Fts5IntegrityCtx*)pContext;
233494 Fts5Termset *pTermset = pCtx->pTermset;
233495 int bPresent;
233496 int ii;
233497 int rc = SQLITE_OK;
233498 int iPos;
233499 int iCol;
233500
233501 UNUSED_PARAM2(iUnused1, iUnused2);
233502 if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
233503
233504 if( (tflags & FTS5_TOKEN_COLOCATED)==0 || pCtx->szCol==0 ){
233505 pCtx->szCol++;
233506 }
233507
233508 switch( pCtx->pConfig->eDetail ){
233509 case FTS5_DETAIL_FULL:
233510 iPos = pCtx->szCol-1;
233511 iCol = pCtx->iCol;
233512 break;
233513
233514 case FTS5_DETAIL_COLUMNS:
233515 iPos = pCtx->iCol;
233516 iCol = 0;
233517 break;
233518
233519 default:
233520 assert( pCtx->pConfig->eDetail==FTS5_DETAIL_NONE );
233521 iPos = 0;
233522 iCol = 0;
233523 break;
233524 }
233525
233526 rc = sqlite3Fts5TermsetAdd(p: pTermset, iIdx: 0, pTerm: pToken, nTerm: nToken, pbPresent: &bPresent);
233527 if( rc==SQLITE_OK && bPresent==0 ){
233528 pCtx->cksum ^= sqlite3Fts5IndexEntryCksum(
233529 iRowid: pCtx->iRowid, iCol, iPos, iIdx: 0, pTerm: pToken, nTerm: nToken
233530 );
233531 }
233532
233533 for(ii=0; rc==SQLITE_OK && ii<pCtx->pConfig->nPrefix; ii++){
233534 const int nChar = pCtx->pConfig->aPrefix[ii];
233535 int nByte = sqlite3Fts5IndexCharlenToBytelen(p: pToken, nByte: nToken, nChar);
233536 if( nByte ){
233537 rc = sqlite3Fts5TermsetAdd(p: pTermset, iIdx: ii+1, pTerm: pToken, nTerm: nByte, pbPresent: &bPresent);
233538 if( bPresent==0 ){
233539 pCtx->cksum ^= sqlite3Fts5IndexEntryCksum(
233540 iRowid: pCtx->iRowid, iCol, iPos, iIdx: ii+1, pTerm: pToken, nTerm: nByte
233541 );
233542 }
233543 }
233544 }
233545
233546 return rc;
233547}
233548
233549/*
233550** Check that the contents of the FTS index match that of the %_content
233551** table. Return SQLITE_OK if they do, or SQLITE_CORRUPT if not. Return
233552** some other SQLite error code if an error occurs while attempting to
233553** determine this.
233554*/
233555static int sqlite3Fts5StorageIntegrity(Fts5Storage *p, int iArg){
233556 Fts5Config *pConfig = p->pConfig;
233557 int rc = SQLITE_OK; /* Return code */
233558 int *aColSize; /* Array of size pConfig->nCol */
233559 i64 *aTotalSize; /* Array of size pConfig->nCol */
233560 Fts5IntegrityCtx ctx;
233561 sqlite3_stmt *pScan;
233562 int bUseCksum;
233563
233564 memset(s: &ctx, c: 0, n: sizeof(Fts5IntegrityCtx));
233565 ctx.pConfig = p->pConfig;
233566 aTotalSize = (i64*)sqlite3_malloc64(n: pConfig->nCol*(sizeof(int)+sizeof(i64)));
233567 if( !aTotalSize ) return SQLITE_NOMEM;
233568 aColSize = (int*)&aTotalSize[pConfig->nCol];
233569 memset(s: aTotalSize, c: 0, n: sizeof(i64) * pConfig->nCol);
233570
233571 bUseCksum = (pConfig->eContent==FTS5_CONTENT_NORMAL
233572 || (pConfig->eContent==FTS5_CONTENT_EXTERNAL && iArg)
233573 );
233574 if( bUseCksum ){
233575 /* Generate the expected index checksum based on the contents of the
233576 ** %_content table. This block stores the checksum in ctx.cksum. */
233577 rc = fts5StorageGetStmt(p, FTS5_STMT_SCAN, ppStmt: &pScan, pzErrMsg: 0);
233578 if( rc==SQLITE_OK ){
233579 int rc2;
233580 while( SQLITE_ROW==sqlite3_step(pStmt: pScan) ){
233581 int i;
233582 ctx.iRowid = sqlite3_column_int64(pStmt: pScan, i: 0);
233583 ctx.szCol = 0;
233584 if( pConfig->bColumnsize ){
233585 rc = sqlite3Fts5StorageDocsize(p, iRowid: ctx.iRowid, aCol: aColSize);
233586 }
233587 if( rc==SQLITE_OK && pConfig->eDetail==FTS5_DETAIL_NONE ){
233588 rc = sqlite3Fts5TermsetNew(pp: &ctx.pTermset);
233589 }
233590 for(i=0; rc==SQLITE_OK && i<pConfig->nCol; i++){
233591 if( pConfig->abUnindexed[i] ) continue;
233592 ctx.iCol = i;
233593 ctx.szCol = 0;
233594 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
233595 rc = sqlite3Fts5TermsetNew(pp: &ctx.pTermset);
233596 }
233597 if( rc==SQLITE_OK ){
233598 const char *zText = (const char*)sqlite3_column_text(pStmt: pScan, i: i+1);
233599 int nText = sqlite3_column_bytes(pStmt: pScan, i: i+1);
233600 rc = sqlite3Fts5Tokenize(pConfig,
233601 FTS5_TOKENIZE_DOCUMENT,
233602 pText: zText, nText,
233603 pCtx: (void*)&ctx,
233604 xToken: fts5StorageIntegrityCallback
233605 );
233606 }
233607 if( rc==SQLITE_OK && pConfig->bColumnsize && ctx.szCol!=aColSize[i] ){
233608 rc = FTS5_CORRUPT;
233609 }
233610 aTotalSize[i] += ctx.szCol;
233611 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
233612 sqlite3Fts5TermsetFree(p: ctx.pTermset);
233613 ctx.pTermset = 0;
233614 }
233615 }
233616 sqlite3Fts5TermsetFree(p: ctx.pTermset);
233617 ctx.pTermset = 0;
233618
233619 if( rc!=SQLITE_OK ) break;
233620 }
233621 rc2 = sqlite3_reset(pStmt: pScan);
233622 if( rc==SQLITE_OK ) rc = rc2;
233623 }
233624
233625 /* Test that the "totals" (sometimes called "averages") record looks Ok */
233626 if( rc==SQLITE_OK ){
233627 int i;
233628 rc = fts5StorageLoadTotals(p, bCache: 0);
233629 for(i=0; rc==SQLITE_OK && i<pConfig->nCol; i++){
233630 if( p->aTotalSize[i]!=aTotalSize[i] ) rc = FTS5_CORRUPT;
233631 }
233632 }
233633
233634 /* Check that the %_docsize and %_content tables contain the expected
233635 ** number of rows. */
233636 if( rc==SQLITE_OK && pConfig->eContent==FTS5_CONTENT_NORMAL ){
233637 i64 nRow = 0;
233638 rc = fts5StorageCount(p, zSuffix: "content", pnRow: &nRow);
233639 if( rc==SQLITE_OK && nRow!=p->nTotalRow ) rc = FTS5_CORRUPT;
233640 }
233641 if( rc==SQLITE_OK && pConfig->bColumnsize ){
233642 i64 nRow = 0;
233643 rc = fts5StorageCount(p, zSuffix: "docsize", pnRow: &nRow);
233644 if( rc==SQLITE_OK && nRow!=p->nTotalRow ) rc = FTS5_CORRUPT;
233645 }
233646 }
233647
233648 /* Pass the expected checksum down to the FTS index module. It will
233649 ** verify, amongst other things, that it matches the checksum generated by
233650 ** inspecting the index itself. */
233651 if( rc==SQLITE_OK ){
233652 rc = sqlite3Fts5IndexIntegrityCheck(p: p->pIndex, cksum: ctx.cksum, bUseCksum);
233653 }
233654
233655 sqlite3_free(p: aTotalSize);
233656 return rc;
233657}
233658
233659/*
233660** Obtain an SQLite statement handle that may be used to read data from the
233661** %_content table.
233662*/
233663static int sqlite3Fts5StorageStmt(
233664 Fts5Storage *p,
233665 int eStmt,
233666 sqlite3_stmt **pp,
233667 char **pzErrMsg
233668){
233669 int rc;
233670 assert( eStmt==FTS5_STMT_SCAN_ASC
233671 || eStmt==FTS5_STMT_SCAN_DESC
233672 || eStmt==FTS5_STMT_LOOKUP
233673 );
233674 rc = fts5StorageGetStmt(p, eStmt, ppStmt: pp, pzErrMsg);
233675 if( rc==SQLITE_OK ){
233676 assert( p->aStmt[eStmt]==*pp );
233677 p->aStmt[eStmt] = 0;
233678 }
233679 return rc;
233680}
233681
233682/*
233683** Release an SQLite statement handle obtained via an earlier call to
233684** sqlite3Fts5StorageStmt(). The eStmt parameter passed to this function
233685** must match that passed to the sqlite3Fts5StorageStmt() call.
233686*/
233687static void sqlite3Fts5StorageStmtRelease(
233688 Fts5Storage *p,
233689 int eStmt,
233690 sqlite3_stmt *pStmt
233691){
233692 assert( eStmt==FTS5_STMT_SCAN_ASC
233693 || eStmt==FTS5_STMT_SCAN_DESC
233694 || eStmt==FTS5_STMT_LOOKUP
233695 );
233696 if( p->aStmt[eStmt]==0 ){
233697 sqlite3_reset(pStmt);
233698 p->aStmt[eStmt] = pStmt;
233699 }else{
233700 sqlite3_finalize(pStmt);
233701 }
233702}
233703
233704static int fts5StorageDecodeSizeArray(
233705 int *aCol, int nCol, /* Array to populate */
233706 const u8 *aBlob, int nBlob /* Record to read varints from */
233707){
233708 int i;
233709 int iOff = 0;
233710 for(i=0; i<nCol; i++){
233711 if( iOff>=nBlob ) return 1;
233712 iOff += fts5GetVarint32(&aBlob[iOff], aCol[i]);
233713 }
233714 return (iOff!=nBlob);
233715}
233716
233717/*
233718** Argument aCol points to an array of integers containing one entry for
233719** each table column. This function reads the %_docsize record for the
233720** specified rowid and populates aCol[] with the results.
233721**
233722** An SQLite error code is returned if an error occurs, or SQLITE_OK
233723** otherwise.
233724*/
233725static int sqlite3Fts5StorageDocsize(Fts5Storage *p, i64 iRowid, int *aCol){
233726 int nCol = p->pConfig->nCol; /* Number of user columns in table */
233727 sqlite3_stmt *pLookup = 0; /* Statement to query %_docsize */
233728 int rc; /* Return Code */
233729
233730 assert( p->pConfig->bColumnsize );
233731 rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP_DOCSIZE, ppStmt: &pLookup, pzErrMsg: 0);
233732 if( pLookup ){
233733 int bCorrupt = 1;
233734 assert( rc==SQLITE_OK );
233735 sqlite3_bind_int64(pStmt: pLookup, i: 1, iValue: iRowid);
233736 if( SQLITE_ROW==sqlite3_step(pStmt: pLookup) ){
233737 const u8 *aBlob = sqlite3_column_blob(pStmt: pLookup, i: 0);
233738 int nBlob = sqlite3_column_bytes(pStmt: pLookup, i: 0);
233739 if( 0==fts5StorageDecodeSizeArray(aCol, nCol, aBlob, nBlob) ){
233740 bCorrupt = 0;
233741 }
233742 }
233743 rc = sqlite3_reset(pStmt: pLookup);
233744 if( bCorrupt && rc==SQLITE_OK ){
233745 rc = FTS5_CORRUPT;
233746 }
233747 }else{
233748 assert( rc!=SQLITE_OK );
233749 }
233750
233751 return rc;
233752}
233753
233754static int sqlite3Fts5StorageSize(Fts5Storage *p, int iCol, i64 *pnToken){
233755 int rc = fts5StorageLoadTotals(p, bCache: 0);
233756 if( rc==SQLITE_OK ){
233757 *pnToken = 0;
233758 if( iCol<0 ){
233759 int i;
233760 for(i=0; i<p->pConfig->nCol; i++){
233761 *pnToken += p->aTotalSize[i];
233762 }
233763 }else if( iCol<p->pConfig->nCol ){
233764 *pnToken = p->aTotalSize[iCol];
233765 }else{
233766 rc = SQLITE_RANGE;
233767 }
233768 }
233769 return rc;
233770}
233771
233772static int sqlite3Fts5StorageRowCount(Fts5Storage *p, i64 *pnRow){
233773 int rc = fts5StorageLoadTotals(p, bCache: 0);
233774 if( rc==SQLITE_OK ){
233775 /* nTotalRow being zero does not necessarily indicate a corrupt
233776 ** database - it might be that the FTS5 table really does contain zero
233777 ** rows. However this function is only called from the xRowCount() API,
233778 ** and there is no way for that API to be invoked if the table contains
233779 ** no rows. Hence the FTS5_CORRUPT return. */
233780 *pnRow = p->nTotalRow;
233781 if( p->nTotalRow<=0 ) rc = FTS5_CORRUPT;
233782 }
233783 return rc;
233784}
233785
233786/*
233787** Flush any data currently held in-memory to disk.
233788*/
233789static int sqlite3Fts5StorageSync(Fts5Storage *p){
233790 int rc = SQLITE_OK;
233791 i64 iLastRowid = sqlite3_last_insert_rowid(db: p->pConfig->db);
233792 if( p->bTotalsValid ){
233793 rc = fts5StorageSaveTotals(p);
233794 p->bTotalsValid = 0;
233795 }
233796 if( rc==SQLITE_OK ){
233797 rc = sqlite3Fts5IndexSync(p: p->pIndex);
233798 }
233799 sqlite3_set_last_insert_rowid(db: p->pConfig->db, iRowid: iLastRowid);
233800 return rc;
233801}
233802
233803static int sqlite3Fts5StorageRollback(Fts5Storage *p){
233804 p->bTotalsValid = 0;
233805 return sqlite3Fts5IndexRollback(p: p->pIndex);
233806}
233807
233808static int sqlite3Fts5StorageConfigValue(
233809 Fts5Storage *p,
233810 const char *z,
233811 sqlite3_value *pVal,
233812 int iVal
233813){
233814 sqlite3_stmt *pReplace = 0;
233815 int rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_CONFIG, ppStmt: &pReplace, pzErrMsg: 0);
233816 if( rc==SQLITE_OK ){
233817 sqlite3_bind_text(pStmt: pReplace, i: 1, zData: z, nData: -1, SQLITE_STATIC);
233818 if( pVal ){
233819 sqlite3_bind_value(pStmt: pReplace, i: 2, pValue: pVal);
233820 }else{
233821 sqlite3_bind_int(p: pReplace, i: 2, iValue: iVal);
233822 }
233823 sqlite3_step(pStmt: pReplace);
233824 rc = sqlite3_reset(pStmt: pReplace);
233825 sqlite3_bind_null(pStmt: pReplace, i: 1);
233826 }
233827 if( rc==SQLITE_OK && pVal ){
233828 int iNew = p->pConfig->iCookie + 1;
233829 rc = sqlite3Fts5IndexSetCookie(p: p->pIndex, iNew);
233830 if( rc==SQLITE_OK ){
233831 p->pConfig->iCookie = iNew;
233832 }
233833 }
233834 return rc;
233835}
233836
233837/*
233838** 2014 May 31
233839**
233840** The author disclaims copyright to this source code. In place of
233841** a legal notice, here is a blessing:
233842**
233843** May you do good and not evil.
233844** May you find forgiveness for yourself and forgive others.
233845** May you share freely, never taking more than you give.
233846**
233847******************************************************************************
233848*/
233849
233850
233851/* #include "fts5Int.h" */
233852
233853/**************************************************************************
233854** Start of ascii tokenizer implementation.
233855*/
233856
233857/*
233858** For tokenizers with no "unicode" modifier, the set of token characters
233859** is the same as the set of ASCII range alphanumeric characters.
233860*/
233861static unsigned char aAsciiTokenChar[128] = {
233862 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00..0x0F */
233863 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10..0x1F */
233864 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20..0x2F */
233865 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 0x30..0x3F */
233866 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40..0x4F */
233867 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 0x50..0x5F */
233868 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60..0x6F */
233869 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 0x70..0x7F */
233870};
233871
233872typedef struct AsciiTokenizer AsciiTokenizer;
233873struct AsciiTokenizer {
233874 unsigned char aTokenChar[128];
233875};
233876
233877static void fts5AsciiAddExceptions(
233878 AsciiTokenizer *p,
233879 const char *zArg,
233880 int bTokenChars
233881){
233882 int i;
233883 for(i=0; zArg[i]; i++){
233884 if( (zArg[i] & 0x80)==0 ){
233885 p->aTokenChar[(int)zArg[i]] = (unsigned char)bTokenChars;
233886 }
233887 }
233888}
233889
233890/*
233891** Delete a "ascii" tokenizer.
233892*/
233893static void fts5AsciiDelete(Fts5Tokenizer *p){
233894 sqlite3_free(p);
233895}
233896
233897/*
233898** Create an "ascii" tokenizer.
233899*/
233900static int fts5AsciiCreate(
233901 void *pUnused,
233902 const char **azArg, int nArg,
233903 Fts5Tokenizer **ppOut
233904){
233905 int rc = SQLITE_OK;
233906 AsciiTokenizer *p = 0;
233907 UNUSED_PARAM(pUnused);
233908 if( nArg%2 ){
233909 rc = SQLITE_ERROR;
233910 }else{
233911 p = sqlite3_malloc(n: sizeof(AsciiTokenizer));
233912 if( p==0 ){
233913 rc = SQLITE_NOMEM;
233914 }else{
233915 int i;
233916 memset(s: p, c: 0, n: sizeof(AsciiTokenizer));
233917 memcpy(dest: p->aTokenChar, src: aAsciiTokenChar, n: sizeof(aAsciiTokenChar));
233918 for(i=0; rc==SQLITE_OK && i<nArg; i+=2){
233919 const char *zArg = azArg[i+1];
233920 if( 0==sqlite3_stricmp(zLeft: azArg[i], zRight: "tokenchars") ){
233921 fts5AsciiAddExceptions(p, zArg, bTokenChars: 1);
233922 }else
233923 if( 0==sqlite3_stricmp(zLeft: azArg[i], zRight: "separators") ){
233924 fts5AsciiAddExceptions(p, zArg, bTokenChars: 0);
233925 }else{
233926 rc = SQLITE_ERROR;
233927 }
233928 }
233929 if( rc!=SQLITE_OK ){
233930 fts5AsciiDelete(p: (Fts5Tokenizer*)p);
233931 p = 0;
233932 }
233933 }
233934 }
233935
233936 *ppOut = (Fts5Tokenizer*)p;
233937 return rc;
233938}
233939
233940
233941static void asciiFold(char *aOut, const char *aIn, int nByte){
233942 int i;
233943 for(i=0; i<nByte; i++){
233944 char c = aIn[i];
233945 if( c>='A' && c<='Z' ) c += 32;
233946 aOut[i] = c;
233947 }
233948}
233949
233950/*
233951** Tokenize some text using the ascii tokenizer.
233952*/
233953static int fts5AsciiTokenize(
233954 Fts5Tokenizer *pTokenizer,
233955 void *pCtx,
233956 int iUnused,
233957 const char *pText, int nText,
233958 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
233959){
233960 AsciiTokenizer *p = (AsciiTokenizer*)pTokenizer;
233961 int rc = SQLITE_OK;
233962 int ie;
233963 int is = 0;
233964
233965 char aFold[64];
233966 int nFold = sizeof(aFold);
233967 char *pFold = aFold;
233968 unsigned char *a = p->aTokenChar;
233969
233970 UNUSED_PARAM(iUnused);
233971
233972 while( is<nText && rc==SQLITE_OK ){
233973 int nByte;
233974
233975 /* Skip any leading divider characters. */
233976 while( is<nText && ((pText[is]&0x80)==0 && a[(int)pText[is]]==0) ){
233977 is++;
233978 }
233979 if( is==nText ) break;
233980
233981 /* Count the token characters */
233982 ie = is+1;
233983 while( ie<nText && ((pText[ie]&0x80) || a[(int)pText[ie]] ) ){
233984 ie++;
233985 }
233986
233987 /* Fold to lower case */
233988 nByte = ie-is;
233989 if( nByte>nFold ){
233990 if( pFold!=aFold ) sqlite3_free(p: pFold);
233991 pFold = sqlite3_malloc64(n: (sqlite3_int64)nByte*2);
233992 if( pFold==0 ){
233993 rc = SQLITE_NOMEM;
233994 break;
233995 }
233996 nFold = nByte*2;
233997 }
233998 asciiFold(aOut: pFold, aIn: &pText[is], nByte);
233999
234000 /* Invoke the token callback */
234001 rc = xToken(pCtx, 0, pFold, nByte, is, ie);
234002 is = ie+1;
234003 }
234004
234005 if( pFold!=aFold ) sqlite3_free(p: pFold);
234006 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
234007 return rc;
234008}
234009
234010/**************************************************************************
234011** Start of unicode61 tokenizer implementation.
234012*/
234013
234014
234015/*
234016** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
234017** from the sqlite3 source file utf.c. If this file is compiled as part
234018** of the amalgamation, they are not required.
234019*/
234020#ifndef SQLITE_AMALGAMATION
234021
234022static const unsigned char sqlite3Utf8Trans1[] = {
234023 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
234024 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
234025 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
234026 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
234027 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
234028 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
234029 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
234030 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
234031};
234032
234033#define READ_UTF8(zIn, zTerm, c) \
234034 c = *(zIn++); \
234035 if( c>=0xc0 ){ \
234036 c = sqlite3Utf8Trans1[c-0xc0]; \
234037 while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ \
234038 c = (c<<6) + (0x3f & *(zIn++)); \
234039 } \
234040 if( c<0x80 \
234041 || (c&0xFFFFF800)==0xD800 \
234042 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
234043 }
234044
234045
234046#define WRITE_UTF8(zOut, c) { \
234047 if( c<0x00080 ){ \
234048 *zOut++ = (unsigned char)(c&0xFF); \
234049 } \
234050 else if( c<0x00800 ){ \
234051 *zOut++ = 0xC0 + (unsigned char)((c>>6)&0x1F); \
234052 *zOut++ = 0x80 + (unsigned char)(c & 0x3F); \
234053 } \
234054 else if( c<0x10000 ){ \
234055 *zOut++ = 0xE0 + (unsigned char)((c>>12)&0x0F); \
234056 *zOut++ = 0x80 + (unsigned char)((c>>6) & 0x3F); \
234057 *zOut++ = 0x80 + (unsigned char)(c & 0x3F); \
234058 }else{ \
234059 *zOut++ = 0xF0 + (unsigned char)((c>>18) & 0x07); \
234060 *zOut++ = 0x80 + (unsigned char)((c>>12) & 0x3F); \
234061 *zOut++ = 0x80 + (unsigned char)((c>>6) & 0x3F); \
234062 *zOut++ = 0x80 + (unsigned char)(c & 0x3F); \
234063 } \
234064}
234065
234066#endif /* ifndef SQLITE_AMALGAMATION */
234067
234068typedef struct Unicode61Tokenizer Unicode61Tokenizer;
234069struct Unicode61Tokenizer {
234070 unsigned char aTokenChar[128]; /* ASCII range token characters */
234071 char *aFold; /* Buffer to fold text into */
234072 int nFold; /* Size of aFold[] in bytes */
234073 int eRemoveDiacritic; /* True if remove_diacritics=1 is set */
234074 int nException;
234075 int *aiException;
234076
234077 unsigned char aCategory[32]; /* True for token char categories */
234078};
234079
234080/* Values for eRemoveDiacritic (must match internals of fts5_unicode2.c) */
234081#define FTS5_REMOVE_DIACRITICS_NONE 0
234082#define FTS5_REMOVE_DIACRITICS_SIMPLE 1
234083#define FTS5_REMOVE_DIACRITICS_COMPLEX 2
234084
234085static int fts5UnicodeAddExceptions(
234086 Unicode61Tokenizer *p, /* Tokenizer object */
234087 const char *z, /* Characters to treat as exceptions */
234088 int bTokenChars /* 1 for 'tokenchars', 0 for 'separators' */
234089){
234090 int rc = SQLITE_OK;
234091 int n = (int)strlen(s: z);
234092 int *aNew;
234093
234094 if( n>0 ){
234095 aNew = (int*)sqlite3_realloc64(pOld: p->aiException,
234096 n: (n+p->nException)*sizeof(int));
234097 if( aNew ){
234098 int nNew = p->nException;
234099 const unsigned char *zCsr = (const unsigned char*)z;
234100 const unsigned char *zTerm = (const unsigned char*)&z[n];
234101 while( zCsr<zTerm ){
234102 u32 iCode;
234103 int bToken;
234104 READ_UTF8(zCsr, zTerm, iCode);
234105 if( iCode<128 ){
234106 p->aTokenChar[iCode] = (unsigned char)bTokenChars;
234107 }else{
234108 bToken = p->aCategory[sqlite3Fts5UnicodeCategory(iCode)];
234109 assert( (bToken==0 || bToken==1) );
234110 assert( (bTokenChars==0 || bTokenChars==1) );
234111 if( bToken!=bTokenChars && sqlite3Fts5UnicodeIsdiacritic(c: iCode)==0 ){
234112 int i;
234113 for(i=0; i<nNew; i++){
234114 if( (u32)aNew[i]>iCode ) break;
234115 }
234116 memmove(dest: &aNew[i+1], src: &aNew[i], n: (nNew-i)*sizeof(int));
234117 aNew[i] = iCode;
234118 nNew++;
234119 }
234120 }
234121 }
234122 p->aiException = aNew;
234123 p->nException = nNew;
234124 }else{
234125 rc = SQLITE_NOMEM;
234126 }
234127 }
234128
234129 return rc;
234130}
234131
234132/*
234133** Return true if the p->aiException[] array contains the value iCode.
234134*/
234135static int fts5UnicodeIsException(Unicode61Tokenizer *p, int iCode){
234136 if( p->nException>0 ){
234137 int *a = p->aiException;
234138 int iLo = 0;
234139 int iHi = p->nException-1;
234140
234141 while( iHi>=iLo ){
234142 int iTest = (iHi + iLo) / 2;
234143 if( iCode==a[iTest] ){
234144 return 1;
234145 }else if( iCode>a[iTest] ){
234146 iLo = iTest+1;
234147 }else{
234148 iHi = iTest-1;
234149 }
234150 }
234151 }
234152
234153 return 0;
234154}
234155
234156/*
234157** Delete a "unicode61" tokenizer.
234158*/
234159static void fts5UnicodeDelete(Fts5Tokenizer *pTok){
234160 if( pTok ){
234161 Unicode61Tokenizer *p = (Unicode61Tokenizer*)pTok;
234162 sqlite3_free(p: p->aiException);
234163 sqlite3_free(p: p->aFold);
234164 sqlite3_free(p);
234165 }
234166 return;
234167}
234168
234169static int unicodeSetCategories(Unicode61Tokenizer *p, const char *zCat){
234170 const char *z = zCat;
234171
234172 while( *z ){
234173 while( *z==' ' || *z=='\t' ) z++;
234174 if( *z && sqlite3Fts5UnicodeCatParse(z, p->aCategory) ){
234175 return SQLITE_ERROR;
234176 }
234177 while( *z!=' ' && *z!='\t' && *z!='\0' ) z++;
234178 }
234179
234180 sqlite3Fts5UnicodeAscii(p->aCategory, p->aTokenChar);
234181 return SQLITE_OK;
234182}
234183
234184/*
234185** Create a "unicode61" tokenizer.
234186*/
234187static int fts5UnicodeCreate(
234188 void *pUnused,
234189 const char **azArg, int nArg,
234190 Fts5Tokenizer **ppOut
234191){
234192 int rc = SQLITE_OK; /* Return code */
234193 Unicode61Tokenizer *p = 0; /* New tokenizer object */
234194
234195 UNUSED_PARAM(pUnused);
234196
234197 if( nArg%2 ){
234198 rc = SQLITE_ERROR;
234199 }else{
234200 p = (Unicode61Tokenizer*)sqlite3_malloc(n: sizeof(Unicode61Tokenizer));
234201 if( p ){
234202 const char *zCat = "L* N* Co";
234203 int i;
234204 memset(s: p, c: 0, n: sizeof(Unicode61Tokenizer));
234205
234206 p->eRemoveDiacritic = FTS5_REMOVE_DIACRITICS_SIMPLE;
234207 p->nFold = 64;
234208 p->aFold = sqlite3_malloc64(n: p->nFold * sizeof(char));
234209 if( p->aFold==0 ){
234210 rc = SQLITE_NOMEM;
234211 }
234212
234213 /* Search for a "categories" argument */
234214 for(i=0; rc==SQLITE_OK && i<nArg; i+=2){
234215 if( 0==sqlite3_stricmp(zLeft: azArg[i], zRight: "categories") ){
234216 zCat = azArg[i+1];
234217 }
234218 }
234219
234220 if( rc==SQLITE_OK ){
234221 rc = unicodeSetCategories(p, zCat);
234222 }
234223
234224 for(i=0; rc==SQLITE_OK && i<nArg; i+=2){
234225 const char *zArg = azArg[i+1];
234226 if( 0==sqlite3_stricmp(zLeft: azArg[i], zRight: "remove_diacritics") ){
234227 if( (zArg[0]!='0' && zArg[0]!='1' && zArg[0]!='2') || zArg[1] ){
234228 rc = SQLITE_ERROR;
234229 }else{
234230 p->eRemoveDiacritic = (zArg[0] - '0');
234231 assert( p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_NONE
234232 || p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_SIMPLE
234233 || p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_COMPLEX
234234 );
234235 }
234236 }else
234237 if( 0==sqlite3_stricmp(zLeft: azArg[i], zRight: "tokenchars") ){
234238 rc = fts5UnicodeAddExceptions(p, z: zArg, bTokenChars: 1);
234239 }else
234240 if( 0==sqlite3_stricmp(zLeft: azArg[i], zRight: "separators") ){
234241 rc = fts5UnicodeAddExceptions(p, z: zArg, bTokenChars: 0);
234242 }else
234243 if( 0==sqlite3_stricmp(zLeft: azArg[i], zRight: "categories") ){
234244 /* no-op */
234245 }else{
234246 rc = SQLITE_ERROR;
234247 }
234248 }
234249
234250 }else{
234251 rc = SQLITE_NOMEM;
234252 }
234253 if( rc!=SQLITE_OK ){
234254 fts5UnicodeDelete(pTok: (Fts5Tokenizer*)p);
234255 p = 0;
234256 }
234257 *ppOut = (Fts5Tokenizer*)p;
234258 }
234259 return rc;
234260}
234261
234262/*
234263** Return true if, for the purposes of tokenizing with the tokenizer
234264** passed as the first argument, codepoint iCode is considered a token
234265** character (not a separator).
234266*/
234267static int fts5UnicodeIsAlnum(Unicode61Tokenizer *p, int iCode){
234268 return (
234269 p->aCategory[sqlite3Fts5UnicodeCategory(iCode: (u32)iCode)]
234270 ^ fts5UnicodeIsException(p, iCode)
234271 );
234272}
234273
234274static int fts5UnicodeTokenize(
234275 Fts5Tokenizer *pTokenizer,
234276 void *pCtx,
234277 int iUnused,
234278 const char *pText, int nText,
234279 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
234280){
234281 Unicode61Tokenizer *p = (Unicode61Tokenizer*)pTokenizer;
234282 int rc = SQLITE_OK;
234283 unsigned char *a = p->aTokenChar;
234284
234285 unsigned char *zTerm = (unsigned char*)&pText[nText];
234286 unsigned char *zCsr = (unsigned char *)pText;
234287
234288 /* Output buffer */
234289 char *aFold = p->aFold;
234290 int nFold = p->nFold;
234291 const char *pEnd = &aFold[nFold-6];
234292
234293 UNUSED_PARAM(iUnused);
234294
234295 /* Each iteration of this loop gobbles up a contiguous run of separators,
234296 ** then the next token. */
234297 while( rc==SQLITE_OK ){
234298 u32 iCode; /* non-ASCII codepoint read from input */
234299 char *zOut = aFold;
234300 int is;
234301 int ie;
234302
234303 /* Skip any separator characters. */
234304 while( 1 ){
234305 if( zCsr>=zTerm ) goto tokenize_done;
234306 if( *zCsr & 0x80 ) {
234307 /* A character outside of the ascii range. Skip past it if it is
234308 ** a separator character. Or break out of the loop if it is not. */
234309 is = zCsr - (unsigned char*)pText;
234310 READ_UTF8(zCsr, zTerm, iCode);
234311 if( fts5UnicodeIsAlnum(p, iCode) ){
234312 goto non_ascii_tokenchar;
234313 }
234314 }else{
234315 if( a[*zCsr] ){
234316 is = zCsr - (unsigned char*)pText;
234317 goto ascii_tokenchar;
234318 }
234319 zCsr++;
234320 }
234321 }
234322
234323 /* Run through the tokenchars. Fold them into the output buffer along
234324 ** the way. */
234325 while( zCsr<zTerm ){
234326
234327 /* Grow the output buffer so that there is sufficient space to fit the
234328 ** largest possible utf-8 character. */
234329 if( zOut>pEnd ){
234330 aFold = sqlite3_malloc64(n: (sqlite3_int64)nFold*2);
234331 if( aFold==0 ){
234332 rc = SQLITE_NOMEM;
234333 goto tokenize_done;
234334 }
234335 zOut = &aFold[zOut - p->aFold];
234336 memcpy(dest: aFold, src: p->aFold, n: nFold);
234337 sqlite3_free(p: p->aFold);
234338 p->aFold = aFold;
234339 p->nFold = nFold = nFold*2;
234340 pEnd = &aFold[nFold-6];
234341 }
234342
234343 if( *zCsr & 0x80 ){
234344 /* An non-ascii-range character. Fold it into the output buffer if
234345 ** it is a token character, or break out of the loop if it is not. */
234346 READ_UTF8(zCsr, zTerm, iCode);
234347 if( fts5UnicodeIsAlnum(p,iCode)||sqlite3Fts5UnicodeIsdiacritic(c: iCode) ){
234348 non_ascii_tokenchar:
234349 iCode = sqlite3Fts5UnicodeFold(c: iCode, bRemoveDiacritic: p->eRemoveDiacritic);
234350 if( iCode ) WRITE_UTF8(zOut, iCode);
234351 }else{
234352 break;
234353 }
234354 }else if( a[*zCsr]==0 ){
234355 /* An ascii-range separator character. End of token. */
234356 break;
234357 }else{
234358 ascii_tokenchar:
234359 if( *zCsr>='A' && *zCsr<='Z' ){
234360 *zOut++ = *zCsr + 32;
234361 }else{
234362 *zOut++ = *zCsr;
234363 }
234364 zCsr++;
234365 }
234366 ie = zCsr - (unsigned char*)pText;
234367 }
234368
234369 /* Invoke the token callback */
234370 rc = xToken(pCtx, 0, aFold, zOut-aFold, is, ie);
234371 }
234372
234373 tokenize_done:
234374 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
234375 return rc;
234376}
234377
234378/**************************************************************************
234379** Start of porter stemmer implementation.
234380*/
234381
234382/* Any tokens larger than this (in bytes) are passed through without
234383** stemming. */
234384#define FTS5_PORTER_MAX_TOKEN 64
234385
234386typedef struct PorterTokenizer PorterTokenizer;
234387struct PorterTokenizer {
234388 fts5_tokenizer tokenizer; /* Parent tokenizer module */
234389 Fts5Tokenizer *pTokenizer; /* Parent tokenizer instance */
234390 char aBuf[FTS5_PORTER_MAX_TOKEN + 64];
234391};
234392
234393/*
234394** Delete a "porter" tokenizer.
234395*/
234396static void fts5PorterDelete(Fts5Tokenizer *pTok){
234397 if( pTok ){
234398 PorterTokenizer *p = (PorterTokenizer*)pTok;
234399 if( p->pTokenizer ){
234400 p->tokenizer.xDelete(p->pTokenizer);
234401 }
234402 sqlite3_free(p);
234403 }
234404}
234405
234406/*
234407** Create a "porter" tokenizer.
234408*/
234409static int fts5PorterCreate(
234410 void *pCtx,
234411 const char **azArg, int nArg,
234412 Fts5Tokenizer **ppOut
234413){
234414 fts5_api *pApi = (fts5_api*)pCtx;
234415 int rc = SQLITE_OK;
234416 PorterTokenizer *pRet;
234417 void *pUserdata = 0;
234418 const char *zBase = "unicode61";
234419
234420 if( nArg>0 ){
234421 zBase = azArg[0];
234422 }
234423
234424 pRet = (PorterTokenizer*)sqlite3_malloc(n: sizeof(PorterTokenizer));
234425 if( pRet ){
234426 memset(s: pRet, c: 0, n: sizeof(PorterTokenizer));
234427 rc = pApi->xFindTokenizer(pApi, zBase, &pUserdata, &pRet->tokenizer);
234428 }else{
234429 rc = SQLITE_NOMEM;
234430 }
234431 if( rc==SQLITE_OK ){
234432 int nArg2 = (nArg>0 ? nArg-1 : 0);
234433 const char **azArg2 = (nArg2 ? &azArg[1] : 0);
234434 rc = pRet->tokenizer.xCreate(pUserdata, azArg2, nArg2, &pRet->pTokenizer);
234435 }
234436
234437 if( rc!=SQLITE_OK ){
234438 fts5PorterDelete(pTok: (Fts5Tokenizer*)pRet);
234439 pRet = 0;
234440 }
234441 *ppOut = (Fts5Tokenizer*)pRet;
234442 return rc;
234443}
234444
234445typedef struct PorterContext PorterContext;
234446struct PorterContext {
234447 void *pCtx;
234448 int (*xToken)(void*, int, const char*, int, int, int);
234449 char *aBuf;
234450};
234451
234452typedef struct PorterRule PorterRule;
234453struct PorterRule {
234454 const char *zSuffix;
234455 int nSuffix;
234456 int (*xCond)(char *zStem, int nStem);
234457 const char *zOutput;
234458 int nOutput;
234459};
234460
234461#if 0
234462static int fts5PorterApply(char *aBuf, int *pnBuf, PorterRule *aRule){
234463 int ret = -1;
234464 int nBuf = *pnBuf;
234465 PorterRule *p;
234466
234467 for(p=aRule; p->zSuffix; p++){
234468 assert( strlen(p->zSuffix)==p->nSuffix );
234469 assert( strlen(p->zOutput)==p->nOutput );
234470 if( nBuf<p->nSuffix ) continue;
234471 if( 0==memcmp(&aBuf[nBuf - p->nSuffix], p->zSuffix, p->nSuffix) ) break;
234472 }
234473
234474 if( p->zSuffix ){
234475 int nStem = nBuf - p->nSuffix;
234476 if( p->xCond==0 || p->xCond(aBuf, nStem) ){
234477 memcpy(&aBuf[nStem], p->zOutput, p->nOutput);
234478 *pnBuf = nStem + p->nOutput;
234479 ret = p - aRule;
234480 }
234481 }
234482
234483 return ret;
234484}
234485#endif
234486
234487static int fts5PorterIsVowel(char c, int bYIsVowel){
234488 return (
234489 c=='a' || c=='e' || c=='i' || c=='o' || c=='u' || (bYIsVowel && c=='y')
234490 );
234491}
234492
234493static int fts5PorterGobbleVC(char *zStem, int nStem, int bPrevCons){
234494 int i;
234495 int bCons = bPrevCons;
234496
234497 /* Scan for a vowel */
234498 for(i=0; i<nStem; i++){
234499 if( 0==(bCons = !fts5PorterIsVowel(c: zStem[i], bYIsVowel: bCons)) ) break;
234500 }
234501
234502 /* Scan for a consonent */
234503 for(i++; i<nStem; i++){
234504 if( (bCons = !fts5PorterIsVowel(c: zStem[i], bYIsVowel: bCons)) ) return i+1;
234505 }
234506 return 0;
234507}
234508
234509/* porter rule condition: (m > 0) */
234510static int fts5Porter_MGt0(char *zStem, int nStem){
234511 return !!fts5PorterGobbleVC(zStem, nStem, bPrevCons: 0);
234512}
234513
234514/* porter rule condition: (m > 1) */
234515static int fts5Porter_MGt1(char *zStem, int nStem){
234516 int n;
234517 n = fts5PorterGobbleVC(zStem, nStem, bPrevCons: 0);
234518 if( n && fts5PorterGobbleVC(zStem: &zStem[n], nStem: nStem-n, bPrevCons: 1) ){
234519 return 1;
234520 }
234521 return 0;
234522}
234523
234524/* porter rule condition: (m = 1) */
234525static int fts5Porter_MEq1(char *zStem, int nStem){
234526 int n;
234527 n = fts5PorterGobbleVC(zStem, nStem, bPrevCons: 0);
234528 if( n && 0==fts5PorterGobbleVC(zStem: &zStem[n], nStem: nStem-n, bPrevCons: 1) ){
234529 return 1;
234530 }
234531 return 0;
234532}
234533
234534/* porter rule condition: (*o) */
234535static int fts5Porter_Ostar(char *zStem, int nStem){
234536 if( zStem[nStem-1]=='w' || zStem[nStem-1]=='x' || zStem[nStem-1]=='y' ){
234537 return 0;
234538 }else{
234539 int i;
234540 int mask = 0;
234541 int bCons = 0;
234542 for(i=0; i<nStem; i++){
234543 bCons = !fts5PorterIsVowel(c: zStem[i], bYIsVowel: bCons);
234544 assert( bCons==0 || bCons==1 );
234545 mask = (mask << 1) + bCons;
234546 }
234547 return ((mask & 0x0007)==0x0005);
234548 }
234549}
234550
234551/* porter rule condition: (m > 1 and (*S or *T)) */
234552static int fts5Porter_MGt1_and_S_or_T(char *zStem, int nStem){
234553 assert( nStem>0 );
234554 return (zStem[nStem-1]=='s' || zStem[nStem-1]=='t')
234555 && fts5Porter_MGt1(zStem, nStem);
234556}
234557
234558/* porter rule condition: (*v*) */
234559static int fts5Porter_Vowel(char *zStem, int nStem){
234560 int i;
234561 for(i=0; i<nStem; i++){
234562 if( fts5PorterIsVowel(c: zStem[i], bYIsVowel: i>0) ){
234563 return 1;
234564 }
234565 }
234566 return 0;
234567}
234568
234569
234570/**************************************************************************
234571***************************************************************************
234572** GENERATED CODE STARTS HERE (mkportersteps.tcl)
234573*/
234574
234575static int fts5PorterStep4(char *aBuf, int *pnBuf){
234576 int ret = 0;
234577 int nBuf = *pnBuf;
234578 switch( aBuf[nBuf-2] ){
234579
234580 case 'a':
234581 if( nBuf>2 && 0==memcmp(s1: "al", s2: &aBuf[nBuf-2], n: 2) ){
234582 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-2) ){
234583 *pnBuf = nBuf - 2;
234584 }
234585 }
234586 break;
234587
234588 case 'c':
234589 if( nBuf>4 && 0==memcmp(s1: "ance", s2: &aBuf[nBuf-4], n: 4) ){
234590 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-4) ){
234591 *pnBuf = nBuf - 4;
234592 }
234593 }else if( nBuf>4 && 0==memcmp(s1: "ence", s2: &aBuf[nBuf-4], n: 4) ){
234594 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-4) ){
234595 *pnBuf = nBuf - 4;
234596 }
234597 }
234598 break;
234599
234600 case 'e':
234601 if( nBuf>2 && 0==memcmp(s1: "er", s2: &aBuf[nBuf-2], n: 2) ){
234602 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-2) ){
234603 *pnBuf = nBuf - 2;
234604 }
234605 }
234606 break;
234607
234608 case 'i':
234609 if( nBuf>2 && 0==memcmp(s1: "ic", s2: &aBuf[nBuf-2], n: 2) ){
234610 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-2) ){
234611 *pnBuf = nBuf - 2;
234612 }
234613 }
234614 break;
234615
234616 case 'l':
234617 if( nBuf>4 && 0==memcmp(s1: "able", s2: &aBuf[nBuf-4], n: 4) ){
234618 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-4) ){
234619 *pnBuf = nBuf - 4;
234620 }
234621 }else if( nBuf>4 && 0==memcmp(s1: "ible", s2: &aBuf[nBuf-4], n: 4) ){
234622 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-4) ){
234623 *pnBuf = nBuf - 4;
234624 }
234625 }
234626 break;
234627
234628 case 'n':
234629 if( nBuf>3 && 0==memcmp(s1: "ant", s2: &aBuf[nBuf-3], n: 3) ){
234630 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-3) ){
234631 *pnBuf = nBuf - 3;
234632 }
234633 }else if( nBuf>5 && 0==memcmp(s1: "ement", s2: &aBuf[nBuf-5], n: 5) ){
234634 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-5) ){
234635 *pnBuf = nBuf - 5;
234636 }
234637 }else if( nBuf>4 && 0==memcmp(s1: "ment", s2: &aBuf[nBuf-4], n: 4) ){
234638 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-4) ){
234639 *pnBuf = nBuf - 4;
234640 }
234641 }else if( nBuf>3 && 0==memcmp(s1: "ent", s2: &aBuf[nBuf-3], n: 3) ){
234642 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-3) ){
234643 *pnBuf = nBuf - 3;
234644 }
234645 }
234646 break;
234647
234648 case 'o':
234649 if( nBuf>3 && 0==memcmp(s1: "ion", s2: &aBuf[nBuf-3], n: 3) ){
234650 if( fts5Porter_MGt1_and_S_or_T(zStem: aBuf, nStem: nBuf-3) ){
234651 *pnBuf = nBuf - 3;
234652 }
234653 }else if( nBuf>2 && 0==memcmp(s1: "ou", s2: &aBuf[nBuf-2], n: 2) ){
234654 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-2) ){
234655 *pnBuf = nBuf - 2;
234656 }
234657 }
234658 break;
234659
234660 case 's':
234661 if( nBuf>3 && 0==memcmp(s1: "ism", s2: &aBuf[nBuf-3], n: 3) ){
234662 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-3) ){
234663 *pnBuf = nBuf - 3;
234664 }
234665 }
234666 break;
234667
234668 case 't':
234669 if( nBuf>3 && 0==memcmp(s1: "ate", s2: &aBuf[nBuf-3], n: 3) ){
234670 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-3) ){
234671 *pnBuf = nBuf - 3;
234672 }
234673 }else if( nBuf>3 && 0==memcmp(s1: "iti", s2: &aBuf[nBuf-3], n: 3) ){
234674 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-3) ){
234675 *pnBuf = nBuf - 3;
234676 }
234677 }
234678 break;
234679
234680 case 'u':
234681 if( nBuf>3 && 0==memcmp(s1: "ous", s2: &aBuf[nBuf-3], n: 3) ){
234682 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-3) ){
234683 *pnBuf = nBuf - 3;
234684 }
234685 }
234686 break;
234687
234688 case 'v':
234689 if( nBuf>3 && 0==memcmp(s1: "ive", s2: &aBuf[nBuf-3], n: 3) ){
234690 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-3) ){
234691 *pnBuf = nBuf - 3;
234692 }
234693 }
234694 break;
234695
234696 case 'z':
234697 if( nBuf>3 && 0==memcmp(s1: "ize", s2: &aBuf[nBuf-3], n: 3) ){
234698 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-3) ){
234699 *pnBuf = nBuf - 3;
234700 }
234701 }
234702 break;
234703
234704 }
234705 return ret;
234706}
234707
234708
234709static int fts5PorterStep1B2(char *aBuf, int *pnBuf){
234710 int ret = 0;
234711 int nBuf = *pnBuf;
234712 switch( aBuf[nBuf-2] ){
234713
234714 case 'a':
234715 if( nBuf>2 && 0==memcmp(s1: "at", s2: &aBuf[nBuf-2], n: 2) ){
234716 memcpy(dest: &aBuf[nBuf-2], src: "ate", n: 3);
234717 *pnBuf = nBuf - 2 + 3;
234718 ret = 1;
234719 }
234720 break;
234721
234722 case 'b':
234723 if( nBuf>2 && 0==memcmp(s1: "bl", s2: &aBuf[nBuf-2], n: 2) ){
234724 memcpy(dest: &aBuf[nBuf-2], src: "ble", n: 3);
234725 *pnBuf = nBuf - 2 + 3;
234726 ret = 1;
234727 }
234728 break;
234729
234730 case 'i':
234731 if( nBuf>2 && 0==memcmp(s1: "iz", s2: &aBuf[nBuf-2], n: 2) ){
234732 memcpy(dest: &aBuf[nBuf-2], src: "ize", n: 3);
234733 *pnBuf = nBuf - 2 + 3;
234734 ret = 1;
234735 }
234736 break;
234737
234738 }
234739 return ret;
234740}
234741
234742
234743static int fts5PorterStep2(char *aBuf, int *pnBuf){
234744 int ret = 0;
234745 int nBuf = *pnBuf;
234746 switch( aBuf[nBuf-2] ){
234747
234748 case 'a':
234749 if( nBuf>7 && 0==memcmp(s1: "ational", s2: &aBuf[nBuf-7], n: 7) ){
234750 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-7) ){
234751 memcpy(dest: &aBuf[nBuf-7], src: "ate", n: 3);
234752 *pnBuf = nBuf - 7 + 3;
234753 }
234754 }else if( nBuf>6 && 0==memcmp(s1: "tional", s2: &aBuf[nBuf-6], n: 6) ){
234755 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-6) ){
234756 memcpy(dest: &aBuf[nBuf-6], src: "tion", n: 4);
234757 *pnBuf = nBuf - 6 + 4;
234758 }
234759 }
234760 break;
234761
234762 case 'c':
234763 if( nBuf>4 && 0==memcmp(s1: "enci", s2: &aBuf[nBuf-4], n: 4) ){
234764 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-4) ){
234765 memcpy(dest: &aBuf[nBuf-4], src: "ence", n: 4);
234766 *pnBuf = nBuf - 4 + 4;
234767 }
234768 }else if( nBuf>4 && 0==memcmp(s1: "anci", s2: &aBuf[nBuf-4], n: 4) ){
234769 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-4) ){
234770 memcpy(dest: &aBuf[nBuf-4], src: "ance", n: 4);
234771 *pnBuf = nBuf - 4 + 4;
234772 }
234773 }
234774 break;
234775
234776 case 'e':
234777 if( nBuf>4 && 0==memcmp(s1: "izer", s2: &aBuf[nBuf-4], n: 4) ){
234778 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-4) ){
234779 memcpy(dest: &aBuf[nBuf-4], src: "ize", n: 3);
234780 *pnBuf = nBuf - 4 + 3;
234781 }
234782 }
234783 break;
234784
234785 case 'g':
234786 if( nBuf>4 && 0==memcmp(s1: "logi", s2: &aBuf[nBuf-4], n: 4) ){
234787 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-4) ){
234788 memcpy(dest: &aBuf[nBuf-4], src: "log", n: 3);
234789 *pnBuf = nBuf - 4 + 3;
234790 }
234791 }
234792 break;
234793
234794 case 'l':
234795 if( nBuf>3 && 0==memcmp(s1: "bli", s2: &aBuf[nBuf-3], n: 3) ){
234796 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-3) ){
234797 memcpy(dest: &aBuf[nBuf-3], src: "ble", n: 3);
234798 *pnBuf = nBuf - 3 + 3;
234799 }
234800 }else if( nBuf>4 && 0==memcmp(s1: "alli", s2: &aBuf[nBuf-4], n: 4) ){
234801 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-4) ){
234802 memcpy(dest: &aBuf[nBuf-4], src: "al", n: 2);
234803 *pnBuf = nBuf - 4 + 2;
234804 }
234805 }else if( nBuf>5 && 0==memcmp(s1: "entli", s2: &aBuf[nBuf-5], n: 5) ){
234806 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-5) ){
234807 memcpy(dest: &aBuf[nBuf-5], src: "ent", n: 3);
234808 *pnBuf = nBuf - 5 + 3;
234809 }
234810 }else if( nBuf>3 && 0==memcmp(s1: "eli", s2: &aBuf[nBuf-3], n: 3) ){
234811 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-3) ){
234812 memcpy(dest: &aBuf[nBuf-3], src: "e", n: 1);
234813 *pnBuf = nBuf - 3 + 1;
234814 }
234815 }else if( nBuf>5 && 0==memcmp(s1: "ousli", s2: &aBuf[nBuf-5], n: 5) ){
234816 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-5) ){
234817 memcpy(dest: &aBuf[nBuf-5], src: "ous", n: 3);
234818 *pnBuf = nBuf - 5 + 3;
234819 }
234820 }
234821 break;
234822
234823 case 'o':
234824 if( nBuf>7 && 0==memcmp(s1: "ization", s2: &aBuf[nBuf-7], n: 7) ){
234825 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-7) ){
234826 memcpy(dest: &aBuf[nBuf-7], src: "ize", n: 3);
234827 *pnBuf = nBuf - 7 + 3;
234828 }
234829 }else if( nBuf>5 && 0==memcmp(s1: "ation", s2: &aBuf[nBuf-5], n: 5) ){
234830 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-5) ){
234831 memcpy(dest: &aBuf[nBuf-5], src: "ate", n: 3);
234832 *pnBuf = nBuf - 5 + 3;
234833 }
234834 }else if( nBuf>4 && 0==memcmp(s1: "ator", s2: &aBuf[nBuf-4], n: 4) ){
234835 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-4) ){
234836 memcpy(dest: &aBuf[nBuf-4], src: "ate", n: 3);
234837 *pnBuf = nBuf - 4 + 3;
234838 }
234839 }
234840 break;
234841
234842 case 's':
234843 if( nBuf>5 && 0==memcmp(s1: "alism", s2: &aBuf[nBuf-5], n: 5) ){
234844 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-5) ){
234845 memcpy(dest: &aBuf[nBuf-5], src: "al", n: 2);
234846 *pnBuf = nBuf - 5 + 2;
234847 }
234848 }else if( nBuf>7 && 0==memcmp(s1: "iveness", s2: &aBuf[nBuf-7], n: 7) ){
234849 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-7) ){
234850 memcpy(dest: &aBuf[nBuf-7], src: "ive", n: 3);
234851 *pnBuf = nBuf - 7 + 3;
234852 }
234853 }else if( nBuf>7 && 0==memcmp(s1: "fulness", s2: &aBuf[nBuf-7], n: 7) ){
234854 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-7) ){
234855 memcpy(dest: &aBuf[nBuf-7], src: "ful", n: 3);
234856 *pnBuf = nBuf - 7 + 3;
234857 }
234858 }else if( nBuf>7 && 0==memcmp(s1: "ousness", s2: &aBuf[nBuf-7], n: 7) ){
234859 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-7) ){
234860 memcpy(dest: &aBuf[nBuf-7], src: "ous", n: 3);
234861 *pnBuf = nBuf - 7 + 3;
234862 }
234863 }
234864 break;
234865
234866 case 't':
234867 if( nBuf>5 && 0==memcmp(s1: "aliti", s2: &aBuf[nBuf-5], n: 5) ){
234868 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-5) ){
234869 memcpy(dest: &aBuf[nBuf-5], src: "al", n: 2);
234870 *pnBuf = nBuf - 5 + 2;
234871 }
234872 }else if( nBuf>5 && 0==memcmp(s1: "iviti", s2: &aBuf[nBuf-5], n: 5) ){
234873 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-5) ){
234874 memcpy(dest: &aBuf[nBuf-5], src: "ive", n: 3);
234875 *pnBuf = nBuf - 5 + 3;
234876 }
234877 }else if( nBuf>6 && 0==memcmp(s1: "biliti", s2: &aBuf[nBuf-6], n: 6) ){
234878 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-6) ){
234879 memcpy(dest: &aBuf[nBuf-6], src: "ble", n: 3);
234880 *pnBuf = nBuf - 6 + 3;
234881 }
234882 }
234883 break;
234884
234885 }
234886 return ret;
234887}
234888
234889
234890static int fts5PorterStep3(char *aBuf, int *pnBuf){
234891 int ret = 0;
234892 int nBuf = *pnBuf;
234893 switch( aBuf[nBuf-2] ){
234894
234895 case 'a':
234896 if( nBuf>4 && 0==memcmp(s1: "ical", s2: &aBuf[nBuf-4], n: 4) ){
234897 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-4) ){
234898 memcpy(dest: &aBuf[nBuf-4], src: "ic", n: 2);
234899 *pnBuf = nBuf - 4 + 2;
234900 }
234901 }
234902 break;
234903
234904 case 's':
234905 if( nBuf>4 && 0==memcmp(s1: "ness", s2: &aBuf[nBuf-4], n: 4) ){
234906 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-4) ){
234907 *pnBuf = nBuf - 4;
234908 }
234909 }
234910 break;
234911
234912 case 't':
234913 if( nBuf>5 && 0==memcmp(s1: "icate", s2: &aBuf[nBuf-5], n: 5) ){
234914 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-5) ){
234915 memcpy(dest: &aBuf[nBuf-5], src: "ic", n: 2);
234916 *pnBuf = nBuf - 5 + 2;
234917 }
234918 }else if( nBuf>5 && 0==memcmp(s1: "iciti", s2: &aBuf[nBuf-5], n: 5) ){
234919 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-5) ){
234920 memcpy(dest: &aBuf[nBuf-5], src: "ic", n: 2);
234921 *pnBuf = nBuf - 5 + 2;
234922 }
234923 }
234924 break;
234925
234926 case 'u':
234927 if( nBuf>3 && 0==memcmp(s1: "ful", s2: &aBuf[nBuf-3], n: 3) ){
234928 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-3) ){
234929 *pnBuf = nBuf - 3;
234930 }
234931 }
234932 break;
234933
234934 case 'v':
234935 if( nBuf>5 && 0==memcmp(s1: "ative", s2: &aBuf[nBuf-5], n: 5) ){
234936 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-5) ){
234937 *pnBuf = nBuf - 5;
234938 }
234939 }
234940 break;
234941
234942 case 'z':
234943 if( nBuf>5 && 0==memcmp(s1: "alize", s2: &aBuf[nBuf-5], n: 5) ){
234944 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-5) ){
234945 memcpy(dest: &aBuf[nBuf-5], src: "al", n: 2);
234946 *pnBuf = nBuf - 5 + 2;
234947 }
234948 }
234949 break;
234950
234951 }
234952 return ret;
234953}
234954
234955
234956static int fts5PorterStep1B(char *aBuf, int *pnBuf){
234957 int ret = 0;
234958 int nBuf = *pnBuf;
234959 switch( aBuf[nBuf-2] ){
234960
234961 case 'e':
234962 if( nBuf>3 && 0==memcmp(s1: "eed", s2: &aBuf[nBuf-3], n: 3) ){
234963 if( fts5Porter_MGt0(zStem: aBuf, nStem: nBuf-3) ){
234964 memcpy(dest: &aBuf[nBuf-3], src: "ee", n: 2);
234965 *pnBuf = nBuf - 3 + 2;
234966 }
234967 }else if( nBuf>2 && 0==memcmp(s1: "ed", s2: &aBuf[nBuf-2], n: 2) ){
234968 if( fts5Porter_Vowel(zStem: aBuf, nStem: nBuf-2) ){
234969 *pnBuf = nBuf - 2;
234970 ret = 1;
234971 }
234972 }
234973 break;
234974
234975 case 'n':
234976 if( nBuf>3 && 0==memcmp(s1: "ing", s2: &aBuf[nBuf-3], n: 3) ){
234977 if( fts5Porter_Vowel(zStem: aBuf, nStem: nBuf-3) ){
234978 *pnBuf = nBuf - 3;
234979 ret = 1;
234980 }
234981 }
234982 break;
234983
234984 }
234985 return ret;
234986}
234987
234988/*
234989** GENERATED CODE ENDS HERE (mkportersteps.tcl)
234990***************************************************************************
234991**************************************************************************/
234992
234993static void fts5PorterStep1A(char *aBuf, int *pnBuf){
234994 int nBuf = *pnBuf;
234995 if( aBuf[nBuf-1]=='s' ){
234996 if( aBuf[nBuf-2]=='e' ){
234997 if( (nBuf>4 && aBuf[nBuf-4]=='s' && aBuf[nBuf-3]=='s')
234998 || (nBuf>3 && aBuf[nBuf-3]=='i' )
234999 ){
235000 *pnBuf = nBuf-2;
235001 }else{
235002 *pnBuf = nBuf-1;
235003 }
235004 }
235005 else if( aBuf[nBuf-2]!='s' ){
235006 *pnBuf = nBuf-1;
235007 }
235008 }
235009}
235010
235011static int fts5PorterCb(
235012 void *pCtx,
235013 int tflags,
235014 const char *pToken,
235015 int nToken,
235016 int iStart,
235017 int iEnd
235018){
235019 PorterContext *p = (PorterContext*)pCtx;
235020
235021 char *aBuf;
235022 int nBuf;
235023
235024 if( nToken>FTS5_PORTER_MAX_TOKEN || nToken<3 ) goto pass_through;
235025 aBuf = p->aBuf;
235026 nBuf = nToken;
235027 memcpy(dest: aBuf, src: pToken, n: nBuf);
235028
235029 /* Step 1. */
235030 fts5PorterStep1A(aBuf, pnBuf: &nBuf);
235031 if( fts5PorterStep1B(aBuf, pnBuf: &nBuf) ){
235032 if( fts5PorterStep1B2(aBuf, pnBuf: &nBuf)==0 ){
235033 char c = aBuf[nBuf-1];
235034 if( fts5PorterIsVowel(c, bYIsVowel: 0)==0
235035 && c!='l' && c!='s' && c!='z' && c==aBuf[nBuf-2]
235036 ){
235037 nBuf--;
235038 }else if( fts5Porter_MEq1(zStem: aBuf, nStem: nBuf) && fts5Porter_Ostar(zStem: aBuf, nStem: nBuf) ){
235039 aBuf[nBuf++] = 'e';
235040 }
235041 }
235042 }
235043
235044 /* Step 1C. */
235045 if( aBuf[nBuf-1]=='y' && fts5Porter_Vowel(zStem: aBuf, nStem: nBuf-1) ){
235046 aBuf[nBuf-1] = 'i';
235047 }
235048
235049 /* Steps 2 through 4. */
235050 fts5PorterStep2(aBuf, pnBuf: &nBuf);
235051 fts5PorterStep3(aBuf, pnBuf: &nBuf);
235052 fts5PorterStep4(aBuf, pnBuf: &nBuf);
235053
235054 /* Step 5a. */
235055 assert( nBuf>0 );
235056 if( aBuf[nBuf-1]=='e' ){
235057 if( fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-1)
235058 || (fts5Porter_MEq1(zStem: aBuf, nStem: nBuf-1) && !fts5Porter_Ostar(zStem: aBuf, nStem: nBuf-1))
235059 ){
235060 nBuf--;
235061 }
235062 }
235063
235064 /* Step 5b. */
235065 if( nBuf>1 && aBuf[nBuf-1]=='l'
235066 && aBuf[nBuf-2]=='l' && fts5Porter_MGt1(zStem: aBuf, nStem: nBuf-1)
235067 ){
235068 nBuf--;
235069 }
235070
235071 return p->xToken(p->pCtx, tflags, aBuf, nBuf, iStart, iEnd);
235072
235073 pass_through:
235074 return p->xToken(p->pCtx, tflags, pToken, nToken, iStart, iEnd);
235075}
235076
235077/*
235078** Tokenize using the porter tokenizer.
235079*/
235080static int fts5PorterTokenize(
235081 Fts5Tokenizer *pTokenizer,
235082 void *pCtx,
235083 int flags,
235084 const char *pText, int nText,
235085 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
235086){
235087 PorterTokenizer *p = (PorterTokenizer*)pTokenizer;
235088 PorterContext sCtx;
235089 sCtx.xToken = xToken;
235090 sCtx.pCtx = pCtx;
235091 sCtx.aBuf = p->aBuf;
235092 return p->tokenizer.xTokenize(
235093 p->pTokenizer, (void*)&sCtx, flags, pText, nText, fts5PorterCb
235094 );
235095}
235096
235097/**************************************************************************
235098** Start of trigram implementation.
235099*/
235100typedef struct TrigramTokenizer TrigramTokenizer;
235101struct TrigramTokenizer {
235102 int bFold; /* True to fold to lower-case */
235103};
235104
235105/*
235106** Free a trigram tokenizer.
235107*/
235108static void fts5TriDelete(Fts5Tokenizer *p){
235109 sqlite3_free(p);
235110}
235111
235112/*
235113** Allocate a trigram tokenizer.
235114*/
235115static int fts5TriCreate(
235116 void *pUnused,
235117 const char **azArg,
235118 int nArg,
235119 Fts5Tokenizer **ppOut
235120){
235121 int rc = SQLITE_OK;
235122 TrigramTokenizer *pNew = (TrigramTokenizer*)sqlite3_malloc(n: sizeof(*pNew));
235123 UNUSED_PARAM(pUnused);
235124 if( pNew==0 ){
235125 rc = SQLITE_NOMEM;
235126 }else{
235127 int i;
235128 pNew->bFold = 1;
235129 for(i=0; rc==SQLITE_OK && i<nArg; i+=2){
235130 const char *zArg = azArg[i+1];
235131 if( 0==sqlite3_stricmp(zLeft: azArg[i], zRight: "case_sensitive") ){
235132 if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1] ){
235133 rc = SQLITE_ERROR;
235134 }else{
235135 pNew->bFold = (zArg[0]=='0');
235136 }
235137 }else{
235138 rc = SQLITE_ERROR;
235139 }
235140 }
235141 if( rc!=SQLITE_OK ){
235142 fts5TriDelete(p: (Fts5Tokenizer*)pNew);
235143 pNew = 0;
235144 }
235145 }
235146 *ppOut = (Fts5Tokenizer*)pNew;
235147 return rc;
235148}
235149
235150/*
235151** Trigram tokenizer tokenize routine.
235152*/
235153static int fts5TriTokenize(
235154 Fts5Tokenizer *pTok,
235155 void *pCtx,
235156 int unusedFlags,
235157 const char *pText, int nText,
235158 int (*xToken)(void*, int, const char*, int, int, int)
235159){
235160 TrigramTokenizer *p = (TrigramTokenizer*)pTok;
235161 int rc = SQLITE_OK;
235162 char aBuf[32];
235163 const unsigned char *zIn = (const unsigned char*)pText;
235164 const unsigned char *zEof = &zIn[nText];
235165 u32 iCode;
235166
235167 UNUSED_PARAM(unusedFlags);
235168 while( 1 ){
235169 char *zOut = aBuf;
235170 int iStart = zIn - (const unsigned char*)pText;
235171 const unsigned char *zNext;
235172
235173 READ_UTF8(zIn, zEof, iCode);
235174 if( iCode==0 ) break;
235175 zNext = zIn;
235176 if( zIn<zEof ){
235177 if( p->bFold ) iCode = sqlite3Fts5UnicodeFold(c: iCode, bRemoveDiacritic: 0);
235178 WRITE_UTF8(zOut, iCode);
235179 READ_UTF8(zIn, zEof, iCode);
235180 if( iCode==0 ) break;
235181 }else{
235182 break;
235183 }
235184 if( zIn<zEof ){
235185 if( p->bFold ) iCode = sqlite3Fts5UnicodeFold(c: iCode, bRemoveDiacritic: 0);
235186 WRITE_UTF8(zOut, iCode);
235187 READ_UTF8(zIn, zEof, iCode);
235188 if( iCode==0 ) break;
235189 if( p->bFold ) iCode = sqlite3Fts5UnicodeFold(c: iCode, bRemoveDiacritic: 0);
235190 WRITE_UTF8(zOut, iCode);
235191 }else{
235192 break;
235193 }
235194 rc = xToken(pCtx, 0, aBuf, zOut-aBuf, iStart, iStart + zOut-aBuf);
235195 if( rc!=SQLITE_OK ) break;
235196 zIn = zNext;
235197 }
235198
235199 return rc;
235200}
235201
235202/*
235203** Argument xCreate is a pointer to a constructor function for a tokenizer.
235204** pTok is a tokenizer previously created using the same method. This function
235205** returns one of FTS5_PATTERN_NONE, FTS5_PATTERN_LIKE or FTS5_PATTERN_GLOB
235206** indicating the style of pattern matching that the tokenizer can support.
235207** In practice, this is:
235208**
235209** "trigram" tokenizer, case_sensitive=1 - FTS5_PATTERN_GLOB
235210** "trigram" tokenizer, case_sensitive=0 (the default) - FTS5_PATTERN_LIKE
235211** all other tokenizers - FTS5_PATTERN_NONE
235212*/
235213static int sqlite3Fts5TokenizerPattern(
235214 int (*xCreate)(void*, const char**, int, Fts5Tokenizer**),
235215 Fts5Tokenizer *pTok
235216){
235217 if( xCreate==fts5TriCreate ){
235218 TrigramTokenizer *p = (TrigramTokenizer*)pTok;
235219 return p->bFold ? FTS5_PATTERN_LIKE : FTS5_PATTERN_GLOB;
235220 }
235221 return FTS5_PATTERN_NONE;
235222}
235223
235224/*
235225** Register all built-in tokenizers with FTS5.
235226*/
235227static int sqlite3Fts5TokenizerInit(fts5_api *pApi){
235228 struct BuiltinTokenizer {
235229 const char *zName;
235230 fts5_tokenizer x;
235231 } aBuiltin[] = {
235232 { "unicode61", {fts5UnicodeCreate, fts5UnicodeDelete, fts5UnicodeTokenize}},
235233 { "ascii", {fts5AsciiCreate, fts5AsciiDelete, fts5AsciiTokenize }},
235234 { "porter", {fts5PorterCreate, fts5PorterDelete, fts5PorterTokenize }},
235235 { "trigram", {fts5TriCreate, fts5TriDelete, fts5TriTokenize}},
235236 };
235237
235238 int rc = SQLITE_OK; /* Return code */
235239 int i; /* To iterate through builtin functions */
235240
235241 for(i=0; rc==SQLITE_OK && i<ArraySize(aBuiltin); i++){
235242 rc = pApi->xCreateTokenizer(pApi,
235243 aBuiltin[i].zName,
235244 (void*)pApi,
235245 &aBuiltin[i].x,
235246 0
235247 );
235248 }
235249
235250 return rc;
235251}
235252
235253/*
235254** 2012-05-25
235255**
235256** The author disclaims copyright to this source code. In place of
235257** a legal notice, here is a blessing:
235258**
235259** May you do good and not evil.
235260** May you find forgiveness for yourself and forgive others.
235261** May you share freely, never taking more than you give.
235262**
235263******************************************************************************
235264*/
235265
235266/*
235267** DO NOT EDIT THIS MACHINE GENERATED FILE.
235268*/
235269
235270
235271/* #include <assert.h> */
235272
235273
235274
235275/*
235276** If the argument is a codepoint corresponding to a lowercase letter
235277** in the ASCII range with a diacritic added, return the codepoint
235278** of the ASCII letter only. For example, if passed 235 - "LATIN
235279** SMALL LETTER E WITH DIAERESIS" - return 65 ("LATIN SMALL LETTER
235280** E"). The resuls of passing a codepoint that corresponds to an
235281** uppercase letter are undefined.
235282*/
235283static int fts5_remove_diacritic(int c, int bComplex){
235284 unsigned short aDia[] = {
235285 0, 1797, 1848, 1859, 1891, 1928, 1940, 1995,
235286 2024, 2040, 2060, 2110, 2168, 2206, 2264, 2286,
235287 2344, 2383, 2472, 2488, 2516, 2596, 2668, 2732,
235288 2782, 2842, 2894, 2954, 2984, 3000, 3028, 3336,
235289 3456, 3696, 3712, 3728, 3744, 3766, 3832, 3896,
235290 3912, 3928, 3944, 3968, 4008, 4040, 4056, 4106,
235291 4138, 4170, 4202, 4234, 4266, 4296, 4312, 4344,
235292 4408, 4424, 4442, 4472, 4488, 4504, 6148, 6198,
235293 6264, 6280, 6360, 6429, 6505, 6529, 61448, 61468,
235294 61512, 61534, 61592, 61610, 61642, 61672, 61688, 61704,
235295 61726, 61784, 61800, 61816, 61836, 61880, 61896, 61914,
235296 61948, 61998, 62062, 62122, 62154, 62184, 62200, 62218,
235297 62252, 62302, 62364, 62410, 62442, 62478, 62536, 62554,
235298 62584, 62604, 62640, 62648, 62656, 62664, 62730, 62766,
235299 62830, 62890, 62924, 62974, 63032, 63050, 63082, 63118,
235300 63182, 63242, 63274, 63310, 63368, 63390,
235301 };
235302#define HIBIT ((unsigned char)0x80)
235303 unsigned char aChar[] = {
235304 '\0', 'a', 'c', 'e', 'i', 'n',
235305 'o', 'u', 'y', 'y', 'a', 'c',
235306 'd', 'e', 'e', 'g', 'h', 'i',
235307 'j', 'k', 'l', 'n', 'o', 'r',
235308 's', 't', 'u', 'u', 'w', 'y',
235309 'z', 'o', 'u', 'a', 'i', 'o',
235310 'u', 'u'|HIBIT, 'a'|HIBIT, 'g', 'k', 'o',
235311 'o'|HIBIT, 'j', 'g', 'n', 'a'|HIBIT, 'a',
235312 'e', 'i', 'o', 'r', 'u', 's',
235313 't', 'h', 'a', 'e', 'o'|HIBIT, 'o',
235314 'o'|HIBIT, 'y', '\0', '\0', '\0', '\0',
235315 '\0', '\0', '\0', '\0', 'a', 'b',
235316 'c'|HIBIT, 'd', 'd', 'e'|HIBIT, 'e', 'e'|HIBIT,
235317 'f', 'g', 'h', 'h', 'i', 'i'|HIBIT,
235318 'k', 'l', 'l'|HIBIT, 'l', 'm', 'n',
235319 'o'|HIBIT, 'p', 'r', 'r'|HIBIT, 'r', 's',
235320 's'|HIBIT, 't', 'u', 'u'|HIBIT, 'v', 'w',
235321 'w', 'x', 'y', 'z', 'h', 't',
235322 'w', 'y', 'a', 'a'|HIBIT, 'a'|HIBIT, 'a'|HIBIT,
235323 'e', 'e'|HIBIT, 'e'|HIBIT, 'i', 'o', 'o'|HIBIT,
235324 'o'|HIBIT, 'o'|HIBIT, 'u', 'u'|HIBIT, 'u'|HIBIT, 'y',
235325 };
235326
235327 unsigned int key = (((unsigned int)c)<<3) | 0x00000007;
235328 int iRes = 0;
235329 int iHi = sizeof(aDia)/sizeof(aDia[0]) - 1;
235330 int iLo = 0;
235331 while( iHi>=iLo ){
235332 int iTest = (iHi + iLo) / 2;
235333 if( key >= aDia[iTest] ){
235334 iRes = iTest;
235335 iLo = iTest+1;
235336 }else{
235337 iHi = iTest-1;
235338 }
235339 }
235340 assert( key>=aDia[iRes] );
235341 if( bComplex==0 && (aChar[iRes] & 0x80) ) return c;
235342 return (c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : ((int)aChar[iRes] & 0x7F);
235343}
235344
235345
235346/*
235347** Return true if the argument interpreted as a unicode codepoint
235348** is a diacritical modifier character.
235349*/
235350static int sqlite3Fts5UnicodeIsdiacritic(int c){
235351 unsigned int mask0 = 0x08029FDF;
235352 unsigned int mask1 = 0x000361F8;
235353 if( c<768 || c>817 ) return 0;
235354 return (c < 768+32) ?
235355 (mask0 & ((unsigned int)1 << (c-768))) :
235356 (mask1 & ((unsigned int)1 << (c-768-32)));
235357}
235358
235359
235360/*
235361** Interpret the argument as a unicode codepoint. If the codepoint
235362** is an upper case character that has a lower case equivalent,
235363** return the codepoint corresponding to the lower case version.
235364** Otherwise, return a copy of the argument.
235365**
235366** The results are undefined if the value passed to this function
235367** is less than zero.
235368*/
235369static int sqlite3Fts5UnicodeFold(int c, int eRemoveDiacritic){
235370 /* Each entry in the following array defines a rule for folding a range
235371 ** of codepoints to lower case. The rule applies to a range of nRange
235372 ** codepoints starting at codepoint iCode.
235373 **
235374 ** If the least significant bit in flags is clear, then the rule applies
235375 ** to all nRange codepoints (i.e. all nRange codepoints are upper case and
235376 ** need to be folded). Or, if it is set, then the rule only applies to
235377 ** every second codepoint in the range, starting with codepoint C.
235378 **
235379 ** The 7 most significant bits in flags are an index into the aiOff[]
235380 ** array. If a specific codepoint C does require folding, then its lower
235381 ** case equivalent is ((C + aiOff[flags>>1]) & 0xFFFF).
235382 **
235383 ** The contents of this array are generated by parsing the CaseFolding.txt
235384 ** file distributed as part of the "Unicode Character Database". See
235385 ** http://www.unicode.org for details.
235386 */
235387 static const struct TableEntry {
235388 unsigned short iCode;
235389 unsigned char flags;
235390 unsigned char nRange;
235391 } aEntry[] = {
235392 {65, 14, 26}, {181, 64, 1}, {192, 14, 23},
235393 {216, 14, 7}, {256, 1, 48}, {306, 1, 6},
235394 {313, 1, 16}, {330, 1, 46}, {376, 116, 1},
235395 {377, 1, 6}, {383, 104, 1}, {385, 50, 1},
235396 {386, 1, 4}, {390, 44, 1}, {391, 0, 1},
235397 {393, 42, 2}, {395, 0, 1}, {398, 32, 1},
235398 {399, 38, 1}, {400, 40, 1}, {401, 0, 1},
235399 {403, 42, 1}, {404, 46, 1}, {406, 52, 1},
235400 {407, 48, 1}, {408, 0, 1}, {412, 52, 1},
235401 {413, 54, 1}, {415, 56, 1}, {416, 1, 6},
235402 {422, 60, 1}, {423, 0, 1}, {425, 60, 1},
235403 {428, 0, 1}, {430, 60, 1}, {431, 0, 1},
235404 {433, 58, 2}, {435, 1, 4}, {439, 62, 1},
235405 {440, 0, 1}, {444, 0, 1}, {452, 2, 1},
235406 {453, 0, 1}, {455, 2, 1}, {456, 0, 1},
235407 {458, 2, 1}, {459, 1, 18}, {478, 1, 18},
235408 {497, 2, 1}, {498, 1, 4}, {502, 122, 1},
235409 {503, 134, 1}, {504, 1, 40}, {544, 110, 1},
235410 {546, 1, 18}, {570, 70, 1}, {571, 0, 1},
235411 {573, 108, 1}, {574, 68, 1}, {577, 0, 1},
235412 {579, 106, 1}, {580, 28, 1}, {581, 30, 1},
235413 {582, 1, 10}, {837, 36, 1}, {880, 1, 4},
235414 {886, 0, 1}, {902, 18, 1}, {904, 16, 3},
235415 {908, 26, 1}, {910, 24, 2}, {913, 14, 17},
235416 {931, 14, 9}, {962, 0, 1}, {975, 4, 1},
235417 {976, 140, 1}, {977, 142, 1}, {981, 146, 1},
235418 {982, 144, 1}, {984, 1, 24}, {1008, 136, 1},
235419 {1009, 138, 1}, {1012, 130, 1}, {1013, 128, 1},
235420 {1015, 0, 1}, {1017, 152, 1}, {1018, 0, 1},
235421 {1021, 110, 3}, {1024, 34, 16}, {1040, 14, 32},
235422 {1120, 1, 34}, {1162, 1, 54}, {1216, 6, 1},
235423 {1217, 1, 14}, {1232, 1, 88}, {1329, 22, 38},
235424 {4256, 66, 38}, {4295, 66, 1}, {4301, 66, 1},
235425 {7680, 1, 150}, {7835, 132, 1}, {7838, 96, 1},
235426 {7840, 1, 96}, {7944, 150, 8}, {7960, 150, 6},
235427 {7976, 150, 8}, {7992, 150, 8}, {8008, 150, 6},
235428 {8025, 151, 8}, {8040, 150, 8}, {8072, 150, 8},
235429 {8088, 150, 8}, {8104, 150, 8}, {8120, 150, 2},
235430 {8122, 126, 2}, {8124, 148, 1}, {8126, 100, 1},
235431 {8136, 124, 4}, {8140, 148, 1}, {8152, 150, 2},
235432 {8154, 120, 2}, {8168, 150, 2}, {8170, 118, 2},
235433 {8172, 152, 1}, {8184, 112, 2}, {8186, 114, 2},
235434 {8188, 148, 1}, {8486, 98, 1}, {8490, 92, 1},
235435 {8491, 94, 1}, {8498, 12, 1}, {8544, 8, 16},
235436 {8579, 0, 1}, {9398, 10, 26}, {11264, 22, 47},
235437 {11360, 0, 1}, {11362, 88, 1}, {11363, 102, 1},
235438 {11364, 90, 1}, {11367, 1, 6}, {11373, 84, 1},
235439 {11374, 86, 1}, {11375, 80, 1}, {11376, 82, 1},
235440 {11378, 0, 1}, {11381, 0, 1}, {11390, 78, 2},
235441 {11392, 1, 100}, {11499, 1, 4}, {11506, 0, 1},
235442 {42560, 1, 46}, {42624, 1, 24}, {42786, 1, 14},
235443 {42802, 1, 62}, {42873, 1, 4}, {42877, 76, 1},
235444 {42878, 1, 10}, {42891, 0, 1}, {42893, 74, 1},
235445 {42896, 1, 4}, {42912, 1, 10}, {42922, 72, 1},
235446 {65313, 14, 26},
235447 };
235448 static const unsigned short aiOff[] = {
235449 1, 2, 8, 15, 16, 26, 28, 32,
235450 37, 38, 40, 48, 63, 64, 69, 71,
235451 79, 80, 116, 202, 203, 205, 206, 207,
235452 209, 210, 211, 213, 214, 217, 218, 219,
235453 775, 7264, 10792, 10795, 23228, 23256, 30204, 54721,
235454 54753, 54754, 54756, 54787, 54793, 54809, 57153, 57274,
235455 57921, 58019, 58363, 61722, 65268, 65341, 65373, 65406,
235456 65408, 65410, 65415, 65424, 65436, 65439, 65450, 65462,
235457 65472, 65476, 65478, 65480, 65482, 65488, 65506, 65511,
235458 65514, 65521, 65527, 65528, 65529,
235459 };
235460
235461 int ret = c;
235462
235463 assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 );
235464
235465 if( c<128 ){
235466 if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
235467 }else if( c<65536 ){
235468 const struct TableEntry *p;
235469 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
235470 int iLo = 0;
235471 int iRes = -1;
235472
235473 assert( c>aEntry[0].iCode );
235474 while( iHi>=iLo ){
235475 int iTest = (iHi + iLo) / 2;
235476 int cmp = (c - aEntry[iTest].iCode);
235477 if( cmp>=0 ){
235478 iRes = iTest;
235479 iLo = iTest+1;
235480 }else{
235481 iHi = iTest-1;
235482 }
235483 }
235484
235485 assert( iRes>=0 && c>=aEntry[iRes].iCode );
235486 p = &aEntry[iRes];
235487 if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
235488 ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
235489 assert( ret>0 );
235490 }
235491
235492 if( eRemoveDiacritic ){
235493 ret = fts5_remove_diacritic(c: ret, bComplex: eRemoveDiacritic==2);
235494 }
235495 }
235496
235497 else if( c>=66560 && c<66600 ){
235498 ret = c + 40;
235499 }
235500
235501 return ret;
235502}
235503
235504
235505static int sqlite3Fts5UnicodeCatParse(const char *zCat, u8 *aArray){
235506 aArray[0] = 1;
235507 switch( zCat[0] ){
235508 case 'C':
235509 switch( zCat[1] ){
235510 case 'c': aArray[1] = 1; break;
235511 case 'f': aArray[2] = 1; break;
235512 case 'n': aArray[3] = 1; break;
235513 case 's': aArray[4] = 1; break;
235514 case 'o': aArray[31] = 1; break;
235515 case '*':
235516 aArray[1] = 1;
235517 aArray[2] = 1;
235518 aArray[3] = 1;
235519 aArray[4] = 1;
235520 aArray[31] = 1;
235521 break;
235522 default: return 1; }
235523 break;
235524
235525 case 'L':
235526 switch( zCat[1] ){
235527 case 'l': aArray[5] = 1; break;
235528 case 'm': aArray[6] = 1; break;
235529 case 'o': aArray[7] = 1; break;
235530 case 't': aArray[8] = 1; break;
235531 case 'u': aArray[9] = 1; break;
235532 case 'C': aArray[30] = 1; break;
235533 case '*':
235534 aArray[5] = 1;
235535 aArray[6] = 1;
235536 aArray[7] = 1;
235537 aArray[8] = 1;
235538 aArray[9] = 1;
235539 aArray[30] = 1;
235540 break;
235541 default: return 1; }
235542 break;
235543
235544 case 'M':
235545 switch( zCat[1] ){
235546 case 'c': aArray[10] = 1; break;
235547 case 'e': aArray[11] = 1; break;
235548 case 'n': aArray[12] = 1; break;
235549 case '*':
235550 aArray[10] = 1;
235551 aArray[11] = 1;
235552 aArray[12] = 1;
235553 break;
235554 default: return 1; }
235555 break;
235556
235557 case 'N':
235558 switch( zCat[1] ){
235559 case 'd': aArray[13] = 1; break;
235560 case 'l': aArray[14] = 1; break;
235561 case 'o': aArray[15] = 1; break;
235562 case '*':
235563 aArray[13] = 1;
235564 aArray[14] = 1;
235565 aArray[15] = 1;
235566 break;
235567 default: return 1; }
235568 break;
235569
235570 case 'P':
235571 switch( zCat[1] ){
235572 case 'c': aArray[16] = 1; break;
235573 case 'd': aArray[17] = 1; break;
235574 case 'e': aArray[18] = 1; break;
235575 case 'f': aArray[19] = 1; break;
235576 case 'i': aArray[20] = 1; break;
235577 case 'o': aArray[21] = 1; break;
235578 case 's': aArray[22] = 1; break;
235579 case '*':
235580 aArray[16] = 1;
235581 aArray[17] = 1;
235582 aArray[18] = 1;
235583 aArray[19] = 1;
235584 aArray[20] = 1;
235585 aArray[21] = 1;
235586 aArray[22] = 1;
235587 break;
235588 default: return 1; }
235589 break;
235590
235591 case 'S':
235592 switch( zCat[1] ){
235593 case 'c': aArray[23] = 1; break;
235594 case 'k': aArray[24] = 1; break;
235595 case 'm': aArray[25] = 1; break;
235596 case 'o': aArray[26] = 1; break;
235597 case '*':
235598 aArray[23] = 1;
235599 aArray[24] = 1;
235600 aArray[25] = 1;
235601 aArray[26] = 1;
235602 break;
235603 default: return 1; }
235604 break;
235605
235606 case 'Z':
235607 switch( zCat[1] ){
235608 case 'l': aArray[27] = 1; break;
235609 case 'p': aArray[28] = 1; break;
235610 case 's': aArray[29] = 1; break;
235611 case '*':
235612 aArray[27] = 1;
235613 aArray[28] = 1;
235614 aArray[29] = 1;
235615 break;
235616 default: return 1; }
235617 break;
235618
235619 }
235620 return 0;
235621}
235622
235623static u16 aFts5UnicodeBlock[] = {
235624 0, 1471, 1753, 1760, 1760, 1760, 1760, 1760, 1760, 1760,
235625 1760, 1760, 1760, 1760, 1760, 1763, 1765,
235626 };
235627static u16 aFts5UnicodeMap[] = {
235628 0, 32, 33, 36, 37, 40, 41, 42, 43, 44,
235629 45, 46, 48, 58, 60, 63, 65, 91, 92, 93,
235630 94, 95, 96, 97, 123, 124, 125, 126, 127, 160,
235631 161, 162, 166, 167, 168, 169, 170, 171, 172, 173,
235632 174, 175, 176, 177, 178, 180, 181, 182, 184, 185,
235633 186, 187, 188, 191, 192, 215, 216, 223, 247, 248,
235634 256, 312, 313, 329, 330, 377, 383, 385, 387, 388,
235635 391, 394, 396, 398, 402, 403, 405, 406, 409, 412,
235636 414, 415, 417, 418, 423, 427, 428, 431, 434, 436,
235637 437, 440, 442, 443, 444, 446, 448, 452, 453, 454,
235638 455, 456, 457, 458, 459, 460, 461, 477, 478, 496,
235639 497, 498, 499, 500, 503, 505, 506, 564, 570, 572,
235640 573, 575, 577, 580, 583, 584, 592, 660, 661, 688,
235641 706, 710, 722, 736, 741, 748, 749, 750, 751, 768,
235642 880, 884, 885, 886, 890, 891, 894, 900, 902, 903,
235643 904, 908, 910, 912, 913, 931, 940, 975, 977, 978,
235644 981, 984, 1008, 1012, 1014, 1015, 1018, 1020, 1021, 1072,
235645 1120, 1154, 1155, 1160, 1162, 1217, 1231, 1232, 1329, 1369,
235646 1370, 1377, 1417, 1418, 1423, 1425, 1470, 1471, 1472, 1473,
235647 1475, 1476, 1478, 1479, 1488, 1520, 1523, 1536, 1542, 1545,
235648 1547, 1548, 1550, 1552, 1563, 1566, 1568, 1600, 1601, 1611,
235649 1632, 1642, 1646, 1648, 1649, 1748, 1749, 1750, 1757, 1758,
235650 1759, 1765, 1767, 1769, 1770, 1774, 1776, 1786, 1789, 1791,
235651 1792, 1807, 1808, 1809, 1810, 1840, 1869, 1958, 1969, 1984,
235652 1994, 2027, 2036, 2038, 2039, 2042, 2048, 2070, 2074, 2075,
235653 2084, 2085, 2088, 2089, 2096, 2112, 2137, 2142, 2208, 2210,
235654 2276, 2304, 2307, 2308, 2362, 2363, 2364, 2365, 2366, 2369,
235655 2377, 2381, 2382, 2384, 2385, 2392, 2402, 2404, 2406, 2416,
235656 2417, 2418, 2425, 2433, 2434, 2437, 2447, 2451, 2474, 2482,
235657 2486, 2492, 2493, 2494, 2497, 2503, 2507, 2509, 2510, 2519,
235658 2524, 2527, 2530, 2534, 2544, 2546, 2548, 2554, 2555, 2561,
235659 2563, 2565, 2575, 2579, 2602, 2610, 2613, 2616, 2620, 2622,
235660 2625, 2631, 2635, 2641, 2649, 2654, 2662, 2672, 2674, 2677,
235661 2689, 2691, 2693, 2703, 2707, 2730, 2738, 2741, 2748, 2749,
235662 2750, 2753, 2759, 2761, 2763, 2765, 2768, 2784, 2786, 2790,
235663 2800, 2801, 2817, 2818, 2821, 2831, 2835, 2858, 2866, 2869,
235664 2876, 2877, 2878, 2879, 2880, 2881, 2887, 2891, 2893, 2902,
235665 2903, 2908, 2911, 2914, 2918, 2928, 2929, 2930, 2946, 2947,
235666 2949, 2958, 2962, 2969, 2972, 2974, 2979, 2984, 2990, 3006,
235667 3008, 3009, 3014, 3018, 3021, 3024, 3031, 3046, 3056, 3059,
235668 3065, 3066, 3073, 3077, 3086, 3090, 3114, 3125, 3133, 3134,
235669 3137, 3142, 3146, 3157, 3160, 3168, 3170, 3174, 3192, 3199,
235670 3202, 3205, 3214, 3218, 3242, 3253, 3260, 3261, 3262, 3263,
235671 3264, 3270, 3271, 3274, 3276, 3285, 3294, 3296, 3298, 3302,
235672 3313, 3330, 3333, 3342, 3346, 3389, 3390, 3393, 3398, 3402,
235673 3405, 3406, 3415, 3424, 3426, 3430, 3440, 3449, 3450, 3458,
235674 3461, 3482, 3507, 3517, 3520, 3530, 3535, 3538, 3542, 3544,
235675 3570, 3572, 3585, 3633, 3634, 3636, 3647, 3648, 3654, 3655,
235676 3663, 3664, 3674, 3713, 3716, 3719, 3722, 3725, 3732, 3737,
235677 3745, 3749, 3751, 3754, 3757, 3761, 3762, 3764, 3771, 3773,
235678 3776, 3782, 3784, 3792, 3804, 3840, 3841, 3844, 3859, 3860,
235679 3861, 3864, 3866, 3872, 3882, 3892, 3893, 3894, 3895, 3896,
235680 3897, 3898, 3899, 3900, 3901, 3902, 3904, 3913, 3953, 3967,
235681 3968, 3973, 3974, 3976, 3981, 3993, 4030, 4038, 4039, 4046,
235682 4048, 4053, 4057, 4096, 4139, 4141, 4145, 4146, 4152, 4153,
235683 4155, 4157, 4159, 4160, 4170, 4176, 4182, 4184, 4186, 4190,
235684 4193, 4194, 4197, 4199, 4206, 4209, 4213, 4226, 4227, 4229,
235685 4231, 4237, 4238, 4239, 4240, 4250, 4253, 4254, 4256, 4295,
235686 4301, 4304, 4347, 4348, 4349, 4682, 4688, 4696, 4698, 4704,
235687 4746, 4752, 4786, 4792, 4800, 4802, 4808, 4824, 4882, 4888,
235688 4957, 4960, 4969, 4992, 5008, 5024, 5120, 5121, 5741, 5743,
235689 5760, 5761, 5787, 5788, 5792, 5867, 5870, 5888, 5902, 5906,
235690 5920, 5938, 5941, 5952, 5970, 5984, 5998, 6002, 6016, 6068,
235691 6070, 6071, 6078, 6086, 6087, 6089, 6100, 6103, 6104, 6107,
235692 6108, 6109, 6112, 6128, 6144, 6150, 6151, 6155, 6158, 6160,
235693 6176, 6211, 6212, 6272, 6313, 6314, 6320, 6400, 6432, 6435,
235694 6439, 6441, 6448, 6450, 6451, 6457, 6464, 6468, 6470, 6480,
235695 6512, 6528, 6576, 6593, 6600, 6608, 6618, 6622, 6656, 6679,
235696 6681, 6686, 6688, 6741, 6742, 6743, 6744, 6752, 6753, 6754,
235697 6755, 6757, 6765, 6771, 6783, 6784, 6800, 6816, 6823, 6824,
235698 6912, 6916, 6917, 6964, 6965, 6966, 6971, 6972, 6973, 6978,
235699 6979, 6981, 6992, 7002, 7009, 7019, 7028, 7040, 7042, 7043,
235700 7073, 7074, 7078, 7080, 7082, 7083, 7084, 7086, 7088, 7098,
235701 7142, 7143, 7144, 7146, 7149, 7150, 7151, 7154, 7164, 7168,
235702 7204, 7212, 7220, 7222, 7227, 7232, 7245, 7248, 7258, 7288,
235703 7294, 7360, 7376, 7379, 7380, 7393, 7394, 7401, 7405, 7406,
235704 7410, 7412, 7413, 7424, 7468, 7531, 7544, 7545, 7579, 7616,
235705 7676, 7680, 7830, 7838, 7936, 7944, 7952, 7960, 7968, 7976,
235706 7984, 7992, 8000, 8008, 8016, 8025, 8027, 8029, 8031, 8033,
235707 8040, 8048, 8064, 8072, 8080, 8088, 8096, 8104, 8112, 8118,
235708 8120, 8124, 8125, 8126, 8127, 8130, 8134, 8136, 8140, 8141,
235709 8144, 8150, 8152, 8157, 8160, 8168, 8173, 8178, 8182, 8184,
235710 8188, 8189, 8192, 8203, 8208, 8214, 8216, 8217, 8218, 8219,
235711 8221, 8222, 8223, 8224, 8232, 8233, 8234, 8239, 8240, 8249,
235712 8250, 8251, 8255, 8257, 8260, 8261, 8262, 8263, 8274, 8275,
235713 8276, 8277, 8287, 8288, 8298, 8304, 8305, 8308, 8314, 8317,
235714 8318, 8319, 8320, 8330, 8333, 8334, 8336, 8352, 8400, 8413,
235715 8417, 8418, 8421, 8448, 8450, 8451, 8455, 8456, 8458, 8459,
235716 8462, 8464, 8467, 8468, 8469, 8470, 8472, 8473, 8478, 8484,
235717 8485, 8486, 8487, 8488, 8489, 8490, 8494, 8495, 8496, 8500,
235718 8501, 8505, 8506, 8508, 8510, 8512, 8517, 8519, 8522, 8523,
235719 8524, 8526, 8527, 8528, 8544, 8579, 8581, 8585, 8592, 8597,
235720 8602, 8604, 8608, 8609, 8611, 8612, 8614, 8615, 8622, 8623,
235721 8654, 8656, 8658, 8659, 8660, 8661, 8692, 8960, 8968, 8972,
235722 8992, 8994, 9001, 9002, 9003, 9084, 9085, 9115, 9140, 9180,
235723 9186, 9216, 9280, 9312, 9372, 9450, 9472, 9655, 9656, 9665,
235724 9666, 9720, 9728, 9839, 9840, 9985, 10088, 10089, 10090, 10091,
235725 10092, 10093, 10094, 10095, 10096, 10097, 10098, 10099, 10100, 10101,
235726 10102, 10132, 10176, 10181, 10182, 10183, 10214, 10215, 10216, 10217,
235727 10218, 10219, 10220, 10221, 10222, 10223, 10224, 10240, 10496, 10627,
235728 10628, 10629, 10630, 10631, 10632, 10633, 10634, 10635, 10636, 10637,
235729 10638, 10639, 10640, 10641, 10642, 10643, 10644, 10645, 10646, 10647,
235730 10648, 10649, 10712, 10713, 10714, 10715, 10716, 10748, 10749, 10750,
235731 11008, 11056, 11077, 11079, 11088, 11264, 11312, 11360, 11363, 11365,
235732 11367, 11374, 11377, 11378, 11380, 11381, 11383, 11388, 11390, 11393,
235733 11394, 11492, 11493, 11499, 11503, 11506, 11513, 11517, 11518, 11520,
235734 11559, 11565, 11568, 11631, 11632, 11647, 11648, 11680, 11688, 11696,
235735 11704, 11712, 11720, 11728, 11736, 11744, 11776, 11778, 11779, 11780,
235736 11781, 11782, 11785, 11786, 11787, 11788, 11789, 11790, 11799, 11800,
235737 11802, 11803, 11804, 11805, 11806, 11808, 11809, 11810, 11811, 11812,
235738 11813, 11814, 11815, 11816, 11817, 11818, 11823, 11824, 11834, 11904,
235739 11931, 12032, 12272, 12288, 12289, 12292, 12293, 12294, 12295, 12296,
235740 12297, 12298, 12299, 12300, 12301, 12302, 12303, 12304, 12305, 12306,
235741 12308, 12309, 12310, 12311, 12312, 12313, 12314, 12315, 12316, 12317,
235742 12318, 12320, 12321, 12330, 12334, 12336, 12337, 12342, 12344, 12347,
235743 12348, 12349, 12350, 12353, 12441, 12443, 12445, 12447, 12448, 12449,
235744 12539, 12540, 12543, 12549, 12593, 12688, 12690, 12694, 12704, 12736,
235745 12784, 12800, 12832, 12842, 12872, 12880, 12881, 12896, 12928, 12938,
235746 12977, 12992, 13056, 13312, 19893, 19904, 19968, 40908, 40960, 40981,
235747 40982, 42128, 42192, 42232, 42238, 42240, 42508, 42509, 42512, 42528,
235748 42538, 42560, 42606, 42607, 42608, 42611, 42612, 42622, 42623, 42624,
235749 42655, 42656, 42726, 42736, 42738, 42752, 42775, 42784, 42786, 42800,
235750 42802, 42864, 42865, 42873, 42878, 42888, 42889, 42891, 42896, 42912,
235751 43000, 43002, 43003, 43010, 43011, 43014, 43015, 43019, 43020, 43043,
235752 43045, 43047, 43048, 43056, 43062, 43064, 43065, 43072, 43124, 43136,
235753 43138, 43188, 43204, 43214, 43216, 43232, 43250, 43256, 43259, 43264,
235754 43274, 43302, 43310, 43312, 43335, 43346, 43359, 43360, 43392, 43395,
235755 43396, 43443, 43444, 43446, 43450, 43452, 43453, 43457, 43471, 43472,
235756 43486, 43520, 43561, 43567, 43569, 43571, 43573, 43584, 43587, 43588,
235757 43596, 43597, 43600, 43612, 43616, 43632, 43633, 43639, 43642, 43643,
235758 43648, 43696, 43697, 43698, 43701, 43703, 43705, 43710, 43712, 43713,
235759 43714, 43739, 43741, 43742, 43744, 43755, 43756, 43758, 43760, 43762,
235760 43763, 43765, 43766, 43777, 43785, 43793, 43808, 43816, 43968, 44003,
235761 44005, 44006, 44008, 44009, 44011, 44012, 44013, 44016, 44032, 55203,
235762 55216, 55243, 55296, 56191, 56319, 57343, 57344, 63743, 63744, 64112,
235763 64256, 64275, 64285, 64286, 64287, 64297, 64298, 64312, 64318, 64320,
235764 64323, 64326, 64434, 64467, 64830, 64831, 64848, 64914, 65008, 65020,
235765 65021, 65024, 65040, 65047, 65048, 65049, 65056, 65072, 65073, 65075,
235766 65077, 65078, 65079, 65080, 65081, 65082, 65083, 65084, 65085, 65086,
235767 65087, 65088, 65089, 65090, 65091, 65092, 65093, 65095, 65096, 65097,
235768 65101, 65104, 65108, 65112, 65113, 65114, 65115, 65116, 65117, 65118,
235769 65119, 65122, 65123, 65124, 65128, 65129, 65130, 65136, 65142, 65279,
235770 65281, 65284, 65285, 65288, 65289, 65290, 65291, 65292, 65293, 65294,
235771 65296, 65306, 65308, 65311, 65313, 65339, 65340, 65341, 65342, 65343,
235772 65344, 65345, 65371, 65372, 65373, 65374, 65375, 65376, 65377, 65378,
235773 65379, 65380, 65382, 65392, 65393, 65438, 65440, 65474, 65482, 65490,
235774 65498, 65504, 65506, 65507, 65508, 65509, 65512, 65513, 65517, 65529,
235775 65532, 0, 13, 40, 60, 63, 80, 128, 256, 263,
235776 311, 320, 373, 377, 394, 400, 464, 509, 640, 672,
235777 768, 800, 816, 833, 834, 842, 896, 927, 928, 968,
235778 976, 977, 1024, 1064, 1104, 1184, 2048, 2056, 2058, 2103,
235779 2108, 2111, 2135, 2136, 2304, 2326, 2335, 2336, 2367, 2432,
235780 2494, 2560, 2561, 2565, 2572, 2576, 2581, 2585, 2616, 2623,
235781 2624, 2640, 2656, 2685, 2687, 2816, 2873, 2880, 2904, 2912,
235782 2936, 3072, 3680, 4096, 4097, 4098, 4099, 4152, 4167, 4178,
235783 4198, 4224, 4226, 4227, 4272, 4275, 4279, 4281, 4283, 4285,
235784 4286, 4304, 4336, 4352, 4355, 4391, 4396, 4397, 4406, 4416,
235785 4480, 4482, 4483, 4531, 4534, 4543, 4545, 4549, 4560, 5760,
235786 5803, 5804, 5805, 5806, 5808, 5814, 5815, 5824, 8192, 9216,
235787 9328, 12288, 26624, 28416, 28496, 28497, 28559, 28563, 45056, 53248,
235788 53504, 53545, 53605, 53607, 53610, 53613, 53619, 53627, 53635, 53637,
235789 53644, 53674, 53678, 53760, 53826, 53829, 54016, 54112, 54272, 54298,
235790 54324, 54350, 54358, 54376, 54402, 54428, 54430, 54434, 54437, 54441,
235791 54446, 54454, 54459, 54461, 54469, 54480, 54506, 54532, 54535, 54541,
235792 54550, 54558, 54584, 54587, 54592, 54598, 54602, 54610, 54636, 54662,
235793 54688, 54714, 54740, 54766, 54792, 54818, 54844, 54870, 54896, 54922,
235794 54952, 54977, 54978, 55003, 55004, 55010, 55035, 55036, 55061, 55062,
235795 55068, 55093, 55094, 55119, 55120, 55126, 55151, 55152, 55177, 55178,
235796 55184, 55209, 55210, 55235, 55236, 55242, 55246, 60928, 60933, 60961,
235797 60964, 60967, 60969, 60980, 60985, 60987, 60994, 60999, 61001, 61003,
235798 61005, 61009, 61012, 61015, 61017, 61019, 61021, 61023, 61025, 61028,
235799 61031, 61036, 61044, 61049, 61054, 61056, 61067, 61089, 61093, 61099,
235800 61168, 61440, 61488, 61600, 61617, 61633, 61649, 61696, 61712, 61744,
235801 61808, 61926, 61968, 62016, 62032, 62208, 62256, 62263, 62336, 62368,
235802 62406, 62432, 62464, 62528, 62530, 62713, 62720, 62784, 62800, 62971,
235803 63045, 63104, 63232, 0, 42710, 42752, 46900, 46912, 47133, 63488,
235804 1, 32, 256, 0, 65533,
235805 };
235806static u16 aFts5UnicodeData[] = {
235807 1025, 61, 117, 55, 117, 54, 50, 53, 57, 53,
235808 49, 85, 333, 85, 121, 85, 841, 54, 53, 50,
235809 56, 48, 56, 837, 54, 57, 50, 57, 1057, 61,
235810 53, 151, 58, 53, 56, 58, 39, 52, 57, 34,
235811 58, 56, 58, 57, 79, 56, 37, 85, 56, 47,
235812 39, 51, 111, 53, 745, 57, 233, 773, 57, 261,
235813 1822, 37, 542, 37, 1534, 222, 69, 73, 37, 126,
235814 126, 73, 69, 137, 37, 73, 37, 105, 101, 73,
235815 37, 73, 37, 190, 158, 37, 126, 126, 73, 37,
235816 126, 94, 37, 39, 94, 69, 135, 41, 40, 37,
235817 41, 40, 37, 41, 40, 37, 542, 37, 606, 37,
235818 41, 40, 37, 126, 73, 37, 1886, 197, 73, 37,
235819 73, 69, 126, 105, 37, 286, 2181, 39, 869, 582,
235820 152, 390, 472, 166, 248, 38, 56, 38, 568, 3596,
235821 158, 38, 56, 94, 38, 101, 53, 88, 41, 53,
235822 105, 41, 73, 37, 553, 297, 1125, 94, 37, 105,
235823 101, 798, 133, 94, 57, 126, 94, 37, 1641, 1541,
235824 1118, 58, 172, 75, 1790, 478, 37, 2846, 1225, 38,
235825 213, 1253, 53, 49, 55, 1452, 49, 44, 53, 76,
235826 53, 76, 53, 44, 871, 103, 85, 162, 121, 85,
235827 55, 85, 90, 364, 53, 85, 1031, 38, 327, 684,
235828 333, 149, 71, 44, 3175, 53, 39, 236, 34, 58,
235829 204, 70, 76, 58, 140, 71, 333, 103, 90, 39,
235830 469, 34, 39, 44, 967, 876, 2855, 364, 39, 333,
235831 1063, 300, 70, 58, 117, 38, 711, 140, 38, 300,
235832 38, 108, 38, 172, 501, 807, 108, 53, 39, 359,
235833 876, 108, 42, 1735, 44, 42, 44, 39, 106, 268,
235834 138, 44, 74, 39, 236, 327, 76, 85, 333, 53,
235835 38, 199, 231, 44, 74, 263, 71, 711, 231, 39,
235836 135, 44, 39, 106, 140, 74, 74, 44, 39, 42,
235837 71, 103, 76, 333, 71, 87, 207, 58, 55, 76,
235838 42, 199, 71, 711, 231, 71, 71, 71, 44, 106,
235839 76, 76, 108, 44, 135, 39, 333, 76, 103, 44,
235840 76, 42, 295, 103, 711, 231, 71, 167, 44, 39,
235841 106, 172, 76, 42, 74, 44, 39, 71, 76, 333,
235842 53, 55, 44, 74, 263, 71, 711, 231, 71, 167,
235843 44, 39, 42, 44, 42, 140, 74, 74, 44, 44,
235844 42, 71, 103, 76, 333, 58, 39, 207, 44, 39,
235845 199, 103, 135, 71, 39, 71, 71, 103, 391, 74,
235846 44, 74, 106, 106, 44, 39, 42, 333, 111, 218,
235847 55, 58, 106, 263, 103, 743, 327, 167, 39, 108,
235848 138, 108, 140, 76, 71, 71, 76, 333, 239, 58,
235849 74, 263, 103, 743, 327, 167, 44, 39, 42, 44,
235850 170, 44, 74, 74, 76, 74, 39, 71, 76, 333,
235851 71, 74, 263, 103, 1319, 39, 106, 140, 106, 106,
235852 44, 39, 42, 71, 76, 333, 207, 58, 199, 74,
235853 583, 775, 295, 39, 231, 44, 106, 108, 44, 266,
235854 74, 53, 1543, 44, 71, 236, 55, 199, 38, 268,
235855 53, 333, 85, 71, 39, 71, 39, 39, 135, 231,
235856 103, 39, 39, 71, 135, 44, 71, 204, 76, 39,
235857 167, 38, 204, 333, 135, 39, 122, 501, 58, 53,
235858 122, 76, 218, 333, 335, 58, 44, 58, 44, 58,
235859 44, 54, 50, 54, 50, 74, 263, 1159, 460, 42,
235860 172, 53, 76, 167, 364, 1164, 282, 44, 218, 90,
235861 181, 154, 85, 1383, 74, 140, 42, 204, 42, 76,
235862 74, 76, 39, 333, 213, 199, 74, 76, 135, 108,
235863 39, 106, 71, 234, 103, 140, 423, 44, 74, 76,
235864 202, 44, 39, 42, 333, 106, 44, 90, 1225, 41,
235865 41, 1383, 53, 38, 10631, 135, 231, 39, 135, 1319,
235866 135, 1063, 135, 231, 39, 135, 487, 1831, 135, 2151,
235867 108, 309, 655, 519, 346, 2727, 49, 19847, 85, 551,
235868 61, 839, 54, 50, 2407, 117, 110, 423, 135, 108,
235869 583, 108, 85, 583, 76, 423, 103, 76, 1671, 76,
235870 42, 236, 266, 44, 74, 364, 117, 38, 117, 55,
235871 39, 44, 333, 335, 213, 49, 149, 108, 61, 333,
235872 1127, 38, 1671, 1319, 44, 39, 2247, 935, 108, 138,
235873 76, 106, 74, 44, 202, 108, 58, 85, 333, 967,
235874 167, 1415, 554, 231, 74, 333, 47, 1114, 743, 76,
235875 106, 85, 1703, 42, 44, 42, 236, 44, 42, 44,
235876 74, 268, 202, 332, 44, 333, 333, 245, 38, 213,
235877 140, 42, 1511, 44, 42, 172, 42, 44, 170, 44,
235878 74, 231, 333, 245, 346, 300, 314, 76, 42, 967,
235879 42, 140, 74, 76, 42, 44, 74, 71, 333, 1415,
235880 44, 42, 76, 106, 44, 42, 108, 74, 149, 1159,
235881 266, 268, 74, 76, 181, 333, 103, 333, 967, 198,
235882 85, 277, 108, 53, 428, 42, 236, 135, 44, 135,
235883 74, 44, 71, 1413, 2022, 421, 38, 1093, 1190, 1260,
235884 140, 4830, 261, 3166, 261, 265, 197, 201, 261, 265,
235885 261, 265, 197, 201, 261, 41, 41, 41, 94, 229,
235886 265, 453, 261, 264, 261, 264, 261, 264, 165, 69,
235887 137, 40, 56, 37, 120, 101, 69, 137, 40, 120,
235888 133, 69, 137, 120, 261, 169, 120, 101, 69, 137,
235889 40, 88, 381, 162, 209, 85, 52, 51, 54, 84,
235890 51, 54, 52, 277, 59, 60, 162, 61, 309, 52,
235891 51, 149, 80, 117, 57, 54, 50, 373, 57, 53,
235892 48, 341, 61, 162, 194, 47, 38, 207, 121, 54,
235893 50, 38, 335, 121, 54, 50, 422, 855, 428, 139,
235894 44, 107, 396, 90, 41, 154, 41, 90, 37, 105,
235895 69, 105, 37, 58, 41, 90, 57, 169, 218, 41,
235896 58, 41, 58, 41, 58, 137, 58, 37, 137, 37,
235897 135, 37, 90, 69, 73, 185, 94, 101, 58, 57,
235898 90, 37, 58, 527, 1134, 94, 142, 47, 185, 186,
235899 89, 154, 57, 90, 57, 90, 57, 250, 57, 1018,
235900 89, 90, 57, 58, 57, 1018, 8601, 282, 153, 666,
235901 89, 250, 54, 50, 2618, 57, 986, 825, 1306, 217,
235902 602, 1274, 378, 1935, 2522, 719, 5882, 57, 314, 57,
235903 1754, 281, 3578, 57, 4634, 3322, 54, 50, 54, 50,
235904 54, 50, 54, 50, 54, 50, 54, 50, 54, 50,
235905 975, 1434, 185, 54, 50, 1017, 54, 50, 54, 50,
235906 54, 50, 54, 50, 54, 50, 537, 8218, 4217, 54,
235907 50, 54, 50, 54, 50, 54, 50, 54, 50, 54,
235908 50, 54, 50, 54, 50, 54, 50, 54, 50, 54,
235909 50, 2041, 54, 50, 54, 50, 1049, 54, 50, 8281,
235910 1562, 697, 90, 217, 346, 1513, 1509, 126, 73, 69,
235911 254, 105, 37, 94, 37, 94, 165, 70, 105, 37,
235912 3166, 37, 218, 158, 108, 94, 149, 47, 85, 1221,
235913 37, 37, 1799, 38, 53, 44, 743, 231, 231, 231,
235914 231, 231, 231, 231, 231, 1036, 85, 52, 51, 52,
235915 51, 117, 52, 51, 53, 52, 51, 309, 49, 85,
235916 49, 53, 52, 51, 85, 52, 51, 54, 50, 54,
235917 50, 54, 50, 54, 50, 181, 38, 341, 81, 858,
235918 2874, 6874, 410, 61, 117, 58, 38, 39, 46, 54,
235919 50, 54, 50, 54, 50, 54, 50, 54, 50, 90,
235920 54, 50, 54, 50, 54, 50, 54, 50, 49, 54,
235921 82, 58, 302, 140, 74, 49, 166, 90, 110, 38,
235922 39, 53, 90, 2759, 76, 88, 70, 39, 49, 2887,
235923 53, 102, 39, 1319, 3015, 90, 143, 346, 871, 1178,
235924 519, 1018, 335, 986, 271, 58, 495, 1050, 335, 1274,
235925 495, 2042, 8218, 39, 39, 2074, 39, 39, 679, 38,
235926 36583, 1786, 1287, 198, 85, 8583, 38, 117, 519, 333,
235927 71, 1502, 39, 44, 107, 53, 332, 53, 38, 798,
235928 44, 2247, 334, 76, 213, 760, 294, 88, 478, 69,
235929 2014, 38, 261, 190, 350, 38, 88, 158, 158, 382,
235930 70, 37, 231, 44, 103, 44, 135, 44, 743, 74,
235931 76, 42, 154, 207, 90, 55, 58, 1671, 149, 74,
235932 1607, 522, 44, 85, 333, 588, 199, 117, 39, 333,
235933 903, 268, 85, 743, 364, 74, 53, 935, 108, 42,
235934 1511, 44, 74, 140, 74, 44, 138, 437, 38, 333,
235935 85, 1319, 204, 74, 76, 74, 76, 103, 44, 263,
235936 44, 42, 333, 149, 519, 38, 199, 122, 39, 42,
235937 1543, 44, 39, 108, 71, 76, 167, 76, 39, 44,
235938 39, 71, 38, 85, 359, 42, 76, 74, 85, 39,
235939 70, 42, 44, 199, 199, 199, 231, 231, 1127, 74,
235940 44, 74, 44, 74, 53, 42, 44, 333, 39, 39,
235941 743, 1575, 36, 68, 68, 36, 63, 63, 11719, 3399,
235942 229, 165, 39, 44, 327, 57, 423, 167, 39, 71,
235943 71, 3463, 536, 11623, 54, 50, 2055, 1735, 391, 55,
235944 58, 524, 245, 54, 50, 53, 236, 53, 81, 80,
235945 54, 50, 54, 50, 54, 50, 54, 50, 54, 50,
235946 54, 50, 54, 50, 54, 50, 85, 54, 50, 149,
235947 112, 117, 149, 49, 54, 50, 54, 50, 54, 50,
235948 117, 57, 49, 121, 53, 55, 85, 167, 4327, 34,
235949 117, 55, 117, 54, 50, 53, 57, 53, 49, 85,
235950 333, 85, 121, 85, 841, 54, 53, 50, 56, 48,
235951 56, 837, 54, 57, 50, 57, 54, 50, 53, 54,
235952 50, 85, 327, 38, 1447, 70, 999, 199, 199, 199,
235953 103, 87, 57, 56, 58, 87, 58, 153, 90, 98,
235954 90, 391, 839, 615, 71, 487, 455, 3943, 117, 1455,
235955 314, 1710, 143, 570, 47, 410, 1466, 44, 935, 1575,
235956 999, 143, 551, 46, 263, 46, 967, 53, 1159, 263,
235957 53, 174, 1289, 1285, 2503, 333, 199, 39, 1415, 71,
235958 39, 743, 53, 271, 711, 207, 53, 839, 53, 1799,
235959 71, 39, 108, 76, 140, 135, 103, 871, 108, 44,
235960 271, 309, 935, 79, 53, 1735, 245, 711, 271, 615,
235961 271, 2343, 1007, 42, 44, 42, 1703, 492, 245, 655,
235962 333, 76, 42, 1447, 106, 140, 74, 76, 85, 34,
235963 149, 807, 333, 108, 1159, 172, 42, 268, 333, 149,
235964 76, 42, 1543, 106, 300, 74, 135, 149, 333, 1383,
235965 44, 42, 44, 74, 204, 42, 44, 333, 28135, 3182,
235966 149, 34279, 18215, 2215, 39, 1482, 140, 422, 71, 7898,
235967 1274, 1946, 74, 108, 122, 202, 258, 268, 90, 236,
235968 986, 140, 1562, 2138, 108, 58, 2810, 591, 841, 837,
235969 841, 229, 581, 841, 837, 41, 73, 41, 73, 137,
235970 265, 133, 37, 229, 357, 841, 837, 73, 137, 265,
235971 233, 837, 73, 137, 169, 41, 233, 837, 841, 837,
235972 841, 837, 841, 837, 841, 837, 841, 837, 841, 901,
235973 809, 57, 805, 57, 197, 809, 57, 805, 57, 197,
235974 809, 57, 805, 57, 197, 809, 57, 805, 57, 197,
235975 809, 57, 805, 57, 197, 94, 1613, 135, 871, 71,
235976 39, 39, 327, 135, 39, 39, 39, 39, 39, 39,
235977 103, 71, 39, 39, 39, 39, 39, 39, 71, 39,
235978 135, 231, 135, 135, 39, 327, 551, 103, 167, 551,
235979 89, 1434, 3226, 506, 474, 506, 506, 367, 1018, 1946,
235980 1402, 954, 1402, 314, 90, 1082, 218, 2266, 666, 1210,
235981 186, 570, 2042, 58, 5850, 154, 2010, 154, 794, 2266,
235982 378, 2266, 3738, 39, 39, 39, 39, 39, 39, 17351,
235983 34, 3074, 7692, 63, 63,
235984 };
235985
235986static int sqlite3Fts5UnicodeCategory(u32 iCode) {
235987 int iRes = -1;
235988 int iHi;
235989 int iLo;
235990 int ret;
235991 u16 iKey;
235992
235993 if( iCode>=(1<<20) ){
235994 return 0;
235995 }
235996 iLo = aFts5UnicodeBlock[(iCode>>16)];
235997 iHi = aFts5UnicodeBlock[1+(iCode>>16)];
235998 iKey = (iCode & 0xFFFF);
235999 while( iHi>iLo ){
236000 int iTest = (iHi + iLo) / 2;
236001 assert( iTest>=iLo && iTest<iHi );
236002 if( iKey>=aFts5UnicodeMap[iTest] ){
236003 iRes = iTest;
236004 iLo = iTest+1;
236005 }else{
236006 iHi = iTest;
236007 }
236008 }
236009
236010 if( iRes<0 ) return 0;
236011 if( iKey>=(aFts5UnicodeMap[iRes]+(aFts5UnicodeData[iRes]>>5)) ) return 0;
236012 ret = aFts5UnicodeData[iRes] & 0x1F;
236013 if( ret!=30 ) return ret;
236014 return ((iKey - aFts5UnicodeMap[iRes]) & 0x01) ? 5 : 9;
236015}
236016
236017static void sqlite3Fts5UnicodeAscii(u8 *aArray, u8 *aAscii){
236018 int i = 0;
236019 int iTbl = 0;
236020 while( i<128 ){
236021 int bToken = aArray[ aFts5UnicodeData[iTbl] & 0x1F ];
236022 int n = (aFts5UnicodeData[iTbl] >> 5) + i;
236023 for(; i<128 && i<n; i++){
236024 aAscii[i] = (u8)bToken;
236025 }
236026 iTbl++;
236027 }
236028 aAscii[0] = 0; /* 0x00 is never a token character */
236029}
236030
236031
236032/*
236033** 2015 May 30
236034**
236035** The author disclaims copyright to this source code. In place of
236036** a legal notice, here is a blessing:
236037**
236038** May you do good and not evil.
236039** May you find forgiveness for yourself and forgive others.
236040** May you share freely, never taking more than you give.
236041**
236042******************************************************************************
236043**
236044** Routines for varint serialization and deserialization.
236045*/
236046
236047
236048/* #include "fts5Int.h" */
236049
236050/*
236051** This is a copy of the sqlite3GetVarint32() routine from the SQLite core.
236052** Except, this version does handle the single byte case that the core
236053** version depends on being handled before its function is called.
236054*/
236055static int sqlite3Fts5GetVarint32(const unsigned char *p, u32 *v){
236056 u32 a,b;
236057
236058 /* The 1-byte case. Overwhelmingly the most common. */
236059 a = *p;
236060 /* a: p0 (unmasked) */
236061 if (!(a&0x80))
236062 {
236063 /* Values between 0 and 127 */
236064 *v = a;
236065 return 1;
236066 }
236067
236068 /* The 2-byte case */
236069 p++;
236070 b = *p;
236071 /* b: p1 (unmasked) */
236072 if (!(b&0x80))
236073 {
236074 /* Values between 128 and 16383 */
236075 a &= 0x7f;
236076 a = a<<7;
236077 *v = a | b;
236078 return 2;
236079 }
236080
236081 /* The 3-byte case */
236082 p++;
236083 a = a<<14;
236084 a |= *p;
236085 /* a: p0<<14 | p2 (unmasked) */
236086 if (!(a&0x80))
236087 {
236088 /* Values between 16384 and 2097151 */
236089 a &= (0x7f<<14)|(0x7f);
236090 b &= 0x7f;
236091 b = b<<7;
236092 *v = a | b;
236093 return 3;
236094 }
236095
236096 /* A 32-bit varint is used to store size information in btrees.
236097 ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
236098 ** A 3-byte varint is sufficient, for example, to record the size
236099 ** of a 1048569-byte BLOB or string.
236100 **
236101 ** We only unroll the first 1-, 2-, and 3- byte cases. The very
236102 ** rare larger cases can be handled by the slower 64-bit varint
236103 ** routine.
236104 */
236105 {
236106 u64 v64;
236107 u8 n;
236108 p -= 2;
236109 n = sqlite3Fts5GetVarint(p, &v64);
236110 *v = ((u32)v64) & 0x7FFFFFFF;
236111 assert( n>3 && n<=9 );
236112 return n;
236113 }
236114}
236115
236116
236117/*
236118** Bitmasks used by sqlite3GetVarint(). These precomputed constants
236119** are defined here rather than simply putting the constant expressions
236120** inline in order to work around bugs in the RVT compiler.
236121**
236122** SLOT_2_0 A mask for (0x7f<<14) | 0x7f
236123**
236124** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0
236125*/
236126#define SLOT_2_0 0x001fc07f
236127#define SLOT_4_2_0 0xf01fc07f
236128
236129/*
236130** Read a 64-bit variable-length integer from memory starting at p[0].
236131** Return the number of bytes read. The value is stored in *v.
236132*/
236133static u8 sqlite3Fts5GetVarint(const unsigned char *p, u64 *v){
236134 u32 a,b,s;
236135
236136 a = *p;
236137 /* a: p0 (unmasked) */
236138 if (!(a&0x80))
236139 {
236140 *v = a;
236141 return 1;
236142 }
236143
236144 p++;
236145 b = *p;
236146 /* b: p1 (unmasked) */
236147 if (!(b&0x80))
236148 {
236149 a &= 0x7f;
236150 a = a<<7;
236151 a |= b;
236152 *v = a;
236153 return 2;
236154 }
236155
236156 /* Verify that constants are precomputed correctly */
236157 assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
236158 assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
236159
236160 p++;
236161 a = a<<14;
236162 a |= *p;
236163 /* a: p0<<14 | p2 (unmasked) */
236164 if (!(a&0x80))
236165 {
236166 a &= SLOT_2_0;
236167 b &= 0x7f;
236168 b = b<<7;
236169 a |= b;
236170 *v = a;
236171 return 3;
236172 }
236173
236174 /* CSE1 from below */
236175 a &= SLOT_2_0;
236176 p++;
236177 b = b<<14;
236178 b |= *p;
236179 /* b: p1<<14 | p3 (unmasked) */
236180 if (!(b&0x80))
236181 {
236182 b &= SLOT_2_0;
236183 /* moved CSE1 up */
236184 /* a &= (0x7f<<14)|(0x7f); */
236185 a = a<<7;
236186 a |= b;
236187 *v = a;
236188 return 4;
236189 }
236190
236191 /* a: p0<<14 | p2 (masked) */
236192 /* b: p1<<14 | p3 (unmasked) */
236193 /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
236194 /* moved CSE1 up */
236195 /* a &= (0x7f<<14)|(0x7f); */
236196 b &= SLOT_2_0;
236197 s = a;
236198 /* s: p0<<14 | p2 (masked) */
236199
236200 p++;
236201 a = a<<14;
236202 a |= *p;
236203 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
236204 if (!(a&0x80))
236205 {
236206 /* we can skip these cause they were (effectively) done above in calc'ing s */
236207 /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
236208 /* b &= (0x7f<<14)|(0x7f); */
236209 b = b<<7;
236210 a |= b;
236211 s = s>>18;
236212 *v = ((u64)s)<<32 | a;
236213 return 5;
236214 }
236215
236216 /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
236217 s = s<<7;
236218 s |= b;
236219 /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
236220
236221 p++;
236222 b = b<<14;
236223 b |= *p;
236224 /* b: p1<<28 | p3<<14 | p5 (unmasked) */
236225 if (!(b&0x80))
236226 {
236227 /* we can skip this cause it was (effectively) done above in calc'ing s */
236228 /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
236229 a &= SLOT_2_0;
236230 a = a<<7;
236231 a |= b;
236232 s = s>>18;
236233 *v = ((u64)s)<<32 | a;
236234 return 6;
236235 }
236236
236237 p++;
236238 a = a<<14;
236239 a |= *p;
236240 /* a: p2<<28 | p4<<14 | p6 (unmasked) */
236241 if (!(a&0x80))
236242 {
236243 a &= SLOT_4_2_0;
236244 b &= SLOT_2_0;
236245 b = b<<7;
236246 a |= b;
236247 s = s>>11;
236248 *v = ((u64)s)<<32 | a;
236249 return 7;
236250 }
236251
236252 /* CSE2 from below */
236253 a &= SLOT_2_0;
236254 p++;
236255 b = b<<14;
236256 b |= *p;
236257 /* b: p3<<28 | p5<<14 | p7 (unmasked) */
236258 if (!(b&0x80))
236259 {
236260 b &= SLOT_4_2_0;
236261 /* moved CSE2 up */
236262 /* a &= (0x7f<<14)|(0x7f); */
236263 a = a<<7;
236264 a |= b;
236265 s = s>>4;
236266 *v = ((u64)s)<<32 | a;
236267 return 8;
236268 }
236269
236270 p++;
236271 a = a<<15;
236272 a |= *p;
236273 /* a: p4<<29 | p6<<15 | p8 (unmasked) */
236274
236275 /* moved CSE2 up */
236276 /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
236277 b &= SLOT_2_0;
236278 b = b<<8;
236279 a |= b;
236280
236281 s = s<<4;
236282 b = p[-4];
236283 b &= 0x7f;
236284 b = b>>3;
236285 s |= b;
236286
236287 *v = ((u64)s)<<32 | a;
236288
236289 return 9;
236290}
236291
236292/*
236293** The variable-length integer encoding is as follows:
236294**
236295** KEY:
236296** A = 0xxxxxxx 7 bits of data and one flag bit
236297** B = 1xxxxxxx 7 bits of data and one flag bit
236298** C = xxxxxxxx 8 bits of data
236299**
236300** 7 bits - A
236301** 14 bits - BA
236302** 21 bits - BBA
236303** 28 bits - BBBA
236304** 35 bits - BBBBA
236305** 42 bits - BBBBBA
236306** 49 bits - BBBBBBA
236307** 56 bits - BBBBBBBA
236308** 64 bits - BBBBBBBBC
236309*/
236310
236311#ifdef SQLITE_NOINLINE
236312# define FTS5_NOINLINE SQLITE_NOINLINE
236313#else
236314# define FTS5_NOINLINE
236315#endif
236316
236317/*
236318** Write a 64-bit variable-length integer to memory starting at p[0].
236319** The length of data write will be between 1 and 9 bytes. The number
236320** of bytes written is returned.
236321**
236322** A variable-length integer consists of the lower 7 bits of each byte
236323** for all bytes that have the 8th bit set and one byte with the 8th
236324** bit clear. Except, if we get to the 9th byte, it stores the full
236325** 8 bits and is the last byte.
236326*/
236327static int FTS5_NOINLINE fts5PutVarint64(unsigned char *p, u64 v){
236328 int i, j, n;
236329 u8 buf[10];
236330 if( v & (((u64)0xff000000)<<32) ){
236331 p[8] = (u8)v;
236332 v >>= 8;
236333 for(i=7; i>=0; i--){
236334 p[i] = (u8)((v & 0x7f) | 0x80);
236335 v >>= 7;
236336 }
236337 return 9;
236338 }
236339 n = 0;
236340 do{
236341 buf[n++] = (u8)((v & 0x7f) | 0x80);
236342 v >>= 7;
236343 }while( v!=0 );
236344 buf[0] &= 0x7f;
236345 assert( n<=9 );
236346 for(i=0, j=n-1; j>=0; j--, i++){
236347 p[i] = buf[j];
236348 }
236349 return n;
236350}
236351
236352static int sqlite3Fts5PutVarint(unsigned char *p, u64 v){
236353 if( v<=0x7f ){
236354 p[0] = v&0x7f;
236355 return 1;
236356 }
236357 if( v<=0x3fff ){
236358 p[0] = ((v>>7)&0x7f)|0x80;
236359 p[1] = v&0x7f;
236360 return 2;
236361 }
236362 return fts5PutVarint64(p,v);
236363}
236364
236365
236366static int sqlite3Fts5GetVarintLen(u32 iVal){
236367#if 0
236368 if( iVal<(1 << 7 ) ) return 1;
236369#endif
236370 assert( iVal>=(1 << 7) );
236371 if( iVal<(1 << 14) ) return 2;
236372 if( iVal<(1 << 21) ) return 3;
236373 if( iVal<(1 << 28) ) return 4;
236374 return 5;
236375}
236376
236377/*
236378** 2015 May 08
236379**
236380** The author disclaims copyright to this source code. In place of
236381** a legal notice, here is a blessing:
236382**
236383** May you do good and not evil.
236384** May you find forgiveness for yourself and forgive others.
236385** May you share freely, never taking more than you give.
236386**
236387******************************************************************************
236388**
236389** This is an SQLite virtual table module implementing direct access to an
236390** existing FTS5 index. The module may create several different types of
236391** tables:
236392**
236393** col:
236394** CREATE TABLE vocab(term, col, doc, cnt, PRIMARY KEY(term, col));
236395**
236396** One row for each term/column combination. The value of $doc is set to
236397** the number of fts5 rows that contain at least one instance of term
236398** $term within column $col. Field $cnt is set to the total number of
236399** instances of term $term in column $col (in any row of the fts5 table).
236400**
236401** row:
236402** CREATE TABLE vocab(term, doc, cnt, PRIMARY KEY(term));
236403**
236404** One row for each term in the database. The value of $doc is set to
236405** the number of fts5 rows that contain at least one instance of term
236406** $term. Field $cnt is set to the total number of instances of term
236407** $term in the database.
236408**
236409** instance:
236410** CREATE TABLE vocab(term, doc, col, offset, PRIMARY KEY(<all-fields>));
236411**
236412** One row for each term instance in the database.
236413*/
236414
236415
236416/* #include "fts5Int.h" */
236417
236418
236419typedef struct Fts5VocabTable Fts5VocabTable;
236420typedef struct Fts5VocabCursor Fts5VocabCursor;
236421
236422struct Fts5VocabTable {
236423 sqlite3_vtab base;
236424 char *zFts5Tbl; /* Name of fts5 table */
236425 char *zFts5Db; /* Db containing fts5 table */
236426 sqlite3 *db; /* Database handle */
236427 Fts5Global *pGlobal; /* FTS5 global object for this database */
236428 int eType; /* FTS5_VOCAB_COL, ROW or INSTANCE */
236429 unsigned bBusy; /* True if busy */
236430};
236431
236432struct Fts5VocabCursor {
236433 sqlite3_vtab_cursor base;
236434 sqlite3_stmt *pStmt; /* Statement holding lock on pIndex */
236435 Fts5Table *pFts5; /* Associated FTS5 table */
236436
236437 int bEof; /* True if this cursor is at EOF */
236438 Fts5IndexIter *pIter; /* Term/rowid iterator object */
236439 void *pStruct; /* From sqlite3Fts5StructureRef() */
236440
236441 int nLeTerm; /* Size of zLeTerm in bytes */
236442 char *zLeTerm; /* (term <= $zLeTerm) paramater, or NULL */
236443
236444 /* These are used by 'col' tables only */
236445 int iCol;
236446 i64 *aCnt;
236447 i64 *aDoc;
236448
236449 /* Output values used by all tables. */
236450 i64 rowid; /* This table's current rowid value */
236451 Fts5Buffer term; /* Current value of 'term' column */
236452
236453 /* Output values Used by 'instance' tables only */
236454 i64 iInstPos;
236455 int iInstOff;
236456};
236457
236458#define FTS5_VOCAB_COL 0
236459#define FTS5_VOCAB_ROW 1
236460#define FTS5_VOCAB_INSTANCE 2
236461
236462#define FTS5_VOCAB_COL_SCHEMA "term, col, doc, cnt"
236463#define FTS5_VOCAB_ROW_SCHEMA "term, doc, cnt"
236464#define FTS5_VOCAB_INST_SCHEMA "term, doc, col, offset"
236465
236466/*
236467** Bits for the mask used as the idxNum value by xBestIndex/xFilter.
236468*/
236469#define FTS5_VOCAB_TERM_EQ 0x01
236470#define FTS5_VOCAB_TERM_GE 0x02
236471#define FTS5_VOCAB_TERM_LE 0x04
236472
236473
236474/*
236475** Translate a string containing an fts5vocab table type to an
236476** FTS5_VOCAB_XXX constant. If successful, set *peType to the output
236477** value and return SQLITE_OK. Otherwise, set *pzErr to an error message
236478** and return SQLITE_ERROR.
236479*/
236480static int fts5VocabTableType(const char *zType, char **pzErr, int *peType){
236481 int rc = SQLITE_OK;
236482 char *zCopy = sqlite3Fts5Strndup(pRc: &rc, pIn: zType, nIn: -1);
236483 if( rc==SQLITE_OK ){
236484 sqlite3Fts5Dequote(z: zCopy);
236485 if( sqlite3_stricmp(zLeft: zCopy, zRight: "col")==0 ){
236486 *peType = FTS5_VOCAB_COL;
236487 }else
236488
236489 if( sqlite3_stricmp(zLeft: zCopy, zRight: "row")==0 ){
236490 *peType = FTS5_VOCAB_ROW;
236491 }else
236492 if( sqlite3_stricmp(zLeft: zCopy, zRight: "instance")==0 ){
236493 *peType = FTS5_VOCAB_INSTANCE;
236494 }else
236495 {
236496 *pzErr = sqlite3_mprintf(zFormat: "fts5vocab: unknown table type: %Q", zCopy);
236497 rc = SQLITE_ERROR;
236498 }
236499 sqlite3_free(p: zCopy);
236500 }
236501
236502 return rc;
236503}
236504
236505
236506/*
236507** The xDisconnect() virtual table method.
236508*/
236509static int fts5VocabDisconnectMethod(sqlite3_vtab *pVtab){
236510 Fts5VocabTable *pTab = (Fts5VocabTable*)pVtab;
236511 sqlite3_free(p: pTab);
236512 return SQLITE_OK;
236513}
236514
236515/*
236516** The xDestroy() virtual table method.
236517*/
236518static int fts5VocabDestroyMethod(sqlite3_vtab *pVtab){
236519 Fts5VocabTable *pTab = (Fts5VocabTable*)pVtab;
236520 sqlite3_free(p: pTab);
236521 return SQLITE_OK;
236522}
236523
236524/*
236525** This function is the implementation of both the xConnect and xCreate
236526** methods of the FTS3 virtual table.
236527**
236528** The argv[] array contains the following:
236529**
236530** argv[0] -> module name ("fts5vocab")
236531** argv[1] -> database name
236532** argv[2] -> table name
236533**
236534** then:
236535**
236536** argv[3] -> name of fts5 table
236537** argv[4] -> type of fts5vocab table
236538**
236539** or, for tables in the TEMP schema only.
236540**
236541** argv[3] -> name of fts5 tables database
236542** argv[4] -> name of fts5 table
236543** argv[5] -> type of fts5vocab table
236544*/
236545static int fts5VocabInitVtab(
236546 sqlite3 *db, /* The SQLite database connection */
236547 void *pAux, /* Pointer to Fts5Global object */
236548 int argc, /* Number of elements in argv array */
236549 const char * const *argv, /* xCreate/xConnect argument array */
236550 sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
236551 char **pzErr /* Write any error message here */
236552){
236553 const char *azSchema[] = {
236554 "CREATE TABlE vocab(" FTS5_VOCAB_COL_SCHEMA ")",
236555 "CREATE TABlE vocab(" FTS5_VOCAB_ROW_SCHEMA ")",
236556 "CREATE TABlE vocab(" FTS5_VOCAB_INST_SCHEMA ")"
236557 };
236558
236559 Fts5VocabTable *pRet = 0;
236560 int rc = SQLITE_OK; /* Return code */
236561 int bDb;
236562
236563 bDb = (argc==6 && strlen(s: argv[1])==4 && memcmp(s1: "temp", s2: argv[1], n: 4)==0);
236564
236565 if( argc!=5 && bDb==0 ){
236566 *pzErr = sqlite3_mprintf(zFormat: "wrong number of vtable arguments");
236567 rc = SQLITE_ERROR;
236568 }else{
236569 int nByte; /* Bytes of space to allocate */
236570 const char *zDb = bDb ? argv[3] : argv[1];
236571 const char *zTab = bDb ? argv[4] : argv[3];
236572 const char *zType = bDb ? argv[5] : argv[4];
236573 int nDb = (int)strlen(s: zDb)+1;
236574 int nTab = (int)strlen(s: zTab)+1;
236575 int eType = 0;
236576
236577 rc = fts5VocabTableType(zType, pzErr, peType: &eType);
236578 if( rc==SQLITE_OK ){
236579 assert( eType>=0 && eType<ArraySize(azSchema) );
236580 rc = sqlite3_declare_vtab(db, zCreateTable: azSchema[eType]);
236581 }
236582
236583 nByte = sizeof(Fts5VocabTable) + nDb + nTab;
236584 pRet = sqlite3Fts5MallocZero(pRc: &rc, nByte);
236585 if( pRet ){
236586 pRet->pGlobal = (Fts5Global*)pAux;
236587 pRet->eType = eType;
236588 pRet->db = db;
236589 pRet->zFts5Tbl = (char*)&pRet[1];
236590 pRet->zFts5Db = &pRet->zFts5Tbl[nTab];
236591 memcpy(dest: pRet->zFts5Tbl, src: zTab, n: nTab);
236592 memcpy(dest: pRet->zFts5Db, src: zDb, n: nDb);
236593 sqlite3Fts5Dequote(z: pRet->zFts5Tbl);
236594 sqlite3Fts5Dequote(z: pRet->zFts5Db);
236595 }
236596 }
236597
236598 *ppVTab = (sqlite3_vtab*)pRet;
236599 return rc;
236600}
236601
236602
236603/*
236604** The xConnect() and xCreate() methods for the virtual table. All the
236605** work is done in function fts5VocabInitVtab().
236606*/
236607static int fts5VocabConnectMethod(
236608 sqlite3 *db, /* Database connection */
236609 void *pAux, /* Pointer to tokenizer hash table */
236610 int argc, /* Number of elements in argv array */
236611 const char * const *argv, /* xCreate/xConnect argument array */
236612 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
236613 char **pzErr /* OUT: sqlite3_malloc'd error message */
236614){
236615 return fts5VocabInitVtab(db, pAux, argc, argv, ppVTab: ppVtab, pzErr);
236616}
236617static int fts5VocabCreateMethod(
236618 sqlite3 *db, /* Database connection */
236619 void *pAux, /* Pointer to tokenizer hash table */
236620 int argc, /* Number of elements in argv array */
236621 const char * const *argv, /* xCreate/xConnect argument array */
236622 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
236623 char **pzErr /* OUT: sqlite3_malloc'd error message */
236624){
236625 return fts5VocabInitVtab(db, pAux, argc, argv, ppVTab: ppVtab, pzErr);
236626}
236627
236628/*
236629** Implementation of the xBestIndex method.
236630**
236631** Only constraints of the form:
236632**
236633** term <= ?
236634** term == ?
236635** term >= ?
236636**
236637** are interpreted. Less-than and less-than-or-equal are treated
236638** identically, as are greater-than and greater-than-or-equal.
236639*/
236640static int fts5VocabBestIndexMethod(
236641 sqlite3_vtab *pUnused,
236642 sqlite3_index_info *pInfo
236643){
236644 int i;
236645 int iTermEq = -1;
236646 int iTermGe = -1;
236647 int iTermLe = -1;
236648 int idxNum = 0;
236649 int nArg = 0;
236650
236651 UNUSED_PARAM(pUnused);
236652
236653 for(i=0; i<pInfo->nConstraint; i++){
236654 struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
236655 if( p->usable==0 ) continue;
236656 if( p->iColumn==0 ){ /* term column */
236657 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ) iTermEq = i;
236658 if( p->op==SQLITE_INDEX_CONSTRAINT_LE ) iTermLe = i;
236659 if( p->op==SQLITE_INDEX_CONSTRAINT_LT ) iTermLe = i;
236660 if( p->op==SQLITE_INDEX_CONSTRAINT_GE ) iTermGe = i;
236661 if( p->op==SQLITE_INDEX_CONSTRAINT_GT ) iTermGe = i;
236662 }
236663 }
236664
236665 if( iTermEq>=0 ){
236666 idxNum |= FTS5_VOCAB_TERM_EQ;
236667 pInfo->aConstraintUsage[iTermEq].argvIndex = ++nArg;
236668 pInfo->estimatedCost = 100;
236669 }else{
236670 pInfo->estimatedCost = 1000000;
236671 if( iTermGe>=0 ){
236672 idxNum |= FTS5_VOCAB_TERM_GE;
236673 pInfo->aConstraintUsage[iTermGe].argvIndex = ++nArg;
236674 pInfo->estimatedCost = pInfo->estimatedCost / 2;
236675 }
236676 if( iTermLe>=0 ){
236677 idxNum |= FTS5_VOCAB_TERM_LE;
236678 pInfo->aConstraintUsage[iTermLe].argvIndex = ++nArg;
236679 pInfo->estimatedCost = pInfo->estimatedCost / 2;
236680 }
236681 }
236682
236683 /* This virtual table always delivers results in ascending order of
236684 ** the "term" column (column 0). So if the user has requested this
236685 ** specifically - "ORDER BY term" or "ORDER BY term ASC" - set the
236686 ** sqlite3_index_info.orderByConsumed flag to tell the core the results
236687 ** are already in sorted order. */
236688 if( pInfo->nOrderBy==1
236689 && pInfo->aOrderBy[0].iColumn==0
236690 && pInfo->aOrderBy[0].desc==0
236691 ){
236692 pInfo->orderByConsumed = 1;
236693 }
236694
236695 pInfo->idxNum = idxNum;
236696 return SQLITE_OK;
236697}
236698
236699/*
236700** Implementation of xOpen method.
236701*/
236702static int fts5VocabOpenMethod(
236703 sqlite3_vtab *pVTab,
236704 sqlite3_vtab_cursor **ppCsr
236705){
236706 Fts5VocabTable *pTab = (Fts5VocabTable*)pVTab;
236707 Fts5Table *pFts5 = 0;
236708 Fts5VocabCursor *pCsr = 0;
236709 int rc = SQLITE_OK;
236710 sqlite3_stmt *pStmt = 0;
236711 char *zSql = 0;
236712
236713 if( pTab->bBusy ){
236714 pVTab->zErrMsg = sqlite3_mprintf(
236715 zFormat: "recursive definition for %s.%s", pTab->zFts5Db, pTab->zFts5Tbl
236716 );
236717 return SQLITE_ERROR;
236718 }
236719 zSql = sqlite3Fts5Mprintf(pRc: &rc,
236720 zFmt: "SELECT t.%Q FROM %Q.%Q AS t WHERE t.%Q MATCH '*id'",
236721 pTab->zFts5Tbl, pTab->zFts5Db, pTab->zFts5Tbl, pTab->zFts5Tbl
236722 );
236723 if( zSql ){
236724 rc = sqlite3_prepare_v2(db: pTab->db, zSql, nBytes: -1, ppStmt: &pStmt, pzTail: 0);
236725 }
236726 sqlite3_free(p: zSql);
236727 assert( rc==SQLITE_OK || pStmt==0 );
236728 if( rc==SQLITE_ERROR ) rc = SQLITE_OK;
236729
236730 pTab->bBusy = 1;
236731 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
236732 i64 iId = sqlite3_column_int64(pStmt, i: 0);
236733 pFts5 = sqlite3Fts5TableFromCsrid(pGlobal: pTab->pGlobal, iCsrId: iId);
236734 }
236735 pTab->bBusy = 0;
236736
236737 if( rc==SQLITE_OK ){
236738 if( pFts5==0 ){
236739 rc = sqlite3_finalize(pStmt);
236740 pStmt = 0;
236741 if( rc==SQLITE_OK ){
236742 pVTab->zErrMsg = sqlite3_mprintf(
236743 zFormat: "no such fts5 table: %s.%s", pTab->zFts5Db, pTab->zFts5Tbl
236744 );
236745 rc = SQLITE_ERROR;
236746 }
236747 }else{
236748 rc = sqlite3Fts5FlushToDisk(pTab: pFts5);
236749 }
236750 }
236751
236752 if( rc==SQLITE_OK ){
236753 i64 nByte = pFts5->pConfig->nCol * sizeof(i64)*2 + sizeof(Fts5VocabCursor);
236754 pCsr = (Fts5VocabCursor*)sqlite3Fts5MallocZero(pRc: &rc, nByte);
236755 }
236756
236757 if( pCsr ){
236758 pCsr->pFts5 = pFts5;
236759 pCsr->pStmt = pStmt;
236760 pCsr->aCnt = (i64*)&pCsr[1];
236761 pCsr->aDoc = &pCsr->aCnt[pFts5->pConfig->nCol];
236762 }else{
236763 sqlite3_finalize(pStmt);
236764 }
236765
236766 *ppCsr = (sqlite3_vtab_cursor*)pCsr;
236767 return rc;
236768}
236769
236770static void fts5VocabResetCursor(Fts5VocabCursor *pCsr){
236771 pCsr->rowid = 0;
236772 sqlite3Fts5IterClose(pIndexIter: pCsr->pIter);
236773 sqlite3Fts5StructureRelease(p: pCsr->pStruct);
236774 pCsr->pStruct = 0;
236775 pCsr->pIter = 0;
236776 sqlite3_free(p: pCsr->zLeTerm);
236777 pCsr->nLeTerm = -1;
236778 pCsr->zLeTerm = 0;
236779 pCsr->bEof = 0;
236780}
236781
236782/*
236783** Close the cursor. For additional information see the documentation
236784** on the xClose method of the virtual table interface.
236785*/
236786static int fts5VocabCloseMethod(sqlite3_vtab_cursor *pCursor){
236787 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
236788 fts5VocabResetCursor(pCsr);
236789 sqlite3Fts5BufferFree(pBuf: &pCsr->term);
236790 sqlite3_finalize(pStmt: pCsr->pStmt);
236791 sqlite3_free(p: pCsr);
236792 return SQLITE_OK;
236793}
236794
236795static int fts5VocabInstanceNewTerm(Fts5VocabCursor *pCsr){
236796 int rc = SQLITE_OK;
236797
236798 if( sqlite3Fts5IterEof(pCsr->pIter) ){
236799 pCsr->bEof = 1;
236800 }else{
236801 const char *zTerm;
236802 int nTerm;
236803 zTerm = sqlite3Fts5IterTerm(pIndexIter: pCsr->pIter, pn: &nTerm);
236804 if( pCsr->nLeTerm>=0 ){
236805 int nCmp = MIN(nTerm, pCsr->nLeTerm);
236806 int bCmp = memcmp(s1: pCsr->zLeTerm, s2: zTerm, n: nCmp);
236807 if( bCmp<0 || (bCmp==0 && pCsr->nLeTerm<nTerm) ){
236808 pCsr->bEof = 1;
236809 }
236810 }
236811
236812 sqlite3Fts5BufferSet(pRc: &rc, pBuf: &pCsr->term, nData: nTerm, pData: (const u8*)zTerm);
236813 }
236814 return rc;
236815}
236816
236817static int fts5VocabInstanceNext(Fts5VocabCursor *pCsr){
236818 int eDetail = pCsr->pFts5->pConfig->eDetail;
236819 int rc = SQLITE_OK;
236820 Fts5IndexIter *pIter = pCsr->pIter;
236821 i64 *pp = &pCsr->iInstPos;
236822 int *po = &pCsr->iInstOff;
236823
236824 assert( sqlite3Fts5IterEof(pIter)==0 );
236825 assert( pCsr->bEof==0 );
236826 while( eDetail==FTS5_DETAIL_NONE
236827 || sqlite3Fts5PoslistNext64(a: pIter->pData, n: pIter->nData, pi: po, piOff: pp)
236828 ){
236829 pCsr->iInstPos = 0;
236830 pCsr->iInstOff = 0;
236831
236832 rc = sqlite3Fts5IterNextScan(pIndexIter: pCsr->pIter);
236833 if( rc==SQLITE_OK ){
236834 rc = fts5VocabInstanceNewTerm(pCsr);
236835 if( pCsr->bEof || eDetail==FTS5_DETAIL_NONE ) break;
236836 }
236837 if( rc ){
236838 pCsr->bEof = 1;
236839 break;
236840 }
236841 }
236842
236843 return rc;
236844}
236845
236846/*
236847** Advance the cursor to the next row in the table.
236848*/
236849static int fts5VocabNextMethod(sqlite3_vtab_cursor *pCursor){
236850 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
236851 Fts5VocabTable *pTab = (Fts5VocabTable*)pCursor->pVtab;
236852 int nCol = pCsr->pFts5->pConfig->nCol;
236853 int rc;
236854
236855 rc = sqlite3Fts5StructureTest(p: pCsr->pFts5->pIndex, pStruct: pCsr->pStruct);
236856 if( rc!=SQLITE_OK ) return rc;
236857 pCsr->rowid++;
236858
236859 if( pTab->eType==FTS5_VOCAB_INSTANCE ){
236860 return fts5VocabInstanceNext(pCsr);
236861 }
236862
236863 if( pTab->eType==FTS5_VOCAB_COL ){
236864 for(pCsr->iCol++; pCsr->iCol<nCol; pCsr->iCol++){
236865 if( pCsr->aDoc[pCsr->iCol] ) break;
236866 }
236867 }
236868
236869 if( pTab->eType!=FTS5_VOCAB_COL || pCsr->iCol>=nCol ){
236870 if( sqlite3Fts5IterEof(pCsr->pIter) ){
236871 pCsr->bEof = 1;
236872 }else{
236873 const char *zTerm;
236874 int nTerm;
236875
236876 zTerm = sqlite3Fts5IterTerm(pIndexIter: pCsr->pIter, pn: &nTerm);
236877 assert( nTerm>=0 );
236878 if( pCsr->nLeTerm>=0 ){
236879 int nCmp = MIN(nTerm, pCsr->nLeTerm);
236880 int bCmp = memcmp(s1: pCsr->zLeTerm, s2: zTerm, n: nCmp);
236881 if( bCmp<0 || (bCmp==0 && pCsr->nLeTerm<nTerm) ){
236882 pCsr->bEof = 1;
236883 return SQLITE_OK;
236884 }
236885 }
236886
236887 sqlite3Fts5BufferSet(pRc: &rc, pBuf: &pCsr->term, nData: nTerm, pData: (const u8*)zTerm);
236888 memset(s: pCsr->aCnt, c: 0, n: nCol * sizeof(i64));
236889 memset(s: pCsr->aDoc, c: 0, n: nCol * sizeof(i64));
236890 pCsr->iCol = 0;
236891
236892 assert( pTab->eType==FTS5_VOCAB_COL || pTab->eType==FTS5_VOCAB_ROW );
236893 while( rc==SQLITE_OK ){
236894 int eDetail = pCsr->pFts5->pConfig->eDetail;
236895 const u8 *pPos; int nPos; /* Position list */
236896 i64 iPos = 0; /* 64-bit position read from poslist */
236897 int iOff = 0; /* Current offset within position list */
236898
236899 pPos = pCsr->pIter->pData;
236900 nPos = pCsr->pIter->nData;
236901
236902 switch( pTab->eType ){
236903 case FTS5_VOCAB_ROW:
236904 if( eDetail==FTS5_DETAIL_FULL ){
236905 while( 0==sqlite3Fts5PoslistNext64(a: pPos, n: nPos, pi: &iOff, piOff: &iPos) ){
236906 pCsr->aCnt[0]++;
236907 }
236908 }
236909 pCsr->aDoc[0]++;
236910 break;
236911
236912 case FTS5_VOCAB_COL:
236913 if( eDetail==FTS5_DETAIL_FULL ){
236914 int iCol = -1;
236915 while( 0==sqlite3Fts5PoslistNext64(a: pPos, n: nPos, pi: &iOff, piOff: &iPos) ){
236916 int ii = FTS5_POS2COLUMN(iPos);
236917 if( iCol!=ii ){
236918 if( ii>=nCol ){
236919 rc = FTS5_CORRUPT;
236920 break;
236921 }
236922 pCsr->aDoc[ii]++;
236923 iCol = ii;
236924 }
236925 pCsr->aCnt[ii]++;
236926 }
236927 }else if( eDetail==FTS5_DETAIL_COLUMNS ){
236928 while( 0==sqlite3Fts5PoslistNext64(a: pPos, n: nPos, pi: &iOff,piOff: &iPos) ){
236929 assert_nc( iPos>=0 && iPos<nCol );
236930 if( iPos>=nCol ){
236931 rc = FTS5_CORRUPT;
236932 break;
236933 }
236934 pCsr->aDoc[iPos]++;
236935 }
236936 }else{
236937 assert( eDetail==FTS5_DETAIL_NONE );
236938 pCsr->aDoc[0]++;
236939 }
236940 break;
236941
236942 default:
236943 assert( pTab->eType==FTS5_VOCAB_INSTANCE );
236944 break;
236945 }
236946
236947 if( rc==SQLITE_OK ){
236948 rc = sqlite3Fts5IterNextScan(pIndexIter: pCsr->pIter);
236949 }
236950 if( pTab->eType==FTS5_VOCAB_INSTANCE ) break;
236951
236952 if( rc==SQLITE_OK ){
236953 zTerm = sqlite3Fts5IterTerm(pIndexIter: pCsr->pIter, pn: &nTerm);
236954 if( nTerm!=pCsr->term.n
236955 || (nTerm>0 && memcmp(s1: zTerm, s2: pCsr->term.p, n: nTerm))
236956 ){
236957 break;
236958 }
236959 if( sqlite3Fts5IterEof(pCsr->pIter) ) break;
236960 }
236961 }
236962 }
236963 }
236964
236965 if( rc==SQLITE_OK && pCsr->bEof==0 && pTab->eType==FTS5_VOCAB_COL ){
236966 for(/* noop */; pCsr->iCol<nCol && pCsr->aDoc[pCsr->iCol]==0; pCsr->iCol++);
236967 if( pCsr->iCol==nCol ){
236968 rc = FTS5_CORRUPT;
236969 }
236970 }
236971 return rc;
236972}
236973
236974/*
236975** This is the xFilter implementation for the virtual table.
236976*/
236977static int fts5VocabFilterMethod(
236978 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
236979 int idxNum, /* Strategy index */
236980 const char *zUnused, /* Unused */
236981 int nUnused, /* Number of elements in apVal */
236982 sqlite3_value **apVal /* Arguments for the indexing scheme */
236983){
236984 Fts5VocabTable *pTab = (Fts5VocabTable*)pCursor->pVtab;
236985 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
236986 int eType = pTab->eType;
236987 int rc = SQLITE_OK;
236988
236989 int iVal = 0;
236990 int f = FTS5INDEX_QUERY_SCAN;
236991 const char *zTerm = 0;
236992 int nTerm = 0;
236993
236994 sqlite3_value *pEq = 0;
236995 sqlite3_value *pGe = 0;
236996 sqlite3_value *pLe = 0;
236997
236998 UNUSED_PARAM2(zUnused, nUnused);
236999
237000 fts5VocabResetCursor(pCsr);
237001 if( idxNum & FTS5_VOCAB_TERM_EQ ) pEq = apVal[iVal++];
237002 if( idxNum & FTS5_VOCAB_TERM_GE ) pGe = apVal[iVal++];
237003 if( idxNum & FTS5_VOCAB_TERM_LE ) pLe = apVal[iVal++];
237004
237005 if( pEq ){
237006 zTerm = (const char *)sqlite3_value_text(pVal: pEq);
237007 nTerm = sqlite3_value_bytes(pVal: pEq);
237008 f = 0;
237009 }else{
237010 if( pGe ){
237011 zTerm = (const char *)sqlite3_value_text(pVal: pGe);
237012 nTerm = sqlite3_value_bytes(pVal: pGe);
237013 }
237014 if( pLe ){
237015 const char *zCopy = (const char *)sqlite3_value_text(pVal: pLe);
237016 if( zCopy==0 ) zCopy = "";
237017 pCsr->nLeTerm = sqlite3_value_bytes(pVal: pLe);
237018 pCsr->zLeTerm = sqlite3_malloc(n: pCsr->nLeTerm+1);
237019 if( pCsr->zLeTerm==0 ){
237020 rc = SQLITE_NOMEM;
237021 }else{
237022 memcpy(dest: pCsr->zLeTerm, src: zCopy, n: pCsr->nLeTerm+1);
237023 }
237024 }
237025 }
237026
237027 if( rc==SQLITE_OK ){
237028 Fts5Index *pIndex = pCsr->pFts5->pIndex;
237029 rc = sqlite3Fts5IndexQuery(p: pIndex, pToken: zTerm, nToken: nTerm, flags: f, pColset: 0, ppIter: &pCsr->pIter);
237030 if( rc==SQLITE_OK ){
237031 pCsr->pStruct = sqlite3Fts5StructureRef(p: pIndex);
237032 }
237033 }
237034 if( rc==SQLITE_OK && eType==FTS5_VOCAB_INSTANCE ){
237035 rc = fts5VocabInstanceNewTerm(pCsr);
237036 }
237037 if( rc==SQLITE_OK && !pCsr->bEof
237038 && (eType!=FTS5_VOCAB_INSTANCE
237039 || pCsr->pFts5->pConfig->eDetail!=FTS5_DETAIL_NONE)
237040 ){
237041 rc = fts5VocabNextMethod(pCursor);
237042 }
237043
237044 return rc;
237045}
237046
237047/*
237048** This is the xEof method of the virtual table. SQLite calls this
237049** routine to find out if it has reached the end of a result set.
237050*/
237051static int fts5VocabEofMethod(sqlite3_vtab_cursor *pCursor){
237052 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
237053 return pCsr->bEof;
237054}
237055
237056static int fts5VocabColumnMethod(
237057 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
237058 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
237059 int iCol /* Index of column to read value from */
237060){
237061 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
237062 int eDetail = pCsr->pFts5->pConfig->eDetail;
237063 int eType = ((Fts5VocabTable*)(pCursor->pVtab))->eType;
237064 i64 iVal = 0;
237065
237066 if( iCol==0 ){
237067 sqlite3_result_text(
237068 pCtx, z: (const char*)pCsr->term.p, n: pCsr->term.n, SQLITE_TRANSIENT
237069 );
237070 }else if( eType==FTS5_VOCAB_COL ){
237071 assert( iCol==1 || iCol==2 || iCol==3 );
237072 if( iCol==1 ){
237073 if( eDetail!=FTS5_DETAIL_NONE ){
237074 const char *z = pCsr->pFts5->pConfig->azCol[pCsr->iCol];
237075 sqlite3_result_text(pCtx, z, n: -1, SQLITE_STATIC);
237076 }
237077 }else if( iCol==2 ){
237078 iVal = pCsr->aDoc[pCsr->iCol];
237079 }else{
237080 iVal = pCsr->aCnt[pCsr->iCol];
237081 }
237082 }else if( eType==FTS5_VOCAB_ROW ){
237083 assert( iCol==1 || iCol==2 );
237084 if( iCol==1 ){
237085 iVal = pCsr->aDoc[0];
237086 }else{
237087 iVal = pCsr->aCnt[0];
237088 }
237089 }else{
237090 assert( eType==FTS5_VOCAB_INSTANCE );
237091 switch( iCol ){
237092 case 1:
237093 sqlite3_result_int64(pCtx, iVal: pCsr->pIter->iRowid);
237094 break;
237095 case 2: {
237096 int ii = -1;
237097 if( eDetail==FTS5_DETAIL_FULL ){
237098 ii = FTS5_POS2COLUMN(pCsr->iInstPos);
237099 }else if( eDetail==FTS5_DETAIL_COLUMNS ){
237100 ii = (int)pCsr->iInstPos;
237101 }
237102 if( ii>=0 && ii<pCsr->pFts5->pConfig->nCol ){
237103 const char *z = pCsr->pFts5->pConfig->azCol[ii];
237104 sqlite3_result_text(pCtx, z, n: -1, SQLITE_STATIC);
237105 }
237106 break;
237107 }
237108 default: {
237109 assert( iCol==3 );
237110 if( eDetail==FTS5_DETAIL_FULL ){
237111 int ii = FTS5_POS2OFFSET(pCsr->iInstPos);
237112 sqlite3_result_int(pCtx, iVal: ii);
237113 }
237114 break;
237115 }
237116 }
237117 }
237118
237119 if( iVal>0 ) sqlite3_result_int64(pCtx, iVal);
237120 return SQLITE_OK;
237121}
237122
237123/*
237124** This is the xRowid method. The SQLite core calls this routine to
237125** retrieve the rowid for the current row of the result set. The
237126** rowid should be written to *pRowid.
237127*/
237128static int fts5VocabRowidMethod(
237129 sqlite3_vtab_cursor *pCursor,
237130 sqlite_int64 *pRowid
237131){
237132 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
237133 *pRowid = pCsr->rowid;
237134 return SQLITE_OK;
237135}
237136
237137static int sqlite3Fts5VocabInit(Fts5Global *pGlobal, sqlite3 *db){
237138 static const sqlite3_module fts5Vocab = {
237139 /* iVersion */ 2,
237140 /* xCreate */ fts5VocabCreateMethod,
237141 /* xConnect */ fts5VocabConnectMethod,
237142 /* xBestIndex */ fts5VocabBestIndexMethod,
237143 /* xDisconnect */ fts5VocabDisconnectMethod,
237144 /* xDestroy */ fts5VocabDestroyMethod,
237145 /* xOpen */ fts5VocabOpenMethod,
237146 /* xClose */ fts5VocabCloseMethod,
237147 /* xFilter */ fts5VocabFilterMethod,
237148 /* xNext */ fts5VocabNextMethod,
237149 /* xEof */ fts5VocabEofMethod,
237150 /* xColumn */ fts5VocabColumnMethod,
237151 /* xRowid */ fts5VocabRowidMethod,
237152 /* xUpdate */ 0,
237153 /* xBegin */ 0,
237154 /* xSync */ 0,
237155 /* xCommit */ 0,
237156 /* xRollback */ 0,
237157 /* xFindFunction */ 0,
237158 /* xRename */ 0,
237159 /* xSavepoint */ 0,
237160 /* xRelease */ 0,
237161 /* xRollbackTo */ 0,
237162 /* xShadowName */ 0
237163 };
237164 void *p = (void*)pGlobal;
237165
237166 return sqlite3_create_module_v2(db, zName: "fts5vocab", pModule: &fts5Vocab, pAux: p, xDestroy: 0);
237167}
237168
237169
237170
237171#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS5) */
237172
237173/************** End of fts5.c ************************************************/
237174/************** Begin file stmt.c ********************************************/
237175/*
237176** 2017-05-31
237177**
237178** The author disclaims copyright to this source code. In place of
237179** a legal notice, here is a blessing:
237180**
237181** May you do good and not evil.
237182** May you find forgiveness for yourself and forgive others.
237183** May you share freely, never taking more than you give.
237184**
237185*************************************************************************
237186**
237187** This file demonstrates an eponymous virtual table that returns information
237188** about all prepared statements for the database connection.
237189**
237190** Usage example:
237191**
237192** .load ./stmt
237193** .mode line
237194** .header on
237195** SELECT * FROM stmt;
237196*/
237197#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB)
237198#if !defined(SQLITEINT_H)
237199/* #include "sqlite3ext.h" */
237200#endif
237201SQLITE_EXTENSION_INIT1
237202/* #include <assert.h> */
237203/* #include <string.h> */
237204
237205#ifndef SQLITE_OMIT_VIRTUALTABLE
237206
237207/* stmt_vtab is a subclass of sqlite3_vtab which will
237208** serve as the underlying representation of a stmt virtual table
237209*/
237210typedef struct stmt_vtab stmt_vtab;
237211struct stmt_vtab {
237212 sqlite3_vtab base; /* Base class - must be first */
237213 sqlite3 *db; /* Database connection for this stmt vtab */
237214};
237215
237216/* stmt_cursor is a subclass of sqlite3_vtab_cursor which will
237217** serve as the underlying representation of a cursor that scans
237218** over rows of the result
237219*/
237220typedef struct stmt_cursor stmt_cursor;
237221struct stmt_cursor {
237222 sqlite3_vtab_cursor base; /* Base class - must be first */
237223 sqlite3 *db; /* Database connection for this cursor */
237224 sqlite3_stmt *pStmt; /* Statement cursor is currently pointing at */
237225 sqlite3_int64 iRowid; /* The rowid */
237226};
237227
237228/*
237229** The stmtConnect() method is invoked to create a new
237230** stmt_vtab that describes the stmt virtual table.
237231**
237232** Think of this routine as the constructor for stmt_vtab objects.
237233**
237234** All this routine needs to do is:
237235**
237236** (1) Allocate the stmt_vtab object and initialize all fields.
237237**
237238** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
237239** result set of queries against stmt will look like.
237240*/
237241static int stmtConnect(
237242 sqlite3 *db,
237243 void *pAux,
237244 int argc, const char *const*argv,
237245 sqlite3_vtab **ppVtab,
237246 char **pzErr
237247){
237248 stmt_vtab *pNew;
237249 int rc;
237250
237251/* Column numbers */
237252#define STMT_COLUMN_SQL 0 /* SQL for the statement */
237253#define STMT_COLUMN_NCOL 1 /* Number of result columns */
237254#define STMT_COLUMN_RO 2 /* True if read-only */
237255#define STMT_COLUMN_BUSY 3 /* True if currently busy */
237256#define STMT_COLUMN_NSCAN 4 /* SQLITE_STMTSTATUS_FULLSCAN_STEP */
237257#define STMT_COLUMN_NSORT 5 /* SQLITE_STMTSTATUS_SORT */
237258#define STMT_COLUMN_NAIDX 6 /* SQLITE_STMTSTATUS_AUTOINDEX */
237259#define STMT_COLUMN_NSTEP 7 /* SQLITE_STMTSTATUS_VM_STEP */
237260#define STMT_COLUMN_REPREP 8 /* SQLITE_STMTSTATUS_REPREPARE */
237261#define STMT_COLUMN_RUN 9 /* SQLITE_STMTSTATUS_RUN */
237262#define STMT_COLUMN_MEM 10 /* SQLITE_STMTSTATUS_MEMUSED */
237263
237264
237265 rc = sqlite3_declare_vtab(db,
237266 "CREATE TABLE x(sql,ncol,ro,busy,nscan,nsort,naidx,nstep,"
237267 "reprep,run,mem)");
237268 if( rc==SQLITE_OK ){
237269 pNew = sqlite3_malloc( sizeof(*pNew) );
237270 *ppVtab = (sqlite3_vtab*)pNew;
237271 if( pNew==0 ) return SQLITE_NOMEM;
237272 memset(pNew, 0, sizeof(*pNew));
237273 pNew->db = db;
237274 }
237275 return rc;
237276}
237277
237278/*
237279** This method is the destructor for stmt_cursor objects.
237280*/
237281static int stmtDisconnect(sqlite3_vtab *pVtab){
237282 sqlite3_free(pVtab);
237283 return SQLITE_OK;
237284}
237285
237286/*
237287** Constructor for a new stmt_cursor object.
237288*/
237289static int stmtOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
237290 stmt_cursor *pCur;
237291 pCur = sqlite3_malloc( sizeof(*pCur) );
237292 if( pCur==0 ) return SQLITE_NOMEM;
237293 memset(pCur, 0, sizeof(*pCur));
237294 pCur->db = ((stmt_vtab*)p)->db;
237295 *ppCursor = &pCur->base;
237296 return SQLITE_OK;
237297}
237298
237299/*
237300** Destructor for a stmt_cursor.
237301*/
237302static int stmtClose(sqlite3_vtab_cursor *cur){
237303 sqlite3_free(cur);
237304 return SQLITE_OK;
237305}
237306
237307
237308/*
237309** Advance a stmt_cursor to its next row of output.
237310*/
237311static int stmtNext(sqlite3_vtab_cursor *cur){
237312 stmt_cursor *pCur = (stmt_cursor*)cur;
237313 pCur->iRowid++;
237314 pCur->pStmt = sqlite3_next_stmt(pCur->db, pCur->pStmt);
237315 return SQLITE_OK;
237316}
237317
237318/*
237319** Return values of columns for the row at which the stmt_cursor
237320** is currently pointing.
237321*/
237322static int stmtColumn(
237323 sqlite3_vtab_cursor *cur, /* The cursor */
237324 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
237325 int i /* Which column to return */
237326){
237327 stmt_cursor *pCur = (stmt_cursor*)cur;
237328 switch( i ){
237329 case STMT_COLUMN_SQL: {
237330 sqlite3_result_text(ctx, sqlite3_sql(pCur->pStmt), -1, SQLITE_TRANSIENT);
237331 break;
237332 }
237333 case STMT_COLUMN_NCOL: {
237334 sqlite3_result_int(ctx, sqlite3_column_count(pCur->pStmt));
237335 break;
237336 }
237337 case STMT_COLUMN_RO: {
237338 sqlite3_result_int(ctx, sqlite3_stmt_readonly(pCur->pStmt));
237339 break;
237340 }
237341 case STMT_COLUMN_BUSY: {
237342 sqlite3_result_int(ctx, sqlite3_stmt_busy(pCur->pStmt));
237343 break;
237344 }
237345 default: {
237346 assert( i==STMT_COLUMN_MEM );
237347 i = SQLITE_STMTSTATUS_MEMUSED +
237348 STMT_COLUMN_NSCAN - SQLITE_STMTSTATUS_FULLSCAN_STEP;
237349 /* Fall thru */
237350 }
237351 case STMT_COLUMN_NSCAN:
237352 case STMT_COLUMN_NSORT:
237353 case STMT_COLUMN_NAIDX:
237354 case STMT_COLUMN_NSTEP:
237355 case STMT_COLUMN_REPREP:
237356 case STMT_COLUMN_RUN: {
237357 sqlite3_result_int(ctx, sqlite3_stmt_status(pCur->pStmt,
237358 i-STMT_COLUMN_NSCAN+SQLITE_STMTSTATUS_FULLSCAN_STEP, 0));
237359 break;
237360 }
237361 }
237362 return SQLITE_OK;
237363}
237364
237365/*
237366** Return the rowid for the current row. In this implementation, the
237367** rowid is the same as the output value.
237368*/
237369static int stmtRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
237370 stmt_cursor *pCur = (stmt_cursor*)cur;
237371 *pRowid = pCur->iRowid;
237372 return SQLITE_OK;
237373}
237374
237375/*
237376** Return TRUE if the cursor has been moved off of the last
237377** row of output.
237378*/
237379static int stmtEof(sqlite3_vtab_cursor *cur){
237380 stmt_cursor *pCur = (stmt_cursor*)cur;
237381 return pCur->pStmt==0;
237382}
237383
237384/*
237385** This method is called to "rewind" the stmt_cursor object back
237386** to the first row of output. This method is always called at least
237387** once prior to any call to stmtColumn() or stmtRowid() or
237388** stmtEof().
237389*/
237390static int stmtFilter(
237391 sqlite3_vtab_cursor *pVtabCursor,
237392 int idxNum, const char *idxStr,
237393 int argc, sqlite3_value **argv
237394){
237395 stmt_cursor *pCur = (stmt_cursor *)pVtabCursor;
237396 pCur->pStmt = 0;
237397 pCur->iRowid = 0;
237398 return stmtNext(pVtabCursor);
237399}
237400
237401/*
237402** SQLite will invoke this method one or more times while planning a query
237403** that uses the stmt virtual table. This routine needs to create
237404** a query plan for each invocation and compute an estimated cost for that
237405** plan.
237406*/
237407static int stmtBestIndex(
237408 sqlite3_vtab *tab,
237409 sqlite3_index_info *pIdxInfo
237410){
237411 pIdxInfo->estimatedCost = (double)500;
237412 pIdxInfo->estimatedRows = 500;
237413 return SQLITE_OK;
237414}
237415
237416/*
237417** This following structure defines all the methods for the
237418** stmt virtual table.
237419*/
237420static sqlite3_module stmtModule = {
237421 0, /* iVersion */
237422 0, /* xCreate */
237423 stmtConnect, /* xConnect */
237424 stmtBestIndex, /* xBestIndex */
237425 stmtDisconnect, /* xDisconnect */
237426 0, /* xDestroy */
237427 stmtOpen, /* xOpen - open a cursor */
237428 stmtClose, /* xClose - close a cursor */
237429 stmtFilter, /* xFilter - configure scan constraints */
237430 stmtNext, /* xNext - advance a cursor */
237431 stmtEof, /* xEof - check for end of scan */
237432 stmtColumn, /* xColumn - read data */
237433 stmtRowid, /* xRowid - read data */
237434 0, /* xUpdate */
237435 0, /* xBegin */
237436 0, /* xSync */
237437 0, /* xCommit */
237438 0, /* xRollback */
237439 0, /* xFindMethod */
237440 0, /* xRename */
237441 0, /* xSavepoint */
237442 0, /* xRelease */
237443 0, /* xRollbackTo */
237444 0, /* xShadowName */
237445};
237446
237447#endif /* SQLITE_OMIT_VIRTUALTABLE */
237448
237449SQLITE_PRIVATE int sqlite3StmtVtabInit(sqlite3 *db){
237450 int rc = SQLITE_OK;
237451#ifndef SQLITE_OMIT_VIRTUALTABLE
237452 rc = sqlite3_create_module(db, "sqlite_stmt", &stmtModule, 0);
237453#endif
237454 return rc;
237455}
237456
237457#ifndef SQLITE_CORE
237458#ifdef _WIN32
237459__declspec(dllexport)
237460#endif
237461SQLITE_API int sqlite3_stmt_init(
237462 sqlite3 *db,
237463 char **pzErrMsg,
237464 const sqlite3_api_routines *pApi
237465){
237466 int rc = SQLITE_OK;
237467 SQLITE_EXTENSION_INIT2(pApi);
237468#ifndef SQLITE_OMIT_VIRTUALTABLE
237469 rc = sqlite3StmtVtabInit(db);
237470#endif
237471 return rc;
237472}
237473#endif /* SQLITE_CORE */
237474#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
237475
237476/************** End of stmt.c ************************************************/
237477/* Return the source-id for this library */
237478SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
237479/************************** End of sqlite3.c ******************************/
237480

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